LLVM  12.0.0git
WebAssemblyDisassembler.cpp
Go to the documentation of this file.
1 //==- WebAssemblyDisassembler.cpp - Disassembler for WebAssembly -*- C++ -*-==//
2 //
3 // 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
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 /// This file is part of the WebAssembly Disassembler.
11 ///
12 /// It contains code to translate the data produced by the decoder into
13 /// MCInsts.
14 ///
15 //===----------------------------------------------------------------------===//
16 
20 #include "llvm/MC/MCContext.h"
23 #include "llvm/MC/MCInst.h"
24 #include "llvm/MC/MCInstrInfo.h"
26 #include "llvm/MC/MCSymbol.h"
27 #include "llvm/MC/MCSymbolWasm.h"
28 #include "llvm/Support/Endian.h"
29 #include "llvm/Support/LEB128.h"
31 
32 using namespace llvm;
33 
34 #define DEBUG_TYPE "wasm-disassembler"
35 
37 
38 #include "WebAssemblyGenDisassemblerTables.inc"
39 
40 namespace {
41 static constexpr int WebAssemblyInstructionTableSize = 256;
42 
43 class 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 &CStream) const override;
49  Optional<DecodeStatus> onSymbolStart(SymbolInfoTy &Symbol, uint64_t &Size,
50  ArrayRef<uint8_t> Bytes,
51  uint64_t Address,
52  raw_ostream &CStream) const override;
53 
54 public:
55  WebAssemblyDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx,
56  std::unique_ptr<const MCInstrInfo> MCII)
57  : MCDisassembler(STI, Ctx), MCII(std::move(MCII)) {}
58 };
59 } // end anonymous namespace
60 
62  const MCSubtargetInfo &STI,
63  MCContext &Ctx) {
64  std::unique_ptr<const MCInstrInfo> MCII(T.createMCInstrInfo());
65  return new WebAssemblyDisassembler(STI, Ctx, std::move(MCII));
66 }
67 
68 extern "C" LLVM_EXTERNAL_VISIBILITY void
70  // Register the disassembler for each target.
75 }
76 
77 static int nextByte(ArrayRef<uint8_t> Bytes, uint64_t &Size) {
78  if (Size >= Bytes.size())
79  return -1;
80  auto V = Bytes[Size];
81  Size++;
82  return V;
83 }
84 
85 static bool nextLEB(int64_t &Val, ArrayRef<uint8_t> Bytes, uint64_t &Size,
86  bool Signed) {
87  unsigned N = 0;
88  const char *Error = nullptr;
89  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));
94  if (Error)
95  return false;
96  Size += N;
97  return true;
98 }
99 
100 static 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;
106  return true;
107 }
108 
109 template <typename T>
110 bool parseImmediate(MCInst &MI, uint64_t &Size, ArrayRef<uint8_t> Bytes) {
111  if (Size + sizeof(T) > Bytes.size())
112  return false;
113  T Val = support::endian::read<T, support::endianness::little, 1>(
114  Bytes.data() + Size);
115  Size += sizeof(T);
116  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  }
121  return true;
122 }
123 
124 Optional<MCDisassembler::DecodeStatus> WebAssemblyDisassembler::onSymbolStart(
125  SymbolInfoTy &Symbol, uint64_t &Size, ArrayRef<uint8_t> Bytes,
126  uint64_t Address, 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 None;
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 None;
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 None;
147  for (int64_t J = 0; J < Count; J++) {
148  if (I || J)
149  outs() << ", ";
151  }
152  }
153  }
154  }
155  outs() << "\n";
157 }
158 
159 MCDisassembler::DecodeStatus WebAssemblyDisassembler::getInstruction(
160  MCInst &MI, uint64_t &Size, ArrayRef<uint8_t> Bytes, uint64_t /*Address*/,
161  raw_ostream &CS) const {
162  CommentStream = &CS;
163  Size = 0;
164  int Opc = nextByte(Bytes, Size);
165  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;
180  int64_t PrefixedOpc;
181  if (!nextLEB(PrefixedOpc, Bytes, Size, false))
182  return MCDisassembler::Fail;
183  if (PrefixedOpc < 0 || PrefixedOpc >= WebAssemblyInstructionTableSize)
184  return MCDisassembler::Fail;
185  WasmInst += PrefixedOpc;
186  }
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++) {
194  auto OT = OperandTable[WasmInst->OperandStart + OPI];
195  switch (OT) {
196  // ULEB operands:
207  if (!parseLEBImmediate(MI, Size, Bytes, false))
208  return MCDisassembler::Fail;
209  break;
210  }
211  // SLEB operands:
214  if (!parseLEBImmediate(MI, Size, Bytes, true))
215  return MCDisassembler::Fail;
216  break;
217  }
218  // block_type operands:
220  int64_t Val;
221  uint64_t PrevSize = Size;
222  if (!nextLEB(Val, Bytes, Size, true))
223  return MCDisassembler::Fail;
224  if (Val < 0) {
225  // Negative values are single septet value types or empty types
226  if (Size != PrevSize + 1) {
227  MI.addOperand(
229  } else {
230  MI.addOperand(MCOperand::createImm(Val & 0x7f));
231  }
232  } else {
233  // We don't have access to the signature, so create a symbol without one
234  MCSymbol *Sym = getContext().createTempSymbol("typeindex", true);
235  auto *WasmSym = cast<MCSymbolWasm>(Sym);
236  WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
237  const MCExpr *Expr = MCSymbolRefExpr::create(
238  WasmSym, MCSymbolRefExpr::VK_WASM_TYPEINDEX, getContext());
240  }
241  break;
242  }
243  // FP operands.
245  if (!parseImmediate<float>(MI, Size, Bytes))
246  return MCDisassembler::Fail;
247  break;
248  }
250  if (!parseImmediate<double>(MI, Size, Bytes))
251  return MCDisassembler::Fail;
252  break;
253  }
254  // Vector lane operands (not LEB encoded).
256  if (!parseImmediate<uint8_t>(MI, Size, Bytes))
257  return MCDisassembler::Fail;
258  break;
259  }
261  if (!parseImmediate<uint16_t>(MI, Size, Bytes))
262  return MCDisassembler::Fail;
263  break;
264  }
266  if (!parseImmediate<uint32_t>(MI, Size, Bytes))
267  return MCDisassembler::Fail;
268  break;
269  }
271  if (!parseImmediate<uint64_t>(MI, Size, Bytes))
272  return MCDisassembler::Fail;
273  break;
274  }
276  int64_t TargetTableLen;
277  if (!nextLEB(TargetTableLen, Bytes, Size, false))
278  return MCDisassembler::Fail;
279  for (int64_t I = 0; I < TargetTableLen; I++) {
280  if (!parseLEBImmediate(MI, Size, Bytes, false))
281  return MCDisassembler::Fail;
282  }
283  // Default case.
284  if (!parseLEBImmediate(MI, Size, Bytes, false))
285  return MCDisassembler::Fail;
286  break;
287  }
289  // The tablegen header currently does not have any register operands since
290  // we use only the stack (_S) instructions.
291  // If you hit this that probably means a bad instruction definition in
292  // tablegen.
293  llvm_unreachable("Register operand in WebAssemblyDisassembler");
294  default:
295  llvm_unreachable("Unknown operand type in WebAssemblyDisassembler");
296  }
297  }
299 }
32-bit floating-point immediates.
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:384
This class represents lattice values for constants.
Definition: AllocatorList.h:23
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
DecodeStatus
Ternary decode status.
static MCOperand createExpr(const MCExpr *Val)
Definition: MCInst.h:136
Superclass for all disassemblers.
This class prints an WebAssembly MCInst to wasm file syntax.
32-bit unsigned memory offsets.
MCInstrInfo * createMCInstrInfo() const
createMCInstrInfo - Create a MCInstrInfo implementation.
Basic block label in a branch construct.
const char * anyTypeToString(unsigned Ty)
static void RegisterMCDisassembler(Target &T, Target::MCDisassemblerCtorTy Fn)
RegisterMCDisassembler - Register a MCDisassembler implementation for the given target.
A list of branch targets for br_list.
This file registers the WebAssembly target.
signature immediate for block/loop.
int64_t decodeSLEB128(const uint8_t *p, unsigned *n=nullptr, const uint8_t *end=nullptr, const char **error=nullptr)
Utility function to decode a SLEB128 value.
Definition: LEB128.h:161
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
Context object for machine code objects.
Definition: MCContext.h:67
static int nextByte(ArrayRef< uint8_t > Bytes, uint64_t &Size)
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:158
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:156
This file provides WebAssembly-specific target descriptions.
uint64_t decodeULEB128(const uint8_t *p, unsigned *n=nullptr, const uint8_t *end=nullptr, const char **error=nullptr)
Utility function to decode a ULEB128 value.
Definition: LEB128.h:128
type signature immediate for call_indirect.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
const T * data() const
Definition: ArrayRef.h:153
static bool nextLEB(int64_t &Val, ArrayRef< uint8_t > Bytes, uint64_t &Size, bool Signed)
void setOpcode(unsigned Op)
Definition: MCInst.h:171
#define LLVM_EXTERNAL_VISIBILITY
Definition: Compiler.h:131
static MCOperand createFPImm(double Val)
Definition: MCInst.h:129
Target - Wrapper for Target specific information.
64-bit floating-point immediates.
32-bit unsigned function indices.
raw_fd_ostream & outs()
This returns a reference to a raw_fd_ostream for standard output.
static MCDisassembler * createWebAssemblyDisassembler(const Target &T, const MCSubtargetInfo &STI, MCContext &Ctx)
p2align immediate for load and store address alignment.
LLVM_EXTERNAL_VISIBILITY void LLVMInitializeWebAssemblyDisassembler()
#define I(x, y, z)
Definition: MD5.cpp:59
#define N
Generic base class for all target subtargets.
static bool parseLEBImmediate(MCInst &MI, uint64_t &Size, ArrayRef< uint8_t > Bytes, bool Signed)
uint32_t Size
Definition: Profile.cpp:46
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
64-bit unsigned memory offsets.
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
IRTranslator LLVM IR MI
void addOperand(const MCOperand &Op)
Definition: MCInst.h:184
Target & getTheWebAssemblyTarget32()
Target & getTheWebAssemblyTarget64()
static MCOperand createImm(int64_t Val)
Definition: MCInst.h:122
bool parseImmediate(MCInst &MI, uint64_t &Size, ArrayRef< uint8_t > Bytes)