blob: 7cf80b02abef318912c6a1c7baa1db60b676c5a9 [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"
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.
Chris Lattner8850a1b2006-03-27 22:48:18 +000033MVT::ValueType llvm::getValueType(Record *Rec, const CodeGenTarget *CGT) {
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";
46 case MVT::f32: return "MVT::f32";
47 case MVT::f64: return "MVT::f64";
48 case MVT::f80: return "MVT::f80";
49 case MVT::f128: return "MVT::f128";
50 case MVT::Flag: return "MVT::Flag";
51 case MVT::isVoid:return "MVT::void";
52 case MVT::v8i8: return "MVT::v8i8";
53 case MVT::v4i16: return "MVT::v4i16";
54 case MVT::v2i32: return "MVT::v2i32";
Bill Wendlingeebc8a12007-03-26 07:53:08 +000055 case MVT::v1i64: return "MVT::v1i64";
Evan Chengd7c2c862006-06-15 00:16:37 +000056 case MVT::v16i8: return "MVT::v16i8";
57 case MVT::v8i16: return "MVT::v8i16";
58 case MVT::v4i32: return "MVT::v4i32";
59 case MVT::v2i64: return "MVT::v2i64";
60 case MVT::v2f32: return "MVT::v2f32";
61 case MVT::v4f32: return "MVT::v4f32";
62 case MVT::v2f64: return "MVT::v2f64";
Evan Cheng6b125162006-05-17 20:55:51 +000063 case MVT::iPTR: return "TLI.getPointerTy()";
Chris Lattnerd3464c12003-08-07 23:15:21 +000064 default: assert(0 && "ILLEGAL VALUE TYPE!"); return "";
Chris Lattner45872072003-08-07 05:38:11 +000065 }
Chris Lattnerd3464c12003-08-07 23:15:21 +000066}
67
Chris Lattner2082ebe2004-08-01 03:55:39 +000068std::string llvm::getEnumName(MVT::ValueType T) {
Chris Lattnerb72fb7e2003-08-10 19:50:32 +000069 switch (T) {
Evan Cheng2618d072006-05-17 20:37:59 +000070 case MVT::Other: return "MVT::Other";
71 case MVT::i1: return "MVT::i1";
72 case MVT::i8: return "MVT::i8";
73 case MVT::i16: return "MVT::i16";
74 case MVT::i32: return "MVT::i32";
75 case MVT::i64: return "MVT::i64";
76 case MVT::i128: return "MVT::i128";
77 case MVT::f32: return "MVT::f32";
78 case MVT::f64: return "MVT::f64";
79 case MVT::f80: return "MVT::f80";
80 case MVT::f128: return "MVT::f128";
81 case MVT::Flag: return "MVT::Flag";
82 case MVT::isVoid:return "MVT::isVoid";
83 case MVT::v8i8: return "MVT::v8i8";
84 case MVT::v4i16: return "MVT::v4i16";
85 case MVT::v2i32: return "MVT::v2i32";
Bill Wendlingeebc8a12007-03-26 07:53:08 +000086 case MVT::v1i64: return "MVT::v1i64";
Evan Cheng2618d072006-05-17 20:37:59 +000087 case MVT::v16i8: return "MVT::v16i8";
88 case MVT::v8i16: return "MVT::v8i16";
89 case MVT::v4i32: return "MVT::v4i32";
90 case MVT::v2i64: return "MVT::v2i64";
91 case MVT::v2f32: return "MVT::v2f32";
92 case MVT::v4f32: return "MVT::v4f32";
93 case MVT::v2f64: return "MVT::v2f64";
Evan Cheng6b125162006-05-17 20:55:51 +000094 case MVT::iPTR: return "TLI.getPointerTy()";
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///
Evan Cheng2618d072006-05-17 20:37:59 +0000107CodeGenTarget::CodeGenTarget() {
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];
Chris Lattner45872072003-08-07 05:38:11 +0000114}
115
116
117const std::string &CodeGenTarget::getName() const {
118 return TargetRec->getName();
119}
120
121Record *CodeGenTarget::getInstructionSet() const {
122 return TargetRec->getValueAsDef("InstructionSet");
123}
Brian Gaeked0fde302003-11-11 22:41:34 +0000124
Chris Lattner175580c2004-08-14 22:50:53 +0000125/// getAsmWriter - Return the AssemblyWriter definition for this target.
126///
127Record *CodeGenTarget::getAsmWriter() const {
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000128 std::vector<Record*> LI = TargetRec->getValueAsListOfDefs("AssemblyWriters");
129 if (AsmWriterNum >= LI.size())
Chris Lattner560a79f2004-10-03 19:34:31 +0000130 throw "Target does not have an AsmWriter #" + utostr(AsmWriterNum) + "!";
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000131 return LI[AsmWriterNum];
Chris Lattner175580c2004-08-14 22:50:53 +0000132}
133
Chris Lattner26693112004-08-16 01:10:21 +0000134void CodeGenTarget::ReadRegisters() const {
135 std::vector<Record*> Regs = Records.getAllDerivedDefinitions("Register");
136 if (Regs.empty())
137 throw std::string("No 'Register' subclasses defined!");
138
139 Registers.reserve(Regs.size());
140 Registers.assign(Regs.begin(), Regs.end());
141}
142
Chris Lattner7a680c62004-08-21 02:24:57 +0000143CodeGenRegister::CodeGenRegister(Record *R) : TheDef(R) {
144 DeclaredSpillSize = R->getValueAsInt("SpillSize");
145 DeclaredSpillAlignment = R->getValueAsInt("SpillAlignment");
146}
147
Chris Lattner26693112004-08-16 01:10:21 +0000148const std::string &CodeGenRegister::getName() const {
149 return TheDef->getName();
150}
151
Chris Lattner056afef2004-08-21 04:05:00 +0000152void CodeGenTarget::ReadRegisterClasses() const {
153 std::vector<Record*> RegClasses =
154 Records.getAllDerivedDefinitions("RegisterClass");
155 if (RegClasses.empty())
156 throw std::string("No 'RegisterClass' subclasses defined!");
157
158 RegisterClasses.reserve(RegClasses.size());
159 RegisterClasses.assign(RegClasses.begin(), RegClasses.end());
160}
161
Evan Cheng44a65fa2006-05-16 07:05:30 +0000162std::vector<unsigned char> CodeGenTarget::getRegisterVTs(Record *R) const {
163 std::vector<unsigned char> Result;
164 const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses();
165 for (unsigned i = 0, e = RCs.size(); i != e; ++i) {
166 const CodeGenRegisterClass &RC = RegisterClasses[i];
167 for (unsigned ei = 0, ee = RC.Elements.size(); ei != ee; ++ei) {
168 if (R == RC.Elements[ei]) {
169 const std::vector<MVT::ValueType> &InVTs = RC.getValueTypes();
170 for (unsigned i = 0, e = InVTs.size(); i != e; ++i)
171 Result.push_back(InVTs[i]);
172 }
173 }
174 }
175 return Result;
176}
177
178
Chris Lattner056afef2004-08-21 04:05:00 +0000179CodeGenRegisterClass::CodeGenRegisterClass(Record *R) : TheDef(R) {
Chris Lattnerc67c18f2005-08-19 18:45:20 +0000180 // Rename anonymous register classes.
181 if (R->getName().size() > 9 && R->getName()[9] == '.') {
182 static unsigned AnonCounter = 0;
183 R->setName("AnonRegClass_"+utostr(AnonCounter++));
184 }
185
Nate Begeman6510b222005-12-01 04:51:06 +0000186 std::vector<Record*> TypeList = R->getValueAsListOfDefs("RegTypes");
187 for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
188 Record *Type = TypeList[i];
189 if (!Type->isSubClassOf("ValueType"))
190 throw "RegTypes list member '" + Type->getName() +
191 "' does not derive from the ValueType class!";
192 VTs.push_back(getValueType(Type));
193 }
194 assert(!VTs.empty() && "RegisterClass must contain at least one ValueType!");
Chris Lattnerac468932005-08-19 19:12:51 +0000195
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000196 std::vector<Record*> RegList = R->getValueAsListOfDefs("MemberList");
197 for (unsigned i = 0, e = RegList.size(); i != e; ++i) {
198 Record *Reg = RegList[i];
Chris Lattner056afef2004-08-21 04:05:00 +0000199 if (!Reg->isSubClassOf("Register"))
200 throw "Register Class member '" + Reg->getName() +
201 "' does not derive from the Register class!";
202 Elements.push_back(Reg);
203 }
Nate Begeman6510b222005-12-01 04:51:06 +0000204
205 // Allow targets to override the size in bits of the RegisterClass.
206 unsigned Size = R->getValueAsInt("Size");
207
208 Namespace = R->getValueAsString("Namespace");
209 SpillSize = Size ? Size : MVT::getSizeInBits(VTs[0]);
210 SpillAlignment = R->getValueAsInt("Alignment");
211 MethodBodies = R->getValueAsCode("MethodBodies");
212 MethodProtos = R->getValueAsCode("MethodProtos");
Chris Lattner056afef2004-08-21 04:05:00 +0000213}
214
215const std::string &CodeGenRegisterClass::getName() const {
216 return TheDef->getName();
217}
218
Chris Lattnere9f4ba82005-09-08 21:43:21 +0000219void CodeGenTarget::ReadLegalValueTypes() const {
220 const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses();
221 for (unsigned i = 0, e = RCs.size(); i != e; ++i)
Nate Begeman6510b222005-12-01 04:51:06 +0000222 for (unsigned ri = 0, re = RCs[i].VTs.size(); ri != re; ++ri)
223 LegalValueTypes.push_back(RCs[i].VTs[ri]);
Chris Lattner75ee2eb2005-10-14 03:54:49 +0000224
225 // Remove duplicates.
226 std::sort(LegalValueTypes.begin(), LegalValueTypes.end());
227 LegalValueTypes.erase(std::unique(LegalValueTypes.begin(),
228 LegalValueTypes.end()),
229 LegalValueTypes.end());
Chris Lattnere9f4ba82005-09-08 21:43:21 +0000230}
Chris Lattner056afef2004-08-21 04:05:00 +0000231
Chris Lattner175580c2004-08-14 22:50:53 +0000232
Chris Lattnerec352402004-08-01 05:04:00 +0000233void CodeGenTarget::ReadInstructions() const {
234 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
Chris Lattneraa77d772006-01-27 01:45:06 +0000235 if (Insts.size() <= 2)
Chris Lattnerec352402004-08-01 05:04:00 +0000236 throw std::string("No 'Instruction' subclasses defined!");
237
Chris Lattneraa77d772006-01-27 01:45:06 +0000238 // Parse the instructions defined in the .td file.
Chris Lattner175580c2004-08-14 22:50:53 +0000239 std::string InstFormatName =
240 getAsmWriter()->getValueAsString("InstFormatName");
241
242 for (unsigned i = 0, e = Insts.size(); i != e; ++i) {
243 std::string AsmStr = Insts[i]->getValueAsString(InstFormatName);
244 Instructions.insert(std::make_pair(Insts[i]->getName(),
245 CodeGenInstruction(Insts[i], AsmStr)));
246 }
Chris Lattnerec352402004-08-01 05:04:00 +0000247}
248
Chris Lattnerd6488672005-01-22 18:58:51 +0000249/// getInstructionsByEnumValue - Return all of the instructions defined by the
250/// target, ordered by their enum value.
251void CodeGenTarget::
252getInstructionsByEnumValue(std::vector<const CodeGenInstruction*>
253 &NumberedInstructions) {
Chris Lattneraa77d772006-01-27 01:45:06 +0000254 std::map<std::string, CodeGenInstruction>::const_iterator I;
255 I = getInstructions().find("PHI");
256 if (I == Instructions.end()) throw "Could not find 'PHI' instruction!";
257 const CodeGenInstruction *PHI = &I->second;
258
259 I = getInstructions().find("INLINEASM");
260 if (I == Instructions.end()) throw "Could not find 'INLINEASM' instruction!";
261 const CodeGenInstruction *INLINEASM = &I->second;
262
Jim Laskeya683f9b2007-01-26 17:29:20 +0000263 I = getInstructions().find("LABEL");
264 if (I == Instructions.end()) throw "Could not find 'LABEL' instruction!";
265 const CodeGenInstruction *LABEL = &I->second;
266
Chris Lattnerd6488672005-01-22 18:58:51 +0000267 // Print out the rest of the instructions now.
Chris Lattnerd6488672005-01-22 18:58:51 +0000268 NumberedInstructions.push_back(PHI);
Chris Lattneraa77d772006-01-27 01:45:06 +0000269 NumberedInstructions.push_back(INLINEASM);
Jim Laskeya683f9b2007-01-26 17:29:20 +0000270 NumberedInstructions.push_back(LABEL);
Chris Lattnerd6488672005-01-22 18:58:51 +0000271 for (inst_iterator II = inst_begin(), E = inst_end(); II != E; ++II)
Jim Laskeya683f9b2007-01-26 17:29:20 +0000272 if (&II->second != PHI &&
273 &II->second != INLINEASM &&
274 &II->second != LABEL)
Chris Lattnerd6488672005-01-22 18:58:51 +0000275 NumberedInstructions.push_back(&II->second);
276}
277
278
Misha Brukman35e83cc2004-10-14 05:50:43 +0000279/// isLittleEndianEncoding - Return whether this target encodes its instruction
280/// in little-endian format, i.e. bits laid out in the order [0..n]
281///
282bool CodeGenTarget::isLittleEndianEncoding() const {
283 return getInstructionSet()->getValueAsBit("isLittleEndianEncoding");
284}
285
Chris Lattner0bb75002006-11-15 02:38:17 +0000286
287
288static void ParseConstraint(const std::string &CStr, CodeGenInstruction *I) {
289 // FIXME: Only supports TIED_TO for now.
290 std::string::size_type pos = CStr.find_first_of('=');
Evan Chenge2ba8972006-11-01 00:27:05 +0000291 assert(pos != std::string::npos && "Unrecognized constraint");
Chris Lattner0bb75002006-11-15 02:38:17 +0000292 std::string Name = CStr.substr(0, pos);
Evan Chenge2ba8972006-11-01 00:27:05 +0000293
Evan Cheng4c2b7a32006-11-01 23:03:11 +0000294 // TIED_TO: $src1 = $dst
Chris Lattner0bb75002006-11-15 02:38:17 +0000295 std::string::size_type wpos = Name.find_first_of(" \t");
296 if (wpos == std::string::npos)
297 throw "Illegal format for tied-to constraint: '" + CStr + "'";
298 std::string DestOpName = Name.substr(0, wpos);
299 std::pair<unsigned,unsigned> DestOp = I->ParseOperandName(DestOpName, false);
Evan Chenge2ba8972006-11-01 00:27:05 +0000300
301 Name = CStr.substr(pos+1);
Chris Lattner0bb75002006-11-15 02:38:17 +0000302 wpos = Name.find_first_not_of(" \t");
303 if (wpos == std::string::npos)
304 throw "Illegal format for tied-to constraint: '" + CStr + "'";
305
306 std::pair<unsigned,unsigned> SrcOp =
307 I->ParseOperandName(Name.substr(wpos), false);
308 if (SrcOp > DestOp)
Evan Cheng4c2b7a32006-11-01 23:03:11 +0000309 throw "Illegal tied-to operand constraint '" + CStr + "'";
Chris Lattnera0cca4a2006-11-06 23:49:51 +0000310
Chris Lattner0bb75002006-11-15 02:38:17 +0000311
312 unsigned FlatOpNo = I->getFlattenedOperandNumber(SrcOp);
313 // Build the string for the operand.
314 std::string OpConstraint =
Evan Cheng05551222006-12-01 22:57:41 +0000315 "((" + utostr(FlatOpNo) + " << 16) | (1 << TOI::TIED_TO))";
Chris Lattner0bb75002006-11-15 02:38:17 +0000316
317
318 if (!I->OperandList[DestOp.first].Constraints[DestOp.second].empty())
319 throw "Operand '" + DestOpName + "' cannot have multiple constraints!";
320 I->OperandList[DestOp.first].Constraints[DestOp.second] = OpConstraint;
Evan Chenge2ba8972006-11-01 00:27:05 +0000321}
322
Chris Lattnera0cca4a2006-11-06 23:49:51 +0000323static void ParseConstraints(const std::string &CStr, CodeGenInstruction *I) {
Chris Lattner0bb75002006-11-15 02:38:17 +0000324 // Make sure the constraints list for each operand is large enough to hold
325 // constraint info, even if none is present.
326 for (unsigned i = 0, e = I->OperandList.size(); i != e; ++i)
327 I->OperandList[i].Constraints.resize(I->OperandList[i].MINumOperands);
328
Chris Lattnera0cca4a2006-11-06 23:49:51 +0000329 if (CStr.empty()) return;
330
Evan Chenge2ba8972006-11-01 00:27:05 +0000331 const std::string delims(",");
332 std::string::size_type bidx, eidx;
333
334 bidx = CStr.find_first_not_of(delims);
335 while (bidx != std::string::npos) {
336 eidx = CStr.find_first_of(delims, bidx);
337 if (eidx == std::string::npos)
338 eidx = CStr.length();
Chris Lattnera0cca4a2006-11-06 23:49:51 +0000339
Chris Lattner0bb75002006-11-15 02:38:17 +0000340 ParseConstraint(CStr.substr(bidx, eidx), I);
Evan Chenge2ba8972006-11-01 00:27:05 +0000341 bidx = CStr.find_first_not_of(delims, eidx);
342 }
Evan Chenge2ba8972006-11-01 00:27:05 +0000343}
344
Chris Lattner175580c2004-08-14 22:50:53 +0000345CodeGenInstruction::CodeGenInstruction(Record *R, const std::string &AsmStr)
346 : TheDef(R), AsmString(AsmStr) {
Chris Lattnerec352402004-08-01 05:04:00 +0000347 Name = R->getValueAsString("Name");
348 Namespace = R->getValueAsString("Namespace");
Chris Lattnerec352402004-08-01 05:04:00 +0000349
Chris Lattnerec352402004-08-01 05:04:00 +0000350 isReturn = R->getValueAsBit("isReturn");
351 isBranch = R->getValueAsBit("isBranch");
352 isBarrier = R->getValueAsBit("isBarrier");
353 isCall = R->getValueAsBit("isCall");
Nate Begemancdd66b52004-09-28 21:01:45 +0000354 isLoad = R->getValueAsBit("isLoad");
355 isStore = R->getValueAsBit("isStore");
Chris Lattnerf64f9a42006-11-15 23:23:02 +0000356 bool isTwoAddress = R->getValueAsBit("isTwoAddress");
Chris Lattnera818e922006-11-06 21:44:54 +0000357 isPredicated = false; // set below.
Chris Lattneraad75aa2005-01-02 02:29:04 +0000358 isConvertibleToThreeAddress = R->getValueAsBit("isConvertibleToThreeAddress");
359 isCommutable = R->getValueAsBit("isCommutable");
Chris Lattnerec352402004-08-01 05:04:00 +0000360 isTerminator = R->getValueAsBit("isTerminator");
Evan Cheng04677a32007-03-19 06:20:37 +0000361 isReMaterializable = R->getValueAsBit("isReMaterializable");
Chris Lattner5b71d3a2004-09-28 18:38:01 +0000362 hasDelaySlot = R->getValueAsBit("hasDelaySlot");
Chris Lattnere3cbf822005-08-26 20:55:40 +0000363 usesCustomDAGSchedInserter = R->getValueAsBit("usesCustomDAGSchedInserter");
Evan Cheng1c3d19e2005-12-04 08:18:16 +0000364 hasCtrlDep = R->getValueAsBit("hasCtrlDep");
Evan Cheng2b4ea792005-12-26 09:11:45 +0000365 noResults = R->getValueAsBit("noResults");
Chris Lattnercfbf96a2005-08-18 23:38:41 +0000366 hasVariableNumberOfOperands = false;
367
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000368 DagInit *DI;
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000369 try {
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000370 DI = R->getValueAsDag("OperandList");
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000371 } catch (...) {
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000372 // Error getting operand list, just ignore it (sparcv9).
Chris Lattner87c59052004-08-01 07:42:39 +0000373 AsmString.clear();
374 OperandList.clear();
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000375 return;
376 }
377
378 unsigned MIOperandNo = 0;
379 std::set<std::string> OperandNames;
380 for (unsigned i = 0, e = DI->getNumArgs(); i != e; ++i) {
381 DefInit *Arg = dynamic_cast<DefInit*>(DI->getArg(i));
382 if (!Arg)
383 throw "Illegal operand for the '" + R->getName() + "' instruction!";
384
385 Record *Rec = Arg->getDef();
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000386 std::string PrintMethod = "printOperand";
387 unsigned NumOps = 1;
Chris Lattner33670792005-11-19 07:48:33 +0000388 DagInit *MIOpInfo = 0;
Nate Begeman86193d12005-12-01 00:12:04 +0000389 if (Rec->isSubClassOf("Operand")) {
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000390 PrintMethod = Rec->getValueAsString("PrintMethod");
Chris Lattner65303d62005-11-19 07:05:57 +0000391 MIOpInfo = Rec->getValueAsDag("MIOperandInfo");
Chris Lattnerd438b532006-11-03 23:45:17 +0000392
393 // Verify that MIOpInfo has an 'ops' root value.
394 if (!dynamic_cast<DefInit*>(MIOpInfo->getOperator()) ||
395 dynamic_cast<DefInit*>(MIOpInfo->getOperator())
396 ->getDef()->getName() != "ops")
397 throw "Bad value for MIOperandInfo in operand '" + Rec->getName() +
398 "'\n";
399
400 // If we have MIOpInfo, then we have #operands equal to number of entries
401 // in MIOperandInfo.
402 if (unsigned NumArgs = MIOpInfo->getNumArgs())
403 NumOps = NumArgs;
404
Chris Lattnera818e922006-11-06 21:44:54 +0000405 isPredicated |= Rec->isSubClassOf("PredicateOperand");
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000406 } else if (Rec->getName() == "variable_ops") {
407 hasVariableNumberOfOperands = true;
408 continue;
Chris Lattnerf1968392006-11-10 02:01:40 +0000409 } else if (!Rec->isSubClassOf("RegisterClass") &&
410 Rec->getName() != "ptr_rc")
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000411 throw "Unknown operand class '" + Rec->getName() +
412 "' in instruction '" + R->getName() + "' instruction!";
413
Chris Lattnerc4a8b732005-09-14 21:13:50 +0000414 // Check that the operand has a name and that it's unique.
415 if (DI->getArgName(i).empty())
416 throw "In instruction '" + R->getName() + "', operand #" + utostr(i) +
417 " has no name!";
418 if (!OperandNames.insert(DI->getArgName(i)).second)
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000419 throw "In instruction '" + R->getName() + "', operand #" + utostr(i) +
420 " has the same name as a previous operand!";
421
Nate Begeman86193d12005-12-01 00:12:04 +0000422 OperandList.push_back(OperandInfo(Rec, DI->getArgName(i), PrintMethod,
423 MIOperandNo, NumOps, MIOpInfo));
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000424 MIOperandNo += NumOps;
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000425 }
Evan Chenge2ba8972006-11-01 00:27:05 +0000426
Chris Lattnerf64f9a42006-11-15 23:23:02 +0000427 // Parse Constraints.
Chris Lattnera0cca4a2006-11-06 23:49:51 +0000428 ParseConstraints(R->getValueAsString("Constraints"), this);
429
430 // For backward compatibility: isTwoAddress means operand 1 is tied to
431 // operand 0.
Chris Lattner0bb75002006-11-15 02:38:17 +0000432 if (isTwoAddress) {
433 if (!OperandList[1].Constraints[0].empty())
434 throw R->getName() + ": cannot use isTwoAddress property: instruction "
435 "already has constraint set!";
Evan Cheng05551222006-12-01 22:57:41 +0000436 OperandList[1].Constraints[0] = "((0 << 16) | (1 << TOI::TIED_TO))";
Chris Lattner0bb75002006-11-15 02:38:17 +0000437 }
Chris Lattnera0cca4a2006-11-06 23:49:51 +0000438
439 // Any operands with unset constraints get 0 as their constraint.
440 for (unsigned op = 0, e = OperandList.size(); op != e; ++op)
Chris Lattner0bb75002006-11-15 02:38:17 +0000441 for (unsigned j = 0, e = OperandList[op].MINumOperands; j != e; ++j)
442 if (OperandList[op].Constraints[j].empty())
443 OperandList[op].Constraints[j] = "0";
Chris Lattnerf64f9a42006-11-15 23:23:02 +0000444
445 // Parse the DisableEncoding field.
446 std::string DisableEncoding = R->getValueAsString("DisableEncoding");
447 while (1) {
448 std::string OpName = getToken(DisableEncoding, " ,\t");
449 if (OpName.empty()) break;
450
451 // Figure out which operand this is.
452 std::pair<unsigned,unsigned> Op = ParseOperandName(OpName, false);
453
454 // Mark the operand as not-to-be encoded.
455 if (Op.second >= OperandList[Op.first].DoNotEncode.size())
456 OperandList[Op.first].DoNotEncode.resize(Op.second+1);
457 OperandList[Op.first].DoNotEncode[Op.second] = true;
458 }
Chris Lattnerec352402004-08-01 05:04:00 +0000459}
460
461
Chris Lattner87c59052004-08-01 07:42:39 +0000462
463/// getOperandNamed - Return the index of the operand with the specified
464/// non-empty name. If the instruction does not have an operand with the
465/// specified name, throw an exception.
Misha Brukman35e83cc2004-10-14 05:50:43 +0000466///
Chris Lattner87c59052004-08-01 07:42:39 +0000467unsigned CodeGenInstruction::getOperandNamed(const std::string &Name) const {
468 assert(!Name.empty() && "Cannot search for operand with no name!");
469 for (unsigned i = 0, e = OperandList.size(); i != e; ++i)
470 if (OperandList[i].Name == Name) return i;
471 throw "Instruction '" + TheDef->getName() +
472 "' does not have an operand named '$" + Name + "'!";
473}
Evan Cheng0fc71982005-12-08 02:00:36 +0000474
Chris Lattner0bb75002006-11-15 02:38:17 +0000475std::pair<unsigned,unsigned>
476CodeGenInstruction::ParseOperandName(const std::string &Op,
477 bool AllowWholeOp) {
478 if (Op.empty() || Op[0] != '$')
479 throw TheDef->getName() + ": Illegal operand name: '" + Op + "'";
480
481 std::string OpName = Op.substr(1);
482 std::string SubOpName;
483
484 // Check to see if this is $foo.bar.
485 std::string::size_type DotIdx = OpName.find_first_of(".");
486 if (DotIdx != std::string::npos) {
487 SubOpName = OpName.substr(DotIdx+1);
488 if (SubOpName.empty())
489 throw TheDef->getName() + ": illegal empty suboperand name in '" +Op +"'";
490 OpName = OpName.substr(0, DotIdx);
491 }
492
493 unsigned OpIdx = getOperandNamed(OpName);
494
495 if (SubOpName.empty()) { // If no suboperand name was specified:
496 // If one was needed, throw.
497 if (OperandList[OpIdx].MINumOperands > 1 && !AllowWholeOp &&
498 SubOpName.empty())
499 throw TheDef->getName() + ": Illegal to refer to"
500 " whole operand part of complex operand '" + Op + "'";
501
502 // Otherwise, return the operand.
503 return std::make_pair(OpIdx, 0U);
504 }
505
506 // Find the suboperand number involved.
507 DagInit *MIOpInfo = OperandList[OpIdx].MIOperandInfo;
508 if (MIOpInfo == 0)
509 throw TheDef->getName() + ": unknown suboperand name in '" + Op + "'";
510
511 // Find the operand with the right name.
512 for (unsigned i = 0, e = MIOpInfo->getNumArgs(); i != e; ++i)
513 if (MIOpInfo->getArgName(i) == SubOpName)
514 return std::make_pair(OpIdx, i);
515
516 // Otherwise, didn't find it!
517 throw TheDef->getName() + ": unknown suboperand name in '" + Op + "'";
518}
519
520
521
522
Evan Cheng0fc71982005-12-08 02:00:36 +0000523//===----------------------------------------------------------------------===//
524// ComplexPattern implementation
525//
526ComplexPattern::ComplexPattern(Record *R) {
Evan Cheng3aa39f42005-12-08 02:14:08 +0000527 Ty = ::getValueType(R->getValueAsDef("Ty"));
528 NumOperands = R->getValueAsInt("NumOperands");
529 SelectFunc = R->getValueAsString("SelectFunc");
530 RootNodes = R->getValueAsListOfDefs("RootNodes");
Evan Cheng94b30402006-10-11 21:02:01 +0000531
532 // Parse the properties.
533 Properties = 0;
534 std::vector<Record*> PropList = R->getValueAsListOfDefs("Properties");
535 for (unsigned i = 0, e = PropList.size(); i != e; ++i)
536 if (PropList[i]->getName() == "SDNPHasChain") {
537 Properties |= 1 << SDNPHasChain;
538 } else if (PropList[i]->getName() == "SDNPOptInFlag") {
539 Properties |= 1 << SDNPOptInFlag;
540 } else {
Bill Wendlingf5da1332006-12-07 22:21:48 +0000541 cerr << "Unsupported SD Node property '" << PropList[i]->getName()
542 << "' on ComplexPattern '" << R->getName() << "'!\n";
Evan Cheng94b30402006-10-11 21:02:01 +0000543 exit(1);
544 }
Evan Cheng0fc71982005-12-08 02:00:36 +0000545}
Evan Cheng3aa39f42005-12-08 02:14:08 +0000546
Chris Lattner43fbbc32006-03-24 19:49:31 +0000547//===----------------------------------------------------------------------===//
548// CodeGenIntrinsic Implementation
549//===----------------------------------------------------------------------===//
550
551std::vector<CodeGenIntrinsic> llvm::LoadIntrinsics(const RecordKeeper &RC) {
552 std::vector<Record*> I = RC.getAllDerivedDefinitions("Intrinsic");
Chris Lattner8850a1b2006-03-27 22:48:18 +0000553
554 std::vector<CodeGenIntrinsic> Result;
Chris Lattner6a160fb2006-03-28 00:15:00 +0000555
556 // If we are in the context of a target .td file, get the target info so that
557 // we can decode the current intptr_t.
558 CodeGenTarget *CGT = 0;
559 if (Records.getClass("Target") &&
560 Records.getAllDerivedDefinitions("Target").size() == 1)
561 CGT = new CodeGenTarget();
562
Chris Lattner8850a1b2006-03-27 22:48:18 +0000563 for (unsigned i = 0, e = I.size(); i != e; ++i)
Chris Lattner6a160fb2006-03-28 00:15:00 +0000564 Result.push_back(CodeGenIntrinsic(I[i], CGT));
565 delete CGT;
Chris Lattner8850a1b2006-03-27 22:48:18 +0000566 return Result;
Chris Lattner43fbbc32006-03-24 19:49:31 +0000567}
568
Chris Lattner76f8c7c2006-03-28 00:03:08 +0000569CodeGenIntrinsic::CodeGenIntrinsic(Record *R, CodeGenTarget *CGT) {
Chris Lattner2ca956f2006-03-24 20:25:01 +0000570 TheDef = R;
Chris Lattner43fbbc32006-03-24 19:49:31 +0000571 std::string DefName = R->getName();
572 ModRef = WriteMem;
573
574 if (DefName.size() <= 4 ||
575 std::string(DefName.begin(), DefName.begin()+4) != "int_")
576 throw "Intrinsic '" + DefName + "' does not start with 'int_'!";
577 EnumName = std::string(DefName.begin()+4, DefName.end());
578 if (R->getValue("GCCBuiltinName")) // Ignore a missing GCCBuiltinName field.
579 GCCBuiltinName = R->getValueAsString("GCCBuiltinName");
580 TargetPrefix = R->getValueAsString("TargetPrefix");
581 Name = R->getValueAsString("LLVMName");
582 if (Name == "") {
583 // If an explicit name isn't specified, derive one from the DefName.
584 Name = "llvm.";
585 for (unsigned i = 0, e = EnumName.size(); i != e; ++i)
586 if (EnumName[i] == '_')
587 Name += '.';
588 else
589 Name += EnumName[i];
590 } else {
591 // Verify it starts with "llvm.".
592 if (Name.size() <= 5 ||
593 std::string(Name.begin(), Name.begin()+5) != "llvm.")
594 throw "Intrinsic '" + DefName + "'s name does not start with 'llvm.'!";
595 }
596
597 // If TargetPrefix is specified, make sure that Name starts with
598 // "llvm.<targetprefix>.".
599 if (!TargetPrefix.empty()) {
600 if (Name.size() < 6+TargetPrefix.size() ||
601 std::string(Name.begin()+5, Name.begin()+6+TargetPrefix.size())
602 != (TargetPrefix+"."))
603 throw "Intrinsic '" + DefName + "' does not start with 'llvm." +
604 TargetPrefix + ".'!";
605 }
606
607 // Parse the list of argument types.
608 ListInit *TypeList = R->getValueAsListInit("Types");
609 for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) {
Chris Lattner50d45652007-02-27 22:08:27 +0000610 Record *TyEl = TypeList->getElementAsRecord(i);
Chris Lattner43fbbc32006-03-24 19:49:31 +0000611 assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!");
612 ArgTypes.push_back(TyEl->getValueAsString("TypeVal"));
613
Chris Lattner6a160fb2006-03-28 00:15:00 +0000614 if (CGT)
615 ArgVTs.push_back(getValueType(TyEl->getValueAsDef("VT"), CGT));
Chris Lattner43fbbc32006-03-24 19:49:31 +0000616 ArgTypeDefs.push_back(TyEl);
617 }
618 if (ArgTypes.size() == 0)
619 throw "Intrinsic '"+DefName+"' needs at least a type for the ret value!";
620
621 // Parse the intrinsic properties.
622 ListInit *PropList = R->getValueAsListInit("Properties");
623 for (unsigned i = 0, e = PropList->getSize(); i != e; ++i) {
Chris Lattner50d45652007-02-27 22:08:27 +0000624 Record *Property = PropList->getElementAsRecord(i);
Chris Lattner43fbbc32006-03-24 19:49:31 +0000625 assert(Property->isSubClassOf("IntrinsicProperty") &&
626 "Expected a property!");
627
Chris Lattner4b2362e2006-04-10 22:02:59 +0000628 if (Property->getName() == "IntrNoMem")
Chris Lattner43fbbc32006-03-24 19:49:31 +0000629 ModRef = NoMem;
Chris Lattner4b2362e2006-04-10 22:02:59 +0000630 else if (Property->getName() == "IntrReadArgMem")
Chris Lattner43fbbc32006-03-24 19:49:31 +0000631 ModRef = ReadArgMem;
632 else if (Property->getName() == "IntrReadMem")
633 ModRef = ReadMem;
Chris Lattner4b2362e2006-04-10 22:02:59 +0000634 else if (Property->getName() == "IntrWriteArgMem")
Chris Lattner43fbbc32006-03-24 19:49:31 +0000635 ModRef = WriteArgMem;
636 else if (Property->getName() == "IntrWriteMem")
637 ModRef = WriteMem;
638 else
639 assert(0 && "Unknown property!");
640 }
641}