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