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