Skip to content

Commit 67c2255

Browse files
committed
WASI runtime
1 parent cad43d1 commit 67c2255

Some content is hidden

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

55 files changed

+4214
-150
lines changed

compiler/bin-wasm_of_ocaml/cmd_arg.ml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ let normalize_effects (effects : [ `Cps | `Jspi ] option) common : Config.effect
4444
| None ->
4545
(* For backward compatibility, consider that [--enable effects] alone means
4646
[--effects cps] *)
47-
if List.mem "effects" ~set:common.Jsoo_cmdline.Arg.optim.enable then `Cps else `Jspi
47+
if List.mem "effects" ~set:common.Jsoo_cmdline.Arg.optim.enable
48+
then `Cps
49+
else if List.mem "wasi" ~set:common.Jsoo_cmdline.Arg.optim.enable
50+
then `Disabled
51+
else `Jspi
4852
| Some ((`Cps | `Jspi) as e) -> e
4953

5054
type t =

compiler/bin-wasm_of_ocaml/compile.ml

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,11 @@ let build_runtime ~runtime_file =
8989
[ ( "effects"
9090
, Wat_preprocess.String
9191
(match Config.effects () with
92+
| `Disabled -> "disabled"
9293
| `Jspi -> "jspi"
9394
| `Cps -> "cps"
94-
| `Disabled | `Double_translation -> assert false) )
95+
| `Double_translation -> assert false) )
96+
; "wasi", Wat_preprocess.Bool (Config.Flag.wasi ())
9597
]
9698
in
9799
match
@@ -110,7 +112,9 @@ let build_runtime ~runtime_file =
110112
; file = module_name ^ ".wat"
111113
; source = Contents contents
112114
})
113-
Runtime_files.wat_files
115+
(if Config.Flag.wasi ()
116+
then ("libc", Runtime_files.wasi_libc) :: Runtime_files.wat_files
117+
else Runtime_files.wat_files)
114118
in
115119
Runtime.build
116120
~link_options:[ "-g" ]
@@ -163,7 +167,10 @@ let link_and_optimize
163167
@@ fun opt_temp_sourcemap' ->
164168
let primitives =
165169
Binaryen.dead_code_elimination
166-
~dependencies:Runtime_files.dependencies
170+
~dependencies:
171+
(if Config.Flag.wasi ()
172+
then Runtime_files.wasi_dependencies
173+
else Runtime_files.dependencies)
167174
~opt_input_sourcemap:opt_temp_sourcemap
168175
~opt_output_sourcemap:opt_temp_sourcemap'
169176
~input_file:temp_file
@@ -283,7 +290,13 @@ let build_js_runtime ~primitives ?runtime_arguments () =
283290
| _ -> assert false
284291
in
285292
let init_fun =
286-
match Parse_js.parse (Parse_js.Lexer.of_string Runtime_files.js_runtime) with
293+
match
294+
Parse_js.parse
295+
(Parse_js.Lexer.of_string
296+
(if Config.Flag.wasi ()
297+
then Runtime_files.js_wasi_launcher
298+
else Runtime_files.js_launcher))
299+
with
287300
| [ (Expression_statement f, _) ] -> f
288301
| _ -> assert false
289302
in
@@ -522,9 +535,12 @@ let run
522535
tmp_wasm_file
523536
in
524537
let wasm_name =
525-
Printf.sprintf
526-
"code-%s"
527-
(String.sub (Digest.to_hex (Digest.file tmp_wasm_file)) ~pos:0 ~len:20)
538+
if Config.Flag.wasi ()
539+
then "code"
540+
else
541+
Printf.sprintf
542+
"code-%s"
543+
(String.sub (Digest.to_hex (Digest.file tmp_wasm_file)) ~pos:0 ~len:20)
528544
in
529545
let tmp_wasm_file' = Filename.concat tmp_dir (wasm_name ^ ".wasm") in
530546
Sys.rename tmp_wasm_file tmp_wasm_file';

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: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,20 @@ let check_js_file fname =
3434

3535
let default_flags = []
3636

37-
let interesting_runtimes = [ [ "effects", `S "jspi" ]; [ "effects", `S "cps" ] ]
37+
let interesting_runtimes =
38+
[ [ "effects", `S "jspi"; "wasi", `B false ]
39+
; [ "effects", `S "cps"; "wasi", `B false ]
40+
; [ "effects", `S "disabled"; "wasi", `B true ]
41+
; [ "effects", `S "cps"; "wasi", `B true ]
42+
]
43+
44+
let defaults = [ "effects", "disabled" ]
3845

3946
let name_runtime standard l =
4047
let flags =
4148
List.filter_map l ~f:(fun (k, v) ->
4249
match v with
43-
| `S s -> Some s
50+
| `S s -> if List.mem (k, s) ~set:defaults then None else Some s
4451
| `B b -> if b then Some k else None)
4552
in
4653
String.concat ~sep:"-" ("runtime" :: (if standard then [ "standard" ] else flags))
@@ -67,25 +74,31 @@ let print_flags f flags =
6774

