11a427287SDan Gohman //==- WebAssemblyDisassembler.cpp - Disassembler for WebAssembly -*- C++ -*-==// 21a427287SDan Gohman // 32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information. 52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 61a427287SDan Gohman // 71a427287SDan Gohman //===----------------------------------------------------------------------===// 81a427287SDan Gohman /// 91a427287SDan Gohman /// \file 105f8f34e4SAdrian Prantl /// This file is part of the WebAssembly Disassembler. 111a427287SDan Gohman /// 121a427287SDan Gohman /// It contains code to translate the data produced by the decoder into 131a427287SDan Gohman /// MCInsts. 141a427287SDan Gohman /// 151a427287SDan Gohman //===----------------------------------------------------------------------===// 161a427287SDan Gohman 17a263aa25SDavid L. Jones #include "MCTargetDesc/WebAssemblyInstPrinter.h" 181a427287SDan Gohman #include "MCTargetDesc/WebAssemblyMCTargetDesc.h" 19c6c42137SRichard Trieu #include "TargetInfo/WebAssemblyTargetInfo.h" 201a427287SDan Gohman #include "llvm/MC/MCContext.h" 21c50b8907SBenjamin Kramer #include "llvm/MC/MCDisassembler/MCDisassembler.h" 2216c16827SSam Clegg #include "llvm/MC/MCFixedLenDisassembler.h" 231a427287SDan Gohman #include "llvm/MC/MCInst.h" 241a427287SDan Gohman #include "llvm/MC/MCInstrInfo.h" 251a427287SDan Gohman #include "llvm/MC/MCSubtargetInfo.h" 261a427287SDan Gohman #include "llvm/MC/MCSymbol.h" 272cb27072SThomas Lively #include "llvm/MC/MCSymbolWasm.h" 281a427287SDan Gohman #include "llvm/Support/Endian.h" 2916c16827SSam Clegg #include "llvm/Support/LEB128.h" 301a427287SDan Gohman #include "llvm/Support/TargetRegistry.h" 3116c16827SSam Clegg 321a427287SDan Gohman using namespace llvm; 331a427287SDan Gohman 341a427287SDan Gohman #define DEBUG_TYPE "wasm-disassembler" 351a427287SDan Gohman 3616c16827SSam Clegg using DecodeStatus = MCDisassembler::DecodeStatus; 3716c16827SSam Clegg 3816c16827SSam Clegg #include "WebAssemblyGenDisassemblerTables.inc" 3916c16827SSam Clegg 401a427287SDan Gohman namespace { 4149550663SFangrui Song static constexpr int WebAssemblyInstructionTableSize = 256; 4249550663SFangrui Song 431a427287SDan Gohman class WebAssemblyDisassembler final : public MCDisassembler { 441a427287SDan Gohman std::unique_ptr<const MCInstrInfo> MCII; 451a427287SDan Gohman 461a427287SDan Gohman DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size, 471a427287SDan Gohman ArrayRef<uint8_t> Bytes, uint64_t Address, 481a427287SDan Gohman raw_ostream &CStream) const override; 49f3b762a0SWouter van Oortmerssen DecodeStatus onSymbolStart(StringRef Name, uint64_t &Size, 50f3b762a0SWouter van Oortmerssen ArrayRef<uint8_t> Bytes, uint64_t Address, 51f3b762a0SWouter van Oortmerssen raw_ostream &CStream) const override; 521a427287SDan Gohman 531a427287SDan Gohman public: 541a427287SDan Gohman WebAssemblyDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx, 551a427287SDan Gohman std::unique_ptr<const MCInstrInfo> MCII) 561a427287SDan Gohman : MCDisassembler(STI, Ctx), MCII(std::move(MCII)) {} 571a427287SDan Gohman }; 581a427287SDan Gohman } // end anonymous namespace 591a427287SDan Gohman 601a427287SDan Gohman static MCDisassembler *createWebAssemblyDisassembler(const Target &T, 611a427287SDan Gohman const MCSubtargetInfo &STI, 621a427287SDan Gohman MCContext &Ctx) { 631a427287SDan Gohman std::unique_ptr<const MCInstrInfo> MCII(T.createMCInstrInfo()); 641a427287SDan Gohman return new WebAssemblyDisassembler(STI, Ctx, std::move(MCII)); 651a427287SDan Gohman } 661a427287SDan Gohman 674b0b2619STom Stellard extern "C" void LLVMInitializeWebAssemblyDisassembler() { 681a427287SDan Gohman // Register the disassembler for each target. 69f42454b9SMehdi Amini TargetRegistry::RegisterMCDisassembler(getTheWebAssemblyTarget32(), 701a427287SDan Gohman createWebAssemblyDisassembler); 71f42454b9SMehdi Amini TargetRegistry::RegisterMCDisassembler(getTheWebAssemblyTarget64(), 721a427287SDan Gohman createWebAssemblyDisassembler); 731a427287SDan Gohman } 741a427287SDan Gohman 75fc3163b6SThomas Lively static int nextByte(ArrayRef<uint8_t> Bytes, uint64_t &Size) { 7616c16827SSam Clegg if (Size >= Bytes.size()) 7716c16827SSam Clegg return -1; 7816c16827SSam Clegg auto V = Bytes[Size]; 7916c16827SSam Clegg Size++; 8016c16827SSam Clegg return V; 8116c16827SSam Clegg } 8216c16827SSam Clegg 832faf0794SThomas Lively static bool nextLEB(int64_t &Val, ArrayRef<uint8_t> Bytes, uint64_t &Size, 84f3b762a0SWouter van Oortmerssen bool Signed) { 8516c16827SSam Clegg unsigned N = 0; 8616c16827SSam Clegg const char *Error = nullptr; 872faf0794SThomas Lively Val = Signed ? decodeSLEB128(Bytes.data() + Size, &N, 8816c16827SSam Clegg Bytes.data() + Bytes.size(), &Error) 892faf0794SThomas Lively : static_cast<int64_t>(decodeULEB128(Bytes.data() + Size, &N, 902faf0794SThomas Lively Bytes.data() + Bytes.size(), 912faf0794SThomas Lively &Error)); 9216c16827SSam Clegg if (Error) 9316c16827SSam Clegg return false; 9416c16827SSam Clegg Size += N; 952faf0794SThomas Lively return true; 962faf0794SThomas Lively } 972faf0794SThomas Lively 982faf0794SThomas Lively static bool parseLEBImmediate(MCInst &MI, uint64_t &Size, 992faf0794SThomas Lively ArrayRef<uint8_t> Bytes, bool Signed) { 1002faf0794SThomas Lively int64_t Val; 1012faf0794SThomas Lively if (!nextLEB(Val, Bytes, Size, Signed)) 1022faf0794SThomas Lively return false; 10316c16827SSam Clegg MI.addOperand(MCOperand::createImm(Val)); 10416c16827SSam Clegg return true; 10516c16827SSam Clegg } 10616c16827SSam Clegg 10716c16827SSam Clegg template <typename T> 10822442924SThomas Lively bool parseImmediate(MCInst &MI, uint64_t &Size, ArrayRef<uint8_t> Bytes) { 10916c16827SSam Clegg if (Size + sizeof(T) > Bytes.size()) 11016c16827SSam Clegg return false; 111df6770f0SHeejin Ahn T Val = support::endian::read<T, support::endianness::little, 1>( 1120d9f3f7fSWouter van Oortmerssen Bytes.data() + Size); 11316c16827SSam Clegg Size += sizeof(T); 11422442924SThomas Lively if (std::is_floating_point<T>::value) { 11516c16827SSam Clegg MI.addOperand(MCOperand::createFPImm(static_cast<double>(Val))); 11622442924SThomas Lively } else { 11722442924SThomas Lively MI.addOperand(MCOperand::createImm(static_cast<int64_t>(Val))); 11822442924SThomas Lively } 11916c16827SSam Clegg return true; 12016c16827SSam Clegg } 12116c16827SSam Clegg 122f3b762a0SWouter van Oortmerssen MCDisassembler::DecodeStatus WebAssemblyDisassembler::onSymbolStart( 123f3b762a0SWouter van Oortmerssen StringRef Name, uint64_t &Size, ArrayRef<uint8_t> Bytes, uint64_t Address, 124*6fdd6a7bSFangrui Song raw_ostream &CStream) const { 125f3b762a0SWouter van Oortmerssen Size = 0; 126f3b762a0SWouter van Oortmerssen if (Address == 0) { 127f3b762a0SWouter van Oortmerssen // Start of a code section: we're parsing only the function count. 128f3b762a0SWouter van Oortmerssen int64_t FunctionCount; 129f3b762a0SWouter van Oortmerssen if (!nextLEB(FunctionCount, Bytes, Size, false)) 130f3b762a0SWouter van Oortmerssen return MCDisassembler::Fail; 131f3b762a0SWouter van Oortmerssen outs() << " # " << FunctionCount << " functions in section."; 132f3b762a0SWouter van Oortmerssen } else { 133f3b762a0SWouter van Oortmerssen // Parse the start of a single function. 134f3b762a0SWouter van Oortmerssen int64_t BodySize, LocalEntryCount; 135f3b762a0SWouter van Oortmerssen if (!nextLEB(BodySize, Bytes, Size, false) || 136f3b762a0SWouter van Oortmerssen !nextLEB(LocalEntryCount, Bytes, Size, false)) 137f3b762a0SWouter van Oortmerssen return MCDisassembler::Fail; 138f3b762a0SWouter van Oortmerssen if (LocalEntryCount) { 139f3b762a0SWouter van Oortmerssen outs() << " .local "; 140f3b762a0SWouter van Oortmerssen for (int64_t I = 0; I < LocalEntryCount; I++) { 141f3b762a0SWouter van Oortmerssen int64_t Count, Type; 142f3b762a0SWouter van Oortmerssen if (!nextLEB(Count, Bytes, Size, false) || 143f3b762a0SWouter van Oortmerssen !nextLEB(Type, Bytes, Size, false)) 144f3b762a0SWouter van Oortmerssen return MCDisassembler::Fail; 145f3b762a0SWouter van Oortmerssen for (int64_t J = 0; J < Count; J++) { 146f3b762a0SWouter van Oortmerssen if (I || J) 147f3b762a0SWouter van Oortmerssen outs() << ", "; 148f3b762a0SWouter van Oortmerssen outs() << WebAssembly::anyTypeToString(Type); 149f3b762a0SWouter van Oortmerssen } 150f3b762a0SWouter van Oortmerssen } 151f3b762a0SWouter van Oortmerssen } 152f3b762a0SWouter van Oortmerssen } 153f3b762a0SWouter van Oortmerssen outs() << "\n"; 154f3b762a0SWouter van Oortmerssen return MCDisassembler::Success; 155f3b762a0SWouter van Oortmerssen } 156f3b762a0SWouter van Oortmerssen 1571a427287SDan Gohman MCDisassembler::DecodeStatus WebAssemblyDisassembler::getInstruction( 1581a427287SDan Gohman MCInst &MI, uint64_t &Size, ArrayRef<uint8_t> Bytes, uint64_t /*Address*/, 159*6fdd6a7bSFangrui Song raw_ostream &CS) const { 16016c16827SSam Clegg CommentStream = &CS; 16116c16827SSam Clegg Size = 0; 162fc3163b6SThomas Lively int Opc = nextByte(Bytes, Size); 16316c16827SSam Clegg if (Opc < 0) 1641a427287SDan Gohman return MCDisassembler::Fail; 16516c16827SSam Clegg const auto *WasmInst = &InstructionTable0[Opc]; 16616c16827SSam Clegg // If this is a prefix byte, indirect to another table. 16716c16827SSam Clegg if (WasmInst->ET == ET_Prefix) { 16816c16827SSam Clegg WasmInst = nullptr; 16916c16827SSam Clegg // Linear search, so far only 2 entries. 17016c16827SSam Clegg for (auto PT = PrefixTable; PT->Table; PT++) { 17116c16827SSam Clegg if (PT->Prefix == Opc) { 17216c16827SSam Clegg WasmInst = PT->Table; 17316c16827SSam Clegg break; 17416c16827SSam Clegg } 17516c16827SSam Clegg } 17616c16827SSam Clegg if (!WasmInst) 17716c16827SSam Clegg return MCDisassembler::Fail; 1782faf0794SThomas Lively int64_t PrefixedOpc; 179f3b762a0SWouter van Oortmerssen if (!nextLEB(PrefixedOpc, Bytes, Size, false)) 18016c16827SSam Clegg return MCDisassembler::Fail; 1812faf0794SThomas Lively if (PrefixedOpc < 0 || PrefixedOpc >= WebAssemblyInstructionTableSize) 1822faf0794SThomas Lively return MCDisassembler::Fail; 1832faf0794SThomas Lively WasmInst += PrefixedOpc; 18416c16827SSam Clegg } 18516c16827SSam Clegg if (WasmInst->ET == ET_Unused) 18616c16827SSam Clegg return MCDisassembler::Fail; 18716c16827SSam Clegg // At this point we must have a valid instruction to decode. 18816c16827SSam Clegg assert(WasmInst->ET == ET_Instruction); 18916c16827SSam Clegg MI.setOpcode(WasmInst->Opcode); 19016c16827SSam Clegg // Parse any operands. 19116c16827SSam Clegg for (uint8_t OPI = 0; OPI < WasmInst->NumOperands; OPI++) { 192820c6263SWouter van Oortmerssen auto OT = OperandTable[WasmInst->OperandStart + OPI]; 193820c6263SWouter van Oortmerssen switch (OT) { 19416c16827SSam Clegg // ULEB operands: 19516c16827SSam Clegg case WebAssembly::OPERAND_BASIC_BLOCK: 19616c16827SSam Clegg case WebAssembly::OPERAND_LOCAL: 19716c16827SSam Clegg case WebAssembly::OPERAND_GLOBAL: 19816c16827SSam Clegg case WebAssembly::OPERAND_FUNCTION32: 19916c16827SSam Clegg case WebAssembly::OPERAND_OFFSET32: 20016c16827SSam Clegg case WebAssembly::OPERAND_P2ALIGN: 20116c16827SSam Clegg case WebAssembly::OPERAND_TYPEINDEX: 2028a28ce1aSWouter van Oortmerssen case WebAssembly::OPERAND_EVENT: 20316c16827SSam Clegg case MCOI::OPERAND_IMMEDIATE: { 20416c16827SSam Clegg if (!parseLEBImmediate(MI, Size, Bytes, false)) 20516c16827SSam Clegg return MCDisassembler::Fail; 20616c16827SSam Clegg break; 20716c16827SSam Clegg } 20816c16827SSam Clegg // SLEB operands: 20916c16827SSam Clegg case WebAssembly::OPERAND_I32IMM: 210ad72f685SWouter van Oortmerssen case WebAssembly::OPERAND_I64IMM: { 21116c16827SSam Clegg if (!parseLEBImmediate(MI, Size, Bytes, true)) 21216c16827SSam Clegg return MCDisassembler::Fail; 21316c16827SSam Clegg break; 21416c16827SSam Clegg } 2152cb27072SThomas Lively // block_type operands: 216ad72f685SWouter van Oortmerssen case WebAssembly::OPERAND_SIGNATURE: { 2172cb27072SThomas Lively int64_t Val; 2182cb27072SThomas Lively uint64_t PrevSize = Size; 2192cb27072SThomas Lively if (!nextLEB(Val, Bytes, Size, true)) 220ad72f685SWouter van Oortmerssen return MCDisassembler::Fail; 2212cb27072SThomas Lively if (Val < 0) { 2222cb27072SThomas Lively // Negative values are single septet value types or empty types 2232cb27072SThomas Lively if (Size != PrevSize + 1) { 2242cb27072SThomas Lively MI.addOperand( 2252cb27072SThomas Lively MCOperand::createImm(int64_t(WebAssembly::BlockType::Invalid))); 2262cb27072SThomas Lively } else { 2272cb27072SThomas Lively MI.addOperand(MCOperand::createImm(Val & 0x7f)); 2282cb27072SThomas Lively } 2292cb27072SThomas Lively } else { 2302cb27072SThomas Lively // We don't have access to the signature, so create a symbol without one 2312cb27072SThomas Lively MCSymbol *Sym = getContext().createTempSymbol("typeindex", true); 2322cb27072SThomas Lively auto *WasmSym = cast<MCSymbolWasm>(Sym); 2332cb27072SThomas Lively WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION); 2342cb27072SThomas Lively const MCExpr *Expr = MCSymbolRefExpr::create( 2352cb27072SThomas Lively WasmSym, MCSymbolRefExpr::VK_WASM_TYPEINDEX, getContext()); 2362cb27072SThomas Lively MI.addOperand(MCOperand::createExpr(Expr)); 2372cb27072SThomas Lively } 238ad72f685SWouter van Oortmerssen break; 239ad72f685SWouter van Oortmerssen } 24016c16827SSam Clegg // FP operands. 24116c16827SSam Clegg case WebAssembly::OPERAND_F32IMM: { 24222442924SThomas Lively if (!parseImmediate<float>(MI, Size, Bytes)) 24316c16827SSam Clegg return MCDisassembler::Fail; 24416c16827SSam Clegg break; 24516c16827SSam Clegg } 24616c16827SSam Clegg case WebAssembly::OPERAND_F64IMM: { 24722442924SThomas Lively if (!parseImmediate<double>(MI, Size, Bytes)) 24822442924SThomas Lively return MCDisassembler::Fail; 24922442924SThomas Lively break; 25022442924SThomas Lively } 25122442924SThomas Lively // Vector lane operands (not LEB encoded). 25222442924SThomas Lively case WebAssembly::OPERAND_VEC_I8IMM: { 25322442924SThomas Lively if (!parseImmediate<uint8_t>(MI, Size, Bytes)) 25422442924SThomas Lively return MCDisassembler::Fail; 25522442924SThomas Lively break; 25622442924SThomas Lively } 25722442924SThomas Lively case WebAssembly::OPERAND_VEC_I16IMM: { 25822442924SThomas Lively if (!parseImmediate<uint16_t>(MI, Size, Bytes)) 25922442924SThomas Lively return MCDisassembler::Fail; 26022442924SThomas Lively break; 26122442924SThomas Lively } 26222442924SThomas Lively case WebAssembly::OPERAND_VEC_I32IMM: { 26322442924SThomas Lively if (!parseImmediate<uint32_t>(MI, Size, Bytes)) 26422442924SThomas Lively return MCDisassembler::Fail; 26522442924SThomas Lively break; 26622442924SThomas Lively } 26722442924SThomas Lively case WebAssembly::OPERAND_VEC_I64IMM: { 26822442924SThomas Lively if (!parseImmediate<uint64_t>(MI, Size, Bytes)) 26916c16827SSam Clegg return MCDisassembler::Fail; 27016c16827SSam Clegg break; 27116c16827SSam Clegg } 272820c6263SWouter van Oortmerssen case WebAssembly::OPERAND_BRLIST: { 273820c6263SWouter van Oortmerssen int64_t TargetTableLen; 274820c6263SWouter van Oortmerssen if (!nextLEB(TargetTableLen, Bytes, Size, false)) 275820c6263SWouter van Oortmerssen return MCDisassembler::Fail; 276820c6263SWouter van Oortmerssen for (int64_t I = 0; I < TargetTableLen; I++) { 277820c6263SWouter van Oortmerssen if (!parseLEBImmediate(MI, Size, Bytes, false)) 278820c6263SWouter van Oortmerssen return MCDisassembler::Fail; 279820c6263SWouter van Oortmerssen } 280820c6263SWouter van Oortmerssen // Default case. 281820c6263SWouter van Oortmerssen if (!parseLEBImmediate(MI, Size, Bytes, false)) 282820c6263SWouter van Oortmerssen return MCDisassembler::Fail; 283820c6263SWouter van Oortmerssen break; 284820c6263SWouter van Oortmerssen } 285a733d08dSWouter van Oortmerssen case MCOI::OPERAND_REGISTER: 286a733d08dSWouter van Oortmerssen // The tablegen header currently does not have any register operands since 287a733d08dSWouter van Oortmerssen // we use only the stack (_S) instructions. 288a733d08dSWouter van Oortmerssen // If you hit this that probably means a bad instruction definition in 289a733d08dSWouter van Oortmerssen // tablegen. 290a733d08dSWouter van Oortmerssen llvm_unreachable("Register operand in WebAssemblyDisassembler"); 29116c16827SSam Clegg default: 29216c16827SSam Clegg llvm_unreachable("Unknown operand type in WebAssemblyDisassembler"); 29316c16827SSam Clegg } 29416c16827SSam Clegg } 29516c16827SSam Clegg return MCDisassembler::Success; 2961a427287SDan Gohman } 297