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