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