blob: 319637c7879e094485834d506f2d431df3a30092 [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//
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 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"
Bill Wendlingf5da1332006-12-07 22:21:48 +000022#include "llvm/Support/Streams.h"
Chris Lattner5d7d3db2005-09-14 21:05:02 +000023#include <set>
Chris Lattner75ee2eb2005-10-14 03:54:49 +000024#include <algorithm>
Chris Lattner2082ebe2004-08-01 03:55:39 +000025using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000026
Chris Lattner560a79f2004-10-03 19:34:31 +000027static cl::opt<unsigned>
28AsmWriterNum("asmwriternum", cl::init(0),
29 cl::desc("Make -gen-asm-writer emit assembly writer #N"));
30
Chris Lattner45872072003-08-07 05:38:11 +000031/// getValueType - Return the MCV::ValueType that the specified TableGen record
32/// corresponds to.
Dan Gohman3bf6e182007-07-13 20:16:50 +000033MVT::ValueType llvm::getValueType(Record *Rec) {
Evan Cheng2618d072006-05-17 20:37:59 +000034 return (MVT::ValueType)Rec->getValueAsInt("Value");
Chris Lattner45872072003-08-07 05:38:11 +000035}
36
Chris Lattner2082ebe2004-08-01 03:55:39 +000037std::string llvm::getName(MVT::ValueType T) {
Chris Lattner45872072003-08-07 05:38:11 +000038 switch (T) {
Chris Lattnerd3464c12003-08-07 23:15:21 +000039 case MVT::Other: return "UNKNOWN";
Evan Chengd7c2c862006-06-15 00:16:37 +000040 case MVT::i1: return "MVT::i1";
41 case MVT::i8: return "MVT::i8";
42 case MVT::i16: return "MVT::i16";
43 case MVT::i32: return "MVT::i32";
44 case MVT::i64: return "MVT::i64";
45 case MVT::i128: return "MVT::i128";
Reid Spencerc4de3de2007-04-01 07:20:02 +000046 case MVT::iAny: return "MVT::iAny";
Dan Gohman0fee3ff2007-08-16 21:57:19 +000047 case MVT::fAny: return "MVT::fAny";
Evan Chengd7c2c862006-06-15 00:16:37 +000048 case MVT::f32: return "MVT::f32";
49 case MVT::f64: return "MVT::f64";
50 case MVT::f80: return "MVT::f80";
51 case MVT::f128: return "MVT::f128";
Dale Johannesen317096a2007-09-28 01:08:20 +000052 case MVT::ppcf128: return "MVT::ppcf128";
Evan Chengd7c2c862006-06-15 00:16:37 +000053 case MVT::Flag: return "MVT::Flag";
54 case MVT::isVoid:return "MVT::void";
55 case MVT::v8i8: return "MVT::v8i8";
56 case MVT::v4i16: return "MVT::v4i16";
57 case MVT::v2i32: return "MVT::v2i32";
Bill Wendlingeebc8a12007-03-26 07:53:08 +000058 case MVT::v1i64: return "MVT::v1i64";
Evan Chengd7c2c862006-06-15 00:16:37 +000059 case MVT::v16i8: return "MVT::v16i8";
60 case MVT::v8i16: return "MVT::v8i16";
61 case MVT::v4i32: return "MVT::v4i32";
62 case MVT::v2i64: return "MVT::v2i64";
63 case MVT::v2f32: return "MVT::v2f32";
64 case MVT::v4f32: return "MVT::v4f32";
65 case MVT::v2f64: return "MVT::v2f64";
Christopher Lamb82455102007-07-26 06:41:18 +000066 case MVT::v3i32: return "MVT::v3i32";
67 case MVT::v3f32: return "MVT::v3f32";
Evan Cheng6b125162006-05-17 20:55:51 +000068 case MVT::iPTR: return "TLI.getPointerTy()";
Chris Lattnerd3464c12003-08-07 23:15:21 +000069 default: assert(0 && "ILLEGAL VALUE TYPE!"); return "";
Chris Lattner45872072003-08-07 05:38:11 +000070 }
Chris Lattnerd3464c12003-08-07 23:15:21 +000071}
72
Chris Lattner2082ebe2004-08-01 03:55:39 +000073std::string llvm::getEnumName(MVT::ValueType T) {
Chris Lattnerb72fb7e2003-08-10 19:50:32 +000074 switch (T) {
Evan Cheng2618d072006-05-17 20:37:59 +000075 case MVT::Other: return "MVT::Other";
76 case MVT::i1: return "MVT::i1";
77 case MVT::i8: return "MVT::i8";
78 case MVT::i16: return "MVT::i16";
79 case MVT::i32: return "MVT::i32";
80 case MVT::i64: return "MVT::i64";
81 case MVT::i128: return "MVT::i128";
Reid Spencerc4de3de2007-04-01 07:20:02 +000082 case MVT::iAny: return "MVT::iAny";
Dan Gohman0fee3ff2007-08-16 21:57:19 +000083 case MVT::fAny: return "MVT::fAny";
Evan Cheng2618d072006-05-17 20:37:59 +000084 case MVT::f32: return "MVT::f32";
85 case MVT::f64: return "MVT::f64";
86 case MVT::f80: return "MVT::f80";
87 case MVT::f128: return "MVT::f128";
Dale Johannesen317096a2007-09-28 01:08:20 +000088 case MVT::ppcf128: return "MVT::ppcf128";
Evan Cheng2618d072006-05-17 20:37:59 +000089 case MVT::Flag: return "MVT::Flag";
90 case MVT::isVoid:return "MVT::isVoid";
91 case MVT::v8i8: return "MVT::v8i8";
92 case MVT::v4i16: return "MVT::v4i16";
93 case MVT::v2i32: return "MVT::v2i32";
Bill Wendlingeebc8a12007-03-26 07:53:08 +000094 case MVT::v1i64: return "MVT::v1i64";
Evan Cheng2618d072006-05-17 20:37:59 +000095 case MVT::v16i8: return "MVT::v16i8";
96 case MVT::v8i16: return "MVT::v8i16";
97 case MVT::v4i32: return "MVT::v4i32";
98 case MVT::v2i64: return "MVT::v2i64";
99 case MVT::v2f32: return "MVT::v2f32";
100 case MVT::v4f32: return "MVT::v4f32";
101 case MVT::v2f64: return "MVT::v2f64";
Christopher Lamb82455102007-07-26 06:41:18 +0000102 case MVT::v3i32: return "MVT::v3i32";
103 case MVT::v3f32: return "MVT::v3f32";
Chandler Carruth69940402007-08-04 01:51:18 +0000104 case MVT::iPTR: return "MVT::iPTR";
Chris Lattnerb72fb7e2003-08-10 19:50:32 +0000105 default: assert(0 && "ILLEGAL VALUE TYPE!"); return "";
106 }
107}
108
Chris Lattnerd3464c12003-08-07 23:15:21 +0000109
Chris Lattner45872072003-08-07 05:38:11 +0000110/// getTarget - Return the current instance of the Target class.
111///
Evan Cheng2618d072006-05-17 20:37:59 +0000112CodeGenTarget::CodeGenTarget() {
Chris Lattner45872072003-08-07 05:38:11 +0000113 std::vector<Record*> Targets = Records.getAllDerivedDefinitions("Target");
Misha Brukmanbebdb202004-06-04 14:59:42 +0000114 if (Targets.size() == 0)
Misha Brukman3da94ae2005-04-22 00:00:37 +0000115 throw std::string("ERROR: No 'Target' subclasses defined!");
Chris Lattner45872072003-08-07 05:38:11 +0000116 if (Targets.size() != 1)
117 throw std::string("ERROR: Multiple subclasses of Target defined!");
118 TargetRec = Targets[0];
Chris Lattner45872072003-08-07 05:38:11 +0000119}
120
121
122const std::string &CodeGenTarget::getName() const {
123 return TargetRec->getName();
124}
125
126Record *CodeGenTarget::getInstructionSet() const {
127 return TargetRec->getValueAsDef("InstructionSet");
128}
Brian Gaeked0fde302003-11-11 22:41:34 +0000129
Chris Lattner175580c2004-08-14 22:50:53 +0000130/// getAsmWriter - Return the AssemblyWriter definition for this target.
131///
132Record *CodeGenTarget::getAsmWriter() const {
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000133 std::vector<Record*> LI = TargetRec->getValueAsListOfDefs("AssemblyWriters");
134 if (AsmWriterNum >= LI.size())
Chris Lattner560a79f2004-10-03 19:34:31 +0000135 throw "Target does not have an AsmWriter #" + utostr(AsmWriterNum) + "!";
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000136 return LI[AsmWriterNum];
Chris Lattner175580c2004-08-14 22:50:53 +0000137}
138
Chris Lattner26693112004-08-16 01:10:21 +0000139void CodeGenTarget::ReadRegisters() const {
140 std::vector<Record*> Regs = Records.getAllDerivedDefinitions("Register");
141 if (Regs.empty())
142 throw std::string("No 'Register' subclasses defined!");
143
144 Registers.reserve(Regs.size());
145 Registers.assign(Regs.begin(), Regs.end());
146}
147
Chris Lattner7a680c62004-08-21 02:24:57 +0000148CodeGenRegister::CodeGenRegister(Record *R) : TheDef(R) {
149 DeclaredSpillSize = R->getValueAsInt("SpillSize");
150 DeclaredSpillAlignment = R->getValueAsInt("SpillAlignment");
151}
152
Chris Lattner26693112004-08-16 01:10:21 +0000153const std::string &CodeGenRegister::getName() const {
154 return TheDef->getName();
155}
156
Chris Lattner056afef2004-08-21 04:05:00 +0000157void CodeGenTarget::ReadRegisterClasses() const {
158 std::vector<Record*> RegClasses =
159 Records.getAllDerivedDefinitions("RegisterClass");
160 if (RegClasses.empty())
161 throw std::string("No 'RegisterClass' subclasses defined!");
162
163 RegisterClasses.reserve(RegClasses.size());
164 RegisterClasses.assign(RegClasses.begin(), RegClasses.end());
165}
166
Evan Cheng44a65fa2006-05-16 07:05:30 +0000167std::vector<unsigned char> CodeGenTarget::getRegisterVTs(Record *R) const {
168 std::vector<unsigned char> Result;
169 const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses();
170 for (unsigned i = 0, e = RCs.size(); i != e; ++i) {
171 const CodeGenRegisterClass &RC = RegisterClasses[i];
172 for (unsigned ei = 0, ee = RC.Elements.size(); ei != ee; ++ei) {
173 if (R == RC.Elements[ei]) {
174 const std::vector<MVT::ValueType> &InVTs = RC.getValueTypes();
175 for (unsigned i = 0, e = InVTs.size(); i != e; ++i)
176 Result.push_back(InVTs[i]);
177 }
178 }
179 }
180 return Result;
181}
182
183
Chris Lattner056afef2004-08-21 04:05:00 +0000184CodeGenRegisterClass::CodeGenRegisterClass(Record *R) : TheDef(R) {
Chris Lattnerc67c18f2005-08-19 18:45:20 +0000185 // Rename anonymous register classes.
186 if (R->getName().size() > 9 && R->getName()[9] == '.') {
187 static unsigned AnonCounter = 0;
188 R->setName("AnonRegClass_"+utostr(AnonCounter++));
189 }
190
Nate Begeman6510b222005-12-01 04:51:06 +0000191 std::vector<Record*> TypeList = R->getValueAsListOfDefs("RegTypes");
192 for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
193 Record *Type = TypeList[i];
194 if (!Type->isSubClassOf("ValueType"))
195 throw "RegTypes list member '" + Type->getName() +
196 "' does not derive from the ValueType class!";
197 VTs.push_back(getValueType(Type));
198 }
199 assert(!VTs.empty() && "RegisterClass must contain at least one ValueType!");
Chris Lattnerac468932005-08-19 19:12:51 +0000200
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000201 std::vector<Record*> RegList = R->getValueAsListOfDefs("MemberList");
202 for (unsigned i = 0, e = RegList.size(); i != e; ++i) {
203 Record *Reg = RegList[i];
Chris Lattner056afef2004-08-21 04:05:00 +0000204 if (!Reg->isSubClassOf("Register"))
205 throw "Register Class member '" + Reg->getName() +
206 "' does not derive from the Register class!";
207 Elements.push_back(Reg);
208 }
Nate Begeman6510b222005-12-01 04:51:06 +0000209
Christopher Lamba3211252007-06-13 22:20:15 +0000210 std::vector<Record*> SubRegClassList =
211 R->getValueAsListOfDefs("SubRegClassList");
212 for (unsigned i = 0, e = SubRegClassList.size(); i != e; ++i) {
213 Record *SubRegClass = SubRegClassList[i];
214 if (!SubRegClass->isSubClassOf("RegisterClass"))
215 throw "Register Class member '" + SubRegClass->getName() +
216 "' does not derive from the RegisterClass class!";
217 SubRegClasses.push_back(SubRegClass);
218 }
219
Nate Begeman6510b222005-12-01 04:51:06 +0000220 // Allow targets to override the size in bits of the RegisterClass.
221 unsigned Size = R->getValueAsInt("Size");
222
223 Namespace = R->getValueAsString("Namespace");
224 SpillSize = Size ? Size : MVT::getSizeInBits(VTs[0]);
225 SpillAlignment = R->getValueAsInt("Alignment");
Evan Chenga3ca3142007-09-19 01:35:01 +0000226 CopyCost = R->getValueAsInt("CopyCost");
Nate Begeman6510b222005-12-01 04:51:06 +0000227 MethodBodies = R->getValueAsCode("MethodBodies");
228 MethodProtos = R->getValueAsCode("MethodProtos");
Chris Lattner056afef2004-08-21 04:05:00 +0000229}
230
231const std::string &CodeGenRegisterClass::getName() const {
232 return TheDef->getName();
233}
234
Chris Lattnere9f4ba82005-09-08 21:43:21 +0000235void CodeGenTarget::ReadLegalValueTypes() const {
236 const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses();
237 for (unsigned i = 0, e = RCs.size(); i != e; ++i)
Nate Begeman6510b222005-12-01 04:51:06 +0000238 for (unsigned ri = 0, re = RCs[i].VTs.size(); ri != re; ++ri)
239 LegalValueTypes.push_back(RCs[i].VTs[ri]);
Chris Lattner75ee2eb2005-10-14 03:54:49 +0000240
241 // Remove duplicates.
242 std::sort(LegalValueTypes.begin(), LegalValueTypes.end());
243 LegalValueTypes.erase(std::unique(LegalValueTypes.begin(),
244 LegalValueTypes.end()),
245 LegalValueTypes.end());
Chris Lattnere9f4ba82005-09-08 21:43:21 +0000246}
Chris Lattner056afef2004-08-21 04:05:00 +0000247
Chris Lattner175580c2004-08-14 22:50:53 +0000248
Chris Lattnerec352402004-08-01 05:04:00 +0000249void CodeGenTarget::ReadInstructions() const {
250 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
Chris Lattneraa77d772006-01-27 01:45:06 +0000251 if (Insts.size() <= 2)
Chris Lattnerec352402004-08-01 05:04:00 +0000252 throw std::string("No 'Instruction' subclasses defined!");
253
Chris Lattneraa77d772006-01-27 01:45:06 +0000254 // Parse the instructions defined in the .td file.
Chris Lattner175580c2004-08-14 22:50:53 +0000255 std::string InstFormatName =
256 getAsmWriter()->getValueAsString("InstFormatName");
257
258 for (unsigned i = 0, e = Insts.size(); i != e; ++i) {
259 std::string AsmStr = Insts[i]->getValueAsString(InstFormatName);
260 Instructions.insert(std::make_pair(Insts[i]->getName(),
261 CodeGenInstruction(Insts[i], AsmStr)));
262 }
Chris Lattnerec352402004-08-01 05:04:00 +0000263}
264
Chris Lattnerd6488672005-01-22 18:58:51 +0000265/// getInstructionsByEnumValue - Return all of the instructions defined by the
266/// target, ordered by their enum value.
267void CodeGenTarget::
268getInstructionsByEnumValue(std::vector<const CodeGenInstruction*>
269 &NumberedInstructions) {
Chris Lattneraa77d772006-01-27 01:45:06 +0000270 std::map<std::string, CodeGenInstruction>::const_iterator I;
271 I = getInstructions().find("PHI");
272 if (I == Instructions.end()) throw "Could not find 'PHI' instruction!";
273 const CodeGenInstruction *PHI = &I->second;
274
275 I = getInstructions().find("INLINEASM");
276 if (I == Instructions.end()) throw "Could not find 'INLINEASM' instruction!";
277 const CodeGenInstruction *INLINEASM = &I->second;
278
Jim Laskeya683f9b2007-01-26 17:29:20 +0000279 I = getInstructions().find("LABEL");
280 if (I == Instructions.end()) throw "Could not find 'LABEL' instruction!";
281 const CodeGenInstruction *LABEL = &I->second;
282
Christopher Lamb08d52072007-07-26 07:48:21 +0000283 I = getInstructions().find("EXTRACT_SUBREG");
284 if (I == Instructions.end())
285 throw "Could not find 'EXTRACT_SUBREG' instruction!";
286 const CodeGenInstruction *EXTRACT_SUBREG = &I->second;
287
288 I = getInstructions().find("INSERT_SUBREG");
289 if (I == Instructions.end())
290 throw "Could not find 'INSERT_SUBREG' instruction!";
291 const CodeGenInstruction *INSERT_SUBREG = &I->second;
292
Chris Lattnerd6488672005-01-22 18:58:51 +0000293 // Print out the rest of the instructions now.
Chris Lattnerd6488672005-01-22 18:58:51 +0000294 NumberedInstructions.push_back(PHI);
Chris Lattneraa77d772006-01-27 01:45:06 +0000295 NumberedInstructions.push_back(INLINEASM);
Jim Laskeya683f9b2007-01-26 17:29:20 +0000296 NumberedInstructions.push_back(LABEL);
Christopher Lamb08d52072007-07-26 07:48:21 +0000297 NumberedInstructions.push_back(EXTRACT_SUBREG);
298 NumberedInstructions.push_back(INSERT_SUBREG);
Chris Lattnerd6488672005-01-22 18:58:51 +0000299 for (inst_iterator II = inst_begin(), E = inst_end(); II != E; ++II)
Jim Laskeya683f9b2007-01-26 17:29:20 +0000300 if (&II->second != PHI &&
301 &II->second != INLINEASM &&
Christopher Lamb08d52072007-07-26 07:48:21 +0000302 &II->second != LABEL &&
303 &II->second != EXTRACT_SUBREG &&
304 &II->second != INSERT_SUBREG)
Chris Lattnerd6488672005-01-22 18:58:51 +0000305 NumberedInstructions.push_back(&II->second);
306}
307
308
Misha Brukman35e83cc2004-10-14 05:50:43 +0000309/// isLittleEndianEncoding - Return whether this target encodes its instruction
310/// in little-endian format, i.e. bits laid out in the order [0..n]
311///
312bool CodeGenTarget::isLittleEndianEncoding() const {
313 return getInstructionSet()->getValueAsBit("isLittleEndianEncoding");
314}
315
Chris Lattner0bb75002006-11-15 02:38:17 +0000316
317
318static void ParseConstraint(const std::string &CStr, CodeGenInstruction *I) {
319 // FIXME: Only supports TIED_TO for now.
320 std::string::size_type pos = CStr.find_first_of('=');
Evan Chenge2ba8972006-11-01 00:27:05 +0000321 assert(pos != std::string::npos && "Unrecognized constraint");
Chris Lattner0bb75002006-11-15 02:38:17 +0000322 std::string Name = CStr.substr(0, pos);
Evan Chenge2ba8972006-11-01 00:27:05 +0000323
Evan Cheng4c2b7a32006-11-01 23:03:11 +0000324 // TIED_TO: $src1 = $dst
Chris Lattner0bb75002006-11-15 02:38:17 +0000325 std::string::size_type wpos = Name.find_first_of(" \t");
326 if (wpos == std::string::npos)
327 throw "Illegal format for tied-to constraint: '" + CStr + "'";
328 std::string DestOpName = Name.substr(0, wpos);
329 std::pair<unsigned,unsigned> DestOp = I->ParseOperandName(DestOpName, false);
Evan Chenge2ba8972006-11-01 00:27:05 +0000330
331 Name = CStr.substr(pos+1);
Chris Lattner0bb75002006-11-15 02:38:17 +0000332 wpos = Name.find_first_not_of(" \t");
333 if (wpos == std::string::npos)
334 throw "Illegal format for tied-to constraint: '" + CStr + "'";
335
336 std::pair<unsigned,unsigned> SrcOp =
337 I->ParseOperandName(Name.substr(wpos), false);
338 if (SrcOp > DestOp)
Evan Cheng4c2b7a32006-11-01 23:03:11 +0000339 throw "Illegal tied-to operand constraint '" + CStr + "'";
Chris Lattnera0cca4a2006-11-06 23:49:51 +0000340
Chris Lattner0bb75002006-11-15 02:38:17 +0000341
342 unsigned FlatOpNo = I->getFlattenedOperandNumber(SrcOp);
343 // Build the string for the operand.
344 std::string OpConstraint =
Evan Cheng05551222006-12-01 22:57:41 +0000345 "((" + utostr(FlatOpNo) + " << 16) | (1 << TOI::TIED_TO))";
Chris Lattner0bb75002006-11-15 02:38:17 +0000346
347
348 if (!I->OperandList[DestOp.first].Constraints[DestOp.second].empty())
349 throw "Operand '" + DestOpName + "' cannot have multiple constraints!";
350 I->OperandList[DestOp.first].Constraints[DestOp.second] = OpConstraint;
Evan Chenge2ba8972006-11-01 00:27:05 +0000351}
352
Chris Lattnera0cca4a2006-11-06 23:49:51 +0000353static void ParseConstraints(const std::string &CStr, CodeGenInstruction *I) {
Chris Lattner0bb75002006-11-15 02:38:17 +0000354 // Make sure the constraints list for each operand is large enough to hold
355 // constraint info, even if none is present.
356 for (unsigned i = 0, e = I->OperandList.size(); i != e; ++i)
357 I->OperandList[i].Constraints.resize(I->OperandList[i].MINumOperands);
358
Chris Lattnera0cca4a2006-11-06 23:49:51 +0000359 if (CStr.empty()) return;
360
Evan Chenge2ba8972006-11-01 00:27:05 +0000361 const std::string delims(",");
362 std::string::size_type bidx, eidx;
363
364 bidx = CStr.find_first_not_of(delims);
365 while (bidx != std::string::npos) {
366 eidx = CStr.find_first_of(delims, bidx);
367 if (eidx == std::string::npos)
368 eidx = CStr.length();
Chris Lattnera0cca4a2006-11-06 23:49:51 +0000369
Chris Lattner0bb75002006-11-15 02:38:17 +0000370 ParseConstraint(CStr.substr(bidx, eidx), I);
Evan Chenge2ba8972006-11-01 00:27:05 +0000371 bidx = CStr.find_first_not_of(delims, eidx);
372 }
Evan Chenge2ba8972006-11-01 00:27:05 +0000373}
374
Chris Lattner175580c2004-08-14 22:50:53 +0000375CodeGenInstruction::CodeGenInstruction(Record *R, const std::string &AsmStr)
376 : TheDef(R), AsmString(AsmStr) {
Chris Lattnerec352402004-08-01 05:04:00 +0000377 Name = R->getValueAsString("Name");
378 Namespace = R->getValueAsString("Namespace");
Chris Lattnerec352402004-08-01 05:04:00 +0000379
Chris Lattnerec352402004-08-01 05:04:00 +0000380 isReturn = R->getValueAsBit("isReturn");
381 isBranch = R->getValueAsBit("isBranch");
Owen Anderson20ab2902007-11-12 07:39:39 +0000382 isIndirectBranch = R->getValueAsBit("isIndirectBranch");
Chris Lattnerec352402004-08-01 05:04:00 +0000383 isBarrier = R->getValueAsBit("isBarrier");
384 isCall = R->getValueAsBit("isCall");
Nate Begemancdd66b52004-09-28 21:01:45 +0000385 isLoad = R->getValueAsBit("isLoad");
386 isStore = R->getValueAsBit("isStore");
Evan Cheng3dd298f2007-12-13 00:42:35 +0000387 isImplicitDef= R->getValueAsBit("isImplicitDef");
Chris Lattnerf64f9a42006-11-15 23:23:02 +0000388 bool isTwoAddress = R->getValueAsBit("isTwoAddress");
Evan Cheng5127ce02007-05-16 20:45:24 +0000389 isPredicable = R->getValueAsBit("isPredicable");
Chris Lattneraad75aa2005-01-02 02:29:04 +0000390 isConvertibleToThreeAddress = R->getValueAsBit("isConvertibleToThreeAddress");
391 isCommutable = R->getValueAsBit("isCommutable");
Chris Lattnerec352402004-08-01 05:04:00 +0000392 isTerminator = R->getValueAsBit("isTerminator");
Dan Gohmand45eddd2007-06-26 00:48:07 +0000393 isReMaterializable = R->getValueAsBit("isReMaterializable");
Chris Lattner5b71d3a2004-09-28 18:38:01 +0000394 hasDelaySlot = R->getValueAsBit("hasDelaySlot");
Chris Lattnere3cbf822005-08-26 20:55:40 +0000395 usesCustomDAGSchedInserter = R->getValueAsBit("usesCustomDAGSchedInserter");
Evan Cheng1c3d19e2005-12-04 08:18:16 +0000396 hasCtrlDep = R->getValueAsBit("hasCtrlDep");
Evan Chengeaa91b02007-06-19 01:26:51 +0000397 isNotDuplicable = R->getValueAsBit("isNotDuplicable");
Bill Wendling6b1da9c2007-12-14 01:48:59 +0000398 mayHaveSideEffects = R->getValueAsBit("mayHaveSideEffects");
399 neverHasSideEffects = R->getValueAsBit("neverHasSideEffects");
Evan Cheng88cc0922007-07-10 18:05:01 +0000400 hasOptionalDef = false;
Chris Lattnercfbf96a2005-08-18 23:38:41 +0000401 hasVariableNumberOfOperands = false;
Bill Wendling6b1da9c2007-12-14 01:48:59 +0000402
403 if (mayHaveSideEffects && neverHasSideEffects)
404 throw R->getName() +
405 ": cannot have both 'mayHaveSideEffects' and 'neverHasSideEffects' set!";
406
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000407 DagInit *DI;
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000408 try {
Evan Cheng64d80e32007-07-19 01:14:50 +0000409 DI = R->getValueAsDag("OutOperandList");
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000410 } catch (...) {
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000411 // Error getting operand list, just ignore it (sparcv9).
Chris Lattner87c59052004-08-01 07:42:39 +0000412 AsmString.clear();
413 OperandList.clear();
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000414 return;
415 }
Evan Cheng64d80e32007-07-19 01:14:50 +0000416 NumDefs = DI->getNumArgs();
417
418 DagInit *IDI;
419 try {
420 IDI = R->getValueAsDag("InOperandList");
421 } catch (...) {
422 // Error getting operand list, just ignore it (sparcv9).
423 AsmString.clear();
424 OperandList.clear();
425 return;
426 }
427 DI = (DagInit*)(new BinOpInit(BinOpInit::CONCAT, DI, IDI))->Fold();
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000428
429 unsigned MIOperandNo = 0;
430 std::set<std::string> OperandNames;
431 for (unsigned i = 0, e = DI->getNumArgs(); i != e; ++i) {
432 DefInit *Arg = dynamic_cast<DefInit*>(DI->getArg(i));
433 if (!Arg)
434 throw "Illegal operand for the '" + R->getName() + "' instruction!";
435
436 Record *Rec = Arg->getDef();
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000437 std::string PrintMethod = "printOperand";
438 unsigned NumOps = 1;
Chris Lattner33670792005-11-19 07:48:33 +0000439 DagInit *MIOpInfo = 0;
Nate Begeman86193d12005-12-01 00:12:04 +0000440 if (Rec->isSubClassOf("Operand")) {
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000441 PrintMethod = Rec->getValueAsString("PrintMethod");
Chris Lattner65303d62005-11-19 07:05:57 +0000442 MIOpInfo = Rec->getValueAsDag("MIOperandInfo");
Chris Lattnerd438b532006-11-03 23:45:17 +0000443
444 // Verify that MIOpInfo has an 'ops' root value.
445 if (!dynamic_cast<DefInit*>(MIOpInfo->getOperator()) ||
446 dynamic_cast<DefInit*>(MIOpInfo->getOperator())
447 ->getDef()->getName() != "ops")
448 throw "Bad value for MIOperandInfo in operand '" + Rec->getName() +
449 "'\n";
450
451 // If we have MIOpInfo, then we have #operands equal to number of entries
452 // in MIOperandInfo.
453 if (unsigned NumArgs = MIOpInfo->getNumArgs())
454 NumOps = NumArgs;
455
Evan Cheng88cc0922007-07-10 18:05:01 +0000456 if (Rec->isSubClassOf("PredicateOperand"))
Evan Chengc419bd32007-07-06 23:23:38 +0000457 isPredicable = true;
Evan Cheng88cc0922007-07-10 18:05:01 +0000458 else if (Rec->isSubClassOf("OptionalDefOperand"))
459 hasOptionalDef = true;
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000460 } else if (Rec->getName() == "variable_ops") {
461 hasVariableNumberOfOperands = true;
462 continue;
Chris Lattnerf1968392006-11-10 02:01:40 +0000463 } else if (!Rec->isSubClassOf("RegisterClass") &&
464 Rec->getName() != "ptr_rc")
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000465 throw "Unknown operand class '" + Rec->getName() +
466 "' in instruction '" + R->getName() + "' instruction!";
467
Chris Lattnerc4a8b732005-09-14 21:13:50 +0000468 // Check that the operand has a name and that it's unique.
469 if (DI->getArgName(i).empty())
470 throw "In instruction '" + R->getName() + "', operand #" + utostr(i) +
471 " has no name!";
472 if (!OperandNames.insert(DI->getArgName(i)).second)
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000473 throw "In instruction '" + R->getName() + "', operand #" + utostr(i) +
474 " has the same name as a previous operand!";
475
Nate Begeman86193d12005-12-01 00:12:04 +0000476 OperandList.push_back(OperandInfo(Rec, DI->getArgName(i), PrintMethod,
477 MIOperandNo, NumOps, MIOpInfo));
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000478 MIOperandNo += NumOps;
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000479 }
Evan Chenge2ba8972006-11-01 00:27:05 +0000480
Chris Lattnerf64f9a42006-11-15 23:23:02 +0000481 // Parse Constraints.
Chris Lattnera0cca4a2006-11-06 23:49:51 +0000482 ParseConstraints(R->getValueAsString("Constraints"), this);
483
484 // For backward compatibility: isTwoAddress means operand 1 is tied to
485 // operand 0.
Chris Lattner0bb75002006-11-15 02:38:17 +0000486 if (isTwoAddress) {
487 if (!OperandList[1].Constraints[0].empty())
488 throw R->getName() + ": cannot use isTwoAddress property: instruction "
489 "already has constraint set!";
Evan Cheng05551222006-12-01 22:57:41 +0000490 OperandList[1].Constraints[0] = "((0 << 16) | (1 << TOI::TIED_TO))";
Chris Lattner0bb75002006-11-15 02:38:17 +0000491 }
Chris Lattnera0cca4a2006-11-06 23:49:51 +0000492
493 // Any operands with unset constraints get 0 as their constraint.
494 for (unsigned op = 0, e = OperandList.size(); op != e; ++op)
Chris Lattner0bb75002006-11-15 02:38:17 +0000495 for (unsigned j = 0, e = OperandList[op].MINumOperands; j != e; ++j)
496 if (OperandList[op].Constraints[j].empty())
497 OperandList[op].Constraints[j] = "0";
Chris Lattnerf64f9a42006-11-15 23:23:02 +0000498
499 // Parse the DisableEncoding field.
500 std::string DisableEncoding = R->getValueAsString("DisableEncoding");
501 while (1) {
502 std::string OpName = getToken(DisableEncoding, " ,\t");
503 if (OpName.empty()) break;
504
505 // Figure out which operand this is.
506 std::pair<unsigned,unsigned> Op = ParseOperandName(OpName, false);
507
508 // Mark the operand as not-to-be encoded.
509 if (Op.second >= OperandList[Op.first].DoNotEncode.size())
510 OperandList[Op.first].DoNotEncode.resize(Op.second+1);
511 OperandList[Op.first].DoNotEncode[Op.second] = true;
512 }
Chris Lattnerec352402004-08-01 05:04:00 +0000513}
514
515
Chris Lattner87c59052004-08-01 07:42:39 +0000516
517/// getOperandNamed - Return the index of the operand with the specified
518/// non-empty name. If the instruction does not have an operand with the
519/// specified name, throw an exception.
Misha Brukman35e83cc2004-10-14 05:50:43 +0000520///
Chris Lattner87c59052004-08-01 07:42:39 +0000521unsigned CodeGenInstruction::getOperandNamed(const std::string &Name) const {
522 assert(!Name.empty() && "Cannot search for operand with no name!");
523 for (unsigned i = 0, e = OperandList.size(); i != e; ++i)
524 if (OperandList[i].Name == Name) return i;
525 throw "Instruction '" + TheDef->getName() +
526 "' does not have an operand named '$" + Name + "'!";
527}
Evan Cheng0fc71982005-12-08 02:00:36 +0000528
Chris Lattner0bb75002006-11-15 02:38:17 +0000529std::pair<unsigned,unsigned>
530CodeGenInstruction::ParseOperandName(const std::string &Op,
531 bool AllowWholeOp) {
532 if (Op.empty() || Op[0] != '$')
533 throw TheDef->getName() + ": Illegal operand name: '" + Op + "'";
534
535 std::string OpName = Op.substr(1);
536 std::string SubOpName;
537
538 // Check to see if this is $foo.bar.
539 std::string::size_type DotIdx = OpName.find_first_of(".");
540 if (DotIdx != std::string::npos) {
541 SubOpName = OpName.substr(DotIdx+1);
542 if (SubOpName.empty())
543 throw TheDef->getName() + ": illegal empty suboperand name in '" +Op +"'";
544 OpName = OpName.substr(0, DotIdx);
545 }
546
547 unsigned OpIdx = getOperandNamed(OpName);
548
549 if (SubOpName.empty()) { // If no suboperand name was specified:
550 // If one was needed, throw.
551 if (OperandList[OpIdx].MINumOperands > 1 && !AllowWholeOp &&
552 SubOpName.empty())
553 throw TheDef->getName() + ": Illegal to refer to"
554 " whole operand part of complex operand '" + Op + "'";
555
556 // Otherwise, return the operand.
557 return std::make_pair(OpIdx, 0U);
558 }
559
560 // Find the suboperand number involved.
561 DagInit *MIOpInfo = OperandList[OpIdx].MIOperandInfo;
562 if (MIOpInfo == 0)
563 throw TheDef->getName() + ": unknown suboperand name in '" + Op + "'";
564
565 // Find the operand with the right name.
566 for (unsigned i = 0, e = MIOpInfo->getNumArgs(); i != e; ++i)
567 if (MIOpInfo->getArgName(i) == SubOpName)
568 return std::make_pair(OpIdx, i);
569
570 // Otherwise, didn't find it!
571 throw TheDef->getName() + ": unknown suboperand name in '" + Op + "'";
572}
573
574
575
576
Evan Cheng0fc71982005-12-08 02:00:36 +0000577//===----------------------------------------------------------------------===//
578// ComplexPattern implementation
579//
580ComplexPattern::ComplexPattern(Record *R) {
Evan Cheng3aa39f42005-12-08 02:14:08 +0000581 Ty = ::getValueType(R->getValueAsDef("Ty"));
582 NumOperands = R->getValueAsInt("NumOperands");
583 SelectFunc = R->getValueAsString("SelectFunc");
584 RootNodes = R->getValueAsListOfDefs("RootNodes");
Evan Cheng94b30402006-10-11 21:02:01 +0000585
586 // Parse the properties.
587 Properties = 0;
588 std::vector<Record*> PropList = R->getValueAsListOfDefs("Properties");
589 for (unsigned i = 0, e = PropList.size(); i != e; ++i)
590 if (PropList[i]->getName() == "SDNPHasChain") {
591 Properties |= 1 << SDNPHasChain;
592 } else if (PropList[i]->getName() == "SDNPOptInFlag") {
593 Properties |= 1 << SDNPOptInFlag;
594 } else {
Bill Wendlingf5da1332006-12-07 22:21:48 +0000595 cerr << "Unsupported SD Node property '" << PropList[i]->getName()
596 << "' on ComplexPattern '" << R->getName() << "'!\n";
Evan Cheng94b30402006-10-11 21:02:01 +0000597 exit(1);
598 }
Evan Cheng0fc71982005-12-08 02:00:36 +0000599}
Evan Cheng3aa39f42005-12-08 02:14:08 +0000600
Chris Lattner43fbbc32006-03-24 19:49:31 +0000601//===----------------------------------------------------------------------===//
602// CodeGenIntrinsic Implementation
603//===----------------------------------------------------------------------===//
604
605std::vector<CodeGenIntrinsic> llvm::LoadIntrinsics(const RecordKeeper &RC) {
606 std::vector<Record*> I = RC.getAllDerivedDefinitions("Intrinsic");
Chris Lattner8850a1b2006-03-27 22:48:18 +0000607
608 std::vector<CodeGenIntrinsic> Result;
Chris Lattner6a160fb2006-03-28 00:15:00 +0000609
610 // If we are in the context of a target .td file, get the target info so that
611 // we can decode the current intptr_t.
612 CodeGenTarget *CGT = 0;
613 if (Records.getClass("Target") &&
614 Records.getAllDerivedDefinitions("Target").size() == 1)
615 CGT = new CodeGenTarget();
616
Chris Lattner8850a1b2006-03-27 22:48:18 +0000617 for (unsigned i = 0, e = I.size(); i != e; ++i)
Chris Lattner6a160fb2006-03-28 00:15:00 +0000618 Result.push_back(CodeGenIntrinsic(I[i], CGT));
619 delete CGT;
Chris Lattner8850a1b2006-03-27 22:48:18 +0000620 return Result;
Chris Lattner43fbbc32006-03-24 19:49:31 +0000621}
622
Chris Lattner76f8c7c2006-03-28 00:03:08 +0000623CodeGenIntrinsic::CodeGenIntrinsic(Record *R, CodeGenTarget *CGT) {
Chris Lattner2ca956f2006-03-24 20:25:01 +0000624 TheDef = R;
Chris Lattner43fbbc32006-03-24 19:49:31 +0000625 std::string DefName = R->getName();
626 ModRef = WriteMem;
Reid Spencerc4de3de2007-04-01 07:20:02 +0000627 isOverloaded = false;
Chris Lattner43fbbc32006-03-24 19:49:31 +0000628
629 if (DefName.size() <= 4 ||
630 std::string(DefName.begin(), DefName.begin()+4) != "int_")
631 throw "Intrinsic '" + DefName + "' does not start with 'int_'!";
632 EnumName = std::string(DefName.begin()+4, DefName.end());
633 if (R->getValue("GCCBuiltinName")) // Ignore a missing GCCBuiltinName field.
634 GCCBuiltinName = R->getValueAsString("GCCBuiltinName");
635 TargetPrefix = R->getValueAsString("TargetPrefix");
636 Name = R->getValueAsString("LLVMName");
637 if (Name == "") {
638 // If an explicit name isn't specified, derive one from the DefName.
639 Name = "llvm.";
640 for (unsigned i = 0, e = EnumName.size(); i != e; ++i)
641 if (EnumName[i] == '_')
642 Name += '.';
643 else
644 Name += EnumName[i];
645 } else {
646 // Verify it starts with "llvm.".
647 if (Name.size() <= 5 ||
648 std::string(Name.begin(), Name.begin()+5) != "llvm.")
649 throw "Intrinsic '" + DefName + "'s name does not start with 'llvm.'!";
650 }
651
652 // If TargetPrefix is specified, make sure that Name starts with
653 // "llvm.<targetprefix>.".
654 if (!TargetPrefix.empty()) {
655 if (Name.size() < 6+TargetPrefix.size() ||
656 std::string(Name.begin()+5, Name.begin()+6+TargetPrefix.size())
657 != (TargetPrefix+"."))
658 throw "Intrinsic '" + DefName + "' does not start with 'llvm." +
659 TargetPrefix + ".'!";
660 }
661
662 // Parse the list of argument types.
663 ListInit *TypeList = R->getValueAsListInit("Types");
664 for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) {
Chris Lattner50d45652007-02-27 22:08:27 +0000665 Record *TyEl = TypeList->getElementAsRecord(i);
Chris Lattner43fbbc32006-03-24 19:49:31 +0000666 assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!");
Dan Gohman3bf6e182007-07-13 20:16:50 +0000667 MVT::ValueType VT = getValueType(TyEl->getValueAsDef("VT"));
Dan Gohman0fee3ff2007-08-16 21:57:19 +0000668 isOverloaded |= VT == MVT::iAny || VT == MVT::fAny;
Reid Spencerc4de3de2007-04-01 07:20:02 +0000669 ArgVTs.push_back(VT);
Chris Lattner43fbbc32006-03-24 19:49:31 +0000670 ArgTypeDefs.push_back(TyEl);
671 }
Chandler Carruth69940402007-08-04 01:51:18 +0000672 if (ArgVTs.size() == 0)
Chris Lattner43fbbc32006-03-24 19:49:31 +0000673 throw "Intrinsic '"+DefName+"' needs at least a type for the ret value!";
Reid Spencerc4de3de2007-04-01 07:20:02 +0000674
Chris Lattner43fbbc32006-03-24 19:49:31 +0000675
676 // Parse the intrinsic properties.
677 ListInit *PropList = R->getValueAsListInit("Properties");
678 for (unsigned i = 0, e = PropList->getSize(); i != e; ++i) {
Chris Lattner50d45652007-02-27 22:08:27 +0000679 Record *Property = PropList->getElementAsRecord(i);
Chris Lattner43fbbc32006-03-24 19:49:31 +0000680 assert(Property->isSubClassOf("IntrinsicProperty") &&
681 "Expected a property!");
682
Chris Lattner4b2362e2006-04-10 22:02:59 +0000683 if (Property->getName() == "IntrNoMem")
Chris Lattner43fbbc32006-03-24 19:49:31 +0000684 ModRef = NoMem;
Chris Lattner4b2362e2006-04-10 22:02:59 +0000685 else if (Property->getName() == "IntrReadArgMem")
Chris Lattner43fbbc32006-03-24 19:49:31 +0000686 ModRef = ReadArgMem;
687 else if (Property->getName() == "IntrReadMem")
688 ModRef = ReadMem;
Chris Lattner4b2362e2006-04-10 22:02:59 +0000689 else if (Property->getName() == "IntrWriteArgMem")
Chris Lattner43fbbc32006-03-24 19:49:31 +0000690 ModRef = WriteArgMem;
691 else if (Property->getName() == "IntrWriteMem")
692 ModRef = WriteMem;
693 else
694 assert(0 && "Unknown property!");
695 }
696}