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 | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 22 | #include "CodeGenFunction.h" |
Charles Davis | 3a811f1 | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 23 | #include "CodeGenModule.h" |
| 24 | #include "Mangle.h" |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 25 | #include <clang/AST/Type.h> |
| 26 | #include <llvm/Value.h> |
Charles Davis | 3a811f1 | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 27 | |
| 28 | using namespace clang; |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 29 | using namespace CodeGen; |
Charles Davis | 3a811f1 | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 30 | |
| 31 | namespace { |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 32 | class ItaniumCXXABI : public CodeGen::CGCXXABI { |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 33 | protected: |
Charles Davis | 3a811f1 | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 34 | CodeGen::MangleContext MangleCtx; |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 35 | bool IsARM; |
Charles Davis | 3a811f1 | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 36 | public: |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 37 | ItaniumCXXABI(CodeGen::CodeGenModule &CGM, bool IsARM = false) : |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 38 | CGCXXABI(CGM), MangleCtx(CGM.getContext(), CGM.getDiags()), IsARM(IsARM) { } |
Charles Davis | 3a811f1 | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 39 | |
| 40 | CodeGen::MangleContext &getMangleContext() { |
| 41 | return MangleCtx; |
| 42 | } |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 43 | |
John McCall | f16aa10 | 2010-08-22 21:01:12 +0000 | [diff] [blame^] | 44 | bool isZeroInitializable(const MemberPointerType *MPT); |
John McCall | cf2c85e | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 45 | |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 46 | llvm::Value *EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF, |
| 47 | llvm::Value *&This, |
| 48 | llvm::Value *MemFnPtr, |
| 49 | const MemberPointerType *MPT); |
John McCall | 3023def | 2010-08-22 03:04:22 +0000 | [diff] [blame] | 50 | |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 51 | llvm::Value *EmitMemberFunctionPointerConversion(CodeGenFunction &CGF, |
| 52 | const CastExpr *E, |
| 53 | llvm::Value *Src); |
John McCall | cf2c85e | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 54 | |
| 55 | llvm::Constant *EmitMemberFunctionPointerConversion(llvm::Constant *C, |
| 56 | const CastExpr *E); |
| 57 | |
John McCall | cf2c85e | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 58 | llvm::Constant *EmitNullMemberFunctionPointer(const MemberPointerType *MPT); |
| 59 | |
John McCall | 875ab10 | 2010-08-22 06:43:33 +0000 | [diff] [blame] | 60 | llvm::Constant *EmitMemberFunctionPointer(const CXXMethodDecl *MD); |
| 61 | |
John McCall | e9fd7eb | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 62 | llvm::Value *EmitMemberFunctionPointerComparison(CodeGenFunction &CGF, |
| 63 | llvm::Value *L, |
| 64 | llvm::Value *R, |
| 65 | const MemberPointerType *MPT, |
| 66 | bool Inequality); |
| 67 | |
| 68 | llvm::Value *EmitMemberFunctionPointerIsNotNull(CodeGenFunction &CGF, |
| 69 | llvm::Value *Addr, |
| 70 | const MemberPointerType *MPT); |
| 71 | |
John McCall | 875ab10 | 2010-08-22 06:43:33 +0000 | [diff] [blame] | 72 | private: |
| 73 | void GetMemberFunctionPointer(const CXXMethodDecl *MD, |
| 74 | llvm::Constant *(&Array)[2]); |
Charles Davis | 3a811f1 | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 75 | }; |
John McCall | ee79a4c | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 76 | |
| 77 | class ARMCXXABI : public ItaniumCXXABI { |
| 78 | public: |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 79 | ARMCXXABI(CodeGen::CodeGenModule &CGM) : ItaniumCXXABI(CGM, /*ARM*/ true) {} |
John McCall | ee79a4c | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 80 | }; |
Charles Davis | 3a811f1 | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 83 | CodeGen::CGCXXABI *CodeGen::CreateItaniumCXXABI(CodeGenModule &CGM) { |
Charles Davis | 3a811f1 | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 84 | return new ItaniumCXXABI(CGM); |
| 85 | } |
| 86 | |
John McCall | ee79a4c | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 87 | CodeGen::CGCXXABI *CodeGen::CreateARMCXXABI(CodeGenModule &CGM) { |
| 88 | return new ARMCXXABI(CGM); |
| 89 | } |
| 90 | |
John McCall | 875ab10 | 2010-08-22 06:43:33 +0000 | [diff] [blame] | 91 | void ItaniumCXXABI::GetMemberFunctionPointer(const CXXMethodDecl *MD, |
| 92 | llvm::Constant *(&MemPtr)[2]) { |
John McCall | 875ab10 | 2010-08-22 06:43:33 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 96 | /// In the Itanium and ARM ABIs, method pointers have the form: |
| 97 | /// struct { ptrdiff_t ptr; ptrdiff_t adj; } memptr; |
| 98 | /// |
| 99 | /// In the Itanium ABI: |
| 100 | /// - method pointers are virtual if (memptr.ptr & 1) is nonzero |
| 101 | /// - the this-adjustment is (memptr.adj) |
| 102 | /// - the virtual offset is (memptr.ptr - 1) |
| 103 | /// |
| 104 | /// In the ARM ABI: |
| 105 | /// - method pointers are virtual if (memptr.adj & 1) is nonzero |
| 106 | /// - the this-adjustment is (memptr.adj >> 1) |
| 107 | /// - the virtual offset is (memptr.ptr) |
| 108 | /// ARM uses 'adj' for the virtual flag because Thumb functions |
| 109 | /// may be only single-byte aligned. |
| 110 | /// |
| 111 | /// If the member is virtual, the adjusted 'this' pointer points |
| 112 | /// to a vtable pointer from which the virtual offset is applied. |
| 113 | /// |
| 114 | /// If the member is non-virtual, memptr.ptr is the address of |
| 115 | /// the function to call. |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 116 | llvm::Value * |
| 117 | ItaniumCXXABI::EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF, |
| 118 | llvm::Value *&This, |
| 119 | llvm::Value *MemFnPtr, |
| 120 | const MemberPointerType *MPT) { |
| 121 | CGBuilderTy &Builder = CGF.Builder; |
| 122 | |
| 123 | const FunctionProtoType *FPT = |
| 124 | MPT->getPointeeType()->getAs<FunctionProtoType>(); |
| 125 | const CXXRecordDecl *RD = |
| 126 | cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl()); |
| 127 | |
| 128 | const llvm::FunctionType *FTy = |
| 129 | CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(RD, FPT), |
| 130 | FPT->isVariadic()); |
| 131 | |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 132 | const llvm::IntegerType *ptrdiff = CGF.IntPtrTy; |
| 133 | llvm::Constant *ptrdiff_1 = llvm::ConstantInt::get(ptrdiff, 1); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 134 | |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 135 | llvm::BasicBlock *FnVirtual = CGF.createBasicBlock("memptr.virtual"); |
| 136 | llvm::BasicBlock *FnNonVirtual = CGF.createBasicBlock("memptr.nonvirtual"); |
| 137 | llvm::BasicBlock *FnEnd = CGF.createBasicBlock("memptr.end"); |
| 138 | |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 139 | // Extract memptr.adj, which is in the second field. |
| 140 | llvm::Value *RawAdj = Builder.CreateExtractValue(MemFnPtr, 1, "memptr.adj"); |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 141 | |
| 142 | // Compute the true adjustment. |
| 143 | llvm::Value *Adj = RawAdj; |
| 144 | if (IsARM) |
| 145 | Adj = Builder.CreateAShr(Adj, ptrdiff_1, "memptr.adj.shifted"); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 146 | |
| 147 | // Apply the adjustment and cast back to the original struct type |
| 148 | // for consistency. |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 149 | llvm::Value *Ptr = Builder.CreateBitCast(This, Builder.getInt8PtrTy()); |
| 150 | Ptr = Builder.CreateInBoundsGEP(Ptr, Adj); |
| 151 | This = Builder.CreateBitCast(Ptr, This->getType(), "this.adjusted"); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 152 | |
| 153 | // Load the function pointer. |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 154 | llvm::Value *FnAsInt = Builder.CreateExtractValue(MemFnPtr, 0, "memptr.ptr"); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 155 | |
| 156 | // If the LSB in the function pointer is 1, the function pointer points to |
| 157 | // a virtual function. |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 158 | llvm::Value *IsVirtual; |
| 159 | if (IsARM) |
| 160 | IsVirtual = Builder.CreateAnd(RawAdj, ptrdiff_1); |
| 161 | else |
| 162 | IsVirtual = Builder.CreateAnd(FnAsInt, ptrdiff_1); |
| 163 | IsVirtual = Builder.CreateIsNotNull(IsVirtual, "memptr.isvirtual"); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 164 | Builder.CreateCondBr(IsVirtual, FnVirtual, FnNonVirtual); |
| 165 | |
| 166 | // In the virtual path, the adjustment left 'This' pointing to the |
| 167 | // vtable of the correct base subobject. The "function pointer" is an |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 168 | // offset within the vtable (+1 for the virtual flag on non-ARM). |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 169 | CGF.EmitBlock(FnVirtual); |
| 170 | |
| 171 | // Cast the adjusted this to a pointer to vtable pointer and load. |
| 172 | const llvm::Type *VTableTy = Builder.getInt8PtrTy(); |
| 173 | llvm::Value *VTable = Builder.CreateBitCast(This, VTableTy->getPointerTo()); |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 174 | VTable = Builder.CreateLoad(VTable, "memptr.vtable"); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 175 | |
| 176 | // Apply the offset. |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 177 | llvm::Value *VTableOffset = FnAsInt; |
| 178 | if (!IsARM) VTableOffset = Builder.CreateSub(VTableOffset, ptrdiff_1); |
| 179 | VTable = Builder.CreateGEP(VTable, VTableOffset); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 180 | |
| 181 | // Load the virtual function to call. |
| 182 | VTable = Builder.CreateBitCast(VTable, FTy->getPointerTo()->getPointerTo()); |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 183 | llvm::Value *VirtualFn = Builder.CreateLoad(VTable, "memptr.virtualfn"); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 184 | CGF.EmitBranch(FnEnd); |
| 185 | |
| 186 | // In the non-virtual path, the function pointer is actually a |
| 187 | // function pointer. |
| 188 | CGF.EmitBlock(FnNonVirtual); |
| 189 | llvm::Value *NonVirtualFn = |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 190 | Builder.CreateIntToPtr(FnAsInt, FTy->getPointerTo(), "memptr.nonvirtualfn"); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 191 | |
| 192 | // We're done. |
| 193 | CGF.EmitBlock(FnEnd); |
| 194 | llvm::PHINode *Callee = Builder.CreatePHI(FTy->getPointerTo()); |
| 195 | Callee->reserveOperandSpace(2); |
| 196 | Callee->addIncoming(VirtualFn, FnVirtual); |
| 197 | Callee->addIncoming(NonVirtualFn, FnNonVirtual); |
| 198 | return Callee; |
| 199 | } |
John McCall | 3023def | 2010-08-22 03:04:22 +0000 | [diff] [blame] | 200 | |
| 201 | /// Perform a derived-to-base or base-to-derived member pointer conversion. |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 202 | llvm::Value * |
| 203 | ItaniumCXXABI::EmitMemberFunctionPointerConversion(CodeGenFunction &CGF, |
| 204 | const CastExpr *E, |
| 205 | llvm::Value *Src) { |
John McCall | 3023def | 2010-08-22 03:04:22 +0000 | [diff] [blame] | 206 | assert(E->getCastKind() == CastExpr::CK_DerivedToBaseMemberPointer || |
| 207 | E->getCastKind() == CastExpr::CK_BaseToDerivedMemberPointer); |
| 208 | |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 209 | if (isa<llvm::Constant>(Src)) |
| 210 | return EmitMemberFunctionPointerConversion(cast<llvm::Constant>(Src), E); |
| 211 | |
John McCall | 3023def | 2010-08-22 03:04:22 +0000 | [diff] [blame] | 212 | CGBuilderTy &Builder = CGF.Builder; |
| 213 | |
| 214 | const MemberPointerType *SrcTy = |
| 215 | E->getSubExpr()->getType()->getAs<MemberPointerType>(); |
| 216 | const MemberPointerType *DestTy = E->getType()->getAs<MemberPointerType>(); |
| 217 | |
| 218 | const CXXRecordDecl *SrcDecl = SrcTy->getClass()->getAsCXXRecordDecl(); |
| 219 | const CXXRecordDecl *DestDecl = DestTy->getClass()->getAsCXXRecordDecl(); |
| 220 | |
John McCall | 3023def | 2010-08-22 03:04:22 +0000 | [diff] [blame] | 221 | bool DerivedToBase = |
| 222 | E->getCastKind() == CastExpr::CK_DerivedToBaseMemberPointer; |
| 223 | |
| 224 | const CXXRecordDecl *BaseDecl, *DerivedDecl; |
| 225 | if (DerivedToBase) |
| 226 | DerivedDecl = SrcDecl, BaseDecl = DestDecl; |
| 227 | else |
| 228 | BaseDecl = SrcDecl, DerivedDecl = DestDecl; |
| 229 | |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 230 | llvm::Constant *Adj = |
| 231 | CGF.CGM.GetNonVirtualBaseClassOffset(DerivedDecl, |
| 232 | E->path_begin(), |
| 233 | E->path_end()); |
| 234 | if (!Adj) return Src; |
John McCall | 875ab10 | 2010-08-22 06:43:33 +0000 | [diff] [blame] | 235 | |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 236 | // The this-adjustment is left-shifted by 1 on ARM. |
| 237 | if (IsARM) { |
| 238 | uint64_t Offset = cast<llvm::ConstantInt>(Adj)->getZExtValue(); |
| 239 | Offset <<= 1; |
| 240 | Adj = llvm::ConstantInt::get(Adj->getType(), Offset); |
| 241 | } |
| 242 | |
John McCall | e14add4 | 2010-08-22 11:04:31 +0000 | [diff] [blame] | 243 | llvm::Value *SrcAdj = Builder.CreateExtractValue(Src, 1, "src.adj"); |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 244 | llvm::Value *DstAdj; |
| 245 | if (DerivedToBase) |
| 246 | DstAdj = Builder.CreateSub(SrcAdj, Adj, "adj"); |
| 247 | else |
| 248 | DstAdj = Builder.CreateAdd(SrcAdj, Adj, "adj"); |
| 249 | |
John McCall | e14add4 | 2010-08-22 11:04:31 +0000 | [diff] [blame] | 250 | return Builder.CreateInsertValue(Src, DstAdj, 1); |
John McCall | 3023def | 2010-08-22 03:04:22 +0000 | [diff] [blame] | 251 | } |
John McCall | cf2c85e | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 252 | |
| 253 | llvm::Constant * |
| 254 | ItaniumCXXABI::EmitMemberFunctionPointerConversion(llvm::Constant *C, |
| 255 | const CastExpr *E) { |
| 256 | const MemberPointerType *SrcTy = |
| 257 | E->getSubExpr()->getType()->getAs<MemberPointerType>(); |
| 258 | const MemberPointerType *DestTy = |
| 259 | E->getType()->getAs<MemberPointerType>(); |
| 260 | |
| 261 | bool DerivedToBase = |
| 262 | E->getCastKind() == CastExpr::CK_DerivedToBaseMemberPointer; |
| 263 | |
| 264 | const CXXRecordDecl *DerivedDecl; |
| 265 | if (DerivedToBase) |
| 266 | DerivedDecl = SrcTy->getClass()->getAsCXXRecordDecl(); |
| 267 | else |
| 268 | DerivedDecl = DestTy->getClass()->getAsCXXRecordDecl(); |
| 269 | |
| 270 | // Calculate the offset to the base class. |
| 271 | llvm::Constant *Offset = |
| 272 | CGM.GetNonVirtualBaseClassOffset(DerivedDecl, |
| 273 | E->path_begin(), |
| 274 | E->path_end()); |
| 275 | // If there's no offset, we're done. |
| 276 | if (!Offset) return C; |
| 277 | |
John McCall | 875ab10 | 2010-08-22 06:43:33 +0000 | [diff] [blame] | 278 | // The this-adjustment is left-shifted by 1 on ARM. |
| 279 | if (IsARM) { |
| 280 | uint64_t OffsetV = cast<llvm::ConstantInt>(Offset)->getZExtValue(); |
| 281 | OffsetV <<= 1; |
| 282 | Offset = llvm::ConstantInt::get(Offset->getType(), OffsetV); |
| 283 | } |
| 284 | |
John McCall | cf2c85e | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 285 | llvm::ConstantStruct *CS = cast<llvm::ConstantStruct>(C); |
| 286 | |
| 287 | llvm::Constant *Values[2] = { |
| 288 | CS->getOperand(0), |
| 289 | llvm::ConstantExpr::getAdd(CS->getOperand(1), Offset) |
| 290 | }; |
| 291 | return llvm::ConstantStruct::get(CGM.getLLVMContext(), Values, 2, |
| 292 | /*Packed=*/false); |
| 293 | } |
| 294 | |
| 295 | |
John McCall | cf2c85e | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 296 | llvm::Constant * |
| 297 | ItaniumCXXABI::EmitNullMemberFunctionPointer(const MemberPointerType *MPT) { |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 298 | const llvm::Type *ptrdiff_t = |
| 299 | CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType()); |
| 300 | |
| 301 | llvm::Constant *Zero = llvm::ConstantInt::get(ptrdiff_t, 0); |
| 302 | llvm::Constant *Values[2] = { Zero, Zero }; |
| 303 | return llvm::ConstantStruct::get(CGM.getLLVMContext(), Values, 2, |
| 304 | /*Packed=*/false); |
John McCall | cf2c85e | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 305 | } |
| 306 | |
John McCall | 875ab10 | 2010-08-22 06:43:33 +0000 | [diff] [blame] | 307 | llvm::Constant * |
| 308 | ItaniumCXXABI::EmitMemberFunctionPointer(const CXXMethodDecl *MD) { |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 309 | assert(MD->isInstance() && "Member function must not be static!"); |
| 310 | MD = MD->getCanonicalDecl(); |
| 311 | |
| 312 | CodeGenTypes &Types = CGM.getTypes(); |
| 313 | const llvm::Type *ptrdiff_t = |
| 314 | Types.ConvertType(CGM.getContext().getPointerDiffType()); |
| 315 | |
| 316 | // Get the function pointer (or index if this is a virtual function). |
| 317 | llvm::Constant *MemPtr[2]; |
| 318 | if (MD->isVirtual()) { |
| 319 | uint64_t Index = CGM.getVTables().getMethodVTableIndex(MD); |
| 320 | |
| 321 | // FIXME: We shouldn't use / 8 here. |
| 322 | uint64_t PointerWidthInBytes = |
| 323 | CGM.getContext().Target.getPointerWidth(0) / 8; |
| 324 | uint64_t VTableOffset = (Index * PointerWidthInBytes); |
| 325 | |
| 326 | if (IsARM) { |
| 327 | // ARM C++ ABI 3.2.1: |
| 328 | // This ABI specifies that adj contains twice the this |
| 329 | // adjustment, plus 1 if the member function is virtual. The |
| 330 | // least significant bit of adj then makes exactly the same |
| 331 | // discrimination as the least significant bit of ptr does for |
| 332 | // Itanium. |
| 333 | MemPtr[0] = llvm::ConstantInt::get(ptrdiff_t, VTableOffset); |
| 334 | MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t, 1); |
| 335 | } else { |
| 336 | // Itanium C++ ABI 2.3: |
| 337 | // For a virtual function, [the pointer field] is 1 plus the |
| 338 | // virtual table offset (in bytes) of the function, |
| 339 | // represented as a ptrdiff_t. |
| 340 | MemPtr[0] = llvm::ConstantInt::get(ptrdiff_t, VTableOffset + 1); |
| 341 | MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t, 0); |
| 342 | } |
| 343 | } else { |
| 344 | const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); |
| 345 | const llvm::Type *Ty; |
| 346 | // Check whether the function has a computable LLVM signature. |
| 347 | if (!CodeGenTypes::VerifyFuncTypeComplete(FPT)) { |
| 348 | // The function has a computable LLVM signature; use the correct type. |
| 349 | Ty = Types.GetFunctionType(Types.getFunctionInfo(MD), FPT->isVariadic()); |
| 350 | } else { |
| 351 | // Use an arbitrary non-function type to tell GetAddrOfFunction that the |
| 352 | // function type is incomplete. |
| 353 | Ty = ptrdiff_t; |
| 354 | } |
| 355 | |
| 356 | llvm::Constant *Addr = CGM.GetAddrOfFunction(MD, Ty); |
| 357 | MemPtr[0] = llvm::ConstantExpr::getPtrToInt(Addr, ptrdiff_t); |
| 358 | MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t, 0); |
| 359 | } |
John McCall | 875ab10 | 2010-08-22 06:43:33 +0000 | [diff] [blame] | 360 | |
| 361 | return llvm::ConstantStruct::get(CGM.getLLVMContext(), |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 362 | MemPtr, 2, /*Packed=*/false); |
John McCall | 875ab10 | 2010-08-22 06:43:33 +0000 | [diff] [blame] | 363 | } |
| 364 | |
John McCall | e9fd7eb | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 365 | /// The comparison algorithm is pretty easy: the member pointers are |
| 366 | /// the same if they're either bitwise identical *or* both null. |
| 367 | /// |
| 368 | /// ARM is different here only because null-ness is more complicated. |
| 369 | llvm::Value * |
| 370 | ItaniumCXXABI::EmitMemberFunctionPointerComparison(CodeGenFunction &CGF, |
| 371 | llvm::Value *L, |
| 372 | llvm::Value *R, |
| 373 | const MemberPointerType *MPT, |
| 374 | bool Inequality) { |
| 375 | CGBuilderTy &Builder = CGF.Builder; |
| 376 | |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 377 | llvm::Value *LPtr = Builder.CreateExtractValue(L, 0, "lhs.memptr.ptr"); |
| 378 | llvm::Value *RPtr = Builder.CreateExtractValue(R, 0, "rhs.memptr.ptr"); |
John McCall | e9fd7eb | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 379 | |
| 380 | // The Itanium tautology is: |
| 381 | // (L == R) <==> (L.ptr == R.ptr /\ (L.ptr == 0 \/ L.adj == R.adj)) |
| 382 | // The ARM tautology is: |
| 383 | // (L == R) <==> (L.ptr == R.ptr /\ |
| 384 | // (L.adj == R.adj \/ |
| 385 | // (L.ptr == 0 /\ ((L.adj|R.adj) & 1) == 0))) |
| 386 | // The inequality tautologies have exactly the same structure, except |
| 387 | // applying De Morgan's laws. |
| 388 | |
| 389 | llvm::ICmpInst::Predicate Eq; |
| 390 | llvm::Instruction::BinaryOps And, Or; |
| 391 | if (Inequality) { |
| 392 | Eq = llvm::ICmpInst::ICMP_NE; |
| 393 | And = llvm::Instruction::Or; |
| 394 | Or = llvm::Instruction::And; |
| 395 | } else { |
| 396 | Eq = llvm::ICmpInst::ICMP_EQ; |
| 397 | And = llvm::Instruction::And; |
| 398 | Or = llvm::Instruction::Or; |
| 399 | } |
| 400 | |
| 401 | // This condition tests whether L.ptr == R.ptr. This must always be |
| 402 | // true for equality to hold. |
| 403 | llvm::Value *PtrEq = Builder.CreateICmp(Eq, LPtr, RPtr, "cmp.ptr"); |
| 404 | |
| 405 | // This condition, together with the assumption that L.ptr == R.ptr, |
| 406 | // tests whether the pointers are both null. ARM imposes an extra |
| 407 | // condition. |
| 408 | llvm::Value *Zero = llvm::Constant::getNullValue(LPtr->getType()); |
| 409 | llvm::Value *EqZero = Builder.CreateICmp(Eq, LPtr, Zero, "cmp.ptr.null"); |
| 410 | |
| 411 | // This condition tests whether L.adj == R.adj. If this isn't |
| 412 | // true, the pointers are unequal unless they're both null. |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 413 | llvm::Value *LAdj = Builder.CreateExtractValue(L, 1, "lhs.memptr.adj"); |
| 414 | llvm::Value *RAdj = Builder.CreateExtractValue(R, 1, "rhs.memptr.adj"); |
John McCall | e9fd7eb | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 415 | llvm::Value *AdjEq = Builder.CreateICmp(Eq, LAdj, RAdj, "cmp.adj"); |
| 416 | |
| 417 | // Null member function pointers on ARM clear the low bit of Adj, |
| 418 | // so the zero condition has to check that neither low bit is set. |
| 419 | if (IsARM) { |
| 420 | llvm::Value *One = llvm::ConstantInt::get(LPtr->getType(), 1); |
| 421 | |
| 422 | // Compute (l.adj | r.adj) & 1 and test it against zero. |
| 423 | llvm::Value *OrAdj = Builder.CreateOr(LAdj, RAdj, "or.adj"); |
| 424 | llvm::Value *OrAdjAnd1 = Builder.CreateAnd(OrAdj, One); |
| 425 | llvm::Value *OrAdjAnd1EqZero = Builder.CreateICmp(Eq, OrAdjAnd1, Zero, |
| 426 | "cmp.or.adj"); |
| 427 | EqZero = Builder.CreateBinOp(And, EqZero, OrAdjAnd1EqZero); |
| 428 | } |
| 429 | |
| 430 | // Tie together all our conditions. |
| 431 | llvm::Value *Result = Builder.CreateBinOp(Or, EqZero, AdjEq); |
| 432 | Result = Builder.CreateBinOp(And, PtrEq, Result, |
| 433 | Inequality ? "memptr.ne" : "memptr.eq"); |
| 434 | return Result; |
| 435 | } |
| 436 | |
| 437 | llvm::Value * |
| 438 | ItaniumCXXABI::EmitMemberFunctionPointerIsNotNull(CodeGenFunction &CGF, |
| 439 | llvm::Value *MemPtr, |
| 440 | const MemberPointerType *MPT) { |
| 441 | CGBuilderTy &Builder = CGF.Builder; |
| 442 | |
| 443 | // In Itanium, a member function pointer is null if 'ptr' is null. |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 444 | llvm::Value *Ptr = Builder.CreateExtractValue(MemPtr, 0, "memptr.ptr"); |
John McCall | e9fd7eb | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 445 | |
| 446 | llvm::Constant *Zero = llvm::ConstantInt::get(Ptr->getType(), 0); |
| 447 | llvm::Value *Result = Builder.CreateICmpNE(Ptr, Zero, "memptr.tobool"); |
| 448 | |
| 449 | // In ARM, it's that, plus the low bit of 'adj' must be zero. |
| 450 | if (IsARM) { |
| 451 | llvm::Constant *One = llvm::ConstantInt::get(Ptr->getType(), 1); |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 452 | llvm::Value *Adj = Builder.CreateExtractValue(MemPtr, 1, "memptr.adj"); |
John McCall | e9fd7eb | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 453 | llvm::Value *VirtualBit = Builder.CreateAnd(Adj, One, "memptr.virtualbit"); |
| 454 | llvm::Value *IsNotVirtual = Builder.CreateICmpEQ(VirtualBit, Zero, |
| 455 | "memptr.notvirtual"); |
| 456 | Result = Builder.CreateAnd(Result, IsNotVirtual); |
| 457 | } |
| 458 | |
| 459 | return Result; |
| 460 | } |
John McCall | 875ab10 | 2010-08-22 06:43:33 +0000 | [diff] [blame] | 461 | |
John McCall | f16aa10 | 2010-08-22 21:01:12 +0000 | [diff] [blame^] | 462 | /// The Itanium ABI requires non-zero initialization only for data |
| 463 | /// member pointers, for which '0' is a valid offset. |
| 464 | bool ItaniumCXXABI::isZeroInitializable(const MemberPointerType *MPT) { |
| 465 | return MPT->getPointeeType()->isFunctionType(); |
John McCall | cf2c85e | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 466 | } |