blob: 1b3605a273c06300ee911807165f78911de008bb [file] [log] [blame]
Chris Lattner803a5f62004-08-01 04:04:35 +00001//===- CodeGenTarget.cpp - CodeGen Target Class Wrapper ---------*- C++ -*-===//
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 Lattner45872072003-08-07 05:38:11 +00009//
Chris Lattner803a5f62004-08-01 04:04:35 +000010// This class wrap target description classes used by the various code
Chris Lattner45872072003-08-07 05:38:11 +000011// generation TableGen backends. This makes it easier to access the data and
12// provides a single place that needs to check it for validity. All of these
13// classes throw exceptions on error conditions.
14//
15//===----------------------------------------------------------------------===//
16
Chris Lattner803a5f62004-08-01 04:04:35 +000017#include "CodeGenTarget.h"
Chris Lattner45872072003-08-07 05:38:11 +000018#include "Record.h"
Chris Lattner560a79f2004-10-03 19:34:31 +000019#include "llvm/ADT/StringExtras.h"
20#include "llvm/Support/CommandLine.h"
Chris Lattner5d7d3db2005-09-14 21:05:02 +000021#include <set>
Chris Lattner75ee2eb2005-10-14 03:54:49 +000022#include <algorithm>
Chris Lattner2082ebe2004-08-01 03:55:39 +000023using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000024
Chris Lattner560a79f2004-10-03 19:34:31 +000025static cl::opt<unsigned>
26AsmWriterNum("asmwriternum", cl::init(0),
27 cl::desc("Make -gen-asm-writer emit assembly writer #N"));
28
Chris Lattner45872072003-08-07 05:38:11 +000029/// getValueType - Return the MCV::ValueType that the specified TableGen record
30/// corresponds to.
Chris Lattner2082ebe2004-08-01 03:55:39 +000031MVT::ValueType llvm::getValueType(Record *Rec) {
Chris Lattner45872072003-08-07 05:38:11 +000032 return (MVT::ValueType)Rec->getValueAsInt("Value");
33}
34
Chris Lattner2082ebe2004-08-01 03:55:39 +000035std::string llvm::getName(MVT::ValueType T) {
Chris Lattner45872072003-08-07 05:38:11 +000036 switch (T) {
Chris Lattnerd3464c12003-08-07 23:15:21 +000037 case MVT::Other: return "UNKNOWN";
38 case MVT::i1: return "i1";
39 case MVT::i8: return "i8";
40 case MVT::i16: return "i16";
41 case MVT::i32: return "i32";
42 case MVT::i64: return "i64";
43 case MVT::i128: return "i128";
44 case MVT::f32: return "f32";
45 case MVT::f64: return "f64";
46 case MVT::f80: return "f80";
47 case MVT::f128: return "f128";
48 case MVT::isVoid:return "void";
49 default: assert(0 && "ILLEGAL VALUE TYPE!"); return "";
Chris Lattner45872072003-08-07 05:38:11 +000050 }
Chris Lattnerd3464c12003-08-07 23:15:21 +000051}
52
Chris Lattner2082ebe2004-08-01 03:55:39 +000053std::string llvm::getEnumName(MVT::ValueType T) {
Chris Lattnerb72fb7e2003-08-10 19:50:32 +000054 switch (T) {
55 case MVT::Other: return "Other";
56 case MVT::i1: return "i1";
57 case MVT::i8: return "i8";
58 case MVT::i16: return "i16";
59 case MVT::i32: return "i32";
60 case MVT::i64: return "i64";
61 case MVT::i128: return "i128";
62 case MVT::f32: return "f32";
63 case MVT::f64: return "f64";
64 case MVT::f80: return "f80";
65 case MVT::f128: return "f128";
66 case MVT::isVoid:return "isVoid";
67 default: assert(0 && "ILLEGAL VALUE TYPE!"); return "";
68 }
69}
70
Chris Lattnerd3464c12003-08-07 23:15:21 +000071
Chris Lattner2082ebe2004-08-01 03:55:39 +000072std::ostream &llvm::operator<<(std::ostream &OS, MVT::ValueType T) {
Chris Lattnerd3464c12003-08-07 23:15:21 +000073 return OS << getName(T);
Chris Lattner45872072003-08-07 05:38:11 +000074}
75
76
Chris Lattner45872072003-08-07 05:38:11 +000077/// getTarget - Return the current instance of the Target class.
78///
Brian Gaekebe7f4af2003-10-10 21:55:29 +000079CodeGenTarget::CodeGenTarget() : PointerType(MVT::Other) {
Chris Lattner45872072003-08-07 05:38:11 +000080 std::vector<Record*> Targets = Records.getAllDerivedDefinitions("Target");
Misha Brukmanbebdb202004-06-04 14:59:42 +000081 if (Targets.size() == 0)
Misha Brukman3da94ae2005-04-22 00:00:37 +000082 throw std::string("ERROR: No 'Target' subclasses defined!");
Chris Lattner45872072003-08-07 05:38:11 +000083 if (Targets.size() != 1)
84 throw std::string("ERROR: Multiple subclasses of Target defined!");
85 TargetRec = Targets[0];
86
87 // Read in all of the CalleeSavedRegisters...
88 ListInit *LI = TargetRec->getValueAsListInit("CalleeSavedRegisters");
89 for (unsigned i = 0, e = LI->getSize(); i != e; ++i)
90 if (DefInit *DI = dynamic_cast<DefInit*>(LI->getElement(i)))
91 CalleeSavedRegisters.push_back(DI->getDef());
92 else
93 throw "Target: " + TargetRec->getName() +
94 " expected register definition in CalleeSavedRegisters list!";
95
96 PointerType = getValueType(TargetRec->getValueAsDef("PointerType"));
97}
98
99
100const std::string &CodeGenTarget::getName() const {
101 return TargetRec->getName();
102}
103
104Record *CodeGenTarget::getInstructionSet() const {
105 return TargetRec->getValueAsDef("InstructionSet");
106}
Brian Gaeked0fde302003-11-11 22:41:34 +0000107
Chris Lattner175580c2004-08-14 22:50:53 +0000108/// getAsmWriter - Return the AssemblyWriter definition for this target.
109///
110Record *CodeGenTarget::getAsmWriter() const {
Chris Lattner560a79f2004-10-03 19:34:31 +0000111 ListInit *LI = TargetRec->getValueAsListInit("AssemblyWriters");
112 if (AsmWriterNum >= LI->getSize())
113 throw "Target does not have an AsmWriter #" + utostr(AsmWriterNum) + "!";
114 DefInit *DI = dynamic_cast<DefInit*>(LI->getElement(AsmWriterNum));
115 if (!DI) throw std::string("AssemblyWriter list should be a list of defs!");
116 return DI->getDef();
Chris Lattner175580c2004-08-14 22:50:53 +0000117}
118
Chris Lattner26693112004-08-16 01:10:21 +0000119void CodeGenTarget::ReadRegisters() const {
120 std::vector<Record*> Regs = Records.getAllDerivedDefinitions("Register");
121 if (Regs.empty())
122 throw std::string("No 'Register' subclasses defined!");
123
124 Registers.reserve(Regs.size());
125 Registers.assign(Regs.begin(), Regs.end());
126}
127
Chris Lattner7a680c62004-08-21 02:24:57 +0000128CodeGenRegister::CodeGenRegister(Record *R) : TheDef(R) {
129 DeclaredSpillSize = R->getValueAsInt("SpillSize");
130 DeclaredSpillAlignment = R->getValueAsInt("SpillAlignment");
131}
132
Chris Lattner26693112004-08-16 01:10:21 +0000133const std::string &CodeGenRegister::getName() const {
134 return TheDef->getName();
135}
136
Chris Lattner056afef2004-08-21 04:05:00 +0000137void CodeGenTarget::ReadRegisterClasses() const {
138 std::vector<Record*> RegClasses =
139 Records.getAllDerivedDefinitions("RegisterClass");
140 if (RegClasses.empty())
141 throw std::string("No 'RegisterClass' subclasses defined!");
142
143 RegisterClasses.reserve(RegClasses.size());
144 RegisterClasses.assign(RegClasses.begin(), RegClasses.end());
145}
146
147CodeGenRegisterClass::CodeGenRegisterClass(Record *R) : TheDef(R) {
Chris Lattnerc67c18f2005-08-19 18:45:20 +0000148 // Rename anonymous register classes.
149 if (R->getName().size() > 9 && R->getName()[9] == '.') {
150 static unsigned AnonCounter = 0;
151 R->setName("AnonRegClass_"+utostr(AnonCounter++));
152 }
153
154 Namespace = R->getValueAsString("Namespace");
Chris Lattner056afef2004-08-21 04:05:00 +0000155 SpillSize = R->getValueAsInt("Size");
Chris Lattner037d7322004-08-21 20:15:25 +0000156 SpillAlignment = R->getValueAsInt("Alignment");
Chris Lattnere9f4ba82005-09-08 21:43:21 +0000157 VT = getValueType(R->getValueAsDef("RegType"));
Chris Lattner056afef2004-08-21 04:05:00 +0000158
Chris Lattner8f493132005-09-13 21:44:28 +0000159 MethodBodies = R->getValueAsCode("MethodBodies");
160 MethodProtos = R->getValueAsCode("MethodProtos");
Chris Lattnerac468932005-08-19 19:12:51 +0000161
Chris Lattner056afef2004-08-21 04:05:00 +0000162 ListInit *RegList = R->getValueAsListInit("MemberList");
163 for (unsigned i = 0, e = RegList->getSize(); i != e; ++i) {
164 DefInit *RegDef = dynamic_cast<DefInit*>(RegList->getElement(i));
Misha Brukman3da94ae2005-04-22 00:00:37 +0000165 if (!RegDef) throw "Register class member is not a record!";
Chris Lattner056afef2004-08-21 04:05:00 +0000166 Record *Reg = RegDef->getDef();
167
168 if (!Reg->isSubClassOf("Register"))
169 throw "Register Class member '" + Reg->getName() +
170 "' does not derive from the Register class!";
171 Elements.push_back(Reg);
172 }
173}
174
175const std::string &CodeGenRegisterClass::getName() const {
176 return TheDef->getName();
177}
178
Chris Lattnere9f4ba82005-09-08 21:43:21 +0000179void CodeGenTarget::ReadLegalValueTypes() const {
180 const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses();
181 for (unsigned i = 0, e = RCs.size(); i != e; ++i)
182 LegalValueTypes.push_back(RCs[i].VT);
Chris Lattner75ee2eb2005-10-14 03:54:49 +0000183
184 // Remove duplicates.
185 std::sort(LegalValueTypes.begin(), LegalValueTypes.end());
186 LegalValueTypes.erase(std::unique(LegalValueTypes.begin(),
187 LegalValueTypes.end()),
188 LegalValueTypes.end());
Chris Lattnere9f4ba82005-09-08 21:43:21 +0000189}
Chris Lattner056afef2004-08-21 04:05:00 +0000190
Chris Lattner175580c2004-08-14 22:50:53 +0000191
Chris Lattnerec352402004-08-01 05:04:00 +0000192void CodeGenTarget::ReadInstructions() const {
193 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
194
Chris Lattner26693112004-08-16 01:10:21 +0000195 if (Insts.empty())
Chris Lattnerec352402004-08-01 05:04:00 +0000196 throw std::string("No 'Instruction' subclasses defined!");
197
Chris Lattner175580c2004-08-14 22:50:53 +0000198 std::string InstFormatName =
199 getAsmWriter()->getValueAsString("InstFormatName");
200
201 for (unsigned i = 0, e = Insts.size(); i != e; ++i) {
202 std::string AsmStr = Insts[i]->getValueAsString(InstFormatName);
203 Instructions.insert(std::make_pair(Insts[i]->getName(),
204 CodeGenInstruction(Insts[i], AsmStr)));
205 }
Chris Lattnerec352402004-08-01 05:04:00 +0000206}
207
208/// getPHIInstruction - Return the designated PHI instruction.
Misha Brukman35e83cc2004-10-14 05:50:43 +0000209///
Chris Lattnerec352402004-08-01 05:04:00 +0000210const CodeGenInstruction &CodeGenTarget::getPHIInstruction() const {
211 Record *PHI = getInstructionSet()->getValueAsDef("PHIInst");
212 std::map<std::string, CodeGenInstruction>::const_iterator I =
213 getInstructions().find(PHI->getName());
214 if (I == Instructions.end())
215 throw "Could not find PHI instruction named '" + PHI->getName() + "'!";
216 return I->second;
217}
218
Chris Lattnerd6488672005-01-22 18:58:51 +0000219/// getInstructionsByEnumValue - Return all of the instructions defined by the
220/// target, ordered by their enum value.
221void CodeGenTarget::
222getInstructionsByEnumValue(std::vector<const CodeGenInstruction*>
223 &NumberedInstructions) {
224
225 // Print out the rest of the instructions now.
226 unsigned i = 0;
227 const CodeGenInstruction *PHI = &getPHIInstruction();
228 NumberedInstructions.push_back(PHI);
229 for (inst_iterator II = inst_begin(), E = inst_end(); II != E; ++II)
230 if (&II->second != PHI)
231 NumberedInstructions.push_back(&II->second);
232}
233
234
Misha Brukman35e83cc2004-10-14 05:50:43 +0000235/// isLittleEndianEncoding - Return whether this target encodes its instruction
236/// in little-endian format, i.e. bits laid out in the order [0..n]
237///
238bool CodeGenTarget::isLittleEndianEncoding() const {
239 return getInstructionSet()->getValueAsBit("isLittleEndianEncoding");
240}
241
Chris Lattner175580c2004-08-14 22:50:53 +0000242CodeGenInstruction::CodeGenInstruction(Record *R, const std::string &AsmStr)
243 : TheDef(R), AsmString(AsmStr) {
Chris Lattnerec352402004-08-01 05:04:00 +0000244 Name = R->getValueAsString("Name");
245 Namespace = R->getValueAsString("Namespace");
Chris Lattnerec352402004-08-01 05:04:00 +0000246
Chris Lattnerec352402004-08-01 05:04:00 +0000247 isReturn = R->getValueAsBit("isReturn");
248 isBranch = R->getValueAsBit("isBranch");
249 isBarrier = R->getValueAsBit("isBarrier");
250 isCall = R->getValueAsBit("isCall");
Nate Begemancdd66b52004-09-28 21:01:45 +0000251 isLoad = R->getValueAsBit("isLoad");
252 isStore = R->getValueAsBit("isStore");
Chris Lattnerec352402004-08-01 05:04:00 +0000253 isTwoAddress = R->getValueAsBit("isTwoAddress");
Chris Lattneraad75aa2005-01-02 02:29:04 +0000254 isConvertibleToThreeAddress = R->getValueAsBit("isConvertibleToThreeAddress");
255 isCommutable = R->getValueAsBit("isCommutable");
Chris Lattnerec352402004-08-01 05:04:00 +0000256 isTerminator = R->getValueAsBit("isTerminator");
Chris Lattner5b71d3a2004-09-28 18:38:01 +0000257 hasDelaySlot = R->getValueAsBit("hasDelaySlot");
Chris Lattnere3cbf822005-08-26 20:55:40 +0000258 usesCustomDAGSchedInserter = R->getValueAsBit("usesCustomDAGSchedInserter");
Chris Lattnercfbf96a2005-08-18 23:38:41 +0000259 hasVariableNumberOfOperands = false;
260
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000261 DagInit *DI;
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000262 try {
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000263 DI = R->getValueAsDag("OperandList");
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000264 } catch (...) {
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000265 // Error getting operand list, just ignore it (sparcv9).
Chris Lattner87c59052004-08-01 07:42:39 +0000266 AsmString.clear();
267 OperandList.clear();
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000268 return;
269 }
270
271 unsigned MIOperandNo = 0;
272 std::set<std::string> OperandNames;
273 for (unsigned i = 0, e = DI->getNumArgs(); i != e; ++i) {
274 DefInit *Arg = dynamic_cast<DefInit*>(DI->getArg(i));
275 if (!Arg)
276 throw "Illegal operand for the '" + R->getName() + "' instruction!";
277
278 Record *Rec = Arg->getDef();
279 MVT::ValueType Ty;
280 std::string PrintMethod = "printOperand";
281 unsigned NumOps = 1;
282 if (Rec->isSubClassOf("RegisterClass")) {
283 Ty = getValueType(Rec->getValueAsDef("RegType"));
284 } else if (Rec->isSubClassOf("Operand")) {
285 Ty = getValueType(Rec->getValueAsDef("Type"));
286 PrintMethod = Rec->getValueAsString("PrintMethod");
287 NumOps = Rec->getValueAsInt("NumMIOperands");
288 } else if (Rec->getName() == "variable_ops") {
289 hasVariableNumberOfOperands = true;
290 continue;
291 } else
292 throw "Unknown operand class '" + Rec->getName() +
293 "' in instruction '" + R->getName() + "' instruction!";
294
Chris Lattnerc4a8b732005-09-14 21:13:50 +0000295 // Check that the operand has a name and that it's unique.
296 if (DI->getArgName(i).empty())
297 throw "In instruction '" + R->getName() + "', operand #" + utostr(i) +
298 " has no name!";
299 if (!OperandNames.insert(DI->getArgName(i)).second)
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000300 throw "In instruction '" + R->getName() + "', operand #" + utostr(i) +
301 " has the same name as a previous operand!";
302
303 OperandList.push_back(OperandInfo(Rec, Ty, DI->getArgName(i),
304 PrintMethod, MIOperandNo, NumOps));
305 MIOperandNo += NumOps;
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000306 }
Chris Lattnerec352402004-08-01 05:04:00 +0000307}
308
309
Chris Lattner87c59052004-08-01 07:42:39 +0000310
311/// getOperandNamed - Return the index of the operand with the specified
312/// non-empty name. If the instruction does not have an operand with the
313/// specified name, throw an exception.
Misha Brukman35e83cc2004-10-14 05:50:43 +0000314///
Chris Lattner87c59052004-08-01 07:42:39 +0000315unsigned CodeGenInstruction::getOperandNamed(const std::string &Name) const {
316 assert(!Name.empty() && "Cannot search for operand with no name!");
317 for (unsigned i = 0, e = OperandList.size(); i != e; ++i)
318 if (OperandList[i].Name == Name) return i;
319 throw "Instruction '" + TheDef->getName() +
320 "' does not have an operand named '$" + Name + "'!";
321}