Skip to content

Commit 5b51983

Browse files
committed
Add flag trap-on-exception
To test with Wasm engines which do not support exceptions
1 parent 64900a8 commit 5b51983

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
@@ -93,6 +93,7 @@ let build_runtime ~runtime_file =
9393
| `Cps -> "cps"
9494
| `Disabled | `Double_translation -> assert false) )
9595
; "wasi", Wat_preprocess.Bool (Config.Flag.wasi ())
96+
; "trap-on-exception", Wat_preprocess.Bool (Config.Flag.trap_on_exception ())
9697
]
9798
in
9899
match

compiler/bin-wasm_of_ocaml/gen/gen.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ 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", `B false ]
77

88
let interesting_runtimes =
99
[ [ "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
@@ -112,9 +112,9 @@ let dead_code_elimination
112112
filter_unused_primitives primitives usage_file
113113

114114
let optimization_options =
115-
[| [ "-O2"; "--skip-pass=inlining-optimizing"; "--traps-never-happen" ]
116-
; [ "-O2"; "--skip-pass=inlining-optimizing"; "--traps-never-happen" ]
117-
; [ "-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" ]
118118
|]
119119

120120
let optimize
@@ -133,6 +133,7 @@ let optimize
133133
command
134134
("wasm-opt"
135135
:: (common_options ()
136+
@ (if Config.Flag.trap_on_exception () then [] else [ "--traps-never-happen" ])
136137
@ Option.value ~default:optimization_options.(level - 1) options
137138
@ [ Filename.quote input_file; "-o"; Filename.quote output_file ])
138139
@ opt_flag "--input-source-map" opt_input_sourcemap

compiler/lib-wasm/wat_output.ml

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -447,19 +447,23 @@ let expression_or_instructions ctx st in_function =
447447
@ [ List (Atom "else" :: expression iff) ])
448448
]
449449
| Try (ty, body, catches) ->
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 (Wasm_ast.Event Code_generation.hidden_location)
460-
@ instruction (Wasm_ast.Br (i + 1, Some (Pop ty))))))
461-
catches))
462-
]
450+
if Config.Flag.trap_on_exception ()
451+
then [ List (Atom "block" :: (block_type st ty @ instructions body)) ]
452+
else
453+
[ List
454+
(Atom "try"
455+
:: (block_type st ty
456+
@ List (Atom "do" :: instructions body)
457+
:: List.map
458+
~f:(fun (tag, i, ty) ->
459+
List
460+
(Atom "catch"
461+
:: index st.tag_names tag
462+
:: (instruction
463+
(Wasm_ast.Event Code_generation.hidden_location)
464+
@ instruction (Wasm_ast.Br (i + 1, Some (Pop ty))))))
465+
catches))
466+
]
463467
| ExternConvertAny e' -> [ List (Atom "extern.convert_any" :: expression e') ]
464468
and instruction i =
465469
match i with
@@ -503,8 +507,14 @@ let expression_or_instructions ctx st in_function =
503507
| None -> []
504508
| Some e -> expression e))
505509
]
506-
| Throw (tag, e) -> [ List (Atom "throw" :: index st.tag_names tag :: expression e) ]
507-
| Rethrow i -> [ List [ Atom "rethrow"; Atom (string_of_int i) ] ]
510+
| Throw (tag, e) ->
511+
if Config.Flag.trap_on_exception ()
512+
then [ List [ Atom "unreachable" ] ]
513+
else [ List (Atom "throw" :: index st.tag_names tag :: expression e) ]
514+
| Rethrow i ->
515+
if Config.Flag.trap_on_exception ()
516+
then [ List [ Atom "unreachable" ] ]
517+
else [ List [ Atom "rethrow"; Atom (string_of_int i) ] ]
508518
| CallInstr (f, l) ->
509519
[ List
510520
(Atom "call"

compiler/lib-wasm/wat_preprocess.ml

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

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

compiler/lib/config.ml

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

105105
let wasi = o ~name:"wasi" ~default:false
106+
107+
let trap_on_exception = o ~name:"trap-on-exception" ~default:false
106108
end
107109

108110
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)