Charles Davis | 3a811f1 | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 1 | //===------- ItaniumCXXABI.cpp - Emit LLVM Code from ASTs for a Module ----===// |
| 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 | // |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 10 | // This provides C++ code generation targeting the Itanium C++ ABI. The class |
Charles Davis | 3a811f1 | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 11 | // in this file generates structures that follow the Itanium C++ ABI, which is |
| 12 | // documented at: |
| 13 | // http://www.codesourcery.com/public/cxx-abi/abi.html |
| 14 | // http://www.codesourcery.com/public/cxx-abi/abi-eh.html |
John McCall | ee79a4c | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 15 | // |
| 16 | // It also supports the closely-related ARM ABI, documented at: |
| 17 | // http://infocenter.arm.com/help/topic/com.arm.doc.ihi0041c/IHI0041C_cppabi.pdf |
| 18 | // |
Charles Davis | 3a811f1 | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 19 | //===----------------------------------------------------------------------===// |
| 20 | |
| 21 | #include "CGCXXABI.h" |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 22 | #include "CGRecordLayout.h" |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 23 | #include "CodeGenFunction.h" |
Charles Davis | 3a811f1 | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 24 | #include "CodeGenModule.h" |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 25 | #include <clang/AST/Mangle.h> |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 26 | #include <clang/AST/Type.h> |
John McCall | 0502a22 | 2011-06-17 07:33:57 +0000 | [diff] [blame] | 27 | #include <llvm/Intrinsics.h> |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 28 | #include <llvm/Target/TargetData.h> |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 29 | #include <llvm/Value.h> |
Charles Davis | 3a811f1 | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 30 | |
| 31 | using namespace clang; |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 32 | using namespace CodeGen; |
Charles Davis | 3a811f1 | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 33 | |
| 34 | namespace { |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 35 | class ItaniumCXXABI : public CodeGen::CGCXXABI { |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 36 | private: |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 37 | llvm::IntegerType *PtrDiffTy; |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 38 | protected: |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 39 | bool IsARM; |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 40 | |
| 41 | // It's a little silly for us to cache this. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 42 | llvm::IntegerType *getPtrDiffTy() { |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 43 | if (!PtrDiffTy) { |
John McCall | 9cb2cee | 2010-09-02 10:25:57 +0000 | [diff] [blame] | 44 | QualType T = getContext().getPointerDiffType(); |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 45 | llvm::Type *Ty = CGM.getTypes().ConvertType(T); |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 46 | PtrDiffTy = cast<llvm::IntegerType>(Ty); |
| 47 | } |
| 48 | return PtrDiffTy; |
| 49 | } |
| 50 | |
Charles Davis | 3a811f1 | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 51 | public: |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 52 | ItaniumCXXABI(CodeGen::CodeGenModule &CGM, bool IsARM = false) : |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 53 | CGCXXABI(CGM), PtrDiffTy(0), IsARM(IsARM) { } |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 54 | |
John McCall | f16aa10 | 2010-08-22 21:01:12 +0000 | [diff] [blame] | 55 | bool isZeroInitializable(const MemberPointerType *MPT); |
John McCall | cf2c85e | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 56 | |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 57 | llvm::Type *ConvertMemberPointerType(const MemberPointerType *MPT); |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 58 | |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 59 | llvm::Value *EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF, |
| 60 | llvm::Value *&This, |
| 61 | llvm::Value *MemFnPtr, |
| 62 | const MemberPointerType *MPT); |
John McCall | 3023def | 2010-08-22 03:04:22 +0000 | [diff] [blame] | 63 | |
John McCall | 6c2ab1d | 2010-08-31 21:07:20 +0000 | [diff] [blame] | 64 | llvm::Value *EmitMemberDataPointerAddress(CodeGenFunction &CGF, |
| 65 | llvm::Value *Base, |
| 66 | llvm::Value *MemPtr, |
| 67 | const MemberPointerType *MPT); |
| 68 | |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 69 | llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF, |
| 70 | const CastExpr *E, |
| 71 | llvm::Value *Src); |
John McCall | 4d4e5c1 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 72 | llvm::Constant *EmitMemberPointerConversion(const CastExpr *E, |
| 73 | llvm::Constant *Src); |
John McCall | cf2c85e | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 74 | |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 75 | llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT); |
John McCall | cf2c85e | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 76 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 77 | llvm::Constant *EmitMemberPointer(const CXXMethodDecl *MD); |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 78 | llvm::Constant *EmitMemberDataPointer(const MemberPointerType *MPT, |
| 79 | CharUnits offset); |
Richard Smith | 2d6a567 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 80 | llvm::Constant *EmitMemberPointer(const APValue &MP, QualType MPT); |
| 81 | llvm::Constant *BuildMemberPointer(const CXXMethodDecl *MD, |
| 82 | CharUnits ThisAdjustment); |
John McCall | 875ab10 | 2010-08-22 06:43:33 +0000 | [diff] [blame] | 83 | |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 84 | llvm::Value *EmitMemberPointerComparison(CodeGenFunction &CGF, |
| 85 | llvm::Value *L, |
| 86 | llvm::Value *R, |
| 87 | const MemberPointerType *MPT, |
| 88 | bool Inequality); |
John McCall | e9fd7eb | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 89 | |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 90 | llvm::Value *EmitMemberPointerIsNotNull(CodeGenFunction &CGF, |
| 91 | llvm::Value *Addr, |
| 92 | const MemberPointerType *MPT); |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 93 | |
| 94 | void BuildConstructorSignature(const CXXConstructorDecl *Ctor, |
| 95 | CXXCtorType T, |
| 96 | CanQualType &ResTy, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 97 | SmallVectorImpl<CanQualType> &ArgTys); |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 98 | |
| 99 | void BuildDestructorSignature(const CXXDestructorDecl *Dtor, |
| 100 | CXXDtorType T, |
| 101 | CanQualType &ResTy, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 102 | SmallVectorImpl<CanQualType> &ArgTys); |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 103 | |
| 104 | void BuildInstanceFunctionParams(CodeGenFunction &CGF, |
| 105 | QualType &ResTy, |
| 106 | FunctionArgList &Params); |
| 107 | |
| 108 | void EmitInstanceFunctionProlog(CodeGenFunction &CGF); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 109 | |
John McCall | e2b45e2 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 110 | CharUnits getArrayCookieSizeImpl(QualType elementType); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 111 | llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF, |
| 112 | llvm::Value *NewPtr, |
| 113 | llvm::Value *NumElements, |
John McCall | 6ec278d | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 114 | const CXXNewExpr *expr, |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 115 | QualType ElementType); |
John McCall | e2b45e2 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 116 | llvm::Value *readArrayCookieImpl(CodeGenFunction &CGF, |
| 117 | llvm::Value *allocPtr, |
| 118 | CharUnits cookieSize); |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 119 | |
John McCall | 3030eb8 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 120 | void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D, |
Chandler Carruth | 0f30a12 | 2012-03-30 19:44:53 +0000 | [diff] [blame] | 121 | llvm::GlobalVariable *DeclPtr, bool PerformInit); |
John McCall | 20bb175 | 2012-05-01 06:13:13 +0000 | [diff] [blame] | 122 | void registerGlobalDtor(CodeGenFunction &CGF, llvm::Constant *dtor, |
| 123 | llvm::Constant *addr); |
Charles Davis | 3a811f1 | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 124 | }; |
John McCall | ee79a4c | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 125 | |
| 126 | class ARMCXXABI : public ItaniumCXXABI { |
| 127 | public: |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 128 | ARMCXXABI(CodeGen::CodeGenModule &CGM) : ItaniumCXXABI(CGM, /*ARM*/ true) {} |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 129 | |
| 130 | void BuildConstructorSignature(const CXXConstructorDecl *Ctor, |
| 131 | CXXCtorType T, |
| 132 | CanQualType &ResTy, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 133 | SmallVectorImpl<CanQualType> &ArgTys); |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 134 | |
| 135 | void BuildDestructorSignature(const CXXDestructorDecl *Dtor, |
| 136 | CXXDtorType T, |
| 137 | CanQualType &ResTy, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 138 | SmallVectorImpl<CanQualType> &ArgTys); |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 139 | |
| 140 | void BuildInstanceFunctionParams(CodeGenFunction &CGF, |
| 141 | QualType &ResTy, |
| 142 | FunctionArgList &Params); |
| 143 | |
| 144 | void EmitInstanceFunctionProlog(CodeGenFunction &CGF); |
| 145 | |
| 146 | void EmitReturnFromThunk(CodeGenFunction &CGF, RValue RV, QualType ResTy); |
| 147 | |
John McCall | e2b45e2 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 148 | CharUnits getArrayCookieSizeImpl(QualType elementType); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 149 | llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF, |
| 150 | llvm::Value *NewPtr, |
| 151 | llvm::Value *NumElements, |
John McCall | 6ec278d | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 152 | const CXXNewExpr *expr, |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 153 | QualType ElementType); |
John McCall | e2b45e2 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 154 | llvm::Value *readArrayCookieImpl(CodeGenFunction &CGF, llvm::Value *allocPtr, |
| 155 | CharUnits cookieSize); |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 156 | |
| 157 | private: |
| 158 | /// \brief Returns true if the given instance method is one of the |
| 159 | /// kinds that the ARM ABI says returns 'this'. |
| 160 | static bool HasThisReturn(GlobalDecl GD) { |
| 161 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
| 162 | return ((isa<CXXDestructorDecl>(MD) && GD.getDtorType() != Dtor_Deleting) || |
| 163 | (isa<CXXConstructorDecl>(MD))); |
| 164 | } |
John McCall | ee79a4c | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 165 | }; |
Charles Davis | 3a811f1 | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 168 | CodeGen::CGCXXABI *CodeGen::CreateItaniumCXXABI(CodeGenModule &CGM) { |
Charles Davis | 3a811f1 | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 169 | return new ItaniumCXXABI(CGM); |
| 170 | } |
| 171 | |
John McCall | ee79a4c | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 172 | CodeGen::CGCXXABI *CodeGen::CreateARMCXXABI(CodeGenModule &CGM) { |
| 173 | return new ARMCXXABI(CGM); |
| 174 | } |
| 175 | |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 176 | llvm::Type * |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 177 | ItaniumCXXABI::ConvertMemberPointerType(const MemberPointerType *MPT) { |
| 178 | if (MPT->isMemberDataPointer()) |
| 179 | return getPtrDiffTy(); |
Chris Lattner | 7650d95 | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 180 | return llvm::StructType::get(getPtrDiffTy(), getPtrDiffTy(), NULL); |
John McCall | 875ab10 | 2010-08-22 06:43:33 +0000 | [diff] [blame] | 181 | } |
| 182 | |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 183 | /// In the Itanium and ARM ABIs, method pointers have the form: |
| 184 | /// struct { ptrdiff_t ptr; ptrdiff_t adj; } memptr; |
| 185 | /// |
| 186 | /// In the Itanium ABI: |
| 187 | /// - method pointers are virtual if (memptr.ptr & 1) is nonzero |
| 188 | /// - the this-adjustment is (memptr.adj) |
| 189 | /// - the virtual offset is (memptr.ptr - 1) |
| 190 | /// |
| 191 | /// In the ARM ABI: |
| 192 | /// - method pointers are virtual if (memptr.adj & 1) is nonzero |
| 193 | /// - the this-adjustment is (memptr.adj >> 1) |
| 194 | /// - the virtual offset is (memptr.ptr) |
| 195 | /// ARM uses 'adj' for the virtual flag because Thumb functions |
| 196 | /// may be only single-byte aligned. |
| 197 | /// |
| 198 | /// If the member is virtual, the adjusted 'this' pointer points |
| 199 | /// to a vtable pointer from which the virtual offset is applied. |
| 200 | /// |
| 201 | /// If the member is non-virtual, memptr.ptr is the address of |
| 202 | /// the function to call. |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 203 | llvm::Value * |
| 204 | ItaniumCXXABI::EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF, |
| 205 | llvm::Value *&This, |
| 206 | llvm::Value *MemFnPtr, |
| 207 | const MemberPointerType *MPT) { |
| 208 | CGBuilderTy &Builder = CGF.Builder; |
| 209 | |
| 210 | const FunctionProtoType *FPT = |
| 211 | MPT->getPointeeType()->getAs<FunctionProtoType>(); |
| 212 | const CXXRecordDecl *RD = |
| 213 | cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl()); |
| 214 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 215 | llvm::FunctionType *FTy = |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 216 | CGM.getTypes().GetFunctionType( |
| 217 | CGM.getTypes().arrangeCXXMethodType(RD, FPT)); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 218 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 219 | llvm::IntegerType *ptrdiff = getPtrDiffTy(); |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 220 | llvm::Constant *ptrdiff_1 = llvm::ConstantInt::get(ptrdiff, 1); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 221 | |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 222 | llvm::BasicBlock *FnVirtual = CGF.createBasicBlock("memptr.virtual"); |
| 223 | llvm::BasicBlock *FnNonVirtual = CGF.createBasicBlock("memptr.nonvirtual"); |
| 224 | llvm::BasicBlock *FnEnd = CGF.createBasicBlock("memptr.end"); |
| 225 | |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 226 | // Extract memptr.adj, which is in the second field. |
| 227 | llvm::Value *RawAdj = Builder.CreateExtractValue(MemFnPtr, 1, "memptr.adj"); |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 228 | |
| 229 | // Compute the true adjustment. |
| 230 | llvm::Value *Adj = RawAdj; |
| 231 | if (IsARM) |
| 232 | Adj = Builder.CreateAShr(Adj, ptrdiff_1, "memptr.adj.shifted"); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 233 | |
| 234 | // Apply the adjustment and cast back to the original struct type |
| 235 | // for consistency. |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 236 | llvm::Value *Ptr = Builder.CreateBitCast(This, Builder.getInt8PtrTy()); |
| 237 | Ptr = Builder.CreateInBoundsGEP(Ptr, Adj); |
| 238 | This = Builder.CreateBitCast(Ptr, This->getType(), "this.adjusted"); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 239 | |
| 240 | // Load the function pointer. |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 241 | llvm::Value *FnAsInt = Builder.CreateExtractValue(MemFnPtr, 0, "memptr.ptr"); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 242 | |
| 243 | // If the LSB in the function pointer is 1, the function pointer points to |
| 244 | // a virtual function. |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 245 | llvm::Value *IsVirtual; |
| 246 | if (IsARM) |
| 247 | IsVirtual = Builder.CreateAnd(RawAdj, ptrdiff_1); |
| 248 | else |
| 249 | IsVirtual = Builder.CreateAnd(FnAsInt, ptrdiff_1); |
| 250 | IsVirtual = Builder.CreateIsNotNull(IsVirtual, "memptr.isvirtual"); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 251 | Builder.CreateCondBr(IsVirtual, FnVirtual, FnNonVirtual); |
| 252 | |
| 253 | // In the virtual path, the adjustment left 'This' pointing to the |
| 254 | // vtable of the correct base subobject. The "function pointer" is an |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 255 | // offset within the vtable (+1 for the virtual flag on non-ARM). |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 256 | CGF.EmitBlock(FnVirtual); |
| 257 | |
| 258 | // Cast the adjusted this to a pointer to vtable pointer and load. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 259 | llvm::Type *VTableTy = Builder.getInt8PtrTy(); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 260 | llvm::Value *VTable = Builder.CreateBitCast(This, VTableTy->getPointerTo()); |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 261 | VTable = Builder.CreateLoad(VTable, "memptr.vtable"); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 262 | |
| 263 | // Apply the offset. |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 264 | llvm::Value *VTableOffset = FnAsInt; |
| 265 | if (!IsARM) VTableOffset = Builder.CreateSub(VTableOffset, ptrdiff_1); |
| 266 | VTable = Builder.CreateGEP(VTable, VTableOffset); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 267 | |
| 268 | // Load the virtual function to call. |
| 269 | VTable = Builder.CreateBitCast(VTable, FTy->getPointerTo()->getPointerTo()); |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 270 | llvm::Value *VirtualFn = Builder.CreateLoad(VTable, "memptr.virtualfn"); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 271 | CGF.EmitBranch(FnEnd); |
| 272 | |
| 273 | // In the non-virtual path, the function pointer is actually a |
| 274 | // function pointer. |
| 275 | CGF.EmitBlock(FnNonVirtual); |
| 276 | llvm::Value *NonVirtualFn = |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 277 | Builder.CreateIntToPtr(FnAsInt, FTy->getPointerTo(), "memptr.nonvirtualfn"); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 278 | |
| 279 | // We're done. |
| 280 | CGF.EmitBlock(FnEnd); |
Jay Foad | bbf3bac | 2011-03-30 11:28:58 +0000 | [diff] [blame] | 281 | llvm::PHINode *Callee = Builder.CreatePHI(FTy->getPointerTo(), 2); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 282 | Callee->addIncoming(VirtualFn, FnVirtual); |
| 283 | Callee->addIncoming(NonVirtualFn, FnNonVirtual); |
| 284 | return Callee; |
| 285 | } |
John McCall | 3023def | 2010-08-22 03:04:22 +0000 | [diff] [blame] | 286 | |
John McCall | 6c2ab1d | 2010-08-31 21:07:20 +0000 | [diff] [blame] | 287 | /// Compute an l-value by applying the given pointer-to-member to a |
| 288 | /// base object. |
| 289 | llvm::Value *ItaniumCXXABI::EmitMemberDataPointerAddress(CodeGenFunction &CGF, |
| 290 | llvm::Value *Base, |
| 291 | llvm::Value *MemPtr, |
| 292 | const MemberPointerType *MPT) { |
| 293 | assert(MemPtr->getType() == getPtrDiffTy()); |
| 294 | |
| 295 | CGBuilderTy &Builder = CGF.Builder; |
| 296 | |
| 297 | unsigned AS = cast<llvm::PointerType>(Base->getType())->getAddressSpace(); |
| 298 | |
| 299 | // Cast to char*. |
| 300 | Base = Builder.CreateBitCast(Base, Builder.getInt8Ty()->getPointerTo(AS)); |
| 301 | |
| 302 | // Apply the offset, which we assume is non-null. |
| 303 | llvm::Value *Addr = Builder.CreateInBoundsGEP(Base, MemPtr, "memptr.offset"); |
| 304 | |
| 305 | // Cast the address to the appropriate pointer type, adopting the |
| 306 | // address space of the base pointer. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 307 | llvm::Type *PType |
Douglas Gregor | eede61a | 2010-09-02 17:38:50 +0000 | [diff] [blame] | 308 | = CGF.ConvertTypeForMem(MPT->getPointeeType())->getPointerTo(AS); |
John McCall | 6c2ab1d | 2010-08-31 21:07:20 +0000 | [diff] [blame] | 309 | return Builder.CreateBitCast(Addr, PType); |
| 310 | } |
| 311 | |
John McCall | 4d4e5c1 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 312 | /// Perform a bitcast, derived-to-base, or base-to-derived member pointer |
| 313 | /// conversion. |
| 314 | /// |
| 315 | /// Bitcast conversions are always a no-op under Itanium. |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 316 | /// |
| 317 | /// Obligatory offset/adjustment diagram: |
| 318 | /// <-- offset --> <-- adjustment --> |
| 319 | /// |--------------------------|----------------------|--------------------| |
| 320 | /// ^Derived address point ^Base address point ^Member address point |
| 321 | /// |
| 322 | /// So when converting a base member pointer to a derived member pointer, |
| 323 | /// we add the offset to the adjustment because the address point has |
| 324 | /// decreased; and conversely, when converting a derived MP to a base MP |
| 325 | /// we subtract the offset from the adjustment because the address point |
| 326 | /// has increased. |
| 327 | /// |
| 328 | /// The standard forbids (at compile time) conversion to and from |
| 329 | /// virtual bases, which is why we don't have to consider them here. |
| 330 | /// |
| 331 | /// The standard forbids (at run time) casting a derived MP to a base |
| 332 | /// MP when the derived MP does not point to a member of the base. |
| 333 | /// This is why -1 is a reasonable choice for null data member |
| 334 | /// pointers. |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 335 | llvm::Value * |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 336 | ItaniumCXXABI::EmitMemberPointerConversion(CodeGenFunction &CGF, |
| 337 | const CastExpr *E, |
John McCall | 4d4e5c1 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 338 | llvm::Value *src) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 339 | assert(E->getCastKind() == CK_DerivedToBaseMemberPointer || |
John McCall | 4d4e5c1 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 340 | E->getCastKind() == CK_BaseToDerivedMemberPointer || |
| 341 | E->getCastKind() == CK_ReinterpretMemberPointer); |
| 342 | |
| 343 | // Under Itanium, reinterprets don't require any additional processing. |
| 344 | if (E->getCastKind() == CK_ReinterpretMemberPointer) return src; |
| 345 | |
| 346 | // Use constant emission if we can. |
| 347 | if (isa<llvm::Constant>(src)) |
| 348 | return EmitMemberPointerConversion(E, cast<llvm::Constant>(src)); |
| 349 | |
| 350 | llvm::Constant *adj = getMemberPointerAdjustment(E); |
| 351 | if (!adj) return src; |
John McCall | 3023def | 2010-08-22 03:04:22 +0000 | [diff] [blame] | 352 | |
| 353 | CGBuilderTy &Builder = CGF.Builder; |
John McCall | 4d4e5c1 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 354 | bool isDerivedToBase = (E->getCastKind() == CK_DerivedToBaseMemberPointer); |
John McCall | 3023def | 2010-08-22 03:04:22 +0000 | [diff] [blame] | 355 | |
John McCall | 4d4e5c1 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 356 | const MemberPointerType *destTy = |
| 357 | E->getType()->castAs<MemberPointerType>(); |
John McCall | 875ab10 | 2010-08-22 06:43:33 +0000 | [diff] [blame] | 358 | |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 359 | // For member data pointers, this is just a matter of adding the |
| 360 | // offset if the source is non-null. |
John McCall | 4d4e5c1 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 361 | if (destTy->isMemberDataPointer()) { |
| 362 | llvm::Value *dst; |
| 363 | if (isDerivedToBase) |
| 364 | dst = Builder.CreateNSWSub(src, adj, "adj"); |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 365 | else |
John McCall | 4d4e5c1 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 366 | dst = Builder.CreateNSWAdd(src, adj, "adj"); |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 367 | |
| 368 | // Null check. |
John McCall | 4d4e5c1 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 369 | llvm::Value *null = llvm::Constant::getAllOnesValue(src->getType()); |
| 370 | llvm::Value *isNull = Builder.CreateICmpEQ(src, null, "memptr.isnull"); |
| 371 | return Builder.CreateSelect(isNull, src, dst); |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 372 | } |
| 373 | |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 374 | // The this-adjustment is left-shifted by 1 on ARM. |
| 375 | if (IsARM) { |
John McCall | 4d4e5c1 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 376 | uint64_t offset = cast<llvm::ConstantInt>(adj)->getZExtValue(); |
| 377 | offset <<= 1; |
| 378 | adj = llvm::ConstantInt::get(adj->getType(), offset); |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 379 | } |
| 380 | |
John McCall | 4d4e5c1 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 381 | llvm::Value *srcAdj = Builder.CreateExtractValue(src, 1, "src.adj"); |
| 382 | llvm::Value *dstAdj; |
| 383 | if (isDerivedToBase) |
| 384 | dstAdj = Builder.CreateNSWSub(srcAdj, adj, "adj"); |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 385 | else |
John McCall | 4d4e5c1 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 386 | dstAdj = Builder.CreateNSWAdd(srcAdj, adj, "adj"); |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 387 | |
John McCall | 4d4e5c1 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 388 | return Builder.CreateInsertValue(src, dstAdj, 1); |
| 389 | } |
| 390 | |
| 391 | llvm::Constant * |
| 392 | ItaniumCXXABI::EmitMemberPointerConversion(const CastExpr *E, |
| 393 | llvm::Constant *src) { |
| 394 | assert(E->getCastKind() == CK_DerivedToBaseMemberPointer || |
| 395 | E->getCastKind() == CK_BaseToDerivedMemberPointer || |
| 396 | E->getCastKind() == CK_ReinterpretMemberPointer); |
| 397 | |
| 398 | // Under Itanium, reinterprets don't require any additional processing. |
| 399 | if (E->getCastKind() == CK_ReinterpretMemberPointer) return src; |
| 400 | |
| 401 | // If the adjustment is trivial, we don't need to do anything. |
| 402 | llvm::Constant *adj = getMemberPointerAdjustment(E); |
| 403 | if (!adj) return src; |
| 404 | |
| 405 | bool isDerivedToBase = (E->getCastKind() == CK_DerivedToBaseMemberPointer); |
| 406 | |
| 407 | const MemberPointerType *destTy = |
| 408 | E->getType()->castAs<MemberPointerType>(); |
| 409 | |
| 410 | // For member data pointers, this is just a matter of adding the |
| 411 | // offset if the source is non-null. |
| 412 | if (destTy->isMemberDataPointer()) { |
| 413 | // null maps to null. |
| 414 | if (src->isAllOnesValue()) return src; |
| 415 | |
| 416 | if (isDerivedToBase) |
| 417 | return llvm::ConstantExpr::getNSWSub(src, adj); |
| 418 | else |
| 419 | return llvm::ConstantExpr::getNSWAdd(src, adj); |
| 420 | } |
| 421 | |
| 422 | // The this-adjustment is left-shifted by 1 on ARM. |
| 423 | if (IsARM) { |
| 424 | uint64_t offset = cast<llvm::ConstantInt>(adj)->getZExtValue(); |
| 425 | offset <<= 1; |
| 426 | adj = llvm::ConstantInt::get(adj->getType(), offset); |
| 427 | } |
| 428 | |
| 429 | llvm::Constant *srcAdj = llvm::ConstantExpr::getExtractValue(src, 1); |
| 430 | llvm::Constant *dstAdj; |
| 431 | if (isDerivedToBase) |
| 432 | dstAdj = llvm::ConstantExpr::getNSWSub(srcAdj, adj); |
| 433 | else |
| 434 | dstAdj = llvm::ConstantExpr::getNSWAdd(srcAdj, adj); |
| 435 | |
| 436 | return llvm::ConstantExpr::getInsertValue(src, dstAdj, 1); |
John McCall | 3023def | 2010-08-22 03:04:22 +0000 | [diff] [blame] | 437 | } |
John McCall | cf2c85e | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 438 | |
| 439 | llvm::Constant * |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 440 | ItaniumCXXABI::EmitNullMemberPointer(const MemberPointerType *MPT) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 441 | llvm::Type *ptrdiff_t = getPtrDiffTy(); |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 442 | |
| 443 | // Itanium C++ ABI 2.3: |
| 444 | // A NULL pointer is represented as -1. |
| 445 | if (MPT->isMemberDataPointer()) |
| 446 | return llvm::ConstantInt::get(ptrdiff_t, -1ULL, /*isSigned=*/true); |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 447 | |
| 448 | llvm::Constant *Zero = llvm::ConstantInt::get(ptrdiff_t, 0); |
| 449 | llvm::Constant *Values[2] = { Zero, Zero }; |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 450 | return llvm::ConstantStruct::getAnon(Values); |
John McCall | cf2c85e | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 451 | } |
| 452 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 453 | llvm::Constant * |
| 454 | ItaniumCXXABI::EmitMemberDataPointer(const MemberPointerType *MPT, |
| 455 | CharUnits offset) { |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 456 | // Itanium C++ ABI 2.3: |
| 457 | // A pointer to data member is an offset from the base address of |
| 458 | // the class object containing it, represented as a ptrdiff_t |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 459 | return llvm::ConstantInt::get(getPtrDiffTy(), offset.getQuantity()); |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 460 | } |
| 461 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 462 | llvm::Constant *ItaniumCXXABI::EmitMemberPointer(const CXXMethodDecl *MD) { |
Richard Smith | 2d6a567 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 463 | return BuildMemberPointer(MD, CharUnits::Zero()); |
| 464 | } |
| 465 | |
| 466 | llvm::Constant *ItaniumCXXABI::BuildMemberPointer(const CXXMethodDecl *MD, |
| 467 | CharUnits ThisAdjustment) { |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 468 | assert(MD->isInstance() && "Member function must not be static!"); |
| 469 | MD = MD->getCanonicalDecl(); |
| 470 | |
| 471 | CodeGenTypes &Types = CGM.getTypes(); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 472 | llvm::Type *ptrdiff_t = getPtrDiffTy(); |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 473 | |
| 474 | // Get the function pointer (or index if this is a virtual function). |
| 475 | llvm::Constant *MemPtr[2]; |
| 476 | if (MD->isVirtual()) { |
Peter Collingbourne | 1d2b317 | 2011-09-26 01:56:30 +0000 | [diff] [blame] | 477 | uint64_t Index = CGM.getVTableContext().getMethodVTableIndex(MD); |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 478 | |
Ken Dyck | 1246ba6 | 2011-04-09 01:30:02 +0000 | [diff] [blame] | 479 | const ASTContext &Context = getContext(); |
| 480 | CharUnits PointerWidth = |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 481 | Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0)); |
Ken Dyck | 1246ba6 | 2011-04-09 01:30:02 +0000 | [diff] [blame] | 482 | uint64_t VTableOffset = (Index * PointerWidth.getQuantity()); |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 483 | |
| 484 | if (IsARM) { |
| 485 | // ARM C++ ABI 3.2.1: |
| 486 | // This ABI specifies that adj contains twice the this |
| 487 | // adjustment, plus 1 if the member function is virtual. The |
| 488 | // least significant bit of adj then makes exactly the same |
| 489 | // discrimination as the least significant bit of ptr does for |
| 490 | // Itanium. |
| 491 | MemPtr[0] = llvm::ConstantInt::get(ptrdiff_t, VTableOffset); |
Richard Smith | 2d6a567 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 492 | MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t, |
| 493 | 2 * ThisAdjustment.getQuantity() + 1); |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 494 | } else { |
| 495 | // Itanium C++ ABI 2.3: |
| 496 | // For a virtual function, [the pointer field] is 1 plus the |
| 497 | // virtual table offset (in bytes) of the function, |
| 498 | // represented as a ptrdiff_t. |
| 499 | MemPtr[0] = llvm::ConstantInt::get(ptrdiff_t, VTableOffset + 1); |
Richard Smith | 2d6a567 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 500 | MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t, |
| 501 | ThisAdjustment.getQuantity()); |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 502 | } |
| 503 | } else { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 504 | const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>(); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 505 | llvm::Type *Ty; |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 506 | // Check whether the function has a computable LLVM signature. |
Chris Lattner | f742eb0 | 2011-07-10 00:18:59 +0000 | [diff] [blame] | 507 | if (Types.isFuncTypeConvertible(FPT)) { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 508 | // The function has a computable LLVM signature; use the correct type. |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 509 | Ty = Types.GetFunctionType(Types.arrangeCXXMethodDeclaration(MD)); |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 510 | } else { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 511 | // Use an arbitrary non-function type to tell GetAddrOfFunction that the |
| 512 | // function type is incomplete. |
| 513 | Ty = ptrdiff_t; |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 514 | } |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 515 | llvm::Constant *addr = CGM.GetAddrOfFunction(MD, Ty); |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 516 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 517 | MemPtr[0] = llvm::ConstantExpr::getPtrToInt(addr, ptrdiff_t); |
Richard Smith | 2d6a567 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 518 | MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t, (IsARM ? 2 : 1) * |
| 519 | ThisAdjustment.getQuantity()); |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 520 | } |
John McCall | 875ab10 | 2010-08-22 06:43:33 +0000 | [diff] [blame] | 521 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 522 | return llvm::ConstantStruct::getAnon(MemPtr); |
John McCall | 875ab10 | 2010-08-22 06:43:33 +0000 | [diff] [blame] | 523 | } |
| 524 | |
Richard Smith | 2d6a567 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 525 | llvm::Constant *ItaniumCXXABI::EmitMemberPointer(const APValue &MP, |
| 526 | QualType MPType) { |
| 527 | const MemberPointerType *MPT = MPType->castAs<MemberPointerType>(); |
| 528 | const ValueDecl *MPD = MP.getMemberPointerDecl(); |
| 529 | if (!MPD) |
| 530 | return EmitNullMemberPointer(MPT); |
| 531 | |
| 532 | // Compute the this-adjustment. |
| 533 | CharUnits ThisAdjustment = CharUnits::Zero(); |
| 534 | ArrayRef<const CXXRecordDecl*> Path = MP.getMemberPointerPath(); |
| 535 | bool DerivedMember = MP.isMemberPointerToDerivedMember(); |
| 536 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(MPD->getDeclContext()); |
| 537 | for (unsigned I = 0, N = Path.size(); I != N; ++I) { |
| 538 | const CXXRecordDecl *Base = RD; |
| 539 | const CXXRecordDecl *Derived = Path[I]; |
| 540 | if (DerivedMember) |
| 541 | std::swap(Base, Derived); |
| 542 | ThisAdjustment += |
| 543 | getContext().getASTRecordLayout(Derived).getBaseClassOffset(Base); |
| 544 | RD = Path[I]; |
| 545 | } |
| 546 | if (DerivedMember) |
| 547 | ThisAdjustment = -ThisAdjustment; |
| 548 | |
| 549 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MPD)) |
| 550 | return BuildMemberPointer(MD, ThisAdjustment); |
| 551 | |
| 552 | CharUnits FieldOffset = |
| 553 | getContext().toCharUnitsFromBits(getContext().getFieldOffset(MPD)); |
| 554 | return EmitMemberDataPointer(MPT, ThisAdjustment + FieldOffset); |
| 555 | } |
| 556 | |
John McCall | e9fd7eb | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 557 | /// The comparison algorithm is pretty easy: the member pointers are |
| 558 | /// the same if they're either bitwise identical *or* both null. |
| 559 | /// |
| 560 | /// ARM is different here only because null-ness is more complicated. |
| 561 | llvm::Value * |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 562 | ItaniumCXXABI::EmitMemberPointerComparison(CodeGenFunction &CGF, |
| 563 | llvm::Value *L, |
| 564 | llvm::Value *R, |
| 565 | const MemberPointerType *MPT, |
| 566 | bool Inequality) { |
John McCall | e9fd7eb | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 567 | CGBuilderTy &Builder = CGF.Builder; |
| 568 | |
John McCall | e9fd7eb | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 569 | llvm::ICmpInst::Predicate Eq; |
| 570 | llvm::Instruction::BinaryOps And, Or; |
| 571 | if (Inequality) { |
| 572 | Eq = llvm::ICmpInst::ICMP_NE; |
| 573 | And = llvm::Instruction::Or; |
| 574 | Or = llvm::Instruction::And; |
| 575 | } else { |
| 576 | Eq = llvm::ICmpInst::ICMP_EQ; |
| 577 | And = llvm::Instruction::And; |
| 578 | Or = llvm::Instruction::Or; |
| 579 | } |
| 580 | |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 581 | // Member data pointers are easy because there's a unique null |
| 582 | // value, so it just comes down to bitwise equality. |
| 583 | if (MPT->isMemberDataPointer()) |
| 584 | return Builder.CreateICmp(Eq, L, R); |
| 585 | |
| 586 | // For member function pointers, the tautologies are more complex. |
| 587 | // The Itanium tautology is: |
John McCall | de719f7 | 2010-08-23 06:56:36 +0000 | [diff] [blame] | 588 | // (L == R) <==> (L.ptr == R.ptr && (L.ptr == 0 || L.adj == R.adj)) |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 589 | // The ARM tautology is: |
John McCall | de719f7 | 2010-08-23 06:56:36 +0000 | [diff] [blame] | 590 | // (L == R) <==> (L.ptr == R.ptr && |
| 591 | // (L.adj == R.adj || |
| 592 | // (L.ptr == 0 && ((L.adj|R.adj) & 1) == 0))) |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 593 | // The inequality tautologies have exactly the same structure, except |
| 594 | // applying De Morgan's laws. |
| 595 | |
| 596 | llvm::Value *LPtr = Builder.CreateExtractValue(L, 0, "lhs.memptr.ptr"); |
| 597 | llvm::Value *RPtr = Builder.CreateExtractValue(R, 0, "rhs.memptr.ptr"); |
| 598 | |
John McCall | e9fd7eb | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 599 | // This condition tests whether L.ptr == R.ptr. This must always be |
| 600 | // true for equality to hold. |
| 601 | llvm::Value *PtrEq = Builder.CreateICmp(Eq, LPtr, RPtr, "cmp.ptr"); |
| 602 | |
| 603 | // This condition, together with the assumption that L.ptr == R.ptr, |
| 604 | // tests whether the pointers are both null. ARM imposes an extra |
| 605 | // condition. |
| 606 | llvm::Value *Zero = llvm::Constant::getNullValue(LPtr->getType()); |
| 607 | llvm::Value *EqZero = Builder.CreateICmp(Eq, LPtr, Zero, "cmp.ptr.null"); |
| 608 | |
| 609 | // This condition tests whether L.adj == R.adj. If this isn't |
| 610 | // true, the pointers are unequal unless they're both null. |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 611 | llvm::Value *LAdj = Builder.CreateExtractValue(L, 1, "lhs.memptr.adj"); |
| 612 | llvm::Value *RAdj = Builder.CreateExtractValue(R, 1, "rhs.memptr.adj"); |
John McCall | e9fd7eb | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 613 | llvm::Value *AdjEq = Builder.CreateICmp(Eq, LAdj, RAdj, "cmp.adj"); |
| 614 | |
| 615 | // Null member function pointers on ARM clear the low bit of Adj, |
| 616 | // so the zero condition has to check that neither low bit is set. |
| 617 | if (IsARM) { |
| 618 | llvm::Value *One = llvm::ConstantInt::get(LPtr->getType(), 1); |
| 619 | |
| 620 | // Compute (l.adj | r.adj) & 1 and test it against zero. |
| 621 | llvm::Value *OrAdj = Builder.CreateOr(LAdj, RAdj, "or.adj"); |
| 622 | llvm::Value *OrAdjAnd1 = Builder.CreateAnd(OrAdj, One); |
| 623 | llvm::Value *OrAdjAnd1EqZero = Builder.CreateICmp(Eq, OrAdjAnd1, Zero, |
| 624 | "cmp.or.adj"); |
| 625 | EqZero = Builder.CreateBinOp(And, EqZero, OrAdjAnd1EqZero); |
| 626 | } |
| 627 | |
| 628 | // Tie together all our conditions. |
| 629 | llvm::Value *Result = Builder.CreateBinOp(Or, EqZero, AdjEq); |
| 630 | Result = Builder.CreateBinOp(And, PtrEq, Result, |
| 631 | Inequality ? "memptr.ne" : "memptr.eq"); |
| 632 | return Result; |
| 633 | } |
| 634 | |
| 635 | llvm::Value * |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 636 | ItaniumCXXABI::EmitMemberPointerIsNotNull(CodeGenFunction &CGF, |
| 637 | llvm::Value *MemPtr, |
| 638 | const MemberPointerType *MPT) { |
John McCall | e9fd7eb | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 639 | CGBuilderTy &Builder = CGF.Builder; |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 640 | |
| 641 | /// For member data pointers, this is just a check against -1. |
| 642 | if (MPT->isMemberDataPointer()) { |
| 643 | assert(MemPtr->getType() == getPtrDiffTy()); |
| 644 | llvm::Value *NegativeOne = |
| 645 | llvm::Constant::getAllOnesValue(MemPtr->getType()); |
| 646 | return Builder.CreateICmpNE(MemPtr, NegativeOne, "memptr.tobool"); |
| 647 | } |
John McCall | e9fd7eb | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 648 | |
Daniel Dunbar | db27b5f | 2011-04-19 23:10:47 +0000 | [diff] [blame] | 649 | // In Itanium, a member function pointer is not null if 'ptr' is not null. |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 650 | llvm::Value *Ptr = Builder.CreateExtractValue(MemPtr, 0, "memptr.ptr"); |
John McCall | e9fd7eb | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 651 | |
| 652 | llvm::Constant *Zero = llvm::ConstantInt::get(Ptr->getType(), 0); |
| 653 | llvm::Value *Result = Builder.CreateICmpNE(Ptr, Zero, "memptr.tobool"); |
| 654 | |
Daniel Dunbar | db27b5f | 2011-04-19 23:10:47 +0000 | [diff] [blame] | 655 | // On ARM, a member function pointer is also non-null if the low bit of 'adj' |
| 656 | // (the virtual bit) is set. |
John McCall | e9fd7eb | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 657 | if (IsARM) { |
| 658 | llvm::Constant *One = llvm::ConstantInt::get(Ptr->getType(), 1); |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 659 | llvm::Value *Adj = Builder.CreateExtractValue(MemPtr, 1, "memptr.adj"); |
John McCall | e9fd7eb | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 660 | llvm::Value *VirtualBit = Builder.CreateAnd(Adj, One, "memptr.virtualbit"); |
Daniel Dunbar | db27b5f | 2011-04-19 23:10:47 +0000 | [diff] [blame] | 661 | llvm::Value *IsVirtual = Builder.CreateICmpNE(VirtualBit, Zero, |
| 662 | "memptr.isvirtual"); |
| 663 | Result = Builder.CreateOr(Result, IsVirtual); |
John McCall | e9fd7eb | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | return Result; |
| 667 | } |
John McCall | 875ab10 | 2010-08-22 06:43:33 +0000 | [diff] [blame] | 668 | |
John McCall | f16aa10 | 2010-08-22 21:01:12 +0000 | [diff] [blame] | 669 | /// The Itanium ABI requires non-zero initialization only for data |
| 670 | /// member pointers, for which '0' is a valid offset. |
| 671 | bool ItaniumCXXABI::isZeroInitializable(const MemberPointerType *MPT) { |
| 672 | return MPT->getPointeeType()->isFunctionType(); |
John McCall | cf2c85e | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 673 | } |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 674 | |
| 675 | /// The generic ABI passes 'this', plus a VTT if it's initializing a |
| 676 | /// base subobject. |
| 677 | void ItaniumCXXABI::BuildConstructorSignature(const CXXConstructorDecl *Ctor, |
| 678 | CXXCtorType Type, |
| 679 | CanQualType &ResTy, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 680 | SmallVectorImpl<CanQualType> &ArgTys) { |
John McCall | 9cb2cee | 2010-09-02 10:25:57 +0000 | [diff] [blame] | 681 | ASTContext &Context = getContext(); |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 682 | |
| 683 | // 'this' is already there. |
| 684 | |
| 685 | // Check if we need to add a VTT parameter (which has type void **). |
| 686 | if (Type == Ctor_Base && Ctor->getParent()->getNumVBases() != 0) |
| 687 | ArgTys.push_back(Context.getPointerType(Context.VoidPtrTy)); |
| 688 | } |
| 689 | |
| 690 | /// The ARM ABI does the same as the Itanium ABI, but returns 'this'. |
| 691 | void ARMCXXABI::BuildConstructorSignature(const CXXConstructorDecl *Ctor, |
| 692 | CXXCtorType Type, |
| 693 | CanQualType &ResTy, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 694 | SmallVectorImpl<CanQualType> &ArgTys) { |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 695 | ItaniumCXXABI::BuildConstructorSignature(Ctor, Type, ResTy, ArgTys); |
| 696 | ResTy = ArgTys[0]; |
| 697 | } |
| 698 | |
| 699 | /// The generic ABI passes 'this', plus a VTT if it's destroying a |
| 700 | /// base subobject. |
| 701 | void ItaniumCXXABI::BuildDestructorSignature(const CXXDestructorDecl *Dtor, |
| 702 | CXXDtorType Type, |
| 703 | CanQualType &ResTy, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 704 | SmallVectorImpl<CanQualType> &ArgTys) { |
John McCall | 9cb2cee | 2010-09-02 10:25:57 +0000 | [diff] [blame] | 705 | ASTContext &Context = getContext(); |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 706 | |
| 707 | // 'this' is already there. |
| 708 | |
| 709 | // Check if we need to add a VTT parameter (which has type void **). |
| 710 | if (Type == Dtor_Base && Dtor->getParent()->getNumVBases() != 0) |
| 711 | ArgTys.push_back(Context.getPointerType(Context.VoidPtrTy)); |
| 712 | } |
| 713 | |
| 714 | /// The ARM ABI does the same as the Itanium ABI, but returns 'this' |
| 715 | /// for non-deleting destructors. |
| 716 | void ARMCXXABI::BuildDestructorSignature(const CXXDestructorDecl *Dtor, |
| 717 | CXXDtorType Type, |
| 718 | CanQualType &ResTy, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 719 | SmallVectorImpl<CanQualType> &ArgTys) { |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 720 | ItaniumCXXABI::BuildDestructorSignature(Dtor, Type, ResTy, ArgTys); |
| 721 | |
| 722 | if (Type != Dtor_Deleting) |
| 723 | ResTy = ArgTys[0]; |
| 724 | } |
| 725 | |
| 726 | void ItaniumCXXABI::BuildInstanceFunctionParams(CodeGenFunction &CGF, |
| 727 | QualType &ResTy, |
| 728 | FunctionArgList &Params) { |
| 729 | /// Create the 'this' variable. |
| 730 | BuildThisParam(CGF, Params); |
| 731 | |
| 732 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(CGF.CurGD.getDecl()); |
| 733 | assert(MD->isInstance()); |
| 734 | |
| 735 | // Check if we need a VTT parameter as well. |
| 736 | if (CodeGenVTables::needsVTTParameter(CGF.CurGD)) { |
John McCall | 9cb2cee | 2010-09-02 10:25:57 +0000 | [diff] [blame] | 737 | ASTContext &Context = getContext(); |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 738 | |
| 739 | // FIXME: avoid the fake decl |
| 740 | QualType T = Context.getPointerType(Context.VoidPtrTy); |
| 741 | ImplicitParamDecl *VTTDecl |
| 742 | = ImplicitParamDecl::Create(Context, 0, MD->getLocation(), |
| 743 | &Context.Idents.get("vtt"), T); |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 744 | Params.push_back(VTTDecl); |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 745 | getVTTDecl(CGF) = VTTDecl; |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | void ARMCXXABI::BuildInstanceFunctionParams(CodeGenFunction &CGF, |
| 750 | QualType &ResTy, |
| 751 | FunctionArgList &Params) { |
| 752 | ItaniumCXXABI::BuildInstanceFunctionParams(CGF, ResTy, Params); |
| 753 | |
| 754 | // Return 'this' from certain constructors and destructors. |
| 755 | if (HasThisReturn(CGF.CurGD)) |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 756 | ResTy = Params[0]->getType(); |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | void ItaniumCXXABI::EmitInstanceFunctionProlog(CodeGenFunction &CGF) { |
| 760 | /// Initialize the 'this' slot. |
| 761 | EmitThisParam(CGF); |
| 762 | |
| 763 | /// Initialize the 'vtt' slot if needed. |
| 764 | if (getVTTDecl(CGF)) { |
| 765 | getVTTValue(CGF) |
| 766 | = CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(getVTTDecl(CGF)), |
| 767 | "vtt"); |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | void ARMCXXABI::EmitInstanceFunctionProlog(CodeGenFunction &CGF) { |
| 772 | ItaniumCXXABI::EmitInstanceFunctionProlog(CGF); |
| 773 | |
| 774 | /// Initialize the return slot to 'this' at the start of the |
| 775 | /// function. |
| 776 | if (HasThisReturn(CGF.CurGD)) |
Eli Friedman | cec5ebd | 2012-02-11 02:57:39 +0000 | [diff] [blame] | 777 | CGF.Builder.CreateStore(getThisValue(CGF), CGF.ReturnValue); |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | void ARMCXXABI::EmitReturnFromThunk(CodeGenFunction &CGF, |
| 781 | RValue RV, QualType ResultType) { |
| 782 | if (!isa<CXXDestructorDecl>(CGF.CurGD.getDecl())) |
| 783 | return ItaniumCXXABI::EmitReturnFromThunk(CGF, RV, ResultType); |
| 784 | |
| 785 | // Destructor thunks in the ARM ABI have indeterminate results. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 786 | llvm::Type *T = |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 787 | cast<llvm::PointerType>(CGF.ReturnValue->getType())->getElementType(); |
| 788 | RValue Undef = RValue::get(llvm::UndefValue::get(T)); |
| 789 | return ItaniumCXXABI::EmitReturnFromThunk(CGF, Undef, ResultType); |
| 790 | } |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 791 | |
| 792 | /************************** Array allocation cookies **************************/ |
| 793 | |
John McCall | e2b45e2 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 794 | CharUnits ItaniumCXXABI::getArrayCookieSizeImpl(QualType elementType) { |
| 795 | // The array cookie is a size_t; pad that up to the element alignment. |
| 796 | // The cookie is actually right-justified in that space. |
| 797 | return std::max(CharUnits::fromQuantity(CGM.SizeSizeInBytes), |
| 798 | CGM.getContext().getTypeAlignInChars(elementType)); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | llvm::Value *ItaniumCXXABI::InitializeArrayCookie(CodeGenFunction &CGF, |
| 802 | llvm::Value *NewPtr, |
| 803 | llvm::Value *NumElements, |
John McCall | 6ec278d | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 804 | const CXXNewExpr *expr, |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 805 | QualType ElementType) { |
John McCall | e2b45e2 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 806 | assert(requiresArrayCookie(expr)); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 807 | |
| 808 | unsigned AS = cast<llvm::PointerType>(NewPtr->getType())->getAddressSpace(); |
| 809 | |
John McCall | 9cb2cee | 2010-09-02 10:25:57 +0000 | [diff] [blame] | 810 | ASTContext &Ctx = getContext(); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 811 | QualType SizeTy = Ctx.getSizeType(); |
| 812 | CharUnits SizeSize = Ctx.getTypeSizeInChars(SizeTy); |
| 813 | |
| 814 | // The size of the cookie. |
| 815 | CharUnits CookieSize = |
| 816 | std::max(SizeSize, Ctx.getTypeAlignInChars(ElementType)); |
John McCall | e2b45e2 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 817 | assert(CookieSize == getArrayCookieSizeImpl(ElementType)); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 818 | |
| 819 | // Compute an offset to the cookie. |
| 820 | llvm::Value *CookiePtr = NewPtr; |
| 821 | CharUnits CookieOffset = CookieSize - SizeSize; |
| 822 | if (!CookieOffset.isZero()) |
| 823 | CookiePtr = CGF.Builder.CreateConstInBoundsGEP1_64(CookiePtr, |
| 824 | CookieOffset.getQuantity()); |
| 825 | |
| 826 | // Write the number of elements into the appropriate slot. |
| 827 | llvm::Value *NumElementsPtr |
| 828 | = CGF.Builder.CreateBitCast(CookiePtr, |
| 829 | CGF.ConvertType(SizeTy)->getPointerTo(AS)); |
| 830 | CGF.Builder.CreateStore(NumElements, NumElementsPtr); |
| 831 | |
| 832 | // Finally, compute a pointer to the actual data buffer by skipping |
| 833 | // over the cookie completely. |
| 834 | return CGF.Builder.CreateConstInBoundsGEP1_64(NewPtr, |
| 835 | CookieSize.getQuantity()); |
| 836 | } |
| 837 | |
John McCall | e2b45e2 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 838 | llvm::Value *ItaniumCXXABI::readArrayCookieImpl(CodeGenFunction &CGF, |
| 839 | llvm::Value *allocPtr, |
| 840 | CharUnits cookieSize) { |
| 841 | // The element size is right-justified in the cookie. |
| 842 | llvm::Value *numElementsPtr = allocPtr; |
| 843 | CharUnits numElementsOffset = |
| 844 | cookieSize - CharUnits::fromQuantity(CGF.SizeSizeInBytes); |
| 845 | if (!numElementsOffset.isZero()) |
| 846 | numElementsPtr = |
| 847 | CGF.Builder.CreateConstInBoundsGEP1_64(numElementsPtr, |
| 848 | numElementsOffset.getQuantity()); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 849 | |
John McCall | e2b45e2 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 850 | unsigned AS = cast<llvm::PointerType>(allocPtr->getType())->getAddressSpace(); |
| 851 | numElementsPtr = |
| 852 | CGF.Builder.CreateBitCast(numElementsPtr, CGF.SizeTy->getPointerTo(AS)); |
| 853 | return CGF.Builder.CreateLoad(numElementsPtr); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 854 | } |
| 855 | |
John McCall | e2b45e2 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 856 | CharUnits ARMCXXABI::getArrayCookieSizeImpl(QualType elementType) { |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 857 | // On ARM, the cookie is always: |
| 858 | // struct array_cookie { |
| 859 | // std::size_t element_size; // element_size != 0 |
| 860 | // std::size_t element_count; |
| 861 | // }; |
| 862 | // TODO: what should we do if the allocated type actually wants |
| 863 | // greater alignment? |
John McCall | e2b45e2 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 864 | return CharUnits::fromQuantity(2 * CGM.SizeSizeInBytes); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 865 | } |
| 866 | |
| 867 | llvm::Value *ARMCXXABI::InitializeArrayCookie(CodeGenFunction &CGF, |
| 868 | llvm::Value *NewPtr, |
| 869 | llvm::Value *NumElements, |
John McCall | 6ec278d | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 870 | const CXXNewExpr *expr, |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 871 | QualType ElementType) { |
John McCall | e2b45e2 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 872 | assert(requiresArrayCookie(expr)); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 873 | |
| 874 | // NewPtr is a char*. |
| 875 | |
| 876 | unsigned AS = cast<llvm::PointerType>(NewPtr->getType())->getAddressSpace(); |
| 877 | |
John McCall | 9cb2cee | 2010-09-02 10:25:57 +0000 | [diff] [blame] | 878 | ASTContext &Ctx = getContext(); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 879 | CharUnits SizeSize = Ctx.getTypeSizeInChars(Ctx.getSizeType()); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 880 | llvm::IntegerType *SizeTy = |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 881 | cast<llvm::IntegerType>(CGF.ConvertType(Ctx.getSizeType())); |
| 882 | |
| 883 | // The cookie is always at the start of the buffer. |
| 884 | llvm::Value *CookiePtr = NewPtr; |
| 885 | |
| 886 | // The first element is the element size. |
| 887 | CookiePtr = CGF.Builder.CreateBitCast(CookiePtr, SizeTy->getPointerTo(AS)); |
| 888 | llvm::Value *ElementSize = llvm::ConstantInt::get(SizeTy, |
| 889 | Ctx.getTypeSizeInChars(ElementType).getQuantity()); |
| 890 | CGF.Builder.CreateStore(ElementSize, CookiePtr); |
| 891 | |
| 892 | // The second element is the element count. |
| 893 | CookiePtr = CGF.Builder.CreateConstInBoundsGEP1_32(CookiePtr, 1); |
| 894 | CGF.Builder.CreateStore(NumElements, CookiePtr); |
| 895 | |
| 896 | // Finally, compute a pointer to the actual data buffer by skipping |
| 897 | // over the cookie completely. |
| 898 | CharUnits CookieSize = 2 * SizeSize; |
| 899 | return CGF.Builder.CreateConstInBoundsGEP1_64(NewPtr, |
| 900 | CookieSize.getQuantity()); |
| 901 | } |
| 902 | |
John McCall | e2b45e2 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 903 | llvm::Value *ARMCXXABI::readArrayCookieImpl(CodeGenFunction &CGF, |
| 904 | llvm::Value *allocPtr, |
| 905 | CharUnits cookieSize) { |
| 906 | // The number of elements is at offset sizeof(size_t) relative to |
| 907 | // the allocated pointer. |
| 908 | llvm::Value *numElementsPtr |
| 909 | = CGF.Builder.CreateConstInBoundsGEP1_64(allocPtr, CGF.SizeSizeInBytes); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 910 | |
John McCall | e2b45e2 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 911 | unsigned AS = cast<llvm::PointerType>(allocPtr->getType())->getAddressSpace(); |
| 912 | numElementsPtr = |
| 913 | CGF.Builder.CreateBitCast(numElementsPtr, CGF.SizeTy->getPointerTo(AS)); |
| 914 | return CGF.Builder.CreateLoad(numElementsPtr); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 915 | } |
| 916 | |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 917 | /*********************** Static local initialization **************************/ |
| 918 | |
| 919 | static llvm::Constant *getGuardAcquireFn(CodeGenModule &CGM, |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 920 | llvm::PointerType *GuardPtrTy) { |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 921 | // int __cxa_guard_acquire(__guard *guard_object); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 922 | llvm::FunctionType *FTy = |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 923 | llvm::FunctionType::get(CGM.getTypes().ConvertType(CGM.getContext().IntTy), |
Jay Foad | da549e8 | 2011-07-29 13:56:53 +0000 | [diff] [blame] | 924 | GuardPtrTy, /*isVarArg=*/false); |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 925 | |
Nick Lewycky | e76872e | 2012-02-13 23:45:02 +0000 | [diff] [blame] | 926 | return CGM.CreateRuntimeFunction(FTy, "__cxa_guard_acquire", |
| 927 | llvm::Attribute::NoUnwind); |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 928 | } |
| 929 | |
| 930 | static llvm::Constant *getGuardReleaseFn(CodeGenModule &CGM, |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 931 | llvm::PointerType *GuardPtrTy) { |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 932 | // void __cxa_guard_release(__guard *guard_object); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 933 | llvm::FunctionType *FTy = |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 934 | llvm::FunctionType::get(CGM.VoidTy, GuardPtrTy, /*isVarArg=*/false); |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 935 | |
Nick Lewycky | e76872e | 2012-02-13 23:45:02 +0000 | [diff] [blame] | 936 | return CGM.CreateRuntimeFunction(FTy, "__cxa_guard_release", |
| 937 | llvm::Attribute::NoUnwind); |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 938 | } |
| 939 | |
| 940 | static llvm::Constant *getGuardAbortFn(CodeGenModule &CGM, |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 941 | llvm::PointerType *GuardPtrTy) { |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 942 | // void __cxa_guard_abort(__guard *guard_object); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 943 | llvm::FunctionType *FTy = |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 944 | llvm::FunctionType::get(CGM.VoidTy, GuardPtrTy, /*isVarArg=*/false); |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 945 | |
Nick Lewycky | e76872e | 2012-02-13 23:45:02 +0000 | [diff] [blame] | 946 | return CGM.CreateRuntimeFunction(FTy, "__cxa_guard_abort", |
| 947 | llvm::Attribute::NoUnwind); |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | namespace { |
| 951 | struct CallGuardAbort : EHScopeStack::Cleanup { |
| 952 | llvm::GlobalVariable *Guard; |
Chandler Carruth | 0f30a12 | 2012-03-30 19:44:53 +0000 | [diff] [blame] | 953 | CallGuardAbort(llvm::GlobalVariable *Guard) : Guard(Guard) {} |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 954 | |
John McCall | ad346f4 | 2011-07-12 20:27:29 +0000 | [diff] [blame] | 955 | void Emit(CodeGenFunction &CGF, Flags flags) { |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 956 | CGF.Builder.CreateCall(getGuardAbortFn(CGF.CGM, Guard->getType()), Guard) |
| 957 | ->setDoesNotThrow(); |
| 958 | } |
| 959 | }; |
| 960 | } |
| 961 | |
| 962 | /// The ARM code here follows the Itanium code closely enough that we |
| 963 | /// just special-case it at particular places. |
John McCall | 3030eb8 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 964 | void ItaniumCXXABI::EmitGuardedInit(CodeGenFunction &CGF, |
| 965 | const VarDecl &D, |
John McCall | 355bba7 | 2012-03-30 21:00:39 +0000 | [diff] [blame] | 966 | llvm::GlobalVariable *var, |
| 967 | bool shouldPerformInit) { |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 968 | CGBuilderTy &Builder = CGF.Builder; |
John McCall | 3030eb8 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 969 | |
| 970 | // We only need to use thread-safe statics for local variables; |
| 971 | // global initialization is always single-threaded. |
John McCall | 0502a22 | 2011-06-17 07:33:57 +0000 | [diff] [blame] | 972 | bool threadsafe = |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 973 | (getContext().getLangOpts().ThreadsafeStatics && D.isLocalVarDecl()); |
Anders Carlsson | 173d512 | 2011-04-27 04:37:08 +0000 | [diff] [blame] | 974 | |
Anders Carlsson | 173d512 | 2011-04-27 04:37:08 +0000 | [diff] [blame] | 975 | // If we have a global variable with internal linkage and thread-safe statics |
| 976 | // are disabled, we can just let the guard variable be of type i8. |
John McCall | 355bba7 | 2012-03-30 21:00:39 +0000 | [diff] [blame] | 977 | bool useInt8GuardVariable = !threadsafe && var->hasInternalLinkage(); |
| 978 | |
| 979 | llvm::IntegerType *guardTy; |
John McCall | 0502a22 | 2011-06-17 07:33:57 +0000 | [diff] [blame] | 980 | if (useInt8GuardVariable) { |
John McCall | 355bba7 | 2012-03-30 21:00:39 +0000 | [diff] [blame] | 981 | guardTy = CGF.Int8Ty; |
John McCall | 0502a22 | 2011-06-17 07:33:57 +0000 | [diff] [blame] | 982 | } else { |
Anders Carlsson | 173d512 | 2011-04-27 04:37:08 +0000 | [diff] [blame] | 983 | // Guard variables are 64 bits in the generic ABI and 32 bits on ARM. |
John McCall | 355bba7 | 2012-03-30 21:00:39 +0000 | [diff] [blame] | 984 | guardTy = (IsARM ? CGF.Int32Ty : CGF.Int64Ty); |
Anders Carlsson | 173d512 | 2011-04-27 04:37:08 +0000 | [diff] [blame] | 985 | } |
John McCall | 355bba7 | 2012-03-30 21:00:39 +0000 | [diff] [blame] | 986 | llvm::PointerType *guardPtrTy = guardTy->getPointerTo(); |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 987 | |
John McCall | 355bba7 | 2012-03-30 21:00:39 +0000 | [diff] [blame] | 988 | // Create the guard variable if we don't already have it (as we |
| 989 | // might if we're double-emitting this function body). |
| 990 | llvm::GlobalVariable *guard = CGM.getStaticLocalDeclGuardAddress(&D); |
| 991 | if (!guard) { |
| 992 | // Mangle the name for the guard. |
| 993 | SmallString<256> guardName; |
| 994 | { |
| 995 | llvm::raw_svector_ostream out(guardName); |
| 996 | getMangleContext().mangleItaniumGuardVariable(&D, out); |
| 997 | out.flush(); |
| 998 | } |
John McCall | 112c967 | 2010-11-02 21:04:24 +0000 | [diff] [blame] | 999 | |
John McCall | 355bba7 | 2012-03-30 21:00:39 +0000 | [diff] [blame] | 1000 | // Create the guard variable with a zero-initializer. |
| 1001 | // Just absorb linkage and visibility from the guarded variable. |
| 1002 | guard = new llvm::GlobalVariable(CGM.getModule(), guardTy, |
| 1003 | false, var->getLinkage(), |
| 1004 | llvm::ConstantInt::get(guardTy, 0), |
| 1005 | guardName.str()); |
| 1006 | guard->setVisibility(var->getVisibility()); |
| 1007 | |
| 1008 | CGM.setStaticLocalDeclGuardAddress(&D, guard); |
| 1009 | } |
John McCall | 49d26d2 | 2012-03-30 07:09:50 +0000 | [diff] [blame] | 1010 | |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1011 | // Test whether the variable has completed initialization. |
John McCall | 355bba7 | 2012-03-30 21:00:39 +0000 | [diff] [blame] | 1012 | llvm::Value *isInitialized; |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1013 | |
| 1014 | // ARM C++ ABI 3.2.3.1: |
| 1015 | // To support the potential use of initialization guard variables |
| 1016 | // as semaphores that are the target of ARM SWP and LDREX/STREX |
| 1017 | // synchronizing instructions we define a static initialization |
| 1018 | // guard variable to be a 4-byte aligned, 4- byte word with the |
| 1019 | // following inline access protocol. |
| 1020 | // #define INITIALIZED 1 |
| 1021 | // if ((obj_guard & INITIALIZED) != INITIALIZED) { |
| 1022 | // if (__cxa_guard_acquire(&obj_guard)) |
| 1023 | // ... |
| 1024 | // } |
John McCall | 0502a22 | 2011-06-17 07:33:57 +0000 | [diff] [blame] | 1025 | if (IsARM && !useInt8GuardVariable) { |
John McCall | 355bba7 | 2012-03-30 21:00:39 +0000 | [diff] [blame] | 1026 | llvm::Value *V = Builder.CreateLoad(guard); |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1027 | V = Builder.CreateAnd(V, Builder.getInt32(1)); |
John McCall | 355bba7 | 2012-03-30 21:00:39 +0000 | [diff] [blame] | 1028 | isInitialized = Builder.CreateIsNull(V, "guard.uninitialized"); |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1029 | |
| 1030 | // Itanium C++ ABI 3.3.2: |
| 1031 | // The following is pseudo-code showing how these functions can be used: |
| 1032 | // if (obj_guard.first_byte == 0) { |
| 1033 | // if ( __cxa_guard_acquire (&obj_guard) ) { |
| 1034 | // try { |
| 1035 | // ... initialize the object ...; |
| 1036 | // } catch (...) { |
| 1037 | // __cxa_guard_abort (&obj_guard); |
| 1038 | // throw; |
| 1039 | // } |
| 1040 | // ... queue object destructor with __cxa_atexit() ...; |
| 1041 | // __cxa_guard_release (&obj_guard); |
| 1042 | // } |
| 1043 | // } |
| 1044 | } else { |
| 1045 | // Load the first byte of the guard variable. |
Chandler Carruth | 0f30a12 | 2012-03-30 19:44:53 +0000 | [diff] [blame] | 1046 | llvm::LoadInst *LI = |
John McCall | 355bba7 | 2012-03-30 21:00:39 +0000 | [diff] [blame] | 1047 | Builder.CreateLoad(Builder.CreateBitCast(guard, CGM.Int8PtrTy)); |
Chandler Carruth | 0f30a12 | 2012-03-30 19:44:53 +0000 | [diff] [blame] | 1048 | LI->setAlignment(1); |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1049 | |
Eli Friedman | eb43f4a | 2011-09-13 22:21:56 +0000 | [diff] [blame] | 1050 | // Itanium ABI: |
| 1051 | // An implementation supporting thread-safety on multiprocessor |
| 1052 | // systems must also guarantee that references to the initialized |
| 1053 | // object do not occur before the load of the initialization flag. |
| 1054 | // |
| 1055 | // In LLVM, we do this by marking the load Acquire. |
| 1056 | if (threadsafe) |
Chandler Carruth | 0f30a12 | 2012-03-30 19:44:53 +0000 | [diff] [blame] | 1057 | LI->setAtomic(llvm::Acquire); |
Eli Friedman | eb43f4a | 2011-09-13 22:21:56 +0000 | [diff] [blame] | 1058 | |
John McCall | 355bba7 | 2012-03-30 21:00:39 +0000 | [diff] [blame] | 1059 | isInitialized = Builder.CreateIsNull(LI, "guard.uninitialized"); |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1060 | } |
| 1061 | |
| 1062 | llvm::BasicBlock *InitCheckBlock = CGF.createBasicBlock("init.check"); |
| 1063 | llvm::BasicBlock *EndBlock = CGF.createBasicBlock("init.end"); |
| 1064 | |
| 1065 | // Check if the first byte of the guard variable is zero. |
John McCall | 355bba7 | 2012-03-30 21:00:39 +0000 | [diff] [blame] | 1066 | Builder.CreateCondBr(isInitialized, InitCheckBlock, EndBlock); |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1067 | |
| 1068 | CGF.EmitBlock(InitCheckBlock); |
| 1069 | |
| 1070 | // Variables used when coping with thread-safe statics and exceptions. |
John McCall | 0502a22 | 2011-06-17 07:33:57 +0000 | [diff] [blame] | 1071 | if (threadsafe) { |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1072 | // Call __cxa_guard_acquire. |
| 1073 | llvm::Value *V |
John McCall | 355bba7 | 2012-03-30 21:00:39 +0000 | [diff] [blame] | 1074 | = Builder.CreateCall(getGuardAcquireFn(CGM, guardPtrTy), guard); |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1075 | |
| 1076 | llvm::BasicBlock *InitBlock = CGF.createBasicBlock("init"); |
| 1077 | |
| 1078 | Builder.CreateCondBr(Builder.CreateIsNotNull(V, "tobool"), |
| 1079 | InitBlock, EndBlock); |
| 1080 | |
| 1081 | // Call __cxa_guard_abort along the exceptional edge. |
John McCall | 355bba7 | 2012-03-30 21:00:39 +0000 | [diff] [blame] | 1082 | CGF.EHStack.pushCleanup<CallGuardAbort>(EHCleanup, guard); |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1083 | |
| 1084 | CGF.EmitBlock(InitBlock); |
| 1085 | } |
| 1086 | |
| 1087 | // Emit the initializer and add a global destructor if appropriate. |
John McCall | 355bba7 | 2012-03-30 21:00:39 +0000 | [diff] [blame] | 1088 | CGF.EmitCXXGlobalVarDeclInit(D, var, shouldPerformInit); |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1089 | |
John McCall | 0502a22 | 2011-06-17 07:33:57 +0000 | [diff] [blame] | 1090 | if (threadsafe) { |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1091 | // Pop the guard-abort cleanup if we pushed one. |
| 1092 | CGF.PopCleanupBlock(); |
| 1093 | |
| 1094 | // Call __cxa_guard_release. This cannot throw. |
John McCall | 355bba7 | 2012-03-30 21:00:39 +0000 | [diff] [blame] | 1095 | Builder.CreateCall(getGuardReleaseFn(CGM, guardPtrTy), guard); |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1096 | } else { |
John McCall | 355bba7 | 2012-03-30 21:00:39 +0000 | [diff] [blame] | 1097 | Builder.CreateStore(llvm::ConstantInt::get(guardTy, 1), guard); |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1098 | } |
| 1099 | |
| 1100 | CGF.EmitBlock(EndBlock); |
| 1101 | } |
John McCall | 20bb175 | 2012-05-01 06:13:13 +0000 | [diff] [blame] | 1102 | |
| 1103 | /// Register a global destructor using __cxa_atexit. |
| 1104 | static void emitGlobalDtorWithCXAAtExit(CodeGenFunction &CGF, |
| 1105 | llvm::Constant *dtor, |
| 1106 | llvm::Constant *addr) { |
| 1107 | // We're assuming that the destructor function is something we can |
| 1108 | // reasonably call with the default CC. Go ahead and cast it to the |
| 1109 | // right prototype. |
| 1110 | llvm::Type *dtorTy = |
| 1111 | llvm::FunctionType::get(CGF.VoidTy, CGF.Int8PtrTy, false)->getPointerTo(); |
| 1112 | |
| 1113 | // extern "C" int __cxa_atexit(void (*f)(void *), void *p, void *d); |
| 1114 | llvm::Type *paramTys[] = { dtorTy, CGF.Int8PtrTy, CGF.Int8PtrTy }; |
| 1115 | llvm::FunctionType *atexitTy = |
| 1116 | llvm::FunctionType::get(CGF.IntTy, paramTys, false); |
| 1117 | |
| 1118 | // Fetch the actual function. |
| 1119 | llvm::Constant *atexit = |
| 1120 | CGF.CGM.CreateRuntimeFunction(atexitTy, "__cxa_atexit"); |
| 1121 | if (llvm::Function *fn = dyn_cast<llvm::Function>(atexit)) |
| 1122 | fn->setDoesNotThrow(); |
| 1123 | |
| 1124 | // Create a variable that binds the atexit to this shared object. |
| 1125 | llvm::Constant *handle = |
| 1126 | CGF.CGM.CreateRuntimeVariable(CGF.Int8Ty, "__dso_handle"); |
| 1127 | |
| 1128 | llvm::Value *args[] = { |
| 1129 | llvm::ConstantExpr::getBitCast(dtor, dtorTy), |
| 1130 | llvm::ConstantExpr::getBitCast(addr, CGF.Int8PtrTy), |
| 1131 | handle |
| 1132 | }; |
| 1133 | CGF.Builder.CreateCall(atexit, args)->setDoesNotThrow(); |
| 1134 | } |
| 1135 | |
| 1136 | /// Register a global destructor as best as we know how. |
| 1137 | void ItaniumCXXABI::registerGlobalDtor(CodeGenFunction &CGF, |
| 1138 | llvm::Constant *dtor, |
| 1139 | llvm::Constant *addr) { |
| 1140 | // Use __cxa_atexit if available. |
| 1141 | if (CGM.getCodeGenOpts().CXAAtExit) { |
| 1142 | return emitGlobalDtorWithCXAAtExit(CGF, dtor, addr); |
| 1143 | } |
| 1144 | |
| 1145 | // In Apple kexts, we want to add a global destructor entry. |
| 1146 | // FIXME: shouldn't this be guarded by some variable? |
| 1147 | if (CGM.getContext().getLangOpts().AppleKext) { |
| 1148 | // Generate a global destructor entry. |
| 1149 | return CGM.AddCXXDtorEntry(dtor, addr); |
| 1150 | } |
| 1151 | |
| 1152 | CGF.registerGlobalDtorWithAtExit(dtor, addr); |
| 1153 | } |