Shih-wei Liao | f8fd82b | 2010-02-10 11:10:31 -0800 | [diff] [blame^] | 1 | //===--- TargetInfo.cpp - Information about Target machine ----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 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/LangOptions.h" |
| 16 | #include "llvm/ADT/APFloat.h" |
| 17 | #include "llvm/ADT/STLExtras.h" |
| 18 | #include <cstdlib> |
| 19 | using namespace clang; |
| 20 | |
| 21 | // TargetInfo Constructor. |
| 22 | TargetInfo::TargetInfo(const std::string &T) : Triple(T) { |
| 23 | // Set defaults. Defaults are set for a 32-bit RISC platform, |
| 24 | // like PPC or SPARC. |
| 25 | // These should be overridden by concrete targets as needed. |
| 26 | TLSSupported = true; |
| 27 | PointerWidth = PointerAlign = 32; |
| 28 | IntWidth = IntAlign = 32; |
| 29 | LongWidth = LongAlign = 32; |
| 30 | LongLongWidth = LongLongAlign = 64; |
| 31 | FloatWidth = 32; |
| 32 | FloatAlign = 32; |
| 33 | DoubleWidth = 64; |
| 34 | DoubleAlign = 64; |
| 35 | LongDoubleWidth = 64; |
| 36 | LongDoubleAlign = 64; |
| 37 | SizeType = UnsignedLong; |
| 38 | PtrDiffType = SignedLong; |
| 39 | IntMaxType = SignedLongLong; |
| 40 | UIntMaxType = UnsignedLongLong; |
| 41 | IntPtrType = SignedLong; |
| 42 | WCharType = SignedInt; |
| 43 | WIntType = SignedInt; |
| 44 | Char16Type = UnsignedShort; |
| 45 | Char32Type = UnsignedInt; |
| 46 | Int64Type = SignedLongLong; |
| 47 | SigAtomicType = SignedInt; |
| 48 | FloatFormat = &llvm::APFloat::IEEEsingle; |
| 49 | DoubleFormat = &llvm::APFloat::IEEEdouble; |
| 50 | LongDoubleFormat = &llvm::APFloat::IEEEdouble; |
| 51 | DescriptionString = "E-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-" |
| 52 | "i64:64:64-f32:32:32-f64:64:64-n32"; |
| 53 | UserLabelPrefix = "_"; |
| 54 | } |
| 55 | |
| 56 | // Out of line virtual dtor for TargetInfo. |
| 57 | TargetInfo::~TargetInfo() {} |
| 58 | |
| 59 | /// getTypeName - Return the user string for the specified integer type enum. |
| 60 | /// For example, SignedShort -> "short". |
| 61 | const char *TargetInfo::getTypeName(IntType T) { |
| 62 | switch (T) { |
| 63 | default: assert(0 && "not an integer!"); |
| 64 | case SignedShort: return "short"; |
| 65 | case UnsignedShort: return "unsigned short"; |
| 66 | case SignedInt: return "int"; |
| 67 | case UnsignedInt: return "unsigned int"; |
| 68 | case SignedLong: return "long int"; |
| 69 | case UnsignedLong: return "long unsigned int"; |
| 70 | case SignedLongLong: return "long long int"; |
| 71 | case UnsignedLongLong: return "long long unsigned int"; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | /// getTypeConstantSuffix - Return the constant suffix for the specified |
| 76 | /// integer type enum. For example, SignedLong -> "L". |
| 77 | const char *TargetInfo::getTypeConstantSuffix(IntType T) { |
| 78 | switch (T) { |
| 79 | default: assert(0 && "not an integer!"); |
| 80 | case SignedShort: |
| 81 | case SignedInt: return ""; |
| 82 | case SignedLong: return "L"; |
| 83 | case SignedLongLong: return "LL"; |
| 84 | case UnsignedShort: |
| 85 | case UnsignedInt: return "U"; |
| 86 | case UnsignedLong: return "UL"; |
| 87 | case UnsignedLongLong: return "ULL"; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | /// getTypeWidth - Return the width (in bits) of the specified integer type |
| 92 | /// enum. For example, SignedInt -> getIntWidth(). |
| 93 | unsigned TargetInfo::getTypeWidth(IntType T) const { |
| 94 | switch (T) { |
| 95 | default: assert(0 && "not an integer!"); |
| 96 | case SignedShort: |
| 97 | case UnsignedShort: return getShortWidth(); |
| 98 | case SignedInt: |
| 99 | case UnsignedInt: return getIntWidth(); |
| 100 | case SignedLong: |
| 101 | case UnsignedLong: return getLongWidth(); |
| 102 | case SignedLongLong: |
| 103 | case UnsignedLongLong: return getLongLongWidth(); |
| 104 | }; |
| 105 | } |
| 106 | |
| 107 | /// getTypeAlign - Return the alignment (in bits) of the specified integer type |
| 108 | /// enum. For example, SignedInt -> getIntAlign(). |
| 109 | unsigned TargetInfo::getTypeAlign(IntType T) const { |
| 110 | switch (T) { |
| 111 | default: assert(0 && "not an integer!"); |
| 112 | case SignedShort: |
| 113 | case UnsignedShort: return getShortAlign(); |
| 114 | case SignedInt: |
| 115 | case UnsignedInt: return getIntAlign(); |
| 116 | case SignedLong: |
| 117 | case UnsignedLong: return getLongAlign(); |
| 118 | case SignedLongLong: |
| 119 | case UnsignedLongLong: return getLongLongAlign(); |
| 120 | }; |
| 121 | } |
| 122 | |
| 123 | /// isTypeSigned - Return whether an integer types is signed. Returns true if |
| 124 | /// the type is signed; false otherwise. |
| 125 | bool TargetInfo::isTypeSigned(IntType T) const { |
| 126 | switch (T) { |
| 127 | default: assert(0 && "not an integer!"); |
| 128 | case SignedShort: |
| 129 | case SignedInt: |
| 130 | case SignedLong: |
| 131 | case SignedLongLong: |
| 132 | return true; |
| 133 | case UnsignedShort: |
| 134 | case UnsignedInt: |
| 135 | case UnsignedLong: |
| 136 | case UnsignedLongLong: |
| 137 | return false; |
| 138 | }; |
| 139 | } |
| 140 | |
| 141 | /// setForcedLangOptions - Set forced language options. |
| 142 | /// Apply changes to the target information with respect to certain |
| 143 | /// language options which change the target configuration. |
| 144 | void TargetInfo::setForcedLangOptions(LangOptions &Opts) { |
| 145 | if (Opts.ShortWChar) { |
| 146 | WCharType = UnsignedShort; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | //===----------------------------------------------------------------------===// |
| 151 | |
| 152 | |
| 153 | static llvm::StringRef removeGCCRegisterPrefix(llvm::StringRef Name) { |
| 154 | if (Name[0] == '%' || Name[0] == '#') |
| 155 | Name = Name.substr(1); |
| 156 | |
| 157 | return Name; |
| 158 | } |
| 159 | |
| 160 | /// isValidGCCRegisterName - Returns whether the passed in string |
| 161 | /// is a valid register name according to GCC. This is used by Sema for |
| 162 | /// inline asm statements. |
| 163 | bool TargetInfo::isValidGCCRegisterName(llvm::StringRef Name) const { |
| 164 | if (Name.empty()) |
| 165 | return false; |
| 166 | |
| 167 | const char * const *Names; |
| 168 | unsigned NumNames; |
| 169 | |
| 170 | // Get rid of any register prefix. |
| 171 | Name = removeGCCRegisterPrefix(Name); |
| 172 | |
| 173 | if (Name == "memory" || Name == "cc") |
| 174 | return true; |
| 175 | |
| 176 | getGCCRegNames(Names, NumNames); |
| 177 | |
| 178 | // If we have a number it maps to an entry in the register name array. |
| 179 | if (isdigit(Name[0])) { |
| 180 | int n; |
| 181 | if (!Name.getAsInteger(0, n)) |
| 182 | return n >= 0 && (unsigned)n < NumNames; |
| 183 | } |
| 184 | |
| 185 | // Check register names. |
| 186 | for (unsigned i = 0; i < NumNames; i++) { |
| 187 | if (Name == Names[i]) |
| 188 | return true; |
| 189 | } |
| 190 | |
| 191 | // Now check aliases. |
| 192 | const GCCRegAlias *Aliases; |
| 193 | unsigned NumAliases; |
| 194 | |
| 195 | getGCCRegAliases(Aliases, NumAliases); |
| 196 | for (unsigned i = 0; i < NumAliases; i++) { |
| 197 | for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) { |
| 198 | if (!Aliases[i].Aliases[j]) |
| 199 | break; |
| 200 | if (Aliases[i].Aliases[j] == Name) |
| 201 | return true; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | return false; |
| 206 | } |
| 207 | |
| 208 | llvm::StringRef |
| 209 | TargetInfo::getNormalizedGCCRegisterName(llvm::StringRef Name) const { |
| 210 | assert(isValidGCCRegisterName(Name) && "Invalid register passed in"); |
| 211 | |
| 212 | // Get rid of any register prefix. |
| 213 | Name = removeGCCRegisterPrefix(Name); |
| 214 | |
| 215 | const char * const *Names; |
| 216 | unsigned NumNames; |
| 217 | |
| 218 | getGCCRegNames(Names, NumNames); |
| 219 | |
| 220 | // First, check if we have a number. |
| 221 | if (isdigit(Name[0])) { |
| 222 | int n; |
| 223 | if (!Name.getAsInteger(0, n)) { |
| 224 | assert(n >= 0 && (unsigned)n < NumNames && |
| 225 | "Out of bounds register number!"); |
| 226 | return Names[n]; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | // Now check aliases. |
| 231 | const GCCRegAlias *Aliases; |
| 232 | unsigned NumAliases; |
| 233 | |
| 234 | getGCCRegAliases(Aliases, NumAliases); |
| 235 | for (unsigned i = 0; i < NumAliases; i++) { |
| 236 | for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) { |
| 237 | if (!Aliases[i].Aliases[j]) |
| 238 | break; |
| 239 | if (Aliases[i].Aliases[j] == Name) |
| 240 | return Aliases[i].Register; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | return Name; |
| 245 | } |
| 246 | |
| 247 | bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const { |
| 248 | const char *Name = Info.getConstraintStr().c_str(); |
| 249 | // An output constraint must start with '=' or '+' |
| 250 | if (*Name != '=' && *Name != '+') |
| 251 | return false; |
| 252 | |
| 253 | if (*Name == '+') |
| 254 | Info.setIsReadWrite(); |
| 255 | |
| 256 | Name++; |
| 257 | while (*Name) { |
| 258 | switch (*Name) { |
| 259 | default: |
| 260 | if (!validateAsmConstraint(Name, Info)) { |
| 261 | // FIXME: We temporarily return false |
| 262 | // so we can add more constraints as we hit it. |
| 263 | // Eventually, an unknown constraint should just be treated as 'g'. |
| 264 | return false; |
| 265 | } |
| 266 | case '&': // early clobber. |
| 267 | break; |
| 268 | case '%': // commutative. |
| 269 | // FIXME: Check that there is a another register after this one. |
| 270 | break; |
| 271 | case 'r': // general register. |
| 272 | Info.setAllowsRegister(); |
| 273 | break; |
| 274 | case 'm': // memory operand. |
| 275 | Info.setAllowsMemory(); |
| 276 | break; |
| 277 | case 'g': // general register, memory operand or immediate integer. |
| 278 | case 'X': // any operand. |
| 279 | Info.setAllowsRegister(); |
| 280 | Info.setAllowsMemory(); |
| 281 | break; |
| 282 | } |
| 283 | |
| 284 | Name++; |
| 285 | } |
| 286 | |
| 287 | return true; |
| 288 | } |
| 289 | |
| 290 | bool TargetInfo::resolveSymbolicName(const char *&Name, |
| 291 | ConstraintInfo *OutputConstraints, |
| 292 | unsigned NumOutputs, |
| 293 | unsigned &Index) const { |
| 294 | assert(*Name == '[' && "Symbolic name did not start with '['"); |
| 295 | Name++; |
| 296 | const char *Start = Name; |
| 297 | while (*Name && *Name != ']') |
| 298 | Name++; |
| 299 | |
| 300 | if (!*Name) { |
| 301 | // Missing ']' |
| 302 | return false; |
| 303 | } |
| 304 | |
| 305 | std::string SymbolicName(Start, Name - Start); |
| 306 | |
| 307 | for (Index = 0; Index != NumOutputs; ++Index) |
| 308 | if (SymbolicName == OutputConstraints[Index].getName()) |
| 309 | return true; |
| 310 | |
| 311 | return false; |
| 312 | } |
| 313 | |
| 314 | bool TargetInfo::validateInputConstraint(ConstraintInfo *OutputConstraints, |
| 315 | unsigned NumOutputs, |
| 316 | ConstraintInfo &Info) const { |
| 317 | const char *Name = Info.ConstraintStr.c_str(); |
| 318 | |
| 319 | while (*Name) { |
| 320 | switch (*Name) { |
| 321 | default: |
| 322 | // Check if we have a matching constraint |
| 323 | if (*Name >= '0' && *Name <= '9') { |
| 324 | unsigned i = *Name - '0'; |
| 325 | |
| 326 | // Check if matching constraint is out of bounds. |
| 327 | if (i >= NumOutputs) |
| 328 | return false; |
| 329 | |
| 330 | // The constraint should have the same info as the respective |
| 331 | // output constraint. |
| 332 | Info.setTiedOperand(i, OutputConstraints[i]); |
| 333 | } else if (!validateAsmConstraint(Name, Info)) { |
| 334 | // FIXME: This error return is in place temporarily so we can |
| 335 | // add more constraints as we hit it. Eventually, an unknown |
| 336 | // constraint should just be treated as 'g'. |
| 337 | return false; |
| 338 | } |
| 339 | break; |
| 340 | case '[': { |
| 341 | unsigned Index = 0; |
| 342 | if (!resolveSymbolicName(Name, OutputConstraints, NumOutputs, Index)) |
| 343 | return false; |
| 344 | |
| 345 | break; |
| 346 | } |
| 347 | case '%': // commutative |
| 348 | // FIXME: Fail if % is used with the last operand. |
| 349 | break; |
| 350 | case 'i': // immediate integer. |
| 351 | case 'n': // immediate integer with a known value. |
| 352 | break; |
| 353 | case 'I': // Various constant constraints with target-specific meanings. |
| 354 | case 'J': |
| 355 | case 'K': |
| 356 | case 'L': |
| 357 | case 'M': |
| 358 | case 'N': |
| 359 | case 'O': |
| 360 | case 'P': |
| 361 | break; |
| 362 | case 'r': // general register. |
| 363 | Info.setAllowsRegister(); |
| 364 | break; |
| 365 | case 'm': // memory operand. |
| 366 | case 'o': // offsettable memory operand |
| 367 | case 'V': // non-offsettable memory operand |
| 368 | Info.setAllowsMemory(); |
| 369 | break; |
| 370 | case 'g': // general register, memory operand or immediate integer. |
| 371 | case 'X': // any operand. |
| 372 | Info.setAllowsRegister(); |
| 373 | Info.setAllowsMemory(); |
| 374 | break; |
| 375 | } |
| 376 | |
| 377 | Name++; |
| 378 | } |
| 379 | |
| 380 | return true; |
| 381 | } |