blob: 40a0c1b260b726b1ecffd5a4d286be777b2eeeeb [file] [log] [blame]
Sean Callanan8ed9f512009-12-19 02:59:52 +00001//===- X86DisassemblerTables.cpp - 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 implementation of the disassembler tables.
12// Documentation for the disassembler emitter in general can be found in
13// X86DisasemblerEmitter.h.
14//
15//===----------------------------------------------------------------------===//
16
Sean Callanan8ed9f512009-12-19 02:59:52 +000017#include "X86DisassemblerTables.h"
Chandler Carruth4ffd89f2012-12-04 10:37:14 +000018#include "X86DisassemblerShared.h"
Joerg Sonnenberger39d7cae2011-04-04 16:25:38 +000019#include "llvm/ADT/STLExtras.h"
Sean Callanan8ed9f512009-12-19 02:59:52 +000020#include "llvm/Support/ErrorHandling.h"
21#include "llvm/Support/Format.h"
Chandler Carruth4ffd89f2012-12-04 10:37:14 +000022#include "llvm/TableGen/TableGenBackend.h"
Craig Topper5a2c6072012-08-01 07:39:18 +000023#include <map>
Sean Callanan8ed9f512009-12-19 02:59:52 +000024
Sean Callanan8ed9f512009-12-19 02:59:52 +000025using namespace llvm;
26using namespace X86Disassembler;
Craig Topper98400092012-07-31 05:27:01 +000027
Sean Callanan8ed9f512009-12-19 02:59:52 +000028/// inheritsFrom - Indicates whether all instructions in one class also belong
29/// to another class.
30///
31/// @param child - The class that may be the subset
32/// @param parent - The class that may be the superset
33/// @return - True if child is a subset of parent, false otherwise.
34static inline bool inheritsFrom(InstructionContext child,
Craig Topper6744a172011-10-04 06:30:42 +000035 InstructionContext parent,
36 bool VEX_LIG = false) {
Sean Callanan8ed9f512009-12-19 02:59:52 +000037 if (child == parent)
38 return true;
Craig Topper98400092012-07-31 05:27:01 +000039
Sean Callanan8ed9f512009-12-19 02:59:52 +000040 switch (parent) {
41 case IC:
Craig Topper5ffedb92011-09-02 04:17:54 +000042 return(inheritsFrom(child, IC_64BIT) ||
43 inheritsFrom(child, IC_OPSIZE) ||
Craig Topper930a1eb2012-02-27 01:54:29 +000044 inheritsFrom(child, IC_ADSIZE) ||
Craig Topper5ffedb92011-09-02 04:17:54 +000045 inheritsFrom(child, IC_XD) ||
46 inheritsFrom(child, IC_XS));
Sean Callanan8ed9f512009-12-19 02:59:52 +000047 case IC_64BIT:
48 return(inheritsFrom(child, IC_64BIT_REXW) ||
49 inheritsFrom(child, IC_64BIT_OPSIZE) ||
Craig Topper930a1eb2012-02-27 01:54:29 +000050 inheritsFrom(child, IC_64BIT_ADSIZE) ||
Sean Callanan8ed9f512009-12-19 02:59:52 +000051 inheritsFrom(child, IC_64BIT_XD) ||
52 inheritsFrom(child, IC_64BIT_XS));
53 case IC_OPSIZE:
Craig Topper5ffedb92011-09-02 04:17:54 +000054 return inheritsFrom(child, IC_64BIT_OPSIZE);
Craig Topper930a1eb2012-02-27 01:54:29 +000055 case IC_ADSIZE:
56 case IC_64BIT_ADSIZE:
57 return false;
Sean Callanan8ed9f512009-12-19 02:59:52 +000058 case IC_XD:
Craig Topper5ffedb92011-09-02 04:17:54 +000059 return inheritsFrom(child, IC_64BIT_XD);
Sean Callanan8ed9f512009-12-19 02:59:52 +000060 case IC_XS:
Craig Topper5ffedb92011-09-02 04:17:54 +000061 return inheritsFrom(child, IC_64BIT_XS);
Craig Toppere1b4a1a2011-10-01 19:54:56 +000062 case IC_XD_OPSIZE:
63 return inheritsFrom(child, IC_64BIT_XD_OPSIZE);
Craig Topper29480fd2011-10-11 04:34:23 +000064 case IC_XS_OPSIZE:
65 return inheritsFrom(child, IC_64BIT_XS_OPSIZE);
Sean Callanan8ed9f512009-12-19 02:59:52 +000066 case IC_64BIT_REXW:
67 return(inheritsFrom(child, IC_64BIT_REXW_XS) ||
68 inheritsFrom(child, IC_64BIT_REXW_XD) ||
69 inheritsFrom(child, IC_64BIT_REXW_OPSIZE));
70 case IC_64BIT_OPSIZE:
71 return(inheritsFrom(child, IC_64BIT_REXW_OPSIZE));
72 case IC_64BIT_XD:
73 return(inheritsFrom(child, IC_64BIT_REXW_XD));
74 case IC_64BIT_XS:
75 return(inheritsFrom(child, IC_64BIT_REXW_XS));
Craig Toppere1b4a1a2011-10-01 19:54:56 +000076 case IC_64BIT_XD_OPSIZE:
Craig Topper29480fd2011-10-11 04:34:23 +000077 case IC_64BIT_XS_OPSIZE:
Craig Toppere1b4a1a2011-10-01 19:54:56 +000078 return false;
Sean Callanan8ed9f512009-12-19 02:59:52 +000079 case IC_64BIT_REXW_XD:
Sean Callanan8ed9f512009-12-19 02:59:52 +000080 case IC_64BIT_REXW_XS:
Sean Callanan8ed9f512009-12-19 02:59:52 +000081 case IC_64BIT_REXW_OPSIZE:
82 return false;
Sean Callanana21e2ea2011-03-15 01:23:15 +000083 case IC_VEX:
Craig Topper6744a172011-10-04 06:30:42 +000084 return inheritsFrom(child, IC_VEX_W) ||
85 (VEX_LIG && inheritsFrom(child, IC_VEX_L));
Sean Callanana21e2ea2011-03-15 01:23:15 +000086 case IC_VEX_XS:
Craig Topper6744a172011-10-04 06:30:42 +000087 return inheritsFrom(child, IC_VEX_W_XS) ||
88 (VEX_LIG && inheritsFrom(child, IC_VEX_L_XS));
Sean Callanana21e2ea2011-03-15 01:23:15 +000089 case IC_VEX_XD:
Craig Topper6744a172011-10-04 06:30:42 +000090 return inheritsFrom(child, IC_VEX_W_XD) ||
91 (VEX_LIG && inheritsFrom(child, IC_VEX_L_XD));
Craig Topper5ffedb92011-09-02 04:17:54 +000092 case IC_VEX_OPSIZE:
Craig Topper6744a172011-10-04 06:30:42 +000093 return inheritsFrom(child, IC_VEX_W_OPSIZE) ||
94 (VEX_LIG && inheritsFrom(child, IC_VEX_L_OPSIZE));
Sean Callanana21e2ea2011-03-15 01:23:15 +000095 case IC_VEX_W:
Sean Callanana21e2ea2011-03-15 01:23:15 +000096 case IC_VEX_W_XS:
Sean Callanana21e2ea2011-03-15 01:23:15 +000097 case IC_VEX_W_XD:
Craig Topper5ffedb92011-09-02 04:17:54 +000098 case IC_VEX_W_OPSIZE:
99 return false;
100 case IC_VEX_L:
Craig Topper5ffedb92011-09-02 04:17:54 +0000101 case IC_VEX_L_XS:
Craig Topper5ffedb92011-09-02 04:17:54 +0000102 case IC_VEX_L_XD:
Craig Topperc8eb8802011-11-06 23:04:08 +0000103 return false;
Craig Topper5ffedb92011-09-02 04:17:54 +0000104 case IC_VEX_L_OPSIZE:
Craig Topperc8eb8802011-11-06 23:04:08 +0000105 return inheritsFrom(child, IC_VEX_L_W_OPSIZE);
106 case IC_VEX_L_W_OPSIZE:
Craig Topper5ffedb92011-09-02 04:17:54 +0000107 return false;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000108 default:
Craig Topper5ffedb92011-09-02 04:17:54 +0000109 llvm_unreachable("Unknown instruction class");
Sean Callanan8ed9f512009-12-19 02:59:52 +0000110 }
111}
112
113/// outranks - Indicates whether, if an instruction has two different applicable
114/// classes, which class should be preferred when performing decode. This
115/// imposes a total ordering (ties are resolved toward "lower")
116///
117/// @param upper - The class that may be preferable
118/// @param lower - The class that may be less preferable
119/// @return - True if upper is to be preferred, false otherwise.
Craig Topper98400092012-07-31 05:27:01 +0000120static inline bool outranks(InstructionContext upper,
Sean Callanan8ed9f512009-12-19 02:59:52 +0000121 InstructionContext lower) {
122 assert(upper < IC_max);
123 assert(lower < IC_max);
Craig Topper98400092012-07-31 05:27:01 +0000124
Sean Callanan8ed9f512009-12-19 02:59:52 +0000125#define ENUM_ENTRY(n, r, d) r,
126 static int ranks[IC_max] = {
127 INSTRUCTION_CONTEXTS
128 };
129#undef ENUM_ENTRY
Craig Topper98400092012-07-31 05:27:01 +0000130
Sean Callanan8ed9f512009-12-19 02:59:52 +0000131 return (ranks[upper] > ranks[lower]);
132}
133
134/// stringForContext - Returns a string containing the name of a particular
135/// InstructionContext, usually for diagnostic purposes.
136///
137/// @param insnContext - The instruction class to transform to a string.
138/// @return - A statically-allocated string constant that contains the
139/// name of the instruction class.
140static inline const char* stringForContext(InstructionContext insnContext) {
141 switch (insnContext) {
142 default:
143 llvm_unreachable("Unhandled instruction class");
144#define ENUM_ENTRY(n, r, d) case n: return #n; break;
145 INSTRUCTION_CONTEXTS
146#undef ENUM_ENTRY
147 }
148}
149
150/// stringForOperandType - Like stringForContext, but for OperandTypes.
151static inline const char* stringForOperandType(OperandType type) {
152 switch (type) {
153 default:
154 llvm_unreachable("Unhandled type");
155#define ENUM_ENTRY(i, d) case i: return #i;
156 TYPES
157#undef ENUM_ENTRY
158 }
159}
160
161/// stringForOperandEncoding - like stringForContext, but for
162/// OperandEncodings.
163static inline const char* stringForOperandEncoding(OperandEncoding encoding) {
164 switch (encoding) {
165 default:
166 llvm_unreachable("Unhandled encoding");
167#define ENUM_ENTRY(i, d) case i: return #i;
168 ENCODINGS
169#undef ENUM_ENTRY
170 }
171}
172
Craig Topperde9e3332012-07-31 06:02:05 +0000173void DisassemblerTables::emitOneID(raw_ostream &o, unsigned &i, InstrUID id,
Sean Callanan8ed9f512009-12-19 02:59:52 +0000174 bool addComma) const {
175 if (id)
176 o.indent(i * 2) << format("0x%hx", id);
177 else
178 o.indent(i * 2) << 0;
Craig Topper98400092012-07-31 05:27:01 +0000179
Sean Callanan8ed9f512009-12-19 02:59:52 +0000180 if (addComma)
181 o << ", ";
182 else
183 o << " ";
Craig Topper98400092012-07-31 05:27:01 +0000184
Sean Callanan8ed9f512009-12-19 02:59:52 +0000185 o << "/* ";
186 o << InstructionSpecifiers[id].name;
187 o << "*/";
Craig Topper98400092012-07-31 05:27:01 +0000188
Sean Callanan8ed9f512009-12-19 02:59:52 +0000189 o << "\n";
190}
191
192/// emitEmptyTable - Emits the modRMEmptyTable, which is used as a ID table by
193/// all ModR/M decisions for instructions that are invalid for all possible
194/// ModR/M byte values.
195///
196/// @param o - The output stream on which to emit the table.
197/// @param i - The indentation level for that output stream.
Craig Topperde9e3332012-07-31 06:02:05 +0000198static void emitEmptyTable(raw_ostream &o, unsigned &i) {
Craig Topperce8f4c52012-02-09 07:45:30 +0000199 o.indent(i * 2) << "0x0, /* EmptyTable */\n";
Sean Callanan8ed9f512009-12-19 02:59:52 +0000200}
201
202/// getDecisionType - Determines whether a ModRM decision with 255 entries can
203/// be compacted by eliminating redundant information.
204///
205/// @param decision - The decision to be compacted.
206/// @return - The compactest available representation for the decision.
Craig Toppere08789f2012-07-31 05:42:02 +0000207static ModRMDecisionType getDecisionType(ModRMDecision &decision) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000208 bool satisfiesOneEntry = true;
209 bool satisfiesSplitRM = true;
Craig Topperf41ab772012-02-09 08:58:07 +0000210 bool satisfiesSplitReg = true;
Craig Topper76b29b52012-09-13 05:45:42 +0000211 bool satisfiesSplitMisc = true;
Craig Topperf41ab772012-02-09 08:58:07 +0000212
Craig Topperde9e3332012-07-31 06:02:05 +0000213 for (unsigned index = 0; index < 256; ++index) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000214 if (decision.instructionIDs[index] != decision.instructionIDs[0])
215 satisfiesOneEntry = false;
Craig Topperf41ab772012-02-09 08:58:07 +0000216
Sean Callanan8ed9f512009-12-19 02:59:52 +0000217 if (((index & 0xc0) == 0xc0) &&
218 (decision.instructionIDs[index] != decision.instructionIDs[0xc0]))
219 satisfiesSplitRM = false;
Craig Topperf41ab772012-02-09 08:58:07 +0000220
Sean Callanan8ed9f512009-12-19 02:59:52 +0000221 if (((index & 0xc0) != 0xc0) &&
222 (decision.instructionIDs[index] != decision.instructionIDs[0x00]))
223 satisfiesSplitRM = false;
Craig Topperf41ab772012-02-09 08:58:07 +0000224
225 if (((index & 0xc0) == 0xc0) &&
226 (decision.instructionIDs[index] != decision.instructionIDs[index&0xf8]))
227 satisfiesSplitReg = false;
228
229 if (((index & 0xc0) != 0xc0) &&
230 (decision.instructionIDs[index] != decision.instructionIDs[index&0x38]))
Craig Topper76b29b52012-09-13 05:45:42 +0000231 satisfiesSplitMisc = false;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000232 }
Craig Topperf41ab772012-02-09 08:58:07 +0000233
Sean Callanan8ed9f512009-12-19 02:59:52 +0000234 if (satisfiesOneEntry)
235 return MODRM_ONEENTRY;
Craig Topperf41ab772012-02-09 08:58:07 +0000236
Sean Callanan8ed9f512009-12-19 02:59:52 +0000237 if (satisfiesSplitRM)
238 return MODRM_SPLITRM;
Craig Topperf41ab772012-02-09 08:58:07 +0000239
Craig Topper76b29b52012-09-13 05:45:42 +0000240 if (satisfiesSplitReg && satisfiesSplitMisc)
Craig Topperf41ab772012-02-09 08:58:07 +0000241 return MODRM_SPLITREG;
242
Craig Topper76b29b52012-09-13 05:45:42 +0000243 if (satisfiesSplitMisc)
244 return MODRM_SPLITMISC;
245
Sean Callanan8ed9f512009-12-19 02:59:52 +0000246 return MODRM_FULL;
247}
248
249/// stringForDecisionType - Returns a statically-allocated string corresponding
250/// to a particular decision type.
251///
252/// @param dt - The decision type.
Craig Topper98400092012-07-31 05:27:01 +0000253/// @return - A pointer to the statically-allocated string (e.g.,
Sean Callanan8ed9f512009-12-19 02:59:52 +0000254/// "MODRM_ONEENTRY" for MODRM_ONEENTRY).
Craig Toppere08789f2012-07-31 05:42:02 +0000255static const char* stringForDecisionType(ModRMDecisionType dt) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000256#define ENUM_ENTRY(n) case n: return #n;
257 switch (dt) {
258 default:
Craig Topper98400092012-07-31 05:27:01 +0000259 llvm_unreachable("Unknown decision type");
Sean Callanan8ed9f512009-12-19 02:59:52 +0000260 MODRMTYPES
Craig Topper98400092012-07-31 05:27:01 +0000261 };
Sean Callanan8ed9f512009-12-19 02:59:52 +0000262#undef ENUM_ENTRY
263}
Craig Topper98400092012-07-31 05:27:01 +0000264
Sean Callanan8ed9f512009-12-19 02:59:52 +0000265/// stringForModifierType - Returns a statically-allocated string corresponding
266/// to an opcode modifier type.
267///
268/// @param mt - The modifier type.
269/// @return - A pointer to the statically-allocated string (e.g.,
270/// "MODIFIER_NONE" for MODIFIER_NONE).
Craig Toppere08789f2012-07-31 05:42:02 +0000271static const char* stringForModifierType(ModifierType mt) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000272#define ENUM_ENTRY(n) case n: return #n;
273 switch(mt) {
274 default:
275 llvm_unreachable("Unknown modifier type");
276 MODIFIER_TYPES
277 };
278#undef ENUM_ENTRY
279}
Craig Topper98400092012-07-31 05:27:01 +0000280
Sean Callanan8ed9f512009-12-19 02:59:52 +0000281DisassemblerTables::DisassemblerTables() {
282 unsigned i;
Craig Topper98400092012-07-31 05:27:01 +0000283
Joerg Sonnenberger39d7cae2011-04-04 16:25:38 +0000284 for (i = 0; i < array_lengthof(Tables); i++) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000285 Tables[i] = new ContextDecision;
Daniel Dunbar87830872009-12-19 04:16:57 +0000286 memset(Tables[i], 0, sizeof(ContextDecision));
Sean Callanan8ed9f512009-12-19 02:59:52 +0000287 }
Craig Topper98400092012-07-31 05:27:01 +0000288
Sean Callanan8ed9f512009-12-19 02:59:52 +0000289 HasConflicts = false;
290}
Craig Topper98400092012-07-31 05:27:01 +0000291
Sean Callanan8ed9f512009-12-19 02:59:52 +0000292DisassemblerTables::~DisassemblerTables() {
293 unsigned i;
Craig Topper98400092012-07-31 05:27:01 +0000294
Joerg Sonnenberger39d7cae2011-04-04 16:25:38 +0000295 for (i = 0; i < array_lengthof(Tables); i++)
Sean Callanan8ed9f512009-12-19 02:59:52 +0000296 delete Tables[i];
297}
Craig Topper98400092012-07-31 05:27:01 +0000298
Craig Toppere08789f2012-07-31 05:42:02 +0000299void DisassemblerTables::emitModRMDecision(raw_ostream &o1, raw_ostream &o2,
Craig Topperde9e3332012-07-31 06:02:05 +0000300 unsigned &i1, unsigned &i2,
Craig Toppere08789f2012-07-31 05:42:02 +0000301 ModRMDecision &decision) const {
Craig Topperde9e3332012-07-31 06:02:05 +0000302 static uint32_t sTableNumber = 0;
303 static uint32_t sEntryNumber = 1;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000304 ModRMDecisionType dt = getDecisionType(decision);
Craig Topperce8f4c52012-02-09 07:45:30 +0000305
Sean Callanan8ed9f512009-12-19 02:59:52 +0000306 if (dt == MODRM_ONEENTRY && decision.instructionIDs[0] == 0)
307 {
308 o2.indent(i2) << "{ /* ModRMDecision */" << "\n";
309 i2++;
Craig Topperce8f4c52012-02-09 07:45:30 +0000310
Sean Callanan8ed9f512009-12-19 02:59:52 +0000311 o2.indent(i2) << stringForDecisionType(dt) << "," << "\n";
Craig Topperce8f4c52012-02-09 07:45:30 +0000312 o2.indent(i2) << 0 << " /* EmptyTable */\n";
313
Sean Callanan8ed9f512009-12-19 02:59:52 +0000314 i2--;
315 o2.indent(i2) << "}";
316 return;
317 }
Sean Callanan8ed9f512009-12-19 02:59:52 +0000318
Craig Topperce8f4c52012-02-09 07:45:30 +0000319 o1 << "/* Table" << sTableNumber << " */\n";
Sean Callanan8ed9f512009-12-19 02:59:52 +0000320 i1++;
Craig Topperce8f4c52012-02-09 07:45:30 +0000321
Sean Callanan8ed9f512009-12-19 02:59:52 +0000322 switch (dt) {
323 default:
324 llvm_unreachable("Unknown decision type");
325 case MODRM_ONEENTRY:
Craig Topperce8f4c52012-02-09 07:45:30 +0000326 emitOneID(o1, i1, decision.instructionIDs[0], true);
Sean Callanan8ed9f512009-12-19 02:59:52 +0000327 break;
328 case MODRM_SPLITRM:
329 emitOneID(o1, i1, decision.instructionIDs[0x00], true); // mod = 0b00
Craig Topperce8f4c52012-02-09 07:45:30 +0000330 emitOneID(o1, i1, decision.instructionIDs[0xc0], true); // mod = 0b11
Sean Callanan8ed9f512009-12-19 02:59:52 +0000331 break;
Craig Topperf41ab772012-02-09 08:58:07 +0000332 case MODRM_SPLITREG:
Craig Topperde9e3332012-07-31 06:02:05 +0000333 for (unsigned index = 0; index < 64; index += 8)
Craig Topperf41ab772012-02-09 08:58:07 +0000334 emitOneID(o1, i1, decision.instructionIDs[index], true);
Craig Topperde9e3332012-07-31 06:02:05 +0000335 for (unsigned index = 0xc0; index < 256; index += 8)
Craig Topperf41ab772012-02-09 08:58:07 +0000336 emitOneID(o1, i1, decision.instructionIDs[index], true);
337 break;
Craig Topper76b29b52012-09-13 05:45:42 +0000338 case MODRM_SPLITMISC:
339 for (unsigned index = 0; index < 64; index += 8)
340 emitOneID(o1, i1, decision.instructionIDs[index], true);
341 for (unsigned index = 0xc0; index < 256; ++index)
342 emitOneID(o1, i1, decision.instructionIDs[index], true);
343 break;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000344 case MODRM_FULL:
Craig Topperde9e3332012-07-31 06:02:05 +0000345 for (unsigned index = 0; index < 256; ++index)
Craig Topperce8f4c52012-02-09 07:45:30 +0000346 emitOneID(o1, i1, decision.instructionIDs[index], true);
Sean Callanan8ed9f512009-12-19 02:59:52 +0000347 break;
348 }
Craig Topperce8f4c52012-02-09 07:45:30 +0000349
Sean Callanan8ed9f512009-12-19 02:59:52 +0000350 i1--;
Craig Topperce8f4c52012-02-09 07:45:30 +0000351
Sean Callanan8ed9f512009-12-19 02:59:52 +0000352 o2.indent(i2) << "{ /* struct ModRMDecision */" << "\n";
353 i2++;
Craig Topperce8f4c52012-02-09 07:45:30 +0000354
Sean Callanan8ed9f512009-12-19 02:59:52 +0000355 o2.indent(i2) << stringForDecisionType(dt) << "," << "\n";
Craig Topperce8f4c52012-02-09 07:45:30 +0000356 o2.indent(i2) << sEntryNumber << " /* Table" << sTableNumber << " */\n";
357
Sean Callanan8ed9f512009-12-19 02:59:52 +0000358 i2--;
359 o2.indent(i2) << "}";
Craig Topperce8f4c52012-02-09 07:45:30 +0000360
361 switch (dt) {
362 default:
363 llvm_unreachable("Unknown decision type");
364 case MODRM_ONEENTRY:
365 sEntryNumber += 1;
366 break;
367 case MODRM_SPLITRM:
368 sEntryNumber += 2;
369 break;
Craig Topperf41ab772012-02-09 08:58:07 +0000370 case MODRM_SPLITREG:
371 sEntryNumber += 16;
372 break;
Craig Topper76b29b52012-09-13 05:45:42 +0000373 case MODRM_SPLITMISC:
374 sEntryNumber += 8 + 64;
375 break;
Craig Topperce8f4c52012-02-09 07:45:30 +0000376 case MODRM_FULL:
377 sEntryNumber += 256;
378 break;
379 }
380
Craig Topper9e6dc8b2012-09-11 04:19:21 +0000381 // We assume that the index can fit into uint16_t.
382 assert(sEntryNumber < 65536U &&
383 "Index into ModRMDecision is too large for uint16_t!");
384
Sean Callanan8ed9f512009-12-19 02:59:52 +0000385 ++sTableNumber;
386}
387
Craig Toppere08789f2012-07-31 05:42:02 +0000388void DisassemblerTables::emitOpcodeDecision(raw_ostream &o1, raw_ostream &o2,
Craig Topperde9e3332012-07-31 06:02:05 +0000389 unsigned &i1, unsigned &i2,
Craig Toppere08789f2012-07-31 05:42:02 +0000390 OpcodeDecision &decision) const {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000391 o2.indent(i2) << "{ /* struct OpcodeDecision */" << "\n";
392 i2++;
393 o2.indent(i2) << "{" << "\n";
394 i2++;
395
Craig Topperde9e3332012-07-31 06:02:05 +0000396 for (unsigned index = 0; index < 256; ++index) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000397 o2.indent(i2);
398
399 o2 << "/* 0x" << format("%02hhx", index) << " */" << "\n";
400
401 emitModRMDecision(o1, o2, i1, i2, decision.modRMDecisions[index]);
402
403 if (index < 255)
404 o2 << ",";
405
406 o2 << "\n";
407 }
408
409 i2--;
410 o2.indent(i2) << "}" << "\n";
411 i2--;
412 o2.indent(i2) << "}" << "\n";
413}
414
Craig Toppere08789f2012-07-31 05:42:02 +0000415void DisassemblerTables::emitContextDecision(raw_ostream &o1, raw_ostream &o2,
Craig Topperde9e3332012-07-31 06:02:05 +0000416 unsigned &i1, unsigned &i2,
Craig Toppere08789f2012-07-31 05:42:02 +0000417 ContextDecision &decision,
418 const char* name) const {
Benjamin Kramer4d1dca92010-10-23 09:10:44 +0000419 o2.indent(i2) << "static const struct ContextDecision " << name << " = {\n";
Sean Callanan8ed9f512009-12-19 02:59:52 +0000420 i2++;
421 o2.indent(i2) << "{ /* opcodeDecisions */" << "\n";
422 i2++;
423
Craig Topperde9e3332012-07-31 06:02:05 +0000424 for (unsigned index = 0; index < IC_max; ++index) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000425 o2.indent(i2) << "/* ";
426 o2 << stringForContext((InstructionContext)index);
427 o2 << " */";
428 o2 << "\n";
429
430 emitOpcodeDecision(o1, o2, i1, i2, decision.opcodeDecisions[index]);
431
432 if (index + 1 < IC_max)
433 o2 << ", ";
434 }
435
436 i2--;
437 o2.indent(i2) << "}" << "\n";
438 i2--;
439 o2.indent(i2) << "};" << "\n";
440}
441
Craig Topperde9e3332012-07-31 06:02:05 +0000442void DisassemblerTables::emitInstructionInfo(raw_ostream &o,
443 unsigned &i) const {
Craig Topper5a2c6072012-08-01 07:39:18 +0000444 unsigned NumInstructions = InstructionSpecifiers.size();
445
446 o << "static const struct OperandSpecifier x86OperandSets[]["
447 << X86_MAX_OPERANDS << "] = {\n";
448
449 typedef std::vector<std::pair<const char *, const char *> > OperandListTy;
450 std::map<OperandListTy, unsigned> OperandSets;
451
452 unsigned OperandSetNum = 0;
453 for (unsigned Index = 0; Index < NumInstructions; ++Index) {
454 OperandListTy OperandList;
455
456 for (unsigned OperandIndex = 0; OperandIndex < X86_MAX_OPERANDS;
457 ++OperandIndex) {
458 const char *Encoding =
459 stringForOperandEncoding((OperandEncoding)InstructionSpecifiers[Index]
460 .operands[OperandIndex].encoding);
461 const char *Type =
462 stringForOperandType((OperandType)InstructionSpecifiers[Index]
463 .operands[OperandIndex].type);
464 OperandList.push_back(std::make_pair(Encoding, Type));
465 }
466 unsigned &N = OperandSets[OperandList];
467 if (N != 0) continue;
468
469 N = ++OperandSetNum;
470
471 o << " { /* " << (OperandSetNum - 1) << " */\n";
472 for (unsigned i = 0, e = OperandList.size(); i != e; ++i) {
473 o << " { " << OperandList[i].first << ", "
474 << OperandList[i].second << " },\n";
475 }
476 o << " },\n";
477 }
478 o << "};" << "\n\n";
479
Benjamin Kramer4d1dca92010-10-23 09:10:44 +0000480 o.indent(i * 2) << "static const struct InstructionSpecifier ";
481 o << INSTRUCTIONS_STR "[" << InstructionSpecifiers.size() << "] = {\n";
Craig Topper98400092012-07-31 05:27:01 +0000482
Sean Callanan8ed9f512009-12-19 02:59:52 +0000483 i++;
484
Craig Topper5a2c6072012-08-01 07:39:18 +0000485 for (unsigned index = 0; index < NumInstructions; ++index) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000486 o.indent(i * 2) << "{ /* " << index << " */" << "\n";
487 i++;
Craig Topper991271d2012-03-04 02:16:41 +0000488
489 o.indent(i * 2) << stringForModifierType(
490 (ModifierType)InstructionSpecifiers[index].modifierType);
Craig Topper5a2c6072012-08-01 07:39:18 +0000491 o << ",\n";
Craig Topper991271d2012-03-04 02:16:41 +0000492
Sean Callanan8ed9f512009-12-19 02:59:52 +0000493 o.indent(i * 2) << "0x";
494 o << format("%02hhx", (uint16_t)InstructionSpecifiers[index].modifierBase);
Craig Topper5a2c6072012-08-01 07:39:18 +0000495 o << ",\n";
Sean Callanan8ed9f512009-12-19 02:59:52 +0000496
Craig Topper5a2c6072012-08-01 07:39:18 +0000497 OperandListTy OperandList;
498 for (unsigned OperandIndex = 0; OperandIndex < X86_MAX_OPERANDS;
499 ++OperandIndex) {
500 const char *Encoding =
501 stringForOperandEncoding((OperandEncoding)InstructionSpecifiers[index]
502 .operands[OperandIndex].encoding);
503 const char *Type =
504 stringForOperandType((OperandType)InstructionSpecifiers[index]
505 .operands[OperandIndex].type);
506 OperandList.push_back(std::make_pair(Encoding, Type));
Sean Callanan8ed9f512009-12-19 02:59:52 +0000507 }
Craig Topper5a2c6072012-08-01 07:39:18 +0000508 o.indent(i * 2) << (OperandSets[OperandList] - 1) << ",\n";
Craig Topper98400092012-07-31 05:27:01 +0000509
Benjamin Kramer953362c2012-02-11 14:50:54 +0000510 o.indent(i * 2) << "/* " << InstructionSpecifiers[index].name << " */";
Sean Callanan8ed9f512009-12-19 02:59:52 +0000511 o << "\n";
512
513 i--;
514 o.indent(i * 2) << "}";
515
Craig Topper5a2c6072012-08-01 07:39:18 +0000516 if (index + 1 < NumInstructions)
Sean Callanan8ed9f512009-12-19 02:59:52 +0000517 o << ",";
518
519 o << "\n";
520 }
521
522 i--;
523 o.indent(i * 2) << "};" << "\n";
524}
525
Craig Topperde9e3332012-07-31 06:02:05 +0000526void DisassemblerTables::emitContextTable(raw_ostream &o, unsigned &i) const {
Craig Topper19dbe2f2012-07-31 06:15:39 +0000527 o.indent(i * 2) << "static const uint8_t " CONTEXTS_STR
Benjamin Kramer86c69c52010-10-23 09:28:42 +0000528 "[256] = {\n";
Sean Callanan8ed9f512009-12-19 02:59:52 +0000529 i++;
530
Craig Topperde9e3332012-07-31 06:02:05 +0000531 for (unsigned index = 0; index < 256; ++index) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000532 o.indent(i * 2);
533
Craig Topperc8eb8802011-11-06 23:04:08 +0000534 if ((index & ATTR_VEXL) && (index & ATTR_REXW) && (index & ATTR_OPSIZE))
535 o << "IC_VEX_L_W_OPSIZE";
536 else if ((index & ATTR_VEXL) && (index & ATTR_OPSIZE))
Sean Callanana21e2ea2011-03-15 01:23:15 +0000537 o << "IC_VEX_L_OPSIZE";
538 else if ((index & ATTR_VEXL) && (index & ATTR_XD))
539 o << "IC_VEX_L_XD";
540 else if ((index & ATTR_VEXL) && (index & ATTR_XS))
541 o << "IC_VEX_L_XS";
542 else if ((index & ATTR_VEX) && (index & ATTR_REXW) && (index & ATTR_OPSIZE))
543 o << "IC_VEX_W_OPSIZE";
544 else if ((index & ATTR_VEX) && (index & ATTR_REXW) && (index & ATTR_XD))
545 o << "IC_VEX_W_XD";
546 else if ((index & ATTR_VEX) && (index & ATTR_REXW) && (index & ATTR_XS))
547 o << "IC_VEX_W_XS";
548 else if (index & ATTR_VEXL)
549 o << "IC_VEX_L";
550 else if ((index & ATTR_VEX) && (index & ATTR_REXW))
551 o << "IC_VEX_W";
552 else if ((index & ATTR_VEX) && (index & ATTR_OPSIZE))
553 o << "IC_VEX_OPSIZE";
554 else if ((index & ATTR_VEX) && (index & ATTR_XD))
555 o << "IC_VEX_XD";
556 else if ((index & ATTR_VEX) && (index & ATTR_XS))
557 o << "IC_VEX_XS";
Craig Topper113061d2011-08-25 07:42:00 +0000558 else if (index & ATTR_VEX)
559 o << "IC_VEX";
Sean Callanana21e2ea2011-03-15 01:23:15 +0000560 else if ((index & ATTR_64BIT) && (index & ATTR_REXW) && (index & ATTR_XS))
Sean Callanan8ed9f512009-12-19 02:59:52 +0000561 o << "IC_64BIT_REXW_XS";
562 else if ((index & ATTR_64BIT) && (index & ATTR_REXW) && (index & ATTR_XD))
563 o << "IC_64BIT_REXW_XD";
Craig Topper98400092012-07-31 05:27:01 +0000564 else if ((index & ATTR_64BIT) && (index & ATTR_REXW) &&
Sean Callanan8ed9f512009-12-19 02:59:52 +0000565 (index & ATTR_OPSIZE))
566 o << "IC_64BIT_REXW_OPSIZE";
Craig Toppere1b4a1a2011-10-01 19:54:56 +0000567 else if ((index & ATTR_64BIT) && (index & ATTR_XD) && (index & ATTR_OPSIZE))
568 o << "IC_64BIT_XD_OPSIZE";
Craig Topper29480fd2011-10-11 04:34:23 +0000569 else if ((index & ATTR_64BIT) && (index & ATTR_XS) && (index & ATTR_OPSIZE))
570 o << "IC_64BIT_XS_OPSIZE";
Sean Callanan8ed9f512009-12-19 02:59:52 +0000571 else if ((index & ATTR_64BIT) && (index & ATTR_XS))
572 o << "IC_64BIT_XS";
573 else if ((index & ATTR_64BIT) && (index & ATTR_XD))
574 o << "IC_64BIT_XD";
575 else if ((index & ATTR_64BIT) && (index & ATTR_OPSIZE))
576 o << "IC_64BIT_OPSIZE";
Craig Topper930a1eb2012-02-27 01:54:29 +0000577 else if ((index & ATTR_64BIT) && (index & ATTR_ADSIZE))
578 o << "IC_64BIT_ADSIZE";
Sean Callanan8ed9f512009-12-19 02:59:52 +0000579 else if ((index & ATTR_64BIT) && (index & ATTR_REXW))
580 o << "IC_64BIT_REXW";
581 else if ((index & ATTR_64BIT))
582 o << "IC_64BIT";
Craig Topper29480fd2011-10-11 04:34:23 +0000583 else if ((index & ATTR_XS) && (index & ATTR_OPSIZE))
584 o << "IC_XS_OPSIZE";
Craig Toppere1b4a1a2011-10-01 19:54:56 +0000585 else if ((index & ATTR_XD) && (index & ATTR_OPSIZE))
586 o << "IC_XD_OPSIZE";
Sean Callanan8ed9f512009-12-19 02:59:52 +0000587 else if (index & ATTR_XS)
588 o << "IC_XS";
589 else if (index & ATTR_XD)
590 o << "IC_XD";
591 else if (index & ATTR_OPSIZE)
592 o << "IC_OPSIZE";
Craig Topper930a1eb2012-02-27 01:54:29 +0000593 else if (index & ATTR_ADSIZE)
594 o << "IC_ADSIZE";
Sean Callanan8ed9f512009-12-19 02:59:52 +0000595 else
596 o << "IC";
597
598 if (index < 255)
599 o << ",";
600 else
601 o << " ";
602
603 o << " /* " << index << " */";
604
605 o << "\n";
606 }
607
608 i--;
609 o.indent(i * 2) << "};" << "\n";
610}
611
Craig Toppere08789f2012-07-31 05:42:02 +0000612void DisassemblerTables::emitContextDecisions(raw_ostream &o1, raw_ostream &o2,
Craig Topperde9e3332012-07-31 06:02:05 +0000613 unsigned &i1, unsigned &i2) const {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000614 emitContextDecision(o1, o2, i1, i2, *Tables[0], ONEBYTE_STR);
615 emitContextDecision(o1, o2, i1, i2, *Tables[1], TWOBYTE_STR);
616 emitContextDecision(o1, o2, i1, i2, *Tables[2], THREEBYTE38_STR);
617 emitContextDecision(o1, o2, i1, i2, *Tables[3], THREEBYTE3A_STR);
Joerg Sonnenberger4a8ac8d2011-04-04 16:58:13 +0000618 emitContextDecision(o1, o2, i1, i2, *Tables[4], THREEBYTEA6_STR);
619 emitContextDecision(o1, o2, i1, i2, *Tables[5], THREEBYTEA7_STR);
Sean Callanan8ed9f512009-12-19 02:59:52 +0000620}
621
622void DisassemblerTables::emit(raw_ostream &o) const {
Craig Topperde9e3332012-07-31 06:02:05 +0000623 unsigned i1 = 0;
624 unsigned i2 = 0;
Craig Topper98400092012-07-31 05:27:01 +0000625
Sean Callanan8ed9f512009-12-19 02:59:52 +0000626 std::string s1;
627 std::string s2;
Craig Topper98400092012-07-31 05:27:01 +0000628
Sean Callanan8ed9f512009-12-19 02:59:52 +0000629 raw_string_ostream o1(s1);
630 raw_string_ostream o2(s2);
Craig Topper98400092012-07-31 05:27:01 +0000631
Sean Callanan8ed9f512009-12-19 02:59:52 +0000632 emitInstructionInfo(o, i2);
633 o << "\n";
634
635 emitContextTable(o, i2);
636 o << "\n";
Craig Topperce8f4c52012-02-09 07:45:30 +0000637
638 o << "static const InstrUID modRMTable[] = {\n";
639 i1++;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000640 emitEmptyTable(o1, i1);
Craig Topperce8f4c52012-02-09 07:45:30 +0000641 i1--;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000642 emitContextDecisions(o1, o2, i1, i2);
Craig Topperce8f4c52012-02-09 07:45:30 +0000643
Sean Callanan8ed9f512009-12-19 02:59:52 +0000644 o << o1.str();
Craig Topperce8f4c52012-02-09 07:45:30 +0000645 o << " 0x0\n";
646 o << "};\n";
Sean Callanan8ed9f512009-12-19 02:59:52 +0000647 o << "\n";
648 o << o2.str();
649 o << "\n";
650 o << "\n";
651}
652
653void DisassemblerTables::setTableFields(ModRMDecision &decision,
654 const ModRMFilter &filter,
655 InstrUID uid,
656 uint8_t opcode) {
Craig Topperde9e3332012-07-31 06:02:05 +0000657 for (unsigned index = 0; index < 256; ++index) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000658 if (filter.accepts(index)) {
659 if (decision.instructionIDs[index] == uid)
660 continue;
661
662 if (decision.instructionIDs[index] != 0) {
663 InstructionSpecifier &newInfo =
664 InstructionSpecifiers[uid];
665 InstructionSpecifier &previousInfo =
666 InstructionSpecifiers[decision.instructionIDs[index]];
Craig Topper98400092012-07-31 05:27:01 +0000667
Sean Callanan8ed9f512009-12-19 02:59:52 +0000668 if(newInfo.filtered)
669 continue; // filtered instructions get lowest priority
Craig Topper98400092012-07-31 05:27:01 +0000670
Craig Topper842f58f2011-09-11 20:23:20 +0000671 if(previousInfo.name == "NOOP" && (newInfo.name == "XCHG16ar" ||
672 newInfo.name == "XCHG32ar" ||
Craig Topper25f6dfd2011-10-07 05:35:38 +0000673 newInfo.name == "XCHG32ar64" ||
Craig Topper842f58f2011-09-11 20:23:20 +0000674 newInfo.name == "XCHG64ar"))
675 continue; // special case for XCHG*ar and NOOP
Sean Callanan8ed9f512009-12-19 02:59:52 +0000676
677 if (outranks(previousInfo.insnContext, newInfo.insnContext))
678 continue;
Craig Topper98400092012-07-31 05:27:01 +0000679
Sean Callanan8ed9f512009-12-19 02:59:52 +0000680 if (previousInfo.insnContext == newInfo.insnContext &&
681 !previousInfo.filtered) {
682 errs() << "Error: Primary decode conflict: ";
683 errs() << newInfo.name << " would overwrite " << previousInfo.name;
684 errs() << "\n";
685 errs() << "ModRM " << index << "\n";
686 errs() << "Opcode " << (uint16_t)opcode << "\n";
687 errs() << "Context " << stringForContext(newInfo.insnContext) << "\n";
688 HasConflicts = true;
689 }
690 }
691
692 decision.instructionIDs[index] = uid;
693 }
694 }
695}
696
697void DisassemblerTables::setTableFields(OpcodeType type,
698 InstructionContext insnContext,
699 uint8_t opcode,
700 const ModRMFilter &filter,
Craig Topper4da632e2011-09-23 06:57:25 +0000701 InstrUID uid,
Craig Topper6744a172011-10-04 06:30:42 +0000702 bool is32bit,
703 bool ignoresVEX_L) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000704 ContextDecision &decision = *Tables[type];
705
Craig Topperde9e3332012-07-31 06:02:05 +0000706 for (unsigned index = 0; index < IC_max; ++index) {
Craig Topper4da632e2011-09-23 06:57:25 +0000707 if (is32bit && inheritsFrom((InstructionContext)index, IC_64BIT))
708 continue;
709
Craig Topper98400092012-07-31 05:27:01 +0000710 if (inheritsFrom((InstructionContext)index,
Craig Topper6744a172011-10-04 06:30:42 +0000711 InstructionSpecifiers[uid].insnContext, ignoresVEX_L))
Craig Topper98400092012-07-31 05:27:01 +0000712 setTableFields(decision.opcodeDecisions[index].modRMDecisions[opcode],
Sean Callanan8ed9f512009-12-19 02:59:52 +0000713 filter,
714 uid,
715 opcode);
716 }
717}