1 //===- WebAssemblyDisassemblerEmitter.cpp - Disassembler tables -*- C++ -*-===//
3 // 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
7 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
16 #include "WebAssemblyDisassemblerEmitter.h"
17 #include "llvm/TableGen/Record.h"
21 static constexpr int WebAssemblyInstructionTableSize
= 256;
23 void emitWebAssemblyDisassemblerTables(
25 const ArrayRef
<const CodeGenInstruction
*> &NumberedInstructions
) {
26 // First lets organize all opcodes by (prefix) byte. Prefix 0 is the
29 std::map
<unsigned, std::pair
<unsigned, const CodeGenInstruction
*>>>
31 for (unsigned I
= 0; I
!= NumberedInstructions
.size(); ++I
) {
32 auto &CGI
= *NumberedInstructions
[I
];
33 auto &Def
= *CGI
.TheDef
;
34 if (!Def
.getValue("Inst"))
36 auto &Inst
= *Def
.getValueAsBitsInit("Inst");
37 auto Opc
= static_cast<unsigned>(
38 reinterpret_cast<IntInit
*>(Inst
.convertInitializerTo(IntRecTy::get()))
40 if (Opc
== 0xFFFFFFFF)
41 continue; // No opcode defined.
42 assert(Opc
<= 0xFFFF);
43 auto Prefix
= Opc
>> 8;
45 auto &CGIP
= OpcodeTable
[Prefix
][Opc
];
46 // All wasm instructions have a StackBased field of type string, we only
47 // want the instructions for which this is "true".
49 Def
.getValue("StackBased")->getValue()->getCastTo(StringRecTy::get());
52 reinterpret_cast<const StringInit
*>(StackString
)->getValue() == "true";
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")
62 ->getAsString() == "1";
63 // We already have one marked explicitly as canonical, so keep it.
64 if (IsCanonicalExisting
)
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.
71 // Pick the one with the shortest name as heuristic.
72 // 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())
78 // Set this instruction as the one to use.
79 CGIP
= std::make_pair(I
, &CGI
);
81 OS
<< "#include \"MCTargetDesc/WebAssemblyMCTargetDesc.h\"\n";
83 OS
<< "namespace llvm {\n\n";
84 OS
<< "static constexpr int WebAssemblyInstructionTableSize = ";
85 OS
<< WebAssemblyInstructionTableSize
<< ";\n\n";
86 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";
92 OS
<< " uint16_t OperandStart;\n";
94 std::vector
<std::string
> OperandTable
, CurOperandList
;
95 // Output one table per prefix.
96 for (auto &PrefixPair
: OpcodeTable
) {
97 if (PrefixPair
.second
.empty())
99 OS
<< "WebAssemblyInstruction InstructionTable" << PrefixPair
.first
;
101 for (unsigned I
= 0; I
< WebAssemblyInstructionTableSize
; I
++) {
102 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
;
108 OS
.write_hex(static_cast<unsigned long long>(I
));
109 OS
<< ": " << CGI
.AsmString
<< "\n";
110 OS
<< " { " << InstIt
->second
.first
<< ", ET_Instruction, ";
111 OS
<< CGI
.Operands
.OperandList
.size() << ", ";
112 // Collect operand types for storage in a shared list.
113 CurOperandList
.clear();
114 for (auto &Op
: CGI
.Operands
.OperandList
) {
115 assert(Op
.OperandType
!= "MCOI::OPERAND_UNKNOWN");
116 CurOperandList
.push_back(Op
.OperandType
);
118 // 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();
125 for (; K
< CurOperandList
.size(); ++K
) {
126 if (OperandTable
[J
+ K
] != CurOperandList
[K
]) break;
128 if (K
== CurOperandList
.size()) {
134 // Store operands if no prior occurrence.
135 if (OperandStart
== OperandTable
.size()) {
136 OperandTable
.insert(OperandTable
.end(), CurOperandList
.begin(),
137 CurOperandList
.end());
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
) {
144 OS
<< " { 0, ET_Prefix, 0, 0";
146 OS
<< " { 0, ET_Unused, 0, 0";
153 // Create a table of all operands:
154 OS
<< "const uint8_t OperandTable[] = {\n";
155 for (auto &Op
: OperandTable
) {
156 OS
<< " " << Op
<< ",\n";
159 // 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
)
165 OS
<< " { " << PrefixPair
.first
<< ", InstructionTable"
169 OS
<< " { 0, nullptr }\n};\n\n";
170 OS
<< "} // end namespace llvm\n";