Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/wasm/wasm-binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2173,17 +2173,36 @@ BinaryConsts::ASTNodes WasmBinaryBuilder::readExpression(Expression*& curr) {
}

void WasmBinaryBuilder::pushBlockElements(Block* curr, size_t start, size_t end) {
// the first dropped element may be consumed by code later - it was on the stack first,
// and is the only thing left on the stack. there must be just one thing on the stack
// since we are at the end of a block context. note that we may need to drop more than
// one thing, since a bunch of concrete values may be all "consumed" by an unreachable
// (in which case, the first value can't be consumed anyhow, so it doesn't matter)
const Index NONE = -1;
Index consumable = NONE;
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());
curr->list.back() = Builder(wasm).makeDrop(item);
if (consumable == NONE) {
// this is the first, and hence consumable value. note the location
consumable = curr->list.size() - 1;
}
}
}
}
expressionStack.resize(start);
// if we have a consumable item and need it, use it
if (consumable != NONE && curr->list.back()->type == none) {
Builder builder(wasm);
auto* item = curr->list[consumable]->cast<Drop>()->value;
auto temp = builder.addVar(currFunction, item->type);
curr->list[consumable] = builder.makeSetLocal(temp, item);
curr->list.push_back(builder.makeGetLocal(temp, item->type));
}
}

void WasmBinaryBuilder::visitBlock(Block *curr) {
Expand Down
Binary file added test/consume-stacky.wasm
Binary file not shown.
16 changes: 16 additions & 0 deletions test/consume-stacky.wasm.fromBinary
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
(module
(type $0 (func (result i32)))
(memory $0 1 1)
(func $0 (; 0 ;) (type $0) (result i32)
(local $0 i32)
(set_local $0
(i32.const 1)
)
(i32.store
(i32.const 2)
(i32.const 3)
)
(get_local $0)
)
)