blob: c19520d317b08b5a3494283155e6cb6fb9ea30fc [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"
24
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;
211
Craig Topperde9e3332012-07-31 06:02:05 +0000212 for (unsigned index = 0; index < 256; ++index) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000213 if (decision.instructionIDs[index] != decision.instructionIDs[0])
214 satisfiesOneEntry = false;
Craig Topperf41ab772012-02-09 08:58:07 +0000215
Sean Callanan8ed9f512009-12-19 02:59:52 +0000216 if (((index & 0xc0) == 0xc0) &&
217 (decision.instructionIDs[index] != decision.instructionIDs[0xc0]))
218 satisfiesSplitRM = false;
Craig Topperf41ab772012-02-09 08:58:07 +0000219
Sean Callanan8ed9f512009-12-19 02:59:52 +0000220 if (((index & 0xc0) != 0xc0) &&
221 (decision.instructionIDs[index] != decision.instructionIDs[0x00]))
222 satisfiesSplitRM = false;
Craig Topperf41ab772012-02-09 08:58:07 +0000223
224 if (((index & 0xc0) == 0xc0) &&
225 (decision.instructionIDs[index] != decision.instructionIDs[index&0xf8]))
226 satisfiesSplitReg = false;
227
228 if (((index & 0xc0) != 0xc0) &&
229 (decision.instructionIDs[index] != decision.instructionIDs[index&0x38]))
230 satisfiesSplitReg = false;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000231 }
Craig Topperf41ab772012-02-09 08:58:07 +0000232
Sean Callanan8ed9f512009-12-19 02:59:52 +0000233 if (satisfiesOneEntry)
234 return MODRM_ONEENTRY;
Craig Topperf41ab772012-02-09 08:58:07 +0000235
Sean Callanan8ed9f512009-12-19 02:59:52 +0000236 if (satisfiesSplitRM)
237 return MODRM_SPLITRM;
Craig Topperf41ab772012-02-09 08:58:07 +0000238
239 if (satisfiesSplitReg)
240 return MODRM_SPLITREG;
241
Sean Callanan8ed9f512009-12-19 02:59:52 +0000242 return MODRM_FULL;
243}
244
245/// stringForDecisionType - Returns a statically-allocated string corresponding
246/// to a particular decision type.
247///
248/// @param dt - The decision type.
Craig Topper98400092012-07-31 05:27:01 +0000249/// @return - A pointer to the statically-allocated string (e.g.,
Sean Callanan8ed9f512009-12-19 02:59:52 +0000250/// "MODRM_ONEENTRY" for MODRM_ONEENTRY).
Craig Toppere08789f2012-07-31 05:42:02 +0000251static const char* stringForDecisionType(ModRMDecisionType dt) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000252#define ENUM_ENTRY(n) case n: return #n;
253 switch (dt) {
254 default:
Craig Topper98400092012-07-31 05:27:01 +0000255 llvm_unreachable("Unknown decision type");
Sean Callanan8ed9f512009-12-19 02:59:52 +0000256 MODRMTYPES
Craig Topper98400092012-07-31 05:27:01 +0000257 };
Sean Callanan8ed9f512009-12-19 02:59:52 +0000258#undef ENUM_ENTRY
259}
Craig Topper98400092012-07-31 05:27:01 +0000260
Sean Callanan8ed9f512009-12-19 02:59:52 +0000261/// stringForModifierType - Returns a statically-allocated string corresponding
262/// to an opcode modifier type.
263///
264/// @param mt - The modifier type.
265/// @return - A pointer to the statically-allocated string (e.g.,
266/// "MODIFIER_NONE" for MODIFIER_NONE).
Craig Toppere08789f2012-07-31 05:42:02 +0000267static const char* stringForModifierType(ModifierType mt) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000268#define ENUM_ENTRY(n) case n: return #n;
269 switch(mt) {
270 default:
271 llvm_unreachable("Unknown modifier type");
272 MODIFIER_TYPES
273 };
274#undef ENUM_ENTRY
275}
Craig Topper98400092012-07-31 05:27:01 +0000276
Sean Callanan8ed9f512009-12-19 02:59:52 +0000277DisassemblerTables::DisassemblerTables() {
278 unsigned i;
Craig Topper98400092012-07-31 05:27:01 +0000279
Joerg Sonnenberger39d7cae2011-04-04 16:25:38 +0000280 for (i = 0; i < array_lengthof(Tables); i++) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000281 Tables[i] = new ContextDecision;
Daniel Dunbar87830872009-12-19 04:16:57 +0000282 memset(Tables[i], 0, sizeof(ContextDecision));
Sean Callanan8ed9f512009-12-19 02:59:52 +0000283 }
Craig Topper98400092012-07-31 05:27:01 +0000284
Sean Callanan8ed9f512009-12-19 02:59:52 +0000285 HasConflicts = false;
286}
Craig Topper98400092012-07-31 05:27:01 +0000287
Sean Callanan8ed9f512009-12-19 02:59:52 +0000288DisassemblerTables::~DisassemblerTables() {
289 unsigned i;
Craig Topper98400092012-07-31 05:27:01 +0000290
Joerg Sonnenberger39d7cae2011-04-04 16:25:38 +0000291 for (i = 0; i < array_lengthof(Tables); i++)
Sean Callanan8ed9f512009-12-19 02:59:52 +0000292 delete Tables[i];
293}
Craig Topper98400092012-07-31 05:27:01 +0000294
Craig Toppere08789f2012-07-31 05:42:02 +0000295void DisassemblerTables::emitModRMDecision(raw_ostream &o1, raw_ostream &o2,
Craig Topperde9e3332012-07-31 06:02:05 +0000296 unsigned &i1, unsigned &i2,
Craig Toppere08789f2012-07-31 05:42:02 +0000297 ModRMDecision &decision) const {
Craig Topperde9e3332012-07-31 06:02:05 +0000298 static uint32_t sTableNumber = 0;
299 static uint32_t sEntryNumber = 1;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000300 ModRMDecisionType dt = getDecisionType(decision);
Craig Topperce8f4c52012-02-09 07:45:30 +0000301
Sean Callanan8ed9f512009-12-19 02:59:52 +0000302 if (dt == MODRM_ONEENTRY && decision.instructionIDs[0] == 0)
303 {
304 o2.indent(i2) << "{ /* ModRMDecision */" << "\n";
305 i2++;
Craig Topperce8f4c52012-02-09 07:45:30 +0000306
Sean Callanan8ed9f512009-12-19 02:59:52 +0000307 o2.indent(i2) << stringForDecisionType(dt) << "," << "\n";
Craig Topperce8f4c52012-02-09 07:45:30 +0000308 o2.indent(i2) << 0 << " /* EmptyTable */\n";
309
Sean Callanan8ed9f512009-12-19 02:59:52 +0000310 i2--;
311 o2.indent(i2) << "}";
312 return;
313 }
Sean Callanan8ed9f512009-12-19 02:59:52 +0000314
Craig Topperce8f4c52012-02-09 07:45:30 +0000315 o1 << "/* Table" << sTableNumber << " */\n";
Sean Callanan8ed9f512009-12-19 02:59:52 +0000316 i1++;
Craig Topperce8f4c52012-02-09 07:45:30 +0000317
Sean Callanan8ed9f512009-12-19 02:59:52 +0000318 switch (dt) {
319 default:
320 llvm_unreachable("Unknown decision type");
321 case MODRM_ONEENTRY:
Craig Topperce8f4c52012-02-09 07:45:30 +0000322 emitOneID(o1, i1, decision.instructionIDs[0], true);
Sean Callanan8ed9f512009-12-19 02:59:52 +0000323 break;
324 case MODRM_SPLITRM:
325 emitOneID(o1, i1, decision.instructionIDs[0x00], true); // mod = 0b00
Craig Topperce8f4c52012-02-09 07:45:30 +0000326 emitOneID(o1, i1, decision.instructionIDs[0xc0], true); // mod = 0b11
Sean Callanan8ed9f512009-12-19 02:59:52 +0000327 break;
Craig Topperf41ab772012-02-09 08:58:07 +0000328 case MODRM_SPLITREG:
Craig Topperde9e3332012-07-31 06:02:05 +0000329 for (unsigned index = 0; index < 64; index += 8)
Craig Topperf41ab772012-02-09 08:58:07 +0000330 emitOneID(o1, i1, decision.instructionIDs[index], true);
Craig Topperde9e3332012-07-31 06:02:05 +0000331 for (unsigned index = 0xc0; index < 256; index += 8)
Craig Topperf41ab772012-02-09 08:58:07 +0000332 emitOneID(o1, i1, decision.instructionIDs[index], true);
333 break;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000334 case MODRM_FULL:
Craig Topperde9e3332012-07-31 06:02:05 +0000335 for (unsigned index = 0; index < 256; ++index)
Craig Topperce8f4c52012-02-09 07:45:30 +0000336 emitOneID(o1, i1, decision.instructionIDs[index], true);
Sean Callanan8ed9f512009-12-19 02:59:52 +0000337 break;
338 }
Craig Topperce8f4c52012-02-09 07:45:30 +0000339
Sean Callanan8ed9f512009-12-19 02:59:52 +0000340 i1--;
Craig Topperce8f4c52012-02-09 07:45:30 +0000341
Sean Callanan8ed9f512009-12-19 02:59:52 +0000342 o2.indent(i2) << "{ /* struct ModRMDecision */" << "\n";
343 i2++;
Craig Topperce8f4c52012-02-09 07:45:30 +0000344
Sean Callanan8ed9f512009-12-19 02:59:52 +0000345 o2.indent(i2) << stringForDecisionType(dt) << "," << "\n";
Craig Topperce8f4c52012-02-09 07:45:30 +0000346 o2.indent(i2) << sEntryNumber << " /* Table" << sTableNumber << " */\n";
347
Sean Callanan8ed9f512009-12-19 02:59:52 +0000348 i2--;
349 o2.indent(i2) << "}";
Craig Topperce8f4c52012-02-09 07:45:30 +0000350
351 switch (dt) {
352 default:
353 llvm_unreachable("Unknown decision type");
354 case MODRM_ONEENTRY:
355 sEntryNumber += 1;
356 break;
357 case MODRM_SPLITRM:
358 sEntryNumber += 2;
359 break;
Craig Topperf41ab772012-02-09 08:58:07 +0000360 case MODRM_SPLITREG:
361 sEntryNumber += 16;
362 break;
Craig Topperce8f4c52012-02-09 07:45:30 +0000363 case MODRM_FULL:
364 sEntryNumber += 256;
365 break;
366 }
367
Sean Callanan8ed9f512009-12-19 02:59:52 +0000368 ++sTableNumber;
369}
370
Craig Toppere08789f2012-07-31 05:42:02 +0000371void DisassemblerTables::emitOpcodeDecision(raw_ostream &o1, raw_ostream &o2,
Craig Topperde9e3332012-07-31 06:02:05 +0000372 unsigned &i1, unsigned &i2,
Craig Toppere08789f2012-07-31 05:42:02 +0000373 OpcodeDecision &decision) const {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000374 o2.indent(i2) << "{ /* struct OpcodeDecision */" << "\n";
375 i2++;
376 o2.indent(i2) << "{" << "\n";
377 i2++;
378
Craig Topperde9e3332012-07-31 06:02:05 +0000379 for (unsigned index = 0; index < 256; ++index) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000380 o2.indent(i2);
381
382 o2 << "/* 0x" << format("%02hhx", index) << " */" << "\n";
383
384 emitModRMDecision(o1, o2, i1, i2, decision.modRMDecisions[index]);
385
386 if (index < 255)
387 o2 << ",";
388
389 o2 << "\n";
390 }
391
392 i2--;
393 o2.indent(i2) << "}" << "\n";
394 i2--;
395 o2.indent(i2) << "}" << "\n";
396}
397
Craig Toppere08789f2012-07-31 05:42:02 +0000398void DisassemblerTables::emitContextDecision(raw_ostream &o1, raw_ostream &o2,
Craig Topperde9e3332012-07-31 06:02:05 +0000399 unsigned &i1, unsigned &i2,
Craig Toppere08789f2012-07-31 05:42:02 +0000400 ContextDecision &decision,
401 const char* name) const {
Benjamin Kramer4d1dca92010-10-23 09:10:44 +0000402 o2.indent(i2) << "static const struct ContextDecision " << name << " = {\n";
Sean Callanan8ed9f512009-12-19 02:59:52 +0000403 i2++;
404 o2.indent(i2) << "{ /* opcodeDecisions */" << "\n";
405 i2++;
406
Craig Topperde9e3332012-07-31 06:02:05 +0000407 for (unsigned index = 0; index < IC_max; ++index) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000408 o2.indent(i2) << "/* ";
409 o2 << stringForContext((InstructionContext)index);
410 o2 << " */";
411 o2 << "\n";
412
413 emitOpcodeDecision(o1, o2, i1, i2, decision.opcodeDecisions[index]);
414
415 if (index + 1 < IC_max)
416 o2 << ", ";
417 }
418
419 i2--;
420 o2.indent(i2) << "}" << "\n";
421 i2--;
422 o2.indent(i2) << "};" << "\n";
423}
424
Craig Topperde9e3332012-07-31 06:02:05 +0000425void DisassemblerTables::emitInstructionInfo(raw_ostream &o,
426 unsigned &i) const {
Benjamin Kramer4d1dca92010-10-23 09:10:44 +0000427 o.indent(i * 2) << "static const struct InstructionSpecifier ";
428 o << INSTRUCTIONS_STR "[" << InstructionSpecifiers.size() << "] = {\n";
Craig Topper98400092012-07-31 05:27:01 +0000429
Sean Callanan8ed9f512009-12-19 02:59:52 +0000430 i++;
431
Craig Topperde9e3332012-07-31 06:02:05 +0000432 unsigned numInstructions = InstructionSpecifiers.size();
Sean Callanan8ed9f512009-12-19 02:59:52 +0000433
Craig Topperde9e3332012-07-31 06:02:05 +0000434 for (unsigned index = 0; index < numInstructions; ++index) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000435 o.indent(i * 2) << "{ /* " << index << " */" << "\n";
436 i++;
Craig Topper991271d2012-03-04 02:16:41 +0000437
438 o.indent(i * 2) << stringForModifierType(
439 (ModifierType)InstructionSpecifiers[index].modifierType);
Sean Callanan8ed9f512009-12-19 02:59:52 +0000440 o << "," << "\n";
Craig Topper991271d2012-03-04 02:16:41 +0000441
Sean Callanan8ed9f512009-12-19 02:59:52 +0000442 o.indent(i * 2) << "0x";
443 o << format("%02hhx", (uint16_t)InstructionSpecifiers[index].modifierBase);
444 o << "," << "\n";
445
446 o.indent(i * 2) << "{" << "\n";
447 i++;
448
Craig Topperde9e3332012-07-31 06:02:05 +0000449 for (unsigned operandIndex = 0; operandIndex < X86_MAX_OPERANDS;
450 ++operandIndex) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000451 o.indent(i * 2) << "{ ";
Craig Topper991271d2012-03-04 02:16:41 +0000452 o <<stringForOperandEncoding((OperandEncoding)InstructionSpecifiers[index]
453 .operands[operandIndex]
454 .encoding);
Sean Callanan8ed9f512009-12-19 02:59:52 +0000455 o << ", ";
Craig Topper991271d2012-03-04 02:16:41 +0000456 o << stringForOperandType((OperandType)InstructionSpecifiers[index]
Sean Callanan8ed9f512009-12-19 02:59:52 +0000457 .operands[operandIndex]
458 .type);
459 o << " }";
460
461 if (operandIndex < X86_MAX_OPERANDS - 1)
462 o << ",";
463
464 o << "\n";
465 }
466
467 i--;
468 o.indent(i * 2) << "}," << "\n";
Craig Topper98400092012-07-31 05:27:01 +0000469
Benjamin Kramer953362c2012-02-11 14:50:54 +0000470 o.indent(i * 2) << "/* " << InstructionSpecifiers[index].name << " */";
Sean Callanan8ed9f512009-12-19 02:59:52 +0000471 o << "\n";
472
473 i--;
474 o.indent(i * 2) << "}";
475
476 if (index + 1 < numInstructions)
477 o << ",";
478
479 o << "\n";
480 }
481
482 i--;
483 o.indent(i * 2) << "};" << "\n";
484}
485
Craig Topperde9e3332012-07-31 06:02:05 +0000486void DisassemblerTables::emitContextTable(raw_ostream &o, unsigned &i) const {
Benjamin Kramer86c69c52010-10-23 09:28:42 +0000487 o.indent(i * 2) << "static const InstructionContext " CONTEXTS_STR
488 "[256] = {\n";
Sean Callanan8ed9f512009-12-19 02:59:52 +0000489 i++;
490
Craig Topperde9e3332012-07-31 06:02:05 +0000491 for (unsigned index = 0; index < 256; ++index) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000492 o.indent(i * 2);
493
Craig Topperc8eb8802011-11-06 23:04:08 +0000494 if ((index & ATTR_VEXL) && (index & ATTR_REXW) && (index & ATTR_OPSIZE))
495 o << "IC_VEX_L_W_OPSIZE";
496 else if ((index & ATTR_VEXL) && (index & ATTR_OPSIZE))
Sean Callanana21e2ea2011-03-15 01:23:15 +0000497 o << "IC_VEX_L_OPSIZE";
498 else if ((index & ATTR_VEXL) && (index & ATTR_XD))
499 o << "IC_VEX_L_XD";
500 else if ((index & ATTR_VEXL) && (index & ATTR_XS))
501 o << "IC_VEX_L_XS";
502 else if ((index & ATTR_VEX) && (index & ATTR_REXW) && (index & ATTR_OPSIZE))
503 o << "IC_VEX_W_OPSIZE";
504 else if ((index & ATTR_VEX) && (index & ATTR_REXW) && (index & ATTR_XD))
505 o << "IC_VEX_W_XD";
506 else if ((index & ATTR_VEX) && (index & ATTR_REXW) && (index & ATTR_XS))
507 o << "IC_VEX_W_XS";
508 else if (index & ATTR_VEXL)
509 o << "IC_VEX_L";
510 else if ((index & ATTR_VEX) && (index & ATTR_REXW))
511 o << "IC_VEX_W";
512 else if ((index & ATTR_VEX) && (index & ATTR_OPSIZE))
513 o << "IC_VEX_OPSIZE";
514 else if ((index & ATTR_VEX) && (index & ATTR_XD))
515 o << "IC_VEX_XD";
516 else if ((index & ATTR_VEX) && (index & ATTR_XS))
517 o << "IC_VEX_XS";
Craig Topper113061d2011-08-25 07:42:00 +0000518 else if (index & ATTR_VEX)
519 o << "IC_VEX";
Sean Callanana21e2ea2011-03-15 01:23:15 +0000520 else if ((index & ATTR_64BIT) && (index & ATTR_REXW) && (index & ATTR_XS))
Sean Callanan8ed9f512009-12-19 02:59:52 +0000521 o << "IC_64BIT_REXW_XS";
522 else if ((index & ATTR_64BIT) && (index & ATTR_REXW) && (index & ATTR_XD))
523 o << "IC_64BIT_REXW_XD";
Craig Topper98400092012-07-31 05:27:01 +0000524 else if ((index & ATTR_64BIT) && (index & ATTR_REXW) &&
Sean Callanan8ed9f512009-12-19 02:59:52 +0000525 (index & ATTR_OPSIZE))
526 o << "IC_64BIT_REXW_OPSIZE";
Craig Toppere1b4a1a2011-10-01 19:54:56 +0000527 else if ((index & ATTR_64BIT) && (index & ATTR_XD) && (index & ATTR_OPSIZE))
528 o << "IC_64BIT_XD_OPSIZE";
Craig Topper29480fd2011-10-11 04:34:23 +0000529 else if ((index & ATTR_64BIT) && (index & ATTR_XS) && (index & ATTR_OPSIZE))
530 o << "IC_64BIT_XS_OPSIZE";
Sean Callanan8ed9f512009-12-19 02:59:52 +0000531 else if ((index & ATTR_64BIT) && (index & ATTR_XS))
532 o << "IC_64BIT_XS";
533 else if ((index & ATTR_64BIT) && (index & ATTR_XD))
534 o << "IC_64BIT_XD";
535 else if ((index & ATTR_64BIT) && (index & ATTR_OPSIZE))
536 o << "IC_64BIT_OPSIZE";
Craig Topper930a1eb2012-02-27 01:54:29 +0000537 else if ((index & ATTR_64BIT) && (index & ATTR_ADSIZE))
538 o << "IC_64BIT_ADSIZE";
Sean Callanan8ed9f512009-12-19 02:59:52 +0000539 else if ((index & ATTR_64BIT) && (index & ATTR_REXW))
540 o << "IC_64BIT_REXW";
541 else if ((index & ATTR_64BIT))
542 o << "IC_64BIT";
Craig Topper29480fd2011-10-11 04:34:23 +0000543 else if ((index & ATTR_XS) && (index & ATTR_OPSIZE))
544 o << "IC_XS_OPSIZE";
Craig Toppere1b4a1a2011-10-01 19:54:56 +0000545 else if ((index & ATTR_XD) && (index & ATTR_OPSIZE))
546 o << "IC_XD_OPSIZE";
Sean Callanan8ed9f512009-12-19 02:59:52 +0000547 else if (index & ATTR_XS)
548 o << "IC_XS";
549 else if (index & ATTR_XD)
550 o << "IC_XD";
551 else if (index & ATTR_OPSIZE)
552 o << "IC_OPSIZE";
Craig Topper930a1eb2012-02-27 01:54:29 +0000553 else if (index & ATTR_ADSIZE)
554 o << "IC_ADSIZE";
Sean Callanan8ed9f512009-12-19 02:59:52 +0000555 else
556 o << "IC";
557
558 if (index < 255)
559 o << ",";
560 else
561 o << " ";
562
563 o << " /* " << index << " */";
564
565 o << "\n";
566 }
567
568 i--;
569 o.indent(i * 2) << "};" << "\n";
570}
571
Craig Toppere08789f2012-07-31 05:42:02 +0000572void DisassemblerTables::emitContextDecisions(raw_ostream &o1, raw_ostream &o2,
Craig Topperde9e3332012-07-31 06:02:05 +0000573 unsigned &i1, unsigned &i2) const {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000574 emitContextDecision(o1, o2, i1, i2, *Tables[0], ONEBYTE_STR);
575 emitContextDecision(o1, o2, i1, i2, *Tables[1], TWOBYTE_STR);
576 emitContextDecision(o1, o2, i1, i2, *Tables[2], THREEBYTE38_STR);
577 emitContextDecision(o1, o2, i1, i2, *Tables[3], THREEBYTE3A_STR);
Joerg Sonnenberger4a8ac8d2011-04-04 16:58:13 +0000578 emitContextDecision(o1, o2, i1, i2, *Tables[4], THREEBYTEA6_STR);
579 emitContextDecision(o1, o2, i1, i2, *Tables[5], THREEBYTEA7_STR);
Sean Callanan8ed9f512009-12-19 02:59:52 +0000580}
581
582void DisassemblerTables::emit(raw_ostream &o) const {
Craig Topperde9e3332012-07-31 06:02:05 +0000583 unsigned i1 = 0;
584 unsigned i2 = 0;
Craig Topper98400092012-07-31 05:27:01 +0000585
Sean Callanan8ed9f512009-12-19 02:59:52 +0000586 std::string s1;
587 std::string s2;
Craig Topper98400092012-07-31 05:27:01 +0000588
Sean Callanan8ed9f512009-12-19 02:59:52 +0000589 raw_string_ostream o1(s1);
590 raw_string_ostream o2(s2);
Craig Topper98400092012-07-31 05:27:01 +0000591
Sean Callanan8ed9f512009-12-19 02:59:52 +0000592 emitInstructionInfo(o, i2);
593 o << "\n";
594
595 emitContextTable(o, i2);
596 o << "\n";
Craig Topperce8f4c52012-02-09 07:45:30 +0000597
598 o << "static const InstrUID modRMTable[] = {\n";
599 i1++;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000600 emitEmptyTable(o1, i1);
Craig Topperce8f4c52012-02-09 07:45:30 +0000601 i1--;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000602 emitContextDecisions(o1, o2, i1, i2);
Craig Topperce8f4c52012-02-09 07:45:30 +0000603
Sean Callanan8ed9f512009-12-19 02:59:52 +0000604 o << o1.str();
Craig Topperce8f4c52012-02-09 07:45:30 +0000605 o << " 0x0\n";
606 o << "};\n";
Sean Callanan8ed9f512009-12-19 02:59:52 +0000607 o << "\n";
608 o << o2.str();
609 o << "\n";
610 o << "\n";
611}
612
613void DisassemblerTables::setTableFields(ModRMDecision &decision,
614 const ModRMFilter &filter,
615 InstrUID uid,
616 uint8_t opcode) {
Craig Topperde9e3332012-07-31 06:02:05 +0000617 for (unsigned index = 0; index < 256; ++index) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000618 if (filter.accepts(index)) {
619 if (decision.instructionIDs[index] == uid)
620 continue;
621
622 if (decision.instructionIDs[index] != 0) {
623 InstructionSpecifier &newInfo =
624 InstructionSpecifiers[uid];
625 InstructionSpecifier &previousInfo =
626 InstructionSpecifiers[decision.instructionIDs[index]];
Craig Topper98400092012-07-31 05:27:01 +0000627
Sean Callanan8ed9f512009-12-19 02:59:52 +0000628 if(newInfo.filtered)
629 continue; // filtered instructions get lowest priority
Craig Topper98400092012-07-31 05:27:01 +0000630
Craig Topper842f58f2011-09-11 20:23:20 +0000631 if(previousInfo.name == "NOOP" && (newInfo.name == "XCHG16ar" ||
632 newInfo.name == "XCHG32ar" ||
Craig Topper25f6dfd2011-10-07 05:35:38 +0000633 newInfo.name == "XCHG32ar64" ||
Craig Topper842f58f2011-09-11 20:23:20 +0000634 newInfo.name == "XCHG64ar"))
635 continue; // special case for XCHG*ar and NOOP
Sean Callanan8ed9f512009-12-19 02:59:52 +0000636
637 if (outranks(previousInfo.insnContext, newInfo.insnContext))
638 continue;
Craig Topper98400092012-07-31 05:27:01 +0000639
Sean Callanan8ed9f512009-12-19 02:59:52 +0000640 if (previousInfo.insnContext == newInfo.insnContext &&
641 !previousInfo.filtered) {
642 errs() << "Error: Primary decode conflict: ";
643 errs() << newInfo.name << " would overwrite " << previousInfo.name;
644 errs() << "\n";
645 errs() << "ModRM " << index << "\n";
646 errs() << "Opcode " << (uint16_t)opcode << "\n";
647 errs() << "Context " << stringForContext(newInfo.insnContext) << "\n";
648 HasConflicts = true;
649 }
650 }
651
652 decision.instructionIDs[index] = uid;
653 }
654 }
655}
656
657void DisassemblerTables::setTableFields(OpcodeType type,
658 InstructionContext insnContext,
659 uint8_t opcode,
660 const ModRMFilter &filter,
Craig Topper4da632e2011-09-23 06:57:25 +0000661 InstrUID uid,
Craig Topper6744a172011-10-04 06:30:42 +0000662 bool is32bit,
663 bool ignoresVEX_L) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000664 ContextDecision &decision = *Tables[type];
665
Craig Topperde9e3332012-07-31 06:02:05 +0000666 for (unsigned index = 0; index < IC_max; ++index) {
Craig Topper4da632e2011-09-23 06:57:25 +0000667 if (is32bit && inheritsFrom((InstructionContext)index, IC_64BIT))
668 continue;
669
Craig Topper98400092012-07-31 05:27:01 +0000670 if (inheritsFrom((InstructionContext)index,
Craig Topper6744a172011-10-04 06:30:42 +0000671 InstructionSpecifiers[uid].insnContext, ignoresVEX_L))
Craig Topper98400092012-07-31 05:27:01 +0000672 setTableFields(decision.opcodeDecisions[index].modRMDecisions[opcode],
Sean Callanan8ed9f512009-12-19 02:59:52 +0000673 filter,
674 uid,
675 opcode);
676 }
677}