Dan Gohman | 1a42728 | 2016-01-12 03:32:29 +0000 | [diff] [blame] | 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 |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 11 | /// This file is part of the WebAssembly Disassembler. |
Dan Gohman | 1a42728 | 2016-01-12 03:32:29 +0000 | [diff] [blame] | 12 | /// |
| 13 | /// It contains code to translate the data produced by the decoder into |
| 14 | /// MCInsts. |
| 15 | /// |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
Wouter van Oortmerssen | f3b762a | 2019-01-17 18:14:09 +0000 | [diff] [blame^] | 18 | #include "InstPrinter/WebAssemblyInstPrinter.h" |
Dan Gohman | 1a42728 | 2016-01-12 03:32:29 +0000 | [diff] [blame] | 19 | #include "MCTargetDesc/WebAssemblyMCTargetDesc.h" |
| 20 | #include "llvm/MC/MCContext.h" |
Benjamin Kramer | c50b890 | 2016-01-26 18:21:38 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCDisassembler/MCDisassembler.h" |
Sam Clegg | 16c1682 | 2018-05-10 22:16:44 +0000 | [diff] [blame] | 22 | #include "llvm/MC/MCFixedLenDisassembler.h" |
Dan Gohman | 1a42728 | 2016-01-12 03:32:29 +0000 | [diff] [blame] | 23 | #include "llvm/MC/MCInst.h" |
| 24 | #include "llvm/MC/MCInstrInfo.h" |
| 25 | #include "llvm/MC/MCSubtargetInfo.h" |
| 26 | #include "llvm/MC/MCSymbol.h" |
| 27 | #include "llvm/Support/Endian.h" |
Sam Clegg | 16c1682 | 2018-05-10 22:16:44 +0000 | [diff] [blame] | 28 | #include "llvm/Support/LEB128.h" |
Dan Gohman | 1a42728 | 2016-01-12 03:32:29 +0000 | [diff] [blame] | 29 | #include "llvm/Support/TargetRegistry.h" |
Sam Clegg | 16c1682 | 2018-05-10 22:16:44 +0000 | [diff] [blame] | 30 | |
Dan Gohman | 1a42728 | 2016-01-12 03:32:29 +0000 | [diff] [blame] | 31 | using namespace llvm; |
| 32 | |
| 33 | #define DEBUG_TYPE "wasm-disassembler" |
| 34 | |
Sam Clegg | 16c1682 | 2018-05-10 22:16:44 +0000 | [diff] [blame] | 35 | using DecodeStatus = MCDisassembler::DecodeStatus; |
| 36 | |
| 37 | #include "WebAssemblyGenDisassemblerTables.inc" |
| 38 | |
Dan Gohman | 1a42728 | 2016-01-12 03:32:29 +0000 | [diff] [blame] | 39 | namespace { |
Fangrui Song | 4955066 | 2018-11-09 18:32:20 +0000 | [diff] [blame] | 40 | static constexpr int WebAssemblyInstructionTableSize = 256; |
| 41 | |
Dan Gohman | 1a42728 | 2016-01-12 03:32:29 +0000 | [diff] [blame] | 42 | class WebAssemblyDisassembler final : public MCDisassembler { |
| 43 | std::unique_ptr<const MCInstrInfo> MCII; |
| 44 | |
| 45 | DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size, |
| 46 | ArrayRef<uint8_t> Bytes, uint64_t Address, |
| 47 | raw_ostream &VStream, |
| 48 | raw_ostream &CStream) const override; |
Wouter van Oortmerssen | f3b762a | 2019-01-17 18:14:09 +0000 | [diff] [blame^] | 49 | DecodeStatus onSymbolStart(StringRef Name, uint64_t &Size, |
| 50 | ArrayRef<uint8_t> Bytes, uint64_t Address, |
| 51 | raw_ostream &VStream, |
| 52 | raw_ostream &CStream) const override; |
Dan Gohman | 1a42728 | 2016-01-12 03:32:29 +0000 | [diff] [blame] | 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 | |
| 61 | static MCDisassembler *createWebAssemblyDisassembler(const Target &T, |
| 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" void LLVMInitializeWebAssemblyDisassembler() { |
| 69 | // Register the disassembler for each target. |
Mehdi Amini | f42454b | 2016-10-09 23:00:34 +0000 | [diff] [blame] | 70 | TargetRegistry::RegisterMCDisassembler(getTheWebAssemblyTarget32(), |
Dan Gohman | 1a42728 | 2016-01-12 03:32:29 +0000 | [diff] [blame] | 71 | createWebAssemblyDisassembler); |
Mehdi Amini | f42454b | 2016-10-09 23:00:34 +0000 | [diff] [blame] | 72 | TargetRegistry::RegisterMCDisassembler(getTheWebAssemblyTarget64(), |
Dan Gohman | 1a42728 | 2016-01-12 03:32:29 +0000 | [diff] [blame] | 73 | createWebAssemblyDisassembler); |
| 74 | } |
| 75 | |
Thomas Lively | fc3163b | 2018-11-15 18:56:49 +0000 | [diff] [blame] | 76 | static int nextByte(ArrayRef<uint8_t> Bytes, uint64_t &Size) { |
Sam Clegg | 16c1682 | 2018-05-10 22:16:44 +0000 | [diff] [blame] | 77 | if (Size >= Bytes.size()) |
| 78 | return -1; |
| 79 | auto V = Bytes[Size]; |
| 80 | Size++; |
| 81 | return V; |
| 82 | } |
| 83 | |
Thomas Lively | 2faf079 | 2018-11-09 01:57:00 +0000 | [diff] [blame] | 84 | static bool nextLEB(int64_t &Val, ArrayRef<uint8_t> Bytes, uint64_t &Size, |
Wouter van Oortmerssen | f3b762a | 2019-01-17 18:14:09 +0000 | [diff] [blame^] | 85 | bool Signed) { |
Sam Clegg | 16c1682 | 2018-05-10 22:16:44 +0000 | [diff] [blame] | 86 | unsigned N = 0; |
| 87 | const char *Error = nullptr; |
Thomas Lively | 2faf079 | 2018-11-09 01:57:00 +0000 | [diff] [blame] | 88 | Val = Signed ? decodeSLEB128(Bytes.data() + Size, &N, |
| 89 | Bytes.data() + Bytes.size(), &Error) |
| 90 | : static_cast<int64_t>(decodeULEB128(Bytes.data() + Size, &N, |
| 91 | Bytes.data() + Bytes.size(), |
| 92 | &Error)); |
Sam Clegg | 16c1682 | 2018-05-10 22:16:44 +0000 | [diff] [blame] | 93 | if (Error) |
| 94 | return false; |
| 95 | Size += N; |
Thomas Lively | 2faf079 | 2018-11-09 01:57:00 +0000 | [diff] [blame] | 96 | return true; |
| 97 | } |
| 98 | |
| 99 | static bool parseLEBImmediate(MCInst &MI, uint64_t &Size, |
| 100 | ArrayRef<uint8_t> Bytes, bool Signed) { |
| 101 | int64_t Val; |
| 102 | if (!nextLEB(Val, Bytes, Size, Signed)) |
| 103 | return false; |
Sam Clegg | 16c1682 | 2018-05-10 22:16:44 +0000 | [diff] [blame] | 104 | MI.addOperand(MCOperand::createImm(Val)); |
| 105 | return true; |
| 106 | } |
| 107 | |
| 108 | template <typename T> |
Thomas Lively | 2244292 | 2018-08-21 21:03:18 +0000 | [diff] [blame] | 109 | bool parseImmediate(MCInst &MI, uint64_t &Size, ArrayRef<uint8_t> Bytes) { |
Sam Clegg | 16c1682 | 2018-05-10 22:16:44 +0000 | [diff] [blame] | 110 | if (Size + sizeof(T) > Bytes.size()) |
| 111 | return false; |
| 112 | T Val; |
| 113 | memcpy(&Val, Bytes.data() + Size, sizeof(T)); |
| 114 | support::endian::byte_swap<T, support::endianness::little>(Val); |
| 115 | Size += sizeof(T); |
Thomas Lively | 2244292 | 2018-08-21 21:03:18 +0000 | [diff] [blame] | 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 | } |
Sam Clegg | 16c1682 | 2018-05-10 22:16:44 +0000 | [diff] [blame] | 121 | return true; |
| 122 | } |
| 123 | |
Wouter van Oortmerssen | f3b762a | 2019-01-17 18:14:09 +0000 | [diff] [blame^] | 124 | MCDisassembler::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 Gohman | 1a42728 | 2016-01-12 03:32:29 +0000 | [diff] [blame] | 159 | MCDisassembler::DecodeStatus WebAssemblyDisassembler::getInstruction( |
| 160 | MCInst &MI, uint64_t &Size, ArrayRef<uint8_t> Bytes, uint64_t /*Address*/, |
Sam Clegg | 16c1682 | 2018-05-10 22:16:44 +0000 | [diff] [blame] | 161 | raw_ostream & /*OS*/, raw_ostream &CS) const { |
| 162 | CommentStream = &CS; |
| 163 | Size = 0; |
Thomas Lively | fc3163b | 2018-11-15 18:56:49 +0000 | [diff] [blame] | 164 | int Opc = nextByte(Bytes, Size); |
Sam Clegg | 16c1682 | 2018-05-10 22:16:44 +0000 | [diff] [blame] | 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; |
Thomas Lively | 2faf079 | 2018-11-09 01:57:00 +0000 | [diff] [blame] | 180 | int64_t PrefixedOpc; |
Wouter van Oortmerssen | f3b762a | 2019-01-17 18:14:09 +0000 | [diff] [blame^] | 181 | if (!nextLEB(PrefixedOpc, Bytes, Size, false)) |
Sam Clegg | 16c1682 | 2018-05-10 22:16:44 +0000 | [diff] [blame] | 182 | return MCDisassembler::Fail; |
Thomas Lively | 2faf079 | 2018-11-09 01:57:00 +0000 | [diff] [blame] | 183 | if (PrefixedOpc < 0 || PrefixedOpc >= WebAssemblyInstructionTableSize) |
| 184 | return MCDisassembler::Fail; |
| 185 | WasmInst += PrefixedOpc; |
Sam Clegg | 16c1682 | 2018-05-10 22:16:44 +0000 | [diff] [blame] | 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++) { |
Wouter van Oortmerssen | 820c626 | 2019-01-03 23:01:30 +0000 | [diff] [blame] | 194 | auto OT = OperandTable[WasmInst->OperandStart + OPI]; |
| 195 | switch (OT) { |
Sam Clegg | 16c1682 | 2018-05-10 22:16:44 +0000 | [diff] [blame] | 196 | // 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: |
| 204 | case MCOI::OPERAND_IMMEDIATE: { |
| 205 | if (!parseLEBImmediate(MI, Size, Bytes, false)) |
| 206 | return MCDisassembler::Fail; |
| 207 | break; |
| 208 | } |
| 209 | // SLEB operands: |
| 210 | case WebAssembly::OPERAND_I32IMM: |
Wouter van Oortmerssen | ad72f68 | 2019-01-02 23:23:51 +0000 | [diff] [blame] | 211 | case WebAssembly::OPERAND_I64IMM: { |
Sam Clegg | 16c1682 | 2018-05-10 22:16:44 +0000 | [diff] [blame] | 212 | if (!parseLEBImmediate(MI, Size, Bytes, true)) |
| 213 | return MCDisassembler::Fail; |
| 214 | break; |
| 215 | } |
Wouter van Oortmerssen | ad72f68 | 2019-01-02 23:23:51 +0000 | [diff] [blame] | 216 | // block_type operands (uint8_t). |
| 217 | case WebAssembly::OPERAND_SIGNATURE: { |
| 218 | if (!parseImmediate<uint8_t>(MI, Size, Bytes)) |
| 219 | return MCDisassembler::Fail; |
| 220 | break; |
| 221 | } |
Sam Clegg | 16c1682 | 2018-05-10 22:16:44 +0000 | [diff] [blame] | 222 | // FP operands. |
| 223 | case WebAssembly::OPERAND_F32IMM: { |
Thomas Lively | 2244292 | 2018-08-21 21:03:18 +0000 | [diff] [blame] | 224 | if (!parseImmediate<float>(MI, Size, Bytes)) |
Sam Clegg | 16c1682 | 2018-05-10 22:16:44 +0000 | [diff] [blame] | 225 | return MCDisassembler::Fail; |
| 226 | break; |
| 227 | } |
| 228 | case WebAssembly::OPERAND_F64IMM: { |
Thomas Lively | 2244292 | 2018-08-21 21:03:18 +0000 | [diff] [blame] | 229 | if (!parseImmediate<double>(MI, Size, Bytes)) |
| 230 | return MCDisassembler::Fail; |
| 231 | break; |
| 232 | } |
| 233 | // Vector lane operands (not LEB encoded). |
| 234 | case WebAssembly::OPERAND_VEC_I8IMM: { |
| 235 | if (!parseImmediate<uint8_t>(MI, Size, Bytes)) |
| 236 | return MCDisassembler::Fail; |
| 237 | break; |
| 238 | } |
| 239 | case WebAssembly::OPERAND_VEC_I16IMM: { |
| 240 | if (!parseImmediate<uint16_t>(MI, Size, Bytes)) |
| 241 | return MCDisassembler::Fail; |
| 242 | break; |
| 243 | } |
| 244 | case WebAssembly::OPERAND_VEC_I32IMM: { |
| 245 | if (!parseImmediate<uint32_t>(MI, Size, Bytes)) |
| 246 | return MCDisassembler::Fail; |
| 247 | break; |
| 248 | } |
| 249 | case WebAssembly::OPERAND_VEC_I64IMM: { |
| 250 | if (!parseImmediate<uint64_t>(MI, Size, Bytes)) |
Sam Clegg | 16c1682 | 2018-05-10 22:16:44 +0000 | [diff] [blame] | 251 | return MCDisassembler::Fail; |
| 252 | break; |
| 253 | } |
Wouter van Oortmerssen | 820c626 | 2019-01-03 23:01:30 +0000 | [diff] [blame] | 254 | case WebAssembly::OPERAND_BRLIST: { |
| 255 | int64_t TargetTableLen; |
| 256 | if (!nextLEB(TargetTableLen, Bytes, Size, false)) |
| 257 | return MCDisassembler::Fail; |
| 258 | for (int64_t I = 0; I < TargetTableLen; I++) { |
| 259 | if (!parseLEBImmediate(MI, Size, Bytes, false)) |
| 260 | return MCDisassembler::Fail; |
| 261 | } |
| 262 | // Default case. |
| 263 | if (!parseLEBImmediate(MI, Size, Bytes, false)) |
| 264 | return MCDisassembler::Fail; |
| 265 | break; |
| 266 | } |
Wouter van Oortmerssen | a733d08 | 2018-08-30 15:40:53 +0000 | [diff] [blame] | 267 | case MCOI::OPERAND_REGISTER: |
| 268 | // The tablegen header currently does not have any register operands since |
| 269 | // we use only the stack (_S) instructions. |
| 270 | // If you hit this that probably means a bad instruction definition in |
| 271 | // tablegen. |
| 272 | llvm_unreachable("Register operand in WebAssemblyDisassembler"); |
Sam Clegg | 16c1682 | 2018-05-10 22:16:44 +0000 | [diff] [blame] | 273 | default: |
| 274 | llvm_unreachable("Unknown operand type in WebAssemblyDisassembler"); |
| 275 | } |
| 276 | } |
| 277 | return MCDisassembler::Success; |
Dan Gohman | 1a42728 | 2016-01-12 03:32:29 +0000 | [diff] [blame] | 278 | } |