blob: 22ac58df597a84497d3d2ca7fb861fed50339810 [file] [log] [blame]
Chris Lattner33ccf7e2003-08-03 17:24:10 +00001//===- InstrInfoEmitter.cpp - Generate a Instruction Set Desc. ------------===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
John Criswell01d45822003-10-20 20:20:30 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman3da94ae2005-04-22 00:00:37 +00007//
John Criswell01d45822003-10-20 20:20:30 +00008//===----------------------------------------------------------------------===//
Chris Lattner33ccf7e2003-08-03 17:24:10 +00009//
10// This tablegen backend is responsible for emitting a description of the target
11// instruction set for the code generator.
12//
13//===----------------------------------------------------------------------===//
14
15#include "InstrInfoEmitter.h"
Chris Lattner803a5f62004-08-01 04:04:35 +000016#include "CodeGenTarget.h"
Chris Lattner33ccf7e2003-08-03 17:24:10 +000017#include "Record.h"
Jeff Cohencb366d92005-11-01 18:04:06 +000018#include <algorithm>
Chris Lattner2082ebe2004-08-01 03:55:39 +000019using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000020
Chris Lattner33ccf7e2003-08-03 17:24:10 +000021// runEnums - Print out enum values for all of the instructions.
22void InstrInfoEmitter::runEnums(std::ostream &OS) {
Chris Lattnerbc017232003-08-06 04:32:07 +000023 EmitSourceFileHeader("Target Instruction Enum Values", OS);
Chris Lattner2c384132004-08-17 03:08:28 +000024 OS << "namespace llvm {\n\n";
Chris Lattner33ccf7e2003-08-03 17:24:10 +000025
Chris Lattner7884b752003-08-07 05:39:09 +000026 CodeGenTarget Target;
27
Chris Lattnera3ae6142003-08-03 21:57:51 +000028 // We must emit the PHI opcode first...
Chris Lattner7884b752003-08-07 05:39:09 +000029 Record *InstrInfo = Target.getInstructionSet();
Chris Lattner33ccf7e2003-08-03 17:24:10 +000030
Chris Lattnerec352402004-08-01 05:04:00 +000031 std::string Namespace = Target.inst_begin()->second.Namespace;
32
33 if (!Namespace.empty())
34 OS << "namespace " << Namespace << " {\n";
35 OS << " enum {\n";
36
Chris Lattnerd6488672005-01-22 18:58:51 +000037 std::vector<const CodeGenInstruction*> NumberedInstructions;
38 Target.getInstructionsByEnumValue(NumberedInstructions);
39
40 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
41 OS << " " << NumberedInstructions[i]->TheDef->getName()
42 << ", \t// " << i << "\n";
43 }
Chris Lattner9fdd6e32005-08-26 20:17:00 +000044 OS << " INSTRUCTION_LIST_END\n";
Chris Lattner33ccf7e2003-08-03 17:24:10 +000045 OS << " };\n";
46 if (!Namespace.empty())
47 OS << "}\n";
Chris Lattner2c384132004-08-17 03:08:28 +000048 OS << "} // End llvm namespace \n";
Chris Lattner33ccf7e2003-08-03 17:24:10 +000049}
Chris Lattnera3ae6142003-08-03 21:57:51 +000050
Chris Lattnera3ac88d2005-08-18 21:36:47 +000051void InstrInfoEmitter::printDefList(const std::vector<Record*> &Uses,
52 unsigned Num, std::ostream &OS) const {
53 OS << "static const unsigned ImplicitList" << Num << "[] = { ";
54 for (unsigned i = 0, e = Uses.size(); i != e; ++i)
55 OS << getQualifiedName(Uses[i]) << ", ";
Chris Lattnera3ae6142003-08-03 21:57:51 +000056 OS << "0 };\n";
57}
58
Chris Lattnerd5aa3e22005-08-19 18:46:26 +000059static std::vector<Record*> GetOperandInfo(const CodeGenInstruction &Inst) {
60 std::vector<Record*> Result;
61 if (Inst.hasVariableNumberOfOperands)
62 return Result; // No info for variable operand instrs.
63
64 for (unsigned i = 0, e = Inst.OperandList.size(); i != e; ++i) {
Chris Lattner65303d62005-11-19 07:05:57 +000065 if (Inst.OperandList[i].Rec->isSubClassOf("RegisterClass")) {
Chris Lattnerd5aa3e22005-08-19 18:46:26 +000066 Result.push_back(Inst.OperandList[i].Rec);
Chris Lattner65303d62005-11-19 07:05:57 +000067 } else {
Chris Lattnerd5aa3e22005-08-19 18:46:26 +000068 // This might be a multiple operand thing.
Chris Lattner65303d62005-11-19 07:05:57 +000069 // Targets like X86 have registers in their multi-operand operands.
70 DagInit *MIOI = Inst.OperandList[i].MIOperandInfo;
71 unsigned NumDefs = MIOI->getNumArgs();
72 for (unsigned j = 0, e = Inst.OperandList[i].MINumOperands; j != e; ++j) {
73 if (NumDefs <= j) {
74 Result.push_back(0);
75 } else {
76 DefInit *Def = dynamic_cast<DefInit*>(MIOI->getArg(j));
77 Result.push_back(Def ? Def->getDef() : 0);
78 }
79 }
Chris Lattnerd5aa3e22005-08-19 18:46:26 +000080 }
81 }
82 return Result;
83}
84
Chris Lattnera3ae6142003-08-03 21:57:51 +000085
86// run - Emit the main instruction description records for the target...
87void InstrInfoEmitter::run(std::ostream &OS) {
Jim Laskeyb5a0c0e2005-10-31 17:16:46 +000088 GatherItinClasses();
89
Chris Lattnerbc017232003-08-06 04:32:07 +000090 EmitSourceFileHeader("Target Instruction Descriptors", OS);
Chris Lattner2c384132004-08-17 03:08:28 +000091 OS << "namespace llvm {\n\n";
92
Chris Lattner7884b752003-08-07 05:39:09 +000093 CodeGenTarget Target;
94 const std::string &TargetName = Target.getName();
95 Record *InstrInfo = Target.getInstructionSet();
Chris Lattnera3ae6142003-08-03 21:57:51 +000096
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +000097 // Emit empty implicit uses and defs lists
Chris Lattnera3ac88d2005-08-18 21:36:47 +000098 OS << "static const unsigned EmptyImpList[] = { 0 };\n";
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +000099
Chris Lattnera3ac88d2005-08-18 21:36:47 +0000100 // Keep track of all of the def lists we have emitted already.
101 std::map<std::vector<Record*>, unsigned> EmittedLists;
Chris Lattnera3ac88d2005-08-18 21:36:47 +0000102 unsigned ListNumber = 0;
103
104 // Emit all of the instruction's implicit uses and defs.
Chris Lattnerec352402004-08-01 05:04:00 +0000105 for (CodeGenTarget::inst_iterator II = Target.inst_begin(),
106 E = Target.inst_end(); II != E; ++II) {
107 Record *Inst = II->second.TheDef;
Chris Lattner366080c2005-10-28 22:59:53 +0000108 std::vector<Record*> Uses = Inst->getValueAsListOfDefs("Uses");
109 if (!Uses.empty()) {
Chris Lattnera3ac88d2005-08-18 21:36:47 +0000110 unsigned &IL = EmittedLists[Uses];
111 if (!IL) printDefList(Uses, IL = ++ListNumber, OS);
Chris Lattnera3ac88d2005-08-18 21:36:47 +0000112 }
Chris Lattner366080c2005-10-28 22:59:53 +0000113 std::vector<Record*> Defs = Inst->getValueAsListOfDefs("Defs");
114 if (!Defs.empty()) {
115 unsigned &IL = EmittedLists[Defs];
116 if (!IL) printDefList(Defs, IL = ++ListNumber, OS);
Chris Lattnera3ac88d2005-08-18 21:36:47 +0000117 }
Chris Lattnera3ae6142003-08-03 21:57:51 +0000118 }
119
Chris Lattnerd5aa3e22005-08-19 18:46:26 +0000120 std::map<std::vector<Record*>, unsigned> OperandInfosEmitted;
121 unsigned OperandListNum = 0;
122 OperandInfosEmitted[std::vector<Record*>()] = ++OperandListNum;
123
Chris Lattner0e384b62005-08-19 16:57:28 +0000124 // Emit all of the operand info records.
125 OS << "\n";
126 for (CodeGenTarget::inst_iterator II = Target.inst_begin(),
127 E = Target.inst_end(); II != E; ++II) {
Chris Lattnerd5aa3e22005-08-19 18:46:26 +0000128 std::vector<Record*> OperandInfo = GetOperandInfo(II->second);
129 unsigned &N = OperandInfosEmitted[OperandInfo];
130 if (N == 0) {
131 N = ++OperandListNum;
132 OS << "static const TargetOperandInfo OperandInfo" << N << "[] = { ";
133 for (unsigned i = 0, e = OperandInfo.size(); i != e; ++i) {
Chris Lattner65303d62005-11-19 07:05:57 +0000134 Record *RC = OperandInfo[i];
135 // FIXME: We only care about register operands for now.
136 if (RC && RC->isSubClassOf("RegisterClass")) {
Chris Lattner8d30c232005-08-19 20:29:14 +0000137 OS << "{ &" << getQualifiedName(RC) << "RegClass }, ";
Chris Lattnerd5aa3e22005-08-19 18:46:26 +0000138 } else {
139 OS << "{ 0 }, ";
140 }
141 }
Chris Lattner0e384b62005-08-19 16:57:28 +0000142 OS << "};\n";
143 }
144 }
145
Chris Lattnerf52e2612006-01-27 01:44:09 +0000146 // Emit all of the TargetInstrDescriptor records in their ENUM ordering.
Chris Lattner0e384b62005-08-19 16:57:28 +0000147 //
Chris Lattnera3ae6142003-08-03 21:57:51 +0000148 OS << "\nstatic const TargetInstrDescriptor " << TargetName
149 << "Insts[] = {\n";
Chris Lattnerf52e2612006-01-27 01:44:09 +0000150 std::vector<const CodeGenInstruction*> NumberedInstructions;
151 Target.getInstructionsByEnumValue(NumberedInstructions);
Chris Lattnera3ae6142003-08-03 21:57:51 +0000152
Chris Lattnerf52e2612006-01-27 01:44:09 +0000153 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i)
154 emitRecord(*NumberedInstructions[i], i, InstrInfo, EmittedLists,
155 OperandInfosEmitted, OS);
Chris Lattnera3ae6142003-08-03 21:57:51 +0000156 OS << "};\n";
Chris Lattner2c384132004-08-17 03:08:28 +0000157 OS << "} // End llvm namespace \n";
Chris Lattnera3ae6142003-08-03 21:57:51 +0000158}
159
Chris Lattnerec352402004-08-01 05:04:00 +0000160void InstrInfoEmitter::emitRecord(const CodeGenInstruction &Inst, unsigned Num,
Chris Lattnera3ac88d2005-08-18 21:36:47 +0000161 Record *InstrInfo,
Chris Lattner366080c2005-10-28 22:59:53 +0000162 std::map<std::vector<Record*>, unsigned> &EmittedLists,
Chris Lattnerd5aa3e22005-08-19 18:46:26 +0000163 std::map<std::vector<Record*>, unsigned> &OpInfo,
Chris Lattnera3ac88d2005-08-18 21:36:47 +0000164 std::ostream &OS) {
Chris Lattnerd98958f2005-08-19 00:59:49 +0000165 int NumOperands;
166 if (Inst.hasVariableNumberOfOperands)
167 NumOperands = -1;
168 else if (!Inst.OperandList.empty())
169 // Each logical operand can be multiple MI operands.
170 NumOperands = Inst.OperandList.back().MIOperandNo +
171 Inst.OperandList.back().MINumOperands;
172 else
173 NumOperands = 0;
174
Chris Lattner2d12b2c2004-08-01 08:38:17 +0000175 OS << " { \"";
176 if (Inst.Name.empty())
177 OS << Inst.TheDef->getName();
178 else
179 OS << Inst.Name;
Jim Laskeyb5a0c0e2005-10-31 17:16:46 +0000180
181 unsigned ItinClass = !IsItineraries ? 0 :
182 ItinClassNumber(Inst.TheDef->getValueAsDef("Itinerary")->getName());
183
184 OS << "\",\t" << NumOperands << ", -1, 0, false, 0, 0, "
185 << ItinClass
186 << ", 0";
Chris Lattnera3ae6142003-08-03 21:57:51 +0000187
188 // Emit all of the target indepedent flags...
Chris Lattnerec352402004-08-01 05:04:00 +0000189 if (Inst.isReturn) OS << "|M_RET_FLAG";
190 if (Inst.isBranch) OS << "|M_BRANCH_FLAG";
191 if (Inst.isBarrier) OS << "|M_BARRIER_FLAG";
Chris Lattner5b71d3a2004-09-28 18:38:01 +0000192 if (Inst.hasDelaySlot) OS << "|M_DELAY_SLOT_FLAG";
Chris Lattnerec352402004-08-01 05:04:00 +0000193 if (Inst.isCall) OS << "|M_CALL_FLAG";
Nate Begemancdd66b52004-09-28 21:01:45 +0000194 if (Inst.isLoad) OS << "|M_LOAD_FLAG";
195 if (Inst.isStore) OS << "|M_STORE_FLAG";
Chris Lattnerec352402004-08-01 05:04:00 +0000196 if (Inst.isTwoAddress) OS << "|M_2_ADDR_FLAG";
Chris Lattneraad75aa2005-01-02 02:29:04 +0000197 if (Inst.isConvertibleToThreeAddress) OS << "|M_CONVERTIBLE_TO_3_ADDR";
198 if (Inst.isCommutable) OS << "|M_COMMUTABLE";
Chris Lattnerec352402004-08-01 05:04:00 +0000199 if (Inst.isTerminator) OS << "|M_TERMINATOR_FLAG";
Chris Lattner5f89bf02005-08-26 20:42:52 +0000200 if (Inst.usesCustomDAGSchedInserter)
Chris Lattner8b50f9b2005-08-26 20:40:46 +0000201 OS << "|M_USES_CUSTOM_DAG_SCHED_INSERTION";
Chris Lattnera3ae6142003-08-03 21:57:51 +0000202 OS << ", 0";
203
204 // Emit all of the target-specific flags...
205 ListInit *LI = InstrInfo->getValueAsListInit("TSFlagsFields");
206 ListInit *Shift = InstrInfo->getValueAsListInit("TSFlagsShifts");
207 if (LI->getSize() != Shift->getSize())
208 throw "Lengths of " + InstrInfo->getName() +
209 ":(TargetInfoFields, TargetInfoPositions) must be equal!";
210
211 for (unsigned i = 0, e = LI->getSize(); i != e; ++i)
Chris Lattnerec352402004-08-01 05:04:00 +0000212 emitShiftedValue(Inst.TheDef, dynamic_cast<StringInit*>(LI->getElement(i)),
Chris Lattnera3ae6142003-08-03 21:57:51 +0000213 dynamic_cast<IntInit*>(Shift->getElement(i)), OS);
214
215 OS << ", ";
216
217 // Emit the implicit uses and defs lists...
Chris Lattner366080c2005-10-28 22:59:53 +0000218 std::vector<Record*> UseList = Inst.TheDef->getValueAsListOfDefs("Uses");
219 if (UseList.empty())
Chris Lattnera3ac88d2005-08-18 21:36:47 +0000220 OS << "EmptyImpList, ";
Misha Brukman3da94ae2005-04-22 00:00:37 +0000221 else
Chris Lattner366080c2005-10-28 22:59:53 +0000222 OS << "ImplicitList" << EmittedLists[UseList] << ", ";
Chris Lattnera3ae6142003-08-03 21:57:51 +0000223
Chris Lattner366080c2005-10-28 22:59:53 +0000224 std::vector<Record*> DefList = Inst.TheDef->getValueAsListOfDefs("Defs");
225 if (DefList.empty())
Chris Lattner0e384b62005-08-19 16:57:28 +0000226 OS << "EmptyImpList, ";
Misha Brukman3da94ae2005-04-22 00:00:37 +0000227 else
Chris Lattner366080c2005-10-28 22:59:53 +0000228 OS << "ImplicitList" << EmittedLists[DefList] << ", ";
Chris Lattnera3ae6142003-08-03 21:57:51 +0000229
Chris Lattner0e384b62005-08-19 16:57:28 +0000230 // Emit the operand info.
Chris Lattnerd5aa3e22005-08-19 18:46:26 +0000231 std::vector<Record*> OperandInfo = GetOperandInfo(Inst);
232 if (OperandInfo.empty())
233 OS << "0";
Chris Lattner0e384b62005-08-19 16:57:28 +0000234 else
Chris Lattnerd5aa3e22005-08-19 18:46:26 +0000235 OS << "OperandInfo" << OpInfo[OperandInfo];
Chris Lattner0e384b62005-08-19 16:57:28 +0000236
Chris Lattnerec352402004-08-01 05:04:00 +0000237 OS << " }, // Inst #" << Num << " = " << Inst.TheDef->getName() << "\n";
Chris Lattnera3ae6142003-08-03 21:57:51 +0000238}
239
Jim Laskeyb5a0c0e2005-10-31 17:16:46 +0000240struct LessRecord {
241 bool operator()(const Record *Rec1, const Record *Rec2) const {
242 return Rec1->getName() < Rec2->getName();
243 }
244};
245void InstrInfoEmitter::GatherItinClasses() {
246 std::vector<Record*> DefList =
247 Records.getAllDerivedDefinitions("InstrItinClass");
248 IsItineraries = !DefList.empty();
249
250 if (!IsItineraries) return;
251
Duraid Madina42d24c72005-12-30 14:56:37 +0000252 std::sort(DefList.begin(), DefList.end(), LessRecord());
Jim Laskeyb5a0c0e2005-10-31 17:16:46 +0000253
254 for (unsigned i = 0, N = DefList.size(); i < N; i++) {
255 Record *Def = DefList[i];
Jim Laskey6cee6302005-11-01 20:06:59 +0000256 ItinClassMap[Def->getName()] = i;
Jim Laskeyb5a0c0e2005-10-31 17:16:46 +0000257 }
258}
259
260unsigned InstrInfoEmitter::ItinClassNumber(std::string ItinName) {
261 return ItinClassMap[ItinName];
262}
263
Chris Lattnera3ae6142003-08-03 21:57:51 +0000264void InstrInfoEmitter::emitShiftedValue(Record *R, StringInit *Val,
265 IntInit *ShiftInt, std::ostream &OS) {
266 if (Val == 0 || ShiftInt == 0)
267 throw std::string("Illegal value or shift amount in TargetInfo*!");
268 RecordVal *RV = R->getValue(Val->getValue());
269 int Shift = ShiftInt->getValue();
270
Chris Lattnerf52e2612006-01-27 01:44:09 +0000271 if (RV == 0 || RV->getValue() == 0) {
272 // This isn't an error if this is a builtin instruction.
273 if (R->getName() != "PHI" && R->getName() != "INLINEASM")
274 throw R->getName() + " doesn't have a field named '" +
275 Val->getValue() + "'!";
276 return;
277 }
Chris Lattnera3ae6142003-08-03 21:57:51 +0000278
279 Init *Value = RV->getValue();
280 if (BitInit *BI = dynamic_cast<BitInit*>(Value)) {
281 if (BI->getValue()) OS << "|(1<<" << Shift << ")";
282 return;
283 } else if (BitsInit *BI = dynamic_cast<BitsInit*>(Value)) {
284 // Convert the Bits to an integer to print...
285 Init *I = BI->convertInitializerTo(new IntRecTy());
286 if (I)
287 if (IntInit *II = dynamic_cast<IntInit*>(I)) {
Chris Lattnerf52e2612006-01-27 01:44:09 +0000288 if (II->getValue()) {
289 if (Shift)
290 OS << "|(" << II->getValue() << "<<" << Shift << ")";
291 else
292 OS << "|" << II->getValue();
293 }
Chris Lattnera3ae6142003-08-03 21:57:51 +0000294 return;
295 }
296
297 } else if (IntInit *II = dynamic_cast<IntInit*>(Value)) {
Chris Lattnerf52e2612006-01-27 01:44:09 +0000298 if (II->getValue()) {
299 if (Shift)
300 OS << "|(" << II->getValue() << "<<" << Shift << ")";
301 else
302 OS << II->getValue();
303 }
Chris Lattnera3ae6142003-08-03 21:57:51 +0000304 return;
305 }
306
307 std::cerr << "Unhandled initializer: " << *Val << "\n";
308 throw "In record '" + R->getName() + "' for TSFlag emission.";
309}
Brian Gaeked0fde302003-11-11 22:41:34 +0000310