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 all of the instruction's implicit uses and defs...
69 for (unsigned i = 0, e = Instructions.size(); i != e; ++i) {
70 Record *Inst = Instructions[i];
71 ListInit *LI = Inst->getValueAsListInit("Uses");
72 if (LI->getSize()) printDefList(LI, Inst->getName()+"ImpUses", OS);
73 LI = Inst->getValueAsListInit("Defs");
74 if (LI->getSize()) printDefList(LI, Inst->getName()+"ImpDefs", OS);
77 OS << "\nstatic const TargetInstrDescriptor " << TargetName
79 emitRecord(PHI, 0, InstrInfo, OS);
81 for (unsigned i = 0, e = Instructions.size(); i != e; ++i)
82 if (Instructions[i] != PHI)
83 emitRecord(Instructions[i], i+1, InstrInfo, OS);
87 void InstrInfoEmitter::emitRecord(Record *R, unsigned Num, Record *InstrInfo,
89 OS << " { \"" << R->getValueAsString("Name")
90 << "\",\t-1, -1, 0, false, 0, 0, 0, 0";
92 // Emit all of the target indepedent flags...
93 if (R->getValueAsBit("isReturn")) OS << "|M_RET_FLAG";
94 if (R->getValueAsBit("isBranch")) OS << "|M_BRANCH_FLAG";
95 if (R->getValueAsBit("isCall" )) OS << "|M_CALL_FLAG";
96 if (R->getValueAsBit("isTwoAddress")) OS << "|M_2_ADDR_FLAG";
97 if (R->getValueAsBit("isTerminator")) OS << "|M_TERMINATOR_FLAG";
100 // Emit all of the target-specific flags...
101 ListInit *LI = InstrInfo->getValueAsListInit("TSFlagsFields");
102 ListInit *Shift = InstrInfo->getValueAsListInit("TSFlagsShifts");
103 if (LI->getSize() != Shift->getSize())
104 throw "Lengths of " + InstrInfo->getName() +
105 ":(TargetInfoFields, TargetInfoPositions) must be equal!";
107 for (unsigned i = 0, e = LI->getSize(); i != e; ++i)
108 emitShiftedValue(R, dynamic_cast<StringInit*>(LI->getElement(i)),
109 dynamic_cast<IntInit*>(Shift->getElement(i)), OS);
113 // Emit the implicit uses and defs lists...
114 LI = R->getValueAsListInit("Uses");
118 OS << R->getName() << "ImpUses, ";
120 LI = R->getValueAsListInit("Defs");
124 OS << R->getName() << "ImpDefs ";
126 OS << " }, // Inst #" << Num << " = " << R->getName() << "\n";
129 void InstrInfoEmitter::emitShiftedValue(Record *R, StringInit *Val,
130 IntInit *ShiftInt, std::ostream &OS) {
131 if (Val == 0 || ShiftInt == 0)
132 throw std::string("Illegal value or shift amount in TargetInfo*!");
133 RecordVal *RV = R->getValue(Val->getValue());
134 int Shift = ShiftInt->getValue();
136 if (RV == 0 || RV->getValue() == 0)
137 throw R->getName() + " doesn't have a field named '" + Val->getValue()+"'!";
139 Init *Value = RV->getValue();
140 if (BitInit *BI = dynamic_cast<BitInit*>(Value)) {
141 if (BI->getValue()) OS << "|(1<<" << Shift << ")";
143 } else if (BitsInit *BI = dynamic_cast<BitsInit*>(Value)) {
144 // Convert the Bits to an integer to print...
145 Init *I = BI->convertInitializerTo(new IntRecTy());
147 if (IntInit *II = dynamic_cast<IntInit*>(I)) {
149 OS << "|(" << II->getValue() << "<<" << Shift << ")";
153 } else if (IntInit *II = dynamic_cast<IntInit*>(Value)) {
154 if (II->getValue()) OS << "|(" << II->getValue() << "<<" << Shift << ")";
158 std::cerr << "Unhandled initializer: " << *Val << "\n";
159 throw "In record '" + R->getName() + "' for TSFlag emission.";