blob: 788f142e125f5d728b3aae1ccf00afdfead52d1e [file] [log] [blame]
Sam Clegg04cf0d72018-05-10 22:16:44 +00001//===- 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
20namespace llvm {
21
Thomas Lively1a899422018-11-09 01:57:00 +000022static constexpr int WebAssemblyInstructionTableSize = 256;
23
Sam Clegg04cf0d72018-05-10 22:16:44 +000024void 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];
Thomas Livelyfbc926a2018-10-22 21:55:26 +000047 // 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";
Wouter van Oortmerssen9a75e492018-08-30 15:40:53 +000054 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.
Sam Clegg04cf0d72018-05-10 22:16:44 +000058 CGIP = std::make_pair(I, &CGI);
59 }
60 }
61 OS << "#include \"MCTargetDesc/WebAssemblyMCTargetDesc.h\"\n";
62 OS << "\n";
63 OS << "namespace llvm {\n\n";
Thomas Lively1a899422018-11-09 01:57:00 +000064 OS << "static constexpr int WebAssemblyInstructionTableSize = ";
65 OS << WebAssemblyInstructionTableSize << ";\n\n";
Sam Clegg04cf0d72018-05-10 22:16:44 +000066 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";
Wouter van Oortmerssen9a75e492018-08-30 15:40:53 +000072 OS << " uint16_t OperandStart;\n";
Sam Clegg04cf0d72018-05-10 22:16:44 +000073 OS << "};\n\n";
Wouter van Oortmerssen9a75e492018-08-30 15:40:53 +000074 std::vector<std::string> OperandTable, CurOperandList;
Sam Clegg04cf0d72018-05-10 22:16:44 +000075 // 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";
Thomas Lively1a899422018-11-09 01:57:00 +000081 for (unsigned I = 0; I < WebAssemblyInstructionTableSize; I++) {
Sam Clegg04cf0d72018-05-10 22:16:44 +000082 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, ";
Wouter van Oortmerssen9a75e492018-08-30 15:40:53 +000091 OS << CGI.Operands.OperandList.size() << ", ";
92 // Collect operand types for storage in a shared list.
93 CurOperandList.clear();
Sam Clegg04cf0d72018-05-10 22:16:44 +000094 for (auto &Op : CGI.Operands.OperandList) {
Wouter van Oortmerssen976e4572019-01-03 23:01:30 +000095 assert(Op.OperandType != "MCOI::OPERAND_UNKNOWN");
Wouter van Oortmerssen9a75e492018-08-30 15:40:53 +000096 CurOperandList.push_back(Op.OperandType);
Sam Clegg04cf0d72018-05-10 22:16:44 +000097 }
Wouter van Oortmerssen9a75e492018-08-30 15:40:53 +000098 // 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;
Sam Clegg04cf0d72018-05-10 22:16:44 +0000120 } 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) {
Wouter van Oortmerssen9a75e492018-08-30 15:40:53 +0000124 OS << " { 0, ET_Prefix, 0, 0";
Sam Clegg04cf0d72018-05-10 22:16:44 +0000125 } else {
Wouter van Oortmerssen9a75e492018-08-30 15:40:53 +0000126 OS << " { 0, ET_Unused, 0, 0";
Sam Clegg04cf0d72018-05-10 22:16:44 +0000127 }
128 }
129 OS << " },\n";
130 }
131 OS << "};\n\n";
132 }
Wouter van Oortmerssen9a75e492018-08-30 15:40:53 +0000133 // 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";
Sam Clegg04cf0d72018-05-10 22:16:44 +0000139 // 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