blob: 21136c4645672b93d45b80e4a5a65bf8d0ffe2b8 [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";
Christopher Lamb82455102007-07-26 06:41:18 +000064 case MVT::v3i32: return "MVT::v3i32";
65 case MVT::v3f32: return "MVT::v3f32";
Evan Cheng6b125162006-05-17 20:55:51 +000066 case MVT::iPTR: return "TLI.getPointerTy()";
Chris Lattnerd3464c12003-08-07 23:15:21 +000067 default: assert(0 && "ILLEGAL VALUE TYPE!"); return "";
Chris Lattner45872072003-08-07 05:38:11 +000068 }
Chris Lattnerd3464c12003-08-07 23:15:21 +000069}
70
Chris Lattner2082ebe2004-08-01 03:55:39 +000071std::string llvm::getEnumName(MVT::ValueType T) {
Chris Lattnerb72fb7e2003-08-10 19:50:32 +000072 switch (T) {
Evan Cheng2618d072006-05-17 20:37:59 +000073 case MVT::Other: return "MVT::Other";
74 case MVT::i1: return "MVT::i1";
75 case MVT::i8: return "MVT::i8";
76 case MVT::i16: return "MVT::i16";
77 case MVT::i32: return "MVT::i32";
78 case MVT::i64: return "MVT::i64";
79 case MVT::i128: return "MVT::i128";
Reid Spencerc4de3de2007-04-01 07:20:02 +000080 case MVT::iAny: return "MVT::iAny";
Evan Cheng2618d072006-05-17 20:37:59 +000081 case MVT::f32: return "MVT::f32";
82 case MVT::f64: return "MVT::f64";
83 case MVT::f80: return "MVT::f80";
84 case MVT::f128: return "MVT::f128";
85 case MVT::Flag: return "MVT::Flag";
86 case MVT::isVoid:return "MVT::isVoid";
87 case MVT::v8i8: return "MVT::v8i8";
88 case MVT::v4i16: return "MVT::v4i16";
89 case MVT::v2i32: return "MVT::v2i32";
Bill Wendlingeebc8a12007-03-26 07:53:08 +000090 case MVT::v1i64: return "MVT::v1i64";
Evan Cheng2618d072006-05-17 20:37:59 +000091 case MVT::v16i8: return "MVT::v16i8";
92 case MVT::v8i16: return "MVT::v8i16";
93 case MVT::v4i32: return "MVT::v4i32";
94 case MVT::v2i64: return "MVT::v2i64";
95 case MVT::v2f32: return "MVT::v2f32";
96 case MVT::v4f32: return "MVT::v4f32";
97 case MVT::v2f64: return "MVT::v2f64";
Christopher Lamb82455102007-07-26 06:41:18 +000098 case MVT::v3i32: return "MVT::v3i32";
99 case MVT::v3f32: return "MVT::v3f32";
Evan Cheng6b125162006-05-17 20:55:51 +0000100 case MVT::iPTR: return "TLI.getPointerTy()";
Chris Lattnerb72fb7e2003-08-10 19:50:32 +0000101 default: assert(0 && "ILLEGAL VALUE TYPE!"); return "";
102 }
103}
104
Chris Lattnerd3464c12003-08-07 23:15:21 +0000105
Chris Lattner45872072003-08-07 05:38:11 +0000106/// getTarget - Return the current instance of the Target class.
107///
Evan Cheng2618d072006-05-17 20:37:59 +0000108CodeGenTarget::CodeGenTarget() {
Chris Lattner45872072003-08-07 05:38:11 +0000109 std::vector<Record*> Targets = Records.getAllDerivedDefinitions("Target");
Misha Brukmanbebdb202004-06-04 14:59:42 +0000110 if (Targets.size() == 0)
Misha Brukman3da94ae2005-04-22 00:00:37 +0000111 throw std::string("ERROR: No 'Target' subclasses defined!");
Chris Lattner45872072003-08-07 05:38:11 +0000112 if (Targets.size() != 1)
113 throw std::string("ERROR: Multiple subclasses of Target defined!");
114 TargetRec = Targets[0];
Chris Lattner45872072003-08-07 05:38:11 +0000115}
116
117
118const std::string &CodeGenTarget::getName() const {
119 return TargetRec->getName();
120}
121
122Record *CodeGenTarget::getInstructionSet() const {
123 return TargetRec->getValueAsDef("InstructionSet");
124}
Brian Gaeked0fde302003-11-11 22:41:34 +0000125
Chris Lattner175580c2004-08-14 22:50:53 +0000126/// getAsmWriter - Return the AssemblyWriter definition for this target.
127///
128Record *CodeGenTarget::getAsmWriter() const {
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000129 std::vector<Record*> LI = TargetRec->getValueAsListOfDefs("AssemblyWriters");
130 if (AsmWriterNum >= LI.size())
Chris Lattner560a79f2004-10-03 19:34:31 +0000131 throw "Target does not have an AsmWriter #" + utostr(AsmWriterNum) + "!";
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000132 return LI[AsmWriterNum];
Chris Lattner175580c2004-08-14 22:50:53 +0000133}
134
Chris Lattner26693112004-08-16 01:10:21 +0000135void CodeGenTarget::ReadRegisters() const {
136 std::vector<Record*> Regs = Records.getAllDerivedDefinitions("Register");
137 if (Regs.empty())
138 throw std::string("No 'Register' subclasses defined!");
139
140 Registers.reserve(Regs.size());
141 Registers.assign(Regs.begin(), Regs.end());
142}
143
Chris Lattner7a680c62004-08-21 02:24:57 +0000144CodeGenRegister::CodeGenRegister(Record *R) : TheDef(R) {
145 DeclaredSpillSize = R->getValueAsInt("SpillSize");
146 DeclaredSpillAlignment = R->getValueAsInt("SpillAlignment");
147}
148
Chris Lattner26693112004-08-16 01:10:21 +0000149const std::string &CodeGenRegister::getName() const {
150 return TheDef->getName();
151}
152
Chris Lattner056afef2004-08-21 04:05:00 +0000153void CodeGenTarget::ReadRegisterClasses() const {
154 std::vector<Record*> RegClasses =
155 Records.getAllDerivedDefinitions("RegisterClass");
156 if (RegClasses.empty())
157 throw std::string("No 'RegisterClass' subclasses defined!");
158
159 RegisterClasses.reserve(RegClasses.size());
160 RegisterClasses.assign(RegClasses.begin(), RegClasses.end());
161}
162
Evan Cheng44a65fa2006-05-16 07:05:30 +0000163std::vector<unsigned char> CodeGenTarget::getRegisterVTs(Record *R) const {
164 std::vector<unsigned char> Result;
165 const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses();
166 for (unsigned i = 0, e = RCs.size(); i != e; ++i) {
167 const CodeGenRegisterClass &RC = RegisterClasses[i];
168 for (unsigned ei = 0, ee = RC.Elements.size(); ei != ee; ++ei) {
169 if (R == RC.Elements[ei]) {
170 const std::vector<MVT::ValueType> &InVTs = RC.getValueTypes();
171 for (unsigned i = 0, e = InVTs.size(); i != e; ++i)
172 Result.push_back(InVTs[i]);
173 }
174 }
175 }
176 return Result;
177}
178
179
Chris Lattner056afef2004-08-21 04:05:00 +0000180CodeGenRegisterClass::CodeGenRegisterClass(Record *R) : TheDef(R) {
Chris Lattnerc67c18f2005-08-19 18:45:20 +0000181 // Rename anonymous register classes.
182 if (R->getName().size() > 9 && R->getName()[9] == '.') {
183 static unsigned AnonCounter = 0;
184 R->setName("AnonRegClass_"+utostr(AnonCounter++));
185 }
186
Nate Begeman6510b222005-12-01 04:51:06 +0000187 std::vector<Record*> TypeList = R->getValueAsListOfDefs("RegTypes");
188 for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
189 Record *Type = TypeList[i];
190 if (!Type->isSubClassOf("ValueType"))
191 throw "RegTypes list member '" + Type->getName() +
192 "' does not derive from the ValueType class!";
193 VTs.push_back(getValueType(Type));
194 }
195 assert(!VTs.empty() && "RegisterClass must contain at least one ValueType!");
Chris Lattnerac468932005-08-19 19:12:51 +0000196
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000197 std::vector<Record*> RegList = R->getValueAsListOfDefs("MemberList");
198 for (unsigned i = 0, e = RegList.size(); i != e; ++i) {
199 Record *Reg = RegList[i];
Chris Lattner056afef2004-08-21 04:05:00 +0000200 if (!Reg->isSubClassOf("Register"))
201 throw "Register Class member '" + Reg->getName() +
202 "' does not derive from the Register class!";
203 Elements.push_back(Reg);
204 }
Nate Begeman6510b222005-12-01 04:51:06 +0000205
Christopher Lamba3211252007-06-13 22:20:15 +0000206 std::vector<Record*> SubRegClassList =
207 R->getValueAsListOfDefs("SubRegClassList");
208 for (unsigned i = 0, e = SubRegClassList.size(); i != e; ++i) {
209 Record *SubRegClass = SubRegClassList[i];
210 if (!SubRegClass->isSubClassOf("RegisterClass"))
211 throw "Register Class member '" + SubRegClass->getName() +
212 "' does not derive from the RegisterClass class!";
213 SubRegClasses.push_back(SubRegClass);
214 }
215
Nate Begeman6510b222005-12-01 04:51:06 +0000216 // Allow targets to override the size in bits of the RegisterClass.
217 unsigned Size = R->getValueAsInt("Size");
218
219 Namespace = R->getValueAsString("Namespace");
220 SpillSize = Size ? Size : MVT::getSizeInBits(VTs[0]);
221 SpillAlignment = R->getValueAsInt("Alignment");
222 MethodBodies = R->getValueAsCode("MethodBodies");
223 MethodProtos = R->getValueAsCode("MethodProtos");
Chris Lattner056afef2004-08-21 04:05:00 +0000224}
225
226const std::string &CodeGenRegisterClass::getName() const {
227 return TheDef->getName();
228}
229
Chris Lattnere9f4ba82005-09-08 21:43:21 +0000230void CodeGenTarget::ReadLegalValueTypes() const {
231 const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses();
232 for (unsigned i = 0, e = RCs.size(); i != e; ++i)
Nate Begeman6510b222005-12-01 04:51:06 +0000233 for (unsigned ri = 0, re = RCs[i].VTs.size(); ri != re; ++ri)
234 LegalValueTypes.push_back(RCs[i].VTs[ri]);
Chris Lattner75ee2eb2005-10-14 03:54:49 +0000235
236 // Remove duplicates.
237 std::sort(LegalValueTypes.begin(), LegalValueTypes.end());
238 LegalValueTypes.erase(std::unique(LegalValueTypes.begin(),
239 LegalValueTypes.end()),
240 LegalValueTypes.end());
Chris Lattnere9f4ba82005-09-08 21:43:21 +0000241}
Chris Lattner056afef2004-08-21 04:05:00 +0000242
Chris Lattner175580c2004-08-14 22:50:53 +0000243
Chris Lattnerec352402004-08-01 05:04:00 +0000244void CodeGenTarget::ReadInstructions() const {
245 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
Chris Lattneraa77d772006-01-27 01:45:06 +0000246 if (Insts.size() <= 2)
Chris Lattnerec352402004-08-01 05:04:00 +0000247 throw std::string("No 'Instruction' subclasses defined!");
248
Chris Lattneraa77d772006-01-27 01:45:06 +0000249 // Parse the instructions defined in the .td file.
Chris Lattner175580c2004-08-14 22:50:53 +0000250 std::string InstFormatName =
251 getAsmWriter()->getValueAsString("InstFormatName");
252
253 for (unsigned i = 0, e = Insts.size(); i != e; ++i) {
254 std::string AsmStr = Insts[i]->getValueAsString(InstFormatName);
255 Instructions.insert(std::make_pair(Insts[i]->getName(),
256 CodeGenInstruction(Insts[i], AsmStr)));
257 }
Chris Lattnerec352402004-08-01 05:04:00 +0000258}
259
Chris Lattnerd6488672005-01-22 18:58:51 +0000260/// getInstructionsByEnumValue - Return all of the instructions defined by the
261/// target, ordered by their enum value.
262void CodeGenTarget::
263getInstructionsByEnumValue(std::vector<const CodeGenInstruction*>
264 &NumberedInstructions) {
Chris Lattneraa77d772006-01-27 01:45:06 +0000265 std::map<std::string, CodeGenInstruction>::const_iterator I;
266 I = getInstructions().find("PHI");
267 if (I == Instructions.end()) throw "Could not find 'PHI' instruction!";
268 const CodeGenInstruction *PHI = &I->second;
269
270 I = getInstructions().find("INLINEASM");
271 if (I == Instructions.end()) throw "Could not find 'INLINEASM' instruction!";
272 const CodeGenInstruction *INLINEASM = &I->second;
273
Jim Laskeya683f9b2007-01-26 17:29:20 +0000274 I = getInstructions().find("LABEL");
275 if (I == Instructions.end()) throw "Could not find 'LABEL' instruction!";
276 const CodeGenInstruction *LABEL = &I->second;
277
Christopher Lamb08d52072007-07-26 07:48:21 +0000278 I = getInstructions().find("EXTRACT_SUBREG");
279 if (I == Instructions.end())
280 throw "Could not find 'EXTRACT_SUBREG' instruction!";
281 const CodeGenInstruction *EXTRACT_SUBREG = &I->second;
282
283 I = getInstructions().find("INSERT_SUBREG");
284 if (I == Instructions.end())
285 throw "Could not find 'INSERT_SUBREG' instruction!";
286 const CodeGenInstruction *INSERT_SUBREG = &I->second;
287
Chris Lattnerd6488672005-01-22 18:58:51 +0000288 // Print out the rest of the instructions now.
Chris Lattnerd6488672005-01-22 18:58:51 +0000289 NumberedInstructions.push_back(PHI);
Chris Lattneraa77d772006-01-27 01:45:06 +0000290 NumberedInstructions.push_back(INLINEASM);
Jim Laskeya683f9b2007-01-26 17:29:20 +0000291 NumberedInstructions.push_back(LABEL);
Christopher Lamb08d52072007-07-26 07:48:21 +0000292 NumberedInstructions.push_back(EXTRACT_SUBREG);
293 NumberedInstructions.push_back(INSERT_SUBREG);
Chris Lattnerd6488672005-01-22 18:58:51 +0000294 for (inst_iterator II = inst_begin(), E = inst_end(); II != E; ++II)
Jim Laskeya683f9b2007-01-26 17:29:20 +0000295 if (&II->second != PHI &&
296 &II->second != INLINEASM &&
Christopher Lamb08d52072007-07-26 07:48:21 +0000297 &II->second != LABEL &&
298 &II->second != EXTRACT_SUBREG &&
299 &II->second != INSERT_SUBREG)
Chris Lattnerd6488672005-01-22 18:58:51 +0000300 NumberedInstructions.push_back(&II->second);
301}
302
303
Misha Brukman35e83cc2004-10-14 05:50:43 +0000304/// isLittleEndianEncoding - Return whether this target encodes its instruction
305/// in little-endian format, i.e. bits laid out in the order [0..n]
306///
307bool CodeGenTarget::isLittleEndianEncoding() const {
308 return getInstructionSet()->getValueAsBit("isLittleEndianEncoding");
309}
310
Chris Lattner0bb75002006-11-15 02:38:17 +0000311
312
313static void ParseConstraint(const std::string &CStr, CodeGenInstruction *I) {
314 // FIXME: Only supports TIED_TO for now.
315 std::string::size_type pos = CStr.find_first_of('=');
Evan Chenge2ba8972006-11-01 00:27:05 +0000316 assert(pos != std::string::npos && "Unrecognized constraint");
Chris Lattner0bb75002006-11-15 02:38:17 +0000317 std::string Name = CStr.substr(0, pos);
Evan Chenge2ba8972006-11-01 00:27:05 +0000318
Evan Cheng4c2b7a32006-11-01 23:03:11 +0000319 // TIED_TO: $src1 = $dst
Chris Lattner0bb75002006-11-15 02:38:17 +0000320 std::string::size_type wpos = Name.find_first_of(" \t");
321 if (wpos == std::string::npos)
322 throw "Illegal format for tied-to constraint: '" + CStr + "'";
323 std::string DestOpName = Name.substr(0, wpos);
324 std::pair<unsigned,unsigned> DestOp = I->ParseOperandName(DestOpName, false);
Evan Chenge2ba8972006-11-01 00:27:05 +0000325
326 Name = CStr.substr(pos+1);
Chris Lattner0bb75002006-11-15 02:38:17 +0000327 wpos = Name.find_first_not_of(" \t");
328 if (wpos == std::string::npos)
329 throw "Illegal format for tied-to constraint: '" + CStr + "'";
330
331 std::pair<unsigned,unsigned> SrcOp =
332 I->ParseOperandName(Name.substr(wpos), false);
333 if (SrcOp > DestOp)
Evan Cheng4c2b7a32006-11-01 23:03:11 +0000334 throw "Illegal tied-to operand constraint '" + CStr + "'";
Chris Lattnera0cca4a2006-11-06 23:49:51 +0000335
Chris Lattner0bb75002006-11-15 02:38:17 +0000336
337 unsigned FlatOpNo = I->getFlattenedOperandNumber(SrcOp);
338 // Build the string for the operand.
339 std::string OpConstraint =
Evan Cheng05551222006-12-01 22:57:41 +0000340 "((" + utostr(FlatOpNo) + " << 16) | (1 << TOI::TIED_TO))";
Chris Lattner0bb75002006-11-15 02:38:17 +0000341
342
343 if (!I->OperandList[DestOp.first].Constraints[DestOp.second].empty())
344 throw "Operand '" + DestOpName + "' cannot have multiple constraints!";
345 I->OperandList[DestOp.first].Constraints[DestOp.second] = OpConstraint;
Evan Chenge2ba8972006-11-01 00:27:05 +0000346}
347
Chris Lattnera0cca4a2006-11-06 23:49:51 +0000348static void ParseConstraints(const std::string &CStr, CodeGenInstruction *I) {
Chris Lattner0bb75002006-11-15 02:38:17 +0000349 // Make sure the constraints list for each operand is large enough to hold
350 // constraint info, even if none is present.
351 for (unsigned i = 0, e = I->OperandList.size(); i != e; ++i)
352 I->OperandList[i].Constraints.resize(I->OperandList[i].MINumOperands);
353
Chris Lattnera0cca4a2006-11-06 23:49:51 +0000354 if (CStr.empty()) return;
355
Evan Chenge2ba8972006-11-01 00:27:05 +0000356 const std::string delims(",");
357 std::string::size_type bidx, eidx;
358
359 bidx = CStr.find_first_not_of(delims);
360 while (bidx != std::string::npos) {
361 eidx = CStr.find_first_of(delims, bidx);
362 if (eidx == std::string::npos)
363 eidx = CStr.length();
Chris Lattnera0cca4a2006-11-06 23:49:51 +0000364
Chris Lattner0bb75002006-11-15 02:38:17 +0000365 ParseConstraint(CStr.substr(bidx, eidx), I);
Evan Chenge2ba8972006-11-01 00:27:05 +0000366 bidx = CStr.find_first_not_of(delims, eidx);
367 }
Evan Chenge2ba8972006-11-01 00:27:05 +0000368}
369
Chris Lattner175580c2004-08-14 22:50:53 +0000370CodeGenInstruction::CodeGenInstruction(Record *R, const std::string &AsmStr)
371 : TheDef(R), AsmString(AsmStr) {
Chris Lattnerec352402004-08-01 05:04:00 +0000372 Name = R->getValueAsString("Name");
373 Namespace = R->getValueAsString("Namespace");
Chris Lattnerec352402004-08-01 05:04:00 +0000374
Chris Lattnerec352402004-08-01 05:04:00 +0000375 isReturn = R->getValueAsBit("isReturn");
376 isBranch = R->getValueAsBit("isBranch");
377 isBarrier = R->getValueAsBit("isBarrier");
378 isCall = R->getValueAsBit("isCall");
Nate Begemancdd66b52004-09-28 21:01:45 +0000379 isLoad = R->getValueAsBit("isLoad");
380 isStore = R->getValueAsBit("isStore");
Chris Lattnerf64f9a42006-11-15 23:23:02 +0000381 bool isTwoAddress = R->getValueAsBit("isTwoAddress");
Evan Cheng5127ce02007-05-16 20:45:24 +0000382 isPredicable = R->getValueAsBit("isPredicable");
Chris Lattneraad75aa2005-01-02 02:29:04 +0000383 isConvertibleToThreeAddress = R->getValueAsBit("isConvertibleToThreeAddress");
384 isCommutable = R->getValueAsBit("isCommutable");
Chris Lattnerec352402004-08-01 05:04:00 +0000385 isTerminator = R->getValueAsBit("isTerminator");
Dan Gohmand45eddd2007-06-26 00:48:07 +0000386 isReMaterializable = R->getValueAsBit("isReMaterializable");
Chris Lattner5b71d3a2004-09-28 18:38:01 +0000387 hasDelaySlot = R->getValueAsBit("hasDelaySlot");
Chris Lattnere3cbf822005-08-26 20:55:40 +0000388 usesCustomDAGSchedInserter = R->getValueAsBit("usesCustomDAGSchedInserter");
Evan Cheng1c3d19e2005-12-04 08:18:16 +0000389 hasCtrlDep = R->getValueAsBit("hasCtrlDep");
Evan Chengeaa91b02007-06-19 01:26:51 +0000390 isNotDuplicable = R->getValueAsBit("isNotDuplicable");
Evan Cheng88cc0922007-07-10 18:05:01 +0000391 hasOptionalDef = false;
Chris Lattnercfbf96a2005-08-18 23:38:41 +0000392 hasVariableNumberOfOperands = false;
393
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000394 DagInit *DI;
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000395 try {
Evan Cheng64d80e32007-07-19 01:14:50 +0000396 DI = R->getValueAsDag("OutOperandList");
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000397 } catch (...) {
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000398 // Error getting operand list, just ignore it (sparcv9).
Chris Lattner87c59052004-08-01 07:42:39 +0000399 AsmString.clear();
400 OperandList.clear();
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000401 return;
402 }
Evan Cheng64d80e32007-07-19 01:14:50 +0000403 NumDefs = DI->getNumArgs();
404
405 DagInit *IDI;
406 try {
407 IDI = R->getValueAsDag("InOperandList");
408 } catch (...) {
409 // Error getting operand list, just ignore it (sparcv9).
410 AsmString.clear();
411 OperandList.clear();
412 return;
413 }
414 DI = (DagInit*)(new BinOpInit(BinOpInit::CONCAT, DI, IDI))->Fold();
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000415
416 unsigned MIOperandNo = 0;
417 std::set<std::string> OperandNames;
418 for (unsigned i = 0, e = DI->getNumArgs(); i != e; ++i) {
419 DefInit *Arg = dynamic_cast<DefInit*>(DI->getArg(i));
420 if (!Arg)
421 throw "Illegal operand for the '" + R->getName() + "' instruction!";
422
423 Record *Rec = Arg->getDef();
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000424 std::string PrintMethod = "printOperand";
425 unsigned NumOps = 1;
Chris Lattner33670792005-11-19 07:48:33 +0000426 DagInit *MIOpInfo = 0;
Nate Begeman86193d12005-12-01 00:12:04 +0000427 if (Rec->isSubClassOf("Operand")) {
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000428 PrintMethod = Rec->getValueAsString("PrintMethod");
Chris Lattner65303d62005-11-19 07:05:57 +0000429 MIOpInfo = Rec->getValueAsDag("MIOperandInfo");
Chris Lattnerd438b532006-11-03 23:45:17 +0000430
431 // Verify that MIOpInfo has an 'ops' root value.
432 if (!dynamic_cast<DefInit*>(MIOpInfo->getOperator()) ||
433 dynamic_cast<DefInit*>(MIOpInfo->getOperator())
434 ->getDef()->getName() != "ops")
435 throw "Bad value for MIOperandInfo in operand '" + Rec->getName() +
436 "'\n";
437
438 // If we have MIOpInfo, then we have #operands equal to number of entries
439 // in MIOperandInfo.
440 if (unsigned NumArgs = MIOpInfo->getNumArgs())
441 NumOps = NumArgs;
442
Evan Cheng88cc0922007-07-10 18:05:01 +0000443 if (Rec->isSubClassOf("PredicateOperand"))
Evan Chengc419bd32007-07-06 23:23:38 +0000444 isPredicable = true;
Evan Cheng88cc0922007-07-10 18:05:01 +0000445 else if (Rec->isSubClassOf("OptionalDefOperand"))
446 hasOptionalDef = true;
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000447 } else if (Rec->getName() == "variable_ops") {
448 hasVariableNumberOfOperands = true;
449 continue;
Chris Lattnerf1968392006-11-10 02:01:40 +0000450 } else if (!Rec->isSubClassOf("RegisterClass") &&
451 Rec->getName() != "ptr_rc")
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000452 throw "Unknown operand class '" + Rec->getName() +
453 "' in instruction '" + R->getName() + "' instruction!";
454
Chris Lattnerc4a8b732005-09-14 21:13:50 +0000455 // Check that the operand has a name and that it's unique.
456 if (DI->getArgName(i).empty())
457 throw "In instruction '" + R->getName() + "', operand #" + utostr(i) +
458 " has no name!";
459 if (!OperandNames.insert(DI->getArgName(i)).second)
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000460 throw "In instruction '" + R->getName() + "', operand #" + utostr(i) +
461 " has the same name as a previous operand!";
462
Nate Begeman86193d12005-12-01 00:12:04 +0000463 OperandList.push_back(OperandInfo(Rec, DI->getArgName(i), PrintMethod,
464 MIOperandNo, NumOps, MIOpInfo));
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000465 MIOperandNo += NumOps;
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000466 }
Evan Chenge2ba8972006-11-01 00:27:05 +0000467
Chris Lattnerf64f9a42006-11-15 23:23:02 +0000468 // Parse Constraints.
Chris Lattnera0cca4a2006-11-06 23:49:51 +0000469 ParseConstraints(R->getValueAsString("Constraints"), this);
470
471 // For backward compatibility: isTwoAddress means operand 1 is tied to
472 // operand 0.
Chris Lattner0bb75002006-11-15 02:38:17 +0000473 if (isTwoAddress) {
474 if (!OperandList[1].Constraints[0].empty())
475 throw R->getName() + ": cannot use isTwoAddress property: instruction "
476 "already has constraint set!";
Evan Cheng05551222006-12-01 22:57:41 +0000477 OperandList[1].Constraints[0] = "((0 << 16) | (1 << TOI::TIED_TO))";
Chris Lattner0bb75002006-11-15 02:38:17 +0000478 }
Chris Lattnera0cca4a2006-11-06 23:49:51 +0000479
480 // Any operands with unset constraints get 0 as their constraint.
481 for (unsigned op = 0, e = OperandList.size(); op != e; ++op)
Chris Lattner0bb75002006-11-15 02:38:17 +0000482 for (unsigned j = 0, e = OperandList[op].MINumOperands; j != e; ++j)
483 if (OperandList[op].Constraints[j].empty())
484 OperandList[op].Constraints[j] = "0";
Chris Lattnerf64f9a42006-11-15 23:23:02 +0000485
486 // Parse the DisableEncoding field.
487 std::string DisableEncoding = R->getValueAsString("DisableEncoding");
488 while (1) {
489 std::string OpName = getToken(DisableEncoding, " ,\t");
490 if (OpName.empty()) break;
491
492 // Figure out which operand this is.
493 std::pair<unsigned,unsigned> Op = ParseOperandName(OpName, false);
494
495 // Mark the operand as not-to-be encoded.
496 if (Op.second >= OperandList[Op.first].DoNotEncode.size())
497 OperandList[Op.first].DoNotEncode.resize(Op.second+1);
498 OperandList[Op.first].DoNotEncode[Op.second] = true;
499 }
Chris Lattnerec352402004-08-01 05:04:00 +0000500}
501
502
Chris Lattner87c59052004-08-01 07:42:39 +0000503
504/// getOperandNamed - Return the index of the operand with the specified
505/// non-empty name. If the instruction does not have an operand with the
506/// specified name, throw an exception.
Misha Brukman35e83cc2004-10-14 05:50:43 +0000507///
Chris Lattner87c59052004-08-01 07:42:39 +0000508unsigned CodeGenInstruction::getOperandNamed(const std::string &Name) const {
509 assert(!Name.empty() && "Cannot search for operand with no name!");
510 for (unsigned i = 0, e = OperandList.size(); i != e; ++i)
511 if (OperandList[i].Name == Name) return i;
512 throw "Instruction '" + TheDef->getName() +
513 "' does not have an operand named '$" + Name + "'!";
514}
Evan Cheng0fc71982005-12-08 02:00:36 +0000515
Chris Lattner0bb75002006-11-15 02:38:17 +0000516std::pair<unsigned,unsigned>
517CodeGenInstruction::ParseOperandName(const std::string &Op,
518 bool AllowWholeOp) {
519 if (Op.empty() || Op[0] != '$')
520 throw TheDef->getName() + ": Illegal operand name: '" + Op + "'";
521
522 std::string OpName = Op.substr(1);
523 std::string SubOpName;
524
525 // Check to see if this is $foo.bar.
526 std::string::size_type DotIdx = OpName.find_first_of(".");
527 if (DotIdx != std::string::npos) {
528 SubOpName = OpName.substr(DotIdx+1);
529 if (SubOpName.empty())
530 throw TheDef->getName() + ": illegal empty suboperand name in '" +Op +"'";
531 OpName = OpName.substr(0, DotIdx);
532 }
533
534 unsigned OpIdx = getOperandNamed(OpName);
535
536 if (SubOpName.empty()) { // If no suboperand name was specified:
537 // If one was needed, throw.
538 if (OperandList[OpIdx].MINumOperands > 1 && !AllowWholeOp &&
539 SubOpName.empty())
540 throw TheDef->getName() + ": Illegal to refer to"
541 " whole operand part of complex operand '" + Op + "'";
542
543 // Otherwise, return the operand.
544 return std::make_pair(OpIdx, 0U);
545 }
546
547 // Find the suboperand number involved.
548 DagInit *MIOpInfo = OperandList[OpIdx].MIOperandInfo;
549 if (MIOpInfo == 0)
550 throw TheDef->getName() + ": unknown suboperand name in '" + Op + "'";
551
552 // Find the operand with the right name.
553 for (unsigned i = 0, e = MIOpInfo->getNumArgs(); i != e; ++i)
554 if (MIOpInfo->getArgName(i) == SubOpName)
555 return std::make_pair(OpIdx, i);
556
557 // Otherwise, didn't find it!
558 throw TheDef->getName() + ": unknown suboperand name in '" + Op + "'";
559}
560
561
562
563
Evan Cheng0fc71982005-12-08 02:00:36 +0000564//===----------------------------------------------------------------------===//
565// ComplexPattern implementation
566//
567ComplexPattern::ComplexPattern(Record *R) {
Evan Cheng3aa39f42005-12-08 02:14:08 +0000568 Ty = ::getValueType(R->getValueAsDef("Ty"));
569 NumOperands = R->getValueAsInt("NumOperands");
570 SelectFunc = R->getValueAsString("SelectFunc");
571 RootNodes = R->getValueAsListOfDefs("RootNodes");
Evan Cheng94b30402006-10-11 21:02:01 +0000572
573 // Parse the properties.
574 Properties = 0;
575 std::vector<Record*> PropList = R->getValueAsListOfDefs("Properties");
576 for (unsigned i = 0, e = PropList.size(); i != e; ++i)
577 if (PropList[i]->getName() == "SDNPHasChain") {
578 Properties |= 1 << SDNPHasChain;
579 } else if (PropList[i]->getName() == "SDNPOptInFlag") {
580 Properties |= 1 << SDNPOptInFlag;
581 } else {
Bill Wendlingf5da1332006-12-07 22:21:48 +0000582 cerr << "Unsupported SD Node property '" << PropList[i]->getName()
583 << "' on ComplexPattern '" << R->getName() << "'!\n";
Evan Cheng94b30402006-10-11 21:02:01 +0000584 exit(1);
585 }
Evan Cheng0fc71982005-12-08 02:00:36 +0000586}
Evan Cheng3aa39f42005-12-08 02:14:08 +0000587
Chris Lattner43fbbc32006-03-24 19:49:31 +0000588//===----------------------------------------------------------------------===//
589// CodeGenIntrinsic Implementation
590//===----------------------------------------------------------------------===//
591
592std::vector<CodeGenIntrinsic> llvm::LoadIntrinsics(const RecordKeeper &RC) {
593 std::vector<Record*> I = RC.getAllDerivedDefinitions("Intrinsic");
Chris Lattner8850a1b2006-03-27 22:48:18 +0000594
595 std::vector<CodeGenIntrinsic> Result;
Chris Lattner6a160fb2006-03-28 00:15:00 +0000596
597 // If we are in the context of a target .td file, get the target info so that
598 // we can decode the current intptr_t.
599 CodeGenTarget *CGT = 0;
600 if (Records.getClass("Target") &&
601 Records.getAllDerivedDefinitions("Target").size() == 1)
602 CGT = new CodeGenTarget();
603
Chris Lattner8850a1b2006-03-27 22:48:18 +0000604 for (unsigned i = 0, e = I.size(); i != e; ++i)
Chris Lattner6a160fb2006-03-28 00:15:00 +0000605 Result.push_back(CodeGenIntrinsic(I[i], CGT));
606 delete CGT;
Chris Lattner8850a1b2006-03-27 22:48:18 +0000607 return Result;
Chris Lattner43fbbc32006-03-24 19:49:31 +0000608}
609
Chris Lattner76f8c7c2006-03-28 00:03:08 +0000610CodeGenIntrinsic::CodeGenIntrinsic(Record *R, CodeGenTarget *CGT) {
Chris Lattner2ca956f2006-03-24 20:25:01 +0000611 TheDef = R;
Chris Lattner43fbbc32006-03-24 19:49:31 +0000612 std::string DefName = R->getName();
613 ModRef = WriteMem;
Reid Spencerc4de3de2007-04-01 07:20:02 +0000614 isOverloaded = false;
Chris Lattner43fbbc32006-03-24 19:49:31 +0000615
616 if (DefName.size() <= 4 ||
617 std::string(DefName.begin(), DefName.begin()+4) != "int_")
618 throw "Intrinsic '" + DefName + "' does not start with 'int_'!";
619 EnumName = std::string(DefName.begin()+4, DefName.end());
620 if (R->getValue("GCCBuiltinName")) // Ignore a missing GCCBuiltinName field.
621 GCCBuiltinName = R->getValueAsString("GCCBuiltinName");
622 TargetPrefix = R->getValueAsString("TargetPrefix");
623 Name = R->getValueAsString("LLVMName");
624 if (Name == "") {
625 // If an explicit name isn't specified, derive one from the DefName.
626 Name = "llvm.";
627 for (unsigned i = 0, e = EnumName.size(); i != e; ++i)
628 if (EnumName[i] == '_')
629 Name += '.';
630 else
631 Name += EnumName[i];
632 } else {
633 // Verify it starts with "llvm.".
634 if (Name.size() <= 5 ||
635 std::string(Name.begin(), Name.begin()+5) != "llvm.")
636 throw "Intrinsic '" + DefName + "'s name does not start with 'llvm.'!";
637 }
638
639 // If TargetPrefix is specified, make sure that Name starts with
640 // "llvm.<targetprefix>.".
641 if (!TargetPrefix.empty()) {
642 if (Name.size() < 6+TargetPrefix.size() ||
643 std::string(Name.begin()+5, Name.begin()+6+TargetPrefix.size())
644 != (TargetPrefix+"."))
645 throw "Intrinsic '" + DefName + "' does not start with 'llvm." +
646 TargetPrefix + ".'!";
647 }
648
649 // Parse the list of argument types.
650 ListInit *TypeList = R->getValueAsListInit("Types");
651 for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) {
Chris Lattner50d45652007-02-27 22:08:27 +0000652 Record *TyEl = TypeList->getElementAsRecord(i);
Chris Lattner43fbbc32006-03-24 19:49:31 +0000653 assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!");
654 ArgTypes.push_back(TyEl->getValueAsString("TypeVal"));
Dan Gohman3bf6e182007-07-13 20:16:50 +0000655 MVT::ValueType VT = getValueType(TyEl->getValueAsDef("VT"));
Reid Spencerc4de3de2007-04-01 07:20:02 +0000656 isOverloaded |= VT == MVT::iAny;
657 ArgVTs.push_back(VT);
Chris Lattner43fbbc32006-03-24 19:49:31 +0000658 ArgTypeDefs.push_back(TyEl);
659 }
660 if (ArgTypes.size() == 0)
661 throw "Intrinsic '"+DefName+"' needs at least a type for the ret value!";
Reid Spencerc4de3de2007-04-01 07:20:02 +0000662
Chris Lattner43fbbc32006-03-24 19:49:31 +0000663
664 // Parse the intrinsic properties.
665 ListInit *PropList = R->getValueAsListInit("Properties");
666 for (unsigned i = 0, e = PropList->getSize(); i != e; ++i) {
Chris Lattner50d45652007-02-27 22:08:27 +0000667 Record *Property = PropList->getElementAsRecord(i);
Chris Lattner43fbbc32006-03-24 19:49:31 +0000668 assert(Property->isSubClassOf("IntrinsicProperty") &&
669 "Expected a property!");
670
Chris Lattner4b2362e2006-04-10 22:02:59 +0000671 if (Property->getName() == "IntrNoMem")
Chris Lattner43fbbc32006-03-24 19:49:31 +0000672 ModRef = NoMem;
Chris Lattner4b2362e2006-04-10 22:02:59 +0000673 else if (Property->getName() == "IntrReadArgMem")
Chris Lattner43fbbc32006-03-24 19:49:31 +0000674 ModRef = ReadArgMem;
675 else if (Property->getName() == "IntrReadMem")
676 ModRef = ReadMem;
Chris Lattner4b2362e2006-04-10 22:02:59 +0000677 else if (Property->getName() == "IntrWriteArgMem")
Chris Lattner43fbbc32006-03-24 19:49:31 +0000678 ModRef = WriteArgMem;
679 else if (Property->getName() == "IntrWriteMem")
680 ModRef = WriteMem;
681 else
682 assert(0 && "Unknown property!");
683 }
684}