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