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 DefaultAlignForAttributeAligned = 128; 40 MinGlobalAlign = 0; 41 HalfWidth = 16; 42 HalfAlign = 16; 43 FloatWidth = 32; 44 FloatAlign = 32; 45 DoubleWidth = 64; 46 DoubleAlign = 64; 47 LongDoubleWidth = 64; 48 LongDoubleAlign = 64; 49 LargeArrayMinWidth = 0; 50 LargeArrayAlign = 0; 51 MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 0; 52 MaxVectorAlign = 0; 53 SimdDefaultAlign = 0; 54 SizeType = UnsignedLong; 55 PtrDiffType = SignedLong; 56 IntMaxType = SignedLongLong; 57 IntPtrType = SignedLong; 58 WCharType = SignedInt; 59 WIntType = SignedInt; 60 Char16Type = UnsignedShort; 61 Char32Type = UnsignedInt; 62 Int64Type = SignedLongLong; 63 SigAtomicType = SignedInt; 64 ProcessIDType = SignedInt; 65 UseSignedCharForObjCBool = true; 66 UseBitFieldTypeAlignment = true; 67 UseZeroLengthBitfieldAlignment = false; 68 ZeroLengthBitfieldBoundary = 0; 69 HalfFormat = &llvm::APFloat::IEEEhalf; 70 FloatFormat = &llvm::APFloat::IEEEsingle; 71 DoubleFormat = &llvm::APFloat::IEEEdouble; 72 LongDoubleFormat = &llvm::APFloat::IEEEdouble; 73 DescriptionString = nullptr; 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 // Set the C++ ABI based on the triple. 87 TheCXXABI.set(Triple.isKnownWindowsMSVCEnvironment() 88 ? TargetCXXABI::Microsoft 89 : TargetCXXABI::GenericItanium); 90 91 // Default to an empty address space map. 92 AddrSpaceMap = &DefaultAddrSpaceMap; 93 UseAddrSpaceMapMangling = false; 94 95 // Default to an unknown platform name. 96 PlatformName = "unknown"; 97 PlatformMinVersion = VersionTuple(); 98 } 99 100 // Out of line virtual dtor for TargetInfo. 101 TargetInfo::~TargetInfo() {} 102 103 /// getTypeName - Return the user string for the specified integer type enum. 104 /// For example, SignedShort -> "short". 105 const char *TargetInfo::getTypeName(IntType T) { 106 switch (T) { 107 default: llvm_unreachable("not an integer!"); 108 case SignedChar: return "signed char"; 109 case UnsignedChar: return "unsigned char"; 110 case SignedShort: return "short"; 111 case UnsignedShort: return "unsigned short"; 112 case SignedInt: return "int"; 113 case UnsignedInt: return "unsigned int"; 114 case SignedLong: return "long int"; 115 case UnsignedLong: return "long unsigned int"; 116 case SignedLongLong: return "long long int"; 117 case UnsignedLongLong: return "long long unsigned int"; 118 } 119 } 120 121 /// getTypeConstantSuffix - Return the constant suffix for the specified 122 /// integer type enum. For example, SignedLong -> "L". 123 const char *TargetInfo::getTypeConstantSuffix(IntType T) const { 124 switch (T) { 125 default: llvm_unreachable("not an integer!"); 126 case SignedChar: 127 case SignedShort: 128 case SignedInt: return ""; 129 case SignedLong: return "L"; 130 case SignedLongLong: return "LL"; 131 case UnsignedChar: 132 if (getCharWidth() < getIntWidth()) 133 return ""; 134 case UnsignedShort: 135 if (getShortWidth() < getIntWidth()) 136 return ""; 137 case UnsignedInt: return "U"; 138 case UnsignedLong: return "UL"; 139 case UnsignedLongLong: return "ULL"; 140 } 141 } 142 143 /// getTypeFormatModifier - Return the printf format modifier for the 144 /// specified integer type enum. For example, SignedLong -> "l". 145 146 const char *TargetInfo::getTypeFormatModifier(IntType T) { 147 switch (T) { 148 default: llvm_unreachable("not an integer!"); 149 case SignedChar: 150 case UnsignedChar: return "hh"; 151 case SignedShort: 152 case UnsignedShort: return "h"; 153 case SignedInt: 154 case UnsignedInt: return ""; 155 case SignedLong: 156 case UnsignedLong: return "l"; 157 case SignedLongLong: 158 case UnsignedLongLong: return "ll"; 159 } 160 } 161 162 /// getTypeWidth - Return the width (in bits) of the specified integer type 163 /// enum. For example, SignedInt -> getIntWidth(). 164 unsigned TargetInfo::getTypeWidth(IntType T) const { 165 switch (T) { 166 default: llvm_unreachable("not an integer!"); 167 case SignedChar: 168 case UnsignedChar: return getCharWidth(); 169 case SignedShort: 170 case UnsignedShort: return getShortWidth(); 171 case SignedInt: 172 case UnsignedInt: return getIntWidth(); 173 case SignedLong: 174 case UnsignedLong: return getLongWidth(); 175 case SignedLongLong: 176 case UnsignedLongLong: return getLongLongWidth(); 177 }; 178 } 179 180 TargetInfo::IntType TargetInfo::getIntTypeByWidth( 181 unsigned BitWidth, bool IsSigned) const { 182 if (getCharWidth() == BitWidth) 183 return IsSigned ? SignedChar : UnsignedChar; 184 if (getShortWidth() == BitWidth) 185 return IsSigned ? SignedShort : UnsignedShort; 186 if (getIntWidth() == BitWidth) 187 return IsSigned ? SignedInt : UnsignedInt; 188 if (getLongWidth() == BitWidth) 189 return IsSigned ? SignedLong : UnsignedLong; 190 if (getLongLongWidth() == BitWidth) 191 return IsSigned ? SignedLongLong : UnsignedLongLong; 192 return NoInt; 193 } 194 195 TargetInfo::IntType TargetInfo::getLeastIntTypeByWidth(unsigned BitWidth, 196 bool IsSigned) const { 197 if (getCharWidth() >= BitWidth) 198 return IsSigned ? SignedChar : UnsignedChar; 199 if (getShortWidth() >= BitWidth) 200 return IsSigned ? SignedShort : UnsignedShort; 201 if (getIntWidth() >= BitWidth) 202 return IsSigned ? SignedInt : UnsignedInt; 203 if (getLongWidth() >= BitWidth) 204 return IsSigned ? SignedLong : UnsignedLong; 205 if (getLongLongWidth() >= BitWidth) 206 return IsSigned ? SignedLongLong : UnsignedLongLong; 207 return NoInt; 208 } 209 210 TargetInfo::RealType TargetInfo::getRealTypeByWidth(unsigned BitWidth) const { 211 if (getFloatWidth() == BitWidth) 212 return Float; 213 if (getDoubleWidth() == BitWidth) 214 return Double; 215 216 switch (BitWidth) { 217 case 96: 218 if (&getLongDoubleFormat() == &llvm::APFloat::x87DoubleExtended) 219 return LongDouble; 220 break; 221 case 128: 222 if (&getLongDoubleFormat() == &llvm::APFloat::PPCDoubleDouble || 223 &getLongDoubleFormat() == &llvm::APFloat::IEEEquad) 224 return LongDouble; 225 break; 226 } 227 228 return NoFloat; 229 } 230 231 /// getTypeAlign - Return the alignment (in bits) of the specified integer type 232 /// enum. For example, SignedInt -> getIntAlign(). 233 unsigned TargetInfo::getTypeAlign(IntType T) const { 234 switch (T) { 235 default: llvm_unreachable("not an integer!"); 236 case SignedChar: 237 case UnsignedChar: return getCharAlign(); 238 case SignedShort: 239 case UnsignedShort: return getShortAlign(); 240 case SignedInt: 241 case UnsignedInt: return getIntAlign(); 242 case SignedLong: 243 case UnsignedLong: return getLongAlign(); 244 case SignedLongLong: 245 case UnsignedLongLong: return getLongLongAlign(); 246 }; 247 } 248 249 /// isTypeSigned - Return whether an integer types is signed. Returns true if 250 /// the type is signed; false otherwise. 251 bool TargetInfo::isTypeSigned(IntType T) { 252 switch (T) { 253 default: llvm_unreachable("not an integer!"); 254 case SignedChar: 255 case SignedShort: 256 case SignedInt: 257 case SignedLong: 258 case SignedLongLong: 259 return true; 260 case UnsignedChar: 261 case UnsignedShort: 262 case UnsignedInt: 263 case UnsignedLong: 264 case UnsignedLongLong: 265 return false; 266 }; 267 } 268 269 /// adjust - Set forced language options. 270 /// Apply changes to the target information with respect to certain 271 /// language options which change the target configuration. 272 void TargetInfo::adjust(const LangOptions &Opts) { 273 if (Opts.NoBitFieldTypeAlign) 274 UseBitFieldTypeAlignment = false; 275 if (Opts.ShortWChar) 276 WCharType = UnsignedShort; 277 278 if (Opts.OpenCL) { 279 // OpenCL C requires specific widths for types, irrespective of 280 // what these normally are for the target. 281 // We also define long long and long double here, although the 282 // OpenCL standard only mentions these as "reserved". 283 IntWidth = IntAlign = 32; 284 LongWidth = LongAlign = 64; 285 LongLongWidth = LongLongAlign = 128; 286 HalfWidth = HalfAlign = 16; 287 FloatWidth = FloatAlign = 32; 288 289 // Embedded 32-bit targets (OpenCL EP) might have double C type 290 // defined as float. Let's not override this as it might lead 291 // to generating illegal code that uses 64bit doubles. 292 if (DoubleWidth != FloatWidth) { 293 DoubleWidth = DoubleAlign = 64; 294 DoubleFormat = &llvm::APFloat::IEEEdouble; 295 } 296 LongDoubleWidth = LongDoubleAlign = 128; 297 298 assert(PointerWidth == 32 || PointerWidth == 64); 299 bool Is32BitArch = PointerWidth == 32; 300 SizeType = Is32BitArch ? UnsignedInt : UnsignedLong; 301 PtrDiffType = Is32BitArch ? SignedInt : SignedLong; 302 IntPtrType = Is32BitArch ? SignedInt : SignedLong; 303 304 IntMaxType = SignedLongLong; 305 Int64Type = SignedLong; 306 307 HalfFormat = &llvm::APFloat::IEEEhalf; 308 FloatFormat = &llvm::APFloat::IEEEsingle; 309 LongDoubleFormat = &llvm::APFloat::IEEEquad; 310 } 311 } 312 313 //===----------------------------------------------------------------------===// 314 315 316 static StringRef removeGCCRegisterPrefix(StringRef Name) { 317 if (Name[0] == '%' || Name[0] == '#') 318 Name = Name.substr(1); 319 320 return Name; 321 } 322 323 /// isValidClobber - Returns whether the passed in string is 324 /// a valid clobber in an inline asm statement. This is used by 325 /// Sema. 326 bool TargetInfo::isValidClobber(StringRef Name) const { 327 return (isValidGCCRegisterName(Name) || 328 Name == "memory" || Name == "cc"); 329 } 330 331 /// isValidGCCRegisterName - Returns whether the passed in string 332 /// is a valid register name according to GCC. This is used by Sema for 333 /// inline asm statements. 334 bool TargetInfo::isValidGCCRegisterName(StringRef Name) const { 335 if (Name.empty()) 336 return false; 337 338 const char * const *Names; 339 unsigned NumNames; 340 341 // Get rid of any register prefix. 342 Name = removeGCCRegisterPrefix(Name); 343 if (Name.empty()) 344 return false; 345 346 getGCCRegNames(Names, NumNames); 347 348 // If we have a number it maps to an entry in the register name array. 349 if (isDigit(Name[0])) { 350 int n; 351 if (!Name.getAsInteger(0, n)) 352 return n >= 0 && (unsigned)n < NumNames; 353 } 354 355 // Check register names. 356 for (unsigned i = 0; i < NumNames; i++) { 357 if (Name == Names[i]) 358 return true; 359 } 360 361 // Check any additional names that we have. 362 const AddlRegName *AddlNames; 363 unsigned NumAddlNames; 364 getGCCAddlRegNames(AddlNames, NumAddlNames); 365 for (unsigned i = 0; i < NumAddlNames; i++) 366 for (unsigned j = 0; j < llvm::array_lengthof(AddlNames[i].Names); j++) { 367 if (!AddlNames[i].Names[j]) 368 break; 369 // Make sure the register that the additional name is for is within 370 // the bounds of the register names from above. 371 if (AddlNames[i].Names[j] == Name && AddlNames[i].RegNum < NumNames) 372 return true; 373 } 374 375 // Now check aliases. 376 const GCCRegAlias *Aliases; 377 unsigned NumAliases; 378 379 getGCCRegAliases(Aliases, NumAliases); 380 for (unsigned i = 0; i < NumAliases; i++) { 381 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) { 382 if (!Aliases[i].Aliases[j]) 383 break; 384 if (Aliases[i].Aliases[j] == Name) 385 return true; 386 } 387 } 388 389 return false; 390 } 391 392 StringRef 393 TargetInfo::getNormalizedGCCRegisterName(StringRef Name) const { 394 assert(isValidGCCRegisterName(Name) && "Invalid register passed in"); 395 396 // Get rid of any register prefix. 397 Name = removeGCCRegisterPrefix(Name); 398 399 const char * const *Names; 400 unsigned NumNames; 401 402 getGCCRegNames(Names, NumNames); 403 404 // First, check if we have a number. 405 if (isDigit(Name[0])) { 406 int n; 407 if (!Name.getAsInteger(0, n)) { 408 assert(n >= 0 && (unsigned)n < NumNames && 409 "Out of bounds register number!"); 410 return Names[n]; 411 } 412 } 413 414 // Check any additional names that we have. 415 const AddlRegName *AddlNames; 416 unsigned NumAddlNames; 417 getGCCAddlRegNames(AddlNames, NumAddlNames); 418 for (unsigned i = 0; i < NumAddlNames; i++) 419 for (unsigned j = 0; j < llvm::array_lengthof(AddlNames[i].Names); j++) { 420 if (!AddlNames[i].Names[j]) 421 break; 422 // Make sure the register that the additional name is for is within 423 // the bounds of the register names from above. 424 if (AddlNames[i].Names[j] == Name && AddlNames[i].RegNum < NumNames) 425 return Name; 426 } 427 428 // Now check aliases. 429 const GCCRegAlias *Aliases; 430 unsigned NumAliases; 431 432 getGCCRegAliases(Aliases, NumAliases); 433 for (unsigned i = 0; i < NumAliases; i++) { 434 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) { 435 if (!Aliases[i].Aliases[j]) 436 break; 437 if (Aliases[i].Aliases[j] == Name) 438 return Aliases[i].Register; 439 } 440 } 441 442 return Name; 443 } 444 445 bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const { 446 const char *Name = Info.getConstraintStr().c_str(); 447 // An output constraint must start with '=' or '+' 448 if (*Name != '=' && *Name != '+') 449 return false; 450 451 if (*Name == '+') 452 Info.setIsReadWrite(); 453 454 Name++; 455 while (*Name) { 456 switch (*Name) { 457 default: 458 if (!validateAsmConstraint(Name, Info)) { 459 // FIXME: We temporarily return false 460 // so we can add more constraints as we hit it. 461 // Eventually, an unknown constraint should just be treated as 'g'. 462 return false; 463 } 464 break; 465 case '&': // early clobber. 466 Info.setEarlyClobber(); 467 break; 468 case '%': // commutative. 469 // FIXME: Check that there is a another register after this one. 470 break; 471 case 'r': // general register. 472 Info.setAllowsRegister(); 473 break; 474 case 'm': // memory operand. 475 case 'o': // offsetable memory operand. 476 case 'V': // non-offsetable memory operand. 477 case '<': // autodecrement memory operand. 478 case '>': // autoincrement memory operand. 479 Info.setAllowsMemory(); 480 break; 481 case 'g': // general register, memory operand or immediate integer. 482 case 'X': // any operand. 483 Info.setAllowsRegister(); 484 Info.setAllowsMemory(); 485 break; 486 case ',': // multiple alternative constraint. Pass it. 487 // Handle additional optional '=' or '+' modifiers. 488 if (Name[1] == '=' || Name[1] == '+') 489 Name++; 490 break; 491 case '#': // Ignore as constraint. 492 while (Name[1] && Name[1] != ',') 493 Name++; 494 break; 495 case '?': // Disparage slightly code. 496 case '!': // Disparage severely. 497 case '*': // Ignore for choosing register preferences. 498 break; // Pass them. 499 } 500 501 Name++; 502 } 503 504 // Early clobber with a read-write constraint which doesn't permit registers 505 // is invalid. 506 if (Info.earlyClobber() && Info.isReadWrite() && !Info.allowsRegister()) 507 return false; 508 509 // If a constraint allows neither memory nor register operands it contains 510 // only modifiers. Reject it. 511 return Info.allowsMemory() || Info.allowsRegister(); 512 } 513 514 bool TargetInfo::resolveSymbolicName(const char *&Name, 515 ConstraintInfo *OutputConstraints, 516 unsigned NumOutputs, 517 unsigned &Index) const { 518 assert(*Name == '[' && "Symbolic name did not start with '['"); 519 Name++; 520 const char *Start = Name; 521 while (*Name && *Name != ']') 522 Name++; 523 524 if (!*Name) { 525 // Missing ']' 526 return false; 527 } 528 529 std::string SymbolicName(Start, Name - Start); 530 531 for (Index = 0; Index != NumOutputs; ++Index) 532 if (SymbolicName == OutputConstraints[Index].getName()) 533 return true; 534 535 return false; 536 } 537 538 bool TargetInfo::validateInputConstraint(ConstraintInfo *OutputConstraints, 539 unsigned NumOutputs, 540 ConstraintInfo &Info) const { 541 const char *Name = Info.ConstraintStr.c_str(); 542 543 if (!*Name) 544 return false; 545 546 while (*Name) { 547 switch (*Name) { 548 default: 549 // Check if we have a matching constraint 550 if (*Name >= '0' && *Name <= '9') { 551 const char *DigitStart = Name; 552 while (Name[1] >= '0' && Name[1] <= '9') 553 Name++; 554 const char *DigitEnd = Name; 555 unsigned i; 556 if (StringRef(DigitStart, DigitEnd - DigitStart + 1) 557 .getAsInteger(10, i)) 558 return false; 559 560 // Check if matching constraint is out of bounds. 561 if (i >= NumOutputs) return false; 562 563 // A number must refer to an output only operand. 564 if (OutputConstraints[i].isReadWrite()) 565 return false; 566 567 // If the constraint is already tied, it must be tied to the 568 // same operand referenced to by the number. 569 if (Info.hasTiedOperand() && Info.getTiedOperand() != i) 570 return false; 571 572 // The constraint should have the same info as the respective 573 // output constraint. 574 Info.setTiedOperand(i, OutputConstraints[i]); 575 } else if (!validateAsmConstraint(Name, Info)) { 576 // FIXME: This error return is in place temporarily so we can 577 // add more constraints as we hit it. Eventually, an unknown 578 // constraint should just be treated as 'g'. 579 return false; 580 } 581 break; 582 case '[': { 583 unsigned Index = 0; 584 if (!resolveSymbolicName(Name, OutputConstraints, NumOutputs, Index)) 585 return false; 586 587 // If the constraint is already tied, it must be tied to the 588 // same operand referenced to by the number. 589 if (Info.hasTiedOperand() && Info.getTiedOperand() != Index) 590 return false; 591 592 // A number must refer to an output only operand. 593 if (OutputConstraints[Index].isReadWrite()) 594 return false; 595 596 Info.setTiedOperand(Index, OutputConstraints[Index]); 597 break; 598 } 599 case '%': // commutative 600 // FIXME: Fail if % is used with the last operand. 601 break; 602 case 'i': // immediate integer. 603 case 'n': // immediate integer with a known value. 604 break; 605 case 'I': // Various constant constraints with target-specific meanings. 606 case 'J': 607 case 'K': 608 case 'L': 609 case 'M': 610 case 'N': 611 case 'O': 612 case 'P': 613 if (!validateAsmConstraint(Name, Info)) 614 return false; 615 break; 616 case 'r': // general register. 617 Info.setAllowsRegister(); 618 break; 619 case 'm': // memory operand. 620 case 'o': // offsettable memory operand. 621 case 'V': // non-offsettable memory operand. 622 case '<': // autodecrement memory operand. 623 case '>': // autoincrement memory operand. 624 Info.setAllowsMemory(); 625 break; 626 case 'g': // general register, memory operand or immediate integer. 627 case 'X': // any operand. 628 Info.setAllowsRegister(); 629 Info.setAllowsMemory(); 630 break; 631 case 'E': // immediate floating point. 632 case 'F': // immediate floating point. 633 case 'p': // address operand. 634 break; 635 case ',': // multiple alternative constraint. Ignore comma. 636 break; 637 case '#': // Ignore as constraint. 638 while (Name[1] && Name[1] != ',') 639 Name++; 640 break; 641 case '?': // Disparage slightly code. 642 case '!': // Disparage severely. 643 case '*': // Ignore for choosing register preferences. 644 break; // Pass them. 645 } 646 647 Name++; 648 } 649 650 return true; 651 } 652 653 bool TargetCXXABI::tryParse(llvm::StringRef name) { 654 const Kind unknown = static_cast<Kind>(-1); 655 Kind kind = llvm::StringSwitch<Kind>(name) 656 .Case("arm", GenericARM) 657 .Case("ios", iOS) 658 .Case("itanium", GenericItanium) 659 .Case("microsoft", Microsoft) 660 .Case("mips", GenericMIPS) 661 .Default(unknown); 662 if (kind == unknown) return false; 663 664 set(kind); 665 return true; 666 } 667