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