Updated the TableGen emitter for the Enhanced
[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/Support/ErrorHandling.h"
23 #include "llvm/Support/Format.h"
24 #include "llvm/Support/raw_ostream.h"
25
26 #include <vector>
27 #include <string>
28
29 #define MAX_OPERANDS 5
30 #define MAX_SYNTAXES 2
31
32 using namespace llvm;
33
34 ///////////////////////////////////////////////////////////
35 // Support classes for emitting nested C data structures //
36 ///////////////////////////////////////////////////////////
37
38 namespace {
39   
40   class EnumEmitter {
41   private:
42     std::string Name;
43     std::vector<std::string> Entries;
44   public:
45     EnumEmitter(const char *N) : Name(N) { 
46     }
47     int addEntry(const char *e) { 
48       Entries.push_back(std::string(e));
49       return Entries.size() - 1; 
50     }
51     void emit(raw_ostream &o, unsigned int &i) {
52       o.indent(i) << "enum " << Name.c_str() << " {" << "\n";
53       i += 2;
54       
55       unsigned int index = 0;
56       unsigned int numEntries = Entries.size();
57       for(index = 0; index < numEntries; ++index) {
58         o.indent(i) << Entries[index];
59         if(index < (numEntries - 1))
60           o << ",";
61         o << "\n";
62       }
63       
64       i -= 2;
65       o.indent(i) << "};" << "\n";
66     }
67     
68     void emitAsFlags(raw_ostream &o, unsigned int &i) {
69       o.indent(i) << "enum " << Name.c_str() << " {" << "\n";
70       i += 2;
71       
72       unsigned int index = 0;
73       unsigned int numEntries = Entries.size();
74       unsigned int flag = 1;
75       for (index = 0; index < numEntries; ++index) {
76         o.indent(i) << Entries[index] << " = " << format("0x%x", flag);
77         if (index < (numEntries - 1))
78           o << ",";
79         o << "\n";
80         flag <<= 1;
81       }
82       
83       i -= 2;
84       o.indent(i) << "};" << "\n";
85     }
86   };
87
88   class StructEmitter {
89   private:
90     std::string Name;
91     std::vector<std::string> MemberTypes;
92     std::vector<std::string> MemberNames;
93   public:
94     StructEmitter(const char *N) : Name(N) {
95     }
96     void addMember(const char *t, const char *n) {
97       MemberTypes.push_back(std::string(t));
98       MemberNames.push_back(std::string(n));
99     }
100     void emit(raw_ostream &o, unsigned int &i) {
101       o.indent(i) << "struct " << Name.c_str() << " {" << "\n";
102       i += 2;
103       
104       unsigned int index = 0;
105       unsigned int numMembers = MemberTypes.size();
106       for (index = 0; index < numMembers; ++index) {
107         o.indent(i) << MemberTypes[index] << " " << MemberNames[index] << ";";
108         o << "\n";
109       }
110       
111       i -= 2;
112       o.indent(i) << "};" << "\n";
113     }
114   };
115   
116   class ConstantEmitter {
117   public:
118     virtual ~ConstantEmitter() { }
119     virtual void emit(raw_ostream &o, unsigned int &i) = 0;
120   };
121   
122   class LiteralConstantEmitter : public ConstantEmitter {
123   private:
124     std::string Literal;
125   public:
126     LiteralConstantEmitter(const char *literal) : Literal(literal) {
127     }
128     LiteralConstantEmitter(int literal) {
129       char buf[256];
130       snprintf(buf, 256, "%d", literal);
131       Literal = buf;
132     }
133     void emit(raw_ostream &o, unsigned int &i) {
134       o << Literal;
135     }
136   };
137   
138   class CompoundConstantEmitter : public ConstantEmitter {
139   private:
140     std::vector<ConstantEmitter*> Entries;
141   public:
142     CompoundConstantEmitter() {
143     }
144     ~CompoundConstantEmitter() {
145       unsigned int index;
146       unsigned int numEntries = Entries.size();
147       for (index = 0; index < numEntries; ++index) {
148         delete Entries[index];
149       }
150     }
151     CompoundConstantEmitter &addEntry(ConstantEmitter *e) {
152       Entries.push_back(e);
153       return *this;
154     }
155     void emit(raw_ostream &o, unsigned int &i) {
156       o << "{" << "\n";
157       i += 2;
158   
159       unsigned int index;
160       unsigned int numEntries = Entries.size();
161       for (index = 0; index < numEntries; ++index) {
162         o.indent(i);
163         Entries[index]->emit(o, i);
164         if (index < (numEntries - 1))
165           o << ",";
166         o << "\n";
167       }
168       
169       i -= 2;
170       o.indent(i) << "}";
171     }
172   };
173   
174   class FlagsConstantEmitter : public ConstantEmitter {
175   private:
176     std::vector<std::string> Flags;
177   public:
178     FlagsConstantEmitter() {
179     }
180     FlagsConstantEmitter &addEntry(const char *f) {
181       Flags.push_back(std::string(f));
182       return *this;
183     }
184     void emit(raw_ostream &o, unsigned int &i) {
185       unsigned int index;
186       unsigned int numFlags = Flags.size();
187       if (numFlags == 0)
188         o << "0";
189       
190       for (index = 0; index < numFlags; ++index) {
191         o << Flags[index].c_str();
192         if (index < (numFlags - 1))
193           o << " | ";
194       }
195     }
196   };
197 }
198
199 EDEmitter::EDEmitter(RecordKeeper &R) : Records(R) {
200 }
201
202 /// populateOperandOrder - Accepts a CodeGenInstruction and generates its
203 ///   AsmWriterInst for the desired assembly syntax, giving an ordered list of
204 ///   operands in the order they appear in the printed instruction.  Then, for
205 ///   each entry in that list, determines the index of the same operand in the
206 ///   CodeGenInstruction, and emits the resulting mapping into an array, filling
207 ///   in unused slots with -1.
208 ///
209 /// @arg operandOrder - The array that will be populated with the operand
210 ///                     mapping.  Each entry will contain -1 (invalid index
211 ///                     into the operands present in the AsmString) or a number
212 ///                     representing an index in the operand descriptor array.
213 /// @arg inst         - The instruction to use when looking up the operands
214 /// @arg syntax       - The syntax to use, according to LLVM's enumeration
215 void populateOperandOrder(CompoundConstantEmitter *operandOrder,
216                           const CodeGenInstruction &inst,
217                           unsigned syntax) {
218   unsigned int numArgs = 0;
219   
220   AsmWriterInst awInst(inst, syntax, -1, -1);
221   
222   std::vector<AsmWriterOperand>::iterator operandIterator;
223   
224   for (operandIterator = awInst.Operands.begin();
225        operandIterator != awInst.Operands.end();
226        ++operandIterator) {
227     if (operandIterator->OperandType == 
228         AsmWriterOperand::isMachineInstrOperand) {
229       char buf[2];
230       snprintf(buf, sizeof(buf), "%u", operandIterator->CGIOpNo);
231       operandOrder->addEntry(new LiteralConstantEmitter(buf));
232       numArgs++;
233     }
234   }
235   
236   for(; numArgs < MAX_OPERANDS; numArgs++) {
237     operandOrder->addEntry(new LiteralConstantEmitter("-1"));
238   }
239 }
240
241 /////////////////////////////////////////////////////
242 // Support functions for handling X86 instructions //
243 /////////////////////////////////////////////////////
244
245 #define ADDFLAG(flag) flags->addEntry(flag)
246
247 #define REG(str) if (name == str) { ADDFLAG("kOperandFlagRegister"); return 0; }
248 #define MEM(str) if (name == str) { ADDFLAG("kOperandFlagMemory"); return 0; }
249 #define LEA(str) if (name == str) { ADDFLAG("kOperandFlagEffectiveAddress"); \
250                                     return 0; }
251 #define IMM(str) if (name == str) { ADDFLAG("kOperandFlagImmediate"); \
252                                     return 0; }
253 #define PCR(str) if (name == str) { ADDFLAG("kOperandFlagMemory"); \
254                                     ADDFLAG("kOperandFlagPCRelative"); \
255                                     return 0; }
256
257 /// X86FlagFromOpName - Processes the name of a single X86 operand (which is
258 ///   actually its type) and translates it into an operand flag
259 ///
260 /// @arg flags    - The flags object to add the flag to
261 /// @arg name     - The name of the operand
262 static int X86FlagFromOpName(FlagsConstantEmitter *flags,
263                              const std::string &name) {
264   REG("GR8");
265   REG("GR8_NOREX");
266   REG("GR16");
267   REG("GR32");
268   REG("GR32_NOREX");
269   REG("FR32");
270   REG("RFP32");
271   REG("GR64");
272   REG("FR64");
273   REG("VR64");
274   REG("RFP64");
275   REG("RFP80");
276   REG("VR128");
277   REG("RST");
278   REG("SEGMENT_REG");
279   REG("DEBUG_REG");
280   REG("CONTROL_REG_32");
281   REG("CONTROL_REG_64");
282   
283   MEM("i8mem");
284   MEM("i8mem_NOREX");
285   MEM("i16mem");
286   MEM("i32mem");
287   MEM("f32mem");
288   MEM("ssmem");
289   MEM("opaque32mem");
290   MEM("opaque48mem");
291   MEM("i64mem");
292   MEM("f64mem");
293   MEM("sdmem");
294   MEM("f80mem");
295   MEM("opaque80mem");
296   MEM("i128mem");
297   MEM("f128mem");
298   MEM("opaque512mem");
299   
300   LEA("lea32mem");
301   LEA("lea64_32mem");
302   LEA("lea64mem");
303   
304   IMM("i8imm");
305   IMM("i16imm");
306   IMM("i16i8imm");
307   IMM("i32imm");
308   IMM("i32imm_pcrel");
309   IMM("i32i8imm");
310   IMM("i64imm");
311   IMM("i64i8imm");
312   IMM("i64i32imm");
313   IMM("i64i32imm_pcrel");
314   IMM("SSECC");
315   
316   PCR("brtarget8");
317   PCR("offset8");
318   PCR("offset16");
319   PCR("offset32");
320   PCR("offset64");
321   PCR("brtarget");
322   
323   return 1;
324 }
325
326 #undef REG
327 #undef MEM
328 #undef LEA
329 #undef IMM
330 #undef PCR
331 #undef ADDFLAG
332
333 /// X86PopulateOperands - Handles all the operands in an X86 instruction, adding
334 ///   the appropriate flags to their descriptors
335 ///
336 /// @operandFlags - A reference the array of operand flag objects
337 /// @inst         - The instruction to use as a source of information
338 static void X86PopulateOperands(
339   FlagsConstantEmitter *(&operandFlags)[MAX_OPERANDS],
340   const CodeGenInstruction &inst) {
341   if (!inst.TheDef->isSubClassOf("X86Inst"))
342     return;
343   
344   unsigned int index;
345   unsigned int numOperands = inst.OperandList.size();
346   
347   for (index = 0; index < numOperands; ++index) {
348     const CodeGenInstruction::OperandInfo &operandInfo = 
349       inst.OperandList[index];
350     Record &rec = *operandInfo.Rec;
351     
352     if (X86FlagFromOpName(operandFlags[index], rec.getName())) {
353       errs() << "Operand type: " << rec.getName().c_str() << "\n";
354       errs() << "Operand name: " << operandInfo.Name.c_str() << "\n";
355       errs() << "Instruction mame: " << inst.TheDef->getName().c_str() << "\n";
356       llvm_unreachable("Unhandled type");
357     }
358   }
359 }
360
361 /// decorate1 - Decorates a named operand with a new flag
362 ///
363 /// @operandFlags - The array of operand flag objects, which don't have names
364 /// @inst         - The CodeGenInstruction, which provides a way to translate
365 ///                 between names and operand indices
366 /// @opName       - The name of the operand
367 /// @flag         - The name of the flag to add
368 static inline void decorate1(FlagsConstantEmitter *(&operandFlags)[MAX_OPERANDS],
369                              const CodeGenInstruction &inst,
370                              const char *opName,
371                              const char *opFlag) {
372   unsigned opIndex;
373   
374   try {
375     opIndex = inst.getOperandNamed(std::string(opName));
376   }
377   catch (...) {
378     errs() << "Instruction: " << inst.TheDef->getName().c_str() << "\n";
379     errs() << "Operand name: " << opName << "\n";
380     llvm_unreachable("Couldn't find operand");
381   }
382   
383   operandFlags[opIndex]->addEntry(opFlag);
384 }
385
386 #define DECORATE1(opName, opFlag) decorate1(operandFlags, inst, opName, opFlag)
387
388 #define MOV(source, target) {                       \
389   instFlags.addEntry("kInstructionFlagMove");       \
390   DECORATE1(source, "kOperandFlagSource");          \
391   DECORATE1(target, "kOperandFlagTarget");          \
392 }
393
394 #define BRANCH(target) {                            \
395   instFlags.addEntry("kInstructionFlagBranch");     \
396   DECORATE1(target, "kOperandFlagTarget");          \
397 }
398
399 #define PUSH(source) {                              \
400   instFlags.addEntry("kInstructionFlagPush");       \
401   DECORATE1(source, "kOperandFlagSource");          \
402 }
403
404 #define POP(target) {                               \
405   instFlags.addEntry("kInstructionFlagPop");        \
406   DECORATE1(target, "kOperandFlagTarget");          \
407 }
408
409 #define CALL(target) {                              \
410   instFlags.addEntry("kInstructionFlagCall");       \
411   DECORATE1(target, "kOperandFlagTarget");          \
412 }
413
414 #define RETURN() {                                  \
415   instFlags.addEntry("kInstructionFlagReturn");     \
416 }
417
418 /// X86ExtractSemantics - Performs various checks on the name of an X86
419 ///   instruction to determine what sort of an instruction it is and then adds 
420 ///   the appropriate flags to the instruction and its operands
421 ///
422 /// @arg instFlags    - A reference to the flags for the instruction as a whole
423 /// @arg operandFlags - A reference to the array of operand flag object pointers
424 /// @arg inst         - A reference to the original instruction
425 static void X86ExtractSemantics(FlagsConstantEmitter &instFlags,
426                                 FlagsConstantEmitter *(&operandFlags)[MAX_OPERANDS],
427                                 const CodeGenInstruction &inst) {
428   const std::string &name = inst.TheDef->getName();
429     
430   if (name.find("MOV") != name.npos) {
431     if (name.find("MOV_V") != name.npos) {
432       // ignore (this is a pseudoinstruction)
433     }
434     else if (name.find("MASK") != name.npos) {
435       // ignore (this is a masking move)
436     }
437     else if (name.find("r0") != name.npos) {
438       // ignore (this is a pseudoinstruction)
439     }
440     else if (name.find("PS") != name.npos ||
441              name.find("PD") != name.npos) {
442       // ignore (this is a shuffling move)
443     }
444     else if (name.find("MOVS") != name.npos) {
445       // ignore (this is a string move)
446     }
447     else if (name.find("_F") != name.npos) {
448       // TODO handle _F moves to ST(0)
449     }
450     else if (name.find("a") != name.npos) {
451       // TODO handle moves to/from %ax
452     }
453     else if (name.find("CMOV") != name.npos) {
454       MOV("src2", "dst");
455     }
456     else if (name.find("PC") != name.npos) {
457       MOV("label", "reg")
458     }
459     else {
460       MOV("src", "dst");
461     }
462   }
463   
464   if (name.find("JMP") != name.npos ||
465       name.find("J") == 0) {
466     if (name.find("FAR") != name.npos && name.find("i") != name.npos) {
467       BRANCH("off");
468     }
469     else {
470       BRANCH("dst");
471     }
472   }
473   
474   if (name.find("PUSH") != name.npos) {
475     if (name.find("FS") != name.npos ||
476         name.find("GS") != name.npos) {
477       instFlags.addEntry("kInstructionFlagPush");
478       // TODO add support for fixed operands
479     }
480     else if (name.find("F") != name.npos) {
481       // ignore (this pushes onto the FP stack)
482     }
483     else if (name[name.length() - 1] == 'm') {
484       PUSH("src");
485     }
486     else if (name.find("i") != name.npos) {
487       PUSH("imm");
488     }
489     else {
490       PUSH("reg");
491     }
492   }
493   
494   if (name.find("POP") != name.npos) {
495     if (name.find("POPCNT") != name.npos) {
496       // ignore (not a real pop)
497     }
498     else if (name.find("FS") != name.npos ||
499              name.find("GS") != name.npos) {
500       instFlags.addEntry("kInstructionFlagPop");
501       // TODO add support for fixed operands
502     }
503     else if (name.find("F") != name.npos) {
504       // ignore (this pops from the FP stack)
505     }
506     else if (name[name.length() - 1] == 'm') {
507       POP("dst");
508     }
509     else {
510       POP("reg");
511     }
512   }
513   
514   if (name.find("CALL") != name.npos) {
515     if (name.find("ADJ") != name.npos) {
516       // ignore (not a call)
517     }
518     else if (name.find("SYSCALL") != name.npos) {
519       // ignore (doesn't go anywhere we know about)
520     }
521     else if (name.find("VMCALL") != name.npos) {
522       // ignore (rather different semantics than a regular call)
523     }
524     else if (name.find("FAR") != name.npos && name.find("i") != name.npos) {
525       CALL("off");
526     }
527     else {
528       CALL("dst");
529     }
530   }
531   
532   if (name.find("RET") != name.npos) {
533     RETURN();
534   }
535 }
536
537 #undef MOV
538 #undef BRANCH
539 #undef PUSH
540 #undef POP
541 #undef CALL
542 #undef RETURN
543
544 #undef COND_DECORATE_2
545 #undef COND_DECORATE_1
546 #undef DECORATE1
547
548 /// populateInstInfo - Fills an array of InstInfos with information about each 
549 ///   instruction in a target
550 ///
551 /// @arg infoArray  - The array of InstInfo objects to populate
552 /// @arg target     - The CodeGenTarget to use as a source of instructions
553 static void populateInstInfo(CompoundConstantEmitter &infoArray,
554                              CodeGenTarget &target) {
555   std::vector<const CodeGenInstruction*> numberedInstructions;
556   target.getInstructionsByEnumValue(numberedInstructions);
557   
558   unsigned int index;
559   unsigned int numInstructions = numberedInstructions.size();
560   
561   for (index = 0; index < numInstructions; ++index) {
562     const CodeGenInstruction& inst = *numberedInstructions[index];
563     
564     CompoundConstantEmitter *infoStruct = new CompoundConstantEmitter;
565     infoArray.addEntry(infoStruct);
566     
567     FlagsConstantEmitter *instFlags = new FlagsConstantEmitter;
568     infoStruct->addEntry(instFlags);
569     
570     LiteralConstantEmitter *numOperandsEmitter = 
571       new LiteralConstantEmitter(inst.OperandList.size());
572     infoStruct->addEntry(numOperandsEmitter);
573                          
574     CompoundConstantEmitter *operandFlagArray = new CompoundConstantEmitter;
575     infoStruct->addEntry(operandFlagArray);
576         
577     FlagsConstantEmitter *operandFlags[MAX_OPERANDS];
578     
579     for (unsigned operandIndex = 0; operandIndex < MAX_OPERANDS; ++operandIndex) {
580       operandFlags[operandIndex] = new FlagsConstantEmitter;
581       operandFlagArray->addEntry(operandFlags[operandIndex]);
582     }
583  
584     unsigned numSyntaxes = 0;
585     
586     if (target.getName() == "X86") {
587       X86PopulateOperands(operandFlags, inst);
588       X86ExtractSemantics(*instFlags, operandFlags, inst);
589       numSyntaxes = 2;
590     }
591     
592     CompoundConstantEmitter *operandOrderArray = new CompoundConstantEmitter;
593     infoStruct->addEntry(operandOrderArray);
594     
595     for (unsigned syntaxIndex = 0; syntaxIndex < MAX_SYNTAXES; ++syntaxIndex) {
596       CompoundConstantEmitter *operandOrder = new CompoundConstantEmitter;
597       operandOrderArray->addEntry(operandOrder);
598       
599       if (syntaxIndex < numSyntaxes) {
600         populateOperandOrder(operandOrder, inst, syntaxIndex);
601       }
602       else {
603         for (unsigned operandIndex = 0; 
604              operandIndex < MAX_OPERANDS; 
605              ++operandIndex) {
606           operandOrder->addEntry(new LiteralConstantEmitter("-1"));
607         }
608       }
609     }
610   }
611 }
612
613 void EDEmitter::run(raw_ostream &o) {
614   unsigned int i = 0;
615   
616   CompoundConstantEmitter infoArray;
617   CodeGenTarget target;
618   
619   populateInstInfo(infoArray, target);
620   
621   o << "InstInfo instInfo" << target.getName().c_str() << "[] = ";
622   infoArray.emit(o, i);
623   o << ";" << "\n";
624 }
625
626 void EDEmitter::runHeader(raw_ostream &o) {
627   EmitSourceFileHeader("Enhanced Disassembly Info Header", o);
628   
629   o << "#ifndef EDInfo_" << "\n";
630   o << "#define EDInfo_" << "\n";
631   o << "\n";
632   o << "#include <inttypes.h>" << "\n";
633   o << "\n";
634   o << "#define MAX_OPERANDS " << format("%d", MAX_OPERANDS) << "\n";
635   o << "#define MAX_SYNTAXES " << format("%d", MAX_SYNTAXES) << "\n";
636   o << "\n";
637   
638   unsigned int i = 0;
639   
640   EnumEmitter operandFlags("OperandFlags");
641   operandFlags.addEntry("kOperandFlagImmediate");
642   operandFlags.addEntry("kOperandFlagRegister");
643   operandFlags.addEntry("kOperandFlagMemory");
644   operandFlags.addEntry("kOperandFlagEffectiveAddress");
645   operandFlags.addEntry("kOperandFlagPCRelative");
646   operandFlags.addEntry("kOperandFlagSource");
647   operandFlags.addEntry("kOperandFlagTarget");
648   operandFlags.emitAsFlags(o, i);
649   
650   o << "\n";
651   
652   EnumEmitter instructionFlags("InstructionFlags");
653   instructionFlags.addEntry("kInstructionFlagMove");
654   instructionFlags.addEntry("kInstructionFlagBranch");
655   instructionFlags.addEntry("kInstructionFlagPush");
656   instructionFlags.addEntry("kInstructionFlagPop");
657   instructionFlags.addEntry("kInstructionFlagCall");
658   instructionFlags.addEntry("kInstructionFlagReturn");
659   instructionFlags.emitAsFlags(o, i);
660   
661   o << "\n";
662   
663   StructEmitter instInfo("InstInfo");
664   instInfo.addMember("uint32_t", "instructionFlags");
665   instInfo.addMember("uint8_t", "numOperands");
666   instInfo.addMember("uint8_t", "operandFlags[MAX_OPERANDS]");
667   instInfo.addMember("const char", "operandOrders[MAX_SYNTAXES][MAX_OPERANDS]");
668   instInfo.emit(o, i);
669   
670   o << "\n";
671   o << "#endif" << "\n";
672 }