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