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