Skip to content

[EH] Rename delegateTarget to exceptionTarget (NFC) #3562

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/ir/branch-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ inline bool replacePossibleTarget(Expression* branch, Name from, Name to) {
}

// Replace all delegate targets within the given AST.
inline void replaceDelegateTargets(Expression* ast, Name from, Name to) {
inline void replaceExceptionTargets(Expression* ast, Name from, Name to) {
struct Replacer
: public PostWalker<Replacer, UnifiedExpressionVisitor<Replacer>> {
Name from, to;
Expand Down
4 changes: 2 additions & 2 deletions src/wasm-binary.h
Original file line number Diff line number Diff line change
Expand Up @@ -1419,7 +1419,7 @@ class WasmBinaryBuilder {
// or not.
std::unordered_set<Name> breakTargetNames;
// the names that delegates target.
std::unordered_set<Name> delegateTargetNames;
std::unordered_set<Name> exceptionTargetNames;

std::vector<Expression*> expressionStack;

Expand Down Expand Up @@ -1524,7 +1524,7 @@ class WasmBinaryBuilder {
Expression* getBlockOrSingleton(Type type);

BreakTarget getBreakTarget(int32_t offset);
Name getDelegateTargetName(int32_t offset);
Name getExceptionTargetName(int32_t offset);

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

Expand Down
2 changes: 1 addition & 1 deletion src/wasm-s-parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ class SExpressionWasmBuilder {
i++;
}
}
enum class LabelType { Break, Delegate };
enum class LabelType { Break, Exception };
Name getLabel(Element& s, LabelType labelType = LabelType::Break);
Expression* makeBreak(Element& s);
Expression* makeBreakTable(Element& s);
Expand Down
2 changes: 1 addition & 1 deletion src/wasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -1945,4 +1945,4 @@ std::ostream& operator<<(std::ostream& o, wasm::StackIR& ir);

} // namespace std

#endif // wasm_wasm_h
#endif // wasm_wasm_h
27 changes: 14 additions & 13 deletions src/wasm/wasm-binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1920,7 +1920,7 @@ void WasmBinaryBuilder::readFunctions() {
// process body
assert(breakStack.empty());
assert(breakTargetNames.empty());
assert(delegateTargetNames.empty());
assert(exceptionTargetNames.empty());
assert(breakStack.empty());
assert(expressionStack.empty());
assert(controlFlowStack.empty());
Expand All @@ -1930,7 +1930,7 @@ void WasmBinaryBuilder::readFunctions() {
assert(depth == 0);
assert(breakStack.empty());
assert(breakTargetNames.empty());
assert(delegateTargetNames.empty());
assert(exceptionTargetNames.empty());
if (!expressionStack.empty()) {
throwError("stack not empty on function exit");
}
Expand Down Expand Up @@ -3389,7 +3389,7 @@ Expression* WasmBinaryBuilder::getBlockOrSingleton(Type type) {
block->finalize(type);
// maybe we don't need a block here?
if (breakTargetNames.find(block->name) == breakTargetNames.end() &&
delegateTargetNames.find(block->name) == delegateTargetNames.end()) {
exceptionTargetNames.find(block->name) == exceptionTargetNames.end()) {
block->name = Name();
if (block->list.size() == 1) {
return block->list[0];
Expand Down Expand Up @@ -3465,8 +3465,8 @@ WasmBinaryBuilder::getBreakTarget(int32_t offset) {
return ret;
}

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

if (lastSeparator == BinaryConsts::Delegate) {
curr->delegateTarget = getDelegateTargetName(getU32LEB());
curr->delegateTarget = getExceptionTargetName(getU32LEB());
}

// For simplicity, we make try's labels only can be targeted by delegates, and
Expand All @@ -5843,21 +5843,22 @@ void WasmBinaryBuilder::visitTryOrTryInBlock(Expression*& out) {
curr->name = getNextLabel();
if (auto* block = curr->body->dynCast<Block>()) {
if (block->name.is()) {
if (delegateTargetNames.find(block->name) != delegateTargetNames.end()) {
BranchUtils::replaceDelegateTargets(block, block->name, curr->name);
delegateTargetNames.erase(block->name);
if (exceptionTargetNames.find(block->name) !=
exceptionTargetNames.end()) {
BranchUtils::replaceExceptionTargets(block, block->name, curr->name);
exceptionTargetNames.erase(block->name);
}
// maybe we don't need a block here?
if (block->list.size() == 1) {
curr->body = block->list[0];
}
}
}
if (delegateTargetNames.find(catchLabel) != delegateTargetNames.end()) {
if (exceptionTargetNames.find(catchLabel) != exceptionTargetNames.end()) {
for (auto* catchBody : curr->catchBodies) {
BranchUtils::replaceDelegateTargets(catchBody, catchLabel, curr->name);
BranchUtils::replaceExceptionTargets(catchBody, catchLabel, curr->name);
}
delegateTargetNames.erase(catchLabel);
exceptionTargetNames.erase(catchLabel);
}
curr->finalize(curr->type);

Expand Down
2 changes: 1 addition & 1 deletion src/wasm/wasm-s-parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2048,7 +2048,7 @@ Expression* SExpressionWasmBuilder::makeTry(Element& s) {
if (inner.size() != 2) {
throw ParseException("invalid delegate", inner.line, inner.col);
}
ret->delegateTarget = getLabel(*inner[1], LabelType::Delegate);
ret->delegateTarget = getLabel(*inner[1], LabelType::Exception);
}

if (i != s.size()) {
Expand Down
16 changes: 8 additions & 8 deletions src/wasm/wasm-validator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ struct FunctionValidator : public WalkerPass<PostWalker<FunctionValidator>> {
};

std::unordered_map<Name, BreakInfo> breakInfos;
std::unordered_set<Name> delegateTargetNames;
std::unordered_set<Name> exceptionTargetNames;

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

Expand Down Expand Up @@ -280,7 +280,7 @@ struct FunctionValidator : public WalkerPass<PostWalker<FunctionValidator>> {
static void visitPreTry(FunctionValidator* self, Expression** currp) {
auto* curr = (*currp)->cast<Try>();
if (curr->name.is()) {
self->delegateTargetNames.insert(curr->name);
self->exceptionTargetNames.insert(curr->name);
}
}

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

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

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

if (curr->isDelegate()) {
noteDelegate(curr->delegateTarget, curr);
noteException(curr->delegateTarget, curr);
}
}

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

shouldBeTrue(
breakInfos.empty(), curr->body, "all named break targets must exist");
shouldBeTrue(delegateTargetNames.empty(),
shouldBeTrue(exceptionTargetNames.empty(),
curr->body,
"all named delegate targets must exist");
returnTypes.clear();
Expand Down