Charles Davis | 3a811f1 | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 1 | //===----- CGCXXABI.h - Interface to C++ ABIs -------------------*- 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 provides an abstract class for C++ code generation. Concrete subclasses |
| 11 | // of this implement code generation for specific C++ ABIs. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #ifndef CLANG_CODEGEN_CXXABI_H |
| 16 | #define CLANG_CODEGEN_CXXABI_H |
| 17 | |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 18 | #include "CodeGenFunction.h" |
| 19 | |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 20 | namespace llvm { |
John McCall | cf2c85e | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 21 | class Constant; |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 22 | class Type; |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 23 | class Value; |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 24 | |
| 25 | template <class T> class SmallVectorImpl; |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 26 | } |
| 27 | |
Charles Davis | 3a811f1 | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 28 | namespace clang { |
John McCall | 3023def | 2010-08-22 03:04:22 +0000 | [diff] [blame] | 29 | class CastExpr; |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 30 | class CXXConstructorDecl; |
| 31 | class CXXDestructorDecl; |
John McCall | 875ab10 | 2010-08-22 06:43:33 +0000 | [diff] [blame] | 32 | class CXXMethodDecl; |
John McCall | cf2c85e | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 33 | class CXXRecordDecl; |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 34 | class FieldDecl; |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 35 | |
Charles Davis | 3a811f1 | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 36 | namespace CodeGen { |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 37 | class CodeGenFunction; |
Charles Davis | 3a811f1 | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 38 | class CodeGenModule; |
| 39 | class MangleContext; |
| 40 | |
| 41 | /// Implements C++ ABI-specific code generation functions. |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 42 | class CGCXXABI { |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 43 | protected: |
| 44 | CodeGenModule &CGM; |
| 45 | |
| 46 | CGCXXABI(CodeGenModule &CGM) : CGM(CGM) {} |
| 47 | |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 48 | protected: |
| 49 | ImplicitParamDecl *&getThisDecl(CodeGenFunction &CGF) { |
| 50 | return CGF.CXXThisDecl; |
| 51 | } |
| 52 | llvm::Value *&getThisValue(CodeGenFunction &CGF) { |
| 53 | return CGF.CXXThisValue; |
| 54 | } |
| 55 | |
| 56 | ImplicitParamDecl *&getVTTDecl(CodeGenFunction &CGF) { |
| 57 | return CGF.CXXVTTDecl; |
| 58 | } |
| 59 | llvm::Value *&getVTTValue(CodeGenFunction &CGF) { |
| 60 | return CGF.CXXVTTValue; |
| 61 | } |
| 62 | |
| 63 | /// Build a parameter variable suitable for 'this'. |
| 64 | void BuildThisParam(CodeGenFunction &CGF, FunctionArgList &Params); |
| 65 | |
| 66 | /// Perform prolog initialization of the parameter variable suitable |
| 67 | /// for 'this' emitted by BuildThisParam. |
| 68 | void EmitThisParam(CodeGenFunction &CGF); |
| 69 | |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 70 | ASTContext &getContext() const { return CGM.getContext(); } |
| 71 | |
Charles Davis | 3a811f1 | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 72 | public: |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 73 | |
Anders Carlsson | 1af610f | 2010-11-28 17:50:09 +0000 | [diff] [blame] | 74 | virtual ~CGCXXABI(); |
Charles Davis | 3a811f1 | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 75 | |
| 76 | /// Gets the mangle context. |
| 77 | virtual MangleContext &getMangleContext() = 0; |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 78 | |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 79 | /// Find the LLVM type used to represent the given member pointer |
| 80 | /// type. |
| 81 | virtual const llvm::Type * |
| 82 | ConvertMemberPointerType(const MemberPointerType *MPT); |
| 83 | |
| 84 | /// Load a member function from an object and a member function |
| 85 | /// pointer. Apply the this-adjustment and set 'This' to the |
| 86 | /// adjusted value. |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 87 | virtual llvm::Value * |
| 88 | EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF, |
| 89 | llvm::Value *&This, |
| 90 | llvm::Value *MemPtr, |
| 91 | const MemberPointerType *MPT); |
John McCall | 3023def | 2010-08-22 03:04:22 +0000 | [diff] [blame] | 92 | |
John McCall | 6c2ab1d | 2010-08-31 21:07:20 +0000 | [diff] [blame] | 93 | /// Calculate an l-value from an object and a data member pointer. |
| 94 | virtual llvm::Value *EmitMemberDataPointerAddress(CodeGenFunction &CGF, |
| 95 | llvm::Value *Base, |
| 96 | llvm::Value *MemPtr, |
| 97 | const MemberPointerType *MPT); |
| 98 | |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 99 | /// Perform a derived-to-base or base-to-derived member pointer |
| 100 | /// conversion. |
| 101 | virtual llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF, |
| 102 | const CastExpr *E, |
| 103 | llvm::Value *Src); |
John McCall | cf2c85e | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 104 | |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 105 | /// Perform a derived-to-base or base-to-derived member pointer |
| 106 | /// conversion on a constant member pointer. |
| 107 | virtual llvm::Constant *EmitMemberPointerConversion(llvm::Constant *C, |
| 108 | const CastExpr *E); |
John McCall | cf2c85e | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 109 | |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 110 | /// Return true if the given member pointer can be zero-initialized |
| 111 | /// (in the C++ sense) with an LLVM zeroinitializer. |
John McCall | f16aa10 | 2010-08-22 21:01:12 +0000 | [diff] [blame] | 112 | virtual bool isZeroInitializable(const MemberPointerType *MPT); |
John McCall | cf2c85e | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 113 | |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 114 | /// Create a null member pointer of the given type. |
| 115 | virtual llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT); |
John McCall | cf2c85e | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 116 | |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 117 | /// Create a member pointer for the given method. |
| 118 | virtual llvm::Constant *EmitMemberPointer(const CXXMethodDecl *MD); |
John McCall | 875ab10 | 2010-08-22 06:43:33 +0000 | [diff] [blame] | 119 | |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 120 | /// Create a member pointer for the given field. |
| 121 | virtual llvm::Constant *EmitMemberPointer(const FieldDecl *FD); |
John McCall | e9fd7eb | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 122 | |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 123 | /// Emit a comparison between two member pointers. Returns an i1. |
John McCall | e9fd7eb | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 124 | virtual llvm::Value * |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 125 | EmitMemberPointerComparison(CodeGenFunction &CGF, |
| 126 | llvm::Value *L, |
| 127 | llvm::Value *R, |
| 128 | const MemberPointerType *MPT, |
| 129 | bool Inequality); |
John McCall | e9fd7eb | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 130 | |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 131 | /// Determine if a member pointer is non-null. Returns an i1. |
John McCall | e9fd7eb | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 132 | virtual llvm::Value * |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 133 | EmitMemberPointerIsNotNull(CodeGenFunction &CGF, |
| 134 | llvm::Value *MemPtr, |
| 135 | const MemberPointerType *MPT); |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 136 | |
| 137 | /// Build the signature of the given constructor variant by adding |
| 138 | /// any required parameters. For convenience, ResTy has been |
| 139 | /// initialized to 'void', and ArgTys has been initialized with the |
| 140 | /// type of 'this' (although this may be changed by the ABI) and |
| 141 | /// will have the formal parameters added to it afterwards. |
| 142 | /// |
| 143 | /// If there are ever any ABIs where the implicit parameters are |
| 144 | /// intermixed with the formal parameters, we can address those |
| 145 | /// then. |
| 146 | virtual void BuildConstructorSignature(const CXXConstructorDecl *Ctor, |
| 147 | CXXCtorType T, |
| 148 | CanQualType &ResTy, |
| 149 | llvm::SmallVectorImpl<CanQualType> &ArgTys) = 0; |
| 150 | |
| 151 | /// Build the signature of the given destructor variant by adding |
| 152 | /// any required parameters. For convenience, ResTy has been |
| 153 | /// initialized to 'void' and ArgTys has been initialized with the |
| 154 | /// type of 'this' (although this may be changed by the ABI). |
| 155 | virtual void BuildDestructorSignature(const CXXDestructorDecl *Dtor, |
| 156 | CXXDtorType T, |
| 157 | CanQualType &ResTy, |
| 158 | llvm::SmallVectorImpl<CanQualType> &ArgTys) = 0; |
| 159 | |
| 160 | /// Build the ABI-specific portion of the parameter list for a |
| 161 | /// function. This generally involves a 'this' parameter and |
| 162 | /// possibly some extra data for constructors and destructors. |
| 163 | /// |
| 164 | /// ABIs may also choose to override the return type, which has been |
| 165 | /// initialized with the formal return type of the function. |
| 166 | virtual void BuildInstanceFunctionParams(CodeGenFunction &CGF, |
| 167 | QualType &ResTy, |
| 168 | FunctionArgList &Params) = 0; |
| 169 | |
| 170 | /// Emit the ABI-specific prolog for the function. |
| 171 | virtual void EmitInstanceFunctionProlog(CodeGenFunction &CGF) = 0; |
| 172 | |
| 173 | virtual void EmitReturnFromThunk(CodeGenFunction &CGF, |
| 174 | RValue RV, QualType ResultType); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 175 | |
| 176 | /**************************** Array cookies ******************************/ |
| 177 | |
| 178 | /// Returns the extra size required in order to store the array |
| 179 | /// cookie for the given type. May return 0 to indicate that no |
| 180 | /// array cookie is required. |
| 181 | /// |
| 182 | /// Several cases are filtered out before this method is called: |
| 183 | /// - non-array allocations never need a cookie |
| 184 | /// - calls to ::operator new(size_t, void*) never need a cookie |
| 185 | /// |
| 186 | /// \param ElementType - the allocated type of the expression, |
| 187 | /// i.e. the pointee type of the expression result type |
| 188 | virtual CharUnits GetArrayCookieSize(QualType ElementType); |
| 189 | |
| 190 | /// Initialize the array cookie for the given allocation. |
| 191 | /// |
| 192 | /// \param NewPtr - a char* which is the presumed-non-null |
| 193 | /// return value of the allocation function |
| 194 | /// \param NumElements - the computed number of elements, |
| 195 | /// potentially collapsed from the multidimensional array case |
| 196 | /// \param ElementType - the base element allocated type, |
| 197 | /// i.e. the allocated type after stripping all array types |
| 198 | virtual llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF, |
| 199 | llvm::Value *NewPtr, |
| 200 | llvm::Value *NumElements, |
| 201 | QualType ElementType); |
| 202 | |
| 203 | /// Reads the array cookie associated with the given pointer, |
| 204 | /// if it has one. |
| 205 | /// |
| 206 | /// \param Ptr - a pointer to the first element in the array |
| 207 | /// \param ElementType - the base element type of elements of the array |
| 208 | /// \param NumElements - an out parameter which will be initialized |
| 209 | /// with the number of elements allocated, or zero if there is no |
| 210 | /// cookie |
| 211 | /// \param AllocPtr - an out parameter which will be initialized |
| 212 | /// with a char* pointing to the address returned by the allocation |
| 213 | /// function |
| 214 | /// \param CookieSize - an out parameter which will be initialized |
| 215 | /// with the size of the cookie, or zero if there is no cookie |
| 216 | virtual void ReadArrayCookie(CodeGenFunction &CGF, llvm::Value *Ptr, |
| 217 | QualType ElementType, llvm::Value *&NumElements, |
| 218 | llvm::Value *&AllocPtr, CharUnits &CookieSize); |
| 219 | |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 220 | /*************************** Static local guards ****************************/ |
| 221 | |
John McCall | 3030eb8 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 222 | /// Emits the guarded initializer and destructor setup for the given |
| 223 | /// variable, given that it couldn't be emitted as a constant. |
| 224 | /// |
| 225 | /// The variable may be: |
| 226 | /// - a static local variable |
| 227 | /// - a static data member of a class template instantiation |
| 228 | virtual void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D, |
| 229 | llvm::GlobalVariable *DeclPtr); |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 230 | |
Charles Davis | 3a811f1 | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 231 | }; |
| 232 | |
| 233 | /// Creates an instance of a C++ ABI class. |
John McCall | ee79a4c | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 234 | CGCXXABI *CreateARMCXXABI(CodeGenModule &CGM); |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 235 | CGCXXABI *CreateItaniumCXXABI(CodeGenModule &CGM); |
| 236 | CGCXXABI *CreateMicrosoftCXXABI(CodeGenModule &CGM); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 237 | |
Charles Davis | 3a811f1 | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | |
| 241 | #endif |