Skip to content

Commit 9811eaa

Browse files
committed
Use a try_table like instruction internally
1 parent a95879e commit 9811eaa

File tree

9 files changed

+63
-66
lines changed

9 files changed

+63
-66
lines changed

compiler/lib/wasm/wa_asm_output.ml

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,17 @@ module Output () = struct
343343
| RefEq _
344344
| RefNull _
345345
| Br_on_cast _
346-
| Br_on_cast_fail _ -> assert false (* Not supported *)
346+
| Br_on_cast_fail _-> assert false (* Not supported *)
347+
| Try (ty, body, catches) ->
348+
Feature.require exception_handling;
349+
line (string "try" ^^ block_type ty)
350+
^^ indent (concat_map (instruction m) body)
351+
^^ concat_map
352+
(fun (tag, i, ty) ->
353+
line (string "catch " ^^ index tag)
354+
^^ indent (instruction m (Wa_ast.Br (i, Some (Pop ty)))))
355+
catches
356+
^^ line (string "end_try")
347357

348358
and instruction m i =
349359
match i with
@@ -394,19 +404,6 @@ module Output () = struct
394404
| CallInstr (x, l) -> concat_map (expression m) l ^^ line (string "call " ^^ index x)
395405
| Nop -> empty
396406
| Push e -> expression m e
397-
| Try (ty, body, catches, catch_all) ->
398-
Feature.require exception_handling;
399-
line (string "try" ^^ block_type ty)
400-
^^ indent (concat_map (instruction m) body)
401-
^^ concat_map
402-
(fun (tag, l) ->
403-
line (string "catch " ^^ index tag)
404-
^^ indent (concat_map (instruction m) l))
405-
catches
406-
^^ (match catch_all with
407-
| None -> empty
408-
| Some l -> line (string "catch_all") ^^ indent (concat_map (instruction m) l))
409-
^^ line (string "end_try")
410407
| Throw (i, e) ->
411408
Feature.require exception_handling;
412409
expression m e ^^ line (string "throw " ^^ index i)

compiler/lib/wasm/wa_ast.ml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ type expression =
174174
| Br_on_cast of int * ref_type * ref_type * expression
175175
| Br_on_cast_fail of int * ref_type * ref_type * expression
176176
| IfExpr of value_type * expression * expression * expression
177+
| Try of func_type * instruction list * (var * int * value_type) list
177178

178179
and instruction =
179180
| Drop of expression
@@ -191,11 +192,6 @@ and instruction =
191192
| CallInstr of var * expression list
192193
| Nop
193194
| Push of expression
194-
| Try of
195-
func_type
196-
* instruction list
197-
* (var * instruction list) list
198-
* instruction list option
199195
| Throw of var * expression
200196
| Rethrow of int
201197
| ArraySet of var * expression * expression * expression

compiler/lib/wasm/wa_code_generation.ml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,8 @@ let rec is_smi e =
458458
| RefCast _
459459
| RefNull _
460460
| Br_on_cast _
461-
| Br_on_cast_fail _ -> false
461+
| Br_on_cast_fail _
462+
| Try _ -> false
462463
| BinOp ((F32 _ | F64 _), _, _) | RefTest _ | RefEq _ -> true
463464
| IfExpr (_, _, ift, iff) -> is_smi ift && is_smi iff
464465

@@ -583,11 +584,9 @@ let if_ ty e l1 l2 =
583584
| W.UnOp (I32 Eqz, e') -> instr (If (ty, e', instrs2, instrs1))
584585
| _ -> instr (If (ty, e, instrs1, instrs2))
585586

586-
let try_ ty body handlers =
587+
let try_expr ty body handlers =
587588
let* body = blk body in
588-
let tags = List.map ~f:fst handlers in
589-
let* handler_bodies = expression_list blk (List.map ~f:snd handlers) in
590-
instr (Try (ty, body, List.combine tags handler_bodies, None))
589+
return (W.Try (ty, body, handlers))
591590

592591
let need_apply_fun ~cps ~arity st =
593592
let ctx = st.context in

compiler/lib/wasm/wa_code_generation.mli

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ val block_expr : Wa_ast.func_type -> unit t -> expression
128128

129129
val if_ : Wa_ast.func_type -> expression -> unit t -> unit t -> unit t
130130

131-
val try_ : Wa_ast.func_type -> unit t -> (Code.Var.t * unit t) list -> unit t
131+
val try_expr :
132+
Wa_ast.func_type -> unit t -> (Code.Var.t * int * Wa_ast.value_type) list -> expression
132133

133134
val add_var : ?typ:Wa_ast.value_type -> Wa_ast.var -> Wa_ast.var t
134135

compiler/lib/wasm/wa_core_target.ml

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -650,13 +650,17 @@ let internal_primitives = Hashtbl.create 0
650650

