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