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 // This file defines the TargetInfo interface. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef LLVM_CLANG_BASIC_TARGETINFO_H 15 #define LLVM_CLANG_BASIC_TARGETINFO_H 16 17 #include "llvm/ADT/IntrusiveRefCntPtr.h" 18 #include "llvm/ADT/StringMap.h" 19 #include "llvm/ADT/StringRef.h" 20 #include "llvm/ADT/StringSwitch.h" 21 #include "llvm/ADT/Triple.h" 22 #include "llvm/Support/DataTypes.h" 23 #include "clang/Basic/AddressSpaces.h" 24 #include "clang/Basic/VersionTuple.h" 25 #include <cassert> 26 #include <vector> 27 #include <string> 28 29 namespace llvm { 30 struct fltSemantics; 31 } 32 33 namespace clang { 34 class Diagnostic; 35 class LangOptions; 36 class MacroBuilder; 37 class SourceLocation; 38 class SourceManager; 39 class TargetOptions; 40 41 namespace Builtin { struct Info; } 42 43 /// TargetCXXABI - The types of C++ ABIs for which we can generate code. 44 enum TargetCXXABI { 45 /// The generic ("Itanium") C++ ABI, documented at: 46 /// http://www.codesourcery.com/public/cxx-abi/ 47 CXXABI_Itanium, 48 49 /// The ARM C++ ABI, based largely on the Itanium ABI but with 50 /// significant differences. 51 /// http://infocenter.arm.com 52 /// /help/topic/com.arm.doc.ihi0041c/IHI0041C_cppabi.pdf 53 CXXABI_ARM, 54 55 /// The Visual Studio ABI. Only scattered official documentation exists. 56 CXXABI_Microsoft 57 }; 58 59 /// TargetInfo - This class exposes information about the current target. 60 /// 61 class TargetInfo : public llvm::RefCountedBase<TargetInfo> { 62 llvm::Triple Triple; 63 protected: 64 // Target values set by the ctor of the actual target implementation. Default 65 // values are specified by the TargetInfo constructor. 66 bool TLSSupported; 67 bool NoAsmVariants; // True if {|} are normal characters. 68 unsigned char PointerWidth, PointerAlign; 69 unsigned char BoolWidth, BoolAlign; 70 unsigned char IntWidth, IntAlign; 71 unsigned char FloatWidth, FloatAlign; 72 unsigned char DoubleWidth, DoubleAlign; 73 unsigned char LongDoubleWidth, LongDoubleAlign; 74 unsigned char LargeArrayMinWidth, LargeArrayAlign; 75 unsigned char LongWidth, LongAlign; 76 unsigned char LongLongWidth, LongLongAlign; 77 const char *DescriptionString; 78 const char *UserLabelPrefix; 79 const char *MCountName; 80 const llvm::fltSemantics *FloatFormat, *DoubleFormat, *LongDoubleFormat; 81 unsigned char RegParmMax, SSERegParmMax; 82 TargetCXXABI CXXABI; 83 const LangAS::Map *AddrSpaceMap; 84 85 mutable llvm::StringRef PlatformName; 86 mutable VersionTuple PlatformMinVersion; 87 88 unsigned HasAlignMac68kSupport : 1; 89 unsigned RealTypeUsesObjCFPRet : 3; 90 91 // TargetInfo Constructor. Default initializes all fields. 92 TargetInfo(const std::string &T); 93 94 public: 95 /// CreateTargetInfo - Construct a target for the given options. 96 /// 97 /// \param Opts - The options to use to initialize the target. The target may 98 /// modify the options to canonicalize the target feature information to match 99 /// what the backend expects. 100 static TargetInfo* CreateTargetInfo(Diagnostic &Diags, TargetOptions &Opts); 101 102 virtual ~TargetInfo(); 103 104 ///===---- Target Data Type Query Methods -------------------------------===// 105 enum IntType { 106 NoInt = 0, 107 SignedShort, 108 UnsignedShort, 109 SignedInt, 110 UnsignedInt, 111 SignedLong, 112 UnsignedLong, 113 SignedLongLong, 114 UnsignedLongLong 115 }; 116 117 enum RealType { 118 Float = 0, 119 Double, 120 LongDouble 121 }; 122 123 protected: 124 IntType SizeType, IntMaxType, UIntMaxType, PtrDiffType, IntPtrType, WCharType, 125 WIntType, Char16Type, Char32Type, Int64Type, SigAtomicType; 126 127 /// Control whether the alignment of bit-field types is respected when laying 128 /// out structures. If true, then the alignment of the bit-field type will be 129 /// used to (a) impact the alignment of the containing structure, and (b) 130 /// ensure that the individual bit-field will not straddle an alignment 131 /// boundary. 132 unsigned UseBitFieldTypeAlignment : 1; 133 134 public: 135 IntType getSizeType() const { return SizeType; } 136 IntType getIntMaxType() const { return IntMaxType; } 137 IntType getUIntMaxType() const { return UIntMaxType; } 138 IntType getPtrDiffType(unsigned AddrSpace) const { 139 return AddrSpace == 0 ? PtrDiffType : getPtrDiffTypeV(AddrSpace); 140 } 141 IntType getIntPtrType() const { return IntPtrType; } 142 IntType getWCharType() const { return WCharType; } 143 IntType getWIntType() const { return WIntType; } 144 IntType getChar16Type() const { return Char16Type; } 145 IntType getChar32Type() const { return Char32Type; } 146 IntType getInt64Type() const { return Int64Type; } 147 IntType getSigAtomicType() const { return SigAtomicType; } 148 149 150 /// getTypeWidth - Return the width (in bits) of the specified integer type 151 /// enum. For example, SignedInt -> getIntWidth(). 152 unsigned getTypeWidth(IntType T) const; 153 154 /// getTypeAlign - Return the alignment (in bits) of the specified integer 155 /// type enum. For example, SignedInt -> getIntAlign(). 156 unsigned getTypeAlign(IntType T) const; 157 158 /// isTypeSigned - Return whether an integer types is signed. Returns true if 159 /// the type is signed; false otherwise. 160 static bool isTypeSigned(IntType T); 161 162 /// getPointerWidth - Return the width of pointers on this target, for the 163 /// specified address space. 164 uint64_t getPointerWidth(unsigned AddrSpace) const { 165 return AddrSpace == 0 ? PointerWidth : getPointerWidthV(AddrSpace); 166 } 167 uint64_t getPointerAlign(unsigned AddrSpace) const { 168 return AddrSpace == 0 ? PointerAlign : getPointerAlignV(AddrSpace); 169 } 170 171 /// getBoolWidth/Align - Return the size of '_Bool' and C++ 'bool' for this 172 /// target, in bits. 173 unsigned getBoolWidth() const { return BoolWidth; } 174 unsigned getBoolAlign() const { return BoolAlign; } 175 176 unsigned getCharWidth() const { return 8; } // FIXME 177 unsigned getCharAlign() const { return 8; } // FIXME 178 179 /// getShortWidth/Align - Return the size of 'signed short' and 180 /// 'unsigned short' for this target, in bits. 181 unsigned getShortWidth() const { return 16; } // FIXME 182 unsigned getShortAlign() const { return 16; } // FIXME 183 184 /// getIntWidth/Align - Return the size of 'signed int' and 'unsigned int' for 185 /// this target, in bits. 186 unsigned getIntWidth() const { return IntWidth; } 187 unsigned getIntAlign() const { return IntAlign; } 188 189 /// getLongWidth/Align - Return the size of 'signed long' and 'unsigned long' 190 /// for this target, in bits. 191 unsigned getLongWidth() const { return LongWidth; } 192 unsigned getLongAlign() const { return LongAlign; } 193 194 /// getLongLongWidth/Align - Return the size of 'signed long long' and 195 /// 'unsigned long long' for this target, in bits. 196 unsigned getLongLongWidth() const { return LongLongWidth; } 197 unsigned getLongLongAlign() const { return LongLongAlign; } 198 199 /// getWCharWidth/Align - Return the size of 'wchar_t' for this target, in 200 /// bits. 201 unsigned getWCharWidth() const { return getTypeWidth(WCharType); } 202 unsigned getWCharAlign() const { return getTypeAlign(WCharType); } 203 204 /// getChar16Width/Align - Return the size of 'char16_t' for this target, in 205 /// bits. 206 unsigned getChar16Width() const { return getTypeWidth(Char16Type); } 207 unsigned getChar16Align() const { return getTypeAlign(Char16Type); } 208 209 /// getChar32Width/Align - Return the size of 'char32_t' for this target, in 210 /// bits. 211 unsigned getChar32Width() const { return getTypeWidth(Char32Type); } 212 unsigned getChar32Align() const { return getTypeAlign(Char32Type); } 213 214 /// getFloatWidth/Align/Format - Return the size/align/format of 'float'. 215 unsigned getFloatWidth() const { return FloatWidth; } 216 unsigned getFloatAlign() const { return FloatAlign; } 217 const llvm::fltSemantics &getFloatFormat() const { return *FloatFormat; } 218 219 /// getDoubleWidth/Align/Format - Return the size/align/format of 'double'. 220 unsigned getDoubleWidth() const { return DoubleWidth; } 221 unsigned getDoubleAlign() const { return DoubleAlign; } 222 const llvm::fltSemantics &getDoubleFormat() const { return *DoubleFormat; } 223 224 /// getLongDoubleWidth/Align/Format - Return the size/align/format of 'long 225 /// double'. 226 unsigned getLongDoubleWidth() const { return LongDoubleWidth; } 227 unsigned getLongDoubleAlign() const { return LongDoubleAlign; } 228 const llvm::fltSemantics &getLongDoubleFormat() const { 229 return *LongDoubleFormat; 230 } 231 232 // getLargeArrayMinWidth/Align - Return the minimum array size that is 233 // 'large' and its alignment. 234 unsigned getLargeArrayMinWidth() const { return LargeArrayMinWidth; } 235 unsigned getLargeArrayAlign() const { return LargeArrayAlign; } 236 237 /// getIntMaxTWidth - Return the size of intmax_t and uintmax_t for this 238 /// target, in bits. 239 unsigned getIntMaxTWidth() const { 240 return getTypeWidth(IntMaxType); 241 } 242 243 /// getRegisterWidth - Return the "preferred" register width on this target. 244 uint64_t getRegisterWidth() const { 245 // Currently we assume the register width on the target matches the pointer 246 // width, we can introduce a new variable for this if/when some target wants 247 // it. 248 return LongWidth; 249 } 250 251 /// getUserLabelPrefix - This returns the default value of the 252 /// __USER_LABEL_PREFIX__ macro, which is the prefix given to user symbols by 253 /// default. On most platforms this is "_", but it is "" on some, and "." on 254 /// others. 255 const char *getUserLabelPrefix() const { 256 return UserLabelPrefix; 257 } 258 259 /// MCountName - This returns name of the mcount instrumentation function. 260 const char *getMCountName() const { 261 return MCountName; 262 } 263 264 bool useBitFieldTypeAlignment() const { 265 return UseBitFieldTypeAlignment; 266 } 267 268 /// hasAlignMac68kSupport - Check whether this target support '#pragma options 269 /// align=mac68k'. 270 bool hasAlignMac68kSupport() const { 271 return HasAlignMac68kSupport; 272 } 273 274 /// getTypeName - Return the user string for the specified integer type enum. 275 /// For example, SignedShort -> "short". 276 static const char *getTypeName(IntType T); 277 278 /// getTypeConstantSuffix - Return the constant suffix for the specified 279 /// integer type enum. For example, SignedLong -> "L". 280 static const char *getTypeConstantSuffix(IntType T); 281 282 /// \brief Check whether the given real type should use the "fpret" flavor of 283 /// Obj-C message passing on this target. 284 bool useObjCFPRetForRealType(RealType T) const { 285 return RealTypeUsesObjCFPRet & (1 << T); 286 } 287 288 ///===---- Other target property query methods --------------------------===// 289 290 /// getTargetDefines - Appends the target-specific #define values for this 291 /// target set to the specified buffer. 292 virtual void getTargetDefines(const LangOptions &Opts, 293 MacroBuilder &Builder) const = 0; 294 295 296 /// getTargetBuiltins - Return information about target-specific builtins for 297 /// the current primary target, and info about which builtins are non-portable 298 /// across the current set of primary and secondary targets. 299 virtual void getTargetBuiltins(const Builtin::Info *&Records, 300 unsigned &NumRecords) const = 0; 301 302 /// getVAListDeclaration - Return the declaration to use for 303 /// __builtin_va_list, which is target-specific. 304 virtual const char *getVAListDeclaration() const = 0; 305 306 /// isValidClobber - Returns whether the passed in string is 307 /// a valid clobber in an inline asm statement. This is used by 308 /// Sema. 309 bool isValidClobber(llvm::StringRef Name) const; 310 311 /// isValidGCCRegisterName - Returns whether the passed in string 312 /// is a valid register name according to GCC. This is used by Sema for 313 /// inline asm statements. 314 bool isValidGCCRegisterName(llvm::StringRef Name) const; 315 316 // getNormalizedGCCRegisterName - Returns the "normalized" GCC register name. 317 // For example, on x86 it will return "ax" when "eax" is passed in. 318 llvm::StringRef getNormalizedGCCRegisterName(llvm::StringRef Name) const; 319 320 struct ConstraintInfo { 321 enum { 322 CI_None = 0x00, 323 CI_AllowsMemory = 0x01, 324 CI_AllowsRegister = 0x02, 325 CI_ReadWrite = 0x04, // "+r" output constraint (read and write). 326 CI_HasMatchingInput = 0x08 // This output operand has a matching input. 327 }; 328 unsigned Flags; 329 int TiedOperand; 330 331 std::string ConstraintStr; // constraint: "=rm" 332 std::string Name; // Operand name: [foo] with no []'s. 333 public: 334 ConstraintInfo(llvm::StringRef ConstraintStr, llvm::StringRef Name) 335 : Flags(0), TiedOperand(-1), ConstraintStr(ConstraintStr.str()), 336 Name(Name.str()) {} 337 338 const std::string &getConstraintStr() const { return ConstraintStr; } 339 const std::string &getName() const { return Name; } 340 bool isReadWrite() const { return (Flags & CI_ReadWrite) != 0; } 341 bool allowsRegister() const { return (Flags & CI_AllowsRegister) != 0; } 342 bool allowsMemory() const { return (Flags & CI_AllowsMemory) != 0; } 343 344 /// hasMatchingInput - Return true if this output operand has a matching 345 /// (tied) input operand. 346 bool hasMatchingInput() const { return (Flags & CI_HasMatchingInput) != 0; } 347 348 /// hasTiedOperand() - Return true if this input operand is a matching 349 /// constraint that ties it to an output operand. If this returns true, 350 /// then getTiedOperand will indicate which output operand this is tied to. 351 bool hasTiedOperand() const { return TiedOperand != -1; } 352 unsigned getTiedOperand() const { 353 assert(hasTiedOperand() && "Has no tied operand!"); 354 return (unsigned)TiedOperand; 355 } 356 357 void setIsReadWrite() { Flags |= CI_ReadWrite; } 358 void setAllowsMemory() { Flags |= CI_AllowsMemory; } 359 void setAllowsRegister() { Flags |= CI_AllowsRegister; } 360 void setHasMatchingInput() { Flags |= CI_HasMatchingInput; } 361 362 /// setTiedOperand - Indicate that this is an input operand that is tied to 363 /// the specified output operand. Copy over the various constraint 364 /// information from the output. 365 void setTiedOperand(unsigned N, ConstraintInfo &Output) { 366 Output.setHasMatchingInput(); 367 Flags = Output.Flags; 368 TiedOperand = N; 369 // Don't copy Name or constraint string. 370 } 371 }; 372 373 // validateOutputConstraint, validateInputConstraint - Checks that 374 // a constraint is valid and provides information about it. 375 // FIXME: These should return a real error instead of just true/false. 376 bool validateOutputConstraint(ConstraintInfo &Info) const; 377 bool validateInputConstraint(ConstraintInfo *OutputConstraints, 378 unsigned NumOutputs, 379 ConstraintInfo &info) const; 380 bool resolveSymbolicName(const char *&Name, 381 ConstraintInfo *OutputConstraints, 382 unsigned NumOutputs, unsigned &Index) const; 383 384 // Constraint parm will be left pointing at the last character of 385 // the constraint. In practice, it won't be changed unless the 386 // constraint is longer than one character. 387 virtual std::string convertConstraint(const char *&Constraint) const { 388 // 'p' defaults to 'r', but can be overridden by targets. 389 if (*Constraint == 'p') 390 return std::string("r"); 391 return std::string(1, *Constraint); 392 } 393 394 // Returns a string of target-specific clobbers, in LLVM format. 395 virtual const char *getClobbers() const = 0; 396 397 398 /// getTriple - Return the target triple of the primary target. 399 const llvm::Triple &getTriple() const { 400 return Triple; 401 } 402 403 const char *getTargetDescription() const { 404 return DescriptionString; 405 } 406 407 struct GCCRegAlias { 408 const char * const Aliases[5]; 409 const char * const Register; 410 }; 411 412 struct AddlRegName { 413 const char * const Names[5]; 414 const unsigned RegNum; 415 }; 416 417 virtual bool useGlobalsForAutomaticVariables() const { return false; } 418 419 /// getCFStringSection - Return the section to use for CFString 420 /// literals, or 0 if no special section is used. 421 virtual const char *getCFStringSection() const { 422 return "__DATA,__cfstring"; 423 } 424 425 /// getNSStringSection - Return the section to use for NSString 426 /// literals, or 0 if no special section is used. 427 virtual const char *getNSStringSection() const { 428 return "__OBJC,__cstring_object,regular,no_dead_strip"; 429 } 430 431 /// getNSStringNonFragileABISection - Return the section to use for 432 /// NSString literals, or 0 if no special section is used (NonFragile ABI). 433 virtual const char *getNSStringNonFragileABISection() const { 434 return "__DATA, __objc_stringobj, regular, no_dead_strip"; 435 } 436 437 /// isValidSectionSpecifier - This is an optional hook that targets can 438 /// implement to perform semantic checking on attribute((section("foo"))) 439 /// specifiers. In this case, "foo" is passed in to be checked. If the 440 /// section specifier is invalid, the backend should return a non-empty string 441 /// that indicates the problem. 442 /// 443 /// This hook is a simple quality of implementation feature to catch errors 444 /// and give good diagnostics in cases when the assembler or code generator 445 /// would otherwise reject the section specifier. 446 /// 447 virtual std::string isValidSectionSpecifier(llvm::StringRef SR) const { 448 return ""; 449 } 450 451 /// setForcedLangOptions - Set forced language options. 452 /// Apply changes to the target information with respect to certain 453 /// language options which change the target configuration. 454 virtual void setForcedLangOptions(LangOptions &Opts); 455 456 /// getDefaultFeatures - Get the default set of target features for 457 /// the \args CPU; this should include all legal feature strings on 458 /// the target. 459 virtual void getDefaultFeatures(const std::string &CPU, 460 llvm::StringMap<bool> &Features) const { 461 } 462 463 /// getABI - Get the ABI in use. 464 virtual const char *getABI() const { 465 return ""; 466 } 467 468 /// getCXXABI - Get the C++ ABI in use. 469 virtual TargetCXXABI getCXXABI() const { 470 return CXXABI; 471 } 472 473 /// setCPU - Target the specific CPU. 474 /// 475 /// \return - False on error (invalid CPU name). 476 // 477 // FIXME: Remove this. 478 virtual bool setCPU(const std::string &Name) { 479 return true; 480 } 481 482 /// setABI - Use the specific ABI. 483 /// 484 /// \return - False on error (invalid ABI name). 485 virtual bool setABI(const std::string &Name) { 486 return false; 487 } 488 489 /// setCXXABI - Use this specific C++ ABI. 490 /// 491 /// \return - False on error (invalid C++ ABI name). 492 bool setCXXABI(const std::string &Name) { 493 static const TargetCXXABI Unknown = static_cast<TargetCXXABI>(-1); 494 TargetCXXABI ABI = llvm::StringSwitch<TargetCXXABI>(Name) 495 .Case("arm", CXXABI_ARM) 496 .Case("itanium", CXXABI_Itanium) 497 .Case("microsoft", CXXABI_Microsoft) 498 .Default(Unknown); 499 if (ABI == Unknown) return false; 500 return setCXXABI(ABI); 501 } 502 503 /// setCXXABI - Set the C++ ABI to be used by this implementation. 504 /// 505 /// \return - False on error (ABI not valid on this target) 506 virtual bool setCXXABI(TargetCXXABI ABI) { 507 CXXABI = ABI; 508 return true; 509 } 510 511 /// setFeatureEnabled - Enable or disable a specific target feature, 512 /// the feature name must be valid. 513 /// 514 /// \return - False on error (invalid feature name). 515 virtual bool setFeatureEnabled(llvm::StringMap<bool> &Features, 516 const std::string &Name, 517 bool Enabled) const { 518 return false; 519 } 520 521 /// HandleTargetOptions - Perform initialization based on the user configured 522 /// set of features (e.g., +sse4). The list is guaranteed to have at most one 523 /// entry per feature. 524 /// 525 /// The target may modify the features list, to change which options are 526 /// passed onwards to the backend. 527 virtual void HandleTargetFeatures(std::vector<std::string> &Features) { 528 } 529 530 // getRegParmMax - Returns maximal number of args passed in registers. 531 unsigned getRegParmMax() const { 532 assert(RegParmMax < 7 && "RegParmMax value is larger than AST can handle"); 533 return RegParmMax; 534 } 535 536 /// isTLSSupported - Whether the target supports thread-local storage. 537 bool isTLSSupported() const { 538 return TLSSupported; 539 } 540 541 /// hasNoAsmVariants - Return true if {|} are normal characters in the 542 /// asm string. If this returns false (the default), then {abc|xyz} is syntax 543 /// that says that when compiling for asm variant #0, "abc" should be 544 /// generated, but when compiling for asm variant #1, "xyz" should be 545 /// generated. 546 bool hasNoAsmVariants() const { 547 return NoAsmVariants; 548 } 549 550 /// getEHDataRegisterNumber - Return the register number that 551 /// __builtin_eh_return_regno would return with the specified argument. 552 virtual int getEHDataRegisterNumber(unsigned RegNo) const { 553 return -1; 554 } 555 556 /// getStaticInitSectionSpecifier - Return the section to use for C++ static 557 /// initialization functions. 558 virtual const char *getStaticInitSectionSpecifier() const { 559 return 0; 560 } 561 562 const LangAS::Map &getAddressSpaceMap() const { 563 return *AddrSpaceMap; 564 } 565 566 /// \brief Retrieve the name of the platform as it is used in the 567 /// availability attribute. 568 llvm::StringRef getPlatformName() const { return PlatformName; } 569 570 /// \brief Retrieve the minimum desired version of the platform, to 571 /// which the program should be compiled. 572 VersionTuple getPlatformMinVersion() const { return PlatformMinVersion; } 573 574 protected: 575 virtual uint64_t getPointerWidthV(unsigned AddrSpace) const { 576 return PointerWidth; 577 } 578 virtual uint64_t getPointerAlignV(unsigned AddrSpace) const { 579 return PointerAlign; 580 } 581 virtual enum IntType getPtrDiffTypeV(unsigned AddrSpace) const { 582 return PtrDiffType; 583 } 584 virtual void getGCCRegNames(const char * const *&Names, 585 unsigned &NumNames) const = 0; 586 virtual void getGCCRegAliases(const GCCRegAlias *&Aliases, 587 unsigned &NumAliases) const = 0; 588 virtual void getGCCAddlRegNames(const AddlRegName *&Addl, 589 unsigned &NumAddl) const { 590 Addl = 0; 591 NumAddl = 0; 592 } 593 virtual bool validateAsmConstraint(const char *&Name, 594 TargetInfo::ConstraintInfo &info) const= 0; 595 }; 596 597 } // end namespace clang 598 599 #endif 600