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