Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- TargetInfo.cpp - Information about Target machine ----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the TargetInfo and TargetInfoImpl interfaces. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/Basic/TargetInfo.h" |
| 15 | #include "clang/Basic/Diagnostic.h" |
| 16 | #include "clang/AST/Builtins.h" |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/APFloat.h" |
Anders Carlsson | 6fa9086 | 2007-11-25 00:25:21 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/StringMap.h" |
| 19 | #include "llvm/ADT/STLExtras.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 20 | #include <set> |
| 21 | using namespace clang; |
| 22 | |
| 23 | void TargetInfoImpl::ANCHOR() {} // out-of-line virtual method for class. |
| 24 | |
| 25 | |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 26 | //===----------------------------------------------------------------------===// |
| 27 | // FIXME: These are temporary hacks, they should revector into the |
| 28 | // TargetInfoImpl. |
| 29 | |
| 30 | void TargetInfo::getFloatInfo(uint64_t &Size, unsigned &Align, |
| 31 | const llvm::fltSemantics *&Format, |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 32 | FullSourceLoc Loc) { |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 33 | Align = 32; // FIXME: implement correctly. |
| 34 | Size = 32; |
| 35 | Format = &llvm::APFloat::IEEEsingle; |
| 36 | } |
| 37 | void TargetInfo::getDoubleInfo(uint64_t &Size, unsigned &Align, |
| 38 | const llvm::fltSemantics *&Format, |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 39 | FullSourceLoc Loc) { |
Anders Carlsson | a9b20e5 | 2008-02-17 03:40:02 +0000 | [diff] [blame] | 40 | Size = 64; // FIXME: implement correctly. |
| 41 | Align = 32; |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 42 | Format = &llvm::APFloat::IEEEdouble; |
| 43 | } |
| 44 | void TargetInfo::getLongDoubleInfo(uint64_t &Size, unsigned &Align, |
| 45 | const llvm::fltSemantics *&Format, |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 46 | FullSourceLoc Loc) { |
Chris Lattner | 1c9bdae | 2007-09-22 18:38:30 +0000 | [diff] [blame] | 47 | Size = Align = 64; // FIXME: implement correctly. |
| 48 | Format = &llvm::APFloat::IEEEdouble; |
| 49 | //Size = 80; Align = 32; // FIXME: implement correctly. |
| 50 | //Format = &llvm::APFloat::x87DoubleExtended; |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | |
| 54 | //===----------------------------------------------------------------------===// |
| 55 | |
Ted Kremenek | ae36076 | 2007-12-03 22:06:55 +0000 | [diff] [blame] | 56 | const char* TargetInfo::getTargetTriple() const { |
| 57 | return PrimaryTarget->getTargetTriple(); |
| 58 | } |
| 59 | |
Anders Carlsson | 44fe49c | 2007-12-08 19:32:57 +0000 | [diff] [blame] | 60 | const char *TargetInfo::getTargetPrefix() const { |
| 61 | return PrimaryTarget->getTargetPrefix(); |
| 62 | } |
| 63 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 64 | /// DiagnoseNonPortability - When a use of a non-portable target feature is |
| 65 | /// used, this method emits the diagnostic and marks the translation unit as |
| 66 | /// non-portable. |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 67 | void TargetInfo::DiagnoseNonPortability(FullSourceLoc Loc, |
| 68 | unsigned DiagKind) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 69 | NonPortable = true; |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 70 | if (Diag && Loc.isValid()) Diag->Report(Loc, DiagKind); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | /// GetTargetDefineMap - Get the set of target #defines in an associative |
| 74 | /// collection for easy lookup. |
| 75 | static void GetTargetDefineMap(const TargetInfoImpl *Target, |
Chris Lattner | e36751b | 2007-07-22 20:11:46 +0000 | [diff] [blame] | 76 | llvm::StringMap<std::string> &Map) { |
Chris Lattner | d15fa82 | 2007-10-06 06:57:34 +0000 | [diff] [blame] | 77 | std::vector<char> Defines; |
| 78 | Defines.reserve(4096); |
| 79 | Target->getTargetDefines(Defines); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 80 | |
Chris Lattner | d15fa82 | 2007-10-06 06:57:34 +0000 | [diff] [blame] | 81 | for (const char *DefStr = &Defines[0], *E = DefStr+Defines.size(); |
| 82 | DefStr != E;) { |
| 83 | // Skip the '#define ' portion. |
| 84 | assert(memcmp(DefStr, "#define ", strlen("#define ")) == 0 && |
| 85 | "#define didn't start with #define!"); |
| 86 | DefStr += strlen("#define "); |
Chris Lattner | e36751b | 2007-07-22 20:11:46 +0000 | [diff] [blame] | 87 | |
Chris Lattner | d15fa82 | 2007-10-06 06:57:34 +0000 | [diff] [blame] | 88 | // Find the divider between the key and value. |
| 89 | const char *SpacePos = strchr(DefStr, ' '); |
| 90 | |
| 91 | std::string &Entry = Map.GetOrCreateValue(DefStr, SpacePos).getValue(); |
| 92 | |
| 93 | const char *EndPos = strchr(SpacePos+1, '\n'); |
| 94 | Entry = std::string(SpacePos+1, EndPos); |
| 95 | DefStr = EndPos+1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
| 99 | /// getTargetDefines - Appends the target-specific #define values for this |
| 100 | /// target set to the specified buffer. |
| 101 | void TargetInfo::getTargetDefines(std::vector<char> &Buffer) { |
Chris Lattner | eab7792 | 2007-10-06 06:29:41 +0000 | [diff] [blame] | 102 | // If we have no secondary targets, be a bit more efficient. |
| 103 | if (SecondaryTargets.empty()) { |
Chris Lattner | d15fa82 | 2007-10-06 06:57:34 +0000 | [diff] [blame] | 104 | PrimaryTarget->getTargetDefines(Buffer); |
Chris Lattner | eab7792 | 2007-10-06 06:29:41 +0000 | [diff] [blame] | 105 | return; |
| 106 | } |
| 107 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 108 | // This is tricky in the face of secondary targets. Specifically, |
| 109 | // target-specific #defines that are present and identical across all |
| 110 | // secondary targets are turned into #defines, #defines that are present in |
| 111 | // the primary target but are missing or different in the secondary targets |
| 112 | // are turned into #define_target, and #defines that are not defined in the |
| 113 | // primary, but are defined in a secondary are turned into |
| 114 | // #define_other_target. This allows the preprocessor to correctly track uses |
| 115 | // of target-specific macros. |
| 116 | |
| 117 | // Get the set of primary #defines. |
Chris Lattner | e36751b | 2007-07-22 20:11:46 +0000 | [diff] [blame] | 118 | llvm::StringMap<std::string> PrimaryDefines; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 119 | GetTargetDefineMap(PrimaryTarget, PrimaryDefines); |
| 120 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 121 | // Get the sets of secondary #defines. |
Chris Lattner | e36751b | 2007-07-22 20:11:46 +0000 | [diff] [blame] | 122 | llvm::StringMap<std::string> *SecondaryDefines |
| 123 | = new llvm::StringMap<std::string>[SecondaryTargets.size()]; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 124 | for (unsigned i = 0, e = SecondaryTargets.size(); i != e; ++i) |
| 125 | GetTargetDefineMap(SecondaryTargets[i], SecondaryDefines[i]); |
| 126 | |
| 127 | // Loop over all defines in the primary target, processing them until we run |
| 128 | // out. |
Chris Lattner | e36751b | 2007-07-22 20:11:46 +0000 | [diff] [blame] | 129 | for (llvm::StringMap<std::string>::iterator PDI = |
| 130 | PrimaryDefines.begin(), E = PrimaryDefines.end(); PDI != E; ++PDI) { |
| 131 | std::string DefineName(PDI->getKeyData(), |
| 132 | PDI->getKeyData() + PDI->getKeyLength()); |
| 133 | std::string DefineValue = PDI->getValue(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 134 | |
| 135 | // Check to see whether all secondary targets have this #define and whether |
| 136 | // it is to the same value. Remember if not, but remove the #define from |
| 137 | // their collection in any case if they have it. |
| 138 | bool isPortable = true; |
| 139 | |
Chris Lattner | e36751b | 2007-07-22 20:11:46 +0000 | [diff] [blame] | 140 | for (unsigned i = 0, e = SecondaryTargets.size(); i != e; ++i) { |
| 141 | llvm::StringMap<std::string>::iterator I = |
| 142 | SecondaryDefines[i].find(&DefineName[0], |
| 143 | &DefineName[0]+DefineName.size()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 144 | if (I == SecondaryDefines[i].end()) { |
| 145 | // Secondary target doesn't have this #define. |
| 146 | isPortable = false; |
| 147 | } else { |
| 148 | // Secondary target has this define, remember if it disagrees. |
| 149 | if (isPortable) |
Chris Lattner | e36751b | 2007-07-22 20:11:46 +0000 | [diff] [blame] | 150 | isPortable = I->getValue() == DefineValue; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 151 | // Remove it from the secondary target unconditionally. |
| 152 | SecondaryDefines[i].erase(I); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | // If this define is non-portable, turn it into #define_target, otherwise |
| 157 | // just use #define. |
| 158 | const char *Command = isPortable ? "#define " : "#define_target "; |
| 159 | Buffer.insert(Buffer.end(), Command, Command+strlen(Command)); |
| 160 | |
| 161 | // Insert "defname defvalue\n". |
| 162 | Buffer.insert(Buffer.end(), DefineName.begin(), DefineName.end()); |
| 163 | Buffer.push_back(' '); |
| 164 | Buffer.insert(Buffer.end(), DefineValue.begin(), DefineValue.end()); |
| 165 | Buffer.push_back('\n'); |
| 166 | } |
| 167 | |
| 168 | // Now that all of the primary target's defines have been handled and removed |
| 169 | // from the secondary target's define sets, go through the remaining secondary |
| 170 | // target's #defines and taint them. |
Chris Lattner | e36751b | 2007-07-22 20:11:46 +0000 | [diff] [blame] | 171 | for (unsigned i = 0, e = SecondaryTargets.size(); i != e; ++i) { |
| 172 | llvm::StringMap<std::string> &Defs = SecondaryDefines[i]; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 173 | while (!Defs.empty()) { |
Chris Lattner | e36751b | 2007-07-22 20:11:46 +0000 | [diff] [blame] | 174 | const char *DefStart = Defs.begin()->getKeyData(); |
| 175 | const char *DefEnd = DefStart + Defs.begin()->getKeyLength(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 176 | |
| 177 | // Insert "#define_other_target defname". |
| 178 | const char *Command = "#define_other_target "; |
| 179 | Buffer.insert(Buffer.end(), Command, Command+strlen(Command)); |
Chris Lattner | e36751b | 2007-07-22 20:11:46 +0000 | [diff] [blame] | 180 | Buffer.insert(Buffer.end(), DefStart, DefEnd); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 181 | Buffer.push_back('\n'); |
| 182 | |
| 183 | // If any other secondary targets have this same define, remove it from |
| 184 | // them to avoid duplicate #define_other_target directives. |
Chris Lattner | e36751b | 2007-07-22 20:11:46 +0000 | [diff] [blame] | 185 | for (unsigned j = i+1; j != e; ++j) { |
| 186 | llvm::StringMap<std::string>::iterator I = |
| 187 | SecondaryDefines[j].find(DefStart, DefEnd); |
| 188 | if (I != SecondaryDefines[j].end()) |
| 189 | SecondaryDefines[j].erase(I); |
| 190 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 191 | Defs.erase(Defs.begin()); |
| 192 | } |
| 193 | } |
Chris Lattner | e36751b | 2007-07-22 20:11:46 +0000 | [diff] [blame] | 194 | |
| 195 | delete[] SecondaryDefines; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | /// ComputeWCharWidth - Determine the width of the wchar_t type for the primary |
| 199 | /// target, diagnosing whether this is non-portable across the secondary |
| 200 | /// targets. |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 201 | void TargetInfo::ComputeWCharInfo(FullSourceLoc Loc) { |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 202 | PrimaryTarget->getWCharInfo(WCharWidth, WCharAlign); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 203 | |
| 204 | // Check whether this is portable across the secondary targets if the T-U is |
| 205 | // portable so far. |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 206 | for (unsigned i = 0, e = SecondaryTargets.size(); i != e; ++i) { |
| 207 | unsigned Width, Align; |
| 208 | SecondaryTargets[i]->getWCharInfo(Width, Align); |
| 209 | if (Width != WCharWidth || Align != WCharAlign) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 210 | return DiagnoseNonPortability(Loc, diag::port_wchar_t); |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 211 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | |
| 215 | /// getTargetBuiltins - Return information about target-specific builtins for |
| 216 | /// the current primary target, and info about which builtins are non-portable |
| 217 | /// across the current set of primary and secondary targets. |
| 218 | void TargetInfo::getTargetBuiltins(const Builtin::Info *&Records, |
| 219 | unsigned &NumRecords, |
| 220 | std::vector<const char *> &NPortable) const { |
| 221 | // Get info about what actual builtins we will expose. |
| 222 | PrimaryTarget->getTargetBuiltins(Records, NumRecords); |
| 223 | if (SecondaryTargets.empty()) return; |
| 224 | |
| 225 | // Compute the set of non-portable builtins. |
| 226 | |
| 227 | // Start by computing a mapping from the primary target's builtins to their |
| 228 | // info records for efficient lookup. |
Chris Lattner | e36751b | 2007-07-22 20:11:46 +0000 | [diff] [blame] | 229 | llvm::StringMap<const Builtin::Info*> PrimaryRecs; |
| 230 | for (unsigned i = 0, e = NumRecords; i != e; ++i) { |
| 231 | const char *BIName = Records[i].Name; |
| 232 | PrimaryRecs.GetOrCreateValue(BIName, BIName+strlen(BIName)).getValue() |
| 233 | = Records+i; |
| 234 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 235 | |
| 236 | for (unsigned i = 0, e = SecondaryTargets.size(); i != e; ++i) { |
| 237 | // Get the builtins for this secondary target. |
| 238 | const Builtin::Info *Records2nd; |
| 239 | unsigned NumRecords2nd; |
| 240 | SecondaryTargets[i]->getTargetBuiltins(Records2nd, NumRecords2nd); |
| 241 | |
| 242 | // Remember all of the secondary builtin names. |
| 243 | std::set<std::string> BuiltinNames2nd; |
| 244 | |
| 245 | for (unsigned j = 0, e = NumRecords2nd; j != e; ++j) { |
| 246 | BuiltinNames2nd.insert(Records2nd[j].Name); |
| 247 | |
| 248 | // Check to see if the primary target has this builtin. |
Chris Lattner | e36751b | 2007-07-22 20:11:46 +0000 | [diff] [blame] | 249 | llvm::StringMap<const Builtin::Info*>::iterator I = |
| 250 | PrimaryRecs.find(Records2nd[j].Name, |
| 251 | Records2nd[j].Name+strlen(Records2nd[j].Name)); |
| 252 | if (I != PrimaryRecs.end()) { |
| 253 | const Builtin::Info *PrimBI = I->getValue(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 254 | // If does. If they are not identical, mark the builtin as being |
| 255 | // non-portable. |
| 256 | if (Records2nd[j] != *PrimBI) |
| 257 | NPortable.push_back(PrimBI->Name); |
| 258 | } else { |
| 259 | // The primary target doesn't have this, it is non-portable. |
| 260 | NPortable.push_back(Records2nd[j].Name); |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | // Now that we checked all the secondary builtins, check to see if the |
| 265 | // primary target has any builtins that the secondary one doesn't. If so, |
| 266 | // then those are non-portable. |
| 267 | for (unsigned j = 0, e = NumRecords; j != e; ++j) { |
| 268 | if (!BuiltinNames2nd.count(Records[j].Name)) |
| 269 | NPortable.push_back(Records[j].Name); |
| 270 | } |
| 271 | } |
| 272 | } |
| 273 | |
Anders Carlsson | fb5e5ba | 2007-10-13 00:45:48 +0000 | [diff] [blame] | 274 | /// getVAListDeclaration - Return the declaration to use for |
| 275 | /// __builtin_va_list, which is target-specific. |
| 276 | const char *TargetInfo::getVAListDeclaration() const { |
| 277 | return PrimaryTarget->getVAListDeclaration(); |
| 278 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 279 | |
Anders Carlsson | ea04175 | 2008-02-06 00:11:32 +0000 | [diff] [blame] | 280 | static void removeGCCRegisterPrefix(const char *&Name) |
| 281 | { |
| 282 | if (Name[0] == '%' || Name[0] == '#') |
| 283 | Name++; |
| 284 | } |
| 285 | |
Anders Carlsson | 3346ae6 | 2007-11-24 23:38:12 +0000 | [diff] [blame] | 286 | /// isValidGCCRegisterName - Returns whether the passed in string |
| 287 | /// is a valid register name according to GCC. This is used by Sema for |
| 288 | /// inline asm statements. |
| 289 | bool TargetInfo::isValidGCCRegisterName(const char *Name) const { |
Anders Carlsson | 6fa9086 | 2007-11-25 00:25:21 +0000 | [diff] [blame] | 290 | const char * const *Names; |
| 291 | unsigned NumNames; |
| 292 | |
| 293 | // Get rid of any register prefix. |
Anders Carlsson | ea04175 | 2008-02-06 00:11:32 +0000 | [diff] [blame] | 294 | removeGCCRegisterPrefix(Name); |
| 295 | |
Anders Carlsson | 6fa9086 | 2007-11-25 00:25:21 +0000 | [diff] [blame] | 296 | |
| 297 | if (strcmp(Name, "memory") == 0 || |
| 298 | strcmp(Name, "cc") == 0) |
| 299 | return true; |
| 300 | |
| 301 | PrimaryTarget->getGCCRegNames(Names, NumNames); |
| 302 | |
| 303 | // If we have a number it maps to an entry in the register name array. |
| 304 | if (isdigit(Name[0])) { |
| 305 | char *End; |
| 306 | int n = (int)strtol(Name, &End, 0); |
| 307 | if (*End == 0) |
| 308 | return n >= 0 && (unsigned)n < NumNames; |
| 309 | } |
| 310 | |
| 311 | // Check register names. |
| 312 | for (unsigned i = 0; i < NumNames; i++) { |
| 313 | if (strcmp(Name, Names[i]) == 0) |
| 314 | return true; |
| 315 | } |
| 316 | |
| 317 | // Now check aliases. |
| 318 | const TargetInfoImpl::GCCRegAlias *Aliases; |
| 319 | unsigned NumAliases; |
| 320 | |
| 321 | PrimaryTarget->getGCCRegAliases(Aliases, NumAliases); |
| 322 | for (unsigned i = 0; i < NumAliases; i++) { |
| 323 | for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) { |
| 324 | if (!Aliases[i].Aliases[j]) |
| 325 | break; |
| 326 | if (strcmp(Aliases[i].Aliases[j], Name) == 0) |
| 327 | return true; |
| 328 | } |
| 329 | } |
| 330 | |
Anders Carlsson | 3346ae6 | 2007-11-24 23:38:12 +0000 | [diff] [blame] | 331 | return false; |
| 332 | } |
Anders Carlsson | d04c6e2 | 2007-11-27 04:11:28 +0000 | [diff] [blame] | 333 | |
| 334 | const char *TargetInfo::getNormalizedGCCRegisterName(const char *Name) const |
| 335 | { |
| 336 | assert(isValidGCCRegisterName(Name) && "Invalid register passed in"); |
| 337 | |
Anders Carlsson | ea04175 | 2008-02-06 00:11:32 +0000 | [diff] [blame] | 338 | removeGCCRegisterPrefix(Name); |
Anders Carlsson | ef3577d | 2008-02-05 23:30:20 +0000 | [diff] [blame] | 339 | |
Anders Carlsson | d04c6e2 | 2007-11-27 04:11:28 +0000 | [diff] [blame] | 340 | const char * const *Names; |
| 341 | unsigned NumNames; |
| 342 | |
| 343 | PrimaryTarget->getGCCRegNames(Names, NumNames); |
| 344 | |
| 345 | // First, check if we have a number. |
| 346 | if (isdigit(Name[0])) { |
| 347 | char *End; |
| 348 | int n = (int)strtol(Name, &End, 0); |
| 349 | if (*End == 0) { |
| 350 | assert(n >= 0 && (unsigned)n < NumNames && |
| 351 | "Out of bounds register number!"); |
| 352 | return Names[n]; |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | // Now check aliases. |
| 357 | const TargetInfoImpl::GCCRegAlias *Aliases; |
| 358 | unsigned NumAliases; |
| 359 | |
| 360 | PrimaryTarget->getGCCRegAliases(Aliases, NumAliases); |
| 361 | for (unsigned i = 0; i < NumAliases; i++) { |
| 362 | for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) { |
| 363 | if (!Aliases[i].Aliases[j]) |
| 364 | break; |
| 365 | if (strcmp(Aliases[i].Aliases[j], Name) == 0) |
| 366 | return Aliases[i].Register; |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | return Name; |
| 371 | } |
| 372 | |
| 373 | bool TargetInfo::validateOutputConstraint(const char *Name, |
| 374 | ConstraintInfo &info) const |
| 375 | { |
| 376 | // An output constraint must start with '=' or '+' |
| 377 | if (*Name != '=' && *Name != '+') |
| 378 | return false; |
| 379 | |
| 380 | if (*Name == '+') |
| 381 | info = CI_ReadWrite; |
| 382 | else |
| 383 | info = CI_None; |
| 384 | |
| 385 | Name++; |
| 386 | while (*Name) { |
| 387 | switch (*Name) { |
| 388 | default: |
| 389 | if (!PrimaryTarget->validateAsmConstraint(*Name, info)) { |
| 390 | // FIXME: This assert is in place temporarily |
| 391 | // so we can add more constraints as we hit it. |
| 392 | // Eventually, an unknown constraint should just be treated as 'g'. |
| 393 | assert(0 && "Unknown output constraint type!"); |
| 394 | } |
| 395 | case '&': // early clobber. |
| 396 | break; |
| 397 | case 'r': // general register. |
| 398 | info = (ConstraintInfo)(info|CI_AllowsRegister); |
| 399 | break; |
| 400 | case 'm': // memory operand. |
| 401 | info = (ConstraintInfo)(info|CI_AllowsMemory); |
| 402 | break; |
| 403 | case 'g': // general register, memory operand or immediate integer. |
| 404 | info = (ConstraintInfo)(info|CI_AllowsMemory|CI_AllowsRegister); |
| 405 | break; |
| 406 | } |
| 407 | |
| 408 | Name++; |
| 409 | } |
| 410 | |
| 411 | return true; |
| 412 | } |
| 413 | |
| 414 | bool TargetInfo::validateInputConstraint(const char *Name, |
| 415 | unsigned NumOutputs, |
| 416 | ConstraintInfo &info) const |
| 417 | { |
| 418 | while (*Name) { |
| 419 | switch (*Name) { |
| 420 | default: |
| 421 | // Check if we have a matching constraint |
| 422 | if (*Name >= '0' && *Name <= '9') { |
| 423 | unsigned i = *Name - '0'; |
| 424 | |
| 425 | // Check if matching constraint is out of bounds. |
| 426 | if (i >= NumOutputs) |
| 427 | return false; |
| 428 | } else if (!PrimaryTarget->validateAsmConstraint(*Name, info)) { |
| 429 | // FIXME: This assert is in place temporarily |
| 430 | // so we can add more constraints as we hit it. |
| 431 | // Eventually, an unknown constraint should just be treated as 'g'. |
| 432 | assert(0 && "Unknown input constraint type!"); |
| 433 | } |
Anders Carlsson | 44fe49c | 2007-12-08 19:32:57 +0000 | [diff] [blame] | 434 | case '%': // commutative |
| 435 | // FIXME: Fail if % is used with the last operand. |
| 436 | break; |
Anders Carlsson | d04c6e2 | 2007-11-27 04:11:28 +0000 | [diff] [blame] | 437 | case 'i': // immediate integer. |
| 438 | break; |
| 439 | case 'r': // general register. |
| 440 | info = (ConstraintInfo)(info|CI_AllowsRegister); |
| 441 | break; |
| 442 | case 'm': // memory operand. |
| 443 | info = (ConstraintInfo)(info|CI_AllowsMemory); |
| 444 | break; |
| 445 | case 'g': // general register, memory operand or immediate integer. |
| 446 | info = (ConstraintInfo)(info|CI_AllowsMemory|CI_AllowsRegister); |
| 447 | break; |
| 448 | } |
| 449 | |
| 450 | Name++; |
| 451 | } |
| 452 | |
| 453 | return true; |
| 454 | } |
| 455 | |
| 456 | const char *TargetInfo::getClobbers() const |
| 457 | { |
| 458 | return PrimaryTarget->getClobbers(); |
| 459 | } |
| 460 | |
| 461 | |