Skip to content

Commit c4335b1

Browse files
committed
Support for several Wasm runtimes (depending on flags)
The runtimes will either be precompiled for most common flag combinations, or compiled on the flight for unusual combinations.
1 parent aebade8 commit c4335b1

File tree

4 files changed

+91
-10
lines changed

4 files changed

+91
-10
lines changed

compiler/bin-wasm_of_ocaml/compile.ml

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,35 @@ let with_runtime_files ~runtime_wasm_files f =
8888
in
8989
Wat_preprocess.with_preprocessed_files ~variables:[] ~inputs f
9090

91+
let build_runtime ~runtime_file =
92+
(* Keep this variables in sync with gen/gen.ml *)
93+
let variables = [] in
94+
match
95+
List.find_opt Runtime_files.precompiled_runtimes ~f:(fun (flags, _) ->
96+
assert (
97+
List.length flags = List.length variables
98+
&& List.for_all2 ~f:(fun (k, _) (k', _) -> String.equal k k') flags variables);
99+
Poly.equal flags variables)
100+
with
101+
| Some (_, contents) -> Fs.write_file ~name:runtime_file ~contents
102+
| None ->
103+
let inputs =
104+
List.map
105+
~f:(fun (module_name, contents) ->
106+
{ Wat_preprocess.module_name
107+
; file = module_name ^ ".wat"
108+
; source = Contents contents
109+
})
110+
Runtime_files.wat_files
111+
in
112+
Runtime.build
113+
~link_options:[ "-g" ]
114+
~opt_options:[ "-g"; "-O2" ]
115+
~variables:
116+
(List.map ~f:(fun (k, v) : (_ * Wat_preprocess.value) -> k, Bool v) variables)
117+
~inputs
118+
~output_file:runtime_file
119+
91120
let link_and_optimize
92121
~profile
93122
~sourcemap_root
@@ -106,7 +135,7 @@ let link_and_optimize
106135
let enable_source_maps = Option.is_some opt_sourcemap_file in
107136
Fs.with_intermediate_file (Filename.temp_file "runtime" ".wasm")
108137
@@ fun runtime_file ->
109-
Fs.write_file ~name:runtime_file ~contents:Runtime_files.wasm_runtime;
138+
build_runtime ~runtime_file;
110139
Fs.with_intermediate_file (Filename.temp_file "wasm-merged" ".wasm")
111140
@@ fun temp_file ->
112141
opt_with
@@ -153,7 +182,7 @@ let link_and_optimize
153182
let link_runtime ~profile runtime_wasm_files output_file =
154183
Fs.with_intermediate_file (Filename.temp_file "runtime" ".wasm")
155184
@@ fun runtime_file ->
156-
Fs.write_file ~name:runtime_file ~contents:Runtime_files.wasm_runtime;
185+
build_runtime ~runtime_file;
157186
Fs.with_intermediate_file (Filename.temp_file "wasm-merged" ".wasm")
158187
@@ fun temp_file ->
159188
with_runtime_files ~runtime_wasm_files

compiler/bin-wasm_of_ocaml/dune

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@
2525
(target runtime_files.ml)
2626
(deps
2727
gen/gen.exe
28-
../../runtime/wasm/runtime.wasm
2928
../../runtime/wasm/runtime.js
30-
../../runtime/wasm/deps.json)
29+
../../runtime/wasm/deps.json
30+
(glob_files ../../runtime/wasm/*.wat)
31+
(glob_files ../../runtime/wasm/runtime-*.wasm))
3132
(action
3233
(with-stdout-to
3334
%{target}

compiler/bin-wasm_of_ocaml/gen/gen.ml

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,61 @@
11
let read_file ic = really_input_string ic (in_channel_length ic)
22

3+
(* Keep the two variables below in sync with function build_runtime in
4+
../compile.ml *)
5+
6+
let default_flags = []
7+
8+
let interesting_runtimes = [ [] ]
9+
10+
let name_runtime l =
11+
let flags = List.filter_map (fun (k, v) -> if v then Some k else None) l in
12+
String.concat "-" ("runtime" :: (if flags = [] then [ "standard" ] else flags))
13+
^ ".wasm"
14+
15+
let print_flags f flags =
16+
Format.fprintf
17+
f
18+
"@[<2>[ %a ]@]"
19+
(Format.pp_print_list
20+
~pp_sep:(fun f () -> Format.fprintf f ";@ ")
21+
(fun f (k, v) ->
22+
Format.fprintf f "@[\"%s\",@ %s@]" k (if v then "true" else "false")))
23+
flags
24+
325
let () =
426
let () = set_binary_mode_out stdout true in
527
Format.printf
6-
"let wasm_runtime = \"%s\"@."
28+
"let js_runtime = \"%s\"@."
729
(String.escaped (read_file (open_in_bin Sys.argv.(1))));
830
Format.printf
9-
"let js_runtime = \"%s\"@."
31+
"let dependencies = \"%s\"@."
1032
(String.escaped (read_file (open_in_bin Sys.argv.(2))));
33+
let wat_files, runtimes =
34+
List.partition
35+
(fun f -> Filename.check_suffix f ".wat")
36+
(Array.to_list (Array.sub Sys.argv 3 (Array.length Sys.argv - 3)))
37+
in
1138
Format.printf
12-
"let dependencies = \"%s\"@."
13-
(String.escaped (read_file (open_in_bin Sys.argv.(3))))
39+
"let wat_files = [%a]@."
40+
(Format.pp_print_list (fun f file ->
41+
Format.fprintf
42+
f
43+
"\"%s\", \"%s\"; "
44+
Filename.(chop_suffix (basename file) ".wat")
45+
(String.escaped (read_file (open_in_bin file)))))
46+
wat_files;
47+
Format.printf
48+
"let precompiled_runtimes = [%a]@."
49+
(Format.pp_print_list (fun f flags ->
50+
let flags = flags @ default_flags in
51+
let name = name_runtime flags in
52+
match List.find_opt (fun file -> Filename.basename file = name) runtimes with
53+
| None -> failwith ("Missing runtime " ^ name)
54+
| Some file ->
55+
Format.fprintf
56+
f
57+
"%a, \"%s\"; "
58+
print_flags
59+
flags
60+
(String.escaped (read_file (open_in_bin file)))))
61+
interesting_runtimes

runtime/wasm/dune

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
(install
22
(section lib)
33
(package wasm_of_ocaml-compiler)
4-
(files runtime.wasm runtime.js))
4+
(files
5+
(glob_files *.wat)
6+
(glob_files runtime-*.wasm)
7+
runtime.js))
58

69
(rule
7-
(target runtime.wasm)
10+
(target runtime-standard.wasm)
811
(deps
912
args
1013
(glob_files *.wat))

0 commit comments

Comments
 (0)