blob: 468a1f81c7198a5e6e3ef5598729abe4bb17d31c [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
17#include "X86DisassemblerShared.h"
18#include "X86DisassemblerTables.h"
19
Peter Collingbourne7c788882011-10-01 16:41:13 +000020#include "llvm/TableGen/TableGenBackend.h"
Joerg Sonnenberger39d7cae2011-04-04 16:25:38 +000021#include "llvm/ADT/STLExtras.h"
Sean Callanan8ed9f512009-12-19 02:59:52 +000022#include "llvm/Support/ErrorHandling.h"
23#include "llvm/Support/Format.h"
Craig Topper5a2c6072012-08-01 07:39:18 +000024#include <map>
Sean Callanan8ed9f512009-12-19 02:59:52 +000025
Sean Callanan8ed9f512009-12-19 02:59:52 +000026using namespace llvm;
27using namespace X86Disassembler;
Craig Topper98400092012-07-31 05:27:01 +000028
Sean Callanan8ed9f512009-12-19 02:59:52 +000029/// inheritsFrom - Indicates whether all instructions in one class also belong
30/// to another class.
31///
32/// @param child - The class that may be the subset
33/// @param parent - The class that may be the superset
34/// @return - True if child is a subset of parent, false otherwise.
35static inline bool inheritsFrom(InstructionContext child,
Craig Topper6744a172011-10-04 06:30:42 +000036 InstructionContext parent,
37 bool VEX_LIG = false) {
Sean Callanan8ed9f512009-12-19 02:59:52 +000038 if (child == parent)
39 return true;
Craig Topper98400092012-07-31 05:27:01 +000040
Sean Callanan8ed9f512009-12-19 02:59:52 +000041 switch (parent) {
42 case IC:
Craig Topper5ffedb92011-09-02 04:17:54 +000043 return(inheritsFrom(child, IC_64BIT) ||
44 inheritsFrom(child, IC_OPSIZE) ||
Craig Topper930a1eb2012-02-27 01:54:29 +000045 inheritsFrom(child, IC_ADSIZE) ||
Craig Topper5ffedb92011-09-02 04:17:54 +000046 inheritsFrom(child, IC_XD) ||
47 inheritsFrom(child, IC_XS));
Sean Callanan8ed9f512009-12-19 02:59:52 +000048 case IC_64BIT:
49 return(inheritsFrom(child, IC_64BIT_REXW) ||
50 inheritsFrom(child, IC_64BIT_OPSIZE) ||
Craig Topper930a1eb2012-02-27 01:54:29 +000051 inheritsFrom(child, IC_64BIT_ADSIZE) ||
Sean Callanan8ed9f512009-12-19 02:59:52 +000052 inheritsFrom(child, IC_64BIT_XD) ||
53 inheritsFrom(child, IC_64BIT_XS));
54 case IC_OPSIZE:
Craig Topper5ffedb92011-09-02 04:17:54 +000055 return inheritsFrom(child, IC_64BIT_OPSIZE);
Craig Topper930a1eb2012-02-27 01:54:29 +000056 case IC_ADSIZE:
57 case IC_64BIT_ADSIZE:
58 return false;
Sean Callanan8ed9f512009-12-19 02:59:52 +000059 case IC_XD:
Craig Topper5ffedb92011-09-02 04:17:54 +000060 return inheritsFrom(child, IC_64BIT_XD);
Sean Callanan8ed9f512009-12-19 02:59:52 +000061 case IC_XS:
Craig Topper5ffedb92011-09-02 04:17:54 +000062 return inheritsFrom(child, IC_64BIT_XS);
Craig Toppere1b4a1a2011-10-01 19:54:56 +000063 case IC_XD_OPSIZE:
64 return inheritsFrom(child, IC_64BIT_XD_OPSIZE);
Craig Topper29480fd2011-10-11 04:34:23 +000065 case IC_XS_OPSIZE:
66 return inheritsFrom(child, IC_64BIT_XS_OPSIZE);
Sean Callanan8ed9f512009-12-19 02:59:52 +000067 case IC_64BIT_REXW:
68 return(inheritsFrom(child, IC_64BIT_REXW_XS) ||
69 inheritsFrom(child, IC_64BIT_REXW_XD) ||
70 inheritsFrom(child, IC_64BIT_REXW_OPSIZE));
71 case IC_64BIT_OPSIZE:
72 return(inheritsFrom(child, IC_64BIT_REXW_OPSIZE));
73 case IC_64BIT_XD:
74 return(inheritsFrom(child, IC_64BIT_REXW_XD));
75 case IC_64BIT_XS:
76 return(inheritsFrom(child, IC_64BIT_REXW_XS));
Craig Toppere1b4a1a2011-10-01 19:54:56 +000077 case IC_64BIT_XD_OPSIZE:
Craig Topper29480fd2011-10-11 04:34:23 +000078 case IC_64BIT_XS_OPSIZE:
Craig Toppere1b4a1a2011-10-01 19:54:56 +000079 return false;
Sean Callanan8ed9f512009-12-19 02:59:52 +000080 case IC_64BIT_REXW_XD:
Sean Callanan8ed9f512009-12-19 02:59:52 +000081 case IC_64BIT_REXW_XS:
Sean Callanan8ed9f512009-12-19 02:59:52 +000082 case IC_64BIT_REXW_OPSIZE:
83 return false;
Sean Callanana21e2ea2011-03-15 01:23:15 +000084 case IC_VEX:
Craig Topper6744a172011-10-04 06:30:42 +000085 return inheritsFrom(child, IC_VEX_W) ||
86 (VEX_LIG && inheritsFrom(child, IC_VEX_L));
Sean Callanana21e2ea2011-03-15 01:23:15 +000087 case IC_VEX_XS:
Craig Topper6744a172011-10-04 06:30:42 +000088 return inheritsFrom(child, IC_VEX_W_XS) ||
89 (VEX_LIG && inheritsFrom(child, IC_VEX_L_XS));
Sean Callanana21e2ea2011-03-15 01:23:15 +000090 case IC_VEX_XD:
Craig Topper6744a172011-10-04 06:30:42 +000091 return inheritsFrom(child, IC_VEX_W_XD) ||
92 (VEX_LIG && inheritsFrom(child, IC_VEX_L_XD));
Craig Topper5ffedb92011-09-02 04:17:54 +000093 case IC_VEX_OPSIZE:
Craig Topper6744a172011-10-04 06:30:42 +000094 return inheritsFrom(child, IC_VEX_W_OPSIZE) ||
95 (VEX_LIG && inheritsFrom(child, IC_VEX_L_OPSIZE));
Sean Callanana21e2ea2011-03-15 01:23:15 +000096 case IC_VEX_W:
Sean Callanana21e2ea2011-03-15 01:23:15 +000097 case IC_VEX_W_XS:
Sean Callanana21e2ea2011-03-15 01:23:15 +000098 case IC_VEX_W_XD:
Craig Topper5ffedb92011-09-02 04:17:54 +000099 case IC_VEX_W_OPSIZE:
100 return false;
101 case IC_VEX_L:
Craig Topper5ffedb92011-09-02 04:17:54 +0000102 case IC_VEX_L_XS:
Craig Topper5ffedb92011-09-02 04:17:54 +0000103 case IC_VEX_L_XD:
Craig Topperc8eb8802011-11-06 23:04:08 +0000104 return false;
Craig Topper5ffedb92011-09-02 04:17:54 +0000105 case IC_VEX_L_OPSIZE:
Craig Topperc8eb8802011-11-06 23:04:08 +0000106 return inheritsFrom(child, IC_VEX_L_W_OPSIZE);
107 case IC_VEX_L_W_OPSIZE:
Craig Topper5ffedb92011-09-02 04:17:54 +0000108 return false;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000109 default:
Craig Topper5ffedb92011-09-02 04:17:54 +0000110 llvm_unreachable("Unknown instruction class");
Sean Callanan8ed9f512009-12-19 02:59:52 +0000111 }
112}
113
114/// outranks - Indicates whether, if an instruction has two different applicable
115/// classes, which class should be preferred when performing decode. This
116/// imposes a total ordering (ties are resolved toward "lower")
117///
118/// @param upper - The class that may be preferable
119/// @param lower - The class that may be less preferable
120/// @return - True if upper is to be preferred, false otherwise.
Craig Topper98400092012-07-31 05:27:01 +0000121static inline bool outranks(InstructionContext upper,
Sean Callanan8ed9f512009-12-19 02:59:52 +0000122 InstructionContext lower) {
123 assert(upper < IC_max);
124 assert(lower < IC_max);
Craig Topper98400092012-07-31 05:27:01 +0000125
Sean Callanan8ed9f512009-12-19 02:59:52 +0000126#define ENUM_ENTRY(n, r, d) r,
127 static int ranks[IC_max] = {
128 INSTRUCTION_CONTEXTS
129 };
130#undef ENUM_ENTRY
Craig Topper98400092012-07-31 05:27:01 +0000131
Sean Callanan8ed9f512009-12-19 02:59:52 +0000132 return (ranks[upper] > ranks[lower]);
133}
134
135/// stringForContext - Returns a string containing the name of a particular
136/// InstructionContext, usually for diagnostic purposes.
137///
138/// @param insnContext - The instruction class to transform to a string.
139/// @return - A statically-allocated string constant that contains the
140/// name of the instruction class.
141static inline const char* stringForContext(InstructionContext insnContext) {
142 switch (insnContext) {
143 default:
144 llvm_unreachable("Unhandled instruction class");
145#define ENUM_ENTRY(n, r, d) case n: return #n; break;
146 INSTRUCTION_CONTEXTS
147#undef ENUM_ENTRY
148 }
149}
150
151/// stringForOperandType - Like stringForContext, but for OperandTypes.
152static inline const char* stringForOperandType(OperandType type) {
153 switch (type) {
154 default:
155 llvm_unreachable("Unhandled type");
156#define ENUM_ENTRY(i, d) case i: return #i;
157 TYPES
158#undef ENUM_ENTRY
159 }
160}
161
162/// stringForOperandEncoding - like stringForContext, but for
163/// OperandEncodings.
164static inline const char* stringForOperandEncoding(OperandEncoding encoding) {
165 switch (encoding) {
166 default:
167 llvm_unreachable("Unhandled encoding");
168#define ENUM_ENTRY(i, d) case i: return #i;
169 ENCODINGS
170#undef ENUM_ENTRY
171 }
172}
173
Craig Topperde9e3332012-07-31 06:02:05 +0000174void DisassemblerTables::emitOneID(raw_ostream &o, unsigned &i, InstrUID id,
Sean Callanan8ed9f512009-12-19 02:59:52 +0000175 bool addComma) const {
176 if (id)
177 o.indent(i * 2) << format("0x%hx", id);
178 else
179 o.indent(i * 2) << 0;
Craig Topper98400092012-07-31 05:27:01 +0000180
Sean Callanan8ed9f512009-12-19 02:59:52 +0000181 if (addComma)
182 o << ", ";
183 else
184 o << " ";
Craig Topper98400092012-07-31 05:27:01 +0000185
Sean Callanan8ed9f512009-12-19 02:59:52 +0000186 o << "/* ";
187 o << InstructionSpecifiers[id].name;
188 o << "*/";
Craig Topper98400092012-07-31 05:27:01 +0000189
Sean Callanan8ed9f512009-12-19 02:59:52 +0000190 o << "\n";
191}
192
193/// emitEmptyTable - Emits the modRMEmptyTable, which is used as a ID table by
194/// all ModR/M decisions for instructions that are invalid for all possible
195/// ModR/M byte values.
196///
197/// @param o - The output stream on which to emit the table.
198/// @param i - The indentation level for that output stream.
Craig Topperde9e3332012-07-31 06:02:05 +0000199static void emitEmptyTable(raw_ostream &o, unsigned &i) {
Craig Topperce8f4c52012-02-09 07:45:30 +0000200 o.indent(i * 2) << "0x0, /* EmptyTable */\n";
Sean Callanan8ed9f512009-12-19 02:59:52 +0000201}
202
203/// getDecisionType - Determines whether a ModRM decision with 255 entries can
204/// be compacted by eliminating redundant information.
205///
206/// @param decision - The decision to be compacted.
207/// @return - The compactest available representation for the decision.
Craig Toppere08789f2012-07-31 05:42:02 +0000208static ModRMDecisionType getDecisionType(ModRMDecision &decision) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000209 bool satisfiesOneEntry = true;
210 bool satisfiesSplitRM = true;
Craig Topperf41ab772012-02-09 08:58:07 +0000211 bool satisfiesSplitReg = true;
Craig Topper76b29b52012-09-13 05:45:42 +0000212 bool satisfiesSplitMisc = true;
Craig Topperf41ab772012-02-09 08:58:07 +0000213
Craig Topperde9e3332012-07-31 06:02:05 +0000214 for (unsigned index = 0; index < 256; ++index) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000215 if (decision.instructionIDs[index] != decision.instructionIDs[0])
216 satisfiesOneEntry = false;
Craig Topperf41ab772012-02-09 08:58:07 +0000217
Sean Callanan8ed9f512009-12-19 02:59:52 +0000218 if (((index & 0xc0) == 0xc0) &&
219 (decision.instructionIDs[index] != decision.instructionIDs[0xc0]))
220 satisfiesSplitRM = false;
Craig Topperf41ab772012-02-09 08:58:07 +0000221
Sean Callanan8ed9f512009-12-19 02:59:52 +0000222 if (((index & 0xc0) != 0xc0) &&
223 (decision.instructionIDs[index] != decision.instructionIDs[0x00]))
224 satisfiesSplitRM = false;
Craig Topperf41ab772012-02-09 08:58:07 +0000225
226 if (((index & 0xc0) == 0xc0) &&
227 (decision.instructionIDs[index] != decision.instructionIDs[index&0xf8]))
228 satisfiesSplitReg = false;
229
230 if (((index & 0xc0) != 0xc0) &&
231 (decision.instructionIDs[index] != decision.instructionIDs[index&0x38]))
Craig Topper76b29b52012-09-13 05:45:42 +0000232 satisfiesSplitMisc = false;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000233 }
Craig Topperf41ab772012-02-09 08:58:07 +0000234
Sean Callanan8ed9f512009-12-19 02:59:52 +0000235 if (satisfiesOneEntry)
236 return MODRM_ONEENTRY;
Craig Topperf41ab772012-02-09 08:58:07 +0000237
Sean Callanan8ed9f512009-12-19 02:59:52 +0000238 if (satisfiesSplitRM)
239 return MODRM_SPLITRM;
Craig Topperf41ab772012-02-09 08:58:07 +0000240
Craig Topper76b29b52012-09-13 05:45:42 +0000241 if (satisfiesSplitReg && satisfiesSplitMisc)
Craig Topperf41ab772012-02-09 08:58:07 +0000242 return MODRM_SPLITREG;
243
Craig Topper76b29b52012-09-13 05:45:42 +0000244 if (satisfiesSplitMisc)
245 return MODRM_SPLITMISC;
246
Sean Callanan8ed9f512009-12-19 02:59:52 +0000247 return MODRM_FULL;
248}
249
250/// stringForDecisionType - Returns a statically-allocated string corresponding
251/// to a particular decision type.
252///
253/// @param dt - The decision type.
Craig Topper98400092012-07-31 05:27:01 +0000254/// @return - A pointer to the statically-allocated string (e.g.,
Sean Callanan8ed9f512009-12-19 02:59:52 +0000255/// "MODRM_ONEENTRY" for MODRM_ONEENTRY).
Craig Toppere08789f2012-07-31 05:42:02 +0000256static const char* stringForDecisionType(ModRMDecisionType dt) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000257#define ENUM_ENTRY(n) case n: return #n;
258 switch (dt) {
259 default:
Craig Topper98400092012-07-31 05:27:01 +0000260 llvm_unreachable("Unknown decision type");
Sean Callanan8ed9f512009-12-19 02:59:52 +0000261 MODRMTYPES
Craig Topper98400092012-07-31 05:27:01 +0000262 };
Sean Callanan8ed9f512009-12-19 02:59:52 +0000263#undef ENUM_ENTRY
264}
Craig Topper98400092012-07-31 05:27:01 +0000265
Sean Callanan8ed9f512009-12-19 02:59:52 +0000266/// stringForModifierType - Returns a statically-allocated string corresponding
267/// to an opcode modifier type.
268///
269/// @param mt - The modifier type.
270/// @return - A pointer to the statically-allocated string (e.g.,
271/// "MODIFIER_NONE" for MODIFIER_NONE).
Craig Toppere08789f2012-07-31 05:42:02 +0000272static const char* stringForModifierType(ModifierType mt) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000273#define ENUM_ENTRY(n) case n: return #n;
274 switch(mt) {
275 default:
276 llvm_unreachable("Unknown modifier type");
277 MODIFIER_TYPES
278 };
279#undef ENUM_ENTRY
280}
Craig Topper98400092012-07-31 05:27:01 +0000281
Sean Callanan8ed9f512009-12-19 02:59:52 +0000282DisassemblerTables::DisassemblerTables() {
283 unsigned i;
Craig Topper98400092012-07-31 05:27:01 +0000284
Joerg Sonnenberger39d7cae2011-04-04 16:25:38 +0000285 for (i = 0; i < array_lengthof(Tables); i++) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000286 Tables[i] = new ContextDecision;
Daniel Dunbar87830872009-12-19 04:16:57 +0000287 memset(Tables[i], 0, sizeof(ContextDecision));
Sean Callanan8ed9f512009-12-19 02:59:52 +0000288 }
Craig Topper98400092012-07-31 05:27:01 +0000289
Sean Callanan8ed9f512009-12-19 02:59:52 +0000290 HasConflicts = false;
291}
Craig Topper98400092012-07-31 05:27:01 +0000292
Sean Callanan8ed9f512009-12-19 02:59:52 +0000293DisassemblerTables::~DisassemblerTables() {
294 unsigned i;
Craig Topper98400092012-07-31 05:27:01 +0000295
Joerg Sonnenberger39d7cae2011-04-04 16:25:38 +0000296 for (i = 0; i < array_lengthof(Tables); i++)
Sean Callanan8ed9f512009-12-19 02:59:52 +0000297 delete Tables[i];
298}
Craig Topper98400092012-07-31 05:27:01 +0000299
Craig Toppere08789f2012-07-31 05:42:02 +0000300void DisassemblerTables::emitModRMDecision(raw_ostream &o1, raw_ostream &o2,
Craig Topperde9e3332012-07-31 06:02:05 +0000301 unsigned &i1, unsigned &i2,
Craig Toppere08789f2012-07-31 05:42:02 +0000302 ModRMDecision &decision) const {
Craig Topperde9e3332012-07-31 06:02:05 +0000303 static uint32_t sTableNumber = 0;
304 static uint32_t sEntryNumber = 1;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000305 ModRMDecisionType dt = getDecisionType(decision);
Craig Topperce8f4c52012-02-09 07:45:30 +0000306
Sean Callanan8ed9f512009-12-19 02:59:52 +0000307 if (dt == MODRM_ONEENTRY && decision.instructionIDs[0] == 0)
308 {
309 o2.indent(i2) << "{ /* ModRMDecision */" << "\n";
310 i2++;
Craig Topperce8f4c52012-02-09 07:45:30 +0000311
Sean Callanan8ed9f512009-12-19 02:59:52 +0000312 o2.indent(i2) << stringForDecisionType(dt) << "," << "\n";
Craig Topperce8f4c52012-02-09 07:45:30 +0000313 o2.indent(i2) << 0 << " /* EmptyTable */\n";
314
Sean Callanan8ed9f512009-12-19 02:59:52 +0000315 i2--;
316 o2.indent(i2) << "}";
317 return;
318 }
Sean Callanan8ed9f512009-12-19 02:59:52 +0000319
Craig Topperce8f4c52012-02-09 07:45:30 +0000320 o1 << "/* Table" << sTableNumber << " */\n";
Sean Callanan8ed9f512009-12-19 02:59:52 +0000321 i1++;
Craig Topperce8f4c52012-02-09 07:45:30 +0000322
Sean Callanan8ed9f512009-12-19 02:59:52 +0000323 switch (dt) {
324 default:
325 llvm_unreachable("Unknown decision type");
326 case MODRM_ONEENTRY:
Craig Topperce8f4c52012-02-09 07:45:30 +0000327 emitOneID(o1, i1, decision.instructionIDs[0], true);
Sean Callanan8ed9f512009-12-19 02:59:52 +0000328 break;
329 case MODRM_SPLITRM:
330 emitOneID(o1, i1, decision.instructionIDs[0x00], true); // mod = 0b00
Craig Topperce8f4c52012-02-09 07:45:30 +0000331 emitOneID(o1, i1, decision.instructionIDs[0xc0], true); // mod = 0b11
Sean Callanan8ed9f512009-12-19 02:59:52 +0000332 break;
Craig Topperf41ab772012-02-09 08:58:07 +0000333 case MODRM_SPLITREG:
Craig Topperde9e3332012-07-31 06:02:05 +0000334 for (unsigned index = 0; index < 64; index += 8)
Craig Topperf41ab772012-02-09 08:58:07 +0000335 emitOneID(o1, i1, decision.instructionIDs[index], true);
Craig Topperde9e3332012-07-31 06:02:05 +0000336 for (unsigned index = 0xc0; index < 256; index += 8)
Craig Topperf41ab772012-02-09 08:58:07 +0000337 emitOneID(o1, i1, decision.instructionIDs[index], true);
338 break;
Craig Topper76b29b52012-09-13 05:45:42 +0000339 case MODRM_SPLITMISC:
340 for (unsigned index = 0; index < 64; index += 8)
341 emitOneID(o1, i1, decision.instructionIDs[index], true);
342 for (unsigned index = 0xc0; index < 256; ++index)
343 emitOneID(o1, i1, decision.instructionIDs[index], true);
344 break;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000345 case MODRM_FULL:
Craig Topperde9e3332012-07-31 06:02:05 +0000346 for (unsigned index = 0; index < 256; ++index)
Craig Topperce8f4c52012-02-09 07:45:30 +0000347 emitOneID(o1, i1, decision.instructionIDs[index], true);
Sean Callanan8ed9f512009-12-19 02:59:52 +0000348 break;
349 }
Craig Topperce8f4c52012-02-09 07:45:30 +0000350
Sean Callanan8ed9f512009-12-19 02:59:52 +0000351 i1--;
Craig Topperce8f4c52012-02-09 07:45:30 +0000352
Sean Callanan8ed9f512009-12-19 02:59:52 +0000353 o2.indent(i2) << "{ /* struct ModRMDecision */" << "\n";
354 i2++;
Craig Topperce8f4c52012-02-09 07:45:30 +0000355
Sean Callanan8ed9f512009-12-19 02:59:52 +0000356 o2.indent(i2) << stringForDecisionType(dt) << "," << "\n";
Craig Topperce8f4c52012-02-09 07:45:30 +0000357 o2.indent(i2) << sEntryNumber << " /* Table" << sTableNumber << " */\n";
358
Sean Callanan8ed9f512009-12-19 02:59:52 +0000359 i2--;
360 o2.indent(i2) << "}";
Craig Topperce8f4c52012-02-09 07:45:30 +0000361
362 switch (dt) {
363 default:
364 llvm_unreachable("Unknown decision type");
365 case MODRM_ONEENTRY:
366 sEntryNumber += 1;
367 break;
368 case MODRM_SPLITRM:
369 sEntryNumber += 2;
370 break;
Craig Topperf41ab772012-02-09 08:58:07 +0000371 case MODRM_SPLITREG:
372 sEntryNumber += 16;
373 break;
Craig Topper76b29b52012-09-13 05:45:42 +0000374 case MODRM_SPLITMISC:
375 sEntryNumber += 8 + 64;
376 break;
Craig Topperce8f4c52012-02-09 07:45:30 +0000377 case MODRM_FULL:
378 sEntryNumber += 256;
379 break;
380 }
381
Craig Topper9e6dc8b2012-09-11 04:19:21 +0000382 // We assume that the index can fit into uint16_t.
383 assert(sEntryNumber < 65536U &&
384 "Index into ModRMDecision is too large for uint16_t!");
385
Sean Callanan8ed9f512009-12-19 02:59:52 +0000386 ++sTableNumber;
387}
388
Craig Toppere08789f2012-07-31 05:42:02 +0000389void DisassemblerTables::emitOpcodeDecision(raw_ostream &o1, raw_ostream &o2,
Craig Topperde9e3332012-07-31 06:02:05 +0000390 unsigned &i1, unsigned &i2,
Craig Toppere08789f2012-07-31 05:42:02 +0000391 OpcodeDecision &decision) const {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000392 o2.indent(i2) << "{ /* struct OpcodeDecision */" << "\n";
393 i2++;
394 o2.indent(i2) << "{" << "\n";
395 i2++;
396
Craig Topperde9e3332012-07-31 06:02:05 +0000397 for (unsigned index = 0; index < 256; ++index) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000398 o2.indent(i2);
399
400 o2 << "/* 0x" << format("%02hhx", index) << " */" << "\n";
401
402 emitModRMDecision(o1, o2, i1, i2, decision.modRMDecisions[index]);
403
404 if (index < 255)
405 o2 << ",";
406
407 o2 << "\n";
408 }
409
410 i2--;
411 o2.indent(i2) << "}" << "\n";
412 i2--;
413 o2.indent(i2) << "}" << "\n";
414}
415
Craig Toppere08789f2012-07-31 05:42:02 +0000416void DisassemblerTables::emitContextDecision(raw_ostream &o1, raw_ostream &o2,
Craig Topperde9e3332012-07-31 06:02:05 +0000417 unsigned &i1, unsigned &i2,
Craig Toppere08789f2012-07-31 05:42:02 +0000418 ContextDecision &decision,
419 const char* name) const {
Benjamin Kramer4d1dca92010-10-23 09:10:44 +0000420 o2.indent(i2) << "static const struct ContextDecision " << name << " = {\n";
Sean Callanan8ed9f512009-12-19 02:59:52 +0000421 i2++;
422 o2.indent(i2) << "{ /* opcodeDecisions */" << "\n";
423 i2++;
424
Craig Topperde9e3332012-07-31 06:02:05 +0000425 for (unsigned index = 0; index < IC_max; ++index) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000426 o2.indent(i2) << "/* ";
427 o2 << stringForContext((InstructionContext)index);
428 o2 << " */";
429 o2 << "\n";
430
431 emitOpcodeDecision(o1, o2, i1, i2, decision.opcodeDecisions[index]);
432
433 if (index + 1 < IC_max)
434 o2 << ", ";
435 }
436
437 i2--;
438 o2.indent(i2) << "}" << "\n";
439 i2--;
440 o2.indent(i2) << "};" << "\n";
441}
442
Craig Topperde9e3332012-07-31 06:02:05 +0000443void DisassemblerTables::emitInstructionInfo(raw_ostream &o,
444 unsigned &i) const {
Craig Topper5a2c6072012-08-01 07:39:18 +0000445 unsigned NumInstructions = InstructionSpecifiers.size();
446
447 o << "static const struct OperandSpecifier x86OperandSets[]["
448 << X86_MAX_OPERANDS << "] = {\n";
449
450 typedef std::vector<std::pair<const char *, const char *> > OperandListTy;
451 std::map<OperandListTy, unsigned> OperandSets;
452
453 unsigned OperandSetNum = 0;
454 for (unsigned Index = 0; Index < NumInstructions; ++Index) {
455 OperandListTy OperandList;
456
457 for (unsigned OperandIndex = 0; OperandIndex < X86_MAX_OPERANDS;
458 ++OperandIndex) {
459 const char *Encoding =
460 stringForOperandEncoding((OperandEncoding)InstructionSpecifiers[Index]
461 .operands[OperandIndex].encoding);
462 const char *Type =
463 stringForOperandType((OperandType)InstructionSpecifiers[Index]
464 .operands[OperandIndex].type);
465 OperandList.push_back(std::make_pair(Encoding, Type));
466 }
467 unsigned &N = OperandSets[OperandList];
468 if (N != 0) continue;
469
470 N = ++OperandSetNum;
471
472 o << " { /* " << (OperandSetNum - 1) << " */\n";
473 for (unsigned i = 0, e = OperandList.size(); i != e; ++i) {
474 o << " { " << OperandList[i].first << ", "
475 << OperandList[i].second << " },\n";
476 }
477 o << " },\n";
478 }
479 o << "};" << "\n\n";
480
Benjamin Kramer4d1dca92010-10-23 09:10:44 +0000481 o.indent(i * 2) << "static const struct InstructionSpecifier ";
482 o << INSTRUCTIONS_STR "[" << InstructionSpecifiers.size() << "] = {\n";
Craig Topper98400092012-07-31 05:27:01 +0000483
Sean Callanan8ed9f512009-12-19 02:59:52 +0000484 i++;
485
Craig Topper5a2c6072012-08-01 07:39:18 +0000486 for (unsigned index = 0; index < NumInstructions; ++index) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000487 o.indent(i * 2) << "{ /* " << index << " */" << "\n";
488 i++;
Craig Topper991271d2012-03-04 02:16:41 +0000489
490 o.indent(i * 2) << stringForModifierType(
491 (ModifierType)InstructionSpecifiers[index].modifierType);
Craig Topper5a2c6072012-08-01 07:39:18 +0000492 o << ",\n";
Craig Topper991271d2012-03-04 02:16:41 +0000493
Sean Callanan8ed9f512009-12-19 02:59:52 +0000494 o.indent(i * 2) << "0x";
495 o << format("%02hhx", (uint16_t)InstructionSpecifiers[index].modifierBase);
Craig Topper5a2c6072012-08-01 07:39:18 +0000496 o << ",\n";
Sean Callanan8ed9f512009-12-19 02:59:52 +0000497
Craig Topper5a2c6072012-08-01 07:39:18 +0000498 OperandListTy OperandList;
499 for (unsigned OperandIndex = 0; OperandIndex < X86_MAX_OPERANDS;
500 ++OperandIndex) {
501 const char *Encoding =
502 stringForOperandEncoding((OperandEncoding)InstructionSpecifiers[index]
503 .operands[OperandIndex].encoding);
504 const char *Type =
505 stringForOperandType((OperandType)InstructionSpecifiers[index]
506 .operands[OperandIndex].type);
507 OperandList.push_back(std::make_pair(Encoding, Type));
Sean Callanan8ed9f512009-12-19 02:59:52 +0000508 }
Craig Topper5a2c6072012-08-01 07:39:18 +0000509 o.indent(i * 2) << (OperandSets[OperandList] - 1) << ",\n";
Craig Topper98400092012-07-31 05:27:01 +0000510
Benjamin Kramer953362c2012-02-11 14:50:54 +0000511 o.indent(i * 2) << "/* " << InstructionSpecifiers[index].name << " */";
Sean Callanan8ed9f512009-12-19 02:59:52 +0000512 o << "\n";
513
514 i--;
515 o.indent(i * 2) << "}";
516
Craig Topper5a2c6072012-08-01 07:39:18 +0000517 if (index + 1 < NumInstructions)
Sean Callanan8ed9f512009-12-19 02:59:52 +0000518 o << ",";
519
520 o << "\n";
521 }
522
523 i--;
524 o.indent(i * 2) << "};" << "\n";
525}
526
Craig Topperde9e3332012-07-31 06:02:05 +0000527void DisassemblerTables::emitContextTable(raw_ostream &o, unsigned &i) const {
Craig Topper19dbe2f2012-07-31 06:15:39 +0000528 o.indent(i * 2) << "static const uint8_t " CONTEXTS_STR
Benjamin Kramer86c69c52010-10-23 09:28:42 +0000529 "[256] = {\n";
Sean Callanan8ed9f512009-12-19 02:59:52 +0000530 i++;
531
Craig Topperde9e3332012-07-31 06:02:05 +0000532 for (unsigned index = 0; index < 256; ++index) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000533 o.indent(i * 2);
534
Craig Topperc8eb8802011-11-06 23:04:08 +0000535 if ((index & ATTR_VEXL) && (index & ATTR_REXW) && (index & ATTR_OPSIZE))
536 o << "IC_VEX_L_W_OPSIZE";
537 else if ((index & ATTR_VEXL) && (index & ATTR_OPSIZE))
Sean Callanana21e2ea2011-03-15 01:23:15 +0000538 o << "IC_VEX_L_OPSIZE";
539 else if ((index & ATTR_VEXL) && (index & ATTR_XD))
540 o << "IC_VEX_L_XD";
541 else if ((index & ATTR_VEXL) && (index & ATTR_XS))
542 o << "IC_VEX_L_XS";
543 else if ((index & ATTR_VEX) && (index & ATTR_REXW) && (index & ATTR_OPSIZE))
544 o << "IC_VEX_W_OPSIZE";
545 else if ((index & ATTR_VEX) && (index & ATTR_REXW) && (index & ATTR_XD))
546 o << "IC_VEX_W_XD";
547 else if ((index & ATTR_VEX) && (index & ATTR_REXW) && (index & ATTR_XS))
548 o << "IC_VEX_W_XS";
549 else if (index & ATTR_VEXL)
550 o << "IC_VEX_L";
551 else if ((index & ATTR_VEX) && (index & ATTR_REXW))
552 o << "IC_VEX_W";
553 else if ((index & ATTR_VEX) && (index & ATTR_OPSIZE))
554 o << "IC_VEX_OPSIZE";
555 else if ((index & ATTR_VEX) && (index & ATTR_XD))
556 o << "IC_VEX_XD";
557 else if ((index & ATTR_VEX) && (index & ATTR_XS))
558 o << "IC_VEX_XS";
Craig Topper113061d2011-08-25 07:42:00 +0000559 else if (index & ATTR_VEX)
560 o << "IC_VEX";
Sean Callanana21e2ea2011-03-15 01:23:15 +0000561 else if ((index & ATTR_64BIT) && (index & ATTR_REXW) && (index & ATTR_XS))
Sean Callanan8ed9f512009-12-19 02:59:52 +0000562 o << "IC_64BIT_REXW_XS";
563 else if ((index & ATTR_64BIT) && (index & ATTR_REXW) && (index & ATTR_XD))
564 o << "IC_64BIT_REXW_XD";
Craig Topper98400092012-07-31 05:27:01 +0000565 else if ((index & ATTR_64BIT) && (index & ATTR_REXW) &&
Sean Callanan8ed9f512009-12-19 02:59:52 +0000566 (index & ATTR_OPSIZE))
567 o << "IC_64BIT_REXW_OPSIZE";
Craig Toppere1b4a1a2011-10-01 19:54:56 +0000568 else if ((index & ATTR_64BIT) && (index & ATTR_XD) && (index & ATTR_OPSIZE))
569 o << "IC_64BIT_XD_OPSIZE";
Craig Topper29480fd2011-10-11 04:34:23 +0000570 else if ((index & ATTR_64BIT) && (index & ATTR_XS) && (index & ATTR_OPSIZE))
571 o << "IC_64BIT_XS_OPSIZE";
Sean Callanan8ed9f512009-12-19 02:59:52 +0000572 else if ((index & ATTR_64BIT) && (index & ATTR_XS))
573 o << "IC_64BIT_XS";
574 else if ((index & ATTR_64BIT) && (index & ATTR_XD))
575 o << "IC_64BIT_XD";
576 else if ((index & ATTR_64BIT) && (index & ATTR_OPSIZE))
577 o << "IC_64BIT_OPSIZE";
Craig Topper930a1eb2012-02-27 01:54:29 +0000578 else if ((index & ATTR_64BIT) && (index & ATTR_ADSIZE))
579 o << "IC_64BIT_ADSIZE";
Sean Callanan8ed9f512009-12-19 02:59:52 +0000580 else if ((index & ATTR_64BIT) && (index & ATTR_REXW))
581 o << "IC_64BIT_REXW";
582 else if ((index & ATTR_64BIT))
583 o << "IC_64BIT";
Craig Topper29480fd2011-10-11 04:34:23 +0000584 else if ((index & ATTR_XS) && (index & ATTR_OPSIZE))
585 o << "IC_XS_OPSIZE";
Craig Toppere1b4a1a2011-10-01 19:54:56 +0000586 else if ((index & ATTR_XD) && (index & ATTR_OPSIZE))
587 o << "IC_XD_OPSIZE";
Sean Callanan8ed9f512009-12-19 02:59:52 +0000588 else if (index & ATTR_XS)
589 o << "IC_XS";
590 else if (index & ATTR_XD)
591 o << "IC_XD";
592 else if (index & ATTR_OPSIZE)
593 o << "IC_OPSIZE";
Craig Topper930a1eb2012-02-27 01:54:29 +0000594 else if (index & ATTR_ADSIZE)
595 o << "IC_ADSIZE";
Sean Callanan8ed9f512009-12-19 02:59:52 +0000596 else
597 o << "IC";
598
599 if (index < 255)
600 o << ",";
601 else
602 o << " ";
603
604 o << " /* " << index << " */";
605
606 o << "\n";
607 }
608
609 i--;
610 o.indent(i * 2) << "};" << "\n";
611}
612
Craig Toppere08789f2012-07-31 05:42:02 +0000613void DisassemblerTables::emitContextDecisions(raw_ostream &o1, raw_ostream &o2,
Craig Topperde9e3332012-07-31 06:02:05 +0000614 unsigned &i1, unsigned &i2) const {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000615 emitContextDecision(o1, o2, i1, i2, *Tables[0], ONEBYTE_STR);
616 emitContextDecision(o1, o2, i1, i2, *Tables[1], TWOBYTE_STR);
617 emitContextDecision(o1, o2, i1, i2, *Tables[2], THREEBYTE38_STR);
618 emitContextDecision(o1, o2, i1, i2, *Tables[3], THREEBYTE3A_STR);
Joerg Sonnenberger4a8ac8d2011-04-04 16:58:13 +0000619 emitContextDecision(o1, o2, i1, i2, *Tables[4], THREEBYTEA6_STR);
620 emitContextDecision(o1, o2, i1, i2, *Tables[5], THREEBYTEA7_STR);
Sean Callanan8ed9f512009-12-19 02:59:52 +0000621}
622
623void DisassemblerTables::emit(raw_ostream &o) const {
Craig Topperde9e3332012-07-31 06:02:05 +0000624 unsigned i1 = 0;
625 unsigned i2 = 0;
Craig Topper98400092012-07-31 05:27:01 +0000626
Sean Callanan8ed9f512009-12-19 02:59:52 +0000627 std::string s1;
628 std::string s2;
Craig Topper98400092012-07-31 05:27:01 +0000629
Sean Callanan8ed9f512009-12-19 02:59:52 +0000630 raw_string_ostream o1(s1);
631 raw_string_ostream o2(s2);
Craig Topper98400092012-07-31 05:27:01 +0000632
Sean Callanan8ed9f512009-12-19 02:59:52 +0000633 emitInstructionInfo(o, i2);
634 o << "\n";
635
636 emitContextTable(o, i2);
637 o << "\n";
Craig Topperce8f4c52012-02-09 07:45:30 +0000638
639 o << "static const InstrUID modRMTable[] = {\n";
640 i1++;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000641 emitEmptyTable(o1, i1);
Craig Topperce8f4c52012-02-09 07:45:30 +0000642 i1--;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000643 emitContextDecisions(o1, o2, i1, i2);
Craig Topperce8f4c52012-02-09 07:45:30 +0000644
Sean Callanan8ed9f512009-12-19 02:59:52 +0000645 o << o1.str();
Craig Topperce8f4c52012-02-09 07:45:30 +0000646 o << " 0x0\n";
647 o << "};\n";
Sean Callanan8ed9f512009-12-19 02:59:52 +0000648 o << "\n";
649 o << o2.str();
650 o << "\n";
651 o << "\n";
652}
653
654void DisassemblerTables::setTableFields(ModRMDecision &decision,
655 const ModRMFilter &filter,
656 InstrUID uid,
657 uint8_t opcode) {
Craig Topperde9e3332012-07-31 06:02:05 +0000658 for (unsigned index = 0; index < 256; ++index) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000659 if (filter.accepts(index)) {
660 if (decision.instructionIDs[index] == uid)
661 continue;
662
663 if (decision.instructionIDs[index] != 0) {
664 InstructionSpecifier &newInfo =
665 InstructionSpecifiers[uid];
666 InstructionSpecifier &previousInfo =
667 InstructionSpecifiers[decision.instructionIDs[index]];
Craig Topper98400092012-07-31 05:27:01 +0000668
Sean Callanan8ed9f512009-12-19 02:59:52 +0000669 if(newInfo.filtered)
670 continue; // filtered instructions get lowest priority
Craig Topper98400092012-07-31 05:27:01 +0000671
Craig Topper842f58f2011-09-11 20:23:20 +0000672 if(previousInfo.name == "NOOP" && (newInfo.name == "XCHG16ar" ||
673 newInfo.name == "XCHG32ar" ||
Craig Topper25f6dfd2011-10-07 05:35:38 +0000674 newInfo.name == "XCHG32ar64" ||
Craig Topper842f58f2011-09-11 20:23:20 +0000675 newInfo.name == "XCHG64ar"))
676 continue; // special case for XCHG*ar and NOOP
Sean Callanan8ed9f512009-12-19 02:59:52 +0000677
678 if (outranks(previousInfo.insnContext, newInfo.insnContext))
679 continue;
Craig Topper98400092012-07-31 05:27:01 +0000680
Sean Callanan8ed9f512009-12-19 02:59:52 +0000681 if (previousInfo.insnContext == newInfo.insnContext &&
682 !previousInfo.filtered) {
683 errs() << "Error: Primary decode conflict: ";
684 errs() << newInfo.name << " would overwrite " << previousInfo.name;
685 errs() << "\n";
686 errs() << "ModRM " << index << "\n";
687 errs() << "Opcode " << (uint16_t)opcode << "\n";
688 errs() << "Context " << stringForContext(newInfo.insnContext) << "\n";
689 HasConflicts = true;
690 }
691 }
692
693 decision.instructionIDs[index] = uid;
694 }
695 }
696}
697
698void DisassemblerTables::setTableFields(OpcodeType type,
699 InstructionContext insnContext,
700 uint8_t opcode,
701 const ModRMFilter &filter,
Craig Topper4da632e2011-09-23 06:57:25 +0000702 InstrUID uid,
Craig Topper6744a172011-10-04 06:30:42 +0000703 bool is32bit,
704 bool ignoresVEX_L) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000705 ContextDecision &decision = *Tables[type];
706
Craig Topperde9e3332012-07-31 06:02:05 +0000707 for (unsigned index = 0; index < IC_max; ++index) {
Craig Topper4da632e2011-09-23 06:57:25 +0000708 if (is32bit && inheritsFrom((InstructionContext)index, IC_64BIT))
709 continue;
710
Craig Topper98400092012-07-31 05:27:01 +0000711 if (inheritsFrom((InstructionContext)index,
Craig Topper6744a172011-10-04 06:30:42 +0000712 InstructionSpecifiers[uid].insnContext, ignoresVEX_L))
Craig Topper98400092012-07-31 05:27:01 +0000713 setTableFields(decision.opcodeDecisions[index].modRMDecisions[opcode],
Sean Callanan8ed9f512009-12-19 02:59:52 +0000714 filter,
715 uid,
716 opcode);
717 }
718}