Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 1 | //===------- MicrosoftCXXABI.cpp - AST support for the Microsoft C++ ABI --===// |
| 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 | // |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 10 | // This provides C++ AST support targeting the Microsoft Visual C++ |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 11 | // ABI. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "CXXABI.h" |
Reid Kleckner | 84e9ab4 | 2013-03-28 20:02:56 +0000 | [diff] [blame] | 16 | #include "clang/AST/Attr.h" |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTContext.h" |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclCXX.h" |
Anders Carlsson | dae0cb5 | 2010-11-25 01:51:53 +0000 | [diff] [blame] | 19 | #include "clang/AST/RecordLayout.h" |
| 20 | #include "clang/AST/Type.h" |
| 21 | #include "clang/Basic/TargetInfo.h" |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 22 | |
| 23 | using namespace clang; |
| 24 | |
| 25 | namespace { |
| 26 | class MicrosoftCXXABI : public CXXABI { |
| 27 | ASTContext &Context; |
| 28 | public: |
| 29 | MicrosoftCXXABI(ASTContext &Ctx) : Context(Ctx) { } |
| 30 | |
Reid Kleckner | 84e9ab4 | 2013-03-28 20:02:56 +0000 | [diff] [blame] | 31 | std::pair<uint64_t, unsigned> |
| 32 | getMemberPointerWidthAndAlign(const MemberPointerType *MPT) const; |
Charles Davis | 424ae98 | 2010-10-29 03:25:11 +0000 | [diff] [blame] | 33 | |
Timur Iskhodzhanov | 8f88a1d | 2012-07-12 09:50:54 +0000 | [diff] [blame] | 34 | CallingConv getDefaultMethodCallConv(bool isVariadic) const { |
| 35 | if (!isVariadic && Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86) |
Charles Davis | 424ae98 | 2010-10-29 03:25:11 +0000 | [diff] [blame] | 36 | return CC_X86ThisCall; |
| 37 | else |
| 38 | return CC_C; |
| 39 | } |
Anders Carlsson | dae0cb5 | 2010-11-25 01:51:53 +0000 | [diff] [blame] | 40 | |
| 41 | bool isNearlyEmpty(const CXXRecordDecl *RD) const { |
| 42 | // FIXME: Audit the corners |
| 43 | if (!RD->isDynamicClass()) |
| 44 | return false; |
| 45 | |
| 46 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 47 | |
| 48 | // In the Microsoft ABI, classes can have one or two vtable pointers. |
Ken Dyck | 5c3633f | 2011-02-01 01:52:10 +0000 | [diff] [blame] | 49 | CharUnits PointerSize = |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 50 | Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0)); |
Ken Dyck | 5c3633f | 2011-02-01 01:52:10 +0000 | [diff] [blame] | 51 | return Layout.getNonVirtualSize() == PointerSize || |
| 52 | Layout.getNonVirtualSize() == PointerSize * 2; |
Anders Carlsson | dae0cb5 | 2010-11-25 01:51:53 +0000 | [diff] [blame] | 53 | } |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 54 | }; |
| 55 | } |
| 56 | |
Reid Kleckner | 84e9ab4 | 2013-03-28 20:02:56 +0000 | [diff] [blame] | 57 | // getNumBases() seems to only give us the number of direct bases, and not the |
| 58 | // total. This function tells us if we inherit from anybody that uses MI, or if |
| 59 | // we have a non-primary base class, which uses the multiple inheritance model. |
Reid Kleckner | 4410489 | 2013-04-02 16:23:57 +0000 | [diff] [blame] | 60 | bool usesMultipleInheritanceModel(const CXXRecordDecl *RD) { |
Reid Kleckner | 84e9ab4 | 2013-03-28 20:02:56 +0000 | [diff] [blame] | 61 | while (RD->getNumBases() > 0) { |
| 62 | if (RD->getNumBases() > 1) |
| 63 | return true; |
| 64 | assert(RD->getNumBases() == 1); |
| 65 | const CXXRecordDecl *Base = RD->bases_begin()->getType()->getAsCXXRecordDecl(); |
| 66 | if (RD->isPolymorphic() && !Base->isPolymorphic()) |
| 67 | return true; |
| 68 | RD = Base; |
| 69 | } |
| 70 | return false; |
| 71 | } |
| 72 | |
Reid Kleckner | 4410489 | 2013-04-02 16:23:57 +0000 | [diff] [blame] | 73 | MSInheritanceModel MSInheritanceAttrToModel(attr::Kind Kind) { |
| 74 | switch (Kind) { |
| 75 | default: llvm_unreachable("expected MS inheritance attribute"); |
| 76 | case attr::SingleInheritance: return IHM_Single; |
| 77 | case attr::MultipleInheritance: return IHM_Multiple; |
| 78 | case attr::VirtualInheritance: return IHM_Virtual; |
| 79 | case attr::UnspecifiedInheritance: return IHM_Unspecified; |
Reid Kleckner | 84e9ab4 | 2013-03-28 20:02:56 +0000 | [diff] [blame] | 80 | } |
Reid Kleckner | 4410489 | 2013-04-02 16:23:57 +0000 | [diff] [blame] | 81 | } |
Reid Kleckner | 84e9ab4 | 2013-03-28 20:02:56 +0000 | [diff] [blame] | 82 | |
Reid Kleckner | 4410489 | 2013-04-02 16:23:57 +0000 | [diff] [blame] | 83 | MSInheritanceModel CXXRecordDecl::getMSInheritanceModel() const { |
| 84 | Attr *IA = this->getAttr<MSInheritanceAttr>(); |
| 85 | if (IA) |
| 86 | return MSInheritanceAttrToModel(IA->getKind()); |
| 87 | // If there was no explicit attribute, the record must be defined already, and |
| 88 | // we can figure out the inheritance model from its other properties. |
| 89 | if (this->getNumVBases() > 0) |
| 90 | return IHM_Virtual; |
| 91 | if (usesMultipleInheritanceModel(this)) |
| 92 | return IHM_Multiple; |
| 93 | return IHM_Single; |
| 94 | } |
| 95 | |
| 96 | // Returns the number of pointer and integer slots used to represent a member |
| 97 | // pointer in the MS C++ ABI. |
| 98 | // |
| 99 | // Member function pointers have the following general form; however, fields |
| 100 | // are dropped as permitted (under the MSVC interpretation) by the inheritance |
| 101 | // model of the actual class. |
| 102 | // |
| 103 | // struct { |
| 104 | // // A pointer to the member function to call. If the member function is |
| 105 | // // virtual, this will be a thunk that forwards to the appropriate vftable |
| 106 | // // slot. |
| 107 | // void *FunctionPointerOrVirtualThunk; |
| 108 | // |
| 109 | // // An offset to add to the address of the vbtable pointer after (possibly) |
| 110 | // // selecting the virtual base but before resolving and calling the function. |
| 111 | // // Only needed if the class has any virtual bases or bases at a non-zero |
| 112 | // // offset. |
| 113 | // int NonVirtualBaseAdjustment; |
| 114 | // |
| 115 | // // An offset within the vb-table that selects the virtual base containing |
| 116 | // // the member. Loading from this offset produces a new offset that is |
| 117 | // // added to the address of the vb-table pointer to produce the base. |
| 118 | // int VirtualBaseAdjustmentOffset; |
| 119 | // |
| 120 | // // The offset of the vb-table pointer within the object. Only needed for |
| 121 | // // incomplete types. |
| 122 | // int VBTableOffset; |
| 123 | // }; |
| 124 | std::pair<unsigned, unsigned> MemberPointerType::getMSMemberPointerSlots() const { |
| 125 | const CXXRecordDecl *RD = this->getClass()->getAsCXXRecordDecl(); |
| 126 | MSInheritanceModel Inheritance = RD->getMSInheritanceModel(); |
| 127 | unsigned Ptrs; |
| 128 | unsigned Ints = 0; |
| 129 | if (this->isMemberFunctionPointer()) { |
Reid Kleckner | 84e9ab4 | 2013-03-28 20:02:56 +0000 | [diff] [blame] | 130 | // Member function pointers are a struct of a function pointer followed by a |
| 131 | // variable number of ints depending on the inheritance model used. The |
| 132 | // function pointer is a real function if it is non-virtual and a vftable |
| 133 | // slot thunk if it is virtual. The ints select the object base passed for |
| 134 | // the 'this' pointer. |
Reid Kleckner | 4410489 | 2013-04-02 16:23:57 +0000 | [diff] [blame] | 135 | Ptrs = 1; // First slot is always a function pointer. |
Reid Kleckner | 84e9ab4 | 2013-03-28 20:02:56 +0000 | [diff] [blame] | 136 | switch (Inheritance) { |
Reid Kleckner | 84e9ab4 | 2013-03-28 20:02:56 +0000 | [diff] [blame] | 137 | default: llvm_unreachable("unknown inheritance model"); |
Reid Kleckner | 4410489 | 2013-04-02 16:23:57 +0000 | [diff] [blame] | 138 | case IHM_Unspecified: ++Ints; // VBTableOffset |
| 139 | case IHM_Virtual: ++Ints; // VirtualBaseAdjustmentOffset |
| 140 | case IHM_Multiple: ++Ints; // NonVirtualBaseAdjustment |
| 141 | case IHM_Single: break; // Nothing |
Reid Kleckner | 84e9ab4 | 2013-03-28 20:02:56 +0000 | [diff] [blame] | 142 | } |
| 143 | } else { |
| 144 | // Data pointers are an aggregate of ints. The first int is an offset |
| 145 | // followed by vbtable-related offsets. |
Reid Kleckner | 4410489 | 2013-04-02 16:23:57 +0000 | [diff] [blame] | 146 | Ptrs = 0; |
Reid Kleckner | 84e9ab4 | 2013-03-28 20:02:56 +0000 | [diff] [blame] | 147 | switch (Inheritance) { |
Reid Kleckner | 84e9ab4 | 2013-03-28 20:02:56 +0000 | [diff] [blame] | 148 | default: llvm_unreachable("unknown inheritance model"); |
Reid Kleckner | 4410489 | 2013-04-02 16:23:57 +0000 | [diff] [blame] | 149 | case IHM_Unspecified: ++Ints; // VBTableOffset |
| 150 | case IHM_Virtual: ++Ints; // VirtualBaseAdjustmentOffset |
| 151 | case IHM_Multiple: // Nothing |
| 152 | case IHM_Single: ++Ints; // Field offset |
Reid Kleckner | 84e9ab4 | 2013-03-28 20:02:56 +0000 | [diff] [blame] | 153 | } |
| 154 | } |
Reid Kleckner | 4410489 | 2013-04-02 16:23:57 +0000 | [diff] [blame] | 155 | return std::make_pair(Ptrs, Ints); |
| 156 | } |
Reid Kleckner | 84e9ab4 | 2013-03-28 20:02:56 +0000 | [diff] [blame] | 157 | |
Reid Kleckner | 4410489 | 2013-04-02 16:23:57 +0000 | [diff] [blame] | 158 | std::pair<uint64_t, unsigned> |
| 159 | MicrosoftCXXABI::getMemberPointerWidthAndAlign(const MemberPointerType *MPT) const { |
Reid Kleckner | 4410489 | 2013-04-02 16:23:57 +0000 | [diff] [blame] | 160 | const TargetInfo &Target = Context.getTargetInfo(); |
| 161 | assert(Target.getTriple().getArch() == llvm::Triple::x86 || |
| 162 | Target.getTriple().getArch() == llvm::Triple::x86_64); |
| 163 | unsigned Ptrs, Ints; |
| 164 | llvm::tie(Ptrs, Ints) = MPT->getMSMemberPointerSlots(); |
| 165 | // The nominal struct is laid out with pointers followed by ints and aligned |
| 166 | // to a pointer width if any are present and an int width otherwise. |
| 167 | unsigned PtrSize = Target.getPointerWidth(0); |
| 168 | unsigned IntSize = Target.getIntWidth(); |
| 169 | uint64_t Width = Ptrs * PtrSize + Ints * IntSize; |
| 170 | unsigned Align = Ptrs > 0 ? Target.getPointerAlign(0) : Target.getIntAlign(); |
| 171 | Width = llvm::RoundUpToAlignment(Width, Align); |
Reid Kleckner | 84e9ab4 | 2013-03-28 20:02:56 +0000 | [diff] [blame] | 172 | return std::make_pair(Width, Align); |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | CXXABI *clang::CreateMicrosoftCXXABI(ASTContext &Ctx) { |
| 176 | return new MicrosoftCXXABI(Ctx); |
| 177 | } |
| 178 | |