Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / llvm / utils / TableGen / X86DisassemblerTables.h
blob4b6f6543acccfca9e786442b5510824c33973ad7
1 //===- X86DisassemblerTables.h - Disassembler tables ------------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file is part of the X86 Disassembler Emitter.
10 // It contains the interface of the disassembler tables.
11 // Documentation for the disassembler emitter in general can be found in
12 // X86DisassemblerEmitter.h.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_UTILS_TABLEGEN_X86DISASSEMBLERTABLES_H
17 #define LLVM_UTILS_TABLEGEN_X86DISASSEMBLERTABLES_H
19 #include "X86DisassemblerShared.h"
20 #include "llvm/Support/X86DisassemblerDecoderCommon.h"
21 #include <map>
22 #include <memory>
23 #include <vector>
25 namespace llvm {
26 class raw_ostream;
28 namespace X86Disassembler {
30 class ModRMFilter;
32 /// DisassemblerTables - Encapsulates all the decode tables being generated by
33 /// the table emitter. Contains functions to populate the tables as well as
34 /// to emit them as hierarchical C structures suitable for consumption by the
35 /// runtime.
36 class DisassemblerTables {
37 private:
38 /// The decoder tables. There is one for each opcode type:
39 /// [0] one-byte opcodes
40 /// [1] two-byte opcodes of the form 0f __
41 /// [2] three-byte opcodes of the form 0f 38 __
42 /// [3] three-byte opcodes of the form 0f 3a __
43 /// [4] XOP8 map opcode
44 /// [5] XOP9 map opcode
45 /// [6] XOPA map opcode
46 /// [7] 3dnow map opcode
47 /// [8] fixed length MAP5 opcode
48 /// [9] fixed length MAP6 opcode
49 /// [10] fixed length MAP7 opcode
50 std::unique_ptr<ContextDecision> Tables[11];
52 // Table of ModRM encodings.
53 typedef std::map<std::vector<unsigned>, unsigned> ModRMMapTy;
54 mutable ModRMMapTy ModRMTable;
56 /// The instruction information table
57 std::vector<InstructionSpecifier> InstructionSpecifiers;
59 /// True if there are primary decode conflicts in the instruction set
60 bool HasConflicts;
62 /// emitModRMDecision - Emits a table of entries corresponding to a single
63 /// ModR/M decision. Compacts the ModR/M decision if possible. ModR/M
64 /// decisions are printed as:
65 ///
66 /// { /* struct ModRMDecision */
67 /// TYPE,
68 /// modRMTablennnn
69 /// }
70 ///
71 /// where nnnn is a unique ID for the corresponding table of IDs.
72 /// TYPE indicates whether the table has one entry that is the same
73 /// regardless of ModR/M byte, two entries - one for bytes 0x00-0xbf and one
74 /// for bytes 0xc0-0xff -, or 256 entries, one for each possible byte.
75 /// nnnn is the number of a table for looking up these values. The tables
76 /// are written separately so that tables consisting entirely of zeros will
77 /// not be duplicated. (These all have the name modRMEmptyTable.) A table
78 /// is printed as:
79 ///
80 /// InstrUID modRMTablennnn[k] = {
81 /// nnnn, /* MNEMONIC */
82 /// ...
83 /// nnnn /* MNEMONIC */
84 /// };
85 ///
86 /// @param o1 - The output stream to print the ID table to.
87 /// @param o2 - The output stream to print the decision structure to.
88 /// @param i1 - The indentation level to use with stream o1.
89 /// @param i2 - The indentation level to use with stream o2.
90 /// @param ModRMTableNum - next table number for adding to ModRMTable.
91 /// @param decision - The ModR/M decision to emit. This decision has 256
92 /// entries - emitModRMDecision decides how to compact it.
93 void emitModRMDecision(raw_ostream &o1, raw_ostream &o2,
94 unsigned &i1, unsigned &i2, unsigned &ModRMTableNum,
95 ModRMDecision &decision) const;
97 /// emitOpcodeDecision - Emits an OpcodeDecision and all its subsidiary ModR/M
98 /// decisions. An OpcodeDecision is printed as:
99 ///
100 /// { /* struct OpcodeDecision */
101 /// /* 0x00 */
102 /// { /* struct ModRMDecision */
103 /// ...
104 /// }
105 /// ...
106 /// }
108 /// where the ModRMDecision structure is printed as described in the
109 /// documentation for emitModRMDecision(). emitOpcodeDecision() passes on a
110 /// stream and indent level for the UID tables generated by
111 /// emitModRMDecision(), but does not use them itself.
113 /// @param o1 - The output stream to print the ID tables generated by
114 /// emitModRMDecision() to.
115 /// @param o2 - The output stream for the decision structure itself.
116 /// @param i1 - The indent level to use with stream o1.
117 /// @param i2 - The indent level to use with stream o2.
118 /// @param ModRMTableNum - next table number for adding to ModRMTable.
119 /// @param decision - The OpcodeDecision to emit along with its subsidiary
120 /// structures.
121 void emitOpcodeDecision(raw_ostream &o1, raw_ostream &o2,
122 unsigned &i1, unsigned &i2, unsigned &ModRMTableNum,
123 OpcodeDecision &decision) const;
125 /// emitContextDecision - Emits a ContextDecision and all its subsidiary
126 /// Opcode and ModRMDecisions. A ContextDecision is printed as:
128 /// struct ContextDecision NAME = {
129 /// { /* OpcodeDecisions */
130 /// /* IC */
131 /// { /* struct OpcodeDecision */
132 /// ...
133 /// },
134 /// ...
135 /// }
136 /// }
138 /// NAME is the name of the ContextDecision (typically one of the four names
139 /// ONEBYTE_SYM, TWOBYTE_SYM, THREEBYTE38_SYM, THREEBYTE3A_SYM from
140 /// X86DisassemblerDecoderCommon.h).
141 /// IC is one of the contexts in InstructionContext. There is an opcode
142 /// decision for each possible context.
143 /// The OpcodeDecision structures are printed as described in the
144 /// documentation for emitOpcodeDecision.
146 /// @param o1 - The output stream to print the ID tables generated by
147 /// emitModRMDecision() to.
148 /// @param o2 - The output stream to print the decision structure to.
149 /// @param i1 - The indent level to use with stream o1.
150 /// @param i2 - The indent level to use with stream o2.
151 /// @param ModRMTableNum - next table number for adding to ModRMTable.
152 /// @param decision - The ContextDecision to emit along with its subsidiary
153 /// structures.
154 /// @param name - The name for the ContextDecision.
155 void emitContextDecision(raw_ostream &o1, raw_ostream &o2,
156 unsigned &i1, unsigned &i2, unsigned &ModRMTableNum,
157 ContextDecision &decision, const char* name) const;
159 /// emitInstructionInfo - Prints the instruction specifier table, which has
160 /// one entry for each instruction, and contains name and operand
161 /// information. This table is printed as:
163 /// struct InstructionSpecifier CONTEXTS_SYM[k] = {
164 /// {
165 /// /* nnnn */
166 /// "MNEMONIC",
167 /// 0xnn,
168 /// {
169 /// {
170 /// ENCODING,
171 /// TYPE
172 /// },
173 /// ...
174 /// }
175 /// },
176 /// };
178 /// k is the total number of instructions.
179 /// nnnn is the ID of the current instruction (0-based). This table
180 /// includes entries for non-instructions like PHINODE.
181 /// 0xnn is the lowest possible opcode for the current instruction, used for
182 /// AddRegFrm instructions to compute the operand's value.
183 /// ENCODING and TYPE describe the encoding and type for a single operand.
185 /// @param o - The output stream to which the instruction table should be
186 /// written.
187 /// @param i - The indent level for use with the stream.
188 void emitInstructionInfo(raw_ostream &o, unsigned &i) const;
190 /// emitContextTable - Prints the table that is used to translate from an
191 /// instruction attribute mask to an instruction context. This table is
192 /// printed as:
194 /// InstructionContext CONTEXTS_STR[256] = {
195 /// IC, /* 0x00 */
196 /// ...
197 /// };
199 /// IC is the context corresponding to the mask 0x00, and there are 256
200 /// possible masks.
202 /// @param o - The output stream to which the context table should be written.
203 /// @param i - The indent level for use with the stream.
204 void emitContextTable(raw_ostream &o, uint32_t &i) const;
206 /// emitContextDecisions - Prints all four ContextDecision structures using
207 /// emitContextDecision().
209 /// @param o1 - The output stream to print the ID tables generated by
210 /// emitModRMDecision() to.
211 /// @param o2 - The output stream to print the decision structures to.
212 /// @param i1 - The indent level to use with stream o1.
213 /// @param i2 - The indent level to use with stream o2.
214 /// @param ModRMTableNum - next table number for adding to ModRMTable.
215 void emitContextDecisions(raw_ostream &o1, raw_ostream &o2,
216 unsigned &i1, unsigned &i2,
217 unsigned &ModRMTableNum) const;
219 /// setTableFields - Uses a ModRMFilter to set the appropriate entries in a
220 /// ModRMDecision to refer to a particular instruction ID.
222 /// @param decision - The ModRMDecision to populate.
223 /// @param filter - The filter to use in deciding which entries to populate.
224 /// @param uid - The unique ID to set matching entries to.
225 /// @param opcode - The opcode of the instruction, for error reporting.
226 void setTableFields(ModRMDecision &decision,
227 const ModRMFilter &filter,
228 InstrUID uid,
229 uint8_t opcode);
230 public:
231 /// Constructor - Allocates space for the class decisions and clears them.
232 DisassemblerTables();
234 ~DisassemblerTables();
236 /// emit - Emits the instruction table, context table, and class decisions.
238 /// @param o - The output stream to print the tables to.
239 void emit(raw_ostream &o) const;
241 /// setTableFields - Uses the opcode type, instruction context, opcode, and a
242 /// ModRMFilter as criteria to set a particular set of entries in the
243 /// decode tables to point to a specific uid.
245 /// @param type - The opcode type (ONEBYTE, TWOBYTE, etc.)
246 /// @param insnContext - The context to use (IC, IC_64BIT, etc.)
247 /// @param opcode - The last byte of the opcode (not counting any escape
248 /// or extended opcodes).
249 /// @param filter - The ModRMFilter that decides which ModR/M byte values
250 /// correspond to the desired instruction.
251 /// @param uid - The unique ID of the instruction.
252 /// @param is32bit - Instructon is only 32-bit
253 /// @param noPrefix - Instruction record has no prefix.
254 /// @param ignoresVEX_L - Instruction ignores VEX.L
255 /// @param ignoresVEX_W - Instruction ignores VEX.W
256 /// @param AddrSize - Instructions address size 16/32/64. 0 is unspecified
257 void setTableFields(OpcodeType type,
258 InstructionContext insnContext,
259 uint8_t opcode,
260 const ModRMFilter &filter,
261 InstrUID uid,
262 bool is32bit,
263 bool noPrefix,
264 bool ignoresVEX_L,
265 bool ignoresVEX_W,
266 unsigned AddrSize);
268 /// specForUID - Returns the instruction specifier for a given unique
269 /// instruction ID. Used when resolving collisions.
271 /// @param uid - The unique ID of the instruction.
272 /// @return - A reference to the instruction specifier.
273 InstructionSpecifier& specForUID(InstrUID uid) {
274 if (uid >= InstructionSpecifiers.size())
275 InstructionSpecifiers.resize(uid + 1);
277 return InstructionSpecifiers[uid];
280 // hasConflicts - Reports whether there were primary decode conflicts
281 // from any instructions added to the tables.
282 // @return - true if there were; false otherwise.
284 bool hasConflicts() {
285 return HasConflicts;
289 } // namespace X86Disassembler
291 } // namespace llvm
293 #endif