Skip to content

Commit b144f46

Browse files
committed
Wat file preprocessor
1 parent c4335b1 commit b144f46

File tree

5 files changed

+63
-5
lines changed

5 files changed

+63
-5
lines changed

compiler/bin-wasm_of_ocaml/compile.ml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ let with_runtime_files ~runtime_wasm_files f =
9090

9191
let build_runtime ~runtime_file =
9292
(* Keep this variables in sync with gen/gen.ml *)
93-
let variables = [] in
93+
let variables =
94+
[ "wasi", Config.Flag.wasi (); "trap-on-exception", Config.Flag.trap_on_exception () ]
95+
in
9496
match
9597
List.find_opt Runtime_files.precompiled_runtimes ~f:(fun (flags, _) ->
9698
assert (
@@ -107,7 +109,9 @@ let build_runtime ~runtime_file =
107109
; file = module_name ^ ".wat"
108110
; source = Contents contents
109111
})
110-
Runtime_files.wat_files
112+
(if Config.Flag.wasi ()
113+
then ("libc", Runtime_files.wasi_libc) :: Runtime_files.wat_files
114+
else Runtime_files.wat_files)
111115
in
112116
Runtime.build
113117
~link_options:[ "-g" ]

compiler/bin-wasm_of_ocaml/dune

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
gen/gen.exe
2828
../../runtime/wasm/runtime.js
2929
../../runtime/wasm/deps.json
30+
../../runtime/wasm/runtime-wasi.js
31+
../../runtime/wasm/deps-wasi.json
32+
../../runtime/wasm/libc.wasm
3033
(glob_files ../../runtime/wasm/*.wat)
3134
(glob_files ../../runtime/wasm/runtime-*.wasm))
3235
(action

compiler/bin-wasm_of_ocaml/gen/gen.ml

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ let read_file ic = really_input_string ic (in_channel_length ic)
33
(* Keep the two variables below in sync with function build_runtime in
44
../compile.ml *)
55

6-
let default_flags = []
6+
let default_flags = [ "trap-on-exception", false ]
77

8-
let interesting_runtimes = [ [] ]
8+
let interesting_runtimes = [ [ "wasi", false ]; [ "wasi", true ] ]
99

1010
let name_runtime l =
1111
let flags = List.filter_map (fun (k, v) -> if v then Some k else None) l in
@@ -30,10 +30,19 @@ let () =
3030
Format.printf
3131
"let dependencies = \"%s\"@."
3232
(String.escaped (read_file (open_in_bin Sys.argv.(2))));
33+
Format.printf
34+
"let js_wasi_launcher = \"%s\"@."
35+
(String.escaped (read_file (open_in_bin Sys.argv.(3))));
36+
Format.printf
37+
"let wasi_dependencies = \"%s\"@."
38+
(String.escaped (read_file (open_in_bin Sys.argv.(4))));
39+
Format.printf
40+
"let wasi_libc = \"%s\"@."
41+
(String.escaped (read_file (open_in_bin Sys.argv.(5))));
3342
let wat_files, runtimes =
3443
List.partition
3544
(fun f -> Filename.check_suffix f ".wat")
36-
(Array.to_list (Array.sub Sys.argv 3 (Array.length Sys.argv - 3)))
45+
(Array.to_list (Array.sub Sys.argv 6 (Array.length Sys.argv - 6)))
3746
in
3847
Format.printf
3948
"let wat_files = [%a]@."

compiler/lib-wasm/wat_preprocess.ml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,32 @@ let rec rewrite_list st l = List.iter ~f:(rewrite st) l
407407

408408
and rewrite st elt =
409409
match elt with
410+
| { desc =
411+
List
412+
({ desc = Atom "try"; _ }
413+
:: ( { desc = List ({ desc = Atom "result"; _ } :: _); _ }
414+
:: { desc = List ({ desc = Atom "do"; loc = _, pos_after_do } :: body)
415+
; loc = _, pos_after_body
416+
}
417+
:: _
418+
| { desc = List ({ desc = Atom "do"; loc = _, pos_after_do } :: body)
419+
; loc = _, pos_after_body
420+
}
421+
:: _ ))
422+
; loc = pos, pos'
423+
}
424+
when false (*ZZZ StringMap.find "trap-on-exception" st.variables*) ->
425+
write st pos;
426+
Buffer.add_string st.buf "(block";
427+
skip st pos_after_do;
428+
rewrite_list st body;
429+
write st pos_after_body;
430+
skip st pos'
431+
| { desc = List ({ desc = Atom "throw"; _ } :: _); loc = pos, pos' }
432+
when false (*ZZZ StringMap.find "trap-on-exception" st.variables*) ->
433+
write st pos;
434+
Buffer.add_string st.buf "(unreachable)";
435+
skip st pos'
410436
| { desc =
411437
List
412438
[ { desc = Atom "@if"; _ }

runtime/wasm/dune

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,22 @@
1616
../../compiler/bin-wasm_of_ocaml/wasmoo_link_wasm.exe
1717
--binaryen=-g
1818
--binaryen-opt=-O3
19+
--disable wasi
20+
%{target}
21+
%{read-lines:args})))
22+
23+
(rule
24+
(target runtime-wasi.wasm)
25+
(deps
26+
args
27+
(glob_files *.wat))
28+
(action
29+
(run
30+
wasmoo_util
31+
link
32+
--binaryen=-g
33+
--binaryen-opt=-O3
34+
--enable wasi
1935
%{target}
2036
%{read-lines:args})))
2137

0 commit comments

Comments
 (0)