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