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