Fixed a nasty layering violation in the edis source
[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_32");
313   REG("CONTROL_REG_64");
314   
315   IMM("i8imm");
316   IMM("i16imm");
317   IMM("i16i8imm");
318   IMM("i32imm");
319   IMM("i32imm_pcrel");
320   IMM("i32i8imm");
321   IMM("i64imm");
322   IMM("i64i8imm");
323   IMM("i64i32imm");
324   IMM("i64i32imm_pcrel");
325   IMM("SSECC");
326   
327   // all R, I, R, I, R
328   MEM("i8mem");
329   MEM("i8mem_NOREX");
330   MEM("i16mem");
331   MEM("i32mem");
332   MEM("i32mem_TC");
333   MEM("f32mem");
334   MEM("ssmem");
335   MEM("opaque32mem");
336   MEM("opaque48mem");
337   MEM("i64mem");
338   MEM("i64mem_TC");
339   MEM("f64mem");
340   MEM("sdmem");
341   MEM("f80mem");
342   MEM("opaque80mem");
343   MEM("i128mem");
344   MEM("f128mem");
345   MEM("opaque512mem");
346   
347   // all R, I, R, I
348   LEA("lea32mem");
349   LEA("lea64_32mem");
350   LEA("lea64mem");
351   
352   // all I
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[name.length() - 1] == 'm') {
505       PUSH("src");
506     } else if (name.find("i") != name.npos) {
507       PUSH("imm");
508     } else {
509       PUSH("reg");
510     }
511   }
512   
513   if (name.find("POP") != name.npos) {
514     if (name.find("POPCNT") != name.npos) {
515       // ignore (not a real pop)
516     } else if (name.find("FS") != name.npos ||
517              name.find("GS") != name.npos) {
518       instType.set("kInstructionTypePop");
519       // TODO add support for fixed operands
520     } else if (name.find("F") != name.npos) {
521       // ignore (this pops from the FP stack)
522     } else if (name[name.length() - 1] == 'm') {
523       POP("dst");
524     } else {
525       POP("reg");
526     }
527   }
528   
529   if (name.find("CALL") != name.npos) {
530     if (name.find("ADJ") != name.npos) {
531       // ignore (not a call)
532     } else if (name.find("SYSCALL") != name.npos) {
533       // ignore (doesn't go anywhere we know about)
534     } else if (name.find("VMCALL") != name.npos) {
535       // ignore (rather different semantics than a regular call)
536     } else if (name.find("FAR") != name.npos && name.find("i") != name.npos) {
537       CALL("off");
538     } else {
539       CALL("dst");
540     }
541   }
542   
543   if (name.find("RET") != name.npos) {
544     RETURN();
545   }
546 }
547
548 #undef MOV
549 #undef BRANCH
550 #undef PUSH
551 #undef POP
552 #undef CALL
553 #undef RETURN
554
555 /////////////////////////////////////////////////////
556 // Support functions for handling ARM instructions //
557 /////////////////////////////////////////////////////
558
559 #define SET(flag) { type->set(flag); return 0; }
560
561 #define REG(str)    if (name == str) SET("kOperandTypeRegister");
562 #define IMM(str)    if (name == str) SET("kOperandTypeImmediate");
563
564 #define MISC(str, type)   if (name == str) SET(type);
565
566 /// ARMFlagFromOpName - Processes the name of a single ARM operand (which is
567 ///   actually its type) and translates it into an operand type
568 ///
569 /// @arg type     - The type object to set
570 /// @arg name     - The name of the operand
571 static int ARMFlagFromOpName(LiteralConstantEmitter *type,
572                              const std::string &name) {
573   REG("GPR");
574   REG("cc_out");
575   REG("s_cc_out");
576   REG("tGPR");
577   REG("DPR");
578   REG("SPR");
579   REG("QPR");
580   REG("DPR_VFP2");
581   REG("DPR_8");
582   
583   IMM("i32imm");
584   IMM("bf_inv_mask_imm");
585   IMM("jtblock_operand");
586   IMM("nohash_imm");
587   IMM("cpinst_operand");
588   IMM("cps_opt");
589   IMM("vfp_f64imm");
590   IMM("vfp_f32imm");
591   IMM("msr_mask");
592   IMM("neg_zero");
593   IMM("imm0_31");
594   IMM("h8imm");
595   IMM("h16imm");
596   IMM("h32imm");
597   IMM("h64imm");
598   IMM("imm0_4095");
599   IMM("jt2block_operand");
600   IMM("t_imm_s4");
601   IMM("pclabel");
602   
603   MISC("brtarget", "kOperandTypeARMBranchTarget");                // ?
604   MISC("so_reg", "kOperandTypeARMSoReg");                         // R, R, I
605   MISC("t2_so_reg", "kOperandTypeThumb2SoReg");                   // R, I
606   MISC("so_imm", "kOperandTypeARMSoImm");                         // I
607   MISC("t2_so_imm", "kOperandTypeThumb2SoImm");                   // I
608   MISC("so_imm2part", "kOperandTypeARMSoImm2Part");               // I
609   MISC("pred", "kOperandTypeARMPredicate");                       // I, R
610   MISC("it_pred", "kOperandTypeARMPredicate");                    // I
611   MISC("addrmode2", "kOperandTypeARMAddrMode2");                  // R, R, I
612   MISC("am2offset", "kOperandTypeARMAddrMode2Offset");            // R, I
613   MISC("addrmode3", "kOperandTypeARMAddrMode3");                  // R, R, I
614   MISC("am3offset", "kOperandTypeARMAddrMode3Offset");            // R, I
615   MISC("addrmode4", "kOperandTypeARMAddrMode4");                  // R, I
616   MISC("addrmode5", "kOperandTypeARMAddrMode5");                  // R, I
617   MISC("addrmode6", "kOperandTypeARMAddrMode6");                  // R, R, I, I
618   MISC("am6offset", "kOperandTypeARMAddrMode6Offset");            // R, I, I
619   MISC("addrmodepc", "kOperandTypeARMAddrModePC");                // R, I
620   MISC("reglist", "kOperandTypeARMRegisterList");                 // I, R, ...
621   MISC("it_mask", "kOperandTypeThumbITMask");                     // I
622   MISC("t2addrmode_imm8", "kOperandTypeThumb2AddrModeImm8");      // R, I
623   MISC("t2am_imm8_offset", "kOperandTypeThumb2AddrModeImm8Offset");//I
624   MISC("t2addrmode_imm12", "kOperandTypeThumb2AddrModeImm12");    // R, I
625   MISC("t2addrmode_so_reg", "kOperandTypeThumb2AddrModeSoReg");   // R, R, I
626   MISC("t2addrmode_imm8s4", "kOperandTypeThumb2AddrModeImm8s4");  // R, I
627   MISC("t2am_imm8s4_offset", "kOperandTypeThumb2AddrModeImm8s4Offset");  
628                                                                   // R, I
629   MISC("tb_addrmode", "kOperandTypeARMTBAddrMode");               // I
630   MISC("t_addrmode_s1", "kOperandTypeThumbAddrModeS1");           // R, I, R
631   MISC("t_addrmode_s2", "kOperandTypeThumbAddrModeS2");           // R, I, R
632   MISC("t_addrmode_s4", "kOperandTypeThumbAddrModeS4");           // R, I, R
633   MISC("t_addrmode_rr", "kOperandTypeThumbAddrModeRR");           // R, R
634   MISC("t_addrmode_sp", "kOperandTypeThumbAddrModeSP");           // R, I
635   
636   return 1;
637 }
638
639 #undef SOREG
640 #undef SOIMM
641 #undef PRED
642 #undef REG
643 #undef MEM
644 #undef LEA
645 #undef IMM
646 #undef PCR
647
648 #undef SET
649
650 /// ARMPopulateOperands - Handles all the operands in an ARM instruction, adding
651 ///   the appropriate flags to their descriptors
652 ///
653 /// @operandFlags - A reference the array of operand flag objects
654 /// @inst         - The instruction to use as a source of information
655 static void ARMPopulateOperands(
656   LiteralConstantEmitter *(&operandTypes)[EDIS_MAX_OPERANDS],
657   const CodeGenInstruction &inst) {
658   if (!inst.TheDef->isSubClassOf("InstARM") &&
659       !inst.TheDef->isSubClassOf("InstThumb"))
660     return;
661   
662   unsigned int index;
663   unsigned int numOperands = inst.OperandList.size();
664   
665   if (numOperands > EDIS_MAX_OPERANDS) {
666     errs() << "numOperands == " << numOperands << " > " << 
667       EDIS_MAX_OPERANDS << '\n';
668     llvm_unreachable("Too many operands");
669   }
670   
671   for (index = 0; index < numOperands; ++index) {
672     const CodeGenInstruction::OperandInfo &operandInfo = 
673     inst.OperandList[index];
674     Record &rec = *operandInfo.Rec;
675     
676     if (ARMFlagFromOpName(operandTypes[index], rec.getName())) {
677       errs() << "Operand type: " << rec.getName() << '\n';
678       errs() << "Operand name: " << operandInfo.Name << '\n';
679       errs() << "Instruction mame: " << inst.TheDef->getName() << '\n';
680       llvm_unreachable("Unhandled type");
681     }
682   }
683 }
684
685 #define BRANCH(target) {                    \
686   instType.set("kInstructionTypeBranch");   \
687   DECORATE1(target, "kOperandFlagTarget");  \
688 }
689
690 /// ARMExtractSemantics - Performs various checks on the name of an ARM
691 ///   instruction to determine what sort of an instruction it is and then adds 
692 ///   the appropriate flags to the instruction and its operands
693 ///
694 /// @arg instType     - A reference to the type for the instruction as a whole
695 /// @arg operandTypes - A reference to the array of operand type object pointers
696 /// @arg operandFlags - A reference to the array of operand flag object pointers
697 /// @arg inst         - A reference to the original instruction
698 static void ARMExtractSemantics(
699   LiteralConstantEmitter &instType,
700   LiteralConstantEmitter *(&operandTypes)[EDIS_MAX_OPERANDS],
701   FlagsConstantEmitter *(&operandFlags)[EDIS_MAX_OPERANDS],
702   const CodeGenInstruction &inst) {
703   const std::string &name = inst.TheDef->getName();
704   
705   if (name == "tBcc"   ||
706       name == "tB"     ||
707       name == "t2Bcc"  ||
708       name == "Bcc"    ||
709       name == "tCBZ"   ||
710       name == "tCBNZ") {
711     BRANCH("target");
712   }
713   
714   if (name == "tBLr9"      ||
715       name == "BLr9_pred"  ||
716       name == "tBLXi_r9"   ||
717       name == "tBLXr_r9"   ||
718       name == "BLXr9"      ||
719       name == "t2BXJ"      ||
720       name == "BXJ") {
721     BRANCH("func");
722     
723     unsigned opIndex;
724     opIndex = inst.getOperandNamed("func");
725     if (operandTypes[opIndex]->is("kOperandTypeImmediate"))
726       operandTypes[opIndex]->set("kOperandTypeARMBranchTarget");
727   }
728 }
729
730 #undef BRANCH
731
732 /// populateInstInfo - Fills an array of InstInfos with information about each 
733 ///   instruction in a target
734 ///
735 /// @arg infoArray  - The array of InstInfo objects to populate
736 /// @arg target     - The CodeGenTarget to use as a source of instructions
737 static void populateInstInfo(CompoundConstantEmitter &infoArray,
738                              CodeGenTarget &target) {
739   const std::vector<const CodeGenInstruction*> &numberedInstructions =
740     target.getInstructionsByEnumValue();
741   
742   unsigned int index;
743   unsigned int numInstructions = numberedInstructions.size();
744   
745   for (index = 0; index < numInstructions; ++index) {
746     const CodeGenInstruction& inst = *numberedInstructions[index];
747     
748     CompoundConstantEmitter *infoStruct = new CompoundConstantEmitter;
749     infoArray.addEntry(infoStruct);
750     
751     LiteralConstantEmitter *instType = new LiteralConstantEmitter;
752     infoStruct->addEntry(instType);
753     
754     LiteralConstantEmitter *numOperandsEmitter = 
755       new LiteralConstantEmitter(inst.OperandList.size());
756     infoStruct->addEntry(numOperandsEmitter);
757     
758     CompoundConstantEmitter *operandTypeArray = new CompoundConstantEmitter;
759     infoStruct->addEntry(operandTypeArray);
760     
761     LiteralConstantEmitter *operandTypes[EDIS_MAX_OPERANDS];
762                          
763     CompoundConstantEmitter *operandFlagArray = new CompoundConstantEmitter;
764     infoStruct->addEntry(operandFlagArray);
765         
766     FlagsConstantEmitter *operandFlags[EDIS_MAX_OPERANDS];
767     
768     for (unsigned operandIndex = 0; 
769          operandIndex < EDIS_MAX_OPERANDS; 
770          ++operandIndex) {
771       operandTypes[operandIndex] = new LiteralConstantEmitter;
772       operandTypeArray->addEntry(operandTypes[operandIndex]);
773       
774       operandFlags[operandIndex] = new FlagsConstantEmitter;
775       operandFlagArray->addEntry(operandFlags[operandIndex]);
776     }
777  
778     unsigned numSyntaxes = 0;
779     
780     if (target.getName() == "X86") {
781       X86PopulateOperands(operandTypes, inst);
782       X86ExtractSemantics(*instType, operandFlags, inst);
783       numSyntaxes = 2;
784     }
785     else if (target.getName() == "ARM") {
786       ARMPopulateOperands(operandTypes, inst);
787       ARMExtractSemantics(*instType, operandTypes, operandFlags, inst);
788       numSyntaxes = 1;
789     }
790     
791     CompoundConstantEmitter *operandOrderArray = new CompoundConstantEmitter;    
792     
793     infoStruct->addEntry(operandOrderArray);
794     
795     for (unsigned syntaxIndex = 0; 
796          syntaxIndex < EDIS_MAX_SYNTAXES; 
797          ++syntaxIndex) {
798       CompoundConstantEmitter *operandOrder = 
799         new CompoundConstantEmitter(EDIS_MAX_OPERANDS);
800       
801       operandOrderArray->addEntry(operandOrder);
802       
803       if (syntaxIndex < numSyntaxes) {
804         populateOperandOrder(operandOrder, inst, syntaxIndex);
805       }
806     }
807     
808     infoStruct = NULL;
809   }
810 }
811
812 static void emitCommonEnums(raw_ostream &o, unsigned int &i) {
813   EnumEmitter operandTypes("OperandTypes");
814   operandTypes.addEntry("kOperandTypeNone");
815   operandTypes.addEntry("kOperandTypeImmediate");
816   operandTypes.addEntry("kOperandTypeRegister");
817   operandTypes.addEntry("kOperandTypeX86Memory");
818   operandTypes.addEntry("kOperandTypeX86EffectiveAddress");
819   operandTypes.addEntry("kOperandTypeX86PCRelative");
820   operandTypes.addEntry("kOperandTypeARMBranchTarget");
821   operandTypes.addEntry("kOperandTypeARMSoReg");
822   operandTypes.addEntry("kOperandTypeARMSoImm");
823   operandTypes.addEntry("kOperandTypeARMSoImm2Part");
824   operandTypes.addEntry("kOperandTypeARMPredicate");
825   operandTypes.addEntry("kOperandTypeARMAddrMode2");
826   operandTypes.addEntry("kOperandTypeARMAddrMode2Offset");
827   operandTypes.addEntry("kOperandTypeARMAddrMode3");
828   operandTypes.addEntry("kOperandTypeARMAddrMode3Offset");
829   operandTypes.addEntry("kOperandTypeARMAddrMode4");
830   operandTypes.addEntry("kOperandTypeARMAddrMode5");
831   operandTypes.addEntry("kOperandTypeARMAddrMode6");
832   operandTypes.addEntry("kOperandTypeARMAddrMode6Offset");
833   operandTypes.addEntry("kOperandTypeARMAddrModePC");
834   operandTypes.addEntry("kOperandTypeARMRegisterList");
835   operandTypes.addEntry("kOperandTypeARMTBAddrMode");
836   operandTypes.addEntry("kOperandTypeThumbITMask");
837   operandTypes.addEntry("kOperandTypeThumbAddrModeS1");
838   operandTypes.addEntry("kOperandTypeThumbAddrModeS2");
839   operandTypes.addEntry("kOperandTypeThumbAddrModeS4");
840   operandTypes.addEntry("kOperandTypeThumbAddrModeRR");
841   operandTypes.addEntry("kOperandTypeThumbAddrModeSP");
842   operandTypes.addEntry("kOperandTypeThumb2SoReg");
843   operandTypes.addEntry("kOperandTypeThumb2SoImm");
844   operandTypes.addEntry("kOperandTypeThumb2AddrModeImm8");
845   operandTypes.addEntry("kOperandTypeThumb2AddrModeImm8Offset");
846   operandTypes.addEntry("kOperandTypeThumb2AddrModeImm12");
847   operandTypes.addEntry("kOperandTypeThumb2AddrModeSoReg");
848   operandTypes.addEntry("kOperandTypeThumb2AddrModeImm8s4");
849   operandTypes.addEntry("kOperandTypeThumb2AddrModeImm8s4Offset");
850   operandTypes.emit(o, i);
851   
852   o << "\n";
853   
854   EnumEmitter operandFlags("OperandFlags");
855   operandFlags.addEntry("kOperandFlagSource");
856   operandFlags.addEntry("kOperandFlagTarget");
857   operandFlags.emitAsFlags(o, i);
858   
859   o << "\n";
860   
861   EnumEmitter instructionTypes("InstructionTypes");
862   instructionTypes.addEntry("kInstructionTypeNone");
863   instructionTypes.addEntry("kInstructionTypeMove");
864   instructionTypes.addEntry("kInstructionTypeBranch");
865   instructionTypes.addEntry("kInstructionTypePush");
866   instructionTypes.addEntry("kInstructionTypePop");
867   instructionTypes.addEntry("kInstructionTypeCall");
868   instructionTypes.addEntry("kInstructionTypeReturn");
869   instructionTypes.emit(o, i);
870   
871   o << "\n";
872 }
873
874 void EDEmitter::run(raw_ostream &o) {
875   unsigned int i = 0;
876   
877   CompoundConstantEmitter infoArray;
878   CodeGenTarget target;
879   
880   populateInstInfo(infoArray, target);
881   
882   emitCommonEnums(o, i);
883   
884   o << "namespace {\n";
885   
886   o << "llvm::EDInstInfo instInfo" << target.getName().c_str() << "[] = ";
887   infoArray.emit(o, i);
888   o << ";" << "\n";
889   
890   o << "}\n";
891 }
892
893 void EDEmitter::runHeader(raw_ostream &o) {
894   EmitSourceFileHeader("Enhanced Disassembly Info Header", o);
895   
896   o << "#ifndef EDInfo_" << "\n";
897   o << "#define EDInfo_" << "\n";
898   o << "\n";
899   o << "#include <inttypes.h>" << "\n";
900   o << "\n";
901   o << "#define EDIS_MAX_OPERANDS " << format("%d", EDIS_MAX_OPERANDS) << "\n";
902   o << "#define EDIS_MAX_SYNTAXES " << format("%d", EDIS_MAX_SYNTAXES) << "\n";
903   o << "\n";
904   
905   unsigned int i = 0;
906   
907   emitCommonEnums(o, i);
908   
909   o << "\n";
910   o << "#endif" << "\n";
911 }