From 9024b061cbf4609a3925bfd0100a6a5734ce9d92 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Sat, 5 Dec 2015 10:53:09 -0800 Subject: switch in wasm2asm --- src/wasm2asm.h | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/wasm2asm.h b/src/wasm2asm.h index 6ea050538..9ff7b7de1 100644 --- a/src/wasm2asm.h +++ b/src/wasm2asm.h @@ -516,7 +516,33 @@ Ref Wasm2AsmBuilder::processFunctionBody(Expression* curr, IString result) { return ret; } Ref visitSwitch(Switch *curr) override { - abort(); // XXX TODO + Ref ret = ValueBuilder::makeLabel(fromName(curr->name), ValueBuilder::makeBlock()); + Ref value; + if (isStatement(curr->value)) { + ScopedTemp temp(i32, parent); + ret[2]->push_back(visit(curr->value, temp)); + value = temp.getAstName(); + } else { + value = visit(curr->value, EXPRESSION_RESULT); + } + Ref theSwitch = ValueBuilder::makeSwitch(value); + ret[2]->push_back(theSwitch); + for (auto& c : curr->cases) { + bool added = false; + for (size_t i = 0; i < curr->targets.size(); i++) { + if (curr->targets[i] == c.name) { + ValueBuilder::appendCaseToSwitch(theSwitch, ValueBuilder::makeNum(i)); + added = true; + } + } + if (c.name == curr->default_) { + ValueBuilder::appendDefaultToSwitch(theSwitch); + added = true; + } + assert(added); + ValueBuilder::appendCodeToSwitch(theSwitch, blockify(visit(c.body, NO_RESULT)), false); + } + return ret; } Ref visitCall(Call *curr) override { Ref theCall = ValueBuilder::makeCall(fromName(curr->target)); -- cgit v1.2.3