From ec5e1d0ffdc2769d754cc7c679c3b286f787bb6e Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Thu, 29 Oct 2015 17:32:59 -0700 Subject: don't emit empty blocks --- src/asm2wasm.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'src') 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(); + if (size == 1) return process(ast[from]); auto block = allocator.alloc(); 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(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(); + topmost->list.push_back(function->body); + function->body = topmost; + } topmost->var = TOPMOST; } // cleanups/checks -- cgit v1.2.3