From e2f49d8227f2b71e4dede5cf4074bb9f65e3d77f Mon Sep 17 00:00:00 2001 From: Heejin Ahn Date: Tue, 13 Aug 2019 00:29:26 +0900 Subject: Add basic exception handling support (#2282) This adds basic support for exception handling instructions, according to the spec: https://github.com/WebAssembly/exception-handling/blob/master/proposals/Exceptions.md This PR includes support for: - Binary reading/writing - Wast reading/writing - Stack IR - Validation - binaryen.js + C API - Few IR routines: branch-utils, type-updating, etc - Few passes: just enough to make `wasm-opt -O` pass - Tests This PR does not include support for many optimization passes, fuzzer, or interpreter. They will be follow-up PRs. Try-catch construct is modeled in Binaryen IR in a similar manner to that of if-else: each of try body and catch body will contain a block, which can be omitted if there is only a single instruction. This block will not be emitted in wast or binary, as in if-else. As in if-else, `class Try` contains two expressions each for try body and catch body, and `catch` is not modeled as an instruction. `exnref` value pushed by `catch` is get by `pop` instruction. `br_on_exn` is special: it returns different types of values when taken and not taken. We make `exnref`, the type `br_on_exn` pushes if not taken, as `br_on_exn`'s type. --- src/wasm-builder.h | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) (limited to 'src/wasm-builder.h') diff --git a/src/wasm-builder.h b/src/wasm-builder.h index 923effb76..a0a43bc07 100644 --- a/src/wasm-builder.h +++ b/src/wasm-builder.h @@ -453,7 +453,6 @@ public: return ret; } Const* makeConst(Literal value) { - assert(isConcreteType(value.type)); auto* ret = allocator.alloc(); ret->value = value; ret->type = value.type; @@ -497,6 +496,53 @@ public: ret->finalize(); return ret; } + Try* makeTry(Expression* body, Expression* catchBody) { + auto* ret = allocator.alloc(); + ret->body = body; + ret->catchBody = catchBody; + ret->finalize(); + return ret; + } + Try* makeTry(Expression* body, Expression* catchBody, Type type) { + auto* ret = allocator.alloc(); + ret->body = body; + ret->catchBody = catchBody; + ret->finalize(type); + return ret; + } + Throw* makeThrow(Event* event, const std::vector& args) { + return makeThrow(event->name, args); + } + Throw* makeThrow(Name event, const std::vector& args) { + auto* ret = allocator.alloc(); + ret->event = event; + ret->operands.set(args); + ret->finalize(); + return ret; + } + Rethrow* makeRethrow(Expression* exnref) { + auto* ret = allocator.alloc(); + ret->exnref = exnref; + ret->finalize(); + return ret; + } + BrOnExn* makeBrOnExn(Name name, Event* event, Expression* exnref) { + return makeBrOnExn(name, event->name, exnref, event->params); + } + BrOnExn* makeBrOnExn(Name name, + Name event, + Expression* exnref, + std::vector& eventParams) { + auto* ret = allocator.alloc(); + ret->name = name; + ret->event = event; + ret->exnref = exnref; + // Copy params info into BrOnExn, because it is necessary when BrOnExn is + // refinalized without the module. + ret->eventParams = eventParams; + ret->finalize(); + return ret; + } Unreachable* makeUnreachable() { return allocator.alloc(); } Push* makePush(Expression* value) { auto* ret = allocator.alloc(); -- cgit v1.2.3