Skip to content

Commit 5a9d1d2

Browse files
Target-specific code
Co-authored-by: Olivier Nicole <[email protected]>
1 parent 818dcd6 commit 5a9d1d2

16 files changed

+492
-167
lines changed

compiler/bin-js_of_ocaml/build_fs.ml

+1-1
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)
7879
~standalone:true
7980
~wrap_with_fun:`Iife
8081
~link:`Needed
81-
pfs_fmt
8282
(Parse_bytecode.Debug.create ~include_cmis:false false)
8383
code
8484
in

compiler/bin-js_of_ocaml/compile.ml

+7-3
Original file line numberDiff line numberDiff line change
@@ -196,12 +196,12 @@ let run
196196
in
197197
let code = Code.prepend one.code instr in
198198
Driver.f
199+
~target:(JavaScript fmt)
199200
~standalone
200201
?profile
201202
~link
202203
~wrap_with_fun
203204
?source_map
204-
fmt
205205
one.debug
206206
code
207207
| `File, fmt ->
@@ -220,12 +220,12 @@ let run
220220
let code = Code.prepend one.code instr in
221221
let res =
222222
Driver.f
223+
~target:(JavaScript fmt)
223224
~standalone
224225
?profile
225226
~link
226227
~wrap_with_fun
227228
?source_map
228-
fmt
229229
one.debug
230230
code
231231
in
@@ -285,7 +285,7 @@ let run
285285
| `None ->
286286
let prims = Linker.list_all () |> StringSet.elements in
287287
assert (List.length prims > 0);
288-
let code, uinfo = Parse_bytecode.predefined_exceptions () in
288+
let code, uinfo = Parse_bytecode.predefined_exceptions ~target:`JavaScript in
289289
let uinfo = { uinfo with primitives = uinfo.primitives @ prims } in
290290
let code : Parse_bytecode.one =
291291
{ code
@@ -331,6 +331,7 @@ let run
331331
let linkall = linkall || toplevel || dynlink in
332332
let code =
333333
Parse_bytecode.from_exe
334+
~target:`JavaScript
334335
~includes:include_dirs
335336
~include_cmis
336337
~link_info:(toplevel || dynlink)
@@ -363,6 +364,7 @@ let run
363364
let t1 = Timer.make () in
364365
let code =
365366
Parse_bytecode.from_cmo
367+
~target:`JavaScript
366368
~includes:include_dirs
367369
~include_cmis
368370
~debug:need_debug
@@ -419,6 +421,7 @@ let run
419421
let t1 = Timer.make () in
420422
let code =
421423
Parse_bytecode.from_cmo
424+
~target:`JavaScript
422425
~includes:include_dirs
423426
~include_cmis
424427
~debug:need_debug
@@ -450,6 +453,7 @@ let run
450453
let t1 = Timer.make () in
451454
let code =
452455
Parse_bytecode.from_cmo
456+
~target:`JavaScript
453457
~includes:include_dirs
454458
~include_cmis
455459
~debug:need_debug

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

+11
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,13 @@ 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+
117+
val set_target : [ `JavaScript | `Wasm ] -> unit

compiler/lib/driver.ml

+59-23
Original file line numberDiff line numberDiff line change
@@ -658,13 +658,34 @@ let configure formatter =
658658
Code.Var.set_pretty (pretty && not (Config.Flag.shortvar ()));
659659
Code.Var.set_stable (Config.Flag.stable_var ())
660660

661-
let full ~standalone ~wrap_with_fun ~profile ~link ~source_map formatter d p =
662-
let exported_runtime = not standalone in
661+
type 'a target =
662+
| JavaScript : Pretty_print.t -> Source_map.t option target
663+
| Wasm
664+
: (Deadcode.variable_uses * Effects.in_cps * Code.program * Parse_bytecode.Debug.t)
665+
target
666+
667+
let link_and_pack ?(standalone = true) ?(wrap_with_fun = `Iife) ?(link = `No) p =
663668
let export_runtime =
664669
match link with
665670
| `All | `All_from _ -> true
666671
| `Needed | `No -> false
667672
in
673+
p
674+
|> link' ~export_runtime ~standalone ~link
675+
|> pack ~wrap_with_fun ~standalone
676+
|> coloring
677+
|> check_js
678+
679+
let full
680+
(type result)
681+
~(target : result target)
682+
~standalone
683+
~wrap_with_fun
684+
~profile
685+
~link
686+
~source_map
687+
d
688+
p : result =
668689
let deadcode_sentinal =
669690
(* If deadcode is disabled, this field is just fresh variable *)
670691
Code.Var.fresh_n "undef"
@@ -677,56 +698,71 @@ let full ~standalone ~wrap_with_fun ~profile ~link ~source_map formatter d p =
677698
| O3 -> o3)
678699
+> exact_calls ~deadcode_sentinal profile
679700
+> effects ~deadcode_sentinal
680-
+> map_fst (if Config.Flag.effects () then fun x -> x else Generate_closure.f)
701+
+> map_fst
702+
(match target with
703+
| JavaScript _ -> if Config.Flag.effects () then Fun.id else Generate_closure.f
704+
| Wasm -> Fun.id)
681705
+> map_fst deadcode'
682706
in
683-
let emit =
684-
generate
685-
d
686-
~exported_runtime
687-
~wrap_with_fun
688-
~warn_on_unhandled_effect:standalone
689-
~deadcode_sentinal
690-
+> link' ~export_runtime ~standalone ~link
691-
+> pack ~wrap_with_fun ~standalone
692-
+> coloring
693-
+> check_js
694-
+> output formatter ~source_map ()
695-
in
696707
if times () then Format.eprintf "Start Optimizing...@.";
697708
let t = Timer.make () in
698709
let r = opt p in
699710
let () = if times () then Format.eprintf " optimizations : %a@." Timer.print t in
700-
emit r
711+
match target with
712+
| JavaScript formatter ->
713+
let exported_runtime = not standalone in
714+
let emit formatter =
715+
generate
716+
d
717+
~exported_runtime
718+
~wrap_with_fun
719+
~warn_on_unhandled_effect:standalone
720+
~deadcode_sentinal
721+
+> link_and_pack ~standalone ~wrap_with_fun ~link
722+
+> output formatter ~source_map ()
723+
in
724+
let source_map = emit formatter r in
725+
source_map
726+
| Wasm ->
727+
let (p, live_vars), _, in_cps = r in
728+
live_vars, in_cps, p, d
701729

702-
let full_no_source_map ~standalone ~wrap_with_fun ~profile ~link formatter d p =
730+
let full_no_source_map ~formatter ~standalone ~wrap_with_fun ~profile ~link d p =
703731
let (_ : Source_map.t option) =
704-
full ~standalone ~wrap_with_fun ~profile ~link ~source_map:None formatter d p
732+
full
733+
~target:(JavaScript formatter)
734+
~standalone
735+
~wrap_with_fun
736+
~profile
737+
~link
738+
~source_map:None
739+
d
740+
p
705741
in
706742
()
707743

708744
let f
745+
~target
709746
?(standalone = true)
710747
?(wrap_with_fun = `Iife)
711748
?(profile = O1)
712749
~link
713750
?source_map
714-
formatter
715751
d
716752
p =
717-
full ~standalone ~wrap_with_fun ~profile ~link ~source_map formatter d p
753+
full ~target ~standalone ~wrap_with_fun ~profile ~link ~source_map d p
718754