651651
let handle_exceptions ~result_typ ~fall_through ~context body x exn_handler =
652652
let* ocaml_tag = register_import ~name:"ocaml_exception" (Tag Value.value) in
653-
try_
653+
block
654654
{ params = []; result = result_typ }
655-
(body ~result_typ ~fall_through:(`Block (-1)) ~context)
656-
[ ( ocaml_tag
657-
, let* () = store ~always:true x (return (W.Pop Value.value)) in
658-
exn_handler ~result_typ ~fall_through ~context )
659-
]
655+
(let* () =
656+
store
657+
x
658+
(try_expr
659+
{ params = []; result = [ Value.value ] }
660+
(body ~result_typ ~fall_through:(`Block (-1)) ~context:(`Skip :: context))
661+
[ ocaml_tag, 0, Value.value ])
662+
in
663+
exn_handler ~result_typ ~fall_through ~context)
660664

661665
let post_process_function_body ~param_names:_ ~locals:_ instrs = instrs
662666

compiler/lib/wasm/wa_gc_target.ml

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,8 @@ module Value = struct
530530
| Pop _
531531
| Call_ref _
532532
| Br_on_cast _
533-
| Br_on_cast_fail _ -> false
533+
| Br_on_cast_fail _
534+
| Try _ -> false
534535
| IfExpr (_, e1, e2, e3) -> effect_free e1 && effect_free e2 && effect_free e3
535536
| ArrayNewFixed (_, l) | StructNew (_, l) -> List.for_all ~f:effect_free l
536537

@@ -1701,16 +1702,25 @@ let handle_exceptions ~result_typ ~fall_through ~context body x exn_handler =
17011702
block
17021703
{ params = []; result = result_typ }
17031704
(let* () =
1704-
try_
1705-
{ params = []; result = [] }
1706-
(body ~result_typ:[] ~fall_through:(`Block (-1)) ~context:(`Skip :: context))
1707-
[ ocaml_tag, store ~always:true x (return (W.Pop Value.value))
1708-
; ( js_tag
1709-
, let exn = Code.Var.fresh () in
1710-
let* () = store ~always:true ~typ:externref exn (return (W.Pop externref)) in
1711-
let* exn = load exn in
1712-
store ~always:true x (return (W.Call (f, [ exn ]))) )
1713-
]
1705+
store
1706+
x
1707+
(block_expr
1708+
{ params = []; result = [ Value.value ] }
1709+
(let* exn =
1710+
block_expr
1711+
{ params = []; result = [ externref ] }
1712+
(let* e =
1713+
try_expr
1714+
{ params = []; result = [ externref ] }
1715+
(body
1716+
~result_typ:[ externref ]
1717+
~fall_through:(`Block (-1))
1718+
~context:(`Skip :: `Skip :: `Skip :: context))
1719+
[ ocaml_tag, 1, Value.value; js_tag, 0, externref ]
1720+
in
1721+
instr (W.Push e))
1722+
in
1723+
instr (W.CallInstr (f, [ exn ]))))
17141724
in
17151725
exn_handler ~result_typ ~fall_through ~context)
17161726

compiler/lib/wasm/wa_initialize_locals.ml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ let rec scan_expression ctx e =
7171
scan_expression ctx cond;
7272
scan_expression (fork_context ctx) e1;
7373
scan_expression (fork_context ctx) e2
74+
| Try (_, body, _) -> scan_instructions ctx body
7475

7576
and scan_expressions ctx l = List.iter ~f:(fun e -> scan_expression ctx e) l
7677

@@ -95,10 +96,6 @@ and scan_instruction ctx i =
9596
scan_expression ctx e;
9697
scan_instructions ctx l;
9798
scan_instructions ctx l'
98-
| Try (_, body, catches, catch_all) ->
99-
scan_instructions ctx body;
100-
List.iter ~f:(fun (_, l) -> scan_instructions ctx l) catches;
101-
Option.iter ~f:(fun l -> scan_instructions ctx l) catch_all
10299
| CallInstr (_, l) | Return_call (_, l) -> scan_expressions ctx l
103100
| Br (_, None) | Return None | Rethrow _ | Nop -> ()
104101
| ArraySet (_, e, e', e'') ->

compiler/lib/wasm/wa_tail_call.ml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,6 @@ let rec instruction ~tail i =
4141
| Wa_ast.Loop (ty, l) -> Wa_ast.Loop (ty, instructions ~tail l)
4242
| Block (ty, l) -> Block (ty, instructions ~tail l)
4343
| If (ty, e, l1, l2) -> If (ty, e, instructions ~tail l1, instructions ~tail l2)
44-
| Try (ty, l, catches, catch_all) ->
45-
Try
46-
( ty
47-
, l
48-
, List.map ~f:(fun (tag, l) -> tag, instructions ~tail l) catches
49-
, Option.map ~f:(fun l -> instructions ~tail l) catch_all )
5044
| Return (Some (Call (symb, l))) -> Return_call (symb, l)
5145
| Return (Some (Call_indirect (ty, e, l))) -> Return_call_indirect (ty, e, l)
5246
| Return (Some (Call_ref (ty, e, l))) -> Return_call_ref (ty, e, l)

compiler/lib/wasm/wa_wat_output.ml

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,19 @@ let expression_or_instructions ctx st in_function =
476476
@ [ List (Atom "then" :: expression ift) ]
477477
@ [ List (Atom "else" :: expression iff) ])
478478
]
479+
| 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 (Wa_ast.Br (i + 1, Some (Pop ty)))))
490+
catches))
491+
]
479492
and instruction i =
480493
match i with
481494
| Drop e -> [ List (Atom "drop" :: expression e) ]
@@ -509,20 +522,6 @@ let expression_or_instructions ctx st in_function =
509522
@ list ~always:true "then" instructions (remove_nops l1)
510523
@ list "else" instructions (remove_nops l2)))
511524
]
512-
| Try (ty, body, catches, catch_all) ->
513-
[ List
514-
(Atom "try"
515-
:: (block_type st ty
516-
@ List (Atom "do" :: instructions body)
517-
:: (List.map
518-
~f:(fun (tag, l) ->
519-
List (Atom "catch" :: index st.tag_names tag :: instructions l))
520-
catches
521-
@
522-
match catch_all with
523-
| None -> []
524-
| Some l -> [ List (Atom "catch_all" :: instructions l) ])))
525-
]
526525
| Br_table (e, l, i) ->
527526
[ List
528527
(Atom "br_table"

0 commit comments

Comments
 (0)