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