Skip to content

Commit d2774a6

Browse files
committed
Add flag trap-on-exception
To test with Wasm engines which do not support exceptions
1 parent 17da5ce commit d2774a6

File tree

7 files changed

+98
-19
lines changed

7 files changed

+98
-19
lines changed

compiler/bin-wasm_of_ocaml/compile.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ let build_runtime ~runtime_file =
9595
| `Cps -> "cps"
9696
| `Double_translation -> assert false) )
9797
; "wasi", Wat_preprocess.Bool (Config.Flag.wasi ())
98+
; "trap-on-exception", Wat_preprocess.Bool (Config.Flag.trap_on_exception ())
9899
]
99100
in
100101
match

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 check_js_file fname =
2525
(* Keep the two variables below in sync with function build_runtime in
2626
../compile.ml *)
2727

28-
let default_flags = []
28+
let default_flags = [ "trap-on-exception", `B false ]
2929

3030
let interesting_runtimes =
3131
[ [ "effects", `S "jspi"; "wasi", `B false ]

compiler/lib-wasm/binaryen.ml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,9 @@ let dead_code_elimination
123123
filter_unused_primitives primitives usage_file
124124

125125
let optimization_options : Profile.t -> _ = function
126-
| O1 -> [ "-O2"; "--skip-pass=inlining-optimizing"; "--traps-never-happen" ]
127-
| O2 -> [ "-O2"; "--skip-pass=inlining-optimizing"; "--traps-never-happen" ]
128-
| O3 -> [ "-O3"; "--skip-pass=inlining-optimizing"; "--traps-never-happen" ]
126+
| O1 -> [ "-O2"; "--skip-pass=inlining-optimizing" ]
127+
| O2 -> [ "-O2"; "--skip-pass=inlining-optimizing" ]
128+
| O3 -> [ "-O3"; "--skip-pass=inlining-optimizing" ]
129129

130130
let optimize
131131
~profile
@@ -138,6 +138,7 @@ let optimize
138138
command
139139
("wasm-opt"
140140
:: (common_options ()
141+
@ (if Config.Flag.trap_on_exception () then [] else [ "--traps-never-happen" ])
141142
@ (match options with
142143
| Some o -> o
143144
| None -> optimization_options profile)

compiler/lib-wasm/wat_output.ml

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -467,19 +467,23 @@ let expression_or_instructions ctx st in_function =
467467
@ [ List (Atom "else" :: expression iff) ])
468468
]
469469
| Try (ty, body, catches) ->
470-
[ List
471-
(Atom "try"
472-
:: (block_type st ty
473-
@ List (Atom "do" :: instructions body)
474-
:: List.map
475-
~f:(fun (tag, i, ty) ->
476-
List
477-
(Atom "catch"
478-
:: index st.tag_names tag
479-
:: (instruction (Wasm_ast.Event Code_generation.hidden_location)
480-
@ instruction (Wasm_ast.Br (i + 1, Some (Pop ty))))))
481-
catches))
482-
]
470+
if Config.Flag.trap_on_exception ()
471+
then [ List (Atom "block" :: (block_type st ty @ instructions body)) ]
472+
else
473+
[ List
474+
(Atom "try"
475+
:: (block_type st ty
476+
@ List (Atom "do" :: instructions body)
477+
:: List.map
478+
~f:(fun (tag, i, ty) ->
479+
List
480+
(Atom "catch"
481+
:: index st.tag_names tag
482+
:: (instruction
483+
(Wasm_ast.Event Code_generation.hidden_location)
484+
@ instruction (Wasm_ast.Br (i + 1, Some (Pop ty))))))
485+
catches))
486+
]
483487
| ExternConvertAny e' -> [ List (Atom "extern.convert_any" :: expression e') ]
484488
and instruction i =
485489
match i with
@@ -523,8 +527,14 @@ let expression_or_instructions ctx st in_function =
523527
| None -> []
524528
| Some e -> expression e))
525529
]
526-
| Throw (tag, e) -> [ List (Atom "throw" :: index st.tag_names tag :: expression e) ]
527-
| Rethrow i -> [ List [ Atom "rethrow"; Atom (string_of_int i) ] ]
530+
| Throw (tag, e) ->
531+
if Config.Flag.trap_on_exception ()
532+
then [ List [ Atom "unreachable" ] ]
533+
else [ List (Atom "throw" :: index st.tag_names tag :: expression e) ]
534+
| Rethrow i ->
535+
if Config.Flag.trap_on_exception ()
536+
then [ List [ Atom "unreachable" ] ]
537+
else [ List [ Atom "rethrow"; Atom (string_of_int i) ] ]
528538
| CallInstr (f, l) ->
529539
[ List
530540
(Atom "call"

compiler/lib-wasm/wat_preprocess.ml

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,69 @@ let rec rewrite_list st l = List.iter ~f:(rewrite st) l
416416

417417
and rewrite st elt =
418418
match elt with
419+
| { desc =
420+
List
421+
({ desc = Atom "try"; _ }
422+
:: { desc = List ({ desc = Atom "result"; _ } :: _)
423+
; loc = pos_before_result, pos_after_result
424+
}
425+
:: { desc = List ({ desc = Atom "do"; loc = _, pos_after_do } :: body)
426+
; loc = _, pos_after_body
427+
}
428+
:: _)
429+
; loc = pos, pos'
430+
}
431+
when variable_is_set st "trap-on-exception" ->
432+
write st pos;
433+
Buffer.add_string st.buf "(block";
434+
skip st pos_before_result;
435+
write st pos_after_result;
436+
skip st pos_after_do;
437+
rewrite_list st body;
438+
write st pos_after_body;
439+
skip st pos'
440+
| { desc =
441+
List
442+
({ desc = Atom "try"; _ }
443+
:: { desc = List ({ desc = Atom "do"; loc = _, pos_after_do } :: body)
444+
; loc = _, pos_after_body
445+
}
446+
:: _)
447+
; loc = pos, pos'
448+
}
449+
when variable_is_set st "trap-on-exception" ->
450+
write st pos;
451+
Buffer.add_string st.buf "(block";
452+
skip st pos_after_do;
453+
rewrite_list st body;
454+
write st pos_after_body;
455+
skip st pos'
456+
| { desc = List ({ desc = Atom "throw"; _ } :: _); loc = pos, pos' }
457+
when variable_is_set st "trap-on-exception" ->
458+
write st pos;
459+
Buffer.add_string st.buf "(unreachable)";
460+
skip st pos'
461+
| { desc = List ({ desc = Atom "tag"; _ } :: _); loc = pos, pos' }
462+
| { desc =
463+
List
464+
({ desc = Atom "import"; _ }
465+
:: _
466+
:: _
467+
:: { desc = List ({ desc = Atom "tag"; _ } :: _); _ }
468+
:: _)
469+
; loc = pos, pos'
470+
}
471+
| { desc =
472+
List
473+
({ desc = Atom "export"; _ }
474+
:: _
475+
:: { desc = List ({ desc = Atom "tag"; _ } :: _); _ }
476+
:: _)
477+
; loc = pos, pos'
478+
}
479+
when variable_is_set st "trap-on-exception" ->
480+
write st pos;
481+
skip st pos'
419482
| { desc =
420483
List
421484
[ { desc = Atom "@if"; _ }

compiler/lib/config.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ module Flag = struct
108108
let es6 = o ~name:"es6" ~default:false
109109

110110
let wasi = o ~name:"wasi" ~default:false
111+
112+
let trap_on_exception = o ~name:"trap-on-exception" ~default:false
111113
end
112114

113115
module Param = struct

compiler/lib/config.mli

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

7979
val wasi : unit -> bool
8080

81+
val trap_on_exception : unit -> bool
82+
8183
val enable : string -> unit
8284

8385
val disable : string -> unit

0 commit comments

Comments
 (0)