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