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