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