Skip to content

Commit 07ef3a6

Browse files
committed
Add flag trap-on-exception
To test with Wasm engines which do not support exceptions
1 parent f454362 commit 07ef3a6

File tree

7 files changed

+100
-20
lines changed

7 files changed

+100
-20
lines changed

compiler/bin-wasm_of_ocaml/compile.ml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ let with_runtime_files ~runtime_wasm_files f =
9090

9191
let build_runtime ~runtime_file =
9292
(* Keep this variables in sync with gen/gen.ml *)
93-
let variables = [ "wasi", Config.Flag.wasi () ] in
93+
let variables =
94+
[ "wasi", Config.Flag.wasi (); "trap-on-exception", Config.Flag.trap_on_exception () ]
95+
in
9496
match
9597
List.find_opt Runtime_files.precompiled_runtimes ~f:(fun (flags, _) ->
9698
assert (

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", false ]
77

88
let interesting_runtimes = [ [ "wasi", false ]; [ "wasi", true ] ]
99

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
@@ -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: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,69 @@ let rec rewrite_list st l = List.iter ~f:(rewrite st) l
407407

408408
and rewrite st elt =
409409
match elt with
410+
| { desc =
411+
List
412+
({ desc = Atom "try"; _ }
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+
:: _)
438+
; loc = pos, pos'
439+
}
440+
when variable_is_set st "trap-on-exception" ->
441+
write st pos;
442+
Buffer.add_string st.buf "(block";
443+
skip st pos_after_do;
444+
rewrite_list st body;
445+
write st pos_after_body;
446+
skip st pos'
447+
| { desc = List ({ desc = Atom "throw"; _ } :: _); loc = pos, pos' }
448+
when variable_is_set st "trap-on-exception" ->
449+
write st pos;
450+
Buffer.add_string st.buf "(unreachable)";
451+
skip st pos'
452+
| { desc = List ({ desc = Atom "tag"; _ } :: _); loc = pos, pos' }
453+
| { desc =
454+
List
455+
({ desc = Atom "import"; _ }
456+
:: _
457+
:: _
458+
:: { desc = List ({ desc = Atom "tag"; _ } :: _); _ }
459+
:: _)
460+
; loc = pos, pos'
461+
}
462+
| { desc =
463+
List
464+
({ desc = Atom "export"; _ }
465+
:: _
466+
:: { desc = List ({ desc = Atom "tag"; _ } :: _); _ }
467+
:: _)
468+
; loc = pos, pos'
469+
}
470+
when variable_is_set st "trap-on-exception" ->
471+
write st pos;
472+
skip st pos'
410473
| { desc =
411474
List
412475
[ { 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)