1 //==- WebAssemblyDisassembler.cpp - Disassembler for WebAssembly -*- C++ -*-==//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 ///
10 /// \file
11 /// This file is part of the WebAssembly Disassembler.
12 ///
13 /// It contains code to translate the data produced by the decoder into
14 /// MCInsts.
15 ///
16 //===----------------------------------------------------------------------===//
17
18 #include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
19 #include "WebAssembly.h"
20 #include "llvm/MC/MCContext.h"
21 #include "llvm/MC/MCDisassembler/MCDisassembler.h"
22 #include "llvm/MC/MCFixedLenDisassembler.h"
23 #include "llvm/MC/MCInst.h"
24 #include "llvm/MC/MCInstrInfo.h"
25 #include "llvm/MC/MCSubtargetInfo.h"
26 #include "llvm/MC/MCSymbol.h"
27 #include "llvm/Support/Endian.h"
28 #include "llvm/Support/LEB128.h"
29 #include "llvm/Support/TargetRegistry.h"
30
31 using namespace llvm;
32
33 #define DEBUG_TYPE "wasm-disassembler"
34
35 using DecodeStatus = MCDisassembler::DecodeStatus;
36
37 #include "WebAssemblyGenDisassemblerTables.inc"
38
39 namespace {
40 class WebAssemblyDisassembler final : public MCDisassembler {
41 std::unique_ptr<const MCInstrInfo> MCII;
42
43 DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size,
44 ArrayRef<uint8_t> Bytes, uint64_t Address,
45 raw_ostream &VStream,
46 raw_ostream &CStream) const override;
47
48 public:
WebAssemblyDisassembler(const MCSubtargetInfo & STI,MCContext & Ctx,std::unique_ptr<const MCInstrInfo> MCII)49 WebAssemblyDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx,
50 std::unique_ptr<const MCInstrInfo> MCII)
51 : MCDisassembler(STI, Ctx), MCII(std::move(MCII)) {}
52 };
53 } // end anonymous namespace
54
createWebAssemblyDisassembler(const Target & T,const MCSubtargetInfo & STI,MCContext & Ctx)55 static MCDisassembler *createWebAssemblyDisassembler(const Target &T,
56 const MCSubtargetInfo &STI,
57 MCContext &Ctx) {
58 std::unique_ptr<const MCInstrInfo> MCII(T.createMCInstrInfo());
59 return new WebAssemblyDisassembler(STI, Ctx, std::move(MCII));
60 }
61
LLVMInitializeWebAssemblyDisassembler()62 extern "C" void LLVMInitializeWebAssemblyDisassembler() {
63 // Register the disassembler for each target.
64 TargetRegistry::RegisterMCDisassembler(getTheWebAssemblyTarget32(),
65 createWebAssemblyDisassembler);
66 TargetRegistry::RegisterMCDisassembler(getTheWebAssemblyTarget64(),
67 createWebAssemblyDisassembler);
68 }
69
nextByte(ArrayRef<uint8_t> Bytes,uint64_t & Size)70 static int nextByte(ArrayRef<uint8_t> Bytes, uint64_t &Size) {
71 if (Size >= Bytes.size())
72 return -1;
73 auto V = Bytes[Size];
74 Size++;
75 return V;
76 }
77
parseLEBImmediate(MCInst & MI,uint64_t & Size,ArrayRef<uint8_t> Bytes,bool Signed)78 static bool parseLEBImmediate(MCInst &MI, uint64_t &Size,
79 ArrayRef<uint8_t> Bytes, bool Signed) {
80 unsigned N = 0;
81 const char *Error = nullptr;
82 auto Val = Signed ? decodeSLEB128(Bytes.data() + Size, &N,
83 Bytes.data() + Bytes.size(), &Error)
84 : static_cast<int64_t>(
85 decodeULEB128(Bytes.data() + Size, &N,
86 Bytes.data() + Bytes.size(), &Error));
87 if (Error)
88 return false;
89 Size += N;
90 MI.addOperand(MCOperand::createImm(Val));
91 return true;
92 }
93
94 template <typename T>
parseFPImmediate(MCInst & MI,uint64_t & Size,ArrayRef<uint8_t> Bytes)95 bool parseFPImmediate(MCInst &MI, uint64_t &Size, ArrayRef<uint8_t> Bytes) {
96 if (Size + sizeof(T) > Bytes.size())
97 return false;
98 T Val;
99 memcpy(&Val, Bytes.data() + Size, sizeof(T));
100 support::endian::byte_swap<T, support::endianness::little>(Val);
101 Size += sizeof(T);
102 MI.addOperand(MCOperand::createFPImm(static_cast<double>(Val)));
103 return true;
104 }
105
getInstruction(MCInst & MI,uint64_t & Size,ArrayRef<uint8_t> Bytes,uint64_t,raw_ostream &,raw_ostream & CS) const106 MCDisassembler::DecodeStatus WebAssemblyDisassembler::getInstruction(
107 MCInst &MI, uint64_t &Size, ArrayRef<uint8_t> Bytes, uint64_t /*Address*/,
108 raw_ostream & /*OS*/, raw_ostream &CS) const {
109 CommentStream = &CS;
110 Size = 0;
111 auto Opc = nextByte(Bytes, Size);
112 if (Opc < 0)
113 return MCDisassembler::Fail;
114 const auto *WasmInst = &InstructionTable0[Opc];
115 // If this is a prefix byte, indirect to another table.
116 if (WasmInst->ET == ET_Prefix) {
117 WasmInst = nullptr;
118 // Linear search, so far only 2 entries.
119 for (auto PT = PrefixTable; PT->Table; PT++) {
120 if (PT->Prefix == Opc) {
121 WasmInst = PT->Table;
122 break;
123 }
124 }
125 if (!WasmInst)
126 return MCDisassembler::Fail;
127 Opc = nextByte(Bytes, Size);
128 if (Opc < 0)
129 return MCDisassembler::Fail;
130 WasmInst += Opc;
131 }
132 if (WasmInst->ET == ET_Unused)
133 return MCDisassembler::Fail;
134 // At this point we must have a valid instruction to decode.
135 assert(WasmInst->ET == ET_Instruction);
136 MI.setOpcode(WasmInst->Opcode);
137 // Parse any operands.
138 for (uint8_t OPI = 0; OPI < WasmInst->NumOperands; OPI++) {
139 switch (WasmInst->Operands[OPI]) {
140 // ULEB operands:
141 case WebAssembly::OPERAND_BASIC_BLOCK:
142 case WebAssembly::OPERAND_LOCAL:
143 case WebAssembly::OPERAND_GLOBAL:
144 case WebAssembly::OPERAND_FUNCTION32:
145 case WebAssembly::OPERAND_OFFSET32:
146 case WebAssembly::OPERAND_P2ALIGN:
147 case WebAssembly::OPERAND_TYPEINDEX:
148 case MCOI::OPERAND_IMMEDIATE: {
149 if (!parseLEBImmediate(MI, Size, Bytes, false))
150 return MCDisassembler::Fail;
151 break;
152 }
153 // SLEB operands:
154 case WebAssembly::OPERAND_I32IMM:
155 case WebAssembly::OPERAND_I64IMM:
156 case WebAssembly::OPERAND_SIGNATURE: {
157 if (!parseLEBImmediate(MI, Size, Bytes, true))
158 return MCDisassembler::Fail;
159 break;
160 }
161 // FP operands.
162 case WebAssembly::OPERAND_F32IMM: {
163 if (!parseFPImmediate<float>(MI, Size, Bytes))
164 return MCDisassembler::Fail;
165 break;
166 }
167 case WebAssembly::OPERAND_F64IMM: {
168 if (!parseFPImmediate<double>(MI, Size, Bytes))
169 return MCDisassembler::Fail;
170 break;
171 }
172 case MCOI::OPERAND_REGISTER: {
173 // These are NOT actually in the instruction stream, but MC is going to
174 // expect operands to be present for them!
175 // FIXME: can MC re-generate register assignments or do we have to
176 // do this? Since this function decodes a single instruction, we don't
177 // have the proper context for tracking an operand stack here.
178 MI.addOperand(MCOperand::createReg(0));
179 break;
180 }
181 default:
182 llvm_unreachable("Unknown operand type in WebAssemblyDisassembler");
183 }
184 }
185 return MCDisassembler::Success;
186 }
187