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