From c6d08d97c196e2ceae5e3b960befcd6c66916cad Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Mon, 4 Jan 2016 14:27:46 -0800 Subject: parse for loops in asm2wasm #60 --- src/asm2wasm.h | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'src/asm2wasm.h') diff --git a/src/asm2wasm.h b/src/asm2wasm.h index d3d76ff83..e37751a66 100644 --- a/src/asm2wasm.h +++ b/src/asm2wasm.h @@ -1338,6 +1338,49 @@ Function* Asm2WasmBuilder::processFunction(Ref ast) { block->list.push_back(continuer); ret->body = block; return ret; + } else if (what == FOR) { + Ref finit = ast[1], + fcond = ast[2], + finc = ast[3], + fbody = ast[4]; + auto ret = allocator.alloc(); + IString out, in; + if (!parentLabel.isNull()) { + out = getBreakLabelName(parentLabel); + in = getContinueLabelName(parentLabel); + parentLabel = IString(); + } else { + out = getNextId("for-out"); + in = getNextId("for-in"); + } + ret->out = out; + ret->in = in; + breakStack.push_back(out); + continueStack.push_back(in); + Break *breakOut = allocator.alloc(); + breakOut->name = out; + If *condition = allocator.alloc(); + condition->condition = process(fcond); + condition->ifTrue = allocator.alloc(); + condition->ifFalse = breakOut; + auto body = allocator.alloc(); + body->list.push_back(condition); + body->list.push_back(process(fbody)); + body->list.push_back(process(finc)); + ret->body = body; + // loops do not automatically loop, add a branch back + Block* block = blockify(ret->body); + auto continuer = allocator.alloc(); + continuer->name = ret->in; + block->list.push_back(continuer); + ret->body = block; + continueStack.pop_back(); + breakStack.pop_back(); + Block *outer = allocator.alloc(); + // add an outer block for the init as well + outer->list.push_back(process(finit)); + outer->list.push_back(ret); + return outer; } else if (what == LABEL) { assert(parentLabel.isNull()); parentLabel = ast[1]->getIString(); -- cgit v1.2.3