blob: bf8b1271e9af4a5747debd2f5d02d9ceeca0b626 [file] [log] [blame]
Sean Callanan8ed9f512009-12-19 02:59:52 +00001//===- X86DisassemblerTables.h - 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 X86 Disassembler Emitter.
11// It contains the interface of the disassembler tables.
12// Documentation for the disassembler emitter in general can be found in
13// X86DisasemblerEmitter.h.
14//
15//===----------------------------------------------------------------------===//
16
17#ifndef X86DISASSEMBLERTABLES_H
18#define X86DISASSEMBLERTABLES_H
19
20#include "X86DisassemblerShared.h"
21#include "X86ModRMFilters.h"
Sean Callanan8ed9f512009-12-19 02:59:52 +000022#include "llvm/Support/raw_ostream.h"
Craig Topper39004b52013-09-30 06:23:19 +000023#include <map>
Sean Callanan8ed9f512009-12-19 02:59:52 +000024#include <vector>
25
26namespace llvm {
27
28namespace X86Disassembler {
29
30/// DisassemblerTables - Encapsulates all the decode tables being generated by
31/// the table emitter. Contains functions to populate the tables as well as
32/// to emit them as hierarchical C structures suitable for consumption by the
33/// runtime.
34class DisassemblerTables {
35private:
36 /// The decoder tables. There is one for each opcode type:
37 /// [0] one-byte opcodes
38 /// [1] two-byte opcodes of the form 0f __
39 /// [2] three-byte opcodes of the form 0f 38 __
40 /// [3] three-byte opcodes of the form 0f 3a __
Joerg Sonnenberger4a8ac8d2011-04-04 16:58:13 +000041 /// [4] three-byte opcodes of the form 0f a6 __
42 /// [5] three-byte opcodes of the form 0f a7 __
Craig Topper279d2822013-10-03 05:17:48 +000043 /// [6] XOP8 map opcode
44 /// [7] XOP9 map opcode
45 /// [8] XOPA map opcode
46 ContextDecision* Tables[9];
Craig Toppera31359a2012-07-31 05:28:41 +000047
Craig Topper39004b52013-09-30 06:23:19 +000048 // Table of ModRM encodings.
49 typedef std::map<std::vector<unsigned>, unsigned> ModRMMapTy;
50 mutable ModRMMapTy ModRMTable;
51
Sean Callanan8ed9f512009-12-19 02:59:52 +000052 /// The instruction information table
53 std::vector<InstructionSpecifier> InstructionSpecifiers;
Craig Toppera31359a2012-07-31 05:28:41 +000054
Sean Callanan8ed9f512009-12-19 02:59:52 +000055 /// True if there are primary decode conflicts in the instruction set
56 bool HasConflicts;
Craig Toppera31359a2012-07-31 05:28:41 +000057
Sean Callanan8ed9f512009-12-19 02:59:52 +000058 /// emitModRMDecision - Emits a table of entries corresponding to a single
59 /// ModR/M decision. Compacts the ModR/M decision if possible. ModR/M
60 /// decisions are printed as:
61 ///
62 /// { /* struct ModRMDecision */
63 /// TYPE,
64 /// modRMTablennnn
65 /// }
66 ///
67 /// where nnnn is a unique ID for the corresponding table of IDs.
68 /// TYPE indicates whether the table has one entry that is the same
69 /// regardless of ModR/M byte, two entries - one for bytes 0x00-0xbf and one
Craig Toppera31359a2012-07-31 05:28:41 +000070 /// for bytes 0xc0-0xff -, or 256 entries, one for each possible byte.
Sean Callanan8ed9f512009-12-19 02:59:52 +000071 /// nnnn is the number of a table for looking up these values. The tables
Chris Lattner7a2bdde2011-04-15 05:18:47 +000072 /// are written separately so that tables consisting entirely of zeros will
Sean Callanan8ed9f512009-12-19 02:59:52 +000073 /// not be duplicated. (These all have the name modRMEmptyTable.) A table
74 /// is printed as:
Craig Toppera31359a2012-07-31 05:28:41 +000075 ///
Sean Callanan8ed9f512009-12-19 02:59:52 +000076 /// InstrUID modRMTablennnn[k] = {
77 /// nnnn, /* MNEMONIC */
78 /// ...
79 /// nnnn /* MNEMONIC */
80 /// };
81 ///
82 /// @param o1 - The output stream to print the ID table to.
83 /// @param o2 - The output stream to print the decision structure to.
84 /// @param i1 - The indentation level to use with stream o1.
85 /// @param i2 - The indentation level to use with stream o2.
Craig Topper39004b52013-09-30 06:23:19 +000086 /// @param ModRMTableNum - next table number for adding to ModRMTable.
Sean Callanan8ed9f512009-12-19 02:59:52 +000087 /// @param decision - The ModR/M decision to emit. This decision has 256
88 /// entries - emitModRMDecision decides how to compact it.
Craig Topper39004b52013-09-30 06:23:19 +000089 void emitModRMDecision(raw_ostream &o1, raw_ostream &o2,
90 unsigned &i1, unsigned &i2, unsigned &ModRMTableNum,
Sean Callanan8ed9f512009-12-19 02:59:52 +000091 ModRMDecision &decision) const;
Craig Toppera31359a2012-07-31 05:28:41 +000092
Sean Callanan8ed9f512009-12-19 02:59:52 +000093 /// emitOpcodeDecision - Emits an OpcodeDecision and all its subsidiary ModR/M
94 /// decisions. An OpcodeDecision is printed as:
95 ///
96 /// { /* struct OpcodeDecision */
97 /// /* 0x00 */
98 /// { /* struct ModRMDecision */
99 /// ...
100 /// }
101 /// ...
102 /// }
103 ///
104 /// where the ModRMDecision structure is printed as described in the
105 /// documentation for emitModRMDecision(). emitOpcodeDecision() passes on a
106 /// stream and indent level for the UID tables generated by
107 /// emitModRMDecision(), but does not use them itself.
108 ///
109 /// @param o1 - The output stream to print the ID tables generated by
110 /// emitModRMDecision() to.
111 /// @param o2 - The output stream for the decision structure itself.
112 /// @param i1 - The indent level to use with stream o1.
113 /// @param i2 - The indent level to use with stream o2.
Craig Topper39004b52013-09-30 06:23:19 +0000114 /// @param ModRMTableNum - next table number for adding to ModRMTable.
Sean Callanan8ed9f512009-12-19 02:59:52 +0000115 /// @param decision - The OpcodeDecision to emit along with its subsidiary
116 /// structures.
Craig Topper39004b52013-09-30 06:23:19 +0000117 void emitOpcodeDecision(raw_ostream &o1, raw_ostream &o2,
118 unsigned &i1, unsigned &i2, unsigned &ModRMTableNum,
Sean Callanan8ed9f512009-12-19 02:59:52 +0000119 OpcodeDecision &decision) const;
Craig Toppera31359a2012-07-31 05:28:41 +0000120
121 /// emitContextDecision - Emits a ContextDecision and all its subsidiary
Sean Callanan8ed9f512009-12-19 02:59:52 +0000122 /// Opcode and ModRMDecisions. A ContextDecision is printed as:
123 ///
124 /// struct ContextDecision NAME = {
125 /// { /* OpcodeDecisions */
126 /// /* IC */
127 /// { /* struct OpcodeDecision */
128 /// ...
129 /// },
130 /// ...
131 /// }
132 /// }
133 ///
Joerg Sonnenberger4a8ac8d2011-04-04 16:58:13 +0000134 /// NAME is the name of the ContextDecision (typically one of the four names
135 /// ONEBYTE_SYM, TWOBYTE_SYM, THREEBYTE38_SYM, THREEBYTE3A_SYM,
136 /// THREEBYTEA6_SYM, and THREEBYTEA7_SYM from
Sean Callanan8ed9f512009-12-19 02:59:52 +0000137 /// X86DisassemblerDecoderCommon.h).
138 /// IC is one of the contexts in InstructionContext. There is an opcode
139 /// decision for each possible context.
140 /// The OpcodeDecision structures are printed as described in the
141 /// documentation for emitOpcodeDecision.
142 ///
143 /// @param o1 - The output stream to print the ID tables generated by
144 /// emitModRMDecision() to.
145 /// @param o2 - The output stream to print the decision structure to.
146 /// @param i1 - The indent level to use with stream o1.
147 /// @param i2 - The indent level to use with stream o2.
Craig Topper39004b52013-09-30 06:23:19 +0000148 /// @param ModRMTableNum - next table number for adding to ModRMTable.
Sean Callanan8ed9f512009-12-19 02:59:52 +0000149 /// @param decision - The ContextDecision to emit along with its subsidiary
150 /// structures.
151 /// @param name - The name for the ContextDecision.
Craig Topper39004b52013-09-30 06:23:19 +0000152 void emitContextDecision(raw_ostream &o1, raw_ostream &o2,
153 unsigned &i1, unsigned &i2, unsigned &ModRMTableNum,
154 ContextDecision &decision, const char* name) const;
Craig Toppera31359a2012-07-31 05:28:41 +0000155
Sean Callanan8ed9f512009-12-19 02:59:52 +0000156 /// emitInstructionInfo - Prints the instruction specifier table, which has
157 /// one entry for each instruction, and contains name and operand
158 /// information. This table is printed as:
159 ///
160 /// struct InstructionSpecifier CONTEXTS_SYM[k] = {
161 /// {
162 /// /* nnnn */
163 /// "MNEMONIC",
164 /// 0xnn,
165 /// {
166 /// {
167 /// ENCODING,
168 /// TYPE
169 /// },
170 /// ...
171 /// }
172 /// },
173 /// };
174 ///
175 /// k is the total number of instructions.
Craig Toppera31359a2012-07-31 05:28:41 +0000176 /// nnnn is the ID of the current instruction (0-based). This table
Sean Callanan8ed9f512009-12-19 02:59:52 +0000177 /// includes entries for non-instructions like PHINODE.
178 /// 0xnn is the lowest possible opcode for the current instruction, used for
179 /// AddRegFrm instructions to compute the operand's value.
180 /// ENCODING and TYPE describe the encoding and type for a single operand.
181 ///
Craig Toppera31359a2012-07-31 05:28:41 +0000182 /// @param o - The output stream to which the instruction table should be
Sean Callanan8ed9f512009-12-19 02:59:52 +0000183 /// written.
184 /// @param i - The indent level for use with the stream.
Craig Topper39004b52013-09-30 06:23:19 +0000185 void emitInstructionInfo(raw_ostream &o, unsigned &i) const;
Craig Toppera31359a2012-07-31 05:28:41 +0000186
Sean Callanan8ed9f512009-12-19 02:59:52 +0000187 /// emitContextTable - Prints the table that is used to translate from an
188 /// instruction attribute mask to an instruction context. This table is
189 /// printed as:
190 ///
191 /// InstructionContext CONTEXTS_STR[256] = {
192 /// IC, /* 0x00 */
193 /// ...
194 /// };
195 ///
196 /// IC is the context corresponding to the mask 0x00, and there are 256
197 /// possible masks.
198 ///
199 /// @param o - The output stream to which the context table should be written.
200 /// @param i - The indent level for use with the stream.
201 void emitContextTable(raw_ostream &o, uint32_t &i) const;
Craig Toppera31359a2012-07-31 05:28:41 +0000202
Sean Callanan8ed9f512009-12-19 02:59:52 +0000203 /// emitContextDecisions - Prints all four ContextDecision structures using
204 /// emitContextDecision().
205 ///
206 /// @param o1 - The output stream to print the ID tables generated by
207 /// emitModRMDecision() to.
208 /// @param o2 - The output stream to print the decision structures to.
209 /// @param i1 - The indent level to use with stream o1.
210 /// @param i2 - The indent level to use with stream o2.
Craig Topper39004b52013-09-30 06:23:19 +0000211 /// @param ModRMTableNum - next table number for adding to ModRMTable.
212 void emitContextDecisions(raw_ostream &o1, raw_ostream &o2,
213 unsigned &i1, unsigned &i2,
214 unsigned &ModRMTableNum) const;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000215
216 /// setTableFields - Uses a ModRMFilter to set the appropriate entries in a
217 /// ModRMDecision to refer to a particular instruction ID.
218 ///
219 /// @param decision - The ModRMDecision to populate.
220 /// @param filter - The filter to use in deciding which entries to populate.
221 /// @param uid - The unique ID to set matching entries to.
222 /// @param opcode - The opcode of the instruction, for error reporting.
223 void setTableFields(ModRMDecision &decision,
224 const ModRMFilter &filter,
225 InstrUID uid,
226 uint8_t opcode);
227public:
228 /// Constructor - Allocates space for the class decisions and clears them.
229 DisassemblerTables();
Craig Toppera31359a2012-07-31 05:28:41 +0000230
Sean Callanan8ed9f512009-12-19 02:59:52 +0000231 ~DisassemblerTables();
Craig Toppera31359a2012-07-31 05:28:41 +0000232
Sean Callanan8ed9f512009-12-19 02:59:52 +0000233 /// emit - Emits the instruction table, context table, and class decisions.
234 ///
235 /// @param o - The output stream to print the tables to.
236 void emit(raw_ostream &o) const;
Craig Toppera31359a2012-07-31 05:28:41 +0000237
Sean Callanan8ed9f512009-12-19 02:59:52 +0000238 /// setTableFields - Uses the opcode type, instruction context, opcode, and a
239 /// ModRMFilter as criteria to set a particular set of entries in the
240 /// decode tables to point to a specific uid.
241 ///
242 /// @param type - The opcode type (ONEBYTE, TWOBYTE, etc.)
243 /// @param insnContext - The context to use (IC, IC_64BIT, etc.)
244 /// @param opcode - The last byte of the opcode (not counting any escape
245 /// or extended opcodes).
246 /// @param filter - The ModRMFilter that decides which ModR/M byte values
247 /// correspond to the desired instruction.
248 /// @param uid - The unique ID of the instruction.
Craig Topper4da632e2011-09-23 06:57:25 +0000249 /// @param is32bit - Instructon is only 32-bit
Craig Topper6744a172011-10-04 06:30:42 +0000250 /// @param ignoresVEX_L - Instruction ignores VEX.L
Sean Callanan8ed9f512009-12-19 02:59:52 +0000251 void setTableFields(OpcodeType type,
252 InstructionContext insnContext,
253 uint8_t opcode,
254 const ModRMFilter &filter,
Craig Topper4da632e2011-09-23 06:57:25 +0000255 InstrUID uid,
Craig Topper6744a172011-10-04 06:30:42 +0000256 bool is32bit,
Craig Toppera31359a2012-07-31 05:28:41 +0000257 bool ignoresVEX_L);
258
Sean Callanan8ed9f512009-12-19 02:59:52 +0000259 /// specForUID - Returns the instruction specifier for a given unique
260 /// instruction ID. Used when resolving collisions.
261 ///
262 /// @param uid - The unique ID of the instruction.
Craig Toppera31359a2012-07-31 05:28:41 +0000263 /// @return - A reference to the instruction specifier.
Sean Callanan8ed9f512009-12-19 02:59:52 +0000264 InstructionSpecifier& specForUID(InstrUID uid) {
265 if (uid >= InstructionSpecifiers.size())
266 InstructionSpecifiers.resize(uid + 1);
Craig Toppera31359a2012-07-31 05:28:41 +0000267
Sean Callanan8ed9f512009-12-19 02:59:52 +0000268 return InstructionSpecifiers[uid];
269 }
Craig Toppera31359a2012-07-31 05:28:41 +0000270
Sean Callanan8ed9f512009-12-19 02:59:52 +0000271 // hasConflicts - Reports whether there were primary decode conflicts
272 // from any instructions added to the tables.
273 // @return - true if there were; false otherwise.
Craig Toppera31359a2012-07-31 05:28:41 +0000274
Sean Callanan8ed9f512009-12-19 02:59:52 +0000275 bool hasConflicts() {
276 return HasConflicts;
277 }
278};
279
280} // namespace X86Disassembler
281
282} // namespace llvm
283
284#endif