db0964f8bf602a50b1433ef8317bbcb809c84d25
[oota-llvm.git] / utils / TableGen / X86DisassemblerTables.cpp
1 //===- 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 "X86DisassemblerTables.h"
18 #include "X86DisassemblerShared.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include "llvm/Support/Format.h"
22 #include "llvm/TableGen/TableGenBackend.h"
23 #include <map>
24
25 using namespace llvm;
26 using 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.
34 static inline bool inheritsFrom(InstructionContext child,
35                                 InstructionContext parent,
36                                 bool VEX_LIG = false) {
37   if (child == parent)
38     return true;
39
40   switch (parent) {
41   case IC:
42     return(inheritsFrom(child, IC_64BIT) ||
43            inheritsFrom(child, IC_OPSIZE) ||
44            inheritsFrom(child, IC_ADSIZE) ||
45            inheritsFrom(child, IC_XD) ||
46            inheritsFrom(child, IC_XS));
47   case IC_64BIT:
48     return(inheritsFrom(child, IC_64BIT_REXW)   ||
49            inheritsFrom(child, IC_64BIT_OPSIZE) ||
50            inheritsFrom(child, IC_64BIT_ADSIZE) ||
51            inheritsFrom(child, IC_64BIT_XD)     ||
52            inheritsFrom(child, IC_64BIT_XS));
53   case IC_OPSIZE:
54     return inheritsFrom(child, IC_64BIT_OPSIZE);
55   case IC_ADSIZE:
56   case IC_64BIT_ADSIZE:
57     return false;
58   case IC_XD:
59     return inheritsFrom(child, IC_64BIT_XD);
60   case IC_XS:
61     return inheritsFrom(child, IC_64BIT_XS);
62   case IC_XD_OPSIZE:
63     return inheritsFrom(child, IC_64BIT_XD_OPSIZE);
64   case IC_XS_OPSIZE:
65     return inheritsFrom(child, IC_64BIT_XS_OPSIZE);
66   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));
76   case IC_64BIT_XD_OPSIZE:
77   case IC_64BIT_XS_OPSIZE:
78     return false;
79   case IC_64BIT_REXW_XD:
80   case IC_64BIT_REXW_XS:
81   case IC_64BIT_REXW_OPSIZE:
82     return false;
83   case IC_VEX:
84     return (VEX_LIG && inheritsFrom(child, IC_VEX_L_W)) ||
85            inheritsFrom(child, IC_VEX_W) ||
86            (VEX_LIG && inheritsFrom(child, IC_VEX_L));
87   case IC_VEX_XS:
88     return (VEX_LIG && inheritsFrom(child, IC_VEX_L_W_XS)) ||
89            inheritsFrom(child, IC_VEX_W_XS) ||
90            (VEX_LIG && inheritsFrom(child, IC_VEX_L_XS));
91   case IC_VEX_XD:
92     return (VEX_LIG && inheritsFrom(child, IC_VEX_L_W_XD)) ||
93            inheritsFrom(child, IC_VEX_W_XD) ||
94            (VEX_LIG && inheritsFrom(child, IC_VEX_L_XD));
95   case IC_VEX_OPSIZE:
96     return (VEX_LIG && inheritsFrom(child, IC_VEX_L_W_OPSIZE)) ||
97            inheritsFrom(child, IC_VEX_W_OPSIZE) ||
98            (VEX_LIG && inheritsFrom(child, IC_VEX_L_OPSIZE));
99   case IC_VEX_W:
100     return VEX_LIG && inheritsFrom(child, IC_VEX_L_W);
101   case IC_VEX_W_XS:
102     return VEX_LIG && inheritsFrom(child, IC_VEX_L_W_XS);
103   case IC_VEX_W_XD:
104     return VEX_LIG && inheritsFrom(child, IC_VEX_L_W_XD);
105   case IC_VEX_W_OPSIZE:
106     return VEX_LIG && inheritsFrom(child, IC_VEX_L_W_OPSIZE);
107   case IC_VEX_L:
108     return inheritsFrom(child, IC_VEX_L_W);
109   case IC_VEX_L_XS:
110     return inheritsFrom(child, IC_VEX_L_W_XS);
111   case IC_VEX_L_XD:
112     return inheritsFrom(child, IC_VEX_L_W_XD);
113   case IC_VEX_L_OPSIZE:
114     return inheritsFrom(child, IC_VEX_L_W_OPSIZE);
115   case IC_VEX_L_W:
116   case IC_VEX_L_W_XS:
117   case IC_VEX_L_W_XD:
118   case IC_VEX_L_W_OPSIZE:
119     return false;
120   case IC_EVEX:
121     return inheritsFrom(child, IC_EVEX_W) ||
122            inheritsFrom(child, IC_EVEX_L_W);
123   case IC_EVEX_XS:
124     return inheritsFrom(child, IC_EVEX_W_XS) ||
125            inheritsFrom(child, IC_EVEX_L_W_XS);
126   case IC_EVEX_XD:
127     return inheritsFrom(child, IC_EVEX_W_XD) ||
128            inheritsFrom(child, IC_EVEX_L_W_XD);
129   case IC_EVEX_OPSIZE:
130     return inheritsFrom(child, IC_EVEX_W_OPSIZE) ||
131            inheritsFrom(child, IC_EVEX_W_OPSIZE);
132   case IC_EVEX_W:
133   case IC_EVEX_W_XS:
134   case IC_EVEX_W_XD:
135   case IC_EVEX_W_OPSIZE:
136     return false;
137   case IC_EVEX_L:
138   case IC_EVEX_L_XS:
139   case IC_EVEX_L_XD:
140   case IC_EVEX_L_OPSIZE:
141     return false;
142   case IC_EVEX_L_W:
143   case IC_EVEX_L_W_XS:
144   case IC_EVEX_L_W_XD:
145   case IC_EVEX_L_W_OPSIZE:
146     return false;
147   case IC_EVEX_L2:
148   case IC_EVEX_L2_XS:
149   case IC_EVEX_L2_XD:
150   case IC_EVEX_L2_OPSIZE:
151     return false;
152   case IC_EVEX_L2_W:
153   case IC_EVEX_L2_W_XS:
154   case IC_EVEX_L2_W_XD:
155   case IC_EVEX_L2_W_OPSIZE:
156     return false;
157   case IC_EVEX_K:
158     return inheritsFrom(child, IC_EVEX_W_K) ||
159            inheritsFrom(child, IC_EVEX_L_W_K);
160   case IC_EVEX_XS_K:
161     return inheritsFrom(child, IC_EVEX_W_XS_K) ||
162            inheritsFrom(child, IC_EVEX_L_W_XS_K);
163   case IC_EVEX_XD_K:
164     return inheritsFrom(child, IC_EVEX_W_XD_K) ||
165            inheritsFrom(child, IC_EVEX_L_W_XD_K);
166   case IC_EVEX_OPSIZE_K:
167     return inheritsFrom(child, IC_EVEX_W_OPSIZE_K) ||
168            inheritsFrom(child, IC_EVEX_W_OPSIZE_K);
169   case IC_EVEX_W_K:
170   case IC_EVEX_W_XS_K:
171   case IC_EVEX_W_XD_K:
172   case IC_EVEX_W_OPSIZE_K:
173     return false;
174   case IC_EVEX_L_K:
175   case IC_EVEX_L_XS_K:
176   case IC_EVEX_L_XD_K:
177   case IC_EVEX_L_OPSIZE_K:
178     return false;
179   case IC_EVEX_L_W_K:
180   case IC_EVEX_L_W_XS_K:
181   case IC_EVEX_L_W_XD_K:
182   case IC_EVEX_L_W_OPSIZE_K:
183     return false;
184   case IC_EVEX_L2_K:
185   case IC_EVEX_L2_B:
186   case IC_EVEX_L2_XS_K:
187   case IC_EVEX_L2_XD_K:
188   case IC_EVEX_L2_OPSIZE_K:
189   case IC_EVEX_L2_OPSIZE_B:
190     return false;
191   case IC_EVEX_L2_W_K:
192   case IC_EVEX_L2_W_XS_K:
193   case IC_EVEX_L2_W_XD_K:
194   case IC_EVEX_L2_W_OPSIZE_K:
195   case IC_EVEX_L2_W_OPSIZE_B:
196     return false;
197   default:
198     llvm_unreachable("Unknown instruction class");
199   }
200 }
201
202 /// outranks - Indicates whether, if an instruction has two different applicable
203 ///   classes, which class should be preferred when performing decode.  This
204 ///   imposes a total ordering (ties are resolved toward "lower")
205 ///
206 /// @param upper  - The class that may be preferable
207 /// @param lower  - The class that may be less preferable
208 /// @return       - True if upper is to be preferred, false otherwise.
209 static inline bool outranks(InstructionContext upper,
210                             InstructionContext lower) {
211   assert(upper < IC_max);
212   assert(lower < IC_max);
213
214 #define ENUM_ENTRY(n, r, d) r,
215 #define ENUM_ENTRY_K_B(n, r, d) ENUM_ENTRY(n, r, d) \
216   ENUM_ENTRY(n##_K_B, r, d) ENUM_ENTRY(n##_K, r, d) ENUM_ENTRY(n##_B, r, d)
217   static int ranks[IC_max] = {
218     INSTRUCTION_CONTEXTS
219   };
220 #undef ENUM_ENTRY
221 #undef ENUM_ENTRY_K_B
222
223   return (ranks[upper] > ranks[lower]);
224 }
225
226 /// stringForContext - Returns a string containing the name of a particular
227 ///   InstructionContext, usually for diagnostic purposes.
228 ///
229 /// @param insnContext  - The instruction class to transform to a string.
230 /// @return           - A statically-allocated string constant that contains the
231 ///                     name of the instruction class.
232 static inline const char* stringForContext(InstructionContext insnContext) {
233   switch (insnContext) {
234   default:
235     llvm_unreachable("Unhandled instruction class");
236 #define ENUM_ENTRY(n, r, d)   case n: return #n; break;
237 #define ENUM_ENTRY_K_B(n, r, d) ENUM_ENTRY(n, r, d) ENUM_ENTRY(n##_K_B, r, d)\
238         ENUM_ENTRY(n##_K, r, d) ENUM_ENTRY(n##_B, r, d)
239   INSTRUCTION_CONTEXTS
240 #undef ENUM_ENTRY
241 #undef ENUM_ENTRY_K_B
242   }
243 }
244
245 /// stringForOperandType - Like stringForContext, but for OperandTypes.
246 static inline const char* stringForOperandType(OperandType type) {
247   switch (type) {
248   default:
249     llvm_unreachable("Unhandled type");
250 #define ENUM_ENTRY(i, d) case i: return #i;
251   TYPES
252 #undef ENUM_ENTRY
253   }
254 }
255
256 /// stringForOperandEncoding - like stringForContext, but for
257 ///   OperandEncodings.
258 static inline const char* stringForOperandEncoding(OperandEncoding encoding) {
259   switch (encoding) {
260   default:
261     llvm_unreachable("Unhandled encoding");
262 #define ENUM_ENTRY(i, d) case i: return #i;
263   ENCODINGS
264 #undef ENUM_ENTRY
265   }
266 }
267
268 void DisassemblerTables::emitOneID(raw_ostream &o, unsigned &i, InstrUID id,
269                                    bool addComma) const {
270   if (id)
271     o.indent(i * 2) << format("0x%hx", id);
272   else
273     o.indent(i * 2) << 0;
274
275   if (addComma)
276     o << ", ";
277   else
278     o << "  ";
279
280   o << "/* ";
281   o << InstructionSpecifiers[id].name;
282   o << "*/";
283
284   o << "\n";
285 }
286
287 /// emitEmptyTable - Emits the modRMEmptyTable, which is used as a ID table by
288 ///   all ModR/M decisions for instructions that are invalid for all possible
289 ///   ModR/M byte values.
290 ///
291 /// @param o        - The output stream on which to emit the table.
292 /// @param i        - The indentation level for that output stream.
293 static void emitEmptyTable(raw_ostream &o, unsigned &i) {
294   o.indent(i * 2) << "0x0, /* EmptyTable */\n";
295 }
296
297 /// getDecisionType - Determines whether a ModRM decision with 255 entries can
298 ///   be compacted by eliminating redundant information.
299 ///
300 /// @param decision - The decision to be compacted.
301 /// @return         - The compactest available representation for the decision.
302 static ModRMDecisionType getDecisionType(ModRMDecision &decision) {
303   bool satisfiesOneEntry = true;
304   bool satisfiesSplitRM = true;
305   bool satisfiesSplitReg = true;
306   bool satisfiesSplitMisc = true;
307
308   for (unsigned index = 0; index < 256; ++index) {
309     if (decision.instructionIDs[index] != decision.instructionIDs[0])
310       satisfiesOneEntry = false;
311
312     if (((index & 0xc0) == 0xc0) &&
313        (decision.instructionIDs[index] != decision.instructionIDs[0xc0]))
314       satisfiesSplitRM = false;
315
316     if (((index & 0xc0) != 0xc0) &&
317        (decision.instructionIDs[index] != decision.instructionIDs[0x00]))
318       satisfiesSplitRM = false;
319
320     if (((index & 0xc0) == 0xc0) &&
321        (decision.instructionIDs[index] != decision.instructionIDs[index&0xf8]))
322       satisfiesSplitReg = false;
323
324     if (((index & 0xc0) != 0xc0) &&
325        (decision.instructionIDs[index] != decision.instructionIDs[index&0x38]))
326       satisfiesSplitMisc = false;
327   }
328
329   if (satisfiesOneEntry)
330     return MODRM_ONEENTRY;
331
332   if (satisfiesSplitRM)
333     return MODRM_SPLITRM;
334
335   if (satisfiesSplitReg && satisfiesSplitMisc)
336     return MODRM_SPLITREG;
337
338   if (satisfiesSplitMisc)
339     return MODRM_SPLITMISC;
340
341   return MODRM_FULL;
342 }
343
344 /// stringForDecisionType - Returns a statically-allocated string corresponding
345 ///   to a particular decision type.
346 ///
347 /// @param dt - The decision type.
348 /// @return   - A pointer to the statically-allocated string (e.g.,
349 ///             "MODRM_ONEENTRY" for MODRM_ONEENTRY).
350 static const char* stringForDecisionType(ModRMDecisionType dt) {
351 #define ENUM_ENTRY(n) case n: return #n;
352   switch (dt) {
353     default:
354       llvm_unreachable("Unknown decision type");
355     MODRMTYPES
356   };
357 #undef ENUM_ENTRY
358 }
359
360 /// stringForModifierType - Returns a statically-allocated string corresponding
361 ///   to an opcode modifier type.
362 ///
363 /// @param mt - The modifier type.
364 /// @return   - A pointer to the statically-allocated string (e.g.,
365 ///             "MODIFIER_NONE" for MODIFIER_NONE).
366 static const char* stringForModifierType(ModifierType mt) {
367 #define ENUM_ENTRY(n) case n: return #n;
368   switch(mt) {
369     default:
370       llvm_unreachable("Unknown modifier type");
371     MODIFIER_TYPES
372   };
373 #undef ENUM_ENTRY
374 }
375
376 DisassemblerTables::DisassemblerTables() {
377   unsigned i;
378
379   for (i = 0; i < array_lengthof(Tables); i++) {
380     Tables[i] = new ContextDecision;
381     memset(Tables[i], 0, sizeof(ContextDecision));
382   }
383
384   HasConflicts = false;
385 }
386
387 DisassemblerTables::~DisassemblerTables() {
388   unsigned i;
389
390   for (i = 0; i < array_lengthof(Tables); i++)
391     delete Tables[i];
392 }
393
394 void DisassemblerTables::emitModRMDecision(raw_ostream &o1, raw_ostream &o2,
395                                            unsigned &i1, unsigned &i2,
396                                            ModRMDecision &decision) const {
397   static uint32_t sTableNumber = 0;
398   static uint32_t sEntryNumber = 1;
399   ModRMDecisionType dt = getDecisionType(decision);
400
401   if (dt == MODRM_ONEENTRY && decision.instructionIDs[0] == 0)
402   {
403     o2.indent(i2) << "{ /* ModRMDecision */" << "\n";
404     i2++;
405
406     o2.indent(i2) << stringForDecisionType(dt) << "," << "\n";
407     o2.indent(i2) << 0 << " /* EmptyTable */\n";
408
409     i2--;
410     o2.indent(i2) << "}";
411     return;
412   }
413
414   o1 << "/* Table" << sTableNumber << " */\n";
415   i1++;
416
417   switch (dt) {
418     default:
419       llvm_unreachable("Unknown decision type");
420     case MODRM_ONEENTRY:
421       emitOneID(o1, i1, decision.instructionIDs[0], true);
422       break;
423     case MODRM_SPLITRM:
424       emitOneID(o1, i1, decision.instructionIDs[0x00], true); // mod = 0b00
425       emitOneID(o1, i1, decision.instructionIDs[0xc0], true); // mod = 0b11
426       break;
427     case MODRM_SPLITREG:
428       for (unsigned index = 0; index < 64; index += 8)
429         emitOneID(o1, i1, decision.instructionIDs[index], true);
430       for (unsigned index = 0xc0; index < 256; index += 8)
431         emitOneID(o1, i1, decision.instructionIDs[index], true);
432       break;
433     case MODRM_SPLITMISC:
434       for (unsigned index = 0; index < 64; index += 8)
435         emitOneID(o1, i1, decision.instructionIDs[index], true);
436       for (unsigned index = 0xc0; index < 256; ++index)
437         emitOneID(o1, i1, decision.instructionIDs[index], true);
438       break;
439     case MODRM_FULL:
440       for (unsigned index = 0; index < 256; ++index)
441         emitOneID(o1, i1, decision.instructionIDs[index], true);
442       break;
443   }
444
445   i1--;
446
447   o2.indent(i2) << "{ /* struct ModRMDecision */" << "\n";
448   i2++;
449
450   o2.indent(i2) << stringForDecisionType(dt) << "," << "\n";
451   o2.indent(i2) << sEntryNumber << " /* Table" << sTableNumber << " */\n";
452
453   i2--;
454   o2.indent(i2) << "}";
455
456   switch (dt) {
457     default:
458       llvm_unreachable("Unknown decision type");
459     case MODRM_ONEENTRY:
460       sEntryNumber += 1;
461       break;
462     case MODRM_SPLITRM:
463       sEntryNumber += 2;
464       break;
465     case MODRM_SPLITREG:
466       sEntryNumber += 16;
467       break;
468     case MODRM_SPLITMISC:
469       sEntryNumber += 8 + 64;
470       break;
471     case MODRM_FULL:
472       sEntryNumber += 256;
473       break;
474   }
475
476   // We assume that the index can fit into uint16_t.
477   assert(sEntryNumber < 65536U &&
478          "Index into ModRMDecision is too large for uint16_t!");
479
480   ++sTableNumber;
481 }
482
483 void DisassemblerTables::emitOpcodeDecision(raw_ostream &o1, raw_ostream &o2,
484                                             unsigned &i1, unsigned &i2,
485                                             OpcodeDecision &decision) const {
486   o2.indent(i2) << "{ /* struct OpcodeDecision */" << "\n";
487   i2++;
488   o2.indent(i2) << "{" << "\n";
489   i2++;
490
491   for (unsigned index = 0; index < 256; ++index) {
492     o2.indent(i2);
493
494     o2 << "/* 0x" << format("%02hhx", index) << " */" << "\n";
495
496     emitModRMDecision(o1, o2, i1, i2, decision.modRMDecisions[index]);
497
498     if (index <  255)
499       o2 << ",";
500
501     o2 << "\n";
502   }
503
504   i2--;
505   o2.indent(i2) << "}" << "\n";
506   i2--;
507   o2.indent(i2) << "}" << "\n";
508 }
509
510 void DisassemblerTables::emitContextDecision(raw_ostream &o1, raw_ostream &o2,
511                                              unsigned &i1, unsigned &i2,
512                                              ContextDecision &decision,
513                                              const char* name) const {
514   o2.indent(i2) << "static const struct ContextDecision " << name << " = {\n";
515   i2++;
516   o2.indent(i2) << "{ /* opcodeDecisions */" << "\n";
517   i2++;
518
519   for (unsigned index = 0; index < IC_max; ++index) {
520     o2.indent(i2) << "/* ";
521     o2 << stringForContext((InstructionContext)index);
522     o2 << " */";
523     o2 << "\n";
524
525     emitOpcodeDecision(o1, o2, i1, i2, decision.opcodeDecisions[index]);
526
527     if (index + 1 < IC_max)
528       o2 << ", ";
529   }
530
531   i2--;
532   o2.indent(i2) << "}" << "\n";
533   i2--;
534   o2.indent(i2) << "};" << "\n";
535 }
536
537 void DisassemblerTables::emitInstructionInfo(raw_ostream &o,
538                                              unsigned &i) const {
539   unsigned NumInstructions = InstructionSpecifiers.size();
540
541   o << "static const struct OperandSpecifier x86OperandSets[]["
542     << X86_MAX_OPERANDS << "] = {\n";
543
544   typedef std::vector<std::pair<const char *, const char *> > OperandListTy;
545   std::map<OperandListTy, unsigned> OperandSets;
546
547   unsigned OperandSetNum = 0;
548   for (unsigned Index = 0; Index < NumInstructions; ++Index) {
549     OperandListTy OperandList;
550
551     for (unsigned OperandIndex = 0; OperandIndex < X86_MAX_OPERANDS;
552          ++OperandIndex) {
553       const char *Encoding =
554         stringForOperandEncoding((OperandEncoding)InstructionSpecifiers[Index]
555                                  .operands[OperandIndex].encoding);
556       const char *Type =
557         stringForOperandType((OperandType)InstructionSpecifiers[Index]
558                              .operands[OperandIndex].type);
559       OperandList.push_back(std::make_pair(Encoding, Type));
560     }
561     unsigned &N = OperandSets[OperandList];
562     if (N != 0) continue;
563
564     N = ++OperandSetNum;
565
566     o << "  { /* " << (OperandSetNum - 1) << " */\n";
567     for (unsigned i = 0, e = OperandList.size(); i != e; ++i) {
568       o << "    { " << OperandList[i].first << ", "
569         << OperandList[i].second << " },\n";
570     }
571     o << "  },\n";
572   }
573   o << "};" << "\n\n";
574
575   o.indent(i * 2) << "static const struct InstructionSpecifier ";
576   o << INSTRUCTIONS_STR "[" << InstructionSpecifiers.size() << "] = {\n";
577
578   i++;
579
580   for (unsigned index = 0; index < NumInstructions; ++index) {
581     o.indent(i * 2) << "{ /* " << index << " */" << "\n";
582     i++;
583
584     o.indent(i * 2) << stringForModifierType(
585                        (ModifierType)InstructionSpecifiers[index].modifierType);
586     o << ",\n";
587
588     o.indent(i * 2) << "0x";
589     o << format("%02hhx", (uint16_t)InstructionSpecifiers[index].modifierBase);
590     o << ",\n";
591
592     OperandListTy OperandList;
593     for (unsigned OperandIndex = 0; OperandIndex < X86_MAX_OPERANDS;
594          ++OperandIndex) {
595       const char *Encoding =
596         stringForOperandEncoding((OperandEncoding)InstructionSpecifiers[index]
597                                  .operands[OperandIndex].encoding);
598       const char *Type =
599         stringForOperandType((OperandType)InstructionSpecifiers[index]
600                              .operands[OperandIndex].type);
601       OperandList.push_back(std::make_pair(Encoding, Type));
602     }
603     o.indent(i * 2) << (OperandSets[OperandList] - 1) << ",\n";
604
605     o.indent(i * 2) << "/* " << InstructionSpecifiers[index].name << " */";
606     o << "\n";
607
608     i--;
609     o.indent(i * 2) << "}";
610
611     if (index + 1 < NumInstructions)
612       o << ",";
613
614     o << "\n";
615   }
616
617   i--;
618   o.indent(i * 2) << "};" << "\n";
619 }
620
621 void DisassemblerTables::emitContextTable(raw_ostream &o, unsigned &i) const {
622   o.indent(i * 2) << "static const uint8_t " CONTEXTS_STR
623                      "[256] = {\n";
624   i++;
625
626   for (unsigned index = 0; index < 256; ++index) {
627     o.indent(i * 2);
628
629     if ((index & ATTR_VEXL) && (index & ATTR_REXW) && (index & ATTR_OPSIZE))
630       o << "IC_VEX_L_W_OPSIZE";
631     else if ((index & ATTR_VEXL) && (index & ATTR_REXW) && (index & ATTR_XD))
632       o << "IC_VEX_L_W_XD";
633     else if ((index & ATTR_VEXL) && (index & ATTR_REXW) && (index & ATTR_XS))
634       o << "IC_VEX_L_W_XS";
635     else if ((index & ATTR_VEXL) && (index & ATTR_REXW))
636       o << "IC_VEX_L_W";
637     else if ((index & ATTR_VEXL) && (index & ATTR_OPSIZE))
638       o << "IC_VEX_L_OPSIZE";
639     else if ((index & ATTR_VEXL) && (index & ATTR_XD))
640       o << "IC_VEX_L_XD";
641     else if ((index & ATTR_VEXL) && (index & ATTR_XS))
642       o << "IC_VEX_L_XS";
643     else if ((index & ATTR_VEX) && (index & ATTR_REXW) && (index & ATTR_OPSIZE))
644       o << "IC_VEX_W_OPSIZE";
645     else if ((index & ATTR_VEX) && (index & ATTR_REXW) && (index & ATTR_XD))
646       o << "IC_VEX_W_XD";
647     else if ((index & ATTR_VEX) && (index & ATTR_REXW) && (index & ATTR_XS))
648       o << "IC_VEX_W_XS";
649     else if (index & ATTR_VEXL)
650       o << "IC_VEX_L";
651     else if ((index & ATTR_VEX) && (index & ATTR_REXW))
652       o << "IC_VEX_W";
653     else if ((index & ATTR_VEX) && (index & ATTR_OPSIZE))
654       o << "IC_VEX_OPSIZE";
655     else if ((index & ATTR_VEX) && (index & ATTR_XD))
656       o << "IC_VEX_XD";
657     else if ((index & ATTR_VEX) && (index & ATTR_XS))
658       o << "IC_VEX_XS";
659     else if (index & ATTR_VEX)
660       o << "IC_VEX";
661     else if ((index & ATTR_64BIT) && (index & ATTR_REXW) && (index & ATTR_XS))
662       o << "IC_64BIT_REXW_XS";
663     else if ((index & ATTR_64BIT) && (index & ATTR_REXW) && (index & ATTR_XD))
664       o << "IC_64BIT_REXW_XD";
665     else if ((index & ATTR_64BIT) && (index & ATTR_REXW) &&
666              (index & ATTR_OPSIZE))
667       o << "IC_64BIT_REXW_OPSIZE";
668     else if ((index & ATTR_64BIT) && (index & ATTR_XD) && (index & ATTR_OPSIZE))
669       o << "IC_64BIT_XD_OPSIZE";
670     else if ((index & ATTR_64BIT) && (index & ATTR_XS) && (index & ATTR_OPSIZE))
671       o << "IC_64BIT_XS_OPSIZE";
672     else if ((index & ATTR_64BIT) && (index & ATTR_XS))
673       o << "IC_64BIT_XS";
674     else if ((index & ATTR_64BIT) && (index & ATTR_XD))
675       o << "IC_64BIT_XD";
676     else if ((index & ATTR_64BIT) && (index & ATTR_OPSIZE))
677       o << "IC_64BIT_OPSIZE";
678     else if ((index & ATTR_64BIT) && (index & ATTR_ADSIZE))
679       o << "IC_64BIT_ADSIZE";
680     else if ((index & ATTR_64BIT) && (index & ATTR_REXW))
681       o << "IC_64BIT_REXW";
682     else if ((index & ATTR_64BIT))
683       o << "IC_64BIT";
684     else if ((index & ATTR_XS) && (index & ATTR_OPSIZE))
685       o << "IC_XS_OPSIZE";
686     else if ((index & ATTR_XD) && (index & ATTR_OPSIZE))
687       o << "IC_XD_OPSIZE";
688     else if (index & ATTR_XS)
689       o << "IC_XS";
690     else if (index & ATTR_XD)
691       o << "IC_XD";
692     else if (index & ATTR_OPSIZE)
693       o << "IC_OPSIZE";
694     else if (index & ATTR_ADSIZE)
695       o << "IC_ADSIZE";
696     else
697       o << "IC";
698
699     if (index < 255)
700       o << ",";
701     else
702       o << " ";
703
704     o << " /* " << index << " */";
705
706     o << "\n";
707   }
708
709   i--;
710   o.indent(i * 2) << "};" << "\n";
711 }
712
713 void DisassemblerTables::emitContextDecisions(raw_ostream &o1, raw_ostream &o2,
714                                              unsigned &i1, unsigned &i2) const {
715   emitContextDecision(o1, o2, i1, i2, *Tables[0], ONEBYTE_STR);
716   emitContextDecision(o1, o2, i1, i2, *Tables[1], TWOBYTE_STR);
717   emitContextDecision(o1, o2, i1, i2, *Tables[2], THREEBYTE38_STR);
718   emitContextDecision(o1, o2, i1, i2, *Tables[3], THREEBYTE3A_STR);
719   emitContextDecision(o1, o2, i1, i2, *Tables[4], THREEBYTEA6_STR);
720   emitContextDecision(o1, o2, i1, i2, *Tables[5], THREEBYTEA7_STR);
721 }
722
723 void DisassemblerTables::emit(raw_ostream &o) const {
724   unsigned i1 = 0;
725   unsigned i2 = 0;
726
727   std::string s1;
728   std::string s2;
729
730   raw_string_ostream o1(s1);
731   raw_string_ostream o2(s2);
732
733   emitInstructionInfo(o, i2);
734   o << "\n";
735
736   emitContextTable(o, i2);
737   o << "\n";
738
739   o << "static const InstrUID modRMTable[] = {\n";
740   i1++;
741   emitEmptyTable(o1, i1);
742   i1--;
743   emitContextDecisions(o1, o2, i1, i2);
744
745   o << o1.str();
746   o << "  0x0\n";
747   o << "};\n";
748   o << "\n";
749   o << o2.str();
750   o << "\n";
751   o << "\n";
752 }
753
754 void DisassemblerTables::setTableFields(ModRMDecision     &decision,
755                                         const ModRMFilter &filter,
756                                         InstrUID          uid,
757                                         uint8_t           opcode) {
758   for (unsigned index = 0; index < 256; ++index) {
759     if (filter.accepts(index)) {
760       if (decision.instructionIDs[index] == uid)
761         continue;
762
763       if (decision.instructionIDs[index] != 0) {
764         InstructionSpecifier &newInfo =
765           InstructionSpecifiers[uid];
766         InstructionSpecifier &previousInfo =
767           InstructionSpecifiers[decision.instructionIDs[index]];
768
769         if(newInfo.filtered)
770           continue; // filtered instructions get lowest priority
771
772         if(previousInfo.name == "NOOP" && (newInfo.name == "XCHG16ar" ||
773                                            newInfo.name == "XCHG32ar" ||
774                                            newInfo.name == "XCHG32ar64" ||
775                                            newInfo.name == "XCHG64ar"))
776           continue; // special case for XCHG*ar and NOOP
777
778         if (outranks(previousInfo.insnContext, newInfo.insnContext))
779           continue;
780
781         if (previousInfo.insnContext == newInfo.insnContext &&
782             !previousInfo.filtered) {
783           errs() << "Error: Primary decode conflict: ";
784           errs() << newInfo.name << " would overwrite " << previousInfo.name;
785           errs() << "\n";
786           errs() << "ModRM   " << index << "\n";
787           errs() << "Opcode  " << (uint16_t)opcode << "\n";
788           errs() << "Context " << stringForContext(newInfo.insnContext) << "\n";
789           HasConflicts = true;
790         }
791       }
792
793       decision.instructionIDs[index] = uid;
794     }
795   }
796 }
797
798 void DisassemblerTables::setTableFields(OpcodeType          type,
799                                         InstructionContext  insnContext,
800                                         uint8_t             opcode,
801                                         const ModRMFilter   &filter,
802                                         InstrUID            uid,
803                                         bool                is32bit,
804                                         bool                ignoresVEX_L) {
805   ContextDecision &decision = *Tables[type];
806
807   for (unsigned index = 0; index < IC_max; ++index) {
808     if (is32bit && inheritsFrom((InstructionContext)index, IC_64BIT))
809       continue;
810
811     if (inheritsFrom((InstructionContext)index,
812                      InstructionSpecifiers[uid].insnContext, ignoresVEX_L))
813       setTableFields(decision.opcodeDecisions[index].modRMDecisions[opcode],
814                      filter,
815                      uid,
816                      opcode);
817   }
818 }