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/AddressSpaces.h" 16 #include "clang/Basic/CharInfo.h" 17 #include "clang/Basic/LangOptions.h" 18 #include "llvm/ADT/APFloat.h" 19 #include "llvm/ADT/STLExtras.h" 20 #include "llvm/Support/ErrorHandling.h" 21 #include <cstdlib> 22 using namespace clang; 23 24 static const LangAS::Map DefaultAddrSpaceMap = { 0 }; 25 26 // TargetInfo Constructor. 27 TargetInfo::TargetInfo(const llvm::Triple &T) : TargetOpts(), Triple(T) { 28 // Set defaults. Defaults are set for a 32-bit RISC platform, like PPC or 29 // SPARC. These should be overridden by concrete targets as needed. 30 BigEndian = true; 31 TLSSupported = true; 32 NoAsmVariants = false; 33 PointerWidth = PointerAlign = 32; 34 BoolWidth = BoolAlign = 8; 35 IntWidth = IntAlign = 32; 36 LongWidth = LongAlign = 32; 37 LongLongWidth = LongLongAlign = 64; 38 SuitableAlign = 64; 39 MinGlobalAlign = 0; 40 HalfWidth = 16; 41 HalfAlign = 16; 42 FloatWidth = 32; 43 FloatAlign = 32; 44 DoubleWidth = 64; 45 DoubleAlign = 64; 46 LongDoubleWidth = 64; 47 LongDoubleAlign = 64; 48 LargeArrayMinWidth = 0; 49 LargeArrayAlign = 0; 50 MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 0; 51 MaxVectorAlign = 0; 52 SizeType = UnsignedLong; 53 PtrDiffType = SignedLong; 54 IntMaxType = SignedLongLong; 55 UIntMaxType = UnsignedLongLong; 56 IntPtrType = SignedLong; 57 WCharType = SignedInt; 58 WIntType = SignedInt; 59 Char16Type = UnsignedShort; 60 Char32Type = UnsignedInt; 61 Int64Type = SignedLongLong; 62 SigAtomicType = SignedInt; 63 ProcessIDType = SignedInt; 64 UseSignedCharForObjCBool = true; 65 UseBitFieldTypeAlignment = true; 66 UseZeroLengthBitfieldAlignment = false; 67 ZeroLengthBitfieldBoundary = 0; 68 HalfFormat = &llvm::APFloat::IEEEhalf; 69 FloatFormat = &llvm::APFloat::IEEEsingle; 70 DoubleFormat = &llvm::APFloat::IEEEdouble; 71 LongDoubleFormat = &llvm::APFloat::IEEEdouble; 72 DescriptionString = "E-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-" 73 "i64:64:64-f32:32:32-f64:64:64-n32"; 74 UserLabelPrefix = "_"; 75 MCountName = "mcount"; 76 RegParmMax = 0; 77 SSERegParmMax = 0; 78 HasAlignMac68kSupport = false; 79 80 // Default to no types using fpret. 81 RealTypeUsesObjCFPRet = 0; 82 83 // Default to not using fp2ret for __Complex long double 84 ComplexLongDoubleUsesFP2Ret = false; 85 86 // Default to using the Itanium ABI. 87 TheCXXABI.set(TargetCXXABI::GenericItanium); 88 89 // Default to an empty address space map. 90 AddrSpaceMap = &DefaultAddrSpaceMap; 91 92 // Default to an unknown platform name. 93 PlatformName = "unknown"; 94 PlatformMinVersion = VersionTuple(); 95 } 96 97 // Out of line virtual dtor for TargetInfo. 98 TargetInfo::~TargetInfo() {} 99 100 /// getTypeName - Return the user string for the specified integer type enum. 101 /// For example, SignedShort -> "short". 102 const char *TargetInfo::getTypeName(IntType T) { 103 switch (T) { 104 default: llvm_unreachable("not an integer!"); 105 case SignedChar: return "char"; 106 case UnsignedChar: return "unsigned char"; 107 case SignedShort: return "short"; 108 case UnsignedShort: return "unsigned short"; 109 case SignedInt: return "int"; 110 case UnsignedInt: return "unsigned int"; 111 case SignedLong: return "long int"; 112 case UnsignedLong: return "long unsigned int"; 113 case SignedLongLong: return "long long int"; 114 case UnsignedLongLong: return "long long unsigned int"; 115 } 116 } 117 118 /// getTypeConstantSuffix - Return the constant suffix for the specified 119 /// integer type enum. For example, SignedLong -> "L". 120 const char *TargetInfo::getTypeConstantSuffix(IntType T) { 121 switch (T) { 122 default: llvm_unreachable("not an integer!"); 123 case SignedChar: 124 case SignedShort: 125 case SignedInt: return ""; 126 case SignedLong: return "L"; 127 case SignedLongLong: return "LL"; 128 case UnsignedChar: 129 case UnsignedShort: 130 case UnsignedInt: return "U"; 131 case UnsignedLong: return "UL"; 132 case UnsignedLongLong: return "ULL"; 133 } 134 } 135 136 /// getTypeWidth - Return the width (in bits) of the specified integer type 137 /// enum. For example, SignedInt -> getIntWidth(). 138 unsigned TargetInfo::getTypeWidth(IntType T) const { 139 switch (T) { 140 default: llvm_unreachable("not an integer!"); 141 case SignedChar: 142 case UnsignedChar: return getCharWidth(); 143 case SignedShort: 144 case UnsignedShort: return getShortWidth(); 145 case SignedInt: 146 case UnsignedInt: return getIntWidth(); 147 case SignedLong: 148 case UnsignedLong: return getLongWidth(); 149 case SignedLongLong: 150 case UnsignedLongLong: return getLongLongWidth(); 151 }; 152 } 153 154 TargetInfo::IntType TargetInfo::getIntTypeByWidth( 155 unsigned BitWidth, bool IsSigned) const { 156 if (getCharWidth() == BitWidth) 157 return IsSigned ? SignedChar : UnsignedChar; 158 if (getShortWidth() == BitWidth) 159 return IsSigned ? SignedShort : UnsignedShort; 160 if (getIntWidth() == BitWidth) 161 return IsSigned ? SignedInt : UnsignedInt; 162 if (getLongWidth() == BitWidth) 163 return IsSigned ? SignedLong : UnsignedLong; 164 if (getLongLongWidth() == BitWidth) 165 return IsSigned ? SignedLongLong : UnsignedLongLong; 166 return NoInt; 167 } 168 169 TargetInfo::RealType TargetInfo::getRealTypeByWidth(unsigned BitWidth) const { 170 if (getFloatWidth() == BitWidth) 171 return Float; 172 if (getDoubleWidth() == BitWidth) 173 return Double; 174 175 switch (BitWidth) { 176 case 96: 177 if (&getLongDoubleFormat() == &llvm::APFloat::x87DoubleExtended) 178 return LongDouble; 179 break; 180 case 128: 181 if (&getLongDoubleFormat() == &llvm::APFloat::PPCDoubleDouble) 182 return LongDouble; 183 break; 184 } 185 186 return NoFloat; 187 } 188 189 /// getTypeAlign - Return the alignment (in bits) of the specified integer type 190 /// enum. For example, SignedInt -> getIntAlign(). 191 unsigned TargetInfo::getTypeAlign(IntType T) const { 192 switch (T) { 193 default: llvm_unreachable("not an integer!"); 194 case SignedChar: 195 case UnsignedChar: return getCharAlign(); 196 case SignedShort: 197 case UnsignedShort: return getShortAlign(); 198 case SignedInt: 199 case UnsignedInt: return getIntAlign(); 200 case SignedLong: 201 case UnsignedLong: return getLongAlign(); 202 case SignedLongLong: 203 case UnsignedLongLong: return getLongLongAlign(); 204 }; 205 } 206 207 /// isTypeSigned - Return whether an integer types is signed. Returns true if 208 /// the type is signed; false otherwise. 209 bool TargetInfo::isTypeSigned(IntType T) { 210 switch (T) { 211 default: llvm_unreachable("not an integer!"); 212 case SignedChar: 213 case SignedShort: 214 case SignedInt: 215 case SignedLong: 216 case SignedLongLong: 217 return true; 218 case UnsignedChar: 219 case UnsignedShort: 220 case UnsignedInt: 221 case UnsignedLong: 222 case UnsignedLongLong: 223 return false; 224 }; 225 } 226 227 /// setForcedLangOptions - Set forced language options. 228 /// Apply changes to the target information with respect to certain 229 /// language options which change the target configuration. 230 void TargetInfo::setForcedLangOptions(LangOptions &Opts) { 231 if (Opts.NoBitFieldTypeAlign) 232 UseBitFieldTypeAlignment = false; 233 if (Opts.ShortWChar) 234 WCharType = UnsignedShort; 235 } 236 237 //===----------------------------------------------------------------------===// 238 239 240 static StringRef removeGCCRegisterPrefix(StringRef Name) { 241 if (Name[0] == '%' || Name[0] == '#') 242 Name = Name.substr(1); 243 244 return Name; 245 } 246 247 /// isValidClobber - Returns whether the passed in string is 248 /// a valid clobber in an inline asm statement. This is used by 249 /// Sema. 250 bool TargetInfo::isValidClobber(StringRef Name) const { 251 return (isValidGCCRegisterName(Name) || 252 Name == "memory" || Name == "cc"); 253 } 254 255 /// isValidGCCRegisterName - Returns whether the passed in string 256 /// is a valid register name according to GCC. This is used by Sema for 257 /// inline asm statements. 258 bool TargetInfo::isValidGCCRegisterName(StringRef Name) const { 259 if (Name.empty()) 260 return false; 261 262 const char * const *Names; 263 unsigned NumNames; 264 265 // Get rid of any register prefix. 266 Name = removeGCCRegisterPrefix(Name); 267 268 getGCCRegNames(Names, NumNames); 269 270 // If we have a number it maps to an entry in the register name array. 271 if (isDigit(Name[0])) { 272 int n; 273 if (!Name.getAsInteger(0, n)) 274 return n >= 0 && (unsigned)n < NumNames; 275 } 276 277 // Check register names. 278 for (unsigned i = 0; i < NumNames; i++) { 279 if (Name == Names[i]) 280 return true; 281 } 282 283 // Check any additional names that we have. 284 const AddlRegName *AddlNames; 285 unsigned NumAddlNames; 286 getGCCAddlRegNames(AddlNames, NumAddlNames); 287 for (unsigned i = 0; i < NumAddlNames; i++) 288 for (unsigned j = 0; j < llvm::array_lengthof(AddlNames[i].Names); j++) { 289 if (!AddlNames[i].Names[j]) 290 break; 291 // Make sure the register that the additional name is for is within 292 // the bounds of the register names from above. 293 if (AddlNames[i].Names[j] == Name && AddlNames[i].RegNum < NumNames) 294 return true; 295 } 296 297 // Now check aliases. 298 const GCCRegAlias *Aliases; 299 unsigned NumAliases; 300 301 getGCCRegAliases(Aliases, NumAliases); 302 for (unsigned i = 0; i < NumAliases; i++) { 303 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) { 304 if (!Aliases[i].Aliases[j]) 305 break; 306 if (Aliases[i].Aliases[j] == Name) 307 return true; 308 } 309 } 310 311 return false; 312 } 313 314 StringRef 315 TargetInfo::getNormalizedGCCRegisterName(StringRef Name) const { 316 assert(isValidGCCRegisterName(Name) && "Invalid register passed in"); 317 318 // Get rid of any register prefix. 319 Name = removeGCCRegisterPrefix(Name); 320 321 const char * const *Names; 322 unsigned NumNames; 323 324 getGCCRegNames(Names, NumNames); 325 326 // First, check if we have a number. 327 if (isDigit(Name[0])) { 328 int n; 329 if (!Name.getAsInteger(0, n)) { 330 assert(n >= 0 && (unsigned)n < NumNames && 331 "Out of bounds register number!"); 332 return Names[n]; 333 } 334 } 335 336 // Check any additional names that we have. 337 const AddlRegName *AddlNames; 338 unsigned NumAddlNames; 339 getGCCAddlRegNames(AddlNames, NumAddlNames); 340 for (unsigned i = 0; i < NumAddlNames; i++) 341 for (unsigned j = 0; j < llvm::array_lengthof(AddlNames[i].Names); j++) { 342 if (!AddlNames[i].Names[j]) 343 break; 344 // Make sure the register that the additional name is for is within 345 // the bounds of the register names from above. 346 if (AddlNames[i].Names[j] == Name && AddlNames[i].RegNum < NumNames) 347 return Name; 348 } 349 350 // Now check aliases. 351 const GCCRegAlias *Aliases; 352 unsigned NumAliases; 353 354 getGCCRegAliases(Aliases, NumAliases); 355 for (unsigned i = 0; i < NumAliases; i++) { 356 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) { 357 if (!Aliases[i].Aliases[j]) 358 break; 359 if (Aliases[i].Aliases[j] == Name) 360 return Aliases[i].Register; 361 } 362 } 363 364 return Name; 365 } 366 367 bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const { 368 const char *Name = Info.getConstraintStr().c_str(); 369 // An output constraint must start with '=' or '+' 370 if (*Name != '=' && *Name != '+') 371 return false; 372 373 if (*Name == '+') 374 Info.setIsReadWrite(); 375 376 Name++; 377 while (*Name) { 378 switch (*Name) { 379 default: 380 if (!validateAsmConstraint(Name, Info)) { 381 // FIXME: We temporarily return false 382 // so we can add more constraints as we hit it. 383 // Eventually, an unknown constraint should just be treated as 'g'. 384 return false; 385 } 386 case '&': // early clobber. 387 break; 388 case '%': // commutative. 389 // FIXME: Check that there is a another register after this one. 390 break; 391 case 'r': // general register. 392 Info.setAllowsRegister(); 393 break; 394 case 'm': // memory operand. 395 case 'o': // offsetable memory operand. 396 case 'V': // non-offsetable memory operand. 397 case '<': // autodecrement memory operand. 398 case '>': // autoincrement memory operand. 399 Info.setAllowsMemory(); 400 break; 401 case 'g': // general register, memory operand or immediate integer. 402 case 'X': // any operand. 403 Info.setAllowsRegister(); 404 Info.setAllowsMemory(); 405 break; 406 case ',': // multiple alternative constraint. Pass it. 407 // Handle additional optional '=' or '+' modifiers. 408 if (Name[1] == '=' || Name[1] == '+') 409 Name++; 410 break; 411 case '?': // Disparage slightly code. 412 case '!': // Disparage severely. 413 case '#': // Ignore as constraint. 414 case '*': // Ignore for choosing register preferences. 415 break; // Pass them. 416 } 417 418 Name++; 419 } 420 421 // If a constraint allows neither memory nor register operands it contains 422 // only modifiers. Reject it. 423 return Info.allowsMemory() || Info.allowsRegister(); 424 } 425 426 bool TargetInfo::resolveSymbolicName(const char *&Name, 427 ConstraintInfo *OutputConstraints, 428 unsigned NumOutputs, 429 unsigned &Index) const { 430 assert(*Name == '[' && "Symbolic name did not start with '['"); 431 Name++; 432 const char *Start = Name; 433 while (*Name && *Name != ']') 434 Name++; 435 436 if (!*Name) { 437 // Missing ']' 438 return false; 439 } 440 441 std::string SymbolicName(Start, Name - Start); 442 443 for (Index = 0; Index != NumOutputs; ++Index) 444 if (SymbolicName == OutputConstraints[Index].getName()) 445 return true; 446 447 return false; 448 } 449 450 bool TargetInfo::validateInputConstraint(ConstraintInfo *OutputConstraints, 451 unsigned NumOutputs, 452 ConstraintInfo &Info) const { 453 const char *Name = Info.ConstraintStr.c_str(); 454 455 while (*Name) { 456 switch (*Name) { 457 default: 458 // Check if we have a matching constraint 459 if (*Name >= '0' && *Name <= '9') { 460 unsigned i = *Name - '0'; 461 462 // Check if matching constraint is out of bounds. 463 if (i >= NumOutputs) 464 return false; 465 466 // A number must refer to an output only operand. 467 if (OutputConstraints[i].isReadWrite()) 468 return false; 469 470 // If the constraint is already tied, it must be tied to the 471 // same operand referenced to by the number. 472 if (Info.hasTiedOperand() && Info.getTiedOperand() != i) 473 return false; 474 475 // The constraint should have the same info as the respective 476 // output constraint. 477 Info.setTiedOperand(i, OutputConstraints[i]); 478 } else if (!validateAsmConstraint(Name, Info)) { 479 // FIXME: This error return is in place temporarily so we can 480 // add more constraints as we hit it. Eventually, an unknown 481 // constraint should just be treated as 'g'. 482 return false; 483 } 484 break; 485 case '[': { 486 unsigned Index = 0; 487 if (!resolveSymbolicName(Name, OutputConstraints, NumOutputs, Index)) 488 return false; 489 490 // If the constraint is already tied, it must be tied to the 491 // same operand referenced to by the number. 492 if (Info.hasTiedOperand() && Info.getTiedOperand() != Index) 493 return false; 494 495 Info.setTiedOperand(Index, OutputConstraints[Index]); 496 break; 497 } 498 case '%': // commutative 499 // FIXME: Fail if % is used with the last operand. 500 break; 501 case 'i': // immediate integer. 502 case 'n': // immediate integer with a known value. 503 break; 504 case 'I': // Various constant constraints with target-specific meanings. 505 case 'J': 506 case 'K': 507 case 'L': 508 case 'M': 509 case 'N': 510 case 'O': 511 case 'P': 512 break; 513 case 'r': // general register. 514 Info.setAllowsRegister(); 515 break; 516 case 'm': // memory operand. 517 case 'o': // offsettable memory operand. 518 case 'V': // non-offsettable memory operand. 519 case '<': // autodecrement memory operand. 520 case '>': // autoincrement memory operand. 521 Info.setAllowsMemory(); 522 break; 523 case 'g': // general register, memory operand or immediate integer. 524 case 'X': // any operand. 525 Info.setAllowsRegister(); 526 Info.setAllowsMemory(); 527 break; 528 case 'E': // immediate floating point. 529 case 'F': // immediate floating point. 530 case 'p': // address operand. 531 break; 532 case ',': // multiple alternative constraint. Ignore comma. 533 break; 534 case '?': // Disparage slightly code. 535 case '!': // Disparage severely. 536 case '#': // Ignore as constraint. 537 case '*': // Ignore for choosing register preferences. 538 break; // Pass them. 539 } 540 541 Name++; 542 } 543 544 return true; 545 } 546 547 bool TargetCXXABI::tryParse(llvm::StringRef name) { 548 const Kind unknown = static_cast<Kind>(-1); 549 Kind kind = llvm::StringSwitch<Kind>(name) 550 .Case("arm", GenericARM) 551 .Case("ios", iOS) 552 .Case("itanium", GenericItanium) 553 .Case("microsoft", Microsoft) 554 .Default(unknown); 555 if (kind == unknown) return false; 556 557 set(kind); 558 return true; 559 } 560