7b64f922f6cc71fbd35354859dfd596844adc5e3
[oota-llvm.git] / utils / TableGen / InstrInfoEmitter.cpp
1 //===- InstrInfoEmitter.cpp - Generate a Instruction Set Desc. ------------===//
2 //
3 // This tablegen backend is responsible for emitting a description of the target
4 // instruction set for the code generator.
5 //
6 //===----------------------------------------------------------------------===//
7
8 #include "InstrInfoEmitter.h"
9 #include "CodeGenWrappers.h"
10 #include "Record.h"
11
12 // runEnums - Print out enum values for all of the instructions.
13 void InstrInfoEmitter::runEnums(std::ostream &OS) {
14   std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
15
16   if (Insts.size() == 0)
17     throw std::string("No 'Instruction' subclasses defined!");
18
19   std::string Namespace = Insts[0]->getValueAsString("Namespace");
20
21   EmitSourceFileHeader("Target Instruction Enum Values", OS);
22
23   if (!Namespace.empty())
24     OS << "namespace " << Namespace << " {\n";
25   OS << "  enum {\n";
26
27   CodeGenTarget Target;
28
29   // We must emit the PHI opcode first...
30   Record *InstrInfo = Target.getInstructionSet();
31   Record *PHI = InstrInfo->getValueAsDef("PHIInst");
32
33   OS << "    " << PHI->getName() << ", \t// 0 (fixed for all targets)\n";
34   
35   // Print out the rest of the instructions now...
36   for (unsigned i = 0, e = Insts.size(); i != e; ++i)
37     if (Insts[i] != PHI)
38       OS << "    " << Insts[i]->getName() << ", \t// " << i+1 << "\n";
39   
40   OS << "  };\n";
41   if (!Namespace.empty())
42     OS << "}\n";
43 }
44
45 void InstrInfoEmitter::printDefList(ListInit *LI, const std::string &Name,
46                                     std::ostream &OS) const {
47   OS << "static const unsigned " << Name << "[] = { ";
48   for (unsigned j = 0, e = LI->getSize(); j != e; ++j)
49     if (DefInit *DI = dynamic_cast<DefInit*>(LI->getElement(j)))
50       OS << getQualifiedName(DI->getDef()) << ", ";
51     else
52       throw "Illegal value in '" + Name + "' list!";
53   OS << "0 };\n";
54 }
55
56
57 // run - Emit the main instruction description records for the target...
58 void InstrInfoEmitter::run(std::ostream &OS) {
59   EmitSourceFileHeader("Target Instruction Descriptors", OS);
60   CodeGenTarget Target;
61   const std::string &TargetName = Target.getName();
62   Record *InstrInfo = Target.getInstructionSet();
63   Record *PHI = InstrInfo->getValueAsDef("PHIInst");
64
65   std::vector<Record*> Instructions =
66     Records.getAllDerivedDefinitions("Instruction");
67   
68   // Emit empty implicit uses and defs lists
69   OS << "static const unsigned EmptyImpUses[] = { 0 };\n"
70      << "static const unsigned EmptyImpDefs[] = { 0 };\n";
71
72   // Emit all of the instruction's implicit uses and defs...
73   for (unsigned i = 0, e = Instructions.size(); i != e; ++i) {
74     Record *Inst = Instructions[i];
75     ListInit *LI = Inst->getValueAsListInit("Uses");
76     if (LI->getSize()) printDefList(LI, Inst->getName()+"ImpUses", OS);
77     LI = Inst->getValueAsListInit("Defs");
78     if (LI->getSize()) printDefList(LI, Inst->getName()+"ImpDefs", OS);
79   }
80
81   OS << "\nstatic const TargetInstrDescriptor " << TargetName
82      << "Insts[] = {\n";
83   emitRecord(PHI, 0, InstrInfo, OS);
84
85   for (unsigned i = 0, e = Instructions.size(); i != e; ++i)
86     if (Instructions[i] != PHI)
87       emitRecord(Instructions[i], i+1, InstrInfo, OS);
88   OS << "};\n";
89 }
90
91 void InstrInfoEmitter::emitRecord(Record *R, unsigned Num, Record *InstrInfo,
92                                   std::ostream &OS) {
93   OS << "  { \"" << R->getValueAsString("Name")
94      << "\",\t-1, -1, 0, false, 0, 0, 0, 0";
95
96   // Emit all of the target indepedent flags...
97   if (R->getValueAsBit("isReturn"))     OS << "|M_RET_FLAG";
98   if (R->getValueAsBit("isBranch"))     OS << "|M_BRANCH_FLAG";
99   if (R->getValueAsBit("isCall"  ))     OS << "|M_CALL_FLAG";
100   if (R->getValueAsBit("isTwoAddress")) OS << "|M_2_ADDR_FLAG";
101   if (R->getValueAsBit("isTerminator")) OS << "|M_TERMINATOR_FLAG";
102   OS << ", 0";
103
104   // Emit all of the target-specific flags...
105   ListInit *LI    = InstrInfo->getValueAsListInit("TSFlagsFields");
106   ListInit *Shift = InstrInfo->getValueAsListInit("TSFlagsShifts");
107   if (LI->getSize() != Shift->getSize())
108     throw "Lengths of " + InstrInfo->getName() +
109           ":(TargetInfoFields, TargetInfoPositions) must be equal!";
110
111   for (unsigned i = 0, e = LI->getSize(); i != e; ++i)
112     emitShiftedValue(R, dynamic_cast<StringInit*>(LI->getElement(i)),
113                      dynamic_cast<IntInit*>(Shift->getElement(i)), OS);
114
115   OS << ", ";
116
117   // Emit the implicit uses and defs lists...
118   LI = R->getValueAsListInit("Uses");
119   if (!LI->getSize())
120     OS << "EmptyImpUses, ";
121   else 
122     OS << R->getName() << "ImpUses, ";
123
124   LI = R->getValueAsListInit("Defs");
125   if (!LI->getSize())
126     OS << "EmptyImpDefs ";
127   else 
128     OS << R->getName() << "ImpDefs ";
129
130   OS << " },  // Inst #" << Num << " = " << R->getName() << "\n";
131 }
132
133 void InstrInfoEmitter::emitShiftedValue(Record *R, StringInit *Val,
134                                         IntInit *ShiftInt, std::ostream &OS) {
135   if (Val == 0 || ShiftInt == 0)
136     throw std::string("Illegal value or shift amount in TargetInfo*!");
137   RecordVal *RV = R->getValue(Val->getValue());
138   int Shift = ShiftInt->getValue();
139
140   if (RV == 0 || RV->getValue() == 0)
141     throw R->getName() + " doesn't have a field named '" + Val->getValue()+"'!";
142
143   Init *Value = RV->getValue();
144   if (BitInit *BI = dynamic_cast<BitInit*>(Value)) {
145     if (BI->getValue()) OS << "|(1<<" << Shift << ")";
146     return;
147   } else if (BitsInit *BI = dynamic_cast<BitsInit*>(Value)) {
148     // Convert the Bits to an integer to print...
149     Init *I = BI->convertInitializerTo(new IntRecTy());
150     if (I)
151       if (IntInit *II = dynamic_cast<IntInit*>(I)) {
152         if (II->getValue())
153           OS << "|(" << II->getValue() << "<<" << Shift << ")";
154         return;
155       }
156
157   } else if (IntInit *II = dynamic_cast<IntInit*>(Value)) {
158     if (II->getValue()) OS << "|(" << II->getValue() << "<<" << Shift << ")";
159     return;
160   }
161
162   std::cerr << "Unhandled initializer: " << *Val << "\n";
163   throw "In record '" + R->getName() + "' for TSFlag emission.";
164 }