summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2015-10-29 17:32:59 -0700
committerAlon Zakai <alonzakai@gmail.com>2015-10-29 17:32:59 -0700
commitec5e1d0ffdc2769d754cc7c679c3b286f787bb6e (patch)
treef39eabfff65a2299429d2523c0fef074c647f0a6
parentd93c05bfa9a7fc813c31c7972e6680d6ef0277d2 (diff)
downloadbinaryen-ec5e1d0ffdc2769d754cc7c679c3b286f787bb6e.tar.gz
binaryen-ec5e1d0ffdc2769d754cc7c679c3b286f787bb6e.tar.bz2
binaryen-ec5e1d0ffdc2769d754cc7c679c3b286f787bb6e.zip
don't emit empty blocks
-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)