Skip to content

Commit 1d48011

Browse files
committed
Make compilation target a global parameter
1 parent fb58a7e commit 1d48011

File tree

10 files changed

+75
-64
lines changed

10 files changed

+75
-64
lines changed

compiler/bin-js_of_ocaml/js_of_ocaml.ml

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ open! Js_of_ocaml_compiler.Stdlib
2222
open Js_of_ocaml_compiler
2323

2424
let () =
25+
Config.set_target `JavaScript;
2526
Sys.catch_break true;
2627
let argv = Jsoo_cmdline.normalize_argv ~warn:(warn "%s") Sys.argv in
2728
let argv =

compiler/lib/config.ml

+9-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ module Param = struct
164164
p
165165
~name:"tc"
166166
~desc:"Set tailcall optimisation"
167-
(enum [ "trampoline", TcTrampoline; (* default *) "none", TcNone ])
167+
(enum [ "trampoline", TcTrampoline (* default *); "none", TcNone ])
168168

169169
let lambda_lifting_threshold =
170170
(* When we reach this depth, we start looking for functions to be lifted *)
@@ -180,3 +180,11 @@ module Param = struct
180180
~desc:"Set baseline for lifting deeply nested functions"
181181
(int 1)
182182
end
183+
184+
(****)
185+
186+
let target_ : [ `JavaScript | `Wasm ] ref = ref `JavaScript
187+
188+
let target () = !target_
189+
190+
let set_target t = target_ := t

compiler/lib/config.mli

+10
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ module Flag : sig
8080
val disable : string -> unit
8181
end
8282

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

@@ -104,3 +105,12 @@ module Param : sig
104105

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

compiler/lib/driver.ml

+29-35
Original file line numberDiff line numberDiff line change
@@ -44,35 +44,35 @@ let deadcode p =
4444
let r, _ = deadcode' p in
4545
r
4646

47-
let inline ~target p =
47+
let inline p =
4848
if Config.Flag.inline () && Config.Flag.deadcode ()
4949
then (
5050
let p, live_vars = deadcode' p in
5151
if debug () then Format.eprintf "Inlining...@.";
52-
Inline.f ~target p live_vars)
52+
Inline.f p live_vars)
5353
else p
5454

5555
let specialize_1 (p, info) =
5656
if debug () then Format.eprintf "Specialize...@.";
5757
Specialize.f ~function_arity:(fun f -> Specialize.function_arity info f) p
5858

59-
let specialize_js ~target (p, info) =
59+
let specialize_js (p, info) =
6060
if debug () then Format.eprintf "Specialize js...@.";
61-
Specialize_js.f ~target info p
61+
Specialize_js.f info p
6262

6363
let specialize_js_once p =
6464
if debug () then Format.eprintf "Specialize js once...@.";
6565
Specialize_js.f_once p
6666

67-
let specialize' ~target (p, info) =
67+
let specialize' (p, info) =
6868
let p = specialize_1 (p, info) in
69-
let p = specialize_js ~target (p, info) in
69+
let p = specialize_js (p, info) in
7070
p, info
7171

