Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 1 | //===- CodeGenTarget.cpp - CodeGen Target Class Wrapper -------------------===// |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 01d4582 | 2003-10-20 20:20:30 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 3060910 | 2007-12-29 20:37:13 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 01d4582 | 2003-10-20 20:20:30 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 4587207 | 2003-08-07 05:38:11 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 10 | // This class wraps target description classes used by the various code |
Chris Lattner | 4587207 | 2003-08-07 05:38:11 +0000 | [diff] [blame] | 11 | // 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 Lattner | 803a5f6 | 2004-08-01 04:04:35 +0000 | [diff] [blame] | 17 | #include "CodeGenTarget.h" |
Chris Lattner | 43fbbc3 | 2006-03-24 19:49:31 +0000 | [diff] [blame] | 18 | #include "CodeGenIntrinsics.h" |
Chris Lattner | 4587207 | 2003-08-07 05:38:11 +0000 | [diff] [blame] | 19 | #include "Record.h" |
Chris Lattner | 560a79f | 2004-10-03 19:34:31 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/StringExtras.h" |
| 21 | #include "llvm/Support/CommandLine.h" |
Bill Wendling | f5da133 | 2006-12-07 22:21:48 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Streams.h" |
Chris Lattner | 75ee2eb | 2005-10-14 03:54:49 +0000 | [diff] [blame] | 23 | #include <algorithm> |
Chris Lattner | 2082ebe | 2004-08-01 03:55:39 +0000 | [diff] [blame] | 24 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 25 | |
Chris Lattner | 560a79f | 2004-10-03 19:34:31 +0000 | [diff] [blame] | 26 | static cl::opt<unsigned> |
| 27 | AsmWriterNum("asmwriternum", cl::init(0), |
| 28 | cl::desc("Make -gen-asm-writer emit assembly writer #N")); |
| 29 | |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame^] | 30 | /// getValueType - Return the MVT::SimpleValueType that the specified TableGen |
| 31 | /// record corresponds to. |
| 32 | MVT::SimpleValueType llvm::getValueType(Record *Rec) { |
| 33 | return (MVT::SimpleValueType)Rec->getValueAsInt("Value"); |
Chris Lattner | 4587207 | 2003-08-07 05:38:11 +0000 | [diff] [blame] | 34 | } |
| 35 | |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame^] | 36 | std::string llvm::getName(MVT::SimpleValueType T) { |
Chris Lattner | 4587207 | 2003-08-07 05:38:11 +0000 | [diff] [blame] | 37 | switch (T) { |
Chris Lattner | d3464c1 | 2003-08-07 23:15:21 +0000 | [diff] [blame] | 38 | case MVT::Other: return "UNKNOWN"; |
Evan Cheng | d7c2c86 | 2006-06-15 00:16:37 +0000 | [diff] [blame] | 39 | case MVT::i1: return "MVT::i1"; |
| 40 | case MVT::i8: return "MVT::i8"; |
| 41 | case MVT::i16: return "MVT::i16"; |
| 42 | case MVT::i32: return "MVT::i32"; |
| 43 | case MVT::i64: return "MVT::i64"; |
| 44 | case MVT::i128: return "MVT::i128"; |
Reid Spencer | c4de3de | 2007-04-01 07:20:02 +0000 | [diff] [blame] | 45 | case MVT::iAny: return "MVT::iAny"; |
Dan Gohman | 0fee3ff | 2007-08-16 21:57:19 +0000 | [diff] [blame] | 46 | case MVT::fAny: return "MVT::fAny"; |
Evan Cheng | d7c2c86 | 2006-06-15 00:16:37 +0000 | [diff] [blame] | 47 | 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"; |
Dale Johannesen | 317096a | 2007-09-28 01:08:20 +0000 | [diff] [blame] | 51 | case MVT::ppcf128: return "MVT::ppcf128"; |
Evan Cheng | d7c2c86 | 2006-06-15 00:16:37 +0000 | [diff] [blame] | 52 | case MVT::Flag: return "MVT::Flag"; |
| 53 | case MVT::isVoid:return "MVT::void"; |
| 54 | case MVT::v8i8: return "MVT::v8i8"; |
| 55 | case MVT::v4i16: return "MVT::v4i16"; |
| 56 | case MVT::v2i32: return "MVT::v2i32"; |
Bill Wendling | eebc8a1 | 2007-03-26 07:53:08 +0000 | [diff] [blame] | 57 | case MVT::v1i64: return "MVT::v1i64"; |
Evan Cheng | d7c2c86 | 2006-06-15 00:16:37 +0000 | [diff] [blame] | 58 | case MVT::v16i8: return "MVT::v16i8"; |
| 59 | case MVT::v8i16: return "MVT::v8i16"; |
| 60 | case MVT::v4i32: return "MVT::v4i32"; |
| 61 | case MVT::v2i64: return "MVT::v2i64"; |
| 62 | case MVT::v2f32: return "MVT::v2f32"; |
| 63 | case MVT::v4f32: return "MVT::v4f32"; |
| 64 | case MVT::v2f64: return "MVT::v2f64"; |
Christopher Lamb | 8245510 | 2007-07-26 06:41:18 +0000 | [diff] [blame] | 65 | case MVT::v3i32: return "MVT::v3i32"; |
| 66 | case MVT::v3f32: return "MVT::v3f32"; |
Evan Cheng | 6b12516 | 2006-05-17 20:55:51 +0000 | [diff] [blame] | 67 | case MVT::iPTR: return "TLI.getPointerTy()"; |
Chris Lattner | d3464c1 | 2003-08-07 23:15:21 +0000 | [diff] [blame] | 68 | default: assert(0 && "ILLEGAL VALUE TYPE!"); return ""; |
Chris Lattner | 4587207 | 2003-08-07 05:38:11 +0000 | [diff] [blame] | 69 | } |
Chris Lattner | d3464c1 | 2003-08-07 23:15:21 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame^] | 72 | std::string llvm::getEnumName(MVT::SimpleValueType T) { |
Chris Lattner | b72fb7e | 2003-08-10 19:50:32 +0000 | [diff] [blame] | 73 | switch (T) { |
Evan Cheng | 2618d07 | 2006-05-17 20:37:59 +0000 | [diff] [blame] | 74 | case MVT::Other: return "MVT::Other"; |
| 75 | case MVT::i1: return "MVT::i1"; |
| 76 | case MVT::i8: return "MVT::i8"; |
| 77 | case MVT::i16: return "MVT::i16"; |
| 78 | case MVT::i32: return "MVT::i32"; |
| 79 | case MVT::i64: return "MVT::i64"; |
| 80 | case MVT::i128: return "MVT::i128"; |
Reid Spencer | c4de3de | 2007-04-01 07:20:02 +0000 | [diff] [blame] | 81 | case MVT::iAny: return "MVT::iAny"; |
Dan Gohman | 0fee3ff | 2007-08-16 21:57:19 +0000 | [diff] [blame] | 82 | case MVT::fAny: return "MVT::fAny"; |
Evan Cheng | 2618d07 | 2006-05-17 20:37:59 +0000 | [diff] [blame] | 83 | case MVT::f32: return "MVT::f32"; |
| 84 | case MVT::f64: return "MVT::f64"; |
| 85 | case MVT::f80: return "MVT::f80"; |
| 86 | case MVT::f128: return "MVT::f128"; |
Dale Johannesen | 317096a | 2007-09-28 01:08:20 +0000 | [diff] [blame] | 87 | case MVT::ppcf128: return "MVT::ppcf128"; |
Evan Cheng | 2618d07 | 2006-05-17 20:37:59 +0000 | [diff] [blame] | 88 | case MVT::Flag: return "MVT::Flag"; |
| 89 | case MVT::isVoid:return "MVT::isVoid"; |
| 90 | case MVT::v8i8: return "MVT::v8i8"; |
| 91 | case MVT::v4i16: return "MVT::v4i16"; |
| 92 | case MVT::v2i32: return "MVT::v2i32"; |
Bill Wendling | eebc8a1 | 2007-03-26 07:53:08 +0000 | [diff] [blame] | 93 | case MVT::v1i64: return "MVT::v1i64"; |
Evan Cheng | 2618d07 | 2006-05-17 20:37:59 +0000 | [diff] [blame] | 94 | case MVT::v16i8: return "MVT::v16i8"; |
| 95 | case MVT::v8i16: return "MVT::v8i16"; |
| 96 | case MVT::v4i32: return "MVT::v4i32"; |
| 97 | case MVT::v2i64: return "MVT::v2i64"; |
| 98 | case MVT::v2f32: return "MVT::v2f32"; |
| 99 | case MVT::v4f32: return "MVT::v4f32"; |
| 100 | case MVT::v2f64: return "MVT::v2f64"; |
Christopher Lamb | 8245510 | 2007-07-26 06:41:18 +0000 | [diff] [blame] | 101 | case MVT::v3i32: return "MVT::v3i32"; |
| 102 | case MVT::v3f32: return "MVT::v3f32"; |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 103 | case MVT::iPTR: return "MVT::iPTR"; |
Chris Lattner | b72fb7e | 2003-08-10 19:50:32 +0000 | [diff] [blame] | 104 | default: assert(0 && "ILLEGAL VALUE TYPE!"); return ""; |
| 105 | } |
| 106 | } |
| 107 | |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 108 | /// getQualifiedName - Return the name of the specified record, with a |
| 109 | /// namespace qualifier if the record contains one. |
| 110 | /// |
| 111 | std::string llvm::getQualifiedName(const Record *R) { |
| 112 | std::string Namespace = R->getValueAsString("Namespace"); |
| 113 | if (Namespace.empty()) return R->getName(); |
| 114 | return Namespace + "::" + R->getName(); |
| 115 | } |
| 116 | |
| 117 | |
| 118 | |
Chris Lattner | d3464c1 | 2003-08-07 23:15:21 +0000 | [diff] [blame] | 119 | |
Chris Lattner | 4587207 | 2003-08-07 05:38:11 +0000 | [diff] [blame] | 120 | /// getTarget - Return the current instance of the Target class. |
| 121 | /// |
Evan Cheng | 2618d07 | 2006-05-17 20:37:59 +0000 | [diff] [blame] | 122 | CodeGenTarget::CodeGenTarget() { |
Chris Lattner | 4587207 | 2003-08-07 05:38:11 +0000 | [diff] [blame] | 123 | std::vector<Record*> Targets = Records.getAllDerivedDefinitions("Target"); |
Misha Brukman | bebdb20 | 2004-06-04 14:59:42 +0000 | [diff] [blame] | 124 | if (Targets.size() == 0) |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 125 | throw std::string("ERROR: No 'Target' subclasses defined!"); |
Chris Lattner | 4587207 | 2003-08-07 05:38:11 +0000 | [diff] [blame] | 126 | if (Targets.size() != 1) |
| 127 | throw std::string("ERROR: Multiple subclasses of Target defined!"); |
| 128 | TargetRec = Targets[0]; |
Chris Lattner | 4587207 | 2003-08-07 05:38:11 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | |
| 132 | const std::string &CodeGenTarget::getName() const { |
| 133 | return TargetRec->getName(); |
| 134 | } |
| 135 | |
| 136 | Record *CodeGenTarget::getInstructionSet() const { |
| 137 | return TargetRec->getValueAsDef("InstructionSet"); |
| 138 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 139 | |
Chris Lattner | 175580c | 2004-08-14 22:50:53 +0000 | [diff] [blame] | 140 | /// getAsmWriter - Return the AssemblyWriter definition for this target. |
| 141 | /// |
| 142 | Record *CodeGenTarget::getAsmWriter() const { |
Chris Lattner | b0e103d | 2005-10-28 22:49:02 +0000 | [diff] [blame] | 143 | std::vector<Record*> LI = TargetRec->getValueAsListOfDefs("AssemblyWriters"); |
| 144 | if (AsmWriterNum >= LI.size()) |
Chris Lattner | 560a79f | 2004-10-03 19:34:31 +0000 | [diff] [blame] | 145 | throw "Target does not have an AsmWriter #" + utostr(AsmWriterNum) + "!"; |
Chris Lattner | b0e103d | 2005-10-28 22:49:02 +0000 | [diff] [blame] | 146 | return LI[AsmWriterNum]; |
Chris Lattner | 175580c | 2004-08-14 22:50:53 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Chris Lattner | 2669311 | 2004-08-16 01:10:21 +0000 | [diff] [blame] | 149 | void CodeGenTarget::ReadRegisters() const { |
| 150 | std::vector<Record*> Regs = Records.getAllDerivedDefinitions("Register"); |
| 151 | if (Regs.empty()) |
| 152 | throw std::string("No 'Register' subclasses defined!"); |
| 153 | |
| 154 | Registers.reserve(Regs.size()); |
| 155 | Registers.assign(Regs.begin(), Regs.end()); |
| 156 | } |
| 157 | |
Chris Lattner | 7a680c6 | 2004-08-21 02:24:57 +0000 | [diff] [blame] | 158 | CodeGenRegister::CodeGenRegister(Record *R) : TheDef(R) { |
| 159 | DeclaredSpillSize = R->getValueAsInt("SpillSize"); |
| 160 | DeclaredSpillAlignment = R->getValueAsInt("SpillAlignment"); |
| 161 | } |
| 162 | |
Chris Lattner | 2669311 | 2004-08-16 01:10:21 +0000 | [diff] [blame] | 163 | const std::string &CodeGenRegister::getName() const { |
| 164 | return TheDef->getName(); |
| 165 | } |
| 166 | |
Chris Lattner | 056afef | 2004-08-21 04:05:00 +0000 | [diff] [blame] | 167 | void CodeGenTarget::ReadRegisterClasses() const { |
| 168 | std::vector<Record*> RegClasses = |
| 169 | Records.getAllDerivedDefinitions("RegisterClass"); |
| 170 | if (RegClasses.empty()) |
| 171 | throw std::string("No 'RegisterClass' subclasses defined!"); |
| 172 | |
| 173 | RegisterClasses.reserve(RegClasses.size()); |
| 174 | RegisterClasses.assign(RegClasses.begin(), RegClasses.end()); |
| 175 | } |
| 176 | |
Evan Cheng | 44a65fa | 2006-05-16 07:05:30 +0000 | [diff] [blame] | 177 | std::vector<unsigned char> CodeGenTarget::getRegisterVTs(Record *R) const { |
| 178 | std::vector<unsigned char> Result; |
| 179 | const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses(); |
| 180 | for (unsigned i = 0, e = RCs.size(); i != e; ++i) { |
| 181 | const CodeGenRegisterClass &RC = RegisterClasses[i]; |
| 182 | for (unsigned ei = 0, ee = RC.Elements.size(); ei != ee; ++ei) { |
| 183 | if (R == RC.Elements[ei]) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame^] | 184 | const std::vector<MVT::SimpleValueType> &InVTs = RC.getValueTypes(); |
Evan Cheng | 44a65fa | 2006-05-16 07:05:30 +0000 | [diff] [blame] | 185 | for (unsigned i = 0, e = InVTs.size(); i != e; ++i) |
| 186 | Result.push_back(InVTs[i]); |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | return Result; |
| 191 | } |
| 192 | |
| 193 | |
Chris Lattner | 056afef | 2004-08-21 04:05:00 +0000 | [diff] [blame] | 194 | CodeGenRegisterClass::CodeGenRegisterClass(Record *R) : TheDef(R) { |
Chris Lattner | c67c18f | 2005-08-19 18:45:20 +0000 | [diff] [blame] | 195 | // Rename anonymous register classes. |
| 196 | if (R->getName().size() > 9 && R->getName()[9] == '.') { |
| 197 | static unsigned AnonCounter = 0; |
| 198 | R->setName("AnonRegClass_"+utostr(AnonCounter++)); |
| 199 | } |
| 200 | |
Nate Begeman | 6510b22 | 2005-12-01 04:51:06 +0000 | [diff] [blame] | 201 | std::vector<Record*> TypeList = R->getValueAsListOfDefs("RegTypes"); |
| 202 | for (unsigned i = 0, e = TypeList.size(); i != e; ++i) { |
| 203 | Record *Type = TypeList[i]; |
| 204 | if (!Type->isSubClassOf("ValueType")) |
| 205 | throw "RegTypes list member '" + Type->getName() + |
| 206 | "' does not derive from the ValueType class!"; |
| 207 | VTs.push_back(getValueType(Type)); |
| 208 | } |
| 209 | assert(!VTs.empty() && "RegisterClass must contain at least one ValueType!"); |
Chris Lattner | ac46893 | 2005-08-19 19:12:51 +0000 | [diff] [blame] | 210 | |
Chris Lattner | b0e103d | 2005-10-28 22:49:02 +0000 | [diff] [blame] | 211 | std::vector<Record*> RegList = R->getValueAsListOfDefs("MemberList"); |
| 212 | for (unsigned i = 0, e = RegList.size(); i != e; ++i) { |
| 213 | Record *Reg = RegList[i]; |
Chris Lattner | 056afef | 2004-08-21 04:05:00 +0000 | [diff] [blame] | 214 | if (!Reg->isSubClassOf("Register")) |
| 215 | throw "Register Class member '" + Reg->getName() + |
| 216 | "' does not derive from the Register class!"; |
| 217 | Elements.push_back(Reg); |
| 218 | } |
Nate Begeman | 6510b22 | 2005-12-01 04:51:06 +0000 | [diff] [blame] | 219 | |
Christopher Lamb | a321125 | 2007-06-13 22:20:15 +0000 | [diff] [blame] | 220 | std::vector<Record*> SubRegClassList = |
| 221 | R->getValueAsListOfDefs("SubRegClassList"); |
| 222 | for (unsigned i = 0, e = SubRegClassList.size(); i != e; ++i) { |
| 223 | Record *SubRegClass = SubRegClassList[i]; |
| 224 | if (!SubRegClass->isSubClassOf("RegisterClass")) |
| 225 | throw "Register Class member '" + SubRegClass->getName() + |
| 226 | "' does not derive from the RegisterClass class!"; |
| 227 | SubRegClasses.push_back(SubRegClass); |
| 228 | } |
| 229 | |
Nate Begeman | 6510b22 | 2005-12-01 04:51:06 +0000 | [diff] [blame] | 230 | // Allow targets to override the size in bits of the RegisterClass. |
| 231 | unsigned Size = R->getValueAsInt("Size"); |
| 232 | |
| 233 | Namespace = R->getValueAsString("Namespace"); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame^] | 234 | SpillSize = Size ? Size : MVT(VTs[0]).getSizeInBits(); |
Nate Begeman | 6510b22 | 2005-12-01 04:51:06 +0000 | [diff] [blame] | 235 | SpillAlignment = R->getValueAsInt("Alignment"); |
Evan Cheng | a3ca314 | 2007-09-19 01:35:01 +0000 | [diff] [blame] | 236 | CopyCost = R->getValueAsInt("CopyCost"); |
Nate Begeman | 6510b22 | 2005-12-01 04:51:06 +0000 | [diff] [blame] | 237 | MethodBodies = R->getValueAsCode("MethodBodies"); |
| 238 | MethodProtos = R->getValueAsCode("MethodProtos"); |
Chris Lattner | 056afef | 2004-08-21 04:05:00 +0000 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | const std::string &CodeGenRegisterClass::getName() const { |
| 242 | return TheDef->getName(); |
| 243 | } |
| 244 | |
Chris Lattner | e9f4ba8 | 2005-09-08 21:43:21 +0000 | [diff] [blame] | 245 | void CodeGenTarget::ReadLegalValueTypes() const { |
| 246 | const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses(); |
| 247 | for (unsigned i = 0, e = RCs.size(); i != e; ++i) |
Nate Begeman | 6510b22 | 2005-12-01 04:51:06 +0000 | [diff] [blame] | 248 | for (unsigned ri = 0, re = RCs[i].VTs.size(); ri != re; ++ri) |
| 249 | LegalValueTypes.push_back(RCs[i].VTs[ri]); |
Chris Lattner | 75ee2eb | 2005-10-14 03:54:49 +0000 | [diff] [blame] | 250 | |
| 251 | // Remove duplicates. |
| 252 | std::sort(LegalValueTypes.begin(), LegalValueTypes.end()); |
| 253 | LegalValueTypes.erase(std::unique(LegalValueTypes.begin(), |
| 254 | LegalValueTypes.end()), |
| 255 | LegalValueTypes.end()); |
Chris Lattner | e9f4ba8 | 2005-09-08 21:43:21 +0000 | [diff] [blame] | 256 | } |
Chris Lattner | 056afef | 2004-08-21 04:05:00 +0000 | [diff] [blame] | 257 | |
Chris Lattner | 175580c | 2004-08-14 22:50:53 +0000 | [diff] [blame] | 258 | |
Chris Lattner | ec35240 | 2004-08-01 05:04:00 +0000 | [diff] [blame] | 259 | void CodeGenTarget::ReadInstructions() const { |
| 260 | std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction"); |
Chris Lattner | aa77d77 | 2006-01-27 01:45:06 +0000 | [diff] [blame] | 261 | if (Insts.size() <= 2) |
Chris Lattner | ec35240 | 2004-08-01 05:04:00 +0000 | [diff] [blame] | 262 | throw std::string("No 'Instruction' subclasses defined!"); |
| 263 | |
Chris Lattner | aa77d77 | 2006-01-27 01:45:06 +0000 | [diff] [blame] | 264 | // Parse the instructions defined in the .td file. |
Chris Lattner | 175580c | 2004-08-14 22:50:53 +0000 | [diff] [blame] | 265 | std::string InstFormatName = |
| 266 | getAsmWriter()->getValueAsString("InstFormatName"); |
| 267 | |
| 268 | for (unsigned i = 0, e = Insts.size(); i != e; ++i) { |
| 269 | std::string AsmStr = Insts[i]->getValueAsString(InstFormatName); |
| 270 | Instructions.insert(std::make_pair(Insts[i]->getName(), |
| 271 | CodeGenInstruction(Insts[i], AsmStr))); |
| 272 | } |
Chris Lattner | ec35240 | 2004-08-01 05:04:00 +0000 | [diff] [blame] | 273 | } |
| 274 | |
Chris Lattner | d648867 | 2005-01-22 18:58:51 +0000 | [diff] [blame] | 275 | /// getInstructionsByEnumValue - Return all of the instructions defined by the |
| 276 | /// target, ordered by their enum value. |
| 277 | void CodeGenTarget:: |
| 278 | getInstructionsByEnumValue(std::vector<const CodeGenInstruction*> |
| 279 | &NumberedInstructions) { |
Chris Lattner | aa77d77 | 2006-01-27 01:45:06 +0000 | [diff] [blame] | 280 | std::map<std::string, CodeGenInstruction>::const_iterator I; |
| 281 | I = getInstructions().find("PHI"); |
| 282 | if (I == Instructions.end()) throw "Could not find 'PHI' instruction!"; |
| 283 | const CodeGenInstruction *PHI = &I->second; |
| 284 | |
| 285 | I = getInstructions().find("INLINEASM"); |
| 286 | if (I == Instructions.end()) throw "Could not find 'INLINEASM' instruction!"; |
| 287 | const CodeGenInstruction *INLINEASM = &I->second; |
| 288 | |
Jim Laskey | a683f9b | 2007-01-26 17:29:20 +0000 | [diff] [blame] | 289 | I = getInstructions().find("LABEL"); |
| 290 | if (I == Instructions.end()) throw "Could not find 'LABEL' instruction!"; |
| 291 | const CodeGenInstruction *LABEL = &I->second; |
| 292 | |
Evan Cheng | a844bde | 2008-02-02 04:07:54 +0000 | [diff] [blame] | 293 | I = getInstructions().find("DECLARE"); |
| 294 | if (I == Instructions.end()) throw "Could not find 'DECLARE' instruction!"; |
| 295 | const CodeGenInstruction *DECLARE = &I->second; |
| 296 | |
Christopher Lamb | 08d5207 | 2007-07-26 07:48:21 +0000 | [diff] [blame] | 297 | I = getInstructions().find("EXTRACT_SUBREG"); |
| 298 | if (I == Instructions.end()) |
| 299 | throw "Could not find 'EXTRACT_SUBREG' instruction!"; |
| 300 | const CodeGenInstruction *EXTRACT_SUBREG = &I->second; |
| 301 | |
| 302 | I = getInstructions().find("INSERT_SUBREG"); |
| 303 | if (I == Instructions.end()) |
| 304 | throw "Could not find 'INSERT_SUBREG' instruction!"; |
| 305 | const CodeGenInstruction *INSERT_SUBREG = &I->second; |
| 306 | |
Evan Cheng | da47e6e | 2008-03-15 00:03:38 +0000 | [diff] [blame] | 307 | I = getInstructions().find("IMPLICIT_DEF"); |
| 308 | if (I == Instructions.end()) |
| 309 | throw "Could not find 'IMPLICIT_DEF' instruction!"; |
| 310 | const CodeGenInstruction *IMPLICIT_DEF = &I->second; |
| 311 | |
Christopher Lamb | c929823 | 2008-03-16 03:12:01 +0000 | [diff] [blame] | 312 | I = getInstructions().find("SUBREG_TO_REG"); |
| 313 | if (I == Instructions.end()) |
| 314 | throw "Could not find 'SUBREG_TO_REG' instruction!"; |
| 315 | const CodeGenInstruction *SUBREG_TO_REG = &I->second; |
| 316 | |
Chris Lattner | d648867 | 2005-01-22 18:58:51 +0000 | [diff] [blame] | 317 | // Print out the rest of the instructions now. |
Chris Lattner | d648867 | 2005-01-22 18:58:51 +0000 | [diff] [blame] | 318 | NumberedInstructions.push_back(PHI); |
Chris Lattner | aa77d77 | 2006-01-27 01:45:06 +0000 | [diff] [blame] | 319 | NumberedInstructions.push_back(INLINEASM); |
Jim Laskey | a683f9b | 2007-01-26 17:29:20 +0000 | [diff] [blame] | 320 | NumberedInstructions.push_back(LABEL); |
Evan Cheng | a844bde | 2008-02-02 04:07:54 +0000 | [diff] [blame] | 321 | NumberedInstructions.push_back(DECLARE); |
Christopher Lamb | 08d5207 | 2007-07-26 07:48:21 +0000 | [diff] [blame] | 322 | NumberedInstructions.push_back(EXTRACT_SUBREG); |
| 323 | NumberedInstructions.push_back(INSERT_SUBREG); |
Evan Cheng | da47e6e | 2008-03-15 00:03:38 +0000 | [diff] [blame] | 324 | NumberedInstructions.push_back(IMPLICIT_DEF); |
Christopher Lamb | c929823 | 2008-03-16 03:12:01 +0000 | [diff] [blame] | 325 | NumberedInstructions.push_back(SUBREG_TO_REG); |
Chris Lattner | d648867 | 2005-01-22 18:58:51 +0000 | [diff] [blame] | 326 | for (inst_iterator II = inst_begin(), E = inst_end(); II != E; ++II) |
Jim Laskey | a683f9b | 2007-01-26 17:29:20 +0000 | [diff] [blame] | 327 | if (&II->second != PHI && |
| 328 | &II->second != INLINEASM && |
Christopher Lamb | 08d5207 | 2007-07-26 07:48:21 +0000 | [diff] [blame] | 329 | &II->second != LABEL && |
Evan Cheng | a844bde | 2008-02-02 04:07:54 +0000 | [diff] [blame] | 330 | &II->second != DECLARE && |
Christopher Lamb | 08d5207 | 2007-07-26 07:48:21 +0000 | [diff] [blame] | 331 | &II->second != EXTRACT_SUBREG && |
Evan Cheng | da47e6e | 2008-03-15 00:03:38 +0000 | [diff] [blame] | 332 | &II->second != INSERT_SUBREG && |
Christopher Lamb | c929823 | 2008-03-16 03:12:01 +0000 | [diff] [blame] | 333 | &II->second != IMPLICIT_DEF && |
| 334 | &II->second != SUBREG_TO_REG) |
Chris Lattner | d648867 | 2005-01-22 18:58:51 +0000 | [diff] [blame] | 335 | NumberedInstructions.push_back(&II->second); |
| 336 | } |
| 337 | |
| 338 | |
Misha Brukman | 35e83cc | 2004-10-14 05:50:43 +0000 | [diff] [blame] | 339 | /// isLittleEndianEncoding - Return whether this target encodes its instruction |
| 340 | /// in little-endian format, i.e. bits laid out in the order [0..n] |
| 341 | /// |
| 342 | bool CodeGenTarget::isLittleEndianEncoding() const { |
| 343 | return getInstructionSet()->getValueAsBit("isLittleEndianEncoding"); |
| 344 | } |
| 345 | |
Evan Cheng | 0fc7198 | 2005-12-08 02:00:36 +0000 | [diff] [blame] | 346 | //===----------------------------------------------------------------------===// |
| 347 | // ComplexPattern implementation |
| 348 | // |
| 349 | ComplexPattern::ComplexPattern(Record *R) { |
Evan Cheng | 3aa39f4 | 2005-12-08 02:14:08 +0000 | [diff] [blame] | 350 | Ty = ::getValueType(R->getValueAsDef("Ty")); |
| 351 | NumOperands = R->getValueAsInt("NumOperands"); |
| 352 | SelectFunc = R->getValueAsString("SelectFunc"); |
| 353 | RootNodes = R->getValueAsListOfDefs("RootNodes"); |
Evan Cheng | 94b3040 | 2006-10-11 21:02:01 +0000 | [diff] [blame] | 354 | |
| 355 | // Parse the properties. |
| 356 | Properties = 0; |
| 357 | std::vector<Record*> PropList = R->getValueAsListOfDefs("Properties"); |
| 358 | for (unsigned i = 0, e = PropList.size(); i != e; ++i) |
| 359 | if (PropList[i]->getName() == "SDNPHasChain") { |
| 360 | Properties |= 1 << SDNPHasChain; |
| 361 | } else if (PropList[i]->getName() == "SDNPOptInFlag") { |
| 362 | Properties |= 1 << SDNPOptInFlag; |
Chris Lattner | ba7e756 | 2008-01-10 07:59:24 +0000 | [diff] [blame] | 363 | } else if (PropList[i]->getName() == "SDNPMayStore") { |
| 364 | Properties |= 1 << SDNPMayStore; |
| 365 | } else if (PropList[i]->getName() == "SDNPMayLoad") { |
| 366 | Properties |= 1 << SDNPMayLoad; |
| 367 | } else if (PropList[i]->getName() == "SDNPSideEffect") { |
| 368 | Properties |= 1 << SDNPSideEffect; |
Evan Cheng | 94b3040 | 2006-10-11 21:02:01 +0000 | [diff] [blame] | 369 | } else { |
Bill Wendling | f5da133 | 2006-12-07 22:21:48 +0000 | [diff] [blame] | 370 | cerr << "Unsupported SD Node property '" << PropList[i]->getName() |
| 371 | << "' on ComplexPattern '" << R->getName() << "'!\n"; |
Evan Cheng | 94b3040 | 2006-10-11 21:02:01 +0000 | [diff] [blame] | 372 | exit(1); |
| 373 | } |
Christopher Lamb | 8535624 | 2008-01-31 07:27:46 +0000 | [diff] [blame] | 374 | |
| 375 | // Parse the attributes. |
| 376 | Attributes = 0; |
| 377 | PropList = R->getValueAsListOfDefs("Attributes"); |
| 378 | for (unsigned i = 0, e = PropList.size(); i != e; ++i) |
| 379 | if (PropList[i]->getName() == "CPAttrParentAsRoot") { |
| 380 | Attributes |= 1 << CPAttrParentAsRoot; |
| 381 | } else { |
| 382 | cerr << "Unsupported pattern attribute '" << PropList[i]->getName() |
| 383 | << "' on ComplexPattern '" << R->getName() << "'!\n"; |
| 384 | exit(1); |
| 385 | } |
Evan Cheng | 0fc7198 | 2005-12-08 02:00:36 +0000 | [diff] [blame] | 386 | } |
Evan Cheng | 3aa39f4 | 2005-12-08 02:14:08 +0000 | [diff] [blame] | 387 | |
Chris Lattner | 43fbbc3 | 2006-03-24 19:49:31 +0000 | [diff] [blame] | 388 | //===----------------------------------------------------------------------===// |
| 389 | // CodeGenIntrinsic Implementation |
| 390 | //===----------------------------------------------------------------------===// |
| 391 | |
| 392 | std::vector<CodeGenIntrinsic> llvm::LoadIntrinsics(const RecordKeeper &RC) { |
| 393 | std::vector<Record*> I = RC.getAllDerivedDefinitions("Intrinsic"); |
Chris Lattner | 8850a1b | 2006-03-27 22:48:18 +0000 | [diff] [blame] | 394 | |
| 395 | std::vector<CodeGenIntrinsic> Result; |
Chris Lattner | 6a160fb | 2006-03-28 00:15:00 +0000 | [diff] [blame] | 396 | |
Chris Lattner | 8850a1b | 2006-03-27 22:48:18 +0000 | [diff] [blame] | 397 | for (unsigned i = 0, e = I.size(); i != e; ++i) |
Dan Gohman | ee4fa19 | 2008-04-03 00:02:49 +0000 | [diff] [blame] | 398 | Result.push_back(CodeGenIntrinsic(I[i])); |
Chris Lattner | 8850a1b | 2006-03-27 22:48:18 +0000 | [diff] [blame] | 399 | return Result; |
Chris Lattner | 43fbbc3 | 2006-03-24 19:49:31 +0000 | [diff] [blame] | 400 | } |
| 401 | |
Dan Gohman | ee4fa19 | 2008-04-03 00:02:49 +0000 | [diff] [blame] | 402 | CodeGenIntrinsic::CodeGenIntrinsic(Record *R) { |
Chris Lattner | 2ca956f | 2006-03-24 20:25:01 +0000 | [diff] [blame] | 403 | TheDef = R; |
Chris Lattner | 43fbbc3 | 2006-03-24 19:49:31 +0000 | [diff] [blame] | 404 | std::string DefName = R->getName(); |
| 405 | ModRef = WriteMem; |
Reid Spencer | c4de3de | 2007-04-01 07:20:02 +0000 | [diff] [blame] | 406 | isOverloaded = false; |
Chris Lattner | 43fbbc3 | 2006-03-24 19:49:31 +0000 | [diff] [blame] | 407 | |
| 408 | if (DefName.size() <= 4 || |
| 409 | std::string(DefName.begin(), DefName.begin()+4) != "int_") |
| 410 | throw "Intrinsic '" + DefName + "' does not start with 'int_'!"; |
| 411 | EnumName = std::string(DefName.begin()+4, DefName.end()); |
| 412 | if (R->getValue("GCCBuiltinName")) // Ignore a missing GCCBuiltinName field. |
| 413 | GCCBuiltinName = R->getValueAsString("GCCBuiltinName"); |
| 414 | TargetPrefix = R->getValueAsString("TargetPrefix"); |
| 415 | Name = R->getValueAsString("LLVMName"); |
| 416 | if (Name == "") { |
| 417 | // If an explicit name isn't specified, derive one from the DefName. |
| 418 | Name = "llvm."; |
| 419 | for (unsigned i = 0, e = EnumName.size(); i != e; ++i) |
| 420 | if (EnumName[i] == '_') |
| 421 | Name += '.'; |
| 422 | else |
| 423 | Name += EnumName[i]; |
| 424 | } else { |
| 425 | // Verify it starts with "llvm.". |
| 426 | if (Name.size() <= 5 || |
| 427 | std::string(Name.begin(), Name.begin()+5) != "llvm.") |
| 428 | throw "Intrinsic '" + DefName + "'s name does not start with 'llvm.'!"; |
| 429 | } |
| 430 | |
| 431 | // If TargetPrefix is specified, make sure that Name starts with |
| 432 | // "llvm.<targetprefix>.". |
| 433 | if (!TargetPrefix.empty()) { |
| 434 | if (Name.size() < 6+TargetPrefix.size() || |
| 435 | std::string(Name.begin()+5, Name.begin()+6+TargetPrefix.size()) |
| 436 | != (TargetPrefix+".")) |
| 437 | throw "Intrinsic '" + DefName + "' does not start with 'llvm." + |
| 438 | TargetPrefix + ".'!"; |
| 439 | } |
| 440 | |
| 441 | // Parse the list of argument types. |
| 442 | ListInit *TypeList = R->getValueAsListInit("Types"); |
| 443 | for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) { |
Chris Lattner | 50d4565 | 2007-02-27 22:08:27 +0000 | [diff] [blame] | 444 | Record *TyEl = TypeList->getElementAsRecord(i); |
Chris Lattner | 43fbbc3 | 2006-03-24 19:49:31 +0000 | [diff] [blame] | 445 | assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!"); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame^] | 446 | MVT::SimpleValueType VT = getValueType(TyEl->getValueAsDef("VT")); |
Dan Gohman | 0fee3ff | 2007-08-16 21:57:19 +0000 | [diff] [blame] | 447 | isOverloaded |= VT == MVT::iAny || VT == MVT::fAny; |
Reid Spencer | c4de3de | 2007-04-01 07:20:02 +0000 | [diff] [blame] | 448 | ArgVTs.push_back(VT); |
Chris Lattner | 43fbbc3 | 2006-03-24 19:49:31 +0000 | [diff] [blame] | 449 | ArgTypeDefs.push_back(TyEl); |
| 450 | } |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 451 | if (ArgVTs.size() == 0) |
Chris Lattner | 43fbbc3 | 2006-03-24 19:49:31 +0000 | [diff] [blame] | 452 | throw "Intrinsic '"+DefName+"' needs at least a type for the ret value!"; |
Reid Spencer | c4de3de | 2007-04-01 07:20:02 +0000 | [diff] [blame] | 453 | |
Chris Lattner | 43fbbc3 | 2006-03-24 19:49:31 +0000 | [diff] [blame] | 454 | |
| 455 | // Parse the intrinsic properties. |
| 456 | ListInit *PropList = R->getValueAsListInit("Properties"); |
| 457 | for (unsigned i = 0, e = PropList->getSize(); i != e; ++i) { |
Chris Lattner | 50d4565 | 2007-02-27 22:08:27 +0000 | [diff] [blame] | 458 | Record *Property = PropList->getElementAsRecord(i); |
Chris Lattner | 43fbbc3 | 2006-03-24 19:49:31 +0000 | [diff] [blame] | 459 | assert(Property->isSubClassOf("IntrinsicProperty") && |
| 460 | "Expected a property!"); |
| 461 | |
Chris Lattner | 4b2362e | 2006-04-10 22:02:59 +0000 | [diff] [blame] | 462 | if (Property->getName() == "IntrNoMem") |
Chris Lattner | 43fbbc3 | 2006-03-24 19:49:31 +0000 | [diff] [blame] | 463 | ModRef = NoMem; |
Chris Lattner | 4b2362e | 2006-04-10 22:02:59 +0000 | [diff] [blame] | 464 | else if (Property->getName() == "IntrReadArgMem") |
Chris Lattner | 43fbbc3 | 2006-03-24 19:49:31 +0000 | [diff] [blame] | 465 | ModRef = ReadArgMem; |
| 466 | else if (Property->getName() == "IntrReadMem") |
| 467 | ModRef = ReadMem; |
Chris Lattner | 4b2362e | 2006-04-10 22:02:59 +0000 | [diff] [blame] | 468 | else if (Property->getName() == "IntrWriteArgMem") |
Chris Lattner | 43fbbc3 | 2006-03-24 19:49:31 +0000 | [diff] [blame] | 469 | ModRef = WriteArgMem; |
| 470 | else if (Property->getName() == "IntrWriteMem") |
| 471 | ModRef = WriteMem; |
| 472 | else |
| 473 | assert(0 && "Unknown property!"); |
| 474 | } |
| 475 | } |