Skip to content

Commit 97ae505

Browse files
authored
[WebAssembly] Support disassembler for try_table (#108800)
This adds support for disassembler for the new `try_table` instruction. This adds tests for `throw` and `throw_ref` as well. Currently tag expressions are not supported for `throw` or `try_table` instruction when instructions are parsed from the disassembler. Not sure whether there is a way to support it. (This is not a new thing for the new EH proposal; it has not been supported for the legacy EH as well.)
1 parent 5df1b79 commit 97ae505

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

llvm/lib/Target/WebAssembly/Disassembler/WebAssemblyDisassembler.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,24 @@ MCDisassembler::DecodeStatus WebAssemblyDisassembler::getInstruction(
289289
return MCDisassembler::Fail;
290290
break;
291291
}
292+
case WebAssembly::OPERAND_CATCH_LIST: {
293+
if (!parseLEBImmediate(MI, Size, Bytes, false))
294+
return MCDisassembler::Fail;
295+
int64_t NumCatches = MI.getOperand(MI.getNumOperands() - 1).getImm();
296+
for (int64_t I = 0; I < NumCatches; I++) {
297+
if (!parseImmediate<uint8_t>(MI, Size, Bytes))
298+
return MCDisassembler::Fail;
299+
int64_t CatchOpcode = MI.getOperand(MI.getNumOperands() - 1).getImm();
300+
if (CatchOpcode == wasm::WASM_OPCODE_CATCH ||
301+
CatchOpcode == wasm::WASM_OPCODE_CATCH_REF) {
302+
if (!parseLEBImmediate(MI, Size, Bytes, false)) // tag index
303+
return MCDisassembler::Fail;
304+
}
305+
if (!parseLEBImmediate(MI, Size, Bytes, false)) // destination
306+
return MCDisassembler::Fail;
307+
}
308+
break;
309+
}
292310
case MCOI::OPERAND_REGISTER:
293311
// The tablegen header currently does not have any register operands since
294312
// we use only the stack (_S) instructions.

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -377,10 +377,15 @@ void WebAssemblyInstPrinter::printCatchList(const MCInst *MI, unsigned OpNo,
377377
auto PrintTagOp = [&](const MCOperand &Op) {
378378
const MCSymbolRefExpr *TagExpr = nullptr;
379379
const MCSymbolWasm *TagSym = nullptr;
380-
assert(Op.isExpr());
381-
TagExpr = dyn_cast<MCSymbolRefExpr>(Op.getExpr());
382-
TagSym = cast<MCSymbolWasm>(&TagExpr->getSymbol());
383-
O << TagSym->getName() << " ";
380+
if (Op.isExpr()) {
381+
TagExpr = dyn_cast<MCSymbolRefExpr>(Op.getExpr());
382+
TagSym = cast<MCSymbolWasm>(&TagExpr->getSymbol());
383+
O << TagSym->getName() << " ";
384+
} else {
385+
// When instructions are parsed from the disassembler, we have an
386+
// immediate tag index and not a tag expr
387+
O << Op.getImm() << " ";
388+
}
384389
};
385390

386391
for (unsigned I = 0; I < NumCatches; I++) {

llvm/test/MC/Disassembler/WebAssembly/wasm.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,10 @@
5353
# This can mean end_block/end_loop/end_if/end_function/end_try..
5454
# CHECK: end
5555
0x0B
56+
57+
# CHECK: try_table (catch 0 0) (catch_ref 0 1) (catch_all 2) (catch_all_ref 3)
58+
0x1F 0x40 0x04 0x00 0x00 0x00 0x01 0x00 0x01 0x02 0x02 0x03 0x03
59+
# CHECK: throw 0
60+
0x08 0x00
61+
# CHECK: throw_ref
62+
0x0a

0 commit comments

Comments
 (0)