blob: 8526621599a911af457fbd57d377f4da8a580241 [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;
212
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]))
231 satisfiesSplitReg = 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
240 if (satisfiesSplitReg)
241 return MODRM_SPLITREG;
242
Sean Callanan8ed9f512009-12-19 02:59:52 +0000243 return MODRM_FULL;
244}
245
246/// stringForDecisionType - Returns a statically-allocated string corresponding
247/// to a particular decision type.
248///
249/// @param dt - The decision type.
Craig Topper98400092012-07-31 05:27:01 +0000250/// @return - A pointer to the statically-allocated string (e.g.,
Sean Callanan8ed9f512009-12-19 02:59:52 +0000251/// "MODRM_ONEENTRY" for MODRM_ONEENTRY).
Craig Toppere08789f2012-07-31 05:42:02 +0000252static const char* stringForDecisionType(ModRMDecisionType dt) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000253#define ENUM_ENTRY(n) case n: return #n;
254 switch (dt) {
255 default:
Craig Topper98400092012-07-31 05:27:01 +0000256 llvm_unreachable("Unknown decision type");
Sean Callanan8ed9f512009-12-19 02:59:52 +0000257 MODRMTYPES
Craig Topper98400092012-07-31 05:27:01 +0000258 };
Sean Callanan8ed9f512009-12-19 02:59:52 +0000259#undef ENUM_ENTRY
260}
Craig Topper98400092012-07-31 05:27:01 +0000261
Sean Callanan8ed9f512009-12-19 02:59:52 +0000262/// stringForModifierType - Returns a statically-allocated string corresponding
263/// to an opcode modifier type.
264///
265/// @param mt - The modifier type.
266/// @return - A pointer to the statically-allocated string (e.g.,
267/// "MODIFIER_NONE" for MODIFIER_NONE).
Craig Toppere08789f2012-07-31 05:42:02 +0000268static const char* stringForModifierType(ModifierType mt) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000269#define ENUM_ENTRY(n) case n: return #n;
270 switch(mt) {
271 default:
272 llvm_unreachable("Unknown modifier type");
273 MODIFIER_TYPES
274 };
275#undef ENUM_ENTRY
276}
Craig Topper98400092012-07-31 05:27:01 +0000277
Sean Callanan8ed9f512009-12-19 02:59:52 +0000278DisassemblerTables::DisassemblerTables() {
279 unsigned i;
Craig Topper98400092012-07-31 05:27:01 +0000280
Joerg Sonnenberger39d7cae2011-04-04 16:25:38 +0000281 for (i = 0; i < array_lengthof(Tables); i++) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000282 Tables[i] = new ContextDecision;
Daniel Dunbar87830872009-12-19 04:16:57 +0000283 memset(Tables[i], 0, sizeof(ContextDecision));
Sean Callanan8ed9f512009-12-19 02:59:52 +0000284 }
Craig Topper98400092012-07-31 05:27:01 +0000285
Sean Callanan8ed9f512009-12-19 02:59:52 +0000286 HasConflicts = false;
287}
Craig Topper98400092012-07-31 05:27:01 +0000288
Sean Callanan8ed9f512009-12-19 02:59:52 +0000289DisassemblerTables::~DisassemblerTables() {
290 unsigned i;
Craig Topper98400092012-07-31 05:27:01 +0000291
Joerg Sonnenberger39d7cae2011-04-04 16:25:38 +0000292 for (i = 0; i < array_lengthof(Tables); i++)
Sean Callanan8ed9f512009-12-19 02:59:52 +0000293 delete Tables[i];
294}
Craig Topper98400092012-07-31 05:27:01 +0000295
Craig Toppere08789f2012-07-31 05:42:02 +0000296void DisassemblerTables::emitModRMDecision(raw_ostream &o1, raw_ostream &o2,
Craig Topperde9e3332012-07-31 06:02:05 +0000297 unsigned &i1, unsigned &i2,
Craig Toppere08789f2012-07-31 05:42:02 +0000298 ModRMDecision &decision) const {
Craig Topperde9e3332012-07-31 06:02:05 +0000299 static uint32_t sTableNumber = 0;
300 static uint32_t sEntryNumber = 1;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000301 ModRMDecisionType dt = getDecisionType(decision);
Craig Topperce8f4c52012-02-09 07:45:30 +0000302
Sean Callanan8ed9f512009-12-19 02:59:52 +0000303 if (dt == MODRM_ONEENTRY && decision.instructionIDs[0] == 0)
304 {
305 o2.indent(i2) << "{ /* ModRMDecision */" << "\n";
306 i2++;
Craig Topperce8f4c52012-02-09 07:45:30 +0000307
Sean Callanan8ed9f512009-12-19 02:59:52 +0000308 o2.indent(i2) << stringForDecisionType(dt) << "," << "\n";
Craig Topperce8f4c52012-02-09 07:45:30 +0000309 o2.indent(i2) << 0 << " /* EmptyTable */\n";
310
Sean Callanan8ed9f512009-12-19 02:59:52 +0000311 i2--;
312 o2.indent(i2) << "}";
313 return;
314 }
Sean Callanan8ed9f512009-12-19 02:59:52 +0000315
Craig Topperce8f4c52012-02-09 07:45:30 +0000316 o1 << "/* Table" << sTableNumber << " */\n";
Sean Callanan8ed9f512009-12-19 02:59:52 +0000317 i1++;
Craig Topperce8f4c52012-02-09 07:45:30 +0000318
Sean Callanan8ed9f512009-12-19 02:59:52 +0000319 switch (dt) {
320 default:
321 llvm_unreachable("Unknown decision type");
322 case MODRM_ONEENTRY:
Craig Topperce8f4c52012-02-09 07:45:30 +0000323 emitOneID(o1, i1, decision.instructionIDs[0], true);
Sean Callanan8ed9f512009-12-19 02:59:52 +0000324 break;
325 case MODRM_SPLITRM:
326 emitOneID(o1, i1, decision.instructionIDs[0x00], true); // mod = 0b00
Craig Topperce8f4c52012-02-09 07:45:30 +0000327 emitOneID(o1, i1, decision.instructionIDs[0xc0], true); // mod = 0b11
Sean Callanan8ed9f512009-12-19 02:59:52 +0000328 break;
Craig Topperf41ab772012-02-09 08:58:07 +0000329 case MODRM_SPLITREG:
Craig Topperde9e3332012-07-31 06:02:05 +0000330 for (unsigned index = 0; index < 64; index += 8)
Craig Topperf41ab772012-02-09 08:58:07 +0000331 emitOneID(o1, i1, decision.instructionIDs[index], true);
Craig Topperde9e3332012-07-31 06:02:05 +0000332 for (unsigned index = 0xc0; index < 256; index += 8)
Craig Topperf41ab772012-02-09 08:58:07 +0000333 emitOneID(o1, i1, decision.instructionIDs[index], true);
334 break;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000335 case MODRM_FULL:
Craig Topperde9e3332012-07-31 06:02:05 +0000336 for (unsigned index = 0; index < 256; ++index)
Craig Topperce8f4c52012-02-09 07:45:30 +0000337 emitOneID(o1, i1, decision.instructionIDs[index], true);
Sean Callanan8ed9f512009-12-19 02:59:52 +0000338 break;
339 }
Craig Topperce8f4c52012-02-09 07:45:30 +0000340
Sean Callanan8ed9f512009-12-19 02:59:52 +0000341 i1--;
Craig Topperce8f4c52012-02-09 07:45:30 +0000342
Sean Callanan8ed9f512009-12-19 02:59:52 +0000343 o2.indent(i2) << "{ /* struct ModRMDecision */" << "\n";
344 i2++;
Craig Topperce8f4c52012-02-09 07:45:30 +0000345
Sean Callanan8ed9f512009-12-19 02:59:52 +0000346 o2.indent(i2) << stringForDecisionType(dt) << "," << "\n";
Craig Topperce8f4c52012-02-09 07:45:30 +0000347 o2.indent(i2) << sEntryNumber << " /* Table" << sTableNumber << " */\n";
348
Sean Callanan8ed9f512009-12-19 02:59:52 +0000349 i2--;
350 o2.indent(i2) << "}";
Craig Topperce8f4c52012-02-09 07:45:30 +0000351
352 switch (dt) {
353 default:
354 llvm_unreachable("Unknown decision type");
355 case MODRM_ONEENTRY:
356 sEntryNumber += 1;
357 break;
358 case MODRM_SPLITRM:
359 sEntryNumber += 2;
360 break;
Craig Topperf41ab772012-02-09 08:58:07 +0000361 case MODRM_SPLITREG:
362 sEntryNumber += 16;
363 break;
Craig Topperce8f4c52012-02-09 07:45:30 +0000364 case MODRM_FULL:
365 sEntryNumber += 256;
366 break;
367 }
368
Craig Topper9e6dc8b2012-09-11 04:19:21 +0000369 // We assume that the index can fit into uint16_t.
370 assert(sEntryNumber < 65536U &&
371 "Index into ModRMDecision is too large for uint16_t!");
372
Sean Callanan8ed9f512009-12-19 02:59:52 +0000373 ++sTableNumber;
374}
375
Craig Toppere08789f2012-07-31 05:42:02 +0000376void DisassemblerTables::emitOpcodeDecision(raw_ostream &o1, raw_ostream &o2,
Craig Topperde9e3332012-07-31 06:02:05 +0000377 unsigned &i1, unsigned &i2,
Craig Toppere08789f2012-07-31 05:42:02 +0000378 OpcodeDecision &decision) const {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000379 o2.indent(i2) << "{ /* struct OpcodeDecision */" << "\n";
380 i2++;
381 o2.indent(i2) << "{" << "\n";
382 i2++;
383
Craig Topperde9e3332012-07-31 06:02:05 +0000384 for (unsigned index = 0; index < 256; ++index) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000385 o2.indent(i2);
386
387 o2 << "/* 0x" << format("%02hhx", index) << " */" << "\n";
388
389 emitModRMDecision(o1, o2, i1, i2, decision.modRMDecisions[index]);
390
391 if (index < 255)
392 o2 << ",";
393
394 o2 << "\n";
395 }
396
397 i2--;
398 o2.indent(i2) << "}" << "\n";
399 i2--;
400 o2.indent(i2) << "}" << "\n";
401}
402
Craig Toppere08789f2012-07-31 05:42:02 +0000403void DisassemblerTables::emitContextDecision(raw_ostream &o1, raw_ostream &o2,
Craig Topperde9e3332012-07-31 06:02:05 +0000404 unsigned &i1, unsigned &i2,
Craig Toppere08789f2012-07-31 05:42:02 +0000405 ContextDecision &decision,
406 const char* name) const {
Benjamin Kramer4d1dca92010-10-23 09:10:44 +0000407 o2.indent(i2) << "static const struct ContextDecision " << name << " = {\n";
Sean Callanan8ed9f512009-12-19 02:59:52 +0000408 i2++;
409 o2.indent(i2) << "{ /* opcodeDecisions */" << "\n";
410 i2++;
411
Craig Topperde9e3332012-07-31 06:02:05 +0000412 for (unsigned index = 0; index < IC_max; ++index) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000413 o2.indent(i2) << "/* ";
414 o2 << stringForContext((InstructionContext)index);
415 o2 << " */";
416 o2 << "\n";
417
418 emitOpcodeDecision(o1, o2, i1, i2, decision.opcodeDecisions[index]);
419
420 if (index + 1 < IC_max)
421 o2 << ", ";
422 }
423
424 i2--;
425 o2.indent(i2) << "}" << "\n";
426 i2--;
427 o2.indent(i2) << "};" << "\n";
428}
429
Craig Topperde9e3332012-07-31 06:02:05 +0000430void DisassemblerTables::emitInstructionInfo(raw_ostream &o,
431 unsigned &i) const {
Craig Topper5a2c6072012-08-01 07:39:18 +0000432 unsigned NumInstructions = InstructionSpecifiers.size();
433
434 o << "static const struct OperandSpecifier x86OperandSets[]["
435 << X86_MAX_OPERANDS << "] = {\n";
436
437 typedef std::vector<std::pair<const char *, const char *> > OperandListTy;
438 std::map<OperandListTy, unsigned> OperandSets;
439
440 unsigned OperandSetNum = 0;
441 for (unsigned Index = 0; Index < NumInstructions; ++Index) {
442 OperandListTy OperandList;
443
444 for (unsigned OperandIndex = 0; OperandIndex < X86_MAX_OPERANDS;
445 ++OperandIndex) {
446 const char *Encoding =
447 stringForOperandEncoding((OperandEncoding)InstructionSpecifiers[Index]
448 .operands[OperandIndex].encoding);
449 const char *Type =
450 stringForOperandType((OperandType)InstructionSpecifiers[Index]
451 .operands[OperandIndex].type);
452 OperandList.push_back(std::make_pair(Encoding, Type));
453 }
454 unsigned &N = OperandSets[OperandList];
455 if (N != 0) continue;
456
457 N = ++OperandSetNum;
458
459 o << " { /* " << (OperandSetNum - 1) << " */\n";
460 for (unsigned i = 0, e = OperandList.size(); i != e; ++i) {
461 o << " { " << OperandList[i].first << ", "
462 << OperandList[i].second << " },\n";
463 }
464 o << " },\n";
465 }
466 o << "};" << "\n\n";
467
Benjamin Kramer4d1dca92010-10-23 09:10:44 +0000468 o.indent(i * 2) << "static const struct InstructionSpecifier ";
469 o << INSTRUCTIONS_STR "[" << InstructionSpecifiers.size() << "] = {\n";
Craig Topper98400092012-07-31 05:27:01 +0000470
Sean Callanan8ed9f512009-12-19 02:59:52 +0000471 i++;
472
Craig Topper5a2c6072012-08-01 07:39:18 +0000473 for (unsigned index = 0; index < NumInstructions; ++index) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000474 o.indent(i * 2) << "{ /* " << index << " */" << "\n";
475 i++;
Craig Topper991271d2012-03-04 02:16:41 +0000476
477 o.indent(i * 2) << stringForModifierType(
478 (ModifierType)InstructionSpecifiers[index].modifierType);
Craig Topper5a2c6072012-08-01 07:39:18 +0000479 o << ",\n";
Craig Topper991271d2012-03-04 02:16:41 +0000480
Sean Callanan8ed9f512009-12-19 02:59:52 +0000481 o.indent(i * 2) << "0x";
482 o << format("%02hhx", (uint16_t)InstructionSpecifiers[index].modifierBase);
Craig Topper5a2c6072012-08-01 07:39:18 +0000483 o << ",\n";
Sean Callanan8ed9f512009-12-19 02:59:52 +0000484
Craig Topper5a2c6072012-08-01 07:39:18 +0000485 OperandListTy OperandList;
486 for (unsigned OperandIndex = 0; OperandIndex < X86_MAX_OPERANDS;
487 ++OperandIndex) {
488 const char *Encoding =
489 stringForOperandEncoding((OperandEncoding)InstructionSpecifiers[index]
490 .operands[OperandIndex].encoding);
491 const char *Type =
492 stringForOperandType((OperandType)InstructionSpecifiers[index]
493 .operands[OperandIndex].type);
494 OperandList.push_back(std::make_pair(Encoding, Type));
Sean Callanan8ed9f512009-12-19 02:59:52 +0000495 }
Craig Topper5a2c6072012-08-01 07:39:18 +0000496 o.indent(i * 2) << (OperandSets[OperandList] - 1) << ",\n";
Craig Topper98400092012-07-31 05:27:01 +0000497
Benjamin Kramer953362c2012-02-11 14:50:54 +0000498 o.indent(i * 2) << "/* " << InstructionSpecifiers[index].name << " */";
Sean Callanan8ed9f512009-12-19 02:59:52 +0000499 o << "\n";
500
501 i--;
502 o.indent(i * 2) << "}";
503
Craig Topper5a2c6072012-08-01 07:39:18 +0000504 if (index + 1 < NumInstructions)
Sean Callanan8ed9f512009-12-19 02:59:52 +0000505 o << ",";
506
507 o << "\n";
508 }
509
510 i--;
511 o.indent(i * 2) << "};" << "\n";
512}
513
Craig Topperde9e3332012-07-31 06:02:05 +0000514void DisassemblerTables::emitContextTable(raw_ostream &o, unsigned &i) const {
Craig Topper19dbe2f2012-07-31 06:15:39 +0000515 o.indent(i * 2) << "static const uint8_t " CONTEXTS_STR
Benjamin Kramer86c69c52010-10-23 09:28:42 +0000516 "[256] = {\n";
Sean Callanan8ed9f512009-12-19 02:59:52 +0000517 i++;
518
Craig Topperde9e3332012-07-31 06:02:05 +0000519 for (unsigned index = 0; index < 256; ++index) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000520 o.indent(i * 2);
521
Craig Topperc8eb8802011-11-06 23:04:08 +0000522 if ((index & ATTR_VEXL) && (index & ATTR_REXW) && (index & ATTR_OPSIZE))
523 o << "IC_VEX_L_W_OPSIZE";
524 else if ((index & ATTR_VEXL) && (index & ATTR_OPSIZE))
Sean Callanana21e2ea2011-03-15 01:23:15 +0000525 o << "IC_VEX_L_OPSIZE";
526 else if ((index & ATTR_VEXL) && (index & ATTR_XD))
527 o << "IC_VEX_L_XD";
528 else if ((index & ATTR_VEXL) && (index & ATTR_XS))
529 o << "IC_VEX_L_XS";
530 else if ((index & ATTR_VEX) && (index & ATTR_REXW) && (index & ATTR_OPSIZE))
531 o << "IC_VEX_W_OPSIZE";
532 else if ((index & ATTR_VEX) && (index & ATTR_REXW) && (index & ATTR_XD))
533 o << "IC_VEX_W_XD";
534 else if ((index & ATTR_VEX) && (index & ATTR_REXW) && (index & ATTR_XS))
535 o << "IC_VEX_W_XS";
536 else if (index & ATTR_VEXL)
537 o << "IC_VEX_L";
538 else if ((index & ATTR_VEX) && (index & ATTR_REXW))
539 o << "IC_VEX_W";
540 else if ((index & ATTR_VEX) && (index & ATTR_OPSIZE))
541 o << "IC_VEX_OPSIZE";
542 else if ((index & ATTR_VEX) && (index & ATTR_XD))
543 o << "IC_VEX_XD";
544 else if ((index & ATTR_VEX) && (index & ATTR_XS))
545 o << "IC_VEX_XS";
Craig Topper113061d2011-08-25 07:42:00 +0000546 else if (index & ATTR_VEX)
547 o << "IC_VEX";
Sean Callanana21e2ea2011-03-15 01:23:15 +0000548 else if ((index & ATTR_64BIT) && (index & ATTR_REXW) && (index & ATTR_XS))
Sean Callanan8ed9f512009-12-19 02:59:52 +0000549 o << "IC_64BIT_REXW_XS";
550 else if ((index & ATTR_64BIT) && (index & ATTR_REXW) && (index & ATTR_XD))
551 o << "IC_64BIT_REXW_XD";
Craig Topper98400092012-07-31 05:27:01 +0000552 else if ((index & ATTR_64BIT) && (index & ATTR_REXW) &&
Sean Callanan8ed9f512009-12-19 02:59:52 +0000553 (index & ATTR_OPSIZE))
554 o << "IC_64BIT_REXW_OPSIZE";
Craig Toppere1b4a1a2011-10-01 19:54:56 +0000555 else if ((index & ATTR_64BIT) && (index & ATTR_XD) && (index & ATTR_OPSIZE))
556 o << "IC_64BIT_XD_OPSIZE";
Craig Topper29480fd2011-10-11 04:34:23 +0000557 else if ((index & ATTR_64BIT) && (index & ATTR_XS) && (index & ATTR_OPSIZE))
558 o << "IC_64BIT_XS_OPSIZE";
Sean Callanan8ed9f512009-12-19 02:59:52 +0000559 else if ((index & ATTR_64BIT) && (index & ATTR_XS))
560 o << "IC_64BIT_XS";
561 else if ((index & ATTR_64BIT) && (index & ATTR_XD))
562 o << "IC_64BIT_XD";
563 else if ((index & ATTR_64BIT) && (index & ATTR_OPSIZE))
564 o << "IC_64BIT_OPSIZE";
Craig Topper930a1eb2012-02-27 01:54:29 +0000565 else if ((index & ATTR_64BIT) && (index & ATTR_ADSIZE))
566 o << "IC_64BIT_ADSIZE";
Sean Callanan8ed9f512009-12-19 02:59:52 +0000567 else if ((index & ATTR_64BIT) && (index & ATTR_REXW))
568 o << "IC_64BIT_REXW";
569 else if ((index & ATTR_64BIT))
570 o << "IC_64BIT";
Craig Topper29480fd2011-10-11 04:34:23 +0000571 else if ((index & ATTR_XS) && (index & ATTR_OPSIZE))
572 o << "IC_XS_OPSIZE";
Craig Toppere1b4a1a2011-10-01 19:54:56 +0000573 else if ((index & ATTR_XD) && (index & ATTR_OPSIZE))
574 o << "IC_XD_OPSIZE";
Sean Callanan8ed9f512009-12-19 02:59:52 +0000575 else if (index & ATTR_XS)
576 o << "IC_XS";
577 else if (index & ATTR_XD)
578 o << "IC_XD";
579 else if (index & ATTR_OPSIZE)
580 o << "IC_OPSIZE";
Craig Topper930a1eb2012-02-27 01:54:29 +0000581 else if (index & ATTR_ADSIZE)
582 o << "IC_ADSIZE";
Sean Callanan8ed9f512009-12-19 02:59:52 +0000583 else
584 o << "IC";
585
586 if (index < 255)
587 o << ",";
588 else
589 o << " ";
590
591 o << " /* " << index << " */";
592
593 o << "\n";
594 }
595
596 i--;
597 o.indent(i * 2) << "};" << "\n";
598}
599
Craig Toppere08789f2012-07-31 05:42:02 +0000600void DisassemblerTables::emitContextDecisions(raw_ostream &o1, raw_ostream &o2,
Craig Topperde9e3332012-07-31 06:02:05 +0000601 unsigned &i1, unsigned &i2) const {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000602 emitContextDecision(o1, o2, i1, i2, *Tables[0], ONEBYTE_STR);
603 emitContextDecision(o1, o2, i1, i2, *Tables[1], TWOBYTE_STR);
604 emitContextDecision(o1, o2, i1, i2, *Tables[2], THREEBYTE38_STR);
605 emitContextDecision(o1, o2, i1, i2, *Tables[3], THREEBYTE3A_STR);
Joerg Sonnenberger4a8ac8d2011-04-04 16:58:13 +0000606 emitContextDecision(o1, o2, i1, i2, *Tables[4], THREEBYTEA6_STR);
607 emitContextDecision(o1, o2, i1, i2, *Tables[5], THREEBYTEA7_STR);
Sean Callanan8ed9f512009-12-19 02:59:52 +0000608}
609
610void DisassemblerTables::emit(raw_ostream &o) const {
Craig Topperde9e3332012-07-31 06:02:05 +0000611 unsigned i1 = 0;
612 unsigned i2 = 0;
Craig Topper98400092012-07-31 05:27:01 +0000613
Sean Callanan8ed9f512009-12-19 02:59:52 +0000614 std::string s1;
615 std::string s2;
Craig Topper98400092012-07-31 05:27:01 +0000616
Sean Callanan8ed9f512009-12-19 02:59:52 +0000617 raw_string_ostream o1(s1);
618 raw_string_ostream o2(s2);
Craig Topper98400092012-07-31 05:27:01 +0000619
Sean Callanan8ed9f512009-12-19 02:59:52 +0000620 emitInstructionInfo(o, i2);
621 o << "\n";
622
623 emitContextTable(o, i2);
624 o << "\n";
Craig Topperce8f4c52012-02-09 07:45:30 +0000625
626 o << "static const InstrUID modRMTable[] = {\n";
627 i1++;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000628 emitEmptyTable(o1, i1);
Craig Topperce8f4c52012-02-09 07:45:30 +0000629 i1--;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000630 emitContextDecisions(o1, o2, i1, i2);
Craig Topperce8f4c52012-02-09 07:45:30 +0000631
Sean Callanan8ed9f512009-12-19 02:59:52 +0000632 o << o1.str();
Craig Topperce8f4c52012-02-09 07:45:30 +0000633 o << " 0x0\n";
634 o << "};\n";
Sean Callanan8ed9f512009-12-19 02:59:52 +0000635 o << "\n";
636 o << o2.str();
637 o << "\n";
638 o << "\n";
639}
640
641void DisassemblerTables::setTableFields(ModRMDecision &decision,
642 const ModRMFilter &filter,
643 InstrUID uid,
644 uint8_t opcode) {
Craig Topperde9e3332012-07-31 06:02:05 +0000645 for (unsigned index = 0; index < 256; ++index) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000646 if (filter.accepts(index)) {
647 if (decision.instructionIDs[index] == uid)
648 continue;
649
650 if (decision.instructionIDs[index] != 0) {
651 InstructionSpecifier &newInfo =
652 InstructionSpecifiers[uid];
653 InstructionSpecifier &previousInfo =
654 InstructionSpecifiers[decision.instructionIDs[index]];
Craig Topper98400092012-07-31 05:27:01 +0000655
Sean Callanan8ed9f512009-12-19 02:59:52 +0000656 if(newInfo.filtered)
657 continue; // filtered instructions get lowest priority
Craig Topper98400092012-07-31 05:27:01 +0000658
Craig Topper842f58f2011-09-11 20:23:20 +0000659 if(previousInfo.name == "NOOP" && (newInfo.name == "XCHG16ar" ||
660 newInfo.name == "XCHG32ar" ||
Craig Topper25f6dfd2011-10-07 05:35:38 +0000661 newInfo.name == "XCHG32ar64" ||
Craig Topper842f58f2011-09-11 20:23:20 +0000662 newInfo.name == "XCHG64ar"))
663 continue; // special case for XCHG*ar and NOOP
Sean Callanan8ed9f512009-12-19 02:59:52 +0000664
665 if (outranks(previousInfo.insnContext, newInfo.insnContext))
666 continue;
Craig Topper98400092012-07-31 05:27:01 +0000667
Sean Callanan8ed9f512009-12-19 02:59:52 +0000668 if (previousInfo.insnContext == newInfo.insnContext &&
669 !previousInfo.filtered) {
670 errs() << "Error: Primary decode conflict: ";
671 errs() << newInfo.name << " would overwrite " << previousInfo.name;
672 errs() << "\n";
673 errs() << "ModRM " << index << "\n";
674 errs() << "Opcode " << (uint16_t)opcode << "\n";
675 errs() << "Context " << stringForContext(newInfo.insnContext) << "\n";
676 HasConflicts = true;
677 }
678 }
679
680 decision.instructionIDs[index] = uid;
681 }
682 }
683}
684
685void DisassemblerTables::setTableFields(OpcodeType type,
686 InstructionContext insnContext,
687 uint8_t opcode,
688 const ModRMFilter &filter,
Craig Topper4da632e2011-09-23 06:57:25 +0000689 InstrUID uid,
Craig Topper6744a172011-10-04 06:30:42 +0000690 bool is32bit,
691 bool ignoresVEX_L) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000692 ContextDecision &decision = *Tables[type];
693
Craig Topperde9e3332012-07-31 06:02:05 +0000694 for (unsigned index = 0; index < IC_max; ++index) {
Craig Topper4da632e2011-09-23 06:57:25 +0000695 if (is32bit && inheritsFrom((InstructionContext)index, IC_64BIT))
696 continue;
697
Craig Topper98400092012-07-31 05:27:01 +0000698 if (inheritsFrom((InstructionContext)index,
Craig Topper6744a172011-10-04 06:30:42 +0000699 InstructionSpecifiers[uid].insnContext, ignoresVEX_L))
Craig Topper98400092012-07-31 05:27:01 +0000700 setTableFields(decision.opcodeDecisions[index].modRMDecisions[opcode],
Sean Callanan8ed9f512009-12-19 02:59:52 +0000701 filter,
702 uid,
703 opcode);
704 }
705}