Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1 | //===--- TargetInfo.cpp - Information about Target machine ----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 959e5be | 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. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +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 | 858eece | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/APFloat.h" |
Anders Carlsson | 49dadd6 | 2007-11-25 00:25:21 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/StringMap.h" |
| 19 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +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 | 858eece | 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 | d7f64cd | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 32 | FullSourceLoc Loc) { |
Chris Lattner | 858eece | 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 | d7f64cd | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 39 | FullSourceLoc Loc) { |
Anders Carlsson | f6db426 | 2008-02-17 03:40:02 +0000 | [diff] [blame] | 40 | Size = 64; // FIXME: implement correctly. |
| 41 | Align = 32; |
Chris Lattner | 858eece | 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 | d7f64cd | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 46 | FullSourceLoc Loc) { |
Chris Lattner | 36f46b8 | 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 | 858eece | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | |
| 54 | //===----------------------------------------------------------------------===// |
| 55 | |
Chris Lattner | 296d1c8 | 2008-03-05 00:53:34 +0000 | [diff] [blame] | 56 | TargetInfo::~TargetInfo() { |
Chris Lattner | fc45700 | 2008-03-05 01:18:20 +0000 | [diff] [blame^] | 57 | delete Target; |
Chris Lattner | 296d1c8 | 2008-03-05 00:53:34 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Ted Kremenek | 4049948 | 2007-12-03 22:06:55 +0000 | [diff] [blame] | 60 | const char* TargetInfo::getTargetTriple() const { |
Chris Lattner | fc45700 | 2008-03-05 01:18:20 +0000 | [diff] [blame^] | 61 | return Target->getTargetTriple(); |
Ted Kremenek | 4049948 | 2007-12-03 22:06:55 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Anders Carlsson | 333052c | 2007-12-08 19:32:57 +0000 | [diff] [blame] | 64 | const char *TargetInfo::getTargetPrefix() const { |
Chris Lattner | fc45700 | 2008-03-05 01:18:20 +0000 | [diff] [blame^] | 65 | return Target->getTargetPrefix(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | /// getTargetDefines - Appends the target-specific #define values for this |
| 69 | /// target set to the specified buffer. |
| 70 | void TargetInfo::getTargetDefines(std::vector<char> &Buffer) { |
Chris Lattner | fc45700 | 2008-03-05 01:18:20 +0000 | [diff] [blame^] | 71 | Target->getTargetDefines(Buffer); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | /// ComputeWCharWidth - Determine the width of the wchar_t type for the primary |
| 75 | /// target, diagnosing whether this is non-portable across the secondary |
| 76 | /// targets. |
Ted Kremenek | d7f64cd | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 77 | void TargetInfo::ComputeWCharInfo(FullSourceLoc Loc) { |
Chris Lattner | fc45700 | 2008-03-05 01:18:20 +0000 | [diff] [blame^] | 78 | Target->getWCharInfo(WCharWidth, WCharAlign); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | |
| 82 | /// getTargetBuiltins - Return information about target-specific builtins for |
| 83 | /// the current primary target, and info about which builtins are non-portable |
| 84 | /// across the current set of primary and secondary targets. |
| 85 | void TargetInfo::getTargetBuiltins(const Builtin::Info *&Records, |
Chris Lattner | fc45700 | 2008-03-05 01:18:20 +0000 | [diff] [blame^] | 86 | unsigned &NumRecords) const { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 87 | // Get info about what actual builtins we will expose. |
Chris Lattner | fc45700 | 2008-03-05 01:18:20 +0000 | [diff] [blame^] | 88 | Target->getTargetBuiltins(Records, NumRecords); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Anders Carlsson | 8b58e8a | 2007-10-13 00:45:48 +0000 | [diff] [blame] | 91 | /// getVAListDeclaration - Return the declaration to use for |
| 92 | /// __builtin_va_list, which is target-specific. |
| 93 | const char *TargetInfo::getVAListDeclaration() const { |
Chris Lattner | fc45700 | 2008-03-05 01:18:20 +0000 | [diff] [blame^] | 94 | return Target->getVAListDeclaration(); |
Anders Carlsson | 8b58e8a | 2007-10-13 00:45:48 +0000 | [diff] [blame] | 95 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 96 | |
Chris Lattner | fc45700 | 2008-03-05 01:18:20 +0000 | [diff] [blame^] | 97 | static void removeGCCRegisterPrefix(const char *&Name) { |
Anders Carlsson | 7438598 | 2008-02-06 00:11:32 +0000 | [diff] [blame] | 98 | if (Name[0] == '%' || Name[0] == '#') |
| 99 | Name++; |
| 100 | } |
| 101 | |
Anders Carlsson | 7dd1c95 | 2007-11-24 23:38:12 +0000 | [diff] [blame] | 102 | /// isValidGCCRegisterName - Returns whether the passed in string |
| 103 | /// is a valid register name according to GCC. This is used by Sema for |
| 104 | /// inline asm statements. |
| 105 | bool TargetInfo::isValidGCCRegisterName(const char *Name) const { |
Anders Carlsson | 49dadd6 | 2007-11-25 00:25:21 +0000 | [diff] [blame] | 106 | const char * const *Names; |
| 107 | unsigned NumNames; |
| 108 | |
| 109 | // Get rid of any register prefix. |
Anders Carlsson | 7438598 | 2008-02-06 00:11:32 +0000 | [diff] [blame] | 110 | removeGCCRegisterPrefix(Name); |
| 111 | |
Anders Carlsson | 49dadd6 | 2007-11-25 00:25:21 +0000 | [diff] [blame] | 112 | |
| 113 | if (strcmp(Name, "memory") == 0 || |
| 114 | strcmp(Name, "cc") == 0) |
| 115 | return true; |
| 116 | |
Chris Lattner | fc45700 | 2008-03-05 01:18:20 +0000 | [diff] [blame^] | 117 | Target->getGCCRegNames(Names, NumNames); |
Anders Carlsson | 49dadd6 | 2007-11-25 00:25:21 +0000 | [diff] [blame] | 118 | |
| 119 | // If we have a number it maps to an entry in the register name array. |
| 120 | if (isdigit(Name[0])) { |
| 121 | char *End; |
| 122 | int n = (int)strtol(Name, &End, 0); |
| 123 | if (*End == 0) |
| 124 | return n >= 0 && (unsigned)n < NumNames; |
| 125 | } |
| 126 | |
| 127 | // Check register names. |
| 128 | for (unsigned i = 0; i < NumNames; i++) { |
| 129 | if (strcmp(Name, Names[i]) == 0) |
| 130 | return true; |
| 131 | } |
| 132 | |
| 133 | // Now check aliases. |
| 134 | const TargetInfoImpl::GCCRegAlias *Aliases; |
| 135 | unsigned NumAliases; |
| 136 | |
Chris Lattner | fc45700 | 2008-03-05 01:18:20 +0000 | [diff] [blame^] | 137 | Target->getGCCRegAliases(Aliases, NumAliases); |
Anders Carlsson | 49dadd6 | 2007-11-25 00:25:21 +0000 | [diff] [blame] | 138 | for (unsigned i = 0; i < NumAliases; i++) { |
| 139 | for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) { |
| 140 | if (!Aliases[i].Aliases[j]) |
| 141 | break; |
| 142 | if (strcmp(Aliases[i].Aliases[j], Name) == 0) |
| 143 | return true; |
| 144 | } |
| 145 | } |
| 146 | |
Anders Carlsson | 7dd1c95 | 2007-11-24 23:38:12 +0000 | [diff] [blame] | 147 | return false; |
| 148 | } |
Anders Carlsson | 4ce4230 | 2007-11-27 04:11:28 +0000 | [diff] [blame] | 149 | |
| 150 | const char *TargetInfo::getNormalizedGCCRegisterName(const char *Name) const |
| 151 | { |
| 152 | assert(isValidGCCRegisterName(Name) && "Invalid register passed in"); |
| 153 | |
Anders Carlsson | 7438598 | 2008-02-06 00:11:32 +0000 | [diff] [blame] | 154 | removeGCCRegisterPrefix(Name); |
Anders Carlsson | 3fb39b1 | 2008-02-05 23:30:20 +0000 | [diff] [blame] | 155 | |
Anders Carlsson | 4ce4230 | 2007-11-27 04:11:28 +0000 | [diff] [blame] | 156 | const char * const *Names; |
| 157 | unsigned NumNames; |
| 158 | |
Chris Lattner | fc45700 | 2008-03-05 01:18:20 +0000 | [diff] [blame^] | 159 | Target->getGCCRegNames(Names, NumNames); |
Anders Carlsson | 4ce4230 | 2007-11-27 04:11:28 +0000 | [diff] [blame] | 160 | |
| 161 | // First, check if we have a number. |
| 162 | if (isdigit(Name[0])) { |
| 163 | char *End; |
| 164 | int n = (int)strtol(Name, &End, 0); |
| 165 | if (*End == 0) { |
| 166 | assert(n >= 0 && (unsigned)n < NumNames && |
| 167 | "Out of bounds register number!"); |
| 168 | return Names[n]; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | // Now check aliases. |
| 173 | const TargetInfoImpl::GCCRegAlias *Aliases; |
| 174 | unsigned NumAliases; |
| 175 | |
Chris Lattner | fc45700 | 2008-03-05 01:18:20 +0000 | [diff] [blame^] | 176 | Target->getGCCRegAliases(Aliases, NumAliases); |
Anders Carlsson | 4ce4230 | 2007-11-27 04:11:28 +0000 | [diff] [blame] | 177 | for (unsigned i = 0; i < NumAliases; i++) { |
| 178 | for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) { |
| 179 | if (!Aliases[i].Aliases[j]) |
| 180 | break; |
| 181 | if (strcmp(Aliases[i].Aliases[j], Name) == 0) |
| 182 | return Aliases[i].Register; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | return Name; |
| 187 | } |
| 188 | |
| 189 | bool TargetInfo::validateOutputConstraint(const char *Name, |
| 190 | ConstraintInfo &info) const |
| 191 | { |
| 192 | // An output constraint must start with '=' or '+' |
| 193 | if (*Name != '=' && *Name != '+') |
| 194 | return false; |
| 195 | |
| 196 | if (*Name == '+') |
| 197 | info = CI_ReadWrite; |
| 198 | else |
| 199 | info = CI_None; |
| 200 | |
| 201 | Name++; |
| 202 | while (*Name) { |
| 203 | switch (*Name) { |
| 204 | default: |
Chris Lattner | fc45700 | 2008-03-05 01:18:20 +0000 | [diff] [blame^] | 205 | if (!Target->validateAsmConstraint(*Name, info)) { |
Anders Carlsson | 4ce4230 | 2007-11-27 04:11:28 +0000 | [diff] [blame] | 206 | // FIXME: This assert is in place temporarily |
| 207 | // so we can add more constraints as we hit it. |
| 208 | // Eventually, an unknown constraint should just be treated as 'g'. |
| 209 | assert(0 && "Unknown output constraint type!"); |
| 210 | } |
| 211 | case '&': // early clobber. |
| 212 | break; |
| 213 | case 'r': // general register. |
| 214 | info = (ConstraintInfo)(info|CI_AllowsRegister); |
| 215 | break; |
| 216 | case 'm': // memory operand. |
| 217 | info = (ConstraintInfo)(info|CI_AllowsMemory); |
| 218 | break; |
| 219 | case 'g': // general register, memory operand or immediate integer. |
| 220 | info = (ConstraintInfo)(info|CI_AllowsMemory|CI_AllowsRegister); |
| 221 | break; |
| 222 | } |
| 223 | |
| 224 | Name++; |
| 225 | } |
| 226 | |
| 227 | return true; |
| 228 | } |
| 229 | |
| 230 | bool TargetInfo::validateInputConstraint(const char *Name, |
| 231 | unsigned NumOutputs, |
| 232 | ConstraintInfo &info) const |
| 233 | { |
| 234 | while (*Name) { |
| 235 | switch (*Name) { |
| 236 | default: |
| 237 | // Check if we have a matching constraint |
| 238 | if (*Name >= '0' && *Name <= '9') { |
| 239 | unsigned i = *Name - '0'; |
| 240 | |
| 241 | // Check if matching constraint is out of bounds. |
| 242 | if (i >= NumOutputs) |
| 243 | return false; |
Chris Lattner | fc45700 | 2008-03-05 01:18:20 +0000 | [diff] [blame^] | 244 | } else if (!Target->validateAsmConstraint(*Name, info)) { |
Anders Carlsson | 4ce4230 | 2007-11-27 04:11:28 +0000 | [diff] [blame] | 245 | // FIXME: This assert is in place temporarily |
| 246 | // so we can add more constraints as we hit it. |
| 247 | // Eventually, an unknown constraint should just be treated as 'g'. |
| 248 | assert(0 && "Unknown input constraint type!"); |
| 249 | } |
Anders Carlsson | 333052c | 2007-12-08 19:32:57 +0000 | [diff] [blame] | 250 | case '%': // commutative |
| 251 | // FIXME: Fail if % is used with the last operand. |
| 252 | break; |
Anders Carlsson | 4ce4230 | 2007-11-27 04:11:28 +0000 | [diff] [blame] | 253 | case 'i': // immediate integer. |
Anders Carlsson | f8e50d9 | 2008-02-18 17:00:25 +0000 | [diff] [blame] | 254 | case 'I': |
Anders Carlsson | 4ce4230 | 2007-11-27 04:11:28 +0000 | [diff] [blame] | 255 | break; |
| 256 | case 'r': // general register. |
| 257 | info = (ConstraintInfo)(info|CI_AllowsRegister); |
| 258 | break; |
| 259 | case 'm': // memory operand. |
| 260 | info = (ConstraintInfo)(info|CI_AllowsMemory); |
| 261 | break; |
| 262 | case 'g': // general register, memory operand or immediate integer. |
| 263 | info = (ConstraintInfo)(info|CI_AllowsMemory|CI_AllowsRegister); |
| 264 | break; |
| 265 | } |
| 266 | |
| 267 | Name++; |
| 268 | } |
| 269 | |
| 270 | return true; |
| 271 | } |
| 272 | |
Lauro Ramos Venancio | bb37a04 | 2008-02-26 18:33:46 +0000 | [diff] [blame] | 273 | std::string TargetInfo::convertConstraint(const char Constraint) const { |
Chris Lattner | fc45700 | 2008-03-05 01:18:20 +0000 | [diff] [blame^] | 274 | return Target->convertConstraint(Constraint); |
Lauro Ramos Venancio | bb37a04 | 2008-02-26 18:33:46 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Chris Lattner | fc45700 | 2008-03-05 01:18:20 +0000 | [diff] [blame^] | 277 | const char *TargetInfo::getClobbers() const { |
| 278 | return Target->getClobbers(); |
Anders Carlsson | 4ce4230 | 2007-11-27 04:11:28 +0000 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | |