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