|
19 | 19 | #include "WebAssembly.h"
|
20 | 20 | #include "llvm/MC/MCContext.h"
|
21 | 21 | #include "llvm/MC/MCDisassembler/MCDisassembler.h"
|
| 22 | +#include "llvm/MC/MCFixedLenDisassembler.h" |
22 | 23 | #include "llvm/MC/MCInst.h"
|
23 | 24 | #include "llvm/MC/MCInstrInfo.h"
|
24 | 25 | #include "llvm/MC/MCSubtargetInfo.h"
|
25 | 26 | #include "llvm/MC/MCSymbol.h"
|
26 | 27 | #include "llvm/Support/Endian.h"
|
| 28 | +#include "llvm/Support/LEB128.h" |
27 | 29 | #include "llvm/Support/TargetRegistry.h"
|
| 30 | + |
28 | 31 | using namespace llvm;
|
29 | 32 |
|
30 | 33 | #define DEBUG_TYPE "wasm-disassembler"
|
31 | 34 |
|
| 35 | +using DecodeStatus = MCDisassembler::DecodeStatus; |
| 36 | + |
| 37 | +#include "WebAssemblyGenDisassemblerTables.inc" |
| 38 | + |
32 | 39 | namespace {
|
33 | 40 | class WebAssemblyDisassembler final : public MCDisassembler {
|
34 | 41 | std::unique_ptr<const MCInstrInfo> MCII;
|
@@ -60,11 +67,120 @@ extern "C" void LLVMInitializeWebAssemblyDisassembler() {
|
60 | 67 | createWebAssemblyDisassembler);
|
61 | 68 | }
|
62 | 69 |
|
63 |
| -MCDisassembler::DecodeStatus WebAssemblyDisassembler::getInstruction( |
64 |
| - MCInst &MI, uint64_t &Size, ArrayRef<uint8_t> Bytes, uint64_t /*Address*/, |
65 |
| - raw_ostream &OS, raw_ostream &CS) const { |
| 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 | +} |
66 | 77 |
|
67 |
| - // TODO: Implement disassembly. |
| 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> |
| 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 | +} |
68 | 105 |
|
69 |
| - return MCDisassembler::Fail; |
| 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: |
| 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; |
70 | 186 | }
|
0 commit comments