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