blob: 797614ee45afade586d2d71780ebb559d985f9a0 [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 Anderson36e3a6e2009-08-11 20:47:22 +000033/// getValueType - Return the MVT::SimpleValueType that the specified TableGen
Duncan Sands92c43912008-06-06 12:08:01 +000034/// record corresponds to.
Owen Anderson36e3a6e2009-08-11 20:47:22 +000035MVT::SimpleValueType llvm::getValueType(Record *Rec) {
36 return (MVT::SimpleValueType)Rec->getValueAsInt("Value");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000037}
38
Owen Anderson36e3a6e2009-08-11 20:47:22 +000039std::string llvm::getName(MVT::SimpleValueType T) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000040 switch (T) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +000041 case MVT::Other: return "UNKNOWN";
42 case MVT::iPTR: return "TLI.getPointerTy()";
43 case MVT::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 Anderson36e3a6e2009-08-11 20:47:22 +000048std::string llvm::getEnumName(MVT::SimpleValueType T) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000049 switch (T) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +000050 case MVT::Other: return "MVT::Other";
51 case MVT::i1: return "MVT::i1";
52 case MVT::i8: return "MVT::i8";
53 case MVT::i16: return "MVT::i16";
54 case MVT::i32: return "MVT::i32";
55 case MVT::i64: return "MVT::i64";
56 case MVT::i128: return "MVT::i128";
57 case MVT::iAny: return "MVT::iAny";
58 case MVT::fAny: return "MVT::fAny";
59 case MVT::vAny: return "MVT::vAny";
60 case MVT::f32: return "MVT::f32";
61 case MVT::f64: return "MVT::f64";
62 case MVT::f80: return "MVT::f80";
63 case MVT::f128: return "MVT::f128";
64 case MVT::ppcf128: return "MVT::ppcf128";
65 case MVT::Flag: return "MVT::Flag";
66 case MVT::isVoid:return "MVT::isVoid";
67 case MVT::v2i8: return "MVT::v2i8";
68 case MVT::v4i8: return "MVT::v4i8";
69 case MVT::v8i8: return "MVT::v8i8";
70 case MVT::v16i8: return "MVT::v16i8";
71 case MVT::v32i8: return "MVT::v32i8";
72 case MVT::v2i16: return "MVT::v2i16";
73 case MVT::v4i16: return "MVT::v4i16";
74 case MVT::v8i16: return "MVT::v8i16";
75 case MVT::v16i16: return "MVT::v16i16";
76 case MVT::v2i32: return "MVT::v2i32";
77 case MVT::v4i32: return "MVT::v4i32";
78 case MVT::v8i32: return "MVT::v8i32";
79 case MVT::v1i64: return "MVT::v1i64";
80 case MVT::v2i64: return "MVT::v2i64";
81 case MVT::v4i64: return "MVT::v4i64";
82 case MVT::v2f32: return "MVT::v2f32";
83 case MVT::v4f32: return "MVT::v4f32";
84 case MVT::v8f32: return "MVT::v8f32";
85 case MVT::v2f64: return "MVT::v2f64";
86 case MVT::v4f64: return "MVT::v4f64";
87 case MVT::Metadata: return "MVT::Metadata";
88 case MVT::iPTR: return "MVT::iPTR";
89 case MVT::iPTRAny: return "MVT::iPTRAny";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000090 default: assert(0 && "ILLEGAL VALUE TYPE!"); return "";
91 }
92}
93
Chris Lattner4ca8ff02008-01-05 22:25:12 +000094/// getQualifiedName - Return the name of the specified record, with a
95/// namespace qualifier if the record contains one.
96///
97std::string llvm::getQualifiedName(const Record *R) {
98 std::string Namespace = R->getValueAsString("Namespace");
99 if (Namespace.empty()) return R->getName();
100 return Namespace + "::" + R->getName();
101}
102
103
104
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000105
106/// getTarget - Return the current instance of the Target class.
107///
108CodeGenTarget::CodeGenTarget() {
109 std::vector<Record*> Targets = Records.getAllDerivedDefinitions("Target");
110 if (Targets.size() == 0)
111 throw std::string("ERROR: No 'Target' subclasses defined!");
112 if (Targets.size() != 1)
113 throw std::string("ERROR: Multiple subclasses of Target defined!");
114 TargetRec = Targets[0];
115}
116
117
118const std::string &CodeGenTarget::getName() const {
119 return TargetRec->getName();
120}
121
Dan Gohman6a36cc92008-08-20 21:45:57 +0000122std::string CodeGenTarget::getInstNamespace() const {
123 std::string InstNS;
124
125 for (inst_iterator i = inst_begin(), e = inst_end(); i != e; ++i) {
126 InstNS = i->second.Namespace;
127
128 // Make sure not to pick up "TargetInstrInfo" by accidentally getting
129 // the namespace off the PHI instruction or something.
130 if (InstNS != "TargetInstrInfo")
131 break;
132 }
133
134 return InstNS;
135}
136
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000137Record *CodeGenTarget::getInstructionSet() const {
138 return TargetRec->getValueAsDef("InstructionSet");
139}
140
Daniel Dunbar85f1b392009-07-29 00:02:19 +0000141/// getAsmParser - Return the AssemblyParser definition for this target.
142///
143Record *CodeGenTarget::getAsmParser() const {
144 std::vector<Record*> LI = TargetRec->getValueAsListOfDefs("AssemblyParsers");
145 if (AsmParserNum >= LI.size())
146 throw "Target does not have an AsmParser #" + utostr(AsmParserNum) + "!";
147 return LI[AsmParserNum];
148}
149
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000150/// getAsmWriter - Return the AssemblyWriter definition for this target.
151///
152Record *CodeGenTarget::getAsmWriter() const {
153 std::vector<Record*> LI = TargetRec->getValueAsListOfDefs("AssemblyWriters");
154 if (AsmWriterNum >= LI.size())
155 throw "Target does not have an AsmWriter #" + utostr(AsmWriterNum) + "!";
156 return LI[AsmWriterNum];
157}
158
159void CodeGenTarget::ReadRegisters() const {
160 std::vector<Record*> Regs = Records.getAllDerivedDefinitions("Register");
161 if (Regs.empty())
162 throw std::string("No 'Register' subclasses defined!");
163
164 Registers.reserve(Regs.size());
165 Registers.assign(Regs.begin(), Regs.end());
166}
167
168CodeGenRegister::CodeGenRegister(Record *R) : TheDef(R) {
169 DeclaredSpillSize = R->getValueAsInt("SpillSize");
170 DeclaredSpillAlignment = R->getValueAsInt("SpillAlignment");
171}
172
173const std::string &CodeGenRegister::getName() const {
174 return TheDef->getName();
175}
176
177void CodeGenTarget::ReadRegisterClasses() const {
178 std::vector<Record*> RegClasses =
179 Records.getAllDerivedDefinitions("RegisterClass");
180 if (RegClasses.empty())
181 throw std::string("No 'RegisterClass' subclasses defined!");
182
183 RegisterClasses.reserve(RegClasses.size());
184 RegisterClasses.assign(RegClasses.begin(), RegClasses.end());
185}
186
187std::vector<unsigned char> CodeGenTarget::getRegisterVTs(Record *R) const {
188 std::vector<unsigned char> Result;
189 const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses();
190 for (unsigned i = 0, e = RCs.size(); i != e; ++i) {
191 const CodeGenRegisterClass &RC = RegisterClasses[i];
192 for (unsigned ei = 0, ee = RC.Elements.size(); ei != ee; ++ei) {
193 if (R == RC.Elements[ei]) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000194 const std::vector<MVT::SimpleValueType> &InVTs = RC.getValueTypes();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000195 for (unsigned i = 0, e = InVTs.size(); i != e; ++i)
196 Result.push_back(InVTs[i]);
197 }
198 }
199 }
200 return Result;
201}
202
203
204CodeGenRegisterClass::CodeGenRegisterClass(Record *R) : TheDef(R) {
205 // Rename anonymous register classes.
206 if (R->getName().size() > 9 && R->getName()[9] == '.') {
207 static unsigned AnonCounter = 0;
208 R->setName("AnonRegClass_"+utostr(AnonCounter++));
209 }
210
211 std::vector<Record*> TypeList = R->getValueAsListOfDefs("RegTypes");
212 for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
213 Record *Type = TypeList[i];
214 if (!Type->isSubClassOf("ValueType"))
215 throw "RegTypes list member '" + Type->getName() +
216 "' does not derive from the ValueType class!";
217 VTs.push_back(getValueType(Type));
218 }
219 assert(!VTs.empty() && "RegisterClass must contain at least one ValueType!");
220
221 std::vector<Record*> RegList = R->getValueAsListOfDefs("MemberList");
222 for (unsigned i = 0, e = RegList.size(); i != e; ++i) {
223 Record *Reg = RegList[i];
224 if (!Reg->isSubClassOf("Register"))
225 throw "Register Class member '" + Reg->getName() +
226 "' does not derive from the Register class!";
227 Elements.push_back(Reg);
228 }
229
230 std::vector<Record*> SubRegClassList =
231 R->getValueAsListOfDefs("SubRegClassList");
232 for (unsigned i = 0, e = SubRegClassList.size(); i != e; ++i) {
233 Record *SubRegClass = SubRegClassList[i];
234 if (!SubRegClass->isSubClassOf("RegisterClass"))
235 throw "Register Class member '" + SubRegClass->getName() +
236 "' does not derive from the RegisterClass class!";
237 SubRegClasses.push_back(SubRegClass);
238 }
239
240 // Allow targets to override the size in bits of the RegisterClass.
241 unsigned Size = R->getValueAsInt("Size");
242
243 Namespace = R->getValueAsString("Namespace");
Owen Andersonac9de032009-08-10 22:56:29 +0000244 SpillSize = Size ? Size : EVT(VTs[0]).getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000245 SpillAlignment = R->getValueAsInt("Alignment");
Evan Cheng77ac1822007-09-19 01:35:01 +0000246 CopyCost = R->getValueAsInt("CopyCost");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000247 MethodBodies = R->getValueAsCode("MethodBodies");
248 MethodProtos = R->getValueAsCode("MethodProtos");
249}
250
251const std::string &CodeGenRegisterClass::getName() const {
252 return TheDef->getName();
253}
254
255void CodeGenTarget::ReadLegalValueTypes() const {
256 const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses();
257 for (unsigned i = 0, e = RCs.size(); i != e; ++i)
258 for (unsigned ri = 0, re = RCs[i].VTs.size(); ri != re; ++ri)
259 LegalValueTypes.push_back(RCs[i].VTs[ri]);
260
261 // Remove duplicates.
262 std::sort(LegalValueTypes.begin(), LegalValueTypes.end());
263 LegalValueTypes.erase(std::unique(LegalValueTypes.begin(),
264 LegalValueTypes.end()),
265 LegalValueTypes.end());
266}
267
268
269void CodeGenTarget::ReadInstructions() const {
270 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
271 if (Insts.size() <= 2)
272 throw std::string("No 'Instruction' subclasses defined!");
273
274 // Parse the instructions defined in the .td file.
275 std::string InstFormatName =
276 getAsmWriter()->getValueAsString("InstFormatName");
277
278 for (unsigned i = 0, e = Insts.size(); i != e; ++i) {
279 std::string AsmStr = Insts[i]->getValueAsString(InstFormatName);
280 Instructions.insert(std::make_pair(Insts[i]->getName(),
281 CodeGenInstruction(Insts[i], AsmStr)));
282 }
283}
284
285/// getInstructionsByEnumValue - Return all of the instructions defined by the
286/// target, ordered by their enum value.
287void CodeGenTarget::
288getInstructionsByEnumValue(std::vector<const CodeGenInstruction*>
289 &NumberedInstructions) {
290 std::map<std::string, CodeGenInstruction>::const_iterator I;
291 I = getInstructions().find("PHI");
292 if (I == Instructions.end()) throw "Could not find 'PHI' instruction!";
293 const CodeGenInstruction *PHI = &I->second;
294
295 I = getInstructions().find("INLINEASM");
296 if (I == Instructions.end()) throw "Could not find 'INLINEASM' instruction!";
297 const CodeGenInstruction *INLINEASM = &I->second;
298
Dan Gohmanfa607c92008-07-01 00:05:16 +0000299 I = getInstructions().find("DBG_LABEL");
300 if (I == Instructions.end()) throw "Could not find 'DBG_LABEL' instruction!";
301 const CodeGenInstruction *DBG_LABEL = &I->second;
302
303 I = getInstructions().find("EH_LABEL");
304 if (I == Instructions.end()) throw "Could not find 'EH_LABEL' instruction!";
305 const CodeGenInstruction *EH_LABEL = &I->second;
306
307 I = getInstructions().find("GC_LABEL");
308 if (I == Instructions.end()) throw "Could not find 'GC_LABEL' instruction!";
309 const CodeGenInstruction *GC_LABEL = &I->second;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000310
Jakob Stoklund Olesen8f12c7c2009-09-28 20:32:26 +0000311 I = getInstructions().find("KILL");
312 if (I == Instructions.end()) throw "Could not find 'KILL' instruction!";
313 const CodeGenInstruction *KILL = &I->second;
Evan Cheng2e28d622008-02-02 04:07:54 +0000314
Christopher Lamb071a2a72007-07-26 07:48:21 +0000315 I = getInstructions().find("EXTRACT_SUBREG");
316 if (I == Instructions.end())
317 throw "Could not find 'EXTRACT_SUBREG' instruction!";
318 const CodeGenInstruction *EXTRACT_SUBREG = &I->second;
319
320 I = getInstructions().find("INSERT_SUBREG");
321 if (I == Instructions.end())
322 throw "Could not find 'INSERT_SUBREG' instruction!";
323 const CodeGenInstruction *INSERT_SUBREG = &I->second;
324
Evan Cheng3c0eda52008-03-15 00:03:38 +0000325 I = getInstructions().find("IMPLICIT_DEF");
326 if (I == Instructions.end())
327 throw "Could not find 'IMPLICIT_DEF' instruction!";
328 const CodeGenInstruction *IMPLICIT_DEF = &I->second;
329
Christopher Lamb76d72da2008-03-16 03:12:01 +0000330 I = getInstructions().find("SUBREG_TO_REG");
331 if (I == Instructions.end())
332 throw "Could not find 'SUBREG_TO_REG' instruction!";
333 const CodeGenInstruction *SUBREG_TO_REG = &I->second;
Dan Gohman1aeb5db2009-04-13 15:38:05 +0000334
Dan Gohman4c10fc72009-04-13 21:06:25 +0000335 I = getInstructions().find("COPY_TO_REGCLASS");
Dan Gohman1aeb5db2009-04-13 15:38:05 +0000336 if (I == Instructions.end())
Dan Gohman4c10fc72009-04-13 21:06:25 +0000337 throw "Could not find 'COPY_TO_REGCLASS' instruction!";
338 const CodeGenInstruction *COPY_TO_REGCLASS = &I->second;
Dan Gohman1aeb5db2009-04-13 15:38:05 +0000339
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000340 // Print out the rest of the instructions now.
341 NumberedInstructions.push_back(PHI);
342 NumberedInstructions.push_back(INLINEASM);
Dan Gohmanfa607c92008-07-01 00:05:16 +0000343 NumberedInstructions.push_back(DBG_LABEL);
344 NumberedInstructions.push_back(EH_LABEL);
345 NumberedInstructions.push_back(GC_LABEL);
Jakob Stoklund Olesen8f12c7c2009-09-28 20:32:26 +0000346 NumberedInstructions.push_back(KILL);
Christopher Lamb071a2a72007-07-26 07:48:21 +0000347 NumberedInstructions.push_back(EXTRACT_SUBREG);
348 NumberedInstructions.push_back(INSERT_SUBREG);
Evan Cheng3c0eda52008-03-15 00:03:38 +0000349 NumberedInstructions.push_back(IMPLICIT_DEF);
Christopher Lamb76d72da2008-03-16 03:12:01 +0000350 NumberedInstructions.push_back(SUBREG_TO_REG);
Dan Gohman4c10fc72009-04-13 21:06:25 +0000351 NumberedInstructions.push_back(COPY_TO_REGCLASS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000352 for (inst_iterator II = inst_begin(), E = inst_end(); II != E; ++II)
353 if (&II->second != PHI &&
354 &II->second != INLINEASM &&
Dan Gohmanfa607c92008-07-01 00:05:16 +0000355 &II->second != DBG_LABEL &&
356 &II->second != EH_LABEL &&
357 &II->second != GC_LABEL &&
Jakob Stoklund Olesen8f12c7c2009-09-28 20:32:26 +0000358 &II->second != KILL &&
Christopher Lamb071a2a72007-07-26 07:48:21 +0000359 &II->second != EXTRACT_SUBREG &&
Evan Cheng3c0eda52008-03-15 00:03:38 +0000360 &II->second != INSERT_SUBREG &&
Christopher Lamb76d72da2008-03-16 03:12:01 +0000361 &II->second != IMPLICIT_DEF &&
Dan Gohman1aeb5db2009-04-13 15:38:05 +0000362 &II->second != SUBREG_TO_REG &&
Dan Gohman4c10fc72009-04-13 21:06:25 +0000363 &II->second != COPY_TO_REGCLASS)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000364 NumberedInstructions.push_back(&II->second);
365}
366
367
368/// isLittleEndianEncoding - Return whether this target encodes its instruction
369/// in little-endian format, i.e. bits laid out in the order [0..n]
370///
371bool CodeGenTarget::isLittleEndianEncoding() const {
372 return getInstructionSet()->getValueAsBit("isLittleEndianEncoding");
373}
374
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000375//===----------------------------------------------------------------------===//
376// ComplexPattern implementation
377//
378ComplexPattern::ComplexPattern(Record *R) {
379 Ty = ::getValueType(R->getValueAsDef("Ty"));
380 NumOperands = R->getValueAsInt("NumOperands");
381 SelectFunc = R->getValueAsString("SelectFunc");
382 RootNodes = R->getValueAsListOfDefs("RootNodes");
383
384 // Parse the properties.
385 Properties = 0;
386 std::vector<Record*> PropList = R->getValueAsListOfDefs("Properties");
387 for (unsigned i = 0, e = PropList.size(); i != e; ++i)
388 if (PropList[i]->getName() == "SDNPHasChain") {
389 Properties |= 1 << SDNPHasChain;
390 } else if (PropList[i]->getName() == "SDNPOptInFlag") {
391 Properties |= 1 << SDNPOptInFlag;
Chris Lattnerc90ee9c2008-01-10 07:59:24 +0000392 } else if (PropList[i]->getName() == "SDNPMayStore") {
393 Properties |= 1 << SDNPMayStore;
394 } else if (PropList[i]->getName() == "SDNPMayLoad") {
395 Properties |= 1 << SDNPMayLoad;
396 } else if (PropList[i]->getName() == "SDNPSideEffect") {
397 Properties |= 1 << SDNPSideEffect;
Mon P Wang6bde9ec2008-06-25 08:15:39 +0000398 } else if (PropList[i]->getName() == "SDNPMemOperand") {
399 Properties |= 1 << SDNPMemOperand;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000400 } else {
Daniel Dunbard4287062009-07-03 00:10:29 +0000401 errs() << "Unsupported SD Node property '" << PropList[i]->getName()
402 << "' on ComplexPattern '" << R->getName() << "'!\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000403 exit(1);
404 }
405}
406
407//===----------------------------------------------------------------------===//
408// CodeGenIntrinsic Implementation
409//===----------------------------------------------------------------------===//
410
Dale Johannesenfe5921a2009-02-05 01:49:45 +0000411std::vector<CodeGenIntrinsic> llvm::LoadIntrinsics(const RecordKeeper &RC,
412 bool TargetOnly) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000413 std::vector<Record*> I = RC.getAllDerivedDefinitions("Intrinsic");
414
415 std::vector<CodeGenIntrinsic> Result;
416
Dale Johannesenfe5921a2009-02-05 01:49:45 +0000417 for (unsigned i = 0, e = I.size(); i != e; ++i) {
418 bool isTarget = I[i]->getValueAsBit("isTarget");
419 if (isTarget == TargetOnly)
420 Result.push_back(CodeGenIntrinsic(I[i]));
421 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000422 return Result;
423}
424
Dan Gohman907df572008-04-03 00:02:49 +0000425CodeGenIntrinsic::CodeGenIntrinsic(Record *R) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000426 TheDef = R;
427 std::string DefName = R->getName();
428 ModRef = WriteMem;
429 isOverloaded = false;
Evan Chengbb46d262008-06-16 20:29:38 +0000430 isCommutative = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000431
432 if (DefName.size() <= 4 ||
Bill Wendling9b4eab82008-11-13 09:08:33 +0000433 std::string(DefName.begin(), DefName.begin() + 4) != "int_")
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000434 throw "Intrinsic '" + DefName + "' does not start with 'int_'!";
Bill Wendling9b4eab82008-11-13 09:08:33 +0000435
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000436 EnumName = std::string(DefName.begin()+4, DefName.end());
Bill Wendling9b4eab82008-11-13 09:08:33 +0000437
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000438 if (R->getValue("GCCBuiltinName")) // Ignore a missing GCCBuiltinName field.
439 GCCBuiltinName = R->getValueAsString("GCCBuiltinName");
Bill Wendling9b4eab82008-11-13 09:08:33 +0000440
441 TargetPrefix = R->getValueAsString("TargetPrefix");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000442 Name = R->getValueAsString("LLVMName");
Bill Wendling9b4eab82008-11-13 09:08:33 +0000443
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000444 if (Name == "") {
445 // If an explicit name isn't specified, derive one from the DefName.
446 Name = "llvm.";
Bill Wendling9b4eab82008-11-13 09:08:33 +0000447
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000448 for (unsigned i = 0, e = EnumName.size(); i != e; ++i)
Bill Wendling9b4eab82008-11-13 09:08:33 +0000449 Name += (EnumName[i] == '_') ? '.' : EnumName[i];
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000450 } else {
451 // Verify it starts with "llvm.".
452 if (Name.size() <= 5 ||
Bill Wendling9b4eab82008-11-13 09:08:33 +0000453 std::string(Name.begin(), Name.begin() + 5) != "llvm.")
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000454 throw "Intrinsic '" + DefName + "'s name does not start with 'llvm.'!";
455 }
456
457 // If TargetPrefix is specified, make sure that Name starts with
458 // "llvm.<targetprefix>.".
459 if (!TargetPrefix.empty()) {
460 if (Name.size() < 6+TargetPrefix.size() ||
Bill Wendling9b4eab82008-11-13 09:08:33 +0000461 std::string(Name.begin() + 5, Name.begin() + 6 + TargetPrefix.size())
462 != (TargetPrefix + "."))
463 throw "Intrinsic '" + DefName + "' does not start with 'llvm." +
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000464 TargetPrefix + ".'!";
465 }
466
Bill Wendling9b4eab82008-11-13 09:08:33 +0000467 // Parse the list of return types.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000468 std::vector<MVT::SimpleValueType> OverloadedVTs;
Bill Wendling9b4eab82008-11-13 09:08:33 +0000469 ListInit *TypeList = R->getValueAsListInit("RetTypes");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000470 for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) {
471 Record *TyEl = TypeList->getElementAsRecord(i);
472 assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!");
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000473 MVT::SimpleValueType VT;
Bob Wilsonc7245f42009-01-07 00:09:01 +0000474 if (TyEl->isSubClassOf("LLVMMatchType")) {
Bob Wilson0f2b86a2009-04-16 21:51:05 +0000475 unsigned MatchTy = TyEl->getValueAsInt("Number");
476 assert(MatchTy < OverloadedVTs.size() &&
477 "Invalid matching number!");
478 VT = OverloadedVTs[MatchTy];
Bob Wilsonc7245f42009-01-07 00:09:01 +0000479 // It only makes sense to use the extended and truncated vector element
480 // variants with iAny types; otherwise, if the intrinsic is not
481 // overloaded, all the types can be specified directly.
482 assert(((!TyEl->isSubClassOf("LLVMExtendedElementVectorType") &&
483 !TyEl->isSubClassOf("LLVMTruncatedElementVectorType")) ||
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000484 VT == MVT::iAny || VT == MVT::vAny) &&
Bob Wilson7b70d4f2009-08-11 05:03:38 +0000485 "Expected iAny or vAny type");
Bob Wilson0f2b86a2009-04-16 21:51:05 +0000486 } else {
Bob Wilsonc7245f42009-01-07 00:09:01 +0000487 VT = getValueType(TyEl->getValueAsDef("VT"));
Bob Wilson0f2b86a2009-04-16 21:51:05 +0000488 }
Bob Wilsonf73db022009-08-11 01:14:02 +0000489 if (EVT(VT).isOverloaded()) {
Bob Wilson0f2b86a2009-04-16 21:51:05 +0000490 OverloadedVTs.push_back(VT);
491 isOverloaded |= true;
492 }
Bill Wendling9b4eab82008-11-13 09:08:33 +0000493 IS.RetVTs.push_back(VT);
494 IS.RetTypeDefs.push_back(TyEl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000495 }
Bill Wendling9b4eab82008-11-13 09:08:33 +0000496
497 if (IS.RetVTs.size() == 0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000498 throw "Intrinsic '"+DefName+"' needs at least a type for the ret value!";
499
Bill Wendling9b4eab82008-11-13 09:08:33 +0000500 // Parse the list of parameter types.
501 TypeList = R->getValueAsListInit("ParamTypes");
502 for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) {
503 Record *TyEl = TypeList->getElementAsRecord(i);
504 assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!");
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000505 MVT::SimpleValueType VT;
Bob Wilsonc7245f42009-01-07 00:09:01 +0000506 if (TyEl->isSubClassOf("LLVMMatchType")) {
507 unsigned MatchTy = TyEl->getValueAsInt("Number");
Bob Wilson0f2b86a2009-04-16 21:51:05 +0000508 assert(MatchTy < OverloadedVTs.size() &&
509 "Invalid matching number!");
510 VT = OverloadedVTs[MatchTy];
Bob Wilsonc7245f42009-01-07 00:09:01 +0000511 // It only makes sense to use the extended and truncated vector element
512 // variants with iAny types; otherwise, if the intrinsic is not
513 // overloaded, all the types can be specified directly.
514 assert(((!TyEl->isSubClassOf("LLVMExtendedElementVectorType") &&
515 !TyEl->isSubClassOf("LLVMTruncatedElementVectorType")) ||
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000516 VT == MVT::iAny || VT == MVT::vAny) &&
Bob Wilson7b70d4f2009-08-11 05:03:38 +0000517 "Expected iAny or vAny type");
Bob Wilsonc7245f42009-01-07 00:09:01 +0000518 } else
519 VT = getValueType(TyEl->getValueAsDef("VT"));
Bob Wilsonf73db022009-08-11 01:14:02 +0000520 if (EVT(VT).isOverloaded()) {
Bob Wilson0f2b86a2009-04-16 21:51:05 +0000521 OverloadedVTs.push_back(VT);
522 isOverloaded |= true;
523 }
Bill Wendling9b4eab82008-11-13 09:08:33 +0000524 IS.ParamVTs.push_back(VT);
525 IS.ParamTypeDefs.push_back(TyEl);
526 }
527
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000528 // Parse the intrinsic properties.
529 ListInit *PropList = R->getValueAsListInit("Properties");
530 for (unsigned i = 0, e = PropList->getSize(); i != e; ++i) {
531 Record *Property = PropList->getElementAsRecord(i);
532 assert(Property->isSubClassOf("IntrinsicProperty") &&
533 "Expected a property!");
534
535 if (Property->getName() == "IntrNoMem")
536 ModRef = NoMem;
537 else if (Property->getName() == "IntrReadArgMem")
538 ModRef = ReadArgMem;
539 else if (Property->getName() == "IntrReadMem")
540 ModRef = ReadMem;
541 else if (Property->getName() == "IntrWriteArgMem")
542 ModRef = WriteArgMem;
543 else if (Property->getName() == "IntrWriteMem")
544 ModRef = WriteMem;
Evan Chengbb46d262008-06-16 20:29:38 +0000545 else if (Property->getName() == "Commutative")
546 isCommutative = true;
Chris Lattner1b0090f2009-01-12 01:12:03 +0000547 else if (Property->isSubClassOf("NoCapture")) {
548 unsigned ArgNo = Property->getValueAsInt("ArgNo");
549 ArgumentAttributes.push_back(std::make_pair(ArgNo, NoCapture));
550 } else
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000551 assert(0 && "Unknown property!");
552 }
553}