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