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