TargetInfo.h revision 6ad474f82f0cf32e13128d89fa5ad3a37ae73530
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  unsigned char PointerWidth, PointerAlign;
40  unsigned char WCharWidth, WCharAlign;
41  unsigned char IntWidth, IntAlign;
42  unsigned char FloatWidth, FloatAlign;
43  unsigned char DoubleWidth, DoubleAlign;
44  unsigned char LongDoubleWidth, LongDoubleAlign;
45  unsigned char LongWidth, LongAlign;
46  unsigned char LongLongWidth, LongLongAlign;
47  unsigned char IntMaxTWidth;
48  const char *DescriptionString;
49  const char *UserLabelPrefix;
50  const llvm::fltSemantics *FloatFormat, *DoubleFormat, *LongDoubleFormat;
51
52  // TargetInfo Constructor.  Default initializes all fields.
53  TargetInfo(const std::string &T);
54
55public:
56  /// CreateTargetInfo - Return the target info object for the specified target
57  /// triple.
58  static TargetInfo* CreateTargetInfo(const std::string &Triple);
59
60  virtual ~TargetInfo();
61
62  ///===---- Target Data Type Query Methods -------------------------------===//
63  enum IntType {
64    NoInt = 0,
65    SignedShort,
66    UnsignedShort,
67    SignedInt,
68    UnsignedInt,
69    SignedLong,
70    UnsignedLong,
71    SignedLongLong,
72    UnsignedLongLong
73  };
74protected:
75  IntType SizeType, IntMaxType, UIntMaxType, PtrDiffType, IntPtrType, WCharType;
76public:
77  IntType getSizeType() const { return SizeType; }
78  IntType getIntMaxType() const { return IntMaxType; }
79  IntType getUIntMaxType() const { return UIntMaxType; }
80  IntType getPtrDiffType(unsigned AddrSpace) const {
81    return AddrSpace == 0 ? PtrDiffType : getPtrDiffTypeV(AddrSpace);
82  }
83  IntType getIntPtrType() const { return IntPtrType; }
84  IntType getWCharType() const { return WCharType; }
85
86  /// isCharSigned - Return true if 'char' is 'signed char' or false if it is
87  /// treated as 'unsigned char'.  This is implementation defined according to
88  /// C99 6.2.5p15.  In our implementation, this is target-specific.
89  bool isCharSigned() const { return CharIsSigned; }
90
91  /// getPointerWidth - Return the width of pointers on this target, for the
92  /// specified address space.
93  uint64_t getPointerWidth(unsigned AddrSpace) const {
94    return AddrSpace == 0 ? PointerWidth : getPointerWidthV(AddrSpace);
95  }
96  uint64_t getPointerAlign(unsigned AddrSpace) const {
97    return AddrSpace == 0 ? PointerAlign : getPointerAlignV(AddrSpace);
98  }
99
100  /// getBoolWidth/Align - Return the size of '_Bool' and C++ 'bool' for this
101  /// target, in bits.
102  unsigned getBoolWidth(bool isWide = false) const { return 8; }  // FIXME
103  unsigned getBoolAlign(bool isWide = false) const { return 8; }  // FIXME
104
105  unsigned getCharWidth(bool isWide = false) const {
106    return isWide ? getWCharWidth() : 8; // FIXME
107  }
108  unsigned getCharAlign(bool isWide = false) const {
109    return isWide ? getWCharAlign() : 8; // FIXME
110  }
111
112  /// getShortWidth/Align - Return the size of 'signed short' and
113  /// 'unsigned short' for this target, in bits.
114  unsigned getShortWidth() const { return 16; } // FIXME
115  unsigned getShortAlign() const { return 16; } // FIXME
116
117  /// getIntWidth/Align - Return the size of 'signed int' and 'unsigned int' for
118  /// this target, in bits.
119  unsigned getIntWidth() const { return IntWidth; }
120  unsigned getIntAlign() const { return IntAlign; }
121
122  /// getLongWidth/Align - Return the size of 'signed long' and 'unsigned long'
123  /// for this target, in bits.
124  unsigned getLongWidth() const { return LongWidth; }
125  unsigned getLongAlign() const { return LongAlign; }
126
127  /// getLongLongWidth/Align - Return the size of 'signed long long' and
128  /// 'unsigned long long' for this target, in bits.
129  unsigned getLongLongWidth() const { return LongLongWidth; }
130  unsigned getLongLongAlign() const { return LongLongAlign; }
131
132  /// getWcharWidth/Align - Return the size of 'wchar_t' for this target, in
133  /// bits.
134  unsigned getWCharWidth() const { return WCharWidth; }
135  unsigned getWCharAlign() const { return WCharAlign; }
136
137  /// getFloatWidth/Align/Format - Return the size/align/format of 'float'.
138  unsigned getFloatWidth() const { return FloatWidth; }
139  unsigned getFloatAlign() const { return FloatAlign; }
140  const llvm::fltSemantics &getFloatFormat() const { return *FloatFormat; }
141
142  /// getDoubleWidth/Align/Format - Return the size/align/format of 'double'.
143  unsigned getDoubleWidth() const { return DoubleWidth; }
144  unsigned getDoubleAlign() const { return DoubleAlign; }
145  const llvm::fltSemantics &getDoubleFormat() const { return *DoubleFormat; }
146
147  /// getLongDoubleWidth/Align/Format - Return the size/align/format of 'long
148  /// double'.
149  unsigned getLongDoubleWidth() const { return LongDoubleWidth; }
150  unsigned getLongDoubleAlign() const { return LongDoubleAlign; }
151  const llvm::fltSemantics &getLongDoubleFormat() const {
152    return *LongDoubleFormat;
153  }
154
155  /// getIntMaxTWidth - Return the size of intmax_t and uintmax_t for this
156  /// target, in bits.
157  unsigned getIntMaxTWidth() const {
158    return IntMaxTWidth;
159  }
160
161  /// getUserLabelPrefix - This returns the default value of the
162  /// __USER_LABEL_PREFIX__ macro, which is the prefix given to user symbols by
163  /// default.  On most platforms this is "_", but it is "" on some, and "." on
164  /// others.
165  const char *getUserLabelPrefix() const {
166    return UserLabelPrefix;
167  }
168
169  /// getTypeName - Return the user string for the specified integer type enum.
170  /// For example, SignedShort -> "short".
171  static const char *getTypeName(IntType T);
172
173  ///===---- Other target property query methods --------------------------===//
174
175  /// getTargetDefines - Appends the target-specific #define values for this
176  /// target set to the specified buffer.
177  virtual void getTargetDefines(std::vector<char> &DefineBuffer) const = 0;
178
179  /// getTargetBuiltins - Return information about target-specific builtins for
180  /// the current primary target, and info about which builtins are non-portable
181  /// across the current set of primary and secondary targets.
182  virtual void getTargetBuiltins(const Builtin::Info *&Records,
183                                 unsigned &NumRecords) const = 0;
184
185  /// getVAListDeclaration - Return the declaration to use for
186  /// __builtin_va_list, which is target-specific.
187  virtual const char *getVAListDeclaration() const = 0;
188
189  /// isValidGCCRegisterName - Returns whether the passed in string
190  /// is a valid register name according to GCC. This is used by Sema for
191  /// inline asm statements.
192  bool isValidGCCRegisterName(const char *Name) const;
193
194  // getNormalizedGCCRegisterName - Returns the "normalized" GCC register name.
195  // For example, on x86 it will return "ax" when "eax" is passed in.
196  const char *getNormalizedGCCRegisterName(const char *Name) const;
197
198  enum ConstraintInfo {
199    CI_None = 0x00,
200    CI_AllowsMemory = 0x01,
201    CI_AllowsRegister = 0x02,
202    CI_ReadWrite = 0x04
203  };
204
205  // validateOutputConstraint, validateInputConstraint - Checks that
206  // a constraint is valid and provides information about it.
207  // FIXME: These should return a real error instead of just true/false.
208  bool validateOutputConstraint(const char *Name, ConstraintInfo &Info) const;
209  bool validateInputConstraint(const char *Name,
210                               const std::string *OutputNamesBegin,
211                               const std::string *OutputNamesEnd,
212                               ConstraintInfo* OutputConstraints,
213                               ConstraintInfo &info) const;
214  bool resolveSymbolicName(const char *&Name,
215                           const std::string *OutputNamesBegin,
216                           const std::string *OutputNamesEnd,
217                           unsigned &Index) const;
218
219  virtual std::string convertConstraint(const char Constraint) const {
220    return std::string(1, Constraint);
221  }
222
223  // Returns a string of target-specific clobbers, in LLVM format.
224  virtual const char *getClobbers() const = 0;
225
226
227  /// getTargetPrefix - Return the target prefix used for identifying
228  /// llvm intrinsics.
229  virtual const char *getTargetPrefix() const = 0;
230
231  /// getTargetTriple - Return the target triple of the primary target.
232  const char *getTargetTriple() const {
233    return Triple.c_str();
234  }
235
236  const char *getTargetDescription() const {
237    return DescriptionString;
238  }
239
240  struct GCCRegAlias {
241    const char * const Aliases[5];
242    const char * const Register;
243  };
244
245  virtual bool useGlobalsForAutomaticVariables() const { return false; }
246
247  /// getDefaultLangOptions - Allow the target to specify default settings for
248  /// various language options.  These may be overridden by command line
249  /// options.
250  virtual void getDefaultLangOptions(LangOptions &Opts) {}
251
252protected:
253  virtual uint64_t getPointerWidthV(unsigned AddrSpace) const {
254    return PointerWidth;
255  }
256  virtual uint64_t getPointerAlignV(unsigned AddrSpace) const {
257    return PointerAlign;
258  }
259  virtual enum IntType getPtrDiffTypeV(unsigned AddrSpace) const {
260    return PtrDiffType;
261  }
262  virtual void getGCCRegNames(const char * const *&Names,
263                              unsigned &NumNames) const = 0;
264  virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
265                                unsigned &NumAliases) const = 0;
266  virtual bool validateAsmConstraint(char c,
267                                     TargetInfo::ConstraintInfo &info) const= 0;
268};
269
270}  // end namespace clang
271
272#endif
273