TargetInfo.h revision 201361
1193326Sed//===--- TargetInfo.h - Expose information about the target -----*- C++ -*-===//
2193326Sed//
3193326Sed//                     The LLVM Compiler Infrastructure
4193326Sed//
5193326Sed// This file is distributed under the University of Illinois Open Source
6193326Sed// License. See LICENSE.TXT for details.
7193326Sed//
8193326Sed//===----------------------------------------------------------------------===//
9193326Sed//
10193326Sed//  This file defines the TargetInfo interface.
11193326Sed//
12193326Sed//===----------------------------------------------------------------------===//
13193326Sed
14193326Sed#ifndef LLVM_CLANG_BASIC_TARGETINFO_H
15193326Sed#define LLVM_CLANG_BASIC_TARGETINFO_H
16193326Sed
17193326Sed// FIXME: Daniel isn't smart enough to use a prototype for this.
18193326Sed#include "llvm/ADT/StringMap.h"
19198092Srdivacky#include "llvm/ADT/Triple.h"
20198893Srdivacky#include "llvm/System/DataTypes.h"
21193326Sed#include <cassert>
22193326Sed#include <vector>
23193326Sed#include <string>
24193326Sed
25198092Srdivackynamespace llvm {
26198092Srdivackystruct fltSemantics;
27198092Srdivackyclass StringRef;
28198092Srdivacky}
29193326Sed
30193326Sednamespace clang {
31193326Sedclass Diagnostic;
32199482Srdivackyclass LangOptions;
33198092Srdivackyclass SourceLocation;
34193326Sedclass SourceManager;
35199482Srdivackyclass TargetOptions;
36199482Srdivacky
37193326Sednamespace Builtin { struct Info; }
38198092Srdivacky
39193326Sed/// TargetInfo - This class exposes information about the current target.
40193326Sed///
41193326Sedclass TargetInfo {
42198092Srdivacky  llvm::Triple Triple;
43193326Sedprotected:
44193326Sed  // Target values set by the ctor of the actual target implementation.  Default
45193326Sed  // values are specified by the TargetInfo constructor.
46193326Sed  bool TLSSupported;
47193326Sed  unsigned char PointerWidth, PointerAlign;
48193326Sed  unsigned char IntWidth, IntAlign;
49193326Sed  unsigned char FloatWidth, FloatAlign;
50193326Sed  unsigned char DoubleWidth, DoubleAlign;
51193326Sed  unsigned char LongDoubleWidth, LongDoubleAlign;
52193326Sed  unsigned char LongWidth, LongAlign;
53193326Sed  unsigned char LongLongWidth, LongLongAlign;
54193326Sed  const char *DescriptionString;
55193326Sed  const char *UserLabelPrefix;
56193326Sed  const llvm::fltSemantics *FloatFormat, *DoubleFormat, *LongDoubleFormat;
57193326Sed  unsigned char RegParmMax, SSERegParmMax;
58193326Sed
59193326Sed  // TargetInfo Constructor.  Default initializes all fields.
60193326Sed  TargetInfo(const std::string &T);
61198092Srdivacky
62198092Srdivackypublic:
63199482Srdivacky  /// CreateTargetInfo - Construct a target for the given options.
64201361Srdivacky  ///
65201361Srdivacky  /// \param Opts - The options to use to initialize the target. The target may
66201361Srdivacky  /// modify the options to canonicalize the target feature information to match
67201361Srdivacky  /// what the backend expects.
68201361Srdivacky  static TargetInfo* CreateTargetInfo(Diagnostic &Diags, TargetOptions &Opts);
69193326Sed
70193326Sed  virtual ~TargetInfo();
71193326Sed
72193326Sed  ///===---- Target Data Type Query Methods -------------------------------===//
73193326Sed  enum IntType {
74193326Sed    NoInt = 0,
75193326Sed    SignedShort,
76193326Sed    UnsignedShort,
77193326Sed    SignedInt,
78193326Sed    UnsignedInt,
79193326Sed    SignedLong,
80193326Sed    UnsignedLong,
81193326Sed    SignedLongLong,
82193326Sed    UnsignedLongLong
83193326Sed  };
84193326Sedprotected:
85195341Sed  IntType SizeType, IntMaxType, UIntMaxType, PtrDiffType, IntPtrType, WCharType,
86199990Srdivacky          WIntType, Char16Type, Char32Type, Int64Type, SigAtomicType;
87193326Sedpublic:
88193326Sed  IntType getSizeType() const { return SizeType; }
89193326Sed  IntType getIntMaxType() const { return IntMaxType; }
90193326Sed  IntType getUIntMaxType() const { return UIntMaxType; }
91193326Sed  IntType getPtrDiffType(unsigned AddrSpace) const {
92193326Sed    return AddrSpace == 0 ? PtrDiffType : getPtrDiffTypeV(AddrSpace);
93193326Sed  }
94193326Sed  IntType getIntPtrType() const { return IntPtrType; }
95193326Sed  IntType getWCharType() const { return WCharType; }
96198398Srdivacky  IntType getWIntType() const { return WIntType; }
97198092Srdivacky  IntType getChar16Type() const { return Char16Type; }
98198092Srdivacky  IntType getChar32Type() const { return Char32Type; }
99195341Sed  IntType getInt64Type() const { return Int64Type; }
100199990Srdivacky  IntType getSigAtomicType() const { return SigAtomicType; }
101193326Sed
102198398Srdivacky
103198398Srdivacky  /// getTypeWidth - Return the width (in bits) of the specified integer type
104198398Srdivacky  /// enum. For example, SignedInt -> getIntWidth().
105198398Srdivacky  unsigned getTypeWidth(IntType T) const;
106198398Srdivacky
107199482Srdivacky  /// getTypeAlign - Return the alignment (in bits) of the specified integer
108199482Srdivacky  /// type enum. For example, SignedInt -> getIntAlign().
109199482Srdivacky  unsigned getTypeAlign(IntType T) const;
110199482Srdivacky
111198893Srdivacky  /// isTypeSigned - Return whether an integer types is signed. Returns true if
112198398Srdivacky  /// the type is signed; false otherwise.
113198893Srdivacky  bool isTypeSigned(IntType T) const;
114198398Srdivacky
115193326Sed  /// getPointerWidth - Return the width of pointers on this target, for the
116193326Sed  /// specified address space.
117193326Sed  uint64_t getPointerWidth(unsigned AddrSpace) const {
118193326Sed    return AddrSpace == 0 ? PointerWidth : getPointerWidthV(AddrSpace);
119193326Sed  }
120193326Sed  uint64_t getPointerAlign(unsigned AddrSpace) const {
121193326Sed    return AddrSpace == 0 ? PointerAlign : getPointerAlignV(AddrSpace);
122193326Sed  }
123198092Srdivacky
124193326Sed  /// getBoolWidth/Align - Return the size of '_Bool' and C++ 'bool' for this
125193326Sed  /// target, in bits.
126193326Sed  unsigned getBoolWidth(bool isWide = false) const { return 8; }  // FIXME
127193326Sed  unsigned getBoolAlign(bool isWide = false) const { return 8; }  // FIXME
128198092Srdivacky
129198092Srdivacky  unsigned getCharWidth() const { return 8; } // FIXME
130198092Srdivacky  unsigned getCharAlign() const { return 8; } // FIXME
131198092Srdivacky
132193326Sed  /// getShortWidth/Align - Return the size of 'signed short' and
133198092Srdivacky  /// 'unsigned short' for this target, in bits.
134193326Sed  unsigned getShortWidth() const { return 16; } // FIXME
135193326Sed  unsigned getShortAlign() const { return 16; } // FIXME
136198092Srdivacky
137193326Sed  /// getIntWidth/Align - Return the size of 'signed int' and 'unsigned int' for
138193326Sed  /// this target, in bits.
139193326Sed  unsigned getIntWidth() const { return IntWidth; }
140193326Sed  unsigned getIntAlign() const { return IntAlign; }
141198092Srdivacky
142193326Sed  /// getLongWidth/Align - Return the size of 'signed long' and 'unsigned long'
143193326Sed  /// for this target, in bits.
144193326Sed  unsigned getLongWidth() const { return LongWidth; }
145193326Sed  unsigned getLongAlign() const { return LongAlign; }
146198092Srdivacky
147193326Sed  /// getLongLongWidth/Align - Return the size of 'signed long long' and
148193326Sed  /// 'unsigned long long' for this target, in bits.
149193326Sed  unsigned getLongLongWidth() const { return LongLongWidth; }
150193326Sed  unsigned getLongLongAlign() const { return LongLongAlign; }
151198092Srdivacky
152198092Srdivacky  /// getWCharWidth/Align - Return the size of 'wchar_t' for this target, in
153193326Sed  /// bits.
154199482Srdivacky  unsigned getWCharWidth() const { return getTypeWidth(WCharType); }
155199482Srdivacky  unsigned getWCharAlign() const { return getTypeAlign(WCharType); }
156193326Sed
157198092Srdivacky  /// getChar16Width/Align - Return the size of 'char16_t' for this target, in
158198092Srdivacky  /// bits.
159199482Srdivacky  unsigned getChar16Width() const { return getTypeWidth(Char16Type); }
160199482Srdivacky  unsigned getChar16Align() const { return getTypeAlign(Char16Type); }
161198092Srdivacky
162198092Srdivacky  /// getChar32Width/Align - Return the size of 'char32_t' for this target, in
163198092Srdivacky  /// bits.
164199482Srdivacky  unsigned getChar32Width() const { return getTypeWidth(Char32Type); }
165199482Srdivacky  unsigned getChar32Align() const { return getTypeAlign(Char32Type); }
166198092Srdivacky
167193326Sed  /// getFloatWidth/Align/Format - Return the size/align/format of 'float'.
168193326Sed  unsigned getFloatWidth() const { return FloatWidth; }
169193326Sed  unsigned getFloatAlign() const { return FloatAlign; }
170193326Sed  const llvm::fltSemantics &getFloatFormat() const { return *FloatFormat; }
171193326Sed
172193326Sed  /// getDoubleWidth/Align/Format - Return the size/align/format of 'double'.
173193326Sed  unsigned getDoubleWidth() const { return DoubleWidth; }
174193326Sed  unsigned getDoubleAlign() const { return DoubleAlign; }
175193326Sed  const llvm::fltSemantics &getDoubleFormat() const { return *DoubleFormat; }
176193326Sed
177193326Sed  /// getLongDoubleWidth/Align/Format - Return the size/align/format of 'long
178193326Sed  /// double'.
179193326Sed  unsigned getLongDoubleWidth() const { return LongDoubleWidth; }
180193326Sed  unsigned getLongDoubleAlign() const { return LongDoubleAlign; }
181193326Sed  const llvm::fltSemantics &getLongDoubleFormat() const {
182193326Sed    return *LongDoubleFormat;
183193326Sed  }
184198092Srdivacky
185193326Sed  /// getIntMaxTWidth - Return the size of intmax_t and uintmax_t for this
186198092Srdivacky  /// target, in bits.
187193326Sed  unsigned getIntMaxTWidth() const {
188199482Srdivacky    return getTypeWidth(IntMaxType);
189193326Sed  }
190198092Srdivacky
191193326Sed  /// getUserLabelPrefix - This returns the default value of the
192193326Sed  /// __USER_LABEL_PREFIX__ macro, which is the prefix given to user symbols by
193193326Sed  /// default.  On most platforms this is "_", but it is "" on some, and "." on
194193326Sed  /// others.
195193326Sed  const char *getUserLabelPrefix() const {
196193326Sed    return UserLabelPrefix;
197193326Sed  }
198198092Srdivacky
199193326Sed  /// getTypeName - Return the user string for the specified integer type enum.
200193326Sed  /// For example, SignedShort -> "short".
201193326Sed  static const char *getTypeName(IntType T);
202198092Srdivacky
203198398Srdivacky  /// getTypeConstantSuffix - Return the constant suffix for the specified
204198398Srdivacky  /// integer type enum. For example, SignedLong -> "L".
205198398Srdivacky  static const char *getTypeConstantSuffix(IntType T);
206198398Srdivacky
207193326Sed  ///===---- Other target property query methods --------------------------===//
208198092Srdivacky
209193326Sed  /// getTargetDefines - Appends the target-specific #define values for this
210193326Sed  /// target set to the specified buffer.
211193326Sed  virtual void getTargetDefines(const LangOptions &Opts,
212193326Sed                                std::vector<char> &DefineBuffer) const = 0;
213198092Srdivacky
214198398Srdivacky
215193326Sed  /// getTargetBuiltins - Return information about target-specific builtins for
216193326Sed  /// the current primary target, and info about which builtins are non-portable
217193326Sed  /// across the current set of primary and secondary targets.
218198092Srdivacky  virtual void getTargetBuiltins(const Builtin::Info *&Records,
219193326Sed                                 unsigned &NumRecords) const = 0;
220193326Sed
221193326Sed  /// getVAListDeclaration - Return the declaration to use for
222193326Sed  /// __builtin_va_list, which is target-specific.
223193326Sed  virtual const char *getVAListDeclaration() const = 0;
224193326Sed
225193326Sed  /// isValidGCCRegisterName - Returns whether the passed in string
226193326Sed  /// is a valid register name according to GCC. This is used by Sema for
227193326Sed  /// inline asm statements.
228193326Sed  bool isValidGCCRegisterName(const char *Name) const;
229193326Sed
230193326Sed  // getNormalizedGCCRegisterName - Returns the "normalized" GCC register name.
231193326Sed  // For example, on x86 it will return "ax" when "eax" is passed in.
232193326Sed  const char *getNormalizedGCCRegisterName(const char *Name) const;
233198092Srdivacky
234193326Sed  struct ConstraintInfo {
235193326Sed    enum {
236193326Sed      CI_None = 0x00,
237193326Sed      CI_AllowsMemory = 0x01,
238193326Sed      CI_AllowsRegister = 0x02,
239193326Sed      CI_ReadWrite = 0x04,       // "+r" output constraint (read and write).
240193326Sed      CI_HasMatchingInput = 0x08 // This output operand has a matching input.
241193326Sed    };
242193326Sed    unsigned Flags;
243193326Sed    int TiedOperand;
244198092Srdivacky
245193326Sed    std::string ConstraintStr;  // constraint: "=rm"
246193326Sed    std::string Name;           // Operand name: [foo] with no []'s.
247193326Sed  public:
248193326Sed    ConstraintInfo(const char *str, unsigned strlen, const std::string &name)
249193326Sed      : Flags(0), TiedOperand(-1), ConstraintStr(str, str+strlen), Name(name) {}
250193326Sed    explicit ConstraintInfo(const std::string &Str, const std::string &name)
251193326Sed      : Flags(0), TiedOperand(-1), ConstraintStr(Str), Name(name) {}
252193326Sed
253193326Sed    const std::string &getConstraintStr() const { return ConstraintStr; }
254193326Sed    const std::string &getName() const { return Name; }
255193326Sed    bool isReadWrite() const { return (Flags & CI_ReadWrite) != 0; }
256193326Sed    bool allowsRegister() const { return (Flags & CI_AllowsRegister) != 0; }
257193326Sed    bool allowsMemory() const { return (Flags & CI_AllowsMemory) != 0; }
258198092Srdivacky
259193326Sed    /// hasMatchingInput - Return true if this output operand has a matching
260193326Sed    /// (tied) input operand.
261193326Sed    bool hasMatchingInput() const { return (Flags & CI_HasMatchingInput) != 0; }
262198092Srdivacky
263193326Sed    /// hasTiedOperand() - Return true if this input operand is a matching
264193326Sed    /// constraint that ties it to an output operand.  If this returns true,
265193326Sed    /// then getTiedOperand will indicate which output operand this is tied to.
266193326Sed    bool hasTiedOperand() const { return TiedOperand != -1; }
267193326Sed    unsigned getTiedOperand() const {
268193326Sed      assert(hasTiedOperand() && "Has no tied operand!");
269193326Sed      return (unsigned)TiedOperand;
270193326Sed    }
271198092Srdivacky
272193326Sed    void setIsReadWrite() { Flags |= CI_ReadWrite; }
273193326Sed    void setAllowsMemory() { Flags |= CI_AllowsMemory; }
274193326Sed    void setAllowsRegister() { Flags |= CI_AllowsRegister; }
275193326Sed    void setHasMatchingInput() { Flags |= CI_HasMatchingInput; }
276198092Srdivacky
277193326Sed    /// setTiedOperand - Indicate that this is an input operand that is tied to
278193326Sed    /// the specified output operand.  Copy over the various constraint
279193326Sed    /// information from the output.
280193326Sed    void setTiedOperand(unsigned N, ConstraintInfo &Output) {
281193326Sed      Output.setHasMatchingInput();
282193326Sed      Flags = Output.Flags;
283193326Sed      TiedOperand = N;
284193326Sed      // Don't copy Name or constraint string.
285193326Sed    }
286193326Sed  };
287193326Sed
288193326Sed  // validateOutputConstraint, validateInputConstraint - Checks that
289193326Sed  // a constraint is valid and provides information about it.
290193326Sed  // FIXME: These should return a real error instead of just true/false.
291193326Sed  bool validateOutputConstraint(ConstraintInfo &Info) const;
292193326Sed  bool validateInputConstraint(ConstraintInfo *OutputConstraints,
293193326Sed                               unsigned NumOutputs,
294193326Sed                               ConstraintInfo &info) const;
295193326Sed  bool resolveSymbolicName(const char *&Name,
296193326Sed                           ConstraintInfo *OutputConstraints,
297193326Sed                           unsigned NumOutputs, unsigned &Index) const;
298198092Srdivacky
299193326Sed  virtual std::string convertConstraint(const char Constraint) const {
300193326Sed    return std::string(1, Constraint);
301193326Sed  }
302198092Srdivacky
303193326Sed  // Returns a string of target-specific clobbers, in LLVM format.
304193326Sed  virtual const char *getClobbers() const = 0;
305193326Sed
306198092Srdivacky
307198092Srdivacky  /// getTriple - Return the target triple of the primary target.
308198092Srdivacky  const llvm::Triple &getTriple() const {
309198092Srdivacky    return Triple;
310193326Sed  }
311198092Srdivacky
312193326Sed  const char *getTargetDescription() const {
313193326Sed    return DescriptionString;
314193326Sed  }
315193326Sed
316193326Sed  struct GCCRegAlias {
317193326Sed    const char * const Aliases[5];
318193326Sed    const char * const Register;
319193326Sed  };
320193326Sed
321193326Sed  virtual bool useGlobalsForAutomaticVariables() const { return false; }
322193326Sed
323193326Sed  /// getUnicodeStringSection - Return the section to use for unicode
324193326Sed  /// string literals, or 0 if no special section is used.
325198092Srdivacky  virtual const char *getUnicodeStringSection() const {
326193326Sed    return 0;
327193326Sed  }
328193326Sed
329193326Sed  /// getCFStringSection - Return the section to use for CFString
330193326Sed  /// literals, or 0 if no special section is used.
331198092Srdivacky  virtual const char *getCFStringSection() const {
332193326Sed    return "__DATA,__cfstring";
333193326Sed  }
334193326Sed
335198092Srdivacky  /// isValidSectionSpecifier - This is an optional hook that targets can
336198092Srdivacky  /// implement to perform semantic checking on attribute((section("foo")))
337198092Srdivacky  /// specifiers.  In this case, "foo" is passed in to be checked.  If the
338198092Srdivacky  /// section specifier is invalid, the backend should return a non-empty string
339198092Srdivacky  /// that indicates the problem.
340198092Srdivacky  ///
341198092Srdivacky  /// This hook is a simple quality of implementation feature to catch errors
342198092Srdivacky  /// and give good diagnostics in cases when the assembler or code generator
343198092Srdivacky  /// would otherwise reject the section specifier.
344198092Srdivacky  ///
345198092Srdivacky  virtual std::string isValidSectionSpecifier(const llvm::StringRef &SR) const {
346198092Srdivacky    return "";
347193326Sed  }
348193326Sed
349199482Srdivacky  /// setForcedLangOptions - Set forced language options.
350199482Srdivacky  /// Apply changes to the target information with respect to certain
351199482Srdivacky  /// language options which change the target configuration.
352199482Srdivacky  virtual void setForcedLangOptions(LangOptions &Opts);
353193326Sed
354193326Sed  /// getDefaultFeatures - Get the default set of target features for
355193326Sed  /// the \args CPU; this should include all legal feature strings on
356193326Sed  /// the target.
357198092Srdivacky  virtual void getDefaultFeatures(const std::string &CPU,
358193326Sed                                  llvm::StringMap<bool> &Features) const {
359193326Sed  }
360193326Sed
361198092Srdivacky  /// getABI - Get the ABI in use.
362198092Srdivacky  virtual const char *getABI() const {
363198092Srdivacky    return "";
364198092Srdivacky  }
365198092Srdivacky
366201361Srdivacky  /// setCPU - Target the specific CPU.
367201361Srdivacky  ///
368201361Srdivacky  /// \return - False on error (invalid CPU name).
369201361Srdivacky  //
370201361Srdivacky  // FIXME: Remove this.
371201361Srdivacky  virtual bool setCPU(const std::string &Name) {
372201361Srdivacky    return true;
373201361Srdivacky  }
374201361Srdivacky
375198092Srdivacky  /// setABI - Use the specific ABI.
376198092Srdivacky  ///
377198092Srdivacky  /// \return - False on error (invalid ABI name).
378198092Srdivacky  virtual bool setABI(const std::string &Name) {
379198092Srdivacky    return false;
380198092Srdivacky  }
381198092Srdivacky
382193326Sed  /// setFeatureEnabled - Enable or disable a specific target feature,
383193326Sed  /// the feature name must be valid.
384193326Sed  ///
385193326Sed  /// \return - False on error (invalid feature name).
386193326Sed  virtual bool setFeatureEnabled(llvm::StringMap<bool> &Features,
387193326Sed                                 const std::string &Name,
388193326Sed                                 bool Enabled) const {
389193326Sed    return false;
390193326Sed  }
391193326Sed
392199482Srdivacky  /// HandleTargetOptions - Perform initialization based on the user configured
393199482Srdivacky  /// set of features (e.g., +sse4). The list is guaranteed to have at most one
394199482Srdivacky  /// entry per feature.
395201361Srdivacky  ///
396201361Srdivacky  /// The target may modify the features list, to change which options are
397201361Srdivacky  /// passed onwards to the backend.
398201361Srdivacky  virtual void HandleTargetFeatures(std::vector<std::string> &Features) {
399193326Sed  }
400193326Sed
401193326Sed  // getRegParmMax - Returns maximal number of args passed in registers.
402193326Sed  unsigned getRegParmMax() const {
403193326Sed    return RegParmMax;
404193326Sed  }
405193326Sed
406198092Srdivacky  /// isTLSSupported - Whether the target supports thread-local storage.
407198092Srdivacky  bool isTLSSupported() const {
408193326Sed    return TLSSupported;
409193326Sed  }
410198092Srdivacky
411198092Srdivacky  /// getEHDataRegisterNumber - Return the register number that
412198092Srdivacky  /// __builtin_eh_return_regno would return with the specified argument.
413198092Srdivacky  virtual int getEHDataRegisterNumber(unsigned RegNo) const {
414198092Srdivacky    return -1;
415198092Srdivacky  }
416198092Srdivacky
417193326Sed
418193326Sedprotected:
419193326Sed  virtual uint64_t getPointerWidthV(unsigned AddrSpace) const {
420193326Sed    return PointerWidth;
421193326Sed  }
422193326Sed  virtual uint64_t getPointerAlignV(unsigned AddrSpace) const {
423193326Sed    return PointerAlign;
424193326Sed  }
425193326Sed  virtual enum IntType getPtrDiffTypeV(unsigned AddrSpace) const {
426193326Sed    return PtrDiffType;
427193326Sed  }
428198092Srdivacky  virtual void getGCCRegNames(const char * const *&Names,
429193326Sed                              unsigned &NumNames) const = 0;
430198092Srdivacky  virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
431193326Sed                                unsigned &NumAliases) const = 0;
432198092Srdivacky  virtual bool validateAsmConstraint(const char *&Name,
433193326Sed                                     TargetInfo::ConstraintInfo &info) const= 0;
434193326Sed};
435193326Sed
436193326Sed}  // end namespace clang
437193326Sed
438193326Sed#endif
439