diff options
author | Alon Zakai <alonzakai@gmail.com> | 2016-09-21 13:33:51 -0700 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2016-09-21 13:53:41 -0700 |
commit | 3355792fba29e62f02cf1f1acb3a219cf5a69970 (patch) | |
tree | 5b6b4145d05bf248e080b784d23ff1d43310f328 | |
parent | 228b9a1cfc89ede2dcc064de9b2e60f53a047128 (diff) | |
download | binaryen-3355792fba29e62f02cf1f1acb3a219cf5a69970.tar.gz binaryen-3355792fba29e62f02cf1f1acb3a219cf5a69970.tar.bz2 binaryen-3355792fba29e62f02cf1f1acb3a219cf5a69970.zip |
new if label behavior
-rw-r--r-- | src/wasm-s-parser.h | 54 | ||||
-rw-r--r-- | test/passes/remove-unused-brs.txt | 16 |
2 files changed, 29 insertions, 41 deletions
diff --git a/src/wasm-s-parser.h b/src/wasm-s-parser.h index 6e0eda826..ace7c31d7 100644 --- a/src/wasm-s-parser.h +++ b/src/wasm-s-parser.h @@ -1227,44 +1227,32 @@ private: Expression* makeIf(Element& s) { auto ret = allocator.alloc<If>(); Index i = 1; + Name label; + if (s[i]->dollared()) { + // the if is labeled + label = s[i++]->str(); + } else { + label = getPrefixedName("if"); + } + labelStack.push_back(label); if (s[i]->isStr()) { - // if type + // if type, TODO: parse? i++; } ret->condition = parseExpression(s[i++]); - - // ifTrue and ifFalse may get implicit blocks - auto handle = [&](const char* title, Element& s) { - Name name = getPrefixedName(title); - bool explicitThenElse = false; - if (s[0]->str() == THEN || s[0]->str() == ELSE) { - explicitThenElse = true; - if (s[1]->isStr() && s[1]->dollared()) { - name = s[1]->str(); - } - } - labelStack.push_back(name); - auto* ret = parseExpression(&s); - labelStack.pop_back(); - if (explicitThenElse) { - ret->dynCast<Block>()->name = name; - } else { - // add a block if we must - if (BreakSeeker::has(ret, name)) { - auto* block = allocator.alloc<Block>(); - block->name = name; - block->list.push_back(ret); - block->finalize(); - ret = block; - } - } - return ret; - }; - - ret->ifTrue = handle("if-true", *s[i++]); + ret->ifTrue = parseExpression(*s[i++]); if (i < s.size()) { - ret->ifFalse = handle("if-else", *s[i++]); - ret->finalize(); + ret->ifFalse = parseExpression(*s[i++]); + } + ret->finalize(); + labelStack.pop_back(); + // create a break target if we must + if (BreakSeeker::has(ret, label)) { + auto* block = allocator.alloc<Block>(); + block->name = label; + block->list.push_back(ret); + block->finalize(); + return block; } return ret; } diff --git a/test/passes/remove-unused-brs.txt b/test/passes/remove-unused-brs.txt index 7d76db7f0..33ec51ea5 100644 --- a/test/passes/remove-unused-brs.txt +++ b/test/passes/remove-unused-brs.txt @@ -299,7 +299,7 @@ (i32.const 1) ) (block $block2 - (block $block3 + (block $block1 (drop (i32.const 2) ) @@ -313,7 +313,7 @@ (if (i32.const 0) (block $block4 - (block $block5 + (block $block3 (drop (i32.const 2) ) @@ -329,7 +329,7 @@ ) (if (block $block6 i32 - (block $block7 + (block $block5 (drop (i32.const 2) ) @@ -349,14 +349,14 @@ (i32.const 0) ) (block $a - (block $block11 + (block $block7 (drop (i32.const 1) ) ) ) (block $a - (block $block13 + (block $block8 (drop (i32.const 2) ) @@ -535,7 +535,7 @@ (block $out (if (i32.const 0) - (block $block15 + (block $block13 (drop (i32.const 1) ) @@ -580,7 +580,7 @@ (block $out (if (i32.const 0) - (block $block22 + (block $block17 (drop (i32.const 1) ) @@ -801,7 +801,7 @@ (i32.const 1) ) (br $out - (block $block2 i32 + (block $block1 i32 (set_local $x (i32.const 0) ) |