diff --git a/compiler/bin-js_of_ocaml/compile.ml b/compiler/bin-js_of_ocaml/compile.ml index d3e924e807..ba99440925 100644 --- a/compiler/bin-js_of_ocaml/compile.ml +++ b/compiler/bin-js_of_ocaml/compile.ml @@ -285,7 +285,7 @@ let run | `None -> let prims = Linker.list_all () |> StringSet.elements in assert (List.length prims > 0); - let code, uinfo = Parse_bytecode.predefined_exceptions ~target:`JavaScript in + let code, uinfo = Parse_bytecode.predefined_exceptions in let uinfo = { uinfo with primitives = uinfo.primitives @ prims } in let code : Parse_bytecode.one = { code @@ -331,7 +331,6 @@ let run let linkall = linkall || toplevel || dynlink in let code = Parse_bytecode.from_exe - ~target:`JavaScript ~includes:include_dirs ~include_cmis ~link_info:(toplevel || dynlink) @@ -364,7 +363,6 @@ let run let t1 = Timer.make () in let code = Parse_bytecode.from_cmo - ~target:`JavaScript ~includes:include_dirs ~include_cmis ~debug:need_debug @@ -421,7 +419,6 @@ let run let t1 = Timer.make () in let code = Parse_bytecode.from_cmo - ~target:`JavaScript ~includes:include_dirs ~include_cmis ~debug:need_debug @@ -453,7 +450,6 @@ let run let t1 = Timer.make () in let code = Parse_bytecode.from_cmo - ~target:`JavaScript ~includes:include_dirs ~include_cmis ~debug:need_debug diff --git a/compiler/lib/config.ml b/compiler/lib/config.ml index 7865b6fd33..caf8f55be6 100644 --- a/compiler/lib/config.ml +++ b/compiler/lib/config.ml @@ -183,8 +183,11 @@ end (****) -let target_ : [ `JavaScript | `Wasm ] ref = ref `JavaScript +let target_ : [ `JavaScript | `Wasm ] option ref = ref (Some `JavaScript) -let target () = !target_ +let target () = + match !target_ with + | Some t -> t + | None -> failwith "target was not set" -let set_target t = target_ := t +let set_target t = target_ := Some t diff --git a/compiler/lib/driver.ml b/compiler/lib/driver.ml index b50df74d65..8836bf82a4 100644 --- a/compiler/lib/driver.ml +++ b/compiler/lib/driver.ml @@ -670,7 +670,19 @@ let link_and_pack ?(standalone = true) ?(wrap_with_fun = `Iife) ?(link = `No) p |> coloring |> check_js -let optimize ~profile ~deadcode_sentinal p = +type optimized_result = + { program : Code.program + ; variable_uses : Deadcode.variable_uses + ; trampolined_calls : Effects.trampolined_calls + ; in_cps : Effects.in_cps + ; deadcode_sentinal : Code.Var.t + } + +let optimize ~profile p = + let deadcode_sentinal = + (* If deadcode is disabled, this field is just fresh variable *) + Code.Var.fresh_n "undef" + in let opt = specialize_js_once +> (match profile with @@ -687,16 +699,14 @@ let optimize ~profile ~deadcode_sentinal p = in if times () then Format.eprintf "Start Optimizing...@."; let t = Timer.make () in - let r = opt p in + let (program, variable_uses), trampolined_calls, in_cps = opt p in let () = if times () then Format.eprintf " optimizations : %a@." Timer.print t in - r + { program; variable_uses; trampolined_calls; in_cps; deadcode_sentinal } let full ~standalone ~wrap_with_fun ~profile ~link ~source_map ~formatter d p = - let deadcode_sentinal = - (* If deadcode is disabled, this field is just fresh variable *) - Code.Var.fresh_n "undef" + let { program; variable_uses; trampolined_calls; in_cps; deadcode_sentinal } = + optimize ~profile p in - let r = optimize ~profile ~deadcode_sentinal p in let exported_runtime = not standalone in let emit formatter = generate @@ -708,7 +718,7 @@ let full ~standalone ~wrap_with_fun ~profile ~link ~source_map ~formatter d p = +> link_and_pack ~standalone ~wrap_with_fun ~link +> output formatter ~source_map () in - let source_map = emit formatter r in + let source_map = emit formatter ((program, variable_uses), trampolined_calls, in_cps) in source_map let full_no_source_map ~formatter ~standalone ~wrap_with_fun ~profile ~link d p = diff --git a/compiler/lib/driver.mli b/compiler/lib/driver.mli index 7f3e16bb4f..91f846b989 100644 --- a/compiler/lib/driver.mli +++ b/compiler/lib/driver.mli @@ -20,13 +20,15 @@ type profile -val optimize : - profile:profile - -> deadcode_sentinal:Code.Var.t - -> Code.program - -> (Code.program * Deadcode.variable_uses) - * Effects.trampolined_calls - * Effects.trampolined_calls +type optimized_result = + { program : Code.program + ; variable_uses : Deadcode.variable_uses + ; trampolined_calls : Effects.trampolined_calls + ; in_cps : Effects.in_cps + ; deadcode_sentinal : Code.Var.t + } + +val optimize : profile:profile -> Code.program -> optimized_result val f : ?standalone:bool diff --git a/compiler/lib/eval.ml b/compiler/lib/eval.ml index 2242ef1ce0..ab4183f302 100644 --- a/compiler/lib/eval.ml +++ b/compiler/lib/eval.ml @@ -47,6 +47,8 @@ module Int32 = struct match l with | [ Int i; Int j ] -> Some (Int (f i (to_int j))) | _ -> None + + let numbits = 32 end module Int31 = struct @@ -71,6 +73,8 @@ module Int31 = struct | [ Int i; Int j ] -> Some (Int (to_int32 (f (of_int32_warning_on_overflow i) (Int32.to_int j)))) | _ -> None + + let numbits = 31 end let float_binop_aux (l : constant list) (f : float -> float -> 'a) : 'a option = @@ -114,9 +118,15 @@ module type Int = sig val int_binop : constant list -> (t -> t -> t) -> constant option val shift_op : constant list -> (t -> int -> t) -> constant option + + val of_int32_warning_on_overflow : int32 -> t + + val to_int32 : t -> int32 + + val numbits : int end -let eval_prim x = +let eval_prim ~target x = match x with | Not, [ Int i ] -> bool Int32.(i = 0l) | Lt, [ Int i; Int j ] -> bool Int32.(i < j) @@ -127,8 +137,15 @@ let eval_prim x = | Extern name, l -> ( let name = Primitive.resolve name in let (module Int : Int) = - match Config.target () with - | `JavaScript -> (module Int32) + match target with + | `JavaScript -> + (module struct + include Int32 + + let of_int32_warning_on_overflow = Fun.id + + let to_int32 = Fun.id + end) | `Wasm -> (module Int31) in match name, l with @@ -158,7 +175,9 @@ let eval_prim x = | "caml_mul_float", _ -> float_binop l ( *. ) | "caml_div_float", _ -> float_binop l ( /. ) | "caml_fmod_float", _ -> float_binop l mod_float - | "caml_int_of_float", [ Float f ] -> Some (Int (Int32.of_float f)) + | "caml_int_of_float", [ Float f ] -> + Some + (Int (Int32.of_float f |> Int.of_int32_warning_on_overflow |> Int.to_int32)) | "to_int", [ Float f ] -> Some (Int (Int32.of_float f)) | "to_int", [ Int i ] -> Some (Int i) (* Math *) @@ -190,12 +209,7 @@ let eval_prim x = | Some env -> Some (String env) | None -> None) | "caml_sys_const_word_size", [ _ ] -> Some (Int 32l) - | "caml_sys_const_int_size", [ _ ] -> - Some - (Int - (match Config.target () with - | `JavaScript -> 32l - | `Wasm -> 31l)) + | "caml_sys_const_int_size", [ _ ] -> Some (Int (Int32.of_int Int.numbits)) | "caml_sys_const_big_endian", [ _ ] -> Some (Int 0l) | "caml_sys_const_naked_pointers_checked", [ _ ] -> Some (Int 0l) | _ -> None) @@ -222,7 +236,7 @@ type is_int = | N | Unknown -let is_int info x = +let is_int ~target info x = match x with | Pv x -> get_approx @@ -231,7 +245,7 @@ let is_int info x = match Flow.Info.def info x with | Some (Constant (Int _)) -> Y | Some (Constant (NativeInt _ | Int32 _)) -> - assert (Poly.equal (Config.target ()) `Wasm); + assert (Poly.equal target `Wasm); N | Some (Block (_, _, _, _) | Constant _) -> N | None | Some _ -> Unknown) @@ -244,7 +258,7 @@ let is_int info x = x | Pc (Int _) -> Y | Pc (NativeInt _ | Int32 _) -> - assert (Poly.equal (Config.target ()) `Wasm); + assert (Poly.equal target `Wasm); N | Pc _ -> N @@ -316,7 +330,7 @@ let constant_js_equal a b = | Tuple _, _ | _, Tuple _ -> None -let eval_instr info ((x, loc) as i) = +let eval_instr ~target info ((x, loc) as i) = match x with | Let (x, Prim (Extern (("caml_equal" | "caml_notequal") as prim), [ y; z ])) -> ( match the_const_of info y, the_const_of info z with @@ -373,7 +387,7 @@ let eval_instr info ((x, loc) as i) = below fail. *) [ i ] | Let (x, Prim (IsInt, [ y ])) -> ( - match is_int info y with + match is_int ~target info y with | Unknown -> [ i ] | (Y | N) as b -> let c = Constant (bool' Poly.(b = Y)) in @@ -389,7 +403,7 @@ let eval_instr info ((x, loc) as i) = | Let (x, Prim (Extern "caml_sys_const_backend_type", [ _ ])) -> let jsoo = Code.Var.fresh () in let backend_name = - match Config.target () with + match target with | `JavaScript -> "js_of_ocaml" | `Wasm -> "wasm_of_ocaml" in @@ -406,6 +420,7 @@ let eval_instr info ((x, loc) as i) = | _ -> false) then eval_prim + ~target ( prim , List.map prim_args' ~f:(function | Some c -> c @@ -423,9 +438,13 @@ let eval_instr info ((x, loc) as i) = , Prim ( prim , List.map2 prim_args prim_args' ~f:(fun arg (c : constant option) -> - match c, Config.target () with - | ( Some ((Int _ | Int32 _ | NativeInt _ | NativeString _) as c) - , _ ) -> Pc c + match c, target with + | Some ((Int _ | NativeString _) as c), _ -> Pc c + | Some ((Int32 _ | NativeInt _) as c), `Wasm -> Pc c + | Some (Int32 _ | NativeInt _), `JavaScript -> + invalid_arg + "Constant of type Int32 or NativeInt unexpected in the \ + JavaScript backend" | Some (Float _ as c), `JavaScript -> Pc c | Some (String _ as c), `JavaScript when Config.Flag.use_js_string () -> Pc c @@ -554,15 +573,15 @@ let drop_exception_handler blocks = blocks blocks -let eval info blocks = +let eval ~target info blocks = Addr.Map.map (fun block -> - let body = List.concat_map block.body ~f:(eval_instr info) in + let body = List.concat_map block.body ~f:(eval_instr ~target info) in let branch = eval_branch info block.branch in { block with Code.body; Code.branch }) blocks let f info p = - let blocks = eval info p.blocks in + let blocks = eval ~target:(Config.target ()) info p.blocks in let blocks = drop_exception_handler blocks in { p with blocks } diff --git a/compiler/lib/link_js.ml b/compiler/lib/link_js.ml index 4fa3778fb9..e4d3d2989e 100644 --- a/compiler/lib/link_js.ml +++ b/compiler/lib/link_js.ml @@ -412,13 +412,7 @@ let link ~output ~linkall ~mklib ~toplevel ~files ~resolve_sourcemap_url ~source List.fold_left units ~init:StringSet.empty ~f:(fun acc (u : Unit_info.t) -> StringSet.union acc (StringSet.of_list u.primitives)) in - let code = - Parse_bytecode.link_info - ~target:`JavaScript - ~symbols:!sym - ~primitives - ~crcs:[] - in + let code = Parse_bytecode.link_info ~symbols:!sym ~primitives ~crcs:[] in let b = Buffer.create 100 in let fmt = Pretty_print.to_buffer b in Driver.configure fmt; diff --git a/compiler/lib/ocaml_compiler.ml b/compiler/lib/ocaml_compiler.ml index d8020a8411..2518753d5f 100644 --- a/compiler/lib/ocaml_compiler.ml +++ b/compiler/lib/ocaml_compiler.ml @@ -18,13 +18,13 @@ open! Stdlib -let rec constant_of_const ~target c : Code.constant = +let rec constant_of_const c : Code.constant = let open Lambda in let open Asttypes in match c with | Const_base (Const_int i) -> Int - (match target with + (match Config.target () with | `JavaScript -> Int32.of_int_warning_on_overflow i | `Wasm -> Int31.(of_int_warning_on_overflow i |> to_int32)) | Const_base (Const_char c) -> Int (Int32.of_int (Char.code c)) @@ -40,11 +40,11 @@ let rec constant_of_const ~target c : Code.constant = Float_array (Array.of_list l) | ((Const_pointer i) [@if ocaml_version < (4, 12, 0)]) -> Int - (match target with + (match Config.target () with | `JavaScript -> Int32.of_int_warning_on_overflow i | `Wasm -> Int31.(of_int_warning_on_overflow i |> to_int32)) | Const_block (tag, l) -> - let l = Array.of_list (List.map l ~f:(fun c -> constant_of_const ~target c)) in + let l = Array.of_list (List.map l ~f:(fun c -> constant_of_const c)) in Tuple (tag, l, Unknown) let rec find_loc_in_summary ident' = function diff --git a/compiler/lib/ocaml_compiler.mli b/compiler/lib/ocaml_compiler.mli index 409381a562..227f1b9f31 100644 --- a/compiler/lib/ocaml_compiler.mli +++ b/compiler/lib/ocaml_compiler.mli @@ -16,8 +16,7 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) -val constant_of_const : - target:[ `JavaScript | `Wasm ] -> Lambda.structured_constant -> Code.constant +val constant_of_const : Lambda.structured_constant -> Code.constant val find_loc_in_summary : Ident.t -> Env.summary -> Location.t option diff --git a/compiler/lib/parse_bytecode.ml b/compiler/lib/parse_bytecode.ml index 4611259751..0db004cdf9 100644 --- a/compiler/lib/parse_bytecode.ml +++ b/compiler/lib/parse_bytecode.ml @@ -443,7 +443,7 @@ end (* Parse constants *) module Constants : sig - val parse : target:[ `JavaScript | `Wasm ] -> Obj.t -> Code.constant + val parse : Obj.t -> Code.constant val inlined : Code.constant -> bool end = struct @@ -477,7 +477,7 @@ end = struct let ident_native = ident_of_custom (Obj.repr 0n) - let rec parse ~target x = + let rec parse x = if Obj.is_block x then let tag = Obj.tag x in @@ -503,15 +503,12 @@ end = struct | None -> assert false else if tag < Obj.no_scan_tag then - Tuple - ( tag - , Array.init (Obj.size x) ~f:(fun i -> parse ~target (Obj.field x i)) - , Unknown ) + Tuple (tag, Array.init (Obj.size x) ~f:(fun i -> parse (Obj.field x i)), Unknown) else assert false else let i : int = Obj.magic x in Int - (match target with + (match Config.target () with | `JavaScript -> Int32.of_int_warning_on_overflow i | `Wasm -> Int31.(of_int_warning_on_overflow i |> to_int32)) @@ -769,10 +766,10 @@ let access_global g i = g.vars.(i) <- Some x; x -let register_global ~target ?(force = false) g i loc rem = +let register_global ?(force = false) g i loc rem = if g.is_exported.(i) && - match target with + match Config.target () with | `Wasm -> true | `JavaScript -> false then ( @@ -805,7 +802,7 @@ let register_global ~target ?(force = false) g i loc rem = :: rem else rem -let get_global ~target state instrs i loc = +let get_global state instrs i loc = State.size_globals state (i + 1); let g = State.globals state in match g.vars.(i) with @@ -820,7 +817,7 @@ let get_global ~target state instrs i loc = x, state, (Let (x, Constant cst), loc) :: instrs else if i < Array.length g.constants || - match target with + match Config.target () with | `Wasm -> false | `JavaScript -> true then ( @@ -853,7 +850,6 @@ type compile_info = ; code : string ; limit : int ; debug : Debug.t - ; target : [ `JavaScript | `Wasm ] } let string_of_addr debug_data addr = @@ -881,7 +877,7 @@ let ( ||| ) x y = | No -> y | _ -> x -let rec compile_block blocks debug_data ~target code pc state = +let rec compile_block blocks debug_data code pc state = match Addr.Map.find_opt pc !tagged_blocks with | Some old_state -> ( (* Check that the shape of the stack is compatible with the one used to compile the block *) @@ -913,7 +909,7 @@ let rec compile_block blocks debug_data ~target code pc state = let state = State.start_block pc state in tagged_blocks := Addr.Map.add pc state !tagged_blocks; let instr, last, state' = - compile { blocks; code; limit; debug = debug_data; target } pc state [] + compile { blocks; code; limit; debug = debug_data } pc state [] in assert (not (Addr.Map.mem pc !compiled_blocks)); (* When jumping to a block that was already visited and the @@ -942,11 +938,10 @@ let rec compile_block blocks debug_data ~target code pc state = in compiled_blocks := Addr.Map.add pc (state, List.rev instr, last) !compiled_blocks; match fst last with - | Branch (pc', _) -> - compile_block blocks debug_data ~target code pc' (adjust_state pc') + | Branch (pc', _) -> compile_block blocks debug_data code pc' (adjust_state pc') | Cond (_, (pc1, _), (pc2, _)) -> - compile_block blocks debug_data ~target code pc1 (adjust_state pc1); - compile_block blocks debug_data ~target code pc2 (adjust_state pc2) + compile_block blocks debug_data code pc1 (adjust_state pc1); + compile_block blocks debug_data code pc2 (adjust_state pc2) | Poptrap (_, _) -> () | Switch (_, _) -> () | Raise _ | Return _ | Stop -> () @@ -1288,7 +1283,7 @@ and compile infos pc state instrs = let params, state' = State.make_stack nparams state' loc in if debug_parser () then Format.printf ") {@."; let state' = State.clear_accu state' in - compile_block infos.blocks infos.debug ~target:infos.target code addr state'; + compile_block infos.blocks infos.debug code addr state'; if debug_parser () then Format.printf "}@."; let args = State.stack_vars state' in let state'', _, _ = Addr.Map.find addr !compiled_blocks in @@ -1345,7 +1340,7 @@ and compile infos pc state instrs = let params, state' = State.make_stack nparams state' loc in if debug_parser () then Format.printf ") {@."; let state' = State.clear_accu state' in - compile_block infos.blocks infos.debug ~target:infos.target code addr state'; + compile_block infos.blocks infos.debug code addr state'; if debug_parser () then Format.printf "}@."; let args = State.stack_vars state' in let state'', _, _ = Addr.Map.find addr !compiled_blocks in @@ -1375,16 +1370,16 @@ and compile infos pc state instrs = compile infos (pc + 2) (State.env_acc n state) instrs | GETGLOBAL -> let i = getu code (pc + 1) in - let _, state, instrs = get_global ~target:infos.target state instrs i loc in + let _, state, instrs = get_global state instrs i loc in compile infos (pc + 2) state instrs | PUSHGETGLOBAL -> let state = State.push state loc in let i = getu code (pc + 1) in - let _, state, instrs = get_global ~target:infos.target state instrs i loc in + let _, state, instrs = get_global state instrs i loc in compile infos (pc + 2) state instrs | GETGLOBALFIELD -> let i = getu code (pc + 1) in - let x, state, instrs = get_global ~target:infos.target state instrs i loc in + let x, state, instrs = get_global state instrs i loc in let j = getu code (pc + 2) in let y, state = State.fresh_var state loc in if debug_parser () then Format.printf "%a = %a[%d]@." Var.print y Var.print x j; @@ -1393,7 +1388,7 @@ and compile infos pc state instrs = let state = State.push state loc in let i = getu code (pc + 1) in - let x, state, instrs = get_global ~target:infos.target state instrs i loc in + let x, state, instrs = get_global state instrs i loc in let j = getu code (pc + 2) in let y, state = State.fresh_var state loc in if debug_parser () then Format.printf "%a = %a[%d]@." Var.print y Var.print x j; @@ -1418,7 +1413,7 @@ and compile infos pc state instrs = in let x, state = State.fresh_var state loc in if debug_parser () then Format.printf "%a = 0@." Var.print x; - let instrs = register_global ~target:infos.target g i loc instrs in + let instrs = register_global g i loc instrs in compile infos (pc + 2) state ((Let (x, const 0l), loc) :: instrs) | ATOM0 -> let x, state = State.fresh_var state loc in @@ -1766,9 +1761,9 @@ and compile infos pc state instrs = let it = Array.init isize ~f:(fun i -> base + gets code (base + i)) in let bt = Array.init bsize ~f:(fun i -> base + gets code (base + isize + i)) in Array.iter it ~f:(fun pc' -> - compile_block infos.blocks infos.debug ~target:infos.target code pc' state); + compile_block infos.blocks infos.debug code pc' state); Array.iter bt ~f:(fun pc' -> - compile_block infos.blocks infos.debug ~target:infos.target code pc' state); + compile_block infos.blocks infos.debug code pc' state); match isize, bsize with | _, 0 -> instrs, (Switch (x, Array.map it ~f:(fun pc -> pc, [])), loc), state | 0, _ -> @@ -1839,17 +1834,10 @@ and compile infos pc state instrs = , (handler_addr, State.stack_vars handler_state) ) , loc ) ) !compiled_blocks; + compile_block infos.blocks infos.debug code handler_addr handler_state; compile_block infos.blocks infos.debug - ~target:infos.target - code - handler_addr - handler_state; - compile_block - infos.blocks - infos.debug - ~target:infos.target code body_addr { (State.push_handler handler_ctx_state) with @@ -1867,7 +1855,6 @@ and compile infos pc state instrs = compile_block infos.blocks infos.debug - ~target:infos.target code addr (State.pop 4 (State.pop_handler state)); @@ -2551,7 +2538,7 @@ type one = ; debug : Debug.t } -let parse_bytecode code globals debug_data ~target = +let parse_bytecode code globals debug_data = let state = State.initial globals in Code.Var.reset (); let blocks = Blocks.analyse debug_data code in @@ -2566,7 +2553,7 @@ let parse_bytecode code globals debug_data ~target = if not (Blocks.is_empty blocks') then ( let start = 0 in - compile_block blocks' debug_data ~target code start state; + compile_block blocks' debug_data code start state; let blocks = Addr.Map.mapi (fun _ (state, instr, last) -> @@ -2686,7 +2673,6 @@ type bytesections = [@@ocaml.warning "-unused-field"] let from_exe - ~target ?(includes = []) ~linkall ~link_info @@ -2700,7 +2686,7 @@ let from_exe let primitive_table = Array.of_list primitives in let code = Toc.read_code toc ic in let init_data = Toc.read_data toc ic in - let init_data = Array.map ~f:(Constants.parse ~target) init_data in + let init_data = Array.map ~f:Constants.parse init_data in let orig_symbols = Toc.read_symb toc ic in let orig_crcs = Toc.read_crcs toc ic in let keeps = @@ -2755,12 +2741,12 @@ let from_exe Ocaml_compiler.Symtable.GlobalMap.iter symbols ~f:(fun id n -> globals.named_value.(n) <- Some (Ocaml_compiler.Symtable.Global.name id); globals.is_exported.(n) <- true); - let p = parse_bytecode code globals debug_data ~target in + let p = parse_bytecode code globals debug_data in (* register predefined exception *) let body = List.fold_left predefined_exceptions ~init:[] ~f:(fun body (i, name) -> globals.named_value.(i) <- Some name; - let body = register_global ~target ~force:true globals i noloc body in + let body = register_global ~force:true globals i noloc body in globals.is_exported.(i) <- false; body) in @@ -2768,7 +2754,7 @@ let from_exe Array.fold_right_i globals.constants ~init:body ~f:(fun i _ l -> match globals.vars.(i) with | Some x when globals.is_const.(i) -> - let l = register_global ~target globals i noloc l in + let l = register_global globals i noloc l in (Let (x, Constant globals.constants.(i)), noloc) :: l | _ -> l) in @@ -2787,8 +2773,8 @@ let from_exe let gdata = Var.fresh () in let need_gdata = ref false in let infos = - [ "sections", Constants.parse ~target (Obj.repr sections) - ; "symbols", Constants.parse ~target (Obj.repr symbols_array) + [ "sections", Constants.parse (Obj.repr sections) + ; "symbols", Constants.parse (Obj.repr symbols_array) ; "prim_count", Int (Int32.of_int (Array.length globals.primitives)) ] in @@ -2879,7 +2865,7 @@ let from_bytes ~prims ~debug (code : bytecode) = t in let globals = make_globals 0 [||] prims in - let p = parse_bytecode code globals debug_data ~target:`JavaScript in + let p = parse_bytecode code globals debug_data in let gdata = Var.fresh_n "global_data" in let need_gdata = ref false in let find_name i = @@ -2944,13 +2930,13 @@ module Reloc = struct let constant_of_const x = Constants.parse x [@@if ocaml_version >= (5, 1, 0)] (* We currently rely on constants to be relocated before globals. *) - let step1 ~target t compunit code = + let step1 t compunit code = if t.step2_started then assert false; let open Cmo_format in List.iter compunit.cu_primitives ~f:(fun name -> Hashtbl.add t.primitives name (Hashtbl.length t.primitives)); let slot_for_literal sc = - t.constants <- constant_of_const ~target sc :: t.constants; + t.constants <- constant_of_const sc :: t.constants; let pos = t.pos in t.pos <- succ t.pos; pos @@ -3018,16 +3004,16 @@ module Reloc = struct globals end -let from_compilation_units ~target ~includes:_ ~include_cmis ~debug_data l = +let from_compilation_units ~includes:_ ~include_cmis ~debug_data l = let reloc = Reloc.create () in - List.iter l ~f:(fun (compunit, code) -> Reloc.step1 ~target reloc compunit code); + List.iter l ~f:(fun (compunit, code) -> Reloc.step1 reloc compunit code); List.iter l ~f:(fun (compunit, code) -> Reloc.step2 reloc compunit code); let globals = Reloc.make_globals reloc in let code = let l = List.map l ~f:(fun (_, c) -> Bytes.to_string c) in String.concat ~sep:"" l in - let prog = parse_bytecode code globals debug_data ~target in + let prog = parse_bytecode code globals debug_data in let gdata = Var.fresh_n "global_data" in let need_gdata = ref false in let body = @@ -3036,7 +3022,7 @@ let from_compilation_units ~target ~includes:_ ~include_cmis ~debug_data l = | Some x when globals.is_const.(i) -> ( match globals.named_value.(i) with | None -> - let l = register_global ~target globals i noloc l in + let l = register_global globals i noloc l in let cst = globals.constants.(i) in (match cst, Code.Var.get_name x with | String str, None -> Code.Var.name x (Printf.sprintf "cst_%s" str) @@ -3069,8 +3055,7 @@ let from_compilation_units ~target ~includes:_ ~include_cmis ~debug_data l = in { code = prepend prog body; cmis; debug = debug_data } -let from_cmo ~target ?(includes = []) ?(include_cmis = false) ?(debug = false) compunit ic - = +let from_cmo ?(includes = []) ?(include_cmis = false) ?(debug = false) compunit ic = let debug_data = Debug.create ~include_cmis debug in seek_in ic compunit.Cmo_format.cu_pos; let code = Bytes.create compunit.Cmo_format.cu_codesize in @@ -3081,13 +3066,11 @@ let from_cmo ~target ?(includes = []) ?(include_cmis = false) ?(debug = false) c seek_in ic compunit.Cmo_format.cu_debug; Debug.read_event_list debug_data ~crcs:[] ~includes ~orig:0 ic); if times () then Format.eprintf " read debug events: %a@." Timer.print t; - let p = - from_compilation_units ~target ~includes ~include_cmis ~debug_data [ compunit, code ] - in + let p = from_compilation_units ~includes ~include_cmis ~debug_data [ compunit, code ] in Code.invariant p.code; p -let from_cma ~target ?(includes = []) ?(include_cmis = false) ?(debug = false) lib ic = +let from_cma ?(includes = []) ?(include_cmis = false) ?(debug = false) lib ic = let debug_data = Debug.create ~include_cmis debug in let orig = ref 0 in let t = ref 0. in @@ -3106,7 +3089,7 @@ let from_cma ~target ?(includes = []) ?(include_cmis = false) ?(debug = false) l compunit, code) in if times () then Format.eprintf " read debug events: %.2f@." !t; - let p = from_compilation_units ~target ~includes ~include_cmis ~debug_data units in + let p = from_compilation_units ~includes ~include_cmis ~debug_data units in Code.invariant p.code; p @@ -3151,7 +3134,7 @@ let from_channel ic = `Exe | _ -> raise Magic_number.(Bad_magic_number (to_string magic))) -let predefined_exceptions ~target = +let predefined_exceptions = let body = let open Code in List.map predefined_exceptions ~f:(fun (index, name) -> @@ -3161,7 +3144,7 @@ let predefined_exceptions ~target = let v_name_js = Var.fresh () in let v_index = Var.fresh () in [ Let (v_name, Constant (String name)), noloc ] - @ (match target with + @ (match Config.target () with | `Wasm -> [] | `JavaScript -> [ ( Let (v_name_js, Constant (NativeString (Native_string.of_string name))) @@ -3184,14 +3167,14 @@ let predefined_exceptions ~target = , [ Pc (Int (Int32.of_int index)) ; Pv exn ; Pv - (match target with + (match Config.target () with | `JavaScript -> v_name_js | `Wasm -> v_name) ] ) ) , noloc ) ] @ - match target with + match Config.target () with | `JavaScript -> [] | `Wasm -> [ ( Let @@ -3213,7 +3196,7 @@ let predefined_exceptions ~target = in { start = 0; blocks = Addr.Map.singleton 0 block; free_pc = 1 }, unit_info -let link_info ~target ~symbols ~primitives ~crcs = +let link_info ~symbols ~primitives ~crcs = let gdata = Code.Var.fresh_n "global_data" in let symbols_array = Ocaml_compiler.Symtable.GlobalMap.fold @@ -3231,8 +3214,8 @@ let link_info ~target ~symbols ~primitives ~crcs = (* Include linking information *) let sections = { symb = symbols; crcs; prim = primitives; dlpt = [] } in let infos = - [ "sections", Constants.parse ~target (Obj.repr sections) - ; "symbols", Constants.parse ~target (Obj.repr symbols_array) + [ "sections", Constants.parse (Obj.repr sections) + ; "symbols", Constants.parse (Obj.repr symbols_array) ; "prim_count", Int (Int32.of_int (List.length primitives)) ] in diff --git a/compiler/lib/parse_bytecode.mli b/compiler/lib/parse_bytecode.mli index 33edf53f1c..dac8e266d9 100644 --- a/compiler/lib/parse_bytecode.mli +++ b/compiler/lib/parse_bytecode.mli @@ -52,8 +52,7 @@ end val read_primitives : Toc.t -> in_channel -> string list val from_exe : - target:[ `JavaScript | `Wasm ] - -> ?includes:string list + ?includes:string list -> linkall:bool -> link_info:bool -> include_cmis:bool @@ -63,8 +62,7 @@ val from_exe : -> one val from_cmo : - target:[ `JavaScript | `Wasm ] - -> ?includes:string list + ?includes:string list -> ?include_cmis:bool -> ?debug:bool -> Cmo_format.compilation_unit @@ -72,8 +70,7 @@ val from_cmo : -> one val from_cma : - target:[ `JavaScript | `Wasm ] - -> ?includes:string list + ?includes:string list -> ?include_cmis:bool -> ?debug:bool -> Cmo_format.library @@ -90,11 +87,10 @@ val from_string : -> string -> Code.program * Debug.t -val predefined_exceptions : target:[ `JavaScript | `Wasm ] -> Code.program * Unit_info.t +val predefined_exceptions : Code.program * Unit_info.t val link_info : - target:[ `JavaScript | `Wasm ] - -> symbols:Ocaml_compiler.Symtable.GlobalMap.t + symbols:Ocaml_compiler.Symtable.GlobalMap.t -> primitives:StringSet.t -> crcs:(string * Digest.t option) list -> Code.program diff --git a/compiler/lib/specialize_js.ml b/compiler/lib/specialize_js.ml index 162b877582..e8fa7b9535 100644 --- a/compiler/lib/specialize_js.ml +++ b/compiler/lib/specialize_js.ml @@ -22,8 +22,8 @@ open! Stdlib open Code open Flow -let specialize_instr info i = - match i, Config.target () with +let specialize_instr ~target info i = + match i, target with | Let (x, Prim (Extern "caml_format_int", [ y; z ])), `JavaScript -> ( match the_string_of info y with | Some "%d" -> ( @@ -156,7 +156,7 @@ let equal3 a b c = Code.Var.equal a b && Code.Var.equal b c let equal4 a b c d = Code.Var.equal a b && Code.Var.equal b c && Code.Var.equal c d -let specialize_instrs info l = +let specialize_instrs ~target info l = let rec aux info checks l acc = match l with | [] -> List.rev acc @@ -285,22 +285,22 @@ let specialize_instrs info l = in aux info ((y, idx) :: checks) r acc | _ -> - let i = specialize_instr info i in + let i = specialize_instr ~target info i in aux info checks r ((i, loc) :: acc)) in aux info [] l [] -let specialize_all_instrs info p = +let specialize_all_instrs ~target info p = let blocks = Addr.Map.map - (fun block -> { block with Code.body = specialize_instrs info block.body }) + (fun block -> { block with Code.body = specialize_instrs ~target info block.body }) p.blocks in { p with blocks } (****) -let f info p = specialize_all_instrs info p +let f info p = specialize_all_instrs ~target:(Config.target ()) info p let f_once p = let rec loop acc l =