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