xref: /freebsd/contrib/llvm-project/llvm/utils/TableGen/WebAssemblyDisassemblerEmitter.cpp (revision c66ec88fed842fbaad62c30d510644ceb7bd2d71)
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)
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.
71         // Pick the one with the shortest name as heuristic.
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       }
77     }
78     // Set this instruction as the one to use.
79     CGIP = std::make_pair(I, &CGI);
80   }
81   OS << "#include \"MCTargetDesc/WebAssemblyMCTargetDesc.h\"\n";
82   OS << "\n";
83   OS << "namespace llvm {\n\n";
84   OS << "static constexpr int WebAssemblyInstructionTableSize = ";
85   OS << WebAssemblyInstructionTableSize << ";\n\n";
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";
92   OS << "  uint16_t OperandStart;\n";
93   OS << "};\n\n";
94   std::vector<std::string> OperandTable, CurOperandList;
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";
101     for (unsigned I = 0; I < WebAssemblyInstructionTableSize; I++) {
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, ";
111         OS << CGI.Operands.OperandList.size() << ", ";
112         // Collect operand types for storage in a shared list.
113         CurOperandList.clear();
114         for (auto &Op : CGI.Operands.OperandList) {
115           assert(Op.OperandType != "MCOI::OPERAND_UNKNOWN");
116           CurOperandList.push_back(Op.OperandType);
117         }
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;
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) {
144           OS << "  { 0, ET_Prefix, 0, 0";
145         } else {
146           OS << "  { 0, ET_Unused, 0, 0";
147         }
148       }
149       OS << "  },\n";
150     }
151     OS << "};\n\n";
152   }
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";
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";
170   OS << "} // end namespace llvm\n";
171 }
172 
173 } // namespace llvm
174