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