Skip to content

Commit 589e671

Browse files
aheejinradekdoulik
authored andcommitted
Rename stack variables in CFGWalker (NFC) (WebAssembly#6232)
This renames `***Stack` variables in `CFGWalker` to be consistent, as a preparation for adding another stack for the new EH. Currently `ifStack` and `loopStack` contains `BasicBlock*`s but `tryStack` contains `Expression*`, and `Try` expressions are rather contained in `unwindExprStack`, which to me is confusing.
1 parent f9948a0 commit 589e671

File tree

1 file changed

+28
-26
lines changed

1 file changed

+28
-26
lines changed

src/cfg/cfg-traversal.h

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,12 @@ struct CFGWalker : public PostWalker<SubType, VisitorType> {
8686
std::map<Name, std::vector<BasicBlock*>> branches;
8787
// stack of the last blocks of if conditions + the last blocks of if true
8888
// bodies
89-
std::vector<BasicBlock*> ifStack;
89+
std::vector<BasicBlock*> ifLastBlockStack;
9090
// stack of the first blocks of loops
91-
std::vector<BasicBlock*> loopStack;
91+
std::vector<BasicBlock*> loopLastBlockStack;
9292

9393
// stack of the last blocks of try bodies
94-
std::vector<BasicBlock*> tryStack;
94+
std::vector<BasicBlock*> tryLastBlockStack;
9595
// Stack of the blocks that contain a throwing instruction, and therefore they
9696
// can reach the first blocks of catches that throwing instructions should
9797
// unwind to at any moment. That is, the topmost item in this vector relates
@@ -100,7 +100,7 @@ struct CFGWalker : public PostWalker<SubType, VisitorType> {
100100
// of the catches, although that could be improved perhaps).
101101
std::vector<std::vector<BasicBlock*>> throwingInstsStack;
102102
// stack of 'Try' expressions corresponding to throwingInstsStack.
103-
std::vector<Expression*> unwindExprStack;
103+
std::vector<Expression*> tryStack;
104104
// A stack for each try, where each entry is a list of blocks, one for each
105105
// catch, used during processing. We start by assigning the start blocks to
106106
// here, and then read those at the appropriate time; when we finish a catch
@@ -186,12 +186,13 @@ struct CFGWalker : public PostWalker<SubType, VisitorType> {
186186
static void doStartIfTrue(SubType* self, Expression** currp) {
187187
auto* last = self->currBasicBlock;
188188
self->link(last, self->startBasicBlock()); // ifTrue
189-
self->ifStack.push_back(last); // the block before the ifTrue
189+
self->ifLastBlockStack.push_back(last); // the block before the ifTrue
190190
}
191191

192192
static void doStartIfFalse(SubType* self, Expression** currp) {
193-
self->ifStack.push_back(self->currBasicBlock); // the ifTrue fallthrough
194-
self->link(self->ifStack[self->ifStack.size() - 2],
193+
self->ifLastBlockStack.push_back(
194+
self->currBasicBlock); // the ifTrue fallthrough
195+
self->link(self->ifLastBlockStack[self->ifLastBlockStack.size() - 2],
195196
self->startBasicBlock()); // before if -> ifFalse
196197
}
197198

@@ -203,13 +204,13 @@ struct CFGWalker : public PostWalker<SubType, VisitorType> {
203204
self->link(last, self->currBasicBlock);
204205
if ((*currp)->cast<If>()->ifFalse) {
205206
// we just linked ifFalse, need to link ifTrue to the end
206-
self->link(self->ifStack.back(), self->currBasicBlock);
207-
self->ifStack.pop_back();
207+
self->link(self->ifLastBlockStack.back(), self->currBasicBlock);
208+
self->ifLastBlockStack.pop_back();
208209
} else {
209210
// no ifFalse, so add a fallthrough for if the if is not taken
210-
self->link(self->ifStack.back(), self->currBasicBlock);
211+
self->link(self->ifLastBlockStack.back(), self->currBasicBlock);
211212
}
212-
self->ifStack.pop_back();
213+
self->ifLastBlockStack.pop_back();
213214
}
214215

215216
static void doStartLoop(SubType* self, Expression** currp) {
@@ -218,7 +219,7 @@ struct CFGWalker : public PostWalker<SubType, VisitorType> {
218219
// a loop with no backedges would still be counted here, but oh well
219220
self->loopTops.push_back(self->currBasicBlock);
220221
self->link(last, self->currBasicBlock);
221-
self->loopStack.push_back(self->currBasicBlock);
222+
self->loopLastBlockStack.push_back(self->currBasicBlock);
222223
}
223224

224225
static void doEndLoop(SubType* self, Expression** currp) {
@@ -227,14 +228,14 @@ struct CFGWalker : public PostWalker<SubType, VisitorType> {
227228
auto* curr = (*currp)->cast<Loop>();
228229
// branches to the top of the loop
229230
if (curr->name.is()) {
230-
auto* loopStart = self->loopStack.back();
231+
auto* loopStart = self->loopLastBlockStack.back();
231232
auto& origins = self->branches[curr->name];
232233
for (auto* origin : origins) {
233234
self->link(origin, loopStart);
234235
}
235236
self->branches.erase(curr->name);
236237
}
237-
self->loopStack.pop_back();
238+
self->loopLastBlockStack.pop_back();
238239
}
239240

240241
static void doEndBranch(SubType* self, Expression** currp) {
@@ -278,9 +279,9 @@ struct CFGWalker : public PostWalker<SubType, VisitorType> {
278279
// catch $e3
279280
// ...
280281
// end
281-
assert(self->unwindExprStack.size() == self->throwingInstsStack.size());
282+
assert(self->tryStack.size() == self->throwingInstsStack.size());
282283
for (int i = self->throwingInstsStack.size() - 1; i >= 0;) {
283-
auto* tryy = self->unwindExprStack[i]->template cast<Try>();
284+
auto* tryy = self->tryStack[i]->template cast<Try>();
284285
if (tryy->isDelegate()) {
285286
// If this delegates to the caller, there is no possibility that this
286287
// instruction can throw to outer catches.
@@ -291,7 +292,7 @@ struct CFGWalker : public PostWalker<SubType, VisitorType> {
291292
// and the target try.
292293
[[maybe_unused]] bool found = false;
293294
for (int j = i - 1; j >= 0; j--) {
294-
if (self->unwindExprStack[j]->template cast<Try>()->name ==
295+
if (self->tryStack[j]->template cast<Try>()->name ==
295296
tryy->delegateTarget) {
296297
i = j;
297298
found = true;
@@ -348,11 +349,12 @@ struct CFGWalker : public PostWalker<SubType, VisitorType> {
348349
static void doStartTry(SubType* self, Expression** currp) {
349350
auto* curr = (*currp)->cast<Try>();
350351
self->throwingInstsStack.emplace_back();
351-
self->unwindExprStack.push_back(curr);
352+
self->tryStack.push_back(curr);
352353
}
353354

354355
static void doStartCatches(SubType* self, Expression** currp) {
355-
self->tryStack.push_back(self->currBasicBlock); // last block of try body
356+
self->tryLastBlockStack.push_back(
357+
self->currBasicBlock); // last block of try body
356358

357359
// Now that we are starting the catches, create the basic blocks that they
358360
// begin with.
@@ -374,7 +376,7 @@ struct CFGWalker : public PostWalker<SubType, VisitorType> {
374376
}
375377

376378
self->throwingInstsStack.pop_back();
377-
self->unwindExprStack.pop_back();
379+
self->tryStack.pop_back();
378380
self->catchIndexStack.push_back(0);
379381
}
380382

@@ -398,8 +400,8 @@ struct CFGWalker : public PostWalker<SubType, VisitorType> {
398400
self->link(last, self->currBasicBlock);
399401
}
400402
// try body's last block -> continuation block
401-
self->link(self->tryStack.back(), self->currBasicBlock);
402-
self->tryStack.pop_back();
403+
self->link(self->tryLastBlockStack.back(), self->currBasicBlock);
404+
self->tryLastBlockStack.pop_back();
403405
self->processCatchStack.pop_back();
404406
self->catchIndexStack.pop_back();
405407
}
@@ -522,11 +524,11 @@ struct CFGWalker : public PostWalker<SubType, VisitorType> {
522524
}
523525

524526
assert(branches.size() == 0);
525-
assert(ifStack.size() == 0);
526-
assert(loopStack.size() == 0);
527-
assert(tryStack.size() == 0);
527+
assert(ifLastBlockStack.size() == 0);
528+
assert(loopLastBlockStack.size() == 0);
529+
assert(tryLastBlockStack.size() == 0);
528530
assert(throwingInstsStack.size() == 0);
529-
assert(unwindExprStack.size() == 0);
531+
assert(tryStack.size() == 0);
530532
assert(processCatchStack.size() == 0);
531533
}
532534

0 commit comments

Comments
 (0)