1 //===--- TargetInfo.h - Expose information about the target -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the TargetInfo interface.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_BASIC_TARGETINFO_H
15 #define LLVM_CLANG_BASIC_TARGETINFO_H
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"
35 class DiagnosticsEngine;
42 namespace Builtin { struct Info; }
44 /// TargetCXXABI - The types of C++ ABIs for which we can generate code.
46 /// The generic ("Itanium") C++ ABI, documented at:
47 /// http://www.codesourcery.com/public/cxx-abi/
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
56 /// The Visual Studio ABI. Only scattered official documentation exists.
60 /// TargetInfo - This class exposes information about the current target.
62 class TargetInfo : public llvm::RefCountedBase<TargetInfo> {
65 // Target values set by the ctor of the actual target implementation. Default
66 // values are specified by the TargetInfo constructor.
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 HalfWidth, HalfAlign;
73 unsigned char FloatWidth, FloatAlign;
74 unsigned char DoubleWidth, DoubleAlign;
75 unsigned char LongDoubleWidth, LongDoubleAlign;
76 unsigned char LargeArrayMinWidth, LargeArrayAlign;
77 unsigned char LongWidth, LongAlign;
78 unsigned char LongLongWidth, LongLongAlign;
79 unsigned char MaxAtomicPromoteWidth, MaxAtomicInlineWidth;
80 const char *DescriptionString;
81 const char *UserLabelPrefix;
82 const char *MCountName;
83 const llvm::fltSemantics *HalfFormat, *FloatFormat, *DoubleFormat,
85 unsigned char RegParmMax, SSERegParmMax;
87 const LangAS::Map *AddrSpaceMap;
89 mutable StringRef PlatformName;
90 mutable VersionTuple PlatformMinVersion;
92 unsigned HasAlignMac68kSupport : 1;
93 unsigned RealTypeUsesObjCFPRet : 3;
95 // TargetInfo Constructor. Default initializes all fields.
96 TargetInfo(const std::string &T);
99 /// CreateTargetInfo - Construct a target for the given options.
101 /// \param Opts - The options to use to initialize the target. The target may
102 /// modify the options to canonicalize the target feature information to match
103 /// what the backend expects.
104 static TargetInfo* CreateTargetInfo(DiagnosticsEngine &Diags,
105 TargetOptions &Opts);
107 virtual ~TargetInfo();
109 ///===---- Target Data Type Query Methods -------------------------------===//
129 IntType SizeType, IntMaxType, UIntMaxType, PtrDiffType, IntPtrType, WCharType,
130 WIntType, Char16Type, Char32Type, Int64Type, SigAtomicType;
132 /// Control whether the alignment of bit-field types is respected when laying
133 /// out structures. If true, then the alignment of the bit-field type will be
134 /// used to (a) impact the alignment of the containing structure, and (b)
135 /// ensure that the individual bit-field will not straddle an alignment
137 unsigned UseBitFieldTypeAlignment : 1;
139 /// Control whether zero length bitfields (e.g., int : 0;) force alignment of
140 /// the next bitfield. If the alignment of the zero length bitfield is
141 /// greater than the member that follows it, `bar', `bar' will be aligned as
142 /// the type of the zero-length bitfield.
143 unsigned UseZeroLengthBitfieldAlignment : 1;
145 /// If non-zero, specifies a fixed alignment value for bitfields that follow
146 /// zero length bitfield, regardless of the zero length bitfield type.
147 unsigned ZeroLengthBitfieldBoundary;
150 IntType getSizeType() const { return SizeType; }
151 IntType getIntMaxType() const { return IntMaxType; }
152 IntType getUIntMaxType() const { return UIntMaxType; }
153 IntType getPtrDiffType(unsigned AddrSpace) const {
154 return AddrSpace == 0 ? PtrDiffType : getPtrDiffTypeV(AddrSpace);
156 IntType getIntPtrType() const { return IntPtrType; }
157 IntType getWCharType() const { return WCharType; }
158 IntType getWIntType() const { return WIntType; }
159 IntType getChar16Type() const { return Char16Type; }
160 IntType getChar32Type() const { return Char32Type; }
161 IntType getInt64Type() const { return Int64Type; }
162 IntType getSigAtomicType() const { return SigAtomicType; }
165 /// getTypeWidth - Return the width (in bits) of the specified integer type
166 /// enum. For example, SignedInt -> getIntWidth().
167 unsigned getTypeWidth(IntType T) const;
169 /// getTypeAlign - Return the alignment (in bits) of the specified integer
170 /// type enum. For example, SignedInt -> getIntAlign().
171 unsigned getTypeAlign(IntType T) const;
173 /// isTypeSigned - Return whether an integer types is signed. Returns true if
174 /// the type is signed; false otherwise.
175 static bool isTypeSigned(IntType T);
177 /// getPointerWidth - Return the width of pointers on this target, for the
178 /// specified address space.
179 uint64_t getPointerWidth(unsigned AddrSpace) const {
180 return AddrSpace == 0 ? PointerWidth : getPointerWidthV(AddrSpace);
182 uint64_t getPointerAlign(unsigned AddrSpace) const {
183 return AddrSpace == 0 ? PointerAlign : getPointerAlignV(AddrSpace);
186 /// getBoolWidth/Align - Return the size of '_Bool' and C++ 'bool' for this
188 unsigned getBoolWidth() const { return BoolWidth; }
189 unsigned getBoolAlign() const { return BoolAlign; }
191 unsigned getCharWidth() const { return 8; } // FIXME
192 unsigned getCharAlign() const { return 8; } // FIXME
194 /// getShortWidth/Align - Return the size of 'signed short' and
195 /// 'unsigned short' for this target, in bits.
196 unsigned getShortWidth() const { return 16; } // FIXME
197 unsigned getShortAlign() const { return 16; } // FIXME
199 /// getIntWidth/Align - Return the size of 'signed int' and 'unsigned int' for
200 /// this target, in bits.
201 unsigned getIntWidth() const { return IntWidth; }
202 unsigned getIntAlign() const { return IntAlign; }
204 /// getLongWidth/Align - Return the size of 'signed long' and 'unsigned long'
205 /// for this target, in bits.
206 unsigned getLongWidth() const { return LongWidth; }
207 unsigned getLongAlign() const { return LongAlign; }
209 /// getLongLongWidth/Align - Return the size of 'signed long long' and
210 /// 'unsigned long long' for this target, in bits.
211 unsigned getLongLongWidth() const { return LongLongWidth; }
212 unsigned getLongLongAlign() const { return LongLongAlign; }
214 /// getWCharWidth/Align - Return the size of 'wchar_t' for this target, in
216 unsigned getWCharWidth() const { return getTypeWidth(WCharType); }
217 unsigned getWCharAlign() const { return getTypeAlign(WCharType); }
219 /// getChar16Width/Align - Return the size of 'char16_t' for this target, in
221 unsigned getChar16Width() const { return getTypeWidth(Char16Type); }
222 unsigned getChar16Align() const { return getTypeAlign(Char16Type); }
224 /// getChar32Width/Align - Return the size of 'char32_t' for this target, in
226 unsigned getChar32Width() const { return getTypeWidth(Char32Type); }
227 unsigned getChar32Align() const { return getTypeAlign(Char32Type); }
229 /// getHalfWidth/Align/Format - Return the size/align/format of 'half'.
230 unsigned getHalfWidth() const { return HalfWidth; }
231 unsigned getHalfAlign() const { return HalfAlign; }
232 const llvm::fltSemantics &getHalfFormat() const { return *HalfFormat; }
234 /// getFloatWidth/Align/Format - Return the size/align/format of 'float'.
235 unsigned getFloatWidth() const { return FloatWidth; }
236 unsigned getFloatAlign() const { return FloatAlign; }
237 const llvm::fltSemantics &getFloatFormat() const { return *FloatFormat; }
239 /// getDoubleWidth/Align/Format - Return the size/align/format of 'double'.
240 unsigned getDoubleWidth() const { return DoubleWidth; }
241 unsigned getDoubleAlign() const { return DoubleAlign; }
242 const llvm::fltSemantics &getDoubleFormat() const { return *DoubleFormat; }
244 /// getLongDoubleWidth/Align/Format - Return the size/align/format of 'long
246 unsigned getLongDoubleWidth() const { return LongDoubleWidth; }
247 unsigned getLongDoubleAlign() const { return LongDoubleAlign; }
248 const llvm::fltSemantics &getLongDoubleFormat() const {
249 return *LongDoubleFormat;
252 // getLargeArrayMinWidth/Align - Return the minimum array size that is
253 // 'large' and its alignment.
254 unsigned getLargeArrayMinWidth() const { return LargeArrayMinWidth; }
255 unsigned getLargeArrayAlign() const { return LargeArrayAlign; }
257 /// getMaxAtomicPromoteWidth - Return the maximum width lock-free atomic
258 /// operation which will ever be supported for the given target
259 unsigned getMaxAtomicPromoteWidth() const { return MaxAtomicPromoteWidth; }
260 /// getMaxAtomicInlineWidth - Return the maximum width lock-free atomic
261 /// operation which can be inlined given the supported features of the
263 unsigned getMaxAtomicInlineWidth() const { return MaxAtomicInlineWidth; }
265 /// getIntMaxTWidth - Return the size of intmax_t and uintmax_t for this
267 unsigned getIntMaxTWidth() const {
268 return getTypeWidth(IntMaxType);
271 /// getRegisterWidth - Return the "preferred" register width on this target.
272 uint64_t getRegisterWidth() const {
273 // Currently we assume the register width on the target matches the pointer
274 // width, we can introduce a new variable for this if/when some target wants
279 /// getUserLabelPrefix - This returns the default value of the
280 /// __USER_LABEL_PREFIX__ macro, which is the prefix given to user symbols by
281 /// default. On most platforms this is "_", but it is "" on some, and "." on
283 const char *getUserLabelPrefix() const {
284 return UserLabelPrefix;
287 /// MCountName - This returns name of the mcount instrumentation function.
288 const char *getMCountName() const {
292 /// useBitFieldTypeAlignment() - Check whether the alignment of bit-field
293 /// types is respected when laying out structures.
294 bool useBitFieldTypeAlignment() const {
295 return UseBitFieldTypeAlignment;
298 /// useZeroLengthBitfieldAlignment() - Check whether zero length bitfields
299 /// should force alignment of the next member.
300 bool useZeroLengthBitfieldAlignment() const {
301 return UseZeroLengthBitfieldAlignment;
304 /// getZeroLengthBitfieldBoundary() - Get the fixed alignment value in bits
305 /// for a member that follows a zero length bitfield.
306 unsigned getZeroLengthBitfieldBoundary() const {
307 return ZeroLengthBitfieldBoundary;
310 /// hasAlignMac68kSupport - Check whether this target support '#pragma options
312 bool hasAlignMac68kSupport() const {
313 return HasAlignMac68kSupport;
316 /// getTypeName - Return the user string for the specified integer type enum.
317 /// For example, SignedShort -> "short".
318 static const char *getTypeName(IntType T);
320 /// getTypeConstantSuffix - Return the constant suffix for the specified
321 /// integer type enum. For example, SignedLong -> "L".
322 static const char *getTypeConstantSuffix(IntType T);
324 /// \brief Check whether the given real type should use the "fpret" flavor of
325 /// Obj-C message passing on this target.
326 bool useObjCFPRetForRealType(RealType T) const {
327 return RealTypeUsesObjCFPRet & (1 << T);
330 ///===---- Other target property query methods --------------------------===//
332 /// getTargetDefines - Appends the target-specific #define values for this
333 /// target set to the specified buffer.
334 virtual void getTargetDefines(const LangOptions &Opts,
335 MacroBuilder &Builder) const = 0;
338 /// getTargetBuiltins - Return information about target-specific builtins for
339 /// the current primary target, and info about which builtins are non-portable
340 /// across the current set of primary and secondary targets.
341 virtual void getTargetBuiltins(const Builtin::Info *&Records,
342 unsigned &NumRecords) const = 0;
344 /// getVAListDeclaration - Return the declaration to use for
345 /// __builtin_va_list, which is target-specific.
346 virtual const char *getVAListDeclaration() const = 0;
348 /// isValidClobber - Returns whether the passed in string is
349 /// a valid clobber in an inline asm statement. This is used by
351 bool isValidClobber(StringRef Name) const;
353 /// isValidGCCRegisterName - Returns whether the passed in string
354 /// is a valid register name according to GCC. This is used by Sema for
355 /// inline asm statements.
356 bool isValidGCCRegisterName(StringRef Name) const;
358 // getNormalizedGCCRegisterName - Returns the "normalized" GCC register name.
359 // For example, on x86 it will return "ax" when "eax" is passed in.
360 StringRef getNormalizedGCCRegisterName(StringRef Name) const;
362 struct ConstraintInfo {
365 CI_AllowsMemory = 0x01,
366 CI_AllowsRegister = 0x02,
367 CI_ReadWrite = 0x04, // "+r" output constraint (read and write).
368 CI_HasMatchingInput = 0x08 // This output operand has a matching input.
373 std::string ConstraintStr; // constraint: "=rm"
374 std::string Name; // Operand name: [foo] with no []'s.
376 ConstraintInfo(StringRef ConstraintStr, StringRef Name)
377 : Flags(0), TiedOperand(-1), ConstraintStr(ConstraintStr.str()),
380 const std::string &getConstraintStr() const { return ConstraintStr; }
381 const std::string &getName() const { return Name; }
382 bool isReadWrite() const { return (Flags & CI_ReadWrite) != 0; }
383 bool allowsRegister() const { return (Flags & CI_AllowsRegister) != 0; }
384 bool allowsMemory() const { return (Flags & CI_AllowsMemory) != 0; }
386 /// hasMatchingInput - Return true if this output operand has a matching
387 /// (tied) input operand.
388 bool hasMatchingInput() const { return (Flags & CI_HasMatchingInput) != 0; }
390 /// hasTiedOperand() - Return true if this input operand is a matching
391 /// constraint that ties it to an output operand. If this returns true,
392 /// then getTiedOperand will indicate which output operand this is tied to.
393 bool hasTiedOperand() const { return TiedOperand != -1; }
394 unsigned getTiedOperand() const {
395 assert(hasTiedOperand() && "Has no tied operand!");
396 return (unsigned)TiedOperand;
399 void setIsReadWrite() { Flags |= CI_ReadWrite; }
400 void setAllowsMemory() { Flags |= CI_AllowsMemory; }
401 void setAllowsRegister() { Flags |= CI_AllowsRegister; }
402 void setHasMatchingInput() { Flags |= CI_HasMatchingInput; }
404 /// setTiedOperand - Indicate that this is an input operand that is tied to
405 /// the specified output operand. Copy over the various constraint
406 /// information from the output.
407 void setTiedOperand(unsigned N, ConstraintInfo &Output) {
408 Output.setHasMatchingInput();
409 Flags = Output.Flags;
411 // Don't copy Name or constraint string.
415 // validateOutputConstraint, validateInputConstraint - Checks that
416 // a constraint is valid and provides information about it.
417 // FIXME: These should return a real error instead of just true/false.
418 bool validateOutputConstraint(ConstraintInfo &Info) const;
419 bool validateInputConstraint(ConstraintInfo *OutputConstraints,
421 ConstraintInfo &info) const;
422 bool resolveSymbolicName(const char *&Name,
423 ConstraintInfo *OutputConstraints,
424 unsigned NumOutputs, unsigned &Index) const;
426 // Constraint parm will be left pointing at the last character of
427 // the constraint. In practice, it won't be changed unless the
428 // constraint is longer than one character.
429 virtual std::string convertConstraint(const char *&Constraint) const {
430 // 'p' defaults to 'r', but can be overridden by targets.
431 if (*Constraint == 'p')
432 return std::string("r");
433 return std::string(1, *Constraint);
436 // Returns a string of target-specific clobbers, in LLVM format.
437 virtual const char *getClobbers() const = 0;
440 /// getTriple - Return the target triple of the primary target.
441 const llvm::Triple &getTriple() const {
445 const char *getTargetDescription() const {
446 return DescriptionString;
450 const char * const Aliases[5];
451 const char * const Register;
455 const char * const Names[5];
456 const unsigned RegNum;
459 virtual bool useGlobalsForAutomaticVariables() const { return false; }
461 /// getCFStringSection - Return the section to use for CFString
462 /// literals, or 0 if no special section is used.
463 virtual const char *getCFStringSection() const {
464 return "__DATA,__cfstring";
467 /// getNSStringSection - Return the section to use for NSString
468 /// literals, or 0 if no special section is used.
469 virtual const char *getNSStringSection() const {
470 return "__OBJC,__cstring_object,regular,no_dead_strip";
473 /// getNSStringNonFragileABISection - Return the section to use for
474 /// NSString literals, or 0 if no special section is used (NonFragile ABI).
475 virtual const char *getNSStringNonFragileABISection() const {
476 return "__DATA, __objc_stringobj, regular, no_dead_strip";
479 /// isValidSectionSpecifier - This is an optional hook that targets can
480 /// implement to perform semantic checking on attribute((section("foo")))
481 /// specifiers. In this case, "foo" is passed in to be checked. If the
482 /// section specifier is invalid, the backend should return a non-empty string
483 /// that indicates the problem.
485 /// This hook is a simple quality of implementation feature to catch errors
486 /// and give good diagnostics in cases when the assembler or code generator
487 /// would otherwise reject the section specifier.
489 virtual std::string isValidSectionSpecifier(StringRef SR) const {
493 /// setForcedLangOptions - Set forced language options.
494 /// Apply changes to the target information with respect to certain
495 /// language options which change the target configuration.
496 virtual void setForcedLangOptions(LangOptions &Opts);
498 /// getDefaultFeatures - Get the default set of target features for the CPU;
499 /// this should include all legal feature strings on the target.
500 virtual void getDefaultFeatures(llvm::StringMap<bool> &Features) const {
503 /// getABI - Get the ABI in use.
504 virtual const char *getABI() const {
508 /// getCXXABI - Get the C++ ABI in use.
509 virtual TargetCXXABI getCXXABI() const {
513 /// setCPU - Target the specific CPU.
515 /// \return - False on error (invalid CPU name).
516 virtual bool setCPU(const std::string &Name) {
520 /// setABI - Use the specific ABI.
522 /// \return - False on error (invalid ABI name).
523 virtual bool setABI(const std::string &Name) {
527 /// setCXXABI - Use this specific C++ ABI.
529 /// \return - False on error (invalid C++ ABI name).
530 bool setCXXABI(const std::string &Name) {
531 static const TargetCXXABI Unknown = static_cast<TargetCXXABI>(-1);
532 TargetCXXABI ABI = llvm::StringSwitch<TargetCXXABI>(Name)
533 .Case("arm", CXXABI_ARM)
534 .Case("itanium", CXXABI_Itanium)
535 .Case("microsoft", CXXABI_Microsoft)
537 if (ABI == Unknown) return false;
538 return setCXXABI(ABI);
541 /// setCXXABI - Set the C++ ABI to be used by this implementation.
543 /// \return - False on error (ABI not valid on this target)
544 virtual bool setCXXABI(TargetCXXABI ABI) {
549 /// setFeatureEnabled - Enable or disable a specific target feature,
550 /// the feature name must be valid.
552 /// \return - False on error (invalid feature name).
553 virtual bool setFeatureEnabled(llvm::StringMap<bool> &Features,
554 const std::string &Name,
555 bool Enabled) const {
559 /// HandleTargetOptions - Perform initialization based on the user configured
560 /// set of features (e.g., +sse4). The list is guaranteed to have at most one
561 /// entry per feature.
563 /// The target may modify the features list, to change which options are
564 /// passed onwards to the backend.
565 virtual void HandleTargetFeatures(std::vector<std::string> &Features) {
568 // getRegParmMax - Returns maximal number of args passed in registers.
569 unsigned getRegParmMax() const {
570 assert(RegParmMax < 7 && "RegParmMax value is larger than AST can handle");
574 /// isTLSSupported - Whether the target supports thread-local storage.
575 bool isTLSSupported() const {
579 /// hasNoAsmVariants - Return true if {|} are normal characters in the
580 /// asm string. If this returns false (the default), then {abc|xyz} is syntax
581 /// that says that when compiling for asm variant #0, "abc" should be
582 /// generated, but when compiling for asm variant #1, "xyz" should be
584 bool hasNoAsmVariants() const {
585 return NoAsmVariants;
588 /// getEHDataRegisterNumber - Return the register number that
589 /// __builtin_eh_return_regno would return with the specified argument.
590 virtual int getEHDataRegisterNumber(unsigned RegNo) const {
594 /// getStaticInitSectionSpecifier - Return the section to use for C++ static
595 /// initialization functions.
596 virtual const char *getStaticInitSectionSpecifier() const {
600 const LangAS::Map &getAddressSpaceMap() const {
601 return *AddrSpaceMap;
604 /// \brief Retrieve the name of the platform as it is used in the
605 /// availability attribute.
606 StringRef getPlatformName() const { return PlatformName; }
608 /// \brief Retrieve the minimum desired version of the platform, to
609 /// which the program should be compiled.
610 VersionTuple getPlatformMinVersion() const { return PlatformMinVersion; }
613 virtual uint64_t getPointerWidthV(unsigned AddrSpace) const {
616 virtual uint64_t getPointerAlignV(unsigned AddrSpace) const {
619 virtual enum IntType getPtrDiffTypeV(unsigned AddrSpace) const {
622 virtual void getGCCRegNames(const char * const *&Names,
623 unsigned &NumNames) const = 0;
624 virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
625 unsigned &NumAliases) const = 0;
626 virtual void getGCCAddlRegNames(const AddlRegName *&Addl,
627 unsigned &NumAddl) const {
631 virtual bool validateAsmConstraint(const char *&Name,
632 TargetInfo::ConstraintInfo &info) const= 0;
635 } // end namespace clang