summaryrefslogtreecommitdiff
path: root/src/wasm-binary.h
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2016-01-29 17:44:33 -0800
committerAlon Zakai <alonzakai@gmail.com>2016-01-29 17:44:33 -0800
commitd569cf1db86179981d27145c6e126247d4d89ae3 (patch)
tree9c0c785fa4856073dce581d6d4b7f7c7b794e5e6 /src/wasm-binary.h
parent9ec15cf362ac5df76e2021282489ab414dfd6fd6 (diff)
downloadbinaryen-d569cf1db86179981d27145c6e126247d4d89ae3.tar.gz
binaryen-d569cf1db86179981d27145c6e126247d4d89ae3.tar.bz2
binaryen-d569cf1db86179981d27145c6e126247d4d89ae3.zip
refactor binary format break code
Diffstat (limited to 'src/wasm-binary.h')
-rw-r--r--src/wasm-binary.h29
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);
}