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