Chris Lattner | 803a5f6 | 2004-08-01 04:04:35 +0000 | [diff] [blame] | 1 | //===- CodeGenTarget.cpp - CodeGen Target Class Wrapper ---------*- C++ -*-===// |
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 | 803a5f6 | 2004-08-01 04:04:35 +0000 | [diff] [blame] | 10 | // This class wrap 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 | 5d7d3db | 2005-09-14 21:05:02 +0000 | [diff] [blame] | 23 | #include <set> |
Chris Lattner | 75ee2eb | 2005-10-14 03:54:49 +0000 | [diff] [blame] | 24 | #include <algorithm> |
Chris Lattner | 2082ebe | 2004-08-01 03:55:39 +0000 | [diff] [blame] | 25 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 26 | |
Chris Lattner | 560a79f | 2004-10-03 19:34:31 +0000 | [diff] [blame] | 27 | static cl::opt<unsigned> |
| 28 | AsmWriterNum("asmwriternum", cl::init(0), |
| 29 | cl::desc("Make -gen-asm-writer emit assembly writer #N")); |
| 30 | |
Chris Lattner | 4587207 | 2003-08-07 05:38:11 +0000 | [diff] [blame] | 31 | /// getValueType - Return the MCV::ValueType that the specified TableGen record |
| 32 | /// corresponds to. |
Dan Gohman | 3bf6e18 | 2007-07-13 20:16:50 +0000 | [diff] [blame] | 33 | MVT::ValueType llvm::getValueType(Record *Rec) { |
Evan Cheng | 2618d07 | 2006-05-17 20:37:59 +0000 | [diff] [blame] | 34 | return (MVT::ValueType)Rec->getValueAsInt("Value"); |
Chris Lattner | 4587207 | 2003-08-07 05:38:11 +0000 | [diff] [blame] | 35 | } |
| 36 | |
Chris Lattner | 2082ebe | 2004-08-01 03:55:39 +0000 | [diff] [blame] | 37 | std::string llvm::getName(MVT::ValueType T) { |
Chris Lattner | 4587207 | 2003-08-07 05:38:11 +0000 | [diff] [blame] | 38 | switch (T) { |
Chris Lattner | d3464c1 | 2003-08-07 23:15:21 +0000 | [diff] [blame] | 39 | case MVT::Other: return "UNKNOWN"; |
Evan Cheng | d7c2c86 | 2006-06-15 00:16:37 +0000 | [diff] [blame] | 40 | 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 Spencer | c4de3de | 2007-04-01 07:20:02 +0000 | [diff] [blame] | 46 | case MVT::iAny: return "MVT::iAny"; |
Dan Gohman | 0fee3ff | 2007-08-16 21:57:19 +0000 | [diff] [blame] | 47 | case MVT::fAny: return "MVT::fAny"; |
Evan Cheng | d7c2c86 | 2006-06-15 00:16:37 +0000 | [diff] [blame] | 48 | case MVT::f32: return "MVT::f32"; |
| 49 | case MVT::f64: return "MVT::f64"; |
| 50 | case MVT::f80: return "MVT::f80"; |
| 51 | case MVT::f128: return "MVT::f128"; |
Dale Johannesen | 317096a | 2007-09-28 01:08:20 +0000 | [diff] [blame] | 52 | case MVT::ppcf128: return "MVT::ppcf128"; |
Evan Cheng | d7c2c86 | 2006-06-15 00:16:37 +0000 | [diff] [blame] | 53 | case MVT::Flag: return "MVT::Flag"; |
| 54 | case MVT::isVoid:return "MVT::void"; |
| 55 | case MVT::v8i8: return "MVT::v8i8"; |
| 56 | case MVT::v4i16: return "MVT::v4i16"; |
| 57 | case MVT::v2i32: return "MVT::v2i32"; |
Bill Wendling | eebc8a1 | 2007-03-26 07:53:08 +0000 | [diff] [blame] | 58 | case MVT::v1i64: return "MVT::v1i64"; |
Evan Cheng | d7c2c86 | 2006-06-15 00:16:37 +0000 | [diff] [blame] | 59 | case MVT::v16i8: return "MVT::v16i8"; |
| 60 | case MVT::v8i16: return "MVT::v8i16"; |
| 61 | case MVT::v4i32: return "MVT::v4i32"; |
| 62 | case MVT::v2i64: return "MVT::v2i64"; |
| 63 | case MVT::v2f32: return "MVT::v2f32"; |
| 64 | case MVT::v4f32: return "MVT::v4f32"; |
| 65 | case MVT::v2f64: return "MVT::v2f64"; |
Christopher Lamb | 8245510 | 2007-07-26 06:41:18 +0000 | [diff] [blame] | 66 | case MVT::v3i32: return "MVT::v3i32"; |
| 67 | case MVT::v3f32: return "MVT::v3f32"; |
Evan Cheng | 6b12516 | 2006-05-17 20:55:51 +0000 | [diff] [blame] | 68 | case MVT::iPTR: return "TLI.getPointerTy()"; |
Chris Lattner | d3464c1 | 2003-08-07 23:15:21 +0000 | [diff] [blame] | 69 | default: assert(0 && "ILLEGAL VALUE TYPE!"); return ""; |
Chris Lattner | 4587207 | 2003-08-07 05:38:11 +0000 | [diff] [blame] | 70 | } |
Chris Lattner | d3464c1 | 2003-08-07 23:15:21 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Chris Lattner | 2082ebe | 2004-08-01 03:55:39 +0000 | [diff] [blame] | 73 | std::string llvm::getEnumName(MVT::ValueType T) { |
Chris Lattner | b72fb7e | 2003-08-10 19:50:32 +0000 | [diff] [blame] | 74 | switch (T) { |
Evan Cheng | 2618d07 | 2006-05-17 20:37:59 +0000 | [diff] [blame] | 75 | case MVT::Other: return "MVT::Other"; |
| 76 | case MVT::i1: return "MVT::i1"; |
| 77 | case MVT::i8: return "MVT::i8"; |
| 78 | case MVT::i16: return "MVT::i16"; |
| 79 | case MVT::i32: return "MVT::i32"; |
| 80 | case MVT::i64: return "MVT::i64"; |
| 81 | case MVT::i128: return "MVT::i128"; |
Reid Spencer | c4de3de | 2007-04-01 07:20:02 +0000 | [diff] [blame] | 82 | case MVT::iAny: return "MVT::iAny"; |
Dan Gohman | 0fee3ff | 2007-08-16 21:57:19 +0000 | [diff] [blame] | 83 | case MVT::fAny: return "MVT::fAny"; |
Evan Cheng | 2618d07 | 2006-05-17 20:37:59 +0000 | [diff] [blame] | 84 | case MVT::f32: return "MVT::f32"; |
| 85 | case MVT::f64: return "MVT::f64"; |
| 86 | case MVT::f80: return "MVT::f80"; |
| 87 | case MVT::f128: return "MVT::f128"; |
Dale Johannesen | 317096a | 2007-09-28 01:08:20 +0000 | [diff] [blame] | 88 | case MVT::ppcf128: return "MVT::ppcf128"; |
Evan Cheng | 2618d07 | 2006-05-17 20:37:59 +0000 | [diff] [blame] | 89 | case MVT::Flag: return "MVT::Flag"; |
| 90 | case MVT::isVoid:return "MVT::isVoid"; |
| 91 | case MVT::v8i8: return "MVT::v8i8"; |
| 92 | case MVT::v4i16: return "MVT::v4i16"; |
| 93 | case MVT::v2i32: return "MVT::v2i32"; |
Bill Wendling | eebc8a1 | 2007-03-26 07:53:08 +0000 | [diff] [blame] | 94 | case MVT::v1i64: return "MVT::v1i64"; |
Evan Cheng | 2618d07 | 2006-05-17 20:37:59 +0000 | [diff] [blame] | 95 | case MVT::v16i8: return "MVT::v16i8"; |
| 96 | case MVT::v8i16: return "MVT::v8i16"; |
| 97 | case MVT::v4i32: return "MVT::v4i32"; |
| 98 | case MVT::v2i64: return "MVT::v2i64"; |
| 99 | case MVT::v2f32: return "MVT::v2f32"; |
| 100 | case MVT::v4f32: return "MVT::v4f32"; |
| 101 | case MVT::v2f64: return "MVT::v2f64"; |
Christopher Lamb | 8245510 | 2007-07-26 06:41:18 +0000 | [diff] [blame] | 102 | case MVT::v3i32: return "MVT::v3i32"; |
| 103 | case MVT::v3f32: return "MVT::v3f32"; |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 104 | case MVT::iPTR: return "MVT::iPTR"; |
Chris Lattner | b72fb7e | 2003-08-10 19:50:32 +0000 | [diff] [blame] | 105 | default: assert(0 && "ILLEGAL VALUE TYPE!"); return ""; |
| 106 | } |
| 107 | } |
| 108 | |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame^] | 109 | /// getQualifiedName - Return the name of the specified record, with a |
| 110 | /// namespace qualifier if the record contains one. |
| 111 | /// |
| 112 | std::string llvm::getQualifiedName(const Record *R) { |
| 113 | std::string Namespace = R->getValueAsString("Namespace"); |
| 114 | if (Namespace.empty()) return R->getName(); |
| 115 | return Namespace + "::" + R->getName(); |
| 116 | } |
| 117 | |
| 118 | |
| 119 | |
Chris Lattner | d3464c1 | 2003-08-07 23:15:21 +0000 | [diff] [blame] | 120 | |
Chris Lattner | 4587207 | 2003-08-07 05:38:11 +0000 | [diff] [blame] | 121 | /// getTarget - Return the current instance of the Target class. |
| 122 | /// |
Evan Cheng | 2618d07 | 2006-05-17 20:37:59 +0000 | [diff] [blame] | 123 | CodeGenTarget::CodeGenTarget() { |
Chris Lattner | 4587207 | 2003-08-07 05:38:11 +0000 | [diff] [blame] | 124 | std::vector<Record*> Targets = Records.getAllDerivedDefinitions("Target"); |
Misha Brukman | bebdb20 | 2004-06-04 14:59:42 +0000 | [diff] [blame] | 125 | if (Targets.size() == 0) |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 126 | throw std::string("ERROR: No 'Target' subclasses defined!"); |
Chris Lattner | 4587207 | 2003-08-07 05:38:11 +0000 | [diff] [blame] | 127 | if (Targets.size() != 1) |
| 128 | throw std::string("ERROR: Multiple subclasses of Target defined!"); |
| 129 | TargetRec = Targets[0]; |
Chris Lattner | 4587207 | 2003-08-07 05:38:11 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | |
| 133 | const std::string &CodeGenTarget::getName() const { |
| 134 | return TargetRec->getName(); |
| 135 | } |
| 136 | |
| 137 | Record *CodeGenTarget::getInstructionSet() const { |
| 138 | return TargetRec->getValueAsDef("InstructionSet"); |
| 139 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 140 | |
Chris Lattner | 175580c | 2004-08-14 22:50:53 +0000 | [diff] [blame] | 141 | /// getAsmWriter - Return the AssemblyWriter definition for this target. |
| 142 | /// |
| 143 | Record *CodeGenTarget::getAsmWriter() const { |
Chris Lattner | b0e103d | 2005-10-28 22:49:02 +0000 | [diff] [blame] | 144 | std::vector<Record*> LI = TargetRec->getValueAsListOfDefs("AssemblyWriters"); |
| 145 | if (AsmWriterNum >= LI.size()) |
Chris Lattner | 560a79f | 2004-10-03 19:34:31 +0000 | [diff] [blame] | 146 | throw "Target does not have an AsmWriter #" + utostr(AsmWriterNum) + "!"; |
Chris Lattner | b0e103d | 2005-10-28 22:49:02 +0000 | [diff] [blame] | 147 | return LI[AsmWriterNum]; |
Chris Lattner | 175580c | 2004-08-14 22:50:53 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Chris Lattner | 2669311 | 2004-08-16 01:10:21 +0000 | [diff] [blame] | 150 | void CodeGenTarget::ReadRegisters() const { |
| 151 | std::vector<Record*> Regs = Records.getAllDerivedDefinitions("Register"); |
| 152 | if (Regs.empty()) |
| 153 | throw std::string("No 'Register' subclasses defined!"); |
| 154 | |
| 155 | Registers.reserve(Regs.size()); |
| 156 | Registers.assign(Regs.begin(), Regs.end()); |
| 157 | } |
| 158 | |
Chris Lattner | 7a680c6 | 2004-08-21 02:24:57 +0000 | [diff] [blame] | 159 | CodeGenRegister::CodeGenRegister(Record *R) : TheDef(R) { |
| 160 | DeclaredSpillSize = R->getValueAsInt("SpillSize"); |
| 161 | DeclaredSpillAlignment = R->getValueAsInt("SpillAlignment"); |
| 162 | } |
| 163 | |
Chris Lattner | 2669311 | 2004-08-16 01:10:21 +0000 | [diff] [blame] | 164 | const std::string &CodeGenRegister::getName() const { |
| 165 | return TheDef->getName(); |
| 166 | } |
| 167 | |
Chris Lattner | 056afef | 2004-08-21 04:05:00 +0000 | [diff] [blame] | 168 | void CodeGenTarget::ReadRegisterClasses() const { |
| 169 | std::vector<Record*> RegClasses = |
| 170 | Records.getAllDerivedDefinitions("RegisterClass"); |
| 171 | if (RegClasses.empty()) |
| 172 | throw std::string("No 'RegisterClass' subclasses defined!"); |
| 173 | |
| 174 | RegisterClasses.reserve(RegClasses.size()); |
| 175 | RegisterClasses.assign(RegClasses.begin(), RegClasses.end()); |
| 176 | } |
| 177 | |
Evan Cheng | 44a65fa | 2006-05-16 07:05:30 +0000 | [diff] [blame] | 178 | std::vector<unsigned char> CodeGenTarget::getRegisterVTs(Record *R) const { |
| 179 | std::vector<unsigned char> Result; |
| 180 | const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses(); |
| 181 | for (unsigned i = 0, e = RCs.size(); i != e; ++i) { |
| 182 | const CodeGenRegisterClass &RC = RegisterClasses[i]; |
| 183 | for (unsigned ei = 0, ee = RC.Elements.size(); ei != ee; ++ei) { |
| 184 | if (R == RC.Elements[ei]) { |
| 185 | const std::vector<MVT::ValueType> &InVTs = RC.getValueTypes(); |
| 186 | for (unsigned i = 0, e = InVTs.size(); i != e; ++i) |
| 187 | Result.push_back(InVTs[i]); |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | return Result; |
| 192 | } |
| 193 | |
| 194 | |
Chris Lattner | 056afef | 2004-08-21 04:05:00 +0000 | [diff] [blame] | 195 | CodeGenRegisterClass::CodeGenRegisterClass(Record *R) : TheDef(R) { |
Chris Lattner | c67c18f | 2005-08-19 18:45:20 +0000 | [diff] [blame] | 196 | // Rename anonymous register classes. |
| 197 | if (R->getName().size() > 9 && R->getName()[9] == '.') { |
| 198 | static unsigned AnonCounter = 0; |
| 199 | R->setName("AnonRegClass_"+utostr(AnonCounter++)); |
| 200 | } |
| 201 | |
Nate Begeman | 6510b22 | 2005-12-01 04:51:06 +0000 | [diff] [blame] | 202 | std::vector<Record*> TypeList = R->getValueAsListOfDefs("RegTypes"); |
| 203 | for (unsigned i = 0, e = TypeList.size(); i != e; ++i) { |
| 204 | Record *Type = TypeList[i]; |
| 205 | if (!Type->isSubClassOf("ValueType")) |
| 206 | throw "RegTypes list member '" + Type->getName() + |
| 207 | "' does not derive from the ValueType class!"; |
| 208 | VTs.push_back(getValueType(Type)); |
| 209 | } |
| 210 | assert(!VTs.empty() && "RegisterClass must contain at least one ValueType!"); |
Chris Lattner | ac46893 | 2005-08-19 19:12:51 +0000 | [diff] [blame] | 211 | |
Chris Lattner | b0e103d | 2005-10-28 22:49:02 +0000 | [diff] [blame] | 212 | std::vector<Record*> RegList = R->getValueAsListOfDefs("MemberList"); |
| 213 | for (unsigned i = 0, e = RegList.size(); i != e; ++i) { |
| 214 | Record *Reg = RegList[i]; |
Chris Lattner | 056afef | 2004-08-21 04:05:00 +0000 | [diff] [blame] | 215 | if (!Reg->isSubClassOf("Register")) |
| 216 | throw "Register Class member '" + Reg->getName() + |
| 217 | "' does not derive from the Register class!"; |
| 218 | Elements.push_back(Reg); |
| 219 | } |
Nate Begeman | 6510b22 | 2005-12-01 04:51:06 +0000 | [diff] [blame] | 220 | |
Christopher Lamb | a321125 | 2007-06-13 22:20:15 +0000 | [diff] [blame] | 221 | std::vector<Record*> SubRegClassList = |
| 222 | R->getValueAsListOfDefs("SubRegClassList"); |
| 223 | for (unsigned i = 0, e = SubRegClassList.size(); i != e; ++i) { |
| 224 | Record *SubRegClass = SubRegClassList[i]; |
| 225 | if (!SubRegClass->isSubClassOf("RegisterClass")) |
| 226 | throw "Register Class member '" + SubRegClass->getName() + |
| 227 | "' does not derive from the RegisterClass class!"; |
| 228 | SubRegClasses.push_back(SubRegClass); |
| 229 | } |
| 230 | |
Nate Begeman | 6510b22 | 2005-12-01 04:51:06 +0000 | [diff] [blame] | 231 | // Allow targets to override the size in bits of the RegisterClass. |
| 232 | unsigned Size = R->getValueAsInt("Size"); |
| 233 | |
| 234 | Namespace = R->getValueAsString("Namespace"); |
| 235 | SpillSize = Size ? Size : MVT::getSizeInBits(VTs[0]); |
| 236 | SpillAlignment = R->getValueAsInt("Alignment"); |
Evan Cheng | a3ca314 | 2007-09-19 01:35:01 +0000 | [diff] [blame] | 237 | CopyCost = R->getValueAsInt("CopyCost"); |
Nate Begeman | 6510b22 | 2005-12-01 04:51:06 +0000 | [diff] [blame] | 238 | MethodBodies = R->getValueAsCode("MethodBodies"); |
| 239 | MethodProtos = R->getValueAsCode("MethodProtos"); |
Chris Lattner | 056afef | 2004-08-21 04:05:00 +0000 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | const std::string &CodeGenRegisterClass::getName() const { |
| 243 | return TheDef->getName(); |
| 244 | } |
| 245 | |
Chris Lattner | e9f4ba8 | 2005-09-08 21:43:21 +0000 | [diff] [blame] | 246 | void CodeGenTarget::ReadLegalValueTypes() const { |
| 247 | const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses(); |
| 248 | for (unsigned i = 0, e = RCs.size(); i != e; ++i) |
Nate Begeman | 6510b22 | 2005-12-01 04:51:06 +0000 | [diff] [blame] | 249 | for (unsigned ri = 0, re = RCs[i].VTs.size(); ri != re; ++ri) |
| 250 | LegalValueTypes.push_back(RCs[i].VTs[ri]); |
Chris Lattner | 75ee2eb | 2005-10-14 03:54:49 +0000 | [diff] [blame] | 251 | |
| 252 | // Remove duplicates. |
| 253 | std::sort(LegalValueTypes.begin(), LegalValueTypes.end()); |
| 254 | LegalValueTypes.erase(std::unique(LegalValueTypes.begin(), |
| 255 | LegalValueTypes.end()), |
| 256 | LegalValueTypes.end()); |
Chris Lattner | e9f4ba8 | 2005-09-08 21:43:21 +0000 | [diff] [blame] | 257 | } |
Chris Lattner | 056afef | 2004-08-21 04:05:00 +0000 | [diff] [blame] | 258 | |
Chris Lattner | 175580c | 2004-08-14 22:50:53 +0000 | [diff] [blame] | 259 | |
Chris Lattner | ec35240 | 2004-08-01 05:04:00 +0000 | [diff] [blame] | 260 | void CodeGenTarget::ReadInstructions() const { |
| 261 | std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction"); |
Chris Lattner | aa77d77 | 2006-01-27 01:45:06 +0000 | [diff] [blame] | 262 | if (Insts.size() <= 2) |
Chris Lattner | ec35240 | 2004-08-01 05:04:00 +0000 | [diff] [blame] | 263 | throw std::string("No 'Instruction' subclasses defined!"); |
| 264 | |
Chris Lattner | aa77d77 | 2006-01-27 01:45:06 +0000 | [diff] [blame] | 265 | // Parse the instructions defined in the .td file. |
Chris Lattner | 175580c | 2004-08-14 22:50:53 +0000 | [diff] [blame] | 266 | std::string InstFormatName = |
| 267 | getAsmWriter()->getValueAsString("InstFormatName"); |
| 268 | |
| 269 | for (unsigned i = 0, e = Insts.size(); i != e; ++i) { |
| 270 | std::string AsmStr = Insts[i]->getValueAsString(InstFormatName); |
| 271 | Instructions.insert(std::make_pair(Insts[i]->getName(), |
| 272 | CodeGenInstruction(Insts[i], AsmStr))); |
| 273 | } |
Chris Lattner | ec35240 | 2004-08-01 05:04:00 +0000 | [diff] [blame] | 274 | } |
| 275 | |
Chris Lattner | d648867 | 2005-01-22 18:58:51 +0000 | [diff] [blame] | 276 | /// getInstructionsByEnumValue - Return all of the instructions defined by the |
| 277 | /// target, ordered by their enum value. |
| 278 | void CodeGenTarget:: |
| 279 | getInstructionsByEnumValue(std::vector<const CodeGenInstruction*> |
| 280 | &NumberedInstructions) { |
Chris Lattner | aa77d77 | 2006-01-27 01:45:06 +0000 | [diff] [blame] | 281 | std::map<std::string, CodeGenInstruction>::const_iterator I; |
| 282 | I = getInstructions().find("PHI"); |
| 283 | if (I == Instructions.end()) throw "Could not find 'PHI' instruction!"; |
| 284 | const CodeGenInstruction *PHI = &I->second; |
| 285 | |
| 286 | I = getInstructions().find("INLINEASM"); |
| 287 | if (I == Instructions.end()) throw "Could not find 'INLINEASM' instruction!"; |
| 288 | const CodeGenInstruction *INLINEASM = &I->second; |
| 289 | |
Jim Laskey | a683f9b | 2007-01-26 17:29:20 +0000 | [diff] [blame] | 290 | I = getInstructions().find("LABEL"); |
| 291 | if (I == Instructions.end()) throw "Could not find 'LABEL' instruction!"; |
| 292 | const CodeGenInstruction *LABEL = &I->second; |
| 293 | |
Christopher Lamb | 08d5207 | 2007-07-26 07:48:21 +0000 | [diff] [blame] | 294 | I = getInstructions().find("EXTRACT_SUBREG"); |
| 295 | if (I == Instructions.end()) |
| 296 | throw "Could not find 'EXTRACT_SUBREG' instruction!"; |
| 297 | const CodeGenInstruction *EXTRACT_SUBREG = &I->second; |
| 298 | |
| 299 | I = getInstructions().find("INSERT_SUBREG"); |
| 300 | if (I == Instructions.end()) |
| 301 | throw "Could not find 'INSERT_SUBREG' instruction!"; |
| 302 | const CodeGenInstruction *INSERT_SUBREG = &I->second; |
| 303 | |
Chris Lattner | d648867 | 2005-01-22 18:58:51 +0000 | [diff] [blame] | 304 | // Print out the rest of the instructions now. |
Chris Lattner | d648867 | 2005-01-22 18:58:51 +0000 | [diff] [blame] | 305 | NumberedInstructions.push_back(PHI); |
Chris Lattner | aa77d77 | 2006-01-27 01:45:06 +0000 | [diff] [blame] | 306 | NumberedInstructions.push_back(INLINEASM); |
Jim Laskey | a683f9b | 2007-01-26 17:29:20 +0000 | [diff] [blame] | 307 | NumberedInstructions.push_back(LABEL); |
Christopher Lamb | 08d5207 | 2007-07-26 07:48:21 +0000 | [diff] [blame] | 308 | NumberedInstructions.push_back(EXTRACT_SUBREG); |
| 309 | NumberedInstructions.push_back(INSERT_SUBREG); |
Chris Lattner | d648867 | 2005-01-22 18:58:51 +0000 | [diff] [blame] | 310 | for (inst_iterator II = inst_begin(), E = inst_end(); II != E; ++II) |
Jim Laskey | a683f9b | 2007-01-26 17:29:20 +0000 | [diff] [blame] | 311 | if (&II->second != PHI && |
| 312 | &II->second != INLINEASM && |
Christopher Lamb | 08d5207 | 2007-07-26 07:48:21 +0000 | [diff] [blame] | 313 | &II->second != LABEL && |
| 314 | &II->second != EXTRACT_SUBREG && |
| 315 | &II->second != INSERT_SUBREG) |
Chris Lattner | d648867 | 2005-01-22 18:58:51 +0000 | [diff] [blame] | 316 | NumberedInstructions.push_back(&II->second); |
| 317 | } |
| 318 | |
| 319 | |
Misha Brukman | 35e83cc | 2004-10-14 05:50:43 +0000 | [diff] [blame] | 320 | /// isLittleEndianEncoding - Return whether this target encodes its instruction |
| 321 | /// in little-endian format, i.e. bits laid out in the order [0..n] |
| 322 | /// |
| 323 | bool CodeGenTarget::isLittleEndianEncoding() const { |
| 324 | return getInstructionSet()->getValueAsBit("isLittleEndianEncoding"); |
| 325 | } |
| 326 | |
Chris Lattner | 0bb7500 | 2006-11-15 02:38:17 +0000 | [diff] [blame] | 327 | |
| 328 | |
| 329 | static void ParseConstraint(const std::string &CStr, CodeGenInstruction *I) { |
| 330 | // FIXME: Only supports TIED_TO for now. |
| 331 | std::string::size_type pos = CStr.find_first_of('='); |
Evan Cheng | e2ba897 | 2006-11-01 00:27:05 +0000 | [diff] [blame] | 332 | assert(pos != std::string::npos && "Unrecognized constraint"); |
Chris Lattner | 0bb7500 | 2006-11-15 02:38:17 +0000 | [diff] [blame] | 333 | std::string Name = CStr.substr(0, pos); |
Evan Cheng | e2ba897 | 2006-11-01 00:27:05 +0000 | [diff] [blame] | 334 | |
Evan Cheng | 4c2b7a3 | 2006-11-01 23:03:11 +0000 | [diff] [blame] | 335 | // TIED_TO: $src1 = $dst |
Chris Lattner | 0bb7500 | 2006-11-15 02:38:17 +0000 | [diff] [blame] | 336 | std::string::size_type wpos = Name.find_first_of(" \t"); |
| 337 | if (wpos == std::string::npos) |
| 338 | throw "Illegal format for tied-to constraint: '" + CStr + "'"; |
| 339 | std::string DestOpName = Name.substr(0, wpos); |
| 340 | std::pair<unsigned,unsigned> DestOp = I->ParseOperandName(DestOpName, false); |
Evan Cheng | e2ba897 | 2006-11-01 00:27:05 +0000 | [diff] [blame] | 341 | |
| 342 | Name = CStr.substr(pos+1); |
Chris Lattner | 0bb7500 | 2006-11-15 02:38:17 +0000 | [diff] [blame] | 343 | wpos = Name.find_first_not_of(" \t"); |
| 344 | if (wpos == std::string::npos) |
| 345 | throw "Illegal format for tied-to constraint: '" + CStr + "'"; |
| 346 | |
| 347 | std::pair<unsigned,unsigned> SrcOp = |
| 348 | I->ParseOperandName(Name.substr(wpos), false); |
| 349 | if (SrcOp > DestOp) |
Evan Cheng | 4c2b7a3 | 2006-11-01 23:03:11 +0000 | [diff] [blame] | 350 | throw "Illegal tied-to operand constraint '" + CStr + "'"; |
Chris Lattner | a0cca4a | 2006-11-06 23:49:51 +0000 | [diff] [blame] | 351 | |
Chris Lattner | 0bb7500 | 2006-11-15 02:38:17 +0000 | [diff] [blame] | 352 | |
| 353 | unsigned FlatOpNo = I->getFlattenedOperandNumber(SrcOp); |
| 354 | // Build the string for the operand. |
| 355 | std::string OpConstraint = |
Evan Cheng | 0555122 | 2006-12-01 22:57:41 +0000 | [diff] [blame] | 356 | "((" + utostr(FlatOpNo) + " << 16) | (1 << TOI::TIED_TO))"; |
Chris Lattner | 0bb7500 | 2006-11-15 02:38:17 +0000 | [diff] [blame] | 357 | |
| 358 | |
| 359 | if (!I->OperandList[DestOp.first].Constraints[DestOp.second].empty()) |
| 360 | throw "Operand '" + DestOpName + "' cannot have multiple constraints!"; |
| 361 | I->OperandList[DestOp.first].Constraints[DestOp.second] = OpConstraint; |
Evan Cheng | e2ba897 | 2006-11-01 00:27:05 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Chris Lattner | a0cca4a | 2006-11-06 23:49:51 +0000 | [diff] [blame] | 364 | static void ParseConstraints(const std::string &CStr, CodeGenInstruction *I) { |
Chris Lattner | 0bb7500 | 2006-11-15 02:38:17 +0000 | [diff] [blame] | 365 | // Make sure the constraints list for each operand is large enough to hold |
| 366 | // constraint info, even if none is present. |
| 367 | for (unsigned i = 0, e = I->OperandList.size(); i != e; ++i) |
| 368 | I->OperandList[i].Constraints.resize(I->OperandList[i].MINumOperands); |
| 369 | |
Chris Lattner | a0cca4a | 2006-11-06 23:49:51 +0000 | [diff] [blame] | 370 | if (CStr.empty()) return; |
| 371 | |
Evan Cheng | e2ba897 | 2006-11-01 00:27:05 +0000 | [diff] [blame] | 372 | const std::string delims(","); |
| 373 | std::string::size_type bidx, eidx; |
| 374 | |
| 375 | bidx = CStr.find_first_not_of(delims); |
| 376 | while (bidx != std::string::npos) { |
| 377 | eidx = CStr.find_first_of(delims, bidx); |
| 378 | if (eidx == std::string::npos) |
| 379 | eidx = CStr.length(); |
Chris Lattner | a0cca4a | 2006-11-06 23:49:51 +0000 | [diff] [blame] | 380 | |
Chris Lattner | 0bb7500 | 2006-11-15 02:38:17 +0000 | [diff] [blame] | 381 | ParseConstraint(CStr.substr(bidx, eidx), I); |
Evan Cheng | e2ba897 | 2006-11-01 00:27:05 +0000 | [diff] [blame] | 382 | bidx = CStr.find_first_not_of(delims, eidx); |
| 383 | } |
Evan Cheng | e2ba897 | 2006-11-01 00:27:05 +0000 | [diff] [blame] | 384 | } |
| 385 | |
Chris Lattner | 175580c | 2004-08-14 22:50:53 +0000 | [diff] [blame] | 386 | CodeGenInstruction::CodeGenInstruction(Record *R, const std::string &AsmStr) |
| 387 | : TheDef(R), AsmString(AsmStr) { |
Chris Lattner | ec35240 | 2004-08-01 05:04:00 +0000 | [diff] [blame] | 388 | Name = R->getValueAsString("Name"); |
| 389 | Namespace = R->getValueAsString("Namespace"); |
Chris Lattner | ec35240 | 2004-08-01 05:04:00 +0000 | [diff] [blame] | 390 | |
Chris Lattner | ec35240 | 2004-08-01 05:04:00 +0000 | [diff] [blame] | 391 | isReturn = R->getValueAsBit("isReturn"); |
| 392 | isBranch = R->getValueAsBit("isBranch"); |
Owen Anderson | 20ab290 | 2007-11-12 07:39:39 +0000 | [diff] [blame] | 393 | isIndirectBranch = R->getValueAsBit("isIndirectBranch"); |
Chris Lattner | ec35240 | 2004-08-01 05:04:00 +0000 | [diff] [blame] | 394 | isBarrier = R->getValueAsBit("isBarrier"); |
| 395 | isCall = R->getValueAsBit("isCall"); |
Nate Begeman | cdd66b5 | 2004-09-28 21:01:45 +0000 | [diff] [blame] | 396 | isLoad = R->getValueAsBit("isLoad"); |
| 397 | isStore = R->getValueAsBit("isStore"); |
Evan Cheng | 3dd298f | 2007-12-13 00:42:35 +0000 | [diff] [blame] | 398 | isImplicitDef= R->getValueAsBit("isImplicitDef"); |
Chris Lattner | f64f9a4 | 2006-11-15 23:23:02 +0000 | [diff] [blame] | 399 | bool isTwoAddress = R->getValueAsBit("isTwoAddress"); |
Evan Cheng | 5127ce0 | 2007-05-16 20:45:24 +0000 | [diff] [blame] | 400 | isPredicable = R->getValueAsBit("isPredicable"); |
Chris Lattner | aad75aa | 2005-01-02 02:29:04 +0000 | [diff] [blame] | 401 | isConvertibleToThreeAddress = R->getValueAsBit("isConvertibleToThreeAddress"); |
| 402 | isCommutable = R->getValueAsBit("isCommutable"); |
Chris Lattner | ec35240 | 2004-08-01 05:04:00 +0000 | [diff] [blame] | 403 | isTerminator = R->getValueAsBit("isTerminator"); |
Dan Gohman | d45eddd | 2007-06-26 00:48:07 +0000 | [diff] [blame] | 404 | isReMaterializable = R->getValueAsBit("isReMaterializable"); |
Chris Lattner | 5b71d3a | 2004-09-28 18:38:01 +0000 | [diff] [blame] | 405 | hasDelaySlot = R->getValueAsBit("hasDelaySlot"); |
Chris Lattner | e3cbf82 | 2005-08-26 20:55:40 +0000 | [diff] [blame] | 406 | usesCustomDAGSchedInserter = R->getValueAsBit("usesCustomDAGSchedInserter"); |
Evan Cheng | 1c3d19e | 2005-12-04 08:18:16 +0000 | [diff] [blame] | 407 | hasCtrlDep = R->getValueAsBit("hasCtrlDep"); |
Evan Cheng | eaa91b0 | 2007-06-19 01:26:51 +0000 | [diff] [blame] | 408 | isNotDuplicable = R->getValueAsBit("isNotDuplicable"); |
Bill Wendling | 6b1da9c | 2007-12-14 01:48:59 +0000 | [diff] [blame] | 409 | mayHaveSideEffects = R->getValueAsBit("mayHaveSideEffects"); |
| 410 | neverHasSideEffects = R->getValueAsBit("neverHasSideEffects"); |
Evan Cheng | 88cc092 | 2007-07-10 18:05:01 +0000 | [diff] [blame] | 411 | hasOptionalDef = false; |
Chris Lattner | cfbf96a | 2005-08-18 23:38:41 +0000 | [diff] [blame] | 412 | hasVariableNumberOfOperands = false; |
Bill Wendling | 6b1da9c | 2007-12-14 01:48:59 +0000 | [diff] [blame] | 413 | |
| 414 | if (mayHaveSideEffects && neverHasSideEffects) |
| 415 | throw R->getName() + |
| 416 | ": cannot have both 'mayHaveSideEffects' and 'neverHasSideEffects' set!"; |
| 417 | |
Chris Lattner | 5d7d3db | 2005-09-14 21:05:02 +0000 | [diff] [blame] | 418 | DagInit *DI; |
Chris Lattner | 2e1f51b | 2004-08-01 05:59:33 +0000 | [diff] [blame] | 419 | try { |
Evan Cheng | 64d80e3 | 2007-07-19 01:14:50 +0000 | [diff] [blame] | 420 | DI = R->getValueAsDag("OutOperandList"); |
Chris Lattner | 2e1f51b | 2004-08-01 05:59:33 +0000 | [diff] [blame] | 421 | } catch (...) { |
Chris Lattner | 5d7d3db | 2005-09-14 21:05:02 +0000 | [diff] [blame] | 422 | // Error getting operand list, just ignore it (sparcv9). |
Chris Lattner | 87c5905 | 2004-08-01 07:42:39 +0000 | [diff] [blame] | 423 | AsmString.clear(); |
| 424 | OperandList.clear(); |
Chris Lattner | 5d7d3db | 2005-09-14 21:05:02 +0000 | [diff] [blame] | 425 | return; |
| 426 | } |
Evan Cheng | 64d80e3 | 2007-07-19 01:14:50 +0000 | [diff] [blame] | 427 | NumDefs = DI->getNumArgs(); |
| 428 | |
| 429 | DagInit *IDI; |
| 430 | try { |
| 431 | IDI = R->getValueAsDag("InOperandList"); |
| 432 | } catch (...) { |
| 433 | // Error getting operand list, just ignore it (sparcv9). |
| 434 | AsmString.clear(); |
| 435 | OperandList.clear(); |
| 436 | return; |
| 437 | } |
| 438 | DI = (DagInit*)(new BinOpInit(BinOpInit::CONCAT, DI, IDI))->Fold(); |
Chris Lattner | 5d7d3db | 2005-09-14 21:05:02 +0000 | [diff] [blame] | 439 | |
| 440 | unsigned MIOperandNo = 0; |
| 441 | std::set<std::string> OperandNames; |
| 442 | for (unsigned i = 0, e = DI->getNumArgs(); i != e; ++i) { |
| 443 | DefInit *Arg = dynamic_cast<DefInit*>(DI->getArg(i)); |
| 444 | if (!Arg) |
| 445 | throw "Illegal operand for the '" + R->getName() + "' instruction!"; |
| 446 | |
| 447 | Record *Rec = Arg->getDef(); |
Chris Lattner | 5d7d3db | 2005-09-14 21:05:02 +0000 | [diff] [blame] | 448 | std::string PrintMethod = "printOperand"; |
| 449 | unsigned NumOps = 1; |
Chris Lattner | 3367079 | 2005-11-19 07:48:33 +0000 | [diff] [blame] | 450 | DagInit *MIOpInfo = 0; |
Nate Begeman | 86193d1 | 2005-12-01 00:12:04 +0000 | [diff] [blame] | 451 | if (Rec->isSubClassOf("Operand")) { |
Chris Lattner | 5d7d3db | 2005-09-14 21:05:02 +0000 | [diff] [blame] | 452 | PrintMethod = Rec->getValueAsString("PrintMethod"); |
Chris Lattner | 65303d6 | 2005-11-19 07:05:57 +0000 | [diff] [blame] | 453 | MIOpInfo = Rec->getValueAsDag("MIOperandInfo"); |
Chris Lattner | d438b53 | 2006-11-03 23:45:17 +0000 | [diff] [blame] | 454 | |
| 455 | // Verify that MIOpInfo has an 'ops' root value. |
| 456 | if (!dynamic_cast<DefInit*>(MIOpInfo->getOperator()) || |
| 457 | dynamic_cast<DefInit*>(MIOpInfo->getOperator()) |
| 458 | ->getDef()->getName() != "ops") |
| 459 | throw "Bad value for MIOperandInfo in operand '" + Rec->getName() + |
| 460 | "'\n"; |
| 461 | |
| 462 | // If we have MIOpInfo, then we have #operands equal to number of entries |
| 463 | // in MIOperandInfo. |
| 464 | if (unsigned NumArgs = MIOpInfo->getNumArgs()) |
| 465 | NumOps = NumArgs; |
| 466 | |
Evan Cheng | 88cc092 | 2007-07-10 18:05:01 +0000 | [diff] [blame] | 467 | if (Rec->isSubClassOf("PredicateOperand")) |
Evan Cheng | c419bd3 | 2007-07-06 23:23:38 +0000 | [diff] [blame] | 468 | isPredicable = true; |
Evan Cheng | 88cc092 | 2007-07-10 18:05:01 +0000 | [diff] [blame] | 469 | else if (Rec->isSubClassOf("OptionalDefOperand")) |
| 470 | hasOptionalDef = true; |
Chris Lattner | 5d7d3db | 2005-09-14 21:05:02 +0000 | [diff] [blame] | 471 | } else if (Rec->getName() == "variable_ops") { |
| 472 | hasVariableNumberOfOperands = true; |
| 473 | continue; |
Chris Lattner | f196839 | 2006-11-10 02:01:40 +0000 | [diff] [blame] | 474 | } else if (!Rec->isSubClassOf("RegisterClass") && |
| 475 | Rec->getName() != "ptr_rc") |
Chris Lattner | 5d7d3db | 2005-09-14 21:05:02 +0000 | [diff] [blame] | 476 | throw "Unknown operand class '" + Rec->getName() + |
| 477 | "' in instruction '" + R->getName() + "' instruction!"; |
| 478 | |
Chris Lattner | c4a8b73 | 2005-09-14 21:13:50 +0000 | [diff] [blame] | 479 | // Check that the operand has a name and that it's unique. |
| 480 | if (DI->getArgName(i).empty()) |
| 481 | throw "In instruction '" + R->getName() + "', operand #" + utostr(i) + |
| 482 | " has no name!"; |
| 483 | if (!OperandNames.insert(DI->getArgName(i)).second) |
Chris Lattner | 5d7d3db | 2005-09-14 21:05:02 +0000 | [diff] [blame] | 484 | throw "In instruction '" + R->getName() + "', operand #" + utostr(i) + |
| 485 | " has the same name as a previous operand!"; |
| 486 | |
Nate Begeman | 86193d1 | 2005-12-01 00:12:04 +0000 | [diff] [blame] | 487 | OperandList.push_back(OperandInfo(Rec, DI->getArgName(i), PrintMethod, |
| 488 | MIOperandNo, NumOps, MIOpInfo)); |
Chris Lattner | 5d7d3db | 2005-09-14 21:05:02 +0000 | [diff] [blame] | 489 | MIOperandNo += NumOps; |
Chris Lattner | 2e1f51b | 2004-08-01 05:59:33 +0000 | [diff] [blame] | 490 | } |
Evan Cheng | e2ba897 | 2006-11-01 00:27:05 +0000 | [diff] [blame] | 491 | |
Chris Lattner | f64f9a4 | 2006-11-15 23:23:02 +0000 | [diff] [blame] | 492 | // Parse Constraints. |
Chris Lattner | a0cca4a | 2006-11-06 23:49:51 +0000 | [diff] [blame] | 493 | ParseConstraints(R->getValueAsString("Constraints"), this); |
| 494 | |
| 495 | // For backward compatibility: isTwoAddress means operand 1 is tied to |
| 496 | // operand 0. |
Chris Lattner | 0bb7500 | 2006-11-15 02:38:17 +0000 | [diff] [blame] | 497 | if (isTwoAddress) { |
| 498 | if (!OperandList[1].Constraints[0].empty()) |
| 499 | throw R->getName() + ": cannot use isTwoAddress property: instruction " |
| 500 | "already has constraint set!"; |
Evan Cheng | 0555122 | 2006-12-01 22:57:41 +0000 | [diff] [blame] | 501 | OperandList[1].Constraints[0] = "((0 << 16) | (1 << TOI::TIED_TO))"; |
Chris Lattner | 0bb7500 | 2006-11-15 02:38:17 +0000 | [diff] [blame] | 502 | } |
Chris Lattner | a0cca4a | 2006-11-06 23:49:51 +0000 | [diff] [blame] | 503 | |
| 504 | // Any operands with unset constraints get 0 as their constraint. |
| 505 | for (unsigned op = 0, e = OperandList.size(); op != e; ++op) |
Chris Lattner | 0bb7500 | 2006-11-15 02:38:17 +0000 | [diff] [blame] | 506 | for (unsigned j = 0, e = OperandList[op].MINumOperands; j != e; ++j) |
| 507 | if (OperandList[op].Constraints[j].empty()) |
| 508 | OperandList[op].Constraints[j] = "0"; |
Chris Lattner | f64f9a4 | 2006-11-15 23:23:02 +0000 | [diff] [blame] | 509 | |
| 510 | // Parse the DisableEncoding field. |
| 511 | std::string DisableEncoding = R->getValueAsString("DisableEncoding"); |
| 512 | while (1) { |
| 513 | std::string OpName = getToken(DisableEncoding, " ,\t"); |
| 514 | if (OpName.empty()) break; |
| 515 | |
| 516 | // Figure out which operand this is. |
| 517 | std::pair<unsigned,unsigned> Op = ParseOperandName(OpName, false); |
| 518 | |
| 519 | // Mark the operand as not-to-be encoded. |
| 520 | if (Op.second >= OperandList[Op.first].DoNotEncode.size()) |
| 521 | OperandList[Op.first].DoNotEncode.resize(Op.second+1); |
| 522 | OperandList[Op.first].DoNotEncode[Op.second] = true; |
| 523 | } |
Chris Lattner | ec35240 | 2004-08-01 05:04:00 +0000 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | |
Chris Lattner | 87c5905 | 2004-08-01 07:42:39 +0000 | [diff] [blame] | 527 | |
| 528 | /// getOperandNamed - Return the index of the operand with the specified |
| 529 | /// non-empty name. If the instruction does not have an operand with the |
| 530 | /// specified name, throw an exception. |
Misha Brukman | 35e83cc | 2004-10-14 05:50:43 +0000 | [diff] [blame] | 531 | /// |
Chris Lattner | 87c5905 | 2004-08-01 07:42:39 +0000 | [diff] [blame] | 532 | unsigned CodeGenInstruction::getOperandNamed(const std::string &Name) const { |
| 533 | assert(!Name.empty() && "Cannot search for operand with no name!"); |
| 534 | for (unsigned i = 0, e = OperandList.size(); i != e; ++i) |
| 535 | if (OperandList[i].Name == Name) return i; |
| 536 | throw "Instruction '" + TheDef->getName() + |
| 537 | "' does not have an operand named '$" + Name + "'!"; |
| 538 | } |
Evan Cheng | 0fc7198 | 2005-12-08 02:00:36 +0000 | [diff] [blame] | 539 | |
Chris Lattner | 0bb7500 | 2006-11-15 02:38:17 +0000 | [diff] [blame] | 540 | std::pair<unsigned,unsigned> |
| 541 | CodeGenInstruction::ParseOperandName(const std::string &Op, |
| 542 | bool AllowWholeOp) { |
| 543 | if (Op.empty() || Op[0] != '$') |
| 544 | throw TheDef->getName() + ": Illegal operand name: '" + Op + "'"; |
| 545 | |
| 546 | std::string OpName = Op.substr(1); |
| 547 | std::string SubOpName; |
| 548 | |
| 549 | // Check to see if this is $foo.bar. |
| 550 | std::string::size_type DotIdx = OpName.find_first_of("."); |
| 551 | if (DotIdx != std::string::npos) { |
| 552 | SubOpName = OpName.substr(DotIdx+1); |
| 553 | if (SubOpName.empty()) |
| 554 | throw TheDef->getName() + ": illegal empty suboperand name in '" +Op +"'"; |
| 555 | OpName = OpName.substr(0, DotIdx); |
| 556 | } |
| 557 | |
| 558 | unsigned OpIdx = getOperandNamed(OpName); |
| 559 | |
| 560 | if (SubOpName.empty()) { // If no suboperand name was specified: |
| 561 | // If one was needed, throw. |
| 562 | if (OperandList[OpIdx].MINumOperands > 1 && !AllowWholeOp && |
| 563 | SubOpName.empty()) |
| 564 | throw TheDef->getName() + ": Illegal to refer to" |
| 565 | " whole operand part of complex operand '" + Op + "'"; |
| 566 | |
| 567 | // Otherwise, return the operand. |
| 568 | return std::make_pair(OpIdx, 0U); |
| 569 | } |
| 570 | |
| 571 | // Find the suboperand number involved. |
| 572 | DagInit *MIOpInfo = OperandList[OpIdx].MIOperandInfo; |
| 573 | if (MIOpInfo == 0) |
| 574 | throw TheDef->getName() + ": unknown suboperand name in '" + Op + "'"; |
| 575 | |
| 576 | // Find the operand with the right name. |
| 577 | for (unsigned i = 0, e = MIOpInfo->getNumArgs(); i != e; ++i) |
| 578 | if (MIOpInfo->getArgName(i) == SubOpName) |
| 579 | return std::make_pair(OpIdx, i); |
| 580 | |
| 581 | // Otherwise, didn't find it! |
| 582 | throw TheDef->getName() + ": unknown suboperand name in '" + Op + "'"; |
| 583 | } |
| 584 | |
| 585 | |
| 586 | |
| 587 | |
Evan Cheng | 0fc7198 | 2005-12-08 02:00:36 +0000 | [diff] [blame] | 588 | //===----------------------------------------------------------------------===// |
| 589 | // ComplexPattern implementation |
| 590 | // |
| 591 | ComplexPattern::ComplexPattern(Record *R) { |
Evan Cheng | 3aa39f4 | 2005-12-08 02:14:08 +0000 | [diff] [blame] | 592 | Ty = ::getValueType(R->getValueAsDef("Ty")); |
| 593 | NumOperands = R->getValueAsInt("NumOperands"); |
| 594 | SelectFunc = R->getValueAsString("SelectFunc"); |
| 595 | RootNodes = R->getValueAsListOfDefs("RootNodes"); |
Evan Cheng | 94b3040 | 2006-10-11 21:02:01 +0000 | [diff] [blame] | 596 | |
| 597 | // Parse the properties. |
| 598 | Properties = 0; |
| 599 | std::vector<Record*> PropList = R->getValueAsListOfDefs("Properties"); |
| 600 | for (unsigned i = 0, e = PropList.size(); i != e; ++i) |
| 601 | if (PropList[i]->getName() == "SDNPHasChain") { |
| 602 | Properties |= 1 << SDNPHasChain; |
| 603 | } else if (PropList[i]->getName() == "SDNPOptInFlag") { |
| 604 | Properties |= 1 << SDNPOptInFlag; |
| 605 | } else { |
Bill Wendling | f5da133 | 2006-12-07 22:21:48 +0000 | [diff] [blame] | 606 | cerr << "Unsupported SD Node property '" << PropList[i]->getName() |
| 607 | << "' on ComplexPattern '" << R->getName() << "'!\n"; |
Evan Cheng | 94b3040 | 2006-10-11 21:02:01 +0000 | [diff] [blame] | 608 | exit(1); |
| 609 | } |
Evan Cheng | 0fc7198 | 2005-12-08 02:00:36 +0000 | [diff] [blame] | 610 | } |
Evan Cheng | 3aa39f4 | 2005-12-08 02:14:08 +0000 | [diff] [blame] | 611 | |
Chris Lattner | 43fbbc3 | 2006-03-24 19:49:31 +0000 | [diff] [blame] | 612 | //===----------------------------------------------------------------------===// |
| 613 | // CodeGenIntrinsic Implementation |
| 614 | //===----------------------------------------------------------------------===// |
| 615 | |
| 616 | std::vector<CodeGenIntrinsic> llvm::LoadIntrinsics(const RecordKeeper &RC) { |
| 617 | std::vector<Record*> I = RC.getAllDerivedDefinitions("Intrinsic"); |
Chris Lattner | 8850a1b | 2006-03-27 22:48:18 +0000 | [diff] [blame] | 618 | |
| 619 | std::vector<CodeGenIntrinsic> Result; |
Chris Lattner | 6a160fb | 2006-03-28 00:15:00 +0000 | [diff] [blame] | 620 | |
| 621 | // If we are in the context of a target .td file, get the target info so that |
| 622 | // we can decode the current intptr_t. |
| 623 | CodeGenTarget *CGT = 0; |
| 624 | if (Records.getClass("Target") && |
| 625 | Records.getAllDerivedDefinitions("Target").size() == 1) |
| 626 | CGT = new CodeGenTarget(); |
| 627 | |
Chris Lattner | 8850a1b | 2006-03-27 22:48:18 +0000 | [diff] [blame] | 628 | for (unsigned i = 0, e = I.size(); i != e; ++i) |
Chris Lattner | 6a160fb | 2006-03-28 00:15:00 +0000 | [diff] [blame] | 629 | Result.push_back(CodeGenIntrinsic(I[i], CGT)); |
| 630 | delete CGT; |
Chris Lattner | 8850a1b | 2006-03-27 22:48:18 +0000 | [diff] [blame] | 631 | return Result; |
Chris Lattner | 43fbbc3 | 2006-03-24 19:49:31 +0000 | [diff] [blame] | 632 | } |
| 633 | |
Chris Lattner | 76f8c7c | 2006-03-28 00:03:08 +0000 | [diff] [blame] | 634 | CodeGenIntrinsic::CodeGenIntrinsic(Record *R, CodeGenTarget *CGT) { |
Chris Lattner | 2ca956f | 2006-03-24 20:25:01 +0000 | [diff] [blame] | 635 | TheDef = R; |
Chris Lattner | 43fbbc3 | 2006-03-24 19:49:31 +0000 | [diff] [blame] | 636 | std::string DefName = R->getName(); |
| 637 | ModRef = WriteMem; |
Reid Spencer | c4de3de | 2007-04-01 07:20:02 +0000 | [diff] [blame] | 638 | isOverloaded = false; |
Chris Lattner | 43fbbc3 | 2006-03-24 19:49:31 +0000 | [diff] [blame] | 639 | |
| 640 | if (DefName.size() <= 4 || |
| 641 | std::string(DefName.begin(), DefName.begin()+4) != "int_") |
| 642 | throw "Intrinsic '" + DefName + "' does not start with 'int_'!"; |
| 643 | EnumName = std::string(DefName.begin()+4, DefName.end()); |
| 644 | if (R->getValue("GCCBuiltinName")) // Ignore a missing GCCBuiltinName field. |
| 645 | GCCBuiltinName = R->getValueAsString("GCCBuiltinName"); |
| 646 | TargetPrefix = R->getValueAsString("TargetPrefix"); |
| 647 | Name = R->getValueAsString("LLVMName"); |
| 648 | if (Name == "") { |
| 649 | // If an explicit name isn't specified, derive one from the DefName. |
| 650 | Name = "llvm."; |
| 651 | for (unsigned i = 0, e = EnumName.size(); i != e; ++i) |
| 652 | if (EnumName[i] == '_') |
| 653 | Name += '.'; |
| 654 | else |
| 655 | Name += EnumName[i]; |
| 656 | } else { |
| 657 | // Verify it starts with "llvm.". |
| 658 | if (Name.size() <= 5 || |
| 659 | std::string(Name.begin(), Name.begin()+5) != "llvm.") |
| 660 | throw "Intrinsic '" + DefName + "'s name does not start with 'llvm.'!"; |
| 661 | } |
| 662 | |
| 663 | // If TargetPrefix is specified, make sure that Name starts with |
| 664 | // "llvm.<targetprefix>.". |
| 665 | if (!TargetPrefix.empty()) { |
| 666 | if (Name.size() < 6+TargetPrefix.size() || |
| 667 | std::string(Name.begin()+5, Name.begin()+6+TargetPrefix.size()) |
| 668 | != (TargetPrefix+".")) |
| 669 | throw "Intrinsic '" + DefName + "' does not start with 'llvm." + |
| 670 | TargetPrefix + ".'!"; |
| 671 | } |
| 672 | |
| 673 | // Parse the list of argument types. |
| 674 | ListInit *TypeList = R->getValueAsListInit("Types"); |
| 675 | for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) { |
Chris Lattner | 50d4565 | 2007-02-27 22:08:27 +0000 | [diff] [blame] | 676 | Record *TyEl = TypeList->getElementAsRecord(i); |
Chris Lattner | 43fbbc3 | 2006-03-24 19:49:31 +0000 | [diff] [blame] | 677 | assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!"); |
Dan Gohman | 3bf6e18 | 2007-07-13 20:16:50 +0000 | [diff] [blame] | 678 | MVT::ValueType VT = getValueType(TyEl->getValueAsDef("VT")); |
Dan Gohman | 0fee3ff | 2007-08-16 21:57:19 +0000 | [diff] [blame] | 679 | isOverloaded |= VT == MVT::iAny || VT == MVT::fAny; |
Reid Spencer | c4de3de | 2007-04-01 07:20:02 +0000 | [diff] [blame] | 680 | ArgVTs.push_back(VT); |
Chris Lattner | 43fbbc3 | 2006-03-24 19:49:31 +0000 | [diff] [blame] | 681 | ArgTypeDefs.push_back(TyEl); |
| 682 | } |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 683 | if (ArgVTs.size() == 0) |
Chris Lattner | 43fbbc3 | 2006-03-24 19:49:31 +0000 | [diff] [blame] | 684 | throw "Intrinsic '"+DefName+"' needs at least a type for the ret value!"; |
Reid Spencer | c4de3de | 2007-04-01 07:20:02 +0000 | [diff] [blame] | 685 | |
Chris Lattner | 43fbbc3 | 2006-03-24 19:49:31 +0000 | [diff] [blame] | 686 | |
| 687 | // Parse the intrinsic properties. |
| 688 | ListInit *PropList = R->getValueAsListInit("Properties"); |
| 689 | for (unsigned i = 0, e = PropList->getSize(); i != e; ++i) { |
Chris Lattner | 50d4565 | 2007-02-27 22:08:27 +0000 | [diff] [blame] | 690 | Record *Property = PropList->getElementAsRecord(i); |
Chris Lattner | 43fbbc3 | 2006-03-24 19:49:31 +0000 | [diff] [blame] | 691 | assert(Property->isSubClassOf("IntrinsicProperty") && |
| 692 | "Expected a property!"); |
| 693 | |
Chris Lattner | 4b2362e | 2006-04-10 22:02:59 +0000 | [diff] [blame] | 694 | if (Property->getName() == "IntrNoMem") |
Chris Lattner | 43fbbc3 | 2006-03-24 19:49:31 +0000 | [diff] [blame] | 695 | ModRef = NoMem; |
Chris Lattner | 4b2362e | 2006-04-10 22:02:59 +0000 | [diff] [blame] | 696 | else if (Property->getName() == "IntrReadArgMem") |
Chris Lattner | 43fbbc3 | 2006-03-24 19:49:31 +0000 | [diff] [blame] | 697 | ModRef = ReadArgMem; |
| 698 | else if (Property->getName() == "IntrReadMem") |
| 699 | ModRef = ReadMem; |
Chris Lattner | 4b2362e | 2006-04-10 22:02:59 +0000 | [diff] [blame] | 700 | else if (Property->getName() == "IntrWriteArgMem") |
Chris Lattner | 43fbbc3 | 2006-03-24 19:49:31 +0000 | [diff] [blame] | 701 | ModRef = WriteArgMem; |
| 702 | else if (Property->getName() == "IntrWriteMem") |
| 703 | ModRef = WriteMem; |
| 704 | else |
| 705 | assert(0 && "Unknown property!"); |
| 706 | } |
| 707 | } |