1 //===--- TargetInfo.cpp - Information about Target machine ----------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements the TargetInfo and TargetInfoImpl interfaces. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "clang/Basic/TargetInfo.h" 14 #include "clang/Basic/AddressSpaces.h" 15 #include "clang/Basic/CharInfo.h" 16 #include "clang/Basic/Diagnostic.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 "llvm/Support/TargetParser.h" 22 #include <cstdlib> 23 using namespace clang; 24 25 static const LangASMap DefaultAddrSpaceMap = {0}; 26 27 // TargetInfo Constructor. 28 TargetInfo::TargetInfo(const llvm::Triple &T) : TargetOpts(), Triple(T) { 29 // Set defaults. Defaults are set for a 32-bit RISC platform, like PPC or 30 // SPARC. These should be overridden by concrete targets as needed. 31 BigEndian = !T.isLittleEndian(); 32 TLSSupported = true; 33 VLASupported = true; 34 NoAsmVariants = false; 35 HasLegalHalfType = false; 36 HasFloat128 = false; 37 HasIbm128 = false; 38 HasFloat16 = false; 39 HasBFloat16 = false; 40 HasLongDouble = true; 41 HasFPReturn = true; 42 HasStrictFP = false; 43 PointerWidth = PointerAlign = 32; 44 BoolWidth = BoolAlign = 8; 45 IntWidth = IntAlign = 32; 46 LongWidth = LongAlign = 32; 47 LongLongWidth = LongLongAlign = 64; 48 49 // Fixed point default bit widths 50 ShortAccumWidth = ShortAccumAlign = 16; 51 AccumWidth = AccumAlign = 32; 52 LongAccumWidth = LongAccumAlign = 64; 53 ShortFractWidth = ShortFractAlign = 8; 54 FractWidth = FractAlign = 16; 55 LongFractWidth = LongFractAlign = 32; 56 57 // Fixed point default integral and fractional bit sizes 58 // We give the _Accum 1 fewer fractional bits than their corresponding _Fract 59 // types by default to have the same number of fractional bits between _Accum 60 // and _Fract types. 61 PaddingOnUnsignedFixedPoint = false; 62 ShortAccumScale = 7; 63 AccumScale = 15; 64 LongAccumScale = 31; 65 66 SuitableAlign = 64; 67 DefaultAlignForAttributeAligned = 128; 68 MinGlobalAlign = 0; 69 // From the glibc documentation, on GNU systems, malloc guarantees 16-byte 70 // alignment on 64-bit systems and 8-byte alignment on 32-bit systems. See 71 // https://www.gnu.org/software/libc/manual/html_node/Malloc-Examples.html. 72 // This alignment guarantee also applies to Windows and Android. On Darwin, 73 // the alignment is 16 bytes on both 64-bit and 32-bit systems. 74 if (T.isGNUEnvironment() || T.isWindowsMSVCEnvironment() || T.isAndroid()) 75 NewAlign = Triple.isArch64Bit() ? 128 : Triple.isArch32Bit() ? 64 : 0; 76 else if (T.isOSDarwin()) 77 NewAlign = 128; 78 else 79 NewAlign = 0; // Infer from basic type alignment. 80 HalfWidth = 16; 81 HalfAlign = 16; 82 FloatWidth = 32; 83 FloatAlign = 32; 84 DoubleWidth = 64; 85 DoubleAlign = 64; 86 LongDoubleWidth = 64; 87 LongDoubleAlign = 64; 88 Float128Align = 128; 89 Ibm128Align = 128; 90 LargeArrayMinWidth = 0; 91 LargeArrayAlign = 0; 92 MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 0; 93 MaxVectorAlign = 0; 94 MaxTLSAlign = 0; 95 SimdDefaultAlign = 0; 96 SizeType = UnsignedLong; 97 PtrDiffType = SignedLong; 98 IntMaxType = SignedLongLong; 99 IntPtrType = SignedLong; 100 WCharType = SignedInt; 101 WIntType = SignedInt; 102 Char16Type = UnsignedShort; 103 Char32Type = UnsignedInt; 104 Int64Type = SignedLongLong; 105 Int16Type = SignedShort; 106 SigAtomicType = SignedInt; 107 ProcessIDType = SignedInt; 108 UseSignedCharForObjCBool = true; 109 UseBitFieldTypeAlignment = true; 110 UseZeroLengthBitfieldAlignment = false; 111 UseLeadingZeroLengthBitfield = true; 112 UseExplicitBitFieldAlignment = true; 113 ZeroLengthBitfieldBoundary = 0; 114 MaxAlignedAttribute = 0; 115 HalfFormat = &llvm::APFloat::IEEEhalf(); 116 FloatFormat = &llvm::APFloat::IEEEsingle(); 117 DoubleFormat = &llvm::APFloat::IEEEdouble(); 118 LongDoubleFormat = &llvm::APFloat::IEEEdouble(); 119 Float128Format = &llvm::APFloat::IEEEquad(); 120 Ibm128Format = &llvm::APFloat::PPCDoubleDouble(); 121 MCountName = "mcount"; 122 UserLabelPrefix = "_"; 123 RegParmMax = 0; 124 SSERegParmMax = 0; 125 HasAlignMac68kSupport = false; 126 HasBuiltinMSVaList = false; 127 IsRenderScriptTarget = false; 128 HasAArch64SVETypes = false; 129 HasRISCVVTypes = false; 130 AllowAMDGPUUnsafeFPAtomics = false; 131 ARMCDECoprocMask = 0; 132 133 // Default to no types using fpret. 134 RealTypeUsesObjCFPRet = 0; 135 136 // Default to not using fp2ret for __Complex long double 137 ComplexLongDoubleUsesFP2Ret = false; 138 139 // Set the C++ ABI based on the triple. 140 TheCXXABI.set(Triple.isKnownWindowsMSVCEnvironment() 141 ? TargetCXXABI::Microsoft 142 : TargetCXXABI::GenericItanium); 143 144 // Default to an empty address space map. 145 AddrSpaceMap = &DefaultAddrSpaceMap; 146 UseAddrSpaceMapMangling = false; 147 148 // Default to an unknown platform name. 149 PlatformName = "unknown"; 150 PlatformMinVersion = VersionTuple(); 151 152 MaxOpenCLWorkGroupSize = 1024; 153 } 154 155 // Out of line virtual dtor for TargetInfo. 156 TargetInfo::~TargetInfo() {} 157 158 void TargetInfo::resetDataLayout(StringRef DL, const char *ULP) { 159 DataLayoutString = DL.str(); 160 UserLabelPrefix = ULP; 161 } 162 163 bool 164 TargetInfo::checkCFProtectionBranchSupported(DiagnosticsEngine &Diags) const { 165 Diags.Report(diag::err_opt_not_valid_on_target) << "cf-protection=branch"; 166 return false; 167 } 168 169 bool 170 TargetInfo::checkCFProtectionReturnSupported(DiagnosticsEngine &Diags) const { 171 Diags.Report(diag::err_opt_not_valid_on_target) << "cf-protection=return"; 172 return false; 173 } 174 175 /// getTypeName - Return the user string for the specified integer type enum. 176 /// For example, SignedShort -> "short". 177 const char *TargetInfo::getTypeName(IntType T) { 178 switch (T) { 179 default: llvm_unreachable("not an integer!"); 180 case SignedChar: return "signed char"; 181 case UnsignedChar: return "unsigned char"; 182 case SignedShort: return "short"; 183 case UnsignedShort: return "unsigned short"; 184 case SignedInt: return "int"; 185 case UnsignedInt: return "unsigned int"; 186 case SignedLong: return "long int"; 187 case UnsignedLong: return "long unsigned int"; 188 case SignedLongLong: return "long long int"; 189 case UnsignedLongLong: return "long long unsigned int"; 190 } 191 } 192 193 /// getTypeConstantSuffix - Return the constant suffix for the specified 194 /// integer type enum. For example, SignedLong -> "L". 195 const char *TargetInfo::getTypeConstantSuffix(IntType T) const { 196 switch (T) { 197 default: llvm_unreachable("not an integer!"); 198 case SignedChar: 199 case SignedShort: 200 case SignedInt: return ""; 201 case SignedLong: return "L"; 202 case SignedLongLong: return "LL"; 203 case UnsignedChar: 204 if (getCharWidth() < getIntWidth()) 205 return ""; 206 LLVM_FALLTHROUGH; 207 case UnsignedShort: 208 if (getShortWidth() < getIntWidth()) 209 return ""; 210 LLVM_FALLTHROUGH; 211 case UnsignedInt: return "U"; 212 case UnsignedLong: return "UL"; 213 case UnsignedLongLong: return "ULL"; 214 } 215 } 216 217 /// getTypeFormatModifier - Return the printf format modifier for the 218 /// specified integer type enum. For example, SignedLong -> "l". 219 220 const char *TargetInfo::getTypeFormatModifier(IntType T) { 221 switch (T) { 222 default: llvm_unreachable("not an integer!"); 223 case SignedChar: 224 case UnsignedChar: return "hh"; 225 case SignedShort: 226 case UnsignedShort: return "h"; 227 case SignedInt: 228 case UnsignedInt: return ""; 229 case SignedLong: 230 case UnsignedLong: return "l"; 231 case SignedLongLong: 232 case UnsignedLongLong: return "ll"; 233 } 234 } 235 236 /// getTypeWidth - Return the width (in bits) of the specified integer type 237 /// enum. For example, SignedInt -> getIntWidth(). 238 unsigned TargetInfo::getTypeWidth(IntType T) const { 239 switch (T) { 240 default: llvm_unreachable("not an integer!"); 241 case SignedChar: 242 case UnsignedChar: return getCharWidth(); 243 case SignedShort: 244 case UnsignedShort: return getShortWidth(); 245 case SignedInt: 246 case UnsignedInt: return getIntWidth(); 247 case SignedLong: 248 case UnsignedLong: return getLongWidth(); 249 case SignedLongLong: 250 case UnsignedLongLong: return getLongLongWidth(); 251 }; 252 } 253 254 TargetInfo::IntType TargetInfo::getIntTypeByWidth( 255 unsigned BitWidth, bool IsSigned) const { 256 if (getCharWidth() == BitWidth) 257 return IsSigned ? SignedChar : UnsignedChar; 258 if (getShortWidth() == BitWidth) 259 return IsSigned ? SignedShort : UnsignedShort; 260 if (getIntWidth() == BitWidth) 261 return IsSigned ? SignedInt : UnsignedInt; 262 if (getLongWidth() == BitWidth) 263 return IsSigned ? SignedLong : UnsignedLong; 264 if (getLongLongWidth() == BitWidth) 265 return IsSigned ? SignedLongLong : UnsignedLongLong; 266 return NoInt; 267 } 268 269 TargetInfo::IntType TargetInfo::getLeastIntTypeByWidth(unsigned BitWidth, 270 bool IsSigned) const { 271 if (getCharWidth() >= BitWidth) 272 return IsSigned ? SignedChar : UnsignedChar; 273 if (getShortWidth() >= BitWidth) 274 return IsSigned ? SignedShort : UnsignedShort; 275 if (getIntWidth() >= BitWidth) 276 return IsSigned ? SignedInt : UnsignedInt; 277 if (getLongWidth() >= BitWidth) 278 return IsSigned ? SignedLong : UnsignedLong; 279 if (getLongLongWidth() >= BitWidth) 280 return IsSigned ? SignedLongLong : UnsignedLongLong; 281 return NoInt; 282 } 283 284 FloatModeKind TargetInfo::getRealTypeByWidth(unsigned BitWidth, 285 FloatModeKind ExplicitType) const { 286 if (getFloatWidth() == BitWidth) 287 return FloatModeKind::Float; 288 if (getDoubleWidth() == BitWidth) 289 return FloatModeKind::Double; 290 291 switch (BitWidth) { 292 case 96: 293 if (&getLongDoubleFormat() == &llvm::APFloat::x87DoubleExtended()) 294 return FloatModeKind::LongDouble; 295 break; 296 case 128: 297 // The caller explicitly asked for an IEEE compliant type but we still 298 // have to check if the target supports it. 299 if (ExplicitType == FloatModeKind::Float128) 300 return hasFloat128Type() ? FloatModeKind::Float128 301 : FloatModeKind::NoFloat; 302 if (ExplicitType == FloatModeKind::Ibm128) 303 return hasIbm128Type() ? FloatModeKind::Ibm128 304 : FloatModeKind::NoFloat; 305 if (&getLongDoubleFormat() == &llvm::APFloat::PPCDoubleDouble() || 306 &getLongDoubleFormat() == &llvm::APFloat::IEEEquad()) 307 return FloatModeKind::LongDouble; 308 if (hasFloat128Type()) 309 return FloatModeKind::Float128; 310 break; 311 } 312 313 return FloatModeKind::NoFloat; 314 } 315 316 /// getTypeAlign - Return the alignment (in bits) of the specified integer type 317 /// enum. For example, SignedInt -> getIntAlign(). 318 unsigned TargetInfo::getTypeAlign(IntType T) const { 319 switch (T) { 320 default: llvm_unreachable("not an integer!"); 321 case SignedChar: 322 case UnsignedChar: return getCharAlign(); 323 case SignedShort: 324 case UnsignedShort: return getShortAlign(); 325 case SignedInt: 326 case UnsignedInt: return getIntAlign(); 327 case SignedLong: 328 case UnsignedLong: return getLongAlign(); 329 case SignedLongLong: 330 case UnsignedLongLong: return getLongLongAlign(); 331 }; 332 } 333 334 /// isTypeSigned - Return whether an integer types is signed. Returns true if 335 /// the type is signed; false otherwise. 336 bool TargetInfo::isTypeSigned(IntType T) { 337 switch (T) { 338 default: llvm_unreachable("not an integer!"); 339 case SignedChar: 340 case SignedShort: 341 case SignedInt: 342 case SignedLong: 343 case SignedLongLong: 344 return true; 345 case UnsignedChar: 346 case UnsignedShort: 347 case UnsignedInt: 348 case UnsignedLong: 349 case UnsignedLongLong: 350 return false; 351 }; 352 } 353 354 /// adjust - Set forced language options. 355 /// Apply changes to the target information with respect to certain 356 /// language options which change the target configuration and adjust 357 /// the language based on the target options where applicable. 358 void TargetInfo::adjust(DiagnosticsEngine &Diags, LangOptions &Opts) { 359 if (Opts.NoBitFieldTypeAlign) 360 UseBitFieldTypeAlignment = false; 361 362 switch (Opts.WCharSize) { 363 default: llvm_unreachable("invalid wchar_t width"); 364 case 0: break; 365 case 1: WCharType = Opts.WCharIsSigned ? SignedChar : UnsignedChar; break; 366 case 2: WCharType = Opts.WCharIsSigned ? SignedShort : UnsignedShort; break; 367 case 4: WCharType = Opts.WCharIsSigned ? SignedInt : UnsignedInt; break; 368 } 369 370 if (Opts.AlignDouble) { 371 DoubleAlign = LongLongAlign = 64; 372 LongDoubleAlign = 64; 373 } 374 375 if (Opts.OpenCL) { 376 // OpenCL C requires specific widths for types, irrespective of 377 // what these normally are for the target. 378 // We also define long long and long double here, although the 379 // OpenCL standard only mentions these as "reserved". 380 IntWidth = IntAlign = 32; 381 LongWidth = LongAlign = 64; 382 LongLongWidth = LongLongAlign = 128; 383 HalfWidth = HalfAlign = 16; 384 FloatWidth = FloatAlign = 32; 385 386 // Embedded 32-bit targets (OpenCL EP) might have double C type 387 // defined as float. Let's not override this as it might lead 388 // to generating illegal code that uses 64bit doubles. 389 if (DoubleWidth != FloatWidth) { 390 DoubleWidth = DoubleAlign = 64; 391 DoubleFormat = &llvm::APFloat::IEEEdouble(); 392 } 393 LongDoubleWidth = LongDoubleAlign = 128; 394 395 unsigned MaxPointerWidth = getMaxPointerWidth(); 396 assert(MaxPointerWidth == 32 || MaxPointerWidth == 64); 397 bool Is32BitArch = MaxPointerWidth == 32; 398 SizeType = Is32BitArch ? UnsignedInt : UnsignedLong; 399 PtrDiffType = Is32BitArch ? SignedInt : SignedLong; 400 IntPtrType = Is32BitArch ? SignedInt : SignedLong; 401 402 IntMaxType = SignedLongLong; 403 Int64Type = SignedLong; 404 405 HalfFormat = &llvm::APFloat::IEEEhalf(); 406 FloatFormat = &llvm::APFloat::IEEEsingle(); 407 LongDoubleFormat = &llvm::APFloat::IEEEquad(); 408 409 // OpenCL C v3.0 s6.7.5 - The generic address space requires support for 410 // OpenCL C 2.0 or OpenCL C 3.0 with the __opencl_c_generic_address_space 411 // feature 412 // OpenCL C v3.0 s6.2.1 - OpenCL pipes require support of OpenCL C 2.0 413 // or later and __opencl_c_pipes feature 414 // FIXME: These language options are also defined in setLangDefaults() 415 // for OpenCL C 2.0 but with no access to target capabilities. Target 416 // should be immutable once created and thus these language options need 417 // to be defined only once. 418 if (Opts.getOpenCLCompatibleVersion() == 300) { 419 const auto &OpenCLFeaturesMap = getSupportedOpenCLOpts(); 420 Opts.OpenCLGenericAddressSpace = hasFeatureEnabled( 421 OpenCLFeaturesMap, "__opencl_c_generic_address_space"); 422 Opts.OpenCLPipes = 423 hasFeatureEnabled(OpenCLFeaturesMap, "__opencl_c_pipes"); 424 } 425 } 426 427 if (Opts.DoubleSize) { 428 if (Opts.DoubleSize == 32) { 429 DoubleWidth = 32; 430 LongDoubleWidth = 32; 431 DoubleFormat = &llvm::APFloat::IEEEsingle(); 432 LongDoubleFormat = &llvm::APFloat::IEEEsingle(); 433 } else if (Opts.DoubleSize == 64) { 434 DoubleWidth = 64; 435 LongDoubleWidth = 64; 436 DoubleFormat = &llvm::APFloat::IEEEdouble(); 437 LongDoubleFormat = &llvm::APFloat::IEEEdouble(); 438 } 439 } 440 441 if (Opts.LongDoubleSize) { 442 if (Opts.LongDoubleSize == DoubleWidth) { 443 LongDoubleWidth = DoubleWidth; 444 LongDoubleAlign = DoubleAlign; 445 LongDoubleFormat = DoubleFormat; 446 } else if (Opts.LongDoubleSize == 128) { 447 LongDoubleWidth = LongDoubleAlign = 128; 448 LongDoubleFormat = &llvm::APFloat::IEEEquad(); 449 } 450 } 451 452 if (Opts.NewAlignOverride) 453 NewAlign = Opts.NewAlignOverride * getCharWidth(); 454 455 // Each unsigned fixed point type has the same number of fractional bits as 456 // its corresponding signed type. 457 PaddingOnUnsignedFixedPoint |= Opts.PaddingOnUnsignedFixedPoint; 458 CheckFixedPointBits(); 459 460 if (Opts.ProtectParens && !checkArithmeticFenceSupported()) { 461 Diags.Report(diag::err_opt_not_valid_on_target) << "-fprotect-parens"; 462 Opts.ProtectParens = false; 463 } 464 } 465 466 bool TargetInfo::initFeatureMap( 467 llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags, StringRef CPU, 468 const std::vector<std::string> &FeatureVec) const { 469 for (const auto &F : FeatureVec) { 470 StringRef Name = F; 471 // Apply the feature via the target. 472 bool Enabled = Name[0] == '+'; 473 setFeatureEnabled(Features, Name.substr(1), Enabled); 474 } 475 return true; 476 } 477 478 TargetInfo::CallingConvKind 479 TargetInfo::getCallingConvKind(bool ClangABICompat4) const { 480 if (getCXXABI() != TargetCXXABI::Microsoft && 481 (ClangABICompat4 || getTriple().getOS() == llvm::Triple::PS4)) 482 return CCK_ClangABI4OrPS4; 483 return CCK_Default; 484 } 485 486 LangAS TargetInfo::getOpenCLTypeAddrSpace(OpenCLTypeKind TK) const { 487 switch (TK) { 488 case OCLTK_Image: 489 case OCLTK_Pipe: 490 return LangAS::opencl_global; 491 492 case OCLTK_Sampler: 493 return LangAS::opencl_constant; 494 495 default: 496 return LangAS::Default; 497 } 498 } 499 500 //===----------------------------------------------------------------------===// 501 502 503 static StringRef removeGCCRegisterPrefix(StringRef Name) { 504 if (Name[0] == '%' || Name[0] == '#') 505 Name = Name.substr(1); 506 507 return Name; 508 } 509 510 /// isValidClobber - Returns whether the passed in string is 511 /// a valid clobber in an inline asm statement. This is used by 512 /// Sema. 513 bool TargetInfo::isValidClobber(StringRef Name) const { 514 return (isValidGCCRegisterName(Name) || Name == "memory" || Name == "cc" || 515 Name == "unwind"); 516 } 517 518 /// isValidGCCRegisterName - Returns whether the passed in string 519 /// is a valid register name according to GCC. This is used by Sema for 520 /// inline asm statements. 521 bool TargetInfo::isValidGCCRegisterName(StringRef Name) const { 522 if (Name.empty()) 523 return false; 524 525 // Get rid of any register prefix. 526 Name = removeGCCRegisterPrefix(Name); 527 if (Name.empty()) 528 return false; 529 530 ArrayRef<const char *> Names = getGCCRegNames(); 531 532 // If we have a number it maps to an entry in the register name array. 533 if (isDigit(Name[0])) { 534 unsigned n; 535 if (!Name.getAsInteger(0, n)) 536 return n < Names.size(); 537 } 538 539 // Check register names. 540 if (llvm::is_contained(Names, Name)) 541 return true; 542 543 // Check any additional names that we have. 544 for (const AddlRegName &ARN : getGCCAddlRegNames()) 545 for (const char *AN : ARN.Names) { 546 if (!AN) 547 break; 548 // Make sure the register that the additional name is for is within 549 // the bounds of the register names from above. 550 if (AN == Name && ARN.RegNum < Names.size()) 551 return true; 552 } 553 554 // Now check aliases. 555 for (const GCCRegAlias &GRA : getGCCRegAliases()) 556 for (const char *A : GRA.Aliases) { 557 if (!A) 558 break; 559 if (A == Name) 560 return true; 561 } 562 563 return false; 564 } 565 566 StringRef TargetInfo::getNormalizedGCCRegisterName(StringRef Name, 567 bool ReturnCanonical) const { 568 assert(isValidGCCRegisterName(Name) && "Invalid register passed in"); 569 570 // Get rid of any register prefix. 571 Name = removeGCCRegisterPrefix(Name); 572 573 ArrayRef<const char *> Names = getGCCRegNames(); 574 575 // First, check if we have a number. 576 if (isDigit(Name[0])) { 577 unsigned n; 578 if (!Name.getAsInteger(0, n)) { 579 assert(n < Names.size() && "Out of bounds register number!"); 580 return Names[n]; 581 } 582 } 583 584 // Check any additional names that we have. 585 for (const AddlRegName &ARN : getGCCAddlRegNames()) 586 for (const char *AN : ARN.Names) { 587 if (!AN) 588 break; 589 // Make sure the register that the additional name is for is within 590 // the bounds of the register names from above. 591 if (AN == Name && ARN.RegNum < Names.size()) 592 return ReturnCanonical ? Names[ARN.RegNum] : Name; 593 } 594 595 // Now check aliases. 596 for (const GCCRegAlias &RA : getGCCRegAliases()) 597 for (const char *A : RA.Aliases) { 598 if (!A) 599 break; 600 if (A == Name) 601 return RA.Register; 602 } 603 604 return Name; 605 } 606 607 bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const { 608 const char *Name = Info.getConstraintStr().c_str(); 609 // An output constraint must start with '=' or '+' 610 if (*Name != '=' && *Name != '+') 611 return false; 612 613 if (*Name == '+') 614 Info.setIsReadWrite(); 615 616 Name++; 617 while (*Name) { 618 switch (*Name) { 619 default: 620 if (!validateAsmConstraint(Name, Info)) { 621 // FIXME: We temporarily return false 622 // so we can add more constraints as we hit it. 623 // Eventually, an unknown constraint should just be treated as 'g'. 624 return false; 625 } 626 break; 627 case '&': // early clobber. 628 Info.setEarlyClobber(); 629 break; 630 case '%': // commutative. 631 // FIXME: Check that there is a another register after this one. 632 break; 633 case 'r': // general register. 634 Info.setAllowsRegister(); 635 break; 636 case 'm': // memory operand. 637 case 'o': // offsetable memory operand. 638 case 'V': // non-offsetable memory operand. 639 case '<': // autodecrement memory operand. 640 case '>': // autoincrement memory operand. 641 Info.setAllowsMemory(); 642 break; 643 case 'g': // general register, memory operand or immediate integer. 644 case 'X': // any operand. 645 Info.setAllowsRegister(); 646 Info.setAllowsMemory(); 647 break; 648 case ',': // multiple alternative constraint. Pass it. 649 // Handle additional optional '=' or '+' modifiers. 650 if (Name[1] == '=' || Name[1] == '+') 651 Name++; 652 break; 653 case '#': // Ignore as constraint. 654 while (Name[1] && Name[1] != ',') 655 Name++; 656 break; 657 case '?': // Disparage slightly code. 658 case '!': // Disparage severely. 659 case '*': // Ignore for choosing register preferences. 660 case 'i': // Ignore i,n,E,F as output constraints (match from the other 661 // chars) 662 case 'n': 663 case 'E': 664 case 'F': 665 break; // Pass them. 666 } 667 668 Name++; 669 } 670 671 // Early clobber with a read-write constraint which doesn't permit registers 672 // is invalid. 673 if (Info.earlyClobber() && Info.isReadWrite() && !Info.allowsRegister()) 674 return false; 675 676 // If a constraint allows neither memory nor register operands it contains 677 // only modifiers. Reject it. 678 return Info.allowsMemory() || Info.allowsRegister(); 679 } 680 681 bool TargetInfo::resolveSymbolicName(const char *&Name, 682 ArrayRef<ConstraintInfo> OutputConstraints, 683 unsigned &Index) const { 684 assert(*Name == '[' && "Symbolic name did not start with '['"); 685 Name++; 686 const char *Start = Name; 687 while (*Name && *Name != ']') 688 Name++; 689 690 if (!*Name) { 691 // Missing ']' 692 return false; 693 } 694 695 std::string SymbolicName(Start, Name - Start); 696 697 for (Index = 0; Index != OutputConstraints.size(); ++Index) 698 if (SymbolicName == OutputConstraints[Index].getName()) 699 return true; 700 701 return false; 702 } 703 704 bool TargetInfo::validateInputConstraint( 705 MutableArrayRef<ConstraintInfo> OutputConstraints, 706 ConstraintInfo &Info) const { 707 const char *Name = Info.ConstraintStr.c_str(); 708 709 if (!*Name) 710 return false; 711 712 while (*Name) { 713 switch (*Name) { 714 default: 715 // Check if we have a matching constraint 716 if (*Name >= '0' && *Name <= '9') { 717 const char *DigitStart = Name; 718 while (Name[1] >= '0' && Name[1] <= '9') 719 Name++; 720 const char *DigitEnd = Name; 721 unsigned i; 722 if (StringRef(DigitStart, DigitEnd - DigitStart + 1) 723 .getAsInteger(10, i)) 724 return false; 725 726 // Check if matching constraint is out of bounds. 727 if (i >= OutputConstraints.size()) return false; 728 729 // A number must refer to an output only operand. 730 if (OutputConstraints[i].isReadWrite()) 731 return false; 732 733 // If the constraint is already tied, it must be tied to the 734 // same operand referenced to by the number. 735 if (Info.hasTiedOperand() && Info.getTiedOperand() != i) 736 return false; 737 738 // The constraint should have the same info as the respective 739 // output constraint. 740 Info.setTiedOperand(i, OutputConstraints[i]); 741 } else if (!validateAsmConstraint(Name, Info)) { 742 // FIXME: This error return is in place temporarily so we can 743 // add more constraints as we hit it. Eventually, an unknown 744 // constraint should just be treated as 'g'. 745 return false; 746 } 747 break; 748 case '[': { 749 unsigned Index = 0; 750 if (!resolveSymbolicName(Name, OutputConstraints, Index)) 751 return false; 752 753 // If the constraint is already tied, it must be tied to the 754 // same operand referenced to by the number. 755 if (Info.hasTiedOperand() && Info.getTiedOperand() != Index) 756 return false; 757 758 // A number must refer to an output only operand. 759 if (OutputConstraints[Index].isReadWrite()) 760 return false; 761 762 Info.setTiedOperand(Index, OutputConstraints[Index]); 763 break; 764 } 765 case '%': // commutative 766 // FIXME: Fail if % is used with the last operand. 767 break; 768 case 'i': // immediate integer. 769 break; 770 case 'n': // immediate integer with a known value. 771 Info.setRequiresImmediate(); 772 break; 773 case 'I': // Various constant constraints with target-specific meanings. 774 case 'J': 775 case 'K': 776 case 'L': 777 case 'M': 778 case 'N': 779 case 'O': 780 case 'P': 781 if (!validateAsmConstraint(Name, Info)) 782 return false; 783 break; 784 case 'r': // general register. 785 Info.setAllowsRegister(); 786 break; 787 case 'm': // memory operand. 788 case 'o': // offsettable memory operand. 789 case 'V': // non-offsettable memory operand. 790 case '<': // autodecrement memory operand. 791 case '>': // autoincrement memory operand. 792 Info.setAllowsMemory(); 793 break; 794 case 'g': // general register, memory operand or immediate integer. 795 case 'X': // any operand. 796 Info.setAllowsRegister(); 797 Info.setAllowsMemory(); 798 break; 799 case 'E': // immediate floating point. 800 case 'F': // immediate floating point. 801 case 'p': // address operand. 802 break; 803 case ',': // multiple alternative constraint. Ignore comma. 804 break; 805 case '#': // Ignore as constraint. 806 while (Name[1] && Name[1] != ',') 807 Name++; 808 break; 809 case '?': // Disparage slightly code. 810 case '!': // Disparage severely. 811 case '*': // Ignore for choosing register preferences. 812 break; // Pass them. 813 } 814 815 Name++; 816 } 817 818 return true; 819 } 820 821 void TargetInfo::CheckFixedPointBits() const { 822 // Check that the number of fractional and integral bits (and maybe sign) can 823 // fit into the bits given for a fixed point type. 824 assert(ShortAccumScale + getShortAccumIBits() + 1 <= ShortAccumWidth); 825 assert(AccumScale + getAccumIBits() + 1 <= AccumWidth); 826 assert(LongAccumScale + getLongAccumIBits() + 1 <= LongAccumWidth); 827 assert(getUnsignedShortAccumScale() + getUnsignedShortAccumIBits() <= 828 ShortAccumWidth); 829 assert(getUnsignedAccumScale() + getUnsignedAccumIBits() <= AccumWidth); 830 assert(getUnsignedLongAccumScale() + getUnsignedLongAccumIBits() <= 831 LongAccumWidth); 832 833 assert(getShortFractScale() + 1 <= ShortFractWidth); 834 assert(getFractScale() + 1 <= FractWidth); 835 assert(getLongFractScale() + 1 <= LongFractWidth); 836 assert(getUnsignedShortFractScale() <= ShortFractWidth); 837 assert(getUnsignedFractScale() <= FractWidth); 838 assert(getUnsignedLongFractScale() <= LongFractWidth); 839 840 // Each unsigned fract type has either the same number of fractional bits 841 // as, or one more fractional bit than, its corresponding signed fract type. 842 assert(getShortFractScale() == getUnsignedShortFractScale() || 843 getShortFractScale() == getUnsignedShortFractScale() - 1); 844 assert(getFractScale() == getUnsignedFractScale() || 845 getFractScale() == getUnsignedFractScale() - 1); 846 assert(getLongFractScale() == getUnsignedLongFractScale() || 847 getLongFractScale() == getUnsignedLongFractScale() - 1); 848 849 // When arranged in order of increasing rank (see 6.3.1.3a), the number of 850 // fractional bits is nondecreasing for each of the following sets of 851 // fixed-point types: 852 // - signed fract types 853 // - unsigned fract types 854 // - signed accum types 855 // - unsigned accum types. 856 assert(getLongFractScale() >= getFractScale() && 857 getFractScale() >= getShortFractScale()); 858 assert(getUnsignedLongFractScale() >= getUnsignedFractScale() && 859 getUnsignedFractScale() >= getUnsignedShortFractScale()); 860 assert(LongAccumScale >= AccumScale && AccumScale >= ShortAccumScale); 861 assert(getUnsignedLongAccumScale() >= getUnsignedAccumScale() && 862 getUnsignedAccumScale() >= getUnsignedShortAccumScale()); 863 864 // When arranged in order of increasing rank (see 6.3.1.3a), the number of 865 // integral bits is nondecreasing for each of the following sets of 866 // fixed-point types: 867 // - signed accum types 868 // - unsigned accum types 869 assert(getLongAccumIBits() >= getAccumIBits() && 870 getAccumIBits() >= getShortAccumIBits()); 871 assert(getUnsignedLongAccumIBits() >= getUnsignedAccumIBits() && 872 getUnsignedAccumIBits() >= getUnsignedShortAccumIBits()); 873 874 // Each signed accum type has at least as many integral bits as its 875 // corresponding unsigned accum type. 876 assert(getShortAccumIBits() >= getUnsignedShortAccumIBits()); 877 assert(getAccumIBits() >= getUnsignedAccumIBits()); 878 assert(getLongAccumIBits() >= getUnsignedLongAccumIBits()); 879 } 880 881 void TargetInfo::copyAuxTarget(const TargetInfo *Aux) { 882 auto *Target = static_cast<TransferrableTargetInfo*>(this); 883 auto *Src = static_cast<const TransferrableTargetInfo*>(Aux); 884 *Target = *Src; 885 } 886