blob: c794cd04418b5975b718093093673301a2b4a532 [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
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);
75 }
76
77 OS << "\nstatic const TargetInstrDescriptor " << TargetName
78 << "Insts[] = {\n";
79 emitRecord(PHI, 0, InstrInfo, OS);
80
81 for (unsigned i = 0, e = Instructions.size(); i != e; ++i)
82 if (Instructions[i] != PHI)
83 emitRecord(Instructions[i], i+1, InstrInfo, OS);
84 OS << "};\n";
85}
86
87void InstrInfoEmitter::emitRecord(Record *R, unsigned Num, Record *InstrInfo,
88 std::ostream &OS) {
89 OS << " { \"" << R->getValueAsString("Name")
90 << "\",\t-1, -1, 0, false, 0, 0, 0, 0";
91
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";
98 OS << ", 0";
99
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!";
106
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);
110
111 OS << ", ";
112
113 // Emit the implicit uses and defs lists...
114 LI = R->getValueAsListInit("Uses");
115 if (!LI->getSize())
116 OS << "0, ";
117 else
118 OS << R->getName() << "ImpUses, ";
119
120 LI = R->getValueAsListInit("Defs");
121 if (!LI->getSize())
122 OS << "0 ";
123 else
124 OS << R->getName() << "ImpDefs ";
125
126 OS << " }, // Inst #" << Num << " = " << R->getName() << "\n";
127}
128
129void 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();
135
136 if (RV == 0 || RV->getValue() == 0)
137 throw R->getName() + " doesn't have a field named '" + Val->getValue()+"'!";
138
139 Init *Value = RV->getValue();
140 if (BitInit *BI = dynamic_cast<BitInit*>(Value)) {
141 if (BI->getValue()) OS << "|(1<<" << Shift << ")";
142 return;
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());
146 if (I)
147 if (IntInit *II = dynamic_cast<IntInit*>(I)) {
148 if (II->getValue())
149 OS << "|(" << II->getValue() << "<<" << Shift << ")";
150 return;
151 }
152
153 } else if (IntInit *II = dynamic_cast<IntInit*>(Value)) {
154 if (II->getValue()) OS << "|(" << II->getValue() << "<<" << Shift << ")";
155 return;
156 }
157
158 std::cerr << "Unhandled initializer: " << *Val << "\n";
159 throw "In record '" + R->getName() + "' for TSFlag emission.";
160}