diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/wasm-binary.h | 29 |
1 files changed, 19 insertions, 10 deletions
diff --git a/src/wasm-binary.h b/src/wasm-binary.h index 5d0c41fba..ce7472b0f 100644 --- a/src/wasm-binary.h +++ b/src/wasm-binary.h @@ -640,21 +640,25 @@ public: breakStack.pop_back(); breakStack.pop_back(); } - void visitBreak(Break *curr) { - if (debug) std::cerr << "zz node: Break" << std::endl; - o << int8_t(curr->condition ? BinaryConsts::BrIf : BinaryConsts::Br); - bool found = false; + + int getBreakIndex(Name name) { // -1 if not found for (int i = breakStack.size() - 1; i >= 0; i--) { - if (breakStack[i] == curr->name) { - o << int8_t(breakStack.size() - 1 - i); - found = true; - break; + if (breakStack[i] == name) { + return breakStack.size() - 1 - i; } } - if (!found) { + return -1; + } + + void visitBreak(Break *curr) { + if (debug) std::cerr << "zz node: Break" << std::endl; + o << int8_t(curr->condition ? BinaryConsts::BrIf : BinaryConsts::Br); + int offset = getBreakIndex(curr->name); + if (offset < 0) { std::cerr << "bad break: " << curr->name << std::endl; abort(); } + o << int8_t(offset); if (curr->condition) recurse(curr->condition); if (curr->value) { recurse(curr->value); @@ -1328,10 +1332,15 @@ public: breakStack.pop_back(); breakStack.pop_back(); } + + Name getBreakName(int offset) { + return breakStack[breakStack.size() - 1 - offset]; + } + void visitBreak(Break *curr, uint8_t code) { if (debug) std::cerr << "zz node: Break" << std::endl; auto offset = getInt8(); - curr->name = breakStack[breakStack.size() - 1 - offset]; + curr->name = getBreakName(offset); if (code == BinaryConsts::BrIf) readExpression(curr->condition); readExpression(curr->value); } |