TargetInfo.h revision 249423
1193326Sed//===--- TargetInfo.h - Expose information about the target -----*- C++ -*-===//
2193326Sed//
3193326Sed//                     The LLVM Compiler Infrastructure
4193326Sed//
5193326Sed// This file is distributed under the University of Illinois Open Source
6193326Sed// License. See LICENSE.TXT for details.
7193326Sed//
8193326Sed//===----------------------------------------------------------------------===//
9239462Sdim///
10239462Sdim/// \file
11239462Sdim/// \brief Defines the clang::TargetInfo interface.
12239462Sdim///
13193326Sed//===----------------------------------------------------------------------===//
14193326Sed
15193326Sed#ifndef LLVM_CLANG_BASIC_TARGETINFO_H
16193326Sed#define LLVM_CLANG_BASIC_TARGETINFO_H
17193326Sed
18249423Sdim#include "clang/Basic/AddressSpaces.h"
19249423Sdim#include "clang/Basic/TargetCXXABI.h"
20226633Sdim#include "clang/Basic/LLVM.h"
21249423Sdim#include "clang/Basic/Specifiers.h"
22249423Sdim#include "clang/Basic/TargetOptions.h"
23249423Sdim#include "clang/Basic/VersionTuple.h"
24221345Sdim#include "llvm/ADT/IntrusiveRefCntPtr.h"
25193326Sed#include "llvm/ADT/StringMap.h"
26221345Sdim#include "llvm/ADT/StringRef.h"
27212904Sdim#include "llvm/ADT/StringSwitch.h"
28198092Srdivacky#include "llvm/ADT/Triple.h"
29218893Sdim#include "llvm/Support/DataTypes.h"
30193326Sed#include <cassert>
31249423Sdim#include <string>
32193326Sed#include <vector>
33193326Sed
34198092Srdivackynamespace llvm {
35198092Srdivackystruct fltSemantics;
36198092Srdivacky}
37193326Sed
38193326Sednamespace clang {
39226633Sdimclass DiagnosticsEngine;
40199482Srdivackyclass LangOptions;
41202379Srdivackyclass MacroBuilder;
42198092Srdivackyclass SourceLocation;
43193326Sedclass SourceManager;
44199482Srdivacky
45193326Sednamespace Builtin { struct Info; }
46198092Srdivacky
47239462Sdim/// \brief Exposes information about the current target.
48193326Sed///
49234353Sdimclass TargetInfo : public RefCountedBase<TargetInfo> {
50249423Sdim  IntrusiveRefCntPtr<TargetOptions> TargetOpts;
51198092Srdivacky  llvm::Triple Triple;
52193326Sedprotected:
53193326Sed  // Target values set by the ctor of the actual target implementation.  Default
54193326Sed  // values are specified by the TargetInfo constructor.
55234353Sdim  bool BigEndian;
56193326Sed  bool TLSSupported;
57207619Srdivacky  bool NoAsmVariants;  // True if {|} are normal characters.
58193326Sed  unsigned char PointerWidth, PointerAlign;
59218893Sdim  unsigned char BoolWidth, BoolAlign;
60193326Sed  unsigned char IntWidth, IntAlign;
61226633Sdim  unsigned char HalfWidth, HalfAlign;
62193326Sed  unsigned char FloatWidth, FloatAlign;
63193326Sed  unsigned char DoubleWidth, DoubleAlign;
64193326Sed  unsigned char LongDoubleWidth, LongDoubleAlign;
65210299Sed  unsigned char LargeArrayMinWidth, LargeArrayAlign;
66193326Sed  unsigned char LongWidth, LongAlign;
67193326Sed  unsigned char LongLongWidth, LongLongAlign;
68234353Sdim  unsigned char SuitableAlign;
69226633Sdim  unsigned char MaxAtomicPromoteWidth, MaxAtomicInlineWidth;
70239462Sdim  unsigned short MaxVectorAlign;
71193326Sed  const char *DescriptionString;
72193326Sed  const char *UserLabelPrefix;
73218893Sdim  const char *MCountName;
74226633Sdim  const llvm::fltSemantics *HalfFormat, *FloatFormat, *DoubleFormat,
75226633Sdim    *LongDoubleFormat;
76193326Sed  unsigned char RegParmMax, SSERegParmMax;
77249423Sdim  TargetCXXABI TheCXXABI;
78221345Sdim  const LangAS::Map *AddrSpaceMap;
79193326Sed
80226633Sdim  mutable StringRef PlatformName;
81221345Sdim  mutable VersionTuple PlatformMinVersion;
82221345Sdim
83208600Srdivacky  unsigned HasAlignMac68kSupport : 1;
84210299Sed  unsigned RealTypeUsesObjCFPRet : 3;
85234353Sdim  unsigned ComplexLongDoubleUsesFP2Ret : 1;
86208600Srdivacky
87193326Sed  // TargetInfo Constructor.  Default initializes all fields.
88193326Sed  TargetInfo(const std::string &T);
89198092Srdivacky
90198092Srdivackypublic:
91239462Sdim  /// \brief Construct a target for the given options.
92201361Srdivacky  ///
93201361Srdivacky  /// \param Opts - The options to use to initialize the target. The target may
94201361Srdivacky  /// modify the options to canonicalize the target feature information to match
95201361Srdivacky  /// what the backend expects.
96226633Sdim  static TargetInfo* CreateTargetInfo(DiagnosticsEngine &Diags,
97249423Sdim                                      TargetOptions *Opts);
98193326Sed
99193326Sed  virtual ~TargetInfo();
100193326Sed
101243830Sdim  /// \brief Retrieve the target options.
102243830Sdim  TargetOptions &getTargetOpts() const {
103243830Sdim    assert(TargetOpts && "Missing target options");
104243830Sdim    return *TargetOpts;
105243830Sdim  }
106243830Sdim
107249423Sdim  void setTargetOpts(TargetOptions *TargetOpts) {
108249423Sdim    this->TargetOpts = TargetOpts;
109243830Sdim  }
110243830Sdim
111193326Sed  ///===---- Target Data Type Query Methods -------------------------------===//
112193326Sed  enum IntType {
113193326Sed    NoInt = 0,
114193326Sed    SignedShort,
115193326Sed    UnsignedShort,
116193326Sed    SignedInt,
117193326Sed    UnsignedInt,
118193326Sed    SignedLong,
119193326Sed    UnsignedLong,
120193326Sed    SignedLongLong,
121193326Sed    UnsignedLongLong
122193326Sed  };
123210299Sed
124210299Sed  enum RealType {
125210299Sed    Float = 0,
126210299Sed    Double,
127210299Sed    LongDouble
128210299Sed  };
129210299Sed
130239462Sdim  /// \brief The different kinds of __builtin_va_list types defined by
131239462Sdim  /// the target implementation.
132239462Sdim  enum BuiltinVaListKind {
133239462Sdim    /// typedef char* __builtin_va_list;
134239462Sdim    CharPtrBuiltinVaList = 0,
135239462Sdim
136239462Sdim    /// typedef void* __builtin_va_list;
137239462Sdim    VoidPtrBuiltinVaList,
138239462Sdim
139249423Sdim    /// __builtin_va_list as defind by the AArch64 ABI
140249423Sdim    /// http://infocenter.arm.com/help/topic/com.arm.doc.ihi0055a/IHI0055A_aapcs64.pdf
141249423Sdim    AArch64ABIBuiltinVaList,
142249423Sdim
143239462Sdim    /// __builtin_va_list as defined by the PNaCl ABI:
144239462Sdim    /// http://www.chromium.org/nativeclient/pnacl/bitcode-abi#TOC-Machine-Types
145239462Sdim    PNaClABIBuiltinVaList,
146239462Sdim
147239462Sdim    /// __builtin_va_list as defined by the Power ABI:
148239462Sdim    /// https://www.power.org
149239462Sdim    ///        /resources/downloads/Power-Arch-32-bit-ABI-supp-1.0-Embedded.pdf
150239462Sdim    PowerABIBuiltinVaList,
151239462Sdim
152239462Sdim    /// __builtin_va_list as defined by the x86-64 ABI:
153239462Sdim    /// http://www.x86-64.org/documentation/abi.pdf
154243830Sdim    X86_64ABIBuiltinVaList,
155243830Sdim
156243830Sdim    /// __builtin_va_list as defined by ARM AAPCS ABI
157243830Sdim    /// http://infocenter.arm.com
158243830Sdim    //        /help/topic/com.arm.doc.ihi0042d/IHI0042D_aapcs.pdf
159243830Sdim    AAPCSABIBuiltinVaList
160239462Sdim  };
161239462Sdim
162193326Sedprotected:
163195341Sed  IntType SizeType, IntMaxType, UIntMaxType, PtrDiffType, IntPtrType, WCharType,
164243830Sdim          WIntType, Char16Type, Char32Type, Int64Type, SigAtomicType,
165243830Sdim          ProcessIDType;
166207619Srdivacky
167239462Sdim  /// \brief Whether Objective-C's built-in boolean type should be signed char.
168239462Sdim  ///
169234982Sdim  /// Otherwise, when this flag is not set, the normal built-in boolean type is
170234982Sdim  /// used.
171234982Sdim  unsigned UseSignedCharForObjCBool : 1;
172234982Sdim
173207619Srdivacky  /// Control whether the alignment of bit-field types is respected when laying
174207619Srdivacky  /// out structures. If true, then the alignment of the bit-field type will be
175207619Srdivacky  /// used to (a) impact the alignment of the containing structure, and (b)
176207619Srdivacky  /// ensure that the individual bit-field will not straddle an alignment
177207619Srdivacky  /// boundary.
178207619Srdivacky  unsigned UseBitFieldTypeAlignment : 1;
179207619Srdivacky
180239462Sdim  /// \brief Whether zero length bitfields (e.g., int : 0;) force alignment of
181239462Sdim  /// the next bitfield.
182239462Sdim  ///
183239462Sdim  /// If the alignment of the zero length bitfield is greater than the member
184239462Sdim  /// that follows it, `bar', `bar' will be aligned as the type of the
185239462Sdim  /// zero-length bitfield.
186226633Sdim  unsigned UseZeroLengthBitfieldAlignment : 1;
187226633Sdim
188226633Sdim  /// If non-zero, specifies a fixed alignment value for bitfields that follow
189226633Sdim  /// zero length bitfield, regardless of the zero length bitfield type.
190226633Sdim  unsigned ZeroLengthBitfieldBoundary;
191226633Sdim
192193326Sedpublic:
193193326Sed  IntType getSizeType() const { return SizeType; }
194193326Sed  IntType getIntMaxType() const { return IntMaxType; }
195193326Sed  IntType getUIntMaxType() const { return UIntMaxType; }
196193326Sed  IntType getPtrDiffType(unsigned AddrSpace) const {
197193326Sed    return AddrSpace == 0 ? PtrDiffType : getPtrDiffTypeV(AddrSpace);
198193326Sed  }
199193326Sed  IntType getIntPtrType() const { return IntPtrType; }
200193326Sed  IntType getWCharType() const { return WCharType; }
201198398Srdivacky  IntType getWIntType() const { return WIntType; }
202198092Srdivacky  IntType getChar16Type() const { return Char16Type; }
203198092Srdivacky  IntType getChar32Type() const { return Char32Type; }
204195341Sed  IntType getInt64Type() const { return Int64Type; }
205199990Srdivacky  IntType getSigAtomicType() const { return SigAtomicType; }
206243830Sdim  IntType getProcessIDType() const { return ProcessIDType; }
207193326Sed
208239462Sdim  /// \brief Return the width (in bits) of the specified integer type enum.
209239462Sdim  ///
210239462Sdim  /// For example, SignedInt -> getIntWidth().
211198398Srdivacky  unsigned getTypeWidth(IntType T) const;
212198398Srdivacky
213239462Sdim  /// \brief Return the alignment (in bits) of the specified integer type enum.
214239462Sdim  ///
215239462Sdim  /// For example, SignedInt -> getIntAlign().
216199482Srdivacky  unsigned getTypeAlign(IntType T) const;
217199482Srdivacky
218239462Sdim  /// \brief Returns true if the type is signed; false otherwise.
219218893Sdim  static bool isTypeSigned(IntType T);
220198398Srdivacky
221239462Sdim  /// \brief Return the width of pointers on this target, for the
222193326Sed  /// specified address space.
223193326Sed  uint64_t getPointerWidth(unsigned AddrSpace) const {
224193326Sed    return AddrSpace == 0 ? PointerWidth : getPointerWidthV(AddrSpace);
225193326Sed  }
226193326Sed  uint64_t getPointerAlign(unsigned AddrSpace) const {
227193326Sed    return AddrSpace == 0 ? PointerAlign : getPointerAlignV(AddrSpace);
228193326Sed  }
229198092Srdivacky
230239462Sdim  /// \brief Return the size of '_Bool' and C++ 'bool' for this target, in bits.
231218893Sdim  unsigned getBoolWidth() const { return BoolWidth; }
232239462Sdim
233239462Sdim  /// \brief Return the alignment of '_Bool' and C++ 'bool' for this target.
234218893Sdim  unsigned getBoolAlign() const { return BoolAlign; }
235198092Srdivacky
236198092Srdivacky  unsigned getCharWidth() const { return 8; } // FIXME
237198092Srdivacky  unsigned getCharAlign() const { return 8; } // FIXME
238198092Srdivacky
239239462Sdim  /// \brief Return the size of 'signed short' and 'unsigned short' for this
240239462Sdim  /// target, in bits.
241193326Sed  unsigned getShortWidth() const { return 16; } // FIXME
242239462Sdim
243239462Sdim  /// \brief Return the alignment of 'signed short' and 'unsigned short' for
244239462Sdim  /// this target.
245193326Sed  unsigned getShortAlign() const { return 16; } // FIXME
246198092Srdivacky
247193326Sed  /// getIntWidth/Align - Return the size of 'signed int' and 'unsigned int' for
248193326Sed  /// this target, in bits.
249193326Sed  unsigned getIntWidth() const { return IntWidth; }
250193326Sed  unsigned getIntAlign() const { return IntAlign; }
251198092Srdivacky
252193326Sed  /// getLongWidth/Align - Return the size of 'signed long' and 'unsigned long'
253193326Sed  /// for this target, in bits.
254193326Sed  unsigned getLongWidth() const { return LongWidth; }
255193326Sed  unsigned getLongAlign() const { return LongAlign; }
256198092Srdivacky
257193326Sed  /// getLongLongWidth/Align - Return the size of 'signed long long' and
258193326Sed  /// 'unsigned long long' for this target, in bits.
259193326Sed  unsigned getLongLongWidth() const { return LongLongWidth; }
260193326Sed  unsigned getLongLongAlign() const { return LongLongAlign; }
261198092Srdivacky
262249423Sdim  /// \brief Determine whether the __int128 type is supported on this target.
263249423Sdim  bool hasInt128Type() const { return getPointerWidth(0) >= 64; } // FIXME
264249423Sdim
265239462Sdim  /// \brief Return the alignment that is suitable for storing any
266234353Sdim  /// object with a fundamental alignment requirement.
267234353Sdim  unsigned getSuitableAlign() const { return SuitableAlign; }
268234353Sdim
269198092Srdivacky  /// getWCharWidth/Align - Return the size of 'wchar_t' for this target, in
270193326Sed  /// bits.
271199482Srdivacky  unsigned getWCharWidth() const { return getTypeWidth(WCharType); }
272199482Srdivacky  unsigned getWCharAlign() const { return getTypeAlign(WCharType); }
273193326Sed
274198092Srdivacky  /// getChar16Width/Align - Return the size of 'char16_t' for this target, in
275198092Srdivacky  /// bits.
276199482Srdivacky  unsigned getChar16Width() const { return getTypeWidth(Char16Type); }
277199482Srdivacky  unsigned getChar16Align() const { return getTypeAlign(Char16Type); }
278198092Srdivacky
279198092Srdivacky  /// getChar32Width/Align - Return the size of 'char32_t' for this target, in
280198092Srdivacky  /// bits.
281199482Srdivacky  unsigned getChar32Width() const { return getTypeWidth(Char32Type); }
282199482Srdivacky  unsigned getChar32Align() const { return getTypeAlign(Char32Type); }
283198092Srdivacky
284226633Sdim  /// getHalfWidth/Align/Format - Return the size/align/format of 'half'.
285226633Sdim  unsigned getHalfWidth() const { return HalfWidth; }
286226633Sdim  unsigned getHalfAlign() const { return HalfAlign; }
287226633Sdim  const llvm::fltSemantics &getHalfFormat() const { return *HalfFormat; }
288226633Sdim
289193326Sed  /// getFloatWidth/Align/Format - Return the size/align/format of 'float'.
290193326Sed  unsigned getFloatWidth() const { return FloatWidth; }
291193326Sed  unsigned getFloatAlign() const { return FloatAlign; }
292193326Sed  const llvm::fltSemantics &getFloatFormat() const { return *FloatFormat; }
293193326Sed
294193326Sed  /// getDoubleWidth/Align/Format - Return the size/align/format of 'double'.
295193326Sed  unsigned getDoubleWidth() const { return DoubleWidth; }
296193326Sed  unsigned getDoubleAlign() const { return DoubleAlign; }
297193326Sed  const llvm::fltSemantics &getDoubleFormat() const { return *DoubleFormat; }
298193326Sed
299193326Sed  /// getLongDoubleWidth/Align/Format - Return the size/align/format of 'long
300193326Sed  /// double'.
301193326Sed  unsigned getLongDoubleWidth() const { return LongDoubleWidth; }
302193326Sed  unsigned getLongDoubleAlign() const { return LongDoubleAlign; }
303193326Sed  const llvm::fltSemantics &getLongDoubleFormat() const {
304193326Sed    return *LongDoubleFormat;
305193326Sed  }
306198092Srdivacky
307239462Sdim  /// \brief Return the value for the C99 FLT_EVAL_METHOD macro.
308234353Sdim  virtual unsigned getFloatEvalMethod() const { return 0; }
309234353Sdim
310210299Sed  // getLargeArrayMinWidth/Align - Return the minimum array size that is
311210299Sed  // 'large' and its alignment.
312210299Sed  unsigned getLargeArrayMinWidth() const { return LargeArrayMinWidth; }
313210299Sed  unsigned getLargeArrayAlign() const { return LargeArrayAlign; }
314210299Sed
315239462Sdim  /// \brief Return the maximum width lock-free atomic operation which will
316239462Sdim  /// ever be supported for the given target
317226633Sdim  unsigned getMaxAtomicPromoteWidth() const { return MaxAtomicPromoteWidth; }
318239462Sdim  /// \brief Return the maximum width lock-free atomic operation which can be
319239462Sdim  /// inlined given the supported features of the given target.
320226633Sdim  unsigned getMaxAtomicInlineWidth() const { return MaxAtomicInlineWidth; }
321226633Sdim
322239462Sdim  /// \brief Return the maximum vector alignment supported for the given target.
323239462Sdim  unsigned getMaxVectorAlign() const { return MaxVectorAlign; }
324239462Sdim
325239462Sdim  /// \brief Return the size of intmax_t and uintmax_t for this target, in bits.
326193326Sed  unsigned getIntMaxTWidth() const {
327199482Srdivacky    return getTypeWidth(IntMaxType);
328193326Sed  }
329198092Srdivacky
330249423Sdim  // Return the size of unwind_word for this target.
331249423Sdim  unsigned getUnwindWordWidth() const { return getPointerWidth(0); }
332249423Sdim
333239462Sdim  /// \brief Return the "preferred" register width on this target.
334224145Sdim  uint64_t getRegisterWidth() const {
335224145Sdim    // Currently we assume the register width on the target matches the pointer
336224145Sdim    // width, we can introduce a new variable for this if/when some target wants
337224145Sdim    // it.
338224145Sdim    return LongWidth;
339224145Sdim  }
340224145Sdim
341239462Sdim  /// \brief Returns the default value of the __USER_LABEL_PREFIX__ macro,
342239462Sdim  /// which is the prefix given to user symbols by default.
343239462Sdim  ///
344239462Sdim  /// On most platforms this is "_", but it is "" on some, and "." on others.
345193326Sed  const char *getUserLabelPrefix() const {
346193326Sed    return UserLabelPrefix;
347193326Sed  }
348198092Srdivacky
349239462Sdim  /// \brief Returns the name of the mcount instrumentation function.
350218893Sdim  const char *getMCountName() const {
351218893Sdim    return MCountName;
352218893Sdim  }
353218893Sdim
354239462Sdim  /// \brief Check if the Objective-C built-in boolean type should be signed
355239462Sdim  /// char.
356239462Sdim  ///
357239462Sdim  /// Otherwise, if this returns false, the normal built-in boolean type
358239462Sdim  /// should also be used for Objective-C.
359234982Sdim  bool useSignedCharForObjCBool() const {
360234982Sdim    return UseSignedCharForObjCBool;
361234982Sdim  }
362234982Sdim  void noSignedCharForObjCBool() {
363234982Sdim    UseSignedCharForObjCBool = false;
364234982Sdim  }
365234982Sdim
366239462Sdim  /// \brief Check whether the alignment of bit-field types is respected
367239462Sdim  /// when laying out structures.
368207619Srdivacky  bool useBitFieldTypeAlignment() const {
369207619Srdivacky    return UseBitFieldTypeAlignment;
370207619Srdivacky  }
371207619Srdivacky
372239462Sdim  /// \brief Check whether zero length bitfields should force alignment of
373239462Sdim  /// the next member.
374226633Sdim  bool useZeroLengthBitfieldAlignment() const {
375226633Sdim    return UseZeroLengthBitfieldAlignment;
376226633Sdim  }
377226633Sdim
378239462Sdim  /// \brief Get the fixed alignment value in bits for a member that follows
379239462Sdim  /// a zero length bitfield.
380226633Sdim  unsigned getZeroLengthBitfieldBoundary() const {
381226633Sdim    return ZeroLengthBitfieldBoundary;
382226633Sdim  }
383226633Sdim
384239462Sdim  /// \brief Check whether this target support '\#pragma options align=mac68k'.
385208600Srdivacky  bool hasAlignMac68kSupport() const {
386208600Srdivacky    return HasAlignMac68kSupport;
387208600Srdivacky  }
388208600Srdivacky
389239462Sdim  /// \brief Return the user string for the specified integer type enum.
390239462Sdim  ///
391193326Sed  /// For example, SignedShort -> "short".
392193326Sed  static const char *getTypeName(IntType T);
393198092Srdivacky
394239462Sdim  /// \brief Return the constant suffix for the specified integer type enum.
395239462Sdim  ///
396239462Sdim  /// For example, SignedLong -> "L".
397198398Srdivacky  static const char *getTypeConstantSuffix(IntType T);
398198398Srdivacky
399210299Sed  /// \brief Check whether the given real type should use the "fpret" flavor of
400239462Sdim  /// Objective-C message passing on this target.
401210299Sed  bool useObjCFPRetForRealType(RealType T) const {
402210299Sed    return RealTypeUsesObjCFPRet & (1 << T);
403210299Sed  }
404210299Sed
405234353Sdim  /// \brief Check whether _Complex long double should use the "fp2ret" flavor
406239462Sdim  /// of Objective-C message passing on this target.
407234353Sdim  bool useObjCFP2RetForComplexLongDouble() const {
408234353Sdim    return ComplexLongDoubleUsesFP2Ret;
409234353Sdim  }
410234353Sdim
411193326Sed  ///===---- Other target property query methods --------------------------===//
412198092Srdivacky
413239462Sdim  /// \brief Appends the target-specific \#define values for this
414193326Sed  /// target set to the specified buffer.
415193326Sed  virtual void getTargetDefines(const LangOptions &Opts,
416202379Srdivacky                                MacroBuilder &Builder) const = 0;
417198092Srdivacky
418198398Srdivacky
419239462Sdim  /// Return information about target-specific builtins for
420193326Sed  /// the current primary target, and info about which builtins are non-portable
421193326Sed  /// across the current set of primary and secondary targets.
422198092Srdivacky  virtual void getTargetBuiltins(const Builtin::Info *&Records,
423193326Sed                                 unsigned &NumRecords) const = 0;
424193326Sed
425239462Sdim  /// The __builtin_clz* and __builtin_ctz* built-in
426234353Sdim  /// functions are specified to have undefined results for zero inputs, but
427234353Sdim  /// on targets that support these operations in a way that provides
428234353Sdim  /// well-defined results for zero without loss of performance, it is a good
429234353Sdim  /// idea to avoid optimizing based on that undef behavior.
430234353Sdim  virtual bool isCLZForZeroUndef() const { return true; }
431234353Sdim
432239462Sdim  /// \brief Returns the kind of __builtin_va_list type that should be used
433239462Sdim  /// with this target.
434239462Sdim  virtual BuiltinVaListKind getBuiltinVaListKind() const = 0;
435193326Sed
436239462Sdim  /// \brief Returns whether the passed in string is a valid clobber in an
437239462Sdim  /// inline asm statement.
438239462Sdim  ///
439239462Sdim  /// This is used by Sema.
440226633Sdim  bool isValidClobber(StringRef Name) const;
441224145Sdim
442239462Sdim  /// \brief Returns whether the passed in string is a valid register name
443239462Sdim  /// according to GCC.
444239462Sdim  ///
445239462Sdim  /// This is used by Sema for inline asm statements.
446226633Sdim  bool isValidGCCRegisterName(StringRef Name) const;
447193326Sed
448239462Sdim  /// \brief Returns the "normalized" GCC register name.
449239462Sdim  ///
450239462Sdim  /// For example, on x86 it will return "ax" when "eax" is passed in.
451226633Sdim  StringRef getNormalizedGCCRegisterName(StringRef Name) const;
452198092Srdivacky
453193326Sed  struct ConstraintInfo {
454193326Sed    enum {
455193326Sed      CI_None = 0x00,
456193326Sed      CI_AllowsMemory = 0x01,
457193326Sed      CI_AllowsRegister = 0x02,
458193326Sed      CI_ReadWrite = 0x04,       // "+r" output constraint (read and write).
459193326Sed      CI_HasMatchingInput = 0x08 // This output operand has a matching input.
460193326Sed    };
461193326Sed    unsigned Flags;
462193326Sed    int TiedOperand;
463198092Srdivacky
464193326Sed    std::string ConstraintStr;  // constraint: "=rm"
465193326Sed    std::string Name;           // Operand name: [foo] with no []'s.
466193326Sed  public:
467226633Sdim    ConstraintInfo(StringRef ConstraintStr, StringRef Name)
468218893Sdim      : Flags(0), TiedOperand(-1), ConstraintStr(ConstraintStr.str()),
469203955Srdivacky      Name(Name.str()) {}
470193326Sed
471193326Sed    const std::string &getConstraintStr() const { return ConstraintStr; }
472193326Sed    const std::string &getName() const { return Name; }
473193326Sed    bool isReadWrite() const { return (Flags & CI_ReadWrite) != 0; }
474193326Sed    bool allowsRegister() const { return (Flags & CI_AllowsRegister) != 0; }
475193326Sed    bool allowsMemory() const { return (Flags & CI_AllowsMemory) != 0; }
476198092Srdivacky
477239462Sdim    /// \brief Return true if this output operand has a matching
478193326Sed    /// (tied) input operand.
479193326Sed    bool hasMatchingInput() const { return (Flags & CI_HasMatchingInput) != 0; }
480198092Srdivacky
481239462Sdim    /// \brief Return true if this input operand is a matching
482239462Sdim    /// constraint that ties it to an output operand.
483239462Sdim    ///
484239462Sdim    /// If this returns true then getTiedOperand will indicate which output
485239462Sdim    /// operand this is tied to.
486193326Sed    bool hasTiedOperand() const { return TiedOperand != -1; }
487193326Sed    unsigned getTiedOperand() const {
488193326Sed      assert(hasTiedOperand() && "Has no tied operand!");
489193326Sed      return (unsigned)TiedOperand;
490193326Sed    }
491198092Srdivacky
492193326Sed    void setIsReadWrite() { Flags |= CI_ReadWrite; }
493193326Sed    void setAllowsMemory() { Flags |= CI_AllowsMemory; }
494193326Sed    void setAllowsRegister() { Flags |= CI_AllowsRegister; }
495193326Sed    void setHasMatchingInput() { Flags |= CI_HasMatchingInput; }
496198092Srdivacky
497239462Sdim    /// \brief Indicate that this is an input operand that is tied to
498239462Sdim    /// the specified output operand.
499239462Sdim    ///
500239462Sdim    /// Copy over the various constraint information from the output.
501193326Sed    void setTiedOperand(unsigned N, ConstraintInfo &Output) {
502193326Sed      Output.setHasMatchingInput();
503193326Sed      Flags = Output.Flags;
504193326Sed      TiedOperand = N;
505193326Sed      // Don't copy Name or constraint string.
506193326Sed    }
507193326Sed  };
508193326Sed
509193326Sed  // validateOutputConstraint, validateInputConstraint - Checks that
510193326Sed  // a constraint is valid and provides information about it.
511193326Sed  // FIXME: These should return a real error instead of just true/false.
512193326Sed  bool validateOutputConstraint(ConstraintInfo &Info) const;
513193326Sed  bool validateInputConstraint(ConstraintInfo *OutputConstraints,
514193326Sed                               unsigned NumOutputs,
515193326Sed                               ConstraintInfo &info) const;
516249423Sdim  virtual bool validateInputSize(StringRef /*Constraint*/,
517249423Sdim                                 unsigned /*Size*/) const {
518249423Sdim    return true;
519249423Sdim  }
520243830Sdim  virtual bool validateConstraintModifier(StringRef /*Constraint*/,
521243830Sdim                                          const char /*Modifier*/,
522243830Sdim                                          unsigned /*Size*/) const {
523243830Sdim    return true;
524243830Sdim  }
525193326Sed  bool resolveSymbolicName(const char *&Name,
526193326Sed                           ConstraintInfo *OutputConstraints,
527193326Sed                           unsigned NumOutputs, unsigned &Index) const;
528198092Srdivacky
529223017Sdim  // Constraint parm will be left pointing at the last character of
530223017Sdim  // the constraint.  In practice, it won't be changed unless the
531223017Sdim  // constraint is longer than one character.
532223017Sdim  virtual std::string convertConstraint(const char *&Constraint) const {
533218893Sdim    // 'p' defaults to 'r', but can be overridden by targets.
534223017Sdim    if (*Constraint == 'p')
535218893Sdim      return std::string("r");
536223017Sdim    return std::string(1, *Constraint);
537193326Sed  }
538198092Srdivacky
539239462Sdim  /// \brief Returns a string of target-specific clobbers, in LLVM format.
540193326Sed  virtual const char *getClobbers() const = 0;
541193326Sed
542198092Srdivacky
543239462Sdim  /// \brief Returns the target triple of the primary target.
544198092Srdivacky  const llvm::Triple &getTriple() const {
545198092Srdivacky    return Triple;
546193326Sed  }
547198092Srdivacky
548193326Sed  const char *getTargetDescription() const {
549193326Sed    return DescriptionString;
550193326Sed  }
551193326Sed
552193326Sed  struct GCCRegAlias {
553193326Sed    const char * const Aliases[5];
554193326Sed    const char * const Register;
555193326Sed  };
556193326Sed
557224145Sdim  struct AddlRegName {
558224145Sdim    const char * const Names[5];
559224145Sdim    const unsigned RegNum;
560224145Sdim  };
561224145Sdim
562239462Sdim  /// \brief Does this target support "protected" visibility?
563234353Sdim  ///
564234353Sdim  /// Any target which dynamic libraries will naturally support
565234353Sdim  /// something like "default" (meaning that the symbol is visible
566234353Sdim  /// outside this shared object) and "hidden" (meaning that it isn't)
567234353Sdim  /// visibilities, but "protected" is really an ELF-specific concept
568239462Sdim  /// with weird semantics designed around the convenience of dynamic
569234353Sdim  /// linker implementations.  Which is not to suggest that there's
570234353Sdim  /// consistent target-independent semantics for "default" visibility
571234353Sdim  /// either; the entire thing is pretty badly mangled.
572234353Sdim  virtual bool hasProtectedVisibility() const { return true; }
573234353Sdim
574239462Sdim  /// \brief Return the section to use for CFString literals, or 0 if no
575239462Sdim  /// special section is used.
576198092Srdivacky  virtual const char *getCFStringSection() const {
577193326Sed    return "__DATA,__cfstring";
578193326Sed  }
579193326Sed
580239462Sdim  /// \brief Return the section to use for NSString literals, or 0 if no
581239462Sdim  /// special section is used.
582207619Srdivacky  virtual const char *getNSStringSection() const {
583207619Srdivacky    return "__OBJC,__cstring_object,regular,no_dead_strip";
584207619Srdivacky  }
585207619Srdivacky
586239462Sdim  /// \brief Return the section to use for NSString literals, or 0 if no
587239462Sdim  /// special section is used (NonFragile ABI).
588207619Srdivacky  virtual const char *getNSStringNonFragileABISection() const {
589207619Srdivacky    return "__DATA, __objc_stringobj, regular, no_dead_strip";
590207619Srdivacky  }
591207619Srdivacky
592239462Sdim  /// \brief An optional hook that targets can implement to perform semantic
593239462Sdim  /// checking on attribute((section("foo"))) specifiers.
594239462Sdim  ///
595239462Sdim  /// In this case, "foo" is passed in to be checked.  If the section
596239462Sdim  /// specifier is invalid, the backend should return a non-empty string
597198092Srdivacky  /// that indicates the problem.
598198092Srdivacky  ///
599198092Srdivacky  /// This hook is a simple quality of implementation feature to catch errors
600198092Srdivacky  /// and give good diagnostics in cases when the assembler or code generator
601198092Srdivacky  /// would otherwise reject the section specifier.
602198092Srdivacky  ///
603226633Sdim  virtual std::string isValidSectionSpecifier(StringRef SR) const {
604198092Srdivacky    return "";
605193326Sed  }
606193326Sed
607239462Sdim  /// \brief Set forced language options.
608239462Sdim  ///
609199482Srdivacky  /// Apply changes to the target information with respect to certain
610199482Srdivacky  /// language options which change the target configuration.
611199482Srdivacky  virtual void setForcedLangOptions(LangOptions &Opts);
612193326Sed
613239462Sdim  /// \brief Get the default set of target features for the CPU;
614226633Sdim  /// this should include all legal feature strings on the target.
615226633Sdim  virtual void getDefaultFeatures(llvm::StringMap<bool> &Features) const {
616193326Sed  }
617193326Sed
618239462Sdim  /// \brief Get the ABI currently in use.
619198092Srdivacky  virtual const char *getABI() const {
620198092Srdivacky    return "";
621198092Srdivacky  }
622198092Srdivacky
623239462Sdim  /// \brief Get the C++ ABI currently in use.
624249423Sdim  TargetCXXABI getCXXABI() const {
625249423Sdim    return TheCXXABI;
626210299Sed  }
627210299Sed
628239462Sdim  /// \brief Target the specified CPU.
629201361Srdivacky  ///
630239462Sdim  /// \return  False on error (invalid CPU name).
631201361Srdivacky  virtual bool setCPU(const std::string &Name) {
632226633Sdim    return false;
633201361Srdivacky  }
634201361Srdivacky
635239462Sdim  /// \brief Use the specified ABI.
636198092Srdivacky  ///
637239462Sdim  /// \return False on error (invalid ABI name).
638198092Srdivacky  virtual bool setABI(const std::string &Name) {
639198092Srdivacky    return false;
640198092Srdivacky  }
641198092Srdivacky
642239462Sdim  /// \brief Use this specified C++ ABI.
643210299Sed  ///
644239462Sdim  /// \return False on error (invalid C++ ABI name).
645249423Sdim  bool setCXXABI(llvm::StringRef name) {
646249423Sdim    TargetCXXABI ABI;
647249423Sdim    if (!ABI.tryParse(name)) return false;
648212904Sdim    return setCXXABI(ABI);
649212904Sdim  }
650212904Sdim
651239462Sdim  /// \brief Set the C++ ABI to be used by this implementation.
652212904Sdim  ///
653239462Sdim  /// \return False on error (ABI not valid on this target)
654212904Sdim  virtual bool setCXXABI(TargetCXXABI ABI) {
655249423Sdim    TheCXXABI = ABI;
656210299Sed    return true;
657210299Sed  }
658210299Sed
659239462Sdim  /// \brief Enable or disable a specific target feature;
660193326Sed  /// the feature name must be valid.
661193326Sed  ///
662239462Sdim  /// \return False on error (invalid feature name).
663193326Sed  virtual bool setFeatureEnabled(llvm::StringMap<bool> &Features,
664234353Sdim                                 StringRef Name,
665193326Sed                                 bool Enabled) const {
666193326Sed    return false;
667193326Sed  }
668193326Sed
669239462Sdim  /// \brief Perform initialization based on the user configured
670239462Sdim  /// set of features (e.g., +sse4).
671201361Srdivacky  ///
672239462Sdim  /// The list is guaranteed to have at most one entry per feature.
673239462Sdim  ///
674201361Srdivacky  /// The target may modify the features list, to change which options are
675201361Srdivacky  /// passed onwards to the backend.
676201361Srdivacky  virtual void HandleTargetFeatures(std::vector<std::string> &Features) {
677193326Sed  }
678193326Sed
679234353Sdim  /// \brief Determine whether the given target has the given feature.
680234353Sdim  virtual bool hasFeature(StringRef Feature) const {
681234353Sdim    return false;
682234353Sdim  }
683234353Sdim
684239462Sdim  // \brief Returns maximal number of args passed in registers.
685193326Sed  unsigned getRegParmMax() const {
686224145Sdim    assert(RegParmMax < 7 && "RegParmMax value is larger than AST can handle");
687193326Sed    return RegParmMax;
688193326Sed  }
689193326Sed
690239462Sdim  /// \brief Whether the target supports thread-local storage.
691198092Srdivacky  bool isTLSSupported() const {
692193326Sed    return TLSSupported;
693193326Sed  }
694218893Sdim
695239462Sdim  /// \brief Return true if {|} are normal characters in the asm string.
696239462Sdim  ///
697239462Sdim  /// If this returns false (the default), then {abc|xyz} is syntax
698207619Srdivacky  /// that says that when compiling for asm variant #0, "abc" should be
699207619Srdivacky  /// generated, but when compiling for asm variant #1, "xyz" should be
700207619Srdivacky  /// generated.
701207619Srdivacky  bool hasNoAsmVariants() const {
702207619Srdivacky    return NoAsmVariants;
703207619Srdivacky  }
704218893Sdim
705239462Sdim  /// \brief Return the register number that __builtin_eh_return_regno would
706239462Sdim  /// return with the specified argument.
707198092Srdivacky  virtual int getEHDataRegisterNumber(unsigned RegNo) const {
708218893Sdim    return -1;
709198092Srdivacky  }
710218893Sdim
711239462Sdim  /// \brief Return the section to use for C++ static initialization functions.
712210299Sed  virtual const char *getStaticInitSectionSpecifier() const {
713210299Sed    return 0;
714210299Sed  }
715221345Sdim
716221345Sdim  const LangAS::Map &getAddressSpaceMap() const {
717221345Sdim    return *AddrSpaceMap;
718221345Sdim  }
719221345Sdim
720221345Sdim  /// \brief Retrieve the name of the platform as it is used in the
721221345Sdim  /// availability attribute.
722226633Sdim  StringRef getPlatformName() const { return PlatformName; }
723221345Sdim
724221345Sdim  /// \brief Retrieve the minimum desired version of the platform, to
725221345Sdim  /// which the program should be compiled.
726221345Sdim  VersionTuple getPlatformMinVersion() const { return PlatformMinVersion; }
727221345Sdim
728234353Sdim  bool isBigEndian() const { return BigEndian; }
729234353Sdim
730249423Sdim  enum CallingConvMethodType {
731249423Sdim    CCMT_Unknown,
732249423Sdim    CCMT_Member,
733249423Sdim    CCMT_NonMember
734249423Sdim  };
735249423Sdim
736243830Sdim  /// \brief Gets the default calling convention for the given target and
737243830Sdim  /// declaration context.
738249423Sdim  virtual CallingConv getDefaultCallingConv(CallingConvMethodType MT) const {
739243830Sdim    // Not all targets will specify an explicit calling convention that we can
740243830Sdim    // express.  This will always do the right thing, even though it's not
741243830Sdim    // an explicit calling convention.
742249423Sdim    return CC_C;
743243830Sdim  }
744243830Sdim
745243830Sdim  enum CallingConvCheckResult {
746243830Sdim    CCCR_OK,
747243830Sdim    CCCR_Warning
748243830Sdim  };
749243830Sdim
750243830Sdim  /// \brief Determines whether a given calling convention is valid for the
751243830Sdim  /// target. A calling convention can either be accepted, produce a warning
752243830Sdim  /// and be substituted with the default calling convention, or (someday)
753243830Sdim  /// produce an error (such as using thiscall on a non-instance function).
754243830Sdim  virtual CallingConvCheckResult checkCallingConvention(CallingConv CC) const {
755243830Sdim    switch (CC) {
756243830Sdim      default:
757243830Sdim        return CCCR_Warning;
758243830Sdim      case CC_C:
759243830Sdim      case CC_Default:
760243830Sdim        return CCCR_OK;
761243830Sdim    }
762243830Sdim  }
763243830Sdim
764193326Sedprotected:
765193326Sed  virtual uint64_t getPointerWidthV(unsigned AddrSpace) const {
766193326Sed    return PointerWidth;
767193326Sed  }
768193326Sed  virtual uint64_t getPointerAlignV(unsigned AddrSpace) const {
769193326Sed    return PointerAlign;
770193326Sed  }
771193326Sed  virtual enum IntType getPtrDiffTypeV(unsigned AddrSpace) const {
772193326Sed    return PtrDiffType;
773193326Sed  }
774198092Srdivacky  virtual void getGCCRegNames(const char * const *&Names,
775193326Sed                              unsigned &NumNames) const = 0;
776198092Srdivacky  virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
777193326Sed                                unsigned &NumAliases) const = 0;
778224145Sdim  virtual void getGCCAddlRegNames(const AddlRegName *&Addl,
779224145Sdim				  unsigned &NumAddl) const {
780224145Sdim    Addl = 0;
781224145Sdim    NumAddl = 0;
782224145Sdim  }
783198092Srdivacky  virtual bool validateAsmConstraint(const char *&Name,
784193326Sed                                     TargetInfo::ConstraintInfo &info) const= 0;
785193326Sed};
786193326Sed
787193326Sed}  // end namespace clang
788193326Sed
789193326Sed#endif
790