Skip to content

Commit b149b9b

Browse files
committed
[EH] Rename delegateTarget to exceptionTarget (NFC)
So far `Try`'s label is only targetted by `delegate`s, but it turns out `rethrow` also has to follow the same rule as `delegate` so it needs to target a `Try` label. So this renames variables like `delegateTargetNames` to `exceptionTargetNames` and methods like `replaceDelegateTargets` to `replaceExceptionTargets`. I considered `tryTarget`, but the branch/block counterpart name we use is not `blockTarget` but `branchTarget`, so I chose `exceptionTarget`.
1 parent f826df6 commit b149b9b

File tree

7 files changed

+28
-27
lines changed

7 files changed

+28
-27
lines changed

src/ir/branch-utils.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ inline bool replacePossibleTarget(Expression* branch, Name from, Name to) {
136136
}
137137

138138
// Replace all delegate targets within the given AST.
139-
inline void replaceDelegateTargets(Expression* ast, Name from, Name to) {
139+
inline void replaceExceptionTargets(Expression* ast, Name from, Name to) {
140140
struct Replacer
141141
: public PostWalker<Replacer, UnifiedExpressionVisitor<Replacer>> {
142142
Name from, to;

src/wasm-binary.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -1419,7 +1419,7 @@ class WasmBinaryBuilder {
14191419
// or not.
14201420
std::unordered_set<Name> breakTargetNames;
14211421
// the names that delegates target.
1422-
std::unordered_set<Name> delegateTargetNames;
1422+
std::unordered_set<Name> exceptionTargetNames;
14231423

14241424
std::vector<Expression*> expressionStack;
14251425

@@ -1524,7 +1524,7 @@ class WasmBinaryBuilder {
15241524
Expression* getBlockOrSingleton(Type type);
15251525

15261526
BreakTarget getBreakTarget(int32_t offset);
1527-
Name getDelegateTargetName(int32_t offset);
1527+
Name getExceptionTargetName(int32_t offset);
15281528

15291529
void readMemoryAccess(Address& alignment, Address& offset);
15301530

src/wasm-s-parser.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ class SExpressionWasmBuilder {
242242
i++;
243243
}
244244
}
245-
enum class LabelType { Break, Delegate };
245+
enum class LabelType { Break, Exception };
246246
Name getLabel(Element& s, LabelType labelType = LabelType::Break);
247247
Expression* makeBreak(Element& s);
248248
Expression* makeBreakTable(Element& s);

src/wasm.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1945,4 +1945,4 @@ std::ostream& operator<<(std::ostream& o, wasm::StackIR& ir);
19451945

19461946
} // namespace std
19471947

1948-
#endif // wasm_wasm_h
1948+
#endif // wasm_wasm_h

src/wasm/wasm-binary.cpp

+14-13
Original file line numberDiff line numberDiff line change
@@ -1920,7 +1920,7 @@ void WasmBinaryBuilder::readFunctions() {
19201920
// process body
19211921
assert(breakStack.empty());
19221922
assert(breakTargetNames.empty());
1923-
assert(delegateTargetNames.empty());
1923+
assert(exceptionTargetNames.empty());
19241924
assert(breakStack.empty());
19251925
assert(expressionStack.empty());
19261926
assert(controlFlowStack.empty());
@@ -1930,7 +1930,7 @@ void WasmBinaryBuilder::readFunctions() {
19301930
assert(depth == 0);
19311931
assert(breakStack.empty());
19321932
assert(breakTargetNames.empty());
1933-
assert(delegateTargetNames.empty());
1933+
assert(exceptionTargetNames.empty());
19341934
if (!expressionStack.empty()) {
19351935
throwError("stack not empty on function exit");
19361936
}
@@ -3389,7 +3389,7 @@ Expression* WasmBinaryBuilder::getBlockOrSingleton(Type type) {
33893389
block->finalize(type);
33903390
// maybe we don't need a block here?
33913391
if (breakTargetNames.find(block->name) == breakTargetNames.end() &&
3392-
delegateTargetNames.find(block->name) == delegateTargetNames.end()) {
3392+
exceptionTargetNames.find(block->name) == exceptionTargetNames.end()) {
33933393
block->name = Name();
33943394
if (block->list.size() == 1) {
33953395
return block->list[0];
@@ -3465,8 +3465,8 @@ WasmBinaryBuilder::getBreakTarget(int32_t offset) {
34653465
return ret;
34663466
}
34673467

3468-
Name WasmBinaryBuilder::getDelegateTargetName(int32_t offset) {
3469-
BYN_TRACE("getDelegateTarget " << offset << std::endl);
3468+
Name WasmBinaryBuilder::getExceptionTargetName(int32_t offset) {
3469+
BYN_TRACE("getExceptionTarget " << offset << std::endl);
34703470
// We always start parsing a function by creating a block label and pushing it
34713471
// in breakStack in getBlockOrSingleton, so if a 'delegate''s target is that
34723472
// block, it does not mean it targets that block; it throws to the caller.
@@ -3482,7 +3482,7 @@ Name WasmBinaryBuilder::getDelegateTargetName(int32_t offset) {
34823482
// if the delegate is in literally unreachable code, then we will not emit it
34833483
// anyhow, so do not note that the target has delegate to it
34843484
if (!willBeIgnored) {
3485-
delegateTargetNames.insert(ret.name);
3485+
exceptionTargetNames.insert(ret.name);
34863486
}
34873487
return ret.name;
34883488
}
@@ -5832,7 +5832,7 @@ void WasmBinaryBuilder::visitTryOrTryInBlock(Expression*& out) {
58325832
breakStack.pop_back();
58335833

58345834
if (lastSeparator == BinaryConsts::Delegate) {
5835-
curr->delegateTarget = getDelegateTargetName(getU32LEB());
5835+
curr->delegateTarget = getExceptionTargetName(getU32LEB());
58365836
}
58375837

58385838
// For simplicity, we make try's labels only can be targeted by delegates, and
@@ -5843,21 +5843,22 @@ void WasmBinaryBuilder::visitTryOrTryInBlock(Expression*& out) {
58435843
curr->name = getNextLabel();
58445844
if (auto* block = curr->body->dynCast<Block>()) {
58455845
if (block->name.is()) {
5846-
if (delegateTargetNames.find(block->name) != delegateTargetNames.end()) {
5847-
BranchUtils::replaceDelegateTargets(block, block->name, curr->name);
5848-
delegateTargetNames.erase(block->name);
5846+
if (exceptionTargetNames.find(block->name) !=
5847+
exceptionTargetNames.end()) {
5848+
BranchUtils::replaceExceptionTargets(block, block->name, curr->name);
5849+
exceptionTargetNames.erase(block->name);
58495850
}
58505851
// maybe we don't need a block here?
58515852
if (block->list.size() == 1) {
58525853
curr->body = block->list[0];
58535854
}
58545855
}
58555856
}
5856-
if (delegateTargetNames.find(catchLabel) != delegateTargetNames.end()) {
5857+
if (exceptionTargetNames.find(catchLabel) != exceptionTargetNames.end()) {
58575858
for (auto* catchBody : curr->catchBodies) {
5858-
BranchUtils::replaceDelegateTargets(catchBody, catchLabel, curr->name);
5859+
BranchUtils::replaceExceptionTargets(catchBody, catchLabel, curr->name);
58595860
}
5860-
delegateTargetNames.erase(catchLabel);
5861+
exceptionTargetNames.erase(catchLabel);
58615862
}
58625863
curr->finalize(curr->type);
58635864

src/wasm/wasm-s-parser.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -2048,7 +2048,7 @@ Expression* SExpressionWasmBuilder::makeTry(Element& s) {
20482048
if (inner.size() != 2) {
20492049
throw ParseException("invalid delegate", inner.line, inner.col);
20502050
}
2051-
ret->delegateTarget = getLabel(*inner[1], LabelType::Delegate);
2051+
ret->delegateTarget = getLabel(*inner[1], LabelType::Exception);
20522052
}
20532053

20542054
if (i != s.size()) {

src/wasm/wasm-validator.cpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ struct FunctionValidator : public WalkerPass<PostWalker<FunctionValidator>> {
236236
};
237237

238238
std::unordered_map<Name, BreakInfo> breakInfos;
239-
std::unordered_set<Name> delegateTargetNames;
239+
std::unordered_set<Name> exceptionTargetNames;
240240

241241
std::set<Type> returnTypes; // types used in returns
242242

@@ -280,7 +280,7 @@ struct FunctionValidator : public WalkerPass<PostWalker<FunctionValidator>> {
280280
static void visitPreTry(FunctionValidator* self, Expression** currp) {
281281
auto* curr = (*currp)->cast<Try>();
282282
if (curr->name.is()) {
283-
self->delegateTargetNames.insert(curr->name);
283+
self->exceptionTargetNames.insert(curr->name);
284284
}
285285
}
286286

@@ -300,7 +300,7 @@ struct FunctionValidator : public WalkerPass<PostWalker<FunctionValidator>> {
300300
static void visitPreCatch(FunctionValidator* self, Expression** currp) {
301301
auto* curr = (*currp)->cast<Try>();
302302
if (curr->name.is()) {
303-
self->delegateTargetNames.erase(curr->name);
303+
self->exceptionTargetNames.erase(curr->name);
304304
}
305305
}
306306

@@ -376,7 +376,7 @@ struct FunctionValidator : public WalkerPass<PostWalker<FunctionValidator>> {
376376
void visitRefIs(RefIs* curr);
377377
void visitRefFunc(RefFunc* curr);
378378
void visitRefEq(RefEq* curr);
379-
void noteDelegate(Name name, Expression* curr);
379+
void noteException(Name name, Expression* curr);
380380
void visitTry(Try* curr);
381381
void visitThrow(Throw* curr);
382382
void visitRethrow(Rethrow* curr);
@@ -2073,9 +2073,9 @@ void FunctionValidator::visitRefEq(RefEq* curr) {
20732073
"ref.eq's right argument should be a subtype of eqref");
20742074
}
20752075

2076-
void FunctionValidator::noteDelegate(Name name, Expression* curr) {
2076+
void FunctionValidator::noteException(Name name, Expression* curr) {
20772077
if (name != DELEGATE_CALLER_TARGET) {
2078-
shouldBeTrue(delegateTargetNames.find(name) != delegateTargetNames.end(),
2078+
shouldBeTrue(exceptionTargetNames.find(name) != exceptionTargetNames.end(),
20792079
curr,
20802080
"all delegate targets must be valid");
20812081
}
@@ -2122,7 +2122,7 @@ void FunctionValidator::visitTry(Try* curr) {
21222122
"try should have either catches or a delegate");
21232123

21242124
if (curr->isDelegate()) {
2125-
noteDelegate(curr->delegateTarget, curr);
2125+
noteException(curr->delegateTarget, curr);
21262126
}
21272127
}
21282128

@@ -2533,7 +2533,7 @@ void FunctionValidator::visitFunction(Function* curr) {
25332533

25342534
shouldBeTrue(
25352535
breakInfos.empty(), curr->body, "all named break targets must exist");
2536-
shouldBeTrue(delegateTargetNames.empty(),
2536+
shouldBeTrue(exceptionTargetNames.empty(),
25372537
curr->body,
25382538
"all named delegate targets must exist");
25392539
returnTypes.clear();

0 commit comments

Comments
 (0)