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 static void EmitSourceHeader(const std::string &Desc, std::ostream &o) {
12 o << "//===- TableGen'erated file -------------------------------------*-"
13 " C++ -*-===//\n//\n// " << Desc << "\n//\n// Automatically generate"
14 "d file, do not edit!\n//\n//===------------------------------------"
15 "----------------------------------===//\n\n";
18 static std::string getQualifiedName(Record *R) {
19 std::string Namespace = R->getValueAsString("Namespace");
20 if (Namespace.empty()) return R->getName();
21 return Namespace + "::" + R->getName();
24 static Record *getTarget(RecordKeeper &RC) {
25 std::vector<Record*> Targets = RC.getAllDerivedDefinitions("Target");
27 if (Targets.size() != 1)
28 throw std::string("ERROR: Multiple subclasses of Target defined!");
32 // runEnums - Print out enum values for all of the instructions.
33 void InstrInfoEmitter::runEnums(std::ostream &OS) {
34 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
36 if (Insts.size() == 0)
37 throw std::string("No 'Instruction' subclasses defined!");
39 std::string Namespace = Insts[0]->getValueAsString("Namespace");
41 EmitSourceHeader("Target Instruction Enum Values", OS);
43 if (!Namespace.empty())
44 OS << "namespace " << Namespace << " {\n";
47 // We must emit the PHI opcode first...
48 Record *Target = getTarget(Records);
49 Record *InstrInfo = Target->getValueAsDef("InstructionSet");
50 Record *PHI = InstrInfo->getValueAsDef("PHIInst");
52 OS << " " << PHI->getName() << ", \t// 0 (fixed for all targets)\n";
54 // Print out the rest of the instructions now...
55 for (unsigned i = 0, e = Insts.size(); i != e; ++i)
57 OS << " " << Insts[i]->getName() << ", \t// " << i+1 << "\n";
60 if (!Namespace.empty())
64 static void printDefList(ListInit *LI, const std::string &Name,
66 OS << "static const unsigned " << Name << "[] = { ";
67 for (unsigned j = 0, e = LI->getSize(); j != e; ++j)
68 if (DefInit *DI = dynamic_cast<DefInit*>(LI->getElement(j)))
69 OS << getQualifiedName(DI->getDef()) << ", ";
71 throw "Illegal value in '" + Name + "' list!";
76 // run - Emit the main instruction description records for the target...
77 void InstrInfoEmitter::run(std::ostream &OS) {
78 EmitSourceHeader("Target Instruction Descriptors", OS);
79 Record *Target = getTarget(Records);
80 const std::string &TargetName = Target->getName();
81 Record *InstrInfo = Target->getValueAsDef("InstructionSet");
82 Record *PHI = InstrInfo->getValueAsDef("PHIInst");
84 std::vector<Record*> Instructions =
85 Records.getAllDerivedDefinitions("Instruction");
87 // Emit all of the instruction's implicit uses and defs...
88 for (unsigned i = 0, e = Instructions.size(); i != e; ++i) {
89 Record *Inst = Instructions[i];
90 ListInit *LI = Inst->getValueAsListInit("Uses");
91 if (LI->getSize()) printDefList(LI, Inst->getName()+"ImpUses", OS);
92 LI = Inst->getValueAsListInit("Defs");
93 if (LI->getSize()) printDefList(LI, Inst->getName()+"ImpDefs", OS);
96 OS << "\nstatic const TargetInstrDescriptor " << TargetName
98 emitRecord(PHI, 0, InstrInfo, OS);
100 for (unsigned i = 0, e = Instructions.size(); i != e; ++i)
101 if (Instructions[i] != PHI)
102 emitRecord(Instructions[i], i+1, InstrInfo, OS);
106 void InstrInfoEmitter::emitRecord(Record *R, unsigned Num, Record *InstrInfo,
108 OS << " { \"" << R->getValueAsString("Name")
109 << "\",\t-1, -1, 0, false, 0, 0, 0, 0";
111 // Emit all of the target indepedent flags...
112 if (R->getValueAsBit("isReturn")) OS << "|M_RET_FLAG";
113 if (R->getValueAsBit("isBranch")) OS << "|M_BRANCH_FLAG";
114 if (R->getValueAsBit("isCall" )) OS << "|M_CALL_FLAG";
115 if (R->getValueAsBit("isTwoAddress")) OS << "|M_2_ADDR_FLAG";
116 if (R->getValueAsBit("isTerminator")) OS << "|M_TERMINATOR_FLAG";
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!";
126 for (unsigned i = 0, e = LI->getSize(); i != e; ++i)
127 emitShiftedValue(R, dynamic_cast<StringInit*>(LI->getElement(i)),
128 dynamic_cast<IntInit*>(Shift->getElement(i)), OS);
132 // Emit the implicit uses and defs lists...
133 LI = R->getValueAsListInit("Uses");
137 OS << R->getName() << "ImpUses, ";
139 LI = R->getValueAsListInit("Defs");
143 OS << R->getName() << "ImpDefs ";
145 OS << " }, // Inst #" << Num << " = " << R->getName() << "\n";
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();
155 if (RV == 0 || RV->getValue() == 0)
156 throw R->getName() + " doesn't have a field named '" + Val->getValue()+"'!";
158 Init *Value = RV->getValue();
159 if (BitInit *BI = dynamic_cast<BitInit*>(Value)) {
160 if (BI->getValue()) OS << "|(1<<" << Shift << ")";
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());
166 if (IntInit *II = dynamic_cast<IntInit*>(I)) {
168 OS << "|(" << II->getValue() << "<<" << Shift << ")";
172 } else if (IntInit *II = dynamic_cast<IntInit*>(Value)) {
173 if (II->getValue()) OS << "|(" << II->getValue() << "<<" << Shift << ")";
177 std::cerr << "Unhandled initializer: " << *Val << "\n";
178 throw "In record '" + R->getName() + "' for TSFlag emission.";