1 //===- X86DisassemblerTables.h - 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 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"
28 namespace X86Disassembler
{
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
36 class DisassemblerTables
{
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
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:
66 /// { /* struct ModRMDecision */
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
80 /// InstrUID modRMTablennnn[k] = {
81 /// nnnn, /* MNEMONIC */
83 /// nnnn /* MNEMONIC */
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:
100 /// { /* struct OpcodeDecision */
102 /// { /* struct ModRMDecision */
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
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 */
131 /// { /* struct OpcodeDecision */
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
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] = {
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
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
194 /// InstructionContext CONTEXTS_STR[256] = {
199 /// IC is the context corresponding to the mask 0x00, and there are 256
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
,
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
,
260 const ModRMFilter
&filter
,
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() {
289 } // namespace X86Disassembler