blob: 8c6436d5de3ef950c81555a3cc7c1d75f979836c [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";
Evan Cheng9073dea2006-03-01 01:10:52 +000057 case MVT::v2f32: return "v2f32";
Nate Begeman02fc8ff2005-11-29 06:19:38 +000058 case MVT::v4f32: return "v4f32";
59 case MVT::v2f64: return "v2f64";
Chris Lattnerd3464c12003-08-07 23:15:21 +000060 default: assert(0 && "ILLEGAL VALUE TYPE!"); return "";
Chris Lattner45872072003-08-07 05:38:11 +000061 }
Chris Lattnerd3464c12003-08-07 23:15:21 +000062}
63
Chris Lattner2082ebe2004-08-01 03:55:39 +000064std::string llvm::getEnumName(MVT::ValueType T) {
Chris Lattnerb72fb7e2003-08-10 19:50:32 +000065 switch (T) {
66 case MVT::Other: return "Other";
67 case MVT::i1: return "i1";
68 case MVT::i8: return "i8";
69 case MVT::i16: return "i16";
70 case MVT::i32: return "i32";
71 case MVT::i64: return "i64";
72 case MVT::i128: return "i128";
73 case MVT::f32: return "f32";
74 case MVT::f64: return "f64";
75 case MVT::f80: return "f80";
76 case MVT::f128: return "f128";
Evan Chengbcecf332005-12-17 01:19:28 +000077 case MVT::Flag: return "Flag";
Chris Lattnerb72fb7e2003-08-10 19:50:32 +000078 case MVT::isVoid:return "isVoid";
Nate Begeman02fc8ff2005-11-29 06:19:38 +000079 case MVT::v16i8: return "v16i8";
80 case MVT::v8i16: return "v8i16";
81 case MVT::v4i32: return "v4i32";
82 case MVT::v2i64: return "v2i64";
Evan Cheng9073dea2006-03-01 01:10:52 +000083 case MVT::v2f32: return "v2f32";
Nate Begeman02fc8ff2005-11-29 06:19:38 +000084 case MVT::v4f32: return "v4f32";
85 case MVT::v2f64: return "v2f64";
Chris Lattnerb72fb7e2003-08-10 19:50:32 +000086 default: assert(0 && "ILLEGAL VALUE TYPE!"); return "";
87 }
88}
89
Chris Lattnerd3464c12003-08-07 23:15:21 +000090
Chris Lattner2082ebe2004-08-01 03:55:39 +000091std::ostream &llvm::operator<<(std::ostream &OS, MVT::ValueType T) {
Chris Lattnerd3464c12003-08-07 23:15:21 +000092 return OS << getName(T);
Chris Lattner45872072003-08-07 05:38:11 +000093}
94
95
Chris Lattner45872072003-08-07 05:38:11 +000096/// getTarget - Return the current instance of the Target class.
97///
Brian Gaekebe7f4af2003-10-10 21:55:29 +000098CodeGenTarget::CodeGenTarget() : PointerType(MVT::Other) {
Chris Lattner45872072003-08-07 05:38:11 +000099 std::vector<Record*> Targets = Records.getAllDerivedDefinitions("Target");
Misha Brukmanbebdb202004-06-04 14:59:42 +0000100 if (Targets.size() == 0)
Misha Brukman3da94ae2005-04-22 00:00:37 +0000101 throw std::string("ERROR: No 'Target' subclasses defined!");
Chris Lattner45872072003-08-07 05:38:11 +0000102 if (Targets.size() != 1)
103 throw std::string("ERROR: Multiple subclasses of Target defined!");
104 TargetRec = Targets[0];
105
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000106 // Read in all of the CalleeSavedRegisters.
107 CalleeSavedRegisters =TargetRec->getValueAsListOfDefs("CalleeSavedRegisters");
Chris Lattner45872072003-08-07 05:38:11 +0000108 PointerType = getValueType(TargetRec->getValueAsDef("PointerType"));
109}
110
111
112const std::string &CodeGenTarget::getName() const {
113 return TargetRec->getName();
114}
115
116Record *CodeGenTarget::getInstructionSet() const {
117 return TargetRec->getValueAsDef("InstructionSet");
118}
Brian Gaeked0fde302003-11-11 22:41:34 +0000119
Chris Lattner175580c2004-08-14 22:50:53 +0000120/// getAsmWriter - Return the AssemblyWriter definition for this target.
121///
122Record *CodeGenTarget::getAsmWriter() const {
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000123 std::vector<Record*> LI = TargetRec->getValueAsListOfDefs("AssemblyWriters");
124 if (AsmWriterNum >= LI.size())
Chris Lattner560a79f2004-10-03 19:34:31 +0000125 throw "Target does not have an AsmWriter #" + utostr(AsmWriterNum) + "!";
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000126 return LI[AsmWriterNum];
Chris Lattner175580c2004-08-14 22:50:53 +0000127}
128
Chris Lattner26693112004-08-16 01:10:21 +0000129void CodeGenTarget::ReadRegisters() const {
130 std::vector<Record*> Regs = Records.getAllDerivedDefinitions("Register");
131 if (Regs.empty())
132 throw std::string("No 'Register' subclasses defined!");
133
134 Registers.reserve(Regs.size());
135 Registers.assign(Regs.begin(), Regs.end());
136}
137
Chris Lattner7a680c62004-08-21 02:24:57 +0000138CodeGenRegister::CodeGenRegister(Record *R) : TheDef(R) {
139 DeclaredSpillSize = R->getValueAsInt("SpillSize");
140 DeclaredSpillAlignment = R->getValueAsInt("SpillAlignment");
141}
142
Chris Lattner26693112004-08-16 01:10:21 +0000143const std::string &CodeGenRegister::getName() const {
144 return TheDef->getName();
145}
146
Chris Lattner056afef2004-08-21 04:05:00 +0000147void CodeGenTarget::ReadRegisterClasses() const {
148 std::vector<Record*> RegClasses =
149 Records.getAllDerivedDefinitions("RegisterClass");
150 if (RegClasses.empty())
151 throw std::string("No 'RegisterClass' subclasses defined!");
152
153 RegisterClasses.reserve(RegClasses.size());
154 RegisterClasses.assign(RegClasses.begin(), RegClasses.end());
155}
156
157CodeGenRegisterClass::CodeGenRegisterClass(Record *R) : TheDef(R) {
Chris Lattnerc67c18f2005-08-19 18:45:20 +0000158 // Rename anonymous register classes.
159 if (R->getName().size() > 9 && R->getName()[9] == '.') {
160 static unsigned AnonCounter = 0;
161 R->setName("AnonRegClass_"+utostr(AnonCounter++));
162 }
163
Nate Begeman6510b222005-12-01 04:51:06 +0000164 std::vector<Record*> TypeList = R->getValueAsListOfDefs("RegTypes");
165 for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
166 Record *Type = TypeList[i];
167 if (!Type->isSubClassOf("ValueType"))
168 throw "RegTypes list member '" + Type->getName() +
169 "' does not derive from the ValueType class!";
170 VTs.push_back(getValueType(Type));
171 }
172 assert(!VTs.empty() && "RegisterClass must contain at least one ValueType!");
Chris Lattnerac468932005-08-19 19:12:51 +0000173
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000174 std::vector<Record*> RegList = R->getValueAsListOfDefs("MemberList");
175 for (unsigned i = 0, e = RegList.size(); i != e; ++i) {
176 Record *Reg = RegList[i];
Chris Lattner056afef2004-08-21 04:05:00 +0000177 if (!Reg->isSubClassOf("Register"))
178 throw "Register Class member '" + Reg->getName() +
179 "' does not derive from the Register class!";
180 Elements.push_back(Reg);
181 }
Nate Begeman6510b222005-12-01 04:51:06 +0000182
183 // Allow targets to override the size in bits of the RegisterClass.
184 unsigned Size = R->getValueAsInt("Size");
185
186 Namespace = R->getValueAsString("Namespace");
187 SpillSize = Size ? Size : MVT::getSizeInBits(VTs[0]);
188 SpillAlignment = R->getValueAsInt("Alignment");
189 MethodBodies = R->getValueAsCode("MethodBodies");
190 MethodProtos = R->getValueAsCode("MethodProtos");
Chris Lattner056afef2004-08-21 04:05:00 +0000191}
192
193const std::string &CodeGenRegisterClass::getName() const {
194 return TheDef->getName();
195}
196
Chris Lattnere9f4ba82005-09-08 21:43:21 +0000197void CodeGenTarget::ReadLegalValueTypes() const {
198 const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses();
199 for (unsigned i = 0, e = RCs.size(); i != e; ++i)
Nate Begeman6510b222005-12-01 04:51:06 +0000200 for (unsigned ri = 0, re = RCs[i].VTs.size(); ri != re; ++ri)
201 LegalValueTypes.push_back(RCs[i].VTs[ri]);
Chris Lattner75ee2eb2005-10-14 03:54:49 +0000202
203 // Remove duplicates.
204 std::sort(LegalValueTypes.begin(), LegalValueTypes.end());
205 LegalValueTypes.erase(std::unique(LegalValueTypes.begin(),
206 LegalValueTypes.end()),
207 LegalValueTypes.end());
Chris Lattnere9f4ba82005-09-08 21:43:21 +0000208}
Chris Lattner056afef2004-08-21 04:05:00 +0000209
Chris Lattner175580c2004-08-14 22:50:53 +0000210
Chris Lattnerec352402004-08-01 05:04:00 +0000211void CodeGenTarget::ReadInstructions() const {
212 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
Chris Lattneraa77d772006-01-27 01:45:06 +0000213 if (Insts.size() <= 2)
Chris Lattnerec352402004-08-01 05:04:00 +0000214 throw std::string("No 'Instruction' subclasses defined!");
215
Chris Lattneraa77d772006-01-27 01:45:06 +0000216 // Parse the instructions defined in the .td file.
Chris Lattner175580c2004-08-14 22:50:53 +0000217 std::string InstFormatName =
218 getAsmWriter()->getValueAsString("InstFormatName");
219
220 for (unsigned i = 0, e = Insts.size(); i != e; ++i) {
221 std::string AsmStr = Insts[i]->getValueAsString(InstFormatName);
222 Instructions.insert(std::make_pair(Insts[i]->getName(),
223 CodeGenInstruction(Insts[i], AsmStr)));
224 }
Chris Lattnerec352402004-08-01 05:04:00 +0000225}
226
Chris Lattnerd6488672005-01-22 18:58:51 +0000227/// getInstructionsByEnumValue - Return all of the instructions defined by the
228/// target, ordered by their enum value.
229void CodeGenTarget::
230getInstructionsByEnumValue(std::vector<const CodeGenInstruction*>
231 &NumberedInstructions) {
Chris Lattneraa77d772006-01-27 01:45:06 +0000232 std::map<std::string, CodeGenInstruction>::const_iterator I;
233 I = getInstructions().find("PHI");
234 if (I == Instructions.end()) throw "Could not find 'PHI' instruction!";
235 const CodeGenInstruction *PHI = &I->second;
236
237 I = getInstructions().find("INLINEASM");
238 if (I == Instructions.end()) throw "Could not find 'INLINEASM' instruction!";
239 const CodeGenInstruction *INLINEASM = &I->second;
240
Chris Lattnerd6488672005-01-22 18:58:51 +0000241 // Print out the rest of the instructions now.
Chris Lattnerd6488672005-01-22 18:58:51 +0000242 NumberedInstructions.push_back(PHI);
Chris Lattneraa77d772006-01-27 01:45:06 +0000243 NumberedInstructions.push_back(INLINEASM);
Chris Lattnerd6488672005-01-22 18:58:51 +0000244 for (inst_iterator II = inst_begin(), E = inst_end(); II != E; ++II)
Chris Lattneraa77d772006-01-27 01:45:06 +0000245 if (&II->second != PHI &&&II->second != INLINEASM)
Chris Lattnerd6488672005-01-22 18:58:51 +0000246 NumberedInstructions.push_back(&II->second);
247}
248
249
Misha Brukman35e83cc2004-10-14 05:50:43 +0000250/// isLittleEndianEncoding - Return whether this target encodes its instruction
251/// in little-endian format, i.e. bits laid out in the order [0..n]
252///
253bool CodeGenTarget::isLittleEndianEncoding() const {
254 return getInstructionSet()->getValueAsBit("isLittleEndianEncoding");
255}
256
Chris Lattner175580c2004-08-14 22:50:53 +0000257CodeGenInstruction::CodeGenInstruction(Record *R, const std::string &AsmStr)
258 : TheDef(R), AsmString(AsmStr) {
Chris Lattnerec352402004-08-01 05:04:00 +0000259 Name = R->getValueAsString("Name");
260 Namespace = R->getValueAsString("Namespace");
Chris Lattnerec352402004-08-01 05:04:00 +0000261
Chris Lattnerec352402004-08-01 05:04:00 +0000262 isReturn = R->getValueAsBit("isReturn");
263 isBranch = R->getValueAsBit("isBranch");
264 isBarrier = R->getValueAsBit("isBarrier");
265 isCall = R->getValueAsBit("isCall");
Nate Begemancdd66b52004-09-28 21:01:45 +0000266 isLoad = R->getValueAsBit("isLoad");
267 isStore = R->getValueAsBit("isStore");
Chris Lattnerec352402004-08-01 05:04:00 +0000268 isTwoAddress = R->getValueAsBit("isTwoAddress");
Chris Lattneraad75aa2005-01-02 02:29:04 +0000269 isConvertibleToThreeAddress = R->getValueAsBit("isConvertibleToThreeAddress");
270 isCommutable = R->getValueAsBit("isCommutable");
Chris Lattnerec352402004-08-01 05:04:00 +0000271 isTerminator = R->getValueAsBit("isTerminator");
Chris Lattner5b71d3a2004-09-28 18:38:01 +0000272 hasDelaySlot = R->getValueAsBit("hasDelaySlot");
Chris Lattnere3cbf822005-08-26 20:55:40 +0000273 usesCustomDAGSchedInserter = R->getValueAsBit("usesCustomDAGSchedInserter");
Evan Cheng1c3d19e2005-12-04 08:18:16 +0000274 hasCtrlDep = R->getValueAsBit("hasCtrlDep");
Evan Cheng2b4ea792005-12-26 09:11:45 +0000275 noResults = R->getValueAsBit("noResults");
Chris Lattnercfbf96a2005-08-18 23:38:41 +0000276 hasVariableNumberOfOperands = false;
277
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000278 DagInit *DI;
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000279 try {
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000280 DI = R->getValueAsDag("OperandList");
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000281 } catch (...) {
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000282 // Error getting operand list, just ignore it (sparcv9).
Chris Lattner87c59052004-08-01 07:42:39 +0000283 AsmString.clear();
284 OperandList.clear();
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000285 return;
286 }
287
288 unsigned MIOperandNo = 0;
289 std::set<std::string> OperandNames;
290 for (unsigned i = 0, e = DI->getNumArgs(); i != e; ++i) {
291 DefInit *Arg = dynamic_cast<DefInit*>(DI->getArg(i));
292 if (!Arg)
293 throw "Illegal operand for the '" + R->getName() + "' instruction!";
294
295 Record *Rec = Arg->getDef();
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000296 std::string PrintMethod = "printOperand";
297 unsigned NumOps = 1;
Chris Lattner33670792005-11-19 07:48:33 +0000298 DagInit *MIOpInfo = 0;
Nate Begeman86193d12005-12-01 00:12:04 +0000299 if (Rec->isSubClassOf("Operand")) {
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000300 PrintMethod = Rec->getValueAsString("PrintMethod");
301 NumOps = Rec->getValueAsInt("NumMIOperands");
Chris Lattner65303d62005-11-19 07:05:57 +0000302 MIOpInfo = Rec->getValueAsDag("MIOperandInfo");
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000303 } else if (Rec->getName() == "variable_ops") {
304 hasVariableNumberOfOperands = true;
305 continue;
Nate Begeman86193d12005-12-01 00:12:04 +0000306 } else if (!Rec->isSubClassOf("RegisterClass"))
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000307 throw "Unknown operand class '" + Rec->getName() +
308 "' in instruction '" + R->getName() + "' instruction!";
309
Chris Lattnerc4a8b732005-09-14 21:13:50 +0000310 // Check that the operand has a name and that it's unique.
311 if (DI->getArgName(i).empty())
312 throw "In instruction '" + R->getName() + "', operand #" + utostr(i) +
313 " has no name!";
314 if (!OperandNames.insert(DI->getArgName(i)).second)
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000315 throw "In instruction '" + R->getName() + "', operand #" + utostr(i) +
316 " has the same name as a previous operand!";
317
Nate Begeman86193d12005-12-01 00:12:04 +0000318 OperandList.push_back(OperandInfo(Rec, DI->getArgName(i), PrintMethod,
319 MIOperandNo, NumOps, MIOpInfo));
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000320 MIOperandNo += NumOps;
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000321 }
Chris Lattnerec352402004-08-01 05:04:00 +0000322}
323
324
Chris Lattner87c59052004-08-01 07:42:39 +0000325
326/// getOperandNamed - Return the index of the operand with the specified
327/// non-empty name. If the instruction does not have an operand with the
328/// specified name, throw an exception.
Misha Brukman35e83cc2004-10-14 05:50:43 +0000329///
Chris Lattner87c59052004-08-01 07:42:39 +0000330unsigned CodeGenInstruction::getOperandNamed(const std::string &Name) const {
331 assert(!Name.empty() && "Cannot search for operand with no name!");
332 for (unsigned i = 0, e = OperandList.size(); i != e; ++i)
333 if (OperandList[i].Name == Name) return i;
334 throw "Instruction '" + TheDef->getName() +
335 "' does not have an operand named '$" + Name + "'!";
336}
Evan Cheng0fc71982005-12-08 02:00:36 +0000337
338//===----------------------------------------------------------------------===//
339// ComplexPattern implementation
340//
341ComplexPattern::ComplexPattern(Record *R) {
Evan Cheng3aa39f42005-12-08 02:14:08 +0000342 Ty = ::getValueType(R->getValueAsDef("Ty"));
343 NumOperands = R->getValueAsInt("NumOperands");
344 SelectFunc = R->getValueAsString("SelectFunc");
345 RootNodes = R->getValueAsListOfDefs("RootNodes");
Evan Cheng0fc71982005-12-08 02:00:36 +0000346}
Evan Cheng3aa39f42005-12-08 02:14:08 +0000347