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 "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"
41 namespace Builtin { struct Info; }
43 /// TargetCXXABI - The types of C++ ABIs for which we can generate code.
45 /// The generic ("Itanium") C++ ABI, documented at:
46 /// http://www.codesourcery.com/public/cxx-abi/
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
55 /// The Visual Studio ABI. Only scattered official documentation exists.
59 /// TargetInfo - This class exposes information about the current target.
61 class TargetInfo : public llvm::RefCountedBase<TargetInfo> {
64 // Target values set by the ctor of the actual target implementation. Default
65 // values are specified by the TargetInfo constructor.
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;
83 const LangAS::Map *AddrSpaceMap;
85 mutable llvm::StringRef PlatformName;
86 mutable VersionTuple PlatformMinVersion;
88 unsigned HasAlignMac68kSupport : 1;
89 unsigned RealTypeUsesObjCFPRet : 3;
91 // TargetInfo Constructor. Default initializes all fields.
92 TargetInfo(const std::string &T);
95 /// CreateTargetInfo - Construct a target for the given options.
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);
102 virtual ~TargetInfo();
104 ///===---- Target Data Type Query Methods -------------------------------===//
124 IntType SizeType, IntMaxType, UIntMaxType, PtrDiffType, IntPtrType, WCharType,
125 WIntType, Char16Type, Char32Type, Int64Type, SigAtomicType;
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
132 unsigned UseBitFieldTypeAlignment : 1;
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);
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; }
150 /// getTypeWidth - Return the width (in bits) of the specified integer type
151 /// enum. For example, SignedInt -> getIntWidth().
152 unsigned getTypeWidth(IntType T) const;
154 /// getTypeAlign - Return the alignment (in bits) of the specified integer
155 /// type enum. For example, SignedInt -> getIntAlign().
156 unsigned getTypeAlign(IntType T) const;
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);
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);
167 uint64_t getPointerAlign(unsigned AddrSpace) const {
168 return AddrSpace == 0 ? PointerAlign : getPointerAlignV(AddrSpace);
171 /// getBoolWidth/Align - Return the size of '_Bool' and C++ 'bool' for this
173 unsigned getBoolWidth() const { return BoolWidth; }
174 unsigned getBoolAlign() const { return BoolAlign; }
176 unsigned getCharWidth() const { return 8; } // FIXME
177 unsigned getCharAlign() const { return 8; } // FIXME
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
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; }
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; }
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; }
199 /// getWCharWidth/Align - Return the size of 'wchar_t' for this target, in
201 unsigned getWCharWidth() const { return getTypeWidth(WCharType); }
202 unsigned getWCharAlign() const { return getTypeAlign(WCharType); }
204 /// getChar16Width/Align - Return the size of 'char16_t' for this target, in
206 unsigned getChar16Width() const { return getTypeWidth(Char16Type); }
207 unsigned getChar16Align() const { return getTypeAlign(Char16Type); }
209 /// getChar32Width/Align - Return the size of 'char32_t' for this target, in
211 unsigned getChar32Width() const { return getTypeWidth(Char32Type); }
212 unsigned getChar32Align() const { return getTypeAlign(Char32Type); }
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; }
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; }
224 /// getLongDoubleWidth/Align/Format - Return the size/align/format of 'long
226 unsigned getLongDoubleWidth() const { return LongDoubleWidth; }
227 unsigned getLongDoubleAlign() const { return LongDoubleAlign; }
228 const llvm::fltSemantics &getLongDoubleFormat() const {
229 return *LongDoubleFormat;
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; }
237 /// getIntMaxTWidth - Return the size of intmax_t and uintmax_t for this
239 unsigned getIntMaxTWidth() const {
240 return getTypeWidth(IntMaxType);
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
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
255 const char *getUserLabelPrefix() const {
256 return UserLabelPrefix;
259 /// MCountName - This returns name of the mcount instrumentation function.
260 const char *getMCountName() const {
264 bool useBitFieldTypeAlignment() const {
265 return UseBitFieldTypeAlignment;
268 /// hasAlignMac68kSupport - Check whether this target support '#pragma options
270 bool hasAlignMac68kSupport() const {
271 return HasAlignMac68kSupport;
274 /// getTypeName - Return the user string for the specified integer type enum.
275 /// For example, SignedShort -> "short".
276 static const char *getTypeName(IntType T);
278 /// getTypeConstantSuffix - Return the constant suffix for the specified
279 /// integer type enum. For example, SignedLong -> "L".
280 static const char *getTypeConstantSuffix(IntType T);
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);
288 ///===---- Other target property query methods --------------------------===//
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;
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;
302 /// getVAListDeclaration - Return the declaration to use for
303 /// __builtin_va_list, which is target-specific.
304 virtual const char *getVAListDeclaration() const = 0;
306 /// isValidClobber - Returns whether the passed in string is
307 /// a valid clobber in an inline asm statement. This is used by
309 bool isValidClobber(llvm::StringRef Name) const;
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;
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;
320 struct ConstraintInfo {
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.
331 std::string ConstraintStr; // constraint: "=rm"
332 std::string Name; // Operand name: [foo] with no []'s.
334 ConstraintInfo(llvm::StringRef ConstraintStr, llvm::StringRef Name)
335 : Flags(0), TiedOperand(-1), ConstraintStr(ConstraintStr.str()),
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; }
344 /// hasMatchingInput - Return true if this output operand has a matching
345 /// (tied) input operand.
346 bool hasMatchingInput() const { return (Flags & CI_HasMatchingInput) != 0; }
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;
357 void setIsReadWrite() { Flags |= CI_ReadWrite; }
358 void setAllowsMemory() { Flags |= CI_AllowsMemory; }
359 void setAllowsRegister() { Flags |= CI_AllowsRegister; }
360 void setHasMatchingInput() { Flags |= CI_HasMatchingInput; }
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;
369 // Don't copy Name or constraint string.
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,
379 ConstraintInfo &info) const;
380 bool resolveSymbolicName(const char *&Name,
381 ConstraintInfo *OutputConstraints,
382 unsigned NumOutputs, unsigned &Index) const;
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);
394 // Returns a string of target-specific clobbers, in LLVM format.
395 virtual const char *getClobbers() const = 0;
398 /// getTriple - Return the target triple of the primary target.
399 const llvm::Triple &getTriple() const {
403 const char *getTargetDescription() const {
404 return DescriptionString;
408 const char * const Aliases[5];
409 const char * const Register;
413 const char * const Names[5];
414 const unsigned RegNum;
417 virtual bool useGlobalsForAutomaticVariables() const { return false; }
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";
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";
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";
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.
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.
447 virtual std::string isValidSectionSpecifier(llvm::StringRef SR) const {
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);
456 /// getDefaultFeatures - Get the default set of target features for
457 /// the \args CPU; this should include all legal feature strings on
459 virtual void getDefaultFeatures(const std::string &CPU,
460 llvm::StringMap<bool> &Features) const {
463 /// getABI - Get the ABI in use.
464 virtual const char *getABI() const {
468 /// getCXXABI - Get the C++ ABI in use.
469 virtual TargetCXXABI getCXXABI() const {
473 /// setCPU - Target the specific CPU.
475 /// \return - False on error (invalid CPU name).
477 // FIXME: Remove this.
478 virtual bool setCPU(const std::string &Name) {
482 /// setABI - Use the specific ABI.
484 /// \return - False on error (invalid ABI name).
485 virtual bool setABI(const std::string &Name) {
489 /// setCXXABI - Use this specific C++ ABI.
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)
499 if (ABI == Unknown) return false;
500 return setCXXABI(ABI);
503 /// setCXXABI - Set the C++ ABI to be used by this implementation.
505 /// \return - False on error (ABI not valid on this target)
506 virtual bool setCXXABI(TargetCXXABI ABI) {
511 /// setFeatureEnabled - Enable or disable a specific target feature,
512 /// the feature name must be valid.
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 {
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.
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) {
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");
536 /// isTLSSupported - Whether the target supports thread-local storage.
537 bool isTLSSupported() const {
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
546 bool hasNoAsmVariants() const {
547 return NoAsmVariants;
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 {
556 /// getStaticInitSectionSpecifier - Return the section to use for C++ static
557 /// initialization functions.
558 virtual const char *getStaticInitSectionSpecifier() const {
562 const LangAS::Map &getAddressSpaceMap() const {
563 return *AddrSpaceMap;
566 /// \brief Retrieve the name of the platform as it is used in the
567 /// availability attribute.
568 llvm::StringRef getPlatformName() const { return PlatformName; }
570 /// \brief Retrieve the minimum desired version of the platform, to
571 /// which the program should be compiled.
572 VersionTuple getPlatformMinVersion() const { return PlatformMinVersion; }
575 virtual uint64_t getPointerWidthV(unsigned AddrSpace) const {
578 virtual uint64_t getPointerAlignV(unsigned AddrSpace) const {
581 virtual enum IntType getPtrDiffTypeV(unsigned AddrSpace) const {
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 {
593 virtual bool validateAsmConstraint(const char *&Name,
594 TargetInfo::ConstraintInfo &info) const= 0;
597 } // end namespace clang