Sam Clegg | 16c1682 | 2018-05-10 22:16:44 +0000 | [diff] [blame] | 1 | //===- WebAssemblyDisassemblerEmitter.cpp - Disassembler tables -*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Sam Clegg | 16c1682 | 2018-05-10 22:16:44 +0000 | [diff] [blame] | 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 | |
Thomas Lively | 2faf079 | 2018-11-09 01:57:00 +0000 | [diff] [blame] | 21 | static constexpr int WebAssemblyInstructionTableSize = 256; |
| 22 | |
Sam Clegg | 16c1682 | 2018-05-10 22:16:44 +0000 | [diff] [blame] | 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]; |
Thomas Lively | c63b5fc | 2018-10-22 21:55:26 +0000 | [diff] [blame] | 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"; |
Wouter van Oortmerssen | 1a91cb0 | 2019-02-05 01:19:45 +0000 | [diff] [blame] | 53 | if (!IsStackBased) |
| 54 | continue; |
| 55 | if (CGIP.second) { |
| 56 | // We already have an instruction for this slot, so decide which one |
| 57 | // should be the canonical one. This determines which variant gets |
| 58 | // printed in a disassembly. We want e.g. "call" not "i32.call", and |
| 59 | // "end" when we don't know if its "end_loop" or "end_block" etc. |
| 60 | auto IsCanonicalExisting = CGIP.second->TheDef->getValue("IsCanonical") |
| 61 | ->getValue() |
| 62 | ->getAsString() == "1"; |
| 63 | // We already have one marked explicitly as canonical, so keep it. |
| 64 | if (IsCanonicalExisting) |
| 65 | continue; |
| 66 | auto IsCanonicalNew = |
| 67 | Def.getValue("IsCanonical")->getValue()->getAsString() == "1"; |
| 68 | // If the new one is explicitly marked as canonical, take it. |
| 69 | if (!IsCanonicalNew) { |
| 70 | // Neither the existing or new instruction is canonical. |
Krasimir Georgiev | 1297180 | 2019-02-05 15:00:56 +0000 | [diff] [blame] | 71 | // Pick the one with the shortest name as heuristic. |
Wouter van Oortmerssen | 1a91cb0 | 2019-02-05 01:19:45 +0000 | [diff] [blame] | 72 | // Though ideally IsCanonical is always defined for at least one |
| 73 | // variant so this never has to apply. |
| 74 | if (CGIP.second->AsmString.size() <= CGI.AsmString.size()) |
| 75 | continue; |
| 76 | } |
Sam Clegg | 16c1682 | 2018-05-10 22:16:44 +0000 | [diff] [blame] | 77 | } |
Wouter van Oortmerssen | 1a91cb0 | 2019-02-05 01:19:45 +0000 | [diff] [blame] | 78 | // Set this instruction as the one to use. |
| 79 | CGIP = std::make_pair(I, &CGI); |
Sam Clegg | 16c1682 | 2018-05-10 22:16:44 +0000 | [diff] [blame] | 80 | } |
| 81 | OS << "#include \"MCTargetDesc/WebAssemblyMCTargetDesc.h\"\n"; |
| 82 | OS << "\n"; |
| 83 | OS << "namespace llvm {\n\n"; |
Thomas Lively | 2faf079 | 2018-11-09 01:57:00 +0000 | [diff] [blame] | 84 | OS << "static constexpr int WebAssemblyInstructionTableSize = "; |
| 85 | OS << WebAssemblyInstructionTableSize << ";\n\n"; |
Sam Clegg | 16c1682 | 2018-05-10 22:16:44 +0000 | [diff] [blame] | 86 | OS << "enum EntryType : uint8_t { "; |
| 87 | OS << "ET_Unused, ET_Prefix, ET_Instruction };\n\n"; |
| 88 | OS << "struct WebAssemblyInstruction {\n"; |
| 89 | OS << " uint16_t Opcode;\n"; |
| 90 | OS << " EntryType ET;\n"; |
| 91 | OS << " uint8_t NumOperands;\n"; |
Wouter van Oortmerssen | a733d08 | 2018-08-30 15:40:53 +0000 | [diff] [blame] | 92 | OS << " uint16_t OperandStart;\n"; |
Sam Clegg | 16c1682 | 2018-05-10 22:16:44 +0000 | [diff] [blame] | 93 | OS << "};\n\n"; |
Wouter van Oortmerssen | a733d08 | 2018-08-30 15:40:53 +0000 | [diff] [blame] | 94 | std::vector<std::string> OperandTable, CurOperandList; |
Sam Clegg | 16c1682 | 2018-05-10 22:16:44 +0000 | [diff] [blame] | 95 | // Output one table per prefix. |
| 96 | for (auto &PrefixPair : OpcodeTable) { |
| 97 | if (PrefixPair.second.empty()) |
| 98 | continue; |
| 99 | OS << "WebAssemblyInstruction InstructionTable" << PrefixPair.first; |
| 100 | OS << "[] = {\n"; |
Thomas Lively | 2faf079 | 2018-11-09 01:57:00 +0000 | [diff] [blame] | 101 | for (unsigned I = 0; I < WebAssemblyInstructionTableSize; I++) { |
Sam Clegg | 16c1682 | 2018-05-10 22:16:44 +0000 | [diff] [blame] | 102 | auto InstIt = PrefixPair.second.find(I); |
| 103 | if (InstIt != PrefixPair.second.end()) { |
| 104 | // Regular instruction. |
| 105 | assert(InstIt->second.second); |
| 106 | auto &CGI = *InstIt->second.second; |
| 107 | OS << " // 0x"; |
| 108 | OS.write_hex(static_cast<unsigned long long>(I)); |
| 109 | OS << ": " << CGI.AsmString << "\n"; |
| 110 | OS << " { " << InstIt->second.first << ", ET_Instruction, "; |
Wouter van Oortmerssen | a733d08 | 2018-08-30 15:40:53 +0000 | [diff] [blame] | 111 | OS << CGI.Operands.OperandList.size() << ", "; |
| 112 | // Collect operand types for storage in a shared list. |
| 113 | CurOperandList.clear(); |
Sam Clegg | 16c1682 | 2018-05-10 22:16:44 +0000 | [diff] [blame] | 114 | for (auto &Op : CGI.Operands.OperandList) { |
Wouter van Oortmerssen | 820c626 | 2019-01-03 23:01:30 +0000 | [diff] [blame] | 115 | assert(Op.OperandType != "MCOI::OPERAND_UNKNOWN"); |
Wouter van Oortmerssen | a733d08 | 2018-08-30 15:40:53 +0000 | [diff] [blame] | 116 | CurOperandList.push_back(Op.OperandType); |
Sam Clegg | 16c1682 | 2018-05-10 22:16:44 +0000 | [diff] [blame] | 117 | } |
Wouter van Oortmerssen | a733d08 | 2018-08-30 15:40:53 +0000 | [diff] [blame] | 118 | // See if we already have stored this sequence before. This is not |
| 119 | // strictly necessary but makes the table really small. |
| 120 | size_t OperandStart = OperandTable.size(); |
| 121 | if (CurOperandList.size() <= OperandTable.size()) { |
| 122 | for (size_t J = 0; J <= OperandTable.size() - CurOperandList.size(); |
| 123 | ++J) { |
| 124 | size_t K = 0; |
| 125 | for (; K < CurOperandList.size(); ++K) { |
| 126 | if (OperandTable[J + K] != CurOperandList[K]) break; |
| 127 | } |
| 128 | if (K == CurOperandList.size()) { |
| 129 | OperandStart = J; |
| 130 | break; |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | // Store operands if no prior occurrence. |
| 135 | if (OperandStart == OperandTable.size()) { |
| 136 | OperandTable.insert(OperandTable.end(), CurOperandList.begin(), |
| 137 | CurOperandList.end()); |
| 138 | } |
| 139 | OS << OperandStart; |
Sam Clegg | 16c1682 | 2018-05-10 22:16:44 +0000 | [diff] [blame] | 140 | } else { |
| 141 | auto PrefixIt = OpcodeTable.find(I); |
| 142 | // If we have a non-empty table for it that's not 0, this is a prefix. |
| 143 | if (PrefixIt != OpcodeTable.end() && I && !PrefixPair.first) { |
Wouter van Oortmerssen | a733d08 | 2018-08-30 15:40:53 +0000 | [diff] [blame] | 144 | OS << " { 0, ET_Prefix, 0, 0"; |
Sam Clegg | 16c1682 | 2018-05-10 22:16:44 +0000 | [diff] [blame] | 145 | } else { |
Wouter van Oortmerssen | a733d08 | 2018-08-30 15:40:53 +0000 | [diff] [blame] | 146 | OS << " { 0, ET_Unused, 0, 0"; |
Sam Clegg | 16c1682 | 2018-05-10 22:16:44 +0000 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | OS << " },\n"; |
| 150 | } |
| 151 | OS << "};\n\n"; |
| 152 | } |
Wouter van Oortmerssen | a733d08 | 2018-08-30 15:40:53 +0000 | [diff] [blame] | 153 | // Create a table of all operands: |
| 154 | OS << "const uint8_t OperandTable[] = {\n"; |
| 155 | for (auto &Op : OperandTable) { |
| 156 | OS << " " << Op << ",\n"; |
| 157 | } |
| 158 | OS << "};\n\n"; |
Sam Clegg | 16c1682 | 2018-05-10 22:16:44 +0000 | [diff] [blame] | 159 | // Create a table of all extension tables: |
| 160 | OS << "struct { uint8_t Prefix; const WebAssemblyInstruction *Table; }\n"; |
| 161 | OS << "PrefixTable[] = {\n"; |
| 162 | for (auto &PrefixPair : OpcodeTable) { |
| 163 | if (PrefixPair.second.empty() || !PrefixPair.first) |
| 164 | continue; |
| 165 | OS << " { " << PrefixPair.first << ", InstructionTable" |
| 166 | << PrefixPair.first; |
| 167 | OS << " },\n"; |
| 168 | } |
| 169 | OS << " { 0, nullptr }\n};\n\n"; |
Bjorn Pettersson | 6bd3a9e | 2019-08-25 10:47:30 +0000 | [diff] [blame^] | 170 | OS << "} // end namespace llvm\n"; |
Sam Clegg | 16c1682 | 2018-05-10 22:16:44 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | } // namespace llvm |