xref: /freebsd/contrib/llvm-project/llvm/utils/TableGen/WebAssemblyDisassemblerEmitter.cpp (revision 271171e0d97b88ba2a7c3bf750c9672b484c1c13)
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 "CodeGenInstruction.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/Support/raw_ostream.h"
20 #include "llvm/TableGen/Record.h"
21 
22 namespace llvm {
23 
24 static constexpr int WebAssemblyInstructionTableSize = 256;
25 
26 void emitWebAssemblyDisassemblerTables(
27     raw_ostream &OS,
28     const ArrayRef<const CodeGenInstruction *> &NumberedInstructions) {
29   // First lets organize all opcodes by (prefix) byte. Prefix 0 is the
30   // starting table.
31   std::map<unsigned,
32            std::map<unsigned, std::pair<unsigned, const CodeGenInstruction *>>>
33       OpcodeTable;
34   for (unsigned I = 0; I != NumberedInstructions.size(); ++I) {
35     auto &CGI = *NumberedInstructions[I];
36     auto &Def = *CGI.TheDef;
37     if (!Def.getValue("Inst"))
38       continue;
39     auto &Inst = *Def.getValueAsBitsInit("Inst");
40     auto Opc = static_cast<unsigned>(
41         reinterpret_cast<IntInit *>(Inst.convertInitializerTo(IntRecTy::get()))
42             ->getValue());
43     if (Opc == 0xFFFFFFFF)
44       continue; // No opcode defined.
45     assert(Opc <= 0xFFFFFF);
46     unsigned Prefix;
47     if (Opc <= 0xFFFF) {
48       Prefix = Opc >> 8;
49       Opc = Opc & 0xFF;
50     } else {
51       Prefix = Opc >> 16;
52       Opc = Opc & 0xFFFF;
53     }
54     auto &CGIP = OpcodeTable[Prefix][Opc];
55     // All wasm instructions have a StackBased field of type string, we only
56     // want the instructions for which this is "true".
57     auto StackString =
58         Def.getValue("StackBased")->getValue()->getCastTo(StringRecTy::get());
59     auto IsStackBased =
60         StackString &&
61         reinterpret_cast<const StringInit *>(StackString)->getValue() == "true";
62     if (!IsStackBased)
63       continue;
64     if (CGIP.second) {
65       // We already have an instruction for this slot, so decide which one
66       // should be the canonical one. This determines which variant gets
67       // printed in a disassembly. We want e.g. "call" not "i32.call", and
68       // "end" when we don't know if its "end_loop" or "end_block" etc.
69       auto IsCanonicalExisting = CGIP.second->TheDef->getValue("IsCanonical")
70                                      ->getValue()
71                                      ->getAsString() == "1";
72       // We already have one marked explicitly as canonical, so keep it.
73       if (IsCanonicalExisting)
74         continue;
75       auto IsCanonicalNew =
76           Def.getValue("IsCanonical")->getValue()->getAsString() == "1";
77       // If the new one is explicitly marked as canonical, take it.
78       if (!IsCanonicalNew) {
79         // Neither the existing or new instruction is canonical.
80         // Pick the one with the shortest name as heuristic.
81         // Though ideally IsCanonical is always defined for at least one
82         // variant so this never has to apply.
83         if (CGIP.second->AsmString.size() <= CGI.AsmString.size())
84           continue;
85       }
86     }
87     // Set this instruction as the one to use.
88     CGIP = std::make_pair(I, &CGI);
89   }
90   OS << "#include \"MCTargetDesc/WebAssemblyMCTargetDesc.h\"\n";
91   OS << "\n";
92   OS << "namespace llvm {\n\n";
93   OS << "static constexpr int WebAssemblyInstructionTableSize = ";
94   OS << WebAssemblyInstructionTableSize << ";\n\n";
95   OS << "enum EntryType : uint8_t { ";
96   OS << "ET_Unused, ET_Prefix, ET_Instruction };\n\n";
97   OS << "struct WebAssemblyInstruction {\n";
98   OS << "  uint16_t Opcode;\n";
99   OS << "  EntryType ET;\n";
100   OS << "  uint8_t NumOperands;\n";
101   OS << "  uint16_t OperandStart;\n";
102   OS << "};\n\n";
103   std::vector<std::string> OperandTable, CurOperandList;
104   // Output one table per prefix.
105   for (auto &PrefixPair : OpcodeTable) {
106     if (PrefixPair.second.empty())
107       continue;
108     OS << "WebAssemblyInstruction InstructionTable" << PrefixPair.first;
109     OS << "[] = {\n";
110     for (unsigned I = 0; I < WebAssemblyInstructionTableSize; I++) {
111       auto InstIt = PrefixPair.second.find(I);
112       if (InstIt != PrefixPair.second.end()) {
113         // Regular instruction.
114         assert(InstIt->second.second);
115         auto &CGI = *InstIt->second.second;
116         OS << "  // 0x";
117         OS.write_hex(static_cast<unsigned long long>(I));
118         OS << ": " << CGI.AsmString << "\n";
119         OS << "  { " << InstIt->second.first << ", ET_Instruction, ";
120         OS << CGI.Operands.OperandList.size() << ", ";
121         // Collect operand types for storage in a shared list.
122         CurOperandList.clear();
123         for (auto &Op : CGI.Operands.OperandList) {
124           assert(Op.OperandType != "MCOI::OPERAND_UNKNOWN");
125           CurOperandList.push_back(Op.OperandType);
126         }
127         // See if we already have stored this sequence before. This is not
128         // strictly necessary but makes the table really small.
129         size_t OperandStart = OperandTable.size();
130         if (CurOperandList.size() <= OperandTable.size()) {
131           for (size_t J = 0; J <= OperandTable.size() - CurOperandList.size();
132                ++J) {
133             size_t K = 0;
134             for (; K < CurOperandList.size(); ++K) {
135               if (OperandTable[J + K] != CurOperandList[K]) break;
136             }
137             if (K == CurOperandList.size()) {
138               OperandStart = J;
139               break;
140             }
141           }
142         }
143         // Store operands if no prior occurrence.
144         if (OperandStart == OperandTable.size()) {
145           llvm::append_range(OperandTable, CurOperandList);
146         }
147         OS << OperandStart;
148       } else {
149         auto PrefixIt = OpcodeTable.find(I);
150         // If we have a non-empty table for it that's not 0, this is a prefix.
151         if (PrefixIt != OpcodeTable.end() && I && !PrefixPair.first) {
152           OS << "  { 0, ET_Prefix, 0, 0";
153         } else {
154           OS << "  { 0, ET_Unused, 0, 0";
155         }
156       }
157       OS << "  },\n";
158     }
159     OS << "};\n\n";
160   }
161   // Create a table of all operands:
162   OS << "const uint8_t OperandTable[] = {\n";
163   for (auto &Op : OperandTable) {
164     OS << "  " << Op << ",\n";
165   }
166   OS << "};\n\n";
167   // Create a table of all extension tables:
168   OS << "struct { uint8_t Prefix; const WebAssemblyInstruction *Table; }\n";
169   OS << "PrefixTable[] = {\n";
170   for (auto &PrefixPair : OpcodeTable) {
171     if (PrefixPair.second.empty() || !PrefixPair.first)
172       continue;
173     OS << "  { " << PrefixPair.first << ", InstructionTable"
174        << PrefixPair.first;
175     OS << " },\n";
176   }
177   OS << "  { 0, nullptr }\n};\n\n";
178   OS << "} // end namespace llvm\n";
179 }
180 
181 } // namespace llvm
182