Skip to content

Commit 93ced73

Browse files
committed
Add flag trap-on-exception
To test with Wasm engines which do not support exceptions
1 parent 7d9c434 commit 93ced73

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
@@ -98,6 +98,7 @@ let build_runtime ~runtime_file =
9898
| `Cps -> "cps"
9999
| `Double_translation -> assert false) )
100100
; "wasi", Wat_preprocess.Bool (Config.Flag.wasi ())
101+
; "trap-on-exception", Wat_preprocess.Bool (Config.Flag.trap_on_exception ())
101102
]
102103
in
103104
match

compiler/bin-wasm_of_ocaml/gen/gen.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ let check_js_file fname =
3232
(* Keep the two variables below in sync with function build_runtime in
3333
../compile.ml *)
3434

35-
let default_flags = []
35+
let default_flags = [ "trap-on-exception", `B false ]
3636

3737
let interesting_runtimes =
3838
[ [ "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
@@ -134,6 +134,7 @@ let optimize
134134
command
135135
("wasm-opt"
136136
:: (common_options ()
137+
@ (if Config.Flag.trap_on_exception () then [] else [ "--traps-never-happen" ])
137138
@ Option.value ~default:optimization_options.(level - 1) options
138139
@ [ Filename.quote input_file; "-o"; Filename.quote output_file ])
139140
@ 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
@@ -462,19 +462,23 @@ let expression_or_instructions ctx st in_function =
462462
@ [ List (Atom "else" :: expression iff) ])
463463
]
464464
| Try (ty, body, catches) ->
465-
[ List
466-
(Atom "try"
467-
:: (block_type st ty
468-
@ List (Atom "do" :: instructions body)
469-
:: List.map
470-
~f:(fun (tag, i, ty) ->
471-
List
472-
(Atom "catch"
473-
:: index st.tag_names tag
474-
:: (instruction (Wasm_ast.Event Code_generation.hidden_location)
475-
@ instruction (Wasm_ast.Br (i + 1, Some (Pop ty))))))
476-
catches))
477-
]
465+
if Config.Flag.trap_on_exception ()
466+
then [ List (Atom "block" :: (block_type st ty @ instructions body)) ]
467+
else
468+
[ List
469+
(Atom "try"
470+
:: (block_type st ty
471+
@ List (Atom "do" :: instructions body)
472+
:: List.map
473+
~f:(fun (tag, i, ty) ->
474+
List
475+
(Atom "catch"
476+
:: index st.tag_names tag
477+
:: (instruction
478+
(Wasm_ast.Event Code_generation.hidden_location)
479+
@ instruction (Wasm_ast.Br (i + 1, Some (Pop ty))))))
480+
catches))
481+
]
478482
| ExternConvertAny e' -> [ List (Atom "extern.convert_any" :: expression e') ]
479483
and instruction i =
480484
match i with
@@ -518,8 +522,14 @@ let expression_or_instructions ctx st in_function =
518522
| None -> []
519523
| Some e -> expression e))
520524
]
521-
| Throw (tag, e) -> [ List (Atom "throw" :: index st.tag_names tag :: expression e) ]
522-
| Rethrow i -> [ List [ Atom "rethrow"; Atom (string_of_int i) ] ]
525+
| Throw (tag, e) ->
526+
if Config.Flag.trap_on_exception ()
527+
then [ List [ Atom "unreachable" ] ]
528+
else [ List (Atom "throw" :: index st.tag_names tag :: expression e) ]
529+
| Rethrow i ->
530+
if Config.Flag.trap_on_exception ()
531+
then [ List [ Atom "unreachable" ] ]
532+
else [ List [ Atom "rethrow"; Atom (string_of_int i) ] ]
523533
| CallInstr (f, l) ->
524534
[ List
525535
(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)