diff options
Diffstat (limited to 'src/ast')
-rw-r--r-- | src/ast/ExpressionManipulator.cpp | 8 | ||||
-rw-r--r-- | src/ast/branch-utils.h | 44 | ||||
-rw-r--r-- | src/ast/manipulation.h | 16 | ||||
-rw-r--r-- | src/ast/type-updating.h | 6 |
4 files changed, 60 insertions, 14 deletions
diff --git a/src/ast/ExpressionManipulator.cpp b/src/ast/ExpressionManipulator.cpp index 160ff55e1..3868ca316 100644 --- a/src/ast/ExpressionManipulator.cpp +++ b/src/ast/ExpressionManipulator.cpp @@ -19,7 +19,9 @@ namespace wasm { -Expression* ExpressionManipulator::flexibleCopy(Expression* original, Module& wasm, CustomCopier custom) { +namespace ExpressionManipulator { + +Expression* flexibleCopy(Expression* original, Module& wasm, CustomCopier custom) { struct Copier : public Visitor<Copier, Expression*> { Module& wasm; CustomCopier custom; @@ -135,7 +137,7 @@ Expression* ExpressionManipulator::flexibleCopy(Expression* original, Module& wa // Splice an item into the middle of a block's list -void ExpressionManipulator::spliceIntoBlock(Block* block, Index index, Expression* add) { +void spliceIntoBlock(Block* block, Index index, Expression* add) { auto& list = block->list; if (index == list.size()) { list.push_back(add); // simple append @@ -150,4 +152,6 @@ void ExpressionManipulator::spliceIntoBlock(Block* block, Index index, Expressio block->finalize(block->type); } +} // namespace ExpressionManipulator + } // namespace wasm diff --git a/src/ast/branch-utils.h b/src/ast/branch-utils.h new file mode 100644 index 000000000..a54b8151f --- /dev/null +++ b/src/ast/branch-utils.h @@ -0,0 +1,44 @@ +/* + * Copyright 2017 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_branch_h +#define wasm_ast_branch_h + +#include "wasm.h" + +namespace wasm { + +namespace BranchUtils { + +// branches not actually taken (e.g. (br $out (unreachable))) +// are trivially ignored in our type system + +inline bool isBranchTaken(Break* br) { + return !(br->value && br->value->type == unreachable) && + !(br->condition && br->condition->type == unreachable); +} + +inline bool isBranchTaken(Switch* sw) { + return !(sw->value && sw->value->type == unreachable) && + sw->condition->type != unreachable; +} + +} // namespace BranchUtils + +} // namespace wasm + +#endif // wasm_ast_branch_h + diff --git a/src/ast/manipulation.h b/src/ast/manipulation.h index 9e01e4c74..29b6a346d 100644 --- a/src/ast/manipulation.h +++ b/src/ast/manipulation.h @@ -21,10 +21,10 @@ namespace wasm { -struct ExpressionManipulator { +namespace ExpressionManipulator { // Re-use a node's memory. This helps avoid allocation when optimizing. template<typename InputType, typename OutputType> - static OutputType* convert(InputType *input) { + inline OutputType* convert(InputType *input) { static_assert(sizeof(OutputType) <= sizeof(InputType), "Can only convert to a smaller size Expression node"); input->~InputType(); // arena-allocaed, so no destructor, but avoid UB. @@ -35,13 +35,13 @@ struct ExpressionManipulator { // Convenience method for nop, which is a common conversion template<typename InputType> - static Nop* nop(InputType* target) { + inline Nop* nop(InputType* target) { return convert<InputType, Nop>(target); } // Convert a node that allocates template<typename InputType, typename OutputType> - static OutputType* convert(InputType *input, MixedArena& allocator) { + inline OutputType* convert(InputType *input, MixedArena& allocator) { assert(sizeof(OutputType) <= sizeof(InputType)); input->~InputType(); // arena-allocaed, so no destructor, but avoid UB. OutputType* output = (OutputType*)(input); @@ -50,9 +50,9 @@ struct ExpressionManipulator { } using CustomCopier = std::function<Expression*(Expression*)>; - static Expression* flexibleCopy(Expression* original, Module& wasm, CustomCopier custom); + Expression* flexibleCopy(Expression* original, Module& wasm, CustomCopier custom); - static Expression* copy(Expression* original, Module& wasm) { + inline Expression* copy(Expression* original, Module& wasm) { auto copy = [](Expression* curr) { return nullptr; }; @@ -60,8 +60,8 @@ struct ExpressionManipulator { } // Splice an item into the middle of a block's list - static void spliceIntoBlock(Block* block, Index index, Expression* add); -}; + void spliceIntoBlock(Block* block, Index index, Expression* add); +} } // wasm diff --git a/src/ast/type-updating.h b/src/ast/type-updating.h index b8988761a..dc0ae0f36 100644 --- a/src/ast/type-updating.h +++ b/src/ast/type-updating.h @@ -132,13 +132,11 @@ struct TypeUpdater : public ExpressionStackWalker<TypeUpdater, UnifiedExpression // adds (or removes) breaks depending on break/switch contents void discoverBreaks(Expression* curr, int change) { if (auto* br = curr->dynCast<Break>()) { - if (!(br->value && br->value->type == unreachable) && - !(br->condition && br->condition->type == unreachable)) { + if (BranchUtils::isBranchTaken(br)) { noteBreakChange(br->name, change, br->value); } } else if (auto* sw = curr->dynCast<Switch>()) { - if (!(sw->value && sw->value->type == unreachable) && - sw->condition->type != unreachable) { + if (BranchUtils::isBranchTaken(sw)) { applySwitchChanges(sw, change); } } |