text
stringlengths
12
786k
let func_labels instr = match instr with | I . Kclosure ( l , _ ) -> [ l ] | Kclosurerec ( ll , _ ) -> ll | _ -> [ ]
let jump_labels instr = match instr with | I . Kbranch l -> [ l ] | Kbranchif l -> [ l ] | Kbranchifnot l -> [ l ] | Kswitch ( la1 , la2 ) -> Array . to_list la1 @ Array . to_list la2 | Kpushtrap l -> [ l ] | _ -> [ ]
let not_continuing instr = match instr with | I . Kbranch _ -> true | Kreturn _ -> true | Kappterm _ -> true | Kswitch _ -> true | Kraise _ -> true | Kstop -> true | _ -> false
let is_trapping instr = match instr with | I . Kpushtrap _ -> true | _ -> false
let create_cfg code labels = let labels = ref labels in let max_label = ref ( Array . length code - 1 ) in let new_label ( ) = let label = ! max_label + 1 in labels := ISet . add label ! labels ; max_label := label ; label in let nodes = Hashtbl . create 7 in let queued = Hashtbl . c...
let split_main_function cfg = let depth_table = Hashtbl . create 7 in let excluded = Hashtbl . create 7 in let exclude p = Hashtbl . replace excluded p true in let rec set_depth depth label = if not ( Hashtbl . mem depth_table label ) then ( Hashtbl . add depth_table label depth ; let node = t...
let is_node_in_loop ctx loop_label label = let node = try IMap . find label ctx . nodes with Not_found -> assert false in List . mem loop_label node . cfg_loops
let recover_structure ctx = let in_degree = ref IMap . empty in let rec inc_degree trail label = let deg = try IMap . find label ! in_degree with Not_found -> 0 in in_degree := IMap . add label ( deg + 1 ) ! in_degree ; if deg = 0 then ( let node = try IMap . find label ctx . nodes with ...
let validate scode = let error func_label last_label message = failwith ( sprintf " validation error function % d near % d : % s " func_label last_label message ) in let rec validate_block labels_in_scope func_label last_label block = if block . loop_label <> None && block . break_label <> None th...
let prefix = Sys . getenv " HOME " ^ " . / wasicaml "
let size_of_function func = let rec size_of_block block = Array . fold_left ( fun acc instr -> match instr with | Block b -> acc + size_of_block b | Label _ -> acc | _ -> acc + 1 ) 0 block . instructions in size_of_block func . block
let size_table s get_defname = let get_letrec_name func = let label = Option . value ~ default : 0 func . scope . cfg_letrec_label in let prefix = if label = 0 then " letrec_main " else if func . scope . cfg_main then sprintf " letrec_main % d " label else sprintf " letrec % d " label i...
let main ( ) = let out = ref " a . out " in let inp = ref None in let cclib = ref [ ] in let cstack = ref ( 1024 * 1024 ) in let quiet = ref false in Arg . parse [ " - o " , Arg . Set_string out , " < file > Set the output file " ; " - cclib " , Arg . String ( ...
let ( ) = try main ( ) with | Failure msg -> eprintf " % s \ n " %! msg ; exit 2 | Arg . Bad msg -> eprintf " % s \ n " %! msg ; exit 2
type wasm_value_type = | TI32 | TI64 | TF64
let string_of_vtype = function | TI32 -> " i32 " | TI64 -> " i64 " | TF64 -> " f64 "
let zero_expr_of_vtype = function | TI32 -> [ L [ K " i32 . const " ; N ( I32 0l ) ] ] | TI64 -> [ L [ K " i64 . const " ; N ( I64 0L ) ] ] | TF64 -> [ L [ K " f64 . const " ; N ( F64 0 . 0 ) ] ]
type letrec_label = | Func of int | Main of int
type gpad = { letrec_name : ( letrec_label , string ) Hashtbl . t ; primitives : ( string , sexp list ) Hashtbl . t ; funcmapping : ( int , letrec_label * int ) Hashtbl . t ; subfunctions : ( letrec_label , int list ) Hashtbl . t ; wasmindex : ( letrec_label , int ) ...
type fpad = { lpad : Wc_unstack . lpad ; fpad_letrec_label : letrec_label ; mutable fpad_scope : Wc_control . cfg_scope ; mutable maxdepth : int ; mutable need_appterm_common : bool ; mutable need_return : bool ; mutable need_panic : bool ; mutable need_tmp1_i32 : bool ; mutable need_tmp2...
let enable_multireturn = ref false
let enable_deadbeef_check = ref false
let make_header size tag = ( size lsl 10 ) lor tag
let vtype repr = match repr with | RValue | RInt | RIntUnclean | RIntVal | RNatInt | RInt32 -> TI32 | RInt64 -> TI64 | RFloat -> TF64
let empty_fpad ( ) = { lpad = Wc_unstack . empty_lpad ( ) ; fpad_letrec_label = Main 0 ; fpad_scope = { cfg_letrec_label = None ; cfg_func_label = 0 ; cfg_try_labels = [ ] ; cfg_main = false } ; maxdepth = 0 ; need_appterm_common = false ; need_return = false ; need_p...
let new_local fpad repr = Wc_unstack . new_local fpad . lpad repr
let req_tmp1_i32 fpad = if fpad . lpad . avoid_locals then ( fpad . need_tmp1_i32 <- true ; " tmp1_i32 " ) else new_local fpad RInt
let req_tmp2_i32 fpad = if fpad . lpad . avoid_locals then ( fpad . need_tmp2_i32 <- true ; " tmp2_i32 " ) else new_local fpad RInt
let req_tmp1_f64 fpad = if fpad . lpad . avoid_locals then ( fpad . need_tmp1_f64 <- true ; " tmp1_f64 " ) else new_local fpad RFloat
let set_bp_1 fpad = [ L [ K " i32 . const " ; N ( I32 ( Int32 . of_int ( 4 * fpad . maxdepth ) ) ) ] ; L [ K " i32 . sub " ] ; L [ K " local . tee " ; ID " bp " ] ; L [ K " global . get " ; ID " wasicaml_stack_threshold " ] ; L [ K " ...
let set_bp fpad = if fpad . maxdepth = 0 then [ ] else [ L [ K " local . get " ; ID " fp " ] ] @ set_bp_1 fpad
let push_const n = [ L [ K " i32 . const " ; N ( I32 n ) ] ]
let push_local var = [ L [ K " local . get " ; ID var ] ]
let pop_to_local var = [ L [ K " local . set " ; ID var ] ]
let pop_to_fp fpad = if fpad . maxdepth = 0 then pop_to_local " fp " else [ L [ K " local . tee " ; ID " fp " ] ] @ set_bp_1 fpad
let load_offset offset = if offset >= 0 then [ L [ K " i32 . load " ; K ( sprintf " offset = 0x % lx " ( Int32 . of_int offset ) ) ; K " align = 2 " ; ] ] else [ L [ K " i32 . const " ; N ( I32 ( Int32 . of_int ( - offset ) ) ) ] ; L [ K " ...
let add_offset offset = if offset <> 0 then [ L [ K " i32 . const " ; N ( I32 ( Int32 . of_int offset ) ) ] ; L [ K " i32 . add " ] ] else [ ]
let push_env = [ L [ K " local . get " ; ID " envptr " ] ; L [ K " i32 . load " ; K " align = 2 " ] ]
let push_field var_base field = [ L [ K " local . get " ; ID var_base ; ] ] @ load_offset ( 4 * field )
let push_global_field var_base field = [ L [ K " global . get " ; ID var_base ; ] ] @ load_offset ( 4 * field )
let push_field_addr var_base field = [ L [ K " local . get " ; ID var_base ] ] @ if field <> 0 then [ L [ K " i32 . const " ; N ( I32 ( Int32 . of_int ( 4 * field ) ) ) ; ] ; L [ K " i32 . add " ] ] else [ ]
let push_global_field_addr var_base field = [ L [ K " global . get " ; ID var_base ; ] ; L [ K " i32 . const " ; N ( I32 ( Int32 . of_int ( 4 * field ) ) ) ; ] ; L [ K " i32 . add " ] ]
let push_stack fpad pos = if pos >= 0 then push_field " fp " pos else push_field " bp " ( pos + fpad . maxdepth )
let push_domain_field field = [ L [ K " global . get " ; ID " wasicaml_domain_state " ] ; L [ K " i32 . load " ; K ( sprintf " offset = 0x % lx " ( Int32 . of_int ( 8 * field ) ) ) ; K " align = 2 " ; ] ; ]
let store_offset addr offset code_value = if offset >= 0 then [ L [ K " local . get " ; ID addr ] ] @ code_value @ [ L [ K " i32 . store " ; K ( sprintf " offset = 0x % lx " ( Int32 . of_int offset ) ) ; K " align = 2 " ; ] ] else [ L [ K " local . ...
let pop_to_field var_base field code_value = store_offset var_base ( 4 * field ) code_value
let pop_to_double_field var_base field code_value = [ L [ K " local . get " ; ID var_base ] ; L [ K " i32 . const " ; N ( I32 ( Int32 . of_int ( 4 * double_size * field ) ) ) ; ] ; L [ K " i32 . add " ] ] @ code_value @ [ L [ K " f64 . store " ; ...
let pop_to_domain_field field code_value = [ L [ K " global . get " ; ID " wasicaml_domain_state " ] ] @ code_value @ [ L [ K " i32 . store " ; K ( sprintf " offset = 0x % lx " ( Int32 . of_int ( 8 * field ) ) ) ; K " align = 2 " ; ] ; ]
let pop_to_stack fpad pos code_value = if pos >= 0 then pop_to_field " fp " pos code_value else pop_to_field " bp " ( pos + fpad . maxdepth ) code_value
let load_double = [ L [ K " f64 . load " ; K " align = 2 " ] ]
let debug2 x0 x1 = [ L [ K " i32 . const " ; N ( I32 ( Int32 . of_int x0 ) ) ] ; L [ K " i32 . const " ; N ( I32 ( Int32 . of_int x1 ) ) ] ; L [ K " call " ; ID " debug2 " ] ]
let debug2_var x0 var = [ L [ K " i32 . const " ; N ( I32 ( Int32 . of_int x0 ) ) ] ; L [ K " local . get " ; ID var ] ; L [ K " call " ; ID " debug2 " ] ]
let deadbeef_init = [ L ( [ [ K " func " ; ID " deadbeef_init " ; L [ K " param " ; ID " bp " ; K " i32 " ] ; L [ K " param " ; ID " fp " ; K " i32 " ] ; ] ; [ L [ K " block " ; ID " loop_exit " ; BR ; L [ K " loop " ; ID " ...
let deadbeef_check = [ L ( [ [ K " func " ; ID " deadbeef_check " ; L [ K " param " ; ID " ptr " ; K " i32 " ] ; L [ K " param " ; ID " fp " ; K " i32 " ] ; ] ; push_local " ptr " ; push_local " fp " ; [ L [ K " i32 . gt_u " ] ...
let stack_init fpad descr = List . map ( fun pos -> pop_to_stack fpad pos ( push_const 1l ) ) descr . stack_uninit |> List . flatten
let setup_for_gc fpad descr = let sp_decr = if descr . stack_save_accu then 1 else 0 in let sexpl_stack = stack_init fpad descr in let sexpl_accu = if descr . stack_save_accu then push_local " accu " |> pop_to_stack fpad ( - descr . stack_depth - 1 ) else [ ] in let sexpl_extern_sp = ( [ ...
let restore_after_gc fpad descr = if descr . stack_save_accu then push_stack fpad ( - descr . stack_depth - 1 ) @ pop_to_local " accu " else [ ]
let alloc_atom fpad tag = [ L [ K " global . get " ; ID " wasicaml_atom_table " ; ] ; L [ K " i32 . const " ; N ( I32 ( Int32 . of_int ( 4 * tag ) ) ) ; ] ; L [ K " i32 . add " ] ; ]
let alloc_fast = [ L ( [ [ K " func " ; ID " alloc_fast " ; L [ K " param " ; ID " bhsize " ; K " i32 " ] ; L [ K " param " ; ID " header " ; K " i32 " ] ; BR ; L [ K " result " ; C " ptr " ; K " i32 " ] ; L [ K " local " ; ...
let alloc_slow ( ) = [ L ( [ [ K " func " ; ID " alloc_slow " ; L [ K " param " ; ID " wosize " ; K " i32 " ] ; L [ K " param " ; ID " header " ; K " i32 " ] ; L [ K " param " ; ID " fp " ; K " i32 " ] ; L [ K " param " ; ...
let call_alloc_slow ( ) = [ L [ K " call " ; ID " alloc_slow " ] ] @ if ! enable_multireturn then [ ] else [ L [ K " global . get " ; ID " retval2 " ] ]
let alloc_non_atom fpad descr size tag = fpad . need_xalloc <- true ; let ptr = " xalloc " in let young = size <= max_young_wosize in let code = if young then [ push_const ( Int32 . of_int ( 4 * ( size + 1 ) ) ) ; push_const ( Int32 . of_int ( make_header size tag ) ) ; [...
let alloc fpad descr size tag = if size = 0 then alloc_atom fpad tag else let ( code , ptr , _ ) = alloc_non_atom fpad descr size tag in code @ push_local ptr
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 " ...
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 "...
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 " ...
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 " ; ...
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 " ;...
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 " pa...
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 " me...
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 ....
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 " ] ; ...
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 ...
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 , _ ) ...
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 n...
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_allo...
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 | _ -> asser...
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 " ] ...
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 " ...
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...
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 ) |>...
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...
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 -> emi...
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 RVal...
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 "...