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