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 . create 7 in let pushtraps = ref IMap . empty in let todo = Queue . create ( ) in let m = ref 0 in let init_scope = { cfg_letrec_label = None ; cfg_func_label = 0 ; cfg_try_labels = [ ] ; cfg_main = true ; } in Queue . add ( init_scope , None , 0 ) todo ; let add where scope popfrom start = try let node = Hashtbl . find nodes start in if node . cfg_scope <> scope then ( eprintf " [ DEBUG ] where =% d start =% d ex_scope =% s new_scope =% s \ n " %! where start ( string_of_scope node . cfg_scope ) ( string_of_scope scope ) ; failwith " bad scoping " ; ) with Not_found -> if not ( Hashtbl . mem queued start ) then ( Queue . add ( scope , popfrom , start ) todo ; Hashtbl . add queued start ( ) ; ) in while not ( Queue . is_empty todo ) do let ( cfg_scope , popfrom , start ) = Queue . take todo in Hashtbl . remove queued start ; if start > ! m then ( m := start ; ) ; let n = ref 0 in let eon = ref false in while not ! eon do let jlabs = jump_labels code . ( start + ! n ) in let flabs = func_labels code . ( start + ! n ) in let no_cont = not_continuing code . ( start + ! n ) in incr n ; let end_of_try = start + ! n < Array . length code && code . ( start + ! n ) = Kpoptrap in eon := no_cont || jlabs <> [ ] || ISet . mem ( start + ! n ) ! labels || end_of_try ; List . iter ( fun flab -> let scope = { cfg_letrec_label = Some ( List . hd flabs ) ; cfg_func_label = flab ; cfg_try_labels = [ ] ; cfg_main = false ; } in add ( start + ! n - 1 ) scope None flab ) flabs done ; let last_no_cont = not_continuing code . ( start + ! n - 1 ) in let last_clabs = if last_no_cont then [ ] else [ start + ! n ] in let last_jlabs = jump_labels code . ( start + ! n - 1 ) in let cfg_succ = last_clabs @ last_jlabs in let is_pushtrap = is_trapping code . ( start + ! n - 1 ) in let is_poptrap = code . ( start ) = Kpoptrap in let cfg_final , cfg_length = match code . ( start + ! n - 1 ) with | Kbranch lab as instr -> ( Some instr , ! n - 1 ) | _ -> if last_no_cont || is_pushtrap then ( None , ! n ) else ( Some ( I . Kbranch ( start + ! n ) ) , ! n ) in let cfg_try = match cfg_scope . cfg_try_labels with | lab :: _ when lab = start -> let exit_label = new_label ( ) in let exit_node = { cfg_scope ; cfg_node_label = exit_label ; cfg_try = Some Try_exit ; cfg_trap = None ; cfg_loops = [ ] ; cfg_succ = [ ] ; cfg_length = 0 ; cfg_final = None ; cfg_next_main = None ; } in Hashtbl . replace nodes exit_label exit_node ; Some ( Try_entry exit_label ) | _ -> None in if is_pushtrap then pushtraps := IMap . add ( start + ! n ) start ! pushtraps ; let cfg_trap = if is_pushtrap then Some ( Trap_push ( start + ! n , None ) ) else if is_poptrap then match popfrom with | None -> eprintf " [ DEBUG ] start =% d n =% d \ n " %! start ! n ; assert false | Some lab -> let push_label = try IMap . find lab ! pushtraps with Not_found -> assert false in let push_node = Hashtbl . find nodes push_label in let push_node ' = { push_node with cfg_trap = ( match push_node . cfg_trap with | Some ( Trap_push ( try_label , _ ) ) -> Some ( Trap_push ( try_label , Some start ) ) | _ -> assert false ) ; cfg_succ = start :: List . tl push_node . cfg_succ } in Hashtbl . replace nodes push_label push_node ' ; let try_node = Hashtbl . find nodes lab in let exit_node = match try_node . cfg_try with | Some ( Try_entry l ) -> l | _ -> assert false in Some ( Trap_pop ( lab , exit_node ) ) else None in let cfg_node = { cfg_scope ; cfg_node_label = start ; cfg_try ; cfg_trap ; cfg_loops = [ ] ; cfg_succ ; cfg_length ; cfg_final ; cfg_next_main = None ; } in Hashtbl . replace nodes start cfg_node ; if not last_no_cont then ( let next = start + ! n in let scope = if is_pushtrap then { cfg_scope with cfg_try_labels = next :: cfg_scope . cfg_try_labels } else if code . ( next ) = Kpoptrap then { cfg_scope with cfg_try_labels = List . tl cfg_scope . cfg_try_labels } else cfg_scope in let popfrom = if code . ( next ) = Kpoptrap then Some ( List . hd cfg_scope . cfg_try_labels ) else None in add next scope popfrom next ) ; List . iter ( fun lab -> let scope , popfrom = if code . ( lab ) = Kpoptrap then { cfg_scope with cfg_try_labels = List . tl cfg_scope . cfg_try_labels } , Some ( List . hd cfg_scope . cfg_try_labels ) else cfg_scope , None in add ( start + ! n - 1 ) scope popfrom lab ) last_jlabs done ; let map_label lab = try let n = Hashtbl . find nodes lab in match n . cfg_trap with | Some ( Trap_pop ( _ , exit_label ) ) -> exit_label | _ -> lab with Not_found -> lab in let nodemap = ref IMap . empty in Hashtbl . iter ( fun label node -> let node = match node . cfg_trap with | Some ( Trap_push _ ) -> node | _ -> { node with cfg_succ = List . map map_label node . cfg_succ ; cfg_final = ( match node . cfg_final with | None -> None | Some instr -> Some ( Wc_reader . map_label_in_instr map_label instr ) ) ; } in nodemap := IMap . add label node ! nodemap ) nodes ; let code = Array . map ( Wc_reader . map_label_in_instr map_label ) code in let cfg = { nodes = ! nodemap ; code ; labels = ! labels ; } in detect_loops cfg ; cfg |
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 = try IMap . find label cfg . nodes with Not_found -> assert false in assert ( node . cfg_scope . cfg_letrec_label = None ) ; assert ( node . cfg_scope . cfg_main ) ; if node . cfg_loops <> [ ] then exclude label ; if node . cfg_trap <> None then exclude label ; let d = ref depth in for lab = label to label + node . cfg_length - 1 do let instr = cfg . code . ( lab ) in d := Wc_traceinstr . trace_stack_instr ! d instr ; match instr with | I . Kbranch _ -> assert false | I . Kbranchif p | I . Kbranchifnot p -> exclude p | I . Kswitch ( plist , qlist ) -> Array . iter exclude plist ; Array . iter exclude qlist ; | _ -> ( ) done ; List . iter ( set_depth ! d ) node . cfg_succ ) in let starts_another_function label = Hashtbl . find depth_table label = 0 && not ( Hashtbl . mem excluded label ) in let visited = Hashtbl . create 7 in let rec split_function new_letrec_label label = if not ( Hashtbl . mem visited label ) then ( Hashtbl . add visited label true ; let node = try IMap . find label cfg . nodes with Not_found -> assert false in let cfg_scope = { node . cfg_scope with cfg_letrec_label = new_letrec_label ; cfg_func_label = new_letrec_label |> Option . value ~ default : 0 ; } in let cfg_final , cfg_next_main = match node . cfg_final with | Some ( I . Kbranch lab ) when starts_another_function lab -> ( None , Some lab ) | _ -> ( node . cfg_final , None ) in let next_funcs , cfg_succ = List . partition starts_another_function node . cfg_succ in let node = { node with cfg_scope ; cfg_succ ; cfg_final ; cfg_next_main } in cfg . nodes <- IMap . add label node cfg . nodes ; List . iter ( split_function new_letrec_label ) cfg_succ ; List . iter ( fun lab -> split_function ( Some lab ) lab ) next_funcs ) in let start_label = 0 in let start_node = try IMap . find start_label cfg . nodes with Not_found -> assert false in assert ( start_node . cfg_scope . cfg_letrec_label = None ) ; set_depth 0 start_label ; split_function None start_label |
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 Not_found -> assert false in let trail ' = ISet . add label trail in let eff_succ = List . filter ( fun lab -> not ( ISet . mem lab trail ' ) ) node . cfg_succ in List . iter ( inc_degree trail ' ) eff_succ ) in let rec dec_degree trail label = let deg = try IMap . find label ! in_degree with Not_found -> assert false in in_degree := IMap . add label ( deg - 1 ) ! in_degree ; if deg = 1 then ( let node = try IMap . find label ctx . nodes with Not_found -> assert false in let trail ' = ISet . add label trail in let eff_succ = List . filter ( fun lab -> not ( ISet . mem lab trail ' ) ) node . cfg_succ in ( [ label ] :: List . map ( dec_degree trail ' ) eff_succ ) |> List . flatten ) else [ ] in let rec build_block prev_loop loops_started inner labels = match labels with | label1 :: labels ' -> let node = try IMap . find label1 ctx . nodes with Not_found -> assert false in let node_loop = match node . cfg_loops with | [ ] -> None | lab :: _ -> Some lab in let inner_instructions = List . map ( fun b -> Block b ) inner |> Array . of_list in let node_instructions = ( if node . cfg_length = 0 then [ | ] | else Array . sub ctx . code label1 node . cfg_length ) |> Array . map ( fun instr -> match instr with | I . Kpushtrap catchlabel -> ( match node . cfg_trap with | Some ( Trap_push ( trylabel , poplabel ) ) -> Trap { trylabel ; catchlabel ; poplabel } | _ -> assert false ) | _ -> Simple instr ) |> ( fun a1 -> match node . cfg_try with | Some Try_exit -> Array . append a1 [ | TryReturn ] | | _ -> a1 ) |> ( fun a1 -> match node . cfg_final with | None -> a1 | Some instr -> assert ( node . cfg_next_main = None ) ; Array . append a1 [ | Simple instr ] | ) |> ( fun a1 -> match node . cfg_next_main with | None -> a1 | Some label -> assert ( node . cfg_final = None ) ; Array . append a1 [ | NextMain label ] | ) |> ( fun a -> Array . append [ | Label label1 ] | a ) in if prev_loop = node_loop || List . mem node_loop loops_started then let instructions = Array . append inner_instructions node_instructions in let break_label = match labels ' with | [ ] -> None | lab :: _ -> Some lab in let inner ' = [ { loop_label = None ; break_label ; instructions } ] in build_block node_loop loops_started inner ' labels ' else let loop_start_label = match node_loop with | Some lab -> lab | None -> assert false in let loop_labels , other_labels = List . partition ( is_node_in_loop ctx loop_start_label ) labels in let loop_body = build_block node_loop ( node_loop :: loops_started ) [ ] loop_labels in let loop_block = { loop_label = node_loop ; break_label = None ; instructions = [ | Block loop_body ] | } in let loop_instructions = [ | Block loop_block ] | in let instructions = Array . append inner_instructions loop_instructions in let break_label = match other_labels with | [ ] -> None | lab :: _ -> Some lab in let inner ' = [ { loop_label = None ; break_label ; instructions ; } ] in build_block None loops_started inner ' other_labels | [ ] -> ( match inner with | [ b ] -> b | _ -> let instructions = List . map ( fun b -> Block b ) inner |> Array . of_list in { loop_label = None ; break_label = None ; instructions } ) in let functions = IMap . filter_map ( fun label node -> let is_entry = label = node . cfg_scope . cfg_func_label || match node . cfg_try with | Some ( Try_entry _ ) -> true | _ -> false in if is_entry then ( inc_degree ISet . empty label ; let sorted_block_labels = dec_degree ISet . empty label in let block = build_block None [ None ] [ ] sorted_block_labels in Some { scope = node . cfg_scope ; block } ) else None ) ctx . nodes in { functions } |
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 then error func_label last_label " both loop_label and break_label " ; let last_label ' = match block . loop_label , block . break_label with | Some lab , _ -> lab | _ , Some lab -> lab | _ -> last_label in let labels_in_scope ' = match block . loop_label with | None -> labels_in_scope | Some lab -> if ISet . mem lab labels_in_scope then error func_label last_label ' " loop_label not new " ; ISet . add lab labels_in_scope in let labels_in_scope ' ' = match block . break_label with | None -> labels_in_scope ' | Some lab -> if ISet . mem lab labels_in_scope ' then error func_label last_label ' " break_label not new " ; ISet . add lab labels_in_scope ' in Array . iter ( validate_instruction labels_in_scope ' ' func_label last_label ' ) block . instructions and validate_instruction labels_in_scope func_label last_label instruction = match instruction with | Block block -> validate_block labels_in_scope func_label last_label block | Simple instr -> ( match instr with | I . Kclosure ( lab , _ ) -> validate_function_label func_label last_label lab | I . Kclosurerec ( labl , _ ) -> List . iter ( validate_function_label func_label last_label ) labl | _ -> let labels = Wc_reader . get_labels_in_instr instr in List . iter ( validate_label labels_in_scope func_label last_label ) labels ) | Trap { catchlabel } -> validate_label labels_in_scope func_label last_label catchlabel | TryReturn -> ( ) | Label _ -> ( ) | NextMain _ -> ( ) and validate_label labels_in_scope func_label last_label label = if not ( ISet . mem label labels_in_scope ) then error func_label last_label ( sprintf " label not defined : % d " label ) and validate_function_label func_label last_label label = if not ( IMap . mem label scode . functions ) then error func_label last_label ( sprintf " function not defined : % d " label ) in let validate_fblock label fblock = validate_block ISet . empty label ( - 1 ) fblock . block in IMap . iter validate_fblock scode . functions |
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 in try prefix ^ " _ " ^ get_defname label with Not_found -> prefix in IMap . bindings s . functions |> List . fold_left ( fun acc ( label , func ) -> let name = get_letrec_name func in let n = try SMap . find name acc with Not_found -> 0 in SMap . add name ( n + size_of_function func ) acc ) SMap . empty |> SMap . bindings |> List . sort ( fun ( l1 , s1 ) ( l2 , s2 ) -> s1 - s2 ) |
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 ( fun s -> cclib := ! cclib @ [ s ] ) , " < option > pass this option to the linker " ; " - cstack " , Arg . Set_int cstack , ( sprintf " < numbytes > set the size of the C shadow stack ( default % d ) " ! cstack ) ; " - q " , Arg . Set quiet , " quiet : reduce verbosity " ; " - enable - multivalue " , Arg . Set Wc_emit . enable_multireturn , " enable Wasm feature : multi - value returns ( EXPERIMENTAL ) " ; " - enable - deadbeef - check " , Arg . Set Wc_emit . enable_deadbeef_check , " enable stack initialization check ( debug ) " ; ] ( fun arg -> if ! inp <> None then raise ( Arg . Bad " only one input file is permitted " ) ; inp := Some arg ) " usage : wasicaml - o output [ . wasm ] bytecode " ; let inp = match ! inp with | None -> failwith " no input file " | Some inp -> inp in if not ! quiet then eprintf " * parsing . . . \ n " ; %! let exec = try read_executable inp with Bytesections . Bad_magic_number -> failwith ( " not a bytecode executable : " ^ inp ) in let code , labels , maplab = decode exec in if not ! quiet then ( eprintf " number instructions : % d \ n " ( Array . length code ) ; eprintf " number labels : % d \ n " %! ( ISet . cardinal labels ) ; ) ; let get_defname = defname_of_label exec maplab in if not ! quiet then eprintf " * creating CFG . . . \ n " ; %! let cfg = create_cfg code labels in split_main_function cfg ; if not ! quiet then eprintf " number nodes : % d \ n " %! ( IMap . cardinal cfg . nodes ) ; if not ! quiet then eprintf " * linearize . . . \ n " ; %! let s = recover_structure cfg in if not ! quiet then ( eprintf " number functions : % d \ n " %! ( IMap . cardinal s . functions ) ; eprintf " * validating . . . \ n " ; %! ) ; validate s ; if not ! quiet then eprintf " * tracing globals . . . \ n " ; %! let globals_table = Wc_traceglobals . trace_globals s in let tab = size_table s get_defname |> List . rev in let have_large_functions = List . exists ( fun ( _ , size ) -> size >= size_limit_for_warning ) tab in if have_large_functions then ( eprintf " * WARNING : there are very large functions which can lead to slow JIT compiling \ n " ; try List . iteri ( fun i ( name , size ) -> if i >= 100 || size < size_limit_for_warning then raise Exit ; eprintf " % s : % d instructions \ n " name size ) tab with Exit -> ( ) ) ; if not ! quiet then eprintf " * translating to WASM . . . \ n " ; %! let sexpl = generate s exec get_defname globals_table in if not ! quiet then eprintf " * print as . wat . . . \ n " ; %! let full = K " module " :: sexpl in let f = open_out ( inp ^ " . wat " ) in print_indented f 0 80 ( L full ) ; close_out f ; if not ! quiet then eprintf " * print as . s ( llvm integrated assembler syntax ) . . . \ n " ; %! let f = open_out ( inp ^ " . s " ) in Wc_sexp2s . write_file f ( inp ^ " . s " ) sexpl ; close_out f ; if not ! quiet then eprintf " * assemble . . . \ n " ; %! let cmd = sprintf " % s / bin / wasi_cc - o % s - c % s " prefix ( Filename . quote ( inp ^ " . o " ) ) ( Filename . quote ( inp ^ " . s " ) ) in if not ! quiet then eprintf " + % s \ n " %! cmd ; let code = Sys . command cmd in if code <> 0 then failwith " command failed " ; if not ! quiet then eprintf " * link . . . \ n " ; %! let cmd = sprintf " % s / bin / wasi_cc - Wl , - z , stack - size =% d - o % s % s / lib / initruntime . o % s - L % s / lib / ocaml % s - lcamlrun " prefix ! cstack ! out prefix ( inp ^ " . o " ) prefix ( List . map Filename . quote ! cclib |> String . concat " " ) in if not ! quiet then eprintf " + % s \ n " %! cmd ; let code = Sys . command cmd in if code <> 0 then failwith " command failed " ; ( ) |
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 ) Hashtbl . t ; mutable need_reinit_frame : bool ; mutable need_reinit_frame_k : ISet . t ; mutable need_mlookup : bool ; mutable globals_table : ( int , Wc_traceglobals . initvalue ) Hashtbl . t ; mutable glbfun_table : ( int , int * Wc_traceglobals . initvalue array ) Hashtbl . t ; } |
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_i32 : bool ; mutable need_tmp1_f64 : bool ; mutable need_xalloc : bool ; } |
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_panic = false ; need_tmp1_i32 = false ; need_tmp2_i32 = false ; need_tmp1_f64 = false ; need_xalloc = false ; } |
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 " i32 . lt_u " ] ; L [ K " if " ; L [ K " then " ; L [ K " call " ; ID " caml_raise_stack_overflow " ] ; ] ] ] |
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 " i32 . sub " ] ; L [ K " i32 . load " ; K " align = 2 " ] ] |
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 . get " ; ID addr ] ; L [ K " i32 . const " ; N ( I32 ( Int32 . of_int ( - offset ) ) ) ] ; L [ K " i32 . sub " ] ] @ code_value @ [ L [ K " i32 . store " ; K " align = 2 " ] ] |
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 " ; K " align = 2 " ] ] |
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 " loop " ; BR ; L [ K " local . get " ; ID " bp " ] ; L [ K " local . get " ; ID " fp " ] ; L [ K " i32 . ge_u " ] ; L [ K " br_if " ; ID " loop_exit " ] ; L [ K " local . get " ; ID " bp " ] ; L [ K " i32 . const " ; N ( I32 0xdeadbeefl ) ] ; L [ K " i32 . store " ] ; L [ K " local . get " ; ID " bp " ] ; L [ K " i32 . const " ; N ( I32 4l ) ] ; L [ K " i32 . add " ] ; L [ K " local . set " ; ID " bp " ] ; L [ K " br " ; ID " loop " ] ] ] ] ] |> List . flatten ) ] |
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 " ] ; L [ K " if " ; L [ K " then " ; L [ K " unreachable " ] ] ] ] ; [ L [ K " block " ; ID " loop_exit " ; BR ; L [ K " loop " ; ID " loop " ; BR ; L [ K " local . get " ; ID " ptr " ] ; L [ K " local . get " ; ID " fp " ] ; L [ K " i32 . ge_u " ] ; L [ K " br_if " ; ID " loop_exit " ] ; L [ K " local . get " ; ID " ptr " ] ; L [ K " i32 . load " ] ; L [ K " i32 . const " ; N ( I32 0xdeadbeefl ) ] ; L [ K " i32 . eq " ] ; L [ K " if " ; L [ K " then " ; L [ K " unreachable " ] ] ] ; L [ K " local . get " ; ID " ptr " ] ; L [ K " i32 . const " ; N ( I32 4l ) ] ; L [ K " i32 . add " ] ; L [ K " local . set " ; ID " ptr " ] ; L [ K " br " ; ID " loop " ] ] ] ] ] |> List . flatten ) ] |
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 = ( [ L [ K " local . get " ; ID " fp " ; ] ] @ ( if descr . stack_depth + sp_decr > 0 then [ L [ K " i32 . const " ; N ( I32 ( Int32 . of_int ( 4 * ( descr . stack_depth + sp_decr ) ) ) ) ; ] ; L [ K " i32 . sub " ] ; ] else [ ] ) ) |> pop_to_domain_field domain_field_extern_sp in let sexpl_check = ( if ! enable_deadbeef_check then push_domain_field domain_field_extern_sp @ [ L [ K " local . get " ; ID " fp " ] ; L [ K " call " ; ID " deadbeef_check " ] ] else [ ] ) in sexpl_stack @ sexpl_accu @ sexpl_extern_sp @ sexpl_check |
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 " ; ID " ptr " ; K " i32 " ] ; ] ; push_domain_field domain_field_young_ptr ; push_local " bhsize " ; [ L [ K " i32 . sub " ] ; L [ K " local . tee " ; ID " ptr " ] ; ] ; ( push_local " ptr " |> pop_to_domain_field domain_field_young_ptr ) ; push_domain_field domain_field_young_limit ; [ L [ K " i32 . lt_u " ] ] ; [ L [ K " if " ; L [ K " then " ; L [ K " i32 . const " ; N ( I32 0l ) ] ; L [ K " return " ] ] ] ] ; ( push_local " header " |> pop_to_field " ptr " 0 ) ; push_local " ptr " ; push_const 4l ; [ L [ K " i32 . add " ] ; L [ K " return " ] ] ] |> List . flatten ) ] |
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 " ; ID " stackdepth " ; K " i32 " ] ; L [ K " param " ; ID " accu " ; K " i32 " ] ; BR ; L [ K " result " ; C " ptr " ; K " i32 " ] ; ] ; if ! enable_multireturn then [ L [ K " result " ; C " out_accu " ; K " i32 " ] ; ] else [ ] ; [ L [ K " local " ; ID " ptr " ; K " i32 " ] ; L [ K " local " ; ID " sp " ; K " i32 " ] ] ; ( [ L [ K " local . get " ; ID " fp " ] ; L [ K " local . get " ; ID " stackdepth " ] ; L [ K " i32 . sub " ] ; L [ K " i32 . const " ; N ( I32 4l ) ] ; L [ K " i32 . sub " ] ; L [ K " local . tee " ; ID " sp " ] ; ] |> pop_to_domain_field domain_field_extern_sp ) @ ( push_local " accu " |> pop_to_field " sp " 0 ) ; ( if ! enable_deadbeef_check then push_domain_field domain_field_extern_sp @ [ L [ K " local . get " ; ID " fp " ] ; L [ K " call " ; ID " deadbeef_check " ] ] else [ ] ) ; push_local " wosize " ; push_const ( Int32 . of_int caml_from_c ) ; push_const 1l ; push_const 0l ; [ L [ K " call " ; ID " caml_alloc_small_dispatch " ] ] ; push_domain_field domain_field_young_ptr ; pop_to_local " ptr " ; ( push_local " header " |> pop_to_field " ptr " 0 ) ; push_local " ptr " ; push_const 4l ; [ L [ K " i32 . add " ] ] ; push_field " sp " 0 ; if ! enable_multireturn then [ ] else [ L [ K " global . set " ; ID " retval2 " ] ; ] ; [ L [ K " return " ] ] ] |> List . flatten ) ] |
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 ) ) ; [ L [ K " call " ; ID " alloc_fast " ] ; L [ K " local . tee " ; ID ptr ] ; ] ; [ L [ K " i32 . eqz " ] ] ; [ L [ K " if " ; L ( [ [ K " then " ; ] ; stack_init fpad descr ; push_const ( Int32 . of_int size ) ; push_const ( Int32 . of_int ( make_header size tag ) ) ; push_local " fp " ; push_const ( Int32 . of_int ( 4 * descr . stack_depth ) ) ; push_local " accu " ; call_alloc_slow ( ) ; pop_to_local " accu " ; pop_to_local ptr ; ] |> List . flatten ) ] ; ] ; ] |> List . flatten else [ L [ K " i32 . const " ; N ( I32 ( Int32 . of_int size ) ) ] ; L [ K " i32 . const " ; N ( I32 ( Int32 . of_int tag ) ) ] ; L [ K " call " ; ID " caml_alloc_shr " ] ; L [ K " local . set " ; ID ptr ] ; ] in ( code , ptr , young ) |
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 " ; 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 " ] ] ) |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.