72-
let specialize ~target p = fst (specialize' ~target p)
72+
let specialize p = fst (specialize' p)
7373

74-
let eval ~target (p, info) =
75-
if Config.Flag.staticeval () then Eval.f ~target info p else p
74+
let eval (p, info) =
75+
if Config.Flag.staticeval () then Eval.f info p else p
7676

7777
let flow p =
7878
if debug () then Format.eprintf "Data flow...@.";
@@ -144,53 +144,53 @@ let identity x = x
144144

145145
(* o1 *)
146146

147-
let o1 ~target : 'a -> 'a =
147+
let o1 : 'a -> 'a =
148148
print
149149
+> tailcall
150150
+> flow_simple (* flow simple to keep information for future tailcall opt *)
151-
+> specialize' ~target
152-
+> eval ~target
153-
+> inline ~target (* inlining may reveal new tailcall opt *)
151+
+> specialize'
152+
+> eval
153+
+> inline (* inlining may reveal new tailcall opt *)
154154
+> deadcode
155155
+> tailcall
156156
+> phi
157157
+> flow
158-
+> specialize' ~target
159-
+> eval ~target
160-
+> inline ~target
158+
+> specialize'
159+
+> eval
160+
+> inline
161161
+> deadcode
162162
+> print
163163
+> flow
164-
+> specialize' ~target
165-
+> eval ~target
166-
+> inline ~target
164+
+> specialize'
165+
+> eval
166+
+> inline
167167
+> deadcode
168168
+> phi
169169
+> flow
170-
+> specialize ~target
170+
+> specialize
171171
+> identity
172172

173173
(* o2 *)
174174

175-
let o2 ~target : 'a -> 'a = loop 10 "o1" (o1 ~target) 1 +> print
175+
let o2 : 'a -> 'a = loop 10 "o1" o1 1 +> print
176176

177177
(* o3 *)
178178

179-
let round1 ~target : 'a -> 'a =
179+
let round1 : 'a -> 'a =
180180
print
181181
+> tailcall
182-
+> inline ~target (* inlining may reveal new tailcall opt *)
182+
+> inline (* inlining may reveal new tailcall opt *)
183183
+> deadcode (* deadcode required before flow simple -> provided by constant *)
184184
+> flow_simple (* flow simple to keep information for future tailcall opt *)
185-
+> specialize' ~target
186-
+> eval ~target
185+
+> specialize'
186+
+> eval
187187
+> identity
188188

189-
let round2 ~target = flow +> specialize' ~target +> eval ~target +> deadcode +> o1 ~target
189+
let round2 = flow +> specialize' +> eval +> deadcode +> o1
190190

191-
let o3 ~target =
192-
loop 10 "tailcall+inline" (round1 ~target) 1
193-
+> loop 10 "flow" (round2 ~target) 1
191+
let o3 =
192+
loop 10 "tailcall+inline" round1 1
193+
+> loop 10 "flow" round2 1
194194
+> print
195195

196196
let generate
@@ -668,11 +668,6 @@ type 'a target =
668668
: (Deadcode.variable_uses * Effects.in_cps * Code.program * Parse_bytecode.Debug.t)
669669
target
670670

671-
let target_flag (type a) (t : a target) =
672-
match t with
673-
| JavaScript _ -> `JavaScript
674-
| Wasm -> `Wasm
675-
676671
let link_and_pack ?(standalone = true) ?(wrap_with_fun = `Iife) ?(link = `No) p =
677672
let export_runtime =
678673
match link with
@@ -705,7 +700,6 @@ let full
705700
| O1 -> o1
706701
| O2 -> o2
707702
| O3 -> o3)
708-
~target:(target_flag target)
709703
+> exact_calls ~deadcode_sentinal profile
710704
+> effects ~deadcode_sentinal
711705
+> map_fst

compiler/lib/eval.ml

+14-15
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ module type Int = sig
108108
val shift_op : (constant list) -> (t -> int -> t) -> constant option
109109
end
110110

111-
let eval_prim ~target x =
111+
let eval_prim x =
112112
match x with
113113
| Not, [ Int i ] -> bool Int32.(i = 0l)
114114
| Lt, [ Int i; Int j ] -> bool Int32.(i < j)
@@ -119,7 +119,7 @@ let eval_prim ~target x =
119119
| Extern name, l -> (
120120
let name = Primitive.resolve name in
121121
let (module Int : Int) =
122-
match target with
122+
match Config.target () with
123123
| `JavaScript -> (module Int32)
124124
| `Wasm -> (module Int31)
125125
in
@@ -185,7 +185,7 @@ let eval_prim ~target x =
185185
| "caml_sys_const_int_size", [ _ ] ->
186186
Some
187187
(Int
188-
(match target with
188+
(match Config.target () with
189189
| `JavaScript -> 32l
190190
| `Wasm -> 31l))
191191
| "caml_sys_const_big_endian", [ _ ] -> Some (Int 0l)
@@ -214,7 +214,7 @@ type is_int =
214214
| N
215215
| Unknown
216216

217-
let is_int ~target info x =
217+
let is_int info x =
218218
match x with
219219
| Pv x ->
220220
get_approx
@@ -223,7 +223,7 @@ let is_int ~target info x =
223223
match Flow.Info.def info x with
224224
| Some (Constant (Int _)) -> Y
225225
| Some (Constant (NativeInt _ | Int32 _)) ->
226-
assert (Poly.equal target `Wasm);
226+
assert (Poly.equal (Config.target ()) `Wasm);
227227
N
228228
| Some (Block (_, _, _, _) | Constant _) -> N
229229
| None | Some _ -> Unknown)
@@ -236,7 +236,7 @@ let is_int ~target info x =
236236
x
237237
| Pc (Int _) -> Y
238238
| Pc (NativeInt _ | Int32 _) ->
239-
assert (Poly.equal target `Wasm);
239+
assert (Poly.equal (Config.target ()) `Wasm);
240240
N
241241
| Pc _ -> N
242242

@@ -308,7 +308,7 @@ let constant_js_equal a b =
308308
| Tuple _, _
309309
| _, Tuple _ -> None
310310

311-
let eval_instr info ~target ((x, loc) as i) =
311+
let eval_instr info ((x, loc) as i) =
312312
match x with
313313
| Let (x, Prim (Extern (("caml_equal" | "caml_notequal") as prim), [ y; z ])) -> (
314314
match the_const_of info y, the_const_of info z with
@@ -365,7 +365,7 @@ let eval_instr info ~target ((x, loc) as i) =
365365
below fail. *)
366366
[ i ]
367367
| Let (x, Prim (IsInt, [ y ])) -> (
368-
match is_int ~target info y with
368+
match is_int info y with
369369
| Unknown -> [ i ]
370370
| (Y | N) as b ->
371371
let c = Constant (bool' Poly.(b = Y)) in
@@ -381,7 +381,7 @@ let eval_instr info ~target ((x, loc) as i) =
381381
| Let (x, Prim (Extern "caml_sys_const_backend_type", [ _ ])) ->
382382
let jsoo = Code.Var.fresh () in
383383
let backend_name =
384-
match target with
384+
match Config.target () with
385385
| `JavaScript -> "js_of_ocaml"
386386
| `Wasm -> "wasm_of_ocaml"
387387
in
@@ -398,7 +398,6 @@ let eval_instr info ~target ((x, loc) as i) =
398398
| _ -> false)
399399
then
400400
eval_prim
401-
~target
402401
( prim
403402
, List.map prim_args' ~f:(function
404403
| Some c -> c
@@ -416,7 +415,7 @@ let eval_instr info ~target ((x, loc) as i) =
416415
, Prim
417416
( prim
418417
, List.map2 prim_args prim_args' ~f:(fun arg (c : constant option) ->
419-
match c, target with
418+
match c, Config.target () with
420419
| ( Some ((Int _ | Int32 _ | NativeInt _ | NativeString _) as c)
421420
, _ ) -> Pc c
422421
| Some (Float _ as c), `JavaScript -> Pc c
@@ -547,15 +546,15 @@ let drop_exception_handler blocks =
547546
blocks
548547
blocks
549548

550-
let eval ~target info blocks =
549+
let eval info blocks =
551550
Addr.Map.map
552551
(fun block ->
553-
let body = List.concat_map block.body ~f:(eval_instr ~target info) in
552+
let body = List.concat_map block.body ~f:(eval_instr info) in
554553
let branch = eval_branch info block.branch in
555554
{ block with Code.body; Code.branch })
556555
blocks
557556

558-
let f ~target info p =
559-
let blocks = eval ~target info p.blocks in
557+
let f info p =
558+
let blocks = eval info p.blocks in
560559
let blocks = drop_exception_handler blocks in
561560
{ p with blocks }

compiler/lib/eval.mli

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ val clear_static_env : unit -> unit
2121

2222
val set_static_env : string -> string -> unit
2323

24-
val f : target:[ `JavaScript | `Wasm ] -> Flow.Info.t -> Code.program -> Code.program
24+
val f : Flow.Info.t -> Code.program -> Code.program

compiler/lib/inline.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -322,9 +322,9 @@ let inline ~first_class_primitives live_vars closures name pc (outer, p) =
322322

323323
let times = Debug.find "times"
324324

325-
let f ~target p live_vars =
325+
let f p live_vars =
326326
let first_class_primitives =
327-
match target with
327+
match Config.target () with
328328
| `JavaScript -> not (Config.Flag.effects ())
329329
| `Wasm -> false
330330
in

compiler/lib/inline.mli

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,4 @@
1818
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
1919
*)
2020

21-
val f :
22-
target:[ `JavaScript | `Wasm ] -> Code.program -> Deadcode.variable_uses -> Code.program
21+
val f : Code.program -> Deadcode.variable_uses -> Code.program

compiler/lib/specialize_js.ml

+7-7
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ open! Stdlib
2222
open Code
2323
open Flow
2424

25-
let specialize_instr ~target info i =
26-
match i, target with
25+
let specialize_instr info i =
26+
match i, Config.target () with
2727
| Let (x, Prim (Extern "caml_format_int", [ y; z ])), `JavaScript -> (
2828
match the_string_of info y with
2929
| Some "%d" -> (
@@ -156,7 +156,7 @@ let equal3 a b c = Code.Var.equal a b && Code.Var.equal b c
156156

157157
let equal4 a b c d = Code.Var.equal a b && Code.Var.equal b c && Code.Var.equal c d
158158

159-
let specialize_instrs ~target info l =
159+
let specialize_instrs info l =
160160
let rec aux info checks l acc =
161161
match l with
162162
| [] -> List.rev acc
@@ -285,22 +285,22 @@ let specialize_instrs ~target info l =
285285
in
286286
aux info ((y, idx) :: checks) r acc
287287
| _ ->
288-
let i = specialize_instr ~target info i in
288+
let i = specialize_instr info i in
289289
aux info checks r ((i, loc) :: acc))
290290
in
291291
aux info [] l []
292292

293-
let specialize_all_instrs ~target info p =
293+
let specialize_all_instrs info p =
294294
let blocks =
295295
Addr.Map.map
296-
(fun block -> { block with Code.body = specialize_instrs ~target info block.body })
296+
(fun block -> { block with Code.body = specialize_instrs info block.body })
297297
p.blocks
298298
in
299299
{ p with blocks }
300300

301301
(****)
302302

303-
let f ~target info p = specialize_all_instrs ~target info p
303+
let f info p = specialize_all_instrs info p
304304

305305
let f_once p =
306306
let rec loop acc l =

compiler/lib/specialize_js.mli

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@
1818
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
1919
*)
2020

21-
val f : target:[ `JavaScript | `Wasm ] -> Flow.Info.t -> Code.program -> Code.program
21+
val f : Flow.Info.t -> Code.program -> Code.program
2222

2323
val f_once : Code.program -> Code.program

0 commit comments

Comments
 (0)