text
stringlengths
0
601k
let alloc_set fpad descr size tag = if size = 0 then ( fpad . need_xalloc <- true ; let ptr = " xalloc " in let young = false in ( alloc_atom fpad tag @ push_local ptr , ptr , young ) ) else alloc_non_atom fpad descr size tag
let grab_helper gpad = let fpad = empty_fpad ( ) in let descr = empty_descr in assert ( descr . stack_save_accu = false ) ; [ L ( [ [ K " func " ; ID " grab_helper " ; L [ K " param " ; ID " envptr " ; K " i32 " ] ; L [ K " param " ; ID " extra_args " ; K " i32 " ] ; L [ K " param " ; ID " codeptr " ; K " i32 " ] ; L [ K " param " ; ID " fp " ; K " i32 " ] ; BR ; L [ K " result " ; K " i32 " ] ; L [ K " local " ; ID " accu " ; K " i32 " ] ; L [ K " local " ; ID " i " ; K " i32 " ] ; ] ; setup_for_gc fpad descr ; [ L [ K " local . get " ; ID " extra_args " ] ; L [ K " i32 . const " ; N ( I32 4l ) ] ; L [ K " i32 . add " ] ; L [ K " i32 . const " ; N ( I32 ( Int32 . of_int closure_tag ) ) ] ; L [ K " call " ; ID " caml_alloc_small " ] ; L [ K " local . set " ; ID " accu " ] ; ] ; restore_after_gc fpad descr ; ( push_env |> pop_to_field " accu " 2 ) ; push_const 0l ; pop_to_local " i " ; [ L [ K " loop " ; ID " fields " ; BR ; L [ K " local . get " ; ID " accu " ] ; L [ K " local . get " ; ID " i " ] ; L [ K " i32 . const " ; N ( I32 3l ) ] ; L [ K " i32 . add " ] ; L [ K " i32 . const " ; N ( I32 2l ) ] ; L [ K " i32 . shl " ] ; L [ K " i32 . add " ] ; L [ K " local . get " ; ID " fp " ] ; L [ K " local . get " ; ID " i " ] ; L [ K " i32 . const " ; N ( I32 2l ) ] ; L [ K " i32 . shl " ] ; L [ K " i32 . add " ] ; L [ K " i32 . load " ; K " align = 2 " ] ; L [ K " i32 . store " ; K " align = 2 " ] ; L [ K " local . get " ; ID " i " ] ; L [ K " i32 . const " ; N ( I32 1l ) ] ; L [ K " i32 . add " ] ; L [ K " local . tee " ; ID " i " ] ; L [ K " local . get " ; ID " extra_args " ] ; L [ K " i32 . le_u " ] ; L [ K " br_if " ; ID " fields " ] ; ] ] ; ( ( push_local " codeptr " @ [ L [ K " i32 . const " ; N ( I32 code_pointer_restart_mask ) ] ; L [ K " i32 . or " ] ] ) |> pop_to_field " accu " 0 ) ; ( push_const 5l |> pop_to_field " accu " 1 ) ; push_local " accu " ; [ L [ K " return " ] ] ] |> List . flatten ) ]
let restart_helper gpad = [ L ( [ [ K " func " ; ID " restart_helper " ; L [ K " param " ; ID " envptr " ; K " i32 " ] ; L [ K " param " ; ID " extra_args " ; K " i32 " ] ; L [ K " param " ; ID " fp " ; K " i32 " ] ; BR ; L [ K " result " ; C " out_extra_args " ; K " i32 " ] ; ] ; if ! enable_multireturn then [ L [ K " result " ; C " out_fp " ; K " i32 " ] ; ] else [ ] ; [ L [ K " local " ; ID " i " ; K " i32 " ] ; L [ K " local " ; ID " num_args " ; K " i32 " ] ; ] ; push_env ; [ L [ K " i32 . const " ; N ( I32 4l ) ] ; L [ K " i32 . sub " ] ; L [ K " i32 . load " ; K " align = 2 " ] ; L [ K " i32 . const " ; N ( I32 10l ) ] ; L [ K " i32 . shr_u " ] ; L [ K " i32 . const " ; N ( I32 3l ) ] ; L [ K " i32 . sub " ] ; L [ K " local . set " ; ID " num_args " ] ; ] ; [ L [ K " local . get " ; ID " fp " ] ; L [ K " local . get " ; ID " num_args " ] ; L [ K " i32 . const " ; N ( I32 2l ) ] ; L [ K " i32 . shl " ] ; L [ K " i32 . sub " ] ; L [ K " local . set " ; ID " fp " ] ; ] ; [ L [ K " i32 . const " ; N ( I32 0l ) ] ; L [ K " local . set " ; ID " i " ] ; L ( [ [ K " loop " ; ID " args " ; BR ; L [ K " local . get " ; ID " fp " ] ; L [ K " local . get " ; ID " i " ] ; L [ K " i32 . const " ; N ( I32 2l ) ] ; L [ K " i32 . shl " ] ; L [ K " i32 . add " ] ] ; push_env ; [ L [ K " local . get " ; ID " i " ] ; L [ K " i32 . const " ; N ( I32 3l ) ] ; L [ K " i32 . add " ] ; L [ K " i32 . const " ; N ( I32 2l ) ] ; L [ K " i32 . shl " ] ; L [ K " i32 . add " ] ; L [ K " i32 . load " ; K " align = 2 " ] ; ] ; [ L [ K " i32 . store " ; K " align = 2 " ] ] ; [ L [ K " local . get " ; ID " i " ] ; L [ K " i32 . const " ; N ( I32 1l ) ] ; L [ K " i32 . add " ] ; L [ K " local . tee " ; ID " i " ] ; L [ K " local . get " ; ID " num_args " ] ; L [ K " i32 . lt_u " ] ; L [ K " br_if " ; ID " args " ] ; ] ] |> List . flatten ) ] ; [ L [ K " local . get " ; ID " envptr " ] ; L [ K " local . get " ; ID " envptr " ] ; L [ K " i32 . load " ; K " align = 2 " ] ; L [ K " i32 . load " ; K " offset = 8 " ; K " align = 2 " ] ; L [ K " i32 . store " ; K " align = 2 " ] ] ; [ L [ K " local . get " ; ID " extra_args " ] ; L [ K " local . get " ; ID " num_args " ] ; L [ K " i32 . add " ] ; L [ K " local . set " ; ID " extra_args " ] ; ] ; push_local " extra_args " ; push_local " fp " ; if ! enable_multireturn then [ ] else [ L [ K " global . set " ; ID " retval2 " ] ] ; [ L [ K " return " ] ] ; ] |> List . flatten ) ]
let call_restart_helper ( ) = [ L [ K " call " ; ID " restart_helper " ] ] @ if ! enable_multireturn then [ ] else [ L [ K " global . get " ; ID " retval2 " ] ]
let reinit_frame = [ L [ K " func " ; ID " reinit_frame " ; L [ K " param " ; ID " fp " ; K " i32 " ] ; L [ K " param " ; ID " depth " ; K " i32 " ] ; L [ K " param " ; ID " old_num_args " ; K " i32 " ] ; L [ K " param " ; ID " new_num_args " ; K " i32 " ] ; BR ; L [ K " result " ; C " out_fp " ; K " i32 " ] ; L [ K " local " ; ID " i " ; K " i32 " ] ; L [ K " local " ; ID " new_fp " ; K " i32 " ] ; L [ K " local . get " ; ID " fp " ] ; L [ K " local . get " ; ID " old_num_args " ] ; L [ K " local . get " ; ID " new_num_args " ] ; L [ K " i32 . sub " ] ; L [ K " i32 . const " ; N ( I32 2l ) ] ; L [ K " i32 . shl " ] ; L [ K " i32 . add " ] ; L [ K " local . set " ; ID " new_fp " ] ; L [ K " local . get " ; ID " new_num_args " ] ; L [ K " local . set " ; ID " i " ] ; L [ K " loop " ; ID " args " ; BR ; L [ K " local . get " ; ID " i " ] ; L [ K " i32 . const " ; N ( I32 1l ) ] ; L [ K " i32 . sub " ] ; L [ K " local . set " ; ID " i " ] ; L [ K " local . get " ; ID " new_fp " ] ; L [ K " local . get " ; ID " i " ] ; L [ K " i32 . const " ; N ( I32 2l ) ] ; L [ K " i32 . shl " ] ; L [ K " i32 . add " ] ; L [ K " local . get " ; ID " fp " ] ; L [ K " local . get " ; ID " depth " ] ; L [ K " local . get " ; ID " i " ] ; L [ K " i32 . sub " ] ; L [ K " i32 . const " ; N ( I32 2l ) ] ; L [ K " i32 . shl " ] ; L [ K " i32 . sub " ] ; L [ K " i32 . load " ; K " align = 2 " ] ; L [ K " i32 . store " ; K " align = 2 " ] ; L [ K " local . get " ; ID " i " ] ; L [ K " br_if " ; ID " args " ] ; ] ; L [ K " local . get " ; ID " new_fp " ] ; L [ K " return " ] ] ; ]
let reinit_frame_k new_num_args = [ L ( [ K " func " ; ID ( sprintf " reinit_frame_ % d " new_num_args ) ; L [ K " param " ; ID " fp " ; K " i32 " ] ; L [ K " param " ; ID " depth " ; K " i32 " ] ; L [ K " param " ; ID " old_num_args " ; K " i32 " ] ; BR ; L [ K " result " ; C " out_fp " ; K " i32 " ] ; L [ K " local " ; ID " i " ; K " i32 " ] ; L [ K " local " ; ID " bp " ; K " i32 " ] ; L [ K " local " ; ID " new_fp " ; K " i32 " ] ; L [ K " local . get " ; ID " fp " ] ; L [ K " local . get " ; ID " old_num_args " ] ; L [ K " i32 . const " ; N ( I32 ( Int32 . of_int new_num_args ) ) ] ; L [ K " i32 . sub " ] ; L [ K " i32 . const " ; N ( I32 2l ) ] ; L [ K " i32 . shl " ] ; L [ K " i32 . add " ] ; L [ K " local . set " ; ID " new_fp " ] ; L [ K " local . get " ; ID " fp " ] ; L [ K " local . get " ; ID " depth " ] ; L [ K " i32 . const " ; N ( I32 2l ) ] ; L [ K " i32 . shl " ] ; L [ K " i32 . sub " ] ; L [ K " local . set " ; ID " bp " ] ; ] @ ( Wc_util . enum 0 new_num_args |> List . map ( fun j -> let i = new_num_args - 1 - j in [ L [ K " local . get " ; ID " new_fp " ] ; L [ K " local . get " ; ID " bp " ] ; L [ K " i32 . load " ; K ( sprintf " offset = 0x % x " ( 4 * i ) ) ; K " align = 2 " ] ; L [ K " i32 . store " ; K ( sprintf " offset = 0x % x " ( 4 * i ) ) ; K " align = 2 " ] ; ] ) |> List . flatten ) @ [ L [ K " local . get " ; ID " new_fp " ] ; L [ K " return " ] ] ) ]
let return_helper = [ L ( [ [ K " func " ; ID " return_helper " ; L [ K " param " ; ID " envptr " ; K " i32 " ] ; L [ K " param " ; ID " extra_args " ; K " i32 " ] ; L [ K " param " ; ID " fp " ; K " i32 " ] ; L [ K " param " ; ID " accu " ; K " i32 " ] ; BR ; L [ K " result " ; K " i32 " ] ; L [ K " local " ; ID " codeptr " ; K " i32 " ] ; ] ; ( push_local " accu " |> pop_to_field " envptr " 0 ) ; [ L [ K " local . get " ; ID " envptr " ] ; L [ K " local . get " ; ID " extra_args " ] ; L [ K " i32 . const " ; N ( I32 1l ) ] ; L [ K " i32 . sub " ] ; ] ; push_field " accu " 0 ; [ L [ K " local . tee " ; ID " codeptr " ] ] ; [ L [ K " local . get " ; ID " fp " ] ; L [ K " local . get " ; ID " codeptr " ] ; L [ K " i32 . const " ; N ( I32 ( Int32 . of_int code_pointer_shift ) ) ] ; L [ K " i32 . shr_u " ] ; L [ K " call_indirect " ; N ( I32 0l ) ; L [ K " param " ; K " i32 " ] ; L [ K " param " ; K " i32 " ] ; L [ K " param " ; K " i32 " ] ; L [ K " param " ; K " i32 " ] ; L [ K " result " ; K " i32 " ] ; ] ; L [ K " return " ] ] ] |> List . flatten ) ]
let appterm_helper ( ) = [ L ( [ [ K " func " ; ID " appterm_helper " ; L [ K " param " ; ID " envptr " ; K " i32 " ] ; L [ K " param " ; ID " codeptr " ; K " i32 " ] ; L [ K " param " ; ID " accu " ; K " i32 " ] ; L [ K " param " ; ID " extra_args " ; K " i32 " ] ; L [ K " param " ; ID " new_num_args " ; K " i32 " ] ; BR ; L [ K " result " ; C " out_codeptr " ; K " i32 " ] ; ] ; if ! enable_multireturn then [ L [ K " result " ; C " out_extra_args " ; K " i32 " ] ] else [ ] ; [ L [ K " local " ; ID " out_codeptr " ; K " i32 " ] ] ; push_local " extra_args " ; push_local " new_num_args " ; [ L [ K " i32 . add " ] ] ; pop_to_local " extra_args " ; ( push_local " accu " |> pop_to_field " envptr " 0 ) ; push_field " accu " 0 ; pop_to_local " out_codeptr " ; [ L [ K " local . get " ; ID " out_codeptr " ] ; L [ K " i32 . const " ; N ( I32 code_pointer_letrec_mask ) ] ; L [ K " i32 . and " ] ; L [ K " local . get " ; ID " codeptr " ] ; L [ K " i32 . const " ; N ( I32 code_pointer_letrec_mask ) ] ; L [ K " i32 . and " ] ; L [ K " i32 . eq " ] ; L [ K " if " ; L ( [ K " then " ; L [ K " local . get " ; ID " out_codeptr " ] ; L [ K " local . get " ; ID " extra_args " ] ; L [ K " i32 . const " ; N ( I32 1l ) ] ; L [ K " i32 . sub " ] ] @ ( if ! enable_multireturn then [ ] else [ L [ K " global . set " ; ID " retval2 " ] ] ) @ [ L [ K " return " ] ] ) ; L ( [ K " else " ; L [ K " i32 . const " ; N ( I32 0l ) ] ; L [ K " local . get " ; ID " extra_args " ] ] @ ( if ! enable_multireturn then [ ] else [ L [ K " global . set " ; ID " retval2 " ] ] ) @ [ L [ K " return " ] ] ) ] ; L [ K " unreachable " ] ] ] |> List . flatten ) ]
let call_appterm_helper ( ) = [ L [ K " call " ; ID " appterm_helper " ] ] @ if ! enable_multireturn then [ ] else [ L [ K " global . get " ; ID " retval2 " ] ]
let mlookup = [ L ( [ [ K " func " ; ID " mlookup " ; L [ K " param " ; ID " obj " ; K " i32 " ] ; L [ K " param " ; ID " tag " ; K " i32 " ] ; L [ K " param " ; ID " cache " ; K " i32 " ] ; BR ; L [ K " result " ; C " method " ; K " i32 " ] ; L [ K " local " ; ID " meths " ; K " i32 " ] ; L [ K " local " ; ID " ofs " ; K " i32 " ] ; L [ K " local " ; ID " li " ; K " i32 " ] ; L [ K " local " ; ID " hi " ; K " i32 " ] ; L [ K " local " ; ID " mi " ; K " i32 " ] ; ] ; [ L [ K " local . get " ; ID " obj " ] ; L [ K " i32 . load " ; K " align = 2 " ] ; L [ K " local . set " ; ID " meths " ] ; ] ; [ L [ K " local . get " ; ID " cache " ] ; L [ K " if " ; L [ K " then " ; L [ K " local . get " ; ID " cache " ] ; L [ K " i32 . load " ; K " align = 2 " ] ; L [ K " local . get " ; ID " meths " ] ; L [ K " i32 . load " ; K " offset = 0x4 " ; K " align = 2 " ] ; L [ K " i32 . and " ] ; L [ K " local . tee " ; ID " ofs " ] ; L [ K " local . get " ; ID " meths " ] ; L [ K " i32 . const " ; N ( I32 12l ) ] ; L [ K " i32 . add " ] ; L [ K " i32 . add " ] ; L [ K " i32 . load " ; K " align = 2 " ] ; L [ K " local . get " ; ID " tag " ] ; L [ K " i32 . eq " ] ; L [ K " if " ; L [ K " then " ; L [ K " local . get " ; ID " meths " ] ; L [ K " i32 . const " ; N ( I32 8l ) ] ; L [ K " i32 . add " ] ; L [ K " local . get " ; ID " ofs " ] ; L [ K " i32 . add " ] ; L [ K " i32 . load " ; K " align = 2 " ] ; L [ K " return " ] ; ] ] ] ] ] ; [ L [ K " i32 . const " ; N ( I32 3l ) ] ; L [ K " local . set " ; ID " li " ] ; ] ; [ L [ K " local . get " ; ID " meths " ] ; L [ K " i32 . load " ; K " align = 2 " ] ; L [ K " local . set " ; ID " hi " ] ; ] ; [ L [ K " block " ; ID " loop_exit " ; BR ; L [ K " loop " ; ID " loop " ; BR ; L [ K " local . get " ; ID " li " ] ; L [ K " local . get " ; ID " hi " ] ; L [ K " i32 . ge_u " ] ; L [ K " br_if " ; ID " loop_exit " ] ; L [ K " local . get " ; ID " li " ] ; L [ K " local . get " ; ID " hi " ] ; L [ K " i32 . add " ] ; L [ K " i32 . const " ; N ( I32 1l ) ] ; L [ K " i32 . shr_s " ] ; L [ K " i32 . const " ; N ( I32 1l ) ] ; L [ K " i32 . or " ] ; L [ K " local . set " ; ID " mi " ] ; L [ K " local . get " ; ID " tag " ] ; L [ K " local . get " ; ID " meths " ] ; L [ K " local . get " ; ID " mi " ] ; L [ K " i32 . const " ; N ( I32 2l ) ] ; L [ K " i32 . shl " ] ; L [ K " i32 . add " ] ; L [ K " i32 . load " ; K " align = 2 " ] ; L [ K " i32 . lt_s " ] ; L [ K " if " ; L [ K " then " ; L [ K " local . get " ; ID " mi " ] ; L [ K " i32 . const " ; N ( I32 2l ) ] ; L [ K " i32 . sub " ] ; L [ K " local . set " ; ID " hi " ] ; ] ; L [ K " else " ; L [ K " local . get " ; ID " mi " ] ; L [ K " local . set " ; ID " li " ] ; ] ] ; L [ K " br " ; ID " loop " ] ; ] ] ] ; [ L [ K " local . get " ; ID " cache " ] ; L [ K " if " ; L [ K " then " ; L [ K " local . get " ; ID " cache " ] ; L [ K " local . get " ; ID " li " ] ; L [ K " i32 . const " ; N ( I32 3l ) ] ; L [ K " i32 . sub " ] ; L [ K " i32 . const " ; N ( I32 4l ) ] ; L [ K " i32 . mul " ] ; L [ K " i32 . store " ; K " align = 2 " ] ; ] ] ] ; [ L [ K " local . get " ; ID " meths " ] ; L [ K " local . get " ; ID " li " ] ; L [ K " i32 . const " ; N ( I32 1l ) ] ; L [ K " i32 . sub " ] ; L [ K " i32 . const " ; N ( I32 2l ) ] ; L [ K " i32 . shl " ] ; L [ K " i32 . add " ] ; L [ K " i32 . load " ; K " align = 2 " ] ; L [ K " return " ] ; ] ] |> List . flatten ) ]
let wasicaml_get_data = [ L [ K " func " ; ID " wasicaml_get_data " ; L [ K " result " ; K " i32 " ] ; BR ; L [ K " i32 . const " ; ID " data " ] ; L [ K " return " ] ] ]
let wasicaml_get_data_size size = [ L [ K " func " ; ID " wasicaml_get_data_size " ; L [ K " result " ; K " i32 " ] ; BR ; L [ K " i32 . const " ; N ( I32 ( Int32 . of_int size ) ) ] ; L [ K " return " ] ] ]
let wasicaml_init = [ L [ K " func " ; ID " wasicaml_init " ; BR ; L [ K " call " ; ID " wasicaml_get_global_data " ] ; L [ K " global . set " ; ID " wasicaml_global_data " ] ; L [ K " call " ; ID " wasicaml_get_domain_state " ] ; L [ K " global . set " ; ID " wasicaml_domain_state " ] ; L [ K " call " ; ID " wasicaml_get_atom_table " ] ; L [ K " global . set " ; ID " wasicaml_atom_table " ] ; L [ K " global . get " ; ID " wasicaml_domain_state " ] ; L [ K " i32 . load " ; K ( sprintf " offset = 0x % lx " ( Int32 . of_int ( 8 * domain_field_stack_threshold ) ) ) ; K " align = 2 " ; ] ; L [ K " global . set " ; ID " wasicaml_stack_threshold " ] ; L [ K " return " ] ] ]
let tovalue_alloc fpad repr descr_opt = match repr with | RValue | RIntVal -> [ ] | RInt | RIntUnclean -> [ L [ K " i32 . const " ; N ( I32 1l ) ; ] ; L [ K " i32 . shl " ] ; L [ K " i32 . const " ; N ( I32 1l ) ; ] ; L [ K " i32 . or " ] ; ] | RFloat -> ( match descr_opt with | None -> failwith " cannot convert to double w / o stack descr " | Some descr -> let ( instrs_alloc , ptr , _ ) = alloc_set fpad descr double_size double_tag in let local = req_tmp1_f64 fpad in let instrs = [ L [ K " local . set " ; ID local ] ] @ instrs_alloc @ [ L [ K " local . get " ; ID ptr ] ; L [ K " local . get " ; ID local ] ; L [ K " f64 . store " ; K " align = 2 " ] ; L [ K " local . get " ; ID ptr ] ; ] in instrs ) | _ -> assert false
let tovalue fpad repr = tovalue_alloc fpad repr None
let toint repr = match repr with | RInt -> [ ] | RIntUnclean -> [ L [ K " i32 . const " ; N ( I32 1l ) ] ; L [ K " i32 . shl " ] ; L [ K " i32 . const " ; N ( I32 1l ) ] ; L [ K " i32 . shr_s " ] ; ] | RValue | RIntVal -> [ L [ K " i32 . const " ; N ( I32 1l ) ] ; L [ K " i32 . shr_s " ] ; ] | _ -> assert false
let tointunclean repr = match repr with | RIntUnclean -> [ ] | _ -> toint repr
let tofloat repr = match repr with | RValue -> load_double | _ -> assert false
let convert fpad repr_from repr_to descr_opt = match repr_to with | RValue | RIntVal -> tovalue_alloc fpad repr_from descr_opt | RInt -> toint repr_from | RIntUnclean -> tointunclean repr_from | RFloat -> tofloat repr_from | _ -> assert false
let push_global offset = [ L [ K " global . get " ; ID " wasicaml_global_data " ; ] ; L [ K " i32 . load " ] ; L [ K " i32 . load " ; K ( sprintf " offset = 0x % lx " ( Int32 . of_int ( 4 * offset ) ) ) ; K " align = 2 " ; ] ]
let follow_path path = List . map ( fun field -> L [ K " i32 . load " ; K ( sprintf " offset = 0x % lx " ( Int32 . of_int ( 4 * field ) ) ) ; K " align = 2 " ; ] ) path
let push fpad store = match store with | RealAccu _ -> push_local " accu " | Local ( repr , name ) -> push_local name | Const x -> push_const ( Int32 . of_int x ) | RealStack pos -> push_stack fpad pos | Atom tag -> alloc_atom fpad tag | TracedGlobal ( Glb glb_offset , path , _ ) -> push_global glb_offset @ follow_path path | TracedGlobal ( Env env_offset , path , _ ) -> push_env @ add_offset ( 4 * env_offset ) @ follow_path path | Invalid -> assert false
let push_alloc_as fpad store req_repr descr_opt = match store , req_repr with | Const x , ( RValue | RIntVal ) -> push_const ( Int32 . logor ( Int32 . shift_left ( Int32 . of_int x ) 1 ) 1l ) | Local ( RInt , name ) , ( RValue | RIntVal ) -> push_local name @ push_local name @ [ L [ K " i32 . add " ] ; L [ K " i32 . const " ; N ( I32 1l ) ] ; L [ K " i32 . or " ] ] | _ -> let sexpl_push = push fpad store in let repr = repr_of_store store in sexpl_push @ convert fpad repr req_repr descr_opt
let push_as fpad store req_repr = push_alloc_as fpad store req_repr None
let pop_to fpad store repr descr_opt code_value = match store with | RealAccu _ -> code_value @ tovalue_alloc fpad repr descr_opt @ pop_to_local " accu " | Local ( lrepr , name ) -> code_value @ convert fpad repr lrepr descr_opt @ pop_to_local name | RealStack pos -> ( code_value @ tovalue_alloc fpad repr descr_opt ) |> pop_to_stack fpad pos | _ -> assert false
let copy fpad src dest descr_opt = match dest with | RealAccu _ -> push_alloc_as fpad src RValue descr_opt @ pop_to_local " accu " | Local ( repr , name ) -> push_as fpad src repr @ pop_to_local name | RealStack pos -> push_alloc_as fpad src RValue descr_opt |> pop_to_stack fpad pos | _ -> assert false
let rec drop n l = if n > 0 then match l with | _ :: l -> drop ( n - 1 ) l | [ ] -> [ ] else l
let emit_unary gpad fpad op src1 dest = match op with | Pnegint -> ( push_as fpad src1 RIntVal @ [ L [ K " i32 . const " ; N ( I32 ( 0xffff_fffel ) ) ; ] ; L [ K " i32 . xor " ] ; L [ K " i32 . const " ; N ( I32 2l ) ; ] ; L [ K " i32 . add " ] ; ] ) |> pop_to fpad dest RIntVal None | Pboolnot -> ( push_as fpad src1 RIntVal @ [ L [ K " i32 . const " ; N ( I32 2l ) ; ] ; L [ K " i32 . xor " ] ] ) |> pop_to fpad dest RIntVal None | Poffsetint offset -> ( push_as fpad src1 RIntVal @ [ L [ K " i32 . const " ; N ( I32 ( Int32 . shift_left ( Int32 . of_int offset ) 1 ) ) ; ] ; L [ K " i32 . add " ] ] ) |> pop_to fpad dest RIntVal None | Pisint -> ( push_as fpad src1 RValue @ [ L [ K " i32 . const " ; N ( I32 1l ) ] ; L [ K " i32 . and " ] ; ] ) |> pop_to fpad dest RInt None | Pgetfield field -> assert ( field >= 0 ) ; ( push_as fpad src1 RValue @ [ L [ K " i32 . load " ; K ( sprintf " offset = 0x % lx " ( Int32 . of_int ( 4 * field ) ) ) ; K " align = 2 " ; ] ; ] ) |> pop_to fpad dest RValue None | Pgetfloatfield field -> ( match dest with | Local ( RFloat , name ) -> push_as fpad src1 RValue @ [ L [ K " f64 . load " ; K ( sprintf " offset = 0x % lx " ( Int32 . of_int ( 4 * double_size * field ) ) ) ; K " align = 2 " ; ] ; L [ K " local . set " ; ID name ] ] | _ -> assert false ) | Pvectlength -> let local = req_tmp1_i32 fpad in ( push_as fpad src1 RValue @ [ L [ K " i32 . const " ; N ( I32 4l ) ] ; L [ K " i32 . sub " ] ; L [ K " i32 . load " ] ; L [ K " local . tee " ; ID local ] ; L [ K " local . get " ; ID local ] ; L [ K " i32 . const " ; N ( I32 0xffl ) ] ; L [ K " i32 . and " ] ; L [ K " i32 . const " ; N ( I32 ( Int32 . of_int double_array_tag ) ) ; ] ; L [ K " i32 . eq " ] ; L [ K " i32 . const " ; N ( I32 9l ) ] ; L [ K " i32 . add " ] ; L [ K " i32 . shr_u " ] ; L [ K " i32 . const " ; N ( I32 1l ) ; ] ; L [ K " i32 . or " ] ; ] ) |> pop_to fpad dest RIntVal None | Pgetpubmet tag -> gpad . need_mlookup <- true ; ( push_as fpad src1 RValue @ push_const ( Int32 . succ ( Int32 . shift_left ( Int32 . of_int tag ) 1 ) ) @ push_const 0l @ [ L [ K " call " ; ID " mlookup " ] ] ) |> pop_to fpad dest RValue None
let emit_unaryeffect fpad op src1 = match op with | Poffsetref offset -> let local = req_tmp1_i32 fpad in push_as fpad src1 RIntVal @ [ L [ K " local . tee " ; ID local ] ; L [ K " local . get " ; ID local ] ; L [ K " i32 . load " ; K " align = 2 " ] ; L [ K " i32 . const " ; N ( I32 ( Int32 . shift_left ( Int32 . of_int offset ) 1 ) ) ; ] ; L [ K " i32 . add " ] ; L [ K " i32 . store " ; K " align = 2 " ] ; ] | Psetglobal ( Global index ) -> [ L [ K " global . get " ; ID " wasicaml_global_data " ; ] ; L [ K " i32 . load " ] ; L [ K " i32 . const " ; N ( I32 ( Int32 . of_int ( 4 * index ) ) ) ; ] ; L [ K " i32 . add " ] ] @ push_as fpad src1 RValue @ [ L [ K " call " ; ID " caml_modify " ; ] ]
let emit_int_binary fpad src1 src2 dest instrs_repr instrs_int = ( push_as fpad src1 RInt @ push_as fpad src2 RInt @ instrs_int ) |> pop_to fpad dest instrs_repr None
let emit_int_binary_unclean_ok fpad src1 src2 dest instrs_int = ( push_as fpad src1 RIntUnclean @ push_as fpad src2 RIntUnclean @ instrs_int ) |> pop_to fpad dest RIntUnclean None
let emit_intval_binary fpad src1 src2 dest instrs_int instrs_intval = if repr_of_store src1 = RInt && repr_of_store src2 = RInt then ( push_as fpad src1 RInt @ push_as fpad src2 RInt @ instrs_int @ tovalue fpad RIntUnclean ) |> pop_to fpad dest RIntVal None else ( push_as fpad src1 RIntVal @ push_as fpad src2 RIntVal @ instrs_intval ) |> pop_to fpad dest RIntVal None
let emit_intval_binary_unclean_ok fpad src1 src2 dest instrs_int instrs_intval = let is_ok st = let repr = repr_of_store st in repr = RInt || repr = RIntUnclean in if is_ok src1 && is_ok src2 then ( push_as fpad src1 RIntUnclean @ push_as fpad src2 RIntUnclean @ instrs_int @ tovalue fpad RIntUnclean ) |> pop_to fpad dest RIntVal None else emit_intval_binary fpad src1 src2 dest instrs_int instrs_intval
let emit_intval_int_binary fpad src1 src2 dest instrs_int instrs_intval = if repr_of_store src1 = RInt then ( push_as fpad src1 RInt @ push_as fpad src2 RInt @ instrs_int @ tovalue fpad RIntUnclean ) |> pop_to fpad dest RIntVal None else ( push_as fpad src1 RIntVal @ push_as fpad src2 RInt @ instrs_intval ) |> pop_to fpad dest RIntVal None
let emit_binary gpad fpad op src1 src2 dest = match op with | Paddint -> emit_intval_binary_unclean_ok fpad src1 src2 dest [ L [ K " i32 . add " ] ] [ L [ K " i32 . add " ] ; L [ K " i32 . const " ; N ( I32 1l ) ] ; L [ K " i32 . sub " ] ] | Psubint -> emit_intval_binary_unclean_ok fpad src1 src2 dest [ L [ K " i32 . sub " ] ] [ L [ K " i32 . sub " ] ; L [ K " i32 . const " ; N ( I32 1l ) ] ; L [ K " i32 . add " ] ] | Pmulint -> emit_int_binary fpad src1 src2 dest RIntUnclean [ L [ K " i32 . mul " ] ] | Pdivint -> ( match src2 with | Const n when n <> 0 -> emit_int_binary fpad src1 src2 dest RInt [ L [ K " i32 . div_s " ] ] | _ -> let local = req_tmp1_i32 fpad in emit_int_binary fpad src1 src2 dest RInt [ L [ K " local . tee " ; ID local ] ; L [ K " i32 . eqz " ] ; L [ K " if " ; L [ K " then " ; L [ K " call " ; ID " caml_raise_zero_divide " ] ] ] ; L [ K " local . get " ; ID local ] ; L [ K " i32 . div_s " ] ] ) | Pmodint -> ( match src2 with | Const n when n <> 0 -> emit_int_binary fpad src1 src2 dest RInt [ L [ K " i32 . rem_s " ] ] | _ -> let local = req_tmp1_i32 fpad in emit_int_binary fpad src1 src2 dest RInt [ L [ K " local . tee " ; ID local ] ; L [ K " i32 . eqz " ] ; L [ K " if " ; L [ K " then " ; L [ K " call " ; ID " caml_raise_zero_divide " ] ] ] ; L [ K " local . get " ; ID local ] ; L [ K " i32 . rem_s " ] ] ) | Pandint -> emit_intval_binary_unclean_ok fpad src1 src2 dest [ L [ K " i32 . and " ] ] [ L [ K " i32 . and " ] ] | Porint -> emit_intval_binary_unclean_ok fpad src1 src2 dest [ L [ K " i32 . or " ] ] [ L [ K " i32 . or " ] ] | Pxorint -> emit_intval_binary_unclean_ok fpad src1 src2 dest [ L [ K " i32 . xor " ] ] [ L [ K " i32 . xor " ] ; L [ K " i32 . const " ; N ( I32 1l ) ] ; L [ K " i32 . or " ] ; ] | Plslint -> let r1 = repr_of_store src1 in if r1 = RInt || r1 = RIntUnclean then emit_int_binary_unclean_ok fpad src1 src2 dest [ L [ K " i32 . shl " ] ] else ( push_as fpad src1 RIntVal @ [ L [ K " i32 . const " ; N ( I32 0xffff_fffel ) ] ; L [ K " i32 . and " ] ] @ push_as fpad src2 RIntUnclean @ [ L [ K " i32 . shl " ] ; L [ K " i32 . const " ; N ( I32 1l ) ] ; L [ K " i32 . or " ] ] ) |> pop_to fpad dest RIntVal None | Plsrint -> emit_intval_int_binary fpad src1 src2 dest [ L [ K " i32 . shr_u " ] ; L [ K " i32 . const " ; N ( I32 0x3fff_ffffl ) ] ; L [ K " i32 . and " ] ] [ L [ K " i32 . shr_u " ] ; L [ K " i32 . const " ; N ( I32 1l ) ] ; L [ K " i32 . or " ] ] | Pasrint -> emit_intval_int_binary fpad src1 src2 dest [ L [ K " i32 . shr_s " ] ] [ L [ K " i32 . shr_s " ] ; L [ K " i32 . const " ; N ( I32 1l ) ] ; L [ K " i32 . or " ] ] | ( Pintcomp cmp | Puintcomp cmp ) -> let wasm_op = match op with | Pintcomp Ceq | Puintcomp Ceq -> " i32 . eq " | Pintcomp Cne | Puintcomp Cne -> " i32 . ne " | Pintcomp Clt -> " i32 . lt_s " | Pintcomp Cle -> " i32 . le_s " | Pintcomp Cgt -> " i32 . gt_s " | Pintcomp Cge -> " i32 . ge_s " | Puintcomp Clt -> " i32 . lt_u " | Puintcomp Cle -> " i32 . le_u " | Puintcomp Cgt -> " i32 . gt_u " | Puintcomp Cge -> " i32 . ge_u " | _ -> assert false in if repr_comparable_as_i32 ( repr_of_store src1 ) ( repr_of_store src2 ) then ( push fpad src1 @ push fpad src2 @ [ L [ K wasm_op ] ] ) |> pop_to fpad dest RInt None else emit_int_binary fpad src1 src2 dest RInt [ L [ K wasm_op ] ] | Pgetvectitem -> ( push_as fpad src1 RValue @ ( match src2 , repr_of_store src2 with | Const c , _ -> [ L [ K " i32 . load " ; K ( sprintf " offset = 0x % lx " ( Int32 . shift_left ( Int32 . of_int c ) 2 ) ) ; K " align = 2 " ] ] | _ , ( RInt | RIntUnclean ) -> push_as fpad src2 RInt @ [ L [ K " i32 . const " ; N ( I32 2l ) ] ; L [ K " i32 . shl " ] ; L [ K " i32 . add " ] ; L [ K " i32 . load " ; K " align = 2 " ] ] | _ -> push_as fpad src2 RIntVal @ [ L [ K " i32 . const " ; N ( I32 1l ) ] ; L [ K " i32 . shl " ] ; L [ K " i32 . const " ; N ( I32 0xffff_fffcl ) ] ; L [ K " i32 . and " ] ; L [ K " i32 . add " ] ; L [ K " i32 . load " ; K " align = 2 " ] ] ) ) |> pop_to fpad dest RValue None | Pgetstringchar | Pgetbyteschar -> ( push_as fpad src1 RValue @ ( match src2 with | Const c -> [ L [ K " i32 . load8_u " ; K ( sprintf " offset = 0x % x " c ) ] ] | _ -> push_as fpad src2 RIntUnclean @ [ L [ K " i32 . add " ] ; L [ K " i32 . load8_u " ] ] ) ) |> pop_to fpad dest RInt None | Pgetmethod -> gpad . need_mlookup <- true ; ( push_as fpad src2 RValue @ [ L [ K " i32 . load " ; K " align = 2 " ] ] @ push_as fpad src1 RInt @ [ L [ K " i32 . const " ; N ( I32 2l ) ] ; L [ K " i32 . shl " ] ; L [ K " i32 . add " ] ; L [ K " i32 . load " ; K " align = 2 " ] ] ) |> pop_to fpad dest RValue None | Pgetdynmet -> gpad . need_mlookup <- true ; ( push_as fpad src2 RValue @ push_as fpad src1 RValue @ push_const 0l @ [ L [ K " call " ; ID " mlookup " ] ] ) |> pop_to fpad dest RValue None
let emit_binaryeffect fpad op src1 src2 = match op with | Psetfield field -> push_as fpad src1 RValue @ ( if field <> 0 then [ L [ K " i32 . const " ; N ( I32 ( Int32 . of_int ( 4 * field ) ) ) ; ] ; L [ K " i32 . add " ] ] else [ ] ) @ push_as fpad src2 RValue @ [ L [ K " call " ; ID " caml_modify " ] ] | Psetfloatfield field -> push_as fpad src1 RValue @ push_as fpad src2 RFloat @ [ L [ K " f64 . store " ; K ( sprintf " offset = 0x % lx " ( Int32 . of_int ( 8 * field ) ) ) ; K " align = 2 " ; ] ]
let emit_ternaryeffect fpad op src1 src2 src3 = match op with | Psetvectitem -> push_as fpad src1 RValue @ ( match src2 , repr_of_store src2 with | Const c , _ -> [ L [ K " i32 . const " ; N ( I32 ( Int32 . shift_left ( Int32 . of_int c ) 2 ) ) ] ; L [ K " i32 . add " ] ] | _ , ( RInt | RIntUnclean ) -> push_as fpad src2 RIntUnclean @ [ L [ K " i32 . const " ; N ( I32 2l ) ] ; L [ K " i32 . shl " ] ; L [ K " i32 . add " ] ; ] | _ -> push_as fpad src2 RIntVal @ [ L [ K " i32 . const " ; N ( I32 1l ) ] ; L [ K " i32 . shl " ] ; L [ K " i32 . const " ; N ( I32 0xffff_fffcl ) ] ; L [ K " i32 . and " ] ; L [ K " i32 . add " ] ; ] ) @ push_as fpad src3 RValue @ [ L [ K " call " ; ID " caml_modify " ] ] | Psetbyteschar -> push_as fpad src1 RValue @ ( match src2 with | Const c -> push_as fpad src3 RInt @ [ L [ K " i32 . store8 " ; K ( sprintf " offset = 0x % x " c ) ] ] | _ -> push_as fpad src2 RIntUnclean @ [ L [ K " i32 . add " ] ] @ push_as fpad src3 RIntUnclean @ [ L [ K " i32 . store8 " ] ] )
type mb_elem = | MB_store of store | MB_const of int32 | MB_code of sexp list
let push_mb_elem fpad = function | MB_store src -> push_as fpad src RValue | MB_const n -> push_const n | MB_code code -> code
let makeblock fpad descr src_list tag = let size = List . length src_list in let sexpl_alloc , ptr , young = alloc_set fpad descr size tag in let sexpl_init = if young then List . mapi ( fun field src -> push_mb_elem fpad src |> pop_to_field ptr field ) src_list else List . mapi ( fun field src -> push_field_addr ptr field @ push_mb_elem fpad src @ [ L [ K " call " ; ID " caml_initialize " ] ] ) src_list in let c1 = [ C " < makeblock " > ] in let c2 = [ C " </ makeblock " > ] in ( ptr , c1 @ sexpl_alloc @ List . flatten sexpl_init @ c2 )
let makefloatblock fpad descr src_list = let size = List . length src_list in let wosize = size * double_size in let sexpl_alloc , ptr , _ = alloc_set fpad descr wosize double_array_tag in let sexpl_init = List . mapi ( fun field src -> ( push_as fpad src RValue @ load_double ) |> pop_to_double_field ptr field ) src_list in ( ptr , sexpl_alloc @ List . flatten sexpl_init )
let lookup_label gpad lab = let letrec_label , subfunc = try Hashtbl . find gpad . funcmapping lab with Not_found -> assert false in let wasmindex = try Hashtbl . find gpad . wasmindex letrec_label with Not_found -> assert false in ( wasmindex , letrec_label , subfunc )
let push_wasmptr gpad lab = let wasmindex , letrec_label , subfunc = lookup_label gpad lab in [ L [ K " i32 . const " ; ID ( Hashtbl . find gpad . letrec_name letrec_label ) ] ]
let push_codeptr gpad lab = let wasmindex , letrec_label , subfunc = lookup_label gpad lab in push_wasmptr gpad lab @ [ L [ K " i32 . const " ; N ( I32 ( Int32 . of_int code_pointer_shift ) ) ] ; L [ K " i32 . shl " ] ; L [ K " i32 . const " ; N ( I32 ( Int32 . of_int ( ( subfunc lsl 2 ) + 1 ) ) ) ] ; L [ K " i32 . or " ] ; ]
let closurerec gpad fpad descr src_list dest_list = let nfuncs = List . length dest_list in let envofs = nfuncs * 3 - 1 in let mb_src_list = ( List . mapi ( fun i ( _ , label ) -> ( if i > 0 then [ MB_const ( Int32 . of_int ( make_header ( 3 * i ) infix_tag ) ) ] else [ ] ) @ [ MB_code ( push_codeptr gpad label ) ] @ [ MB_const ( Int32 . of_int ( ( ( envofs - 3 * i ) lsl 1 ) + 1 ) ) ] ) dest_list |> List . flatten ) @ List . map ( fun src -> MB_store src ) src_list in let ptr , sexpl_mb = makeblock fpad descr mb_src_list closure_tag in let sexpl_dest = List . mapi ( fun i ( dest , _ ) -> ( [ L [ K " local . get " ; ID ptr ] ] @ ( if i > 0 then [ L [ K " i32 . const " ; N ( I32 ( Int32 . of_int ( 12 * i ) ) ) ] ; L [ K " i32 . add " ] ] else [ ] ) ) |> pop_to fpad dest RValue None ) dest_list |> List . flatten in sexpl_mb @ sexpl_dest
let c_call gpad fpad descr src_list name = let descr = { descr with stack_save_accu = false } in let sexpl_setup = setup_for_gc fpad descr in let sexpl_restore = restore_after_gc fpad descr in let sexpl_args = List . map ( fun src -> push_as fpad src RValue ) src_list in let sexpl_call = [ L [ K " call " ; ID name ] ] @ pop_to_local " accu " in let p_i32 = L [ K " param " ; K " i32 " ] in let r_i32 = L [ K " result " ; K " i32 " ] in let ty = ( src_list |> List . map ( fun _ -> p_i32 ) ) @ [ r_i32 ] in Hashtbl . replace gpad . primitives name ty ; sexpl_setup @ List . flatten sexpl_args @ sexpl_call @ sexpl_restore
let c_call_vector gpad fpad descr numargs depth name = let descr = { descr with stack_save_accu = false } in let sexpl_setup = setup_for_gc fpad descr in let sexpl_restore = restore_after_gc fpad descr in let sexpl_call = push_field_addr " fp " ( - depth ) @ push_const ( Int32 . of_int numargs ) @ [ L [ K " call " ; ID name ] ] @ pop_to_local " accu " in let ty = [ L [ K " param " ; K " i32 " ] ; L [ K " param " ; K " i32 " ] ; L [ K " result " ; K " i32 " ] ] in Hashtbl . replace gpad . primitives name ty ; sexpl_setup @ sexpl_call @ sexpl_restore
let string_label = function | Label k -> sprintf " label % d " k | Loop k -> sprintf " loop % d " k
let switch fpad src labls_ints labls_blocks = fpad . need_panic <- true ; let value = req_tmp1_i32 fpad in push_as fpad src RValue @ pop_to_local value @ [ L [ K " local . get " ; ID value ] ; L [ K " i32 . const " ; N ( I32 1l ) ] ; L [ K " i32 . and " ] ; L [ K " if " ; L [ K " then " ; L [ K " local . get " ; ID value ] ; L [ K " i32 . const " ; N ( I32 1l ) ] ; L [ K " i32 . shr_s " ] ; L ( [ K " br_table " ] @ ( Array . map ( fun lab -> ID ( string_label lab ) ) labls_ints |> Array . to_list ) @ [ ID " panic " ] ) ; ] ; L [ K " else " ; L [ K " local . get " ; ID value ] ; L [ K " i32 . const " ; N ( I32 4l ) ] ; L [ K " i32 . sub " ] ; L [ K " i32 . load " ; K " align = 2 " ] ; L [ K " i32 . const " ; N ( I32 0xffl ) ] ; L [ K " i32 . and " ] ; L ( [ K " br_table " ] @ ( Array . map ( fun lab -> ID ( string_label lab ) ) labls_blocks |> Array . to_list ) @ [ ID " panic " ] ) ; ] ; ] ]
let grab fpad num = let sexpl = [ L [ K " local . get " ; ID " codeptr " ] ; L [ K " i32 . const " ; N ( I32 code_pointer_restart_mask ) ] ; L [ K " i32 . and " ] ; L [ K " if " ; L ( [ K " then " ; L [ K " local . get " ; ID " envptr " ] ; L [ K " local . get " ; ID " extra_args " ] ; L [ K " local . get " ; ID " fp " ] ; ] @ call_restart_helper ( ) @ pop_to_fp fpad @ [ L [ K " local . set " ; ID " extra_args " ] ; ] ) ] ; L [ K " local . get " ; ID " extra_args " ] ; L [ K " i32 . const " ; N ( I32 ( Int32 . of_int num ) ) ] ; L [ K " i32 . ge_u " ] ; L [ K " if " ; L [ K " then " ; L [ K " local . get " ; ID " extra_args " ] ; L [ K " i32 . const " ; N ( I32 ( Int32 . of_int num ) ) ] ; L [ K " i32 . sub " ] ; L [ K " local . set " ; ID " extra_args " ] ; ] ; L [ K " else " ; L [ K " local . get " ; ID " envptr " ] ; L [ K " local . get " ; ID " extra_args " ] ; L [ K " local . get " ; ID " codeptr " ] ; L [ K " local . get " ; ID " fp " ] ; L [ K " call " ; ID " grab_helper " ] ; L [ K " return " ] ; ] ] ; ] in sexpl
let return = [ C " $ return " ; L [ K " local . get " ; ID " extra_args " ] ; L [ K " if " ; L [ K " then " ; L [ K " local . get " ; ID " envptr " ] ; L [ K " local . get " ; ID " extra_args " ] ; L [ K " local . get " ; ID " fp " ] ; L [ K " local . get " ; ID " accu " ] ; L [ K " call " ; ID " return_helper " ] ; L [ K " return " ] ] ] ; L [ K " local . get " ; ID " accu " ] ; L [ K " return " ] ; ]
let throw fpad = if fpad . fpad_scope . cfg_main && fpad . fpad_scope . cfg_try_labels = [ ] then [ L [ K " call " ; ID " wasicaml_throw " ] ; L [ K " unreachable " ] ] else if fpad . fpad_scope . cfg_try_labels = [ ] then [ L [ K " i32 . const " ; N ( I32 0l ) ] ; L [ K " return " ] ] else [ L [ K " i32 . const " ; N ( I32 0l ) ] ; L [ K " global . set " ; ID " exn_result " ] ; L [ K " i32 . const " ; N ( I32 0l ) ] ; L [ K " return " ] ]
let apply_direct gpad fpad funlabel numargs depth = let _ , letrec_label , _ = lookup_label gpad funlabel in let letrec_name = Hashtbl . find gpad . letrec_name letrec_label in let env_pos = ( - depth + numargs + 1 ) in ( push_local " accu " |> pop_to_stack fpad env_pos ) @ push_field_addr " fp " env_pos @ [ L [ K " i32 . const " ; N ( I32 ( Int32 . of_int ( numargs - 1 ) ) ) ] ; ] @ push_field " accu " 0 @ push_local " fp " @ [ L [ K " i32 . const " ; N ( I32 ( Int32 . of_int ( 4 * depth ) ) ) ] ; L [ K " i32 . sub " ] ; L [ K " call " ; ID letrec_name ] ; L [ K " local . tee " ; ID " accu " ] ; L [ K " i32 . eqz " ] ; L [ K " if " ; L ( [ K " then " ] @ throw fpad ) ] ; ]
let apply fpad numargs depth = let env_pos = ( - depth + numargs + 1 ) in let codeptr = req_tmp1_i32 fpad in ( push_local " accu " |> pop_to_stack fpad env_pos ) @ push_field_addr " fp " env_pos @ [ L [ K " i32 . const " ; N ( I32 ( Int32 . of_int ( numargs - 1 ) ) ) ] ; ] @ push_field " accu " 0 @ ( if ! enable_deadbeef_check then [ L [ K " local . get " ; ID " fp " ] ; L [ K " i32 . const " ; N ( I32 ( Int32 . of_int ( 4 * depth ) ) ) ] ; L [ K " i32 . sub " ] ; L [ K " local . get " ; ID " fp " ] ; L [ K " call " ; ID " deadbeef_check " ] ] else [ ] ) @ [ L [ K " local . tee " ; ID codeptr ] ; L [ K " local . get " ; ID " fp " ] ; L [ K " i32 . const " ; N ( I32 ( Int32 . of_int ( 4 * depth ) ) ) ] ; L [ K " i32 . sub " ] ; L [ K " local . get " ; ID codeptr ] ; L [ K " i32 . const " ; N ( I32 ( Int32 . of_int code_pointer_shift ) ) ] ; L [ K " i32 . shr_u " ] ; L [ K " call_indirect " ; N ( I32 0l ) ; L [ K " param " ; K " i32 " ] ; L [ K " param " ; K " i32 " ] ; L [ K " param " ; K " i32 " ] ; L [ K " param " ; K " i32 " ] ; L [ K " result " ; K " i32 " ] ; ] ; L [ K " local . tee " ; ID " accu " ] ; L [ K " i32 . eqz " ] ; L [ K " if " ; L ( [ K " then " ] @ throw fpad ) ] ; ]
let appterm_common fpad = [ C " $ appterm_common " ; L [ K " local . get " ; ID " envptr " ] ; L [ K " local . get " ; ID " codeptr " ] ; L [ K " local . get " ; ID " accu " ] ; L [ K " local . get " ; ID " extra_args " ] ; L [ K " local . get " ; ID " appterm_new_num_args " ] ; ] @ call_appterm_helper ( ) @ [ L [ K " local . set " ; ID " extra_args " ] ; L [ K " local . tee " ; ID " codeptr " ] ; L [ K " if " ; L [ K " then " ; L [ K " br " ; ID " startover " ] ] ; L [ K " else " ; L [ K " br " ; ID " return " ] ] ] ]
let appterm gpad fpad numargs oldnumargs depth = let sexpl = if numargs <= 10 then ( gpad . need_reinit_frame_k <- ISet . add numargs gpad . need_reinit_frame_k ; push_local " fp " @ push_const ( Int32 . of_int depth ) @ push_const ( Int32 . of_int oldnumargs ) @ [ L [ K " call " ; ID ( sprintf " reinit_frame_ % d " numargs ) ] ] @ pop_to_local " fp " @ push_const ( Int32 . of_int numargs ) @ pop_to_local " appterm_new_num_args " @ [ L [ K " br " ; ID " appterm_common " ] ] ) else ( gpad . need_reinit_frame <- true ; push_local " fp " @ push_const ( Int32 . of_int depth ) @ push_const ( Int32 . of_int oldnumargs ) @ push_const ( Int32 . of_int numargs ) @ [ L [ K " call " ; ID " reinit_frame " ] ] @ pop_to_local " fp " @ push_const ( Int32 . of_int numargs ) @ pop_to_local " appterm_new_num_args " @ [ L [ K " br " ; ID " appterm_common " ] ] ) in fpad . need_appterm_common <- true ; fpad . need_return <- true ; sexpl
let trap gpad fpad trylabel catchlabel depth = let local1 = req_tmp1_i32 fpad in let local2 = req_tmp2_i32 fpad in let sexpl_stack = ( push_const 0l |> pop_to_stack fpad ( - depth - 1 ) ) @ ( push_const 0l |> pop_to_stack fpad ( - depth - 2 ) ) @ ( push_const 0l |> pop_to_stack fpad ( - depth - 3 ) ) @ ( push_local " extra_args " |> pop_to_stack fpad ( - depth - 4 ) ) @ ( if ! enable_deadbeef_check then [ L [ K " local . get " ; ID " fp " ] ; L [ K " i32 . const " ; N ( I32 ( Int32 . of_int ( 4 * ( depth + 4 ) ) ) ) ] ; L [ K " i32 . sub " ] ; L [ K " local . get " ; ID " fp " ] ; L [ K " call " ; ID " deadbeef_check " ] ] else [ ] ) in let sexpl_try = push_domain_field domain_field_local_roots @ pop_to_local local2 @ push_wasmptr gpad trylabel @ push_local " envptr " @ push_local " extra_args " @ push_codeptr gpad trylabel @ push_local " fp " @ [ L [ K " i32 . const " ; N ( I32 ( Int32 . of_int ( 4 * ( depth + 4 ) ) ) ) ] ; L [ K " i32 . sub " ] ] @ [ L [ K " call " ; ID " wasicaml_wraptry4 " ] ; L [ K " local . set " ; ID local1 ] ; ] @ ( push_local local2 |> pop_to_domain_field domain_field_local_roots ) @ [ L [ K " local . get " ; ID local1 ] ; L [ K " global . get " ; ID " exn_result " ] ; L [ K " i32 . eqz " ] ; L [ K " i32 . or " ] ; L [ K " if " ; L [ K " then " ; L [ K " global . get " ; ID " wasicaml_domain_state " ] ; L [ K " i32 . load " ; K ( sprintf " offset = 0x % lx " ( Int32 . of_int ( 8 * domain_field_exn_bucket ) ) ) ; K " align = 2 " ] ; L [ K " local . set " ; ID " accu " ] ; L [ K " br " ; ID ( sprintf " label % d " catchlabel ) ] ] ] ; L [ K " global . get " ; ID " exn_result " ] ; L [ K " local . set " ; ID " accu " ] ] in sexpl_stack @ sexpl_try
let rec emit_instr gpad fpad instr = match instr with | Wcomment s -> [ ] | Wblock arg -> ( match arg . label with | Label lab -> [ L ( [ K " block " ; ID ( sprintf " label % d " lab ) ; BR ] @ emit_instrs gpad fpad arg . body ) ] | Loop lab -> [ L ( [ K " loop " ; ID ( sprintf " loop % d " lab ) ; BR ] @ emit_instrs gpad fpad arg . body ) ] ) | Wcond { cond ; ontrue ; onfalse } -> emit_instr gpad fpad ( if ! cond then ontrue else onfalse ) | Wcopy arg -> copy fpad arg . src arg . dest None | Walloc arg -> copy fpad arg . src arg . dest ( Some arg . descr ) | Wenv arg -> push_env @ load_offset ( 4 * arg . field ) @ pop_to_local " accu " | Wcopyenv arg -> push_env @ add_offset ( 4 * arg . offset ) @ pop_to_local " accu " | Wgetglobal arg -> let Global offset = arg . src in push_global offset @ pop_to_local " accu " | Wunary arg -> emit_unary gpad fpad arg . op arg . src1 arg . dest | Wunaryeffect arg -> emit_unaryeffect fpad arg . op arg . src1 | Wbinary arg -> emit_binary gpad fpad arg . op arg . src1 arg . src2 arg . dest | Wbinaryeffect arg -> emit_binaryeffect fpad arg . op arg . src1 arg . src2 | Wternaryeffect arg -> emit_ternaryeffect fpad arg . op arg . src1 arg . src2 arg . src3 | Wmakeblock arg -> let src = List . map ( fun s -> MB_store s ) arg . src in let ptr , sexpl = makeblock fpad arg . descr src arg . tag in sexpl @ push_local ptr @ pop_to_local " accu " | Wmakefloatblock arg -> let ptr , sexpl = makefloatblock fpad arg . descr arg . src in sexpl @ push_local ptr @ pop_to_local " accu " | Wccall arg -> c_call gpad fpad arg . descr arg . src arg . name | Wccall_vector arg -> c_call_vector gpad fpad arg . descr arg . numargs arg . depth arg . name | Wbranch arg -> [ L [ K " br " ; ID ( string_label arg . label ) ] ] | Wif arg -> ( match repr_of_store arg . src with | RInt -> push_as fpad arg . src RInt @ ( if arg . neg then [ L [ K " i32 . eqz " ] ] else [ ] ) | RIntUnclean -> push_as fpad arg . src RIntUnclean @ [ L [ K " i32 . const " ; N ( I32 0x7fff_ffffl ) ] ; L [ K " i32 . and " ] ] @ ( if arg . neg then [ L [ K " i32 . eqz " ] ] else [ ] ) | _ -> push_as fpad arg . src RIntVal @ [ L [ K " i32 . const " ; N ( I32 1l ) ] ; L [ K ( if arg . neg then " i32 . le_u " else " i32 . gt_u " ) ] ] ) @ [ L [ K " if " ; L ( K " then " :: ( List . map ( emit_instr gpad fpad ) arg . body |> List . flatten ) ) ] ] | Wswitch arg -> switch fpad arg . src arg . labels_int arg . labels_blk | Wapply arg -> apply fpad arg . numargs arg . depth | Wapply_direct arg -> let fn = Wc_traceglobals . Unknown in let src = TracedGlobal ( arg . global , arg . path , fn ) in copy fpad src Wc_unstack . real_accu None @ apply_direct gpad fpad arg . funlabel arg . numargs arg . depth | Wappterm arg -> appterm gpad fpad arg . numargs arg . oldnumargs arg . depth | Wreturn arg -> let no_function = match arg . src with | RealAccu { no_function } -> no_function | _ -> repr_of_store arg . src <> RValue in if no_function then push_as fpad arg . src RValue @ [ L [ K " return " ] ] else ( fpad . need_return <- true ; push_as fpad arg . src RValue @ pop_to_local " accu " @ push_field_addr " fp " arg . arity @ pop_to_local " fp " @ [ L [ K " br " ; ID " return " ] ] ) | Wgrab arg -> grab fpad arg . numargs | Wclosurerec arg -> closurerec gpad fpad arg . descr arg . src arg . dest | Wraise arg -> ( push_as fpad arg . src RValue |> pop_to_domain_field domain_field_exn_bucket ) @ ( push_local " fp " |> pop_to_domain_field domain_field_extern_sp ) @ throw fpad | Wtrap arg -> trap gpad fpad arg . trylabel arg . catchlabel arg . depth | Wtryreturn arg -> push_as fpad arg . src RValue @ [ L [ K " global . set " ; ID " exn_result " ] ; L [ K " i32 . const " ; N ( I32 0l ) ] ; L [ K " return " ] ] | Wnextmain { label } -> push_local " accu " @ push_local " extra_args " @ push_local " codeptr " @ push_local " fp " @ [ L [ K " call " ; ID ( sprintf " letrec_main % d " label ) ] ; L [ K " return " ] ] | Wstop -> [ L [ K " i32 . const " ; N ( I32 0l ) ] ; L [ K " return " ] ] | Wnop -> [ ] | Wunreachable -> [ L [ K " unreachable " ] ] List . fold_left ( fun acc instr -> let comment = string_of_winstruction instr in List . rev_append ( emit_instr gpad fpad instr ) ( List . rev_append [ C comment ] acc ) ) [ ] instrs |> List . rev
let emit_fblock gpad fpad fblock = let maxdepth = Wc_tracestack . max_stack_depth_of_fblock fblock in fpad . maxdepth <- if maxdepth > 0 then maxdepth + 2 else 0 ; let instrs = Wc_unstack . transl_fblock fpad . lpad fblock |> emit_instrs gpad fpad in set_bp fpad @ ( if ! enable_deadbeef_check && fpad . maxdepth > 0 then [ L [ K " local . get " ; ID " bp " ] ; L [ K " local . get " ; ID " fp " ] ; L [ K " call " ; ID " deadbeef_init " ] ] else [ ] ) @ instrs
let get_funcmapping scode = let open Wc_control in let funcmapping = Hashtbl . create 7 in let subfunction_num = Hashtbl . create 7 in let subfunctions = Hashtbl . create 7 in IMap . iter ( fun func_label fblock -> let letrec_func_label = match fblock . scope . cfg_letrec_label with | None -> 0 | Some label -> label in let letrec_label = if fblock . scope . cfg_main then Main letrec_func_label else Func letrec_func_label in let subfunc_num = try Hashtbl . find subfunction_num letrec_label with Not_found -> 0 in Hashtbl . replace subfunction_num letrec_label ( subfunc_num + 1 ) ; Hashtbl . add funcmapping func_label ( letrec_label , subfunc_num ) ; let subfunc_list = try Hashtbl . find subfunctions letrec_label with Not_found -> [ ] in Hashtbl . replace subfunctions letrec_label ( func_label :: subfunc_list ) ) scode . functions ; let subfunctions_rev = Hashtbl . create 7 in Hashtbl . iter ( fun letrec_label subfunc_labels -> Hashtbl . add subfunctions_rev letrec_label ( List . rev subfunc_labels ) ) subfunctions ; ( funcmapping , subfunctions_rev )
let block_cascade start_sexpl label_sexpl_pairs = let rec shift prev_sexpl pairs = match pairs with | ( label , lsexpl ) :: pairs ' -> ( prev_sexpl , Some label ) :: shift lsexpl pairs ' | [ ] -> [ prev_sexpl , None ] in let rec arrange inner_sexpl shifted = match shifted with | ( sexpl , label_opt ) :: shifted ' -> let body = inner_sexpl @ sexpl @ [ L [ K " unreachable " ] ] in let inner_sexpl ' = match label_opt with | None -> body | Some lab -> [ L ( [ K " block " ; ID lab ; BR ; ] @ body ) ] in arrange inner_sexpl ' shifted ' | [ ] -> inner_sexpl in arrange [ ] ( shift start_sexpl label_sexpl_pairs )
let cond_section cond label sexpl_section sexpl_users = if cond then [ L ( [ K " block " ; ID label ; BR ] @ sexpl_users ) ] @ sexpl_section else sexpl_users
let cond_loop cond label sexpl = if cond then [ L ( [ K " loop " ; ID label ; BR ] @ sexpl ) ] else sexpl
let eff_label = function | Func l -> l | Main l -> l
let init_lpad_for_subfunc gpad fpad func_label = let func_offset , environment = ( try Hashtbl . find gpad . glbfun_table func_label with Not_found -> 0 , [ | ] | ) in fpad . lpad . environment <- environment ; fpad . lpad . func_offset <- func_offset
let generate_function scode gpad letrec_label func_name subfunc_labels export_flag = let fpad = { ( empty_fpad ( ) ) with fpad_letrec_label = letrec_label } in Hashtbl . add fpad . lpad . locals " accu " RValue ; Hashtbl . add fpad . lpad . locals " bp " RValue ; fpad . lpad . avoid_locals <- export_flag ; fpad . lpad . globals_table <- gpad . globals_table ; let subfunc_pairs = List . map ( fun func_label -> let fblock = IMap . find func_label Wc_control . ( scode . functions ) in fpad . fpad_scope <- fblock . scope ; let label = sprintf " func % d " func_label in init_lpad_for_subfunc gpad fpad func_label ; let sexpl = emit_fblock gpad fpad fblock in ( label , sexpl ) ) subfunc_labels in let subfunc_pairs_with_panic = subfunc_pairs @ [ " panic " , [ ] ] in let subfunc_sexpl = match subfunc_pairs with | [ ] -> assert false | [ label , sexpl ] -> cond_section fpad . need_panic " panic " [ L [ K " unreachable " ] ] sexpl | _ -> let labels = List . map ( fun ( label , _ ) -> ID label ) subfunc_pairs_with_panic in let body = [ L [ K " local . get " ; ID " codeptr " ] ; L [ K " i32 . const " ; N ( I32 code_pointer_subfunc_mask ) ] ; L [ K " i32 . and " ] ; L [ K " i32 . const " ; N ( I32 2l ) ] ; L [ K " i32 . shr_u " ] ; L ( [ K " br_table " ] @ labels ) ] in block_cascade body subfunc_pairs_with_panic in let sexpl = ( match letrec_label with | Main 0 | Func _ -> [ L [ K " i32 . const " ; N ( I32 1l ) ] ; L [ K " local . set " ; ID " accu " ] ] | Main _ -> [ L [ K " local . get " ; ID " envptr " ] ; L [ K " local . set " ; ID " accu " ] ; L [ K " i32 . const " ; N ( I32 0xffff_fffcl ) ] ; L [ K " local . set " ; ID " envptr " ] ] ) @ ( subfunc_sexpl |> cond_section fpad . need_appterm_common " appterm_common " ( appterm_common fpad ) |> cond_loop fpad . need_appterm_common " startover " |> cond_section fpad . need_return " return " return ) in if fpad . need_appterm_common then ( Hashtbl . add fpad . lpad . locals " appterm_new_num_args " RInt ; ) ; if fpad . need_xalloc then Hashtbl . add fpad . lpad . locals " xalloc " RValue ; if fpad . need_tmp1_i32 then Hashtbl . add fpad . lpad . locals " tmp1_i32 " RValue ; if fpad . need_tmp2_i32 then Hashtbl . add fpad . lpad . locals " tmp2_i32 " RValue ; if fpad . need_tmp1_f64 then Hashtbl . add fpad . lpad . locals " tmp1_f64 " RValue ; let locals = Hashtbl . fold ( fun name vtype acc -> ( name , vtype ) :: acc ) fpad . lpad . locals [ ] in let letrec = [ L ( [ K " func " ; ID func_name ; ] @ ( if export_flag then [ L [ K " export " ; S func_name ] ] else [ ] ) @ [ L [ K " param " ; ID " envptr " ; K " i32 " ] ; L [ K " param " ; ID " extra_args " ; K " i32 " ] ; L [ K " param " ; ID " codeptr " ; K " i32 " ] ; L [ K " param " ; ID " fp " ; K " i32 " ] ; BR ; L [ K " result " ; K " i32 " ] ; ] @ ( List . map ( fun ( name , repr ) -> L [ K " local " ; ID name ; K ( string_of_vtype ( vtype repr ) ) ] ; ) locals ) L [ K " i32 . const " ; N ( I32 ( - 1l ) ) ] ; L [ K " i32 . ne " ] ; L [ K " if " ; L ( [ K " then " ; L [ K " i32 . const " ; N ( I32 14l ) ] ] @ push_env @ [ L [ K " call " ; ID " debug2 " ] ] ) ] ] ) * @ sexpl @ [ L [ K " unreachable " ] ] ) ] in letrec
let generate_letrec scode gpad letrec_label = let subfunc_labels = try Hashtbl . find gpad . subfunctions letrec_label with Not_found -> assert false in assert ( subfunc_labels <> [ ] ) ; let func_name = Hashtbl . find gpad . letrec_name letrec_label in let export = ( letrec_label = Main 0 ) in generate_function scode gpad letrec_label func_name subfunc_labels export
let globals ( ) = [ " wasicaml_global_data " , true , TI32 ; " wasicaml_domain_state " , true , TI32 ; " wasicaml_atom_table " , true , TI32 ; " wasicaml_stack_threshold " , true , TI32 ; " exn_result " , true , TI32 ; ] @ if ! enable_multireturn then [ ] else [ " retval2 " , true , TI32 ; " retval3 " , true , TI32 ; ]
let imp_functions = [ " env " , " caml_alloc_small_dispatch " , [ L [ K " param " ; K " i32 " ] ; L [ K " param " ; K " i32 " ] ; L [ K " param " ; K " i32 " ] ; L [ K " param " ; K " i32 " ] ; ] ; " env " , " caml_alloc_small " , [ L [ K " param " ; K " i32 " ] ; L [ K " param " ; K " i32 " ] ; L [ K " result " ; K " i32 " ] ] ; " env " , " caml_alloc_shr " , [ L [ K " param " ; K " i32 " ] ; L [ K " param " ; K " i32 " ] ; L [ K " result " ; K " i32 " ] ] ; " env " , " caml_initialize " , [ L [ K " param " ; K " i32 " ] ; L [ K " param " ; K " i32 " ] ; ] ; " env " , " caml_modify " , [ L [ K " param " ; K " i32 " ] ; L [ K " param " ; K " i32 " ] ; ] ; " env " , " caml_raise_zero_divide " , [ ] ; " env " , " caml_raise_stack_overflow " , [ ] ; " env " , " wasicaml_wraptry4 " , [ L [ K " param " ; K " i32 " ] ; L [ K " param " ; K " i32 " ] ; L [ K " param " ; K " i32 " ] ; L [ K " param " ; K " i32 " ] ; L [ K " param " ; K " i32 " ] ; L [ K " result " ; K " i32 " ] ] ; " env " , " wasicaml_get_global_data " , [ L [ K " result " ; K " i32 " ] ] ; " env " , " wasicaml_get_domain_state " , [ L [ K " result " ; K " i32 " ] ] ; " env " , " wasicaml_get_atom_table " , [ L [ K " result " ; K " i32 " ] ] ; " env " , " debug2 " , [ L [ K " param " ; K " i32 " ] ; L [ K " param " ; K " i32 " ] ; ] ; " wasicaml " , " wasicaml_throw " , [ ] ; ]
let sanitize = String . map ( fun c -> match c with | ' A ' . . ' Z ' | ' a ' . . ' z ' | ' 0 ' . . ' 9 ' | ' _ ' | ' . ' -> c | _ -> ' ' ? )
let bigarray_to_string_list limit ba = let n = Array1 . dim ba in let l = ref [ ] in let p = ref 0 in while ! p < n do let q = min ( n - ! p ) limit in let by = Bytes . create q in for i = 0 to q - 1 do Bytes . set by i ba . { ! p + i } done ; l := Bytes . to_string by :: ! l ; p := ! p + q done ; List . rev ! l
let generate scode exe get_defname globals_table = let ( funcmapping , subfunctions ) = get_funcmapping scode in let letrec_name = Hashtbl . create 7 in Hashtbl . iter ( fun _ ( letrec_label , _ ) -> match letrec_label with | Main 0 -> Hashtbl . add letrec_name letrec_label " letrec_main " | Main lab -> Hashtbl . add letrec_name letrec_label ( sprintf " letrec_main % d " lab ) | Func lab -> let suffix = try let defname = get_defname lab in " _ " ^ sanitize defname with | Not_found -> " " in Hashtbl . add letrec_name letrec_label ( sprintf " letrec % d % s " lab suffix ) ) funcmapping ; let wasmindex = Hashtbl . create 7 in let nextindex = ref 0 in let _elements = Hashtbl . fold ( fun _ ( letrec_label , _ ) acc -> Hashtbl . add wasmindex letrec_label ! nextindex ; incr nextindex ; let name = Hashtbl . find letrec_name letrec_label in ( ID name ) :: acc ) funcmapping [ ] in let data = bigarray_to_string_list 4096 Wc_reader . ( exe . data ) |> List . map ( fun s -> S s ) in let gpad = { funcmapping ; subfunctions ; primitives = Hashtbl . create 7 ; wasmindex ; letrec_name ; need_reinit_frame = false ; need_reinit_frame_k = ISet . empty ; need_mlookup = false ; globals_table ; glbfun_table = Wc_traceglobals . derive_glbfun_table globals_table ; } in let sexpl_code = Hashtbl . fold ( fun letrec_label _ acc -> let sexpl = generate_letrec scode gpad letrec_label in sexpl @ acc ) gpad . subfunctions [ ] in let sexpl_memory = [ L [ K " import " ; S " env " ; S " memory " ; L [ K " memory " ; ID " memory " ; N ( I32 65536l ) ; ] ] ] in let sexpl_table = [ L [ K " import " ; S " env " ; S " table " ; L [ K " table " ; N ( I32 ( Int32 . of_int ( Hashtbl . length subfunctions ) ) ) ; K " funcref " ] ] ] in let sexpl_functions = List . map ( fun ( modname , name , typeuse ) -> L [ K " import " ; S modname ; S name ; L ( [ K " func " ; ID name ; ] @ typeuse ) ] ) imp_functions @ Hashtbl . fold ( fun name typeuse acc -> ( L [ K " import " ; S " env " ; S name ; L ( [ K " func " ; ID name ; ] @ typeuse ) ] ) :: acc ) gpad . primitives [ ] in let sexpl_globals = List . map ( fun ( name , mut , vtype ) -> L ( [ K " global " ; ID name ; ( if mut then L [ K " mut " ; K ( string_of_vtype vtype ) ] else K ( string_of_vtype vtype ) ) ; ] @ zero_expr_of_vtype vtype ) ) ( globals ( ) ) in sexpl_memory @ sexpl_functions @ sexpl_table @ sexpl_globals @ wasicaml_init @ wasicaml_get_data @ wasicaml_get_data_size Wc_reader . ( Array1 . dim exe . data ) @ alloc_fast @ alloc_slow ( ) @ grab_helper gpad @ return_helper @ appterm_helper ( ) @ restart_helper gpad @ ( if gpad . need_reinit_frame then reinit_frame else [ ] ) @ ( if gpad . need_mlookup then mlookup else [ ] ) @ ( if ! enable_deadbeef_check then deadbeef_init @ deadbeef_check else [ ] ) @ ( ISet . elements gpad . need_reinit_frame_k |> List . map reinit_frame_k |> List . flatten ) @ sexpl_code @ [ L ( [ K " data " ; L [ K " memory " ; N ( I32 0l ) ] ; ID " data " ; ] @ data ) ]
type repr = | RValue | RInt | RIntUnclean | RIntVal | RNatInt | RInt32 | RInt64 | RFloat
type global_lookup = | Glb of int | Env of int
type store = | RealStack of int | RealAccu of { no_function : bool } | Const of int | Local of repr * string | Atom of int | TracedGlobal of global_lookup * int list * Wc_traceglobals . initvalue | Invalid
type global = Global of int
type label = Label of int | Loop of int
type stack_descriptor = { stack_uninit : int list ; stack_depth : int ; stack_save_accu : bool ; }
type unop = | Pnegint | Pboolnot | Poffsetint of int | Pisint | Pgetfield of int | Pgetfloatfield of int | Pvectlength | Pgetpubmet of int
type uneffect = | Poffsetref of int | Psetglobal of global
type binop = | Paddint | Psubint | Pmulint | Pdivint | Pmodint | Pandint | Porint | Pxorint | Plslint | Plsrint | Pasrint | Pintcomp of Lambda . integer_comparison | Puintcomp of Lambda . integer_comparison | Pgetvectitem | Pgetstringchar | Pgetbyteschar | Pgetmethod | Pgetdynmet
type bineffect = | Psetfield of int | Psetfloatfield of int
type terneffect = | Psetvectitem | Psetbyteschar
type winstruction = | Wcomment of string | Wblock of { label : label ; body : winstruction list } | Wcond of { cond : bool ref ; ontrue : winstruction ; onfalse : winstruction } | Wcopy of { src : store ; dest : store } | Walloc of { src : store ; dest : store ; descr : stack_descriptor } | Wenv of { field : int } | Wcopyenv of { offset : int } | Wgetglobal of { src : global } | Wunary of { op : unop ; src1 : store ; dest : store } | Wunaryeffect of { op : uneffect ; src1 : store } | Wbinary of { op : binop ; src1 : store ; src2 : store ; dest : store } | Wbinaryeffect of { op : bineffect ; src1 : store ; src2 : store } | Wternaryeffect of { op : terneffect ; src1 : store ; src2 : store ; src3 : store } | Wmakeblock of { tag : int ; src : store list ; descr : stack_descriptor } | Wmakefloatblock of { src : store list ; descr : stack_descriptor } | Wccall of { name : string ; src : store list ; descr : stack_descriptor } | Wccall_vector of { name : string ; numargs : int ; depth : int ; descr : stack_descriptor } | Wbranch of { label : label } | Wif of { src : store ; neg : bool ; body : winstruction list } | Wswitch of { labels_int : label array ; labels_blk : label array ; src : store } | Wapply of { numargs : int ; depth : int } | Wapply_direct of { funlabel : int ; global : global_lookup ; path : int list ; numargs : int ; depth : int } | Wappterm of { numargs : int ; oldnumargs : int ; depth : int } | Wreturn of { src : store ; arity : int } | Wgrab of { numargs : int } | Wclosurerec of { src : store list ; dest ( : store * int ) list ; descr : stack_descriptor } | Wraise of { src : store ; kind : Lambda . raise_kind } | Wtrap of { trylabel : int ; catchlabel : int ; depth : int } | Wtryreturn of { src : store } | Wnextmain of { label : int } | Wstop | Wnop | Wunreachable
let repr_comparable_as_i32 r1 r2 = match r1 , r2 with | ( RValue | RIntVal ) , ( RValue | RIntVal ) -> true | ( RInt | RNatInt | RInt32 ) , ( RInt | RNatInt | RInt32 ) -> true | _ -> false
let repr_of_store = function | RealStack _ -> RValue | Const _ -> RInt | Local ( repr , _ ) -> repr | RealAccu _ -> RValue | Atom _ -> RValue | TracedGlobal _ -> RValue | Invalid -> assert false
let empty_descr = { stack_uninit = [ ] ; stack_depth = 0 ; stack_save_accu = false ; }
let string_of_store = function | RealStack pos -> sprintf " fp [ % d ] " pos | RealAccu _ -> " accu " | Const k -> sprintf " % d " k | Local ( repr , name ) -> name | Atom k -> sprintf " atom % d " k | TracedGlobal ( glb , path , _ ) -> let s_glb = match glb with | Glb i -> sprintf " global % d " i | Env i -> sprintf " env +% d " i in let s_path = if path = [ ] then " " else List . map ( fun d -> sprintf " [ % d ] " d ) path |> String . concat " " in sprintf " % s % s " s_glb s_path | Invalid -> " invalid "
let string_label = function | Label k -> sprintf " label % d " k | Loop k -> sprintf " loop % d " k
let extract_directly_callable_function st = match st with | TracedGlobal ( glb , path , FuncInEnv { func_offset ; env } ) -> let func = env . ( func_offset ) in ( match func with | Function { label } -> Some ( glb , path , label , env ) | _ -> None ) | _ -> None
let rec string_of_winstruction = function | Wcomment s -> s | Wblock arg -> ( match arg . label with | Label k -> sprintf " Wblock ( . . . , label % d ) " k | Loop k -> sprintf " Wblock ( loop % d , . . . ) " k ) | Wcond { cond ; ontrue ; onfalse } -> string_of_winstruction ( if ! cond then ontrue else onfalse ) | Wcopy arg -> sprintf " Wcopy ( % s = % s ) " ( string_of_store arg . dest ) ( string_of_store arg . src ) | Walloc arg -> sprintf " Walloc ( % s = % s ) " ( string_of_store arg . dest ) ( string_of_store arg . src ) | Wenv arg -> sprintf " Wenv ( accu = env [ % d ] ) " arg . field | Wcopyenv arg -> sprintf " Wcopyenv ( accu = env +% d ) " arg . offset | Wgetglobal { src = Global k } -> sprintf " Wgetglobal ( accu = global % d ) " k | Wunary arg -> sprintf " Wunary ( % s = f ( % s ) ) " ( string_of_store arg . dest ) ( string_of_store arg . src1 ) | Wunaryeffect arg -> sprintf " Wunaryeffect ( f ( % s ) ) " ( string_of_store arg . src1 ) | Wbinary arg -> sprintf " Wbinary ( % s = f ( % s , % s ) ) " ( string_of_store arg . dest ) ( string_of_store arg . src1 ) ( string_of_store arg . src2 ) | Wbinaryeffect arg -> sprintf " Wbinaryeffect ( f ( % s , % s ) ) " ( string_of_store arg . src1 ) ( string_of_store arg . src2 ) | Wternaryeffect arg -> sprintf " Wbinaryeffect ( f ( % s , % s , % s ) ) " ( string_of_store arg . src1 ) ( string_of_store arg . src2 ) ( string_of_store arg . src3 ) | Wmakeblock arg -> sprintf " Wmakeblock ( tag =% d , accu [ =% s ] ) " arg . tag ( List . map string_of_store arg . src |> String . concat " , " ) | Wmakefloatblock arg -> sprintf " Wmakefloatblock ( accu [ =% s ] ) " ( List . map string_of_store arg . src |> String . concat " , " ) | Wccall arg -> sprintf " Wccall ( accu = % S ( % s ) ) " arg . name ( List . map string_of_store arg . src |> String . concat " , " ) | Wccall_vector arg -> sprintf " Wccall_vector ( accu = % S ( fp [ % d ] . . . fp [ % d ] ) ) " arg . name ( - arg . depth ) ( - arg . depth + arg . numargs - 1 ) | Wbranch arg -> sprintf " Wbranch ( % s ) " ( string_label arg . label ) | Wif arg -> sprintf " Wif ( if % s % s -> % s ) " ( if arg . neg then " not " else " " ) ( string_of_store arg . src ) ( List . map string_of_winstruction arg . body |> String . concat " , " ) | Wswitch arg -> let s1 = Array . to_list arg . labels_int |> List . map string_label |> String . concat " , " in let s2 = Array . to_list arg . labels_blk |> List . map string_label |> String . concat " , " in sprintf " Wswitch ( % s ; % s on % s ) " s1 s2 ( string_of_store arg . src ) | Wapply arg -> sprintf " Wapply ( f = accu , args [ = fp [ % d ] . . . fp [ % d ] ] ) " ( - arg . depth ) ( - arg . depth + arg . numargs - 1 ) | Wapply_direct arg -> sprintf " Wapply_direct ( f = letrec % d , args [ = fp [ % d ] . . . fp [ % d ] ] ) " arg . funlabel ( - arg . depth ) ( - arg . depth + arg . numargs - 1 ) | Wappterm arg -> sprintf " Wappterm ( f = accu , args [ = fp [ % d ] . . . fp [ % d ] ] , oldnum =% d ) " ( - arg . depth ) ( - arg . depth + arg . numargs - 1 ) arg . oldnumargs | Wreturn arg -> sprintf " Wreturn ( % s ) " ( string_of_store arg . src ) | Wgrab arg -> sprintf " Wgrab ( num =% d ) " arg . numargs | Wclosurerec arg -> sprintf " Wclosurerec ( env [ =% s ] , dest [ =% s ] ) " ( List . map string_of_store arg . src |> String . concat " , " ) ( List . map ( fun ( s , lab ) -> sprintf " % s = label % d " ( string_of_store s ) lab ) arg . dest |> String . concat " , " ) | Wraise arg -> sprintf " Wraise ( % s ) " ( string_of_store arg . src ) | Wtrap arg -> sprintf " Wtrap ( try label % d catch label % d , depth =% d ) " arg . trylabel arg . catchlabel arg . depth | Wtryreturn arg -> sprintf " Wtryreturn ( % s ) " ( string_of_store arg . src ) | Wnextmain arg -> sprintf " Wnextmain ( letrec_main % d ) " arg . label | Wstop -> " Wstop " | Wnop -> " Wnop " | Wunreachable -> " Wunreachable "
let mk_table l = let tab = Hashtbl . create 7 in List . iter ( fun k -> Hashtbl . add tab k ( ) ) l ; tab
let prims_non_func_result = [ " caml_abs_float " ; " caml_acos_float " ; " caml_add_debug_info " ; " caml_add_float " ; " caml_alloc_dummy_float " ; " caml_array_append " ; " caml_array_blit " ; " caml_array_concat " ; " caml_array_fill " ; " caml_array_get_float " ; " caml_array_set " ; " caml_array_set_addr " ; " caml_array_set_float " ; " caml_array_sub " ; " caml_array_unsafe_get_float " ; " caml_array_unsafe_set " ; " caml_array_unsafe_set_addr " ; " caml_array_unsafe_set_float " ; " caml_asin_float " ; " caml_atan2_float " ; " caml_atan_float " ; " caml_ba_blit " ; " caml_ba_change_layout " ; " caml_ba_create " ; " caml_ba_dim " ; " caml_ba_dim_1 " ; " caml_ba_dim_2 " ; " caml_ba_dim_3 " ; " caml_ba_fill " ; " caml_ba_get_1 " ; " caml_ba_get_2 " ; " caml_ba_get_3 " ; " caml_ba_get_generic " ; " caml_ba_kind " ; " caml_ba_layout " ; " caml_ba_num_dims " ; " caml_ba_reshape " ; " caml_ba_set_1 " ; " caml_ba_set_2 " ; " caml_ba_set_3 " ; " caml_ba_set_generic " ; " caml_ba_slice " ; " caml_ba_sub " ; " caml_ba_uint8_get16 " ; " caml_ba_uint8_get32 " ; " caml_ba_uint8_get64 " ; " caml_ba_uint8_set16 " ; " caml_ba_uint8_set32 " ; " caml_ba_uint8_set64 " ; " caml_backtrace_status " ; " caml_blit_bytes " ; " caml_blit_string " ; " caml_bswap16 " ; " caml_bytes_compare " ; " caml_bytes_equal " ; " caml_bytes_get " ; " caml_bytes_get16 " ; " caml_bytes_get32 " ; " caml_bytes_get64 " ; " caml_bytes_greaterequal " ; " caml_bytes_greaterthan " ; " caml_bytes_lessequal " ; " caml_bytes_lessthan " ; " caml_bytes_notequal " ; " caml_bytes_of_string " ; " caml_bytes_set " ; " caml_bytes_set16 " ; " caml_bytes_set32 " ; " caml_bytes_set64 " ; " caml_ceil_float " ; " caml_channel_descriptor " ; " caml_classify_float " ; " caml_compare " ; " caml_convert_raw_backtrace " ; " caml_convert_raw_backtrace_slot " ; " caml_copysign_float " ; " caml_cos_float " ; " caml_cosh_float " ; " caml_create_bytes " ; " caml_create_string " ; " caml_div_float " ; " caml_dynlink_add_primitive " ; " caml_dynlink_close_lib " ; " caml_dynlink_get_current_libs " ; " caml_dynlink_lookup_symbol " ; " caml_dynlink_open_lib " ; " caml_ensure_stack_capacity " ; " caml_ephe_blit_data " ; " caml_ephe_blit_key " ; " caml_ephe_check_data " ; " caml_ephe_check_key " ; " caml_ephe_create " ; " caml_ephe_get_data " ; " caml_ephe_get_data_copy " ; " caml_ephe_get_key " ; " caml_ephe_get_key_copy " ; " caml_ephe_set_data " ; " caml_ephe_set_key " ; " caml_ephe_unset_data " ; " caml_ephe_unset_key " ; " caml_eq_float " ; " caml_equal " ; " caml_eventlog_pause " ; " caml_eventlog_resume " ; " caml_exp_float " ; " caml_expm1_float " ; " caml_fill_bytes " ; " caml_fill_string " ; " caml_final_register " ; " caml_final_register_called_without_value " ; " caml_final_release " ; " caml_float_compare " ; " caml_float_of_int " ; " caml_float_of_string " ; " caml_floatarray_blit " ; " caml_floatarray_create " ; " caml_floatarray_get " ; " caml_floatarray_set " ; " caml_floatarray_unsafe_get " ; " caml_floatarray_unsafe_set " ; " caml_floor_float " ; " caml_fma_float " ; " caml_fmod_float " ; " caml_format_float " ; " caml_format_int " ; " caml_fresh_oo_id " ; " caml_frexp_float " ; " caml_gc_compaction " ; " caml_gc_counters " ; " caml_gc_full_major " ; " caml_gc_get " ; " caml_gc_huge_fallback_count " ; " caml_gc_major " ; " caml_gc_major_slice " ; " caml_gc_minor " ; " caml_gc_minor_words " ; " caml_gc_quick_stat " ; " caml_gc_set " ; " caml_gc_stat " ; " caml_ge_float " ; " caml_get_current_callstack " ; " caml_get_current_environment " ; " caml_get_exception_backtrace " ; " caml_get_exception_raw_backtrace " ; " caml_get_global_data " ; " caml_get_major_bucket " ; " caml_get_major_credit " ; " caml_get_minor_free " ; " caml_get_public_method " ; " caml_get_section_table " ; " caml_greaterequal " ; " caml_greaterthan " ; " caml_gt_float " ; " caml_hash " ; " caml_hexstring_of_float " ; " caml_hypot_float " ; " caml_install_signal_handler " ; " caml_int32_add " ; " caml_int32_and " ; " caml_int32_bits_of_float " ; " caml_int32_bswap " ; " caml_int32_compare " ; " caml_int32_div " ; " caml_int32_float_of_bits " ; " caml_int32_format " ; " caml_int32_mod " ; " caml_int32_mul " ; " caml_int32_neg " ; " caml_int32_of_float " ; " caml_int32_of_int " ; " caml_int32_of_string " ; " caml_int32_or " ; " caml_int32_shift_left " ; " caml_int32_shift_right " ; " caml_int32_shift_right_unsigned " ; " caml_int32_sub " ; " caml_int32_to_float " ; " caml_int32_to_int " ; " caml_int32_xor " ; " caml_int64_add " ; " caml_int64_add_native " ; " caml_int64_and " ; " caml_int64_and_native " ; " caml_int64_bits_of_float " ; " caml_int64_bswap " ; " caml_int64_compare " ; " caml_int64_div " ; " caml_int64_div_native " ; " caml_int64_float_of_bits " ; " caml_int64_format " ; " caml_int64_mod " ; " caml_int64_mod_native " ; " caml_int64_mul " ; " caml_int64_mul_native " ; " caml_int64_neg " ; " caml_int64_neg_native " ; " caml_int64_of_float " ; " caml_int64_of_int " ; " caml_int64_of_int32 " ; " caml_int64_of_nativeint " ; " caml_int64_of_string " ; " caml_int64_or " ; " caml_int64_or_native " ; " caml_int64_shift_left " ; " caml_int64_shift_right " ; " caml_int64_shift_right_unsigned " ; " caml_int64_sub " ; " caml_int64_sub_native " ; " caml_int64_to_float " ; " caml_int64_to_int " ; " caml_int64_to_int32 " ; " caml_int64_to_nativeint " ; " caml_int64_xor " ; " caml_int64_xor_native " ; " caml_int_as_pointer " ; " caml_int_compare " ; " caml_int_of_float " ; " caml_int_of_string " ; " caml_lazy_make_forward " ; " caml_ldexp_float " ; " caml_le_float " ; " caml_lessequal " ; " caml_lessthan " ; " caml_lex_engine " ; " caml_log10_float " ; " caml_log1p_float " ; " caml_log_float " ; " caml_lt_float " ; " caml_make_array " ; " caml_make_float_vect " ; " caml_make_vect " ; " caml_marshal_data_size " ; " caml_md5_chan " ; " caml_md5_string " ; " caml_memprof_start " ; " caml_memprof_stop " ; " caml_ml_bytes_length " ; " caml_ml_channel_size " ; " caml_ml_channel_size_64 " ; " caml_ml_close_channel " ; " caml_ml_debug_info_status " ; " caml_ml_enable_runtime_warnings " ; " caml_ml_flush " ; " caml_ml_input " ; " caml_ml_input_char " ; " caml_ml_input_int " ; " caml_ml_input_scan_line " ; " caml_ml_open_descriptor_in " ; " caml_ml_open_descriptor_out " ; " caml_ml_out_channels_list " ; " caml_ml_output " ; " caml_ml_output_bytes " ; " caml_ml_output_char " ; " caml_ml_output_int " ; " caml_ml_pos_in " ; " caml_ml_pos_in_64 " ; " caml_ml_pos_out " ; " caml_ml_pos_out_64 " ; " caml_ml_runtime_warnings_enabled " ; " caml_ml_seek_in " ; " caml_ml_seek_in_64 " ; " caml_ml_seek_out " ; " caml_ml_seek_out_64 " ; " caml_ml_set_binary_mode " ; " caml_ml_set_channel_name " ; " caml_ml_string_length " ; " caml_modf_float " ; " caml_mul_float " ; " caml_nativeint_add " ; " caml_nativeint_and " ; " caml_nativeint_bswap " ; " caml_nativeint_compare " ; " caml_nativeint_div " ; " caml_nativeint_format " ; " caml_nativeint_mod " ; " caml_nativeint_mul " ; " caml_nativeint_neg " ; " caml_nativeint_of_float " ; " caml_nativeint_of_int " ; " caml_nativeint_of_int32 " ; " caml_nativeint_of_string " ; " caml_nativeint_or " ; " caml_nativeint_shift_left " ; " caml_nativeint_shift_right " ; " caml_nativeint_shift_right_unsigned " ; " caml_nativeint_sub " ; " caml_nativeint_to_float " ; " caml_nativeint_to_int " ; " caml_nativeint_to_int32 " ; " caml_nativeint_xor " ; " caml_neg_float " ; " caml_neq_float " ; " caml_new_lex_engine " ; " caml_nextafter_float " ; " caml_notequal " ; " caml_obj_block " ; " caml_obj_make_forward " ; " caml_obj_reachable_words " ; " caml_obj_set_raw_field " ; " caml_obj_set_tag " ; " caml_obj_tag " ; " caml_output_value " ; " caml_output_value_to_buffer " ; " caml_output_value_to_bytes " ; " caml_output_value_to_string " ; " caml_parse_engine " ; " caml_power_float " ; " caml_raw_backtrace_length " ; " caml_raw_backtrace_next_slot " ; " caml_raw_backtrace_slot " ; " caml_realloc_global " ; " caml_record_backtrace " ; " caml_register_named_value " ; " caml_reify_bytecode " ; " caml_remove_debug_info " ; " caml_reset_afl_instrumentation " ; " caml_restore_raw_backtrace " ; " caml_round_float " ; " caml_runtime_parameters " ; " caml_runtime_variant " ; " caml_set_oo_id " ; " caml_set_parser_trace " ; " caml_setup_afl " ; " caml_signbit " ; " caml_signbit_float " ; " caml_sin_float " ; " caml_sinh_float " ; " caml_sqrt_float " ; " caml_static_release_bytecode " ; " caml_string_compare " ; " caml_string_equal " ; " caml_string_get " ; " caml_string_get16 " ; " caml_string_get32 " ; " caml_string_get64 " ; " caml_string_greaterequal " ; " caml_string_greaterthan " ; " caml_string_lessequal " ; " caml_string_lessthan " ; " caml_string_notequal " ; " caml_string_of_bytes " ; " caml_string_set " ; " caml_sub_float " ; " caml_sys_argv " ; " caml_sys_chdir " ; " caml_sys_close " ; " caml_sys_const_backend_type " ; " caml_sys_const_big_endian " ; " caml_sys_const_int_size " ; " caml_sys_const_max_wosize " ; " caml_sys_const_naked_pointers_checked " ; " caml_sys_const_ostype_cygwin " ; " caml_sys_const_ostype_unix " ; " caml_sys_const_ostype_win32 " ; " caml_sys_const_word_size " ; " caml_sys_executable_name " ; " caml_sys_exit " ; " caml_sys_file_exists " ; " caml_sys_get_argv " ; " caml_sys_get_config " ; " caml_sys_getcwd " ; " caml_sys_getenv " ; " caml_sys_is_directory " ; " caml_sys_isatty " ; " caml_sys_mkdir " ; " caml_sys_modify_argv " ; " caml_sys_open " ; " caml_sys_random_seed " ; " caml_sys_read_directory " ; " caml_sys_remove " ; " caml_sys_rename " ; " caml_sys_rmdir " ; " caml_sys_system_command " ; " caml_sys_time " ; " caml_sys_time_include_children " ; " caml_sys_unsafe_getenv " ; " caml_tan_float " ; " caml_tanh_float " ; " caml_terminfo_rows " ; " caml_trunc_float " ; " caml_update_dummy " ; " caml_weak_blit " ; " caml_weak_check " ; " caml_weak_create " ; " caml_weak_get " ; " caml_weak_get_copy " ; " caml_weak_set " ; ] |> mk_table
type executable = { dll_paths : string list ; dll_names : string list ; primitives : string array ; code : string ; data : ( char , int8_unsigned_elt , c_layout ) Array1 . t ; symbols : Symtable . global_map ; debug : ( int , string ) Hashtbl . t ; }
let read_bigstring f len = let ba = Array1 . create Char c_layout len in let by = Bytes . create 4096 in let n = ref len in let k = ref 0 in while ! n > 0 do let p = min ! n 4096 in really_input f by 0 p ; for i = 0 to p - 1 do ba . { ! k + i } <- Bytes . get by i done ; k := ! k + p ; n := ! n - p ; done ; ba
let input_stringlist f section = try let s = Bytesections . read_section_string f section in let l = String . split_on_char ' \ 000 ' s in List . filter ( fun elem -> elem <> " " ) l with | Not_found -> [ ]
let input_objarray_as_bigstring f section = try let _len = Bytesections . seek_section f section in let p1 = pos_in f in let _dummay = Marshal . from_channel f in let p2 = pos_in f in let _len = Bytesections . seek_section f section in read_bigstring f ( p2 - p1 ) with | Not_found -> Array1 . create Char c_layout 0
let input_symbols f section : Symtable . global_map = try let _len = Bytesections . seek_section f section in Marshal . from_channel f with | Not_found -> Symtable . empty_global_map
let input_debug f section = let ht = Hashtbl . create 7 in try let _len = Bytesections . seek_section f section in let n = input_binary_int f in for k = 1 to n do let ofs = input_binary_int f in let evl = input_value f in let _ = input_value f in List . iter ( fun ev -> Hashtbl . add ht I . ( ( ofs + ev . ev_pos ) / 4 ) I . ( ev . ev_defname ) ) evl done ; ht with | Not_found -> ht
let read_executable name = let f = open_in name in Bytesections . read_toc f ; let dll_paths = input_stringlist f " DLPT " in let dll_names = input_stringlist f " DLLS " in let primitives = input_stringlist f " PRIM " |> Array . of_list in let code = Bytesections . read_section_string f " CODE " in let data = input_objarray_as_bigstring f " DATA " in let symbols = input_symbols f " SYMB " in let debug = input_debug f " DBUG " in close_in f ; { dll_paths ; dll_names ; primitives ; code ; data ; symbols ; debug }