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