1 //===- InstrInfoEmitter.cpp - Generate a Instruction Set Desc. ------------===//
3 // This tablegen backend is responsible for emitting a description of the target
4 // instruction set for the code generator.
6 //===----------------------------------------------------------------------===//
8 #include "InstrInfoEmitter.h"
9 #include "CodeGenWrappers.h"
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");
16 if (Insts.size() == 0)
17 throw std::string("No 'Instruction' subclasses defined!");
19 std::string Namespace = Insts[0]->getValueAsString("Namespace");
21 EmitSourceFileHeader("Target Instruction Enum Values", OS);
23 if (!Namespace.empty())
24 OS << "namespace " << Namespace << " {\n";
29 // We must emit the PHI opcode first...
30 Record *InstrInfo = Target.getInstructionSet();
31 Record *PHI = InstrInfo->getValueAsDef("PHIInst");
33 OS << " " << PHI->getName() << ", \t// 0 (fixed for all targets)\n";
35 // Print out the rest of the instructions now...
36 for (unsigned i = 0, e = Insts.size(); i != e; ++i)
38 OS << " " << Insts[i]->getName() << ", \t// " << i+1 << "\n";
41 if (!Namespace.empty())
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()) << ", ";
52 throw "Illegal value in '" + Name + "' list!";
57 // run - Emit the main instruction description records for the target...
58 void InstrInfoEmitter::run(std::ostream &OS) {
59 EmitSourceFileHeader("Target Instruction Descriptors", OS);
61 const std::string &TargetName = Target.getName();
62 Record *InstrInfo = Target.getInstructionSet();
63 Record *PHI = InstrInfo->getValueAsDef("PHIInst");
65 std::vector<Record*> Instructions =
66 Records.getAllDerivedDefinitions("Instruction");
68 // Emit empty implicit uses and defs lists
69 OS << "static const unsigned EmptyImpUses[] = { 0 };\n"
70 << "static const unsigned EmptyImpDefs[] = { 0 };\n";
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);
81 OS << "\nstatic const TargetInstrDescriptor " << TargetName
83 emitRecord(PHI, 0, InstrInfo, OS);
85 for (unsigned i = 0, e = Instructions.size(); i != e; ++i)
86 if (Instructions[i] != PHI)
87 emitRecord(Instructions[i], i+1, InstrInfo, OS);
91 void InstrInfoEmitter::emitRecord(Record *R, unsigned Num, Record *InstrInfo,
93 OS << " { \"" << R->getValueAsString("Name")
94 << "\",\t-1, -1, 0, false, 0, 0, 0, 0";
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";
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!";
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);
117 // Emit the implicit uses and defs lists...
118 LI = R->getValueAsListInit("Uses");
120 OS << "EmptyImpUses, ";
122 OS << R->getName() << "ImpUses, ";
124 LI = R->getValueAsListInit("Defs");
126 OS << "EmptyImpDefs ";
128 OS << R->getName() << "ImpDefs ";
130 OS << " }, // Inst #" << Num << " = " << R->getName() << "\n";
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();
140 if (RV == 0 || RV->getValue() == 0)
141 throw R->getName() + " doesn't have a field named '" + Val->getValue()+"'!";
143 Init *Value = RV->getValue();
144 if (BitInit *BI = dynamic_cast<BitInit*>(Value)) {
145 if (BI->getValue()) OS << "|(1<<" << Shift << ")";
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());
151 if (IntInit *II = dynamic_cast<IntInit*>(I)) {
153 OS << "|(" << II->getValue() << "<<" << Shift << ")";
157 } else if (IntInit *II = dynamic_cast<IntInit*>(Value)) {
158 if (II->getValue()) OS << "|(" << II->getValue() << "<<" << Shift << ")";
162 std::cerr << "Unhandled initializer: " << *Val << "\n";
163 throw "In record '" + R->getName() + "' for TSFlag emission.";