TargetInfo.h revision 44def070435a2b5d67f0534f7a3a85a7389d60f2
1//===--- TargetInfo.h - Expose information about the target -----*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10//  This file defines the TargetInfo interface.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_BASIC_TARGETINFO_H
15#define LLVM_CLANG_BASIC_TARGETINFO_H
16
17#include "llvm/Support/DataTypes.h"
18#include <vector>
19#include <string>
20
21namespace llvm { struct fltSemantics; }
22
23namespace clang {
24
25class Diagnostic;
26class SourceManager;
27class LangOptions;
28
29namespace Builtin { struct Info; }
30
31/// TargetInfo - This class exposes information about the current target.
32///
33class TargetInfo {
34  std::string Triple;
35protected:
36  // Target values set by the ctor of the actual target implementation.  Default
37  // values are specified by the TargetInfo constructor.
38  bool CharIsSigned;
39  bool TLSSupported;
40  unsigned char PointerWidth, PointerAlign;
41  unsigned char WCharWidth, WCharAlign;
42  unsigned char IntWidth, IntAlign;
43  unsigned char FloatWidth, FloatAlign;
44  unsigned char DoubleWidth, DoubleAlign;
45  unsigned char LongDoubleWidth, LongDoubleAlign;
46  unsigned char LongWidth, LongAlign;
47  unsigned char LongLongWidth, LongLongAlign;
48  unsigned char IntMaxTWidth;
49  const char *DescriptionString;
50  const char *UserLabelPrefix;
51  const llvm::fltSemantics *FloatFormat, *DoubleFormat, *LongDoubleFormat;
52  unsigned char RegParmMax, SSERegParmMax;
53
54  // TargetInfo Constructor.  Default initializes all fields.
55  TargetInfo(const std::string &T);
56
57public:
58  /// CreateTargetInfo - Return the target info object for the specified target
59  /// triple.
60  static TargetInfo* CreateTargetInfo(const std::string &Triple);
61
62  virtual ~TargetInfo();
63
64  ///===---- Target Data Type Query Methods -------------------------------===//
65  enum IntType {
66    NoInt = 0,
67    SignedShort,
68    UnsignedShort,
69    SignedInt,
70    UnsignedInt,
71    SignedLong,
72    UnsignedLong,
73    SignedLongLong,
74    UnsignedLongLong
75  };
76protected:
77  IntType SizeType, IntMaxType, UIntMaxType, PtrDiffType, IntPtrType, WCharType;
78public:
79  IntType getSizeType() const { return SizeType; }
80  IntType getIntMaxType() const { return IntMaxType; }
81  IntType getUIntMaxType() const { return UIntMaxType; }
82  IntType getPtrDiffType(unsigned AddrSpace) const {
83    return AddrSpace == 0 ? PtrDiffType : getPtrDiffTypeV(AddrSpace);
84  }
85  IntType getIntPtrType() const { return IntPtrType; }
86  IntType getWCharType() const { return WCharType; }
87
88  /// isCharSigned - Return true if 'char' is 'signed char' or false if it is
89  /// treated as 'unsigned char'.  This is implementation defined according to
90  /// C99 6.2.5p15.  In our implementation, this is target-specific.
91  bool isCharSigned() const { return CharIsSigned; }
92
93  /// getPointerWidth - Return the width of pointers on this target, for the
94  /// specified address space.
95  uint64_t getPointerWidth(unsigned AddrSpace) const {
96    return AddrSpace == 0 ? PointerWidth : getPointerWidthV(AddrSpace);
97  }
98  uint64_t getPointerAlign(unsigned AddrSpace) const {
99    return AddrSpace == 0 ? PointerAlign : getPointerAlignV(AddrSpace);
100  }
101
102  /// getBoolWidth/Align - Return the size of '_Bool' and C++ 'bool' for this
103  /// target, in bits.
104  unsigned getBoolWidth(bool isWide = false) const { return 8; }  // FIXME
105  unsigned getBoolAlign(bool isWide = false) const { return 8; }  // FIXME
106
107  unsigned getCharWidth(bool isWide = false) const {
108    return isWide ? getWCharWidth() : 8; // FIXME
109  }
110  unsigned getCharAlign(bool isWide = false) const {
111    return isWide ? getWCharAlign() : 8; // FIXME
112  }
113
114  /// getShortWidth/Align - Return the size of 'signed short' and
115  /// 'unsigned short' for this target, in bits.
116  unsigned getShortWidth() const { return 16; } // FIXME
117  unsigned getShortAlign() const { return 16; } // FIXME
118
119  /// getIntWidth/Align - Return the size of 'signed int' and 'unsigned int' for
120  /// this target, in bits.
121  unsigned getIntWidth() const { return IntWidth; }
122  unsigned getIntAlign() const { return IntAlign; }
123
124  /// getLongWidth/Align - Return the size of 'signed long' and 'unsigned long'
125  /// for this target, in bits.
126  unsigned getLongWidth() const { return LongWidth; }
127  unsigned getLongAlign() const { return LongAlign; }
128
129  /// getLongLongWidth/Align - Return the size of 'signed long long' and
130  /// 'unsigned long long' for this target, in bits.
131  unsigned getLongLongWidth() const { return LongLongWidth; }
132  unsigned getLongLongAlign() const { return LongLongAlign; }
133
134  /// getWcharWidth/Align - Return the size of 'wchar_t' for this target, in
135  /// bits.
136  unsigned getWCharWidth() const { return WCharWidth; }
137  unsigned getWCharAlign() const { return WCharAlign; }
138
139  /// getFloatWidth/Align/Format - Return the size/align/format of 'float'.
140  unsigned getFloatWidth() const { return FloatWidth; }
141  unsigned getFloatAlign() const { return FloatAlign; }
142  const llvm::fltSemantics &getFloatFormat() const { return *FloatFormat; }
143
144  /// getDoubleWidth/Align/Format - Return the size/align/format of 'double'.
145  unsigned getDoubleWidth() const { return DoubleWidth; }
146  unsigned getDoubleAlign() const { return DoubleAlign; }
147  const llvm::fltSemantics &getDoubleFormat() const { return *DoubleFormat; }
148
149  /// getLongDoubleWidth/Align/Format - Return the size/align/format of 'long
150  /// double'.
151  unsigned getLongDoubleWidth() const { return LongDoubleWidth; }
152  unsigned getLongDoubleAlign() const { return LongDoubleAlign; }
153  const llvm::fltSemantics &getLongDoubleFormat() const {
154    return *LongDoubleFormat;
155  }
156
157  /// getIntMaxTWidth - Return the size of intmax_t and uintmax_t for this
158  /// target, in bits.
159  unsigned getIntMaxTWidth() const {
160    return IntMaxTWidth;
161  }
162
163  /// getUserLabelPrefix - This returns the default value of the
164  /// __USER_LABEL_PREFIX__ macro, which is the prefix given to user symbols by
165  /// default.  On most platforms this is "_", but it is "" on some, and "." on
166  /// others.
167  const char *getUserLabelPrefix() const {
168    return UserLabelPrefix;
169  }
170
171  /// getTypeName - Return the user string for the specified integer type enum.
172  /// For example, SignedShort -> "short".
173  static const char *getTypeName(IntType T);
174
175  ///===---- Other target property query methods --------------------------===//
176
177  /// getTargetDefines - Appends the target-specific #define values for this
178  /// target set to the specified buffer.
179  virtual void getTargetDefines(const LangOptions &Opts,
180                                std::vector<char> &DefineBuffer) const = 0;
181
182  /// getTargetBuiltins - Return information about target-specific builtins for
183  /// the current primary target, and info about which builtins are non-portable
184  /// across the current set of primary and secondary targets.
185  virtual void getTargetBuiltins(const Builtin::Info *&Records,
186                                 unsigned &NumRecords) const = 0;
187
188  /// getVAListDeclaration - Return the declaration to use for
189  /// __builtin_va_list, which is target-specific.
190  virtual const char *getVAListDeclaration() const = 0;
191
192  /// isValidGCCRegisterName - Returns whether the passed in string
193  /// is a valid register name according to GCC. This is used by Sema for
194  /// inline asm statements.
195  bool isValidGCCRegisterName(const char *Name) const;
196
197  // getNormalizedGCCRegisterName - Returns the "normalized" GCC register name.
198  // For example, on x86 it will return "ax" when "eax" is passed in.
199  const char *getNormalizedGCCRegisterName(const char *Name) const;
200
201  struct ConstraintInfo {
202    enum {
203      CI_None = 0x00,
204      CI_AllowsMemory = 0x01,
205      CI_AllowsRegister = 0x02,
206      CI_ReadWrite = 0x04   // "+r" output constraint (read and write).
207    };
208    unsigned Flags;
209    int TiedOperand;
210  public:
211    ConstraintInfo() : Flags(0), TiedOperand(-1) {}
212
213    bool isReadWrite() const { return (Flags & CI_ReadWrite) != 0; }
214    bool allowsRegister() const { return (Flags & CI_AllowsRegister) != 0; }
215    bool allowsMemory() const { return (Flags & CI_AllowsMemory) != 0; }
216    bool hasTiedOperand() const { return TiedOperand != -1; }
217    unsigned getTiedOperand() const {
218      assert(hasTiedOperand() && "Has no tied operand!");
219      return (unsigned)TiedOperand;
220    }
221
222    void setIsReadWrite() { Flags |= CI_ReadWrite; }
223    void setAllowsMemory() { Flags |= CI_AllowsMemory; }
224    void setAllowsRegister() { Flags |= CI_AllowsRegister; }
225    void setTiedOperand(unsigned N) { TiedOperand = N; }
226  };
227
228  // validateOutputConstraint, validateInputConstraint - Checks that
229  // a constraint is valid and provides information about it.
230  // FIXME: These should return a real error instead of just true/false.
231  bool validateOutputConstraint(const char *Name, ConstraintInfo &Info) const;
232  bool validateInputConstraint(const char *Name,
233                               const std::string *OutputNamesBegin,
234                               const std::string *OutputNamesEnd,
235                               ConstraintInfo* OutputConstraints,
236                               ConstraintInfo &info) const;
237  bool resolveSymbolicName(const char *&Name,
238                           const std::string *OutputNamesBegin,
239                           const std::string *OutputNamesEnd,
240                           unsigned &Index) const;
241
242  virtual std::string convertConstraint(const char Constraint) const {
243    return std::string(1, Constraint);
244  }
245
246  // Returns a string of target-specific clobbers, in LLVM format.
247  virtual const char *getClobbers() const = 0;
248
249
250  /// getTargetPrefix - Return the target prefix used for identifying
251  /// llvm intrinsics.
252  virtual const char *getTargetPrefix() const = 0;
253
254  /// getTargetTriple - Return the target triple of the primary target.
255  const char *getTargetTriple() const {
256    return Triple.c_str();
257  }
258
259  const char *getTargetDescription() const {
260    return DescriptionString;
261  }
262
263  struct GCCRegAlias {
264    const char * const Aliases[5];
265    const char * const Register;
266  };
267
268  virtual bool useGlobalsForAutomaticVariables() const { return false; }
269
270  /// getStringSymbolPrefix - Get the default symbol prefix to
271  /// use for string literals.
272  virtual const char *getStringSymbolPrefix(bool IsConstant) const {
273    return ".str";
274  }
275
276  /// getCFStringSymbolPrefix - Get the default symbol prefix
277  /// to use for CFString literals.
278  virtual const char *getCFStringSymbolPrefix() const {
279    return "";
280  }
281
282  /// getUnicodeStringSymbolPrefix - Get the default symbol prefix to
283  /// use for string literals.
284  virtual const char *getUnicodeStringSymbolPrefix() const {
285    return ".str";
286  }
287
288  /// getUnicodeStringSection - Return the section to use for unicode
289  /// string literals, or 0 if no special section is used.
290  virtual const char *getUnicodeStringSection() const {
291    return 0;
292  }
293
294  /// getCFStringSection - Return the section to use for CFString
295  /// literals, or 0 if no special section is used.
296  virtual const char *getCFStringSection() const {
297    return "__DATA,__cfstring";
298  }
299
300  /// getCFStringDataSection - Return the section to use for the
301  /// constant string data associated with a CFString literal, or 0 if
302  /// no special section is used.
303  virtual const char *getCFStringDataSection() const {
304    return "__TEXT,__cstring,cstring_literals";
305  }
306
307  /// getDefaultLangOptions - Allow the target to specify default settings for
308  /// various language options.  These may be overridden by command line
309  /// options.
310  virtual void getDefaultLangOptions(LangOptions &Opts) {}
311
312  /// HandleTargetFeatures - Handle target-specific options like -mattr=+sse2
313  /// and friends.  An array of arguments is passed in: if they are all valid,
314  /// this should handle them and return -1.  If there is an error, the index of
315  /// the invalid argument should be returned along with an optional error
316  /// string.
317  ///
318  /// Note that the driver should have already consolidated all the
319  /// target-feature settings and passed them to us in the -mattr list.  The
320  /// -mattr list is treated by the code generator as a diff against the -mcpu
321  /// setting, but the driver should pass all enabled options as "+" settings.
322  /// This means that the target should only look at + settings.
323  virtual int HandleTargetFeatures(std::string *StrArray, unsigned NumStrs,
324                                   std::string &ErrorReason) {
325    if (NumStrs == 0)
326      return -1;
327    return 0;
328  }
329
330  // getRegParmMax - Returns maximal number of args passed in registers.
331  unsigned getRegParmMax() const {
332    return RegParmMax;
333  }
334
335  // isTLSSupported - Whether the target supports thread-local storage
336  unsigned isTLSSupported() const {
337    return TLSSupported;
338  }
339
340protected:
341  virtual uint64_t getPointerWidthV(unsigned AddrSpace) const {
342    return PointerWidth;
343  }
344  virtual uint64_t getPointerAlignV(unsigned AddrSpace) const {
345    return PointerAlign;
346  }
347  virtual enum IntType getPtrDiffTypeV(unsigned AddrSpace) const {
348    return PtrDiffType;
349  }
350  virtual void getGCCRegNames(const char * const *&Names,
351                              unsigned &NumNames) const = 0;
352  virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
353                                unsigned &NumAliases) const = 0;
354  virtual bool validateAsmConstraint(const char *&Name,
355                                     TargetInfo::ConstraintInfo &info) const= 0;
356};
357
358}  // end namespace clang
359
360#endif
361