6875
let () =
6976
let () = set_binary_mode_out stdout true in
70-
let js_runtime, deps, wat_files, runtimes =
77+
let js_launcher, deps, js_wasi_launcher, wasi_deps, wasi_libc, wat_files, runtimes =
7178
match Array.to_list Sys.argv with
72-
| _ :: js_runtime :: deps :: rest ->
73-
assert (Filename.check_suffix js_runtime ".js");
79+
| _ :: js_launcher :: deps :: js_wasi_launcher :: wasi_deps :: wasi_libc :: rest ->
80+
assert (Filename.check_suffix js_launcher ".js");
81+
assert (Filename.check_suffix js_wasi_launcher ".js");
7482
assert (Filename.check_suffix deps ".json");
83+
assert (Filename.check_suffix wasi_deps ".json");
7584
let wat_files, rest =
7685
List.partition rest ~f:(fun f -> Filename.check_suffix f ".wat")
7786
in
7887
let wasm_files, rest =
7988
List.partition rest ~f:(fun f -> Filename.check_suffix f ".wasm")
8089
in
8190
assert (List.is_empty rest);
82-
js_runtime, deps, wat_files, wasm_files
91+
js_launcher, deps, js_wasi_launcher, wasi_deps, wasi_libc, wat_files, wasm_files
8392
| _ -> assert false
8493
in
85-
check_js_file js_runtime;
94+
check_js_file js_launcher;
95+
check_js_file js_wasi_launcher;
8696
Format.printf "open Wasm_of_ocaml_compiler@.";
87-
Format.printf "let js_runtime = {|\n%s\n|}@." (Fs.read_file js_runtime);
97+
Format.printf "let js_launcher = {|\n%s\n|}@." (Fs.read_file js_launcher);
8898
Format.printf "let dependencies = {|\n%s\n|}@." (Fs.read_file deps);
99+
Format.printf "let js_wasi_launcher = {|\n%s\n|}@." (Fs.read_file js_wasi_launcher);
100+
Format.printf "let wasi_dependencies = {|\n%s\n|}@." (Fs.read_file wasi_deps);
101+
Format.printf "let wasi_libc = %S@." (String.escaped (Fs.read_file wasi_libc));
89102
Format.printf
90103
"let wat_files = [%a]@."
91104
(Format.pp_print_list (fun f file ->

compiler/lib-wasm/binaryen.ml

Lines changed: 1 addition & 0 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

compiler/lib-wasm/gc_target.ml

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

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

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

581585
let ult = binop Arith.(ult)
582586

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

13011305
let unary name x =
1302-
let* f = register_import ~import_module:"Math" ~name (Fun (float_func_type 1)) in
1306+
let* f =
1307+
register_import
1308+
~import_module:(if Config.Flag.wasi () then "env" else "Math")
1309+
~name
1310+
(Fun (float_func_type 1))
1311+
in
13031312
let* x = x in
13041313
return (W.Call (f, [ x ]))
13051314

@@ -1342,7 +1351,12 @@ module Math = struct
13421351
let log10 f = unary "log10" f
13431352

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

compiler/lib-wasm/generate.ml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ let times = Debug.find "times"
2626
let effects_cps () =
2727
match Config.effects () with
2828
| `Cps | `Double_translation -> true
29-
| `Jspi -> false
30-
| `Disabled -> assert false
29+
| `Disabled | `Jspi -> false
3130

3231
module Generate (Target : Target_sig.S) = struct
3332
open Target

compiler/lib/config.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ 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
104106
end
105107

106108
module Param = struct

compiler/lib/config.mli

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

7777
val es6 : unit -> bool
7878

79+
val wasi : unit -> bool
80+
7981
val enable : string -> unit
8082

8183
val disable : string -> unit

compiler/lib/driver.ml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -703,8 +703,9 @@ let optimize ~profile p =
703703
+> map_fst
704704
(match Config.target (), Config.effects () with
705705
| `JavaScript, `Disabled -> Generate_closure.f
706-
| `JavaScript, (`Cps | `Double_translation) | `Wasm, (`Jspi | `Cps) -> Fun.id
707-
| `JavaScript, `Jspi | `Wasm, (`Disabled | `Double_translation) -> assert false)
706+
| `JavaScript, (`Cps | `Double_translation) | `Wasm, (`Disabled | `Jspi | `Cps)
707+
-> Fun.id
708+
| `JavaScript, `Jspi | `Wasm, `Double_translation -> assert false)
708709
+> map_fst deadcode'
709710
in
710711
if times () then Format.eprintf "Start Optimizing...@.";

0 commit comments

Comments
 (0)