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