Charles Davis | 74ce859 | 2010-06-09 23:25:41 +0000 | [diff] [blame] | 1 | //===--- MicrosoftCXXABI.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 | // |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 10 | // This provides C++ code generation targeting the Microsoft Visual C++ ABI. |
Charles Davis | 74ce859 | 2010-06-09 23:25:41 +0000 | [diff] [blame] | 11 | // The class in this file generates structures that follow the Microsoft |
| 12 | // Visual C++ ABI, which is actually not very well documented at all outside |
| 13 | // of Microsoft. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
| 17 | #include "CGCXXABI.h" |
Reid Kleckner | 7810af0 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 18 | #include "CGVTables.h" |
Chandler Carruth | 5553d0d | 2014-01-07 11:51:46 +0000 | [diff] [blame] | 19 | #include "CodeGenModule.h" |
David Majnemer | e7a818f | 2015-03-06 18:53:55 +0000 | [diff] [blame] | 20 | #include "CodeGenTypes.h" |
Reid Kleckner | fff8e7f | 2015-03-03 19:21:04 +0000 | [diff] [blame] | 21 | #include "TargetInfo.h" |
Charles Davis | 74ce859 | 2010-06-09 23:25:41 +0000 | [diff] [blame] | 22 | #include "clang/AST/Decl.h" |
| 23 | #include "clang/AST/DeclCXX.h" |
Reid Kleckner | fff8e7f | 2015-03-03 19:21:04 +0000 | [diff] [blame] | 24 | #include "clang/AST/StmtCXX.h" |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 25 | #include "clang/AST/VTableBuilder.h" |
David Majnemer | 611cdb9 | 2014-07-07 08:09:15 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/StringExtras.h" |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/StringSet.h" |
David Majnemer | 1162d25 | 2014-06-22 19:05:33 +0000 | [diff] [blame] | 28 | #include "llvm/IR/CallSite.h" |
Reid Kleckner | fff8e7f | 2015-03-03 19:21:04 +0000 | [diff] [blame] | 29 | #include "llvm/IR/Intrinsics.h" |
Charles Davis | 74ce859 | 2010-06-09 23:25:41 +0000 | [diff] [blame] | 30 | |
| 31 | using namespace clang; |
| 32 | using namespace CodeGen; |
| 33 | |
| 34 | namespace { |
| 35 | |
Reid Kleckner | b40a27d | 2014-01-03 00:14:35 +0000 | [diff] [blame] | 36 | /// Holds all the vbtable globals for a given class. |
| 37 | struct VBTableGlobals { |
Reid Kleckner | 9c6e9e3 | 2014-02-27 19:40:09 +0000 | [diff] [blame] | 38 | const VPtrInfoVector *VBTables; |
Reid Kleckner | b40a27d | 2014-01-03 00:14:35 +0000 | [diff] [blame] | 39 | SmallVector<llvm::GlobalVariable *, 2> Globals; |
| 40 | }; |
| 41 | |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 42 | class MicrosoftCXXABI : public CGCXXABI { |
Charles Davis | 74ce859 | 2010-06-09 23:25:41 +0000 | [diff] [blame] | 43 | public: |
David Majnemer | 611cdb9 | 2014-07-07 08:09:15 +0000 | [diff] [blame] | 44 | MicrosoftCXXABI(CodeGenModule &CGM) |
| 45 | : CGCXXABI(CGM), BaseClassDescriptorType(nullptr), |
| 46 | ClassHierarchyDescriptorType(nullptr), |
David Majnemer | 7c23707 | 2015-03-05 00:46:22 +0000 | [diff] [blame] | 47 | CompleteObjectLocatorType(nullptr), CatchableTypeType(nullptr), |
| 48 | ThrowInfoType(nullptr) {} |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 49 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 50 | bool HasThisReturn(GlobalDecl GD) const override; |
David Majnemer | 0c0b6d9 | 2014-10-31 20:09:12 +0000 | [diff] [blame] | 51 | bool hasMostDerivedReturn(GlobalDecl GD) const override; |
Stephen Lin | 9dc6eef | 2013-06-30 20:40:16 +0000 | [diff] [blame] | 52 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 53 | bool classifyReturnType(CGFunctionInfo &FI) const override; |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 54 | |
Reid Kleckner | e39ee21 | 2014-05-03 00:33:28 +0000 | [diff] [blame] | 55 | RecordArgABI getRecordArgABI(const CXXRecordDecl *RD) const override; |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 56 | |
Reid Kleckner | 37abaca | 2014-05-09 22:46:15 +0000 | [diff] [blame] | 57 | bool isSRetParameterAfterThis() const override { return true; } |
| 58 | |
David Majnemer | 196ac33 | 2014-09-11 23:05:02 +0000 | [diff] [blame] | 59 | size_t getSrcArgforCopyCtor(const CXXConstructorDecl *CD, |
| 60 | FunctionArgList &Args) const override { |
| 61 | assert(Args.size() >= 2 && |
| 62 | "expected the arglist to have at least two args!"); |
| 63 | // The 'most_derived' parameter goes second if the ctor is variadic and |
| 64 | // has v-bases. |
| 65 | if (CD->getParent()->getNumVBases() > 0 && |
| 66 | CD->getType()->castAs<FunctionProtoType>()->isVariadic()) |
| 67 | return 2; |
| 68 | return 1; |
| 69 | } |
| 70 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 71 | StringRef GetPureVirtualCallName() override { return "_purecall"; } |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 72 | StringRef GetDeletedVirtualCallName() override { return "_purecall"; } |
Joao Matos | 2ce88ef | 2012-07-17 17:10:11 +0000 | [diff] [blame] | 73 | |
David Majnemer | 0868137 | 2014-11-01 07:37:17 +0000 | [diff] [blame] | 74 | void emitVirtualObjectDelete(CodeGenFunction &CGF, const CXXDeleteExpr *DE, |
David Majnemer | 0c0b6d9 | 2014-10-31 20:09:12 +0000 | [diff] [blame] | 75 | llvm::Value *Ptr, QualType ElementType, |
David Majnemer | 0c0b6d9 | 2014-10-31 20:09:12 +0000 | [diff] [blame] | 76 | const CXXDestructorDecl *Dtor) override; |
John McCall | 82fb892 | 2012-09-25 10:10:39 +0000 | [diff] [blame] | 77 | |
Chandler Carruth | 4b9e857 | 2014-11-25 08:59:34 +0000 | [diff] [blame] | 78 | void emitRethrow(CodeGenFunction &CGF, bool isNoReturn) override; |
David Majnemer | 7c23707 | 2015-03-05 00:46:22 +0000 | [diff] [blame] | 79 | void emitThrow(CodeGenFunction &CGF, const CXXThrowExpr *E) override; |
David Majnemer | 442d0a2 | 2014-11-25 07:20:20 +0000 | [diff] [blame] | 80 | |
Reid Kleckner | fff8e7f | 2015-03-03 19:21:04 +0000 | [diff] [blame] | 81 | void emitBeginCatch(CodeGenFunction &CGF, const CXXCatchStmt *C) override; |
| 82 | |
David Majnemer | e2cb8d1 | 2014-07-07 06:20:47 +0000 | [diff] [blame] | 83 | llvm::GlobalVariable *getMSCompleteObjectLocator(const CXXRecordDecl *RD, |
| 84 | const VPtrInfo *Info); |
| 85 | |
| 86 | llvm::Constant *getAddrOfRTTIDescriptor(QualType Ty) override; |
| 87 | |
David Majnemer | 1162d25 | 2014-06-22 19:05:33 +0000 | [diff] [blame] | 88 | bool shouldTypeidBeNullChecked(bool IsDeref, QualType SrcRecordTy) override; |
| 89 | void EmitBadTypeidCall(CodeGenFunction &CGF) override; |
| 90 | llvm::Value *EmitTypeid(CodeGenFunction &CGF, QualType SrcRecordTy, |
| 91 | llvm::Value *ThisPtr, |
| 92 | llvm::Type *StdTypeInfoPtrTy) override; |
| 93 | |
| 94 | bool shouldDynamicCastCallBeNullChecked(bool SrcIsPtr, |
| 95 | QualType SrcRecordTy) override; |
| 96 | |
| 97 | llvm::Value *EmitDynamicCastCall(CodeGenFunction &CGF, llvm::Value *Value, |
| 98 | QualType SrcRecordTy, QualType DestTy, |
| 99 | QualType DestRecordTy, |
| 100 | llvm::BasicBlock *CastEnd) override; |
| 101 | |
| 102 | llvm::Value *EmitDynamicCastToVoid(CodeGenFunction &CGF, llvm::Value *Value, |
| 103 | QualType SrcRecordTy, |
| 104 | QualType DestTy) override; |
| 105 | |
| 106 | bool EmitBadCastCall(CodeGenFunction &CGF) override; |
| 107 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 108 | llvm::Value * |
| 109 | GetVirtualBaseClassOffset(CodeGenFunction &CGF, llvm::Value *This, |
| 110 | const CXXRecordDecl *ClassDecl, |
| 111 | const CXXRecordDecl *BaseClassDecl) override; |
Reid Kleckner | d8cbeec | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 112 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 113 | llvm::BasicBlock * |
| 114 | EmitCtorCompleteObjectHandler(CodeGenFunction &CGF, |
| 115 | const CXXRecordDecl *RD) override; |
Timur Iskhodzhanov | 57cbe5c | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 116 | |
Timur Iskhodzhanov | b648732 | 2013-10-09 18:16:58 +0000 | [diff] [blame] | 117 | void initializeHiddenVirtualInheritanceMembers(CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 118 | const CXXRecordDecl *RD) override; |
Timur Iskhodzhanov | b648732 | 2013-10-09 18:16:58 +0000 | [diff] [blame] | 119 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 120 | void EmitCXXConstructors(const CXXConstructorDecl *D) override; |
Timur Iskhodzhanov | 40f2fa9 | 2013-08-04 17:30:04 +0000 | [diff] [blame] | 121 | |
Reid Kleckner | e7de47e | 2013-07-22 13:51:44 +0000 | [diff] [blame] | 122 | // Background on MSVC destructors |
| 123 | // ============================== |
| 124 | // |
| 125 | // Both Itanium and MSVC ABIs have destructor variants. The variant names |
| 126 | // roughly correspond in the following way: |
| 127 | // Itanium Microsoft |
| 128 | // Base -> no name, just ~Class |
| 129 | // Complete -> vbase destructor |
| 130 | // Deleting -> scalar deleting destructor |
| 131 | // vector deleting destructor |
| 132 | // |
| 133 | // The base and complete destructors are the same as in Itanium, although the |
| 134 | // complete destructor does not accept a VTT parameter when there are virtual |
| 135 | // bases. A separate mechanism involving vtordisps is used to ensure that |
| 136 | // virtual methods of destroyed subobjects are not called. |
| 137 | // |
| 138 | // The deleting destructors accept an i32 bitfield as a second parameter. Bit |
| 139 | // 1 indicates if the memory should be deleted. Bit 2 indicates if the this |
| 140 | // pointer points to an array. The scalar deleting destructor assumes that |
| 141 | // bit 2 is zero, and therefore does not contain a loop. |
| 142 | // |
| 143 | // For virtual destructors, only one entry is reserved in the vftable, and it |
| 144 | // always points to the vector deleting destructor. The vector deleting |
| 145 | // destructor is the most general, so it can be used to destroy objects in |
| 146 | // place, delete single heap objects, or delete arrays. |
| 147 | // |
| 148 | // A TU defining a non-inline destructor is only guaranteed to emit a base |
| 149 | // destructor, and all of the other variants are emitted on an as-needed basis |
| 150 | // in COMDATs. Because a non-base destructor can be emitted in a TU that |
| 151 | // lacks a definition for the destructor, non-base destructors must always |
| 152 | // delegate to or alias the base destructor. |
| 153 | |
Rafael Espindola | 8d2a19b | 2014-09-08 16:01:27 +0000 | [diff] [blame] | 154 | void buildStructorSignature(const CXXMethodDecl *MD, StructorType T, |
| 155 | SmallVectorImpl<CanQualType> &ArgTys) override; |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 156 | |
Reid Kleckner | e7de47e | 2013-07-22 13:51:44 +0000 | [diff] [blame] | 157 | /// Non-base dtors should be emitted as delegating thunks in this ABI. |
| 158 | bool useThunkForDtorVariant(const CXXDestructorDecl *Dtor, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 159 | CXXDtorType DT) const override { |
Reid Kleckner | e7de47e | 2013-07-22 13:51:44 +0000 | [diff] [blame] | 160 | return DT != Dtor_Base; |
| 161 | } |
| 162 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 163 | void EmitCXXDestructors(const CXXDestructorDecl *D) override; |
Reid Kleckner | e7de47e | 2013-07-22 13:51:44 +0000 | [diff] [blame] | 164 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 165 | const CXXRecordDecl * |
| 166 | getThisArgumentTypeForMethod(const CXXMethodDecl *MD) override { |
Timur Iskhodzhanov | 88fd439 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 167 | MD = MD->getCanonicalDecl(); |
| 168 | if (MD->isVirtual() && !isa<CXXDestructorDecl>(MD)) { |
Timur Iskhodzhanov | 5877663 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 169 | MicrosoftVTableContext::MethodVFTableLocation ML = |
| 170 | CGM.getMicrosoftVTableContext().getMethodVFTableLocation(MD); |
Timur Iskhodzhanov | 88fd439 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 171 | // The vbases might be ordered differently in the final overrider object |
| 172 | // and the complete object, so the "this" argument may sometimes point to |
| 173 | // memory that has no particular type (e.g. past the complete object). |
| 174 | // In this case, we just use a generic pointer type. |
| 175 | // FIXME: might want to have a more precise type in the non-virtual |
| 176 | // multiple inheritance case. |
Timur Iskhodzhanov | 9e7f505 | 2013-11-07 13:34:02 +0000 | [diff] [blame] | 177 | if (ML.VBase || !ML.VFPtrOffset.isZero()) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 178 | return nullptr; |
Timur Iskhodzhanov | 88fd439 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 179 | } |
| 180 | return MD->getParent(); |
| 181 | } |
| 182 | |
Timur Iskhodzhanov | f174942 | 2014-03-14 17:43:37 +0000 | [diff] [blame] | 183 | llvm::Value * |
| 184 | adjustThisArgumentForVirtualFunctionCall(CodeGenFunction &CGF, GlobalDecl GD, |
| 185 | llvm::Value *This, |
| 186 | bool VirtualCall) override; |
Timur Iskhodzhanov | 88fd439 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 187 | |
Reid Kleckner | 89077a1 | 2013-12-17 19:46:40 +0000 | [diff] [blame] | 188 | void addImplicitStructorParams(CodeGenFunction &CGF, QualType &ResTy, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 189 | FunctionArgList &Params) override; |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 190 | |
Timur Iskhodzhanov | 88fd439 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 191 | llvm::Value *adjustThisParameterInVirtualFunctionPrologue( |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 192 | CodeGenFunction &CGF, GlobalDecl GD, llvm::Value *This) override; |
Timur Iskhodzhanov | 88fd439 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 193 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 194 | void EmitInstanceFunctionProlog(CodeGenFunction &CGF) override; |
John McCall | 2903675 | 2011-01-27 02:46:02 +0000 | [diff] [blame] | 195 | |
Reid Kleckner | 89077a1 | 2013-12-17 19:46:40 +0000 | [diff] [blame] | 196 | unsigned addImplicitConstructorArgs(CodeGenFunction &CGF, |
| 197 | const CXXConstructorDecl *D, |
| 198 | CXXCtorType Type, bool ForVirtualBase, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 199 | bool Delegating, |
| 200 | CallArgList &Args) override; |
Timur Iskhodzhanov | 88fd439 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 201 | |
Reid Kleckner | 6fe771a | 2013-12-13 00:53:54 +0000 | [diff] [blame] | 202 | void EmitDestructorCall(CodeGenFunction &CGF, const CXXDestructorDecl *DD, |
| 203 | CXXDtorType Type, bool ForVirtualBase, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 204 | bool Delegating, llvm::Value *This) override; |
Reid Kleckner | 6fe771a | 2013-12-13 00:53:54 +0000 | [diff] [blame] | 205 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 206 | void emitVTableDefinitions(CodeGenVTables &CGVT, |
| 207 | const CXXRecordDecl *RD) override; |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 208 | |
| 209 | llvm::Value *getVTableAddressPointInStructor( |
| 210 | CodeGenFunction &CGF, const CXXRecordDecl *VTableClass, |
| 211 | BaseSubobject Base, const CXXRecordDecl *NearestVBase, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 212 | bool &NeedsVirtualOffset) override; |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 213 | |
| 214 | llvm::Constant * |
| 215 | getVTableAddressPointForConstExpr(BaseSubobject Base, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 216 | const CXXRecordDecl *VTableClass) override; |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 217 | |
| 218 | llvm::GlobalVariable *getAddrOfVTable(const CXXRecordDecl *RD, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 219 | CharUnits VPtrOffset) override; |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 220 | |
Timur Iskhodzhanov | 88fd439 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 221 | llvm::Value *getVirtualFunctionPointer(CodeGenFunction &CGF, GlobalDecl GD, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 222 | llvm::Value *This, |
| 223 | llvm::Type *Ty) override; |
Timur Iskhodzhanov | 88fd439 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 224 | |
David Majnemer | 0c0b6d9 | 2014-10-31 20:09:12 +0000 | [diff] [blame] | 225 | llvm::Value *EmitVirtualDestructorCall(CodeGenFunction &CGF, |
| 226 | const CXXDestructorDecl *Dtor, |
| 227 | CXXDtorType DtorType, |
| 228 | llvm::Value *This, |
| 229 | const CXXMemberCallExpr *CE) override; |
Timur Iskhodzhanov | d619711 | 2013-02-15 14:45:22 +0000 | [diff] [blame] | 230 | |
Timur Iskhodzhanov | ad9d3b8 | 2013-10-09 09:23:58 +0000 | [diff] [blame] | 231 | void adjustCallArgsForDestructorThunk(CodeGenFunction &CGF, GlobalDecl GD, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 232 | CallArgList &CallArgs) override { |
Timur Iskhodzhanov | ad9d3b8 | 2013-10-09 09:23:58 +0000 | [diff] [blame] | 233 | assert(GD.getDtorType() == Dtor_Deleting && |
| 234 | "Only deleting destructor thunks are available in this ABI"); |
| 235 | CallArgs.add(RValue::get(getStructorImplicitParamValue(CGF)), |
David Majnemer | 9ced3dd | 2015-03-14 23:44:48 +0000 | [diff] [blame] | 236 | getContext().IntTy); |
Timur Iskhodzhanov | ad9d3b8 | 2013-10-09 09:23:58 +0000 | [diff] [blame] | 237 | } |
| 238 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 239 | void emitVirtualInheritanceTables(const CXXRecordDecl *RD) override; |
Reid Kleckner | 7810af0 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 240 | |
Reid Kleckner | b40a27d | 2014-01-03 00:14:35 +0000 | [diff] [blame] | 241 | llvm::GlobalVariable * |
Reid Kleckner | 9c6e9e3 | 2014-02-27 19:40:09 +0000 | [diff] [blame] | 242 | getAddrOfVBTable(const VPtrInfo &VBT, const CXXRecordDecl *RD, |
Reid Kleckner | b40a27d | 2014-01-03 00:14:35 +0000 | [diff] [blame] | 243 | llvm::GlobalVariable::LinkageTypes Linkage); |
| 244 | |
Reid Kleckner | 9c6e9e3 | 2014-02-27 19:40:09 +0000 | [diff] [blame] | 245 | void emitVBTableDefinition(const VPtrInfo &VBT, const CXXRecordDecl *RD, |
Reid Kleckner | b40a27d | 2014-01-03 00:14:35 +0000 | [diff] [blame] | 246 | llvm::GlobalVariable *GV) const; |
| 247 | |
Hans Wennborg | c94391d | 2014-06-06 20:04:01 +0000 | [diff] [blame] | 248 | void setThunkLinkage(llvm::Function *Thunk, bool ForVTable, |
| 249 | GlobalDecl GD, bool ReturnAdjustment) override { |
Hans Wennborg | 853ae94 | 2014-05-30 16:59:42 +0000 | [diff] [blame] | 250 | // Never dllimport/dllexport thunks. |
| 251 | Thunk->setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass); |
Hans Wennborg | c94391d | 2014-06-06 20:04:01 +0000 | [diff] [blame] | 252 | |
| 253 | GVALinkage Linkage = |
| 254 | getContext().GetGVALinkageForFunction(cast<FunctionDecl>(GD.getDecl())); |
| 255 | |
| 256 | if (Linkage == GVA_Internal) |
| 257 | Thunk->setLinkage(llvm::GlobalValue::InternalLinkage); |
| 258 | else if (ReturnAdjustment) |
| 259 | Thunk->setLinkage(llvm::GlobalValue::WeakODRLinkage); |
| 260 | else |
| 261 | Thunk->setLinkage(llvm::GlobalValue::LinkOnceODRLinkage); |
Timur Iskhodzhanov | ad9d3b8 | 2013-10-09 09:23:58 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Timur Iskhodzhanov | 0201432 | 2013-10-30 11:55:43 +0000 | [diff] [blame] | 264 | llvm::Value *performThisAdjustment(CodeGenFunction &CGF, llvm::Value *This, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 265 | const ThisAdjustment &TA) override; |
Timur Iskhodzhanov | 0201432 | 2013-10-30 11:55:43 +0000 | [diff] [blame] | 266 | |
| 267 | llvm::Value *performReturnAdjustment(CodeGenFunction &CGF, llvm::Value *Ret, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 268 | const ReturnAdjustment &RA) override; |
Timur Iskhodzhanov | 0201432 | 2013-10-30 11:55:43 +0000 | [diff] [blame] | 269 | |
David Majnemer | b3341ea | 2014-10-05 05:05:40 +0000 | [diff] [blame] | 270 | void EmitThreadLocalInitFuncs( |
| 271 | CodeGenModule &CGM, |
| 272 | ArrayRef<std::pair<const VarDecl *, llvm::GlobalVariable *>> |
| 273 | CXXThreadLocals, |
| 274 | ArrayRef<llvm::Function *> CXXThreadLocalInits, |
| 275 | ArrayRef<llvm::GlobalVariable *> CXXThreadLocalInitVars) override; |
| 276 | |
| 277 | bool usesThreadWrapperFunction() const override { return false; } |
| 278 | LValue EmitThreadLocalVarDeclLValue(CodeGenFunction &CGF, const VarDecl *VD, |
| 279 | QualType LValType) override; |
| 280 | |
John McCall | c84ed6a | 2012-05-01 06:13:13 +0000 | [diff] [blame] | 281 | void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D, |
| 282 | llvm::GlobalVariable *DeclPtr, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 283 | bool PerformInit) override; |
David Majnemer | b3341ea | 2014-10-05 05:05:40 +0000 | [diff] [blame] | 284 | void registerGlobalDtor(CodeGenFunction &CGF, const VarDecl &D, |
| 285 | llvm::Constant *Dtor, llvm::Constant *Addr) override; |
John McCall | c84ed6a | 2012-05-01 06:13:13 +0000 | [diff] [blame] | 286 | |
John McCall | 2903675 | 2011-01-27 02:46:02 +0000 | [diff] [blame] | 287 | // ==== Notes on array cookies ========= |
| 288 | // |
| 289 | // MSVC seems to only use cookies when the class has a destructor; a |
| 290 | // two-argument usual array deallocation function isn't sufficient. |
| 291 | // |
| 292 | // For example, this code prints "100" and "1": |
| 293 | // struct A { |
| 294 | // char x; |
| 295 | // void *operator new[](size_t sz) { |
| 296 | // printf("%u\n", sz); |
| 297 | // return malloc(sz); |
| 298 | // } |
| 299 | // void operator delete[](void *p, size_t sz) { |
| 300 | // printf("%u\n", sz); |
| 301 | // free(p); |
| 302 | // } |
| 303 | // }; |
| 304 | // int main() { |
| 305 | // A *p = new A[100]; |
| 306 | // delete[] p; |
| 307 | // } |
| 308 | // Whereas it prints "104" and "104" if you give A a destructor. |
John McCall | b91cd66 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 309 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 310 | bool requiresArrayCookie(const CXXDeleteExpr *expr, |
| 311 | QualType elementType) override; |
| 312 | bool requiresArrayCookie(const CXXNewExpr *expr) override; |
| 313 | CharUnits getArrayCookieSizeImpl(QualType type) override; |
John McCall | b91cd66 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 314 | llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF, |
| 315 | llvm::Value *NewPtr, |
| 316 | llvm::Value *NumElements, |
| 317 | const CXXNewExpr *expr, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 318 | QualType ElementType) override; |
John McCall | b91cd66 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 319 | llvm::Value *readArrayCookieImpl(CodeGenFunction &CGF, |
| 320 | llvm::Value *allocPtr, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 321 | CharUnits cookieSize) override; |
Reid Kleckner | 407e8b6 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 322 | |
David Majnemer | 611cdb9 | 2014-07-07 08:09:15 +0000 | [diff] [blame] | 323 | friend struct MSRTTIBuilder; |
| 324 | |
| 325 | bool isImageRelative() const { |
| 326 | return CGM.getTarget().getPointerWidth(/*AddressSpace=*/0) == 64; |
| 327 | } |
| 328 | |
| 329 | // 5 routines for constructing the llvm types for MS RTTI structs. |
| 330 | llvm::StructType *getTypeDescriptorType(StringRef TypeInfoString) { |
| 331 | llvm::SmallString<32> TDTypeName("rtti.TypeDescriptor"); |
| 332 | TDTypeName += llvm::utostr(TypeInfoString.size()); |
| 333 | llvm::StructType *&TypeDescriptorType = |
| 334 | TypeDescriptorTypeMap[TypeInfoString.size()]; |
| 335 | if (TypeDescriptorType) |
| 336 | return TypeDescriptorType; |
| 337 | llvm::Type *FieldTypes[] = { |
| 338 | CGM.Int8PtrPtrTy, |
| 339 | CGM.Int8PtrTy, |
| 340 | llvm::ArrayType::get(CGM.Int8Ty, TypeInfoString.size() + 1)}; |
| 341 | TypeDescriptorType = |
| 342 | llvm::StructType::create(CGM.getLLVMContext(), FieldTypes, TDTypeName); |
| 343 | return TypeDescriptorType; |
| 344 | } |
| 345 | |
| 346 | llvm::Type *getImageRelativeType(llvm::Type *PtrType) { |
| 347 | if (!isImageRelative()) |
| 348 | return PtrType; |
| 349 | return CGM.IntTy; |
| 350 | } |
| 351 | |
| 352 | llvm::StructType *getBaseClassDescriptorType() { |
| 353 | if (BaseClassDescriptorType) |
| 354 | return BaseClassDescriptorType; |
| 355 | llvm::Type *FieldTypes[] = { |
| 356 | getImageRelativeType(CGM.Int8PtrTy), |
| 357 | CGM.IntTy, |
| 358 | CGM.IntTy, |
| 359 | CGM.IntTy, |
| 360 | CGM.IntTy, |
| 361 | CGM.IntTy, |
| 362 | getImageRelativeType(getClassHierarchyDescriptorType()->getPointerTo()), |
| 363 | }; |
| 364 | BaseClassDescriptorType = llvm::StructType::create( |
| 365 | CGM.getLLVMContext(), FieldTypes, "rtti.BaseClassDescriptor"); |
| 366 | return BaseClassDescriptorType; |
| 367 | } |
| 368 | |
| 369 | llvm::StructType *getClassHierarchyDescriptorType() { |
| 370 | if (ClassHierarchyDescriptorType) |
| 371 | return ClassHierarchyDescriptorType; |
| 372 | // Forward-declare RTTIClassHierarchyDescriptor to break a cycle. |
| 373 | ClassHierarchyDescriptorType = llvm::StructType::create( |
| 374 | CGM.getLLVMContext(), "rtti.ClassHierarchyDescriptor"); |
| 375 | llvm::Type *FieldTypes[] = { |
| 376 | CGM.IntTy, |
| 377 | CGM.IntTy, |
| 378 | CGM.IntTy, |
| 379 | getImageRelativeType( |
| 380 | getBaseClassDescriptorType()->getPointerTo()->getPointerTo()), |
| 381 | }; |
| 382 | ClassHierarchyDescriptorType->setBody(FieldTypes); |
| 383 | return ClassHierarchyDescriptorType; |
| 384 | } |
| 385 | |
| 386 | llvm::StructType *getCompleteObjectLocatorType() { |
| 387 | if (CompleteObjectLocatorType) |
| 388 | return CompleteObjectLocatorType; |
| 389 | CompleteObjectLocatorType = llvm::StructType::create( |
| 390 | CGM.getLLVMContext(), "rtti.CompleteObjectLocator"); |
| 391 | llvm::Type *FieldTypes[] = { |
| 392 | CGM.IntTy, |
| 393 | CGM.IntTy, |
| 394 | CGM.IntTy, |
| 395 | getImageRelativeType(CGM.Int8PtrTy), |
| 396 | getImageRelativeType(getClassHierarchyDescriptorType()->getPointerTo()), |
| 397 | getImageRelativeType(CompleteObjectLocatorType), |
| 398 | }; |
| 399 | llvm::ArrayRef<llvm::Type *> FieldTypesRef(FieldTypes); |
| 400 | if (!isImageRelative()) |
| 401 | FieldTypesRef = FieldTypesRef.drop_back(); |
| 402 | CompleteObjectLocatorType->setBody(FieldTypesRef); |
| 403 | return CompleteObjectLocatorType; |
| 404 | } |
| 405 | |
| 406 | llvm::GlobalVariable *getImageBase() { |
| 407 | StringRef Name = "__ImageBase"; |
| 408 | if (llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(Name)) |
| 409 | return GV; |
| 410 | |
| 411 | return new llvm::GlobalVariable(CGM.getModule(), CGM.Int8Ty, |
| 412 | /*isConstant=*/true, |
| 413 | llvm::GlobalValue::ExternalLinkage, |
| 414 | /*Initializer=*/nullptr, Name); |
| 415 | } |
| 416 | |
| 417 | llvm::Constant *getImageRelativeConstant(llvm::Constant *PtrVal) { |
| 418 | if (!isImageRelative()) |
| 419 | return PtrVal; |
| 420 | |
David Majnemer | 7c23707 | 2015-03-05 00:46:22 +0000 | [diff] [blame] | 421 | if (PtrVal->isNullValue()) |
| 422 | return llvm::Constant::getNullValue(CGM.IntTy); |
| 423 | |
David Majnemer | 611cdb9 | 2014-07-07 08:09:15 +0000 | [diff] [blame] | 424 | llvm::Constant *ImageBaseAsInt = |
| 425 | llvm::ConstantExpr::getPtrToInt(getImageBase(), CGM.IntPtrTy); |
| 426 | llvm::Constant *PtrValAsInt = |
| 427 | llvm::ConstantExpr::getPtrToInt(PtrVal, CGM.IntPtrTy); |
| 428 | llvm::Constant *Diff = |
| 429 | llvm::ConstantExpr::getSub(PtrValAsInt, ImageBaseAsInt, |
| 430 | /*HasNUW=*/true, /*HasNSW=*/true); |
| 431 | return llvm::ConstantExpr::getTrunc(Diff, CGM.IntTy); |
| 432 | } |
| 433 | |
Reid Kleckner | 407e8b6 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 434 | private: |
Timur Iskhodzhanov | 6745522 | 2013-10-03 06:26:13 +0000 | [diff] [blame] | 435 | MicrosoftMangleContext &getMangleContext() { |
| 436 | return cast<MicrosoftMangleContext>(CodeGen::CGCXXABI::getMangleContext()); |
| 437 | } |
| 438 | |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 439 | llvm::Constant *getZeroInt() { |
| 440 | return llvm::ConstantInt::get(CGM.IntTy, 0); |
Reid Kleckner | 407e8b6 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 441 | } |
| 442 | |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 443 | llvm::Constant *getAllOnesInt() { |
| 444 | return llvm::Constant::getAllOnesValue(CGM.IntTy); |
Reid Kleckner | 407e8b6 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 445 | } |
| 446 | |
Reid Kleckner | 452abac | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 447 | llvm::Constant *getConstantOrZeroInt(llvm::Constant *C) { |
| 448 | return C ? C : getZeroInt(); |
| 449 | } |
| 450 | |
| 451 | llvm::Value *getValueOrZeroInt(llvm::Value *C) { |
| 452 | return C ? C : getZeroInt(); |
| 453 | } |
| 454 | |
Timur Iskhodzhanov | f174942 | 2014-03-14 17:43:37 +0000 | [diff] [blame] | 455 | CharUnits getVirtualFunctionPrologueThisAdjustment(GlobalDecl GD); |
| 456 | |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 457 | void |
| 458 | GetNullMemberPointerFields(const MemberPointerType *MPT, |
| 459 | llvm::SmallVectorImpl<llvm::Constant *> &fields); |
| 460 | |
Reid Kleckner | d8cbeec | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 461 | /// \brief Shared code for virtual base adjustment. Returns the offset from |
| 462 | /// the vbptr to the virtual base. Optionally returns the address of the |
| 463 | /// vbptr itself. |
| 464 | llvm::Value *GetVBaseOffsetFromVBPtr(CodeGenFunction &CGF, |
| 465 | llvm::Value *Base, |
| 466 | llvm::Value *VBPtrOffset, |
| 467 | llvm::Value *VBTableOffset, |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 468 | llvm::Value **VBPtr = nullptr); |
Reid Kleckner | d8cbeec | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 469 | |
Timur Iskhodzhanov | 0201432 | 2013-10-30 11:55:43 +0000 | [diff] [blame] | 470 | llvm::Value *GetVBaseOffsetFromVBPtr(CodeGenFunction &CGF, |
| 471 | llvm::Value *Base, |
| 472 | int32_t VBPtrOffset, |
| 473 | int32_t VBTableOffset, |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 474 | llvm::Value **VBPtr = nullptr) { |
Reid Kleckner | 0ba8ba4 | 2014-10-22 17:26:00 +0000 | [diff] [blame] | 475 | assert(VBTableOffset % 4 == 0 && "should be byte offset into table of i32s"); |
Timur Iskhodzhanov | 0201432 | 2013-10-30 11:55:43 +0000 | [diff] [blame] | 476 | llvm::Value *VBPOffset = llvm::ConstantInt::get(CGM.IntTy, VBPtrOffset), |
| 477 | *VBTOffset = llvm::ConstantInt::get(CGM.IntTy, VBTableOffset); |
| 478 | return GetVBaseOffsetFromVBPtr(CGF, Base, VBPOffset, VBTOffset, VBPtr); |
| 479 | } |
| 480 | |
David Majnemer | 5bc883f | 2015-02-27 02:38:02 +0000 | [diff] [blame] | 481 | std::pair<llvm::Value *, llvm::Value *> |
| 482 | performBaseAdjustment(CodeGenFunction &CGF, llvm::Value *Value, |
| 483 | QualType SrcRecordTy); |
| 484 | |
Reid Kleckner | d8cbeec | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 485 | /// \brief Performs a full virtual base adjustment. Used to dereference |
| 486 | /// pointers to members of virtual bases. |
David Majnemer | 2b0d66d | 2014-02-20 23:22:07 +0000 | [diff] [blame] | 487 | llvm::Value *AdjustVirtualBase(CodeGenFunction &CGF, const Expr *E, |
| 488 | const CXXRecordDecl *RD, llvm::Value *Base, |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 489 | llvm::Value *VirtualBaseAdjustmentOffset, |
| 490 | llvm::Value *VBPtrOffset /* optional */); |
| 491 | |
Reid Kleckner | 7d0efb5 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 492 | /// \brief Emits a full member pointer with the fields common to data and |
| 493 | /// function member pointers. |
| 494 | llvm::Constant *EmitFullMemberPointer(llvm::Constant *FirstField, |
| 495 | bool IsMemberFunction, |
Reid Kleckner | 452abac | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 496 | const CXXRecordDecl *RD, |
| 497 | CharUnits NonVirtualBaseAdjustment); |
| 498 | |
| 499 | llvm::Constant *BuildMemberPointer(const CXXRecordDecl *RD, |
| 500 | const CXXMethodDecl *MD, |
| 501 | CharUnits NonVirtualBaseAdjustment); |
| 502 | |
| 503 | bool MemberPointerConstantIsNull(const MemberPointerType *MPT, |
| 504 | llvm::Constant *MP); |
Reid Kleckner | 7d0efb5 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 505 | |
Reid Kleckner | 7810af0 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 506 | /// \brief - Initialize all vbptrs of 'this' with RD as the complete type. |
| 507 | void EmitVBPtrStores(CodeGenFunction &CGF, const CXXRecordDecl *RD); |
| 508 | |
| 509 | /// \brief Caching wrapper around VBTableBuilder::enumerateVBTables(). |
Reid Kleckner | b40a27d | 2014-01-03 00:14:35 +0000 | [diff] [blame] | 510 | const VBTableGlobals &enumerateVBTables(const CXXRecordDecl *RD); |
Reid Kleckner | 7810af0 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 511 | |
Hans Wennborg | 88497d6 | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 512 | /// \brief Generate a thunk for calling a virtual member function MD. |
Reid Kleckner | e4a5220 | 2014-02-21 02:27:32 +0000 | [diff] [blame] | 513 | llvm::Function *EmitVirtualMemPtrThunk( |
| 514 | const CXXMethodDecl *MD, |
| 515 | const MicrosoftVTableContext::MethodVFTableLocation &ML); |
Hans Wennborg | 88497d6 | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 516 | |
Reid Kleckner | 407e8b6 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 517 | public: |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 518 | llvm::Type *ConvertMemberPointerType(const MemberPointerType *MPT) override; |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 519 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 520 | bool isZeroInitializable(const MemberPointerType *MPT) override; |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 521 | |
David Majnemer | b3e5654 | 2014-08-07 22:56:13 +0000 | [diff] [blame] | 522 | bool isMemberPointerConvertible(const MemberPointerType *MPT) const override { |
| 523 | const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl(); |
David Majnemer | 9928106 | 2014-09-18 22:05:54 +0000 | [diff] [blame] | 524 | return RD->hasAttr<MSInheritanceAttr>(); |
| 525 | } |
| 526 | |
David Blaikie | 1cbb971 | 2014-11-14 19:09:44 +0000 | [diff] [blame] | 527 | bool isTypeInfoCalculable(QualType Ty) const override { |
David Majnemer | 9928106 | 2014-09-18 22:05:54 +0000 | [diff] [blame] | 528 | if (!CGCXXABI::isTypeInfoCalculable(Ty)) |
| 529 | return false; |
| 530 | if (const auto *MPT = Ty->getAs<MemberPointerType>()) { |
| 531 | const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl(); |
| 532 | if (!RD->hasAttr<MSInheritanceAttr>()) |
| 533 | return false; |
| 534 | } |
| 535 | return true; |
David Majnemer | b3e5654 | 2014-08-07 22:56:13 +0000 | [diff] [blame] | 536 | } |
| 537 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 538 | llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT) override; |
Reid Kleckner | 407e8b6 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 539 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 540 | llvm::Constant *EmitMemberDataPointer(const MemberPointerType *MPT, |
| 541 | CharUnits offset) override; |
| 542 | llvm::Constant *EmitMemberPointer(const CXXMethodDecl *MD) override; |
| 543 | llvm::Constant *EmitMemberPointer(const APValue &MP, QualType MPT) override; |
Reid Kleckner | 407e8b6 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 544 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 545 | llvm::Value *EmitMemberPointerComparison(CodeGenFunction &CGF, |
| 546 | llvm::Value *L, |
| 547 | llvm::Value *R, |
| 548 | const MemberPointerType *MPT, |
| 549 | bool Inequality) override; |
Reid Kleckner | 700c3ee | 2013-04-30 20:15:14 +0000 | [diff] [blame] | 550 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 551 | llvm::Value *EmitMemberPointerIsNotNull(CodeGenFunction &CGF, |
| 552 | llvm::Value *MemPtr, |
| 553 | const MemberPointerType *MPT) override; |
Reid Kleckner | 407e8b6 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 554 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 555 | llvm::Value * |
David Majnemer | 2b0d66d | 2014-02-20 23:22:07 +0000 | [diff] [blame] | 556 | EmitMemberDataPointerAddress(CodeGenFunction &CGF, const Expr *E, |
| 557 | llvm::Value *Base, llvm::Value *MemPtr, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 558 | const MemberPointerType *MPT) override; |
Reid Kleckner | 407e8b6 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 559 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 560 | llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF, |
| 561 | const CastExpr *E, |
| 562 | llvm::Value *Src) override; |
Reid Kleckner | 452abac | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 563 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 564 | llvm::Constant *EmitMemberPointerConversion(const CastExpr *E, |
| 565 | llvm::Constant *Src) override; |
Reid Kleckner | 452abac | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 566 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 567 | llvm::Value * |
David Majnemer | 2b0d66d | 2014-02-20 23:22:07 +0000 | [diff] [blame] | 568 | EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF, const Expr *E, |
| 569 | llvm::Value *&This, llvm::Value *MemPtr, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 570 | const MemberPointerType *MPT) override; |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 571 | |
Rafael Espindola | 91f68b4 | 2014-09-15 19:20:10 +0000 | [diff] [blame] | 572 | void emitCXXStructor(const CXXMethodDecl *MD, StructorType Type) override; |
| 573 | |
David Majnemer | 7c23707 | 2015-03-05 00:46:22 +0000 | [diff] [blame] | 574 | llvm::StructType *getCatchableTypeType() { |
| 575 | if (CatchableTypeType) |
| 576 | return CatchableTypeType; |
| 577 | llvm::Type *FieldTypes[] = { |
| 578 | CGM.IntTy, // Flags |
| 579 | getImageRelativeType(CGM.Int8PtrTy), // TypeDescriptor |
| 580 | CGM.IntTy, // NonVirtualAdjustment |
| 581 | CGM.IntTy, // OffsetToVBPtr |
| 582 | CGM.IntTy, // VBTableIndex |
| 583 | CGM.IntTy, // Size |
| 584 | getImageRelativeType(CGM.Int8PtrTy) // CopyCtor |
| 585 | }; |
| 586 | CatchableTypeType = llvm::StructType::create( |
| 587 | CGM.getLLVMContext(), FieldTypes, "eh.CatchableType"); |
| 588 | return CatchableTypeType; |
| 589 | } |
| 590 | |
| 591 | llvm::StructType *getCatchableTypeArrayType(uint32_t NumEntries) { |
| 592 | llvm::StructType *&CatchableTypeArrayType = |
| 593 | CatchableTypeArrayTypeMap[NumEntries]; |
| 594 | if (CatchableTypeArrayType) |
| 595 | return CatchableTypeArrayType; |
| 596 | |
| 597 | llvm::SmallString<23> CTATypeName("eh.CatchableTypeArray."); |
| 598 | CTATypeName += llvm::utostr(NumEntries); |
| 599 | llvm::Type *CTType = |
| 600 | getImageRelativeType(getCatchableTypeType()->getPointerTo()); |
| 601 | llvm::Type *FieldTypes[] = { |
| 602 | CGM.IntTy, // NumEntries |
| 603 | llvm::ArrayType::get(CTType, NumEntries) // CatchableTypes |
| 604 | }; |
| 605 | CatchableTypeArrayType = |
| 606 | llvm::StructType::create(CGM.getLLVMContext(), FieldTypes, CTATypeName); |
| 607 | return CatchableTypeArrayType; |
| 608 | } |
| 609 | |
| 610 | llvm::StructType *getThrowInfoType() { |
| 611 | if (ThrowInfoType) |
| 612 | return ThrowInfoType; |
| 613 | llvm::Type *FieldTypes[] = { |
| 614 | CGM.IntTy, // Flags |
| 615 | getImageRelativeType(CGM.Int8PtrTy), // CleanupFn |
| 616 | getImageRelativeType(CGM.Int8PtrTy), // ForwardCompat |
| 617 | getImageRelativeType(CGM.Int8PtrTy) // CatchableTypeArray |
| 618 | }; |
| 619 | ThrowInfoType = llvm::StructType::create(CGM.getLLVMContext(), FieldTypes, |
| 620 | "eh.ThrowInfo"); |
| 621 | return ThrowInfoType; |
| 622 | } |
| 623 | |
| 624 | llvm::Constant *getThrowFn() { |
| 625 | // _CxxThrowException is passed an exception object and a ThrowInfo object |
| 626 | // which describes the exception. |
| 627 | llvm::Type *Args[] = {CGM.Int8PtrTy, getThrowInfoType()->getPointerTo()}; |
| 628 | llvm::FunctionType *FTy = |
| 629 | llvm::FunctionType::get(CGM.VoidTy, Args, /*IsVarArgs=*/false); |
| 630 | auto *Fn = cast<llvm::Function>( |
| 631 | CGM.CreateRuntimeFunction(FTy, "_CxxThrowException")); |
| 632 | // _CxxThrowException is stdcall on 32-bit x86 platforms. |
| 633 | if (CGM.getTarget().getTriple().getArch() == llvm::Triple::x86) |
| 634 | Fn->setCallingConv(llvm::CallingConv::X86_StdCall); |
| 635 | return Fn; |
| 636 | } |
| 637 | |
David Majnemer | 37fd66e | 2015-03-13 22:36:55 +0000 | [diff] [blame] | 638 | llvm::Function *getAddrOfCXXCtorClosure(const CXXConstructorDecl *CD, |
| 639 | CXXCtorType CT); |
David Majnemer | dfa6d20 | 2015-03-11 18:36:39 +0000 | [diff] [blame] | 640 | |
David Majnemer | 7c23707 | 2015-03-05 00:46:22 +0000 | [diff] [blame] | 641 | llvm::Constant *getCatchableType(QualType T, |
| 642 | uint32_t NVOffset = 0, |
| 643 | int32_t VBPtrOffset = -1, |
| 644 | uint32_t VBIndex = 0); |
| 645 | |
| 646 | llvm::GlobalVariable *getCatchableTypeArray(QualType T); |
| 647 | |
David Majnemer | ba3e5ec | 2015-03-13 18:26:17 +0000 | [diff] [blame] | 648 | llvm::GlobalVariable *getThrowInfo(QualType T) override; |
David Majnemer | 7c23707 | 2015-03-05 00:46:22 +0000 | [diff] [blame] | 649 | |
Reid Kleckner | 7810af0 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 650 | private: |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 651 | typedef std::pair<const CXXRecordDecl *, CharUnits> VFTableIdTy; |
David Majnemer | d905da4 | 2014-07-01 20:30:31 +0000 | [diff] [blame] | 652 | typedef llvm::DenseMap<VFTableIdTy, llvm::GlobalVariable *> VTablesMapTy; |
| 653 | typedef llvm::DenseMap<VFTableIdTy, llvm::GlobalValue *> VFTablesMapTy; |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 654 | /// \brief All the vftables that have been referenced. |
| 655 | VFTablesMapTy VFTablesMap; |
David Majnemer | d905da4 | 2014-07-01 20:30:31 +0000 | [diff] [blame] | 656 | VTablesMapTy VTablesMap; |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 657 | |
| 658 | /// \brief This set holds the record decls we've deferred vtable emission for. |
| 659 | llvm::SmallPtrSet<const CXXRecordDecl *, 4> DeferredVFTables; |
| 660 | |
| 661 | |
| 662 | /// \brief All the vbtables which have been referenced. |
Reid Kleckner | b40a27d | 2014-01-03 00:14:35 +0000 | [diff] [blame] | 663 | llvm::DenseMap<const CXXRecordDecl *, VBTableGlobals> VBTablesMap; |
Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 664 | |
| 665 | /// Info on the global variable used to guard initialization of static locals. |
| 666 | /// The BitIndex field is only used for externally invisible declarations. |
| 667 | struct GuardInfo { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 668 | GuardInfo() : Guard(nullptr), BitIndex(0) {} |
Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 669 | llvm::GlobalVariable *Guard; |
| 670 | unsigned BitIndex; |
| 671 | }; |
| 672 | |
| 673 | /// Map from DeclContext to the current guard variable. We assume that the |
| 674 | /// AST is visited in source code order. |
| 675 | llvm::DenseMap<const DeclContext *, GuardInfo> GuardVariableMap; |
David Majnemer | 611cdb9 | 2014-07-07 08:09:15 +0000 | [diff] [blame] | 676 | |
| 677 | llvm::DenseMap<size_t, llvm::StructType *> TypeDescriptorTypeMap; |
| 678 | llvm::StructType *BaseClassDescriptorType; |
| 679 | llvm::StructType *ClassHierarchyDescriptorType; |
| 680 | llvm::StructType *CompleteObjectLocatorType; |
David Majnemer | 7c23707 | 2015-03-05 00:46:22 +0000 | [diff] [blame] | 681 | |
| 682 | llvm::DenseMap<QualType, llvm::GlobalVariable *> CatchableTypeArrays; |
| 683 | |
| 684 | llvm::StructType *CatchableTypeType; |
| 685 | llvm::DenseMap<uint32_t, llvm::StructType *> CatchableTypeArrayTypeMap; |
| 686 | llvm::StructType *ThrowInfoType; |
Charles Davis | 74ce859 | 2010-06-09 23:25:41 +0000 | [diff] [blame] | 687 | }; |
| 688 | |
| 689 | } |
| 690 | |
Reid Kleckner | e39ee21 | 2014-05-03 00:33:28 +0000 | [diff] [blame] | 691 | CGCXXABI::RecordArgABI |
| 692 | MicrosoftCXXABI::getRecordArgABI(const CXXRecordDecl *RD) const { |
| 693 | switch (CGM.getTarget().getTriple().getArch()) { |
| 694 | default: |
| 695 | // FIXME: Implement for other architectures. |
| 696 | return RAA_Default; |
| 697 | |
| 698 | case llvm::Triple::x86: |
Reid Kleckner | cf87e10 | 2014-05-14 16:02:09 +0000 | [diff] [blame] | 699 | // All record arguments are passed in memory on x86. Decide whether to |
| 700 | // construct the object directly in argument memory, or to construct the |
| 701 | // argument elsewhere and copy the bytes during the call. |
| 702 | |
| 703 | // If C++ prohibits us from making a copy, construct the arguments directly |
| 704 | // into argument memory. |
| 705 | if (!canCopyArgument(RD)) |
Reid Kleckner | e39ee21 | 2014-05-03 00:33:28 +0000 | [diff] [blame] | 706 | return RAA_DirectInMemory; |
Reid Kleckner | cf87e10 | 2014-05-14 16:02:09 +0000 | [diff] [blame] | 707 | |
| 708 | // Otherwise, construct the argument into a temporary and copy the bytes |
| 709 | // into the outgoing argument memory. |
Reid Kleckner | e39ee21 | 2014-05-03 00:33:28 +0000 | [diff] [blame] | 710 | return RAA_Default; |
| 711 | |
| 712 | case llvm::Triple::x86_64: |
| 713 | // Win64 passes objects with non-trivial copy ctors indirectly. |
| 714 | if (RD->hasNonTrivialCopyConstructor()) |
| 715 | return RAA_Indirect; |
Reid Kleckner | cf87e10 | 2014-05-14 16:02:09 +0000 | [diff] [blame] | 716 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 717 | // If an object has a destructor, we'd really like to pass it indirectly |
| 718 | // because it allows us to elide copies. Unfortunately, MSVC makes that |
| 719 | // impossible for small types, which it will pass in a single register or |
| 720 | // stack slot. Most objects with dtors are large-ish, so handle that early. |
| 721 | // We can't call out all large objects as being indirect because there are |
| 722 | // multiple x64 calling conventions and the C++ ABI code shouldn't dictate |
| 723 | // how we pass large POD types. |
| 724 | if (RD->hasNonTrivialDestructor() && |
| 725 | getContext().getTypeSize(RD->getTypeForDecl()) > 64) |
Reid Kleckner | e39ee21 | 2014-05-03 00:33:28 +0000 | [diff] [blame] | 726 | return RAA_Indirect; |
Reid Kleckner | cf87e10 | 2014-05-14 16:02:09 +0000 | [diff] [blame] | 727 | |
| 728 | // We have a trivial copy constructor or no copy constructors, but we have |
| 729 | // to make sure it isn't deleted. |
| 730 | bool CopyDeleted = false; |
| 731 | for (const CXXConstructorDecl *CD : RD->ctors()) { |
| 732 | if (CD->isCopyConstructor()) { |
| 733 | assert(CD->isTrivial()); |
| 734 | // We had at least one undeleted trivial copy ctor. Return directly. |
| 735 | if (!CD->isDeleted()) |
| 736 | return RAA_Default; |
| 737 | CopyDeleted = true; |
| 738 | } |
| 739 | } |
| 740 | |
| 741 | // The trivial copy constructor was deleted. Return indirectly. |
| 742 | if (CopyDeleted) |
| 743 | return RAA_Indirect; |
| 744 | |
| 745 | // There were no copy ctors. Return in RAX. |
Reid Kleckner | e39ee21 | 2014-05-03 00:33:28 +0000 | [diff] [blame] | 746 | return RAA_Default; |
| 747 | } |
| 748 | |
| 749 | llvm_unreachable("invalid enum"); |
| 750 | } |
| 751 | |
David Majnemer | 0868137 | 2014-11-01 07:37:17 +0000 | [diff] [blame] | 752 | void MicrosoftCXXABI::emitVirtualObjectDelete(CodeGenFunction &CGF, |
| 753 | const CXXDeleteExpr *DE, |
| 754 | llvm::Value *Ptr, |
| 755 | QualType ElementType, |
| 756 | const CXXDestructorDecl *Dtor) { |
David Majnemer | 0c0b6d9 | 2014-10-31 20:09:12 +0000 | [diff] [blame] | 757 | // FIXME: Provide a source location here even though there's no |
| 758 | // CXXMemberCallExpr for dtor call. |
David Majnemer | 0868137 | 2014-11-01 07:37:17 +0000 | [diff] [blame] | 759 | bool UseGlobalDelete = DE->isGlobalDelete(); |
David Majnemer | 0c0b6d9 | 2014-10-31 20:09:12 +0000 | [diff] [blame] | 760 | CXXDtorType DtorType = UseGlobalDelete ? Dtor_Complete : Dtor_Deleting; |
| 761 | llvm::Value *MDThis = |
| 762 | EmitVirtualDestructorCall(CGF, Dtor, DtorType, Ptr, /*CE=*/nullptr); |
| 763 | if (UseGlobalDelete) |
David Majnemer | 0868137 | 2014-11-01 07:37:17 +0000 | [diff] [blame] | 764 | CGF.EmitDeleteCall(DE->getOperatorDelete(), MDThis, ElementType); |
John McCall | 82fb892 | 2012-09-25 10:10:39 +0000 | [diff] [blame] | 765 | } |
| 766 | |
David Majnemer | 442d0a2 | 2014-11-25 07:20:20 +0000 | [diff] [blame] | 767 | void MicrosoftCXXABI::emitRethrow(CodeGenFunction &CGF, bool isNoReturn) { |
David Majnemer | 7c23707 | 2015-03-05 00:46:22 +0000 | [diff] [blame] | 768 | llvm::Value *Args[] = { |
| 769 | llvm::ConstantPointerNull::get(CGM.Int8PtrTy), |
| 770 | llvm::ConstantPointerNull::get(getThrowInfoType()->getPointerTo())}; |
| 771 | auto *Fn = getThrowFn(); |
David Majnemer | 442d0a2 | 2014-11-25 07:20:20 +0000 | [diff] [blame] | 772 | if (isNoReturn) |
| 773 | CGF.EmitNoreturnRuntimeCallOrInvoke(Fn, Args); |
| 774 | else |
| 775 | CGF.EmitRuntimeCallOrInvoke(Fn, Args); |
| 776 | } |
| 777 | |
Reid Kleckner | fff8e7f | 2015-03-03 19:21:04 +0000 | [diff] [blame] | 778 | namespace { |
| 779 | struct CallEndCatchMSVC : EHScopeStack::Cleanup { |
| 780 | CallEndCatchMSVC() {} |
| 781 | void Emit(CodeGenFunction &CGF, Flags flags) override { |
| 782 | CGF.EmitNounwindRuntimeCall( |
| 783 | CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_endcatch)); |
| 784 | } |
| 785 | }; |
| 786 | } |
| 787 | |
| 788 | void MicrosoftCXXABI::emitBeginCatch(CodeGenFunction &CGF, |
| 789 | const CXXCatchStmt *S) { |
| 790 | // In the MS ABI, the runtime handles the copy, and the catch handler is |
| 791 | // responsible for destruction. |
| 792 | VarDecl *CatchParam = S->getExceptionDecl(); |
| 793 | llvm::Value *Exn = CGF.getExceptionFromSlot(); |
| 794 | llvm::Function *BeginCatch = |
| 795 | CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_begincatch); |
| 796 | |
| 797 | if (!CatchParam) { |
| 798 | llvm::Value *Args[2] = {Exn, llvm::Constant::getNullValue(CGF.Int8PtrTy)}; |
| 799 | CGF.EmitNounwindRuntimeCall(BeginCatch, Args); |
| 800 | CGF.EHStack.pushCleanup<CallEndCatchMSVC>(NormalAndEHCleanup); |
| 801 | return; |
| 802 | } |
| 803 | |
| 804 | CodeGenFunction::AutoVarEmission var = CGF.EmitAutoVarAlloca(*CatchParam); |
| 805 | llvm::Value *ParamAddr = |
| 806 | CGF.Builder.CreateBitCast(var.getObjectAddress(CGF), CGF.Int8PtrTy); |
| 807 | llvm::Value *Args[2] = {Exn, ParamAddr}; |
| 808 | CGF.EmitNounwindRuntimeCall(BeginCatch, Args); |
| 809 | // FIXME: Do we really need exceptional endcatch cleanups? |
| 810 | CGF.EHStack.pushCleanup<CallEndCatchMSVC>(NormalAndEHCleanup); |
| 811 | CGF.EmitAutoVarCleanups(var); |
| 812 | } |
| 813 | |
David Majnemer | 5bc883f | 2015-02-27 02:38:02 +0000 | [diff] [blame] | 814 | std::pair<llvm::Value *, llvm::Value *> |
| 815 | MicrosoftCXXABI::performBaseAdjustment(CodeGenFunction &CGF, llvm::Value *Value, |
| 816 | QualType SrcRecordTy) { |
David Majnemer | 1162d25 | 2014-06-22 19:05:33 +0000 | [diff] [blame] | 817 | Value = CGF.Builder.CreateBitCast(Value, CGF.Int8PtrTy); |
| 818 | const CXXRecordDecl *SrcDecl = SrcRecordTy->getAsCXXRecordDecl(); |
David Majnemer | 9ced3dd | 2015-03-14 23:44:48 +0000 | [diff] [blame] | 819 | const ASTContext &Context = getContext(); |
David Majnemer | 1162d25 | 2014-06-22 19:05:33 +0000 | [diff] [blame] | 820 | |
David Majnemer | 5bc883f | 2015-02-27 02:38:02 +0000 | [diff] [blame] | 821 | if (Context.getASTRecordLayout(SrcDecl).hasExtendableVFPtr()) |
David Majnemer | 1162d25 | 2014-06-22 19:05:33 +0000 | [diff] [blame] | 822 | return std::make_pair(Value, llvm::ConstantInt::get(CGF.Int32Ty, 0)); |
| 823 | |
| 824 | // Perform a base adjustment. |
David Majnemer | 5bc883f | 2015-02-27 02:38:02 +0000 | [diff] [blame] | 825 | const CXXBaseSpecifier *PolymorphicBase = std::find_if( |
| 826 | SrcDecl->vbases_begin(), SrcDecl->vbases_end(), |
| 827 | [&](const CXXBaseSpecifier &Base) { |
| 828 | const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl(); |
| 829 | return Context.getASTRecordLayout(BaseDecl).hasExtendableVFPtr(); |
| 830 | }); |
| 831 | llvm::Value *Offset = GetVirtualBaseClassOffset( |
| 832 | CGF, Value, SrcDecl, PolymorphicBase->getType()->getAsCXXRecordDecl()); |
David Majnemer | 1162d25 | 2014-06-22 19:05:33 +0000 | [diff] [blame] | 833 | Value = CGF.Builder.CreateInBoundsGEP(Value, Offset); |
| 834 | Offset = CGF.Builder.CreateTrunc(Offset, CGF.Int32Ty); |
| 835 | return std::make_pair(Value, Offset); |
| 836 | } |
| 837 | |
| 838 | bool MicrosoftCXXABI::shouldTypeidBeNullChecked(bool IsDeref, |
| 839 | QualType SrcRecordTy) { |
| 840 | const CXXRecordDecl *SrcDecl = SrcRecordTy->getAsCXXRecordDecl(); |
| 841 | return IsDeref && |
David Majnemer | 9ced3dd | 2015-03-14 23:44:48 +0000 | [diff] [blame] | 842 | !getContext().getASTRecordLayout(SrcDecl).hasExtendableVFPtr(); |
David Majnemer | 1162d25 | 2014-06-22 19:05:33 +0000 | [diff] [blame] | 843 | } |
| 844 | |
| 845 | static llvm::CallSite emitRTtypeidCall(CodeGenFunction &CGF, |
| 846 | llvm::Value *Argument) { |
| 847 | llvm::Type *ArgTypes[] = {CGF.Int8PtrTy}; |
| 848 | llvm::FunctionType *FTy = |
| 849 | llvm::FunctionType::get(CGF.Int8PtrTy, ArgTypes, false); |
| 850 | llvm::Value *Args[] = {Argument}; |
| 851 | llvm::Constant *Fn = CGF.CGM.CreateRuntimeFunction(FTy, "__RTtypeid"); |
| 852 | return CGF.EmitRuntimeCallOrInvoke(Fn, Args); |
| 853 | } |
| 854 | |
| 855 | void MicrosoftCXXABI::EmitBadTypeidCall(CodeGenFunction &CGF) { |
| 856 | llvm::CallSite Call = |
| 857 | emitRTtypeidCall(CGF, llvm::Constant::getNullValue(CGM.VoidPtrTy)); |
| 858 | Call.setDoesNotReturn(); |
| 859 | CGF.Builder.CreateUnreachable(); |
| 860 | } |
| 861 | |
| 862 | llvm::Value *MicrosoftCXXABI::EmitTypeid(CodeGenFunction &CGF, |
| 863 | QualType SrcRecordTy, |
| 864 | llvm::Value *ThisPtr, |
| 865 | llvm::Type *StdTypeInfoPtrTy) { |
| 866 | llvm::Value *Offset; |
| 867 | std::tie(ThisPtr, Offset) = performBaseAdjustment(CGF, ThisPtr, SrcRecordTy); |
| 868 | return CGF.Builder.CreateBitCast( |
| 869 | emitRTtypeidCall(CGF, ThisPtr).getInstruction(), StdTypeInfoPtrTy); |
| 870 | } |
| 871 | |
| 872 | bool MicrosoftCXXABI::shouldDynamicCastCallBeNullChecked(bool SrcIsPtr, |
| 873 | QualType SrcRecordTy) { |
| 874 | const CXXRecordDecl *SrcDecl = SrcRecordTy->getAsCXXRecordDecl(); |
| 875 | return SrcIsPtr && |
David Majnemer | 9ced3dd | 2015-03-14 23:44:48 +0000 | [diff] [blame] | 876 | !getContext().getASTRecordLayout(SrcDecl).hasExtendableVFPtr(); |
David Majnemer | 1162d25 | 2014-06-22 19:05:33 +0000 | [diff] [blame] | 877 | } |
| 878 | |
| 879 | llvm::Value *MicrosoftCXXABI::EmitDynamicCastCall( |
| 880 | CodeGenFunction &CGF, llvm::Value *Value, QualType SrcRecordTy, |
| 881 | QualType DestTy, QualType DestRecordTy, llvm::BasicBlock *CastEnd) { |
| 882 | llvm::Type *DestLTy = CGF.ConvertType(DestTy); |
| 883 | |
| 884 | llvm::Value *SrcRTTI = |
| 885 | CGF.CGM.GetAddrOfRTTIDescriptor(SrcRecordTy.getUnqualifiedType()); |
| 886 | llvm::Value *DestRTTI = |
| 887 | CGF.CGM.GetAddrOfRTTIDescriptor(DestRecordTy.getUnqualifiedType()); |
| 888 | |
| 889 | llvm::Value *Offset; |
| 890 | std::tie(Value, Offset) = performBaseAdjustment(CGF, Value, SrcRecordTy); |
| 891 | |
| 892 | // PVOID __RTDynamicCast( |
| 893 | // PVOID inptr, |
| 894 | // LONG VfDelta, |
| 895 | // PVOID SrcType, |
| 896 | // PVOID TargetType, |
| 897 | // BOOL isReference) |
| 898 | llvm::Type *ArgTypes[] = {CGF.Int8PtrTy, CGF.Int32Ty, CGF.Int8PtrTy, |
| 899 | CGF.Int8PtrTy, CGF.Int32Ty}; |
| 900 | llvm::Constant *Function = CGF.CGM.CreateRuntimeFunction( |
| 901 | llvm::FunctionType::get(CGF.Int8PtrTy, ArgTypes, false), |
| 902 | "__RTDynamicCast"); |
| 903 | llvm::Value *Args[] = { |
| 904 | Value, Offset, SrcRTTI, DestRTTI, |
| 905 | llvm::ConstantInt::get(CGF.Int32Ty, DestTy->isReferenceType())}; |
| 906 | Value = CGF.EmitRuntimeCallOrInvoke(Function, Args).getInstruction(); |
| 907 | return CGF.Builder.CreateBitCast(Value, DestLTy); |
| 908 | } |
| 909 | |
| 910 | llvm::Value * |
| 911 | MicrosoftCXXABI::EmitDynamicCastToVoid(CodeGenFunction &CGF, llvm::Value *Value, |
| 912 | QualType SrcRecordTy, |
| 913 | QualType DestTy) { |
| 914 | llvm::Value *Offset; |
| 915 | std::tie(Value, Offset) = performBaseAdjustment(CGF, Value, SrcRecordTy); |
| 916 | |
| 917 | // PVOID __RTCastToVoid( |
| 918 | // PVOID inptr) |
| 919 | llvm::Type *ArgTypes[] = {CGF.Int8PtrTy}; |
| 920 | llvm::Constant *Function = CGF.CGM.CreateRuntimeFunction( |
| 921 | llvm::FunctionType::get(CGF.Int8PtrTy, ArgTypes, false), |
| 922 | "__RTCastToVoid"); |
| 923 | llvm::Value *Args[] = {Value}; |
| 924 | return CGF.EmitRuntimeCall(Function, Args); |
| 925 | } |
| 926 | |
| 927 | bool MicrosoftCXXABI::EmitBadCastCall(CodeGenFunction &CGF) { |
| 928 | return false; |
| 929 | } |
| 930 | |
David Majnemer | ca32f93 | 2014-09-01 18:50:02 +0000 | [diff] [blame] | 931 | llvm::Value *MicrosoftCXXABI::GetVirtualBaseClassOffset( |
| 932 | CodeGenFunction &CGF, llvm::Value *This, const CXXRecordDecl *ClassDecl, |
| 933 | const CXXRecordDecl *BaseClassDecl) { |
David Majnemer | 9ced3dd | 2015-03-14 23:44:48 +0000 | [diff] [blame] | 934 | const ASTContext &Context = getContext(); |
Reid Kleckner | 5b1b5d5 | 2014-01-14 00:50:39 +0000 | [diff] [blame] | 935 | int64_t VBPtrChars = |
David Majnemer | 9ced3dd | 2015-03-14 23:44:48 +0000 | [diff] [blame] | 936 | Context.getASTRecordLayout(ClassDecl).getVBPtrOffset().getQuantity(); |
Reid Kleckner | d8cbeec | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 937 | llvm::Value *VBPtrOffset = llvm::ConstantInt::get(CGM.PtrDiffTy, VBPtrChars); |
David Majnemer | 9ced3dd | 2015-03-14 23:44:48 +0000 | [diff] [blame] | 938 | CharUnits IntSize = Context.getTypeSizeInChars(Context.IntTy); |
Timur Iskhodzhanov | 5877663 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 939 | CharUnits VBTableChars = |
| 940 | IntSize * |
| 941 | CGM.getMicrosoftVTableContext().getVBTableIndex(ClassDecl, BaseClassDecl); |
Reid Kleckner | d8cbeec | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 942 | llvm::Value *VBTableOffset = |
David Majnemer | ca32f93 | 2014-09-01 18:50:02 +0000 | [diff] [blame] | 943 | llvm::ConstantInt::get(CGM.IntTy, VBTableChars.getQuantity()); |
Reid Kleckner | d8cbeec | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 944 | |
| 945 | llvm::Value *VBPtrToNewBase = |
David Majnemer | ca32f93 | 2014-09-01 18:50:02 +0000 | [diff] [blame] | 946 | GetVBaseOffsetFromVBPtr(CGF, This, VBPtrOffset, VBTableOffset); |
Reid Kleckner | d8cbeec | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 947 | VBPtrToNewBase = |
David Majnemer | ca32f93 | 2014-09-01 18:50:02 +0000 | [diff] [blame] | 948 | CGF.Builder.CreateSExtOrBitCast(VBPtrToNewBase, CGM.PtrDiffTy); |
Reid Kleckner | d8cbeec | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 949 | return CGF.Builder.CreateNSWAdd(VBPtrOffset, VBPtrToNewBase); |
| 950 | } |
| 951 | |
Stephen Lin | 9dc6eef | 2013-06-30 20:40:16 +0000 | [diff] [blame] | 952 | bool MicrosoftCXXABI::HasThisReturn(GlobalDecl GD) const { |
| 953 | return isa<CXXConstructorDecl>(GD.getDecl()); |
John McCall | 0f999f3 | 2012-09-25 08:00:39 +0000 | [diff] [blame] | 954 | } |
| 955 | |
David Majnemer | 0c0b6d9 | 2014-10-31 20:09:12 +0000 | [diff] [blame] | 956 | static bool isDeletingDtor(GlobalDecl GD) { |
| 957 | return isa<CXXDestructorDecl>(GD.getDecl()) && |
| 958 | GD.getDtorType() == Dtor_Deleting; |
| 959 | } |
| 960 | |
| 961 | bool MicrosoftCXXABI::hasMostDerivedReturn(GlobalDecl GD) const { |
| 962 | return isDeletingDtor(GD); |
| 963 | } |
| 964 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 965 | bool MicrosoftCXXABI::classifyReturnType(CGFunctionInfo &FI) const { |
| 966 | const CXXRecordDecl *RD = FI.getReturnType()->getAsCXXRecordDecl(); |
| 967 | if (!RD) |
| 968 | return false; |
| 969 | |
| 970 | if (FI.isInstanceMethod()) { |
| 971 | // If it's an instance method, aggregates are always returned indirectly via |
| 972 | // the second parameter. |
| 973 | FI.getReturnInfo() = ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 974 | FI.getReturnInfo().setSRetAfterThis(FI.isInstanceMethod()); |
| 975 | return true; |
| 976 | } else if (!RD->isPOD()) { |
| 977 | // If it's a free function, non-POD types are returned indirectly. |
| 978 | FI.getReturnInfo() = ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 979 | return true; |
| 980 | } |
| 981 | |
| 982 | // Otherwise, use the C ABI rules. |
| 983 | return false; |
| 984 | } |
| 985 | |
Reid Kleckner | 7810af0 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 986 | llvm::BasicBlock * |
| 987 | MicrosoftCXXABI::EmitCtorCompleteObjectHandler(CodeGenFunction &CGF, |
| 988 | const CXXRecordDecl *RD) { |
Timur Iskhodzhanov | 57cbe5c | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 989 | llvm::Value *IsMostDerivedClass = getStructorImplicitParamValue(CGF); |
| 990 | assert(IsMostDerivedClass && |
| 991 | "ctor for a class with virtual bases must have an implicit parameter"); |
Reid Kleckner | 7810af0 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 992 | llvm::Value *IsCompleteObject = |
| 993 | CGF.Builder.CreateIsNotNull(IsMostDerivedClass, "is_complete_object"); |
Timur Iskhodzhanov | 57cbe5c | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 994 | |
| 995 | llvm::BasicBlock *CallVbaseCtorsBB = CGF.createBasicBlock("ctor.init_vbases"); |
| 996 | llvm::BasicBlock *SkipVbaseCtorsBB = CGF.createBasicBlock("ctor.skip_vbases"); |
| 997 | CGF.Builder.CreateCondBr(IsCompleteObject, |
| 998 | CallVbaseCtorsBB, SkipVbaseCtorsBB); |
| 999 | |
| 1000 | CGF.EmitBlock(CallVbaseCtorsBB); |
Reid Kleckner | 7810af0 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 1001 | |
| 1002 | // Fill in the vbtable pointers here. |
| 1003 | EmitVBPtrStores(CGF, RD); |
Timur Iskhodzhanov | 57cbe5c | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 1004 | |
| 1005 | // CGF will put the base ctor calls in this basic block for us later. |
| 1006 | |
| 1007 | return SkipVbaseCtorsBB; |
John McCall | 0f999f3 | 2012-09-25 08:00:39 +0000 | [diff] [blame] | 1008 | } |
| 1009 | |
Timur Iskhodzhanov | b648732 | 2013-10-09 18:16:58 +0000 | [diff] [blame] | 1010 | void MicrosoftCXXABI::initializeHiddenVirtualInheritanceMembers( |
| 1011 | CodeGenFunction &CGF, const CXXRecordDecl *RD) { |
| 1012 | // In most cases, an override for a vbase virtual method can adjust |
| 1013 | // the "this" parameter by applying a constant offset. |
| 1014 | // However, this is not enough while a constructor or a destructor of some |
| 1015 | // class X is being executed if all the following conditions are met: |
| 1016 | // - X has virtual bases, (1) |
| 1017 | // - X overrides a virtual method M of a vbase Y, (2) |
| 1018 | // - X itself is a vbase of the most derived class. |
| 1019 | // |
| 1020 | // If (1) and (2) are true, the vtorDisp for vbase Y is a hidden member of X |
| 1021 | // which holds the extra amount of "this" adjustment we must do when we use |
| 1022 | // the X vftables (i.e. during X ctor or dtor). |
| 1023 | // Outside the ctors and dtors, the values of vtorDisps are zero. |
| 1024 | |
| 1025 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
| 1026 | typedef ASTRecordLayout::VBaseOffsetsMapTy VBOffsets; |
| 1027 | const VBOffsets &VBaseMap = Layout.getVBaseOffsetsMap(); |
| 1028 | CGBuilderTy &Builder = CGF.Builder; |
| 1029 | |
| 1030 | unsigned AS = |
| 1031 | cast<llvm::PointerType>(getThisValue(CGF)->getType())->getAddressSpace(); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1032 | llvm::Value *Int8This = nullptr; // Initialize lazily. |
Timur Iskhodzhanov | b648732 | 2013-10-09 18:16:58 +0000 | [diff] [blame] | 1033 | |
| 1034 | for (VBOffsets::const_iterator I = VBaseMap.begin(), E = VBaseMap.end(); |
| 1035 | I != E; ++I) { |
| 1036 | if (!I->second.hasVtorDisp()) |
| 1037 | continue; |
| 1038 | |
Timur Iskhodzhanov | 4ddf592 | 2013-11-13 16:03:43 +0000 | [diff] [blame] | 1039 | llvm::Value *VBaseOffset = |
| 1040 | GetVirtualBaseClassOffset(CGF, getThisValue(CGF), RD, I->first); |
Timur Iskhodzhanov | b648732 | 2013-10-09 18:16:58 +0000 | [diff] [blame] | 1041 | // FIXME: it doesn't look right that we SExt in GetVirtualBaseClassOffset() |
| 1042 | // just to Trunc back immediately. |
| 1043 | VBaseOffset = Builder.CreateTruncOrBitCast(VBaseOffset, CGF.Int32Ty); |
| 1044 | uint64_t ConstantVBaseOffset = |
| 1045 | Layout.getVBaseClassOffset(I->first).getQuantity(); |
| 1046 | |
| 1047 | // vtorDisp_for_vbase = vbptr[vbase_idx] - offsetof(RD, vbase). |
| 1048 | llvm::Value *VtorDispValue = Builder.CreateSub( |
| 1049 | VBaseOffset, llvm::ConstantInt::get(CGM.Int32Ty, ConstantVBaseOffset), |
| 1050 | "vtordisp.value"); |
| 1051 | |
| 1052 | if (!Int8This) |
| 1053 | Int8This = Builder.CreateBitCast(getThisValue(CGF), |
| 1054 | CGF.Int8Ty->getPointerTo(AS)); |
| 1055 | llvm::Value *VtorDispPtr = Builder.CreateInBoundsGEP(Int8This, VBaseOffset); |
| 1056 | // vtorDisp is always the 32-bits before the vbase in the class layout. |
| 1057 | VtorDispPtr = Builder.CreateConstGEP1_32(VtorDispPtr, -4); |
| 1058 | VtorDispPtr = Builder.CreateBitCast( |
| 1059 | VtorDispPtr, CGF.Int32Ty->getPointerTo(AS), "vtordisp.ptr"); |
| 1060 | |
| 1061 | Builder.CreateStore(VtorDispValue, VtorDispPtr); |
| 1062 | } |
| 1063 | } |
| 1064 | |
David Majnemer | 37fd66e | 2015-03-13 22:36:55 +0000 | [diff] [blame] | 1065 | static bool hasDefaultCXXMethodCC(ASTContext &Context, |
| 1066 | const CXXMethodDecl *MD) { |
| 1067 | CallingConv ExpectedCallingConv = Context.getDefaultCallingConvention( |
| 1068 | /*IsVariadic=*/false, /*IsCXXMethod=*/true); |
| 1069 | CallingConv ActualCallingConv = |
| 1070 | MD->getType()->getAs<FunctionProtoType>()->getCallConv(); |
| 1071 | return ExpectedCallingConv == ActualCallingConv; |
| 1072 | } |
| 1073 | |
Timur Iskhodzhanov | 40f2fa9 | 2013-08-04 17:30:04 +0000 | [diff] [blame] | 1074 | void MicrosoftCXXABI::EmitCXXConstructors(const CXXConstructorDecl *D) { |
| 1075 | // There's only one constructor type in this ABI. |
| 1076 | CGM.EmitGlobal(GlobalDecl(D, Ctor_Complete)); |
David Majnemer | 37fd66e | 2015-03-13 22:36:55 +0000 | [diff] [blame] | 1077 | |
| 1078 | // Exported default constructors either have a simple call-site where they use |
| 1079 | // the typical calling convention and have a single 'this' pointer for an |
| 1080 | // argument -or- they get a wrapper function which appropriately thunks to the |
| 1081 | // real default constructor. This thunk is the default constructor closure. |
| 1082 | if (D->hasAttr<DLLExportAttr>() && D->isDefaultConstructor()) |
| 1083 | if (!hasDefaultCXXMethodCC(getContext(), D) || D->getNumParams() != 0) { |
| 1084 | llvm::Function *Fn = getAddrOfCXXCtorClosure(D, Ctor_DefaultClosure); |
| 1085 | Fn->setLinkage(llvm::GlobalValue::WeakODRLinkage); |
| 1086 | Fn->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass); |
| 1087 | } |
Timur Iskhodzhanov | 40f2fa9 | 2013-08-04 17:30:04 +0000 | [diff] [blame] | 1088 | } |
| 1089 | |
Reid Kleckner | 7810af0 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 1090 | void MicrosoftCXXABI::EmitVBPtrStores(CodeGenFunction &CGF, |
| 1091 | const CXXRecordDecl *RD) { |
| 1092 | llvm::Value *ThisInt8Ptr = |
| 1093 | CGF.Builder.CreateBitCast(getThisValue(CGF), CGM.Int8PtrTy, "this.int8"); |
David Majnemer | 9ced3dd | 2015-03-14 23:44:48 +0000 | [diff] [blame] | 1094 | const ASTContext &Context = getContext(); |
| 1095 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
Reid Kleckner | 7810af0 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 1096 | |
Reid Kleckner | b40a27d | 2014-01-03 00:14:35 +0000 | [diff] [blame] | 1097 | const VBTableGlobals &VBGlobals = enumerateVBTables(RD); |
| 1098 | for (unsigned I = 0, E = VBGlobals.VBTables->size(); I != E; ++I) { |
Reid Kleckner | 9c6e9e3 | 2014-02-27 19:40:09 +0000 | [diff] [blame] | 1099 | const VPtrInfo *VBT = (*VBGlobals.VBTables)[I]; |
Reid Kleckner | b40a27d | 2014-01-03 00:14:35 +0000 | [diff] [blame] | 1100 | llvm::GlobalVariable *GV = VBGlobals.Globals[I]; |
Reid Kleckner | 7810af0 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 1101 | const ASTRecordLayout &SubobjectLayout = |
David Majnemer | 9ced3dd | 2015-03-14 23:44:48 +0000 | [diff] [blame] | 1102 | Context.getASTRecordLayout(VBT->BaseWithVPtr); |
Reid Kleckner | 5f08094 | 2014-01-03 23:42:00 +0000 | [diff] [blame] | 1103 | CharUnits Offs = VBT->NonVirtualOffset; |
| 1104 | Offs += SubobjectLayout.getVBPtrOffset(); |
Reid Kleckner | 9c6e9e3 | 2014-02-27 19:40:09 +0000 | [diff] [blame] | 1105 | if (VBT->getVBaseWithVPtr()) |
| 1106 | Offs += Layout.getVBaseClassOffset(VBT->getVBaseWithVPtr()); |
Reid Kleckner | 7810af0 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 1107 | llvm::Value *VBPtr = |
Reid Kleckner | 5f08094 | 2014-01-03 23:42:00 +0000 | [diff] [blame] | 1108 | CGF.Builder.CreateConstInBoundsGEP1_64(ThisInt8Ptr, Offs.getQuantity()); |
Reid Kleckner | 0ba8ba4 | 2014-10-22 17:26:00 +0000 | [diff] [blame] | 1109 | llvm::Value *GVPtr = CGF.Builder.CreateConstInBoundsGEP2_32(GV, 0, 0); |
| 1110 | VBPtr = CGF.Builder.CreateBitCast(VBPtr, GVPtr->getType()->getPointerTo(0), |
Reid Kleckner | 5f08094 | 2014-01-03 23:42:00 +0000 | [diff] [blame] | 1111 | "vbptr." + VBT->ReusingBase->getName()); |
Reid Kleckner | 0ba8ba4 | 2014-10-22 17:26:00 +0000 | [diff] [blame] | 1112 | CGF.Builder.CreateStore(GVPtr, VBPtr); |
Reid Kleckner | 7810af0 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 1113 | } |
| 1114 | } |
| 1115 | |
Rafael Espindola | 8d2a19b | 2014-09-08 16:01:27 +0000 | [diff] [blame] | 1116 | void |
| 1117 | MicrosoftCXXABI::buildStructorSignature(const CXXMethodDecl *MD, StructorType T, |
Timur Iskhodzhanov | ee6bc53 | 2013-02-13 08:37:51 +0000 | [diff] [blame] | 1118 | SmallVectorImpl<CanQualType> &ArgTys) { |
Timur Iskhodzhanov | ee6bc53 | 2013-02-13 08:37:51 +0000 | [diff] [blame] | 1119 | // TODO: 'for base' flag |
Rafael Espindola | 8d2a19b | 2014-09-08 16:01:27 +0000 | [diff] [blame] | 1120 | if (T == StructorType::Deleting) { |
Timur Iskhodzhanov | 701981f | 2013-08-27 10:38:19 +0000 | [diff] [blame] | 1121 | // The scalar deleting destructor takes an implicit int parameter. |
David Majnemer | 9ced3dd | 2015-03-14 23:44:48 +0000 | [diff] [blame] | 1122 | ArgTys.push_back(getContext().IntTy); |
Timur Iskhodzhanov | ee6bc53 | 2013-02-13 08:37:51 +0000 | [diff] [blame] | 1123 | } |
Rafael Espindola | 8d2a19b | 2014-09-08 16:01:27 +0000 | [diff] [blame] | 1124 | auto *CD = dyn_cast<CXXConstructorDecl>(MD); |
| 1125 | if (!CD) |
| 1126 | return; |
| 1127 | |
| 1128 | // All parameters are already in place except is_most_derived, which goes |
| 1129 | // after 'this' if it's variadic and last if it's not. |
| 1130 | |
| 1131 | const CXXRecordDecl *Class = CD->getParent(); |
| 1132 | const FunctionProtoType *FPT = CD->getType()->castAs<FunctionProtoType>(); |
| 1133 | if (Class->getNumVBases()) { |
| 1134 | if (FPT->isVariadic()) |
David Majnemer | 9ced3dd | 2015-03-14 23:44:48 +0000 | [diff] [blame] | 1135 | ArgTys.insert(ArgTys.begin() + 1, getContext().IntTy); |
Rafael Espindola | 8d2a19b | 2014-09-08 16:01:27 +0000 | [diff] [blame] | 1136 | else |
David Majnemer | 9ced3dd | 2015-03-14 23:44:48 +0000 | [diff] [blame] | 1137 | ArgTys.push_back(getContext().IntTy); |
Rafael Espindola | 8d2a19b | 2014-09-08 16:01:27 +0000 | [diff] [blame] | 1138 | } |
Timur Iskhodzhanov | ee6bc53 | 2013-02-13 08:37:51 +0000 | [diff] [blame] | 1139 | } |
| 1140 | |
Reid Kleckner | e7de47e | 2013-07-22 13:51:44 +0000 | [diff] [blame] | 1141 | void MicrosoftCXXABI::EmitCXXDestructors(const CXXDestructorDecl *D) { |
| 1142 | // The TU defining a dtor is only guaranteed to emit a base destructor. All |
| 1143 | // other destructor variants are delegating thunks. |
| 1144 | CGM.EmitGlobal(GlobalDecl(D, Dtor_Base)); |
| 1145 | } |
| 1146 | |
Timur Iskhodzhanov | f174942 | 2014-03-14 17:43:37 +0000 | [diff] [blame] | 1147 | CharUnits |
| 1148 | MicrosoftCXXABI::getVirtualFunctionPrologueThisAdjustment(GlobalDecl GD) { |
Timur Iskhodzhanov | 88fd439 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 1149 | GD = GD.getCanonicalDecl(); |
| 1150 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
Timur Iskhodzhanov | f174942 | 2014-03-14 17:43:37 +0000 | [diff] [blame] | 1151 | |
| 1152 | GlobalDecl LookupGD = GD; |
| 1153 | if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) { |
| 1154 | // Complete destructors take a pointer to the complete object as a |
| 1155 | // parameter, thus don't need this adjustment. |
| 1156 | if (GD.getDtorType() == Dtor_Complete) |
| 1157 | return CharUnits(); |
| 1158 | |
| 1159 | // There's no Dtor_Base in vftable but it shares the this adjustment with |
| 1160 | // the deleting one, so look it up instead. |
| 1161 | LookupGD = GlobalDecl(DD, Dtor_Deleting); |
| 1162 | } |
| 1163 | |
| 1164 | MicrosoftVTableContext::MethodVFTableLocation ML = |
| 1165 | CGM.getMicrosoftVTableContext().getMethodVFTableLocation(LookupGD); |
| 1166 | CharUnits Adjustment = ML.VFPtrOffset; |
| 1167 | |
| 1168 | // Normal virtual instance methods need to adjust from the vfptr that first |
| 1169 | // defined the virtual method to the virtual base subobject, but destructors |
| 1170 | // do not. The vector deleting destructor thunk applies this adjustment for |
| 1171 | // us if necessary. |
| 1172 | if (isa<CXXDestructorDecl>(MD)) |
| 1173 | Adjustment = CharUnits::Zero(); |
| 1174 | |
| 1175 | if (ML.VBase) { |
| 1176 | const ASTRecordLayout &DerivedLayout = |
David Majnemer | 9ced3dd | 2015-03-14 23:44:48 +0000 | [diff] [blame] | 1177 | getContext().getASTRecordLayout(MD->getParent()); |
Timur Iskhodzhanov | f174942 | 2014-03-14 17:43:37 +0000 | [diff] [blame] | 1178 | Adjustment += DerivedLayout.getVBaseClassOffset(ML.VBase); |
| 1179 | } |
| 1180 | |
| 1181 | return Adjustment; |
| 1182 | } |
| 1183 | |
| 1184 | llvm::Value *MicrosoftCXXABI::adjustThisArgumentForVirtualFunctionCall( |
| 1185 | CodeGenFunction &CGF, GlobalDecl GD, llvm::Value *This, bool VirtualCall) { |
| 1186 | if (!VirtualCall) { |
| 1187 | // If the call of a virtual function is not virtual, we just have to |
| 1188 | // compensate for the adjustment the virtual function does in its prologue. |
| 1189 | CharUnits Adjustment = getVirtualFunctionPrologueThisAdjustment(GD); |
| 1190 | if (Adjustment.isZero()) |
| 1191 | return This; |
| 1192 | |
| 1193 | unsigned AS = cast<llvm::PointerType>(This->getType())->getAddressSpace(); |
| 1194 | llvm::Type *charPtrTy = CGF.Int8Ty->getPointerTo(AS); |
| 1195 | This = CGF.Builder.CreateBitCast(This, charPtrTy); |
| 1196 | assert(Adjustment.isPositive()); |
| 1197 | return CGF.Builder.CreateConstGEP1_32(This, Adjustment.getQuantity()); |
| 1198 | } |
| 1199 | |
| 1200 | GD = GD.getCanonicalDecl(); |
| 1201 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
Timur Iskhodzhanov | 88fd439 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 1202 | |
Timur Iskhodzhanov | 62082b7 | 2013-10-16 18:24:06 +0000 | [diff] [blame] | 1203 | GlobalDecl LookupGD = GD; |
| 1204 | if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) { |
| 1205 | // Complete dtors take a pointer to the complete object, |
| 1206 | // thus don't need adjustment. |
| 1207 | if (GD.getDtorType() == Dtor_Complete) |
| 1208 | return This; |
| 1209 | |
| 1210 | // There's only Dtor_Deleting in vftable but it shares the this adjustment |
| 1211 | // with the base one, so look up the deleting one instead. |
| 1212 | LookupGD = GlobalDecl(DD, Dtor_Deleting); |
| 1213 | } |
Timur Iskhodzhanov | 5877663 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 1214 | MicrosoftVTableContext::MethodVFTableLocation ML = |
| 1215 | CGM.getMicrosoftVTableContext().getMethodVFTableLocation(LookupGD); |
Timur Iskhodzhanov | 88fd439 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 1216 | |
| 1217 | unsigned AS = cast<llvm::PointerType>(This->getType())->getAddressSpace(); |
| 1218 | llvm::Type *charPtrTy = CGF.Int8Ty->getPointerTo(AS); |
Timur Iskhodzhanov | 9e7f505 | 2013-11-07 13:34:02 +0000 | [diff] [blame] | 1219 | CharUnits StaticOffset = ML.VFPtrOffset; |
Reid Kleckner | 0c12b36 | 2014-02-18 22:51:52 +0000 | [diff] [blame] | 1220 | |
| 1221 | // Base destructors expect 'this' to point to the beginning of the base |
| 1222 | // subobject, not the first vfptr that happens to contain the virtual dtor. |
| 1223 | // However, we still need to apply the virtual base adjustment. |
| 1224 | if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() == Dtor_Base) |
| 1225 | StaticOffset = CharUnits::Zero(); |
| 1226 | |
Timur Iskhodzhanov | 62082b7 | 2013-10-16 18:24:06 +0000 | [diff] [blame] | 1227 | if (ML.VBase) { |
Timur Iskhodzhanov | f174942 | 2014-03-14 17:43:37 +0000 | [diff] [blame] | 1228 | This = CGF.Builder.CreateBitCast(This, charPtrTy); |
| 1229 | llvm::Value *VBaseOffset = |
| 1230 | GetVirtualBaseClassOffset(CGF, This, MD->getParent(), ML.VBase); |
| 1231 | This = CGF.Builder.CreateInBoundsGEP(This, VBaseOffset); |
Timur Iskhodzhanov | 62082b7 | 2013-10-16 18:24:06 +0000 | [diff] [blame] | 1232 | } |
Timur Iskhodzhanov | 88fd439 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 1233 | if (!StaticOffset.isZero()) { |
| 1234 | assert(StaticOffset.isPositive()); |
| 1235 | This = CGF.Builder.CreateBitCast(This, charPtrTy); |
Timur Iskhodzhanov | 827365e | 2013-10-22 18:15:24 +0000 | [diff] [blame] | 1236 | if (ML.VBase) { |
| 1237 | // Non-virtual adjustment might result in a pointer outside the allocated |
| 1238 | // object, e.g. if the final overrider class is laid out after the virtual |
| 1239 | // base that declares a method in the most derived class. |
| 1240 | // FIXME: Update the code that emits this adjustment in thunks prologues. |
| 1241 | This = CGF.Builder.CreateConstGEP1_32(This, StaticOffset.getQuantity()); |
| 1242 | } else { |
| 1243 | This = CGF.Builder.CreateConstInBoundsGEP1_32(This, |
| 1244 | StaticOffset.getQuantity()); |
| 1245 | } |
Timur Iskhodzhanov | 88fd439 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 1246 | } |
| 1247 | return This; |
| 1248 | } |
| 1249 | |
Reid Kleckner | 89077a1 | 2013-12-17 19:46:40 +0000 | [diff] [blame] | 1250 | void MicrosoftCXXABI::addImplicitStructorParams(CodeGenFunction &CGF, |
| 1251 | QualType &ResTy, |
| 1252 | FunctionArgList &Params) { |
Timur Iskhodzhanov | 57cbe5c | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 1253 | ASTContext &Context = getContext(); |
| 1254 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(CGF.CurGD.getDecl()); |
Reid Kleckner | 89077a1 | 2013-12-17 19:46:40 +0000 | [diff] [blame] | 1255 | assert(isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD)); |
Timur Iskhodzhanov | 57cbe5c | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 1256 | if (isa<CXXConstructorDecl>(MD) && MD->getParent()->getNumVBases()) { |
| 1257 | ImplicitParamDecl *IsMostDerived |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1258 | = ImplicitParamDecl::Create(Context, nullptr, |
Timur Iskhodzhanov | 57cbe5c | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 1259 | CGF.CurGD.getDecl()->getLocation(), |
| 1260 | &Context.Idents.get("is_most_derived"), |
| 1261 | Context.IntTy); |
Reid Kleckner | 89077a1 | 2013-12-17 19:46:40 +0000 | [diff] [blame] | 1262 | // The 'most_derived' parameter goes second if the ctor is variadic and last |
| 1263 | // if it's not. Dtors can't be variadic. |
| 1264 | const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>(); |
| 1265 | if (FPT->isVariadic()) |
| 1266 | Params.insert(Params.begin() + 1, IsMostDerived); |
| 1267 | else |
| 1268 | Params.push_back(IsMostDerived); |
Timur Iskhodzhanov | 57cbe5c | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 1269 | getStructorImplicitParamDecl(CGF) = IsMostDerived; |
David Majnemer | 0c0b6d9 | 2014-10-31 20:09:12 +0000 | [diff] [blame] | 1270 | } else if (isDeletingDtor(CGF.CurGD)) { |
Timur Iskhodzhanov | ee6bc53 | 2013-02-13 08:37:51 +0000 | [diff] [blame] | 1271 | ImplicitParamDecl *ShouldDelete |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1272 | = ImplicitParamDecl::Create(Context, nullptr, |
Timur Iskhodzhanov | ee6bc53 | 2013-02-13 08:37:51 +0000 | [diff] [blame] | 1273 | CGF.CurGD.getDecl()->getLocation(), |
| 1274 | &Context.Idents.get("should_call_delete"), |
Timur Iskhodzhanov | 701981f | 2013-08-27 10:38:19 +0000 | [diff] [blame] | 1275 | Context.IntTy); |
Timur Iskhodzhanov | ee6bc53 | 2013-02-13 08:37:51 +0000 | [diff] [blame] | 1276 | Params.push_back(ShouldDelete); |
| 1277 | getStructorImplicitParamDecl(CGF) = ShouldDelete; |
| 1278 | } |
John McCall | 0f999f3 | 2012-09-25 08:00:39 +0000 | [diff] [blame] | 1279 | } |
| 1280 | |
Timur Iskhodzhanov | 88fd439 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 1281 | llvm::Value *MicrosoftCXXABI::adjustThisParameterInVirtualFunctionPrologue( |
| 1282 | CodeGenFunction &CGF, GlobalDecl GD, llvm::Value *This) { |
Timur Iskhodzhanov | 88fd439 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 1283 | // In this ABI, every virtual function takes a pointer to one of the |
| 1284 | // subobjects that first defines it as the 'this' parameter, rather than a |
Alp Toker | f6a24ce | 2013-12-05 16:25:25 +0000 | [diff] [blame] | 1285 | // pointer to the final overrider subobject. Thus, we need to adjust it back |
Timur Iskhodzhanov | 88fd439 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 1286 | // to the final overrider subobject before use. |
| 1287 | // See comments in the MicrosoftVFTableContext implementation for the details. |
Timur Iskhodzhanov | f174942 | 2014-03-14 17:43:37 +0000 | [diff] [blame] | 1288 | CharUnits Adjustment = getVirtualFunctionPrologueThisAdjustment(GD); |
Timur Iskhodzhanov | 88fd439 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 1289 | if (Adjustment.isZero()) |
| 1290 | return This; |
| 1291 | |
| 1292 | unsigned AS = cast<llvm::PointerType>(This->getType())->getAddressSpace(); |
| 1293 | llvm::Type *charPtrTy = CGF.Int8Ty->getPointerTo(AS), |
| 1294 | *thisTy = This->getType(); |
| 1295 | |
| 1296 | This = CGF.Builder.CreateBitCast(This, charPtrTy); |
| 1297 | assert(Adjustment.isPositive()); |
Timur Iskhodzhanov | 827365e | 2013-10-22 18:15:24 +0000 | [diff] [blame] | 1298 | This = |
| 1299 | CGF.Builder.CreateConstInBoundsGEP1_32(This, -Adjustment.getQuantity()); |
Timur Iskhodzhanov | 88fd439 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 1300 | return CGF.Builder.CreateBitCast(This, thisTy); |
| 1301 | } |
| 1302 | |
John McCall | 0f999f3 | 2012-09-25 08:00:39 +0000 | [diff] [blame] | 1303 | void MicrosoftCXXABI::EmitInstanceFunctionProlog(CodeGenFunction &CGF) { |
| 1304 | EmitThisParam(CGF); |
Stephen Lin | 9dc6eef | 2013-06-30 20:40:16 +0000 | [diff] [blame] | 1305 | |
| 1306 | /// If this is a function that the ABI specifies returns 'this', initialize |
| 1307 | /// the return slot to 'this' at the start of the function. |
| 1308 | /// |
| 1309 | /// Unlike the setting of return types, this is done within the ABI |
| 1310 | /// implementation instead of by clients of CGCXXABI because: |
| 1311 | /// 1) getThisValue is currently protected |
| 1312 | /// 2) in theory, an ABI could implement 'this' returns some other way; |
| 1313 | /// HasThisReturn only specifies a contract, not the implementation |
| 1314 | if (HasThisReturn(CGF.CurGD)) |
John McCall | 0f999f3 | 2012-09-25 08:00:39 +0000 | [diff] [blame] | 1315 | CGF.Builder.CreateStore(getThisValue(CGF), CGF.ReturnValue); |
David Majnemer | 0c0b6d9 | 2014-10-31 20:09:12 +0000 | [diff] [blame] | 1316 | else if (hasMostDerivedReturn(CGF.CurGD)) |
| 1317 | CGF.Builder.CreateStore(CGF.EmitCastToVoidPtr(getThisValue(CGF)), |
| 1318 | CGF.ReturnValue); |
Timur Iskhodzhanov | 57cbe5c | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 1319 | |
| 1320 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(CGF.CurGD.getDecl()); |
| 1321 | if (isa<CXXConstructorDecl>(MD) && MD->getParent()->getNumVBases()) { |
| 1322 | assert(getStructorImplicitParamDecl(CGF) && |
| 1323 | "no implicit parameter for a constructor with virtual bases?"); |
| 1324 | getStructorImplicitParamValue(CGF) |
| 1325 | = CGF.Builder.CreateLoad( |
| 1326 | CGF.GetAddrOfLocalVar(getStructorImplicitParamDecl(CGF)), |
| 1327 | "is_most_derived"); |
| 1328 | } |
| 1329 | |
David Majnemer | 0c0b6d9 | 2014-10-31 20:09:12 +0000 | [diff] [blame] | 1330 | if (isDeletingDtor(CGF.CurGD)) { |
Timur Iskhodzhanov | ee6bc53 | 2013-02-13 08:37:51 +0000 | [diff] [blame] | 1331 | assert(getStructorImplicitParamDecl(CGF) && |
| 1332 | "no implicit parameter for a deleting destructor?"); |
| 1333 | getStructorImplicitParamValue(CGF) |
| 1334 | = CGF.Builder.CreateLoad( |
| 1335 | CGF.GetAddrOfLocalVar(getStructorImplicitParamDecl(CGF)), |
| 1336 | "should_call_delete"); |
| 1337 | } |
John McCall | 0f999f3 | 2012-09-25 08:00:39 +0000 | [diff] [blame] | 1338 | } |
| 1339 | |
Reid Kleckner | 89077a1 | 2013-12-17 19:46:40 +0000 | [diff] [blame] | 1340 | unsigned MicrosoftCXXABI::addImplicitConstructorArgs( |
| 1341 | CodeGenFunction &CGF, const CXXConstructorDecl *D, CXXCtorType Type, |
| 1342 | bool ForVirtualBase, bool Delegating, CallArgList &Args) { |
Timur Iskhodzhanov | 57cbe5c | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 1343 | assert(Type == Ctor_Complete || Type == Ctor_Base); |
Timur Iskhodzhanov | 57cbe5c | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 1344 | |
Reid Kleckner | 89077a1 | 2013-12-17 19:46:40 +0000 | [diff] [blame] | 1345 | // Check if we need a 'most_derived' parameter. |
| 1346 | if (!D->getParent()->getNumVBases()) |
| 1347 | return 0; |
| 1348 | |
| 1349 | // Add the 'most_derived' argument second if we are variadic or last if not. |
| 1350 | const FunctionProtoType *FPT = D->getType()->castAs<FunctionProtoType>(); |
| 1351 | llvm::Value *MostDerivedArg = |
| 1352 | llvm::ConstantInt::get(CGM.Int32Ty, Type == Ctor_Complete); |
| 1353 | RValue RV = RValue::get(MostDerivedArg); |
| 1354 | if (MostDerivedArg) { |
| 1355 | if (FPT->isVariadic()) |
| 1356 | Args.insert(Args.begin() + 1, |
| 1357 | CallArg(RV, getContext().IntTy, /*needscopy=*/false)); |
| 1358 | else |
| 1359 | Args.add(RV, getContext().IntTy); |
Timur Iskhodzhanov | 57cbe5c | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 1360 | } |
| 1361 | |
Reid Kleckner | 89077a1 | 2013-12-17 19:46:40 +0000 | [diff] [blame] | 1362 | return 1; // Added one arg. |
Timur Iskhodzhanov | 57cbe5c | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 1363 | } |
| 1364 | |
Reid Kleckner | 6fe771a | 2013-12-13 00:53:54 +0000 | [diff] [blame] | 1365 | void MicrosoftCXXABI::EmitDestructorCall(CodeGenFunction &CGF, |
| 1366 | const CXXDestructorDecl *DD, |
| 1367 | CXXDtorType Type, bool ForVirtualBase, |
| 1368 | bool Delegating, llvm::Value *This) { |
Rafael Espindola | 1ac0ec8 | 2014-09-11 15:42:06 +0000 | [diff] [blame] | 1369 | llvm::Value *Callee = CGM.getAddrOfCXXStructor(DD, getFromDtorType(Type)); |
Reid Kleckner | 6fe771a | 2013-12-13 00:53:54 +0000 | [diff] [blame] | 1370 | |
Timur Iskhodzhanov | f174942 | 2014-03-14 17:43:37 +0000 | [diff] [blame] | 1371 | if (DD->isVirtual()) { |
| 1372 | assert(Type != CXXDtorType::Dtor_Deleting && |
| 1373 | "The deleting destructor should only be called via a virtual call"); |
| 1374 | This = adjustThisArgumentForVirtualFunctionCall(CGF, GlobalDecl(DD, Type), |
| 1375 | This, false); |
| 1376 | } |
Reid Kleckner | 6fe771a | 2013-12-13 00:53:54 +0000 | [diff] [blame] | 1377 | |
David Majnemer | 0c0b6d9 | 2014-10-31 20:09:12 +0000 | [diff] [blame] | 1378 | CGF.EmitCXXStructorCall(DD, Callee, ReturnValueSlot(), This, |
| 1379 | /*ImplicitParam=*/nullptr, |
| 1380 | /*ImplicitParamTy=*/QualType(), nullptr, |
| 1381 | getFromDtorType(Type)); |
Reid Kleckner | 6fe771a | 2013-12-13 00:53:54 +0000 | [diff] [blame] | 1382 | } |
| 1383 | |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1384 | void MicrosoftCXXABI::emitVTableDefinitions(CodeGenVTables &CGVT, |
| 1385 | const CXXRecordDecl *RD) { |
Timur Iskhodzhanov | 5877663 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 1386 | MicrosoftVTableContext &VFTContext = CGM.getMicrosoftVTableContext(); |
Benjamin Kramer | 22c68ef | 2014-09-11 14:13:49 +0000 | [diff] [blame] | 1387 | const VPtrInfoVector &VFPtrs = VFTContext.getVFPtrOffsets(RD); |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1388 | |
Warren Hunt | 5c2b4ea | 2014-05-23 16:07:43 +0000 | [diff] [blame] | 1389 | for (VPtrInfo *Info : VFPtrs) { |
| 1390 | llvm::GlobalVariable *VTable = getAddrOfVTable(RD, Info->FullOffsetInMDC); |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1391 | if (VTable->hasInitializer()) |
| 1392 | continue; |
David Majnemer | d905da4 | 2014-07-01 20:30:31 +0000 | [diff] [blame] | 1393 | |
David Majnemer | 65f8732 | 2014-07-24 06:09:19 +0000 | [diff] [blame] | 1394 | llvm::Constant *RTTI = getContext().getLangOpts().RTTIData |
| 1395 | ? getMSCompleteObjectLocator(RD, Info) |
| 1396 | : nullptr; |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1397 | |
| 1398 | const VTableLayout &VTLayout = |
Warren Hunt | 5c2b4ea | 2014-05-23 16:07:43 +0000 | [diff] [blame] | 1399 | VFTContext.getVFTableLayout(RD, Info->FullOffsetInMDC); |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1400 | llvm::Constant *Init = CGVT.CreateVTableInitializer( |
| 1401 | RD, VTLayout.vtable_component_begin(), |
| 1402 | VTLayout.getNumVTableComponents(), VTLayout.vtable_thunk_begin(), |
David Majnemer | d905da4 | 2014-07-01 20:30:31 +0000 | [diff] [blame] | 1403 | VTLayout.getNumVTableThunks(), RTTI); |
| 1404 | |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1405 | VTable->setInitializer(Init); |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1406 | } |
| 1407 | } |
| 1408 | |
| 1409 | llvm::Value *MicrosoftCXXABI::getVTableAddressPointInStructor( |
| 1410 | CodeGenFunction &CGF, const CXXRecordDecl *VTableClass, BaseSubobject Base, |
| 1411 | const CXXRecordDecl *NearestVBase, bool &NeedsVirtualOffset) { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1412 | NeedsVirtualOffset = (NearestVBase != nullptr); |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1413 | |
David Majnemer | d905da4 | 2014-07-01 20:30:31 +0000 | [diff] [blame] | 1414 | (void)getAddrOfVTable(VTableClass, Base.getBaseOffset()); |
| 1415 | VFTableIdTy ID(VTableClass, Base.getBaseOffset()); |
| 1416 | llvm::GlobalValue *VTableAddressPoint = VFTablesMap[ID]; |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1417 | if (!VTableAddressPoint) { |
| 1418 | assert(Base.getBase()->getNumVBases() && |
David Majnemer | 9ced3dd | 2015-03-14 23:44:48 +0000 | [diff] [blame] | 1419 | !getContext().getASTRecordLayout(Base.getBase()).hasOwnVFPtr()); |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1420 | } |
| 1421 | return VTableAddressPoint; |
| 1422 | } |
| 1423 | |
Timur Iskhodzhanov | 6745522 | 2013-10-03 06:26:13 +0000 | [diff] [blame] | 1424 | static void mangleVFTableName(MicrosoftMangleContext &MangleContext, |
Reid Kleckner | 9c6e9e3 | 2014-02-27 19:40:09 +0000 | [diff] [blame] | 1425 | const CXXRecordDecl *RD, const VPtrInfo *VFPtr, |
Timur Iskhodzhanov | 6745522 | 2013-10-03 06:26:13 +0000 | [diff] [blame] | 1426 | SmallString<256> &Name) { |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1427 | llvm::raw_svector_ostream Out(Name); |
Reid Kleckner | 9c6e9e3 | 2014-02-27 19:40:09 +0000 | [diff] [blame] | 1428 | MangleContext.mangleCXXVFTable(RD, VFPtr->MangledPath, Out); |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1429 | } |
| 1430 | |
| 1431 | llvm::Constant *MicrosoftCXXABI::getVTableAddressPointForConstExpr( |
| 1432 | BaseSubobject Base, const CXXRecordDecl *VTableClass) { |
David Majnemer | d905da4 | 2014-07-01 20:30:31 +0000 | [diff] [blame] | 1433 | (void)getAddrOfVTable(VTableClass, Base.getBaseOffset()); |
| 1434 | VFTableIdTy ID(VTableClass, Base.getBaseOffset()); |
| 1435 | llvm::GlobalValue *VFTable = VFTablesMap[ID]; |
| 1436 | assert(VFTable && "Couldn't find a vftable for the given base?"); |
| 1437 | return VFTable; |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1438 | } |
| 1439 | |
| 1440 | llvm::GlobalVariable *MicrosoftCXXABI::getAddrOfVTable(const CXXRecordDecl *RD, |
| 1441 | CharUnits VPtrOffset) { |
| 1442 | // getAddrOfVTable may return 0 if asked to get an address of a vtable which |
| 1443 | // shouldn't be used in the given record type. We want to cache this result in |
| 1444 | // VFTablesMap, thus a simple zero check is not sufficient. |
| 1445 | VFTableIdTy ID(RD, VPtrOffset); |
David Majnemer | d905da4 | 2014-07-01 20:30:31 +0000 | [diff] [blame] | 1446 | VTablesMapTy::iterator I; |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1447 | bool Inserted; |
David Majnemer | d905da4 | 2014-07-01 20:30:31 +0000 | [diff] [blame] | 1448 | std::tie(I, Inserted) = VTablesMap.insert(std::make_pair(ID, nullptr)); |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1449 | if (!Inserted) |
| 1450 | return I->second; |
| 1451 | |
| 1452 | llvm::GlobalVariable *&VTable = I->second; |
| 1453 | |
Timur Iskhodzhanov | 5877663 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 1454 | MicrosoftVTableContext &VTContext = CGM.getMicrosoftVTableContext(); |
Reid Kleckner | 9c6e9e3 | 2014-02-27 19:40:09 +0000 | [diff] [blame] | 1455 | const VPtrInfoVector &VFPtrs = VTContext.getVFPtrOffsets(RD); |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1456 | |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 1457 | if (DeferredVFTables.insert(RD).second) { |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1458 | // We haven't processed this record type before. |
| 1459 | // Queue up this v-table for possible deferred emission. |
| 1460 | CGM.addDeferredVTable(RD); |
| 1461 | |
| 1462 | #ifndef NDEBUG |
| 1463 | // Create all the vftables at once in order to make sure each vftable has |
| 1464 | // a unique mangled name. |
| 1465 | llvm::StringSet<> ObservedMangledNames; |
| 1466 | for (size_t J = 0, F = VFPtrs.size(); J != F; ++J) { |
| 1467 | SmallString<256> Name; |
Timur Iskhodzhanov | 6745522 | 2013-10-03 06:26:13 +0000 | [diff] [blame] | 1468 | mangleVFTableName(getMangleContext(), RD, VFPtrs[J], Name); |
David Blaikie | 61b86d4 | 2014-11-19 02:56:13 +0000 | [diff] [blame] | 1469 | if (!ObservedMangledNames.insert(Name.str()).second) |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1470 | llvm_unreachable("Already saw this mangling before?"); |
| 1471 | } |
| 1472 | #endif |
| 1473 | } |
| 1474 | |
| 1475 | for (size_t J = 0, F = VFPtrs.size(); J != F; ++J) { |
Reid Kleckner | 9c6e9e3 | 2014-02-27 19:40:09 +0000 | [diff] [blame] | 1476 | if (VFPtrs[J]->FullOffsetInMDC != VPtrOffset) |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1477 | continue; |
David Majnemer | d905da4 | 2014-07-01 20:30:31 +0000 | [diff] [blame] | 1478 | SmallString<256> VFTableName; |
| 1479 | mangleVFTableName(getMangleContext(), RD, VFPtrs[J], VFTableName); |
| 1480 | StringRef VTableName = VFTableName; |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1481 | |
David Majnemer | d905da4 | 2014-07-01 20:30:31 +0000 | [diff] [blame] | 1482 | uint64_t NumVTableSlots = |
Reid Kleckner | 9c6e9e3 | 2014-02-27 19:40:09 +0000 | [diff] [blame] | 1483 | VTContext.getVFTableLayout(RD, VFPtrs[J]->FullOffsetInMDC) |
David Majnemer | d905da4 | 2014-07-01 20:30:31 +0000 | [diff] [blame] | 1484 | .getNumVTableComponents(); |
| 1485 | llvm::GlobalValue::LinkageTypes VTableLinkage = |
| 1486 | llvm::GlobalValue::ExternalLinkage; |
| 1487 | llvm::ArrayType *VTableType = |
| 1488 | llvm::ArrayType::get(CGM.Int8PtrTy, NumVTableSlots); |
David Majnemer | f607234 | 2014-07-01 22:24:56 +0000 | [diff] [blame] | 1489 | if (getContext().getLangOpts().RTTIData) { |
David Majnemer | d905da4 | 2014-07-01 20:30:31 +0000 | [diff] [blame] | 1490 | VTableLinkage = llvm::GlobalValue::PrivateLinkage; |
| 1491 | VTableName = ""; |
| 1492 | } |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1493 | |
David Majnemer | d905da4 | 2014-07-01 20:30:31 +0000 | [diff] [blame] | 1494 | VTable = CGM.getModule().getNamedGlobal(VFTableName); |
| 1495 | if (!VTable) { |
David Majnemer | bb84871 | 2014-07-01 22:37:08 +0000 | [diff] [blame] | 1496 | // Create a backing variable for the contents of VTable. The VTable may |
| 1497 | // or may not include space for a pointer to RTTI data. |
David Majnemer | d905da4 | 2014-07-01 20:30:31 +0000 | [diff] [blame] | 1498 | llvm::GlobalValue *VFTable = VTable = new llvm::GlobalVariable( |
| 1499 | CGM.getModule(), VTableType, /*isConstant=*/true, VTableLinkage, |
| 1500 | /*Initializer=*/nullptr, VTableName); |
| 1501 | VTable->setUnnamedAddr(true); |
David Majnemer | bb84871 | 2014-07-01 22:37:08 +0000 | [diff] [blame] | 1502 | |
| 1503 | // Only insert a pointer into the VFTable for RTTI data if we are not |
| 1504 | // importing it. We never reference the RTTI data directly so there is no |
| 1505 | // need to make room for it. |
David Majnemer | f607234 | 2014-07-01 22:24:56 +0000 | [diff] [blame] | 1506 | if (getContext().getLangOpts().RTTIData && |
| 1507 | !RD->hasAttr<DLLImportAttr>()) { |
David Majnemer | d905da4 | 2014-07-01 20:30:31 +0000 | [diff] [blame] | 1508 | llvm::Value *GEPIndices[] = {llvm::ConstantInt::get(CGM.IntTy, 0), |
| 1509 | llvm::ConstantInt::get(CGM.IntTy, 1)}; |
David Majnemer | bb84871 | 2014-07-01 22:37:08 +0000 | [diff] [blame] | 1510 | // Create a GEP which points just after the first entry in the VFTable, |
| 1511 | // this should be the location of the first virtual method. |
David Majnemer | d905da4 | 2014-07-01 20:30:31 +0000 | [diff] [blame] | 1512 | llvm::Constant *VTableGEP = |
| 1513 | llvm::ConstantExpr::getInBoundsGetElementPtr(VTable, GEPIndices); |
David Majnemer | bb84871 | 2014-07-01 22:37:08 +0000 | [diff] [blame] | 1514 | // The symbol for the VFTable is an alias to the GEP. It is |
| 1515 | // transparent, to other modules, what the nature of this symbol is; all |
| 1516 | // that matters is that the alias be the address of the first virtual |
| 1517 | // method. |
David Majnemer | d905da4 | 2014-07-01 20:30:31 +0000 | [diff] [blame] | 1518 | VFTable = llvm::GlobalAlias::create( |
| 1519 | cast<llvm::SequentialType>(VTableGEP->getType())->getElementType(), |
| 1520 | /*AddressSpace=*/0, llvm::GlobalValue::ExternalLinkage, |
| 1521 | VFTableName.str(), VTableGEP, &CGM.getModule()); |
| 1522 | } else { |
David Majnemer | bb84871 | 2014-07-01 22:37:08 +0000 | [diff] [blame] | 1523 | // We don't need a GlobalAlias to be a symbol for the VTable if we won't |
| 1524 | // be referencing any RTTI data. The GlobalVariable will end up being |
| 1525 | // an appropriate definition of the VFTable. |
David Majnemer | d905da4 | 2014-07-01 20:30:31 +0000 | [diff] [blame] | 1526 | VTable->setName(VFTableName.str()); |
| 1527 | } |
| 1528 | |
| 1529 | VFTable->setUnnamedAddr(true); |
| 1530 | if (RD->hasAttr<DLLImportAttr>()) |
| 1531 | VFTable->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass); |
| 1532 | else if (RD->hasAttr<DLLExportAttr>()) |
| 1533 | VFTable->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass); |
| 1534 | |
| 1535 | llvm::GlobalValue::LinkageTypes VFTableLinkage = CGM.getVTableLinkage(RD); |
| 1536 | if (VFTable != VTable) { |
| 1537 | if (llvm::GlobalValue::isAvailableExternallyLinkage(VFTableLinkage)) { |
| 1538 | // AvailableExternally implies that we grabbed the data from another |
| 1539 | // executable. No need to stick the alias in a Comdat. |
David Majnemer | b2615aa | 2014-07-13 05:19:56 +0000 | [diff] [blame] | 1540 | } else if (llvm::GlobalValue::isInternalLinkage(VFTableLinkage) || |
| 1541 | llvm::GlobalValue::isWeakODRLinkage(VFTableLinkage) || |
David Majnemer | d905da4 | 2014-07-01 20:30:31 +0000 | [diff] [blame] | 1542 | llvm::GlobalValue::isLinkOnceODRLinkage(VFTableLinkage)) { |
| 1543 | // The alias is going to be dropped into a Comdat, no need to make it |
| 1544 | // weak. |
David Majnemer | b2615aa | 2014-07-13 05:19:56 +0000 | [diff] [blame] | 1545 | if (!llvm::GlobalValue::isInternalLinkage(VFTableLinkage)) |
| 1546 | VFTableLinkage = llvm::GlobalValue::ExternalLinkage; |
David Majnemer | d905da4 | 2014-07-01 20:30:31 +0000 | [diff] [blame] | 1547 | llvm::Comdat *C = |
| 1548 | CGM.getModule().getOrInsertComdat(VFTable->getName()); |
David Majnemer | bb84871 | 2014-07-01 22:37:08 +0000 | [diff] [blame] | 1549 | // We must indicate which VFTable is larger to support linking between |
| 1550 | // translation units which do and do not have RTTI data. The largest |
| 1551 | // VFTable contains the RTTI data; translation units which reference |
| 1552 | // the smaller VFTable always reference it relative to the first |
| 1553 | // virtual method. |
David Majnemer | d905da4 | 2014-07-01 20:30:31 +0000 | [diff] [blame] | 1554 | C->setSelectionKind(llvm::Comdat::Largest); |
| 1555 | VTable->setComdat(C); |
| 1556 | } else { |
| 1557 | llvm_unreachable("unexpected linkage for vftable!"); |
| 1558 | } |
Rafael Espindola | 4af2cdb | 2015-01-16 21:41:44 +0000 | [diff] [blame] | 1559 | } else { |
| 1560 | if (llvm::GlobalValue::isWeakForLinker(VFTableLinkage)) |
| 1561 | VTable->setComdat( |
| 1562 | CGM.getModule().getOrInsertComdat(VTable->getName())); |
David Majnemer | d905da4 | 2014-07-01 20:30:31 +0000 | [diff] [blame] | 1563 | } |
| 1564 | VFTable->setLinkage(VFTableLinkage); |
| 1565 | CGM.setGlobalVisibility(VFTable, RD); |
| 1566 | VFTablesMap[ID] = VFTable; |
| 1567 | } |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1568 | break; |
| 1569 | } |
| 1570 | |
| 1571 | return VTable; |
| 1572 | } |
| 1573 | |
Timur Iskhodzhanov | 88fd439 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 1574 | llvm::Value *MicrosoftCXXABI::getVirtualFunctionPointer(CodeGenFunction &CGF, |
| 1575 | GlobalDecl GD, |
| 1576 | llvm::Value *This, |
| 1577 | llvm::Type *Ty) { |
| 1578 | GD = GD.getCanonicalDecl(); |
| 1579 | CGBuilderTy &Builder = CGF.Builder; |
| 1580 | |
| 1581 | Ty = Ty->getPointerTo()->getPointerTo(); |
Timur Iskhodzhanov | f174942 | 2014-03-14 17:43:37 +0000 | [diff] [blame] | 1582 | llvm::Value *VPtr = |
| 1583 | adjustThisArgumentForVirtualFunctionCall(CGF, GD, This, true); |
Timur Iskhodzhanov | 88fd439 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 1584 | llvm::Value *VTable = CGF.GetVTablePtr(VPtr, Ty); |
| 1585 | |
Timur Iskhodzhanov | 5877663 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 1586 | MicrosoftVTableContext::MethodVFTableLocation ML = |
| 1587 | CGM.getMicrosoftVTableContext().getMethodVFTableLocation(GD); |
Timur Iskhodzhanov | 88fd439 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 1588 | llvm::Value *VFuncPtr = |
| 1589 | Builder.CreateConstInBoundsGEP1_64(VTable, ML.Index, "vfn"); |
| 1590 | return Builder.CreateLoad(VFuncPtr); |
| 1591 | } |
| 1592 | |
David Majnemer | 0c0b6d9 | 2014-10-31 20:09:12 +0000 | [diff] [blame] | 1593 | llvm::Value *MicrosoftCXXABI::EmitVirtualDestructorCall( |
| 1594 | CodeGenFunction &CGF, const CXXDestructorDecl *Dtor, CXXDtorType DtorType, |
| 1595 | llvm::Value *This, const CXXMemberCallExpr *CE) { |
Alexey Samsonov | a5bf76b | 2014-08-25 20:17:35 +0000 | [diff] [blame] | 1596 | assert(CE == nullptr || CE->arg_begin() == CE->arg_end()); |
Timur Iskhodzhanov | d619711 | 2013-02-15 14:45:22 +0000 | [diff] [blame] | 1597 | assert(DtorType == Dtor_Deleting || DtorType == Dtor_Complete); |
| 1598 | |
| 1599 | // We have only one destructor in the vftable but can get both behaviors |
Timur Iskhodzhanov | 701981f | 2013-08-27 10:38:19 +0000 | [diff] [blame] | 1600 | // by passing an implicit int parameter. |
Timur Iskhodzhanov | 62082b7 | 2013-10-16 18:24:06 +0000 | [diff] [blame] | 1601 | GlobalDecl GD(Dtor, Dtor_Deleting); |
Rafael Espindola | 8d2a19b | 2014-09-08 16:01:27 +0000 | [diff] [blame] | 1602 | const CGFunctionInfo *FInfo = &CGM.getTypes().arrangeCXXStructorDeclaration( |
| 1603 | Dtor, StructorType::Deleting); |
Timur Iskhodzhanov | d619711 | 2013-02-15 14:45:22 +0000 | [diff] [blame] | 1604 | llvm::Type *Ty = CGF.CGM.getTypes().GetFunctionType(*FInfo); |
Timur Iskhodzhanov | 62082b7 | 2013-10-16 18:24:06 +0000 | [diff] [blame] | 1605 | llvm::Value *Callee = getVirtualFunctionPointer(CGF, GD, This, Ty); |
Timur Iskhodzhanov | d619711 | 2013-02-15 14:45:22 +0000 | [diff] [blame] | 1606 | |
David Majnemer | 9ced3dd | 2015-03-14 23:44:48 +0000 | [diff] [blame] | 1607 | ASTContext &Context = getContext(); |
Nico Weber | 0a02992 | 2015-01-12 21:24:10 +0000 | [diff] [blame] | 1608 | llvm::Value *ImplicitParam = llvm::ConstantInt::get( |
| 1609 | llvm::IntegerType::getInt32Ty(CGF.getLLVMContext()), |
| 1610 | DtorType == Dtor_Deleting); |
Timur Iskhodzhanov | d619711 | 2013-02-15 14:45:22 +0000 | [diff] [blame] | 1611 | |
Timur Iskhodzhanov | f174942 | 2014-03-14 17:43:37 +0000 | [diff] [blame] | 1612 | This = adjustThisArgumentForVirtualFunctionCall(CGF, GD, This, true); |
David Majnemer | 0c0b6d9 | 2014-10-31 20:09:12 +0000 | [diff] [blame] | 1613 | RValue RV = CGF.EmitCXXStructorCall(Dtor, Callee, ReturnValueSlot(), This, |
| 1614 | ImplicitParam, Context.IntTy, CE, |
| 1615 | StructorType::Deleting); |
| 1616 | return RV.getScalarVal(); |
Timur Iskhodzhanov | d619711 | 2013-02-15 14:45:22 +0000 | [diff] [blame] | 1617 | } |
| 1618 | |
Reid Kleckner | b40a27d | 2014-01-03 00:14:35 +0000 | [diff] [blame] | 1619 | const VBTableGlobals & |
| 1620 | MicrosoftCXXABI::enumerateVBTables(const CXXRecordDecl *RD) { |
Reid Kleckner | 7810af0 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 1621 | // At this layer, we can key the cache off of a single class, which is much |
Reid Kleckner | b40a27d | 2014-01-03 00:14:35 +0000 | [diff] [blame] | 1622 | // easier than caching each vbtable individually. |
| 1623 | llvm::DenseMap<const CXXRecordDecl*, VBTableGlobals>::iterator Entry; |
| 1624 | bool Added; |
Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 1625 | std::tie(Entry, Added) = |
| 1626 | VBTablesMap.insert(std::make_pair(RD, VBTableGlobals())); |
Reid Kleckner | b40a27d | 2014-01-03 00:14:35 +0000 | [diff] [blame] | 1627 | VBTableGlobals &VBGlobals = Entry->second; |
| 1628 | if (!Added) |
| 1629 | return VBGlobals; |
Reid Kleckner | 7810af0 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 1630 | |
Reid Kleckner | b40a27d | 2014-01-03 00:14:35 +0000 | [diff] [blame] | 1631 | MicrosoftVTableContext &Context = CGM.getMicrosoftVTableContext(); |
| 1632 | VBGlobals.VBTables = &Context.enumerateVBTables(RD); |
Reid Kleckner | 7810af0 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 1633 | |
Reid Kleckner | b40a27d | 2014-01-03 00:14:35 +0000 | [diff] [blame] | 1634 | // Cache the globals for all vbtables so we don't have to recompute the |
| 1635 | // mangled names. |
| 1636 | llvm::GlobalVariable::LinkageTypes Linkage = CGM.getVTableLinkage(RD); |
Reid Kleckner | 9c6e9e3 | 2014-02-27 19:40:09 +0000 | [diff] [blame] | 1637 | for (VPtrInfoVector::const_iterator I = VBGlobals.VBTables->begin(), |
| 1638 | E = VBGlobals.VBTables->end(); |
Reid Kleckner | b40a27d | 2014-01-03 00:14:35 +0000 | [diff] [blame] | 1639 | I != E; ++I) { |
Reid Kleckner | 5f08094 | 2014-01-03 23:42:00 +0000 | [diff] [blame] | 1640 | VBGlobals.Globals.push_back(getAddrOfVBTable(**I, RD, Linkage)); |
Reid Kleckner | b40a27d | 2014-01-03 00:14:35 +0000 | [diff] [blame] | 1641 | } |
| 1642 | |
| 1643 | return VBGlobals; |
Reid Kleckner | 7810af0 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 1644 | } |
| 1645 | |
Reid Kleckner | e4a5220 | 2014-02-21 02:27:32 +0000 | [diff] [blame] | 1646 | llvm::Function *MicrosoftCXXABI::EmitVirtualMemPtrThunk( |
| 1647 | const CXXMethodDecl *MD, |
| 1648 | const MicrosoftVTableContext::MethodVFTableLocation &ML) { |
Reid Kleckner | c347351 | 2014-08-29 21:43:29 +0000 | [diff] [blame] | 1649 | assert(!isa<CXXConstructorDecl>(MD) && !isa<CXXDestructorDecl>(MD) && |
| 1650 | "can't form pointers to ctors or virtual dtors"); |
| 1651 | |
Reid Kleckner | e4a5220 | 2014-02-21 02:27:32 +0000 | [diff] [blame] | 1652 | // Calculate the mangled name. |
| 1653 | SmallString<256> ThunkName; |
| 1654 | llvm::raw_svector_ostream Out(ThunkName); |
| 1655 | getMangleContext().mangleVirtualMemPtrThunk(MD, Out); |
| 1656 | Out.flush(); |
| 1657 | |
Hans Wennborg | 88497d6 | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 1658 | // If the thunk has been generated previously, just return it. |
| 1659 | if (llvm::GlobalValue *GV = CGM.getModule().getNamedValue(ThunkName)) |
| 1660 | return cast<llvm::Function>(GV); |
| 1661 | |
| 1662 | // Create the llvm::Function. |
Reid Kleckner | c347351 | 2014-08-29 21:43:29 +0000 | [diff] [blame] | 1663 | const CGFunctionInfo &FnInfo = CGM.getTypes().arrangeMSMemberPointerThunk(MD); |
Hans Wennborg | 88497d6 | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 1664 | llvm::FunctionType *ThunkTy = CGM.getTypes().GetFunctionType(FnInfo); |
| 1665 | llvm::Function *ThunkFn = |
| 1666 | llvm::Function::Create(ThunkTy, llvm::Function::ExternalLinkage, |
| 1667 | ThunkName.str(), &CGM.getModule()); |
| 1668 | assert(ThunkFn->getName() == ThunkName && "name was uniqued!"); |
| 1669 | |
Hans Wennborg | 88497d6 | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 1670 | ThunkFn->setLinkage(MD->isExternallyVisible() |
| 1671 | ? llvm::GlobalValue::LinkOnceODRLinkage |
| 1672 | : llvm::GlobalValue::InternalLinkage); |
David Majnemer | 8c9cdb6 | 2015-01-21 01:21:31 +0000 | [diff] [blame] | 1673 | if (MD->isExternallyVisible()) |
| 1674 | ThunkFn->setComdat(CGM.getModule().getOrInsertComdat(ThunkFn->getName())); |
Hans Wennborg | 88497d6 | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 1675 | |
| 1676 | CGM.SetLLVMFunctionAttributes(MD, FnInfo, ThunkFn); |
| 1677 | CGM.SetLLVMFunctionAttributesForDefinition(MD, ThunkFn); |
| 1678 | |
Reid Kleckner | 9da9448 | 2015-01-21 22:18:17 +0000 | [diff] [blame] | 1679 | // Add the "thunk" attribute so that LLVM knows that the return type is |
| 1680 | // meaningless. These thunks can be used to call functions with differing |
| 1681 | // return types, and the caller is required to cast the prototype |
| 1682 | // appropriately to extract the correct value. |
| 1683 | ThunkFn->addFnAttr("thunk"); |
| 1684 | |
Reid Kleckner | b9538a6 | 2014-08-15 18:12:40 +0000 | [diff] [blame] | 1685 | // These thunks can be compared, so they are not unnamed. |
| 1686 | ThunkFn->setUnnamedAddr(false); |
| 1687 | |
Hans Wennborg | 88497d6 | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 1688 | // Start codegen. |
| 1689 | CodeGenFunction CGF(CGM); |
Reid Kleckner | c347351 | 2014-08-29 21:43:29 +0000 | [diff] [blame] | 1690 | CGF.CurGD = GlobalDecl(MD); |
| 1691 | CGF.CurFuncIsThunk = true; |
| 1692 | |
| 1693 | // Build FunctionArgs, but only include the implicit 'this' parameter |
| 1694 | // declaration. |
| 1695 | FunctionArgList FunctionArgs; |
| 1696 | buildThisParam(CGF, FunctionArgs); |
| 1697 | |
| 1698 | // Start defining the function. |
| 1699 | CGF.StartFunction(GlobalDecl(), FnInfo.getReturnType(), ThunkFn, FnInfo, |
| 1700 | FunctionArgs, MD->getLocation(), SourceLocation()); |
| 1701 | EmitThisParam(CGF); |
Hans Wennborg | 88497d6 | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 1702 | |
Reid Kleckner | e4a5220 | 2014-02-21 02:27:32 +0000 | [diff] [blame] | 1703 | // Load the vfptr and then callee from the vftable. The callee should have |
| 1704 | // adjusted 'this' so that the vfptr is at offset zero. |
Reid Kleckner | c347351 | 2014-08-29 21:43:29 +0000 | [diff] [blame] | 1705 | llvm::Value *VTable = CGF.GetVTablePtr( |
| 1706 | getThisValue(CGF), ThunkTy->getPointerTo()->getPointerTo()); |
Reid Kleckner | e4a5220 | 2014-02-21 02:27:32 +0000 | [diff] [blame] | 1707 | llvm::Value *VFuncPtr = |
| 1708 | CGF.Builder.CreateConstInBoundsGEP1_64(VTable, ML.Index, "vfn"); |
| 1709 | llvm::Value *Callee = CGF.Builder.CreateLoad(VFuncPtr); |
Hans Wennborg | 88497d6 | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 1710 | |
Reid Kleckner | c347351 | 2014-08-29 21:43:29 +0000 | [diff] [blame] | 1711 | CGF.EmitMustTailThunk(MD, getThisValue(CGF), Callee); |
Hans Wennborg | 88497d6 | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 1712 | |
| 1713 | return ThunkFn; |
| 1714 | } |
| 1715 | |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1716 | void MicrosoftCXXABI::emitVirtualInheritanceTables(const CXXRecordDecl *RD) { |
Reid Kleckner | b40a27d | 2014-01-03 00:14:35 +0000 | [diff] [blame] | 1717 | const VBTableGlobals &VBGlobals = enumerateVBTables(RD); |
| 1718 | for (unsigned I = 0, E = VBGlobals.VBTables->size(); I != E; ++I) { |
Reid Kleckner | 9c6e9e3 | 2014-02-27 19:40:09 +0000 | [diff] [blame] | 1719 | const VPtrInfo *VBT = (*VBGlobals.VBTables)[I]; |
Reid Kleckner | b40a27d | 2014-01-03 00:14:35 +0000 | [diff] [blame] | 1720 | llvm::GlobalVariable *GV = VBGlobals.Globals[I]; |
David Majnemer | 129f417 | 2015-02-02 10:22:20 +0000 | [diff] [blame] | 1721 | if (GV->isDeclaration()) |
| 1722 | emitVBTableDefinition(*VBT, RD, GV); |
Reid Kleckner | 7810af0 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 1723 | } |
| 1724 | } |
| 1725 | |
Reid Kleckner | b40a27d | 2014-01-03 00:14:35 +0000 | [diff] [blame] | 1726 | llvm::GlobalVariable * |
Reid Kleckner | 9c6e9e3 | 2014-02-27 19:40:09 +0000 | [diff] [blame] | 1727 | MicrosoftCXXABI::getAddrOfVBTable(const VPtrInfo &VBT, const CXXRecordDecl *RD, |
Reid Kleckner | b40a27d | 2014-01-03 00:14:35 +0000 | [diff] [blame] | 1728 | llvm::GlobalVariable::LinkageTypes Linkage) { |
| 1729 | SmallString<256> OutName; |
| 1730 | llvm::raw_svector_ostream Out(OutName); |
David Majnemer | 611cdb9 | 2014-07-07 08:09:15 +0000 | [diff] [blame] | 1731 | getMangleContext().mangleCXXVBTable(RD, VBT.MangledPath, Out); |
Reid Kleckner | b40a27d | 2014-01-03 00:14:35 +0000 | [diff] [blame] | 1732 | Out.flush(); |
| 1733 | StringRef Name = OutName.str(); |
| 1734 | |
| 1735 | llvm::ArrayType *VBTableType = |
| 1736 | llvm::ArrayType::get(CGM.IntTy, 1 + VBT.ReusingBase->getNumVBases()); |
| 1737 | |
| 1738 | assert(!CGM.getModule().getNamedGlobal(Name) && |
| 1739 | "vbtable with this name already exists: mangling bug?"); |
| 1740 | llvm::GlobalVariable *GV = |
| 1741 | CGM.CreateOrReplaceCXXRuntimeVariable(Name, VBTableType, Linkage); |
| 1742 | GV->setUnnamedAddr(true); |
Hans Wennborg | 853ae94 | 2014-05-30 16:59:42 +0000 | [diff] [blame] | 1743 | |
| 1744 | if (RD->hasAttr<DLLImportAttr>()) |
| 1745 | GV->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass); |
| 1746 | else if (RD->hasAttr<DLLExportAttr>()) |
| 1747 | GV->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass); |
| 1748 | |
David Majnemer | 129f417 | 2015-02-02 10:22:20 +0000 | [diff] [blame] | 1749 | if (!GV->hasExternalLinkage()) |
| 1750 | emitVBTableDefinition(VBT, RD, GV); |
| 1751 | |
Reid Kleckner | b40a27d | 2014-01-03 00:14:35 +0000 | [diff] [blame] | 1752 | return GV; |
| 1753 | } |
| 1754 | |
Reid Kleckner | 9c6e9e3 | 2014-02-27 19:40:09 +0000 | [diff] [blame] | 1755 | void MicrosoftCXXABI::emitVBTableDefinition(const VPtrInfo &VBT, |
Reid Kleckner | b40a27d | 2014-01-03 00:14:35 +0000 | [diff] [blame] | 1756 | const CXXRecordDecl *RD, |
| 1757 | llvm::GlobalVariable *GV) const { |
| 1758 | const CXXRecordDecl *ReusingBase = VBT.ReusingBase; |
| 1759 | |
| 1760 | assert(RD->getNumVBases() && ReusingBase->getNumVBases() && |
| 1761 | "should only emit vbtables for classes with vbtables"); |
| 1762 | |
| 1763 | const ASTRecordLayout &BaseLayout = |
David Majnemer | 9ced3dd | 2015-03-14 23:44:48 +0000 | [diff] [blame] | 1764 | getContext().getASTRecordLayout(VBT.BaseWithVPtr); |
| 1765 | const ASTRecordLayout &DerivedLayout = getContext().getASTRecordLayout(RD); |
Reid Kleckner | b40a27d | 2014-01-03 00:14:35 +0000 | [diff] [blame] | 1766 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1767 | SmallVector<llvm::Constant *, 4> Offsets(1 + ReusingBase->getNumVBases(), |
| 1768 | nullptr); |
Reid Kleckner | b40a27d | 2014-01-03 00:14:35 +0000 | [diff] [blame] | 1769 | |
| 1770 | // The offset from ReusingBase's vbptr to itself always leads. |
| 1771 | CharUnits VBPtrOffset = BaseLayout.getVBPtrOffset(); |
| 1772 | Offsets[0] = llvm::ConstantInt::get(CGM.IntTy, -VBPtrOffset.getQuantity()); |
| 1773 | |
| 1774 | MicrosoftVTableContext &Context = CGM.getMicrosoftVTableContext(); |
Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 1775 | for (const auto &I : ReusingBase->vbases()) { |
| 1776 | const CXXRecordDecl *VBase = I.getType()->getAsCXXRecordDecl(); |
Reid Kleckner | b40a27d | 2014-01-03 00:14:35 +0000 | [diff] [blame] | 1777 | CharUnits Offset = DerivedLayout.getVBaseClassOffset(VBase); |
| 1778 | assert(!Offset.isNegative()); |
Reid Kleckner | 5f08094 | 2014-01-03 23:42:00 +0000 | [diff] [blame] | 1779 | |
Reid Kleckner | b40a27d | 2014-01-03 00:14:35 +0000 | [diff] [blame] | 1780 | // Make it relative to the subobject vbptr. |
Reid Kleckner | 5f08094 | 2014-01-03 23:42:00 +0000 | [diff] [blame] | 1781 | CharUnits CompleteVBPtrOffset = VBT.NonVirtualOffset + VBPtrOffset; |
Reid Kleckner | 9c6e9e3 | 2014-02-27 19:40:09 +0000 | [diff] [blame] | 1782 | if (VBT.getVBaseWithVPtr()) |
Reid Kleckner | 5f08094 | 2014-01-03 23:42:00 +0000 | [diff] [blame] | 1783 | CompleteVBPtrOffset += |
Reid Kleckner | 9c6e9e3 | 2014-02-27 19:40:09 +0000 | [diff] [blame] | 1784 | DerivedLayout.getVBaseClassOffset(VBT.getVBaseWithVPtr()); |
Reid Kleckner | 5f08094 | 2014-01-03 23:42:00 +0000 | [diff] [blame] | 1785 | Offset -= CompleteVBPtrOffset; |
| 1786 | |
Reid Kleckner | b40a27d | 2014-01-03 00:14:35 +0000 | [diff] [blame] | 1787 | unsigned VBIndex = Context.getVBTableIndex(ReusingBase, VBase); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1788 | assert(Offsets[VBIndex] == nullptr && "The same vbindex seen twice?"); |
Reid Kleckner | b40a27d | 2014-01-03 00:14:35 +0000 | [diff] [blame] | 1789 | Offsets[VBIndex] = llvm::ConstantInt::get(CGM.IntTy, Offset.getQuantity()); |
| 1790 | } |
| 1791 | |
| 1792 | assert(Offsets.size() == |
| 1793 | cast<llvm::ArrayType>(cast<llvm::PointerType>(GV->getType()) |
| 1794 | ->getElementType())->getNumElements()); |
| 1795 | llvm::ArrayType *VBTableType = |
| 1796 | llvm::ArrayType::get(CGM.IntTy, Offsets.size()); |
| 1797 | llvm::Constant *Init = llvm::ConstantArray::get(VBTableType, Offsets); |
| 1798 | GV->setInitializer(Init); |
| 1799 | |
| 1800 | // Set the right visibility. |
John McCall | 8f80a61 | 2014-02-08 00:41:16 +0000 | [diff] [blame] | 1801 | CGM.setGlobalVisibility(GV, RD); |
Reid Kleckner | b40a27d | 2014-01-03 00:14:35 +0000 | [diff] [blame] | 1802 | } |
| 1803 | |
Timur Iskhodzhanov | 0201432 | 2013-10-30 11:55:43 +0000 | [diff] [blame] | 1804 | llvm::Value *MicrosoftCXXABI::performThisAdjustment(CodeGenFunction &CGF, |
| 1805 | llvm::Value *This, |
| 1806 | const ThisAdjustment &TA) { |
| 1807 | if (TA.isEmpty()) |
| 1808 | return This; |
| 1809 | |
| 1810 | llvm::Value *V = CGF.Builder.CreateBitCast(This, CGF.Int8PtrTy); |
| 1811 | |
Timur Iskhodzhanov | 053142a | 2013-11-06 06:24:31 +0000 | [diff] [blame] | 1812 | if (!TA.Virtual.isEmpty()) { |
| 1813 | assert(TA.Virtual.Microsoft.VtordispOffset < 0); |
| 1814 | // Adjust the this argument based on the vtordisp value. |
| 1815 | llvm::Value *VtorDispPtr = |
| 1816 | CGF.Builder.CreateConstGEP1_32(V, TA.Virtual.Microsoft.VtordispOffset); |
| 1817 | VtorDispPtr = |
| 1818 | CGF.Builder.CreateBitCast(VtorDispPtr, CGF.Int32Ty->getPointerTo()); |
| 1819 | llvm::Value *VtorDisp = CGF.Builder.CreateLoad(VtorDispPtr, "vtordisp"); |
| 1820 | V = CGF.Builder.CreateGEP(V, CGF.Builder.CreateNeg(VtorDisp)); |
| 1821 | |
| 1822 | if (TA.Virtual.Microsoft.VBPtrOffset) { |
| 1823 | // If the final overrider is defined in a virtual base other than the one |
| 1824 | // that holds the vfptr, we have to use a vtordispex thunk which looks up |
| 1825 | // the vbtable of the derived class. |
| 1826 | assert(TA.Virtual.Microsoft.VBPtrOffset > 0); |
| 1827 | assert(TA.Virtual.Microsoft.VBOffsetOffset >= 0); |
| 1828 | llvm::Value *VBPtr; |
| 1829 | llvm::Value *VBaseOffset = |
| 1830 | GetVBaseOffsetFromVBPtr(CGF, V, -TA.Virtual.Microsoft.VBPtrOffset, |
| 1831 | TA.Virtual.Microsoft.VBOffsetOffset, &VBPtr); |
| 1832 | V = CGF.Builder.CreateInBoundsGEP(VBPtr, VBaseOffset); |
| 1833 | } |
| 1834 | } |
Timur Iskhodzhanov | 0201432 | 2013-10-30 11:55:43 +0000 | [diff] [blame] | 1835 | |
| 1836 | if (TA.NonVirtual) { |
| 1837 | // Non-virtual adjustment might result in a pointer outside the allocated |
| 1838 | // object, e.g. if the final overrider class is laid out after the virtual |
| 1839 | // base that declares a method in the most derived class. |
| 1840 | V = CGF.Builder.CreateConstGEP1_32(V, TA.NonVirtual); |
| 1841 | } |
| 1842 | |
| 1843 | // Don't need to bitcast back, the call CodeGen will handle this. |
| 1844 | return V; |
| 1845 | } |
| 1846 | |
| 1847 | llvm::Value * |
| 1848 | MicrosoftCXXABI::performReturnAdjustment(CodeGenFunction &CGF, llvm::Value *Ret, |
| 1849 | const ReturnAdjustment &RA) { |
| 1850 | if (RA.isEmpty()) |
| 1851 | return Ret; |
| 1852 | |
| 1853 | llvm::Value *V = CGF.Builder.CreateBitCast(Ret, CGF.Int8PtrTy); |
| 1854 | |
| 1855 | if (RA.Virtual.Microsoft.VBIndex) { |
| 1856 | assert(RA.Virtual.Microsoft.VBIndex > 0); |
David Majnemer | 9ced3dd | 2015-03-14 23:44:48 +0000 | [diff] [blame] | 1857 | const ASTContext &Context = getContext(); |
| 1858 | int32_t IntSize = Context.getTypeSizeInChars(Context.IntTy).getQuantity(); |
Timur Iskhodzhanov | 0201432 | 2013-10-30 11:55:43 +0000 | [diff] [blame] | 1859 | llvm::Value *VBPtr; |
| 1860 | llvm::Value *VBaseOffset = |
| 1861 | GetVBaseOffsetFromVBPtr(CGF, V, RA.Virtual.Microsoft.VBPtrOffset, |
| 1862 | IntSize * RA.Virtual.Microsoft.VBIndex, &VBPtr); |
| 1863 | V = CGF.Builder.CreateInBoundsGEP(VBPtr, VBaseOffset); |
| 1864 | } |
| 1865 | |
| 1866 | if (RA.NonVirtual) |
| 1867 | V = CGF.Builder.CreateConstInBoundsGEP1_32(V, RA.NonVirtual); |
| 1868 | |
| 1869 | // Cast back to the original type. |
| 1870 | return CGF.Builder.CreateBitCast(V, Ret->getType()); |
| 1871 | } |
| 1872 | |
John McCall | b91cd66 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 1873 | bool MicrosoftCXXABI::requiresArrayCookie(const CXXDeleteExpr *expr, |
| 1874 | QualType elementType) { |
| 1875 | // Microsoft seems to completely ignore the possibility of a |
| 1876 | // two-argument usual deallocation function. |
| 1877 | return elementType.isDestructedType(); |
| 1878 | } |
| 1879 | |
| 1880 | bool MicrosoftCXXABI::requiresArrayCookie(const CXXNewExpr *expr) { |
| 1881 | // Microsoft seems to completely ignore the possibility of a |
| 1882 | // two-argument usual deallocation function. |
| 1883 | return expr->getAllocatedType().isDestructedType(); |
| 1884 | } |
| 1885 | |
| 1886 | CharUnits MicrosoftCXXABI::getArrayCookieSizeImpl(QualType type) { |
| 1887 | // The array cookie is always a size_t; we then pad that out to the |
| 1888 | // alignment of the element type. |
| 1889 | ASTContext &Ctx = getContext(); |
| 1890 | return std::max(Ctx.getTypeSizeInChars(Ctx.getSizeType()), |
| 1891 | Ctx.getTypeAlignInChars(type)); |
| 1892 | } |
| 1893 | |
| 1894 | llvm::Value *MicrosoftCXXABI::readArrayCookieImpl(CodeGenFunction &CGF, |
| 1895 | llvm::Value *allocPtr, |
| 1896 | CharUnits cookieSize) { |
Micah Villmow | ea2fea2 | 2012-10-25 15:39:14 +0000 | [diff] [blame] | 1897 | unsigned AS = allocPtr->getType()->getPointerAddressSpace(); |
John McCall | b91cd66 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 1898 | llvm::Value *numElementsPtr = |
| 1899 | CGF.Builder.CreateBitCast(allocPtr, CGF.SizeTy->getPointerTo(AS)); |
| 1900 | return CGF.Builder.CreateLoad(numElementsPtr); |
| 1901 | } |
| 1902 | |
| 1903 | llvm::Value* MicrosoftCXXABI::InitializeArrayCookie(CodeGenFunction &CGF, |
| 1904 | llvm::Value *newPtr, |
| 1905 | llvm::Value *numElements, |
| 1906 | const CXXNewExpr *expr, |
| 1907 | QualType elementType) { |
| 1908 | assert(requiresArrayCookie(expr)); |
| 1909 | |
| 1910 | // The size of the cookie. |
| 1911 | CharUnits cookieSize = getArrayCookieSizeImpl(elementType); |
| 1912 | |
| 1913 | // Compute an offset to the cookie. |
| 1914 | llvm::Value *cookiePtr = newPtr; |
| 1915 | |
| 1916 | // Write the number of elements into the appropriate slot. |
Micah Villmow | ea2fea2 | 2012-10-25 15:39:14 +0000 | [diff] [blame] | 1917 | unsigned AS = newPtr->getType()->getPointerAddressSpace(); |
John McCall | b91cd66 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 1918 | llvm::Value *numElementsPtr |
| 1919 | = CGF.Builder.CreateBitCast(cookiePtr, CGF.SizeTy->getPointerTo(AS)); |
| 1920 | CGF.Builder.CreateStore(numElements, numElementsPtr); |
| 1921 | |
| 1922 | // Finally, compute a pointer to the actual data buffer by skipping |
| 1923 | // over the cookie completely. |
| 1924 | return CGF.Builder.CreateConstInBoundsGEP1_64(newPtr, |
| 1925 | cookieSize.getQuantity()); |
| 1926 | } |
| 1927 | |
David Majnemer | b3341ea | 2014-10-05 05:05:40 +0000 | [diff] [blame] | 1928 | static void emitGlobalDtorWithTLRegDtor(CodeGenFunction &CGF, const VarDecl &VD, |
| 1929 | llvm::Constant *Dtor, |
| 1930 | llvm::Constant *Addr) { |
| 1931 | // Create a function which calls the destructor. |
| 1932 | llvm::Constant *DtorStub = CGF.createAtExitStub(VD, Dtor, Addr); |
| 1933 | |
| 1934 | // extern "C" int __tlregdtor(void (*f)(void)); |
| 1935 | llvm::FunctionType *TLRegDtorTy = llvm::FunctionType::get( |
| 1936 | CGF.IntTy, DtorStub->getType(), /*IsVarArg=*/false); |
| 1937 | |
| 1938 | llvm::Constant *TLRegDtor = |
| 1939 | CGF.CGM.CreateRuntimeFunction(TLRegDtorTy, "__tlregdtor"); |
| 1940 | if (llvm::Function *TLRegDtorFn = dyn_cast<llvm::Function>(TLRegDtor)) |
| 1941 | TLRegDtorFn->setDoesNotThrow(); |
| 1942 | |
| 1943 | CGF.EmitNounwindRuntimeCall(TLRegDtor, DtorStub); |
| 1944 | } |
| 1945 | |
| 1946 | void MicrosoftCXXABI::registerGlobalDtor(CodeGenFunction &CGF, const VarDecl &D, |
| 1947 | llvm::Constant *Dtor, |
| 1948 | llvm::Constant *Addr) { |
| 1949 | if (D.getTLSKind()) |
| 1950 | return emitGlobalDtorWithTLRegDtor(CGF, D, Dtor, Addr); |
| 1951 | |
| 1952 | // The default behavior is to use atexit. |
| 1953 | CGF.registerGlobalDtorWithAtExit(D, Dtor, Addr); |
| 1954 | } |
| 1955 | |
| 1956 | void MicrosoftCXXABI::EmitThreadLocalInitFuncs( |
| 1957 | CodeGenModule &CGM, |
| 1958 | ArrayRef<std::pair<const VarDecl *, llvm::GlobalVariable *>> |
| 1959 | CXXThreadLocals, |
| 1960 | ArrayRef<llvm::Function *> CXXThreadLocalInits, |
| 1961 | ArrayRef<llvm::GlobalVariable *> CXXThreadLocalInitVars) { |
| 1962 | // This will create a GV in the .CRT$XDU section. It will point to our |
| 1963 | // initialization function. The CRT will call all of these function |
| 1964 | // pointers at start-up time and, eventually, at thread-creation time. |
| 1965 | auto AddToXDU = [&CGM](llvm::Function *InitFunc) { |
| 1966 | llvm::GlobalVariable *InitFuncPtr = new llvm::GlobalVariable( |
| 1967 | CGM.getModule(), InitFunc->getType(), /*IsConstant=*/true, |
| 1968 | llvm::GlobalVariable::InternalLinkage, InitFunc, |
| 1969 | Twine(InitFunc->getName(), "$initializer$")); |
| 1970 | InitFuncPtr->setSection(".CRT$XDU"); |
| 1971 | // This variable has discardable linkage, we have to add it to @llvm.used to |
| 1972 | // ensure it won't get discarded. |
| 1973 | CGM.addUsedGlobal(InitFuncPtr); |
| 1974 | return InitFuncPtr; |
| 1975 | }; |
| 1976 | |
| 1977 | std::vector<llvm::Function *> NonComdatInits; |
| 1978 | for (size_t I = 0, E = CXXThreadLocalInitVars.size(); I != E; ++I) { |
| 1979 | llvm::GlobalVariable *GV = CXXThreadLocalInitVars[I]; |
| 1980 | llvm::Function *F = CXXThreadLocalInits[I]; |
| 1981 | |
| 1982 | // If the GV is already in a comdat group, then we have to join it. |
Rafael Espindola | 0d4fb98 | 2015-01-12 22:13:53 +0000 | [diff] [blame] | 1983 | if (llvm::Comdat *C = GV->getComdat()) |
David Majnemer | b3341ea | 2014-10-05 05:05:40 +0000 | [diff] [blame] | 1984 | AddToXDU(F)->setComdat(C); |
Rafael Espindola | 0d4fb98 | 2015-01-12 22:13:53 +0000 | [diff] [blame] | 1985 | else |
David Majnemer | b3341ea | 2014-10-05 05:05:40 +0000 | [diff] [blame] | 1986 | NonComdatInits.push_back(F); |
David Majnemer | b3341ea | 2014-10-05 05:05:40 +0000 | [diff] [blame] | 1987 | } |
| 1988 | |
| 1989 | if (!NonComdatInits.empty()) { |
| 1990 | llvm::FunctionType *FTy = |
| 1991 | llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false); |
Alexey Samsonov | 1444bb9 | 2014-10-17 00:20:19 +0000 | [diff] [blame] | 1992 | llvm::Function *InitFunc = CGM.CreateGlobalInitOrDestructFunction( |
| 1993 | FTy, "__tls_init", SourceLocation(), |
| 1994 | /*TLS=*/true); |
David Majnemer | b3341ea | 2014-10-05 05:05:40 +0000 | [diff] [blame] | 1995 | CodeGenFunction(CGM).GenerateCXXGlobalInitFunc(InitFunc, NonComdatInits); |
| 1996 | |
| 1997 | AddToXDU(InitFunc); |
| 1998 | } |
| 1999 | } |
| 2000 | |
| 2001 | LValue MicrosoftCXXABI::EmitThreadLocalVarDeclLValue(CodeGenFunction &CGF, |
| 2002 | const VarDecl *VD, |
| 2003 | QualType LValType) { |
| 2004 | CGF.CGM.ErrorUnsupported(VD, "thread wrappers"); |
| 2005 | return LValue(); |
| 2006 | } |
| 2007 | |
John McCall | c84ed6a | 2012-05-01 06:13:13 +0000 | [diff] [blame] | 2008 | void MicrosoftCXXABI::EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D, |
Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 2009 | llvm::GlobalVariable *GV, |
John McCall | c84ed6a | 2012-05-01 06:13:13 +0000 | [diff] [blame] | 2010 | bool PerformInit) { |
Reid Kleckner | 563f0e8 | 2014-05-23 21:13:45 +0000 | [diff] [blame] | 2011 | // MSVC only uses guards for static locals. |
| 2012 | if (!D.isStaticLocal()) { |
| 2013 | assert(GV->hasWeakLinkage() || GV->hasLinkOnceLinkage()); |
| 2014 | // GlobalOpt is allowed to discard the initializer, so use linkonce_odr. |
Rafael Espindola | 77abc3a | 2015-01-16 16:04:45 +0000 | [diff] [blame] | 2015 | llvm::Function *F = CGF.CurFn; |
| 2016 | F->setLinkage(llvm::GlobalValue::LinkOnceODRLinkage); |
| 2017 | F->setComdat(CGM.getModule().getOrInsertComdat(F->getName())); |
Reid Kleckner | 563f0e8 | 2014-05-23 21:13:45 +0000 | [diff] [blame] | 2018 | CGF.EmitCXXGlobalVarDeclInit(D, GV, PerformInit); |
| 2019 | return; |
| 2020 | } |
| 2021 | |
Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 2022 | // MSVC always uses an i32 bitfield to guard initialization, which is *not* |
| 2023 | // threadsafe. Since the user may be linking in inline functions compiled by |
| 2024 | // cl.exe, there's no reason to provide a false sense of security by using |
| 2025 | // critical sections here. |
John McCall | c84ed6a | 2012-05-01 06:13:13 +0000 | [diff] [blame] | 2026 | |
Richard Smith | dbf74ba | 2013-04-14 23:01:42 +0000 | [diff] [blame] | 2027 | if (D.getTLSKind()) |
| 2028 | CGM.ErrorUnsupported(&D, "dynamic TLS initialization"); |
| 2029 | |
Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 2030 | CGBuilderTy &Builder = CGF.Builder; |
| 2031 | llvm::IntegerType *GuardTy = CGF.Int32Ty; |
| 2032 | llvm::ConstantInt *Zero = llvm::ConstantInt::get(GuardTy, 0); |
| 2033 | |
| 2034 | // Get the guard variable for this function if we have one already. |
Reid Kleckner | 563f0e8 | 2014-05-23 21:13:45 +0000 | [diff] [blame] | 2035 | GuardInfo *GI = &GuardVariableMap[D.getDeclContext()]; |
Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 2036 | |
| 2037 | unsigned BitIndex; |
Reid Kleckner | e9591b3 | 2014-04-23 18:22:11 +0000 | [diff] [blame] | 2038 | if (D.isStaticLocal() && D.isExternallyVisible()) { |
Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 2039 | // Externally visible variables have to be numbered in Sema to properly |
| 2040 | // handle unreachable VarDecls. |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 2041 | BitIndex = getContext().getStaticLocalNumber(&D); |
Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 2042 | assert(BitIndex > 0); |
| 2043 | BitIndex--; |
| 2044 | } else { |
| 2045 | // Non-externally visible variables are numbered here in CodeGen. |
Reid Kleckner | e9591b3 | 2014-04-23 18:22:11 +0000 | [diff] [blame] | 2046 | BitIndex = GI->BitIndex++; |
Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 2047 | } |
| 2048 | |
| 2049 | if (BitIndex >= 32) { |
| 2050 | if (D.isExternallyVisible()) |
| 2051 | ErrorUnsupportedABI(CGF, "more than 32 guarded initializations"); |
| 2052 | BitIndex %= 32; |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2053 | GI->Guard = nullptr; |
Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 2054 | } |
| 2055 | |
| 2056 | // Lazily create the i32 bitfield for this function. |
Reid Kleckner | e9591b3 | 2014-04-23 18:22:11 +0000 | [diff] [blame] | 2057 | if (!GI->Guard) { |
Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 2058 | // Mangle the name for the guard. |
| 2059 | SmallString<256> GuardName; |
| 2060 | { |
| 2061 | llvm::raw_svector_ostream Out(GuardName); |
| 2062 | getMangleContext().mangleStaticGuardVariable(&D, Out); |
| 2063 | Out.flush(); |
| 2064 | } |
| 2065 | |
Hans Wennborg | ef2272c | 2014-06-18 15:55:13 +0000 | [diff] [blame] | 2066 | // Create the guard variable with a zero-initializer. Just absorb linkage, |
| 2067 | // visibility and dll storage class from the guarded variable. |
Reid Kleckner | e9591b3 | 2014-04-23 18:22:11 +0000 | [diff] [blame] | 2068 | GI->Guard = |
| 2069 | new llvm::GlobalVariable(CGM.getModule(), GuardTy, false, |
| 2070 | GV->getLinkage(), Zero, GuardName.str()); |
| 2071 | GI->Guard->setVisibility(GV->getVisibility()); |
Hans Wennborg | ef2272c | 2014-06-18 15:55:13 +0000 | [diff] [blame] | 2072 | GI->Guard->setDLLStorageClass(GV->getDLLStorageClass()); |
David Majnemer | 3072fc8 | 2015-01-21 01:04:30 +0000 | [diff] [blame] | 2073 | if (GI->Guard->isWeakForLinker()) |
| 2074 | GI->Guard->setComdat( |
| 2075 | CGM.getModule().getOrInsertComdat(GI->Guard->getName())); |
Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 2076 | } else { |
Reid Kleckner | e9591b3 | 2014-04-23 18:22:11 +0000 | [diff] [blame] | 2077 | assert(GI->Guard->getLinkage() == GV->getLinkage() && |
Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 2078 | "static local from the same function had different linkage"); |
| 2079 | } |
| 2080 | |
| 2081 | // Pseudo code for the test: |
| 2082 | // if (!(GuardVar & MyGuardBit)) { |
| 2083 | // GuardVar |= MyGuardBit; |
| 2084 | // ... initialize the object ...; |
| 2085 | // } |
| 2086 | |
| 2087 | // Test our bit from the guard variable. |
| 2088 | llvm::ConstantInt *Bit = llvm::ConstantInt::get(GuardTy, 1U << BitIndex); |
Reid Kleckner | e9591b3 | 2014-04-23 18:22:11 +0000 | [diff] [blame] | 2089 | llvm::LoadInst *LI = Builder.CreateLoad(GI->Guard); |
Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 2090 | llvm::Value *IsInitialized = |
| 2091 | Builder.CreateICmpNE(Builder.CreateAnd(LI, Bit), Zero); |
| 2092 | llvm::BasicBlock *InitBlock = CGF.createBasicBlock("init"); |
| 2093 | llvm::BasicBlock *EndBlock = CGF.createBasicBlock("init.end"); |
| 2094 | Builder.CreateCondBr(IsInitialized, EndBlock, InitBlock); |
| 2095 | |
| 2096 | // Set our bit in the guard variable and emit the initializer and add a global |
| 2097 | // destructor if appropriate. |
| 2098 | CGF.EmitBlock(InitBlock); |
Reid Kleckner | e9591b3 | 2014-04-23 18:22:11 +0000 | [diff] [blame] | 2099 | Builder.CreateStore(Builder.CreateOr(LI, Bit), GI->Guard); |
Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 2100 | CGF.EmitCXXGlobalVarDeclInit(D, GV, PerformInit); |
| 2101 | Builder.CreateBr(EndBlock); |
| 2102 | |
| 2103 | // Continue. |
| 2104 | CGF.EmitBlock(EndBlock); |
John McCall | c84ed6a | 2012-05-01 06:13:13 +0000 | [diff] [blame] | 2105 | } |
| 2106 | |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2107 | bool MicrosoftCXXABI::isZeroInitializable(const MemberPointerType *MPT) { |
| 2108 | // Null-ness for function memptrs only depends on the first field, which is |
| 2109 | // the function pointer. The rest don't matter, so we can zero initialize. |
| 2110 | if (MPT->isMemberFunctionPointer()) |
| 2111 | return true; |
| 2112 | |
| 2113 | // The virtual base adjustment field is always -1 for null, so if we have one |
| 2114 | // we can't zero initialize. The field offset is sometimes also -1 if 0 is a |
| 2115 | // valid field offset. |
David Majnemer | 1cdd96d | 2014-01-17 09:01:00 +0000 | [diff] [blame] | 2116 | const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl(); |
| 2117 | MSInheritanceAttr::Spelling Inheritance = RD->getMSInheritanceModel(); |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 2118 | return (!MSInheritanceAttr::hasVBTableOffsetField(Inheritance) && |
| 2119 | RD->nullFieldOffsetIsZero()); |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2120 | } |
| 2121 | |
| 2122 | llvm::Type * |
| 2123 | MicrosoftCXXABI::ConvertMemberPointerType(const MemberPointerType *MPT) { |
David Majnemer | 1cdd96d | 2014-01-17 09:01:00 +0000 | [diff] [blame] | 2124 | const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl(); |
| 2125 | MSInheritanceAttr::Spelling Inheritance = RD->getMSInheritanceModel(); |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2126 | llvm::SmallVector<llvm::Type *, 4> fields; |
| 2127 | if (MPT->isMemberFunctionPointer()) |
| 2128 | fields.push_back(CGM.VoidPtrTy); // FunctionPointerOrVirtualThunk |
| 2129 | else |
| 2130 | fields.push_back(CGM.IntTy); // FieldOffset |
| 2131 | |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 2132 | if (MSInheritanceAttr::hasNVOffsetField(MPT->isMemberFunctionPointer(), |
| 2133 | Inheritance)) |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2134 | fields.push_back(CGM.IntTy); |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 2135 | if (MSInheritanceAttr::hasVBPtrOffsetField(Inheritance)) |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2136 | fields.push_back(CGM.IntTy); |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 2137 | if (MSInheritanceAttr::hasVBTableOffsetField(Inheritance)) |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2138 | fields.push_back(CGM.IntTy); // VirtualBaseAdjustmentOffset |
| 2139 | |
| 2140 | if (fields.size() == 1) |
| 2141 | return fields[0]; |
| 2142 | return llvm::StructType::get(CGM.getLLVMContext(), fields); |
| 2143 | } |
| 2144 | |
| 2145 | void MicrosoftCXXABI:: |
| 2146 | GetNullMemberPointerFields(const MemberPointerType *MPT, |
| 2147 | llvm::SmallVectorImpl<llvm::Constant *> &fields) { |
| 2148 | assert(fields.empty()); |
David Majnemer | 1cdd96d | 2014-01-17 09:01:00 +0000 | [diff] [blame] | 2149 | const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl(); |
| 2150 | MSInheritanceAttr::Spelling Inheritance = RD->getMSInheritanceModel(); |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2151 | if (MPT->isMemberFunctionPointer()) { |
| 2152 | // FunctionPointerOrVirtualThunk |
| 2153 | fields.push_back(llvm::Constant::getNullValue(CGM.VoidPtrTy)); |
| 2154 | } else { |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 2155 | if (RD->nullFieldOffsetIsZero()) |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2156 | fields.push_back(getZeroInt()); // FieldOffset |
| 2157 | else |
| 2158 | fields.push_back(getAllOnesInt()); // FieldOffset |
Reid Kleckner | 407e8b6 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 2159 | } |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2160 | |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 2161 | if (MSInheritanceAttr::hasNVOffsetField(MPT->isMemberFunctionPointer(), |
| 2162 | Inheritance)) |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2163 | fields.push_back(getZeroInt()); |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 2164 | if (MSInheritanceAttr::hasVBPtrOffsetField(Inheritance)) |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2165 | fields.push_back(getZeroInt()); |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 2166 | if (MSInheritanceAttr::hasVBTableOffsetField(Inheritance)) |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2167 | fields.push_back(getAllOnesInt()); |
Reid Kleckner | 407e8b6 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 2168 | } |
| 2169 | |
| 2170 | llvm::Constant * |
| 2171 | MicrosoftCXXABI::EmitNullMemberPointer(const MemberPointerType *MPT) { |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2172 | llvm::SmallVector<llvm::Constant *, 4> fields; |
| 2173 | GetNullMemberPointerFields(MPT, fields); |
| 2174 | if (fields.size() == 1) |
| 2175 | return fields[0]; |
| 2176 | llvm::Constant *Res = llvm::ConstantStruct::getAnon(fields); |
| 2177 | assert(Res->getType() == ConvertMemberPointerType(MPT)); |
| 2178 | return Res; |
Reid Kleckner | 407e8b6 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 2179 | } |
| 2180 | |
| 2181 | llvm::Constant * |
Reid Kleckner | 7d0efb5 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 2182 | MicrosoftCXXABI::EmitFullMemberPointer(llvm::Constant *FirstField, |
| 2183 | bool IsMemberFunction, |
Reid Kleckner | 452abac | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 2184 | const CXXRecordDecl *RD, |
| 2185 | CharUnits NonVirtualBaseAdjustment) |
Reid Kleckner | 7d0efb5 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 2186 | { |
David Majnemer | 1cdd96d | 2014-01-17 09:01:00 +0000 | [diff] [blame] | 2187 | MSInheritanceAttr::Spelling Inheritance = RD->getMSInheritanceModel(); |
Reid Kleckner | 7d0efb5 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 2188 | |
| 2189 | // Single inheritance class member pointer are represented as scalars instead |
| 2190 | // of aggregates. |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 2191 | if (MSInheritanceAttr::hasOnlyOneField(IsMemberFunction, Inheritance)) |
Reid Kleckner | 7d0efb5 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 2192 | return FirstField; |
| 2193 | |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2194 | llvm::SmallVector<llvm::Constant *, 4> fields; |
Reid Kleckner | 7d0efb5 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 2195 | fields.push_back(FirstField); |
| 2196 | |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 2197 | if (MSInheritanceAttr::hasNVOffsetField(IsMemberFunction, Inheritance)) |
Reid Kleckner | 452abac | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 2198 | fields.push_back(llvm::ConstantInt::get( |
| 2199 | CGM.IntTy, NonVirtualBaseAdjustment.getQuantity())); |
Reid Kleckner | 7d0efb5 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 2200 | |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 2201 | if (MSInheritanceAttr::hasVBPtrOffsetField(Inheritance)) { |
Reid Kleckner | aec4409 | 2013-10-15 01:18:02 +0000 | [diff] [blame] | 2202 | CharUnits Offs = CharUnits::Zero(); |
| 2203 | if (RD->getNumVBases()) |
Reid Kleckner | 5b1b5d5 | 2014-01-14 00:50:39 +0000 | [diff] [blame] | 2204 | Offs = getContext().getASTRecordLayout(RD).getVBPtrOffset(); |
Reid Kleckner | aec4409 | 2013-10-15 01:18:02 +0000 | [diff] [blame] | 2205 | fields.push_back(llvm::ConstantInt::get(CGM.IntTy, Offs.getQuantity())); |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2206 | } |
Reid Kleckner | 7d0efb5 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 2207 | |
| 2208 | // The rest of the fields are adjusted by conversions to a more derived class. |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 2209 | if (MSInheritanceAttr::hasVBTableOffsetField(Inheritance)) |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2210 | fields.push_back(getZeroInt()); |
Reid Kleckner | 7d0efb5 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 2211 | |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2212 | return llvm::ConstantStruct::getAnon(fields); |
Reid Kleckner | 407e8b6 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 2213 | } |
| 2214 | |
Reid Kleckner | 7d0efb5 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 2215 | llvm::Constant * |
| 2216 | MicrosoftCXXABI::EmitMemberDataPointer(const MemberPointerType *MPT, |
| 2217 | CharUnits offset) { |
David Majnemer | 1cdd96d | 2014-01-17 09:01:00 +0000 | [diff] [blame] | 2218 | const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl(); |
Reid Kleckner | 7d0efb5 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 2219 | llvm::Constant *FirstField = |
| 2220 | llvm::ConstantInt::get(CGM.IntTy, offset.getQuantity()); |
Reid Kleckner | 452abac | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 2221 | return EmitFullMemberPointer(FirstField, /*IsMemberFunction=*/false, RD, |
| 2222 | CharUnits::Zero()); |
| 2223 | } |
| 2224 | |
| 2225 | llvm::Constant *MicrosoftCXXABI::EmitMemberPointer(const CXXMethodDecl *MD) { |
| 2226 | return BuildMemberPointer(MD->getParent(), MD, CharUnits::Zero()); |
| 2227 | } |
| 2228 | |
| 2229 | llvm::Constant *MicrosoftCXXABI::EmitMemberPointer(const APValue &MP, |
| 2230 | QualType MPType) { |
| 2231 | const MemberPointerType *MPT = MPType->castAs<MemberPointerType>(); |
| 2232 | const ValueDecl *MPD = MP.getMemberPointerDecl(); |
| 2233 | if (!MPD) |
| 2234 | return EmitNullMemberPointer(MPT); |
| 2235 | |
| 2236 | CharUnits ThisAdjustment = getMemberPointerPathAdjustment(MP); |
| 2237 | |
| 2238 | // FIXME PR15713: Support virtual inheritance paths. |
| 2239 | |
| 2240 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MPD)) |
David Majnemer | 1cdd96d | 2014-01-17 09:01:00 +0000 | [diff] [blame] | 2241 | return BuildMemberPointer(MPT->getMostRecentCXXRecordDecl(), MD, |
| 2242 | ThisAdjustment); |
Reid Kleckner | 452abac | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 2243 | |
| 2244 | CharUnits FieldOffset = |
| 2245 | getContext().toCharUnitsFromBits(getContext().getFieldOffset(MPD)); |
| 2246 | return EmitMemberDataPointer(MPT, ThisAdjustment + FieldOffset); |
Reid Kleckner | 7d0efb5 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 2247 | } |
| 2248 | |
| 2249 | llvm::Constant * |
Reid Kleckner | 452abac | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 2250 | MicrosoftCXXABI::BuildMemberPointer(const CXXRecordDecl *RD, |
| 2251 | const CXXMethodDecl *MD, |
| 2252 | CharUnits NonVirtualBaseAdjustment) { |
Reid Kleckner | 7d0efb5 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 2253 | assert(MD->isInstance() && "Member function must not be static!"); |
| 2254 | MD = MD->getCanonicalDecl(); |
David Majnemer | 1cdd96d | 2014-01-17 09:01:00 +0000 | [diff] [blame] | 2255 | RD = RD->getMostRecentDecl(); |
Reid Kleckner | 7d0efb5 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 2256 | CodeGenTypes &Types = CGM.getTypes(); |
| 2257 | |
| 2258 | llvm::Constant *FirstField; |
Reid Kleckner | c347351 | 2014-08-29 21:43:29 +0000 | [diff] [blame] | 2259 | const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>(); |
Hans Wennborg | 88497d6 | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 2260 | if (!MD->isVirtual()) { |
Reid Kleckner | 7d0efb5 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 2261 | llvm::Type *Ty; |
| 2262 | // Check whether the function has a computable LLVM signature. |
| 2263 | if (Types.isFuncTypeConvertible(FPT)) { |
| 2264 | // The function has a computable LLVM signature; use the correct type. |
| 2265 | Ty = Types.GetFunctionType(Types.arrangeCXXMethodDeclaration(MD)); |
| 2266 | } else { |
| 2267 | // Use an arbitrary non-function type to tell GetAddrOfFunction that the |
| 2268 | // function type is incomplete. |
| 2269 | Ty = CGM.PtrDiffTy; |
| 2270 | } |
| 2271 | FirstField = CGM.GetAddrOfFunction(MD, Ty); |
| 2272 | FirstField = llvm::ConstantExpr::getBitCast(FirstField, CGM.VoidPtrTy); |
Hans Wennborg | 88497d6 | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 2273 | } else { |
| 2274 | MicrosoftVTableContext::MethodVFTableLocation ML = |
| 2275 | CGM.getMicrosoftVTableContext().getMethodVFTableLocation(MD); |
Reid Kleckner | c347351 | 2014-08-29 21:43:29 +0000 | [diff] [blame] | 2276 | if (!CGM.getTypes().isFuncTypeConvertible( |
| 2277 | MD->getType()->castAs<FunctionType>())) { |
Hans Wennborg | 88497d6 | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 2278 | CGM.ErrorUnsupported(MD, "pointer to virtual member function with " |
| 2279 | "incomplete return or parameter type"); |
| 2280 | FirstField = llvm::Constant::getNullValue(CGM.VoidPtrTy); |
Reid Kleckner | c347351 | 2014-08-29 21:43:29 +0000 | [diff] [blame] | 2281 | } else if (FPT->getCallConv() == CC_X86FastCall) { |
| 2282 | CGM.ErrorUnsupported(MD, "pointer to fastcall virtual member function"); |
| 2283 | FirstField = llvm::Constant::getNullValue(CGM.VoidPtrTy); |
Hans Wennborg | 88497d6 | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 2284 | } else if (ML.VBase) { |
| 2285 | CGM.ErrorUnsupported(MD, "pointer to virtual member function overriding " |
| 2286 | "member function in virtual base class"); |
| 2287 | FirstField = llvm::Constant::getNullValue(CGM.VoidPtrTy); |
| 2288 | } else { |
Reid Kleckner | e4a5220 | 2014-02-21 02:27:32 +0000 | [diff] [blame] | 2289 | llvm::Function *Thunk = EmitVirtualMemPtrThunk(MD, ML); |
Hans Wennborg | 88497d6 | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 2290 | FirstField = llvm::ConstantExpr::getBitCast(Thunk, CGM.VoidPtrTy); |
Reid Kleckner | e4a5220 | 2014-02-21 02:27:32 +0000 | [diff] [blame] | 2291 | // Include the vfptr adjustment if the method is in a non-primary vftable. |
| 2292 | NonVirtualBaseAdjustment += ML.VFPtrOffset; |
Hans Wennborg | 88497d6 | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 2293 | } |
Reid Kleckner | 7d0efb5 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 2294 | } |
| 2295 | |
| 2296 | // The rest of the fields are common with data member pointers. |
Reid Kleckner | 452abac | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 2297 | return EmitFullMemberPointer(FirstField, /*IsMemberFunction=*/true, RD, |
| 2298 | NonVirtualBaseAdjustment); |
Reid Kleckner | 7d0efb5 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 2299 | } |
| 2300 | |
Reid Kleckner | 700c3ee | 2013-04-30 20:15:14 +0000 | [diff] [blame] | 2301 | /// Member pointers are the same if they're either bitwise identical *or* both |
| 2302 | /// null. Null-ness for function members is determined by the first field, |
| 2303 | /// while for data member pointers we must compare all fields. |
| 2304 | llvm::Value * |
| 2305 | MicrosoftCXXABI::EmitMemberPointerComparison(CodeGenFunction &CGF, |
| 2306 | llvm::Value *L, |
| 2307 | llvm::Value *R, |
| 2308 | const MemberPointerType *MPT, |
| 2309 | bool Inequality) { |
| 2310 | CGBuilderTy &Builder = CGF.Builder; |
| 2311 | |
| 2312 | // Handle != comparisons by switching the sense of all boolean operations. |
| 2313 | llvm::ICmpInst::Predicate Eq; |
| 2314 | llvm::Instruction::BinaryOps And, Or; |
| 2315 | if (Inequality) { |
| 2316 | Eq = llvm::ICmpInst::ICMP_NE; |
| 2317 | And = llvm::Instruction::Or; |
| 2318 | Or = llvm::Instruction::And; |
| 2319 | } else { |
| 2320 | Eq = llvm::ICmpInst::ICMP_EQ; |
| 2321 | And = llvm::Instruction::And; |
| 2322 | Or = llvm::Instruction::Or; |
| 2323 | } |
| 2324 | |
| 2325 | // If this is a single field member pointer (single inheritance), this is a |
| 2326 | // single icmp. |
David Majnemer | 1cdd96d | 2014-01-17 09:01:00 +0000 | [diff] [blame] | 2327 | const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl(); |
| 2328 | MSInheritanceAttr::Spelling Inheritance = RD->getMSInheritanceModel(); |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 2329 | if (MSInheritanceAttr::hasOnlyOneField(MPT->isMemberFunctionPointer(), |
| 2330 | Inheritance)) |
Reid Kleckner | 700c3ee | 2013-04-30 20:15:14 +0000 | [diff] [blame] | 2331 | return Builder.CreateICmp(Eq, L, R); |
| 2332 | |
| 2333 | // Compare the first field. |
| 2334 | llvm::Value *L0 = Builder.CreateExtractValue(L, 0, "lhs.0"); |
| 2335 | llvm::Value *R0 = Builder.CreateExtractValue(R, 0, "rhs.0"); |
| 2336 | llvm::Value *Cmp0 = Builder.CreateICmp(Eq, L0, R0, "memptr.cmp.first"); |
| 2337 | |
| 2338 | // Compare everything other than the first field. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2339 | llvm::Value *Res = nullptr; |
Reid Kleckner | 700c3ee | 2013-04-30 20:15:14 +0000 | [diff] [blame] | 2340 | llvm::StructType *LType = cast<llvm::StructType>(L->getType()); |
| 2341 | for (unsigned I = 1, E = LType->getNumElements(); I != E; ++I) { |
| 2342 | llvm::Value *LF = Builder.CreateExtractValue(L, I); |
| 2343 | llvm::Value *RF = Builder.CreateExtractValue(R, I); |
| 2344 | llvm::Value *Cmp = Builder.CreateICmp(Eq, LF, RF, "memptr.cmp.rest"); |
| 2345 | if (Res) |
| 2346 | Res = Builder.CreateBinOp(And, Res, Cmp); |
| 2347 | else |
| 2348 | Res = Cmp; |
| 2349 | } |
| 2350 | |
| 2351 | // Check if the first field is 0 if this is a function pointer. |
| 2352 | if (MPT->isMemberFunctionPointer()) { |
| 2353 | // (l1 == r1 && ...) || l0 == 0 |
| 2354 | llvm::Value *Zero = llvm::Constant::getNullValue(L0->getType()); |
| 2355 | llvm::Value *IsZero = Builder.CreateICmp(Eq, L0, Zero, "memptr.cmp.iszero"); |
| 2356 | Res = Builder.CreateBinOp(Or, Res, IsZero); |
| 2357 | } |
| 2358 | |
| 2359 | // Combine the comparison of the first field, which must always be true for |
| 2360 | // this comparison to succeeed. |
| 2361 | return Builder.CreateBinOp(And, Res, Cmp0, "memptr.cmp"); |
| 2362 | } |
| 2363 | |
Reid Kleckner | 407e8b6 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 2364 | llvm::Value * |
| 2365 | MicrosoftCXXABI::EmitMemberPointerIsNotNull(CodeGenFunction &CGF, |
| 2366 | llvm::Value *MemPtr, |
| 2367 | const MemberPointerType *MPT) { |
| 2368 | CGBuilderTy &Builder = CGF.Builder; |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2369 | llvm::SmallVector<llvm::Constant *, 4> fields; |
| 2370 | // We only need one field for member functions. |
| 2371 | if (MPT->isMemberFunctionPointer()) |
| 2372 | fields.push_back(llvm::Constant::getNullValue(CGM.VoidPtrTy)); |
| 2373 | else |
| 2374 | GetNullMemberPointerFields(MPT, fields); |
| 2375 | assert(!fields.empty()); |
| 2376 | llvm::Value *FirstField = MemPtr; |
| 2377 | if (MemPtr->getType()->isStructTy()) |
| 2378 | FirstField = Builder.CreateExtractValue(MemPtr, 0); |
| 2379 | llvm::Value *Res = Builder.CreateICmpNE(FirstField, fields[0], "memptr.cmp0"); |
Reid Kleckner | 407e8b6 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 2380 | |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2381 | // For function member pointers, we only need to test the function pointer |
| 2382 | // field. The other fields if any can be garbage. |
| 2383 | if (MPT->isMemberFunctionPointer()) |
| 2384 | return Res; |
| 2385 | |
| 2386 | // Otherwise, emit a series of compares and combine the results. |
| 2387 | for (int I = 1, E = fields.size(); I < E; ++I) { |
| 2388 | llvm::Value *Field = Builder.CreateExtractValue(MemPtr, I); |
| 2389 | llvm::Value *Next = Builder.CreateICmpNE(Field, fields[I], "memptr.cmp"); |
Reid Kleckner | 34a38d8 | 2014-05-02 00:05:16 +0000 | [diff] [blame] | 2390 | Res = Builder.CreateOr(Res, Next, "memptr.tobool"); |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2391 | } |
| 2392 | return Res; |
| 2393 | } |
| 2394 | |
Reid Kleckner | 452abac | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 2395 | bool MicrosoftCXXABI::MemberPointerConstantIsNull(const MemberPointerType *MPT, |
| 2396 | llvm::Constant *Val) { |
| 2397 | // Function pointers are null if the pointer in the first field is null. |
| 2398 | if (MPT->isMemberFunctionPointer()) { |
| 2399 | llvm::Constant *FirstField = Val->getType()->isStructTy() ? |
| 2400 | Val->getAggregateElement(0U) : Val; |
| 2401 | return FirstField->isNullValue(); |
| 2402 | } |
| 2403 | |
| 2404 | // If it's not a function pointer and it's zero initializable, we can easily |
| 2405 | // check zero. |
| 2406 | if (isZeroInitializable(MPT) && Val->isNullValue()) |
| 2407 | return true; |
| 2408 | |
| 2409 | // Otherwise, break down all the fields for comparison. Hopefully these |
| 2410 | // little Constants are reused, while a big null struct might not be. |
| 2411 | llvm::SmallVector<llvm::Constant *, 4> Fields; |
| 2412 | GetNullMemberPointerFields(MPT, Fields); |
| 2413 | if (Fields.size() == 1) { |
| 2414 | assert(Val->getType()->isIntegerTy()); |
| 2415 | return Val == Fields[0]; |
| 2416 | } |
| 2417 | |
| 2418 | unsigned I, E; |
| 2419 | for (I = 0, E = Fields.size(); I != E; ++I) { |
| 2420 | if (Val->getAggregateElement(I) != Fields[I]) |
| 2421 | break; |
| 2422 | } |
| 2423 | return I == E; |
| 2424 | } |
| 2425 | |
Reid Kleckner | d8cbeec | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 2426 | llvm::Value * |
| 2427 | MicrosoftCXXABI::GetVBaseOffsetFromVBPtr(CodeGenFunction &CGF, |
| 2428 | llvm::Value *This, |
Reid Kleckner | d8cbeec | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 2429 | llvm::Value *VBPtrOffset, |
Timur Iskhodzhanov | 07e6eff | 2013-10-27 17:10:27 +0000 | [diff] [blame] | 2430 | llvm::Value *VBTableOffset, |
Reid Kleckner | d8cbeec | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 2431 | llvm::Value **VBPtrOut) { |
| 2432 | CGBuilderTy &Builder = CGF.Builder; |
| 2433 | // Load the vbtable pointer from the vbptr in the instance. |
| 2434 | This = Builder.CreateBitCast(This, CGM.Int8PtrTy); |
| 2435 | llvm::Value *VBPtr = |
| 2436 | Builder.CreateInBoundsGEP(This, VBPtrOffset, "vbptr"); |
| 2437 | if (VBPtrOut) *VBPtrOut = VBPtr; |
Reid Kleckner | 0ba8ba4 | 2014-10-22 17:26:00 +0000 | [diff] [blame] | 2438 | VBPtr = Builder.CreateBitCast(VBPtr, |
| 2439 | CGM.Int32Ty->getPointerTo(0)->getPointerTo(0)); |
Reid Kleckner | d8cbeec | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 2440 | llvm::Value *VBTable = Builder.CreateLoad(VBPtr, "vbtable"); |
| 2441 | |
Reid Kleckner | 0ba8ba4 | 2014-10-22 17:26:00 +0000 | [diff] [blame] | 2442 | // Translate from byte offset to table index. It improves analyzability. |
| 2443 | llvm::Value *VBTableIndex = Builder.CreateAShr( |
| 2444 | VBTableOffset, llvm::ConstantInt::get(VBTableOffset->getType(), 2), |
| 2445 | "vbtindex", /*isExact=*/true); |
| 2446 | |
Reid Kleckner | d8cbeec | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 2447 | // Load an i32 offset from the vb-table. |
Reid Kleckner | 0ba8ba4 | 2014-10-22 17:26:00 +0000 | [diff] [blame] | 2448 | llvm::Value *VBaseOffs = Builder.CreateInBoundsGEP(VBTable, VBTableIndex); |
Reid Kleckner | d8cbeec | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 2449 | VBaseOffs = Builder.CreateBitCast(VBaseOffs, CGM.Int32Ty->getPointerTo(0)); |
| 2450 | return Builder.CreateLoad(VBaseOffs, "vbase_offs"); |
| 2451 | } |
| 2452 | |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2453 | // Returns an adjusted base cast to i8*, since we do more address arithmetic on |
| 2454 | // it. |
David Majnemer | 2b0d66d | 2014-02-20 23:22:07 +0000 | [diff] [blame] | 2455 | llvm::Value *MicrosoftCXXABI::AdjustVirtualBase( |
| 2456 | CodeGenFunction &CGF, const Expr *E, const CXXRecordDecl *RD, |
| 2457 | llvm::Value *Base, llvm::Value *VBTableOffset, llvm::Value *VBPtrOffset) { |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2458 | CGBuilderTy &Builder = CGF.Builder; |
| 2459 | Base = Builder.CreateBitCast(Base, CGM.Int8PtrTy); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2460 | llvm::BasicBlock *OriginalBB = nullptr; |
| 2461 | llvm::BasicBlock *SkipAdjustBB = nullptr; |
| 2462 | llvm::BasicBlock *VBaseAdjustBB = nullptr; |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2463 | |
| 2464 | // In the unspecified inheritance model, there might not be a vbtable at all, |
| 2465 | // in which case we need to skip the virtual base lookup. If there is a |
| 2466 | // vbtable, the first entry is a no-op entry that gives back the original |
| 2467 | // base, so look for a virtual base adjustment offset of zero. |
| 2468 | if (VBPtrOffset) { |
| 2469 | OriginalBB = Builder.GetInsertBlock(); |
| 2470 | VBaseAdjustBB = CGF.createBasicBlock("memptr.vadjust"); |
| 2471 | SkipAdjustBB = CGF.createBasicBlock("memptr.skip_vadjust"); |
| 2472 | llvm::Value *IsVirtual = |
Reid Kleckner | d8cbeec | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 2473 | Builder.CreateICmpNE(VBTableOffset, getZeroInt(), |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2474 | "memptr.is_vbase"); |
| 2475 | Builder.CreateCondBr(IsVirtual, VBaseAdjustBB, SkipAdjustBB); |
| 2476 | CGF.EmitBlock(VBaseAdjustBB); |
Reid Kleckner | 407e8b6 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 2477 | } |
| 2478 | |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2479 | // If we weren't given a dynamic vbptr offset, RD should be complete and we'll |
| 2480 | // know the vbptr offset. |
| 2481 | if (!VBPtrOffset) { |
Reid Kleckner | d8cbeec | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 2482 | CharUnits offs = CharUnits::Zero(); |
David Majnemer | 2b0d66d | 2014-02-20 23:22:07 +0000 | [diff] [blame] | 2483 | if (!RD->hasDefinition()) { |
| 2484 | DiagnosticsEngine &Diags = CGF.CGM.getDiags(); |
| 2485 | unsigned DiagID = Diags.getCustomDiagID( |
| 2486 | DiagnosticsEngine::Error, |
| 2487 | "member pointer representation requires a " |
| 2488 | "complete class type for %0 to perform this expression"); |
| 2489 | Diags.Report(E->getExprLoc(), DiagID) << RD << E->getSourceRange(); |
| 2490 | } else if (RD->getNumVBases()) |
Reid Kleckner | 5b1b5d5 | 2014-01-14 00:50:39 +0000 | [diff] [blame] | 2491 | offs = getContext().getASTRecordLayout(RD).getVBPtrOffset(); |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2492 | VBPtrOffset = llvm::ConstantInt::get(CGM.IntTy, offs.getQuantity()); |
| 2493 | } |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2494 | llvm::Value *VBPtr = nullptr; |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2495 | llvm::Value *VBaseOffs = |
Timur Iskhodzhanov | 07e6eff | 2013-10-27 17:10:27 +0000 | [diff] [blame] | 2496 | GetVBaseOffsetFromVBPtr(CGF, Base, VBPtrOffset, VBTableOffset, &VBPtr); |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2497 | llvm::Value *AdjustedBase = Builder.CreateInBoundsGEP(VBPtr, VBaseOffs); |
| 2498 | |
| 2499 | // Merge control flow with the case where we didn't have to adjust. |
| 2500 | if (VBaseAdjustBB) { |
| 2501 | Builder.CreateBr(SkipAdjustBB); |
| 2502 | CGF.EmitBlock(SkipAdjustBB); |
| 2503 | llvm::PHINode *Phi = Builder.CreatePHI(CGM.Int8PtrTy, 2, "memptr.base"); |
| 2504 | Phi->addIncoming(Base, OriginalBB); |
| 2505 | Phi->addIncoming(AdjustedBase, VBaseAdjustBB); |
| 2506 | return Phi; |
| 2507 | } |
| 2508 | return AdjustedBase; |
Reid Kleckner | 407e8b6 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 2509 | } |
| 2510 | |
David Majnemer | 2b0d66d | 2014-02-20 23:22:07 +0000 | [diff] [blame] | 2511 | llvm::Value *MicrosoftCXXABI::EmitMemberDataPointerAddress( |
| 2512 | CodeGenFunction &CGF, const Expr *E, llvm::Value *Base, llvm::Value *MemPtr, |
| 2513 | const MemberPointerType *MPT) { |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2514 | assert(MPT->isMemberDataPointer()); |
Reid Kleckner | 407e8b6 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 2515 | unsigned AS = Base->getType()->getPointerAddressSpace(); |
| 2516 | llvm::Type *PType = |
| 2517 | CGF.ConvertTypeForMem(MPT->getPointeeType())->getPointerTo(AS); |
| 2518 | CGBuilderTy &Builder = CGF.Builder; |
David Majnemer | 1cdd96d | 2014-01-17 09:01:00 +0000 | [diff] [blame] | 2519 | const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl(); |
| 2520 | MSInheritanceAttr::Spelling Inheritance = RD->getMSInheritanceModel(); |
Reid Kleckner | 407e8b6 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 2521 | |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2522 | // Extract the fields we need, regardless of model. We'll apply them if we |
| 2523 | // have them. |
| 2524 | llvm::Value *FieldOffset = MemPtr; |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2525 | llvm::Value *VirtualBaseAdjustmentOffset = nullptr; |
| 2526 | llvm::Value *VBPtrOffset = nullptr; |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2527 | if (MemPtr->getType()->isStructTy()) { |
| 2528 | // We need to extract values. |
| 2529 | unsigned I = 0; |
| 2530 | FieldOffset = Builder.CreateExtractValue(MemPtr, I++); |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 2531 | if (MSInheritanceAttr::hasVBPtrOffsetField(Inheritance)) |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2532 | VBPtrOffset = Builder.CreateExtractValue(MemPtr, I++); |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 2533 | if (MSInheritanceAttr::hasVBTableOffsetField(Inheritance)) |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2534 | VirtualBaseAdjustmentOffset = Builder.CreateExtractValue(MemPtr, I++); |
Reid Kleckner | 407e8b6 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 2535 | } |
| 2536 | |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2537 | if (VirtualBaseAdjustmentOffset) { |
David Majnemer | 2b0d66d | 2014-02-20 23:22:07 +0000 | [diff] [blame] | 2538 | Base = AdjustVirtualBase(CGF, E, RD, Base, VirtualBaseAdjustmentOffset, |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2539 | VBPtrOffset); |
Reid Kleckner | 407e8b6 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 2540 | } |
Reid Kleckner | ae94512 | 2013-12-05 22:44:07 +0000 | [diff] [blame] | 2541 | |
| 2542 | // Cast to char*. |
| 2543 | Base = Builder.CreateBitCast(Base, Builder.getInt8Ty()->getPointerTo(AS)); |
| 2544 | |
| 2545 | // Apply the offset, which we assume is non-null. |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2546 | llvm::Value *Addr = |
| 2547 | Builder.CreateInBoundsGEP(Base, FieldOffset, "memptr.offset"); |
Reid Kleckner | 407e8b6 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 2548 | |
| 2549 | // Cast the address to the appropriate pointer type, adopting the address |
| 2550 | // space of the base pointer. |
| 2551 | return Builder.CreateBitCast(Addr, PType); |
| 2552 | } |
| 2553 | |
David Majnemer | 1cdd96d | 2014-01-17 09:01:00 +0000 | [diff] [blame] | 2554 | static MSInheritanceAttr::Spelling |
Reid Kleckner | 452abac | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 2555 | getInheritanceFromMemptr(const MemberPointerType *MPT) { |
David Majnemer | 1cdd96d | 2014-01-17 09:01:00 +0000 | [diff] [blame] | 2556 | return MPT->getMostRecentCXXRecordDecl()->getMSInheritanceModel(); |
Reid Kleckner | 452abac | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 2557 | } |
| 2558 | |
| 2559 | llvm::Value * |
| 2560 | MicrosoftCXXABI::EmitMemberPointerConversion(CodeGenFunction &CGF, |
| 2561 | const CastExpr *E, |
| 2562 | llvm::Value *Src) { |
| 2563 | assert(E->getCastKind() == CK_DerivedToBaseMemberPointer || |
| 2564 | E->getCastKind() == CK_BaseToDerivedMemberPointer || |
| 2565 | E->getCastKind() == CK_ReinterpretMemberPointer); |
| 2566 | |
| 2567 | // Use constant emission if we can. |
| 2568 | if (isa<llvm::Constant>(Src)) |
| 2569 | return EmitMemberPointerConversion(E, cast<llvm::Constant>(Src)); |
| 2570 | |
| 2571 | // We may be adding or dropping fields from the member pointer, so we need |
| 2572 | // both types and the inheritance models of both records. |
| 2573 | const MemberPointerType *SrcTy = |
| 2574 | E->getSubExpr()->getType()->castAs<MemberPointerType>(); |
| 2575 | const MemberPointerType *DstTy = E->getType()->castAs<MemberPointerType>(); |
Reid Kleckner | 452abac | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 2576 | bool IsFunc = SrcTy->isMemberFunctionPointer(); |
| 2577 | |
| 2578 | // If the classes use the same null representation, reinterpret_cast is a nop. |
| 2579 | bool IsReinterpret = E->getCastKind() == CK_ReinterpretMemberPointer; |
David Majnemer | 1cdd96d | 2014-01-17 09:01:00 +0000 | [diff] [blame] | 2580 | if (IsReinterpret && IsFunc) |
| 2581 | return Src; |
| 2582 | |
| 2583 | CXXRecordDecl *SrcRD = SrcTy->getMostRecentCXXRecordDecl(); |
| 2584 | CXXRecordDecl *DstRD = DstTy->getMostRecentCXXRecordDecl(); |
| 2585 | if (IsReinterpret && |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 2586 | SrcRD->nullFieldOffsetIsZero() == DstRD->nullFieldOffsetIsZero()) |
Reid Kleckner | 452abac | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 2587 | return Src; |
| 2588 | |
| 2589 | CGBuilderTy &Builder = CGF.Builder; |
| 2590 | |
| 2591 | // Branch past the conversion if Src is null. |
| 2592 | llvm::Value *IsNotNull = EmitMemberPointerIsNotNull(CGF, Src, SrcTy); |
| 2593 | llvm::Constant *DstNull = EmitNullMemberPointer(DstTy); |
| 2594 | |
| 2595 | // C++ 5.2.10p9: The null member pointer value is converted to the null member |
| 2596 | // pointer value of the destination type. |
| 2597 | if (IsReinterpret) { |
| 2598 | // For reinterpret casts, sema ensures that src and dst are both functions |
| 2599 | // or data and have the same size, which means the LLVM types should match. |
| 2600 | assert(Src->getType() == DstNull->getType()); |
| 2601 | return Builder.CreateSelect(IsNotNull, Src, DstNull); |
| 2602 | } |
| 2603 | |
| 2604 | llvm::BasicBlock *OriginalBB = Builder.GetInsertBlock(); |
| 2605 | llvm::BasicBlock *ConvertBB = CGF.createBasicBlock("memptr.convert"); |
| 2606 | llvm::BasicBlock *ContinueBB = CGF.createBasicBlock("memptr.converted"); |
| 2607 | Builder.CreateCondBr(IsNotNull, ConvertBB, ContinueBB); |
| 2608 | CGF.EmitBlock(ConvertBB); |
| 2609 | |
| 2610 | // Decompose src. |
| 2611 | llvm::Value *FirstField = Src; |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2612 | llvm::Value *NonVirtualBaseAdjustment = nullptr; |
| 2613 | llvm::Value *VirtualBaseAdjustmentOffset = nullptr; |
| 2614 | llvm::Value *VBPtrOffset = nullptr; |
David Majnemer | 1cdd96d | 2014-01-17 09:01:00 +0000 | [diff] [blame] | 2615 | MSInheritanceAttr::Spelling SrcInheritance = SrcRD->getMSInheritanceModel(); |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 2616 | if (!MSInheritanceAttr::hasOnlyOneField(IsFunc, SrcInheritance)) { |
Reid Kleckner | 452abac | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 2617 | // We need to extract values. |
| 2618 | unsigned I = 0; |
| 2619 | FirstField = Builder.CreateExtractValue(Src, I++); |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 2620 | if (MSInheritanceAttr::hasNVOffsetField(IsFunc, SrcInheritance)) |
Reid Kleckner | 452abac | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 2621 | NonVirtualBaseAdjustment = Builder.CreateExtractValue(Src, I++); |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 2622 | if (MSInheritanceAttr::hasVBPtrOffsetField(SrcInheritance)) |
Reid Kleckner | 452abac | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 2623 | VBPtrOffset = Builder.CreateExtractValue(Src, I++); |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 2624 | if (MSInheritanceAttr::hasVBTableOffsetField(SrcInheritance)) |
Reid Kleckner | 452abac | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 2625 | VirtualBaseAdjustmentOffset = Builder.CreateExtractValue(Src, I++); |
| 2626 | } |
| 2627 | |
| 2628 | // For data pointers, we adjust the field offset directly. For functions, we |
| 2629 | // have a separate field. |
| 2630 | llvm::Constant *Adj = getMemberPointerAdjustment(E); |
| 2631 | if (Adj) { |
| 2632 | Adj = llvm::ConstantExpr::getTruncOrBitCast(Adj, CGM.IntTy); |
| 2633 | llvm::Value *&NVAdjustField = IsFunc ? NonVirtualBaseAdjustment : FirstField; |
| 2634 | bool isDerivedToBase = (E->getCastKind() == CK_DerivedToBaseMemberPointer); |
| 2635 | if (!NVAdjustField) // If this field didn't exist in src, it's zero. |
| 2636 | NVAdjustField = getZeroInt(); |
| 2637 | if (isDerivedToBase) |
| 2638 | NVAdjustField = Builder.CreateNSWSub(NVAdjustField, Adj, "adj"); |
| 2639 | else |
| 2640 | NVAdjustField = Builder.CreateNSWAdd(NVAdjustField, Adj, "adj"); |
| 2641 | } |
| 2642 | |
| 2643 | // FIXME PR15713: Support conversions through virtually derived classes. |
| 2644 | |
| 2645 | // Recompose dst from the null struct and the adjusted fields from src. |
David Majnemer | 1cdd96d | 2014-01-17 09:01:00 +0000 | [diff] [blame] | 2646 | MSInheritanceAttr::Spelling DstInheritance = DstRD->getMSInheritanceModel(); |
Reid Kleckner | 452abac | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 2647 | llvm::Value *Dst; |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 2648 | if (MSInheritanceAttr::hasOnlyOneField(IsFunc, DstInheritance)) { |
Reid Kleckner | 452abac | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 2649 | Dst = FirstField; |
| 2650 | } else { |
| 2651 | Dst = llvm::UndefValue::get(DstNull->getType()); |
| 2652 | unsigned Idx = 0; |
| 2653 | Dst = Builder.CreateInsertValue(Dst, FirstField, Idx++); |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 2654 | if (MSInheritanceAttr::hasNVOffsetField(IsFunc, DstInheritance)) |
Reid Kleckner | 452abac | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 2655 | Dst = Builder.CreateInsertValue( |
| 2656 | Dst, getValueOrZeroInt(NonVirtualBaseAdjustment), Idx++); |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 2657 | if (MSInheritanceAttr::hasVBPtrOffsetField(DstInheritance)) |
Reid Kleckner | 452abac | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 2658 | Dst = Builder.CreateInsertValue( |
| 2659 | Dst, getValueOrZeroInt(VBPtrOffset), Idx++); |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 2660 | if (MSInheritanceAttr::hasVBTableOffsetField(DstInheritance)) |
Reid Kleckner | 452abac | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 2661 | Dst = Builder.CreateInsertValue( |
| 2662 | Dst, getValueOrZeroInt(VirtualBaseAdjustmentOffset), Idx++); |
| 2663 | } |
| 2664 | Builder.CreateBr(ContinueBB); |
| 2665 | |
| 2666 | // In the continuation, choose between DstNull and Dst. |
| 2667 | CGF.EmitBlock(ContinueBB); |
| 2668 | llvm::PHINode *Phi = Builder.CreatePHI(DstNull->getType(), 2, "memptr.converted"); |
| 2669 | Phi->addIncoming(DstNull, OriginalBB); |
| 2670 | Phi->addIncoming(Dst, ConvertBB); |
| 2671 | return Phi; |
| 2672 | } |
| 2673 | |
| 2674 | llvm::Constant * |
| 2675 | MicrosoftCXXABI::EmitMemberPointerConversion(const CastExpr *E, |
| 2676 | llvm::Constant *Src) { |
| 2677 | const MemberPointerType *SrcTy = |
| 2678 | E->getSubExpr()->getType()->castAs<MemberPointerType>(); |
| 2679 | const MemberPointerType *DstTy = E->getType()->castAs<MemberPointerType>(); |
| 2680 | |
| 2681 | // If src is null, emit a new null for dst. We can't return src because dst |
| 2682 | // might have a new representation. |
| 2683 | if (MemberPointerConstantIsNull(SrcTy, Src)) |
| 2684 | return EmitNullMemberPointer(DstTy); |
| 2685 | |
| 2686 | // We don't need to do anything for reinterpret_casts of non-null member |
| 2687 | // pointers. We should only get here when the two type representations have |
| 2688 | // the same size. |
| 2689 | if (E->getCastKind() == CK_ReinterpretMemberPointer) |
| 2690 | return Src; |
| 2691 | |
David Majnemer | 1cdd96d | 2014-01-17 09:01:00 +0000 | [diff] [blame] | 2692 | MSInheritanceAttr::Spelling SrcInheritance = getInheritanceFromMemptr(SrcTy); |
| 2693 | MSInheritanceAttr::Spelling DstInheritance = getInheritanceFromMemptr(DstTy); |
Reid Kleckner | 452abac | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 2694 | |
| 2695 | // Decompose src. |
| 2696 | llvm::Constant *FirstField = Src; |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2697 | llvm::Constant *NonVirtualBaseAdjustment = nullptr; |
| 2698 | llvm::Constant *VirtualBaseAdjustmentOffset = nullptr; |
| 2699 | llvm::Constant *VBPtrOffset = nullptr; |
Reid Kleckner | 452abac | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 2700 | bool IsFunc = SrcTy->isMemberFunctionPointer(); |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 2701 | if (!MSInheritanceAttr::hasOnlyOneField(IsFunc, SrcInheritance)) { |
Reid Kleckner | 452abac | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 2702 | // We need to extract values. |
| 2703 | unsigned I = 0; |
| 2704 | FirstField = Src->getAggregateElement(I++); |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 2705 | if (MSInheritanceAttr::hasNVOffsetField(IsFunc, SrcInheritance)) |
Reid Kleckner | 452abac | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 2706 | NonVirtualBaseAdjustment = Src->getAggregateElement(I++); |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 2707 | if (MSInheritanceAttr::hasVBPtrOffsetField(SrcInheritance)) |
Reid Kleckner | 452abac | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 2708 | VBPtrOffset = Src->getAggregateElement(I++); |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 2709 | if (MSInheritanceAttr::hasVBTableOffsetField(SrcInheritance)) |
Reid Kleckner | 452abac | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 2710 | VirtualBaseAdjustmentOffset = Src->getAggregateElement(I++); |
| 2711 | } |
| 2712 | |
| 2713 | // For data pointers, we adjust the field offset directly. For functions, we |
| 2714 | // have a separate field. |
| 2715 | llvm::Constant *Adj = getMemberPointerAdjustment(E); |
| 2716 | if (Adj) { |
| 2717 | Adj = llvm::ConstantExpr::getTruncOrBitCast(Adj, CGM.IntTy); |
| 2718 | llvm::Constant *&NVAdjustField = |
| 2719 | IsFunc ? NonVirtualBaseAdjustment : FirstField; |
| 2720 | bool IsDerivedToBase = (E->getCastKind() == CK_DerivedToBaseMemberPointer); |
| 2721 | if (!NVAdjustField) // If this field didn't exist in src, it's zero. |
| 2722 | NVAdjustField = getZeroInt(); |
| 2723 | if (IsDerivedToBase) |
| 2724 | NVAdjustField = llvm::ConstantExpr::getNSWSub(NVAdjustField, Adj); |
| 2725 | else |
| 2726 | NVAdjustField = llvm::ConstantExpr::getNSWAdd(NVAdjustField, Adj); |
| 2727 | } |
| 2728 | |
| 2729 | // FIXME PR15713: Support conversions through virtually derived classes. |
| 2730 | |
| 2731 | // Recompose dst from the null struct and the adjusted fields from src. |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 2732 | if (MSInheritanceAttr::hasOnlyOneField(IsFunc, DstInheritance)) |
Reid Kleckner | 452abac | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 2733 | return FirstField; |
| 2734 | |
| 2735 | llvm::SmallVector<llvm::Constant *, 4> Fields; |
| 2736 | Fields.push_back(FirstField); |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 2737 | if (MSInheritanceAttr::hasNVOffsetField(IsFunc, DstInheritance)) |
Reid Kleckner | 452abac | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 2738 | Fields.push_back(getConstantOrZeroInt(NonVirtualBaseAdjustment)); |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 2739 | if (MSInheritanceAttr::hasVBPtrOffsetField(DstInheritance)) |
Reid Kleckner | 452abac | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 2740 | Fields.push_back(getConstantOrZeroInt(VBPtrOffset)); |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 2741 | if (MSInheritanceAttr::hasVBTableOffsetField(DstInheritance)) |
Reid Kleckner | 452abac | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 2742 | Fields.push_back(getConstantOrZeroInt(VirtualBaseAdjustmentOffset)); |
| 2743 | return llvm::ConstantStruct::getAnon(Fields); |
| 2744 | } |
| 2745 | |
David Majnemer | 2b0d66d | 2014-02-20 23:22:07 +0000 | [diff] [blame] | 2746 | llvm::Value *MicrosoftCXXABI::EmitLoadOfMemberFunctionPointer( |
| 2747 | CodeGenFunction &CGF, const Expr *E, llvm::Value *&This, |
| 2748 | llvm::Value *MemPtr, const MemberPointerType *MPT) { |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2749 | assert(MPT->isMemberFunctionPointer()); |
| 2750 | const FunctionProtoType *FPT = |
| 2751 | MPT->getPointeeType()->castAs<FunctionProtoType>(); |
David Majnemer | 1cdd96d | 2014-01-17 09:01:00 +0000 | [diff] [blame] | 2752 | const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl(); |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2753 | llvm::FunctionType *FTy = |
| 2754 | CGM.getTypes().GetFunctionType( |
| 2755 | CGM.getTypes().arrangeCXXMethodType(RD, FPT)); |
| 2756 | CGBuilderTy &Builder = CGF.Builder; |
| 2757 | |
David Majnemer | 1cdd96d | 2014-01-17 09:01:00 +0000 | [diff] [blame] | 2758 | MSInheritanceAttr::Spelling Inheritance = RD->getMSInheritanceModel(); |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2759 | |
| 2760 | // Extract the fields we need, regardless of model. We'll apply them if we |
| 2761 | // have them. |
| 2762 | llvm::Value *FunctionPointer = MemPtr; |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2763 | llvm::Value *NonVirtualBaseAdjustment = nullptr; |
| 2764 | llvm::Value *VirtualBaseAdjustmentOffset = nullptr; |
| 2765 | llvm::Value *VBPtrOffset = nullptr; |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2766 | if (MemPtr->getType()->isStructTy()) { |
| 2767 | // We need to extract values. |
| 2768 | unsigned I = 0; |
| 2769 | FunctionPointer = Builder.CreateExtractValue(MemPtr, I++); |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 2770 | if (MSInheritanceAttr::hasNVOffsetField(MPT, Inheritance)) |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2771 | NonVirtualBaseAdjustment = Builder.CreateExtractValue(MemPtr, I++); |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 2772 | if (MSInheritanceAttr::hasVBPtrOffsetField(Inheritance)) |
Reid Kleckner | 7d0efb5 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 2773 | VBPtrOffset = Builder.CreateExtractValue(MemPtr, I++); |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 2774 | if (MSInheritanceAttr::hasVBTableOffsetField(Inheritance)) |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2775 | VirtualBaseAdjustmentOffset = Builder.CreateExtractValue(MemPtr, I++); |
| 2776 | } |
| 2777 | |
| 2778 | if (VirtualBaseAdjustmentOffset) { |
David Majnemer | 2b0d66d | 2014-02-20 23:22:07 +0000 | [diff] [blame] | 2779 | This = AdjustVirtualBase(CGF, E, RD, This, VirtualBaseAdjustmentOffset, |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2780 | VBPtrOffset); |
| 2781 | } |
| 2782 | |
| 2783 | if (NonVirtualBaseAdjustment) { |
| 2784 | // Apply the adjustment and cast back to the original struct type. |
| 2785 | llvm::Value *Ptr = Builder.CreateBitCast(This, Builder.getInt8PtrTy()); |
| 2786 | Ptr = Builder.CreateInBoundsGEP(Ptr, NonVirtualBaseAdjustment); |
| 2787 | This = Builder.CreateBitCast(Ptr, This->getType(), "this.adjusted"); |
| 2788 | } |
| 2789 | |
| 2790 | return Builder.CreateBitCast(FunctionPointer, FTy->getPointerTo()); |
| 2791 | } |
| 2792 | |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 2793 | CGCXXABI *clang::CodeGen::CreateMicrosoftCXXABI(CodeGenModule &CGM) { |
Charles Davis | 74ce859 | 2010-06-09 23:25:41 +0000 | [diff] [blame] | 2794 | return new MicrosoftCXXABI(CGM); |
| 2795 | } |
David Majnemer | e2cb8d1 | 2014-07-07 06:20:47 +0000 | [diff] [blame] | 2796 | |
| 2797 | // MS RTTI Overview: |
| 2798 | // The run time type information emitted by cl.exe contains 5 distinct types of |
| 2799 | // structures. Many of them reference each other. |
| 2800 | // |
| 2801 | // TypeInfo: Static classes that are returned by typeid. |
| 2802 | // |
| 2803 | // CompleteObjectLocator: Referenced by vftables. They contain information |
| 2804 | // required for dynamic casting, including OffsetFromTop. They also contain |
| 2805 | // a reference to the TypeInfo for the type and a reference to the |
| 2806 | // CompleteHierarchyDescriptor for the type. |
| 2807 | // |
| 2808 | // ClassHieararchyDescriptor: Contains information about a class hierarchy. |
| 2809 | // Used during dynamic_cast to walk a class hierarchy. References a base |
| 2810 | // class array and the size of said array. |
| 2811 | // |
| 2812 | // BaseClassArray: Contains a list of classes in a hierarchy. BaseClassArray is |
| 2813 | // somewhat of a misnomer because the most derived class is also in the list |
| 2814 | // as well as multiple copies of virtual bases (if they occur multiple times |
| 2815 | // in the hiearchy.) The BaseClassArray contains one BaseClassDescriptor for |
| 2816 | // every path in the hierarchy, in pre-order depth first order. Note, we do |
| 2817 | // not declare a specific llvm type for BaseClassArray, it's merely an array |
| 2818 | // of BaseClassDescriptor pointers. |
| 2819 | // |
| 2820 | // BaseClassDescriptor: Contains information about a class in a class hierarchy. |
| 2821 | // BaseClassDescriptor is also somewhat of a misnomer for the same reason that |
| 2822 | // BaseClassArray is. It contains information about a class within a |
| 2823 | // hierarchy such as: is this base is ambiguous and what is its offset in the |
| 2824 | // vbtable. The names of the BaseClassDescriptors have all of their fields |
| 2825 | // mangled into them so they can be aggressively deduplicated by the linker. |
| 2826 | |
David Majnemer | e2cb8d1 | 2014-07-07 06:20:47 +0000 | [diff] [blame] | 2827 | static llvm::GlobalVariable *getTypeInfoVTable(CodeGenModule &CGM) { |
| 2828 | StringRef MangledName("\01??_7type_info@@6B@"); |
| 2829 | if (auto VTable = CGM.getModule().getNamedGlobal(MangledName)) |
| 2830 | return VTable; |
| 2831 | return new llvm::GlobalVariable(CGM.getModule(), CGM.Int8PtrTy, |
| 2832 | /*Constant=*/true, |
| 2833 | llvm::GlobalVariable::ExternalLinkage, |
| 2834 | /*Initializer=*/nullptr, MangledName); |
| 2835 | } |
| 2836 | |
| 2837 | namespace { |
| 2838 | |
| 2839 | /// \brief A Helper struct that stores information about a class in a class |
| 2840 | /// hierarchy. The information stored in these structs struct is used during |
| 2841 | /// the generation of ClassHierarchyDescriptors and BaseClassDescriptors. |
| 2842 | // During RTTI creation, MSRTTIClasses are stored in a contiguous array with |
| 2843 | // implicit depth first pre-order tree connectivity. getFirstChild and |
| 2844 | // getNextSibling allow us to walk the tree efficiently. |
| 2845 | struct MSRTTIClass { |
| 2846 | enum { |
| 2847 | IsPrivateOnPath = 1 | 8, |
| 2848 | IsAmbiguous = 2, |
| 2849 | IsPrivate = 4, |
| 2850 | IsVirtual = 16, |
| 2851 | HasHierarchyDescriptor = 64 |
| 2852 | }; |
| 2853 | MSRTTIClass(const CXXRecordDecl *RD) : RD(RD) {} |
| 2854 | uint32_t initialize(const MSRTTIClass *Parent, |
| 2855 | const CXXBaseSpecifier *Specifier); |
| 2856 | |
| 2857 | MSRTTIClass *getFirstChild() { return this + 1; } |
| 2858 | static MSRTTIClass *getNextChild(MSRTTIClass *Child) { |
| 2859 | return Child + 1 + Child->NumBases; |
| 2860 | } |
| 2861 | |
| 2862 | const CXXRecordDecl *RD, *VirtualRoot; |
| 2863 | uint32_t Flags, NumBases, OffsetInVBase; |
| 2864 | }; |
| 2865 | |
| 2866 | /// \brief Recursively initialize the base class array. |
| 2867 | uint32_t MSRTTIClass::initialize(const MSRTTIClass *Parent, |
| 2868 | const CXXBaseSpecifier *Specifier) { |
| 2869 | Flags = HasHierarchyDescriptor; |
| 2870 | if (!Parent) { |
| 2871 | VirtualRoot = nullptr; |
| 2872 | OffsetInVBase = 0; |
| 2873 | } else { |
| 2874 | if (Specifier->getAccessSpecifier() != AS_public) |
| 2875 | Flags |= IsPrivate | IsPrivateOnPath; |
| 2876 | if (Specifier->isVirtual()) { |
| 2877 | Flags |= IsVirtual; |
| 2878 | VirtualRoot = RD; |
| 2879 | OffsetInVBase = 0; |
| 2880 | } else { |
| 2881 | if (Parent->Flags & IsPrivateOnPath) |
| 2882 | Flags |= IsPrivateOnPath; |
| 2883 | VirtualRoot = Parent->VirtualRoot; |
| 2884 | OffsetInVBase = Parent->OffsetInVBase + RD->getASTContext() |
| 2885 | .getASTRecordLayout(Parent->RD).getBaseClassOffset(RD).getQuantity(); |
| 2886 | } |
| 2887 | } |
| 2888 | NumBases = 0; |
| 2889 | MSRTTIClass *Child = getFirstChild(); |
| 2890 | for (const CXXBaseSpecifier &Base : RD->bases()) { |
| 2891 | NumBases += Child->initialize(this, &Base) + 1; |
| 2892 | Child = getNextChild(Child); |
| 2893 | } |
| 2894 | return NumBases; |
| 2895 | } |
| 2896 | |
| 2897 | static llvm::GlobalValue::LinkageTypes getLinkageForRTTI(QualType Ty) { |
| 2898 | switch (Ty->getLinkage()) { |
| 2899 | case NoLinkage: |
| 2900 | case InternalLinkage: |
| 2901 | case UniqueExternalLinkage: |
| 2902 | return llvm::GlobalValue::InternalLinkage; |
| 2903 | |
| 2904 | case VisibleNoLinkage: |
| 2905 | case ExternalLinkage: |
| 2906 | return llvm::GlobalValue::LinkOnceODRLinkage; |
| 2907 | } |
| 2908 | llvm_unreachable("Invalid linkage!"); |
| 2909 | } |
| 2910 | |
| 2911 | /// \brief An ephemeral helper class for building MS RTTI types. It caches some |
| 2912 | /// calls to the module and information about the most derived class in a |
| 2913 | /// hierarchy. |
| 2914 | struct MSRTTIBuilder { |
| 2915 | enum { |
| 2916 | HasBranchingHierarchy = 1, |
| 2917 | HasVirtualBranchingHierarchy = 2, |
| 2918 | HasAmbiguousBases = 4 |
| 2919 | }; |
| 2920 | |
David Majnemer | 611cdb9 | 2014-07-07 08:09:15 +0000 | [diff] [blame] | 2921 | MSRTTIBuilder(MicrosoftCXXABI &ABI, const CXXRecordDecl *RD) |
| 2922 | : CGM(ABI.CGM), Context(CGM.getContext()), |
| 2923 | VMContext(CGM.getLLVMContext()), Module(CGM.getModule()), RD(RD), |
David Majnemer | e2cb8d1 | 2014-07-07 06:20:47 +0000 | [diff] [blame] | 2924 | Linkage(getLinkageForRTTI(CGM.getContext().getTagDeclType(RD))), |
David Majnemer | 611cdb9 | 2014-07-07 08:09:15 +0000 | [diff] [blame] | 2925 | ABI(ABI) {} |
David Majnemer | e2cb8d1 | 2014-07-07 06:20:47 +0000 | [diff] [blame] | 2926 | |
| 2927 | llvm::GlobalVariable *getBaseClassDescriptor(const MSRTTIClass &Classes); |
| 2928 | llvm::GlobalVariable * |
| 2929 | getBaseClassArray(SmallVectorImpl<MSRTTIClass> &Classes); |
| 2930 | llvm::GlobalVariable *getClassHierarchyDescriptor(); |
| 2931 | llvm::GlobalVariable *getCompleteObjectLocator(const VPtrInfo *Info); |
| 2932 | |
| 2933 | CodeGenModule &CGM; |
| 2934 | ASTContext &Context; |
| 2935 | llvm::LLVMContext &VMContext; |
| 2936 | llvm::Module &Module; |
| 2937 | const CXXRecordDecl *RD; |
| 2938 | llvm::GlobalVariable::LinkageTypes Linkage; |
David Majnemer | 611cdb9 | 2014-07-07 08:09:15 +0000 | [diff] [blame] | 2939 | MicrosoftCXXABI &ABI; |
David Majnemer | e2cb8d1 | 2014-07-07 06:20:47 +0000 | [diff] [blame] | 2940 | }; |
| 2941 | |
| 2942 | } // namespace |
| 2943 | |
| 2944 | /// \brief Recursively serializes a class hierarchy in pre-order depth first |
| 2945 | /// order. |
| 2946 | static void serializeClassHierarchy(SmallVectorImpl<MSRTTIClass> &Classes, |
| 2947 | const CXXRecordDecl *RD) { |
| 2948 | Classes.push_back(MSRTTIClass(RD)); |
| 2949 | for (const CXXBaseSpecifier &Base : RD->bases()) |
| 2950 | serializeClassHierarchy(Classes, Base.getType()->getAsCXXRecordDecl()); |
| 2951 | } |
| 2952 | |
| 2953 | /// \brief Find ambiguity among base classes. |
| 2954 | static void |
| 2955 | detectAmbiguousBases(SmallVectorImpl<MSRTTIClass> &Classes) { |
| 2956 | llvm::SmallPtrSet<const CXXRecordDecl *, 8> VirtualBases; |
| 2957 | llvm::SmallPtrSet<const CXXRecordDecl *, 8> UniqueBases; |
| 2958 | llvm::SmallPtrSet<const CXXRecordDecl *, 8> AmbiguousBases; |
| 2959 | for (MSRTTIClass *Class = &Classes.front(); Class <= &Classes.back();) { |
| 2960 | if ((Class->Flags & MSRTTIClass::IsVirtual) && |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 2961 | !VirtualBases.insert(Class->RD).second) { |
David Majnemer | e2cb8d1 | 2014-07-07 06:20:47 +0000 | [diff] [blame] | 2962 | Class = MSRTTIClass::getNextChild(Class); |
| 2963 | continue; |
| 2964 | } |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 2965 | if (!UniqueBases.insert(Class->RD).second) |
David Majnemer | e2cb8d1 | 2014-07-07 06:20:47 +0000 | [diff] [blame] | 2966 | AmbiguousBases.insert(Class->RD); |
| 2967 | Class++; |
| 2968 | } |
| 2969 | if (AmbiguousBases.empty()) |
| 2970 | return; |
| 2971 | for (MSRTTIClass &Class : Classes) |
| 2972 | if (AmbiguousBases.count(Class.RD)) |
| 2973 | Class.Flags |= MSRTTIClass::IsAmbiguous; |
| 2974 | } |
| 2975 | |
| 2976 | llvm::GlobalVariable *MSRTTIBuilder::getClassHierarchyDescriptor() { |
| 2977 | SmallString<256> MangledName; |
| 2978 | { |
| 2979 | llvm::raw_svector_ostream Out(MangledName); |
David Majnemer | 611cdb9 | 2014-07-07 08:09:15 +0000 | [diff] [blame] | 2980 | ABI.getMangleContext().mangleCXXRTTIClassHierarchyDescriptor(RD, Out); |
David Majnemer | e2cb8d1 | 2014-07-07 06:20:47 +0000 | [diff] [blame] | 2981 | } |
| 2982 | |
| 2983 | // Check to see if we've already declared this ClassHierarchyDescriptor. |
| 2984 | if (auto CHD = Module.getNamedGlobal(MangledName)) |
| 2985 | return CHD; |
| 2986 | |
| 2987 | // Serialize the class hierarchy and initialize the CHD Fields. |
| 2988 | SmallVector<MSRTTIClass, 8> Classes; |
| 2989 | serializeClassHierarchy(Classes, RD); |
| 2990 | Classes.front().initialize(/*Parent=*/nullptr, /*Specifier=*/nullptr); |
| 2991 | detectAmbiguousBases(Classes); |
| 2992 | int Flags = 0; |
| 2993 | for (auto Class : Classes) { |
| 2994 | if (Class.RD->getNumBases() > 1) |
| 2995 | Flags |= HasBranchingHierarchy; |
| 2996 | // Note: cl.exe does not calculate "HasAmbiguousBases" correctly. We |
| 2997 | // believe the field isn't actually used. |
| 2998 | if (Class.Flags & MSRTTIClass::IsAmbiguous) |
| 2999 | Flags |= HasAmbiguousBases; |
| 3000 | } |
| 3001 | if ((Flags & HasBranchingHierarchy) && RD->getNumVBases() != 0) |
| 3002 | Flags |= HasVirtualBranchingHierarchy; |
| 3003 | // These gep indices are used to get the address of the first element of the |
| 3004 | // base class array. |
| 3005 | llvm::Value *GEPIndices[] = {llvm::ConstantInt::get(CGM.IntTy, 0), |
| 3006 | llvm::ConstantInt::get(CGM.IntTy, 0)}; |
| 3007 | |
| 3008 | // Forward-declare the class hierarchy descriptor |
David Majnemer | 611cdb9 | 2014-07-07 08:09:15 +0000 | [diff] [blame] | 3009 | auto Type = ABI.getClassHierarchyDescriptorType(); |
David Majnemer | e2cb8d1 | 2014-07-07 06:20:47 +0000 | [diff] [blame] | 3010 | auto CHD = new llvm::GlobalVariable(Module, Type, /*Constant=*/true, Linkage, |
| 3011 | /*Initializer=*/nullptr, |
David Majnemer | 7c23707 | 2015-03-05 00:46:22 +0000 | [diff] [blame] | 3012 | StringRef(MangledName)); |
Rafael Espindola | 654542a | 2015-01-16 19:23:42 +0000 | [diff] [blame] | 3013 | if (CHD->isWeakForLinker()) |
| 3014 | CHD->setComdat(CGM.getModule().getOrInsertComdat(CHD->getName())); |
David Majnemer | e2cb8d1 | 2014-07-07 06:20:47 +0000 | [diff] [blame] | 3015 | |
| 3016 | // Initialize the base class ClassHierarchyDescriptor. |
| 3017 | llvm::Constant *Fields[] = { |
David Majnemer | 611cdb9 | 2014-07-07 08:09:15 +0000 | [diff] [blame] | 3018 | llvm::ConstantInt::get(CGM.IntTy, 0), // Unknown |
| 3019 | llvm::ConstantInt::get(CGM.IntTy, Flags), |
| 3020 | llvm::ConstantInt::get(CGM.IntTy, Classes.size()), |
| 3021 | ABI.getImageRelativeConstant(llvm::ConstantExpr::getInBoundsGetElementPtr( |
| 3022 | getBaseClassArray(Classes), |
| 3023 | llvm::ArrayRef<llvm::Value *>(GEPIndices))), |
David Majnemer | e2cb8d1 | 2014-07-07 06:20:47 +0000 | [diff] [blame] | 3024 | }; |
| 3025 | CHD->setInitializer(llvm::ConstantStruct::get(Type, Fields)); |
| 3026 | return CHD; |
| 3027 | } |
| 3028 | |
| 3029 | llvm::GlobalVariable * |
| 3030 | MSRTTIBuilder::getBaseClassArray(SmallVectorImpl<MSRTTIClass> &Classes) { |
| 3031 | SmallString<256> MangledName; |
| 3032 | { |
| 3033 | llvm::raw_svector_ostream Out(MangledName); |
David Majnemer | 611cdb9 | 2014-07-07 08:09:15 +0000 | [diff] [blame] | 3034 | ABI.getMangleContext().mangleCXXRTTIBaseClassArray(RD, Out); |
David Majnemer | e2cb8d1 | 2014-07-07 06:20:47 +0000 | [diff] [blame] | 3035 | } |
| 3036 | |
| 3037 | // Forward-declare the base class array. |
| 3038 | // cl.exe pads the base class array with 1 (in 32 bit mode) or 4 (in 64 bit |
| 3039 | // mode) bytes of padding. We provide a pointer sized amount of padding by |
| 3040 | // adding +1 to Classes.size(). The sections have pointer alignment and are |
| 3041 | // marked pick-any so it shouldn't matter. |
David Majnemer | 26a90f8 | 2014-07-07 15:29:10 +0000 | [diff] [blame] | 3042 | llvm::Type *PtrType = ABI.getImageRelativeType( |
David Majnemer | 611cdb9 | 2014-07-07 08:09:15 +0000 | [diff] [blame] | 3043 | ABI.getBaseClassDescriptorType()->getPointerTo()); |
David Majnemer | 26a90f8 | 2014-07-07 15:29:10 +0000 | [diff] [blame] | 3044 | auto *ArrType = llvm::ArrayType::get(PtrType, Classes.size() + 1); |
David Majnemer | 7c23707 | 2015-03-05 00:46:22 +0000 | [diff] [blame] | 3045 | auto *BCA = |
| 3046 | new llvm::GlobalVariable(Module, ArrType, |
| 3047 | /*Constant=*/true, Linkage, |
| 3048 | /*Initializer=*/nullptr, StringRef(MangledName)); |
Rafael Espindola | 654542a | 2015-01-16 19:23:42 +0000 | [diff] [blame] | 3049 | if (BCA->isWeakForLinker()) |
| 3050 | BCA->setComdat(CGM.getModule().getOrInsertComdat(BCA->getName())); |
David Majnemer | e2cb8d1 | 2014-07-07 06:20:47 +0000 | [diff] [blame] | 3051 | |
| 3052 | // Initialize the BaseClassArray. |
| 3053 | SmallVector<llvm::Constant *, 8> BaseClassArrayData; |
| 3054 | for (MSRTTIClass &Class : Classes) |
| 3055 | BaseClassArrayData.push_back( |
David Majnemer | 611cdb9 | 2014-07-07 08:09:15 +0000 | [diff] [blame] | 3056 | ABI.getImageRelativeConstant(getBaseClassDescriptor(Class))); |
David Majnemer | e2cb8d1 | 2014-07-07 06:20:47 +0000 | [diff] [blame] | 3057 | BaseClassArrayData.push_back(llvm::Constant::getNullValue(PtrType)); |
David Majnemer | 26a90f8 | 2014-07-07 15:29:10 +0000 | [diff] [blame] | 3058 | BCA->setInitializer(llvm::ConstantArray::get(ArrType, BaseClassArrayData)); |
David Majnemer | e2cb8d1 | 2014-07-07 06:20:47 +0000 | [diff] [blame] | 3059 | return BCA; |
| 3060 | } |
| 3061 | |
| 3062 | llvm::GlobalVariable * |
| 3063 | MSRTTIBuilder::getBaseClassDescriptor(const MSRTTIClass &Class) { |
| 3064 | // Compute the fields for the BaseClassDescriptor. They are computed up front |
| 3065 | // because they are mangled into the name of the object. |
| 3066 | uint32_t OffsetInVBTable = 0; |
| 3067 | int32_t VBPtrOffset = -1; |
| 3068 | if (Class.VirtualRoot) { |
| 3069 | auto &VTableContext = CGM.getMicrosoftVTableContext(); |
| 3070 | OffsetInVBTable = VTableContext.getVBTableIndex(RD, Class.VirtualRoot) * 4; |
| 3071 | VBPtrOffset = Context.getASTRecordLayout(RD).getVBPtrOffset().getQuantity(); |
| 3072 | } |
| 3073 | |
| 3074 | SmallString<256> MangledName; |
| 3075 | { |
| 3076 | llvm::raw_svector_ostream Out(MangledName); |
David Majnemer | 611cdb9 | 2014-07-07 08:09:15 +0000 | [diff] [blame] | 3077 | ABI.getMangleContext().mangleCXXRTTIBaseClassDescriptor( |
| 3078 | Class.RD, Class.OffsetInVBase, VBPtrOffset, OffsetInVBTable, |
| 3079 | Class.Flags, Out); |
David Majnemer | e2cb8d1 | 2014-07-07 06:20:47 +0000 | [diff] [blame] | 3080 | } |
| 3081 | |
David Majnemer | 26a90f8 | 2014-07-07 15:29:10 +0000 | [diff] [blame] | 3082 | // Check to see if we've already declared this object. |
David Majnemer | e2cb8d1 | 2014-07-07 06:20:47 +0000 | [diff] [blame] | 3083 | if (auto BCD = Module.getNamedGlobal(MangledName)) |
| 3084 | return BCD; |
| 3085 | |
| 3086 | // Forward-declare the base class descriptor. |
David Majnemer | 611cdb9 | 2014-07-07 08:09:15 +0000 | [diff] [blame] | 3087 | auto Type = ABI.getBaseClassDescriptorType(); |
David Majnemer | 7c23707 | 2015-03-05 00:46:22 +0000 | [diff] [blame] | 3088 | auto BCD = |
| 3089 | new llvm::GlobalVariable(Module, Type, /*Constant=*/true, Linkage, |
| 3090 | /*Initializer=*/nullptr, StringRef(MangledName)); |
Rafael Espindola | 654542a | 2015-01-16 19:23:42 +0000 | [diff] [blame] | 3091 | if (BCD->isWeakForLinker()) |
| 3092 | BCD->setComdat(CGM.getModule().getOrInsertComdat(BCD->getName())); |
David Majnemer | e2cb8d1 | 2014-07-07 06:20:47 +0000 | [diff] [blame] | 3093 | |
| 3094 | // Initialize the BaseClassDescriptor. |
| 3095 | llvm::Constant *Fields[] = { |
David Majnemer | 611cdb9 | 2014-07-07 08:09:15 +0000 | [diff] [blame] | 3096 | ABI.getImageRelativeConstant( |
David Majnemer | ccef89d | 2014-07-07 15:29:16 +0000 | [diff] [blame] | 3097 | ABI.getAddrOfRTTIDescriptor(Context.getTypeDeclType(Class.RD))), |
David Majnemer | 611cdb9 | 2014-07-07 08:09:15 +0000 | [diff] [blame] | 3098 | llvm::ConstantInt::get(CGM.IntTy, Class.NumBases), |
| 3099 | llvm::ConstantInt::get(CGM.IntTy, Class.OffsetInVBase), |
| 3100 | llvm::ConstantInt::get(CGM.IntTy, VBPtrOffset), |
| 3101 | llvm::ConstantInt::get(CGM.IntTy, OffsetInVBTable), |
| 3102 | llvm::ConstantInt::get(CGM.IntTy, Class.Flags), |
| 3103 | ABI.getImageRelativeConstant( |
| 3104 | MSRTTIBuilder(ABI, Class.RD).getClassHierarchyDescriptor()), |
David Majnemer | e2cb8d1 | 2014-07-07 06:20:47 +0000 | [diff] [blame] | 3105 | }; |
| 3106 | BCD->setInitializer(llvm::ConstantStruct::get(Type, Fields)); |
| 3107 | return BCD; |
| 3108 | } |
| 3109 | |
| 3110 | llvm::GlobalVariable * |
| 3111 | MSRTTIBuilder::getCompleteObjectLocator(const VPtrInfo *Info) { |
| 3112 | SmallString<256> MangledName; |
| 3113 | { |
| 3114 | llvm::raw_svector_ostream Out(MangledName); |
David Majnemer | 611cdb9 | 2014-07-07 08:09:15 +0000 | [diff] [blame] | 3115 | ABI.getMangleContext().mangleCXXRTTICompleteObjectLocator(RD, Info->MangledPath, Out); |
David Majnemer | e2cb8d1 | 2014-07-07 06:20:47 +0000 | [diff] [blame] | 3116 | } |
| 3117 | |
| 3118 | // Check to see if we've already computed this complete object locator. |
| 3119 | if (auto COL = Module.getNamedGlobal(MangledName)) |
| 3120 | return COL; |
| 3121 | |
| 3122 | // Compute the fields of the complete object locator. |
| 3123 | int OffsetToTop = Info->FullOffsetInMDC.getQuantity(); |
| 3124 | int VFPtrOffset = 0; |
| 3125 | // The offset includes the vtordisp if one exists. |
| 3126 | if (const CXXRecordDecl *VBase = Info->getVBaseWithVPtr()) |
| 3127 | if (Context.getASTRecordLayout(RD) |
| 3128 | .getVBaseOffsetsMap() |
| 3129 | .find(VBase) |
| 3130 | ->second.hasVtorDisp()) |
| 3131 | VFPtrOffset = Info->NonVirtualOffset.getQuantity() + 4; |
| 3132 | |
| 3133 | // Forward-declare the complete object locator. |
David Majnemer | 611cdb9 | 2014-07-07 08:09:15 +0000 | [diff] [blame] | 3134 | llvm::StructType *Type = ABI.getCompleteObjectLocatorType(); |
David Majnemer | e2cb8d1 | 2014-07-07 06:20:47 +0000 | [diff] [blame] | 3135 | auto COL = new llvm::GlobalVariable(Module, Type, /*Constant=*/true, Linkage, |
David Majnemer | 7c23707 | 2015-03-05 00:46:22 +0000 | [diff] [blame] | 3136 | /*Initializer=*/nullptr, StringRef(MangledName)); |
David Majnemer | e2cb8d1 | 2014-07-07 06:20:47 +0000 | [diff] [blame] | 3137 | |
| 3138 | // Initialize the CompleteObjectLocator. |
| 3139 | llvm::Constant *Fields[] = { |
David Majnemer | 611cdb9 | 2014-07-07 08:09:15 +0000 | [diff] [blame] | 3140 | llvm::ConstantInt::get(CGM.IntTy, ABI.isImageRelative()), |
| 3141 | llvm::ConstantInt::get(CGM.IntTy, OffsetToTop), |
| 3142 | llvm::ConstantInt::get(CGM.IntTy, VFPtrOffset), |
| 3143 | ABI.getImageRelativeConstant( |
| 3144 | CGM.GetAddrOfRTTIDescriptor(Context.getTypeDeclType(RD))), |
| 3145 | ABI.getImageRelativeConstant(getClassHierarchyDescriptor()), |
| 3146 | ABI.getImageRelativeConstant(COL), |
David Majnemer | e2cb8d1 | 2014-07-07 06:20:47 +0000 | [diff] [blame] | 3147 | }; |
| 3148 | llvm::ArrayRef<llvm::Constant *> FieldsRef(Fields); |
David Majnemer | 611cdb9 | 2014-07-07 08:09:15 +0000 | [diff] [blame] | 3149 | if (!ABI.isImageRelative()) |
| 3150 | FieldsRef = FieldsRef.drop_back(); |
David Majnemer | e2cb8d1 | 2014-07-07 06:20:47 +0000 | [diff] [blame] | 3151 | COL->setInitializer(llvm::ConstantStruct::get(Type, FieldsRef)); |
Rafael Espindola | 654542a | 2015-01-16 19:23:42 +0000 | [diff] [blame] | 3152 | if (COL->isWeakForLinker()) |
| 3153 | COL->setComdat(CGM.getModule().getOrInsertComdat(COL->getName())); |
David Majnemer | e2cb8d1 | 2014-07-07 06:20:47 +0000 | [diff] [blame] | 3154 | return COL; |
| 3155 | } |
| 3156 | |
| 3157 | /// \brief Gets a TypeDescriptor. Returns a llvm::Constant * rather than a |
| 3158 | /// llvm::GlobalVariable * because different type descriptors have different |
| 3159 | /// types, and need to be abstracted. They are abstracting by casting the |
| 3160 | /// address to an Int8PtrTy. |
| 3161 | llvm::Constant *MicrosoftCXXABI::getAddrOfRTTIDescriptor(QualType Type) { |
David Majnemer | e2cb8d1 | 2014-07-07 06:20:47 +0000 | [diff] [blame] | 3162 | SmallString<256> MangledName, TypeInfoString; |
| 3163 | { |
| 3164 | llvm::raw_svector_ostream Out(MangledName); |
David Majnemer | 611cdb9 | 2014-07-07 08:09:15 +0000 | [diff] [blame] | 3165 | getMangleContext().mangleCXXRTTI(Type, Out); |
David Majnemer | e2cb8d1 | 2014-07-07 06:20:47 +0000 | [diff] [blame] | 3166 | } |
| 3167 | |
| 3168 | // Check to see if we've already declared this TypeDescriptor. |
| 3169 | if (llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(MangledName)) |
| 3170 | return llvm::ConstantExpr::getBitCast(GV, CGM.Int8PtrTy); |
| 3171 | |
| 3172 | // Compute the fields for the TypeDescriptor. |
| 3173 | { |
| 3174 | llvm::raw_svector_ostream Out(TypeInfoString); |
David Majnemer | 611cdb9 | 2014-07-07 08:09:15 +0000 | [diff] [blame] | 3175 | getMangleContext().mangleCXXRTTIName(Type, Out); |
David Majnemer | e2cb8d1 | 2014-07-07 06:20:47 +0000 | [diff] [blame] | 3176 | } |
| 3177 | |
| 3178 | // Declare and initialize the TypeDescriptor. |
| 3179 | llvm::Constant *Fields[] = { |
| 3180 | getTypeInfoVTable(CGM), // VFPtr |
| 3181 | llvm::ConstantPointerNull::get(CGM.Int8PtrTy), // Runtime data |
| 3182 | llvm::ConstantDataArray::getString(CGM.getLLVMContext(), TypeInfoString)}; |
| 3183 | llvm::StructType *TypeDescriptorType = |
David Majnemer | 611cdb9 | 2014-07-07 08:09:15 +0000 | [diff] [blame] | 3184 | getTypeDescriptorType(TypeInfoString); |
Rafael Espindola | 654542a | 2015-01-16 19:23:42 +0000 | [diff] [blame] | 3185 | auto *Var = new llvm::GlobalVariable( |
| 3186 | CGM.getModule(), TypeDescriptorType, /*Constant=*/false, |
| 3187 | getLinkageForRTTI(Type), |
| 3188 | llvm::ConstantStruct::get(TypeDescriptorType, Fields), |
David Majnemer | 7c23707 | 2015-03-05 00:46:22 +0000 | [diff] [blame] | 3189 | StringRef(MangledName)); |
Rafael Espindola | 654542a | 2015-01-16 19:23:42 +0000 | [diff] [blame] | 3190 | if (Var->isWeakForLinker()) |
| 3191 | Var->setComdat(CGM.getModule().getOrInsertComdat(Var->getName())); |
| 3192 | return llvm::ConstantExpr::getBitCast(Var, CGM.Int8PtrTy); |
David Majnemer | e2cb8d1 | 2014-07-07 06:20:47 +0000 | [diff] [blame] | 3193 | } |
| 3194 | |
| 3195 | /// \brief Gets or a creates a Microsoft CompleteObjectLocator. |
| 3196 | llvm::GlobalVariable * |
| 3197 | MicrosoftCXXABI::getMSCompleteObjectLocator(const CXXRecordDecl *RD, |
| 3198 | const VPtrInfo *Info) { |
David Majnemer | 611cdb9 | 2014-07-07 08:09:15 +0000 | [diff] [blame] | 3199 | return MSRTTIBuilder(*this, RD).getCompleteObjectLocator(Info); |
David Majnemer | e2cb8d1 | 2014-07-07 06:20:47 +0000 | [diff] [blame] | 3200 | } |
Rafael Espindola | 91f68b4 | 2014-09-15 19:20:10 +0000 | [diff] [blame] | 3201 | |
| 3202 | static void emitCXXConstructor(CodeGenModule &CGM, |
| 3203 | const CXXConstructorDecl *ctor, |
| 3204 | StructorType ctorType) { |
Rafael Espindola | d48b51b | 2014-09-15 19:24:44 +0000 | [diff] [blame] | 3205 | // There are no constructor variants, always emit the complete destructor. |
Rafael Espindola | 694cb5d | 2015-01-16 15:37:11 +0000 | [diff] [blame] | 3206 | llvm::Function *Fn = CGM.codegenCXXStructor(ctor, StructorType::Complete); |
| 3207 | CGM.maybeSetTrivialComdat(*ctor, *Fn); |
Rafael Espindola | 91f68b4 | 2014-09-15 19:20:10 +0000 | [diff] [blame] | 3208 | } |
| 3209 | |
| 3210 | static void emitCXXDestructor(CodeGenModule &CGM, const CXXDestructorDecl *dtor, |
| 3211 | StructorType dtorType) { |
| 3212 | // The complete destructor is equivalent to the base destructor for |
| 3213 | // classes with no virtual bases, so try to emit it as an alias. |
| 3214 | if (!dtor->getParent()->getNumVBases() && |
| 3215 | (dtorType == StructorType::Complete || dtorType == StructorType::Base)) { |
| 3216 | bool ProducedAlias = !CGM.TryEmitDefinitionAsAlias( |
| 3217 | GlobalDecl(dtor, Dtor_Complete), GlobalDecl(dtor, Dtor_Base), true); |
| 3218 | if (ProducedAlias) { |
| 3219 | if (dtorType == StructorType::Complete) |
| 3220 | return; |
| 3221 | if (dtor->isVirtual()) |
| 3222 | CGM.getVTables().EmitThunks(GlobalDecl(dtor, Dtor_Complete)); |
| 3223 | } |
| 3224 | } |
| 3225 | |
| 3226 | // The base destructor is equivalent to the base destructor of its |
| 3227 | // base class if there is exactly one non-virtual base class with a |
| 3228 | // non-trivial destructor, there are no fields with a non-trivial |
| 3229 | // destructor, and the body of the destructor is trivial. |
| 3230 | if (dtorType == StructorType::Base && !CGM.TryEmitBaseDestructorAsAlias(dtor)) |
| 3231 | return; |
| 3232 | |
Rafael Espindola | 694cb5d | 2015-01-16 15:37:11 +0000 | [diff] [blame] | 3233 | llvm::Function *Fn = CGM.codegenCXXStructor(dtor, dtorType); |
Rafael Espindola | d3e0469 | 2015-01-17 01:47:39 +0000 | [diff] [blame] | 3234 | if (Fn->isWeakForLinker()) |
| 3235 | Fn->setComdat(CGM.getModule().getOrInsertComdat(Fn->getName())); |
Rafael Espindola | 91f68b4 | 2014-09-15 19:20:10 +0000 | [diff] [blame] | 3236 | } |
| 3237 | |
| 3238 | void MicrosoftCXXABI::emitCXXStructor(const CXXMethodDecl *MD, |
| 3239 | StructorType Type) { |
| 3240 | if (auto *CD = dyn_cast<CXXConstructorDecl>(MD)) { |
| 3241 | emitCXXConstructor(CGM, CD, Type); |
| 3242 | return; |
| 3243 | } |
| 3244 | emitCXXDestructor(CGM, cast<CXXDestructorDecl>(MD), Type); |
| 3245 | } |
David Majnemer | 7c23707 | 2015-03-05 00:46:22 +0000 | [diff] [blame] | 3246 | |
David Majnemer | dfa6d20 | 2015-03-11 18:36:39 +0000 | [diff] [blame] | 3247 | llvm::Function * |
David Majnemer | 37fd66e | 2015-03-13 22:36:55 +0000 | [diff] [blame] | 3248 | MicrosoftCXXABI::getAddrOfCXXCtorClosure(const CXXConstructorDecl *CD, |
| 3249 | CXXCtorType CT) { |
| 3250 | assert(CT == Ctor_CopyingClosure || CT == Ctor_DefaultClosure); |
| 3251 | |
David Majnemer | dfa6d20 | 2015-03-11 18:36:39 +0000 | [diff] [blame] | 3252 | // Calculate the mangled name. |
| 3253 | SmallString<256> ThunkName; |
| 3254 | llvm::raw_svector_ostream Out(ThunkName); |
David Majnemer | 37fd66e | 2015-03-13 22:36:55 +0000 | [diff] [blame] | 3255 | getMangleContext().mangleCXXCtor(CD, CT, Out); |
David Majnemer | dfa6d20 | 2015-03-11 18:36:39 +0000 | [diff] [blame] | 3256 | Out.flush(); |
| 3257 | |
| 3258 | // If the thunk has been generated previously, just return it. |
| 3259 | if (llvm::GlobalValue *GV = CGM.getModule().getNamedValue(ThunkName)) |
| 3260 | return cast<llvm::Function>(GV); |
| 3261 | |
| 3262 | // Create the llvm::Function. |
David Majnemer | 37fd66e | 2015-03-13 22:36:55 +0000 | [diff] [blame] | 3263 | const CGFunctionInfo &FnInfo = CGM.getTypes().arrangeMSCtorClosure(CD, CT); |
David Majnemer | dfa6d20 | 2015-03-11 18:36:39 +0000 | [diff] [blame] | 3264 | llvm::FunctionType *ThunkTy = CGM.getTypes().GetFunctionType(FnInfo); |
| 3265 | const CXXRecordDecl *RD = CD->getParent(); |
| 3266 | QualType RecordTy = getContext().getRecordType(RD); |
| 3267 | llvm::Function *ThunkFn = llvm::Function::Create( |
| 3268 | ThunkTy, getLinkageForRTTI(RecordTy), ThunkName.str(), &CGM.getModule()); |
David Majnemer | 37fd66e | 2015-03-13 22:36:55 +0000 | [diff] [blame] | 3269 | bool IsCopy = CT == Ctor_CopyingClosure; |
David Majnemer | dfa6d20 | 2015-03-11 18:36:39 +0000 | [diff] [blame] | 3270 | |
| 3271 | // Start codegen. |
| 3272 | CodeGenFunction CGF(CGM); |
| 3273 | CGF.CurGD = GlobalDecl(CD, Ctor_Complete); |
| 3274 | |
| 3275 | // Build FunctionArgs. |
| 3276 | FunctionArgList FunctionArgs; |
| 3277 | |
David Majnemer | 37fd66e | 2015-03-13 22:36:55 +0000 | [diff] [blame] | 3278 | // A constructor always starts with a 'this' pointer as its first argument. |
David Majnemer | dfa6d20 | 2015-03-11 18:36:39 +0000 | [diff] [blame] | 3279 | buildThisParam(CGF, FunctionArgs); |
| 3280 | |
| 3281 | // Following the 'this' pointer is a reference to the source object that we |
| 3282 | // are copying from. |
| 3283 | ImplicitParamDecl SrcParam( |
| 3284 | getContext(), nullptr, SourceLocation(), &getContext().Idents.get("src"), |
| 3285 | getContext().getLValueReferenceType(RecordTy, |
| 3286 | /*SpelledAsLValue=*/true)); |
David Majnemer | 37fd66e | 2015-03-13 22:36:55 +0000 | [diff] [blame] | 3287 | if (IsCopy) |
| 3288 | FunctionArgs.push_back(&SrcParam); |
David Majnemer | dfa6d20 | 2015-03-11 18:36:39 +0000 | [diff] [blame] | 3289 | |
David Majnemer | 37fd66e | 2015-03-13 22:36:55 +0000 | [diff] [blame] | 3290 | // Constructors for classes which utilize virtual bases have an additional |
| 3291 | // parameter which indicates whether or not it is being delegated to by a more |
| 3292 | // derived constructor. |
David Majnemer | dfa6d20 | 2015-03-11 18:36:39 +0000 | [diff] [blame] | 3293 | ImplicitParamDecl IsMostDerived(getContext(), nullptr, SourceLocation(), |
| 3294 | &getContext().Idents.get("is_most_derived"), |
| 3295 | getContext().IntTy); |
| 3296 | // Only add the parameter to the list if thie class has virtual bases. |
| 3297 | if (RD->getNumVBases() > 0) |
| 3298 | FunctionArgs.push_back(&IsMostDerived); |
| 3299 | |
| 3300 | // Start defining the function. |
| 3301 | CGF.StartFunction(GlobalDecl(), FnInfo.getReturnType(), ThunkFn, FnInfo, |
| 3302 | FunctionArgs, CD->getLocation(), SourceLocation()); |
| 3303 | EmitThisParam(CGF); |
| 3304 | llvm::Value *This = getThisValue(CGF); |
| 3305 | |
| 3306 | llvm::Value *SrcVal = |
David Majnemer | 37fd66e | 2015-03-13 22:36:55 +0000 | [diff] [blame] | 3307 | IsCopy ? CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(&SrcParam), "src") |
| 3308 | : nullptr; |
David Majnemer | dfa6d20 | 2015-03-11 18:36:39 +0000 | [diff] [blame] | 3309 | |
| 3310 | CallArgList Args; |
| 3311 | |
| 3312 | // Push the this ptr. |
| 3313 | Args.add(RValue::get(This), CD->getThisType(getContext())); |
| 3314 | |
| 3315 | // Push the src ptr. |
David Majnemer | 37fd66e | 2015-03-13 22:36:55 +0000 | [diff] [blame] | 3316 | if (SrcVal) |
| 3317 | Args.add(RValue::get(SrcVal), SrcParam.getType()); |
David Majnemer | dfa6d20 | 2015-03-11 18:36:39 +0000 | [diff] [blame] | 3318 | |
| 3319 | // Add the rest of the default arguments. |
| 3320 | std::vector<Stmt *> ArgVec; |
David Majnemer | 37fd66e | 2015-03-13 22:36:55 +0000 | [diff] [blame] | 3321 | for (unsigned I = IsCopy ? 1 : 0, E = CD->getNumParams(); I != E; ++I) |
David Majnemer | dfa6d20 | 2015-03-11 18:36:39 +0000 | [diff] [blame] | 3322 | ArgVec.push_back(getContext().getDefaultArgExprForConstructor(CD, I)); |
| 3323 | |
| 3324 | CodeGenFunction::RunCleanupsScope Cleanups(CGF); |
| 3325 | |
| 3326 | const auto *FPT = CD->getType()->castAs<FunctionProtoType>(); |
Aaron Ballman | 0c22d5a | 2015-03-12 13:49:45 +0000 | [diff] [blame] | 3327 | ConstExprIterator ArgBegin(ArgVec.data()), |
| 3328 | ArgEnd(ArgVec.data() + ArgVec.size()); |
David Majnemer | 37fd66e | 2015-03-13 22:36:55 +0000 | [diff] [blame] | 3329 | CGF.EmitCallArgs(Args, FPT, ArgBegin, ArgEnd, CD, IsCopy ? 1 : 0); |
David Majnemer | dfa6d20 | 2015-03-11 18:36:39 +0000 | [diff] [blame] | 3330 | |
| 3331 | // Insert any ABI-specific implicit constructor arguments. |
| 3332 | unsigned ExtraArgs = addImplicitConstructorArgs(CGF, CD, Ctor_Complete, |
| 3333 | /*ForVirtualBase=*/false, |
| 3334 | /*Delegating=*/false, Args); |
| 3335 | |
| 3336 | // Call the destructor with our arguments. |
| 3337 | llvm::Value *CalleeFn = CGM.getAddrOfCXXStructor(CD, StructorType::Complete); |
| 3338 | const CGFunctionInfo &CalleeInfo = CGM.getTypes().arrangeCXXConstructorCall( |
| 3339 | Args, CD, Ctor_Complete, ExtraArgs); |
| 3340 | CGF.EmitCall(CalleeInfo, CalleeFn, ReturnValueSlot(), Args, CD); |
| 3341 | |
| 3342 | Cleanups.ForceCleanup(); |
| 3343 | |
| 3344 | // Emit the ret instruction, remove any temporary instructions created for the |
| 3345 | // aid of CodeGen. |
| 3346 | CGF.FinishFunction(SourceLocation()); |
| 3347 | |
| 3348 | return ThunkFn; |
| 3349 | } |
| 3350 | |
David Majnemer | 7c23707 | 2015-03-05 00:46:22 +0000 | [diff] [blame] | 3351 | llvm::Constant *MicrosoftCXXABI::getCatchableType(QualType T, |
| 3352 | uint32_t NVOffset, |
| 3353 | int32_t VBPtrOffset, |
| 3354 | uint32_t VBIndex) { |
| 3355 | assert(!T->isReferenceType()); |
| 3356 | |
David Majnemer | e7a818f | 2015-03-06 18:53:55 +0000 | [diff] [blame] | 3357 | CXXRecordDecl *RD = T->getAsCXXRecordDecl(); |
| 3358 | const CXXConstructorDecl *CD = |
| 3359 | RD ? CGM.getContext().getCopyConstructorForExceptionObject(RD) : nullptr; |
David Majnemer | dfa6d20 | 2015-03-11 18:36:39 +0000 | [diff] [blame] | 3360 | CXXCtorType CT = Ctor_Complete; |
David Majnemer | 37fd66e | 2015-03-13 22:36:55 +0000 | [diff] [blame] | 3361 | if (CD) |
| 3362 | if (!hasDefaultCXXMethodCC(getContext(), CD) || CD->getNumParams() != 1) |
David Majnemer | dfa6d20 | 2015-03-11 18:36:39 +0000 | [diff] [blame] | 3363 | CT = Ctor_CopyingClosure; |
David Majnemer | dfa6d20 | 2015-03-11 18:36:39 +0000 | [diff] [blame] | 3364 | |
David Majnemer | 7c23707 | 2015-03-05 00:46:22 +0000 | [diff] [blame] | 3365 | uint32_t Size = getContext().getTypeSizeInChars(T).getQuantity(); |
| 3366 | SmallString<256> MangledName; |
| 3367 | { |
| 3368 | llvm::raw_svector_ostream Out(MangledName); |
David Majnemer | dfa6d20 | 2015-03-11 18:36:39 +0000 | [diff] [blame] | 3369 | getMangleContext().mangleCXXCatchableType(T, CD, CT, Size, NVOffset, |
David Majnemer | 999cbf9 | 2015-03-10 19:01:51 +0000 | [diff] [blame] | 3370 | VBPtrOffset, VBIndex, Out); |
David Majnemer | 7c23707 | 2015-03-05 00:46:22 +0000 | [diff] [blame] | 3371 | } |
| 3372 | if (llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(MangledName)) |
| 3373 | return getImageRelativeConstant(GV); |
| 3374 | |
David Majnemer | dfa6d20 | 2015-03-11 18:36:39 +0000 | [diff] [blame] | 3375 | // The TypeDescriptor is used by the runtime to determine if a catch handler |
David Majnemer | 7c23707 | 2015-03-05 00:46:22 +0000 | [diff] [blame] | 3376 | // is appropriate for the exception object. |
| 3377 | llvm::Constant *TD = getImageRelativeConstant(getAddrOfRTTIDescriptor(T)); |
| 3378 | |
| 3379 | // The runtime is responsible for calling the copy constructor if the |
| 3380 | // exception is caught by value. |
David Majnemer | dfa6d20 | 2015-03-11 18:36:39 +0000 | [diff] [blame] | 3381 | llvm::Constant *CopyCtor; |
| 3382 | if (CD) { |
| 3383 | if (CT == Ctor_CopyingClosure) |
David Majnemer | 37fd66e | 2015-03-13 22:36:55 +0000 | [diff] [blame] | 3384 | CopyCtor = getAddrOfCXXCtorClosure(CD, Ctor_CopyingClosure); |
David Majnemer | dfa6d20 | 2015-03-11 18:36:39 +0000 | [diff] [blame] | 3385 | else |
| 3386 | CopyCtor = CGM.getAddrOfCXXStructor(CD, StructorType::Complete); |
| 3387 | |
| 3388 | CopyCtor = llvm::ConstantExpr::getBitCast(CopyCtor, CGM.Int8PtrTy); |
| 3389 | } else { |
| 3390 | CopyCtor = llvm::Constant::getNullValue(CGM.Int8PtrTy); |
| 3391 | } |
David Majnemer | e7a818f | 2015-03-06 18:53:55 +0000 | [diff] [blame] | 3392 | CopyCtor = getImageRelativeConstant(CopyCtor); |
David Majnemer | 7c23707 | 2015-03-05 00:46:22 +0000 | [diff] [blame] | 3393 | |
David Majnemer | e7a818f | 2015-03-06 18:53:55 +0000 | [diff] [blame] | 3394 | bool IsScalar = !RD; |
David Majnemer | 7c23707 | 2015-03-05 00:46:22 +0000 | [diff] [blame] | 3395 | bool HasVirtualBases = false; |
| 3396 | bool IsStdBadAlloc = false; // std::bad_alloc is special for some reason. |
David Majnemer | 7c23707 | 2015-03-05 00:46:22 +0000 | [diff] [blame] | 3397 | QualType PointeeType = T; |
| 3398 | if (T->isPointerType()) |
| 3399 | PointeeType = T->getPointeeType(); |
| 3400 | if (const CXXRecordDecl *RD = PointeeType->getAsCXXRecordDecl()) { |
| 3401 | HasVirtualBases = RD->getNumVBases() > 0; |
| 3402 | if (IdentifierInfo *II = RD->getIdentifier()) |
| 3403 | IsStdBadAlloc = II->isStr("bad_alloc") && RD->isInStdNamespace(); |
| 3404 | } |
| 3405 | |
| 3406 | // Encode the relevant CatchableType properties into the Flags bitfield. |
| 3407 | // FIXME: Figure out how bits 2 or 8 can get set. |
| 3408 | uint32_t Flags = 0; |
| 3409 | if (IsScalar) |
| 3410 | Flags |= 1; |
| 3411 | if (HasVirtualBases) |
| 3412 | Flags |= 4; |
| 3413 | if (IsStdBadAlloc) |
| 3414 | Flags |= 16; |
| 3415 | |
| 3416 | llvm::Constant *Fields[] = { |
| 3417 | llvm::ConstantInt::get(CGM.IntTy, Flags), // Flags |
| 3418 | TD, // TypeDescriptor |
| 3419 | llvm::ConstantInt::get(CGM.IntTy, NVOffset), // NonVirtualAdjustment |
| 3420 | llvm::ConstantInt::get(CGM.IntTy, VBPtrOffset), // OffsetToVBPtr |
| 3421 | llvm::ConstantInt::get(CGM.IntTy, VBIndex), // VBTableIndex |
| 3422 | llvm::ConstantInt::get(CGM.IntTy, Size), // Size |
| 3423 | CopyCtor // CopyCtor |
| 3424 | }; |
| 3425 | llvm::StructType *CTType = getCatchableTypeType(); |
| 3426 | auto *GV = new llvm::GlobalVariable( |
| 3427 | CGM.getModule(), CTType, /*Constant=*/true, getLinkageForRTTI(T), |
| 3428 | llvm::ConstantStruct::get(CTType, Fields), StringRef(MangledName)); |
David Majnemer | 322fe41 | 2015-03-06 23:45:23 +0000 | [diff] [blame] | 3429 | GV->setUnnamedAddr(true); |
| 3430 | GV->setSection(".xdata"); |
David Majnemer | 7c23707 | 2015-03-05 00:46:22 +0000 | [diff] [blame] | 3431 | if (GV->isWeakForLinker()) |
| 3432 | GV->setComdat(CGM.getModule().getOrInsertComdat(GV->getName())); |
David Majnemer | 7c23707 | 2015-03-05 00:46:22 +0000 | [diff] [blame] | 3433 | return getImageRelativeConstant(GV); |
| 3434 | } |
| 3435 | |
| 3436 | llvm::GlobalVariable *MicrosoftCXXABI::getCatchableTypeArray(QualType T) { |
| 3437 | assert(!T->isReferenceType()); |
| 3438 | |
| 3439 | // See if we've already generated a CatchableTypeArray for this type before. |
| 3440 | llvm::GlobalVariable *&CTA = CatchableTypeArrays[T]; |
| 3441 | if (CTA) |
| 3442 | return CTA; |
| 3443 | |
| 3444 | // Ensure that we don't have duplicate entries in our CatchableTypeArray by |
| 3445 | // using a SmallSetVector. Duplicates may arise due to virtual bases |
| 3446 | // occurring more than once in the hierarchy. |
| 3447 | llvm::SmallSetVector<llvm::Constant *, 2> CatchableTypes; |
| 3448 | |
| 3449 | // C++14 [except.handle]p3: |
| 3450 | // A handler is a match for an exception object of type E if [...] |
| 3451 | // - the handler is of type cv T or cv T& and T is an unambiguous public |
| 3452 | // base class of E, or |
| 3453 | // - the handler is of type cv T or const T& where T is a pointer type and |
| 3454 | // E is a pointer type that can be converted to T by [...] |
| 3455 | // - a standard pointer conversion (4.10) not involving conversions to |
| 3456 | // pointers to private or protected or ambiguous classes |
| 3457 | const CXXRecordDecl *MostDerivedClass = nullptr; |
| 3458 | bool IsPointer = T->isPointerType(); |
| 3459 | if (IsPointer) |
| 3460 | MostDerivedClass = T->getPointeeType()->getAsCXXRecordDecl(); |
| 3461 | else |
| 3462 | MostDerivedClass = T->getAsCXXRecordDecl(); |
| 3463 | |
| 3464 | // Collect all the unambiguous public bases of the MostDerivedClass. |
| 3465 | if (MostDerivedClass) { |
David Majnemer | 9ced3dd | 2015-03-14 23:44:48 +0000 | [diff] [blame] | 3466 | const ASTContext &Context = getContext(); |
David Majnemer | 7c23707 | 2015-03-05 00:46:22 +0000 | [diff] [blame] | 3467 | const ASTRecordLayout &MostDerivedLayout = |
| 3468 | Context.getASTRecordLayout(MostDerivedClass); |
| 3469 | MicrosoftVTableContext &VTableContext = CGM.getMicrosoftVTableContext(); |
| 3470 | SmallVector<MSRTTIClass, 8> Classes; |
| 3471 | serializeClassHierarchy(Classes, MostDerivedClass); |
| 3472 | Classes.front().initialize(/*Parent=*/nullptr, /*Specifier=*/nullptr); |
| 3473 | detectAmbiguousBases(Classes); |
| 3474 | for (const MSRTTIClass &Class : Classes) { |
| 3475 | // Skip any ambiguous or private bases. |
| 3476 | if (Class.Flags & |
| 3477 | (MSRTTIClass::IsPrivateOnPath | MSRTTIClass::IsAmbiguous)) |
| 3478 | continue; |
| 3479 | // Write down how to convert from a derived pointer to a base pointer. |
| 3480 | uint32_t OffsetInVBTable = 0; |
| 3481 | int32_t VBPtrOffset = -1; |
| 3482 | if (Class.VirtualRoot) { |
| 3483 | OffsetInVBTable = |
| 3484 | VTableContext.getVBTableIndex(MostDerivedClass, Class.VirtualRoot)*4; |
| 3485 | VBPtrOffset = MostDerivedLayout.getVBPtrOffset().getQuantity(); |
| 3486 | } |
| 3487 | |
| 3488 | // Turn our record back into a pointer if the exception object is a |
| 3489 | // pointer. |
| 3490 | QualType RTTITy = QualType(Class.RD->getTypeForDecl(), 0); |
| 3491 | if (IsPointer) |
| 3492 | RTTITy = Context.getPointerType(RTTITy); |
| 3493 | CatchableTypes.insert(getCatchableType(RTTITy, Class.OffsetInVBase, |
| 3494 | VBPtrOffset, OffsetInVBTable)); |
| 3495 | } |
| 3496 | } |
| 3497 | |
| 3498 | // C++14 [except.handle]p3: |
| 3499 | // A handler is a match for an exception object of type E if |
| 3500 | // - The handler is of type cv T or cv T& and E and T are the same type |
| 3501 | // (ignoring the top-level cv-qualifiers) |
| 3502 | CatchableTypes.insert(getCatchableType(T)); |
| 3503 | |
| 3504 | // C++14 [except.handle]p3: |
| 3505 | // A handler is a match for an exception object of type E if |
| 3506 | // - the handler is of type cv T or const T& where T is a pointer type and |
| 3507 | // E is a pointer type that can be converted to T by [...] |
| 3508 | // - a standard pointer conversion (4.10) not involving conversions to |
| 3509 | // pointers to private or protected or ambiguous classes |
| 3510 | // |
David Majnemer | d3d7669 | 2015-03-06 23:45:20 +0000 | [diff] [blame] | 3511 | // All pointers are convertible to pointer-to-void so ensure that it is in the |
David Majnemer | 7c23707 | 2015-03-05 00:46:22 +0000 | [diff] [blame] | 3512 | // CatchableTypeArray. |
| 3513 | if (IsPointer) |
| 3514 | CatchableTypes.insert(getCatchableType(getContext().VoidPtrTy)); |
| 3515 | |
David Majnemer | a1aea9a | 2015-03-12 17:44:49 +0000 | [diff] [blame] | 3516 | // C++14 [except.handle]p3: |
| 3517 | // A handler is a match for an exception object of type E if [...] |
| 3518 | // - the handler is of type cv T or const T& where T is a pointer or |
| 3519 | // pointer to member type and E is std::nullptr_t. |
| 3520 | // |
| 3521 | // We cannot possibly list all possible pointer types here, making this |
| 3522 | // implementation incompatible with the standard. However, MSVC includes an |
| 3523 | // entry for pointer-to-void in this case. Let's do the same. |
| 3524 | if (T->isNullPtrType()) |
| 3525 | CatchableTypes.insert(getCatchableType(getContext().VoidPtrTy)); |
| 3526 | |
David Majnemer | 7c23707 | 2015-03-05 00:46:22 +0000 | [diff] [blame] | 3527 | uint32_t NumEntries = CatchableTypes.size(); |
| 3528 | llvm::Type *CTType = |
| 3529 | getImageRelativeType(getCatchableTypeType()->getPointerTo()); |
| 3530 | llvm::ArrayType *AT = llvm::ArrayType::get(CTType, NumEntries); |
| 3531 | llvm::StructType *CTAType = getCatchableTypeArrayType(NumEntries); |
| 3532 | llvm::Constant *Fields[] = { |
| 3533 | llvm::ConstantInt::get(CGM.IntTy, NumEntries), // NumEntries |
| 3534 | llvm::ConstantArray::get( |
| 3535 | AT, llvm::makeArrayRef(CatchableTypes.begin(), |
| 3536 | CatchableTypes.end())) // CatchableTypes |
| 3537 | }; |
| 3538 | SmallString<256> MangledName; |
| 3539 | { |
| 3540 | llvm::raw_svector_ostream Out(MangledName); |
| 3541 | getMangleContext().mangleCXXCatchableTypeArray(T, NumEntries, Out); |
| 3542 | } |
| 3543 | CTA = new llvm::GlobalVariable( |
| 3544 | CGM.getModule(), CTAType, /*Constant=*/true, getLinkageForRTTI(T), |
| 3545 | llvm::ConstantStruct::get(CTAType, Fields), StringRef(MangledName)); |
David Majnemer | 322fe41 | 2015-03-06 23:45:23 +0000 | [diff] [blame] | 3546 | CTA->setUnnamedAddr(true); |
| 3547 | CTA->setSection(".xdata"); |
David Majnemer | 7c23707 | 2015-03-05 00:46:22 +0000 | [diff] [blame] | 3548 | if (CTA->isWeakForLinker()) |
| 3549 | CTA->setComdat(CGM.getModule().getOrInsertComdat(CTA->getName())); |
David Majnemer | 7c23707 | 2015-03-05 00:46:22 +0000 | [diff] [blame] | 3550 | return CTA; |
| 3551 | } |
| 3552 | |
| 3553 | llvm::GlobalVariable *MicrosoftCXXABI::getThrowInfo(QualType T) { |
David Majnemer | d3d7669 | 2015-03-06 23:45:20 +0000 | [diff] [blame] | 3554 | T = getContext().getExceptionObjectType(T); |
| 3555 | |
David Majnemer | 7c23707 | 2015-03-05 00:46:22 +0000 | [diff] [blame] | 3556 | // C++14 [except.handle]p3: |
| 3557 | // A handler is a match for an exception object of type E if [...] |
| 3558 | // - the handler is of type cv T or const T& where T is a pointer type and |
| 3559 | // E is a pointer type that can be converted to T by [...] |
| 3560 | // - a qualification conversion |
| 3561 | bool IsConst = false, IsVolatile = false; |
| 3562 | QualType PointeeType = T->getPointeeType(); |
| 3563 | if (!PointeeType.isNull()) { |
| 3564 | IsConst = PointeeType.isConstQualified(); |
| 3565 | IsVolatile = PointeeType.isVolatileQualified(); |
| 3566 | } |
David Majnemer | d3d7669 | 2015-03-06 23:45:20 +0000 | [diff] [blame] | 3567 | |
| 3568 | // Member pointer types like "const int A::*" are represented by having RTTI |
| 3569 | // for "int A::*" and separately storing the const qualifier. |
| 3570 | if (const auto *MPTy = T->getAs<MemberPointerType>()) |
| 3571 | T = getContext().getMemberPointerType(PointeeType.getUnqualifiedType(), |
| 3572 | MPTy->getClass()); |
| 3573 | |
| 3574 | // Pointer types like "const int * const *" are represented by having RTTI |
| 3575 | // for "const int **" and separately storing the const qualifier. |
| 3576 | if (T->isPointerType()) |
| 3577 | T = getContext().getPointerType(PointeeType.getUnqualifiedType()); |
David Majnemer | 7c23707 | 2015-03-05 00:46:22 +0000 | [diff] [blame] | 3578 | |
| 3579 | // The CatchableTypeArray enumerates the various (CV-unqualified) types that |
| 3580 | // the exception object may be caught as. |
| 3581 | llvm::GlobalVariable *CTA = getCatchableTypeArray(T); |
| 3582 | // The first field in a CatchableTypeArray is the number of CatchableTypes. |
| 3583 | // This is used as a component of the mangled name which means that we need to |
| 3584 | // know what it is in order to see if we have previously generated the |
| 3585 | // ThrowInfo. |
| 3586 | uint32_t NumEntries = |
| 3587 | cast<llvm::ConstantInt>(CTA->getInitializer()->getAggregateElement(0U)) |
| 3588 | ->getLimitedValue(); |
| 3589 | |
| 3590 | SmallString<256> MangledName; |
| 3591 | { |
| 3592 | llvm::raw_svector_ostream Out(MangledName); |
| 3593 | getMangleContext().mangleCXXThrowInfo(T, IsConst, IsVolatile, NumEntries, |
| 3594 | Out); |
| 3595 | } |
| 3596 | |
| 3597 | // Reuse a previously generated ThrowInfo if we have generated an appropriate |
| 3598 | // one before. |
| 3599 | if (llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(MangledName)) |
| 3600 | return GV; |
| 3601 | |
| 3602 | // The RTTI TypeDescriptor uses an unqualified type but catch clauses must |
| 3603 | // be at least as CV qualified. Encode this requirement into the Flags |
| 3604 | // bitfield. |
| 3605 | uint32_t Flags = 0; |
| 3606 | if (IsConst) |
| 3607 | Flags |= 1; |
| 3608 | if (IsVolatile) |
| 3609 | Flags |= 2; |
| 3610 | |
| 3611 | // The cleanup-function (a destructor) must be called when the exception |
| 3612 | // object's lifetime ends. |
| 3613 | llvm::Constant *CleanupFn = llvm::Constant::getNullValue(CGM.Int8PtrTy); |
| 3614 | if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
| 3615 | if (CXXDestructorDecl *DtorD = RD->getDestructor()) |
| 3616 | if (!DtorD->isTrivial()) |
| 3617 | CleanupFn = llvm::ConstantExpr::getBitCast( |
| 3618 | CGM.getAddrOfCXXStructor(DtorD, StructorType::Complete), |
| 3619 | CGM.Int8PtrTy); |
| 3620 | // This is unused as far as we can tell, initialize it to null. |
| 3621 | llvm::Constant *ForwardCompat = |
| 3622 | getImageRelativeConstant(llvm::Constant::getNullValue(CGM.Int8PtrTy)); |
| 3623 | llvm::Constant *PointerToCatchableTypes = getImageRelativeConstant( |
| 3624 | llvm::ConstantExpr::getBitCast(CTA, CGM.Int8PtrTy)); |
| 3625 | llvm::StructType *TIType = getThrowInfoType(); |
| 3626 | llvm::Constant *Fields[] = { |
| 3627 | llvm::ConstantInt::get(CGM.IntTy, Flags), // Flags |
| 3628 | getImageRelativeConstant(CleanupFn), // CleanupFn |
| 3629 | ForwardCompat, // ForwardCompat |
| 3630 | PointerToCatchableTypes // CatchableTypeArray |
| 3631 | }; |
| 3632 | auto *GV = new llvm::GlobalVariable( |
| 3633 | CGM.getModule(), TIType, /*Constant=*/true, getLinkageForRTTI(T), |
| 3634 | llvm::ConstantStruct::get(TIType, Fields), StringRef(MangledName)); |
David Majnemer | 322fe41 | 2015-03-06 23:45:23 +0000 | [diff] [blame] | 3635 | GV->setUnnamedAddr(true); |
| 3636 | GV->setSection(".xdata"); |
David Majnemer | 7c23707 | 2015-03-05 00:46:22 +0000 | [diff] [blame] | 3637 | if (GV->isWeakForLinker()) |
| 3638 | GV->setComdat(CGM.getModule().getOrInsertComdat(GV->getName())); |
David Majnemer | 7c23707 | 2015-03-05 00:46:22 +0000 | [diff] [blame] | 3639 | return GV; |
| 3640 | } |
| 3641 | |
| 3642 | void MicrosoftCXXABI::emitThrow(CodeGenFunction &CGF, const CXXThrowExpr *E) { |
| 3643 | const Expr *SubExpr = E->getSubExpr(); |
| 3644 | QualType ThrowType = SubExpr->getType(); |
| 3645 | // The exception object lives on the stack and it's address is passed to the |
| 3646 | // runtime function. |
| 3647 | llvm::AllocaInst *AI = CGF.CreateMemTemp(ThrowType); |
| 3648 | CGF.EmitAnyExprToMem(SubExpr, AI, ThrowType.getQualifiers(), |
| 3649 | /*IsInit=*/true); |
| 3650 | |
| 3651 | // The so-called ThrowInfo is used to describe how the exception object may be |
| 3652 | // caught. |
| 3653 | llvm::GlobalVariable *TI = getThrowInfo(ThrowType); |
| 3654 | |
| 3655 | // Call into the runtime to throw the exception. |
| 3656 | llvm::Value *Args[] = {CGF.Builder.CreateBitCast(AI, CGM.Int8PtrTy), TI}; |
| 3657 | CGF.EmitNoreturnRuntimeCallOrInvoke(getThrowFn(), Args); |
| 3658 | } |