blob: c18dff351f1082bee4b925fbfbe247e0536226f0 [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 Lattner2082ebe2004-08-01 03:55:39 +000022using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000023
Chris Lattner560a79f2004-10-03 19:34:31 +000024static cl::opt<unsigned>
25AsmWriterNum("asmwriternum", cl::init(0),
26 cl::desc("Make -gen-asm-writer emit assembly writer #N"));
27
Chris Lattner45872072003-08-07 05:38:11 +000028/// getValueType - Return the MCV::ValueType that the specified TableGen record
29/// corresponds to.
Chris Lattner2082ebe2004-08-01 03:55:39 +000030MVT::ValueType llvm::getValueType(Record *Rec) {
Chris Lattner45872072003-08-07 05:38:11 +000031 return (MVT::ValueType)Rec->getValueAsInt("Value");
32}
33
Chris Lattner2082ebe2004-08-01 03:55:39 +000034std::string llvm::getName(MVT::ValueType T) {
Chris Lattner45872072003-08-07 05:38:11 +000035 switch (T) {
Chris Lattnerd3464c12003-08-07 23:15:21 +000036 case MVT::Other: return "UNKNOWN";
37 case MVT::i1: return "i1";
38 case MVT::i8: return "i8";
39 case MVT::i16: return "i16";
40 case MVT::i32: return "i32";
41 case MVT::i64: return "i64";
42 case MVT::i128: return "i128";
43 case MVT::f32: return "f32";
44 case MVT::f64: return "f64";
45 case MVT::f80: return "f80";
46 case MVT::f128: return "f128";
47 case MVT::isVoid:return "void";
48 default: assert(0 && "ILLEGAL VALUE TYPE!"); return "";
Chris Lattner45872072003-08-07 05:38:11 +000049 }
Chris Lattnerd3464c12003-08-07 23:15:21 +000050}
51
Chris Lattner2082ebe2004-08-01 03:55:39 +000052std::string llvm::getEnumName(MVT::ValueType T) {
Chris Lattnerb72fb7e2003-08-10 19:50:32 +000053 switch (T) {
54 case MVT::Other: return "Other";
55 case MVT::i1: return "i1";
56 case MVT::i8: return "i8";
57 case MVT::i16: return "i16";
58 case MVT::i32: return "i32";
59 case MVT::i64: return "i64";
60 case MVT::i128: return "i128";
61 case MVT::f32: return "f32";
62 case MVT::f64: return "f64";
63 case MVT::f80: return "f80";
64 case MVT::f128: return "f128";
65 case MVT::isVoid:return "isVoid";
66 default: assert(0 && "ILLEGAL VALUE TYPE!"); return "";
67 }
68}
69
Chris Lattnerd3464c12003-08-07 23:15:21 +000070
Chris Lattner2082ebe2004-08-01 03:55:39 +000071std::ostream &llvm::operator<<(std::ostream &OS, MVT::ValueType T) {
Chris Lattnerd3464c12003-08-07 23:15:21 +000072 return OS << getName(T);
Chris Lattner45872072003-08-07 05:38:11 +000073}
74
75
Chris Lattner45872072003-08-07 05:38:11 +000076/// getTarget - Return the current instance of the Target class.
77///
Brian Gaekebe7f4af2003-10-10 21:55:29 +000078CodeGenTarget::CodeGenTarget() : PointerType(MVT::Other) {
Chris Lattner45872072003-08-07 05:38:11 +000079 std::vector<Record*> Targets = Records.getAllDerivedDefinitions("Target");
Misha Brukmanbebdb202004-06-04 14:59:42 +000080 if (Targets.size() == 0)
Misha Brukman3da94ae2005-04-22 00:00:37 +000081 throw std::string("ERROR: No 'Target' subclasses defined!");
Chris Lattner45872072003-08-07 05:38:11 +000082 if (Targets.size() != 1)
83 throw std::string("ERROR: Multiple subclasses of Target defined!");
84 TargetRec = Targets[0];
85
86 // Read in all of the CalleeSavedRegisters...
87 ListInit *LI = TargetRec->getValueAsListInit("CalleeSavedRegisters");
88 for (unsigned i = 0, e = LI->getSize(); i != e; ++i)
89 if (DefInit *DI = dynamic_cast<DefInit*>(LI->getElement(i)))
90 CalleeSavedRegisters.push_back(DI->getDef());
91 else
92 throw "Target: " + TargetRec->getName() +
93 " expected register definition in CalleeSavedRegisters list!";
94
95 PointerType = getValueType(TargetRec->getValueAsDef("PointerType"));
96}
97
98
99const std::string &CodeGenTarget::getName() const {
100 return TargetRec->getName();
101}
102
103Record *CodeGenTarget::getInstructionSet() const {
104 return TargetRec->getValueAsDef("InstructionSet");
105}
Brian Gaeked0fde302003-11-11 22:41:34 +0000106
Chris Lattner175580c2004-08-14 22:50:53 +0000107/// getAsmWriter - Return the AssemblyWriter definition for this target.
108///
109Record *CodeGenTarget::getAsmWriter() const {
Chris Lattner560a79f2004-10-03 19:34:31 +0000110 ListInit *LI = TargetRec->getValueAsListInit("AssemblyWriters");
111 if (AsmWriterNum >= LI->getSize())
112 throw "Target does not have an AsmWriter #" + utostr(AsmWriterNum) + "!";
113 DefInit *DI = dynamic_cast<DefInit*>(LI->getElement(AsmWriterNum));
114 if (!DI) throw std::string("AssemblyWriter list should be a list of defs!");
115 return DI->getDef();
Chris Lattner175580c2004-08-14 22:50:53 +0000116}
117
Chris Lattner26693112004-08-16 01:10:21 +0000118void CodeGenTarget::ReadRegisters() const {
119 std::vector<Record*> Regs = Records.getAllDerivedDefinitions("Register");
120 if (Regs.empty())
121 throw std::string("No 'Register' subclasses defined!");
122
123 Registers.reserve(Regs.size());
124 Registers.assign(Regs.begin(), Regs.end());
125}
126
Chris Lattner7a680c62004-08-21 02:24:57 +0000127CodeGenRegister::CodeGenRegister(Record *R) : TheDef(R) {
128 DeclaredSpillSize = R->getValueAsInt("SpillSize");
129 DeclaredSpillAlignment = R->getValueAsInt("SpillAlignment");
130}
131
Chris Lattner26693112004-08-16 01:10:21 +0000132const std::string &CodeGenRegister::getName() const {
133 return TheDef->getName();
134}
135
Chris Lattner056afef2004-08-21 04:05:00 +0000136void CodeGenTarget::ReadRegisterClasses() const {
137 std::vector<Record*> RegClasses =
138 Records.getAllDerivedDefinitions("RegisterClass");
139 if (RegClasses.empty())
140 throw std::string("No 'RegisterClass' subclasses defined!");
141
142 RegisterClasses.reserve(RegClasses.size());
143 RegisterClasses.assign(RegClasses.begin(), RegClasses.end());
144}
145
146CodeGenRegisterClass::CodeGenRegisterClass(Record *R) : TheDef(R) {
Chris Lattnerc67c18f2005-08-19 18:45:20 +0000147 // Rename anonymous register classes.
148 if (R->getName().size() > 9 && R->getName()[9] == '.') {
149 static unsigned AnonCounter = 0;
150 R->setName("AnonRegClass_"+utostr(AnonCounter++));
151 }
152
153 Namespace = R->getValueAsString("Namespace");
Chris Lattner056afef2004-08-21 04:05:00 +0000154 SpillSize = R->getValueAsInt("Size");
Chris Lattner037d7322004-08-21 20:15:25 +0000155 SpillAlignment = R->getValueAsInt("Alignment");
Chris Lattnere9f4ba82005-09-08 21:43:21 +0000156 VT = getValueType(R->getValueAsDef("RegType"));
Chris Lattner056afef2004-08-21 04:05:00 +0000157
Chris Lattner8f493132005-09-13 21:44:28 +0000158 MethodBodies = R->getValueAsCode("MethodBodies");
159 MethodProtos = R->getValueAsCode("MethodProtos");
Chris Lattnerac468932005-08-19 19:12:51 +0000160
Chris Lattner056afef2004-08-21 04:05:00 +0000161 ListInit *RegList = R->getValueAsListInit("MemberList");
162 for (unsigned i = 0, e = RegList->getSize(); i != e; ++i) {
163 DefInit *RegDef = dynamic_cast<DefInit*>(RegList->getElement(i));
Misha Brukman3da94ae2005-04-22 00:00:37 +0000164 if (!RegDef) throw "Register class member is not a record!";
Chris Lattner056afef2004-08-21 04:05:00 +0000165 Record *Reg = RegDef->getDef();
166
167 if (!Reg->isSubClassOf("Register"))
168 throw "Register Class member '" + Reg->getName() +
169 "' does not derive from the Register class!";
170 Elements.push_back(Reg);
171 }
172}
173
174const std::string &CodeGenRegisterClass::getName() const {
175 return TheDef->getName();
176}
177
Chris Lattnere9f4ba82005-09-08 21:43:21 +0000178void CodeGenTarget::ReadLegalValueTypes() const {
179 const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses();
180 for (unsigned i = 0, e = RCs.size(); i != e; ++i)
181 LegalValueTypes.push_back(RCs[i].VT);
182}
Chris Lattner056afef2004-08-21 04:05:00 +0000183
Chris Lattner175580c2004-08-14 22:50:53 +0000184
Chris Lattnerec352402004-08-01 05:04:00 +0000185void CodeGenTarget::ReadInstructions() const {
186 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
187
Chris Lattner26693112004-08-16 01:10:21 +0000188 if (Insts.empty())
Chris Lattnerec352402004-08-01 05:04:00 +0000189 throw std::string("No 'Instruction' subclasses defined!");
190
Chris Lattner175580c2004-08-14 22:50:53 +0000191 std::string InstFormatName =
192 getAsmWriter()->getValueAsString("InstFormatName");
193
194 for (unsigned i = 0, e = Insts.size(); i != e; ++i) {
195 std::string AsmStr = Insts[i]->getValueAsString(InstFormatName);
196 Instructions.insert(std::make_pair(Insts[i]->getName(),
197 CodeGenInstruction(Insts[i], AsmStr)));
198 }
Chris Lattnerec352402004-08-01 05:04:00 +0000199}
200
201/// getPHIInstruction - Return the designated PHI instruction.
Misha Brukman35e83cc2004-10-14 05:50:43 +0000202///
Chris Lattnerec352402004-08-01 05:04:00 +0000203const CodeGenInstruction &CodeGenTarget::getPHIInstruction() const {
204 Record *PHI = getInstructionSet()->getValueAsDef("PHIInst");
205 std::map<std::string, CodeGenInstruction>::const_iterator I =
206 getInstructions().find(PHI->getName());
207 if (I == Instructions.end())
208 throw "Could not find PHI instruction named '" + PHI->getName() + "'!";
209 return I->second;
210}
211
Chris Lattnerd6488672005-01-22 18:58:51 +0000212/// getInstructionsByEnumValue - Return all of the instructions defined by the
213/// target, ordered by their enum value.
214void CodeGenTarget::
215getInstructionsByEnumValue(std::vector<const CodeGenInstruction*>
216 &NumberedInstructions) {
217
218 // Print out the rest of the instructions now.
219 unsigned i = 0;
220 const CodeGenInstruction *PHI = &getPHIInstruction();
221 NumberedInstructions.push_back(PHI);
222 for (inst_iterator II = inst_begin(), E = inst_end(); II != E; ++II)
223 if (&II->second != PHI)
224 NumberedInstructions.push_back(&II->second);
225}
226
227
Misha Brukman35e83cc2004-10-14 05:50:43 +0000228/// isLittleEndianEncoding - Return whether this target encodes its instruction
229/// in little-endian format, i.e. bits laid out in the order [0..n]
230///
231bool CodeGenTarget::isLittleEndianEncoding() const {
232 return getInstructionSet()->getValueAsBit("isLittleEndianEncoding");
233}
234
Chris Lattner175580c2004-08-14 22:50:53 +0000235CodeGenInstruction::CodeGenInstruction(Record *R, const std::string &AsmStr)
236 : TheDef(R), AsmString(AsmStr) {
Chris Lattnerec352402004-08-01 05:04:00 +0000237 Name = R->getValueAsString("Name");
238 Namespace = R->getValueAsString("Namespace");
Chris Lattnerec352402004-08-01 05:04:00 +0000239
Chris Lattnerec352402004-08-01 05:04:00 +0000240 isReturn = R->getValueAsBit("isReturn");
241 isBranch = R->getValueAsBit("isBranch");
242 isBarrier = R->getValueAsBit("isBarrier");
243 isCall = R->getValueAsBit("isCall");
Nate Begemancdd66b52004-09-28 21:01:45 +0000244 isLoad = R->getValueAsBit("isLoad");
245 isStore = R->getValueAsBit("isStore");
Chris Lattnerec352402004-08-01 05:04:00 +0000246 isTwoAddress = R->getValueAsBit("isTwoAddress");
Chris Lattneraad75aa2005-01-02 02:29:04 +0000247 isConvertibleToThreeAddress = R->getValueAsBit("isConvertibleToThreeAddress");
248 isCommutable = R->getValueAsBit("isCommutable");
Chris Lattnerec352402004-08-01 05:04:00 +0000249 isTerminator = R->getValueAsBit("isTerminator");
Chris Lattner5b71d3a2004-09-28 18:38:01 +0000250 hasDelaySlot = R->getValueAsBit("hasDelaySlot");
Chris Lattnere3cbf822005-08-26 20:55:40 +0000251 usesCustomDAGSchedInserter = R->getValueAsBit("usesCustomDAGSchedInserter");
Chris Lattnercfbf96a2005-08-18 23:38:41 +0000252 hasVariableNumberOfOperands = false;
253
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000254 DagInit *DI;
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000255 try {
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000256 DI = R->getValueAsDag("OperandList");
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000257 } catch (...) {
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000258 // Error getting operand list, just ignore it (sparcv9).
Chris Lattner87c59052004-08-01 07:42:39 +0000259 AsmString.clear();
260 OperandList.clear();
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000261 return;
262 }
263
264 unsigned MIOperandNo = 0;
265 std::set<std::string> OperandNames;
266 for (unsigned i = 0, e = DI->getNumArgs(); i != e; ++i) {
267 DefInit *Arg = dynamic_cast<DefInit*>(DI->getArg(i));
268 if (!Arg)
269 throw "Illegal operand for the '" + R->getName() + "' instruction!";
270
271 Record *Rec = Arg->getDef();
272 MVT::ValueType Ty;
273 std::string PrintMethod = "printOperand";
274 unsigned NumOps = 1;
275 if (Rec->isSubClassOf("RegisterClass")) {
276 Ty = getValueType(Rec->getValueAsDef("RegType"));
277 } else if (Rec->isSubClassOf("Operand")) {
278 Ty = getValueType(Rec->getValueAsDef("Type"));
279 PrintMethod = Rec->getValueAsString("PrintMethod");
280 NumOps = Rec->getValueAsInt("NumMIOperands");
281 } else if (Rec->getName() == "variable_ops") {
282 hasVariableNumberOfOperands = true;
283 continue;
284 } else
285 throw "Unknown operand class '" + Rec->getName() +
286 "' in instruction '" + R->getName() + "' instruction!";
287
288 if (!DI->getArgName(i).empty() &&
289 !OperandNames.insert(DI->getArgName(i)).second)
290 throw "In instruction '" + R->getName() + "', operand #" + utostr(i) +
291 " has the same name as a previous operand!";
292
293 OperandList.push_back(OperandInfo(Rec, Ty, DI->getArgName(i),
294 PrintMethod, MIOperandNo, NumOps));
295 MIOperandNo += NumOps;
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000296 }
Chris Lattnerec352402004-08-01 05:04:00 +0000297}
298
299
Chris Lattner87c59052004-08-01 07:42:39 +0000300
301/// getOperandNamed - Return the index of the operand with the specified
302/// non-empty name. If the instruction does not have an operand with the
303/// specified name, throw an exception.
Misha Brukman35e83cc2004-10-14 05:50:43 +0000304///
Chris Lattner87c59052004-08-01 07:42:39 +0000305unsigned CodeGenInstruction::getOperandNamed(const std::string &Name) const {
306 assert(!Name.empty() && "Cannot search for operand with no name!");
307 for (unsigned i = 0, e = OperandList.size(); i != e; ++i)
308 if (OperandList[i].Name == Name) return i;
309 throw "Instruction '" + TheDef->getName() +
310 "' does not have an operand named '$" + Name + "'!";
311}