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