1 //===- X86DisassemblerTables.cpp - Disassembler tables ----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
15 //===----------------------------------------------------------------------===//
17 #include "X86DisassemblerShared.h"
18 #include "X86DisassemblerTables.h"
20 #include "llvm/TableGen/TableGenBackend.h"
21 #include "llvm/ADT/STLExtras.h"
22 #include "llvm/Support/ErrorHandling.h"
23 #include "llvm/Support/Format.h"
27 using namespace X86Disassembler;
29 /// inheritsFrom - Indicates whether all instructions in one class also belong
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.
35 static inline bool inheritsFrom(InstructionContext child,
36 InstructionContext parent,
37 bool VEX_LIG = false) {
43 return(inheritsFrom(child, IC_64BIT) ||
44 inheritsFrom(child, IC_OPSIZE) ||
45 inheritsFrom(child, IC_ADSIZE) ||
46 inheritsFrom(child, IC_XD) ||
47 inheritsFrom(child, IC_XS));
49 return(inheritsFrom(child, IC_64BIT_REXW) ||
50 inheritsFrom(child, IC_64BIT_OPSIZE) ||
51 inheritsFrom(child, IC_64BIT_ADSIZE) ||
52 inheritsFrom(child, IC_64BIT_XD) ||
53 inheritsFrom(child, IC_64BIT_XS));
55 return inheritsFrom(child, IC_64BIT_OPSIZE);
60 return inheritsFrom(child, IC_64BIT_XD);
62 return inheritsFrom(child, IC_64BIT_XS);
64 return inheritsFrom(child, IC_64BIT_XD_OPSIZE);
66 return inheritsFrom(child, IC_64BIT_XS_OPSIZE);
68 return(inheritsFrom(child, IC_64BIT_REXW_XS) ||
69 inheritsFrom(child, IC_64BIT_REXW_XD) ||
70 inheritsFrom(child, IC_64BIT_REXW_OPSIZE));
72 return(inheritsFrom(child, IC_64BIT_REXW_OPSIZE));
74 return(inheritsFrom(child, IC_64BIT_REXW_XD));
76 return(inheritsFrom(child, IC_64BIT_REXW_XS));
77 case IC_64BIT_XD_OPSIZE:
78 case IC_64BIT_XS_OPSIZE:
80 case IC_64BIT_REXW_XD:
81 case IC_64BIT_REXW_XS:
82 case IC_64BIT_REXW_OPSIZE:
85 return inheritsFrom(child, IC_VEX_W) ||
86 (VEX_LIG && inheritsFrom(child, IC_VEX_L));
88 return inheritsFrom(child, IC_VEX_W_XS) ||
89 (VEX_LIG && inheritsFrom(child, IC_VEX_L_XS));
91 return inheritsFrom(child, IC_VEX_W_XD) ||
92 (VEX_LIG && inheritsFrom(child, IC_VEX_L_XD));
94 return inheritsFrom(child, IC_VEX_W_OPSIZE) ||
95 (VEX_LIG && inheritsFrom(child, IC_VEX_L_OPSIZE));
105 case IC_VEX_L_OPSIZE:
106 return inheritsFrom(child, IC_VEX_L_W_OPSIZE);
107 case IC_VEX_L_W_OPSIZE:
110 llvm_unreachable("Unknown instruction class");
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")
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.
121 static inline bool outranks(InstructionContext upper,
122 InstructionContext lower) {
123 assert(upper < IC_max);
124 assert(lower < IC_max);
126 #define ENUM_ENTRY(n, r, d) r,
127 static int ranks[IC_max] = {
132 return (ranks[upper] > ranks[lower]);
135 /// stringForContext - Returns a string containing the name of a particular
136 /// InstructionContext, usually for diagnostic purposes.
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.
141 static inline const char* stringForContext(InstructionContext insnContext) {
142 switch (insnContext) {
144 llvm_unreachable("Unhandled instruction class");
145 #define ENUM_ENTRY(n, r, d) case n: return #n; break;
151 /// stringForOperandType - Like stringForContext, but for OperandTypes.
152 static inline const char* stringForOperandType(OperandType type) {
155 llvm_unreachable("Unhandled type");
156 #define ENUM_ENTRY(i, d) case i: return #i;
162 /// stringForOperandEncoding - like stringForContext, but for
163 /// OperandEncodings.
164 static inline const char* stringForOperandEncoding(OperandEncoding encoding) {
167 llvm_unreachable("Unhandled encoding");
168 #define ENUM_ENTRY(i, d) case i: return #i;
174 void DisassemblerTables::emitOneID(raw_ostream &o, unsigned &i, InstrUID id,
175 bool addComma) const {
177 o.indent(i * 2) << format("0x%hx", id);
179 o.indent(i * 2) << 0;
187 o << InstructionSpecifiers[id].name;
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.
197 /// @param o - The output stream on which to emit the table.
198 /// @param i - The indentation level for that output stream.
199 static void emitEmptyTable(raw_ostream &o, unsigned &i) {
200 o.indent(i * 2) << "0x0, /* EmptyTable */\n";
203 /// getDecisionType - Determines whether a ModRM decision with 255 entries can
204 /// be compacted by eliminating redundant information.
206 /// @param decision - The decision to be compacted.
207 /// @return - The compactest available representation for the decision.
208 static ModRMDecisionType getDecisionType(ModRMDecision &decision) {
209 bool satisfiesOneEntry = true;
210 bool satisfiesSplitRM = true;
211 bool satisfiesSplitReg = true;
212 bool satisfiesSplitMisc = true;
214 for (unsigned index = 0; index < 256; ++index) {
215 if (decision.instructionIDs[index] != decision.instructionIDs[0])
216 satisfiesOneEntry = false;
218 if (((index & 0xc0) == 0xc0) &&
219 (decision.instructionIDs[index] != decision.instructionIDs[0xc0]))
220 satisfiesSplitRM = false;
222 if (((index & 0xc0) != 0xc0) &&
223 (decision.instructionIDs[index] != decision.instructionIDs[0x00]))
224 satisfiesSplitRM = false;
226 if (((index & 0xc0) == 0xc0) &&
227 (decision.instructionIDs[index] != decision.instructionIDs[index&0xf8]))
228 satisfiesSplitReg = false;
230 if (((index & 0xc0) != 0xc0) &&
231 (decision.instructionIDs[index] != decision.instructionIDs[index&0x38]))
232 satisfiesSplitMisc = false;
235 if (satisfiesOneEntry)
236 return MODRM_ONEENTRY;
238 if (satisfiesSplitRM)
239 return MODRM_SPLITRM;
241 if (satisfiesSplitReg && satisfiesSplitMisc)
242 return MODRM_SPLITREG;
244 if (satisfiesSplitMisc)
245 return MODRM_SPLITMISC;
250 /// stringForDecisionType - Returns a statically-allocated string corresponding
251 /// to a particular decision type.
253 /// @param dt - The decision type.
254 /// @return - A pointer to the statically-allocated string (e.g.,
255 /// "MODRM_ONEENTRY" for MODRM_ONEENTRY).
256 static const char* stringForDecisionType(ModRMDecisionType dt) {
257 #define ENUM_ENTRY(n) case n: return #n;
260 llvm_unreachable("Unknown decision type");
266 /// stringForModifierType - Returns a statically-allocated string corresponding
267 /// to an opcode modifier type.
269 /// @param mt - The modifier type.
270 /// @return - A pointer to the statically-allocated string (e.g.,
271 /// "MODIFIER_NONE" for MODIFIER_NONE).
272 static const char* stringForModifierType(ModifierType mt) {
273 #define ENUM_ENTRY(n) case n: return #n;
276 llvm_unreachable("Unknown modifier type");
282 DisassemblerTables::DisassemblerTables() {
285 for (i = 0; i < array_lengthof(Tables); i++) {
286 Tables[i] = new ContextDecision;
287 memset(Tables[i], 0, sizeof(ContextDecision));
290 HasConflicts = false;
293 DisassemblerTables::~DisassemblerTables() {
296 for (i = 0; i < array_lengthof(Tables); i++)
300 void DisassemblerTables::emitModRMDecision(raw_ostream &o1, raw_ostream &o2,
301 unsigned &i1, unsigned &i2,
302 ModRMDecision &decision) const {
303 static uint32_t sTableNumber = 0;
304 static uint32_t sEntryNumber = 1;
305 ModRMDecisionType dt = getDecisionType(decision);
307 if (dt == MODRM_ONEENTRY && decision.instructionIDs[0] == 0)
309 o2.indent(i2) << "{ /* ModRMDecision */" << "\n";
312 o2.indent(i2) << stringForDecisionType(dt) << "," << "\n";
313 o2.indent(i2) << 0 << " /* EmptyTable */\n";
316 o2.indent(i2) << "}";
320 o1 << "/* Table" << sTableNumber << " */\n";
325 llvm_unreachable("Unknown decision type");
327 emitOneID(o1, i1, decision.instructionIDs[0], true);
330 emitOneID(o1, i1, decision.instructionIDs[0x00], true); // mod = 0b00
331 emitOneID(o1, i1, decision.instructionIDs[0xc0], true); // mod = 0b11
334 for (unsigned index = 0; index < 64; index += 8)
335 emitOneID(o1, i1, decision.instructionIDs[index], true);
336 for (unsigned index = 0xc0; index < 256; index += 8)
337 emitOneID(o1, i1, decision.instructionIDs[index], true);
339 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);
346 for (unsigned index = 0; index < 256; ++index)
347 emitOneID(o1, i1, decision.instructionIDs[index], true);
353 o2.indent(i2) << "{ /* struct ModRMDecision */" << "\n";
356 o2.indent(i2) << stringForDecisionType(dt) << "," << "\n";
357 o2.indent(i2) << sEntryNumber << " /* Table" << sTableNumber << " */\n";
360 o2.indent(i2) << "}";
364 llvm_unreachable("Unknown decision type");
374 case MODRM_SPLITMISC:
375 sEntryNumber += 8 + 64;
382 // We assume that the index can fit into uint16_t.
383 assert(sEntryNumber < 65536U &&
384 "Index into ModRMDecision is too large for uint16_t!");
389 void DisassemblerTables::emitOpcodeDecision(raw_ostream &o1, raw_ostream &o2,
390 unsigned &i1, unsigned &i2,
391 OpcodeDecision &decision) const {
392 o2.indent(i2) << "{ /* struct OpcodeDecision */" << "\n";
394 o2.indent(i2) << "{" << "\n";
397 for (unsigned index = 0; index < 256; ++index) {
400 o2 << "/* 0x" << format("%02hhx", index) << " */" << "\n";
402 emitModRMDecision(o1, o2, i1, i2, decision.modRMDecisions[index]);
411 o2.indent(i2) << "}" << "\n";
413 o2.indent(i2) << "}" << "\n";
416 void DisassemblerTables::emitContextDecision(raw_ostream &o1, raw_ostream &o2,
417 unsigned &i1, unsigned &i2,
418 ContextDecision &decision,
419 const char* name) const {
420 o2.indent(i2) << "static const struct ContextDecision " << name << " = {\n";
422 o2.indent(i2) << "{ /* opcodeDecisions */" << "\n";
425 for (unsigned index = 0; index < IC_max; ++index) {
426 o2.indent(i2) << "/* ";
427 o2 << stringForContext((InstructionContext)index);
431 emitOpcodeDecision(o1, o2, i1, i2, decision.opcodeDecisions[index]);
433 if (index + 1 < IC_max)
438 o2.indent(i2) << "}" << "\n";
440 o2.indent(i2) << "};" << "\n";
443 void DisassemblerTables::emitInstructionInfo(raw_ostream &o,
445 unsigned NumInstructions = InstructionSpecifiers.size();
447 o << "static const struct OperandSpecifier x86OperandSets[]["
448 << X86_MAX_OPERANDS << "] = {\n";
450 typedef std::vector<std::pair<const char *, const char *> > OperandListTy;
451 std::map<OperandListTy, unsigned> OperandSets;
453 unsigned OperandSetNum = 0;
454 for (unsigned Index = 0; Index < NumInstructions; ++Index) {
455 OperandListTy OperandList;
457 for (unsigned OperandIndex = 0; OperandIndex < X86_MAX_OPERANDS;
459 const char *Encoding =
460 stringForOperandEncoding((OperandEncoding)InstructionSpecifiers[Index]
461 .operands[OperandIndex].encoding);
463 stringForOperandType((OperandType)InstructionSpecifiers[Index]
464 .operands[OperandIndex].type);
465 OperandList.push_back(std::make_pair(Encoding, Type));
467 unsigned &N = OperandSets[OperandList];
468 if (N != 0) continue;
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";
481 o.indent(i * 2) << "static const struct InstructionSpecifier ";
482 o << INSTRUCTIONS_STR "[" << InstructionSpecifiers.size() << "] = {\n";
486 for (unsigned index = 0; index < NumInstructions; ++index) {
487 o.indent(i * 2) << "{ /* " << index << " */" << "\n";
490 o.indent(i * 2) << stringForModifierType(
491 (ModifierType)InstructionSpecifiers[index].modifierType);
494 o.indent(i * 2) << "0x";
495 o << format("%02hhx", (uint16_t)InstructionSpecifiers[index].modifierBase);
498 OperandListTy OperandList;
499 for (unsigned OperandIndex = 0; OperandIndex < X86_MAX_OPERANDS;
501 const char *Encoding =
502 stringForOperandEncoding((OperandEncoding)InstructionSpecifiers[index]
503 .operands[OperandIndex].encoding);
505 stringForOperandType((OperandType)InstructionSpecifiers[index]
506 .operands[OperandIndex].type);
507 OperandList.push_back(std::make_pair(Encoding, Type));
509 o.indent(i * 2) << (OperandSets[OperandList] - 1) << ",\n";
511 o.indent(i * 2) << "/* " << InstructionSpecifiers[index].name << " */";
515 o.indent(i * 2) << "}";
517 if (index + 1 < NumInstructions)
524 o.indent(i * 2) << "};" << "\n";
527 void DisassemblerTables::emitContextTable(raw_ostream &o, unsigned &i) const {
528 o.indent(i * 2) << "static const uint8_t " CONTEXTS_STR
532 for (unsigned index = 0; index < 256; ++index) {
535 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))
538 o << "IC_VEX_L_OPSIZE";
539 else if ((index & ATTR_VEXL) && (index & ATTR_XD))
541 else if ((index & ATTR_VEXL) && (index & ATTR_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))
547 else if ((index & ATTR_VEX) && (index & ATTR_REXW) && (index & ATTR_XS))
549 else if (index & ATTR_VEXL)
551 else if ((index & ATTR_VEX) && (index & ATTR_REXW))
553 else if ((index & ATTR_VEX) && (index & ATTR_OPSIZE))
554 o << "IC_VEX_OPSIZE";
555 else if ((index & ATTR_VEX) && (index & ATTR_XD))
557 else if ((index & ATTR_VEX) && (index & ATTR_XS))
559 else if (index & ATTR_VEX)
561 else if ((index & ATTR_64BIT) && (index & ATTR_REXW) && (index & ATTR_XS))
562 o << "IC_64BIT_REXW_XS";
563 else if ((index & ATTR_64BIT) && (index & ATTR_REXW) && (index & ATTR_XD))
564 o << "IC_64BIT_REXW_XD";
565 else if ((index & ATTR_64BIT) && (index & ATTR_REXW) &&
566 (index & ATTR_OPSIZE))
567 o << "IC_64BIT_REXW_OPSIZE";
568 else if ((index & ATTR_64BIT) && (index & ATTR_XD) && (index & ATTR_OPSIZE))
569 o << "IC_64BIT_XD_OPSIZE";
570 else if ((index & ATTR_64BIT) && (index & ATTR_XS) && (index & ATTR_OPSIZE))
571 o << "IC_64BIT_XS_OPSIZE";
572 else if ((index & ATTR_64BIT) && (index & ATTR_XS))
574 else if ((index & ATTR_64BIT) && (index & ATTR_XD))
576 else if ((index & ATTR_64BIT) && (index & ATTR_OPSIZE))
577 o << "IC_64BIT_OPSIZE";
578 else if ((index & ATTR_64BIT) && (index & ATTR_ADSIZE))
579 o << "IC_64BIT_ADSIZE";
580 else if ((index & ATTR_64BIT) && (index & ATTR_REXW))
581 o << "IC_64BIT_REXW";
582 else if ((index & ATTR_64BIT))
584 else if ((index & ATTR_XS) && (index & ATTR_OPSIZE))
586 else if ((index & ATTR_XD) && (index & ATTR_OPSIZE))
588 else if (index & ATTR_XS)
590 else if (index & ATTR_XD)
592 else if (index & ATTR_OPSIZE)
594 else if (index & ATTR_ADSIZE)
604 o << " /* " << index << " */";
610 o.indent(i * 2) << "};" << "\n";
613 void DisassemblerTables::emitContextDecisions(raw_ostream &o1, raw_ostream &o2,
614 unsigned &i1, unsigned &i2) const {
615 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);
619 emitContextDecision(o1, o2, i1, i2, *Tables[4], THREEBYTEA6_STR);
620 emitContextDecision(o1, o2, i1, i2, *Tables[5], THREEBYTEA7_STR);
623 void DisassemblerTables::emit(raw_ostream &o) const {
630 raw_string_ostream o1(s1);
631 raw_string_ostream o2(s2);
633 emitInstructionInfo(o, i2);
636 emitContextTable(o, i2);
639 o << "static const InstrUID modRMTable[] = {\n";
641 emitEmptyTable(o1, i1);
643 emitContextDecisions(o1, o2, i1, i2);
654 void DisassemblerTables::setTableFields(ModRMDecision &decision,
655 const ModRMFilter &filter,
658 for (unsigned index = 0; index < 256; ++index) {
659 if (filter.accepts(index)) {
660 if (decision.instructionIDs[index] == uid)
663 if (decision.instructionIDs[index] != 0) {
664 InstructionSpecifier &newInfo =
665 InstructionSpecifiers[uid];
666 InstructionSpecifier &previousInfo =
667 InstructionSpecifiers[decision.instructionIDs[index]];
670 continue; // filtered instructions get lowest priority
672 if(previousInfo.name == "NOOP" && (newInfo.name == "XCHG16ar" ||
673 newInfo.name == "XCHG32ar" ||
674 newInfo.name == "XCHG32ar64" ||
675 newInfo.name == "XCHG64ar"))
676 continue; // special case for XCHG*ar and NOOP
678 if (outranks(previousInfo.insnContext, newInfo.insnContext))
681 if (previousInfo.insnContext == newInfo.insnContext &&
682 !previousInfo.filtered) {
683 errs() << "Error: Primary decode conflict: ";
684 errs() << newInfo.name << " would overwrite " << previousInfo.name;
686 errs() << "ModRM " << index << "\n";
687 errs() << "Opcode " << (uint16_t)opcode << "\n";
688 errs() << "Context " << stringForContext(newInfo.insnContext) << "\n";
693 decision.instructionIDs[index] = uid;
698 void DisassemblerTables::setTableFields(OpcodeType type,
699 InstructionContext insnContext,
701 const ModRMFilter &filter,
705 ContextDecision &decision = *Tables[type];
707 for (unsigned index = 0; index < IC_max; ++index) {
708 if (is32bit && inheritsFrom((InstructionContext)index, IC_64BIT))
711 if (inheritsFrom((InstructionContext)index,
712 InstructionSpecifiers[uid].insnContext, ignoresVEX_L))
713 setTableFields(decision.opcodeDecisions[index].modRMDecisions[opcode],