blob: f42119c07405a04a628bff97dd34ab6d6b388eda [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;
27
28/// 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;
39
40 switch (parent) {
41 case IC:
Craig Topper5ffedb92011-09-02 04:17:54 +000042 return(inheritsFrom(child, IC_64BIT) ||
43 inheritsFrom(child, IC_OPSIZE) ||
44 inheritsFrom(child, IC_XD) ||
45 inheritsFrom(child, IC_XS));
Sean Callanan8ed9f512009-12-19 02:59:52 +000046 case IC_64BIT:
47 return(inheritsFrom(child, IC_64BIT_REXW) ||
48 inheritsFrom(child, IC_64BIT_OPSIZE) ||
49 inheritsFrom(child, IC_64BIT_XD) ||
50 inheritsFrom(child, IC_64BIT_XS));
51 case IC_OPSIZE:
Craig Topper5ffedb92011-09-02 04:17:54 +000052 return inheritsFrom(child, IC_64BIT_OPSIZE);
Sean Callanan8ed9f512009-12-19 02:59:52 +000053 case IC_XD:
Craig Topper5ffedb92011-09-02 04:17:54 +000054 return inheritsFrom(child, IC_64BIT_XD);
Sean Callanan8ed9f512009-12-19 02:59:52 +000055 case IC_XS:
Craig Topper5ffedb92011-09-02 04:17:54 +000056 return inheritsFrom(child, IC_64BIT_XS);
Craig Toppere1b4a1a2011-10-01 19:54:56 +000057 case IC_XD_OPSIZE:
58 return inheritsFrom(child, IC_64BIT_XD_OPSIZE);
Sean Callanan8ed9f512009-12-19 02:59:52 +000059 case IC_64BIT_REXW:
60 return(inheritsFrom(child, IC_64BIT_REXW_XS) ||
61 inheritsFrom(child, IC_64BIT_REXW_XD) ||
62 inheritsFrom(child, IC_64BIT_REXW_OPSIZE));
63 case IC_64BIT_OPSIZE:
64 return(inheritsFrom(child, IC_64BIT_REXW_OPSIZE));
65 case IC_64BIT_XD:
66 return(inheritsFrom(child, IC_64BIT_REXW_XD));
67 case IC_64BIT_XS:
68 return(inheritsFrom(child, IC_64BIT_REXW_XS));
Craig Toppere1b4a1a2011-10-01 19:54:56 +000069 case IC_64BIT_XD_OPSIZE:
70 return false;
Sean Callanan8ed9f512009-12-19 02:59:52 +000071 case IC_64BIT_REXW_XD:
Sean Callanan8ed9f512009-12-19 02:59:52 +000072 case IC_64BIT_REXW_XS:
Sean Callanan8ed9f512009-12-19 02:59:52 +000073 case IC_64BIT_REXW_OPSIZE:
74 return false;
Sean Callanana21e2ea2011-03-15 01:23:15 +000075 case IC_VEX:
Craig Topper6744a172011-10-04 06:30:42 +000076 return inheritsFrom(child, IC_VEX_W) ||
77 (VEX_LIG && inheritsFrom(child, IC_VEX_L));
Sean Callanana21e2ea2011-03-15 01:23:15 +000078 case IC_VEX_XS:
Craig Topper6744a172011-10-04 06:30:42 +000079 return inheritsFrom(child, IC_VEX_W_XS) ||
80 (VEX_LIG && inheritsFrom(child, IC_VEX_L_XS));
Sean Callanana21e2ea2011-03-15 01:23:15 +000081 case IC_VEX_XD:
Craig Topper6744a172011-10-04 06:30:42 +000082 return inheritsFrom(child, IC_VEX_W_XD) ||
83 (VEX_LIG && inheritsFrom(child, IC_VEX_L_XD));
Craig Topper5ffedb92011-09-02 04:17:54 +000084 case IC_VEX_OPSIZE:
Craig Topper6744a172011-10-04 06:30:42 +000085 return inheritsFrom(child, IC_VEX_W_OPSIZE) ||
86 (VEX_LIG && inheritsFrom(child, IC_VEX_L_OPSIZE));
Sean Callanana21e2ea2011-03-15 01:23:15 +000087 case IC_VEX_W:
Sean Callanana21e2ea2011-03-15 01:23:15 +000088 case IC_VEX_W_XS:
Sean Callanana21e2ea2011-03-15 01:23:15 +000089 case IC_VEX_W_XD:
Craig Topper5ffedb92011-09-02 04:17:54 +000090 case IC_VEX_W_OPSIZE:
91 return false;
92 case IC_VEX_L:
Craig Topper5ffedb92011-09-02 04:17:54 +000093 case IC_VEX_L_XS:
Craig Topper5ffedb92011-09-02 04:17:54 +000094 case IC_VEX_L_XD:
Craig Topper5ffedb92011-09-02 04:17:54 +000095 case IC_VEX_L_OPSIZE:
96 return false;
Sean Callanan8ed9f512009-12-19 02:59:52 +000097 default:
Craig Topper5ffedb92011-09-02 04:17:54 +000098 llvm_unreachable("Unknown instruction class");
Sean Callanan8ed9f512009-12-19 02:59:52 +000099 return false;
100 }
101}
102
103/// outranks - Indicates whether, if an instruction has two different applicable
104/// classes, which class should be preferred when performing decode. This
105/// imposes a total ordering (ties are resolved toward "lower")
106///
107/// @param upper - The class that may be preferable
108/// @param lower - The class that may be less preferable
109/// @return - True if upper is to be preferred, false otherwise.
110static inline bool outranks(InstructionContext upper,
111 InstructionContext lower) {
112 assert(upper < IC_max);
113 assert(lower < IC_max);
114
115#define ENUM_ENTRY(n, r, d) r,
116 static int ranks[IC_max] = {
117 INSTRUCTION_CONTEXTS
118 };
119#undef ENUM_ENTRY
120
121 return (ranks[upper] > ranks[lower]);
122}
123
124/// stringForContext - Returns a string containing the name of a particular
125/// InstructionContext, usually for diagnostic purposes.
126///
127/// @param insnContext - The instruction class to transform to a string.
128/// @return - A statically-allocated string constant that contains the
129/// name of the instruction class.
130static inline const char* stringForContext(InstructionContext insnContext) {
131 switch (insnContext) {
132 default:
133 llvm_unreachable("Unhandled instruction class");
134#define ENUM_ENTRY(n, r, d) case n: return #n; break;
135 INSTRUCTION_CONTEXTS
136#undef ENUM_ENTRY
137 }
Daniel Dunbare5976b82009-12-23 00:45:10 +0000138
139 return 0;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000140}
141
142/// stringForOperandType - Like stringForContext, but for OperandTypes.
143static inline const char* stringForOperandType(OperandType type) {
144 switch (type) {
145 default:
146 llvm_unreachable("Unhandled type");
147#define ENUM_ENTRY(i, d) case i: return #i;
148 TYPES
149#undef ENUM_ENTRY
150 }
151}
152
153/// stringForOperandEncoding - like stringForContext, but for
154/// OperandEncodings.
155static inline const char* stringForOperandEncoding(OperandEncoding encoding) {
156 switch (encoding) {
157 default:
158 llvm_unreachable("Unhandled encoding");
159#define ENUM_ENTRY(i, d) case i: return #i;
160 ENCODINGS
161#undef ENUM_ENTRY
162 }
163}
164
165void DisassemblerTables::emitOneID(raw_ostream &o,
166 uint32_t &i,
167 InstrUID id,
168 bool addComma) const {
169 if (id)
170 o.indent(i * 2) << format("0x%hx", id);
171 else
172 o.indent(i * 2) << 0;
173
174 if (addComma)
175 o << ", ";
176 else
177 o << " ";
178
179 o << "/* ";
180 o << InstructionSpecifiers[id].name;
181 o << "*/";
182
183 o << "\n";
184}
185
186/// emitEmptyTable - Emits the modRMEmptyTable, which is used as a ID table by
187/// all ModR/M decisions for instructions that are invalid for all possible
188/// ModR/M byte values.
189///
190/// @param o - The output stream on which to emit the table.
191/// @param i - The indentation level for that output stream.
192static void emitEmptyTable(raw_ostream &o, uint32_t &i)
193{
Benjamin Kramer86c69c52010-10-23 09:28:42 +0000194 o.indent(i * 2) << "static const InstrUID modRMEmptyTable[1] = { 0 };\n";
Sean Callanan8ed9f512009-12-19 02:59:52 +0000195 o << "\n";
196}
197
198/// getDecisionType - Determines whether a ModRM decision with 255 entries can
199/// be compacted by eliminating redundant information.
200///
201/// @param decision - The decision to be compacted.
202/// @return - The compactest available representation for the decision.
203static ModRMDecisionType getDecisionType(ModRMDecision &decision)
204{
205 bool satisfiesOneEntry = true;
206 bool satisfiesSplitRM = true;
207
208 uint16_t index;
209
210 for (index = 0; index < 256; ++index) {
211 if (decision.instructionIDs[index] != decision.instructionIDs[0])
212 satisfiesOneEntry = false;
213
214 if (((index & 0xc0) == 0xc0) &&
215 (decision.instructionIDs[index] != decision.instructionIDs[0xc0]))
216 satisfiesSplitRM = false;
217
218 if (((index & 0xc0) != 0xc0) &&
219 (decision.instructionIDs[index] != decision.instructionIDs[0x00]))
220 satisfiesSplitRM = false;
221 }
222
223 if (satisfiesOneEntry)
224 return MODRM_ONEENTRY;
225
226 if (satisfiesSplitRM)
227 return MODRM_SPLITRM;
228
229 return MODRM_FULL;
230}
231
232/// stringForDecisionType - Returns a statically-allocated string corresponding
233/// to a particular decision type.
234///
235/// @param dt - The decision type.
236/// @return - A pointer to the statically-allocated string (e.g.,
237/// "MODRM_ONEENTRY" for MODRM_ONEENTRY).
238static const char* stringForDecisionType(ModRMDecisionType dt)
239{
240#define ENUM_ENTRY(n) case n: return #n;
241 switch (dt) {
242 default:
243 llvm_unreachable("Unknown decision type");
244 MODRMTYPES
245 };
246#undef ENUM_ENTRY
247}
248
249/// stringForModifierType - Returns a statically-allocated string corresponding
250/// to an opcode modifier type.
251///
252/// @param mt - The modifier type.
253/// @return - A pointer to the statically-allocated string (e.g.,
254/// "MODIFIER_NONE" for MODIFIER_NONE).
255static const char* stringForModifierType(ModifierType mt)
256{
257#define ENUM_ENTRY(n) case n: return #n;
258 switch(mt) {
259 default:
260 llvm_unreachable("Unknown modifier type");
261 MODIFIER_TYPES
262 };
263#undef ENUM_ENTRY
264}
265
266DisassemblerTables::DisassemblerTables() {
267 unsigned i;
268
Joerg Sonnenberger39d7cae2011-04-04 16:25:38 +0000269 for (i = 0; i < array_lengthof(Tables); i++) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000270 Tables[i] = new ContextDecision;
Daniel Dunbar87830872009-12-19 04:16:57 +0000271 memset(Tables[i], 0, sizeof(ContextDecision));
Sean Callanan8ed9f512009-12-19 02:59:52 +0000272 }
273
274 HasConflicts = false;
275}
276
277DisassemblerTables::~DisassemblerTables() {
278 unsigned i;
279
Joerg Sonnenberger39d7cae2011-04-04 16:25:38 +0000280 for (i = 0; i < array_lengthof(Tables); i++)
Sean Callanan8ed9f512009-12-19 02:59:52 +0000281 delete Tables[i];
282}
283
284void DisassemblerTables::emitModRMDecision(raw_ostream &o1,
285 raw_ostream &o2,
286 uint32_t &i1,
287 uint32_t &i2,
288 ModRMDecision &decision)
289 const {
290 static uint64_t sTableNumber = 0;
291 uint64_t thisTableNumber = sTableNumber;
292 ModRMDecisionType dt = getDecisionType(decision);
293 uint16_t index;
294
295 if (dt == MODRM_ONEENTRY && decision.instructionIDs[0] == 0)
296 {
297 o2.indent(i2) << "{ /* ModRMDecision */" << "\n";
298 i2++;
299
300 o2.indent(i2) << stringForDecisionType(dt) << "," << "\n";
301 o2.indent(i2) << "modRMEmptyTable";
302
303 i2--;
304 o2.indent(i2) << "}";
305 return;
306 }
307
Benjamin Kramer4d1dca92010-10-23 09:10:44 +0000308 o1.indent(i1) << "static const InstrUID modRMTable" << thisTableNumber;
Sean Callanan8ed9f512009-12-19 02:59:52 +0000309
310 switch (dt) {
311 default:
312 llvm_unreachable("Unknown decision type");
313 case MODRM_ONEENTRY:
314 o1 << "[1]";
315 break;
316 case MODRM_SPLITRM:
317 o1 << "[2]";
318 break;
319 case MODRM_FULL:
320 o1 << "[256]";
321 break;
322 }
323
324 o1 << " = {" << "\n";
325 i1++;
326
327 switch (dt) {
328 default:
329 llvm_unreachable("Unknown decision type");
330 case MODRM_ONEENTRY:
331 emitOneID(o1, i1, decision.instructionIDs[0], false);
332 break;
333 case MODRM_SPLITRM:
334 emitOneID(o1, i1, decision.instructionIDs[0x00], true); // mod = 0b00
335 emitOneID(o1, i1, decision.instructionIDs[0xc0], false); // mod = 0b11
336 break;
337 case MODRM_FULL:
338 for (index = 0; index < 256; ++index)
339 emitOneID(o1, i1, decision.instructionIDs[index], index < 255);
340 break;
341 }
342
343 i1--;
344 o1.indent(i1) << "};" << "\n";
345 o1 << "\n";
346
347 o2.indent(i2) << "{ /* struct ModRMDecision */" << "\n";
348 i2++;
349
350 o2.indent(i2) << stringForDecisionType(dt) << "," << "\n";
351 o2.indent(i2) << "modRMTable" << sTableNumber << "\n";
352
353 i2--;
354 o2.indent(i2) << "}";
355
356 ++sTableNumber;
357}
358
359void DisassemblerTables::emitOpcodeDecision(
360 raw_ostream &o1,
361 raw_ostream &o2,
362 uint32_t &i1,
363 uint32_t &i2,
364 OpcodeDecision &decision) const {
365 uint16_t index;
366
367 o2.indent(i2) << "{ /* struct OpcodeDecision */" << "\n";
368 i2++;
369 o2.indent(i2) << "{" << "\n";
370 i2++;
371
372 for (index = 0; index < 256; ++index) {
373 o2.indent(i2);
374
375 o2 << "/* 0x" << format("%02hhx", index) << " */" << "\n";
376
377 emitModRMDecision(o1, o2, i1, i2, decision.modRMDecisions[index]);
378
379 if (index < 255)
380 o2 << ",";
381
382 o2 << "\n";
383 }
384
385 i2--;
386 o2.indent(i2) << "}" << "\n";
387 i2--;
388 o2.indent(i2) << "}" << "\n";
389}
390
391void DisassemblerTables::emitContextDecision(
392 raw_ostream &o1,
393 raw_ostream &o2,
394 uint32_t &i1,
395 uint32_t &i2,
396 ContextDecision &decision,
397 const char* name) const {
Benjamin Kramer4d1dca92010-10-23 09:10:44 +0000398 o2.indent(i2) << "static const struct ContextDecision " << name << " = {\n";
Sean Callanan8ed9f512009-12-19 02:59:52 +0000399 i2++;
400 o2.indent(i2) << "{ /* opcodeDecisions */" << "\n";
401 i2++;
402
403 unsigned index;
404
405 for (index = 0; index < IC_max; ++index) {
406 o2.indent(i2) << "/* ";
407 o2 << stringForContext((InstructionContext)index);
408 o2 << " */";
409 o2 << "\n";
410
411 emitOpcodeDecision(o1, o2, i1, i2, decision.opcodeDecisions[index]);
412
413 if (index + 1 < IC_max)
414 o2 << ", ";
415 }
416
417 i2--;
418 o2.indent(i2) << "}" << "\n";
419 i2--;
420 o2.indent(i2) << "};" << "\n";
421}
422
423void DisassemblerTables::emitInstructionInfo(raw_ostream &o, uint32_t &i)
424 const {
Benjamin Kramer4d1dca92010-10-23 09:10:44 +0000425 o.indent(i * 2) << "static const struct InstructionSpecifier ";
426 o << INSTRUCTIONS_STR "[" << InstructionSpecifiers.size() << "] = {\n";
Sean Callanan8ed9f512009-12-19 02:59:52 +0000427
428 i++;
429
430 uint16_t numInstructions = InstructionSpecifiers.size();
431 uint16_t index, operandIndex;
432
433 for (index = 0; index < numInstructions; ++index) {
434 o.indent(i * 2) << "{ /* " << index << " */" << "\n";
435 i++;
436
437 o.indent(i * 2) <<
438 stringForModifierType(InstructionSpecifiers[index].modifierType);
439 o << "," << "\n";
440
441 o.indent(i * 2) << "0x";
442 o << format("%02hhx", (uint16_t)InstructionSpecifiers[index].modifierBase);
443 o << "," << "\n";
444
445 o.indent(i * 2) << "{" << "\n";
446 i++;
447
448 for (operandIndex = 0; operandIndex < X86_MAX_OPERANDS; ++operandIndex) {
449 o.indent(i * 2) << "{ ";
450 o << stringForOperandEncoding(InstructionSpecifiers[index]
451 .operands[operandIndex]
452 .encoding);
453 o << ", ";
454 o << stringForOperandType(InstructionSpecifiers[index]
455 .operands[operandIndex]
456 .type);
457 o << " }";
458
459 if (operandIndex < X86_MAX_OPERANDS - 1)
460 o << ",";
461
462 o << "\n";
463 }
464
465 i--;
466 o.indent(i * 2) << "}," << "\n";
467
468 o.indent(i * 2) << "\"" << InstructionSpecifiers[index].name << "\"";
469 o << "\n";
470
471 i--;
472 o.indent(i * 2) << "}";
473
474 if (index + 1 < numInstructions)
475 o << ",";
476
477 o << "\n";
478 }
479
480 i--;
481 o.indent(i * 2) << "};" << "\n";
482}
483
484void DisassemblerTables::emitContextTable(raw_ostream &o, uint32_t &i) const {
485 uint16_t index;
486
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
491 for (index = 0; index < 256; ++index) {
492 o.indent(i * 2);
493
Sean Callanana21e2ea2011-03-15 01:23:15 +0000494 if ((index & ATTR_VEXL) && (index & ATTR_OPSIZE))
495 o << "IC_VEX_L_OPSIZE";
496 else if ((index & ATTR_VEXL) && (index & ATTR_XD))
497 o << "IC_VEX_L_XD";
498 else if ((index & ATTR_VEXL) && (index & ATTR_XS))
499 o << "IC_VEX_L_XS";
500 else if ((index & ATTR_VEX) && (index & ATTR_REXW) && (index & ATTR_OPSIZE))
501 o << "IC_VEX_W_OPSIZE";
502 else if ((index & ATTR_VEX) && (index & ATTR_REXW) && (index & ATTR_XD))
503 o << "IC_VEX_W_XD";
504 else if ((index & ATTR_VEX) && (index & ATTR_REXW) && (index & ATTR_XS))
505 o << "IC_VEX_W_XS";
506 else if (index & ATTR_VEXL)
507 o << "IC_VEX_L";
508 else if ((index & ATTR_VEX) && (index & ATTR_REXW))
509 o << "IC_VEX_W";
510 else if ((index & ATTR_VEX) && (index & ATTR_OPSIZE))
511 o << "IC_VEX_OPSIZE";
512 else if ((index & ATTR_VEX) && (index & ATTR_XD))
513 o << "IC_VEX_XD";
514 else if ((index & ATTR_VEX) && (index & ATTR_XS))
515 o << "IC_VEX_XS";
Craig Topper113061d2011-08-25 07:42:00 +0000516 else if (index & ATTR_VEX)
517 o << "IC_VEX";
Sean Callanana21e2ea2011-03-15 01:23:15 +0000518 else if ((index & ATTR_64BIT) && (index & ATTR_REXW) && (index & ATTR_XS))
Sean Callanan8ed9f512009-12-19 02:59:52 +0000519 o << "IC_64BIT_REXW_XS";
520 else if ((index & ATTR_64BIT) && (index & ATTR_REXW) && (index & ATTR_XD))
521 o << "IC_64BIT_REXW_XD";
522 else if ((index & ATTR_64BIT) && (index & ATTR_REXW) &&
523 (index & ATTR_OPSIZE))
524 o << "IC_64BIT_REXW_OPSIZE";
Craig Toppere1b4a1a2011-10-01 19:54:56 +0000525 else if ((index & ATTR_64BIT) && (index & ATTR_XD) && (index & ATTR_OPSIZE))
526 o << "IC_64BIT_XD_OPSIZE";
Sean Callanan8ed9f512009-12-19 02:59:52 +0000527 else if ((index & ATTR_64BIT) && (index & ATTR_XS))
528 o << "IC_64BIT_XS";
529 else if ((index & ATTR_64BIT) && (index & ATTR_XD))
530 o << "IC_64BIT_XD";
531 else if ((index & ATTR_64BIT) && (index & ATTR_OPSIZE))
532 o << "IC_64BIT_OPSIZE";
533 else if ((index & ATTR_64BIT) && (index & ATTR_REXW))
534 o << "IC_64BIT_REXW";
535 else if ((index & ATTR_64BIT))
536 o << "IC_64BIT";
Craig Toppere1b4a1a2011-10-01 19:54:56 +0000537 else if ((index & ATTR_XD) && (index & ATTR_OPSIZE))
538 o << "IC_XD_OPSIZE";
Sean Callanan8ed9f512009-12-19 02:59:52 +0000539 else if (index & ATTR_XS)
540 o << "IC_XS";
541 else if (index & ATTR_XD)
542 o << "IC_XD";
543 else if (index & ATTR_OPSIZE)
544 o << "IC_OPSIZE";
Sean Callanan8ed9f512009-12-19 02:59:52 +0000545 else
546 o << "IC";
547
548 if (index < 255)
549 o << ",";
550 else
551 o << " ";
552
553 o << " /* " << index << " */";
554
555 o << "\n";
556 }
557
558 i--;
559 o.indent(i * 2) << "};" << "\n";
560}
561
562void DisassemblerTables::emitContextDecisions(raw_ostream &o1,
563 raw_ostream &o2,
564 uint32_t &i1,
565 uint32_t &i2)
566 const {
567 emitContextDecision(o1, o2, i1, i2, *Tables[0], ONEBYTE_STR);
568 emitContextDecision(o1, o2, i1, i2, *Tables[1], TWOBYTE_STR);
569 emitContextDecision(o1, o2, i1, i2, *Tables[2], THREEBYTE38_STR);
570 emitContextDecision(o1, o2, i1, i2, *Tables[3], THREEBYTE3A_STR);
Joerg Sonnenberger4a8ac8d2011-04-04 16:58:13 +0000571 emitContextDecision(o1, o2, i1, i2, *Tables[4], THREEBYTEA6_STR);
572 emitContextDecision(o1, o2, i1, i2, *Tables[5], THREEBYTEA7_STR);
Sean Callanan8ed9f512009-12-19 02:59:52 +0000573}
574
575void DisassemblerTables::emit(raw_ostream &o) const {
576 uint32_t i1 = 0;
577 uint32_t i2 = 0;
578
579 std::string s1;
580 std::string s2;
581
582 raw_string_ostream o1(s1);
583 raw_string_ostream o2(s2);
584
585 emitInstructionInfo(o, i2);
586 o << "\n";
587
588 emitContextTable(o, i2);
589 o << "\n";
590
591 emitEmptyTable(o1, i1);
592 emitContextDecisions(o1, o2, i1, i2);
593
594 o << o1.str();
595 o << "\n";
596 o << o2.str();
597 o << "\n";
598 o << "\n";
599}
600
601void DisassemblerTables::setTableFields(ModRMDecision &decision,
602 const ModRMFilter &filter,
603 InstrUID uid,
604 uint8_t opcode) {
605 unsigned index;
606
607 for (index = 0; index < 256; ++index) {
608 if (filter.accepts(index)) {
609 if (decision.instructionIDs[index] == uid)
610 continue;
611
612 if (decision.instructionIDs[index] != 0) {
613 InstructionSpecifier &newInfo =
614 InstructionSpecifiers[uid];
615 InstructionSpecifier &previousInfo =
616 InstructionSpecifiers[decision.instructionIDs[index]];
617
618 if(newInfo.filtered)
619 continue; // filtered instructions get lowest priority
620
Craig Topper842f58f2011-09-11 20:23:20 +0000621 if(previousInfo.name == "NOOP" && (newInfo.name == "XCHG16ar" ||
622 newInfo.name == "XCHG32ar" ||
Craig Topper25f6dfd2011-10-07 05:35:38 +0000623 newInfo.name == "XCHG32ar64" ||
Craig Topper842f58f2011-09-11 20:23:20 +0000624 newInfo.name == "XCHG64ar"))
625 continue; // special case for XCHG*ar and NOOP
Sean Callanan8ed9f512009-12-19 02:59:52 +0000626
627 if (outranks(previousInfo.insnContext, newInfo.insnContext))
628 continue;
629
630 if (previousInfo.insnContext == newInfo.insnContext &&
631 !previousInfo.filtered) {
632 errs() << "Error: Primary decode conflict: ";
633 errs() << newInfo.name << " would overwrite " << previousInfo.name;
634 errs() << "\n";
635 errs() << "ModRM " << index << "\n";
636 errs() << "Opcode " << (uint16_t)opcode << "\n";
637 errs() << "Context " << stringForContext(newInfo.insnContext) << "\n";
638 HasConflicts = true;
639 }
640 }
641
642 decision.instructionIDs[index] = uid;
643 }
644 }
645}
646
647void DisassemblerTables::setTableFields(OpcodeType type,
648 InstructionContext insnContext,
649 uint8_t opcode,
650 const ModRMFilter &filter,
Craig Topper4da632e2011-09-23 06:57:25 +0000651 InstrUID uid,
Craig Topper6744a172011-10-04 06:30:42 +0000652 bool is32bit,
653 bool ignoresVEX_L) {
Sean Callanan8ed9f512009-12-19 02:59:52 +0000654 unsigned index;
655
656 ContextDecision &decision = *Tables[type];
657
658 for (index = 0; index < IC_max; ++index) {
Craig Topper4da632e2011-09-23 06:57:25 +0000659 if (is32bit && inheritsFrom((InstructionContext)index, IC_64BIT))
660 continue;
661
Sean Callanan8ed9f512009-12-19 02:59:52 +0000662 if (inheritsFrom((InstructionContext)index,
Craig Topper6744a172011-10-04 06:30:42 +0000663 InstructionSpecifiers[uid].insnContext, ignoresVEX_L))
Sean Callanan8ed9f512009-12-19 02:59:52 +0000664 setTableFields(decision.opcodeDecisions[index].modRMDecisions[opcode],
665 filter,
666 uid,
667 opcode);
668 }
669}