1 //===- WebAssemblyDisassemblerEmitter.cpp - Disassembler tables -*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file is part of the WebAssembly Disassembler Emitter.
11 // It contains the implementation of the disassembler tables.
12 // Documentation for the disassembler emitter in general can be found in
13 // WebAssemblyDisassemblerEmitter.h.
14 //
15 //===----------------------------------------------------------------------===//
16 
17 #include "WebAssemblyDisassemblerEmitter.h"
18 #include "llvm/TableGen/Record.h"
19 
20 namespace llvm {
21 
22 static constexpr int WebAssemblyInstructionTableSize = 256;
23 
24 void emitWebAssemblyDisassemblerTables(
25     raw_ostream &OS,
26     const ArrayRef<const CodeGenInstruction *> &NumberedInstructions) {
27   // First lets organize all opcodes by (prefix) byte. Prefix 0 is the
28   // starting table.
29   std::map<unsigned,
30            std::map<unsigned, std::pair<unsigned, const CodeGenInstruction *>>>
31       OpcodeTable;
32   for (unsigned I = 0; I != NumberedInstructions.size(); ++I) {
33     auto &CGI = *NumberedInstructions[I];
34     auto &Def = *CGI.TheDef;
35     if (!Def.getValue("Inst"))
36       continue;
37     auto &Inst = *Def.getValueAsBitsInit("Inst");
38     auto Opc = static_cast<unsigned>(
39         reinterpret_cast<IntInit *>(Inst.convertInitializerTo(IntRecTy::get()))
40             ->getValue());
41     if (Opc == 0xFFFFFFFF)
42       continue; // No opcode defined.
43     assert(Opc <= 0xFFFF);
44     auto Prefix = Opc >> 8;
45     Opc = Opc & 0xFF;
46     auto &CGIP = OpcodeTable[Prefix][Opc];
47     // All wasm instructions have a StackBased field of type string, we only
48     // want the instructions for which this is "true".
49     auto StackString =
50         Def.getValue("StackBased")->getValue()->getCastTo(StringRecTy::get());
51     auto IsStackBased =
52         StackString &&
53         reinterpret_cast<const StringInit *>(StackString)->getValue() == "true";
54     if (IsStackBased && !CGIP.second) {
55       // this picks the first of many typed variants, which is
56       // currently the except_ref one, though this shouldn't matter for
57       // disassembly purposes.
58       CGIP = std::make_pair(I, &CGI);
59     }
60   }
61   OS << "#include \"MCTargetDesc/WebAssemblyMCTargetDesc.h\"\n";
62   OS << "\n";
63   OS << "namespace llvm {\n\n";
64   OS << "static constexpr int WebAssemblyInstructionTableSize = ";
65   OS << WebAssemblyInstructionTableSize << ";\n\n";
66   OS << "enum EntryType : uint8_t { ";
67   OS << "ET_Unused, ET_Prefix, ET_Instruction };\n\n";
68   OS << "struct WebAssemblyInstruction {\n";
69   OS << "  uint16_t Opcode;\n";
70   OS << "  EntryType ET;\n";
71   OS << "  uint8_t NumOperands;\n";
72   OS << "  uint16_t OperandStart;\n";
73   OS << "};\n\n";
74   std::vector<std::string> OperandTable, CurOperandList;
75   // Output one table per prefix.
76   for (auto &PrefixPair : OpcodeTable) {
77     if (PrefixPair.second.empty())
78       continue;
79     OS << "WebAssemblyInstruction InstructionTable" << PrefixPair.first;
80     OS << "[] = {\n";
81     for (unsigned I = 0; I < WebAssemblyInstructionTableSize; I++) {
82       auto InstIt = PrefixPair.second.find(I);
83       if (InstIt != PrefixPair.second.end()) {
84         // Regular instruction.
85         assert(InstIt->second.second);
86         auto &CGI = *InstIt->second.second;
87         OS << "  // 0x";
88         OS.write_hex(static_cast<unsigned long long>(I));
89         OS << ": " << CGI.AsmString << "\n";
90         OS << "  { " << InstIt->second.first << ", ET_Instruction, ";
91         OS << CGI.Operands.OperandList.size() << ", ";
92         // Collect operand types for storage in a shared list.
93         CurOperandList.clear();
94         for (auto &Op : CGI.Operands.OperandList) {
95           CurOperandList.push_back(Op.OperandType);
96         }
97         // See if we already have stored this sequence before. This is not
98         // strictly necessary but makes the table really small.
99         size_t OperandStart = OperandTable.size();
100         if (CurOperandList.size() <= OperandTable.size()) {
101           for (size_t J = 0; J <= OperandTable.size() - CurOperandList.size();
102                ++J) {
103             size_t K = 0;
104             for (; K < CurOperandList.size(); ++K) {
105               if (OperandTable[J + K] != CurOperandList[K]) break;
106             }
107             if (K == CurOperandList.size()) {
108               OperandStart = J;
109               break;
110             }
111           }
112         }
113         // Store operands if no prior occurrence.
114         if (OperandStart == OperandTable.size()) {
115           OperandTable.insert(OperandTable.end(), CurOperandList.begin(),
116                               CurOperandList.end());
117         }
118         OS << OperandStart;
119       } else {
120         auto PrefixIt = OpcodeTable.find(I);
121         // If we have a non-empty table for it that's not 0, this is a prefix.
122         if (PrefixIt != OpcodeTable.end() && I && !PrefixPair.first) {
123           OS << "  { 0, ET_Prefix, 0, 0";
124         } else {
125           OS << "  { 0, ET_Unused, 0, 0";
126         }
127       }
128       OS << "  },\n";
129     }
130     OS << "};\n\n";
131   }
132   // Create a table of all operands:
133   OS << "const uint8_t OperandTable[] = {\n";
134   for (auto &Op : OperandTable) {
135     OS << "  " << Op << ",\n";
136   }
137   OS << "};\n\n";
138   // Create a table of all extension tables:
139   OS << "struct { uint8_t Prefix; const WebAssemblyInstruction *Table; }\n";
140   OS << "PrefixTable[] = {\n";
141   for (auto &PrefixPair : OpcodeTable) {
142     if (PrefixPair.second.empty() || !PrefixPair.first)
143       continue;
144     OS << "  { " << PrefixPair.first << ", InstructionTable"
145        << PrefixPair.first;
146     OS << " },\n";
147   }
148   OS << "  { 0, nullptr }\n};\n\n";
149   OS << "} // End llvm namespace\n";
150 }
151 
152 } // namespace llvm
153