Skip to content

Commit 2ab1d30

Browse files
OlivierNicolevouillonhhugo
committed
Target-specific code (#1655)
Co-authored-by: Olivier Nicole <[email protected]> Co-authored-by: Jérôme Vouillon <[email protected]> Co-authored-by: Hugo Heuzard <[email protected]>
1 parent 99c46f0 commit 2ab1d30

25 files changed

+686
-296
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: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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
@@ -200,9 +201,10 @@ let run
200201
~link
201202
~wrap_with_fun
202203
?source_map
204+
~formatter
203205
one.debug
204206
code
205-
| `File, fmt ->
207+
| `File, formatter ->
206208
let fs_instr1, fs_instr2 =
207209
match fs_output with
208210
| None -> pseudo_fs_instr `create_file one.debug one.cmis, []
@@ -224,6 +226,7 @@ let run
224226
~link
225227
~wrap_with_fun
226228
?source_map
229+
~formatter
227230
one.debug
228231
code
229232
in

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/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: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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/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 ])

compiler/lib/config.mli

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ module Flag : sig
7878
val disable : string -> unit
7979
end
8080

81+
(** This module contains parameters that may be modified through command-line flags. *)
8182
module Param : sig
8283
val set : string -> string -> unit
8384

@@ -102,3 +103,13 @@ module Param : sig
102103

103104
val lambda_lifting_baseline : unit -> int
104105
end
106+
107+
(****)
108+
109+
(** {2 Parameters that are constant across a program run} *)
110+
111+
(** These parameters should be set at most once at the beginning of the program. *)
112+
113+
val target : unit -> [ `JavaScript | `Wasm ]
114+
115+
val set_target : [ `JavaScript | `Wasm ] -> unit

0 commit comments

Comments
 (0)