1 //===--- TargetInfo.h - Expose information about the target -----*- C++ -*-===// 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 /// \file 11 /// \brief Defines the clang::TargetInfo interface. 12 /// 13 //===----------------------------------------------------------------------===// 14 15 #ifndef LLVM_CLANG_BASIC_TARGETINFO_H 16 #define LLVM_CLANG_BASIC_TARGETINFO_H 17 18 #include "clang/Basic/AddressSpaces.h" 19 #include "clang/Basic/LLVM.h" 20 #include "clang/Basic/Specifiers.h" 21 #include "clang/Basic/TargetCXXABI.h" 22 #include "clang/Basic/TargetOptions.h" 23 #include "clang/Basic/VersionTuple.h" 24 #include "llvm/ADT/APInt.h" 25 #include "llvm/ADT/IntrusiveRefCntPtr.h" 26 #include "llvm/ADT/SmallSet.h" 27 #include "llvm/ADT/StringMap.h" 28 #include "llvm/ADT/StringRef.h" 29 #include "llvm/ADT/StringSwitch.h" 30 #include "llvm/ADT/Triple.h" 31 #include "llvm/IR/DataLayout.h" 32 #include "llvm/Support/DataTypes.h" 33 #include <cassert> 34 #include <string> 35 #include <vector> 36 37 namespace llvm { 38 struct fltSemantics; 39 } 40 41 namespace clang { 42 class DiagnosticsEngine; 43 class LangOptions; 44 class MacroBuilder; 45 class SourceLocation; 46 class SourceManager; 47 48 namespace Builtin { struct Info; } 49 50 /// \brief Exposes information about the current target. 51 /// 52 class TargetInfo : public RefCountedBase<TargetInfo> { 53 std::shared_ptr<TargetOptions> TargetOpts; 54 llvm::Triple Triple; 55 protected: 56 // Target values set by the ctor of the actual target implementation. Default 57 // values are specified by the TargetInfo constructor. 58 bool BigEndian; 59 bool TLSSupported; 60 bool NoAsmVariants; // True if {|} are normal characters. 61 bool HasFloat128; 62 unsigned char PointerWidth, PointerAlign; 63 unsigned char BoolWidth, BoolAlign; 64 unsigned char IntWidth, IntAlign; 65 unsigned char HalfWidth, HalfAlign; 66 unsigned char FloatWidth, FloatAlign; 67 unsigned char DoubleWidth, DoubleAlign; 68 unsigned char LongDoubleWidth, LongDoubleAlign, Float128Align; 69 unsigned char LargeArrayMinWidth, LargeArrayAlign; 70 unsigned char LongWidth, LongAlign; 71 unsigned char LongLongWidth, LongLongAlign; 72 unsigned char SuitableAlign; 73 unsigned char DefaultAlignForAttributeAligned; 74 unsigned char MinGlobalAlign; 75 unsigned char MaxAtomicPromoteWidth, MaxAtomicInlineWidth; 76 unsigned short MaxVectorAlign; 77 unsigned short MaxTLSAlign; 78 unsigned short SimdDefaultAlign; 79 std::unique_ptr<llvm::DataLayout> DataLayout; 80 const char *MCountName; 81 const llvm::fltSemantics *HalfFormat, *FloatFormat, *DoubleFormat, 82 *LongDoubleFormat, *Float128Format; 83 unsigned char RegParmMax, SSERegParmMax; 84 TargetCXXABI TheCXXABI; 85 const LangAS::Map *AddrSpaceMap; 86 87 mutable StringRef PlatformName; 88 mutable VersionTuple PlatformMinVersion; 89 90 unsigned HasAlignMac68kSupport : 1; 91 unsigned RealTypeUsesObjCFPRet : 3; 92 unsigned ComplexLongDoubleUsesFP2Ret : 1; 93 94 unsigned HasBuiltinMSVaList : 1; 95 96 unsigned IsRenderScriptTarget : 1; 97 98 // TargetInfo Constructor. Default initializes all fields. 99 TargetInfo(const llvm::Triple &T); 100 101 void resetDataLayout(StringRef DL) { 102 DataLayout.reset(new llvm::DataLayout(DL)); 103 } 104 105 public: 106 /// \brief Construct a target for the given options. 107 /// 108 /// \param Opts - The options to use to initialize the target. The target may 109 /// modify the options to canonicalize the target feature information to match 110 /// what the backend expects. 111 static TargetInfo * 112 CreateTargetInfo(DiagnosticsEngine &Diags, 113 const std::shared_ptr<TargetOptions> &Opts); 114 115 virtual ~TargetInfo(); 116 117 /// \brief Retrieve the target options. 118 TargetOptions &getTargetOpts() const { 119 assert(TargetOpts && "Missing target options"); 120 return *TargetOpts; 121 } 122 123 ///===---- Target Data Type Query Methods -------------------------------===// 124 enum IntType { 125 NoInt = 0, 126 SignedChar, 127 UnsignedChar, 128 SignedShort, 129 UnsignedShort, 130 SignedInt, 131 UnsignedInt, 132 SignedLong, 133 UnsignedLong, 134 SignedLongLong, 135 UnsignedLongLong 136 }; 137 138 enum RealType { 139 NoFloat = 255, 140 Float = 0, 141 Double, 142 LongDouble, 143 Float128 144 }; 145 146 /// \brief The different kinds of __builtin_va_list types defined by 147 /// the target implementation. 148 enum BuiltinVaListKind { 149 /// typedef char* __builtin_va_list; 150 CharPtrBuiltinVaList = 0, 151 152 /// typedef void* __builtin_va_list; 153 VoidPtrBuiltinVaList, 154 155 /// __builtin_va_list as defind by the AArch64 ABI 156 /// http://infocenter.arm.com/help/topic/com.arm.doc.ihi0055a/IHI0055A_aapcs64.pdf 157 AArch64ABIBuiltinVaList, 158 159 /// __builtin_va_list as defined by the PNaCl ABI: 160 /// http://www.chromium.org/nativeclient/pnacl/bitcode-abi#TOC-Machine-Types 161 PNaClABIBuiltinVaList, 162 163 /// __builtin_va_list as defined by the Power ABI: 164 /// https://www.power.org 165 /// /resources/downloads/Power-Arch-32-bit-ABI-supp-1.0-Embedded.pdf 166 PowerABIBuiltinVaList, 167 168 /// __builtin_va_list as defined by the x86-64 ABI: 169 /// http://www.x86-64.org/documentation/abi.pdf 170 X86_64ABIBuiltinVaList, 171 172 /// __builtin_va_list as defined by ARM AAPCS ABI 173 /// http://infocenter.arm.com 174 // /help/topic/com.arm.doc.ihi0042d/IHI0042D_aapcs.pdf 175 AAPCSABIBuiltinVaList, 176 177 // typedef struct __va_list_tag 178 // { 179 // long __gpr; 180 // long __fpr; 181 // void *__overflow_arg_area; 182 // void *__reg_save_area; 183 // } va_list[1]; 184 SystemZBuiltinVaList 185 }; 186 187 protected: 188 IntType SizeType, IntMaxType, PtrDiffType, IntPtrType, WCharType, 189 WIntType, Char16Type, Char32Type, Int64Type, SigAtomicType, 190 ProcessIDType; 191 192 /// \brief Whether Objective-C's built-in boolean type should be signed char. 193 /// 194 /// Otherwise, when this flag is not set, the normal built-in boolean type is 195 /// used. 196 unsigned UseSignedCharForObjCBool : 1; 197 198 /// Control whether the alignment of bit-field types is respected when laying 199 /// out structures. If true, then the alignment of the bit-field type will be 200 /// used to (a) impact the alignment of the containing structure, and (b) 201 /// ensure that the individual bit-field will not straddle an alignment 202 /// boundary. 203 unsigned UseBitFieldTypeAlignment : 1; 204 205 /// \brief Whether zero length bitfields (e.g., int : 0;) force alignment of 206 /// the next bitfield. 207 /// 208 /// If the alignment of the zero length bitfield is greater than the member 209 /// that follows it, `bar', `bar' will be aligned as the type of the 210 /// zero-length bitfield. 211 unsigned UseZeroLengthBitfieldAlignment : 1; 212 213 /// \brief Whether explicit bit field alignment attributes are honored. 214 unsigned UseExplicitBitFieldAlignment : 1; 215 216 /// If non-zero, specifies a fixed alignment value for bitfields that follow 217 /// zero length bitfield, regardless of the zero length bitfield type. 218 unsigned ZeroLengthBitfieldBoundary; 219 220 /// \brief Specify if mangling based on address space map should be used or 221 /// not for language specific address spaces 222 bool UseAddrSpaceMapMangling; 223 224 public: 225 IntType getSizeType() const { return SizeType; } 226 IntType getIntMaxType() const { return IntMaxType; } 227 IntType getUIntMaxType() const { 228 return getCorrespondingUnsignedType(IntMaxType); 229 } 230 IntType getPtrDiffType(unsigned AddrSpace) const { 231 return AddrSpace == 0 ? PtrDiffType : getPtrDiffTypeV(AddrSpace); 232 } 233 IntType getIntPtrType() const { return IntPtrType; } 234 IntType getUIntPtrType() const { 235 return getCorrespondingUnsignedType(IntPtrType); 236 } 237 IntType getWCharType() const { return WCharType; } 238 IntType getWIntType() const { return WIntType; } 239 IntType getChar16Type() const { return Char16Type; } 240 IntType getChar32Type() const { return Char32Type; } 241 IntType getInt64Type() const { return Int64Type; } 242 IntType getUInt64Type() const { 243 return getCorrespondingUnsignedType(Int64Type); 244 } 245 IntType getSigAtomicType() const { return SigAtomicType; } 246 IntType getProcessIDType() const { return ProcessIDType; } 247 248 static IntType getCorrespondingUnsignedType(IntType T) { 249 switch (T) { 250 case SignedChar: 251 return UnsignedChar; 252 case SignedShort: 253 return UnsignedShort; 254 case SignedInt: 255 return UnsignedInt; 256 case SignedLong: 257 return UnsignedLong; 258 case SignedLongLong: 259 return UnsignedLongLong; 260 default: 261 llvm_unreachable("Unexpected signed integer type"); 262 } 263 } 264 265 /// \brief Return the width (in bits) of the specified integer type enum. 266 /// 267 /// For example, SignedInt -> getIntWidth(). 268 unsigned getTypeWidth(IntType T) const; 269 270 /// \brief Return integer type with specified width. 271 virtual IntType getIntTypeByWidth(unsigned BitWidth, bool IsSigned) const; 272 273 /// \brief Return the smallest integer type with at least the specified width. 274 virtual IntType getLeastIntTypeByWidth(unsigned BitWidth, 275 bool IsSigned) const; 276 277 /// \brief Return floating point type with specified width. 278 RealType getRealTypeByWidth(unsigned BitWidth) const; 279 280 /// \brief Return the alignment (in bits) of the specified integer type enum. 281 /// 282 /// For example, SignedInt -> getIntAlign(). 283 unsigned getTypeAlign(IntType T) const; 284 285 /// \brief Returns true if the type is signed; false otherwise. 286 static bool isTypeSigned(IntType T); 287 288 /// \brief Return the width of pointers on this target, for the 289 /// specified address space. 290 uint64_t getPointerWidth(unsigned AddrSpace) const { 291 return AddrSpace == 0 ? PointerWidth : getPointerWidthV(AddrSpace); 292 } 293 uint64_t getPointerAlign(unsigned AddrSpace) const { 294 return AddrSpace == 0 ? PointerAlign : getPointerAlignV(AddrSpace); 295 } 296 297 /// \brief Return the size of '_Bool' and C++ 'bool' for this target, in bits. 298 unsigned getBoolWidth() const { return BoolWidth; } 299 300 /// \brief Return the alignment of '_Bool' and C++ 'bool' for this target. 301 unsigned getBoolAlign() const { return BoolAlign; } 302 303 unsigned getCharWidth() const { return 8; } // FIXME 304 unsigned getCharAlign() const { return 8; } // FIXME 305 306 /// \brief Return the size of 'signed short' and 'unsigned short' for this 307 /// target, in bits. 308 unsigned getShortWidth() const { return 16; } // FIXME 309 310 /// \brief Return the alignment of 'signed short' and 'unsigned short' for 311 /// this target. 312 unsigned getShortAlign() const { return 16; } // FIXME 313 314 /// getIntWidth/Align - Return the size of 'signed int' and 'unsigned int' for 315 /// this target, in bits. 316 unsigned getIntWidth() const { return IntWidth; } 317 unsigned getIntAlign() const { return IntAlign; } 318 319 /// getLongWidth/Align - Return the size of 'signed long' and 'unsigned long' 320 /// for this target, in bits. 321 unsigned getLongWidth() const { return LongWidth; } 322 unsigned getLongAlign() const { return LongAlign; } 323 324 /// getLongLongWidth/Align - Return the size of 'signed long long' and 325 /// 'unsigned long long' for this target, in bits. 326 unsigned getLongLongWidth() const { return LongLongWidth; } 327 unsigned getLongLongAlign() const { return LongLongAlign; } 328 329 /// \brief Determine whether the __int128 type is supported on this target. 330 virtual bool hasInt128Type() const { 331 return getPointerWidth(0) >= 64; 332 } // FIXME 333 334 /// \brief Determine whether the __float128 type is supported on this target. 335 virtual bool hasFloat128Type() const { return HasFloat128; } 336 337 /// \brief Return the alignment that is suitable for storing any 338 /// object with a fundamental alignment requirement. 339 unsigned getSuitableAlign() const { return SuitableAlign; } 340 341 /// \brief Return the default alignment for __attribute__((aligned)) on 342 /// this target, to be used if no alignment value is specified. 343 unsigned getDefaultAlignForAttributeAligned() const { 344 return DefaultAlignForAttributeAligned; 345 } 346 347 /// getMinGlobalAlign - Return the minimum alignment of a global variable, 348 /// unless its alignment is explicitly reduced via attributes. 349 unsigned getMinGlobalAlign() const { return MinGlobalAlign; } 350 351 /// getWCharWidth/Align - Return the size of 'wchar_t' for this target, in 352 /// bits. 353 unsigned getWCharWidth() const { return getTypeWidth(WCharType); } 354 unsigned getWCharAlign() const { return getTypeAlign(WCharType); } 355 356 /// getChar16Width/Align - Return the size of 'char16_t' for this target, in 357 /// bits. 358 unsigned getChar16Width() const { return getTypeWidth(Char16Type); } 359 unsigned getChar16Align() const { return getTypeAlign(Char16Type); } 360 361 /// getChar32Width/Align - Return the size of 'char32_t' for this target, in 362 /// bits. 363 unsigned getChar32Width() const { return getTypeWidth(Char32Type); } 364 unsigned getChar32Align() const { return getTypeAlign(Char32Type); } 365 366 /// getHalfWidth/Align/Format - Return the size/align/format of 'half'. 367 unsigned getHalfWidth() const { return HalfWidth; } 368 unsigned getHalfAlign() const { return HalfAlign; } 369 const llvm::fltSemantics &getHalfFormat() const { return *HalfFormat; } 370 371 /// getFloatWidth/Align/Format - Return the size/align/format of 'float'. 372 unsigned getFloatWidth() const { return FloatWidth; } 373 unsigned getFloatAlign() const { return FloatAlign; } 374 const llvm::fltSemantics &getFloatFormat() const { return *FloatFormat; } 375 376 /// getDoubleWidth/Align/Format - Return the size/align/format of 'double'. 377 unsigned getDoubleWidth() const { return DoubleWidth; } 378 unsigned getDoubleAlign() const { return DoubleAlign; } 379 const llvm::fltSemantics &getDoubleFormat() const { return *DoubleFormat; } 380 381 /// getLongDoubleWidth/Align/Format - Return the size/align/format of 'long 382 /// double'. 383 unsigned getLongDoubleWidth() const { return LongDoubleWidth; } 384 unsigned getLongDoubleAlign() const { return LongDoubleAlign; } 385 const llvm::fltSemantics &getLongDoubleFormat() const { 386 return *LongDoubleFormat; 387 } 388 389 /// getFloat128Width/Align/Format - Return the size/align/format of 390 /// '__float128'. 391 unsigned getFloat128Width() const { return 128; } 392 unsigned getFloat128Align() const { return Float128Align; } 393 const llvm::fltSemantics &getFloat128Format() const { 394 return *Float128Format; 395 } 396 397 /// \brief Return true if the 'long double' type should be mangled like 398 /// __float128. 399 virtual bool useFloat128ManglingForLongDouble() const { return false; } 400 401 /// \brief Return the value for the C99 FLT_EVAL_METHOD macro. 402 virtual unsigned getFloatEvalMethod() const { return 0; } 403 404 // getLargeArrayMinWidth/Align - Return the minimum array size that is 405 // 'large' and its alignment. 406 unsigned getLargeArrayMinWidth() const { return LargeArrayMinWidth; } 407 unsigned getLargeArrayAlign() const { return LargeArrayAlign; } 408 409 /// \brief Return the maximum width lock-free atomic operation which will 410 /// ever be supported for the given target 411 unsigned getMaxAtomicPromoteWidth() const { return MaxAtomicPromoteWidth; } 412 /// \brief Return the maximum width lock-free atomic operation which can be 413 /// inlined given the supported features of the given target. 414 unsigned getMaxAtomicInlineWidth() const { return MaxAtomicInlineWidth; } 415 /// \brief Returns true if the given target supports lock-free atomic 416 /// operations at the specified width and alignment. 417 virtual bool hasBuiltinAtomic(uint64_t AtomicSizeInBits, 418 uint64_t AlignmentInBits) const { 419 return AtomicSizeInBits <= AlignmentInBits && 420 AtomicSizeInBits <= getMaxAtomicInlineWidth() && 421 (AtomicSizeInBits <= getCharWidth() || 422 llvm::isPowerOf2_64(AtomicSizeInBits / getCharWidth())); 423 } 424 425 /// \brief Return the maximum vector alignment supported for the given target. 426 unsigned getMaxVectorAlign() const { return MaxVectorAlign; } 427 /// \brief Return default simd alignment for the given target. Generally, this 428 /// value is type-specific, but this alignment can be used for most of the 429 /// types for the given target. 430 unsigned getSimdDefaultAlign() const { return SimdDefaultAlign; } 431 432 /// Return the alignment (in bits) of the thrown exception object. This is 433 /// only meaningful for targets that allocate C++ exceptions in a system 434 /// runtime, such as those using the Itanium C++ ABI. 435 virtual unsigned getExnObjectAlignment() const { 436 // Itanium says that an _Unwind_Exception has to be "double-word" 437 // aligned (and thus the end of it is also so-aligned), meaning 16 438 // bytes. Of course, that was written for the actual Itanium, 439 // which is a 64-bit platform. Classically, the ABI doesn't really 440 // specify the alignment on other platforms, but in practice 441 // libUnwind declares the struct with __attribute__((aligned)), so 442 // we assume that alignment here. (It's generally 16 bytes, but 443 // some targets overwrite it.) 444 return getDefaultAlignForAttributeAligned(); 445 } 446 447 /// \brief Return the size of intmax_t and uintmax_t for this target, in bits. 448 unsigned getIntMaxTWidth() const { 449 return getTypeWidth(IntMaxType); 450 } 451 452 // Return the size of unwind_word for this target. 453 virtual unsigned getUnwindWordWidth() const { return getPointerWidth(0); } 454 455 /// \brief Return the "preferred" register width on this target. 456 virtual unsigned getRegisterWidth() const { 457 // Currently we assume the register width on the target matches the pointer 458 // width, we can introduce a new variable for this if/when some target wants 459 // it. 460 return PointerWidth; 461 } 462 463 /// \brief Returns the name of the mcount instrumentation function. 464 const char *getMCountName() const { 465 return MCountName; 466 } 467 468 /// \brief Check if the Objective-C built-in boolean type should be signed 469 /// char. 470 /// 471 /// Otherwise, if this returns false, the normal built-in boolean type 472 /// should also be used for Objective-C. 473 bool useSignedCharForObjCBool() const { 474 return UseSignedCharForObjCBool; 475 } 476 void noSignedCharForObjCBool() { 477 UseSignedCharForObjCBool = false; 478 } 479 480 /// \brief Check whether the alignment of bit-field types is respected 481 /// when laying out structures. 482 bool useBitFieldTypeAlignment() const { 483 return UseBitFieldTypeAlignment; 484 } 485 486 /// \brief Check whether zero length bitfields should force alignment of 487 /// the next member. 488 bool useZeroLengthBitfieldAlignment() const { 489 return UseZeroLengthBitfieldAlignment; 490 } 491 492 /// \brief Get the fixed alignment value in bits for a member that follows 493 /// a zero length bitfield. 494 unsigned getZeroLengthBitfieldBoundary() const { 495 return ZeroLengthBitfieldBoundary; 496 } 497 498 /// \brief Check whether explicit bitfield alignment attributes should be 499 // honored, as in "__attribute__((aligned(2))) int b : 1;". 500 bool useExplicitBitFieldAlignment() const { 501 return UseExplicitBitFieldAlignment; 502 } 503 504 /// \brief Check whether this target support '\#pragma options align=mac68k'. 505 bool hasAlignMac68kSupport() const { 506 return HasAlignMac68kSupport; 507 } 508 509 /// \brief Return the user string for the specified integer type enum. 510 /// 511 /// For example, SignedShort -> "short". 512 static const char *getTypeName(IntType T); 513 514 /// \brief Return the constant suffix for the specified integer type enum. 515 /// 516 /// For example, SignedLong -> "L". 517 const char *getTypeConstantSuffix(IntType T) const; 518 519 /// \brief Return the printf format modifier for the specified 520 /// integer type enum. 521 /// 522 /// For example, SignedLong -> "l". 523 static const char *getTypeFormatModifier(IntType T); 524 525 /// \brief Check whether the given real type should use the "fpret" flavor of 526 /// Objective-C message passing on this target. 527 bool useObjCFPRetForRealType(RealType T) const { 528 return RealTypeUsesObjCFPRet & (1 << T); 529 } 530 531 /// \brief Check whether _Complex long double should use the "fp2ret" flavor 532 /// of Objective-C message passing on this target. 533 bool useObjCFP2RetForComplexLongDouble() const { 534 return ComplexLongDoubleUsesFP2Ret; 535 } 536 537 /// \brief Specify if mangling based on address space map should be used or 538 /// not for language specific address spaces 539 bool useAddressSpaceMapMangling() const { 540 return UseAddrSpaceMapMangling; 541 } 542 543 ///===---- Other target property query methods --------------------------===// 544 545 /// \brief Appends the target-specific \#define values for this 546 /// target set to the specified buffer. 547 virtual void getTargetDefines(const LangOptions &Opts, 548 MacroBuilder &Builder) const = 0; 549 550 551 /// Return information about target-specific builtins for 552 /// the current primary target, and info about which builtins are non-portable 553 /// across the current set of primary and secondary targets. 554 virtual ArrayRef<Builtin::Info> getTargetBuiltins() const = 0; 555 556 /// The __builtin_clz* and __builtin_ctz* built-in 557 /// functions are specified to have undefined results for zero inputs, but 558 /// on targets that support these operations in a way that provides 559 /// well-defined results for zero without loss of performance, it is a good 560 /// idea to avoid optimizing based on that undef behavior. 561 virtual bool isCLZForZeroUndef() const { return true; } 562 563 /// \brief Returns the kind of __builtin_va_list type that should be used 564 /// with this target. 565 virtual BuiltinVaListKind getBuiltinVaListKind() const = 0; 566 567 /// Returns whether or not type \c __builtin_ms_va_list type is 568 /// available on this target. 569 bool hasBuiltinMSVaList() const { return HasBuiltinMSVaList; } 570 571 /// Returns true for RenderScript. 572 bool isRenderScriptTarget() const { return IsRenderScriptTarget; } 573 574 /// \brief Returns whether the passed in string is a valid clobber in an 575 /// inline asm statement. 576 /// 577 /// This is used by Sema. 578 bool isValidClobber(StringRef Name) const; 579 580 /// \brief Returns whether the passed in string is a valid register name 581 /// according to GCC. 582 /// 583 /// This is used by Sema for inline asm statements. 584 bool isValidGCCRegisterName(StringRef Name) const; 585 586 /// \brief Returns the "normalized" GCC register name. 587 /// 588 /// For example, on x86 it will return "ax" when "eax" is passed in. 589 StringRef getNormalizedGCCRegisterName(StringRef Name) const; 590 591 struct ConstraintInfo { 592 enum { 593 CI_None = 0x00, 594 CI_AllowsMemory = 0x01, 595 CI_AllowsRegister = 0x02, 596 CI_ReadWrite = 0x04, // "+r" output constraint (read and write). 597 CI_HasMatchingInput = 0x08, // This output operand has a matching input. 598 CI_ImmediateConstant = 0x10, // This operand must be an immediate constant 599 CI_EarlyClobber = 0x20, // "&" output constraint (early clobber). 600 }; 601 unsigned Flags; 602 int TiedOperand; 603 struct { 604 int Min; 605 int Max; 606 } ImmRange; 607 llvm::SmallSet<int, 4> ImmSet; 608 609 std::string ConstraintStr; // constraint: "=rm" 610 std::string Name; // Operand name: [foo] with no []'s. 611 public: 612 ConstraintInfo(StringRef ConstraintStr, StringRef Name) 613 : Flags(0), TiedOperand(-1), ConstraintStr(ConstraintStr.str()), 614 Name(Name.str()) { 615 ImmRange.Min = ImmRange.Max = 0; 616 } 617 618 const std::string &getConstraintStr() const { return ConstraintStr; } 619 const std::string &getName() const { return Name; } 620 bool isReadWrite() const { return (Flags & CI_ReadWrite) != 0; } 621 bool earlyClobber() { return (Flags & CI_EarlyClobber) != 0; } 622 bool allowsRegister() const { return (Flags & CI_AllowsRegister) != 0; } 623 bool allowsMemory() const { return (Flags & CI_AllowsMemory) != 0; } 624 625 /// \brief Return true if this output operand has a matching 626 /// (tied) input operand. 627 bool hasMatchingInput() const { return (Flags & CI_HasMatchingInput) != 0; } 628 629 /// \brief Return true if this input operand is a matching 630 /// constraint that ties it to an output operand. 631 /// 632 /// If this returns true then getTiedOperand will indicate which output 633 /// operand this is tied to. 634 bool hasTiedOperand() const { return TiedOperand != -1; } 635 unsigned getTiedOperand() const { 636 assert(hasTiedOperand() && "Has no tied operand!"); 637 return (unsigned)TiedOperand; 638 } 639 640 bool requiresImmediateConstant() const { 641 return (Flags & CI_ImmediateConstant) != 0; 642 } 643 bool isValidAsmImmediate(const llvm::APInt &Value) const { 644 return (Value.sge(ImmRange.Min) && Value.sle(ImmRange.Max)) || 645 ImmSet.count(Value.getZExtValue()) != 0; 646 } 647 648 void setIsReadWrite() { Flags |= CI_ReadWrite; } 649 void setEarlyClobber() { Flags |= CI_EarlyClobber; } 650 void setAllowsMemory() { Flags |= CI_AllowsMemory; } 651 void setAllowsRegister() { Flags |= CI_AllowsRegister; } 652 void setHasMatchingInput() { Flags |= CI_HasMatchingInput; } 653 void setRequiresImmediate(int Min, int Max) { 654 Flags |= CI_ImmediateConstant; 655 ImmRange.Min = Min; 656 ImmRange.Max = Max; 657 } 658 void setRequiresImmediate(llvm::ArrayRef<int> Exacts) { 659 Flags |= CI_ImmediateConstant; 660 for (int Exact : Exacts) 661 ImmSet.insert(Exact); 662 } 663 void setRequiresImmediate(int Exact) { 664 Flags |= CI_ImmediateConstant; 665 ImmSet.insert(Exact); 666 } 667 void setRequiresImmediate() { 668 Flags |= CI_ImmediateConstant; 669 ImmRange.Min = INT_MIN; 670 ImmRange.Max = INT_MAX; 671 } 672 673 /// \brief Indicate that this is an input operand that is tied to 674 /// the specified output operand. 675 /// 676 /// Copy over the various constraint information from the output. 677 void setTiedOperand(unsigned N, ConstraintInfo &Output) { 678 Output.setHasMatchingInput(); 679 Flags = Output.Flags; 680 TiedOperand = N; 681 // Don't copy Name or constraint string. 682 } 683 }; 684 685 /// \brief Validate register name used for global register variables. 686 /// 687 /// This function returns true if the register passed in RegName can be used 688 /// for global register variables on this target. In addition, it returns 689 /// true in HasSizeMismatch if the size of the register doesn't match the 690 /// variable size passed in RegSize. 691 virtual bool validateGlobalRegisterVariable(StringRef RegName, 692 unsigned RegSize, 693 bool &HasSizeMismatch) const { 694 HasSizeMismatch = false; 695 return true; 696 } 697 698 // validateOutputConstraint, validateInputConstraint - Checks that 699 // a constraint is valid and provides information about it. 700 // FIXME: These should return a real error instead of just true/false. 701 bool validateOutputConstraint(ConstraintInfo &Info) const; 702 bool validateInputConstraint(MutableArrayRef<ConstraintInfo> OutputConstraints, 703 ConstraintInfo &info) const; 704 705 virtual bool validateOutputSize(StringRef /*Constraint*/, 706 unsigned /*Size*/) const { 707 return true; 708 } 709 710 virtual bool validateInputSize(StringRef /*Constraint*/, 711 unsigned /*Size*/) const { 712 return true; 713 } 714 virtual bool 715 validateConstraintModifier(StringRef /*Constraint*/, 716 char /*Modifier*/, 717 unsigned /*Size*/, 718 std::string &/*SuggestedModifier*/) const { 719 return true; 720 } 721 virtual bool 722 validateAsmConstraint(const char *&Name, 723 TargetInfo::ConstraintInfo &info) const = 0; 724 725 bool resolveSymbolicName(const char *&Name, 726 ArrayRef<ConstraintInfo> OutputConstraints, 727 unsigned &Index) const; 728 729 // Constraint parm will be left pointing at the last character of 730 // the constraint. In practice, it won't be changed unless the 731 // constraint is longer than one character. 732 virtual std::string convertConstraint(const char *&Constraint) const { 733 // 'p' defaults to 'r', but can be overridden by targets. 734 if (*Constraint == 'p') 735 return std::string("r"); 736 return std::string(1, *Constraint); 737 } 738 739 /// \brief Returns a string of target-specific clobbers, in LLVM format. 740 virtual const char *getClobbers() const = 0; 741 742 /// \brief Returns true if NaN encoding is IEEE 754-2008. 743 /// Only MIPS allows a different encoding. 744 virtual bool isNan2008() const { 745 return true; 746 } 747 748 /// \brief Returns the target triple of the primary target. 749 const llvm::Triple &getTriple() const { 750 return Triple; 751 } 752 753 const llvm::DataLayout &getDataLayout() const { 754 assert(DataLayout && "Uninitialized DataLayout!"); 755 return *DataLayout; 756 } 757 758 struct GCCRegAlias { 759 const char * const Aliases[5]; 760 const char * const Register; 761 }; 762 763 struct AddlRegName { 764 const char * const Names[5]; 765 const unsigned RegNum; 766 }; 767 768 /// \brief Does this target support "protected" visibility? 769 /// 770 /// Any target which dynamic libraries will naturally support 771 /// something like "default" (meaning that the symbol is visible 772 /// outside this shared object) and "hidden" (meaning that it isn't) 773 /// visibilities, but "protected" is really an ELF-specific concept 774 /// with weird semantics designed around the convenience of dynamic 775 /// linker implementations. Which is not to suggest that there's 776 /// consistent target-independent semantics for "default" visibility 777 /// either; the entire thing is pretty badly mangled. 778 virtual bool hasProtectedVisibility() const { return true; } 779 780 /// \brief An optional hook that targets can implement to perform semantic 781 /// checking on attribute((section("foo"))) specifiers. 782 /// 783 /// In this case, "foo" is passed in to be checked. If the section 784 /// specifier is invalid, the backend should return a non-empty string 785 /// that indicates the problem. 786 /// 787 /// This hook is a simple quality of implementation feature to catch errors 788 /// and give good diagnostics in cases when the assembler or code generator 789 /// would otherwise reject the section specifier. 790 /// 791 virtual std::string isValidSectionSpecifier(StringRef SR) const { 792 return ""; 793 } 794 795 /// \brief Set forced language options. 796 /// 797 /// Apply changes to the target information with respect to certain 798 /// language options which change the target configuration. 799 virtual void adjust(const LangOptions &Opts); 800 801 /// \brief Initialize the map with the default set of target features for the 802 /// CPU this should include all legal feature strings on the target. 803 /// 804 /// \return False on error (invalid features). 805 virtual bool initFeatureMap(llvm::StringMap<bool> &Features, 806 DiagnosticsEngine &Diags, StringRef CPU, 807 const std::vector<std::string> &FeatureVec) const; 808 809 /// \brief Get the ABI currently in use. 810 virtual StringRef getABI() const { return StringRef(); } 811 812 /// \brief Get the C++ ABI currently in use. 813 TargetCXXABI getCXXABI() const { 814 return TheCXXABI; 815 } 816 817 /// \brief Target the specified CPU. 818 /// 819 /// \return False on error (invalid CPU name). 820 virtual bool setCPU(const std::string &Name) { 821 return false; 822 } 823 824 /// \brief Use the specified ABI. 825 /// 826 /// \return False on error (invalid ABI name). 827 virtual bool setABI(const std::string &Name) { 828 return false; 829 } 830 831 /// \brief Use the specified unit for FP math. 832 /// 833 /// \return False on error (invalid unit name). 834 virtual bool setFPMath(StringRef Name) { 835 return false; 836 } 837 838 /// \brief Enable or disable a specific target feature; 839 /// the feature name must be valid. 840 virtual void setFeatureEnabled(llvm::StringMap<bool> &Features, 841 StringRef Name, 842 bool Enabled) const { 843 Features[Name] = Enabled; 844 } 845 846 /// \brief Perform initialization based on the user configured 847 /// set of features (e.g., +sse4). 848 /// 849 /// The list is guaranteed to have at most one entry per feature. 850 /// 851 /// The target may modify the features list, to change which options are 852 /// passed onwards to the backend. 853 /// FIXME: This part should be fixed so that we can change handleTargetFeatures 854 /// to merely a TargetInfo initialization routine. 855 /// 856 /// \return False on error. 857 virtual bool handleTargetFeatures(std::vector<std::string> &Features, 858 DiagnosticsEngine &Diags) { 859 return true; 860 } 861 862 /// \brief Determine whether the given target has the given feature. 863 virtual bool hasFeature(StringRef Feature) const { 864 return false; 865 } 866 867 // \brief Validate the contents of the __builtin_cpu_supports(const char*) 868 // argument. 869 virtual bool validateCpuSupports(StringRef Name) const { return false; } 870 871 // \brief Returns maximal number of args passed in registers. 872 unsigned getRegParmMax() const { 873 assert(RegParmMax < 7 && "RegParmMax value is larger than AST can handle"); 874 return RegParmMax; 875 } 876 877 /// \brief Whether the target supports thread-local storage. 878 bool isTLSSupported() const { 879 return TLSSupported; 880 } 881 882 /// \brief Return the maximum alignment (in bits) of a TLS variable 883 /// 884 /// Gets the maximum alignment (in bits) of a TLS variable on this target. 885 /// Returns zero if there is no such constraint. 886 unsigned short getMaxTLSAlign() const { 887 return MaxTLSAlign; 888 } 889 890 /// \brief Whether the target supports SEH __try. 891 bool isSEHTrySupported() const { 892 return getTriple().isOSWindows() && 893 (getTriple().getArch() == llvm::Triple::x86 || 894 getTriple().getArch() == llvm::Triple::x86_64); 895 } 896 897 /// \brief Return true if {|} are normal characters in the asm string. 898 /// 899 /// If this returns false (the default), then {abc|xyz} is syntax 900 /// that says that when compiling for asm variant #0, "abc" should be 901 /// generated, but when compiling for asm variant #1, "xyz" should be 902 /// generated. 903 bool hasNoAsmVariants() const { 904 return NoAsmVariants; 905 } 906 907 /// \brief Return the register number that __builtin_eh_return_regno would 908 /// return with the specified argument. 909 /// This corresponds with TargetLowering's getExceptionPointerRegister 910 /// and getExceptionSelectorRegister in the backend. 911 virtual int getEHDataRegisterNumber(unsigned RegNo) const { 912 return -1; 913 } 914 915 /// \brief Return the section to use for C++ static initialization functions. 916 virtual const char *getStaticInitSectionSpecifier() const { 917 return nullptr; 918 } 919 920 const LangAS::Map &getAddressSpaceMap() const { 921 return *AddrSpaceMap; 922 } 923 924 /// \brief Retrieve the name of the platform as it is used in the 925 /// availability attribute. 926 StringRef getPlatformName() const { return PlatformName; } 927 928 /// \brief Retrieve the minimum desired version of the platform, to 929 /// which the program should be compiled. 930 VersionTuple getPlatformMinVersion() const { return PlatformMinVersion; } 931 932 bool isBigEndian() const { return BigEndian; } 933 934 enum CallingConvMethodType { 935 CCMT_Unknown, 936 CCMT_Member, 937 CCMT_NonMember 938 }; 939 940 /// \brief Gets the default calling convention for the given target and 941 /// declaration context. 942 virtual CallingConv getDefaultCallingConv(CallingConvMethodType MT) const { 943 // Not all targets will specify an explicit calling convention that we can 944 // express. This will always do the right thing, even though it's not 945 // an explicit calling convention. 946 return CC_C; 947 } 948 949 enum CallingConvCheckResult { 950 CCCR_OK, 951 CCCR_Warning, 952 CCCR_Ignore, 953 }; 954 955 /// \brief Determines whether a given calling convention is valid for the 956 /// target. A calling convention can either be accepted, produce a warning 957 /// and be substituted with the default calling convention, or (someday) 958 /// produce an error (such as using thiscall on a non-instance function). 959 virtual CallingConvCheckResult checkCallingConvention(CallingConv CC) const { 960 switch (CC) { 961 default: 962 return CCCR_Warning; 963 case CC_C: 964 return CCCR_OK; 965 } 966 } 967 968 /// Controls if __builtin_longjmp / __builtin_setjmp can be lowered to 969 /// llvm.eh.sjlj.longjmp / llvm.eh.sjlj.setjmp. 970 virtual bool hasSjLjLowering() const { 971 return false; 972 } 973 974 /// \brief Whether target allows to overalign ABI-specified prefered alignment 975 virtual bool allowsLargerPreferedTypeAlignment() const { return true; } 976 977 /// \brief Set supported OpenCL extensions and optional core features. 978 virtual void setSupportedOpenCLOpts() {} 979 980 /// \brief Get supported OpenCL extensions and optional core features. 981 OpenCLOptions &getSupportedOpenCLOpts() { 982 return getTargetOpts().SupportedOpenCLOptions; 983 } 984 985 /// \brief Get const supported OpenCL extensions and optional core features. 986 const OpenCLOptions &getSupportedOpenCLOpts() const { 987 return getTargetOpts().SupportedOpenCLOptions; 988 } 989 990 /// \brief Check the target is valid after it is fully initialized. 991 virtual bool validateTarget(DiagnosticsEngine &Diags) const { 992 return true; 993 } 994 995 protected: 996 virtual uint64_t getPointerWidthV(unsigned AddrSpace) const { 997 return PointerWidth; 998 } 999 virtual uint64_t getPointerAlignV(unsigned AddrSpace) const { 1000 return PointerAlign; 1001 } 1002 virtual enum IntType getPtrDiffTypeV(unsigned AddrSpace) const { 1003 return PtrDiffType; 1004 } 1005 virtual ArrayRef<const char *> getGCCRegNames() const = 0; 1006 virtual ArrayRef<GCCRegAlias> getGCCRegAliases() const = 0; 1007 virtual ArrayRef<AddlRegName> getGCCAddlRegNames() const { 1008 return None; 1009 } 1010 }; 1011 1012 } // end namespace clang 1013 1014 #endif 1015