blob: ad501dc4261f502efedeb5714132c064f9fcba9a [file] [log] [blame]
Chris Lattner33ccf7e2003-08-03 17:24:10 +00001//===- InstrInfoEmitter.cpp - Generate a Instruction Set Desc. ------------===//
2//
3// This tablegen backend is responsible for emitting a description of the target
4// instruction set for the code generator.
5//
6//===----------------------------------------------------------------------===//
7
8#include "InstrInfoEmitter.h"
9#include "Record.h"
10
Chris Lattner33ccf7e2003-08-03 17:24:10 +000011// runEnums - Print out enum values for all of the instructions.
12void InstrInfoEmitter::runEnums(std::ostream &OS) {
13 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
14
15 if (Insts.size() == 0)
16 throw std::string("No 'Instruction' subclasses defined!");
17
18 std::string Namespace = Insts[0]->getValueAsString("Namespace");
19
Chris Lattnerbc017232003-08-06 04:32:07 +000020 EmitSourceFileHeader("Target Instruction Enum Values", OS);
Chris Lattner33ccf7e2003-08-03 17:24:10 +000021
22 if (!Namespace.empty())
23 OS << "namespace " << Namespace << " {\n";
24 OS << " enum {\n";
25
Chris Lattnera3ae6142003-08-03 21:57:51 +000026 // We must emit the PHI opcode first...
Chris Lattner33ccf7e2003-08-03 17:24:10 +000027 Record *Target = getTarget(Records);
28 Record *InstrInfo = Target->getValueAsDef("InstructionSet");
Chris Lattner33ccf7e2003-08-03 17:24:10 +000029 Record *PHI = InstrInfo->getValueAsDef("PHIInst");
Chris Lattner33ccf7e2003-08-03 17:24:10 +000030
Chris Lattnera3ae6142003-08-03 21:57:51 +000031 OS << " " << PHI->getName() << ", \t// 0 (fixed for all targets)\n";
Chris Lattner33ccf7e2003-08-03 17:24:10 +000032
33 // Print out the rest of the instructions now...
34 for (unsigned i = 0, e = Insts.size(); i != e; ++i)
Chris Lattnera3ae6142003-08-03 21:57:51 +000035 if (Insts[i] != PHI)
36 OS << " " << Insts[i]->getName() << ", \t// " << i+1 << "\n";
Chris Lattner33ccf7e2003-08-03 17:24:10 +000037
38 OS << " };\n";
39 if (!Namespace.empty())
40 OS << "}\n";
41}
Chris Lattnera3ae6142003-08-03 21:57:51 +000042
Chris Lattnerbc017232003-08-06 04:32:07 +000043void InstrInfoEmitter::printDefList(ListInit *LI, const std::string &Name,
44 std::ostream &OS) const {
Chris Lattnera3ae6142003-08-03 21:57:51 +000045 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()) << ", ";
49 else
50 throw "Illegal value in '" + Name + "' list!";
51 OS << "0 };\n";
52}
53
54
55// run - Emit the main instruction description records for the target...
56void InstrInfoEmitter::run(std::ostream &OS) {
Chris Lattnerbc017232003-08-06 04:32:07 +000057 EmitSourceFileHeader("Target Instruction Descriptors", OS);
Chris Lattnera3ae6142003-08-03 21:57:51 +000058 Record *Target = getTarget(Records);
59 const std::string &TargetName = Target->getName();
60 Record *InstrInfo = Target->getValueAsDef("InstructionSet");
61 Record *PHI = InstrInfo->getValueAsDef("PHIInst");
62
63 std::vector<Record*> Instructions =
64 Records.getAllDerivedDefinitions("Instruction");
65
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);
73 }
74
75 OS << "\nstatic const TargetInstrDescriptor " << TargetName
76 << "Insts[] = {\n";
77 emitRecord(PHI, 0, InstrInfo, OS);
78
79 for (unsigned i = 0, e = Instructions.size(); i != e; ++i)
80 if (Instructions[i] != PHI)
81 emitRecord(Instructions[i], i+1, InstrInfo, OS);
82 OS << "};\n";
83}
84
85void InstrInfoEmitter::emitRecord(Record *R, unsigned Num, Record *InstrInfo,
86 std::ostream &OS) {
87 OS << " { \"" << R->getValueAsString("Name")
88 << "\",\t-1, -1, 0, false, 0, 0, 0, 0";
89
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";
96 OS << ", 0";
97
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!";
104
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);
108
109 OS << ", ";
110
111 // Emit the implicit uses and defs lists...
112 LI = R->getValueAsListInit("Uses");
113 if (!LI->getSize())
114 OS << "0, ";
115 else
116 OS << R->getName() << "ImpUses, ";
117
118 LI = R->getValueAsListInit("Defs");
119 if (!LI->getSize())
120 OS << "0 ";
121 else
122 OS << R->getName() << "ImpDefs ";
123
124 OS << " }, // Inst #" << Num << " = " << R->getName() << "\n";
125}
126
127void 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();
133
134 if (RV == 0 || RV->getValue() == 0)
135 throw R->getName() + " doesn't have a field named '" + Val->getValue()+"'!";
136
137 Init *Value = RV->getValue();
138 if (BitInit *BI = dynamic_cast<BitInit*>(Value)) {
139 if (BI->getValue()) OS << "|(1<<" << Shift << ")";
140 return;
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());
144 if (I)
145 if (IntInit *II = dynamic_cast<IntInit*>(I)) {
146 if (II->getValue())
147 OS << "|(" << II->getValue() << "<<" << Shift << ")";
148 return;
149 }
150
151 } else if (IntInit *II = dynamic_cast<IntInit*>(Value)) {
152 if (II->getValue()) OS << "|(" << II->getValue() << "<<" << Shift << ")";
153 return;
154 }
155
156 std::cerr << "Unhandled initializer: " << *Val << "\n";
157 throw "In record '" + R->getName() + "' for TSFlag emission.";
158}