blob: 7952ca79aa8a6dac3b8c6f2bbd7bdffe42a8b69c [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.
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";
Evan Chengd7c2c862006-06-15 00:16:37 +000047 case MVT::f32: return "MVT::f32";
48 case MVT::f64: return "MVT::f64";
49 case MVT::f80: return "MVT::f80";
50 case MVT::f128: return "MVT::f128";
51 case MVT::Flag: return "MVT::Flag";
52 case MVT::isVoid:return "MVT::void";
53 case MVT::v8i8: return "MVT::v8i8";
54 case MVT::v4i16: return "MVT::v4i16";
55 case MVT::v2i32: return "MVT::v2i32";
Bill Wendlingeebc8a12007-03-26 07:53:08 +000056 case MVT::v1i64: return "MVT::v1i64";
Evan Chengd7c2c862006-06-15 00:16:37 +000057 case MVT::v16i8: return "MVT::v16i8";
58 case MVT::v8i16: return "MVT::v8i16";
59 case MVT::v4i32: return "MVT::v4i32";
60 case MVT::v2i64: return "MVT::v2i64";
61 case MVT::v2f32: return "MVT::v2f32";
62 case MVT::v4f32: return "MVT::v4f32";
63 case MVT::v2f64: return "MVT::v2f64";
Christopher Lamb82455102007-07-26 06:41:18 +000064 case MVT::v3i32: return "MVT::v3i32";
65 case MVT::v3f32: return "MVT::v3f32";
Evan Cheng6b125162006-05-17 20:55:51 +000066 case MVT::iPTR: return "TLI.getPointerTy()";
Chris Lattnerd3464c12003-08-07 23:15:21 +000067 default: assert(0 && "ILLEGAL VALUE TYPE!"); return "";
Chris Lattner45872072003-08-07 05:38:11 +000068 }
Chris Lattnerd3464c12003-08-07 23:15:21 +000069}
70
Chris Lattner2082ebe2004-08-01 03:55:39 +000071std::string llvm::getEnumName(MVT::ValueType T) {
Chris Lattnerb72fb7e2003-08-10 19:50:32 +000072 switch (T) {
Evan Cheng2618d072006-05-17 20:37:59 +000073 case MVT::Other: return "MVT::Other";
74 case MVT::i1: return "MVT::i1";
75 case MVT::i8: return "MVT::i8";
76 case MVT::i16: return "MVT::i16";
77 case MVT::i32: return "MVT::i32";
78 case MVT::i64: return "MVT::i64";
79 case MVT::i128: return "MVT::i128";
Reid Spencerc4de3de2007-04-01 07:20:02 +000080 case MVT::iAny: return "MVT::iAny";
Evan Cheng2618d072006-05-17 20:37:59 +000081 case MVT::f32: return "MVT::f32";
82 case MVT::f64: return "MVT::f64";
83 case MVT::f80: return "MVT::f80";
84 case MVT::f128: return "MVT::f128";
85 case MVT::Flag: return "MVT::Flag";
86 case MVT::isVoid:return "MVT::isVoid";
87 case MVT::v8i8: return "MVT::v8i8";
88 case MVT::v4i16: return "MVT::v4i16";
89 case MVT::v2i32: return "MVT::v2i32";
Bill Wendlingeebc8a12007-03-26 07:53:08 +000090 case MVT::v1i64: return "MVT::v1i64";
Evan Cheng2618d072006-05-17 20:37:59 +000091 case MVT::v16i8: return "MVT::v16i8";
92 case MVT::v8i16: return "MVT::v8i16";
93 case MVT::v4i32: return "MVT::v4i32";
94 case MVT::v2i64: return "MVT::v2i64";
95 case MVT::v2f32: return "MVT::v2f32";
96 case MVT::v4f32: return "MVT::v4f32";
97 case MVT::v2f64: return "MVT::v2f64";
Christopher Lamb82455102007-07-26 06:41:18 +000098 case MVT::v3i32: return "MVT::v3i32";
99 case MVT::v3f32: return "MVT::v3f32";
Evan Cheng6b125162006-05-17 20:55:51 +0000100 case MVT::iPTR: return "TLI.getPointerTy()";
Chris Lattnerb72fb7e2003-08-10 19:50:32 +0000101 default: assert(0 && "ILLEGAL VALUE TYPE!"); return "";
102 }
103}
104
Chris Lattnerd3464c12003-08-07 23:15:21 +0000105
Chris Lattner45872072003-08-07 05:38:11 +0000106/// getTarget - Return the current instance of the Target class.
107///
Evan Cheng2618d072006-05-17 20:37:59 +0000108CodeGenTarget::CodeGenTarget() {
Chris Lattner45872072003-08-07 05:38:11 +0000109 std::vector<Record*> Targets = Records.getAllDerivedDefinitions("Target");
Misha Brukmanbebdb202004-06-04 14:59:42 +0000110 if (Targets.size() == 0)
Misha Brukman3da94ae2005-04-22 00:00:37 +0000111 throw std::string("ERROR: No 'Target' subclasses defined!");
Chris Lattner45872072003-08-07 05:38:11 +0000112 if (Targets.size() != 1)
113 throw std::string("ERROR: Multiple subclasses of Target defined!");
114 TargetRec = Targets[0];
Chris Lattner45872072003-08-07 05:38:11 +0000115}
116
117
118const std::string &CodeGenTarget::getName() const {
119 return TargetRec->getName();
120}
121
122Record *CodeGenTarget::getInstructionSet() const {
123 return TargetRec->getValueAsDef("InstructionSet");
124}
Brian Gaeked0fde302003-11-11 22:41:34 +0000125
Chris Lattner175580c2004-08-14 22:50:53 +0000126/// getAsmWriter - Return the AssemblyWriter definition for this target.
127///
128Record *CodeGenTarget::getAsmWriter() const {
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000129 std::vector<Record*> LI = TargetRec->getValueAsListOfDefs("AssemblyWriters");
130 if (AsmWriterNum >= LI.size())
Chris Lattner560a79f2004-10-03 19:34:31 +0000131 throw "Target does not have an AsmWriter #" + utostr(AsmWriterNum) + "!";
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000132 return LI[AsmWriterNum];
Chris Lattner175580c2004-08-14 22:50:53 +0000133}
134
Chris Lattner26693112004-08-16 01:10:21 +0000135void CodeGenTarget::ReadRegisters() const {
136 std::vector<Record*> Regs = Records.getAllDerivedDefinitions("Register");
137 if (Regs.empty())
138 throw std::string("No 'Register' subclasses defined!");
139
140 Registers.reserve(Regs.size());
141 Registers.assign(Regs.begin(), Regs.end());
142}
143
Chris Lattner7a680c62004-08-21 02:24:57 +0000144CodeGenRegister::CodeGenRegister(Record *R) : TheDef(R) {
145 DeclaredSpillSize = R->getValueAsInt("SpillSize");
146 DeclaredSpillAlignment = R->getValueAsInt("SpillAlignment");
147}
148
Chris Lattner26693112004-08-16 01:10:21 +0000149const std::string &CodeGenRegister::getName() const {
150 return TheDef->getName();
151}
152
Chris Lattner056afef2004-08-21 04:05:00 +0000153void CodeGenTarget::ReadRegisterClasses() const {
154 std::vector<Record*> RegClasses =
155 Records.getAllDerivedDefinitions("RegisterClass");
156 if (RegClasses.empty())
157 throw std::string("No 'RegisterClass' subclasses defined!");
158
159 RegisterClasses.reserve(RegClasses.size());
160 RegisterClasses.assign(RegClasses.begin(), RegClasses.end());
161}
162
Evan Cheng44a65fa2006-05-16 07:05:30 +0000163std::vector<unsigned char> CodeGenTarget::getRegisterVTs(Record *R) const {
164 std::vector<unsigned char> Result;
165 const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses();
166 for (unsigned i = 0, e = RCs.size(); i != e; ++i) {
167 const CodeGenRegisterClass &RC = RegisterClasses[i];
168 for (unsigned ei = 0, ee = RC.Elements.size(); ei != ee; ++ei) {
169 if (R == RC.Elements[ei]) {
170 const std::vector<MVT::ValueType> &InVTs = RC.getValueTypes();
171 for (unsigned i = 0, e = InVTs.size(); i != e; ++i)
172 Result.push_back(InVTs[i]);
173 }
174 }
175 }
176 return Result;
177}
178
179
Chris Lattner056afef2004-08-21 04:05:00 +0000180CodeGenRegisterClass::CodeGenRegisterClass(Record *R) : TheDef(R) {
Chris Lattnerc67c18f2005-08-19 18:45:20 +0000181 // Rename anonymous register classes.
182 if (R->getName().size() > 9 && R->getName()[9] == '.') {
183 static unsigned AnonCounter = 0;
184 R->setName("AnonRegClass_"+utostr(AnonCounter++));
185 }
186
Nate Begeman6510b222005-12-01 04:51:06 +0000187 std::vector<Record*> TypeList = R->getValueAsListOfDefs("RegTypes");
188 for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
189 Record *Type = TypeList[i];
190 if (!Type->isSubClassOf("ValueType"))
191 throw "RegTypes list member '" + Type->getName() +
192 "' does not derive from the ValueType class!";
193 VTs.push_back(getValueType(Type));
194 }
195 assert(!VTs.empty() && "RegisterClass must contain at least one ValueType!");
Chris Lattnerac468932005-08-19 19:12:51 +0000196
Chris Lattnerb0e103d2005-10-28 22:49:02 +0000197 std::vector<Record*> RegList = R->getValueAsListOfDefs("MemberList");
198 for (unsigned i = 0, e = RegList.size(); i != e; ++i) {
199 Record *Reg = RegList[i];
Chris Lattner056afef2004-08-21 04:05:00 +0000200 if (!Reg->isSubClassOf("Register"))
201 throw "Register Class member '" + Reg->getName() +
202 "' does not derive from the Register class!";
203 Elements.push_back(Reg);
204 }
Nate Begeman6510b222005-12-01 04:51:06 +0000205
Christopher Lamba3211252007-06-13 22:20:15 +0000206 std::vector<Record*> SubRegClassList =
207 R->getValueAsListOfDefs("SubRegClassList");
208 for (unsigned i = 0, e = SubRegClassList.size(); i != e; ++i) {
209 Record *SubRegClass = SubRegClassList[i];
210 if (!SubRegClass->isSubClassOf("RegisterClass"))
211 throw "Register Class member '" + SubRegClass->getName() +
212 "' does not derive from the RegisterClass class!";
213 SubRegClasses.push_back(SubRegClass);
214 }
215
Nate Begeman6510b222005-12-01 04:51:06 +0000216 // Allow targets to override the size in bits of the RegisterClass.
217 unsigned Size = R->getValueAsInt("Size");
218
219 Namespace = R->getValueAsString("Namespace");
220 SpillSize = Size ? Size : MVT::getSizeInBits(VTs[0]);
221 SpillAlignment = R->getValueAsInt("Alignment");
222 MethodBodies = R->getValueAsCode("MethodBodies");
223 MethodProtos = R->getValueAsCode("MethodProtos");
Chris Lattner056afef2004-08-21 04:05:00 +0000224}
225
226const std::string &CodeGenRegisterClass::getName() const {
227 return TheDef->getName();
228}
229
Chris Lattnere9f4ba82005-09-08 21:43:21 +0000230void CodeGenTarget::ReadLegalValueTypes() const {
231 const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses();
232 for (unsigned i = 0, e = RCs.size(); i != e; ++i)
Nate Begeman6510b222005-12-01 04:51:06 +0000233 for (unsigned ri = 0, re = RCs[i].VTs.size(); ri != re; ++ri)
234 LegalValueTypes.push_back(RCs[i].VTs[ri]);
Chris Lattner75ee2eb2005-10-14 03:54:49 +0000235
236 // Remove duplicates.
237 std::sort(LegalValueTypes.begin(), LegalValueTypes.end());
238 LegalValueTypes.erase(std::unique(LegalValueTypes.begin(),
239 LegalValueTypes.end()),
240 LegalValueTypes.end());
Chris Lattnere9f4ba82005-09-08 21:43:21 +0000241}
Chris Lattner056afef2004-08-21 04:05:00 +0000242
Chris Lattner175580c2004-08-14 22:50:53 +0000243
Chris Lattnerec352402004-08-01 05:04:00 +0000244void CodeGenTarget::ReadInstructions() const {
245 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
Chris Lattneraa77d772006-01-27 01:45:06 +0000246 if (Insts.size() <= 2)
Chris Lattnerec352402004-08-01 05:04:00 +0000247 throw std::string("No 'Instruction' subclasses defined!");
248
Chris Lattneraa77d772006-01-27 01:45:06 +0000249 // Parse the instructions defined in the .td file.
Chris Lattner175580c2004-08-14 22:50:53 +0000250 std::string InstFormatName =
251 getAsmWriter()->getValueAsString("InstFormatName");
252
253 for (unsigned i = 0, e = Insts.size(); i != e; ++i) {
254 std::string AsmStr = Insts[i]->getValueAsString(InstFormatName);
255 Instructions.insert(std::make_pair(Insts[i]->getName(),
256 CodeGenInstruction(Insts[i], AsmStr)));
257 }
Chris Lattnerec352402004-08-01 05:04:00 +0000258}
259
Chris Lattnerd6488672005-01-22 18:58:51 +0000260/// getInstructionsByEnumValue - Return all of the instructions defined by the
261/// target, ordered by their enum value.
262void CodeGenTarget::
263getInstructionsByEnumValue(std::vector<const CodeGenInstruction*>
264 &NumberedInstructions) {
Chris Lattneraa77d772006-01-27 01:45:06 +0000265 std::map<std::string, CodeGenInstruction>::const_iterator I;
266 I = getInstructions().find("PHI");
267 if (I == Instructions.end()) throw "Could not find 'PHI' instruction!";
268 const CodeGenInstruction *PHI = &I->second;
269
270 I = getInstructions().find("INLINEASM");
271 if (I == Instructions.end()) throw "Could not find 'INLINEASM' instruction!";
272 const CodeGenInstruction *INLINEASM = &I->second;
273
Jim Laskeya683f9b2007-01-26 17:29:20 +0000274 I = getInstructions().find("LABEL");
275 if (I == Instructions.end()) throw "Could not find 'LABEL' instruction!";
276 const CodeGenInstruction *LABEL = &I->second;
277
Chris Lattnerd6488672005-01-22 18:58:51 +0000278 // Print out the rest of the instructions now.
Chris Lattnerd6488672005-01-22 18:58:51 +0000279 NumberedInstructions.push_back(PHI);
Chris Lattneraa77d772006-01-27 01:45:06 +0000280 NumberedInstructions.push_back(INLINEASM);
Jim Laskeya683f9b2007-01-26 17:29:20 +0000281 NumberedInstructions.push_back(LABEL);
Chris Lattnerd6488672005-01-22 18:58:51 +0000282 for (inst_iterator II = inst_begin(), E = inst_end(); II != E; ++II)
Jim Laskeya683f9b2007-01-26 17:29:20 +0000283 if (&II->second != PHI &&
284 &II->second != INLINEASM &&
285 &II->second != LABEL)
Chris Lattnerd6488672005-01-22 18:58:51 +0000286 NumberedInstructions.push_back(&II->second);
287}
288
289
Misha Brukman35e83cc2004-10-14 05:50:43 +0000290/// isLittleEndianEncoding - Return whether this target encodes its instruction
291/// in little-endian format, i.e. bits laid out in the order [0..n]
292///
293bool CodeGenTarget::isLittleEndianEncoding() const {
294 return getInstructionSet()->getValueAsBit("isLittleEndianEncoding");
295}
296
Chris Lattner0bb75002006-11-15 02:38:17 +0000297
298
299static void ParseConstraint(const std::string &CStr, CodeGenInstruction *I) {
300 // FIXME: Only supports TIED_TO for now.
301 std::string::size_type pos = CStr.find_first_of('=');
Evan Chenge2ba8972006-11-01 00:27:05 +0000302 assert(pos != std::string::npos && "Unrecognized constraint");
Chris Lattner0bb75002006-11-15 02:38:17 +0000303 std::string Name = CStr.substr(0, pos);
Evan Chenge2ba8972006-11-01 00:27:05 +0000304
Evan Cheng4c2b7a32006-11-01 23:03:11 +0000305 // TIED_TO: $src1 = $dst
Chris Lattner0bb75002006-11-15 02:38:17 +0000306 std::string::size_type wpos = Name.find_first_of(" \t");
307 if (wpos == std::string::npos)
308 throw "Illegal format for tied-to constraint: '" + CStr + "'";
309 std::string DestOpName = Name.substr(0, wpos);
310 std::pair<unsigned,unsigned> DestOp = I->ParseOperandName(DestOpName, false);
Evan Chenge2ba8972006-11-01 00:27:05 +0000311
312 Name = CStr.substr(pos+1);
Chris Lattner0bb75002006-11-15 02:38:17 +0000313 wpos = Name.find_first_not_of(" \t");
314 if (wpos == std::string::npos)
315 throw "Illegal format for tied-to constraint: '" + CStr + "'";
316
317 std::pair<unsigned,unsigned> SrcOp =
318 I->ParseOperandName(Name.substr(wpos), false);
319 if (SrcOp > DestOp)
Evan Cheng4c2b7a32006-11-01 23:03:11 +0000320 throw "Illegal tied-to operand constraint '" + CStr + "'";
Chris Lattnera0cca4a2006-11-06 23:49:51 +0000321
Chris Lattner0bb75002006-11-15 02:38:17 +0000322
323 unsigned FlatOpNo = I->getFlattenedOperandNumber(SrcOp);
324 // Build the string for the operand.
325 std::string OpConstraint =
Evan Cheng05551222006-12-01 22:57:41 +0000326 "((" + utostr(FlatOpNo) + " << 16) | (1 << TOI::TIED_TO))";
Chris Lattner0bb75002006-11-15 02:38:17 +0000327
328
329 if (!I->OperandList[DestOp.first].Constraints[DestOp.second].empty())
330 throw "Operand '" + DestOpName + "' cannot have multiple constraints!";
331 I->OperandList[DestOp.first].Constraints[DestOp.second] = OpConstraint;
Evan Chenge2ba8972006-11-01 00:27:05 +0000332}
333
Chris Lattnera0cca4a2006-11-06 23:49:51 +0000334static void ParseConstraints(const std::string &CStr, CodeGenInstruction *I) {
Chris Lattner0bb75002006-11-15 02:38:17 +0000335 // Make sure the constraints list for each operand is large enough to hold
336 // constraint info, even if none is present.
337 for (unsigned i = 0, e = I->OperandList.size(); i != e; ++i)
338 I->OperandList[i].Constraints.resize(I->OperandList[i].MINumOperands);
339
Chris Lattnera0cca4a2006-11-06 23:49:51 +0000340 if (CStr.empty()) return;
341
Evan Chenge2ba8972006-11-01 00:27:05 +0000342 const std::string delims(",");
343 std::string::size_type bidx, eidx;
344
345 bidx = CStr.find_first_not_of(delims);
346 while (bidx != std::string::npos) {
347 eidx = CStr.find_first_of(delims, bidx);
348 if (eidx == std::string::npos)
349 eidx = CStr.length();
Chris Lattnera0cca4a2006-11-06 23:49:51 +0000350
Chris Lattner0bb75002006-11-15 02:38:17 +0000351 ParseConstraint(CStr.substr(bidx, eidx), I);
Evan Chenge2ba8972006-11-01 00:27:05 +0000352 bidx = CStr.find_first_not_of(delims, eidx);
353 }
Evan Chenge2ba8972006-11-01 00:27:05 +0000354}
355
Chris Lattner175580c2004-08-14 22:50:53 +0000356CodeGenInstruction::CodeGenInstruction(Record *R, const std::string &AsmStr)
357 : TheDef(R), AsmString(AsmStr) {
Chris Lattnerec352402004-08-01 05:04:00 +0000358 Name = R->getValueAsString("Name");
359 Namespace = R->getValueAsString("Namespace");
Chris Lattnerec352402004-08-01 05:04:00 +0000360
Chris Lattnerec352402004-08-01 05:04:00 +0000361 isReturn = R->getValueAsBit("isReturn");
362 isBranch = R->getValueAsBit("isBranch");
363 isBarrier = R->getValueAsBit("isBarrier");
364 isCall = R->getValueAsBit("isCall");
Nate Begemancdd66b52004-09-28 21:01:45 +0000365 isLoad = R->getValueAsBit("isLoad");
366 isStore = R->getValueAsBit("isStore");
Chris Lattnerf64f9a42006-11-15 23:23:02 +0000367 bool isTwoAddress = R->getValueAsBit("isTwoAddress");
Evan Cheng5127ce02007-05-16 20:45:24 +0000368 isPredicable = R->getValueAsBit("isPredicable");
Chris Lattneraad75aa2005-01-02 02:29:04 +0000369 isConvertibleToThreeAddress = R->getValueAsBit("isConvertibleToThreeAddress");
370 isCommutable = R->getValueAsBit("isCommutable");
Chris Lattnerec352402004-08-01 05:04:00 +0000371 isTerminator = R->getValueAsBit("isTerminator");
Dan Gohmand45eddd2007-06-26 00:48:07 +0000372 isReMaterializable = R->getValueAsBit("isReMaterializable");
Chris Lattner5b71d3a2004-09-28 18:38:01 +0000373 hasDelaySlot = R->getValueAsBit("hasDelaySlot");
Chris Lattnere3cbf822005-08-26 20:55:40 +0000374 usesCustomDAGSchedInserter = R->getValueAsBit("usesCustomDAGSchedInserter");
Evan Cheng1c3d19e2005-12-04 08:18:16 +0000375 hasCtrlDep = R->getValueAsBit("hasCtrlDep");
Evan Chengeaa91b02007-06-19 01:26:51 +0000376 isNotDuplicable = R->getValueAsBit("isNotDuplicable");
Evan Cheng88cc0922007-07-10 18:05:01 +0000377 hasOptionalDef = false;
Chris Lattnercfbf96a2005-08-18 23:38:41 +0000378 hasVariableNumberOfOperands = false;
379
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000380 DagInit *DI;
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000381 try {
Evan Cheng64d80e32007-07-19 01:14:50 +0000382 DI = R->getValueAsDag("OutOperandList");
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000383 } catch (...) {
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000384 // Error getting operand list, just ignore it (sparcv9).
Chris Lattner87c59052004-08-01 07:42:39 +0000385 AsmString.clear();
386 OperandList.clear();
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000387 return;
388 }
Evan Cheng64d80e32007-07-19 01:14:50 +0000389 NumDefs = DI->getNumArgs();
390
391 DagInit *IDI;
392 try {
393 IDI = R->getValueAsDag("InOperandList");
394 } catch (...) {
395 // Error getting operand list, just ignore it (sparcv9).
396 AsmString.clear();
397 OperandList.clear();
398 return;
399 }
400 DI = (DagInit*)(new BinOpInit(BinOpInit::CONCAT, DI, IDI))->Fold();
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000401
402 unsigned MIOperandNo = 0;
403 std::set<std::string> OperandNames;
404 for (unsigned i = 0, e = DI->getNumArgs(); i != e; ++i) {
405 DefInit *Arg = dynamic_cast<DefInit*>(DI->getArg(i));
406 if (!Arg)
407 throw "Illegal operand for the '" + R->getName() + "' instruction!";
408
409 Record *Rec = Arg->getDef();
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000410 std::string PrintMethod = "printOperand";
411 unsigned NumOps = 1;
Chris Lattner33670792005-11-19 07:48:33 +0000412 DagInit *MIOpInfo = 0;
Nate Begeman86193d12005-12-01 00:12:04 +0000413 if (Rec->isSubClassOf("Operand")) {
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000414 PrintMethod = Rec->getValueAsString("PrintMethod");
Chris Lattner65303d62005-11-19 07:05:57 +0000415 MIOpInfo = Rec->getValueAsDag("MIOperandInfo");
Chris Lattnerd438b532006-11-03 23:45:17 +0000416
417 // Verify that MIOpInfo has an 'ops' root value.
418 if (!dynamic_cast<DefInit*>(MIOpInfo->getOperator()) ||
419 dynamic_cast<DefInit*>(MIOpInfo->getOperator())
420 ->getDef()->getName() != "ops")
421 throw "Bad value for MIOperandInfo in operand '" + Rec->getName() +
422 "'\n";
423
424 // If we have MIOpInfo, then we have #operands equal to number of entries
425 // in MIOperandInfo.
426 if (unsigned NumArgs = MIOpInfo->getNumArgs())
427 NumOps = NumArgs;
428
Evan Cheng88cc0922007-07-10 18:05:01 +0000429 if (Rec->isSubClassOf("PredicateOperand"))
Evan Chengc419bd32007-07-06 23:23:38 +0000430 isPredicable = true;
Evan Cheng88cc0922007-07-10 18:05:01 +0000431 else if (Rec->isSubClassOf("OptionalDefOperand"))
432 hasOptionalDef = true;
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000433 } else if (Rec->getName() == "variable_ops") {
434 hasVariableNumberOfOperands = true;
435 continue;
Chris Lattnerf1968392006-11-10 02:01:40 +0000436 } else if (!Rec->isSubClassOf("RegisterClass") &&
437 Rec->getName() != "ptr_rc")
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000438 throw "Unknown operand class '" + Rec->getName() +
439 "' in instruction '" + R->getName() + "' instruction!";
440
Chris Lattnerc4a8b732005-09-14 21:13:50 +0000441 // Check that the operand has a name and that it's unique.
442 if (DI->getArgName(i).empty())
443 throw "In instruction '" + R->getName() + "', operand #" + utostr(i) +
444 " has no name!";
445 if (!OperandNames.insert(DI->getArgName(i)).second)
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000446 throw "In instruction '" + R->getName() + "', operand #" + utostr(i) +
447 " has the same name as a previous operand!";
448
Nate Begeman86193d12005-12-01 00:12:04 +0000449 OperandList.push_back(OperandInfo(Rec, DI->getArgName(i), PrintMethod,
450 MIOperandNo, NumOps, MIOpInfo));
Chris Lattner5d7d3db2005-09-14 21:05:02 +0000451 MIOperandNo += NumOps;
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000452 }
Evan Chenge2ba8972006-11-01 00:27:05 +0000453
Chris Lattnerf64f9a42006-11-15 23:23:02 +0000454 // Parse Constraints.
Chris Lattnera0cca4a2006-11-06 23:49:51 +0000455 ParseConstraints(R->getValueAsString("Constraints"), this);
456
457 // For backward compatibility: isTwoAddress means operand 1 is tied to
458 // operand 0.
Chris Lattner0bb75002006-11-15 02:38:17 +0000459 if (isTwoAddress) {
460 if (!OperandList[1].Constraints[0].empty())
461 throw R->getName() + ": cannot use isTwoAddress property: instruction "
462 "already has constraint set!";
Evan Cheng05551222006-12-01 22:57:41 +0000463 OperandList[1].Constraints[0] = "((0 << 16) | (1 << TOI::TIED_TO))";
Chris Lattner0bb75002006-11-15 02:38:17 +0000464 }
Chris Lattnera0cca4a2006-11-06 23:49:51 +0000465
466 // Any operands with unset constraints get 0 as their constraint.
467 for (unsigned op = 0, e = OperandList.size(); op != e; ++op)
Chris Lattner0bb75002006-11-15 02:38:17 +0000468 for (unsigned j = 0, e = OperandList[op].MINumOperands; j != e; ++j)
469 if (OperandList[op].Constraints[j].empty())
470 OperandList[op].Constraints[j] = "0";
Chris Lattnerf64f9a42006-11-15 23:23:02 +0000471
472 // Parse the DisableEncoding field.
473 std::string DisableEncoding = R->getValueAsString("DisableEncoding");
474 while (1) {
475 std::string OpName = getToken(DisableEncoding, " ,\t");
476 if (OpName.empty()) break;
477
478 // Figure out which operand this is.
479 std::pair<unsigned,unsigned> Op = ParseOperandName(OpName, false);
480
481 // Mark the operand as not-to-be encoded.
482 if (Op.second >= OperandList[Op.first].DoNotEncode.size())
483 OperandList[Op.first].DoNotEncode.resize(Op.second+1);
484 OperandList[Op.first].DoNotEncode[Op.second] = true;
485 }
Chris Lattnerec352402004-08-01 05:04:00 +0000486}
487
488
Chris Lattner87c59052004-08-01 07:42:39 +0000489
490/// getOperandNamed - Return the index of the operand with the specified
491/// non-empty name. If the instruction does not have an operand with the
492/// specified name, throw an exception.
Misha Brukman35e83cc2004-10-14 05:50:43 +0000493///
Chris Lattner87c59052004-08-01 07:42:39 +0000494unsigned CodeGenInstruction::getOperandNamed(const std::string &Name) const {
495 assert(!Name.empty() && "Cannot search for operand with no name!");
496 for (unsigned i = 0, e = OperandList.size(); i != e; ++i)
497 if (OperandList[i].Name == Name) return i;
498 throw "Instruction '" + TheDef->getName() +
499 "' does not have an operand named '$" + Name + "'!";
500}
Evan Cheng0fc71982005-12-08 02:00:36 +0000501
Chris Lattner0bb75002006-11-15 02:38:17 +0000502std::pair<unsigned,unsigned>
503CodeGenInstruction::ParseOperandName(const std::string &Op,
504 bool AllowWholeOp) {
505 if (Op.empty() || Op[0] != '$')
506 throw TheDef->getName() + ": Illegal operand name: '" + Op + "'";
507
508 std::string OpName = Op.substr(1);
509 std::string SubOpName;
510
511 // Check to see if this is $foo.bar.
512 std::string::size_type DotIdx = OpName.find_first_of(".");
513 if (DotIdx != std::string::npos) {
514 SubOpName = OpName.substr(DotIdx+1);
515 if (SubOpName.empty())
516 throw TheDef->getName() + ": illegal empty suboperand name in '" +Op +"'";
517 OpName = OpName.substr(0, DotIdx);
518 }
519
520 unsigned OpIdx = getOperandNamed(OpName);
521
522 if (SubOpName.empty()) { // If no suboperand name was specified:
523 // If one was needed, throw.
524 if (OperandList[OpIdx].MINumOperands > 1 && !AllowWholeOp &&
525 SubOpName.empty())
526 throw TheDef->getName() + ": Illegal to refer to"
527 " whole operand part of complex operand '" + Op + "'";
528
529 // Otherwise, return the operand.
530 return std::make_pair(OpIdx, 0U);
531 }
532
533 // Find the suboperand number involved.
534 DagInit *MIOpInfo = OperandList[OpIdx].MIOperandInfo;
535 if (MIOpInfo == 0)
536 throw TheDef->getName() + ": unknown suboperand name in '" + Op + "'";
537
538 // Find the operand with the right name.
539 for (unsigned i = 0, e = MIOpInfo->getNumArgs(); i != e; ++i)
540 if (MIOpInfo->getArgName(i) == SubOpName)
541 return std::make_pair(OpIdx, i);
542
543 // Otherwise, didn't find it!
544 throw TheDef->getName() + ": unknown suboperand name in '" + Op + "'";
545}
546
547
548
549
Evan Cheng0fc71982005-12-08 02:00:36 +0000550//===----------------------------------------------------------------------===//
551// ComplexPattern implementation
552//
553ComplexPattern::ComplexPattern(Record *R) {
Evan Cheng3aa39f42005-12-08 02:14:08 +0000554 Ty = ::getValueType(R->getValueAsDef("Ty"));
555 NumOperands = R->getValueAsInt("NumOperands");
556 SelectFunc = R->getValueAsString("SelectFunc");
557 RootNodes = R->getValueAsListOfDefs("RootNodes");
Evan Cheng94b30402006-10-11 21:02:01 +0000558
559 // Parse the properties.
560 Properties = 0;
561 std::vector<Record*> PropList = R->getValueAsListOfDefs("Properties");
562 for (unsigned i = 0, e = PropList.size(); i != e; ++i)
563 if (PropList[i]->getName() == "SDNPHasChain") {
564 Properties |= 1 << SDNPHasChain;
565 } else if (PropList[i]->getName() == "SDNPOptInFlag") {
566 Properties |= 1 << SDNPOptInFlag;
567 } else {
Bill Wendlingf5da1332006-12-07 22:21:48 +0000568 cerr << "Unsupported SD Node property '" << PropList[i]->getName()
569 << "' on ComplexPattern '" << R->getName() << "'!\n";
Evan Cheng94b30402006-10-11 21:02:01 +0000570 exit(1);
571 }
Evan Cheng0fc71982005-12-08 02:00:36 +0000572}
Evan Cheng3aa39f42005-12-08 02:14:08 +0000573
Chris Lattner43fbbc32006-03-24 19:49:31 +0000574//===----------------------------------------------------------------------===//
575// CodeGenIntrinsic Implementation
576//===----------------------------------------------------------------------===//
577
578std::vector<CodeGenIntrinsic> llvm::LoadIntrinsics(const RecordKeeper &RC) {
579 std::vector<Record*> I = RC.getAllDerivedDefinitions("Intrinsic");
Chris Lattner8850a1b2006-03-27 22:48:18 +0000580
581 std::vector<CodeGenIntrinsic> Result;
Chris Lattner6a160fb2006-03-28 00:15:00 +0000582
583 // If we are in the context of a target .td file, get the target info so that
584 // we can decode the current intptr_t.
585 CodeGenTarget *CGT = 0;
586 if (Records.getClass("Target") &&
587 Records.getAllDerivedDefinitions("Target").size() == 1)
588 CGT = new CodeGenTarget();
589
Chris Lattner8850a1b2006-03-27 22:48:18 +0000590 for (unsigned i = 0, e = I.size(); i != e; ++i)
Chris Lattner6a160fb2006-03-28 00:15:00 +0000591 Result.push_back(CodeGenIntrinsic(I[i], CGT));
592 delete CGT;
Chris Lattner8850a1b2006-03-27 22:48:18 +0000593 return Result;
Chris Lattner43fbbc32006-03-24 19:49:31 +0000594}
595
Chris Lattner76f8c7c2006-03-28 00:03:08 +0000596CodeGenIntrinsic::CodeGenIntrinsic(Record *R, CodeGenTarget *CGT) {
Chris Lattner2ca956f2006-03-24 20:25:01 +0000597 TheDef = R;
Chris Lattner43fbbc32006-03-24 19:49:31 +0000598 std::string DefName = R->getName();
599 ModRef = WriteMem;
Reid Spencerc4de3de2007-04-01 07:20:02 +0000600 isOverloaded = false;
Chris Lattner43fbbc32006-03-24 19:49:31 +0000601
602 if (DefName.size() <= 4 ||
603 std::string(DefName.begin(), DefName.begin()+4) != "int_")
604 throw "Intrinsic '" + DefName + "' does not start with 'int_'!";
605 EnumName = std::string(DefName.begin()+4, DefName.end());
606 if (R->getValue("GCCBuiltinName")) // Ignore a missing GCCBuiltinName field.
607 GCCBuiltinName = R->getValueAsString("GCCBuiltinName");
608 TargetPrefix = R->getValueAsString("TargetPrefix");
609 Name = R->getValueAsString("LLVMName");
610 if (Name == "") {
611 // If an explicit name isn't specified, derive one from the DefName.
612 Name = "llvm.";
613 for (unsigned i = 0, e = EnumName.size(); i != e; ++i)
614 if (EnumName[i] == '_')
615 Name += '.';
616 else
617 Name += EnumName[i];
618 } else {
619 // Verify it starts with "llvm.".
620 if (Name.size() <= 5 ||
621 std::string(Name.begin(), Name.begin()+5) != "llvm.")
622 throw "Intrinsic '" + DefName + "'s name does not start with 'llvm.'!";
623 }
624
625 // If TargetPrefix is specified, make sure that Name starts with
626 // "llvm.<targetprefix>.".
627 if (!TargetPrefix.empty()) {
628 if (Name.size() < 6+TargetPrefix.size() ||
629 std::string(Name.begin()+5, Name.begin()+6+TargetPrefix.size())
630 != (TargetPrefix+"."))
631 throw "Intrinsic '" + DefName + "' does not start with 'llvm." +
632 TargetPrefix + ".'!";
633 }
634
635 // Parse the list of argument types.
636 ListInit *TypeList = R->getValueAsListInit("Types");
637 for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) {
Chris Lattner50d45652007-02-27 22:08:27 +0000638 Record *TyEl = TypeList->getElementAsRecord(i);
Chris Lattner43fbbc32006-03-24 19:49:31 +0000639 assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!");
640 ArgTypes.push_back(TyEl->getValueAsString("TypeVal"));
Dan Gohman3bf6e182007-07-13 20:16:50 +0000641 MVT::ValueType VT = getValueType(TyEl->getValueAsDef("VT"));
Reid Spencerc4de3de2007-04-01 07:20:02 +0000642 isOverloaded |= VT == MVT::iAny;
643 ArgVTs.push_back(VT);
Chris Lattner43fbbc32006-03-24 19:49:31 +0000644 ArgTypeDefs.push_back(TyEl);
645 }
646 if (ArgTypes.size() == 0)
647 throw "Intrinsic '"+DefName+"' needs at least a type for the ret value!";
Reid Spencerc4de3de2007-04-01 07:20:02 +0000648
Chris Lattner43fbbc32006-03-24 19:49:31 +0000649
650 // Parse the intrinsic properties.
651 ListInit *PropList = R->getValueAsListInit("Properties");
652 for (unsigned i = 0, e = PropList->getSize(); i != e; ++i) {
Chris Lattner50d45652007-02-27 22:08:27 +0000653 Record *Property = PropList->getElementAsRecord(i);
Chris Lattner43fbbc32006-03-24 19:49:31 +0000654 assert(Property->isSubClassOf("IntrinsicProperty") &&
655 "Expected a property!");
656
Chris Lattner4b2362e2006-04-10 22:02:59 +0000657 if (Property->getName() == "IntrNoMem")
Chris Lattner43fbbc32006-03-24 19:49:31 +0000658 ModRef = NoMem;
Chris Lattner4b2362e2006-04-10 22:02:59 +0000659 else if (Property->getName() == "IntrReadArgMem")
Chris Lattner43fbbc32006-03-24 19:49:31 +0000660 ModRef = ReadArgMem;
661 else if (Property->getName() == "IntrReadMem")
662 ModRef = ReadMem;
Chris Lattner4b2362e2006-04-10 22:02:59 +0000663 else if (Property->getName() == "IntrWriteArgMem")
Chris Lattner43fbbc32006-03-24 19:49:31 +0000664 ModRef = WriteArgMem;
665 else if (Property->getName() == "IntrWriteMem")
666 ModRef = WriteMem;
667 else
668 assert(0 && "Unknown property!");
669 }
670}