blob: 7b64f922f6cc71fbd35354859dfd596844adc5e3 [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"
Chris Lattner7884b752003-08-07 05:39:09 +00009#include "CodeGenWrappers.h"
Chris Lattner33ccf7e2003-08-03 17:24:10 +000010#include "Record.h"
11
Chris Lattner33ccf7e2003-08-03 17:24:10 +000012// runEnums - Print out enum values for all of the instructions.
13void InstrInfoEmitter::runEnums(std::ostream &OS) {
14 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
15
16 if (Insts.size() == 0)
17 throw std::string("No 'Instruction' subclasses defined!");
18
19 std::string Namespace = Insts[0]->getValueAsString("Namespace");
20
Chris Lattnerbc017232003-08-06 04:32:07 +000021 EmitSourceFileHeader("Target Instruction Enum Values", OS);
Chris Lattner33ccf7e2003-08-03 17:24:10 +000022
23 if (!Namespace.empty())
24 OS << "namespace " << Namespace << " {\n";
25 OS << " enum {\n";
26
Chris Lattner7884b752003-08-07 05:39:09 +000027 CodeGenTarget Target;
28
Chris Lattnera3ae6142003-08-03 21:57:51 +000029 // We must emit the PHI opcode first...
Chris Lattner7884b752003-08-07 05:39:09 +000030 Record *InstrInfo = Target.getInstructionSet();
Chris Lattner33ccf7e2003-08-03 17:24:10 +000031 Record *PHI = InstrInfo->getValueAsDef("PHIInst");
Chris Lattner33ccf7e2003-08-03 17:24:10 +000032
Chris Lattnera3ae6142003-08-03 21:57:51 +000033 OS << " " << PHI->getName() << ", \t// 0 (fixed for all targets)\n";
Chris Lattner33ccf7e2003-08-03 17:24:10 +000034
35 // Print out the rest of the instructions now...
36 for (unsigned i = 0, e = Insts.size(); i != e; ++i)
Chris Lattnera3ae6142003-08-03 21:57:51 +000037 if (Insts[i] != PHI)
38 OS << " " << Insts[i]->getName() << ", \t// " << i+1 << "\n";
Chris Lattner33ccf7e2003-08-03 17:24:10 +000039
40 OS << " };\n";
41 if (!Namespace.empty())
42 OS << "}\n";
43}
Chris Lattnera3ae6142003-08-03 21:57:51 +000044
Chris Lattnerbc017232003-08-06 04:32:07 +000045void InstrInfoEmitter::printDefList(ListInit *LI, const std::string &Name,
46 std::ostream &OS) const {
Chris Lattnera3ae6142003-08-03 21:57:51 +000047 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()) << ", ";
51 else
52 throw "Illegal value in '" + Name + "' list!";
53 OS << "0 };\n";
54}
55
56
57// run - Emit the main instruction description records for the target...
58void InstrInfoEmitter::run(std::ostream &OS) {
Chris Lattnerbc017232003-08-06 04:32:07 +000059 EmitSourceFileHeader("Target Instruction Descriptors", OS);
Chris Lattner7884b752003-08-07 05:39:09 +000060 CodeGenTarget Target;
61 const std::string &TargetName = Target.getName();
62 Record *InstrInfo = Target.getInstructionSet();
Chris Lattnera3ae6142003-08-03 21:57:51 +000063 Record *PHI = InstrInfo->getValueAsDef("PHIInst");
64
65 std::vector<Record*> Instructions =
66 Records.getAllDerivedDefinitions("Instruction");
67
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +000068 // Emit empty implicit uses and defs lists
69 OS << "static const unsigned EmptyImpUses[] = { 0 };\n"
70 << "static const unsigned EmptyImpDefs[] = { 0 };\n";
71
Chris Lattnera3ae6142003-08-03 21:57:51 +000072 // 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);
79 }
80
81 OS << "\nstatic const TargetInstrDescriptor " << TargetName
82 << "Insts[] = {\n";
83 emitRecord(PHI, 0, InstrInfo, OS);
84
85 for (unsigned i = 0, e = Instructions.size(); i != e; ++i)
86 if (Instructions[i] != PHI)
87 emitRecord(Instructions[i], i+1, InstrInfo, OS);
88 OS << "};\n";
89}
90
91void InstrInfoEmitter::emitRecord(Record *R, unsigned Num, Record *InstrInfo,
92 std::ostream &OS) {
93 OS << " { \"" << R->getValueAsString("Name")
94 << "\",\t-1, -1, 0, false, 0, 0, 0, 0";
95
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";
102 OS << ", 0";
103
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!";
110
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);
114
115 OS << ", ";
116
117 // Emit the implicit uses and defs lists...
118 LI = R->getValueAsListInit("Uses");
119 if (!LI->getSize())
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000120 OS << "EmptyImpUses, ";
Chris Lattnera3ae6142003-08-03 21:57:51 +0000121 else
122 OS << R->getName() << "ImpUses, ";
123
124 LI = R->getValueAsListInit("Defs");
125 if (!LI->getSize())
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000126 OS << "EmptyImpDefs ";
Chris Lattnera3ae6142003-08-03 21:57:51 +0000127 else
128 OS << R->getName() << "ImpDefs ";
129
130 OS << " }, // Inst #" << Num << " = " << R->getName() << "\n";
131}
132
133void 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();
139
140 if (RV == 0 || RV->getValue() == 0)
141 throw R->getName() + " doesn't have a field named '" + Val->getValue()+"'!";
142
143 Init *Value = RV->getValue();
144 if (BitInit *BI = dynamic_cast<BitInit*>(Value)) {
145 if (BI->getValue()) OS << "|(1<<" << Shift << ")";
146 return;
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());
150 if (I)
151 if (IntInit *II = dynamic_cast<IntInit*>(I)) {
152 if (II->getValue())
153 OS << "|(" << II->getValue() << "<<" << Shift << ")";
154 return;
155 }
156
157 } else if (IntInit *II = dynamic_cast<IntInit*>(Value)) {
158 if (II->getValue()) OS << "|(" << II->getValue() << "<<" << Shift << ")";
159 return;
160 }
161
162 std::cerr << "Unhandled initializer: " << *Val << "\n";
163 throw "In record '" + R->getName() + "' for TSFlag emission.";
164}