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