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