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