TargetInfo.h revision 1752ee4849f4c37f5e03193e658be92650b0e65a
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  /// getTargetPrefix - Return the target prefix used for identifying
288  /// llvm intrinsics.
289  virtual const char *getTargetPrefix() const = 0;
290
291  /// getTriple - Return the target triple of the primary target.
292  const llvm::Triple &getTriple() const {
293    return Triple;
294  }
295
296  const char *getTargetDescription() const {
297    return DescriptionString;
298  }
299
300  struct GCCRegAlias {
301    const char * const Aliases[5];
302    const char * const Register;
303  };
304
305  virtual bool useGlobalsForAutomaticVariables() const { return false; }
306
307  /// getUnicodeStringSymbolPrefix - Get the default symbol prefix to
308  /// use for string literals.
309  virtual const char *getUnicodeStringSymbolPrefix() const {
310    return ".str";
311  }
312
313  /// getUnicodeStringSection - Return the section to use for unicode
314  /// string literals, or 0 if no special section is used.
315  virtual const char *getUnicodeStringSection() const {
316    return 0;
317  }
318
319  /// getCFStringSection - Return the section to use for CFString
320  /// literals, or 0 if no special section is used.
321  virtual const char *getCFStringSection() const {
322    return "__DATA,__cfstring";
323  }
324
325  /// getCFStringDataSection - Return the section to use for the
326  /// constant string data associated with a CFString literal, or 0 if
327  /// no special section is used.
328  virtual const char *getCFStringDataSection() const {
329    return "__TEXT,__cstring,cstring_literals";
330  }
331
332
333  /// isValidSectionSpecifier - This is an optional hook that targets can
334  /// implement to perform semantic checking on attribute((section("foo")))
335  /// specifiers.  In this case, "foo" is passed in to be checked.  If the
336  /// section specifier is invalid, the backend should return a non-empty string
337  /// that indicates the problem.
338  ///
339  /// This hook is a simple quality of implementation feature to catch errors
340  /// and give good diagnostics in cases when the assembler or code generator
341  /// would otherwise reject the section specifier.
342  ///
343  virtual std::string isValidSectionSpecifier(const llvm::StringRef &SR) const {
344    return "";
345  }
346
347  /// getDefaultLangOptions - Allow the target to specify default settings for
348  /// various language options.  These may be overridden by command line
349  /// options.
350  virtual void getDefaultLangOptions(LangOptions &Opts) {}
351
352  /// getDefaultFeatures - Get the default set of target features for
353  /// the \args CPU; this should include all legal feature strings on
354  /// the target.
355  virtual void getDefaultFeatures(const std::string &CPU,
356                                  llvm::StringMap<bool> &Features) const {
357  }
358
359  /// setFeatureEnabled - Enable or disable a specific target feature,
360  /// the feature name must be valid.
361  ///
362  /// \return - False on error (invalid feature name).
363  virtual bool setFeatureEnabled(llvm::StringMap<bool> &Features,
364                                 const std::string &Name,
365                                 bool Enabled) const {
366    return false;
367  }
368
369  /// HandleTargetOptions - Perform initialization based on the user
370  /// configured set of features.
371  virtual void HandleTargetFeatures(const llvm::StringMap<bool> &Features) {
372  }
373
374  // getRegParmMax - Returns maximal number of args passed in registers.
375  unsigned getRegParmMax() const {
376    return RegParmMax;
377  }
378
379  // isTLSSupported - Whether the target supports thread-local storage
380  unsigned isTLSSupported() const {
381    return TLSSupported;
382  }
383
384protected:
385  virtual uint64_t getPointerWidthV(unsigned AddrSpace) const {
386    return PointerWidth;
387  }
388  virtual uint64_t getPointerAlignV(unsigned AddrSpace) const {
389    return PointerAlign;
390  }
391  virtual enum IntType getPtrDiffTypeV(unsigned AddrSpace) const {
392    return PtrDiffType;
393  }
394  virtual void getGCCRegNames(const char * const *&Names,
395                              unsigned &NumNames) const = 0;
396  virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
397                                unsigned &NumAliases) const = 0;
398  virtual bool validateAsmConstraint(const char *&Name,
399                                     TargetInfo::ConstraintInfo &info) const= 0;
400};
401
402}  // end namespace clang
403
404#endif
405