blob: 54aa5a8164f28c0ca6def5c5f75e7fb83c8f07f9 [file] [log] [blame]
Sam Clegg16c16822018-05-10 22:16:44 +00001//===- WebAssemblyDisassemblerEmitter.cpp - Disassembler tables -*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Sam Clegg16c16822018-05-10 22:16:44 +00006//
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
19namespace llvm {
20
Thomas Lively2faf0792018-11-09 01:57:00 +000021static constexpr int WebAssemblyInstructionTableSize = 256;
22
Sam Clegg16c16822018-05-10 22:16:44 +000023void 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 <= 0xFFFF);
43 auto Prefix = Opc >> 8;
44 Opc = Opc & 0xFF;
45 auto &CGIP = OpcodeTable[Prefix][Opc];
Thomas Livelyc63b5fc2018-10-22 21:55:26 +000046 // All wasm instructions have a StackBased field of type string, we only
47 // want the instructions for which this is "true".
48 auto StackString =
49 Def.getValue("StackBased")->getValue()->getCastTo(StringRecTy::get());
50 auto IsStackBased =
51 StackString &&
52 reinterpret_cast<const StringInit *>(StackString)->getValue() == "true";
Wouter van Oortmerssen1a91cb02019-02-05 01:19:45 +000053 if (!IsStackBased)
54 continue;
55 if (CGIP.second) {
56 // We already have an instruction for this slot, so decide which one
57 // should be the canonical one. This determines which variant gets
58 // printed in a disassembly. We want e.g. "call" not "i32.call", and
59 // "end" when we don't know if its "end_loop" or "end_block" etc.
60 auto IsCanonicalExisting = CGIP.second->TheDef->getValue("IsCanonical")
61 ->getValue()
62 ->getAsString() == "1";
63 // We already have one marked explicitly as canonical, so keep it.
64 if (IsCanonicalExisting)
65 continue;
66 auto IsCanonicalNew =
67 Def.getValue("IsCanonical")->getValue()->getAsString() == "1";
68 // If the new one is explicitly marked as canonical, take it.
69 if (!IsCanonicalNew) {
70 // Neither the existing or new instruction is canonical.
Krasimir Georgiev12971802019-02-05 15:00:56 +000071 // Pick the one with the shortest name as heuristic.
Wouter van Oortmerssen1a91cb02019-02-05 01:19:45 +000072 // Though ideally IsCanonical is always defined for at least one
73 // variant so this never has to apply.
74 if (CGIP.second->AsmString.size() <= CGI.AsmString.size())
75 continue;
76 }
Sam Clegg16c16822018-05-10 22:16:44 +000077 }
Wouter van Oortmerssen1a91cb02019-02-05 01:19:45 +000078 // Set this instruction as the one to use.
79 CGIP = std::make_pair(I, &CGI);
Sam Clegg16c16822018-05-10 22:16:44 +000080 }
81 OS << "#include \"MCTargetDesc/WebAssemblyMCTargetDesc.h\"\n";
82 OS << "\n";
83 OS << "namespace llvm {\n\n";
Thomas Lively2faf0792018-11-09 01:57:00 +000084 OS << "static constexpr int WebAssemblyInstructionTableSize = ";
85 OS << WebAssemblyInstructionTableSize << ";\n\n";
Sam Clegg16c16822018-05-10 22:16:44 +000086 OS << "enum EntryType : uint8_t { ";
87 OS << "ET_Unused, ET_Prefix, ET_Instruction };\n\n";
88 OS << "struct WebAssemblyInstruction {\n";
89 OS << " uint16_t Opcode;\n";
90 OS << " EntryType ET;\n";
91 OS << " uint8_t NumOperands;\n";
Wouter van Oortmerssena733d082018-08-30 15:40:53 +000092 OS << " uint16_t OperandStart;\n";
Sam Clegg16c16822018-05-10 22:16:44 +000093 OS << "};\n\n";
Wouter van Oortmerssena733d082018-08-30 15:40:53 +000094 std::vector<std::string> OperandTable, CurOperandList;
Sam Clegg16c16822018-05-10 22:16:44 +000095 // Output one table per prefix.
96 for (auto &PrefixPair : OpcodeTable) {
97 if (PrefixPair.second.empty())
98 continue;
99 OS << "WebAssemblyInstruction InstructionTable" << PrefixPair.first;
100 OS << "[] = {\n";
Thomas Lively2faf0792018-11-09 01:57:00 +0000101 for (unsigned I = 0; I < WebAssemblyInstructionTableSize; I++) {
Sam Clegg16c16822018-05-10 22:16:44 +0000102 auto InstIt = PrefixPair.second.find(I);
103 if (InstIt != PrefixPair.second.end()) {
104 // Regular instruction.
105 assert(InstIt->second.second);
106 auto &CGI = *InstIt->second.second;
107 OS << " // 0x";
108 OS.write_hex(static_cast<unsigned long long>(I));
109 OS << ": " << CGI.AsmString << "\n";
110 OS << " { " << InstIt->second.first << ", ET_Instruction, ";
Wouter van Oortmerssena733d082018-08-30 15:40:53 +0000111 OS << CGI.Operands.OperandList.size() << ", ";
112 // Collect operand types for storage in a shared list.
113 CurOperandList.clear();
Sam Clegg16c16822018-05-10 22:16:44 +0000114 for (auto &Op : CGI.Operands.OperandList) {
Wouter van Oortmerssen820c6262019-01-03 23:01:30 +0000115 assert(Op.OperandType != "MCOI::OPERAND_UNKNOWN");
Wouter van Oortmerssena733d082018-08-30 15:40:53 +0000116 CurOperandList.push_back(Op.OperandType);
Sam Clegg16c16822018-05-10 22:16:44 +0000117 }
Wouter van Oortmerssena733d082018-08-30 15:40:53 +0000118 // See if we already have stored this sequence before. This is not
119 // strictly necessary but makes the table really small.
120 size_t OperandStart = OperandTable.size();
121 if (CurOperandList.size() <= OperandTable.size()) {
122 for (size_t J = 0; J <= OperandTable.size() - CurOperandList.size();
123 ++J) {
124 size_t K = 0;
125 for (; K < CurOperandList.size(); ++K) {
126 if (OperandTable[J + K] != CurOperandList[K]) break;
127 }
128 if (K == CurOperandList.size()) {
129 OperandStart = J;
130 break;
131 }
132 }
133 }
134 // Store operands if no prior occurrence.
135 if (OperandStart == OperandTable.size()) {
136 OperandTable.insert(OperandTable.end(), CurOperandList.begin(),
137 CurOperandList.end());
138 }
139 OS << OperandStart;
Sam Clegg16c16822018-05-10 22:16:44 +0000140 } else {
141 auto PrefixIt = OpcodeTable.find(I);
142 // If we have a non-empty table for it that's not 0, this is a prefix.
143 if (PrefixIt != OpcodeTable.end() && I && !PrefixPair.first) {
Wouter van Oortmerssena733d082018-08-30 15:40:53 +0000144 OS << " { 0, ET_Prefix, 0, 0";
Sam Clegg16c16822018-05-10 22:16:44 +0000145 } else {
Wouter van Oortmerssena733d082018-08-30 15:40:53 +0000146 OS << " { 0, ET_Unused, 0, 0";
Sam Clegg16c16822018-05-10 22:16:44 +0000147 }
148 }
149 OS << " },\n";
150 }
151 OS << "};\n\n";
152 }
Wouter van Oortmerssena733d082018-08-30 15:40:53 +0000153 // Create a table of all operands:
154 OS << "const uint8_t OperandTable[] = {\n";
155 for (auto &Op : OperandTable) {
156 OS << " " << Op << ",\n";
157 }
158 OS << "};\n\n";
Sam Clegg16c16822018-05-10 22:16:44 +0000159 // Create a table of all extension tables:
160 OS << "struct { uint8_t Prefix; const WebAssemblyInstruction *Table; }\n";
161 OS << "PrefixTable[] = {\n";
162 for (auto &PrefixPair : OpcodeTable) {
163 if (PrefixPair.second.empty() || !PrefixPair.first)
164 continue;
165 OS << " { " << PrefixPair.first << ", InstructionTable"
166 << PrefixPair.first;
167 OS << " },\n";
168 }
169 OS << " { 0, nullptr }\n};\n\n";
Bjorn Pettersson6bd3a9e2019-08-25 10:47:30 +0000170 OS << "} // end namespace llvm\n";
Sam Clegg16c16822018-05-10 22:16:44 +0000171}
172
173} // namespace llvm