Sam Clegg | 16c1682 | 2018-05-10 22:16:44 +0000 | [diff] [blame] | 1 | //===- WebAssemblyDisassemblerEmitter.cpp - Disassembler tables -*- 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 | // This file is part of the WebAssembly Disassembler Emitter. |
| 11 | // It contains the implementation of the disassembler tables. |
| 12 | // Documentation for the disassembler emitter in general can be found in |
| 13 | // WebAssemblyDisassemblerEmitter.h. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
| 17 | #include "WebAssemblyDisassemblerEmitter.h" |
| 18 | #include "llvm/TableGen/Record.h" |
| 19 | |
| 20 | namespace llvm { |
| 21 | |
| 22 | void emitWebAssemblyDisassemblerTables( |
| 23 | raw_ostream &OS, |
| 24 | const ArrayRef<const CodeGenInstruction *> &NumberedInstructions) { |
| 25 | // First lets organize all opcodes by (prefix) byte. Prefix 0 is the |
| 26 | // starting table. |
| 27 | std::map<unsigned, |
| 28 | std::map<unsigned, std::pair<unsigned, const CodeGenInstruction *>>> |
| 29 | OpcodeTable; |
| 30 | for (unsigned I = 0; I != NumberedInstructions.size(); ++I) { |
| 31 | auto &CGI = *NumberedInstructions[I]; |
| 32 | auto &Def = *CGI.TheDef; |
| 33 | if (!Def.getValue("Inst")) |
| 34 | continue; |
| 35 | auto &Inst = *Def.getValueAsBitsInit("Inst"); |
| 36 | auto Opc = static_cast<unsigned>( |
| 37 | reinterpret_cast<IntInit *>(Inst.convertInitializerTo(IntRecTy::get())) |
| 38 | ->getValue()); |
| 39 | if (Opc == 0xFFFFFFFF) |
| 40 | continue; // No opcode defined. |
| 41 | assert(Opc <= 0xFFFF); |
| 42 | auto Prefix = Opc >> 8; |
| 43 | Opc = Opc & 0xFF; |
| 44 | auto &CGIP = OpcodeTable[Prefix][Opc]; |
Thomas Lively | c63b5fc | 2018-10-22 21:55:26 +0000 | [diff] [blame^] | 45 | // All wasm instructions have a StackBased field of type string, we only |
| 46 | // want the instructions for which this is "true". |
| 47 | auto StackString = |
| 48 | Def.getValue("StackBased")->getValue()->getCastTo(StringRecTy::get()); |
| 49 | auto IsStackBased = |
| 50 | StackString && |
| 51 | reinterpret_cast<const StringInit *>(StackString)->getValue() == "true"; |
Wouter van Oortmerssen | a733d08 | 2018-08-30 15:40:53 +0000 | [diff] [blame] | 52 | if (IsStackBased && !CGIP.second) { |
| 53 | // this picks the first of many typed variants, which is |
| 54 | // currently the except_ref one, though this shouldn't matter for |
| 55 | // disassembly purposes. |
Sam Clegg | 16c1682 | 2018-05-10 22:16:44 +0000 | [diff] [blame] | 56 | CGIP = std::make_pair(I, &CGI); |
| 57 | } |
| 58 | } |
| 59 | OS << "#include \"MCTargetDesc/WebAssemblyMCTargetDesc.h\"\n"; |
| 60 | OS << "\n"; |
| 61 | OS << "namespace llvm {\n\n"; |
| 62 | OS << "enum EntryType : uint8_t { "; |
| 63 | OS << "ET_Unused, ET_Prefix, ET_Instruction };\n\n"; |
| 64 | OS << "struct WebAssemblyInstruction {\n"; |
| 65 | OS << " uint16_t Opcode;\n"; |
| 66 | OS << " EntryType ET;\n"; |
| 67 | OS << " uint8_t NumOperands;\n"; |
Wouter van Oortmerssen | a733d08 | 2018-08-30 15:40:53 +0000 | [diff] [blame] | 68 | OS << " uint16_t OperandStart;\n"; |
Sam Clegg | 16c1682 | 2018-05-10 22:16:44 +0000 | [diff] [blame] | 69 | OS << "};\n\n"; |
Wouter van Oortmerssen | a733d08 | 2018-08-30 15:40:53 +0000 | [diff] [blame] | 70 | std::vector<std::string> OperandTable, CurOperandList; |
Sam Clegg | 16c1682 | 2018-05-10 22:16:44 +0000 | [diff] [blame] | 71 | // Output one table per prefix. |
| 72 | for (auto &PrefixPair : OpcodeTable) { |
| 73 | if (PrefixPair.second.empty()) |
| 74 | continue; |
| 75 | OS << "WebAssemblyInstruction InstructionTable" << PrefixPair.first; |
| 76 | OS << "[] = {\n"; |
| 77 | for (unsigned I = 0; I <= 0xFF; I++) { |
| 78 | auto InstIt = PrefixPair.second.find(I); |
| 79 | if (InstIt != PrefixPair.second.end()) { |
| 80 | // Regular instruction. |
| 81 | assert(InstIt->second.second); |
| 82 | auto &CGI = *InstIt->second.second; |
| 83 | OS << " // 0x"; |
| 84 | OS.write_hex(static_cast<unsigned long long>(I)); |
| 85 | OS << ": " << CGI.AsmString << "\n"; |
| 86 | OS << " { " << InstIt->second.first << ", ET_Instruction, "; |
Wouter van Oortmerssen | a733d08 | 2018-08-30 15:40:53 +0000 | [diff] [blame] | 87 | OS << CGI.Operands.OperandList.size() << ", "; |
| 88 | // Collect operand types for storage in a shared list. |
| 89 | CurOperandList.clear(); |
Sam Clegg | 16c1682 | 2018-05-10 22:16:44 +0000 | [diff] [blame] | 90 | for (auto &Op : CGI.Operands.OperandList) { |
Wouter van Oortmerssen | a733d08 | 2018-08-30 15:40:53 +0000 | [diff] [blame] | 91 | CurOperandList.push_back(Op.OperandType); |
Sam Clegg | 16c1682 | 2018-05-10 22:16:44 +0000 | [diff] [blame] | 92 | } |
Wouter van Oortmerssen | a733d08 | 2018-08-30 15:40:53 +0000 | [diff] [blame] | 93 | // See if we already have stored this sequence before. This is not |
| 94 | // strictly necessary but makes the table really small. |
| 95 | size_t OperandStart = OperandTable.size(); |
| 96 | if (CurOperandList.size() <= OperandTable.size()) { |
| 97 | for (size_t J = 0; J <= OperandTable.size() - CurOperandList.size(); |
| 98 | ++J) { |
| 99 | size_t K = 0; |
| 100 | for (; K < CurOperandList.size(); ++K) { |
| 101 | if (OperandTable[J + K] != CurOperandList[K]) break; |
| 102 | } |
| 103 | if (K == CurOperandList.size()) { |
| 104 | OperandStart = J; |
| 105 | break; |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | // Store operands if no prior occurrence. |
| 110 | if (OperandStart == OperandTable.size()) { |
| 111 | OperandTable.insert(OperandTable.end(), CurOperandList.begin(), |
| 112 | CurOperandList.end()); |
| 113 | } |
| 114 | OS << OperandStart; |
Sam Clegg | 16c1682 | 2018-05-10 22:16:44 +0000 | [diff] [blame] | 115 | } else { |
| 116 | auto PrefixIt = OpcodeTable.find(I); |
| 117 | // If we have a non-empty table for it that's not 0, this is a prefix. |
| 118 | if (PrefixIt != OpcodeTable.end() && I && !PrefixPair.first) { |
Wouter van Oortmerssen | a733d08 | 2018-08-30 15:40:53 +0000 | [diff] [blame] | 119 | OS << " { 0, ET_Prefix, 0, 0"; |
Sam Clegg | 16c1682 | 2018-05-10 22:16:44 +0000 | [diff] [blame] | 120 | } else { |
Wouter van Oortmerssen | a733d08 | 2018-08-30 15:40:53 +0000 | [diff] [blame] | 121 | OS << " { 0, ET_Unused, 0, 0"; |
Sam Clegg | 16c1682 | 2018-05-10 22:16:44 +0000 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | OS << " },\n"; |
| 125 | } |
| 126 | OS << "};\n\n"; |
| 127 | } |
Wouter van Oortmerssen | a733d08 | 2018-08-30 15:40:53 +0000 | [diff] [blame] | 128 | // Create a table of all operands: |
| 129 | OS << "const uint8_t OperandTable[] = {\n"; |
| 130 | for (auto &Op : OperandTable) { |
| 131 | OS << " " << Op << ",\n"; |
| 132 | } |
| 133 | OS << "};\n\n"; |
Sam Clegg | 16c1682 | 2018-05-10 22:16:44 +0000 | [diff] [blame] | 134 | // Create a table of all extension tables: |
| 135 | OS << "struct { uint8_t Prefix; const WebAssemblyInstruction *Table; }\n"; |
| 136 | OS << "PrefixTable[] = {\n"; |
| 137 | for (auto &PrefixPair : OpcodeTable) { |
| 138 | if (PrefixPair.second.empty() || !PrefixPair.first) |
| 139 | continue; |
| 140 | OS << " { " << PrefixPair.first << ", InstructionTable" |
| 141 | << PrefixPair.first; |
| 142 | OS << " },\n"; |
| 143 | } |
| 144 | OS << " { 0, nullptr }\n};\n\n"; |
| 145 | OS << "} // End llvm namespace\n"; |
| 146 | } |
| 147 | |
| 148 | } // namespace llvm |