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