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