blob: 15aa2de1d58c8265c4e860ba22f7e0a16cdf8701 [file] [log] [blame]
Chris Lattner4a7bcdc2008-01-06 01:35:39 +00001//===- CodeGenTarget.cpp - CodeGen Target Class Wrapper -------------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerfd6c2f02007-12-29 20:37:13 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner4a7bcdc2008-01-06 01:35:39 +000010// This class wraps target description classes used by the various code
Dan Gohmanf17a25c2007-07-18 16:29:46 +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
17#include "CodeGenTarget.h"
18#include "CodeGenIntrinsics.h"
19#include "Record.h"
20#include "llvm/ADT/StringExtras.h"
21#include "llvm/Support/CommandLine.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000022#include <algorithm>
23using namespace llvm;
24
25static cl::opt<unsigned>
Daniel Dunbar85f1b392009-07-29 00:02:19 +000026AsmParserNum("asmparsernum", cl::init(0),
27 cl::desc("Make -gen-asm-parser emit assembly parser #N"));
28
29static cl::opt<unsigned>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030AsmWriterNum("asmwriternum", cl::init(0),
31 cl::desc("Make -gen-asm-writer emit assembly writer #N"));
32
Owen Andersonac9de032009-08-10 22:56:29 +000033/// getValueType - Return the EVT::SimpleValueType that the specified TableGen
Duncan Sands92c43912008-06-06 12:08:01 +000034/// record corresponds to.
Owen Andersonac9de032009-08-10 22:56:29 +000035EVT::SimpleValueType llvm::getValueType(Record *Rec) {
36 return (EVT::SimpleValueType)Rec->getValueAsInt("Value");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000037}
38
Owen Andersonac9de032009-08-10 22:56:29 +000039std::string llvm::getName(EVT::SimpleValueType T) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000040 switch (T) {
Owen Andersonac9de032009-08-10 22:56:29 +000041 case EVT::Other: return "UNKNOWN";
42 case EVT::iPTR: return "TLI.getPointerTy()";
43 case EVT::iPTRAny: return "TLI.getPointerTy()";
Bob Wilsonb6b793c2009-07-10 22:25:24 +000044 default: return getEnumName(T);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000045 }
46}
47
Owen Andersonac9de032009-08-10 22:56:29 +000048std::string llvm::getEnumName(EVT::SimpleValueType T) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000049 switch (T) {
Owen Andersonac9de032009-08-10 22:56:29 +000050 case EVT::Other: return "EVT::Other";
51 case EVT::i1: return "EVT::i1";
52 case EVT::i8: return "EVT::i8";
53 case EVT::i16: return "EVT::i16";
54 case EVT::i32: return "EVT::i32";
55 case EVT::i64: return "EVT::i64";
56 case EVT::i128: return "EVT::i128";
57 case EVT::iAny: return "EVT::iAny";
58 case EVT::fAny: return "EVT::fAny";
59 case EVT::f32: return "EVT::f32";
60 case EVT::f64: return "EVT::f64";
61 case EVT::f80: return "EVT::f80";
62 case EVT::f128: return "EVT::f128";
63 case EVT::ppcf128: return "EVT::ppcf128";
64 case EVT::Flag: return "EVT::Flag";
65 case EVT::isVoid:return "EVT::isVoid";
66 case EVT::v2i8: return "EVT::v2i8";
67 case EVT::v4i8: return "EVT::v4i8";
68 case EVT::v8i8: return "EVT::v8i8";
69 case EVT::v16i8: return "EVT::v16i8";
70 case EVT::v32i8: return "EVT::v32i8";
71 case EVT::v2i16: return "EVT::v2i16";
72 case EVT::v4i16: return "EVT::v4i16";
73 case EVT::v8i16: return "EVT::v8i16";
74 case EVT::v16i16: return "EVT::v16i16";
75 case EVT::v2i32: return "EVT::v2i32";
76 case EVT::v4i32: return "EVT::v4i32";
77 case EVT::v8i32: return "EVT::v8i32";
78 case EVT::v1i64: return "EVT::v1i64";
79 case EVT::v2i64: return "EVT::v2i64";
80 case EVT::v4i64: return "EVT::v4i64";
81 case EVT::v2f32: return "EVT::v2f32";
82 case EVT::v4f32: return "EVT::v4f32";
83 case EVT::v8f32: return "EVT::v8f32";
84 case EVT::v2f64: return "EVT::v2f64";
85 case EVT::v4f64: return "EVT::v4f64";
86 case EVT::Metadata: return "EVT::Metadata";
87 case EVT::iPTR: return "EVT::iPTR";
88 case EVT::iPTRAny: return "EVT::iPTRAny";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000089 default: assert(0 && "ILLEGAL VALUE TYPE!"); return "";
90 }
91}
92
Chris Lattner4ca8ff02008-01-05 22:25:12 +000093/// getQualifiedName - Return the name of the specified record, with a
94/// namespace qualifier if the record contains one.
95///
96std::string llvm::getQualifiedName(const Record *R) {
97 std::string Namespace = R->getValueAsString("Namespace");
98 if (Namespace.empty()) return R->getName();
99 return Namespace + "::" + R->getName();
100}
101
102
103
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000104
105/// getTarget - Return the current instance of the Target class.
106///
107CodeGenTarget::CodeGenTarget() {
108 std::vector<Record*> Targets = Records.getAllDerivedDefinitions("Target");
109 if (Targets.size() == 0)
110 throw std::string("ERROR: No 'Target' subclasses defined!");
111 if (Targets.size() != 1)
112 throw std::string("ERROR: Multiple subclasses of Target defined!");
113 TargetRec = Targets[0];
114}
115
116
117const std::string &CodeGenTarget::getName() const {
118 return TargetRec->getName();
119}
120
Dan Gohman6a36cc92008-08-20 21:45:57 +0000121std::string CodeGenTarget::getInstNamespace() const {
122 std::string InstNS;
123
124 for (inst_iterator i = inst_begin(), e = inst_end(); i != e; ++i) {
125 InstNS = i->second.Namespace;
126
127 // Make sure not to pick up "TargetInstrInfo" by accidentally getting
128 // the namespace off the PHI instruction or something.
129 if (InstNS != "TargetInstrInfo")
130 break;
131 }
132
133 return InstNS;
134}
135
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000136Record *CodeGenTarget::getInstructionSet() const {
137 return TargetRec->getValueAsDef("InstructionSet");
138}
139
Daniel Dunbar85f1b392009-07-29 00:02:19 +0000140/// getAsmParser - Return the AssemblyParser definition for this target.
141///
142Record *CodeGenTarget::getAsmParser() const {
143 std::vector<Record*> LI = TargetRec->getValueAsListOfDefs("AssemblyParsers");
144 if (AsmParserNum >= LI.size())
145 throw "Target does not have an AsmParser #" + utostr(AsmParserNum) + "!";
146 return LI[AsmParserNum];
147}
148
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000149/// getAsmWriter - Return the AssemblyWriter definition for this target.
150///
151Record *CodeGenTarget::getAsmWriter() const {
152 std::vector<Record*> LI = TargetRec->getValueAsListOfDefs("AssemblyWriters");
153 if (AsmWriterNum >= LI.size())
154 throw "Target does not have an AsmWriter #" + utostr(AsmWriterNum) + "!";
155 return LI[AsmWriterNum];
156}
157
158void CodeGenTarget::ReadRegisters() const {
159 std::vector<Record*> Regs = Records.getAllDerivedDefinitions("Register");
160 if (Regs.empty())
161 throw std::string("No 'Register' subclasses defined!");
162
163 Registers.reserve(Regs.size());
164 Registers.assign(Regs.begin(), Regs.end());
165}
166
167CodeGenRegister::CodeGenRegister(Record *R) : TheDef(R) {
168 DeclaredSpillSize = R->getValueAsInt("SpillSize");
169 DeclaredSpillAlignment = R->getValueAsInt("SpillAlignment");
170}
171
172const std::string &CodeGenRegister::getName() const {
173 return TheDef->getName();
174}
175
176void CodeGenTarget::ReadRegisterClasses() const {
177 std::vector<Record*> RegClasses =
178 Records.getAllDerivedDefinitions("RegisterClass");
179 if (RegClasses.empty())
180 throw std::string("No 'RegisterClass' subclasses defined!");
181
182 RegisterClasses.reserve(RegClasses.size());
183 RegisterClasses.assign(RegClasses.begin(), RegClasses.end());
184}
185
186std::vector<unsigned char> CodeGenTarget::getRegisterVTs(Record *R) const {
187 std::vector<unsigned char> Result;
188 const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses();
189 for (unsigned i = 0, e = RCs.size(); i != e; ++i) {
190 const CodeGenRegisterClass &RC = RegisterClasses[i];
191 for (unsigned ei = 0, ee = RC.Elements.size(); ei != ee; ++ei) {
192 if (R == RC.Elements[ei]) {
Owen Andersonac9de032009-08-10 22:56:29 +0000193 const std::vector<EVT::SimpleValueType> &InVTs = RC.getValueTypes();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000194 for (unsigned i = 0, e = InVTs.size(); i != e; ++i)
195 Result.push_back(InVTs[i]);
196 }
197 }
198 }
199 return Result;
200}
201
202
203CodeGenRegisterClass::CodeGenRegisterClass(Record *R) : TheDef(R) {
204 // Rename anonymous register classes.
205 if (R->getName().size() > 9 && R->getName()[9] == '.') {
206 static unsigned AnonCounter = 0;
207 R->setName("AnonRegClass_"+utostr(AnonCounter++));
208 }
209
210 std::vector<Record*> TypeList = R->getValueAsListOfDefs("RegTypes");
211 for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
212 Record *Type = TypeList[i];
213 if (!Type->isSubClassOf("ValueType"))
214 throw "RegTypes list member '" + Type->getName() +
215 "' does not derive from the ValueType class!";
216 VTs.push_back(getValueType(Type));
217 }
218 assert(!VTs.empty() && "RegisterClass must contain at least one ValueType!");
219
220 std::vector<Record*> RegList = R->getValueAsListOfDefs("MemberList");
221 for (unsigned i = 0, e = RegList.size(); i != e; ++i) {
222 Record *Reg = RegList[i];
223 if (!Reg->isSubClassOf("Register"))
224 throw "Register Class member '" + Reg->getName() +
225 "' does not derive from the Register class!";
226 Elements.push_back(Reg);
227 }
228
229 std::vector<Record*> SubRegClassList =
230 R->getValueAsListOfDefs("SubRegClassList");
231 for (unsigned i = 0, e = SubRegClassList.size(); i != e; ++i) {
232 Record *SubRegClass = SubRegClassList[i];
233 if (!SubRegClass->isSubClassOf("RegisterClass"))
234 throw "Register Class member '" + SubRegClass->getName() +
235 "' does not derive from the RegisterClass class!";
236 SubRegClasses.push_back(SubRegClass);
237 }
238
239 // Allow targets to override the size in bits of the RegisterClass.
240 unsigned Size = R->getValueAsInt("Size");
241
242 Namespace = R->getValueAsString("Namespace");
Owen Andersonac9de032009-08-10 22:56:29 +0000243 SpillSize = Size ? Size : EVT(VTs[0]).getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000244 SpillAlignment = R->getValueAsInt("Alignment");
Evan Cheng77ac1822007-09-19 01:35:01 +0000245 CopyCost = R->getValueAsInt("CopyCost");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000246 MethodBodies = R->getValueAsCode("MethodBodies");
247 MethodProtos = R->getValueAsCode("MethodProtos");
248}
249
250const std::string &CodeGenRegisterClass::getName() const {
251 return TheDef->getName();
252}
253
254void CodeGenTarget::ReadLegalValueTypes() const {
255 const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses();
256 for (unsigned i = 0, e = RCs.size(); i != e; ++i)
257 for (unsigned ri = 0, re = RCs[i].VTs.size(); ri != re; ++ri)
258 LegalValueTypes.push_back(RCs[i].VTs[ri]);
259
260 // Remove duplicates.
261 std::sort(LegalValueTypes.begin(), LegalValueTypes.end());
262 LegalValueTypes.erase(std::unique(LegalValueTypes.begin(),
263 LegalValueTypes.end()),
264 LegalValueTypes.end());
265}
266
267
268void CodeGenTarget::ReadInstructions() const {
269 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
270 if (Insts.size() <= 2)
271 throw std::string("No 'Instruction' subclasses defined!");
272
273 // Parse the instructions defined in the .td file.
274 std::string InstFormatName =
275 getAsmWriter()->getValueAsString("InstFormatName");
276
277 for (unsigned i = 0, e = Insts.size(); i != e; ++i) {
278 std::string AsmStr = Insts[i]->getValueAsString(InstFormatName);
279 Instructions.insert(std::make_pair(Insts[i]->getName(),
280 CodeGenInstruction(Insts[i], AsmStr)));
281 }
282}
283
284/// getInstructionsByEnumValue - Return all of the instructions defined by the
285/// target, ordered by their enum value.
286void CodeGenTarget::
287getInstructionsByEnumValue(std::vector<const CodeGenInstruction*>
288 &NumberedInstructions) {
289 std::map<std::string, CodeGenInstruction>::const_iterator I;
290 I = getInstructions().find("PHI");
291 if (I == Instructions.end()) throw "Could not find 'PHI' instruction!";
292 const CodeGenInstruction *PHI = &I->second;
293
294 I = getInstructions().find("INLINEASM");
295 if (I == Instructions.end()) throw "Could not find 'INLINEASM' instruction!";
296 const CodeGenInstruction *INLINEASM = &I->second;
297
Dan Gohmanfa607c92008-07-01 00:05:16 +0000298 I = getInstructions().find("DBG_LABEL");
299 if (I == Instructions.end()) throw "Could not find 'DBG_LABEL' instruction!";
300 const CodeGenInstruction *DBG_LABEL = &I->second;
301
302 I = getInstructions().find("EH_LABEL");
303 if (I == Instructions.end()) throw "Could not find 'EH_LABEL' instruction!";
304 const CodeGenInstruction *EH_LABEL = &I->second;
305
306 I = getInstructions().find("GC_LABEL");
307 if (I == Instructions.end()) throw "Could not find 'GC_LABEL' instruction!";
308 const CodeGenInstruction *GC_LABEL = &I->second;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000309
Evan Cheng2e28d622008-02-02 04:07:54 +0000310 I = getInstructions().find("DECLARE");
311 if (I == Instructions.end()) throw "Could not find 'DECLARE' instruction!";
312 const CodeGenInstruction *DECLARE = &I->second;
313
Christopher Lamb071a2a72007-07-26 07:48:21 +0000314 I = getInstructions().find("EXTRACT_SUBREG");
315 if (I == Instructions.end())
316 throw "Could not find 'EXTRACT_SUBREG' instruction!";
317 const CodeGenInstruction *EXTRACT_SUBREG = &I->second;
318
319 I = getInstructions().find("INSERT_SUBREG");
320 if (I == Instructions.end())
321 throw "Could not find 'INSERT_SUBREG' instruction!";
322 const CodeGenInstruction *INSERT_SUBREG = &I->second;
323
Evan Cheng3c0eda52008-03-15 00:03:38 +0000324 I = getInstructions().find("IMPLICIT_DEF");
325 if (I == Instructions.end())
326 throw "Could not find 'IMPLICIT_DEF' instruction!";
327 const CodeGenInstruction *IMPLICIT_DEF = &I->second;
328
Christopher Lamb76d72da2008-03-16 03:12:01 +0000329 I = getInstructions().find("SUBREG_TO_REG");
330 if (I == Instructions.end())
331 throw "Could not find 'SUBREG_TO_REG' instruction!";
332 const CodeGenInstruction *SUBREG_TO_REG = &I->second;
Dan Gohman1aeb5db2009-04-13 15:38:05 +0000333
Dan Gohman4c10fc72009-04-13 21:06:25 +0000334 I = getInstructions().find("COPY_TO_REGCLASS");
Dan Gohman1aeb5db2009-04-13 15:38:05 +0000335 if (I == Instructions.end())
Dan Gohman4c10fc72009-04-13 21:06:25 +0000336 throw "Could not find 'COPY_TO_REGCLASS' instruction!";
337 const CodeGenInstruction *COPY_TO_REGCLASS = &I->second;
Dan Gohman1aeb5db2009-04-13 15:38:05 +0000338
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000339 // Print out the rest of the instructions now.
340 NumberedInstructions.push_back(PHI);
341 NumberedInstructions.push_back(INLINEASM);
Dan Gohmanfa607c92008-07-01 00:05:16 +0000342 NumberedInstructions.push_back(DBG_LABEL);
343 NumberedInstructions.push_back(EH_LABEL);
344 NumberedInstructions.push_back(GC_LABEL);
Evan Cheng2e28d622008-02-02 04:07:54 +0000345 NumberedInstructions.push_back(DECLARE);
Christopher Lamb071a2a72007-07-26 07:48:21 +0000346 NumberedInstructions.push_back(EXTRACT_SUBREG);
347 NumberedInstructions.push_back(INSERT_SUBREG);
Evan Cheng3c0eda52008-03-15 00:03:38 +0000348 NumberedInstructions.push_back(IMPLICIT_DEF);
Christopher Lamb76d72da2008-03-16 03:12:01 +0000349 NumberedInstructions.push_back(SUBREG_TO_REG);
Dan Gohman4c10fc72009-04-13 21:06:25 +0000350 NumberedInstructions.push_back(COPY_TO_REGCLASS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000351 for (inst_iterator II = inst_begin(), E = inst_end(); II != E; ++II)
352 if (&II->second != PHI &&
353 &II->second != INLINEASM &&
Dan Gohmanfa607c92008-07-01 00:05:16 +0000354 &II->second != DBG_LABEL &&
355 &II->second != EH_LABEL &&
356 &II->second != GC_LABEL &&
Evan Cheng2e28d622008-02-02 04:07:54 +0000357 &II->second != DECLARE &&
Christopher Lamb071a2a72007-07-26 07:48:21 +0000358 &II->second != EXTRACT_SUBREG &&
Evan Cheng3c0eda52008-03-15 00:03:38 +0000359 &II->second != INSERT_SUBREG &&
Christopher Lamb76d72da2008-03-16 03:12:01 +0000360 &II->second != IMPLICIT_DEF &&
Dan Gohman1aeb5db2009-04-13 15:38:05 +0000361 &II->second != SUBREG_TO_REG &&
Dan Gohman4c10fc72009-04-13 21:06:25 +0000362 &II->second != COPY_TO_REGCLASS)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000363 NumberedInstructions.push_back(&II->second);
364}
365
366
367/// isLittleEndianEncoding - Return whether this target encodes its instruction
368/// in little-endian format, i.e. bits laid out in the order [0..n]
369///
370bool CodeGenTarget::isLittleEndianEncoding() const {
371 return getInstructionSet()->getValueAsBit("isLittleEndianEncoding");
372}
373
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000374//===----------------------------------------------------------------------===//
375// ComplexPattern implementation
376//
377ComplexPattern::ComplexPattern(Record *R) {
378 Ty = ::getValueType(R->getValueAsDef("Ty"));
379 NumOperands = R->getValueAsInt("NumOperands");
380 SelectFunc = R->getValueAsString("SelectFunc");
381 RootNodes = R->getValueAsListOfDefs("RootNodes");
382
383 // Parse the properties.
384 Properties = 0;
385 std::vector<Record*> PropList = R->getValueAsListOfDefs("Properties");
386 for (unsigned i = 0, e = PropList.size(); i != e; ++i)
387 if (PropList[i]->getName() == "SDNPHasChain") {
388 Properties |= 1 << SDNPHasChain;
389 } else if (PropList[i]->getName() == "SDNPOptInFlag") {
390 Properties |= 1 << SDNPOptInFlag;
Chris Lattnerc90ee9c2008-01-10 07:59:24 +0000391 } else if (PropList[i]->getName() == "SDNPMayStore") {
392 Properties |= 1 << SDNPMayStore;
393 } else if (PropList[i]->getName() == "SDNPMayLoad") {
394 Properties |= 1 << SDNPMayLoad;
395 } else if (PropList[i]->getName() == "SDNPSideEffect") {
396 Properties |= 1 << SDNPSideEffect;
Mon P Wang6bde9ec2008-06-25 08:15:39 +0000397 } else if (PropList[i]->getName() == "SDNPMemOperand") {
398 Properties |= 1 << SDNPMemOperand;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000399 } else {
Daniel Dunbard4287062009-07-03 00:10:29 +0000400 errs() << "Unsupported SD Node property '" << PropList[i]->getName()
401 << "' on ComplexPattern '" << R->getName() << "'!\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000402 exit(1);
403 }
Christopher Lamb059c7c92008-01-31 07:27:46 +0000404
405 // Parse the attributes.
406 Attributes = 0;
407 PropList = R->getValueAsListOfDefs("Attributes");
408 for (unsigned i = 0, e = PropList.size(); i != e; ++i)
409 if (PropList[i]->getName() == "CPAttrParentAsRoot") {
410 Attributes |= 1 << CPAttrParentAsRoot;
411 } else {
Daniel Dunbard4287062009-07-03 00:10:29 +0000412 errs() << "Unsupported pattern attribute '" << PropList[i]->getName()
413 << "' on ComplexPattern '" << R->getName() << "'!\n";
Christopher Lamb059c7c92008-01-31 07:27:46 +0000414 exit(1);
415 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000416}
417
418//===----------------------------------------------------------------------===//
419// CodeGenIntrinsic Implementation
420//===----------------------------------------------------------------------===//
421
Dale Johannesenfe5921a2009-02-05 01:49:45 +0000422std::vector<CodeGenIntrinsic> llvm::LoadIntrinsics(const RecordKeeper &RC,
423 bool TargetOnly) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000424 std::vector<Record*> I = RC.getAllDerivedDefinitions("Intrinsic");
425
426 std::vector<CodeGenIntrinsic> Result;
427
Dale Johannesenfe5921a2009-02-05 01:49:45 +0000428 for (unsigned i = 0, e = I.size(); i != e; ++i) {
429 bool isTarget = I[i]->getValueAsBit("isTarget");
430 if (isTarget == TargetOnly)
431 Result.push_back(CodeGenIntrinsic(I[i]));
432 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000433 return Result;
434}
435
Dan Gohman907df572008-04-03 00:02:49 +0000436CodeGenIntrinsic::CodeGenIntrinsic(Record *R) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000437 TheDef = R;
438 std::string DefName = R->getName();
439 ModRef = WriteMem;
440 isOverloaded = false;
Evan Chengbb46d262008-06-16 20:29:38 +0000441 isCommutative = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000442
443 if (DefName.size() <= 4 ||
Bill Wendling9b4eab82008-11-13 09:08:33 +0000444 std::string(DefName.begin(), DefName.begin() + 4) != "int_")
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000445 throw "Intrinsic '" + DefName + "' does not start with 'int_'!";
Bill Wendling9b4eab82008-11-13 09:08:33 +0000446
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000447 EnumName = std::string(DefName.begin()+4, DefName.end());
Bill Wendling9b4eab82008-11-13 09:08:33 +0000448
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000449 if (R->getValue("GCCBuiltinName")) // Ignore a missing GCCBuiltinName field.
450 GCCBuiltinName = R->getValueAsString("GCCBuiltinName");
Bill Wendling9b4eab82008-11-13 09:08:33 +0000451
452 TargetPrefix = R->getValueAsString("TargetPrefix");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000453 Name = R->getValueAsString("LLVMName");
Bill Wendling9b4eab82008-11-13 09:08:33 +0000454
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000455 if (Name == "") {
456 // If an explicit name isn't specified, derive one from the DefName.
457 Name = "llvm.";
Bill Wendling9b4eab82008-11-13 09:08:33 +0000458
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000459 for (unsigned i = 0, e = EnumName.size(); i != e; ++i)
Bill Wendling9b4eab82008-11-13 09:08:33 +0000460 Name += (EnumName[i] == '_') ? '.' : EnumName[i];
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000461 } else {
462 // Verify it starts with "llvm.".
463 if (Name.size() <= 5 ||
Bill Wendling9b4eab82008-11-13 09:08:33 +0000464 std::string(Name.begin(), Name.begin() + 5) != "llvm.")
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000465 throw "Intrinsic '" + DefName + "'s name does not start with 'llvm.'!";
466 }
467
468 // If TargetPrefix is specified, make sure that Name starts with
469 // "llvm.<targetprefix>.".
470 if (!TargetPrefix.empty()) {
471 if (Name.size() < 6+TargetPrefix.size() ||
Bill Wendling9b4eab82008-11-13 09:08:33 +0000472 std::string(Name.begin() + 5, Name.begin() + 6 + TargetPrefix.size())
473 != (TargetPrefix + "."))
474 throw "Intrinsic '" + DefName + "' does not start with 'llvm." +
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000475 TargetPrefix + ".'!";
476 }
477
Bill Wendling9b4eab82008-11-13 09:08:33 +0000478 // Parse the list of return types.
Owen Andersonac9de032009-08-10 22:56:29 +0000479 std::vector<EVT::SimpleValueType> OverloadedVTs;
Bill Wendling9b4eab82008-11-13 09:08:33 +0000480 ListInit *TypeList = R->getValueAsListInit("RetTypes");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000481 for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) {
482 Record *TyEl = TypeList->getElementAsRecord(i);
483 assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!");
Owen Andersonac9de032009-08-10 22:56:29 +0000484 EVT::SimpleValueType VT;
Bob Wilsonc7245f42009-01-07 00:09:01 +0000485 if (TyEl->isSubClassOf("LLVMMatchType")) {
Bob Wilson0f2b86a2009-04-16 21:51:05 +0000486 unsigned MatchTy = TyEl->getValueAsInt("Number");
487 assert(MatchTy < OverloadedVTs.size() &&
488 "Invalid matching number!");
489 VT = OverloadedVTs[MatchTy];
Bob Wilsonc7245f42009-01-07 00:09:01 +0000490 // It only makes sense to use the extended and truncated vector element
491 // variants with iAny types; otherwise, if the intrinsic is not
492 // overloaded, all the types can be specified directly.
493 assert(((!TyEl->isSubClassOf("LLVMExtendedElementVectorType") &&
494 !TyEl->isSubClassOf("LLVMTruncatedElementVectorType")) ||
Owen Andersonac9de032009-08-10 22:56:29 +0000495 VT == EVT::iAny) && "Expected iAny type");
Bob Wilson0f2b86a2009-04-16 21:51:05 +0000496 } else {
Bob Wilsonc7245f42009-01-07 00:09:01 +0000497 VT = getValueType(TyEl->getValueAsDef("VT"));
Bob Wilson0f2b86a2009-04-16 21:51:05 +0000498 }
Owen Andersonac9de032009-08-10 22:56:29 +0000499 if (VT == EVT::iAny || VT == EVT::fAny || VT == EVT::iPTRAny) {
Bob Wilson0f2b86a2009-04-16 21:51:05 +0000500 OverloadedVTs.push_back(VT);
501 isOverloaded |= true;
502 }
Bill Wendling9b4eab82008-11-13 09:08:33 +0000503 IS.RetVTs.push_back(VT);
504 IS.RetTypeDefs.push_back(TyEl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000505 }
Bill Wendling9b4eab82008-11-13 09:08:33 +0000506
507 if (IS.RetVTs.size() == 0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000508 throw "Intrinsic '"+DefName+"' needs at least a type for the ret value!";
509
Bill Wendling9b4eab82008-11-13 09:08:33 +0000510 // Parse the list of parameter types.
511 TypeList = R->getValueAsListInit("ParamTypes");
512 for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) {
513 Record *TyEl = TypeList->getElementAsRecord(i);
514 assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!");
Owen Andersonac9de032009-08-10 22:56:29 +0000515 EVT::SimpleValueType VT;
Bob Wilsonc7245f42009-01-07 00:09:01 +0000516 if (TyEl->isSubClassOf("LLVMMatchType")) {
517 unsigned MatchTy = TyEl->getValueAsInt("Number");
Bob Wilson0f2b86a2009-04-16 21:51:05 +0000518 assert(MatchTy < OverloadedVTs.size() &&
519 "Invalid matching number!");
520 VT = OverloadedVTs[MatchTy];
Bob Wilsonc7245f42009-01-07 00:09:01 +0000521 // It only makes sense to use the extended and truncated vector element
522 // variants with iAny types; otherwise, if the intrinsic is not
523 // overloaded, all the types can be specified directly.
524 assert(((!TyEl->isSubClassOf("LLVMExtendedElementVectorType") &&
525 !TyEl->isSubClassOf("LLVMTruncatedElementVectorType")) ||
Owen Andersonac9de032009-08-10 22:56:29 +0000526 VT == EVT::iAny) && "Expected iAny type");
Bob Wilsonc7245f42009-01-07 00:09:01 +0000527 } else
528 VT = getValueType(TyEl->getValueAsDef("VT"));
Owen Andersonac9de032009-08-10 22:56:29 +0000529 if (VT == EVT::iAny || VT == EVT::fAny || VT == EVT::iPTRAny) {
Bob Wilson0f2b86a2009-04-16 21:51:05 +0000530 OverloadedVTs.push_back(VT);
531 isOverloaded |= true;
532 }
Bill Wendling9b4eab82008-11-13 09:08:33 +0000533 IS.ParamVTs.push_back(VT);
534 IS.ParamTypeDefs.push_back(TyEl);
535 }
536
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000537 // Parse the intrinsic properties.
538 ListInit *PropList = R->getValueAsListInit("Properties");
539 for (unsigned i = 0, e = PropList->getSize(); i != e; ++i) {
540 Record *Property = PropList->getElementAsRecord(i);
541 assert(Property->isSubClassOf("IntrinsicProperty") &&
542 "Expected a property!");
543
544 if (Property->getName() == "IntrNoMem")
545 ModRef = NoMem;
546 else if (Property->getName() == "IntrReadArgMem")
547 ModRef = ReadArgMem;
548 else if (Property->getName() == "IntrReadMem")
549 ModRef = ReadMem;
550 else if (Property->getName() == "IntrWriteArgMem")
551 ModRef = WriteArgMem;
552 else if (Property->getName() == "IntrWriteMem")
553 ModRef = WriteMem;
Evan Chengbb46d262008-06-16 20:29:38 +0000554 else if (Property->getName() == "Commutative")
555 isCommutative = true;
Chris Lattner1b0090f2009-01-12 01:12:03 +0000556 else if (Property->isSubClassOf("NoCapture")) {
557 unsigned ArgNo = Property->getValueAsInt("ArgNo");
558 ArgumentAttributes.push_back(std::make_pair(ArgNo, NoCapture));
559 } else
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000560 assert(0 && "Unknown property!");
561 }
562}