Charles Davis | c392664 | 2010-06-09 23:25:41 +0000 | [diff] [blame] | 1 | //===--- MicrosoftCXXABI.cpp - Emit LLVM Code from ASTs for a Module ------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 10 | // This provides C++ code generation targeting the Microsoft Visual C++ ABI. |
Charles Davis | c392664 | 2010-06-09 23:25:41 +0000 | [diff] [blame] | 11 | // The class in this file generates structures that follow the Microsoft |
| 12 | // Visual C++ ABI, which is actually not very well documented at all outside |
| 13 | // of Microsoft. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
| 17 | #include "CGCXXABI.h" |
| 18 | #include "CodeGenModule.h" |
Reid Kleckner | 9063302 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 19 | #include "CGVTables.h" |
| 20 | #include "MicrosoftVBTables.h" |
Charles Davis | c392664 | 2010-06-09 23:25:41 +0000 | [diff] [blame] | 21 | #include "clang/AST/Decl.h" |
| 22 | #include "clang/AST/DeclCXX.h" |
Timur Iskhodzhanov | 635de28 | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 23 | #include "clang/AST/VTableBuilder.h" |
Charles Davis | c392664 | 2010-06-09 23:25:41 +0000 | [diff] [blame] | 24 | |
| 25 | using namespace clang; |
| 26 | using namespace CodeGen; |
| 27 | |
| 28 | namespace { |
| 29 | |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 30 | class MicrosoftCXXABI : public CGCXXABI { |
Charles Davis | c392664 | 2010-06-09 23:25:41 +0000 | [diff] [blame] | 31 | public: |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 32 | MicrosoftCXXABI(CodeGenModule &CGM) : CGCXXABI(CGM) {} |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 33 | |
Stephen Lin | 3b50e8d | 2013-06-30 20:40:16 +0000 | [diff] [blame] | 34 | bool HasThisReturn(GlobalDecl GD) const; |
| 35 | |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 36 | bool isReturnTypeIndirect(const CXXRecordDecl *RD) const { |
| 37 | // Structures that are not C++03 PODs are always indirect. |
| 38 | return !RD->isPOD(); |
| 39 | } |
| 40 | |
| 41 | RecordArgABI getRecordArgABI(const CXXRecordDecl *RD) const { |
Reid Kleckner | 9b60195 | 2013-06-21 12:45:15 +0000 | [diff] [blame] | 42 | if (RD->hasNonTrivialCopyConstructor() || RD->hasNonTrivialDestructor()) |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 43 | return RAA_DirectInMemory; |
| 44 | return RAA_Default; |
| 45 | } |
| 46 | |
Joao Matos | 285baac | 2012-07-17 17:10:11 +0000 | [diff] [blame] | 47 | StringRef GetPureVirtualCallName() { return "_purecall"; } |
David Blaikie | 2eb9a95 | 2012-10-16 22:56:05 +0000 | [diff] [blame] | 48 | // No known support for deleted functions in MSVC yet, so this choice is |
| 49 | // arbitrary. |
| 50 | StringRef GetDeletedVirtualCallName() { return "_purecall"; } |
Joao Matos | 285baac | 2012-07-17 17:10:11 +0000 | [diff] [blame] | 51 | |
John McCall | ecd03b4 | 2012-09-25 10:10:39 +0000 | [diff] [blame] | 52 | llvm::Value *adjustToCompleteObject(CodeGenFunction &CGF, |
| 53 | llvm::Value *ptr, |
| 54 | QualType type); |
| 55 | |
Reid Kleckner | b0f533e | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 56 | llvm::Value *GetVirtualBaseClassOffset(CodeGenFunction &CGF, |
| 57 | llvm::Value *This, |
| 58 | const CXXRecordDecl *ClassDecl, |
| 59 | const CXXRecordDecl *BaseClassDecl); |
| 60 | |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 61 | void BuildConstructorSignature(const CXXConstructorDecl *Ctor, |
| 62 | CXXCtorType Type, |
| 63 | CanQualType &ResTy, |
John McCall | bd31574 | 2012-09-25 08:00:39 +0000 | [diff] [blame] | 64 | SmallVectorImpl<CanQualType> &ArgTys); |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 65 | |
Reid Kleckner | 9063302 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 66 | llvm::BasicBlock *EmitCtorCompleteObjectHandler(CodeGenFunction &CGF, |
| 67 | const CXXRecordDecl *RD); |
Timur Iskhodzhanov | 1d4fff5 | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 68 | |
Timur Iskhodzhanov | bb1b797 | 2013-08-04 17:30:04 +0000 | [diff] [blame] | 69 | void EmitCXXConstructors(const CXXConstructorDecl *D); |
| 70 | |
Reid Kleckner | a4130ba | 2013-07-22 13:51:44 +0000 | [diff] [blame] | 71 | // Background on MSVC destructors |
| 72 | // ============================== |
| 73 | // |
| 74 | // Both Itanium and MSVC ABIs have destructor variants. The variant names |
| 75 | // roughly correspond in the following way: |
| 76 | // Itanium Microsoft |
| 77 | // Base -> no name, just ~Class |
| 78 | // Complete -> vbase destructor |
| 79 | // Deleting -> scalar deleting destructor |
| 80 | // vector deleting destructor |
| 81 | // |
| 82 | // The base and complete destructors are the same as in Itanium, although the |
| 83 | // complete destructor does not accept a VTT parameter when there are virtual |
| 84 | // bases. A separate mechanism involving vtordisps is used to ensure that |
| 85 | // virtual methods of destroyed subobjects are not called. |
| 86 | // |
| 87 | // The deleting destructors accept an i32 bitfield as a second parameter. Bit |
| 88 | // 1 indicates if the memory should be deleted. Bit 2 indicates if the this |
| 89 | // pointer points to an array. The scalar deleting destructor assumes that |
| 90 | // bit 2 is zero, and therefore does not contain a loop. |
| 91 | // |
| 92 | // For virtual destructors, only one entry is reserved in the vftable, and it |
| 93 | // always points to the vector deleting destructor. The vector deleting |
| 94 | // destructor is the most general, so it can be used to destroy objects in |
| 95 | // place, delete single heap objects, or delete arrays. |
| 96 | // |
| 97 | // A TU defining a non-inline destructor is only guaranteed to emit a base |
| 98 | // destructor, and all of the other variants are emitted on an as-needed basis |
| 99 | // in COMDATs. Because a non-base destructor can be emitted in a TU that |
| 100 | // lacks a definition for the destructor, non-base destructors must always |
| 101 | // delegate to or alias the base destructor. |
| 102 | |
| 103 | void BuildDestructorSignature(const CXXDestructorDecl *Dtor, |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 104 | CXXDtorType Type, |
| 105 | CanQualType &ResTy, |
Timur Iskhodzhanov | 59660c2 | 2013-02-13 08:37:51 +0000 | [diff] [blame] | 106 | SmallVectorImpl<CanQualType> &ArgTys); |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 107 | |
Reid Kleckner | a4130ba | 2013-07-22 13:51:44 +0000 | [diff] [blame] | 108 | /// Non-base dtors should be emitted as delegating thunks in this ABI. |
| 109 | bool useThunkForDtorVariant(const CXXDestructorDecl *Dtor, |
| 110 | CXXDtorType DT) const { |
| 111 | return DT != Dtor_Base; |
| 112 | } |
| 113 | |
| 114 | void EmitCXXDestructors(const CXXDestructorDecl *D); |
| 115 | |
Timur Iskhodzhanov | 8f189a9 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 116 | const CXXRecordDecl *getThisArgumentTypeForMethod(const CXXMethodDecl *MD) { |
| 117 | MD = MD->getCanonicalDecl(); |
| 118 | if (MD->isVirtual() && !isa<CXXDestructorDecl>(MD)) { |
| 119 | MicrosoftVFTableContext::MethodVFTableLocation ML = |
| 120 | CGM.getVFTableContext().getMethodVFTableLocation(MD); |
| 121 | // The vbases might be ordered differently in the final overrider object |
| 122 | // and the complete object, so the "this" argument may sometimes point to |
| 123 | // memory that has no particular type (e.g. past the complete object). |
| 124 | // In this case, we just use a generic pointer type. |
| 125 | // FIXME: might want to have a more precise type in the non-virtual |
| 126 | // multiple inheritance case. |
| 127 | if (ML.VBase || !ML.VFTableOffset.isZero()) |
| 128 | return 0; |
| 129 | } |
| 130 | return MD->getParent(); |
| 131 | } |
| 132 | |
| 133 | llvm::Value *adjustThisArgumentForVirtualCall(CodeGenFunction &CGF, |
| 134 | GlobalDecl GD, |
| 135 | llvm::Value *This); |
| 136 | |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 137 | void BuildInstanceFunctionParams(CodeGenFunction &CGF, |
| 138 | QualType &ResTy, |
John McCall | bd31574 | 2012-09-25 08:00:39 +0000 | [diff] [blame] | 139 | FunctionArgList &Params); |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 140 | |
Timur Iskhodzhanov | 8f189a9 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 141 | llvm::Value *adjustThisParameterInVirtualFunctionPrologue( |
| 142 | CodeGenFunction &CGF, GlobalDecl GD, llvm::Value *This); |
| 143 | |
John McCall | bd31574 | 2012-09-25 08:00:39 +0000 | [diff] [blame] | 144 | void EmitInstanceFunctionProlog(CodeGenFunction &CGF); |
John McCall | fd70826 | 2011-01-27 02:46:02 +0000 | [diff] [blame] | 145 | |
Stephen Lin | 3b50e8d | 2013-06-30 20:40:16 +0000 | [diff] [blame] | 146 | void EmitConstructorCall(CodeGenFunction &CGF, |
| 147 | const CXXConstructorDecl *D, CXXCtorType Type, |
| 148 | bool ForVirtualBase, bool Delegating, |
Stephen Lin | 4444dbb | 2013-06-19 18:10:35 +0000 | [diff] [blame] | 149 | llvm::Value *This, |
| 150 | CallExpr::const_arg_iterator ArgBeg, |
| 151 | CallExpr::const_arg_iterator ArgEnd); |
Timur Iskhodzhanov | 8f189a9 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 152 | |
| 153 | llvm::Value *getVirtualFunctionPointer(CodeGenFunction &CGF, GlobalDecl GD, |
| 154 | llvm::Value *This, llvm::Type *Ty); |
| 155 | |
Stephen Lin | 3b50e8d | 2013-06-30 20:40:16 +0000 | [diff] [blame] | 156 | void EmitVirtualDestructorCall(CodeGenFunction &CGF, |
| 157 | const CXXDestructorDecl *Dtor, |
| 158 | CXXDtorType DtorType, SourceLocation CallLoc, |
| 159 | llvm::Value *This); |
Timur Iskhodzhanov | 0f9827f | 2013-02-15 14:45:22 +0000 | [diff] [blame] | 160 | |
Reid Kleckner | 9063302 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 161 | void EmitVirtualInheritanceTables(llvm::GlobalVariable::LinkageTypes Linkage, |
| 162 | const CXXRecordDecl *RD); |
| 163 | |
John McCall | 20bb175 | 2012-05-01 06:13:13 +0000 | [diff] [blame] | 164 | void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D, |
| 165 | llvm::GlobalVariable *DeclPtr, |
| 166 | bool PerformInit); |
| 167 | |
John McCall | fd70826 | 2011-01-27 02:46:02 +0000 | [diff] [blame] | 168 | // ==== Notes on array cookies ========= |
| 169 | // |
| 170 | // MSVC seems to only use cookies when the class has a destructor; a |
| 171 | // two-argument usual array deallocation function isn't sufficient. |
| 172 | // |
| 173 | // For example, this code prints "100" and "1": |
| 174 | // struct A { |
| 175 | // char x; |
| 176 | // void *operator new[](size_t sz) { |
| 177 | // printf("%u\n", sz); |
| 178 | // return malloc(sz); |
| 179 | // } |
| 180 | // void operator delete[](void *p, size_t sz) { |
| 181 | // printf("%u\n", sz); |
| 182 | // free(p); |
| 183 | // } |
| 184 | // }; |
| 185 | // int main() { |
| 186 | // A *p = new A[100]; |
| 187 | // delete[] p; |
| 188 | // } |
| 189 | // Whereas it prints "104" and "104" if you give A a destructor. |
John McCall | e2b45e2 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 190 | |
| 191 | bool requiresArrayCookie(const CXXDeleteExpr *expr, QualType elementType); |
| 192 | bool requiresArrayCookie(const CXXNewExpr *expr); |
| 193 | CharUnits getArrayCookieSizeImpl(QualType type); |
| 194 | llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF, |
| 195 | llvm::Value *NewPtr, |
| 196 | llvm::Value *NumElements, |
| 197 | const CXXNewExpr *expr, |
| 198 | QualType ElementType); |
| 199 | llvm::Value *readArrayCookieImpl(CodeGenFunction &CGF, |
| 200 | llvm::Value *allocPtr, |
| 201 | CharUnits cookieSize); |
Reid Kleckner | a8a0f76 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 202 | |
| 203 | private: |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 204 | llvm::Constant *getZeroInt() { |
| 205 | return llvm::ConstantInt::get(CGM.IntTy, 0); |
Reid Kleckner | a8a0f76 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 208 | llvm::Constant *getAllOnesInt() { |
| 209 | return llvm::Constant::getAllOnesValue(CGM.IntTy); |
Reid Kleckner | a8a0f76 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Reid Kleckner | f632730 | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 212 | llvm::Constant *getConstantOrZeroInt(llvm::Constant *C) { |
| 213 | return C ? C : getZeroInt(); |
| 214 | } |
| 215 | |
| 216 | llvm::Value *getValueOrZeroInt(llvm::Value *C) { |
| 217 | return C ? C : getZeroInt(); |
| 218 | } |
| 219 | |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 220 | void |
| 221 | GetNullMemberPointerFields(const MemberPointerType *MPT, |
| 222 | llvm::SmallVectorImpl<llvm::Constant *> &fields); |
| 223 | |
Reid Kleckner | b0f533e | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 224 | /// \brief Finds the offset from the base of RD to the vbptr it uses, even if |
| 225 | /// it is reusing a vbptr from a non-virtual base. RD must have morally |
| 226 | /// virtual bases. |
| 227 | CharUnits GetVBPtrOffsetFromBases(const CXXRecordDecl *RD); |
| 228 | |
| 229 | /// \brief Shared code for virtual base adjustment. Returns the offset from |
| 230 | /// the vbptr to the virtual base. Optionally returns the address of the |
| 231 | /// vbptr itself. |
| 232 | llvm::Value *GetVBaseOffsetFromVBPtr(CodeGenFunction &CGF, |
| 233 | llvm::Value *Base, |
| 234 | llvm::Value *VBPtrOffset, |
| 235 | llvm::Value *VBTableOffset, |
| 236 | llvm::Value **VBPtr = 0); |
| 237 | |
| 238 | /// \brief Performs a full virtual base adjustment. Used to dereference |
| 239 | /// pointers to members of virtual bases. |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 240 | llvm::Value *AdjustVirtualBase(CodeGenFunction &CGF, const CXXRecordDecl *RD, |
| 241 | llvm::Value *Base, |
| 242 | llvm::Value *VirtualBaseAdjustmentOffset, |
| 243 | llvm::Value *VBPtrOffset /* optional */); |
| 244 | |
Reid Kleckner | 79e0291 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 245 | /// \brief Emits a full member pointer with the fields common to data and |
| 246 | /// function member pointers. |
| 247 | llvm::Constant *EmitFullMemberPointer(llvm::Constant *FirstField, |
| 248 | bool IsMemberFunction, |
Reid Kleckner | f632730 | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 249 | const CXXRecordDecl *RD, |
| 250 | CharUnits NonVirtualBaseAdjustment); |
| 251 | |
| 252 | llvm::Constant *BuildMemberPointer(const CXXRecordDecl *RD, |
| 253 | const CXXMethodDecl *MD, |
| 254 | CharUnits NonVirtualBaseAdjustment); |
| 255 | |
| 256 | bool MemberPointerConstantIsNull(const MemberPointerType *MPT, |
| 257 | llvm::Constant *MP); |
Reid Kleckner | 79e0291 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 258 | |
Reid Kleckner | 9063302 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 259 | /// \brief - Initialize all vbptrs of 'this' with RD as the complete type. |
| 260 | void EmitVBPtrStores(CodeGenFunction &CGF, const CXXRecordDecl *RD); |
| 261 | |
| 262 | /// \brief Caching wrapper around VBTableBuilder::enumerateVBTables(). |
| 263 | const VBTableVector &EnumerateVBTables(const CXXRecordDecl *RD); |
| 264 | |
Reid Kleckner | a8a0f76 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 265 | public: |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 266 | virtual llvm::Type *ConvertMemberPointerType(const MemberPointerType *MPT); |
| 267 | |
| 268 | virtual bool isZeroInitializable(const MemberPointerType *MPT); |
| 269 | |
Reid Kleckner | a8a0f76 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 270 | virtual llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT); |
| 271 | |
| 272 | virtual llvm::Constant *EmitMemberDataPointer(const MemberPointerType *MPT, |
| 273 | CharUnits offset); |
Reid Kleckner | 79e0291 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 274 | virtual llvm::Constant *EmitMemberPointer(const CXXMethodDecl *MD); |
| 275 | virtual llvm::Constant *EmitMemberPointer(const APValue &MP, QualType MPT); |
Reid Kleckner | a8a0f76 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 276 | |
Reid Kleckner | 3d2f000 | 2013-04-30 20:15:14 +0000 | [diff] [blame] | 277 | virtual llvm::Value *EmitMemberPointerComparison(CodeGenFunction &CGF, |
| 278 | llvm::Value *L, |
| 279 | llvm::Value *R, |
| 280 | const MemberPointerType *MPT, |
| 281 | bool Inequality); |
| 282 | |
Reid Kleckner | a8a0f76 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 283 | virtual llvm::Value *EmitMemberPointerIsNotNull(CodeGenFunction &CGF, |
| 284 | llvm::Value *MemPtr, |
| 285 | const MemberPointerType *MPT); |
| 286 | |
| 287 | virtual llvm::Value *EmitMemberDataPointerAddress(CodeGenFunction &CGF, |
| 288 | llvm::Value *Base, |
| 289 | llvm::Value *MemPtr, |
| 290 | const MemberPointerType *MPT); |
| 291 | |
Reid Kleckner | f632730 | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 292 | virtual llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF, |
| 293 | const CastExpr *E, |
| 294 | llvm::Value *Src); |
| 295 | |
| 296 | virtual llvm::Constant *EmitMemberPointerConversion(const CastExpr *E, |
| 297 | llvm::Constant *Src); |
| 298 | |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 299 | virtual llvm::Value * |
| 300 | EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF, |
| 301 | llvm::Value *&This, |
| 302 | llvm::Value *MemPtr, |
| 303 | const MemberPointerType *MPT); |
| 304 | |
Reid Kleckner | 9063302 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 305 | private: |
| 306 | /// VBTables - All the vbtables which have been referenced. |
| 307 | llvm::DenseMap<const CXXRecordDecl *, VBTableVector> VBTablesMap; |
Charles Davis | c392664 | 2010-06-09 23:25:41 +0000 | [diff] [blame] | 308 | }; |
| 309 | |
| 310 | } |
| 311 | |
John McCall | ecd03b4 | 2012-09-25 10:10:39 +0000 | [diff] [blame] | 312 | llvm::Value *MicrosoftCXXABI::adjustToCompleteObject(CodeGenFunction &CGF, |
| 313 | llvm::Value *ptr, |
| 314 | QualType type) { |
| 315 | // FIXME: implement |
| 316 | return ptr; |
| 317 | } |
| 318 | |
Reid Kleckner | 8c47432 | 2013-06-04 21:32:29 +0000 | [diff] [blame] | 319 | /// \brief Finds the first non-virtual base of RD that has virtual bases. If RD |
| 320 | /// doesn't have a vbptr, it will reuse the vbptr of the returned class. |
| 321 | static const CXXRecordDecl *FindFirstNVBaseWithVBases(const CXXRecordDecl *RD) { |
| 322 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 323 | E = RD->bases_end(); I != E; ++I) { |
| 324 | const CXXRecordDecl *Base = I->getType()->getAsCXXRecordDecl(); |
| 325 | if (!I->isVirtual() && Base->getNumVBases() > 0) |
| 326 | return Base; |
| 327 | } |
| 328 | llvm_unreachable("RD must have an nv base with vbases"); |
| 329 | } |
| 330 | |
Reid Kleckner | b0f533e | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 331 | CharUnits MicrosoftCXXABI::GetVBPtrOffsetFromBases(const CXXRecordDecl *RD) { |
| 332 | assert(RD->getNumVBases()); |
| 333 | CharUnits Total = CharUnits::Zero(); |
| 334 | while (RD) { |
| 335 | const ASTRecordLayout &RDLayout = getContext().getASTRecordLayout(RD); |
| 336 | CharUnits VBPtrOffset = RDLayout.getVBPtrOffset(); |
| 337 | // -1 is the sentinel for no vbptr. |
| 338 | if (VBPtrOffset != CharUnits::fromQuantity(-1)) { |
| 339 | Total += VBPtrOffset; |
| 340 | break; |
| 341 | } |
Reid Kleckner | 8c47432 | 2013-06-04 21:32:29 +0000 | [diff] [blame] | 342 | RD = FindFirstNVBaseWithVBases(RD); |
| 343 | Total += RDLayout.getBaseClassOffset(RD); |
Reid Kleckner | b0f533e | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 344 | } |
| 345 | return Total; |
| 346 | } |
| 347 | |
| 348 | llvm::Value * |
| 349 | MicrosoftCXXABI::GetVirtualBaseClassOffset(CodeGenFunction &CGF, |
| 350 | llvm::Value *This, |
| 351 | const CXXRecordDecl *ClassDecl, |
| 352 | const CXXRecordDecl *BaseClassDecl) { |
| 353 | int64_t VBPtrChars = GetVBPtrOffsetFromBases(ClassDecl).getQuantity(); |
| 354 | llvm::Value *VBPtrOffset = llvm::ConstantInt::get(CGM.PtrDiffTy, VBPtrChars); |
Reid Kleckner | b0f533e | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 355 | CharUnits IntSize = getContext().getTypeSizeInChars(getContext().IntTy); |
Reid Kleckner | 8c47432 | 2013-06-04 21:32:29 +0000 | [diff] [blame] | 356 | CharUnits VBTableChars = IntSize * GetVBTableIndex(ClassDecl, BaseClassDecl); |
Reid Kleckner | b0f533e | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 357 | llvm::Value *VBTableOffset = |
| 358 | llvm::ConstantInt::get(CGM.IntTy, VBTableChars.getQuantity()); |
| 359 | |
| 360 | llvm::Value *VBPtrToNewBase = |
| 361 | GetVBaseOffsetFromVBPtr(CGF, This, VBTableOffset, VBPtrOffset); |
| 362 | VBPtrToNewBase = |
| 363 | CGF.Builder.CreateSExtOrBitCast(VBPtrToNewBase, CGM.PtrDiffTy); |
| 364 | return CGF.Builder.CreateNSWAdd(VBPtrOffset, VBPtrToNewBase); |
| 365 | } |
| 366 | |
Stephen Lin | 3b50e8d | 2013-06-30 20:40:16 +0000 | [diff] [blame] | 367 | bool MicrosoftCXXABI::HasThisReturn(GlobalDecl GD) const { |
| 368 | return isa<CXXConstructorDecl>(GD.getDecl()); |
John McCall | bd31574 | 2012-09-25 08:00:39 +0000 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | void MicrosoftCXXABI::BuildConstructorSignature(const CXXConstructorDecl *Ctor, |
| 372 | CXXCtorType Type, |
| 373 | CanQualType &ResTy, |
| 374 | SmallVectorImpl<CanQualType> &ArgTys) { |
Stephen Lin | 3b50e8d | 2013-06-30 20:40:16 +0000 | [diff] [blame] | 375 | // 'this' parameter and 'this' return are already in place |
Timur Iskhodzhanov | 1d4fff5 | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 376 | |
| 377 | const CXXRecordDecl *Class = Ctor->getParent(); |
| 378 | if (Class->getNumVBases()) { |
| 379 | // Constructors of classes with virtual bases take an implicit parameter. |
| 380 | ArgTys.push_back(CGM.getContext().IntTy); |
| 381 | } |
| 382 | } |
| 383 | |
Reid Kleckner | 9063302 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 384 | llvm::BasicBlock * |
| 385 | MicrosoftCXXABI::EmitCtorCompleteObjectHandler(CodeGenFunction &CGF, |
| 386 | const CXXRecordDecl *RD) { |
Timur Iskhodzhanov | 1d4fff5 | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 387 | llvm::Value *IsMostDerivedClass = getStructorImplicitParamValue(CGF); |
| 388 | assert(IsMostDerivedClass && |
| 389 | "ctor for a class with virtual bases must have an implicit parameter"); |
Reid Kleckner | 9063302 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 390 | llvm::Value *IsCompleteObject = |
| 391 | CGF.Builder.CreateIsNotNull(IsMostDerivedClass, "is_complete_object"); |
Timur Iskhodzhanov | 1d4fff5 | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 392 | |
| 393 | llvm::BasicBlock *CallVbaseCtorsBB = CGF.createBasicBlock("ctor.init_vbases"); |
| 394 | llvm::BasicBlock *SkipVbaseCtorsBB = CGF.createBasicBlock("ctor.skip_vbases"); |
| 395 | CGF.Builder.CreateCondBr(IsCompleteObject, |
| 396 | CallVbaseCtorsBB, SkipVbaseCtorsBB); |
| 397 | |
| 398 | CGF.EmitBlock(CallVbaseCtorsBB); |
Reid Kleckner | 9063302 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 399 | |
| 400 | // Fill in the vbtable pointers here. |
| 401 | EmitVBPtrStores(CGF, RD); |
Timur Iskhodzhanov | 1d4fff5 | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 402 | |
| 403 | // CGF will put the base ctor calls in this basic block for us later. |
| 404 | |
| 405 | return SkipVbaseCtorsBB; |
John McCall | bd31574 | 2012-09-25 08:00:39 +0000 | [diff] [blame] | 406 | } |
| 407 | |
Timur Iskhodzhanov | bb1b797 | 2013-08-04 17:30:04 +0000 | [diff] [blame] | 408 | void MicrosoftCXXABI::EmitCXXConstructors(const CXXConstructorDecl *D) { |
| 409 | // There's only one constructor type in this ABI. |
| 410 | CGM.EmitGlobal(GlobalDecl(D, Ctor_Complete)); |
| 411 | } |
| 412 | |
Reid Kleckner | 9063302 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 413 | void MicrosoftCXXABI::EmitVBPtrStores(CodeGenFunction &CGF, |
| 414 | const CXXRecordDecl *RD) { |
| 415 | llvm::Value *ThisInt8Ptr = |
| 416 | CGF.Builder.CreateBitCast(getThisValue(CGF), CGM.Int8PtrTy, "this.int8"); |
| 417 | |
| 418 | const VBTableVector &VBTables = EnumerateVBTables(RD); |
| 419 | for (VBTableVector::const_iterator I = VBTables.begin(), E = VBTables.end(); |
| 420 | I != E; ++I) { |
| 421 | const ASTRecordLayout &SubobjectLayout = |
| 422 | CGM.getContext().getASTRecordLayout(I->VBPtrSubobject.getBase()); |
| 423 | uint64_t Offs = (I->VBPtrSubobject.getBaseOffset() + |
| 424 | SubobjectLayout.getVBPtrOffset()).getQuantity(); |
| 425 | llvm::Value *VBPtr = |
| 426 | CGF.Builder.CreateConstInBoundsGEP1_64(ThisInt8Ptr, Offs); |
| 427 | VBPtr = CGF.Builder.CreateBitCast(VBPtr, I->GV->getType()->getPointerTo(0), |
| 428 | "vbptr." + I->ReusingBase->getName()); |
| 429 | CGF.Builder.CreateStore(I->GV, VBPtr); |
| 430 | } |
| 431 | } |
| 432 | |
Timur Iskhodzhanov | 59660c2 | 2013-02-13 08:37:51 +0000 | [diff] [blame] | 433 | void MicrosoftCXXABI::BuildDestructorSignature(const CXXDestructorDecl *Dtor, |
| 434 | CXXDtorType Type, |
| 435 | CanQualType &ResTy, |
| 436 | SmallVectorImpl<CanQualType> &ArgTys) { |
| 437 | // 'this' is already in place |
Stephen Lin | 3b50e8d | 2013-06-30 20:40:16 +0000 | [diff] [blame] | 438 | |
Timur Iskhodzhanov | 59660c2 | 2013-02-13 08:37:51 +0000 | [diff] [blame] | 439 | // TODO: 'for base' flag |
| 440 | |
| 441 | if (Type == Dtor_Deleting) { |
Timur Iskhodzhanov | 1f71f39 | 2013-08-27 10:38:19 +0000 | [diff] [blame^] | 442 | // The scalar deleting destructor takes an implicit int parameter. |
| 443 | ArgTys.push_back(CGM.getContext().IntTy); |
Timur Iskhodzhanov | 59660c2 | 2013-02-13 08:37:51 +0000 | [diff] [blame] | 444 | } |
| 445 | } |
| 446 | |
Reid Kleckner | a4130ba | 2013-07-22 13:51:44 +0000 | [diff] [blame] | 447 | void MicrosoftCXXABI::EmitCXXDestructors(const CXXDestructorDecl *D) { |
| 448 | // The TU defining a dtor is only guaranteed to emit a base destructor. All |
| 449 | // other destructor variants are delegating thunks. |
| 450 | CGM.EmitGlobal(GlobalDecl(D, Dtor_Base)); |
| 451 | } |
| 452 | |
Timur Iskhodzhanov | 8f189a9 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 453 | llvm::Value *MicrosoftCXXABI::adjustThisArgumentForVirtualCall( |
| 454 | CodeGenFunction &CGF, GlobalDecl GD, llvm::Value *This) { |
| 455 | GD = GD.getCanonicalDecl(); |
| 456 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
| 457 | if (isa<CXXDestructorDecl>(MD)) |
| 458 | return This; |
| 459 | |
| 460 | MicrosoftVFTableContext::MethodVFTableLocation ML = |
| 461 | CGM.getVFTableContext().getMethodVFTableLocation(GD); |
| 462 | |
| 463 | unsigned AS = cast<llvm::PointerType>(This->getType())->getAddressSpace(); |
| 464 | llvm::Type *charPtrTy = CGF.Int8Ty->getPointerTo(AS); |
| 465 | if (ML.VBase) { |
| 466 | This = CGF.Builder.CreateBitCast(This, charPtrTy); |
| 467 | llvm::Value *VBaseOffset = CGM.getCXXABI() |
| 468 | .GetVirtualBaseClassOffset(CGF, This, MD->getParent(), ML.VBase); |
| 469 | This = CGF.Builder.CreateInBoundsGEP(This, VBaseOffset); |
| 470 | } |
| 471 | CharUnits StaticOffset = ML.VFTableOffset; |
| 472 | if (!StaticOffset.isZero()) { |
| 473 | assert(StaticOffset.isPositive()); |
| 474 | This = CGF.Builder.CreateBitCast(This, charPtrTy); |
| 475 | This = CGF.Builder |
| 476 | .CreateConstInBoundsGEP1_64(This, StaticOffset.getQuantity()); |
| 477 | } |
| 478 | return This; |
| 479 | } |
| 480 | |
Timur Iskhodzhanov | 59660c2 | 2013-02-13 08:37:51 +0000 | [diff] [blame] | 481 | static bool IsDeletingDtor(GlobalDecl GD) { |
| 482 | const CXXMethodDecl* MD = cast<CXXMethodDecl>(GD.getDecl()); |
| 483 | if (isa<CXXDestructorDecl>(MD)) { |
| 484 | return GD.getDtorType() == Dtor_Deleting; |
| 485 | } |
| 486 | return false; |
| 487 | } |
| 488 | |
John McCall | bd31574 | 2012-09-25 08:00:39 +0000 | [diff] [blame] | 489 | void MicrosoftCXXABI::BuildInstanceFunctionParams(CodeGenFunction &CGF, |
| 490 | QualType &ResTy, |
| 491 | FunctionArgList &Params) { |
| 492 | BuildThisParam(CGF, Params); |
Timur Iskhodzhanov | 59660c2 | 2013-02-13 08:37:51 +0000 | [diff] [blame] | 493 | |
Timur Iskhodzhanov | 1d4fff5 | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 494 | ASTContext &Context = getContext(); |
| 495 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(CGF.CurGD.getDecl()); |
| 496 | if (isa<CXXConstructorDecl>(MD) && MD->getParent()->getNumVBases()) { |
| 497 | ImplicitParamDecl *IsMostDerived |
| 498 | = ImplicitParamDecl::Create(Context, 0, |
| 499 | CGF.CurGD.getDecl()->getLocation(), |
| 500 | &Context.Idents.get("is_most_derived"), |
| 501 | Context.IntTy); |
| 502 | Params.push_back(IsMostDerived); |
| 503 | getStructorImplicitParamDecl(CGF) = IsMostDerived; |
| 504 | } else if (IsDeletingDtor(CGF.CurGD)) { |
Timur Iskhodzhanov | 59660c2 | 2013-02-13 08:37:51 +0000 | [diff] [blame] | 505 | ImplicitParamDecl *ShouldDelete |
| 506 | = ImplicitParamDecl::Create(Context, 0, |
| 507 | CGF.CurGD.getDecl()->getLocation(), |
| 508 | &Context.Idents.get("should_call_delete"), |
Timur Iskhodzhanov | 1f71f39 | 2013-08-27 10:38:19 +0000 | [diff] [blame^] | 509 | Context.IntTy); |
Timur Iskhodzhanov | 59660c2 | 2013-02-13 08:37:51 +0000 | [diff] [blame] | 510 | Params.push_back(ShouldDelete); |
| 511 | getStructorImplicitParamDecl(CGF) = ShouldDelete; |
| 512 | } |
John McCall | bd31574 | 2012-09-25 08:00:39 +0000 | [diff] [blame] | 513 | } |
| 514 | |
Timur Iskhodzhanov | 8f189a9 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 515 | llvm::Value *MicrosoftCXXABI::adjustThisParameterInVirtualFunctionPrologue( |
| 516 | CodeGenFunction &CGF, GlobalDecl GD, llvm::Value *This) { |
| 517 | GD = GD.getCanonicalDecl(); |
| 518 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
| 519 | if (isa<CXXDestructorDecl>(MD)) |
| 520 | return This; |
| 521 | |
| 522 | // In this ABI, every virtual function takes a pointer to one of the |
| 523 | // subobjects that first defines it as the 'this' parameter, rather than a |
| 524 | // pointer to ther final overrider subobject. Thus, we need to adjust it back |
| 525 | // to the final overrider subobject before use. |
| 526 | // See comments in the MicrosoftVFTableContext implementation for the details. |
| 527 | |
| 528 | MicrosoftVFTableContext::MethodVFTableLocation ML = |
| 529 | CGM.getVFTableContext().getMethodVFTableLocation(GD); |
| 530 | CharUnits Adjustment = ML.VFTableOffset; |
| 531 | if (ML.VBase) { |
| 532 | const ASTRecordLayout &DerivedLayout = |
| 533 | CGF.getContext().getASTRecordLayout(MD->getParent()); |
| 534 | Adjustment += DerivedLayout.getVBaseClassOffset(ML.VBase); |
| 535 | } |
| 536 | |
| 537 | if (Adjustment.isZero()) |
| 538 | return This; |
| 539 | |
| 540 | unsigned AS = cast<llvm::PointerType>(This->getType())->getAddressSpace(); |
| 541 | llvm::Type *charPtrTy = CGF.Int8Ty->getPointerTo(AS), |
| 542 | *thisTy = This->getType(); |
| 543 | |
| 544 | This = CGF.Builder.CreateBitCast(This, charPtrTy); |
| 545 | assert(Adjustment.isPositive()); |
| 546 | This = CGF.Builder.CreateConstGEP1_64(This, -Adjustment.getQuantity()); |
| 547 | return CGF.Builder.CreateBitCast(This, thisTy); |
| 548 | } |
| 549 | |
John McCall | bd31574 | 2012-09-25 08:00:39 +0000 | [diff] [blame] | 550 | void MicrosoftCXXABI::EmitInstanceFunctionProlog(CodeGenFunction &CGF) { |
| 551 | EmitThisParam(CGF); |
Stephen Lin | 3b50e8d | 2013-06-30 20:40:16 +0000 | [diff] [blame] | 552 | |
| 553 | /// If this is a function that the ABI specifies returns 'this', initialize |
| 554 | /// the return slot to 'this' at the start of the function. |
| 555 | /// |
| 556 | /// Unlike the setting of return types, this is done within the ABI |
| 557 | /// implementation instead of by clients of CGCXXABI because: |
| 558 | /// 1) getThisValue is currently protected |
| 559 | /// 2) in theory, an ABI could implement 'this' returns some other way; |
| 560 | /// HasThisReturn only specifies a contract, not the implementation |
| 561 | if (HasThisReturn(CGF.CurGD)) |
John McCall | bd31574 | 2012-09-25 08:00:39 +0000 | [diff] [blame] | 562 | CGF.Builder.CreateStore(getThisValue(CGF), CGF.ReturnValue); |
Timur Iskhodzhanov | 1d4fff5 | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 563 | |
| 564 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(CGF.CurGD.getDecl()); |
| 565 | if (isa<CXXConstructorDecl>(MD) && MD->getParent()->getNumVBases()) { |
| 566 | assert(getStructorImplicitParamDecl(CGF) && |
| 567 | "no implicit parameter for a constructor with virtual bases?"); |
| 568 | getStructorImplicitParamValue(CGF) |
| 569 | = CGF.Builder.CreateLoad( |
| 570 | CGF.GetAddrOfLocalVar(getStructorImplicitParamDecl(CGF)), |
| 571 | "is_most_derived"); |
| 572 | } |
| 573 | |
Timur Iskhodzhanov | 59660c2 | 2013-02-13 08:37:51 +0000 | [diff] [blame] | 574 | if (IsDeletingDtor(CGF.CurGD)) { |
| 575 | assert(getStructorImplicitParamDecl(CGF) && |
| 576 | "no implicit parameter for a deleting destructor?"); |
| 577 | getStructorImplicitParamValue(CGF) |
| 578 | = CGF.Builder.CreateLoad( |
| 579 | CGF.GetAddrOfLocalVar(getStructorImplicitParamDecl(CGF)), |
| 580 | "should_call_delete"); |
| 581 | } |
John McCall | bd31574 | 2012-09-25 08:00:39 +0000 | [diff] [blame] | 582 | } |
| 583 | |
Stephen Lin | 3b50e8d | 2013-06-30 20:40:16 +0000 | [diff] [blame] | 584 | void MicrosoftCXXABI::EmitConstructorCall(CodeGenFunction &CGF, |
Stephen Lin | 4444dbb | 2013-06-19 18:10:35 +0000 | [diff] [blame] | 585 | const CXXConstructorDecl *D, |
Stephen Lin | 3b50e8d | 2013-06-30 20:40:16 +0000 | [diff] [blame] | 586 | CXXCtorType Type, |
| 587 | bool ForVirtualBase, |
Stephen Lin | 4444dbb | 2013-06-19 18:10:35 +0000 | [diff] [blame] | 588 | bool Delegating, |
| 589 | llvm::Value *This, |
Timur Iskhodzhanov | 1d4fff5 | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 590 | CallExpr::const_arg_iterator ArgBeg, |
| 591 | CallExpr::const_arg_iterator ArgEnd) { |
| 592 | assert(Type == Ctor_Complete || Type == Ctor_Base); |
| 593 | llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(D, Ctor_Complete); |
| 594 | |
| 595 | llvm::Value *ImplicitParam = 0; |
| 596 | QualType ImplicitParamTy; |
| 597 | if (D->getParent()->getNumVBases()) { |
| 598 | ImplicitParam = llvm::ConstantInt::get(CGM.Int32Ty, Type == Ctor_Complete); |
| 599 | ImplicitParamTy = getContext().IntTy; |
| 600 | } |
| 601 | |
| 602 | // FIXME: Provide a source location here. |
Stephen Lin | 4444dbb | 2013-06-19 18:10:35 +0000 | [diff] [blame] | 603 | CGF.EmitCXXMemberCall(D, SourceLocation(), Callee, ReturnValueSlot(), This, |
Stephen Lin | 3b50e8d | 2013-06-30 20:40:16 +0000 | [diff] [blame] | 604 | ImplicitParam, ImplicitParamTy, ArgBeg, ArgEnd); |
Timur Iskhodzhanov | 1d4fff5 | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 605 | } |
| 606 | |
Timur Iskhodzhanov | 8f189a9 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 607 | llvm::Value *MicrosoftCXXABI::getVirtualFunctionPointer(CodeGenFunction &CGF, |
| 608 | GlobalDecl GD, |
| 609 | llvm::Value *This, |
| 610 | llvm::Type *Ty) { |
| 611 | GD = GD.getCanonicalDecl(); |
| 612 | CGBuilderTy &Builder = CGF.Builder; |
| 613 | |
| 614 | Ty = Ty->getPointerTo()->getPointerTo(); |
| 615 | llvm::Value *VPtr = adjustThisArgumentForVirtualCall(CGF, GD, This); |
| 616 | llvm::Value *VTable = CGF.GetVTablePtr(VPtr, Ty); |
| 617 | |
| 618 | MicrosoftVFTableContext::MethodVFTableLocation ML = |
| 619 | CGM.getVFTableContext().getMethodVFTableLocation(GD); |
| 620 | llvm::Value *VFuncPtr = |
| 621 | Builder.CreateConstInBoundsGEP1_64(VTable, ML.Index, "vfn"); |
| 622 | return Builder.CreateLoad(VFuncPtr); |
| 623 | } |
| 624 | |
Stephen Lin | 3b50e8d | 2013-06-30 20:40:16 +0000 | [diff] [blame] | 625 | void MicrosoftCXXABI::EmitVirtualDestructorCall(CodeGenFunction &CGF, |
| 626 | const CXXDestructorDecl *Dtor, |
| 627 | CXXDtorType DtorType, |
| 628 | SourceLocation CallLoc, |
| 629 | llvm::Value *This) { |
Timur Iskhodzhanov | 0f9827f | 2013-02-15 14:45:22 +0000 | [diff] [blame] | 630 | assert(DtorType == Dtor_Deleting || DtorType == Dtor_Complete); |
| 631 | |
| 632 | // We have only one destructor in the vftable but can get both behaviors |
Timur Iskhodzhanov | 1f71f39 | 2013-08-27 10:38:19 +0000 | [diff] [blame^] | 633 | // by passing an implicit int parameter. |
Timur Iskhodzhanov | 8f189a9 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 634 | const CGFunctionInfo *FInfo = |
| 635 | &CGM.getTypes().arrangeCXXDestructor(Dtor, Dtor_Deleting); |
Timur Iskhodzhanov | 0f9827f | 2013-02-15 14:45:22 +0000 | [diff] [blame] | 636 | llvm::Type *Ty = CGF.CGM.getTypes().GetFunctionType(*FInfo); |
Timur Iskhodzhanov | 8f189a9 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 637 | llvm::Value *Callee = |
| 638 | getVirtualFunctionPointer(CGF, GlobalDecl(Dtor, Dtor_Deleting), This, Ty); |
Timur Iskhodzhanov | 0f9827f | 2013-02-15 14:45:22 +0000 | [diff] [blame] | 639 | |
| 640 | ASTContext &Context = CGF.getContext(); |
Timur Iskhodzhanov | 8f189a9 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 641 | llvm::Value *ImplicitParam = |
Timur Iskhodzhanov | 1f71f39 | 2013-08-27 10:38:19 +0000 | [diff] [blame^] | 642 | llvm::ConstantInt::get(llvm::IntegerType::getInt32Ty(CGF.getLLVMContext()), |
Timur Iskhodzhanov | 0f9827f | 2013-02-15 14:45:22 +0000 | [diff] [blame] | 643 | DtorType == Dtor_Deleting); |
| 644 | |
Stephen Lin | 3b50e8d | 2013-06-30 20:40:16 +0000 | [diff] [blame] | 645 | CGF.EmitCXXMemberCall(Dtor, CallLoc, Callee, ReturnValueSlot(), This, |
Timur Iskhodzhanov | 1f71f39 | 2013-08-27 10:38:19 +0000 | [diff] [blame^] | 646 | ImplicitParam, Context.IntTy, 0, 0); |
Timur Iskhodzhanov | 0f9827f | 2013-02-15 14:45:22 +0000 | [diff] [blame] | 647 | } |
| 648 | |
Reid Kleckner | 9063302 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 649 | const VBTableVector & |
| 650 | MicrosoftCXXABI::EnumerateVBTables(const CXXRecordDecl *RD) { |
| 651 | // At this layer, we can key the cache off of a single class, which is much |
| 652 | // easier than caching at the GlobalVariable layer. |
| 653 | llvm::DenseMap<const CXXRecordDecl*, VBTableVector>::iterator I; |
| 654 | bool added; |
| 655 | llvm::tie(I, added) = VBTablesMap.insert(std::make_pair(RD, VBTableVector())); |
| 656 | VBTableVector &VBTables = I->second; |
| 657 | if (!added) |
| 658 | return VBTables; |
| 659 | |
| 660 | VBTableBuilder(CGM, RD).enumerateVBTables(VBTables); |
| 661 | |
| 662 | return VBTables; |
| 663 | } |
| 664 | |
| 665 | void MicrosoftCXXABI::EmitVirtualInheritanceTables( |
| 666 | llvm::GlobalVariable::LinkageTypes Linkage, const CXXRecordDecl *RD) { |
| 667 | const VBTableVector &VBTables = EnumerateVBTables(RD); |
| 668 | for (VBTableVector::const_iterator I = VBTables.begin(), E = VBTables.end(); |
| 669 | I != E; ++I) { |
| 670 | I->EmitVBTableDefinition(CGM, RD, Linkage); |
| 671 | } |
| 672 | } |
| 673 | |
John McCall | e2b45e2 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 674 | bool MicrosoftCXXABI::requiresArrayCookie(const CXXDeleteExpr *expr, |
| 675 | QualType elementType) { |
| 676 | // Microsoft seems to completely ignore the possibility of a |
| 677 | // two-argument usual deallocation function. |
| 678 | return elementType.isDestructedType(); |
| 679 | } |
| 680 | |
| 681 | bool MicrosoftCXXABI::requiresArrayCookie(const CXXNewExpr *expr) { |
| 682 | // Microsoft seems to completely ignore the possibility of a |
| 683 | // two-argument usual deallocation function. |
| 684 | return expr->getAllocatedType().isDestructedType(); |
| 685 | } |
| 686 | |
| 687 | CharUnits MicrosoftCXXABI::getArrayCookieSizeImpl(QualType type) { |
| 688 | // The array cookie is always a size_t; we then pad that out to the |
| 689 | // alignment of the element type. |
| 690 | ASTContext &Ctx = getContext(); |
| 691 | return std::max(Ctx.getTypeSizeInChars(Ctx.getSizeType()), |
| 692 | Ctx.getTypeAlignInChars(type)); |
| 693 | } |
| 694 | |
| 695 | llvm::Value *MicrosoftCXXABI::readArrayCookieImpl(CodeGenFunction &CGF, |
| 696 | llvm::Value *allocPtr, |
| 697 | CharUnits cookieSize) { |
Micah Villmow | 956a5a1 | 2012-10-25 15:39:14 +0000 | [diff] [blame] | 698 | unsigned AS = allocPtr->getType()->getPointerAddressSpace(); |
John McCall | e2b45e2 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 699 | llvm::Value *numElementsPtr = |
| 700 | CGF.Builder.CreateBitCast(allocPtr, CGF.SizeTy->getPointerTo(AS)); |
| 701 | return CGF.Builder.CreateLoad(numElementsPtr); |
| 702 | } |
| 703 | |
| 704 | llvm::Value* MicrosoftCXXABI::InitializeArrayCookie(CodeGenFunction &CGF, |
| 705 | llvm::Value *newPtr, |
| 706 | llvm::Value *numElements, |
| 707 | const CXXNewExpr *expr, |
| 708 | QualType elementType) { |
| 709 | assert(requiresArrayCookie(expr)); |
| 710 | |
| 711 | // The size of the cookie. |
| 712 | CharUnits cookieSize = getArrayCookieSizeImpl(elementType); |
| 713 | |
| 714 | // Compute an offset to the cookie. |
| 715 | llvm::Value *cookiePtr = newPtr; |
| 716 | |
| 717 | // Write the number of elements into the appropriate slot. |
Micah Villmow | 956a5a1 | 2012-10-25 15:39:14 +0000 | [diff] [blame] | 718 | unsigned AS = newPtr->getType()->getPointerAddressSpace(); |
John McCall | e2b45e2 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 719 | llvm::Value *numElementsPtr |
| 720 | = CGF.Builder.CreateBitCast(cookiePtr, CGF.SizeTy->getPointerTo(AS)); |
| 721 | CGF.Builder.CreateStore(numElements, numElementsPtr); |
| 722 | |
| 723 | // Finally, compute a pointer to the actual data buffer by skipping |
| 724 | // over the cookie completely. |
| 725 | return CGF.Builder.CreateConstInBoundsGEP1_64(newPtr, |
| 726 | cookieSize.getQuantity()); |
| 727 | } |
| 728 | |
John McCall | 20bb175 | 2012-05-01 06:13:13 +0000 | [diff] [blame] | 729 | void MicrosoftCXXABI::EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D, |
| 730 | llvm::GlobalVariable *DeclPtr, |
| 731 | bool PerformInit) { |
| 732 | // FIXME: this code was only tested for global initialization. |
| 733 | // Not sure whether we want thread-safe static local variables as VS |
| 734 | // doesn't make them thread-safe. |
| 735 | |
Richard Smith | 04e5176 | 2013-04-14 23:01:42 +0000 | [diff] [blame] | 736 | if (D.getTLSKind()) |
| 737 | CGM.ErrorUnsupported(&D, "dynamic TLS initialization"); |
| 738 | |
John McCall | 20bb175 | 2012-05-01 06:13:13 +0000 | [diff] [blame] | 739 | // Emit the initializer and add a global destructor if appropriate. |
| 740 | CGF.EmitCXXGlobalVarDeclInit(D, DeclPtr, PerformInit); |
| 741 | } |
| 742 | |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 743 | // Member pointer helpers. |
| 744 | static bool hasVBPtrOffsetField(MSInheritanceModel Inheritance) { |
| 745 | return Inheritance == MSIM_Unspecified; |
Reid Kleckner | a8a0f76 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 746 | } |
| 747 | |
Reid Kleckner | f632730 | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 748 | static bool hasOnlyOneField(bool IsMemberFunction, |
| 749 | MSInheritanceModel Inheritance) { |
| 750 | return Inheritance <= MSIM_SinglePolymorphic || |
| 751 | (!IsMemberFunction && Inheritance <= MSIM_MultiplePolymorphic); |
Reid Kleckner | 3d2f000 | 2013-04-30 20:15:14 +0000 | [diff] [blame] | 752 | } |
| 753 | |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 754 | // Only member pointers to functions need a this adjustment, since it can be |
| 755 | // combined with the field offset for data pointers. |
Reid Kleckner | 79e0291 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 756 | static bool hasNonVirtualBaseAdjustmentField(bool IsMemberFunction, |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 757 | MSInheritanceModel Inheritance) { |
Reid Kleckner | 79e0291 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 758 | return (IsMemberFunction && Inheritance >= MSIM_Multiple); |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | static bool hasVirtualBaseAdjustmentField(MSInheritanceModel Inheritance) { |
| 762 | return Inheritance >= MSIM_Virtual; |
| 763 | } |
| 764 | |
| 765 | // Use zero for the field offset of a null data member pointer if we can |
| 766 | // guarantee that zero is not a valid field offset, or if the member pointer has |
| 767 | // multiple fields. Polymorphic classes have a vfptr at offset zero, so we can |
| 768 | // use zero for null. If there are multiple fields, we can use zero even if it |
| 769 | // is a valid field offset because null-ness testing will check the other |
| 770 | // fields. |
| 771 | static bool nullFieldOffsetIsZero(MSInheritanceModel Inheritance) { |
| 772 | return Inheritance != MSIM_Multiple && Inheritance != MSIM_Single; |
| 773 | } |
| 774 | |
| 775 | bool MicrosoftCXXABI::isZeroInitializable(const MemberPointerType *MPT) { |
| 776 | // Null-ness for function memptrs only depends on the first field, which is |
| 777 | // the function pointer. The rest don't matter, so we can zero initialize. |
| 778 | if (MPT->isMemberFunctionPointer()) |
| 779 | return true; |
| 780 | |
| 781 | // The virtual base adjustment field is always -1 for null, so if we have one |
| 782 | // we can't zero initialize. The field offset is sometimes also -1 if 0 is a |
| 783 | // valid field offset. |
| 784 | const CXXRecordDecl *RD = MPT->getClass()->getAsCXXRecordDecl(); |
| 785 | MSInheritanceModel Inheritance = RD->getMSInheritanceModel(); |
| 786 | return (!hasVirtualBaseAdjustmentField(Inheritance) && |
| 787 | nullFieldOffsetIsZero(Inheritance)); |
| 788 | } |
| 789 | |
| 790 | llvm::Type * |
| 791 | MicrosoftCXXABI::ConvertMemberPointerType(const MemberPointerType *MPT) { |
| 792 | const CXXRecordDecl *RD = MPT->getClass()->getAsCXXRecordDecl(); |
| 793 | MSInheritanceModel Inheritance = RD->getMSInheritanceModel(); |
| 794 | llvm::SmallVector<llvm::Type *, 4> fields; |
| 795 | if (MPT->isMemberFunctionPointer()) |
| 796 | fields.push_back(CGM.VoidPtrTy); // FunctionPointerOrVirtualThunk |
| 797 | else |
| 798 | fields.push_back(CGM.IntTy); // FieldOffset |
| 799 | |
Reid Kleckner | 79e0291 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 800 | if (hasNonVirtualBaseAdjustmentField(MPT->isMemberFunctionPointer(), |
| 801 | Inheritance)) |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 802 | fields.push_back(CGM.IntTy); |
Reid Kleckner | 79e0291 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 803 | if (hasVBPtrOffsetField(Inheritance)) |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 804 | fields.push_back(CGM.IntTy); |
| 805 | if (hasVirtualBaseAdjustmentField(Inheritance)) |
| 806 | fields.push_back(CGM.IntTy); // VirtualBaseAdjustmentOffset |
| 807 | |
| 808 | if (fields.size() == 1) |
| 809 | return fields[0]; |
| 810 | return llvm::StructType::get(CGM.getLLVMContext(), fields); |
| 811 | } |
| 812 | |
| 813 | void MicrosoftCXXABI:: |
| 814 | GetNullMemberPointerFields(const MemberPointerType *MPT, |
| 815 | llvm::SmallVectorImpl<llvm::Constant *> &fields) { |
| 816 | assert(fields.empty()); |
| 817 | const CXXRecordDecl *RD = MPT->getClass()->getAsCXXRecordDecl(); |
| 818 | MSInheritanceModel Inheritance = RD->getMSInheritanceModel(); |
| 819 | if (MPT->isMemberFunctionPointer()) { |
| 820 | // FunctionPointerOrVirtualThunk |
| 821 | fields.push_back(llvm::Constant::getNullValue(CGM.VoidPtrTy)); |
| 822 | } else { |
| 823 | if (nullFieldOffsetIsZero(Inheritance)) |
| 824 | fields.push_back(getZeroInt()); // FieldOffset |
| 825 | else |
| 826 | fields.push_back(getAllOnesInt()); // FieldOffset |
Reid Kleckner | a8a0f76 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 827 | } |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 828 | |
Reid Kleckner | 79e0291 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 829 | if (hasNonVirtualBaseAdjustmentField(MPT->isMemberFunctionPointer(), |
| 830 | Inheritance)) |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 831 | fields.push_back(getZeroInt()); |
Reid Kleckner | 79e0291 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 832 | if (hasVBPtrOffsetField(Inheritance)) |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 833 | fields.push_back(getZeroInt()); |
| 834 | if (hasVirtualBaseAdjustmentField(Inheritance)) |
| 835 | fields.push_back(getAllOnesInt()); |
Reid Kleckner | a8a0f76 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | llvm::Constant * |
| 839 | MicrosoftCXXABI::EmitNullMemberPointer(const MemberPointerType *MPT) { |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 840 | llvm::SmallVector<llvm::Constant *, 4> fields; |
| 841 | GetNullMemberPointerFields(MPT, fields); |
| 842 | if (fields.size() == 1) |
| 843 | return fields[0]; |
| 844 | llvm::Constant *Res = llvm::ConstantStruct::getAnon(fields); |
| 845 | assert(Res->getType() == ConvertMemberPointerType(MPT)); |
| 846 | return Res; |
Reid Kleckner | a8a0f76 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | llvm::Constant * |
Reid Kleckner | 79e0291 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 850 | MicrosoftCXXABI::EmitFullMemberPointer(llvm::Constant *FirstField, |
| 851 | bool IsMemberFunction, |
Reid Kleckner | f632730 | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 852 | const CXXRecordDecl *RD, |
| 853 | CharUnits NonVirtualBaseAdjustment) |
Reid Kleckner | 79e0291 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 854 | { |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 855 | MSInheritanceModel Inheritance = RD->getMSInheritanceModel(); |
Reid Kleckner | 79e0291 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 856 | |
| 857 | // Single inheritance class member pointer are represented as scalars instead |
| 858 | // of aggregates. |
Reid Kleckner | f632730 | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 859 | if (hasOnlyOneField(IsMemberFunction, Inheritance)) |
Reid Kleckner | 79e0291 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 860 | return FirstField; |
| 861 | |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 862 | llvm::SmallVector<llvm::Constant *, 4> fields; |
Reid Kleckner | 79e0291 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 863 | fields.push_back(FirstField); |
| 864 | |
| 865 | if (hasNonVirtualBaseAdjustmentField(IsMemberFunction, Inheritance)) |
Reid Kleckner | f632730 | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 866 | fields.push_back(llvm::ConstantInt::get( |
| 867 | CGM.IntTy, NonVirtualBaseAdjustment.getQuantity())); |
Reid Kleckner | 79e0291 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 868 | |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 869 | if (hasVBPtrOffsetField(Inheritance)) { |
Reid Kleckner | a06d585 | 2013-06-05 15:58:29 +0000 | [diff] [blame] | 870 | fields.push_back(llvm::ConstantInt::get( |
| 871 | CGM.IntTy, GetVBPtrOffsetFromBases(RD).getQuantity())); |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 872 | } |
Reid Kleckner | 79e0291 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 873 | |
| 874 | // The rest of the fields are adjusted by conversions to a more derived class. |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 875 | if (hasVirtualBaseAdjustmentField(Inheritance)) |
| 876 | fields.push_back(getZeroInt()); |
Reid Kleckner | 79e0291 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 877 | |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 878 | return llvm::ConstantStruct::getAnon(fields); |
Reid Kleckner | a8a0f76 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 879 | } |
| 880 | |
Reid Kleckner | 79e0291 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 881 | llvm::Constant * |
| 882 | MicrosoftCXXABI::EmitMemberDataPointer(const MemberPointerType *MPT, |
| 883 | CharUnits offset) { |
| 884 | const CXXRecordDecl *RD = MPT->getClass()->getAsCXXRecordDecl(); |
| 885 | llvm::Constant *FirstField = |
| 886 | llvm::ConstantInt::get(CGM.IntTy, offset.getQuantity()); |
Reid Kleckner | f632730 | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 887 | return EmitFullMemberPointer(FirstField, /*IsMemberFunction=*/false, RD, |
| 888 | CharUnits::Zero()); |
| 889 | } |
| 890 | |
| 891 | llvm::Constant *MicrosoftCXXABI::EmitMemberPointer(const CXXMethodDecl *MD) { |
| 892 | return BuildMemberPointer(MD->getParent(), MD, CharUnits::Zero()); |
| 893 | } |
| 894 | |
| 895 | llvm::Constant *MicrosoftCXXABI::EmitMemberPointer(const APValue &MP, |
| 896 | QualType MPType) { |
| 897 | const MemberPointerType *MPT = MPType->castAs<MemberPointerType>(); |
| 898 | const ValueDecl *MPD = MP.getMemberPointerDecl(); |
| 899 | if (!MPD) |
| 900 | return EmitNullMemberPointer(MPT); |
| 901 | |
| 902 | CharUnits ThisAdjustment = getMemberPointerPathAdjustment(MP); |
| 903 | |
| 904 | // FIXME PR15713: Support virtual inheritance paths. |
| 905 | |
| 906 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MPD)) |
| 907 | return BuildMemberPointer(MPT->getClass()->getAsCXXRecordDecl(), |
| 908 | MD, ThisAdjustment); |
| 909 | |
| 910 | CharUnits FieldOffset = |
| 911 | getContext().toCharUnitsFromBits(getContext().getFieldOffset(MPD)); |
| 912 | return EmitMemberDataPointer(MPT, ThisAdjustment + FieldOffset); |
Reid Kleckner | 79e0291 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 913 | } |
| 914 | |
| 915 | llvm::Constant * |
Reid Kleckner | f632730 | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 916 | MicrosoftCXXABI::BuildMemberPointer(const CXXRecordDecl *RD, |
| 917 | const CXXMethodDecl *MD, |
| 918 | CharUnits NonVirtualBaseAdjustment) { |
Reid Kleckner | 79e0291 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 919 | assert(MD->isInstance() && "Member function must not be static!"); |
| 920 | MD = MD->getCanonicalDecl(); |
Reid Kleckner | 79e0291 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 921 | CodeGenTypes &Types = CGM.getTypes(); |
| 922 | |
| 923 | llvm::Constant *FirstField; |
| 924 | if (MD->isVirtual()) { |
| 925 | // FIXME: We have to instantiate a thunk that loads the vftable and jumps to |
| 926 | // the right offset. |
| 927 | FirstField = llvm::Constant::getNullValue(CGM.VoidPtrTy); |
| 928 | } else { |
| 929 | const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>(); |
| 930 | llvm::Type *Ty; |
| 931 | // Check whether the function has a computable LLVM signature. |
| 932 | if (Types.isFuncTypeConvertible(FPT)) { |
| 933 | // The function has a computable LLVM signature; use the correct type. |
| 934 | Ty = Types.GetFunctionType(Types.arrangeCXXMethodDeclaration(MD)); |
| 935 | } else { |
| 936 | // Use an arbitrary non-function type to tell GetAddrOfFunction that the |
| 937 | // function type is incomplete. |
| 938 | Ty = CGM.PtrDiffTy; |
| 939 | } |
| 940 | FirstField = CGM.GetAddrOfFunction(MD, Ty); |
| 941 | FirstField = llvm::ConstantExpr::getBitCast(FirstField, CGM.VoidPtrTy); |
| 942 | } |
| 943 | |
| 944 | // The rest of the fields are common with data member pointers. |
Reid Kleckner | f632730 | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 945 | return EmitFullMemberPointer(FirstField, /*IsMemberFunction=*/true, RD, |
| 946 | NonVirtualBaseAdjustment); |
Reid Kleckner | 79e0291 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 947 | } |
| 948 | |
Reid Kleckner | 3d2f000 | 2013-04-30 20:15:14 +0000 | [diff] [blame] | 949 | /// Member pointers are the same if they're either bitwise identical *or* both |
| 950 | /// null. Null-ness for function members is determined by the first field, |
| 951 | /// while for data member pointers we must compare all fields. |
| 952 | llvm::Value * |
| 953 | MicrosoftCXXABI::EmitMemberPointerComparison(CodeGenFunction &CGF, |
| 954 | llvm::Value *L, |
| 955 | llvm::Value *R, |
| 956 | const MemberPointerType *MPT, |
| 957 | bool Inequality) { |
| 958 | CGBuilderTy &Builder = CGF.Builder; |
| 959 | |
| 960 | // Handle != comparisons by switching the sense of all boolean operations. |
| 961 | llvm::ICmpInst::Predicate Eq; |
| 962 | llvm::Instruction::BinaryOps And, Or; |
| 963 | if (Inequality) { |
| 964 | Eq = llvm::ICmpInst::ICMP_NE; |
| 965 | And = llvm::Instruction::Or; |
| 966 | Or = llvm::Instruction::And; |
| 967 | } else { |
| 968 | Eq = llvm::ICmpInst::ICMP_EQ; |
| 969 | And = llvm::Instruction::And; |
| 970 | Or = llvm::Instruction::Or; |
| 971 | } |
| 972 | |
| 973 | // If this is a single field member pointer (single inheritance), this is a |
| 974 | // single icmp. |
| 975 | const CXXRecordDecl *RD = MPT->getClass()->getAsCXXRecordDecl(); |
| 976 | MSInheritanceModel Inheritance = RD->getMSInheritanceModel(); |
Reid Kleckner | f632730 | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 977 | if (hasOnlyOneField(MPT->isMemberFunctionPointer(), Inheritance)) |
Reid Kleckner | 3d2f000 | 2013-04-30 20:15:14 +0000 | [diff] [blame] | 978 | return Builder.CreateICmp(Eq, L, R); |
| 979 | |
| 980 | // Compare the first field. |
| 981 | llvm::Value *L0 = Builder.CreateExtractValue(L, 0, "lhs.0"); |
| 982 | llvm::Value *R0 = Builder.CreateExtractValue(R, 0, "rhs.0"); |
| 983 | llvm::Value *Cmp0 = Builder.CreateICmp(Eq, L0, R0, "memptr.cmp.first"); |
| 984 | |
| 985 | // Compare everything other than the first field. |
| 986 | llvm::Value *Res = 0; |
| 987 | llvm::StructType *LType = cast<llvm::StructType>(L->getType()); |
| 988 | for (unsigned I = 1, E = LType->getNumElements(); I != E; ++I) { |
| 989 | llvm::Value *LF = Builder.CreateExtractValue(L, I); |
| 990 | llvm::Value *RF = Builder.CreateExtractValue(R, I); |
| 991 | llvm::Value *Cmp = Builder.CreateICmp(Eq, LF, RF, "memptr.cmp.rest"); |
| 992 | if (Res) |
| 993 | Res = Builder.CreateBinOp(And, Res, Cmp); |
| 994 | else |
| 995 | Res = Cmp; |
| 996 | } |
| 997 | |
| 998 | // Check if the first field is 0 if this is a function pointer. |
| 999 | if (MPT->isMemberFunctionPointer()) { |
| 1000 | // (l1 == r1 && ...) || l0 == 0 |
| 1001 | llvm::Value *Zero = llvm::Constant::getNullValue(L0->getType()); |
| 1002 | llvm::Value *IsZero = Builder.CreateICmp(Eq, L0, Zero, "memptr.cmp.iszero"); |
| 1003 | Res = Builder.CreateBinOp(Or, Res, IsZero); |
| 1004 | } |
| 1005 | |
| 1006 | // Combine the comparison of the first field, which must always be true for |
| 1007 | // this comparison to succeeed. |
| 1008 | return Builder.CreateBinOp(And, Res, Cmp0, "memptr.cmp"); |
| 1009 | } |
| 1010 | |
Reid Kleckner | a8a0f76 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 1011 | llvm::Value * |
| 1012 | MicrosoftCXXABI::EmitMemberPointerIsNotNull(CodeGenFunction &CGF, |
| 1013 | llvm::Value *MemPtr, |
| 1014 | const MemberPointerType *MPT) { |
| 1015 | CGBuilderTy &Builder = CGF.Builder; |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 1016 | llvm::SmallVector<llvm::Constant *, 4> fields; |
| 1017 | // We only need one field for member functions. |
| 1018 | if (MPT->isMemberFunctionPointer()) |
| 1019 | fields.push_back(llvm::Constant::getNullValue(CGM.VoidPtrTy)); |
| 1020 | else |
| 1021 | GetNullMemberPointerFields(MPT, fields); |
| 1022 | assert(!fields.empty()); |
| 1023 | llvm::Value *FirstField = MemPtr; |
| 1024 | if (MemPtr->getType()->isStructTy()) |
| 1025 | FirstField = Builder.CreateExtractValue(MemPtr, 0); |
| 1026 | llvm::Value *Res = Builder.CreateICmpNE(FirstField, fields[0], "memptr.cmp0"); |
Reid Kleckner | a8a0f76 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 1027 | |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 1028 | // For function member pointers, we only need to test the function pointer |
| 1029 | // field. The other fields if any can be garbage. |
| 1030 | if (MPT->isMemberFunctionPointer()) |
| 1031 | return Res; |
| 1032 | |
| 1033 | // Otherwise, emit a series of compares and combine the results. |
| 1034 | for (int I = 1, E = fields.size(); I < E; ++I) { |
| 1035 | llvm::Value *Field = Builder.CreateExtractValue(MemPtr, I); |
| 1036 | llvm::Value *Next = Builder.CreateICmpNE(Field, fields[I], "memptr.cmp"); |
| 1037 | Res = Builder.CreateAnd(Res, Next, "memptr.tobool"); |
| 1038 | } |
| 1039 | return Res; |
| 1040 | } |
| 1041 | |
Reid Kleckner | f632730 | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 1042 | bool MicrosoftCXXABI::MemberPointerConstantIsNull(const MemberPointerType *MPT, |
| 1043 | llvm::Constant *Val) { |
| 1044 | // Function pointers are null if the pointer in the first field is null. |
| 1045 | if (MPT->isMemberFunctionPointer()) { |
| 1046 | llvm::Constant *FirstField = Val->getType()->isStructTy() ? |
| 1047 | Val->getAggregateElement(0U) : Val; |
| 1048 | return FirstField->isNullValue(); |
| 1049 | } |
| 1050 | |
| 1051 | // If it's not a function pointer and it's zero initializable, we can easily |
| 1052 | // check zero. |
| 1053 | if (isZeroInitializable(MPT) && Val->isNullValue()) |
| 1054 | return true; |
| 1055 | |
| 1056 | // Otherwise, break down all the fields for comparison. Hopefully these |
| 1057 | // little Constants are reused, while a big null struct might not be. |
| 1058 | llvm::SmallVector<llvm::Constant *, 4> Fields; |
| 1059 | GetNullMemberPointerFields(MPT, Fields); |
| 1060 | if (Fields.size() == 1) { |
| 1061 | assert(Val->getType()->isIntegerTy()); |
| 1062 | return Val == Fields[0]; |
| 1063 | } |
| 1064 | |
| 1065 | unsigned I, E; |
| 1066 | for (I = 0, E = Fields.size(); I != E; ++I) { |
| 1067 | if (Val->getAggregateElement(I) != Fields[I]) |
| 1068 | break; |
| 1069 | } |
| 1070 | return I == E; |
| 1071 | } |
| 1072 | |
Reid Kleckner | b0f533e | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 1073 | llvm::Value * |
| 1074 | MicrosoftCXXABI::GetVBaseOffsetFromVBPtr(CodeGenFunction &CGF, |
| 1075 | llvm::Value *This, |
| 1076 | llvm::Value *VBTableOffset, |
| 1077 | llvm::Value *VBPtrOffset, |
| 1078 | llvm::Value **VBPtrOut) { |
| 1079 | CGBuilderTy &Builder = CGF.Builder; |
| 1080 | // Load the vbtable pointer from the vbptr in the instance. |
| 1081 | This = Builder.CreateBitCast(This, CGM.Int8PtrTy); |
| 1082 | llvm::Value *VBPtr = |
| 1083 | Builder.CreateInBoundsGEP(This, VBPtrOffset, "vbptr"); |
| 1084 | if (VBPtrOut) *VBPtrOut = VBPtr; |
| 1085 | VBPtr = Builder.CreateBitCast(VBPtr, CGM.Int8PtrTy->getPointerTo(0)); |
| 1086 | llvm::Value *VBTable = Builder.CreateLoad(VBPtr, "vbtable"); |
| 1087 | |
| 1088 | // Load an i32 offset from the vb-table. |
| 1089 | llvm::Value *VBaseOffs = Builder.CreateInBoundsGEP(VBTable, VBTableOffset); |
| 1090 | VBaseOffs = Builder.CreateBitCast(VBaseOffs, CGM.Int32Ty->getPointerTo(0)); |
| 1091 | return Builder.CreateLoad(VBaseOffs, "vbase_offs"); |
| 1092 | } |
| 1093 | |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 1094 | // Returns an adjusted base cast to i8*, since we do more address arithmetic on |
| 1095 | // it. |
| 1096 | llvm::Value * |
| 1097 | MicrosoftCXXABI::AdjustVirtualBase(CodeGenFunction &CGF, |
| 1098 | const CXXRecordDecl *RD, llvm::Value *Base, |
Reid Kleckner | b0f533e | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 1099 | llvm::Value *VBTableOffset, |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 1100 | llvm::Value *VBPtrOffset) { |
| 1101 | CGBuilderTy &Builder = CGF.Builder; |
| 1102 | Base = Builder.CreateBitCast(Base, CGM.Int8PtrTy); |
| 1103 | llvm::BasicBlock *OriginalBB = 0; |
| 1104 | llvm::BasicBlock *SkipAdjustBB = 0; |
| 1105 | llvm::BasicBlock *VBaseAdjustBB = 0; |
| 1106 | |
| 1107 | // In the unspecified inheritance model, there might not be a vbtable at all, |
| 1108 | // in which case we need to skip the virtual base lookup. If there is a |
| 1109 | // vbtable, the first entry is a no-op entry that gives back the original |
| 1110 | // base, so look for a virtual base adjustment offset of zero. |
| 1111 | if (VBPtrOffset) { |
| 1112 | OriginalBB = Builder.GetInsertBlock(); |
| 1113 | VBaseAdjustBB = CGF.createBasicBlock("memptr.vadjust"); |
| 1114 | SkipAdjustBB = CGF.createBasicBlock("memptr.skip_vadjust"); |
| 1115 | llvm::Value *IsVirtual = |
Reid Kleckner | b0f533e | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 1116 | Builder.CreateICmpNE(VBTableOffset, getZeroInt(), |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 1117 | "memptr.is_vbase"); |
| 1118 | Builder.CreateCondBr(IsVirtual, VBaseAdjustBB, SkipAdjustBB); |
| 1119 | CGF.EmitBlock(VBaseAdjustBB); |
Reid Kleckner | a8a0f76 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 1120 | } |
| 1121 | |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 1122 | // If we weren't given a dynamic vbptr offset, RD should be complete and we'll |
| 1123 | // know the vbptr offset. |
| 1124 | if (!VBPtrOffset) { |
Reid Kleckner | b0f533e | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 1125 | CharUnits offs = CharUnits::Zero(); |
| 1126 | if (RD->getNumVBases()) { |
| 1127 | offs = GetVBPtrOffsetFromBases(RD); |
| 1128 | } |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 1129 | VBPtrOffset = llvm::ConstantInt::get(CGM.IntTy, offs.getQuantity()); |
| 1130 | } |
Reid Kleckner | b0f533e | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 1131 | llvm::Value *VBPtr = 0; |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 1132 | llvm::Value *VBaseOffs = |
Reid Kleckner | b0f533e | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 1133 | GetVBaseOffsetFromVBPtr(CGF, Base, VBTableOffset, VBPtrOffset, &VBPtr); |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 1134 | llvm::Value *AdjustedBase = Builder.CreateInBoundsGEP(VBPtr, VBaseOffs); |
| 1135 | |
| 1136 | // Merge control flow with the case where we didn't have to adjust. |
| 1137 | if (VBaseAdjustBB) { |
| 1138 | Builder.CreateBr(SkipAdjustBB); |
| 1139 | CGF.EmitBlock(SkipAdjustBB); |
| 1140 | llvm::PHINode *Phi = Builder.CreatePHI(CGM.Int8PtrTy, 2, "memptr.base"); |
| 1141 | Phi->addIncoming(Base, OriginalBB); |
| 1142 | Phi->addIncoming(AdjustedBase, VBaseAdjustBB); |
| 1143 | return Phi; |
| 1144 | } |
| 1145 | return AdjustedBase; |
Reid Kleckner | a8a0f76 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 1146 | } |
| 1147 | |
| 1148 | llvm::Value * |
| 1149 | MicrosoftCXXABI::EmitMemberDataPointerAddress(CodeGenFunction &CGF, |
| 1150 | llvm::Value *Base, |
| 1151 | llvm::Value *MemPtr, |
| 1152 | const MemberPointerType *MPT) { |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 1153 | assert(MPT->isMemberDataPointer()); |
Reid Kleckner | a8a0f76 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 1154 | unsigned AS = Base->getType()->getPointerAddressSpace(); |
| 1155 | llvm::Type *PType = |
| 1156 | CGF.ConvertTypeForMem(MPT->getPointeeType())->getPointerTo(AS); |
| 1157 | CGBuilderTy &Builder = CGF.Builder; |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 1158 | const CXXRecordDecl *RD = MPT->getClass()->getAsCXXRecordDecl(); |
| 1159 | MSInheritanceModel Inheritance = RD->getMSInheritanceModel(); |
Reid Kleckner | a8a0f76 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 1160 | |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 1161 | // Extract the fields we need, regardless of model. We'll apply them if we |
| 1162 | // have them. |
| 1163 | llvm::Value *FieldOffset = MemPtr; |
| 1164 | llvm::Value *VirtualBaseAdjustmentOffset = 0; |
| 1165 | llvm::Value *VBPtrOffset = 0; |
| 1166 | if (MemPtr->getType()->isStructTy()) { |
| 1167 | // We need to extract values. |
| 1168 | unsigned I = 0; |
| 1169 | FieldOffset = Builder.CreateExtractValue(MemPtr, I++); |
| 1170 | if (hasVBPtrOffsetField(Inheritance)) |
| 1171 | VBPtrOffset = Builder.CreateExtractValue(MemPtr, I++); |
| 1172 | if (hasVirtualBaseAdjustmentField(Inheritance)) |
| 1173 | VirtualBaseAdjustmentOffset = Builder.CreateExtractValue(MemPtr, I++); |
Reid Kleckner | a8a0f76 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 1174 | } |
| 1175 | |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 1176 | if (VirtualBaseAdjustmentOffset) { |
| 1177 | Base = AdjustVirtualBase(CGF, RD, Base, VirtualBaseAdjustmentOffset, |
| 1178 | VBPtrOffset); |
Reid Kleckner | a8a0f76 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 1179 | } |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 1180 | llvm::Value *Addr = |
| 1181 | Builder.CreateInBoundsGEP(Base, FieldOffset, "memptr.offset"); |
Reid Kleckner | a8a0f76 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 1182 | |
| 1183 | // Cast the address to the appropriate pointer type, adopting the address |
| 1184 | // space of the base pointer. |
| 1185 | return Builder.CreateBitCast(Addr, PType); |
| 1186 | } |
| 1187 | |
Reid Kleckner | f632730 | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 1188 | static MSInheritanceModel |
| 1189 | getInheritanceFromMemptr(const MemberPointerType *MPT) { |
| 1190 | return MPT->getClass()->getAsCXXRecordDecl()->getMSInheritanceModel(); |
| 1191 | } |
| 1192 | |
| 1193 | llvm::Value * |
| 1194 | MicrosoftCXXABI::EmitMemberPointerConversion(CodeGenFunction &CGF, |
| 1195 | const CastExpr *E, |
| 1196 | llvm::Value *Src) { |
| 1197 | assert(E->getCastKind() == CK_DerivedToBaseMemberPointer || |
| 1198 | E->getCastKind() == CK_BaseToDerivedMemberPointer || |
| 1199 | E->getCastKind() == CK_ReinterpretMemberPointer); |
| 1200 | |
| 1201 | // Use constant emission if we can. |
| 1202 | if (isa<llvm::Constant>(Src)) |
| 1203 | return EmitMemberPointerConversion(E, cast<llvm::Constant>(Src)); |
| 1204 | |
| 1205 | // We may be adding or dropping fields from the member pointer, so we need |
| 1206 | // both types and the inheritance models of both records. |
| 1207 | const MemberPointerType *SrcTy = |
| 1208 | E->getSubExpr()->getType()->castAs<MemberPointerType>(); |
| 1209 | const MemberPointerType *DstTy = E->getType()->castAs<MemberPointerType>(); |
| 1210 | MSInheritanceModel SrcInheritance = getInheritanceFromMemptr(SrcTy); |
| 1211 | MSInheritanceModel DstInheritance = getInheritanceFromMemptr(DstTy); |
| 1212 | bool IsFunc = SrcTy->isMemberFunctionPointer(); |
| 1213 | |
| 1214 | // If the classes use the same null representation, reinterpret_cast is a nop. |
| 1215 | bool IsReinterpret = E->getCastKind() == CK_ReinterpretMemberPointer; |
| 1216 | if (IsReinterpret && (IsFunc || |
| 1217 | nullFieldOffsetIsZero(SrcInheritance) == |
| 1218 | nullFieldOffsetIsZero(DstInheritance))) |
| 1219 | return Src; |
| 1220 | |
| 1221 | CGBuilderTy &Builder = CGF.Builder; |
| 1222 | |
| 1223 | // Branch past the conversion if Src is null. |
| 1224 | llvm::Value *IsNotNull = EmitMemberPointerIsNotNull(CGF, Src, SrcTy); |
| 1225 | llvm::Constant *DstNull = EmitNullMemberPointer(DstTy); |
| 1226 | |
| 1227 | // C++ 5.2.10p9: The null member pointer value is converted to the null member |
| 1228 | // pointer value of the destination type. |
| 1229 | if (IsReinterpret) { |
| 1230 | // For reinterpret casts, sema ensures that src and dst are both functions |
| 1231 | // or data and have the same size, which means the LLVM types should match. |
| 1232 | assert(Src->getType() == DstNull->getType()); |
| 1233 | return Builder.CreateSelect(IsNotNull, Src, DstNull); |
| 1234 | } |
| 1235 | |
| 1236 | llvm::BasicBlock *OriginalBB = Builder.GetInsertBlock(); |
| 1237 | llvm::BasicBlock *ConvertBB = CGF.createBasicBlock("memptr.convert"); |
| 1238 | llvm::BasicBlock *ContinueBB = CGF.createBasicBlock("memptr.converted"); |
| 1239 | Builder.CreateCondBr(IsNotNull, ConvertBB, ContinueBB); |
| 1240 | CGF.EmitBlock(ConvertBB); |
| 1241 | |
| 1242 | // Decompose src. |
| 1243 | llvm::Value *FirstField = Src; |
| 1244 | llvm::Value *NonVirtualBaseAdjustment = 0; |
| 1245 | llvm::Value *VirtualBaseAdjustmentOffset = 0; |
| 1246 | llvm::Value *VBPtrOffset = 0; |
| 1247 | if (!hasOnlyOneField(IsFunc, SrcInheritance)) { |
| 1248 | // We need to extract values. |
| 1249 | unsigned I = 0; |
| 1250 | FirstField = Builder.CreateExtractValue(Src, I++); |
| 1251 | if (hasNonVirtualBaseAdjustmentField(IsFunc, SrcInheritance)) |
| 1252 | NonVirtualBaseAdjustment = Builder.CreateExtractValue(Src, I++); |
| 1253 | if (hasVBPtrOffsetField(SrcInheritance)) |
| 1254 | VBPtrOffset = Builder.CreateExtractValue(Src, I++); |
| 1255 | if (hasVirtualBaseAdjustmentField(SrcInheritance)) |
| 1256 | VirtualBaseAdjustmentOffset = Builder.CreateExtractValue(Src, I++); |
| 1257 | } |
| 1258 | |
| 1259 | // For data pointers, we adjust the field offset directly. For functions, we |
| 1260 | // have a separate field. |
| 1261 | llvm::Constant *Adj = getMemberPointerAdjustment(E); |
| 1262 | if (Adj) { |
| 1263 | Adj = llvm::ConstantExpr::getTruncOrBitCast(Adj, CGM.IntTy); |
| 1264 | llvm::Value *&NVAdjustField = IsFunc ? NonVirtualBaseAdjustment : FirstField; |
| 1265 | bool isDerivedToBase = (E->getCastKind() == CK_DerivedToBaseMemberPointer); |
| 1266 | if (!NVAdjustField) // If this field didn't exist in src, it's zero. |
| 1267 | NVAdjustField = getZeroInt(); |
| 1268 | if (isDerivedToBase) |
| 1269 | NVAdjustField = Builder.CreateNSWSub(NVAdjustField, Adj, "adj"); |
| 1270 | else |
| 1271 | NVAdjustField = Builder.CreateNSWAdd(NVAdjustField, Adj, "adj"); |
| 1272 | } |
| 1273 | |
| 1274 | // FIXME PR15713: Support conversions through virtually derived classes. |
| 1275 | |
| 1276 | // Recompose dst from the null struct and the adjusted fields from src. |
| 1277 | llvm::Value *Dst; |
| 1278 | if (hasOnlyOneField(IsFunc, DstInheritance)) { |
| 1279 | Dst = FirstField; |
| 1280 | } else { |
| 1281 | Dst = llvm::UndefValue::get(DstNull->getType()); |
| 1282 | unsigned Idx = 0; |
| 1283 | Dst = Builder.CreateInsertValue(Dst, FirstField, Idx++); |
| 1284 | if (hasNonVirtualBaseAdjustmentField(IsFunc, DstInheritance)) |
| 1285 | Dst = Builder.CreateInsertValue( |
| 1286 | Dst, getValueOrZeroInt(NonVirtualBaseAdjustment), Idx++); |
| 1287 | if (hasVBPtrOffsetField(DstInheritance)) |
| 1288 | Dst = Builder.CreateInsertValue( |
| 1289 | Dst, getValueOrZeroInt(VBPtrOffset), Idx++); |
| 1290 | if (hasVirtualBaseAdjustmentField(DstInheritance)) |
| 1291 | Dst = Builder.CreateInsertValue( |
| 1292 | Dst, getValueOrZeroInt(VirtualBaseAdjustmentOffset), Idx++); |
| 1293 | } |
| 1294 | Builder.CreateBr(ContinueBB); |
| 1295 | |
| 1296 | // In the continuation, choose between DstNull and Dst. |
| 1297 | CGF.EmitBlock(ContinueBB); |
| 1298 | llvm::PHINode *Phi = Builder.CreatePHI(DstNull->getType(), 2, "memptr.converted"); |
| 1299 | Phi->addIncoming(DstNull, OriginalBB); |
| 1300 | Phi->addIncoming(Dst, ConvertBB); |
| 1301 | return Phi; |
| 1302 | } |
| 1303 | |
| 1304 | llvm::Constant * |
| 1305 | MicrosoftCXXABI::EmitMemberPointerConversion(const CastExpr *E, |
| 1306 | llvm::Constant *Src) { |
| 1307 | const MemberPointerType *SrcTy = |
| 1308 | E->getSubExpr()->getType()->castAs<MemberPointerType>(); |
| 1309 | const MemberPointerType *DstTy = E->getType()->castAs<MemberPointerType>(); |
| 1310 | |
| 1311 | // If src is null, emit a new null for dst. We can't return src because dst |
| 1312 | // might have a new representation. |
| 1313 | if (MemberPointerConstantIsNull(SrcTy, Src)) |
| 1314 | return EmitNullMemberPointer(DstTy); |
| 1315 | |
| 1316 | // We don't need to do anything for reinterpret_casts of non-null member |
| 1317 | // pointers. We should only get here when the two type representations have |
| 1318 | // the same size. |
| 1319 | if (E->getCastKind() == CK_ReinterpretMemberPointer) |
| 1320 | return Src; |
| 1321 | |
| 1322 | MSInheritanceModel SrcInheritance = getInheritanceFromMemptr(SrcTy); |
| 1323 | MSInheritanceModel DstInheritance = getInheritanceFromMemptr(DstTy); |
| 1324 | |
| 1325 | // Decompose src. |
| 1326 | llvm::Constant *FirstField = Src; |
| 1327 | llvm::Constant *NonVirtualBaseAdjustment = 0; |
| 1328 | llvm::Constant *VirtualBaseAdjustmentOffset = 0; |
| 1329 | llvm::Constant *VBPtrOffset = 0; |
| 1330 | bool IsFunc = SrcTy->isMemberFunctionPointer(); |
| 1331 | if (!hasOnlyOneField(IsFunc, SrcInheritance)) { |
| 1332 | // We need to extract values. |
| 1333 | unsigned I = 0; |
| 1334 | FirstField = Src->getAggregateElement(I++); |
| 1335 | if (hasNonVirtualBaseAdjustmentField(IsFunc, SrcInheritance)) |
| 1336 | NonVirtualBaseAdjustment = Src->getAggregateElement(I++); |
| 1337 | if (hasVBPtrOffsetField(SrcInheritance)) |
| 1338 | VBPtrOffset = Src->getAggregateElement(I++); |
| 1339 | if (hasVirtualBaseAdjustmentField(SrcInheritance)) |
| 1340 | VirtualBaseAdjustmentOffset = Src->getAggregateElement(I++); |
| 1341 | } |
| 1342 | |
| 1343 | // For data pointers, we adjust the field offset directly. For functions, we |
| 1344 | // have a separate field. |
| 1345 | llvm::Constant *Adj = getMemberPointerAdjustment(E); |
| 1346 | if (Adj) { |
| 1347 | Adj = llvm::ConstantExpr::getTruncOrBitCast(Adj, CGM.IntTy); |
| 1348 | llvm::Constant *&NVAdjustField = |
| 1349 | IsFunc ? NonVirtualBaseAdjustment : FirstField; |
| 1350 | bool IsDerivedToBase = (E->getCastKind() == CK_DerivedToBaseMemberPointer); |
| 1351 | if (!NVAdjustField) // If this field didn't exist in src, it's zero. |
| 1352 | NVAdjustField = getZeroInt(); |
| 1353 | if (IsDerivedToBase) |
| 1354 | NVAdjustField = llvm::ConstantExpr::getNSWSub(NVAdjustField, Adj); |
| 1355 | else |
| 1356 | NVAdjustField = llvm::ConstantExpr::getNSWAdd(NVAdjustField, Adj); |
| 1357 | } |
| 1358 | |
| 1359 | // FIXME PR15713: Support conversions through virtually derived classes. |
| 1360 | |
| 1361 | // Recompose dst from the null struct and the adjusted fields from src. |
| 1362 | if (hasOnlyOneField(IsFunc, DstInheritance)) |
| 1363 | return FirstField; |
| 1364 | |
| 1365 | llvm::SmallVector<llvm::Constant *, 4> Fields; |
| 1366 | Fields.push_back(FirstField); |
| 1367 | if (hasNonVirtualBaseAdjustmentField(IsFunc, DstInheritance)) |
| 1368 | Fields.push_back(getConstantOrZeroInt(NonVirtualBaseAdjustment)); |
| 1369 | if (hasVBPtrOffsetField(DstInheritance)) |
| 1370 | Fields.push_back(getConstantOrZeroInt(VBPtrOffset)); |
| 1371 | if (hasVirtualBaseAdjustmentField(DstInheritance)) |
| 1372 | Fields.push_back(getConstantOrZeroInt(VirtualBaseAdjustmentOffset)); |
| 1373 | return llvm::ConstantStruct::getAnon(Fields); |
| 1374 | } |
| 1375 | |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 1376 | llvm::Value * |
| 1377 | MicrosoftCXXABI::EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF, |
| 1378 | llvm::Value *&This, |
| 1379 | llvm::Value *MemPtr, |
| 1380 | const MemberPointerType *MPT) { |
| 1381 | assert(MPT->isMemberFunctionPointer()); |
| 1382 | const FunctionProtoType *FPT = |
| 1383 | MPT->getPointeeType()->castAs<FunctionProtoType>(); |
| 1384 | const CXXRecordDecl *RD = MPT->getClass()->getAsCXXRecordDecl(); |
| 1385 | llvm::FunctionType *FTy = |
| 1386 | CGM.getTypes().GetFunctionType( |
| 1387 | CGM.getTypes().arrangeCXXMethodType(RD, FPT)); |
| 1388 | CGBuilderTy &Builder = CGF.Builder; |
| 1389 | |
| 1390 | MSInheritanceModel Inheritance = RD->getMSInheritanceModel(); |
| 1391 | |
| 1392 | // Extract the fields we need, regardless of model. We'll apply them if we |
| 1393 | // have them. |
| 1394 | llvm::Value *FunctionPointer = MemPtr; |
| 1395 | llvm::Value *NonVirtualBaseAdjustment = NULL; |
| 1396 | llvm::Value *VirtualBaseAdjustmentOffset = NULL; |
| 1397 | llvm::Value *VBPtrOffset = NULL; |
| 1398 | if (MemPtr->getType()->isStructTy()) { |
| 1399 | // We need to extract values. |
| 1400 | unsigned I = 0; |
| 1401 | FunctionPointer = Builder.CreateExtractValue(MemPtr, I++); |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 1402 | if (hasNonVirtualBaseAdjustmentField(MPT, Inheritance)) |
| 1403 | NonVirtualBaseAdjustment = Builder.CreateExtractValue(MemPtr, I++); |
Reid Kleckner | 79e0291 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 1404 | if (hasVBPtrOffsetField(Inheritance)) |
| 1405 | VBPtrOffset = Builder.CreateExtractValue(MemPtr, I++); |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 1406 | if (hasVirtualBaseAdjustmentField(Inheritance)) |
| 1407 | VirtualBaseAdjustmentOffset = Builder.CreateExtractValue(MemPtr, I++); |
| 1408 | } |
| 1409 | |
| 1410 | if (VirtualBaseAdjustmentOffset) { |
| 1411 | This = AdjustVirtualBase(CGF, RD, This, VirtualBaseAdjustmentOffset, |
| 1412 | VBPtrOffset); |
| 1413 | } |
| 1414 | |
| 1415 | if (NonVirtualBaseAdjustment) { |
| 1416 | // Apply the adjustment and cast back to the original struct type. |
| 1417 | llvm::Value *Ptr = Builder.CreateBitCast(This, Builder.getInt8PtrTy()); |
| 1418 | Ptr = Builder.CreateInBoundsGEP(Ptr, NonVirtualBaseAdjustment); |
| 1419 | This = Builder.CreateBitCast(Ptr, This->getType(), "this.adjusted"); |
| 1420 | } |
| 1421 | |
| 1422 | return Builder.CreateBitCast(FunctionPointer, FTy->getPointerTo()); |
| 1423 | } |
| 1424 | |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 1425 | CGCXXABI *clang::CodeGen::CreateMicrosoftCXXABI(CodeGenModule &CGM) { |
Charles Davis | c392664 | 2010-06-09 23:25:41 +0000 | [diff] [blame] | 1426 | return new MicrosoftCXXABI(CGM); |
| 1427 | } |
| 1428 | |