Skip to content

Commit 1ae0170

Browse files
committed
Allow plain try with no catch, etc.
Matches recent changes in the exception handling spec that allowed this case to reduce special cases in the syntax: WebAssembly/exception-handling#157
1 parent 9500425 commit 1ae0170

File tree

10 files changed

+83
-74
lines changed

10 files changed

+83
-74
lines changed

src/binary-reader-ir.cc

+3-3
Original file line numberDiff line numberDiff line change
@@ -1012,7 +1012,7 @@ Result BinaryReaderIR::AppendCatch(Catch&& catch_) {
10121012
return Result::Error;
10131013
}
10141014

1015-
if (try_->kind == TryKind::Invalid) {
1015+
if (try_->kind == TryKind::Plain) {
10161016
try_->kind = TryKind::Catch;
10171017
} else if (try_->kind != TryKind::Catch) {
10181018
PrintError("catch not allowed in try-unwind or try-delegate");
@@ -1043,7 +1043,7 @@ Result BinaryReaderIR::OnUnwindExpr() {
10431043

10441044
auto* try_ = cast<TryExpr>(label->context);
10451045

1046-
if (try_->kind == TryKind::Invalid) {
1046+
if (try_->kind == TryKind::Plain) {
10471047
try_->kind = TryKind::Unwind;
10481048
} else if (try_->kind != TryKind::Unwind) {
10491049
PrintError("unwind not allowed in try-catch or try-delegate");
@@ -1065,7 +1065,7 @@ Result BinaryReaderIR::OnDelegateExpr(Index depth) {
10651065

10661066
auto* try_ = cast<TryExpr>(label->context);
10671067

1068-
if (try_->kind == TryKind::Invalid) {
1068+
if (try_->kind == TryKind::Plain) {
10691069
try_->kind = TryKind::Delegate;
10701070
} else if (try_->kind != TryKind::Delegate) {
10711071
PrintError("delegate not allowed in try-catch or try-unwind");

src/binary-writer.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -1017,8 +1017,8 @@ void BinaryWriter::WriteExpr(const Func* func, const Expr* expr) {
10171017
GetLabelVarDepth(&try_expr->delegate_target),
10181018
"delegate depth");
10191019
break;
1020-
case TryKind::Invalid:
1021-
// Should not occur.
1020+
case TryKind::Plain:
1021+
WriteOpcode(stream_, Opcode::End);
10221022
break;
10231023
}
10241024
break;

src/expr-visitor.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ Result ExprVisitor::VisitExpr(Expr* root_expr) {
114114
case TryKind::Delegate:
115115
CHECK_RESULT(delegate_->OnDelegateExpr(try_expr));
116116
break;
117-
case TryKind::Invalid:
118-
// Should not happen.
117+
case TryKind::Plain:
118+
CHECK_RESULT(delegate_->EndTryExpr(try_expr));
119119
break;
120120
}
121121
}

src/ir.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ struct Catch {
378378
typedef std::vector<Catch> CatchVector;
379379

380380
enum class TryKind {
381-
Invalid,
381+
Plain,
382382
Catch,
383383
Unwind,
384384
Delegate
@@ -604,7 +604,7 @@ class IfExpr : public ExprMixin<ExprType::If> {
604604
class TryExpr : public ExprMixin<ExprType::Try> {
605605
public:
606606
explicit TryExpr(const Location& loc = Location())
607-
: ExprMixin<ExprType::Try>(loc), kind(TryKind::Invalid) {}
607+
: ExprMixin<ExprType::Try>(loc), kind(TryKind::Plain) {}
608608

609609
TryKind kind;
610610
Block block;

src/wast-parser.cc

+26-27
Original file line numberDiff line numberDiff line change
@@ -2601,8 +2601,6 @@ Result WastParser::ParseBlockInstr(std::unique_ptr<Expr>* out_expr) {
26012601
CHECK_RESULT(ParseVar(&var));
26022602
expr->delegate_target = var;
26032603
expr->kind = TryKind::Delegate;
2604-
} else {
2605-
return ErrorExpected({"catch", "catch_all", "unwind", "delegate"});
26062604
}
26072605
CHECK_RESULT(ErrorIfLpar({"a valid try clause"}));
26082606
expr->block.end_loc = GetLocation();
@@ -2771,32 +2769,33 @@ Result WastParser::ParseExpr(ExprList* exprs) {
27712769
EXPECT(Do);
27722770
CHECK_RESULT(ParseInstrList(&expr->block.exprs));
27732771
EXPECT(Rpar);
2774-
EXPECT(Lpar);
2775-
TokenType type = Peek();
2776-
switch (type) {
2777-
case TokenType::Catch:
2778-
case TokenType::CatchAll:
2779-
CHECK_RESULT(ParseCatchExprList(&expr->catches));
2780-
expr->kind = TryKind::Catch;
2781-
break;
2782-
case TokenType::Unwind:
2783-
Consume();
2784-
CHECK_RESULT(ParseTerminatingInstrList(&expr->unwind));
2785-
expr->kind = TryKind::Unwind;
2786-
EXPECT(Rpar);
2787-
break;
2788-
case TokenType::Delegate: {
2789-
Consume();
2790-
Var var;
2791-
CHECK_RESULT(ParseVar(&var));
2792-
expr->delegate_target = var;
2793-
expr->kind = TryKind::Delegate;
2794-
EXPECT(Rpar);
2795-
break;
2772+
if (PeekMatch(TokenType::Lpar)) {
2773+
Consume();
2774+
TokenType type = Peek();
2775+
switch (type) {
2776+
case TokenType::Catch:
2777+
case TokenType::CatchAll:
2778+
CHECK_RESULT(ParseCatchExprList(&expr->catches));
2779+
expr->kind = TryKind::Catch;
2780+
break;
2781+
case TokenType::Unwind:
2782+
Consume();
2783+
CHECK_RESULT(ParseTerminatingInstrList(&expr->unwind));
2784+
expr->kind = TryKind::Unwind;
2785+
EXPECT(Rpar);
2786+
break;
2787+
case TokenType::Delegate: {
2788+
Consume();
2789+
Var var;
2790+
CHECK_RESULT(ParseVar(&var));
2791+
expr->delegate_target = var;
2792+
expr->kind = TryKind::Delegate;
2793+
EXPECT(Rpar);
2794+
break;
2795+
}
2796+
default:
2797+
break;
27962798
}
2797-
default:
2798-
ErrorExpected({"catch", "catch_all", "unwind", "delegate"});
2799-
break;
28002799
}
28012800
CHECK_RESULT(ErrorIfLpar({"a valid try clause"}));
28022801
expr->block.end_loc = GetLocation();

src/wat-writer.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -1187,8 +1187,8 @@ void WatWriter::FlushExprTree(const ExprTree& expr_tree) {
11871187
WriteVar(try_expr->delegate_target, NextChar::None);
11881188
WritePuts(")", NextChar::Newline);
11891189
break;
1190-
case TryKind::Invalid:
1191-
// Should not occur.
1190+
case TryKind::Plain:
1191+
// Nothing to do.
11921192
break;
11931193
}
11941194
WriteCloseNewline();

test/dump/try.txt

+32-22
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
(module
44
(tag $e (param i32))
55
(func
6+
try
7+
nop
8+
end
69
try $try1 (result i32)
710
nop
811
i32.const 7
@@ -55,22 +58,26 @@
5558
0000021: 00 ; func body size (guess)
5659
0000022: 00 ; local decl count
5760
0000023: 06 ; try
58-
0000024: 7f ; i32
61+
0000024: 40 ; void
5962
0000025: 01 ; nop
60-
0000026: 41 ; i32.const
61-
0000027: 07 ; i32 literal
62-
0000028: 07 ; catch
63-
0000029: 00 ; catch tag
64-
000002a: 1a ; drop
65-
000002b: 41 ; i32.const
66-
000002c: 08 ; i32 literal
67-
000002d: 0b ; end
63+
0000026: 0b ; end
64+
0000027: 06 ; try
65+
0000028: 7f ; i32
66+
0000029: 01 ; nop
67+
000002a: 41 ; i32.const
68+
000002b: 07 ; i32 literal
69+
000002c: 07 ; catch
70+
000002d: 00 ; catch tag
6871
000002e: 1a ; drop
69-
000002f: 0b ; end
70-
0000021: 0e ; FIXUP func body size
71-
000001f: 10 ; FIXUP section size
72-
; move data: [1e, 30) -> [1b, 2d)
73-
; truncate to 45 (0x2d)
72+
000002f: 41 ; i32.const
73+
0000030: 08 ; i32 literal
74+
0000031: 0b ; end
75+
0000032: 1a ; drop
76+
0000033: 0b ; end
77+
0000021: 12 ; FIXUP func body size
78+
000001f: 14 ; FIXUP section size
79+
; move data: [1e, 34) -> [1b, 31)
80+
; truncate to 49 (0x31)
7481
;;; STDERR ;;)
7582
(;; STDOUT ;;;
7683

@@ -79,13 +86,16 @@ try.wasm: file format wasm 0x1
7986
Code Disassembly:
8087

8188
00001f func[0]:
82-
000020: 06 7f | try i32
89+
000020: 06 40 | try
8390
000022: 01 | nop
84-
000023: 41 07 | i32.const 7
85-
000025: 07 00 | catch 0
86-
000027: 1a | drop
87-
000028: 41 08 | i32.const 8
88-
00002a: 0b | end
89-
00002b: 1a | drop
90-
00002c: 0b | end
91+
000023: 0b | end
92+
000024: 06 7f | try i32
93+
000026: 01 | nop
94+
000027: 41 07 | i32.const 7
95+
000029: 07 00 | catch 0
96+
00002b: 1a | drop
97+
00002c: 41 08 | i32.const 8
98+
00002e: 0b | end
99+
00002f: 1a | drop
100+
000030: 0b | end
91101
;;; STDOUT ;;)

test/parse/expr/bad-try-no-catch.txt

-14
This file was deleted.

test/parse/expr/try.txt

+6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
(module
44
(tag (param i32))
55

6+
;; no catch
7+
(func
8+
try
9+
nop
10+
end)
11+
612
;; bare
713
(func
814
try

test/roundtrip/fold-try.txt

+8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
;;; ARGS: --stdout --fold-exprs --enable-exceptions --debug-names
33
(module
44
(func (result i32)
5+
try (result i32)
6+
i32.const 6
7+
end
8+
drop
59
try (result i32)
610
nop
711
i32.const 7
@@ -14,6 +18,10 @@
1418
(module
1519
(type (;0;) (func (result i32)))
1620
(func (;0;) (type 0) (result i32)
21+
(drop
22+
(try (result i32) ;; label = @1
23+
(do
24+
(i32.const 6))))
1725
(try (result i32) ;; label = @1
1826
(do
1927
(nop)

0 commit comments

Comments
 (0)