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) { |
| 44 | QualType T = CGM.getContext().getPointerDiffType(); |
| 45 | const llvm::Type *Ty = CGM.getTypes().ConvertTypeRecursive(T); |
| 46 | PtrDiffTy = cast<llvm::IntegerType>(Ty); |
| 47 | } |
| 48 | return PtrDiffTy; |
| 49 | } |
| 50 | |
Charles Davis | 3a811f1 | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 51 | public: |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 52 | ItaniumCXXABI(CodeGen::CodeGenModule &CGM, bool IsARM = false) : |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 53 | CGCXXABI(CGM), PtrDiffTy(0), MangleCtx(CGM.getContext(), CGM.getDiags()), |
| 54 | IsARM(IsARM) { } |
Charles Davis | 3a811f1 | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 55 | |
| 56 | CodeGen::MangleContext &getMangleContext() { |
| 57 | return MangleCtx; |
| 58 | } |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 59 | |
John McCall | f16aa10 | 2010-08-22 21:01:12 +0000 | [diff] [blame] | 60 | bool isZeroInitializable(const MemberPointerType *MPT); |
John McCall | cf2c85e | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 61 | |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 62 | const llvm::Type *ConvertMemberPointerType(const MemberPointerType *MPT); |
| 63 | |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 64 | llvm::Value *EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF, |
| 65 | llvm::Value *&This, |
| 66 | llvm::Value *MemFnPtr, |
| 67 | const MemberPointerType *MPT); |
John McCall | 3023def | 2010-08-22 03:04:22 +0000 | [diff] [blame] | 68 | |
John McCall | 6c2ab1d | 2010-08-31 21:07:20 +0000 | [diff] [blame^] | 69 | llvm::Value *EmitMemberDataPointerAddress(CodeGenFunction &CGF, |
| 70 | llvm::Value *Base, |
| 71 | llvm::Value *MemPtr, |
| 72 | const MemberPointerType *MPT); |
| 73 | |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 74 | llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF, |
| 75 | const CastExpr *E, |
| 76 | llvm::Value *Src); |
John McCall | cf2c85e | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 77 | |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 78 | llvm::Constant *EmitMemberPointerConversion(llvm::Constant *C, |
| 79 | const CastExpr *E); |
John McCall | cf2c85e | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 80 | |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 81 | llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT); |
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 *EmitMemberPointer(const CXXMethodDecl *MD); |
| 84 | llvm::Constant *EmitMemberPointer(const FieldDecl *FD); |
John McCall | 875ab10 | 2010-08-22 06:43:33 +0000 | [diff] [blame] | 85 | |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 86 | llvm::Value *EmitMemberPointerComparison(CodeGenFunction &CGF, |
| 87 | llvm::Value *L, |
| 88 | llvm::Value *R, |
| 89 | const MemberPointerType *MPT, |
| 90 | bool Inequality); |
John McCall | e9fd7eb | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 91 | |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 92 | llvm::Value *EmitMemberPointerIsNotNull(CodeGenFunction &CGF, |
| 93 | llvm::Value *Addr, |
| 94 | const MemberPointerType *MPT); |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 95 | |
| 96 | void BuildConstructorSignature(const CXXConstructorDecl *Ctor, |
| 97 | CXXCtorType T, |
| 98 | CanQualType &ResTy, |
| 99 | llvm::SmallVectorImpl<CanQualType> &ArgTys); |
| 100 | |
| 101 | void BuildDestructorSignature(const CXXDestructorDecl *Dtor, |
| 102 | CXXDtorType T, |
| 103 | CanQualType &ResTy, |
| 104 | llvm::SmallVectorImpl<CanQualType> &ArgTys); |
| 105 | |
| 106 | void BuildInstanceFunctionParams(CodeGenFunction &CGF, |
| 107 | QualType &ResTy, |
| 108 | FunctionArgList &Params); |
| 109 | |
| 110 | void EmitInstanceFunctionProlog(CodeGenFunction &CGF); |
Charles Davis | 3a811f1 | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 111 | }; |
John McCall | ee79a4c | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 112 | |
| 113 | class ARMCXXABI : public ItaniumCXXABI { |
| 114 | public: |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 115 | ARMCXXABI(CodeGen::CodeGenModule &CGM) : ItaniumCXXABI(CGM, /*ARM*/ true) {} |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 116 | |
| 117 | void BuildConstructorSignature(const CXXConstructorDecl *Ctor, |
| 118 | CXXCtorType T, |
| 119 | CanQualType &ResTy, |
| 120 | llvm::SmallVectorImpl<CanQualType> &ArgTys); |
| 121 | |
| 122 | void BuildDestructorSignature(const CXXDestructorDecl *Dtor, |
| 123 | CXXDtorType T, |
| 124 | CanQualType &ResTy, |
| 125 | llvm::SmallVectorImpl<CanQualType> &ArgTys); |
| 126 | |
| 127 | void BuildInstanceFunctionParams(CodeGenFunction &CGF, |
| 128 | QualType &ResTy, |
| 129 | FunctionArgList &Params); |
| 130 | |
| 131 | void EmitInstanceFunctionProlog(CodeGenFunction &CGF); |
| 132 | |
| 133 | void EmitReturnFromThunk(CodeGenFunction &CGF, RValue RV, QualType ResTy); |
| 134 | |
| 135 | |
| 136 | private: |
| 137 | /// \brief Returns true if the given instance method is one of the |
| 138 | /// kinds that the ARM ABI says returns 'this'. |
| 139 | static bool HasThisReturn(GlobalDecl GD) { |
| 140 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
| 141 | return ((isa<CXXDestructorDecl>(MD) && GD.getDtorType() != Dtor_Deleting) || |
| 142 | (isa<CXXConstructorDecl>(MD))); |
| 143 | } |
John McCall | ee79a4c | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 144 | }; |
Charles Davis | 3a811f1 | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 147 | CodeGen::CGCXXABI *CodeGen::CreateItaniumCXXABI(CodeGenModule &CGM) { |
Charles Davis | 3a811f1 | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 148 | return new ItaniumCXXABI(CGM); |
| 149 | } |
| 150 | |
John McCall | ee79a4c | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 151 | CodeGen::CGCXXABI *CodeGen::CreateARMCXXABI(CodeGenModule &CGM) { |
| 152 | return new ARMCXXABI(CGM); |
| 153 | } |
| 154 | |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 155 | const llvm::Type * |
| 156 | ItaniumCXXABI::ConvertMemberPointerType(const MemberPointerType *MPT) { |
| 157 | if (MPT->isMemberDataPointer()) |
| 158 | return getPtrDiffTy(); |
| 159 | else |
| 160 | return llvm::StructType::get(CGM.getLLVMContext(), |
| 161 | getPtrDiffTy(), getPtrDiffTy(), NULL); |
John McCall | 875ab10 | 2010-08-22 06:43:33 +0000 | [diff] [blame] | 162 | } |
| 163 | |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 164 | /// In the Itanium and ARM ABIs, method pointers have the form: |
| 165 | /// struct { ptrdiff_t ptr; ptrdiff_t adj; } memptr; |
| 166 | /// |
| 167 | /// In the Itanium ABI: |
| 168 | /// - method pointers are virtual if (memptr.ptr & 1) is nonzero |
| 169 | /// - the this-adjustment is (memptr.adj) |
| 170 | /// - the virtual offset is (memptr.ptr - 1) |
| 171 | /// |
| 172 | /// In the ARM ABI: |
| 173 | /// - method pointers are virtual if (memptr.adj & 1) is nonzero |
| 174 | /// - the this-adjustment is (memptr.adj >> 1) |
| 175 | /// - the virtual offset is (memptr.ptr) |
| 176 | /// ARM uses 'adj' for the virtual flag because Thumb functions |
| 177 | /// may be only single-byte aligned. |
| 178 | /// |
| 179 | /// If the member is virtual, the adjusted 'this' pointer points |
| 180 | /// to a vtable pointer from which the virtual offset is applied. |
| 181 | /// |
| 182 | /// If the member is non-virtual, memptr.ptr is the address of |
| 183 | /// the function to call. |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 184 | llvm::Value * |
| 185 | ItaniumCXXABI::EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF, |
| 186 | llvm::Value *&This, |
| 187 | llvm::Value *MemFnPtr, |
| 188 | const MemberPointerType *MPT) { |
| 189 | CGBuilderTy &Builder = CGF.Builder; |
| 190 | |
| 191 | const FunctionProtoType *FPT = |
| 192 | MPT->getPointeeType()->getAs<FunctionProtoType>(); |
| 193 | const CXXRecordDecl *RD = |
| 194 | cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl()); |
| 195 | |
| 196 | const llvm::FunctionType *FTy = |
| 197 | CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(RD, FPT), |
| 198 | FPT->isVariadic()); |
| 199 | |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 200 | const llvm::IntegerType *ptrdiff = getPtrDiffTy(); |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 201 | llvm::Constant *ptrdiff_1 = llvm::ConstantInt::get(ptrdiff, 1); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 202 | |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 203 | llvm::BasicBlock *FnVirtual = CGF.createBasicBlock("memptr.virtual"); |
| 204 | llvm::BasicBlock *FnNonVirtual = CGF.createBasicBlock("memptr.nonvirtual"); |
| 205 | llvm::BasicBlock *FnEnd = CGF.createBasicBlock("memptr.end"); |
| 206 | |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 207 | // Extract memptr.adj, which is in the second field. |
| 208 | llvm::Value *RawAdj = Builder.CreateExtractValue(MemFnPtr, 1, "memptr.adj"); |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 209 | |
| 210 | // Compute the true adjustment. |
| 211 | llvm::Value *Adj = RawAdj; |
| 212 | if (IsARM) |
| 213 | Adj = Builder.CreateAShr(Adj, ptrdiff_1, "memptr.adj.shifted"); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 214 | |
| 215 | // Apply the adjustment and cast back to the original struct type |
| 216 | // for consistency. |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 217 | llvm::Value *Ptr = Builder.CreateBitCast(This, Builder.getInt8PtrTy()); |
| 218 | Ptr = Builder.CreateInBoundsGEP(Ptr, Adj); |
| 219 | This = Builder.CreateBitCast(Ptr, This->getType(), "this.adjusted"); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 220 | |
| 221 | // Load the function pointer. |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 222 | llvm::Value *FnAsInt = Builder.CreateExtractValue(MemFnPtr, 0, "memptr.ptr"); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 223 | |
| 224 | // If the LSB in the function pointer is 1, the function pointer points to |
| 225 | // a virtual function. |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 226 | llvm::Value *IsVirtual; |
| 227 | if (IsARM) |
| 228 | IsVirtual = Builder.CreateAnd(RawAdj, ptrdiff_1); |
| 229 | else |
| 230 | IsVirtual = Builder.CreateAnd(FnAsInt, ptrdiff_1); |
| 231 | IsVirtual = Builder.CreateIsNotNull(IsVirtual, "memptr.isvirtual"); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 232 | Builder.CreateCondBr(IsVirtual, FnVirtual, FnNonVirtual); |
| 233 | |
| 234 | // In the virtual path, the adjustment left 'This' pointing to the |
| 235 | // vtable of the correct base subobject. The "function pointer" is an |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 236 | // offset within the vtable (+1 for the virtual flag on non-ARM). |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 237 | CGF.EmitBlock(FnVirtual); |
| 238 | |
| 239 | // Cast the adjusted this to a pointer to vtable pointer and load. |
| 240 | const llvm::Type *VTableTy = Builder.getInt8PtrTy(); |
| 241 | llvm::Value *VTable = Builder.CreateBitCast(This, VTableTy->getPointerTo()); |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 242 | VTable = Builder.CreateLoad(VTable, "memptr.vtable"); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 243 | |
| 244 | // Apply the offset. |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 245 | llvm::Value *VTableOffset = FnAsInt; |
| 246 | if (!IsARM) VTableOffset = Builder.CreateSub(VTableOffset, ptrdiff_1); |
| 247 | VTable = Builder.CreateGEP(VTable, VTableOffset); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 248 | |
| 249 | // Load the virtual function to call. |
| 250 | VTable = Builder.CreateBitCast(VTable, FTy->getPointerTo()->getPointerTo()); |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 251 | llvm::Value *VirtualFn = Builder.CreateLoad(VTable, "memptr.virtualfn"); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 252 | CGF.EmitBranch(FnEnd); |
| 253 | |
| 254 | // In the non-virtual path, the function pointer is actually a |
| 255 | // function pointer. |
| 256 | CGF.EmitBlock(FnNonVirtual); |
| 257 | llvm::Value *NonVirtualFn = |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 258 | Builder.CreateIntToPtr(FnAsInt, FTy->getPointerTo(), "memptr.nonvirtualfn"); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 259 | |
| 260 | // We're done. |
| 261 | CGF.EmitBlock(FnEnd); |
| 262 | llvm::PHINode *Callee = Builder.CreatePHI(FTy->getPointerTo()); |
| 263 | Callee->reserveOperandSpace(2); |
| 264 | Callee->addIncoming(VirtualFn, FnVirtual); |
| 265 | Callee->addIncoming(NonVirtualFn, FnNonVirtual); |
| 266 | return Callee; |
| 267 | } |
John McCall | 3023def | 2010-08-22 03:04:22 +0000 | [diff] [blame] | 268 | |
John McCall | 6c2ab1d | 2010-08-31 21:07:20 +0000 | [diff] [blame^] | 269 | /// Compute an l-value by applying the given pointer-to-member to a |
| 270 | /// base object. |
| 271 | llvm::Value *ItaniumCXXABI::EmitMemberDataPointerAddress(CodeGenFunction &CGF, |
| 272 | llvm::Value *Base, |
| 273 | llvm::Value *MemPtr, |
| 274 | const MemberPointerType *MPT) { |
| 275 | assert(MemPtr->getType() == getPtrDiffTy()); |
| 276 | |
| 277 | CGBuilderTy &Builder = CGF.Builder; |
| 278 | |
| 279 | unsigned AS = cast<llvm::PointerType>(Base->getType())->getAddressSpace(); |
| 280 | |
| 281 | // Cast to char*. |
| 282 | Base = Builder.CreateBitCast(Base, Builder.getInt8Ty()->getPointerTo(AS)); |
| 283 | |
| 284 | // Apply the offset, which we assume is non-null. |
| 285 | llvm::Value *Addr = Builder.CreateInBoundsGEP(Base, MemPtr, "memptr.offset"); |
| 286 | |
| 287 | // Cast the address to the appropriate pointer type, adopting the |
| 288 | // address space of the base pointer. |
| 289 | const llvm::Type *PType |
| 290 | = CGF.ConvertType(MPT->getPointeeType())->getPointerTo(AS); |
| 291 | return Builder.CreateBitCast(Addr, PType); |
| 292 | } |
| 293 | |
John McCall | 3023def | 2010-08-22 03:04:22 +0000 | [diff] [blame] | 294 | /// Perform a derived-to-base or base-to-derived member pointer conversion. |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 295 | /// |
| 296 | /// Obligatory offset/adjustment diagram: |
| 297 | /// <-- offset --> <-- adjustment --> |
| 298 | /// |--------------------------|----------------------|--------------------| |
| 299 | /// ^Derived address point ^Base address point ^Member address point |
| 300 | /// |
| 301 | /// So when converting a base member pointer to a derived member pointer, |
| 302 | /// we add the offset to the adjustment because the address point has |
| 303 | /// decreased; and conversely, when converting a derived MP to a base MP |
| 304 | /// we subtract the offset from the adjustment because the address point |
| 305 | /// has increased. |
| 306 | /// |
| 307 | /// The standard forbids (at compile time) conversion to and from |
| 308 | /// virtual bases, which is why we don't have to consider them here. |
| 309 | /// |
| 310 | /// The standard forbids (at run time) casting a derived MP to a base |
| 311 | /// MP when the derived MP does not point to a member of the base. |
| 312 | /// This is why -1 is a reasonable choice for null data member |
| 313 | /// pointers. |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 314 | llvm::Value * |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 315 | ItaniumCXXABI::EmitMemberPointerConversion(CodeGenFunction &CGF, |
| 316 | const CastExpr *E, |
| 317 | llvm::Value *Src) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 318 | assert(E->getCastKind() == CK_DerivedToBaseMemberPointer || |
| 319 | E->getCastKind() == CK_BaseToDerivedMemberPointer); |
John McCall | 3023def | 2010-08-22 03:04:22 +0000 | [diff] [blame] | 320 | |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 321 | if (isa<llvm::Constant>(Src)) |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 322 | return EmitMemberPointerConversion(cast<llvm::Constant>(Src), E); |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 323 | |
John McCall | 3023def | 2010-08-22 03:04:22 +0000 | [diff] [blame] | 324 | CGBuilderTy &Builder = CGF.Builder; |
| 325 | |
| 326 | const MemberPointerType *SrcTy = |
| 327 | E->getSubExpr()->getType()->getAs<MemberPointerType>(); |
| 328 | const MemberPointerType *DestTy = E->getType()->getAs<MemberPointerType>(); |
| 329 | |
| 330 | const CXXRecordDecl *SrcDecl = SrcTy->getClass()->getAsCXXRecordDecl(); |
| 331 | const CXXRecordDecl *DestDecl = DestTy->getClass()->getAsCXXRecordDecl(); |
| 332 | |
John McCall | 3023def | 2010-08-22 03:04:22 +0000 | [diff] [blame] | 333 | bool DerivedToBase = |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 334 | E->getCastKind() == CK_DerivedToBaseMemberPointer; |
John McCall | 3023def | 2010-08-22 03:04:22 +0000 | [diff] [blame] | 335 | |
| 336 | const CXXRecordDecl *BaseDecl, *DerivedDecl; |
| 337 | if (DerivedToBase) |
| 338 | DerivedDecl = SrcDecl, BaseDecl = DestDecl; |
| 339 | else |
| 340 | BaseDecl = SrcDecl, DerivedDecl = DestDecl; |
| 341 | |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 342 | llvm::Constant *Adj = |
| 343 | CGF.CGM.GetNonVirtualBaseClassOffset(DerivedDecl, |
| 344 | E->path_begin(), |
| 345 | E->path_end()); |
| 346 | if (!Adj) return Src; |
John McCall | 875ab10 | 2010-08-22 06:43:33 +0000 | [diff] [blame] | 347 | |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 348 | // For member data pointers, this is just a matter of adding the |
| 349 | // offset if the source is non-null. |
| 350 | if (SrcTy->isMemberDataPointer()) { |
| 351 | llvm::Value *Dst; |
| 352 | if (DerivedToBase) |
| 353 | Dst = Builder.CreateNSWSub(Src, Adj, "adj"); |
| 354 | else |
| 355 | Dst = Builder.CreateNSWAdd(Src, Adj, "adj"); |
| 356 | |
| 357 | // Null check. |
| 358 | llvm::Value *Null = llvm::Constant::getAllOnesValue(Src->getType()); |
| 359 | llvm::Value *IsNull = Builder.CreateICmpEQ(Src, Null, "memptr.isnull"); |
| 360 | return Builder.CreateSelect(IsNull, Src, Dst); |
| 361 | } |
| 362 | |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 363 | // The this-adjustment is left-shifted by 1 on ARM. |
| 364 | if (IsARM) { |
| 365 | uint64_t Offset = cast<llvm::ConstantInt>(Adj)->getZExtValue(); |
| 366 | Offset <<= 1; |
| 367 | Adj = llvm::ConstantInt::get(Adj->getType(), Offset); |
| 368 | } |
| 369 | |
John McCall | e14add4 | 2010-08-22 11:04:31 +0000 | [diff] [blame] | 370 | llvm::Value *SrcAdj = Builder.CreateExtractValue(Src, 1, "src.adj"); |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 371 | llvm::Value *DstAdj; |
| 372 | if (DerivedToBase) |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 373 | DstAdj = Builder.CreateNSWSub(SrcAdj, Adj, "adj"); |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 374 | else |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 375 | DstAdj = Builder.CreateNSWAdd(SrcAdj, Adj, "adj"); |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 376 | |
John McCall | e14add4 | 2010-08-22 11:04:31 +0000 | [diff] [blame] | 377 | return Builder.CreateInsertValue(Src, DstAdj, 1); |
John McCall | 3023def | 2010-08-22 03:04:22 +0000 | [diff] [blame] | 378 | } |
John McCall | cf2c85e | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 379 | |
| 380 | llvm::Constant * |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 381 | ItaniumCXXABI::EmitMemberPointerConversion(llvm::Constant *C, |
| 382 | const CastExpr *E) { |
John McCall | cf2c85e | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 383 | const MemberPointerType *SrcTy = |
| 384 | E->getSubExpr()->getType()->getAs<MemberPointerType>(); |
| 385 | const MemberPointerType *DestTy = |
| 386 | E->getType()->getAs<MemberPointerType>(); |
| 387 | |
| 388 | bool DerivedToBase = |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 389 | E->getCastKind() == CK_DerivedToBaseMemberPointer; |
John McCall | cf2c85e | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 390 | |
| 391 | const CXXRecordDecl *DerivedDecl; |
| 392 | if (DerivedToBase) |
| 393 | DerivedDecl = SrcTy->getClass()->getAsCXXRecordDecl(); |
| 394 | else |
| 395 | DerivedDecl = DestTy->getClass()->getAsCXXRecordDecl(); |
| 396 | |
| 397 | // Calculate the offset to the base class. |
| 398 | llvm::Constant *Offset = |
| 399 | CGM.GetNonVirtualBaseClassOffset(DerivedDecl, |
| 400 | E->path_begin(), |
| 401 | E->path_end()); |
| 402 | // If there's no offset, we're done. |
| 403 | if (!Offset) return C; |
| 404 | |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 405 | // If the source is a member data pointer, we have to do a null |
| 406 | // check and then add the offset. In the common case, we can fold |
| 407 | // away the offset. |
| 408 | if (SrcTy->isMemberDataPointer()) { |
| 409 | assert(C->getType() == getPtrDiffTy()); |
| 410 | |
| 411 | // If it's a constant int, just create a new constant int. |
| 412 | if (llvm::ConstantInt *CI = dyn_cast<llvm::ConstantInt>(C)) { |
| 413 | int64_t Src = CI->getSExtValue(); |
| 414 | |
| 415 | // Null converts to null. |
| 416 | if (Src == -1) return CI; |
| 417 | |
| 418 | // Otherwise, just add the offset. |
| 419 | int64_t OffsetV = cast<llvm::ConstantInt>(Offset)->getSExtValue(); |
| 420 | int64_t Dst = (DerivedToBase ? Src - OffsetV : Src + OffsetV); |
| 421 | return llvm::ConstantInt::get(CI->getType(), Dst, /*signed*/ true); |
| 422 | } |
| 423 | |
| 424 | // Otherwise, we have to form a constant select expression. |
| 425 | llvm::Constant *Null = llvm::Constant::getAllOnesValue(C->getType()); |
| 426 | |
| 427 | llvm::Constant *IsNull = |
| 428 | llvm::ConstantExpr::getICmp(llvm::ICmpInst::ICMP_EQ, C, Null); |
| 429 | |
| 430 | llvm::Constant *Dst; |
| 431 | if (DerivedToBase) |
| 432 | Dst = llvm::ConstantExpr::getNSWSub(C, Offset); |
| 433 | else |
| 434 | Dst = llvm::ConstantExpr::getNSWAdd(C, Offset); |
| 435 | |
| 436 | return llvm::ConstantExpr::getSelect(IsNull, Null, Dst); |
| 437 | } |
| 438 | |
John McCall | 875ab10 | 2010-08-22 06:43:33 +0000 | [diff] [blame] | 439 | // The this-adjustment is left-shifted by 1 on ARM. |
| 440 | if (IsARM) { |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 441 | int64_t OffsetV = cast<llvm::ConstantInt>(Offset)->getSExtValue(); |
John McCall | 875ab10 | 2010-08-22 06:43:33 +0000 | [diff] [blame] | 442 | OffsetV <<= 1; |
| 443 | Offset = llvm::ConstantInt::get(Offset->getType(), OffsetV); |
| 444 | } |
| 445 | |
John McCall | cf2c85e | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 446 | llvm::ConstantStruct *CS = cast<llvm::ConstantStruct>(C); |
| 447 | |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 448 | llvm::Constant *Values[2] = { CS->getOperand(0), 0 }; |
| 449 | if (DerivedToBase) |
| 450 | Values[1] = llvm::ConstantExpr::getSub(CS->getOperand(1), Offset); |
| 451 | else |
| 452 | Values[1] = llvm::ConstantExpr::getAdd(CS->getOperand(1), Offset); |
| 453 | |
John McCall | cf2c85e | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 454 | return llvm::ConstantStruct::get(CGM.getLLVMContext(), Values, 2, |
| 455 | /*Packed=*/false); |
| 456 | } |
| 457 | |
| 458 | |
John McCall | cf2c85e | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 459 | llvm::Constant * |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 460 | ItaniumCXXABI::EmitNullMemberPointer(const MemberPointerType *MPT) { |
| 461 | const llvm::Type *ptrdiff_t = getPtrDiffTy(); |
| 462 | |
| 463 | // Itanium C++ ABI 2.3: |
| 464 | // A NULL pointer is represented as -1. |
| 465 | if (MPT->isMemberDataPointer()) |
| 466 | return llvm::ConstantInt::get(ptrdiff_t, -1ULL, /*isSigned=*/true); |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 467 | |
| 468 | llvm::Constant *Zero = llvm::ConstantInt::get(ptrdiff_t, 0); |
| 469 | llvm::Constant *Values[2] = { Zero, Zero }; |
| 470 | return llvm::ConstantStruct::get(CGM.getLLVMContext(), Values, 2, |
| 471 | /*Packed=*/false); |
John McCall | cf2c85e | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 472 | } |
| 473 | |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 474 | llvm::Constant *ItaniumCXXABI::EmitMemberPointer(const FieldDecl *FD) { |
| 475 | // Itanium C++ ABI 2.3: |
| 476 | // A pointer to data member is an offset from the base address of |
| 477 | // the class object containing it, represented as a ptrdiff_t |
| 478 | |
| 479 | QualType ClassType = CGM.getContext().getTypeDeclType(FD->getParent()); |
| 480 | const llvm::StructType *ClassLTy = |
| 481 | cast<llvm::StructType>(CGM.getTypes().ConvertType(ClassType)); |
| 482 | |
| 483 | const CGRecordLayout &RL = CGM.getTypes().getCGRecordLayout(FD->getParent()); |
| 484 | unsigned FieldNo = RL.getLLVMFieldNo(FD); |
| 485 | uint64_t Offset = |
| 486 | CGM.getTargetData().getStructLayout(ClassLTy)->getElementOffset(FieldNo); |
| 487 | |
| 488 | return llvm::ConstantInt::get(getPtrDiffTy(), Offset); |
| 489 | } |
| 490 | |
| 491 | llvm::Constant *ItaniumCXXABI::EmitMemberPointer(const CXXMethodDecl *MD) { |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 492 | assert(MD->isInstance() && "Member function must not be static!"); |
| 493 | MD = MD->getCanonicalDecl(); |
| 494 | |
| 495 | CodeGenTypes &Types = CGM.getTypes(); |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 496 | const llvm::Type *ptrdiff_t = getPtrDiffTy(); |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 497 | |
| 498 | // Get the function pointer (or index if this is a virtual function). |
| 499 | llvm::Constant *MemPtr[2]; |
| 500 | if (MD->isVirtual()) { |
| 501 | uint64_t Index = CGM.getVTables().getMethodVTableIndex(MD); |
| 502 | |
| 503 | // FIXME: We shouldn't use / 8 here. |
| 504 | uint64_t PointerWidthInBytes = |
| 505 | CGM.getContext().Target.getPointerWidth(0) / 8; |
| 506 | uint64_t VTableOffset = (Index * PointerWidthInBytes); |
| 507 | |
| 508 | if (IsARM) { |
| 509 | // ARM C++ ABI 3.2.1: |
| 510 | // This ABI specifies that adj contains twice the this |
| 511 | // adjustment, plus 1 if the member function is virtual. The |
| 512 | // least significant bit of adj then makes exactly the same |
| 513 | // discrimination as the least significant bit of ptr does for |
| 514 | // Itanium. |
| 515 | MemPtr[0] = llvm::ConstantInt::get(ptrdiff_t, VTableOffset); |
| 516 | MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t, 1); |
| 517 | } else { |
| 518 | // Itanium C++ ABI 2.3: |
| 519 | // For a virtual function, [the pointer field] is 1 plus the |
| 520 | // virtual table offset (in bytes) of the function, |
| 521 | // represented as a ptrdiff_t. |
| 522 | MemPtr[0] = llvm::ConstantInt::get(ptrdiff_t, VTableOffset + 1); |
| 523 | MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t, 0); |
| 524 | } |
| 525 | } else { |
| 526 | const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); |
| 527 | const llvm::Type *Ty; |
| 528 | // Check whether the function has a computable LLVM signature. |
| 529 | if (!CodeGenTypes::VerifyFuncTypeComplete(FPT)) { |
| 530 | // The function has a computable LLVM signature; use the correct type. |
| 531 | Ty = Types.GetFunctionType(Types.getFunctionInfo(MD), FPT->isVariadic()); |
| 532 | } else { |
| 533 | // Use an arbitrary non-function type to tell GetAddrOfFunction that the |
| 534 | // function type is incomplete. |
| 535 | Ty = ptrdiff_t; |
| 536 | } |
| 537 | |
| 538 | llvm::Constant *Addr = CGM.GetAddrOfFunction(MD, Ty); |
| 539 | MemPtr[0] = llvm::ConstantExpr::getPtrToInt(Addr, ptrdiff_t); |
| 540 | MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t, 0); |
| 541 | } |
John McCall | 875ab10 | 2010-08-22 06:43:33 +0000 | [diff] [blame] | 542 | |
| 543 | return llvm::ConstantStruct::get(CGM.getLLVMContext(), |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 544 | MemPtr, 2, /*Packed=*/false); |
John McCall | 875ab10 | 2010-08-22 06:43:33 +0000 | [diff] [blame] | 545 | } |
| 546 | |
John McCall | e9fd7eb | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 547 | /// The comparison algorithm is pretty easy: the member pointers are |
| 548 | /// the same if they're either bitwise identical *or* both null. |
| 549 | /// |
| 550 | /// ARM is different here only because null-ness is more complicated. |
| 551 | llvm::Value * |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 552 | ItaniumCXXABI::EmitMemberPointerComparison(CodeGenFunction &CGF, |
| 553 | llvm::Value *L, |
| 554 | llvm::Value *R, |
| 555 | const MemberPointerType *MPT, |
| 556 | bool Inequality) { |
John McCall | e9fd7eb | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 557 | CGBuilderTy &Builder = CGF.Builder; |
| 558 | |
John McCall | e9fd7eb | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 559 | llvm::ICmpInst::Predicate Eq; |
| 560 | llvm::Instruction::BinaryOps And, Or; |
| 561 | if (Inequality) { |
| 562 | Eq = llvm::ICmpInst::ICMP_NE; |
| 563 | And = llvm::Instruction::Or; |
| 564 | Or = llvm::Instruction::And; |
| 565 | } else { |
| 566 | Eq = llvm::ICmpInst::ICMP_EQ; |
| 567 | And = llvm::Instruction::And; |
| 568 | Or = llvm::Instruction::Or; |
| 569 | } |
| 570 | |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 571 | // Member data pointers are easy because there's a unique null |
| 572 | // value, so it just comes down to bitwise equality. |
| 573 | if (MPT->isMemberDataPointer()) |
| 574 | return Builder.CreateICmp(Eq, L, R); |
| 575 | |
| 576 | // For member function pointers, the tautologies are more complex. |
| 577 | // The Itanium tautology is: |
John McCall | de719f7 | 2010-08-23 06:56:36 +0000 | [diff] [blame] | 578 | // (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] | 579 | // The ARM tautology is: |
John McCall | de719f7 | 2010-08-23 06:56:36 +0000 | [diff] [blame] | 580 | // (L == R) <==> (L.ptr == R.ptr && |
| 581 | // (L.adj == R.adj || |
| 582 | // (L.ptr == 0 && ((L.adj|R.adj) & 1) == 0))) |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 583 | // The inequality tautologies have exactly the same structure, except |
| 584 | // applying De Morgan's laws. |
| 585 | |
| 586 | llvm::Value *LPtr = Builder.CreateExtractValue(L, 0, "lhs.memptr.ptr"); |
| 587 | llvm::Value *RPtr = Builder.CreateExtractValue(R, 0, "rhs.memptr.ptr"); |
| 588 | |
John McCall | e9fd7eb | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 589 | // This condition tests whether L.ptr == R.ptr. This must always be |
| 590 | // true for equality to hold. |
| 591 | llvm::Value *PtrEq = Builder.CreateICmp(Eq, LPtr, RPtr, "cmp.ptr"); |
| 592 | |
| 593 | // This condition, together with the assumption that L.ptr == R.ptr, |
| 594 | // tests whether the pointers are both null. ARM imposes an extra |
| 595 | // condition. |
| 596 | llvm::Value *Zero = llvm::Constant::getNullValue(LPtr->getType()); |
| 597 | llvm::Value *EqZero = Builder.CreateICmp(Eq, LPtr, Zero, "cmp.ptr.null"); |
| 598 | |
| 599 | // This condition tests whether L.adj == R.adj. If this isn't |
| 600 | // true, the pointers are unequal unless they're both null. |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 601 | llvm::Value *LAdj = Builder.CreateExtractValue(L, 1, "lhs.memptr.adj"); |
| 602 | llvm::Value *RAdj = Builder.CreateExtractValue(R, 1, "rhs.memptr.adj"); |
John McCall | e9fd7eb | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 603 | llvm::Value *AdjEq = Builder.CreateICmp(Eq, LAdj, RAdj, "cmp.adj"); |
| 604 | |
| 605 | // Null member function pointers on ARM clear the low bit of Adj, |
| 606 | // so the zero condition has to check that neither low bit is set. |
| 607 | if (IsARM) { |
| 608 | llvm::Value *One = llvm::ConstantInt::get(LPtr->getType(), 1); |
| 609 | |
| 610 | // Compute (l.adj | r.adj) & 1 and test it against zero. |
| 611 | llvm::Value *OrAdj = Builder.CreateOr(LAdj, RAdj, "or.adj"); |
| 612 | llvm::Value *OrAdjAnd1 = Builder.CreateAnd(OrAdj, One); |
| 613 | llvm::Value *OrAdjAnd1EqZero = Builder.CreateICmp(Eq, OrAdjAnd1, Zero, |
| 614 | "cmp.or.adj"); |
| 615 | EqZero = Builder.CreateBinOp(And, EqZero, OrAdjAnd1EqZero); |
| 616 | } |
| 617 | |
| 618 | // Tie together all our conditions. |
| 619 | llvm::Value *Result = Builder.CreateBinOp(Or, EqZero, AdjEq); |
| 620 | Result = Builder.CreateBinOp(And, PtrEq, Result, |
| 621 | Inequality ? "memptr.ne" : "memptr.eq"); |
| 622 | return Result; |
| 623 | } |
| 624 | |
| 625 | llvm::Value * |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 626 | ItaniumCXXABI::EmitMemberPointerIsNotNull(CodeGenFunction &CGF, |
| 627 | llvm::Value *MemPtr, |
| 628 | const MemberPointerType *MPT) { |
John McCall | e9fd7eb | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 629 | CGBuilderTy &Builder = CGF.Builder; |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 630 | |
| 631 | /// For member data pointers, this is just a check against -1. |
| 632 | if (MPT->isMemberDataPointer()) { |
| 633 | assert(MemPtr->getType() == getPtrDiffTy()); |
| 634 | llvm::Value *NegativeOne = |
| 635 | llvm::Constant::getAllOnesValue(MemPtr->getType()); |
| 636 | return Builder.CreateICmpNE(MemPtr, NegativeOne, "memptr.tobool"); |
| 637 | } |
John McCall | e9fd7eb | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 638 | |
| 639 | // In Itanium, a member function pointer is null if 'ptr' is null. |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 640 | llvm::Value *Ptr = Builder.CreateExtractValue(MemPtr, 0, "memptr.ptr"); |
John McCall | e9fd7eb | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 641 | |
| 642 | llvm::Constant *Zero = llvm::ConstantInt::get(Ptr->getType(), 0); |
| 643 | llvm::Value *Result = Builder.CreateICmpNE(Ptr, Zero, "memptr.tobool"); |
| 644 | |
| 645 | // In ARM, it's that, plus the low bit of 'adj' must be zero. |
| 646 | if (IsARM) { |
| 647 | llvm::Constant *One = llvm::ConstantInt::get(Ptr->getType(), 1); |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 648 | llvm::Value *Adj = Builder.CreateExtractValue(MemPtr, 1, "memptr.adj"); |
John McCall | e9fd7eb | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 649 | llvm::Value *VirtualBit = Builder.CreateAnd(Adj, One, "memptr.virtualbit"); |
| 650 | llvm::Value *IsNotVirtual = Builder.CreateICmpEQ(VirtualBit, Zero, |
| 651 | "memptr.notvirtual"); |
| 652 | Result = Builder.CreateAnd(Result, IsNotVirtual); |
| 653 | } |
| 654 | |
| 655 | return Result; |
| 656 | } |
John McCall | 875ab10 | 2010-08-22 06:43:33 +0000 | [diff] [blame] | 657 | |
John McCall | f16aa10 | 2010-08-22 21:01:12 +0000 | [diff] [blame] | 658 | /// The Itanium ABI requires non-zero initialization only for data |
| 659 | /// member pointers, for which '0' is a valid offset. |
| 660 | bool ItaniumCXXABI::isZeroInitializable(const MemberPointerType *MPT) { |
| 661 | return MPT->getPointeeType()->isFunctionType(); |
John McCall | cf2c85e | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 662 | } |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 663 | |
| 664 | /// The generic ABI passes 'this', plus a VTT if it's initializing a |
| 665 | /// base subobject. |
| 666 | void ItaniumCXXABI::BuildConstructorSignature(const CXXConstructorDecl *Ctor, |
| 667 | CXXCtorType Type, |
| 668 | CanQualType &ResTy, |
| 669 | llvm::SmallVectorImpl<CanQualType> &ArgTys) { |
| 670 | ASTContext &Context = CGM.getContext(); |
| 671 | |
| 672 | // 'this' is already there. |
| 673 | |
| 674 | // Check if we need to add a VTT parameter (which has type void **). |
| 675 | if (Type == Ctor_Base && Ctor->getParent()->getNumVBases() != 0) |
| 676 | ArgTys.push_back(Context.getPointerType(Context.VoidPtrTy)); |
| 677 | } |
| 678 | |
| 679 | /// The ARM ABI does the same as the Itanium ABI, but returns 'this'. |
| 680 | void ARMCXXABI::BuildConstructorSignature(const CXXConstructorDecl *Ctor, |
| 681 | CXXCtorType Type, |
| 682 | CanQualType &ResTy, |
| 683 | llvm::SmallVectorImpl<CanQualType> &ArgTys) { |
| 684 | ItaniumCXXABI::BuildConstructorSignature(Ctor, Type, ResTy, ArgTys); |
| 685 | ResTy = ArgTys[0]; |
| 686 | } |
| 687 | |
| 688 | /// The generic ABI passes 'this', plus a VTT if it's destroying a |
| 689 | /// base subobject. |
| 690 | void ItaniumCXXABI::BuildDestructorSignature(const CXXDestructorDecl *Dtor, |
| 691 | CXXDtorType Type, |
| 692 | CanQualType &ResTy, |
| 693 | llvm::SmallVectorImpl<CanQualType> &ArgTys) { |
| 694 | ASTContext &Context = CGM.getContext(); |
| 695 | |
| 696 | // 'this' is already there. |
| 697 | |
| 698 | // Check if we need to add a VTT parameter (which has type void **). |
| 699 | if (Type == Dtor_Base && Dtor->getParent()->getNumVBases() != 0) |
| 700 | ArgTys.push_back(Context.getPointerType(Context.VoidPtrTy)); |
| 701 | } |
| 702 | |
| 703 | /// The ARM ABI does the same as the Itanium ABI, but returns 'this' |
| 704 | /// for non-deleting destructors. |
| 705 | void ARMCXXABI::BuildDestructorSignature(const CXXDestructorDecl *Dtor, |
| 706 | CXXDtorType Type, |
| 707 | CanQualType &ResTy, |
| 708 | llvm::SmallVectorImpl<CanQualType> &ArgTys) { |
| 709 | ItaniumCXXABI::BuildDestructorSignature(Dtor, Type, ResTy, ArgTys); |
| 710 | |
| 711 | if (Type != Dtor_Deleting) |
| 712 | ResTy = ArgTys[0]; |
| 713 | } |
| 714 | |
| 715 | void ItaniumCXXABI::BuildInstanceFunctionParams(CodeGenFunction &CGF, |
| 716 | QualType &ResTy, |
| 717 | FunctionArgList &Params) { |
| 718 | /// Create the 'this' variable. |
| 719 | BuildThisParam(CGF, Params); |
| 720 | |
| 721 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(CGF.CurGD.getDecl()); |
| 722 | assert(MD->isInstance()); |
| 723 | |
| 724 | // Check if we need a VTT parameter as well. |
| 725 | if (CodeGenVTables::needsVTTParameter(CGF.CurGD)) { |
| 726 | ASTContext &Context = CGF.getContext(); |
| 727 | |
| 728 | // FIXME: avoid the fake decl |
| 729 | QualType T = Context.getPointerType(Context.VoidPtrTy); |
| 730 | ImplicitParamDecl *VTTDecl |
| 731 | = ImplicitParamDecl::Create(Context, 0, MD->getLocation(), |
| 732 | &Context.Idents.get("vtt"), T); |
| 733 | Params.push_back(std::make_pair(VTTDecl, VTTDecl->getType())); |
| 734 | getVTTDecl(CGF) = VTTDecl; |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | void ARMCXXABI::BuildInstanceFunctionParams(CodeGenFunction &CGF, |
| 739 | QualType &ResTy, |
| 740 | FunctionArgList &Params) { |
| 741 | ItaniumCXXABI::BuildInstanceFunctionParams(CGF, ResTy, Params); |
| 742 | |
| 743 | // Return 'this' from certain constructors and destructors. |
| 744 | if (HasThisReturn(CGF.CurGD)) |
| 745 | ResTy = Params[0].second; |
| 746 | } |
| 747 | |
| 748 | void ItaniumCXXABI::EmitInstanceFunctionProlog(CodeGenFunction &CGF) { |
| 749 | /// Initialize the 'this' slot. |
| 750 | EmitThisParam(CGF); |
| 751 | |
| 752 | /// Initialize the 'vtt' slot if needed. |
| 753 | if (getVTTDecl(CGF)) { |
| 754 | getVTTValue(CGF) |
| 755 | = CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(getVTTDecl(CGF)), |
| 756 | "vtt"); |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | void ARMCXXABI::EmitInstanceFunctionProlog(CodeGenFunction &CGF) { |
| 761 | ItaniumCXXABI::EmitInstanceFunctionProlog(CGF); |
| 762 | |
| 763 | /// Initialize the return slot to 'this' at the start of the |
| 764 | /// function. |
| 765 | if (HasThisReturn(CGF.CurGD)) |
| 766 | CGF.Builder.CreateStore(CGF.LoadCXXThis(), CGF.ReturnValue); |
| 767 | } |
| 768 | |
| 769 | void ARMCXXABI::EmitReturnFromThunk(CodeGenFunction &CGF, |
| 770 | RValue RV, QualType ResultType) { |
| 771 | if (!isa<CXXDestructorDecl>(CGF.CurGD.getDecl())) |
| 772 | return ItaniumCXXABI::EmitReturnFromThunk(CGF, RV, ResultType); |
| 773 | |
| 774 | // Destructor thunks in the ARM ABI have indeterminate results. |
| 775 | const llvm::Type *T = |
| 776 | cast<llvm::PointerType>(CGF.ReturnValue->getType())->getElementType(); |
| 777 | RValue Undef = RValue::get(llvm::UndefValue::get(T)); |
| 778 | return ItaniumCXXABI::EmitReturnFromThunk(CGF, Undef, ResultType); |
| 779 | } |