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