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