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