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: |
| 34 | CodeGenModule &CGM; |
Charles Davis | 3a811f1 | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 35 | CodeGen::MangleContext MangleCtx; |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame^] | 36 | bool IsARM; |
Charles Davis | 3a811f1 | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 37 | public: |
John McCall | babc9a9 | 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 | 3a811f1 | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 40 | |
| 41 | CodeGen::MangleContext &getMangleContext() { |
| 42 | return MangleCtx; |
| 43 | } |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 44 | |
| 45 | llvm::Value *EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF, |
| 46 | llvm::Value *&This, |
| 47 | llvm::Value *MemFnPtr, |
| 48 | const MemberPointerType *MPT); |
Charles Davis | 3a811f1 | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 49 | }; |
John McCall | ee79a4c | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 50 | |
| 51 | class ARMCXXABI : public ItaniumCXXABI { |
| 52 | public: |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame^] | 53 | ARMCXXABI(CodeGen::CodeGenModule &CGM) : ItaniumCXXABI(CGM, /*ARM*/ true) {} |
John McCall | ee79a4c | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 54 | }; |
Charles Davis | 3a811f1 | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 57 | CodeGen::CGCXXABI *CodeGen::CreateItaniumCXXABI(CodeGenModule &CGM) { |
Charles Davis | 3a811f1 | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 58 | return new ItaniumCXXABI(CGM); |
| 59 | } |
| 60 | |
John McCall | ee79a4c | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 61 | CodeGen::CGCXXABI *CodeGen::CreateARMCXXABI(CodeGenModule &CGM) { |
| 62 | return new ARMCXXABI(CGM); |
| 63 | } |
| 64 | |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame^] | 65 | /// In the Itanium and ARM ABIs, method pointers have the form: |
| 66 | /// struct { ptrdiff_t ptr; ptrdiff_t adj; } memptr; |
| 67 | /// |
| 68 | /// In the Itanium ABI: |
| 69 | /// - method pointers are virtual if (memptr.ptr & 1) is nonzero |
| 70 | /// - the this-adjustment is (memptr.adj) |
| 71 | /// - the virtual offset is (memptr.ptr - 1) |
| 72 | /// |
| 73 | /// In the ARM ABI: |
| 74 | /// - method pointers are virtual if (memptr.adj & 1) is nonzero |
| 75 | /// - the this-adjustment is (memptr.adj >> 1) |
| 76 | /// - the virtual offset is (memptr.ptr) |
| 77 | /// ARM uses 'adj' for the virtual flag because Thumb functions |
| 78 | /// may be only single-byte aligned. |
| 79 | /// |
| 80 | /// If the member is virtual, the adjusted 'this' pointer points |
| 81 | /// to a vtable pointer from which the virtual offset is applied. |
| 82 | /// |
| 83 | /// If the member is non-virtual, memptr.ptr is the address of |
| 84 | /// the function to call. |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 85 | llvm::Value * |
| 86 | ItaniumCXXABI::EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF, |
| 87 | llvm::Value *&This, |
| 88 | llvm::Value *MemFnPtr, |
| 89 | const MemberPointerType *MPT) { |
| 90 | CGBuilderTy &Builder = CGF.Builder; |
| 91 | |
| 92 | const FunctionProtoType *FPT = |
| 93 | MPT->getPointeeType()->getAs<FunctionProtoType>(); |
| 94 | const CXXRecordDecl *RD = |
| 95 | cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl()); |
| 96 | |
| 97 | const llvm::FunctionType *FTy = |
| 98 | CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(RD, FPT), |
| 99 | FPT->isVariadic()); |
| 100 | |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame^] | 101 | const llvm::IntegerType *ptrdiff = CGF.IntPtrTy; |
| 102 | llvm::Constant *ptrdiff_1 = llvm::ConstantInt::get(ptrdiff, 1); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 103 | |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame^] | 104 | llvm::BasicBlock *FnVirtual = CGF.createBasicBlock("memptr.virtual"); |
| 105 | llvm::BasicBlock *FnNonVirtual = CGF.createBasicBlock("memptr.nonvirtual"); |
| 106 | llvm::BasicBlock *FnEnd = CGF.createBasicBlock("memptr.end"); |
| 107 | |
| 108 | // Load memptr.adj, which is in the second field. |
| 109 | llvm::Value *RawAdj = Builder.CreateStructGEP(MemFnPtr, 1); |
| 110 | RawAdj = Builder.CreateLoad(RawAdj, "memptr.adj"); |
| 111 | |
| 112 | // Compute the true adjustment. |
| 113 | llvm::Value *Adj = RawAdj; |
| 114 | if (IsARM) |
| 115 | Adj = Builder.CreateAShr(Adj, ptrdiff_1, "memptr.adj.shifted"); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 116 | |
| 117 | // Apply the adjustment and cast back to the original struct type |
| 118 | // for consistency. |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame^] | 119 | llvm::Value *Ptr = Builder.CreateBitCast(This, Builder.getInt8PtrTy()); |
| 120 | Ptr = Builder.CreateInBoundsGEP(Ptr, Adj); |
| 121 | This = Builder.CreateBitCast(Ptr, This->getType(), "this.adjusted"); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 122 | |
| 123 | // Load the function pointer. |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame^] | 124 | llvm::Value *FnPtr = Builder.CreateStructGEP(MemFnPtr, 0); |
| 125 | llvm::Value *FnAsInt = Builder.CreateLoad(FnPtr, "memptr.ptr"); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 126 | |
| 127 | // If the LSB in the function pointer is 1, the function pointer points to |
| 128 | // a virtual function. |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame^] | 129 | llvm::Value *IsVirtual; |
| 130 | if (IsARM) |
| 131 | IsVirtual = Builder.CreateAnd(RawAdj, ptrdiff_1); |
| 132 | else |
| 133 | IsVirtual = Builder.CreateAnd(FnAsInt, ptrdiff_1); |
| 134 | IsVirtual = Builder.CreateIsNotNull(IsVirtual, "memptr.isvirtual"); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 135 | Builder.CreateCondBr(IsVirtual, FnVirtual, FnNonVirtual); |
| 136 | |
| 137 | // In the virtual path, the adjustment left 'This' pointing to the |
| 138 | // vtable of the correct base subobject. The "function pointer" is an |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame^] | 139 | // offset within the vtable (+1 for the virtual flag on non-ARM). |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 140 | CGF.EmitBlock(FnVirtual); |
| 141 | |
| 142 | // Cast the adjusted this to a pointer to vtable pointer and load. |
| 143 | const llvm::Type *VTableTy = Builder.getInt8PtrTy(); |
| 144 | llvm::Value *VTable = Builder.CreateBitCast(This, VTableTy->getPointerTo()); |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame^] | 145 | VTable = Builder.CreateLoad(VTable, "memptr.vtable"); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 146 | |
| 147 | // Apply the offset. |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame^] | 148 | llvm::Value *VTableOffset = FnAsInt; |
| 149 | if (!IsARM) VTableOffset = Builder.CreateSub(VTableOffset, ptrdiff_1); |
| 150 | VTable = Builder.CreateGEP(VTable, VTableOffset); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 151 | |
| 152 | // Load the virtual function to call. |
| 153 | VTable = Builder.CreateBitCast(VTable, FTy->getPointerTo()->getPointerTo()); |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame^] | 154 | llvm::Value *VirtualFn = Builder.CreateLoad(VTable, "memptr.virtualfn"); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 155 | CGF.EmitBranch(FnEnd); |
| 156 | |
| 157 | // In the non-virtual path, the function pointer is actually a |
| 158 | // function pointer. |
| 159 | CGF.EmitBlock(FnNonVirtual); |
| 160 | llvm::Value *NonVirtualFn = |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame^] | 161 | Builder.CreateIntToPtr(FnAsInt, FTy->getPointerTo(), "memptr.nonvirtualfn"); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 162 | |
| 163 | // We're done. |
| 164 | CGF.EmitBlock(FnEnd); |
| 165 | llvm::PHINode *Callee = Builder.CreatePHI(FTy->getPointerTo()); |
| 166 | Callee->reserveOperandSpace(2); |
| 167 | Callee->addIncoming(VirtualFn, FnVirtual); |
| 168 | Callee->addIncoming(NonVirtualFn, FnNonVirtual); |
| 169 | return Callee; |
| 170 | } |