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