LLVM: lib/Target/WebAssembly/Disassembler/WebAssemblyDisassembler.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
32
33using namespace llvm;
34
35#define DEBUG_TYPE "wasm-disassembler"
36
38
39#include "WebAssemblyGenDisassemblerTables.inc"
40
42
43namespace {
44class WebAssemblyDisassembler final : public MCDisassembler {
45 std::unique_ptr MCII;
46
50
53 uint64_t Address) const override;
54
55public:
57 std::unique_ptr MCII)
59};
60}
61
65 std::unique_ptr MCII(T.createMCInstrInfo());
66 return new WebAssemblyDisassembler(STI, Ctx, std::move(MCII));
67}
68
77
80 return -1;
81 auto V = Bytes[Size];
83 return V;
84}
85
88 unsigned N = 0;
89 const char *Error = nullptr;
96 return false;
98 return true;
99}
100
103 int64_t Val;
105 return false;
107 return true;
108}
109
110template
112 if (Size + sizeof(T) > Bytes.size())
113 return false;
114 T Val =
117 if (std::is_floating_point::value) {
118 MI.addOperand(
120 } else {
122 }
123 return true;
124}
125
132
133 int64_t FunctionCount;
134 if ((FunctionCount, Bytes, Size, false))
135 return false;
136 outs() << " # " << FunctionCount << " functions in section.";
137 } else {
138
139 int64_t BodySize, LocalEntryCount;
140 if ((BodySize, Bytes, Size, false) ||
141 (LocalEntryCount, Bytes, Size, false))
142 return false;
143 if (LocalEntryCount) {
144 outs() << " .local ";
145 for (int64_t I = 0; I < LocalEntryCount; I++) {
149 return false;
150 for (int64_t J = 0; J < Count; J++) {
151 if (I || J)
152 outs() << ", ";
154 }
155 }
156 }
157 }
158 outs() << "\n";
159 return true;
160}
161
163 MCInst &MI, uint64_t &Size, ArrayRef<uint8_t> Bytes, uint64_t ,
164 raw_ostream &CS) const {
165 CommentStream = &CS;
168 if (Opc < 0)
170 const auto *WasmInst = &InstructionTable0[Opc];
171
172 if (WasmInst->ET == ET_Prefix) {
173 WasmInst = nullptr;
174
175 for (const auto &[Prefix, Table] : PrefixTable) {
176 if (Prefix == Opc) {
177 WasmInst = Table;
178 break;
179 }
180 }
181 if (!WasmInst)
183 int64_t PrefixedOpc;
184 if ((PrefixedOpc, Bytes, Size, false))
188 WasmInst += PrefixedOpc;
189 }
190 if (WasmInst->ET == ET_Unused)
192
193 assert(WasmInst->ET == ET_Instruction);
194 MI.setOpcode(WasmInst->Opcode);
195
196 for (uint8_t OPI = 0; OPI < WasmInst->NumOperands; OPI++) {
197 auto OT = OperandTable[WasmInst->OperandStart + OPI];
198 switch (OT) {
199
213 break;
214 }
215
220 break;
221 }
222
224 int64_t Val;
225 uint64_t PrevSize = Size;
228 if (Val < 0) {
229
230 if (Size != PrevSize + 1) {
231 MI.addOperand(
233 } else {
235 }
236 } else {
237
239 auto *WasmSym = static_cast<MCSymbolWasm *>(Sym);
244 }
245 break;
246 }
247
251 break;
252 }
256 break;
257 }
258
262 break;
263 }
267 break;
268 }
272 break;
273 }
277 break;
278 }
280 int64_t TargetTableLen;
281 if ((TargetTableLen, Bytes, Size, false))
283 for (int64_t I = 0; I < TargetTableLen; I++) {
286 }
287
290 break;
291 }
295 int64_t NumCatches = MI.getOperand(MI.getNumOperands() - 1).getImm();
296 for (int64_t I = 0; I < NumCatches; I++) {
299 int64_t CatchOpcode = MI.getOperand(MI.getNumOperands() - 1).getImm();
304 }
307 }
308 break;
309 }
311
312
313
314
315 llvm_unreachable("Register operand in WebAssemblyDisassembler");
316 default:
317 llvm_unreachable("Unknown operand type in WebAssemblyDisassembler");
318 }
319 }
321}
MCDisassembler::DecodeStatus DecodeStatus
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define LLVM_EXTERNAL_VISIBILITY
static constexpr int WebAssemblyInstructionTableSize
Definition WebAssemblyDisassembler.cpp:41
static int nextByte(ArrayRef< uint8_t > Bytes, uint64_t &Size)
Definition WebAssemblyDisassembler.cpp:78
static bool nextLEB(int64_t &Val, ArrayRef< uint8_t > Bytes, uint64_t &Size, bool Signed)
Definition WebAssemblyDisassembler.cpp:86
static MCDisassembler * createWebAssemblyDisassembler(const Target &T, const MCSubtargetInfo &STI, MCContext &Ctx)
Definition WebAssemblyDisassembler.cpp:62
LLVM_ABI LLVM_EXTERNAL_VISIBILITY void LLVMInitializeWebAssemblyDisassembler()
Definition WebAssemblyDisassembler.cpp:70
bool parseImmediate(MCInst &MI, uint64_t &Size, ArrayRef< uint8_t > Bytes)
Definition WebAssemblyDisassembler.cpp:111
static bool parseLEBImmediate(MCInst &MI, uint64_t &Size, ArrayRef< uint8_t > Bytes, bool Signed)
Definition WebAssemblyDisassembler.cpp:101
This file contains the declaration of the WebAssemblyMCAsmInfo class.
This file contains the declaration of the WebAssembly-specific type parsing utility functions.
This file registers the WebAssembly target.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
size_t size() const
size - Get the array size.
Lightweight error class with error context and mandatory checking.
Tagged union holding either a T or a Error.
Context object for machine code objects.
Superclass for all disassemblers.
DecodeStatus
Ternary decode status.
Instances of this class represent a single low-level machine instruction.
static MCOperand createExpr(const MCExpr *Val)
static MCOperand createImm(int64_t Val)
static MCOperand createDFPImm(uint64_t Val)
Generic base class for all target subtargets.
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx, SMLoc Loc=SMLoc())
Target - Wrapper for Target specific information.
This class implements an extremely fast bulk output stream that can only output to a stream.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ OPERAND_GLOBAL
Global index.
@ OPERAND_CATCH_LIST
A list of catch clauses for try_table.
@ OPERAND_OFFSET64
64-bit unsigned memory offsets.
@ OPERAND_I32IMM
32-bit integer immediates.
@ OPERAND_P2ALIGN
p2align immediate for load and store address alignment.
@ OPERAND_TABLE
32-bit unsigned table number.
@ OPERAND_LOCAL
Local index.
@ OPERAND_VEC_I64IMM
64-bit vector lane immediate
@ OPERAND_VEC_I16IMM
16-bit vector lane immediate
@ OPERAND_TYPEINDEX
type signature immediate for call_indirect.
@ OPERAND_FUNCTION32
32-bit unsigned function indices.
@ OPERAND_F32IMM
32-bit floating-point immediates.
@ OPERAND_BASIC_BLOCK
Basic block label in a branch construct.
@ OPERAND_VEC_I32IMM
32-bit vector lane immediate
@ OPERAND_BRLIST
A list of branch targets for br_list.
@ OPERAND_F64IMM
64-bit floating-point immediates.
@ OPERAND_VEC_I8IMM
8-bit vector lane immediate
@ OPERAND_SIGNATURE
signature immediate for block/loop.
@ OPERAND_I64IMM
64-bit integer immediates.
@ OPERAND_OFFSET32
32-bit unsigned memory offsets.
const char * anyTypeToString(unsigned Type)
Context & getContext() const
value_type read(const void *memory, endianness endian)
Read a value of a particular endianness from memory.
@ WASM_SYMBOL_TYPE_SECTION
@ WASM_SYMBOL_TYPE_FUNCTION
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI raw_fd_ostream & outs()
This returns a reference to a raw_fd_ostream for standard output.
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.
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.
Target & getTheWebAssemblyTarget32()
FunctionAddr VTableAddr Count
Target & getTheWebAssemblyTarget64()
To bit_cast(const From &from) noexcept
static void RegisterMCDisassembler(Target &T, Target::MCDisassemblerCtorTy Fn)
RegisterMCDisassembler - Register a MCDisassembler implementation for the given target.