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