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]; 45 // All wasm instructions have a StackBased fieldof type bit, we only want 46 // the instructions for which this is 1. 47 auto Bit = Def.getValue("StackBased")->getValue()-> 48 getCastTo(BitRecTy::get()); 49 auto IsStackBased = Bit && reinterpret_cast<const BitInit *>(Bit) 50 ->getValue(); 51 if (IsStackBased && !CGIP.second) { 52 // this picks the first of many typed variants, which is 53 // currently the except_ref one, though this shouldn't matter for 54 // disassembly purposes. 55 CGIP = std::make_pair(I, &CGI); 56 } 57 } 58 OS << "#include \"MCTargetDesc/WebAssemblyMCTargetDesc.h\"\n"; 59 OS << "\n"; 60 OS << "namespace llvm {\n\n"; 61 OS << "enum EntryType : uint8_t { "; 62 OS << "ET_Unused, ET_Prefix, ET_Instruction };\n\n"; 63 OS << "struct WebAssemblyInstruction {\n"; 64 OS << " uint16_t Opcode;\n"; 65 OS << " EntryType ET;\n"; 66 OS << " uint8_t NumOperands;\n"; 67 OS << " uint16_t OperandStart;\n"; 68 OS << "};\n\n"; 69 std::vector<std::string> OperandTable, CurOperandList; 70 // Output one table per prefix. 71 for (auto &PrefixPair : OpcodeTable) { 72 if (PrefixPair.second.empty()) 73 continue; 74 OS << "WebAssemblyInstruction InstructionTable" << PrefixPair.first; 75 OS << "[] = {\n"; 76 for (unsigned I = 0; I <= 0xFF; I++) { 77 auto InstIt = PrefixPair.second.find(I); 78 if (InstIt != PrefixPair.second.end()) { 79 // Regular instruction. 80 assert(InstIt->second.second); 81 auto &CGI = *InstIt->second.second; 82 OS << " // 0x"; 83 OS.write_hex(static_cast<unsigned long long>(I)); 84 OS << ": " << CGI.AsmString << "\n"; 85 OS << " { " << InstIt->second.first << ", ET_Instruction, "; 86 OS << CGI.Operands.OperandList.size() << ", "; 87 // Collect operand types for storage in a shared list. 88 CurOperandList.clear(); 89 for (auto &Op : CGI.Operands.OperandList) { 90 CurOperandList.push_back(Op.OperandType); 91 } 92 // See if we already have stored this sequence before. This is not 93 // strictly necessary but makes the table really small. 94 size_t OperandStart = OperandTable.size(); 95 if (CurOperandList.size() <= OperandTable.size()) { 96 for (size_t J = 0; J <= OperandTable.size() - CurOperandList.size(); 97 ++J) { 98 size_t K = 0; 99 for (; K < CurOperandList.size(); ++K) { 100 if (OperandTable[J + K] != CurOperandList[K]) break; 101 } 102 if (K == CurOperandList.size()) { 103 OperandStart = J; 104 break; 105 } 106 } 107 } 108 // Store operands if no prior occurrence. 109 if (OperandStart == OperandTable.size()) { 110 OperandTable.insert(OperandTable.end(), CurOperandList.begin(), 111 CurOperandList.end()); 112 } 113 OS << OperandStart; 114 } else { 115 auto PrefixIt = OpcodeTable.find(I); 116 // If we have a non-empty table for it that's not 0, this is a prefix. 117 if (PrefixIt != OpcodeTable.end() && I && !PrefixPair.first) { 118 OS << " { 0, ET_Prefix, 0, 0"; 119 } else { 120 OS << " { 0, ET_Unused, 0, 0"; 121 } 122 } 123 OS << " },\n"; 124 } 125 OS << "};\n\n"; 126 } 127 // Create a table of all operands: 128 OS << "const uint8_t OperandTable[] = {\n"; 129 for (auto &Op : OperandTable) { 130 OS << " " << Op << ",\n"; 131 } 132 OS << "};\n\n"; 133 // Create a table of all extension tables: 134 OS << "struct { uint8_t Prefix; const WebAssemblyInstruction *Table; }\n"; 135 OS << "PrefixTable[] = {\n"; 136 for (auto &PrefixPair : OpcodeTable) { 137 if (PrefixPair.second.empty() || !PrefixPair.first) 138 continue; 139 OS << " { " << PrefixPair.first << ", InstructionTable" 140 << PrefixPair.first; 141 OS << " },\n"; 142 } 143 OS << " { 0, nullptr }\n};\n\n"; 144 OS << "} // End llvm namespace\n"; 145 } 146 147 } // namespace llvm 148