From 4d8022da3af7d2604e7779e43f721ade1f04b89e Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Sun, 8 Nov 2015 10:30:34 -0800 Subject: more switch fixes --- src/wasm-s-parser.h | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/wasm-s-parser.h b/src/wasm-s-parser.h index 20867191d..b8dbdfd69 100644 --- a/src/wasm-s-parser.h +++ b/src/wasm-s-parser.h @@ -857,10 +857,10 @@ private: return ret; } - Expression* makeMaybeBlock(Element &s, size_t i) { + Expression* makeMaybeBlock(Element& s, size_t i, size_t stopAt=-1) { if (s.size() == i+1) return parseExpression(s[i]); auto ret = allocator.alloc(); - for (; i < s.size(); i++) { + for (; i < s.size() && i < stopAt; i++) { ret->list.push_back(parseExpression(s[i])); } return ret; @@ -960,16 +960,20 @@ private: } Name name = getPrefixedName(otherIndex++, "switch-case"); ret->targets.push_back(name); - // if there is no body at all, this is a trivial fallthrough - auto body = curr.size() < 3 ? allocator.alloc() : parseExpression(curr[2]); - // if not fallthrough, add a break on the switch itself - if (curr.size() == 4) { - assert(curr[3]->str() == IString("fallthru")); - } else if (curr.size() == 3) { - Break *exit = allocator.alloc(); - exit->name = ret->name; - exit->value = body; - body = exit; + Expression* body; + size_t size = curr.size(); + if (size == 2) { + body = allocator.alloc(); // trivial fallthrough + } else { + size_t j = 2; + bool fallThrough = curr[size-1]->isStr() && curr[size-1]->str() == IString("fallthrough"); + body = makeMaybeBlock(curr, j, fallThrough ? size-1 : size); + if (!fallThrough) { + Break *exit = allocator.alloc(); + exit->name = ret->name; + exit->value = body; + body = exit; + } } ret->cases.emplace_back(name, body); } else { -- cgit v1.2.3