blob: 8458d23aeaea568996370c590194c286c29c2a9c [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";
Evan Chengbcecf332005-12-17 01:19:28 +000048 case MVT::Flag: return "Flag";
Chris Lattnerd3464c12003-08-07 23:15:21 +000049 case MVT::isVoid:return "void";
Evan Chengaea20f52006-02-20 22:34:53 +000050 case MVT::v8i8: return "v8i8";
51 case MVT::v4i16: return "v4i16";
52 case MVT::v2i32: return "v2i32";
Nate Begeman02fc8ff2005-11-29 06:19:38 +000053 case MVT::v16i8: return "v16i8";
54 case MVT::v8i16: return "v8i16";
55 case MVT::v4i32: return "v4i32";
56 case MVT::v2i64: return "v2i64";
57 case MVT::v4f32: return "v4f32";
58 case MVT::v2f64: return "v2f64";
Chris Lattnerd3464c12003-08-07 23:15:21 +000059 default: assert(0 && "ILLEGAL VALUE TYPE!"); return "";
Chris Lattner45872072003-08-07 05:38:11 +000060 }
Chris Lattnerd3464c12003-08-07 23:15:21 +000061}
62
Chris Lattner2082ebe2004-08-01 03:55:39 +000063std::string llvm::getEnumName(MVT::ValueType T) {
Chris Lattnerb72fb7e2003-08-10 19:50:32 +000064 switch (T) {
65 case MVT::Other: return "Other";
66 case MVT::i1: return "i1";
67 case MVT::i8: return "i8";
68 case MVT::i16: return "i16";
69 case MVT::i32: return "i32";
70 case MVT::i64: return "i64";
71 case MVT::i128: return "i128";
72 case MVT::f32: return "f32";
73 case MVT::f64: return "f64";
74 case MVT::f80: return "f80";
75 case MVT::f128: return "f128";
Evan Chengbcecf332005-12-17 01:19:28 +000076 case MVT::Flag: return "Flag";
Chris Lattnerb72fb7e2003-08-10 19:50:32 +000077 case MVT::isVoid:return "isVoid";
Nate Begeman02fc8ff2005-11-29 06:19:38 +000078 case MVT::v16i8: return "v16i8";
79 case MVT::v8i16: return "v8i16";
80 case MVT::v4i32: return "v4i32";
81 case MVT::v2i64: return "v2i64";
82 case MVT::v4f32: return "v4f32";
83 case MVT::v2f64: return "v2f64";
Chris Lattnerb72fb7e2003-08-10 19:50:32 +000084 default: assert(0 && "ILLEGAL VALUE TYPE!"); return "";
85 }
86}
87
Chris Lattnerd3464c12003-08-07 23:15:21 +000088
Chris Lattner2082ebe2004-08-01 03:55:39 +000089std::ostream &llvm::operator<<(std::ostream &OS, MVT::ValueType T) {
Chris Lattnerd3464c12003-08-07 23:15:21 +000090 return OS << getName(T);
Chris Lattner45872072003-08-07 05:38:11 +000091}
92
93
Chris Lattner45872072003-08-07 05:38:11 +000094/// getTarget - Return the current instance of the Target class.
95///
Brian Gaekebe7f4af2003-10-10 21:55:29 +000096CodeGenTarget::CodeGenTarget() : PointerType(MVT::Other) {
Chris Lattner45872072003-08-07 05:38:11 +000097 std::vector<Record*> Targets = Records.getAllDerivedDefinitions("Target");
Misha Brukmanbebdb202004-06-04 14:59:42 +000098 if (Targets.size() == 0)
Misha Brukman3da94ae2005-04-22 00:00:37 +000099 throw std::string("ERROR: No 'Target' subclasses defined!");
Chris Lattner45872072003-08-07 05:38:11 +0000100 if (Targets.size() != 1)
101 throw std::string("ERROR: Multiple subclasses of Target defined!");
102 TargetRec = Targets[0];
103
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000104 // Read in all of the CalleeSavedRegisters.
105 CalleeSavedRegisters =TargetRec->getValueAsListOfDefs("CalleeSavedRegisters");
Chris Lattner45872072003-08-07 05:38:11 +0000106 PointerType = getValueType(TargetRec->getValueAsDef("PointerType"));
107}
108
109
110const std::string &CodeGenTarget::getName() const {
111 return TargetRec->getName();
112}
113
114Record *CodeGenTarget::getInstructionSet() const {
115 return TargetRec->getValueAsDef("InstructionSet");
116}
Brian Gaeked0fde302003-11-11 22:41:34 +0000117
Chris Lattner175580c2004-08-14 22:50:53 +0000118/// getAsmWriter - Return the AssemblyWriter definition for this target.
119///
120Record *CodeGenTarget::getAsmWriter() const {
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000121 std::vector<Record*> LI = TargetRec->getValueAsListOfDefs("AssemblyWriters");
122 if (AsmWriterNum >= LI.size())
Chris Lattner560a79f2004-10-03 19:34:31 +0000123 throw "Target does not have an AsmWriter #" + utostr(AsmWriterNum) + "!";
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000124 return LI[AsmWriterNum];
Chris Lattner175580c2004-08-14 22:50:53 +0000125}
126
Chris Lattner26693112004-08-16 01:10:21 +0000127void CodeGenTarget::ReadRegisters() const {
128 std::vector<Record*> Regs = Records.getAllDerivedDefinitions("Register");
129 if (Regs.empty())
130 throw std::string("No 'Register' subclasses defined!");
131
132 Registers.reserve(Regs.size());
133 Registers.assign(Regs.begin(), Regs.end());
134}
135
Chris Lattner7a680c62004-08-21 02:24:57 +0000136CodeGenRegister::CodeGenRegister(Record *R) : TheDef(R) {
137 DeclaredSpillSize = R->getValueAsInt("SpillSize");
138 DeclaredSpillAlignment = R->getValueAsInt("SpillAlignment");
139}
140
Chris Lattner26693112004-08-16 01:10:21 +0000141const std::string &CodeGenRegister::getName() const {
142 return TheDef->getName();
143}
144
Chris Lattner056afef2004-08-21 04:05:00 +0000145void CodeGenTarget::ReadRegisterClasses() const {
146 std::vector<Record*> RegClasses =
147 Records.getAllDerivedDefinitions("RegisterClass");
148 if (RegClasses.empty())
149 throw std::string("No 'RegisterClass' subclasses defined!");
150
151 RegisterClasses.reserve(RegClasses.size());
152 RegisterClasses.assign(RegClasses.begin(), RegClasses.end());
153}
154
155CodeGenRegisterClass::CodeGenRegisterClass(Record *R) : TheDef(R) {
Chris Lattnerc67c18f2005-08-19 18:45:20 +0000156 // Rename anonymous register classes.
157 if (R->getName().size() > 9 && R->getName()[9] == '.') {
158 static unsigned AnonCounter = 0;
159 R->setName("AnonRegClass_"+utostr(AnonCounter++));
160 }
161
Nate Begeman6510b222005-12-01 04:51:06 +0000162 std::vector<Record*> TypeList = R->getValueAsListOfDefs("RegTypes");
163 for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
164 Record *Type = TypeList[i];
165 if (!Type->isSubClassOf("ValueType"))
166 throw "RegTypes list member '" + Type->getName() +
167 "' does not derive from the ValueType class!";
168 VTs.push_back(getValueType(Type));
169 }
170 assert(!VTs.empty() && "RegisterClass must contain at least one ValueType!");
Chris Lattnerac468932005-08-19 19:12:51 +0000171
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000172 std::vector<Record*> RegList = R->getValueAsListOfDefs("MemberList");
173 for (unsigned i = 0, e = RegList.size(); i != e; ++i) {
174 Record *Reg = RegList[i];
Chris Lattner056afef2004-08-21 04:05:00 +0000175 if (!Reg->isSubClassOf("Register"))
176 throw "Register Class member '" + Reg->getName() +
177 "' does not derive from the Register class!";
178 Elements.push_back(Reg);
179 }
Nate Begeman6510b222005-12-01 04:51:06 +0000180
181 // Allow targets to override the size in bits of the RegisterClass.
182 unsigned Size = R->getValueAsInt("Size");
183
184 Namespace = R->getValueAsString("Namespace");
185 SpillSize = Size ? Size : MVT::getSizeInBits(VTs[0]);
186 SpillAlignment = R->getValueAsInt("Alignment");
187 MethodBodies = R->getValueAsCode("MethodBodies");
188 MethodProtos = R->getValueAsCode("MethodProtos");
Chris Lattner056afef2004-08-21 04:05:00 +0000189}
190
191const std::string &CodeGenRegisterClass::getName() const {
192 return TheDef->getName();
193}
194
Chris Lattnere9f4ba82005-09-08 21:43:21 +0000195void CodeGenTarget::ReadLegalValueTypes() const {
196 const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses();
197 for (unsigned i = 0, e = RCs.size(); i != e; ++i)
Nate Begeman6510b222005-12-01 04:51:06 +0000198 for (unsigned ri = 0, re = RCs[i].VTs.size(); ri != re; ++ri)
199 LegalValueTypes.push_back(RCs[i].VTs[ri]);
Chris Lattner75ee2eb2005-10-14 03:54:49 +0000200
201 // Remove duplicates.
202 std::sort(LegalValueTypes.begin(), LegalValueTypes.end());
203 LegalValueTypes.erase(std::unique(LegalValueTypes.begin(),
204 LegalValueTypes.end()),
205 LegalValueTypes.end());
Chris Lattnere9f4ba82005-09-08 21:43:21 +0000206}
Chris Lattner056afef2004-08-21 04:05:00 +0000207
Chris Lattner175580c2004-08-14 22:50:53 +0000208
Chris Lattnerec352402004-08-01 05:04:00 +0000209void CodeGenTarget::ReadInstructions() const {
210 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
Chris Lattneraa77d772006-01-27 01:45:06 +0000211 if (Insts.size() <= 2)
Chris Lattnerec352402004-08-01 05:04:00 +0000212 throw std::string("No 'Instruction' subclasses defined!");
213
Chris Lattneraa77d772006-01-27 01:45:06 +0000214 // Parse the instructions defined in the .td file.
Chris Lattner175580c2004-08-14 22:50:53 +0000215 std::string InstFormatName =
216 getAsmWriter()->getValueAsString("InstFormatName");
217
218 for (unsigned i = 0, e = Insts.size(); i != e; ++i) {
219 std::string AsmStr = Insts[i]->getValueAsString(InstFormatName);
220 Instructions.insert(std::make_pair(Insts[i]->getName(),
221 CodeGenInstruction(Insts[i], AsmStr)));
222 }
Chris Lattnerec352402004-08-01 05:04:00 +0000223}
224
Chris Lattnerd6488672005-01-22 18:58:51 +0000225/// getInstructionsByEnumValue - Return all of the instructions defined by the
226/// target, ordered by their enum value.
227void CodeGenTarget::
228getInstructionsByEnumValue(std::vector<const CodeGenInstruction*>
229 &NumberedInstructions) {
Chris Lattneraa77d772006-01-27 01:45:06 +0000230 std::map<std::string, CodeGenInstruction>::const_iterator I;
231 I = getInstructions().find("PHI");
232 if (I == Instructions.end()) throw "Could not find 'PHI' instruction!";
233 const CodeGenInstruction *PHI = &I->second;
234
235 I = getInstructions().find("INLINEASM");
236 if (I == Instructions.end()) throw "Could not find 'INLINEASM' instruction!";
237 const CodeGenInstruction *INLINEASM = &I->second;
238
Chris Lattnerd6488672005-01-22 18:58:51 +0000239 // Print out the rest of the instructions now.
Chris Lattnerd6488672005-01-22 18:58:51 +0000240 NumberedInstructions.push_back(PHI);
Chris Lattneraa77d772006-01-27 01:45:06 +0000241 NumberedInstructions.push_back(INLINEASM);
Chris Lattnerd6488672005-01-22 18:58:51 +0000242 for (inst_iterator II = inst_begin(), E = inst_end(); II != E; ++II)
Chris Lattneraa77d772006-01-27 01:45:06 +0000243 if (&II->second != PHI &&&II->second != INLINEASM)
Chris Lattnerd6488672005-01-22 18:58:51 +0000244 NumberedInstructions.push_back(&II->second);
245}
246
247
Misha Brukman35e83cc2004-10-14 05:50:43 +0000248/// isLittleEndianEncoding - Return whether this target encodes its instruction
249/// in little-endian format, i.e. bits laid out in the order [0..n]
250///
251bool CodeGenTarget::isLittleEndianEncoding() const {
252 return getInstructionSet()->getValueAsBit("isLittleEndianEncoding");
253}
254
Chris Lattner175580c2004-08-14 22:50:53 +0000255CodeGenInstruction::CodeGenInstruction(Record *R, const std::string &AsmStr)
256 : TheDef(R), AsmString(AsmStr) {
Chris Lattnerec352402004-08-01 05:04:00 +0000257 Name = R->getValueAsString("Name");
258 Namespace = R->getValueAsString("Namespace");
Chris Lattnerec352402004-08-01 05:04:00 +0000259
Chris Lattnerec352402004-08-01 05:04:00 +0000260 isReturn = R->getValueAsBit("isReturn");
261 isBranch = R->getValueAsBit("isBranch");
262 isBarrier = R->getValueAsBit("isBarrier");
263 isCall = R->getValueAsBit("isCall");
Nate Begemancdd66b52004-09-28 21:01:45 +0000264 isLoad = R->getValueAsBit("isLoad");
265 isStore = R->getValueAsBit("isStore");
Chris Lattnerec352402004-08-01 05:04:00 +0000266 isTwoAddress = R->getValueAsBit("isTwoAddress");
Chris Lattneraad75aa2005-01-02 02:29:04 +0000267 isConvertibleToThreeAddress = R->getValueAsBit("isConvertibleToThreeAddress");
268 isCommutable = R->getValueAsBit("isCommutable");
Chris Lattnerec352402004-08-01 05:04:00 +0000269 isTerminator = R->getValueAsBit("isTerminator");
Chris Lattner5b71d3a2004-09-28 18:38:01 +0000270 hasDelaySlot = R->getValueAsBit("hasDelaySlot");
Chris Lattnere3cbf822005-08-26 20:55:40 +0000271 usesCustomDAGSchedInserter = R->getValueAsBit("usesCustomDAGSchedInserter");
Evan Cheng1c3d19e2005-12-04 08:18:16 +0000272 hasCtrlDep = R->getValueAsBit("hasCtrlDep");
Evan Cheng2b4ea792005-12-26 09:11:45 +0000273 noResults = R->getValueAsBit("noResults");
Chris Lattnercfbf96a2005-08-18 23:38:41 +0000274 hasVariableNumberOfOperands = false;
275
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000276 DagInit *DI;
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000277 try {
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000278 DI = R->getValueAsDag("OperandList");
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000279 } catch (...) {
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000280 // Error getting operand list, just ignore it (sparcv9).
Chris Lattner87c59052004-08-01 07:42:39 +0000281 AsmString.clear();
282 OperandList.clear();
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000283 return;
284 }
285
286 unsigned MIOperandNo = 0;
287 std::set<std::string> OperandNames;
288 for (unsigned i = 0, e = DI->getNumArgs(); i != e; ++i) {
289 DefInit *Arg = dynamic_cast<DefInit*>(DI->getArg(i));
290 if (!Arg)
291 throw "Illegal operand for the '" + R->getName() + "' instruction!";
292
293 Record *Rec = Arg->getDef();
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000294 std::string PrintMethod = "printOperand";
295 unsigned NumOps = 1;
Chris Lattner33670792005-11-19 07:48:33 +0000296 DagInit *MIOpInfo = 0;
Nate Begeman86193d12005-12-01 00:12:04 +0000297 if (Rec->isSubClassOf("Operand")) {
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000298 PrintMethod = Rec->getValueAsString("PrintMethod");
299 NumOps = Rec->getValueAsInt("NumMIOperands");
Chris Lattner65303d62005-11-19 07:05:57 +0000300 MIOpInfo = Rec->getValueAsDag("MIOperandInfo");
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000301 } else if (Rec->getName() == "variable_ops") {
302 hasVariableNumberOfOperands = true;
303 continue;
Nate Begeman86193d12005-12-01 00:12:04 +0000304 } else if (!Rec->isSubClassOf("RegisterClass"))
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000305 throw "Unknown operand class '" + Rec->getName() +
306 "' in instruction '" + R->getName() + "' instruction!";
307
Chris Lattnerc4a8b732005-09-14 21:13:50 +0000308 // Check that the operand has a name and that it's unique.
309 if (DI->getArgName(i).empty())
310 throw "In instruction '" + R->getName() + "', operand #" + utostr(i) +
311 " has no name!";
312 if (!OperandNames.insert(DI->getArgName(i)).second)
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000313 throw "In instruction '" + R->getName() + "', operand #" + utostr(i) +
314 " has the same name as a previous operand!";
315
Nate Begeman86193d12005-12-01 00:12:04 +0000316 OperandList.push_back(OperandInfo(Rec, DI->getArgName(i), PrintMethod,
317 MIOperandNo, NumOps, MIOpInfo));
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000318 MIOperandNo += NumOps;
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000319 }
Chris Lattnerec352402004-08-01 05:04:00 +0000320}
321
322
Chris Lattner87c59052004-08-01 07:42:39 +0000323
324/// getOperandNamed - Return the index of the operand with the specified
325/// non-empty name. If the instruction does not have an operand with the
326/// specified name, throw an exception.
Misha Brukman35e83cc2004-10-14 05:50:43 +0000327///
Chris Lattner87c59052004-08-01 07:42:39 +0000328unsigned CodeGenInstruction::getOperandNamed(const std::string &Name) const {
329 assert(!Name.empty() && "Cannot search for operand with no name!");
330 for (unsigned i = 0, e = OperandList.size(); i != e; ++i)
331 if (OperandList[i].Name == Name) return i;
332 throw "Instruction '" + TheDef->getName() +
333 "' does not have an operand named '$" + Name + "'!";
334}
Evan Cheng0fc71982005-12-08 02:00:36 +0000335
336//===----------------------------------------------------------------------===//
337// ComplexPattern implementation
338//
339ComplexPattern::ComplexPattern(Record *R) {
Evan Cheng3aa39f42005-12-08 02:14:08 +0000340 Ty = ::getValueType(R->getValueAsDef("Ty"));
341 NumOperands = R->getValueAsInt("NumOperands");
342 SelectFunc = R->getValueAsString("SelectFunc");
343 RootNodes = R->getValueAsListOfDefs("RootNodes");
Evan Cheng0fc71982005-12-08 02:00:36 +0000344}
Evan Cheng3aa39f42005-12-08 02:14:08 +0000345