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