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