Skip to content

Commit d4c21f5

Browse files
authored
Merge pull request #85 from OlivierNicole/converge-jsoo-merge-08
Integrate "Target-specific code" (ocsigen/js_of_ocaml#1655)
2 parents 87f2119 + 509636e commit d4c21f5

40 files changed

+826
-491
lines changed

compiler/bin-js_of_ocaml/build_fs.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ function jsoo_create_file_extern(name,content){
7575
let pfs_fmt = Pretty_print.to_out_channel chan in
7676
let (_ : Source_map.t option) =
7777
Driver.f
78-
~target:(JavaScript pfs_fmt)
7978
~standalone:true
8079
~wrap_with_fun:`Iife
8180
~link:`Needed
81+
~formatter:pfs_fmt
8282
(Parse_bytecode.Debug.create ~include_cmis:false false)
8383
code
8484
in

compiler/bin-js_of_ocaml/check_runtime.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ let print_groups output l =
4343
output_string output (Printf.sprintf "%s\n" name)))
4444

4545
let f (runtime_files, bytecode, target_env) =
46-
Generate.init ();
46+
Config.set_target `JavaScript;
47+
Linker.reset ();
4748
let runtime_files, builtin =
4849
List.partition_map runtime_files ~f:(fun name ->
4950
match Builtins.find name with

compiler/bin-js_of_ocaml/compile.ml

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ let output_gen ~standalone ~custom_header ~build_info ~source_map output_file f
5353
let data = Source_map.to_string sm in
5454
"data:application/json;base64," ^ Base64.encode_exn data
5555
| Some output_file ->
56-
Source_map.to_file sm ~file:output_file;
56+
Source_map.to_file sm output_file;
5757
Filename.basename output_file
5858
in
5959
Pretty_print.newline fmt;
@@ -91,6 +91,7 @@ let run
9191
} =
9292
let include_cmis = toplevel && not no_cmis in
9393
let custom_header = common.Jsoo_cmdline.Arg.custom_header in
94+
Config.set_target `JavaScript;
9495
Jsoo_cmdline.Arg.eval common;
9596
Generate.init ();
9697
(match output_file with
@@ -184,7 +185,7 @@ let run
184185
let init_pseudo_fs = fs_external && standalone in
185186
let sm =
186187
match output_file with
187-
| `Stdout, fmt ->
188+
| `Stdout, formatter ->
188189
let instr =
189190
List.concat
190191
[ pseudo_fs_instr `create_file one.debug one.cmis
@@ -194,15 +195,15 @@ let run
194195
in
195196
let code = Code.prepend one.code instr in
196197
Driver.f
197-
~target:(JavaScript fmt)
198198
~standalone
199199
?profile
200200
~link
201201
~wrap_with_fun
202202
?source_map
203+
~formatter
203204
one.debug
204205
code
205-
| `File, fmt ->
206+
| `File, formatter ->
206207
let fs_instr1, fs_instr2 =
207208
match fs_output with
208209
| None -> pseudo_fs_instr `create_file one.debug one.cmis, []
@@ -218,12 +219,12 @@ let run
218219
let code = Code.prepend one.code instr in
219220
let res =
220221
Driver.f
221-
~target:(JavaScript fmt)
222222
~standalone
223223
?profile
224224
~link
225225
~wrap_with_fun
226226
?source_map
227+
~formatter
227228
one.debug
228229
code
229230
in
@@ -282,7 +283,7 @@ let run
282283
then (
283284
let prims = Linker.list_all () |> StringSet.elements in
284285
assert (List.length prims > 0);
285-
let code, uinfo = Parse_bytecode.predefined_exceptions ~target:`JavaScript in
286+
let code, uinfo = Parse_bytecode.predefined_exceptions () in
286287
let uinfo = { uinfo with primitives = uinfo.primitives @ prims } in
287288
let code : Parse_bytecode.one =
288289
{ code
@@ -322,7 +323,6 @@ let run
322323
let linkall = linkall || toplevel || dynlink in
323324
let code =
324325
Parse_bytecode.from_exe
325-
~target:`JavaScript
326326
~includes:include_dirs
327327
~include_cmis
328328
~link_info:(toplevel || dynlink)
@@ -355,7 +355,6 @@ let run
355355
let t1 = Timer.make () in
356356
let code =
357357
Parse_bytecode.from_cmo
358-
~target:`JavaScript
359358
~includes:include_dirs
360359
~include_cmis
361360
~debug:need_debug
@@ -412,7 +411,6 @@ let run
412411
let t1 = Timer.make () in
413412
let code =
414413
Parse_bytecode.from_cmo
415-
~target:`JavaScript
416414
~includes:include_dirs
417415
~include_cmis
418416
~debug:need_debug
@@ -444,7 +442,6 @@ let run
444442
let t1 = Timer.make () in
445443
let code =
446444
Parse_bytecode.from_cmo
447-
~target:`JavaScript
448445
~includes:include_dirs
449446
~include_cmis
450447
~debug:need_debug

compiler/bin-js_of_ocaml/link.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ let f
150150
; mklib
151151
; toplevel
152152
} =
153+
Config.set_target `JavaScript;
153154
Jsoo_cmdline.Arg.eval common;
154155
let with_output f =
155156
match output_file with

compiler/bin-wasm_of_ocaml/compile.ml

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ let update_sourcemap ~sourcemap_root ~sourcemap_don't_inline_content sourcemap_f
3030
if Option.is_some sourcemap_root || not sourcemap_don't_inline_content
3131
then (
3232
let open Source_map in
33-
let source_map, mappings = Source_map.of_file_no_mappings sourcemap_file in
33+
let source_map = Source_map.of_file sourcemap_file in
3434
assert (List.is_empty (Option.value source_map.sources_content ~default:[]));
3535
(* Add source file contents to source map *)
3636
let sources_content =
@@ -50,7 +50,7 @@ let update_sourcemap ~sourcemap_root ~sourcemap_don't_inline_content sourcemap_f
5050
(if Option.is_some sourcemap_root then sourcemap_root else source_map.sourceroot)
5151
}
5252
in
53-
Source_map.to_file ?mappings source_map ~file:sourcemap_file)
53+
Source_map.to_file source_map sourcemap_file)
5454

5555
let opt_with action x f =
5656
match x with
@@ -140,17 +140,23 @@ let link_runtime ~profile runtime_wasm_files output_file =
140140
let generate_prelude ~out_file =
141141
Filename.gen_file out_file
142142
@@ fun ch ->
143-
let code, uinfo = Parse_bytecode.predefined_exceptions ~target:`Wasm in
144-
let live_vars, in_cps, p, debug =
145-
Driver.f
146-
~target:Wasm
147-
~link:`Needed
148-
(Parse_bytecode.Debug.create ~include_cmis:false false)
149-
code
143+
let code, uinfo = Parse_bytecode.predefined_exceptions () in
144+
let profile =
145+
match Driver.profile 1 with
146+
| Some p -> p
147+
| None -> assert false
150148
in
149+
let Driver.{ program; variable_uses; in_cps; _ } = Driver.optimize ~profile code in
151150
let context = Wa_generate.start () in
151+
let debug = Parse_bytecode.Debug.create ~include_cmis:false false in
152152
let _ =
153-
Wa_generate.f ~context ~unit_name:(Some "prelude") ~live_vars ~in_cps ~debug p
153+
Wa_generate.f
154+
~context
155+
~unit_name:(Some "prelude")
156+
~live_vars:variable_uses
157+
~in_cps
158+
~debug
159+
program
154160
in
155161
Wa_generate.output ch ~context ~debug;
156162
uinfo.provides
@@ -244,6 +250,7 @@ let run
244250
; sourcemap_root
245251
; sourcemap_don't_inline_content
246252
} =
253+
Config.set_target `Wasm;
247254
Jsoo_cmdline.Arg.eval common;
248255
Wa_generate.init ();
249256
let output_file = fst output_file in
@@ -270,15 +277,8 @@ let run
270277
List.iter builtin ~f:(fun t ->
271278
let filename = Builtins.File.name t in
272279
let runtimes = Linker.Fragment.parse_builtin t in
273-
Linker.load_fragments
274-
~ignore_always_annotation:true
275-
~target_env:Target_env.Isomorphic
276-
~filename
277-
runtimes);
278-
Linker.load_files
279-
~ignore_always_annotation:true
280-
~target_env:Target_env.Isomorphic
281-
runtime_js_files;
280+
Linker.load_fragments ~target_env:Target_env.Isomorphic ~filename runtimes);
281+
Linker.load_files ~target_env:Target_env.Isomorphic runtime_js_files;
282282
Linker.check_deps ();
283283
if times () then Format.eprintf " parsing js: %a@." Timer.print t1;
284284
if times () then Format.eprintf "Start parsing...@.";
@@ -299,12 +299,17 @@ let run
299299
check_debug one;
300300
let code = one.code in
301301
let standalone = Option.is_none unit_name in
302-
let live_vars, in_cps, p, debug =
303-
Driver.f ~target:Wasm ~standalone ?profile ~link:`No one.debug code
302+
let profile =
303+
match profile, Driver.profile 1 with
304+
| Some p, _ -> p
305+
| None, Some p -> p
306+
| None, None -> assert false
304307
in
308+
let Driver.{ program; variable_uses; in_cps; _ } = Driver.optimize ~profile code in
305309
let context = Wa_generate.start () in
310+
let debug = one.debug in
306311
let toplevel_name, generated_js =
307-
Wa_generate.f ~context ~unit_name ~live_vars ~in_cps ~debug p
312+
Wa_generate.f ~context ~unit_name ~live_vars:variable_uses ~in_cps ~debug program
308313
in
309314
if standalone then Wa_generate.add_start_function ~context toplevel_name;
310315
Wa_generate.output ch ~context ~debug;
@@ -352,12 +357,7 @@ let run
352357
let compile_cmo cmo cont =
353358
let t1 = Timer.make () in
354359
let code =
355-
Parse_bytecode.from_cmo
356-
~target:`Wasm
357-
~includes:include_dirs
358-
~debug:need_debug
359-
cmo
360-
ic
360+
Parse_bytecode.from_cmo ~includes:include_dirs ~debug:need_debug cmo ic
361361
in
362362
let unit_info = Unit_info.of_cmo cmo in
363363
let unit_name = Ocaml_compiler.Cmo_format.name cmo in
@@ -391,7 +391,6 @@ let run
391391
let t1 = Timer.make () in
392392
let code =
393393
Parse_bytecode.from_exe
394-
~target:`Wasm
395394
~includes:include_dirs
396395
~include_cmis:false
397396
~link_info:false

