blob: 53dbbf86f14ae4e14d0f2fef1db844fc438c1674 [file] [log] [blame]
Chris Lattner6cc654b2008-01-06 01:35:39 +00001//===- CodeGenTarget.cpp - CodeGen Target Class Wrapper -------------------===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
John Criswell01d45822003-10-20 20:20:30 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner30609102007-12-29 20:37:13 +00005// This file is distributed under the University of Illinois Open Source
6// 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 Lattner6cc654b2008-01-06 01:35:39 +000010// This class wraps 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 Lattner75ee2eb2005-10-14 03:54:49 +000023#include <algorithm>
Chris Lattner2082ebe2004-08-01 03:55:39 +000024using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000025
Chris Lattner560a79f2004-10-03 19:34:31 +000026static cl::opt<unsigned>
27AsmWriterNum("asmwriternum", cl::init(0),
28 cl::desc("Make -gen-asm-writer emit assembly writer #N"));
29
Chris Lattner45872072003-08-07 05:38:11 +000030/// getValueType - Return the MCV::ValueType that the specified TableGen record
31/// corresponds to.
Dan Gohman3bf6e182007-07-13 20:16:50 +000032MVT::ValueType llvm::getValueType(Record *Rec) {
Evan Cheng2618d072006-05-17 20:37:59 +000033 return (MVT::ValueType)Rec->getValueAsInt("Value");
Chris Lattner45872072003-08-07 05:38:11 +000034}
35
Chris Lattner2082ebe2004-08-01 03:55:39 +000036std::string llvm::getName(MVT::ValueType T) {
Chris Lattner45872072003-08-07 05:38:11 +000037 switch (T) {
Chris Lattnerd3464c12003-08-07 23:15:21 +000038 case MVT::Other: return "UNKNOWN";
Evan Chengd7c2c862006-06-15 00:16:37 +000039 case MVT::i1: return "MVT::i1";
40 case MVT::i8: return "MVT::i8";
41 case MVT::i16: return "MVT::i16";
42 case MVT::i32: return "MVT::i32";
43 case MVT::i64: return "MVT::i64";
44 case MVT::i128: return "MVT::i128";
Reid Spencerc4de3de2007-04-01 07:20:02 +000045 case MVT::iAny: return "MVT::iAny";
Dan Gohman0fee3ff2007-08-16 21:57:19 +000046 case MVT::fAny: return "MVT::fAny";
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";
Dale Johannesen317096a2007-09-28 01:08:20 +000051 case MVT::ppcf128: return "MVT::ppcf128";
Evan Chengd7c2c862006-06-15 00:16:37 +000052 case MVT::Flag: return "MVT::Flag";
53 case MVT::isVoid:return "MVT::void";
54 case MVT::v8i8: return "MVT::v8i8";
55 case MVT::v4i16: return "MVT::v4i16";
56 case MVT::v2i32: return "MVT::v2i32";
Bill Wendlingeebc8a12007-03-26 07:53:08 +000057 case MVT::v1i64: return "MVT::v1i64";
Evan Chengd7c2c862006-06-15 00:16:37 +000058 case MVT::v16i8: return "MVT::v16i8";
59 case MVT::v8i16: return "MVT::v8i16";
60 case MVT::v4i32: return "MVT::v4i32";
61 case MVT::v2i64: return "MVT::v2i64";
62 case MVT::v2f32: return "MVT::v2f32";
63 case MVT::v4f32: return "MVT::v4f32";
64 case MVT::v2f64: return "MVT::v2f64";
Christopher Lamb82455102007-07-26 06:41:18 +000065 case MVT::v3i32: return "MVT::v3i32";
66 case MVT::v3f32: return "MVT::v3f32";
Evan Cheng6b125162006-05-17 20:55:51 +000067 case MVT::iPTR: return "TLI.getPointerTy()";
Chris Lattnerd3464c12003-08-07 23:15:21 +000068 default: assert(0 && "ILLEGAL VALUE TYPE!"); return "";
Chris Lattner45872072003-08-07 05:38:11 +000069 }
Chris Lattnerd3464c12003-08-07 23:15:21 +000070}
71
Chris Lattner2082ebe2004-08-01 03:55:39 +000072std::string llvm::getEnumName(MVT::ValueType T) {
Chris Lattnerb72fb7e2003-08-10 19:50:32 +000073 switch (T) {
Evan Cheng2618d072006-05-17 20:37:59 +000074 case MVT::Other: return "MVT::Other";
75 case MVT::i1: return "MVT::i1";
76 case MVT::i8: return "MVT::i8";
77 case MVT::i16: return "MVT::i16";
78 case MVT::i32: return "MVT::i32";
79 case MVT::i64: return "MVT::i64";
80 case MVT::i128: return "MVT::i128";
Reid Spencerc4de3de2007-04-01 07:20:02 +000081 case MVT::iAny: return "MVT::iAny";
Dan Gohman0fee3ff2007-08-16 21:57:19 +000082 case MVT::fAny: return "MVT::fAny";
Evan Cheng2618d072006-05-17 20:37:59 +000083 case MVT::f32: return "MVT::f32";
84 case MVT::f64: return "MVT::f64";
85 case MVT::f80: return "MVT::f80";
86 case MVT::f128: return "MVT::f128";
Dale Johannesen317096a2007-09-28 01:08:20 +000087 case MVT::ppcf128: return "MVT::ppcf128";
Evan Cheng2618d072006-05-17 20:37:59 +000088 case MVT::Flag: return "MVT::Flag";
89 case MVT::isVoid:return "MVT::isVoid";
90 case MVT::v8i8: return "MVT::v8i8";
91 case MVT::v4i16: return "MVT::v4i16";
92 case MVT::v2i32: return "MVT::v2i32";
Bill Wendlingeebc8a12007-03-26 07:53:08 +000093 case MVT::v1i64: return "MVT::v1i64";
Evan Cheng2618d072006-05-17 20:37:59 +000094 case MVT::v16i8: return "MVT::v16i8";
95 case MVT::v8i16: return "MVT::v8i16";
96 case MVT::v4i32: return "MVT::v4i32";
97 case MVT::v2i64: return "MVT::v2i64";
98 case MVT::v2f32: return "MVT::v2f32";
99 case MVT::v4f32: return "MVT::v4f32";
100 case MVT::v2f64: return "MVT::v2f64";
Christopher Lamb82455102007-07-26 06:41:18 +0000101 case MVT::v3i32: return "MVT::v3i32";
102 case MVT::v3f32: return "MVT::v3f32";
Chandler Carruth69940402007-08-04 01:51:18 +0000103 case MVT::iPTR: return "MVT::iPTR";
Chris Lattnerb72fb7e2003-08-10 19:50:32 +0000104 default: assert(0 && "ILLEGAL VALUE TYPE!"); return "";
105 }
106}
107
Chris Lattner6cefb772008-01-05 22:25:12 +0000108/// getQualifiedName - Return the name of the specified record, with a
109/// namespace qualifier if the record contains one.
110///
111std::string llvm::getQualifiedName(const Record *R) {
112 std::string Namespace = R->getValueAsString("Namespace");
113 if (Namespace.empty()) return R->getName();
114 return Namespace + "::" + R->getName();
115}
116
117
118
Chris Lattnerd3464c12003-08-07 23:15:21 +0000119
Chris Lattner45872072003-08-07 05:38:11 +0000120/// getTarget - Return the current instance of the Target class.
121///
Evan Cheng2618d072006-05-17 20:37:59 +0000122CodeGenTarget::CodeGenTarget() {
Chris Lattner45872072003-08-07 05:38:11 +0000123 std::vector<Record*> Targets = Records.getAllDerivedDefinitions("Target");
Misha Brukmanbebdb202004-06-04 14:59:42 +0000124 if (Targets.size() == 0)
Misha Brukman3da94ae2005-04-22 00:00:37 +0000125 throw std::string("ERROR: No 'Target' subclasses defined!");
Chris Lattner45872072003-08-07 05:38:11 +0000126 if (Targets.size() != 1)
127 throw std::string("ERROR: Multiple subclasses of Target defined!");
128 TargetRec = Targets[0];
Chris Lattner45872072003-08-07 05:38:11 +0000129}
130
131
132const std::string &CodeGenTarget::getName() const {
133 return TargetRec->getName();
134}
135
136Record *CodeGenTarget::getInstructionSet() const {
137 return TargetRec->getValueAsDef("InstructionSet");
138}
Brian Gaeked0fde302003-11-11 22:41:34 +0000139
Chris Lattner175580c2004-08-14 22:50:53 +0000140/// getAsmWriter - Return the AssemblyWriter definition for this target.
141///
142Record *CodeGenTarget::getAsmWriter() const {
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000143 std::vector<Record*> LI = TargetRec->getValueAsListOfDefs("AssemblyWriters");
144 if (AsmWriterNum >= LI.size())
Chris Lattner560a79f2004-10-03 19:34:31 +0000145 throw "Target does not have an AsmWriter #" + utostr(AsmWriterNum) + "!";
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000146 return LI[AsmWriterNum];
Chris Lattner175580c2004-08-14 22:50:53 +0000147}
148
Chris Lattner26693112004-08-16 01:10:21 +0000149void CodeGenTarget::ReadRegisters() const {
150 std::vector<Record*> Regs = Records.getAllDerivedDefinitions("Register");
151 if (Regs.empty())
152 throw std::string("No 'Register' subclasses defined!");
153
154 Registers.reserve(Regs.size());
155 Registers.assign(Regs.begin(), Regs.end());
156}
157
Chris Lattner7a680c62004-08-21 02:24:57 +0000158CodeGenRegister::CodeGenRegister(Record *R) : TheDef(R) {
159 DeclaredSpillSize = R->getValueAsInt("SpillSize");
160 DeclaredSpillAlignment = R->getValueAsInt("SpillAlignment");
161}
162
Chris Lattner26693112004-08-16 01:10:21 +0000163const std::string &CodeGenRegister::getName() const {
164 return TheDef->getName();
165}
166
Chris Lattner056afef2004-08-21 04:05:00 +0000167void CodeGenTarget::ReadRegisterClasses() const {
168 std::vector<Record*> RegClasses =
169 Records.getAllDerivedDefinitions("RegisterClass");
170 if (RegClasses.empty())
171 throw std::string("No 'RegisterClass' subclasses defined!");
172
173 RegisterClasses.reserve(RegClasses.size());
174 RegisterClasses.assign(RegClasses.begin(), RegClasses.end());
175}
176
Evan Cheng44a65fa2006-05-16 07:05:30 +0000177std::vector<unsigned char> CodeGenTarget::getRegisterVTs(Record *R) const {
178 std::vector<unsigned char> Result;
179 const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses();
180 for (unsigned i = 0, e = RCs.size(); i != e; ++i) {
181 const CodeGenRegisterClass &RC = RegisterClasses[i];
182 for (unsigned ei = 0, ee = RC.Elements.size(); ei != ee; ++ei) {
183 if (R == RC.Elements[ei]) {
184 const std::vector<MVT::ValueType> &InVTs = RC.getValueTypes();
185 for (unsigned i = 0, e = InVTs.size(); i != e; ++i)
186 Result.push_back(InVTs[i]);
187 }
188 }
189 }
190 return Result;
191}
192
193
Chris Lattner056afef2004-08-21 04:05:00 +0000194CodeGenRegisterClass::CodeGenRegisterClass(Record *R) : TheDef(R) {
Chris Lattnerc67c18f2005-08-19 18:45:20 +0000195 // Rename anonymous register classes.
196 if (R->getName().size() > 9 && R->getName()[9] == '.') {
197 static unsigned AnonCounter = 0;
198 R->setName("AnonRegClass_"+utostr(AnonCounter++));
199 }
200
Nate Begeman6510b222005-12-01 04:51:06 +0000201 std::vector<Record*> TypeList = R->getValueAsListOfDefs("RegTypes");
202 for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
203 Record *Type = TypeList[i];
204 if (!Type->isSubClassOf("ValueType"))
205 throw "RegTypes list member '" + Type->getName() +
206 "' does not derive from the ValueType class!";
207 VTs.push_back(getValueType(Type));
208 }
209 assert(!VTs.empty() && "RegisterClass must contain at least one ValueType!");
Chris Lattnerac468932005-08-19 19:12:51 +0000210
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000211 std::vector<Record*> RegList = R->getValueAsListOfDefs("MemberList");
212 for (unsigned i = 0, e = RegList.size(); i != e; ++i) {
213 Record *Reg = RegList[i];
Chris Lattner056afef2004-08-21 04:05:00 +0000214 if (!Reg->isSubClassOf("Register"))
215 throw "Register Class member '" + Reg->getName() +
216 "' does not derive from the Register class!";
217 Elements.push_back(Reg);
218 }
Nate Begeman6510b222005-12-01 04:51:06 +0000219
Christopher Lamba3211252007-06-13 22:20:15 +0000220 std::vector<Record*> SubRegClassList =
221 R->getValueAsListOfDefs("SubRegClassList");
222 for (unsigned i = 0, e = SubRegClassList.size(); i != e; ++i) {
223 Record *SubRegClass = SubRegClassList[i];
224 if (!SubRegClass->isSubClassOf("RegisterClass"))
225 throw "Register Class member '" + SubRegClass->getName() +
226 "' does not derive from the RegisterClass class!";
227 SubRegClasses.push_back(SubRegClass);
228 }
229
Nate Begeman6510b222005-12-01 04:51:06 +0000230 // Allow targets to override the size in bits of the RegisterClass.
231 unsigned Size = R->getValueAsInt("Size");
232
233 Namespace = R->getValueAsString("Namespace");
234 SpillSize = Size ? Size : MVT::getSizeInBits(VTs[0]);
235 SpillAlignment = R->getValueAsInt("Alignment");
Evan Chenga3ca3142007-09-19 01:35:01 +0000236 CopyCost = R->getValueAsInt("CopyCost");
Nate Begeman6510b222005-12-01 04:51:06 +0000237 MethodBodies = R->getValueAsCode("MethodBodies");
238 MethodProtos = R->getValueAsCode("MethodProtos");
Chris Lattner056afef2004-08-21 04:05:00 +0000239}
240
241const std::string &CodeGenRegisterClass::getName() const {
242 return TheDef->getName();
243}
244
Chris Lattnere9f4ba82005-09-08 21:43:21 +0000245void CodeGenTarget::ReadLegalValueTypes() const {
246 const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses();
247 for (unsigned i = 0, e = RCs.size(); i != e; ++i)
Nate Begeman6510b222005-12-01 04:51:06 +0000248 for (unsigned ri = 0, re = RCs[i].VTs.size(); ri != re; ++ri)
249 LegalValueTypes.push_back(RCs[i].VTs[ri]);
Chris Lattner75ee2eb2005-10-14 03:54:49 +0000250
251 // Remove duplicates.
252 std::sort(LegalValueTypes.begin(), LegalValueTypes.end());
253 LegalValueTypes.erase(std::unique(LegalValueTypes.begin(),
254 LegalValueTypes.end()),
255 LegalValueTypes.end());
Chris Lattnere9f4ba82005-09-08 21:43:21 +0000256}
Chris Lattner056afef2004-08-21 04:05:00 +0000257
Chris Lattner175580c2004-08-14 22:50:53 +0000258
Chris Lattnerec352402004-08-01 05:04:00 +0000259void CodeGenTarget::ReadInstructions() const {
260 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
Chris Lattneraa77d772006-01-27 01:45:06 +0000261 if (Insts.size() <= 2)
Chris Lattnerec352402004-08-01 05:04:00 +0000262 throw std::string("No 'Instruction' subclasses defined!");
263
Chris Lattneraa77d772006-01-27 01:45:06 +0000264 // Parse the instructions defined in the .td file.
Chris Lattner175580c2004-08-14 22:50:53 +0000265 std::string InstFormatName =
266 getAsmWriter()->getValueAsString("InstFormatName");
267
268 for (unsigned i = 0, e = Insts.size(); i != e; ++i) {
269 std::string AsmStr = Insts[i]->getValueAsString(InstFormatName);
270 Instructions.insert(std::make_pair(Insts[i]->getName(),
271 CodeGenInstruction(Insts[i], AsmStr)));
272 }
Chris Lattnerec352402004-08-01 05:04:00 +0000273}
274
Chris Lattnerd6488672005-01-22 18:58:51 +0000275/// getInstructionsByEnumValue - Return all of the instructions defined by the
276/// target, ordered by their enum value.
277void CodeGenTarget::
278getInstructionsByEnumValue(std::vector<const CodeGenInstruction*>
279 &NumberedInstructions) {
Chris Lattneraa77d772006-01-27 01:45:06 +0000280 std::map<std::string, CodeGenInstruction>::const_iterator I;
281 I = getInstructions().find("PHI");
282 if (I == Instructions.end()) throw "Could not find 'PHI' instruction!";
283 const CodeGenInstruction *PHI = &I->second;
284
285 I = getInstructions().find("INLINEASM");
286 if (I == Instructions.end()) throw "Could not find 'INLINEASM' instruction!";
287 const CodeGenInstruction *INLINEASM = &I->second;
288
Jim Laskeya683f9b2007-01-26 17:29:20 +0000289 I = getInstructions().find("LABEL");
290 if (I == Instructions.end()) throw "Could not find 'LABEL' instruction!";
291 const CodeGenInstruction *LABEL = &I->second;
292
Christopher Lamb08d52072007-07-26 07:48:21 +0000293 I = getInstructions().find("EXTRACT_SUBREG");
294 if (I == Instructions.end())
295 throw "Could not find 'EXTRACT_SUBREG' instruction!";
296 const CodeGenInstruction *EXTRACT_SUBREG = &I->second;
297
298 I = getInstructions().find("INSERT_SUBREG");
299 if (I == Instructions.end())
300 throw "Could not find 'INSERT_SUBREG' instruction!";
301 const CodeGenInstruction *INSERT_SUBREG = &I->second;
302
Chris Lattnerd6488672005-01-22 18:58:51 +0000303 // Print out the rest of the instructions now.
Chris Lattnerd6488672005-01-22 18:58:51 +0000304 NumberedInstructions.push_back(PHI);
Chris Lattneraa77d772006-01-27 01:45:06 +0000305 NumberedInstructions.push_back(INLINEASM);
Jim Laskeya683f9b2007-01-26 17:29:20 +0000306 NumberedInstructions.push_back(LABEL);
Christopher Lamb08d52072007-07-26 07:48:21 +0000307 NumberedInstructions.push_back(EXTRACT_SUBREG);
308 NumberedInstructions.push_back(INSERT_SUBREG);
Chris Lattnerd6488672005-01-22 18:58:51 +0000309 for (inst_iterator II = inst_begin(), E = inst_end(); II != E; ++II)
Jim Laskeya683f9b2007-01-26 17:29:20 +0000310 if (&II->second != PHI &&
311 &II->second != INLINEASM &&
Christopher Lamb08d52072007-07-26 07:48:21 +0000312 &II->second != LABEL &&
313 &II->second != EXTRACT_SUBREG &&
314 &II->second != INSERT_SUBREG)
Chris Lattnerd6488672005-01-22 18:58:51 +0000315 NumberedInstructions.push_back(&II->second);
316}
317
318
Misha Brukman35e83cc2004-10-14 05:50:43 +0000319/// isLittleEndianEncoding - Return whether this target encodes its instruction
320/// in little-endian format, i.e. bits laid out in the order [0..n]
321///
322bool CodeGenTarget::isLittleEndianEncoding() const {
323 return getInstructionSet()->getValueAsBit("isLittleEndianEncoding");
324}
325
Evan Cheng0fc71982005-12-08 02:00:36 +0000326//===----------------------------------------------------------------------===//
327// ComplexPattern implementation
328//
329ComplexPattern::ComplexPattern(Record *R) {
Evan Cheng3aa39f42005-12-08 02:14:08 +0000330 Ty = ::getValueType(R->getValueAsDef("Ty"));
331 NumOperands = R->getValueAsInt("NumOperands");
332 SelectFunc = R->getValueAsString("SelectFunc");
333 RootNodes = R->getValueAsListOfDefs("RootNodes");
Evan Cheng94b30402006-10-11 21:02:01 +0000334
335 // Parse the properties.
336 Properties = 0;
337 std::vector<Record*> PropList = R->getValueAsListOfDefs("Properties");
338 for (unsigned i = 0, e = PropList.size(); i != e; ++i)
339 if (PropList[i]->getName() == "SDNPHasChain") {
340 Properties |= 1 << SDNPHasChain;
341 } else if (PropList[i]->getName() == "SDNPOptInFlag") {
342 Properties |= 1 << SDNPOptInFlag;
343 } else {
Bill Wendlingf5da1332006-12-07 22:21:48 +0000344 cerr << "Unsupported SD Node property '" << PropList[i]->getName()
345 << "' on ComplexPattern '" << R->getName() << "'!\n";
Evan Cheng94b30402006-10-11 21:02:01 +0000346 exit(1);
347 }
Evan Cheng0fc71982005-12-08 02:00:36 +0000348}
Evan Cheng3aa39f42005-12-08 02:14:08 +0000349
Chris Lattner43fbbc32006-03-24 19:49:31 +0000350//===----------------------------------------------------------------------===//
351// CodeGenIntrinsic Implementation
352//===----------------------------------------------------------------------===//
353
354std::vector<CodeGenIntrinsic> llvm::LoadIntrinsics(const RecordKeeper &RC) {
355 std::vector<Record*> I = RC.getAllDerivedDefinitions("Intrinsic");
Chris Lattner8850a1b2006-03-27 22:48:18 +0000356
357 std::vector<CodeGenIntrinsic> Result;
Chris Lattner6a160fb2006-03-28 00:15:00 +0000358
359 // If we are in the context of a target .td file, get the target info so that
360 // we can decode the current intptr_t.
361 CodeGenTarget *CGT = 0;
362 if (Records.getClass("Target") &&
363 Records.getAllDerivedDefinitions("Target").size() == 1)
364 CGT = new CodeGenTarget();
365
Chris Lattner8850a1b2006-03-27 22:48:18 +0000366 for (unsigned i = 0, e = I.size(); i != e; ++i)
Chris Lattner6a160fb2006-03-28 00:15:00 +0000367 Result.push_back(CodeGenIntrinsic(I[i], CGT));
368 delete CGT;
Chris Lattner8850a1b2006-03-27 22:48:18 +0000369 return Result;
Chris Lattner43fbbc32006-03-24 19:49:31 +0000370}
371
Chris Lattner76f8c7c2006-03-28 00:03:08 +0000372CodeGenIntrinsic::CodeGenIntrinsic(Record *R, CodeGenTarget *CGT) {
Chris Lattner2ca956f2006-03-24 20:25:01 +0000373 TheDef = R;
Chris Lattner43fbbc32006-03-24 19:49:31 +0000374 std::string DefName = R->getName();
375 ModRef = WriteMem;
Reid Spencerc4de3de2007-04-01 07:20:02 +0000376 isOverloaded = false;
Chris Lattner43fbbc32006-03-24 19:49:31 +0000377
378 if (DefName.size() <= 4 ||
379 std::string(DefName.begin(), DefName.begin()+4) != "int_")
380 throw "Intrinsic '" + DefName + "' does not start with 'int_'!";
381 EnumName = std::string(DefName.begin()+4, DefName.end());
382 if (R->getValue("GCCBuiltinName")) // Ignore a missing GCCBuiltinName field.
383 GCCBuiltinName = R->getValueAsString("GCCBuiltinName");
384 TargetPrefix = R->getValueAsString("TargetPrefix");
385 Name = R->getValueAsString("LLVMName");
386 if (Name == "") {
387 // If an explicit name isn't specified, derive one from the DefName.
388 Name = "llvm.";
389 for (unsigned i = 0, e = EnumName.size(); i != e; ++i)
390 if (EnumName[i] == '_')
391 Name += '.';
392 else
393 Name += EnumName[i];
394 } else {
395 // Verify it starts with "llvm.".
396 if (Name.size() <= 5 ||
397 std::string(Name.begin(), Name.begin()+5) != "llvm.")
398 throw "Intrinsic '" + DefName + "'s name does not start with 'llvm.'!";
399 }
400
401 // If TargetPrefix is specified, make sure that Name starts with
402 // "llvm.<targetprefix>.".
403 if (!TargetPrefix.empty()) {
404 if (Name.size() < 6+TargetPrefix.size() ||
405 std::string(Name.begin()+5, Name.begin()+6+TargetPrefix.size())
406 != (TargetPrefix+"."))
407 throw "Intrinsic '" + DefName + "' does not start with 'llvm." +
408 TargetPrefix + ".'!";
409 }
410
411 // Parse the list of argument types.
412 ListInit *TypeList = R->getValueAsListInit("Types");
413 for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) {
Chris Lattner50d45652007-02-27 22:08:27 +0000414 Record *TyEl = TypeList->getElementAsRecord(i);
Chris Lattner43fbbc32006-03-24 19:49:31 +0000415 assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!");
Dan Gohman3bf6e182007-07-13 20:16:50 +0000416 MVT::ValueType VT = getValueType(TyEl->getValueAsDef("VT"));
Dan Gohman0fee3ff2007-08-16 21:57:19 +0000417 isOverloaded |= VT == MVT::iAny || VT == MVT::fAny;
Reid Spencerc4de3de2007-04-01 07:20:02 +0000418 ArgVTs.push_back(VT);
Chris Lattner43fbbc32006-03-24 19:49:31 +0000419 ArgTypeDefs.push_back(TyEl);
420 }
Chandler Carruth69940402007-08-04 01:51:18 +0000421 if (ArgVTs.size() == 0)
Chris Lattner43fbbc32006-03-24 19:49:31 +0000422 throw "Intrinsic '"+DefName+"' needs at least a type for the ret value!";
Reid Spencerc4de3de2007-04-01 07:20:02 +0000423
Chris Lattner43fbbc32006-03-24 19:49:31 +0000424
425 // Parse the intrinsic properties.
426 ListInit *PropList = R->getValueAsListInit("Properties");
427 for (unsigned i = 0, e = PropList->getSize(); i != e; ++i) {
Chris Lattner50d45652007-02-27 22:08:27 +0000428 Record *Property = PropList->getElementAsRecord(i);
Chris Lattner43fbbc32006-03-24 19:49:31 +0000429 assert(Property->isSubClassOf("IntrinsicProperty") &&
430 "Expected a property!");
431
Chris Lattner4b2362e2006-04-10 22:02:59 +0000432 if (Property->getName() == "IntrNoMem")
Chris Lattner43fbbc32006-03-24 19:49:31 +0000433 ModRef = NoMem;
Chris Lattner4b2362e2006-04-10 22:02:59 +0000434 else if (Property->getName() == "IntrReadArgMem")
Chris Lattner43fbbc32006-03-24 19:49:31 +0000435 ModRef = ReadArgMem;
436 else if (Property->getName() == "IntrReadMem")
437 ModRef = ReadMem;
Chris Lattner4b2362e2006-04-10 22:02:59 +0000438 else if (Property->getName() == "IntrWriteArgMem")
Chris Lattner43fbbc32006-03-24 19:49:31 +0000439 ModRef = WriteArgMem;
440 else if (Property->getName() == "IntrWriteMem")
441 ModRef = WriteMem;
442 else
443 assert(0 && "Unknown property!");
444 }
445}