diff options
author | Heejin Ahn <aheejin@gmail.com> | 2021-01-15 18:48:00 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-01-15 18:48:00 +0900 |
commit | beccdf70258cd99ea25f10af13103e14dc243ffa (patch) | |
tree | 1081d7d350fbab7f901b917f2f082c8d351c3157 /src/wasm/wasm-stack.cpp | |
parent | f18c18e01d03d6d293fe3d701408855bbcea58bd (diff) | |
download | binaryen-beccdf70258cd99ea25f10af13103e14dc243ffa.tar.gz binaryen-beccdf70258cd99ea25f10af13103e14dc243ffa.tar.bz2 binaryen-beccdf70258cd99ea25f10af13103e14dc243ffa.zip |
Basic EH instrucion support for the new spec (#3487)
This updates `try`-`catch`-`catch_all` and `rethrow` instructions to
match the new spec. `delegate` is not included. Now `Try` contains not a
single `catchBody` expression but a vector of catch
bodies and events.
This updates most existing routines, optimizations, and tests modulo the
interpreter and the CFG traversal. Because the interpreter has not been
updated yet, the EH spec test is temporarily disabled in check.py. Also,
because the CFG traversal for EH is not yet updated, several EH tests in
`rse_all-features.wast`, which uses CFG traversal, are temporarily
commented out.
Also added a few more tests in existing EH test functions in
test/passes. In the previous spec, `catch` was catching all exceptions
so it was assumed that anything `try` body throws is caught by its
`catch`, but now we can assume the same only if there is a `catch_all`.
Newly added tests test cases when there is a `catch_all` and cases there
are only `catch`es separately.
Diffstat (limited to 'src/wasm/wasm-stack.cpp')
-rw-r--r-- | src/wasm/wasm-stack.cpp | 32 |
1 files changed, 24 insertions, 8 deletions
diff --git a/src/wasm/wasm-stack.cpp b/src/wasm/wasm-stack.cpp index 049573fc5..c96ceabb6 100644 --- a/src/wasm/wasm-stack.cpp +++ b/src/wasm/wasm-stack.cpp @@ -1859,14 +1859,24 @@ void BinaryInstWriter::visitTry(Try* curr) { emitResultType(curr->type); } -void BinaryInstWriter::emitCatch(Try* curr) { +void BinaryInstWriter::emitCatch(Try* curr, Index i) { assert(!breakStack.empty()); breakStack.pop_back(); breakStack.emplace_back(IMPOSSIBLE_CONTINUE); + // TODO Fix handling of BinaryLocations for the new EH spec if (func && !sourceMap) { parent.writeExtraDebugLocation(curr, func, BinaryLocations::Catch); } - o << int8_t(BinaryConsts::Catch); + o << int8_t(BinaryConsts::Catch) + << U32LEB(parent.getEventIndex(curr->catchEvents[i])); +} + +void BinaryInstWriter::emitCatchAll(Try* curr) { + assert(!breakStack.empty()); + breakStack.pop_back(); + breakStack.emplace_back(IMPOSSIBLE_CONTINUE); + // TODO Fix handling of BinaryLocations for the new EH spec + o << int8_t(BinaryConsts::CatchAll); } void BinaryInstWriter::visitThrow(Throw* curr) { @@ -1874,7 +1884,7 @@ void BinaryInstWriter::visitThrow(Throw* curr) { } void BinaryInstWriter::visitRethrow(Rethrow* curr) { - o << int8_t(BinaryConsts::Rethrow); + o << int8_t(BinaryConsts::Rethrow) << U32LEB(curr->depth); } void BinaryInstWriter::visitBrOnExn(BrOnExn* curr) { @@ -2200,23 +2210,29 @@ StackInst* StackIRGenerator::makeStackInst(StackInst::Op op, void StackIRToBinaryWriter::write() { writer.mapLocalsAndEmitHeader(); + // Stack to track indices of catches within a try + SmallVector<Index, 4> catchIndexStack; for (auto* inst : *func->stackIR) { if (!inst) { continue; // a nullptr is just something we can skip } switch (inst->op) { + case StackInst::TryBegin: + catchIndexStack.push_back(0); + // fallthrough case StackInst::Basic: case StackInst::BlockBegin: case StackInst::IfBegin: - case StackInst::LoopBegin: - case StackInst::TryBegin: { + case StackInst::LoopBegin: { writer.visit(inst->origin); break; } + case StackInst::TryEnd: + catchIndexStack.pop_back(); + // fallthrough case StackInst::BlockEnd: case StackInst::IfEnd: - case StackInst::LoopEnd: - case StackInst::TryEnd: { + case StackInst::LoopEnd: { writer.emitScopeEnd(inst->origin); break; } @@ -2225,7 +2241,7 @@ void StackIRToBinaryWriter::write() { break; } case StackInst::Catch: { - writer.emitCatch(inst->origin->cast<Try>()); + writer.emitCatch(inst->origin->cast<Try>(), catchIndexStack.back()++); break; } default: |