blob: 9a9c31cff2d530c874a51933da4c65bf4d175659 [file] [log] [blame]
Dan Gohman1a427282016-01-12 03:32:29 +00001//==- WebAssemblyDisassembler.cpp - Disassembler for WebAssembly -*- C++ -*-==//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Dan Gohman1a427282016-01-12 03:32:29 +00006//
7//===----------------------------------------------------------------------===//
8///
9/// \file
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000010/// This file is part of the WebAssembly Disassembler.
Dan Gohman1a427282016-01-12 03:32:29 +000011///
12/// It contains code to translate the data produced by the decoder into
13/// MCInsts.
14///
15//===----------------------------------------------------------------------===//
16
David L. Jonesa263aa22019-05-13 03:32:41 +000017#include "MCTargetDesc/WebAssemblyInstPrinter.h"
Dan Gohman1a427282016-01-12 03:32:29 +000018#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
Richard Trieuc6c42132019-05-15 01:03:00 +000019#include "TargetInfo/WebAssemblyTargetInfo.h"
Dan Gohman1a427282016-01-12 03:32:29 +000020#include "llvm/MC/MCContext.h"
Benjamin Kramerc50b8902016-01-26 18:21:38 +000021#include "llvm/MC/MCDisassembler/MCDisassembler.h"
Sam Clegg16c16822018-05-10 22:16:44 +000022#include "llvm/MC/MCFixedLenDisassembler.h"
Dan Gohman1a427282016-01-12 03:32:29 +000023#include "llvm/MC/MCInst.h"
24#include "llvm/MC/MCInstrInfo.h"
25#include "llvm/MC/MCSubtargetInfo.h"
26#include "llvm/MC/MCSymbol.h"
Thomas Lively2cb27072019-10-15 18:28:22 +000027#include "llvm/MC/MCSymbolWasm.h"
Dan Gohman1a427282016-01-12 03:32:29 +000028#include "llvm/Support/Endian.h"
Sam Clegg16c16822018-05-10 22:16:44 +000029#include "llvm/Support/LEB128.h"
Dan Gohman1a427282016-01-12 03:32:29 +000030#include "llvm/Support/TargetRegistry.h"
Sam Clegg16c16822018-05-10 22:16:44 +000031
Dan Gohman1a427282016-01-12 03:32:29 +000032using namespace llvm;
33
34#define DEBUG_TYPE "wasm-disassembler"
35
Sam Clegg16c16822018-05-10 22:16:44 +000036using DecodeStatus = MCDisassembler::DecodeStatus;
37
38#include "WebAssemblyGenDisassemblerTables.inc"
39
Dan Gohman1a427282016-01-12 03:32:29 +000040namespace {
Fangrui Song49550662018-11-09 18:32:20 +000041static constexpr int WebAssemblyInstructionTableSize = 256;
42
Dan Gohman1a427282016-01-12 03:32:29 +000043class WebAssemblyDisassembler final : public MCDisassembler {
44 std::unique_ptr<const MCInstrInfo> MCII;
45
46 DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size,
47 ArrayRef<uint8_t> Bytes, uint64_t Address,
48 raw_ostream &VStream,
49 raw_ostream &CStream) const override;
Wouter van Oortmerssenf3b762a2019-01-17 18:14:09 +000050 DecodeStatus onSymbolStart(StringRef Name, uint64_t &Size,
51 ArrayRef<uint8_t> Bytes, uint64_t Address,
52 raw_ostream &VStream,
53 raw_ostream &CStream) const override;
Dan Gohman1a427282016-01-12 03:32:29 +000054
55public:
56 WebAssemblyDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx,
57 std::unique_ptr<const MCInstrInfo> MCII)
58 : MCDisassembler(STI, Ctx), MCII(std::move(MCII)) {}
59};
60} // end anonymous namespace
61
62static MCDisassembler *createWebAssemblyDisassembler(const Target &T,
63 const MCSubtargetInfo &STI,
64 MCContext &Ctx) {
65 std::unique_ptr<const MCInstrInfo> MCII(T.createMCInstrInfo());
66 return new WebAssemblyDisassembler(STI, Ctx, std::move(MCII));
67}
68
Tom Stellard4b0b2612019-06-11 03:21:13 +000069extern "C" void LLVMInitializeWebAssemblyDisassembler() {
Dan Gohman1a427282016-01-12 03:32:29 +000070 // Register the disassembler for each target.
Mehdi Aminif42454b2016-10-09 23:00:34 +000071 TargetRegistry::RegisterMCDisassembler(getTheWebAssemblyTarget32(),
Dan Gohman1a427282016-01-12 03:32:29 +000072 createWebAssemblyDisassembler);
Mehdi Aminif42454b2016-10-09 23:00:34 +000073 TargetRegistry::RegisterMCDisassembler(getTheWebAssemblyTarget64(),
Dan Gohman1a427282016-01-12 03:32:29 +000074 createWebAssemblyDisassembler);
75}
76
Thomas Livelyfc3163b2018-11-15 18:56:49 +000077static int nextByte(ArrayRef<uint8_t> Bytes, uint64_t &Size) {
Sam Clegg16c16822018-05-10 22:16:44 +000078 if (Size >= Bytes.size())
79 return -1;
80 auto V = Bytes[Size];
81 Size++;
82 return V;
83}
84
Thomas Lively2faf0792018-11-09 01:57:00 +000085static bool nextLEB(int64_t &Val, ArrayRef<uint8_t> Bytes, uint64_t &Size,
Wouter van Oortmerssenf3b762a2019-01-17 18:14:09 +000086 bool Signed) {
Sam Clegg16c16822018-05-10 22:16:44 +000087 unsigned N = 0;
88 const char *Error = nullptr;
Thomas Lively2faf0792018-11-09 01:57:00 +000089 Val = Signed ? decodeSLEB128(Bytes.data() + Size, &N,
90 Bytes.data() + Bytes.size(), &Error)
91 : static_cast<int64_t>(decodeULEB128(Bytes.data() + Size, &N,
92 Bytes.data() + Bytes.size(),
93 &Error));
Sam Clegg16c16822018-05-10 22:16:44 +000094 if (Error)
95 return false;
96 Size += N;
Thomas Lively2faf0792018-11-09 01:57:00 +000097 return true;
98}
99
100static bool parseLEBImmediate(MCInst &MI, uint64_t &Size,
101 ArrayRef<uint8_t> Bytes, bool Signed) {
102 int64_t Val;
103 if (!nextLEB(Val, Bytes, Size, Signed))
104 return false;
Sam Clegg16c16822018-05-10 22:16:44 +0000105 MI.addOperand(MCOperand::createImm(Val));
106 return true;
107}
108
109template <typename T>
Thomas Lively22442922018-08-21 21:03:18 +0000110bool parseImmediate(MCInst &MI, uint64_t &Size, ArrayRef<uint8_t> Bytes) {
Sam Clegg16c16822018-05-10 22:16:44 +0000111 if (Size + sizeof(T) > Bytes.size())
112 return false;
Heejin Ahndf6770f2019-02-08 04:06:56 +0000113 T Val = support::endian::read<T, support::endianness::little, 1>(
Wouter van Oortmerssen0d9f3f72019-02-08 01:43:23 +0000114 Bytes.data() + Size);
Sam Clegg16c16822018-05-10 22:16:44 +0000115 Size += sizeof(T);
Thomas Lively22442922018-08-21 21:03:18 +0000116 if (std::is_floating_point<T>::value) {
117 MI.addOperand(MCOperand::createFPImm(static_cast<double>(Val)));
118 } else {
119 MI.addOperand(MCOperand::createImm(static_cast<int64_t>(Val)));
120 }
Sam Clegg16c16822018-05-10 22:16:44 +0000121 return true;
122}
123
Wouter van Oortmerssenf3b762a2019-01-17 18:14:09 +0000124MCDisassembler::DecodeStatus WebAssemblyDisassembler::onSymbolStart(
125 StringRef Name, uint64_t &Size, ArrayRef<uint8_t> Bytes, uint64_t Address,
126 raw_ostream &VStream, raw_ostream &CStream) const {
127 Size = 0;
128 if (Address == 0) {
129 // Start of a code section: we're parsing only the function count.
130 int64_t FunctionCount;
131 if (!nextLEB(FunctionCount, Bytes, Size, false))
132 return MCDisassembler::Fail;
133 outs() << " # " << FunctionCount << " functions in section.";
134 } else {
135 // Parse the start of a single function.
136 int64_t BodySize, LocalEntryCount;
137 if (!nextLEB(BodySize, Bytes, Size, false) ||
138 !nextLEB(LocalEntryCount, Bytes, Size, false))
139 return MCDisassembler::Fail;
140 if (LocalEntryCount) {
141 outs() << " .local ";
142 for (int64_t I = 0; I < LocalEntryCount; I++) {
143 int64_t Count, Type;
144 if (!nextLEB(Count, Bytes, Size, false) ||
145 !nextLEB(Type, Bytes, Size, false))
146 return MCDisassembler::Fail;
147 for (int64_t J = 0; J < Count; J++) {
148 if (I || J)
149 outs() << ", ";
150 outs() << WebAssembly::anyTypeToString(Type);
151 }
152 }
153 }
154 }
155 outs() << "\n";
156 return MCDisassembler::Success;
157}
158
Dan Gohman1a427282016-01-12 03:32:29 +0000159MCDisassembler::DecodeStatus WebAssemblyDisassembler::getInstruction(
160 MCInst &MI, uint64_t &Size, ArrayRef<uint8_t> Bytes, uint64_t /*Address*/,
Sam Clegg16c16822018-05-10 22:16:44 +0000161 raw_ostream & /*OS*/, raw_ostream &CS) const {
162 CommentStream = &CS;
163 Size = 0;
Thomas Livelyfc3163b2018-11-15 18:56:49 +0000164 int Opc = nextByte(Bytes, Size);
Sam Clegg16c16822018-05-10 22:16:44 +0000165 if (Opc < 0)
166 return MCDisassembler::Fail;
167 const auto *WasmInst = &InstructionTable0[Opc];
168 // If this is a prefix byte, indirect to another table.
169 if (WasmInst->ET == ET_Prefix) {
170 WasmInst = nullptr;
171 // Linear search, so far only 2 entries.
172 for (auto PT = PrefixTable; PT->Table; PT++) {
173 if (PT->Prefix == Opc) {
174 WasmInst = PT->Table;
175 break;
176 }
177 }
178 if (!WasmInst)
179 return MCDisassembler::Fail;
Thomas Lively2faf0792018-11-09 01:57:00 +0000180 int64_t PrefixedOpc;
Wouter van Oortmerssenf3b762a2019-01-17 18:14:09 +0000181 if (!nextLEB(PrefixedOpc, Bytes, Size, false))
Sam Clegg16c16822018-05-10 22:16:44 +0000182 return MCDisassembler::Fail;
Thomas Lively2faf0792018-11-09 01:57:00 +0000183 if (PrefixedOpc < 0 || PrefixedOpc >= WebAssemblyInstructionTableSize)
184 return MCDisassembler::Fail;
185 WasmInst += PrefixedOpc;
Sam Clegg16c16822018-05-10 22:16:44 +0000186 }
187 if (WasmInst->ET == ET_Unused)
188 return MCDisassembler::Fail;
189 // At this point we must have a valid instruction to decode.
190 assert(WasmInst->ET == ET_Instruction);
191 MI.setOpcode(WasmInst->Opcode);
192 // Parse any operands.
193 for (uint8_t OPI = 0; OPI < WasmInst->NumOperands; OPI++) {
Wouter van Oortmerssen820c6262019-01-03 23:01:30 +0000194 auto OT = OperandTable[WasmInst->OperandStart + OPI];
195 switch (OT) {
Sam Clegg16c16822018-05-10 22:16:44 +0000196 // ULEB operands:
197 case WebAssembly::OPERAND_BASIC_BLOCK:
198 case WebAssembly::OPERAND_LOCAL:
199 case WebAssembly::OPERAND_GLOBAL:
200 case WebAssembly::OPERAND_FUNCTION32:
201 case WebAssembly::OPERAND_OFFSET32:
202 case WebAssembly::OPERAND_P2ALIGN:
203 case WebAssembly::OPERAND_TYPEINDEX:
Wouter van Oortmerssen8a28ce12019-02-20 00:55:59 +0000204 case WebAssembly::OPERAND_EVENT:
Sam Clegg16c16822018-05-10 22:16:44 +0000205 case MCOI::OPERAND_IMMEDIATE: {
206 if (!parseLEBImmediate(MI, Size, Bytes, false))
207 return MCDisassembler::Fail;
208 break;
209 }
210 // SLEB operands:
211 case WebAssembly::OPERAND_I32IMM:
Wouter van Oortmerssenad72f682019-01-02 23:23:51 +0000212 case WebAssembly::OPERAND_I64IMM: {
Sam Clegg16c16822018-05-10 22:16:44 +0000213 if (!parseLEBImmediate(MI, Size, Bytes, true))
214 return MCDisassembler::Fail;
215 break;
216 }
Thomas Lively2cb27072019-10-15 18:28:22 +0000217 // block_type operands:
Wouter van Oortmerssenad72f682019-01-02 23:23:51 +0000218 case WebAssembly::OPERAND_SIGNATURE: {
Thomas Lively2cb27072019-10-15 18:28:22 +0000219 int64_t Val;
220 uint64_t PrevSize = Size;
221 if (!nextLEB(Val, Bytes, Size, true))
Wouter van Oortmerssenad72f682019-01-02 23:23:51 +0000222 return MCDisassembler::Fail;
Thomas Lively2cb27072019-10-15 18:28:22 +0000223 if (Val < 0) {
224 // Negative values are single septet value types or empty types
225 if (Size != PrevSize + 1) {
226 MI.addOperand(
227 MCOperand::createImm(int64_t(WebAssembly::BlockType::Invalid)));
228 } else {
229 MI.addOperand(MCOperand::createImm(Val & 0x7f));
230 }
231 } else {
232 // We don't have access to the signature, so create a symbol without one
233 MCSymbol *Sym = getContext().createTempSymbol("typeindex", true);
234 auto *WasmSym = cast<MCSymbolWasm>(Sym);
235 WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
236 const MCExpr *Expr = MCSymbolRefExpr::create(
237 WasmSym, MCSymbolRefExpr::VK_WASM_TYPEINDEX, getContext());
238 MI.addOperand(MCOperand::createExpr(Expr));
239 }
Wouter van Oortmerssenad72f682019-01-02 23:23:51 +0000240 break;
241 }
Sam Clegg16c16822018-05-10 22:16:44 +0000242 // FP operands.
243 case WebAssembly::OPERAND_F32IMM: {
Thomas Lively22442922018-08-21 21:03:18 +0000244 if (!parseImmediate<float>(MI, Size, Bytes))
Sam Clegg16c16822018-05-10 22:16:44 +0000245 return MCDisassembler::Fail;
246 break;
247 }
248 case WebAssembly::OPERAND_F64IMM: {
Thomas Lively22442922018-08-21 21:03:18 +0000249 if (!parseImmediate<double>(MI, Size, Bytes))
250 return MCDisassembler::Fail;
251 break;
252 }
253 // Vector lane operands (not LEB encoded).
254 case WebAssembly::OPERAND_VEC_I8IMM: {
255 if (!parseImmediate<uint8_t>(MI, Size, Bytes))
256 return MCDisassembler::Fail;
257 break;
258 }
259 case WebAssembly::OPERAND_VEC_I16IMM: {
260 if (!parseImmediate<uint16_t>(MI, Size, Bytes))
261 return MCDisassembler::Fail;
262 break;
263 }
264 case WebAssembly::OPERAND_VEC_I32IMM: {
265 if (!parseImmediate<uint32_t>(MI, Size, Bytes))
266 return MCDisassembler::Fail;
267 break;
268 }
269 case WebAssembly::OPERAND_VEC_I64IMM: {
270 if (!parseImmediate<uint64_t>(MI, Size, Bytes))
Sam Clegg16c16822018-05-10 22:16:44 +0000271 return MCDisassembler::Fail;
272 break;
273 }
Wouter van Oortmerssen820c6262019-01-03 23:01:30 +0000274 case WebAssembly::OPERAND_BRLIST: {
275 int64_t TargetTableLen;
276 if (!nextLEB(TargetTableLen, Bytes, Size, false))
277 return MCDisassembler::Fail;
278 for (int64_t I = 0; I < TargetTableLen; I++) {
279 if (!parseLEBImmediate(MI, Size, Bytes, false))
280 return MCDisassembler::Fail;
281 }
282 // Default case.
283 if (!parseLEBImmediate(MI, Size, Bytes, false))
284 return MCDisassembler::Fail;
285 break;
286 }
Wouter van Oortmerssena733d082018-08-30 15:40:53 +0000287 case MCOI::OPERAND_REGISTER:
288 // The tablegen header currently does not have any register operands since
289 // we use only the stack (_S) instructions.
290 // If you hit this that probably means a bad instruction definition in
291 // tablegen.
292 llvm_unreachable("Register operand in WebAssemblyDisassembler");
Sam Clegg16c16822018-05-10 22:16:44 +0000293 default:
294 llvm_unreachable("Unknown operand type in WebAssemblyDisassembler");
295 }
296 }
297 return MCDisassembler::Success;
Dan Gohman1a427282016-01-12 03:32:29 +0000298}