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"
11 // runEnums - Print out enum values for all of the instructions.
12 void InstrInfoEmitter::runEnums(std::ostream &OS) {
13 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
15 if (Insts.size() == 0)
16 throw std::string("No 'Instruction' subclasses defined!");
18 std::string Namespace = Insts[0]->getValueAsString("Namespace");
20 EmitSourceFileHeader("Target Instruction Enum Values", OS);
22 if (!Namespace.empty())
23 OS << "namespace " << Namespace << " {\n";
26 // We must emit the PHI opcode first...
27 Record *Target = getTarget(Records);
28 Record *InstrInfo = Target->getValueAsDef("InstructionSet");
29 Record *PHI = InstrInfo->getValueAsDef("PHIInst");
31 OS << " " << PHI->getName() << ", \t// 0 (fixed for all targets)\n";
33 // Print out the rest of the instructions now...
34 for (unsigned i = 0, e = Insts.size(); i != e; ++i)
36 OS << " " << Insts[i]->getName() << ", \t// " << i+1 << "\n";
39 if (!Namespace.empty())
43 void InstrInfoEmitter::printDefList(ListInit *LI, const std::string &Name,
44 std::ostream &OS) const {
45 OS << "static const unsigned " << Name << "[] = { ";
46 for (unsigned j = 0, e = LI->getSize(); j != e; ++j)
47 if (DefInit *DI = dynamic_cast<DefInit*>(LI->getElement(j)))
48 OS << getQualifiedName(DI->getDef()) << ", ";
50 throw "Illegal value in '" + Name + "' list!";
55 // run - Emit the main instruction description records for the target...
56 void InstrInfoEmitter::run(std::ostream &OS) {
57 EmitSourceFileHeader("Target Instruction Descriptors", OS);
58 Record *Target = getTarget(Records);
59 const std::string &TargetName = Target->getName();
60 Record *InstrInfo = Target->getValueAsDef("InstructionSet");
61 Record *PHI = InstrInfo->getValueAsDef("PHIInst");
63 std::vector<Record*> Instructions =
64 Records.getAllDerivedDefinitions("Instruction");
66 // Emit all of the instruction's implicit uses and defs...
67 for (unsigned i = 0, e = Instructions.size(); i != e; ++i) {
68 Record *Inst = Instructions[i];
69 ListInit *LI = Inst->getValueAsListInit("Uses");
70 if (LI->getSize()) printDefList(LI, Inst->getName()+"ImpUses", OS);
71 LI = Inst->getValueAsListInit("Defs");
72 if (LI->getSize()) printDefList(LI, Inst->getName()+"ImpDefs", OS);
75 OS << "\nstatic const TargetInstrDescriptor " << TargetName
77 emitRecord(PHI, 0, InstrInfo, OS);
79 for (unsigned i = 0, e = Instructions.size(); i != e; ++i)
80 if (Instructions[i] != PHI)
81 emitRecord(Instructions[i], i+1, InstrInfo, OS);
85 void InstrInfoEmitter::emitRecord(Record *R, unsigned Num, Record *InstrInfo,
87 OS << " { \"" << R->getValueAsString("Name")
88 << "\",\t-1, -1, 0, false, 0, 0, 0, 0";
90 // Emit all of the target indepedent flags...
91 if (R->getValueAsBit("isReturn")) OS << "|M_RET_FLAG";
92 if (R->getValueAsBit("isBranch")) OS << "|M_BRANCH_FLAG";
93 if (R->getValueAsBit("isCall" )) OS << "|M_CALL_FLAG";
94 if (R->getValueAsBit("isTwoAddress")) OS << "|M_2_ADDR_FLAG";
95 if (R->getValueAsBit("isTerminator")) OS << "|M_TERMINATOR_FLAG";
98 // Emit all of the target-specific flags...
99 ListInit *LI = InstrInfo->getValueAsListInit("TSFlagsFields");
100 ListInit *Shift = InstrInfo->getValueAsListInit("TSFlagsShifts");
101 if (LI->getSize() != Shift->getSize())
102 throw "Lengths of " + InstrInfo->getName() +
103 ":(TargetInfoFields, TargetInfoPositions) must be equal!";
105 for (unsigned i = 0, e = LI->getSize(); i != e; ++i)
106 emitShiftedValue(R, dynamic_cast<StringInit*>(LI->getElement(i)),
107 dynamic_cast<IntInit*>(Shift->getElement(i)), OS);
111 // Emit the implicit uses and defs lists...
112 LI = R->getValueAsListInit("Uses");
116 OS << R->getName() << "ImpUses, ";
118 LI = R->getValueAsListInit("Defs");
122 OS << R->getName() << "ImpDefs ";
124 OS << " }, // Inst #" << Num << " = " << R->getName() << "\n";
127 void InstrInfoEmitter::emitShiftedValue(Record *R, StringInit *Val,
128 IntInit *ShiftInt, std::ostream &OS) {
129 if (Val == 0 || ShiftInt == 0)
130 throw std::string("Illegal value or shift amount in TargetInfo*!");
131 RecordVal *RV = R->getValue(Val->getValue());
132 int Shift = ShiftInt->getValue();
134 if (RV == 0 || RV->getValue() == 0)
135 throw R->getName() + " doesn't have a field named '" + Val->getValue()+"'!";
137 Init *Value = RV->getValue();
138 if (BitInit *BI = dynamic_cast<BitInit*>(Value)) {
139 if (BI->getValue()) OS << "|(1<<" << Shift << ")";
141 } else if (BitsInit *BI = dynamic_cast<BitsInit*>(Value)) {
142 // Convert the Bits to an integer to print...
143 Init *I = BI->convertInitializerTo(new IntRecTy());
145 if (IntInit *II = dynamic_cast<IntInit*>(I)) {
147 OS << "|(" << II->getValue() << "<<" << Shift << ")";
151 } else if (IntInit *II = dynamic_cast<IntInit*>(Value)) {
152 if (II->getValue()) OS << "|(" << II->getValue() << "<<" << Shift << ")";
156 std::cerr << "Unhandled initializer: " << *Val << "\n";
157 throw "In record '" + R->getName() + "' for TSFlag emission.";