blob: ea0746fa3372a038a881d06c84064aa223e917aa [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
Chris Lattner82d9a642010-03-19 00:07:20 +0000141
142CodeGenInstruction &CodeGenTarget::getInstruction(const Record *InstRec) const {
143 return getInstruction(InstRec->getName());
144}
145
146
Daniel Dunbar85f1b392009-07-29 00:02:19 +0000147/// getAsmParser - Return the AssemblyParser definition for this target.
148///
149Record *CodeGenTarget::getAsmParser() const {
150 std::vector<Record*> LI = TargetRec->getValueAsListOfDefs("AssemblyParsers");
151 if (AsmParserNum >= LI.size())
152 throw "Target does not have an AsmParser #" + utostr(AsmParserNum) + "!";
153 return LI[AsmParserNum];
154}
155
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000156/// getAsmWriter - Return the AssemblyWriter definition for this target.
157///
158Record *CodeGenTarget::getAsmWriter() const {
159 std::vector<Record*> LI = TargetRec->getValueAsListOfDefs("AssemblyWriters");
160 if (AsmWriterNum >= LI.size())
161 throw "Target does not have an AsmWriter #" + utostr(AsmWriterNum) + "!";
162 return LI[AsmWriterNum];
163}
164
165void CodeGenTarget::ReadRegisters() const {
166 std::vector<Record*> Regs = Records.getAllDerivedDefinitions("Register");
167 if (Regs.empty())
168 throw std::string("No 'Register' subclasses defined!");
169
170 Registers.reserve(Regs.size());
171 Registers.assign(Regs.begin(), Regs.end());
172}
173
174CodeGenRegister::CodeGenRegister(Record *R) : TheDef(R) {
175 DeclaredSpillSize = R->getValueAsInt("SpillSize");
176 DeclaredSpillAlignment = R->getValueAsInt("SpillAlignment");
177}
178
179const std::string &CodeGenRegister::getName() const {
180 return TheDef->getName();
181}
182
183void CodeGenTarget::ReadRegisterClasses() const {
184 std::vector<Record*> RegClasses =
185 Records.getAllDerivedDefinitions("RegisterClass");
186 if (RegClasses.empty())
187 throw std::string("No 'RegisterClass' subclasses defined!");
188
189 RegisterClasses.reserve(RegClasses.size());
190 RegisterClasses.assign(RegClasses.begin(), RegClasses.end());
191}
192
Chris Lattner3f9bead2010-03-15 06:00:16 +0000193std::vector<MVT::SimpleValueType> CodeGenTarget::
194getRegisterVTs(Record *R) const {
195 std::vector<MVT::SimpleValueType> Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000196 const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses();
197 for (unsigned i = 0, e = RCs.size(); i != e; ++i) {
198 const CodeGenRegisterClass &RC = RegisterClasses[i];
199 for (unsigned ei = 0, ee = RC.Elements.size(); ei != ee; ++ei) {
200 if (R == RC.Elements[ei]) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000201 const std::vector<MVT::SimpleValueType> &InVTs = RC.getValueTypes();
Chris Lattner3f9bead2010-03-15 06:00:16 +0000202 Result.insert(Result.end(), InVTs.begin(), InVTs.end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000203 }
204 }
205 }
206 return Result;
207}
208
209
210CodeGenRegisterClass::CodeGenRegisterClass(Record *R) : TheDef(R) {
211 // Rename anonymous register classes.
212 if (R->getName().size() > 9 && R->getName()[9] == '.') {
213 static unsigned AnonCounter = 0;
214 R->setName("AnonRegClass_"+utostr(AnonCounter++));
215 }
216
217 std::vector<Record*> TypeList = R->getValueAsListOfDefs("RegTypes");
218 for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
219 Record *Type = TypeList[i];
220 if (!Type->isSubClassOf("ValueType"))
221 throw "RegTypes list member '" + Type->getName() +
222 "' does not derive from the ValueType class!";
223 VTs.push_back(getValueType(Type));
224 }
225 assert(!VTs.empty() && "RegisterClass must contain at least one ValueType!");
226
227 std::vector<Record*> RegList = R->getValueAsListOfDefs("MemberList");
228 for (unsigned i = 0, e = RegList.size(); i != e; ++i) {
229 Record *Reg = RegList[i];
230 if (!Reg->isSubClassOf("Register"))
231 throw "Register Class member '" + Reg->getName() +
232 "' does not derive from the Register class!";
233 Elements.push_back(Reg);
234 }
235
236 std::vector<Record*> SubRegClassList =
237 R->getValueAsListOfDefs("SubRegClassList");
238 for (unsigned i = 0, e = SubRegClassList.size(); i != e; ++i) {
239 Record *SubRegClass = SubRegClassList[i];
240 if (!SubRegClass->isSubClassOf("RegisterClass"))
241 throw "Register Class member '" + SubRegClass->getName() +
242 "' does not derive from the RegisterClass class!";
243 SubRegClasses.push_back(SubRegClass);
244 }
245
246 // Allow targets to override the size in bits of the RegisterClass.
247 unsigned Size = R->getValueAsInt("Size");
248
249 Namespace = R->getValueAsString("Namespace");
Owen Andersonac9de032009-08-10 22:56:29 +0000250 SpillSize = Size ? Size : EVT(VTs[0]).getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000251 SpillAlignment = R->getValueAsInt("Alignment");
Evan Cheng77ac1822007-09-19 01:35:01 +0000252 CopyCost = R->getValueAsInt("CopyCost");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000253 MethodBodies = R->getValueAsCode("MethodBodies");
254 MethodProtos = R->getValueAsCode("MethodProtos");
255}
256
257const std::string &CodeGenRegisterClass::getName() const {
258 return TheDef->getName();
259}
260
261void CodeGenTarget::ReadLegalValueTypes() const {
262 const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses();
263 for (unsigned i = 0, e = RCs.size(); i != e; ++i)
264 for (unsigned ri = 0, re = RCs[i].VTs.size(); ri != re; ++ri)
265 LegalValueTypes.push_back(RCs[i].VTs[ri]);
266
267 // Remove duplicates.
268 std::sort(LegalValueTypes.begin(), LegalValueTypes.end());
269 LegalValueTypes.erase(std::unique(LegalValueTypes.begin(),
270 LegalValueTypes.end()),
271 LegalValueTypes.end());
272}
273
274
275void CodeGenTarget::ReadInstructions() const {
276 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
277 if (Insts.size() <= 2)
278 throw std::string("No 'Instruction' subclasses defined!");
279
280 // Parse the instructions defined in the .td file.
281 std::string InstFormatName =
282 getAsmWriter()->getValueAsString("InstFormatName");
283
284 for (unsigned i = 0, e = Insts.size(); i != e; ++i) {
285 std::string AsmStr = Insts[i]->getValueAsString(InstFormatName);
286 Instructions.insert(std::make_pair(Insts[i]->getName(),
287 CodeGenInstruction(Insts[i], AsmStr)));
288 }
289}
290
Chris Lattnere864fde2010-03-19 00:23:20 +0000291static const CodeGenInstruction *
292GetInstByName(const char *Name,
293 const std::map<std::string, CodeGenInstruction> &Insts) {
294 std::map<std::string, CodeGenInstruction>::const_iterator
295 I = Insts.find(Name);
296 if (I == Insts.end())
297 throw std::string("Could not find '") + Name + "' instruction!";
298 return &I->second;
299}
300
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000301/// getInstructionsByEnumValue - Return all of the instructions defined by the
302/// target, ordered by their enum value.
303void CodeGenTarget::
304getInstructionsByEnumValue(std::vector<const CodeGenInstruction*>
305 &NumberedInstructions) {
Chris Lattnere864fde2010-03-19 00:23:20 +0000306 const std::map<std::string, CodeGenInstruction> &Insts = getInstructions();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000307
Chris Lattnere864fde2010-03-19 00:23:20 +0000308 const CodeGenInstruction *PHI = GetInstByName("PHI", Insts);
309 const CodeGenInstruction *INLINEASM = GetInstByName("INLINEASM", Insts);
310 const CodeGenInstruction *DBG_LABEL = GetInstByName("DBG_LABEL", Insts);
311 const CodeGenInstruction *EH_LABEL = GetInstByName("EH_LABEL", Insts);
312 const CodeGenInstruction *GC_LABEL = GetInstByName("GC_LABEL", Insts);
313 const CodeGenInstruction *KILL = GetInstByName("KILL", Insts);
314 const CodeGenInstruction *EXTRACT_SUBREG =
315 GetInstByName("EXTRACT_SUBREG", Insts);
316 const CodeGenInstruction *INSERT_SUBREG =
317 GetInstByName("INSERT_SUBREG", Insts);
318 const CodeGenInstruction *IMPLICIT_DEF = GetInstByName("IMPLICIT_DEF", Insts);
319 const CodeGenInstruction *SUBREG_TO_REG =
320 GetInstByName("SUBREG_TO_REG", Insts);
321 const CodeGenInstruction *COPY_TO_REGCLASS =
322 GetInstByName("COPY_TO_REGCLASS", Insts);
323 const CodeGenInstruction *DBG_VALUE = GetInstByName("DBG_VALUE", Insts);
Dale Johannesen4b04d8a2010-01-08 23:51:25 +0000324
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000325 // Print out the rest of the instructions now.
326 NumberedInstructions.push_back(PHI);
327 NumberedInstructions.push_back(INLINEASM);
Dan Gohmanfa607c92008-07-01 00:05:16 +0000328 NumberedInstructions.push_back(DBG_LABEL);
329 NumberedInstructions.push_back(EH_LABEL);
330 NumberedInstructions.push_back(GC_LABEL);
Jakob Stoklund Olesen8f12c7c2009-09-28 20:32:26 +0000331 NumberedInstructions.push_back(KILL);
Christopher Lamb071a2a72007-07-26 07:48:21 +0000332 NumberedInstructions.push_back(EXTRACT_SUBREG);
333 NumberedInstructions.push_back(INSERT_SUBREG);
Evan Cheng3c0eda52008-03-15 00:03:38 +0000334 NumberedInstructions.push_back(IMPLICIT_DEF);
Christopher Lamb76d72da2008-03-16 03:12:01 +0000335 NumberedInstructions.push_back(SUBREG_TO_REG);
Dan Gohman4c10fc72009-04-13 21:06:25 +0000336 NumberedInstructions.push_back(COPY_TO_REGCLASS);
Chris Lattner4052b292010-02-09 19:54:29 +0000337 NumberedInstructions.push_back(DBG_VALUE);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000338 for (inst_iterator II = inst_begin(), E = inst_end(); II != E; ++II)
339 if (&II->second != PHI &&
340 &II->second != INLINEASM &&
Dan Gohmanfa607c92008-07-01 00:05:16 +0000341 &II->second != DBG_LABEL &&
342 &II->second != EH_LABEL &&
343 &II->second != GC_LABEL &&
Jakob Stoklund Olesen8f12c7c2009-09-28 20:32:26 +0000344 &II->second != KILL &&
Christopher Lamb071a2a72007-07-26 07:48:21 +0000345 &II->second != EXTRACT_SUBREG &&
Evan Cheng3c0eda52008-03-15 00:03:38 +0000346 &II->second != INSERT_SUBREG &&
Christopher Lamb76d72da2008-03-16 03:12:01 +0000347 &II->second != IMPLICIT_DEF &&
Dan Gohman1aeb5db2009-04-13 15:38:05 +0000348 &II->second != SUBREG_TO_REG &&
Dale Johannesen4b04d8a2010-01-08 23:51:25 +0000349 &II->second != COPY_TO_REGCLASS &&
Chris Lattner4052b292010-02-09 19:54:29 +0000350 &II->second != DBG_VALUE)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000351 NumberedInstructions.push_back(&II->second);
352}
353
354
355/// isLittleEndianEncoding - Return whether this target encodes its instruction
356/// in little-endian format, i.e. bits laid out in the order [0..n]
357///
358bool CodeGenTarget::isLittleEndianEncoding() const {
359 return getInstructionSet()->getValueAsBit("isLittleEndianEncoding");
360}
361
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000362//===----------------------------------------------------------------------===//
363// ComplexPattern implementation
364//
365ComplexPattern::ComplexPattern(Record *R) {
366 Ty = ::getValueType(R->getValueAsDef("Ty"));
367 NumOperands = R->getValueAsInt("NumOperands");
368 SelectFunc = R->getValueAsString("SelectFunc");
369 RootNodes = R->getValueAsListOfDefs("RootNodes");
370
371 // Parse the properties.
372 Properties = 0;
373 std::vector<Record*> PropList = R->getValueAsListOfDefs("Properties");
374 for (unsigned i = 0, e = PropList.size(); i != e; ++i)
375 if (PropList[i]->getName() == "SDNPHasChain") {
376 Properties |= 1 << SDNPHasChain;
377 } else if (PropList[i]->getName() == "SDNPOptInFlag") {
378 Properties |= 1 << SDNPOptInFlag;
Chris Lattnerc90ee9c2008-01-10 07:59:24 +0000379 } else if (PropList[i]->getName() == "SDNPMayStore") {
380 Properties |= 1 << SDNPMayStore;
381 } else if (PropList[i]->getName() == "SDNPMayLoad") {
382 Properties |= 1 << SDNPMayLoad;
383 } else if (PropList[i]->getName() == "SDNPSideEffect") {
384 Properties |= 1 << SDNPSideEffect;
Mon P Wang6bde9ec2008-06-25 08:15:39 +0000385 } else if (PropList[i]->getName() == "SDNPMemOperand") {
386 Properties |= 1 << SDNPMemOperand;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000387 } else {
Daniel Dunbard4287062009-07-03 00:10:29 +0000388 errs() << "Unsupported SD Node property '" << PropList[i]->getName()
389 << "' on ComplexPattern '" << R->getName() << "'!\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000390 exit(1);
391 }
392}
393
394//===----------------------------------------------------------------------===//
395// CodeGenIntrinsic Implementation
396//===----------------------------------------------------------------------===//
397
Dale Johannesenfe5921a2009-02-05 01:49:45 +0000398std::vector<CodeGenIntrinsic> llvm::LoadIntrinsics(const RecordKeeper &RC,
399 bool TargetOnly) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000400 std::vector<Record*> I = RC.getAllDerivedDefinitions("Intrinsic");
401
402 std::vector<CodeGenIntrinsic> Result;
403
Dale Johannesenfe5921a2009-02-05 01:49:45 +0000404 for (unsigned i = 0, e = I.size(); i != e; ++i) {
405 bool isTarget = I[i]->getValueAsBit("isTarget");
406 if (isTarget == TargetOnly)
407 Result.push_back(CodeGenIntrinsic(I[i]));
408 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000409 return Result;
410}
411
Dan Gohman907df572008-04-03 00:02:49 +0000412CodeGenIntrinsic::CodeGenIntrinsic(Record *R) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000413 TheDef = R;
414 std::string DefName = R->getName();
415 ModRef = WriteMem;
416 isOverloaded = false;
Evan Chengbb46d262008-06-16 20:29:38 +0000417 isCommutative = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000418
419 if (DefName.size() <= 4 ||
Bill Wendling9b4eab82008-11-13 09:08:33 +0000420 std::string(DefName.begin(), DefName.begin() + 4) != "int_")
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000421 throw "Intrinsic '" + DefName + "' does not start with 'int_'!";
Bill Wendling9b4eab82008-11-13 09:08:33 +0000422
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000423 EnumName = std::string(DefName.begin()+4, DefName.end());
Bill Wendling9b4eab82008-11-13 09:08:33 +0000424
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000425 if (R->getValue("GCCBuiltinName")) // Ignore a missing GCCBuiltinName field.
426 GCCBuiltinName = R->getValueAsString("GCCBuiltinName");
Bill Wendling9b4eab82008-11-13 09:08:33 +0000427
428 TargetPrefix = R->getValueAsString("TargetPrefix");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000429 Name = R->getValueAsString("LLVMName");
Bill Wendling9b4eab82008-11-13 09:08:33 +0000430
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000431 if (Name == "") {
432 // If an explicit name isn't specified, derive one from the DefName.
433 Name = "llvm.";
Bill Wendling9b4eab82008-11-13 09:08:33 +0000434
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000435 for (unsigned i = 0, e = EnumName.size(); i != e; ++i)
Bill Wendling9b4eab82008-11-13 09:08:33 +0000436 Name += (EnumName[i] == '_') ? '.' : EnumName[i];
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000437 } else {
438 // Verify it starts with "llvm.".
439 if (Name.size() <= 5 ||
Bill Wendling9b4eab82008-11-13 09:08:33 +0000440 std::string(Name.begin(), Name.begin() + 5) != "llvm.")
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000441 throw "Intrinsic '" + DefName + "'s name does not start with 'llvm.'!";
442 }
443
444 // If TargetPrefix is specified, make sure that Name starts with
445 // "llvm.<targetprefix>.".
446 if (!TargetPrefix.empty()) {
447 if (Name.size() < 6+TargetPrefix.size() ||
Bill Wendling9b4eab82008-11-13 09:08:33 +0000448 std::string(Name.begin() + 5, Name.begin() + 6 + TargetPrefix.size())
449 != (TargetPrefix + "."))
450 throw "Intrinsic '" + DefName + "' does not start with 'llvm." +
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000451 TargetPrefix + ".'!";
452 }
453
Bill Wendling9b4eab82008-11-13 09:08:33 +0000454 // Parse the list of return types.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000455 std::vector<MVT::SimpleValueType> OverloadedVTs;
Bill Wendling9b4eab82008-11-13 09:08:33 +0000456 ListInit *TypeList = R->getValueAsListInit("RetTypes");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000457 for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) {
458 Record *TyEl = TypeList->getElementAsRecord(i);
459 assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!");
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000460 MVT::SimpleValueType VT;
Bob Wilsonc7245f42009-01-07 00:09:01 +0000461 if (TyEl->isSubClassOf("LLVMMatchType")) {
Bob Wilson0f2b86a2009-04-16 21:51:05 +0000462 unsigned MatchTy = TyEl->getValueAsInt("Number");
463 assert(MatchTy < OverloadedVTs.size() &&
464 "Invalid matching number!");
465 VT = OverloadedVTs[MatchTy];
Bob Wilsonc7245f42009-01-07 00:09:01 +0000466 // It only makes sense to use the extended and truncated vector element
467 // variants with iAny types; otherwise, if the intrinsic is not
468 // overloaded, all the types can be specified directly.
469 assert(((!TyEl->isSubClassOf("LLVMExtendedElementVectorType") &&
470 !TyEl->isSubClassOf("LLVMTruncatedElementVectorType")) ||
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000471 VT == MVT::iAny || VT == MVT::vAny) &&
Bob Wilson7b70d4f2009-08-11 05:03:38 +0000472 "Expected iAny or vAny type");
Bob Wilson0f2b86a2009-04-16 21:51:05 +0000473 } else {
Bob Wilsonc7245f42009-01-07 00:09:01 +0000474 VT = getValueType(TyEl->getValueAsDef("VT"));
Bob Wilson0f2b86a2009-04-16 21:51:05 +0000475 }
Bob Wilsonf73db022009-08-11 01:14:02 +0000476 if (EVT(VT).isOverloaded()) {
Bob Wilson0f2b86a2009-04-16 21:51:05 +0000477 OverloadedVTs.push_back(VT);
478 isOverloaded |= true;
479 }
Bill Wendling9b4eab82008-11-13 09:08:33 +0000480 IS.RetVTs.push_back(VT);
481 IS.RetTypeDefs.push_back(TyEl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000482 }
Bill Wendling9b4eab82008-11-13 09:08:33 +0000483
484 if (IS.RetVTs.size() == 0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000485 throw "Intrinsic '"+DefName+"' needs at least a type for the ret value!";
486
Bill Wendling9b4eab82008-11-13 09:08:33 +0000487 // Parse the list of parameter types.
488 TypeList = R->getValueAsListInit("ParamTypes");
489 for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) {
490 Record *TyEl = TypeList->getElementAsRecord(i);
491 assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!");
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000492 MVT::SimpleValueType VT;
Bob Wilsonc7245f42009-01-07 00:09:01 +0000493 if (TyEl->isSubClassOf("LLVMMatchType")) {
494 unsigned MatchTy = TyEl->getValueAsInt("Number");
Bob Wilson0f2b86a2009-04-16 21:51:05 +0000495 assert(MatchTy < OverloadedVTs.size() &&
496 "Invalid matching number!");
497 VT = OverloadedVTs[MatchTy];
Bob Wilsonc7245f42009-01-07 00:09:01 +0000498 // It only makes sense to use the extended and truncated vector element
499 // variants with iAny types; otherwise, if the intrinsic is not
500 // overloaded, all the types can be specified directly.
501 assert(((!TyEl->isSubClassOf("LLVMExtendedElementVectorType") &&
502 !TyEl->isSubClassOf("LLVMTruncatedElementVectorType")) ||
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000503 VT == MVT::iAny || VT == MVT::vAny) &&
Bob Wilson7b70d4f2009-08-11 05:03:38 +0000504 "Expected iAny or vAny type");
Bob Wilsonc7245f42009-01-07 00:09:01 +0000505 } else
506 VT = getValueType(TyEl->getValueAsDef("VT"));
Bob Wilsonf73db022009-08-11 01:14:02 +0000507 if (EVT(VT).isOverloaded()) {
Bob Wilson0f2b86a2009-04-16 21:51:05 +0000508 OverloadedVTs.push_back(VT);
509 isOverloaded |= true;
510 }
Bill Wendling9b4eab82008-11-13 09:08:33 +0000511 IS.ParamVTs.push_back(VT);
512 IS.ParamTypeDefs.push_back(TyEl);
513 }
514
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000515 // Parse the intrinsic properties.
516 ListInit *PropList = R->getValueAsListInit("Properties");
517 for (unsigned i = 0, e = PropList->getSize(); i != e; ++i) {
518 Record *Property = PropList->getElementAsRecord(i);
519 assert(Property->isSubClassOf("IntrinsicProperty") &&
520 "Expected a property!");
521
522 if (Property->getName() == "IntrNoMem")
523 ModRef = NoMem;
524 else if (Property->getName() == "IntrReadArgMem")
525 ModRef = ReadArgMem;
526 else if (Property->getName() == "IntrReadMem")
527 ModRef = ReadMem;
528 else if (Property->getName() == "IntrWriteArgMem")
529 ModRef = WriteArgMem;
530 else if (Property->getName() == "IntrWriteMem")
531 ModRef = WriteMem;
Evan Chengbb46d262008-06-16 20:29:38 +0000532 else if (Property->getName() == "Commutative")
533 isCommutative = true;
Chris Lattner1b0090f2009-01-12 01:12:03 +0000534 else if (Property->isSubClassOf("NoCapture")) {
535 unsigned ArgNo = Property->getValueAsInt("ArgNo");
536 ArgumentAttributes.push_back(std::make_pair(ArgNo, NoCapture));
537 } else
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000538 assert(0 && "Unknown property!");
539 }
540}