summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/asm2wasm.cpp12
-rw-r--r--test/emcc_hello_world.wast3
2 files changed, 11 insertions, 4 deletions
diff --git a/src/asm2wasm.cpp b/src/asm2wasm.cpp
index ef43a6ac2..3f6173adf 100644
--- a/src/asm2wasm.cpp
+++ b/src/asm2wasm.cpp
@@ -1093,7 +1093,10 @@ Function* Asm2WasmModule::processFunction(Ref ast) {
abort_on("bad processUnshifted", ptr);
};
- processStatements = [&](Ref ast, unsigned from) {
+ processStatements = [&](Ref ast, unsigned from) -> Expression* {
+ unsigned size = ast->size() - from;
+ if (size == 0) return allocator.alloc<Nop>();
+ if (size == 1) return process(ast[from]);
auto block = allocator.alloc<Block>();
for (unsigned i = from; i < ast->size(); i++) {
block->list.push_back(process(ast[i]));
@@ -1104,7 +1107,12 @@ Function* Asm2WasmModule::processFunction(Ref ast) {
function->body = processStatements(body, start);
if (needTopmost) {
Block* topmost = dynamic_cast<Block*>(function->body);
- assert(topmost);
+ // if there's no block there, or there is a block but it already has a var, we need a new block.
+ if (!topmost || topmost->var.is()) {
+ topmost = allocator.alloc<Block>();
+ topmost->list.push_back(function->body);
+ function->body = topmost;
+ }
topmost->var = TOPMOST;
}
// cleanups/checks
diff --git a/test/emcc_hello_world.wast b/test/emcc_hello_world.wast
index 2a5fe5699..d1e083ec2 100644
--- a/test/emcc_hello_world.wast
+++ b/test/emcc_hello_world.wast
@@ -24758,8 +24758,7 @@
)
)
(func $runPostSets
- (block
- )
+ (nop)
)
(func $_i64Subtract (param $a i32) (param $b i32) (param $c i32) (param $d i32) (result i32)
(local $l i32)