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" 27*2cb27072SThomas 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 &VStream, 491a427287SDan Gohman raw_ostream &CStream) const override; 50f3b762a0SWouter van Oortmerssen DecodeStatus onSymbolStart(StringRef Name, uint64_t &Size, 51f3b762a0SWouter van Oortmerssen ArrayRef<uint8_t> Bytes, uint64_t Address, 52f3b762a0SWouter van Oortmerssen raw_ostream &VStream, 53f3b762a0SWouter van Oortmerssen raw_ostream &CStream) const override; 541a427287SDan Gohman 551a427287SDan Gohman public: 561a427287SDan Gohman WebAssemblyDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx, 571a427287SDan Gohman std::unique_ptr<const MCInstrInfo> MCII) 581a427287SDan Gohman : MCDisassembler(STI, Ctx), MCII(std::move(MCII)) {} 591a427287SDan Gohman }; 601a427287SDan Gohman } // end anonymous namespace 611a427287SDan Gohman 621a427287SDan Gohman static MCDisassembler *createWebAssemblyDisassembler(const Target &T, 631a427287SDan Gohman const MCSubtargetInfo &STI, 641a427287SDan Gohman MCContext &Ctx) { 651a427287SDan Gohman std::unique_ptr<const MCInstrInfo> MCII(T.createMCInstrInfo()); 661a427287SDan Gohman return new WebAssemblyDisassembler(STI, Ctx, std::move(MCII)); 671a427287SDan Gohman } 681a427287SDan Gohman 694b0b2619STom Stellard extern "C" void LLVMInitializeWebAssemblyDisassembler() { 701a427287SDan Gohman // Register the disassembler for each target. 71f42454b9SMehdi Amini TargetRegistry::RegisterMCDisassembler(getTheWebAssemblyTarget32(), 721a427287SDan Gohman createWebAssemblyDisassembler); 73f42454b9SMehdi Amini TargetRegistry::RegisterMCDisassembler(getTheWebAssemblyTarget64(), 741a427287SDan Gohman createWebAssemblyDisassembler); 751a427287SDan Gohman } 761a427287SDan Gohman 77fc3163b6SThomas Lively static int nextByte(ArrayRef<uint8_t> Bytes, uint64_t &Size) { 7816c16827SSam Clegg if (Size >= Bytes.size()) 7916c16827SSam Clegg return -1; 8016c16827SSam Clegg auto V = Bytes[Size]; 8116c16827SSam Clegg Size++; 8216c16827SSam Clegg return V; 8316c16827SSam Clegg } 8416c16827SSam Clegg 852faf0794SThomas Lively static bool nextLEB(int64_t &Val, ArrayRef<uint8_t> Bytes, uint64_t &Size, 86f3b762a0SWouter van Oortmerssen bool Signed) { 8716c16827SSam Clegg unsigned N = 0; 8816c16827SSam Clegg const char *Error = nullptr; 892faf0794SThomas Lively Val = Signed ? decodeSLEB128(Bytes.data() + Size, &N, 9016c16827SSam Clegg Bytes.data() + Bytes.size(), &Error) 912faf0794SThomas Lively : static_cast<int64_t>(decodeULEB128(Bytes.data() + Size, &N, 922faf0794SThomas Lively Bytes.data() + Bytes.size(), 932faf0794SThomas Lively &Error)); 9416c16827SSam Clegg if (Error) 9516c16827SSam Clegg return false; 9616c16827SSam Clegg Size += N; 972faf0794SThomas Lively return true; 982faf0794SThomas Lively } 992faf0794SThomas Lively 1002faf0794SThomas Lively static bool parseLEBImmediate(MCInst &MI, uint64_t &Size, 1012faf0794SThomas Lively ArrayRef<uint8_t> Bytes, bool Signed) { 1022faf0794SThomas Lively int64_t Val; 1032faf0794SThomas Lively if (!nextLEB(Val, Bytes, Size, Signed)) 1042faf0794SThomas Lively return false; 10516c16827SSam Clegg MI.addOperand(MCOperand::createImm(Val)); 10616c16827SSam Clegg return true; 10716c16827SSam Clegg } 10816c16827SSam Clegg 10916c16827SSam Clegg template <typename T> 11022442924SThomas Lively bool parseImmediate(MCInst &MI, uint64_t &Size, ArrayRef<uint8_t> Bytes) { 11116c16827SSam Clegg if (Size + sizeof(T) > Bytes.size()) 11216c16827SSam Clegg return false; 113df6770f0SHeejin Ahn T Val = support::endian::read<T, support::endianness::little, 1>( 1140d9f3f7fSWouter van Oortmerssen Bytes.data() + Size); 11516c16827SSam Clegg Size += sizeof(T); 11622442924SThomas Lively if (std::is_floating_point<T>::value) { 11716c16827SSam Clegg MI.addOperand(MCOperand::createFPImm(static_cast<double>(Val))); 11822442924SThomas Lively } else { 11922442924SThomas Lively MI.addOperand(MCOperand::createImm(static_cast<int64_t>(Val))); 12022442924SThomas Lively } 12116c16827SSam Clegg return true; 12216c16827SSam Clegg } 12316c16827SSam Clegg 124f3b762a0SWouter van Oortmerssen MCDisassembler::DecodeStatus WebAssemblyDisassembler::onSymbolStart( 125f3b762a0SWouter van Oortmerssen StringRef Name, uint64_t &Size, ArrayRef<uint8_t> Bytes, uint64_t Address, 126f3b762a0SWouter van Oortmerssen raw_ostream &VStream, raw_ostream &CStream) const { 127f3b762a0SWouter van Oortmerssen Size = 0; 128f3b762a0SWouter van Oortmerssen if (Address == 0) { 129f3b762a0SWouter van Oortmerssen // Start of a code section: we're parsing only the function count. 130f3b762a0SWouter van Oortmerssen int64_t FunctionCount; 131f3b762a0SWouter van Oortmerssen if (!nextLEB(FunctionCount, Bytes, Size, false)) 132f3b762a0SWouter van Oortmerssen return MCDisassembler::Fail; 133f3b762a0SWouter van Oortmerssen outs() << " # " << FunctionCount << " functions in section."; 134f3b762a0SWouter van Oortmerssen } else { 135f3b762a0SWouter van Oortmerssen // Parse the start of a single function. 136f3b762a0SWouter van Oortmerssen int64_t BodySize, LocalEntryCount; 137f3b762a0SWouter van Oortmerssen if (!nextLEB(BodySize, Bytes, Size, false) || 138f3b762a0SWouter van Oortmerssen !nextLEB(LocalEntryCount, Bytes, Size, false)) 139f3b762a0SWouter van Oortmerssen return MCDisassembler::Fail; 140f3b762a0SWouter van Oortmerssen if (LocalEntryCount) { 141f3b762a0SWouter van Oortmerssen outs() << " .local "; 142f3b762a0SWouter van Oortmerssen for (int64_t I = 0; I < LocalEntryCount; I++) { 143f3b762a0SWouter van Oortmerssen int64_t Count, Type; 144f3b762a0SWouter van Oortmerssen if (!nextLEB(Count, Bytes, Size, false) || 145f3b762a0SWouter van Oortmerssen !nextLEB(Type, Bytes, Size, false)) 146f3b762a0SWouter van Oortmerssen return MCDisassembler::Fail; 147f3b762a0SWouter van Oortmerssen for (int64_t J = 0; J < Count; J++) { 148f3b762a0SWouter van Oortmerssen if (I || J) 149f3b762a0SWouter van Oortmerssen outs() << ", "; 150f3b762a0SWouter van Oortmerssen outs() << WebAssembly::anyTypeToString(Type); 151f3b762a0SWouter van Oortmerssen } 152f3b762a0SWouter van Oortmerssen } 153f3b762a0SWouter van Oortmerssen } 154f3b762a0SWouter van Oortmerssen } 155f3b762a0SWouter van Oortmerssen outs() << "\n"; 156f3b762a0SWouter van Oortmerssen return MCDisassembler::Success; 157f3b762a0SWouter van Oortmerssen } 158f3b762a0SWouter van Oortmerssen 1591a427287SDan Gohman MCDisassembler::DecodeStatus WebAssemblyDisassembler::getInstruction( 1601a427287SDan Gohman MCInst &MI, uint64_t &Size, ArrayRef<uint8_t> Bytes, uint64_t /*Address*/, 16116c16827SSam Clegg raw_ostream & /*OS*/, raw_ostream &CS) const { 16216c16827SSam Clegg CommentStream = &CS; 16316c16827SSam Clegg Size = 0; 164fc3163b6SThomas Lively int Opc = nextByte(Bytes, Size); 16516c16827SSam Clegg if (Opc < 0) 1661a427287SDan Gohman return MCDisassembler::Fail; 16716c16827SSam Clegg const auto *WasmInst = &InstructionTable0[Opc]; 16816c16827SSam Clegg // If this is a prefix byte, indirect to another table. 16916c16827SSam Clegg if (WasmInst->ET == ET_Prefix) { 17016c16827SSam Clegg WasmInst = nullptr; 17116c16827SSam Clegg // Linear search, so far only 2 entries. 17216c16827SSam Clegg for (auto PT = PrefixTable; PT->Table; PT++) { 17316c16827SSam Clegg if (PT->Prefix == Opc) { 17416c16827SSam Clegg WasmInst = PT->Table; 17516c16827SSam Clegg break; 17616c16827SSam Clegg } 17716c16827SSam Clegg } 17816c16827SSam Clegg if (!WasmInst) 17916c16827SSam Clegg return MCDisassembler::Fail; 1802faf0794SThomas Lively int64_t PrefixedOpc; 181f3b762a0SWouter van Oortmerssen if (!nextLEB(PrefixedOpc, Bytes, Size, false)) 18216c16827SSam Clegg return MCDisassembler::Fail; 1832faf0794SThomas Lively if (PrefixedOpc < 0 || PrefixedOpc >= WebAssemblyInstructionTableSize) 1842faf0794SThomas Lively return MCDisassembler::Fail; 1852faf0794SThomas Lively WasmInst += PrefixedOpc; 18616c16827SSam Clegg } 18716c16827SSam Clegg if (WasmInst->ET == ET_Unused) 18816c16827SSam Clegg return MCDisassembler::Fail; 18916c16827SSam Clegg // At this point we must have a valid instruction to decode. 19016c16827SSam Clegg assert(WasmInst->ET == ET_Instruction); 19116c16827SSam Clegg MI.setOpcode(WasmInst->Opcode); 19216c16827SSam Clegg // Parse any operands. 19316c16827SSam Clegg for (uint8_t OPI = 0; OPI < WasmInst->NumOperands; OPI++) { 194820c6263SWouter van Oortmerssen auto OT = OperandTable[WasmInst->OperandStart + OPI]; 195820c6263SWouter van Oortmerssen switch (OT) { 19616c16827SSam Clegg // ULEB operands: 19716c16827SSam Clegg case WebAssembly::OPERAND_BASIC_BLOCK: 19816c16827SSam Clegg case WebAssembly::OPERAND_LOCAL: 19916c16827SSam Clegg case WebAssembly::OPERAND_GLOBAL: 20016c16827SSam Clegg case WebAssembly::OPERAND_FUNCTION32: 20116c16827SSam Clegg case WebAssembly::OPERAND_OFFSET32: 20216c16827SSam Clegg case WebAssembly::OPERAND_P2ALIGN: 20316c16827SSam Clegg case WebAssembly::OPERAND_TYPEINDEX: 2048a28ce1aSWouter van Oortmerssen case WebAssembly::OPERAND_EVENT: 20516c16827SSam Clegg case MCOI::OPERAND_IMMEDIATE: { 20616c16827SSam Clegg if (!parseLEBImmediate(MI, Size, Bytes, false)) 20716c16827SSam Clegg return MCDisassembler::Fail; 20816c16827SSam Clegg break; 20916c16827SSam Clegg } 21016c16827SSam Clegg // SLEB operands: 21116c16827SSam Clegg case WebAssembly::OPERAND_I32IMM: 212ad72f685SWouter van Oortmerssen case WebAssembly::OPERAND_I64IMM: { 21316c16827SSam Clegg if (!parseLEBImmediate(MI, Size, Bytes, true)) 21416c16827SSam Clegg return MCDisassembler::Fail; 21516c16827SSam Clegg break; 21616c16827SSam Clegg } 217*2cb27072SThomas Lively // block_type operands: 218ad72f685SWouter van Oortmerssen case WebAssembly::OPERAND_SIGNATURE: { 219*2cb27072SThomas Lively int64_t Val; 220*2cb27072SThomas Lively uint64_t PrevSize = Size; 221*2cb27072SThomas Lively if (!nextLEB(Val, Bytes, Size, true)) 222ad72f685SWouter van Oortmerssen return MCDisassembler::Fail; 223*2cb27072SThomas Lively if (Val < 0) { 224*2cb27072SThomas Lively // Negative values are single septet value types or empty types 225*2cb27072SThomas Lively if (Size != PrevSize + 1) { 226*2cb27072SThomas Lively MI.addOperand( 227*2cb27072SThomas Lively MCOperand::createImm(int64_t(WebAssembly::BlockType::Invalid))); 228*2cb27072SThomas Lively } else { 229*2cb27072SThomas Lively MI.addOperand(MCOperand::createImm(Val & 0x7f)); 230*2cb27072SThomas Lively } 231*2cb27072SThomas Lively } else { 232*2cb27072SThomas Lively // We don't have access to the signature, so create a symbol without one 233*2cb27072SThomas Lively MCSymbol *Sym = getContext().createTempSymbol("typeindex", true); 234*2cb27072SThomas Lively auto *WasmSym = cast<MCSymbolWasm>(Sym); 235*2cb27072SThomas Lively WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION); 236*2cb27072SThomas Lively const MCExpr *Expr = MCSymbolRefExpr::create( 237*2cb27072SThomas Lively WasmSym, MCSymbolRefExpr::VK_WASM_TYPEINDEX, getContext()); 238*2cb27072SThomas Lively MI.addOperand(MCOperand::createExpr(Expr)); 239*2cb27072SThomas Lively } 240ad72f685SWouter van Oortmerssen break; 241ad72f685SWouter van Oortmerssen } 24216c16827SSam Clegg // FP operands. 24316c16827SSam Clegg case WebAssembly::OPERAND_F32IMM: { 24422442924SThomas Lively if (!parseImmediate<float>(MI, Size, Bytes)) 24516c16827SSam Clegg return MCDisassembler::Fail; 24616c16827SSam Clegg break; 24716c16827SSam Clegg } 24816c16827SSam Clegg case WebAssembly::OPERAND_F64IMM: { 24922442924SThomas Lively if (!parseImmediate<double>(MI, Size, Bytes)) 25022442924SThomas Lively return MCDisassembler::Fail; 25122442924SThomas Lively break; 25222442924SThomas Lively } 25322442924SThomas Lively // Vector lane operands (not LEB encoded). 25422442924SThomas Lively case WebAssembly::OPERAND_VEC_I8IMM: { 25522442924SThomas Lively if (!parseImmediate<uint8_t>(MI, Size, Bytes)) 25622442924SThomas Lively return MCDisassembler::Fail; 25722442924SThomas Lively break; 25822442924SThomas Lively } 25922442924SThomas Lively case WebAssembly::OPERAND_VEC_I16IMM: { 26022442924SThomas Lively if (!parseImmediate<uint16_t>(MI, Size, Bytes)) 26122442924SThomas Lively return MCDisassembler::Fail; 26222442924SThomas Lively break; 26322442924SThomas Lively } 26422442924SThomas Lively case WebAssembly::OPERAND_VEC_I32IMM: { 26522442924SThomas Lively if (!parseImmediate<uint32_t>(MI, Size, Bytes)) 26622442924SThomas Lively return MCDisassembler::Fail; 26722442924SThomas Lively break; 26822442924SThomas Lively } 26922442924SThomas Lively case WebAssembly::OPERAND_VEC_I64IMM: { 27022442924SThomas Lively if (!parseImmediate<uint64_t>(MI, Size, Bytes)) 27116c16827SSam Clegg return MCDisassembler::Fail; 27216c16827SSam Clegg break; 27316c16827SSam Clegg } 274820c6263SWouter van Oortmerssen case WebAssembly::OPERAND_BRLIST: { 275820c6263SWouter van Oortmerssen int64_t TargetTableLen; 276820c6263SWouter van Oortmerssen if (!nextLEB(TargetTableLen, Bytes, Size, false)) 277820c6263SWouter van Oortmerssen return MCDisassembler::Fail; 278820c6263SWouter van Oortmerssen for (int64_t I = 0; I < TargetTableLen; I++) { 279820c6263SWouter van Oortmerssen if (!parseLEBImmediate(MI, Size, Bytes, false)) 280820c6263SWouter van Oortmerssen return MCDisassembler::Fail; 281820c6263SWouter van Oortmerssen } 282820c6263SWouter van Oortmerssen // Default case. 283820c6263SWouter van Oortmerssen if (!parseLEBImmediate(MI, Size, Bytes, false)) 284820c6263SWouter van Oortmerssen return MCDisassembler::Fail; 285820c6263SWouter van Oortmerssen break; 286820c6263SWouter van Oortmerssen } 287a733d08dSWouter van Oortmerssen case MCOI::OPERAND_REGISTER: 288a733d08dSWouter van Oortmerssen // The tablegen header currently does not have any register operands since 289a733d08dSWouter van Oortmerssen // we use only the stack (_S) instructions. 290a733d08dSWouter van Oortmerssen // If you hit this that probably means a bad instruction definition in 291a733d08dSWouter van Oortmerssen // tablegen. 292a733d08dSWouter van Oortmerssen llvm_unreachable("Register operand in WebAssemblyDisassembler"); 29316c16827SSam Clegg default: 29416c16827SSam Clegg llvm_unreachable("Unknown operand type in WebAssemblyDisassembler"); 29516c16827SSam Clegg } 29616c16827SSam Clegg } 29716c16827SSam Clegg return MCDisassembler::Success; 2981a427287SDan Gohman } 299