blob: c2e736968ef0a6adcd9b7a37c3abac9215044fe8 [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 Lattner43fbbc32006-03-24 19:49:31 +000018#include "CodeGenIntrinsics.h"
Chris Lattner45872072003-08-07 05:38:11 +000019#include "Record.h"
Chris Lattner560a79f2004-10-03 19:34:31 +000020#include "llvm/ADT/StringExtras.h"
21#include "llvm/Support/CommandLine.h"
Bill Wendlingf5da1332006-12-07 22:21:48 +000022#include "llvm/Support/Streams.h"
Chris Lattner5d7d3db2005-09-14 21:05:02 +000023#include <set>
Chris Lattner75ee2eb2005-10-14 03:54:49 +000024#include <algorithm>
Chris Lattner2082ebe2004-08-01 03:55:39 +000025using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000026
Chris Lattner560a79f2004-10-03 19:34:31 +000027static cl::opt<unsigned>
28AsmWriterNum("asmwriternum", cl::init(0),
29 cl::desc("Make -gen-asm-writer emit assembly writer #N"));
30
Chris Lattner45872072003-08-07 05:38:11 +000031/// getValueType - Return the MCV::ValueType that the specified TableGen record
32/// corresponds to.
Dan Gohman3bf6e182007-07-13 20:16:50 +000033MVT::ValueType llvm::getValueType(Record *Rec) {
Evan Cheng2618d072006-05-17 20:37:59 +000034 return (MVT::ValueType)Rec->getValueAsInt("Value");
Chris Lattner45872072003-08-07 05:38:11 +000035}
36
Chris Lattner2082ebe2004-08-01 03:55:39 +000037std::string llvm::getName(MVT::ValueType T) {
Chris Lattner45872072003-08-07 05:38:11 +000038 switch (T) {
Chris Lattnerd3464c12003-08-07 23:15:21 +000039 case MVT::Other: return "UNKNOWN";
Evan Chengd7c2c862006-06-15 00:16:37 +000040 case MVT::i1: return "MVT::i1";
41 case MVT::i8: return "MVT::i8";
42 case MVT::i16: return "MVT::i16";
43 case MVT::i32: return "MVT::i32";
44 case MVT::i64: return "MVT::i64";
45 case MVT::i128: return "MVT::i128";
Reid Spencerc4de3de2007-04-01 07:20:02 +000046 case MVT::iAny: return "MVT::iAny";
Evan Chengd7c2c862006-06-15 00:16:37 +000047 case MVT::f32: return "MVT::f32";
48 case MVT::f64: return "MVT::f64";
49 case MVT::f80: return "MVT::f80";
50 case MVT::f128: return "MVT::f128";
51 case MVT::Flag: return "MVT::Flag";
52 case MVT::isVoid:return "MVT::void";
53 case MVT::v8i8: return "MVT::v8i8";
54 case MVT::v4i16: return "MVT::v4i16";
55 case MVT::v2i32: return "MVT::v2i32";
Bill Wendlingeebc8a12007-03-26 07:53:08 +000056 case MVT::v1i64: return "MVT::v1i64";
Evan Chengd7c2c862006-06-15 00:16:37 +000057 case MVT::v16i8: return "MVT::v16i8";
58 case MVT::v8i16: return "MVT::v8i16";
59 case MVT::v4i32: return "MVT::v4i32";
60 case MVT::v2i64: return "MVT::v2i64";
61 case MVT::v2f32: return "MVT::v2f32";
62 case MVT::v4f32: return "MVT::v4f32";
63 case MVT::v2f64: return "MVT::v2f64";
Evan Cheng6b125162006-05-17 20:55:51 +000064 case MVT::iPTR: return "TLI.getPointerTy()";
Chris Lattnerd3464c12003-08-07 23:15:21 +000065 default: assert(0 && "ILLEGAL VALUE TYPE!"); return "";
Chris Lattner45872072003-08-07 05:38:11 +000066 }
Chris Lattnerd3464c12003-08-07 23:15:21 +000067}
68
Chris Lattner2082ebe2004-08-01 03:55:39 +000069std::string llvm::getEnumName(MVT::ValueType T) {
Chris Lattnerb72fb7e2003-08-10 19:50:32 +000070 switch (T) {
Evan Cheng2618d072006-05-17 20:37:59 +000071 case MVT::Other: return "MVT::Other";
72 case MVT::i1: return "MVT::i1";
73 case MVT::i8: return "MVT::i8";
74 case MVT::i16: return "MVT::i16";
75 case MVT::i32: return "MVT::i32";
76 case MVT::i64: return "MVT::i64";
77 case MVT::i128: return "MVT::i128";
Reid Spencerc4de3de2007-04-01 07:20:02 +000078 case MVT::iAny: return "MVT::iAny";
Evan Cheng2618d072006-05-17 20:37:59 +000079 case MVT::f32: return "MVT::f32";
80 case MVT::f64: return "MVT::f64";
81 case MVT::f80: return "MVT::f80";
82 case MVT::f128: return "MVT::f128";
83 case MVT::Flag: return "MVT::Flag";
84 case MVT::isVoid:return "MVT::isVoid";
85 case MVT::v8i8: return "MVT::v8i8";
86 case MVT::v4i16: return "MVT::v4i16";
87 case MVT::v2i32: return "MVT::v2i32";
Bill Wendlingeebc8a12007-03-26 07:53:08 +000088 case MVT::v1i64: return "MVT::v1i64";
Evan Cheng2618d072006-05-17 20:37:59 +000089 case MVT::v16i8: return "MVT::v16i8";
90 case MVT::v8i16: return "MVT::v8i16";
91 case MVT::v4i32: return "MVT::v4i32";
92 case MVT::v2i64: return "MVT::v2i64";
93 case MVT::v2f32: return "MVT::v2f32";
94 case MVT::v4f32: return "MVT::v4f32";
95 case MVT::v2f64: return "MVT::v2f64";
Evan Cheng6b125162006-05-17 20:55:51 +000096 case MVT::iPTR: return "TLI.getPointerTy()";
Chris Lattnerb72fb7e2003-08-10 19:50:32 +000097 default: assert(0 && "ILLEGAL VALUE TYPE!"); return "";
98 }
99}
100
Chris Lattnerd3464c12003-08-07 23:15:21 +0000101
Chris Lattner45872072003-08-07 05:38:11 +0000102/// getTarget - Return the current instance of the Target class.
103///
Evan Cheng2618d072006-05-17 20:37:59 +0000104CodeGenTarget::CodeGenTarget() {
Chris Lattner45872072003-08-07 05:38:11 +0000105 std::vector<Record*> Targets = Records.getAllDerivedDefinitions("Target");
Misha Brukmanbebdb202004-06-04 14:59:42 +0000106 if (Targets.size() == 0)
Misha Brukman3da94ae2005-04-22 00:00:37 +0000107 throw std::string("ERROR: No 'Target' subclasses defined!");
Chris Lattner45872072003-08-07 05:38:11 +0000108 if (Targets.size() != 1)
109 throw std::string("ERROR: Multiple subclasses of Target defined!");
110 TargetRec = Targets[0];
Chris Lattner45872072003-08-07 05:38:11 +0000111}
112
113
114const std::string &CodeGenTarget::getName() const {
115 return TargetRec->getName();
116}
117
118Record *CodeGenTarget::getInstructionSet() const {
119 return TargetRec->getValueAsDef("InstructionSet");
120}
Brian Gaeked0fde302003-11-11 22:41:34 +0000121
Chris Lattner175580c2004-08-14 22:50:53 +0000122/// getAsmWriter - Return the AssemblyWriter definition for this target.
123///
124Record *CodeGenTarget::getAsmWriter() const {
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000125 std::vector<Record*> LI = TargetRec->getValueAsListOfDefs("AssemblyWriters");
126 if (AsmWriterNum >= LI.size())
Chris Lattner560a79f2004-10-03 19:34:31 +0000127 throw "Target does not have an AsmWriter #" + utostr(AsmWriterNum) + "!";
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000128 return LI[AsmWriterNum];
Chris Lattner175580c2004-08-14 22:50:53 +0000129}
130
Chris Lattner26693112004-08-16 01:10:21 +0000131void CodeGenTarget::ReadRegisters() const {
132 std::vector<Record*> Regs = Records.getAllDerivedDefinitions("Register");
133 if (Regs.empty())
134 throw std::string("No 'Register' subclasses defined!");
135
136 Registers.reserve(Regs.size());
137 Registers.assign(Regs.begin(), Regs.end());
138}
139
Chris Lattner7a680c62004-08-21 02:24:57 +0000140CodeGenRegister::CodeGenRegister(Record *R) : TheDef(R) {
141 DeclaredSpillSize = R->getValueAsInt("SpillSize");
142 DeclaredSpillAlignment = R->getValueAsInt("SpillAlignment");
143}
144
Chris Lattner26693112004-08-16 01:10:21 +0000145const std::string &CodeGenRegister::getName() const {
146 return TheDef->getName();
147}
148
Chris Lattner056afef2004-08-21 04:05:00 +0000149void CodeGenTarget::ReadRegisterClasses() const {
150 std::vector<Record*> RegClasses =
151 Records.getAllDerivedDefinitions("RegisterClass");
152 if (RegClasses.empty())
153 throw std::string("No 'RegisterClass' subclasses defined!");
154
155 RegisterClasses.reserve(RegClasses.size());
156 RegisterClasses.assign(RegClasses.begin(), RegClasses.end());
157}
158
Evan Cheng44a65fa2006-05-16 07:05:30 +0000159std::vector<unsigned char> CodeGenTarget::getRegisterVTs(Record *R) const {
160 std::vector<unsigned char> Result;
161 const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses();
162 for (unsigned i = 0, e = RCs.size(); i != e; ++i) {
163 const CodeGenRegisterClass &RC = RegisterClasses[i];
164 for (unsigned ei = 0, ee = RC.Elements.size(); ei != ee; ++ei) {
165 if (R == RC.Elements[ei]) {
166 const std::vector<MVT::ValueType> &InVTs = RC.getValueTypes();
167 for (unsigned i = 0, e = InVTs.size(); i != e; ++i)
168 Result.push_back(InVTs[i]);
169 }
170 }
171 }
172 return Result;
173}
174
175
Chris Lattner056afef2004-08-21 04:05:00 +0000176CodeGenRegisterClass::CodeGenRegisterClass(Record *R) : TheDef(R) {
Chris Lattnerc67c18f2005-08-19 18:45:20 +0000177 // Rename anonymous register classes.
178 if (R->getName().size() > 9 && R->getName()[9] == '.') {
179 static unsigned AnonCounter = 0;
180 R->setName("AnonRegClass_"+utostr(AnonCounter++));
181 }
182
Nate Begeman6510b222005-12-01 04:51:06 +0000183 std::vector<Record*> TypeList = R->getValueAsListOfDefs("RegTypes");
184 for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
185 Record *Type = TypeList[i];
186 if (!Type->isSubClassOf("ValueType"))
187 throw "RegTypes list member '" + Type->getName() +
188 "' does not derive from the ValueType class!";
189 VTs.push_back(getValueType(Type));
190 }
191 assert(!VTs.empty() && "RegisterClass must contain at least one ValueType!");
Chris Lattnerac468932005-08-19 19:12:51 +0000192
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000193 std::vector<Record*> RegList = R->getValueAsListOfDefs("MemberList");
194 for (unsigned i = 0, e = RegList.size(); i != e; ++i) {
195 Record *Reg = RegList[i];
Chris Lattner056afef2004-08-21 04:05:00 +0000196 if (!Reg->isSubClassOf("Register"))
197 throw "Register Class member '" + Reg->getName() +
198 "' does not derive from the Register class!";
199 Elements.push_back(Reg);
200 }
Nate Begeman6510b222005-12-01 04:51:06 +0000201
Christopher Lamba3211252007-06-13 22:20:15 +0000202 std::vector<Record*> SubRegClassList =
203 R->getValueAsListOfDefs("SubRegClassList");
204 for (unsigned i = 0, e = SubRegClassList.size(); i != e; ++i) {
205 Record *SubRegClass = SubRegClassList[i];
206 if (!SubRegClass->isSubClassOf("RegisterClass"))
207 throw "Register Class member '" + SubRegClass->getName() +
208 "' does not derive from the RegisterClass class!";
209 SubRegClasses.push_back(SubRegClass);
210 }
211
Nate Begeman6510b222005-12-01 04:51:06 +0000212 // Allow targets to override the size in bits of the RegisterClass.
213 unsigned Size = R->getValueAsInt("Size");
214
215 Namespace = R->getValueAsString("Namespace");
216 SpillSize = Size ? Size : MVT::getSizeInBits(VTs[0]);
217 SpillAlignment = R->getValueAsInt("Alignment");
218 MethodBodies = R->getValueAsCode("MethodBodies");
219 MethodProtos = R->getValueAsCode("MethodProtos");
Chris Lattner056afef2004-08-21 04:05:00 +0000220}
221
222const std::string &CodeGenRegisterClass::getName() const {
223 return TheDef->getName();
224}
225
Chris Lattnere9f4ba82005-09-08 21:43:21 +0000226void CodeGenTarget::ReadLegalValueTypes() const {
227 const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses();
228 for (unsigned i = 0, e = RCs.size(); i != e; ++i)
Nate Begeman6510b222005-12-01 04:51:06 +0000229 for (unsigned ri = 0, re = RCs[i].VTs.size(); ri != re; ++ri)
230 LegalValueTypes.push_back(RCs[i].VTs[ri]);
Chris Lattner75ee2eb2005-10-14 03:54:49 +0000231
232 // Remove duplicates.
233 std::sort(LegalValueTypes.begin(), LegalValueTypes.end());
234 LegalValueTypes.erase(std::unique(LegalValueTypes.begin(),
235 LegalValueTypes.end()),
236 LegalValueTypes.end());
Chris Lattnere9f4ba82005-09-08 21:43:21 +0000237}
Chris Lattner056afef2004-08-21 04:05:00 +0000238
Chris Lattner175580c2004-08-14 22:50:53 +0000239
Chris Lattnerec352402004-08-01 05:04:00 +0000240void CodeGenTarget::ReadInstructions() const {
241 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
Chris Lattneraa77d772006-01-27 01:45:06 +0000242 if (Insts.size() <= 2)
Chris Lattnerec352402004-08-01 05:04:00 +0000243 throw std::string("No 'Instruction' subclasses defined!");
244
Chris Lattneraa77d772006-01-27 01:45:06 +0000245 // Parse the instructions defined in the .td file.
Chris Lattner175580c2004-08-14 22:50:53 +0000246 std::string InstFormatName =
247 getAsmWriter()->getValueAsString("InstFormatName");
248
249 for (unsigned i = 0, e = Insts.size(); i != e; ++i) {
250 std::string AsmStr = Insts[i]->getValueAsString(InstFormatName);
251 Instructions.insert(std::make_pair(Insts[i]->getName(),
252 CodeGenInstruction(Insts[i], AsmStr)));
253 }
Chris Lattnerec352402004-08-01 05:04:00 +0000254}
255
Chris Lattnerd6488672005-01-22 18:58:51 +0000256/// getInstructionsByEnumValue - Return all of the instructions defined by the
257/// target, ordered by their enum value.
258void CodeGenTarget::
259getInstructionsByEnumValue(std::vector<const CodeGenInstruction*>
260 &NumberedInstructions) {
Chris Lattneraa77d772006-01-27 01:45:06 +0000261 std::map<std::string, CodeGenInstruction>::const_iterator I;
262 I = getInstructions().find("PHI");
263 if (I == Instructions.end()) throw "Could not find 'PHI' instruction!";
264 const CodeGenInstruction *PHI = &I->second;
265
266 I = getInstructions().find("INLINEASM");
267 if (I == Instructions.end()) throw "Could not find 'INLINEASM' instruction!";
268 const CodeGenInstruction *INLINEASM = &I->second;
269
Jim Laskeya683f9b2007-01-26 17:29:20 +0000270 I = getInstructions().find("LABEL");
271 if (I == Instructions.end()) throw "Could not find 'LABEL' instruction!";
272 const CodeGenInstruction *LABEL = &I->second;
273
Chris Lattnerd6488672005-01-22 18:58:51 +0000274 // Print out the rest of the instructions now.
Chris Lattnerd6488672005-01-22 18:58:51 +0000275 NumberedInstructions.push_back(PHI);
Chris Lattneraa77d772006-01-27 01:45:06 +0000276 NumberedInstructions.push_back(INLINEASM);
Jim Laskeya683f9b2007-01-26 17:29:20 +0000277 NumberedInstructions.push_back(LABEL);
Chris Lattnerd6488672005-01-22 18:58:51 +0000278 for (inst_iterator II = inst_begin(), E = inst_end(); II != E; ++II)
Jim Laskeya683f9b2007-01-26 17:29:20 +0000279 if (&II->second != PHI &&
280 &II->second != INLINEASM &&
281 &II->second != LABEL)
Chris Lattnerd6488672005-01-22 18:58:51 +0000282 NumberedInstructions.push_back(&II->second);
283}
284
285
Misha Brukman35e83cc2004-10-14 05:50:43 +0000286/// isLittleEndianEncoding - Return whether this target encodes its instruction
287/// in little-endian format, i.e. bits laid out in the order [0..n]
288///
289bool CodeGenTarget::isLittleEndianEncoding() const {
290 return getInstructionSet()->getValueAsBit("isLittleEndianEncoding");
291}
292
Chris Lattner0bb75002006-11-15 02:38:17 +0000293
294
295static void ParseConstraint(const std::string &CStr, CodeGenInstruction *I) {
296 // FIXME: Only supports TIED_TO for now.
297 std::string::size_type pos = CStr.find_first_of('=');
Evan Chenge2ba8972006-11-01 00:27:05 +0000298 assert(pos != std::string::npos && "Unrecognized constraint");
Chris Lattner0bb75002006-11-15 02:38:17 +0000299 std::string Name = CStr.substr(0, pos);
Evan Chenge2ba8972006-11-01 00:27:05 +0000300
Evan Cheng4c2b7a32006-11-01 23:03:11 +0000301 // TIED_TO: $src1 = $dst
Chris Lattner0bb75002006-11-15 02:38:17 +0000302 std::string::size_type wpos = Name.find_first_of(" \t");
303 if (wpos == std::string::npos)
304 throw "Illegal format for tied-to constraint: '" + CStr + "'";
305 std::string DestOpName = Name.substr(0, wpos);
306 std::pair<unsigned,unsigned> DestOp = I->ParseOperandName(DestOpName, false);
Evan Chenge2ba8972006-11-01 00:27:05 +0000307
308 Name = CStr.substr(pos+1);
Chris Lattner0bb75002006-11-15 02:38:17 +0000309 wpos = Name.find_first_not_of(" \t");
310 if (wpos == std::string::npos)
311 throw "Illegal format for tied-to constraint: '" + CStr + "'";
312
313 std::pair<unsigned,unsigned> SrcOp =
314 I->ParseOperandName(Name.substr(wpos), false);
315 if (SrcOp > DestOp)
Evan Cheng4c2b7a32006-11-01 23:03:11 +0000316 throw "Illegal tied-to operand constraint '" + CStr + "'";
Chris Lattnera0cca4a2006-11-06 23:49:51 +0000317
Chris Lattner0bb75002006-11-15 02:38:17 +0000318
319 unsigned FlatOpNo = I->getFlattenedOperandNumber(SrcOp);
320 // Build the string for the operand.
321 std::string OpConstraint =
Evan Cheng05551222006-12-01 22:57:41 +0000322 "((" + utostr(FlatOpNo) + " << 16) | (1 << TOI::TIED_TO))";
Chris Lattner0bb75002006-11-15 02:38:17 +0000323
324
325 if (!I->OperandList[DestOp.first].Constraints[DestOp.second].empty())
326 throw "Operand '" + DestOpName + "' cannot have multiple constraints!";
327 I->OperandList[DestOp.first].Constraints[DestOp.second] = OpConstraint;
Evan Chenge2ba8972006-11-01 00:27:05 +0000328}
329
Chris Lattnera0cca4a2006-11-06 23:49:51 +0000330static void ParseConstraints(const std::string &CStr, CodeGenInstruction *I) {
Chris Lattner0bb75002006-11-15 02:38:17 +0000331 // Make sure the constraints list for each operand is large enough to hold
332 // constraint info, even if none is present.
333 for (unsigned i = 0, e = I->OperandList.size(); i != e; ++i)
334 I->OperandList[i].Constraints.resize(I->OperandList[i].MINumOperands);
335
Chris Lattnera0cca4a2006-11-06 23:49:51 +0000336 if (CStr.empty()) return;
337
Evan Chenge2ba8972006-11-01 00:27:05 +0000338 const std::string delims(",");
339 std::string::size_type bidx, eidx;
340
341 bidx = CStr.find_first_not_of(delims);
342 while (bidx != std::string::npos) {
343 eidx = CStr.find_first_of(delims, bidx);
344 if (eidx == std::string::npos)
345 eidx = CStr.length();
Chris Lattnera0cca4a2006-11-06 23:49:51 +0000346
Chris Lattner0bb75002006-11-15 02:38:17 +0000347 ParseConstraint(CStr.substr(bidx, eidx), I);
Evan Chenge2ba8972006-11-01 00:27:05 +0000348 bidx = CStr.find_first_not_of(delims, eidx);
349 }
Evan Chenge2ba8972006-11-01 00:27:05 +0000350}
351
Chris Lattner175580c2004-08-14 22:50:53 +0000352CodeGenInstruction::CodeGenInstruction(Record *R, const std::string &AsmStr)
353 : TheDef(R), AsmString(AsmStr) {
Chris Lattnerec352402004-08-01 05:04:00 +0000354 Name = R->getValueAsString("Name");
355 Namespace = R->getValueAsString("Namespace");
Chris Lattnerec352402004-08-01 05:04:00 +0000356
Chris Lattnerec352402004-08-01 05:04:00 +0000357 isReturn = R->getValueAsBit("isReturn");
358 isBranch = R->getValueAsBit("isBranch");
359 isBarrier = R->getValueAsBit("isBarrier");
360 isCall = R->getValueAsBit("isCall");
Nate Begemancdd66b52004-09-28 21:01:45 +0000361 isLoad = R->getValueAsBit("isLoad");
362 isStore = R->getValueAsBit("isStore");
Chris Lattnerf64f9a42006-11-15 23:23:02 +0000363 bool isTwoAddress = R->getValueAsBit("isTwoAddress");
Evan Cheng5127ce02007-05-16 20:45:24 +0000364 isPredicable = R->getValueAsBit("isPredicable");
Chris Lattneraad75aa2005-01-02 02:29:04 +0000365 isConvertibleToThreeAddress = R->getValueAsBit("isConvertibleToThreeAddress");
366 isCommutable = R->getValueAsBit("isCommutable");
Chris Lattnerec352402004-08-01 05:04:00 +0000367 isTerminator = R->getValueAsBit("isTerminator");
Dan Gohmand45eddd2007-06-26 00:48:07 +0000368 isReMaterializable = R->getValueAsBit("isReMaterializable");
Chris Lattner5b71d3a2004-09-28 18:38:01 +0000369 hasDelaySlot = R->getValueAsBit("hasDelaySlot");
Chris Lattnere3cbf822005-08-26 20:55:40 +0000370 usesCustomDAGSchedInserter = R->getValueAsBit("usesCustomDAGSchedInserter");
Evan Cheng1c3d19e2005-12-04 08:18:16 +0000371 hasCtrlDep = R->getValueAsBit("hasCtrlDep");
Evan Chengeaa91b02007-06-19 01:26:51 +0000372 isNotDuplicable = R->getValueAsBit("isNotDuplicable");
Evan Cheng88cc0922007-07-10 18:05:01 +0000373 hasOptionalDef = false;
Chris Lattnercfbf96a2005-08-18 23:38:41 +0000374 hasVariableNumberOfOperands = false;
375
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000376 DagInit *DI;
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000377 try {
Evan Cheng64d80e32007-07-19 01:14:50 +0000378 DI = R->getValueAsDag("OutOperandList");
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000379 } catch (...) {
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000380 // Error getting operand list, just ignore it (sparcv9).
Chris Lattner87c59052004-08-01 07:42:39 +0000381 AsmString.clear();
382 OperandList.clear();
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000383 return;
384 }
Evan Cheng64d80e32007-07-19 01:14:50 +0000385 NumDefs = DI->getNumArgs();
386
387 DagInit *IDI;
388 try {
389 IDI = R->getValueAsDag("InOperandList");
390 } catch (...) {
391 // Error getting operand list, just ignore it (sparcv9).
392 AsmString.clear();
393 OperandList.clear();
394 return;
395 }
396 DI = (DagInit*)(new BinOpInit(BinOpInit::CONCAT, DI, IDI))->Fold();
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000397
398 unsigned MIOperandNo = 0;
399 std::set<std::string> OperandNames;
400 for (unsigned i = 0, e = DI->getNumArgs(); i != e; ++i) {
401 DefInit *Arg = dynamic_cast<DefInit*>(DI->getArg(i));
402 if (!Arg)
403 throw "Illegal operand for the '" + R->getName() + "' instruction!";
404
405 Record *Rec = Arg->getDef();
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000406 std::string PrintMethod = "printOperand";
407 unsigned NumOps = 1;
Chris Lattner33670792005-11-19 07:48:33 +0000408 DagInit *MIOpInfo = 0;
Nate Begeman86193d12005-12-01 00:12:04 +0000409 if (Rec->isSubClassOf("Operand")) {
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000410 PrintMethod = Rec->getValueAsString("PrintMethod");
Chris Lattner65303d62005-11-19 07:05:57 +0000411 MIOpInfo = Rec->getValueAsDag("MIOperandInfo");
Chris Lattnerd438b532006-11-03 23:45:17 +0000412
413 // Verify that MIOpInfo has an 'ops' root value.
414 if (!dynamic_cast<DefInit*>(MIOpInfo->getOperator()) ||
415 dynamic_cast<DefInit*>(MIOpInfo->getOperator())
416 ->getDef()->getName() != "ops")
417 throw "Bad value for MIOperandInfo in operand '" + Rec->getName() +
418 "'\n";
419
420 // If we have MIOpInfo, then we have #operands equal to number of entries
421 // in MIOperandInfo.
422 if (unsigned NumArgs = MIOpInfo->getNumArgs())
423 NumOps = NumArgs;
424
Evan Cheng88cc0922007-07-10 18:05:01 +0000425 if (Rec->isSubClassOf("PredicateOperand"))
Evan Chengc419bd32007-07-06 23:23:38 +0000426 isPredicable = true;
Evan Cheng88cc0922007-07-10 18:05:01 +0000427 else if (Rec->isSubClassOf("OptionalDefOperand"))
428 hasOptionalDef = true;
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000429 } else if (Rec->getName() == "variable_ops") {
430 hasVariableNumberOfOperands = true;
431 continue;
Chris Lattnerf1968392006-11-10 02:01:40 +0000432 } else if (!Rec->isSubClassOf("RegisterClass") &&
433 Rec->getName() != "ptr_rc")
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000434 throw "Unknown operand class '" + Rec->getName() +
435 "' in instruction '" + R->getName() + "' instruction!";
436
Chris Lattnerc4a8b732005-09-14 21:13:50 +0000437 // Check that the operand has a name and that it's unique.
438 if (DI->getArgName(i).empty())
439 throw "In instruction '" + R->getName() + "', operand #" + utostr(i) +
440 " has no name!";
441 if (!OperandNames.insert(DI->getArgName(i)).second)
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000442 throw "In instruction '" + R->getName() + "', operand #" + utostr(i) +
443 " has the same name as a previous operand!";
444
Nate Begeman86193d12005-12-01 00:12:04 +0000445 OperandList.push_back(OperandInfo(Rec, DI->getArgName(i), PrintMethod,
446 MIOperandNo, NumOps, MIOpInfo));
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000447 MIOperandNo += NumOps;
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000448 }
Evan Chenge2ba8972006-11-01 00:27:05 +0000449
Chris Lattnerf64f9a42006-11-15 23:23:02 +0000450 // Parse Constraints.
Chris Lattnera0cca4a2006-11-06 23:49:51 +0000451 ParseConstraints(R->getValueAsString("Constraints"), this);
452
453 // For backward compatibility: isTwoAddress means operand 1 is tied to
454 // operand 0.
Chris Lattner0bb75002006-11-15 02:38:17 +0000455 if (isTwoAddress) {
456 if (!OperandList[1].Constraints[0].empty())
457 throw R->getName() + ": cannot use isTwoAddress property: instruction "
458 "already has constraint set!";
Evan Cheng05551222006-12-01 22:57:41 +0000459 OperandList[1].Constraints[0] = "((0 << 16) | (1 << TOI::TIED_TO))";
Chris Lattner0bb75002006-11-15 02:38:17 +0000460 }
Chris Lattnera0cca4a2006-11-06 23:49:51 +0000461
462 // Any operands with unset constraints get 0 as their constraint.
463 for (unsigned op = 0, e = OperandList.size(); op != e; ++op)
Chris Lattner0bb75002006-11-15 02:38:17 +0000464 for (unsigned j = 0, e = OperandList[op].MINumOperands; j != e; ++j)
465 if (OperandList[op].Constraints[j].empty())
466 OperandList[op].Constraints[j] = "0";
Chris Lattnerf64f9a42006-11-15 23:23:02 +0000467
468 // Parse the DisableEncoding field.
469 std::string DisableEncoding = R->getValueAsString("DisableEncoding");
470 while (1) {
471 std::string OpName = getToken(DisableEncoding, " ,\t");
472 if (OpName.empty()) break;
473
474 // Figure out which operand this is.
475 std::pair<unsigned,unsigned> Op = ParseOperandName(OpName, false);
476
477 // Mark the operand as not-to-be encoded.
478 if (Op.second >= OperandList[Op.first].DoNotEncode.size())
479 OperandList[Op.first].DoNotEncode.resize(Op.second+1);
480 OperandList[Op.first].DoNotEncode[Op.second] = true;
481 }
Chris Lattnerec352402004-08-01 05:04:00 +0000482}
483
484
Chris Lattner87c59052004-08-01 07:42:39 +0000485
486/// getOperandNamed - Return the index of the operand with the specified
487/// non-empty name. If the instruction does not have an operand with the
488/// specified name, throw an exception.
Misha Brukman35e83cc2004-10-14 05:50:43 +0000489///
Chris Lattner87c59052004-08-01 07:42:39 +0000490unsigned CodeGenInstruction::getOperandNamed(const std::string &Name) const {
491 assert(!Name.empty() && "Cannot search for operand with no name!");
492 for (unsigned i = 0, e = OperandList.size(); i != e; ++i)
493 if (OperandList[i].Name == Name) return i;
494 throw "Instruction '" + TheDef->getName() +
495 "' does not have an operand named '$" + Name + "'!";
496}
Evan Cheng0fc71982005-12-08 02:00:36 +0000497
Chris Lattner0bb75002006-11-15 02:38:17 +0000498std::pair<unsigned,unsigned>
499CodeGenInstruction::ParseOperandName(const std::string &Op,
500 bool AllowWholeOp) {
501 if (Op.empty() || Op[0] != '$')
502 throw TheDef->getName() + ": Illegal operand name: '" + Op + "'";
503
504 std::string OpName = Op.substr(1);
505 std::string SubOpName;
506
507 // Check to see if this is $foo.bar.
508 std::string::size_type DotIdx = OpName.find_first_of(".");
509 if (DotIdx != std::string::npos) {
510 SubOpName = OpName.substr(DotIdx+1);
511 if (SubOpName.empty())
512 throw TheDef->getName() + ": illegal empty suboperand name in '" +Op +"'";
513 OpName = OpName.substr(0, DotIdx);
514 }
515
516 unsigned OpIdx = getOperandNamed(OpName);
517
518 if (SubOpName.empty()) { // If no suboperand name was specified:
519 // If one was needed, throw.
520 if (OperandList[OpIdx].MINumOperands > 1 && !AllowWholeOp &&
521 SubOpName.empty())
522 throw TheDef->getName() + ": Illegal to refer to"
523 " whole operand part of complex operand '" + Op + "'";
524
525 // Otherwise, return the operand.
526 return std::make_pair(OpIdx, 0U);
527 }
528
529 // Find the suboperand number involved.
530 DagInit *MIOpInfo = OperandList[OpIdx].MIOperandInfo;
531 if (MIOpInfo == 0)
532 throw TheDef->getName() + ": unknown suboperand name in '" + Op + "'";
533
534 // Find the operand with the right name.
535 for (unsigned i = 0, e = MIOpInfo->getNumArgs(); i != e; ++i)
536 if (MIOpInfo->getArgName(i) == SubOpName)
537 return std::make_pair(OpIdx, i);
538
539 // Otherwise, didn't find it!
540 throw TheDef->getName() + ": unknown suboperand name in '" + Op + "'";
541}
542
543
544
545
Evan Cheng0fc71982005-12-08 02:00:36 +0000546//===----------------------------------------------------------------------===//
547// ComplexPattern implementation
548//
549ComplexPattern::ComplexPattern(Record *R) {
Evan Cheng3aa39f42005-12-08 02:14:08 +0000550 Ty = ::getValueType(R->getValueAsDef("Ty"));
551 NumOperands = R->getValueAsInt("NumOperands");
552 SelectFunc = R->getValueAsString("SelectFunc");
553 RootNodes = R->getValueAsListOfDefs("RootNodes");
Evan Cheng94b30402006-10-11 21:02:01 +0000554
555 // Parse the properties.
556 Properties = 0;
557 std::vector<Record*> PropList = R->getValueAsListOfDefs("Properties");
558 for (unsigned i = 0, e = PropList.size(); i != e; ++i)
559 if (PropList[i]->getName() == "SDNPHasChain") {
560 Properties |= 1 << SDNPHasChain;
561 } else if (PropList[i]->getName() == "SDNPOptInFlag") {
562 Properties |= 1 << SDNPOptInFlag;
563 } else {
Bill Wendlingf5da1332006-12-07 22:21:48 +0000564 cerr << "Unsupported SD Node property '" << PropList[i]->getName()
565 << "' on ComplexPattern '" << R->getName() << "'!\n";
Evan Cheng94b30402006-10-11 21:02:01 +0000566 exit(1);
567 }
Evan Cheng0fc71982005-12-08 02:00:36 +0000568}
Evan Cheng3aa39f42005-12-08 02:14:08 +0000569
Chris Lattner43fbbc32006-03-24 19:49:31 +0000570//===----------------------------------------------------------------------===//
571// CodeGenIntrinsic Implementation
572//===----------------------------------------------------------------------===//
573
574std::vector<CodeGenIntrinsic> llvm::LoadIntrinsics(const RecordKeeper &RC) {
575 std::vector<Record*> I = RC.getAllDerivedDefinitions("Intrinsic");
Chris Lattner8850a1b2006-03-27 22:48:18 +0000576
577 std::vector<CodeGenIntrinsic> Result;
Chris Lattner6a160fb2006-03-28 00:15:00 +0000578
579 // If we are in the context of a target .td file, get the target info so that
580 // we can decode the current intptr_t.
581 CodeGenTarget *CGT = 0;
582 if (Records.getClass("Target") &&
583 Records.getAllDerivedDefinitions("Target").size() == 1)
584 CGT = new CodeGenTarget();
585
Chris Lattner8850a1b2006-03-27 22:48:18 +0000586 for (unsigned i = 0, e = I.size(); i != e; ++i)
Chris Lattner6a160fb2006-03-28 00:15:00 +0000587 Result.push_back(CodeGenIntrinsic(I[i], CGT));
588 delete CGT;
Chris Lattner8850a1b2006-03-27 22:48:18 +0000589 return Result;
Chris Lattner43fbbc32006-03-24 19:49:31 +0000590}
591
Chris Lattner76f8c7c2006-03-28 00:03:08 +0000592CodeGenIntrinsic::CodeGenIntrinsic(Record *R, CodeGenTarget *CGT) {
Chris Lattner2ca956f2006-03-24 20:25:01 +0000593 TheDef = R;
Chris Lattner43fbbc32006-03-24 19:49:31 +0000594 std::string DefName = R->getName();
595 ModRef = WriteMem;
Reid Spencerc4de3de2007-04-01 07:20:02 +0000596 isOverloaded = false;
Chris Lattner43fbbc32006-03-24 19:49:31 +0000597
598 if (DefName.size() <= 4 ||
599 std::string(DefName.begin(), DefName.begin()+4) != "int_")
600 throw "Intrinsic '" + DefName + "' does not start with 'int_'!";
601 EnumName = std::string(DefName.begin()+4, DefName.end());
602 if (R->getValue("GCCBuiltinName")) // Ignore a missing GCCBuiltinName field.
603 GCCBuiltinName = R->getValueAsString("GCCBuiltinName");
604 TargetPrefix = R->getValueAsString("TargetPrefix");
605 Name = R->getValueAsString("LLVMName");
606 if (Name == "") {
607 // If an explicit name isn't specified, derive one from the DefName.
608 Name = "llvm.";
609 for (unsigned i = 0, e = EnumName.size(); i != e; ++i)
610 if (EnumName[i] == '_')
611 Name += '.';
612 else
613 Name += EnumName[i];
614 } else {
615 // Verify it starts with "llvm.".
616 if (Name.size() <= 5 ||
617 std::string(Name.begin(), Name.begin()+5) != "llvm.")
618 throw "Intrinsic '" + DefName + "'s name does not start with 'llvm.'!";
619 }
620
621 // If TargetPrefix is specified, make sure that Name starts with
622 // "llvm.<targetprefix>.".
623 if (!TargetPrefix.empty()) {
624 if (Name.size() < 6+TargetPrefix.size() ||
625 std::string(Name.begin()+5, Name.begin()+6+TargetPrefix.size())
626 != (TargetPrefix+"."))
627 throw "Intrinsic '" + DefName + "' does not start with 'llvm." +
628 TargetPrefix + ".'!";
629 }
630
631 // Parse the list of argument types.
632 ListInit *TypeList = R->getValueAsListInit("Types");
633 for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) {
Chris Lattner50d45652007-02-27 22:08:27 +0000634 Record *TyEl = TypeList->getElementAsRecord(i);
Chris Lattner43fbbc32006-03-24 19:49:31 +0000635 assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!");
636 ArgTypes.push_back(TyEl->getValueAsString("TypeVal"));
Dan Gohman3bf6e182007-07-13 20:16:50 +0000637 MVT::ValueType VT = getValueType(TyEl->getValueAsDef("VT"));
Reid Spencerc4de3de2007-04-01 07:20:02 +0000638 isOverloaded |= VT == MVT::iAny;
639 ArgVTs.push_back(VT);
Chris Lattner43fbbc32006-03-24 19:49:31 +0000640 ArgTypeDefs.push_back(TyEl);
641 }
642 if (ArgTypes.size() == 0)
643 throw "Intrinsic '"+DefName+"' needs at least a type for the ret value!";
Reid Spencerc4de3de2007-04-01 07:20:02 +0000644
Chris Lattner43fbbc32006-03-24 19:49:31 +0000645
646 // Parse the intrinsic properties.
647 ListInit *PropList = R->getValueAsListInit("Properties");
648 for (unsigned i = 0, e = PropList->getSize(); i != e; ++i) {
Chris Lattner50d45652007-02-27 22:08:27 +0000649 Record *Property = PropList->getElementAsRecord(i);
Chris Lattner43fbbc32006-03-24 19:49:31 +0000650 assert(Property->isSubClassOf("IntrinsicProperty") &&
651 "Expected a property!");
652
Chris Lattner4b2362e2006-04-10 22:02:59 +0000653 if (Property->getName() == "IntrNoMem")
Chris Lattner43fbbc32006-03-24 19:49:31 +0000654 ModRef = NoMem;
Chris Lattner4b2362e2006-04-10 22:02:59 +0000655 else if (Property->getName() == "IntrReadArgMem")
Chris Lattner43fbbc32006-03-24 19:49:31 +0000656 ModRef = ReadArgMem;
657 else if (Property->getName() == "IntrReadMem")
658 ModRef = ReadMem;
Chris Lattner4b2362e2006-04-10 22:02:59 +0000659 else if (Property->getName() == "IntrWriteArgMem")
Chris Lattner43fbbc32006-03-24 19:49:31 +0000660 ModRef = WriteArgMem;
661 else if (Property->getName() == "IntrWriteMem")
662 ModRef = WriteMem;
663 else
664 assert(0 && "Unknown property!");
665 }
666}