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