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