LLVM  7.0.0
WebAssemblyDisassembler.cpp
Go to the documentation of this file.
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 
19 #include "WebAssembly.h"
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/Support/Endian.h"
28 #include "llvm/Support/LEB128.h"
30 
31 using namespace llvm;
32 
33 #define DEBUG_TYPE "wasm-disassembler"
34 
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:
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 
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 
63  // Register the disassembler for each target.
68 }
69 
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 
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;
91  return true;
92 }
93 
94 template <typename T>
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 
106 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:
149  if (!parseLEBImmediate(MI, Size, Bytes, false))
150  return MCDisassembler::Fail;
151  break;
152  }
153  // SLEB operands:
157  if (!parseLEBImmediate(MI, Size, Bytes, true))
158  return MCDisassembler::Fail;
159  break;
160  }
161  // FP operands.
163  if (!parseFPImmediate<float>(MI, Size, Bytes))
164  return MCDisassembler::Fail;
165  break;
166  }
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.
179  break;
180  }
181  default:
182  llvm_unreachable("Unknown operand type in WebAssemblyDisassembler");
183  }
184  }
186 }
32-bit floating-point immediates.
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
void LLVMInitializeWebAssemblyDisassembler()
DecodeStatus
Ternary decode status.
Superclass for all disassemblers.
32-bit unsigned memory offsets.
MCInstrInfo * createMCInstrInfo() const
createMCInstrInfo - Create a MCInstrInfo implementation.
Basic block label in a branch construct.
static void RegisterMCDisassembler(Target &T, Target::MCDisassemblerCtorTy Fn)
RegisterMCDisassembler - Register a MCDisassembler implementation for the given target.
This file contains the entry points for global functions defined in the LLVM WebAssembly back-end...
signature immediate for block/loop.
static MCOperand createReg(unsigned Reg)
Definition: MCInst.h:116
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:162
Context object for machine code objects.
Definition: MCContext.h:63
#define T
static int nextByte(ArrayRef< uint8_t > Bytes, uint64_t &Size)
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:161
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:149
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:129
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:146
void setOpcode(unsigned Op)
Definition: MCInst.h:173
static MCOperand createFPImm(double Val)
Definition: MCInst.h:130
Target - Wrapper for Target specific information.
bool parseFPImmediate(MCInst &MI, uint64_t &Size, ArrayRef< uint8_t > Bytes)
64-bit floating-point immediates.
32-bit unsigned function indices.
static MCDisassembler * createWebAssemblyDisassembler(const Target &T, const MCSubtargetInfo &STI, MCContext &Ctx)
p2align immediate for load and store address alignment.
#define N
Generic base class for all target subtargets.
static bool parseLEBImmediate(MCInst &MI, uint64_t &Size, ArrayRef< uint8_t > Bytes, bool Signed)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
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:186
Target & getTheWebAssemblyTarget32()
Target & getTheWebAssemblyTarget64()
static MCOperand createImm(int64_t Val)
Definition: MCInst.h:123