TargetInfo.h revision 64b22d8351a83593220803ed702e036c73bb8710
1//===---- TargetInfo.h - Encapsulate target details -------------*- 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// These classes wrap the information about a call or function
11// definition used to handle ABI compliancy.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef CLANG_CODEGEN_TARGETINFO_H
16#define CLANG_CODEGEN_TARGETINFO_H
17
18#include "clang/AST/Type.h"
19#include "clang/Basic/LLVM.h"
20#include "llvm/ADT/StringRef.h"
21#include "llvm/ADT/SmallString.h"
22
23namespace llvm {
24  class GlobalValue;
25  class Type;
26  class Value;
27}
28
29namespace clang {
30  class ABIInfo;
31  class Decl;
32
33  namespace CodeGen {
34    class CallArgList;
35    class CodeGenModule;
36    class CodeGenFunction;
37    class CGFunctionInfo;
38  }
39
40  /// TargetCodeGenInfo - This class organizes various target-specific
41  /// codegeneration issues, like target-specific attributes, builtins and so
42  /// on.
43  class TargetCodeGenInfo {
44    ABIInfo *Info;
45  public:
46    // WARNING: Acquires the ownership of ABIInfo.
47    TargetCodeGenInfo(ABIInfo *info = 0):Info(info) { }
48    virtual ~TargetCodeGenInfo();
49
50    /// getABIInfo() - Returns ABI info helper for the target.
51    const ABIInfo& getABIInfo() const { return *Info; }
52
53    /// SetTargetAttributes - Provides a convenient hook to handle extra
54    /// target-specific attributes for the given global.
55    virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
56                                     CodeGen::CodeGenModule &M) const { }
57
58    /// Determines the size of struct _Unwind_Exception on this platform,
59    /// in 8-bit units.  The Itanium ABI defines this as:
60    ///   struct _Unwind_Exception {
61    ///     uint64 exception_class;
62    ///     _Unwind_Exception_Cleanup_Fn exception_cleanup;
63    ///     uint64 private_1;
64    ///     uint64 private_2;
65    ///   };
66    virtual unsigned getSizeOfUnwindException() const;
67
68    /// Controls whether __builtin_extend_pointer should sign-extend
69    /// pointers to uint64_t or zero-extend them (the default).  Has
70    /// no effect for targets:
71    ///   - that have 64-bit pointers, or
72    ///   - that cannot address through registers larger than pointers, or
73    ///   - that implicitly ignore/truncate the top bits when addressing
74    ///     through such registers.
75    virtual bool extendPointerWithSExt() const { return false; }
76
77    /// Controls whether BIpow* emit an intrinsic call instead of a library
78    /// function call.
79    virtual bool emitIntrinsicForPow() const { return true; }
80
81    /// Determines the DWARF register number for the stack pointer, for
82    /// exception-handling purposes.  Implements __builtin_dwarf_sp_column.
83    ///
84    /// Returns -1 if the operation is unsupported by this target.
85    virtual int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
86      return -1;
87    }
88
89    /// Initializes the given DWARF EH register-size table, a char*.
90    /// Implements __builtin_init_dwarf_reg_size_table.
91    ///
92    /// Returns true if the operation is unsupported by this target.
93    virtual bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
94                                         llvm::Value *Address) const {
95      return true;
96    }
97
98    /// Performs the code-generation required to convert a return
99    /// address as stored by the system into the actual address of the
100    /// next instruction that will be executed.
101    ///
102    /// Used by __builtin_extract_return_addr().
103    virtual llvm::Value *decodeReturnAddress(CodeGen::CodeGenFunction &CGF,
104                                             llvm::Value *Address) const {
105      return Address;
106    }
107
108    /// Performs the code-generation required to convert the address
109    /// of an instruction into a return address suitable for storage
110    /// by the system in a return slot.
111    ///
112    /// Used by __builtin_frob_return_addr().
113    virtual llvm::Value *encodeReturnAddress(CodeGen::CodeGenFunction &CGF,
114                                             llvm::Value *Address) const {
115      return Address;
116    }
117
118    /// Corrects the low-level LLVM type for a given constraint and "usual"
119    /// type.
120    ///
121    /// \returns A pointer to a new LLVM type, possibly the same as the original
122    /// on success; 0 on failure.
123    virtual llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
124                                            StringRef Constraint,
125                                            llvm::Type* Ty) const {
126      return Ty;
127    }
128
129    /// Retrieve the address of a function to call immediately before
130    /// calling objc_retainAutoreleasedReturnValue.  The
131    /// implementation of objc_autoreleaseReturnValue sniffs the
132    /// instruction stream following its return address to decide
133    /// whether it's a call to objc_retainAutoreleasedReturnValue.
134    /// This can be prohibitively expensive, depending on the
135    /// relocation model, and so on some targets it instead sniffs for
136    /// a particular instruction sequence.  This functions returns
137    /// that instruction sequence in inline assembly, which will be
138    /// empty if none is required.
139    virtual StringRef getARCRetainAutoreleasedReturnValueMarker() const {
140      return "";
141    }
142
143    /// Determine whether a call to an unprototyped functions under
144    /// the given calling convention should use the variadic
145    /// convention or the non-variadic convention.
146    ///
147    /// There's a good reason to make a platform's variadic calling
148    /// convention be different from its non-variadic calling
149    /// convention: the non-variadic arguments can be passed in
150    /// registers (better for performance), and the variadic arguments
151    /// can be passed on the stack (also better for performance).  If
152    /// this is done, however, unprototyped functions *must* use the
153    /// non-variadic convention, because C99 states that a call
154    /// through an unprototyped function type must succeed if the
155    /// function was defined with a non-variadic prototype with
156    /// compatible parameters.  Therefore, splitting the conventions
157    /// makes it impossible to call a variadic function through an
158    /// unprototyped type.  Since function prototypes came out in the
159    /// late 1970s, this is probably an acceptable trade-off.
160    /// Nonetheless, not all platforms are willing to make it, and in
161    /// particularly x86-64 bends over backwards to make the
162    /// conventions compatible.
163    ///
164    /// The default is false.  This is correct whenever:
165    ///   - the conventions are exactly the same, because it does not
166    ///     matter and the resulting IR will be somewhat prettier in
167    ///     certain cases; or
168    ///   - the conventions are substantively different in how they pass
169    ///     arguments, because in this case using the variadic convention
170    ///     will lead to C99 violations.
171    ///
172    /// However, some platforms make the conventions identical except
173    /// for passing additional out-of-band information to a variadic
174    /// function: for example, x86-64 passes the number of SSE
175    /// arguments in %al.  On these platforms, it is desireable to
176    /// call unprototyped functions using the variadic convention so
177    /// that unprototyped calls to varargs functions still succeed.
178    ///
179    /// Relatedly, platforms which pass the fixed arguments to this:
180    ///   A foo(B, C, D);
181    /// differently than they would pass them to this:
182    ///   A foo(B, C, D, ...);
183    /// may need to adjust the debugger-support code in Sema to do the
184    /// right thing when calling a function with no know signature.
185    virtual bool isNoProtoCallVariadic(const CodeGen::CallArgList &args,
186                                       const FunctionNoProtoType *fnType) const;
187
188    /// Gets the linker options necessary to link a dependent library on this
189    /// platform.
190    virtual void getDependentLibraryOption(llvm::StringRef Lib,
191                                           llvm::SmallString<24> &Opt) const;
192
193    /// Gets the linker options necessary to detect object file mismatches on
194    /// this platform.
195    virtual void getDetectMismatchOption(llvm::StringRef Name,
196                                         llvm::StringRef Value,
197                                         llvm::SmallString<32> &Opt) const {}
198  };
199}
200
201#endif // CLANG_CODEGEN_TARGETINFO_H
202