|
16 | 16 |
|
17 | 17 | #include "ir/eh-utils.h"
|
18 | 18 | #include "ir/branch-utils.h"
|
| 19 | +#include "ir/find_all.h" |
19 | 20 |
|
20 | 21 | namespace wasm {
|
21 | 22 |
|
22 | 23 | namespace EHUtils {
|
23 | 24 |
|
24 |
| -bool isPopValid(Expression* catchBody) { |
25 |
| - Expression* firstChild = nullptr; |
26 |
| - auto* block = catchBody->dynCast<Block>(); |
27 |
| - if (!block) { |
28 |
| - firstChild = catchBody; |
29 |
| - } else { |
30 |
| - // When there are multiple expressions within a catch body, an implicit |
31 |
| - // block is created within it for convenience purposes, and if there are no |
32 |
| - // branches that targets the block, it will be omitted when written back. |
33 |
| - // But if there is a branch targetting this block, this block cannot be |
34 |
| - // removed, and 'pop''s location will be like |
35 |
| - // (catch $e |
36 |
| - // (block $l0 |
37 |
| - // (pop i32) ;; within a block! |
38 |
| - // (br $l0) |
39 |
| - // ... |
40 |
| - // ) |
41 |
| - // ) |
42 |
| - // which is invalid. |
43 |
| - if (BranchUtils::BranchSeeker::has(block, block->name)) { |
44 |
| - return false; |
45 |
| - } |
46 |
| - // There should be a pop somewhere |
47 |
| - if (block->list.empty()) { |
48 |
| - return false; |
49 |
| - } |
50 |
| - firstChild = *block->list.begin(); |
51 |
| - } |
| 25 | +// This returns three values, some of them as output parameters: |
| 26 | +// - Return value: 'pop' expression (Expression*), when there is one in |
| 27 | +// first-descendant line. If there's no such pop, it returns null. |
| 28 | +// - isPopNested: Whether the discovered 'pop' is nested within a block |
| 29 | +// - popPtr: 'pop' expression's pointer (Expression**), when there is one found |
| 30 | +// |
| 31 | +// When 'catchBody' itself is a 'pop', 'pop''s pointer is null, because there is |
| 32 | +// no way to get the given expression's address. But that's fine because pop's |
| 33 | +// pointer is only necessary (in handleBlockNestedPops) to fix it up when it is |
| 34 | +// nested, and if 'catchBody' itself is a pop, we don't need to fix it up. |
| 35 | +static Expression* |
| 36 | +getFirstPop(Expression* catchBody, bool& isPopNested, Expression**& popPtr) { |
| 37 | + Expression* firstChild = catchBody; |
| 38 | + isPopNested = false; |
| 39 | + popPtr = nullptr; |
| 40 | + // When there are multiple expressions within a catch body, an implicit |
| 41 | + // block is created within it for convenience purposes. |
| 42 | + auto* implicitBlock = catchBody->dynCast<Block>(); |
52 | 43 |
|
53 | 44 | // Go down the line for the first child until we reach a leaf. A pop should be
|
54 |
| - // in that first-decendent line. |
| 45 | + // in that first-decendant line. |
| 46 | + Expression** firstChildPtr = nullptr; |
55 | 47 | while (true) {
|
56 | 48 | if (firstChild->is<Pop>()) {
|
57 |
| - return true; |
| 49 | + popPtr = firstChildPtr; |
| 50 | + return firstChild; |
58 | 51 | }
|
59 |
| - // We use ValueChildIterator in order not to go into block/loop/try/if |
60 |
| - // bodies, because a pop cannot be in those control flow expressions. |
61 |
| - ValueChildIterator it(firstChild); |
| 52 | + |
| 53 | + if (Properties::isControlFlowStructure(firstChild)) { |
| 54 | + // If's condition is a value child who comes before an 'if' instruction |
| 55 | + // in binary, it is fine if a 'pop' is in there. We don't allow a 'pop' to |
| 56 | + // be in an 'if''s then or else body because they are not |
| 57 | + // first descendants. |
| 58 | + if (auto* if_ = firstChild->dynCast<If>()) { |
| 59 | + firstChild = if_->condition; |
| 60 | + continue; |
| 61 | + } |
| 62 | + // We don't allow the pop to be included in a loop, because it cannot be |
| 63 | + // run more than once |
| 64 | + if (firstChild->is<Loop>()) { |
| 65 | + return nullptr; |
| 66 | + } |
| 67 | + if (firstChild->is<Block>()) { |
| 68 | + // If there are no branches that targets the implicit block, it will be |
| 69 | + // removed when written back. But there are branches that target the |
| 70 | + // implicit block, |
| 71 | + // (catch $e |
| 72 | + // (block $l0 |
| 73 | + // (pop i32) ;; within a block! |
| 74 | + // (br $l0) |
| 75 | + // ... |
| 76 | + // ) |
| 77 | + // This cannot be removed, so this is considered a nested pop (which we |
| 78 | + // should fix). |
| 79 | + if (firstChild == implicitBlock) { |
| 80 | + if (BranchUtils::BranchSeeker::has(implicitBlock, |
| 81 | + implicitBlock->name)) { |
| 82 | + isPopNested = true; |
| 83 | + } |
| 84 | + } else { |
| 85 | + isPopNested = true; |
| 86 | + } |
| 87 | + } |
| 88 | + if (firstChild->is<Try>()) { |
| 89 | + isPopNested = true; |
| 90 | + } |
| 91 | + } |
| 92 | + ChildIterator it(firstChild); |
62 | 93 | if (it.begin() == it.end()) {
|
63 |
| - return false; |
| 94 | + return nullptr; |
| 95 | + } |
| 96 | + firstChildPtr = &*it.begin(); |
| 97 | + firstChild = *firstChildPtr; |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +bool isPopValid(Expression* catchBody) { |
| 102 | + bool isPopNested = false; |
| 103 | + Expression** popPtr = nullptr; |
| 104 | + auto* pop = getFirstPop(catchBody, isPopNested, popPtr); |
| 105 | + return pop != nullptr && !isPopNested; |
| 106 | +} |
| 107 | + |
| 108 | +void handleBlockNestedPops(Function* func, Module& wasm) { |
| 109 | + Builder builder(wasm); |
| 110 | + FindAll<Try> trys(func->body); |
| 111 | + for (auto* try_ : trys.list) { |
| 112 | + for (Index i = 0; i < try_->catchTags.size(); i++) { |
| 113 | + Name tagName = try_->catchTags[i]; |
| 114 | + auto* tag = wasm.getTagOrNull(tagName); |
| 115 | + if (tag->sig.params == Type::none) { |
| 116 | + continue; |
| 117 | + } |
| 118 | + |
| 119 | + auto* catchBody = try_->catchBodies[i]; |
| 120 | + bool isPopNested = false; |
| 121 | + Expression** popPtr = nullptr; |
| 122 | + Expression* pop = getFirstPop(catchBody, isPopNested, popPtr); |
| 123 | + assert(pop && "Pop has not been found in this catch"); |
| 124 | + |
| 125 | + // Change code like |
| 126 | + // (catch $e |
| 127 | + // ... |
| 128 | + // (block |
| 129 | + // (pop i32) |
| 130 | + // ) |
| 131 | + // ) |
| 132 | + // into |
| 133 | + // (catch $e |
| 134 | + // (local.set $new |
| 135 | + // (pop i32) |
| 136 | + // ) |
| 137 | + // ... |
| 138 | + // (block |
| 139 | + // (local.get $new) |
| 140 | + // ) |
| 141 | + // ) |
| 142 | + if (isPopNested) { |
| 143 | + assert(popPtr); |
| 144 | + Index newLocal = builder.addVar(func, pop->type); |
| 145 | + try_->catchBodies[i] = |
| 146 | + builder.makeSequence(builder.makeLocalSet(newLocal, pop), catchBody); |
| 147 | + *popPtr = builder.makeLocalGet(newLocal, pop->type); |
| 148 | + } |
64 | 149 | }
|
65 |
| - firstChild = *it.begin(); |
66 | 150 | }
|
67 | 151 | }
|
68 | 152 |
|
|
0 commit comments