]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - contrib/llvm/utils/TableGen/EDEmitter.cpp
MFC r244628:
[FreeBSD/stable/9.git] / contrib / llvm / 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("DPR");
585   REG("DPR_VFP2");
586   REG("DPR_8");
587   REG("DPair");
588   REG("SPR");
589   REG("QPR");
590   REG("QQPR");
591   REG("QQQQPR");
592   REG("VecListOneD");
593   REG("VecListDPair");
594   REG("VecListDPairSpaced");
595   REG("VecListThreeD");
596   REG("VecListFourD");
597   REG("VecListOneDAllLanes");
598   REG("VecListDPairAllLanes");
599   REG("VecListDPairSpacedAllLanes");
600
601   IMM("i32imm");
602   IMM("fbits16");
603   IMM("fbits32");
604   IMM("i32imm_hilo16");
605   IMM("bf_inv_mask_imm");
606   IMM("lsb_pos_imm");
607   IMM("width_imm");
608   IMM("jtblock_operand");
609   IMM("nohash_imm");
610   IMM("p_imm");
611   IMM("pf_imm");
612   IMM("c_imm");
613   IMM("coproc_option_imm");
614   IMM("imod_op");
615   IMM("iflags_op");
616   IMM("cpinst_operand");
617   IMM("setend_op");
618   IMM("cps_opt");
619   IMM("vfp_f64imm");
620   IMM("vfp_f32imm");
621   IMM("memb_opt");
622   IMM("msr_mask");
623   IMM("neg_zero");
624   IMM("imm0_31");
625   IMM("imm0_31_m1");
626   IMM("imm1_16");
627   IMM("imm1_32");
628   IMM("nModImm");
629   IMM("nImmSplatI8");
630   IMM("nImmSplatI16");
631   IMM("nImmSplatI32");
632   IMM("nImmSplatI64");
633   IMM("nImmVMOVI32");
634   IMM("nImmVMOVF32");
635   IMM("imm8");
636   IMM("imm16");
637   IMM("imm32");
638   IMM("imm1_7");
639   IMM("imm1_15");
640   IMM("imm1_31");
641   IMM("imm0_1");
642   IMM("imm0_3");
643   IMM("imm0_7");
644   IMM("imm0_15");
645   IMM("imm0_255");
646   IMM("imm0_4095");
647   IMM("imm0_65535");
648   IMM("imm0_65535_expr");
649   IMM("imm24b");
650   IMM("pkh_lsl_amt");
651   IMM("pkh_asr_amt");
652   IMM("jt2block_operand");
653   IMM("t_imm0_1020s4");
654   IMM("t_imm0_508s4");
655   IMM("pclabel");
656   IMM("adrlabel");
657   IMM("t_adrlabel");
658   IMM("t2adrlabel");
659   IMM("shift_imm");
660   IMM("t2_shift_imm");
661   IMM("neon_vcvt_imm32");
662   IMM("shr_imm8");
663   IMM("shr_imm16");
664   IMM("shr_imm32");
665   IMM("shr_imm64");
666   IMM("t2ldrlabel");
667   IMM("postidx_imm8");
668   IMM("postidx_imm8s4");
669   IMM("imm_sr");
670   IMM("imm1_31");
671   IMM("VectorIndex8");
672   IMM("VectorIndex16");
673   IMM("VectorIndex32");
674
675   MISC("brtarget", "kOperandTypeARMBranchTarget");                // ?
676   MISC("uncondbrtarget", "kOperandTypeARMBranchTarget");           // ?
677   MISC("t_brtarget", "kOperandTypeARMBranchTarget");              // ?
678   MISC("t_bcctarget", "kOperandTypeARMBranchTarget");             // ?
679   MISC("t_cbtarget", "kOperandTypeARMBranchTarget");              // ?
680   MISC("bltarget", "kOperandTypeARMBranchTarget");                // ?
681
682   MISC("br_target", "kOperandTypeARMBranchTarget");                // ?
683   MISC("bl_target", "kOperandTypeARMBranchTarget");                // ?
684   MISC("blx_target", "kOperandTypeARMBranchTarget");                // ?
685
686   MISC("t_bltarget", "kOperandTypeARMBranchTarget");              // ?
687   MISC("t_blxtarget", "kOperandTypeARMBranchTarget");             // ?
688   MISC("so_reg_imm", "kOperandTypeARMSoRegReg");                         // R, R, I
689   MISC("so_reg_reg", "kOperandTypeARMSoRegImm");                         // R, R, I
690   MISC("shift_so_reg_reg", "kOperandTypeARMSoRegReg");                   // R, R, I
691   MISC("shift_so_reg_imm", "kOperandTypeARMSoRegImm");                   // R, R, I
692   MISC("t2_so_reg", "kOperandTypeThumb2SoReg");                   // R, I
693   MISC("so_imm", "kOperandTypeARMSoImm");                         // I
694   MISC("rot_imm", "kOperandTypeARMRotImm");                       // I
695   MISC("t2_so_imm", "kOperandTypeThumb2SoImm");                   // I
696   MISC("so_imm2part", "kOperandTypeARMSoImm2Part");               // I
697   MISC("pred", "kOperandTypeARMPredicate");                       // I, R
698   MISC("it_pred", "kOperandTypeARMPredicate");                    // I
699   MISC("addrmode_imm12", "kOperandTypeAddrModeImm12");            // R, I
700   MISC("ldst_so_reg", "kOperandTypeLdStSOReg");                   // R, R, I
701   MISC("postidx_reg", "kOperandTypeARMAddrMode3Offset");          // R, I
702   MISC("addrmode2", "kOperandTypeARMAddrMode2");                  // R, R, I
703   MISC("am2offset_reg", "kOperandTypeARMAddrMode2Offset");        // R, I
704   MISC("am2offset_imm", "kOperandTypeARMAddrMode2Offset");        // R, I
705   MISC("addrmode3", "kOperandTypeARMAddrMode3");                  // R, R, I
706   MISC("am3offset", "kOperandTypeARMAddrMode3Offset");            // R, I
707   MISC("ldstm_mode", "kOperandTypeARMLdStmMode");                 // I
708   MISC("addrmode5", "kOperandTypeARMAddrMode5");                  // R, I
709   MISC("addrmode6", "kOperandTypeARMAddrMode6");                  // R, R, I, I
710   MISC("am6offset", "kOperandTypeARMAddrMode6Offset");            // R, I, I
711   MISC("addrmode6dup", "kOperandTypeARMAddrMode6");               // R, R, I, I
712   MISC("addrmode6oneL32", "kOperandTypeARMAddrMode6");            // R, R, I, I
713   MISC("addrmodepc", "kOperandTypeARMAddrModePC");                // R, I
714   MISC("addr_offset_none", "kOperandTypeARMAddrMode7");           // R
715   MISC("reglist", "kOperandTypeARMRegisterList");                 // I, R, ...
716   MISC("dpr_reglist", "kOperandTypeARMDPRRegisterList");          // I, R, ...
717   MISC("spr_reglist", "kOperandTypeARMSPRRegisterList");          // I, R, ...
718   MISC("it_mask", "kOperandTypeThumbITMask");                     // I
719   MISC("t2addrmode_reg", "kOperandTypeThumb2AddrModeReg");        // R
720   MISC("t2addrmode_posimm8", "kOperandTypeThumb2AddrModeImm8");   // R, I
721   MISC("t2addrmode_negimm8", "kOperandTypeThumb2AddrModeImm8");   // R, I
722   MISC("t2addrmode_imm8", "kOperandTypeThumb2AddrModeImm8");      // R, I
723   MISC("t2am_imm8_offset", "kOperandTypeThumb2AddrModeImm8Offset");//I
724   MISC("t2addrmode_imm12", "kOperandTypeThumb2AddrModeImm12");    // R, I
725   MISC("t2addrmode_so_reg", "kOperandTypeThumb2AddrModeSoReg");   // R, R, I
726   MISC("t2addrmode_imm8s4", "kOperandTypeThumb2AddrModeImm8s4");  // R, I
727   MISC("t2addrmode_imm0_1020s4", "kOperandTypeThumb2AddrModeImm8s4");  // R, I
728   MISC("t2am_imm8s4_offset", "kOperandTypeThumb2AddrModeImm8s4Offset");
729                                                                   // R, I
730   MISC("tb_addrmode", "kOperandTypeARMTBAddrMode");               // I
731   MISC("t_addrmode_rrs1", "kOperandTypeThumbAddrModeRegS1");      // R, R
732   MISC("t_addrmode_rrs2", "kOperandTypeThumbAddrModeRegS2");      // R, R
733   MISC("t_addrmode_rrs4", "kOperandTypeThumbAddrModeRegS4");      // R, R
734   MISC("t_addrmode_is1", "kOperandTypeThumbAddrModeImmS1");       // R, I
735   MISC("t_addrmode_is2", "kOperandTypeThumbAddrModeImmS2");       // R, I
736   MISC("t_addrmode_is4", "kOperandTypeThumbAddrModeImmS4");       // R, I
737   MISC("t_addrmode_rr", "kOperandTypeThumbAddrModeRR");           // R, R
738   MISC("t_addrmode_sp", "kOperandTypeThumbAddrModeSP");           // R, I
739   MISC("t_addrmode_pc", "kOperandTypeThumbAddrModePC");           // R, I
740   MISC("addrmode_tbb", "kOperandTypeThumbAddrModeRR");            // R, R
741   MISC("addrmode_tbh", "kOperandTypeThumbAddrModeRR");            // R, R
742
743   return 1;
744 }
745
746 #undef REG
747 #undef MEM
748 #undef MISC
749
750 #undef SET
751
752 /// ARMPopulateOperands - Handles all the operands in an ARM instruction, adding
753 ///   the appropriate flags to their descriptors
754 ///
755 /// \param operandTypes A reference the array of operand type objects
756 /// \param inst         The instruction to use as a source of information
757 static void ARMPopulateOperands(
758   LiteralConstantEmitter *(&operandTypes)[EDIS_MAX_OPERANDS],
759   const CodeGenInstruction &inst) {
760   if (!inst.TheDef->isSubClassOf("InstARM") &&
761       !inst.TheDef->isSubClassOf("InstThumb"))
762     return;
763
764   unsigned int index;
765   unsigned int numOperands = inst.Operands.size();
766
767   if (numOperands > EDIS_MAX_OPERANDS) {
768     errs() << "numOperands == " << numOperands << " > " <<
769       EDIS_MAX_OPERANDS << '\n';
770     llvm_unreachable("Too many operands");
771   }
772
773   for (index = 0; index < numOperands; ++index) {
774     const CGIOperandList::OperandInfo &operandInfo = inst.Operands[index];
775     Record &rec = *operandInfo.Rec;
776
777     if (ARMFlagFromOpName(operandTypes[index], rec.getName())) {
778       errs() << "Operand type: " << rec.getName() << '\n';
779       errs() << "Operand name: " << operandInfo.Name << '\n';
780       errs() << "Instruction name: " << inst.TheDef->getName() << '\n';
781       PrintFatalError("Unhandled type in EDEmitter");
782     }
783   }
784 }
785
786 #define BRANCH(target) {                    \
787   instType.set("kInstructionTypeBranch");   \
788   DECORATE1(target, "kOperandFlagTarget");  \
789 }
790
791 /// ARMExtractSemantics - Performs various checks on the name of an ARM
792 ///   instruction to determine what sort of an instruction it is and then adds
793 ///   the appropriate flags to the instruction and its operands
794 ///
795 /// \param instType     A reference to the type for the instruction as a whole
796 /// \param operandTypes A reference to the array of operand type object pointers
797 /// \param operandFlags A reference to the array of operand flag object pointers
798 /// \param inst         A reference to the original instruction
799 static void ARMExtractSemantics(
800   LiteralConstantEmitter &instType,
801   LiteralConstantEmitter *(&operandTypes)[EDIS_MAX_OPERANDS],
802   FlagsConstantEmitter *(&operandFlags)[EDIS_MAX_OPERANDS],
803   const CodeGenInstruction &inst) {
804   const std::string &name = inst.TheDef->getName();
805
806   if (name == "tBcc"   ||
807       name == "tB"     ||
808       name == "t2Bcc"  ||
809       name == "Bcc"    ||
810       name == "tCBZ"   ||
811       name == "tCBNZ") {
812     BRANCH("target");
813   }
814
815   if (name == "tBLr9"      ||
816       name == "BLr9_pred"  ||
817       name == "tBLXi_r9"   ||
818       name == "tBLXr_r9"   ||
819       name == "BLXr9"      ||
820       name == "t2BXJ"      ||
821       name == "BXJ") {
822     BRANCH("func");
823
824     unsigned opIndex;
825     opIndex = inst.Operands.getOperandNamed("func");
826     if (operandTypes[opIndex]->is("kOperandTypeImmediate"))
827       operandTypes[opIndex]->set("kOperandTypeARMBranchTarget");
828   }
829 }
830
831 #undef BRANCH
832
833 /// populateInstInfo - Fills an array of InstInfos with information about each
834 ///   instruction in a target
835 ///
836 /// \param infoArray The array of InstInfo objects to populate
837 /// \param target    The CodeGenTarget to use as a source of instructions
838 static void populateInstInfo(CompoundConstantEmitter &infoArray,
839                              CodeGenTarget &target) {
840   const std::vector<const CodeGenInstruction*> &numberedInstructions =
841     target.getInstructionsByEnumValue();
842
843   unsigned int index;
844   unsigned int numInstructions = numberedInstructions.size();
845
846   for (index = 0; index < numInstructions; ++index) {
847     const CodeGenInstruction& inst = *numberedInstructions[index];
848
849     CompoundConstantEmitter *infoStruct = new CompoundConstantEmitter;
850     infoArray.addEntry(infoStruct);
851
852     LiteralConstantEmitter *instType = new LiteralConstantEmitter;
853     infoStruct->addEntry(instType);
854
855     LiteralConstantEmitter *numOperandsEmitter =
856       new LiteralConstantEmitter(inst.Operands.size());
857     infoStruct->addEntry(numOperandsEmitter);
858
859     CompoundConstantEmitter *operandTypeArray = new CompoundConstantEmitter;
860     infoStruct->addEntry(operandTypeArray);
861
862     LiteralConstantEmitter *operandTypes[EDIS_MAX_OPERANDS];
863
864     CompoundConstantEmitter *operandFlagArray = new CompoundConstantEmitter;
865     infoStruct->addEntry(operandFlagArray);
866
867     FlagsConstantEmitter *operandFlags[EDIS_MAX_OPERANDS];
868
869     for (unsigned operandIndex = 0;
870          operandIndex < EDIS_MAX_OPERANDS;
871          ++operandIndex) {
872       operandTypes[operandIndex] = new LiteralConstantEmitter;
873       operandTypeArray->addEntry(operandTypes[operandIndex]);
874
875       operandFlags[operandIndex] = new FlagsConstantEmitter;
876       operandFlagArray->addEntry(operandFlags[operandIndex]);
877     }
878
879     unsigned numSyntaxes = 0;
880
881     // We don't need to do anything for pseudo-instructions, as we'll never
882     // see them here. We'll only see real instructions.
883     // We still need to emit null initializers for everything.
884     if (!inst.isPseudo) {
885       if (target.getName() == "X86") {
886         X86PopulateOperands(operandTypes, inst);
887         X86ExtractSemantics(*instType, operandFlags, inst);
888         numSyntaxes = 2;
889       }
890       else if (target.getName() == "ARM") {
891         ARMPopulateOperands(operandTypes, inst);
892         ARMExtractSemantics(*instType, operandTypes, operandFlags, inst);
893         numSyntaxes = 1;
894       }
895     }
896
897     CompoundConstantEmitter *operandOrderArray = new CompoundConstantEmitter;
898
899     infoStruct->addEntry(operandOrderArray);
900
901     for (unsigned syntaxIndex = 0;
902          syntaxIndex < EDIS_MAX_SYNTAXES;
903          ++syntaxIndex) {
904       CompoundConstantEmitter *operandOrder =
905         new CompoundConstantEmitter(EDIS_MAX_OPERANDS);
906
907       operandOrderArray->addEntry(operandOrder);
908
909       if (syntaxIndex < numSyntaxes) {
910         populateOperandOrder(operandOrder, inst, syntaxIndex);
911       }
912     }
913
914     infoStruct = NULL;
915   }
916 }
917
918 static void emitCommonEnums(raw_ostream &o, unsigned int &i) {
919   EnumEmitter operandTypes("OperandTypes");
920   operandTypes.addEntry("kOperandTypeNone");
921   operandTypes.addEntry("kOperandTypeImmediate");
922   operandTypes.addEntry("kOperandTypeRegister");
923   operandTypes.addEntry("kOperandTypeX86Memory");
924   operandTypes.addEntry("kOperandTypeX86EffectiveAddress");
925   operandTypes.addEntry("kOperandTypeX86PCRelative");
926   operandTypes.addEntry("kOperandTypeARMBranchTarget");
927   operandTypes.addEntry("kOperandTypeARMSoRegReg");
928   operandTypes.addEntry("kOperandTypeARMSoRegImm");
929   operandTypes.addEntry("kOperandTypeARMSoImm");
930   operandTypes.addEntry("kOperandTypeARMRotImm");
931   operandTypes.addEntry("kOperandTypeARMSoImm2Part");
932   operandTypes.addEntry("kOperandTypeARMPredicate");
933   operandTypes.addEntry("kOperandTypeAddrModeImm12");
934   operandTypes.addEntry("kOperandTypeLdStSOReg");
935   operandTypes.addEntry("kOperandTypeARMAddrMode2");
936   operandTypes.addEntry("kOperandTypeARMAddrMode2Offset");
937   operandTypes.addEntry("kOperandTypeARMAddrMode3");
938   operandTypes.addEntry("kOperandTypeARMAddrMode3Offset");
939   operandTypes.addEntry("kOperandTypeARMLdStmMode");
940   operandTypes.addEntry("kOperandTypeARMAddrMode5");
941   operandTypes.addEntry("kOperandTypeARMAddrMode6");
942   operandTypes.addEntry("kOperandTypeARMAddrMode6Offset");
943   operandTypes.addEntry("kOperandTypeARMAddrMode7");
944   operandTypes.addEntry("kOperandTypeARMAddrModePC");
945   operandTypes.addEntry("kOperandTypeARMRegisterList");
946   operandTypes.addEntry("kOperandTypeARMDPRRegisterList");
947   operandTypes.addEntry("kOperandTypeARMSPRRegisterList");
948   operandTypes.addEntry("kOperandTypeARMTBAddrMode");
949   operandTypes.addEntry("kOperandTypeThumbITMask");
950   operandTypes.addEntry("kOperandTypeThumbAddrModeImmS1");
951   operandTypes.addEntry("kOperandTypeThumbAddrModeImmS2");
952   operandTypes.addEntry("kOperandTypeThumbAddrModeImmS4");
953   operandTypes.addEntry("kOperandTypeThumbAddrModeRegS1");
954   operandTypes.addEntry("kOperandTypeThumbAddrModeRegS2");
955   operandTypes.addEntry("kOperandTypeThumbAddrModeRegS4");
956   operandTypes.addEntry("kOperandTypeThumbAddrModeRR");
957   operandTypes.addEntry("kOperandTypeThumbAddrModeSP");
958   operandTypes.addEntry("kOperandTypeThumbAddrModePC");
959   operandTypes.addEntry("kOperandTypeThumb2AddrModeReg");
960   operandTypes.addEntry("kOperandTypeThumb2SoReg");
961   operandTypes.addEntry("kOperandTypeThumb2SoImm");
962   operandTypes.addEntry("kOperandTypeThumb2AddrModeImm8");
963   operandTypes.addEntry("kOperandTypeThumb2AddrModeImm8Offset");
964   operandTypes.addEntry("kOperandTypeThumb2AddrModeImm12");
965   operandTypes.addEntry("kOperandTypeThumb2AddrModeSoReg");
966   operandTypes.addEntry("kOperandTypeThumb2AddrModeImm8s4");
967   operandTypes.addEntry("kOperandTypeThumb2AddrModeImm8s4Offset");
968   operandTypes.emit(o, i);
969
970   o << "\n";
971
972   EnumEmitter operandFlags("OperandFlags");
973   operandFlags.addEntry("kOperandFlagSource");
974   operandFlags.addEntry("kOperandFlagTarget");
975   operandFlags.emitAsFlags(o, i);
976
977   o << "\n";
978
979   EnumEmitter instructionTypes("InstructionTypes");
980   instructionTypes.addEntry("kInstructionTypeNone");
981   instructionTypes.addEntry("kInstructionTypeMove");
982   instructionTypes.addEntry("kInstructionTypeBranch");
983   instructionTypes.addEntry("kInstructionTypePush");
984   instructionTypes.addEntry("kInstructionTypePop");
985   instructionTypes.addEntry("kInstructionTypeCall");
986   instructionTypes.addEntry("kInstructionTypeReturn");
987   instructionTypes.emit(o, i);
988
989   o << "\n";
990 }
991
992 namespace llvm {
993
994 void EmitEnhancedDisassemblerInfo(RecordKeeper &RK, raw_ostream &OS) {
995   emitSourceFileHeader("Enhanced Disassembler Info", OS);
996   unsigned int i = 0;
997
998   CompoundConstantEmitter infoArray;
999   CodeGenTarget target(RK);
1000
1001   populateInstInfo(infoArray, target);
1002
1003   emitCommonEnums(OS, i);
1004
1005   OS << "static const llvm::EDInstInfo instInfo"
1006      << target.getName() << "[] = ";
1007   infoArray.emit(OS, i);
1008   OS << ";" << "\n";
1009 }
1010
1011 } // End llvm namespace