Skip to content

Commit a0b7abe

Browse files
authored
binary/wat: Implement EHv4 (#2470)
This pull request implements EHv4. Binary is mostly untested until interp is working.
1 parent 958d0a7 commit a0b7abe

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+2014
-1324
lines changed

include/wabt/binary-reader-logging.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,10 @@ class BinaryReaderLogging : public BinaryReaderDelegate {
232232
Address alignment_log2,
233233
Address offset) override;
234234
Result OnThrowExpr(Index tag_index) override;
235+
Result OnThrowRefExpr() override;
235236
Result OnTryExpr(Type sig_type) override;
237+
Result OnTryTableExpr(Type sig_type,
238+
const CatchClauseVector& catches) override;
236239
Result OnUnaryExpr(Opcode opcode) override;
237240
Result OnTernaryExpr(Opcode opcode) override;
238241
Result OnUnreachableExpr() override;

include/wabt/binary-reader-nop.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,12 @@ class BinaryReaderNop : public BinaryReaderDelegate {
319319
return Result::Ok;
320320
}
321321
Result OnThrowExpr(Index depth) override { return Result::Ok; }
322+
Result OnThrowRefExpr() override { return Result::Ok; }
322323
Result OnTryExpr(Type sig_type) override { return Result::Ok; }
324+
Result OnTryTableExpr(Type sig_type,
325+
const CatchClauseVector& catches) override {
326+
return Result::Ok;
327+
}
323328
Result OnUnaryExpr(Opcode opcode) override { return Result::Ok; }
324329
Result OnTernaryExpr(Opcode opcode) override { return Result::Ok; }
325330
Result OnUnreachableExpr() override { return Result::Ok; }

include/wabt/binary-reader.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ struct TypeMut {
5959
};
6060
using TypeMutVector = std::vector<TypeMut>;
6161

62+
struct CatchClause {
63+
CatchKind kind;
64+
Index tag;
65+
Index depth;
66+
};
67+
using CatchClauseVector = std::vector<CatchClause>;
68+
6269
class BinaryReaderDelegate {
6370
public:
6471
struct State {
@@ -302,7 +309,10 @@ class BinaryReaderDelegate {
302309
Address alignment_log2,
303310
Address offset) = 0;
304311
virtual Result OnThrowExpr(Index tag_index) = 0;
312+
virtual Result OnThrowRefExpr() = 0;
305313
virtual Result OnTryExpr(Type sig_type) = 0;
314+
virtual Result OnTryTableExpr(Type sig_type,
315+
const CatchClauseVector& catches) = 0;
306316

307317
virtual Result OnUnaryExpr(Opcode opcode) = 0;
308318
virtual Result OnTernaryExpr(Opcode opcode) = 0;

include/wabt/common.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ enum class LabelType {
194194
If,
195195
Else,
196196
Try,
197+
TryTable,
197198
Catch,
198199

199200
First = Func,
@@ -239,6 +240,13 @@ enum class SegmentKind {
239240
Declared,
240241
};
241242

243+
enum class CatchKind {
244+
Catch,
245+
CatchRef,
246+
CatchAll,
247+
CatchAllRef,
248+
};
249+
242250
// Used in test asserts for special expected values "nan:canonical" and
243251
// "nan:arithmetic"
244252
enum class ExpectedNan {

include/wabt/expr-visitor.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class ExprVisitor {
4141
IfFalse,
4242
Loop,
4343
Try,
44+
TryTable,
4445
Catch,
4546
};
4647

@@ -73,6 +74,8 @@ class ExprVisitor::Delegate {
7374
virtual Result OnBrExpr(BrExpr*) = 0;
7475
virtual Result OnBrIfExpr(BrIfExpr*) = 0;
7576
virtual Result OnBrTableExpr(BrTableExpr*) = 0;
77+
virtual Result BeginTryTableExpr(TryTableExpr*) = 0;
78+
virtual Result EndTryTableExpr(TryTableExpr*) = 0;
7679
virtual Result OnCallExpr(CallExpr*) = 0;
7780
virtual Result OnCallIndirectExpr(CallIndirectExpr*) = 0;
7881
virtual Result OnCallRefExpr(CallRefExpr*) = 0;
@@ -122,6 +125,7 @@ class ExprVisitor::Delegate {
122125
virtual Result OnDelegateExpr(TryExpr*) = 0;
123126
virtual Result EndTryExpr(TryExpr*) = 0;
124127
virtual Result OnThrowExpr(ThrowExpr*) = 0;
128+
virtual Result OnThrowRefExpr(ThrowRefExpr*) = 0;
125129
virtual Result OnRethrowExpr(RethrowExpr*) = 0;
126130
virtual Result OnAtomicWaitExpr(AtomicWaitExpr*) = 0;
127131
virtual Result OnAtomicFenceExpr(AtomicFenceExpr*) = 0;
@@ -147,6 +151,8 @@ class ExprVisitor::DelegateNop : public ExprVisitor::Delegate {
147151
Result OnBrExpr(BrExpr*) override { return Result::Ok; }
148152
Result OnBrIfExpr(BrIfExpr*) override { return Result::Ok; }
149153
Result OnBrTableExpr(BrTableExpr*) override { return Result::Ok; }
154+
Result BeginTryTableExpr(TryTableExpr*) override { return Result::Ok; }
155+
Result EndTryTableExpr(TryTableExpr*) override { return Result::Ok; }
150156
Result OnCallExpr(CallExpr*) override { return Result::Ok; }
151157
Result OnCallIndirectExpr(CallIndirectExpr*) override { return Result::Ok; }
152158
Result OnCallRefExpr(CallRefExpr*) override { return Result::Ok; }
@@ -198,6 +204,7 @@ class ExprVisitor::DelegateNop : public ExprVisitor::Delegate {
198204
Result OnDelegateExpr(TryExpr*) override { return Result::Ok; }
199205
Result EndTryExpr(TryExpr*) override { return Result::Ok; }
200206
Result OnThrowExpr(ThrowExpr*) override { return Result::Ok; }
207+
Result OnThrowRefExpr(ThrowRefExpr*) override { return Result::Ok; }
201208
Result OnRethrowExpr(RethrowExpr*) override { return Result::Ok; }
202209
Result OnAtomicWaitExpr(AtomicWaitExpr*) override { return Result::Ok; }
203210
Result OnAtomicFenceExpr(AtomicFenceExpr*) override { return Result::Ok; }

include/wabt/ir.h

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
#include <cstddef>
2222
#include <cstdint>
2323
#include <memory>
24+
#include <set>
2425
#include <string>
2526
#include <string_view>
2627
#include <type_traits>
2728
#include <vector>
28-
#include <set>
2929

3030
#include "wabt/binding-hash.h"
3131
#include "wabt/common.h"
@@ -422,7 +422,9 @@ enum class ExprType {
422422
TableFill,
423423
Ternary,
424424
Throw,
425+
ThrowRef,
425426
Try,
427+
TryTable,
426428
Unary,
427429
Unreachable,
428430

@@ -460,6 +462,21 @@ struct Catch {
460462
};
461463
using CatchVector = std::vector<Catch>;
462464

465+
struct TableCatch {
466+
explicit TableCatch(const Location& loc = Location()) : loc(loc) {}
467+
Location loc;
468+
Var tag;
469+
Var target;
470+
CatchKind kind;
471+
bool IsCatchAll() const {
472+
return kind == CatchKind::CatchAll || kind == CatchKind::CatchAllRef;
473+
}
474+
bool IsRef() const {
475+
return kind == CatchKind::CatchRef || kind == CatchKind::CatchAllRef;
476+
}
477+
};
478+
using TryTableVector = std::vector<TableCatch>;
479+
463480
enum class TryKind { Plain, Catch, Delegate };
464481

465482
class Expr : public intrusive_list_base<Expr> {
@@ -516,6 +533,7 @@ using DropExpr = ExprMixin<ExprType::Drop>;
516533
using NopExpr = ExprMixin<ExprType::Nop>;
517534
using ReturnExpr = ExprMixin<ExprType::Return>;
518535
using UnreachableExpr = ExprMixin<ExprType::Unreachable>;
536+
using ThrowRefExpr = ExprMixin<ExprType::ThrowRef>;
519537

520538
using MemoryGrowExpr = MemoryExpr<ExprType::MemoryGrow>;
521539
using MemorySizeExpr = MemoryExpr<ExprType::MemorySize>;
@@ -743,6 +761,15 @@ class IfExpr : public ExprMixin<ExprType::If> {
743761
Location false_end_loc;
744762
};
745763

764+
class TryTableExpr : public ExprMixin<ExprType::TryTable> {
765+
public:
766+
explicit TryTableExpr(const Location& loc = Location())
767+
: ExprMixin<ExprType::TryTable>(loc) {}
768+
769+
Block block;
770+
TryTableVector catches;
771+
};
772+
746773
class TryExpr : public ExprMixin<ExprType::Try> {
747774
public:
748775
explicit TryExpr(const Location& loc = Location())

include/wabt/opcode.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ WABT_OPCODE(___, ___, ___, ___, 0, 0, 0x06, Try, "try", "")
4545
WABT_OPCODE(___, ___, ___, ___, 0, 0, 0x07, Catch, "catch", "")
4646
WABT_OPCODE(___, ___, ___, ___, 0, 0, 0x08, Throw, "throw", "")
4747
WABT_OPCODE(___, ___, ___, ___, 0, 0, 0x09, Rethrow, "rethrow", "")
48+
WABT_OPCODE(___, ___, ___, ___, 0, 0, 0x0a, ThrowRef, "throw_ref", "")
4849
WABT_OPCODE(___, ___, ___, ___, 0, 0, 0x0b, End, "end", "")
4950
WABT_OPCODE(___, ___, ___, ___, 0, 0, 0x0c, Br, "br", "")
5051
WABT_OPCODE(___, I32, ___, ___, 0, 0, 0x0d, BrIf, "br_if", "")
@@ -60,6 +61,7 @@ WABT_OPCODE(___, ___, ___, ___, 0, 0, 0x19, CatchAll, "catch_all", "")
6061
WABT_OPCODE(___, ___, ___, ___, 0, 0, 0x1a, Drop, "drop", "")
6162
WABT_OPCODE(___, ___, ___, I32, 0, 0, 0x1b, Select, "select", "")
6263
WABT_OPCODE(___, ___, ___, I32, 0, 0, 0x1c, SelectT, "select", "")
64+
WABT_OPCODE(___, ___, ___, ___, 0, 0, 0x1f, TryTable, "try_table", "")
6365
WABT_OPCODE(___, ___, ___, ___, 0, 0, 0x20, LocalGet, "local.get", "")
6466
WABT_OPCODE(___, ___, ___, ___, 0, 0, 0x21, LocalSet, "local.set", "")
6567
WABT_OPCODE(___, ___, ___, ___, 0, 0, 0x22, LocalTee, "local.tee", "")

include/wabt/shared-validator.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,11 @@ class SharedValidator {
211211
Result OnTableSize(const Location&, Var table_var);
212212
Result OnTernary(const Location&, Opcode);
213213
Result OnThrow(const Location&, Var tag_var);
214+
Result OnThrowRef(const Location&);
214215
Result OnTry(const Location&, Type sig_type);
216+
Result BeginTryTable(const Location&, Type sig_type);
217+
Result OnTryTableCatch(const Location&, const TableCatch&);
218+
Result EndTryTable(const Location&, Type sig_type);
215219
Result OnUnary(const Location&, Opcode);
216220
Result OnUnreachable(const Location&);
217221

include/wabt/token.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ WABT_TOKEN(CallIndirect, "call_indirect")
105105
WABT_TOKEN(CallRef, "call_ref")
106106
WABT_TOKEN(Catch, "catch")
107107
WABT_TOKEN(CatchAll, "catch_all")
108+
WABT_TOKEN(CatchRef, "catch_ref")
109+
WABT_TOKEN(CatchAllRef, "catch_all_ref")
108110
WABT_TOKEN(Compare, "COMPARE")
109111
WABT_TOKEN(Const, "CONST")
110112
WABT_TOKEN(Convert, "CONVERT")
@@ -151,7 +153,9 @@ WABT_TOKEN(TableSet, "table.set")
151153
WABT_TOKEN(TableSize, "table.size")
152154
WABT_TOKEN(Ternary, "TERNARY")
153155
WABT_TOKEN(Throw, "throw")
156+
WABT_TOKEN(ThrowRef, "throw_ref")
154157
WABT_TOKEN(Try, "try")
158+
WABT_TOKEN(TryTable, "try_table")
155159
WABT_TOKEN(Unary, "UNARY")
156160
WABT_TOKEN(Unreachable, "unreachable")
157161
WABT_TOKEN_FIRST(Opcode, AtomicFence)

include/wabt/type-checker.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,12 @@ class TypeChecker {
128128
Result OnStore(Opcode, const Limits& limits);
129129
Result OnTernary(Opcode);
130130
Result OnThrow(const TypeVector& sig);
131+
Result OnThrowRef();
131132
Result OnTry(const TypeVector& param_types, const TypeVector& result_types);
133+
Result OnTryTableCatch(const TypeVector& sig, Index);
134+
Result BeginTryTable(const TypeVector& param_types);
135+
Result EndTryTable(const TypeVector& param_types,
136+
const TypeVector& result_types);
132137
Result OnUnary(Opcode);
133138
Result OnUnreachable();
134139
Result EndFunction();

0 commit comments

Comments
 (0)