1 //===- WebAssemblyDisassemblerEmitter.cpp - Disassembler tables -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
15 //===----------------------------------------------------------------------===//
17 #include "WebAssemblyDisassemblerEmitter.h"
18 #include "llvm/TableGen/Record.h"
22 static constexpr int WebAssemblyInstructionTableSize = 256;
24 void emitWebAssemblyDisassemblerTables(
26 const ArrayRef<const CodeGenInstruction *> &NumberedInstructions) {
27 // First lets organize all opcodes by (prefix) byte. Prefix 0 is the
30 std::map<unsigned, std::pair<unsigned, const CodeGenInstruction *>>>
32 for (unsigned I = 0; I != NumberedInstructions.size(); ++I) {
33 auto &CGI = *NumberedInstructions[I];
34 auto &Def = *CGI.TheDef;
35 if (!Def.getValue("Inst"))
37 auto &Inst = *Def.getValueAsBitsInit("Inst");
38 auto Opc = static_cast<unsigned>(
39 reinterpret_cast<IntInit *>(Inst.convertInitializerTo(IntRecTy::get()))
41 if (Opc == 0xFFFFFFFF)
42 continue; // No opcode defined.
43 assert(Opc <= 0xFFFF);
44 auto Prefix = Opc >> 8;
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".
50 Def.getValue("StackBased")->getValue()->getCastTo(StringRecTy::get());
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);
61 OS << "#include \"MCTargetDesc/WebAssemblyMCTargetDesc.h\"\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";
74 std::vector<std::string> OperandTable, CurOperandList;
75 // Output one table per prefix.
76 for (auto &PrefixPair : OpcodeTable) {
77 if (PrefixPair.second.empty())
79 OS << "WebAssemblyInstruction InstructionTable" << PrefixPair.first;
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;
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);
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();
105 for (; K < CurOperandList.size(); ++K) {
106 if (OperandTable[J + K] != CurOperandList[K]) break;
108 if (K == CurOperandList.size()) {
114 // Store operands if no prior occurrence.
115 if (OperandStart == OperandTable.size()) {
116 OperandTable.insert(OperandTable.end(), CurOperandList.begin(),
117 CurOperandList.end());
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";
126 OS << " { 0, ET_Unused, 0, 0";
133 // Create a table of all operands:
134 OS << "const uint8_t OperandTable[] = {\n";
135 for (auto &Op : OperandTable) {
136 OS << " " << Op << ",\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)
145 OS << " { " << PrefixPair.first << ", InstructionTable"
149 OS << " { 0, nullptr }\n};\n\n";
150 OS << "} // End llvm namespace\n";