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 | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame^] | 69 | llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF, |
| 70 | const CastExpr *E, |
| 71 | llvm::Value *Src); |
John McCall | cf2c85e | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 72 | |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame^] | 73 | llvm::Constant *EmitMemberPointerConversion(llvm::Constant *C, |
| 74 | const CastExpr *E); |
John McCall | cf2c85e | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 75 | |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame^] | 76 | llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT); |
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 *EmitMemberPointer(const CXXMethodDecl *MD); |
| 79 | llvm::Constant *EmitMemberPointer(const FieldDecl *FD); |
John McCall | 875ab10 | 2010-08-22 06:43:33 +0000 | [diff] [blame] | 80 | |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame^] | 81 | llvm::Value *EmitMemberPointerComparison(CodeGenFunction &CGF, |
| 82 | llvm::Value *L, |
| 83 | llvm::Value *R, |
| 84 | const MemberPointerType *MPT, |
| 85 | bool Inequality); |
John McCall | e9fd7eb | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 86 | |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame^] | 87 | llvm::Value *EmitMemberPointerIsNotNull(CodeGenFunction &CGF, |
| 88 | llvm::Value *Addr, |
| 89 | const MemberPointerType *MPT); |
Charles Davis | 3a811f1 | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 90 | }; |
John McCall | ee79a4c | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 91 | |
| 92 | class ARMCXXABI : public ItaniumCXXABI { |
| 93 | public: |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 94 | ARMCXXABI(CodeGen::CodeGenModule &CGM) : ItaniumCXXABI(CGM, /*ARM*/ true) {} |
John McCall | ee79a4c | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 95 | }; |
Charles Davis | 3a811f1 | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 98 | CodeGen::CGCXXABI *CodeGen::CreateItaniumCXXABI(CodeGenModule &CGM) { |
Charles Davis | 3a811f1 | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 99 | return new ItaniumCXXABI(CGM); |
| 100 | } |
| 101 | |
John McCall | ee79a4c | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 102 | CodeGen::CGCXXABI *CodeGen::CreateARMCXXABI(CodeGenModule &CGM) { |
| 103 | return new ARMCXXABI(CGM); |
| 104 | } |
| 105 | |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame^] | 106 | const llvm::Type * |
| 107 | ItaniumCXXABI::ConvertMemberPointerType(const MemberPointerType *MPT) { |
| 108 | if (MPT->isMemberDataPointer()) |
| 109 | return getPtrDiffTy(); |
| 110 | else |
| 111 | return llvm::StructType::get(CGM.getLLVMContext(), |
| 112 | getPtrDiffTy(), getPtrDiffTy(), NULL); |
John McCall | 875ab10 | 2010-08-22 06:43:33 +0000 | [diff] [blame] | 113 | } |
| 114 | |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 115 | /// In the Itanium and ARM ABIs, method pointers have the form: |
| 116 | /// struct { ptrdiff_t ptr; ptrdiff_t adj; } memptr; |
| 117 | /// |
| 118 | /// In the Itanium ABI: |
| 119 | /// - method pointers are virtual if (memptr.ptr & 1) is nonzero |
| 120 | /// - the this-adjustment is (memptr.adj) |
| 121 | /// - the virtual offset is (memptr.ptr - 1) |
| 122 | /// |
| 123 | /// In the ARM ABI: |
| 124 | /// - method pointers are virtual if (memptr.adj & 1) is nonzero |
| 125 | /// - the this-adjustment is (memptr.adj >> 1) |
| 126 | /// - the virtual offset is (memptr.ptr) |
| 127 | /// ARM uses 'adj' for the virtual flag because Thumb functions |
| 128 | /// may be only single-byte aligned. |
| 129 | /// |
| 130 | /// If the member is virtual, the adjusted 'this' pointer points |
| 131 | /// to a vtable pointer from which the virtual offset is applied. |
| 132 | /// |
| 133 | /// If the member is non-virtual, memptr.ptr is the address of |
| 134 | /// the function to call. |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 135 | llvm::Value * |
| 136 | ItaniumCXXABI::EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF, |
| 137 | llvm::Value *&This, |
| 138 | llvm::Value *MemFnPtr, |
| 139 | const MemberPointerType *MPT) { |
| 140 | CGBuilderTy &Builder = CGF.Builder; |
| 141 | |
| 142 | const FunctionProtoType *FPT = |
| 143 | MPT->getPointeeType()->getAs<FunctionProtoType>(); |
| 144 | const CXXRecordDecl *RD = |
| 145 | cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl()); |
| 146 | |
| 147 | const llvm::FunctionType *FTy = |
| 148 | CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(RD, FPT), |
| 149 | FPT->isVariadic()); |
| 150 | |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame^] | 151 | const llvm::IntegerType *ptrdiff = getPtrDiffTy(); |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 152 | llvm::Constant *ptrdiff_1 = llvm::ConstantInt::get(ptrdiff, 1); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 153 | |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 154 | llvm::BasicBlock *FnVirtual = CGF.createBasicBlock("memptr.virtual"); |
| 155 | llvm::BasicBlock *FnNonVirtual = CGF.createBasicBlock("memptr.nonvirtual"); |
| 156 | llvm::BasicBlock *FnEnd = CGF.createBasicBlock("memptr.end"); |
| 157 | |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 158 | // Extract memptr.adj, which is in the second field. |
| 159 | llvm::Value *RawAdj = Builder.CreateExtractValue(MemFnPtr, 1, "memptr.adj"); |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 160 | |
| 161 | // Compute the true adjustment. |
| 162 | llvm::Value *Adj = RawAdj; |
| 163 | if (IsARM) |
| 164 | Adj = Builder.CreateAShr(Adj, ptrdiff_1, "memptr.adj.shifted"); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 165 | |
| 166 | // Apply the adjustment and cast back to the original struct type |
| 167 | // for consistency. |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 168 | llvm::Value *Ptr = Builder.CreateBitCast(This, Builder.getInt8PtrTy()); |
| 169 | Ptr = Builder.CreateInBoundsGEP(Ptr, Adj); |
| 170 | This = Builder.CreateBitCast(Ptr, This->getType(), "this.adjusted"); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 171 | |
| 172 | // Load the function pointer. |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 173 | llvm::Value *FnAsInt = Builder.CreateExtractValue(MemFnPtr, 0, "memptr.ptr"); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 174 | |
| 175 | // If the LSB in the function pointer is 1, the function pointer points to |
| 176 | // a virtual function. |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 177 | llvm::Value *IsVirtual; |
| 178 | if (IsARM) |
| 179 | IsVirtual = Builder.CreateAnd(RawAdj, ptrdiff_1); |
| 180 | else |
| 181 | IsVirtual = Builder.CreateAnd(FnAsInt, ptrdiff_1); |
| 182 | IsVirtual = Builder.CreateIsNotNull(IsVirtual, "memptr.isvirtual"); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 183 | Builder.CreateCondBr(IsVirtual, FnVirtual, FnNonVirtual); |
| 184 | |
| 185 | // In the virtual path, the adjustment left 'This' pointing to the |
| 186 | // vtable of the correct base subobject. The "function pointer" is an |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 187 | // offset within the vtable (+1 for the virtual flag on non-ARM). |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 188 | CGF.EmitBlock(FnVirtual); |
| 189 | |
| 190 | // Cast the adjusted this to a pointer to vtable pointer and load. |
| 191 | const llvm::Type *VTableTy = Builder.getInt8PtrTy(); |
| 192 | llvm::Value *VTable = Builder.CreateBitCast(This, VTableTy->getPointerTo()); |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 193 | VTable = Builder.CreateLoad(VTable, "memptr.vtable"); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 194 | |
| 195 | // Apply the offset. |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 196 | llvm::Value *VTableOffset = FnAsInt; |
| 197 | if (!IsARM) VTableOffset = Builder.CreateSub(VTableOffset, ptrdiff_1); |
| 198 | VTable = Builder.CreateGEP(VTable, VTableOffset); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 199 | |
| 200 | // Load the virtual function to call. |
| 201 | VTable = Builder.CreateBitCast(VTable, FTy->getPointerTo()->getPointerTo()); |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 202 | llvm::Value *VirtualFn = Builder.CreateLoad(VTable, "memptr.virtualfn"); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 203 | CGF.EmitBranch(FnEnd); |
| 204 | |
| 205 | // In the non-virtual path, the function pointer is actually a |
| 206 | // function pointer. |
| 207 | CGF.EmitBlock(FnNonVirtual); |
| 208 | llvm::Value *NonVirtualFn = |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 209 | Builder.CreateIntToPtr(FnAsInt, FTy->getPointerTo(), "memptr.nonvirtualfn"); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 210 | |
| 211 | // We're done. |
| 212 | CGF.EmitBlock(FnEnd); |
| 213 | llvm::PHINode *Callee = Builder.CreatePHI(FTy->getPointerTo()); |
| 214 | Callee->reserveOperandSpace(2); |
| 215 | Callee->addIncoming(VirtualFn, FnVirtual); |
| 216 | Callee->addIncoming(NonVirtualFn, FnNonVirtual); |
| 217 | return Callee; |
| 218 | } |
John McCall | 3023def | 2010-08-22 03:04:22 +0000 | [diff] [blame] | 219 | |
| 220 | /// Perform a derived-to-base or base-to-derived member pointer conversion. |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame^] | 221 | /// |
| 222 | /// Obligatory offset/adjustment diagram: |
| 223 | /// <-- offset --> <-- adjustment --> |
| 224 | /// |--------------------------|----------------------|--------------------| |
| 225 | /// ^Derived address point ^Base address point ^Member address point |
| 226 | /// |
| 227 | /// So when converting a base member pointer to a derived member pointer, |
| 228 | /// we add the offset to the adjustment because the address point has |
| 229 | /// decreased; and conversely, when converting a derived MP to a base MP |
| 230 | /// we subtract the offset from the adjustment because the address point |
| 231 | /// has increased. |
| 232 | /// |
| 233 | /// The standard forbids (at compile time) conversion to and from |
| 234 | /// virtual bases, which is why we don't have to consider them here. |
| 235 | /// |
| 236 | /// The standard forbids (at run time) casting a derived MP to a base |
| 237 | /// MP when the derived MP does not point to a member of the base. |
| 238 | /// This is why -1 is a reasonable choice for null data member |
| 239 | /// pointers. |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 240 | llvm::Value * |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame^] | 241 | ItaniumCXXABI::EmitMemberPointerConversion(CodeGenFunction &CGF, |
| 242 | const CastExpr *E, |
| 243 | llvm::Value *Src) { |
John McCall | 3023def | 2010-08-22 03:04:22 +0000 | [diff] [blame] | 244 | assert(E->getCastKind() == CastExpr::CK_DerivedToBaseMemberPointer || |
| 245 | E->getCastKind() == CastExpr::CK_BaseToDerivedMemberPointer); |
| 246 | |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 247 | if (isa<llvm::Constant>(Src)) |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame^] | 248 | return EmitMemberPointerConversion(cast<llvm::Constant>(Src), E); |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 249 | |
John McCall | 3023def | 2010-08-22 03:04:22 +0000 | [diff] [blame] | 250 | CGBuilderTy &Builder = CGF.Builder; |
| 251 | |
| 252 | const MemberPointerType *SrcTy = |
| 253 | E->getSubExpr()->getType()->getAs<MemberPointerType>(); |
| 254 | const MemberPointerType *DestTy = E->getType()->getAs<MemberPointerType>(); |
| 255 | |
| 256 | const CXXRecordDecl *SrcDecl = SrcTy->getClass()->getAsCXXRecordDecl(); |
| 257 | const CXXRecordDecl *DestDecl = DestTy->getClass()->getAsCXXRecordDecl(); |
| 258 | |
John McCall | 3023def | 2010-08-22 03:04:22 +0000 | [diff] [blame] | 259 | bool DerivedToBase = |
| 260 | E->getCastKind() == CastExpr::CK_DerivedToBaseMemberPointer; |
| 261 | |
| 262 | const CXXRecordDecl *BaseDecl, *DerivedDecl; |
| 263 | if (DerivedToBase) |
| 264 | DerivedDecl = SrcDecl, BaseDecl = DestDecl; |
| 265 | else |
| 266 | BaseDecl = SrcDecl, DerivedDecl = DestDecl; |
| 267 | |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 268 | llvm::Constant *Adj = |
| 269 | CGF.CGM.GetNonVirtualBaseClassOffset(DerivedDecl, |
| 270 | E->path_begin(), |
| 271 | E->path_end()); |
| 272 | if (!Adj) return Src; |
John McCall | 875ab10 | 2010-08-22 06:43:33 +0000 | [diff] [blame] | 273 | |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame^] | 274 | // For member data pointers, this is just a matter of adding the |
| 275 | // offset if the source is non-null. |
| 276 | if (SrcTy->isMemberDataPointer()) { |
| 277 | llvm::Value *Dst; |
| 278 | if (DerivedToBase) |
| 279 | Dst = Builder.CreateNSWSub(Src, Adj, "adj"); |
| 280 | else |
| 281 | Dst = Builder.CreateNSWAdd(Src, Adj, "adj"); |
| 282 | |
| 283 | // Null check. |
| 284 | llvm::Value *Null = llvm::Constant::getAllOnesValue(Src->getType()); |
| 285 | llvm::Value *IsNull = Builder.CreateICmpEQ(Src, Null, "memptr.isnull"); |
| 286 | return Builder.CreateSelect(IsNull, Src, Dst); |
| 287 | } |
| 288 | |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 289 | // The this-adjustment is left-shifted by 1 on ARM. |
| 290 | if (IsARM) { |
| 291 | uint64_t Offset = cast<llvm::ConstantInt>(Adj)->getZExtValue(); |
| 292 | Offset <<= 1; |
| 293 | Adj = llvm::ConstantInt::get(Adj->getType(), Offset); |
| 294 | } |
| 295 | |
John McCall | e14add4 | 2010-08-22 11:04:31 +0000 | [diff] [blame] | 296 | llvm::Value *SrcAdj = Builder.CreateExtractValue(Src, 1, "src.adj"); |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 297 | llvm::Value *DstAdj; |
| 298 | if (DerivedToBase) |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame^] | 299 | DstAdj = Builder.CreateNSWSub(SrcAdj, Adj, "adj"); |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 300 | else |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame^] | 301 | DstAdj = Builder.CreateNSWAdd(SrcAdj, Adj, "adj"); |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 302 | |
John McCall | e14add4 | 2010-08-22 11:04:31 +0000 | [diff] [blame] | 303 | return Builder.CreateInsertValue(Src, DstAdj, 1); |
John McCall | 3023def | 2010-08-22 03:04:22 +0000 | [diff] [blame] | 304 | } |
John McCall | cf2c85e | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 305 | |
| 306 | llvm::Constant * |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame^] | 307 | ItaniumCXXABI::EmitMemberPointerConversion(llvm::Constant *C, |
| 308 | const CastExpr *E) { |
John McCall | cf2c85e | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 309 | const MemberPointerType *SrcTy = |
| 310 | E->getSubExpr()->getType()->getAs<MemberPointerType>(); |
| 311 | const MemberPointerType *DestTy = |
| 312 | E->getType()->getAs<MemberPointerType>(); |
| 313 | |
| 314 | bool DerivedToBase = |
| 315 | E->getCastKind() == CastExpr::CK_DerivedToBaseMemberPointer; |
| 316 | |
| 317 | const CXXRecordDecl *DerivedDecl; |
| 318 | if (DerivedToBase) |
| 319 | DerivedDecl = SrcTy->getClass()->getAsCXXRecordDecl(); |
| 320 | else |
| 321 | DerivedDecl = DestTy->getClass()->getAsCXXRecordDecl(); |
| 322 | |
| 323 | // Calculate the offset to the base class. |
| 324 | llvm::Constant *Offset = |
| 325 | CGM.GetNonVirtualBaseClassOffset(DerivedDecl, |
| 326 | E->path_begin(), |
| 327 | E->path_end()); |
| 328 | // If there's no offset, we're done. |
| 329 | if (!Offset) return C; |
| 330 | |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame^] | 331 | // If the source is a member data pointer, we have to do a null |
| 332 | // check and then add the offset. In the common case, we can fold |
| 333 | // away the offset. |
| 334 | if (SrcTy->isMemberDataPointer()) { |
| 335 | assert(C->getType() == getPtrDiffTy()); |
| 336 | |
| 337 | // If it's a constant int, just create a new constant int. |
| 338 | if (llvm::ConstantInt *CI = dyn_cast<llvm::ConstantInt>(C)) { |
| 339 | int64_t Src = CI->getSExtValue(); |
| 340 | |
| 341 | // Null converts to null. |
| 342 | if (Src == -1) return CI; |
| 343 | |
| 344 | // Otherwise, just add the offset. |
| 345 | int64_t OffsetV = cast<llvm::ConstantInt>(Offset)->getSExtValue(); |
| 346 | int64_t Dst = (DerivedToBase ? Src - OffsetV : Src + OffsetV); |
| 347 | return llvm::ConstantInt::get(CI->getType(), Dst, /*signed*/ true); |
| 348 | } |
| 349 | |
| 350 | // Otherwise, we have to form a constant select expression. |
| 351 | llvm::Constant *Null = llvm::Constant::getAllOnesValue(C->getType()); |
| 352 | |
| 353 | llvm::Constant *IsNull = |
| 354 | llvm::ConstantExpr::getICmp(llvm::ICmpInst::ICMP_EQ, C, Null); |
| 355 | |
| 356 | llvm::Constant *Dst; |
| 357 | if (DerivedToBase) |
| 358 | Dst = llvm::ConstantExpr::getNSWSub(C, Offset); |
| 359 | else |
| 360 | Dst = llvm::ConstantExpr::getNSWAdd(C, Offset); |
| 361 | |
| 362 | return llvm::ConstantExpr::getSelect(IsNull, Null, Dst); |
| 363 | } |
| 364 | |
John McCall | 875ab10 | 2010-08-22 06:43:33 +0000 | [diff] [blame] | 365 | // The this-adjustment is left-shifted by 1 on ARM. |
| 366 | if (IsARM) { |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame^] | 367 | int64_t OffsetV = cast<llvm::ConstantInt>(Offset)->getSExtValue(); |
John McCall | 875ab10 | 2010-08-22 06:43:33 +0000 | [diff] [blame] | 368 | OffsetV <<= 1; |
| 369 | Offset = llvm::ConstantInt::get(Offset->getType(), OffsetV); |
| 370 | } |
| 371 | |
John McCall | cf2c85e | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 372 | llvm::ConstantStruct *CS = cast<llvm::ConstantStruct>(C); |
| 373 | |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame^] | 374 | llvm::Constant *Values[2] = { CS->getOperand(0), 0 }; |
| 375 | if (DerivedToBase) |
| 376 | Values[1] = llvm::ConstantExpr::getSub(CS->getOperand(1), Offset); |
| 377 | else |
| 378 | Values[1] = llvm::ConstantExpr::getAdd(CS->getOperand(1), Offset); |
| 379 | |
John McCall | cf2c85e | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 380 | return llvm::ConstantStruct::get(CGM.getLLVMContext(), Values, 2, |
| 381 | /*Packed=*/false); |
| 382 | } |
| 383 | |
| 384 | |
John McCall | cf2c85e | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 385 | llvm::Constant * |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame^] | 386 | ItaniumCXXABI::EmitNullMemberPointer(const MemberPointerType *MPT) { |
| 387 | const llvm::Type *ptrdiff_t = getPtrDiffTy(); |
| 388 | |
| 389 | // Itanium C++ ABI 2.3: |
| 390 | // A NULL pointer is represented as -1. |
| 391 | if (MPT->isMemberDataPointer()) |
| 392 | return llvm::ConstantInt::get(ptrdiff_t, -1ULL, /*isSigned=*/true); |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 393 | |
| 394 | llvm::Constant *Zero = llvm::ConstantInt::get(ptrdiff_t, 0); |
| 395 | llvm::Constant *Values[2] = { Zero, Zero }; |
| 396 | return llvm::ConstantStruct::get(CGM.getLLVMContext(), Values, 2, |
| 397 | /*Packed=*/false); |
John McCall | cf2c85e | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 398 | } |
| 399 | |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame^] | 400 | llvm::Constant *ItaniumCXXABI::EmitMemberPointer(const FieldDecl *FD) { |
| 401 | // Itanium C++ ABI 2.3: |
| 402 | // A pointer to data member is an offset from the base address of |
| 403 | // the class object containing it, represented as a ptrdiff_t |
| 404 | |
| 405 | QualType ClassType = CGM.getContext().getTypeDeclType(FD->getParent()); |
| 406 | const llvm::StructType *ClassLTy = |
| 407 | cast<llvm::StructType>(CGM.getTypes().ConvertType(ClassType)); |
| 408 | |
| 409 | const CGRecordLayout &RL = CGM.getTypes().getCGRecordLayout(FD->getParent()); |
| 410 | unsigned FieldNo = RL.getLLVMFieldNo(FD); |
| 411 | uint64_t Offset = |
| 412 | CGM.getTargetData().getStructLayout(ClassLTy)->getElementOffset(FieldNo); |
| 413 | |
| 414 | return llvm::ConstantInt::get(getPtrDiffTy(), Offset); |
| 415 | } |
| 416 | |
| 417 | llvm::Constant *ItaniumCXXABI::EmitMemberPointer(const CXXMethodDecl *MD) { |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 418 | assert(MD->isInstance() && "Member function must not be static!"); |
| 419 | MD = MD->getCanonicalDecl(); |
| 420 | |
| 421 | CodeGenTypes &Types = CGM.getTypes(); |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame^] | 422 | const llvm::Type *ptrdiff_t = getPtrDiffTy(); |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 423 | |
| 424 | // Get the function pointer (or index if this is a virtual function). |
| 425 | llvm::Constant *MemPtr[2]; |
| 426 | if (MD->isVirtual()) { |
| 427 | uint64_t Index = CGM.getVTables().getMethodVTableIndex(MD); |
| 428 | |
| 429 | // FIXME: We shouldn't use / 8 here. |
| 430 | uint64_t PointerWidthInBytes = |
| 431 | CGM.getContext().Target.getPointerWidth(0) / 8; |
| 432 | uint64_t VTableOffset = (Index * PointerWidthInBytes); |
| 433 | |
| 434 | if (IsARM) { |
| 435 | // ARM C++ ABI 3.2.1: |
| 436 | // This ABI specifies that adj contains twice the this |
| 437 | // adjustment, plus 1 if the member function is virtual. The |
| 438 | // least significant bit of adj then makes exactly the same |
| 439 | // discrimination as the least significant bit of ptr does for |
| 440 | // Itanium. |
| 441 | MemPtr[0] = llvm::ConstantInt::get(ptrdiff_t, VTableOffset); |
| 442 | MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t, 1); |
| 443 | } else { |
| 444 | // Itanium C++ ABI 2.3: |
| 445 | // For a virtual function, [the pointer field] is 1 plus the |
| 446 | // virtual table offset (in bytes) of the function, |
| 447 | // represented as a ptrdiff_t. |
| 448 | MemPtr[0] = llvm::ConstantInt::get(ptrdiff_t, VTableOffset + 1); |
| 449 | MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t, 0); |
| 450 | } |
| 451 | } else { |
| 452 | const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); |
| 453 | const llvm::Type *Ty; |
| 454 | // Check whether the function has a computable LLVM signature. |
| 455 | if (!CodeGenTypes::VerifyFuncTypeComplete(FPT)) { |
| 456 | // The function has a computable LLVM signature; use the correct type. |
| 457 | Ty = Types.GetFunctionType(Types.getFunctionInfo(MD), FPT->isVariadic()); |
| 458 | } else { |
| 459 | // Use an arbitrary non-function type to tell GetAddrOfFunction that the |
| 460 | // function type is incomplete. |
| 461 | Ty = ptrdiff_t; |
| 462 | } |
| 463 | |
| 464 | llvm::Constant *Addr = CGM.GetAddrOfFunction(MD, Ty); |
| 465 | MemPtr[0] = llvm::ConstantExpr::getPtrToInt(Addr, ptrdiff_t); |
| 466 | MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t, 0); |
| 467 | } |
John McCall | 875ab10 | 2010-08-22 06:43:33 +0000 | [diff] [blame] | 468 | |
| 469 | return llvm::ConstantStruct::get(CGM.getLLVMContext(), |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 470 | MemPtr, 2, /*Packed=*/false); |
John McCall | 875ab10 | 2010-08-22 06:43:33 +0000 | [diff] [blame] | 471 | } |
| 472 | |
John McCall | e9fd7eb | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 473 | /// The comparison algorithm is pretty easy: the member pointers are |
| 474 | /// the same if they're either bitwise identical *or* both null. |
| 475 | /// |
| 476 | /// ARM is different here only because null-ness is more complicated. |
| 477 | llvm::Value * |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame^] | 478 | ItaniumCXXABI::EmitMemberPointerComparison(CodeGenFunction &CGF, |
| 479 | llvm::Value *L, |
| 480 | llvm::Value *R, |
| 481 | const MemberPointerType *MPT, |
| 482 | bool Inequality) { |
John McCall | e9fd7eb | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 483 | CGBuilderTy &Builder = CGF.Builder; |
| 484 | |
John McCall | e9fd7eb | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 485 | llvm::ICmpInst::Predicate Eq; |
| 486 | llvm::Instruction::BinaryOps And, Or; |
| 487 | if (Inequality) { |
| 488 | Eq = llvm::ICmpInst::ICMP_NE; |
| 489 | And = llvm::Instruction::Or; |
| 490 | Or = llvm::Instruction::And; |
| 491 | } else { |
| 492 | Eq = llvm::ICmpInst::ICMP_EQ; |
| 493 | And = llvm::Instruction::And; |
| 494 | Or = llvm::Instruction::Or; |
| 495 | } |
| 496 | |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame^] | 497 | // Member data pointers are easy because there's a unique null |
| 498 | // value, so it just comes down to bitwise equality. |
| 499 | if (MPT->isMemberDataPointer()) |
| 500 | return Builder.CreateICmp(Eq, L, R); |
| 501 | |
| 502 | // For member function pointers, the tautologies are more complex. |
| 503 | // The Itanium tautology is: |
| 504 | // (L == R) <==> (L.ptr == R.ptr /\ (L.ptr == 0 \/ L.adj == R.adj)) |
| 505 | // The ARM tautology is: |
| 506 | // (L == R) <==> (L.ptr == R.ptr /\ |
| 507 | // (L.adj == R.adj \/ |
| 508 | // (L.ptr == 0 /\ ((L.adj|R.adj) & 1) == 0))) |
| 509 | // The inequality tautologies have exactly the same structure, except |
| 510 | // applying De Morgan's laws. |
| 511 | |
| 512 | llvm::Value *LPtr = Builder.CreateExtractValue(L, 0, "lhs.memptr.ptr"); |
| 513 | llvm::Value *RPtr = Builder.CreateExtractValue(R, 0, "rhs.memptr.ptr"); |
| 514 | |
John McCall | e9fd7eb | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 515 | // This condition tests whether L.ptr == R.ptr. This must always be |
| 516 | // true for equality to hold. |
| 517 | llvm::Value *PtrEq = Builder.CreateICmp(Eq, LPtr, RPtr, "cmp.ptr"); |
| 518 | |
| 519 | // This condition, together with the assumption that L.ptr == R.ptr, |
| 520 | // tests whether the pointers are both null. ARM imposes an extra |
| 521 | // condition. |
| 522 | llvm::Value *Zero = llvm::Constant::getNullValue(LPtr->getType()); |
| 523 | llvm::Value *EqZero = Builder.CreateICmp(Eq, LPtr, Zero, "cmp.ptr.null"); |
| 524 | |
| 525 | // This condition tests whether L.adj == R.adj. If this isn't |
| 526 | // true, the pointers are unequal unless they're both null. |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 527 | llvm::Value *LAdj = Builder.CreateExtractValue(L, 1, "lhs.memptr.adj"); |
| 528 | llvm::Value *RAdj = Builder.CreateExtractValue(R, 1, "rhs.memptr.adj"); |
John McCall | e9fd7eb | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 529 | llvm::Value *AdjEq = Builder.CreateICmp(Eq, LAdj, RAdj, "cmp.adj"); |
| 530 | |
| 531 | // Null member function pointers on ARM clear the low bit of Adj, |
| 532 | // so the zero condition has to check that neither low bit is set. |
| 533 | if (IsARM) { |
| 534 | llvm::Value *One = llvm::ConstantInt::get(LPtr->getType(), 1); |
| 535 | |
| 536 | // Compute (l.adj | r.adj) & 1 and test it against zero. |
| 537 | llvm::Value *OrAdj = Builder.CreateOr(LAdj, RAdj, "or.adj"); |
| 538 | llvm::Value *OrAdjAnd1 = Builder.CreateAnd(OrAdj, One); |
| 539 | llvm::Value *OrAdjAnd1EqZero = Builder.CreateICmp(Eq, OrAdjAnd1, Zero, |
| 540 | "cmp.or.adj"); |
| 541 | EqZero = Builder.CreateBinOp(And, EqZero, OrAdjAnd1EqZero); |
| 542 | } |
| 543 | |
| 544 | // Tie together all our conditions. |
| 545 | llvm::Value *Result = Builder.CreateBinOp(Or, EqZero, AdjEq); |
| 546 | Result = Builder.CreateBinOp(And, PtrEq, Result, |
| 547 | Inequality ? "memptr.ne" : "memptr.eq"); |
| 548 | return Result; |
| 549 | } |
| 550 | |
| 551 | llvm::Value * |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame^] | 552 | ItaniumCXXABI::EmitMemberPointerIsNotNull(CodeGenFunction &CGF, |
| 553 | llvm::Value *MemPtr, |
| 554 | const MemberPointerType *MPT) { |
John McCall | e9fd7eb | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 555 | CGBuilderTy &Builder = CGF.Builder; |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame^] | 556 | |
| 557 | /// For member data pointers, this is just a check against -1. |
| 558 | if (MPT->isMemberDataPointer()) { |
| 559 | assert(MemPtr->getType() == getPtrDiffTy()); |
| 560 | llvm::Value *NegativeOne = |
| 561 | llvm::Constant::getAllOnesValue(MemPtr->getType()); |
| 562 | return Builder.CreateICmpNE(MemPtr, NegativeOne, "memptr.tobool"); |
| 563 | } |
John McCall | e9fd7eb | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 564 | |
| 565 | // In Itanium, a member function pointer is null if 'ptr' is null. |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 566 | llvm::Value *Ptr = Builder.CreateExtractValue(MemPtr, 0, "memptr.ptr"); |
John McCall | e9fd7eb | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 567 | |
| 568 | llvm::Constant *Zero = llvm::ConstantInt::get(Ptr->getType(), 0); |
| 569 | llvm::Value *Result = Builder.CreateICmpNE(Ptr, Zero, "memptr.tobool"); |
| 570 | |
| 571 | // In ARM, it's that, plus the low bit of 'adj' must be zero. |
| 572 | if (IsARM) { |
| 573 | llvm::Constant *One = llvm::ConstantInt::get(Ptr->getType(), 1); |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 574 | llvm::Value *Adj = Builder.CreateExtractValue(MemPtr, 1, "memptr.adj"); |
John McCall | e9fd7eb | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 575 | llvm::Value *VirtualBit = Builder.CreateAnd(Adj, One, "memptr.virtualbit"); |
| 576 | llvm::Value *IsNotVirtual = Builder.CreateICmpEQ(VirtualBit, Zero, |
| 577 | "memptr.notvirtual"); |
| 578 | Result = Builder.CreateAnd(Result, IsNotVirtual); |
| 579 | } |
| 580 | |
| 581 | return Result; |
| 582 | } |
John McCall | 875ab10 | 2010-08-22 06:43:33 +0000 | [diff] [blame] | 583 | |
John McCall | f16aa10 | 2010-08-22 21:01:12 +0000 | [diff] [blame] | 584 | /// The Itanium ABI requires non-zero initialization only for data |
| 585 | /// member pointers, for which '0' is a valid offset. |
| 586 | bool ItaniumCXXABI::isZeroInitializable(const MemberPointerType *MPT) { |
| 587 | return MPT->getPointeeType()->isFunctionType(); |
John McCall | cf2c85e | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 588 | } |