719755
let f' ?(standalone = true) ?(wrap_with_fun = `Iife) ?(profile = O1) ~link formatter d p =
720-
full_no_source_map ~standalone ~wrap_with_fun ~profile ~link formatter d p
756+
full_no_source_map ~formatter ~standalone ~wrap_with_fun ~profile ~link d p
721757

722758
let from_string ~prims ~debug s formatter =
723759
let p, d = Parse_bytecode.from_string ~prims ~debug s in
724760
full_no_source_map
761+
~formatter
725762
~standalone:false
726763
~wrap_with_fun:`Anonymous
727764
~profile:O1
728765
~link:`No
729-
formatter
730766
d
731767
p
732768

compiler/lib/driver.mli

+16-3
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,22 @@
2020

2121
type profile
2222

23+
type 'a target =
24+
| JavaScript : Pretty_print.t -> Source_map.t option target
25+
| Wasm
26+
: (Deadcode.variable_uses * Effects.in_cps * Code.program * Parse_bytecode.Debug.t)
27+
target
28+
2329
val f :
24-
?standalone:bool
30+
target:'result target
31+
-> ?standalone:bool
2532
-> ?wrap_with_fun:[ `Iife | `Anonymous | `Named of string ]
2633
-> ?profile:profile
2734
-> link:[ `All | `All_from of string list | `Needed | `No ]
2835
-> ?source_map:Source_map.t
29-
-> Pretty_print.t
3036
-> Parse_bytecode.Debug.t
3137
-> Code.program
32-
-> Source_map.t option
38+
-> 'result
3339

3440
val f' :
3541
?standalone:bool
@@ -48,6 +54,13 @@ val from_string :
4854
-> Pretty_print.t
4955
-> unit
5056

57+
val link_and_pack :
58+
?standalone:bool
59+
-> ?wrap_with_fun:[ `Iife | `Anonymous | `Named of string ]
60+
-> ?link:[ `All | `All_from of string list | `Needed | `No ]
61+
-> Javascript.statement_list
62+
-> Javascript.statement_list
63+
5164
val configure : Pretty_print.t -> unit
5265

5366
val profiles : (int * profile) list

0 commit comments

Comments
 (0)