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