blob: de36eb0902ddd2765ef74401616dc22636c6a6de [file] [log] [blame]
Chris Lattner803a5f62004-08-01 04:04:35 +00001//===- CodeGenTarget.cpp - CodeGen Target Class Wrapper ---------*- C++ -*-===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
John Criswell01d45822003-10-20 20:20:30 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman3da94ae2005-04-22 00:00:37 +00007//
John Criswell01d45822003-10-20 20:20:30 +00008//===----------------------------------------------------------------------===//
Chris Lattner45872072003-08-07 05:38:11 +00009//
Chris Lattner803a5f62004-08-01 04:04:35 +000010// This class wrap target description classes used by the various code
Chris Lattner45872072003-08-07 05:38:11 +000011// generation TableGen backends. This makes it easier to access the data and
12// provides a single place that needs to check it for validity. All of these
13// classes throw exceptions on error conditions.
14//
15//===----------------------------------------------------------------------===//
16
Chris Lattner803a5f62004-08-01 04:04:35 +000017#include "CodeGenTarget.h"
Chris Lattner43fbbc32006-03-24 19:49:31 +000018#include "CodeGenIntrinsics.h"
Chris Lattner45872072003-08-07 05:38:11 +000019#include "Record.h"
Chris Lattner560a79f2004-10-03 19:34:31 +000020#include "llvm/ADT/StringExtras.h"
21#include "llvm/Support/CommandLine.h"
Chris Lattner5d7d3db2005-09-14 21:05:02 +000022#include <set>
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.
Chris Lattner8850a1b2006-03-27 22:48:18 +000032MVT::ValueType llvm::getValueType(Record *Rec, const CodeGenTarget *CGT) {
33 MVT::ValueType VT = (MVT::ValueType)Rec->getValueAsInt("Value");
34 if (VT == MVT::iPTR) {
35 assert(CGT && "Use a pointer type in a place that isn't supported yet!");
36 VT = CGT->getPointerType();
37 }
38 return VT;
Chris Lattner45872072003-08-07 05:38:11 +000039}
40
Chris Lattner2082ebe2004-08-01 03:55:39 +000041std::string llvm::getName(MVT::ValueType T) {
Chris Lattner45872072003-08-07 05:38:11 +000042 switch (T) {
Chris Lattnerd3464c12003-08-07 23:15:21 +000043 case MVT::Other: return "UNKNOWN";
44 case MVT::i1: return "i1";
45 case MVT::i8: return "i8";
46 case MVT::i16: return "i16";
47 case MVT::i32: return "i32";
48 case MVT::i64: return "i64";
49 case MVT::i128: return "i128";
50 case MVT::f32: return "f32";
51 case MVT::f64: return "f64";
52 case MVT::f80: return "f80";
53 case MVT::f128: return "f128";
Evan Chengbcecf332005-12-17 01:19:28 +000054 case MVT::Flag: return "Flag";
Chris Lattnerd3464c12003-08-07 23:15:21 +000055 case MVT::isVoid:return "void";
Evan Chengaea20f52006-02-20 22:34:53 +000056 case MVT::v8i8: return "v8i8";
57 case MVT::v4i16: return "v4i16";
58 case MVT::v2i32: return "v2i32";
Nate Begeman02fc8ff2005-11-29 06:19:38 +000059 case MVT::v16i8: return "v16i8";
60 case MVT::v8i16: return "v8i16";
61 case MVT::v4i32: return "v4i32";
62 case MVT::v2i64: return "v2i64";
Evan Cheng9073dea2006-03-01 01:10:52 +000063 case MVT::v2f32: return "v2f32";
Nate Begeman02fc8ff2005-11-29 06:19:38 +000064 case MVT::v4f32: return "v4f32";
65 case MVT::v2f64: return "v2f64";
Chris Lattnerd3464c12003-08-07 23:15:21 +000066 default: assert(0 && "ILLEGAL VALUE TYPE!"); return "";
Chris Lattner45872072003-08-07 05:38:11 +000067 }
Chris Lattnerd3464c12003-08-07 23:15:21 +000068}
69
Chris Lattner2082ebe2004-08-01 03:55:39 +000070std::string llvm::getEnumName(MVT::ValueType T) {
Chris Lattnerb72fb7e2003-08-10 19:50:32 +000071 switch (T) {
72 case MVT::Other: return "Other";
73 case MVT::i1: return "i1";
74 case MVT::i8: return "i8";
75 case MVT::i16: return "i16";
76 case MVT::i32: return "i32";
77 case MVT::i64: return "i64";
78 case MVT::i128: return "i128";
79 case MVT::f32: return "f32";
80 case MVT::f64: return "f64";
81 case MVT::f80: return "f80";
82 case MVT::f128: return "f128";
Evan Chengbcecf332005-12-17 01:19:28 +000083 case MVT::Flag: return "Flag";
Chris Lattnerb72fb7e2003-08-10 19:50:32 +000084 case MVT::isVoid:return "isVoid";
Evan Chengdcfccac2006-03-19 07:57:34 +000085 case MVT::v8i8: return "v8i8";
86 case MVT::v4i16: return "v4i16";
87 case MVT::v2i32: return "v2i32";
Nate Begeman02fc8ff2005-11-29 06:19:38 +000088 case MVT::v16i8: return "v16i8";
89 case MVT::v8i16: return "v8i16";
90 case MVT::v4i32: return "v4i32";
91 case MVT::v2i64: return "v2i64";
Evan Cheng9073dea2006-03-01 01:10:52 +000092 case MVT::v2f32: return "v2f32";
Nate Begeman02fc8ff2005-11-29 06:19:38 +000093 case MVT::v4f32: return "v4f32";
94 case MVT::v2f64: return "v2f64";
Chris Lattnerb72fb7e2003-08-10 19:50:32 +000095 default: assert(0 && "ILLEGAL VALUE TYPE!"); return "";
96 }
97}
98
Chris Lattnerd3464c12003-08-07 23:15:21 +000099
Chris Lattner2082ebe2004-08-01 03:55:39 +0000100std::ostream &llvm::operator<<(std::ostream &OS, MVT::ValueType T) {
Chris Lattnerd3464c12003-08-07 23:15:21 +0000101 return OS << getName(T);
Chris Lattner45872072003-08-07 05:38:11 +0000102}
103
104
Chris Lattner45872072003-08-07 05:38:11 +0000105/// getTarget - Return the current instance of the Target class.
106///
Brian Gaekebe7f4af2003-10-10 21:55:29 +0000107CodeGenTarget::CodeGenTarget() : PointerType(MVT::Other) {
Chris Lattner45872072003-08-07 05:38:11 +0000108 std::vector<Record*> Targets = Records.getAllDerivedDefinitions("Target");
Misha Brukmanbebdb202004-06-04 14:59:42 +0000109 if (Targets.size() == 0)
Misha Brukman3da94ae2005-04-22 00:00:37 +0000110 throw std::string("ERROR: No 'Target' subclasses defined!");
Chris Lattner45872072003-08-07 05:38:11 +0000111 if (Targets.size() != 1)
112 throw std::string("ERROR: Multiple subclasses of Target defined!");
113 TargetRec = Targets[0];
114
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000115 // Read in all of the CalleeSavedRegisters.
116 CalleeSavedRegisters =TargetRec->getValueAsListOfDefs("CalleeSavedRegisters");
Chris Lattner45872072003-08-07 05:38:11 +0000117 PointerType = getValueType(TargetRec->getValueAsDef("PointerType"));
118}
119
120
121const std::string &CodeGenTarget::getName() const {
122 return TargetRec->getName();
123}
124
125Record *CodeGenTarget::getInstructionSet() const {
126 return TargetRec->getValueAsDef("InstructionSet");
127}
Brian Gaeked0fde302003-11-11 22:41:34 +0000128
Chris Lattner175580c2004-08-14 22:50:53 +0000129/// getAsmWriter - Return the AssemblyWriter definition for this target.
130///
131Record *CodeGenTarget::getAsmWriter() const {
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000132 std::vector<Record*> LI = TargetRec->getValueAsListOfDefs("AssemblyWriters");
133 if (AsmWriterNum >= LI.size())
Chris Lattner560a79f2004-10-03 19:34:31 +0000134 throw "Target does not have an AsmWriter #" + utostr(AsmWriterNum) + "!";
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000135 return LI[AsmWriterNum];
Chris Lattner175580c2004-08-14 22:50:53 +0000136}
137
Chris Lattner26693112004-08-16 01:10:21 +0000138void CodeGenTarget::ReadRegisters() const {
139 std::vector<Record*> Regs = Records.getAllDerivedDefinitions("Register");
140 if (Regs.empty())
141 throw std::string("No 'Register' subclasses defined!");
142
143 Registers.reserve(Regs.size());
144 Registers.assign(Regs.begin(), Regs.end());
145}
146
Chris Lattner7a680c62004-08-21 02:24:57 +0000147CodeGenRegister::CodeGenRegister(Record *R) : TheDef(R) {
148 DeclaredSpillSize = R->getValueAsInt("SpillSize");
149 DeclaredSpillAlignment = R->getValueAsInt("SpillAlignment");
150}
151
Chris Lattner26693112004-08-16 01:10:21 +0000152const std::string &CodeGenRegister::getName() const {
153 return TheDef->getName();
154}
155
Chris Lattner056afef2004-08-21 04:05:00 +0000156void CodeGenTarget::ReadRegisterClasses() const {
157 std::vector<Record*> RegClasses =
158 Records.getAllDerivedDefinitions("RegisterClass");
159 if (RegClasses.empty())
160 throw std::string("No 'RegisterClass' subclasses defined!");
161
162 RegisterClasses.reserve(RegClasses.size());
163 RegisterClasses.assign(RegClasses.begin(), RegClasses.end());
164}
165
166CodeGenRegisterClass::CodeGenRegisterClass(Record *R) : TheDef(R) {
Chris Lattnerc67c18f2005-08-19 18:45:20 +0000167 // Rename anonymous register classes.
168 if (R->getName().size() > 9 && R->getName()[9] == '.') {
169 static unsigned AnonCounter = 0;
170 R->setName("AnonRegClass_"+utostr(AnonCounter++));
171 }
172
Nate Begeman6510b222005-12-01 04:51:06 +0000173 std::vector<Record*> TypeList = R->getValueAsListOfDefs("RegTypes");
174 for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
175 Record *Type = TypeList[i];
176 if (!Type->isSubClassOf("ValueType"))
177 throw "RegTypes list member '" + Type->getName() +
178 "' does not derive from the ValueType class!";
179 VTs.push_back(getValueType(Type));
180 }
181 assert(!VTs.empty() && "RegisterClass must contain at least one ValueType!");
Chris Lattnerac468932005-08-19 19:12:51 +0000182
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000183 std::vector<Record*> RegList = R->getValueAsListOfDefs("MemberList");
184 for (unsigned i = 0, e = RegList.size(); i != e; ++i) {
185 Record *Reg = RegList[i];
Chris Lattner056afef2004-08-21 04:05:00 +0000186 if (!Reg->isSubClassOf("Register"))
187 throw "Register Class member '" + Reg->getName() +
188 "' does not derive from the Register class!";
189 Elements.push_back(Reg);
190 }
Nate Begeman6510b222005-12-01 04:51:06 +0000191
192 // Allow targets to override the size in bits of the RegisterClass.
193 unsigned Size = R->getValueAsInt("Size");
194
195 Namespace = R->getValueAsString("Namespace");
196 SpillSize = Size ? Size : MVT::getSizeInBits(VTs[0]);
197 SpillAlignment = R->getValueAsInt("Alignment");
198 MethodBodies = R->getValueAsCode("MethodBodies");
199 MethodProtos = R->getValueAsCode("MethodProtos");
Chris Lattner056afef2004-08-21 04:05:00 +0000200}
201
202const std::string &CodeGenRegisterClass::getName() const {
203 return TheDef->getName();
204}
205
Chris Lattnere9f4ba82005-09-08 21:43:21 +0000206void CodeGenTarget::ReadLegalValueTypes() const {
207 const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses();
208 for (unsigned i = 0, e = RCs.size(); i != e; ++i)
Nate Begeman6510b222005-12-01 04:51:06 +0000209 for (unsigned ri = 0, re = RCs[i].VTs.size(); ri != re; ++ri)
210 LegalValueTypes.push_back(RCs[i].VTs[ri]);
Chris Lattner75ee2eb2005-10-14 03:54:49 +0000211
212 // Remove duplicates.
213 std::sort(LegalValueTypes.begin(), LegalValueTypes.end());
214 LegalValueTypes.erase(std::unique(LegalValueTypes.begin(),
215 LegalValueTypes.end()),
216 LegalValueTypes.end());
Chris Lattnere9f4ba82005-09-08 21:43:21 +0000217}
Chris Lattner056afef2004-08-21 04:05:00 +0000218
Chris Lattner175580c2004-08-14 22:50:53 +0000219
Chris Lattnerec352402004-08-01 05:04:00 +0000220void CodeGenTarget::ReadInstructions() const {
221 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
Chris Lattneraa77d772006-01-27 01:45:06 +0000222 if (Insts.size() <= 2)
Chris Lattnerec352402004-08-01 05:04:00 +0000223 throw std::string("No 'Instruction' subclasses defined!");
224
Chris Lattneraa77d772006-01-27 01:45:06 +0000225 // Parse the instructions defined in the .td file.
Chris Lattner175580c2004-08-14 22:50:53 +0000226 std::string InstFormatName =
227 getAsmWriter()->getValueAsString("InstFormatName");
228
229 for (unsigned i = 0, e = Insts.size(); i != e; ++i) {
230 std::string AsmStr = Insts[i]->getValueAsString(InstFormatName);
231 Instructions.insert(std::make_pair(Insts[i]->getName(),
232 CodeGenInstruction(Insts[i], AsmStr)));
233 }
Chris Lattnerec352402004-08-01 05:04:00 +0000234}
235
Chris Lattnerd6488672005-01-22 18:58:51 +0000236/// getInstructionsByEnumValue - Return all of the instructions defined by the
237/// target, ordered by their enum value.
238void CodeGenTarget::
239getInstructionsByEnumValue(std::vector<const CodeGenInstruction*>
240 &NumberedInstructions) {
Chris Lattneraa77d772006-01-27 01:45:06 +0000241 std::map<std::string, CodeGenInstruction>::const_iterator I;
242 I = getInstructions().find("PHI");
243 if (I == Instructions.end()) throw "Could not find 'PHI' instruction!";
244 const CodeGenInstruction *PHI = &I->second;
245
246 I = getInstructions().find("INLINEASM");
247 if (I == Instructions.end()) throw "Could not find 'INLINEASM' instruction!";
248 const CodeGenInstruction *INLINEASM = &I->second;
249
Chris Lattnerd6488672005-01-22 18:58:51 +0000250 // Print out the rest of the instructions now.
Chris Lattnerd6488672005-01-22 18:58:51 +0000251 NumberedInstructions.push_back(PHI);
Chris Lattneraa77d772006-01-27 01:45:06 +0000252 NumberedInstructions.push_back(INLINEASM);
Chris Lattnerd6488672005-01-22 18:58:51 +0000253 for (inst_iterator II = inst_begin(), E = inst_end(); II != E; ++II)
Chris Lattneraa77d772006-01-27 01:45:06 +0000254 if (&II->second != PHI &&&II->second != INLINEASM)
Chris Lattnerd6488672005-01-22 18:58:51 +0000255 NumberedInstructions.push_back(&II->second);
256}
257
258
Misha Brukman35e83cc2004-10-14 05:50:43 +0000259/// isLittleEndianEncoding - Return whether this target encodes its instruction
260/// in little-endian format, i.e. bits laid out in the order [0..n]
261///
262bool CodeGenTarget::isLittleEndianEncoding() const {
263 return getInstructionSet()->getValueAsBit("isLittleEndianEncoding");
264}
265
Chris Lattner175580c2004-08-14 22:50:53 +0000266CodeGenInstruction::CodeGenInstruction(Record *R, const std::string &AsmStr)
267 : TheDef(R), AsmString(AsmStr) {
Chris Lattnerec352402004-08-01 05:04:00 +0000268 Name = R->getValueAsString("Name");
269 Namespace = R->getValueAsString("Namespace");
Chris Lattnerec352402004-08-01 05:04:00 +0000270
Chris Lattnerec352402004-08-01 05:04:00 +0000271 isReturn = R->getValueAsBit("isReturn");
272 isBranch = R->getValueAsBit("isBranch");
273 isBarrier = R->getValueAsBit("isBarrier");
274 isCall = R->getValueAsBit("isCall");
Nate Begemancdd66b52004-09-28 21:01:45 +0000275 isLoad = R->getValueAsBit("isLoad");
276 isStore = R->getValueAsBit("isStore");
Chris Lattnerec352402004-08-01 05:04:00 +0000277 isTwoAddress = R->getValueAsBit("isTwoAddress");
Chris Lattneraad75aa2005-01-02 02:29:04 +0000278 isConvertibleToThreeAddress = R->getValueAsBit("isConvertibleToThreeAddress");
279 isCommutable = R->getValueAsBit("isCommutable");
Chris Lattnerec352402004-08-01 05:04:00 +0000280 isTerminator = R->getValueAsBit("isTerminator");
Chris Lattner5b71d3a2004-09-28 18:38:01 +0000281 hasDelaySlot = R->getValueAsBit("hasDelaySlot");
Chris Lattnere3cbf822005-08-26 20:55:40 +0000282 usesCustomDAGSchedInserter = R->getValueAsBit("usesCustomDAGSchedInserter");
Evan Cheng1c3d19e2005-12-04 08:18:16 +0000283 hasCtrlDep = R->getValueAsBit("hasCtrlDep");
Evan Cheng2b4ea792005-12-26 09:11:45 +0000284 noResults = R->getValueAsBit("noResults");
Chris Lattnercfbf96a2005-08-18 23:38:41 +0000285 hasVariableNumberOfOperands = false;
286
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000287 DagInit *DI;
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000288 try {
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000289 DI = R->getValueAsDag("OperandList");
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000290 } catch (...) {
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000291 // Error getting operand list, just ignore it (sparcv9).
Chris Lattner87c59052004-08-01 07:42:39 +0000292 AsmString.clear();
293 OperandList.clear();
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000294 return;
295 }
296
297 unsigned MIOperandNo = 0;
298 std::set<std::string> OperandNames;
299 for (unsigned i = 0, e = DI->getNumArgs(); i != e; ++i) {
300 DefInit *Arg = dynamic_cast<DefInit*>(DI->getArg(i));
301 if (!Arg)
302 throw "Illegal operand for the '" + R->getName() + "' instruction!";
303
304 Record *Rec = Arg->getDef();
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000305 std::string PrintMethod = "printOperand";
306 unsigned NumOps = 1;
Chris Lattner33670792005-11-19 07:48:33 +0000307 DagInit *MIOpInfo = 0;
Nate Begeman86193d12005-12-01 00:12:04 +0000308 if (Rec->isSubClassOf("Operand")) {
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000309 PrintMethod = Rec->getValueAsString("PrintMethod");
310 NumOps = Rec->getValueAsInt("NumMIOperands");
Chris Lattner65303d62005-11-19 07:05:57 +0000311 MIOpInfo = Rec->getValueAsDag("MIOperandInfo");
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000312 } else if (Rec->getName() == "variable_ops") {
313 hasVariableNumberOfOperands = true;
314 continue;
Nate Begeman86193d12005-12-01 00:12:04 +0000315 } else if (!Rec->isSubClassOf("RegisterClass"))
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000316 throw "Unknown operand class '" + Rec->getName() +
317 "' in instruction '" + R->getName() + "' instruction!";
318
Chris Lattnerc4a8b732005-09-14 21:13:50 +0000319 // Check that the operand has a name and that it's unique.
320 if (DI->getArgName(i).empty())
321 throw "In instruction '" + R->getName() + "', operand #" + utostr(i) +
322 " has no name!";
323 if (!OperandNames.insert(DI->getArgName(i)).second)
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000324 throw "In instruction '" + R->getName() + "', operand #" + utostr(i) +
325 " has the same name as a previous operand!";
326
Nate Begeman86193d12005-12-01 00:12:04 +0000327 OperandList.push_back(OperandInfo(Rec, DI->getArgName(i), PrintMethod,
328 MIOperandNo, NumOps, MIOpInfo));
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000329 MIOperandNo += NumOps;
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000330 }
Chris Lattnerec352402004-08-01 05:04:00 +0000331}
332
333
Chris Lattner87c59052004-08-01 07:42:39 +0000334
335/// getOperandNamed - Return the index of the operand with the specified
336/// non-empty name. If the instruction does not have an operand with the
337/// specified name, throw an exception.
Misha Brukman35e83cc2004-10-14 05:50:43 +0000338///
Chris Lattner87c59052004-08-01 07:42:39 +0000339unsigned CodeGenInstruction::getOperandNamed(const std::string &Name) const {
340 assert(!Name.empty() && "Cannot search for operand with no name!");
341 for (unsigned i = 0, e = OperandList.size(); i != e; ++i)
342 if (OperandList[i].Name == Name) return i;
343 throw "Instruction '" + TheDef->getName() +
344 "' does not have an operand named '$" + Name + "'!";
345}
Evan Cheng0fc71982005-12-08 02:00:36 +0000346
347//===----------------------------------------------------------------------===//
348// ComplexPattern implementation
349//
350ComplexPattern::ComplexPattern(Record *R) {
Evan Cheng3aa39f42005-12-08 02:14:08 +0000351 Ty = ::getValueType(R->getValueAsDef("Ty"));
352 NumOperands = R->getValueAsInt("NumOperands");
353 SelectFunc = R->getValueAsString("SelectFunc");
354 RootNodes = R->getValueAsListOfDefs("RootNodes");
Evan Cheng0fc71982005-12-08 02:00:36 +0000355}
Evan Cheng3aa39f42005-12-08 02:14:08 +0000356
Chris Lattner43fbbc32006-03-24 19:49:31 +0000357//===----------------------------------------------------------------------===//
358// CodeGenIntrinsic Implementation
359//===----------------------------------------------------------------------===//
360
361std::vector<CodeGenIntrinsic> llvm::LoadIntrinsics(const RecordKeeper &RC) {
362 std::vector<Record*> I = RC.getAllDerivedDefinitions("Intrinsic");
Chris Lattner8850a1b2006-03-27 22:48:18 +0000363
364 std::vector<CodeGenIntrinsic> Result;
Chris Lattner6a160fb2006-03-28 00:15:00 +0000365
366 // If we are in the context of a target .td file, get the target info so that
367 // we can decode the current intptr_t.
368 CodeGenTarget *CGT = 0;
369 if (Records.getClass("Target") &&
370 Records.getAllDerivedDefinitions("Target").size() == 1)
371 CGT = new CodeGenTarget();
372
Chris Lattner8850a1b2006-03-27 22:48:18 +0000373 for (unsigned i = 0, e = I.size(); i != e; ++i)
Chris Lattner6a160fb2006-03-28 00:15:00 +0000374 Result.push_back(CodeGenIntrinsic(I[i], CGT));
375 delete CGT;
Chris Lattner8850a1b2006-03-27 22:48:18 +0000376 return Result;
Chris Lattner43fbbc32006-03-24 19:49:31 +0000377}
378
Chris Lattner76f8c7c2006-03-28 00:03:08 +0000379CodeGenIntrinsic::CodeGenIntrinsic(Record *R, CodeGenTarget *CGT) {
Chris Lattner2ca956f2006-03-24 20:25:01 +0000380 TheDef = R;
Chris Lattner43fbbc32006-03-24 19:49:31 +0000381 std::string DefName = R->getName();
382 ModRef = WriteMem;
383
384 if (DefName.size() <= 4 ||
385 std::string(DefName.begin(), DefName.begin()+4) != "int_")
386 throw "Intrinsic '" + DefName + "' does not start with 'int_'!";
387 EnumName = std::string(DefName.begin()+4, DefName.end());
388 if (R->getValue("GCCBuiltinName")) // Ignore a missing GCCBuiltinName field.
389 GCCBuiltinName = R->getValueAsString("GCCBuiltinName");
390 TargetPrefix = R->getValueAsString("TargetPrefix");
391 Name = R->getValueAsString("LLVMName");
392 if (Name == "") {
393 // If an explicit name isn't specified, derive one from the DefName.
394 Name = "llvm.";
395 for (unsigned i = 0, e = EnumName.size(); i != e; ++i)
396 if (EnumName[i] == '_')
397 Name += '.';
398 else
399 Name += EnumName[i];
400 } else {
401 // Verify it starts with "llvm.".
402 if (Name.size() <= 5 ||
403 std::string(Name.begin(), Name.begin()+5) != "llvm.")
404 throw "Intrinsic '" + DefName + "'s name does not start with 'llvm.'!";
405 }
406
407 // If TargetPrefix is specified, make sure that Name starts with
408 // "llvm.<targetprefix>.".
409 if (!TargetPrefix.empty()) {
410 if (Name.size() < 6+TargetPrefix.size() ||
411 std::string(Name.begin()+5, Name.begin()+6+TargetPrefix.size())
412 != (TargetPrefix+"."))
413 throw "Intrinsic '" + DefName + "' does not start with 'llvm." +
414 TargetPrefix + ".'!";
415 }
416
417 // Parse the list of argument types.
418 ListInit *TypeList = R->getValueAsListInit("Types");
419 for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) {
420 DefInit *DI = dynamic_cast<DefInit*>(TypeList->getElement(i));
421 assert(DI && "Invalid list type!");
422 Record *TyEl = DI->getDef();
423 assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!");
424 ArgTypes.push_back(TyEl->getValueAsString("TypeVal"));
425
Chris Lattner6a160fb2006-03-28 00:15:00 +0000426 if (CGT)
427 ArgVTs.push_back(getValueType(TyEl->getValueAsDef("VT"), CGT));
Chris Lattner43fbbc32006-03-24 19:49:31 +0000428 ArgTypeDefs.push_back(TyEl);
429 }
430 if (ArgTypes.size() == 0)
431 throw "Intrinsic '"+DefName+"' needs at least a type for the ret value!";
432
433 // Parse the intrinsic properties.
434 ListInit *PropList = R->getValueAsListInit("Properties");
435 for (unsigned i = 0, e = PropList->getSize(); i != e; ++i) {
436 DefInit *DI = dynamic_cast<DefInit*>(PropList->getElement(i));
437 assert(DI && "Invalid list type!");
438 Record *Property = DI->getDef();
439 assert(Property->isSubClassOf("IntrinsicProperty") &&
440 "Expected a property!");
441
Chris Lattner4b2362e2006-04-10 22:02:59 +0000442 if (Property->getName() == "IntrNoMem")
Chris Lattner43fbbc32006-03-24 19:49:31 +0000443 ModRef = NoMem;
Chris Lattner4b2362e2006-04-10 22:02:59 +0000444 else if (Property->getName() == "IntrReadArgMem")
Chris Lattner43fbbc32006-03-24 19:49:31 +0000445 ModRef = ReadArgMem;
446 else if (Property->getName() == "IntrReadMem")
447 ModRef = ReadMem;
Chris Lattner4b2362e2006-04-10 22:02:59 +0000448 else if (Property->getName() == "IntrWriteArgMem")
Chris Lattner43fbbc32006-03-24 19:49:31 +0000449 ModRef = WriteArgMem;
450 else if (Property->getName() == "IntrWriteMem")
451 ModRef = WriteMem;
452 else
453 assert(0 && "Unknown property!");
454 }
455}