blob: d29ce514bedb0933368e1e28333ad4dfc7135576 [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";
55 case MVT::v16i8: return "MVT::v16i8";
56 case MVT::v8i16: return "MVT::v8i16";
57 case MVT::v4i32: return "MVT::v4i32";
58 case MVT::v2i64: return "MVT::v2i64";
59 case MVT::v2f32: return "MVT::v2f32";
60 case MVT::v4f32: return "MVT::v4f32";
61 case MVT::v2f64: return "MVT::v2f64";
Evan Cheng6b125162006-05-17 20:55:51 +000062 case MVT::iPTR: return "TLI.getPointerTy()";
Chris Lattnerd3464c12003-08-07 23:15:21 +000063 default: assert(0 && "ILLEGAL VALUE TYPE!"); return "";
Chris Lattner45872072003-08-07 05:38:11 +000064 }
Chris Lattnerd3464c12003-08-07 23:15:21 +000065}
66
Chris Lattner2082ebe2004-08-01 03:55:39 +000067std::string llvm::getEnumName(MVT::ValueType T) {
Chris Lattnerb72fb7e2003-08-10 19:50:32 +000068 switch (T) {
Evan Cheng2618d072006-05-17 20:37:59 +000069 case MVT::Other: return "MVT::Other";
70 case MVT::i1: return "MVT::i1";
71 case MVT::i8: return "MVT::i8";
72 case MVT::i16: return "MVT::i16";
73 case MVT::i32: return "MVT::i32";
74 case MVT::i64: return "MVT::i64";
75 case MVT::i128: return "MVT::i128";
76 case MVT::f32: return "MVT::f32";
77 case MVT::f64: return "MVT::f64";
78 case MVT::f80: return "MVT::f80";
79 case MVT::f128: return "MVT::f128";
80 case MVT::Flag: return "MVT::Flag";
81 case MVT::isVoid:return "MVT::isVoid";
82 case MVT::v8i8: return "MVT::v8i8";
83 case MVT::v4i16: return "MVT::v4i16";
84 case MVT::v2i32: return "MVT::v2i32";
85 case MVT::v16i8: return "MVT::v16i8";
86 case MVT::v8i16: return "MVT::v8i16";
87 case MVT::v4i32: return "MVT::v4i32";
88 case MVT::v2i64: return "MVT::v2i64";
89 case MVT::v2f32: return "MVT::v2f32";
90 case MVT::v4f32: return "MVT::v4f32";
91 case MVT::v2f64: return "MVT::v2f64";
Evan Cheng6b125162006-05-17 20:55:51 +000092 case MVT::iPTR: return "TLI.getPointerTy()";
Chris Lattnerb72fb7e2003-08-10 19:50:32 +000093 default: assert(0 && "ILLEGAL VALUE TYPE!"); return "";
94 }
95}
96
Chris Lattnerd3464c12003-08-07 23:15:21 +000097
Chris Lattner2082ebe2004-08-01 03:55:39 +000098std::ostream &llvm::operator<<(std::ostream &OS, MVT::ValueType T) {
Chris Lattnerd3464c12003-08-07 23:15:21 +000099 return OS << getName(T);
Chris Lattner45872072003-08-07 05:38:11 +0000100}
101
102
Chris Lattner45872072003-08-07 05:38:11 +0000103/// getTarget - Return the current instance of the Target class.
104///
Evan Cheng2618d072006-05-17 20:37:59 +0000105CodeGenTarget::CodeGenTarget() {
Chris Lattner45872072003-08-07 05:38:11 +0000106 std::vector<Record*> Targets = Records.getAllDerivedDefinitions("Target");
Misha Brukmanbebdb202004-06-04 14:59:42 +0000107 if (Targets.size() == 0)
Misha Brukman3da94ae2005-04-22 00:00:37 +0000108 throw std::string("ERROR: No 'Target' subclasses defined!");
Chris Lattner45872072003-08-07 05:38:11 +0000109 if (Targets.size() != 1)
110 throw std::string("ERROR: Multiple subclasses of Target defined!");
111 TargetRec = Targets[0];
Chris Lattner45872072003-08-07 05:38:11 +0000112}
113
114
115const std::string &CodeGenTarget::getName() const {
116 return TargetRec->getName();
117}
118
119Record *CodeGenTarget::getInstructionSet() const {
120 return TargetRec->getValueAsDef("InstructionSet");
121}
Brian Gaeked0fde302003-11-11 22:41:34 +0000122
Chris Lattner175580c2004-08-14 22:50:53 +0000123/// getAsmWriter - Return the AssemblyWriter definition for this target.
124///
125Record *CodeGenTarget::getAsmWriter() const {
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000126 std::vector<Record*> LI = TargetRec->getValueAsListOfDefs("AssemblyWriters");
127 if (AsmWriterNum >= LI.size())
Chris Lattner560a79f2004-10-03 19:34:31 +0000128 throw "Target does not have an AsmWriter #" + utostr(AsmWriterNum) + "!";
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000129 return LI[AsmWriterNum];
Chris Lattner175580c2004-08-14 22:50:53 +0000130}
131
Chris Lattner26693112004-08-16 01:10:21 +0000132void CodeGenTarget::ReadRegisters() const {
133 std::vector<Record*> Regs = Records.getAllDerivedDefinitions("Register");
134 if (Regs.empty())
135 throw std::string("No 'Register' subclasses defined!");
136
137 Registers.reserve(Regs.size());
138 Registers.assign(Regs.begin(), Regs.end());
139}
140
Chris Lattner7a680c62004-08-21 02:24:57 +0000141CodeGenRegister::CodeGenRegister(Record *R) : TheDef(R) {
142 DeclaredSpillSize = R->getValueAsInt("SpillSize");
143 DeclaredSpillAlignment = R->getValueAsInt("SpillAlignment");
144}
145
Chris Lattner26693112004-08-16 01:10:21 +0000146const std::string &CodeGenRegister::getName() const {
147 return TheDef->getName();
148}
149
Chris Lattner056afef2004-08-21 04:05:00 +0000150void CodeGenTarget::ReadRegisterClasses() const {
151 std::vector<Record*> RegClasses =
152 Records.getAllDerivedDefinitions("RegisterClass");
153 if (RegClasses.empty())
154 throw std::string("No 'RegisterClass' subclasses defined!");
155
156 RegisterClasses.reserve(RegClasses.size());
157 RegisterClasses.assign(RegClasses.begin(), RegClasses.end());
158}
159
Evan Cheng44a65fa2006-05-16 07:05:30 +0000160std::vector<unsigned char> CodeGenTarget::getRegisterVTs(Record *R) const {
161 std::vector<unsigned char> Result;
162 const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses();
163 for (unsigned i = 0, e = RCs.size(); i != e; ++i) {
164 const CodeGenRegisterClass &RC = RegisterClasses[i];
165 for (unsigned ei = 0, ee = RC.Elements.size(); ei != ee; ++ei) {
166 if (R == RC.Elements[ei]) {
167 const std::vector<MVT::ValueType> &InVTs = RC.getValueTypes();
168 for (unsigned i = 0, e = InVTs.size(); i != e; ++i)
169 Result.push_back(InVTs[i]);
170 }
171 }
172 }
173 return Result;
174}
175
176
Chris Lattner056afef2004-08-21 04:05:00 +0000177CodeGenRegisterClass::CodeGenRegisterClass(Record *R) : TheDef(R) {
Chris Lattnerc67c18f2005-08-19 18:45:20 +0000178 // Rename anonymous register classes.
179 if (R->getName().size() > 9 && R->getName()[9] == '.') {
180 static unsigned AnonCounter = 0;
181 R->setName("AnonRegClass_"+utostr(AnonCounter++));
182 }
183
Nate Begeman6510b222005-12-01 04:51:06 +0000184 std::vector<Record*> TypeList = R->getValueAsListOfDefs("RegTypes");
185 for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
186 Record *Type = TypeList[i];
187 if (!Type->isSubClassOf("ValueType"))
188 throw "RegTypes list member '" + Type->getName() +
189 "' does not derive from the ValueType class!";
190 VTs.push_back(getValueType(Type));
191 }
192 assert(!VTs.empty() && "RegisterClass must contain at least one ValueType!");
Chris Lattnerac468932005-08-19 19:12:51 +0000193
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000194 std::vector<Record*> RegList = R->getValueAsListOfDefs("MemberList");
195 for (unsigned i = 0, e = RegList.size(); i != e; ++i) {
196 Record *Reg = RegList[i];
Chris Lattner056afef2004-08-21 04:05:00 +0000197 if (!Reg->isSubClassOf("Register"))
198 throw "Register Class member '" + Reg->getName() +
199 "' does not derive from the Register class!";
200 Elements.push_back(Reg);
201 }
Nate Begeman6510b222005-12-01 04:51:06 +0000202
203 // Allow targets to override the size in bits of the RegisterClass.
204 unsigned Size = R->getValueAsInt("Size");
205
206 Namespace = R->getValueAsString("Namespace");
207 SpillSize = Size ? Size : MVT::getSizeInBits(VTs[0]);
208 SpillAlignment = R->getValueAsInt("Alignment");
209 MethodBodies = R->getValueAsCode("MethodBodies");
210 MethodProtos = R->getValueAsCode("MethodProtos");
Chris Lattner056afef2004-08-21 04:05:00 +0000211}
212
213const std::string &CodeGenRegisterClass::getName() const {
214 return TheDef->getName();
215}
216
Chris Lattnere9f4ba82005-09-08 21:43:21 +0000217void CodeGenTarget::ReadLegalValueTypes() const {
218 const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses();
219 for (unsigned i = 0, e = RCs.size(); i != e; ++i)
Nate Begeman6510b222005-12-01 04:51:06 +0000220 for (unsigned ri = 0, re = RCs[i].VTs.size(); ri != re; ++ri)
221 LegalValueTypes.push_back(RCs[i].VTs[ri]);
Chris Lattner75ee2eb2005-10-14 03:54:49 +0000222
223 // Remove duplicates.
224 std::sort(LegalValueTypes.begin(), LegalValueTypes.end());
225 LegalValueTypes.erase(std::unique(LegalValueTypes.begin(),
226 LegalValueTypes.end()),
227 LegalValueTypes.end());
Chris Lattnere9f4ba82005-09-08 21:43:21 +0000228}
Chris Lattner056afef2004-08-21 04:05:00 +0000229
Chris Lattner175580c2004-08-14 22:50:53 +0000230
Chris Lattnerec352402004-08-01 05:04:00 +0000231void CodeGenTarget::ReadInstructions() const {
232 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
Chris Lattneraa77d772006-01-27 01:45:06 +0000233 if (Insts.size() <= 2)
Chris Lattnerec352402004-08-01 05:04:00 +0000234 throw std::string("No 'Instruction' subclasses defined!");
235
Chris Lattneraa77d772006-01-27 01:45:06 +0000236 // Parse the instructions defined in the .td file.
Chris Lattner175580c2004-08-14 22:50:53 +0000237 std::string InstFormatName =
238 getAsmWriter()->getValueAsString("InstFormatName");
239
240 for (unsigned i = 0, e = Insts.size(); i != e; ++i) {
241 std::string AsmStr = Insts[i]->getValueAsString(InstFormatName);
242 Instructions.insert(std::make_pair(Insts[i]->getName(),
243 CodeGenInstruction(Insts[i], AsmStr)));
244 }
Chris Lattnerec352402004-08-01 05:04:00 +0000245}
246
Chris Lattnerd6488672005-01-22 18:58:51 +0000247/// getInstructionsByEnumValue - Return all of the instructions defined by the
248/// target, ordered by their enum value.
249void CodeGenTarget::
250getInstructionsByEnumValue(std::vector<const CodeGenInstruction*>
251 &NumberedInstructions) {
Chris Lattneraa77d772006-01-27 01:45:06 +0000252 std::map<std::string, CodeGenInstruction>::const_iterator I;
253 I = getInstructions().find("PHI");
254 if (I == Instructions.end()) throw "Could not find 'PHI' instruction!";
255 const CodeGenInstruction *PHI = &I->second;
256
257 I = getInstructions().find("INLINEASM");
258 if (I == Instructions.end()) throw "Could not find 'INLINEASM' instruction!";
259 const CodeGenInstruction *INLINEASM = &I->second;
260
Jim Laskeya683f9b2007-01-26 17:29:20 +0000261 I = getInstructions().find("LABEL");
262 if (I == Instructions.end()) throw "Could not find 'LABEL' instruction!";
263 const CodeGenInstruction *LABEL = &I->second;
264
Chris Lattnerd6488672005-01-22 18:58:51 +0000265 // Print out the rest of the instructions now.
Chris Lattnerd6488672005-01-22 18:58:51 +0000266 NumberedInstructions.push_back(PHI);
Chris Lattneraa77d772006-01-27 01:45:06 +0000267 NumberedInstructions.push_back(INLINEASM);
Jim Laskeya683f9b2007-01-26 17:29:20 +0000268 NumberedInstructions.push_back(LABEL);
Chris Lattnerd6488672005-01-22 18:58:51 +0000269 for (inst_iterator II = inst_begin(), E = inst_end(); II != E; ++II)
Jim Laskeya683f9b2007-01-26 17:29:20 +0000270 if (&II->second != PHI &&
271 &II->second != INLINEASM &&
272 &II->second != LABEL)
Chris Lattnerd6488672005-01-22 18:58:51 +0000273 NumberedInstructions.push_back(&II->second);
274}
275
276
Misha Brukman35e83cc2004-10-14 05:50:43 +0000277/// isLittleEndianEncoding - Return whether this target encodes its instruction
278/// in little-endian format, i.e. bits laid out in the order [0..n]
279///
280bool CodeGenTarget::isLittleEndianEncoding() const {
281 return getInstructionSet()->getValueAsBit("isLittleEndianEncoding");
282}
283
Chris Lattner0bb75002006-11-15 02:38:17 +0000284
285
286static void ParseConstraint(const std::string &CStr, CodeGenInstruction *I) {
287 // FIXME: Only supports TIED_TO for now.
288 std::string::size_type pos = CStr.find_first_of('=');
Evan Chenge2ba8972006-11-01 00:27:05 +0000289 assert(pos != std::string::npos && "Unrecognized constraint");
Chris Lattner0bb75002006-11-15 02:38:17 +0000290 std::string Name = CStr.substr(0, pos);
Evan Chenge2ba8972006-11-01 00:27:05 +0000291
Evan Cheng4c2b7a32006-11-01 23:03:11 +0000292 // TIED_TO: $src1 = $dst
Chris Lattner0bb75002006-11-15 02:38:17 +0000293 std::string::size_type wpos = Name.find_first_of(" \t");
294 if (wpos == std::string::npos)
295 throw "Illegal format for tied-to constraint: '" + CStr + "'";
296 std::string DestOpName = Name.substr(0, wpos);
297 std::pair<unsigned,unsigned> DestOp = I->ParseOperandName(DestOpName, false);
Evan Chenge2ba8972006-11-01 00:27:05 +0000298
299 Name = CStr.substr(pos+1);
Chris Lattner0bb75002006-11-15 02:38:17 +0000300 wpos = Name.find_first_not_of(" \t");
301 if (wpos == std::string::npos)
302 throw "Illegal format for tied-to constraint: '" + CStr + "'";
303
304 std::pair<unsigned,unsigned> SrcOp =
305 I->ParseOperandName(Name.substr(wpos), false);
306 if (SrcOp > DestOp)
Evan Cheng4c2b7a32006-11-01 23:03:11 +0000307 throw "Illegal tied-to operand constraint '" + CStr + "'";
Chris Lattnera0cca4a2006-11-06 23:49:51 +0000308
Chris Lattner0bb75002006-11-15 02:38:17 +0000309
310 unsigned FlatOpNo = I->getFlattenedOperandNumber(SrcOp);
311 // Build the string for the operand.
312 std::string OpConstraint =
Evan Cheng05551222006-12-01 22:57:41 +0000313 "((" + utostr(FlatOpNo) + " << 16) | (1 << TOI::TIED_TO))";
Chris Lattner0bb75002006-11-15 02:38:17 +0000314
315
316 if (!I->OperandList[DestOp.first].Constraints[DestOp.second].empty())
317 throw "Operand '" + DestOpName + "' cannot have multiple constraints!";
318 I->OperandList[DestOp.first].Constraints[DestOp.second] = OpConstraint;
Evan Chenge2ba8972006-11-01 00:27:05 +0000319}
320
Chris Lattnera0cca4a2006-11-06 23:49:51 +0000321static void ParseConstraints(const std::string &CStr, CodeGenInstruction *I) {
Chris Lattner0bb75002006-11-15 02:38:17 +0000322 // Make sure the constraints list for each operand is large enough to hold
323 // constraint info, even if none is present.
324 for (unsigned i = 0, e = I->OperandList.size(); i != e; ++i)
325 I->OperandList[i].Constraints.resize(I->OperandList[i].MINumOperands);
326
Chris Lattnera0cca4a2006-11-06 23:49:51 +0000327 if (CStr.empty()) return;
328
Evan Chenge2ba8972006-11-01 00:27:05 +0000329 const std::string delims(",");
330 std::string::size_type bidx, eidx;
331
332 bidx = CStr.find_first_not_of(delims);
333 while (bidx != std::string::npos) {
334 eidx = CStr.find_first_of(delims, bidx);
335 if (eidx == std::string::npos)
336 eidx = CStr.length();
Chris Lattnera0cca4a2006-11-06 23:49:51 +0000337
Chris Lattner0bb75002006-11-15 02:38:17 +0000338 ParseConstraint(CStr.substr(bidx, eidx), I);
Evan Chenge2ba8972006-11-01 00:27:05 +0000339 bidx = CStr.find_first_not_of(delims, eidx);
340 }
Evan Chenge2ba8972006-11-01 00:27:05 +0000341}
342
Chris Lattner175580c2004-08-14 22:50:53 +0000343CodeGenInstruction::CodeGenInstruction(Record *R, const std::string &AsmStr)
344 : TheDef(R), AsmString(AsmStr) {
Chris Lattnerec352402004-08-01 05:04:00 +0000345 Name = R->getValueAsString("Name");
346 Namespace = R->getValueAsString("Namespace");
Chris Lattnerec352402004-08-01 05:04:00 +0000347
Chris Lattnerec352402004-08-01 05:04:00 +0000348 isReturn = R->getValueAsBit("isReturn");
349 isBranch = R->getValueAsBit("isBranch");
350 isBarrier = R->getValueAsBit("isBarrier");
351 isCall = R->getValueAsBit("isCall");
Nate Begemancdd66b52004-09-28 21:01:45 +0000352 isLoad = R->getValueAsBit("isLoad");
353 isStore = R->getValueAsBit("isStore");
Chris Lattnerf64f9a42006-11-15 23:23:02 +0000354 bool isTwoAddress = R->getValueAsBit("isTwoAddress");
Chris Lattnera818e922006-11-06 21:44:54 +0000355 isPredicated = false; // set below.
Chris Lattneraad75aa2005-01-02 02:29:04 +0000356 isConvertibleToThreeAddress = R->getValueAsBit("isConvertibleToThreeAddress");
357 isCommutable = R->getValueAsBit("isCommutable");
Chris Lattnerec352402004-08-01 05:04:00 +0000358 isTerminator = R->getValueAsBit("isTerminator");
Chris Lattner5b71d3a2004-09-28 18:38:01 +0000359 hasDelaySlot = R->getValueAsBit("hasDelaySlot");
Chris Lattnere3cbf822005-08-26 20:55:40 +0000360 usesCustomDAGSchedInserter = R->getValueAsBit("usesCustomDAGSchedInserter");
Evan Cheng1c3d19e2005-12-04 08:18:16 +0000361 hasCtrlDep = R->getValueAsBit("hasCtrlDep");
Evan Cheng2b4ea792005-12-26 09:11:45 +0000362 noResults = R->getValueAsBit("noResults");
Chris Lattnercfbf96a2005-08-18 23:38:41 +0000363 hasVariableNumberOfOperands = false;
364
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000365 DagInit *DI;
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000366 try {
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000367 DI = R->getValueAsDag("OperandList");
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000368 } catch (...) {
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000369 // Error getting operand list, just ignore it (sparcv9).
Chris Lattner87c59052004-08-01 07:42:39 +0000370 AsmString.clear();
371 OperandList.clear();
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000372 return;
373 }
374
375 unsigned MIOperandNo = 0;
376 std::set<std::string> OperandNames;
377 for (unsigned i = 0, e = DI->getNumArgs(); i != e; ++i) {
378 DefInit *Arg = dynamic_cast<DefInit*>(DI->getArg(i));
379 if (!Arg)
380 throw "Illegal operand for the '" + R->getName() + "' instruction!";
381
382 Record *Rec = Arg->getDef();
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000383 std::string PrintMethod = "printOperand";
384 unsigned NumOps = 1;
Chris Lattner33670792005-11-19 07:48:33 +0000385 DagInit *MIOpInfo = 0;
Nate Begeman86193d12005-12-01 00:12:04 +0000386 if (Rec->isSubClassOf("Operand")) {
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000387 PrintMethod = Rec->getValueAsString("PrintMethod");
Chris Lattner65303d62005-11-19 07:05:57 +0000388 MIOpInfo = Rec->getValueAsDag("MIOperandInfo");
Chris Lattnerd438b532006-11-03 23:45:17 +0000389
390 // Verify that MIOpInfo has an 'ops' root value.
391 if (!dynamic_cast<DefInit*>(MIOpInfo->getOperator()) ||
392 dynamic_cast<DefInit*>(MIOpInfo->getOperator())
393 ->getDef()->getName() != "ops")
394 throw "Bad value for MIOperandInfo in operand '" + Rec->getName() +
395 "'\n";
396
397 // If we have MIOpInfo, then we have #operands equal to number of entries
398 // in MIOperandInfo.
399 if (unsigned NumArgs = MIOpInfo->getNumArgs())
400 NumOps = NumArgs;
401
Chris Lattnera818e922006-11-06 21:44:54 +0000402 isPredicated |= Rec->isSubClassOf("PredicateOperand");
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000403 } else if (Rec->getName() == "variable_ops") {
404 hasVariableNumberOfOperands = true;
405 continue;
Chris Lattnerf1968392006-11-10 02:01:40 +0000406 } else if (!Rec->isSubClassOf("RegisterClass") &&
407 Rec->getName() != "ptr_rc")
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000408 throw "Unknown operand class '" + Rec->getName() +
409 "' in instruction '" + R->getName() + "' instruction!";
410
Chris Lattnerc4a8b732005-09-14 21:13:50 +0000411 // Check that the operand has a name and that it's unique.
412 if (DI->getArgName(i).empty())
413 throw "In instruction '" + R->getName() + "', operand #" + utostr(i) +
414 " has no name!";
415 if (!OperandNames.insert(DI->getArgName(i)).second)
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000416 throw "In instruction '" + R->getName() + "', operand #" + utostr(i) +
417 " has the same name as a previous operand!";
418
Nate Begeman86193d12005-12-01 00:12:04 +0000419 OperandList.push_back(OperandInfo(Rec, DI->getArgName(i), PrintMethod,
420 MIOperandNo, NumOps, MIOpInfo));
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000421 MIOperandNo += NumOps;
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000422 }
Evan Chenge2ba8972006-11-01 00:27:05 +0000423
Chris Lattnerf64f9a42006-11-15 23:23:02 +0000424 // Parse Constraints.
Chris Lattnera0cca4a2006-11-06 23:49:51 +0000425 ParseConstraints(R->getValueAsString("Constraints"), this);
426
427 // For backward compatibility: isTwoAddress means operand 1 is tied to
428 // operand 0.
Chris Lattner0bb75002006-11-15 02:38:17 +0000429 if (isTwoAddress) {
430 if (!OperandList[1].Constraints[0].empty())
431 throw R->getName() + ": cannot use isTwoAddress property: instruction "
432 "already has constraint set!";
Evan Cheng05551222006-12-01 22:57:41 +0000433 OperandList[1].Constraints[0] = "((0 << 16) | (1 << TOI::TIED_TO))";
Chris Lattner0bb75002006-11-15 02:38:17 +0000434 }
Chris Lattnera0cca4a2006-11-06 23:49:51 +0000435
436 // Any operands with unset constraints get 0 as their constraint.
437 for (unsigned op = 0, e = OperandList.size(); op != e; ++op)
Chris Lattner0bb75002006-11-15 02:38:17 +0000438 for (unsigned j = 0, e = OperandList[op].MINumOperands; j != e; ++j)
439 if (OperandList[op].Constraints[j].empty())
440 OperandList[op].Constraints[j] = "0";
Chris Lattnerf64f9a42006-11-15 23:23:02 +0000441
442 // Parse the DisableEncoding field.
443 std::string DisableEncoding = R->getValueAsString("DisableEncoding");
444 while (1) {
445 std::string OpName = getToken(DisableEncoding, " ,\t");
446 if (OpName.empty()) break;
447
448 // Figure out which operand this is.
449 std::pair<unsigned,unsigned> Op = ParseOperandName(OpName, false);
450
451 // Mark the operand as not-to-be encoded.
452 if (Op.second >= OperandList[Op.first].DoNotEncode.size())
453 OperandList[Op.first].DoNotEncode.resize(Op.second+1);
454 OperandList[Op.first].DoNotEncode[Op.second] = true;
455 }
Chris Lattnerec352402004-08-01 05:04:00 +0000456}
457
458
Chris Lattner87c59052004-08-01 07:42:39 +0000459
460/// getOperandNamed - Return the index of the operand with the specified
461/// non-empty name. If the instruction does not have an operand with the
462/// specified name, throw an exception.
Misha Brukman35e83cc2004-10-14 05:50:43 +0000463///
Chris Lattner87c59052004-08-01 07:42:39 +0000464unsigned CodeGenInstruction::getOperandNamed(const std::string &Name) const {
465 assert(!Name.empty() && "Cannot search for operand with no name!");
466 for (unsigned i = 0, e = OperandList.size(); i != e; ++i)
467 if (OperandList[i].Name == Name) return i;
468 throw "Instruction '" + TheDef->getName() +
469 "' does not have an operand named '$" + Name + "'!";
470}
Evan Cheng0fc71982005-12-08 02:00:36 +0000471
Chris Lattner0bb75002006-11-15 02:38:17 +0000472std::pair<unsigned,unsigned>
473CodeGenInstruction::ParseOperandName(const std::string &Op,
474 bool AllowWholeOp) {
475 if (Op.empty() || Op[0] != '$')
476 throw TheDef->getName() + ": Illegal operand name: '" + Op + "'";
477
478 std::string OpName = Op.substr(1);
479 std::string SubOpName;
480
481 // Check to see if this is $foo.bar.
482 std::string::size_type DotIdx = OpName.find_first_of(".");
483 if (DotIdx != std::string::npos) {
484 SubOpName = OpName.substr(DotIdx+1);
485 if (SubOpName.empty())
486 throw TheDef->getName() + ": illegal empty suboperand name in '" +Op +"'";
487 OpName = OpName.substr(0, DotIdx);
488 }
489
490 unsigned OpIdx = getOperandNamed(OpName);
491
492 if (SubOpName.empty()) { // If no suboperand name was specified:
493 // If one was needed, throw.
494 if (OperandList[OpIdx].MINumOperands > 1 && !AllowWholeOp &&
495 SubOpName.empty())
496 throw TheDef->getName() + ": Illegal to refer to"
497 " whole operand part of complex operand '" + Op + "'";
498
499 // Otherwise, return the operand.
500 return std::make_pair(OpIdx, 0U);
501 }
502
503 // Find the suboperand number involved.
504 DagInit *MIOpInfo = OperandList[OpIdx].MIOperandInfo;
505 if (MIOpInfo == 0)
506 throw TheDef->getName() + ": unknown suboperand name in '" + Op + "'";
507
508 // Find the operand with the right name.
509 for (unsigned i = 0, e = MIOpInfo->getNumArgs(); i != e; ++i)
510 if (MIOpInfo->getArgName(i) == SubOpName)
511 return std::make_pair(OpIdx, i);
512
513 // Otherwise, didn't find it!
514 throw TheDef->getName() + ": unknown suboperand name in '" + Op + "'";
515}
516
517
518
519
Evan Cheng0fc71982005-12-08 02:00:36 +0000520//===----------------------------------------------------------------------===//
521// ComplexPattern implementation
522//
523ComplexPattern::ComplexPattern(Record *R) {
Evan Cheng3aa39f42005-12-08 02:14:08 +0000524 Ty = ::getValueType(R->getValueAsDef("Ty"));
525 NumOperands = R->getValueAsInt("NumOperands");
526 SelectFunc = R->getValueAsString("SelectFunc");
527 RootNodes = R->getValueAsListOfDefs("RootNodes");
Evan Cheng94b30402006-10-11 21:02:01 +0000528
529 // Parse the properties.
530 Properties = 0;
531 std::vector<Record*> PropList = R->getValueAsListOfDefs("Properties");
532 for (unsigned i = 0, e = PropList.size(); i != e; ++i)
533 if (PropList[i]->getName() == "SDNPHasChain") {
534 Properties |= 1 << SDNPHasChain;
535 } else if (PropList[i]->getName() == "SDNPOptInFlag") {
536 Properties |= 1 << SDNPOptInFlag;
537 } else {
Bill Wendlingf5da1332006-12-07 22:21:48 +0000538 cerr << "Unsupported SD Node property '" << PropList[i]->getName()
539 << "' on ComplexPattern '" << R->getName() << "'!\n";
Evan Cheng94b30402006-10-11 21:02:01 +0000540 exit(1);
541 }
Evan Cheng0fc71982005-12-08 02:00:36 +0000542}
Evan Cheng3aa39f42005-12-08 02:14:08 +0000543
Chris Lattner43fbbc32006-03-24 19:49:31 +0000544//===----------------------------------------------------------------------===//
545// CodeGenIntrinsic Implementation
546//===----------------------------------------------------------------------===//
547
548std::vector<CodeGenIntrinsic> llvm::LoadIntrinsics(const RecordKeeper &RC) {
549 std::vector<Record*> I = RC.getAllDerivedDefinitions("Intrinsic");
Chris Lattner8850a1b2006-03-27 22:48:18 +0000550
551 std::vector<CodeGenIntrinsic> Result;
Chris Lattner6a160fb2006-03-28 00:15:00 +0000552
553 // If we are in the context of a target .td file, get the target info so that
554 // we can decode the current intptr_t.
555 CodeGenTarget *CGT = 0;
556 if (Records.getClass("Target") &&
557 Records.getAllDerivedDefinitions("Target").size() == 1)
558 CGT = new CodeGenTarget();
559
Chris Lattner8850a1b2006-03-27 22:48:18 +0000560 for (unsigned i = 0, e = I.size(); i != e; ++i)
Chris Lattner6a160fb2006-03-28 00:15:00 +0000561 Result.push_back(CodeGenIntrinsic(I[i], CGT));
562 delete CGT;
Chris Lattner8850a1b2006-03-27 22:48:18 +0000563 return Result;
Chris Lattner43fbbc32006-03-24 19:49:31 +0000564}
565
Chris Lattner76f8c7c2006-03-28 00:03:08 +0000566CodeGenIntrinsic::CodeGenIntrinsic(Record *R, CodeGenTarget *CGT) {
Chris Lattner2ca956f2006-03-24 20:25:01 +0000567 TheDef = R;
Chris Lattner43fbbc32006-03-24 19:49:31 +0000568 std::string DefName = R->getName();
569 ModRef = WriteMem;
570
571 if (DefName.size() <= 4 ||
572 std::string(DefName.begin(), DefName.begin()+4) != "int_")
573 throw "Intrinsic '" + DefName + "' does not start with 'int_'!";
574 EnumName = std::string(DefName.begin()+4, DefName.end());
575 if (R->getValue("GCCBuiltinName")) // Ignore a missing GCCBuiltinName field.
576 GCCBuiltinName = R->getValueAsString("GCCBuiltinName");
577 TargetPrefix = R->getValueAsString("TargetPrefix");
578 Name = R->getValueAsString("LLVMName");
579 if (Name == "") {
580 // If an explicit name isn't specified, derive one from the DefName.
581 Name = "llvm.";
582 for (unsigned i = 0, e = EnumName.size(); i != e; ++i)
583 if (EnumName[i] == '_')
584 Name += '.';
585 else
586 Name += EnumName[i];
587 } else {
588 // Verify it starts with "llvm.".
589 if (Name.size() <= 5 ||
590 std::string(Name.begin(), Name.begin()+5) != "llvm.")
591 throw "Intrinsic '" + DefName + "'s name does not start with 'llvm.'!";
592 }
593
594 // If TargetPrefix is specified, make sure that Name starts with
595 // "llvm.<targetprefix>.".
596 if (!TargetPrefix.empty()) {
597 if (Name.size() < 6+TargetPrefix.size() ||
598 std::string(Name.begin()+5, Name.begin()+6+TargetPrefix.size())
599 != (TargetPrefix+"."))
600 throw "Intrinsic '" + DefName + "' does not start with 'llvm." +
601 TargetPrefix + ".'!";
602 }
603
604 // Parse the list of argument types.
605 ListInit *TypeList = R->getValueAsListInit("Types");
606 for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) {
Chris Lattner50d45652007-02-27 22:08:27 +0000607 Record *TyEl = TypeList->getElementAsRecord(i);
Chris Lattner43fbbc32006-03-24 19:49:31 +0000608 assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!");
609 ArgTypes.push_back(TyEl->getValueAsString("TypeVal"));
610
Chris Lattner6a160fb2006-03-28 00:15:00 +0000611 if (CGT)
612 ArgVTs.push_back(getValueType(TyEl->getValueAsDef("VT"), CGT));
Chris Lattner43fbbc32006-03-24 19:49:31 +0000613 ArgTypeDefs.push_back(TyEl);
614 }
615 if (ArgTypes.size() == 0)
616 throw "Intrinsic '"+DefName+"' needs at least a type for the ret value!";
617
618 // Parse the intrinsic properties.
619 ListInit *PropList = R->getValueAsListInit("Properties");
620 for (unsigned i = 0, e = PropList->getSize(); i != e; ++i) {
Chris Lattner50d45652007-02-27 22:08:27 +0000621 Record *Property = PropList->getElementAsRecord(i);
Chris Lattner43fbbc32006-03-24 19:49:31 +0000622 assert(Property->isSubClassOf("IntrinsicProperty") &&
623 "Expected a property!");
624
Chris Lattner4b2362e2006-04-10 22:02:59 +0000625 if (Property->getName() == "IntrNoMem")
Chris Lattner43fbbc32006-03-24 19:49:31 +0000626 ModRef = NoMem;
Chris Lattner4b2362e2006-04-10 22:02:59 +0000627 else if (Property->getName() == "IntrReadArgMem")
Chris Lattner43fbbc32006-03-24 19:49:31 +0000628 ModRef = ReadArgMem;
629 else if (Property->getName() == "IntrReadMem")
630 ModRef = ReadMem;
Chris Lattner4b2362e2006-04-10 22:02:59 +0000631 else if (Property->getName() == "IntrWriteArgMem")
Chris Lattner43fbbc32006-03-24 19:49:31 +0000632 ModRef = WriteArgMem;
633 else if (Property->getName() == "IntrWriteMem")
634 ModRef = WriteMem;
635 else
636 assert(0 && "Unknown property!");
637 }
638}