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