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