Skip to content

Commit 65bf454

Browse files
committed
Use a try_table like instruction internally
1 parent b086746 commit 65bf454

File tree

8 files changed

+56
-81
lines changed

8 files changed

+56
-81
lines changed

compiler/lib/wasm/wa_ast.ml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ type expression =
165165
| Br_on_cast of int * ref_type * ref_type * expression
166166
| Br_on_cast_fail of int * ref_type * ref_type * expression
167167
| IfExpr of value_type * expression * expression * expression
168+
| Try of func_type * instruction list * (var * int * value_type) list
168169

169170
and instruction =
170171
| Drop of expression
@@ -180,11 +181,6 @@ and instruction =
180181
| CallInstr of var * expression list
181182
| Nop
182183
| Push of expression
183-
| Try of
184-
func_type
185-
* instruction list
186-
* (var * instruction list) list
187-
* instruction list option
188184
| Throw of var * expression
189185
| Rethrow of int
190186
| 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
@@ -447,7 +447,8 @@ let rec is_smi e =
447447
| RefCast _
448448
| RefNull _
449449
| Br_on_cast _
450-
| Br_on_cast_fail _ -> false
450+
| Br_on_cast_fail _
451+
| Try _ -> false
451452
| BinOp ((F32 _ | F64 _), _, _) | RefTest _ | RefEq _ -> true
452453
| IfExpr (_, _, ift, iff) -> is_smi ift && is_smi iff
453454

@@ -572,11 +573,9 @@ let if_ ty e l1 l2 =
572573
| W.UnOp (I32 Eqz, e') -> instr (If (ty, e', instrs2, instrs1))
573574
| _ -> instr (If (ty, e, instrs1, instrs2))
574575

575-
let try_ ty body handlers =
576+
let try_expr ty body handlers =
576577
let* body = blk body in
577-
let tags = List.map ~f:fst handlers in
578-
let* handler_bodies = expression_list blk (List.map ~f:snd handlers) in
579-
instr (Try (ty, body, List.combine tags handler_bodies, None))
578+
return (W.Try (ty, body, handlers))
580579

581580
let need_apply_fun ~cps ~arity st =
582581
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_gc_target.ml

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,8 @@ module Value = struct
526526
| Pop _
527527
| Call_ref _
528528
| Br_on_cast _
529-
| Br_on_cast_fail _ -> false
529+
| Br_on_cast_fail _
530+
| Try _ -> false
530531
| IfExpr (_, e1, e2, e3) -> effect_free e1 && effect_free e2 && effect_free e3
531532
| ArrayNewFixed (_, l) | StructNew (_, l) -> List.for_all ~f:effect_free l
532533

@@ -1676,16 +1677,25 @@ let handle_exceptions ~result_typ ~fall_through ~context body x exn_handler =
16761677
block
16771678
{ params = []; result = result_typ }
16781679
(let* () =
1679-
try_
1680-
{ params = []; result = [] }
1681-
(body ~result_typ:[] ~fall_through:(`Block (-1)) ~context:(`Skip :: context))
1682-
[ ocaml_tag, store ~always:true x (return (W.Pop Value.value))
1683-
; ( js_tag
1684-
, let exn = Code.Var.fresh () in
1685-
let* () = store ~always:true ~typ:externref exn (return (W.Pop externref)) in
1686-
let* exn = load exn in
1687-
store ~always:true x (return (W.Call (f, [ exn ]))) )
1688-
]
1680+
store
1681+
x
1682+
(block_expr
1683+
{ params = []; result = [ Value.value ] }
1684+
(let* exn =
1685+
block_expr
1686+
{ params = []; result = [ externref ] }
1687+
(let* e =
1688+
try_expr
1689+
{ params = []; result = [ externref ] }
1690+
(body
1691+
~result_typ:[ externref ]
1692+
~fall_through:(`Block (-1))
1693+
~context:(`Skip :: `Skip :: `Skip :: context))
1694+
[ ocaml_tag, 1, Value.value; js_tag, 0, externref ]
1695+
in
1696+
instr (W.Push e))
1697+
in
1698+
instr (W.CallInstr (f, [ exn ]))))
16891699
in
16901700
exn_handler ~result_typ ~fall_through ~context)
16911701

compiler/lib/wasm/wa_initialize_locals.ml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ let rec scan_expression ctx e =
6868
scan_expression ctx cond;
6969
scan_expression (fork_context ctx) e1;
7070
scan_expression (fork_context ctx) e2
71+
| Try (_, body, _) -> scan_instructions ctx body
7172

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

@@ -92,10 +93,6 @@ and scan_instruction ctx i =
9293
scan_expression ctx e;
9394
scan_instructions ctx l;
9495
scan_instructions ctx l'
95-
| Try (_, body, catches, catch_all) ->
96-
scan_instructions ctx body;
97-
List.iter ~f:(fun (_, l) -> scan_instructions ctx l) catches;
98-
Option.iter ~f:(fun l -> scan_instructions ctx l) catch_all
9996
| CallInstr (_, l) | Return_call (_, l) -> scan_expressions ctx l
10097
| Br (_, None) | Return None | Rethrow _ | Nop -> ()
10198
| 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
@@ -39,12 +39,6 @@ let rec instruction ~tail i =
3939
| Wa_ast.Loop (ty, l) -> Wa_ast.Loop (ty, instructions ~tail l)
4040
| Block (ty, l) -> Block (ty, instructions ~tail l)
4141
| If (ty, e, l1, l2) -> If (ty, e, instructions ~tail l1, instructions ~tail l2)
42-
| Try (ty, l, catches, catch_all) ->
43-
Try
44-
( ty
45-
, l
46-
, List.map ~f:(fun (tag, l) -> tag, instructions ~tail l) catches
47-
, Option.map ~f:(fun l -> instructions ~tail l) catch_all )
4842
| Return (Some (Call (symb, l))) -> Return_call (symb, l)
4943
| Return (Some (Call_ref (ty, e, l))) -> Return_call_ref (ty, e, l)
5044
| Push (Call (symb, l)) when tail -> Return_call (symb, l)