compiler/lib-dynlink/js_of_ocaml_compiler_dynlink.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ let split_primitives p =
1616
external get_section_table : unit -> (string * Obj.t) list = "caml_get_section_table"
1717

1818
let () =
19+
(match Sys.backend_type with
20+
| Sys.Other "js_of_ocaml" -> Config.set_target `JavaScript
21+
| Sys.(Native | Bytecode | Other _) -> failwith "Expected backend `js_of_ocaml`");
1922
let global = J.pure_js_expr "globalThis" in
2023
Config.Flag.set "use-js-string" (Jsoo_runtime.Sys.Config.use_js_string ());
2124
Config.Flag.set "effects" (Jsoo_runtime.Sys.Config.effects ());

compiler/lib-runtime-files/gen/gen.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ let rec list_product l =
4747
let bool = [ true; false ]
4848

4949
let () =
50+
Js_of_ocaml_compiler.Config.set_target `JavaScript;
5051
let () = set_binary_mode_out stdout true in
5152
match Array.to_list Sys.argv with
5253
| [] -> assert false

compiler/lib/code.ml

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -284,10 +284,10 @@ type constant =
284284
| NativeString of Native_string.t
285285
| Float of float
286286
| Float_array of float array
287-
| Int of int32
288-
| Int32 of int32
289-
| Int64 of int64
290-
| NativeInt of nativeint
287+
| Int of Int32.t
288+
| Int32 of Int32.t
289+
| Int64 of Int64.t
290+
| NativeInt of Int32.t (* Native int are 32bit on all known backend *)
291291
| Tuple of int * constant array * array_or_not
292292

293293
module Constant = struct
@@ -311,7 +311,7 @@ module Constant = struct
311311
!same
312312
| Int a, Int b | Int32 a, Int32 b -> Some (Int32.equal a b)
313313
| Int64 a, Int64 b -> Some (Int64.equal a b)
314-
| NativeInt a, NativeInt b -> Some (Nativeint.equal a b)
314+
| NativeInt a, NativeInt b -> Some (Int32.equal a b)
315315
| Float_array a, Float_array b -> Some (Array.equal Float.ieee_equal a b)
316316
| Float a, Float b -> Some (Float.ieee_equal a b)
317317
| String _, NativeString _ | NativeString _, String _ -> None
@@ -459,7 +459,7 @@ module Print = struct
459459
| Int i -> Format.fprintf f "%ld" i
460460
| Int32 i -> Format.fprintf f "%ldl" i
461461
| Int64 i -> Format.fprintf f "%LdL" i
462-
| NativeInt i -> Format.fprintf f "%ndn" i
462+
| NativeInt i -> Format.fprintf f "%ldn" i
463463
| Tuple (tag, a, _) -> (
464464
Format.fprintf f "<%d>" tag;
465465
match Array.length a with
@@ -816,6 +816,7 @@ let with_invariant = Debug.find "invariant"
816816
let check_defs = false
817817

818818
let invariant { blocks; start; _ } =
819+
let target = Config.target () in
819820
if with_invariant ()
820821
then (
821822
assert (Addr.Map.mem start blocks);
@@ -830,15 +831,28 @@ let invariant { blocks; start; _ } =
830831
assert (not (Var.ISet.mem defs x));
831832
Var.ISet.add defs x)
832833
in
834+
let check_constant = function
835+
| NativeInt _ | Int32 _ ->
836+
assert (
837+
match target with
838+
| `Wasm -> true
839+
| _ -> false)
840+
| String _ | NativeString _ | Float _ | Float_array _ | Int _ | Int64 _
841+
| Tuple (_, _, _) -> ()
842+
in
843+
let check_prim_arg = function
844+
| Pc c -> check_constant c
845+
| Pv _ -> ()
846+
in
833847
let check_expr = function
834848
| Apply _ -> ()
835849
| Block (_, _, _, _) -> ()
836850
| Field (_, _, _) -> ()
837851
| Closure (l, cont) ->
838852
List.iter l ~f:define;
839853
check_cont cont
840-
| Constant _ -> ()
841-
| Prim (_, _) -> ()
854+
| Constant c -> check_constant c
855+
| Prim (_, args) -> List.iter ~f:check_prim_arg args
842856
| Special _ -> ()
843857
in
844858
let check_instr (i, _loc) =

compiler/lib/code.mli

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,10 @@ type constant =
164164
| NativeString of Native_string.t
165165
| Float of float
166166
| Float_array of float array
167-
| Int of int32
168-
| Int32 of int32 (** Only produced when compiling to WebAssembly. *)
169-
| Int64 of int64
170-
| NativeInt of nativeint (** Only produced when compiling to WebAssembly. *)
167+
| Int of Int32.t
168+
| Int32 of Int32.t (** Only produced when compiling to WebAssembly. *)
169+
| Int64 of Int64.t
170+
| NativeInt of Int32.t (** Only produced when compiling to WebAssembly. *)
171171
| Tuple of int * constant array * array_or_not
172172

173173
module Constant : sig

compiler/lib/config.ml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ module Param = struct
162162
p
163163
~name:"tc"
164164
~desc:"Set tailcall optimisation"
165-
(enum [ "trampoline", TcTrampoline; (* default *) "none", TcNone ])
165+
(enum [ "trampoline", TcTrampoline (* default *); "none", TcNone ])
166166

167167
let lambda_lifting_threshold =
168168
(* When we reach this depth, we start looking for functions to be lifted *)
@@ -178,3 +178,15 @@ module Param = struct
178178
~desc:"Set baseline for lifting deeply nested functions"
179179
(int 1)
180180
end
181+
182+
(****)
183+
184+
let target_ : [ `JavaScript | `Wasm | `None ] ref = ref `None
185+
186+
let target () =
187+
match !target_ with
188+
| `None -> failwith "target was not set"
189+
| (`JavaScript | `Wasm) as t -> t
190+
191+
let set_target (t : [ `JavaScript | `Wasm ]) =
192+
target_ := (t :> [ `JavaScript | `Wasm | `None ])

0 commit comments

Comments
 (0)