Charles Davis | 4e786dd | 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 | 8635341 | 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 | 4e786dd | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 19 | //===----------------------------------------------------------------------===// |
| 20 | |
| 21 | #include "CGCXXABI.h" |
John McCall | 475999d | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 22 | #include "CodeGenFunction.h" |
Charles Davis | 4e786dd | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 23 | #include "CodeGenModule.h" |
| 24 | #include "Mangle.h" |
John McCall | 475999d | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 25 | #include <clang/AST/Type.h> |
| 26 | #include <llvm/Value.h> |
Charles Davis | 4e786dd | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 27 | |
| 28 | using namespace clang; |
John McCall | 475999d | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 29 | using namespace CodeGen; |
Charles Davis | 4e786dd | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 30 | |
| 31 | namespace { |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 32 | class ItaniumCXXABI : public CodeGen::CGCXXABI { |
John McCall | 475999d | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 33 | protected: |
| 34 | CodeGenModule &CGM; |
Charles Davis | 4e786dd | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 35 | CodeGen::MangleContext MangleCtx; |
John McCall | d9c6c0b | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 36 | bool IsARM; |
Charles Davis | 4e786dd | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 37 | public: |
John McCall | d9c6c0b | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 38 | ItaniumCXXABI(CodeGen::CodeGenModule &CGM, bool IsARM = false) : |
| 39 | CGM(CGM), MangleCtx(CGM.getContext(), CGM.getDiags()), IsARM(IsARM) { } |
Charles Davis | 4e786dd | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 40 | |
| 41 | CodeGen::MangleContext &getMangleContext() { |
| 42 | return MangleCtx; |
| 43 | } |
John McCall | 475999d | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 44 | |
John McCall | 84fa510 | 2010-08-22 04:16:24 +0000 | [diff] [blame^] | 45 | bool RequiresNonZeroInitializer(QualType T); |
| 46 | bool RequiresNonZeroInitializer(const CXXRecordDecl *D); |
| 47 | |
John McCall | 475999d | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 48 | llvm::Value *EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF, |
| 49 | llvm::Value *&This, |
| 50 | llvm::Value *MemFnPtr, |
| 51 | const MemberPointerType *MPT); |
John McCall | a8bbb82 | 2010-08-22 03:04:22 +0000 | [diff] [blame] | 52 | |
John McCall | 84fa510 | 2010-08-22 04:16:24 +0000 | [diff] [blame^] | 53 | void EmitMemberFunctionPointerConversion(CodeGenFunction &CGF, |
| 54 | const CastExpr *E, |
| 55 | llvm::Value *Src, |
| 56 | llvm::Value *Dest, |
| 57 | bool VolatileDest); |
| 58 | |
| 59 | llvm::Constant *EmitMemberFunctionPointerConversion(llvm::Constant *C, |
| 60 | const CastExpr *E); |
| 61 | |
| 62 | void EmitNullMemberFunctionPointer(CodeGenFunction &CGF, |
| 63 | const MemberPointerType *MPT, |
| 64 | llvm::Value *Dest, |
| 65 | bool VolatileDest); |
| 66 | |
| 67 | llvm::Constant *EmitNullMemberFunctionPointer(const MemberPointerType *MPT); |
| 68 | |
Charles Davis | 4e786dd | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 69 | }; |
John McCall | 8635341 | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 70 | |
| 71 | class ARMCXXABI : public ItaniumCXXABI { |
| 72 | public: |
John McCall | d9c6c0b | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 73 | ARMCXXABI(CodeGen::CodeGenModule &CGM) : ItaniumCXXABI(CGM, /*ARM*/ true) {} |
John McCall | 8635341 | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 74 | }; |
Charles Davis | 4e786dd | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 77 | CodeGen::CGCXXABI *CodeGen::CreateItaniumCXXABI(CodeGenModule &CGM) { |
Charles Davis | 4e786dd | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 78 | return new ItaniumCXXABI(CGM); |
| 79 | } |
| 80 | |
John McCall | 8635341 | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 81 | CodeGen::CGCXXABI *CodeGen::CreateARMCXXABI(CodeGenModule &CGM) { |
| 82 | return new ARMCXXABI(CGM); |
| 83 | } |
| 84 | |
John McCall | d9c6c0b | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 85 | /// In the Itanium and ARM ABIs, method pointers have the form: |
| 86 | /// struct { ptrdiff_t ptr; ptrdiff_t adj; } memptr; |
| 87 | /// |
| 88 | /// In the Itanium ABI: |
| 89 | /// - method pointers are virtual if (memptr.ptr & 1) is nonzero |
| 90 | /// - the this-adjustment is (memptr.adj) |
| 91 | /// - the virtual offset is (memptr.ptr - 1) |
| 92 | /// |
| 93 | /// In the ARM ABI: |
| 94 | /// - method pointers are virtual if (memptr.adj & 1) is nonzero |
| 95 | /// - the this-adjustment is (memptr.adj >> 1) |
| 96 | /// - the virtual offset is (memptr.ptr) |
| 97 | /// ARM uses 'adj' for the virtual flag because Thumb functions |
| 98 | /// may be only single-byte aligned. |
| 99 | /// |
| 100 | /// If the member is virtual, the adjusted 'this' pointer points |
| 101 | /// to a vtable pointer from which the virtual offset is applied. |
| 102 | /// |
| 103 | /// If the member is non-virtual, memptr.ptr is the address of |
| 104 | /// the function to call. |
John McCall | 475999d | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 105 | llvm::Value * |
| 106 | ItaniumCXXABI::EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF, |
| 107 | llvm::Value *&This, |
| 108 | llvm::Value *MemFnPtr, |
| 109 | const MemberPointerType *MPT) { |
| 110 | CGBuilderTy &Builder = CGF.Builder; |
| 111 | |
| 112 | const FunctionProtoType *FPT = |
| 113 | MPT->getPointeeType()->getAs<FunctionProtoType>(); |
| 114 | const CXXRecordDecl *RD = |
| 115 | cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl()); |
| 116 | |
| 117 | const llvm::FunctionType *FTy = |
| 118 | CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(RD, FPT), |
| 119 | FPT->isVariadic()); |
| 120 | |
John McCall | d9c6c0b | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 121 | const llvm::IntegerType *ptrdiff = CGF.IntPtrTy; |
| 122 | llvm::Constant *ptrdiff_1 = llvm::ConstantInt::get(ptrdiff, 1); |
John McCall | 475999d | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 123 | |
John McCall | d9c6c0b | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 124 | llvm::BasicBlock *FnVirtual = CGF.createBasicBlock("memptr.virtual"); |
| 125 | llvm::BasicBlock *FnNonVirtual = CGF.createBasicBlock("memptr.nonvirtual"); |
| 126 | llvm::BasicBlock *FnEnd = CGF.createBasicBlock("memptr.end"); |
| 127 | |
| 128 | // Load memptr.adj, which is in the second field. |
| 129 | llvm::Value *RawAdj = Builder.CreateStructGEP(MemFnPtr, 1); |
| 130 | RawAdj = Builder.CreateLoad(RawAdj, "memptr.adj"); |
| 131 | |
| 132 | // Compute the true adjustment. |
| 133 | llvm::Value *Adj = RawAdj; |
| 134 | if (IsARM) |
| 135 | Adj = Builder.CreateAShr(Adj, ptrdiff_1, "memptr.adj.shifted"); |
John McCall | 475999d | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 136 | |
| 137 | // Apply the adjustment and cast back to the original struct type |
| 138 | // for consistency. |
John McCall | d9c6c0b | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 139 | llvm::Value *Ptr = Builder.CreateBitCast(This, Builder.getInt8PtrTy()); |
| 140 | Ptr = Builder.CreateInBoundsGEP(Ptr, Adj); |
| 141 | This = Builder.CreateBitCast(Ptr, This->getType(), "this.adjusted"); |
John McCall | 475999d | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 142 | |
| 143 | // Load the function pointer. |
John McCall | d9c6c0b | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 144 | llvm::Value *FnPtr = Builder.CreateStructGEP(MemFnPtr, 0); |
| 145 | llvm::Value *FnAsInt = Builder.CreateLoad(FnPtr, "memptr.ptr"); |
John McCall | 475999d | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 146 | |
| 147 | // If the LSB in the function pointer is 1, the function pointer points to |
| 148 | // a virtual function. |
John McCall | d9c6c0b | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 149 | llvm::Value *IsVirtual; |
| 150 | if (IsARM) |
| 151 | IsVirtual = Builder.CreateAnd(RawAdj, ptrdiff_1); |
| 152 | else |
| 153 | IsVirtual = Builder.CreateAnd(FnAsInt, ptrdiff_1); |
| 154 | IsVirtual = Builder.CreateIsNotNull(IsVirtual, "memptr.isvirtual"); |
John McCall | 475999d | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 155 | Builder.CreateCondBr(IsVirtual, FnVirtual, FnNonVirtual); |
| 156 | |
| 157 | // In the virtual path, the adjustment left 'This' pointing to the |
| 158 | // vtable of the correct base subobject. The "function pointer" is an |
John McCall | d9c6c0b | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 159 | // offset within the vtable (+1 for the virtual flag on non-ARM). |
John McCall | 475999d | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 160 | CGF.EmitBlock(FnVirtual); |
| 161 | |
| 162 | // Cast the adjusted this to a pointer to vtable pointer and load. |
| 163 | const llvm::Type *VTableTy = Builder.getInt8PtrTy(); |
| 164 | llvm::Value *VTable = Builder.CreateBitCast(This, VTableTy->getPointerTo()); |
John McCall | d9c6c0b | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 165 | VTable = Builder.CreateLoad(VTable, "memptr.vtable"); |
John McCall | 475999d | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 166 | |
| 167 | // Apply the offset. |
John McCall | d9c6c0b | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 168 | llvm::Value *VTableOffset = FnAsInt; |
| 169 | if (!IsARM) VTableOffset = Builder.CreateSub(VTableOffset, ptrdiff_1); |
| 170 | VTable = Builder.CreateGEP(VTable, VTableOffset); |
John McCall | 475999d | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 171 | |
| 172 | // Load the virtual function to call. |
| 173 | VTable = Builder.CreateBitCast(VTable, FTy->getPointerTo()->getPointerTo()); |
John McCall | d9c6c0b | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 174 | llvm::Value *VirtualFn = Builder.CreateLoad(VTable, "memptr.virtualfn"); |
John McCall | 475999d | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 175 | CGF.EmitBranch(FnEnd); |
| 176 | |
| 177 | // In the non-virtual path, the function pointer is actually a |
| 178 | // function pointer. |
| 179 | CGF.EmitBlock(FnNonVirtual); |
| 180 | llvm::Value *NonVirtualFn = |
John McCall | d9c6c0b | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 181 | Builder.CreateIntToPtr(FnAsInt, FTy->getPointerTo(), "memptr.nonvirtualfn"); |
John McCall | 475999d | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 182 | |
| 183 | // We're done. |
| 184 | CGF.EmitBlock(FnEnd); |
| 185 | llvm::PHINode *Callee = Builder.CreatePHI(FTy->getPointerTo()); |
| 186 | Callee->reserveOperandSpace(2); |
| 187 | Callee->addIncoming(VirtualFn, FnVirtual); |
| 188 | Callee->addIncoming(NonVirtualFn, FnNonVirtual); |
| 189 | return Callee; |
| 190 | } |
John McCall | a8bbb82 | 2010-08-22 03:04:22 +0000 | [diff] [blame] | 191 | |
| 192 | /// Perform a derived-to-base or base-to-derived member pointer conversion. |
John McCall | 84fa510 | 2010-08-22 04:16:24 +0000 | [diff] [blame^] | 193 | void ItaniumCXXABI::EmitMemberFunctionPointerConversion(CodeGenFunction &CGF, |
| 194 | const CastExpr *E, |
| 195 | llvm::Value *Src, |
| 196 | llvm::Value *Dest, |
| 197 | bool VolatileDest) { |
John McCall | a8bbb82 | 2010-08-22 03:04:22 +0000 | [diff] [blame] | 198 | assert(E->getCastKind() == CastExpr::CK_DerivedToBaseMemberPointer || |
| 199 | E->getCastKind() == CastExpr::CK_BaseToDerivedMemberPointer); |
| 200 | |
| 201 | CGBuilderTy &Builder = CGF.Builder; |
| 202 | |
| 203 | const MemberPointerType *SrcTy = |
| 204 | E->getSubExpr()->getType()->getAs<MemberPointerType>(); |
| 205 | const MemberPointerType *DestTy = E->getType()->getAs<MemberPointerType>(); |
| 206 | |
| 207 | const CXXRecordDecl *SrcDecl = SrcTy->getClass()->getAsCXXRecordDecl(); |
| 208 | const CXXRecordDecl *DestDecl = DestTy->getClass()->getAsCXXRecordDecl(); |
| 209 | |
| 210 | llvm::Value *SrcPtr = Builder.CreateStructGEP(Src, 0, "src.ptr"); |
| 211 | SrcPtr = Builder.CreateLoad(SrcPtr); |
| 212 | |
| 213 | llvm::Value *SrcAdj = Builder.CreateStructGEP(Src, 1, "src.adj"); |
| 214 | SrcAdj = Builder.CreateLoad(SrcAdj); |
| 215 | |
| 216 | llvm::Value *DstPtr = Builder.CreateStructGEP(Dest, 0, "dst.ptr"); |
| 217 | Builder.CreateStore(SrcPtr, DstPtr, VolatileDest); |
| 218 | |
| 219 | llvm::Value *DstAdj = Builder.CreateStructGEP(Dest, 1, "dst.adj"); |
| 220 | |
| 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 | |
| 230 | if (llvm::Constant *Adj = |
| 231 | CGF.CGM.GetNonVirtualBaseClassOffset(DerivedDecl, |
| 232 | E->path_begin(), |
| 233 | E->path_end())) { |
| 234 | if (DerivedToBase) |
| 235 | SrcAdj = Builder.CreateSub(SrcAdj, Adj, "adj"); |
| 236 | else |
| 237 | SrcAdj = Builder.CreateAdd(SrcAdj, Adj, "adj"); |
| 238 | } |
| 239 | |
| 240 | Builder.CreateStore(SrcAdj, DstAdj, VolatileDest); |
| 241 | } |
John McCall | 84fa510 | 2010-08-22 04:16:24 +0000 | [diff] [blame^] | 242 | |
| 243 | llvm::Constant * |
| 244 | ItaniumCXXABI::EmitMemberFunctionPointerConversion(llvm::Constant *C, |
| 245 | const CastExpr *E) { |
| 246 | const MemberPointerType *SrcTy = |
| 247 | E->getSubExpr()->getType()->getAs<MemberPointerType>(); |
| 248 | const MemberPointerType *DestTy = |
| 249 | E->getType()->getAs<MemberPointerType>(); |
| 250 | |
| 251 | bool DerivedToBase = |
| 252 | E->getCastKind() == CastExpr::CK_DerivedToBaseMemberPointer; |
| 253 | |
| 254 | const CXXRecordDecl *DerivedDecl; |
| 255 | if (DerivedToBase) |
| 256 | DerivedDecl = SrcTy->getClass()->getAsCXXRecordDecl(); |
| 257 | else |
| 258 | DerivedDecl = DestTy->getClass()->getAsCXXRecordDecl(); |
| 259 | |
| 260 | // Calculate the offset to the base class. |
| 261 | llvm::Constant *Offset = |
| 262 | CGM.GetNonVirtualBaseClassOffset(DerivedDecl, |
| 263 | E->path_begin(), |
| 264 | E->path_end()); |
| 265 | // If there's no offset, we're done. |
| 266 | if (!Offset) return C; |
| 267 | |
| 268 | llvm::ConstantStruct *CS = cast<llvm::ConstantStruct>(C); |
| 269 | |
| 270 | llvm::Constant *Values[2] = { |
| 271 | CS->getOperand(0), |
| 272 | llvm::ConstantExpr::getAdd(CS->getOperand(1), Offset) |
| 273 | }; |
| 274 | return llvm::ConstantStruct::get(CGM.getLLVMContext(), Values, 2, |
| 275 | /*Packed=*/false); |
| 276 | } |
| 277 | |
| 278 | |
| 279 | void ItaniumCXXABI::EmitNullMemberFunctionPointer(CodeGenFunction &CGF, |
| 280 | const MemberPointerType *MPT, |
| 281 | llvm::Value *Dest, |
| 282 | bool VolatileDest) { |
| 283 | // Should this be "unabstracted" and implemented in terms of the |
| 284 | // Constant version? |
| 285 | |
| 286 | CGBuilderTy &Builder = CGF.Builder; |
| 287 | |
| 288 | const llvm::IntegerType *PtrDiffTy = CGF.IntPtrTy; |
| 289 | llvm::Value *Zero = llvm::ConstantInt::get(PtrDiffTy, 0); |
| 290 | |
| 291 | llvm::Value *Ptr = Builder.CreateStructGEP(Dest, 0, "ptr"); |
| 292 | Builder.CreateStore(Zero, Ptr, VolatileDest); |
| 293 | |
| 294 | llvm::Value *Adj = Builder.CreateStructGEP(Dest, 1, "adj"); |
| 295 | Builder.CreateStore(Zero, Adj, VolatileDest); |
| 296 | } |
| 297 | |
| 298 | llvm::Constant * |
| 299 | ItaniumCXXABI::EmitNullMemberFunctionPointer(const MemberPointerType *MPT) { |
| 300 | return CGM.EmitNullConstant(QualType(MPT, 0)); |
| 301 | } |
| 302 | |
| 303 | bool ItaniumCXXABI::RequiresNonZeroInitializer(QualType T) { |
| 304 | return CGM.getTypes().ContainsPointerToDataMember(T); |
| 305 | } |
| 306 | |
| 307 | bool ItaniumCXXABI::RequiresNonZeroInitializer(const CXXRecordDecl *D) { |
| 308 | return CGM.getTypes().ContainsPointerToDataMember(D); |
| 309 | } |