Skip to content

Commit 2fcc40a

Browse files
committed
Add flag trap-on-exception
To test with Wasm engines which do not support exceptions
1 parent c8a7b96 commit 2fcc40a

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
@@ -86,6 +86,7 @@ let preprocessor_variables () =
8686
| `Cps -> "cps"
8787
| `Double_translation -> assert false) )
8888
; "wasi", Wat_preprocess.Bool (Config.Flag.wasi ())
89+
; "trap-on-exception", Wat_preprocess.Bool (Config.Flag.trap_on_exception ())
8990
]
9091

9192
let with_runtime_files ~runtime_wasm_files f =

compiler/bin-wasm_of_ocaml/gen/gen.ml

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

78-
let default_flags = []
78+
let default_flags = [ "trap-on-exception", `B false ]
7979

8080
let interesting_runtimes =
8181
[ [ "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
@@ -477,19 +477,23 @@ let expression_or_instructions ctx st in_function =
477477
@ [ List (Atom "else" :: expression iff) ])
478478
]
479479
| Try (ty, body, catches) ->
480-
[ List
481-
(Atom "try"
482-
:: (block_type st ty
483-
@ List (Atom "do" :: instructions body)
484-
:: List.map
485-
~f:(fun (tag, i, ty) ->
486-
List
487-
(Atom "catch"
488-
:: index st.tag_names tag
489-
:: (instruction (Wasm_ast.Event Code_generation.hidden_location)
490-
@ instruction (Wasm_ast.Br (i + 1, Some (Pop ty))))))
491-
catches))
492-
]
480+
if Config.Flag.trap_on_exception ()
481+
then [ List (Atom "block" :: (block_type st ty @ instructions body)) ]
482+
else
483+
[ List
484+
(Atom "try"
485+
:: (block_type st ty
486+
@ List (Atom "do" :: instructions body)
487+
:: List.map
488+
~f:(fun (tag, i, ty) ->
489+
List
490+
(Atom "catch"
491+
:: index st.tag_names tag
492+
:: (instruction
493+
(Wasm_ast.Event Code_generation.hidden_location)
494+
@ instruction (Wasm_ast.Br (i + 1, Some (Pop ty))))))
495+
catches))
496+
]
493497
| ExternConvertAny e' -> [ List (Atom "extern.convert_any" :: expression e') ]
494498
| AnyConvertExtern e' -> [ List (Atom "any.convert_extern" :: expression e') ]
495499
and instruction i =
@@ -534,8 +538,14 @@ let expression_or_instructions ctx st in_function =
534538
| None -> []
535539
| Some e -> expression e))
536540
]
537-
| Throw (tag, e) -> [ List (Atom "throw" :: index st.tag_names tag :: expression e) ]
538-
| Rethrow i -> [ List [ Atom "rethrow"; Atom (string_of_int i) ] ]
541+
| Throw (tag, e) ->
542+
if Config.Flag.trap_on_exception ()
543+
then [ List [ Atom "unreachable" ] ]
544+
else [ List (Atom "throw" :: index st.tag_names tag :: expression e) ]
545+
| Rethrow i ->
546+
if Config.Flag.trap_on_exception ()
547+
then [ List [ Atom "unreachable" ] ]
548+
else [ List [ Atom "rethrow"; Atom (string_of_int i) ] ]
539549
| CallInstr (f, l) ->
540550
[ List
541551
(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 load_shapes_auto = o ~name:"load-shapes-auto" ~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)