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