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 // 10 // This provides C++ code generation targeting the Microsoft Visual C++ ABI. 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 "CGVTables.h" 19 #include "CodeGenModule.h" 20 #include "clang/AST/Decl.h" 21 #include "clang/AST/DeclCXX.h" 22 #include "clang/AST/VTableBuilder.h" 23 #include "llvm/ADT/StringExtras.h" 24 #include "llvm/ADT/StringSet.h" 25 #include "llvm/IR/CallSite.h" 26 27 using namespace clang; 28 using namespace CodeGen; 29 30 namespace { 31 32 /// Holds all the vbtable globals for a given class. 33 struct VBTableGlobals { 34 const VPtrInfoVector *VBTables; 35 SmallVector<llvm::GlobalVariable *, 2> Globals; 36 }; 37 38 class MicrosoftCXXABI : public CGCXXABI { 39 public: 40 MicrosoftCXXABI(CodeGenModule &CGM) 41 : CGCXXABI(CGM), BaseClassDescriptorType(nullptr), 42 ClassHierarchyDescriptorType(nullptr), 43 CompleteObjectLocatorType(nullptr) {} 44 45 bool HasThisReturn(GlobalDecl GD) const override; 46 bool hasMostDerivedReturn(GlobalDecl GD) const override; 47 48 bool classifyReturnType(CGFunctionInfo &FI) const override; 49 50 RecordArgABI getRecordArgABI(const CXXRecordDecl *RD) const override; 51 52 bool isSRetParameterAfterThis() const override { return true; } 53 54 size_t getSrcArgforCopyCtor(const CXXConstructorDecl *CD, 55 FunctionArgList &Args) const override { 56 assert(Args.size() >= 2 && 57 "expected the arglist to have at least two args!"); 58 // The 'most_derived' parameter goes second if the ctor is variadic and 59 // has v-bases. 60 if (CD->getParent()->getNumVBases() > 0 && 61 CD->getType()->castAs<FunctionProtoType>()->isVariadic()) 62 return 2; 63 return 1; 64 } 65 66 StringRef GetPureVirtualCallName() override { return "_purecall"; } 67 StringRef GetDeletedVirtualCallName() override { return "_purecall"; } 68 69 void emitVirtualObjectDelete(CodeGenFunction &CGF, const CXXDeleteExpr *DE, 70 llvm::Value *Ptr, QualType ElementType, 71 const CXXDestructorDecl *Dtor) override; 72 73 llvm::GlobalVariable *getMSCompleteObjectLocator(const CXXRecordDecl *RD, 74 const VPtrInfo *Info); 75 76 llvm::Constant *getAddrOfRTTIDescriptor(QualType Ty) override; 77 78 bool shouldTypeidBeNullChecked(bool IsDeref, QualType SrcRecordTy) override; 79 void EmitBadTypeidCall(CodeGenFunction &CGF) override; 80 llvm::Value *EmitTypeid(CodeGenFunction &CGF, QualType SrcRecordTy, 81 llvm::Value *ThisPtr, 82 llvm::Type *StdTypeInfoPtrTy) override; 83 84 bool shouldDynamicCastCallBeNullChecked(bool SrcIsPtr, 85 QualType SrcRecordTy) override; 86 87 llvm::Value *EmitDynamicCastCall(CodeGenFunction &CGF, llvm::Value *Value, 88 QualType SrcRecordTy, QualType DestTy, 89 QualType DestRecordTy, 90 llvm::BasicBlock *CastEnd) override; 91 92 llvm::Value *EmitDynamicCastToVoid(CodeGenFunction &CGF, llvm::Value *Value, 93 QualType SrcRecordTy, 94 QualType DestTy) override; 95 96 bool EmitBadCastCall(CodeGenFunction &CGF) override; 97 98 llvm::Value * 99 GetVirtualBaseClassOffset(CodeGenFunction &CGF, llvm::Value *This, 100 const CXXRecordDecl *ClassDecl, 101 const CXXRecordDecl *BaseClassDecl) override; 102 103 llvm::BasicBlock * 104 EmitCtorCompleteObjectHandler(CodeGenFunction &CGF, 105 const CXXRecordDecl *RD) override; 106 107 void initializeHiddenVirtualInheritanceMembers(CodeGenFunction &CGF, 108 const CXXRecordDecl *RD) override; 109 110 void EmitCXXConstructors(const CXXConstructorDecl *D) override; 111 112 // Background on MSVC destructors 113 // ============================== 114 // 115 // Both Itanium and MSVC ABIs have destructor variants. The variant names 116 // roughly correspond in the following way: 117 // Itanium Microsoft 118 // Base -> no name, just ~Class 119 // Complete -> vbase destructor 120 // Deleting -> scalar deleting destructor 121 // vector deleting destructor 122 // 123 // The base and complete destructors are the same as in Itanium, although the 124 // complete destructor does not accept a VTT parameter when there are virtual 125 // bases. A separate mechanism involving vtordisps is used to ensure that 126 // virtual methods of destroyed subobjects are not called. 127 // 128 // The deleting destructors accept an i32 bitfield as a second parameter. Bit 129 // 1 indicates if the memory should be deleted. Bit 2 indicates if the this 130 // pointer points to an array. The scalar deleting destructor assumes that 131 // bit 2 is zero, and therefore does not contain a loop. 132 // 133 // For virtual destructors, only one entry is reserved in the vftable, and it 134 // always points to the vector deleting destructor. The vector deleting 135 // destructor is the most general, so it can be used to destroy objects in 136 // place, delete single heap objects, or delete arrays. 137 // 138 // A TU defining a non-inline destructor is only guaranteed to emit a base 139 // destructor, and all of the other variants are emitted on an as-needed basis 140 // in COMDATs. Because a non-base destructor can be emitted in a TU that 141 // lacks a definition for the destructor, non-base destructors must always 142 // delegate to or alias the base destructor. 143 144 void buildStructorSignature(const CXXMethodDecl *MD, StructorType T, 145 SmallVectorImpl<CanQualType> &ArgTys) override; 146 147 /// Non-base dtors should be emitted as delegating thunks in this ABI. 148 bool useThunkForDtorVariant(const CXXDestructorDecl *Dtor, 149 CXXDtorType DT) const override { 150 return DT != Dtor_Base; 151 } 152 153 void EmitCXXDestructors(const CXXDestructorDecl *D) override; 154 155 const CXXRecordDecl * 156 getThisArgumentTypeForMethod(const CXXMethodDecl *MD) override { 157 MD = MD->getCanonicalDecl(); 158 if (MD->isVirtual() && !isa<CXXDestructorDecl>(MD)) { 159 MicrosoftVTableContext::MethodVFTableLocation ML = 160 CGM.getMicrosoftVTableContext().getMethodVFTableLocation(MD); 161 // The vbases might be ordered differently in the final overrider object 162 // and the complete object, so the "this" argument may sometimes point to 163 // memory that has no particular type (e.g. past the complete object). 164 // In this case, we just use a generic pointer type. 165 // FIXME: might want to have a more precise type in the non-virtual 166 // multiple inheritance case. 167 if (ML.VBase || !ML.VFPtrOffset.isZero()) 168 return nullptr; 169 } 170 return MD->getParent(); 171 } 172 173 llvm::Value * 174 adjustThisArgumentForVirtualFunctionCall(CodeGenFunction &CGF, GlobalDecl GD, 175 llvm::Value *This, 176 bool VirtualCall) override; 177 178 void addImplicitStructorParams(CodeGenFunction &CGF, QualType &ResTy, 179 FunctionArgList &Params) override; 180 181 llvm::Value *adjustThisParameterInVirtualFunctionPrologue( 182 CodeGenFunction &CGF, GlobalDecl GD, llvm::Value *This) override; 183 184 void EmitInstanceFunctionProlog(CodeGenFunction &CGF) override; 185 186 unsigned addImplicitConstructorArgs(CodeGenFunction &CGF, 187 const CXXConstructorDecl *D, 188 CXXCtorType Type, bool ForVirtualBase, 189 bool Delegating, 190 CallArgList &Args) override; 191 192 void EmitDestructorCall(CodeGenFunction &CGF, const CXXDestructorDecl *DD, 193 CXXDtorType Type, bool ForVirtualBase, 194 bool Delegating, llvm::Value *This) override; 195 196 void emitVTableDefinitions(CodeGenVTables &CGVT, 197 const CXXRecordDecl *RD) override; 198 199 llvm::Value *getVTableAddressPointInStructor( 200 CodeGenFunction &CGF, const CXXRecordDecl *VTableClass, 201 BaseSubobject Base, const CXXRecordDecl *NearestVBase, 202 bool &NeedsVirtualOffset) override; 203 204 llvm::Constant * 205 getVTableAddressPointForConstExpr(BaseSubobject Base, 206 const CXXRecordDecl *VTableClass) override; 207 208 llvm::GlobalVariable *getAddrOfVTable(const CXXRecordDecl *RD, 209 CharUnits VPtrOffset) override; 210 211 llvm::Value *getVirtualFunctionPointer(CodeGenFunction &CGF, GlobalDecl GD, 212 llvm::Value *This, 213 llvm::Type *Ty) override; 214 215 llvm::Value *EmitVirtualDestructorCall(CodeGenFunction &CGF, 216 const CXXDestructorDecl *Dtor, 217 CXXDtorType DtorType, 218 llvm::Value *This, 219 const CXXMemberCallExpr *CE) override; 220 221 void adjustCallArgsForDestructorThunk(CodeGenFunction &CGF, GlobalDecl GD, 222 CallArgList &CallArgs) override { 223 assert(GD.getDtorType() == Dtor_Deleting && 224 "Only deleting destructor thunks are available in this ABI"); 225 CallArgs.add(RValue::get(getStructorImplicitParamValue(CGF)), 226 CGM.getContext().IntTy); 227 } 228 229 void emitVirtualInheritanceTables(const CXXRecordDecl *RD) override; 230 231 llvm::GlobalVariable * 232 getAddrOfVBTable(const VPtrInfo &VBT, const CXXRecordDecl *RD, 233 llvm::GlobalVariable::LinkageTypes Linkage); 234 235 void emitVBTableDefinition(const VPtrInfo &VBT, const CXXRecordDecl *RD, 236 llvm::GlobalVariable *GV) const; 237 238 void setThunkLinkage(llvm::Function *Thunk, bool ForVTable, 239 GlobalDecl GD, bool ReturnAdjustment) override { 240 // Never dllimport/dllexport thunks. 241 Thunk->setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass); 242 243 GVALinkage Linkage = 244 getContext().GetGVALinkageForFunction(cast<FunctionDecl>(GD.getDecl())); 245 246 if (Linkage == GVA_Internal) 247 Thunk->setLinkage(llvm::GlobalValue::InternalLinkage); 248 else if (ReturnAdjustment) 249 Thunk->setLinkage(llvm::GlobalValue::WeakODRLinkage); 250 else 251 Thunk->setLinkage(llvm::GlobalValue::LinkOnceODRLinkage); 252 } 253 254 llvm::Value *performThisAdjustment(CodeGenFunction &CGF, llvm::Value *This, 255 const ThisAdjustment &TA) override; 256 257 llvm::Value *performReturnAdjustment(CodeGenFunction &CGF, llvm::Value *Ret, 258 const ReturnAdjustment &RA) override; 259 260 void EmitThreadLocalInitFuncs( 261 CodeGenModule &CGM, 262 ArrayRef<std::pair<const VarDecl *, llvm::GlobalVariable *>> 263 CXXThreadLocals, 264 ArrayRef<llvm::Function *> CXXThreadLocalInits, 265 ArrayRef<llvm::GlobalVariable *> CXXThreadLocalInitVars) override; 266 267 bool usesThreadWrapperFunction() const override { return false; } 268 LValue EmitThreadLocalVarDeclLValue(CodeGenFunction &CGF, const VarDecl *VD, 269 QualType LValType) override; 270 271 void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D, 272 llvm::GlobalVariable *DeclPtr, 273 bool PerformInit) override; 274 void registerGlobalDtor(CodeGenFunction &CGF, const VarDecl &D, 275 llvm::Constant *Dtor, llvm::Constant *Addr) override; 276 277 // ==== Notes on array cookies ========= 278 // 279 // MSVC seems to only use cookies when the class has a destructor; a 280 // two-argument usual array deallocation function isn't sufficient. 281 // 282 // For example, this code prints "100" and "1": 283 // struct A { 284 // char x; 285 // void *operator new[](size_t sz) { 286 // printf("%u\n", sz); 287 // return malloc(sz); 288 // } 289 // void operator delete[](void *p, size_t sz) { 290 // printf("%u\n", sz); 291 // free(p); 292 // } 293 // }; 294 // int main() { 295 // A *p = new A[100]; 296 // delete[] p; 297 // } 298 // Whereas it prints "104" and "104" if you give A a destructor. 299 300 bool requiresArrayCookie(const CXXDeleteExpr *expr, 301 QualType elementType) override; 302 bool requiresArrayCookie(const CXXNewExpr *expr) override; 303 CharUnits getArrayCookieSizeImpl(QualType type) override; 304 llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF, 305 llvm::Value *NewPtr, 306 llvm::Value *NumElements, 307 const CXXNewExpr *expr, 308 QualType ElementType) override; 309 llvm::Value *readArrayCookieImpl(CodeGenFunction &CGF, 310 llvm::Value *allocPtr, 311 CharUnits cookieSize) override; 312 313 friend struct MSRTTIBuilder; 314 315 bool isImageRelative() const { 316 return CGM.getTarget().getPointerWidth(/*AddressSpace=*/0) == 64; 317 } 318 319 // 5 routines for constructing the llvm types for MS RTTI structs. 320 llvm::StructType *getTypeDescriptorType(StringRef TypeInfoString) { 321 llvm::SmallString<32> TDTypeName("rtti.TypeDescriptor"); 322 TDTypeName += llvm::utostr(TypeInfoString.size()); 323 llvm::StructType *&TypeDescriptorType = 324 TypeDescriptorTypeMap[TypeInfoString.size()]; 325 if (TypeDescriptorType) 326 return TypeDescriptorType; 327 llvm::Type *FieldTypes[] = { 328 CGM.Int8PtrPtrTy, 329 CGM.Int8PtrTy, 330 llvm::ArrayType::get(CGM.Int8Ty, TypeInfoString.size() + 1)}; 331 TypeDescriptorType = 332 llvm::StructType::create(CGM.getLLVMContext(), FieldTypes, TDTypeName); 333 return TypeDescriptorType; 334 } 335 336 llvm::Type *getImageRelativeType(llvm::Type *PtrType) { 337 if (!isImageRelative()) 338 return PtrType; 339 return CGM.IntTy; 340 } 341 342 llvm::StructType *getBaseClassDescriptorType() { 343 if (BaseClassDescriptorType) 344 return BaseClassDescriptorType; 345 llvm::Type *FieldTypes[] = { 346 getImageRelativeType(CGM.Int8PtrTy), 347 CGM.IntTy, 348 CGM.IntTy, 349 CGM.IntTy, 350 CGM.IntTy, 351 CGM.IntTy, 352 getImageRelativeType(getClassHierarchyDescriptorType()->getPointerTo()), 353 }; 354 BaseClassDescriptorType = llvm::StructType::create( 355 CGM.getLLVMContext(), FieldTypes, "rtti.BaseClassDescriptor"); 356 return BaseClassDescriptorType; 357 } 358 359 llvm::StructType *getClassHierarchyDescriptorType() { 360 if (ClassHierarchyDescriptorType) 361 return ClassHierarchyDescriptorType; 362 // Forward-declare RTTIClassHierarchyDescriptor to break a cycle. 363 ClassHierarchyDescriptorType = llvm::StructType::create( 364 CGM.getLLVMContext(), "rtti.ClassHierarchyDescriptor"); 365 llvm::Type *FieldTypes[] = { 366 CGM.IntTy, 367 CGM.IntTy, 368 CGM.IntTy, 369 getImageRelativeType( 370 getBaseClassDescriptorType()->getPointerTo()->getPointerTo()), 371 }; 372 ClassHierarchyDescriptorType->setBody(FieldTypes); 373 return ClassHierarchyDescriptorType; 374 } 375 376 llvm::StructType *getCompleteObjectLocatorType() { 377 if (CompleteObjectLocatorType) 378 return CompleteObjectLocatorType; 379 CompleteObjectLocatorType = llvm::StructType::create( 380 CGM.getLLVMContext(), "rtti.CompleteObjectLocator"); 381 llvm::Type *FieldTypes[] = { 382 CGM.IntTy, 383 CGM.IntTy, 384 CGM.IntTy, 385 getImageRelativeType(CGM.Int8PtrTy), 386 getImageRelativeType(getClassHierarchyDescriptorType()->getPointerTo()), 387 getImageRelativeType(CompleteObjectLocatorType), 388 }; 389 llvm::ArrayRef<llvm::Type *> FieldTypesRef(FieldTypes); 390 if (!isImageRelative()) 391 FieldTypesRef = FieldTypesRef.drop_back(); 392 CompleteObjectLocatorType->setBody(FieldTypesRef); 393 return CompleteObjectLocatorType; 394 } 395 396 llvm::GlobalVariable *getImageBase() { 397 StringRef Name = "__ImageBase"; 398 if (llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(Name)) 399 return GV; 400 401 return new llvm::GlobalVariable(CGM.getModule(), CGM.Int8Ty, 402 /*isConstant=*/true, 403 llvm::GlobalValue::ExternalLinkage, 404 /*Initializer=*/nullptr, Name); 405 } 406 407 llvm::Constant *getImageRelativeConstant(llvm::Constant *PtrVal) { 408 if (!isImageRelative()) 409 return PtrVal; 410 411 llvm::Constant *ImageBaseAsInt = 412 llvm::ConstantExpr::getPtrToInt(getImageBase(), CGM.IntPtrTy); 413 llvm::Constant *PtrValAsInt = 414 llvm::ConstantExpr::getPtrToInt(PtrVal, CGM.IntPtrTy); 415 llvm::Constant *Diff = 416 llvm::ConstantExpr::getSub(PtrValAsInt, ImageBaseAsInt, 417 /*HasNUW=*/true, /*HasNSW=*/true); 418 return llvm::ConstantExpr::getTrunc(Diff, CGM.IntTy); 419 } 420 421 private: 422 MicrosoftMangleContext &getMangleContext() { 423 return cast<MicrosoftMangleContext>(CodeGen::CGCXXABI::getMangleContext()); 424 } 425 426 llvm::Constant *getZeroInt() { 427 return llvm::ConstantInt::get(CGM.IntTy, 0); 428 } 429 430 llvm::Constant *getAllOnesInt() { 431 return llvm::Constant::getAllOnesValue(CGM.IntTy); 432 } 433 434 llvm::Constant *getConstantOrZeroInt(llvm::Constant *C) { 435 return C ? C : getZeroInt(); 436 } 437 438 llvm::Value *getValueOrZeroInt(llvm::Value *C) { 439 return C ? C : getZeroInt(); 440 } 441 442 CharUnits getVirtualFunctionPrologueThisAdjustment(GlobalDecl GD); 443 444 void 445 GetNullMemberPointerFields(const MemberPointerType *MPT, 446 llvm::SmallVectorImpl<llvm::Constant *> &fields); 447 448 /// \brief Shared code for virtual base adjustment. Returns the offset from 449 /// the vbptr to the virtual base. Optionally returns the address of the 450 /// vbptr itself. 451 llvm::Value *GetVBaseOffsetFromVBPtr(CodeGenFunction &CGF, 452 llvm::Value *Base, 453 llvm::Value *VBPtrOffset, 454 llvm::Value *VBTableOffset, 455 llvm::Value **VBPtr = nullptr); 456 457 llvm::Value *GetVBaseOffsetFromVBPtr(CodeGenFunction &CGF, 458 llvm::Value *Base, 459 int32_t VBPtrOffset, 460 int32_t VBTableOffset, 461 llvm::Value **VBPtr = nullptr) { 462 assert(VBTableOffset % 4 == 0 && "should be byte offset into table of i32s"); 463 llvm::Value *VBPOffset = llvm::ConstantInt::get(CGM.IntTy, VBPtrOffset), 464 *VBTOffset = llvm::ConstantInt::get(CGM.IntTy, VBTableOffset); 465 return GetVBaseOffsetFromVBPtr(CGF, Base, VBPOffset, VBTOffset, VBPtr); 466 } 467 468 /// \brief Performs a full virtual base adjustment. Used to dereference 469 /// pointers to members of virtual bases. 470 llvm::Value *AdjustVirtualBase(CodeGenFunction &CGF, const Expr *E, 471 const CXXRecordDecl *RD, llvm::Value *Base, 472 llvm::Value *VirtualBaseAdjustmentOffset, 473 llvm::Value *VBPtrOffset /* optional */); 474 475 /// \brief Emits a full member pointer with the fields common to data and 476 /// function member pointers. 477 llvm::Constant *EmitFullMemberPointer(llvm::Constant *FirstField, 478 bool IsMemberFunction, 479 const CXXRecordDecl *RD, 480 CharUnits NonVirtualBaseAdjustment); 481 482 llvm::Constant *BuildMemberPointer(const CXXRecordDecl *RD, 483 const CXXMethodDecl *MD, 484 CharUnits NonVirtualBaseAdjustment); 485 486 bool MemberPointerConstantIsNull(const MemberPointerType *MPT, 487 llvm::Constant *MP); 488 489 /// \brief - Initialize all vbptrs of 'this' with RD as the complete type. 490 void EmitVBPtrStores(CodeGenFunction &CGF, const CXXRecordDecl *RD); 491 492 /// \brief Caching wrapper around VBTableBuilder::enumerateVBTables(). 493 const VBTableGlobals &enumerateVBTables(const CXXRecordDecl *RD); 494 495 /// \brief Generate a thunk for calling a virtual member function MD. 496 llvm::Function *EmitVirtualMemPtrThunk( 497 const CXXMethodDecl *MD, 498 const MicrosoftVTableContext::MethodVFTableLocation &ML); 499 500 public: 501 llvm::Type *ConvertMemberPointerType(const MemberPointerType *MPT) override; 502 503 bool isZeroInitializable(const MemberPointerType *MPT) override; 504 505 bool isMemberPointerConvertible(const MemberPointerType *MPT) const override { 506 const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl(); 507 return RD->hasAttr<MSInheritanceAttr>(); 508 } 509 510 bool isTypeInfoCalculable(QualType Ty) const override { 511 if (!CGCXXABI::isTypeInfoCalculable(Ty)) 512 return false; 513 if (const auto *MPT = Ty->getAs<MemberPointerType>()) { 514 const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl(); 515 if (!RD->hasAttr<MSInheritanceAttr>()) 516 return false; 517 } 518 return true; 519 } 520 521 llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT) override; 522 523 llvm::Constant *EmitMemberDataPointer(const MemberPointerType *MPT, 524 CharUnits offset) override; 525 llvm::Constant *EmitMemberPointer(const CXXMethodDecl *MD) override; 526 llvm::Constant *EmitMemberPointer(const APValue &MP, QualType MPT) override; 527 528 llvm::Value *EmitMemberPointerComparison(CodeGenFunction &CGF, 529 llvm::Value *L, 530 llvm::Value *R, 531 const MemberPointerType *MPT, 532 bool Inequality) override; 533 534 llvm::Value *EmitMemberPointerIsNotNull(CodeGenFunction &CGF, 535 llvm::Value *MemPtr, 536 const MemberPointerType *MPT) override; 537 538 llvm::Value * 539 EmitMemberDataPointerAddress(CodeGenFunction &CGF, const Expr *E, 540 llvm::Value *Base, llvm::Value *MemPtr, 541 const MemberPointerType *MPT) override; 542 543 llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF, 544 const CastExpr *E, 545 llvm::Value *Src) override; 546 547 llvm::Constant *EmitMemberPointerConversion(const CastExpr *E, 548 llvm::Constant *Src) override; 549 550 llvm::Value * 551 EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF, const Expr *E, 552 llvm::Value *&This, llvm::Value *MemPtr, 553 const MemberPointerType *MPT) override; 554 555 void emitCXXStructor(const CXXMethodDecl *MD, StructorType Type) override; 556 557 private: 558 typedef std::pair<const CXXRecordDecl *, CharUnits> VFTableIdTy; 559 typedef llvm::DenseMap<VFTableIdTy, llvm::GlobalVariable *> VTablesMapTy; 560 typedef llvm::DenseMap<VFTableIdTy, llvm::GlobalValue *> VFTablesMapTy; 561 /// \brief All the vftables that have been referenced. 562 VFTablesMapTy VFTablesMap; 563 VTablesMapTy VTablesMap; 564 565 /// \brief This set holds the record decls we've deferred vtable emission for. 566 llvm::SmallPtrSet<const CXXRecordDecl *, 4> DeferredVFTables; 567 568 569 /// \brief All the vbtables which have been referenced. 570 llvm::DenseMap<const CXXRecordDecl *, VBTableGlobals> VBTablesMap; 571 572 /// Info on the global variable used to guard initialization of static locals. 573 /// The BitIndex field is only used for externally invisible declarations. 574 struct GuardInfo { 575 GuardInfo() : Guard(nullptr), BitIndex(0) {} 576 llvm::GlobalVariable *Guard; 577 unsigned BitIndex; 578 }; 579 580 /// Map from DeclContext to the current guard variable. We assume that the 581 /// AST is visited in source code order. 582 llvm::DenseMap<const DeclContext *, GuardInfo> GuardVariableMap; 583 584 llvm::DenseMap<size_t, llvm::StructType *> TypeDescriptorTypeMap; 585 llvm::StructType *BaseClassDescriptorType; 586 llvm::StructType *ClassHierarchyDescriptorType; 587 llvm::StructType *CompleteObjectLocatorType; 588 }; 589 590 } 591 592 CGCXXABI::RecordArgABI 593 MicrosoftCXXABI::getRecordArgABI(const CXXRecordDecl *RD) const { 594 switch (CGM.getTarget().getTriple().getArch()) { 595 default: 596 // FIXME: Implement for other architectures. 597 return RAA_Default; 598 599 case llvm::Triple::x86: 600 // All record arguments are passed in memory on x86. Decide whether to 601 // construct the object directly in argument memory, or to construct the 602 // argument elsewhere and copy the bytes during the call. 603 604 // If C++ prohibits us from making a copy, construct the arguments directly 605 // into argument memory. 606 if (!canCopyArgument(RD)) 607 return RAA_DirectInMemory; 608 609 // Otherwise, construct the argument into a temporary and copy the bytes 610 // into the outgoing argument memory. 611 return RAA_Default; 612 613 case llvm::Triple::x86_64: 614 // Win64 passes objects with non-trivial copy ctors indirectly. 615 if (RD->hasNonTrivialCopyConstructor()) 616 return RAA_Indirect; 617 618 // If an object has a destructor, we'd really like to pass it indirectly 619 // because it allows us to elide copies. Unfortunately, MSVC makes that 620 // impossible for small types, which it will pass in a single register or 621 // stack slot. Most objects with dtors are large-ish, so handle that early. 622 // We can't call out all large objects as being indirect because there are 623 // multiple x64 calling conventions and the C++ ABI code shouldn't dictate 624 // how we pass large POD types. 625 if (RD->hasNonTrivialDestructor() && 626 getContext().getTypeSize(RD->getTypeForDecl()) > 64) 627 return RAA_Indirect; 628 629 // We have a trivial copy constructor or no copy constructors, but we have 630 // to make sure it isn't deleted. 631 bool CopyDeleted = false; 632 for (const CXXConstructorDecl *CD : RD->ctors()) { 633 if (CD->isCopyConstructor()) { 634 assert(CD->isTrivial()); 635 // We had at least one undeleted trivial copy ctor. Return directly. 636 if (!CD->isDeleted()) 637 return RAA_Default; 638 CopyDeleted = true; 639 } 640 } 641 642 // The trivial copy constructor was deleted. Return indirectly. 643 if (CopyDeleted) 644 return RAA_Indirect; 645 646 // There were no copy ctors. Return in RAX. 647 return RAA_Default; 648 } 649 650 llvm_unreachable("invalid enum"); 651 } 652 653 void MicrosoftCXXABI::emitVirtualObjectDelete(CodeGenFunction &CGF, 654 const CXXDeleteExpr *DE, 655 llvm::Value *Ptr, 656 QualType ElementType, 657 const CXXDestructorDecl *Dtor) { 658 // FIXME: Provide a source location here even though there's no 659 // CXXMemberCallExpr for dtor call. 660 bool UseGlobalDelete = DE->isGlobalDelete(); 661 CXXDtorType DtorType = UseGlobalDelete ? Dtor_Complete : Dtor_Deleting; 662 llvm::Value *MDThis = 663 EmitVirtualDestructorCall(CGF, Dtor, DtorType, Ptr, /*CE=*/nullptr); 664 if (UseGlobalDelete) 665 CGF.EmitDeleteCall(DE->getOperatorDelete(), MDThis, ElementType); 666 } 667 668 /// \brief Gets the offset to the virtual base that contains the vfptr for 669 /// MS-ABI polymorphic types. 670 static llvm::Value *getPolymorphicOffset(CodeGenFunction &CGF, 671 const CXXRecordDecl *RD, 672 llvm::Value *Value) { 673 const ASTContext &Context = RD->getASTContext(); 674 for (const CXXBaseSpecifier &Base : RD->vbases()) 675 if (Context.getASTRecordLayout(Base.getType()->getAsCXXRecordDecl()) 676 .hasExtendableVFPtr()) 677 return CGF.CGM.getCXXABI().GetVirtualBaseClassOffset( 678 CGF, Value, RD, Base.getType()->getAsCXXRecordDecl()); 679 llvm_unreachable("One of our vbases should be polymorphic."); 680 } 681 682 static std::pair<llvm::Value *, llvm::Value *> 683 performBaseAdjustment(CodeGenFunction &CGF, llvm::Value *Value, 684 QualType SrcRecordTy) { 685 Value = CGF.Builder.CreateBitCast(Value, CGF.Int8PtrTy); 686 const CXXRecordDecl *SrcDecl = SrcRecordTy->getAsCXXRecordDecl(); 687 688 if (CGF.getContext().getASTRecordLayout(SrcDecl).hasExtendableVFPtr()) 689 return std::make_pair(Value, llvm::ConstantInt::get(CGF.Int32Ty, 0)); 690 691 // Perform a base adjustment. 692 llvm::Value *Offset = getPolymorphicOffset(CGF, SrcDecl, Value); 693 Value = CGF.Builder.CreateInBoundsGEP(Value, Offset); 694 Offset = CGF.Builder.CreateTrunc(Offset, CGF.Int32Ty); 695 return std::make_pair(Value, Offset); 696 } 697 698 bool MicrosoftCXXABI::shouldTypeidBeNullChecked(bool IsDeref, 699 QualType SrcRecordTy) { 700 const CXXRecordDecl *SrcDecl = SrcRecordTy->getAsCXXRecordDecl(); 701 return IsDeref && 702 !CGM.getContext().getASTRecordLayout(SrcDecl).hasExtendableVFPtr(); 703 } 704 705 static llvm::CallSite emitRTtypeidCall(CodeGenFunction &CGF, 706 llvm::Value *Argument) { 707 llvm::Type *ArgTypes[] = {CGF.Int8PtrTy}; 708 llvm::FunctionType *FTy = 709 llvm::FunctionType::get(CGF.Int8PtrTy, ArgTypes, false); 710 llvm::Value *Args[] = {Argument}; 711 llvm::Constant *Fn = CGF.CGM.CreateRuntimeFunction(FTy, "__RTtypeid"); 712 return CGF.EmitRuntimeCallOrInvoke(Fn, Args); 713 } 714 715 void MicrosoftCXXABI::EmitBadTypeidCall(CodeGenFunction &CGF) { 716 llvm::CallSite Call = 717 emitRTtypeidCall(CGF, llvm::Constant::getNullValue(CGM.VoidPtrTy)); 718 Call.setDoesNotReturn(); 719 CGF.Builder.CreateUnreachable(); 720 } 721 722 llvm::Value *MicrosoftCXXABI::EmitTypeid(CodeGenFunction &CGF, 723 QualType SrcRecordTy, 724 llvm::Value *ThisPtr, 725 llvm::Type *StdTypeInfoPtrTy) { 726 llvm::Value *Offset; 727 std::tie(ThisPtr, Offset) = performBaseAdjustment(CGF, ThisPtr, SrcRecordTy); 728 return CGF.Builder.CreateBitCast( 729 emitRTtypeidCall(CGF, ThisPtr).getInstruction(), StdTypeInfoPtrTy); 730 } 731 732 bool MicrosoftCXXABI::shouldDynamicCastCallBeNullChecked(bool SrcIsPtr, 733 QualType SrcRecordTy) { 734 const CXXRecordDecl *SrcDecl = SrcRecordTy->getAsCXXRecordDecl(); 735 return SrcIsPtr && 736 !CGM.getContext().getASTRecordLayout(SrcDecl).hasExtendableVFPtr(); 737 } 738 739 llvm::Value *MicrosoftCXXABI::EmitDynamicCastCall( 740 CodeGenFunction &CGF, llvm::Value *Value, QualType SrcRecordTy, 741 QualType DestTy, QualType DestRecordTy, llvm::BasicBlock *CastEnd) { 742 llvm::Type *DestLTy = CGF.ConvertType(DestTy); 743 744 llvm::Value *SrcRTTI = 745 CGF.CGM.GetAddrOfRTTIDescriptor(SrcRecordTy.getUnqualifiedType()); 746 llvm::Value *DestRTTI = 747 CGF.CGM.GetAddrOfRTTIDescriptor(DestRecordTy.getUnqualifiedType()); 748 749 llvm::Value *Offset; 750 std::tie(Value, Offset) = performBaseAdjustment(CGF, Value, SrcRecordTy); 751 752 // PVOID __RTDynamicCast( 753 // PVOID inptr, 754 // LONG VfDelta, 755 // PVOID SrcType, 756 // PVOID TargetType, 757 // BOOL isReference) 758 llvm::Type *ArgTypes[] = {CGF.Int8PtrTy, CGF.Int32Ty, CGF.Int8PtrTy, 759 CGF.Int8PtrTy, CGF.Int32Ty}; 760 llvm::Constant *Function = CGF.CGM.CreateRuntimeFunction( 761 llvm::FunctionType::get(CGF.Int8PtrTy, ArgTypes, false), 762 "__RTDynamicCast"); 763 llvm::Value *Args[] = { 764 Value, Offset, SrcRTTI, DestRTTI, 765 llvm::ConstantInt::get(CGF.Int32Ty, DestTy->isReferenceType())}; 766 Value = CGF.EmitRuntimeCallOrInvoke(Function, Args).getInstruction(); 767 return CGF.Builder.CreateBitCast(Value, DestLTy); 768 } 769 770 llvm::Value * 771 MicrosoftCXXABI::EmitDynamicCastToVoid(CodeGenFunction &CGF, llvm::Value *Value, 772 QualType SrcRecordTy, 773 QualType DestTy) { 774 llvm::Value *Offset; 775 std::tie(Value, Offset) = performBaseAdjustment(CGF, Value, SrcRecordTy); 776 777 // PVOID __RTCastToVoid( 778 // PVOID inptr) 779 llvm::Type *ArgTypes[] = {CGF.Int8PtrTy}; 780 llvm::Constant *Function = CGF.CGM.CreateRuntimeFunction( 781 llvm::FunctionType::get(CGF.Int8PtrTy, ArgTypes, false), 782 "__RTCastToVoid"); 783 llvm::Value *Args[] = {Value}; 784 return CGF.EmitRuntimeCall(Function, Args); 785 } 786 787 bool MicrosoftCXXABI::EmitBadCastCall(CodeGenFunction &CGF) { 788 return false; 789 } 790 791 llvm::Value *MicrosoftCXXABI::GetVirtualBaseClassOffset( 792 CodeGenFunction &CGF, llvm::Value *This, const CXXRecordDecl *ClassDecl, 793 const CXXRecordDecl *BaseClassDecl) { 794 int64_t VBPtrChars = 795 getContext().getASTRecordLayout(ClassDecl).getVBPtrOffset().getQuantity(); 796 llvm::Value *VBPtrOffset = llvm::ConstantInt::get(CGM.PtrDiffTy, VBPtrChars); 797 CharUnits IntSize = getContext().getTypeSizeInChars(getContext().IntTy); 798 CharUnits VBTableChars = 799 IntSize * 800 CGM.getMicrosoftVTableContext().getVBTableIndex(ClassDecl, BaseClassDecl); 801 llvm::Value *VBTableOffset = 802 llvm::ConstantInt::get(CGM.IntTy, VBTableChars.getQuantity()); 803 804 llvm::Value *VBPtrToNewBase = 805 GetVBaseOffsetFromVBPtr(CGF, This, VBPtrOffset, VBTableOffset); 806 VBPtrToNewBase = 807 CGF.Builder.CreateSExtOrBitCast(VBPtrToNewBase, CGM.PtrDiffTy); 808 return CGF.Builder.CreateNSWAdd(VBPtrOffset, VBPtrToNewBase); 809 } 810 811 bool MicrosoftCXXABI::HasThisReturn(GlobalDecl GD) const { 812 return isa<CXXConstructorDecl>(GD.getDecl()); 813 } 814 815 static bool isDeletingDtor(GlobalDecl GD) { 816 return isa<CXXDestructorDecl>(GD.getDecl()) && 817 GD.getDtorType() == Dtor_Deleting; 818 } 819 820 bool MicrosoftCXXABI::hasMostDerivedReturn(GlobalDecl GD) const { 821 return isDeletingDtor(GD); 822 } 823 824 bool MicrosoftCXXABI::classifyReturnType(CGFunctionInfo &FI) const { 825 const CXXRecordDecl *RD = FI.getReturnType()->getAsCXXRecordDecl(); 826 if (!RD) 827 return false; 828 829 if (FI.isInstanceMethod()) { 830 // If it's an instance method, aggregates are always returned indirectly via 831 // the second parameter. 832 FI.getReturnInfo() = ABIArgInfo::getIndirect(0, /*ByVal=*/false); 833 FI.getReturnInfo().setSRetAfterThis(FI.isInstanceMethod()); 834 return true; 835 } else if (!RD->isPOD()) { 836 // If it's a free function, non-POD types are returned indirectly. 837 FI.getReturnInfo() = ABIArgInfo::getIndirect(0, /*ByVal=*/false); 838 return true; 839 } 840 841 // Otherwise, use the C ABI rules. 842 return false; 843 } 844 845 llvm::BasicBlock * 846 MicrosoftCXXABI::EmitCtorCompleteObjectHandler(CodeGenFunction &CGF, 847 const CXXRecordDecl *RD) { 848 llvm::Value *IsMostDerivedClass = getStructorImplicitParamValue(CGF); 849 assert(IsMostDerivedClass && 850 "ctor for a class with virtual bases must have an implicit parameter"); 851 llvm::Value *IsCompleteObject = 852 CGF.Builder.CreateIsNotNull(IsMostDerivedClass, "is_complete_object"); 853 854 llvm::BasicBlock *CallVbaseCtorsBB = CGF.createBasicBlock("ctor.init_vbases"); 855 llvm::BasicBlock *SkipVbaseCtorsBB = CGF.createBasicBlock("ctor.skip_vbases"); 856 CGF.Builder.CreateCondBr(IsCompleteObject, 857 CallVbaseCtorsBB, SkipVbaseCtorsBB); 858 859 CGF.EmitBlock(CallVbaseCtorsBB); 860 861 // Fill in the vbtable pointers here. 862 EmitVBPtrStores(CGF, RD); 863 864 // CGF will put the base ctor calls in this basic block for us later. 865 866 return SkipVbaseCtorsBB; 867 } 868 869 void MicrosoftCXXABI::initializeHiddenVirtualInheritanceMembers( 870 CodeGenFunction &CGF, const CXXRecordDecl *RD) { 871 // In most cases, an override for a vbase virtual method can adjust 872 // the "this" parameter by applying a constant offset. 873 // However, this is not enough while a constructor or a destructor of some 874 // class X is being executed if all the following conditions are met: 875 // - X has virtual bases, (1) 876 // - X overrides a virtual method M of a vbase Y, (2) 877 // - X itself is a vbase of the most derived class. 878 // 879 // If (1) and (2) are true, the vtorDisp for vbase Y is a hidden member of X 880 // which holds the extra amount of "this" adjustment we must do when we use 881 // the X vftables (i.e. during X ctor or dtor). 882 // Outside the ctors and dtors, the values of vtorDisps are zero. 883 884 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); 885 typedef ASTRecordLayout::VBaseOffsetsMapTy VBOffsets; 886 const VBOffsets &VBaseMap = Layout.getVBaseOffsetsMap(); 887 CGBuilderTy &Builder = CGF.Builder; 888 889 unsigned AS = 890 cast<llvm::PointerType>(getThisValue(CGF)->getType())->getAddressSpace(); 891 llvm::Value *Int8This = nullptr; // Initialize lazily. 892 893 for (VBOffsets::const_iterator I = VBaseMap.begin(), E = VBaseMap.end(); 894 I != E; ++I) { 895 if (!I->second.hasVtorDisp()) 896 continue; 897 898 llvm::Value *VBaseOffset = 899 GetVirtualBaseClassOffset(CGF, getThisValue(CGF), RD, I->first); 900 // FIXME: it doesn't look right that we SExt in GetVirtualBaseClassOffset() 901 // just to Trunc back immediately. 902 VBaseOffset = Builder.CreateTruncOrBitCast(VBaseOffset, CGF.Int32Ty); 903 uint64_t ConstantVBaseOffset = 904 Layout.getVBaseClassOffset(I->first).getQuantity(); 905 906 // vtorDisp_for_vbase = vbptr[vbase_idx] - offsetof(RD, vbase). 907 llvm::Value *VtorDispValue = Builder.CreateSub( 908 VBaseOffset, llvm::ConstantInt::get(CGM.Int32Ty, ConstantVBaseOffset), 909 "vtordisp.value"); 910 911 if (!Int8This) 912 Int8This = Builder.CreateBitCast(getThisValue(CGF), 913 CGF.Int8Ty->getPointerTo(AS)); 914 llvm::Value *VtorDispPtr = Builder.CreateInBoundsGEP(Int8This, VBaseOffset); 915 // vtorDisp is always the 32-bits before the vbase in the class layout. 916 VtorDispPtr = Builder.CreateConstGEP1_32(VtorDispPtr, -4); 917 VtorDispPtr = Builder.CreateBitCast( 918 VtorDispPtr, CGF.Int32Ty->getPointerTo(AS), "vtordisp.ptr"); 919 920 Builder.CreateStore(VtorDispValue, VtorDispPtr); 921 } 922 } 923 924 void MicrosoftCXXABI::EmitCXXConstructors(const CXXConstructorDecl *D) { 925 // There's only one constructor type in this ABI. 926 CGM.EmitGlobal(GlobalDecl(D, Ctor_Complete)); 927 } 928 929 void MicrosoftCXXABI::EmitVBPtrStores(CodeGenFunction &CGF, 930 const CXXRecordDecl *RD) { 931 llvm::Value *ThisInt8Ptr = 932 CGF.Builder.CreateBitCast(getThisValue(CGF), CGM.Int8PtrTy, "this.int8"); 933 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); 934 935 const VBTableGlobals &VBGlobals = enumerateVBTables(RD); 936 for (unsigned I = 0, E = VBGlobals.VBTables->size(); I != E; ++I) { 937 const VPtrInfo *VBT = (*VBGlobals.VBTables)[I]; 938 llvm::GlobalVariable *GV = VBGlobals.Globals[I]; 939 const ASTRecordLayout &SubobjectLayout = 940 CGM.getContext().getASTRecordLayout(VBT->BaseWithVPtr); 941 CharUnits Offs = VBT->NonVirtualOffset; 942 Offs += SubobjectLayout.getVBPtrOffset(); 943 if (VBT->getVBaseWithVPtr()) 944 Offs += Layout.getVBaseClassOffset(VBT->getVBaseWithVPtr()); 945 llvm::Value *VBPtr = 946 CGF.Builder.CreateConstInBoundsGEP1_64(ThisInt8Ptr, Offs.getQuantity()); 947 llvm::Value *GVPtr = CGF.Builder.CreateConstInBoundsGEP2_32(GV, 0, 0); 948 VBPtr = CGF.Builder.CreateBitCast(VBPtr, GVPtr->getType()->getPointerTo(0), 949 "vbptr." + VBT->ReusingBase->getName()); 950 CGF.Builder.CreateStore(GVPtr, VBPtr); 951 } 952 } 953 954 void 955 MicrosoftCXXABI::buildStructorSignature(const CXXMethodDecl *MD, StructorType T, 956 SmallVectorImpl<CanQualType> &ArgTys) { 957 // TODO: 'for base' flag 958 if (T == StructorType::Deleting) { 959 // The scalar deleting destructor takes an implicit int parameter. 960 ArgTys.push_back(CGM.getContext().IntTy); 961 } 962 auto *CD = dyn_cast<CXXConstructorDecl>(MD); 963 if (!CD) 964 return; 965 966 // All parameters are already in place except is_most_derived, which goes 967 // after 'this' if it's variadic and last if it's not. 968 969 const CXXRecordDecl *Class = CD->getParent(); 970 const FunctionProtoType *FPT = CD->getType()->castAs<FunctionProtoType>(); 971 if (Class->getNumVBases()) { 972 if (FPT->isVariadic()) 973 ArgTys.insert(ArgTys.begin() + 1, CGM.getContext().IntTy); 974 else 975 ArgTys.push_back(CGM.getContext().IntTy); 976 } 977 } 978 979 void MicrosoftCXXABI::EmitCXXDestructors(const CXXDestructorDecl *D) { 980 // The TU defining a dtor is only guaranteed to emit a base destructor. All 981 // other destructor variants are delegating thunks. 982 CGM.EmitGlobal(GlobalDecl(D, Dtor_Base)); 983 } 984 985 CharUnits 986 MicrosoftCXXABI::getVirtualFunctionPrologueThisAdjustment(GlobalDecl GD) { 987 GD = GD.getCanonicalDecl(); 988 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); 989 990 GlobalDecl LookupGD = GD; 991 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) { 992 // Complete destructors take a pointer to the complete object as a 993 // parameter, thus don't need this adjustment. 994 if (GD.getDtorType() == Dtor_Complete) 995 return CharUnits(); 996 997 // There's no Dtor_Base in vftable but it shares the this adjustment with 998 // the deleting one, so look it up instead. 999 LookupGD = GlobalDecl(DD, Dtor_Deleting); 1000 } 1001 1002 MicrosoftVTableContext::MethodVFTableLocation ML = 1003 CGM.getMicrosoftVTableContext().getMethodVFTableLocation(LookupGD); 1004 CharUnits Adjustment = ML.VFPtrOffset; 1005 1006 // Normal virtual instance methods need to adjust from the vfptr that first 1007 // defined the virtual method to the virtual base subobject, but destructors 1008 // do not. The vector deleting destructor thunk applies this adjustment for 1009 // us if necessary. 1010 if (isa<CXXDestructorDecl>(MD)) 1011 Adjustment = CharUnits::Zero(); 1012 1013 if (ML.VBase) { 1014 const ASTRecordLayout &DerivedLayout = 1015 CGM.getContext().getASTRecordLayout(MD->getParent()); 1016 Adjustment += DerivedLayout.getVBaseClassOffset(ML.VBase); 1017 } 1018 1019 return Adjustment; 1020 } 1021 1022 llvm::Value *MicrosoftCXXABI::adjustThisArgumentForVirtualFunctionCall( 1023 CodeGenFunction &CGF, GlobalDecl GD, llvm::Value *This, bool VirtualCall) { 1024 if (!VirtualCall) { 1025 // If the call of a virtual function is not virtual, we just have to 1026 // compensate for the adjustment the virtual function does in its prologue. 1027 CharUnits Adjustment = getVirtualFunctionPrologueThisAdjustment(GD); 1028 if (Adjustment.isZero()) 1029 return This; 1030 1031 unsigned AS = cast<llvm::PointerType>(This->getType())->getAddressSpace(); 1032 llvm::Type *charPtrTy = CGF.Int8Ty->getPointerTo(AS); 1033 This = CGF.Builder.CreateBitCast(This, charPtrTy); 1034 assert(Adjustment.isPositive()); 1035 return CGF.Builder.CreateConstGEP1_32(This, Adjustment.getQuantity()); 1036 } 1037 1038 GD = GD.getCanonicalDecl(); 1039 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); 1040 1041 GlobalDecl LookupGD = GD; 1042 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) { 1043 // Complete dtors take a pointer to the complete object, 1044 // thus don't need adjustment. 1045 if (GD.getDtorType() == Dtor_Complete) 1046 return This; 1047 1048 // There's only Dtor_Deleting in vftable but it shares the this adjustment 1049 // with the base one, so look up the deleting one instead. 1050 LookupGD = GlobalDecl(DD, Dtor_Deleting); 1051 } 1052 MicrosoftVTableContext::MethodVFTableLocation ML = 1053 CGM.getMicrosoftVTableContext().getMethodVFTableLocation(LookupGD); 1054 1055 unsigned AS = cast<llvm::PointerType>(This->getType())->getAddressSpace(); 1056 llvm::Type *charPtrTy = CGF.Int8Ty->getPointerTo(AS); 1057 CharUnits StaticOffset = ML.VFPtrOffset; 1058 1059 // Base destructors expect 'this' to point to the beginning of the base 1060 // subobject, not the first vfptr that happens to contain the virtual dtor. 1061 // However, we still need to apply the virtual base adjustment. 1062 if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() == Dtor_Base) 1063 StaticOffset = CharUnits::Zero(); 1064 1065 if (ML.VBase) { 1066 This = CGF.Builder.CreateBitCast(This, charPtrTy); 1067 llvm::Value *VBaseOffset = 1068 GetVirtualBaseClassOffset(CGF, This, MD->getParent(), ML.VBase); 1069 This = CGF.Builder.CreateInBoundsGEP(This, VBaseOffset); 1070 } 1071 if (!StaticOffset.isZero()) { 1072 assert(StaticOffset.isPositive()); 1073 This = CGF.Builder.CreateBitCast(This, charPtrTy); 1074 if (ML.VBase) { 1075 // Non-virtual adjustment might result in a pointer outside the allocated 1076 // object, e.g. if the final overrider class is laid out after the virtual 1077 // base that declares a method in the most derived class. 1078 // FIXME: Update the code that emits this adjustment in thunks prologues. 1079 This = CGF.Builder.CreateConstGEP1_32(This, StaticOffset.getQuantity()); 1080 } else { 1081 This = CGF.Builder.CreateConstInBoundsGEP1_32(This, 1082 StaticOffset.getQuantity()); 1083 } 1084 } 1085 return This; 1086 } 1087 1088 void MicrosoftCXXABI::addImplicitStructorParams(CodeGenFunction &CGF, 1089 QualType &ResTy, 1090 FunctionArgList &Params) { 1091 ASTContext &Context = getContext(); 1092 const CXXMethodDecl *MD = cast<CXXMethodDecl>(CGF.CurGD.getDecl()); 1093 assert(isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD)); 1094 if (isa<CXXConstructorDecl>(MD) && MD->getParent()->getNumVBases()) { 1095 ImplicitParamDecl *IsMostDerived 1096 = ImplicitParamDecl::Create(Context, nullptr, 1097 CGF.CurGD.getDecl()->getLocation(), 1098 &Context.Idents.get("is_most_derived"), 1099 Context.IntTy); 1100 // The 'most_derived' parameter goes second if the ctor is variadic and last 1101 // if it's not. Dtors can't be variadic. 1102 const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>(); 1103 if (FPT->isVariadic()) 1104 Params.insert(Params.begin() + 1, IsMostDerived); 1105 else 1106 Params.push_back(IsMostDerived); 1107 getStructorImplicitParamDecl(CGF) = IsMostDerived; 1108 } else if (isDeletingDtor(CGF.CurGD)) { 1109 ImplicitParamDecl *ShouldDelete 1110 = ImplicitParamDecl::Create(Context, nullptr, 1111 CGF.CurGD.getDecl()->getLocation(), 1112 &Context.Idents.get("should_call_delete"), 1113 Context.IntTy); 1114 Params.push_back(ShouldDelete); 1115 getStructorImplicitParamDecl(CGF) = ShouldDelete; 1116 } 1117 } 1118 1119 llvm::Value *MicrosoftCXXABI::adjustThisParameterInVirtualFunctionPrologue( 1120 CodeGenFunction &CGF, GlobalDecl GD, llvm::Value *This) { 1121 // In this ABI, every virtual function takes a pointer to one of the 1122 // subobjects that first defines it as the 'this' parameter, rather than a 1123 // pointer to the final overrider subobject. Thus, we need to adjust it back 1124 // to the final overrider subobject before use. 1125 // See comments in the MicrosoftVFTableContext implementation for the details. 1126 CharUnits Adjustment = getVirtualFunctionPrologueThisAdjustment(GD); 1127 if (Adjustment.isZero()) 1128 return This; 1129 1130 unsigned AS = cast<llvm::PointerType>(This->getType())->getAddressSpace(); 1131 llvm::Type *charPtrTy = CGF.Int8Ty->getPointerTo(AS), 1132 *thisTy = This->getType(); 1133 1134 This = CGF.Builder.CreateBitCast(This, charPtrTy); 1135 assert(Adjustment.isPositive()); 1136 This = 1137 CGF.Builder.CreateConstInBoundsGEP1_32(This, -Adjustment.getQuantity()); 1138 return CGF.Builder.CreateBitCast(This, thisTy); 1139 } 1140 1141 void MicrosoftCXXABI::EmitInstanceFunctionProlog(CodeGenFunction &CGF) { 1142 EmitThisParam(CGF); 1143 1144 /// If this is a function that the ABI specifies returns 'this', initialize 1145 /// the return slot to 'this' at the start of the function. 1146 /// 1147 /// Unlike the setting of return types, this is done within the ABI 1148 /// implementation instead of by clients of CGCXXABI because: 1149 /// 1) getThisValue is currently protected 1150 /// 2) in theory, an ABI could implement 'this' returns some other way; 1151 /// HasThisReturn only specifies a contract, not the implementation 1152 if (HasThisReturn(CGF.CurGD)) 1153 CGF.Builder.CreateStore(getThisValue(CGF), CGF.ReturnValue); 1154 else if (hasMostDerivedReturn(CGF.CurGD)) 1155 CGF.Builder.CreateStore(CGF.EmitCastToVoidPtr(getThisValue(CGF)), 1156 CGF.ReturnValue); 1157 1158 const CXXMethodDecl *MD = cast<CXXMethodDecl>(CGF.CurGD.getDecl()); 1159 if (isa<CXXConstructorDecl>(MD) && MD->getParent()->getNumVBases()) { 1160 assert(getStructorImplicitParamDecl(CGF) && 1161 "no implicit parameter for a constructor with virtual bases?"); 1162 getStructorImplicitParamValue(CGF) 1163 = CGF.Builder.CreateLoad( 1164 CGF.GetAddrOfLocalVar(getStructorImplicitParamDecl(CGF)), 1165 "is_most_derived"); 1166 } 1167 1168 if (isDeletingDtor(CGF.CurGD)) { 1169 assert(getStructorImplicitParamDecl(CGF) && 1170 "no implicit parameter for a deleting destructor?"); 1171 getStructorImplicitParamValue(CGF) 1172 = CGF.Builder.CreateLoad( 1173 CGF.GetAddrOfLocalVar(getStructorImplicitParamDecl(CGF)), 1174 "should_call_delete"); 1175 } 1176 } 1177 1178 unsigned MicrosoftCXXABI::addImplicitConstructorArgs( 1179 CodeGenFunction &CGF, const CXXConstructorDecl *D, CXXCtorType Type, 1180 bool ForVirtualBase, bool Delegating, CallArgList &Args) { 1181 assert(Type == Ctor_Complete || Type == Ctor_Base); 1182 1183 // Check if we need a 'most_derived' parameter. 1184 if (!D->getParent()->getNumVBases()) 1185 return 0; 1186 1187 // Add the 'most_derived' argument second if we are variadic or last if not. 1188 const FunctionProtoType *FPT = D->getType()->castAs<FunctionProtoType>(); 1189 llvm::Value *MostDerivedArg = 1190 llvm::ConstantInt::get(CGM.Int32Ty, Type == Ctor_Complete); 1191 RValue RV = RValue::get(MostDerivedArg); 1192 if (MostDerivedArg) { 1193 if (FPT->isVariadic()) 1194 Args.insert(Args.begin() + 1, 1195 CallArg(RV, getContext().IntTy, /*needscopy=*/false)); 1196 else 1197 Args.add(RV, getContext().IntTy); 1198 } 1199 1200 return 1; // Added one arg. 1201 } 1202 1203 void MicrosoftCXXABI::EmitDestructorCall(CodeGenFunction &CGF, 1204 const CXXDestructorDecl *DD, 1205 CXXDtorType Type, bool ForVirtualBase, 1206 bool Delegating, llvm::Value *This) { 1207 llvm::Value *Callee = CGM.getAddrOfCXXStructor(DD, getFromDtorType(Type)); 1208 1209 if (DD->isVirtual()) { 1210 assert(Type != CXXDtorType::Dtor_Deleting && 1211 "The deleting destructor should only be called via a virtual call"); 1212 This = adjustThisArgumentForVirtualFunctionCall(CGF, GlobalDecl(DD, Type), 1213 This, false); 1214 } 1215 1216 CGF.EmitCXXStructorCall(DD, Callee, ReturnValueSlot(), This, 1217 /*ImplicitParam=*/nullptr, 1218 /*ImplicitParamTy=*/QualType(), nullptr, 1219 getFromDtorType(Type)); 1220 } 1221 1222 void MicrosoftCXXABI::emitVTableDefinitions(CodeGenVTables &CGVT, 1223 const CXXRecordDecl *RD) { 1224 MicrosoftVTableContext &VFTContext = CGM.getMicrosoftVTableContext(); 1225 const VPtrInfoVector &VFPtrs = VFTContext.getVFPtrOffsets(RD); 1226 1227 for (VPtrInfo *Info : VFPtrs) { 1228 llvm::GlobalVariable *VTable = getAddrOfVTable(RD, Info->FullOffsetInMDC); 1229 if (VTable->hasInitializer()) 1230 continue; 1231 1232 llvm::Constant *RTTI = getContext().getLangOpts().RTTIData 1233 ? getMSCompleteObjectLocator(RD, Info) 1234 : nullptr; 1235 1236 const VTableLayout &VTLayout = 1237 VFTContext.getVFTableLayout(RD, Info->FullOffsetInMDC); 1238 llvm::Constant *Init = CGVT.CreateVTableInitializer( 1239 RD, VTLayout.vtable_component_begin(), 1240 VTLayout.getNumVTableComponents(), VTLayout.vtable_thunk_begin(), 1241 VTLayout.getNumVTableThunks(), RTTI); 1242 1243 VTable->setInitializer(Init); 1244 } 1245 } 1246 1247 llvm::Value *MicrosoftCXXABI::getVTableAddressPointInStructor( 1248 CodeGenFunction &CGF, const CXXRecordDecl *VTableClass, BaseSubobject Base, 1249 const CXXRecordDecl *NearestVBase, bool &NeedsVirtualOffset) { 1250 NeedsVirtualOffset = (NearestVBase != nullptr); 1251 1252 (void)getAddrOfVTable(VTableClass, Base.getBaseOffset()); 1253 VFTableIdTy ID(VTableClass, Base.getBaseOffset()); 1254 llvm::GlobalValue *VTableAddressPoint = VFTablesMap[ID]; 1255 if (!VTableAddressPoint) { 1256 assert(Base.getBase()->getNumVBases() && 1257 !CGM.getContext().getASTRecordLayout(Base.getBase()).hasOwnVFPtr()); 1258 } 1259 return VTableAddressPoint; 1260 } 1261 1262 static void mangleVFTableName(MicrosoftMangleContext &MangleContext, 1263 const CXXRecordDecl *RD, const VPtrInfo *VFPtr, 1264 SmallString<256> &Name) { 1265 llvm::raw_svector_ostream Out(Name); 1266 MangleContext.mangleCXXVFTable(RD, VFPtr->MangledPath, Out); 1267 } 1268 1269 llvm::Constant *MicrosoftCXXABI::getVTableAddressPointForConstExpr( 1270 BaseSubobject Base, const CXXRecordDecl *VTableClass) { 1271 (void)getAddrOfVTable(VTableClass, Base.getBaseOffset()); 1272 VFTableIdTy ID(VTableClass, Base.getBaseOffset()); 1273 llvm::GlobalValue *VFTable = VFTablesMap[ID]; 1274 assert(VFTable && "Couldn't find a vftable for the given base?"); 1275 return VFTable; 1276 } 1277 1278 llvm::GlobalVariable *MicrosoftCXXABI::getAddrOfVTable(const CXXRecordDecl *RD, 1279 CharUnits VPtrOffset) { 1280 // getAddrOfVTable may return 0 if asked to get an address of a vtable which 1281 // shouldn't be used in the given record type. We want to cache this result in 1282 // VFTablesMap, thus a simple zero check is not sufficient. 1283 VFTableIdTy ID(RD, VPtrOffset); 1284 VTablesMapTy::iterator I; 1285 bool Inserted; 1286 std::tie(I, Inserted) = VTablesMap.insert(std::make_pair(ID, nullptr)); 1287 if (!Inserted) 1288 return I->second; 1289 1290 llvm::GlobalVariable *&VTable = I->second; 1291 1292 MicrosoftVTableContext &VTContext = CGM.getMicrosoftVTableContext(); 1293 const VPtrInfoVector &VFPtrs = VTContext.getVFPtrOffsets(RD); 1294 1295 if (DeferredVFTables.insert(RD).second) { 1296 // We haven't processed this record type before. 1297 // Queue up this v-table for possible deferred emission. 1298 CGM.addDeferredVTable(RD); 1299 1300 #ifndef NDEBUG 1301 // Create all the vftables at once in order to make sure each vftable has 1302 // a unique mangled name. 1303 llvm::StringSet<> ObservedMangledNames; 1304 for (size_t J = 0, F = VFPtrs.size(); J != F; ++J) { 1305 SmallString<256> Name; 1306 mangleVFTableName(getMangleContext(), RD, VFPtrs[J], Name); 1307 if (!ObservedMangledNames.insert(Name.str()).second) 1308 llvm_unreachable("Already saw this mangling before?"); 1309 } 1310 #endif 1311 } 1312 1313 for (size_t J = 0, F = VFPtrs.size(); J != F; ++J) { 1314 if (VFPtrs[J]->FullOffsetInMDC != VPtrOffset) 1315 continue; 1316 SmallString<256> VFTableName; 1317 mangleVFTableName(getMangleContext(), RD, VFPtrs[J], VFTableName); 1318 StringRef VTableName = VFTableName; 1319 1320 uint64_t NumVTableSlots = 1321 VTContext.getVFTableLayout(RD, VFPtrs[J]->FullOffsetInMDC) 1322 .getNumVTableComponents(); 1323 llvm::GlobalValue::LinkageTypes VTableLinkage = 1324 llvm::GlobalValue::ExternalLinkage; 1325 llvm::ArrayType *VTableType = 1326 llvm::ArrayType::get(CGM.Int8PtrTy, NumVTableSlots); 1327 if (getContext().getLangOpts().RTTIData) { 1328 VTableLinkage = llvm::GlobalValue::PrivateLinkage; 1329 VTableName = ""; 1330 } 1331 1332 VTable = CGM.getModule().getNamedGlobal(VFTableName); 1333 if (!VTable) { 1334 // Create a backing variable for the contents of VTable. The VTable may 1335 // or may not include space for a pointer to RTTI data. 1336 llvm::GlobalValue *VFTable = VTable = new llvm::GlobalVariable( 1337 CGM.getModule(), VTableType, /*isConstant=*/true, VTableLinkage, 1338 /*Initializer=*/nullptr, VTableName); 1339 VTable->setUnnamedAddr(true); 1340 1341 // Only insert a pointer into the VFTable for RTTI data if we are not 1342 // importing it. We never reference the RTTI data directly so there is no 1343 // need to make room for it. 1344 if (getContext().getLangOpts().RTTIData && 1345 !RD->hasAttr<DLLImportAttr>()) { 1346 llvm::Value *GEPIndices[] = {llvm::ConstantInt::get(CGM.IntTy, 0), 1347 llvm::ConstantInt::get(CGM.IntTy, 1)}; 1348 // Create a GEP which points just after the first entry in the VFTable, 1349 // this should be the location of the first virtual method. 1350 llvm::Constant *VTableGEP = 1351 llvm::ConstantExpr::getInBoundsGetElementPtr(VTable, GEPIndices); 1352 // The symbol for the VFTable is an alias to the GEP. It is 1353 // transparent, to other modules, what the nature of this symbol is; all 1354 // that matters is that the alias be the address of the first virtual 1355 // method. 1356 VFTable = llvm::GlobalAlias::create( 1357 cast<llvm::SequentialType>(VTableGEP->getType())->getElementType(), 1358 /*AddressSpace=*/0, llvm::GlobalValue::ExternalLinkage, 1359 VFTableName.str(), VTableGEP, &CGM.getModule()); 1360 } else { 1361 // We don't need a GlobalAlias to be a symbol for the VTable if we won't 1362 // be referencing any RTTI data. The GlobalVariable will end up being 1363 // an appropriate definition of the VFTable. 1364 VTable->setName(VFTableName.str()); 1365 } 1366 1367 VFTable->setUnnamedAddr(true); 1368 if (RD->hasAttr<DLLImportAttr>()) 1369 VFTable->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass); 1370 else if (RD->hasAttr<DLLExportAttr>()) 1371 VFTable->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass); 1372 1373 llvm::GlobalValue::LinkageTypes VFTableLinkage = CGM.getVTableLinkage(RD); 1374 if (VFTable != VTable) { 1375 if (llvm::GlobalValue::isAvailableExternallyLinkage(VFTableLinkage)) { 1376 // AvailableExternally implies that we grabbed the data from another 1377 // executable. No need to stick the alias in a Comdat. 1378 } else if (llvm::GlobalValue::isInternalLinkage(VFTableLinkage) || 1379 llvm::GlobalValue::isWeakODRLinkage(VFTableLinkage) || 1380 llvm::GlobalValue::isLinkOnceODRLinkage(VFTableLinkage)) { 1381 // The alias is going to be dropped into a Comdat, no need to make it 1382 // weak. 1383 if (!llvm::GlobalValue::isInternalLinkage(VFTableLinkage)) 1384 VFTableLinkage = llvm::GlobalValue::ExternalLinkage; 1385 llvm::Comdat *C = 1386 CGM.getModule().getOrInsertComdat(VFTable->getName()); 1387 // We must indicate which VFTable is larger to support linking between 1388 // translation units which do and do not have RTTI data. The largest 1389 // VFTable contains the RTTI data; translation units which reference 1390 // the smaller VFTable always reference it relative to the first 1391 // virtual method. 1392 C->setSelectionKind(llvm::Comdat::Largest); 1393 VTable->setComdat(C); 1394 } else { 1395 llvm_unreachable("unexpected linkage for vftable!"); 1396 } 1397 } 1398 VFTable->setLinkage(VFTableLinkage); 1399 CGM.setGlobalVisibility(VFTable, RD); 1400 VFTablesMap[ID] = VFTable; 1401 } 1402 break; 1403 } 1404 1405 return VTable; 1406 } 1407 1408 llvm::Value *MicrosoftCXXABI::getVirtualFunctionPointer(CodeGenFunction &CGF, 1409 GlobalDecl GD, 1410 llvm::Value *This, 1411 llvm::Type *Ty) { 1412 GD = GD.getCanonicalDecl(); 1413 CGBuilderTy &Builder = CGF.Builder; 1414 1415 Ty = Ty->getPointerTo()->getPointerTo(); 1416 llvm::Value *VPtr = 1417 adjustThisArgumentForVirtualFunctionCall(CGF, GD, This, true); 1418 llvm::Value *VTable = CGF.GetVTablePtr(VPtr, Ty); 1419 1420 MicrosoftVTableContext::MethodVFTableLocation ML = 1421 CGM.getMicrosoftVTableContext().getMethodVFTableLocation(GD); 1422 llvm::Value *VFuncPtr = 1423 Builder.CreateConstInBoundsGEP1_64(VTable, ML.Index, "vfn"); 1424 return Builder.CreateLoad(VFuncPtr); 1425 } 1426 1427 llvm::Value *MicrosoftCXXABI::EmitVirtualDestructorCall( 1428 CodeGenFunction &CGF, const CXXDestructorDecl *Dtor, CXXDtorType DtorType, 1429 llvm::Value *This, const CXXMemberCallExpr *CE) { 1430 assert(CE == nullptr || CE->arg_begin() == CE->arg_end()); 1431 assert(DtorType == Dtor_Deleting || DtorType == Dtor_Complete); 1432 1433 // We have only one destructor in the vftable but can get both behaviors 1434 // by passing an implicit int parameter. 1435 GlobalDecl GD(Dtor, Dtor_Deleting); 1436 const CGFunctionInfo *FInfo = &CGM.getTypes().arrangeCXXStructorDeclaration( 1437 Dtor, StructorType::Deleting); 1438 llvm::Type *Ty = CGF.CGM.getTypes().GetFunctionType(*FInfo); 1439 llvm::Value *Callee = getVirtualFunctionPointer(CGF, GD, This, Ty); 1440 1441 ASTContext &Context = CGF.getContext(); 1442 llvm::Value *ImplicitParam = 1443 llvm::ConstantInt::get(llvm::IntegerType::getInt32Ty(CGF.getLLVMContext()), 1444 DtorType == Dtor_Deleting); 1445 1446 This = adjustThisArgumentForVirtualFunctionCall(CGF, GD, This, true); 1447 RValue RV = CGF.EmitCXXStructorCall(Dtor, Callee, ReturnValueSlot(), This, 1448 ImplicitParam, Context.IntTy, CE, 1449 StructorType::Deleting); 1450 return RV.getScalarVal(); 1451 } 1452 1453 const VBTableGlobals & 1454 MicrosoftCXXABI::enumerateVBTables(const CXXRecordDecl *RD) { 1455 // At this layer, we can key the cache off of a single class, which is much 1456 // easier than caching each vbtable individually. 1457 llvm::DenseMap<const CXXRecordDecl*, VBTableGlobals>::iterator Entry; 1458 bool Added; 1459 std::tie(Entry, Added) = 1460 VBTablesMap.insert(std::make_pair(RD, VBTableGlobals())); 1461 VBTableGlobals &VBGlobals = Entry->second; 1462 if (!Added) 1463 return VBGlobals; 1464 1465 MicrosoftVTableContext &Context = CGM.getMicrosoftVTableContext(); 1466 VBGlobals.VBTables = &Context.enumerateVBTables(RD); 1467 1468 // Cache the globals for all vbtables so we don't have to recompute the 1469 // mangled names. 1470 llvm::GlobalVariable::LinkageTypes Linkage = CGM.getVTableLinkage(RD); 1471 for (VPtrInfoVector::const_iterator I = VBGlobals.VBTables->begin(), 1472 E = VBGlobals.VBTables->end(); 1473 I != E; ++I) { 1474 VBGlobals.Globals.push_back(getAddrOfVBTable(**I, RD, Linkage)); 1475 } 1476 1477 return VBGlobals; 1478 } 1479 1480 llvm::Function *MicrosoftCXXABI::EmitVirtualMemPtrThunk( 1481 const CXXMethodDecl *MD, 1482 const MicrosoftVTableContext::MethodVFTableLocation &ML) { 1483 assert(!isa<CXXConstructorDecl>(MD) && !isa<CXXDestructorDecl>(MD) && 1484 "can't form pointers to ctors or virtual dtors"); 1485 1486 // Calculate the mangled name. 1487 SmallString<256> ThunkName; 1488 llvm::raw_svector_ostream Out(ThunkName); 1489 getMangleContext().mangleVirtualMemPtrThunk(MD, Out); 1490 Out.flush(); 1491 1492 // If the thunk has been generated previously, just return it. 1493 if (llvm::GlobalValue *GV = CGM.getModule().getNamedValue(ThunkName)) 1494 return cast<llvm::Function>(GV); 1495 1496 // Create the llvm::Function. 1497 const CGFunctionInfo &FnInfo = CGM.getTypes().arrangeMSMemberPointerThunk(MD); 1498 llvm::FunctionType *ThunkTy = CGM.getTypes().GetFunctionType(FnInfo); 1499 llvm::Function *ThunkFn = 1500 llvm::Function::Create(ThunkTy, llvm::Function::ExternalLinkage, 1501 ThunkName.str(), &CGM.getModule()); 1502 assert(ThunkFn->getName() == ThunkName && "name was uniqued!"); 1503 1504 ThunkFn->setLinkage(MD->isExternallyVisible() 1505 ? llvm::GlobalValue::LinkOnceODRLinkage 1506 : llvm::GlobalValue::InternalLinkage); 1507 1508 CGM.SetLLVMFunctionAttributes(MD, FnInfo, ThunkFn); 1509 CGM.SetLLVMFunctionAttributesForDefinition(MD, ThunkFn); 1510 1511 // These thunks can be compared, so they are not unnamed. 1512 ThunkFn->setUnnamedAddr(false); 1513 1514 // Start codegen. 1515 CodeGenFunction CGF(CGM); 1516 CGF.CurGD = GlobalDecl(MD); 1517 CGF.CurFuncIsThunk = true; 1518 1519 // Build FunctionArgs, but only include the implicit 'this' parameter 1520 // declaration. 1521 FunctionArgList FunctionArgs; 1522 buildThisParam(CGF, FunctionArgs); 1523 1524 // Start defining the function. 1525 CGF.StartFunction(GlobalDecl(), FnInfo.getReturnType(), ThunkFn, FnInfo, 1526 FunctionArgs, MD->getLocation(), SourceLocation()); 1527 EmitThisParam(CGF); 1528 1529 // Load the vfptr and then callee from the vftable. The callee should have 1530 // adjusted 'this' so that the vfptr is at offset zero. 1531 llvm::Value *VTable = CGF.GetVTablePtr( 1532 getThisValue(CGF), ThunkTy->getPointerTo()->getPointerTo()); 1533 llvm::Value *VFuncPtr = 1534 CGF.Builder.CreateConstInBoundsGEP1_64(VTable, ML.Index, "vfn"); 1535 llvm::Value *Callee = CGF.Builder.CreateLoad(VFuncPtr); 1536 1537 CGF.EmitMustTailThunk(MD, getThisValue(CGF), Callee); 1538 1539 return ThunkFn; 1540 } 1541 1542 void MicrosoftCXXABI::emitVirtualInheritanceTables(const CXXRecordDecl *RD) { 1543 const VBTableGlobals &VBGlobals = enumerateVBTables(RD); 1544 for (unsigned I = 0, E = VBGlobals.VBTables->size(); I != E; ++I) { 1545 const VPtrInfo *VBT = (*VBGlobals.VBTables)[I]; 1546 llvm::GlobalVariable *GV = VBGlobals.Globals[I]; 1547 emitVBTableDefinition(*VBT, RD, GV); 1548 } 1549 } 1550 1551 llvm::GlobalVariable * 1552 MicrosoftCXXABI::getAddrOfVBTable(const VPtrInfo &VBT, const CXXRecordDecl *RD, 1553 llvm::GlobalVariable::LinkageTypes Linkage) { 1554 SmallString<256> OutName; 1555 llvm::raw_svector_ostream Out(OutName); 1556 getMangleContext().mangleCXXVBTable(RD, VBT.MangledPath, Out); 1557 Out.flush(); 1558 StringRef Name = OutName.str(); 1559 1560 llvm::ArrayType *VBTableType = 1561 llvm::ArrayType::get(CGM.IntTy, 1 + VBT.ReusingBase->getNumVBases()); 1562 1563 assert(!CGM.getModule().getNamedGlobal(Name) && 1564 "vbtable with this name already exists: mangling bug?"); 1565 llvm::GlobalVariable *GV = 1566 CGM.CreateOrReplaceCXXRuntimeVariable(Name, VBTableType, Linkage); 1567 GV->setUnnamedAddr(true); 1568 1569 if (RD->hasAttr<DLLImportAttr>()) 1570 GV->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass); 1571 else if (RD->hasAttr<DLLExportAttr>()) 1572 GV->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass); 1573 1574 return GV; 1575 } 1576 1577 void MicrosoftCXXABI::emitVBTableDefinition(const VPtrInfo &VBT, 1578 const CXXRecordDecl *RD, 1579 llvm::GlobalVariable *GV) const { 1580 const CXXRecordDecl *ReusingBase = VBT.ReusingBase; 1581 1582 assert(RD->getNumVBases() && ReusingBase->getNumVBases() && 1583 "should only emit vbtables for classes with vbtables"); 1584 1585 const ASTRecordLayout &BaseLayout = 1586 CGM.getContext().getASTRecordLayout(VBT.BaseWithVPtr); 1587 const ASTRecordLayout &DerivedLayout = 1588 CGM.getContext().getASTRecordLayout(RD); 1589 1590 SmallVector<llvm::Constant *, 4> Offsets(1 + ReusingBase->getNumVBases(), 1591 nullptr); 1592 1593 // The offset from ReusingBase's vbptr to itself always leads. 1594 CharUnits VBPtrOffset = BaseLayout.getVBPtrOffset(); 1595 Offsets[0] = llvm::ConstantInt::get(CGM.IntTy, -VBPtrOffset.getQuantity()); 1596 1597 MicrosoftVTableContext &Context = CGM.getMicrosoftVTableContext(); 1598 for (const auto &I : ReusingBase->vbases()) { 1599 const CXXRecordDecl *VBase = I.getType()->getAsCXXRecordDecl(); 1600 CharUnits Offset = DerivedLayout.getVBaseClassOffset(VBase); 1601 assert(!Offset.isNegative()); 1602 1603 // Make it relative to the subobject vbptr. 1604 CharUnits CompleteVBPtrOffset = VBT.NonVirtualOffset + VBPtrOffset; 1605 if (VBT.getVBaseWithVPtr()) 1606 CompleteVBPtrOffset += 1607 DerivedLayout.getVBaseClassOffset(VBT.getVBaseWithVPtr()); 1608 Offset -= CompleteVBPtrOffset; 1609 1610 unsigned VBIndex = Context.getVBTableIndex(ReusingBase, VBase); 1611 assert(Offsets[VBIndex] == nullptr && "The same vbindex seen twice?"); 1612 Offsets[VBIndex] = llvm::ConstantInt::get(CGM.IntTy, Offset.getQuantity()); 1613 } 1614 1615 assert(Offsets.size() == 1616 cast<llvm::ArrayType>(cast<llvm::PointerType>(GV->getType()) 1617 ->getElementType())->getNumElements()); 1618 llvm::ArrayType *VBTableType = 1619 llvm::ArrayType::get(CGM.IntTy, Offsets.size()); 1620 llvm::Constant *Init = llvm::ConstantArray::get(VBTableType, Offsets); 1621 GV->setInitializer(Init); 1622 1623 // Set the right visibility. 1624 CGM.setGlobalVisibility(GV, RD); 1625 } 1626 1627 llvm::Value *MicrosoftCXXABI::performThisAdjustment(CodeGenFunction &CGF, 1628 llvm::Value *This, 1629 const ThisAdjustment &TA) { 1630 if (TA.isEmpty()) 1631 return This; 1632 1633 llvm::Value *V = CGF.Builder.CreateBitCast(This, CGF.Int8PtrTy); 1634 1635 if (!TA.Virtual.isEmpty()) { 1636 assert(TA.Virtual.Microsoft.VtordispOffset < 0); 1637 // Adjust the this argument based on the vtordisp value. 1638 llvm::Value *VtorDispPtr = 1639 CGF.Builder.CreateConstGEP1_32(V, TA.Virtual.Microsoft.VtordispOffset); 1640 VtorDispPtr = 1641 CGF.Builder.CreateBitCast(VtorDispPtr, CGF.Int32Ty->getPointerTo()); 1642 llvm::Value *VtorDisp = CGF.Builder.CreateLoad(VtorDispPtr, "vtordisp"); 1643 V = CGF.Builder.CreateGEP(V, CGF.Builder.CreateNeg(VtorDisp)); 1644 1645 if (TA.Virtual.Microsoft.VBPtrOffset) { 1646 // If the final overrider is defined in a virtual base other than the one 1647 // that holds the vfptr, we have to use a vtordispex thunk which looks up 1648 // the vbtable of the derived class. 1649 assert(TA.Virtual.Microsoft.VBPtrOffset > 0); 1650 assert(TA.Virtual.Microsoft.VBOffsetOffset >= 0); 1651 llvm::Value *VBPtr; 1652 llvm::Value *VBaseOffset = 1653 GetVBaseOffsetFromVBPtr(CGF, V, -TA.Virtual.Microsoft.VBPtrOffset, 1654 TA.Virtual.Microsoft.VBOffsetOffset, &VBPtr); 1655 V = CGF.Builder.CreateInBoundsGEP(VBPtr, VBaseOffset); 1656 } 1657 } 1658 1659 if (TA.NonVirtual) { 1660 // Non-virtual adjustment might result in a pointer outside the allocated 1661 // object, e.g. if the final overrider class is laid out after the virtual 1662 // base that declares a method in the most derived class. 1663 V = CGF.Builder.CreateConstGEP1_32(V, TA.NonVirtual); 1664 } 1665 1666 // Don't need to bitcast back, the call CodeGen will handle this. 1667 return V; 1668 } 1669 1670 llvm::Value * 1671 MicrosoftCXXABI::performReturnAdjustment(CodeGenFunction &CGF, llvm::Value *Ret, 1672 const ReturnAdjustment &RA) { 1673 if (RA.isEmpty()) 1674 return Ret; 1675 1676 llvm::Value *V = CGF.Builder.CreateBitCast(Ret, CGF.Int8PtrTy); 1677 1678 if (RA.Virtual.Microsoft.VBIndex) { 1679 assert(RA.Virtual.Microsoft.VBIndex > 0); 1680 int32_t IntSize = 1681 getContext().getTypeSizeInChars(getContext().IntTy).getQuantity(); 1682 llvm::Value *VBPtr; 1683 llvm::Value *VBaseOffset = 1684 GetVBaseOffsetFromVBPtr(CGF, V, RA.Virtual.Microsoft.VBPtrOffset, 1685 IntSize * RA.Virtual.Microsoft.VBIndex, &VBPtr); 1686 V = CGF.Builder.CreateInBoundsGEP(VBPtr, VBaseOffset); 1687 } 1688 1689 if (RA.NonVirtual) 1690 V = CGF.Builder.CreateConstInBoundsGEP1_32(V, RA.NonVirtual); 1691 1692 // Cast back to the original type. 1693 return CGF.Builder.CreateBitCast(V, Ret->getType()); 1694 } 1695 1696 bool MicrosoftCXXABI::requiresArrayCookie(const CXXDeleteExpr *expr, 1697 QualType elementType) { 1698 // Microsoft seems to completely ignore the possibility of a 1699 // two-argument usual deallocation function. 1700 return elementType.isDestructedType(); 1701 } 1702 1703 bool MicrosoftCXXABI::requiresArrayCookie(const CXXNewExpr *expr) { 1704 // Microsoft seems to completely ignore the possibility of a 1705 // two-argument usual deallocation function. 1706 return expr->getAllocatedType().isDestructedType(); 1707 } 1708 1709 CharUnits MicrosoftCXXABI::getArrayCookieSizeImpl(QualType type) { 1710 // The array cookie is always a size_t; we then pad that out to the 1711 // alignment of the element type. 1712 ASTContext &Ctx = getContext(); 1713 return std::max(Ctx.getTypeSizeInChars(Ctx.getSizeType()), 1714 Ctx.getTypeAlignInChars(type)); 1715 } 1716 1717 llvm::Value *MicrosoftCXXABI::readArrayCookieImpl(CodeGenFunction &CGF, 1718 llvm::Value *allocPtr, 1719 CharUnits cookieSize) { 1720 unsigned AS = allocPtr->getType()->getPointerAddressSpace(); 1721 llvm::Value *numElementsPtr = 1722 CGF.Builder.CreateBitCast(allocPtr, CGF.SizeTy->getPointerTo(AS)); 1723 return CGF.Builder.CreateLoad(numElementsPtr); 1724 } 1725 1726 llvm::Value* MicrosoftCXXABI::InitializeArrayCookie(CodeGenFunction &CGF, 1727 llvm::Value *newPtr, 1728 llvm::Value *numElements, 1729 const CXXNewExpr *expr, 1730 QualType elementType) { 1731 assert(requiresArrayCookie(expr)); 1732 1733 // The size of the cookie. 1734 CharUnits cookieSize = getArrayCookieSizeImpl(elementType); 1735 1736 // Compute an offset to the cookie. 1737 llvm::Value *cookiePtr = newPtr; 1738 1739 // Write the number of elements into the appropriate slot. 1740 unsigned AS = newPtr->getType()->getPointerAddressSpace(); 1741 llvm::Value *numElementsPtr 1742 = CGF.Builder.CreateBitCast(cookiePtr, CGF.SizeTy->getPointerTo(AS)); 1743 CGF.Builder.CreateStore(numElements, numElementsPtr); 1744 1745 // Finally, compute a pointer to the actual data buffer by skipping 1746 // over the cookie completely. 1747 return CGF.Builder.CreateConstInBoundsGEP1_64(newPtr, 1748 cookieSize.getQuantity()); 1749 } 1750 1751 static void emitGlobalDtorWithTLRegDtor(CodeGenFunction &CGF, const VarDecl &VD, 1752 llvm::Constant *Dtor, 1753 llvm::Constant *Addr) { 1754 // Create a function which calls the destructor. 1755 llvm::Constant *DtorStub = CGF.createAtExitStub(VD, Dtor, Addr); 1756 1757 // extern "C" int __tlregdtor(void (*f)(void)); 1758 llvm::FunctionType *TLRegDtorTy = llvm::FunctionType::get( 1759 CGF.IntTy, DtorStub->getType(), /*IsVarArg=*/false); 1760 1761 llvm::Constant *TLRegDtor = 1762 CGF.CGM.CreateRuntimeFunction(TLRegDtorTy, "__tlregdtor"); 1763 if (llvm::Function *TLRegDtorFn = dyn_cast<llvm::Function>(TLRegDtor)) 1764 TLRegDtorFn->setDoesNotThrow(); 1765 1766 CGF.EmitNounwindRuntimeCall(TLRegDtor, DtorStub); 1767 } 1768 1769 void MicrosoftCXXABI::registerGlobalDtor(CodeGenFunction &CGF, const VarDecl &D, 1770 llvm::Constant *Dtor, 1771 llvm::Constant *Addr) { 1772 if (D.getTLSKind()) 1773 return emitGlobalDtorWithTLRegDtor(CGF, D, Dtor, Addr); 1774 1775 // The default behavior is to use atexit. 1776 CGF.registerGlobalDtorWithAtExit(D, Dtor, Addr); 1777 } 1778 1779 void MicrosoftCXXABI::EmitThreadLocalInitFuncs( 1780 CodeGenModule &CGM, 1781 ArrayRef<std::pair<const VarDecl *, llvm::GlobalVariable *>> 1782 CXXThreadLocals, 1783 ArrayRef<llvm::Function *> CXXThreadLocalInits, 1784 ArrayRef<llvm::GlobalVariable *> CXXThreadLocalInitVars) { 1785 // This will create a GV in the .CRT$XDU section. It will point to our 1786 // initialization function. The CRT will call all of these function 1787 // pointers at start-up time and, eventually, at thread-creation time. 1788 auto AddToXDU = [&CGM](llvm::Function *InitFunc) { 1789 llvm::GlobalVariable *InitFuncPtr = new llvm::GlobalVariable( 1790 CGM.getModule(), InitFunc->getType(), /*IsConstant=*/true, 1791 llvm::GlobalVariable::InternalLinkage, InitFunc, 1792 Twine(InitFunc->getName(), "$initializer$")); 1793 InitFuncPtr->setSection(".CRT$XDU"); 1794 // This variable has discardable linkage, we have to add it to @llvm.used to 1795 // ensure it won't get discarded. 1796 CGM.addUsedGlobal(InitFuncPtr); 1797 return InitFuncPtr; 1798 }; 1799 1800 std::vector<llvm::Function *> NonComdatInits; 1801 for (size_t I = 0, E = CXXThreadLocalInitVars.size(); I != E; ++I) { 1802 llvm::GlobalVariable *GV = CXXThreadLocalInitVars[I]; 1803 llvm::Function *F = CXXThreadLocalInits[I]; 1804 1805 // If the GV is already in a comdat group, then we have to join it. 1806 llvm::Comdat *C = GV->getComdat(); 1807 1808 // LinkOnce and Weak linkage are lowered down to a single-member comdat 1809 // group. 1810 // Make an explicit group so we can join it. 1811 if (!C && (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage())) { 1812 C = CGM.getModule().getOrInsertComdat(GV->getName()); 1813 GV->setComdat(C); 1814 AddToXDU(F)->setComdat(C); 1815 } else { 1816 NonComdatInits.push_back(F); 1817 } 1818 } 1819 1820 if (!NonComdatInits.empty()) { 1821 llvm::FunctionType *FTy = 1822 llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false); 1823 llvm::Function *InitFunc = CGM.CreateGlobalInitOrDestructFunction( 1824 FTy, "__tls_init", SourceLocation(), 1825 /*TLS=*/true); 1826 CodeGenFunction(CGM).GenerateCXXGlobalInitFunc(InitFunc, NonComdatInits); 1827 1828 AddToXDU(InitFunc); 1829 } 1830 } 1831 1832 LValue MicrosoftCXXABI::EmitThreadLocalVarDeclLValue(CodeGenFunction &CGF, 1833 const VarDecl *VD, 1834 QualType LValType) { 1835 CGF.CGM.ErrorUnsupported(VD, "thread wrappers"); 1836 return LValue(); 1837 } 1838 1839 void MicrosoftCXXABI::EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D, 1840 llvm::GlobalVariable *GV, 1841 bool PerformInit) { 1842 // MSVC only uses guards for static locals. 1843 if (!D.isStaticLocal()) { 1844 assert(GV->hasWeakLinkage() || GV->hasLinkOnceLinkage()); 1845 // GlobalOpt is allowed to discard the initializer, so use linkonce_odr. 1846 CGF.CurFn->setLinkage(llvm::GlobalValue::LinkOnceODRLinkage); 1847 CGF.EmitCXXGlobalVarDeclInit(D, GV, PerformInit); 1848 return; 1849 } 1850 1851 // MSVC always uses an i32 bitfield to guard initialization, which is *not* 1852 // threadsafe. Since the user may be linking in inline functions compiled by 1853 // cl.exe, there's no reason to provide a false sense of security by using 1854 // critical sections here. 1855 1856 if (D.getTLSKind()) 1857 CGM.ErrorUnsupported(&D, "dynamic TLS initialization"); 1858 1859 CGBuilderTy &Builder = CGF.Builder; 1860 llvm::IntegerType *GuardTy = CGF.Int32Ty; 1861 llvm::ConstantInt *Zero = llvm::ConstantInt::get(GuardTy, 0); 1862 1863 // Get the guard variable for this function if we have one already. 1864 GuardInfo *GI = &GuardVariableMap[D.getDeclContext()]; 1865 1866 unsigned BitIndex; 1867 if (D.isStaticLocal() && D.isExternallyVisible()) { 1868 // Externally visible variables have to be numbered in Sema to properly 1869 // handle unreachable VarDecls. 1870 BitIndex = getContext().getStaticLocalNumber(&D); 1871 assert(BitIndex > 0); 1872 BitIndex--; 1873 } else { 1874 // Non-externally visible variables are numbered here in CodeGen. 1875 BitIndex = GI->BitIndex++; 1876 } 1877 1878 if (BitIndex >= 32) { 1879 if (D.isExternallyVisible()) 1880 ErrorUnsupportedABI(CGF, "more than 32 guarded initializations"); 1881 BitIndex %= 32; 1882 GI->Guard = nullptr; 1883 } 1884 1885 // Lazily create the i32 bitfield for this function. 1886 if (!GI->Guard) { 1887 // Mangle the name for the guard. 1888 SmallString<256> GuardName; 1889 { 1890 llvm::raw_svector_ostream Out(GuardName); 1891 getMangleContext().mangleStaticGuardVariable(&D, Out); 1892 Out.flush(); 1893 } 1894 1895 // Create the guard variable with a zero-initializer. Just absorb linkage, 1896 // visibility and dll storage class from the guarded variable. 1897 GI->Guard = 1898 new llvm::GlobalVariable(CGM.getModule(), GuardTy, false, 1899 GV->getLinkage(), Zero, GuardName.str()); 1900 GI->Guard->setVisibility(GV->getVisibility()); 1901 GI->Guard->setDLLStorageClass(GV->getDLLStorageClass()); 1902 } else { 1903 assert(GI->Guard->getLinkage() == GV->getLinkage() && 1904 "static local from the same function had different linkage"); 1905 } 1906 1907 // Pseudo code for the test: 1908 // if (!(GuardVar & MyGuardBit)) { 1909 // GuardVar |= MyGuardBit; 1910 // ... initialize the object ...; 1911 // } 1912 1913 // Test our bit from the guard variable. 1914 llvm::ConstantInt *Bit = llvm::ConstantInt::get(GuardTy, 1U << BitIndex); 1915 llvm::LoadInst *LI = Builder.CreateLoad(GI->Guard); 1916 llvm::Value *IsInitialized = 1917 Builder.CreateICmpNE(Builder.CreateAnd(LI, Bit), Zero); 1918 llvm::BasicBlock *InitBlock = CGF.createBasicBlock("init"); 1919 llvm::BasicBlock *EndBlock = CGF.createBasicBlock("init.end"); 1920 Builder.CreateCondBr(IsInitialized, EndBlock, InitBlock); 1921 1922 // Set our bit in the guard variable and emit the initializer and add a global 1923 // destructor if appropriate. 1924 CGF.EmitBlock(InitBlock); 1925 Builder.CreateStore(Builder.CreateOr(LI, Bit), GI->Guard); 1926 CGF.EmitCXXGlobalVarDeclInit(D, GV, PerformInit); 1927 Builder.CreateBr(EndBlock); 1928 1929 // Continue. 1930 CGF.EmitBlock(EndBlock); 1931 } 1932 1933 bool MicrosoftCXXABI::isZeroInitializable(const MemberPointerType *MPT) { 1934 // Null-ness for function memptrs only depends on the first field, which is 1935 // the function pointer. The rest don't matter, so we can zero initialize. 1936 if (MPT->isMemberFunctionPointer()) 1937 return true; 1938 1939 // The virtual base adjustment field is always -1 for null, so if we have one 1940 // we can't zero initialize. The field offset is sometimes also -1 if 0 is a 1941 // valid field offset. 1942 const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl(); 1943 MSInheritanceAttr::Spelling Inheritance = RD->getMSInheritanceModel(); 1944 return (!MSInheritanceAttr::hasVBTableOffsetField(Inheritance) && 1945 RD->nullFieldOffsetIsZero()); 1946 } 1947 1948 llvm::Type * 1949 MicrosoftCXXABI::ConvertMemberPointerType(const MemberPointerType *MPT) { 1950 const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl(); 1951 MSInheritanceAttr::Spelling Inheritance = RD->getMSInheritanceModel(); 1952 llvm::SmallVector<llvm::Type *, 4> fields; 1953 if (MPT->isMemberFunctionPointer()) 1954 fields.push_back(CGM.VoidPtrTy); // FunctionPointerOrVirtualThunk 1955 else 1956 fields.push_back(CGM.IntTy); // FieldOffset 1957 1958 if (MSInheritanceAttr::hasNVOffsetField(MPT->isMemberFunctionPointer(), 1959 Inheritance)) 1960 fields.push_back(CGM.IntTy); 1961 if (MSInheritanceAttr::hasVBPtrOffsetField(Inheritance)) 1962 fields.push_back(CGM.IntTy); 1963 if (MSInheritanceAttr::hasVBTableOffsetField(Inheritance)) 1964 fields.push_back(CGM.IntTy); // VirtualBaseAdjustmentOffset 1965 1966 if (fields.size() == 1) 1967 return fields[0]; 1968 return llvm::StructType::get(CGM.getLLVMContext(), fields); 1969 } 1970 1971 void MicrosoftCXXABI:: 1972 GetNullMemberPointerFields(const MemberPointerType *MPT, 1973 llvm::SmallVectorImpl<llvm::Constant *> &fields) { 1974 assert(fields.empty()); 1975 const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl(); 1976 MSInheritanceAttr::Spelling Inheritance = RD->getMSInheritanceModel(); 1977 if (MPT->isMemberFunctionPointer()) { 1978 // FunctionPointerOrVirtualThunk 1979 fields.push_back(llvm::Constant::getNullValue(CGM.VoidPtrTy)); 1980 } else { 1981 if (RD->nullFieldOffsetIsZero()) 1982 fields.push_back(getZeroInt()); // FieldOffset 1983 else 1984 fields.push_back(getAllOnesInt()); // FieldOffset 1985 } 1986 1987 if (MSInheritanceAttr::hasNVOffsetField(MPT->isMemberFunctionPointer(), 1988 Inheritance)) 1989 fields.push_back(getZeroInt()); 1990 if (MSInheritanceAttr::hasVBPtrOffsetField(Inheritance)) 1991 fields.push_back(getZeroInt()); 1992 if (MSInheritanceAttr::hasVBTableOffsetField(Inheritance)) 1993 fields.push_back(getAllOnesInt()); 1994 } 1995 1996 llvm::Constant * 1997 MicrosoftCXXABI::EmitNullMemberPointer(const MemberPointerType *MPT) { 1998 llvm::SmallVector<llvm::Constant *, 4> fields; 1999 GetNullMemberPointerFields(MPT, fields); 2000 if (fields.size() == 1) 2001 return fields[0]; 2002 llvm::Constant *Res = llvm::ConstantStruct::getAnon(fields); 2003 assert(Res->getType() == ConvertMemberPointerType(MPT)); 2004 return Res; 2005 } 2006 2007 llvm::Constant * 2008 MicrosoftCXXABI::EmitFullMemberPointer(llvm::Constant *FirstField, 2009 bool IsMemberFunction, 2010 const CXXRecordDecl *RD, 2011 CharUnits NonVirtualBaseAdjustment) 2012 { 2013 MSInheritanceAttr::Spelling Inheritance = RD->getMSInheritanceModel(); 2014 2015 // Single inheritance class member pointer are represented as scalars instead 2016 // of aggregates. 2017 if (MSInheritanceAttr::hasOnlyOneField(IsMemberFunction, Inheritance)) 2018 return FirstField; 2019 2020 llvm::SmallVector<llvm::Constant *, 4> fields; 2021 fields.push_back(FirstField); 2022 2023 if (MSInheritanceAttr::hasNVOffsetField(IsMemberFunction, Inheritance)) 2024 fields.push_back(llvm::ConstantInt::get( 2025 CGM.IntTy, NonVirtualBaseAdjustment.getQuantity())); 2026 2027 if (MSInheritanceAttr::hasVBPtrOffsetField(Inheritance)) { 2028 CharUnits Offs = CharUnits::Zero(); 2029 if (RD->getNumVBases()) 2030 Offs = getContext().getASTRecordLayout(RD).getVBPtrOffset(); 2031 fields.push_back(llvm::ConstantInt::get(CGM.IntTy, Offs.getQuantity())); 2032 } 2033 2034 // The rest of the fields are adjusted by conversions to a more derived class. 2035 if (MSInheritanceAttr::hasVBTableOffsetField(Inheritance)) 2036 fields.push_back(getZeroInt()); 2037 2038 return llvm::ConstantStruct::getAnon(fields); 2039 } 2040 2041 llvm::Constant * 2042 MicrosoftCXXABI::EmitMemberDataPointer(const MemberPointerType *MPT, 2043 CharUnits offset) { 2044 const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl(); 2045 llvm::Constant *FirstField = 2046 llvm::ConstantInt::get(CGM.IntTy, offset.getQuantity()); 2047 return EmitFullMemberPointer(FirstField, /*IsMemberFunction=*/false, RD, 2048 CharUnits::Zero()); 2049 } 2050 2051 llvm::Constant *MicrosoftCXXABI::EmitMemberPointer(const CXXMethodDecl *MD) { 2052 return BuildMemberPointer(MD->getParent(), MD, CharUnits::Zero()); 2053 } 2054 2055 llvm::Constant *MicrosoftCXXABI::EmitMemberPointer(const APValue &MP, 2056 QualType MPType) { 2057 const MemberPointerType *MPT = MPType->castAs<MemberPointerType>(); 2058 const ValueDecl *MPD = MP.getMemberPointerDecl(); 2059 if (!MPD) 2060 return EmitNullMemberPointer(MPT); 2061 2062 CharUnits ThisAdjustment = getMemberPointerPathAdjustment(MP); 2063 2064 // FIXME PR15713: Support virtual inheritance paths. 2065 2066 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MPD)) 2067 return BuildMemberPointer(MPT->getMostRecentCXXRecordDecl(), MD, 2068 ThisAdjustment); 2069 2070 CharUnits FieldOffset = 2071 getContext().toCharUnitsFromBits(getContext().getFieldOffset(MPD)); 2072 return EmitMemberDataPointer(MPT, ThisAdjustment + FieldOffset); 2073 } 2074 2075 llvm::Constant * 2076 MicrosoftCXXABI::BuildMemberPointer(const CXXRecordDecl *RD, 2077 const CXXMethodDecl *MD, 2078 CharUnits NonVirtualBaseAdjustment) { 2079 assert(MD->isInstance() && "Member function must not be static!"); 2080 MD = MD->getCanonicalDecl(); 2081 RD = RD->getMostRecentDecl(); 2082 CodeGenTypes &Types = CGM.getTypes(); 2083 2084 llvm::Constant *FirstField; 2085 const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>(); 2086 if (!MD->isVirtual()) { 2087 llvm::Type *Ty; 2088 // Check whether the function has a computable LLVM signature. 2089 if (Types.isFuncTypeConvertible(FPT)) { 2090 // The function has a computable LLVM signature; use the correct type. 2091 Ty = Types.GetFunctionType(Types.arrangeCXXMethodDeclaration(MD)); 2092 } else { 2093 // Use an arbitrary non-function type to tell GetAddrOfFunction that the 2094 // function type is incomplete. 2095 Ty = CGM.PtrDiffTy; 2096 } 2097 FirstField = CGM.GetAddrOfFunction(MD, Ty); 2098 FirstField = llvm::ConstantExpr::getBitCast(FirstField, CGM.VoidPtrTy); 2099 } else { 2100 MicrosoftVTableContext::MethodVFTableLocation ML = 2101 CGM.getMicrosoftVTableContext().getMethodVFTableLocation(MD); 2102 if (!CGM.getTypes().isFuncTypeConvertible( 2103 MD->getType()->castAs<FunctionType>())) { 2104 CGM.ErrorUnsupported(MD, "pointer to virtual member function with " 2105 "incomplete return or parameter type"); 2106 FirstField = llvm::Constant::getNullValue(CGM.VoidPtrTy); 2107 } else if (FPT->getCallConv() == CC_X86FastCall) { 2108 CGM.ErrorUnsupported(MD, "pointer to fastcall virtual member function"); 2109 FirstField = llvm::Constant::getNullValue(CGM.VoidPtrTy); 2110 } else if (ML.VBase) { 2111 CGM.ErrorUnsupported(MD, "pointer to virtual member function overriding " 2112 "member function in virtual base class"); 2113 FirstField = llvm::Constant::getNullValue(CGM.VoidPtrTy); 2114 } else { 2115 llvm::Function *Thunk = EmitVirtualMemPtrThunk(MD, ML); 2116 FirstField = llvm::ConstantExpr::getBitCast(Thunk, CGM.VoidPtrTy); 2117 // Include the vfptr adjustment if the method is in a non-primary vftable. 2118 NonVirtualBaseAdjustment += ML.VFPtrOffset; 2119 } 2120 } 2121 2122 // The rest of the fields are common with data member pointers. 2123 return EmitFullMemberPointer(FirstField, /*IsMemberFunction=*/true, RD, 2124 NonVirtualBaseAdjustment); 2125 } 2126 2127 /// Member pointers are the same if they're either bitwise identical *or* both 2128 /// null. Null-ness for function members is determined by the first field, 2129 /// while for data member pointers we must compare all fields. 2130 llvm::Value * 2131 MicrosoftCXXABI::EmitMemberPointerComparison(CodeGenFunction &CGF, 2132 llvm::Value *L, 2133 llvm::Value *R, 2134 const MemberPointerType *MPT, 2135 bool Inequality) { 2136 CGBuilderTy &Builder = CGF.Builder; 2137 2138 // Handle != comparisons by switching the sense of all boolean operations. 2139 llvm::ICmpInst::Predicate Eq; 2140 llvm::Instruction::BinaryOps And, Or; 2141 if (Inequality) { 2142 Eq = llvm::ICmpInst::ICMP_NE; 2143 And = llvm::Instruction::Or; 2144 Or = llvm::Instruction::And; 2145 } else { 2146 Eq = llvm::ICmpInst::ICMP_EQ; 2147 And = llvm::Instruction::And; 2148 Or = llvm::Instruction::Or; 2149 } 2150 2151 // If this is a single field member pointer (single inheritance), this is a 2152 // single icmp. 2153 const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl(); 2154 MSInheritanceAttr::Spelling Inheritance = RD->getMSInheritanceModel(); 2155 if (MSInheritanceAttr::hasOnlyOneField(MPT->isMemberFunctionPointer(), 2156 Inheritance)) 2157 return Builder.CreateICmp(Eq, L, R); 2158 2159 // Compare the first field. 2160 llvm::Value *L0 = Builder.CreateExtractValue(L, 0, "lhs.0"); 2161 llvm::Value *R0 = Builder.CreateExtractValue(R, 0, "rhs.0"); 2162 llvm::Value *Cmp0 = Builder.CreateICmp(Eq, L0, R0, "memptr.cmp.first"); 2163 2164 // Compare everything other than the first field. 2165 llvm::Value *Res = nullptr; 2166 llvm::StructType *LType = cast<llvm::StructType>(L->getType()); 2167 for (unsigned I = 1, E = LType->getNumElements(); I != E; ++I) { 2168 llvm::Value *LF = Builder.CreateExtractValue(L, I); 2169 llvm::Value *RF = Builder.CreateExtractValue(R, I); 2170 llvm::Value *Cmp = Builder.CreateICmp(Eq, LF, RF, "memptr.cmp.rest"); 2171 if (Res) 2172 Res = Builder.CreateBinOp(And, Res, Cmp); 2173 else 2174 Res = Cmp; 2175 } 2176 2177 // Check if the first field is 0 if this is a function pointer. 2178 if (MPT->isMemberFunctionPointer()) { 2179 // (l1 == r1 && ...) || l0 == 0 2180 llvm::Value *Zero = llvm::Constant::getNullValue(L0->getType()); 2181 llvm::Value *IsZero = Builder.CreateICmp(Eq, L0, Zero, "memptr.cmp.iszero"); 2182 Res = Builder.CreateBinOp(Or, Res, IsZero); 2183 } 2184 2185 // Combine the comparison of the first field, which must always be true for 2186 // this comparison to succeeed. 2187 return Builder.CreateBinOp(And, Res, Cmp0, "memptr.cmp"); 2188 } 2189 2190 llvm::Value * 2191 MicrosoftCXXABI::EmitMemberPointerIsNotNull(CodeGenFunction &CGF, 2192 llvm::Value *MemPtr, 2193 const MemberPointerType *MPT) { 2194 CGBuilderTy &Builder = CGF.Builder; 2195 llvm::SmallVector<llvm::Constant *, 4> fields; 2196 // We only need one field for member functions. 2197 if (MPT->isMemberFunctionPointer()) 2198 fields.push_back(llvm::Constant::getNullValue(CGM.VoidPtrTy)); 2199 else 2200 GetNullMemberPointerFields(MPT, fields); 2201 assert(!fields.empty()); 2202 llvm::Value *FirstField = MemPtr; 2203 if (MemPtr->getType()->isStructTy()) 2204 FirstField = Builder.CreateExtractValue(MemPtr, 0); 2205 llvm::Value *Res = Builder.CreateICmpNE(FirstField, fields[0], "memptr.cmp0"); 2206 2207 // For function member pointers, we only need to test the function pointer 2208 // field. The other fields if any can be garbage. 2209 if (MPT->isMemberFunctionPointer()) 2210 return Res; 2211 2212 // Otherwise, emit a series of compares and combine the results. 2213 for (int I = 1, E = fields.size(); I < E; ++I) { 2214 llvm::Value *Field = Builder.CreateExtractValue(MemPtr, I); 2215 llvm::Value *Next = Builder.CreateICmpNE(Field, fields[I], "memptr.cmp"); 2216 Res = Builder.CreateOr(Res, Next, "memptr.tobool"); 2217 } 2218 return Res; 2219 } 2220 2221 bool MicrosoftCXXABI::MemberPointerConstantIsNull(const MemberPointerType *MPT, 2222 llvm::Constant *Val) { 2223 // Function pointers are null if the pointer in the first field is null. 2224 if (MPT->isMemberFunctionPointer()) { 2225 llvm::Constant *FirstField = Val->getType()->isStructTy() ? 2226 Val->getAggregateElement(0U) : Val; 2227 return FirstField->isNullValue(); 2228 } 2229 2230 // If it's not a function pointer and it's zero initializable, we can easily 2231 // check zero. 2232 if (isZeroInitializable(MPT) && Val->isNullValue()) 2233 return true; 2234 2235 // Otherwise, break down all the fields for comparison. Hopefully these 2236 // little Constants are reused, while a big null struct might not be. 2237 llvm::SmallVector<llvm::Constant *, 4> Fields; 2238 GetNullMemberPointerFields(MPT, Fields); 2239 if (Fields.size() == 1) { 2240 assert(Val->getType()->isIntegerTy()); 2241 return Val == Fields[0]; 2242 } 2243 2244 unsigned I, E; 2245 for (I = 0, E = Fields.size(); I != E; ++I) { 2246 if (Val->getAggregateElement(I) != Fields[I]) 2247 break; 2248 } 2249 return I == E; 2250 } 2251 2252 llvm::Value * 2253 MicrosoftCXXABI::GetVBaseOffsetFromVBPtr(CodeGenFunction &CGF, 2254 llvm::Value *This, 2255 llvm::Value *VBPtrOffset, 2256 llvm::Value *VBTableOffset, 2257 llvm::Value **VBPtrOut) { 2258 CGBuilderTy &Builder = CGF.Builder; 2259 // Load the vbtable pointer from the vbptr in the instance. 2260 This = Builder.CreateBitCast(This, CGM.Int8PtrTy); 2261 llvm::Value *VBPtr = 2262 Builder.CreateInBoundsGEP(This, VBPtrOffset, "vbptr"); 2263 if (VBPtrOut) *VBPtrOut = VBPtr; 2264 VBPtr = Builder.CreateBitCast(VBPtr, 2265 CGM.Int32Ty->getPointerTo(0)->getPointerTo(0)); 2266 llvm::Value *VBTable = Builder.CreateLoad(VBPtr, "vbtable"); 2267 2268 // Translate from byte offset to table index. It improves analyzability. 2269 llvm::Value *VBTableIndex = Builder.CreateAShr( 2270 VBTableOffset, llvm::ConstantInt::get(VBTableOffset->getType(), 2), 2271 "vbtindex", /*isExact=*/true); 2272 2273 // Load an i32 offset from the vb-table. 2274 llvm::Value *VBaseOffs = Builder.CreateInBoundsGEP(VBTable, VBTableIndex); 2275 VBaseOffs = Builder.CreateBitCast(VBaseOffs, CGM.Int32Ty->getPointerTo(0)); 2276 return Builder.CreateLoad(VBaseOffs, "vbase_offs"); 2277 } 2278 2279 // Returns an adjusted base cast to i8*, since we do more address arithmetic on 2280 // it. 2281 llvm::Value *MicrosoftCXXABI::AdjustVirtualBase( 2282 CodeGenFunction &CGF, const Expr *E, const CXXRecordDecl *RD, 2283 llvm::Value *Base, llvm::Value *VBTableOffset, llvm::Value *VBPtrOffset) { 2284 CGBuilderTy &Builder = CGF.Builder; 2285 Base = Builder.CreateBitCast(Base, CGM.Int8PtrTy); 2286 llvm::BasicBlock *OriginalBB = nullptr; 2287 llvm::BasicBlock *SkipAdjustBB = nullptr; 2288 llvm::BasicBlock *VBaseAdjustBB = nullptr; 2289 2290 // In the unspecified inheritance model, there might not be a vbtable at all, 2291 // in which case we need to skip the virtual base lookup. If there is a 2292 // vbtable, the first entry is a no-op entry that gives back the original 2293 // base, so look for a virtual base adjustment offset of zero. 2294 if (VBPtrOffset) { 2295 OriginalBB = Builder.GetInsertBlock(); 2296 VBaseAdjustBB = CGF.createBasicBlock("memptr.vadjust"); 2297 SkipAdjustBB = CGF.createBasicBlock("memptr.skip_vadjust"); 2298 llvm::Value *IsVirtual = 2299 Builder.CreateICmpNE(VBTableOffset, getZeroInt(), 2300 "memptr.is_vbase"); 2301 Builder.CreateCondBr(IsVirtual, VBaseAdjustBB, SkipAdjustBB); 2302 CGF.EmitBlock(VBaseAdjustBB); 2303 } 2304 2305 // If we weren't given a dynamic vbptr offset, RD should be complete and we'll 2306 // know the vbptr offset. 2307 if (!VBPtrOffset) { 2308 CharUnits offs = CharUnits::Zero(); 2309 if (!RD->hasDefinition()) { 2310 DiagnosticsEngine &Diags = CGF.CGM.getDiags(); 2311 unsigned DiagID = Diags.getCustomDiagID( 2312 DiagnosticsEngine::Error, 2313 "member pointer representation requires a " 2314 "complete class type for %0 to perform this expression"); 2315 Diags.Report(E->getExprLoc(), DiagID) << RD << E->getSourceRange(); 2316 } else if (RD->getNumVBases()) 2317 offs = getContext().getASTRecordLayout(RD).getVBPtrOffset(); 2318 VBPtrOffset = llvm::ConstantInt::get(CGM.IntTy, offs.getQuantity()); 2319 } 2320 llvm::Value *VBPtr = nullptr; 2321 llvm::Value *VBaseOffs = 2322 GetVBaseOffsetFromVBPtr(CGF, Base, VBPtrOffset, VBTableOffset, &VBPtr); 2323 llvm::Value *AdjustedBase = Builder.CreateInBoundsGEP(VBPtr, VBaseOffs); 2324 2325 // Merge control flow with the case where we didn't have to adjust. 2326 if (VBaseAdjustBB) { 2327 Builder.CreateBr(SkipAdjustBB); 2328 CGF.EmitBlock(SkipAdjustBB); 2329 llvm::PHINode *Phi = Builder.CreatePHI(CGM.Int8PtrTy, 2, "memptr.base"); 2330 Phi->addIncoming(Base, OriginalBB); 2331 Phi->addIncoming(AdjustedBase, VBaseAdjustBB); 2332 return Phi; 2333 } 2334 return AdjustedBase; 2335 } 2336 2337 llvm::Value *MicrosoftCXXABI::EmitMemberDataPointerAddress( 2338 CodeGenFunction &CGF, const Expr *E, llvm::Value *Base, llvm::Value *MemPtr, 2339 const MemberPointerType *MPT) { 2340 assert(MPT->isMemberDataPointer()); 2341 unsigned AS = Base->getType()->getPointerAddressSpace(); 2342 llvm::Type *PType = 2343 CGF.ConvertTypeForMem(MPT->getPointeeType())->getPointerTo(AS); 2344 CGBuilderTy &Builder = CGF.Builder; 2345 const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl(); 2346 MSInheritanceAttr::Spelling Inheritance = RD->getMSInheritanceModel(); 2347 2348 // Extract the fields we need, regardless of model. We'll apply them if we 2349 // have them. 2350 llvm::Value *FieldOffset = MemPtr; 2351 llvm::Value *VirtualBaseAdjustmentOffset = nullptr; 2352 llvm::Value *VBPtrOffset = nullptr; 2353 if (MemPtr->getType()->isStructTy()) { 2354 // We need to extract values. 2355 unsigned I = 0; 2356 FieldOffset = Builder.CreateExtractValue(MemPtr, I++); 2357 if (MSInheritanceAttr::hasVBPtrOffsetField(Inheritance)) 2358 VBPtrOffset = Builder.CreateExtractValue(MemPtr, I++); 2359 if (MSInheritanceAttr::hasVBTableOffsetField(Inheritance)) 2360 VirtualBaseAdjustmentOffset = Builder.CreateExtractValue(MemPtr, I++); 2361 } 2362 2363 if (VirtualBaseAdjustmentOffset) { 2364 Base = AdjustVirtualBase(CGF, E, RD, Base, VirtualBaseAdjustmentOffset, 2365 VBPtrOffset); 2366 } 2367 2368 // Cast to char*. 2369 Base = Builder.CreateBitCast(Base, Builder.getInt8Ty()->getPointerTo(AS)); 2370 2371 // Apply the offset, which we assume is non-null. 2372 llvm::Value *Addr = 2373 Builder.CreateInBoundsGEP(Base, FieldOffset, "memptr.offset"); 2374 2375 // Cast the address to the appropriate pointer type, adopting the address 2376 // space of the base pointer. 2377 return Builder.CreateBitCast(Addr, PType); 2378 } 2379 2380 static MSInheritanceAttr::Spelling 2381 getInheritanceFromMemptr(const MemberPointerType *MPT) { 2382 return MPT->getMostRecentCXXRecordDecl()->getMSInheritanceModel(); 2383 } 2384 2385 llvm::Value * 2386 MicrosoftCXXABI::EmitMemberPointerConversion(CodeGenFunction &CGF, 2387 const CastExpr *E, 2388 llvm::Value *Src) { 2389 assert(E->getCastKind() == CK_DerivedToBaseMemberPointer || 2390 E->getCastKind() == CK_BaseToDerivedMemberPointer || 2391 E->getCastKind() == CK_ReinterpretMemberPointer); 2392 2393 // Use constant emission if we can. 2394 if (isa<llvm::Constant>(Src)) 2395 return EmitMemberPointerConversion(E, cast<llvm::Constant>(Src)); 2396 2397 // We may be adding or dropping fields from the member pointer, so we need 2398 // both types and the inheritance models of both records. 2399 const MemberPointerType *SrcTy = 2400 E->getSubExpr()->getType()->castAs<MemberPointerType>(); 2401 const MemberPointerType *DstTy = E->getType()->castAs<MemberPointerType>(); 2402 bool IsFunc = SrcTy->isMemberFunctionPointer(); 2403 2404 // If the classes use the same null representation, reinterpret_cast is a nop. 2405 bool IsReinterpret = E->getCastKind() == CK_ReinterpretMemberPointer; 2406 if (IsReinterpret && IsFunc) 2407 return Src; 2408 2409 CXXRecordDecl *SrcRD = SrcTy->getMostRecentCXXRecordDecl(); 2410 CXXRecordDecl *DstRD = DstTy->getMostRecentCXXRecordDecl(); 2411 if (IsReinterpret && 2412 SrcRD->nullFieldOffsetIsZero() == DstRD->nullFieldOffsetIsZero()) 2413 return Src; 2414 2415 CGBuilderTy &Builder = CGF.Builder; 2416 2417 // Branch past the conversion if Src is null. 2418 llvm::Value *IsNotNull = EmitMemberPointerIsNotNull(CGF, Src, SrcTy); 2419 llvm::Constant *DstNull = EmitNullMemberPointer(DstTy); 2420 2421 // C++ 5.2.10p9: The null member pointer value is converted to the null member 2422 // pointer value of the destination type. 2423 if (IsReinterpret) { 2424 // For reinterpret casts, sema ensures that src and dst are both functions 2425 // or data and have the same size, which means the LLVM types should match. 2426 assert(Src->getType() == DstNull->getType()); 2427 return Builder.CreateSelect(IsNotNull, Src, DstNull); 2428 } 2429 2430 llvm::BasicBlock *OriginalBB = Builder.GetInsertBlock(); 2431 llvm::BasicBlock *ConvertBB = CGF.createBasicBlock("memptr.convert"); 2432 llvm::BasicBlock *ContinueBB = CGF.createBasicBlock("memptr.converted"); 2433 Builder.CreateCondBr(IsNotNull, ConvertBB, ContinueBB); 2434 CGF.EmitBlock(ConvertBB); 2435 2436 // Decompose src. 2437 llvm::Value *FirstField = Src; 2438 llvm::Value *NonVirtualBaseAdjustment = nullptr; 2439 llvm::Value *VirtualBaseAdjustmentOffset = nullptr; 2440 llvm::Value *VBPtrOffset = nullptr; 2441 MSInheritanceAttr::Spelling SrcInheritance = SrcRD->getMSInheritanceModel(); 2442 if (!MSInheritanceAttr::hasOnlyOneField(IsFunc, SrcInheritance)) { 2443 // We need to extract values. 2444 unsigned I = 0; 2445 FirstField = Builder.CreateExtractValue(Src, I++); 2446 if (MSInheritanceAttr::hasNVOffsetField(IsFunc, SrcInheritance)) 2447 NonVirtualBaseAdjustment = Builder.CreateExtractValue(Src, I++); 2448 if (MSInheritanceAttr::hasVBPtrOffsetField(SrcInheritance)) 2449 VBPtrOffset = Builder.CreateExtractValue(Src, I++); 2450 if (MSInheritanceAttr::hasVBTableOffsetField(SrcInheritance)) 2451 VirtualBaseAdjustmentOffset = Builder.CreateExtractValue(Src, I++); 2452 } 2453 2454 // For data pointers, we adjust the field offset directly. For functions, we 2455 // have a separate field. 2456 llvm::Constant *Adj = getMemberPointerAdjustment(E); 2457 if (Adj) { 2458 Adj = llvm::ConstantExpr::getTruncOrBitCast(Adj, CGM.IntTy); 2459 llvm::Value *&NVAdjustField = IsFunc ? NonVirtualBaseAdjustment : FirstField; 2460 bool isDerivedToBase = (E->getCastKind() == CK_DerivedToBaseMemberPointer); 2461 if (!NVAdjustField) // If this field didn't exist in src, it's zero. 2462 NVAdjustField = getZeroInt(); 2463 if (isDerivedToBase) 2464 NVAdjustField = Builder.CreateNSWSub(NVAdjustField, Adj, "adj"); 2465 else 2466 NVAdjustField = Builder.CreateNSWAdd(NVAdjustField, Adj, "adj"); 2467 } 2468 2469 // FIXME PR15713: Support conversions through virtually derived classes. 2470 2471 // Recompose dst from the null struct and the adjusted fields from src. 2472 MSInheritanceAttr::Spelling DstInheritance = DstRD->getMSInheritanceModel(); 2473 llvm::Value *Dst; 2474 if (MSInheritanceAttr::hasOnlyOneField(IsFunc, DstInheritance)) { 2475 Dst = FirstField; 2476 } else { 2477 Dst = llvm::UndefValue::get(DstNull->getType()); 2478 unsigned Idx = 0; 2479 Dst = Builder.CreateInsertValue(Dst, FirstField, Idx++); 2480 if (MSInheritanceAttr::hasNVOffsetField(IsFunc, DstInheritance)) 2481 Dst = Builder.CreateInsertValue( 2482 Dst, getValueOrZeroInt(NonVirtualBaseAdjustment), Idx++); 2483 if (MSInheritanceAttr::hasVBPtrOffsetField(DstInheritance)) 2484 Dst = Builder.CreateInsertValue( 2485 Dst, getValueOrZeroInt(VBPtrOffset), Idx++); 2486 if (MSInheritanceAttr::hasVBTableOffsetField(DstInheritance)) 2487 Dst = Builder.CreateInsertValue( 2488 Dst, getValueOrZeroInt(VirtualBaseAdjustmentOffset), Idx++); 2489 } 2490 Builder.CreateBr(ContinueBB); 2491 2492 // In the continuation, choose between DstNull and Dst. 2493 CGF.EmitBlock(ContinueBB); 2494 llvm::PHINode *Phi = Builder.CreatePHI(DstNull->getType(), 2, "memptr.converted"); 2495 Phi->addIncoming(DstNull, OriginalBB); 2496 Phi->addIncoming(Dst, ConvertBB); 2497 return Phi; 2498 } 2499 2500 llvm::Constant * 2501 MicrosoftCXXABI::EmitMemberPointerConversion(const CastExpr *E, 2502 llvm::Constant *Src) { 2503 const MemberPointerType *SrcTy = 2504 E->getSubExpr()->getType()->castAs<MemberPointerType>(); 2505 const MemberPointerType *DstTy = E->getType()->castAs<MemberPointerType>(); 2506 2507 // If src is null, emit a new null for dst. We can't return src because dst 2508 // might have a new representation. 2509 if (MemberPointerConstantIsNull(SrcTy, Src)) 2510 return EmitNullMemberPointer(DstTy); 2511 2512 // We don't need to do anything for reinterpret_casts of non-null member 2513 // pointers. We should only get here when the two type representations have 2514 // the same size. 2515 if (E->getCastKind() == CK_ReinterpretMemberPointer) 2516 return Src; 2517 2518 MSInheritanceAttr::Spelling SrcInheritance = getInheritanceFromMemptr(SrcTy); 2519 MSInheritanceAttr::Spelling DstInheritance = getInheritanceFromMemptr(DstTy); 2520 2521 // Decompose src. 2522 llvm::Constant *FirstField = Src; 2523 llvm::Constant *NonVirtualBaseAdjustment = nullptr; 2524 llvm::Constant *VirtualBaseAdjustmentOffset = nullptr; 2525 llvm::Constant *VBPtrOffset = nullptr; 2526 bool IsFunc = SrcTy->isMemberFunctionPointer(); 2527 if (!MSInheritanceAttr::hasOnlyOneField(IsFunc, SrcInheritance)) { 2528 // We need to extract values. 2529 unsigned I = 0; 2530 FirstField = Src->getAggregateElement(I++); 2531 if (MSInheritanceAttr::hasNVOffsetField(IsFunc, SrcInheritance)) 2532 NonVirtualBaseAdjustment = Src->getAggregateElement(I++); 2533 if (MSInheritanceAttr::hasVBPtrOffsetField(SrcInheritance)) 2534 VBPtrOffset = Src->getAggregateElement(I++); 2535 if (MSInheritanceAttr::hasVBTableOffsetField(SrcInheritance)) 2536 VirtualBaseAdjustmentOffset = Src->getAggregateElement(I++); 2537 } 2538 2539 // For data pointers, we adjust the field offset directly. For functions, we 2540 // have a separate field. 2541 llvm::Constant *Adj = getMemberPointerAdjustment(E); 2542 if (Adj) { 2543 Adj = llvm::ConstantExpr::getTruncOrBitCast(Adj, CGM.IntTy); 2544 llvm::Constant *&NVAdjustField = 2545 IsFunc ? NonVirtualBaseAdjustment : FirstField; 2546 bool IsDerivedToBase = (E->getCastKind() == CK_DerivedToBaseMemberPointer); 2547 if (!NVAdjustField) // If this field didn't exist in src, it's zero. 2548 NVAdjustField = getZeroInt(); 2549 if (IsDerivedToBase) 2550 NVAdjustField = llvm::ConstantExpr::getNSWSub(NVAdjustField, Adj); 2551 else 2552 NVAdjustField = llvm::ConstantExpr::getNSWAdd(NVAdjustField, Adj); 2553 } 2554 2555 // FIXME PR15713: Support conversions through virtually derived classes. 2556 2557 // Recompose dst from the null struct and the adjusted fields from src. 2558 if (MSInheritanceAttr::hasOnlyOneField(IsFunc, DstInheritance)) 2559 return FirstField; 2560 2561 llvm::SmallVector<llvm::Constant *, 4> Fields; 2562 Fields.push_back(FirstField); 2563 if (MSInheritanceAttr::hasNVOffsetField(IsFunc, DstInheritance)) 2564 Fields.push_back(getConstantOrZeroInt(NonVirtualBaseAdjustment)); 2565 if (MSInheritanceAttr::hasVBPtrOffsetField(DstInheritance)) 2566 Fields.push_back(getConstantOrZeroInt(VBPtrOffset)); 2567 if (MSInheritanceAttr::hasVBTableOffsetField(DstInheritance)) 2568 Fields.push_back(getConstantOrZeroInt(VirtualBaseAdjustmentOffset)); 2569 return llvm::ConstantStruct::getAnon(Fields); 2570 } 2571 2572 llvm::Value *MicrosoftCXXABI::EmitLoadOfMemberFunctionPointer( 2573 CodeGenFunction &CGF, const Expr *E, llvm::Value *&This, 2574 llvm::Value *MemPtr, const MemberPointerType *MPT) { 2575 assert(MPT->isMemberFunctionPointer()); 2576 const FunctionProtoType *FPT = 2577 MPT->getPointeeType()->castAs<FunctionProtoType>(); 2578 const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl(); 2579 llvm::FunctionType *FTy = 2580 CGM.getTypes().GetFunctionType( 2581 CGM.getTypes().arrangeCXXMethodType(RD, FPT)); 2582 CGBuilderTy &Builder = CGF.Builder; 2583 2584 MSInheritanceAttr::Spelling Inheritance = RD->getMSInheritanceModel(); 2585 2586 // Extract the fields we need, regardless of model. We'll apply them if we 2587 // have them. 2588 llvm::Value *FunctionPointer = MemPtr; 2589 llvm::Value *NonVirtualBaseAdjustment = nullptr; 2590 llvm::Value *VirtualBaseAdjustmentOffset = nullptr; 2591 llvm::Value *VBPtrOffset = nullptr; 2592 if (MemPtr->getType()->isStructTy()) { 2593 // We need to extract values. 2594 unsigned I = 0; 2595 FunctionPointer = Builder.CreateExtractValue(MemPtr, I++); 2596 if (MSInheritanceAttr::hasNVOffsetField(MPT, Inheritance)) 2597 NonVirtualBaseAdjustment = Builder.CreateExtractValue(MemPtr, I++); 2598 if (MSInheritanceAttr::hasVBPtrOffsetField(Inheritance)) 2599 VBPtrOffset = Builder.CreateExtractValue(MemPtr, I++); 2600 if (MSInheritanceAttr::hasVBTableOffsetField(Inheritance)) 2601 VirtualBaseAdjustmentOffset = Builder.CreateExtractValue(MemPtr, I++); 2602 } 2603 2604 if (VirtualBaseAdjustmentOffset) { 2605 This = AdjustVirtualBase(CGF, E, RD, This, VirtualBaseAdjustmentOffset, 2606 VBPtrOffset); 2607 } 2608 2609 if (NonVirtualBaseAdjustment) { 2610 // Apply the adjustment and cast back to the original struct type. 2611 llvm::Value *Ptr = Builder.CreateBitCast(This, Builder.getInt8PtrTy()); 2612 Ptr = Builder.CreateInBoundsGEP(Ptr, NonVirtualBaseAdjustment); 2613 This = Builder.CreateBitCast(Ptr, This->getType(), "this.adjusted"); 2614 } 2615 2616 return Builder.CreateBitCast(FunctionPointer, FTy->getPointerTo()); 2617 } 2618 2619 CGCXXABI *clang::CodeGen::CreateMicrosoftCXXABI(CodeGenModule &CGM) { 2620 return new MicrosoftCXXABI(CGM); 2621 } 2622 2623 // MS RTTI Overview: 2624 // The run time type information emitted by cl.exe contains 5 distinct types of 2625 // structures. Many of them reference each other. 2626 // 2627 // TypeInfo: Static classes that are returned by typeid. 2628 // 2629 // CompleteObjectLocator: Referenced by vftables. They contain information 2630 // required for dynamic casting, including OffsetFromTop. They also contain 2631 // a reference to the TypeInfo for the type and a reference to the 2632 // CompleteHierarchyDescriptor for the type. 2633 // 2634 // ClassHieararchyDescriptor: Contains information about a class hierarchy. 2635 // Used during dynamic_cast to walk a class hierarchy. References a base 2636 // class array and the size of said array. 2637 // 2638 // BaseClassArray: Contains a list of classes in a hierarchy. BaseClassArray is 2639 // somewhat of a misnomer because the most derived class is also in the list 2640 // as well as multiple copies of virtual bases (if they occur multiple times 2641 // in the hiearchy.) The BaseClassArray contains one BaseClassDescriptor for 2642 // every path in the hierarchy, in pre-order depth first order. Note, we do 2643 // not declare a specific llvm type for BaseClassArray, it's merely an array 2644 // of BaseClassDescriptor pointers. 2645 // 2646 // BaseClassDescriptor: Contains information about a class in a class hierarchy. 2647 // BaseClassDescriptor is also somewhat of a misnomer for the same reason that 2648 // BaseClassArray is. It contains information about a class within a 2649 // hierarchy such as: is this base is ambiguous and what is its offset in the 2650 // vbtable. The names of the BaseClassDescriptors have all of their fields 2651 // mangled into them so they can be aggressively deduplicated by the linker. 2652 2653 static llvm::GlobalVariable *getTypeInfoVTable(CodeGenModule &CGM) { 2654 StringRef MangledName("\01??_7type_info@@6B@"); 2655 if (auto VTable = CGM.getModule().getNamedGlobal(MangledName)) 2656 return VTable; 2657 return new llvm::GlobalVariable(CGM.getModule(), CGM.Int8PtrTy, 2658 /*Constant=*/true, 2659 llvm::GlobalVariable::ExternalLinkage, 2660 /*Initializer=*/nullptr, MangledName); 2661 } 2662 2663 namespace { 2664 2665 /// \brief A Helper struct that stores information about a class in a class 2666 /// hierarchy. The information stored in these structs struct is used during 2667 /// the generation of ClassHierarchyDescriptors and BaseClassDescriptors. 2668 // During RTTI creation, MSRTTIClasses are stored in a contiguous array with 2669 // implicit depth first pre-order tree connectivity. getFirstChild and 2670 // getNextSibling allow us to walk the tree efficiently. 2671 struct MSRTTIClass { 2672 enum { 2673 IsPrivateOnPath = 1 | 8, 2674 IsAmbiguous = 2, 2675 IsPrivate = 4, 2676 IsVirtual = 16, 2677 HasHierarchyDescriptor = 64 2678 }; 2679 MSRTTIClass(const CXXRecordDecl *RD) : RD(RD) {} 2680 uint32_t initialize(const MSRTTIClass *Parent, 2681 const CXXBaseSpecifier *Specifier); 2682 2683 MSRTTIClass *getFirstChild() { return this + 1; } 2684 static MSRTTIClass *getNextChild(MSRTTIClass *Child) { 2685 return Child + 1 + Child->NumBases; 2686 } 2687 2688 const CXXRecordDecl *RD, *VirtualRoot; 2689 uint32_t Flags, NumBases, OffsetInVBase; 2690 }; 2691 2692 /// \brief Recursively initialize the base class array. 2693 uint32_t MSRTTIClass::initialize(const MSRTTIClass *Parent, 2694 const CXXBaseSpecifier *Specifier) { 2695 Flags = HasHierarchyDescriptor; 2696 if (!Parent) { 2697 VirtualRoot = nullptr; 2698 OffsetInVBase = 0; 2699 } else { 2700 if (Specifier->getAccessSpecifier() != AS_public) 2701 Flags |= IsPrivate | IsPrivateOnPath; 2702 if (Specifier->isVirtual()) { 2703 Flags |= IsVirtual; 2704 VirtualRoot = RD; 2705 OffsetInVBase = 0; 2706 } else { 2707 if (Parent->Flags & IsPrivateOnPath) 2708 Flags |= IsPrivateOnPath; 2709 VirtualRoot = Parent->VirtualRoot; 2710 OffsetInVBase = Parent->OffsetInVBase + RD->getASTContext() 2711 .getASTRecordLayout(Parent->RD).getBaseClassOffset(RD).getQuantity(); 2712 } 2713 } 2714 NumBases = 0; 2715 MSRTTIClass *Child = getFirstChild(); 2716 for (const CXXBaseSpecifier &Base : RD->bases()) { 2717 NumBases += Child->initialize(this, &Base) + 1; 2718 Child = getNextChild(Child); 2719 } 2720 return NumBases; 2721 } 2722 2723 static llvm::GlobalValue::LinkageTypes getLinkageForRTTI(QualType Ty) { 2724 switch (Ty->getLinkage()) { 2725 case NoLinkage: 2726 case InternalLinkage: 2727 case UniqueExternalLinkage: 2728 return llvm::GlobalValue::InternalLinkage; 2729 2730 case VisibleNoLinkage: 2731 case ExternalLinkage: 2732 return llvm::GlobalValue::LinkOnceODRLinkage; 2733 } 2734 llvm_unreachable("Invalid linkage!"); 2735 } 2736 2737 /// \brief An ephemeral helper class for building MS RTTI types. It caches some 2738 /// calls to the module and information about the most derived class in a 2739 /// hierarchy. 2740 struct MSRTTIBuilder { 2741 enum { 2742 HasBranchingHierarchy = 1, 2743 HasVirtualBranchingHierarchy = 2, 2744 HasAmbiguousBases = 4 2745 }; 2746 2747 MSRTTIBuilder(MicrosoftCXXABI &ABI, const CXXRecordDecl *RD) 2748 : CGM(ABI.CGM), Context(CGM.getContext()), 2749 VMContext(CGM.getLLVMContext()), Module(CGM.getModule()), RD(RD), 2750 Linkage(getLinkageForRTTI(CGM.getContext().getTagDeclType(RD))), 2751 ABI(ABI) {} 2752 2753 llvm::GlobalVariable *getBaseClassDescriptor(const MSRTTIClass &Classes); 2754 llvm::GlobalVariable * 2755 getBaseClassArray(SmallVectorImpl<MSRTTIClass> &Classes); 2756 llvm::GlobalVariable *getClassHierarchyDescriptor(); 2757 llvm::GlobalVariable *getCompleteObjectLocator(const VPtrInfo *Info); 2758 2759 CodeGenModule &CGM; 2760 ASTContext &Context; 2761 llvm::LLVMContext &VMContext; 2762 llvm::Module &Module; 2763 const CXXRecordDecl *RD; 2764 llvm::GlobalVariable::LinkageTypes Linkage; 2765 MicrosoftCXXABI &ABI; 2766 }; 2767 2768 } // namespace 2769 2770 /// \brief Recursively serializes a class hierarchy in pre-order depth first 2771 /// order. 2772 static void serializeClassHierarchy(SmallVectorImpl<MSRTTIClass> &Classes, 2773 const CXXRecordDecl *RD) { 2774 Classes.push_back(MSRTTIClass(RD)); 2775 for (const CXXBaseSpecifier &Base : RD->bases()) 2776 serializeClassHierarchy(Classes, Base.getType()->getAsCXXRecordDecl()); 2777 } 2778 2779 /// \brief Find ambiguity among base classes. 2780 static void 2781 detectAmbiguousBases(SmallVectorImpl<MSRTTIClass> &Classes) { 2782 llvm::SmallPtrSet<const CXXRecordDecl *, 8> VirtualBases; 2783 llvm::SmallPtrSet<const CXXRecordDecl *, 8> UniqueBases; 2784 llvm::SmallPtrSet<const CXXRecordDecl *, 8> AmbiguousBases; 2785 for (MSRTTIClass *Class = &Classes.front(); Class <= &Classes.back();) { 2786 if ((Class->Flags & MSRTTIClass::IsVirtual) && 2787 !VirtualBases.insert(Class->RD).second) { 2788 Class = MSRTTIClass::getNextChild(Class); 2789 continue; 2790 } 2791 if (!UniqueBases.insert(Class->RD).second) 2792 AmbiguousBases.insert(Class->RD); 2793 Class++; 2794 } 2795 if (AmbiguousBases.empty()) 2796 return; 2797 for (MSRTTIClass &Class : Classes) 2798 if (AmbiguousBases.count(Class.RD)) 2799 Class.Flags |= MSRTTIClass::IsAmbiguous; 2800 } 2801 2802 llvm::GlobalVariable *MSRTTIBuilder::getClassHierarchyDescriptor() { 2803 SmallString<256> MangledName; 2804 { 2805 llvm::raw_svector_ostream Out(MangledName); 2806 ABI.getMangleContext().mangleCXXRTTIClassHierarchyDescriptor(RD, Out); 2807 } 2808 2809 // Check to see if we've already declared this ClassHierarchyDescriptor. 2810 if (auto CHD = Module.getNamedGlobal(MangledName)) 2811 return CHD; 2812 2813 // Serialize the class hierarchy and initialize the CHD Fields. 2814 SmallVector<MSRTTIClass, 8> Classes; 2815 serializeClassHierarchy(Classes, RD); 2816 Classes.front().initialize(/*Parent=*/nullptr, /*Specifier=*/nullptr); 2817 detectAmbiguousBases(Classes); 2818 int Flags = 0; 2819 for (auto Class : Classes) { 2820 if (Class.RD->getNumBases() > 1) 2821 Flags |= HasBranchingHierarchy; 2822 // Note: cl.exe does not calculate "HasAmbiguousBases" correctly. We 2823 // believe the field isn't actually used. 2824 if (Class.Flags & MSRTTIClass::IsAmbiguous) 2825 Flags |= HasAmbiguousBases; 2826 } 2827 if ((Flags & HasBranchingHierarchy) && RD->getNumVBases() != 0) 2828 Flags |= HasVirtualBranchingHierarchy; 2829 // These gep indices are used to get the address of the first element of the 2830 // base class array. 2831 llvm::Value *GEPIndices[] = {llvm::ConstantInt::get(CGM.IntTy, 0), 2832 llvm::ConstantInt::get(CGM.IntTy, 0)}; 2833 2834 // Forward-declare the class hierarchy descriptor 2835 auto Type = ABI.getClassHierarchyDescriptorType(); 2836 auto CHD = new llvm::GlobalVariable(Module, Type, /*Constant=*/true, Linkage, 2837 /*Initializer=*/nullptr, 2838 MangledName.c_str()); 2839 2840 // Initialize the base class ClassHierarchyDescriptor. 2841 llvm::Constant *Fields[] = { 2842 llvm::ConstantInt::get(CGM.IntTy, 0), // Unknown 2843 llvm::ConstantInt::get(CGM.IntTy, Flags), 2844 llvm::ConstantInt::get(CGM.IntTy, Classes.size()), 2845 ABI.getImageRelativeConstant(llvm::ConstantExpr::getInBoundsGetElementPtr( 2846 getBaseClassArray(Classes), 2847 llvm::ArrayRef<llvm::Value *>(GEPIndices))), 2848 }; 2849 CHD->setInitializer(llvm::ConstantStruct::get(Type, Fields)); 2850 return CHD; 2851 } 2852 2853 llvm::GlobalVariable * 2854 MSRTTIBuilder::getBaseClassArray(SmallVectorImpl<MSRTTIClass> &Classes) { 2855 SmallString<256> MangledName; 2856 { 2857 llvm::raw_svector_ostream Out(MangledName); 2858 ABI.getMangleContext().mangleCXXRTTIBaseClassArray(RD, Out); 2859 } 2860 2861 // Forward-declare the base class array. 2862 // cl.exe pads the base class array with 1 (in 32 bit mode) or 4 (in 64 bit 2863 // mode) bytes of padding. We provide a pointer sized amount of padding by 2864 // adding +1 to Classes.size(). The sections have pointer alignment and are 2865 // marked pick-any so it shouldn't matter. 2866 llvm::Type *PtrType = ABI.getImageRelativeType( 2867 ABI.getBaseClassDescriptorType()->getPointerTo()); 2868 auto *ArrType = llvm::ArrayType::get(PtrType, Classes.size() + 1); 2869 auto *BCA = new llvm::GlobalVariable( 2870 Module, ArrType, 2871 /*Constant=*/true, Linkage, /*Initializer=*/nullptr, MangledName.c_str()); 2872 2873 // Initialize the BaseClassArray. 2874 SmallVector<llvm::Constant *, 8> BaseClassArrayData; 2875 for (MSRTTIClass &Class : Classes) 2876 BaseClassArrayData.push_back( 2877 ABI.getImageRelativeConstant(getBaseClassDescriptor(Class))); 2878 BaseClassArrayData.push_back(llvm::Constant::getNullValue(PtrType)); 2879 BCA->setInitializer(llvm::ConstantArray::get(ArrType, BaseClassArrayData)); 2880 return BCA; 2881 } 2882 2883 llvm::GlobalVariable * 2884 MSRTTIBuilder::getBaseClassDescriptor(const MSRTTIClass &Class) { 2885 // Compute the fields for the BaseClassDescriptor. They are computed up front 2886 // because they are mangled into the name of the object. 2887 uint32_t OffsetInVBTable = 0; 2888 int32_t VBPtrOffset = -1; 2889 if (Class.VirtualRoot) { 2890 auto &VTableContext = CGM.getMicrosoftVTableContext(); 2891 OffsetInVBTable = VTableContext.getVBTableIndex(RD, Class.VirtualRoot) * 4; 2892 VBPtrOffset = Context.getASTRecordLayout(RD).getVBPtrOffset().getQuantity(); 2893 } 2894 2895 SmallString<256> MangledName; 2896 { 2897 llvm::raw_svector_ostream Out(MangledName); 2898 ABI.getMangleContext().mangleCXXRTTIBaseClassDescriptor( 2899 Class.RD, Class.OffsetInVBase, VBPtrOffset, OffsetInVBTable, 2900 Class.Flags, Out); 2901 } 2902 2903 // Check to see if we've already declared this object. 2904 if (auto BCD = Module.getNamedGlobal(MangledName)) 2905 return BCD; 2906 2907 // Forward-declare the base class descriptor. 2908 auto Type = ABI.getBaseClassDescriptorType(); 2909 auto BCD = new llvm::GlobalVariable(Module, Type, /*Constant=*/true, Linkage, 2910 /*Initializer=*/nullptr, 2911 MangledName.c_str()); 2912 2913 // Initialize the BaseClassDescriptor. 2914 llvm::Constant *Fields[] = { 2915 ABI.getImageRelativeConstant( 2916 ABI.getAddrOfRTTIDescriptor(Context.getTypeDeclType(Class.RD))), 2917 llvm::ConstantInt::get(CGM.IntTy, Class.NumBases), 2918 llvm::ConstantInt::get(CGM.IntTy, Class.OffsetInVBase), 2919 llvm::ConstantInt::get(CGM.IntTy, VBPtrOffset), 2920 llvm::ConstantInt::get(CGM.IntTy, OffsetInVBTable), 2921 llvm::ConstantInt::get(CGM.IntTy, Class.Flags), 2922 ABI.getImageRelativeConstant( 2923 MSRTTIBuilder(ABI, Class.RD).getClassHierarchyDescriptor()), 2924 }; 2925 BCD->setInitializer(llvm::ConstantStruct::get(Type, Fields)); 2926 return BCD; 2927 } 2928 2929 llvm::GlobalVariable * 2930 MSRTTIBuilder::getCompleteObjectLocator(const VPtrInfo *Info) { 2931 SmallString<256> MangledName; 2932 { 2933 llvm::raw_svector_ostream Out(MangledName); 2934 ABI.getMangleContext().mangleCXXRTTICompleteObjectLocator(RD, Info->MangledPath, Out); 2935 } 2936 2937 // Check to see if we've already computed this complete object locator. 2938 if (auto COL = Module.getNamedGlobal(MangledName)) 2939 return COL; 2940 2941 // Compute the fields of the complete object locator. 2942 int OffsetToTop = Info->FullOffsetInMDC.getQuantity(); 2943 int VFPtrOffset = 0; 2944 // The offset includes the vtordisp if one exists. 2945 if (const CXXRecordDecl *VBase = Info->getVBaseWithVPtr()) 2946 if (Context.getASTRecordLayout(RD) 2947 .getVBaseOffsetsMap() 2948 .find(VBase) 2949 ->second.hasVtorDisp()) 2950 VFPtrOffset = Info->NonVirtualOffset.getQuantity() + 4; 2951 2952 // Forward-declare the complete object locator. 2953 llvm::StructType *Type = ABI.getCompleteObjectLocatorType(); 2954 auto COL = new llvm::GlobalVariable(Module, Type, /*Constant=*/true, Linkage, 2955 /*Initializer=*/nullptr, MangledName.c_str()); 2956 2957 // Initialize the CompleteObjectLocator. 2958 llvm::Constant *Fields[] = { 2959 llvm::ConstantInt::get(CGM.IntTy, ABI.isImageRelative()), 2960 llvm::ConstantInt::get(CGM.IntTy, OffsetToTop), 2961 llvm::ConstantInt::get(CGM.IntTy, VFPtrOffset), 2962 ABI.getImageRelativeConstant( 2963 CGM.GetAddrOfRTTIDescriptor(Context.getTypeDeclType(RD))), 2964 ABI.getImageRelativeConstant(getClassHierarchyDescriptor()), 2965 ABI.getImageRelativeConstant(COL), 2966 }; 2967 llvm::ArrayRef<llvm::Constant *> FieldsRef(Fields); 2968 if (!ABI.isImageRelative()) 2969 FieldsRef = FieldsRef.drop_back(); 2970 COL->setInitializer(llvm::ConstantStruct::get(Type, FieldsRef)); 2971 return COL; 2972 } 2973 2974 /// \brief Gets a TypeDescriptor. Returns a llvm::Constant * rather than a 2975 /// llvm::GlobalVariable * because different type descriptors have different 2976 /// types, and need to be abstracted. They are abstracting by casting the 2977 /// address to an Int8PtrTy. 2978 llvm::Constant *MicrosoftCXXABI::getAddrOfRTTIDescriptor(QualType Type) { 2979 SmallString<256> MangledName, TypeInfoString; 2980 { 2981 llvm::raw_svector_ostream Out(MangledName); 2982 getMangleContext().mangleCXXRTTI(Type, Out); 2983 } 2984 2985 // Check to see if we've already declared this TypeDescriptor. 2986 if (llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(MangledName)) 2987 return llvm::ConstantExpr::getBitCast(GV, CGM.Int8PtrTy); 2988 2989 // Compute the fields for the TypeDescriptor. 2990 { 2991 llvm::raw_svector_ostream Out(TypeInfoString); 2992 getMangleContext().mangleCXXRTTIName(Type, Out); 2993 } 2994 2995 // Declare and initialize the TypeDescriptor. 2996 llvm::Constant *Fields[] = { 2997 getTypeInfoVTable(CGM), // VFPtr 2998 llvm::ConstantPointerNull::get(CGM.Int8PtrTy), // Runtime data 2999 llvm::ConstantDataArray::getString(CGM.getLLVMContext(), TypeInfoString)}; 3000 llvm::StructType *TypeDescriptorType = 3001 getTypeDescriptorType(TypeInfoString); 3002 return llvm::ConstantExpr::getBitCast( 3003 new llvm::GlobalVariable( 3004 CGM.getModule(), TypeDescriptorType, /*Constant=*/false, 3005 getLinkageForRTTI(Type), 3006 llvm::ConstantStruct::get(TypeDescriptorType, Fields), 3007 MangledName.c_str()), 3008 CGM.Int8PtrTy); 3009 } 3010 3011 /// \brief Gets or a creates a Microsoft CompleteObjectLocator. 3012 llvm::GlobalVariable * 3013 MicrosoftCXXABI::getMSCompleteObjectLocator(const CXXRecordDecl *RD, 3014 const VPtrInfo *Info) { 3015 return MSRTTIBuilder(*this, RD).getCompleteObjectLocator(Info); 3016 } 3017 3018 static void emitCXXConstructor(CodeGenModule &CGM, 3019 const CXXConstructorDecl *ctor, 3020 StructorType ctorType) { 3021 // There are no constructor variants, always emit the complete destructor. 3022 CGM.codegenCXXStructor(ctor, StructorType::Complete); 3023 } 3024 3025 static void emitCXXDestructor(CodeGenModule &CGM, const CXXDestructorDecl *dtor, 3026 StructorType dtorType) { 3027 // The complete destructor is equivalent to the base destructor for 3028 // classes with no virtual bases, so try to emit it as an alias. 3029 if (!dtor->getParent()->getNumVBases() && 3030 (dtorType == StructorType::Complete || dtorType == StructorType::Base)) { 3031 bool ProducedAlias = !CGM.TryEmitDefinitionAsAlias( 3032 GlobalDecl(dtor, Dtor_Complete), GlobalDecl(dtor, Dtor_Base), true); 3033 if (ProducedAlias) { 3034 if (dtorType == StructorType::Complete) 3035 return; 3036 if (dtor->isVirtual()) 3037 CGM.getVTables().EmitThunks(GlobalDecl(dtor, Dtor_Complete)); 3038 } 3039 } 3040 3041 // The base destructor is equivalent to the base destructor of its 3042 // base class if there is exactly one non-virtual base class with a 3043 // non-trivial destructor, there are no fields with a non-trivial 3044 // destructor, and the body of the destructor is trivial. 3045 if (dtorType == StructorType::Base && !CGM.TryEmitBaseDestructorAsAlias(dtor)) 3046 return; 3047 3048 CGM.codegenCXXStructor(dtor, dtorType); 3049 } 3050 3051 void MicrosoftCXXABI::emitCXXStructor(const CXXMethodDecl *MD, 3052 StructorType Type) { 3053 if (auto *CD = dyn_cast<CXXConstructorDecl>(MD)) { 3054 emitCXXConstructor(CGM, CD, Type); 3055 return; 3056 } 3057 emitCXXDestructor(CGM, cast<CXXDestructorDecl>(MD), Type); 3058 } 3059