blob: f91babcf860bccc03baea8e0a5af9ee6acacc109 [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 Lattnereff5c362006-05-01 23:46:16 +000029 std::string Namespace;
30 for (CodeGenTarget::inst_iterator II = Target.inst_begin(),
31 E = Target.inst_end(); II != E; ++II) {
32 if (II->second.Namespace != "TargetInstrInfo") {
33 Namespace = II->second.Namespace;
34 break;
35 }
36 }
37
38 if (Namespace.empty()) {
39 std::cerr << "No instructions defined!\n";
40 exit(1);
41 }
Chris Lattnerec352402004-08-01 05:04:00 +000042
Chris Lattnerd6488672005-01-22 18:58:51 +000043 std::vector<const CodeGenInstruction*> NumberedInstructions;
44 Target.getInstructionsByEnumValue(NumberedInstructions);
45
Chris Lattnereff5c362006-05-01 23:46:16 +000046 OS << "namespace " << Namespace << " {\n";
47 OS << " enum {\n";
Chris Lattnerd6488672005-01-22 18:58:51 +000048 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
49 OS << " " << NumberedInstructions[i]->TheDef->getName()
Chris Lattnereff5c362006-05-01 23:46:16 +000050 << "\t= " << i << ",\n";
Chris Lattnerd6488672005-01-22 18:58:51 +000051 }
Chris Lattnereff5c362006-05-01 23:46:16 +000052 OS << " INSTRUCTION_LIST_END = " << NumberedInstructions.size() << "\n";
53 OS << " };\n}\n";
Chris Lattner2c384132004-08-17 03:08:28 +000054 OS << "} // End llvm namespace \n";
Chris Lattner33ccf7e2003-08-03 17:24:10 +000055}
Chris Lattnera3ae6142003-08-03 21:57:51 +000056
Chris Lattnera3ac88d2005-08-18 21:36:47 +000057void InstrInfoEmitter::printDefList(const std::vector<Record*> &Uses,
58 unsigned Num, std::ostream &OS) const {
59 OS << "static const unsigned ImplicitList" << Num << "[] = { ";
60 for (unsigned i = 0, e = Uses.size(); i != e; ++i)
61 OS << getQualifiedName(Uses[i]) << ", ";
Chris Lattnera3ae6142003-08-03 21:57:51 +000062 OS << "0 };\n";
63}
64
Chris Lattnerd5aa3e22005-08-19 18:46:26 +000065static std::vector<Record*> GetOperandInfo(const CodeGenInstruction &Inst) {
66 std::vector<Record*> Result;
Chris Lattnerd5aa3e22005-08-19 18:46:26 +000067 for (unsigned i = 0, e = Inst.OperandList.size(); i != e; ++i) {
Chris Lattner65303d62005-11-19 07:05:57 +000068 if (Inst.OperandList[i].Rec->isSubClassOf("RegisterClass")) {
Chris Lattnerd5aa3e22005-08-19 18:46:26 +000069 Result.push_back(Inst.OperandList[i].Rec);
Chris Lattner65303d62005-11-19 07:05:57 +000070 } else {
Chris Lattnerd5aa3e22005-08-19 18:46:26 +000071 // This might be a multiple operand thing.
Chris Lattner65303d62005-11-19 07:05:57 +000072 // Targets like X86 have registers in their multi-operand operands.
73 DagInit *MIOI = Inst.OperandList[i].MIOperandInfo;
74 unsigned NumDefs = MIOI->getNumArgs();
75 for (unsigned j = 0, e = Inst.OperandList[i].MINumOperands; j != e; ++j) {
76 if (NumDefs <= j) {
77 Result.push_back(0);
78 } else {
79 DefInit *Def = dynamic_cast<DefInit*>(MIOI->getArg(j));
80 Result.push_back(Def ? Def->getDef() : 0);
81 }
82 }
Chris Lattnerd5aa3e22005-08-19 18:46:26 +000083 }
84 }
85 return Result;
86}
87
Chris Lattnera3ae6142003-08-03 21:57:51 +000088
89// run - Emit the main instruction description records for the target...
90void InstrInfoEmitter::run(std::ostream &OS) {
Jim Laskeyb5a0c0e2005-10-31 17:16:46 +000091 GatherItinClasses();
92
Chris Lattnerbc017232003-08-06 04:32:07 +000093 EmitSourceFileHeader("Target Instruction Descriptors", OS);
Chris Lattner2c384132004-08-17 03:08:28 +000094 OS << "namespace llvm {\n\n";
95
Chris Lattner7884b752003-08-07 05:39:09 +000096 CodeGenTarget Target;
97 const std::string &TargetName = Target.getName();
98 Record *InstrInfo = Target.getInstructionSet();
Chris Lattnera3ae6142003-08-03 21:57:51 +000099
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000100 // Emit empty implicit uses and defs lists
Chris Lattnera3ac88d2005-08-18 21:36:47 +0000101 OS << "static const unsigned EmptyImpList[] = { 0 };\n";
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000102
Chris Lattnera3ac88d2005-08-18 21:36:47 +0000103 // Keep track of all of the def lists we have emitted already.
104 std::map<std::vector<Record*>, unsigned> EmittedLists;
Chris Lattnera3ac88d2005-08-18 21:36:47 +0000105 unsigned ListNumber = 0;
106
107 // Emit all of the instruction's implicit uses and defs.
Chris Lattnerec352402004-08-01 05:04:00 +0000108 for (CodeGenTarget::inst_iterator II = Target.inst_begin(),
109 E = Target.inst_end(); II != E; ++II) {
110 Record *Inst = II->second.TheDef;
Chris Lattner366080c2005-10-28 22:59:53 +0000111 std::vector<Record*> Uses = Inst->getValueAsListOfDefs("Uses");
112 if (!Uses.empty()) {
Chris Lattnera3ac88d2005-08-18 21:36:47 +0000113 unsigned &IL = EmittedLists[Uses];
114 if (!IL) printDefList(Uses, IL = ++ListNumber, OS);
Chris Lattnera3ac88d2005-08-18 21:36:47 +0000115 }
Chris Lattner366080c2005-10-28 22:59:53 +0000116 std::vector<Record*> Defs = Inst->getValueAsListOfDefs("Defs");
117 if (!Defs.empty()) {
118 unsigned &IL = EmittedLists[Defs];
119 if (!IL) printDefList(Defs, IL = ++ListNumber, OS);
Chris Lattnera3ac88d2005-08-18 21:36:47 +0000120 }
Chris Lattnera3ae6142003-08-03 21:57:51 +0000121 }
122
Chris Lattnerd5aa3e22005-08-19 18:46:26 +0000123 std::map<std::vector<Record*>, unsigned> OperandInfosEmitted;
124 unsigned OperandListNum = 0;
125 OperandInfosEmitted[std::vector<Record*>()] = ++OperandListNum;
126
Chris Lattner0e384b62005-08-19 16:57:28 +0000127 // Emit all of the operand info records.
128 OS << "\n";
129 for (CodeGenTarget::inst_iterator II = Target.inst_begin(),
130 E = Target.inst_end(); II != E; ++II) {
Chris Lattnerd5aa3e22005-08-19 18:46:26 +0000131 std::vector<Record*> OperandInfo = GetOperandInfo(II->second);
132 unsigned &N = OperandInfosEmitted[OperandInfo];
133 if (N == 0) {
134 N = ++OperandListNum;
135 OS << "static const TargetOperandInfo OperandInfo" << N << "[] = { ";
136 for (unsigned i = 0, e = OperandInfo.size(); i != e; ++i) {
Chris Lattner65303d62005-11-19 07:05:57 +0000137 Record *RC = OperandInfo[i];
138 // FIXME: We only care about register operands for now.
Evan Cheng21d03f22006-05-18 20:42:07 +0000139 if (RC && RC->isSubClassOf("RegisterClass"))
140 OS << "{ &" << getQualifiedName(RC) << "RegClass, 0 }, ";
141 else if (RC && RC->getName() == "ptr_rc")
142 // Ptr value whose register class is resolved via callback.
143 OS << "{ 0, 1 }, ";
144 else
145 OS << "{ 0, 0 }, ";
Chris Lattnerd5aa3e22005-08-19 18:46:26 +0000146 }
Chris Lattner0e384b62005-08-19 16:57:28 +0000147 OS << "};\n";
148 }
149 }
150
Chris Lattnerf52e2612006-01-27 01:44:09 +0000151 // Emit all of the TargetInstrDescriptor records in their ENUM ordering.
Chris Lattner0e384b62005-08-19 16:57:28 +0000152 //
Chris Lattnera3ae6142003-08-03 21:57:51 +0000153 OS << "\nstatic const TargetInstrDescriptor " << TargetName
154 << "Insts[] = {\n";
Chris Lattnerf52e2612006-01-27 01:44:09 +0000155 std::vector<const CodeGenInstruction*> NumberedInstructions;
156 Target.getInstructionsByEnumValue(NumberedInstructions);
Chris Lattnera3ae6142003-08-03 21:57:51 +0000157
Chris Lattnerf52e2612006-01-27 01:44:09 +0000158 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i)
159 emitRecord(*NumberedInstructions[i], i, InstrInfo, EmittedLists,
160 OperandInfosEmitted, OS);
Chris Lattnera3ae6142003-08-03 21:57:51 +0000161 OS << "};\n";
Chris Lattner2c384132004-08-17 03:08:28 +0000162 OS << "} // End llvm namespace \n";
Chris Lattnera3ae6142003-08-03 21:57:51 +0000163}
164
Chris Lattnerec352402004-08-01 05:04:00 +0000165void InstrInfoEmitter::emitRecord(const CodeGenInstruction &Inst, unsigned Num,
Chris Lattnera3ac88d2005-08-18 21:36:47 +0000166 Record *InstrInfo,
Chris Lattner366080c2005-10-28 22:59:53 +0000167 std::map<std::vector<Record*>, unsigned> &EmittedLists,
Chris Lattnerd5aa3e22005-08-19 18:46:26 +0000168 std::map<std::vector<Record*>, unsigned> &OpInfo,
Chris Lattnera3ac88d2005-08-18 21:36:47 +0000169 std::ostream &OS) {
Evan Cheng8d3af5e2006-06-15 07:22:16 +0000170 int MinOperands;
171 if (!Inst.OperandList.empty())
Chris Lattnerd98958f2005-08-19 00:59:49 +0000172 // Each logical operand can be multiple MI operands.
Evan Cheng8d3af5e2006-06-15 07:22:16 +0000173 MinOperands = Inst.OperandList.back().MIOperandNo +
Chris Lattnerd98958f2005-08-19 00:59:49 +0000174 Inst.OperandList.back().MINumOperands;
175 else
Evan Cheng8d3af5e2006-06-15 07:22:16 +0000176 MinOperands = 0;
Chris Lattnerd98958f2005-08-19 00:59:49 +0000177
Chris Lattner2d12b2c2004-08-01 08:38:17 +0000178 OS << " { \"";
179 if (Inst.Name.empty())
180 OS << Inst.TheDef->getName();
181 else
182 OS << Inst.Name;
Jim Laskeyb5a0c0e2005-10-31 17:16:46 +0000183
184 unsigned ItinClass = !IsItineraries ? 0 :
185 ItinClassNumber(Inst.TheDef->getValueAsDef("Itinerary")->getName());
186
Evan Cheng8d3af5e2006-06-15 07:22:16 +0000187 OS << "\",\t" << MinOperands << ", " << ItinClass
Jim Laskeyb5a0c0e2005-10-31 17:16:46 +0000188 << ", 0";
Chris Lattnera3ae6142003-08-03 21:57:51 +0000189
Evan Cheng6f6360d2006-05-01 09:04:20 +0000190 // Try to determine (from the pattern), if the instruction is a store.
191 bool isStore = false;
192 if (dynamic_cast<ListInit*>(Inst.TheDef->getValueInit("Pattern"))) {
193 ListInit *LI = Inst.TheDef->getValueAsListInit("Pattern");
194 if (LI && LI->getSize() > 0) {
195 DagInit *Dag = (DagInit *)LI->getElement(0);
196 DefInit *OpDef = dynamic_cast<DefInit*>(Dag->getOperator());
197 if (OpDef) {
198 Record *Operator = OpDef->getDef();
Evan Cheng108714c2006-05-03 02:08:34 +0000199 if (Operator->isSubClassOf("SDNode")) {
200 const std::string Opcode = Operator->getValueAsString("Opcode");
201 if (Opcode == "ISD::STORE" || Opcode == "ISD::TRUNCSTORE")
202 isStore = true;
203 }
Evan Cheng6f6360d2006-05-01 09:04:20 +0000204 }
205 }
206 }
207
Chris Lattnera3ae6142003-08-03 21:57:51 +0000208 // Emit all of the target indepedent flags...
Chris Lattnerec352402004-08-01 05:04:00 +0000209 if (Inst.isReturn) OS << "|M_RET_FLAG";
210 if (Inst.isBranch) OS << "|M_BRANCH_FLAG";
211 if (Inst.isBarrier) OS << "|M_BARRIER_FLAG";
Chris Lattner5b71d3a2004-09-28 18:38:01 +0000212 if (Inst.hasDelaySlot) OS << "|M_DELAY_SLOT_FLAG";
Chris Lattnerec352402004-08-01 05:04:00 +0000213 if (Inst.isCall) OS << "|M_CALL_FLAG";
Nate Begemancdd66b52004-09-28 21:01:45 +0000214 if (Inst.isLoad) OS << "|M_LOAD_FLAG";
Evan Cheng6f6360d2006-05-01 09:04:20 +0000215 if (Inst.isStore || isStore) OS << "|M_STORE_FLAG";
Chris Lattnerec352402004-08-01 05:04:00 +0000216 if (Inst.isTwoAddress) OS << "|M_2_ADDR_FLAG";
Chris Lattneraad75aa2005-01-02 02:29:04 +0000217 if (Inst.isConvertibleToThreeAddress) OS << "|M_CONVERTIBLE_TO_3_ADDR";
218 if (Inst.isCommutable) OS << "|M_COMMUTABLE";
Chris Lattnerec352402004-08-01 05:04:00 +0000219 if (Inst.isTerminator) OS << "|M_TERMINATOR_FLAG";
Chris Lattner5f89bf02005-08-26 20:42:52 +0000220 if (Inst.usesCustomDAGSchedInserter)
Chris Lattner8b50f9b2005-08-26 20:40:46 +0000221 OS << "|M_USES_CUSTOM_DAG_SCHED_INSERTION";
Evan Cheng8d3af5e2006-06-15 07:22:16 +0000222 if (Inst.hasVariableNumberOfOperands)
223 OS << "|M_VARIABLE_OPS";
Chris Lattnera3ae6142003-08-03 21:57:51 +0000224 OS << ", 0";
225
226 // Emit all of the target-specific flags...
227 ListInit *LI = InstrInfo->getValueAsListInit("TSFlagsFields");
228 ListInit *Shift = InstrInfo->getValueAsListInit("TSFlagsShifts");
229 if (LI->getSize() != Shift->getSize())
230 throw "Lengths of " + InstrInfo->getName() +
231 ":(TargetInfoFields, TargetInfoPositions) must be equal!";
232
233 for (unsigned i = 0, e = LI->getSize(); i != e; ++i)
Chris Lattnerec352402004-08-01 05:04:00 +0000234 emitShiftedValue(Inst.TheDef, dynamic_cast<StringInit*>(LI->getElement(i)),
Chris Lattnera3ae6142003-08-03 21:57:51 +0000235 dynamic_cast<IntInit*>(Shift->getElement(i)), OS);
236
237 OS << ", ";
238
239 // Emit the implicit uses and defs lists...
Chris Lattner366080c2005-10-28 22:59:53 +0000240 std::vector<Record*> UseList = Inst.TheDef->getValueAsListOfDefs("Uses");
241 if (UseList.empty())
Chris Lattnera3ac88d2005-08-18 21:36:47 +0000242 OS << "EmptyImpList, ";
Misha Brukman3da94ae2005-04-22 00:00:37 +0000243 else
Chris Lattner366080c2005-10-28 22:59:53 +0000244 OS << "ImplicitList" << EmittedLists[UseList] << ", ";
Chris Lattnera3ae6142003-08-03 21:57:51 +0000245
Chris Lattner366080c2005-10-28 22:59:53 +0000246 std::vector<Record*> DefList = Inst.TheDef->getValueAsListOfDefs("Defs");
247 if (DefList.empty())
Chris Lattner0e384b62005-08-19 16:57:28 +0000248 OS << "EmptyImpList, ";
Misha Brukman3da94ae2005-04-22 00:00:37 +0000249 else
Chris Lattner366080c2005-10-28 22:59:53 +0000250 OS << "ImplicitList" << EmittedLists[DefList] << ", ";
Chris Lattnera3ae6142003-08-03 21:57:51 +0000251
Chris Lattner0e384b62005-08-19 16:57:28 +0000252 // Emit the operand info.
Chris Lattnerd5aa3e22005-08-19 18:46:26 +0000253 std::vector<Record*> OperandInfo = GetOperandInfo(Inst);
254 if (OperandInfo.empty())
255 OS << "0";
Chris Lattner0e384b62005-08-19 16:57:28 +0000256 else
Chris Lattnerd5aa3e22005-08-19 18:46:26 +0000257 OS << "OperandInfo" << OpInfo[OperandInfo];
Chris Lattner0e384b62005-08-19 16:57:28 +0000258
Chris Lattnerec352402004-08-01 05:04:00 +0000259 OS << " }, // Inst #" << Num << " = " << Inst.TheDef->getName() << "\n";
Chris Lattnera3ae6142003-08-03 21:57:51 +0000260}
261
Jim Laskeyb5a0c0e2005-10-31 17:16:46 +0000262struct LessRecord {
263 bool operator()(const Record *Rec1, const Record *Rec2) const {
264 return Rec1->getName() < Rec2->getName();
265 }
266};
267void InstrInfoEmitter::GatherItinClasses() {
268 std::vector<Record*> DefList =
269 Records.getAllDerivedDefinitions("InstrItinClass");
270 IsItineraries = !DefList.empty();
271
272 if (!IsItineraries) return;
273
Duraid Madina42d24c72005-12-30 14:56:37 +0000274 std::sort(DefList.begin(), DefList.end(), LessRecord());
Jim Laskeyb5a0c0e2005-10-31 17:16:46 +0000275
276 for (unsigned i = 0, N = DefList.size(); i < N; i++) {
277 Record *Def = DefList[i];
Jim Laskey6cee6302005-11-01 20:06:59 +0000278 ItinClassMap[Def->getName()] = i;
Jim Laskeyb5a0c0e2005-10-31 17:16:46 +0000279 }
280}
281
282unsigned InstrInfoEmitter::ItinClassNumber(std::string ItinName) {
283 return ItinClassMap[ItinName];
284}
285
Chris Lattnera3ae6142003-08-03 21:57:51 +0000286void InstrInfoEmitter::emitShiftedValue(Record *R, StringInit *Val,
287 IntInit *ShiftInt, std::ostream &OS) {
288 if (Val == 0 || ShiftInt == 0)
289 throw std::string("Illegal value or shift amount in TargetInfo*!");
290 RecordVal *RV = R->getValue(Val->getValue());
291 int Shift = ShiftInt->getValue();
292
Chris Lattnerf52e2612006-01-27 01:44:09 +0000293 if (RV == 0 || RV->getValue() == 0) {
294 // This isn't an error if this is a builtin instruction.
295 if (R->getName() != "PHI" && R->getName() != "INLINEASM")
296 throw R->getName() + " doesn't have a field named '" +
297 Val->getValue() + "'!";
298 return;
299 }
Chris Lattnera3ae6142003-08-03 21:57:51 +0000300
301 Init *Value = RV->getValue();
302 if (BitInit *BI = dynamic_cast<BitInit*>(Value)) {
303 if (BI->getValue()) OS << "|(1<<" << Shift << ")";
304 return;
305 } else if (BitsInit *BI = dynamic_cast<BitsInit*>(Value)) {
306 // Convert the Bits to an integer to print...
307 Init *I = BI->convertInitializerTo(new IntRecTy());
308 if (I)
309 if (IntInit *II = dynamic_cast<IntInit*>(I)) {
Chris Lattnerf52e2612006-01-27 01:44:09 +0000310 if (II->getValue()) {
311 if (Shift)
312 OS << "|(" << II->getValue() << "<<" << Shift << ")";
313 else
314 OS << "|" << II->getValue();
315 }
Chris Lattnera3ae6142003-08-03 21:57:51 +0000316 return;
317 }
318
319 } else if (IntInit *II = dynamic_cast<IntInit*>(Value)) {
Chris Lattnerf52e2612006-01-27 01:44:09 +0000320 if (II->getValue()) {
321 if (Shift)
322 OS << "|(" << II->getValue() << "<<" << Shift << ")";
323 else
324 OS << II->getValue();
325 }
Chris Lattnera3ae6142003-08-03 21:57:51 +0000326 return;
327 }
328
329 std::cerr << "Unhandled initializer: " << *Val << "\n";
330 throw "In record '" + R->getName() + "' for TSFlag emission.";
331}
Brian Gaeked0fde302003-11-11 22:41:34 +0000332