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 | // |
| 10 | // This provides C++ code generation targetting the Itanium C++ ABI. The class |
| 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" |
| 25 | #include "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: |
Charles Davis | 3a811f1 | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 38 | CodeGen::MangleContext MangleCtx; |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 39 | bool IsARM; |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 40 | |
| 41 | // It's a little silly for us to cache this. |
| 42 | const llvm::IntegerType *getPtrDiffTy() { |
| 43 | if (!PtrDiffTy) { |
John McCall | 9cb2cee | 2010-09-02 10:25:57 +0000 | [diff] [blame] | 44 | QualType T = getContext().getPointerDiffType(); |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 45 | const llvm::Type *Ty = CGM.getTypes().ConvertTypeRecursive(T); |
| 46 | PtrDiffTy = cast<llvm::IntegerType>(Ty); |
| 47 | } |
| 48 | return PtrDiffTy; |
| 49 | } |
| 50 | |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 51 | bool NeedsArrayCookie(QualType ElementType); |
| 52 | |
Charles Davis | 3a811f1 | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 53 | public: |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 54 | ItaniumCXXABI(CodeGen::CodeGenModule &CGM, bool IsARM = false) : |
John McCall | 9cb2cee | 2010-09-02 10:25:57 +0000 | [diff] [blame] | 55 | CGCXXABI(CGM), PtrDiffTy(0), MangleCtx(getContext(), CGM.getDiags()), |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 56 | IsARM(IsARM) { } |
Charles Davis | 3a811f1 | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 57 | |
| 58 | CodeGen::MangleContext &getMangleContext() { |
| 59 | return MangleCtx; |
| 60 | } |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 61 | |
John McCall | f16aa10 | 2010-08-22 21:01:12 +0000 | [diff] [blame] | 62 | bool isZeroInitializable(const MemberPointerType *MPT); |
John McCall | cf2c85e | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 63 | |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 64 | const llvm::Type *ConvertMemberPointerType(const MemberPointerType *MPT); |
| 65 | |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 66 | llvm::Value *EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF, |
| 67 | llvm::Value *&This, |
| 68 | llvm::Value *MemFnPtr, |
| 69 | const MemberPointerType *MPT); |
John McCall | 3023def | 2010-08-22 03:04:22 +0000 | [diff] [blame] | 70 | |
John McCall | 6c2ab1d | 2010-08-31 21:07:20 +0000 | [diff] [blame] | 71 | llvm::Value *EmitMemberDataPointerAddress(CodeGenFunction &CGF, |
| 72 | llvm::Value *Base, |
| 73 | llvm::Value *MemPtr, |
| 74 | const MemberPointerType *MPT); |
| 75 | |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 76 | llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF, |
| 77 | const CastExpr *E, |
| 78 | llvm::Value *Src); |
John McCall | cf2c85e | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 79 | |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 80 | llvm::Constant *EmitMemberPointerConversion(llvm::Constant *C, |
| 81 | const CastExpr *E); |
John McCall | cf2c85e | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 82 | |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 83 | llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT); |
John McCall | cf2c85e | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 84 | |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 85 | llvm::Constant *EmitMemberPointer(const CXXMethodDecl *MD); |
| 86 | llvm::Constant *EmitMemberPointer(const FieldDecl *FD); |
John McCall | 875ab10 | 2010-08-22 06:43:33 +0000 | [diff] [blame] | 87 | |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 88 | llvm::Value *EmitMemberPointerComparison(CodeGenFunction &CGF, |
| 89 | llvm::Value *L, |
| 90 | llvm::Value *R, |
| 91 | const MemberPointerType *MPT, |
| 92 | bool Inequality); |
John McCall | e9fd7eb | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 93 | |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 94 | llvm::Value *EmitMemberPointerIsNotNull(CodeGenFunction &CGF, |
| 95 | llvm::Value *Addr, |
| 96 | const MemberPointerType *MPT); |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 97 | |
| 98 | void BuildConstructorSignature(const CXXConstructorDecl *Ctor, |
| 99 | CXXCtorType T, |
| 100 | CanQualType &ResTy, |
| 101 | llvm::SmallVectorImpl<CanQualType> &ArgTys); |
| 102 | |
| 103 | void BuildDestructorSignature(const CXXDestructorDecl *Dtor, |
| 104 | CXXDtorType T, |
| 105 | CanQualType &ResTy, |
| 106 | llvm::SmallVectorImpl<CanQualType> &ArgTys); |
| 107 | |
| 108 | void BuildInstanceFunctionParams(CodeGenFunction &CGF, |
| 109 | QualType &ResTy, |
| 110 | FunctionArgList &Params); |
| 111 | |
| 112 | void EmitInstanceFunctionProlog(CodeGenFunction &CGF); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 113 | |
| 114 | CharUnits GetArrayCookieSize(QualType ElementType); |
| 115 | llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF, |
| 116 | llvm::Value *NewPtr, |
| 117 | llvm::Value *NumElements, |
| 118 | QualType ElementType); |
| 119 | void ReadArrayCookie(CodeGenFunction &CGF, llvm::Value *Ptr, |
| 120 | QualType ElementType, llvm::Value *&NumElements, |
| 121 | llvm::Value *&AllocPtr, CharUnits &CookieSize); |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 122 | |
John McCall | 3030eb8 | 2010-11-06 09:44:32 +0000 | [diff] [blame^] | 123 | void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D, |
| 124 | llvm::GlobalVariable *DeclPtr); |
Charles Davis | 3a811f1 | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 125 | }; |
John McCall | ee79a4c | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 126 | |
| 127 | class ARMCXXABI : public ItaniumCXXABI { |
| 128 | public: |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 129 | ARMCXXABI(CodeGen::CodeGenModule &CGM) : ItaniumCXXABI(CGM, /*ARM*/ true) {} |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 130 | |
| 131 | void BuildConstructorSignature(const CXXConstructorDecl *Ctor, |
| 132 | CXXCtorType T, |
| 133 | CanQualType &ResTy, |
| 134 | llvm::SmallVectorImpl<CanQualType> &ArgTys); |
| 135 | |
| 136 | void BuildDestructorSignature(const CXXDestructorDecl *Dtor, |
| 137 | CXXDtorType T, |
| 138 | CanQualType &ResTy, |
| 139 | llvm::SmallVectorImpl<CanQualType> &ArgTys); |
| 140 | |
| 141 | void BuildInstanceFunctionParams(CodeGenFunction &CGF, |
| 142 | QualType &ResTy, |
| 143 | FunctionArgList &Params); |
| 144 | |
| 145 | void EmitInstanceFunctionProlog(CodeGenFunction &CGF); |
| 146 | |
| 147 | void EmitReturnFromThunk(CodeGenFunction &CGF, RValue RV, QualType ResTy); |
| 148 | |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 149 | CharUnits GetArrayCookieSize(QualType ElementType); |
| 150 | llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF, |
| 151 | llvm::Value *NewPtr, |
| 152 | llvm::Value *NumElements, |
| 153 | QualType ElementType); |
| 154 | void ReadArrayCookie(CodeGenFunction &CGF, llvm::Value *Ptr, |
| 155 | QualType ElementType, llvm::Value *&NumElements, |
| 156 | llvm::Value *&AllocPtr, CharUnits &CookieSize); |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 157 | |
| 158 | private: |
| 159 | /// \brief Returns true if the given instance method is one of the |
| 160 | /// kinds that the ARM ABI says returns 'this'. |
| 161 | static bool HasThisReturn(GlobalDecl GD) { |
| 162 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
| 163 | return ((isa<CXXDestructorDecl>(MD) && GD.getDtorType() != Dtor_Deleting) || |
| 164 | (isa<CXXConstructorDecl>(MD))); |
| 165 | } |
John McCall | ee79a4c | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 166 | }; |
Charles Davis | 3a811f1 | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 169 | CodeGen::CGCXXABI *CodeGen::CreateItaniumCXXABI(CodeGenModule &CGM) { |
Charles Davis | 3a811f1 | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 170 | return new ItaniumCXXABI(CGM); |
| 171 | } |
| 172 | |
John McCall | ee79a4c | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 173 | CodeGen::CGCXXABI *CodeGen::CreateARMCXXABI(CodeGenModule &CGM) { |
| 174 | return new ARMCXXABI(CGM); |
| 175 | } |
| 176 | |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 177 | const llvm::Type * |
| 178 | ItaniumCXXABI::ConvertMemberPointerType(const MemberPointerType *MPT) { |
| 179 | if (MPT->isMemberDataPointer()) |
| 180 | return getPtrDiffTy(); |
| 181 | else |
| 182 | return llvm::StructType::get(CGM.getLLVMContext(), |
| 183 | getPtrDiffTy(), getPtrDiffTy(), NULL); |
John McCall | 875ab10 | 2010-08-22 06:43:33 +0000 | [diff] [blame] | 184 | } |
| 185 | |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 186 | /// In the Itanium and ARM ABIs, method pointers have the form: |
| 187 | /// struct { ptrdiff_t ptr; ptrdiff_t adj; } memptr; |
| 188 | /// |
| 189 | /// In the Itanium ABI: |
| 190 | /// - method pointers are virtual if (memptr.ptr & 1) is nonzero |
| 191 | /// - the this-adjustment is (memptr.adj) |
| 192 | /// - the virtual offset is (memptr.ptr - 1) |
| 193 | /// |
| 194 | /// In the ARM ABI: |
| 195 | /// - method pointers are virtual if (memptr.adj & 1) is nonzero |
| 196 | /// - the this-adjustment is (memptr.adj >> 1) |
| 197 | /// - the virtual offset is (memptr.ptr) |
| 198 | /// ARM uses 'adj' for the virtual flag because Thumb functions |
| 199 | /// may be only single-byte aligned. |
| 200 | /// |
| 201 | /// If the member is virtual, the adjusted 'this' pointer points |
| 202 | /// to a vtable pointer from which the virtual offset is applied. |
| 203 | /// |
| 204 | /// If the member is non-virtual, memptr.ptr is the address of |
| 205 | /// the function to call. |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 206 | llvm::Value * |
| 207 | ItaniumCXXABI::EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF, |
| 208 | llvm::Value *&This, |
| 209 | llvm::Value *MemFnPtr, |
| 210 | const MemberPointerType *MPT) { |
| 211 | CGBuilderTy &Builder = CGF.Builder; |
| 212 | |
| 213 | const FunctionProtoType *FPT = |
| 214 | MPT->getPointeeType()->getAs<FunctionProtoType>(); |
| 215 | const CXXRecordDecl *RD = |
| 216 | cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl()); |
| 217 | |
| 218 | const llvm::FunctionType *FTy = |
| 219 | CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(RD, FPT), |
| 220 | FPT->isVariadic()); |
| 221 | |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 222 | const llvm::IntegerType *ptrdiff = getPtrDiffTy(); |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 223 | llvm::Constant *ptrdiff_1 = llvm::ConstantInt::get(ptrdiff, 1); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 224 | |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 225 | llvm::BasicBlock *FnVirtual = CGF.createBasicBlock("memptr.virtual"); |
| 226 | llvm::BasicBlock *FnNonVirtual = CGF.createBasicBlock("memptr.nonvirtual"); |
| 227 | llvm::BasicBlock *FnEnd = CGF.createBasicBlock("memptr.end"); |
| 228 | |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 229 | // Extract memptr.adj, which is in the second field. |
| 230 | llvm::Value *RawAdj = Builder.CreateExtractValue(MemFnPtr, 1, "memptr.adj"); |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 231 | |
| 232 | // Compute the true adjustment. |
| 233 | llvm::Value *Adj = RawAdj; |
| 234 | if (IsARM) |
| 235 | Adj = Builder.CreateAShr(Adj, ptrdiff_1, "memptr.adj.shifted"); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 236 | |
| 237 | // Apply the adjustment and cast back to the original struct type |
| 238 | // for consistency. |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 239 | llvm::Value *Ptr = Builder.CreateBitCast(This, Builder.getInt8PtrTy()); |
| 240 | Ptr = Builder.CreateInBoundsGEP(Ptr, Adj); |
| 241 | This = Builder.CreateBitCast(Ptr, This->getType(), "this.adjusted"); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 242 | |
| 243 | // Load the function pointer. |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 244 | llvm::Value *FnAsInt = Builder.CreateExtractValue(MemFnPtr, 0, "memptr.ptr"); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 245 | |
| 246 | // If the LSB in the function pointer is 1, the function pointer points to |
| 247 | // a virtual function. |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 248 | llvm::Value *IsVirtual; |
| 249 | if (IsARM) |
| 250 | IsVirtual = Builder.CreateAnd(RawAdj, ptrdiff_1); |
| 251 | else |
| 252 | IsVirtual = Builder.CreateAnd(FnAsInt, ptrdiff_1); |
| 253 | IsVirtual = Builder.CreateIsNotNull(IsVirtual, "memptr.isvirtual"); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 254 | Builder.CreateCondBr(IsVirtual, FnVirtual, FnNonVirtual); |
| 255 | |
| 256 | // In the virtual path, the adjustment left 'This' pointing to the |
| 257 | // vtable of the correct base subobject. The "function pointer" is an |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 258 | // offset within the vtable (+1 for the virtual flag on non-ARM). |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 259 | CGF.EmitBlock(FnVirtual); |
| 260 | |
| 261 | // Cast the adjusted this to a pointer to vtable pointer and load. |
| 262 | const llvm::Type *VTableTy = Builder.getInt8PtrTy(); |
| 263 | llvm::Value *VTable = Builder.CreateBitCast(This, VTableTy->getPointerTo()); |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 264 | VTable = Builder.CreateLoad(VTable, "memptr.vtable"); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 265 | |
| 266 | // Apply the offset. |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 267 | llvm::Value *VTableOffset = FnAsInt; |
| 268 | if (!IsARM) VTableOffset = Builder.CreateSub(VTableOffset, ptrdiff_1); |
| 269 | VTable = Builder.CreateGEP(VTable, VTableOffset); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 270 | |
| 271 | // Load the virtual function to call. |
| 272 | VTable = Builder.CreateBitCast(VTable, FTy->getPointerTo()->getPointerTo()); |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 273 | llvm::Value *VirtualFn = Builder.CreateLoad(VTable, "memptr.virtualfn"); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 274 | CGF.EmitBranch(FnEnd); |
| 275 | |
| 276 | // In the non-virtual path, the function pointer is actually a |
| 277 | // function pointer. |
| 278 | CGF.EmitBlock(FnNonVirtual); |
| 279 | llvm::Value *NonVirtualFn = |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 280 | Builder.CreateIntToPtr(FnAsInt, FTy->getPointerTo(), "memptr.nonvirtualfn"); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 281 | |
| 282 | // We're done. |
| 283 | CGF.EmitBlock(FnEnd); |
| 284 | llvm::PHINode *Callee = Builder.CreatePHI(FTy->getPointerTo()); |
| 285 | Callee->reserveOperandSpace(2); |
| 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 | |
| 358 | const CXXRecordDecl *BaseDecl, *DerivedDecl; |
| 359 | if (DerivedToBase) |
| 360 | DerivedDecl = SrcDecl, BaseDecl = DestDecl; |
| 361 | else |
| 362 | BaseDecl = SrcDecl, DerivedDecl = DestDecl; |
| 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 | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 496 | llvm::Constant *ItaniumCXXABI::EmitMemberPointer(const FieldDecl *FD) { |
| 497 | // Itanium C++ ABI 2.3: |
| 498 | // A pointer to data member is an offset from the base address of |
| 499 | // the class object containing it, represented as a ptrdiff_t |
| 500 | |
John McCall | 9cb2cee | 2010-09-02 10:25:57 +0000 | [diff] [blame] | 501 | QualType ClassType = getContext().getTypeDeclType(FD->getParent()); |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 502 | const llvm::StructType *ClassLTy = |
| 503 | cast<llvm::StructType>(CGM.getTypes().ConvertType(ClassType)); |
| 504 | |
| 505 | const CGRecordLayout &RL = CGM.getTypes().getCGRecordLayout(FD->getParent()); |
| 506 | unsigned FieldNo = RL.getLLVMFieldNo(FD); |
| 507 | uint64_t Offset = |
| 508 | CGM.getTargetData().getStructLayout(ClassLTy)->getElementOffset(FieldNo); |
| 509 | |
| 510 | return llvm::ConstantInt::get(getPtrDiffTy(), Offset); |
| 511 | } |
| 512 | |
| 513 | llvm::Constant *ItaniumCXXABI::EmitMemberPointer(const CXXMethodDecl *MD) { |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 514 | assert(MD->isInstance() && "Member function must not be static!"); |
| 515 | MD = MD->getCanonicalDecl(); |
| 516 | |
| 517 | CodeGenTypes &Types = CGM.getTypes(); |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 518 | const llvm::Type *ptrdiff_t = getPtrDiffTy(); |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 519 | |
| 520 | // Get the function pointer (or index if this is a virtual function). |
| 521 | llvm::Constant *MemPtr[2]; |
| 522 | if (MD->isVirtual()) { |
| 523 | uint64_t Index = CGM.getVTables().getMethodVTableIndex(MD); |
| 524 | |
| 525 | // FIXME: We shouldn't use / 8 here. |
| 526 | uint64_t PointerWidthInBytes = |
John McCall | 9cb2cee | 2010-09-02 10:25:57 +0000 | [diff] [blame] | 527 | getContext().Target.getPointerWidth(0) / 8; |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 528 | uint64_t VTableOffset = (Index * PointerWidthInBytes); |
| 529 | |
| 530 | if (IsARM) { |
| 531 | // ARM C++ ABI 3.2.1: |
| 532 | // This ABI specifies that adj contains twice the this |
| 533 | // adjustment, plus 1 if the member function is virtual. The |
| 534 | // least significant bit of adj then makes exactly the same |
| 535 | // discrimination as the least significant bit of ptr does for |
| 536 | // Itanium. |
| 537 | MemPtr[0] = llvm::ConstantInt::get(ptrdiff_t, VTableOffset); |
| 538 | MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t, 1); |
| 539 | } else { |
| 540 | // Itanium C++ ABI 2.3: |
| 541 | // For a virtual function, [the pointer field] is 1 plus the |
| 542 | // virtual table offset (in bytes) of the function, |
| 543 | // represented as a ptrdiff_t. |
| 544 | MemPtr[0] = llvm::ConstantInt::get(ptrdiff_t, VTableOffset + 1); |
| 545 | MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t, 0); |
| 546 | } |
| 547 | } else { |
| 548 | const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); |
| 549 | const llvm::Type *Ty; |
| 550 | // Check whether the function has a computable LLVM signature. |
| 551 | if (!CodeGenTypes::VerifyFuncTypeComplete(FPT)) { |
| 552 | // The function has a computable LLVM signature; use the correct type. |
| 553 | Ty = Types.GetFunctionType(Types.getFunctionInfo(MD), FPT->isVariadic()); |
| 554 | } else { |
| 555 | // Use an arbitrary non-function type to tell GetAddrOfFunction that the |
| 556 | // function type is incomplete. |
| 557 | Ty = ptrdiff_t; |
| 558 | } |
| 559 | |
| 560 | llvm::Constant *Addr = CGM.GetAddrOfFunction(MD, Ty); |
| 561 | MemPtr[0] = llvm::ConstantExpr::getPtrToInt(Addr, ptrdiff_t); |
| 562 | MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t, 0); |
| 563 | } |
John McCall | 875ab10 | 2010-08-22 06:43:33 +0000 | [diff] [blame] | 564 | |
| 565 | return llvm::ConstantStruct::get(CGM.getLLVMContext(), |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 566 | MemPtr, 2, /*Packed=*/false); |
John McCall | 875ab10 | 2010-08-22 06:43:33 +0000 | [diff] [blame] | 567 | } |
| 568 | |
John McCall | e9fd7eb | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 569 | /// The comparison algorithm is pretty easy: the member pointers are |
| 570 | /// the same if they're either bitwise identical *or* both null. |
| 571 | /// |
| 572 | /// ARM is different here only because null-ness is more complicated. |
| 573 | llvm::Value * |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 574 | ItaniumCXXABI::EmitMemberPointerComparison(CodeGenFunction &CGF, |
| 575 | llvm::Value *L, |
| 576 | llvm::Value *R, |
| 577 | const MemberPointerType *MPT, |
| 578 | bool Inequality) { |
John McCall | e9fd7eb | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 579 | CGBuilderTy &Builder = CGF.Builder; |
| 580 | |
John McCall | e9fd7eb | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 581 | llvm::ICmpInst::Predicate Eq; |
| 582 | llvm::Instruction::BinaryOps And, Or; |
| 583 | if (Inequality) { |
| 584 | Eq = llvm::ICmpInst::ICMP_NE; |
| 585 | And = llvm::Instruction::Or; |
| 586 | Or = llvm::Instruction::And; |
| 587 | } else { |
| 588 | Eq = llvm::ICmpInst::ICMP_EQ; |
| 589 | And = llvm::Instruction::And; |
| 590 | Or = llvm::Instruction::Or; |
| 591 | } |
| 592 | |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 593 | // Member data pointers are easy because there's a unique null |
| 594 | // value, so it just comes down to bitwise equality. |
| 595 | if (MPT->isMemberDataPointer()) |
| 596 | return Builder.CreateICmp(Eq, L, R); |
| 597 | |
| 598 | // For member function pointers, the tautologies are more complex. |
| 599 | // The Itanium tautology is: |
John McCall | de719f7 | 2010-08-23 06:56:36 +0000 | [diff] [blame] | 600 | // (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] | 601 | // The ARM tautology is: |
John McCall | de719f7 | 2010-08-23 06:56:36 +0000 | [diff] [blame] | 602 | // (L == R) <==> (L.ptr == R.ptr && |
| 603 | // (L.adj == R.adj || |
| 604 | // (L.ptr == 0 && ((L.adj|R.adj) & 1) == 0))) |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 605 | // The inequality tautologies have exactly the same structure, except |
| 606 | // applying De Morgan's laws. |
| 607 | |
| 608 | llvm::Value *LPtr = Builder.CreateExtractValue(L, 0, "lhs.memptr.ptr"); |
| 609 | llvm::Value *RPtr = Builder.CreateExtractValue(R, 0, "rhs.memptr.ptr"); |
| 610 | |
John McCall | e9fd7eb | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 611 | // This condition tests whether L.ptr == R.ptr. This must always be |
| 612 | // true for equality to hold. |
| 613 | llvm::Value *PtrEq = Builder.CreateICmp(Eq, LPtr, RPtr, "cmp.ptr"); |
| 614 | |
| 615 | // This condition, together with the assumption that L.ptr == R.ptr, |
| 616 | // tests whether the pointers are both null. ARM imposes an extra |
| 617 | // condition. |
| 618 | llvm::Value *Zero = llvm::Constant::getNullValue(LPtr->getType()); |
| 619 | llvm::Value *EqZero = Builder.CreateICmp(Eq, LPtr, Zero, "cmp.ptr.null"); |
| 620 | |
| 621 | // This condition tests whether L.adj == R.adj. If this isn't |
| 622 | // true, the pointers are unequal unless they're both null. |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 623 | llvm::Value *LAdj = Builder.CreateExtractValue(L, 1, "lhs.memptr.adj"); |
| 624 | llvm::Value *RAdj = Builder.CreateExtractValue(R, 1, "rhs.memptr.adj"); |
John McCall | e9fd7eb | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 625 | llvm::Value *AdjEq = Builder.CreateICmp(Eq, LAdj, RAdj, "cmp.adj"); |
| 626 | |
| 627 | // Null member function pointers on ARM clear the low bit of Adj, |
| 628 | // so the zero condition has to check that neither low bit is set. |
| 629 | if (IsARM) { |
| 630 | llvm::Value *One = llvm::ConstantInt::get(LPtr->getType(), 1); |
| 631 | |
| 632 | // Compute (l.adj | r.adj) & 1 and test it against zero. |
| 633 | llvm::Value *OrAdj = Builder.CreateOr(LAdj, RAdj, "or.adj"); |
| 634 | llvm::Value *OrAdjAnd1 = Builder.CreateAnd(OrAdj, One); |
| 635 | llvm::Value *OrAdjAnd1EqZero = Builder.CreateICmp(Eq, OrAdjAnd1, Zero, |
| 636 | "cmp.or.adj"); |
| 637 | EqZero = Builder.CreateBinOp(And, EqZero, OrAdjAnd1EqZero); |
| 638 | } |
| 639 | |
| 640 | // Tie together all our conditions. |
| 641 | llvm::Value *Result = Builder.CreateBinOp(Or, EqZero, AdjEq); |
| 642 | Result = Builder.CreateBinOp(And, PtrEq, Result, |
| 643 | Inequality ? "memptr.ne" : "memptr.eq"); |
| 644 | return Result; |
| 645 | } |
| 646 | |
| 647 | llvm::Value * |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 648 | ItaniumCXXABI::EmitMemberPointerIsNotNull(CodeGenFunction &CGF, |
| 649 | llvm::Value *MemPtr, |
| 650 | const MemberPointerType *MPT) { |
John McCall | e9fd7eb | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 651 | CGBuilderTy &Builder = CGF.Builder; |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 652 | |
| 653 | /// For member data pointers, this is just a check against -1. |
| 654 | if (MPT->isMemberDataPointer()) { |
| 655 | assert(MemPtr->getType() == getPtrDiffTy()); |
| 656 | llvm::Value *NegativeOne = |
| 657 | llvm::Constant::getAllOnesValue(MemPtr->getType()); |
| 658 | return Builder.CreateICmpNE(MemPtr, NegativeOne, "memptr.tobool"); |
| 659 | } |
John McCall | e9fd7eb | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 660 | |
| 661 | // In Itanium, a member function pointer is null if 'ptr' is null. |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 662 | llvm::Value *Ptr = Builder.CreateExtractValue(MemPtr, 0, "memptr.ptr"); |
John McCall | e9fd7eb | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 663 | |
| 664 | llvm::Constant *Zero = llvm::ConstantInt::get(Ptr->getType(), 0); |
| 665 | llvm::Value *Result = Builder.CreateICmpNE(Ptr, Zero, "memptr.tobool"); |
| 666 | |
| 667 | // In ARM, it's that, plus the low bit of 'adj' must be zero. |
| 668 | if (IsARM) { |
| 669 | llvm::Constant *One = llvm::ConstantInt::get(Ptr->getType(), 1); |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 670 | llvm::Value *Adj = Builder.CreateExtractValue(MemPtr, 1, "memptr.adj"); |
John McCall | e9fd7eb | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 671 | llvm::Value *VirtualBit = Builder.CreateAnd(Adj, One, "memptr.virtualbit"); |
| 672 | llvm::Value *IsNotVirtual = Builder.CreateICmpEQ(VirtualBit, Zero, |
| 673 | "memptr.notvirtual"); |
| 674 | Result = Builder.CreateAnd(Result, IsNotVirtual); |
| 675 | } |
| 676 | |
| 677 | return Result; |
| 678 | } |
John McCall | 875ab10 | 2010-08-22 06:43:33 +0000 | [diff] [blame] | 679 | |
John McCall | f16aa10 | 2010-08-22 21:01:12 +0000 | [diff] [blame] | 680 | /// The Itanium ABI requires non-zero initialization only for data |
| 681 | /// member pointers, for which '0' is a valid offset. |
| 682 | bool ItaniumCXXABI::isZeroInitializable(const MemberPointerType *MPT) { |
| 683 | return MPT->getPointeeType()->isFunctionType(); |
John McCall | cf2c85e | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 684 | } |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 685 | |
| 686 | /// The generic ABI passes 'this', plus a VTT if it's initializing a |
| 687 | /// base subobject. |
| 688 | void ItaniumCXXABI::BuildConstructorSignature(const CXXConstructorDecl *Ctor, |
| 689 | CXXCtorType Type, |
| 690 | CanQualType &ResTy, |
| 691 | llvm::SmallVectorImpl<CanQualType> &ArgTys) { |
John McCall | 9cb2cee | 2010-09-02 10:25:57 +0000 | [diff] [blame] | 692 | ASTContext &Context = getContext(); |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 693 | |
| 694 | // 'this' is already there. |
| 695 | |
| 696 | // Check if we need to add a VTT parameter (which has type void **). |
| 697 | if (Type == Ctor_Base && Ctor->getParent()->getNumVBases() != 0) |
| 698 | ArgTys.push_back(Context.getPointerType(Context.VoidPtrTy)); |
| 699 | } |
| 700 | |
| 701 | /// The ARM ABI does the same as the Itanium ABI, but returns 'this'. |
| 702 | void ARMCXXABI::BuildConstructorSignature(const CXXConstructorDecl *Ctor, |
| 703 | CXXCtorType Type, |
| 704 | CanQualType &ResTy, |
| 705 | llvm::SmallVectorImpl<CanQualType> &ArgTys) { |
| 706 | ItaniumCXXABI::BuildConstructorSignature(Ctor, Type, ResTy, ArgTys); |
| 707 | ResTy = ArgTys[0]; |
| 708 | } |
| 709 | |
| 710 | /// The generic ABI passes 'this', plus a VTT if it's destroying a |
| 711 | /// base subobject. |
| 712 | void ItaniumCXXABI::BuildDestructorSignature(const CXXDestructorDecl *Dtor, |
| 713 | CXXDtorType Type, |
| 714 | CanQualType &ResTy, |
| 715 | llvm::SmallVectorImpl<CanQualType> &ArgTys) { |
John McCall | 9cb2cee | 2010-09-02 10:25:57 +0000 | [diff] [blame] | 716 | ASTContext &Context = getContext(); |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 717 | |
| 718 | // 'this' is already there. |
| 719 | |
| 720 | // Check if we need to add a VTT parameter (which has type void **). |
| 721 | if (Type == Dtor_Base && Dtor->getParent()->getNumVBases() != 0) |
| 722 | ArgTys.push_back(Context.getPointerType(Context.VoidPtrTy)); |
| 723 | } |
| 724 | |
| 725 | /// The ARM ABI does the same as the Itanium ABI, but returns 'this' |
| 726 | /// for non-deleting destructors. |
| 727 | void ARMCXXABI::BuildDestructorSignature(const CXXDestructorDecl *Dtor, |
| 728 | CXXDtorType Type, |
| 729 | CanQualType &ResTy, |
| 730 | llvm::SmallVectorImpl<CanQualType> &ArgTys) { |
| 731 | ItaniumCXXABI::BuildDestructorSignature(Dtor, Type, ResTy, ArgTys); |
| 732 | |
| 733 | if (Type != Dtor_Deleting) |
| 734 | ResTy = ArgTys[0]; |
| 735 | } |
| 736 | |
| 737 | void ItaniumCXXABI::BuildInstanceFunctionParams(CodeGenFunction &CGF, |
| 738 | QualType &ResTy, |
| 739 | FunctionArgList &Params) { |
| 740 | /// Create the 'this' variable. |
| 741 | BuildThisParam(CGF, Params); |
| 742 | |
| 743 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(CGF.CurGD.getDecl()); |
| 744 | assert(MD->isInstance()); |
| 745 | |
| 746 | // Check if we need a VTT parameter as well. |
| 747 | if (CodeGenVTables::needsVTTParameter(CGF.CurGD)) { |
John McCall | 9cb2cee | 2010-09-02 10:25:57 +0000 | [diff] [blame] | 748 | ASTContext &Context = getContext(); |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 749 | |
| 750 | // FIXME: avoid the fake decl |
| 751 | QualType T = Context.getPointerType(Context.VoidPtrTy); |
| 752 | ImplicitParamDecl *VTTDecl |
| 753 | = ImplicitParamDecl::Create(Context, 0, MD->getLocation(), |
| 754 | &Context.Idents.get("vtt"), T); |
| 755 | Params.push_back(std::make_pair(VTTDecl, VTTDecl->getType())); |
| 756 | getVTTDecl(CGF) = VTTDecl; |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | void ARMCXXABI::BuildInstanceFunctionParams(CodeGenFunction &CGF, |
| 761 | QualType &ResTy, |
| 762 | FunctionArgList &Params) { |
| 763 | ItaniumCXXABI::BuildInstanceFunctionParams(CGF, ResTy, Params); |
| 764 | |
| 765 | // Return 'this' from certain constructors and destructors. |
| 766 | if (HasThisReturn(CGF.CurGD)) |
| 767 | ResTy = Params[0].second; |
| 768 | } |
| 769 | |
| 770 | void ItaniumCXXABI::EmitInstanceFunctionProlog(CodeGenFunction &CGF) { |
| 771 | /// Initialize the 'this' slot. |
| 772 | EmitThisParam(CGF); |
| 773 | |
| 774 | /// Initialize the 'vtt' slot if needed. |
| 775 | if (getVTTDecl(CGF)) { |
| 776 | getVTTValue(CGF) |
| 777 | = CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(getVTTDecl(CGF)), |
| 778 | "vtt"); |
| 779 | } |
| 780 | } |
| 781 | |
| 782 | void ARMCXXABI::EmitInstanceFunctionProlog(CodeGenFunction &CGF) { |
| 783 | ItaniumCXXABI::EmitInstanceFunctionProlog(CGF); |
| 784 | |
| 785 | /// Initialize the return slot to 'this' at the start of the |
| 786 | /// function. |
| 787 | if (HasThisReturn(CGF.CurGD)) |
| 788 | CGF.Builder.CreateStore(CGF.LoadCXXThis(), CGF.ReturnValue); |
| 789 | } |
| 790 | |
| 791 | void ARMCXXABI::EmitReturnFromThunk(CodeGenFunction &CGF, |
| 792 | RValue RV, QualType ResultType) { |
| 793 | if (!isa<CXXDestructorDecl>(CGF.CurGD.getDecl())) |
| 794 | return ItaniumCXXABI::EmitReturnFromThunk(CGF, RV, ResultType); |
| 795 | |
| 796 | // Destructor thunks in the ARM ABI have indeterminate results. |
| 797 | const llvm::Type *T = |
| 798 | cast<llvm::PointerType>(CGF.ReturnValue->getType())->getElementType(); |
| 799 | RValue Undef = RValue::get(llvm::UndefValue::get(T)); |
| 800 | return ItaniumCXXABI::EmitReturnFromThunk(CGF, Undef, ResultType); |
| 801 | } |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 802 | |
| 803 | /************************** Array allocation cookies **************************/ |
| 804 | |
| 805 | bool ItaniumCXXABI::NeedsArrayCookie(QualType ElementType) { |
John McCall | 9cb2cee | 2010-09-02 10:25:57 +0000 | [diff] [blame] | 806 | ElementType = getContext().getBaseElementType(ElementType); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 807 | const RecordType *RT = ElementType->getAs<RecordType>(); |
| 808 | if (!RT) return false; |
| 809 | |
| 810 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 811 | |
| 812 | // If the class has a non-trivial destructor, it always needs a cookie. |
| 813 | if (!RD->hasTrivialDestructor()) return true; |
| 814 | |
| 815 | // If the class's usual deallocation function takes two arguments, |
| 816 | // it needs a cookie. Otherwise we don't need a cookie. |
| 817 | const CXXMethodDecl *UsualDeallocationFunction = 0; |
| 818 | |
| 819 | // Usual deallocation functions of this form are always found on the |
| 820 | // class. |
| 821 | // |
| 822 | // FIXME: what exactly is this code supposed to do if there's an |
| 823 | // ambiguity? That's possible with using declarations. |
| 824 | DeclarationName OpName = |
John McCall | 9cb2cee | 2010-09-02 10:25:57 +0000 | [diff] [blame] | 825 | getContext().DeclarationNames.getCXXOperatorName(OO_Array_Delete); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 826 | DeclContext::lookup_const_iterator Op, OpEnd; |
| 827 | for (llvm::tie(Op, OpEnd) = RD->lookup(OpName); Op != OpEnd; ++Op) { |
| 828 | const CXXMethodDecl *Delete = |
| 829 | cast<CXXMethodDecl>((*Op)->getUnderlyingDecl()); |
| 830 | |
| 831 | if (Delete->isUsualDeallocationFunction()) { |
| 832 | UsualDeallocationFunction = Delete; |
| 833 | break; |
| 834 | } |
| 835 | } |
| 836 | |
| 837 | // No usual deallocation function, we don't need a cookie. |
| 838 | if (!UsualDeallocationFunction) |
| 839 | return false; |
| 840 | |
| 841 | // The usual deallocation function doesn't take a size_t argument, |
| 842 | // so we don't need a cookie. |
| 843 | if (UsualDeallocationFunction->getNumParams() == 1) |
| 844 | return false; |
| 845 | |
| 846 | assert(UsualDeallocationFunction->getNumParams() == 2 && |
| 847 | "Unexpected deallocation function type!"); |
| 848 | return true; |
| 849 | } |
| 850 | |
| 851 | CharUnits ItaniumCXXABI::GetArrayCookieSize(QualType ElementType) { |
| 852 | if (!NeedsArrayCookie(ElementType)) |
| 853 | return CharUnits::Zero(); |
| 854 | |
| 855 | // Padding is the maximum of sizeof(size_t) and alignof(ElementType) |
John McCall | 9cb2cee | 2010-09-02 10:25:57 +0000 | [diff] [blame] | 856 | ASTContext &Ctx = getContext(); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 857 | return std::max(Ctx.getTypeSizeInChars(Ctx.getSizeType()), |
| 858 | Ctx.getTypeAlignInChars(ElementType)); |
| 859 | } |
| 860 | |
| 861 | llvm::Value *ItaniumCXXABI::InitializeArrayCookie(CodeGenFunction &CGF, |
| 862 | llvm::Value *NewPtr, |
| 863 | llvm::Value *NumElements, |
| 864 | QualType ElementType) { |
| 865 | assert(NeedsArrayCookie(ElementType)); |
| 866 | |
| 867 | unsigned AS = cast<llvm::PointerType>(NewPtr->getType())->getAddressSpace(); |
| 868 | |
John McCall | 9cb2cee | 2010-09-02 10:25:57 +0000 | [diff] [blame] | 869 | ASTContext &Ctx = getContext(); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 870 | QualType SizeTy = Ctx.getSizeType(); |
| 871 | CharUnits SizeSize = Ctx.getTypeSizeInChars(SizeTy); |
| 872 | |
| 873 | // The size of the cookie. |
| 874 | CharUnits CookieSize = |
| 875 | std::max(SizeSize, Ctx.getTypeAlignInChars(ElementType)); |
| 876 | |
| 877 | // Compute an offset to the cookie. |
| 878 | llvm::Value *CookiePtr = NewPtr; |
| 879 | CharUnits CookieOffset = CookieSize - SizeSize; |
| 880 | if (!CookieOffset.isZero()) |
| 881 | CookiePtr = CGF.Builder.CreateConstInBoundsGEP1_64(CookiePtr, |
| 882 | CookieOffset.getQuantity()); |
| 883 | |
| 884 | // Write the number of elements into the appropriate slot. |
| 885 | llvm::Value *NumElementsPtr |
| 886 | = CGF.Builder.CreateBitCast(CookiePtr, |
| 887 | CGF.ConvertType(SizeTy)->getPointerTo(AS)); |
| 888 | CGF.Builder.CreateStore(NumElements, NumElementsPtr); |
| 889 | |
| 890 | // Finally, compute a pointer to the actual data buffer by skipping |
| 891 | // over the cookie completely. |
| 892 | return CGF.Builder.CreateConstInBoundsGEP1_64(NewPtr, |
| 893 | CookieSize.getQuantity()); |
| 894 | } |
| 895 | |
| 896 | void ItaniumCXXABI::ReadArrayCookie(CodeGenFunction &CGF, |
| 897 | llvm::Value *Ptr, |
| 898 | QualType ElementType, |
| 899 | llvm::Value *&NumElements, |
| 900 | llvm::Value *&AllocPtr, |
| 901 | CharUnits &CookieSize) { |
| 902 | // Derive a char* in the same address space as the pointer. |
| 903 | unsigned AS = cast<llvm::PointerType>(Ptr->getType())->getAddressSpace(); |
| 904 | const llvm::Type *CharPtrTy = CGF.Builder.getInt8Ty()->getPointerTo(AS); |
| 905 | |
| 906 | // If we don't need an array cookie, bail out early. |
| 907 | if (!NeedsArrayCookie(ElementType)) { |
| 908 | AllocPtr = CGF.Builder.CreateBitCast(Ptr, CharPtrTy); |
| 909 | NumElements = 0; |
| 910 | CookieSize = CharUnits::Zero(); |
| 911 | return; |
| 912 | } |
| 913 | |
John McCall | 9cb2cee | 2010-09-02 10:25:57 +0000 | [diff] [blame] | 914 | QualType SizeTy = getContext().getSizeType(); |
| 915 | CharUnits SizeSize = getContext().getTypeSizeInChars(SizeTy); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 916 | const llvm::Type *SizeLTy = CGF.ConvertType(SizeTy); |
| 917 | |
| 918 | CookieSize |
John McCall | 9cb2cee | 2010-09-02 10:25:57 +0000 | [diff] [blame] | 919 | = std::max(SizeSize, getContext().getTypeAlignInChars(ElementType)); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 920 | |
| 921 | CharUnits NumElementsOffset = CookieSize - SizeSize; |
| 922 | |
| 923 | // Compute the allocated pointer. |
| 924 | AllocPtr = CGF.Builder.CreateBitCast(Ptr, CharPtrTy); |
| 925 | AllocPtr = CGF.Builder.CreateConstInBoundsGEP1_64(AllocPtr, |
| 926 | -CookieSize.getQuantity()); |
| 927 | |
| 928 | llvm::Value *NumElementsPtr = AllocPtr; |
| 929 | if (!NumElementsOffset.isZero()) |
| 930 | NumElementsPtr = |
| 931 | CGF.Builder.CreateConstInBoundsGEP1_64(NumElementsPtr, |
| 932 | NumElementsOffset.getQuantity()); |
| 933 | NumElementsPtr = |
| 934 | CGF.Builder.CreateBitCast(NumElementsPtr, SizeLTy->getPointerTo(AS)); |
| 935 | NumElements = CGF.Builder.CreateLoad(NumElementsPtr); |
| 936 | } |
| 937 | |
| 938 | CharUnits ARMCXXABI::GetArrayCookieSize(QualType ElementType) { |
| 939 | if (!NeedsArrayCookie(ElementType)) |
| 940 | return CharUnits::Zero(); |
| 941 | |
| 942 | // On ARM, the cookie is always: |
| 943 | // struct array_cookie { |
| 944 | // std::size_t element_size; // element_size != 0 |
| 945 | // std::size_t element_count; |
| 946 | // }; |
| 947 | // TODO: what should we do if the allocated type actually wants |
| 948 | // greater alignment? |
| 949 | return getContext().getTypeSizeInChars(getContext().getSizeType()) * 2; |
| 950 | } |
| 951 | |
| 952 | llvm::Value *ARMCXXABI::InitializeArrayCookie(CodeGenFunction &CGF, |
| 953 | llvm::Value *NewPtr, |
| 954 | llvm::Value *NumElements, |
| 955 | QualType ElementType) { |
| 956 | assert(NeedsArrayCookie(ElementType)); |
| 957 | |
| 958 | // NewPtr is a char*. |
| 959 | |
| 960 | unsigned AS = cast<llvm::PointerType>(NewPtr->getType())->getAddressSpace(); |
| 961 | |
John McCall | 9cb2cee | 2010-09-02 10:25:57 +0000 | [diff] [blame] | 962 | ASTContext &Ctx = getContext(); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 963 | CharUnits SizeSize = Ctx.getTypeSizeInChars(Ctx.getSizeType()); |
| 964 | const llvm::IntegerType *SizeTy = |
| 965 | cast<llvm::IntegerType>(CGF.ConvertType(Ctx.getSizeType())); |
| 966 | |
| 967 | // The cookie is always at the start of the buffer. |
| 968 | llvm::Value *CookiePtr = NewPtr; |
| 969 | |
| 970 | // The first element is the element size. |
| 971 | CookiePtr = CGF.Builder.CreateBitCast(CookiePtr, SizeTy->getPointerTo(AS)); |
| 972 | llvm::Value *ElementSize = llvm::ConstantInt::get(SizeTy, |
| 973 | Ctx.getTypeSizeInChars(ElementType).getQuantity()); |
| 974 | CGF.Builder.CreateStore(ElementSize, CookiePtr); |
| 975 | |
| 976 | // The second element is the element count. |
| 977 | CookiePtr = CGF.Builder.CreateConstInBoundsGEP1_32(CookiePtr, 1); |
| 978 | CGF.Builder.CreateStore(NumElements, CookiePtr); |
| 979 | |
| 980 | // Finally, compute a pointer to the actual data buffer by skipping |
| 981 | // over the cookie completely. |
| 982 | CharUnits CookieSize = 2 * SizeSize; |
| 983 | return CGF.Builder.CreateConstInBoundsGEP1_64(NewPtr, |
| 984 | CookieSize.getQuantity()); |
| 985 | } |
| 986 | |
| 987 | void ARMCXXABI::ReadArrayCookie(CodeGenFunction &CGF, |
| 988 | llvm::Value *Ptr, |
| 989 | QualType ElementType, |
| 990 | llvm::Value *&NumElements, |
| 991 | llvm::Value *&AllocPtr, |
| 992 | CharUnits &CookieSize) { |
| 993 | // Derive a char* in the same address space as the pointer. |
| 994 | unsigned AS = cast<llvm::PointerType>(Ptr->getType())->getAddressSpace(); |
| 995 | const llvm::Type *CharPtrTy = CGF.Builder.getInt8Ty()->getPointerTo(AS); |
| 996 | |
| 997 | // If we don't need an array cookie, bail out early. |
| 998 | if (!NeedsArrayCookie(ElementType)) { |
| 999 | AllocPtr = CGF.Builder.CreateBitCast(Ptr, CharPtrTy); |
| 1000 | NumElements = 0; |
| 1001 | CookieSize = CharUnits::Zero(); |
| 1002 | return; |
| 1003 | } |
| 1004 | |
John McCall | 9cb2cee | 2010-09-02 10:25:57 +0000 | [diff] [blame] | 1005 | QualType SizeTy = getContext().getSizeType(); |
| 1006 | CharUnits SizeSize = getContext().getTypeSizeInChars(SizeTy); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1007 | const llvm::Type *SizeLTy = CGF.ConvertType(SizeTy); |
| 1008 | |
| 1009 | // The cookie size is always 2 * sizeof(size_t). |
| 1010 | CookieSize = 2 * SizeSize; |
| 1011 | CharUnits NumElementsOffset = CookieSize - SizeSize; |
| 1012 | |
| 1013 | // The allocated pointer is the input ptr, minus that amount. |
| 1014 | AllocPtr = CGF.Builder.CreateBitCast(Ptr, CharPtrTy); |
| 1015 | AllocPtr = CGF.Builder.CreateConstInBoundsGEP1_64(AllocPtr, |
| 1016 | -CookieSize.getQuantity()); |
| 1017 | |
| 1018 | // The number of elements is at offset sizeof(size_t) relative to that. |
| 1019 | llvm::Value *NumElementsPtr |
| 1020 | = CGF.Builder.CreateConstInBoundsGEP1_64(AllocPtr, |
| 1021 | SizeSize.getQuantity()); |
| 1022 | NumElementsPtr = |
| 1023 | CGF.Builder.CreateBitCast(NumElementsPtr, SizeLTy->getPointerTo(AS)); |
| 1024 | NumElements = CGF.Builder.CreateLoad(NumElementsPtr); |
| 1025 | } |
| 1026 | |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1027 | /*********************** Static local initialization **************************/ |
| 1028 | |
| 1029 | static llvm::Constant *getGuardAcquireFn(CodeGenModule &CGM, |
| 1030 | const llvm::PointerType *GuardPtrTy) { |
| 1031 | // int __cxa_guard_acquire(__guard *guard_object); |
| 1032 | |
| 1033 | std::vector<const llvm::Type*> Args(1, GuardPtrTy); |
| 1034 | const llvm::FunctionType *FTy = |
| 1035 | llvm::FunctionType::get(CGM.getTypes().ConvertType(CGM.getContext().IntTy), |
| 1036 | Args, /*isVarArg=*/false); |
| 1037 | |
| 1038 | return CGM.CreateRuntimeFunction(FTy, "__cxa_guard_acquire"); |
| 1039 | } |
| 1040 | |
| 1041 | static llvm::Constant *getGuardReleaseFn(CodeGenModule &CGM, |
| 1042 | const llvm::PointerType *GuardPtrTy) { |
| 1043 | // void __cxa_guard_release(__guard *guard_object); |
| 1044 | |
| 1045 | std::vector<const llvm::Type*> Args(1, GuardPtrTy); |
| 1046 | |
| 1047 | const llvm::FunctionType *FTy = |
| 1048 | llvm::FunctionType::get(llvm::Type::getVoidTy(CGM.getLLVMContext()), |
| 1049 | Args, /*isVarArg=*/false); |
| 1050 | |
| 1051 | return CGM.CreateRuntimeFunction(FTy, "__cxa_guard_release"); |
| 1052 | } |
| 1053 | |
| 1054 | static llvm::Constant *getGuardAbortFn(CodeGenModule &CGM, |
| 1055 | const llvm::PointerType *GuardPtrTy) { |
| 1056 | // void __cxa_guard_abort(__guard *guard_object); |
| 1057 | |
| 1058 | std::vector<const llvm::Type*> Args(1, GuardPtrTy); |
| 1059 | |
| 1060 | const llvm::FunctionType *FTy = |
| 1061 | llvm::FunctionType::get(llvm::Type::getVoidTy(CGM.getLLVMContext()), |
| 1062 | Args, /*isVarArg=*/false); |
| 1063 | |
| 1064 | return CGM.CreateRuntimeFunction(FTy, "__cxa_guard_abort"); |
| 1065 | } |
| 1066 | |
| 1067 | namespace { |
| 1068 | struct CallGuardAbort : EHScopeStack::Cleanup { |
| 1069 | llvm::GlobalVariable *Guard; |
| 1070 | CallGuardAbort(llvm::GlobalVariable *Guard) : Guard(Guard) {} |
| 1071 | |
| 1072 | void Emit(CodeGenFunction &CGF, bool IsForEH) { |
| 1073 | CGF.Builder.CreateCall(getGuardAbortFn(CGF.CGM, Guard->getType()), Guard) |
| 1074 | ->setDoesNotThrow(); |
| 1075 | } |
| 1076 | }; |
| 1077 | } |
| 1078 | |
| 1079 | /// The ARM code here follows the Itanium code closely enough that we |
| 1080 | /// just special-case it at particular places. |
John McCall | 3030eb8 | 2010-11-06 09:44:32 +0000 | [diff] [blame^] | 1081 | void ItaniumCXXABI::EmitGuardedInit(CodeGenFunction &CGF, |
| 1082 | const VarDecl &D, |
| 1083 | llvm::GlobalVariable *GV) { |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1084 | CGBuilderTy &Builder = CGF.Builder; |
John McCall | 3030eb8 | 2010-11-06 09:44:32 +0000 | [diff] [blame^] | 1085 | |
| 1086 | // We only need to use thread-safe statics for local variables; |
| 1087 | // global initialization is always single-threaded. |
| 1088 | bool ThreadsafeStatics = (getContext().getLangOptions().ThreadsafeStatics && |
| 1089 | D.isLocalVarDecl()); |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1090 | |
| 1091 | // Guard variables are 64 bits in the generic ABI and 32 bits on ARM. |
| 1092 | const llvm::IntegerType *GuardTy |
| 1093 | = (IsARM ? Builder.getInt32Ty() : Builder.getInt64Ty()); |
| 1094 | const llvm::PointerType *GuardPtrTy = GuardTy->getPointerTo(); |
| 1095 | |
| 1096 | // Create the guard variable. |
| 1097 | llvm::SmallString<256> GuardVName; |
| 1098 | getMangleContext().mangleItaniumGuardVariable(&D, GuardVName); |
John McCall | 112c967 | 2010-11-02 21:04:24 +0000 | [diff] [blame] | 1099 | |
John McCall | 3030eb8 | 2010-11-06 09:44:32 +0000 | [diff] [blame^] | 1100 | // Just absorb linkage and visibility from the variable. |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1101 | llvm::GlobalVariable *GuardVariable = |
| 1102 | new llvm::GlobalVariable(CGM.getModule(), GuardTy, |
John McCall | 3030eb8 | 2010-11-06 09:44:32 +0000 | [diff] [blame^] | 1103 | false, GV->getLinkage(), |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1104 | llvm::ConstantInt::get(GuardTy, 0), |
| 1105 | GuardVName.str()); |
John McCall | 112c967 | 2010-11-02 21:04:24 +0000 | [diff] [blame] | 1106 | GuardVariable->setVisibility(GV->getVisibility()); |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1107 | |
| 1108 | // Test whether the variable has completed initialization. |
| 1109 | llvm::Value *IsInitialized; |
| 1110 | |
| 1111 | // ARM C++ ABI 3.2.3.1: |
| 1112 | // To support the potential use of initialization guard variables |
| 1113 | // as semaphores that are the target of ARM SWP and LDREX/STREX |
| 1114 | // synchronizing instructions we define a static initialization |
| 1115 | // guard variable to be a 4-byte aligned, 4- byte word with the |
| 1116 | // following inline access protocol. |
| 1117 | // #define INITIALIZED 1 |
| 1118 | // if ((obj_guard & INITIALIZED) != INITIALIZED) { |
| 1119 | // if (__cxa_guard_acquire(&obj_guard)) |
| 1120 | // ... |
| 1121 | // } |
| 1122 | if (IsARM) { |
| 1123 | llvm::Value *V = Builder.CreateLoad(GuardVariable); |
| 1124 | V = Builder.CreateAnd(V, Builder.getInt32(1)); |
| 1125 | IsInitialized = Builder.CreateIsNull(V, "guard.uninitialized"); |
| 1126 | |
| 1127 | // Itanium C++ ABI 3.3.2: |
| 1128 | // The following is pseudo-code showing how these functions can be used: |
| 1129 | // if (obj_guard.first_byte == 0) { |
| 1130 | // if ( __cxa_guard_acquire (&obj_guard) ) { |
| 1131 | // try { |
| 1132 | // ... initialize the object ...; |
| 1133 | // } catch (...) { |
| 1134 | // __cxa_guard_abort (&obj_guard); |
| 1135 | // throw; |
| 1136 | // } |
| 1137 | // ... queue object destructor with __cxa_atexit() ...; |
| 1138 | // __cxa_guard_release (&obj_guard); |
| 1139 | // } |
| 1140 | // } |
| 1141 | } else { |
| 1142 | // Load the first byte of the guard variable. |
| 1143 | const llvm::Type *PtrTy = Builder.getInt8PtrTy(); |
| 1144 | llvm::Value *V = |
| 1145 | Builder.CreateLoad(Builder.CreateBitCast(GuardVariable, PtrTy), "tmp"); |
| 1146 | |
| 1147 | IsInitialized = Builder.CreateIsNull(V, "guard.uninitialized"); |
| 1148 | } |
| 1149 | |
| 1150 | llvm::BasicBlock *InitCheckBlock = CGF.createBasicBlock("init.check"); |
| 1151 | llvm::BasicBlock *EndBlock = CGF.createBasicBlock("init.end"); |
| 1152 | |
| 1153 | // Check if the first byte of the guard variable is zero. |
| 1154 | Builder.CreateCondBr(IsInitialized, InitCheckBlock, EndBlock); |
| 1155 | |
| 1156 | CGF.EmitBlock(InitCheckBlock); |
| 1157 | |
| 1158 | // Variables used when coping with thread-safe statics and exceptions. |
| 1159 | if (ThreadsafeStatics) { |
| 1160 | // Call __cxa_guard_acquire. |
| 1161 | llvm::Value *V |
| 1162 | = Builder.CreateCall(getGuardAcquireFn(CGM, GuardPtrTy), GuardVariable); |
| 1163 | |
| 1164 | llvm::BasicBlock *InitBlock = CGF.createBasicBlock("init"); |
| 1165 | |
| 1166 | Builder.CreateCondBr(Builder.CreateIsNotNull(V, "tobool"), |
| 1167 | InitBlock, EndBlock); |
| 1168 | |
| 1169 | // Call __cxa_guard_abort along the exceptional edge. |
| 1170 | CGF.EHStack.pushCleanup<CallGuardAbort>(EHCleanup, GuardVariable); |
| 1171 | |
| 1172 | CGF.EmitBlock(InitBlock); |
| 1173 | } |
| 1174 | |
| 1175 | // Emit the initializer and add a global destructor if appropriate. |
| 1176 | CGF.EmitCXXGlobalVarDeclInit(D, GV); |
| 1177 | |
| 1178 | if (ThreadsafeStatics) { |
| 1179 | // Pop the guard-abort cleanup if we pushed one. |
| 1180 | CGF.PopCleanupBlock(); |
| 1181 | |
| 1182 | // Call __cxa_guard_release. This cannot throw. |
| 1183 | Builder.CreateCall(getGuardReleaseFn(CGM, GuardPtrTy), GuardVariable); |
| 1184 | } else { |
| 1185 | Builder.CreateStore(llvm::ConstantInt::get(GuardTy, 1), GuardVariable); |
| 1186 | } |
| 1187 | |
| 1188 | CGF.EmitBlock(EndBlock); |
| 1189 | } |