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