diff options
author | Alon Zakai <alonzakai@gmail.com> | 2016-03-07 15:03:08 -0800 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2016-03-07 15:03:08 -0800 |
commit | 6299a2d2fb198845c9b140a308b6b7d433d5902b (patch) | |
tree | f57a4adf3cc0657cc2af7ee7976ebd991a122812 /src | |
parent | 6edb9f8958f155e289babad80fa234c2074af4d2 (diff) | |
download | binaryen-6299a2d2fb198845c9b140a308b6b7d433d5902b.tar.gz binaryen-6299a2d2fb198845c9b140a308b6b7d433d5902b.tar.bz2 binaryen-6299a2d2fb198845c9b140a308b6b7d433d5902b.zip |
update if parsing to new spec rules
Diffstat (limited to 'src')
-rw-r--r-- | src/asm2wasm.h | 12 | ||||
-rw-r--r-- | src/ast_utils.h | 43 | ||||
-rw-r--r-- | src/passes/Print.cpp | 17 | ||||
-rw-r--r-- | src/wasm-s-parser.h | 28 |
4 files changed, 84 insertions, 16 deletions
diff --git a/src/asm2wasm.h b/src/asm2wasm.h index cb1998aa4..be11344b2 100644 --- a/src/asm2wasm.h +++ b/src/asm2wasm.h @@ -28,6 +28,7 @@ #include "shared-constants.h" #include "asm_v_wasm.h" #include "pass.h" +#include "ast_utils.h" namespace wasm { @@ -66,17 +67,6 @@ struct AstStackHelper { std::vector<Ref> AstStackHelper::astStack; -struct BreakSeeker : public WasmWalker<BreakSeeker> { - IString target; // look for this one - size_t found; - - BreakSeeker(IString target) : target(target), found(false) {} - - void visitBreak(Break *curr) { - if (curr->name == target) found++; - } -}; - // // Asm2WasmPreProcessor - does some initial parsing/processing // of asm.js code. diff --git a/src/ast_utils.h b/src/ast_utils.h new file mode 100644 index 000000000..df2ffe578 --- /dev/null +++ b/src/ast_utils.h @@ -0,0 +1,43 @@ +/* + * Copyright 2016 WebAssembly Community Group participants + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef wasm_ast_utils_h +#define wasm_ast_utils_h + +#include "wasm.h" + +namespace wasm { + +struct BreakSeeker : public WasmWalker<BreakSeeker> { + Name target; // look for this one + size_t found; + + BreakSeeker(Name target) : target(target), found(false) {} + + void visitBreak(Break *curr) { + if (curr->name == target) found++; + } + + static bool has(Expression* tree, Name target) { + BreakSeeker breakSeeker(target); + breakSeeker.walk(tree); + return breakSeeker.found > 0; + } +}; + +} // namespace wasm + +#endif // wasm_ast_utils_h diff --git a/src/passes/Print.cpp b/src/passes/Print.cpp index f365db02f..e8e917a6e 100644 --- a/src/passes/Print.cpp +++ b/src/passes/Print.cpp @@ -65,11 +65,22 @@ struct PrintSExpression : public WasmVisitor<PrintSExpression, void> { decIndent(); } void visitIf(If *curr) { - printOpening(o, curr->ifFalse ? "if_else" : "if"); + printOpening(o, "if"); incIndent(); printFullLine(curr->condition); - printFullLine(curr->ifTrue); - if (curr->ifFalse) printFullLine(curr->ifFalse); + // ifTrue and False have implict blocks, avoid printing them if possible + if (curr->ifTrue->is<Block>() && curr->ifTrue->dyn_cast<Block>()->name.isNull() && curr->ifTrue->dyn_cast<Block>()->list.size() == 1) { + printFullLine(curr->ifTrue->dyn_cast<Block>()->list.back()); + } else { + printFullLine(curr->ifTrue); + } + if (curr->ifFalse) { + if (curr->ifFalse->is<Block>() && curr->ifFalse->dyn_cast<Block>()->name.isNull() && curr->ifFalse->dyn_cast<Block>()->list.size() == 1) { + printFullLine(curr->ifFalse->dyn_cast<Block>()->list.back()); + } else { + printFullLine(curr->ifFalse); + } + } decIndent(); } void visitLoop(Loop *curr) { diff --git a/src/wasm-s-parser.h b/src/wasm-s-parser.h index f0745f63f..381a57bb8 100644 --- a/src/wasm-s-parser.h +++ b/src/wasm-s-parser.h @@ -29,6 +29,7 @@ #include "shared-constants.h" #include "parsing.h" #include "asm_v_wasm.h" +#include "ast_utils.h" namespace wasm { @@ -789,9 +790,32 @@ private: Expression* makeIf(Element& s) { auto ret = allocator.alloc<If>(); ret->condition = parseExpression(s[1]); - ret->ifTrue = parseExpression(s[2]); + // ifTrue and ifFalse may get implicit blocks + Name ifTrueName = getPrefixedName("if-true"); + labelStack.push_back(ifTrueName); + auto* ifTrue = parseExpression(s[2]); + labelStack.pop_back(); + if (BreakSeeker::has(ifTrue, ifTrueName)) { + auto* block = allocator.alloc<Block>(); + block->name = ifTrueName; + block->list.push_back(ifTrue); + block->finalize(); + ifTrue = block; + } + ret->ifTrue = ifTrue; if (s.size() == 4) { - ret->ifFalse = parseExpression(s[3]); + Name ifFalseName = getPrefixedName("if-false"); + labelStack.push_back(ifFalseName); + auto* ifFalse = parseExpression(s[3]); + labelStack.pop_back(); + if (BreakSeeker::has(ifFalse, ifFalseName)) { + auto* block = allocator.alloc<Block>(); + block->name = ifFalseName; + block->list.push_back(ifFalse); + block->finalize(); + ifFalse = block; + } + ret->ifFalse = ifFalse; ret->finalize(); } return ret; |