TargetInfo.h revision 278b9f06933c385ffbccc15f8491787470cb4a1b
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// FIXME: Daniel isn't smart enough to use a prototype for this.
18#include "llvm/ADT/StringMap.h"
19#include "llvm/ADT/Triple.h"
20#include "llvm/Support/DataTypes.h"
21#include <cassert>
22#include <vector>
23#include <string>
24
25namespace llvm {
26struct fltSemantics;
27class StringRef;
28}
29
30namespace clang {
31
32class Diagnostic;
33class SourceLocation;
34class SourceManager;
35class LangOptions;
36namespace Builtin { struct Info; }
37
38/// TargetInfo - This class exposes information about the current target.
39///
40class TargetInfo {
41  llvm::Triple Triple;
42protected:
43  // Target values set by the ctor of the actual target implementation.  Default
44  // values are specified by the TargetInfo constructor.
45  bool TLSSupported;
46  unsigned char PointerWidth, PointerAlign;
47  unsigned char WCharWidth, WCharAlign;
48  unsigned char Char16Width, Char16Align;
49  unsigned char Char32Width, Char32Align;
50  unsigned char IntWidth, IntAlign;
51  unsigned char FloatWidth, FloatAlign;
52  unsigned char DoubleWidth, DoubleAlign;
53  unsigned char LongDoubleWidth, LongDoubleAlign;
54  unsigned char LongWidth, LongAlign;
55  unsigned char LongLongWidth, LongLongAlign;
56  unsigned char IntMaxTWidth;
57  const char *DescriptionString;
58  const char *UserLabelPrefix;
59  const llvm::fltSemantics *FloatFormat, *DoubleFormat, *LongDoubleFormat;
60  unsigned char RegParmMax, SSERegParmMax;
61
62  // TargetInfo Constructor.  Default initializes all fields.
63  TargetInfo(const std::string &T);
64
65public:
66  /// CreateTargetInfo - Return the target info object for the specified target
67  /// triple.
68  static TargetInfo* CreateTargetInfo(const std::string &Triple);
69
70  virtual ~TargetInfo();
71
72  ///===---- Target Data Type Query Methods -------------------------------===//
73  enum IntType {
74    NoInt = 0,
75    SignedShort,
76    UnsignedShort,
77    SignedInt,
78    UnsignedInt,
79    SignedLong,
80    UnsignedLong,
81    SignedLongLong,
82    UnsignedLongLong
83  };
84protected:
85  IntType SizeType, IntMaxType, UIntMaxType, PtrDiffType, IntPtrType, WCharType,
86          Char16Type, Char32Type, Int64Type;
87public:
88  IntType getSizeType() const { return SizeType; }
89  IntType getIntMaxType() const { return IntMaxType; }
90  IntType getUIntMaxType() const { return UIntMaxType; }
91  IntType getPtrDiffType(unsigned AddrSpace) const {
92    return AddrSpace == 0 ? PtrDiffType : getPtrDiffTypeV(AddrSpace);
93  }
94  IntType getIntPtrType() const { return IntPtrType; }
95  IntType getWCharType() const { return WCharType; }
96  IntType getChar16Type() const { return Char16Type; }
97  IntType getChar32Type() const { return Char32Type; }
98  IntType getInt64Type() const { return Int64Type; }
99
100  /// getPointerWidth - Return the width of pointers on this target, for the
101  /// specified address space.
102  uint64_t getPointerWidth(unsigned AddrSpace) const {
103    return AddrSpace == 0 ? PointerWidth : getPointerWidthV(AddrSpace);
104  }
105  uint64_t getPointerAlign(unsigned AddrSpace) const {
106    return AddrSpace == 0 ? PointerAlign : getPointerAlignV(AddrSpace);
107  }
108
109  /// getBoolWidth/Align - Return the size of '_Bool' and C++ 'bool' for this
110  /// target, in bits.
111  unsigned getBoolWidth(bool isWide = false) const { return 8; }  // FIXME
112  unsigned getBoolAlign(bool isWide = false) const { return 8; }  // FIXME
113
114  unsigned getCharWidth() const { return 8; } // FIXME
115  unsigned getCharAlign() const { return 8; } // FIXME
116
117  /// getShortWidth/Align - Return the size of 'signed short' and
118  /// 'unsigned short' for this target, in bits.
119  unsigned getShortWidth() const { return 16; } // FIXME
120  unsigned getShortAlign() const { return 16; } // FIXME
121
122  /// getIntWidth/Align - Return the size of 'signed int' and 'unsigned int' for
123  /// this target, in bits.
124  unsigned getIntWidth() const { return IntWidth; }
125  unsigned getIntAlign() const { return IntAlign; }
126
127  /// getLongWidth/Align - Return the size of 'signed long' and 'unsigned long'
128  /// for this target, in bits.
129  unsigned getLongWidth() const { return LongWidth; }
130  unsigned getLongAlign() const { return LongAlign; }
131
132  /// getLongLongWidth/Align - Return the size of 'signed long long' and
133  /// 'unsigned long long' for this target, in bits.
134  unsigned getLongLongWidth() const { return LongLongWidth; }
135  unsigned getLongLongAlign() const { return LongLongAlign; }
136
137  /// getWCharWidth/Align - Return the size of 'wchar_t' for this target, in
138  /// bits.
139  unsigned getWCharWidth() const { return WCharWidth; }
140  unsigned getWCharAlign() const { return WCharAlign; }
141
142  /// getChar16Width/Align - Return the size of 'char16_t' for this target, in
143  /// bits.
144  unsigned getChar16Width() const { return Char16Width; }
145  unsigned getChar16Align() const { return Char16Align; }
146
147  /// getChar32Width/Align - Return the size of 'char32_t' for this target, in
148  /// bits.
149  unsigned getChar32Width() const { return Char32Width; }
150  unsigned getChar32Align() const { return Char32Align; }
151
152  /// getFloatWidth/Align/Format - Return the size/align/format of 'float'.
153  unsigned getFloatWidth() const { return FloatWidth; }
154  unsigned getFloatAlign() const { return FloatAlign; }
155  const llvm::fltSemantics &getFloatFormat() const { return *FloatFormat; }
156
157  /// getDoubleWidth/Align/Format - Return the size/align/format of 'double'.
158  unsigned getDoubleWidth() const { return DoubleWidth; }
159  unsigned getDoubleAlign() const { return DoubleAlign; }
160  const llvm::fltSemantics &getDoubleFormat() const { return *DoubleFormat; }
161
162  /// getLongDoubleWidth/Align/Format - Return the size/align/format of 'long
163  /// double'.
164  unsigned getLongDoubleWidth() const { return LongDoubleWidth; }
165  unsigned getLongDoubleAlign() const { return LongDoubleAlign; }
166  const llvm::fltSemantics &getLongDoubleFormat() const {
167    return *LongDoubleFormat;
168  }
169
170  /// getIntMaxTWidth - Return the size of intmax_t and uintmax_t for this
171  /// target, in bits.
172  unsigned getIntMaxTWidth() const {
173    return IntMaxTWidth;
174  }
175
176  /// getUserLabelPrefix - This returns the default value of the
177  /// __USER_LABEL_PREFIX__ macro, which is the prefix given to user symbols by
178  /// default.  On most platforms this is "_", but it is "" on some, and "." on
179  /// others.
180  const char *getUserLabelPrefix() const {
181    return UserLabelPrefix;
182  }
183
184  /// getTypeName - Return the user string for the specified integer type enum.
185  /// For example, SignedShort -> "short".
186  static const char *getTypeName(IntType T);
187
188  ///===---- Other target property query methods --------------------------===//
189
190  /// getTargetDefines - Appends the target-specific #define values for this
191  /// target set to the specified buffer.
192  virtual void getTargetDefines(const LangOptions &Opts,
193                                std::vector<char> &DefineBuffer) const = 0;
194
195  /// getTargetBuiltins - Return information about target-specific builtins for
196  /// the current primary target, and info about which builtins are non-portable
197  /// across the current set of primary and secondary targets.
198  virtual void getTargetBuiltins(const Builtin::Info *&Records,
199                                 unsigned &NumRecords) const = 0;
200
201  /// getVAListDeclaration - Return the declaration to use for
202  /// __builtin_va_list, which is target-specific.
203  virtual const char *getVAListDeclaration() const = 0;
204
205  /// isValidGCCRegisterName - Returns whether the passed in string
206  /// is a valid register name according to GCC. This is used by Sema for
207  /// inline asm statements.
208  bool isValidGCCRegisterName(const char *Name) const;
209
210  // getNormalizedGCCRegisterName - Returns the "normalized" GCC register name.
211  // For example, on x86 it will return "ax" when "eax" is passed in.
212  const char *getNormalizedGCCRegisterName(const char *Name) const;
213
214  struct ConstraintInfo {
215    enum {
216      CI_None = 0x00,
217      CI_AllowsMemory = 0x01,
218      CI_AllowsRegister = 0x02,
219      CI_ReadWrite = 0x04,       // "+r" output constraint (read and write).
220      CI_HasMatchingInput = 0x08 // This output operand has a matching input.
221    };
222    unsigned Flags;
223    int TiedOperand;
224
225    std::string ConstraintStr;  // constraint: "=rm"
226    std::string Name;           // Operand name: [foo] with no []'s.
227  public:
228    ConstraintInfo(const char *str, unsigned strlen, const std::string &name)
229      : Flags(0), TiedOperand(-1), ConstraintStr(str, str+strlen), Name(name) {}
230    explicit ConstraintInfo(const std::string &Str, const std::string &name)
231      : Flags(0), TiedOperand(-1), ConstraintStr(Str), Name(name) {}
232
233    const std::string &getConstraintStr() const { return ConstraintStr; }
234    const std::string &getName() const { return Name; }
235    bool isReadWrite() const { return (Flags & CI_ReadWrite) != 0; }
236    bool allowsRegister() const { return (Flags & CI_AllowsRegister) != 0; }
237    bool allowsMemory() const { return (Flags & CI_AllowsMemory) != 0; }
238
239    /// hasMatchingInput - Return true if this output operand has a matching
240    /// (tied) input operand.
241    bool hasMatchingInput() const { return (Flags & CI_HasMatchingInput) != 0; }
242
243    /// hasTiedOperand() - Return true if this input operand is a matching
244    /// constraint that ties it to an output operand.  If this returns true,
245    /// then getTiedOperand will indicate which output operand this is tied to.
246    bool hasTiedOperand() const { return TiedOperand != -1; }
247    unsigned getTiedOperand() const {
248      assert(hasTiedOperand() && "Has no tied operand!");
249      return (unsigned)TiedOperand;
250    }
251
252    void setIsReadWrite() { Flags |= CI_ReadWrite; }
253    void setAllowsMemory() { Flags |= CI_AllowsMemory; }
254    void setAllowsRegister() { Flags |= CI_AllowsRegister; }
255    void setHasMatchingInput() { Flags |= CI_HasMatchingInput; }
256
257    /// setTiedOperand - Indicate that this is an input operand that is tied to
258    /// the specified output operand.  Copy over the various constraint
259    /// information from the output.
260    void setTiedOperand(unsigned N, ConstraintInfo &Output) {
261      Output.setHasMatchingInput();
262      Flags = Output.Flags;
263      TiedOperand = N;
264      // Don't copy Name or constraint string.
265    }
266  };
267
268  // validateOutputConstraint, validateInputConstraint - Checks that
269  // a constraint is valid and provides information about it.
270  // FIXME: These should return a real error instead of just true/false.
271  bool validateOutputConstraint(ConstraintInfo &Info) const;
272  bool validateInputConstraint(ConstraintInfo *OutputConstraints,
273                               unsigned NumOutputs,
274                               ConstraintInfo &info) const;
275  bool resolveSymbolicName(const char *&Name,
276                           ConstraintInfo *OutputConstraints,
277                           unsigned NumOutputs, unsigned &Index) const;
278
279  virtual std::string convertConstraint(const char Constraint) const {
280    return std::string(1, Constraint);
281  }
282
283  // Returns a string of target-specific clobbers, in LLVM format.
284  virtual const char *getClobbers() const = 0;
285
286
287  /// getTriple - Return the target triple of the primary target.
288  const llvm::Triple &getTriple() const {
289    return Triple;
290  }
291
292  const char *getTargetDescription() const {
293    return DescriptionString;
294  }
295
296  struct GCCRegAlias {
297    const char * const Aliases[5];
298    const char * const Register;
299  };
300
301  virtual bool useGlobalsForAutomaticVariables() const { return false; }
302
303  /// getUnicodeStringSection - Return the section to use for unicode
304  /// string literals, or 0 if no special section is used.
305  virtual const char *getUnicodeStringSection() const {
306    return 0;
307  }
308
309  /// getCFStringSection - Return the section to use for CFString
310  /// literals, or 0 if no special section is used.
311  virtual const char *getCFStringSection() const {
312    return "__DATA,__cfstring";
313  }
314
315  /// isValidSectionSpecifier - This is an optional hook that targets can
316  /// implement to perform semantic checking on attribute((section("foo")))
317  /// specifiers.  In this case, "foo" is passed in to be checked.  If the
318  /// section specifier is invalid, the backend should return a non-empty string
319  /// that indicates the problem.
320  ///
321  /// This hook is a simple quality of implementation feature to catch errors
322  /// and give good diagnostics in cases when the assembler or code generator
323  /// would otherwise reject the section specifier.
324  ///
325  virtual std::string isValidSectionSpecifier(const llvm::StringRef &SR) const {
326    return "";
327  }
328
329  /// getDefaultLangOptions - Allow the target to specify default settings for
330  /// various language options.  These may be overridden by command line
331  /// options.
332  virtual void getDefaultLangOptions(LangOptions &Opts) {}
333
334  /// getDefaultFeatures - Get the default set of target features for
335  /// the \args CPU; this should include all legal feature strings on
336  /// the target.
337  virtual void getDefaultFeatures(const std::string &CPU,
338                                  llvm::StringMap<bool> &Features) const {
339  }
340
341  /// getABI - Get the ABI in use.
342  virtual const char *getABI() const {
343    return "";
344  }
345
346  /// setABI - Use the specific ABI.
347  ///
348  /// \return - False on error (invalid ABI name).
349  virtual bool setABI(const std::string &Name) {
350    return false;
351  }
352
353  /// setFeatureEnabled - Enable or disable a specific target feature,
354  /// the feature name must be valid.
355  ///
356  /// \return - False on error (invalid feature name).
357  virtual bool setFeatureEnabled(llvm::StringMap<bool> &Features,
358                                 const std::string &Name,
359                                 bool Enabled) const {
360    return false;
361  }
362
363  /// HandleTargetOptions - Perform initialization based on the user
364  /// configured set of features.
365  virtual void HandleTargetFeatures(const llvm::StringMap<bool> &Features) {
366  }
367
368  // getRegParmMax - Returns maximal number of args passed in registers.
369  unsigned getRegParmMax() const {
370    return RegParmMax;
371  }
372
373  /// isTLSSupported - Whether the target supports thread-local storage.
374  bool isTLSSupported() const {
375    return TLSSupported;
376  }
377
378  /// getEHDataRegisterNumber - Return the register number that
379  /// __builtin_eh_return_regno would return with the specified argument.
380  virtual int getEHDataRegisterNumber(unsigned RegNo) const {
381    return -1;
382  }
383
384
385protected:
386  virtual uint64_t getPointerWidthV(unsigned AddrSpace) const {
387    return PointerWidth;
388  }
389  virtual uint64_t getPointerAlignV(unsigned AddrSpace) const {
390    return PointerAlign;
391  }
392  virtual enum IntType getPtrDiffTypeV(unsigned AddrSpace) const {
393    return PtrDiffType;
394  }
395  virtual void getGCCRegNames(const char * const *&Names,
396                              unsigned &NumNames) const = 0;
397  virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
398                                unsigned &NumAliases) const = 0;
399  virtual bool validateAsmConstraint(const char *&Name,
400                                     TargetInfo::ConstraintInfo &info) const= 0;
401};
402
403}  // end namespace clang
404
405#endif
406