1 //===--- MicrosoftCXXABI.cpp - Emit LLVM Code from ASTs for a Module ------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This provides C++ code generation targeting the Microsoft Visual C++ ABI. 10 // The class in this file generates structures that follow the Microsoft 11 // Visual C++ ABI, which is actually not very well documented at all outside 12 // of Microsoft. 13 // 14 //===----------------------------------------------------------------------===// 15 16 #include "CGCXXABI.h" 17 #include "CGCleanup.h" 18 #include "CGVTables.h" 19 #include "CodeGenModule.h" 20 #include "CodeGenTypes.h" 21 #include "TargetInfo.h" 22 #include "clang/CodeGen/ConstantInitBuilder.h" 23 #include "clang/AST/Decl.h" 24 #include "clang/AST/DeclCXX.h" 25 #include "clang/AST/StmtCXX.h" 26 #include "clang/AST/VTableBuilder.h" 27 #include "llvm/ADT/StringExtras.h" 28 #include "llvm/ADT/StringSet.h" 29 #include "llvm/IR/Intrinsics.h" 30 31 using namespace clang; 32 using namespace CodeGen; 33 34 namespace { 35 36 /// Holds all the vbtable globals for a given class. 37 struct VBTableGlobals { 38 const VPtrInfoVector *VBTables; 39 SmallVector<llvm::GlobalVariable *, 2> Globals; 40 }; 41 42 class MicrosoftCXXABI : public CGCXXABI { 43 public: 44 MicrosoftCXXABI(CodeGenModule &CGM) 45 : CGCXXABI(CGM), BaseClassDescriptorType(nullptr), 46 ClassHierarchyDescriptorType(nullptr), 47 CompleteObjectLocatorType(nullptr), CatchableTypeType(nullptr), 48 ThrowInfoType(nullptr) {} 49 50 bool HasThisReturn(GlobalDecl GD) const override; 51 bool hasMostDerivedReturn(GlobalDecl GD) const override; 52 53 bool classifyReturnType(CGFunctionInfo &FI) const override; 54 55 RecordArgABI getRecordArgABI(const CXXRecordDecl *RD) const override; 56 57 bool isSRetParameterAfterThis() const override { return true; } 58 59 bool isThisCompleteObject(GlobalDecl GD) const override { 60 // The Microsoft ABI doesn't use separate complete-object vs. 61 // base-object variants of constructors, but it does of destructors. 62 if (isa<CXXDestructorDecl>(GD.getDecl())) { 63 switch (GD.getDtorType()) { 64 case Dtor_Complete: 65 case Dtor_Deleting: 66 return true; 67 68 case Dtor_Base: 69 return false; 70 71 case Dtor_Comdat: llvm_unreachable("emitting dtor comdat as function?"); 72 } 73 llvm_unreachable("bad dtor kind"); 74 } 75 76 // No other kinds. 77 return false; 78 } 79 80 size_t getSrcArgforCopyCtor(const CXXConstructorDecl *CD, 81 FunctionArgList &Args) const override { 82 assert(Args.size() >= 2 && 83 "expected the arglist to have at least two args!"); 84 // The 'most_derived' parameter goes second if the ctor is variadic and 85 // has v-bases. 86 if (CD->getParent()->getNumVBases() > 0 && 87 CD->getType()->castAs<FunctionProtoType>()->isVariadic()) 88 return 2; 89 return 1; 90 } 91 92 std::vector<CharUnits> getVBPtrOffsets(const CXXRecordDecl *RD) override { 93 std::vector<CharUnits> VBPtrOffsets; 94 const ASTContext &Context = getContext(); 95 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); 96 97 const VBTableGlobals &VBGlobals = enumerateVBTables(RD); 98 for (const std::unique_ptr<VPtrInfo> &VBT : *VBGlobals.VBTables) { 99 const ASTRecordLayout &SubobjectLayout = 100 Context.getASTRecordLayout(VBT->IntroducingObject); 101 CharUnits Offs = VBT->NonVirtualOffset; 102 Offs += SubobjectLayout.getVBPtrOffset(); 103 if (VBT->getVBaseWithVPtr()) 104 Offs += Layout.getVBaseClassOffset(VBT->getVBaseWithVPtr()); 105 VBPtrOffsets.push_back(Offs); 106 } 107 llvm::array_pod_sort(VBPtrOffsets.begin(), VBPtrOffsets.end()); 108 return VBPtrOffsets; 109 } 110 111 StringRef GetPureVirtualCallName() override { return "_purecall"; } 112 StringRef GetDeletedVirtualCallName() override { return "_purecall"; } 113 114 void emitVirtualObjectDelete(CodeGenFunction &CGF, const CXXDeleteExpr *DE, 115 Address Ptr, QualType ElementType, 116 const CXXDestructorDecl *Dtor) override; 117 118 void emitRethrow(CodeGenFunction &CGF, bool isNoReturn) override; 119 void emitThrow(CodeGenFunction &CGF, const CXXThrowExpr *E) override; 120 121 void emitBeginCatch(CodeGenFunction &CGF, const CXXCatchStmt *C) override; 122 123 llvm::GlobalVariable *getMSCompleteObjectLocator(const CXXRecordDecl *RD, 124 const VPtrInfo &Info); 125 126 llvm::Constant *getAddrOfRTTIDescriptor(QualType Ty) override; 127 CatchTypeInfo 128 getAddrOfCXXCatchHandlerType(QualType Ty, QualType CatchHandlerType) override; 129 130 /// MSVC needs an extra flag to indicate a catchall. 131 CatchTypeInfo getCatchAllTypeInfo() override { 132 return CatchTypeInfo{nullptr, 0x40}; 133 } 134 135 bool shouldTypeidBeNullChecked(bool IsDeref, QualType SrcRecordTy) override; 136 void EmitBadTypeidCall(CodeGenFunction &CGF) override; 137 llvm::Value *EmitTypeid(CodeGenFunction &CGF, QualType SrcRecordTy, 138 Address ThisPtr, 139 llvm::Type *StdTypeInfoPtrTy) override; 140 141 bool shouldDynamicCastCallBeNullChecked(bool SrcIsPtr, 142 QualType SrcRecordTy) override; 143 144 llvm::Value *EmitDynamicCastCall(CodeGenFunction &CGF, Address Value, 145 QualType SrcRecordTy, QualType DestTy, 146 QualType DestRecordTy, 147 llvm::BasicBlock *CastEnd) override; 148 149 llvm::Value *EmitDynamicCastToVoid(CodeGenFunction &CGF, Address Value, 150 QualType SrcRecordTy, 151 QualType DestTy) override; 152 153 bool EmitBadCastCall(CodeGenFunction &CGF) override; 154 bool canSpeculativelyEmitVTable(const CXXRecordDecl *RD) const override { 155 return false; 156 } 157 158 llvm::Value * 159 GetVirtualBaseClassOffset(CodeGenFunction &CGF, Address This, 160 const CXXRecordDecl *ClassDecl, 161 const CXXRecordDecl *BaseClassDecl) override; 162 163 llvm::BasicBlock * 164 EmitCtorCompleteObjectHandler(CodeGenFunction &CGF, 165 const CXXRecordDecl *RD) override; 166 167 llvm::BasicBlock * 168 EmitDtorCompleteObjectHandler(CodeGenFunction &CGF); 169 170 void initializeHiddenVirtualInheritanceMembers(CodeGenFunction &CGF, 171 const CXXRecordDecl *RD) override; 172 173 void EmitCXXConstructors(const CXXConstructorDecl *D) override; 174 175 // Background on MSVC destructors 176 // ============================== 177 // 178 // Both Itanium and MSVC ABIs have destructor variants. The variant names 179 // roughly correspond in the following way: 180 // Itanium Microsoft 181 // Base -> no name, just ~Class 182 // Complete -> vbase destructor 183 // Deleting -> scalar deleting destructor 184 // vector deleting destructor 185 // 186 // The base and complete destructors are the same as in Itanium, although the 187 // complete destructor does not accept a VTT parameter when there are virtual 188 // bases. A separate mechanism involving vtordisps is used to ensure that 189 // virtual methods of destroyed subobjects are not called. 190 // 191 // The deleting destructors accept an i32 bitfield as a second parameter. Bit 192 // 1 indicates if the memory should be deleted. Bit 2 indicates if the this 193 // pointer points to an array. The scalar deleting destructor assumes that 194 // bit 2 is zero, and therefore does not contain a loop. 195 // 196 // For virtual destructors, only one entry is reserved in the vftable, and it 197 // always points to the vector deleting destructor. The vector deleting 198 // destructor is the most general, so it can be used to destroy objects in 199 // place, delete single heap objects, or delete arrays. 200 // 201 // A TU defining a non-inline destructor is only guaranteed to emit a base 202 // destructor, and all of the other variants are emitted on an as-needed basis 203 // in COMDATs. Because a non-base destructor can be emitted in a TU that 204 // lacks a definition for the destructor, non-base destructors must always 205 // delegate to or alias the base destructor. 206 207 AddedStructorArgs 208 buildStructorSignature(GlobalDecl GD, 209 SmallVectorImpl<CanQualType> &ArgTys) override; 210 211 /// Non-base dtors should be emitted as delegating thunks in this ABI. 212 bool useThunkForDtorVariant(const CXXDestructorDecl *Dtor, 213 CXXDtorType DT) const override { 214 return DT != Dtor_Base; 215 } 216 217 void setCXXDestructorDLLStorage(llvm::GlobalValue *GV, 218 const CXXDestructorDecl *Dtor, 219 CXXDtorType DT) const override; 220 221 llvm::GlobalValue::LinkageTypes 222 getCXXDestructorLinkage(GVALinkage Linkage, const CXXDestructorDecl *Dtor, 223 CXXDtorType DT) const override; 224 225 void EmitCXXDestructors(const CXXDestructorDecl *D) override; 226 227 const CXXRecordDecl * 228 getThisArgumentTypeForMethod(const CXXMethodDecl *MD) override { 229 if (MD->isVirtual() && !isa<CXXDestructorDecl>(MD)) { 230 MethodVFTableLocation ML = 231 CGM.getMicrosoftVTableContext().getMethodVFTableLocation(MD); 232 // The vbases might be ordered differently in the final overrider object 233 // and the complete object, so the "this" argument may sometimes point to 234 // memory that has no particular type (e.g. past the complete object). 235 // In this case, we just use a generic pointer type. 236 // FIXME: might want to have a more precise type in the non-virtual 237 // multiple inheritance case. 238 if (ML.VBase || !ML.VFPtrOffset.isZero()) 239 return nullptr; 240 } 241 return MD->getParent(); 242 } 243 244 Address 245 adjustThisArgumentForVirtualFunctionCall(CodeGenFunction &CGF, GlobalDecl GD, 246 Address This, 247 bool VirtualCall) override; 248 249 void addImplicitStructorParams(CodeGenFunction &CGF, QualType &ResTy, 250 FunctionArgList &Params) override; 251 252 void EmitInstanceFunctionProlog(CodeGenFunction &CGF) override; 253 254 AddedStructorArgs 255 addImplicitConstructorArgs(CodeGenFunction &CGF, const CXXConstructorDecl *D, 256 CXXCtorType Type, bool ForVirtualBase, 257 bool Delegating, CallArgList &Args) override; 258 259 void EmitDestructorCall(CodeGenFunction &CGF, const CXXDestructorDecl *DD, 260 CXXDtorType Type, bool ForVirtualBase, 261 bool Delegating, Address This, 262 QualType ThisTy) override; 263 264 void emitVTableTypeMetadata(const VPtrInfo &Info, const CXXRecordDecl *RD, 265 llvm::GlobalVariable *VTable); 266 267 void emitVTableDefinitions(CodeGenVTables &CGVT, 268 const CXXRecordDecl *RD) override; 269 270 bool isVirtualOffsetNeededForVTableField(CodeGenFunction &CGF, 271 CodeGenFunction::VPtr Vptr) override; 272 273 /// Don't initialize vptrs if dynamic class 274 /// is marked with with the 'novtable' attribute. 275 bool doStructorsInitializeVPtrs(const CXXRecordDecl *VTableClass) override { 276 return !VTableClass->hasAttr<MSNoVTableAttr>(); 277 } 278 279 llvm::Constant * 280 getVTableAddressPoint(BaseSubobject Base, 281 const CXXRecordDecl *VTableClass) override; 282 283 llvm::Value *getVTableAddressPointInStructor( 284 CodeGenFunction &CGF, const CXXRecordDecl *VTableClass, 285 BaseSubobject Base, const CXXRecordDecl *NearestVBase) override; 286 287 llvm::Constant * 288 getVTableAddressPointForConstExpr(BaseSubobject Base, 289 const CXXRecordDecl *VTableClass) override; 290 291 llvm::GlobalVariable *getAddrOfVTable(const CXXRecordDecl *RD, 292 CharUnits VPtrOffset) override; 293 294 CGCallee getVirtualFunctionPointer(CodeGenFunction &CGF, GlobalDecl GD, 295 Address This, llvm::Type *Ty, 296 SourceLocation Loc) override; 297 298 llvm::Value *EmitVirtualDestructorCall(CodeGenFunction &CGF, 299 const CXXDestructorDecl *Dtor, 300 CXXDtorType DtorType, Address This, 301 DeleteOrMemberCallExpr E) override; 302 303 void adjustCallArgsForDestructorThunk(CodeGenFunction &CGF, GlobalDecl GD, 304 CallArgList &CallArgs) override { 305 assert(GD.getDtorType() == Dtor_Deleting && 306 "Only deleting destructor thunks are available in this ABI"); 307 CallArgs.add(RValue::get(getStructorImplicitParamValue(CGF)), 308 getContext().IntTy); 309 } 310 311 void emitVirtualInheritanceTables(const CXXRecordDecl *RD) override; 312 313 llvm::GlobalVariable * 314 getAddrOfVBTable(const VPtrInfo &VBT, const CXXRecordDecl *RD, 315 llvm::GlobalVariable::LinkageTypes Linkage); 316 317 llvm::GlobalVariable * 318 getAddrOfVirtualDisplacementMap(const CXXRecordDecl *SrcRD, 319 const CXXRecordDecl *DstRD) { 320 SmallString<256> OutName; 321 llvm::raw_svector_ostream Out(OutName); 322 getMangleContext().mangleCXXVirtualDisplacementMap(SrcRD, DstRD, Out); 323 StringRef MangledName = OutName.str(); 324 325 if (auto *VDispMap = CGM.getModule().getNamedGlobal(MangledName)) 326 return VDispMap; 327 328 MicrosoftVTableContext &VTContext = CGM.getMicrosoftVTableContext(); 329 unsigned NumEntries = 1 + SrcRD->getNumVBases(); 330 SmallVector<llvm::Constant *, 4> Map(NumEntries, 331 llvm::UndefValue::get(CGM.IntTy)); 332 Map[0] = llvm::ConstantInt::get(CGM.IntTy, 0); 333 bool AnyDifferent = false; 334 for (const auto &I : SrcRD->vbases()) { 335 const CXXRecordDecl *VBase = I.getType()->getAsCXXRecordDecl(); 336 if (!DstRD->isVirtuallyDerivedFrom(VBase)) 337 continue; 338 339 unsigned SrcVBIndex = VTContext.getVBTableIndex(SrcRD, VBase); 340 unsigned DstVBIndex = VTContext.getVBTableIndex(DstRD, VBase); 341 Map[SrcVBIndex] = llvm::ConstantInt::get(CGM.IntTy, DstVBIndex * 4); 342 AnyDifferent |= SrcVBIndex != DstVBIndex; 343 } 344 // This map would be useless, don't use it. 345 if (!AnyDifferent) 346 return nullptr; 347 348 llvm::ArrayType *VDispMapTy = llvm::ArrayType::get(CGM.IntTy, Map.size()); 349 llvm::Constant *Init = llvm::ConstantArray::get(VDispMapTy, Map); 350 llvm::GlobalValue::LinkageTypes Linkage = 351 SrcRD->isExternallyVisible() && DstRD->isExternallyVisible() 352 ? llvm::GlobalValue::LinkOnceODRLinkage 353 : llvm::GlobalValue::InternalLinkage; 354 auto *VDispMap = new llvm::GlobalVariable( 355 CGM.getModule(), VDispMapTy, /*isConstant=*/true, Linkage, 356 /*Initializer=*/Init, MangledName); 357 return VDispMap; 358 } 359 360 void emitVBTableDefinition(const VPtrInfo &VBT, const CXXRecordDecl *RD, 361 llvm::GlobalVariable *GV) const; 362 363 void setThunkLinkage(llvm::Function *Thunk, bool ForVTable, 364 GlobalDecl GD, bool ReturnAdjustment) override { 365 GVALinkage Linkage = 366 getContext().GetGVALinkageForFunction(cast<FunctionDecl>(GD.getDecl())); 367 368 if (Linkage == GVA_Internal) 369 Thunk->setLinkage(llvm::GlobalValue::InternalLinkage); 370 else if (ReturnAdjustment) 371 Thunk->setLinkage(llvm::GlobalValue::WeakODRLinkage); 372 else 373 Thunk->setLinkage(llvm::GlobalValue::LinkOnceODRLinkage); 374 } 375 376 bool exportThunk() override { return false; } 377 378 llvm::Value *performThisAdjustment(CodeGenFunction &CGF, Address This, 379 const ThisAdjustment &TA) override; 380 381 llvm::Value *performReturnAdjustment(CodeGenFunction &CGF, Address Ret, 382 const ReturnAdjustment &RA) override; 383 384 void EmitThreadLocalInitFuncs( 385 CodeGenModule &CGM, ArrayRef<const VarDecl *> CXXThreadLocals, 386 ArrayRef<llvm::Function *> CXXThreadLocalInits, 387 ArrayRef<const VarDecl *> CXXThreadLocalInitVars) override; 388 389 bool usesThreadWrapperFunction(const VarDecl *VD) const override { 390 return false; 391 } 392 LValue EmitThreadLocalVarDeclLValue(CodeGenFunction &CGF, const VarDecl *VD, 393 QualType LValType) override; 394 395 void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D, 396 llvm::GlobalVariable *DeclPtr, 397 bool PerformInit) override; 398 void registerGlobalDtor(CodeGenFunction &CGF, const VarDecl &D, 399 llvm::FunctionCallee Dtor, 400 llvm::Constant *Addr) override; 401 402 // ==== Notes on array cookies ========= 403 // 404 // MSVC seems to only use cookies when the class has a destructor; a 405 // two-argument usual array deallocation function isn't sufficient. 406 // 407 // For example, this code prints "100" and "1": 408 // struct A { 409 // char x; 410 // void *operator new[](size_t sz) { 411 // printf("%u\n", sz); 412 // return malloc(sz); 413 // } 414 // void operator delete[](void *p, size_t sz) { 415 // printf("%u\n", sz); 416 // free(p); 417 // } 418 // }; 419 // int main() { 420 // A *p = new A[100]; 421 // delete[] p; 422 // } 423 // Whereas it prints "104" and "104" if you give A a destructor. 424 425 bool requiresArrayCookie(const CXXDeleteExpr *expr, 426 QualType elementType) override; 427 bool requiresArrayCookie(const CXXNewExpr *expr) override; 428 CharUnits getArrayCookieSizeImpl(QualType type) override; 429 Address InitializeArrayCookie(CodeGenFunction &CGF, 430 Address NewPtr, 431 llvm::Value *NumElements, 432 const CXXNewExpr *expr, 433 QualType ElementType) override; 434 llvm::Value *readArrayCookieImpl(CodeGenFunction &CGF, 435 Address allocPtr, 436 CharUnits cookieSize) override; 437 438 friend struct MSRTTIBuilder; 439 440 bool isImageRelative() const { 441 return CGM.getTarget().getPointerWidth(/*AddrSpace=*/0) == 64; 442 } 443 444 // 5 routines for constructing the llvm types for MS RTTI structs. 445 llvm::StructType *getTypeDescriptorType(StringRef TypeInfoString) { 446 llvm::SmallString<32> TDTypeName("rtti.TypeDescriptor"); 447 TDTypeName += llvm::utostr(TypeInfoString.size()); 448 llvm::StructType *&TypeDescriptorType = 449 TypeDescriptorTypeMap[TypeInfoString.size()]; 450 if (TypeDescriptorType) 451 return TypeDescriptorType; 452 llvm::Type *FieldTypes[] = { 453 CGM.Int8PtrPtrTy, 454 CGM.Int8PtrTy, 455 llvm::ArrayType::get(CGM.Int8Ty, TypeInfoString.size() + 1)}; 456 TypeDescriptorType = 457 llvm::StructType::create(CGM.getLLVMContext(), FieldTypes, TDTypeName); 458 return TypeDescriptorType; 459 } 460 461 llvm::Type *getImageRelativeType(llvm::Type *PtrType) { 462 if (!isImageRelative()) 463 return PtrType; 464 return CGM.IntTy; 465 } 466 467 llvm::StructType *getBaseClassDescriptorType() { 468 if (BaseClassDescriptorType) 469 return BaseClassDescriptorType; 470 llvm::Type *FieldTypes[] = { 471 getImageRelativeType(CGM.Int8PtrTy), 472 CGM.IntTy, 473 CGM.IntTy, 474 CGM.IntTy, 475 CGM.IntTy, 476 CGM.IntTy, 477 getImageRelativeType(getClassHierarchyDescriptorType()->getPointerTo()), 478 }; 479 BaseClassDescriptorType = llvm::StructType::create( 480 CGM.getLLVMContext(), FieldTypes, "rtti.BaseClassDescriptor"); 481 return BaseClassDescriptorType; 482 } 483 484 llvm::StructType *getClassHierarchyDescriptorType() { 485 if (ClassHierarchyDescriptorType) 486 return ClassHierarchyDescriptorType; 487 // Forward-declare RTTIClassHierarchyDescriptor to break a cycle. 488 ClassHierarchyDescriptorType = llvm::StructType::create( 489 CGM.getLLVMContext(), "rtti.ClassHierarchyDescriptor"); 490 llvm::Type *FieldTypes[] = { 491 CGM.IntTy, 492 CGM.IntTy, 493 CGM.IntTy, 494 getImageRelativeType( 495 getBaseClassDescriptorType()->getPointerTo()->getPointerTo()), 496 }; 497 ClassHierarchyDescriptorType->setBody(FieldTypes); 498 return ClassHierarchyDescriptorType; 499 } 500 501 llvm::StructType *getCompleteObjectLocatorType() { 502 if (CompleteObjectLocatorType) 503 return CompleteObjectLocatorType; 504 CompleteObjectLocatorType = llvm::StructType::create( 505 CGM.getLLVMContext(), "rtti.CompleteObjectLocator"); 506 llvm::Type *FieldTypes[] = { 507 CGM.IntTy, 508 CGM.IntTy, 509 CGM.IntTy, 510 getImageRelativeType(CGM.Int8PtrTy), 511 getImageRelativeType(getClassHierarchyDescriptorType()->getPointerTo()), 512 getImageRelativeType(CompleteObjectLocatorType), 513 }; 514 llvm::ArrayRef<llvm::Type *> FieldTypesRef(FieldTypes); 515 if (!isImageRelative()) 516 FieldTypesRef = FieldTypesRef.drop_back(); 517 CompleteObjectLocatorType->setBody(FieldTypesRef); 518 return CompleteObjectLocatorType; 519 } 520 521 llvm::GlobalVariable *getImageBase() { 522 StringRef Name = "__ImageBase"; 523 if (llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(Name)) 524 return GV; 525 526 auto *GV = new llvm::GlobalVariable(CGM.getModule(), CGM.Int8Ty, 527 /*isConstant=*/true, 528 llvm::GlobalValue::ExternalLinkage, 529 /*Initializer=*/nullptr, Name); 530 CGM.setDSOLocal(GV); 531 return GV; 532 } 533 534 llvm::Constant *getImageRelativeConstant(llvm::Constant *PtrVal) { 535 if (!isImageRelative()) 536 return PtrVal; 537 538 if (PtrVal->isNullValue()) 539 return llvm::Constant::getNullValue(CGM.IntTy); 540 541 llvm::Constant *ImageBaseAsInt = 542 llvm::ConstantExpr::getPtrToInt(getImageBase(), CGM.IntPtrTy); 543 llvm::Constant *PtrValAsInt = 544 llvm::ConstantExpr::getPtrToInt(PtrVal, CGM.IntPtrTy); 545 llvm::Constant *Diff = 546 llvm::ConstantExpr::getSub(PtrValAsInt, ImageBaseAsInt, 547 /*HasNUW=*/true, /*HasNSW=*/true); 548 return llvm::ConstantExpr::getTrunc(Diff, CGM.IntTy); 549 } 550 551 private: 552 MicrosoftMangleContext &getMangleContext() { 553 return cast<MicrosoftMangleContext>(CodeGen::CGCXXABI::getMangleContext()); 554 } 555 556 llvm::Constant *getZeroInt() { 557 return llvm::ConstantInt::get(CGM.IntTy, 0); 558 } 559 560 llvm::Constant *getAllOnesInt() { 561 return llvm::Constant::getAllOnesValue(CGM.IntTy); 562 } 563 564 CharUnits getVirtualFunctionPrologueThisAdjustment(GlobalDecl GD) override; 565 566 void 567 GetNullMemberPointerFields(const MemberPointerType *MPT, 568 llvm::SmallVectorImpl<llvm::Constant *> &fields); 569 570 /// Shared code for virtual base adjustment. Returns the offset from 571 /// the vbptr to the virtual base. Optionally returns the address of the 572 /// vbptr itself. 573 llvm::Value *GetVBaseOffsetFromVBPtr(CodeGenFunction &CGF, 574 Address Base, 575 llvm::Value *VBPtrOffset, 576 llvm::Value *VBTableOffset, 577 llvm::Value **VBPtr = nullptr); 578 579 llvm::Value *GetVBaseOffsetFromVBPtr(CodeGenFunction &CGF, 580 Address Base, 581 int32_t VBPtrOffset, 582 int32_t VBTableOffset, 583 llvm::Value **VBPtr = nullptr) { 584 assert(VBTableOffset % 4 == 0 && "should be byte offset into table of i32s"); 585 llvm::Value *VBPOffset = llvm::ConstantInt::get(CGM.IntTy, VBPtrOffset), 586 *VBTOffset = llvm::ConstantInt::get(CGM.IntTy, VBTableOffset); 587 return GetVBaseOffsetFromVBPtr(CGF, Base, VBPOffset, VBTOffset, VBPtr); 588 } 589 590 std::tuple<Address, llvm::Value *, const CXXRecordDecl *> 591 performBaseAdjustment(CodeGenFunction &CGF, Address Value, 592 QualType SrcRecordTy); 593 594 /// Performs a full virtual base adjustment. Used to dereference 595 /// pointers to members of virtual bases. 596 llvm::Value *AdjustVirtualBase(CodeGenFunction &CGF, const Expr *E, 597 const CXXRecordDecl *RD, Address Base, 598 llvm::Value *VirtualBaseAdjustmentOffset, 599 llvm::Value *VBPtrOffset /* optional */); 600 601 /// Emits a full member pointer with the fields common to data and 602 /// function member pointers. 603 llvm::Constant *EmitFullMemberPointer(llvm::Constant *FirstField, 604 bool IsMemberFunction, 605 const CXXRecordDecl *RD, 606 CharUnits NonVirtualBaseAdjustment, 607 unsigned VBTableIndex); 608 609 bool MemberPointerConstantIsNull(const MemberPointerType *MPT, 610 llvm::Constant *MP); 611 612 /// - Initialize all vbptrs of 'this' with RD as the complete type. 613 void EmitVBPtrStores(CodeGenFunction &CGF, const CXXRecordDecl *RD); 614 615 /// Caching wrapper around VBTableBuilder::enumerateVBTables(). 616 const VBTableGlobals &enumerateVBTables(const CXXRecordDecl *RD); 617 618 /// Generate a thunk for calling a virtual member function MD. 619 llvm::Function *EmitVirtualMemPtrThunk(const CXXMethodDecl *MD, 620 const MethodVFTableLocation &ML); 621 622 public: 623 llvm::Type *ConvertMemberPointerType(const MemberPointerType *MPT) override; 624 625 bool isZeroInitializable(const MemberPointerType *MPT) override; 626 627 bool isMemberPointerConvertible(const MemberPointerType *MPT) const override { 628 const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl(); 629 return RD->hasAttr<MSInheritanceAttr>(); 630 } 631 632 llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT) override; 633 634 llvm::Constant *EmitMemberDataPointer(const MemberPointerType *MPT, 635 CharUnits offset) override; 636 llvm::Constant *EmitMemberFunctionPointer(const CXXMethodDecl *MD) override; 637 llvm::Constant *EmitMemberPointer(const APValue &MP, QualType MPT) override; 638 639 llvm::Value *EmitMemberPointerComparison(CodeGenFunction &CGF, 640 llvm::Value *L, 641 llvm::Value *R, 642 const MemberPointerType *MPT, 643 bool Inequality) override; 644 645 llvm::Value *EmitMemberPointerIsNotNull(CodeGenFunction &CGF, 646 llvm::Value *MemPtr, 647 const MemberPointerType *MPT) override; 648 649 llvm::Value * 650 EmitMemberDataPointerAddress(CodeGenFunction &CGF, const Expr *E, 651 Address Base, llvm::Value *MemPtr, 652 const MemberPointerType *MPT) override; 653 654 llvm::Value *EmitNonNullMemberPointerConversion( 655 const MemberPointerType *SrcTy, const MemberPointerType *DstTy, 656 CastKind CK, CastExpr::path_const_iterator PathBegin, 657 CastExpr::path_const_iterator PathEnd, llvm::Value *Src, 658 CGBuilderTy &Builder); 659 660 llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF, 661 const CastExpr *E, 662 llvm::Value *Src) override; 663 664 llvm::Constant *EmitMemberPointerConversion(const CastExpr *E, 665 llvm::Constant *Src) override; 666 667 llvm::Constant *EmitMemberPointerConversion( 668 const MemberPointerType *SrcTy, const MemberPointerType *DstTy, 669 CastKind CK, CastExpr::path_const_iterator PathBegin, 670 CastExpr::path_const_iterator PathEnd, llvm::Constant *Src); 671 672 CGCallee 673 EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF, const Expr *E, 674 Address This, llvm::Value *&ThisPtrForCall, 675 llvm::Value *MemPtr, 676 const MemberPointerType *MPT) override; 677 678 void emitCXXStructor(GlobalDecl GD) override; 679 680 llvm::StructType *getCatchableTypeType() { 681 if (CatchableTypeType) 682 return CatchableTypeType; 683 llvm::Type *FieldTypes[] = { 684 CGM.IntTy, // Flags 685 getImageRelativeType(CGM.Int8PtrTy), // TypeDescriptor 686 CGM.IntTy, // NonVirtualAdjustment 687 CGM.IntTy, // OffsetToVBPtr 688 CGM.IntTy, // VBTableIndex 689 CGM.IntTy, // Size 690 getImageRelativeType(CGM.Int8PtrTy) // CopyCtor 691 }; 692 CatchableTypeType = llvm::StructType::create( 693 CGM.getLLVMContext(), FieldTypes, "eh.CatchableType"); 694 return CatchableTypeType; 695 } 696 697 llvm::StructType *getCatchableTypeArrayType(uint32_t NumEntries) { 698 llvm::StructType *&CatchableTypeArrayType = 699 CatchableTypeArrayTypeMap[NumEntries]; 700 if (CatchableTypeArrayType) 701 return CatchableTypeArrayType; 702 703 llvm::SmallString<23> CTATypeName("eh.CatchableTypeArray."); 704 CTATypeName += llvm::utostr(NumEntries); 705 llvm::Type *CTType = 706 getImageRelativeType(getCatchableTypeType()->getPointerTo()); 707 llvm::Type *FieldTypes[] = { 708 CGM.IntTy, // NumEntries 709 llvm::ArrayType::get(CTType, NumEntries) // CatchableTypes 710 }; 711 CatchableTypeArrayType = 712 llvm::StructType::create(CGM.getLLVMContext(), FieldTypes, CTATypeName); 713 return CatchableTypeArrayType; 714 } 715 716 llvm::StructType *getThrowInfoType() { 717 if (ThrowInfoType) 718 return ThrowInfoType; 719 llvm::Type *FieldTypes[] = { 720 CGM.IntTy, // Flags 721 getImageRelativeType(CGM.Int8PtrTy), // CleanupFn 722 getImageRelativeType(CGM.Int8PtrTy), // ForwardCompat 723 getImageRelativeType(CGM.Int8PtrTy) // CatchableTypeArray 724 }; 725 ThrowInfoType = llvm::StructType::create(CGM.getLLVMContext(), FieldTypes, 726 "eh.ThrowInfo"); 727 return ThrowInfoType; 728 } 729 730 llvm::FunctionCallee getThrowFn() { 731 // _CxxThrowException is passed an exception object and a ThrowInfo object 732 // which describes the exception. 733 llvm::Type *Args[] = {CGM.Int8PtrTy, getThrowInfoType()->getPointerTo()}; 734 llvm::FunctionType *FTy = 735 llvm::FunctionType::get(CGM.VoidTy, Args, /*isVarArg=*/false); 736 llvm::FunctionCallee Throw = 737 CGM.CreateRuntimeFunction(FTy, "_CxxThrowException"); 738 // _CxxThrowException is stdcall on 32-bit x86 platforms. 739 if (CGM.getTarget().getTriple().getArch() == llvm::Triple::x86) { 740 if (auto *Fn = dyn_cast<llvm::Function>(Throw.getCallee())) 741 Fn->setCallingConv(llvm::CallingConv::X86_StdCall); 742 } 743 return Throw; 744 } 745 746 llvm::Function *getAddrOfCXXCtorClosure(const CXXConstructorDecl *CD, 747 CXXCtorType CT); 748 749 llvm::Constant *getCatchableType(QualType T, 750 uint32_t NVOffset = 0, 751 int32_t VBPtrOffset = -1, 752 uint32_t VBIndex = 0); 753 754 llvm::GlobalVariable *getCatchableTypeArray(QualType T); 755 756 llvm::GlobalVariable *getThrowInfo(QualType T) override; 757 758 std::pair<llvm::Value *, const CXXRecordDecl *> 759 LoadVTablePtr(CodeGenFunction &CGF, Address This, 760 const CXXRecordDecl *RD) override; 761 762 private: 763 typedef std::pair<const CXXRecordDecl *, CharUnits> VFTableIdTy; 764 typedef llvm::DenseMap<VFTableIdTy, llvm::GlobalVariable *> VTablesMapTy; 765 typedef llvm::DenseMap<VFTableIdTy, llvm::GlobalValue *> VFTablesMapTy; 766 /// All the vftables that have been referenced. 767 VFTablesMapTy VFTablesMap; 768 VTablesMapTy VTablesMap; 769 770 /// This set holds the record decls we've deferred vtable emission for. 771 llvm::SmallPtrSet<const CXXRecordDecl *, 4> DeferredVFTables; 772 773 774 /// All the vbtables which have been referenced. 775 llvm::DenseMap<const CXXRecordDecl *, VBTableGlobals> VBTablesMap; 776 777 /// Info on the global variable used to guard initialization of static locals. 778 /// The BitIndex field is only used for externally invisible declarations. 779 struct GuardInfo { 780 GuardInfo() : Guard(nullptr), BitIndex(0) {} 781 llvm::GlobalVariable *Guard; 782 unsigned BitIndex; 783 }; 784 785 /// Map from DeclContext to the current guard variable. We assume that the 786 /// AST is visited in source code order. 787 llvm::DenseMap<const DeclContext *, GuardInfo> GuardVariableMap; 788 llvm::DenseMap<const DeclContext *, GuardInfo> ThreadLocalGuardVariableMap; 789 llvm::DenseMap<const DeclContext *, unsigned> ThreadSafeGuardNumMap; 790 791 llvm::DenseMap<size_t, llvm::StructType *> TypeDescriptorTypeMap; 792 llvm::StructType *BaseClassDescriptorType; 793 llvm::StructType *ClassHierarchyDescriptorType; 794 llvm::StructType *CompleteObjectLocatorType; 795 796 llvm::DenseMap<QualType, llvm::GlobalVariable *> CatchableTypeArrays; 797 798 llvm::StructType *CatchableTypeType; 799 llvm::DenseMap<uint32_t, llvm::StructType *> CatchableTypeArrayTypeMap; 800 llvm::StructType *ThrowInfoType; 801 }; 802 803 } 804 805 CGCXXABI::RecordArgABI 806 MicrosoftCXXABI::getRecordArgABI(const CXXRecordDecl *RD) const { 807 switch (CGM.getTarget().getTriple().getArch()) { 808 default: 809 // FIXME: Implement for other architectures. 810 return RAA_Default; 811 812 case llvm::Triple::thumb: 813 // Use the simple Itanium rules for now. 814 // FIXME: This is incompatible with MSVC for arguments with a dtor and no 815 // copy ctor. 816 return !RD->canPassInRegisters() ? RAA_Indirect : RAA_Default; 817 818 case llvm::Triple::x86: 819 // All record arguments are passed in memory on x86. Decide whether to 820 // construct the object directly in argument memory, or to construct the 821 // argument elsewhere and copy the bytes during the call. 822 823 // If C++ prohibits us from making a copy, construct the arguments directly 824 // into argument memory. 825 if (!RD->canPassInRegisters()) 826 return RAA_DirectInMemory; 827 828 // Otherwise, construct the argument into a temporary and copy the bytes 829 // into the outgoing argument memory. 830 return RAA_Default; 831 832 case llvm::Triple::x86_64: 833 case llvm::Triple::aarch64: 834 return !RD->canPassInRegisters() ? RAA_Indirect : RAA_Default; 835 } 836 837 llvm_unreachable("invalid enum"); 838 } 839 840 void MicrosoftCXXABI::emitVirtualObjectDelete(CodeGenFunction &CGF, 841 const CXXDeleteExpr *DE, 842 Address Ptr, 843 QualType ElementType, 844 const CXXDestructorDecl *Dtor) { 845 // FIXME: Provide a source location here even though there's no 846 // CXXMemberCallExpr for dtor call. 847 bool UseGlobalDelete = DE->isGlobalDelete(); 848 CXXDtorType DtorType = UseGlobalDelete ? Dtor_Complete : Dtor_Deleting; 849 llvm::Value *MDThis = EmitVirtualDestructorCall(CGF, Dtor, DtorType, Ptr, DE); 850 if (UseGlobalDelete) 851 CGF.EmitDeleteCall(DE->getOperatorDelete(), MDThis, ElementType); 852 } 853 854 void MicrosoftCXXABI::emitRethrow(CodeGenFunction &CGF, bool isNoReturn) { 855 llvm::Value *Args[] = { 856 llvm::ConstantPointerNull::get(CGM.Int8PtrTy), 857 llvm::ConstantPointerNull::get(getThrowInfoType()->getPointerTo())}; 858 llvm::FunctionCallee Fn = getThrowFn(); 859 if (isNoReturn) 860 CGF.EmitNoreturnRuntimeCallOrInvoke(Fn, Args); 861 else 862 CGF.EmitRuntimeCallOrInvoke(Fn, Args); 863 } 864 865 void MicrosoftCXXABI::emitBeginCatch(CodeGenFunction &CGF, 866 const CXXCatchStmt *S) { 867 // In the MS ABI, the runtime handles the copy, and the catch handler is 868 // responsible for destruction. 869 VarDecl *CatchParam = S->getExceptionDecl(); 870 llvm::BasicBlock *CatchPadBB = CGF.Builder.GetInsertBlock(); 871 llvm::CatchPadInst *CPI = 872 cast<llvm::CatchPadInst>(CatchPadBB->getFirstNonPHI()); 873 CGF.CurrentFuncletPad = CPI; 874 875 // If this is a catch-all or the catch parameter is unnamed, we don't need to 876 // emit an alloca to the object. 877 if (!CatchParam || !CatchParam->getDeclName()) { 878 CGF.EHStack.pushCleanup<CatchRetScope>(NormalCleanup, CPI); 879 return; 880 } 881 882 CodeGenFunction::AutoVarEmission var = CGF.EmitAutoVarAlloca(*CatchParam); 883 CPI->setArgOperand(2, var.getObjectAddress(CGF).getPointer()); 884 CGF.EHStack.pushCleanup<CatchRetScope>(NormalCleanup, CPI); 885 CGF.EmitAutoVarCleanups(var); 886 } 887 888 /// We need to perform a generic polymorphic operation (like a typeid 889 /// or a cast), which requires an object with a vfptr. Adjust the 890 /// address to point to an object with a vfptr. 891 std::tuple<Address, llvm::Value *, const CXXRecordDecl *> 892 MicrosoftCXXABI::performBaseAdjustment(CodeGenFunction &CGF, Address Value, 893 QualType SrcRecordTy) { 894 Value = CGF.Builder.CreateBitCast(Value, CGF.Int8PtrTy); 895 const CXXRecordDecl *SrcDecl = SrcRecordTy->getAsCXXRecordDecl(); 896 const ASTContext &Context = getContext(); 897 898 // If the class itself has a vfptr, great. This check implicitly 899 // covers non-virtual base subobjects: a class with its own virtual 900 // functions would be a candidate to be a primary base. 901 if (Context.getASTRecordLayout(SrcDecl).hasExtendableVFPtr()) 902 return std::make_tuple(Value, llvm::ConstantInt::get(CGF.Int32Ty, 0), 903 SrcDecl); 904 905 // Okay, one of the vbases must have a vfptr, or else this isn't 906 // actually a polymorphic class. 907 const CXXRecordDecl *PolymorphicBase = nullptr; 908 for (auto &Base : SrcDecl->vbases()) { 909 const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl(); 910 if (Context.getASTRecordLayout(BaseDecl).hasExtendableVFPtr()) { 911 PolymorphicBase = BaseDecl; 912 break; 913 } 914 } 915 assert(PolymorphicBase && "polymorphic class has no apparent vfptr?"); 916 917 llvm::Value *Offset = 918 GetVirtualBaseClassOffset(CGF, Value, SrcDecl, PolymorphicBase); 919 llvm::Value *Ptr = CGF.Builder.CreateInBoundsGEP(Value.getPointer(), Offset); 920 CharUnits VBaseAlign = 921 CGF.CGM.getVBaseAlignment(Value.getAlignment(), SrcDecl, PolymorphicBase); 922 return std::make_tuple(Address(Ptr, VBaseAlign), Offset, PolymorphicBase); 923 } 924 925 bool MicrosoftCXXABI::shouldTypeidBeNullChecked(bool IsDeref, 926 QualType SrcRecordTy) { 927 const CXXRecordDecl *SrcDecl = SrcRecordTy->getAsCXXRecordDecl(); 928 return IsDeref && 929 !getContext().getASTRecordLayout(SrcDecl).hasExtendableVFPtr(); 930 } 931 932 static llvm::CallBase *emitRTtypeidCall(CodeGenFunction &CGF, 933 llvm::Value *Argument) { 934 llvm::Type *ArgTypes[] = {CGF.Int8PtrTy}; 935 llvm::FunctionType *FTy = 936 llvm::FunctionType::get(CGF.Int8PtrTy, ArgTypes, false); 937 llvm::Value *Args[] = {Argument}; 938 llvm::FunctionCallee Fn = CGF.CGM.CreateRuntimeFunction(FTy, "__RTtypeid"); 939 return CGF.EmitRuntimeCallOrInvoke(Fn, Args); 940 } 941 942 void MicrosoftCXXABI::EmitBadTypeidCall(CodeGenFunction &CGF) { 943 llvm::CallBase *Call = 944 emitRTtypeidCall(CGF, llvm::Constant::getNullValue(CGM.VoidPtrTy)); 945 Call->setDoesNotReturn(); 946 CGF.Builder.CreateUnreachable(); 947 } 948 949 llvm::Value *MicrosoftCXXABI::EmitTypeid(CodeGenFunction &CGF, 950 QualType SrcRecordTy, 951 Address ThisPtr, 952 llvm::Type *StdTypeInfoPtrTy) { 953 std::tie(ThisPtr, std::ignore, std::ignore) = 954 performBaseAdjustment(CGF, ThisPtr, SrcRecordTy); 955 llvm::CallBase *Typeid = emitRTtypeidCall(CGF, ThisPtr.getPointer()); 956 return CGF.Builder.CreateBitCast(Typeid, StdTypeInfoPtrTy); 957 } 958 959 bool MicrosoftCXXABI::shouldDynamicCastCallBeNullChecked(bool SrcIsPtr, 960 QualType SrcRecordTy) { 961 const CXXRecordDecl *SrcDecl = SrcRecordTy->getAsCXXRecordDecl(); 962 return SrcIsPtr && 963 !getContext().getASTRecordLayout(SrcDecl).hasExtendableVFPtr(); 964 } 965 966 llvm::Value *MicrosoftCXXABI::EmitDynamicCastCall( 967 CodeGenFunction &CGF, Address This, QualType SrcRecordTy, 968 QualType DestTy, QualType DestRecordTy, llvm::BasicBlock *CastEnd) { 969 llvm::Type *DestLTy = CGF.ConvertType(DestTy); 970 971 llvm::Value *SrcRTTI = 972 CGF.CGM.GetAddrOfRTTIDescriptor(SrcRecordTy.getUnqualifiedType()); 973 llvm::Value *DestRTTI = 974 CGF.CGM.GetAddrOfRTTIDescriptor(DestRecordTy.getUnqualifiedType()); 975 976 llvm::Value *Offset; 977 std::tie(This, Offset, std::ignore) = 978 performBaseAdjustment(CGF, This, SrcRecordTy); 979 llvm::Value *ThisPtr = This.getPointer(); 980 Offset = CGF.Builder.CreateTrunc(Offset, CGF.Int32Ty); 981 982 // PVOID __RTDynamicCast( 983 // PVOID inptr, 984 // LONG VfDelta, 985 // PVOID SrcType, 986 // PVOID TargetType, 987 // BOOL isReference) 988 llvm::Type *ArgTypes[] = {CGF.Int8PtrTy, CGF.Int32Ty, CGF.Int8PtrTy, 989 CGF.Int8PtrTy, CGF.Int32Ty}; 990 llvm::FunctionCallee Function = CGF.CGM.CreateRuntimeFunction( 991 llvm::FunctionType::get(CGF.Int8PtrTy, ArgTypes, false), 992 "__RTDynamicCast"); 993 llvm::Value *Args[] = { 994 ThisPtr, Offset, SrcRTTI, DestRTTI, 995 llvm::ConstantInt::get(CGF.Int32Ty, DestTy->isReferenceType())}; 996 ThisPtr = CGF.EmitRuntimeCallOrInvoke(Function, Args); 997 return CGF.Builder.CreateBitCast(ThisPtr, DestLTy); 998 } 999 1000 llvm::Value * 1001 MicrosoftCXXABI::EmitDynamicCastToVoid(CodeGenFunction &CGF, Address Value, 1002 QualType SrcRecordTy, 1003 QualType DestTy) { 1004 std::tie(Value, std::ignore, std::ignore) = 1005 performBaseAdjustment(CGF, Value, SrcRecordTy); 1006 1007 // PVOID __RTCastToVoid( 1008 // PVOID inptr) 1009 llvm::Type *ArgTypes[] = {CGF.Int8PtrTy}; 1010 llvm::FunctionCallee Function = CGF.CGM.CreateRuntimeFunction( 1011 llvm::FunctionType::get(CGF.Int8PtrTy, ArgTypes, false), 1012 "__RTCastToVoid"); 1013 llvm::Value *Args[] = {Value.getPointer()}; 1014 return CGF.EmitRuntimeCall(Function, Args); 1015 } 1016 1017 bool MicrosoftCXXABI::EmitBadCastCall(CodeGenFunction &CGF) { 1018 return false; 1019 } 1020 1021 llvm::Value *MicrosoftCXXABI::GetVirtualBaseClassOffset( 1022 CodeGenFunction &CGF, Address This, const CXXRecordDecl *ClassDecl, 1023 const CXXRecordDecl *BaseClassDecl) { 1024 const ASTContext &Context = getContext(); 1025 int64_t VBPtrChars = 1026 Context.getASTRecordLayout(ClassDecl).getVBPtrOffset().getQuantity(); 1027 llvm::Value *VBPtrOffset = llvm::ConstantInt::get(CGM.PtrDiffTy, VBPtrChars); 1028 CharUnits IntSize = Context.getTypeSizeInChars(Context.IntTy); 1029 CharUnits VBTableChars = 1030 IntSize * 1031 CGM.getMicrosoftVTableContext().getVBTableIndex(ClassDecl, BaseClassDecl); 1032 llvm::Value *VBTableOffset = 1033 llvm::ConstantInt::get(CGM.IntTy, VBTableChars.getQuantity()); 1034 1035 llvm::Value *VBPtrToNewBase = 1036 GetVBaseOffsetFromVBPtr(CGF, This, VBPtrOffset, VBTableOffset); 1037 VBPtrToNewBase = 1038 CGF.Builder.CreateSExtOrBitCast(VBPtrToNewBase, CGM.PtrDiffTy); 1039 return CGF.Builder.CreateNSWAdd(VBPtrOffset, VBPtrToNewBase); 1040 } 1041 1042 bool MicrosoftCXXABI::HasThisReturn(GlobalDecl GD) const { 1043 return isa<CXXConstructorDecl>(GD.getDecl()); 1044 } 1045 1046 static bool isDeletingDtor(GlobalDecl GD) { 1047 return isa<CXXDestructorDecl>(GD.getDecl()) && 1048 GD.getDtorType() == Dtor_Deleting; 1049 } 1050 1051 bool MicrosoftCXXABI::hasMostDerivedReturn(GlobalDecl GD) const { 1052 return isDeletingDtor(GD); 1053 } 1054 1055 static bool IsSizeGreaterThan128(const CXXRecordDecl *RD) { 1056 return RD->getASTContext().getTypeSize(RD->getTypeForDecl()) > 128; 1057 } 1058 1059 static bool hasMicrosoftABIRestrictions(const CXXRecordDecl *RD) { 1060 // For AArch64, we use the C++14 definition of an aggregate, so we also 1061 // check for: 1062 // No private or protected non static data members. 1063 // No base classes 1064 // No virtual functions 1065 // Additionally, we need to ensure that there is a trivial copy assignment 1066 // operator, a trivial destructor and no user-provided constructors. 1067 if (RD->hasProtectedFields() || RD->hasPrivateFields()) 1068 return true; 1069 if (RD->getNumBases() > 0) 1070 return true; 1071 if (RD->isPolymorphic()) 1072 return true; 1073 if (RD->hasNonTrivialCopyAssignment()) 1074 return true; 1075 for (const CXXConstructorDecl *Ctor : RD->ctors()) 1076 if (Ctor->isUserProvided()) 1077 return true; 1078 if (RD->hasNonTrivialDestructor()) 1079 return true; 1080 return false; 1081 } 1082 1083 bool MicrosoftCXXABI::classifyReturnType(CGFunctionInfo &FI) const { 1084 const CXXRecordDecl *RD = FI.getReturnType()->getAsCXXRecordDecl(); 1085 if (!RD) 1086 return false; 1087 1088 bool isAArch64 = CGM.getTarget().getTriple().isAArch64(); 1089 bool isSimple = !isAArch64 || !hasMicrosoftABIRestrictions(RD); 1090 bool isIndirectReturn = 1091 isAArch64 ? (!RD->canPassInRegisters() || 1092 IsSizeGreaterThan128(RD)) 1093 : !RD->isPOD(); 1094 bool isInstanceMethod = FI.isInstanceMethod(); 1095 1096 if (isIndirectReturn || !isSimple || isInstanceMethod) { 1097 CharUnits Align = CGM.getContext().getTypeAlignInChars(FI.getReturnType()); 1098 FI.getReturnInfo() = ABIArgInfo::getIndirect(Align, /*ByVal=*/false); 1099 FI.getReturnInfo().setSRetAfterThis(isInstanceMethod); 1100 1101 FI.getReturnInfo().setInReg(isAArch64 && 1102 !(isSimple && IsSizeGreaterThan128(RD))); 1103 1104 return true; 1105 } 1106 1107 // Otherwise, use the C ABI rules. 1108 return false; 1109 } 1110 1111 llvm::BasicBlock * 1112 MicrosoftCXXABI::EmitCtorCompleteObjectHandler(CodeGenFunction &CGF, 1113 const CXXRecordDecl *RD) { 1114 llvm::Value *IsMostDerivedClass = getStructorImplicitParamValue(CGF); 1115 assert(IsMostDerivedClass && 1116 "ctor for a class with virtual bases must have an implicit parameter"); 1117 llvm::Value *IsCompleteObject = 1118 CGF.Builder.CreateIsNotNull(IsMostDerivedClass, "is_complete_object"); 1119 1120 llvm::BasicBlock *CallVbaseCtorsBB = CGF.createBasicBlock("ctor.init_vbases"); 1121 llvm::BasicBlock *SkipVbaseCtorsBB = CGF.createBasicBlock("ctor.skip_vbases"); 1122 CGF.Builder.CreateCondBr(IsCompleteObject, 1123 CallVbaseCtorsBB, SkipVbaseCtorsBB); 1124 1125 CGF.EmitBlock(CallVbaseCtorsBB); 1126 1127 // Fill in the vbtable pointers here. 1128 EmitVBPtrStores(CGF, RD); 1129 1130 // CGF will put the base ctor calls in this basic block for us later. 1131 1132 return SkipVbaseCtorsBB; 1133 } 1134 1135 llvm::BasicBlock * 1136 MicrosoftCXXABI::EmitDtorCompleteObjectHandler(CodeGenFunction &CGF) { 1137 llvm::Value *IsMostDerivedClass = getStructorImplicitParamValue(CGF); 1138 assert(IsMostDerivedClass && 1139 "ctor for a class with virtual bases must have an implicit parameter"); 1140 llvm::Value *IsCompleteObject = 1141 CGF.Builder.CreateIsNotNull(IsMostDerivedClass, "is_complete_object"); 1142 1143 llvm::BasicBlock *CallVbaseDtorsBB = CGF.createBasicBlock("Dtor.dtor_vbases"); 1144 llvm::BasicBlock *SkipVbaseDtorsBB = CGF.createBasicBlock("Dtor.skip_vbases"); 1145 CGF.Builder.CreateCondBr(IsCompleteObject, 1146 CallVbaseDtorsBB, SkipVbaseDtorsBB); 1147 1148 CGF.EmitBlock(CallVbaseDtorsBB); 1149 // CGF will put the base dtor calls in this basic block for us later. 1150 1151 return SkipVbaseDtorsBB; 1152 } 1153 1154 void MicrosoftCXXABI::initializeHiddenVirtualInheritanceMembers( 1155 CodeGenFunction &CGF, const CXXRecordDecl *RD) { 1156 // In most cases, an override for a vbase virtual method can adjust 1157 // the "this" parameter by applying a constant offset. 1158 // However, this is not enough while a constructor or a destructor of some 1159 // class X is being executed if all the following conditions are met: 1160 // - X has virtual bases, (1) 1161 // - X overrides a virtual method M of a vbase Y, (2) 1162 // - X itself is a vbase of the most derived class. 1163 // 1164 // If (1) and (2) are true, the vtorDisp for vbase Y is a hidden member of X 1165 // which holds the extra amount of "this" adjustment we must do when we use 1166 // the X vftables (i.e. during X ctor or dtor). 1167 // Outside the ctors and dtors, the values of vtorDisps are zero. 1168 1169 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); 1170 typedef ASTRecordLayout::VBaseOffsetsMapTy VBOffsets; 1171 const VBOffsets &VBaseMap = Layout.getVBaseOffsetsMap(); 1172 CGBuilderTy &Builder = CGF.Builder; 1173 1174 unsigned AS = getThisAddress(CGF).getAddressSpace(); 1175 llvm::Value *Int8This = nullptr; // Initialize lazily. 1176 1177 for (const CXXBaseSpecifier &S : RD->vbases()) { 1178 const CXXRecordDecl *VBase = S.getType()->getAsCXXRecordDecl(); 1179 auto I = VBaseMap.find(VBase); 1180 assert(I != VBaseMap.end()); 1181 if (!I->second.hasVtorDisp()) 1182 continue; 1183 1184 llvm::Value *VBaseOffset = 1185 GetVirtualBaseClassOffset(CGF, getThisAddress(CGF), RD, VBase); 1186 uint64_t ConstantVBaseOffset = I->second.VBaseOffset.getQuantity(); 1187 1188 // vtorDisp_for_vbase = vbptr[vbase_idx] - offsetof(RD, vbase). 1189 llvm::Value *VtorDispValue = Builder.CreateSub( 1190 VBaseOffset, llvm::ConstantInt::get(CGM.PtrDiffTy, ConstantVBaseOffset), 1191 "vtordisp.value"); 1192 VtorDispValue = Builder.CreateTruncOrBitCast(VtorDispValue, CGF.Int32Ty); 1193 1194 if (!Int8This) 1195 Int8This = Builder.CreateBitCast(getThisValue(CGF), 1196 CGF.Int8Ty->getPointerTo(AS)); 1197 llvm::Value *VtorDispPtr = Builder.CreateInBoundsGEP(Int8This, VBaseOffset); 1198 // vtorDisp is always the 32-bits before the vbase in the class layout. 1199 VtorDispPtr = Builder.CreateConstGEP1_32(VtorDispPtr, -4); 1200 VtorDispPtr = Builder.CreateBitCast( 1201 VtorDispPtr, CGF.Int32Ty->getPointerTo(AS), "vtordisp.ptr"); 1202 1203 Builder.CreateAlignedStore(VtorDispValue, VtorDispPtr, 1204 CharUnits::fromQuantity(4)); 1205 } 1206 } 1207 1208 static bool hasDefaultCXXMethodCC(ASTContext &Context, 1209 const CXXMethodDecl *MD) { 1210 CallingConv ExpectedCallingConv = Context.getDefaultCallingConvention( 1211 /*IsVariadic=*/false, /*IsCXXMethod=*/true); 1212 CallingConv ActualCallingConv = 1213 MD->getType()->castAs<FunctionProtoType>()->getCallConv(); 1214 return ExpectedCallingConv == ActualCallingConv; 1215 } 1216 1217 void MicrosoftCXXABI::EmitCXXConstructors(const CXXConstructorDecl *D) { 1218 // There's only one constructor type in this ABI. 1219 CGM.EmitGlobal(GlobalDecl(D, Ctor_Complete)); 1220 1221 // Exported default constructors either have a simple call-site where they use 1222 // the typical calling convention and have a single 'this' pointer for an 1223 // argument -or- they get a wrapper function which appropriately thunks to the 1224 // real default constructor. This thunk is the default constructor closure. 1225 if (D->hasAttr<DLLExportAttr>() && D->isDefaultConstructor()) 1226 if (!hasDefaultCXXMethodCC(getContext(), D) || D->getNumParams() != 0) { 1227 llvm::Function *Fn = getAddrOfCXXCtorClosure(D, Ctor_DefaultClosure); 1228 Fn->setLinkage(llvm::GlobalValue::WeakODRLinkage); 1229 CGM.setGVProperties(Fn, D); 1230 } 1231 } 1232 1233 void MicrosoftCXXABI::EmitVBPtrStores(CodeGenFunction &CGF, 1234 const CXXRecordDecl *RD) { 1235 Address This = getThisAddress(CGF); 1236 This = CGF.Builder.CreateElementBitCast(This, CGM.Int8Ty, "this.int8"); 1237 const ASTContext &Context = getContext(); 1238 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); 1239 1240 const VBTableGlobals &VBGlobals = enumerateVBTables(RD); 1241 for (unsigned I = 0, E = VBGlobals.VBTables->size(); I != E; ++I) { 1242 const std::unique_ptr<VPtrInfo> &VBT = (*VBGlobals.VBTables)[I]; 1243 llvm::GlobalVariable *GV = VBGlobals.Globals[I]; 1244 const ASTRecordLayout &SubobjectLayout = 1245 Context.getASTRecordLayout(VBT->IntroducingObject); 1246 CharUnits Offs = VBT->NonVirtualOffset; 1247 Offs += SubobjectLayout.getVBPtrOffset(); 1248 if (VBT->getVBaseWithVPtr()) 1249 Offs += Layout.getVBaseClassOffset(VBT->getVBaseWithVPtr()); 1250 Address VBPtr = CGF.Builder.CreateConstInBoundsByteGEP(This, Offs); 1251 llvm::Value *GVPtr = 1252 CGF.Builder.CreateConstInBoundsGEP2_32(GV->getValueType(), GV, 0, 0); 1253 VBPtr = CGF.Builder.CreateElementBitCast(VBPtr, GVPtr->getType(), 1254 "vbptr." + VBT->ObjectWithVPtr->getName()); 1255 CGF.Builder.CreateStore(GVPtr, VBPtr); 1256 } 1257 } 1258 1259 CGCXXABI::AddedStructorArgs 1260 MicrosoftCXXABI::buildStructorSignature(GlobalDecl GD, 1261 SmallVectorImpl<CanQualType> &ArgTys) { 1262 AddedStructorArgs Added; 1263 // TODO: 'for base' flag 1264 if (isa<CXXDestructorDecl>(GD.getDecl()) && 1265 GD.getDtorType() == Dtor_Deleting) { 1266 // The scalar deleting destructor takes an implicit int parameter. 1267 ArgTys.push_back(getContext().IntTy); 1268 ++Added.Suffix; 1269 } 1270 auto *CD = dyn_cast<CXXConstructorDecl>(GD.getDecl()); 1271 if (!CD) 1272 return Added; 1273 1274 // All parameters are already in place except is_most_derived, which goes 1275 // after 'this' if it's variadic and last if it's not. 1276 1277 const CXXRecordDecl *Class = CD->getParent(); 1278 const FunctionProtoType *FPT = CD->getType()->castAs<FunctionProtoType>(); 1279 if (Class->getNumVBases()) { 1280 if (FPT->isVariadic()) { 1281 ArgTys.insert(ArgTys.begin() + 1, getContext().IntTy); 1282 ++Added.Prefix; 1283 } else { 1284 ArgTys.push_back(getContext().IntTy); 1285 ++Added.Suffix; 1286 } 1287 } 1288 1289 return Added; 1290 } 1291 1292 void MicrosoftCXXABI::setCXXDestructorDLLStorage(llvm::GlobalValue *GV, 1293 const CXXDestructorDecl *Dtor, 1294 CXXDtorType DT) const { 1295 // Deleting destructor variants are never imported or exported. Give them the 1296 // default storage class. 1297 if (DT == Dtor_Deleting) { 1298 GV->setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass); 1299 } else { 1300 const NamedDecl *ND = Dtor; 1301 CGM.setDLLImportDLLExport(GV, ND); 1302 } 1303 } 1304 1305 llvm::GlobalValue::LinkageTypes MicrosoftCXXABI::getCXXDestructorLinkage( 1306 GVALinkage Linkage, const CXXDestructorDecl *Dtor, CXXDtorType DT) const { 1307 // Internal things are always internal, regardless of attributes. After this, 1308 // we know the thunk is externally visible. 1309 if (Linkage == GVA_Internal) 1310 return llvm::GlobalValue::InternalLinkage; 1311 1312 switch (DT) { 1313 case Dtor_Base: 1314 // The base destructor most closely tracks the user-declared constructor, so 1315 // we delegate back to the normal declarator case. 1316 return CGM.getLLVMLinkageForDeclarator(Dtor, Linkage, 1317 /*IsConstantVariable=*/false); 1318 case Dtor_Complete: 1319 // The complete destructor is like an inline function, but it may be 1320 // imported and therefore must be exported as well. This requires changing 1321 // the linkage if a DLL attribute is present. 1322 if (Dtor->hasAttr<DLLExportAttr>()) 1323 return llvm::GlobalValue::WeakODRLinkage; 1324 if (Dtor->hasAttr<DLLImportAttr>()) 1325 return llvm::GlobalValue::AvailableExternallyLinkage; 1326 return llvm::GlobalValue::LinkOnceODRLinkage; 1327 case Dtor_Deleting: 1328 // Deleting destructors are like inline functions. They have vague linkage 1329 // and are emitted everywhere they are used. They are internal if the class 1330 // is internal. 1331 return llvm::GlobalValue::LinkOnceODRLinkage; 1332 case Dtor_Comdat: 1333 llvm_unreachable("MS C++ ABI does not support comdat dtors"); 1334 } 1335 llvm_unreachable("invalid dtor type"); 1336 } 1337 1338 void MicrosoftCXXABI::EmitCXXDestructors(const CXXDestructorDecl *D) { 1339 // The TU defining a dtor is only guaranteed to emit a base destructor. All 1340 // other destructor variants are delegating thunks. 1341 CGM.EmitGlobal(GlobalDecl(D, Dtor_Base)); 1342 } 1343 1344 CharUnits 1345 MicrosoftCXXABI::getVirtualFunctionPrologueThisAdjustment(GlobalDecl GD) { 1346 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); 1347 1348 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) { 1349 // Complete destructors take a pointer to the complete object as a 1350 // parameter, thus don't need this adjustment. 1351 if (GD.getDtorType() == Dtor_Complete) 1352 return CharUnits(); 1353 1354 // There's no Dtor_Base in vftable but it shares the this adjustment with 1355 // the deleting one, so look it up instead. 1356 GD = GlobalDecl(DD, Dtor_Deleting); 1357 } 1358 1359 MethodVFTableLocation ML = 1360 CGM.getMicrosoftVTableContext().getMethodVFTableLocation(GD); 1361 CharUnits Adjustment = ML.VFPtrOffset; 1362 1363 // Normal virtual instance methods need to adjust from the vfptr that first 1364 // defined the virtual method to the virtual base subobject, but destructors 1365 // do not. The vector deleting destructor thunk applies this adjustment for 1366 // us if necessary. 1367 if (isa<CXXDestructorDecl>(MD)) 1368 Adjustment = CharUnits::Zero(); 1369 1370 if (ML.VBase) { 1371 const ASTRecordLayout &DerivedLayout = 1372 getContext().getASTRecordLayout(MD->getParent()); 1373 Adjustment += DerivedLayout.getVBaseClassOffset(ML.VBase); 1374 } 1375 1376 return Adjustment; 1377 } 1378 1379 Address MicrosoftCXXABI::adjustThisArgumentForVirtualFunctionCall( 1380 CodeGenFunction &CGF, GlobalDecl GD, Address This, 1381 bool VirtualCall) { 1382 if (!VirtualCall) { 1383 // If the call of a virtual function is not virtual, we just have to 1384 // compensate for the adjustment the virtual function does in its prologue. 1385 CharUnits Adjustment = getVirtualFunctionPrologueThisAdjustment(GD); 1386 if (Adjustment.isZero()) 1387 return This; 1388 1389 This = CGF.Builder.CreateElementBitCast(This, CGF.Int8Ty); 1390 assert(Adjustment.isPositive()); 1391 return CGF.Builder.CreateConstByteGEP(This, Adjustment); 1392 } 1393 1394 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); 1395 1396 GlobalDecl LookupGD = GD; 1397 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) { 1398 // Complete dtors take a pointer to the complete object, 1399 // thus don't need adjustment. 1400 if (GD.getDtorType() == Dtor_Complete) 1401 return This; 1402 1403 // There's only Dtor_Deleting in vftable but it shares the this adjustment 1404 // with the base one, so look up the deleting one instead. 1405 LookupGD = GlobalDecl(DD, Dtor_Deleting); 1406 } 1407 MethodVFTableLocation ML = 1408 CGM.getMicrosoftVTableContext().getMethodVFTableLocation(LookupGD); 1409 1410 CharUnits StaticOffset = ML.VFPtrOffset; 1411 1412 // Base destructors expect 'this' to point to the beginning of the base 1413 // subobject, not the first vfptr that happens to contain the virtual dtor. 1414 // However, we still need to apply the virtual base adjustment. 1415 if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() == Dtor_Base) 1416 StaticOffset = CharUnits::Zero(); 1417 1418 Address Result = This; 1419 if (ML.VBase) { 1420 Result = CGF.Builder.CreateElementBitCast(Result, CGF.Int8Ty); 1421 1422 const CXXRecordDecl *Derived = MD->getParent(); 1423 const CXXRecordDecl *VBase = ML.VBase; 1424 llvm::Value *VBaseOffset = 1425 GetVirtualBaseClassOffset(CGF, Result, Derived, VBase); 1426 llvm::Value *VBasePtr = 1427 CGF.Builder.CreateInBoundsGEP(Result.getPointer(), VBaseOffset); 1428 CharUnits VBaseAlign = 1429 CGF.CGM.getVBaseAlignment(Result.getAlignment(), Derived, VBase); 1430 Result = Address(VBasePtr, VBaseAlign); 1431 } 1432 if (!StaticOffset.isZero()) { 1433 assert(StaticOffset.isPositive()); 1434 Result = CGF.Builder.CreateElementBitCast(Result, CGF.Int8Ty); 1435 if (ML.VBase) { 1436 // Non-virtual adjustment might result in a pointer outside the allocated 1437 // object, e.g. if the final overrider class is laid out after the virtual 1438 // base that declares a method in the most derived class. 1439 // FIXME: Update the code that emits this adjustment in thunks prologues. 1440 Result = CGF.Builder.CreateConstByteGEP(Result, StaticOffset); 1441 } else { 1442 Result = CGF.Builder.CreateConstInBoundsByteGEP(Result, StaticOffset); 1443 } 1444 } 1445 return Result; 1446 } 1447 1448 void MicrosoftCXXABI::addImplicitStructorParams(CodeGenFunction &CGF, 1449 QualType &ResTy, 1450 FunctionArgList &Params) { 1451 ASTContext &Context = getContext(); 1452 const CXXMethodDecl *MD = cast<CXXMethodDecl>(CGF.CurGD.getDecl()); 1453 assert(isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD)); 1454 if (isa<CXXConstructorDecl>(MD) && MD->getParent()->getNumVBases()) { 1455 auto *IsMostDerived = ImplicitParamDecl::Create( 1456 Context, /*DC=*/nullptr, CGF.CurGD.getDecl()->getLocation(), 1457 &Context.Idents.get("is_most_derived"), Context.IntTy, 1458 ImplicitParamDecl::Other); 1459 // The 'most_derived' parameter goes second if the ctor is variadic and last 1460 // if it's not. Dtors can't be variadic. 1461 const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>(); 1462 if (FPT->isVariadic()) 1463 Params.insert(Params.begin() + 1, IsMostDerived); 1464 else 1465 Params.push_back(IsMostDerived); 1466 getStructorImplicitParamDecl(CGF) = IsMostDerived; 1467 } else if (isDeletingDtor(CGF.CurGD)) { 1468 auto *ShouldDelete = ImplicitParamDecl::Create( 1469 Context, /*DC=*/nullptr, CGF.CurGD.getDecl()->getLocation(), 1470 &Context.Idents.get("should_call_delete"), Context.IntTy, 1471 ImplicitParamDecl::Other); 1472 Params.push_back(ShouldDelete); 1473 getStructorImplicitParamDecl(CGF) = ShouldDelete; 1474 } 1475 } 1476 1477 void MicrosoftCXXABI::EmitInstanceFunctionProlog(CodeGenFunction &CGF) { 1478 // Naked functions have no prolog. 1479 if (CGF.CurFuncDecl && CGF.CurFuncDecl->hasAttr<NakedAttr>()) 1480 return; 1481 1482 // Overridden virtual methods of non-primary bases need to adjust the incoming 1483 // 'this' pointer in the prologue. In this hierarchy, C::b will subtract 1484 // sizeof(void*) to adjust from B* to C*: 1485 // struct A { virtual void a(); }; 1486 // struct B { virtual void b(); }; 1487 // struct C : A, B { virtual void b(); }; 1488 // 1489 // Leave the value stored in the 'this' alloca unadjusted, so that the 1490 // debugger sees the unadjusted value. Microsoft debuggers require this, and 1491 // will apply the ThisAdjustment in the method type information. 1492 // FIXME: Do something better for DWARF debuggers, which won't expect this, 1493 // without making our codegen depend on debug info settings. 1494 llvm::Value *This = loadIncomingCXXThis(CGF); 1495 const CXXMethodDecl *MD = cast<CXXMethodDecl>(CGF.CurGD.getDecl()); 1496 if (!CGF.CurFuncIsThunk && MD->isVirtual()) { 1497 CharUnits Adjustment = getVirtualFunctionPrologueThisAdjustment(CGF.CurGD); 1498 if (!Adjustment.isZero()) { 1499 unsigned AS = cast<llvm::PointerType>(This->getType())->getAddressSpace(); 1500 llvm::Type *charPtrTy = CGF.Int8Ty->getPointerTo(AS), 1501 *thisTy = This->getType(); 1502 This = CGF.Builder.CreateBitCast(This, charPtrTy); 1503 assert(Adjustment.isPositive()); 1504 This = CGF.Builder.CreateConstInBoundsGEP1_32(CGF.Int8Ty, This, 1505 -Adjustment.getQuantity()); 1506 This = CGF.Builder.CreateBitCast(This, thisTy, "this.adjusted"); 1507 } 1508 } 1509 setCXXABIThisValue(CGF, This); 1510 1511 // If this is a function that the ABI specifies returns 'this', initialize 1512 // the return slot to 'this' at the start of the function. 1513 // 1514 // Unlike the setting of return types, this is done within the ABI 1515 // implementation instead of by clients of CGCXXABI because: 1516 // 1) getThisValue is currently protected 1517 // 2) in theory, an ABI could implement 'this' returns some other way; 1518 // HasThisReturn only specifies a contract, not the implementation 1519 if (HasThisReturn(CGF.CurGD)) 1520 CGF.Builder.CreateStore(getThisValue(CGF), CGF.ReturnValue); 1521 else if (hasMostDerivedReturn(CGF.CurGD)) 1522 CGF.Builder.CreateStore(CGF.EmitCastToVoidPtr(getThisValue(CGF)), 1523 CGF.ReturnValue); 1524 1525 if (isa<CXXConstructorDecl>(MD) && MD->getParent()->getNumVBases()) { 1526 assert(getStructorImplicitParamDecl(CGF) && 1527 "no implicit parameter for a constructor with virtual bases?"); 1528 getStructorImplicitParamValue(CGF) 1529 = CGF.Builder.CreateLoad( 1530 CGF.GetAddrOfLocalVar(getStructorImplicitParamDecl(CGF)), 1531 "is_most_derived"); 1532 } 1533 1534 if (isDeletingDtor(CGF.CurGD)) { 1535 assert(getStructorImplicitParamDecl(CGF) && 1536 "no implicit parameter for a deleting destructor?"); 1537 getStructorImplicitParamValue(CGF) 1538 = CGF.Builder.CreateLoad( 1539 CGF.GetAddrOfLocalVar(getStructorImplicitParamDecl(CGF)), 1540 "should_call_delete"); 1541 } 1542 } 1543 1544 CGCXXABI::AddedStructorArgs MicrosoftCXXABI::addImplicitConstructorArgs( 1545 CodeGenFunction &CGF, const CXXConstructorDecl *D, CXXCtorType Type, 1546 bool ForVirtualBase, bool Delegating, CallArgList &Args) { 1547 assert(Type == Ctor_Complete || Type == Ctor_Base); 1548 1549 // Check if we need a 'most_derived' parameter. 1550 if (!D->getParent()->getNumVBases()) 1551 return AddedStructorArgs{}; 1552 1553 // Add the 'most_derived' argument second if we are variadic or last if not. 1554 const FunctionProtoType *FPT = D->getType()->castAs<FunctionProtoType>(); 1555 llvm::Value *MostDerivedArg; 1556 if (Delegating) { 1557 MostDerivedArg = getStructorImplicitParamValue(CGF); 1558 } else { 1559 MostDerivedArg = llvm::ConstantInt::get(CGM.Int32Ty, Type == Ctor_Complete); 1560 } 1561 RValue RV = RValue::get(MostDerivedArg); 1562 if (FPT->isVariadic()) { 1563 Args.insert(Args.begin() + 1, CallArg(RV, getContext().IntTy)); 1564 return AddedStructorArgs::prefix(1); 1565 } 1566 Args.add(RV, getContext().IntTy); 1567 return AddedStructorArgs::suffix(1); 1568 } 1569 1570 void MicrosoftCXXABI::EmitDestructorCall(CodeGenFunction &CGF, 1571 const CXXDestructorDecl *DD, 1572 CXXDtorType Type, bool ForVirtualBase, 1573 bool Delegating, Address This, 1574 QualType ThisTy) { 1575 // Use the base destructor variant in place of the complete destructor variant 1576 // if the class has no virtual bases. This effectively implements some of the 1577 // -mconstructor-aliases optimization, but as part of the MS C++ ABI. 1578 if (Type == Dtor_Complete && DD->getParent()->getNumVBases() == 0) 1579 Type = Dtor_Base; 1580 1581 GlobalDecl GD(DD, Type); 1582 CGCallee Callee = CGCallee::forDirect(CGM.getAddrOfCXXStructor(GD), GD); 1583 1584 if (DD->isVirtual()) { 1585 assert(Type != CXXDtorType::Dtor_Deleting && 1586 "The deleting destructor should only be called via a virtual call"); 1587 This = adjustThisArgumentForVirtualFunctionCall(CGF, GlobalDecl(DD, Type), 1588 This, false); 1589 } 1590 1591 llvm::BasicBlock *BaseDtorEndBB = nullptr; 1592 if (ForVirtualBase && isa<CXXConstructorDecl>(CGF.CurCodeDecl)) { 1593 BaseDtorEndBB = EmitDtorCompleteObjectHandler(CGF); 1594 } 1595 1596 CGF.EmitCXXDestructorCall(GD, Callee, This.getPointer(), ThisTy, 1597 /*ImplicitParam=*/nullptr, 1598 /*ImplicitParamTy=*/QualType(), nullptr); 1599 if (BaseDtorEndBB) { 1600 // Complete object handler should continue to be the remaining 1601 CGF.Builder.CreateBr(BaseDtorEndBB); 1602 CGF.EmitBlock(BaseDtorEndBB); 1603 } 1604 } 1605 1606 void MicrosoftCXXABI::emitVTableTypeMetadata(const VPtrInfo &Info, 1607 const CXXRecordDecl *RD, 1608 llvm::GlobalVariable *VTable) { 1609 if (!CGM.getCodeGenOpts().LTOUnit) 1610 return; 1611 1612 // The location of the first virtual function pointer in the virtual table, 1613 // aka the "address point" on Itanium. This is at offset 0 if RTTI is 1614 // disabled, or sizeof(void*) if RTTI is enabled. 1615 CharUnits AddressPoint = 1616 getContext().getLangOpts().RTTIData 1617 ? getContext().toCharUnitsFromBits( 1618 getContext().getTargetInfo().getPointerWidth(0)) 1619 : CharUnits::Zero(); 1620 1621 if (Info.PathToIntroducingObject.empty()) { 1622 CGM.AddVTableTypeMetadata(VTable, AddressPoint, RD); 1623 return; 1624 } 1625 1626 // Add a bitset entry for the least derived base belonging to this vftable. 1627 CGM.AddVTableTypeMetadata(VTable, AddressPoint, 1628 Info.PathToIntroducingObject.back()); 1629 1630 // Add a bitset entry for each derived class that is laid out at the same 1631 // offset as the least derived base. 1632 for (unsigned I = Info.PathToIntroducingObject.size() - 1; I != 0; --I) { 1633 const CXXRecordDecl *DerivedRD = Info.PathToIntroducingObject[I - 1]; 1634 const CXXRecordDecl *BaseRD = Info.PathToIntroducingObject[I]; 1635 1636 const ASTRecordLayout &Layout = 1637 getContext().getASTRecordLayout(DerivedRD); 1638 CharUnits Offset; 1639 auto VBI = Layout.getVBaseOffsetsMap().find(BaseRD); 1640 if (VBI == Layout.getVBaseOffsetsMap().end()) 1641 Offset = Layout.getBaseClassOffset(BaseRD); 1642 else 1643 Offset = VBI->second.VBaseOffset; 1644 if (!Offset.isZero()) 1645 return; 1646 CGM.AddVTableTypeMetadata(VTable, AddressPoint, DerivedRD); 1647 } 1648 1649 // Finally do the same for the most derived class. 1650 if (Info.FullOffsetInMDC.isZero()) 1651 CGM.AddVTableTypeMetadata(VTable, AddressPoint, RD); 1652 } 1653 1654 void MicrosoftCXXABI::emitVTableDefinitions(CodeGenVTables &CGVT, 1655 const CXXRecordDecl *RD) { 1656 MicrosoftVTableContext &VFTContext = CGM.getMicrosoftVTableContext(); 1657 const VPtrInfoVector &VFPtrs = VFTContext.getVFPtrOffsets(RD); 1658 1659 for (const std::unique_ptr<VPtrInfo>& Info : VFPtrs) { 1660 llvm::GlobalVariable *VTable = getAddrOfVTable(RD, Info->FullOffsetInMDC); 1661 if (VTable->hasInitializer()) 1662 continue; 1663 1664 const VTableLayout &VTLayout = 1665 VFTContext.getVFTableLayout(RD, Info->FullOffsetInMDC); 1666 1667 llvm::Constant *RTTI = nullptr; 1668 if (any_of(VTLayout.vtable_components(), 1669 [](const VTableComponent &VTC) { return VTC.isRTTIKind(); })) 1670 RTTI = getMSCompleteObjectLocator(RD, *Info); 1671 1672 ConstantInitBuilder Builder(CGM); 1673 auto Components = Builder.beginStruct(); 1674 CGVT.createVTableInitializer(Components, VTLayout, RTTI); 1675 Components.finishAndSetAsInitializer(VTable); 1676 1677 emitVTableTypeMetadata(*Info, RD, VTable); 1678 } 1679 } 1680 1681 bool MicrosoftCXXABI::isVirtualOffsetNeededForVTableField( 1682 CodeGenFunction &CGF, CodeGenFunction::VPtr Vptr) { 1683 return Vptr.NearestVBase != nullptr; 1684 } 1685 1686 llvm::Value *MicrosoftCXXABI::getVTableAddressPointInStructor( 1687 CodeGenFunction &CGF, const CXXRecordDecl *VTableClass, BaseSubobject Base, 1688 const CXXRecordDecl *NearestVBase) { 1689 llvm::Constant *VTableAddressPoint = getVTableAddressPoint(Base, VTableClass); 1690 if (!VTableAddressPoint) { 1691 assert(Base.getBase()->getNumVBases() && 1692 !getContext().getASTRecordLayout(Base.getBase()).hasOwnVFPtr()); 1693 } 1694 return VTableAddressPoint; 1695 } 1696 1697 static void mangleVFTableName(MicrosoftMangleContext &MangleContext, 1698 const CXXRecordDecl *RD, const VPtrInfo &VFPtr, 1699 SmallString<256> &Name) { 1700 llvm::raw_svector_ostream Out(Name); 1701 MangleContext.mangleCXXVFTable(RD, VFPtr.MangledPath, Out); 1702 } 1703 1704 llvm::Constant * 1705 MicrosoftCXXABI::getVTableAddressPoint(BaseSubobject Base, 1706 const CXXRecordDecl *VTableClass) { 1707 (void)getAddrOfVTable(VTableClass, Base.getBaseOffset()); 1708 VFTableIdTy ID(VTableClass, Base.getBaseOffset()); 1709 return VFTablesMap[ID]; 1710 } 1711 1712 llvm::Constant *MicrosoftCXXABI::getVTableAddressPointForConstExpr( 1713 BaseSubobject Base, const CXXRecordDecl *VTableClass) { 1714 llvm::Constant *VFTable = getVTableAddressPoint(Base, VTableClass); 1715 assert(VFTable && "Couldn't find a vftable for the given base?"); 1716 return VFTable; 1717 } 1718 1719 llvm::GlobalVariable *MicrosoftCXXABI::getAddrOfVTable(const CXXRecordDecl *RD, 1720 CharUnits VPtrOffset) { 1721 // getAddrOfVTable may return 0 if asked to get an address of a vtable which 1722 // shouldn't be used in the given record type. We want to cache this result in 1723 // VFTablesMap, thus a simple zero check is not sufficient. 1724 1725 VFTableIdTy ID(RD, VPtrOffset); 1726 VTablesMapTy::iterator I; 1727 bool Inserted; 1728 std::tie(I, Inserted) = VTablesMap.insert(std::make_pair(ID, nullptr)); 1729 if (!Inserted) 1730 return I->second; 1731 1732 llvm::GlobalVariable *&VTable = I->second; 1733 1734 MicrosoftVTableContext &VTContext = CGM.getMicrosoftVTableContext(); 1735 const VPtrInfoVector &VFPtrs = VTContext.getVFPtrOffsets(RD); 1736 1737 if (DeferredVFTables.insert(RD).second) { 1738 // We haven't processed this record type before. 1739 // Queue up this vtable for possible deferred emission. 1740 CGM.addDeferredVTable(RD); 1741 1742 #ifndef NDEBUG 1743 // Create all the vftables at once in order to make sure each vftable has 1744 // a unique mangled name. 1745 llvm::StringSet<> ObservedMangledNames; 1746 for (size_t J = 0, F = VFPtrs.size(); J != F; ++J) { 1747 SmallString<256> Name; 1748 mangleVFTableName(getMangleContext(), RD, *VFPtrs[J], Name); 1749 if (!ObservedMangledNames.insert(Name.str()).second) 1750 llvm_unreachable("Already saw this mangling before?"); 1751 } 1752 #endif 1753 } 1754 1755 const std::unique_ptr<VPtrInfo> *VFPtrI = std::find_if( 1756 VFPtrs.begin(), VFPtrs.end(), [&](const std::unique_ptr<VPtrInfo>& VPI) { 1757 return VPI->FullOffsetInMDC == VPtrOffset; 1758 }); 1759 if (VFPtrI == VFPtrs.end()) { 1760 VFTablesMap[ID] = nullptr; 1761 return nullptr; 1762 } 1763 const std::unique_ptr<VPtrInfo> &VFPtr = *VFPtrI; 1764 1765 SmallString<256> VFTableName; 1766 mangleVFTableName(getMangleContext(), RD, *VFPtr, VFTableName); 1767 1768 // Classes marked __declspec(dllimport) need vftables generated on the 1769 // import-side in order to support features like constexpr. No other 1770 // translation unit relies on the emission of the local vftable, translation 1771 // units are expected to generate them as needed. 1772 // 1773 // Because of this unique behavior, we maintain this logic here instead of 1774 // getVTableLinkage. 1775 llvm::GlobalValue::LinkageTypes VFTableLinkage = 1776 RD->hasAttr<DLLImportAttr>() ? llvm::GlobalValue::LinkOnceODRLinkage 1777 : CGM.getVTableLinkage(RD); 1778 bool VFTableComesFromAnotherTU = 1779 llvm::GlobalValue::isAvailableExternallyLinkage(VFTableLinkage) || 1780 llvm::GlobalValue::isExternalLinkage(VFTableLinkage); 1781 bool VTableAliasIsRequred = 1782 !VFTableComesFromAnotherTU && getContext().getLangOpts().RTTIData; 1783 1784 if (llvm::GlobalValue *VFTable = 1785 CGM.getModule().getNamedGlobal(VFTableName)) { 1786 VFTablesMap[ID] = VFTable; 1787 VTable = VTableAliasIsRequred 1788 ? cast<llvm::GlobalVariable>( 1789 cast<llvm::GlobalAlias>(VFTable)->getBaseObject()) 1790 : cast<llvm::GlobalVariable>(VFTable); 1791 return VTable; 1792 } 1793 1794 const VTableLayout &VTLayout = 1795 VTContext.getVFTableLayout(RD, VFPtr->FullOffsetInMDC); 1796 llvm::GlobalValue::LinkageTypes VTableLinkage = 1797 VTableAliasIsRequred ? llvm::GlobalValue::PrivateLinkage : VFTableLinkage; 1798 1799 StringRef VTableName = VTableAliasIsRequred ? StringRef() : VFTableName.str(); 1800 1801 llvm::Type *VTableType = CGM.getVTables().getVTableType(VTLayout); 1802 1803 // Create a backing variable for the contents of VTable. The VTable may 1804 // or may not include space for a pointer to RTTI data. 1805 llvm::GlobalValue *VFTable; 1806 VTable = new llvm::GlobalVariable(CGM.getModule(), VTableType, 1807 /*isConstant=*/true, VTableLinkage, 1808 /*Initializer=*/nullptr, VTableName); 1809 VTable->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); 1810 1811 llvm::Comdat *C = nullptr; 1812 if (!VFTableComesFromAnotherTU && 1813 (llvm::GlobalValue::isWeakForLinker(VFTableLinkage) || 1814 (llvm::GlobalValue::isLocalLinkage(VFTableLinkage) && 1815 VTableAliasIsRequred))) 1816 C = CGM.getModule().getOrInsertComdat(VFTableName.str()); 1817 1818 // Only insert a pointer into the VFTable for RTTI data if we are not 1819 // importing it. We never reference the RTTI data directly so there is no 1820 // need to make room for it. 1821 if (VTableAliasIsRequred) { 1822 llvm::Value *GEPIndices[] = {llvm::ConstantInt::get(CGM.Int32Ty, 0), 1823 llvm::ConstantInt::get(CGM.Int32Ty, 0), 1824 llvm::ConstantInt::get(CGM.Int32Ty, 1)}; 1825 // Create a GEP which points just after the first entry in the VFTable, 1826 // this should be the location of the first virtual method. 1827 llvm::Constant *VTableGEP = llvm::ConstantExpr::getInBoundsGetElementPtr( 1828 VTable->getValueType(), VTable, GEPIndices); 1829 if (llvm::GlobalValue::isWeakForLinker(VFTableLinkage)) { 1830 VFTableLinkage = llvm::GlobalValue::ExternalLinkage; 1831 if (C) 1832 C->setSelectionKind(llvm::Comdat::Largest); 1833 } 1834 VFTable = llvm::GlobalAlias::create(CGM.Int8PtrTy, 1835 /*AddressSpace=*/0, VFTableLinkage, 1836 VFTableName.str(), VTableGEP, 1837 &CGM.getModule()); 1838 VFTable->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); 1839 } else { 1840 // We don't need a GlobalAlias to be a symbol for the VTable if we won't 1841 // be referencing any RTTI data. 1842 // The GlobalVariable will end up being an appropriate definition of the 1843 // VFTable. 1844 VFTable = VTable; 1845 } 1846 if (C) 1847 VTable->setComdat(C); 1848 1849 if (RD->hasAttr<DLLExportAttr>()) 1850 VFTable->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass); 1851 1852 VFTablesMap[ID] = VFTable; 1853 return VTable; 1854 } 1855 1856 CGCallee MicrosoftCXXABI::getVirtualFunctionPointer(CodeGenFunction &CGF, 1857 GlobalDecl GD, 1858 Address This, 1859 llvm::Type *Ty, 1860 SourceLocation Loc) { 1861 CGBuilderTy &Builder = CGF.Builder; 1862 1863 Ty = Ty->getPointerTo()->getPointerTo(); 1864 Address VPtr = 1865 adjustThisArgumentForVirtualFunctionCall(CGF, GD, This, true); 1866 1867 auto *MethodDecl = cast<CXXMethodDecl>(GD.getDecl()); 1868 llvm::Value *VTable = CGF.GetVTablePtr(VPtr, Ty, MethodDecl->getParent()); 1869 1870 MicrosoftVTableContext &VFTContext = CGM.getMicrosoftVTableContext(); 1871 MethodVFTableLocation ML = VFTContext.getMethodVFTableLocation(GD); 1872 1873 // Compute the identity of the most derived class whose virtual table is 1874 // located at the MethodVFTableLocation ML. 1875 auto getObjectWithVPtr = [&] { 1876 return llvm::find_if(VFTContext.getVFPtrOffsets( 1877 ML.VBase ? ML.VBase : MethodDecl->getParent()), 1878 [&](const std::unique_ptr<VPtrInfo> &Info) { 1879 return Info->FullOffsetInMDC == ML.VFPtrOffset; 1880 }) 1881 ->get() 1882 ->ObjectWithVPtr; 1883 }; 1884 1885 llvm::Value *VFunc; 1886 if (CGF.ShouldEmitVTableTypeCheckedLoad(MethodDecl->getParent())) { 1887 VFunc = CGF.EmitVTableTypeCheckedLoad( 1888 getObjectWithVPtr(), VTable, 1889 ML.Index * CGM.getContext().getTargetInfo().getPointerWidth(0) / 8); 1890 } else { 1891 if (CGM.getCodeGenOpts().PrepareForLTO) 1892 CGF.EmitTypeMetadataCodeForVCall(getObjectWithVPtr(), VTable, Loc); 1893 1894 llvm::Value *VFuncPtr = 1895 Builder.CreateConstInBoundsGEP1_64(VTable, ML.Index, "vfn"); 1896 VFunc = Builder.CreateAlignedLoad(VFuncPtr, CGF.getPointerAlign()); 1897 } 1898 1899 CGCallee Callee(GD, VFunc); 1900 return Callee; 1901 } 1902 1903 llvm::Value *MicrosoftCXXABI::EmitVirtualDestructorCall( 1904 CodeGenFunction &CGF, const CXXDestructorDecl *Dtor, CXXDtorType DtorType, 1905 Address This, DeleteOrMemberCallExpr E) { 1906 auto *CE = E.dyn_cast<const CXXMemberCallExpr *>(); 1907 auto *D = E.dyn_cast<const CXXDeleteExpr *>(); 1908 assert((CE != nullptr) ^ (D != nullptr)); 1909 assert(CE == nullptr || CE->arg_begin() == CE->arg_end()); 1910 assert(DtorType == Dtor_Deleting || DtorType == Dtor_Complete); 1911 1912 // We have only one destructor in the vftable but can get both behaviors 1913 // by passing an implicit int parameter. 1914 GlobalDecl GD(Dtor, Dtor_Deleting); 1915 const CGFunctionInfo *FInfo = 1916 &CGM.getTypes().arrangeCXXStructorDeclaration(GD); 1917 llvm::FunctionType *Ty = CGF.CGM.getTypes().GetFunctionType(*FInfo); 1918 CGCallee Callee = CGCallee::forVirtual(CE, GD, This, Ty); 1919 1920 ASTContext &Context = getContext(); 1921 llvm::Value *ImplicitParam = llvm::ConstantInt::get( 1922 llvm::IntegerType::getInt32Ty(CGF.getLLVMContext()), 1923 DtorType == Dtor_Deleting); 1924 1925 QualType ThisTy; 1926 if (CE) { 1927 ThisTy = CE->getObjectType(); 1928 } else { 1929 ThisTy = D->getDestroyedType(); 1930 } 1931 1932 This = adjustThisArgumentForVirtualFunctionCall(CGF, GD, This, true); 1933 RValue RV = CGF.EmitCXXDestructorCall(GD, Callee, This.getPointer(), ThisTy, 1934 ImplicitParam, Context.IntTy, CE); 1935 return RV.getScalarVal(); 1936 } 1937 1938 const VBTableGlobals & 1939 MicrosoftCXXABI::enumerateVBTables(const CXXRecordDecl *RD) { 1940 // At this layer, we can key the cache off of a single class, which is much 1941 // easier than caching each vbtable individually. 1942 llvm::DenseMap<const CXXRecordDecl*, VBTableGlobals>::iterator Entry; 1943 bool Added; 1944 std::tie(Entry, Added) = 1945 VBTablesMap.insert(std::make_pair(RD, VBTableGlobals())); 1946 VBTableGlobals &VBGlobals = Entry->second; 1947 if (!Added) 1948 return VBGlobals; 1949 1950 MicrosoftVTableContext &Context = CGM.getMicrosoftVTableContext(); 1951 VBGlobals.VBTables = &Context.enumerateVBTables(RD); 1952 1953 // Cache the globals for all vbtables so we don't have to recompute the 1954 // mangled names. 1955 llvm::GlobalVariable::LinkageTypes Linkage = CGM.getVTableLinkage(RD); 1956 for (VPtrInfoVector::const_iterator I = VBGlobals.VBTables->begin(), 1957 E = VBGlobals.VBTables->end(); 1958 I != E; ++I) { 1959 VBGlobals.Globals.push_back(getAddrOfVBTable(**I, RD, Linkage)); 1960 } 1961 1962 return VBGlobals; 1963 } 1964 1965 llvm::Function * 1966 MicrosoftCXXABI::EmitVirtualMemPtrThunk(const CXXMethodDecl *MD, 1967 const MethodVFTableLocation &ML) { 1968 assert(!isa<CXXConstructorDecl>(MD) && !isa<CXXDestructorDecl>(MD) && 1969 "can't form pointers to ctors or virtual dtors"); 1970 1971 // Calculate the mangled name. 1972 SmallString<256> ThunkName; 1973 llvm::raw_svector_ostream Out(ThunkName); 1974 getMangleContext().mangleVirtualMemPtrThunk(MD, ML, Out); 1975 1976 // If the thunk has been generated previously, just return it. 1977 if (llvm::GlobalValue *GV = CGM.getModule().getNamedValue(ThunkName)) 1978 return cast<llvm::Function>(GV); 1979 1980 // Create the llvm::Function. 1981 const CGFunctionInfo &FnInfo = 1982 CGM.getTypes().arrangeUnprototypedMustTailThunk(MD); 1983 llvm::FunctionType *ThunkTy = CGM.getTypes().GetFunctionType(FnInfo); 1984 llvm::Function *ThunkFn = 1985 llvm::Function::Create(ThunkTy, llvm::Function::ExternalLinkage, 1986 ThunkName.str(), &CGM.getModule()); 1987 assert(ThunkFn->getName() == ThunkName && "name was uniqued!"); 1988 1989 ThunkFn->setLinkage(MD->isExternallyVisible() 1990 ? llvm::GlobalValue::LinkOnceODRLinkage 1991 : llvm::GlobalValue::InternalLinkage); 1992 if (MD->isExternallyVisible()) 1993 ThunkFn->setComdat(CGM.getModule().getOrInsertComdat(ThunkFn->getName())); 1994 1995 CGM.SetLLVMFunctionAttributes(MD, FnInfo, ThunkFn); 1996 CGM.SetLLVMFunctionAttributesForDefinition(MD, ThunkFn); 1997 1998 // Add the "thunk" attribute so that LLVM knows that the return type is 1999 // meaningless. These thunks can be used to call functions with differing 2000 // return types, and the caller is required to cast the prototype 2001 // appropriately to extract the correct value. 2002 ThunkFn->addFnAttr("thunk"); 2003 2004 // These thunks can be compared, so they are not unnamed. 2005 ThunkFn->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::None); 2006 2007 // Start codegen. 2008 CodeGenFunction CGF(CGM); 2009 CGF.CurGD = GlobalDecl(MD); 2010 CGF.CurFuncIsThunk = true; 2011 2012 // Build FunctionArgs, but only include the implicit 'this' parameter 2013 // declaration. 2014 FunctionArgList FunctionArgs; 2015 buildThisParam(CGF, FunctionArgs); 2016 2017 // Start defining the function. 2018 CGF.StartFunction(GlobalDecl(), FnInfo.getReturnType(), ThunkFn, FnInfo, 2019 FunctionArgs, MD->getLocation(), SourceLocation()); 2020 setCXXABIThisValue(CGF, loadIncomingCXXThis(CGF)); 2021 2022 // Load the vfptr and then callee from the vftable. The callee should have 2023 // adjusted 'this' so that the vfptr is at offset zero. 2024 llvm::Value *VTable = CGF.GetVTablePtr( 2025 getThisAddress(CGF), ThunkTy->getPointerTo()->getPointerTo(), MD->getParent()); 2026 2027 llvm::Value *VFuncPtr = 2028 CGF.Builder.CreateConstInBoundsGEP1_64(VTable, ML.Index, "vfn"); 2029 llvm::Value *Callee = 2030 CGF.Builder.CreateAlignedLoad(VFuncPtr, CGF.getPointerAlign()); 2031 2032 CGF.EmitMustTailThunk(MD, getThisValue(CGF), {ThunkTy, Callee}); 2033 2034 return ThunkFn; 2035 } 2036 2037 void MicrosoftCXXABI::emitVirtualInheritanceTables(const CXXRecordDecl *RD) { 2038 const VBTableGlobals &VBGlobals = enumerateVBTables(RD); 2039 for (unsigned I = 0, E = VBGlobals.VBTables->size(); I != E; ++I) { 2040 const std::unique_ptr<VPtrInfo>& VBT = (*VBGlobals.VBTables)[I]; 2041 llvm::GlobalVariable *GV = VBGlobals.Globals[I]; 2042 if (GV->isDeclaration()) 2043 emitVBTableDefinition(*VBT, RD, GV); 2044 } 2045 } 2046 2047 llvm::GlobalVariable * 2048 MicrosoftCXXABI::getAddrOfVBTable(const VPtrInfo &VBT, const CXXRecordDecl *RD, 2049 llvm::GlobalVariable::LinkageTypes Linkage) { 2050 SmallString<256> OutName; 2051 llvm::raw_svector_ostream Out(OutName); 2052 getMangleContext().mangleCXXVBTable(RD, VBT.MangledPath, Out); 2053 StringRef Name = OutName.str(); 2054 2055 llvm::ArrayType *VBTableType = 2056 llvm::ArrayType::get(CGM.IntTy, 1 + VBT.ObjectWithVPtr->getNumVBases()); 2057 2058 assert(!CGM.getModule().getNamedGlobal(Name) && 2059 "vbtable with this name already exists: mangling bug?"); 2060 CharUnits Alignment = 2061 CGM.getContext().getTypeAlignInChars(CGM.getContext().IntTy); 2062 llvm::GlobalVariable *GV = CGM.CreateOrReplaceCXXRuntimeVariable( 2063 Name, VBTableType, Linkage, Alignment.getQuantity()); 2064 GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); 2065 2066 if (RD->hasAttr<DLLImportAttr>()) 2067 GV->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass); 2068 else if (RD->hasAttr<DLLExportAttr>()) 2069 GV->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass); 2070 2071 if (!GV->hasExternalLinkage()) 2072 emitVBTableDefinition(VBT, RD, GV); 2073 2074 return GV; 2075 } 2076 2077 void MicrosoftCXXABI::emitVBTableDefinition(const VPtrInfo &VBT, 2078 const CXXRecordDecl *RD, 2079 llvm::GlobalVariable *GV) const { 2080 const CXXRecordDecl *ObjectWithVPtr = VBT.ObjectWithVPtr; 2081 2082 assert(RD->getNumVBases() && ObjectWithVPtr->getNumVBases() && 2083 "should only emit vbtables for classes with vbtables"); 2084 2085 const ASTRecordLayout &BaseLayout = 2086 getContext().getASTRecordLayout(VBT.IntroducingObject); 2087 const ASTRecordLayout &DerivedLayout = getContext().getASTRecordLayout(RD); 2088 2089 SmallVector<llvm::Constant *, 4> Offsets(1 + ObjectWithVPtr->getNumVBases(), 2090 nullptr); 2091 2092 // The offset from ObjectWithVPtr's vbptr to itself always leads. 2093 CharUnits VBPtrOffset = BaseLayout.getVBPtrOffset(); 2094 Offsets[0] = llvm::ConstantInt::get(CGM.IntTy, -VBPtrOffset.getQuantity()); 2095 2096 MicrosoftVTableContext &Context = CGM.getMicrosoftVTableContext(); 2097 for (const auto &I : ObjectWithVPtr->vbases()) { 2098 const CXXRecordDecl *VBase = I.getType()->getAsCXXRecordDecl(); 2099 CharUnits Offset = DerivedLayout.getVBaseClassOffset(VBase); 2100 assert(!Offset.isNegative()); 2101 2102 // Make it relative to the subobject vbptr. 2103 CharUnits CompleteVBPtrOffset = VBT.NonVirtualOffset + VBPtrOffset; 2104 if (VBT.getVBaseWithVPtr()) 2105 CompleteVBPtrOffset += 2106 DerivedLayout.getVBaseClassOffset(VBT.getVBaseWithVPtr()); 2107 Offset -= CompleteVBPtrOffset; 2108 2109 unsigned VBIndex = Context.getVBTableIndex(ObjectWithVPtr, VBase); 2110 assert(Offsets[VBIndex] == nullptr && "The same vbindex seen twice?"); 2111 Offsets[VBIndex] = llvm::ConstantInt::get(CGM.IntTy, Offset.getQuantity()); 2112 } 2113 2114 assert(Offsets.size() == 2115 cast<llvm::ArrayType>(cast<llvm::PointerType>(GV->getType()) 2116 ->getElementType())->getNumElements()); 2117 llvm::ArrayType *VBTableType = 2118 llvm::ArrayType::get(CGM.IntTy, Offsets.size()); 2119 llvm::Constant *Init = llvm::ConstantArray::get(VBTableType, Offsets); 2120 GV->setInitializer(Init); 2121 2122 if (RD->hasAttr<DLLImportAttr>()) 2123 GV->setLinkage(llvm::GlobalVariable::AvailableExternallyLinkage); 2124 } 2125 2126 llvm::Value *MicrosoftCXXABI::performThisAdjustment(CodeGenFunction &CGF, 2127 Address This, 2128 const ThisAdjustment &TA) { 2129 if (TA.isEmpty()) 2130 return This.getPointer(); 2131 2132 This = CGF.Builder.CreateElementBitCast(This, CGF.Int8Ty); 2133 2134 llvm::Value *V; 2135 if (TA.Virtual.isEmpty()) { 2136 V = This.getPointer(); 2137 } else { 2138 assert(TA.Virtual.Microsoft.VtordispOffset < 0); 2139 // Adjust the this argument based on the vtordisp value. 2140 Address VtorDispPtr = 2141 CGF.Builder.CreateConstInBoundsByteGEP(This, 2142 CharUnits::fromQuantity(TA.Virtual.Microsoft.VtordispOffset)); 2143 VtorDispPtr = CGF.Builder.CreateElementBitCast(VtorDispPtr, CGF.Int32Ty); 2144 llvm::Value *VtorDisp = CGF.Builder.CreateLoad(VtorDispPtr, "vtordisp"); 2145 V = CGF.Builder.CreateGEP(This.getPointer(), 2146 CGF.Builder.CreateNeg(VtorDisp)); 2147 2148 // Unfortunately, having applied the vtordisp means that we no 2149 // longer really have a known alignment for the vbptr step. 2150 // We'll assume the vbptr is pointer-aligned. 2151 2152 if (TA.Virtual.Microsoft.VBPtrOffset) { 2153 // If the final overrider is defined in a virtual base other than the one 2154 // that holds the vfptr, we have to use a vtordispex thunk which looks up 2155 // the vbtable of the derived class. 2156 assert(TA.Virtual.Microsoft.VBPtrOffset > 0); 2157 assert(TA.Virtual.Microsoft.VBOffsetOffset >= 0); 2158 llvm::Value *VBPtr; 2159 llvm::Value *VBaseOffset = 2160 GetVBaseOffsetFromVBPtr(CGF, Address(V, CGF.getPointerAlign()), 2161 -TA.Virtual.Microsoft.VBPtrOffset, 2162 TA.Virtual.Microsoft.VBOffsetOffset, &VBPtr); 2163 V = CGF.Builder.CreateInBoundsGEP(VBPtr, VBaseOffset); 2164 } 2165 } 2166 2167 if (TA.NonVirtual) { 2168 // Non-virtual adjustment might result in a pointer outside the allocated 2169 // object, e.g. if the final overrider class is laid out after the virtual 2170 // base that declares a method in the most derived class. 2171 V = CGF.Builder.CreateConstGEP1_32(V, TA.NonVirtual); 2172 } 2173 2174 // Don't need to bitcast back, the call CodeGen will handle this. 2175 return V; 2176 } 2177 2178 llvm::Value * 2179 MicrosoftCXXABI::performReturnAdjustment(CodeGenFunction &CGF, Address Ret, 2180 const ReturnAdjustment &RA) { 2181 if (RA.isEmpty()) 2182 return Ret.getPointer(); 2183 2184 auto OrigTy = Ret.getType(); 2185 Ret = CGF.Builder.CreateElementBitCast(Ret, CGF.Int8Ty); 2186 2187 llvm::Value *V = Ret.getPointer(); 2188 if (RA.Virtual.Microsoft.VBIndex) { 2189 assert(RA.Virtual.Microsoft.VBIndex > 0); 2190 int32_t IntSize = CGF.getIntSize().getQuantity(); 2191 llvm::Value *VBPtr; 2192 llvm::Value *VBaseOffset = 2193 GetVBaseOffsetFromVBPtr(CGF, Ret, RA.Virtual.Microsoft.VBPtrOffset, 2194 IntSize * RA.Virtual.Microsoft.VBIndex, &VBPtr); 2195 V = CGF.Builder.CreateInBoundsGEP(VBPtr, VBaseOffset); 2196 } 2197 2198 if (RA.NonVirtual) 2199 V = CGF.Builder.CreateConstInBoundsGEP1_32(CGF.Int8Ty, V, RA.NonVirtual); 2200 2201 // Cast back to the original type. 2202 return CGF.Builder.CreateBitCast(V, OrigTy); 2203 } 2204 2205 bool MicrosoftCXXABI::requiresArrayCookie(const CXXDeleteExpr *expr, 2206 QualType elementType) { 2207 // Microsoft seems to completely ignore the possibility of a 2208 // two-argument usual deallocation function. 2209 return elementType.isDestructedType(); 2210 } 2211 2212 bool MicrosoftCXXABI::requiresArrayCookie(const CXXNewExpr *expr) { 2213 // Microsoft seems to completely ignore the possibility of a 2214 // two-argument usual deallocation function. 2215 return expr->getAllocatedType().isDestructedType(); 2216 } 2217 2218 CharUnits MicrosoftCXXABI::getArrayCookieSizeImpl(QualType type) { 2219 // The array cookie is always a size_t; we then pad that out to the 2220 // alignment of the element type. 2221 ASTContext &Ctx = getContext(); 2222 return std::max(Ctx.getTypeSizeInChars(Ctx.getSizeType()), 2223 Ctx.getTypeAlignInChars(type)); 2224 } 2225 2226 llvm::Value *MicrosoftCXXABI::readArrayCookieImpl(CodeGenFunction &CGF, 2227 Address allocPtr, 2228 CharUnits cookieSize) { 2229 Address numElementsPtr = 2230 CGF.Builder.CreateElementBitCast(allocPtr, CGF.SizeTy); 2231 return CGF.Builder.CreateLoad(numElementsPtr); 2232 } 2233 2234 Address MicrosoftCXXABI::InitializeArrayCookie(CodeGenFunction &CGF, 2235 Address newPtr, 2236 llvm::Value *numElements, 2237 const CXXNewExpr *expr, 2238 QualType elementType) { 2239 assert(requiresArrayCookie(expr)); 2240 2241 // The size of the cookie. 2242 CharUnits cookieSize = getArrayCookieSizeImpl(elementType); 2243 2244 // Compute an offset to the cookie. 2245 Address cookiePtr = newPtr; 2246 2247 // Write the number of elements into the appropriate slot. 2248 Address numElementsPtr 2249 = CGF.Builder.CreateElementBitCast(cookiePtr, CGF.SizeTy); 2250 CGF.Builder.CreateStore(numElements, numElementsPtr); 2251 2252 // Finally, compute a pointer to the actual data buffer by skipping 2253 // over the cookie completely. 2254 return CGF.Builder.CreateConstInBoundsByteGEP(newPtr, cookieSize); 2255 } 2256 2257 static void emitGlobalDtorWithTLRegDtor(CodeGenFunction &CGF, const VarDecl &VD, 2258 llvm::FunctionCallee Dtor, 2259 llvm::Constant *Addr) { 2260 // Create a function which calls the destructor. 2261 llvm::Constant *DtorStub = CGF.createAtExitStub(VD, Dtor, Addr); 2262 2263 // extern "C" int __tlregdtor(void (*f)(void)); 2264 llvm::FunctionType *TLRegDtorTy = llvm::FunctionType::get( 2265 CGF.IntTy, DtorStub->getType(), /*isVarArg=*/false); 2266 2267 llvm::FunctionCallee TLRegDtor = CGF.CGM.CreateRuntimeFunction( 2268 TLRegDtorTy, "__tlregdtor", llvm::AttributeList(), /*Local=*/true); 2269 if (llvm::Function *TLRegDtorFn = 2270 dyn_cast<llvm::Function>(TLRegDtor.getCallee())) 2271 TLRegDtorFn->setDoesNotThrow(); 2272 2273 CGF.EmitNounwindRuntimeCall(TLRegDtor, DtorStub); 2274 } 2275 2276 void MicrosoftCXXABI::registerGlobalDtor(CodeGenFunction &CGF, const VarDecl &D, 2277 llvm::FunctionCallee Dtor, 2278 llvm::Constant *Addr) { 2279 if (D.isNoDestroy(CGM.getContext())) 2280 return; 2281 2282 if (D.getTLSKind()) 2283 return emitGlobalDtorWithTLRegDtor(CGF, D, Dtor, Addr); 2284 2285 // The default behavior is to use atexit. 2286 CGF.registerGlobalDtorWithAtExit(D, Dtor, Addr); 2287 } 2288 2289 void MicrosoftCXXABI::EmitThreadLocalInitFuncs( 2290 CodeGenModule &CGM, ArrayRef<const VarDecl *> CXXThreadLocals, 2291 ArrayRef<llvm::Function *> CXXThreadLocalInits, 2292 ArrayRef<const VarDecl *> CXXThreadLocalInitVars) { 2293 if (CXXThreadLocalInits.empty()) 2294 return; 2295 2296 CGM.AppendLinkerOptions(CGM.getTarget().getTriple().getArch() == 2297 llvm::Triple::x86 2298 ? "/include:___dyn_tls_init@12" 2299 : "/include:__dyn_tls_init"); 2300 2301 // This will create a GV in the .CRT$XDU section. It will point to our 2302 // initialization function. The CRT will call all of these function 2303 // pointers at start-up time and, eventually, at thread-creation time. 2304 auto AddToXDU = [&CGM](llvm::Function *InitFunc) { 2305 llvm::GlobalVariable *InitFuncPtr = new llvm::GlobalVariable( 2306 CGM.getModule(), InitFunc->getType(), /*isConstant=*/true, 2307 llvm::GlobalVariable::InternalLinkage, InitFunc, 2308 Twine(InitFunc->getName(), "$initializer$")); 2309 InitFuncPtr->setSection(".CRT$XDU"); 2310 // This variable has discardable linkage, we have to add it to @llvm.used to 2311 // ensure it won't get discarded. 2312 CGM.addUsedGlobal(InitFuncPtr); 2313 return InitFuncPtr; 2314 }; 2315 2316 std::vector<llvm::Function *> NonComdatInits; 2317 for (size_t I = 0, E = CXXThreadLocalInitVars.size(); I != E; ++I) { 2318 llvm::GlobalVariable *GV = cast<llvm::GlobalVariable>( 2319 CGM.GetGlobalValue(CGM.getMangledName(CXXThreadLocalInitVars[I]))); 2320 llvm::Function *F = CXXThreadLocalInits[I]; 2321 2322 // If the GV is already in a comdat group, then we have to join it. 2323 if (llvm::Comdat *C = GV->getComdat()) 2324 AddToXDU(F)->setComdat(C); 2325 else 2326 NonComdatInits.push_back(F); 2327 } 2328 2329 if (!NonComdatInits.empty()) { 2330 llvm::FunctionType *FTy = 2331 llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false); 2332 llvm::Function *InitFunc = CGM.CreateGlobalInitOrDestructFunction( 2333 FTy, "__tls_init", CGM.getTypes().arrangeNullaryFunction(), 2334 SourceLocation(), /*TLS=*/true); 2335 CodeGenFunction(CGM).GenerateCXXGlobalInitFunc(InitFunc, NonComdatInits); 2336 2337 AddToXDU(InitFunc); 2338 } 2339 } 2340 2341 LValue MicrosoftCXXABI::EmitThreadLocalVarDeclLValue(CodeGenFunction &CGF, 2342 const VarDecl *VD, 2343 QualType LValType) { 2344 CGF.CGM.ErrorUnsupported(VD, "thread wrappers"); 2345 return LValue(); 2346 } 2347 2348 static ConstantAddress getInitThreadEpochPtr(CodeGenModule &CGM) { 2349 StringRef VarName("_Init_thread_epoch"); 2350 CharUnits Align = CGM.getIntAlign(); 2351 if (auto *GV = CGM.getModule().getNamedGlobal(VarName)) 2352 return ConstantAddress(GV, Align); 2353 auto *GV = new llvm::GlobalVariable( 2354 CGM.getModule(), CGM.IntTy, 2355 /*isConstant=*/false, llvm::GlobalVariable::ExternalLinkage, 2356 /*Initializer=*/nullptr, VarName, 2357 /*InsertBefore=*/nullptr, llvm::GlobalVariable::GeneralDynamicTLSModel); 2358 GV->setAlignment(Align.getAsAlign()); 2359 return ConstantAddress(GV, Align); 2360 } 2361 2362 static llvm::FunctionCallee getInitThreadHeaderFn(CodeGenModule &CGM) { 2363 llvm::FunctionType *FTy = 2364 llvm::FunctionType::get(llvm::Type::getVoidTy(CGM.getLLVMContext()), 2365 CGM.IntTy->getPointerTo(), /*isVarArg=*/false); 2366 return CGM.CreateRuntimeFunction( 2367 FTy, "_Init_thread_header", 2368 llvm::AttributeList::get(CGM.getLLVMContext(), 2369 llvm::AttributeList::FunctionIndex, 2370 llvm::Attribute::NoUnwind), 2371 /*Local=*/true); 2372 } 2373 2374 static llvm::FunctionCallee getInitThreadFooterFn(CodeGenModule &CGM) { 2375 llvm::FunctionType *FTy = 2376 llvm::FunctionType::get(llvm::Type::getVoidTy(CGM.getLLVMContext()), 2377 CGM.IntTy->getPointerTo(), /*isVarArg=*/false); 2378 return CGM.CreateRuntimeFunction( 2379 FTy, "_Init_thread_footer", 2380 llvm::AttributeList::get(CGM.getLLVMContext(), 2381 llvm::AttributeList::FunctionIndex, 2382 llvm::Attribute::NoUnwind), 2383 /*Local=*/true); 2384 } 2385 2386 static llvm::FunctionCallee getInitThreadAbortFn(CodeGenModule &CGM) { 2387 llvm::FunctionType *FTy = 2388 llvm::FunctionType::get(llvm::Type::getVoidTy(CGM.getLLVMContext()), 2389 CGM.IntTy->getPointerTo(), /*isVarArg=*/false); 2390 return CGM.CreateRuntimeFunction( 2391 FTy, "_Init_thread_abort", 2392 llvm::AttributeList::get(CGM.getLLVMContext(), 2393 llvm::AttributeList::FunctionIndex, 2394 llvm::Attribute::NoUnwind), 2395 /*Local=*/true); 2396 } 2397 2398 namespace { 2399 struct ResetGuardBit final : EHScopeStack::Cleanup { 2400 Address Guard; 2401 unsigned GuardNum; 2402 ResetGuardBit(Address Guard, unsigned GuardNum) 2403 : Guard(Guard), GuardNum(GuardNum) {} 2404 2405 void Emit(CodeGenFunction &CGF, Flags flags) override { 2406 // Reset the bit in the mask so that the static variable may be 2407 // reinitialized. 2408 CGBuilderTy &Builder = CGF.Builder; 2409 llvm::LoadInst *LI = Builder.CreateLoad(Guard); 2410 llvm::ConstantInt *Mask = 2411 llvm::ConstantInt::get(CGF.IntTy, ~(1ULL << GuardNum)); 2412 Builder.CreateStore(Builder.CreateAnd(LI, Mask), Guard); 2413 } 2414 }; 2415 2416 struct CallInitThreadAbort final : EHScopeStack::Cleanup { 2417 llvm::Value *Guard; 2418 CallInitThreadAbort(Address Guard) : Guard(Guard.getPointer()) {} 2419 2420 void Emit(CodeGenFunction &CGF, Flags flags) override { 2421 // Calling _Init_thread_abort will reset the guard's state. 2422 CGF.EmitNounwindRuntimeCall(getInitThreadAbortFn(CGF.CGM), Guard); 2423 } 2424 }; 2425 } 2426 2427 void MicrosoftCXXABI::EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D, 2428 llvm::GlobalVariable *GV, 2429 bool PerformInit) { 2430 // MSVC only uses guards for static locals. 2431 if (!D.isStaticLocal()) { 2432 assert(GV->hasWeakLinkage() || GV->hasLinkOnceLinkage()); 2433 // GlobalOpt is allowed to discard the initializer, so use linkonce_odr. 2434 llvm::Function *F = CGF.CurFn; 2435 F->setLinkage(llvm::GlobalValue::LinkOnceODRLinkage); 2436 F->setComdat(CGM.getModule().getOrInsertComdat(F->getName())); 2437 CGF.EmitCXXGlobalVarDeclInit(D, GV, PerformInit); 2438 return; 2439 } 2440 2441 bool ThreadlocalStatic = D.getTLSKind(); 2442 bool ThreadsafeStatic = getContext().getLangOpts().ThreadsafeStatics; 2443 2444 // Thread-safe static variables which aren't thread-specific have a 2445 // per-variable guard. 2446 bool HasPerVariableGuard = ThreadsafeStatic && !ThreadlocalStatic; 2447 2448 CGBuilderTy &Builder = CGF.Builder; 2449 llvm::IntegerType *GuardTy = CGF.Int32Ty; 2450 llvm::ConstantInt *Zero = llvm::ConstantInt::get(GuardTy, 0); 2451 CharUnits GuardAlign = CharUnits::fromQuantity(4); 2452 2453 // Get the guard variable for this function if we have one already. 2454 GuardInfo *GI = nullptr; 2455 if (ThreadlocalStatic) 2456 GI = &ThreadLocalGuardVariableMap[D.getDeclContext()]; 2457 else if (!ThreadsafeStatic) 2458 GI = &GuardVariableMap[D.getDeclContext()]; 2459 2460 llvm::GlobalVariable *GuardVar = GI ? GI->Guard : nullptr; 2461 unsigned GuardNum; 2462 if (D.isExternallyVisible()) { 2463 // Externally visible variables have to be numbered in Sema to properly 2464 // handle unreachable VarDecls. 2465 GuardNum = getContext().getStaticLocalNumber(&D); 2466 assert(GuardNum > 0); 2467 GuardNum--; 2468 } else if (HasPerVariableGuard) { 2469 GuardNum = ThreadSafeGuardNumMap[D.getDeclContext()]++; 2470 } else { 2471 // Non-externally visible variables are numbered here in CodeGen. 2472 GuardNum = GI->BitIndex++; 2473 } 2474 2475 if (!HasPerVariableGuard && GuardNum >= 32) { 2476 if (D.isExternallyVisible()) 2477 ErrorUnsupportedABI(CGF, "more than 32 guarded initializations"); 2478 GuardNum %= 32; 2479 GuardVar = nullptr; 2480 } 2481 2482 if (!GuardVar) { 2483 // Mangle the name for the guard. 2484 SmallString<256> GuardName; 2485 { 2486 llvm::raw_svector_ostream Out(GuardName); 2487 if (HasPerVariableGuard) 2488 getMangleContext().mangleThreadSafeStaticGuardVariable(&D, GuardNum, 2489 Out); 2490 else 2491 getMangleContext().mangleStaticGuardVariable(&D, Out); 2492 } 2493 2494 // Create the guard variable with a zero-initializer. Just absorb linkage, 2495 // visibility and dll storage class from the guarded variable. 2496 GuardVar = 2497 new llvm::GlobalVariable(CGM.getModule(), GuardTy, /*isConstant=*/false, 2498 GV->getLinkage(), Zero, GuardName.str()); 2499 GuardVar->setVisibility(GV->getVisibility()); 2500 GuardVar->setDLLStorageClass(GV->getDLLStorageClass()); 2501 GuardVar->setAlignment(GuardAlign.getAsAlign()); 2502 if (GuardVar->isWeakForLinker()) 2503 GuardVar->setComdat( 2504 CGM.getModule().getOrInsertComdat(GuardVar->getName())); 2505 if (D.getTLSKind()) 2506 GuardVar->setThreadLocal(true); 2507 if (GI && !HasPerVariableGuard) 2508 GI->Guard = GuardVar; 2509 } 2510 2511 ConstantAddress GuardAddr(GuardVar, GuardAlign); 2512 2513 assert(GuardVar->getLinkage() == GV->getLinkage() && 2514 "static local from the same function had different linkage"); 2515 2516 if (!HasPerVariableGuard) { 2517 // Pseudo code for the test: 2518 // if (!(GuardVar & MyGuardBit)) { 2519 // GuardVar |= MyGuardBit; 2520 // ... initialize the object ...; 2521 // } 2522 2523 // Test our bit from the guard variable. 2524 llvm::ConstantInt *Bit = llvm::ConstantInt::get(GuardTy, 1ULL << GuardNum); 2525 llvm::LoadInst *LI = Builder.CreateLoad(GuardAddr); 2526 llvm::Value *NeedsInit = 2527 Builder.CreateICmpEQ(Builder.CreateAnd(LI, Bit), Zero); 2528 llvm::BasicBlock *InitBlock = CGF.createBasicBlock("init"); 2529 llvm::BasicBlock *EndBlock = CGF.createBasicBlock("init.end"); 2530 CGF.EmitCXXGuardedInitBranch(NeedsInit, InitBlock, EndBlock, 2531 CodeGenFunction::GuardKind::VariableGuard, &D); 2532 2533 // Set our bit in the guard variable and emit the initializer and add a global 2534 // destructor if appropriate. 2535 CGF.EmitBlock(InitBlock); 2536 Builder.CreateStore(Builder.CreateOr(LI, Bit), GuardAddr); 2537 CGF.EHStack.pushCleanup<ResetGuardBit>(EHCleanup, GuardAddr, GuardNum); 2538 CGF.EmitCXXGlobalVarDeclInit(D, GV, PerformInit); 2539 CGF.PopCleanupBlock(); 2540 Builder.CreateBr(EndBlock); 2541 2542 // Continue. 2543 CGF.EmitBlock(EndBlock); 2544 } else { 2545 // Pseudo code for the test: 2546 // if (TSS > _Init_thread_epoch) { 2547 // _Init_thread_header(&TSS); 2548 // if (TSS == -1) { 2549 // ... initialize the object ...; 2550 // _Init_thread_footer(&TSS); 2551 // } 2552 // } 2553 // 2554 // The algorithm is almost identical to what can be found in the appendix 2555 // found in N2325. 2556 2557 // This BasicBLock determines whether or not we have any work to do. 2558 llvm::LoadInst *FirstGuardLoad = Builder.CreateLoad(GuardAddr); 2559 FirstGuardLoad->setOrdering(llvm::AtomicOrdering::Unordered); 2560 llvm::LoadInst *InitThreadEpoch = 2561 Builder.CreateLoad(getInitThreadEpochPtr(CGM)); 2562 llvm::Value *IsUninitialized = 2563 Builder.CreateICmpSGT(FirstGuardLoad, InitThreadEpoch); 2564 llvm::BasicBlock *AttemptInitBlock = CGF.createBasicBlock("init.attempt"); 2565 llvm::BasicBlock *EndBlock = CGF.createBasicBlock("init.end"); 2566 CGF.EmitCXXGuardedInitBranch(IsUninitialized, AttemptInitBlock, EndBlock, 2567 CodeGenFunction::GuardKind::VariableGuard, &D); 2568 2569 // This BasicBlock attempts to determine whether or not this thread is 2570 // responsible for doing the initialization. 2571 CGF.EmitBlock(AttemptInitBlock); 2572 CGF.EmitNounwindRuntimeCall(getInitThreadHeaderFn(CGM), 2573 GuardAddr.getPointer()); 2574 llvm::LoadInst *SecondGuardLoad = Builder.CreateLoad(GuardAddr); 2575 SecondGuardLoad->setOrdering(llvm::AtomicOrdering::Unordered); 2576 llvm::Value *ShouldDoInit = 2577 Builder.CreateICmpEQ(SecondGuardLoad, getAllOnesInt()); 2578 llvm::BasicBlock *InitBlock = CGF.createBasicBlock("init"); 2579 Builder.CreateCondBr(ShouldDoInit, InitBlock, EndBlock); 2580 2581 // Ok, we ended up getting selected as the initializing thread. 2582 CGF.EmitBlock(InitBlock); 2583 CGF.EHStack.pushCleanup<CallInitThreadAbort>(EHCleanup, GuardAddr); 2584 CGF.EmitCXXGlobalVarDeclInit(D, GV, PerformInit); 2585 CGF.PopCleanupBlock(); 2586 CGF.EmitNounwindRuntimeCall(getInitThreadFooterFn(CGM), 2587 GuardAddr.getPointer()); 2588 Builder.CreateBr(EndBlock); 2589 2590 CGF.EmitBlock(EndBlock); 2591 } 2592 } 2593 2594 bool MicrosoftCXXABI::isZeroInitializable(const MemberPointerType *MPT) { 2595 // Null-ness for function memptrs only depends on the first field, which is 2596 // the function pointer. The rest don't matter, so we can zero initialize. 2597 if (MPT->isMemberFunctionPointer()) 2598 return true; 2599 2600 // The virtual base adjustment field is always -1 for null, so if we have one 2601 // we can't zero initialize. The field offset is sometimes also -1 if 0 is a 2602 // valid field offset. 2603 const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl(); 2604 MSInheritanceAttr::Spelling Inheritance = RD->getMSInheritanceModel(); 2605 return (!MSInheritanceAttr::hasVBTableOffsetField(Inheritance) && 2606 RD->nullFieldOffsetIsZero()); 2607 } 2608 2609 llvm::Type * 2610 MicrosoftCXXABI::ConvertMemberPointerType(const MemberPointerType *MPT) { 2611 const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl(); 2612 MSInheritanceAttr::Spelling Inheritance = RD->getMSInheritanceModel(); 2613 llvm::SmallVector<llvm::Type *, 4> fields; 2614 if (MPT->isMemberFunctionPointer()) 2615 fields.push_back(CGM.VoidPtrTy); // FunctionPointerOrVirtualThunk 2616 else 2617 fields.push_back(CGM.IntTy); // FieldOffset 2618 2619 if (MSInheritanceAttr::hasNVOffsetField(MPT->isMemberFunctionPointer(), 2620 Inheritance)) 2621 fields.push_back(CGM.IntTy); 2622 if (MSInheritanceAttr::hasVBPtrOffsetField(Inheritance)) 2623 fields.push_back(CGM.IntTy); 2624 if (MSInheritanceAttr::hasVBTableOffsetField(Inheritance)) 2625 fields.push_back(CGM.IntTy); // VirtualBaseAdjustmentOffset 2626 2627 if (fields.size() == 1) 2628 return fields[0]; 2629 return llvm::StructType::get(CGM.getLLVMContext(), fields); 2630 } 2631 2632 void MicrosoftCXXABI:: 2633 GetNullMemberPointerFields(const MemberPointerType *MPT, 2634 llvm::SmallVectorImpl<llvm::Constant *> &fields) { 2635 assert(fields.empty()); 2636 const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl(); 2637 MSInheritanceAttr::Spelling Inheritance = RD->getMSInheritanceModel(); 2638 if (MPT->isMemberFunctionPointer()) { 2639 // FunctionPointerOrVirtualThunk 2640 fields.push_back(llvm::Constant::getNullValue(CGM.VoidPtrTy)); 2641 } else { 2642 if (RD->nullFieldOffsetIsZero()) 2643 fields.push_back(getZeroInt()); // FieldOffset 2644 else 2645 fields.push_back(getAllOnesInt()); // FieldOffset 2646 } 2647 2648 if (MSInheritanceAttr::hasNVOffsetField(MPT->isMemberFunctionPointer(), 2649 Inheritance)) 2650 fields.push_back(getZeroInt()); 2651 if (MSInheritanceAttr::hasVBPtrOffsetField(Inheritance)) 2652 fields.push_back(getZeroInt()); 2653 if (MSInheritanceAttr::hasVBTableOffsetField(Inheritance)) 2654 fields.push_back(getAllOnesInt()); 2655 } 2656 2657 llvm::Constant * 2658 MicrosoftCXXABI::EmitNullMemberPointer(const MemberPointerType *MPT) { 2659 llvm::SmallVector<llvm::Constant *, 4> fields; 2660 GetNullMemberPointerFields(MPT, fields); 2661 if (fields.size() == 1) 2662 return fields[0]; 2663 llvm::Constant *Res = llvm::ConstantStruct::getAnon(fields); 2664 assert(Res->getType() == ConvertMemberPointerType(MPT)); 2665 return Res; 2666 } 2667 2668 llvm::Constant * 2669 MicrosoftCXXABI::EmitFullMemberPointer(llvm::Constant *FirstField, 2670 bool IsMemberFunction, 2671 const CXXRecordDecl *RD, 2672 CharUnits NonVirtualBaseAdjustment, 2673 unsigned VBTableIndex) { 2674 MSInheritanceAttr::Spelling Inheritance = RD->getMSInheritanceModel(); 2675 2676 // Single inheritance class member pointer are represented as scalars instead 2677 // of aggregates. 2678 if (MSInheritanceAttr::hasOnlyOneField(IsMemberFunction, Inheritance)) 2679 return FirstField; 2680 2681 llvm::SmallVector<llvm::Constant *, 4> fields; 2682 fields.push_back(FirstField); 2683 2684 if (MSInheritanceAttr::hasNVOffsetField(IsMemberFunction, Inheritance)) 2685 fields.push_back(llvm::ConstantInt::get( 2686 CGM.IntTy, NonVirtualBaseAdjustment.getQuantity())); 2687 2688 if (MSInheritanceAttr::hasVBPtrOffsetField(Inheritance)) { 2689 CharUnits Offs = CharUnits::Zero(); 2690 if (VBTableIndex) 2691 Offs = getContext().getASTRecordLayout(RD).getVBPtrOffset(); 2692 fields.push_back(llvm::ConstantInt::get(CGM.IntTy, Offs.getQuantity())); 2693 } 2694 2695 // The rest of the fields are adjusted by conversions to a more derived class. 2696 if (MSInheritanceAttr::hasVBTableOffsetField(Inheritance)) 2697 fields.push_back(llvm::ConstantInt::get(CGM.IntTy, VBTableIndex)); 2698 2699 return llvm::ConstantStruct::getAnon(fields); 2700 } 2701 2702 llvm::Constant * 2703 MicrosoftCXXABI::EmitMemberDataPointer(const MemberPointerType *MPT, 2704 CharUnits offset) { 2705 const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl(); 2706 if (RD->getMSInheritanceModel() == 2707 MSInheritanceAttr::Keyword_virtual_inheritance) 2708 offset -= getContext().getOffsetOfBaseWithVBPtr(RD); 2709 llvm::Constant *FirstField = 2710 llvm::ConstantInt::get(CGM.IntTy, offset.getQuantity()); 2711 return EmitFullMemberPointer(FirstField, /*IsMemberFunction=*/false, RD, 2712 CharUnits::Zero(), /*VBTableIndex=*/0); 2713 } 2714 2715 llvm::Constant *MicrosoftCXXABI::EmitMemberPointer(const APValue &MP, 2716 QualType MPType) { 2717 const MemberPointerType *DstTy = MPType->castAs<MemberPointerType>(); 2718 const ValueDecl *MPD = MP.getMemberPointerDecl(); 2719 if (!MPD) 2720 return EmitNullMemberPointer(DstTy); 2721 2722 ASTContext &Ctx = getContext(); 2723 ArrayRef<const CXXRecordDecl *> MemberPointerPath = MP.getMemberPointerPath(); 2724 2725 llvm::Constant *C; 2726 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MPD)) { 2727 C = EmitMemberFunctionPointer(MD); 2728 } else { 2729 CharUnits FieldOffset = Ctx.toCharUnitsFromBits(Ctx.getFieldOffset(MPD)); 2730 C = EmitMemberDataPointer(DstTy, FieldOffset); 2731 } 2732 2733 if (!MemberPointerPath.empty()) { 2734 const CXXRecordDecl *SrcRD = cast<CXXRecordDecl>(MPD->getDeclContext()); 2735 const Type *SrcRecTy = Ctx.getTypeDeclType(SrcRD).getTypePtr(); 2736 const MemberPointerType *SrcTy = 2737 Ctx.getMemberPointerType(DstTy->getPointeeType(), SrcRecTy) 2738 ->castAs<MemberPointerType>(); 2739 2740 bool DerivedMember = MP.isMemberPointerToDerivedMember(); 2741 SmallVector<const CXXBaseSpecifier *, 4> DerivedToBasePath; 2742 const CXXRecordDecl *PrevRD = SrcRD; 2743 for (const CXXRecordDecl *PathElem : MemberPointerPath) { 2744 const CXXRecordDecl *Base = nullptr; 2745 const CXXRecordDecl *Derived = nullptr; 2746 if (DerivedMember) { 2747 Base = PathElem; 2748 Derived = PrevRD; 2749 } else { 2750 Base = PrevRD; 2751 Derived = PathElem; 2752 } 2753 for (const CXXBaseSpecifier &BS : Derived->bases()) 2754 if (BS.getType()->getAsCXXRecordDecl()->getCanonicalDecl() == 2755 Base->getCanonicalDecl()) 2756 DerivedToBasePath.push_back(&BS); 2757 PrevRD = PathElem; 2758 } 2759 assert(DerivedToBasePath.size() == MemberPointerPath.size()); 2760 2761 CastKind CK = DerivedMember ? CK_DerivedToBaseMemberPointer 2762 : CK_BaseToDerivedMemberPointer; 2763 C = EmitMemberPointerConversion(SrcTy, DstTy, CK, DerivedToBasePath.begin(), 2764 DerivedToBasePath.end(), C); 2765 } 2766 return C; 2767 } 2768 2769 llvm::Constant * 2770 MicrosoftCXXABI::EmitMemberFunctionPointer(const CXXMethodDecl *MD) { 2771 assert(MD->isInstance() && "Member function must not be static!"); 2772 2773 CharUnits NonVirtualBaseAdjustment = CharUnits::Zero(); 2774 const CXXRecordDecl *RD = MD->getParent()->getMostRecentNonInjectedDecl(); 2775 CodeGenTypes &Types = CGM.getTypes(); 2776 2777 unsigned VBTableIndex = 0; 2778 llvm::Constant *FirstField; 2779 const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>(); 2780 if (!MD->isVirtual()) { 2781 llvm::Type *Ty; 2782 // Check whether the function has a computable LLVM signature. 2783 if (Types.isFuncTypeConvertible(FPT)) { 2784 // The function has a computable LLVM signature; use the correct type. 2785 Ty = Types.GetFunctionType(Types.arrangeCXXMethodDeclaration(MD)); 2786 } else { 2787 // Use an arbitrary non-function type to tell GetAddrOfFunction that the 2788 // function type is incomplete. 2789 Ty = CGM.PtrDiffTy; 2790 } 2791 FirstField = CGM.GetAddrOfFunction(MD, Ty); 2792 } else { 2793 auto &VTableContext = CGM.getMicrosoftVTableContext(); 2794 MethodVFTableLocation ML = VTableContext.getMethodVFTableLocation(MD); 2795 FirstField = EmitVirtualMemPtrThunk(MD, ML); 2796 // Include the vfptr adjustment if the method is in a non-primary vftable. 2797 NonVirtualBaseAdjustment += ML.VFPtrOffset; 2798 if (ML.VBase) 2799 VBTableIndex = VTableContext.getVBTableIndex(RD, ML.VBase) * 4; 2800 } 2801 2802 if (VBTableIndex == 0 && 2803 RD->getMSInheritanceModel() == 2804 MSInheritanceAttr::Keyword_virtual_inheritance) 2805 NonVirtualBaseAdjustment -= getContext().getOffsetOfBaseWithVBPtr(RD); 2806 2807 // The rest of the fields are common with data member pointers. 2808 FirstField = llvm::ConstantExpr::getBitCast(FirstField, CGM.VoidPtrTy); 2809 return EmitFullMemberPointer(FirstField, /*IsMemberFunction=*/true, RD, 2810 NonVirtualBaseAdjustment, VBTableIndex); 2811 } 2812 2813 /// Member pointers are the same if they're either bitwise identical *or* both 2814 /// null. Null-ness for function members is determined by the first field, 2815 /// while for data member pointers we must compare all fields. 2816 llvm::Value * 2817 MicrosoftCXXABI::EmitMemberPointerComparison(CodeGenFunction &CGF, 2818 llvm::Value *L, 2819 llvm::Value *R, 2820 const MemberPointerType *MPT, 2821 bool Inequality) { 2822 CGBuilderTy &Builder = CGF.Builder; 2823 2824 // Handle != comparisons by switching the sense of all boolean operations. 2825 llvm::ICmpInst::Predicate Eq; 2826 llvm::Instruction::BinaryOps And, Or; 2827 if (Inequality) { 2828 Eq = llvm::ICmpInst::ICMP_NE; 2829 And = llvm::Instruction::Or; 2830 Or = llvm::Instruction::And; 2831 } else { 2832 Eq = llvm::ICmpInst::ICMP_EQ; 2833 And = llvm::Instruction::And; 2834 Or = llvm::Instruction::Or; 2835 } 2836 2837 // If this is a single field member pointer (single inheritance), this is a 2838 // single icmp. 2839 const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl(); 2840 MSInheritanceAttr::Spelling Inheritance = RD->getMSInheritanceModel(); 2841 if (MSInheritanceAttr::hasOnlyOneField(MPT->isMemberFunctionPointer(), 2842 Inheritance)) 2843 return Builder.CreateICmp(Eq, L, R); 2844 2845 // Compare the first field. 2846 llvm::Value *L0 = Builder.CreateExtractValue(L, 0, "lhs.0"); 2847 llvm::Value *R0 = Builder.CreateExtractValue(R, 0, "rhs.0"); 2848 llvm::Value *Cmp0 = Builder.CreateICmp(Eq, L0, R0, "memptr.cmp.first"); 2849 2850 // Compare everything other than the first field. 2851 llvm::Value *Res = nullptr; 2852 llvm::StructType *LType = cast<llvm::StructType>(L->getType()); 2853 for (unsigned I = 1, E = LType->getNumElements(); I != E; ++I) { 2854 llvm::Value *LF = Builder.CreateExtractValue(L, I); 2855 llvm::Value *RF = Builder.CreateExtractValue(R, I); 2856 llvm::Value *Cmp = Builder.CreateICmp(Eq, LF, RF, "memptr.cmp.rest"); 2857 if (Res) 2858 Res = Builder.CreateBinOp(And, Res, Cmp); 2859 else 2860 Res = Cmp; 2861 } 2862 2863 // Check if the first field is 0 if this is a function pointer. 2864 if (MPT->isMemberFunctionPointer()) { 2865 // (l1 == r1 && ...) || l0 == 0 2866 llvm::Value *Zero = llvm::Constant::getNullValue(L0->getType()); 2867 llvm::Value *IsZero = Builder.CreateICmp(Eq, L0, Zero, "memptr.cmp.iszero"); 2868 Res = Builder.CreateBinOp(Or, Res, IsZero); 2869 } 2870 2871 // Combine the comparison of the first field, which must always be true for 2872 // this comparison to succeeed. 2873 return Builder.CreateBinOp(And, Res, Cmp0, "memptr.cmp"); 2874 } 2875 2876 llvm::Value * 2877 MicrosoftCXXABI::EmitMemberPointerIsNotNull(CodeGenFunction &CGF, 2878 llvm::Value *MemPtr, 2879 const MemberPointerType *MPT) { 2880 CGBuilderTy &Builder = CGF.Builder; 2881 llvm::SmallVector<llvm::Constant *, 4> fields; 2882 // We only need one field for member functions. 2883 if (MPT->isMemberFunctionPointer()) 2884 fields.push_back(llvm::Constant::getNullValue(CGM.VoidPtrTy)); 2885 else 2886 GetNullMemberPointerFields(MPT, fields); 2887 assert(!fields.empty()); 2888 llvm::Value *FirstField = MemPtr; 2889 if (MemPtr->getType()->isStructTy()) 2890 FirstField = Builder.CreateExtractValue(MemPtr, 0); 2891 llvm::Value *Res = Builder.CreateICmpNE(FirstField, fields[0], "memptr.cmp0"); 2892 2893 // For function member pointers, we only need to test the function pointer 2894 // field. The other fields if any can be garbage. 2895 if (MPT->isMemberFunctionPointer()) 2896 return Res; 2897 2898 // Otherwise, emit a series of compares and combine the results. 2899 for (int I = 1, E = fields.size(); I < E; ++I) { 2900 llvm::Value *Field = Builder.CreateExtractValue(MemPtr, I); 2901 llvm::Value *Next = Builder.CreateICmpNE(Field, fields[I], "memptr.cmp"); 2902 Res = Builder.CreateOr(Res, Next, "memptr.tobool"); 2903 } 2904 return Res; 2905 } 2906 2907 bool MicrosoftCXXABI::MemberPointerConstantIsNull(const MemberPointerType *MPT, 2908 llvm::Constant *Val) { 2909 // Function pointers are null if the pointer in the first field is null. 2910 if (MPT->isMemberFunctionPointer()) { 2911 llvm::Constant *FirstField = Val->getType()->isStructTy() ? 2912 Val->getAggregateElement(0U) : Val; 2913 return FirstField->isNullValue(); 2914 } 2915 2916 // If it's not a function pointer and it's zero initializable, we can easily 2917 // check zero. 2918 if (isZeroInitializable(MPT) && Val->isNullValue()) 2919 return true; 2920 2921 // Otherwise, break down all the fields for comparison. Hopefully these 2922 // little Constants are reused, while a big null struct might not be. 2923 llvm::SmallVector<llvm::Constant *, 4> Fields; 2924 GetNullMemberPointerFields(MPT, Fields); 2925 if (Fields.size() == 1) { 2926 assert(Val->getType()->isIntegerTy()); 2927 return Val == Fields[0]; 2928 } 2929 2930 unsigned I, E; 2931 for (I = 0, E = Fields.size(); I != E; ++I) { 2932 if (Val->getAggregateElement(I) != Fields[I]) 2933 break; 2934 } 2935 return I == E; 2936 } 2937 2938 llvm::Value * 2939 MicrosoftCXXABI::GetVBaseOffsetFromVBPtr(CodeGenFunction &CGF, 2940 Address This, 2941 llvm::Value *VBPtrOffset, 2942 llvm::Value *VBTableOffset, 2943 llvm::Value **VBPtrOut) { 2944 CGBuilderTy &Builder = CGF.Builder; 2945 // Load the vbtable pointer from the vbptr in the instance. 2946 This = Builder.CreateElementBitCast(This, CGM.Int8Ty); 2947 llvm::Value *VBPtr = 2948 Builder.CreateInBoundsGEP(This.getPointer(), VBPtrOffset, "vbptr"); 2949 if (VBPtrOut) *VBPtrOut = VBPtr; 2950 VBPtr = Builder.CreateBitCast(VBPtr, 2951 CGM.Int32Ty->getPointerTo(0)->getPointerTo(This.getAddressSpace())); 2952 2953 CharUnits VBPtrAlign; 2954 if (auto CI = dyn_cast<llvm::ConstantInt>(VBPtrOffset)) { 2955 VBPtrAlign = This.getAlignment().alignmentAtOffset( 2956 CharUnits::fromQuantity(CI->getSExtValue())); 2957 } else { 2958 VBPtrAlign = CGF.getPointerAlign(); 2959 } 2960 2961 llvm::Value *VBTable = Builder.CreateAlignedLoad(VBPtr, VBPtrAlign, "vbtable"); 2962 2963 // Translate from byte offset to table index. It improves analyzability. 2964 llvm::Value *VBTableIndex = Builder.CreateAShr( 2965 VBTableOffset, llvm::ConstantInt::get(VBTableOffset->getType(), 2), 2966 "vbtindex", /*isExact=*/true); 2967 2968 // Load an i32 offset from the vb-table. 2969 llvm::Value *VBaseOffs = Builder.CreateInBoundsGEP(VBTable, VBTableIndex); 2970 VBaseOffs = Builder.CreateBitCast(VBaseOffs, CGM.Int32Ty->getPointerTo(0)); 2971 return Builder.CreateAlignedLoad(VBaseOffs, CharUnits::fromQuantity(4), 2972 "vbase_offs"); 2973 } 2974 2975 // Returns an adjusted base cast to i8*, since we do more address arithmetic on 2976 // it. 2977 llvm::Value *MicrosoftCXXABI::AdjustVirtualBase( 2978 CodeGenFunction &CGF, const Expr *E, const CXXRecordDecl *RD, 2979 Address Base, llvm::Value *VBTableOffset, llvm::Value *VBPtrOffset) { 2980 CGBuilderTy &Builder = CGF.Builder; 2981 Base = Builder.CreateElementBitCast(Base, CGM.Int8Ty); 2982 llvm::BasicBlock *OriginalBB = nullptr; 2983 llvm::BasicBlock *SkipAdjustBB = nullptr; 2984 llvm::BasicBlock *VBaseAdjustBB = nullptr; 2985 2986 // In the unspecified inheritance model, there might not be a vbtable at all, 2987 // in which case we need to skip the virtual base lookup. If there is a 2988 // vbtable, the first entry is a no-op entry that gives back the original 2989 // base, so look for a virtual base adjustment offset of zero. 2990 if (VBPtrOffset) { 2991 OriginalBB = Builder.GetInsertBlock(); 2992 VBaseAdjustBB = CGF.createBasicBlock("memptr.vadjust"); 2993 SkipAdjustBB = CGF.createBasicBlock("memptr.skip_vadjust"); 2994 llvm::Value *IsVirtual = 2995 Builder.CreateICmpNE(VBTableOffset, getZeroInt(), 2996 "memptr.is_vbase"); 2997 Builder.CreateCondBr(IsVirtual, VBaseAdjustBB, SkipAdjustBB); 2998 CGF.EmitBlock(VBaseAdjustBB); 2999 } 3000 3001 // If we weren't given a dynamic vbptr offset, RD should be complete and we'll 3002 // know the vbptr offset. 3003 if (!VBPtrOffset) { 3004 CharUnits offs = CharUnits::Zero(); 3005 if (!RD->hasDefinition()) { 3006 DiagnosticsEngine &Diags = CGF.CGM.getDiags(); 3007 unsigned DiagID = Diags.getCustomDiagID( 3008 DiagnosticsEngine::Error, 3009 "member pointer representation requires a " 3010 "complete class type for %0 to perform this expression"); 3011 Diags.Report(E->getExprLoc(), DiagID) << RD << E->getSourceRange(); 3012 } else if (RD->getNumVBases()) 3013 offs = getContext().getASTRecordLayout(RD).getVBPtrOffset(); 3014 VBPtrOffset = llvm::ConstantInt::get(CGM.IntTy, offs.getQuantity()); 3015 } 3016 llvm::Value *VBPtr = nullptr; 3017 llvm::Value *VBaseOffs = 3018 GetVBaseOffsetFromVBPtr(CGF, Base, VBPtrOffset, VBTableOffset, &VBPtr); 3019 llvm::Value *AdjustedBase = Builder.CreateInBoundsGEP(VBPtr, VBaseOffs); 3020 3021 // Merge control flow with the case where we didn't have to adjust. 3022 if (VBaseAdjustBB) { 3023 Builder.CreateBr(SkipAdjustBB); 3024 CGF.EmitBlock(SkipAdjustBB); 3025 llvm::PHINode *Phi = Builder.CreatePHI(CGM.Int8PtrTy, 2, "memptr.base"); 3026 Phi->addIncoming(Base.getPointer(), OriginalBB); 3027 Phi->addIncoming(AdjustedBase, VBaseAdjustBB); 3028 return Phi; 3029 } 3030 return AdjustedBase; 3031 } 3032 3033 llvm::Value *MicrosoftCXXABI::EmitMemberDataPointerAddress( 3034 CodeGenFunction &CGF, const Expr *E, Address Base, llvm::Value *MemPtr, 3035 const MemberPointerType *MPT) { 3036 assert(MPT->isMemberDataPointer()); 3037 unsigned AS = Base.getAddressSpace(); 3038 llvm::Type *PType = 3039 CGF.ConvertTypeForMem(MPT->getPointeeType())->getPointerTo(AS); 3040 CGBuilderTy &Builder = CGF.Builder; 3041 const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl(); 3042 MSInheritanceAttr::Spelling Inheritance = RD->getMSInheritanceModel(); 3043 3044 // Extract the fields we need, regardless of model. We'll apply them if we 3045 // have them. 3046 llvm::Value *FieldOffset = MemPtr; 3047 llvm::Value *VirtualBaseAdjustmentOffset = nullptr; 3048 llvm::Value *VBPtrOffset = nullptr; 3049 if (MemPtr->getType()->isStructTy()) { 3050 // We need to extract values. 3051 unsigned I = 0; 3052 FieldOffset = Builder.CreateExtractValue(MemPtr, I++); 3053 if (MSInheritanceAttr::hasVBPtrOffsetField(Inheritance)) 3054 VBPtrOffset = Builder.CreateExtractValue(MemPtr, I++); 3055 if (MSInheritanceAttr::hasVBTableOffsetField(Inheritance)) 3056 VirtualBaseAdjustmentOffset = Builder.CreateExtractValue(MemPtr, I++); 3057 } 3058 3059 llvm::Value *Addr; 3060 if (VirtualBaseAdjustmentOffset) { 3061 Addr = AdjustVirtualBase(CGF, E, RD, Base, VirtualBaseAdjustmentOffset, 3062 VBPtrOffset); 3063 } else { 3064 Addr = Base.getPointer(); 3065 } 3066 3067 // Cast to char*. 3068 Addr = Builder.CreateBitCast(Addr, CGF.Int8Ty->getPointerTo(AS)); 3069 3070 // Apply the offset, which we assume is non-null. 3071 Addr = Builder.CreateInBoundsGEP(Addr, FieldOffset, "memptr.offset"); 3072 3073 // Cast the address to the appropriate pointer type, adopting the address 3074 // space of the base pointer. 3075 return Builder.CreateBitCast(Addr, PType); 3076 } 3077 3078 llvm::Value * 3079 MicrosoftCXXABI::EmitMemberPointerConversion(CodeGenFunction &CGF, 3080 const CastExpr *E, 3081 llvm::Value *Src) { 3082 assert(E->getCastKind() == CK_DerivedToBaseMemberPointer || 3083 E->getCastKind() == CK_BaseToDerivedMemberPointer || 3084 E->getCastKind() == CK_ReinterpretMemberPointer); 3085 3086 // Use constant emission if we can. 3087 if (isa<llvm::Constant>(Src)) 3088 return EmitMemberPointerConversion(E, cast<llvm::Constant>(Src)); 3089 3090 // We may be adding or dropping fields from the member pointer, so we need 3091 // both types and the inheritance models of both records. 3092 const MemberPointerType *SrcTy = 3093 E->getSubExpr()->getType()->castAs<MemberPointerType>(); 3094 const MemberPointerType *DstTy = E->getType()->castAs<MemberPointerType>(); 3095 bool IsFunc = SrcTy->isMemberFunctionPointer(); 3096 3097 // If the classes use the same null representation, reinterpret_cast is a nop. 3098 bool IsReinterpret = E->getCastKind() == CK_ReinterpretMemberPointer; 3099 if (IsReinterpret && IsFunc) 3100 return Src; 3101 3102 CXXRecordDecl *SrcRD = SrcTy->getMostRecentCXXRecordDecl(); 3103 CXXRecordDecl *DstRD = DstTy->getMostRecentCXXRecordDecl(); 3104 if (IsReinterpret && 3105 SrcRD->nullFieldOffsetIsZero() == DstRD->nullFieldOffsetIsZero()) 3106 return Src; 3107 3108 CGBuilderTy &Builder = CGF.Builder; 3109 3110 // Branch past the conversion if Src is null. 3111 llvm::Value *IsNotNull = EmitMemberPointerIsNotNull(CGF, Src, SrcTy); 3112 llvm::Constant *DstNull = EmitNullMemberPointer(DstTy); 3113 3114 // C++ 5.2.10p9: The null member pointer value is converted to the null member 3115 // pointer value of the destination type. 3116 if (IsReinterpret) { 3117 // For reinterpret casts, sema ensures that src and dst are both functions 3118 // or data and have the same size, which means the LLVM types should match. 3119 assert(Src->getType() == DstNull->getType()); 3120 return Builder.CreateSelect(IsNotNull, Src, DstNull); 3121 } 3122 3123 llvm::BasicBlock *OriginalBB = Builder.GetInsertBlock(); 3124 llvm::BasicBlock *ConvertBB = CGF.createBasicBlock("memptr.convert"); 3125 llvm::BasicBlock *ContinueBB = CGF.createBasicBlock("memptr.converted"); 3126 Builder.CreateCondBr(IsNotNull, ConvertBB, ContinueBB); 3127 CGF.EmitBlock(ConvertBB); 3128 3129 llvm::Value *Dst = EmitNonNullMemberPointerConversion( 3130 SrcTy, DstTy, E->getCastKind(), E->path_begin(), E->path_end(), Src, 3131 Builder); 3132 3133 Builder.CreateBr(ContinueBB); 3134 3135 // In the continuation, choose between DstNull and Dst. 3136 CGF.EmitBlock(ContinueBB); 3137 llvm::PHINode *Phi = Builder.CreatePHI(DstNull->getType(), 2, "memptr.converted"); 3138 Phi->addIncoming(DstNull, OriginalBB); 3139 Phi->addIncoming(Dst, ConvertBB); 3140 return Phi; 3141 } 3142 3143 llvm::Value *MicrosoftCXXABI::EmitNonNullMemberPointerConversion( 3144 const MemberPointerType *SrcTy, const MemberPointerType *DstTy, CastKind CK, 3145 CastExpr::path_const_iterator PathBegin, 3146 CastExpr::path_const_iterator PathEnd, llvm::Value *Src, 3147 CGBuilderTy &Builder) { 3148 const CXXRecordDecl *SrcRD = SrcTy->getMostRecentCXXRecordDecl(); 3149 const CXXRecordDecl *DstRD = DstTy->getMostRecentCXXRecordDecl(); 3150 MSInheritanceAttr::Spelling SrcInheritance = SrcRD->getMSInheritanceModel(); 3151 MSInheritanceAttr::Spelling DstInheritance = DstRD->getMSInheritanceModel(); 3152 bool IsFunc = SrcTy->isMemberFunctionPointer(); 3153 bool IsConstant = isa<llvm::Constant>(Src); 3154 3155 // Decompose src. 3156 llvm::Value *FirstField = Src; 3157 llvm::Value *NonVirtualBaseAdjustment = getZeroInt(); 3158 llvm::Value *VirtualBaseAdjustmentOffset = getZeroInt(); 3159 llvm::Value *VBPtrOffset = getZeroInt(); 3160 if (!MSInheritanceAttr::hasOnlyOneField(IsFunc, SrcInheritance)) { 3161 // We need to extract values. 3162 unsigned I = 0; 3163 FirstField = Builder.CreateExtractValue(Src, I++); 3164 if (MSInheritanceAttr::hasNVOffsetField(IsFunc, SrcInheritance)) 3165 NonVirtualBaseAdjustment = Builder.CreateExtractValue(Src, I++); 3166 if (MSInheritanceAttr::hasVBPtrOffsetField(SrcInheritance)) 3167 VBPtrOffset = Builder.CreateExtractValue(Src, I++); 3168 if (MSInheritanceAttr::hasVBTableOffsetField(SrcInheritance)) 3169 VirtualBaseAdjustmentOffset = Builder.CreateExtractValue(Src, I++); 3170 } 3171 3172 bool IsDerivedToBase = (CK == CK_DerivedToBaseMemberPointer); 3173 const MemberPointerType *DerivedTy = IsDerivedToBase ? SrcTy : DstTy; 3174 const CXXRecordDecl *DerivedClass = DerivedTy->getMostRecentCXXRecordDecl(); 3175 3176 // For data pointers, we adjust the field offset directly. For functions, we 3177 // have a separate field. 3178 llvm::Value *&NVAdjustField = IsFunc ? NonVirtualBaseAdjustment : FirstField; 3179 3180 // The virtual inheritance model has a quirk: the virtual base table is always 3181 // referenced when dereferencing a member pointer even if the member pointer 3182 // is non-virtual. This is accounted for by adjusting the non-virtual offset 3183 // to point backwards to the top of the MDC from the first VBase. Undo this 3184 // adjustment to normalize the member pointer. 3185 llvm::Value *SrcVBIndexEqZero = 3186 Builder.CreateICmpEQ(VirtualBaseAdjustmentOffset, getZeroInt()); 3187 if (SrcInheritance == MSInheritanceAttr::Keyword_virtual_inheritance) { 3188 if (int64_t SrcOffsetToFirstVBase = 3189 getContext().getOffsetOfBaseWithVBPtr(SrcRD).getQuantity()) { 3190 llvm::Value *UndoSrcAdjustment = Builder.CreateSelect( 3191 SrcVBIndexEqZero, 3192 llvm::ConstantInt::get(CGM.IntTy, SrcOffsetToFirstVBase), 3193 getZeroInt()); 3194 NVAdjustField = Builder.CreateNSWAdd(NVAdjustField, UndoSrcAdjustment); 3195 } 3196 } 3197 3198 // A non-zero vbindex implies that we are dealing with a source member in a 3199 // floating virtual base in addition to some non-virtual offset. If the 3200 // vbindex is zero, we are dealing with a source that exists in a non-virtual, 3201 // fixed, base. The difference between these two cases is that the vbindex + 3202 // nvoffset *always* point to the member regardless of what context they are 3203 // evaluated in so long as the vbindex is adjusted. A member inside a fixed 3204 // base requires explicit nv adjustment. 3205 llvm::Constant *BaseClassOffset = llvm::ConstantInt::get( 3206 CGM.IntTy, 3207 CGM.computeNonVirtualBaseClassOffset(DerivedClass, PathBegin, PathEnd) 3208 .getQuantity()); 3209 3210 llvm::Value *NVDisp; 3211 if (IsDerivedToBase) 3212 NVDisp = Builder.CreateNSWSub(NVAdjustField, BaseClassOffset, "adj"); 3213 else 3214 NVDisp = Builder.CreateNSWAdd(NVAdjustField, BaseClassOffset, "adj"); 3215 3216 NVAdjustField = Builder.CreateSelect(SrcVBIndexEqZero, NVDisp, getZeroInt()); 3217 3218 // Update the vbindex to an appropriate value in the destination because 3219 // SrcRD's vbtable might not be a strict prefix of the one in DstRD. 3220 llvm::Value *DstVBIndexEqZero = SrcVBIndexEqZero; 3221 if (MSInheritanceAttr::hasVBTableOffsetField(DstInheritance) && 3222 MSInheritanceAttr::hasVBTableOffsetField(SrcInheritance)) { 3223 if (llvm::GlobalVariable *VDispMap = 3224 getAddrOfVirtualDisplacementMap(SrcRD, DstRD)) { 3225 llvm::Value *VBIndex = Builder.CreateExactUDiv( 3226 VirtualBaseAdjustmentOffset, llvm::ConstantInt::get(CGM.IntTy, 4)); 3227 if (IsConstant) { 3228 llvm::Constant *Mapping = VDispMap->getInitializer(); 3229 VirtualBaseAdjustmentOffset = 3230 Mapping->getAggregateElement(cast<llvm::Constant>(VBIndex)); 3231 } else { 3232 llvm::Value *Idxs[] = {getZeroInt(), VBIndex}; 3233 VirtualBaseAdjustmentOffset = 3234 Builder.CreateAlignedLoad(Builder.CreateInBoundsGEP(VDispMap, Idxs), 3235 CharUnits::fromQuantity(4)); 3236 } 3237 3238 DstVBIndexEqZero = 3239 Builder.CreateICmpEQ(VirtualBaseAdjustmentOffset, getZeroInt()); 3240 } 3241 } 3242 3243 // Set the VBPtrOffset to zero if the vbindex is zero. Otherwise, initialize 3244 // it to the offset of the vbptr. 3245 if (MSInheritanceAttr::hasVBPtrOffsetField(DstInheritance)) { 3246 llvm::Value *DstVBPtrOffset = llvm::ConstantInt::get( 3247 CGM.IntTy, 3248 getContext().getASTRecordLayout(DstRD).getVBPtrOffset().getQuantity()); 3249 VBPtrOffset = 3250 Builder.CreateSelect(DstVBIndexEqZero, getZeroInt(), DstVBPtrOffset); 3251 } 3252 3253 // Likewise, apply a similar adjustment so that dereferencing the member 3254 // pointer correctly accounts for the distance between the start of the first 3255 // virtual base and the top of the MDC. 3256 if (DstInheritance == MSInheritanceAttr::Keyword_virtual_inheritance) { 3257 if (int64_t DstOffsetToFirstVBase = 3258 getContext().getOffsetOfBaseWithVBPtr(DstRD).getQuantity()) { 3259 llvm::Value *DoDstAdjustment = Builder.CreateSelect( 3260 DstVBIndexEqZero, 3261 llvm::ConstantInt::get(CGM.IntTy, DstOffsetToFirstVBase), 3262 getZeroInt()); 3263 NVAdjustField = Builder.CreateNSWSub(NVAdjustField, DoDstAdjustment); 3264 } 3265 } 3266 3267 // Recompose dst from the null struct and the adjusted fields from src. 3268 llvm::Value *Dst; 3269 if (MSInheritanceAttr::hasOnlyOneField(IsFunc, DstInheritance)) { 3270 Dst = FirstField; 3271 } else { 3272 Dst = llvm::UndefValue::get(ConvertMemberPointerType(DstTy)); 3273 unsigned Idx = 0; 3274 Dst = Builder.CreateInsertValue(Dst, FirstField, Idx++); 3275 if (MSInheritanceAttr::hasNVOffsetField(IsFunc, DstInheritance)) 3276 Dst = Builder.CreateInsertValue(Dst, NonVirtualBaseAdjustment, Idx++); 3277 if (MSInheritanceAttr::hasVBPtrOffsetField(DstInheritance)) 3278 Dst = Builder.CreateInsertValue(Dst, VBPtrOffset, Idx++); 3279 if (MSInheritanceAttr::hasVBTableOffsetField(DstInheritance)) 3280 Dst = Builder.CreateInsertValue(Dst, VirtualBaseAdjustmentOffset, Idx++); 3281 } 3282 return Dst; 3283 } 3284 3285 llvm::Constant * 3286 MicrosoftCXXABI::EmitMemberPointerConversion(const CastExpr *E, 3287 llvm::Constant *Src) { 3288 const MemberPointerType *SrcTy = 3289 E->getSubExpr()->getType()->castAs<MemberPointerType>(); 3290 const MemberPointerType *DstTy = E->getType()->castAs<MemberPointerType>(); 3291 3292 CastKind CK = E->getCastKind(); 3293 3294 return EmitMemberPointerConversion(SrcTy, DstTy, CK, E->path_begin(), 3295 E->path_end(), Src); 3296 } 3297 3298 llvm::Constant *MicrosoftCXXABI::EmitMemberPointerConversion( 3299 const MemberPointerType *SrcTy, const MemberPointerType *DstTy, CastKind CK, 3300 CastExpr::path_const_iterator PathBegin, 3301 CastExpr::path_const_iterator PathEnd, llvm::Constant *Src) { 3302 assert(CK == CK_DerivedToBaseMemberPointer || 3303 CK == CK_BaseToDerivedMemberPointer || 3304 CK == CK_ReinterpretMemberPointer); 3305 // If src is null, emit a new null for dst. We can't return src because dst 3306 // might have a new representation. 3307 if (MemberPointerConstantIsNull(SrcTy, Src)) 3308 return EmitNullMemberPointer(DstTy); 3309 3310 // We don't need to do anything for reinterpret_casts of non-null member 3311 // pointers. We should only get here when the two type representations have 3312 // the same size. 3313 if (CK == CK_ReinterpretMemberPointer) 3314 return Src; 3315 3316 CGBuilderTy Builder(CGM, CGM.getLLVMContext()); 3317 auto *Dst = cast<llvm::Constant>(EmitNonNullMemberPointerConversion( 3318 SrcTy, DstTy, CK, PathBegin, PathEnd, Src, Builder)); 3319 3320 return Dst; 3321 } 3322 3323 CGCallee MicrosoftCXXABI::EmitLoadOfMemberFunctionPointer( 3324 CodeGenFunction &CGF, const Expr *E, Address This, 3325 llvm::Value *&ThisPtrForCall, llvm::Value *MemPtr, 3326 const MemberPointerType *MPT) { 3327 assert(MPT->isMemberFunctionPointer()); 3328 const FunctionProtoType *FPT = 3329 MPT->getPointeeType()->castAs<FunctionProtoType>(); 3330 const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl(); 3331 llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType( 3332 CGM.getTypes().arrangeCXXMethodType(RD, FPT, /*FD=*/nullptr)); 3333 CGBuilderTy &Builder = CGF.Builder; 3334 3335 MSInheritanceAttr::Spelling Inheritance = RD->getMSInheritanceModel(); 3336 3337 // Extract the fields we need, regardless of model. We'll apply them if we 3338 // have them. 3339 llvm::Value *FunctionPointer = MemPtr; 3340 llvm::Value *NonVirtualBaseAdjustment = nullptr; 3341 llvm::Value *VirtualBaseAdjustmentOffset = nullptr; 3342 llvm::Value *VBPtrOffset = nullptr; 3343 if (MemPtr->getType()->isStructTy()) { 3344 // We need to extract values. 3345 unsigned I = 0; 3346 FunctionPointer = Builder.CreateExtractValue(MemPtr, I++); 3347 if (MSInheritanceAttr::hasNVOffsetField(MPT, Inheritance)) 3348 NonVirtualBaseAdjustment = Builder.CreateExtractValue(MemPtr, I++); 3349 if (MSInheritanceAttr::hasVBPtrOffsetField(Inheritance)) 3350 VBPtrOffset = Builder.CreateExtractValue(MemPtr, I++); 3351 if (MSInheritanceAttr::hasVBTableOffsetField(Inheritance)) 3352 VirtualBaseAdjustmentOffset = Builder.CreateExtractValue(MemPtr, I++); 3353 } 3354 3355 if (VirtualBaseAdjustmentOffset) { 3356 ThisPtrForCall = AdjustVirtualBase(CGF, E, RD, This, 3357 VirtualBaseAdjustmentOffset, VBPtrOffset); 3358 } else { 3359 ThisPtrForCall = This.getPointer(); 3360 } 3361 3362 if (NonVirtualBaseAdjustment) { 3363 // Apply the adjustment and cast back to the original struct type. 3364 llvm::Value *Ptr = Builder.CreateBitCast(ThisPtrForCall, CGF.Int8PtrTy); 3365 Ptr = Builder.CreateInBoundsGEP(Ptr, NonVirtualBaseAdjustment); 3366 ThisPtrForCall = Builder.CreateBitCast(Ptr, ThisPtrForCall->getType(), 3367 "this.adjusted"); 3368 } 3369 3370 FunctionPointer = 3371 Builder.CreateBitCast(FunctionPointer, FTy->getPointerTo()); 3372 CGCallee Callee(FPT, FunctionPointer); 3373 return Callee; 3374 } 3375 3376 CGCXXABI *clang::CodeGen::CreateMicrosoftCXXABI(CodeGenModule &CGM) { 3377 return new MicrosoftCXXABI(CGM); 3378 } 3379 3380 // MS RTTI Overview: 3381 // The run time type information emitted by cl.exe contains 5 distinct types of 3382 // structures. Many of them reference each other. 3383 // 3384 // TypeInfo: Static classes that are returned by typeid. 3385 // 3386 // CompleteObjectLocator: Referenced by vftables. They contain information 3387 // required for dynamic casting, including OffsetFromTop. They also contain 3388 // a reference to the TypeInfo for the type and a reference to the 3389 // CompleteHierarchyDescriptor for the type. 3390 // 3391 // ClassHierarchyDescriptor: Contains information about a class hierarchy. 3392 // Used during dynamic_cast to walk a class hierarchy. References a base 3393 // class array and the size of said array. 3394 // 3395 // BaseClassArray: Contains a list of classes in a hierarchy. BaseClassArray is 3396 // somewhat of a misnomer because the most derived class is also in the list 3397 // as well as multiple copies of virtual bases (if they occur multiple times 3398 // in the hierarchy.) The BaseClassArray contains one BaseClassDescriptor for 3399 // every path in the hierarchy, in pre-order depth first order. Note, we do 3400 // not declare a specific llvm type for BaseClassArray, it's merely an array 3401 // of BaseClassDescriptor pointers. 3402 // 3403 // BaseClassDescriptor: Contains information about a class in a class hierarchy. 3404 // BaseClassDescriptor is also somewhat of a misnomer for the same reason that 3405 // BaseClassArray is. It contains information about a class within a 3406 // hierarchy such as: is this base is ambiguous and what is its offset in the 3407 // vbtable. The names of the BaseClassDescriptors have all of their fields 3408 // mangled into them so they can be aggressively deduplicated by the linker. 3409 3410 static llvm::GlobalVariable *getTypeInfoVTable(CodeGenModule &CGM) { 3411 StringRef MangledName("??_7type_info@@6B@"); 3412 if (auto VTable = CGM.getModule().getNamedGlobal(MangledName)) 3413 return VTable; 3414 return new llvm::GlobalVariable(CGM.getModule(), CGM.Int8PtrTy, 3415 /*isConstant=*/true, 3416 llvm::GlobalVariable::ExternalLinkage, 3417 /*Initializer=*/nullptr, MangledName); 3418 } 3419 3420 namespace { 3421 3422 /// A Helper struct that stores information about a class in a class 3423 /// hierarchy. The information stored in these structs struct is used during 3424 /// the generation of ClassHierarchyDescriptors and BaseClassDescriptors. 3425 // During RTTI creation, MSRTTIClasses are stored in a contiguous array with 3426 // implicit depth first pre-order tree connectivity. getFirstChild and 3427 // getNextSibling allow us to walk the tree efficiently. 3428 struct MSRTTIClass { 3429 enum { 3430 IsPrivateOnPath = 1 | 8, 3431 IsAmbiguous = 2, 3432 IsPrivate = 4, 3433 IsVirtual = 16, 3434 HasHierarchyDescriptor = 64 3435 }; 3436 MSRTTIClass(const CXXRecordDecl *RD) : RD(RD) {} 3437 uint32_t initialize(const MSRTTIClass *Parent, 3438 const CXXBaseSpecifier *Specifier); 3439 3440 MSRTTIClass *getFirstChild() { return this + 1; } 3441 static MSRTTIClass *getNextChild(MSRTTIClass *Child) { 3442 return Child + 1 + Child->NumBases; 3443 } 3444 3445 const CXXRecordDecl *RD, *VirtualRoot; 3446 uint32_t Flags, NumBases, OffsetInVBase; 3447 }; 3448 3449 /// Recursively initialize the base class array. 3450 uint32_t MSRTTIClass::initialize(const MSRTTIClass *Parent, 3451 const CXXBaseSpecifier *Specifier) { 3452 Flags = HasHierarchyDescriptor; 3453 if (!Parent) { 3454 VirtualRoot = nullptr; 3455 OffsetInVBase = 0; 3456 } else { 3457 if (Specifier->getAccessSpecifier() != AS_public) 3458 Flags |= IsPrivate | IsPrivateOnPath; 3459 if (Specifier->isVirtual()) { 3460 Flags |= IsVirtual; 3461 VirtualRoot = RD; 3462 OffsetInVBase = 0; 3463 } else { 3464 if (Parent->Flags & IsPrivateOnPath) 3465 Flags |= IsPrivateOnPath; 3466 VirtualRoot = Parent->VirtualRoot; 3467 OffsetInVBase = Parent->OffsetInVBase + RD->getASTContext() 3468 .getASTRecordLayout(Parent->RD).getBaseClassOffset(RD).getQuantity(); 3469 } 3470 } 3471 NumBases = 0; 3472 MSRTTIClass *Child = getFirstChild(); 3473 for (const CXXBaseSpecifier &Base : RD->bases()) { 3474 NumBases += Child->initialize(this, &Base) + 1; 3475 Child = getNextChild(Child); 3476 } 3477 return NumBases; 3478 } 3479 3480 static llvm::GlobalValue::LinkageTypes getLinkageForRTTI(QualType Ty) { 3481 switch (Ty->getLinkage()) { 3482 case NoLinkage: 3483 case InternalLinkage: 3484 case UniqueExternalLinkage: 3485 return llvm::GlobalValue::InternalLinkage; 3486 3487 case VisibleNoLinkage: 3488 case ModuleInternalLinkage: 3489 case ModuleLinkage: 3490 case ExternalLinkage: 3491 return llvm::GlobalValue::LinkOnceODRLinkage; 3492 } 3493 llvm_unreachable("Invalid linkage!"); 3494 } 3495 3496 /// An ephemeral helper class for building MS RTTI types. It caches some 3497 /// calls to the module and information about the most derived class in a 3498 /// hierarchy. 3499 struct MSRTTIBuilder { 3500 enum { 3501 HasBranchingHierarchy = 1, 3502 HasVirtualBranchingHierarchy = 2, 3503 HasAmbiguousBases = 4 3504 }; 3505 3506 MSRTTIBuilder(MicrosoftCXXABI &ABI, const CXXRecordDecl *RD) 3507 : CGM(ABI.CGM), Context(CGM.getContext()), 3508 VMContext(CGM.getLLVMContext()), Module(CGM.getModule()), RD(RD), 3509 Linkage(getLinkageForRTTI(CGM.getContext().getTagDeclType(RD))), 3510 ABI(ABI) {} 3511 3512 llvm::GlobalVariable *getBaseClassDescriptor(const MSRTTIClass &Classes); 3513 llvm::GlobalVariable * 3514 getBaseClassArray(SmallVectorImpl<MSRTTIClass> &Classes); 3515 llvm::GlobalVariable *getClassHierarchyDescriptor(); 3516 llvm::GlobalVariable *getCompleteObjectLocator(const VPtrInfo &Info); 3517 3518 CodeGenModule &CGM; 3519 ASTContext &Context; 3520 llvm::LLVMContext &VMContext; 3521 llvm::Module &Module; 3522 const CXXRecordDecl *RD; 3523 llvm::GlobalVariable::LinkageTypes Linkage; 3524 MicrosoftCXXABI &ABI; 3525 }; 3526 3527 } // namespace 3528 3529 /// Recursively serializes a class hierarchy in pre-order depth first 3530 /// order. 3531 static void serializeClassHierarchy(SmallVectorImpl<MSRTTIClass> &Classes, 3532 const CXXRecordDecl *RD) { 3533 Classes.push_back(MSRTTIClass(RD)); 3534 for (const CXXBaseSpecifier &Base : RD->bases()) 3535 serializeClassHierarchy(Classes, Base.getType()->getAsCXXRecordDecl()); 3536 } 3537 3538 /// Find ambiguity among base classes. 3539 static void 3540 detectAmbiguousBases(SmallVectorImpl<MSRTTIClass> &Classes) { 3541 llvm::SmallPtrSet<const CXXRecordDecl *, 8> VirtualBases; 3542 llvm::SmallPtrSet<const CXXRecordDecl *, 8> UniqueBases; 3543 llvm::SmallPtrSet<const CXXRecordDecl *, 8> AmbiguousBases; 3544 for (MSRTTIClass *Class = &Classes.front(); Class <= &Classes.back();) { 3545 if ((Class->Flags & MSRTTIClass::IsVirtual) && 3546 !VirtualBases.insert(Class->RD).second) { 3547 Class = MSRTTIClass::getNextChild(Class); 3548 continue; 3549 } 3550 if (!UniqueBases.insert(Class->RD).second) 3551 AmbiguousBases.insert(Class->RD); 3552 Class++; 3553 } 3554 if (AmbiguousBases.empty()) 3555 return; 3556 for (MSRTTIClass &Class : Classes) 3557 if (AmbiguousBases.count(Class.RD)) 3558 Class.Flags |= MSRTTIClass::IsAmbiguous; 3559 } 3560 3561 llvm::GlobalVariable *MSRTTIBuilder::getClassHierarchyDescriptor() { 3562 SmallString<256> MangledName; 3563 { 3564 llvm::raw_svector_ostream Out(MangledName); 3565 ABI.getMangleContext().mangleCXXRTTIClassHierarchyDescriptor(RD, Out); 3566 } 3567 3568 // Check to see if we've already declared this ClassHierarchyDescriptor. 3569 if (auto CHD = Module.getNamedGlobal(MangledName)) 3570 return CHD; 3571 3572 // Serialize the class hierarchy and initialize the CHD Fields. 3573 SmallVector<MSRTTIClass, 8> Classes; 3574 serializeClassHierarchy(Classes, RD); 3575 Classes.front().initialize(/*Parent=*/nullptr, /*Specifier=*/nullptr); 3576 detectAmbiguousBases(Classes); 3577 int Flags = 0; 3578 for (auto Class : Classes) { 3579 if (Class.RD->getNumBases() > 1) 3580 Flags |= HasBranchingHierarchy; 3581 // Note: cl.exe does not calculate "HasAmbiguousBases" correctly. We 3582 // believe the field isn't actually used. 3583 if (Class.Flags & MSRTTIClass::IsAmbiguous) 3584 Flags |= HasAmbiguousBases; 3585 } 3586 if ((Flags & HasBranchingHierarchy) && RD->getNumVBases() != 0) 3587 Flags |= HasVirtualBranchingHierarchy; 3588 // These gep indices are used to get the address of the first element of the 3589 // base class array. 3590 llvm::Value *GEPIndices[] = {llvm::ConstantInt::get(CGM.IntTy, 0), 3591 llvm::ConstantInt::get(CGM.IntTy, 0)}; 3592 3593 // Forward-declare the class hierarchy descriptor 3594 auto Type = ABI.getClassHierarchyDescriptorType(); 3595 auto CHD = new llvm::GlobalVariable(Module, Type, /*isConstant=*/true, Linkage, 3596 /*Initializer=*/nullptr, 3597 MangledName); 3598 if (CHD->isWeakForLinker()) 3599 CHD->setComdat(CGM.getModule().getOrInsertComdat(CHD->getName())); 3600 3601 auto *Bases = getBaseClassArray(Classes); 3602 3603 // Initialize the base class ClassHierarchyDescriptor. 3604 llvm::Constant *Fields[] = { 3605 llvm::ConstantInt::get(CGM.IntTy, 0), // reserved by the runtime 3606 llvm::ConstantInt::get(CGM.IntTy, Flags), 3607 llvm::ConstantInt::get(CGM.IntTy, Classes.size()), 3608 ABI.getImageRelativeConstant(llvm::ConstantExpr::getInBoundsGetElementPtr( 3609 Bases->getValueType(), Bases, 3610 llvm::ArrayRef<llvm::Value *>(GEPIndices))), 3611 }; 3612 CHD->setInitializer(llvm::ConstantStruct::get(Type, Fields)); 3613 return CHD; 3614 } 3615 3616 llvm::GlobalVariable * 3617 MSRTTIBuilder::getBaseClassArray(SmallVectorImpl<MSRTTIClass> &Classes) { 3618 SmallString<256> MangledName; 3619 { 3620 llvm::raw_svector_ostream Out(MangledName); 3621 ABI.getMangleContext().mangleCXXRTTIBaseClassArray(RD, Out); 3622 } 3623 3624 // Forward-declare the base class array. 3625 // cl.exe pads the base class array with 1 (in 32 bit mode) or 4 (in 64 bit 3626 // mode) bytes of padding. We provide a pointer sized amount of padding by 3627 // adding +1 to Classes.size(). The sections have pointer alignment and are 3628 // marked pick-any so it shouldn't matter. 3629 llvm::Type *PtrType = ABI.getImageRelativeType( 3630 ABI.getBaseClassDescriptorType()->getPointerTo()); 3631 auto *ArrType = llvm::ArrayType::get(PtrType, Classes.size() + 1); 3632 auto *BCA = 3633 new llvm::GlobalVariable(Module, ArrType, 3634 /*isConstant=*/true, Linkage, 3635 /*Initializer=*/nullptr, MangledName); 3636 if (BCA->isWeakForLinker()) 3637 BCA->setComdat(CGM.getModule().getOrInsertComdat(BCA->getName())); 3638 3639 // Initialize the BaseClassArray. 3640 SmallVector<llvm::Constant *, 8> BaseClassArrayData; 3641 for (MSRTTIClass &Class : Classes) 3642 BaseClassArrayData.push_back( 3643 ABI.getImageRelativeConstant(getBaseClassDescriptor(Class))); 3644 BaseClassArrayData.push_back(llvm::Constant::getNullValue(PtrType)); 3645 BCA->setInitializer(llvm::ConstantArray::get(ArrType, BaseClassArrayData)); 3646 return BCA; 3647 } 3648 3649 llvm::GlobalVariable * 3650 MSRTTIBuilder::getBaseClassDescriptor(const MSRTTIClass &Class) { 3651 // Compute the fields for the BaseClassDescriptor. They are computed up front 3652 // because they are mangled into the name of the object. 3653 uint32_t OffsetInVBTable = 0; 3654 int32_t VBPtrOffset = -1; 3655 if (Class.VirtualRoot) { 3656 auto &VTableContext = CGM.getMicrosoftVTableContext(); 3657 OffsetInVBTable = VTableContext.getVBTableIndex(RD, Class.VirtualRoot) * 4; 3658 VBPtrOffset = Context.getASTRecordLayout(RD).getVBPtrOffset().getQuantity(); 3659 } 3660 3661 SmallString<256> MangledName; 3662 { 3663 llvm::raw_svector_ostream Out(MangledName); 3664 ABI.getMangleContext().mangleCXXRTTIBaseClassDescriptor( 3665 Class.RD, Class.OffsetInVBase, VBPtrOffset, OffsetInVBTable, 3666 Class.Flags, Out); 3667 } 3668 3669 // Check to see if we've already declared this object. 3670 if (auto BCD = Module.getNamedGlobal(MangledName)) 3671 return BCD; 3672 3673 // Forward-declare the base class descriptor. 3674 auto Type = ABI.getBaseClassDescriptorType(); 3675 auto BCD = 3676 new llvm::GlobalVariable(Module, Type, /*isConstant=*/true, Linkage, 3677 /*Initializer=*/nullptr, MangledName); 3678 if (BCD->isWeakForLinker()) 3679 BCD->setComdat(CGM.getModule().getOrInsertComdat(BCD->getName())); 3680 3681 // Initialize the BaseClassDescriptor. 3682 llvm::Constant *Fields[] = { 3683 ABI.getImageRelativeConstant( 3684 ABI.getAddrOfRTTIDescriptor(Context.getTypeDeclType(Class.RD))), 3685 llvm::ConstantInt::get(CGM.IntTy, Class.NumBases), 3686 llvm::ConstantInt::get(CGM.IntTy, Class.OffsetInVBase), 3687 llvm::ConstantInt::get(CGM.IntTy, VBPtrOffset), 3688 llvm::ConstantInt::get(CGM.IntTy, OffsetInVBTable), 3689 llvm::ConstantInt::get(CGM.IntTy, Class.Flags), 3690 ABI.getImageRelativeConstant( 3691 MSRTTIBuilder(ABI, Class.RD).getClassHierarchyDescriptor()), 3692 }; 3693 BCD->setInitializer(llvm::ConstantStruct::get(Type, Fields)); 3694 return BCD; 3695 } 3696 3697 llvm::GlobalVariable * 3698 MSRTTIBuilder::getCompleteObjectLocator(const VPtrInfo &Info) { 3699 SmallString<256> MangledName; 3700 { 3701 llvm::raw_svector_ostream Out(MangledName); 3702 ABI.getMangleContext().mangleCXXRTTICompleteObjectLocator(RD, Info.MangledPath, Out); 3703 } 3704 3705 // Check to see if we've already computed this complete object locator. 3706 if (auto COL = Module.getNamedGlobal(MangledName)) 3707 return COL; 3708 3709 // Compute the fields of the complete object locator. 3710 int OffsetToTop = Info.FullOffsetInMDC.getQuantity(); 3711 int VFPtrOffset = 0; 3712 // The offset includes the vtordisp if one exists. 3713 if (const CXXRecordDecl *VBase = Info.getVBaseWithVPtr()) 3714 if (Context.getASTRecordLayout(RD) 3715 .getVBaseOffsetsMap() 3716 .find(VBase) 3717 ->second.hasVtorDisp()) 3718 VFPtrOffset = Info.NonVirtualOffset.getQuantity() + 4; 3719 3720 // Forward-declare the complete object locator. 3721 llvm::StructType *Type = ABI.getCompleteObjectLocatorType(); 3722 auto COL = new llvm::GlobalVariable(Module, Type, /*isConstant=*/true, Linkage, 3723 /*Initializer=*/nullptr, MangledName); 3724 3725 // Initialize the CompleteObjectLocator. 3726 llvm::Constant *Fields[] = { 3727 llvm::ConstantInt::get(CGM.IntTy, ABI.isImageRelative()), 3728 llvm::ConstantInt::get(CGM.IntTy, OffsetToTop), 3729 llvm::ConstantInt::get(CGM.IntTy, VFPtrOffset), 3730 ABI.getImageRelativeConstant( 3731 CGM.GetAddrOfRTTIDescriptor(Context.getTypeDeclType(RD))), 3732 ABI.getImageRelativeConstant(getClassHierarchyDescriptor()), 3733 ABI.getImageRelativeConstant(COL), 3734 }; 3735 llvm::ArrayRef<llvm::Constant *> FieldsRef(Fields); 3736 if (!ABI.isImageRelative()) 3737 FieldsRef = FieldsRef.drop_back(); 3738 COL->setInitializer(llvm::ConstantStruct::get(Type, FieldsRef)); 3739 if (COL->isWeakForLinker()) 3740 COL->setComdat(CGM.getModule().getOrInsertComdat(COL->getName())); 3741 return COL; 3742 } 3743 3744 static QualType decomposeTypeForEH(ASTContext &Context, QualType T, 3745 bool &IsConst, bool &IsVolatile, 3746 bool &IsUnaligned) { 3747 T = Context.getExceptionObjectType(T); 3748 3749 // C++14 [except.handle]p3: 3750 // A handler is a match for an exception object of type E if [...] 3751 // - the handler is of type cv T or const T& where T is a pointer type and 3752 // E is a pointer type that can be converted to T by [...] 3753 // - a qualification conversion 3754 IsConst = false; 3755 IsVolatile = false; 3756 IsUnaligned = false; 3757 QualType PointeeType = T->getPointeeType(); 3758 if (!PointeeType.isNull()) { 3759 IsConst = PointeeType.isConstQualified(); 3760 IsVolatile = PointeeType.isVolatileQualified(); 3761 IsUnaligned = PointeeType.getQualifiers().hasUnaligned(); 3762 } 3763 3764 // Member pointer types like "const int A::*" are represented by having RTTI 3765 // for "int A::*" and separately storing the const qualifier. 3766 if (const auto *MPTy = T->getAs<MemberPointerType>()) 3767 T = Context.getMemberPointerType(PointeeType.getUnqualifiedType(), 3768 MPTy->getClass()); 3769 3770 // Pointer types like "const int * const *" are represented by having RTTI 3771 // for "const int **" and separately storing the const qualifier. 3772 if (T->isPointerType()) 3773 T = Context.getPointerType(PointeeType.getUnqualifiedType()); 3774 3775 return T; 3776 } 3777 3778 CatchTypeInfo 3779 MicrosoftCXXABI::getAddrOfCXXCatchHandlerType(QualType Type, 3780 QualType CatchHandlerType) { 3781 // TypeDescriptors for exceptions never have qualified pointer types, 3782 // qualifiers are stored separately in order to support qualification 3783 // conversions. 3784 bool IsConst, IsVolatile, IsUnaligned; 3785 Type = 3786 decomposeTypeForEH(getContext(), Type, IsConst, IsVolatile, IsUnaligned); 3787 3788 bool IsReference = CatchHandlerType->isReferenceType(); 3789 3790 uint32_t Flags = 0; 3791 if (IsConst) 3792 Flags |= 1; 3793 if (IsVolatile) 3794 Flags |= 2; 3795 if (IsUnaligned) 3796 Flags |= 4; 3797 if (IsReference) 3798 Flags |= 8; 3799 3800 return CatchTypeInfo{getAddrOfRTTIDescriptor(Type)->stripPointerCasts(), 3801 Flags}; 3802 } 3803 3804 /// Gets a TypeDescriptor. Returns a llvm::Constant * rather than a 3805 /// llvm::GlobalVariable * because different type descriptors have different 3806 /// types, and need to be abstracted. They are abstracting by casting the 3807 /// address to an Int8PtrTy. 3808 llvm::Constant *MicrosoftCXXABI::getAddrOfRTTIDescriptor(QualType Type) { 3809 SmallString<256> MangledName; 3810 { 3811 llvm::raw_svector_ostream Out(MangledName); 3812 getMangleContext().mangleCXXRTTI(Type, Out); 3813 } 3814 3815 // Check to see if we've already declared this TypeDescriptor. 3816 if (llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(MangledName)) 3817 return llvm::ConstantExpr::getBitCast(GV, CGM.Int8PtrTy); 3818 3819 // Note for the future: If we would ever like to do deferred emission of 3820 // RTTI, check if emitting vtables opportunistically need any adjustment. 3821 3822 // Compute the fields for the TypeDescriptor. 3823 SmallString<256> TypeInfoString; 3824 { 3825 llvm::raw_svector_ostream Out(TypeInfoString); 3826 getMangleContext().mangleCXXRTTIName(Type, Out); 3827 } 3828 3829 // Declare and initialize the TypeDescriptor. 3830 llvm::Constant *Fields[] = { 3831 getTypeInfoVTable(CGM), // VFPtr 3832 llvm::ConstantPointerNull::get(CGM.Int8PtrTy), // Runtime data 3833 llvm::ConstantDataArray::getString(CGM.getLLVMContext(), TypeInfoString)}; 3834 llvm::StructType *TypeDescriptorType = 3835 getTypeDescriptorType(TypeInfoString); 3836 auto *Var = new llvm::GlobalVariable( 3837 CGM.getModule(), TypeDescriptorType, /*isConstant=*/false, 3838 getLinkageForRTTI(Type), 3839 llvm::ConstantStruct::get(TypeDescriptorType, Fields), 3840 MangledName); 3841 if (Var->isWeakForLinker()) 3842 Var->setComdat(CGM.getModule().getOrInsertComdat(Var->getName())); 3843 return llvm::ConstantExpr::getBitCast(Var, CGM.Int8PtrTy); 3844 } 3845 3846 /// Gets or a creates a Microsoft CompleteObjectLocator. 3847 llvm::GlobalVariable * 3848 MicrosoftCXXABI::getMSCompleteObjectLocator(const CXXRecordDecl *RD, 3849 const VPtrInfo &Info) { 3850 return MSRTTIBuilder(*this, RD).getCompleteObjectLocator(Info); 3851 } 3852 3853 void MicrosoftCXXABI::emitCXXStructor(GlobalDecl GD) { 3854 if (auto *ctor = dyn_cast<CXXConstructorDecl>(GD.getDecl())) { 3855 // There are no constructor variants, always emit the complete destructor. 3856 llvm::Function *Fn = 3857 CGM.codegenCXXStructor(GD.getWithCtorType(Ctor_Complete)); 3858 CGM.maybeSetTrivialComdat(*ctor, *Fn); 3859 return; 3860 } 3861 3862 auto *dtor = cast<CXXDestructorDecl>(GD.getDecl()); 3863 3864 // Emit the base destructor if the base and complete (vbase) destructors are 3865 // equivalent. This effectively implements -mconstructor-aliases as part of 3866 // the ABI. 3867 if (GD.getDtorType() == Dtor_Complete && 3868 dtor->getParent()->getNumVBases() == 0) 3869 GD = GD.getWithDtorType(Dtor_Base); 3870 3871 // The base destructor is equivalent to the base destructor of its 3872 // base class if there is exactly one non-virtual base class with a 3873 // non-trivial destructor, there are no fields with a non-trivial 3874 // destructor, and the body of the destructor is trivial. 3875 if (GD.getDtorType() == Dtor_Base && !CGM.TryEmitBaseDestructorAsAlias(dtor)) 3876 return; 3877 3878 llvm::Function *Fn = CGM.codegenCXXStructor(GD); 3879 if (Fn->isWeakForLinker()) 3880 Fn->setComdat(CGM.getModule().getOrInsertComdat(Fn->getName())); 3881 } 3882 3883 llvm::Function * 3884 MicrosoftCXXABI::getAddrOfCXXCtorClosure(const CXXConstructorDecl *CD, 3885 CXXCtorType CT) { 3886 assert(CT == Ctor_CopyingClosure || CT == Ctor_DefaultClosure); 3887 3888 // Calculate the mangled name. 3889 SmallString<256> ThunkName; 3890 llvm::raw_svector_ostream Out(ThunkName); 3891 getMangleContext().mangleCXXCtor(CD, CT, Out); 3892 3893 // If the thunk has been generated previously, just return it. 3894 if (llvm::GlobalValue *GV = CGM.getModule().getNamedValue(ThunkName)) 3895 return cast<llvm::Function>(GV); 3896 3897 // Create the llvm::Function. 3898 const CGFunctionInfo &FnInfo = CGM.getTypes().arrangeMSCtorClosure(CD, CT); 3899 llvm::FunctionType *ThunkTy = CGM.getTypes().GetFunctionType(FnInfo); 3900 const CXXRecordDecl *RD = CD->getParent(); 3901 QualType RecordTy = getContext().getRecordType(RD); 3902 llvm::Function *ThunkFn = llvm::Function::Create( 3903 ThunkTy, getLinkageForRTTI(RecordTy), ThunkName.str(), &CGM.getModule()); 3904 ThunkFn->setCallingConv(static_cast<llvm::CallingConv::ID>( 3905 FnInfo.getEffectiveCallingConvention())); 3906 if (ThunkFn->isWeakForLinker()) 3907 ThunkFn->setComdat(CGM.getModule().getOrInsertComdat(ThunkFn->getName())); 3908 bool IsCopy = CT == Ctor_CopyingClosure; 3909 3910 // Start codegen. 3911 CodeGenFunction CGF(CGM); 3912 CGF.CurGD = GlobalDecl(CD, Ctor_Complete); 3913 3914 // Build FunctionArgs. 3915 FunctionArgList FunctionArgs; 3916 3917 // A constructor always starts with a 'this' pointer as its first argument. 3918 buildThisParam(CGF, FunctionArgs); 3919 3920 // Following the 'this' pointer is a reference to the source object that we 3921 // are copying from. 3922 ImplicitParamDecl SrcParam( 3923 getContext(), /*DC=*/nullptr, SourceLocation(), 3924 &getContext().Idents.get("src"), 3925 getContext().getLValueReferenceType(RecordTy, 3926 /*SpelledAsLValue=*/true), 3927 ImplicitParamDecl::Other); 3928 if (IsCopy) 3929 FunctionArgs.push_back(&SrcParam); 3930 3931 // Constructors for classes which utilize virtual bases have an additional 3932 // parameter which indicates whether or not it is being delegated to by a more 3933 // derived constructor. 3934 ImplicitParamDecl IsMostDerived(getContext(), /*DC=*/nullptr, 3935 SourceLocation(), 3936 &getContext().Idents.get("is_most_derived"), 3937 getContext().IntTy, ImplicitParamDecl::Other); 3938 // Only add the parameter to the list if the class has virtual bases. 3939 if (RD->getNumVBases() > 0) 3940 FunctionArgs.push_back(&IsMostDerived); 3941 3942 // Start defining the function. 3943 auto NL = ApplyDebugLocation::CreateEmpty(CGF); 3944 CGF.StartFunction(GlobalDecl(), FnInfo.getReturnType(), ThunkFn, FnInfo, 3945 FunctionArgs, CD->getLocation(), SourceLocation()); 3946 // Create a scope with an artificial location for the body of this function. 3947 auto AL = ApplyDebugLocation::CreateArtificial(CGF); 3948 setCXXABIThisValue(CGF, loadIncomingCXXThis(CGF)); 3949 llvm::Value *This = getThisValue(CGF); 3950 3951 llvm::Value *SrcVal = 3952 IsCopy ? CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(&SrcParam), "src") 3953 : nullptr; 3954 3955 CallArgList Args; 3956 3957 // Push the this ptr. 3958 Args.add(RValue::get(This), CD->getThisType()); 3959 3960 // Push the src ptr. 3961 if (SrcVal) 3962 Args.add(RValue::get(SrcVal), SrcParam.getType()); 3963 3964 // Add the rest of the default arguments. 3965 SmallVector<const Stmt *, 4> ArgVec; 3966 ArrayRef<ParmVarDecl *> params = CD->parameters().drop_front(IsCopy ? 1 : 0); 3967 for (const ParmVarDecl *PD : params) { 3968 assert(PD->hasDefaultArg() && "ctor closure lacks default args"); 3969 ArgVec.push_back(PD->getDefaultArg()); 3970 } 3971 3972 CodeGenFunction::RunCleanupsScope Cleanups(CGF); 3973 3974 const auto *FPT = CD->getType()->castAs<FunctionProtoType>(); 3975 CGF.EmitCallArgs(Args, FPT, llvm::makeArrayRef(ArgVec), CD, IsCopy ? 1 : 0); 3976 3977 // Insert any ABI-specific implicit constructor arguments. 3978 AddedStructorArgs ExtraArgs = 3979 addImplicitConstructorArgs(CGF, CD, Ctor_Complete, 3980 /*ForVirtualBase=*/false, 3981 /*Delegating=*/false, Args); 3982 // Call the destructor with our arguments. 3983 llvm::Constant *CalleePtr = 3984 CGM.getAddrOfCXXStructor(GlobalDecl(CD, Ctor_Complete)); 3985 CGCallee Callee = 3986 CGCallee::forDirect(CalleePtr, GlobalDecl(CD, Ctor_Complete)); 3987 const CGFunctionInfo &CalleeInfo = CGM.getTypes().arrangeCXXConstructorCall( 3988 Args, CD, Ctor_Complete, ExtraArgs.Prefix, ExtraArgs.Suffix); 3989 CGF.EmitCall(CalleeInfo, Callee, ReturnValueSlot(), Args); 3990 3991 Cleanups.ForceCleanup(); 3992 3993 // Emit the ret instruction, remove any temporary instructions created for the 3994 // aid of CodeGen. 3995 CGF.FinishFunction(SourceLocation()); 3996 3997 return ThunkFn; 3998 } 3999 4000 llvm::Constant *MicrosoftCXXABI::getCatchableType(QualType T, 4001 uint32_t NVOffset, 4002 int32_t VBPtrOffset, 4003 uint32_t VBIndex) { 4004 assert(!T->isReferenceType()); 4005 4006 CXXRecordDecl *RD = T->getAsCXXRecordDecl(); 4007 const CXXConstructorDecl *CD = 4008 RD ? CGM.getContext().getCopyConstructorForExceptionObject(RD) : nullptr; 4009 CXXCtorType CT = Ctor_Complete; 4010 if (CD) 4011 if (!hasDefaultCXXMethodCC(getContext(), CD) || CD->getNumParams() != 1) 4012 CT = Ctor_CopyingClosure; 4013 4014 uint32_t Size = getContext().getTypeSizeInChars(T).getQuantity(); 4015 SmallString<256> MangledName; 4016 { 4017 llvm::raw_svector_ostream Out(MangledName); 4018 getMangleContext().mangleCXXCatchableType(T, CD, CT, Size, NVOffset, 4019 VBPtrOffset, VBIndex, Out); 4020 } 4021 if (llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(MangledName)) 4022 return getImageRelativeConstant(GV); 4023 4024 // The TypeDescriptor is used by the runtime to determine if a catch handler 4025 // is appropriate for the exception object. 4026 llvm::Constant *TD = getImageRelativeConstant(getAddrOfRTTIDescriptor(T)); 4027 4028 // The runtime is responsible for calling the copy constructor if the 4029 // exception is caught by value. 4030 llvm::Constant *CopyCtor; 4031 if (CD) { 4032 if (CT == Ctor_CopyingClosure) 4033 CopyCtor = getAddrOfCXXCtorClosure(CD, Ctor_CopyingClosure); 4034 else 4035 CopyCtor = CGM.getAddrOfCXXStructor(GlobalDecl(CD, Ctor_Complete)); 4036 4037 CopyCtor = llvm::ConstantExpr::getBitCast(CopyCtor, CGM.Int8PtrTy); 4038 } else { 4039 CopyCtor = llvm::Constant::getNullValue(CGM.Int8PtrTy); 4040 } 4041 CopyCtor = getImageRelativeConstant(CopyCtor); 4042 4043 bool IsScalar = !RD; 4044 bool HasVirtualBases = false; 4045 bool IsStdBadAlloc = false; // std::bad_alloc is special for some reason. 4046 QualType PointeeType = T; 4047 if (T->isPointerType()) 4048 PointeeType = T->getPointeeType(); 4049 if (const CXXRecordDecl *RD = PointeeType->getAsCXXRecordDecl()) { 4050 HasVirtualBases = RD->getNumVBases() > 0; 4051 if (IdentifierInfo *II = RD->getIdentifier()) 4052 IsStdBadAlloc = II->isStr("bad_alloc") && RD->isInStdNamespace(); 4053 } 4054 4055 // Encode the relevant CatchableType properties into the Flags bitfield. 4056 // FIXME: Figure out how bits 2 or 8 can get set. 4057 uint32_t Flags = 0; 4058 if (IsScalar) 4059 Flags |= 1; 4060 if (HasVirtualBases) 4061 Flags |= 4; 4062 if (IsStdBadAlloc) 4063 Flags |= 16; 4064 4065 llvm::Constant *Fields[] = { 4066 llvm::ConstantInt::get(CGM.IntTy, Flags), // Flags 4067 TD, // TypeDescriptor 4068 llvm::ConstantInt::get(CGM.IntTy, NVOffset), // NonVirtualAdjustment 4069 llvm::ConstantInt::get(CGM.IntTy, VBPtrOffset), // OffsetToVBPtr 4070 llvm::ConstantInt::get(CGM.IntTy, VBIndex), // VBTableIndex 4071 llvm::ConstantInt::get(CGM.IntTy, Size), // Size 4072 CopyCtor // CopyCtor 4073 }; 4074 llvm::StructType *CTType = getCatchableTypeType(); 4075 auto *GV = new llvm::GlobalVariable( 4076 CGM.getModule(), CTType, /*isConstant=*/true, getLinkageForRTTI(T), 4077 llvm::ConstantStruct::get(CTType, Fields), MangledName); 4078 GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); 4079 GV->setSection(".xdata"); 4080 if (GV->isWeakForLinker()) 4081 GV->setComdat(CGM.getModule().getOrInsertComdat(GV->getName())); 4082 return getImageRelativeConstant(GV); 4083 } 4084 4085 llvm::GlobalVariable *MicrosoftCXXABI::getCatchableTypeArray(QualType T) { 4086 assert(!T->isReferenceType()); 4087 4088 // See if we've already generated a CatchableTypeArray for this type before. 4089 llvm::GlobalVariable *&CTA = CatchableTypeArrays[T]; 4090 if (CTA) 4091 return CTA; 4092 4093 // Ensure that we don't have duplicate entries in our CatchableTypeArray by 4094 // using a SmallSetVector. Duplicates may arise due to virtual bases 4095 // occurring more than once in the hierarchy. 4096 llvm::SmallSetVector<llvm::Constant *, 2> CatchableTypes; 4097 4098 // C++14 [except.handle]p3: 4099 // A handler is a match for an exception object of type E if [...] 4100 // - the handler is of type cv T or cv T& and T is an unambiguous public 4101 // base class of E, or 4102 // - the handler is of type cv T or const T& where T is a pointer type and 4103 // E is a pointer type that can be converted to T by [...] 4104 // - a standard pointer conversion (4.10) not involving conversions to 4105 // pointers to private or protected or ambiguous classes 4106 const CXXRecordDecl *MostDerivedClass = nullptr; 4107 bool IsPointer = T->isPointerType(); 4108 if (IsPointer) 4109 MostDerivedClass = T->getPointeeType()->getAsCXXRecordDecl(); 4110 else 4111 MostDerivedClass = T->getAsCXXRecordDecl(); 4112 4113 // Collect all the unambiguous public bases of the MostDerivedClass. 4114 if (MostDerivedClass) { 4115 const ASTContext &Context = getContext(); 4116 const ASTRecordLayout &MostDerivedLayout = 4117 Context.getASTRecordLayout(MostDerivedClass); 4118 MicrosoftVTableContext &VTableContext = CGM.getMicrosoftVTableContext(); 4119 SmallVector<MSRTTIClass, 8> Classes; 4120 serializeClassHierarchy(Classes, MostDerivedClass); 4121 Classes.front().initialize(/*Parent=*/nullptr, /*Specifier=*/nullptr); 4122 detectAmbiguousBases(Classes); 4123 for (const MSRTTIClass &Class : Classes) { 4124 // Skip any ambiguous or private bases. 4125 if (Class.Flags & 4126 (MSRTTIClass::IsPrivateOnPath | MSRTTIClass::IsAmbiguous)) 4127 continue; 4128 // Write down how to convert from a derived pointer to a base pointer. 4129 uint32_t OffsetInVBTable = 0; 4130 int32_t VBPtrOffset = -1; 4131 if (Class.VirtualRoot) { 4132 OffsetInVBTable = 4133 VTableContext.getVBTableIndex(MostDerivedClass, Class.VirtualRoot)*4; 4134 VBPtrOffset = MostDerivedLayout.getVBPtrOffset().getQuantity(); 4135 } 4136 4137 // Turn our record back into a pointer if the exception object is a 4138 // pointer. 4139 QualType RTTITy = QualType(Class.RD->getTypeForDecl(), 0); 4140 if (IsPointer) 4141 RTTITy = Context.getPointerType(RTTITy); 4142 CatchableTypes.insert(getCatchableType(RTTITy, Class.OffsetInVBase, 4143 VBPtrOffset, OffsetInVBTable)); 4144 } 4145 } 4146 4147 // C++14 [except.handle]p3: 4148 // A handler is a match for an exception object of type E if 4149 // - The handler is of type cv T or cv T& and E and T are the same type 4150 // (ignoring the top-level cv-qualifiers) 4151 CatchableTypes.insert(getCatchableType(T)); 4152 4153 // C++14 [except.handle]p3: 4154 // A handler is a match for an exception object of type E if 4155 // - the handler is of type cv T or const T& where T is a pointer type and 4156 // E is a pointer type that can be converted to T by [...] 4157 // - a standard pointer conversion (4.10) not involving conversions to 4158 // pointers to private or protected or ambiguous classes 4159 // 4160 // C++14 [conv.ptr]p2: 4161 // A prvalue of type "pointer to cv T," where T is an object type, can be 4162 // converted to a prvalue of type "pointer to cv void". 4163 if (IsPointer && T->getPointeeType()->isObjectType()) 4164 CatchableTypes.insert(getCatchableType(getContext().VoidPtrTy)); 4165 4166 // C++14 [except.handle]p3: 4167 // A handler is a match for an exception object of type E if [...] 4168 // - the handler is of type cv T or const T& where T is a pointer or 4169 // pointer to member type and E is std::nullptr_t. 4170 // 4171 // We cannot possibly list all possible pointer types here, making this 4172 // implementation incompatible with the standard. However, MSVC includes an 4173 // entry for pointer-to-void in this case. Let's do the same. 4174 if (T->isNullPtrType()) 4175 CatchableTypes.insert(getCatchableType(getContext().VoidPtrTy)); 4176 4177 uint32_t NumEntries = CatchableTypes.size(); 4178 llvm::Type *CTType = 4179 getImageRelativeType(getCatchableTypeType()->getPointerTo()); 4180 llvm::ArrayType *AT = llvm::ArrayType::get(CTType, NumEntries); 4181 llvm::StructType *CTAType = getCatchableTypeArrayType(NumEntries); 4182 llvm::Constant *Fields[] = { 4183 llvm::ConstantInt::get(CGM.IntTy, NumEntries), // NumEntries 4184 llvm::ConstantArray::get( 4185 AT, llvm::makeArrayRef(CatchableTypes.begin(), 4186 CatchableTypes.end())) // CatchableTypes 4187 }; 4188 SmallString<256> MangledName; 4189 { 4190 llvm::raw_svector_ostream Out(MangledName); 4191 getMangleContext().mangleCXXCatchableTypeArray(T, NumEntries, Out); 4192 } 4193 CTA = new llvm::GlobalVariable( 4194 CGM.getModule(), CTAType, /*isConstant=*/true, getLinkageForRTTI(T), 4195 llvm::ConstantStruct::get(CTAType, Fields), MangledName); 4196 CTA->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); 4197 CTA->setSection(".xdata"); 4198 if (CTA->isWeakForLinker()) 4199 CTA->setComdat(CGM.getModule().getOrInsertComdat(CTA->getName())); 4200 return CTA; 4201 } 4202 4203 llvm::GlobalVariable *MicrosoftCXXABI::getThrowInfo(QualType T) { 4204 bool IsConst, IsVolatile, IsUnaligned; 4205 T = decomposeTypeForEH(getContext(), T, IsConst, IsVolatile, IsUnaligned); 4206 4207 // The CatchableTypeArray enumerates the various (CV-unqualified) types that 4208 // the exception object may be caught as. 4209 llvm::GlobalVariable *CTA = getCatchableTypeArray(T); 4210 // The first field in a CatchableTypeArray is the number of CatchableTypes. 4211 // This is used as a component of the mangled name which means that we need to 4212 // know what it is in order to see if we have previously generated the 4213 // ThrowInfo. 4214 uint32_t NumEntries = 4215 cast<llvm::ConstantInt>(CTA->getInitializer()->getAggregateElement(0U)) 4216 ->getLimitedValue(); 4217 4218 SmallString<256> MangledName; 4219 { 4220 llvm::raw_svector_ostream Out(MangledName); 4221 getMangleContext().mangleCXXThrowInfo(T, IsConst, IsVolatile, IsUnaligned, 4222 NumEntries, Out); 4223 } 4224 4225 // Reuse a previously generated ThrowInfo if we have generated an appropriate 4226 // one before. 4227 if (llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(MangledName)) 4228 return GV; 4229 4230 // The RTTI TypeDescriptor uses an unqualified type but catch clauses must 4231 // be at least as CV qualified. Encode this requirement into the Flags 4232 // bitfield. 4233 uint32_t Flags = 0; 4234 if (IsConst) 4235 Flags |= 1; 4236 if (IsVolatile) 4237 Flags |= 2; 4238 if (IsUnaligned) 4239 Flags |= 4; 4240 4241 // The cleanup-function (a destructor) must be called when the exception 4242 // object's lifetime ends. 4243 llvm::Constant *CleanupFn = llvm::Constant::getNullValue(CGM.Int8PtrTy); 4244 if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl()) 4245 if (CXXDestructorDecl *DtorD = RD->getDestructor()) 4246 if (!DtorD->isTrivial()) 4247 CleanupFn = llvm::ConstantExpr::getBitCast( 4248 CGM.getAddrOfCXXStructor(GlobalDecl(DtorD, Dtor_Complete)), 4249 CGM.Int8PtrTy); 4250 // This is unused as far as we can tell, initialize it to null. 4251 llvm::Constant *ForwardCompat = 4252 getImageRelativeConstant(llvm::Constant::getNullValue(CGM.Int8PtrTy)); 4253 llvm::Constant *PointerToCatchableTypes = getImageRelativeConstant( 4254 llvm::ConstantExpr::getBitCast(CTA, CGM.Int8PtrTy)); 4255 llvm::StructType *TIType = getThrowInfoType(); 4256 llvm::Constant *Fields[] = { 4257 llvm::ConstantInt::get(CGM.IntTy, Flags), // Flags 4258 getImageRelativeConstant(CleanupFn), // CleanupFn 4259 ForwardCompat, // ForwardCompat 4260 PointerToCatchableTypes // CatchableTypeArray 4261 }; 4262 auto *GV = new llvm::GlobalVariable( 4263 CGM.getModule(), TIType, /*isConstant=*/true, getLinkageForRTTI(T), 4264 llvm::ConstantStruct::get(TIType, Fields), StringRef(MangledName)); 4265 GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); 4266 GV->setSection(".xdata"); 4267 if (GV->isWeakForLinker()) 4268 GV->setComdat(CGM.getModule().getOrInsertComdat(GV->getName())); 4269 return GV; 4270 } 4271 4272 void MicrosoftCXXABI::emitThrow(CodeGenFunction &CGF, const CXXThrowExpr *E) { 4273 const Expr *SubExpr = E->getSubExpr(); 4274 QualType ThrowType = SubExpr->getType(); 4275 // The exception object lives on the stack and it's address is passed to the 4276 // runtime function. 4277 Address AI = CGF.CreateMemTemp(ThrowType); 4278 CGF.EmitAnyExprToMem(SubExpr, AI, ThrowType.getQualifiers(), 4279 /*IsInit=*/true); 4280 4281 // The so-called ThrowInfo is used to describe how the exception object may be 4282 // caught. 4283 llvm::GlobalVariable *TI = getThrowInfo(ThrowType); 4284 4285 // Call into the runtime to throw the exception. 4286 llvm::Value *Args[] = { 4287 CGF.Builder.CreateBitCast(AI.getPointer(), CGM.Int8PtrTy), 4288 TI 4289 }; 4290 CGF.EmitNoreturnRuntimeCallOrInvoke(getThrowFn(), Args); 4291 } 4292 4293 std::pair<llvm::Value *, const CXXRecordDecl *> 4294 MicrosoftCXXABI::LoadVTablePtr(CodeGenFunction &CGF, Address This, 4295 const CXXRecordDecl *RD) { 4296 std::tie(This, std::ignore, RD) = 4297 performBaseAdjustment(CGF, This, QualType(RD->getTypeForDecl(), 0)); 4298 return {CGF.GetVTablePtr(This, CGM.Int8PtrTy, RD), RD}; 4299 } 4300