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