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