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