Call static functions so that they aren't left unused.
[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 ConstantEmitter {
88   public:
89     virtual ~ConstantEmitter() { }
90     virtual void emit(raw_ostream &o, unsigned int &i) = 0;
91   };
92
93   class LiteralConstantEmitter : public ConstantEmitter {
94   private:
95     bool IsNumber;
96     union {
97       int Number;
98       const char* String;
99     };
100   public:
101     LiteralConstantEmitter(int number = 0) :
102       IsNumber(true),
103       Number(number) {
104     }
105     void set(const char *string) {
106       IsNumber = false;
107       Number = 0;
108       String = string;
109     }
110     bool is(const char *string) {
111       return !strcmp(String, string);
112     }
113     void emit(raw_ostream &o, unsigned int &i) {
114       if (IsNumber)
115         o << Number;
116       else
117         o << String;
118     }
119   };
120
121   class CompoundConstantEmitter : public ConstantEmitter {
122   private:
123     unsigned int Padding;
124     std::vector<ConstantEmitter *> Entries;
125   public:
126     CompoundConstantEmitter(unsigned int padding = 0) : Padding(padding) {
127     }
128     CompoundConstantEmitter &addEntry(ConstantEmitter *e) {
129       Entries.push_back(e);
130
131       return *this;
132     }
133     ~CompoundConstantEmitter() {
134       while (Entries.size()) {
135         ConstantEmitter *entry = Entries.back();
136         Entries.pop_back();
137         delete entry;
138       }
139     }
140     void emit(raw_ostream &o, unsigned int &i) {
141       o << "{" << "\n";
142       i += 2;
143
144       unsigned int index;
145       unsigned int numEntries = Entries.size();
146
147       unsigned int numToPrint;
148
149       if (Padding) {
150         if (numEntries > Padding) {
151           fprintf(stderr, "%u entries but %u padding\n", numEntries, Padding);
152           llvm_unreachable("More entries than padding");
153         }
154         numToPrint = Padding;
155       } else {
156         numToPrint = numEntries;
157       }
158
159       for (index = 0; index < numToPrint; ++index) {
160         o.indent(i);
161         if (index < numEntries)
162           Entries[index]->emit(o, i);
163         else
164           o << "-1";
165
166         if (index < (numToPrint - 1))
167           o << ",";
168         o << "\n";
169       }
170
171       i -= 2;
172       o.indent(i) << "}";
173     }
174   };
175
176   class FlagsConstantEmitter : public ConstantEmitter {
177   private:
178     std::vector<std::string> Flags;
179   public:
180     FlagsConstantEmitter() {
181     }
182     FlagsConstantEmitter &addEntry(const char *f) {
183       Flags.push_back(std::string(f));
184       return *this;
185     }
186     void emit(raw_ostream &o, unsigned int &i) {
187       unsigned int index;
188       unsigned int numFlags = Flags.size();
189       if (numFlags == 0)
190         o << "0";
191
192       for (index = 0; index < numFlags; ++index) {
193         o << Flags[index].c_str();
194         if (index < (numFlags - 1))
195           o << " | ";
196       }
197     }
198   };
199 }
200
201 EDEmitter::EDEmitter(RecordKeeper &R) : Records(R) {
202 }
203
204 /// populateOperandOrder - Accepts a CodeGenInstruction and generates its
205 ///   AsmWriterInst for the desired assembly syntax, giving an ordered list of
206 ///   operands in the order they appear in the printed instruction.  Then, for
207 ///   each entry in that list, determines the index of the same operand in the
208 ///   CodeGenInstruction, and emits the resulting mapping into an array, filling
209 ///   in unused slots with -1.
210 ///
211 /// @arg operandOrder - The array that will be populated with the operand
212 ///                     mapping.  Each entry will contain -1 (invalid index
213 ///                     into the operands present in the AsmString) or a number
214 ///                     representing an index in the operand descriptor array.
215 /// @arg inst         - The instruction to use when looking up the operands
216 /// @arg syntax       - The syntax to use, according to LLVM's enumeration
217 void populateOperandOrder(CompoundConstantEmitter *operandOrder,
218                           const CodeGenInstruction &inst,
219                           unsigned syntax) {
220   unsigned int numArgs = 0;
221
222   AsmWriterInst awInst(inst, syntax, -1, -1);
223
224   std::vector<AsmWriterOperand>::iterator operandIterator;
225
226   for (operandIterator = awInst.Operands.begin();
227        operandIterator != awInst.Operands.end();
228        ++operandIterator) {
229     if (operandIterator->OperandType ==
230         AsmWriterOperand::isMachineInstrOperand) {
231       operandOrder->addEntry(
232         new LiteralConstantEmitter(operandIterator->CGIOpNo));
233       numArgs++;
234     }
235   }
236 }
237
238 /////////////////////////////////////////////////////
239 // Support functions for handling X86 instructions //
240 /////////////////////////////////////////////////////
241
242 #define SET(flag) { type->set(flag); return 0; }
243
244 #define REG(str) if (name == str) SET("kOperandTypeRegister");
245 #define MEM(str) if (name == str) SET("kOperandTypeX86Memory");
246 #define LEA(str) if (name == str) SET("kOperandTypeX86EffectiveAddress");
247 #define IMM(str) if (name == str) SET("kOperandTypeImmediate");
248 #define PCR(str) if (name == str) SET("kOperandTypeX86PCRelative");
249
250 /// X86TypeFromOpName - Processes the name of a single X86 operand (which is
251 ///   actually its type) and translates it into an operand type
252 ///
253 /// @arg flags    - The type object to set
254 /// @arg name     - The name of the operand
255 static int X86TypeFromOpName(LiteralConstantEmitter *type,
256                              const std::string &name) {
257   REG("GR8");
258   REG("GR8_NOREX");
259   REG("GR16");
260   REG("GR32");
261   REG("GR32_NOREX");
262   REG("GR32_TC");
263   REG("FR32");
264   REG("RFP32");
265   REG("GR64");
266   REG("GR64_TC");
267   REG("FR64");
268   REG("VR64");
269   REG("RFP64");
270   REG("RFP80");
271   REG("VR128");
272   REG("VR256");
273   REG("RST");
274   REG("SEGMENT_REG");
275   REG("DEBUG_REG");
276   REG("CONTROL_REG");
277
278   IMM("i8imm");
279   IMM("i16imm");
280   IMM("i16i8imm");
281   IMM("i32imm");
282   IMM("i32i8imm");
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("tcGPR");
559   REG("cc_out");
560   REG("s_cc_out");
561   REG("tGPR");
562   REG("DPR");
563   REG("DPR_VFP2");
564   REG("DPR_8");
565   REG("SPR");
566   REG("QPR");
567   REG("QQPR");
568   REG("QQQQPR");
569
570   IMM("i32imm");
571   IMM("i32imm_hilo16");
572   IMM("bf_inv_mask_imm");
573   IMM("lsb_pos_imm");
574   IMM("width_imm");
575   IMM("jtblock_operand");
576   IMM("nohash_imm");
577   IMM("p_imm");
578   IMM("c_imm");
579   IMM("imod_op");
580   IMM("iflags_op");
581   IMM("cpinst_operand");
582   IMM("setend_op");
583   IMM("cps_opt");
584   IMM("vfp_f64imm");
585   IMM("vfp_f32imm");
586   IMM("memb_opt");
587   IMM("msr_mask");
588   IMM("neg_zero");
589   IMM("imm0_31");
590   IMM("imm0_31_m1");
591   IMM("nModImm");
592   IMM("imm0_4095");
593   IMM("jt2block_operand");
594   IMM("t_imm_s4");
595   IMM("pclabel");
596   IMM("adrlabel");
597   IMM("t_adrlabel");
598   IMM("t2adrlabel");
599   IMM("shift_imm");
600   IMM("neon_vcvt_imm32");
601   IMM("shr_imm8");
602   IMM("shr_imm16");
603   IMM("shr_imm32");
604   IMM("shr_imm64");
605   IMM("t2ldrlabel");
606
607   MISC("brtarget", "kOperandTypeARMBranchTarget");                // ?
608   MISC("uncondbrtarget", "kOperandTypeARMBranchTarget");           // ?
609   MISC("t_brtarget", "kOperandTypeARMBranchTarget");              // ?
610   MISC("t_bcctarget", "kOperandTypeARMBranchTarget");             // ?
611   MISC("t_cbtarget", "kOperandTypeARMBranchTarget");              // ?
612   MISC("bltarget", "kOperandTypeARMBranchTarget");                // ?
613
614   MISC("br_target", "kOperandTypeARMBranchTarget");                // ?
615   MISC("bl_target", "kOperandTypeARMBranchTarget");                // ?
616
617   MISC("t_bltarget", "kOperandTypeARMBranchTarget");              // ?
618   MISC("t_blxtarget", "kOperandTypeARMBranchTarget");             // ?
619   MISC("so_reg", "kOperandTypeARMSoReg");                         // R, R, I
620   MISC("shift_so_reg", "kOperandTypeARMSoReg");                   // R, R, I
621   MISC("t2_so_reg", "kOperandTypeThumb2SoReg");                   // R, I
622   MISC("so_imm", "kOperandTypeARMSoImm");                         // I
623   MISC("rot_imm", "kOperandTypeARMRotImm");                       // I
624   MISC("t2_so_imm", "kOperandTypeThumb2SoImm");                   // I
625   MISC("so_imm2part", "kOperandTypeARMSoImm2Part");               // I
626   MISC("pred", "kOperandTypeARMPredicate");                       // I, R
627   MISC("it_pred", "kOperandTypeARMPredicate");                    // I
628   MISC("addrmode_imm12", "kOperandTypeAddrModeImm12");            // R, I
629   MISC("ldst_so_reg", "kOperandTypeLdStSOReg");                   // R, R, I
630   MISC("addrmode2", "kOperandTypeARMAddrMode2");                  // R, R, I
631   MISC("am2offset", "kOperandTypeARMAddrMode2Offset");            // R, I
632   MISC("addrmode3", "kOperandTypeARMAddrMode3");                  // R, R, I
633   MISC("am3offset", "kOperandTypeARMAddrMode3Offset");            // R, I
634   MISC("ldstm_mode", "kOperandTypeARMLdStmMode");                 // I
635   MISC("addrmode5", "kOperandTypeARMAddrMode5");                  // R, I
636   MISC("addrmode6", "kOperandTypeARMAddrMode6");                  // R, R, I, I
637   MISC("am6offset", "kOperandTypeARMAddrMode6Offset");            // R, I, I
638   MISC("addrmode6dup", "kOperandTypeARMAddrMode6");               // R, R, I, I
639   MISC("addrmodepc", "kOperandTypeARMAddrModePC");                // R, I
640   MISC("reglist", "kOperandTypeARMRegisterList");                 // I, R, ...
641   MISC("dpr_reglist", "kOperandTypeARMDPRRegisterList");          // I, R, ...
642   MISC("spr_reglist", "kOperandTypeARMSPRRegisterList");          // I, R, ...
643   MISC("it_mask", "kOperandTypeThumbITMask");                     // I
644   MISC("t2addrmode_imm8", "kOperandTypeThumb2AddrModeImm8");      // R, I
645   MISC("t2am_imm8_offset", "kOperandTypeThumb2AddrModeImm8Offset");//I
646   MISC("t2addrmode_imm12", "kOperandTypeThumb2AddrModeImm12");    // R, I
647   MISC("t2addrmode_so_reg", "kOperandTypeThumb2AddrModeSoReg");   // R, R, I
648   MISC("t2addrmode_imm8s4", "kOperandTypeThumb2AddrModeImm8s4");  // R, I
649   MISC("t2am_imm8s4_offset", "kOperandTypeThumb2AddrModeImm8s4Offset");
650                                                                   // R, I
651   MISC("tb_addrmode", "kOperandTypeARMTBAddrMode");               // I
652   MISC("t_addrmode_rrs1", "kOperandTypeThumbAddrModeRegS");       // R, R
653   MISC("t_addrmode_rrs2", "kOperandTypeThumbAddrModeRegS");       // R, R
654   MISC("t_addrmode_rrs4", "kOperandTypeThumbAddrModeRegS");       // R, R
655   MISC("t_addrmode_is1", "kOperandTypeThumbAddrModeImmS");        // R, I
656   MISC("t_addrmode_is2", "kOperandTypeThumbAddrModeImmS");        // R, I
657   MISC("t_addrmode_is4", "kOperandTypeThumbAddrModeImmS");        // R, I
658   MISC("t_addrmode_rr", "kOperandTypeThumbAddrModeRR");           // R, R
659   MISC("t_addrmode_sp", "kOperandTypeThumbAddrModeSP");           // R, I
660   MISC("t_addrmode_pc", "kOperandTypeThumbAddrModePC");           // R, I
661
662   return 1;
663 }
664
665 #undef SOREG
666 #undef SOIMM
667 #undef PRED
668 #undef REG
669 #undef MEM
670 #undef LEA
671 #undef IMM
672 #undef PCR
673
674 #undef SET
675
676 /// ARMPopulateOperands - Handles all the operands in an ARM instruction, adding
677 ///   the appropriate flags to their descriptors
678 ///
679 /// @operandFlags - A reference the array of operand flag objects
680 /// @inst         - The instruction to use as a source of information
681 static void ARMPopulateOperands(
682   LiteralConstantEmitter *(&operandTypes)[EDIS_MAX_OPERANDS],
683   const CodeGenInstruction &inst) {
684   if (!inst.TheDef->isSubClassOf("InstARM") &&
685       !inst.TheDef->isSubClassOf("InstThumb"))
686     return;
687
688   unsigned int index;
689   unsigned int numOperands = inst.Operands.size();
690
691   if (numOperands > EDIS_MAX_OPERANDS) {
692     errs() << "numOperands == " << numOperands << " > " <<
693       EDIS_MAX_OPERANDS << '\n';
694     llvm_unreachable("Too many operands");
695   }
696
697   for (index = 0; index < numOperands; ++index) {
698     const CGIOperandList::OperandInfo &operandInfo = inst.Operands[index];
699     Record &rec = *operandInfo.Rec;
700
701     if (ARMFlagFromOpName(operandTypes[index], rec.getName())) {
702       errs() << "Operand type: " << rec.getName() << '\n';
703       errs() << "Operand name: " << operandInfo.Name << '\n';
704       errs() << "Instruction name: " << inst.TheDef->getName() << '\n';
705       llvm_unreachable("Unhandled type");
706     }
707   }
708 }
709
710 #define BRANCH(target) {                    \
711   instType.set("kInstructionTypeBranch");   \
712   DECORATE1(target, "kOperandFlagTarget");  \
713 }
714
715 /// ARMExtractSemantics - Performs various checks on the name of an ARM
716 ///   instruction to determine what sort of an instruction it is and then adds
717 ///   the appropriate flags to the instruction and its operands
718 ///
719 /// @arg instType     - A reference to the type for the instruction as a whole
720 /// @arg operandTypes - A reference to the array of operand type object pointers
721 /// @arg operandFlags - A reference to the array of operand flag object pointers
722 /// @arg inst         - A reference to the original instruction
723 static void ARMExtractSemantics(
724   LiteralConstantEmitter &instType,
725   LiteralConstantEmitter *(&operandTypes)[EDIS_MAX_OPERANDS],
726   FlagsConstantEmitter *(&operandFlags)[EDIS_MAX_OPERANDS],
727   const CodeGenInstruction &inst) {
728   const std::string &name = inst.TheDef->getName();
729
730   if (name == "tBcc"   ||
731       name == "tB"     ||
732       name == "t2Bcc"  ||
733       name == "Bcc"    ||
734       name == "tCBZ"   ||
735       name == "tCBNZ") {
736     BRANCH("target");
737   }
738
739   if (name == "tBLr9"      ||
740       name == "BLr9_pred"  ||
741       name == "tBLXi_r9"   ||
742       name == "tBLXr_r9"   ||
743       name == "BLXr9"      ||
744       name == "t2BXJ"      ||
745       name == "BXJ") {
746     BRANCH("func");
747
748     unsigned opIndex;
749     opIndex = inst.Operands.getOperandNamed("func");
750     if (operandTypes[opIndex]->is("kOperandTypeImmediate"))
751       operandTypes[opIndex]->set("kOperandTypeARMBranchTarget");
752   }
753 }
754
755 #undef BRANCH
756
757 /// populateInstInfo - Fills an array of InstInfos with information about each
758 ///   instruction in a target
759 ///
760 /// @arg infoArray  - The array of InstInfo objects to populate
761 /// @arg target     - The CodeGenTarget to use as a source of instructions
762 static void populateInstInfo(CompoundConstantEmitter &infoArray,
763                              CodeGenTarget &target) {
764   const std::vector<const CodeGenInstruction*> &numberedInstructions =
765     target.getInstructionsByEnumValue();
766
767   unsigned int index;
768   unsigned int numInstructions = numberedInstructions.size();
769
770   for (index = 0; index < numInstructions; ++index) {
771     const CodeGenInstruction& inst = *numberedInstructions[index];
772
773     CompoundConstantEmitter *infoStruct = new CompoundConstantEmitter;
774     infoArray.addEntry(infoStruct);
775
776     LiteralConstantEmitter *instType = new LiteralConstantEmitter;
777     infoStruct->addEntry(instType);
778
779     LiteralConstantEmitter *numOperandsEmitter =
780       new LiteralConstantEmitter(inst.Operands.size());
781     infoStruct->addEntry(numOperandsEmitter);
782
783     CompoundConstantEmitter *operandTypeArray = new CompoundConstantEmitter;
784     infoStruct->addEntry(operandTypeArray);
785
786     LiteralConstantEmitter *operandTypes[EDIS_MAX_OPERANDS];
787
788     CompoundConstantEmitter *operandFlagArray = new CompoundConstantEmitter;
789     infoStruct->addEntry(operandFlagArray);
790
791     FlagsConstantEmitter *operandFlags[EDIS_MAX_OPERANDS];
792
793     for (unsigned operandIndex = 0;
794          operandIndex < EDIS_MAX_OPERANDS;
795          ++operandIndex) {
796       operandTypes[operandIndex] = new LiteralConstantEmitter;
797       operandTypeArray->addEntry(operandTypes[operandIndex]);
798
799       operandFlags[operandIndex] = new FlagsConstantEmitter;
800       operandFlagArray->addEntry(operandFlags[operandIndex]);
801     }
802
803     unsigned numSyntaxes = 0;
804
805     if (target.getName() == "X86") {
806       X86PopulateOperands(operandTypes, inst);
807       X86ExtractSemantics(*instType, operandFlags, inst);
808       numSyntaxes = 2;
809     }
810     else if (target.getName() == "ARM") {
811       ARMPopulateOperands(operandTypes, inst);
812       ARMExtractSemantics(*instType, operandTypes, operandFlags, inst);
813       numSyntaxes = 1;
814     }
815
816     CompoundConstantEmitter *operandOrderArray = new CompoundConstantEmitter;
817
818     infoStruct->addEntry(operandOrderArray);
819
820     for (unsigned syntaxIndex = 0;
821          syntaxIndex < EDIS_MAX_SYNTAXES;
822          ++syntaxIndex) {
823       CompoundConstantEmitter *operandOrder =
824         new CompoundConstantEmitter(EDIS_MAX_OPERANDS);
825
826       operandOrderArray->addEntry(operandOrder);
827
828       if (syntaxIndex < numSyntaxes) {
829         populateOperandOrder(operandOrder, inst, syntaxIndex);
830       }
831     }
832
833     infoStruct = NULL;
834   }
835 }
836
837 static void emitCommonEnums(raw_ostream &o, unsigned int &i) {
838   EnumEmitter operandTypes("OperandTypes");
839   operandTypes.addEntry("kOperandTypeNone");
840   operandTypes.addEntry("kOperandTypeImmediate");
841   operandTypes.addEntry("kOperandTypeRegister");
842   operandTypes.addEntry("kOperandTypeX86Memory");
843   operandTypes.addEntry("kOperandTypeX86EffectiveAddress");
844   operandTypes.addEntry("kOperandTypeX86PCRelative");
845   operandTypes.addEntry("kOperandTypeARMBranchTarget");
846   operandTypes.addEntry("kOperandTypeARMSoReg");
847   operandTypes.addEntry("kOperandTypeARMSoImm");
848   operandTypes.addEntry("kOperandTypeARMRotImm");
849   operandTypes.addEntry("kOperandTypeARMSoImm2Part");
850   operandTypes.addEntry("kOperandTypeARMPredicate");
851   operandTypes.addEntry("kOperandTypeAddrModeImm12");
852   operandTypes.addEntry("kOperandTypeLdStSOReg");
853   operandTypes.addEntry("kOperandTypeARMAddrMode2");
854   operandTypes.addEntry("kOperandTypeARMAddrMode2Offset");
855   operandTypes.addEntry("kOperandTypeARMAddrMode3");
856   operandTypes.addEntry("kOperandTypeARMAddrMode3Offset");
857   operandTypes.addEntry("kOperandTypeARMLdStmMode");
858   operandTypes.addEntry("kOperandTypeARMAddrMode5");
859   operandTypes.addEntry("kOperandTypeARMAddrMode6");
860   operandTypes.addEntry("kOperandTypeARMAddrMode6Offset");
861   operandTypes.addEntry("kOperandTypeARMAddrModePC");
862   operandTypes.addEntry("kOperandTypeARMRegisterList");
863   operandTypes.addEntry("kOperandTypeARMDPRRegisterList");
864   operandTypes.addEntry("kOperandTypeARMSPRRegisterList");
865   operandTypes.addEntry("kOperandTypeARMTBAddrMode");
866   operandTypes.addEntry("kOperandTypeThumbITMask");
867   operandTypes.addEntry("kOperandTypeThumbAddrModeRegS");
868   operandTypes.addEntry("kOperandTypeThumbAddrModeImmS");
869   operandTypes.addEntry("kOperandTypeThumbAddrModeRR");
870   operandTypes.addEntry("kOperandTypeThumbAddrModeSP");
871   operandTypes.addEntry("kOperandTypeThumbAddrModePC");
872   operandTypes.addEntry("kOperandTypeThumb2SoReg");
873   operandTypes.addEntry("kOperandTypeThumb2SoImm");
874   operandTypes.addEntry("kOperandTypeThumb2AddrModeImm8");
875   operandTypes.addEntry("kOperandTypeThumb2AddrModeImm8Offset");
876   operandTypes.addEntry("kOperandTypeThumb2AddrModeImm12");
877   operandTypes.addEntry("kOperandTypeThumb2AddrModeSoReg");
878   operandTypes.addEntry("kOperandTypeThumb2AddrModeImm8s4");
879   operandTypes.addEntry("kOperandTypeThumb2AddrModeImm8s4Offset");
880   operandTypes.emit(o, i);
881
882   o << "\n";
883
884   EnumEmitter operandFlags("OperandFlags");
885   operandFlags.addEntry("kOperandFlagSource");
886   operandFlags.addEntry("kOperandFlagTarget");
887   operandFlags.emitAsFlags(o, i);
888
889   o << "\n";
890
891   EnumEmitter instructionTypes("InstructionTypes");
892   instructionTypes.addEntry("kInstructionTypeNone");
893   instructionTypes.addEntry("kInstructionTypeMove");
894   instructionTypes.addEntry("kInstructionTypeBranch");
895   instructionTypes.addEntry("kInstructionTypePush");
896   instructionTypes.addEntry("kInstructionTypePop");
897   instructionTypes.addEntry("kInstructionTypeCall");
898   instructionTypes.addEntry("kInstructionTypeReturn");
899   instructionTypes.emit(o, i);
900
901   o << "\n";
902 }
903
904 void EDEmitter::run(raw_ostream &o) {
905   unsigned int i = 0;
906
907   CompoundConstantEmitter infoArray;
908   CodeGenTarget target(Records);
909
910   populateInstInfo(infoArray, target);
911
912   emitCommonEnums(o, i);
913
914   o << "namespace {\n";
915
916   o << "llvm::EDInstInfo instInfo" << target.getName().c_str() << "[] = ";
917   infoArray.emit(o, i);
918   o << ";" << "\n";
919
920   o << "}\n";
921 }