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