compiler/lib/wasm/wa_wasm_output.ml

Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,18 @@ end = struct
628628
output_byte ch 0x05;
629629
output_expression st ch e3;
630630
output_byte ch 0x0B
631+
| Try (typ, l, catches) ->
632+
Feature.require exception_handling;
633+
output_byte ch 0x06;
634+
output_blocktype st.type_names ch typ;
635+
List.iter ~f:(fun i' -> output_instruction st ch i') l;
636+
List.iter
637+
~f:(fun (tag, l, ty) ->
638+
output_byte ch 0x07;
639+
output_uint ch (Hashtbl.find st.tag_names tag);
640+
output_instruction st ch (Br (l + 1, Some (Pop ty))))
641+
catches;
642+
output_byte ch 0X0B
631643

632644
and output_instruction st ch i =
633645
match i with
@@ -688,23 +700,6 @@ end = struct
688700
output_uint ch (Hashtbl.find st.func_names f)
689701
| Nop -> ()
690702
| Push e -> output_expression st ch e
691-
| Try (typ, l, catches, catchall) ->
692-
Feature.require exception_handling;
693-
output_byte ch 0x06;
694-
output_blocktype st.type_names ch typ;
695-
List.iter ~f:(fun i' -> output_instruction st ch i') l;
696-
List.iter
697-
~f:(fun (tag, l) ->
698-
output_byte ch 0x07;
699-
output_uint ch (Hashtbl.find st.tag_names tag);
700-
List.iter ~f:(fun i' -> output_instruction st ch i') l)
701-
catches;
702-
(match catchall with
703-
| Some l ->
704-
output_byte ch 0x05;
705-
List.iter ~f:(fun i' -> output_instruction st ch i') l
706-
| None -> ());
707-
output_byte ch 0X0B
708703
| Throw (tag, e) ->
709704
Feature.require exception_handling;
710705
output_expression st ch e;
@@ -881,6 +876,8 @@ end = struct
881876
~f:(fun set i -> expr_function_references i set)
882877
~init:(expr_function_references e' set)
883878
l
879+
| Try (_, l, _) ->
880+
List.fold_left ~f:(fun set i -> instr_function_references i set) ~init:set l
884881

885882
and instr_function_references i set =
886883
match i with
@@ -905,24 +902,6 @@ end = struct
905902
| Br (_, None) | Return None | Nop | Rethrow _ -> set
906903
| CallInstr (_, l) ->
907904
List.fold_left ~f:(fun set i -> expr_function_references i set) ~init:set l
908-
| Try (_, l, catches, catchall) ->
909-
List.fold_left ~f:(fun set i -> instr_function_references i set) ~init:set l
910-
|> (fun init ->
911-
List.fold_left
912-
~f:(fun set (_, l) ->
913-
List.fold_left
914-
~f:(fun set i -> instr_function_references i set)
915-
~init:set
916-
l)
917-
~init
918-
catches)
919-
|> fun init ->
920-
List.fold_left
921-
~f:(fun set i -> instr_function_references i set)
922-
~init
923-
(match catchall with
924-
| Some l -> l
925-
| None -> [])
926905
| ArraySet (_, e1, e2, e3) ->
927906
set
928907
|> expr_function_references e1

compiler/lib/wasm/wa_wat_output.ml

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,19 @@ let expression_or_instructions ctx st in_function =
428428
@ [ List (Atom "then" :: expression ift) ]
429429
@ [ List (Atom "else" :: expression iff) ])
430430
]
431+
| Try (ty, body, catches) ->
432+
[ List
433+
(Atom "try"
434+
:: (block_type st ty
435+
@ List (Atom "do" :: instructions body)
436+
:: List.map
437+
~f:(fun (tag, i, ty) ->
438+
List
439+
(Atom "catch"
440+
:: index st.tag_names tag
441+
:: instruction (Wa_ast.Br (i + 1, Some (Pop ty)))))
442+
catches))
443+
]
431444
and instruction i =
432445
match i with
433446
| Drop e -> [ List (Atom "drop" :: expression e) ]
@@ -446,20 +459,6 @@ let expression_or_instructions ctx st in_function =
446459
@ list ~always:true "then" instructions (remove_nops l1)
447460
@ list "else" instructions (remove_nops l2)))
448461
]
449-
| Try (ty, body, catches, catch_all) ->
450-
[ List
451-
(Atom "try"
452-
:: (block_type st ty
453-
@ List (Atom "do" :: instructions body)
454-
:: (List.map
455-
~f:(fun (tag, l) ->
456-
List (Atom "catch" :: index st.tag_names tag :: instructions l))
457-
catches
458-
@
459-
match catch_all with
460-
| None -> []
461-
| Some l -> [ List (Atom "catch_all" :: instructions l) ])))
462-
]
463462
| Br_table (e, l, i) ->
464463
[ List
465464
(Atom "br_table"

0 commit comments

Comments
 (0)