Skip to content

Commit 13ec76d

Browse files
authored
Fix reading breaks to the function exit (#1304)
* remove unneeded code to handle a br to the return from the function. Now that we use getBlockOrSingleton there, it does that for us anyhow
1 parent 4da2ad6 commit 13ec76d

File tree

4 files changed

+15
-18
lines changed

4 files changed

+15
-18
lines changed

src/wasm-binary.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -864,7 +864,6 @@ class WasmBinaryBuilder {
864864
};
865865
std::vector<BreakTarget> breakStack;
866866
std::unordered_set<Name> breakTargetNames;
867-
bool breaksToReturn; // whether a break is done to the function scope, which is in effect a return
868867

869868
std::vector<Expression*> expressionStack;
870869

src/wasm/wasm-binary.cpp

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,8 +1204,6 @@ void WasmBinaryWriter::visitDrop(Drop *curr) {
12041204

12051205
// reader
12061206

1207-
static Name RETURN_BREAK("binaryen|break-to-return");
1208-
12091207
void WasmBinaryBuilder::read() {
12101208

12111209
readHeader();
@@ -1626,28 +1624,21 @@ void WasmBinaryBuilder::readFunctions() {
16261624
if (debug) std::cerr << "processing function: " << i << std::endl;
16271625
nextLabel = 0;
16281626
useDebugLocation = false;
1629-
breaksToReturn = false;
16301627
// process body
16311628
assert(breakTargetNames.size() == 0);
16321629
assert(breakStack.empty());
1633-
breakStack.emplace_back(RETURN_BREAK, func->result != none); // the break target for the function scope
16341630
assert(expressionStack.empty());
16351631
assert(depth == 0);
16361632
func->body = getBlockOrSingleton(func->result);
16371633
assert(depth == 0);
1638-
assert(breakStack.size() == 1);
1639-
breakStack.pop_back();
1634+
assert(breakStack.size() == 0);
16401635
assert(breakTargetNames.size() == 0);
16411636
if (!expressionStack.empty()) {
16421637
throw ParseException("stack not empty on function exit");
16431638
}
16441639
if (pos != endOfFunction) {
16451640
throw ParseException("binary offset at function exit not at expected location");
16461641
}
1647-
if (breaksToReturn) {
1648-
// we broke to return, so we need an outer block to break to
1649-
func->body = Builder(wasm).blockifyWithName(func->body, RETURN_BREAK);
1650-
}
16511642
}
16521643
currFunction = nullptr;
16531644
functions.push_back(func);
@@ -2325,15 +2316,12 @@ void WasmBinaryBuilder::visitLoop(Loop *curr) {
23252316

23262317
WasmBinaryBuilder::BreakTarget WasmBinaryBuilder::getBreakTarget(int32_t offset) {
23272318
if (debug) std::cerr << "getBreakTarget " << offset << std::endl;
2319+
if (breakStack.size() < 1 + size_t(offset)) {
2320+
throw ParseException("bad breakindex (low)");
2321+
}
23282322
size_t index = breakStack.size() - 1 - offset;
23292323
if (index >= breakStack.size()) {
2330-
throw ParseException("bad breakindex");
2331-
}
2332-
if (index == 0) {
2333-
// trying to access the topmost element means we break out
2334-
// to the function scope, doing in effect a return, we'll
2335-
// need to create a block for that.
2336-
breaksToReturn = true;
2324+
throw ParseException("bad breakindex (high)");
23372325
}
23382326
if (debug) std::cerr << "breaktarget "<< breakStack[index].name << " arity " << breakStack[index].arity << std::endl;
23392327
auto& ret = breakStack[index];

test/br_to_exit.wasm

26 Bytes
Binary file not shown.

test/br_to_exit.wasm.fromBinary

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
(module
2+
(type $0 (func))
3+
(memory $0 0)
4+
(func $0 (; 0 ;) (type $0)
5+
(block $label$0
6+
(br $label$0)
7+
)
8+
)
9+
)
10+

0 commit comments

Comments
 (0)