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