Transform (x&C)>V into (x&C)!=0 where possible
[oota-llvm.git] / utils / TableGen / EDEmitter.cpp
1 //===- EDEmitter.cpp - Generate instruction descriptions for ED -*- 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 tablegen backend is responsible for emitting a description of each
11 // instruction in a format that the enhanced disassembler can use to tokenize
12 // and parse instructions.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #include "AsmWriterInst.h"
17 #include "CodeGenTarget.h"
18 #include "llvm/MC/EDInstInfo.h"
19 #include "llvm/Support/ErrorHandling.h"
20 #include "llvm/Support/Format.h"
21 #include "llvm/Support/raw_ostream.h"
22 #include "llvm/TableGen/Error.h"
23 #include "llvm/TableGen/Record.h"
24 #include "llvm/TableGen/TableGenBackend.h"
25 #include <string>
26 #include <vector>
27
28 using namespace llvm;
29
30 // TODO: There's a suspiciously large amount of "table" data in this
31 // backend which should probably be in the TableGen file itself.
32
33 ///////////////////////////////////////////////////////////
34 // Support classes for emitting nested C data structures //
35 ///////////////////////////////////////////////////////////
36
37 // TODO: These classes are probably generally useful to other backends;
38 // add them to TableGen's "helper" API's.
39
40 namespace {
41 class EnumEmitter {
42 private:
43   std::string Name;
44   std::vector<std::string> Entries;
45 public:
46   EnumEmitter(const char *N) : Name(N) {
47   }
48   int addEntry(const char *e) {
49     Entries.push_back(std::string(e));
50     return Entries.size() - 1;
51   }
52   void emit(raw_ostream &o, unsigned int &i) {
53     o.indent(i) << "enum " << Name.c_str() << " {" << "\n";
54     i += 2;
55
56     unsigned int index = 0;
57     unsigned int numEntries = Entries.size();
58     for (index = 0; index < numEntries; ++index) {
59       o.indent(i) << Entries[index];
60       if (index < (numEntries - 1))
61         o << ",";
62       o << "\n";
63     }
64
65     i -= 2;
66     o.indent(i) << "};" << "\n";
67   }
68
69   void emitAsFlags(raw_ostream &o, unsigned int &i) {
70     o.indent(i) << "enum " << Name.c_str() << " {" << "\n";
71     i += 2;
72
73     unsigned int index = 0;
74     unsigned int numEntries = Entries.size();
75     unsigned int flag = 1;
76     for (index = 0; index < numEntries; ++index) {
77       o.indent(i) << Entries[index] << " = " << format("0x%x", flag);
78       if (index < (numEntries - 1))
79         o << ",";
80       o << "\n";
81       flag <<= 1;
82     }
83
84     i -= 2;
85     o.indent(i) << "};" << "\n";
86   }
87 };
88 } // End anonymous namespace
89
90 namespace {
91 class ConstantEmitter {
92 public:
93   virtual ~ConstantEmitter() { }
94   virtual void emit(raw_ostream &o, unsigned int &i) = 0;
95 };
96 } // End anonymous namespace
97
98 namespace {
99 class LiteralConstantEmitter : public ConstantEmitter {
100 private:
101   bool IsNumber;
102   union {
103     int Number;
104     const char* String;
105   };
106 public:
107   LiteralConstantEmitter(int number = 0) :
108     IsNumber(true),
109     Number(number) {
110   }
111   void set(const char *string) {
112     IsNumber = false;
113     Number = 0;
114     String = string;
115   }
116   bool is(const char *string) {
117     return !strcmp(String, string);
118   }
119   void emit(raw_ostream &o, unsigned int &i) {
120     if (IsNumber)
121       o << Number;
122     else
123       o << String;
124   }
125 };
126 } // End anonymous namespace
127
128 namespace {
129 class CompoundConstantEmitter : public ConstantEmitter {
130 private:
131   unsigned int Padding;
132   std::vector<ConstantEmitter *> Entries;
133 public:
134   CompoundConstantEmitter(unsigned int padding = 0) : Padding(padding) {
135   }
136   CompoundConstantEmitter &addEntry(ConstantEmitter *e) {
137     Entries.push_back(e);
138
139     return *this;
140   }
141   ~CompoundConstantEmitter() {
142     while (Entries.size()) {
143       ConstantEmitter *entry = Entries.back();
144       Entries.pop_back();
145       delete entry;
146     }
147   }
148   void emit(raw_ostream &o, unsigned int &i) {
149     o << "{" << "\n";
150     i += 2;
151
152     unsigned int index;
153     unsigned int numEntries = Entries.size();
154
155     unsigned int numToPrint;
156
157     if (Padding) {
158       if (numEntries > Padding) {
159         fprintf(stderr, "%u entries but %u padding\n", numEntries, Padding);
160         llvm_unreachable("More entries than padding");
161       }
162       numToPrint = Padding;
163     } else {
164       numToPrint = numEntries;
165     }
166
167     for (index = 0; index < numToPrint; ++index) {
168       o.indent(i);
169       if (index < numEntries)
170         Entries[index]->emit(o, i);
171       else
172         o << "-1";
173
174       if (index < (numToPrint - 1))
175         o << ",";
176       o << "\n";
177     }
178
179     i -= 2;
180     o.indent(i) << "}";
181   }
182 };
183 } // End anonymous namespace
184
185 namespace {
186 class FlagsConstantEmitter : public ConstantEmitter {
187 private:
188   std::vector<std::string> Flags;
189 public:
190   FlagsConstantEmitter() {
191   }
192   FlagsConstantEmitter &addEntry(const char *f) {
193     Flags.push_back(std::string(f));
194     return *this;
195   }
196   void emit(raw_ostream &o, unsigned int &i) {
197     unsigned int index;
198     unsigned int numFlags = Flags.size();
199     if (numFlags == 0)
200       o << "0";
201
202     for (index = 0; index < numFlags; ++index) {
203       o << Flags[index].c_str();
204       if (index < (numFlags - 1))
205         o << " | ";
206     }
207   }
208 };
209 } // End anonymous namespace
210
211 /// populateOperandOrder - Accepts a CodeGenInstruction and generates its
212 ///   AsmWriterInst for the desired assembly syntax, giving an ordered list of
213 ///   operands in the order they appear in the printed instruction.  Then, for
214 ///   each entry in that list, determines the index of the same operand in the
215 ///   CodeGenInstruction, and emits the resulting mapping into an array, filling
216 ///   in unused slots with -1.
217 ///
218 /// @arg operandOrder - The array that will be populated with the operand
219 ///                     mapping.  Each entry will contain -1 (invalid index
220 ///                     into the operands present in the AsmString) or a number
221 ///                     representing an index in the operand descriptor array.
222 /// @arg inst         - The instruction to use when looking up the operands
223 /// @arg syntax       - The syntax to use, according to LLVM's enumeration
224 static void populateOperandOrder(CompoundConstantEmitter *operandOrder,
225                                  const CodeGenInstruction &inst,
226                                  unsigned syntax) {
227   unsigned int numArgs = 0;
228
229   AsmWriterInst awInst(inst, syntax, -1, -1);
230
231   std::vector<AsmWriterOperand>::iterator operandIterator;
232
233   for (operandIterator = awInst.Operands.begin();
234        operandIterator != awInst.Operands.end();
235        ++operandIterator) {
236     if (operandIterator->OperandType ==
237         AsmWriterOperand::isMachineInstrOperand) {
238       operandOrder->addEntry(
239         new LiteralConstantEmitter(operandIterator->CGIOpNo));
240       numArgs++;
241     }
242   }
243 }
244
245 /////////////////////////////////////////////////////
246 // Support functions for handling X86 instructions //
247 /////////////////////////////////////////////////////
248
249 #define SET(flag) { type->set(flag); return 0; }
250
251 #define REG(str) if (name == str) SET("kOperandTypeRegister");
252 #define MEM(str) if (name == str) SET("kOperandTypeX86Memory");
253 #define LEA(str) if (name == str) SET("kOperandTypeX86EffectiveAddress");
254 #define IMM(str) if (name == str) SET("kOperandTypeImmediate");
255 #define PCR(str) if (name == str) SET("kOperandTypeX86PCRelative");
256
257 /// X86TypeFromOpName - Processes the name of a single X86 operand (which is
258 ///   actually its type) and translates it into an operand type
259 ///
260 /// @arg flags    - The type object to set
261 /// @arg name     - The name of the operand
262 static int X86TypeFromOpName(LiteralConstantEmitter *type,
263                              const std::string &name) {
264   REG("GR8");
265   REG("GR8_NOREX");
266   REG("GR16");
267   REG("GR16_NOAX");
268   REG("GR32");
269   REG("GR32_NOAX");
270   REG("GR32_NOREX");
271   REG("GR32_TC");
272   REG("FR32");
273   REG("RFP32");
274   REG("GR64");
275   REG("GR64_NOAX");
276   REG("GR64_TC");
277   REG("FR64");
278   REG("VR64");
279   REG("RFP64");
280   REG("RFP80");
281   REG("VR128");
282   REG("VR256");
283   REG("RST");
284   REG("SEGMENT_REG");
285   REG("DEBUG_REG");
286   REG("CONTROL_REG");
287
288   IMM("i8imm");
289   IMM("i16imm");
290   IMM("i16i8imm");
291   IMM("i32imm");
292   IMM("i32i8imm");
293   IMM("u32u8imm");
294   IMM("i64imm");
295   IMM("i64i8imm");
296   IMM("i64i32imm");
297   IMM("SSECC");
298   IMM("AVXCC");
299
300   // all R, I, R, I, R
301   MEM("i8mem");
302   MEM("i8mem_NOREX");
303   MEM("i16mem");
304   MEM("i32mem");
305   MEM("i32mem_TC");
306   MEM("f32mem");
307   MEM("ssmem");
308   MEM("opaque32mem");
309   MEM("opaque48mem");
310   MEM("i64mem");
311   MEM("i64mem_TC");
312   MEM("f64mem");
313   MEM("sdmem");
314   MEM("f80mem");
315   MEM("opaque80mem");
316   MEM("i128mem");
317   MEM("i256mem");
318   MEM("f128mem");
319   MEM("f256mem");
320   MEM("opaque512mem");
321   // Gather
322   MEM("vx32mem")
323   MEM("vy32mem")
324   MEM("vx64mem")
325   MEM("vy64mem")
326
327   // all R, I, R, I
328   LEA("lea32mem");
329   LEA("lea64_32mem");
330   LEA("lea64mem");
331
332   // all I
333   PCR("i16imm_pcrel");
334   PCR("i32imm_pcrel");
335   PCR("i64i32imm_pcrel");
336   PCR("brtarget8");
337   PCR("offset8");
338   PCR("offset16");
339   PCR("offset32");
340   PCR("offset64");
341   PCR("brtarget");
342   PCR("uncondbrtarget");
343   PCR("bltarget");
344
345   // all I, ARM mode only, conditional/unconditional
346   PCR("br_target");
347   PCR("bl_target");
348   return 1;
349 }
350
351 #undef REG
352 #undef MEM
353 #undef LEA
354 #undef IMM
355 #undef PCR
356
357 #undef SET
358
359 /// X86PopulateOperands - Handles all the operands in an X86 instruction, adding
360 ///   the appropriate flags to their descriptors
361 ///
362 /// \param operandTypes A reference the array of operand type objects
363 /// \param inst         The instruction to use as a source of information
364 static void X86PopulateOperands(
365   LiteralConstantEmitter *(&operandTypes)[EDIS_MAX_OPERANDS],
366   const CodeGenInstruction &inst) {
367   if (!inst.TheDef->isSubClassOf("X86Inst"))
368     return;
369
370   unsigned int index;
371   unsigned int numOperands = inst.Operands.size();
372
373   for (index = 0; index < numOperands; ++index) {
374     const CGIOperandList::OperandInfo &operandInfo = inst.Operands[index];
375     Record &rec = *operandInfo.Rec;
376
377     if (X86TypeFromOpName(operandTypes[index], rec.getName()) &&
378         !rec.isSubClassOf("PointerLikeRegClass")) {
379       errs() << "Operand type: " << rec.getName().c_str() << "\n";
380       errs() << "Operand name: " << operandInfo.Name.c_str() << "\n";
381       errs() << "Instruction name: " << inst.TheDef->getName().c_str() << "\n";
382       llvm_unreachable("Unhandled type");
383     }
384   }
385 }
386
387 /// decorate1 - Decorates a named operand with a new flag
388 ///
389 /// \param operandFlags The array of operand flag objects, which don't have
390 ///                     names
391 /// \param inst         The CodeGenInstruction, which provides a way to
392 //                      translate between names and operand indices
393 /// \param opName       The name of the operand
394 /// \param opFlag       The name of the flag to add
395 static inline void decorate1(
396   FlagsConstantEmitter *(&operandFlags)[EDIS_MAX_OPERANDS],
397   const CodeGenInstruction &inst,
398   const char *opName,
399   const char *opFlag) {
400   unsigned opIndex;
401
402   opIndex = inst.Operands.getOperandNamed(std::string(opName));
403
404   operandFlags[opIndex]->addEntry(opFlag);
405 }
406
407 #define DECORATE1(opName, opFlag) decorate1(operandFlags, inst, opName, opFlag)
408
409 #define MOV(source, target) {               \
410   instType.set("kInstructionTypeMove");     \
411   DECORATE1(source, "kOperandFlagSource");  \
412   DECORATE1(target, "kOperandFlagTarget");  \
413 }
414
415 #define BRANCH(target) {                    \
416   instType.set("kInstructionTypeBranch");   \
417   DECORATE1(target, "kOperandFlagTarget");  \
418 }
419
420 #define PUSH(source) {                      \
421   instType.set("kInstructionTypePush");     \
422   DECORATE1(source, "kOperandFlagSource");  \
423 }
424
425 #define POP(target) {                       \
426   instType.set("kInstructionTypePop");      \
427   DECORATE1(target, "kOperandFlagTarget");  \
428 }
429
430 #define CALL(target) {                      \
431   instType.set("kInstructionTypeCall");     \
432   DECORATE1(target, "kOperandFlagTarget");  \
433 }
434
435 #define RETURN() {                          \
436   instType.set("kInstructionTypeReturn");   \
437 }
438
439 /// X86ExtractSemantics - Performs various checks on the name of an X86
440 ///   instruction to determine what sort of an instruction it is and then adds
441 ///   the appropriate flags to the instruction and its operands
442 ///
443 /// \param instType     A reference to the type for the instruction as a whole
444 /// \param operandFlags A reference to the array of operand flag object pointers
445 /// \param inst         A reference to the original instruction
446 static void X86ExtractSemantics(
447   LiteralConstantEmitter &instType,
448   FlagsConstantEmitter *(&operandFlags)[EDIS_MAX_OPERANDS],
449   const CodeGenInstruction &inst) {
450   const std::string &name = inst.TheDef->getName();
451
452   if (name.find("MOV") != name.npos) {
453     if (name.find("MOV_V") != name.npos) {
454       // ignore (this is a pseudoinstruction)
455     } else if (name.find("MASK") != name.npos) {
456       // ignore (this is a masking move)
457     } else if (name.find("r0") != name.npos) {
458       // ignore (this is a pseudoinstruction)
459     } else if (name.find("PS") != name.npos ||
460              name.find("PD") != name.npos) {
461       // ignore (this is a shuffling move)
462     } else if (name.find("MOVS") != name.npos) {
463       // ignore (this is a string move)
464     } else if (name.find("_F") != name.npos) {
465       // TODO handle _F moves to ST(0)
466     } else if (name.find("a") != name.npos) {
467       // TODO handle moves to/from %ax
468     } else if (name.find("CMOV") != name.npos) {
469       MOV("src2", "dst");
470     } else if (name.find("PC") != name.npos) {
471       MOV("label", "reg")
472     } else {
473       MOV("src", "dst");
474     }
475   }
476
477   if (name.find("JMP") != name.npos ||
478       name.find("J") == 0) {
479     if (name.find("FAR") != name.npos && name.find("i") != name.npos) {
480       BRANCH("off");
481     } else {
482       BRANCH("dst");
483     }
484   }
485
486   if (name.find("PUSH") != name.npos) {
487     if (name.find("CS") != name.npos ||
488         name.find("DS") != name.npos ||
489         name.find("ES") != name.npos ||
490         name.find("FS") != name.npos ||
491         name.find("GS") != name.npos ||
492         name.find("SS") != name.npos) {
493       instType.set("kInstructionTypePush");
494       // TODO add support for fixed operands
495     } else if (name.find("F") != name.npos) {
496       // ignore (this pushes onto the FP stack)
497     } else if (name.find("A") != name.npos) {
498       // ignore (pushes all GP registoers onto the stack)
499     } else if (name[name.length() - 1] == 'm') {
500       PUSH("src");
501     } else if (name.find("i") != name.npos) {
502       PUSH("imm");
503     } else {
504       PUSH("reg");
505     }
506   }
507
508   if (name.find("POP") != name.npos) {
509     if (name.find("POPCNT") != name.npos) {
510       // ignore (not a real pop)
511     } else if (name.find("CS") != name.npos ||
512                name.find("DS") != name.npos ||
513                name.find("ES") != name.npos ||
514                name.find("FS") != name.npos ||
515                name.find("GS") != name.npos ||
516                name.find("SS") != name.npos) {
517       instType.set("kInstructionTypePop");
518       // TODO add support for fixed operands
519     } else if (name.find("F") != name.npos) {
520       // ignore (this pops from the FP stack)
521     } else if (name.find("A") != name.npos) {
522       // ignore (pushes all GP registoers onto the stack)
523     } else if (name[name.length() - 1] == 'm') {
524       POP("dst");
525     } else {
526       POP("reg");
527     }
528   }
529
530   if (name.find("CALL") != name.npos) {
531     if (name.find("ADJ") != name.npos) {
532       // ignore (not a call)
533     } else if (name.find("SYSCALL") != name.npos) {
534       // ignore (doesn't go anywhere we know about)
535     } else if (name.find("VMCALL") != name.npos) {
536       // ignore (rather different semantics than a regular call)
537     } else if (name.find("VMMCALL") != name.npos) {
538       // ignore (rather different semantics than a regular call)
539     } else if (name.find("FAR") != name.npos && name.find("i") != name.npos) {
540       CALL("off");
541     } else {
542       CALL("dst");
543     }
544   }
545
546   if (name.find("RET") != name.npos) {
547     RETURN();
548   }
549 }
550
551 #undef MOV
552 #undef BRANCH
553 #undef PUSH
554 #undef POP
555 #undef CALL
556 #undef RETURN
557
558 /////////////////////////////////////////////////////
559 // Support functions for handling ARM instructions //
560 /////////////////////////////////////////////////////
561
562 #define SET(flag) { type->set(flag); return 0; }
563
564 #define REG(str)    if (name == str) SET("kOperandTypeRegister");
565 #define IMM(str)    if (name == str) SET("kOperandTypeImmediate");
566
567 #define MISC(str, type)   if (name == str) SET(type);
568
569 /// ARMFlagFromOpName - Processes the name of a single ARM operand (which is
570 ///   actually its type) and translates it into an operand type
571 ///
572 /// \param type The type object to set
573 /// \param name The name of the operand
574 static int ARMFlagFromOpName(LiteralConstantEmitter *type,
575                              const std::string &name) {
576   REG("GPR");
577   REG("rGPR");
578   REG("GPRnopc");
579   REG("GPRsp");
580   REG("tcGPR");
581   REG("cc_out");
582   REG("s_cc_out");
583   REG("tGPR");
584   REG("GPRPairOp");
585   REG("DPR");
586   REG("DPR_VFP2");
587   REG("DPR_8");
588   REG("DPair");
589   REG("SPR");
590   REG("QPR");
591   REG("QQPR");
592   REG("QQQQPR");
593   REG("VecListOneD");
594   REG("VecListDPair");
595   REG("VecListDPairSpaced");
596   REG("VecListThreeD");
597   REG("VecListFourD");
598   REG("VecListOneDAllLanes");
599   REG("VecListDPairAllLanes");
600   REG("VecListDPairSpacedAllLanes");
601
602   IMM("i32imm");
603   IMM("fbits16");
604   IMM("fbits32");
605   IMM("i32imm_hilo16");
606   IMM("bf_inv_mask_imm");
607   IMM("lsb_pos_imm");
608   IMM("width_imm");
609   IMM("jtblock_operand");
610   IMM("nohash_imm");
611   IMM("p_imm");
612   IMM("pf_imm");
613   IMM("c_imm");
614   IMM("coproc_option_imm");
615   IMM("imod_op");
616   IMM("iflags_op");
617   IMM("cpinst_operand");
618   IMM("setend_op");
619   IMM("cps_opt");
620   IMM("vfp_f64imm");
621   IMM("vfp_f32imm");
622   IMM("memb_opt");
623   IMM("msr_mask");
624   IMM("neg_zero");
625   IMM("imm0_31");
626   IMM("imm0_31_m1");
627   IMM("imm1_16");
628   IMM("imm1_32");
629   IMM("nModImm");
630   IMM("nImmSplatI8");
631   IMM("nImmSplatI16");
632   IMM("nImmSplatI32");
633   IMM("nImmSplatI64");
634   IMM("nImmVMOVI32");
635   IMM("nImmVMOVF32");
636   IMM("imm8");
637   IMM("imm16");
638   IMM("imm32");
639   IMM("imm1_7");
640   IMM("imm1_15");
641   IMM("imm1_31");
642   IMM("imm0_1");
643   IMM("imm0_3");
644   IMM("imm0_7");
645   IMM("imm0_15");
646   IMM("imm0_255");
647   IMM("imm0_4095");
648   IMM("imm0_65535");
649   IMM("imm0_65535_expr");
650   IMM("imm24b");
651   IMM("pkh_lsl_amt");
652   IMM("pkh_asr_amt");
653   IMM("jt2block_operand");
654   IMM("t_imm0_1020s4");
655   IMM("t_imm0_508s4");
656   IMM("pclabel");
657   IMM("adrlabel");
658   IMM("t_adrlabel");
659   IMM("t2adrlabel");
660   IMM("shift_imm");
661   IMM("t2_shift_imm");
662   IMM("neon_vcvt_imm32");
663   IMM("shr_imm8");
664   IMM("shr_imm16");
665   IMM("shr_imm32");
666   IMM("shr_imm64");
667   IMM("t2ldrlabel");
668   IMM("postidx_imm8");
669   IMM("postidx_imm8s4");
670   IMM("imm_sr");
671   IMM("imm1_31");
672   IMM("VectorIndex8");
673   IMM("VectorIndex16");
674   IMM("VectorIndex32");
675
676   MISC("brtarget", "kOperandTypeARMBranchTarget");                // ?
677   MISC("uncondbrtarget", "kOperandTypeARMBranchTarget");           // ?
678   MISC("t_brtarget", "kOperandTypeARMBranchTarget");              // ?
679   MISC("t_bcctarget", "kOperandTypeARMBranchTarget");             // ?
680   MISC("t_cbtarget", "kOperandTypeARMBranchTarget");              // ?
681   MISC("bltarget", "kOperandTypeARMBranchTarget");                // ?
682
683   MISC("br_target", "kOperandTypeARMBranchTarget");                // ?
684   MISC("bl_target", "kOperandTypeARMBranchTarget");                // ?
685   MISC("blx_target", "kOperandTypeARMBranchTarget");                // ?
686
687   MISC("t_bltarget", "kOperandTypeARMBranchTarget");              // ?
688   MISC("t_blxtarget", "kOperandTypeARMBranchTarget");             // ?
689   MISC("so_reg_imm", "kOperandTypeARMSoRegReg");                         // R, R, I
690   MISC("so_reg_reg", "kOperandTypeARMSoRegImm");                         // R, R, I
691   MISC("shift_so_reg_reg", "kOperandTypeARMSoRegReg");                   // R, R, I
692   MISC("shift_so_reg_imm", "kOperandTypeARMSoRegImm");                   // R, R, I
693   MISC("t2_so_reg", "kOperandTypeThumb2SoReg");                   // R, I
694   MISC("so_imm", "kOperandTypeARMSoImm");                         // I
695   MISC("rot_imm", "kOperandTypeARMRotImm");                       // I
696   MISC("t2_so_imm", "kOperandTypeThumb2SoImm");                   // I
697   MISC("so_imm2part", "kOperandTypeARMSoImm2Part");               // I
698   MISC("pred", "kOperandTypeARMPredicate");                       // I, R
699   MISC("it_pred", "kOperandTypeARMPredicate");                    // I
700   MISC("addrmode_imm12", "kOperandTypeAddrModeImm12");            // R, I
701   MISC("ldst_so_reg", "kOperandTypeLdStSOReg");                   // R, R, I
702   MISC("postidx_reg", "kOperandTypeARMAddrMode3Offset");          // R, I
703   MISC("addrmode2", "kOperandTypeARMAddrMode2");                  // R, R, I
704   MISC("am2offset_reg", "kOperandTypeARMAddrMode2Offset");        // R, I
705   MISC("am2offset_imm", "kOperandTypeARMAddrMode2Offset");        // R, I
706   MISC("addrmode3", "kOperandTypeARMAddrMode3");                  // R, R, I
707   MISC("am3offset", "kOperandTypeARMAddrMode3Offset");            // R, I
708   MISC("ldstm_mode", "kOperandTypeARMLdStmMode");                 // I
709   MISC("addrmode5", "kOperandTypeARMAddrMode5");                  // R, I
710   MISC("addrmode6", "kOperandTypeARMAddrMode6");                  // R, R, I, I
711   MISC("am6offset", "kOperandTypeARMAddrMode6Offset");            // R, I, I
712   MISC("addrmode6dup", "kOperandTypeARMAddrMode6");               // R, R, I, I
713   MISC("addrmode6oneL32", "kOperandTypeARMAddrMode6");            // R, R, I, I
714   MISC("addrmodepc", "kOperandTypeARMAddrModePC");                // R, I
715   MISC("addr_offset_none", "kOperandTypeARMAddrMode7");           // R
716   MISC("reglist", "kOperandTypeARMRegisterList");                 // I, R, ...
717   MISC("dpr_reglist", "kOperandTypeARMDPRRegisterList");          // I, R, ...
718   MISC("spr_reglist", "kOperandTypeARMSPRRegisterList");          // I, R, ...
719   MISC("it_mask", "kOperandTypeThumbITMask");                     // I
720   MISC("t2addrmode_reg", "kOperandTypeThumb2AddrModeReg");        // R
721   MISC("t2addrmode_posimm8", "kOperandTypeThumb2AddrModeImm8");   // R, I
722   MISC("t2addrmode_negimm8", "kOperandTypeThumb2AddrModeImm8");   // R, I
723   MISC("t2addrmode_imm8", "kOperandTypeThumb2AddrModeImm8");      // R, I
724   MISC("t2am_imm8_offset", "kOperandTypeThumb2AddrModeImm8Offset");//I
725   MISC("t2addrmode_imm12", "kOperandTypeThumb2AddrModeImm12");    // R, I
726   MISC("t2addrmode_so_reg", "kOperandTypeThumb2AddrModeSoReg");   // R, R, I
727   MISC("t2addrmode_imm8s4", "kOperandTypeThumb2AddrModeImm8s4");  // R, I
728   MISC("t2addrmode_imm0_1020s4", "kOperandTypeThumb2AddrModeImm8s4");  // R, I
729   MISC("t2am_imm8s4_offset", "kOperandTypeThumb2AddrModeImm8s4Offset");
730                                                                   // R, I
731   MISC("tb_addrmode", "kOperandTypeARMTBAddrMode");               // I
732   MISC("t_addrmode_rrs1", "kOperandTypeThumbAddrModeRegS1");      // R, R
733   MISC("t_addrmode_rrs2", "kOperandTypeThumbAddrModeRegS2");      // R, R
734   MISC("t_addrmode_rrs4", "kOperandTypeThumbAddrModeRegS4");      // R, R
735   MISC("t_addrmode_is1", "kOperandTypeThumbAddrModeImmS1");       // R, I
736   MISC("t_addrmode_is2", "kOperandTypeThumbAddrModeImmS2");       // R, I
737   MISC("t_addrmode_is4", "kOperandTypeThumbAddrModeImmS4");       // R, I
738   MISC("t_addrmode_rr", "kOperandTypeThumbAddrModeRR");           // R, R
739   MISC("t_addrmode_sp", "kOperandTypeThumbAddrModeSP");           // R, I
740   MISC("t_addrmode_pc", "kOperandTypeThumbAddrModePC");           // R, I
741   MISC("addrmode_tbb", "kOperandTypeThumbAddrModeRR");            // R, R
742   MISC("addrmode_tbh", "kOperandTypeThumbAddrModeRR");            // R, R
743
744   return 1;
745 }
746
747 #undef REG
748 #undef MEM
749 #undef MISC
750
751 #undef SET
752
753 /// ARMPopulateOperands - Handles all the operands in an ARM instruction, adding
754 ///   the appropriate flags to their descriptors
755 ///
756 /// \param operandTypes A reference the array of operand type objects
757 /// \param inst         The instruction to use as a source of information
758 static void ARMPopulateOperands(
759   LiteralConstantEmitter *(&operandTypes)[EDIS_MAX_OPERANDS],
760   const CodeGenInstruction &inst) {
761   if (!inst.TheDef->isSubClassOf("InstARM") &&
762       !inst.TheDef->isSubClassOf("InstThumb"))
763     return;
764
765   unsigned int index;
766   unsigned int numOperands = inst.Operands.size();
767
768   if (numOperands > EDIS_MAX_OPERANDS) {
769     errs() << "numOperands == " << numOperands << " > " <<
770       EDIS_MAX_OPERANDS << '\n';
771     llvm_unreachable("Too many operands");
772   }
773
774   for (index = 0; index < numOperands; ++index) {
775     const CGIOperandList::OperandInfo &operandInfo = inst.Operands[index];
776     Record &rec = *operandInfo.Rec;
777
778     if (ARMFlagFromOpName(operandTypes[index], rec.getName())) {
779       errs() << "Operand type: " << rec.getName() << '\n';
780       errs() << "Operand name: " << operandInfo.Name << '\n';
781       errs() << "Instruction name: " << inst.TheDef->getName() << '\n';
782       PrintFatalError("Unhandled type in EDEmitter");
783     }
784   }
785 }
786
787 #define BRANCH(target) {                    \
788   instType.set("kInstructionTypeBranch");   \
789   DECORATE1(target, "kOperandFlagTarget");  \
790 }
791
792 /// ARMExtractSemantics - Performs various checks on the name of an ARM
793 ///   instruction to determine what sort of an instruction it is and then adds
794 ///   the appropriate flags to the instruction and its operands
795 ///
796 /// \param instType     A reference to the type for the instruction as a whole
797 /// \param operandTypes A reference to the array of operand type object pointers
798 /// \param operandFlags A reference to the array of operand flag object pointers
799 /// \param inst         A reference to the original instruction
800 static void ARMExtractSemantics(
801   LiteralConstantEmitter &instType,
802   LiteralConstantEmitter *(&operandTypes)[EDIS_MAX_OPERANDS],
803   FlagsConstantEmitter *(&operandFlags)[EDIS_MAX_OPERANDS],
804   const CodeGenInstruction &inst) {
805   const std::string &name = inst.TheDef->getName();
806
807   if (name == "tBcc"   ||
808       name == "tB"     ||
809       name == "t2Bcc"  ||
810       name == "Bcc"    ||
811       name == "tCBZ"   ||
812       name == "tCBNZ") {
813     BRANCH("target");
814   }
815
816   if (name == "tBLr9"      ||
817       name == "BLr9_pred"  ||
818       name == "tBLXi_r9"   ||
819       name == "tBLXr_r9"   ||
820       name == "BLXr9"      ||
821       name == "t2BXJ"      ||
822       name == "BXJ") {
823     BRANCH("func");
824
825     unsigned opIndex;
826     opIndex = inst.Operands.getOperandNamed("func");
827     if (operandTypes[opIndex]->is("kOperandTypeImmediate"))
828       operandTypes[opIndex]->set("kOperandTypeARMBranchTarget");
829   }
830 }
831
832 #undef BRANCH
833
834 /// populateInstInfo - Fills an array of InstInfos with information about each
835 ///   instruction in a target
836 ///
837 /// \param infoArray The array of InstInfo objects to populate
838 /// \param target    The CodeGenTarget to use as a source of instructions
839 static void populateInstInfo(CompoundConstantEmitter &infoArray,
840                              CodeGenTarget &target) {
841   const std::vector<const CodeGenInstruction*> &numberedInstructions =
842     target.getInstructionsByEnumValue();
843
844   unsigned int index;
845   unsigned int numInstructions = numberedInstructions.size();
846
847   for (index = 0; index < numInstructions; ++index) {
848     const CodeGenInstruction& inst = *numberedInstructions[index];
849
850     CompoundConstantEmitter *infoStruct = new CompoundConstantEmitter;
851     infoArray.addEntry(infoStruct);
852
853     LiteralConstantEmitter *instType = new LiteralConstantEmitter;
854     infoStruct->addEntry(instType);
855
856     LiteralConstantEmitter *numOperandsEmitter =
857       new LiteralConstantEmitter(inst.Operands.size());
858     infoStruct->addEntry(numOperandsEmitter);
859
860     CompoundConstantEmitter *operandTypeArray = new CompoundConstantEmitter;
861     infoStruct->addEntry(operandTypeArray);
862
863     LiteralConstantEmitter *operandTypes[EDIS_MAX_OPERANDS];
864
865     CompoundConstantEmitter *operandFlagArray = new CompoundConstantEmitter;
866     infoStruct->addEntry(operandFlagArray);
867
868     FlagsConstantEmitter *operandFlags[EDIS_MAX_OPERANDS];
869
870     for (unsigned operandIndex = 0;
871          operandIndex < EDIS_MAX_OPERANDS;
872          ++operandIndex) {
873       operandTypes[operandIndex] = new LiteralConstantEmitter;
874       operandTypeArray->addEntry(operandTypes[operandIndex]);
875
876       operandFlags[operandIndex] = new FlagsConstantEmitter;
877       operandFlagArray->addEntry(operandFlags[operandIndex]);
878     }
879
880     unsigned numSyntaxes = 0;
881
882     // We don't need to do anything for pseudo-instructions, as we'll never
883     // see them here. We'll only see real instructions.
884     // We still need to emit null initializers for everything.
885     if (!inst.isPseudo) {
886       if (target.getName() == "X86") {
887         X86PopulateOperands(operandTypes, inst);
888         X86ExtractSemantics(*instType, operandFlags, inst);
889         numSyntaxes = 2;
890       }
891       else if (target.getName() == "ARM") {
892         ARMPopulateOperands(operandTypes, inst);
893         ARMExtractSemantics(*instType, operandTypes, operandFlags, inst);
894         numSyntaxes = 1;
895       }
896     }
897
898     CompoundConstantEmitter *operandOrderArray = new CompoundConstantEmitter;
899
900     infoStruct->addEntry(operandOrderArray);
901
902     for (unsigned syntaxIndex = 0;
903          syntaxIndex < EDIS_MAX_SYNTAXES;
904          ++syntaxIndex) {
905       CompoundConstantEmitter *operandOrder =
906         new CompoundConstantEmitter(EDIS_MAX_OPERANDS);
907
908       operandOrderArray->addEntry(operandOrder);
909
910       if (syntaxIndex < numSyntaxes) {
911         populateOperandOrder(operandOrder, inst, syntaxIndex);
912       }
913     }
914
915     infoStruct = NULL;
916   }
917 }
918
919 static void emitCommonEnums(raw_ostream &o, unsigned int &i) {
920   EnumEmitter operandTypes("OperandTypes");
921   operandTypes.addEntry("kOperandTypeNone");
922   operandTypes.addEntry("kOperandTypeImmediate");
923   operandTypes.addEntry("kOperandTypeRegister");
924   operandTypes.addEntry("kOperandTypeX86Memory");
925   operandTypes.addEntry("kOperandTypeX86EffectiveAddress");
926   operandTypes.addEntry("kOperandTypeX86PCRelative");
927   operandTypes.addEntry("kOperandTypeARMBranchTarget");
928   operandTypes.addEntry("kOperandTypeARMSoRegReg");
929   operandTypes.addEntry("kOperandTypeARMSoRegImm");
930   operandTypes.addEntry("kOperandTypeARMSoImm");
931   operandTypes.addEntry("kOperandTypeARMRotImm");
932   operandTypes.addEntry("kOperandTypeARMSoImm2Part");
933   operandTypes.addEntry("kOperandTypeARMPredicate");
934   operandTypes.addEntry("kOperandTypeAddrModeImm12");
935   operandTypes.addEntry("kOperandTypeLdStSOReg");
936   operandTypes.addEntry("kOperandTypeARMAddrMode2");
937   operandTypes.addEntry("kOperandTypeARMAddrMode2Offset");
938   operandTypes.addEntry("kOperandTypeARMAddrMode3");
939   operandTypes.addEntry("kOperandTypeARMAddrMode3Offset");
940   operandTypes.addEntry("kOperandTypeARMLdStmMode");
941   operandTypes.addEntry("kOperandTypeARMAddrMode5");
942   operandTypes.addEntry("kOperandTypeARMAddrMode6");
943   operandTypes.addEntry("kOperandTypeARMAddrMode6Offset");
944   operandTypes.addEntry("kOperandTypeARMAddrMode7");
945   operandTypes.addEntry("kOperandTypeARMAddrModePC");
946   operandTypes.addEntry("kOperandTypeARMRegisterList");
947   operandTypes.addEntry("kOperandTypeARMDPRRegisterList");
948   operandTypes.addEntry("kOperandTypeARMSPRRegisterList");
949   operandTypes.addEntry("kOperandTypeARMTBAddrMode");
950   operandTypes.addEntry("kOperandTypeThumbITMask");
951   operandTypes.addEntry("kOperandTypeThumbAddrModeImmS1");
952   operandTypes.addEntry("kOperandTypeThumbAddrModeImmS2");
953   operandTypes.addEntry("kOperandTypeThumbAddrModeImmS4");
954   operandTypes.addEntry("kOperandTypeThumbAddrModeRegS1");
955   operandTypes.addEntry("kOperandTypeThumbAddrModeRegS2");
956   operandTypes.addEntry("kOperandTypeThumbAddrModeRegS4");
957   operandTypes.addEntry("kOperandTypeThumbAddrModeRR");
958   operandTypes.addEntry("kOperandTypeThumbAddrModeSP");
959   operandTypes.addEntry("kOperandTypeThumbAddrModePC");
960   operandTypes.addEntry("kOperandTypeThumb2AddrModeReg");
961   operandTypes.addEntry("kOperandTypeThumb2SoReg");
962   operandTypes.addEntry("kOperandTypeThumb2SoImm");
963   operandTypes.addEntry("kOperandTypeThumb2AddrModeImm8");
964   operandTypes.addEntry("kOperandTypeThumb2AddrModeImm8Offset");
965   operandTypes.addEntry("kOperandTypeThumb2AddrModeImm12");
966   operandTypes.addEntry("kOperandTypeThumb2AddrModeSoReg");
967   operandTypes.addEntry("kOperandTypeThumb2AddrModeImm8s4");
968   operandTypes.addEntry("kOperandTypeThumb2AddrModeImm8s4Offset");
969   operandTypes.emit(o, i);
970
971   o << "\n";
972
973   EnumEmitter operandFlags("OperandFlags");
974   operandFlags.addEntry("kOperandFlagSource");
975   operandFlags.addEntry("kOperandFlagTarget");
976   operandFlags.emitAsFlags(o, i);
977
978   o << "\n";
979
980   EnumEmitter instructionTypes("InstructionTypes");
981   instructionTypes.addEntry("kInstructionTypeNone");
982   instructionTypes.addEntry("kInstructionTypeMove");
983   instructionTypes.addEntry("kInstructionTypeBranch");
984   instructionTypes.addEntry("kInstructionTypePush");
985   instructionTypes.addEntry("kInstructionTypePop");
986   instructionTypes.addEntry("kInstructionTypeCall");
987   instructionTypes.addEntry("kInstructionTypeReturn");
988   instructionTypes.emit(o, i);
989
990   o << "\n";
991 }
992
993 namespace llvm {
994
995 void EmitEnhancedDisassemblerInfo(RecordKeeper &RK, raw_ostream &OS) {
996   emitSourceFileHeader("Enhanced Disassembler Info", OS);
997   unsigned int i = 0;
998
999   CompoundConstantEmitter infoArray;
1000   CodeGenTarget target(RK);
1001
1002   populateInstInfo(infoArray, target);
1003
1004   emitCommonEnums(OS, i);
1005
1006   OS << "static const llvm::EDInstInfo instInfo"
1007      << target.getName() << "[] = ";
1008   infoArray.emit(OS, i);
1009   OS << ";" << "\n";
1010 }
1011
1012 } // End llvm namespace