blob: de85478a24f62b30fa3415c84118ae612ff20516 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
/*
* Copyright 2021 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.
*/
#include "ir/eh-utils.h"
#include "ir/branch-utils.h"
namespace wasm {
namespace EHUtils {
bool isPopValid(Expression* catchBody) {
Expression* firstChild = nullptr;
auto* block = catchBody->dynCast<Block>();
if (!block) {
firstChild = catchBody;
} else {
// When there are multiple expressions within a catch body, an implicit
// block is created within it for convenience purposes, and if there are no
// branches that targets the block, it will be omitted when written back.
// But if there is a branch targetting this block, this block cannot be
// removed, and 'pop''s location will be like
// (catch $e
// (block $l0
// (pop i32) ;; within a block!
// (br $l0)
// ...
// )
// )
// which is invalid.
if (BranchUtils::BranchSeeker::has(block, block->name)) {
return false;
}
// There should be a pop somewhere
if (block->list.empty()) {
return false;
}
firstChild = *block->list.begin();
}
// Go down the line for the first child until we reach a leaf. A pop should be
// in that first-decendent line.
while (true) {
if (firstChild->is<Pop>()) {
return true;
}
// We use ValueChildIterator in order not to go into block/loop/try/if
// bodies, because a pop cannot be in those control flow expressions.
ValueChildIterator it(firstChild);
if (it.begin() == it.end()) {
return false;
}
firstChild = *it.begin();
}
}
} // namespace EHUtils
} // namespace wasm
|