Skip to content

Commit 1388887

Browse files
aheejintmsri
authored andcommitted
[WebAssembly] Support annotation for try_table (llvm#109029)
This adds support for annotations (`down to labelN`) for `try_table`.
1 parent 699f228 commit 1388887

File tree

2 files changed

+69
-11
lines changed

2 files changed

+69
-11
lines changed

llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyInstPrinter.cpp

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,20 @@ void WebAssemblyInstPrinter::printInst(const MCInst *MI, uint64_t Address,
110110
// Print any added annotation.
111111
printAnnotation(OS, Annot);
112112

113+
auto PrintBranchAnnotation = [&](const MCOperand &Op,
114+
SmallSet<uint64_t, 8> &Printed) {
115+
uint64_t Depth = Op.getImm();
116+
if (!Printed.insert(Depth).second)
117+
return;
118+
if (Depth >= ControlFlowStack.size()) {
119+
printAnnotation(OS, "Invalid depth argument!");
120+
} else {
121+
const auto &Pair = ControlFlowStack.rbegin()[Depth];
122+
printAnnotation(OS, utostr(Depth) + ": " + (Pair.second ? "up" : "down") +
123+
" to label" + utostr(Pair.first));
124+
}
125+
};
126+
113127
if (CommentStream) {
114128
// Observe any effects on the control flow stack, for use in annotating
115129
// control flow label references.
@@ -136,6 +150,23 @@ void WebAssemblyInstPrinter::printInst(const MCInst *MI, uint64_t Address,
136150
EHInstStack.push_back(TRY);
137151
return;
138152

153+
case WebAssembly::TRY_TABLE:
154+
case WebAssembly::TRY_TABLE_S: {
155+
SmallSet<uint64_t, 8> Printed;
156+
unsigned OpIdx = 1;
157+
const MCOperand &Op = MI->getOperand(OpIdx++);
158+
unsigned NumCatches = Op.getImm();
159+
for (unsigned I = 0; I < NumCatches; I++) {
160+
int64_t CatchOpcode = MI->getOperand(OpIdx++).getImm();
161+
if (CatchOpcode == wasm::WASM_OPCODE_CATCH ||
162+
CatchOpcode == wasm::WASM_OPCODE_CATCH_REF)
163+
OpIdx++; // Skip tag
164+
PrintBranchAnnotation(MI->getOperand(OpIdx++), Printed);
165+
}
166+
ControlFlowStack.push_back(std::make_pair(ControlFlowCounter++, false));
167+
return;
168+
}
169+
139170
case WebAssembly::END_LOOP:
140171
case WebAssembly::END_LOOP_S:
141172
if (ControlFlowStack.empty()) {
@@ -147,6 +178,8 @@ void WebAssemblyInstPrinter::printInst(const MCInst *MI, uint64_t Address,
147178

148179
case WebAssembly::END_BLOCK:
149180
case WebAssembly::END_BLOCK_S:
181+
case WebAssembly::END_TRY_TABLE:
182+
case WebAssembly::END_TRY_TABLE_S:
150183
if (ControlFlowStack.empty()) {
151184
printAnnotation(OS, "End marker mismatch!");
152185
} else {
@@ -251,17 +284,7 @@ void WebAssemblyInstPrinter::printInst(const MCInst *MI, uint64_t Address,
251284
if (!MI->getOperand(I).isImm())
252285
continue;
253286
}
254-
uint64_t Depth = MI->getOperand(I).getImm();
255-
if (!Printed.insert(Depth).second)
256-
continue;
257-
if (Depth >= ControlFlowStack.size()) {
258-
printAnnotation(OS, "Invalid depth argument!");
259-
} else {
260-
const auto &Pair = ControlFlowStack.rbegin()[Depth];
261-
printAnnotation(OS, utostr(Depth) + ": " +
262-
(Pair.second ? "up" : "down") + " to label" +
263-
utostr(Pair.first));
264-
}
287+
PrintBranchAnnotation(MI->getOperand(I), Printed);
265288
}
266289
}
267290
}

llvm/test/MC/WebAssembly/annotations.s

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,22 @@ test_annotation:
3333
rethrow 0
3434
end_try
3535
end_try
36+
37+
block exnref
38+
block
39+
block () -> (i32, exnref)
40+
block i32
41+
try_table (catch __cpp_exception 0) (catch_ref __c_longjmp 1) (catch_all 2) (catch_all_ref 3)
42+
end_try_table
43+
return
44+
end_block
45+
return
46+
end_block
47+
return
48+
end_block
49+
return
50+
end_block
51+
drop
3652
end_function
3753

3854

@@ -61,5 +77,24 @@ test_annotation:
6177
# CHECK-NEXT: rethrow 0 # to caller
6278
# CHECK-NEXT: end_try # label3:
6379
# CHECK-NEXT: end_try # label0:
80+
81+
# CHECK: block exnref
82+
# CHECK-NEXT: block
83+
# CHECK-NEXT: block () -> (i32, exnref)
84+
# CHECK-NEXT: block i32
85+
# CHECK-NEXT: try_table (catch __cpp_exception 0) (catch_ref __c_longjmp 1) (catch_all 2) (catch_all_ref 3) # 0: down to label10
86+
# CHECK-NEXT: # 1: down to label9
87+
# CHECK-NEXT: # 2: down to label8
88+
# CHECK-NEXT: # 3: down to label7
89+
# CHECK-NEXT: end_try_table # label11:
90+
# CHECK-NEXT: return
91+
# CHECK-NEXT: end_block # label10:
92+
# CHECK-NEXT: return
93+
# CHECK-NEXT: end_block # label9:
94+
# CHECK-NEXT: return
95+
# CHECK-NEXT: end_block # label8:
96+
# CHECK-NEXT: return
97+
# CHECK-NEXT: end_block # label7:
98+
# CHECK-NEXT: drop
6499
# CHECK-NEXT: end_function
65100

0 commit comments

Comments
 (0)