1 //===- InstrInfoEmitter.cpp - Generate a Instruction Set Desc. ------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This tablegen backend is responsible for emitting a description of the target
11 // instruction set for the code generator.
13 //===----------------------------------------------------------------------===//
15 #include "InstrInfoEmitter.h"
16 #include "CodeGenTarget.h"
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";
27 // We must emit the PHI opcode first...
28 Record *InstrInfo = Target.getInstructionSet();
30 std::string Namespace = Target.inst_begin()->second.Namespace;
32 if (!Namespace.empty())
33 OS << "namespace " << Namespace << " {\n";
36 std::vector<const CodeGenInstruction*> NumberedInstructions;
37 Target.getInstructionsByEnumValue(NumberedInstructions);
39 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
40 OS << " " << NumberedInstructions[i]->TheDef->getName()
41 << ", \t// " << i << "\n";
44 if (!Namespace.empty())
46 OS << "} // End llvm namespace \n";
49 void InstrInfoEmitter::printDefList(ListInit *LI, const std::string &Name,
50 std::ostream &OS) const {
51 OS << "static const unsigned " << Name << "[] = { ";
52 for (unsigned j = 0, e = LI->getSize(); j != e; ++j)
53 if (DefInit *DI = dynamic_cast<DefInit*>(LI->getElement(j)))
54 OS << getQualifiedName(DI->getDef()) << ", ";
56 throw "Illegal value in '" + Name + "' list!";
61 // run - Emit the main instruction description records for the target...
62 void InstrInfoEmitter::run(std::ostream &OS) {
63 EmitSourceFileHeader("Target Instruction Descriptors", OS);
64 OS << "namespace llvm {\n\n";
67 const std::string &TargetName = Target.getName();
68 Record *InstrInfo = Target.getInstructionSet();
69 Record *PHI = InstrInfo->getValueAsDef("PHIInst");
71 // Emit empty implicit uses and defs lists
72 OS << "static const unsigned EmptyImpUses[] = { 0 };\n"
73 << "static const unsigned EmptyImpDefs[] = { 0 };\n";
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);
85 OS << "\nstatic const TargetInstrDescriptor " << TargetName
87 emitRecord(Target.getPHIInstruction(), 0, InstrInfo, OS);
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);
95 OS << "} // End llvm namespace \n";
98 void InstrInfoEmitter::emitRecord(const CodeGenInstruction &Inst, unsigned Num,
99 Record *InstrInfo, std::ostream &OS) {
101 if (Inst.Name.empty())
102 OS << Inst.TheDef->getName();
105 OS << "\",\t-1, -1, 0, false, 0, 0, 0, 0";
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.hasDelaySlot) OS << "|M_DELAY_SLOT_FLAG";
112 if (Inst.isCall) OS << "|M_CALL_FLAG";
113 if (Inst.isLoad) OS << "|M_LOAD_FLAG";
114 if (Inst.isStore) OS << "|M_STORE_FLAG";
115 if (Inst.isTwoAddress) OS << "|M_2_ADDR_FLAG";
116 if (Inst.isConvertibleToThreeAddress) OS << "|M_CONVERTIBLE_TO_3_ADDR";
117 if (Inst.isCommutable) OS << "|M_COMMUTABLE";
118 if (Inst.isTerminator) OS << "|M_TERMINATOR_FLAG";
121 // Emit all of the target-specific flags...
122 ListInit *LI = InstrInfo->getValueAsListInit("TSFlagsFields");
123 ListInit *Shift = InstrInfo->getValueAsListInit("TSFlagsShifts");
124 if (LI->getSize() != Shift->getSize())
125 throw "Lengths of " + InstrInfo->getName() +
126 ":(TargetInfoFields, TargetInfoPositions) must be equal!";
128 for (unsigned i = 0, e = LI->getSize(); i != e; ++i)
129 emitShiftedValue(Inst.TheDef, dynamic_cast<StringInit*>(LI->getElement(i)),
130 dynamic_cast<IntInit*>(Shift->getElement(i)), OS);
134 // Emit the implicit uses and defs lists...
135 LI = Inst.TheDef->getValueAsListInit("Uses");
137 OS << "EmptyImpUses, ";
139 OS << Inst.TheDef->getName() << "ImpUses, ";
141 LI = Inst.TheDef->getValueAsListInit("Defs");
143 OS << "EmptyImpDefs ";
145 OS << Inst.TheDef->getName() << "ImpDefs ";
147 OS << " }, // Inst #" << Num << " = " << Inst.TheDef->getName() << "\n";
150 void InstrInfoEmitter::emitShiftedValue(Record *R, StringInit *Val,
151 IntInit *ShiftInt, std::ostream &OS) {
152 if (Val == 0 || ShiftInt == 0)
153 throw std::string("Illegal value or shift amount in TargetInfo*!");
154 RecordVal *RV = R->getValue(Val->getValue());
155 int Shift = ShiftInt->getValue();
157 if (RV == 0 || RV->getValue() == 0)
158 throw R->getName() + " doesn't have a field named '" + Val->getValue()+"'!";
160 Init *Value = RV->getValue();
161 if (BitInit *BI = dynamic_cast<BitInit*>(Value)) {
162 if (BI->getValue()) OS << "|(1<<" << Shift << ")";
164 } else if (BitsInit *BI = dynamic_cast<BitsInit*>(Value)) {
165 // Convert the Bits to an integer to print...
166 Init *I = BI->convertInitializerTo(new IntRecTy());
168 if (IntInit *II = dynamic_cast<IntInit*>(I)) {
170 OS << "|(" << II->getValue() << "<<" << Shift << ")";
174 } else if (IntInit *II = dynamic_cast<IntInit*>(Value)) {
175 if (II->getValue()) OS << "|(" << II->getValue() << "<<" << Shift << ")";
179 std::cerr << "Unhandled initializer: " << *Val << "\n";
180 throw "In record '" + R->getName() + "' for TSFlag emission.";