]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/utils/TableGen/WebAssemblyDisassemblerEmitter.cpp
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[FreeBSD/FreeBSD.git] / contrib / llvm / utils / TableGen / WebAssemblyDisassemblerEmitter.cpp
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           assert(Op.OperandType != "MCOI::OPERAND_UNKNOWN");
96           CurOperandList.push_back(Op.OperandType);
97         }
98         // See if we already have stored this sequence before. This is not
99         // strictly necessary but makes the table really small.
100         size_t OperandStart = OperandTable.size();
101         if (CurOperandList.size() <= OperandTable.size()) {
102           for (size_t J = 0; J <= OperandTable.size() - CurOperandList.size();
103                ++J) {
104             size_t K = 0;
105             for (; K < CurOperandList.size(); ++K) {
106               if (OperandTable[J + K] != CurOperandList[K]) break;
107             }
108             if (K == CurOperandList.size()) {
109               OperandStart = J;
110               break;
111             }
112           }
113         }
114         // Store operands if no prior occurrence.
115         if (OperandStart == OperandTable.size()) {
116           OperandTable.insert(OperandTable.end(), CurOperandList.begin(),
117                               CurOperandList.end());
118         }
119         OS << OperandStart;
120       } else {
121         auto PrefixIt = OpcodeTable.find(I);
122         // If we have a non-empty table for it that's not 0, this is a prefix.
123         if (PrefixIt != OpcodeTable.end() && I && !PrefixPair.first) {
124           OS << "  { 0, ET_Prefix, 0, 0";
125         } else {
126           OS << "  { 0, ET_Unused, 0, 0";
127         }
128       }
129       OS << "  },\n";
130     }
131     OS << "};\n\n";
132   }
133   // Create a table of all operands:
134   OS << "const uint8_t OperandTable[] = {\n";
135   for (auto &Op : OperandTable) {
136     OS << "  " << Op << ",\n";
137   }
138   OS << "};\n\n";
139   // Create a table of all extension tables:
140   OS << "struct { uint8_t Prefix; const WebAssemblyInstruction *Table; }\n";
141   OS << "PrefixTable[] = {\n";
142   for (auto &PrefixPair : OpcodeTable) {
143     if (PrefixPair.second.empty() || !PrefixPair.first)
144       continue;
145     OS << "  { " << PrefixPair.first << ", InstructionTable"
146        << PrefixPair.first;
147     OS << " },\n";
148   }
149   OS << "  { 0, nullptr }\n};\n\n";
150   OS << "} // End llvm namespace\n";
151 }
152
153 } // namespace llvm