blob: b16bc8831d744d2fbd56b414c6085af866da47b1 [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";
Nate Begeman02fc8ff2005-11-29 06:19:38 +000049 case MVT::v16i8: return "v16i8";
50 case MVT::v8i16: return "v8i16";
51 case MVT::v4i32: return "v4i32";
52 case MVT::v2i64: return "v2i64";
53 case MVT::v4f32: return "v4f32";
54 case MVT::v2f64: return "v2f64";
Chris Lattnerd3464c12003-08-07 23:15:21 +000055 default: assert(0 && "ILLEGAL VALUE TYPE!"); return "";
Chris Lattner45872072003-08-07 05:38:11 +000056 }
Chris Lattnerd3464c12003-08-07 23:15:21 +000057}
58
Chris Lattner2082ebe2004-08-01 03:55:39 +000059std::string llvm::getEnumName(MVT::ValueType T) {
Chris Lattnerb72fb7e2003-08-10 19:50:32 +000060 switch (T) {
61 case MVT::Other: return "Other";
62 case MVT::i1: return "i1";
63 case MVT::i8: return "i8";
64 case MVT::i16: return "i16";
65 case MVT::i32: return "i32";
66 case MVT::i64: return "i64";
67 case MVT::i128: return "i128";
68 case MVT::f32: return "f32";
69 case MVT::f64: return "f64";
70 case MVT::f80: return "f80";
71 case MVT::f128: return "f128";
72 case MVT::isVoid:return "isVoid";
Nate Begeman02fc8ff2005-11-29 06:19:38 +000073 case MVT::v16i8: return "v16i8";
74 case MVT::v8i16: return "v8i16";
75 case MVT::v4i32: return "v4i32";
76 case MVT::v2i64: return "v2i64";
77 case MVT::v4f32: return "v4f32";
78 case MVT::v2f64: return "v2f64";
Chris Lattnerb72fb7e2003-08-10 19:50:32 +000079 default: assert(0 && "ILLEGAL VALUE TYPE!"); return "";
80 }
81}
82
Chris Lattnerd3464c12003-08-07 23:15:21 +000083
Chris Lattner2082ebe2004-08-01 03:55:39 +000084std::ostream &llvm::operator<<(std::ostream &OS, MVT::ValueType T) {
Chris Lattnerd3464c12003-08-07 23:15:21 +000085 return OS << getName(T);
Chris Lattner45872072003-08-07 05:38:11 +000086}
87
88
Chris Lattner45872072003-08-07 05:38:11 +000089/// getTarget - Return the current instance of the Target class.
90///
Brian Gaekebe7f4af2003-10-10 21:55:29 +000091CodeGenTarget::CodeGenTarget() : PointerType(MVT::Other) {
Chris Lattner45872072003-08-07 05:38:11 +000092 std::vector<Record*> Targets = Records.getAllDerivedDefinitions("Target");
Misha Brukmanbebdb202004-06-04 14:59:42 +000093 if (Targets.size() == 0)
Misha Brukman3da94ae2005-04-22 00:00:37 +000094 throw std::string("ERROR: No 'Target' subclasses defined!");
Chris Lattner45872072003-08-07 05:38:11 +000095 if (Targets.size() != 1)
96 throw std::string("ERROR: Multiple subclasses of Target defined!");
97 TargetRec = Targets[0];
98
Chris Lattnerb0e103d2005-10-28 22:49:02 +000099 // Read in all of the CalleeSavedRegisters.
100 CalleeSavedRegisters =TargetRec->getValueAsListOfDefs("CalleeSavedRegisters");
Chris Lattner45872072003-08-07 05:38:11 +0000101 PointerType = getValueType(TargetRec->getValueAsDef("PointerType"));
102}
103
104
105const std::string &CodeGenTarget::getName() const {
106 return TargetRec->getName();
107}
108
109Record *CodeGenTarget::getInstructionSet() const {
110 return TargetRec->getValueAsDef("InstructionSet");
111}
Brian Gaeked0fde302003-11-11 22:41:34 +0000112
Chris Lattner175580c2004-08-14 22:50:53 +0000113/// getAsmWriter - Return the AssemblyWriter definition for this target.
114///
115Record *CodeGenTarget::getAsmWriter() const {
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000116 std::vector<Record*> LI = TargetRec->getValueAsListOfDefs("AssemblyWriters");
117 if (AsmWriterNum >= LI.size())
Chris Lattner560a79f2004-10-03 19:34:31 +0000118 throw "Target does not have an AsmWriter #" + utostr(AsmWriterNum) + "!";
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000119 return LI[AsmWriterNum];
Chris Lattner175580c2004-08-14 22:50:53 +0000120}
121
Chris Lattner26693112004-08-16 01:10:21 +0000122void CodeGenTarget::ReadRegisters() const {
123 std::vector<Record*> Regs = Records.getAllDerivedDefinitions("Register");
124 if (Regs.empty())
125 throw std::string("No 'Register' subclasses defined!");
126
127 Registers.reserve(Regs.size());
128 Registers.assign(Regs.begin(), Regs.end());
129}
130
Chris Lattner7a680c62004-08-21 02:24:57 +0000131CodeGenRegister::CodeGenRegister(Record *R) : TheDef(R) {
132 DeclaredSpillSize = R->getValueAsInt("SpillSize");
133 DeclaredSpillAlignment = R->getValueAsInt("SpillAlignment");
134}
135
Chris Lattner26693112004-08-16 01:10:21 +0000136const std::string &CodeGenRegister::getName() const {
137 return TheDef->getName();
138}
139
Chris Lattner056afef2004-08-21 04:05:00 +0000140void CodeGenTarget::ReadRegisterClasses() const {
141 std::vector<Record*> RegClasses =
142 Records.getAllDerivedDefinitions("RegisterClass");
143 if (RegClasses.empty())
144 throw std::string("No 'RegisterClass' subclasses defined!");
145
146 RegisterClasses.reserve(RegClasses.size());
147 RegisterClasses.assign(RegClasses.begin(), RegClasses.end());
148}
149
150CodeGenRegisterClass::CodeGenRegisterClass(Record *R) : TheDef(R) {
Chris Lattnerc67c18f2005-08-19 18:45:20 +0000151 // Rename anonymous register classes.
152 if (R->getName().size() > 9 && R->getName()[9] == '.') {
153 static unsigned AnonCounter = 0;
154 R->setName("AnonRegClass_"+utostr(AnonCounter++));
155 }
156
Nate Begeman6510b222005-12-01 04:51:06 +0000157 std::vector<Record*> TypeList = R->getValueAsListOfDefs("RegTypes");
158 for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
159 Record *Type = TypeList[i];
160 if (!Type->isSubClassOf("ValueType"))
161 throw "RegTypes list member '" + Type->getName() +
162 "' does not derive from the ValueType class!";
163 VTs.push_back(getValueType(Type));
164 }
165 assert(!VTs.empty() && "RegisterClass must contain at least one ValueType!");
Chris Lattnerac468932005-08-19 19:12:51 +0000166
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000167 std::vector<Record*> RegList = R->getValueAsListOfDefs("MemberList");
168 for (unsigned i = 0, e = RegList.size(); i != e; ++i) {
169 Record *Reg = RegList[i];
Chris Lattner056afef2004-08-21 04:05:00 +0000170 if (!Reg->isSubClassOf("Register"))
171 throw "Register Class member '" + Reg->getName() +
172 "' does not derive from the Register class!";
173 Elements.push_back(Reg);
174 }
Nate Begeman6510b222005-12-01 04:51:06 +0000175
176 // Allow targets to override the size in bits of the RegisterClass.
177 unsigned Size = R->getValueAsInt("Size");
178
179 Namespace = R->getValueAsString("Namespace");
180 SpillSize = Size ? Size : MVT::getSizeInBits(VTs[0]);
181 SpillAlignment = R->getValueAsInt("Alignment");
182 MethodBodies = R->getValueAsCode("MethodBodies");
183 MethodProtos = R->getValueAsCode("MethodProtos");
Chris Lattner056afef2004-08-21 04:05:00 +0000184}
185
186const std::string &CodeGenRegisterClass::getName() const {
187 return TheDef->getName();
188}
189
Chris Lattnere9f4ba82005-09-08 21:43:21 +0000190void CodeGenTarget::ReadLegalValueTypes() const {
191 const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses();
192 for (unsigned i = 0, e = RCs.size(); i != e; ++i)
Nate Begeman6510b222005-12-01 04:51:06 +0000193 for (unsigned ri = 0, re = RCs[i].VTs.size(); ri != re; ++ri)
194 LegalValueTypes.push_back(RCs[i].VTs[ri]);
Chris Lattner75ee2eb2005-10-14 03:54:49 +0000195
196 // Remove duplicates.
197 std::sort(LegalValueTypes.begin(), LegalValueTypes.end());
198 LegalValueTypes.erase(std::unique(LegalValueTypes.begin(),
199 LegalValueTypes.end()),
200 LegalValueTypes.end());
Chris Lattnere9f4ba82005-09-08 21:43:21 +0000201}
Chris Lattner056afef2004-08-21 04:05:00 +0000202
Chris Lattner175580c2004-08-14 22:50:53 +0000203
Chris Lattnerec352402004-08-01 05:04:00 +0000204void CodeGenTarget::ReadInstructions() const {
205 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
206
Chris Lattner26693112004-08-16 01:10:21 +0000207 if (Insts.empty())
Chris Lattnerec352402004-08-01 05:04:00 +0000208 throw std::string("No 'Instruction' subclasses defined!");
209
Chris Lattner175580c2004-08-14 22:50:53 +0000210 std::string InstFormatName =
211 getAsmWriter()->getValueAsString("InstFormatName");
212
213 for (unsigned i = 0, e = Insts.size(); i != e; ++i) {
214 std::string AsmStr = Insts[i]->getValueAsString(InstFormatName);
215 Instructions.insert(std::make_pair(Insts[i]->getName(),
216 CodeGenInstruction(Insts[i], AsmStr)));
217 }
Chris Lattnerec352402004-08-01 05:04:00 +0000218}
219
220/// getPHIInstruction - Return the designated PHI instruction.
Misha Brukman35e83cc2004-10-14 05:50:43 +0000221///
Chris Lattnerec352402004-08-01 05:04:00 +0000222const CodeGenInstruction &CodeGenTarget::getPHIInstruction() const {
223 Record *PHI = getInstructionSet()->getValueAsDef("PHIInst");
224 std::map<std::string, CodeGenInstruction>::const_iterator I =
225 getInstructions().find(PHI->getName());
226 if (I == Instructions.end())
227 throw "Could not find PHI instruction named '" + PHI->getName() + "'!";
228 return I->second;
229}
230
Chris Lattnerd6488672005-01-22 18:58:51 +0000231/// getInstructionsByEnumValue - Return all of the instructions defined by the
232/// target, ordered by their enum value.
233void CodeGenTarget::
234getInstructionsByEnumValue(std::vector<const CodeGenInstruction*>
235 &NumberedInstructions) {
236
237 // Print out the rest of the instructions now.
238 unsigned i = 0;
239 const CodeGenInstruction *PHI = &getPHIInstruction();
240 NumberedInstructions.push_back(PHI);
241 for (inst_iterator II = inst_begin(), E = inst_end(); II != E; ++II)
242 if (&II->second != PHI)
243 NumberedInstructions.push_back(&II->second);
244}
245
246
Misha Brukman35e83cc2004-10-14 05:50:43 +0000247/// isLittleEndianEncoding - Return whether this target encodes its instruction
248/// in little-endian format, i.e. bits laid out in the order [0..n]
249///
250bool CodeGenTarget::isLittleEndianEncoding() const {
251 return getInstructionSet()->getValueAsBit("isLittleEndianEncoding");
252}
253
Chris Lattner175580c2004-08-14 22:50:53 +0000254CodeGenInstruction::CodeGenInstruction(Record *R, const std::string &AsmStr)
255 : TheDef(R), AsmString(AsmStr) {
Chris Lattnerec352402004-08-01 05:04:00 +0000256 Name = R->getValueAsString("Name");
257 Namespace = R->getValueAsString("Namespace");
Chris Lattnerec352402004-08-01 05:04:00 +0000258
Chris Lattnerec352402004-08-01 05:04:00 +0000259 isReturn = R->getValueAsBit("isReturn");
260 isBranch = R->getValueAsBit("isBranch");
261 isBarrier = R->getValueAsBit("isBarrier");
262 isCall = R->getValueAsBit("isCall");
Nate Begemancdd66b52004-09-28 21:01:45 +0000263 isLoad = R->getValueAsBit("isLoad");
264 isStore = R->getValueAsBit("isStore");
Chris Lattnerec352402004-08-01 05:04:00 +0000265 isTwoAddress = R->getValueAsBit("isTwoAddress");
Chris Lattneraad75aa2005-01-02 02:29:04 +0000266 isConvertibleToThreeAddress = R->getValueAsBit("isConvertibleToThreeAddress");
267 isCommutable = R->getValueAsBit("isCommutable");
Chris Lattnerec352402004-08-01 05:04:00 +0000268 isTerminator = R->getValueAsBit("isTerminator");
Chris Lattner5b71d3a2004-09-28 18:38:01 +0000269 hasDelaySlot = R->getValueAsBit("hasDelaySlot");
Chris Lattnere3cbf822005-08-26 20:55:40 +0000270 usesCustomDAGSchedInserter = R->getValueAsBit("usesCustomDAGSchedInserter");
Evan Cheng1c3d19e2005-12-04 08:18:16 +0000271 hasCtrlDep = R->getValueAsBit("hasCtrlDep");
Chris Lattnercfbf96a2005-08-18 23:38:41 +0000272 hasVariableNumberOfOperands = false;
273
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000274 DagInit *DI;
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000275 try {
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000276 DI = R->getValueAsDag("OperandList");
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000277 } catch (...) {
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000278 // Error getting operand list, just ignore it (sparcv9).
Chris Lattner87c59052004-08-01 07:42:39 +0000279 AsmString.clear();
280 OperandList.clear();
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000281 return;
282 }
283
284 unsigned MIOperandNo = 0;
285 std::set<std::string> OperandNames;
286 for (unsigned i = 0, e = DI->getNumArgs(); i != e; ++i) {
287 DefInit *Arg = dynamic_cast<DefInit*>(DI->getArg(i));
288 if (!Arg)
289 throw "Illegal operand for the '" + R->getName() + "' instruction!";
290
291 Record *Rec = Arg->getDef();
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000292 std::string PrintMethod = "printOperand";
293 unsigned NumOps = 1;
Chris Lattner33670792005-11-19 07:48:33 +0000294 DagInit *MIOpInfo = 0;
Nate Begeman86193d12005-12-01 00:12:04 +0000295 if (Rec->isSubClassOf("Operand")) {
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000296 PrintMethod = Rec->getValueAsString("PrintMethod");
297 NumOps = Rec->getValueAsInt("NumMIOperands");
Chris Lattner65303d62005-11-19 07:05:57 +0000298 MIOpInfo = Rec->getValueAsDag("MIOperandInfo");
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000299 } else if (Rec->getName() == "variable_ops") {
300 hasVariableNumberOfOperands = true;
301 continue;
Nate Begeman86193d12005-12-01 00:12:04 +0000302 } else if (!Rec->isSubClassOf("RegisterClass"))
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000303 throw "Unknown operand class '" + Rec->getName() +
304 "' in instruction '" + R->getName() + "' instruction!";
305
Chris Lattnerc4a8b732005-09-14 21:13:50 +0000306 // Check that the operand has a name and that it's unique.
307 if (DI->getArgName(i).empty())
308 throw "In instruction '" + R->getName() + "', operand #" + utostr(i) +
309 " has no name!";
310 if (!OperandNames.insert(DI->getArgName(i)).second)
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000311 throw "In instruction '" + R->getName() + "', operand #" + utostr(i) +
312 " has the same name as a previous operand!";
313
Nate Begeman86193d12005-12-01 00:12:04 +0000314 OperandList.push_back(OperandInfo(Rec, DI->getArgName(i), PrintMethod,
315 MIOperandNo, NumOps, MIOpInfo));
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000316 MIOperandNo += NumOps;
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000317 }
Chris Lattnerec352402004-08-01 05:04:00 +0000318}
319
320
Chris Lattner87c59052004-08-01 07:42:39 +0000321
322/// getOperandNamed - Return the index of the operand with the specified
323/// non-empty name. If the instruction does not have an operand with the
324/// specified name, throw an exception.
Misha Brukman35e83cc2004-10-14 05:50:43 +0000325///
Chris Lattner87c59052004-08-01 07:42:39 +0000326unsigned CodeGenInstruction::getOperandNamed(const std::string &Name) const {
327 assert(!Name.empty() && "Cannot search for operand with no name!");
328 for (unsigned i = 0, e = OperandList.size(); i != e; ++i)
329 if (OperandList[i].Name == Name) return i;
330 throw "Instruction '" + TheDef->getName() +
331 "' does not have an operand named '$" + Name + "'!";
332}
Evan Cheng0fc71982005-12-08 02:00:36 +0000333
334//===----------------------------------------------------------------------===//
335// ComplexPattern implementation
336//
337ComplexPattern::ComplexPattern(Record *R) {
Evan Cheng3aa39f42005-12-08 02:14:08 +0000338 Ty = ::getValueType(R->getValueAsDef("Ty"));
339 NumOperands = R->getValueAsInt("NumOperands");
340 SelectFunc = R->getValueAsString("SelectFunc");
341 RootNodes = R->getValueAsListOfDefs("RootNodes");
Evan Cheng0fc71982005-12-08 02:00:36 +0000342}
Evan Cheng3aa39f42005-12-08 02:14:08 +0000343