Charles Davis | 74ce859 | 2010-06-09 23:25:41 +0000 | [diff] [blame] | 1 | //===--- MicrosoftCXXABI.cpp - Emit LLVM Code from ASTs for a Module ------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 10 | // This provides C++ code generation targeting the Microsoft Visual C++ ABI. |
Charles Davis | 74ce859 | 2010-06-09 23:25:41 +0000 | [diff] [blame] | 11 | // The class in this file generates structures that follow the Microsoft |
| 12 | // Visual C++ ABI, which is actually not very well documented at all outside |
| 13 | // of Microsoft. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
| 17 | #include "CGCXXABI.h" |
| 18 | #include "CodeGenModule.h" |
Reid Kleckner | 7810af0 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 19 | #include "CGVTables.h" |
| 20 | #include "MicrosoftVBTables.h" |
Charles Davis | 74ce859 | 2010-06-09 23:25:41 +0000 | [diff] [blame] | 21 | #include "clang/AST/Decl.h" |
| 22 | #include "clang/AST/DeclCXX.h" |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 23 | #include "clang/AST/VTableBuilder.h" |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/StringSet.h" |
Charles Davis | 74ce859 | 2010-06-09 23:25:41 +0000 | [diff] [blame] | 25 | |
| 26 | using namespace clang; |
| 27 | using namespace CodeGen; |
| 28 | |
| 29 | namespace { |
| 30 | |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 31 | class MicrosoftCXXABI : public CGCXXABI { |
Charles Davis | 74ce859 | 2010-06-09 23:25:41 +0000 | [diff] [blame] | 32 | public: |
Peter Collingbourne | 0ff0b37 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 33 | MicrosoftCXXABI(CodeGenModule &CGM) : CGCXXABI(CGM) {} |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 34 | |
Stephen Lin | 9dc6eef | 2013-06-30 20:40:16 +0000 | [diff] [blame] | 35 | bool HasThisReturn(GlobalDecl GD) const; |
| 36 | |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 37 | bool isReturnTypeIndirect(const CXXRecordDecl *RD) const { |
| 38 | // Structures that are not C++03 PODs are always indirect. |
| 39 | return !RD->isPOD(); |
| 40 | } |
| 41 | |
| 42 | RecordArgABI getRecordArgABI(const CXXRecordDecl *RD) const { |
Reid Kleckner | 23f4c4b | 2013-06-21 12:45:15 +0000 | [diff] [blame] | 43 | if (RD->hasNonTrivialCopyConstructor() || RD->hasNonTrivialDestructor()) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 44 | return RAA_DirectInMemory; |
| 45 | return RAA_Default; |
| 46 | } |
| 47 | |
Joao Matos | 2ce88ef | 2012-07-17 17:10:11 +0000 | [diff] [blame] | 48 | StringRef GetPureVirtualCallName() { return "_purecall"; } |
David Blaikie | eb7d598 | 2012-10-16 22:56:05 +0000 | [diff] [blame] | 49 | // No known support for deleted functions in MSVC yet, so this choice is |
| 50 | // arbitrary. |
| 51 | StringRef GetDeletedVirtualCallName() { return "_purecall"; } |
Joao Matos | 2ce88ef | 2012-07-17 17:10:11 +0000 | [diff] [blame] | 52 | |
Hans Wennborg | feedf85 | 2013-11-21 00:15:56 +0000 | [diff] [blame] | 53 | bool isInlineInitializedStaticDataMemberLinkOnce() { return true; } |
| 54 | |
John McCall | 82fb892 | 2012-09-25 10:10:39 +0000 | [diff] [blame] | 55 | llvm::Value *adjustToCompleteObject(CodeGenFunction &CGF, |
| 56 | llvm::Value *ptr, |
| 57 | QualType type); |
| 58 | |
Reid Kleckner | d8cbeec | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 59 | llvm::Value *GetVirtualBaseClassOffset(CodeGenFunction &CGF, |
| 60 | llvm::Value *This, |
| 61 | const CXXRecordDecl *ClassDecl, |
| 62 | const CXXRecordDecl *BaseClassDecl); |
| 63 | |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 64 | void BuildConstructorSignature(const CXXConstructorDecl *Ctor, |
| 65 | CXXCtorType Type, |
| 66 | CanQualType &ResTy, |
John McCall | 0f999f3 | 2012-09-25 08:00:39 +0000 | [diff] [blame] | 67 | SmallVectorImpl<CanQualType> &ArgTys); |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 68 | |
Reid Kleckner | 7810af0 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 69 | llvm::BasicBlock *EmitCtorCompleteObjectHandler(CodeGenFunction &CGF, |
| 70 | const CXXRecordDecl *RD); |
Timur Iskhodzhanov | 57cbe5c | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 71 | |
Timur Iskhodzhanov | b648732 | 2013-10-09 18:16:58 +0000 | [diff] [blame] | 72 | void initializeHiddenVirtualInheritanceMembers(CodeGenFunction &CGF, |
| 73 | const CXXRecordDecl *RD); |
| 74 | |
Timur Iskhodzhanov | 40f2fa9 | 2013-08-04 17:30:04 +0000 | [diff] [blame] | 75 | void EmitCXXConstructors(const CXXConstructorDecl *D); |
| 76 | |
Reid Kleckner | e7de47e | 2013-07-22 13:51:44 +0000 | [diff] [blame] | 77 | // Background on MSVC destructors |
| 78 | // ============================== |
| 79 | // |
| 80 | // Both Itanium and MSVC ABIs have destructor variants. The variant names |
| 81 | // roughly correspond in the following way: |
| 82 | // Itanium Microsoft |
| 83 | // Base -> no name, just ~Class |
| 84 | // Complete -> vbase destructor |
| 85 | // Deleting -> scalar deleting destructor |
| 86 | // vector deleting destructor |
| 87 | // |
| 88 | // The base and complete destructors are the same as in Itanium, although the |
| 89 | // complete destructor does not accept a VTT parameter when there are virtual |
| 90 | // bases. A separate mechanism involving vtordisps is used to ensure that |
| 91 | // virtual methods of destroyed subobjects are not called. |
| 92 | // |
| 93 | // The deleting destructors accept an i32 bitfield as a second parameter. Bit |
| 94 | // 1 indicates if the memory should be deleted. Bit 2 indicates if the this |
| 95 | // pointer points to an array. The scalar deleting destructor assumes that |
| 96 | // bit 2 is zero, and therefore does not contain a loop. |
| 97 | // |
| 98 | // For virtual destructors, only one entry is reserved in the vftable, and it |
| 99 | // always points to the vector deleting destructor. The vector deleting |
| 100 | // destructor is the most general, so it can be used to destroy objects in |
| 101 | // place, delete single heap objects, or delete arrays. |
| 102 | // |
| 103 | // A TU defining a non-inline destructor is only guaranteed to emit a base |
| 104 | // destructor, and all of the other variants are emitted on an as-needed basis |
| 105 | // in COMDATs. Because a non-base destructor can be emitted in a TU that |
| 106 | // lacks a definition for the destructor, non-base destructors must always |
| 107 | // delegate to or alias the base destructor. |
| 108 | |
| 109 | void BuildDestructorSignature(const CXXDestructorDecl *Dtor, |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 110 | CXXDtorType Type, |
| 111 | CanQualType &ResTy, |
Timur Iskhodzhanov | ee6bc53 | 2013-02-13 08:37:51 +0000 | [diff] [blame] | 112 | SmallVectorImpl<CanQualType> &ArgTys); |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 113 | |
Reid Kleckner | e7de47e | 2013-07-22 13:51:44 +0000 | [diff] [blame] | 114 | /// Non-base dtors should be emitted as delegating thunks in this ABI. |
| 115 | bool useThunkForDtorVariant(const CXXDestructorDecl *Dtor, |
| 116 | CXXDtorType DT) const { |
| 117 | return DT != Dtor_Base; |
| 118 | } |
| 119 | |
| 120 | void EmitCXXDestructors(const CXXDestructorDecl *D); |
| 121 | |
Timur Iskhodzhanov | 88fd439 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 122 | const CXXRecordDecl *getThisArgumentTypeForMethod(const CXXMethodDecl *MD) { |
| 123 | MD = MD->getCanonicalDecl(); |
| 124 | if (MD->isVirtual() && !isa<CXXDestructorDecl>(MD)) { |
Timur Iskhodzhanov | 5877663 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 125 | MicrosoftVTableContext::MethodVFTableLocation ML = |
| 126 | CGM.getMicrosoftVTableContext().getMethodVFTableLocation(MD); |
Timur Iskhodzhanov | 88fd439 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 127 | // The vbases might be ordered differently in the final overrider object |
| 128 | // and the complete object, so the "this" argument may sometimes point to |
| 129 | // memory that has no particular type (e.g. past the complete object). |
| 130 | // In this case, we just use a generic pointer type. |
| 131 | // FIXME: might want to have a more precise type in the non-virtual |
| 132 | // multiple inheritance case. |
Timur Iskhodzhanov | 9e7f505 | 2013-11-07 13:34:02 +0000 | [diff] [blame] | 133 | if (ML.VBase || !ML.VFPtrOffset.isZero()) |
Timur Iskhodzhanov | 88fd439 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 134 | return 0; |
| 135 | } |
| 136 | return MD->getParent(); |
| 137 | } |
| 138 | |
| 139 | llvm::Value *adjustThisArgumentForVirtualCall(CodeGenFunction &CGF, |
| 140 | GlobalDecl GD, |
| 141 | llvm::Value *This); |
| 142 | |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 143 | void BuildInstanceFunctionParams(CodeGenFunction &CGF, |
| 144 | QualType &ResTy, |
John McCall | 0f999f3 | 2012-09-25 08:00:39 +0000 | [diff] [blame] | 145 | FunctionArgList &Params); |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 146 | |
Timur Iskhodzhanov | 88fd439 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 147 | llvm::Value *adjustThisParameterInVirtualFunctionPrologue( |
| 148 | CodeGenFunction &CGF, GlobalDecl GD, llvm::Value *This); |
| 149 | |
John McCall | 0f999f3 | 2012-09-25 08:00:39 +0000 | [diff] [blame] | 150 | void EmitInstanceFunctionProlog(CodeGenFunction &CGF); |
John McCall | 2903675 | 2011-01-27 02:46:02 +0000 | [diff] [blame] | 151 | |
Stephen Lin | 9dc6eef | 2013-06-30 20:40:16 +0000 | [diff] [blame] | 152 | void EmitConstructorCall(CodeGenFunction &CGF, |
| 153 | const CXXConstructorDecl *D, CXXCtorType Type, |
| 154 | bool ForVirtualBase, bool Delegating, |
Stephen Lin | c467c87 | 2013-06-19 18:10:35 +0000 | [diff] [blame] | 155 | llvm::Value *This, |
| 156 | CallExpr::const_arg_iterator ArgBeg, |
| 157 | CallExpr::const_arg_iterator ArgEnd); |
Timur Iskhodzhanov | 88fd439 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 158 | |
Reid Kleckner | 6fe771a | 2013-12-13 00:53:54 +0000 | [diff] [blame^] | 159 | void EmitDestructorCall(CodeGenFunction &CGF, const CXXDestructorDecl *DD, |
| 160 | CXXDtorType Type, bool ForVirtualBase, |
| 161 | bool Delegating, llvm::Value *This); |
| 162 | |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 163 | void emitVTableDefinitions(CodeGenVTables &CGVT, const CXXRecordDecl *RD); |
| 164 | |
| 165 | llvm::Value *getVTableAddressPointInStructor( |
| 166 | CodeGenFunction &CGF, const CXXRecordDecl *VTableClass, |
| 167 | BaseSubobject Base, const CXXRecordDecl *NearestVBase, |
| 168 | bool &NeedsVirtualOffset); |
| 169 | |
| 170 | llvm::Constant * |
| 171 | getVTableAddressPointForConstExpr(BaseSubobject Base, |
| 172 | const CXXRecordDecl *VTableClass); |
| 173 | |
| 174 | llvm::GlobalVariable *getAddrOfVTable(const CXXRecordDecl *RD, |
| 175 | CharUnits VPtrOffset); |
| 176 | |
Timur Iskhodzhanov | 88fd439 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 177 | llvm::Value *getVirtualFunctionPointer(CodeGenFunction &CGF, GlobalDecl GD, |
| 178 | llvm::Value *This, llvm::Type *Ty); |
| 179 | |
Stephen Lin | 9dc6eef | 2013-06-30 20:40:16 +0000 | [diff] [blame] | 180 | void EmitVirtualDestructorCall(CodeGenFunction &CGF, |
| 181 | const CXXDestructorDecl *Dtor, |
| 182 | CXXDtorType DtorType, SourceLocation CallLoc, |
| 183 | llvm::Value *This); |
Timur Iskhodzhanov | d619711 | 2013-02-15 14:45:22 +0000 | [diff] [blame] | 184 | |
Timur Iskhodzhanov | ad9d3b8 | 2013-10-09 09:23:58 +0000 | [diff] [blame] | 185 | void adjustCallArgsForDestructorThunk(CodeGenFunction &CGF, GlobalDecl GD, |
| 186 | CallArgList &CallArgs) { |
| 187 | assert(GD.getDtorType() == Dtor_Deleting && |
| 188 | "Only deleting destructor thunks are available in this ABI"); |
| 189 | CallArgs.add(RValue::get(getStructorImplicitParamValue(CGF)), |
| 190 | CGM.getContext().IntTy); |
| 191 | } |
| 192 | |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 193 | void emitVirtualInheritanceTables(const CXXRecordDecl *RD); |
Reid Kleckner | 7810af0 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 194 | |
Timur Iskhodzhanov | ad9d3b8 | 2013-10-09 09:23:58 +0000 | [diff] [blame] | 195 | void setThunkLinkage(llvm::Function *Thunk, bool ForVTable) { |
| 196 | Thunk->setLinkage(llvm::GlobalValue::WeakAnyLinkage); |
| 197 | } |
| 198 | |
Timur Iskhodzhanov | 0201432 | 2013-10-30 11:55:43 +0000 | [diff] [blame] | 199 | llvm::Value *performThisAdjustment(CodeGenFunction &CGF, llvm::Value *This, |
| 200 | const ThisAdjustment &TA); |
| 201 | |
| 202 | llvm::Value *performReturnAdjustment(CodeGenFunction &CGF, llvm::Value *Ret, |
| 203 | const ReturnAdjustment &RA); |
| 204 | |
John McCall | c84ed6a | 2012-05-01 06:13:13 +0000 | [diff] [blame] | 205 | void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D, |
| 206 | llvm::GlobalVariable *DeclPtr, |
| 207 | bool PerformInit); |
| 208 | |
John McCall | 2903675 | 2011-01-27 02:46:02 +0000 | [diff] [blame] | 209 | // ==== Notes on array cookies ========= |
| 210 | // |
| 211 | // MSVC seems to only use cookies when the class has a destructor; a |
| 212 | // two-argument usual array deallocation function isn't sufficient. |
| 213 | // |
| 214 | // For example, this code prints "100" and "1": |
| 215 | // struct A { |
| 216 | // char x; |
| 217 | // void *operator new[](size_t sz) { |
| 218 | // printf("%u\n", sz); |
| 219 | // return malloc(sz); |
| 220 | // } |
| 221 | // void operator delete[](void *p, size_t sz) { |
| 222 | // printf("%u\n", sz); |
| 223 | // free(p); |
| 224 | // } |
| 225 | // }; |
| 226 | // int main() { |
| 227 | // A *p = new A[100]; |
| 228 | // delete[] p; |
| 229 | // } |
| 230 | // Whereas it prints "104" and "104" if you give A a destructor. |
John McCall | b91cd66 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 231 | |
| 232 | bool requiresArrayCookie(const CXXDeleteExpr *expr, QualType elementType); |
| 233 | bool requiresArrayCookie(const CXXNewExpr *expr); |
| 234 | CharUnits getArrayCookieSizeImpl(QualType type); |
| 235 | llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF, |
| 236 | llvm::Value *NewPtr, |
| 237 | llvm::Value *NumElements, |
| 238 | const CXXNewExpr *expr, |
| 239 | QualType ElementType); |
| 240 | llvm::Value *readArrayCookieImpl(CodeGenFunction &CGF, |
| 241 | llvm::Value *allocPtr, |
| 242 | CharUnits cookieSize); |
Reid Kleckner | 407e8b6 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 243 | |
| 244 | private: |
Timur Iskhodzhanov | 6745522 | 2013-10-03 06:26:13 +0000 | [diff] [blame] | 245 | MicrosoftMangleContext &getMangleContext() { |
| 246 | return cast<MicrosoftMangleContext>(CodeGen::CGCXXABI::getMangleContext()); |
| 247 | } |
| 248 | |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 249 | llvm::Constant *getZeroInt() { |
| 250 | return llvm::ConstantInt::get(CGM.IntTy, 0); |
Reid Kleckner | 407e8b6 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 253 | llvm::Constant *getAllOnesInt() { |
| 254 | return llvm::Constant::getAllOnesValue(CGM.IntTy); |
Reid Kleckner | 407e8b6 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 255 | } |
| 256 | |
Reid Kleckner | 452abac | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 257 | llvm::Constant *getConstantOrZeroInt(llvm::Constant *C) { |
| 258 | return C ? C : getZeroInt(); |
| 259 | } |
| 260 | |
| 261 | llvm::Value *getValueOrZeroInt(llvm::Value *C) { |
| 262 | return C ? C : getZeroInt(); |
| 263 | } |
| 264 | |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 265 | void |
| 266 | GetNullMemberPointerFields(const MemberPointerType *MPT, |
| 267 | llvm::SmallVectorImpl<llvm::Constant *> &fields); |
| 268 | |
Reid Kleckner | d8cbeec | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 269 | /// \brief Finds the offset from the base of RD to the vbptr it uses, even if |
| 270 | /// it is reusing a vbptr from a non-virtual base. RD must have morally |
| 271 | /// virtual bases. |
| 272 | CharUnits GetVBPtrOffsetFromBases(const CXXRecordDecl *RD); |
| 273 | |
| 274 | /// \brief Shared code for virtual base adjustment. Returns the offset from |
| 275 | /// the vbptr to the virtual base. Optionally returns the address of the |
| 276 | /// vbptr itself. |
| 277 | llvm::Value *GetVBaseOffsetFromVBPtr(CodeGenFunction &CGF, |
| 278 | llvm::Value *Base, |
| 279 | llvm::Value *VBPtrOffset, |
| 280 | llvm::Value *VBTableOffset, |
| 281 | llvm::Value **VBPtr = 0); |
| 282 | |
Timur Iskhodzhanov | 0201432 | 2013-10-30 11:55:43 +0000 | [diff] [blame] | 283 | llvm::Value *GetVBaseOffsetFromVBPtr(CodeGenFunction &CGF, |
| 284 | llvm::Value *Base, |
| 285 | int32_t VBPtrOffset, |
| 286 | int32_t VBTableOffset, |
| 287 | llvm::Value **VBPtr = 0) { |
| 288 | llvm::Value *VBPOffset = llvm::ConstantInt::get(CGM.IntTy, VBPtrOffset), |
| 289 | *VBTOffset = llvm::ConstantInt::get(CGM.IntTy, VBTableOffset); |
| 290 | return GetVBaseOffsetFromVBPtr(CGF, Base, VBPOffset, VBTOffset, VBPtr); |
| 291 | } |
| 292 | |
Reid Kleckner | d8cbeec | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 293 | /// \brief Performs a full virtual base adjustment. Used to dereference |
| 294 | /// pointers to members of virtual bases. |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 295 | llvm::Value *AdjustVirtualBase(CodeGenFunction &CGF, const CXXRecordDecl *RD, |
| 296 | llvm::Value *Base, |
| 297 | llvm::Value *VirtualBaseAdjustmentOffset, |
| 298 | llvm::Value *VBPtrOffset /* optional */); |
| 299 | |
Reid Kleckner | 7d0efb5 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 300 | /// \brief Emits a full member pointer with the fields common to data and |
| 301 | /// function member pointers. |
| 302 | llvm::Constant *EmitFullMemberPointer(llvm::Constant *FirstField, |
| 303 | bool IsMemberFunction, |
Reid Kleckner | 452abac | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 304 | const CXXRecordDecl *RD, |
| 305 | CharUnits NonVirtualBaseAdjustment); |
| 306 | |
| 307 | llvm::Constant *BuildMemberPointer(const CXXRecordDecl *RD, |
| 308 | const CXXMethodDecl *MD, |
| 309 | CharUnits NonVirtualBaseAdjustment); |
| 310 | |
| 311 | bool MemberPointerConstantIsNull(const MemberPointerType *MPT, |
| 312 | llvm::Constant *MP); |
Reid Kleckner | 7d0efb5 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 313 | |
Reid Kleckner | 7810af0 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 314 | /// \brief - Initialize all vbptrs of 'this' with RD as the complete type. |
| 315 | void EmitVBPtrStores(CodeGenFunction &CGF, const CXXRecordDecl *RD); |
| 316 | |
| 317 | /// \brief Caching wrapper around VBTableBuilder::enumerateVBTables(). |
| 318 | const VBTableVector &EnumerateVBTables(const CXXRecordDecl *RD); |
| 319 | |
Hans Wennborg | 88497d6 | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 320 | /// \brief Generate a thunk for calling a virtual member function MD. |
| 321 | llvm::Function *EmitVirtualMemPtrThunk(const CXXMethodDecl *MD, |
| 322 | StringRef ThunkName); |
| 323 | |
Reid Kleckner | 407e8b6 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 324 | public: |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 325 | virtual llvm::Type *ConvertMemberPointerType(const MemberPointerType *MPT); |
| 326 | |
| 327 | virtual bool isZeroInitializable(const MemberPointerType *MPT); |
| 328 | |
Reid Kleckner | 407e8b6 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 329 | virtual llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT); |
| 330 | |
| 331 | virtual llvm::Constant *EmitMemberDataPointer(const MemberPointerType *MPT, |
| 332 | CharUnits offset); |
Reid Kleckner | 7d0efb5 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 333 | virtual llvm::Constant *EmitMemberPointer(const CXXMethodDecl *MD); |
| 334 | virtual llvm::Constant *EmitMemberPointer(const APValue &MP, QualType MPT); |
Reid Kleckner | 407e8b6 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 335 | |
Reid Kleckner | 700c3ee | 2013-04-30 20:15:14 +0000 | [diff] [blame] | 336 | virtual llvm::Value *EmitMemberPointerComparison(CodeGenFunction &CGF, |
| 337 | llvm::Value *L, |
| 338 | llvm::Value *R, |
| 339 | const MemberPointerType *MPT, |
| 340 | bool Inequality); |
| 341 | |
Reid Kleckner | 407e8b6 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 342 | virtual llvm::Value *EmitMemberPointerIsNotNull(CodeGenFunction &CGF, |
| 343 | llvm::Value *MemPtr, |
| 344 | const MemberPointerType *MPT); |
| 345 | |
| 346 | virtual llvm::Value *EmitMemberDataPointerAddress(CodeGenFunction &CGF, |
| 347 | llvm::Value *Base, |
| 348 | llvm::Value *MemPtr, |
| 349 | const MemberPointerType *MPT); |
| 350 | |
Reid Kleckner | 452abac | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 351 | virtual llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF, |
| 352 | const CastExpr *E, |
| 353 | llvm::Value *Src); |
| 354 | |
| 355 | virtual llvm::Constant *EmitMemberPointerConversion(const CastExpr *E, |
| 356 | llvm::Constant *Src); |
| 357 | |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 358 | virtual llvm::Value * |
| 359 | EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF, |
| 360 | llvm::Value *&This, |
| 361 | llvm::Value *MemPtr, |
| 362 | const MemberPointerType *MPT); |
| 363 | |
Reid Kleckner | 7810af0 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 364 | private: |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 365 | typedef std::pair<const CXXRecordDecl *, CharUnits> VFTableIdTy; |
| 366 | typedef llvm::DenseMap<VFTableIdTy, llvm::GlobalVariable *> VFTablesMapTy; |
| 367 | /// \brief All the vftables that have been referenced. |
| 368 | VFTablesMapTy VFTablesMap; |
| 369 | |
| 370 | /// \brief This set holds the record decls we've deferred vtable emission for. |
| 371 | llvm::SmallPtrSet<const CXXRecordDecl *, 4> DeferredVFTables; |
| 372 | |
| 373 | |
| 374 | /// \brief All the vbtables which have been referenced. |
Reid Kleckner | 7810af0 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 375 | llvm::DenseMap<const CXXRecordDecl *, VBTableVector> VBTablesMap; |
Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 376 | |
| 377 | /// Info on the global variable used to guard initialization of static locals. |
| 378 | /// The BitIndex field is only used for externally invisible declarations. |
| 379 | struct GuardInfo { |
| 380 | GuardInfo() : Guard(0), BitIndex(0) {} |
| 381 | llvm::GlobalVariable *Guard; |
| 382 | unsigned BitIndex; |
| 383 | }; |
| 384 | |
| 385 | /// Map from DeclContext to the current guard variable. We assume that the |
| 386 | /// AST is visited in source code order. |
| 387 | llvm::DenseMap<const DeclContext *, GuardInfo> GuardVariableMap; |
Charles Davis | 74ce859 | 2010-06-09 23:25:41 +0000 | [diff] [blame] | 388 | }; |
| 389 | |
| 390 | } |
| 391 | |
John McCall | 82fb892 | 2012-09-25 10:10:39 +0000 | [diff] [blame] | 392 | llvm::Value *MicrosoftCXXABI::adjustToCompleteObject(CodeGenFunction &CGF, |
| 393 | llvm::Value *ptr, |
| 394 | QualType type) { |
| 395 | // FIXME: implement |
| 396 | return ptr; |
| 397 | } |
| 398 | |
Reid Kleckner | 3758f9d | 2013-06-04 21:32:29 +0000 | [diff] [blame] | 399 | /// \brief Finds the first non-virtual base of RD that has virtual bases. If RD |
| 400 | /// doesn't have a vbptr, it will reuse the vbptr of the returned class. |
| 401 | static const CXXRecordDecl *FindFirstNVBaseWithVBases(const CXXRecordDecl *RD) { |
| 402 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 403 | E = RD->bases_end(); I != E; ++I) { |
| 404 | const CXXRecordDecl *Base = I->getType()->getAsCXXRecordDecl(); |
| 405 | if (!I->isVirtual() && Base->getNumVBases() > 0) |
| 406 | return Base; |
| 407 | } |
| 408 | llvm_unreachable("RD must have an nv base with vbases"); |
| 409 | } |
| 410 | |
Reid Kleckner | d8cbeec | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 411 | CharUnits MicrosoftCXXABI::GetVBPtrOffsetFromBases(const CXXRecordDecl *RD) { |
| 412 | assert(RD->getNumVBases()); |
| 413 | CharUnits Total = CharUnits::Zero(); |
| 414 | while (RD) { |
| 415 | const ASTRecordLayout &RDLayout = getContext().getASTRecordLayout(RD); |
| 416 | CharUnits VBPtrOffset = RDLayout.getVBPtrOffset(); |
| 417 | // -1 is the sentinel for no vbptr. |
| 418 | if (VBPtrOffset != CharUnits::fromQuantity(-1)) { |
| 419 | Total += VBPtrOffset; |
| 420 | break; |
| 421 | } |
Reid Kleckner | 3758f9d | 2013-06-04 21:32:29 +0000 | [diff] [blame] | 422 | RD = FindFirstNVBaseWithVBases(RD); |
| 423 | Total += RDLayout.getBaseClassOffset(RD); |
Reid Kleckner | d8cbeec | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 424 | } |
| 425 | return Total; |
| 426 | } |
| 427 | |
| 428 | llvm::Value * |
| 429 | MicrosoftCXXABI::GetVirtualBaseClassOffset(CodeGenFunction &CGF, |
| 430 | llvm::Value *This, |
| 431 | const CXXRecordDecl *ClassDecl, |
| 432 | const CXXRecordDecl *BaseClassDecl) { |
| 433 | int64_t VBPtrChars = GetVBPtrOffsetFromBases(ClassDecl).getQuantity(); |
| 434 | llvm::Value *VBPtrOffset = llvm::ConstantInt::get(CGM.PtrDiffTy, VBPtrChars); |
Reid Kleckner | d8cbeec | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 435 | CharUnits IntSize = getContext().getTypeSizeInChars(getContext().IntTy); |
Timur Iskhodzhanov | 5877663 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 436 | CharUnits VBTableChars = |
| 437 | IntSize * |
| 438 | CGM.getMicrosoftVTableContext().getVBTableIndex(ClassDecl, BaseClassDecl); |
Reid Kleckner | d8cbeec | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 439 | llvm::Value *VBTableOffset = |
| 440 | llvm::ConstantInt::get(CGM.IntTy, VBTableChars.getQuantity()); |
| 441 | |
| 442 | llvm::Value *VBPtrToNewBase = |
Timur Iskhodzhanov | 07e6eff | 2013-10-27 17:10:27 +0000 | [diff] [blame] | 443 | GetVBaseOffsetFromVBPtr(CGF, This, VBPtrOffset, VBTableOffset); |
Reid Kleckner | d8cbeec | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 444 | VBPtrToNewBase = |
| 445 | CGF.Builder.CreateSExtOrBitCast(VBPtrToNewBase, CGM.PtrDiffTy); |
| 446 | return CGF.Builder.CreateNSWAdd(VBPtrOffset, VBPtrToNewBase); |
| 447 | } |
| 448 | |
Stephen Lin | 9dc6eef | 2013-06-30 20:40:16 +0000 | [diff] [blame] | 449 | bool MicrosoftCXXABI::HasThisReturn(GlobalDecl GD) const { |
| 450 | return isa<CXXConstructorDecl>(GD.getDecl()); |
John McCall | 0f999f3 | 2012-09-25 08:00:39 +0000 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | void MicrosoftCXXABI::BuildConstructorSignature(const CXXConstructorDecl *Ctor, |
| 454 | CXXCtorType Type, |
| 455 | CanQualType &ResTy, |
| 456 | SmallVectorImpl<CanQualType> &ArgTys) { |
Stephen Lin | 9dc6eef | 2013-06-30 20:40:16 +0000 | [diff] [blame] | 457 | // 'this' parameter and 'this' return are already in place |
Timur Iskhodzhanov | 57cbe5c | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 458 | |
| 459 | const CXXRecordDecl *Class = Ctor->getParent(); |
| 460 | if (Class->getNumVBases()) { |
| 461 | // Constructors of classes with virtual bases take an implicit parameter. |
| 462 | ArgTys.push_back(CGM.getContext().IntTy); |
| 463 | } |
| 464 | } |
| 465 | |
Reid Kleckner | 7810af0 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 466 | llvm::BasicBlock * |
| 467 | MicrosoftCXXABI::EmitCtorCompleteObjectHandler(CodeGenFunction &CGF, |
| 468 | const CXXRecordDecl *RD) { |
Timur Iskhodzhanov | 57cbe5c | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 469 | llvm::Value *IsMostDerivedClass = getStructorImplicitParamValue(CGF); |
| 470 | assert(IsMostDerivedClass && |
| 471 | "ctor for a class with virtual bases must have an implicit parameter"); |
Reid Kleckner | 7810af0 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 472 | llvm::Value *IsCompleteObject = |
| 473 | CGF.Builder.CreateIsNotNull(IsMostDerivedClass, "is_complete_object"); |
Timur Iskhodzhanov | 57cbe5c | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 474 | |
| 475 | llvm::BasicBlock *CallVbaseCtorsBB = CGF.createBasicBlock("ctor.init_vbases"); |
| 476 | llvm::BasicBlock *SkipVbaseCtorsBB = CGF.createBasicBlock("ctor.skip_vbases"); |
| 477 | CGF.Builder.CreateCondBr(IsCompleteObject, |
| 478 | CallVbaseCtorsBB, SkipVbaseCtorsBB); |
| 479 | |
| 480 | CGF.EmitBlock(CallVbaseCtorsBB); |
Reid Kleckner | 7810af0 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 481 | |
| 482 | // Fill in the vbtable pointers here. |
| 483 | EmitVBPtrStores(CGF, RD); |
Timur Iskhodzhanov | 57cbe5c | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 484 | |
| 485 | // CGF will put the base ctor calls in this basic block for us later. |
| 486 | |
| 487 | return SkipVbaseCtorsBB; |
John McCall | 0f999f3 | 2012-09-25 08:00:39 +0000 | [diff] [blame] | 488 | } |
| 489 | |
Timur Iskhodzhanov | b648732 | 2013-10-09 18:16:58 +0000 | [diff] [blame] | 490 | void MicrosoftCXXABI::initializeHiddenVirtualInheritanceMembers( |
| 491 | CodeGenFunction &CGF, const CXXRecordDecl *RD) { |
| 492 | // In most cases, an override for a vbase virtual method can adjust |
| 493 | // the "this" parameter by applying a constant offset. |
| 494 | // However, this is not enough while a constructor or a destructor of some |
| 495 | // class X is being executed if all the following conditions are met: |
| 496 | // - X has virtual bases, (1) |
| 497 | // - X overrides a virtual method M of a vbase Y, (2) |
| 498 | // - X itself is a vbase of the most derived class. |
| 499 | // |
| 500 | // If (1) and (2) are true, the vtorDisp for vbase Y is a hidden member of X |
| 501 | // which holds the extra amount of "this" adjustment we must do when we use |
| 502 | // the X vftables (i.e. during X ctor or dtor). |
| 503 | // Outside the ctors and dtors, the values of vtorDisps are zero. |
| 504 | |
| 505 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
| 506 | typedef ASTRecordLayout::VBaseOffsetsMapTy VBOffsets; |
| 507 | const VBOffsets &VBaseMap = Layout.getVBaseOffsetsMap(); |
| 508 | CGBuilderTy &Builder = CGF.Builder; |
| 509 | |
| 510 | unsigned AS = |
| 511 | cast<llvm::PointerType>(getThisValue(CGF)->getType())->getAddressSpace(); |
| 512 | llvm::Value *Int8This = 0; // Initialize lazily. |
| 513 | |
| 514 | for (VBOffsets::const_iterator I = VBaseMap.begin(), E = VBaseMap.end(); |
| 515 | I != E; ++I) { |
| 516 | if (!I->second.hasVtorDisp()) |
| 517 | continue; |
| 518 | |
Timur Iskhodzhanov | 4ddf592 | 2013-11-13 16:03:43 +0000 | [diff] [blame] | 519 | llvm::Value *VBaseOffset = |
| 520 | GetVirtualBaseClassOffset(CGF, getThisValue(CGF), RD, I->first); |
Timur Iskhodzhanov | b648732 | 2013-10-09 18:16:58 +0000 | [diff] [blame] | 521 | // FIXME: it doesn't look right that we SExt in GetVirtualBaseClassOffset() |
| 522 | // just to Trunc back immediately. |
| 523 | VBaseOffset = Builder.CreateTruncOrBitCast(VBaseOffset, CGF.Int32Ty); |
| 524 | uint64_t ConstantVBaseOffset = |
| 525 | Layout.getVBaseClassOffset(I->first).getQuantity(); |
| 526 | |
| 527 | // vtorDisp_for_vbase = vbptr[vbase_idx] - offsetof(RD, vbase). |
| 528 | llvm::Value *VtorDispValue = Builder.CreateSub( |
| 529 | VBaseOffset, llvm::ConstantInt::get(CGM.Int32Ty, ConstantVBaseOffset), |
| 530 | "vtordisp.value"); |
| 531 | |
| 532 | if (!Int8This) |
| 533 | Int8This = Builder.CreateBitCast(getThisValue(CGF), |
| 534 | CGF.Int8Ty->getPointerTo(AS)); |
| 535 | llvm::Value *VtorDispPtr = Builder.CreateInBoundsGEP(Int8This, VBaseOffset); |
| 536 | // vtorDisp is always the 32-bits before the vbase in the class layout. |
| 537 | VtorDispPtr = Builder.CreateConstGEP1_32(VtorDispPtr, -4); |
| 538 | VtorDispPtr = Builder.CreateBitCast( |
| 539 | VtorDispPtr, CGF.Int32Ty->getPointerTo(AS), "vtordisp.ptr"); |
| 540 | |
| 541 | Builder.CreateStore(VtorDispValue, VtorDispPtr); |
| 542 | } |
| 543 | } |
| 544 | |
Timur Iskhodzhanov | 40f2fa9 | 2013-08-04 17:30:04 +0000 | [diff] [blame] | 545 | void MicrosoftCXXABI::EmitCXXConstructors(const CXXConstructorDecl *D) { |
| 546 | // There's only one constructor type in this ABI. |
| 547 | CGM.EmitGlobal(GlobalDecl(D, Ctor_Complete)); |
| 548 | } |
| 549 | |
Reid Kleckner | 7810af0 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 550 | void MicrosoftCXXABI::EmitVBPtrStores(CodeGenFunction &CGF, |
| 551 | const CXXRecordDecl *RD) { |
| 552 | llvm::Value *ThisInt8Ptr = |
| 553 | CGF.Builder.CreateBitCast(getThisValue(CGF), CGM.Int8PtrTy, "this.int8"); |
| 554 | |
| 555 | const VBTableVector &VBTables = EnumerateVBTables(RD); |
| 556 | for (VBTableVector::const_iterator I = VBTables.begin(), E = VBTables.end(); |
| 557 | I != E; ++I) { |
| 558 | const ASTRecordLayout &SubobjectLayout = |
| 559 | CGM.getContext().getASTRecordLayout(I->VBPtrSubobject.getBase()); |
| 560 | uint64_t Offs = (I->VBPtrSubobject.getBaseOffset() + |
| 561 | SubobjectLayout.getVBPtrOffset()).getQuantity(); |
| 562 | llvm::Value *VBPtr = |
| 563 | CGF.Builder.CreateConstInBoundsGEP1_64(ThisInt8Ptr, Offs); |
| 564 | VBPtr = CGF.Builder.CreateBitCast(VBPtr, I->GV->getType()->getPointerTo(0), |
| 565 | "vbptr." + I->ReusingBase->getName()); |
| 566 | CGF.Builder.CreateStore(I->GV, VBPtr); |
| 567 | } |
| 568 | } |
| 569 | |
Timur Iskhodzhanov | ee6bc53 | 2013-02-13 08:37:51 +0000 | [diff] [blame] | 570 | void MicrosoftCXXABI::BuildDestructorSignature(const CXXDestructorDecl *Dtor, |
| 571 | CXXDtorType Type, |
| 572 | CanQualType &ResTy, |
| 573 | SmallVectorImpl<CanQualType> &ArgTys) { |
| 574 | // 'this' is already in place |
Stephen Lin | 9dc6eef | 2013-06-30 20:40:16 +0000 | [diff] [blame] | 575 | |
Timur Iskhodzhanov | ee6bc53 | 2013-02-13 08:37:51 +0000 | [diff] [blame] | 576 | // TODO: 'for base' flag |
| 577 | |
| 578 | if (Type == Dtor_Deleting) { |
Timur Iskhodzhanov | 701981f | 2013-08-27 10:38:19 +0000 | [diff] [blame] | 579 | // The scalar deleting destructor takes an implicit int parameter. |
| 580 | ArgTys.push_back(CGM.getContext().IntTy); |
Timur Iskhodzhanov | ee6bc53 | 2013-02-13 08:37:51 +0000 | [diff] [blame] | 581 | } |
| 582 | } |
| 583 | |
Reid Kleckner | e7de47e | 2013-07-22 13:51:44 +0000 | [diff] [blame] | 584 | void MicrosoftCXXABI::EmitCXXDestructors(const CXXDestructorDecl *D) { |
| 585 | // The TU defining a dtor is only guaranteed to emit a base destructor. All |
| 586 | // other destructor variants are delegating thunks. |
| 587 | CGM.EmitGlobal(GlobalDecl(D, Dtor_Base)); |
| 588 | } |
| 589 | |
Timur Iskhodzhanov | 88fd439 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 590 | llvm::Value *MicrosoftCXXABI::adjustThisArgumentForVirtualCall( |
| 591 | CodeGenFunction &CGF, GlobalDecl GD, llvm::Value *This) { |
| 592 | GD = GD.getCanonicalDecl(); |
| 593 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
Timur Iskhodzhanov | 62082b7 | 2013-10-16 18:24:06 +0000 | [diff] [blame] | 594 | // FIXME: consider splitting the vdtor vs regular method code into two |
| 595 | // functions. |
Timur Iskhodzhanov | 88fd439 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 596 | |
Timur Iskhodzhanov | 62082b7 | 2013-10-16 18:24:06 +0000 | [diff] [blame] | 597 | GlobalDecl LookupGD = GD; |
| 598 | if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) { |
| 599 | // Complete dtors take a pointer to the complete object, |
| 600 | // thus don't need adjustment. |
| 601 | if (GD.getDtorType() == Dtor_Complete) |
| 602 | return This; |
| 603 | |
| 604 | // There's only Dtor_Deleting in vftable but it shares the this adjustment |
| 605 | // with the base one, so look up the deleting one instead. |
| 606 | LookupGD = GlobalDecl(DD, Dtor_Deleting); |
| 607 | } |
Timur Iskhodzhanov | 5877663 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 608 | MicrosoftVTableContext::MethodVFTableLocation ML = |
| 609 | CGM.getMicrosoftVTableContext().getMethodVFTableLocation(LookupGD); |
Timur Iskhodzhanov | 88fd439 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 610 | |
| 611 | unsigned AS = cast<llvm::PointerType>(This->getType())->getAddressSpace(); |
| 612 | llvm::Type *charPtrTy = CGF.Int8Ty->getPointerTo(AS); |
Timur Iskhodzhanov | 9e7f505 | 2013-11-07 13:34:02 +0000 | [diff] [blame] | 613 | CharUnits StaticOffset = ML.VFPtrOffset; |
Timur Iskhodzhanov | 62082b7 | 2013-10-16 18:24:06 +0000 | [diff] [blame] | 614 | if (ML.VBase) { |
| 615 | bool AvoidVirtualOffset = false; |
| 616 | if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() == Dtor_Base) { |
| 617 | // A base destructor can only be called from a complete destructor of the |
Timur Iskhodzhanov | 406a479 | 2013-10-17 09:11:45 +0000 | [diff] [blame] | 618 | // same record type or another destructor of a more derived type; |
| 619 | // or a constructor of the same record type if an exception is thrown. |
| 620 | assert(isa<CXXDestructorDecl>(CGF.CurGD.getDecl()) || |
| 621 | isa<CXXConstructorDecl>(CGF.CurGD.getDecl())); |
Timur Iskhodzhanov | 62082b7 | 2013-10-16 18:24:06 +0000 | [diff] [blame] | 622 | const CXXRecordDecl *CurRD = |
Timur Iskhodzhanov | 406a479 | 2013-10-17 09:11:45 +0000 | [diff] [blame] | 623 | cast<CXXMethodDecl>(CGF.CurGD.getDecl())->getParent(); |
Timur Iskhodzhanov | 62082b7 | 2013-10-16 18:24:06 +0000 | [diff] [blame] | 624 | |
| 625 | if (MD->getParent() == CurRD) { |
Timur Iskhodzhanov | 406a479 | 2013-10-17 09:11:45 +0000 | [diff] [blame] | 626 | if (isa<CXXDestructorDecl>(CGF.CurGD.getDecl())) |
| 627 | assert(CGF.CurGD.getDtorType() == Dtor_Complete); |
| 628 | if (isa<CXXConstructorDecl>(CGF.CurGD.getDecl())) |
| 629 | assert(CGF.CurGD.getCtorType() == Ctor_Complete); |
| 630 | // We're calling the main base dtor from a complete structor, |
| 631 | // so we know the "this" offset statically. |
Timur Iskhodzhanov | 62082b7 | 2013-10-16 18:24:06 +0000 | [diff] [blame] | 632 | AvoidVirtualOffset = true; |
| 633 | } else { |
| 634 | // Let's see if we try to call a destructor of a non-virtual base. |
| 635 | for (CXXRecordDecl::base_class_const_iterator I = CurRD->bases_begin(), |
| 636 | E = CurRD->bases_end(); I != E; ++I) { |
| 637 | if (I->getType()->getAsCXXRecordDecl() != MD->getParent()) |
| 638 | continue; |
| 639 | // If we call a base destructor for a non-virtual base, we statically |
| 640 | // know where it expects the vfptr and "this" to be. |
Timur Iskhodzhanov | 406a479 | 2013-10-17 09:11:45 +0000 | [diff] [blame] | 641 | // The total offset should reflect the adjustment done by |
| 642 | // adjustThisParameterInVirtualFunctionPrologue(). |
Timur Iskhodzhanov | 62082b7 | 2013-10-16 18:24:06 +0000 | [diff] [blame] | 643 | AvoidVirtualOffset = true; |
| 644 | break; |
| 645 | } |
| 646 | } |
| 647 | } |
| 648 | |
| 649 | if (AvoidVirtualOffset) { |
| 650 | const ASTRecordLayout &Layout = |
| 651 | CGF.getContext().getASTRecordLayout(MD->getParent()); |
Timur Iskhodzhanov | 62082b7 | 2013-10-16 18:24:06 +0000 | [diff] [blame] | 652 | StaticOffset += Layout.getVBaseClassOffset(ML.VBase); |
| 653 | } else { |
| 654 | This = CGF.Builder.CreateBitCast(This, charPtrTy); |
Timur Iskhodzhanov | 4ddf592 | 2013-11-13 16:03:43 +0000 | [diff] [blame] | 655 | llvm::Value *VBaseOffset = |
| 656 | GetVirtualBaseClassOffset(CGF, This, MD->getParent(), ML.VBase); |
Timur Iskhodzhanov | 62082b7 | 2013-10-16 18:24:06 +0000 | [diff] [blame] | 657 | This = CGF.Builder.CreateInBoundsGEP(This, VBaseOffset); |
| 658 | } |
| 659 | } |
Timur Iskhodzhanov | 88fd439 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 660 | if (!StaticOffset.isZero()) { |
| 661 | assert(StaticOffset.isPositive()); |
| 662 | This = CGF.Builder.CreateBitCast(This, charPtrTy); |
Timur Iskhodzhanov | 827365e | 2013-10-22 18:15:24 +0000 | [diff] [blame] | 663 | if (ML.VBase) { |
| 664 | // Non-virtual adjustment might result in a pointer outside the allocated |
| 665 | // object, e.g. if the final overrider class is laid out after the virtual |
| 666 | // base that declares a method in the most derived class. |
| 667 | // FIXME: Update the code that emits this adjustment in thunks prologues. |
| 668 | This = CGF.Builder.CreateConstGEP1_32(This, StaticOffset.getQuantity()); |
| 669 | } else { |
| 670 | This = CGF.Builder.CreateConstInBoundsGEP1_32(This, |
| 671 | StaticOffset.getQuantity()); |
| 672 | } |
Timur Iskhodzhanov | 88fd439 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 673 | } |
| 674 | return This; |
| 675 | } |
| 676 | |
Timur Iskhodzhanov | ee6bc53 | 2013-02-13 08:37:51 +0000 | [diff] [blame] | 677 | static bool IsDeletingDtor(GlobalDecl GD) { |
| 678 | const CXXMethodDecl* MD = cast<CXXMethodDecl>(GD.getDecl()); |
| 679 | if (isa<CXXDestructorDecl>(MD)) { |
| 680 | return GD.getDtorType() == Dtor_Deleting; |
| 681 | } |
| 682 | return false; |
| 683 | } |
| 684 | |
John McCall | 0f999f3 | 2012-09-25 08:00:39 +0000 | [diff] [blame] | 685 | void MicrosoftCXXABI::BuildInstanceFunctionParams(CodeGenFunction &CGF, |
| 686 | QualType &ResTy, |
| 687 | FunctionArgList &Params) { |
| 688 | BuildThisParam(CGF, Params); |
Timur Iskhodzhanov | ee6bc53 | 2013-02-13 08:37:51 +0000 | [diff] [blame] | 689 | |
Timur Iskhodzhanov | 57cbe5c | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 690 | ASTContext &Context = getContext(); |
| 691 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(CGF.CurGD.getDecl()); |
| 692 | if (isa<CXXConstructorDecl>(MD) && MD->getParent()->getNumVBases()) { |
| 693 | ImplicitParamDecl *IsMostDerived |
| 694 | = ImplicitParamDecl::Create(Context, 0, |
| 695 | CGF.CurGD.getDecl()->getLocation(), |
| 696 | &Context.Idents.get("is_most_derived"), |
| 697 | Context.IntTy); |
| 698 | Params.push_back(IsMostDerived); |
| 699 | getStructorImplicitParamDecl(CGF) = IsMostDerived; |
| 700 | } else if (IsDeletingDtor(CGF.CurGD)) { |
Timur Iskhodzhanov | ee6bc53 | 2013-02-13 08:37:51 +0000 | [diff] [blame] | 701 | ImplicitParamDecl *ShouldDelete |
| 702 | = ImplicitParamDecl::Create(Context, 0, |
| 703 | CGF.CurGD.getDecl()->getLocation(), |
| 704 | &Context.Idents.get("should_call_delete"), |
Timur Iskhodzhanov | 701981f | 2013-08-27 10:38:19 +0000 | [diff] [blame] | 705 | Context.IntTy); |
Timur Iskhodzhanov | ee6bc53 | 2013-02-13 08:37:51 +0000 | [diff] [blame] | 706 | Params.push_back(ShouldDelete); |
| 707 | getStructorImplicitParamDecl(CGF) = ShouldDelete; |
| 708 | } |
John McCall | 0f999f3 | 2012-09-25 08:00:39 +0000 | [diff] [blame] | 709 | } |
| 710 | |
Timur Iskhodzhanov | 88fd439 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 711 | llvm::Value *MicrosoftCXXABI::adjustThisParameterInVirtualFunctionPrologue( |
| 712 | CodeGenFunction &CGF, GlobalDecl GD, llvm::Value *This) { |
| 713 | GD = GD.getCanonicalDecl(); |
| 714 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
Timur Iskhodzhanov | 62082b7 | 2013-10-16 18:24:06 +0000 | [diff] [blame] | 715 | |
| 716 | GlobalDecl LookupGD = GD; |
| 717 | if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) { |
| 718 | // Complete destructors take a pointer to the complete object as a |
| 719 | // parameter, thus don't need this adjustment. |
| 720 | if (GD.getDtorType() == Dtor_Complete) |
| 721 | return This; |
| 722 | |
| 723 | // There's no Dtor_Base in vftable but it shares the this adjustment with |
| 724 | // the deleting one, so look it up instead. |
| 725 | LookupGD = GlobalDecl(DD, Dtor_Deleting); |
| 726 | } |
Timur Iskhodzhanov | 88fd439 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 727 | |
| 728 | // In this ABI, every virtual function takes a pointer to one of the |
| 729 | // subobjects that first defines it as the 'this' parameter, rather than a |
Alp Toker | f6a24ce | 2013-12-05 16:25:25 +0000 | [diff] [blame] | 730 | // pointer to the final overrider subobject. Thus, we need to adjust it back |
Timur Iskhodzhanov | 88fd439 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 731 | // to the final overrider subobject before use. |
| 732 | // See comments in the MicrosoftVFTableContext implementation for the details. |
| 733 | |
Timur Iskhodzhanov | 5877663 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 734 | MicrosoftVTableContext::MethodVFTableLocation ML = |
| 735 | CGM.getMicrosoftVTableContext().getMethodVFTableLocation(LookupGD); |
Timur Iskhodzhanov | 9e7f505 | 2013-11-07 13:34:02 +0000 | [diff] [blame] | 736 | CharUnits Adjustment = ML.VFPtrOffset; |
Timur Iskhodzhanov | 88fd439 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 737 | if (ML.VBase) { |
| 738 | const ASTRecordLayout &DerivedLayout = |
| 739 | CGF.getContext().getASTRecordLayout(MD->getParent()); |
| 740 | Adjustment += DerivedLayout.getVBaseClassOffset(ML.VBase); |
| 741 | } |
| 742 | |
| 743 | if (Adjustment.isZero()) |
| 744 | return This; |
| 745 | |
| 746 | unsigned AS = cast<llvm::PointerType>(This->getType())->getAddressSpace(); |
| 747 | llvm::Type *charPtrTy = CGF.Int8Ty->getPointerTo(AS), |
| 748 | *thisTy = This->getType(); |
| 749 | |
| 750 | This = CGF.Builder.CreateBitCast(This, charPtrTy); |
| 751 | assert(Adjustment.isPositive()); |
Timur Iskhodzhanov | 827365e | 2013-10-22 18:15:24 +0000 | [diff] [blame] | 752 | This = |
| 753 | CGF.Builder.CreateConstInBoundsGEP1_32(This, -Adjustment.getQuantity()); |
Timur Iskhodzhanov | 88fd439 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 754 | return CGF.Builder.CreateBitCast(This, thisTy); |
| 755 | } |
| 756 | |
John McCall | 0f999f3 | 2012-09-25 08:00:39 +0000 | [diff] [blame] | 757 | void MicrosoftCXXABI::EmitInstanceFunctionProlog(CodeGenFunction &CGF) { |
| 758 | EmitThisParam(CGF); |
Stephen Lin | 9dc6eef | 2013-06-30 20:40:16 +0000 | [diff] [blame] | 759 | |
| 760 | /// If this is a function that the ABI specifies returns 'this', initialize |
| 761 | /// the return slot to 'this' at the start of the function. |
| 762 | /// |
| 763 | /// Unlike the setting of return types, this is done within the ABI |
| 764 | /// implementation instead of by clients of CGCXXABI because: |
| 765 | /// 1) getThisValue is currently protected |
| 766 | /// 2) in theory, an ABI could implement 'this' returns some other way; |
| 767 | /// HasThisReturn only specifies a contract, not the implementation |
| 768 | if (HasThisReturn(CGF.CurGD)) |
John McCall | 0f999f3 | 2012-09-25 08:00:39 +0000 | [diff] [blame] | 769 | CGF.Builder.CreateStore(getThisValue(CGF), CGF.ReturnValue); |
Timur Iskhodzhanov | 57cbe5c | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 770 | |
| 771 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(CGF.CurGD.getDecl()); |
| 772 | if (isa<CXXConstructorDecl>(MD) && MD->getParent()->getNumVBases()) { |
| 773 | assert(getStructorImplicitParamDecl(CGF) && |
| 774 | "no implicit parameter for a constructor with virtual bases?"); |
| 775 | getStructorImplicitParamValue(CGF) |
| 776 | = CGF.Builder.CreateLoad( |
| 777 | CGF.GetAddrOfLocalVar(getStructorImplicitParamDecl(CGF)), |
| 778 | "is_most_derived"); |
| 779 | } |
| 780 | |
Timur Iskhodzhanov | ee6bc53 | 2013-02-13 08:37:51 +0000 | [diff] [blame] | 781 | if (IsDeletingDtor(CGF.CurGD)) { |
| 782 | assert(getStructorImplicitParamDecl(CGF) && |
| 783 | "no implicit parameter for a deleting destructor?"); |
| 784 | getStructorImplicitParamValue(CGF) |
| 785 | = CGF.Builder.CreateLoad( |
| 786 | CGF.GetAddrOfLocalVar(getStructorImplicitParamDecl(CGF)), |
| 787 | "should_call_delete"); |
| 788 | } |
John McCall | 0f999f3 | 2012-09-25 08:00:39 +0000 | [diff] [blame] | 789 | } |
| 790 | |
Stephen Lin | 9dc6eef | 2013-06-30 20:40:16 +0000 | [diff] [blame] | 791 | void MicrosoftCXXABI::EmitConstructorCall(CodeGenFunction &CGF, |
Stephen Lin | c467c87 | 2013-06-19 18:10:35 +0000 | [diff] [blame] | 792 | const CXXConstructorDecl *D, |
Stephen Lin | 9dc6eef | 2013-06-30 20:40:16 +0000 | [diff] [blame] | 793 | CXXCtorType Type, |
| 794 | bool ForVirtualBase, |
Stephen Lin | c467c87 | 2013-06-19 18:10:35 +0000 | [diff] [blame] | 795 | bool Delegating, |
| 796 | llvm::Value *This, |
Timur Iskhodzhanov | 57cbe5c | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 797 | CallExpr::const_arg_iterator ArgBeg, |
| 798 | CallExpr::const_arg_iterator ArgEnd) { |
| 799 | assert(Type == Ctor_Complete || Type == Ctor_Base); |
| 800 | llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(D, Ctor_Complete); |
| 801 | |
| 802 | llvm::Value *ImplicitParam = 0; |
| 803 | QualType ImplicitParamTy; |
| 804 | if (D->getParent()->getNumVBases()) { |
| 805 | ImplicitParam = llvm::ConstantInt::get(CGM.Int32Ty, Type == Ctor_Complete); |
| 806 | ImplicitParamTy = getContext().IntTy; |
| 807 | } |
| 808 | |
| 809 | // FIXME: Provide a source location here. |
Stephen Lin | c467c87 | 2013-06-19 18:10:35 +0000 | [diff] [blame] | 810 | CGF.EmitCXXMemberCall(D, SourceLocation(), Callee, ReturnValueSlot(), This, |
Stephen Lin | 9dc6eef | 2013-06-30 20:40:16 +0000 | [diff] [blame] | 811 | ImplicitParam, ImplicitParamTy, ArgBeg, ArgEnd); |
Timur Iskhodzhanov | 57cbe5c | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 812 | } |
| 813 | |
Reid Kleckner | 6fe771a | 2013-12-13 00:53:54 +0000 | [diff] [blame^] | 814 | void MicrosoftCXXABI::EmitDestructorCall(CodeGenFunction &CGF, |
| 815 | const CXXDestructorDecl *DD, |
| 816 | CXXDtorType Type, bool ForVirtualBase, |
| 817 | bool Delegating, llvm::Value *This) { |
| 818 | llvm::Value *Callee = CGM.GetAddrOfCXXDestructor(DD, Type); |
| 819 | |
| 820 | if (DD->isVirtual()) |
| 821 | This = adjustThisArgumentForVirtualCall(CGF, GlobalDecl(DD, Type), This); |
| 822 | |
| 823 | // FIXME: Provide a source location here. |
| 824 | CGF.EmitCXXMemberCall(DD, SourceLocation(), Callee, ReturnValueSlot(), This, |
| 825 | /*ImplicitParam=*/0, /*ImplicitParamTy=*/QualType(), 0, 0); |
| 826 | } |
| 827 | |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 828 | void MicrosoftCXXABI::emitVTableDefinitions(CodeGenVTables &CGVT, |
| 829 | const CXXRecordDecl *RD) { |
Timur Iskhodzhanov | 5877663 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 830 | MicrosoftVTableContext &VFTContext = CGM.getMicrosoftVTableContext(); |
| 831 | MicrosoftVTableContext::VFPtrListTy VFPtrs = VFTContext.getVFPtrOffsets(RD); |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 832 | llvm::GlobalVariable::LinkageTypes Linkage = CGM.getVTableLinkage(RD); |
| 833 | |
Timur Iskhodzhanov | 5877663 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 834 | for (MicrosoftVTableContext::VFPtrListTy::iterator I = VFPtrs.begin(), |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 835 | E = VFPtrs.end(); I != E; ++I) { |
| 836 | llvm::GlobalVariable *VTable = getAddrOfVTable(RD, I->VFPtrFullOffset); |
| 837 | if (VTable->hasInitializer()) |
| 838 | continue; |
| 839 | |
| 840 | const VTableLayout &VTLayout = |
| 841 | VFTContext.getVFTableLayout(RD, I->VFPtrFullOffset); |
| 842 | llvm::Constant *Init = CGVT.CreateVTableInitializer( |
| 843 | RD, VTLayout.vtable_component_begin(), |
| 844 | VTLayout.getNumVTableComponents(), VTLayout.vtable_thunk_begin(), |
| 845 | VTLayout.getNumVTableThunks()); |
| 846 | VTable->setInitializer(Init); |
| 847 | |
| 848 | VTable->setLinkage(Linkage); |
| 849 | CGM.setTypeVisibility(VTable, RD, CodeGenModule::TVK_ForVTable); |
| 850 | } |
| 851 | } |
| 852 | |
| 853 | llvm::Value *MicrosoftCXXABI::getVTableAddressPointInStructor( |
| 854 | CodeGenFunction &CGF, const CXXRecordDecl *VTableClass, BaseSubobject Base, |
| 855 | const CXXRecordDecl *NearestVBase, bool &NeedsVirtualOffset) { |
| 856 | NeedsVirtualOffset = (NearestVBase != 0); |
| 857 | |
| 858 | llvm::Value *VTableAddressPoint = |
| 859 | getAddrOfVTable(VTableClass, Base.getBaseOffset()); |
| 860 | if (!VTableAddressPoint) { |
| 861 | assert(Base.getBase()->getNumVBases() && |
| 862 | !CGM.getContext().getASTRecordLayout(Base.getBase()).hasOwnVFPtr()); |
| 863 | } |
| 864 | return VTableAddressPoint; |
| 865 | } |
| 866 | |
Timur Iskhodzhanov | 6745522 | 2013-10-03 06:26:13 +0000 | [diff] [blame] | 867 | static void mangleVFTableName(MicrosoftMangleContext &MangleContext, |
| 868 | const CXXRecordDecl *RD, const VFPtrInfo &VFPtr, |
| 869 | SmallString<256> &Name) { |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 870 | llvm::raw_svector_ostream Out(Name); |
Timur Iskhodzhanov | 6745522 | 2013-10-03 06:26:13 +0000 | [diff] [blame] | 871 | MangleContext.mangleCXXVFTable(RD, VFPtr.PathToMangle, Out); |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 872 | } |
| 873 | |
| 874 | llvm::Constant *MicrosoftCXXABI::getVTableAddressPointForConstExpr( |
| 875 | BaseSubobject Base, const CXXRecordDecl *VTableClass) { |
| 876 | llvm::Constant *VTable = getAddrOfVTable(VTableClass, Base.getBaseOffset()); |
| 877 | assert(VTable && "Couldn't find a vftable for the given base?"); |
| 878 | return VTable; |
| 879 | } |
| 880 | |
| 881 | llvm::GlobalVariable *MicrosoftCXXABI::getAddrOfVTable(const CXXRecordDecl *RD, |
| 882 | CharUnits VPtrOffset) { |
| 883 | // getAddrOfVTable may return 0 if asked to get an address of a vtable which |
| 884 | // shouldn't be used in the given record type. We want to cache this result in |
| 885 | // VFTablesMap, thus a simple zero check is not sufficient. |
| 886 | VFTableIdTy ID(RD, VPtrOffset); |
| 887 | VFTablesMapTy::iterator I; |
| 888 | bool Inserted; |
| 889 | llvm::tie(I, Inserted) = VFTablesMap.insert( |
| 890 | std::make_pair(ID, static_cast<llvm::GlobalVariable *>(0))); |
| 891 | if (!Inserted) |
| 892 | return I->second; |
| 893 | |
| 894 | llvm::GlobalVariable *&VTable = I->second; |
| 895 | |
Timur Iskhodzhanov | 5877663 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 896 | MicrosoftVTableContext &VTContext = CGM.getMicrosoftVTableContext(); |
| 897 | const MicrosoftVTableContext::VFPtrListTy &VFPtrs = |
| 898 | VTContext.getVFPtrOffsets(RD); |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 899 | |
| 900 | if (DeferredVFTables.insert(RD)) { |
| 901 | // We haven't processed this record type before. |
| 902 | // Queue up this v-table for possible deferred emission. |
| 903 | CGM.addDeferredVTable(RD); |
| 904 | |
| 905 | #ifndef NDEBUG |
| 906 | // Create all the vftables at once in order to make sure each vftable has |
| 907 | // a unique mangled name. |
| 908 | llvm::StringSet<> ObservedMangledNames; |
| 909 | for (size_t J = 0, F = VFPtrs.size(); J != F; ++J) { |
| 910 | SmallString<256> Name; |
Timur Iskhodzhanov | 6745522 | 2013-10-03 06:26:13 +0000 | [diff] [blame] | 911 | mangleVFTableName(getMangleContext(), RD, VFPtrs[J], Name); |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 912 | if (!ObservedMangledNames.insert(Name.str())) |
| 913 | llvm_unreachable("Already saw this mangling before?"); |
| 914 | } |
| 915 | #endif |
| 916 | } |
| 917 | |
| 918 | for (size_t J = 0, F = VFPtrs.size(); J != F; ++J) { |
| 919 | if (VFPtrs[J].VFPtrFullOffset != VPtrOffset) |
| 920 | continue; |
| 921 | |
| 922 | llvm::ArrayType *ArrayType = llvm::ArrayType::get( |
| 923 | CGM.Int8PtrTy, |
Timur Iskhodzhanov | 5877663 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 924 | VTContext.getVFTableLayout(RD, VFPtrs[J].VFPtrFullOffset) |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 925 | .getNumVTableComponents()); |
| 926 | |
| 927 | SmallString<256> Name; |
Timur Iskhodzhanov | 6745522 | 2013-10-03 06:26:13 +0000 | [diff] [blame] | 928 | mangleVFTableName(getMangleContext(), RD, VFPtrs[J], Name); |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 929 | VTable = CGM.CreateOrReplaceCXXRuntimeVariable( |
| 930 | Name.str(), ArrayType, llvm::GlobalValue::ExternalLinkage); |
| 931 | VTable->setUnnamedAddr(true); |
| 932 | break; |
| 933 | } |
| 934 | |
| 935 | return VTable; |
| 936 | } |
| 937 | |
Timur Iskhodzhanov | 88fd439 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 938 | llvm::Value *MicrosoftCXXABI::getVirtualFunctionPointer(CodeGenFunction &CGF, |
| 939 | GlobalDecl GD, |
| 940 | llvm::Value *This, |
| 941 | llvm::Type *Ty) { |
| 942 | GD = GD.getCanonicalDecl(); |
| 943 | CGBuilderTy &Builder = CGF.Builder; |
| 944 | |
| 945 | Ty = Ty->getPointerTo()->getPointerTo(); |
| 946 | llvm::Value *VPtr = adjustThisArgumentForVirtualCall(CGF, GD, This); |
| 947 | llvm::Value *VTable = CGF.GetVTablePtr(VPtr, Ty); |
| 948 | |
Timur Iskhodzhanov | 5877663 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 949 | MicrosoftVTableContext::MethodVFTableLocation ML = |
| 950 | CGM.getMicrosoftVTableContext().getMethodVFTableLocation(GD); |
Timur Iskhodzhanov | 88fd439 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 951 | llvm::Value *VFuncPtr = |
| 952 | Builder.CreateConstInBoundsGEP1_64(VTable, ML.Index, "vfn"); |
| 953 | return Builder.CreateLoad(VFuncPtr); |
| 954 | } |
| 955 | |
Stephen Lin | 9dc6eef | 2013-06-30 20:40:16 +0000 | [diff] [blame] | 956 | void MicrosoftCXXABI::EmitVirtualDestructorCall(CodeGenFunction &CGF, |
| 957 | const CXXDestructorDecl *Dtor, |
| 958 | CXXDtorType DtorType, |
| 959 | SourceLocation CallLoc, |
| 960 | llvm::Value *This) { |
Timur Iskhodzhanov | d619711 | 2013-02-15 14:45:22 +0000 | [diff] [blame] | 961 | assert(DtorType == Dtor_Deleting || DtorType == Dtor_Complete); |
| 962 | |
| 963 | // We have only one destructor in the vftable but can get both behaviors |
Timur Iskhodzhanov | 701981f | 2013-08-27 10:38:19 +0000 | [diff] [blame] | 964 | // by passing an implicit int parameter. |
Timur Iskhodzhanov | 62082b7 | 2013-10-16 18:24:06 +0000 | [diff] [blame] | 965 | GlobalDecl GD(Dtor, Dtor_Deleting); |
Timur Iskhodzhanov | 88fd439 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 966 | const CGFunctionInfo *FInfo = |
| 967 | &CGM.getTypes().arrangeCXXDestructor(Dtor, Dtor_Deleting); |
Timur Iskhodzhanov | d619711 | 2013-02-15 14:45:22 +0000 | [diff] [blame] | 968 | llvm::Type *Ty = CGF.CGM.getTypes().GetFunctionType(*FInfo); |
Timur Iskhodzhanov | 62082b7 | 2013-10-16 18:24:06 +0000 | [diff] [blame] | 969 | llvm::Value *Callee = getVirtualFunctionPointer(CGF, GD, This, Ty); |
Timur Iskhodzhanov | d619711 | 2013-02-15 14:45:22 +0000 | [diff] [blame] | 970 | |
| 971 | ASTContext &Context = CGF.getContext(); |
Timur Iskhodzhanov | 88fd439 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 972 | llvm::Value *ImplicitParam = |
Timur Iskhodzhanov | 701981f | 2013-08-27 10:38:19 +0000 | [diff] [blame] | 973 | llvm::ConstantInt::get(llvm::IntegerType::getInt32Ty(CGF.getLLVMContext()), |
Timur Iskhodzhanov | d619711 | 2013-02-15 14:45:22 +0000 | [diff] [blame] | 974 | DtorType == Dtor_Deleting); |
| 975 | |
Timur Iskhodzhanov | 62082b7 | 2013-10-16 18:24:06 +0000 | [diff] [blame] | 976 | This = adjustThisArgumentForVirtualCall(CGF, GD, This); |
Stephen Lin | 9dc6eef | 2013-06-30 20:40:16 +0000 | [diff] [blame] | 977 | CGF.EmitCXXMemberCall(Dtor, CallLoc, Callee, ReturnValueSlot(), This, |
Timur Iskhodzhanov | 701981f | 2013-08-27 10:38:19 +0000 | [diff] [blame] | 978 | ImplicitParam, Context.IntTy, 0, 0); |
Timur Iskhodzhanov | d619711 | 2013-02-15 14:45:22 +0000 | [diff] [blame] | 979 | } |
| 980 | |
Reid Kleckner | 7810af0 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 981 | const VBTableVector & |
| 982 | MicrosoftCXXABI::EnumerateVBTables(const CXXRecordDecl *RD) { |
| 983 | // At this layer, we can key the cache off of a single class, which is much |
| 984 | // easier than caching at the GlobalVariable layer. |
| 985 | llvm::DenseMap<const CXXRecordDecl*, VBTableVector>::iterator I; |
| 986 | bool added; |
| 987 | llvm::tie(I, added) = VBTablesMap.insert(std::make_pair(RD, VBTableVector())); |
| 988 | VBTableVector &VBTables = I->second; |
| 989 | if (!added) |
| 990 | return VBTables; |
| 991 | |
| 992 | VBTableBuilder(CGM, RD).enumerateVBTables(VBTables); |
| 993 | |
| 994 | return VBTables; |
| 995 | } |
| 996 | |
Hans Wennborg | 88497d6 | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 997 | llvm::Function * |
| 998 | MicrosoftCXXABI::EmitVirtualMemPtrThunk(const CXXMethodDecl *MD, |
| 999 | StringRef ThunkName) { |
| 1000 | // If the thunk has been generated previously, just return it. |
| 1001 | if (llvm::GlobalValue *GV = CGM.getModule().getNamedValue(ThunkName)) |
| 1002 | return cast<llvm::Function>(GV); |
| 1003 | |
| 1004 | // Create the llvm::Function. |
| 1005 | const CGFunctionInfo &FnInfo = CGM.getTypes().arrangeGlobalDeclaration(MD); |
| 1006 | llvm::FunctionType *ThunkTy = CGM.getTypes().GetFunctionType(FnInfo); |
| 1007 | llvm::Function *ThunkFn = |
| 1008 | llvm::Function::Create(ThunkTy, llvm::Function::ExternalLinkage, |
| 1009 | ThunkName.str(), &CGM.getModule()); |
| 1010 | assert(ThunkFn->getName() == ThunkName && "name was uniqued!"); |
| 1011 | |
Hans Wennborg | 88497d6 | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 1012 | ThunkFn->setLinkage(MD->isExternallyVisible() |
| 1013 | ? llvm::GlobalValue::LinkOnceODRLinkage |
| 1014 | : llvm::GlobalValue::InternalLinkage); |
| 1015 | |
| 1016 | CGM.SetLLVMFunctionAttributes(MD, FnInfo, ThunkFn); |
| 1017 | CGM.SetLLVMFunctionAttributesForDefinition(MD, ThunkFn); |
| 1018 | |
| 1019 | // Start codegen. |
| 1020 | CodeGenFunction CGF(CGM); |
| 1021 | CGF.StartThunk(ThunkFn, MD, FnInfo); |
| 1022 | |
| 1023 | // Get to the Callee. |
| 1024 | llvm::Value *This = CGF.LoadCXXThis(); |
| 1025 | llvm::Value *Callee = getVirtualFunctionPointer(CGF, MD, This, ThunkTy); |
| 1026 | |
| 1027 | // Make the call and return the result. |
| 1028 | CGF.EmitCallAndReturnForThunk(MD, Callee, 0); |
| 1029 | |
| 1030 | return ThunkFn; |
| 1031 | } |
| 1032 | |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1033 | void MicrosoftCXXABI::emitVirtualInheritanceTables(const CXXRecordDecl *RD) { |
Reid Kleckner | 7810af0 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 1034 | const VBTableVector &VBTables = EnumerateVBTables(RD); |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1035 | llvm::GlobalVariable::LinkageTypes Linkage = CGM.getVTableLinkage(RD); |
| 1036 | |
Reid Kleckner | 7810af0 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 1037 | for (VBTableVector::const_iterator I = VBTables.begin(), E = VBTables.end(); |
| 1038 | I != E; ++I) { |
| 1039 | I->EmitVBTableDefinition(CGM, RD, Linkage); |
| 1040 | } |
| 1041 | } |
| 1042 | |
Timur Iskhodzhanov | 0201432 | 2013-10-30 11:55:43 +0000 | [diff] [blame] | 1043 | llvm::Value *MicrosoftCXXABI::performThisAdjustment(CodeGenFunction &CGF, |
| 1044 | llvm::Value *This, |
| 1045 | const ThisAdjustment &TA) { |
| 1046 | if (TA.isEmpty()) |
| 1047 | return This; |
| 1048 | |
| 1049 | llvm::Value *V = CGF.Builder.CreateBitCast(This, CGF.Int8PtrTy); |
| 1050 | |
Timur Iskhodzhanov | 053142a | 2013-11-06 06:24:31 +0000 | [diff] [blame] | 1051 | if (!TA.Virtual.isEmpty()) { |
| 1052 | assert(TA.Virtual.Microsoft.VtordispOffset < 0); |
| 1053 | // Adjust the this argument based on the vtordisp value. |
| 1054 | llvm::Value *VtorDispPtr = |
| 1055 | CGF.Builder.CreateConstGEP1_32(V, TA.Virtual.Microsoft.VtordispOffset); |
| 1056 | VtorDispPtr = |
| 1057 | CGF.Builder.CreateBitCast(VtorDispPtr, CGF.Int32Ty->getPointerTo()); |
| 1058 | llvm::Value *VtorDisp = CGF.Builder.CreateLoad(VtorDispPtr, "vtordisp"); |
| 1059 | V = CGF.Builder.CreateGEP(V, CGF.Builder.CreateNeg(VtorDisp)); |
| 1060 | |
| 1061 | if (TA.Virtual.Microsoft.VBPtrOffset) { |
| 1062 | // If the final overrider is defined in a virtual base other than the one |
| 1063 | // that holds the vfptr, we have to use a vtordispex thunk which looks up |
| 1064 | // the vbtable of the derived class. |
| 1065 | assert(TA.Virtual.Microsoft.VBPtrOffset > 0); |
| 1066 | assert(TA.Virtual.Microsoft.VBOffsetOffset >= 0); |
| 1067 | llvm::Value *VBPtr; |
| 1068 | llvm::Value *VBaseOffset = |
| 1069 | GetVBaseOffsetFromVBPtr(CGF, V, -TA.Virtual.Microsoft.VBPtrOffset, |
| 1070 | TA.Virtual.Microsoft.VBOffsetOffset, &VBPtr); |
| 1071 | V = CGF.Builder.CreateInBoundsGEP(VBPtr, VBaseOffset); |
| 1072 | } |
| 1073 | } |
Timur Iskhodzhanov | 0201432 | 2013-10-30 11:55:43 +0000 | [diff] [blame] | 1074 | |
| 1075 | if (TA.NonVirtual) { |
| 1076 | // Non-virtual adjustment might result in a pointer outside the allocated |
| 1077 | // object, e.g. if the final overrider class is laid out after the virtual |
| 1078 | // base that declares a method in the most derived class. |
| 1079 | V = CGF.Builder.CreateConstGEP1_32(V, TA.NonVirtual); |
| 1080 | } |
| 1081 | |
| 1082 | // Don't need to bitcast back, the call CodeGen will handle this. |
| 1083 | return V; |
| 1084 | } |
| 1085 | |
| 1086 | llvm::Value * |
| 1087 | MicrosoftCXXABI::performReturnAdjustment(CodeGenFunction &CGF, llvm::Value *Ret, |
| 1088 | const ReturnAdjustment &RA) { |
| 1089 | if (RA.isEmpty()) |
| 1090 | return Ret; |
| 1091 | |
| 1092 | llvm::Value *V = CGF.Builder.CreateBitCast(Ret, CGF.Int8PtrTy); |
| 1093 | |
| 1094 | if (RA.Virtual.Microsoft.VBIndex) { |
| 1095 | assert(RA.Virtual.Microsoft.VBIndex > 0); |
| 1096 | int32_t IntSize = |
| 1097 | getContext().getTypeSizeInChars(getContext().IntTy).getQuantity(); |
| 1098 | llvm::Value *VBPtr; |
| 1099 | llvm::Value *VBaseOffset = |
| 1100 | GetVBaseOffsetFromVBPtr(CGF, V, RA.Virtual.Microsoft.VBPtrOffset, |
| 1101 | IntSize * RA.Virtual.Microsoft.VBIndex, &VBPtr); |
| 1102 | V = CGF.Builder.CreateInBoundsGEP(VBPtr, VBaseOffset); |
| 1103 | } |
| 1104 | |
| 1105 | if (RA.NonVirtual) |
| 1106 | V = CGF.Builder.CreateConstInBoundsGEP1_32(V, RA.NonVirtual); |
| 1107 | |
| 1108 | // Cast back to the original type. |
| 1109 | return CGF.Builder.CreateBitCast(V, Ret->getType()); |
| 1110 | } |
| 1111 | |
John McCall | b91cd66 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 1112 | bool MicrosoftCXXABI::requiresArrayCookie(const CXXDeleteExpr *expr, |
| 1113 | QualType elementType) { |
| 1114 | // Microsoft seems to completely ignore the possibility of a |
| 1115 | // two-argument usual deallocation function. |
| 1116 | return elementType.isDestructedType(); |
| 1117 | } |
| 1118 | |
| 1119 | bool MicrosoftCXXABI::requiresArrayCookie(const CXXNewExpr *expr) { |
| 1120 | // Microsoft seems to completely ignore the possibility of a |
| 1121 | // two-argument usual deallocation function. |
| 1122 | return expr->getAllocatedType().isDestructedType(); |
| 1123 | } |
| 1124 | |
| 1125 | CharUnits MicrosoftCXXABI::getArrayCookieSizeImpl(QualType type) { |
| 1126 | // The array cookie is always a size_t; we then pad that out to the |
| 1127 | // alignment of the element type. |
| 1128 | ASTContext &Ctx = getContext(); |
| 1129 | return std::max(Ctx.getTypeSizeInChars(Ctx.getSizeType()), |
| 1130 | Ctx.getTypeAlignInChars(type)); |
| 1131 | } |
| 1132 | |
| 1133 | llvm::Value *MicrosoftCXXABI::readArrayCookieImpl(CodeGenFunction &CGF, |
| 1134 | llvm::Value *allocPtr, |
| 1135 | CharUnits cookieSize) { |
Micah Villmow | ea2fea2 | 2012-10-25 15:39:14 +0000 | [diff] [blame] | 1136 | unsigned AS = allocPtr->getType()->getPointerAddressSpace(); |
John McCall | b91cd66 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 1137 | llvm::Value *numElementsPtr = |
| 1138 | CGF.Builder.CreateBitCast(allocPtr, CGF.SizeTy->getPointerTo(AS)); |
| 1139 | return CGF.Builder.CreateLoad(numElementsPtr); |
| 1140 | } |
| 1141 | |
| 1142 | llvm::Value* MicrosoftCXXABI::InitializeArrayCookie(CodeGenFunction &CGF, |
| 1143 | llvm::Value *newPtr, |
| 1144 | llvm::Value *numElements, |
| 1145 | const CXXNewExpr *expr, |
| 1146 | QualType elementType) { |
| 1147 | assert(requiresArrayCookie(expr)); |
| 1148 | |
| 1149 | // The size of the cookie. |
| 1150 | CharUnits cookieSize = getArrayCookieSizeImpl(elementType); |
| 1151 | |
| 1152 | // Compute an offset to the cookie. |
| 1153 | llvm::Value *cookiePtr = newPtr; |
| 1154 | |
| 1155 | // Write the number of elements into the appropriate slot. |
Micah Villmow | ea2fea2 | 2012-10-25 15:39:14 +0000 | [diff] [blame] | 1156 | unsigned AS = newPtr->getType()->getPointerAddressSpace(); |
John McCall | b91cd66 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 1157 | llvm::Value *numElementsPtr |
| 1158 | = CGF.Builder.CreateBitCast(cookiePtr, CGF.SizeTy->getPointerTo(AS)); |
| 1159 | CGF.Builder.CreateStore(numElements, numElementsPtr); |
| 1160 | |
| 1161 | // Finally, compute a pointer to the actual data buffer by skipping |
| 1162 | // over the cookie completely. |
| 1163 | return CGF.Builder.CreateConstInBoundsGEP1_64(newPtr, |
| 1164 | cookieSize.getQuantity()); |
| 1165 | } |
| 1166 | |
John McCall | c84ed6a | 2012-05-01 06:13:13 +0000 | [diff] [blame] | 1167 | void MicrosoftCXXABI::EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D, |
Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 1168 | llvm::GlobalVariable *GV, |
John McCall | c84ed6a | 2012-05-01 06:13:13 +0000 | [diff] [blame] | 1169 | bool PerformInit) { |
Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 1170 | // MSVC always uses an i32 bitfield to guard initialization, which is *not* |
| 1171 | // threadsafe. Since the user may be linking in inline functions compiled by |
| 1172 | // cl.exe, there's no reason to provide a false sense of security by using |
| 1173 | // critical sections here. |
John McCall | c84ed6a | 2012-05-01 06:13:13 +0000 | [diff] [blame] | 1174 | |
Richard Smith | dbf74ba | 2013-04-14 23:01:42 +0000 | [diff] [blame] | 1175 | if (D.getTLSKind()) |
| 1176 | CGM.ErrorUnsupported(&D, "dynamic TLS initialization"); |
| 1177 | |
Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 1178 | CGBuilderTy &Builder = CGF.Builder; |
| 1179 | llvm::IntegerType *GuardTy = CGF.Int32Ty; |
| 1180 | llvm::ConstantInt *Zero = llvm::ConstantInt::get(GuardTy, 0); |
| 1181 | |
| 1182 | // Get the guard variable for this function if we have one already. |
| 1183 | GuardInfo &GI = GuardVariableMap[D.getDeclContext()]; |
| 1184 | |
| 1185 | unsigned BitIndex; |
| 1186 | if (D.isExternallyVisible()) { |
| 1187 | // Externally visible variables have to be numbered in Sema to properly |
| 1188 | // handle unreachable VarDecls. |
| 1189 | BitIndex = getContext().getManglingNumber(&D); |
| 1190 | assert(BitIndex > 0); |
| 1191 | BitIndex--; |
| 1192 | } else { |
| 1193 | // Non-externally visible variables are numbered here in CodeGen. |
| 1194 | BitIndex = GI.BitIndex++; |
| 1195 | } |
| 1196 | |
| 1197 | if (BitIndex >= 32) { |
| 1198 | if (D.isExternallyVisible()) |
| 1199 | ErrorUnsupportedABI(CGF, "more than 32 guarded initializations"); |
| 1200 | BitIndex %= 32; |
| 1201 | GI.Guard = 0; |
| 1202 | } |
| 1203 | |
| 1204 | // Lazily create the i32 bitfield for this function. |
| 1205 | if (!GI.Guard) { |
| 1206 | // Mangle the name for the guard. |
| 1207 | SmallString<256> GuardName; |
| 1208 | { |
| 1209 | llvm::raw_svector_ostream Out(GuardName); |
| 1210 | getMangleContext().mangleStaticGuardVariable(&D, Out); |
| 1211 | Out.flush(); |
| 1212 | } |
| 1213 | |
| 1214 | // Create the guard variable with a zero-initializer. Just absorb linkage |
| 1215 | // and visibility from the guarded variable. |
| 1216 | GI.Guard = new llvm::GlobalVariable(CGM.getModule(), GuardTy, false, |
| 1217 | GV->getLinkage(), Zero, GuardName.str()); |
| 1218 | GI.Guard->setVisibility(GV->getVisibility()); |
| 1219 | } else { |
| 1220 | assert(GI.Guard->getLinkage() == GV->getLinkage() && |
| 1221 | "static local from the same function had different linkage"); |
| 1222 | } |
| 1223 | |
| 1224 | // Pseudo code for the test: |
| 1225 | // if (!(GuardVar & MyGuardBit)) { |
| 1226 | // GuardVar |= MyGuardBit; |
| 1227 | // ... initialize the object ...; |
| 1228 | // } |
| 1229 | |
| 1230 | // Test our bit from the guard variable. |
| 1231 | llvm::ConstantInt *Bit = llvm::ConstantInt::get(GuardTy, 1U << BitIndex); |
| 1232 | llvm::LoadInst *LI = Builder.CreateLoad(GI.Guard); |
| 1233 | llvm::Value *IsInitialized = |
| 1234 | Builder.CreateICmpNE(Builder.CreateAnd(LI, Bit), Zero); |
| 1235 | llvm::BasicBlock *InitBlock = CGF.createBasicBlock("init"); |
| 1236 | llvm::BasicBlock *EndBlock = CGF.createBasicBlock("init.end"); |
| 1237 | Builder.CreateCondBr(IsInitialized, EndBlock, InitBlock); |
| 1238 | |
| 1239 | // Set our bit in the guard variable and emit the initializer and add a global |
| 1240 | // destructor if appropriate. |
| 1241 | CGF.EmitBlock(InitBlock); |
| 1242 | Builder.CreateStore(Builder.CreateOr(LI, Bit), GI.Guard); |
| 1243 | CGF.EmitCXXGlobalVarDeclInit(D, GV, PerformInit); |
| 1244 | Builder.CreateBr(EndBlock); |
| 1245 | |
| 1246 | // Continue. |
| 1247 | CGF.EmitBlock(EndBlock); |
John McCall | c84ed6a | 2012-05-01 06:13:13 +0000 | [diff] [blame] | 1248 | } |
| 1249 | |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 1250 | // Member pointer helpers. |
| 1251 | static bool hasVBPtrOffsetField(MSInheritanceModel Inheritance) { |
| 1252 | return Inheritance == MSIM_Unspecified; |
Reid Kleckner | 407e8b6 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 1253 | } |
| 1254 | |
Reid Kleckner | 452abac | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 1255 | static bool hasOnlyOneField(bool IsMemberFunction, |
| 1256 | MSInheritanceModel Inheritance) { |
| 1257 | return Inheritance <= MSIM_SinglePolymorphic || |
| 1258 | (!IsMemberFunction && Inheritance <= MSIM_MultiplePolymorphic); |
Reid Kleckner | 700c3ee | 2013-04-30 20:15:14 +0000 | [diff] [blame] | 1259 | } |
| 1260 | |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 1261 | // Only member pointers to functions need a this adjustment, since it can be |
| 1262 | // combined with the field offset for data pointers. |
Reid Kleckner | 7d0efb5 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 1263 | static bool hasNonVirtualBaseAdjustmentField(bool IsMemberFunction, |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 1264 | MSInheritanceModel Inheritance) { |
Reid Kleckner | 7d0efb5 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 1265 | return (IsMemberFunction && Inheritance >= MSIM_Multiple); |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 1266 | } |
| 1267 | |
| 1268 | static bool hasVirtualBaseAdjustmentField(MSInheritanceModel Inheritance) { |
| 1269 | return Inheritance >= MSIM_Virtual; |
| 1270 | } |
| 1271 | |
| 1272 | // Use zero for the field offset of a null data member pointer if we can |
| 1273 | // guarantee that zero is not a valid field offset, or if the member pointer has |
| 1274 | // multiple fields. Polymorphic classes have a vfptr at offset zero, so we can |
| 1275 | // use zero for null. If there are multiple fields, we can use zero even if it |
| 1276 | // is a valid field offset because null-ness testing will check the other |
| 1277 | // fields. |
| 1278 | static bool nullFieldOffsetIsZero(MSInheritanceModel Inheritance) { |
| 1279 | return Inheritance != MSIM_Multiple && Inheritance != MSIM_Single; |
| 1280 | } |
| 1281 | |
| 1282 | bool MicrosoftCXXABI::isZeroInitializable(const MemberPointerType *MPT) { |
| 1283 | // Null-ness for function memptrs only depends on the first field, which is |
| 1284 | // the function pointer. The rest don't matter, so we can zero initialize. |
| 1285 | if (MPT->isMemberFunctionPointer()) |
| 1286 | return true; |
| 1287 | |
| 1288 | // The virtual base adjustment field is always -1 for null, so if we have one |
| 1289 | // we can't zero initialize. The field offset is sometimes also -1 if 0 is a |
| 1290 | // valid field offset. |
| 1291 | const CXXRecordDecl *RD = MPT->getClass()->getAsCXXRecordDecl(); |
| 1292 | MSInheritanceModel Inheritance = RD->getMSInheritanceModel(); |
| 1293 | return (!hasVirtualBaseAdjustmentField(Inheritance) && |
| 1294 | nullFieldOffsetIsZero(Inheritance)); |
| 1295 | } |
| 1296 | |
| 1297 | llvm::Type * |
| 1298 | MicrosoftCXXABI::ConvertMemberPointerType(const MemberPointerType *MPT) { |
| 1299 | const CXXRecordDecl *RD = MPT->getClass()->getAsCXXRecordDecl(); |
| 1300 | MSInheritanceModel Inheritance = RD->getMSInheritanceModel(); |
| 1301 | llvm::SmallVector<llvm::Type *, 4> fields; |
| 1302 | if (MPT->isMemberFunctionPointer()) |
| 1303 | fields.push_back(CGM.VoidPtrTy); // FunctionPointerOrVirtualThunk |
| 1304 | else |
| 1305 | fields.push_back(CGM.IntTy); // FieldOffset |
| 1306 | |
Reid Kleckner | 7d0efb5 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 1307 | if (hasNonVirtualBaseAdjustmentField(MPT->isMemberFunctionPointer(), |
| 1308 | Inheritance)) |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 1309 | fields.push_back(CGM.IntTy); |
Reid Kleckner | 7d0efb5 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 1310 | if (hasVBPtrOffsetField(Inheritance)) |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 1311 | fields.push_back(CGM.IntTy); |
| 1312 | if (hasVirtualBaseAdjustmentField(Inheritance)) |
| 1313 | fields.push_back(CGM.IntTy); // VirtualBaseAdjustmentOffset |
| 1314 | |
| 1315 | if (fields.size() == 1) |
| 1316 | return fields[0]; |
| 1317 | return llvm::StructType::get(CGM.getLLVMContext(), fields); |
| 1318 | } |
| 1319 | |
| 1320 | void MicrosoftCXXABI:: |
| 1321 | GetNullMemberPointerFields(const MemberPointerType *MPT, |
| 1322 | llvm::SmallVectorImpl<llvm::Constant *> &fields) { |
| 1323 | assert(fields.empty()); |
| 1324 | const CXXRecordDecl *RD = MPT->getClass()->getAsCXXRecordDecl(); |
| 1325 | MSInheritanceModel Inheritance = RD->getMSInheritanceModel(); |
| 1326 | if (MPT->isMemberFunctionPointer()) { |
| 1327 | // FunctionPointerOrVirtualThunk |
| 1328 | fields.push_back(llvm::Constant::getNullValue(CGM.VoidPtrTy)); |
| 1329 | } else { |
| 1330 | if (nullFieldOffsetIsZero(Inheritance)) |
| 1331 | fields.push_back(getZeroInt()); // FieldOffset |
| 1332 | else |
| 1333 | fields.push_back(getAllOnesInt()); // FieldOffset |
Reid Kleckner | 407e8b6 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 1334 | } |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 1335 | |
Reid Kleckner | 7d0efb5 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 1336 | if (hasNonVirtualBaseAdjustmentField(MPT->isMemberFunctionPointer(), |
| 1337 | Inheritance)) |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 1338 | fields.push_back(getZeroInt()); |
Reid Kleckner | 7d0efb5 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 1339 | if (hasVBPtrOffsetField(Inheritance)) |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 1340 | fields.push_back(getZeroInt()); |
| 1341 | if (hasVirtualBaseAdjustmentField(Inheritance)) |
| 1342 | fields.push_back(getAllOnesInt()); |
Reid Kleckner | 407e8b6 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 1343 | } |
| 1344 | |
| 1345 | llvm::Constant * |
| 1346 | MicrosoftCXXABI::EmitNullMemberPointer(const MemberPointerType *MPT) { |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 1347 | llvm::SmallVector<llvm::Constant *, 4> fields; |
| 1348 | GetNullMemberPointerFields(MPT, fields); |
| 1349 | if (fields.size() == 1) |
| 1350 | return fields[0]; |
| 1351 | llvm::Constant *Res = llvm::ConstantStruct::getAnon(fields); |
| 1352 | assert(Res->getType() == ConvertMemberPointerType(MPT)); |
| 1353 | return Res; |
Reid Kleckner | 407e8b6 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 1354 | } |
| 1355 | |
| 1356 | llvm::Constant * |
Reid Kleckner | 7d0efb5 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 1357 | MicrosoftCXXABI::EmitFullMemberPointer(llvm::Constant *FirstField, |
| 1358 | bool IsMemberFunction, |
Reid Kleckner | 452abac | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 1359 | const CXXRecordDecl *RD, |
| 1360 | CharUnits NonVirtualBaseAdjustment) |
Reid Kleckner | 7d0efb5 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 1361 | { |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 1362 | MSInheritanceModel Inheritance = RD->getMSInheritanceModel(); |
Reid Kleckner | 7d0efb5 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 1363 | |
| 1364 | // Single inheritance class member pointer are represented as scalars instead |
| 1365 | // of aggregates. |
Reid Kleckner | 452abac | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 1366 | if (hasOnlyOneField(IsMemberFunction, Inheritance)) |
Reid Kleckner | 7d0efb5 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 1367 | return FirstField; |
| 1368 | |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 1369 | llvm::SmallVector<llvm::Constant *, 4> fields; |
Reid Kleckner | 7d0efb5 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 1370 | fields.push_back(FirstField); |
| 1371 | |
| 1372 | if (hasNonVirtualBaseAdjustmentField(IsMemberFunction, Inheritance)) |
Reid Kleckner | 452abac | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 1373 | fields.push_back(llvm::ConstantInt::get( |
| 1374 | CGM.IntTy, NonVirtualBaseAdjustment.getQuantity())); |
Reid Kleckner | 7d0efb5 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 1375 | |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 1376 | if (hasVBPtrOffsetField(Inheritance)) { |
Reid Kleckner | aec4409 | 2013-10-15 01:18:02 +0000 | [diff] [blame] | 1377 | CharUnits Offs = CharUnits::Zero(); |
| 1378 | if (RD->getNumVBases()) |
| 1379 | Offs = GetVBPtrOffsetFromBases(RD); |
| 1380 | fields.push_back(llvm::ConstantInt::get(CGM.IntTy, Offs.getQuantity())); |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 1381 | } |
Reid Kleckner | 7d0efb5 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 1382 | |
| 1383 | // The rest of the fields are adjusted by conversions to a more derived class. |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 1384 | if (hasVirtualBaseAdjustmentField(Inheritance)) |
| 1385 | fields.push_back(getZeroInt()); |
Reid Kleckner | 7d0efb5 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 1386 | |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 1387 | return llvm::ConstantStruct::getAnon(fields); |
Reid Kleckner | 407e8b6 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 1388 | } |
| 1389 | |
Reid Kleckner | 7d0efb5 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 1390 | llvm::Constant * |
| 1391 | MicrosoftCXXABI::EmitMemberDataPointer(const MemberPointerType *MPT, |
| 1392 | CharUnits offset) { |
| 1393 | const CXXRecordDecl *RD = MPT->getClass()->getAsCXXRecordDecl(); |
| 1394 | llvm::Constant *FirstField = |
| 1395 | llvm::ConstantInt::get(CGM.IntTy, offset.getQuantity()); |
Reid Kleckner | 452abac | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 1396 | return EmitFullMemberPointer(FirstField, /*IsMemberFunction=*/false, RD, |
| 1397 | CharUnits::Zero()); |
| 1398 | } |
| 1399 | |
| 1400 | llvm::Constant *MicrosoftCXXABI::EmitMemberPointer(const CXXMethodDecl *MD) { |
| 1401 | return BuildMemberPointer(MD->getParent(), MD, CharUnits::Zero()); |
| 1402 | } |
| 1403 | |
| 1404 | llvm::Constant *MicrosoftCXXABI::EmitMemberPointer(const APValue &MP, |
| 1405 | QualType MPType) { |
| 1406 | const MemberPointerType *MPT = MPType->castAs<MemberPointerType>(); |
| 1407 | const ValueDecl *MPD = MP.getMemberPointerDecl(); |
| 1408 | if (!MPD) |
| 1409 | return EmitNullMemberPointer(MPT); |
| 1410 | |
| 1411 | CharUnits ThisAdjustment = getMemberPointerPathAdjustment(MP); |
| 1412 | |
| 1413 | // FIXME PR15713: Support virtual inheritance paths. |
| 1414 | |
| 1415 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MPD)) |
| 1416 | return BuildMemberPointer(MPT->getClass()->getAsCXXRecordDecl(), |
| 1417 | MD, ThisAdjustment); |
| 1418 | |
| 1419 | CharUnits FieldOffset = |
| 1420 | getContext().toCharUnitsFromBits(getContext().getFieldOffset(MPD)); |
| 1421 | return EmitMemberDataPointer(MPT, ThisAdjustment + FieldOffset); |
Reid Kleckner | 7d0efb5 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 1422 | } |
| 1423 | |
| 1424 | llvm::Constant * |
Reid Kleckner | 452abac | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 1425 | MicrosoftCXXABI::BuildMemberPointer(const CXXRecordDecl *RD, |
| 1426 | const CXXMethodDecl *MD, |
| 1427 | CharUnits NonVirtualBaseAdjustment) { |
Reid Kleckner | 7d0efb5 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 1428 | assert(MD->isInstance() && "Member function must not be static!"); |
| 1429 | MD = MD->getCanonicalDecl(); |
Reid Kleckner | 7d0efb5 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 1430 | CodeGenTypes &Types = CGM.getTypes(); |
| 1431 | |
| 1432 | llvm::Constant *FirstField; |
Hans Wennborg | 88497d6 | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 1433 | if (!MD->isVirtual()) { |
Reid Kleckner | 7d0efb5 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 1434 | const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>(); |
| 1435 | llvm::Type *Ty; |
| 1436 | // Check whether the function has a computable LLVM signature. |
| 1437 | if (Types.isFuncTypeConvertible(FPT)) { |
| 1438 | // The function has a computable LLVM signature; use the correct type. |
| 1439 | Ty = Types.GetFunctionType(Types.arrangeCXXMethodDeclaration(MD)); |
| 1440 | } else { |
| 1441 | // Use an arbitrary non-function type to tell GetAddrOfFunction that the |
| 1442 | // function type is incomplete. |
| 1443 | Ty = CGM.PtrDiffTy; |
| 1444 | } |
| 1445 | FirstField = CGM.GetAddrOfFunction(MD, Ty); |
| 1446 | FirstField = llvm::ConstantExpr::getBitCast(FirstField, CGM.VoidPtrTy); |
Hans Wennborg | 88497d6 | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 1447 | } else { |
| 1448 | MicrosoftVTableContext::MethodVFTableLocation ML = |
| 1449 | CGM.getMicrosoftVTableContext().getMethodVFTableLocation(MD); |
| 1450 | if (MD->isVariadic()) { |
| 1451 | CGM.ErrorUnsupported(MD, "pointer to variadic virtual member function"); |
| 1452 | FirstField = llvm::Constant::getNullValue(CGM.VoidPtrTy); |
| 1453 | } else if (!CGM.getTypes().isFuncTypeConvertible( |
| 1454 | MD->getType()->castAs<FunctionType>())) { |
| 1455 | CGM.ErrorUnsupported(MD, "pointer to virtual member function with " |
| 1456 | "incomplete return or parameter type"); |
| 1457 | FirstField = llvm::Constant::getNullValue(CGM.VoidPtrTy); |
| 1458 | } else if (ML.VBase) { |
| 1459 | CGM.ErrorUnsupported(MD, "pointer to virtual member function overriding " |
| 1460 | "member function in virtual base class"); |
| 1461 | FirstField = llvm::Constant::getNullValue(CGM.VoidPtrTy); |
| 1462 | } else { |
| 1463 | SmallString<256> ThunkName; |
David Majnemer | 2a81645 | 2013-12-09 10:44:32 +0000 | [diff] [blame] | 1464 | CharUnits PointerWidth = getContext().toCharUnitsFromBits( |
| 1465 | getContext().getTargetInfo().getPointerWidth(0)); |
| 1466 | uint64_t OffsetInVFTable = ML.Index * PointerWidth.getQuantity(); |
Hans Wennborg | 88497d6 | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 1467 | llvm::raw_svector_ostream Out(ThunkName); |
| 1468 | getMangleContext().mangleVirtualMemPtrThunk(MD, OffsetInVFTable, Out); |
| 1469 | Out.flush(); |
| 1470 | |
| 1471 | llvm::Function *Thunk = EmitVirtualMemPtrThunk(MD, ThunkName.str()); |
| 1472 | FirstField = llvm::ConstantExpr::getBitCast(Thunk, CGM.VoidPtrTy); |
| 1473 | } |
Reid Kleckner | 7d0efb5 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 1474 | } |
| 1475 | |
| 1476 | // The rest of the fields are common with data member pointers. |
Reid Kleckner | 452abac | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 1477 | return EmitFullMemberPointer(FirstField, /*IsMemberFunction=*/true, RD, |
| 1478 | NonVirtualBaseAdjustment); |
Reid Kleckner | 7d0efb5 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 1479 | } |
| 1480 | |
Reid Kleckner | 700c3ee | 2013-04-30 20:15:14 +0000 | [diff] [blame] | 1481 | /// Member pointers are the same if they're either bitwise identical *or* both |
| 1482 | /// null. Null-ness for function members is determined by the first field, |
| 1483 | /// while for data member pointers we must compare all fields. |
| 1484 | llvm::Value * |
| 1485 | MicrosoftCXXABI::EmitMemberPointerComparison(CodeGenFunction &CGF, |
| 1486 | llvm::Value *L, |
| 1487 | llvm::Value *R, |
| 1488 | const MemberPointerType *MPT, |
| 1489 | bool Inequality) { |
| 1490 | CGBuilderTy &Builder = CGF.Builder; |
| 1491 | |
| 1492 | // Handle != comparisons by switching the sense of all boolean operations. |
| 1493 | llvm::ICmpInst::Predicate Eq; |
| 1494 | llvm::Instruction::BinaryOps And, Or; |
| 1495 | if (Inequality) { |
| 1496 | Eq = llvm::ICmpInst::ICMP_NE; |
| 1497 | And = llvm::Instruction::Or; |
| 1498 | Or = llvm::Instruction::And; |
| 1499 | } else { |
| 1500 | Eq = llvm::ICmpInst::ICMP_EQ; |
| 1501 | And = llvm::Instruction::And; |
| 1502 | Or = llvm::Instruction::Or; |
| 1503 | } |
| 1504 | |
| 1505 | // If this is a single field member pointer (single inheritance), this is a |
| 1506 | // single icmp. |
| 1507 | const CXXRecordDecl *RD = MPT->getClass()->getAsCXXRecordDecl(); |
| 1508 | MSInheritanceModel Inheritance = RD->getMSInheritanceModel(); |
Reid Kleckner | 452abac | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 1509 | if (hasOnlyOneField(MPT->isMemberFunctionPointer(), Inheritance)) |
Reid Kleckner | 700c3ee | 2013-04-30 20:15:14 +0000 | [diff] [blame] | 1510 | return Builder.CreateICmp(Eq, L, R); |
| 1511 | |
| 1512 | // Compare the first field. |
| 1513 | llvm::Value *L0 = Builder.CreateExtractValue(L, 0, "lhs.0"); |
| 1514 | llvm::Value *R0 = Builder.CreateExtractValue(R, 0, "rhs.0"); |
| 1515 | llvm::Value *Cmp0 = Builder.CreateICmp(Eq, L0, R0, "memptr.cmp.first"); |
| 1516 | |
| 1517 | // Compare everything other than the first field. |
| 1518 | llvm::Value *Res = 0; |
| 1519 | llvm::StructType *LType = cast<llvm::StructType>(L->getType()); |
| 1520 | for (unsigned I = 1, E = LType->getNumElements(); I != E; ++I) { |
| 1521 | llvm::Value *LF = Builder.CreateExtractValue(L, I); |
| 1522 | llvm::Value *RF = Builder.CreateExtractValue(R, I); |
| 1523 | llvm::Value *Cmp = Builder.CreateICmp(Eq, LF, RF, "memptr.cmp.rest"); |
| 1524 | if (Res) |
| 1525 | Res = Builder.CreateBinOp(And, Res, Cmp); |
| 1526 | else |
| 1527 | Res = Cmp; |
| 1528 | } |
| 1529 | |
| 1530 | // Check if the first field is 0 if this is a function pointer. |
| 1531 | if (MPT->isMemberFunctionPointer()) { |
| 1532 | // (l1 == r1 && ...) || l0 == 0 |
| 1533 | llvm::Value *Zero = llvm::Constant::getNullValue(L0->getType()); |
| 1534 | llvm::Value *IsZero = Builder.CreateICmp(Eq, L0, Zero, "memptr.cmp.iszero"); |
| 1535 | Res = Builder.CreateBinOp(Or, Res, IsZero); |
| 1536 | } |
| 1537 | |
| 1538 | // Combine the comparison of the first field, which must always be true for |
| 1539 | // this comparison to succeeed. |
| 1540 | return Builder.CreateBinOp(And, Res, Cmp0, "memptr.cmp"); |
| 1541 | } |
| 1542 | |
Reid Kleckner | 407e8b6 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 1543 | llvm::Value * |
| 1544 | MicrosoftCXXABI::EmitMemberPointerIsNotNull(CodeGenFunction &CGF, |
| 1545 | llvm::Value *MemPtr, |
| 1546 | const MemberPointerType *MPT) { |
| 1547 | CGBuilderTy &Builder = CGF.Builder; |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 1548 | llvm::SmallVector<llvm::Constant *, 4> fields; |
| 1549 | // We only need one field for member functions. |
| 1550 | if (MPT->isMemberFunctionPointer()) |
| 1551 | fields.push_back(llvm::Constant::getNullValue(CGM.VoidPtrTy)); |
| 1552 | else |
| 1553 | GetNullMemberPointerFields(MPT, fields); |
| 1554 | assert(!fields.empty()); |
| 1555 | llvm::Value *FirstField = MemPtr; |
| 1556 | if (MemPtr->getType()->isStructTy()) |
| 1557 | FirstField = Builder.CreateExtractValue(MemPtr, 0); |
| 1558 | llvm::Value *Res = Builder.CreateICmpNE(FirstField, fields[0], "memptr.cmp0"); |
Reid Kleckner | 407e8b6 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 1559 | |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 1560 | // For function member pointers, we only need to test the function pointer |
| 1561 | // field. The other fields if any can be garbage. |
| 1562 | if (MPT->isMemberFunctionPointer()) |
| 1563 | return Res; |
| 1564 | |
| 1565 | // Otherwise, emit a series of compares and combine the results. |
| 1566 | for (int I = 1, E = fields.size(); I < E; ++I) { |
| 1567 | llvm::Value *Field = Builder.CreateExtractValue(MemPtr, I); |
| 1568 | llvm::Value *Next = Builder.CreateICmpNE(Field, fields[I], "memptr.cmp"); |
| 1569 | Res = Builder.CreateAnd(Res, Next, "memptr.tobool"); |
| 1570 | } |
| 1571 | return Res; |
| 1572 | } |
| 1573 | |
Reid Kleckner | 452abac | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 1574 | bool MicrosoftCXXABI::MemberPointerConstantIsNull(const MemberPointerType *MPT, |
| 1575 | llvm::Constant *Val) { |
| 1576 | // Function pointers are null if the pointer in the first field is null. |
| 1577 | if (MPT->isMemberFunctionPointer()) { |
| 1578 | llvm::Constant *FirstField = Val->getType()->isStructTy() ? |
| 1579 | Val->getAggregateElement(0U) : Val; |
| 1580 | return FirstField->isNullValue(); |
| 1581 | } |
| 1582 | |
| 1583 | // If it's not a function pointer and it's zero initializable, we can easily |
| 1584 | // check zero. |
| 1585 | if (isZeroInitializable(MPT) && Val->isNullValue()) |
| 1586 | return true; |
| 1587 | |
| 1588 | // Otherwise, break down all the fields for comparison. Hopefully these |
| 1589 | // little Constants are reused, while a big null struct might not be. |
| 1590 | llvm::SmallVector<llvm::Constant *, 4> Fields; |
| 1591 | GetNullMemberPointerFields(MPT, Fields); |
| 1592 | if (Fields.size() == 1) { |
| 1593 | assert(Val->getType()->isIntegerTy()); |
| 1594 | return Val == Fields[0]; |
| 1595 | } |
| 1596 | |
| 1597 | unsigned I, E; |
| 1598 | for (I = 0, E = Fields.size(); I != E; ++I) { |
| 1599 | if (Val->getAggregateElement(I) != Fields[I]) |
| 1600 | break; |
| 1601 | } |
| 1602 | return I == E; |
| 1603 | } |
| 1604 | |
Reid Kleckner | d8cbeec | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 1605 | llvm::Value * |
| 1606 | MicrosoftCXXABI::GetVBaseOffsetFromVBPtr(CodeGenFunction &CGF, |
| 1607 | llvm::Value *This, |
Reid Kleckner | d8cbeec | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 1608 | llvm::Value *VBPtrOffset, |
Timur Iskhodzhanov | 07e6eff | 2013-10-27 17:10:27 +0000 | [diff] [blame] | 1609 | llvm::Value *VBTableOffset, |
Reid Kleckner | d8cbeec | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 1610 | llvm::Value **VBPtrOut) { |
| 1611 | CGBuilderTy &Builder = CGF.Builder; |
| 1612 | // Load the vbtable pointer from the vbptr in the instance. |
| 1613 | This = Builder.CreateBitCast(This, CGM.Int8PtrTy); |
| 1614 | llvm::Value *VBPtr = |
| 1615 | Builder.CreateInBoundsGEP(This, VBPtrOffset, "vbptr"); |
| 1616 | if (VBPtrOut) *VBPtrOut = VBPtr; |
| 1617 | VBPtr = Builder.CreateBitCast(VBPtr, CGM.Int8PtrTy->getPointerTo(0)); |
| 1618 | llvm::Value *VBTable = Builder.CreateLoad(VBPtr, "vbtable"); |
| 1619 | |
| 1620 | // Load an i32 offset from the vb-table. |
| 1621 | llvm::Value *VBaseOffs = Builder.CreateInBoundsGEP(VBTable, VBTableOffset); |
| 1622 | VBaseOffs = Builder.CreateBitCast(VBaseOffs, CGM.Int32Ty->getPointerTo(0)); |
| 1623 | return Builder.CreateLoad(VBaseOffs, "vbase_offs"); |
| 1624 | } |
| 1625 | |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 1626 | // Returns an adjusted base cast to i8*, since we do more address arithmetic on |
| 1627 | // it. |
| 1628 | llvm::Value * |
| 1629 | MicrosoftCXXABI::AdjustVirtualBase(CodeGenFunction &CGF, |
| 1630 | const CXXRecordDecl *RD, llvm::Value *Base, |
Reid Kleckner | d8cbeec | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 1631 | llvm::Value *VBTableOffset, |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 1632 | llvm::Value *VBPtrOffset) { |
| 1633 | CGBuilderTy &Builder = CGF.Builder; |
| 1634 | Base = Builder.CreateBitCast(Base, CGM.Int8PtrTy); |
| 1635 | llvm::BasicBlock *OriginalBB = 0; |
| 1636 | llvm::BasicBlock *SkipAdjustBB = 0; |
| 1637 | llvm::BasicBlock *VBaseAdjustBB = 0; |
| 1638 | |
| 1639 | // In the unspecified inheritance model, there might not be a vbtable at all, |
| 1640 | // in which case we need to skip the virtual base lookup. If there is a |
| 1641 | // vbtable, the first entry is a no-op entry that gives back the original |
| 1642 | // base, so look for a virtual base adjustment offset of zero. |
| 1643 | if (VBPtrOffset) { |
| 1644 | OriginalBB = Builder.GetInsertBlock(); |
| 1645 | VBaseAdjustBB = CGF.createBasicBlock("memptr.vadjust"); |
| 1646 | SkipAdjustBB = CGF.createBasicBlock("memptr.skip_vadjust"); |
| 1647 | llvm::Value *IsVirtual = |
Reid Kleckner | d8cbeec | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 1648 | Builder.CreateICmpNE(VBTableOffset, getZeroInt(), |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 1649 | "memptr.is_vbase"); |
| 1650 | Builder.CreateCondBr(IsVirtual, VBaseAdjustBB, SkipAdjustBB); |
| 1651 | CGF.EmitBlock(VBaseAdjustBB); |
Reid Kleckner | 407e8b6 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 1652 | } |
| 1653 | |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 1654 | // If we weren't given a dynamic vbptr offset, RD should be complete and we'll |
| 1655 | // know the vbptr offset. |
| 1656 | if (!VBPtrOffset) { |
Reid Kleckner | d8cbeec | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 1657 | CharUnits offs = CharUnits::Zero(); |
| 1658 | if (RD->getNumVBases()) { |
| 1659 | offs = GetVBPtrOffsetFromBases(RD); |
| 1660 | } |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 1661 | VBPtrOffset = llvm::ConstantInt::get(CGM.IntTy, offs.getQuantity()); |
| 1662 | } |
Reid Kleckner | d8cbeec | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 1663 | llvm::Value *VBPtr = 0; |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 1664 | llvm::Value *VBaseOffs = |
Timur Iskhodzhanov | 07e6eff | 2013-10-27 17:10:27 +0000 | [diff] [blame] | 1665 | GetVBaseOffsetFromVBPtr(CGF, Base, VBPtrOffset, VBTableOffset, &VBPtr); |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 1666 | llvm::Value *AdjustedBase = Builder.CreateInBoundsGEP(VBPtr, VBaseOffs); |
| 1667 | |
| 1668 | // Merge control flow with the case where we didn't have to adjust. |
| 1669 | if (VBaseAdjustBB) { |
| 1670 | Builder.CreateBr(SkipAdjustBB); |
| 1671 | CGF.EmitBlock(SkipAdjustBB); |
| 1672 | llvm::PHINode *Phi = Builder.CreatePHI(CGM.Int8PtrTy, 2, "memptr.base"); |
| 1673 | Phi->addIncoming(Base, OriginalBB); |
| 1674 | Phi->addIncoming(AdjustedBase, VBaseAdjustBB); |
| 1675 | return Phi; |
| 1676 | } |
| 1677 | return AdjustedBase; |
Reid Kleckner | 407e8b6 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 1678 | } |
| 1679 | |
| 1680 | llvm::Value * |
| 1681 | MicrosoftCXXABI::EmitMemberDataPointerAddress(CodeGenFunction &CGF, |
| 1682 | llvm::Value *Base, |
| 1683 | llvm::Value *MemPtr, |
| 1684 | const MemberPointerType *MPT) { |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 1685 | assert(MPT->isMemberDataPointer()); |
Reid Kleckner | 407e8b6 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 1686 | unsigned AS = Base->getType()->getPointerAddressSpace(); |
| 1687 | llvm::Type *PType = |
| 1688 | CGF.ConvertTypeForMem(MPT->getPointeeType())->getPointerTo(AS); |
| 1689 | CGBuilderTy &Builder = CGF.Builder; |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 1690 | const CXXRecordDecl *RD = MPT->getClass()->getAsCXXRecordDecl(); |
| 1691 | MSInheritanceModel Inheritance = RD->getMSInheritanceModel(); |
Reid Kleckner | 407e8b6 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 1692 | |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 1693 | // Extract the fields we need, regardless of model. We'll apply them if we |
| 1694 | // have them. |
| 1695 | llvm::Value *FieldOffset = MemPtr; |
| 1696 | llvm::Value *VirtualBaseAdjustmentOffset = 0; |
| 1697 | llvm::Value *VBPtrOffset = 0; |
| 1698 | if (MemPtr->getType()->isStructTy()) { |
| 1699 | // We need to extract values. |
| 1700 | unsigned I = 0; |
| 1701 | FieldOffset = Builder.CreateExtractValue(MemPtr, I++); |
| 1702 | if (hasVBPtrOffsetField(Inheritance)) |
| 1703 | VBPtrOffset = Builder.CreateExtractValue(MemPtr, I++); |
| 1704 | if (hasVirtualBaseAdjustmentField(Inheritance)) |
| 1705 | VirtualBaseAdjustmentOffset = Builder.CreateExtractValue(MemPtr, I++); |
Reid Kleckner | 407e8b6 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 1706 | } |
| 1707 | |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 1708 | if (VirtualBaseAdjustmentOffset) { |
| 1709 | Base = AdjustVirtualBase(CGF, RD, Base, VirtualBaseAdjustmentOffset, |
| 1710 | VBPtrOffset); |
Reid Kleckner | 407e8b6 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 1711 | } |
Reid Kleckner | ae94512 | 2013-12-05 22:44:07 +0000 | [diff] [blame] | 1712 | |
| 1713 | // Cast to char*. |
| 1714 | Base = Builder.CreateBitCast(Base, Builder.getInt8Ty()->getPointerTo(AS)); |
| 1715 | |
| 1716 | // Apply the offset, which we assume is non-null. |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 1717 | llvm::Value *Addr = |
| 1718 | Builder.CreateInBoundsGEP(Base, FieldOffset, "memptr.offset"); |
Reid Kleckner | 407e8b6 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 1719 | |
| 1720 | // Cast the address to the appropriate pointer type, adopting the address |
| 1721 | // space of the base pointer. |
| 1722 | return Builder.CreateBitCast(Addr, PType); |
| 1723 | } |
| 1724 | |
Reid Kleckner | 452abac | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 1725 | static MSInheritanceModel |
| 1726 | getInheritanceFromMemptr(const MemberPointerType *MPT) { |
| 1727 | return MPT->getClass()->getAsCXXRecordDecl()->getMSInheritanceModel(); |
| 1728 | } |
| 1729 | |
| 1730 | llvm::Value * |
| 1731 | MicrosoftCXXABI::EmitMemberPointerConversion(CodeGenFunction &CGF, |
| 1732 | const CastExpr *E, |
| 1733 | llvm::Value *Src) { |
| 1734 | assert(E->getCastKind() == CK_DerivedToBaseMemberPointer || |
| 1735 | E->getCastKind() == CK_BaseToDerivedMemberPointer || |
| 1736 | E->getCastKind() == CK_ReinterpretMemberPointer); |
| 1737 | |
| 1738 | // Use constant emission if we can. |
| 1739 | if (isa<llvm::Constant>(Src)) |
| 1740 | return EmitMemberPointerConversion(E, cast<llvm::Constant>(Src)); |
| 1741 | |
| 1742 | // We may be adding or dropping fields from the member pointer, so we need |
| 1743 | // both types and the inheritance models of both records. |
| 1744 | const MemberPointerType *SrcTy = |
| 1745 | E->getSubExpr()->getType()->castAs<MemberPointerType>(); |
| 1746 | const MemberPointerType *DstTy = E->getType()->castAs<MemberPointerType>(); |
| 1747 | MSInheritanceModel SrcInheritance = getInheritanceFromMemptr(SrcTy); |
| 1748 | MSInheritanceModel DstInheritance = getInheritanceFromMemptr(DstTy); |
| 1749 | bool IsFunc = SrcTy->isMemberFunctionPointer(); |
| 1750 | |
| 1751 | // If the classes use the same null representation, reinterpret_cast is a nop. |
| 1752 | bool IsReinterpret = E->getCastKind() == CK_ReinterpretMemberPointer; |
| 1753 | if (IsReinterpret && (IsFunc || |
| 1754 | nullFieldOffsetIsZero(SrcInheritance) == |
| 1755 | nullFieldOffsetIsZero(DstInheritance))) |
| 1756 | return Src; |
| 1757 | |
| 1758 | CGBuilderTy &Builder = CGF.Builder; |
| 1759 | |
| 1760 | // Branch past the conversion if Src is null. |
| 1761 | llvm::Value *IsNotNull = EmitMemberPointerIsNotNull(CGF, Src, SrcTy); |
| 1762 | llvm::Constant *DstNull = EmitNullMemberPointer(DstTy); |
| 1763 | |
| 1764 | // C++ 5.2.10p9: The null member pointer value is converted to the null member |
| 1765 | // pointer value of the destination type. |
| 1766 | if (IsReinterpret) { |
| 1767 | // For reinterpret casts, sema ensures that src and dst are both functions |
| 1768 | // or data and have the same size, which means the LLVM types should match. |
| 1769 | assert(Src->getType() == DstNull->getType()); |
| 1770 | return Builder.CreateSelect(IsNotNull, Src, DstNull); |
| 1771 | } |
| 1772 | |
| 1773 | llvm::BasicBlock *OriginalBB = Builder.GetInsertBlock(); |
| 1774 | llvm::BasicBlock *ConvertBB = CGF.createBasicBlock("memptr.convert"); |
| 1775 | llvm::BasicBlock *ContinueBB = CGF.createBasicBlock("memptr.converted"); |
| 1776 | Builder.CreateCondBr(IsNotNull, ConvertBB, ContinueBB); |
| 1777 | CGF.EmitBlock(ConvertBB); |
| 1778 | |
| 1779 | // Decompose src. |
| 1780 | llvm::Value *FirstField = Src; |
| 1781 | llvm::Value *NonVirtualBaseAdjustment = 0; |
| 1782 | llvm::Value *VirtualBaseAdjustmentOffset = 0; |
| 1783 | llvm::Value *VBPtrOffset = 0; |
| 1784 | if (!hasOnlyOneField(IsFunc, SrcInheritance)) { |
| 1785 | // We need to extract values. |
| 1786 | unsigned I = 0; |
| 1787 | FirstField = Builder.CreateExtractValue(Src, I++); |
| 1788 | if (hasNonVirtualBaseAdjustmentField(IsFunc, SrcInheritance)) |
| 1789 | NonVirtualBaseAdjustment = Builder.CreateExtractValue(Src, I++); |
| 1790 | if (hasVBPtrOffsetField(SrcInheritance)) |
| 1791 | VBPtrOffset = Builder.CreateExtractValue(Src, I++); |
| 1792 | if (hasVirtualBaseAdjustmentField(SrcInheritance)) |
| 1793 | VirtualBaseAdjustmentOffset = Builder.CreateExtractValue(Src, I++); |
| 1794 | } |
| 1795 | |
| 1796 | // For data pointers, we adjust the field offset directly. For functions, we |
| 1797 | // have a separate field. |
| 1798 | llvm::Constant *Adj = getMemberPointerAdjustment(E); |
| 1799 | if (Adj) { |
| 1800 | Adj = llvm::ConstantExpr::getTruncOrBitCast(Adj, CGM.IntTy); |
| 1801 | llvm::Value *&NVAdjustField = IsFunc ? NonVirtualBaseAdjustment : FirstField; |
| 1802 | bool isDerivedToBase = (E->getCastKind() == CK_DerivedToBaseMemberPointer); |
| 1803 | if (!NVAdjustField) // If this field didn't exist in src, it's zero. |
| 1804 | NVAdjustField = getZeroInt(); |
| 1805 | if (isDerivedToBase) |
| 1806 | NVAdjustField = Builder.CreateNSWSub(NVAdjustField, Adj, "adj"); |
| 1807 | else |
| 1808 | NVAdjustField = Builder.CreateNSWAdd(NVAdjustField, Adj, "adj"); |
| 1809 | } |
| 1810 | |
| 1811 | // FIXME PR15713: Support conversions through virtually derived classes. |
| 1812 | |
| 1813 | // Recompose dst from the null struct and the adjusted fields from src. |
| 1814 | llvm::Value *Dst; |
| 1815 | if (hasOnlyOneField(IsFunc, DstInheritance)) { |
| 1816 | Dst = FirstField; |
| 1817 | } else { |
| 1818 | Dst = llvm::UndefValue::get(DstNull->getType()); |
| 1819 | unsigned Idx = 0; |
| 1820 | Dst = Builder.CreateInsertValue(Dst, FirstField, Idx++); |
| 1821 | if (hasNonVirtualBaseAdjustmentField(IsFunc, DstInheritance)) |
| 1822 | Dst = Builder.CreateInsertValue( |
| 1823 | Dst, getValueOrZeroInt(NonVirtualBaseAdjustment), Idx++); |
| 1824 | if (hasVBPtrOffsetField(DstInheritance)) |
| 1825 | Dst = Builder.CreateInsertValue( |
| 1826 | Dst, getValueOrZeroInt(VBPtrOffset), Idx++); |
| 1827 | if (hasVirtualBaseAdjustmentField(DstInheritance)) |
| 1828 | Dst = Builder.CreateInsertValue( |
| 1829 | Dst, getValueOrZeroInt(VirtualBaseAdjustmentOffset), Idx++); |
| 1830 | } |
| 1831 | Builder.CreateBr(ContinueBB); |
| 1832 | |
| 1833 | // In the continuation, choose between DstNull and Dst. |
| 1834 | CGF.EmitBlock(ContinueBB); |
| 1835 | llvm::PHINode *Phi = Builder.CreatePHI(DstNull->getType(), 2, "memptr.converted"); |
| 1836 | Phi->addIncoming(DstNull, OriginalBB); |
| 1837 | Phi->addIncoming(Dst, ConvertBB); |
| 1838 | return Phi; |
| 1839 | } |
| 1840 | |
| 1841 | llvm::Constant * |
| 1842 | MicrosoftCXXABI::EmitMemberPointerConversion(const CastExpr *E, |
| 1843 | llvm::Constant *Src) { |
| 1844 | const MemberPointerType *SrcTy = |
| 1845 | E->getSubExpr()->getType()->castAs<MemberPointerType>(); |
| 1846 | const MemberPointerType *DstTy = E->getType()->castAs<MemberPointerType>(); |
| 1847 | |
| 1848 | // If src is null, emit a new null for dst. We can't return src because dst |
| 1849 | // might have a new representation. |
| 1850 | if (MemberPointerConstantIsNull(SrcTy, Src)) |
| 1851 | return EmitNullMemberPointer(DstTy); |
| 1852 | |
| 1853 | // We don't need to do anything for reinterpret_casts of non-null member |
| 1854 | // pointers. We should only get here when the two type representations have |
| 1855 | // the same size. |
| 1856 | if (E->getCastKind() == CK_ReinterpretMemberPointer) |
| 1857 | return Src; |
| 1858 | |
| 1859 | MSInheritanceModel SrcInheritance = getInheritanceFromMemptr(SrcTy); |
| 1860 | MSInheritanceModel DstInheritance = getInheritanceFromMemptr(DstTy); |
| 1861 | |
| 1862 | // Decompose src. |
| 1863 | llvm::Constant *FirstField = Src; |
| 1864 | llvm::Constant *NonVirtualBaseAdjustment = 0; |
| 1865 | llvm::Constant *VirtualBaseAdjustmentOffset = 0; |
| 1866 | llvm::Constant *VBPtrOffset = 0; |
| 1867 | bool IsFunc = SrcTy->isMemberFunctionPointer(); |
| 1868 | if (!hasOnlyOneField(IsFunc, SrcInheritance)) { |
| 1869 | // We need to extract values. |
| 1870 | unsigned I = 0; |
| 1871 | FirstField = Src->getAggregateElement(I++); |
| 1872 | if (hasNonVirtualBaseAdjustmentField(IsFunc, SrcInheritance)) |
| 1873 | NonVirtualBaseAdjustment = Src->getAggregateElement(I++); |
| 1874 | if (hasVBPtrOffsetField(SrcInheritance)) |
| 1875 | VBPtrOffset = Src->getAggregateElement(I++); |
| 1876 | if (hasVirtualBaseAdjustmentField(SrcInheritance)) |
| 1877 | VirtualBaseAdjustmentOffset = Src->getAggregateElement(I++); |
| 1878 | } |
| 1879 | |
| 1880 | // For data pointers, we adjust the field offset directly. For functions, we |
| 1881 | // have a separate field. |
| 1882 | llvm::Constant *Adj = getMemberPointerAdjustment(E); |
| 1883 | if (Adj) { |
| 1884 | Adj = llvm::ConstantExpr::getTruncOrBitCast(Adj, CGM.IntTy); |
| 1885 | llvm::Constant *&NVAdjustField = |
| 1886 | IsFunc ? NonVirtualBaseAdjustment : FirstField; |
| 1887 | bool IsDerivedToBase = (E->getCastKind() == CK_DerivedToBaseMemberPointer); |
| 1888 | if (!NVAdjustField) // If this field didn't exist in src, it's zero. |
| 1889 | NVAdjustField = getZeroInt(); |
| 1890 | if (IsDerivedToBase) |
| 1891 | NVAdjustField = llvm::ConstantExpr::getNSWSub(NVAdjustField, Adj); |
| 1892 | else |
| 1893 | NVAdjustField = llvm::ConstantExpr::getNSWAdd(NVAdjustField, Adj); |
| 1894 | } |
| 1895 | |
| 1896 | // FIXME PR15713: Support conversions through virtually derived classes. |
| 1897 | |
| 1898 | // Recompose dst from the null struct and the adjusted fields from src. |
| 1899 | if (hasOnlyOneField(IsFunc, DstInheritance)) |
| 1900 | return FirstField; |
| 1901 | |
| 1902 | llvm::SmallVector<llvm::Constant *, 4> Fields; |
| 1903 | Fields.push_back(FirstField); |
| 1904 | if (hasNonVirtualBaseAdjustmentField(IsFunc, DstInheritance)) |
| 1905 | Fields.push_back(getConstantOrZeroInt(NonVirtualBaseAdjustment)); |
| 1906 | if (hasVBPtrOffsetField(DstInheritance)) |
| 1907 | Fields.push_back(getConstantOrZeroInt(VBPtrOffset)); |
| 1908 | if (hasVirtualBaseAdjustmentField(DstInheritance)) |
| 1909 | Fields.push_back(getConstantOrZeroInt(VirtualBaseAdjustmentOffset)); |
| 1910 | return llvm::ConstantStruct::getAnon(Fields); |
| 1911 | } |
| 1912 | |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 1913 | llvm::Value * |
| 1914 | MicrosoftCXXABI::EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF, |
| 1915 | llvm::Value *&This, |
| 1916 | llvm::Value *MemPtr, |
| 1917 | const MemberPointerType *MPT) { |
| 1918 | assert(MPT->isMemberFunctionPointer()); |
| 1919 | const FunctionProtoType *FPT = |
| 1920 | MPT->getPointeeType()->castAs<FunctionProtoType>(); |
| 1921 | const CXXRecordDecl *RD = MPT->getClass()->getAsCXXRecordDecl(); |
| 1922 | llvm::FunctionType *FTy = |
| 1923 | CGM.getTypes().GetFunctionType( |
| 1924 | CGM.getTypes().arrangeCXXMethodType(RD, FPT)); |
| 1925 | CGBuilderTy &Builder = CGF.Builder; |
| 1926 | |
| 1927 | MSInheritanceModel Inheritance = RD->getMSInheritanceModel(); |
| 1928 | |
| 1929 | // Extract the fields we need, regardless of model. We'll apply them if we |
| 1930 | // have them. |
| 1931 | llvm::Value *FunctionPointer = MemPtr; |
| 1932 | llvm::Value *NonVirtualBaseAdjustment = NULL; |
| 1933 | llvm::Value *VirtualBaseAdjustmentOffset = NULL; |
| 1934 | llvm::Value *VBPtrOffset = NULL; |
| 1935 | if (MemPtr->getType()->isStructTy()) { |
| 1936 | // We need to extract values. |
| 1937 | unsigned I = 0; |
| 1938 | FunctionPointer = Builder.CreateExtractValue(MemPtr, I++); |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 1939 | if (hasNonVirtualBaseAdjustmentField(MPT, Inheritance)) |
| 1940 | NonVirtualBaseAdjustment = Builder.CreateExtractValue(MemPtr, I++); |
Reid Kleckner | 7d0efb5 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 1941 | if (hasVBPtrOffsetField(Inheritance)) |
| 1942 | VBPtrOffset = Builder.CreateExtractValue(MemPtr, I++); |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 1943 | if (hasVirtualBaseAdjustmentField(Inheritance)) |
| 1944 | VirtualBaseAdjustmentOffset = Builder.CreateExtractValue(MemPtr, I++); |
| 1945 | } |
| 1946 | |
| 1947 | if (VirtualBaseAdjustmentOffset) { |
| 1948 | This = AdjustVirtualBase(CGF, RD, This, VirtualBaseAdjustmentOffset, |
| 1949 | VBPtrOffset); |
| 1950 | } |
| 1951 | |
| 1952 | if (NonVirtualBaseAdjustment) { |
| 1953 | // Apply the adjustment and cast back to the original struct type. |
| 1954 | llvm::Value *Ptr = Builder.CreateBitCast(This, Builder.getInt8PtrTy()); |
| 1955 | Ptr = Builder.CreateInBoundsGEP(Ptr, NonVirtualBaseAdjustment); |
| 1956 | This = Builder.CreateBitCast(Ptr, This->getType(), "this.adjusted"); |
| 1957 | } |
| 1958 | |
| 1959 | return Builder.CreateBitCast(FunctionPointer, FTy->getPointerTo()); |
| 1960 | } |
| 1961 | |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 1962 | CGCXXABI *clang::CodeGen::CreateMicrosoftCXXABI(CodeGenModule &CGM) { |
Charles Davis | 74ce859 | 2010-06-09 23:25:41 +0000 | [diff] [blame] | 1963 | return new MicrosoftCXXABI(CGM); |
| 1964 | } |