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