summaryrefslogtreecommitdiff
path: root/src/wasm
diff options
context:
space:
mode:
authorAlon Zakai (kripken) <alonzakai@gmail.com>2017-08-05 10:44:12 -0700
committerAlon Zakai (kripken) <alonzakai@gmail.com>2017-08-05 13:42:53 -0700
commit9149f4cdeee0949e5dab4b0e0e076c73b822cbfc (patch)
treefa221dd65215b50409c97f53332536091ad9cfdf /src/wasm
parentb9ce58696487fb4961070e322879b3d3e0cfc9d3 (diff)
downloadbinaryen-9149f4cdeee0949e5dab4b0e0e076c73b822cbfc.tar.gz
binaryen-9149f4cdeee0949e5dab4b0e0e076c73b822cbfc.tar.bz2
binaryen-9149f4cdeee0949e5dab4b0e0e076c73b822cbfc.zip
fix reading of stacky unreadable code with elements we need to drop
Diffstat (limited to 'src/wasm')
-rw-r--r--src/wasm/wasm-binary.cpp32
1 files changed, 16 insertions, 16 deletions
diff --git a/src/wasm/wasm-binary.cpp b/src/wasm/wasm-binary.cpp
index 47bcc8ba5..018e463e0 100644
--- a/src/wasm/wasm-binary.cpp
+++ b/src/wasm/wasm-binary.cpp
@@ -2073,6 +2073,20 @@ BinaryConsts::ASTNodes WasmBinaryBuilder::readExpression(Expression*& curr) {
return BinaryConsts::ASTNodes(code);
}
+void WasmBinaryBuilder::pushBlockElements(Block* curr, size_t start, size_t end) {
+ for (size_t i = start; i < end; i++) {
+ auto* item = expressionStack[i];
+ curr->list.push_back(item);
+ if (i < end - 1) {
+ // stacky&unreachable code may introduce elements that need to be dropped in non-final positions
+ if (isConcreteWasmType(item->type)) {
+ curr->list.back() = Builder(wasm).makeDrop(curr->list.back());
+ }
+ }
+ }
+ expressionStack.resize(start);
+}
+
void WasmBinaryBuilder::visitBlock(Block *curr) {
if (debug) std::cerr << "zz node: Block" << std::endl;
// special-case Block and de-recurse nested blocks in their first position, as that is
@@ -2108,18 +2122,7 @@ void WasmBinaryBuilder::visitBlock(Block *curr) {
if (end < start) {
throw ParseException("block cannot pop from outside");
}
- for (size_t i = start; i < end; i++) {
- if (debug) std::cerr << " " << size_t(expressionStack[i]) << "\n zz Block element " << curr->list.size() << std::endl;
- auto* item = expressionStack[i];
- curr->list.push_back(item);
- if (i < end - 1) {
- // stacky&unreachable code may introduce elements that need to be dropped in non-final positions
- if (isConcreteWasmType(item->type)) {
- curr->list.back() = Builder(wasm).makeDrop(curr->list.back());
- }
- }
- }
- expressionStack.resize(start);
+ pushBlockElements(curr, start, end);
curr->finalize(curr->type);
breakStack.pop_back();
}
@@ -2136,11 +2139,8 @@ Expression* WasmBinaryBuilder::getMaybeBlock(WasmType type) {
throw ParseException("block cannot pop from outside");
}
auto* block = allocator.alloc<Block>();
- for (size_t i = start; i < end; i++) {
- block->list.push_back(expressionStack[i]);
- }
+ pushBlockElements(block, start, end);
block->finalize(type);
- expressionStack.resize(start);
return block;
}