Skip to content

Commit 5cfd549

Browse files
committed
WASI runtime
1 parent b144f46 commit 5cfd549

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+4294
-345
lines changed

compiler/bin-wasm_of_ocaml/compile.ml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,10 @@ let link_and_optimize
165165
@@ fun opt_temp_sourcemap' ->
166166
let primitives =
167167
Binaryen.dead_code_elimination
168-
~dependencies:Runtime_files.dependencies
168+
~dependencies:
169+
(if Config.Flag.wasi ()
170+
then Runtime_files.wasi_dependencies
171+
else Runtime_files.dependencies)
169172
~opt_input_sourcemap:opt_temp_sourcemap
170173
~opt_output_sourcemap:opt_temp_sourcemap'
171174
~input_file:temp_file
@@ -273,7 +276,13 @@ let build_js_runtime ~primitives ?runtime_arguments () =
273276
in
274277
let prelude = Link.output_js always_required_js in
275278
let init_fun =
276-
match Parse_js.parse (Parse_js.Lexer.of_string Runtime_files.js_runtime) with
279+
match
280+
Parse_js.parse
281+
(Parse_js.Lexer.of_string
282+
(if Config.Flag.wasi ()
283+
then Runtime_files.js_wasi_launcher
284+
else Runtime_files.js_launcher))
285+
with
277286
| [ (Expression_statement f, _) ] -> f
278287
| _ -> assert false
279288
in

compiler/bin-wasm_of_ocaml/gen/gen.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ let print_flags f flags =
2525
let () =
2626
let () = set_binary_mode_out stdout true in
2727
Format.printf
28-
"let js_runtime = \"%s\"@."
28+
"let js_launcher = \"%s\"@."
2929
(String.escaped (read_file (open_in_bin Sys.argv.(1))));
3030
Format.printf
3131
"let dependencies = \"%s\"@."

compiler/lib-wasm/binaryen.ml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ let common_options () =
3636
; "--enable-bulk-memory"
3737
; "--enable-nontrapping-float-to-int"
3838
; "--enable-strings"
39+
; "--enable-multimemory" (* To keep wasm-merge happy *)
3940
]
4041
in
4142
if Config.Flag.pretty () then "-g" :: l else l
@@ -111,9 +112,9 @@ let dead_code_elimination
111112
filter_unused_primitives primitives usage_file
112113

113114
let optimization_options =
114-
[| [ "-O2"; "--skip-pass=inlining-optimizing"; "--traps-never-happen" ]
115-
; [ "-O2"; "--skip-pass=inlining-optimizing"; "--traps-never-happen" ]
116-
; [ "-O3"; "--skip-pass=inlining-optimizing"; "--traps-never-happen" ]
115+
[| [ "-O2"; "--skip-pass=inlining-optimizing" ]
116+
; [ "-O2"; "--skip-pass=inlining-optimizing" ]
117+
; [ "-O3"; "--skip-pass=inlining-optimizing" ]
117118
|]
118119

119120
let optimize
@@ -132,6 +133,7 @@ let optimize
132133
command
133134
("wasm-opt"
134135
:: (common_options ()
136+
@ (if Config.Flag.trap_on_exception () then [] else [ "--traps-never-happen" ])
135137
@ Option.value ~default:optimization_options.(level - 1) options
136138
@ [ Filename.quote input_file; "-o"; Filename.quote output_file ])
137139
@ opt_flag "--input-source-map" opt_input_sourcemap

compiler/lib-wasm/gc_target.ml

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -573,9 +573,13 @@ module Value = struct
573573
return ())
574574
(val_int (if negate then Arith.eqz n else n))
575575

576-
let eq x y = eq_gen ~negate:false x y
576+
let eq x y =
577+
if Config.Flag.wasi () then val_int (ref_eq x y) else eq_gen ~negate:false x y
577578

578-
let neq x y = eq_gen ~negate:true x y
579+
let neq x y =
580+
if Config.Flag.wasi ()
581+
then val_int (Arith.eqz (ref_eq x y))
582+
else eq_gen ~negate:true x y
579583

580584
let ult = binop Arith.(ult)
581585

@@ -1294,7 +1298,12 @@ module Math = struct
12941298
{ W.params = List.init ~len:n ~f:(fun _ : W.value_type -> F64); result = [ F64 ] }
12951299

12961300
let unary name x =
1297-
let* f = register_import ~import_module:"Math" ~name (Fun (float_func_type 1)) in
1301+
let* f =
1302+
register_import
1303+
~import_module:(if Config.Flag.wasi () then "env" else "Math")
1304+
~name
1305+
(Fun (float_func_type 1))
1306+
in
12981307
let* x = x in
12991308
return (W.Call (f, [ x ]))
13001309

@@ -1337,7 +1346,12 @@ module Math = struct
13371346
let log10 f = unary "log10" f
13381347

13391348
let binary name x y =
1340-
let* f = register_import ~import_module:"Math" ~name (Fun (float_func_type 2)) in
1349+
let* f =
1350+
register_import
1351+
~import_module:(if Config.Flag.wasi () then "env" else "Math")
1352+
~name
1353+
(Fun (float_func_type 2))
1354+
in
13411355
let* x = x in
13421356
let* y = y in
13431357
return (W.Call (f, [ x; y ]))
@@ -1682,21 +1696,34 @@ let handle_exceptions ~result_typ ~fall_through ~context body x exn_handler =
16821696
x
16831697
(block_expr
16841698
{ params = []; result = [ Value.value ] }
1685-
(let* exn =
1686-
block_expr
1687-
{ params = []; result = [ externref ] }
1688-
(let* e =
1689-
try_expr
1690-
{ params = []; result = [ externref ] }
1691-
(body
1692-
~result_typ:[ externref ]
1693-
~fall_through:`Skip
1694-
~context:(`Skip :: `Skip :: `Catch :: context))
1695-
[ ocaml_tag, 1, Value.value; js_tag, 0, externref ]
1696-
in
1697-
instr (W.Push e))
1698-
in
1699-
instr (W.CallInstr (f, [ exn ]))))
1699+
(if Config.Flag.wasi ()
1700+
then
1701+
let* e =
1702+
try_expr
1703+
{ params = []; result = [ Value.value ] }
1704+
(body
1705+
~result_typ:[ Value.value ]
1706+
~fall_through:`Skip
1707+
~context:(`Skip :: `Catch :: context))
1708+
[ ocaml_tag, 0, Value.value ]
1709+
in
1710+
instr (W.Push e)
1711+
else
1712+
let* exn =
1713+
block_expr
1714+
{ params = []; result = [ externref ] }
1715+
(let* e =
1716+
try_expr
1717+
{ params = []; result = [ externref ] }
1718+
(body
1719+
~result_typ:[ externref ]
1720+
~fall_through:`Skip
1721+
~context:(`Skip :: `Skip :: `Catch :: context))
1722+
[ ocaml_tag, 1, Value.value; js_tag, 0, externref ]
1723+
in
1724+
instr (W.Push e))
1725+
in
1726+
instr (W.CallInstr (f, [ exn ]))))
17001727
in
17011728
let* () = no_event in
17021729
exn_handler ~result_typ ~fall_through ~context)

compiler/lib-wasm/wat_output.ml

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -444,19 +444,23 @@ let expression_or_instructions ctx st in_function =
444444
@ [ List (Atom "else" :: expression iff) ])
445445
]
446446
| Try (ty, body, catches) ->
447-
[ List
448-
(Atom "try"
449-
:: (block_type st ty
450-
@ List (Atom "do" :: instructions body)
451-
:: List.map
452-
~f:(fun (tag, i, ty) ->
453-
List
454-
(Atom "catch"
455-
:: index st.tag_names tag
456-
:: (instruction (Wasm_ast.Event Code_generation.hidden_location)
457-
@ instruction (Wasm_ast.Br (i + 1, Some (Pop ty))))))
458-
catches))
459-
]
447+
if Config.Flag.trap_on_exception ()
448+
then [ List (Atom "block" :: (block_type st ty @ instructions body)) ]
449+
else
450+
[ List
451+
(Atom "try"
452+
:: (block_type st ty
453+
@ List (Atom "do" :: instructions body)
454+
:: List.map
455+
~f:(fun (tag, i, ty) ->
456+
List
457+
(Atom "catch"
458+
:: index st.tag_names tag
459+
:: (instruction
460+
(Wasm_ast.Event Code_generation.hidden_location)
461+
@ instruction (Wasm_ast.Br (i + 1, Some (Pop ty))))))
462+
catches))
463+
]
460464
and instruction i =
461465
match i with
462466
| Drop e -> [ List (Atom "drop" :: expression e) ]
@@ -499,8 +503,14 @@ let expression_or_instructions ctx st in_function =
499503
| None -> []
500504
| Some e -> expression e))
501505
]
502-
| Throw (tag, e) -> [ List (Atom "throw" :: index st.tag_names tag :: expression e) ]
503-
| Rethrow i -> [ List [ Atom "rethrow"; Atom (string_of_int i) ] ]
506+
| Throw (tag, e) ->
507+
if Config.Flag.trap_on_exception ()
508+
then [ List [ Atom "unreachable" ] ]
509+
else [ List (Atom "throw" :: index st.tag_names tag :: expression e) ]
510+
| Rethrow i ->
511+
if Config.Flag.trap_on_exception ()
512+
then [ List [ Atom "unreachable" ] ]
513+
else [ List [ Atom "rethrow"; Atom (string_of_int i) ] ]
504514
| CallInstr (f, l) ->
505515
[ List
506516
(Atom "call"

compiler/lib-wasm/wat_preprocess.ml

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -410,26 +410,42 @@ and rewrite st elt =
410410
| { desc =
411411
List
412412
({ 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-
:: _ ))
413+
:: { desc = List ({ desc = Atom "result"; _ } :: _)
414+
; loc = pos_before_result, pos_after_result
415+
}
416+
:: { desc = List ({ desc = Atom "do"; loc = _, pos_after_do } :: body)
417+
; loc = _, pos_after_body
418+
}
419+
:: _)
420+
; loc = pos, pos'
421+
}
422+
when variable_is_set st "trap-on-exception" ->
423+
write st pos;
424+
Buffer.add_string st.buf "(block";
425+
skip st pos_before_result;
426+
write st pos_after_result;
427+
skip st pos_after_do;
428+
rewrite_list st body;
429+
write st pos_after_body;
430+
skip st pos'
431+
| { desc =
432+
List
433+
({ desc = Atom "try"; _ }
434+
:: { desc = List ({ desc = Atom "do"; loc = _, pos_after_do } :: body)
435+
; loc = _, pos_after_body
436+
}
437+
:: _)
422438
; loc = pos, pos'
423439
}
424-
when false (*ZZZ StringMap.find "trap-on-exception" st.variables*) ->
440+
when variable_is_set st "trap-on-exception" ->
425441
write st pos;
426442
Buffer.add_string st.buf "(block";
427443
skip st pos_after_do;
428444
rewrite_list st body;
429445
write st pos_after_body;
430446
skip st pos'
431447
| { desc = List ({ desc = Atom "throw"; _ } :: _); loc = pos, pos' }
432-
when false (*ZZZ StringMap.find "trap-on-exception" st.variables*) ->
448+
when variable_is_set st "trap-on-exception" ->
433449
write st pos;
434450
Buffer.add_string st.buf "(unreachable)";
435451
skip st pos'

compiler/lib/config.ml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ module Flag = struct
101101
let auto_link = o ~name:"auto-link" ~default:true
102102

103103
let es6 = o ~name:"es6" ~default:false
104+
105+
let wasi = o ~name:"wasi" ~default:false
106+
107+
let trap_on_exception = o ~name:"trap-on-exception" ~default:false
104108
end
105109

106110
module Param = struct

compiler/lib/config.mli

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ module Flag : sig
7676

7777
val es6 : unit -> bool
7878

79+
val wasi : unit -> bool
80+
81+
val trap_on_exception : unit -> bool
82+
7983
val enable : string -> unit
8084

8185
val disable : string -> unit

compiler/lib/inline.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ let inline ~first_class_primitives live_vars closures name pc (outer, p) =
328328

329329
let times = Debug.find "times"
330330

331-
let f p live_vars =
331+
let f p (live_vars : Deadcode.variable_uses) =
332332
let first_class_primitives =
333333
match Config.target (), Config.effects () with
334334
| `JavaScript, `Disabled -> true

compiler/tests-jsoo/dune

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
(enabled_if
66
(>= %{ocaml_version} 4.14))
77
(inline_tests
8+
(deps
9+
(sandbox preserve_file_kind))
810
(modes js wasm best))
911
(preprocess
1012
(pps ppx_expect)))
@@ -16,6 +18,8 @@
1618
(enabled_if
1719
(>= %{ocaml_version} 5.1.1))
1820
(inline_tests
21+
(deps
22+
(sandbox preserve_file_kind))
1923
(modes js wasm best))
2024
(preprocess
2125
(pps ppx_expect)))
@@ -27,6 +31,21 @@
2731
(enabled_if
2832
(>= %{ocaml_version} 5.1.1))
2933
(inline_tests
34+
(deps
35+
(sandbox preserve_file_kind))
36+
(modes js wasm best))
37+
(preprocess
38+
(pps ppx_expect)))
39+
40+
(library
41+
(name jsoo_testsuite_perms)
42+
(modules test_unix_perms)
43+
(libraries unix)
44+
(enabled_if
45+
(<> %{profile} wasi))
46+
(inline_tests
47+
(deps
48+
(sandbox preserve_file_kind))
3049
(modes js wasm best))
3150
(preprocess
3251
(pps ppx_expect)))
@@ -41,13 +60,16 @@
4160
test_float16
4261
test_marshal_compressed
4362
test_parsing
63+
test_unix_perms
4464
calc_parser
4565
calc_lexer))
4666
(libraries unix compiler-libs.common js_of_ocaml-compiler)
4767
(foreign_stubs
4868
(language c)
4969
(names bigarray_stubs))
5070
(inline_tests
71+
(deps
72+
(sandbox preserve_file_kind))
5173
(modes js wasm best))
5274
(preprocess
5375
(pps ppx_expect)))

compiler/tests-jsoo/lib-effects/dune

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
(env
22
(with-effects-double-translation)
33
(with-effects)
4+
(wasi
5+
(wasm_of_ocaml
6+
(flags
7+
(:standard --enable effects))))
48
(_
59
(js_of_ocaml
610
(flags
@@ -11,6 +15,8 @@
1115
(enabled_if
1216
(>= %{ocaml_version} 5))
1317
(inline_tests
18+
(deps
19+
(sandbox preserve_file_kind))
1420
(modes js wasm best))
1521
(modules
1622
(:standard

0 commit comments

Comments
 (0)