2010-06-10 07:25:41 +08:00
|
|
|
//===--- MicrosoftCXXABI.cpp - Emit LLVM Code from ASTs for a Module ------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2011-04-15 13:22:18 +08:00
|
|
|
// This provides C++ code generation targeting the Microsoft Visual C++ ABI.
|
2010-06-10 07:25:41 +08:00
|
|
|
// The class in this file generates structures that follow the Microsoft
|
|
|
|
// Visual C++ ABI, which is actually not very well documented at all outside
|
|
|
|
// of Microsoft.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "CGCXXABI.h"
|
2013-06-19 23:20:38 +08:00
|
|
|
#include "CGVTables.h"
|
2014-01-07 19:51:46 +08:00
|
|
|
#include "CodeGenModule.h"
|
2015-03-07 02:53:55 +08:00
|
|
|
#include "CodeGenTypes.h"
|
2015-03-04 03:21:04 +08:00
|
|
|
#include "TargetInfo.h"
|
2010-06-10 07:25:41 +08:00
|
|
|
#include "clang/AST/Decl.h"
|
|
|
|
#include "clang/AST/DeclCXX.h"
|
2015-03-04 03:21:04 +08:00
|
|
|
#include "clang/AST/StmtCXX.h"
|
2013-07-30 17:46:19 +08:00
|
|
|
#include "clang/AST/VTableBuilder.h"
|
2014-07-07 16:09:15 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2013-09-27 22:48:01 +08:00
|
|
|
#include "llvm/ADT/StringSet.h"
|
2014-06-23 03:05:33 +08:00
|
|
|
#include "llvm/IR/CallSite.h"
|
2015-03-04 03:21:04 +08:00
|
|
|
#include "llvm/IR/Intrinsics.h"
|
2010-06-10 07:25:41 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace CodeGen;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2014-01-03 08:14:35 +08:00
|
|
|
/// Holds all the vbtable globals for a given class.
|
|
|
|
struct VBTableGlobals {
|
2014-02-28 03:40:09 +08:00
|
|
|
const VPtrInfoVector *VBTables;
|
2014-01-03 08:14:35 +08:00
|
|
|
SmallVector<llvm::GlobalVariable *, 2> Globals;
|
|
|
|
};
|
|
|
|
|
2010-08-16 11:33:14 +08:00
|
|
|
class MicrosoftCXXABI : public CGCXXABI {
|
2010-06-10 07:25:41 +08:00
|
|
|
public:
|
2014-07-07 16:09:15 +08:00
|
|
|
MicrosoftCXXABI(CodeGenModule &CGM)
|
|
|
|
: CGCXXABI(CGM), BaseClassDescriptorType(nullptr),
|
|
|
|
ClassHierarchyDescriptorType(nullptr),
|
2015-03-05 08:46:22 +08:00
|
|
|
CompleteObjectLocatorType(nullptr), CatchableTypeType(nullptr),
|
2015-03-30 05:55:10 +08:00
|
|
|
ThrowInfoType(nullptr), CatchHandlerTypeType(nullptr) {}
|
2010-08-31 15:33:07 +08:00
|
|
|
|
2014-03-12 14:41:41 +08:00
|
|
|
bool HasThisReturn(GlobalDecl GD) const override;
|
2014-11-01 04:09:12 +08:00
|
|
|
bool hasMostDerivedReturn(GlobalDecl GD) const override;
|
2013-07-01 04:40:16 +08:00
|
|
|
|
2014-05-14 06:05:45 +08:00
|
|
|
bool classifyReturnType(CGFunctionInfo &FI) const override;
|
2013-04-17 20:54:10 +08:00
|
|
|
|
2014-05-03 08:33:28 +08:00
|
|
|
RecordArgABI getRecordArgABI(const CXXRecordDecl *RD) const override;
|
2013-04-17 20:54:10 +08:00
|
|
|
|
2014-05-10 06:46:15 +08:00
|
|
|
bool isSRetParameterAfterThis() const override { return true; }
|
|
|
|
|
2014-09-12 07:05:02 +08:00
|
|
|
size_t getSrcArgforCopyCtor(const CXXConstructorDecl *CD,
|
|
|
|
FunctionArgList &Args) const override {
|
|
|
|
assert(Args.size() >= 2 &&
|
|
|
|
"expected the arglist to have at least two args!");
|
|
|
|
// The 'most_derived' parameter goes second if the ctor is variadic and
|
|
|
|
// has v-bases.
|
|
|
|
if (CD->getParent()->getNumVBases() > 0 &&
|
|
|
|
CD->getType()->castAs<FunctionProtoType>()->isVariadic())
|
|
|
|
return 2;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-03-12 14:41:41 +08:00
|
|
|
StringRef GetPureVirtualCallName() override { return "_purecall"; }
|
|
|
|
StringRef GetDeletedVirtualCallName() override { return "_purecall"; }
|
2012-07-18 01:10:11 +08:00
|
|
|
|
2014-11-01 15:37:17 +08:00
|
|
|
void emitVirtualObjectDelete(CodeGenFunction &CGF, const CXXDeleteExpr *DE,
|
2014-11-01 04:09:12 +08:00
|
|
|
llvm::Value *Ptr, QualType ElementType,
|
|
|
|
const CXXDestructorDecl *Dtor) override;
|
2012-09-25 18:10:39 +08:00
|
|
|
|
2014-11-25 16:59:34 +08:00
|
|
|
void emitRethrow(CodeGenFunction &CGF, bool isNoReturn) override;
|
2015-03-05 08:46:22 +08:00
|
|
|
void emitThrow(CodeGenFunction &CGF, const CXXThrowExpr *E) override;
|
2014-11-25 15:20:20 +08:00
|
|
|
|
2015-03-04 03:21:04 +08:00
|
|
|
void emitBeginCatch(CodeGenFunction &CGF, const CXXCatchStmt *C) override;
|
|
|
|
|
2014-07-07 14:20:47 +08:00
|
|
|
llvm::GlobalVariable *getMSCompleteObjectLocator(const CXXRecordDecl *RD,
|
|
|
|
const VPtrInfo *Info);
|
|
|
|
|
2015-03-18 04:35:00 +08:00
|
|
|
llvm::Constant *getAddrOfRTTIDescriptor(QualType Ty) override;
|
2015-03-18 04:35:05 +08:00
|
|
|
llvm::Constant *
|
2015-03-30 05:55:10 +08:00
|
|
|
getAddrOfCXXCatchHandlerType(QualType Ty, QualType CatchHandlerType) override;
|
2014-07-07 14:20:47 +08:00
|
|
|
|
2014-06-23 03:05:33 +08:00
|
|
|
bool shouldTypeidBeNullChecked(bool IsDeref, QualType SrcRecordTy) override;
|
|
|
|
void EmitBadTypeidCall(CodeGenFunction &CGF) override;
|
|
|
|
llvm::Value *EmitTypeid(CodeGenFunction &CGF, QualType SrcRecordTy,
|
|
|
|
llvm::Value *ThisPtr,
|
|
|
|
llvm::Type *StdTypeInfoPtrTy) override;
|
|
|
|
|
|
|
|
bool shouldDynamicCastCallBeNullChecked(bool SrcIsPtr,
|
|
|
|
QualType SrcRecordTy) override;
|
|
|
|
|
|
|
|
llvm::Value *EmitDynamicCastCall(CodeGenFunction &CGF, llvm::Value *Value,
|
|
|
|
QualType SrcRecordTy, QualType DestTy,
|
|
|
|
QualType DestRecordTy,
|
|
|
|
llvm::BasicBlock *CastEnd) override;
|
|
|
|
|
|
|
|
llvm::Value *EmitDynamicCastToVoid(CodeGenFunction &CGF, llvm::Value *Value,
|
|
|
|
QualType SrcRecordTy,
|
|
|
|
QualType DestTy) override;
|
|
|
|
|
|
|
|
bool EmitBadCastCall(CodeGenFunction &CGF) override;
|
|
|
|
|
2014-03-12 14:41:41 +08:00
|
|
|
llvm::Value *
|
|
|
|
GetVirtualBaseClassOffset(CodeGenFunction &CGF, llvm::Value *This,
|
|
|
|
const CXXRecordDecl *ClassDecl,
|
|
|
|
const CXXRecordDecl *BaseClassDecl) override;
|
2013-05-30 02:02:47 +08:00
|
|
|
|
2014-03-12 14:41:41 +08:00
|
|
|
llvm::BasicBlock *
|
|
|
|
EmitCtorCompleteObjectHandler(CodeGenFunction &CGF,
|
|
|
|
const CXXRecordDecl *RD) override;
|
2013-02-27 21:46:31 +08:00
|
|
|
|
2013-10-10 02:16:58 +08:00
|
|
|
void initializeHiddenVirtualInheritanceMembers(CodeGenFunction &CGF,
|
2014-03-12 14:41:41 +08:00
|
|
|
const CXXRecordDecl *RD) override;
|
2013-10-10 02:16:58 +08:00
|
|
|
|
2014-03-12 14:41:41 +08:00
|
|
|
void EmitCXXConstructors(const CXXConstructorDecl *D) override;
|
2013-08-05 01:30:04 +08:00
|
|
|
|
[ms-cxxabi] Emit linkonce complete dtors in TUs that need them
Based on Peter Collingbourne's destructor patches.
Prior to this change, clang was considering ?1 to be the complete
destructor and the base destructor, which was wrong. This lead to
crashes when clang tried to emit two LLVM functions with the same name.
In this ABI, TUs with non-inline dtors might not emit a complete
destructor. They are emitted as inline thunks in TUs that need them,
and they always delegate to the base dtors of the complete class and its
virtual bases. This change uses the DeferredDecls machinery to emit
complete dtors as needed.
Currently in clang try body destructors can catch exceptions thrown by
virtual base destructors. In the Microsoft C++ ABI, clang may not have
the destructor definition, in which case clang won't wrap the virtual
virtual base destructor calls in a try-catch. Diagnosing this in user
code is TODO.
Finally, for classes that don't use virtual inheritance, MSVC always
calls the base destructor (?1) directly. This is a useful code size
optimization that avoids emitting lots of extra thunks or aliases.
Implementing it also means our existing tests continue to pass, and is
consistent with MSVC's output.
We can do the same for Itanium by tweaking GetAddrOfCXXDestructor, but
it will require further testing.
Reviewers: rjmccall
CC: cfe-commits
Differential Revision: http://llvm-reviews.chandlerc.com/D1066
llvm-svn: 186828
2013-07-22 21:51:44 +08:00
|
|
|
// Background on MSVC destructors
|
|
|
|
// ==============================
|
|
|
|
//
|
|
|
|
// Both Itanium and MSVC ABIs have destructor variants. The variant names
|
|
|
|
// roughly correspond in the following way:
|
|
|
|
// Itanium Microsoft
|
|
|
|
// Base -> no name, just ~Class
|
|
|
|
// Complete -> vbase destructor
|
|
|
|
// Deleting -> scalar deleting destructor
|
|
|
|
// vector deleting destructor
|
|
|
|
//
|
|
|
|
// The base and complete destructors are the same as in Itanium, although the
|
|
|
|
// complete destructor does not accept a VTT parameter when there are virtual
|
|
|
|
// bases. A separate mechanism involving vtordisps is used to ensure that
|
|
|
|
// virtual methods of destroyed subobjects are not called.
|
|
|
|
//
|
|
|
|
// The deleting destructors accept an i32 bitfield as a second parameter. Bit
|
|
|
|
// 1 indicates if the memory should be deleted. Bit 2 indicates if the this
|
|
|
|
// pointer points to an array. The scalar deleting destructor assumes that
|
|
|
|
// bit 2 is zero, and therefore does not contain a loop.
|
|
|
|
//
|
|
|
|
// For virtual destructors, only one entry is reserved in the vftable, and it
|
|
|
|
// always points to the vector deleting destructor. The vector deleting
|
|
|
|
// destructor is the most general, so it can be used to destroy objects in
|
|
|
|
// place, delete single heap objects, or delete arrays.
|
|
|
|
//
|
|
|
|
// A TU defining a non-inline destructor is only guaranteed to emit a base
|
|
|
|
// destructor, and all of the other variants are emitted on an as-needed basis
|
|
|
|
// in COMDATs. Because a non-base destructor can be emitted in a TU that
|
|
|
|
// lacks a definition for the destructor, non-base destructors must always
|
|
|
|
// delegate to or alias the base destructor.
|
|
|
|
|
2014-09-09 00:01:27 +08:00
|
|
|
void buildStructorSignature(const CXXMethodDecl *MD, StructorType T,
|
|
|
|
SmallVectorImpl<CanQualType> &ArgTys) override;
|
2010-08-31 15:33:07 +08:00
|
|
|
|
[ms-cxxabi] Emit linkonce complete dtors in TUs that need them
Based on Peter Collingbourne's destructor patches.
Prior to this change, clang was considering ?1 to be the complete
destructor and the base destructor, which was wrong. This lead to
crashes when clang tried to emit two LLVM functions with the same name.
In this ABI, TUs with non-inline dtors might not emit a complete
destructor. They are emitted as inline thunks in TUs that need them,
and they always delegate to the base dtors of the complete class and its
virtual bases. This change uses the DeferredDecls machinery to emit
complete dtors as needed.
Currently in clang try body destructors can catch exceptions thrown by
virtual base destructors. In the Microsoft C++ ABI, clang may not have
the destructor definition, in which case clang won't wrap the virtual
virtual base destructor calls in a try-catch. Diagnosing this in user
code is TODO.
Finally, for classes that don't use virtual inheritance, MSVC always
calls the base destructor (?1) directly. This is a useful code size
optimization that avoids emitting lots of extra thunks or aliases.
Implementing it also means our existing tests continue to pass, and is
consistent with MSVC's output.
We can do the same for Itanium by tweaking GetAddrOfCXXDestructor, but
it will require further testing.
Reviewers: rjmccall
CC: cfe-commits
Differential Revision: http://llvm-reviews.chandlerc.com/D1066
llvm-svn: 186828
2013-07-22 21:51:44 +08:00
|
|
|
/// Non-base dtors should be emitted as delegating thunks in this ABI.
|
|
|
|
bool useThunkForDtorVariant(const CXXDestructorDecl *Dtor,
|
2014-03-12 14:41:41 +08:00
|
|
|
CXXDtorType DT) const override {
|
[ms-cxxabi] Emit linkonce complete dtors in TUs that need them
Based on Peter Collingbourne's destructor patches.
Prior to this change, clang was considering ?1 to be the complete
destructor and the base destructor, which was wrong. This lead to
crashes when clang tried to emit two LLVM functions with the same name.
In this ABI, TUs with non-inline dtors might not emit a complete
destructor. They are emitted as inline thunks in TUs that need them,
and they always delegate to the base dtors of the complete class and its
virtual bases. This change uses the DeferredDecls machinery to emit
complete dtors as needed.
Currently in clang try body destructors can catch exceptions thrown by
virtual base destructors. In the Microsoft C++ ABI, clang may not have
the destructor definition, in which case clang won't wrap the virtual
virtual base destructor calls in a try-catch. Diagnosing this in user
code is TODO.
Finally, for classes that don't use virtual inheritance, MSVC always
calls the base destructor (?1) directly. This is a useful code size
optimization that avoids emitting lots of extra thunks or aliases.
Implementing it also means our existing tests continue to pass, and is
consistent with MSVC's output.
We can do the same for Itanium by tweaking GetAddrOfCXXDestructor, but
it will require further testing.
Reviewers: rjmccall
CC: cfe-commits
Differential Revision: http://llvm-reviews.chandlerc.com/D1066
llvm-svn: 186828
2013-07-22 21:51:44 +08:00
|
|
|
return DT != Dtor_Base;
|
|
|
|
}
|
|
|
|
|
2014-03-12 14:41:41 +08:00
|
|
|
void EmitCXXDestructors(const CXXDestructorDecl *D) override;
|
[ms-cxxabi] Emit linkonce complete dtors in TUs that need them
Based on Peter Collingbourne's destructor patches.
Prior to this change, clang was considering ?1 to be the complete
destructor and the base destructor, which was wrong. This lead to
crashes when clang tried to emit two LLVM functions with the same name.
In this ABI, TUs with non-inline dtors might not emit a complete
destructor. They are emitted as inline thunks in TUs that need them,
and they always delegate to the base dtors of the complete class and its
virtual bases. This change uses the DeferredDecls machinery to emit
complete dtors as needed.
Currently in clang try body destructors can catch exceptions thrown by
virtual base destructors. In the Microsoft C++ ABI, clang may not have
the destructor definition, in which case clang won't wrap the virtual
virtual base destructor calls in a try-catch. Diagnosing this in user
code is TODO.
Finally, for classes that don't use virtual inheritance, MSVC always
calls the base destructor (?1) directly. This is a useful code size
optimization that avoids emitting lots of extra thunks or aliases.
Implementing it also means our existing tests continue to pass, and is
consistent with MSVC's output.
We can do the same for Itanium by tweaking GetAddrOfCXXDestructor, but
it will require further testing.
Reviewers: rjmccall
CC: cfe-commits
Differential Revision: http://llvm-reviews.chandlerc.com/D1066
llvm-svn: 186828
2013-07-22 21:51:44 +08:00
|
|
|
|
2014-03-12 14:41:41 +08:00
|
|
|
const CXXRecordDecl *
|
|
|
|
getThisArgumentTypeForMethod(const CXXMethodDecl *MD) override {
|
2013-08-21 14:25:03 +08:00
|
|
|
MD = MD->getCanonicalDecl();
|
|
|
|
if (MD->isVirtual() && !isa<CXXDestructorDecl>(MD)) {
|
2013-11-05 23:54:58 +08:00
|
|
|
MicrosoftVTableContext::MethodVFTableLocation ML =
|
|
|
|
CGM.getMicrosoftVTableContext().getMethodVFTableLocation(MD);
|
2013-08-21 14:25:03 +08:00
|
|
|
// The vbases might be ordered differently in the final overrider object
|
|
|
|
// and the complete object, so the "this" argument may sometimes point to
|
|
|
|
// memory that has no particular type (e.g. past the complete object).
|
|
|
|
// In this case, we just use a generic pointer type.
|
|
|
|
// FIXME: might want to have a more precise type in the non-virtual
|
|
|
|
// multiple inheritance case.
|
2013-11-07 21:34:02 +08:00
|
|
|
if (ML.VBase || !ML.VFPtrOffset.isZero())
|
2014-05-21 13:09:00 +08:00
|
|
|
return nullptr;
|
2013-08-21 14:25:03 +08:00
|
|
|
}
|
|
|
|
return MD->getParent();
|
|
|
|
}
|
|
|
|
|
2014-03-15 01:43:37 +08:00
|
|
|
llvm::Value *
|
|
|
|
adjustThisArgumentForVirtualFunctionCall(CodeGenFunction &CGF, GlobalDecl GD,
|
|
|
|
llvm::Value *This,
|
|
|
|
bool VirtualCall) override;
|
2013-08-21 14:25:03 +08:00
|
|
|
|
2013-12-18 03:46:40 +08:00
|
|
|
void addImplicitStructorParams(CodeGenFunction &CGF, QualType &ResTy,
|
2014-03-12 14:41:41 +08:00
|
|
|
FunctionArgList &Params) override;
|
2010-08-31 15:33:07 +08:00
|
|
|
|
2013-08-21 14:25:03 +08:00
|
|
|
llvm::Value *adjustThisParameterInVirtualFunctionPrologue(
|
2014-03-12 14:41:41 +08:00
|
|
|
CodeGenFunction &CGF, GlobalDecl GD, llvm::Value *This) override;
|
2013-08-21 14:25:03 +08:00
|
|
|
|
2014-03-12 14:41:41 +08:00
|
|
|
void EmitInstanceFunctionProlog(CodeGenFunction &CGF) override;
|
2011-01-27 10:46:02 +08:00
|
|
|
|
2013-12-18 03:46:40 +08:00
|
|
|
unsigned addImplicitConstructorArgs(CodeGenFunction &CGF,
|
|
|
|
const CXXConstructorDecl *D,
|
|
|
|
CXXCtorType Type, bool ForVirtualBase,
|
2014-03-12 14:41:41 +08:00
|
|
|
bool Delegating,
|
|
|
|
CallArgList &Args) override;
|
2013-08-21 14:25:03 +08:00
|
|
|
|
2013-12-13 08:53:54 +08:00
|
|
|
void EmitDestructorCall(CodeGenFunction &CGF, const CXXDestructorDecl *DD,
|
|
|
|
CXXDtorType Type, bool ForVirtualBase,
|
2014-03-12 14:41:41 +08:00
|
|
|
bool Delegating, llvm::Value *This) override;
|
2013-12-13 08:53:54 +08:00
|
|
|
|
2014-03-12 14:41:41 +08:00
|
|
|
void emitVTableDefinitions(CodeGenVTables &CGVT,
|
|
|
|
const CXXRecordDecl *RD) override;
|
2013-09-27 22:48:01 +08:00
|
|
|
|
|
|
|
llvm::Value *getVTableAddressPointInStructor(
|
|
|
|
CodeGenFunction &CGF, const CXXRecordDecl *VTableClass,
|
|
|
|
BaseSubobject Base, const CXXRecordDecl *NearestVBase,
|
2014-03-12 14:41:41 +08:00
|
|
|
bool &NeedsVirtualOffset) override;
|
2013-09-27 22:48:01 +08:00
|
|
|
|
|
|
|
llvm::Constant *
|
|
|
|
getVTableAddressPointForConstExpr(BaseSubobject Base,
|
2014-03-12 14:41:41 +08:00
|
|
|
const CXXRecordDecl *VTableClass) override;
|
2013-09-27 22:48:01 +08:00
|
|
|
|
|
|
|
llvm::GlobalVariable *getAddrOfVTable(const CXXRecordDecl *RD,
|
2014-03-12 14:41:41 +08:00
|
|
|
CharUnits VPtrOffset) override;
|
2013-09-27 22:48:01 +08:00
|
|
|
|
2013-08-21 14:25:03 +08:00
|
|
|
llvm::Value *getVirtualFunctionPointer(CodeGenFunction &CGF, GlobalDecl GD,
|
2014-03-12 14:41:41 +08:00
|
|
|
llvm::Value *This,
|
|
|
|
llvm::Type *Ty) override;
|
2013-08-21 14:25:03 +08:00
|
|
|
|
2014-11-01 04:09:12 +08:00
|
|
|
llvm::Value *EmitVirtualDestructorCall(CodeGenFunction &CGF,
|
|
|
|
const CXXDestructorDecl *Dtor,
|
|
|
|
CXXDtorType DtorType,
|
|
|
|
llvm::Value *This,
|
|
|
|
const CXXMemberCallExpr *CE) override;
|
2013-02-15 22:45:22 +08:00
|
|
|
|
2013-10-09 17:23:58 +08:00
|
|
|
void adjustCallArgsForDestructorThunk(CodeGenFunction &CGF, GlobalDecl GD,
|
2014-03-12 14:41:41 +08:00
|
|
|
CallArgList &CallArgs) override {
|
2013-10-09 17:23:58 +08:00
|
|
|
assert(GD.getDtorType() == Dtor_Deleting &&
|
|
|
|
"Only deleting destructor thunks are available in this ABI");
|
|
|
|
CallArgs.add(RValue::get(getStructorImplicitParamValue(CGF)),
|
2015-03-15 07:44:48 +08:00
|
|
|
getContext().IntTy);
|
2013-10-09 17:23:58 +08:00
|
|
|
}
|
|
|
|
|
2014-03-12 14:41:41 +08:00
|
|
|
void emitVirtualInheritanceTables(const CXXRecordDecl *RD) override;
|
2013-06-19 23:20:38 +08:00
|
|
|
|
2014-01-03 08:14:35 +08:00
|
|
|
llvm::GlobalVariable *
|
2014-02-28 03:40:09 +08:00
|
|
|
getAddrOfVBTable(const VPtrInfo &VBT, const CXXRecordDecl *RD,
|
2014-01-03 08:14:35 +08:00
|
|
|
llvm::GlobalVariable::LinkageTypes Linkage);
|
|
|
|
|
2014-02-28 03:40:09 +08:00
|
|
|
void emitVBTableDefinition(const VPtrInfo &VBT, const CXXRecordDecl *RD,
|
2014-01-03 08:14:35 +08:00
|
|
|
llvm::GlobalVariable *GV) const;
|
|
|
|
|
2014-06-07 04:04:01 +08:00
|
|
|
void setThunkLinkage(llvm::Function *Thunk, bool ForVTable,
|
|
|
|
GlobalDecl GD, bool ReturnAdjustment) override {
|
2014-05-31 00:59:42 +08:00
|
|
|
// Never dllimport/dllexport thunks.
|
|
|
|
Thunk->setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass);
|
2014-06-07 04:04:01 +08:00
|
|
|
|
|
|
|
GVALinkage Linkage =
|
|
|
|
getContext().GetGVALinkageForFunction(cast<FunctionDecl>(GD.getDecl()));
|
|
|
|
|
|
|
|
if (Linkage == GVA_Internal)
|
|
|
|
Thunk->setLinkage(llvm::GlobalValue::InternalLinkage);
|
|
|
|
else if (ReturnAdjustment)
|
|
|
|
Thunk->setLinkage(llvm::GlobalValue::WeakODRLinkage);
|
|
|
|
else
|
|
|
|
Thunk->setLinkage(llvm::GlobalValue::LinkOnceODRLinkage);
|
2013-10-09 17:23:58 +08:00
|
|
|
}
|
|
|
|
|
2013-10-30 19:55:43 +08:00
|
|
|
llvm::Value *performThisAdjustment(CodeGenFunction &CGF, llvm::Value *This,
|
2014-03-12 14:41:41 +08:00
|
|
|
const ThisAdjustment &TA) override;
|
2013-10-30 19:55:43 +08:00
|
|
|
|
|
|
|
llvm::Value *performReturnAdjustment(CodeGenFunction &CGF, llvm::Value *Ret,
|
2014-03-12 14:41:41 +08:00
|
|
|
const ReturnAdjustment &RA) override;
|
2013-10-30 19:55:43 +08:00
|
|
|
|
2014-10-05 13:05:40 +08:00
|
|
|
void EmitThreadLocalInitFuncs(
|
|
|
|
CodeGenModule &CGM,
|
|
|
|
ArrayRef<std::pair<const VarDecl *, llvm::GlobalVariable *>>
|
|
|
|
CXXThreadLocals,
|
|
|
|
ArrayRef<llvm::Function *> CXXThreadLocalInits,
|
|
|
|
ArrayRef<llvm::GlobalVariable *> CXXThreadLocalInitVars) override;
|
|
|
|
|
|
|
|
bool usesThreadWrapperFunction() const override { return false; }
|
|
|
|
LValue EmitThreadLocalVarDeclLValue(CodeGenFunction &CGF, const VarDecl *VD,
|
|
|
|
QualType LValType) override;
|
|
|
|
|
2012-05-01 14:13:13 +08:00
|
|
|
void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D,
|
|
|
|
llvm::GlobalVariable *DeclPtr,
|
2014-03-12 14:41:41 +08:00
|
|
|
bool PerformInit) override;
|
2014-10-05 13:05:40 +08:00
|
|
|
void registerGlobalDtor(CodeGenFunction &CGF, const VarDecl &D,
|
|
|
|
llvm::Constant *Dtor, llvm::Constant *Addr) override;
|
2012-05-01 14:13:13 +08:00
|
|
|
|
2011-01-27 10:46:02 +08:00
|
|
|
// ==== Notes on array cookies =========
|
|
|
|
//
|
|
|
|
// MSVC seems to only use cookies when the class has a destructor; a
|
|
|
|
// two-argument usual array deallocation function isn't sufficient.
|
|
|
|
//
|
|
|
|
// For example, this code prints "100" and "1":
|
|
|
|
// struct A {
|
|
|
|
// char x;
|
|
|
|
// void *operator new[](size_t sz) {
|
|
|
|
// printf("%u\n", sz);
|
|
|
|
// return malloc(sz);
|
|
|
|
// }
|
|
|
|
// void operator delete[](void *p, size_t sz) {
|
|
|
|
// printf("%u\n", sz);
|
|
|
|
// free(p);
|
|
|
|
// }
|
|
|
|
// };
|
|
|
|
// int main() {
|
|
|
|
// A *p = new A[100];
|
|
|
|
// delete[] p;
|
|
|
|
// }
|
|
|
|
// Whereas it prints "104" and "104" if you give A a destructor.
|
2012-05-01 13:23:51 +08:00
|
|
|
|
2014-03-12 14:41:41 +08:00
|
|
|
bool requiresArrayCookie(const CXXDeleteExpr *expr,
|
|
|
|
QualType elementType) override;
|
|
|
|
bool requiresArrayCookie(const CXXNewExpr *expr) override;
|
|
|
|
CharUnits getArrayCookieSizeImpl(QualType type) override;
|
2012-05-01 13:23:51 +08:00
|
|
|
llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF,
|
|
|
|
llvm::Value *NewPtr,
|
|
|
|
llvm::Value *NumElements,
|
|
|
|
const CXXNewExpr *expr,
|
2014-03-12 14:41:41 +08:00
|
|
|
QualType ElementType) override;
|
2012-05-01 13:23:51 +08:00
|
|
|
llvm::Value *readArrayCookieImpl(CodeGenFunction &CGF,
|
|
|
|
llvm::Value *allocPtr,
|
2014-03-12 14:41:41 +08:00
|
|
|
CharUnits cookieSize) override;
|
2013-03-23 03:02:54 +08:00
|
|
|
|
2014-07-07 16:09:15 +08:00
|
|
|
friend struct MSRTTIBuilder;
|
|
|
|
|
|
|
|
bool isImageRelative() const {
|
|
|
|
return CGM.getTarget().getPointerWidth(/*AddressSpace=*/0) == 64;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 5 routines for constructing the llvm types for MS RTTI structs.
|
|
|
|
llvm::StructType *getTypeDescriptorType(StringRef TypeInfoString) {
|
|
|
|
llvm::SmallString<32> TDTypeName("rtti.TypeDescriptor");
|
|
|
|
TDTypeName += llvm::utostr(TypeInfoString.size());
|
|
|
|
llvm::StructType *&TypeDescriptorType =
|
|
|
|
TypeDescriptorTypeMap[TypeInfoString.size()];
|
|
|
|
if (TypeDescriptorType)
|
|
|
|
return TypeDescriptorType;
|
|
|
|
llvm::Type *FieldTypes[] = {
|
|
|
|
CGM.Int8PtrPtrTy,
|
|
|
|
CGM.Int8PtrTy,
|
|
|
|
llvm::ArrayType::get(CGM.Int8Ty, TypeInfoString.size() + 1)};
|
|
|
|
TypeDescriptorType =
|
|
|
|
llvm::StructType::create(CGM.getLLVMContext(), FieldTypes, TDTypeName);
|
|
|
|
return TypeDescriptorType;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Type *getImageRelativeType(llvm::Type *PtrType) {
|
|
|
|
if (!isImageRelative())
|
|
|
|
return PtrType;
|
|
|
|
return CGM.IntTy;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::StructType *getBaseClassDescriptorType() {
|
|
|
|
if (BaseClassDescriptorType)
|
|
|
|
return BaseClassDescriptorType;
|
|
|
|
llvm::Type *FieldTypes[] = {
|
|
|
|
getImageRelativeType(CGM.Int8PtrTy),
|
|
|
|
CGM.IntTy,
|
|
|
|
CGM.IntTy,
|
|
|
|
CGM.IntTy,
|
|
|
|
CGM.IntTy,
|
|
|
|
CGM.IntTy,
|
|
|
|
getImageRelativeType(getClassHierarchyDescriptorType()->getPointerTo()),
|
|
|
|
};
|
|
|
|
BaseClassDescriptorType = llvm::StructType::create(
|
|
|
|
CGM.getLLVMContext(), FieldTypes, "rtti.BaseClassDescriptor");
|
|
|
|
return BaseClassDescriptorType;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::StructType *getClassHierarchyDescriptorType() {
|
|
|
|
if (ClassHierarchyDescriptorType)
|
|
|
|
return ClassHierarchyDescriptorType;
|
|
|
|
// Forward-declare RTTIClassHierarchyDescriptor to break a cycle.
|
|
|
|
ClassHierarchyDescriptorType = llvm::StructType::create(
|
|
|
|
CGM.getLLVMContext(), "rtti.ClassHierarchyDescriptor");
|
|
|
|
llvm::Type *FieldTypes[] = {
|
|
|
|
CGM.IntTy,
|
|
|
|
CGM.IntTy,
|
|
|
|
CGM.IntTy,
|
|
|
|
getImageRelativeType(
|
|
|
|
getBaseClassDescriptorType()->getPointerTo()->getPointerTo()),
|
|
|
|
};
|
|
|
|
ClassHierarchyDescriptorType->setBody(FieldTypes);
|
|
|
|
return ClassHierarchyDescriptorType;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::StructType *getCompleteObjectLocatorType() {
|
|
|
|
if (CompleteObjectLocatorType)
|
|
|
|
return CompleteObjectLocatorType;
|
|
|
|
CompleteObjectLocatorType = llvm::StructType::create(
|
|
|
|
CGM.getLLVMContext(), "rtti.CompleteObjectLocator");
|
|
|
|
llvm::Type *FieldTypes[] = {
|
|
|
|
CGM.IntTy,
|
|
|
|
CGM.IntTy,
|
|
|
|
CGM.IntTy,
|
|
|
|
getImageRelativeType(CGM.Int8PtrTy),
|
|
|
|
getImageRelativeType(getClassHierarchyDescriptorType()->getPointerTo()),
|
|
|
|
getImageRelativeType(CompleteObjectLocatorType),
|
|
|
|
};
|
|
|
|
llvm::ArrayRef<llvm::Type *> FieldTypesRef(FieldTypes);
|
|
|
|
if (!isImageRelative())
|
|
|
|
FieldTypesRef = FieldTypesRef.drop_back();
|
|
|
|
CompleteObjectLocatorType->setBody(FieldTypesRef);
|
|
|
|
return CompleteObjectLocatorType;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::GlobalVariable *getImageBase() {
|
|
|
|
StringRef Name = "__ImageBase";
|
|
|
|
if (llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(Name))
|
|
|
|
return GV;
|
|
|
|
|
|
|
|
return new llvm::GlobalVariable(CGM.getModule(), CGM.Int8Ty,
|
|
|
|
/*isConstant=*/true,
|
|
|
|
llvm::GlobalValue::ExternalLinkage,
|
|
|
|
/*Initializer=*/nullptr, Name);
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Constant *getImageRelativeConstant(llvm::Constant *PtrVal) {
|
|
|
|
if (!isImageRelative())
|
|
|
|
return PtrVal;
|
|
|
|
|
2015-03-05 08:46:22 +08:00
|
|
|
if (PtrVal->isNullValue())
|
|
|
|
return llvm::Constant::getNullValue(CGM.IntTy);
|
|
|
|
|
2014-07-07 16:09:15 +08:00
|
|
|
llvm::Constant *ImageBaseAsInt =
|
|
|
|
llvm::ConstantExpr::getPtrToInt(getImageBase(), CGM.IntPtrTy);
|
|
|
|
llvm::Constant *PtrValAsInt =
|
|
|
|
llvm::ConstantExpr::getPtrToInt(PtrVal, CGM.IntPtrTy);
|
|
|
|
llvm::Constant *Diff =
|
|
|
|
llvm::ConstantExpr::getSub(PtrValAsInt, ImageBaseAsInt,
|
|
|
|
/*HasNUW=*/true, /*HasNSW=*/true);
|
|
|
|
return llvm::ConstantExpr::getTrunc(Diff, CGM.IntTy);
|
|
|
|
}
|
|
|
|
|
2013-03-23 03:02:54 +08:00
|
|
|
private:
|
2013-10-03 14:26:13 +08:00
|
|
|
MicrosoftMangleContext &getMangleContext() {
|
|
|
|
return cast<MicrosoftMangleContext>(CodeGen::CGCXXABI::getMangleContext());
|
|
|
|
}
|
|
|
|
|
2013-04-12 02:13:19 +08:00
|
|
|
llvm::Constant *getZeroInt() {
|
|
|
|
return llvm::ConstantInt::get(CGM.IntTy, 0);
|
2013-03-23 03:02:54 +08:00
|
|
|
}
|
|
|
|
|
2013-04-12 02:13:19 +08:00
|
|
|
llvm::Constant *getAllOnesInt() {
|
|
|
|
return llvm::Constant::getAllOnesValue(CGM.IntTy);
|
2013-03-23 03:02:54 +08:00
|
|
|
}
|
|
|
|
|
2013-05-10 05:01:17 +08:00
|
|
|
llvm::Constant *getConstantOrZeroInt(llvm::Constant *C) {
|
|
|
|
return C ? C : getZeroInt();
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Value *getValueOrZeroInt(llvm::Value *C) {
|
|
|
|
return C ? C : getZeroInt();
|
|
|
|
}
|
|
|
|
|
2014-03-15 01:43:37 +08:00
|
|
|
CharUnits getVirtualFunctionPrologueThisAdjustment(GlobalDecl GD);
|
|
|
|
|
2013-04-12 02:13:19 +08:00
|
|
|
void
|
|
|
|
GetNullMemberPointerFields(const MemberPointerType *MPT,
|
|
|
|
llvm::SmallVectorImpl<llvm::Constant *> &fields);
|
|
|
|
|
2013-05-30 02:02:47 +08:00
|
|
|
/// \brief Shared code for virtual base adjustment. Returns the offset from
|
|
|
|
/// the vbptr to the virtual base. Optionally returns the address of the
|
|
|
|
/// vbptr itself.
|
|
|
|
llvm::Value *GetVBaseOffsetFromVBPtr(CodeGenFunction &CGF,
|
|
|
|
llvm::Value *Base,
|
|
|
|
llvm::Value *VBPtrOffset,
|
|
|
|
llvm::Value *VBTableOffset,
|
2014-05-21 13:09:00 +08:00
|
|
|
llvm::Value **VBPtr = nullptr);
|
2013-05-30 02:02:47 +08:00
|
|
|
|
2013-10-30 19:55:43 +08:00
|
|
|
llvm::Value *GetVBaseOffsetFromVBPtr(CodeGenFunction &CGF,
|
|
|
|
llvm::Value *Base,
|
|
|
|
int32_t VBPtrOffset,
|
|
|
|
int32_t VBTableOffset,
|
2014-05-21 13:09:00 +08:00
|
|
|
llvm::Value **VBPtr = nullptr) {
|
2014-10-23 01:26:00 +08:00
|
|
|
assert(VBTableOffset % 4 == 0 && "should be byte offset into table of i32s");
|
2013-10-30 19:55:43 +08:00
|
|
|
llvm::Value *VBPOffset = llvm::ConstantInt::get(CGM.IntTy, VBPtrOffset),
|
|
|
|
*VBTOffset = llvm::ConstantInt::get(CGM.IntTy, VBTableOffset);
|
|
|
|
return GetVBaseOffsetFromVBPtr(CGF, Base, VBPOffset, VBTOffset, VBPtr);
|
|
|
|
}
|
|
|
|
|
2015-02-27 10:38:02 +08:00
|
|
|
std::pair<llvm::Value *, llvm::Value *>
|
|
|
|
performBaseAdjustment(CodeGenFunction &CGF, llvm::Value *Value,
|
|
|
|
QualType SrcRecordTy);
|
|
|
|
|
2013-05-30 02:02:47 +08:00
|
|
|
/// \brief Performs a full virtual base adjustment. Used to dereference
|
|
|
|
/// pointers to members of virtual bases.
|
2014-02-21 07:22:07 +08:00
|
|
|
llvm::Value *AdjustVirtualBase(CodeGenFunction &CGF, const Expr *E,
|
|
|
|
const CXXRecordDecl *RD, llvm::Value *Base,
|
2013-04-12 02:13:19 +08:00
|
|
|
llvm::Value *VirtualBaseAdjustmentOffset,
|
|
|
|
llvm::Value *VBPtrOffset /* optional */);
|
|
|
|
|
2013-05-03 09:15:11 +08:00
|
|
|
/// \brief Emits a full member pointer with the fields common to data and
|
|
|
|
/// function member pointers.
|
|
|
|
llvm::Constant *EmitFullMemberPointer(llvm::Constant *FirstField,
|
|
|
|
bool IsMemberFunction,
|
2013-05-10 05:01:17 +08:00
|
|
|
const CXXRecordDecl *RD,
|
|
|
|
CharUnits NonVirtualBaseAdjustment);
|
|
|
|
|
|
|
|
llvm::Constant *BuildMemberPointer(const CXXRecordDecl *RD,
|
|
|
|
const CXXMethodDecl *MD,
|
|
|
|
CharUnits NonVirtualBaseAdjustment);
|
|
|
|
|
|
|
|
bool MemberPointerConstantIsNull(const MemberPointerType *MPT,
|
|
|
|
llvm::Constant *MP);
|
2013-05-03 09:15:11 +08:00
|
|
|
|
2013-06-19 23:20:38 +08:00
|
|
|
/// \brief - Initialize all vbptrs of 'this' with RD as the complete type.
|
|
|
|
void EmitVBPtrStores(CodeGenFunction &CGF, const CXXRecordDecl *RD);
|
|
|
|
|
|
|
|
/// \brief Caching wrapper around VBTableBuilder::enumerateVBTables().
|
2014-01-03 08:14:35 +08:00
|
|
|
const VBTableGlobals &enumerateVBTables(const CXXRecordDecl *RD);
|
2013-06-19 23:20:38 +08:00
|
|
|
|
2013-11-16 01:24:45 +08:00
|
|
|
/// \brief Generate a thunk for calling a virtual member function MD.
|
2014-02-21 10:27:32 +08:00
|
|
|
llvm::Function *EmitVirtualMemPtrThunk(
|
|
|
|
const CXXMethodDecl *MD,
|
|
|
|
const MicrosoftVTableContext::MethodVFTableLocation &ML);
|
2013-11-16 01:24:45 +08:00
|
|
|
|
2013-03-23 03:02:54 +08:00
|
|
|
public:
|
2014-03-12 14:41:41 +08:00
|
|
|
llvm::Type *ConvertMemberPointerType(const MemberPointerType *MPT) override;
|
2013-04-12 02:13:19 +08:00
|
|
|
|
2014-03-12 14:41:41 +08:00
|
|
|
bool isZeroInitializable(const MemberPointerType *MPT) override;
|
2013-04-12 02:13:19 +08:00
|
|
|
|
2014-08-08 06:56:13 +08:00
|
|
|
bool isMemberPointerConvertible(const MemberPointerType *MPT) const override {
|
|
|
|
const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl();
|
2014-09-19 06:05:54 +08:00
|
|
|
return RD->hasAttr<MSInheritanceAttr>();
|
|
|
|
}
|
|
|
|
|
2014-11-15 03:09:44 +08:00
|
|
|
bool isTypeInfoCalculable(QualType Ty) const override {
|
2014-09-19 06:05:54 +08:00
|
|
|
if (!CGCXXABI::isTypeInfoCalculable(Ty))
|
|
|
|
return false;
|
|
|
|
if (const auto *MPT = Ty->getAs<MemberPointerType>()) {
|
|
|
|
const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl();
|
|
|
|
if (!RD->hasAttr<MSInheritanceAttr>())
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2014-08-08 06:56:13 +08:00
|
|
|
}
|
|
|
|
|
2014-03-12 14:41:41 +08:00
|
|
|
llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT) override;
|
2013-03-23 03:02:54 +08:00
|
|
|
|
2014-03-12 14:41:41 +08:00
|
|
|
llvm::Constant *EmitMemberDataPointer(const MemberPointerType *MPT,
|
|
|
|
CharUnits offset) override;
|
|
|
|
llvm::Constant *EmitMemberPointer(const CXXMethodDecl *MD) override;
|
|
|
|
llvm::Constant *EmitMemberPointer(const APValue &MP, QualType MPT) override;
|
2013-03-23 03:02:54 +08:00
|
|
|
|
2014-03-12 14:41:41 +08:00
|
|
|
llvm::Value *EmitMemberPointerComparison(CodeGenFunction &CGF,
|
|
|
|
llvm::Value *L,
|
|
|
|
llvm::Value *R,
|
|
|
|
const MemberPointerType *MPT,
|
|
|
|
bool Inequality) override;
|
2013-05-01 04:15:14 +08:00
|
|
|
|
2014-03-12 14:41:41 +08:00
|
|
|
llvm::Value *EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
|
|
|
|
llvm::Value *MemPtr,
|
|
|
|
const MemberPointerType *MPT) override;
|
2013-03-23 03:02:54 +08:00
|
|
|
|
2014-03-12 14:41:41 +08:00
|
|
|
llvm::Value *
|
2014-02-21 07:22:07 +08:00
|
|
|
EmitMemberDataPointerAddress(CodeGenFunction &CGF, const Expr *E,
|
|
|
|
llvm::Value *Base, llvm::Value *MemPtr,
|
2014-03-12 14:41:41 +08:00
|
|
|
const MemberPointerType *MPT) override;
|
2013-03-23 03:02:54 +08:00
|
|
|
|
2014-03-12 14:41:41 +08:00
|
|
|
llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF,
|
|
|
|
const CastExpr *E,
|
|
|
|
llvm::Value *Src) override;
|
2013-05-10 05:01:17 +08:00
|
|
|
|
2014-03-12 14:41:41 +08:00
|
|
|
llvm::Constant *EmitMemberPointerConversion(const CastExpr *E,
|
|
|
|
llvm::Constant *Src) override;
|
2013-05-10 05:01:17 +08:00
|
|
|
|
2014-03-12 14:41:41 +08:00
|
|
|
llvm::Value *
|
2014-02-21 07:22:07 +08:00
|
|
|
EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF, const Expr *E,
|
|
|
|
llvm::Value *&This, llvm::Value *MemPtr,
|
2014-03-12 14:41:41 +08:00
|
|
|
const MemberPointerType *MPT) override;
|
2013-04-12 02:13:19 +08:00
|
|
|
|
2014-09-16 03:20:10 +08:00
|
|
|
void emitCXXStructor(const CXXMethodDecl *MD, StructorType Type) override;
|
|
|
|
|
2015-03-30 05:55:10 +08:00
|
|
|
llvm::StructType *getCatchHandlerTypeType() {
|
|
|
|
if (!CatchHandlerTypeType) {
|
2015-03-18 04:35:05 +08:00
|
|
|
llvm::Type *FieldTypes[] = {
|
2015-03-30 05:55:10 +08:00
|
|
|
CGM.IntTy, // Flags
|
|
|
|
CGM.Int8PtrTy, // TypeDescriptor
|
2015-03-18 04:35:05 +08:00
|
|
|
};
|
2015-03-30 05:55:10 +08:00
|
|
|
CatchHandlerTypeType = llvm::StructType::create(
|
|
|
|
CGM.getLLVMContext(), FieldTypes, "eh.CatchHandlerType");
|
2015-03-18 04:35:05 +08:00
|
|
|
}
|
2015-03-30 05:55:10 +08:00
|
|
|
return CatchHandlerTypeType;
|
2015-03-18 04:35:05 +08:00
|
|
|
}
|
|
|
|
|
2015-03-05 08:46:22 +08:00
|
|
|
llvm::StructType *getCatchableTypeType() {
|
|
|
|
if (CatchableTypeType)
|
|
|
|
return CatchableTypeType;
|
|
|
|
llvm::Type *FieldTypes[] = {
|
|
|
|
CGM.IntTy, // Flags
|
|
|
|
getImageRelativeType(CGM.Int8PtrTy), // TypeDescriptor
|
|
|
|
CGM.IntTy, // NonVirtualAdjustment
|
|
|
|
CGM.IntTy, // OffsetToVBPtr
|
|
|
|
CGM.IntTy, // VBTableIndex
|
|
|
|
CGM.IntTy, // Size
|
|
|
|
getImageRelativeType(CGM.Int8PtrTy) // CopyCtor
|
|
|
|
};
|
|
|
|
CatchableTypeType = llvm::StructType::create(
|
|
|
|
CGM.getLLVMContext(), FieldTypes, "eh.CatchableType");
|
|
|
|
return CatchableTypeType;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::StructType *getCatchableTypeArrayType(uint32_t NumEntries) {
|
|
|
|
llvm::StructType *&CatchableTypeArrayType =
|
|
|
|
CatchableTypeArrayTypeMap[NumEntries];
|
|
|
|
if (CatchableTypeArrayType)
|
|
|
|
return CatchableTypeArrayType;
|
|
|
|
|
|
|
|
llvm::SmallString<23> CTATypeName("eh.CatchableTypeArray.");
|
|
|
|
CTATypeName += llvm::utostr(NumEntries);
|
|
|
|
llvm::Type *CTType =
|
|
|
|
getImageRelativeType(getCatchableTypeType()->getPointerTo());
|
|
|
|
llvm::Type *FieldTypes[] = {
|
|
|
|
CGM.IntTy, // NumEntries
|
|
|
|
llvm::ArrayType::get(CTType, NumEntries) // CatchableTypes
|
|
|
|
};
|
|
|
|
CatchableTypeArrayType =
|
|
|
|
llvm::StructType::create(CGM.getLLVMContext(), FieldTypes, CTATypeName);
|
|
|
|
return CatchableTypeArrayType;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::StructType *getThrowInfoType() {
|
|
|
|
if (ThrowInfoType)
|
|
|
|
return ThrowInfoType;
|
|
|
|
llvm::Type *FieldTypes[] = {
|
|
|
|
CGM.IntTy, // Flags
|
|
|
|
getImageRelativeType(CGM.Int8PtrTy), // CleanupFn
|
|
|
|
getImageRelativeType(CGM.Int8PtrTy), // ForwardCompat
|
|
|
|
getImageRelativeType(CGM.Int8PtrTy) // CatchableTypeArray
|
|
|
|
};
|
|
|
|
ThrowInfoType = llvm::StructType::create(CGM.getLLVMContext(), FieldTypes,
|
|
|
|
"eh.ThrowInfo");
|
|
|
|
return ThrowInfoType;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Constant *getThrowFn() {
|
|
|
|
// _CxxThrowException is passed an exception object and a ThrowInfo object
|
|
|
|
// which describes the exception.
|
|
|
|
llvm::Type *Args[] = {CGM.Int8PtrTy, getThrowInfoType()->getPointerTo()};
|
|
|
|
llvm::FunctionType *FTy =
|
|
|
|
llvm::FunctionType::get(CGM.VoidTy, Args, /*IsVarArgs=*/false);
|
|
|
|
auto *Fn = cast<llvm::Function>(
|
|
|
|
CGM.CreateRuntimeFunction(FTy, "_CxxThrowException"));
|
|
|
|
// _CxxThrowException is stdcall on 32-bit x86 platforms.
|
|
|
|
if (CGM.getTarget().getTriple().getArch() == llvm::Triple::x86)
|
|
|
|
Fn->setCallingConv(llvm::CallingConv::X86_StdCall);
|
|
|
|
return Fn;
|
|
|
|
}
|
|
|
|
|
2015-03-14 06:36:55 +08:00
|
|
|
llvm::Function *getAddrOfCXXCtorClosure(const CXXConstructorDecl *CD,
|
|
|
|
CXXCtorType CT);
|
2015-03-12 02:36:39 +08:00
|
|
|
|
2015-03-05 08:46:22 +08:00
|
|
|
llvm::Constant *getCatchableType(QualType T,
|
|
|
|
uint32_t NVOffset = 0,
|
|
|
|
int32_t VBPtrOffset = -1,
|
|
|
|
uint32_t VBIndex = 0);
|
|
|
|
|
|
|
|
llvm::GlobalVariable *getCatchableTypeArray(QualType T);
|
|
|
|
|
2015-03-14 02:26:17 +08:00
|
|
|
llvm::GlobalVariable *getThrowInfo(QualType T) override;
|
2015-03-05 08:46:22 +08:00
|
|
|
|
2013-06-19 23:20:38 +08:00
|
|
|
private:
|
2013-09-27 22:48:01 +08:00
|
|
|
typedef std::pair<const CXXRecordDecl *, CharUnits> VFTableIdTy;
|
2014-07-02 04:30:31 +08:00
|
|
|
typedef llvm::DenseMap<VFTableIdTy, llvm::GlobalVariable *> VTablesMapTy;
|
|
|
|
typedef llvm::DenseMap<VFTableIdTy, llvm::GlobalValue *> VFTablesMapTy;
|
2013-09-27 22:48:01 +08:00
|
|
|
/// \brief All the vftables that have been referenced.
|
|
|
|
VFTablesMapTy VFTablesMap;
|
2014-07-02 04:30:31 +08:00
|
|
|
VTablesMapTy VTablesMap;
|
2013-09-27 22:48:01 +08:00
|
|
|
|
|
|
|
/// \brief This set holds the record decls we've deferred vtable emission for.
|
|
|
|
llvm::SmallPtrSet<const CXXRecordDecl *, 4> DeferredVFTables;
|
|
|
|
|
|
|
|
|
|
|
|
/// \brief All the vbtables which have been referenced.
|
2014-01-03 08:14:35 +08:00
|
|
|
llvm::DenseMap<const CXXRecordDecl *, VBTableGlobals> VBTablesMap;
|
2013-09-11 04:14:30 +08:00
|
|
|
|
|
|
|
/// Info on the global variable used to guard initialization of static locals.
|
|
|
|
/// The BitIndex field is only used for externally invisible declarations.
|
|
|
|
struct GuardInfo {
|
2014-05-21 13:09:00 +08:00
|
|
|
GuardInfo() : Guard(nullptr), BitIndex(0) {}
|
2013-09-11 04:14:30 +08:00
|
|
|
llvm::GlobalVariable *Guard;
|
|
|
|
unsigned BitIndex;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Map from DeclContext to the current guard variable. We assume that the
|
|
|
|
/// AST is visited in source code order.
|
|
|
|
llvm::DenseMap<const DeclContext *, GuardInfo> GuardVariableMap;
|
2014-07-07 16:09:15 +08:00
|
|
|
|
|
|
|
llvm::DenseMap<size_t, llvm::StructType *> TypeDescriptorTypeMap;
|
|
|
|
llvm::StructType *BaseClassDescriptorType;
|
|
|
|
llvm::StructType *ClassHierarchyDescriptorType;
|
|
|
|
llvm::StructType *CompleteObjectLocatorType;
|
2015-03-05 08:46:22 +08:00
|
|
|
|
|
|
|
llvm::DenseMap<QualType, llvm::GlobalVariable *> CatchableTypeArrays;
|
|
|
|
|
|
|
|
llvm::StructType *CatchableTypeType;
|
|
|
|
llvm::DenseMap<uint32_t, llvm::StructType *> CatchableTypeArrayTypeMap;
|
|
|
|
llvm::StructType *ThrowInfoType;
|
2015-03-30 05:55:10 +08:00
|
|
|
llvm::StructType *CatchHandlerTypeType;
|
2010-06-10 07:25:41 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-05-03 08:33:28 +08:00
|
|
|
CGCXXABI::RecordArgABI
|
|
|
|
MicrosoftCXXABI::getRecordArgABI(const CXXRecordDecl *RD) const {
|
|
|
|
switch (CGM.getTarget().getTriple().getArch()) {
|
|
|
|
default:
|
|
|
|
// FIXME: Implement for other architectures.
|
|
|
|
return RAA_Default;
|
|
|
|
|
|
|
|
case llvm::Triple::x86:
|
2014-05-15 00:02:09 +08:00
|
|
|
// All record arguments are passed in memory on x86. Decide whether to
|
|
|
|
// construct the object directly in argument memory, or to construct the
|
|
|
|
// argument elsewhere and copy the bytes during the call.
|
|
|
|
|
|
|
|
// If C++ prohibits us from making a copy, construct the arguments directly
|
|
|
|
// into argument memory.
|
|
|
|
if (!canCopyArgument(RD))
|
2014-05-03 08:33:28 +08:00
|
|
|
return RAA_DirectInMemory;
|
2014-05-15 00:02:09 +08:00
|
|
|
|
|
|
|
// Otherwise, construct the argument into a temporary and copy the bytes
|
|
|
|
// into the outgoing argument memory.
|
2014-05-03 08:33:28 +08:00
|
|
|
return RAA_Default;
|
|
|
|
|
|
|
|
case llvm::Triple::x86_64:
|
|
|
|
// Win64 passes objects with non-trivial copy ctors indirectly.
|
|
|
|
if (RD->hasNonTrivialCopyConstructor())
|
|
|
|
return RAA_Indirect;
|
2014-05-15 00:02:09 +08:00
|
|
|
|
2014-11-01 06:00:51 +08:00
|
|
|
// If an object has a destructor, we'd really like to pass it indirectly
|
|
|
|
// because it allows us to elide copies. Unfortunately, MSVC makes that
|
|
|
|
// impossible for small types, which it will pass in a single register or
|
|
|
|
// stack slot. Most objects with dtors are large-ish, so handle that early.
|
|
|
|
// We can't call out all large objects as being indirect because there are
|
|
|
|
// multiple x64 calling conventions and the C++ ABI code shouldn't dictate
|
|
|
|
// how we pass large POD types.
|
|
|
|
if (RD->hasNonTrivialDestructor() &&
|
|
|
|
getContext().getTypeSize(RD->getTypeForDecl()) > 64)
|
2014-05-03 08:33:28 +08:00
|
|
|
return RAA_Indirect;
|
2014-05-15 00:02:09 +08:00
|
|
|
|
|
|
|
// We have a trivial copy constructor or no copy constructors, but we have
|
|
|
|
// to make sure it isn't deleted.
|
|
|
|
bool CopyDeleted = false;
|
|
|
|
for (const CXXConstructorDecl *CD : RD->ctors()) {
|
|
|
|
if (CD->isCopyConstructor()) {
|
|
|
|
assert(CD->isTrivial());
|
|
|
|
// We had at least one undeleted trivial copy ctor. Return directly.
|
|
|
|
if (!CD->isDeleted())
|
|
|
|
return RAA_Default;
|
|
|
|
CopyDeleted = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The trivial copy constructor was deleted. Return indirectly.
|
|
|
|
if (CopyDeleted)
|
|
|
|
return RAA_Indirect;
|
|
|
|
|
|
|
|
// There were no copy ctors. Return in RAX.
|
2014-05-03 08:33:28 +08:00
|
|
|
return RAA_Default;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm_unreachable("invalid enum");
|
|
|
|
}
|
|
|
|
|
2014-11-01 15:37:17 +08:00
|
|
|
void MicrosoftCXXABI::emitVirtualObjectDelete(CodeGenFunction &CGF,
|
|
|
|
const CXXDeleteExpr *DE,
|
|
|
|
llvm::Value *Ptr,
|
|
|
|
QualType ElementType,
|
|
|
|
const CXXDestructorDecl *Dtor) {
|
2014-11-01 04:09:12 +08:00
|
|
|
// FIXME: Provide a source location here even though there's no
|
|
|
|
// CXXMemberCallExpr for dtor call.
|
2014-11-01 15:37:17 +08:00
|
|
|
bool UseGlobalDelete = DE->isGlobalDelete();
|
2014-11-01 04:09:12 +08:00
|
|
|
CXXDtorType DtorType = UseGlobalDelete ? Dtor_Complete : Dtor_Deleting;
|
|
|
|
llvm::Value *MDThis =
|
|
|
|
EmitVirtualDestructorCall(CGF, Dtor, DtorType, Ptr, /*CE=*/nullptr);
|
|
|
|
if (UseGlobalDelete)
|
2014-11-01 15:37:17 +08:00
|
|
|
CGF.EmitDeleteCall(DE->getOperatorDelete(), MDThis, ElementType);
|
2012-09-25 18:10:39 +08:00
|
|
|
}
|
|
|
|
|
2014-11-25 15:20:20 +08:00
|
|
|
void MicrosoftCXXABI::emitRethrow(CodeGenFunction &CGF, bool isNoReturn) {
|
2015-03-05 08:46:22 +08:00
|
|
|
llvm::Value *Args[] = {
|
|
|
|
llvm::ConstantPointerNull::get(CGM.Int8PtrTy),
|
|
|
|
llvm::ConstantPointerNull::get(getThrowInfoType()->getPointerTo())};
|
|
|
|
auto *Fn = getThrowFn();
|
2014-11-25 15:20:20 +08:00
|
|
|
if (isNoReturn)
|
|
|
|
CGF.EmitNoreturnRuntimeCallOrInvoke(Fn, Args);
|
|
|
|
else
|
|
|
|
CGF.EmitRuntimeCallOrInvoke(Fn, Args);
|
|
|
|
}
|
|
|
|
|
2015-03-04 03:21:04 +08:00
|
|
|
namespace {
|
|
|
|
struct CallEndCatchMSVC : EHScopeStack::Cleanup {
|
|
|
|
CallEndCatchMSVC() {}
|
|
|
|
void Emit(CodeGenFunction &CGF, Flags flags) override {
|
|
|
|
CGF.EmitNounwindRuntimeCall(
|
|
|
|
CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_endcatch));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void MicrosoftCXXABI::emitBeginCatch(CodeGenFunction &CGF,
|
|
|
|
const CXXCatchStmt *S) {
|
|
|
|
// In the MS ABI, the runtime handles the copy, and the catch handler is
|
|
|
|
// responsible for destruction.
|
|
|
|
VarDecl *CatchParam = S->getExceptionDecl();
|
|
|
|
llvm::Value *Exn = CGF.getExceptionFromSlot();
|
|
|
|
llvm::Function *BeginCatch =
|
|
|
|
CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_begincatch);
|
|
|
|
|
|
|
|
if (!CatchParam) {
|
|
|
|
llvm::Value *Args[2] = {Exn, llvm::Constant::getNullValue(CGF.Int8PtrTy)};
|
|
|
|
CGF.EmitNounwindRuntimeCall(BeginCatch, Args);
|
|
|
|
CGF.EHStack.pushCleanup<CallEndCatchMSVC>(NormalAndEHCleanup);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CodeGenFunction::AutoVarEmission var = CGF.EmitAutoVarAlloca(*CatchParam);
|
|
|
|
llvm::Value *ParamAddr =
|
|
|
|
CGF.Builder.CreateBitCast(var.getObjectAddress(CGF), CGF.Int8PtrTy);
|
|
|
|
llvm::Value *Args[2] = {Exn, ParamAddr};
|
|
|
|
CGF.EmitNounwindRuntimeCall(BeginCatch, Args);
|
|
|
|
// FIXME: Do we really need exceptional endcatch cleanups?
|
|
|
|
CGF.EHStack.pushCleanup<CallEndCatchMSVC>(NormalAndEHCleanup);
|
|
|
|
CGF.EmitAutoVarCleanups(var);
|
|
|
|
}
|
|
|
|
|
2015-02-27 10:38:02 +08:00
|
|
|
std::pair<llvm::Value *, llvm::Value *>
|
|
|
|
MicrosoftCXXABI::performBaseAdjustment(CodeGenFunction &CGF, llvm::Value *Value,
|
|
|
|
QualType SrcRecordTy) {
|
2014-06-23 03:05:33 +08:00
|
|
|
Value = CGF.Builder.CreateBitCast(Value, CGF.Int8PtrTy);
|
|
|
|
const CXXRecordDecl *SrcDecl = SrcRecordTy->getAsCXXRecordDecl();
|
2015-03-15 07:44:48 +08:00
|
|
|
const ASTContext &Context = getContext();
|
2014-06-23 03:05:33 +08:00
|
|
|
|
2015-02-27 10:38:02 +08:00
|
|
|
if (Context.getASTRecordLayout(SrcDecl).hasExtendableVFPtr())
|
2014-06-23 03:05:33 +08:00
|
|
|
return std::make_pair(Value, llvm::ConstantInt::get(CGF.Int32Ty, 0));
|
|
|
|
|
|
|
|
// Perform a base adjustment.
|
2015-02-27 10:38:02 +08:00
|
|
|
const CXXBaseSpecifier *PolymorphicBase = std::find_if(
|
|
|
|
SrcDecl->vbases_begin(), SrcDecl->vbases_end(),
|
|
|
|
[&](const CXXBaseSpecifier &Base) {
|
|
|
|
const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl();
|
|
|
|
return Context.getASTRecordLayout(BaseDecl).hasExtendableVFPtr();
|
|
|
|
});
|
|
|
|
llvm::Value *Offset = GetVirtualBaseClassOffset(
|
|
|
|
CGF, Value, SrcDecl, PolymorphicBase->getType()->getAsCXXRecordDecl());
|
2014-06-23 03:05:33 +08:00
|
|
|
Value = CGF.Builder.CreateInBoundsGEP(Value, Offset);
|
|
|
|
Offset = CGF.Builder.CreateTrunc(Offset, CGF.Int32Ty);
|
|
|
|
return std::make_pair(Value, Offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MicrosoftCXXABI::shouldTypeidBeNullChecked(bool IsDeref,
|
|
|
|
QualType SrcRecordTy) {
|
|
|
|
const CXXRecordDecl *SrcDecl = SrcRecordTy->getAsCXXRecordDecl();
|
|
|
|
return IsDeref &&
|
2015-03-15 07:44:48 +08:00
|
|
|
!getContext().getASTRecordLayout(SrcDecl).hasExtendableVFPtr();
|
2014-06-23 03:05:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static llvm::CallSite emitRTtypeidCall(CodeGenFunction &CGF,
|
|
|
|
llvm::Value *Argument) {
|
|
|
|
llvm::Type *ArgTypes[] = {CGF.Int8PtrTy};
|
|
|
|
llvm::FunctionType *FTy =
|
|
|
|
llvm::FunctionType::get(CGF.Int8PtrTy, ArgTypes, false);
|
|
|
|
llvm::Value *Args[] = {Argument};
|
|
|
|
llvm::Constant *Fn = CGF.CGM.CreateRuntimeFunction(FTy, "__RTtypeid");
|
|
|
|
return CGF.EmitRuntimeCallOrInvoke(Fn, Args);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MicrosoftCXXABI::EmitBadTypeidCall(CodeGenFunction &CGF) {
|
|
|
|
llvm::CallSite Call =
|
|
|
|
emitRTtypeidCall(CGF, llvm::Constant::getNullValue(CGM.VoidPtrTy));
|
|
|
|
Call.setDoesNotReturn();
|
|
|
|
CGF.Builder.CreateUnreachable();
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Value *MicrosoftCXXABI::EmitTypeid(CodeGenFunction &CGF,
|
|
|
|
QualType SrcRecordTy,
|
|
|
|
llvm::Value *ThisPtr,
|
|
|
|
llvm::Type *StdTypeInfoPtrTy) {
|
|
|
|
llvm::Value *Offset;
|
|
|
|
std::tie(ThisPtr, Offset) = performBaseAdjustment(CGF, ThisPtr, SrcRecordTy);
|
|
|
|
return CGF.Builder.CreateBitCast(
|
|
|
|
emitRTtypeidCall(CGF, ThisPtr).getInstruction(), StdTypeInfoPtrTy);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MicrosoftCXXABI::shouldDynamicCastCallBeNullChecked(bool SrcIsPtr,
|
|
|
|
QualType SrcRecordTy) {
|
|
|
|
const CXXRecordDecl *SrcDecl = SrcRecordTy->getAsCXXRecordDecl();
|
|
|
|
return SrcIsPtr &&
|
2015-03-15 07:44:48 +08:00
|
|
|
!getContext().getASTRecordLayout(SrcDecl).hasExtendableVFPtr();
|
2014-06-23 03:05:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Value *MicrosoftCXXABI::EmitDynamicCastCall(
|
|
|
|
CodeGenFunction &CGF, llvm::Value *Value, QualType SrcRecordTy,
|
|
|
|
QualType DestTy, QualType DestRecordTy, llvm::BasicBlock *CastEnd) {
|
|
|
|
llvm::Type *DestLTy = CGF.ConvertType(DestTy);
|
|
|
|
|
|
|
|
llvm::Value *SrcRTTI =
|
|
|
|
CGF.CGM.GetAddrOfRTTIDescriptor(SrcRecordTy.getUnqualifiedType());
|
|
|
|
llvm::Value *DestRTTI =
|
|
|
|
CGF.CGM.GetAddrOfRTTIDescriptor(DestRecordTy.getUnqualifiedType());
|
|
|
|
|
|
|
|
llvm::Value *Offset;
|
|
|
|
std::tie(Value, Offset) = performBaseAdjustment(CGF, Value, SrcRecordTy);
|
|
|
|
|
|
|
|
// PVOID __RTDynamicCast(
|
|
|
|
// PVOID inptr,
|
|
|
|
// LONG VfDelta,
|
|
|
|
// PVOID SrcType,
|
|
|
|
// PVOID TargetType,
|
|
|
|
// BOOL isReference)
|
|
|
|
llvm::Type *ArgTypes[] = {CGF.Int8PtrTy, CGF.Int32Ty, CGF.Int8PtrTy,
|
|
|
|
CGF.Int8PtrTy, CGF.Int32Ty};
|
|
|
|
llvm::Constant *Function = CGF.CGM.CreateRuntimeFunction(
|
|
|
|
llvm::FunctionType::get(CGF.Int8PtrTy, ArgTypes, false),
|
|
|
|
"__RTDynamicCast");
|
|
|
|
llvm::Value *Args[] = {
|
|
|
|
Value, Offset, SrcRTTI, DestRTTI,
|
|
|
|
llvm::ConstantInt::get(CGF.Int32Ty, DestTy->isReferenceType())};
|
|
|
|
Value = CGF.EmitRuntimeCallOrInvoke(Function, Args).getInstruction();
|
|
|
|
return CGF.Builder.CreateBitCast(Value, DestLTy);
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Value *
|
|
|
|
MicrosoftCXXABI::EmitDynamicCastToVoid(CodeGenFunction &CGF, llvm::Value *Value,
|
|
|
|
QualType SrcRecordTy,
|
|
|
|
QualType DestTy) {
|
|
|
|
llvm::Value *Offset;
|
|
|
|
std::tie(Value, Offset) = performBaseAdjustment(CGF, Value, SrcRecordTy);
|
|
|
|
|
|
|
|
// PVOID __RTCastToVoid(
|
|
|
|
// PVOID inptr)
|
|
|
|
llvm::Type *ArgTypes[] = {CGF.Int8PtrTy};
|
|
|
|
llvm::Constant *Function = CGF.CGM.CreateRuntimeFunction(
|
|
|
|
llvm::FunctionType::get(CGF.Int8PtrTy, ArgTypes, false),
|
|
|
|
"__RTCastToVoid");
|
|
|
|
llvm::Value *Args[] = {Value};
|
|
|
|
return CGF.EmitRuntimeCall(Function, Args);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MicrosoftCXXABI::EmitBadCastCall(CodeGenFunction &CGF) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-09-02 02:50:02 +08:00
|
|
|
llvm::Value *MicrosoftCXXABI::GetVirtualBaseClassOffset(
|
|
|
|
CodeGenFunction &CGF, llvm::Value *This, const CXXRecordDecl *ClassDecl,
|
|
|
|
const CXXRecordDecl *BaseClassDecl) {
|
2015-03-15 07:44:48 +08:00
|
|
|
const ASTContext &Context = getContext();
|
2014-01-14 08:50:39 +08:00
|
|
|
int64_t VBPtrChars =
|
2015-03-15 07:44:48 +08:00
|
|
|
Context.getASTRecordLayout(ClassDecl).getVBPtrOffset().getQuantity();
|
2013-05-30 02:02:47 +08:00
|
|
|
llvm::Value *VBPtrOffset = llvm::ConstantInt::get(CGM.PtrDiffTy, VBPtrChars);
|
2015-03-15 07:44:48 +08:00
|
|
|
CharUnits IntSize = Context.getTypeSizeInChars(Context.IntTy);
|
2013-11-05 23:54:58 +08:00
|
|
|
CharUnits VBTableChars =
|
|
|
|
IntSize *
|
|
|
|
CGM.getMicrosoftVTableContext().getVBTableIndex(ClassDecl, BaseClassDecl);
|
2013-05-30 02:02:47 +08:00
|
|
|
llvm::Value *VBTableOffset =
|
2014-09-02 02:50:02 +08:00
|
|
|
llvm::ConstantInt::get(CGM.IntTy, VBTableChars.getQuantity());
|
2013-05-30 02:02:47 +08:00
|
|
|
|
|
|
|
llvm::Value *VBPtrToNewBase =
|
2014-09-02 02:50:02 +08:00
|
|
|
GetVBaseOffsetFromVBPtr(CGF, This, VBPtrOffset, VBTableOffset);
|
2013-05-30 02:02:47 +08:00
|
|
|
VBPtrToNewBase =
|
2014-09-02 02:50:02 +08:00
|
|
|
CGF.Builder.CreateSExtOrBitCast(VBPtrToNewBase, CGM.PtrDiffTy);
|
2013-05-30 02:02:47 +08:00
|
|
|
return CGF.Builder.CreateNSWAdd(VBPtrOffset, VBPtrToNewBase);
|
|
|
|
}
|
|
|
|
|
2013-07-01 04:40:16 +08:00
|
|
|
bool MicrosoftCXXABI::HasThisReturn(GlobalDecl GD) const {
|
|
|
|
return isa<CXXConstructorDecl>(GD.getDecl());
|
2012-09-25 16:00:39 +08:00
|
|
|
}
|
|
|
|
|
2014-11-01 04:09:12 +08:00
|
|
|
static bool isDeletingDtor(GlobalDecl GD) {
|
|
|
|
return isa<CXXDestructorDecl>(GD.getDecl()) &&
|
|
|
|
GD.getDtorType() == Dtor_Deleting;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MicrosoftCXXABI::hasMostDerivedReturn(GlobalDecl GD) const {
|
|
|
|
return isDeletingDtor(GD);
|
|
|
|
}
|
|
|
|
|
2014-05-14 06:05:45 +08:00
|
|
|
bool MicrosoftCXXABI::classifyReturnType(CGFunctionInfo &FI) const {
|
|
|
|
const CXXRecordDecl *RD = FI.getReturnType()->getAsCXXRecordDecl();
|
|
|
|
if (!RD)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (FI.isInstanceMethod()) {
|
|
|
|
// If it's an instance method, aggregates are always returned indirectly via
|
|
|
|
// the second parameter.
|
|
|
|
FI.getReturnInfo() = ABIArgInfo::getIndirect(0, /*ByVal=*/false);
|
|
|
|
FI.getReturnInfo().setSRetAfterThis(FI.isInstanceMethod());
|
|
|
|
return true;
|
|
|
|
} else if (!RD->isPOD()) {
|
|
|
|
// If it's a free function, non-POD types are returned indirectly.
|
|
|
|
FI.getReturnInfo() = ABIArgInfo::getIndirect(0, /*ByVal=*/false);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, use the C ABI rules.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-06-19 23:20:38 +08:00
|
|
|
llvm::BasicBlock *
|
|
|
|
MicrosoftCXXABI::EmitCtorCompleteObjectHandler(CodeGenFunction &CGF,
|
|
|
|
const CXXRecordDecl *RD) {
|
2013-02-27 21:46:31 +08:00
|
|
|
llvm::Value *IsMostDerivedClass = getStructorImplicitParamValue(CGF);
|
|
|
|
assert(IsMostDerivedClass &&
|
|
|
|
"ctor for a class with virtual bases must have an implicit parameter");
|
2013-06-19 23:20:38 +08:00
|
|
|
llvm::Value *IsCompleteObject =
|
|
|
|
CGF.Builder.CreateIsNotNull(IsMostDerivedClass, "is_complete_object");
|
2013-02-27 21:46:31 +08:00
|
|
|
|
|
|
|
llvm::BasicBlock *CallVbaseCtorsBB = CGF.createBasicBlock("ctor.init_vbases");
|
|
|
|
llvm::BasicBlock *SkipVbaseCtorsBB = CGF.createBasicBlock("ctor.skip_vbases");
|
|
|
|
CGF.Builder.CreateCondBr(IsCompleteObject,
|
|
|
|
CallVbaseCtorsBB, SkipVbaseCtorsBB);
|
|
|
|
|
|
|
|
CGF.EmitBlock(CallVbaseCtorsBB);
|
2013-06-19 23:20:38 +08:00
|
|
|
|
|
|
|
// Fill in the vbtable pointers here.
|
|
|
|
EmitVBPtrStores(CGF, RD);
|
2013-02-27 21:46:31 +08:00
|
|
|
|
|
|
|
// CGF will put the base ctor calls in this basic block for us later.
|
|
|
|
|
|
|
|
return SkipVbaseCtorsBB;
|
2012-09-25 16:00:39 +08:00
|
|
|
}
|
|
|
|
|
2013-10-10 02:16:58 +08:00
|
|
|
void MicrosoftCXXABI::initializeHiddenVirtualInheritanceMembers(
|
|
|
|
CodeGenFunction &CGF, const CXXRecordDecl *RD) {
|
|
|
|
// In most cases, an override for a vbase virtual method can adjust
|
|
|
|
// the "this" parameter by applying a constant offset.
|
|
|
|
// However, this is not enough while a constructor or a destructor of some
|
|
|
|
// class X is being executed if all the following conditions are met:
|
|
|
|
// - X has virtual bases, (1)
|
|
|
|
// - X overrides a virtual method M of a vbase Y, (2)
|
|
|
|
// - X itself is a vbase of the most derived class.
|
|
|
|
//
|
|
|
|
// If (1) and (2) are true, the vtorDisp for vbase Y is a hidden member of X
|
|
|
|
// which holds the extra amount of "this" adjustment we must do when we use
|
|
|
|
// the X vftables (i.e. during X ctor or dtor).
|
|
|
|
// Outside the ctors and dtors, the values of vtorDisps are zero.
|
|
|
|
|
|
|
|
const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
|
|
|
|
typedef ASTRecordLayout::VBaseOffsetsMapTy VBOffsets;
|
|
|
|
const VBOffsets &VBaseMap = Layout.getVBaseOffsetsMap();
|
|
|
|
CGBuilderTy &Builder = CGF.Builder;
|
|
|
|
|
|
|
|
unsigned AS =
|
|
|
|
cast<llvm::PointerType>(getThisValue(CGF)->getType())->getAddressSpace();
|
2014-05-21 13:09:00 +08:00
|
|
|
llvm::Value *Int8This = nullptr; // Initialize lazily.
|
2013-10-10 02:16:58 +08:00
|
|
|
|
|
|
|
for (VBOffsets::const_iterator I = VBaseMap.begin(), E = VBaseMap.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
if (!I->second.hasVtorDisp())
|
|
|
|
continue;
|
|
|
|
|
2013-11-14 00:03:43 +08:00
|
|
|
llvm::Value *VBaseOffset =
|
|
|
|
GetVirtualBaseClassOffset(CGF, getThisValue(CGF), RD, I->first);
|
2013-10-10 02:16:58 +08:00
|
|
|
// FIXME: it doesn't look right that we SExt in GetVirtualBaseClassOffset()
|
|
|
|
// just to Trunc back immediately.
|
|
|
|
VBaseOffset = Builder.CreateTruncOrBitCast(VBaseOffset, CGF.Int32Ty);
|
|
|
|
uint64_t ConstantVBaseOffset =
|
|
|
|
Layout.getVBaseClassOffset(I->first).getQuantity();
|
|
|
|
|
|
|
|
// vtorDisp_for_vbase = vbptr[vbase_idx] - offsetof(RD, vbase).
|
|
|
|
llvm::Value *VtorDispValue = Builder.CreateSub(
|
|
|
|
VBaseOffset, llvm::ConstantInt::get(CGM.Int32Ty, ConstantVBaseOffset),
|
|
|
|
"vtordisp.value");
|
|
|
|
|
|
|
|
if (!Int8This)
|
|
|
|
Int8This = Builder.CreateBitCast(getThisValue(CGF),
|
|
|
|
CGF.Int8Ty->getPointerTo(AS));
|
|
|
|
llvm::Value *VtorDispPtr = Builder.CreateInBoundsGEP(Int8This, VBaseOffset);
|
|
|
|
// vtorDisp is always the 32-bits before the vbase in the class layout.
|
|
|
|
VtorDispPtr = Builder.CreateConstGEP1_32(VtorDispPtr, -4);
|
|
|
|
VtorDispPtr = Builder.CreateBitCast(
|
|
|
|
VtorDispPtr, CGF.Int32Ty->getPointerTo(AS), "vtordisp.ptr");
|
|
|
|
|
|
|
|
Builder.CreateStore(VtorDispValue, VtorDispPtr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-14 06:36:55 +08:00
|
|
|
static bool hasDefaultCXXMethodCC(ASTContext &Context,
|
|
|
|
const CXXMethodDecl *MD) {
|
|
|
|
CallingConv ExpectedCallingConv = Context.getDefaultCallingConvention(
|
|
|
|
/*IsVariadic=*/false, /*IsCXXMethod=*/true);
|
|
|
|
CallingConv ActualCallingConv =
|
|
|
|
MD->getType()->getAs<FunctionProtoType>()->getCallConv();
|
|
|
|
return ExpectedCallingConv == ActualCallingConv;
|
|
|
|
}
|
|
|
|
|
2013-08-05 01:30:04 +08:00
|
|
|
void MicrosoftCXXABI::EmitCXXConstructors(const CXXConstructorDecl *D) {
|
|
|
|
// There's only one constructor type in this ABI.
|
|
|
|
CGM.EmitGlobal(GlobalDecl(D, Ctor_Complete));
|
2015-03-14 06:36:55 +08:00
|
|
|
|
|
|
|
// Exported default constructors either have a simple call-site where they use
|
|
|
|
// the typical calling convention and have a single 'this' pointer for an
|
|
|
|
// argument -or- they get a wrapper function which appropriately thunks to the
|
|
|
|
// real default constructor. This thunk is the default constructor closure.
|
|
|
|
if (D->hasAttr<DLLExportAttr>() && D->isDefaultConstructor())
|
|
|
|
if (!hasDefaultCXXMethodCC(getContext(), D) || D->getNumParams() != 0) {
|
|
|
|
llvm::Function *Fn = getAddrOfCXXCtorClosure(D, Ctor_DefaultClosure);
|
|
|
|
Fn->setLinkage(llvm::GlobalValue::WeakODRLinkage);
|
|
|
|
Fn->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass);
|
|
|
|
}
|
2013-08-05 01:30:04 +08:00
|
|
|
}
|
|
|
|
|
2013-06-19 23:20:38 +08:00
|
|
|
void MicrosoftCXXABI::EmitVBPtrStores(CodeGenFunction &CGF,
|
|
|
|
const CXXRecordDecl *RD) {
|
|
|
|
llvm::Value *ThisInt8Ptr =
|
|
|
|
CGF.Builder.CreateBitCast(getThisValue(CGF), CGM.Int8PtrTy, "this.int8");
|
2015-03-15 07:44:48 +08:00
|
|
|
const ASTContext &Context = getContext();
|
|
|
|
const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
|
2013-06-19 23:20:38 +08:00
|
|
|
|
2014-01-03 08:14:35 +08:00
|
|
|
const VBTableGlobals &VBGlobals = enumerateVBTables(RD);
|
|
|
|
for (unsigned I = 0, E = VBGlobals.VBTables->size(); I != E; ++I) {
|
2014-02-28 03:40:09 +08:00
|
|
|
const VPtrInfo *VBT = (*VBGlobals.VBTables)[I];
|
2014-01-03 08:14:35 +08:00
|
|
|
llvm::GlobalVariable *GV = VBGlobals.Globals[I];
|
2013-06-19 23:20:38 +08:00
|
|
|
const ASTRecordLayout &SubobjectLayout =
|
2015-03-15 07:44:48 +08:00
|
|
|
Context.getASTRecordLayout(VBT->BaseWithVPtr);
|
2014-01-04 07:42:00 +08:00
|
|
|
CharUnits Offs = VBT->NonVirtualOffset;
|
|
|
|
Offs += SubobjectLayout.getVBPtrOffset();
|
2014-02-28 03:40:09 +08:00
|
|
|
if (VBT->getVBaseWithVPtr())
|
|
|
|
Offs += Layout.getVBaseClassOffset(VBT->getVBaseWithVPtr());
|
2013-06-19 23:20:38 +08:00
|
|
|
llvm::Value *VBPtr =
|
2014-01-04 07:42:00 +08:00
|
|
|
CGF.Builder.CreateConstInBoundsGEP1_64(ThisInt8Ptr, Offs.getQuantity());
|
2014-10-23 01:26:00 +08:00
|
|
|
llvm::Value *GVPtr = CGF.Builder.CreateConstInBoundsGEP2_32(GV, 0, 0);
|
|
|
|
VBPtr = CGF.Builder.CreateBitCast(VBPtr, GVPtr->getType()->getPointerTo(0),
|
2014-01-04 07:42:00 +08:00
|
|
|
"vbptr." + VBT->ReusingBase->getName());
|
2014-10-23 01:26:00 +08:00
|
|
|
CGF.Builder.CreateStore(GVPtr, VBPtr);
|
2013-06-19 23:20:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-09 00:01:27 +08:00
|
|
|
void
|
|
|
|
MicrosoftCXXABI::buildStructorSignature(const CXXMethodDecl *MD, StructorType T,
|
2013-02-13 16:37:51 +08:00
|
|
|
SmallVectorImpl<CanQualType> &ArgTys) {
|
|
|
|
// TODO: 'for base' flag
|
2014-09-09 00:01:27 +08:00
|
|
|
if (T == StructorType::Deleting) {
|
2013-08-27 18:38:19 +08:00
|
|
|
// The scalar deleting destructor takes an implicit int parameter.
|
2015-03-15 07:44:48 +08:00
|
|
|
ArgTys.push_back(getContext().IntTy);
|
2013-02-13 16:37:51 +08:00
|
|
|
}
|
2014-09-09 00:01:27 +08:00
|
|
|
auto *CD = dyn_cast<CXXConstructorDecl>(MD);
|
|
|
|
if (!CD)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// All parameters are already in place except is_most_derived, which goes
|
|
|
|
// after 'this' if it's variadic and last if it's not.
|
|
|
|
|
|
|
|
const CXXRecordDecl *Class = CD->getParent();
|
|
|
|
const FunctionProtoType *FPT = CD->getType()->castAs<FunctionProtoType>();
|
|
|
|
if (Class->getNumVBases()) {
|
|
|
|
if (FPT->isVariadic())
|
2015-03-15 07:44:48 +08:00
|
|
|
ArgTys.insert(ArgTys.begin() + 1, getContext().IntTy);
|
2014-09-09 00:01:27 +08:00
|
|
|
else
|
2015-03-15 07:44:48 +08:00
|
|
|
ArgTys.push_back(getContext().IntTy);
|
2014-09-09 00:01:27 +08:00
|
|
|
}
|
2013-02-13 16:37:51 +08:00
|
|
|
}
|
|
|
|
|
[ms-cxxabi] Emit linkonce complete dtors in TUs that need them
Based on Peter Collingbourne's destructor patches.
Prior to this change, clang was considering ?1 to be the complete
destructor and the base destructor, which was wrong. This lead to
crashes when clang tried to emit two LLVM functions with the same name.
In this ABI, TUs with non-inline dtors might not emit a complete
destructor. They are emitted as inline thunks in TUs that need them,
and they always delegate to the base dtors of the complete class and its
virtual bases. This change uses the DeferredDecls machinery to emit
complete dtors as needed.
Currently in clang try body destructors can catch exceptions thrown by
virtual base destructors. In the Microsoft C++ ABI, clang may not have
the destructor definition, in which case clang won't wrap the virtual
virtual base destructor calls in a try-catch. Diagnosing this in user
code is TODO.
Finally, for classes that don't use virtual inheritance, MSVC always
calls the base destructor (?1) directly. This is a useful code size
optimization that avoids emitting lots of extra thunks or aliases.
Implementing it also means our existing tests continue to pass, and is
consistent with MSVC's output.
We can do the same for Itanium by tweaking GetAddrOfCXXDestructor, but
it will require further testing.
Reviewers: rjmccall
CC: cfe-commits
Differential Revision: http://llvm-reviews.chandlerc.com/D1066
llvm-svn: 186828
2013-07-22 21:51:44 +08:00
|
|
|
void MicrosoftCXXABI::EmitCXXDestructors(const CXXDestructorDecl *D) {
|
|
|
|
// The TU defining a dtor is only guaranteed to emit a base destructor. All
|
|
|
|
// other destructor variants are delegating thunks.
|
|
|
|
CGM.EmitGlobal(GlobalDecl(D, Dtor_Base));
|
|
|
|
}
|
|
|
|
|
2014-03-15 01:43:37 +08:00
|
|
|
CharUnits
|
|
|
|
MicrosoftCXXABI::getVirtualFunctionPrologueThisAdjustment(GlobalDecl GD) {
|
|
|
|
GD = GD.getCanonicalDecl();
|
|
|
|
const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
|
|
|
|
|
|
|
|
GlobalDecl LookupGD = GD;
|
|
|
|
if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
|
|
|
|
// Complete destructors take a pointer to the complete object as a
|
|
|
|
// parameter, thus don't need this adjustment.
|
|
|
|
if (GD.getDtorType() == Dtor_Complete)
|
|
|
|
return CharUnits();
|
|
|
|
|
|
|
|
// There's no Dtor_Base in vftable but it shares the this adjustment with
|
|
|
|
// the deleting one, so look it up instead.
|
|
|
|
LookupGD = GlobalDecl(DD, Dtor_Deleting);
|
|
|
|
}
|
|
|
|
|
|
|
|
MicrosoftVTableContext::MethodVFTableLocation ML =
|
|
|
|
CGM.getMicrosoftVTableContext().getMethodVFTableLocation(LookupGD);
|
|
|
|
CharUnits Adjustment = ML.VFPtrOffset;
|
|
|
|
|
|
|
|
// Normal virtual instance methods need to adjust from the vfptr that first
|
|
|
|
// defined the virtual method to the virtual base subobject, but destructors
|
|
|
|
// do not. The vector deleting destructor thunk applies this adjustment for
|
|
|
|
// us if necessary.
|
|
|
|
if (isa<CXXDestructorDecl>(MD))
|
|
|
|
Adjustment = CharUnits::Zero();
|
|
|
|
|
|
|
|
if (ML.VBase) {
|
|
|
|
const ASTRecordLayout &DerivedLayout =
|
2015-03-15 07:44:48 +08:00
|
|
|
getContext().getASTRecordLayout(MD->getParent());
|
2014-03-15 01:43:37 +08:00
|
|
|
Adjustment += DerivedLayout.getVBaseClassOffset(ML.VBase);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Adjustment;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Value *MicrosoftCXXABI::adjustThisArgumentForVirtualFunctionCall(
|
|
|
|
CodeGenFunction &CGF, GlobalDecl GD, llvm::Value *This, bool VirtualCall) {
|
|
|
|
if (!VirtualCall) {
|
|
|
|
// If the call of a virtual function is not virtual, we just have to
|
|
|
|
// compensate for the adjustment the virtual function does in its prologue.
|
|
|
|
CharUnits Adjustment = getVirtualFunctionPrologueThisAdjustment(GD);
|
|
|
|
if (Adjustment.isZero())
|
|
|
|
return This;
|
|
|
|
|
|
|
|
unsigned AS = cast<llvm::PointerType>(This->getType())->getAddressSpace();
|
|
|
|
llvm::Type *charPtrTy = CGF.Int8Ty->getPointerTo(AS);
|
|
|
|
This = CGF.Builder.CreateBitCast(This, charPtrTy);
|
|
|
|
assert(Adjustment.isPositive());
|
|
|
|
return CGF.Builder.CreateConstGEP1_32(This, Adjustment.getQuantity());
|
|
|
|
}
|
|
|
|
|
2013-08-21 14:25:03 +08:00
|
|
|
GD = GD.getCanonicalDecl();
|
|
|
|
const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
|
2013-10-17 02:24:06 +08:00
|
|
|
|
|
|
|
GlobalDecl LookupGD = GD;
|
|
|
|
if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
|
|
|
|
// Complete dtors take a pointer to the complete object,
|
|
|
|
// thus don't need adjustment.
|
|
|
|
if (GD.getDtorType() == Dtor_Complete)
|
|
|
|
return This;
|
|
|
|
|
|
|
|
// There's only Dtor_Deleting in vftable but it shares the this adjustment
|
|
|
|
// with the base one, so look up the deleting one instead.
|
|
|
|
LookupGD = GlobalDecl(DD, Dtor_Deleting);
|
|
|
|
}
|
2013-11-05 23:54:58 +08:00
|
|
|
MicrosoftVTableContext::MethodVFTableLocation ML =
|
|
|
|
CGM.getMicrosoftVTableContext().getMethodVFTableLocation(LookupGD);
|
2013-08-21 14:25:03 +08:00
|
|
|
|
|
|
|
unsigned AS = cast<llvm::PointerType>(This->getType())->getAddressSpace();
|
|
|
|
llvm::Type *charPtrTy = CGF.Int8Ty->getPointerTo(AS);
|
2013-11-07 21:34:02 +08:00
|
|
|
CharUnits StaticOffset = ML.VFPtrOffset;
|
2014-02-19 06:51:52 +08:00
|
|
|
|
|
|
|
// Base destructors expect 'this' to point to the beginning of the base
|
|
|
|
// subobject, not the first vfptr that happens to contain the virtual dtor.
|
|
|
|
// However, we still need to apply the virtual base adjustment.
|
|
|
|
if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() == Dtor_Base)
|
|
|
|
StaticOffset = CharUnits::Zero();
|
|
|
|
|
2013-08-21 14:25:03 +08:00
|
|
|
if (ML.VBase) {
|
2014-03-15 01:43:37 +08:00
|
|
|
This = CGF.Builder.CreateBitCast(This, charPtrTy);
|
|
|
|
llvm::Value *VBaseOffset =
|
|
|
|
GetVirtualBaseClassOffset(CGF, This, MD->getParent(), ML.VBase);
|
|
|
|
This = CGF.Builder.CreateInBoundsGEP(This, VBaseOffset);
|
2013-08-21 14:25:03 +08:00
|
|
|
}
|
|
|
|
if (!StaticOffset.isZero()) {
|
|
|
|
assert(StaticOffset.isPositive());
|
|
|
|
This = CGF.Builder.CreateBitCast(This, charPtrTy);
|
2013-10-23 02:15:24 +08:00
|
|
|
if (ML.VBase) {
|
|
|
|
// Non-virtual adjustment might result in a pointer outside the allocated
|
|
|
|
// object, e.g. if the final overrider class is laid out after the virtual
|
|
|
|
// base that declares a method in the most derived class.
|
|
|
|
// FIXME: Update the code that emits this adjustment in thunks prologues.
|
|
|
|
This = CGF.Builder.CreateConstGEP1_32(This, StaticOffset.getQuantity());
|
|
|
|
} else {
|
|
|
|
This = CGF.Builder.CreateConstInBoundsGEP1_32(This,
|
|
|
|
StaticOffset.getQuantity());
|
|
|
|
}
|
2013-08-21 14:25:03 +08:00
|
|
|
}
|
|
|
|
return This;
|
|
|
|
}
|
|
|
|
|
2013-12-18 03:46:40 +08:00
|
|
|
void MicrosoftCXXABI::addImplicitStructorParams(CodeGenFunction &CGF,
|
|
|
|
QualType &ResTy,
|
|
|
|
FunctionArgList &Params) {
|
2013-02-27 21:46:31 +08:00
|
|
|
ASTContext &Context = getContext();
|
|
|
|
const CXXMethodDecl *MD = cast<CXXMethodDecl>(CGF.CurGD.getDecl());
|
2013-12-18 03:46:40 +08:00
|
|
|
assert(isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD));
|
2013-02-27 21:46:31 +08:00
|
|
|
if (isa<CXXConstructorDecl>(MD) && MD->getParent()->getNumVBases()) {
|
|
|
|
ImplicitParamDecl *IsMostDerived
|
2014-05-21 13:09:00 +08:00
|
|
|
= ImplicitParamDecl::Create(Context, nullptr,
|
2013-02-27 21:46:31 +08:00
|
|
|
CGF.CurGD.getDecl()->getLocation(),
|
|
|
|
&Context.Idents.get("is_most_derived"),
|
|
|
|
Context.IntTy);
|
2013-12-18 03:46:40 +08:00
|
|
|
// The 'most_derived' parameter goes second if the ctor is variadic and last
|
|
|
|
// if it's not. Dtors can't be variadic.
|
|
|
|
const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>();
|
|
|
|
if (FPT->isVariadic())
|
|
|
|
Params.insert(Params.begin() + 1, IsMostDerived);
|
|
|
|
else
|
|
|
|
Params.push_back(IsMostDerived);
|
2013-02-27 21:46:31 +08:00
|
|
|
getStructorImplicitParamDecl(CGF) = IsMostDerived;
|
2014-11-01 04:09:12 +08:00
|
|
|
} else if (isDeletingDtor(CGF.CurGD)) {
|
2013-02-13 16:37:51 +08:00
|
|
|
ImplicitParamDecl *ShouldDelete
|
2014-05-21 13:09:00 +08:00
|
|
|
= ImplicitParamDecl::Create(Context, nullptr,
|
2013-02-13 16:37:51 +08:00
|
|
|
CGF.CurGD.getDecl()->getLocation(),
|
|
|
|
&Context.Idents.get("should_call_delete"),
|
2013-08-27 18:38:19 +08:00
|
|
|
Context.IntTy);
|
2013-02-13 16:37:51 +08:00
|
|
|
Params.push_back(ShouldDelete);
|
|
|
|
getStructorImplicitParamDecl(CGF) = ShouldDelete;
|
|
|
|
}
|
2012-09-25 16:00:39 +08:00
|
|
|
}
|
|
|
|
|
2013-08-21 14:25:03 +08:00
|
|
|
llvm::Value *MicrosoftCXXABI::adjustThisParameterInVirtualFunctionPrologue(
|
|
|
|
CodeGenFunction &CGF, GlobalDecl GD, llvm::Value *This) {
|
|
|
|
// In this ABI, every virtual function takes a pointer to one of the
|
|
|
|
// subobjects that first defines it as the 'this' parameter, rather than a
|
2013-12-06 00:25:25 +08:00
|
|
|
// pointer to the final overrider subobject. Thus, we need to adjust it back
|
2013-08-21 14:25:03 +08:00
|
|
|
// to the final overrider subobject before use.
|
|
|
|
// See comments in the MicrosoftVFTableContext implementation for the details.
|
2014-03-15 01:43:37 +08:00
|
|
|
CharUnits Adjustment = getVirtualFunctionPrologueThisAdjustment(GD);
|
2013-08-21 14:25:03 +08:00
|
|
|
if (Adjustment.isZero())
|
|
|
|
return This;
|
|
|
|
|
|
|
|
unsigned AS = cast<llvm::PointerType>(This->getType())->getAddressSpace();
|
|
|
|
llvm::Type *charPtrTy = CGF.Int8Ty->getPointerTo(AS),
|
|
|
|
*thisTy = This->getType();
|
|
|
|
|
|
|
|
This = CGF.Builder.CreateBitCast(This, charPtrTy);
|
|
|
|
assert(Adjustment.isPositive());
|
2013-10-23 02:15:24 +08:00
|
|
|
This =
|
|
|
|
CGF.Builder.CreateConstInBoundsGEP1_32(This, -Adjustment.getQuantity());
|
2013-08-21 14:25:03 +08:00
|
|
|
return CGF.Builder.CreateBitCast(This, thisTy);
|
|
|
|
}
|
|
|
|
|
2012-09-25 16:00:39 +08:00
|
|
|
void MicrosoftCXXABI::EmitInstanceFunctionProlog(CodeGenFunction &CGF) {
|
|
|
|
EmitThisParam(CGF);
|
2013-07-01 04:40:16 +08:00
|
|
|
|
|
|
|
/// If this is a function that the ABI specifies returns 'this', initialize
|
|
|
|
/// the return slot to 'this' at the start of the function.
|
|
|
|
///
|
|
|
|
/// Unlike the setting of return types, this is done within the ABI
|
|
|
|
/// implementation instead of by clients of CGCXXABI because:
|
|
|
|
/// 1) getThisValue is currently protected
|
|
|
|
/// 2) in theory, an ABI could implement 'this' returns some other way;
|
|
|
|
/// HasThisReturn only specifies a contract, not the implementation
|
|
|
|
if (HasThisReturn(CGF.CurGD))
|
2012-09-25 16:00:39 +08:00
|
|
|
CGF.Builder.CreateStore(getThisValue(CGF), CGF.ReturnValue);
|
2014-11-01 04:09:12 +08:00
|
|
|
else if (hasMostDerivedReturn(CGF.CurGD))
|
|
|
|
CGF.Builder.CreateStore(CGF.EmitCastToVoidPtr(getThisValue(CGF)),
|
|
|
|
CGF.ReturnValue);
|
2013-02-27 21:46:31 +08:00
|
|
|
|
|
|
|
const CXXMethodDecl *MD = cast<CXXMethodDecl>(CGF.CurGD.getDecl());
|
|
|
|
if (isa<CXXConstructorDecl>(MD) && MD->getParent()->getNumVBases()) {
|
|
|
|
assert(getStructorImplicitParamDecl(CGF) &&
|
|
|
|
"no implicit parameter for a constructor with virtual bases?");
|
|
|
|
getStructorImplicitParamValue(CGF)
|
|
|
|
= CGF.Builder.CreateLoad(
|
|
|
|
CGF.GetAddrOfLocalVar(getStructorImplicitParamDecl(CGF)),
|
|
|
|
"is_most_derived");
|
|
|
|
}
|
|
|
|
|
2014-11-01 04:09:12 +08:00
|
|
|
if (isDeletingDtor(CGF.CurGD)) {
|
2013-02-13 16:37:51 +08:00
|
|
|
assert(getStructorImplicitParamDecl(CGF) &&
|
|
|
|
"no implicit parameter for a deleting destructor?");
|
|
|
|
getStructorImplicitParamValue(CGF)
|
|
|
|
= CGF.Builder.CreateLoad(
|
|
|
|
CGF.GetAddrOfLocalVar(getStructorImplicitParamDecl(CGF)),
|
|
|
|
"should_call_delete");
|
|
|
|
}
|
2012-09-25 16:00:39 +08:00
|
|
|
}
|
|
|
|
|
2013-12-18 03:46:40 +08:00
|
|
|
unsigned MicrosoftCXXABI::addImplicitConstructorArgs(
|
|
|
|
CodeGenFunction &CGF, const CXXConstructorDecl *D, CXXCtorType Type,
|
|
|
|
bool ForVirtualBase, bool Delegating, CallArgList &Args) {
|
2013-02-27 21:46:31 +08:00
|
|
|
assert(Type == Ctor_Complete || Type == Ctor_Base);
|
|
|
|
|
2013-12-18 03:46:40 +08:00
|
|
|
// Check if we need a 'most_derived' parameter.
|
|
|
|
if (!D->getParent()->getNumVBases())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// Add the 'most_derived' argument second if we are variadic or last if not.
|
|
|
|
const FunctionProtoType *FPT = D->getType()->castAs<FunctionProtoType>();
|
|
|
|
llvm::Value *MostDerivedArg =
|
|
|
|
llvm::ConstantInt::get(CGM.Int32Ty, Type == Ctor_Complete);
|
|
|
|
RValue RV = RValue::get(MostDerivedArg);
|
|
|
|
if (MostDerivedArg) {
|
|
|
|
if (FPT->isVariadic())
|
|
|
|
Args.insert(Args.begin() + 1,
|
|
|
|
CallArg(RV, getContext().IntTy, /*needscopy=*/false));
|
|
|
|
else
|
|
|
|
Args.add(RV, getContext().IntTy);
|
2013-02-27 21:46:31 +08:00
|
|
|
}
|
|
|
|
|
2013-12-18 03:46:40 +08:00
|
|
|
return 1; // Added one arg.
|
2013-02-27 21:46:31 +08:00
|
|
|
}
|
|
|
|
|
2013-12-13 08:53:54 +08:00
|
|
|
void MicrosoftCXXABI::EmitDestructorCall(CodeGenFunction &CGF,
|
|
|
|
const CXXDestructorDecl *DD,
|
|
|
|
CXXDtorType Type, bool ForVirtualBase,
|
|
|
|
bool Delegating, llvm::Value *This) {
|
2014-09-11 23:42:06 +08:00
|
|
|
llvm::Value *Callee = CGM.getAddrOfCXXStructor(DD, getFromDtorType(Type));
|
2013-12-13 08:53:54 +08:00
|
|
|
|
2014-03-15 01:43:37 +08:00
|
|
|
if (DD->isVirtual()) {
|
|
|
|
assert(Type != CXXDtorType::Dtor_Deleting &&
|
|
|
|
"The deleting destructor should only be called via a virtual call");
|
|
|
|
This = adjustThisArgumentForVirtualFunctionCall(CGF, GlobalDecl(DD, Type),
|
|
|
|
This, false);
|
|
|
|
}
|
2013-12-13 08:53:54 +08:00
|
|
|
|
2014-11-01 04:09:12 +08:00
|
|
|
CGF.EmitCXXStructorCall(DD, Callee, ReturnValueSlot(), This,
|
|
|
|
/*ImplicitParam=*/nullptr,
|
|
|
|
/*ImplicitParamTy=*/QualType(), nullptr,
|
|
|
|
getFromDtorType(Type));
|
2013-12-13 08:53:54 +08:00
|
|
|
}
|
|
|
|
|
2013-09-27 22:48:01 +08:00
|
|
|
void MicrosoftCXXABI::emitVTableDefinitions(CodeGenVTables &CGVT,
|
|
|
|
const CXXRecordDecl *RD) {
|
2013-11-05 23:54:58 +08:00
|
|
|
MicrosoftVTableContext &VFTContext = CGM.getMicrosoftVTableContext();
|
2014-09-11 22:13:49 +08:00
|
|
|
const VPtrInfoVector &VFPtrs = VFTContext.getVFPtrOffsets(RD);
|
2013-09-27 22:48:01 +08:00
|
|
|
|
2014-05-24 00:07:43 +08:00
|
|
|
for (VPtrInfo *Info : VFPtrs) {
|
|
|
|
llvm::GlobalVariable *VTable = getAddrOfVTable(RD, Info->FullOffsetInMDC);
|
2013-09-27 22:48:01 +08:00
|
|
|
if (VTable->hasInitializer())
|
|
|
|
continue;
|
2014-07-02 04:30:31 +08:00
|
|
|
|
2014-07-24 14:09:19 +08:00
|
|
|
llvm::Constant *RTTI = getContext().getLangOpts().RTTIData
|
|
|
|
? getMSCompleteObjectLocator(RD, Info)
|
|
|
|
: nullptr;
|
2013-09-27 22:48:01 +08:00
|
|
|
|
|
|
|
const VTableLayout &VTLayout =
|
2014-05-24 00:07:43 +08:00
|
|
|
VFTContext.getVFTableLayout(RD, Info->FullOffsetInMDC);
|
2013-09-27 22:48:01 +08:00
|
|
|
llvm::Constant *Init = CGVT.CreateVTableInitializer(
|
|
|
|
RD, VTLayout.vtable_component_begin(),
|
|
|
|
VTLayout.getNumVTableComponents(), VTLayout.vtable_thunk_begin(),
|
2014-07-02 04:30:31 +08:00
|
|
|
VTLayout.getNumVTableThunks(), RTTI);
|
2014-05-31 00:59:42 +08:00
|
|
|
|
2014-07-02 04:30:31 +08:00
|
|
|
VTable->setInitializer(Init);
|
2013-09-27 22:48:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Value *MicrosoftCXXABI::getVTableAddressPointInStructor(
|
|
|
|
CodeGenFunction &CGF, const CXXRecordDecl *VTableClass, BaseSubobject Base,
|
|
|
|
const CXXRecordDecl *NearestVBase, bool &NeedsVirtualOffset) {
|
2014-05-21 13:09:00 +08:00
|
|
|
NeedsVirtualOffset = (NearestVBase != nullptr);
|
2013-09-27 22:48:01 +08:00
|
|
|
|
2014-07-02 04:30:31 +08:00
|
|
|
(void)getAddrOfVTable(VTableClass, Base.getBaseOffset());
|
|
|
|
VFTableIdTy ID(VTableClass, Base.getBaseOffset());
|
|
|
|
llvm::GlobalValue *VTableAddressPoint = VFTablesMap[ID];
|
2013-09-27 22:48:01 +08:00
|
|
|
if (!VTableAddressPoint) {
|
|
|
|
assert(Base.getBase()->getNumVBases() &&
|
2015-03-15 07:44:48 +08:00
|
|
|
!getContext().getASTRecordLayout(Base.getBase()).hasOwnVFPtr());
|
2013-09-27 22:48:01 +08:00
|
|
|
}
|
|
|
|
return VTableAddressPoint;
|
|
|
|
}
|
|
|
|
|
2013-10-03 14:26:13 +08:00
|
|
|
static void mangleVFTableName(MicrosoftMangleContext &MangleContext,
|
2014-02-28 03:40:09 +08:00
|
|
|
const CXXRecordDecl *RD, const VPtrInfo *VFPtr,
|
2013-10-03 14:26:13 +08:00
|
|
|
SmallString<256> &Name) {
|
2013-09-27 22:48:01 +08:00
|
|
|
llvm::raw_svector_ostream Out(Name);
|
2014-02-28 03:40:09 +08:00
|
|
|
MangleContext.mangleCXXVFTable(RD, VFPtr->MangledPath, Out);
|
2013-09-27 22:48:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Constant *MicrosoftCXXABI::getVTableAddressPointForConstExpr(
|
|
|
|
BaseSubobject Base, const CXXRecordDecl *VTableClass) {
|
2014-07-02 04:30:31 +08:00
|
|
|
(void)getAddrOfVTable(VTableClass, Base.getBaseOffset());
|
|
|
|
VFTableIdTy ID(VTableClass, Base.getBaseOffset());
|
|
|
|
llvm::GlobalValue *VFTable = VFTablesMap[ID];
|
|
|
|
assert(VFTable && "Couldn't find a vftable for the given base?");
|
|
|
|
return VFTable;
|
2013-09-27 22:48:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm::GlobalVariable *MicrosoftCXXABI::getAddrOfVTable(const CXXRecordDecl *RD,
|
|
|
|
CharUnits VPtrOffset) {
|
|
|
|
// getAddrOfVTable may return 0 if asked to get an address of a vtable which
|
|
|
|
// shouldn't be used in the given record type. We want to cache this result in
|
|
|
|
// VFTablesMap, thus a simple zero check is not sufficient.
|
|
|
|
VFTableIdTy ID(RD, VPtrOffset);
|
2014-07-02 04:30:31 +08:00
|
|
|
VTablesMapTy::iterator I;
|
2013-09-27 22:48:01 +08:00
|
|
|
bool Inserted;
|
2014-07-02 04:30:31 +08:00
|
|
|
std::tie(I, Inserted) = VTablesMap.insert(std::make_pair(ID, nullptr));
|
2013-09-27 22:48:01 +08:00
|
|
|
if (!Inserted)
|
|
|
|
return I->second;
|
|
|
|
|
|
|
|
llvm::GlobalVariable *&VTable = I->second;
|
|
|
|
|
2013-11-05 23:54:58 +08:00
|
|
|
MicrosoftVTableContext &VTContext = CGM.getMicrosoftVTableContext();
|
2014-02-28 03:40:09 +08:00
|
|
|
const VPtrInfoVector &VFPtrs = VTContext.getVFPtrOffsets(RD);
|
2013-09-27 22:48:01 +08:00
|
|
|
|
2014-11-19 15:49:47 +08:00
|
|
|
if (DeferredVFTables.insert(RD).second) {
|
2013-09-27 22:48:01 +08:00
|
|
|
// We haven't processed this record type before.
|
|
|
|
// Queue up this v-table for possible deferred emission.
|
|
|
|
CGM.addDeferredVTable(RD);
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
// Create all the vftables at once in order to make sure each vftable has
|
|
|
|
// a unique mangled name.
|
|
|
|
llvm::StringSet<> ObservedMangledNames;
|
|
|
|
for (size_t J = 0, F = VFPtrs.size(); J != F; ++J) {
|
|
|
|
SmallString<256> Name;
|
2013-10-03 14:26:13 +08:00
|
|
|
mangleVFTableName(getMangleContext(), RD, VFPtrs[J], Name);
|
2014-11-19 10:56:13 +08:00
|
|
|
if (!ObservedMangledNames.insert(Name.str()).second)
|
2013-09-27 22:48:01 +08:00
|
|
|
llvm_unreachable("Already saw this mangling before?");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-03-19 06:04:43 +08:00
|
|
|
VPtrInfo *const *VFPtrI =
|
|
|
|
std::find_if(VFPtrs.begin(), VFPtrs.end(), [&](VPtrInfo *VPI) {
|
|
|
|
return VPI->FullOffsetInMDC == VPtrOffset;
|
|
|
|
});
|
|
|
|
if (VFPtrI == VFPtrs.end()) {
|
|
|
|
VFTablesMap[ID] = nullptr;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
VPtrInfo *VFPtr = *VFPtrI;
|
|
|
|
|
|
|
|
SmallString<256> VFTableName;
|
|
|
|
mangleVFTableName(getMangleContext(), RD, VFPtr, VFTableName);
|
|
|
|
|
|
|
|
llvm::GlobalValue::LinkageTypes VFTableLinkage = CGM.getVTableLinkage(RD);
|
|
|
|
bool VFTableComesFromAnotherTU =
|
|
|
|
llvm::GlobalValue::isAvailableExternallyLinkage(VFTableLinkage) ||
|
|
|
|
llvm::GlobalValue::isExternalLinkage(VFTableLinkage);
|
|
|
|
bool VTableAliasIsRequred =
|
|
|
|
!VFTableComesFromAnotherTU && getContext().getLangOpts().RTTIData;
|
|
|
|
|
|
|
|
if (llvm::GlobalValue *VFTable =
|
|
|
|
CGM.getModule().getNamedGlobal(VFTableName)) {
|
|
|
|
VFTablesMap[ID] = VFTable;
|
|
|
|
return VTableAliasIsRequred
|
|
|
|
? cast<llvm::GlobalVariable>(
|
|
|
|
cast<llvm::GlobalAlias>(VFTable)->getBaseObject())
|
|
|
|
: cast<llvm::GlobalVariable>(VFTable);
|
|
|
|
}
|
2014-07-02 04:30:31 +08:00
|
|
|
|
2015-03-19 06:04:43 +08:00
|
|
|
uint64_t NumVTableSlots =
|
|
|
|
VTContext.getVFTableLayout(RD, VFPtr->FullOffsetInMDC)
|
|
|
|
.getNumVTableComponents();
|
|
|
|
llvm::GlobalValue::LinkageTypes VTableLinkage =
|
|
|
|
VTableAliasIsRequred ? llvm::GlobalValue::PrivateLinkage : VFTableLinkage;
|
|
|
|
|
|
|
|
StringRef VTableName = VTableAliasIsRequred ? StringRef() : VFTableName.str();
|
|
|
|
|
|
|
|
llvm::ArrayType *VTableType =
|
|
|
|
llvm::ArrayType::get(CGM.Int8PtrTy, NumVTableSlots);
|
|
|
|
|
|
|
|
// Create a backing variable for the contents of VTable. The VTable may
|
|
|
|
// or may not include space for a pointer to RTTI data.
|
|
|
|
llvm::GlobalValue *VFTable;
|
|
|
|
VTable = new llvm::GlobalVariable(CGM.getModule(), VTableType,
|
|
|
|
/*isConstant=*/true, VTableLinkage,
|
|
|
|
/*Initializer=*/nullptr, VTableName);
|
|
|
|
VTable->setUnnamedAddr(true);
|
|
|
|
|
|
|
|
llvm::Comdat *C = nullptr;
|
|
|
|
if (!VFTableComesFromAnotherTU &&
|
|
|
|
(llvm::GlobalValue::isWeakForLinker(VFTableLinkage) ||
|
|
|
|
(llvm::GlobalValue::isLocalLinkage(VFTableLinkage) &&
|
|
|
|
VTableAliasIsRequred)))
|
|
|
|
C = CGM.getModule().getOrInsertComdat(VFTableName.str());
|
|
|
|
|
|
|
|
// Only insert a pointer into the VFTable for RTTI data if we are not
|
|
|
|
// importing it. We never reference the RTTI data directly so there is no
|
|
|
|
// need to make room for it.
|
|
|
|
if (VTableAliasIsRequred) {
|
|
|
|
llvm::Value *GEPIndices[] = {llvm::ConstantInt::get(CGM.IntTy, 0),
|
|
|
|
llvm::ConstantInt::get(CGM.IntTy, 1)};
|
|
|
|
// Create a GEP which points just after the first entry in the VFTable,
|
|
|
|
// this should be the location of the first virtual method.
|
|
|
|
llvm::Constant *VTableGEP =
|
|
|
|
llvm::ConstantExpr::getInBoundsGetElementPtr(VTable, GEPIndices);
|
|
|
|
if (llvm::GlobalValue::isWeakForLinker(VFTableLinkage)) {
|
|
|
|
VFTableLinkage = llvm::GlobalValue::ExternalLinkage;
|
|
|
|
if (C)
|
|
|
|
C->setSelectionKind(llvm::Comdat::Largest);
|
2014-07-02 04:30:31 +08:00
|
|
|
}
|
2015-03-19 06:04:43 +08:00
|
|
|
VFTable = llvm::GlobalAlias::create(
|
|
|
|
cast<llvm::SequentialType>(VTableGEP->getType())->getElementType(),
|
|
|
|
/*AddressSpace=*/0, VFTableLinkage, VFTableName.str(), VTableGEP,
|
|
|
|
&CGM.getModule());
|
|
|
|
VFTable->setUnnamedAddr(true);
|
|
|
|
} else {
|
|
|
|
// We don't need a GlobalAlias to be a symbol for the VTable if we won't
|
|
|
|
// be referencing any RTTI data.
|
|
|
|
// The GlobalVariable will end up being an appropriate definition of the
|
|
|
|
// VFTable.
|
|
|
|
VFTable = VTable;
|
2013-09-27 22:48:01 +08:00
|
|
|
}
|
2015-03-19 06:04:43 +08:00
|
|
|
if (C)
|
|
|
|
VTable->setComdat(C);
|
|
|
|
|
|
|
|
if (RD->hasAttr<DLLImportAttr>())
|
|
|
|
VFTable->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
|
|
|
|
else if (RD->hasAttr<DLLExportAttr>())
|
|
|
|
VFTable->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass);
|
2013-09-27 22:48:01 +08:00
|
|
|
|
2015-03-19 06:04:43 +08:00
|
|
|
VFTablesMap[ID] = VFTable;
|
2013-09-27 22:48:01 +08:00
|
|
|
return VTable;
|
|
|
|
}
|
|
|
|
|
2013-08-21 14:25:03 +08:00
|
|
|
llvm::Value *MicrosoftCXXABI::getVirtualFunctionPointer(CodeGenFunction &CGF,
|
|
|
|
GlobalDecl GD,
|
|
|
|
llvm::Value *This,
|
|
|
|
llvm::Type *Ty) {
|
|
|
|
GD = GD.getCanonicalDecl();
|
|
|
|
CGBuilderTy &Builder = CGF.Builder;
|
|
|
|
|
|
|
|
Ty = Ty->getPointerTo()->getPointerTo();
|
2014-03-15 01:43:37 +08:00
|
|
|
llvm::Value *VPtr =
|
|
|
|
adjustThisArgumentForVirtualFunctionCall(CGF, GD, This, true);
|
2013-08-21 14:25:03 +08:00
|
|
|
llvm::Value *VTable = CGF.GetVTablePtr(VPtr, Ty);
|
|
|
|
|
2013-11-05 23:54:58 +08:00
|
|
|
MicrosoftVTableContext::MethodVFTableLocation ML =
|
|
|
|
CGM.getMicrosoftVTableContext().getMethodVFTableLocation(GD);
|
2013-08-21 14:25:03 +08:00
|
|
|
llvm::Value *VFuncPtr =
|
|
|
|
Builder.CreateConstInBoundsGEP1_64(VTable, ML.Index, "vfn");
|
|
|
|
return Builder.CreateLoad(VFuncPtr);
|
|
|
|
}
|
|
|
|
|
2014-11-01 04:09:12 +08:00
|
|
|
llvm::Value *MicrosoftCXXABI::EmitVirtualDestructorCall(
|
|
|
|
CodeGenFunction &CGF, const CXXDestructorDecl *Dtor, CXXDtorType DtorType,
|
|
|
|
llvm::Value *This, const CXXMemberCallExpr *CE) {
|
2014-08-26 04:17:35 +08:00
|
|
|
assert(CE == nullptr || CE->arg_begin() == CE->arg_end());
|
2013-02-15 22:45:22 +08:00
|
|
|
assert(DtorType == Dtor_Deleting || DtorType == Dtor_Complete);
|
|
|
|
|
|
|
|
// We have only one destructor in the vftable but can get both behaviors
|
2013-08-27 18:38:19 +08:00
|
|
|
// by passing an implicit int parameter.
|
2013-10-17 02:24:06 +08:00
|
|
|
GlobalDecl GD(Dtor, Dtor_Deleting);
|
2014-09-09 00:01:27 +08:00
|
|
|
const CGFunctionInfo *FInfo = &CGM.getTypes().arrangeCXXStructorDeclaration(
|
|
|
|
Dtor, StructorType::Deleting);
|
2013-02-15 22:45:22 +08:00
|
|
|
llvm::Type *Ty = CGF.CGM.getTypes().GetFunctionType(*FInfo);
|
2013-10-17 02:24:06 +08:00
|
|
|
llvm::Value *Callee = getVirtualFunctionPointer(CGF, GD, This, Ty);
|
2013-02-15 22:45:22 +08:00
|
|
|
|
2015-03-15 07:44:48 +08:00
|
|
|
ASTContext &Context = getContext();
|
2015-01-13 05:24:10 +08:00
|
|
|
llvm::Value *ImplicitParam = llvm::ConstantInt::get(
|
|
|
|
llvm::IntegerType::getInt32Ty(CGF.getLLVMContext()),
|
|
|
|
DtorType == Dtor_Deleting);
|
2013-02-15 22:45:22 +08:00
|
|
|
|
2014-03-15 01:43:37 +08:00
|
|
|
This = adjustThisArgumentForVirtualFunctionCall(CGF, GD, This, true);
|
2014-11-01 04:09:12 +08:00
|
|
|
RValue RV = CGF.EmitCXXStructorCall(Dtor, Callee, ReturnValueSlot(), This,
|
|
|
|
ImplicitParam, Context.IntTy, CE,
|
|
|
|
StructorType::Deleting);
|
|
|
|
return RV.getScalarVal();
|
2013-02-15 22:45:22 +08:00
|
|
|
}
|
|
|
|
|
2014-01-03 08:14:35 +08:00
|
|
|
const VBTableGlobals &
|
|
|
|
MicrosoftCXXABI::enumerateVBTables(const CXXRecordDecl *RD) {
|
2013-06-19 23:20:38 +08:00
|
|
|
// At this layer, we can key the cache off of a single class, which is much
|
2014-01-03 08:14:35 +08:00
|
|
|
// easier than caching each vbtable individually.
|
|
|
|
llvm::DenseMap<const CXXRecordDecl*, VBTableGlobals>::iterator Entry;
|
|
|
|
bool Added;
|
2014-03-02 21:01:17 +08:00
|
|
|
std::tie(Entry, Added) =
|
|
|
|
VBTablesMap.insert(std::make_pair(RD, VBTableGlobals()));
|
2014-01-03 08:14:35 +08:00
|
|
|
VBTableGlobals &VBGlobals = Entry->second;
|
|
|
|
if (!Added)
|
|
|
|
return VBGlobals;
|
|
|
|
|
|
|
|
MicrosoftVTableContext &Context = CGM.getMicrosoftVTableContext();
|
|
|
|
VBGlobals.VBTables = &Context.enumerateVBTables(RD);
|
|
|
|
|
|
|
|
// Cache the globals for all vbtables so we don't have to recompute the
|
|
|
|
// mangled names.
|
|
|
|
llvm::GlobalVariable::LinkageTypes Linkage = CGM.getVTableLinkage(RD);
|
2014-02-28 03:40:09 +08:00
|
|
|
for (VPtrInfoVector::const_iterator I = VBGlobals.VBTables->begin(),
|
|
|
|
E = VBGlobals.VBTables->end();
|
2014-01-03 08:14:35 +08:00
|
|
|
I != E; ++I) {
|
2014-01-04 07:42:00 +08:00
|
|
|
VBGlobals.Globals.push_back(getAddrOfVBTable(**I, RD, Linkage));
|
2014-01-03 08:14:35 +08:00
|
|
|
}
|
2013-06-19 23:20:38 +08:00
|
|
|
|
2014-01-03 08:14:35 +08:00
|
|
|
return VBGlobals;
|
2013-06-19 23:20:38 +08:00
|
|
|
}
|
|
|
|
|
2014-02-21 10:27:32 +08:00
|
|
|
llvm::Function *MicrosoftCXXABI::EmitVirtualMemPtrThunk(
|
|
|
|
const CXXMethodDecl *MD,
|
|
|
|
const MicrosoftVTableContext::MethodVFTableLocation &ML) {
|
2014-08-30 05:43:29 +08:00
|
|
|
assert(!isa<CXXConstructorDecl>(MD) && !isa<CXXDestructorDecl>(MD) &&
|
|
|
|
"can't form pointers to ctors or virtual dtors");
|
|
|
|
|
2014-02-21 10:27:32 +08:00
|
|
|
// Calculate the mangled name.
|
|
|
|
SmallString<256> ThunkName;
|
|
|
|
llvm::raw_svector_ostream Out(ThunkName);
|
|
|
|
getMangleContext().mangleVirtualMemPtrThunk(MD, Out);
|
|
|
|
Out.flush();
|
|
|
|
|
2013-11-16 01:24:45 +08:00
|
|
|
// If the thunk has been generated previously, just return it.
|
|
|
|
if (llvm::GlobalValue *GV = CGM.getModule().getNamedValue(ThunkName))
|
|
|
|
return cast<llvm::Function>(GV);
|
|
|
|
|
|
|
|
// Create the llvm::Function.
|
2014-08-30 05:43:29 +08:00
|
|
|
const CGFunctionInfo &FnInfo = CGM.getTypes().arrangeMSMemberPointerThunk(MD);
|
2013-11-16 01:24:45 +08:00
|
|
|
llvm::FunctionType *ThunkTy = CGM.getTypes().GetFunctionType(FnInfo);
|
|
|
|
llvm::Function *ThunkFn =
|
|
|
|
llvm::Function::Create(ThunkTy, llvm::Function::ExternalLinkage,
|
|
|
|
ThunkName.str(), &CGM.getModule());
|
|
|
|
assert(ThunkFn->getName() == ThunkName && "name was uniqued!");
|
|
|
|
|
|
|
|
ThunkFn->setLinkage(MD->isExternallyVisible()
|
|
|
|
? llvm::GlobalValue::LinkOnceODRLinkage
|
|
|
|
: llvm::GlobalValue::InternalLinkage);
|
2015-01-21 09:21:31 +08:00
|
|
|
if (MD->isExternallyVisible())
|
|
|
|
ThunkFn->setComdat(CGM.getModule().getOrInsertComdat(ThunkFn->getName()));
|
2013-11-16 01:24:45 +08:00
|
|
|
|
|
|
|
CGM.SetLLVMFunctionAttributes(MD, FnInfo, ThunkFn);
|
|
|
|
CGM.SetLLVMFunctionAttributesForDefinition(MD, ThunkFn);
|
|
|
|
|
2015-01-22 06:18:17 +08:00
|
|
|
// Add the "thunk" attribute so that LLVM knows that the return type is
|
|
|
|
// meaningless. These thunks can be used to call functions with differing
|
|
|
|
// return types, and the caller is required to cast the prototype
|
|
|
|
// appropriately to extract the correct value.
|
|
|
|
ThunkFn->addFnAttr("thunk");
|
|
|
|
|
2014-08-16 02:12:40 +08:00
|
|
|
// These thunks can be compared, so they are not unnamed.
|
|
|
|
ThunkFn->setUnnamedAddr(false);
|
|
|
|
|
2013-11-16 01:24:45 +08:00
|
|
|
// Start codegen.
|
|
|
|
CodeGenFunction CGF(CGM);
|
2014-08-30 05:43:29 +08:00
|
|
|
CGF.CurGD = GlobalDecl(MD);
|
|
|
|
CGF.CurFuncIsThunk = true;
|
|
|
|
|
|
|
|
// Build FunctionArgs, but only include the implicit 'this' parameter
|
|
|
|
// declaration.
|
|
|
|
FunctionArgList FunctionArgs;
|
|
|
|
buildThisParam(CGF, FunctionArgs);
|
|
|
|
|
|
|
|
// Start defining the function.
|
|
|
|
CGF.StartFunction(GlobalDecl(), FnInfo.getReturnType(), ThunkFn, FnInfo,
|
|
|
|
FunctionArgs, MD->getLocation(), SourceLocation());
|
|
|
|
EmitThisParam(CGF);
|
2013-11-16 01:24:45 +08:00
|
|
|
|
2014-02-21 10:27:32 +08:00
|
|
|
// Load the vfptr and then callee from the vftable. The callee should have
|
|
|
|
// adjusted 'this' so that the vfptr is at offset zero.
|
2014-08-30 05:43:29 +08:00
|
|
|
llvm::Value *VTable = CGF.GetVTablePtr(
|
|
|
|
getThisValue(CGF), ThunkTy->getPointerTo()->getPointerTo());
|
2014-02-21 10:27:32 +08:00
|
|
|
llvm::Value *VFuncPtr =
|
|
|
|
CGF.Builder.CreateConstInBoundsGEP1_64(VTable, ML.Index, "vfn");
|
|
|
|
llvm::Value *Callee = CGF.Builder.CreateLoad(VFuncPtr);
|
2013-11-16 01:24:45 +08:00
|
|
|
|
2014-08-30 05:43:29 +08:00
|
|
|
CGF.EmitMustTailThunk(MD, getThisValue(CGF), Callee);
|
2013-11-16 01:24:45 +08:00
|
|
|
|
|
|
|
return ThunkFn;
|
|
|
|
}
|
|
|
|
|
2013-09-27 22:48:01 +08:00
|
|
|
void MicrosoftCXXABI::emitVirtualInheritanceTables(const CXXRecordDecl *RD) {
|
2014-01-03 08:14:35 +08:00
|
|
|
const VBTableGlobals &VBGlobals = enumerateVBTables(RD);
|
|
|
|
for (unsigned I = 0, E = VBGlobals.VBTables->size(); I != E; ++I) {
|
2014-02-28 03:40:09 +08:00
|
|
|
const VPtrInfo *VBT = (*VBGlobals.VBTables)[I];
|
2014-01-03 08:14:35 +08:00
|
|
|
llvm::GlobalVariable *GV = VBGlobals.Globals[I];
|
2015-02-02 18:22:20 +08:00
|
|
|
if (GV->isDeclaration())
|
|
|
|
emitVBTableDefinition(*VBT, RD, GV);
|
2014-01-03 08:14:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::GlobalVariable *
|
2014-02-28 03:40:09 +08:00
|
|
|
MicrosoftCXXABI::getAddrOfVBTable(const VPtrInfo &VBT, const CXXRecordDecl *RD,
|
2014-01-03 08:14:35 +08:00
|
|
|
llvm::GlobalVariable::LinkageTypes Linkage) {
|
|
|
|
SmallString<256> OutName;
|
|
|
|
llvm::raw_svector_ostream Out(OutName);
|
2014-07-07 16:09:15 +08:00
|
|
|
getMangleContext().mangleCXXVBTable(RD, VBT.MangledPath, Out);
|
2014-01-03 08:14:35 +08:00
|
|
|
Out.flush();
|
|
|
|
StringRef Name = OutName.str();
|
|
|
|
|
|
|
|
llvm::ArrayType *VBTableType =
|
|
|
|
llvm::ArrayType::get(CGM.IntTy, 1 + VBT.ReusingBase->getNumVBases());
|
|
|
|
|
|
|
|
assert(!CGM.getModule().getNamedGlobal(Name) &&
|
|
|
|
"vbtable with this name already exists: mangling bug?");
|
|
|
|
llvm::GlobalVariable *GV =
|
|
|
|
CGM.CreateOrReplaceCXXRuntimeVariable(Name, VBTableType, Linkage);
|
|
|
|
GV->setUnnamedAddr(true);
|
2014-05-31 00:59:42 +08:00
|
|
|
|
|
|
|
if (RD->hasAttr<DLLImportAttr>())
|
|
|
|
GV->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
|
|
|
|
else if (RD->hasAttr<DLLExportAttr>())
|
|
|
|
GV->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass);
|
|
|
|
|
2015-02-02 18:22:20 +08:00
|
|
|
if (!GV->hasExternalLinkage())
|
|
|
|
emitVBTableDefinition(VBT, RD, GV);
|
|
|
|
|
2014-01-03 08:14:35 +08:00
|
|
|
return GV;
|
|
|
|
}
|
|
|
|
|
2014-02-28 03:40:09 +08:00
|
|
|
void MicrosoftCXXABI::emitVBTableDefinition(const VPtrInfo &VBT,
|
2014-01-03 08:14:35 +08:00
|
|
|
const CXXRecordDecl *RD,
|
|
|
|
llvm::GlobalVariable *GV) const {
|
|
|
|
const CXXRecordDecl *ReusingBase = VBT.ReusingBase;
|
|
|
|
|
|
|
|
assert(RD->getNumVBases() && ReusingBase->getNumVBases() &&
|
|
|
|
"should only emit vbtables for classes with vbtables");
|
2013-09-27 22:48:01 +08:00
|
|
|
|
2014-01-03 08:14:35 +08:00
|
|
|
const ASTRecordLayout &BaseLayout =
|
2015-03-15 07:44:48 +08:00
|
|
|
getContext().getASTRecordLayout(VBT.BaseWithVPtr);
|
|
|
|
const ASTRecordLayout &DerivedLayout = getContext().getASTRecordLayout(RD);
|
2014-01-03 08:14:35 +08:00
|
|
|
|
2014-05-21 13:09:00 +08:00
|
|
|
SmallVector<llvm::Constant *, 4> Offsets(1 + ReusingBase->getNumVBases(),
|
|
|
|
nullptr);
|
2014-01-03 08:14:35 +08:00
|
|
|
|
|
|
|
// The offset from ReusingBase's vbptr to itself always leads.
|
|
|
|
CharUnits VBPtrOffset = BaseLayout.getVBPtrOffset();
|
|
|
|
Offsets[0] = llvm::ConstantInt::get(CGM.IntTy, -VBPtrOffset.getQuantity());
|
|
|
|
|
|
|
|
MicrosoftVTableContext &Context = CGM.getMicrosoftVTableContext();
|
2014-03-14 00:15:17 +08:00
|
|
|
for (const auto &I : ReusingBase->vbases()) {
|
|
|
|
const CXXRecordDecl *VBase = I.getType()->getAsCXXRecordDecl();
|
2014-01-03 08:14:35 +08:00
|
|
|
CharUnits Offset = DerivedLayout.getVBaseClassOffset(VBase);
|
|
|
|
assert(!Offset.isNegative());
|
2014-01-04 07:42:00 +08:00
|
|
|
|
2014-01-03 08:14:35 +08:00
|
|
|
// Make it relative to the subobject vbptr.
|
2014-01-04 07:42:00 +08:00
|
|
|
CharUnits CompleteVBPtrOffset = VBT.NonVirtualOffset + VBPtrOffset;
|
2014-02-28 03:40:09 +08:00
|
|
|
if (VBT.getVBaseWithVPtr())
|
2014-01-04 07:42:00 +08:00
|
|
|
CompleteVBPtrOffset +=
|
2014-02-28 03:40:09 +08:00
|
|
|
DerivedLayout.getVBaseClassOffset(VBT.getVBaseWithVPtr());
|
2014-01-04 07:42:00 +08:00
|
|
|
Offset -= CompleteVBPtrOffset;
|
|
|
|
|
2014-01-03 08:14:35 +08:00
|
|
|
unsigned VBIndex = Context.getVBTableIndex(ReusingBase, VBase);
|
2014-05-21 13:09:00 +08:00
|
|
|
assert(Offsets[VBIndex] == nullptr && "The same vbindex seen twice?");
|
2014-01-03 08:14:35 +08:00
|
|
|
Offsets[VBIndex] = llvm::ConstantInt::get(CGM.IntTy, Offset.getQuantity());
|
2013-06-19 23:20:38 +08:00
|
|
|
}
|
2014-01-03 08:14:35 +08:00
|
|
|
|
|
|
|
assert(Offsets.size() ==
|
|
|
|
cast<llvm::ArrayType>(cast<llvm::PointerType>(GV->getType())
|
|
|
|
->getElementType())->getNumElements());
|
|
|
|
llvm::ArrayType *VBTableType =
|
|
|
|
llvm::ArrayType::get(CGM.IntTy, Offsets.size());
|
|
|
|
llvm::Constant *Init = llvm::ConstantArray::get(VBTableType, Offsets);
|
|
|
|
GV->setInitializer(Init);
|
2013-06-19 23:20:38 +08:00
|
|
|
}
|
|
|
|
|
2013-10-30 19:55:43 +08:00
|
|
|
llvm::Value *MicrosoftCXXABI::performThisAdjustment(CodeGenFunction &CGF,
|
|
|
|
llvm::Value *This,
|
|
|
|
const ThisAdjustment &TA) {
|
|
|
|
if (TA.isEmpty())
|
|
|
|
return This;
|
|
|
|
|
|
|
|
llvm::Value *V = CGF.Builder.CreateBitCast(This, CGF.Int8PtrTy);
|
|
|
|
|
2013-11-06 14:24:31 +08:00
|
|
|
if (!TA.Virtual.isEmpty()) {
|
|
|
|
assert(TA.Virtual.Microsoft.VtordispOffset < 0);
|
|
|
|
// Adjust the this argument based on the vtordisp value.
|
|
|
|
llvm::Value *VtorDispPtr =
|
|
|
|
CGF.Builder.CreateConstGEP1_32(V, TA.Virtual.Microsoft.VtordispOffset);
|
|
|
|
VtorDispPtr =
|
|
|
|
CGF.Builder.CreateBitCast(VtorDispPtr, CGF.Int32Ty->getPointerTo());
|
|
|
|
llvm::Value *VtorDisp = CGF.Builder.CreateLoad(VtorDispPtr, "vtordisp");
|
|
|
|
V = CGF.Builder.CreateGEP(V, CGF.Builder.CreateNeg(VtorDisp));
|
|
|
|
|
|
|
|
if (TA.Virtual.Microsoft.VBPtrOffset) {
|
|
|
|
// If the final overrider is defined in a virtual base other than the one
|
|
|
|
// that holds the vfptr, we have to use a vtordispex thunk which looks up
|
|
|
|
// the vbtable of the derived class.
|
|
|
|
assert(TA.Virtual.Microsoft.VBPtrOffset > 0);
|
|
|
|
assert(TA.Virtual.Microsoft.VBOffsetOffset >= 0);
|
|
|
|
llvm::Value *VBPtr;
|
|
|
|
llvm::Value *VBaseOffset =
|
|
|
|
GetVBaseOffsetFromVBPtr(CGF, V, -TA.Virtual.Microsoft.VBPtrOffset,
|
|
|
|
TA.Virtual.Microsoft.VBOffsetOffset, &VBPtr);
|
|
|
|
V = CGF.Builder.CreateInBoundsGEP(VBPtr, VBaseOffset);
|
|
|
|
}
|
|
|
|
}
|
2013-10-30 19:55:43 +08:00
|
|
|
|
|
|
|
if (TA.NonVirtual) {
|
|
|
|
// Non-virtual adjustment might result in a pointer outside the allocated
|
|
|
|
// object, e.g. if the final overrider class is laid out after the virtual
|
|
|
|
// base that declares a method in the most derived class.
|
|
|
|
V = CGF.Builder.CreateConstGEP1_32(V, TA.NonVirtual);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't need to bitcast back, the call CodeGen will handle this.
|
|
|
|
return V;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Value *
|
|
|
|
MicrosoftCXXABI::performReturnAdjustment(CodeGenFunction &CGF, llvm::Value *Ret,
|
|
|
|
const ReturnAdjustment &RA) {
|
|
|
|
if (RA.isEmpty())
|
|
|
|
return Ret;
|
|
|
|
|
|
|
|
llvm::Value *V = CGF.Builder.CreateBitCast(Ret, CGF.Int8PtrTy);
|
|
|
|
|
|
|
|
if (RA.Virtual.Microsoft.VBIndex) {
|
|
|
|
assert(RA.Virtual.Microsoft.VBIndex > 0);
|
2015-03-15 07:44:48 +08:00
|
|
|
const ASTContext &Context = getContext();
|
|
|
|
int32_t IntSize = Context.getTypeSizeInChars(Context.IntTy).getQuantity();
|
2013-10-30 19:55:43 +08:00
|
|
|
llvm::Value *VBPtr;
|
|
|
|
llvm::Value *VBaseOffset =
|
|
|
|
GetVBaseOffsetFromVBPtr(CGF, V, RA.Virtual.Microsoft.VBPtrOffset,
|
|
|
|
IntSize * RA.Virtual.Microsoft.VBIndex, &VBPtr);
|
|
|
|
V = CGF.Builder.CreateInBoundsGEP(VBPtr, VBaseOffset);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (RA.NonVirtual)
|
|
|
|
V = CGF.Builder.CreateConstInBoundsGEP1_32(V, RA.NonVirtual);
|
|
|
|
|
|
|
|
// Cast back to the original type.
|
|
|
|
return CGF.Builder.CreateBitCast(V, Ret->getType());
|
|
|
|
}
|
|
|
|
|
2012-05-01 13:23:51 +08:00
|
|
|
bool MicrosoftCXXABI::requiresArrayCookie(const CXXDeleteExpr *expr,
|
|
|
|
QualType elementType) {
|
|
|
|
// Microsoft seems to completely ignore the possibility of a
|
|
|
|
// two-argument usual deallocation function.
|
|
|
|
return elementType.isDestructedType();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MicrosoftCXXABI::requiresArrayCookie(const CXXNewExpr *expr) {
|
|
|
|
// Microsoft seems to completely ignore the possibility of a
|
|
|
|
// two-argument usual deallocation function.
|
|
|
|
return expr->getAllocatedType().isDestructedType();
|
|
|
|
}
|
|
|
|
|
|
|
|
CharUnits MicrosoftCXXABI::getArrayCookieSizeImpl(QualType type) {
|
|
|
|
// The array cookie is always a size_t; we then pad that out to the
|
|
|
|
// alignment of the element type.
|
|
|
|
ASTContext &Ctx = getContext();
|
|
|
|
return std::max(Ctx.getTypeSizeInChars(Ctx.getSizeType()),
|
|
|
|
Ctx.getTypeAlignInChars(type));
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Value *MicrosoftCXXABI::readArrayCookieImpl(CodeGenFunction &CGF,
|
|
|
|
llvm::Value *allocPtr,
|
|
|
|
CharUnits cookieSize) {
|
2012-10-25 23:39:14 +08:00
|
|
|
unsigned AS = allocPtr->getType()->getPointerAddressSpace();
|
2012-05-01 13:23:51 +08:00
|
|
|
llvm::Value *numElementsPtr =
|
|
|
|
CGF.Builder.CreateBitCast(allocPtr, CGF.SizeTy->getPointerTo(AS));
|
|
|
|
return CGF.Builder.CreateLoad(numElementsPtr);
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Value* MicrosoftCXXABI::InitializeArrayCookie(CodeGenFunction &CGF,
|
|
|
|
llvm::Value *newPtr,
|
|
|
|
llvm::Value *numElements,
|
|
|
|
const CXXNewExpr *expr,
|
|
|
|
QualType elementType) {
|
|
|
|
assert(requiresArrayCookie(expr));
|
|
|
|
|
|
|
|
// The size of the cookie.
|
|
|
|
CharUnits cookieSize = getArrayCookieSizeImpl(elementType);
|
|
|
|
|
|
|
|
// Compute an offset to the cookie.
|
|
|
|
llvm::Value *cookiePtr = newPtr;
|
|
|
|
|
|
|
|
// Write the number of elements into the appropriate slot.
|
2012-10-25 23:39:14 +08:00
|
|
|
unsigned AS = newPtr->getType()->getPointerAddressSpace();
|
2012-05-01 13:23:51 +08:00
|
|
|
llvm::Value *numElementsPtr
|
|
|
|
= CGF.Builder.CreateBitCast(cookiePtr, CGF.SizeTy->getPointerTo(AS));
|
|
|
|
CGF.Builder.CreateStore(numElements, numElementsPtr);
|
|
|
|
|
|
|
|
// Finally, compute a pointer to the actual data buffer by skipping
|
|
|
|
// over the cookie completely.
|
|
|
|
return CGF.Builder.CreateConstInBoundsGEP1_64(newPtr,
|
|
|
|
cookieSize.getQuantity());
|
|
|
|
}
|
|
|
|
|
2014-10-05 13:05:40 +08:00
|
|
|
static void emitGlobalDtorWithTLRegDtor(CodeGenFunction &CGF, const VarDecl &VD,
|
|
|
|
llvm::Constant *Dtor,
|
|
|
|
llvm::Constant *Addr) {
|
|
|
|
// Create a function which calls the destructor.
|
|
|
|
llvm::Constant *DtorStub = CGF.createAtExitStub(VD, Dtor, Addr);
|
|
|
|
|
|
|
|
// extern "C" int __tlregdtor(void (*f)(void));
|
|
|
|
llvm::FunctionType *TLRegDtorTy = llvm::FunctionType::get(
|
|
|
|
CGF.IntTy, DtorStub->getType(), /*IsVarArg=*/false);
|
|
|
|
|
|
|
|
llvm::Constant *TLRegDtor =
|
|
|
|
CGF.CGM.CreateRuntimeFunction(TLRegDtorTy, "__tlregdtor");
|
|
|
|
if (llvm::Function *TLRegDtorFn = dyn_cast<llvm::Function>(TLRegDtor))
|
|
|
|
TLRegDtorFn->setDoesNotThrow();
|
|
|
|
|
|
|
|
CGF.EmitNounwindRuntimeCall(TLRegDtor, DtorStub);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MicrosoftCXXABI::registerGlobalDtor(CodeGenFunction &CGF, const VarDecl &D,
|
|
|
|
llvm::Constant *Dtor,
|
|
|
|
llvm::Constant *Addr) {
|
|
|
|
if (D.getTLSKind())
|
|
|
|
return emitGlobalDtorWithTLRegDtor(CGF, D, Dtor, Addr);
|
|
|
|
|
|
|
|
// The default behavior is to use atexit.
|
|
|
|
CGF.registerGlobalDtorWithAtExit(D, Dtor, Addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MicrosoftCXXABI::EmitThreadLocalInitFuncs(
|
|
|
|
CodeGenModule &CGM,
|
|
|
|
ArrayRef<std::pair<const VarDecl *, llvm::GlobalVariable *>>
|
|
|
|
CXXThreadLocals,
|
|
|
|
ArrayRef<llvm::Function *> CXXThreadLocalInits,
|
|
|
|
ArrayRef<llvm::GlobalVariable *> CXXThreadLocalInitVars) {
|
|
|
|
// This will create a GV in the .CRT$XDU section. It will point to our
|
|
|
|
// initialization function. The CRT will call all of these function
|
|
|
|
// pointers at start-up time and, eventually, at thread-creation time.
|
|
|
|
auto AddToXDU = [&CGM](llvm::Function *InitFunc) {
|
|
|
|
llvm::GlobalVariable *InitFuncPtr = new llvm::GlobalVariable(
|
|
|
|
CGM.getModule(), InitFunc->getType(), /*IsConstant=*/true,
|
|
|
|
llvm::GlobalVariable::InternalLinkage, InitFunc,
|
|
|
|
Twine(InitFunc->getName(), "$initializer$"));
|
|
|
|
InitFuncPtr->setSection(".CRT$XDU");
|
|
|
|
// This variable has discardable linkage, we have to add it to @llvm.used to
|
|
|
|
// ensure it won't get discarded.
|
|
|
|
CGM.addUsedGlobal(InitFuncPtr);
|
|
|
|
return InitFuncPtr;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<llvm::Function *> NonComdatInits;
|
|
|
|
for (size_t I = 0, E = CXXThreadLocalInitVars.size(); I != E; ++I) {
|
|
|
|
llvm::GlobalVariable *GV = CXXThreadLocalInitVars[I];
|
|
|
|
llvm::Function *F = CXXThreadLocalInits[I];
|
|
|
|
|
|
|
|
// If the GV is already in a comdat group, then we have to join it.
|
2015-01-13 06:13:53 +08:00
|
|
|
if (llvm::Comdat *C = GV->getComdat())
|
2014-10-05 13:05:40 +08:00
|
|
|
AddToXDU(F)->setComdat(C);
|
2015-01-13 06:13:53 +08:00
|
|
|
else
|
2014-10-05 13:05:40 +08:00
|
|
|
NonComdatInits.push_back(F);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!NonComdatInits.empty()) {
|
|
|
|
llvm::FunctionType *FTy =
|
|
|
|
llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false);
|
SanitizerBlacklist: blacklist functions by their source location.
This commit changes the way we blacklist functions in ASan, TSan,
MSan and UBSan. We used to treat function as "blacklisted"
and turned off instrumentation in it in two cases:
1) Function is explicitly blacklisted by its mangled name.
This part is not changed.
2) Function is located in llvm::Module, whose identifier is
contained in the list of blacklisted sources. This is completely
wrong, as llvm::Module may not correspond to the actual source
file function is defined in. Also, function can be defined in
a header, in which case user had to blacklist the .cpp file
this header was #include'd into, not the header itself.
Such functions could cause other problems - for instance, if the
header was included in multiple source files, compiled
separately and linked into a single executable, we could end up
with both instrumented and non-instrumented version of the same
function participating in the same link.
After this change we will make blacklisting decision based on
the SourceLocation of a function definition. If a function is
not explicitly defined in the source file, (for example, the
function is compiler-generated and responsible for
initialization/destruction of a global variable), then it will
be blacklisted if the corresponding global variable is defined
in blacklisted source file, and will be instrumented otherwise.
After this commit, the active users of blacklist files may have
to revisit them. This is a backwards-incompatible change, but
I don't think it's possible or makes sense to support the
old incorrect behavior.
I plan to make similar change for blacklisting GlobalVariables
(which is ASan-specific).
llvm-svn: 219997
2014-10-17 08:20:19 +08:00
|
|
|
llvm::Function *InitFunc = CGM.CreateGlobalInitOrDestructFunction(
|
|
|
|
FTy, "__tls_init", SourceLocation(),
|
|
|
|
/*TLS=*/true);
|
2014-10-05 13:05:40 +08:00
|
|
|
CodeGenFunction(CGM).GenerateCXXGlobalInitFunc(InitFunc, NonComdatInits);
|
|
|
|
|
|
|
|
AddToXDU(InitFunc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LValue MicrosoftCXXABI::EmitThreadLocalVarDeclLValue(CodeGenFunction &CGF,
|
|
|
|
const VarDecl *VD,
|
|
|
|
QualType LValType) {
|
|
|
|
CGF.CGM.ErrorUnsupported(VD, "thread wrappers");
|
|
|
|
return LValue();
|
|
|
|
}
|
|
|
|
|
2012-05-01 14:13:13 +08:00
|
|
|
void MicrosoftCXXABI::EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D,
|
2013-09-11 04:14:30 +08:00
|
|
|
llvm::GlobalVariable *GV,
|
2012-05-01 14:13:13 +08:00
|
|
|
bool PerformInit) {
|
2014-05-24 05:13:45 +08:00
|
|
|
// MSVC only uses guards for static locals.
|
|
|
|
if (!D.isStaticLocal()) {
|
|
|
|
assert(GV->hasWeakLinkage() || GV->hasLinkOnceLinkage());
|
|
|
|
// GlobalOpt is allowed to discard the initializer, so use linkonce_odr.
|
2015-01-17 00:04:45 +08:00
|
|
|
llvm::Function *F = CGF.CurFn;
|
|
|
|
F->setLinkage(llvm::GlobalValue::LinkOnceODRLinkage);
|
|
|
|
F->setComdat(CGM.getModule().getOrInsertComdat(F->getName()));
|
2014-05-24 05:13:45 +08:00
|
|
|
CGF.EmitCXXGlobalVarDeclInit(D, GV, PerformInit);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-11 04:14:30 +08:00
|
|
|
// MSVC always uses an i32 bitfield to guard initialization, which is *not*
|
|
|
|
// threadsafe. Since the user may be linking in inline functions compiled by
|
|
|
|
// cl.exe, there's no reason to provide a false sense of security by using
|
|
|
|
// critical sections here.
|
2012-05-01 14:13:13 +08:00
|
|
|
|
2013-04-15 07:01:42 +08:00
|
|
|
if (D.getTLSKind())
|
|
|
|
CGM.ErrorUnsupported(&D, "dynamic TLS initialization");
|
|
|
|
|
2013-09-11 04:14:30 +08:00
|
|
|
CGBuilderTy &Builder = CGF.Builder;
|
|
|
|
llvm::IntegerType *GuardTy = CGF.Int32Ty;
|
|
|
|
llvm::ConstantInt *Zero = llvm::ConstantInt::get(GuardTy, 0);
|
|
|
|
|
|
|
|
// Get the guard variable for this function if we have one already.
|
2014-05-24 05:13:45 +08:00
|
|
|
GuardInfo *GI = &GuardVariableMap[D.getDeclContext()];
|
2013-09-11 04:14:30 +08:00
|
|
|
|
|
|
|
unsigned BitIndex;
|
2014-04-24 02:22:11 +08:00
|
|
|
if (D.isStaticLocal() && D.isExternallyVisible()) {
|
2013-09-11 04:14:30 +08:00
|
|
|
// Externally visible variables have to be numbered in Sema to properly
|
|
|
|
// handle unreachable VarDecls.
|
2014-03-05 16:57:59 +08:00
|
|
|
BitIndex = getContext().getStaticLocalNumber(&D);
|
2013-09-11 04:14:30 +08:00
|
|
|
assert(BitIndex > 0);
|
|
|
|
BitIndex--;
|
|
|
|
} else {
|
|
|
|
// Non-externally visible variables are numbered here in CodeGen.
|
2014-04-24 02:22:11 +08:00
|
|
|
BitIndex = GI->BitIndex++;
|
2013-09-11 04:14:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (BitIndex >= 32) {
|
|
|
|
if (D.isExternallyVisible())
|
|
|
|
ErrorUnsupportedABI(CGF, "more than 32 guarded initializations");
|
|
|
|
BitIndex %= 32;
|
2014-05-21 13:09:00 +08:00
|
|
|
GI->Guard = nullptr;
|
2013-09-11 04:14:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Lazily create the i32 bitfield for this function.
|
2014-04-24 02:22:11 +08:00
|
|
|
if (!GI->Guard) {
|
2013-09-11 04:14:30 +08:00
|
|
|
// Mangle the name for the guard.
|
|
|
|
SmallString<256> GuardName;
|
|
|
|
{
|
|
|
|
llvm::raw_svector_ostream Out(GuardName);
|
|
|
|
getMangleContext().mangleStaticGuardVariable(&D, Out);
|
|
|
|
Out.flush();
|
|
|
|
}
|
|
|
|
|
2014-06-18 23:55:13 +08:00
|
|
|
// Create the guard variable with a zero-initializer. Just absorb linkage,
|
|
|
|
// visibility and dll storage class from the guarded variable.
|
2014-04-24 02:22:11 +08:00
|
|
|
GI->Guard =
|
|
|
|
new llvm::GlobalVariable(CGM.getModule(), GuardTy, false,
|
|
|
|
GV->getLinkage(), Zero, GuardName.str());
|
|
|
|
GI->Guard->setVisibility(GV->getVisibility());
|
2014-06-18 23:55:13 +08:00
|
|
|
GI->Guard->setDLLStorageClass(GV->getDLLStorageClass());
|
2015-01-21 09:04:30 +08:00
|
|
|
if (GI->Guard->isWeakForLinker())
|
|
|
|
GI->Guard->setComdat(
|
|
|
|
CGM.getModule().getOrInsertComdat(GI->Guard->getName()));
|
2013-09-11 04:14:30 +08:00
|
|
|
} else {
|
2014-04-24 02:22:11 +08:00
|
|
|
assert(GI->Guard->getLinkage() == GV->getLinkage() &&
|
2013-09-11 04:14:30 +08:00
|
|
|
"static local from the same function had different linkage");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pseudo code for the test:
|
|
|
|
// if (!(GuardVar & MyGuardBit)) {
|
|
|
|
// GuardVar |= MyGuardBit;
|
|
|
|
// ... initialize the object ...;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// Test our bit from the guard variable.
|
|
|
|
llvm::ConstantInt *Bit = llvm::ConstantInt::get(GuardTy, 1U << BitIndex);
|
2014-04-24 02:22:11 +08:00
|
|
|
llvm::LoadInst *LI = Builder.CreateLoad(GI->Guard);
|
2013-09-11 04:14:30 +08:00
|
|
|
llvm::Value *IsInitialized =
|
|
|
|
Builder.CreateICmpNE(Builder.CreateAnd(LI, Bit), Zero);
|
|
|
|
llvm::BasicBlock *InitBlock = CGF.createBasicBlock("init");
|
|
|
|
llvm::BasicBlock *EndBlock = CGF.createBasicBlock("init.end");
|
|
|
|
Builder.CreateCondBr(IsInitialized, EndBlock, InitBlock);
|
|
|
|
|
|
|
|
// Set our bit in the guard variable and emit the initializer and add a global
|
|
|
|
// destructor if appropriate.
|
|
|
|
CGF.EmitBlock(InitBlock);
|
2014-04-24 02:22:11 +08:00
|
|
|
Builder.CreateStore(Builder.CreateOr(LI, Bit), GI->Guard);
|
2013-09-11 04:14:30 +08:00
|
|
|
CGF.EmitCXXGlobalVarDeclInit(D, GV, PerformInit);
|
|
|
|
Builder.CreateBr(EndBlock);
|
|
|
|
|
|
|
|
// Continue.
|
|
|
|
CGF.EmitBlock(EndBlock);
|
2012-05-01 14:13:13 +08:00
|
|
|
}
|
|
|
|
|
2013-04-12 02:13:19 +08:00
|
|
|
bool MicrosoftCXXABI::isZeroInitializable(const MemberPointerType *MPT) {
|
|
|
|
// Null-ness for function memptrs only depends on the first field, which is
|
|
|
|
// the function pointer. The rest don't matter, so we can zero initialize.
|
|
|
|
if (MPT->isMemberFunctionPointer())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// The virtual base adjustment field is always -1 for null, so if we have one
|
|
|
|
// we can't zero initialize. The field offset is sometimes also -1 if 0 is a
|
|
|
|
// valid field offset.
|
2014-01-17 17:01:00 +08:00
|
|
|
const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl();
|
|
|
|
MSInheritanceAttr::Spelling Inheritance = RD->getMSInheritanceModel();
|
2014-02-06 01:27:08 +08:00
|
|
|
return (!MSInheritanceAttr::hasVBTableOffsetField(Inheritance) &&
|
|
|
|
RD->nullFieldOffsetIsZero());
|
2013-03-23 03:02:54 +08:00
|
|
|
}
|
|
|
|
|
2013-04-12 02:13:19 +08:00
|
|
|
llvm::Type *
|
|
|
|
MicrosoftCXXABI::ConvertMemberPointerType(const MemberPointerType *MPT) {
|
2014-01-17 17:01:00 +08:00
|
|
|
const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl();
|
|
|
|
MSInheritanceAttr::Spelling Inheritance = RD->getMSInheritanceModel();
|
2013-04-12 02:13:19 +08:00
|
|
|
llvm::SmallVector<llvm::Type *, 4> fields;
|
|
|
|
if (MPT->isMemberFunctionPointer())
|
|
|
|
fields.push_back(CGM.VoidPtrTy); // FunctionPointerOrVirtualThunk
|
|
|
|
else
|
|
|
|
fields.push_back(CGM.IntTy); // FieldOffset
|
|
|
|
|
2014-02-06 01:27:08 +08:00
|
|
|
if (MSInheritanceAttr::hasNVOffsetField(MPT->isMemberFunctionPointer(),
|
|
|
|
Inheritance))
|
2013-04-12 02:13:19 +08:00
|
|
|
fields.push_back(CGM.IntTy);
|
2014-02-06 01:27:08 +08:00
|
|
|
if (MSInheritanceAttr::hasVBPtrOffsetField(Inheritance))
|
2013-04-12 02:13:19 +08:00
|
|
|
fields.push_back(CGM.IntTy);
|
2014-02-06 01:27:08 +08:00
|
|
|
if (MSInheritanceAttr::hasVBTableOffsetField(Inheritance))
|
2013-04-12 02:13:19 +08:00
|
|
|
fields.push_back(CGM.IntTy); // VirtualBaseAdjustmentOffset
|
|
|
|
|
|
|
|
if (fields.size() == 1)
|
|
|
|
return fields[0];
|
|
|
|
return llvm::StructType::get(CGM.getLLVMContext(), fields);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MicrosoftCXXABI::
|
|
|
|
GetNullMemberPointerFields(const MemberPointerType *MPT,
|
|
|
|
llvm::SmallVectorImpl<llvm::Constant *> &fields) {
|
|
|
|
assert(fields.empty());
|
2014-01-17 17:01:00 +08:00
|
|
|
const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl();
|
|
|
|
MSInheritanceAttr::Spelling Inheritance = RD->getMSInheritanceModel();
|
2013-04-12 02:13:19 +08:00
|
|
|
if (MPT->isMemberFunctionPointer()) {
|
|
|
|
// FunctionPointerOrVirtualThunk
|
|
|
|
fields.push_back(llvm::Constant::getNullValue(CGM.VoidPtrTy));
|
|
|
|
} else {
|
2014-02-06 01:27:08 +08:00
|
|
|
if (RD->nullFieldOffsetIsZero())
|
2013-04-12 02:13:19 +08:00
|
|
|
fields.push_back(getZeroInt()); // FieldOffset
|
|
|
|
else
|
|
|
|
fields.push_back(getAllOnesInt()); // FieldOffset
|
2013-03-23 03:02:54 +08:00
|
|
|
}
|
2013-04-12 02:13:19 +08:00
|
|
|
|
2014-02-06 01:27:08 +08:00
|
|
|
if (MSInheritanceAttr::hasNVOffsetField(MPT->isMemberFunctionPointer(),
|
|
|
|
Inheritance))
|
2013-04-12 02:13:19 +08:00
|
|
|
fields.push_back(getZeroInt());
|
2014-02-06 01:27:08 +08:00
|
|
|
if (MSInheritanceAttr::hasVBPtrOffsetField(Inheritance))
|
2013-04-12 02:13:19 +08:00
|
|
|
fields.push_back(getZeroInt());
|
2014-02-06 01:27:08 +08:00
|
|
|
if (MSInheritanceAttr::hasVBTableOffsetField(Inheritance))
|
2013-04-12 02:13:19 +08:00
|
|
|
fields.push_back(getAllOnesInt());
|
2013-03-23 03:02:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Constant *
|
|
|
|
MicrosoftCXXABI::EmitNullMemberPointer(const MemberPointerType *MPT) {
|
2013-04-12 02:13:19 +08:00
|
|
|
llvm::SmallVector<llvm::Constant *, 4> fields;
|
|
|
|
GetNullMemberPointerFields(MPT, fields);
|
|
|
|
if (fields.size() == 1)
|
|
|
|
return fields[0];
|
|
|
|
llvm::Constant *Res = llvm::ConstantStruct::getAnon(fields);
|
|
|
|
assert(Res->getType() == ConvertMemberPointerType(MPT));
|
|
|
|
return Res;
|
2013-03-23 03:02:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Constant *
|
2013-05-03 09:15:11 +08:00
|
|
|
MicrosoftCXXABI::EmitFullMemberPointer(llvm::Constant *FirstField,
|
|
|
|
bool IsMemberFunction,
|
2013-05-10 05:01:17 +08:00
|
|
|
const CXXRecordDecl *RD,
|
|
|
|
CharUnits NonVirtualBaseAdjustment)
|
2013-05-03 09:15:11 +08:00
|
|
|
{
|
2014-01-17 17:01:00 +08:00
|
|
|
MSInheritanceAttr::Spelling Inheritance = RD->getMSInheritanceModel();
|
2013-05-03 09:15:11 +08:00
|
|
|
|
|
|
|
// Single inheritance class member pointer are represented as scalars instead
|
|
|
|
// of aggregates.
|
2014-02-06 01:27:08 +08:00
|
|
|
if (MSInheritanceAttr::hasOnlyOneField(IsMemberFunction, Inheritance))
|
2013-05-03 09:15:11 +08:00
|
|
|
return FirstField;
|
|
|
|
|
2013-04-12 02:13:19 +08:00
|
|
|
llvm::SmallVector<llvm::Constant *, 4> fields;
|
2013-05-03 09:15:11 +08:00
|
|
|
fields.push_back(FirstField);
|
|
|
|
|
2014-02-06 01:27:08 +08:00
|
|
|
if (MSInheritanceAttr::hasNVOffsetField(IsMemberFunction, Inheritance))
|
2013-05-10 05:01:17 +08:00
|
|
|
fields.push_back(llvm::ConstantInt::get(
|
|
|
|
CGM.IntTy, NonVirtualBaseAdjustment.getQuantity()));
|
2013-05-03 09:15:11 +08:00
|
|
|
|
2014-02-06 01:27:08 +08:00
|
|
|
if (MSInheritanceAttr::hasVBPtrOffsetField(Inheritance)) {
|
2013-10-15 09:18:02 +08:00
|
|
|
CharUnits Offs = CharUnits::Zero();
|
|
|
|
if (RD->getNumVBases())
|
2014-01-14 08:50:39 +08:00
|
|
|
Offs = getContext().getASTRecordLayout(RD).getVBPtrOffset();
|
2013-10-15 09:18:02 +08:00
|
|
|
fields.push_back(llvm::ConstantInt::get(CGM.IntTy, Offs.getQuantity()));
|
2013-04-12 02:13:19 +08:00
|
|
|
}
|
2013-05-03 09:15:11 +08:00
|
|
|
|
|
|
|
// The rest of the fields are adjusted by conversions to a more derived class.
|
2014-02-06 01:27:08 +08:00
|
|
|
if (MSInheritanceAttr::hasVBTableOffsetField(Inheritance))
|
2013-04-12 02:13:19 +08:00
|
|
|
fields.push_back(getZeroInt());
|
2013-05-03 09:15:11 +08:00
|
|
|
|
2013-04-12 02:13:19 +08:00
|
|
|
return llvm::ConstantStruct::getAnon(fields);
|
2013-03-23 03:02:54 +08:00
|
|
|
}
|
|
|
|
|
2013-05-03 09:15:11 +08:00
|
|
|
llvm::Constant *
|
|
|
|
MicrosoftCXXABI::EmitMemberDataPointer(const MemberPointerType *MPT,
|
|
|
|
CharUnits offset) {
|
2014-01-17 17:01:00 +08:00
|
|
|
const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl();
|
2013-05-03 09:15:11 +08:00
|
|
|
llvm::Constant *FirstField =
|
|
|
|
llvm::ConstantInt::get(CGM.IntTy, offset.getQuantity());
|
2013-05-10 05:01:17 +08:00
|
|
|
return EmitFullMemberPointer(FirstField, /*IsMemberFunction=*/false, RD,
|
|
|
|
CharUnits::Zero());
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Constant *MicrosoftCXXABI::EmitMemberPointer(const CXXMethodDecl *MD) {
|
|
|
|
return BuildMemberPointer(MD->getParent(), MD, CharUnits::Zero());
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Constant *MicrosoftCXXABI::EmitMemberPointer(const APValue &MP,
|
|
|
|
QualType MPType) {
|
|
|
|
const MemberPointerType *MPT = MPType->castAs<MemberPointerType>();
|
|
|
|
const ValueDecl *MPD = MP.getMemberPointerDecl();
|
|
|
|
if (!MPD)
|
|
|
|
return EmitNullMemberPointer(MPT);
|
|
|
|
|
|
|
|
CharUnits ThisAdjustment = getMemberPointerPathAdjustment(MP);
|
|
|
|
|
|
|
|
// FIXME PR15713: Support virtual inheritance paths.
|
|
|
|
|
|
|
|
if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MPD))
|
2014-01-17 17:01:00 +08:00
|
|
|
return BuildMemberPointer(MPT->getMostRecentCXXRecordDecl(), MD,
|
|
|
|
ThisAdjustment);
|
2013-05-10 05:01:17 +08:00
|
|
|
|
|
|
|
CharUnits FieldOffset =
|
|
|
|
getContext().toCharUnitsFromBits(getContext().getFieldOffset(MPD));
|
|
|
|
return EmitMemberDataPointer(MPT, ThisAdjustment + FieldOffset);
|
2013-05-03 09:15:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Constant *
|
2013-05-10 05:01:17 +08:00
|
|
|
MicrosoftCXXABI::BuildMemberPointer(const CXXRecordDecl *RD,
|
|
|
|
const CXXMethodDecl *MD,
|
|
|
|
CharUnits NonVirtualBaseAdjustment) {
|
2013-05-03 09:15:11 +08:00
|
|
|
assert(MD->isInstance() && "Member function must not be static!");
|
|
|
|
MD = MD->getCanonicalDecl();
|
2014-01-17 17:01:00 +08:00
|
|
|
RD = RD->getMostRecentDecl();
|
2013-05-03 09:15:11 +08:00
|
|
|
CodeGenTypes &Types = CGM.getTypes();
|
|
|
|
|
|
|
|
llvm::Constant *FirstField;
|
2014-08-30 05:43:29 +08:00
|
|
|
const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>();
|
2013-11-16 01:24:45 +08:00
|
|
|
if (!MD->isVirtual()) {
|
2013-05-03 09:15:11 +08:00
|
|
|
llvm::Type *Ty;
|
|
|
|
// Check whether the function has a computable LLVM signature.
|
|
|
|
if (Types.isFuncTypeConvertible(FPT)) {
|
|
|
|
// The function has a computable LLVM signature; use the correct type.
|
|
|
|
Ty = Types.GetFunctionType(Types.arrangeCXXMethodDeclaration(MD));
|
|
|
|
} else {
|
|
|
|
// Use an arbitrary non-function type to tell GetAddrOfFunction that the
|
|
|
|
// function type is incomplete.
|
|
|
|
Ty = CGM.PtrDiffTy;
|
|
|
|
}
|
|
|
|
FirstField = CGM.GetAddrOfFunction(MD, Ty);
|
|
|
|
FirstField = llvm::ConstantExpr::getBitCast(FirstField, CGM.VoidPtrTy);
|
2013-11-16 01:24:45 +08:00
|
|
|
} else {
|
|
|
|
MicrosoftVTableContext::MethodVFTableLocation ML =
|
|
|
|
CGM.getMicrosoftVTableContext().getMethodVFTableLocation(MD);
|
2014-08-30 05:43:29 +08:00
|
|
|
if (!CGM.getTypes().isFuncTypeConvertible(
|
|
|
|
MD->getType()->castAs<FunctionType>())) {
|
2013-11-16 01:24:45 +08:00
|
|
|
CGM.ErrorUnsupported(MD, "pointer to virtual member function with "
|
|
|
|
"incomplete return or parameter type");
|
|
|
|
FirstField = llvm::Constant::getNullValue(CGM.VoidPtrTy);
|
2014-08-30 05:43:29 +08:00
|
|
|
} else if (FPT->getCallConv() == CC_X86FastCall) {
|
|
|
|
CGM.ErrorUnsupported(MD, "pointer to fastcall virtual member function");
|
|
|
|
FirstField = llvm::Constant::getNullValue(CGM.VoidPtrTy);
|
2013-11-16 01:24:45 +08:00
|
|
|
} else if (ML.VBase) {
|
|
|
|
CGM.ErrorUnsupported(MD, "pointer to virtual member function overriding "
|
|
|
|
"member function in virtual base class");
|
|
|
|
FirstField = llvm::Constant::getNullValue(CGM.VoidPtrTy);
|
|
|
|
} else {
|
2014-02-21 10:27:32 +08:00
|
|
|
llvm::Function *Thunk = EmitVirtualMemPtrThunk(MD, ML);
|
2013-11-16 01:24:45 +08:00
|
|
|
FirstField = llvm::ConstantExpr::getBitCast(Thunk, CGM.VoidPtrTy);
|
2014-02-21 10:27:32 +08:00
|
|
|
// Include the vfptr adjustment if the method is in a non-primary vftable.
|
|
|
|
NonVirtualBaseAdjustment += ML.VFPtrOffset;
|
2013-11-16 01:24:45 +08:00
|
|
|
}
|
2013-05-03 09:15:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// The rest of the fields are common with data member pointers.
|
2013-05-10 05:01:17 +08:00
|
|
|
return EmitFullMemberPointer(FirstField, /*IsMemberFunction=*/true, RD,
|
|
|
|
NonVirtualBaseAdjustment);
|
2013-05-03 09:15:11 +08:00
|
|
|
}
|
|
|
|
|
2013-05-01 04:15:14 +08:00
|
|
|
/// Member pointers are the same if they're either bitwise identical *or* both
|
|
|
|
/// null. Null-ness for function members is determined by the first field,
|
|
|
|
/// while for data member pointers we must compare all fields.
|
|
|
|
llvm::Value *
|
|
|
|
MicrosoftCXXABI::EmitMemberPointerComparison(CodeGenFunction &CGF,
|
|
|
|
llvm::Value *L,
|
|
|
|
llvm::Value *R,
|
|
|
|
const MemberPointerType *MPT,
|
|
|
|
bool Inequality) {
|
|
|
|
CGBuilderTy &Builder = CGF.Builder;
|
|
|
|
|
|
|
|
// Handle != comparisons by switching the sense of all boolean operations.
|
|
|
|
llvm::ICmpInst::Predicate Eq;
|
|
|
|
llvm::Instruction::BinaryOps And, Or;
|
|
|
|
if (Inequality) {
|
|
|
|
Eq = llvm::ICmpInst::ICMP_NE;
|
|
|
|
And = llvm::Instruction::Or;
|
|
|
|
Or = llvm::Instruction::And;
|
|
|
|
} else {
|
|
|
|
Eq = llvm::ICmpInst::ICMP_EQ;
|
|
|
|
And = llvm::Instruction::And;
|
|
|
|
Or = llvm::Instruction::Or;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this is a single field member pointer (single inheritance), this is a
|
|
|
|
// single icmp.
|
2014-01-17 17:01:00 +08:00
|
|
|
const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl();
|
|
|
|
MSInheritanceAttr::Spelling Inheritance = RD->getMSInheritanceModel();
|
2014-02-06 01:27:08 +08:00
|
|
|
if (MSInheritanceAttr::hasOnlyOneField(MPT->isMemberFunctionPointer(),
|
|
|
|
Inheritance))
|
2013-05-01 04:15:14 +08:00
|
|
|
return Builder.CreateICmp(Eq, L, R);
|
|
|
|
|
|
|
|
// Compare the first field.
|
|
|
|
llvm::Value *L0 = Builder.CreateExtractValue(L, 0, "lhs.0");
|
|
|
|
llvm::Value *R0 = Builder.CreateExtractValue(R, 0, "rhs.0");
|
|
|
|
llvm::Value *Cmp0 = Builder.CreateICmp(Eq, L0, R0, "memptr.cmp.first");
|
|
|
|
|
|
|
|
// Compare everything other than the first field.
|
2014-05-21 13:09:00 +08:00
|
|
|
llvm::Value *Res = nullptr;
|
2013-05-01 04:15:14 +08:00
|
|
|
llvm::StructType *LType = cast<llvm::StructType>(L->getType());
|
|
|
|
for (unsigned I = 1, E = LType->getNumElements(); I != E; ++I) {
|
|
|
|
llvm::Value *LF = Builder.CreateExtractValue(L, I);
|
|
|
|
llvm::Value *RF = Builder.CreateExtractValue(R, I);
|
|
|
|
llvm::Value *Cmp = Builder.CreateICmp(Eq, LF, RF, "memptr.cmp.rest");
|
|
|
|
if (Res)
|
|
|
|
Res = Builder.CreateBinOp(And, Res, Cmp);
|
|
|
|
else
|
|
|
|
Res = Cmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the first field is 0 if this is a function pointer.
|
|
|
|
if (MPT->isMemberFunctionPointer()) {
|
|
|
|
// (l1 == r1 && ...) || l0 == 0
|
|
|
|
llvm::Value *Zero = llvm::Constant::getNullValue(L0->getType());
|
|
|
|
llvm::Value *IsZero = Builder.CreateICmp(Eq, L0, Zero, "memptr.cmp.iszero");
|
|
|
|
Res = Builder.CreateBinOp(Or, Res, IsZero);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Combine the comparison of the first field, which must always be true for
|
|
|
|
// this comparison to succeeed.
|
|
|
|
return Builder.CreateBinOp(And, Res, Cmp0, "memptr.cmp");
|
|
|
|
}
|
|
|
|
|
2013-03-23 03:02:54 +08:00
|
|
|
llvm::Value *
|
|
|
|
MicrosoftCXXABI::EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
|
|
|
|
llvm::Value *MemPtr,
|
|
|
|
const MemberPointerType *MPT) {
|
|
|
|
CGBuilderTy &Builder = CGF.Builder;
|
2013-04-12 02:13:19 +08:00
|
|
|
llvm::SmallVector<llvm::Constant *, 4> fields;
|
|
|
|
// We only need one field for member functions.
|
|
|
|
if (MPT->isMemberFunctionPointer())
|
|
|
|
fields.push_back(llvm::Constant::getNullValue(CGM.VoidPtrTy));
|
|
|
|
else
|
|
|
|
GetNullMemberPointerFields(MPT, fields);
|
|
|
|
assert(!fields.empty());
|
|
|
|
llvm::Value *FirstField = MemPtr;
|
|
|
|
if (MemPtr->getType()->isStructTy())
|
|
|
|
FirstField = Builder.CreateExtractValue(MemPtr, 0);
|
|
|
|
llvm::Value *Res = Builder.CreateICmpNE(FirstField, fields[0], "memptr.cmp0");
|
|
|
|
|
|
|
|
// For function member pointers, we only need to test the function pointer
|
|
|
|
// field. The other fields if any can be garbage.
|
|
|
|
if (MPT->isMemberFunctionPointer())
|
|
|
|
return Res;
|
|
|
|
|
|
|
|
// Otherwise, emit a series of compares and combine the results.
|
|
|
|
for (int I = 1, E = fields.size(); I < E; ++I) {
|
|
|
|
llvm::Value *Field = Builder.CreateExtractValue(MemPtr, I);
|
|
|
|
llvm::Value *Next = Builder.CreateICmpNE(Field, fields[I], "memptr.cmp");
|
2014-05-02 08:05:16 +08:00
|
|
|
Res = Builder.CreateOr(Res, Next, "memptr.tobool");
|
2013-04-12 02:13:19 +08:00
|
|
|
}
|
|
|
|
return Res;
|
|
|
|
}
|
2013-03-23 03:02:54 +08:00
|
|
|
|
2013-05-10 05:01:17 +08:00
|
|
|
bool MicrosoftCXXABI::MemberPointerConstantIsNull(const MemberPointerType *MPT,
|
|
|
|
llvm::Constant *Val) {
|
|
|
|
// Function pointers are null if the pointer in the first field is null.
|
|
|
|
if (MPT->isMemberFunctionPointer()) {
|
|
|
|
llvm::Constant *FirstField = Val->getType()->isStructTy() ?
|
|
|
|
Val->getAggregateElement(0U) : Val;
|
|
|
|
return FirstField->isNullValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
// If it's not a function pointer and it's zero initializable, we can easily
|
|
|
|
// check zero.
|
|
|
|
if (isZeroInitializable(MPT) && Val->isNullValue())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Otherwise, break down all the fields for comparison. Hopefully these
|
|
|
|
// little Constants are reused, while a big null struct might not be.
|
|
|
|
llvm::SmallVector<llvm::Constant *, 4> Fields;
|
|
|
|
GetNullMemberPointerFields(MPT, Fields);
|
|
|
|
if (Fields.size() == 1) {
|
|
|
|
assert(Val->getType()->isIntegerTy());
|
|
|
|
return Val == Fields[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned I, E;
|
|
|
|
for (I = 0, E = Fields.size(); I != E; ++I) {
|
|
|
|
if (Val->getAggregateElement(I) != Fields[I])
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return I == E;
|
|
|
|
}
|
|
|
|
|
2013-05-30 02:02:47 +08:00
|
|
|
llvm::Value *
|
|
|
|
MicrosoftCXXABI::GetVBaseOffsetFromVBPtr(CodeGenFunction &CGF,
|
|
|
|
llvm::Value *This,
|
|
|
|
llvm::Value *VBPtrOffset,
|
2013-10-28 01:10:27 +08:00
|
|
|
llvm::Value *VBTableOffset,
|
2013-05-30 02:02:47 +08:00
|
|
|
llvm::Value **VBPtrOut) {
|
|
|
|
CGBuilderTy &Builder = CGF.Builder;
|
|
|
|
// Load the vbtable pointer from the vbptr in the instance.
|
|
|
|
This = Builder.CreateBitCast(This, CGM.Int8PtrTy);
|
|
|
|
llvm::Value *VBPtr =
|
|
|
|
Builder.CreateInBoundsGEP(This, VBPtrOffset, "vbptr");
|
|
|
|
if (VBPtrOut) *VBPtrOut = VBPtr;
|
2014-10-23 01:26:00 +08:00
|
|
|
VBPtr = Builder.CreateBitCast(VBPtr,
|
|
|
|
CGM.Int32Ty->getPointerTo(0)->getPointerTo(0));
|
2013-05-30 02:02:47 +08:00
|
|
|
llvm::Value *VBTable = Builder.CreateLoad(VBPtr, "vbtable");
|
|
|
|
|
2014-10-23 01:26:00 +08:00
|
|
|
// Translate from byte offset to table index. It improves analyzability.
|
|
|
|
llvm::Value *VBTableIndex = Builder.CreateAShr(
|
|
|
|
VBTableOffset, llvm::ConstantInt::get(VBTableOffset->getType(), 2),
|
|
|
|
"vbtindex", /*isExact=*/true);
|
|
|
|
|
2013-05-30 02:02:47 +08:00
|
|
|
// Load an i32 offset from the vb-table.
|
2014-10-23 01:26:00 +08:00
|
|
|
llvm::Value *VBaseOffs = Builder.CreateInBoundsGEP(VBTable, VBTableIndex);
|
2013-05-30 02:02:47 +08:00
|
|
|
VBaseOffs = Builder.CreateBitCast(VBaseOffs, CGM.Int32Ty->getPointerTo(0));
|
|
|
|
return Builder.CreateLoad(VBaseOffs, "vbase_offs");
|
|
|
|
}
|
|
|
|
|
2013-04-12 02:13:19 +08:00
|
|
|
// Returns an adjusted base cast to i8*, since we do more address arithmetic on
|
|
|
|
// it.
|
2014-02-21 07:22:07 +08:00
|
|
|
llvm::Value *MicrosoftCXXABI::AdjustVirtualBase(
|
|
|
|
CodeGenFunction &CGF, const Expr *E, const CXXRecordDecl *RD,
|
|
|
|
llvm::Value *Base, llvm::Value *VBTableOffset, llvm::Value *VBPtrOffset) {
|
2013-04-12 02:13:19 +08:00
|
|
|
CGBuilderTy &Builder = CGF.Builder;
|
|
|
|
Base = Builder.CreateBitCast(Base, CGM.Int8PtrTy);
|
2014-05-21 13:09:00 +08:00
|
|
|
llvm::BasicBlock *OriginalBB = nullptr;
|
|
|
|
llvm::BasicBlock *SkipAdjustBB = nullptr;
|
|
|
|
llvm::BasicBlock *VBaseAdjustBB = nullptr;
|
2013-04-12 02:13:19 +08:00
|
|
|
|
|
|
|
// In the unspecified inheritance model, there might not be a vbtable at all,
|
|
|
|
// in which case we need to skip the virtual base lookup. If there is a
|
|
|
|
// vbtable, the first entry is a no-op entry that gives back the original
|
|
|
|
// base, so look for a virtual base adjustment offset of zero.
|
|
|
|
if (VBPtrOffset) {
|
|
|
|
OriginalBB = Builder.GetInsertBlock();
|
|
|
|
VBaseAdjustBB = CGF.createBasicBlock("memptr.vadjust");
|
|
|
|
SkipAdjustBB = CGF.createBasicBlock("memptr.skip_vadjust");
|
|
|
|
llvm::Value *IsVirtual =
|
2013-05-30 02:02:47 +08:00
|
|
|
Builder.CreateICmpNE(VBTableOffset, getZeroInt(),
|
2013-04-12 02:13:19 +08:00
|
|
|
"memptr.is_vbase");
|
|
|
|
Builder.CreateCondBr(IsVirtual, VBaseAdjustBB, SkipAdjustBB);
|
|
|
|
CGF.EmitBlock(VBaseAdjustBB);
|
2013-03-23 03:02:54 +08:00
|
|
|
}
|
|
|
|
|
2013-04-12 02:13:19 +08:00
|
|
|
// If we weren't given a dynamic vbptr offset, RD should be complete and we'll
|
|
|
|
// know the vbptr offset.
|
|
|
|
if (!VBPtrOffset) {
|
2013-05-30 02:02:47 +08:00
|
|
|
CharUnits offs = CharUnits::Zero();
|
2014-02-21 07:22:07 +08:00
|
|
|
if (!RD->hasDefinition()) {
|
|
|
|
DiagnosticsEngine &Diags = CGF.CGM.getDiags();
|
|
|
|
unsigned DiagID = Diags.getCustomDiagID(
|
|
|
|
DiagnosticsEngine::Error,
|
|
|
|
"member pointer representation requires a "
|
|
|
|
"complete class type for %0 to perform this expression");
|
|
|
|
Diags.Report(E->getExprLoc(), DiagID) << RD << E->getSourceRange();
|
|
|
|
} else if (RD->getNumVBases())
|
2014-01-14 08:50:39 +08:00
|
|
|
offs = getContext().getASTRecordLayout(RD).getVBPtrOffset();
|
2013-04-12 02:13:19 +08:00
|
|
|
VBPtrOffset = llvm::ConstantInt::get(CGM.IntTy, offs.getQuantity());
|
|
|
|
}
|
2014-05-21 13:09:00 +08:00
|
|
|
llvm::Value *VBPtr = nullptr;
|
2013-04-12 02:13:19 +08:00
|
|
|
llvm::Value *VBaseOffs =
|
2013-10-28 01:10:27 +08:00
|
|
|
GetVBaseOffsetFromVBPtr(CGF, Base, VBPtrOffset, VBTableOffset, &VBPtr);
|
2013-04-12 02:13:19 +08:00
|
|
|
llvm::Value *AdjustedBase = Builder.CreateInBoundsGEP(VBPtr, VBaseOffs);
|
|
|
|
|
|
|
|
// Merge control flow with the case where we didn't have to adjust.
|
|
|
|
if (VBaseAdjustBB) {
|
|
|
|
Builder.CreateBr(SkipAdjustBB);
|
|
|
|
CGF.EmitBlock(SkipAdjustBB);
|
|
|
|
llvm::PHINode *Phi = Builder.CreatePHI(CGM.Int8PtrTy, 2, "memptr.base");
|
|
|
|
Phi->addIncoming(Base, OriginalBB);
|
|
|
|
Phi->addIncoming(AdjustedBase, VBaseAdjustBB);
|
|
|
|
return Phi;
|
|
|
|
}
|
|
|
|
return AdjustedBase;
|
2013-03-23 03:02:54 +08:00
|
|
|
}
|
|
|
|
|
2014-02-21 07:22:07 +08:00
|
|
|
llvm::Value *MicrosoftCXXABI::EmitMemberDataPointerAddress(
|
|
|
|
CodeGenFunction &CGF, const Expr *E, llvm::Value *Base, llvm::Value *MemPtr,
|
|
|
|
const MemberPointerType *MPT) {
|
2013-04-12 02:13:19 +08:00
|
|
|
assert(MPT->isMemberDataPointer());
|
2013-03-23 03:02:54 +08:00
|
|
|
unsigned AS = Base->getType()->getPointerAddressSpace();
|
|
|
|
llvm::Type *PType =
|
|
|
|
CGF.ConvertTypeForMem(MPT->getPointeeType())->getPointerTo(AS);
|
|
|
|
CGBuilderTy &Builder = CGF.Builder;
|
2014-01-17 17:01:00 +08:00
|
|
|
const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl();
|
|
|
|
MSInheritanceAttr::Spelling Inheritance = RD->getMSInheritanceModel();
|
2013-04-12 02:13:19 +08:00
|
|
|
|
|
|
|
// Extract the fields we need, regardless of model. We'll apply them if we
|
|
|
|
// have them.
|
|
|
|
llvm::Value *FieldOffset = MemPtr;
|
2014-05-21 13:09:00 +08:00
|
|
|
llvm::Value *VirtualBaseAdjustmentOffset = nullptr;
|
|
|
|
llvm::Value *VBPtrOffset = nullptr;
|
2013-04-12 02:13:19 +08:00
|
|
|
if (MemPtr->getType()->isStructTy()) {
|
|
|
|
// We need to extract values.
|
|
|
|
unsigned I = 0;
|
|
|
|
FieldOffset = Builder.CreateExtractValue(MemPtr, I++);
|
2014-02-06 01:27:08 +08:00
|
|
|
if (MSInheritanceAttr::hasVBPtrOffsetField(Inheritance))
|
2013-04-12 02:13:19 +08:00
|
|
|
VBPtrOffset = Builder.CreateExtractValue(MemPtr, I++);
|
2014-02-06 01:27:08 +08:00
|
|
|
if (MSInheritanceAttr::hasVBTableOffsetField(Inheritance))
|
2013-04-12 02:13:19 +08:00
|
|
|
VirtualBaseAdjustmentOffset = Builder.CreateExtractValue(MemPtr, I++);
|
2013-03-23 03:02:54 +08:00
|
|
|
}
|
|
|
|
|
2013-04-12 02:13:19 +08:00
|
|
|
if (VirtualBaseAdjustmentOffset) {
|
2014-02-21 07:22:07 +08:00
|
|
|
Base = AdjustVirtualBase(CGF, E, RD, Base, VirtualBaseAdjustmentOffset,
|
2013-04-12 02:13:19 +08:00
|
|
|
VBPtrOffset);
|
2013-03-23 03:02:54 +08:00
|
|
|
}
|
2013-12-06 06:44:07 +08:00
|
|
|
|
|
|
|
// Cast to char*.
|
|
|
|
Base = Builder.CreateBitCast(Base, Builder.getInt8Ty()->getPointerTo(AS));
|
|
|
|
|
|
|
|
// Apply the offset, which we assume is non-null.
|
2013-04-12 02:13:19 +08:00
|
|
|
llvm::Value *Addr =
|
|
|
|
Builder.CreateInBoundsGEP(Base, FieldOffset, "memptr.offset");
|
2013-03-23 03:02:54 +08:00
|
|
|
|
|
|
|
// Cast the address to the appropriate pointer type, adopting the address
|
|
|
|
// space of the base pointer.
|
|
|
|
return Builder.CreateBitCast(Addr, PType);
|
|
|
|
}
|
|
|
|
|
2014-01-17 17:01:00 +08:00
|
|
|
static MSInheritanceAttr::Spelling
|
2013-05-10 05:01:17 +08:00
|
|
|
getInheritanceFromMemptr(const MemberPointerType *MPT) {
|
2014-01-17 17:01:00 +08:00
|
|
|
return MPT->getMostRecentCXXRecordDecl()->getMSInheritanceModel();
|
2013-05-10 05:01:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Value *
|
|
|
|
MicrosoftCXXABI::EmitMemberPointerConversion(CodeGenFunction &CGF,
|
|
|
|
const CastExpr *E,
|
|
|
|
llvm::Value *Src) {
|
|
|
|
assert(E->getCastKind() == CK_DerivedToBaseMemberPointer ||
|
|
|
|
E->getCastKind() == CK_BaseToDerivedMemberPointer ||
|
|
|
|
E->getCastKind() == CK_ReinterpretMemberPointer);
|
|
|
|
|
|
|
|
// Use constant emission if we can.
|
|
|
|
if (isa<llvm::Constant>(Src))
|
|
|
|
return EmitMemberPointerConversion(E, cast<llvm::Constant>(Src));
|
|
|
|
|
|
|
|
// We may be adding or dropping fields from the member pointer, so we need
|
|
|
|
// both types and the inheritance models of both records.
|
|
|
|
const MemberPointerType *SrcTy =
|
|
|
|
E->getSubExpr()->getType()->castAs<MemberPointerType>();
|
|
|
|
const MemberPointerType *DstTy = E->getType()->castAs<MemberPointerType>();
|
|
|
|
bool IsFunc = SrcTy->isMemberFunctionPointer();
|
|
|
|
|
|
|
|
// If the classes use the same null representation, reinterpret_cast is a nop.
|
|
|
|
bool IsReinterpret = E->getCastKind() == CK_ReinterpretMemberPointer;
|
2014-01-17 17:01:00 +08:00
|
|
|
if (IsReinterpret && IsFunc)
|
|
|
|
return Src;
|
|
|
|
|
|
|
|
CXXRecordDecl *SrcRD = SrcTy->getMostRecentCXXRecordDecl();
|
|
|
|
CXXRecordDecl *DstRD = DstTy->getMostRecentCXXRecordDecl();
|
|
|
|
if (IsReinterpret &&
|
2014-02-06 01:27:08 +08:00
|
|
|
SrcRD->nullFieldOffsetIsZero() == DstRD->nullFieldOffsetIsZero())
|
2013-05-10 05:01:17 +08:00
|
|
|
return Src;
|
|
|
|
|
|
|
|
CGBuilderTy &Builder = CGF.Builder;
|
|
|
|
|
|
|
|
// Branch past the conversion if Src is null.
|
|
|
|
llvm::Value *IsNotNull = EmitMemberPointerIsNotNull(CGF, Src, SrcTy);
|
|
|
|
llvm::Constant *DstNull = EmitNullMemberPointer(DstTy);
|
|
|
|
|
|
|
|
// C++ 5.2.10p9: The null member pointer value is converted to the null member
|
|
|
|
// pointer value of the destination type.
|
|
|
|
if (IsReinterpret) {
|
|
|
|
// For reinterpret casts, sema ensures that src and dst are both functions
|
|
|
|
// or data and have the same size, which means the LLVM types should match.
|
|
|
|
assert(Src->getType() == DstNull->getType());
|
|
|
|
return Builder.CreateSelect(IsNotNull, Src, DstNull);
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::BasicBlock *OriginalBB = Builder.GetInsertBlock();
|
|
|
|
llvm::BasicBlock *ConvertBB = CGF.createBasicBlock("memptr.convert");
|
|
|
|
llvm::BasicBlock *ContinueBB = CGF.createBasicBlock("memptr.converted");
|
|
|
|
Builder.CreateCondBr(IsNotNull, ConvertBB, ContinueBB);
|
|
|
|
CGF.EmitBlock(ConvertBB);
|
|
|
|
|
|
|
|
// Decompose src.
|
|
|
|
llvm::Value *FirstField = Src;
|
2014-05-21 13:09:00 +08:00
|
|
|
llvm::Value *NonVirtualBaseAdjustment = nullptr;
|
|
|
|
llvm::Value *VirtualBaseAdjustmentOffset = nullptr;
|
|
|
|
llvm::Value *VBPtrOffset = nullptr;
|
2014-01-17 17:01:00 +08:00
|
|
|
MSInheritanceAttr::Spelling SrcInheritance = SrcRD->getMSInheritanceModel();
|
2014-02-06 01:27:08 +08:00
|
|
|
if (!MSInheritanceAttr::hasOnlyOneField(IsFunc, SrcInheritance)) {
|
2013-05-10 05:01:17 +08:00
|
|
|
// We need to extract values.
|
|
|
|
unsigned I = 0;
|
|
|
|
FirstField = Builder.CreateExtractValue(Src, I++);
|
2014-02-06 01:27:08 +08:00
|
|
|
if (MSInheritanceAttr::hasNVOffsetField(IsFunc, SrcInheritance))
|
2013-05-10 05:01:17 +08:00
|
|
|
NonVirtualBaseAdjustment = Builder.CreateExtractValue(Src, I++);
|
2014-02-06 01:27:08 +08:00
|
|
|
if (MSInheritanceAttr::hasVBPtrOffsetField(SrcInheritance))
|
2013-05-10 05:01:17 +08:00
|
|
|
VBPtrOffset = Builder.CreateExtractValue(Src, I++);
|
2014-02-06 01:27:08 +08:00
|
|
|
if (MSInheritanceAttr::hasVBTableOffsetField(SrcInheritance))
|
2013-05-10 05:01:17 +08:00
|
|
|
VirtualBaseAdjustmentOffset = Builder.CreateExtractValue(Src, I++);
|
|
|
|
}
|
|
|
|
|
|
|
|
// For data pointers, we adjust the field offset directly. For functions, we
|
|
|
|
// have a separate field.
|
|
|
|
llvm::Constant *Adj = getMemberPointerAdjustment(E);
|
|
|
|
if (Adj) {
|
|
|
|
Adj = llvm::ConstantExpr::getTruncOrBitCast(Adj, CGM.IntTy);
|
|
|
|
llvm::Value *&NVAdjustField = IsFunc ? NonVirtualBaseAdjustment : FirstField;
|
|
|
|
bool isDerivedToBase = (E->getCastKind() == CK_DerivedToBaseMemberPointer);
|
|
|
|
if (!NVAdjustField) // If this field didn't exist in src, it's zero.
|
|
|
|
NVAdjustField = getZeroInt();
|
|
|
|
if (isDerivedToBase)
|
|
|
|
NVAdjustField = Builder.CreateNSWSub(NVAdjustField, Adj, "adj");
|
|
|
|
else
|
|
|
|
NVAdjustField = Builder.CreateNSWAdd(NVAdjustField, Adj, "adj");
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME PR15713: Support conversions through virtually derived classes.
|
|
|
|
|
|
|
|
// Recompose dst from the null struct and the adjusted fields from src.
|
2014-01-17 17:01:00 +08:00
|
|
|
MSInheritanceAttr::Spelling DstInheritance = DstRD->getMSInheritanceModel();
|
2013-05-10 05:01:17 +08:00
|
|
|
llvm::Value *Dst;
|
2014-02-06 01:27:08 +08:00
|
|
|
if (MSInheritanceAttr::hasOnlyOneField(IsFunc, DstInheritance)) {
|
2013-05-10 05:01:17 +08:00
|
|
|
Dst = FirstField;
|
|
|
|
} else {
|
|
|
|
Dst = llvm::UndefValue::get(DstNull->getType());
|
|
|
|
unsigned Idx = 0;
|
|
|
|
Dst = Builder.CreateInsertValue(Dst, FirstField, Idx++);
|
2014-02-06 01:27:08 +08:00
|
|
|
if (MSInheritanceAttr::hasNVOffsetField(IsFunc, DstInheritance))
|
2013-05-10 05:01:17 +08:00
|
|
|
Dst = Builder.CreateInsertValue(
|
|
|
|
Dst, getValueOrZeroInt(NonVirtualBaseAdjustment), Idx++);
|
2014-02-06 01:27:08 +08:00
|
|
|
if (MSInheritanceAttr::hasVBPtrOffsetField(DstInheritance))
|
2013-05-10 05:01:17 +08:00
|
|
|
Dst = Builder.CreateInsertValue(
|
|
|
|
Dst, getValueOrZeroInt(VBPtrOffset), Idx++);
|
2014-02-06 01:27:08 +08:00
|
|
|
if (MSInheritanceAttr::hasVBTableOffsetField(DstInheritance))
|
2013-05-10 05:01:17 +08:00
|
|
|
Dst = Builder.CreateInsertValue(
|
|
|
|
Dst, getValueOrZeroInt(VirtualBaseAdjustmentOffset), Idx++);
|
|
|
|
}
|
|
|
|
Builder.CreateBr(ContinueBB);
|
|
|
|
|
|
|
|
// In the continuation, choose between DstNull and Dst.
|
|
|
|
CGF.EmitBlock(ContinueBB);
|
|
|
|
llvm::PHINode *Phi = Builder.CreatePHI(DstNull->getType(), 2, "memptr.converted");
|
|
|
|
Phi->addIncoming(DstNull, OriginalBB);
|
|
|
|
Phi->addIncoming(Dst, ConvertBB);
|
|
|
|
return Phi;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Constant *
|
|
|
|
MicrosoftCXXABI::EmitMemberPointerConversion(const CastExpr *E,
|
|
|
|
llvm::Constant *Src) {
|
|
|
|
const MemberPointerType *SrcTy =
|
|
|
|
E->getSubExpr()->getType()->castAs<MemberPointerType>();
|
|
|
|
const MemberPointerType *DstTy = E->getType()->castAs<MemberPointerType>();
|
|
|
|
|
|
|
|
// If src is null, emit a new null for dst. We can't return src because dst
|
|
|
|
// might have a new representation.
|
|
|
|
if (MemberPointerConstantIsNull(SrcTy, Src))
|
|
|
|
return EmitNullMemberPointer(DstTy);
|
|
|
|
|
|
|
|
// We don't need to do anything for reinterpret_casts of non-null member
|
|
|
|
// pointers. We should only get here when the two type representations have
|
|
|
|
// the same size.
|
|
|
|
if (E->getCastKind() == CK_ReinterpretMemberPointer)
|
|
|
|
return Src;
|
|
|
|
|
2014-01-17 17:01:00 +08:00
|
|
|
MSInheritanceAttr::Spelling SrcInheritance = getInheritanceFromMemptr(SrcTy);
|
|
|
|
MSInheritanceAttr::Spelling DstInheritance = getInheritanceFromMemptr(DstTy);
|
2013-05-10 05:01:17 +08:00
|
|
|
|
|
|
|
// Decompose src.
|
|
|
|
llvm::Constant *FirstField = Src;
|
2014-05-21 13:09:00 +08:00
|
|
|
llvm::Constant *NonVirtualBaseAdjustment = nullptr;
|
|
|
|
llvm::Constant *VirtualBaseAdjustmentOffset = nullptr;
|
|
|
|
llvm::Constant *VBPtrOffset = nullptr;
|
2013-05-10 05:01:17 +08:00
|
|
|
bool IsFunc = SrcTy->isMemberFunctionPointer();
|
2014-02-06 01:27:08 +08:00
|
|
|
if (!MSInheritanceAttr::hasOnlyOneField(IsFunc, SrcInheritance)) {
|
2013-05-10 05:01:17 +08:00
|
|
|
// We need to extract values.
|
|
|
|
unsigned I = 0;
|
|
|
|
FirstField = Src->getAggregateElement(I++);
|
2014-02-06 01:27:08 +08:00
|
|
|
if (MSInheritanceAttr::hasNVOffsetField(IsFunc, SrcInheritance))
|
2013-05-10 05:01:17 +08:00
|
|
|
NonVirtualBaseAdjustment = Src->getAggregateElement(I++);
|
2014-02-06 01:27:08 +08:00
|
|
|
if (MSInheritanceAttr::hasVBPtrOffsetField(SrcInheritance))
|
2013-05-10 05:01:17 +08:00
|
|
|
VBPtrOffset = Src->getAggregateElement(I++);
|
2014-02-06 01:27:08 +08:00
|
|
|
if (MSInheritanceAttr::hasVBTableOffsetField(SrcInheritance))
|
2013-05-10 05:01:17 +08:00
|
|
|
VirtualBaseAdjustmentOffset = Src->getAggregateElement(I++);
|
|
|
|
}
|
|
|
|
|
|
|
|
// For data pointers, we adjust the field offset directly. For functions, we
|
|
|
|
// have a separate field.
|
|
|
|
llvm::Constant *Adj = getMemberPointerAdjustment(E);
|
|
|
|
if (Adj) {
|
|
|
|
Adj = llvm::ConstantExpr::getTruncOrBitCast(Adj, CGM.IntTy);
|
|
|
|
llvm::Constant *&NVAdjustField =
|
|
|
|
IsFunc ? NonVirtualBaseAdjustment : FirstField;
|
|
|
|
bool IsDerivedToBase = (E->getCastKind() == CK_DerivedToBaseMemberPointer);
|
|
|
|
if (!NVAdjustField) // If this field didn't exist in src, it's zero.
|
|
|
|
NVAdjustField = getZeroInt();
|
|
|
|
if (IsDerivedToBase)
|
|
|
|
NVAdjustField = llvm::ConstantExpr::getNSWSub(NVAdjustField, Adj);
|
|
|
|
else
|
|
|
|
NVAdjustField = llvm::ConstantExpr::getNSWAdd(NVAdjustField, Adj);
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME PR15713: Support conversions through virtually derived classes.
|
|
|
|
|
|
|
|
// Recompose dst from the null struct and the adjusted fields from src.
|
2014-02-06 01:27:08 +08:00
|
|
|
if (MSInheritanceAttr::hasOnlyOneField(IsFunc, DstInheritance))
|
2013-05-10 05:01:17 +08:00
|
|
|
return FirstField;
|
|
|
|
|
|
|
|
llvm::SmallVector<llvm::Constant *, 4> Fields;
|
|
|
|
Fields.push_back(FirstField);
|
2014-02-06 01:27:08 +08:00
|
|
|
if (MSInheritanceAttr::hasNVOffsetField(IsFunc, DstInheritance))
|
2013-05-10 05:01:17 +08:00
|
|
|
Fields.push_back(getConstantOrZeroInt(NonVirtualBaseAdjustment));
|
2014-02-06 01:27:08 +08:00
|
|
|
if (MSInheritanceAttr::hasVBPtrOffsetField(DstInheritance))
|
2013-05-10 05:01:17 +08:00
|
|
|
Fields.push_back(getConstantOrZeroInt(VBPtrOffset));
|
2014-02-06 01:27:08 +08:00
|
|
|
if (MSInheritanceAttr::hasVBTableOffsetField(DstInheritance))
|
2013-05-10 05:01:17 +08:00
|
|
|
Fields.push_back(getConstantOrZeroInt(VirtualBaseAdjustmentOffset));
|
|
|
|
return llvm::ConstantStruct::getAnon(Fields);
|
|
|
|
}
|
|
|
|
|
2014-02-21 07:22:07 +08:00
|
|
|
llvm::Value *MicrosoftCXXABI::EmitLoadOfMemberFunctionPointer(
|
|
|
|
CodeGenFunction &CGF, const Expr *E, llvm::Value *&This,
|
|
|
|
llvm::Value *MemPtr, const MemberPointerType *MPT) {
|
2013-04-12 02:13:19 +08:00
|
|
|
assert(MPT->isMemberFunctionPointer());
|
|
|
|
const FunctionProtoType *FPT =
|
|
|
|
MPT->getPointeeType()->castAs<FunctionProtoType>();
|
2014-01-17 17:01:00 +08:00
|
|
|
const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl();
|
2013-04-12 02:13:19 +08:00
|
|
|
llvm::FunctionType *FTy =
|
|
|
|
CGM.getTypes().GetFunctionType(
|
|
|
|
CGM.getTypes().arrangeCXXMethodType(RD, FPT));
|
|
|
|
CGBuilderTy &Builder = CGF.Builder;
|
|
|
|
|
2014-01-17 17:01:00 +08:00
|
|
|
MSInheritanceAttr::Spelling Inheritance = RD->getMSInheritanceModel();
|
2013-04-12 02:13:19 +08:00
|
|
|
|
|
|
|
// Extract the fields we need, regardless of model. We'll apply them if we
|
|
|
|
// have them.
|
|
|
|
llvm::Value *FunctionPointer = MemPtr;
|
2014-05-21 13:09:00 +08:00
|
|
|
llvm::Value *NonVirtualBaseAdjustment = nullptr;
|
|
|
|
llvm::Value *VirtualBaseAdjustmentOffset = nullptr;
|
|
|
|
llvm::Value *VBPtrOffset = nullptr;
|
2013-04-12 02:13:19 +08:00
|
|
|
if (MemPtr->getType()->isStructTy()) {
|
|
|
|
// We need to extract values.
|
|
|
|
unsigned I = 0;
|
|
|
|
FunctionPointer = Builder.CreateExtractValue(MemPtr, I++);
|
2014-02-06 01:27:08 +08:00
|
|
|
if (MSInheritanceAttr::hasNVOffsetField(MPT, Inheritance))
|
2013-04-12 02:13:19 +08:00
|
|
|
NonVirtualBaseAdjustment = Builder.CreateExtractValue(MemPtr, I++);
|
2014-02-06 01:27:08 +08:00
|
|
|
if (MSInheritanceAttr::hasVBPtrOffsetField(Inheritance))
|
2013-05-03 09:15:11 +08:00
|
|
|
VBPtrOffset = Builder.CreateExtractValue(MemPtr, I++);
|
2014-02-06 01:27:08 +08:00
|
|
|
if (MSInheritanceAttr::hasVBTableOffsetField(Inheritance))
|
2013-04-12 02:13:19 +08:00
|
|
|
VirtualBaseAdjustmentOffset = Builder.CreateExtractValue(MemPtr, I++);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (VirtualBaseAdjustmentOffset) {
|
2014-02-21 07:22:07 +08:00
|
|
|
This = AdjustVirtualBase(CGF, E, RD, This, VirtualBaseAdjustmentOffset,
|
2013-04-12 02:13:19 +08:00
|
|
|
VBPtrOffset);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NonVirtualBaseAdjustment) {
|
|
|
|
// Apply the adjustment and cast back to the original struct type.
|
|
|
|
llvm::Value *Ptr = Builder.CreateBitCast(This, Builder.getInt8PtrTy());
|
|
|
|
Ptr = Builder.CreateInBoundsGEP(Ptr, NonVirtualBaseAdjustment);
|
|
|
|
This = Builder.CreateBitCast(Ptr, This->getType(), "this.adjusted");
|
|
|
|
}
|
|
|
|
|
|
|
|
return Builder.CreateBitCast(FunctionPointer, FTy->getPointerTo());
|
|
|
|
}
|
|
|
|
|
2010-08-16 11:33:14 +08:00
|
|
|
CGCXXABI *clang::CodeGen::CreateMicrosoftCXXABI(CodeGenModule &CGM) {
|
2010-06-10 07:25:41 +08:00
|
|
|
return new MicrosoftCXXABI(CGM);
|
|
|
|
}
|
2014-07-07 14:20:47 +08:00
|
|
|
|
|
|
|
// MS RTTI Overview:
|
|
|
|
// The run time type information emitted by cl.exe contains 5 distinct types of
|
|
|
|
// structures. Many of them reference each other.
|
|
|
|
//
|
|
|
|
// TypeInfo: Static classes that are returned by typeid.
|
|
|
|
//
|
|
|
|
// CompleteObjectLocator: Referenced by vftables. They contain information
|
|
|
|
// required for dynamic casting, including OffsetFromTop. They also contain
|
|
|
|
// a reference to the TypeInfo for the type and a reference to the
|
|
|
|
// CompleteHierarchyDescriptor for the type.
|
|
|
|
//
|
|
|
|
// ClassHieararchyDescriptor: Contains information about a class hierarchy.
|
|
|
|
// Used during dynamic_cast to walk a class hierarchy. References a base
|
|
|
|
// class array and the size of said array.
|
|
|
|
//
|
|
|
|
// BaseClassArray: Contains a list of classes in a hierarchy. BaseClassArray is
|
|
|
|
// somewhat of a misnomer because the most derived class is also in the list
|
|
|
|
// as well as multiple copies of virtual bases (if they occur multiple times
|
|
|
|
// in the hiearchy.) The BaseClassArray contains one BaseClassDescriptor for
|
|
|
|
// every path in the hierarchy, in pre-order depth first order. Note, we do
|
|
|
|
// not declare a specific llvm type for BaseClassArray, it's merely an array
|
|
|
|
// of BaseClassDescriptor pointers.
|
|
|
|
//
|
|
|
|
// BaseClassDescriptor: Contains information about a class in a class hierarchy.
|
|
|
|
// BaseClassDescriptor is also somewhat of a misnomer for the same reason that
|
|
|
|
// BaseClassArray is. It contains information about a class within a
|
|
|
|
// hierarchy such as: is this base is ambiguous and what is its offset in the
|
|
|
|
// vbtable. The names of the BaseClassDescriptors have all of their fields
|
|
|
|
// mangled into them so they can be aggressively deduplicated by the linker.
|
|
|
|
|
|
|
|
static llvm::GlobalVariable *getTypeInfoVTable(CodeGenModule &CGM) {
|
|
|
|
StringRef MangledName("\01??_7type_info@@6B@");
|
|
|
|
if (auto VTable = CGM.getModule().getNamedGlobal(MangledName))
|
|
|
|
return VTable;
|
|
|
|
return new llvm::GlobalVariable(CGM.getModule(), CGM.Int8PtrTy,
|
|
|
|
/*Constant=*/true,
|
|
|
|
llvm::GlobalVariable::ExternalLinkage,
|
|
|
|
/*Initializer=*/nullptr, MangledName);
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
/// \brief A Helper struct that stores information about a class in a class
|
|
|
|
/// hierarchy. The information stored in these structs struct is used during
|
|
|
|
/// the generation of ClassHierarchyDescriptors and BaseClassDescriptors.
|
|
|
|
// During RTTI creation, MSRTTIClasses are stored in a contiguous array with
|
|
|
|
// implicit depth first pre-order tree connectivity. getFirstChild and
|
|
|
|
// getNextSibling allow us to walk the tree efficiently.
|
|
|
|
struct MSRTTIClass {
|
|
|
|
enum {
|
|
|
|
IsPrivateOnPath = 1 | 8,
|
|
|
|
IsAmbiguous = 2,
|
|
|
|
IsPrivate = 4,
|
|
|
|
IsVirtual = 16,
|
|
|
|
HasHierarchyDescriptor = 64
|
|
|
|
};
|
|
|
|
MSRTTIClass(const CXXRecordDecl *RD) : RD(RD) {}
|
|
|
|
uint32_t initialize(const MSRTTIClass *Parent,
|
|
|
|
const CXXBaseSpecifier *Specifier);
|
|
|
|
|
|
|
|
MSRTTIClass *getFirstChild() { return this + 1; }
|
|
|
|
static MSRTTIClass *getNextChild(MSRTTIClass *Child) {
|
|
|
|
return Child + 1 + Child->NumBases;
|
|
|
|
}
|
|
|
|
|
|
|
|
const CXXRecordDecl *RD, *VirtualRoot;
|
|
|
|
uint32_t Flags, NumBases, OffsetInVBase;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief Recursively initialize the base class array.
|
|
|
|
uint32_t MSRTTIClass::initialize(const MSRTTIClass *Parent,
|
|
|
|
const CXXBaseSpecifier *Specifier) {
|
|
|
|
Flags = HasHierarchyDescriptor;
|
|
|
|
if (!Parent) {
|
|
|
|
VirtualRoot = nullptr;
|
|
|
|
OffsetInVBase = 0;
|
|
|
|
} else {
|
|
|
|
if (Specifier->getAccessSpecifier() != AS_public)
|
|
|
|
Flags |= IsPrivate | IsPrivateOnPath;
|
|
|
|
if (Specifier->isVirtual()) {
|
|
|
|
Flags |= IsVirtual;
|
|
|
|
VirtualRoot = RD;
|
|
|
|
OffsetInVBase = 0;
|
|
|
|
} else {
|
|
|
|
if (Parent->Flags & IsPrivateOnPath)
|
|
|
|
Flags |= IsPrivateOnPath;
|
|
|
|
VirtualRoot = Parent->VirtualRoot;
|
|
|
|
OffsetInVBase = Parent->OffsetInVBase + RD->getASTContext()
|
|
|
|
.getASTRecordLayout(Parent->RD).getBaseClassOffset(RD).getQuantity();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
NumBases = 0;
|
|
|
|
MSRTTIClass *Child = getFirstChild();
|
|
|
|
for (const CXXBaseSpecifier &Base : RD->bases()) {
|
|
|
|
NumBases += Child->initialize(this, &Base) + 1;
|
|
|
|
Child = getNextChild(Child);
|
|
|
|
}
|
|
|
|
return NumBases;
|
|
|
|
}
|
|
|
|
|
|
|
|
static llvm::GlobalValue::LinkageTypes getLinkageForRTTI(QualType Ty) {
|
|
|
|
switch (Ty->getLinkage()) {
|
|
|
|
case NoLinkage:
|
|
|
|
case InternalLinkage:
|
|
|
|
case UniqueExternalLinkage:
|
|
|
|
return llvm::GlobalValue::InternalLinkage;
|
|
|
|
|
|
|
|
case VisibleNoLinkage:
|
|
|
|
case ExternalLinkage:
|
|
|
|
return llvm::GlobalValue::LinkOnceODRLinkage;
|
|
|
|
}
|
|
|
|
llvm_unreachable("Invalid linkage!");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief An ephemeral helper class for building MS RTTI types. It caches some
|
|
|
|
/// calls to the module and information about the most derived class in a
|
|
|
|
/// hierarchy.
|
|
|
|
struct MSRTTIBuilder {
|
|
|
|
enum {
|
|
|
|
HasBranchingHierarchy = 1,
|
|
|
|
HasVirtualBranchingHierarchy = 2,
|
|
|
|
HasAmbiguousBases = 4
|
|
|
|
};
|
|
|
|
|
2014-07-07 16:09:15 +08:00
|
|
|
MSRTTIBuilder(MicrosoftCXXABI &ABI, const CXXRecordDecl *RD)
|
|
|
|
: CGM(ABI.CGM), Context(CGM.getContext()),
|
|
|
|
VMContext(CGM.getLLVMContext()), Module(CGM.getModule()), RD(RD),
|
2014-07-07 14:20:47 +08:00
|
|
|
Linkage(getLinkageForRTTI(CGM.getContext().getTagDeclType(RD))),
|
2014-07-07 16:09:15 +08:00
|
|
|
ABI(ABI) {}
|
2014-07-07 14:20:47 +08:00
|
|
|
|
|
|
|
llvm::GlobalVariable *getBaseClassDescriptor(const MSRTTIClass &Classes);
|
|
|
|
llvm::GlobalVariable *
|
|
|
|
getBaseClassArray(SmallVectorImpl<MSRTTIClass> &Classes);
|
|
|
|
llvm::GlobalVariable *getClassHierarchyDescriptor();
|
|
|
|
llvm::GlobalVariable *getCompleteObjectLocator(const VPtrInfo *Info);
|
|
|
|
|
|
|
|
CodeGenModule &CGM;
|
|
|
|
ASTContext &Context;
|
|
|
|
llvm::LLVMContext &VMContext;
|
|
|
|
llvm::Module &Module;
|
|
|
|
const CXXRecordDecl *RD;
|
|
|
|
llvm::GlobalVariable::LinkageTypes Linkage;
|
2014-07-07 16:09:15 +08:00
|
|
|
MicrosoftCXXABI &ABI;
|
2014-07-07 14:20:47 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
/// \brief Recursively serializes a class hierarchy in pre-order depth first
|
|
|
|
/// order.
|
|
|
|
static void serializeClassHierarchy(SmallVectorImpl<MSRTTIClass> &Classes,
|
|
|
|
const CXXRecordDecl *RD) {
|
|
|
|
Classes.push_back(MSRTTIClass(RD));
|
|
|
|
for (const CXXBaseSpecifier &Base : RD->bases())
|
|
|
|
serializeClassHierarchy(Classes, Base.getType()->getAsCXXRecordDecl());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Find ambiguity among base classes.
|
|
|
|
static void
|
|
|
|
detectAmbiguousBases(SmallVectorImpl<MSRTTIClass> &Classes) {
|
|
|
|
llvm::SmallPtrSet<const CXXRecordDecl *, 8> VirtualBases;
|
|
|
|
llvm::SmallPtrSet<const CXXRecordDecl *, 8> UniqueBases;
|
|
|
|
llvm::SmallPtrSet<const CXXRecordDecl *, 8> AmbiguousBases;
|
|
|
|
for (MSRTTIClass *Class = &Classes.front(); Class <= &Classes.back();) {
|
|
|
|
if ((Class->Flags & MSRTTIClass::IsVirtual) &&
|
2014-11-19 15:49:47 +08:00
|
|
|
!VirtualBases.insert(Class->RD).second) {
|
2014-07-07 14:20:47 +08:00
|
|
|
Class = MSRTTIClass::getNextChild(Class);
|
|
|
|
continue;
|
|
|
|
}
|
2014-11-19 15:49:47 +08:00
|
|
|
if (!UniqueBases.insert(Class->RD).second)
|
2014-07-07 14:20:47 +08:00
|
|
|
AmbiguousBases.insert(Class->RD);
|
|
|
|
Class++;
|
|
|
|
}
|
|
|
|
if (AmbiguousBases.empty())
|
|
|
|
return;
|
|
|
|
for (MSRTTIClass &Class : Classes)
|
|
|
|
if (AmbiguousBases.count(Class.RD))
|
|
|
|
Class.Flags |= MSRTTIClass::IsAmbiguous;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::GlobalVariable *MSRTTIBuilder::getClassHierarchyDescriptor() {
|
|
|
|
SmallString<256> MangledName;
|
|
|
|
{
|
|
|
|
llvm::raw_svector_ostream Out(MangledName);
|
2014-07-07 16:09:15 +08:00
|
|
|
ABI.getMangleContext().mangleCXXRTTIClassHierarchyDescriptor(RD, Out);
|
2014-07-07 14:20:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check to see if we've already declared this ClassHierarchyDescriptor.
|
|
|
|
if (auto CHD = Module.getNamedGlobal(MangledName))
|
|
|
|
return CHD;
|
|
|
|
|
|
|
|
// Serialize the class hierarchy and initialize the CHD Fields.
|
|
|
|
SmallVector<MSRTTIClass, 8> Classes;
|
|
|
|
serializeClassHierarchy(Classes, RD);
|
|
|
|
Classes.front().initialize(/*Parent=*/nullptr, /*Specifier=*/nullptr);
|
|
|
|
detectAmbiguousBases(Classes);
|
|
|
|
int Flags = 0;
|
|
|
|
for (auto Class : Classes) {
|
|
|
|
if (Class.RD->getNumBases() > 1)
|
|
|
|
Flags |= HasBranchingHierarchy;
|
|
|
|
// Note: cl.exe does not calculate "HasAmbiguousBases" correctly. We
|
|
|
|
// believe the field isn't actually used.
|
|
|
|
if (Class.Flags & MSRTTIClass::IsAmbiguous)
|
|
|
|
Flags |= HasAmbiguousBases;
|
|
|
|
}
|
|
|
|
if ((Flags & HasBranchingHierarchy) && RD->getNumVBases() != 0)
|
|
|
|
Flags |= HasVirtualBranchingHierarchy;
|
|
|
|
// These gep indices are used to get the address of the first element of the
|
|
|
|
// base class array.
|
|
|
|
llvm::Value *GEPIndices[] = {llvm::ConstantInt::get(CGM.IntTy, 0),
|
|
|
|
llvm::ConstantInt::get(CGM.IntTy, 0)};
|
|
|
|
|
|
|
|
// Forward-declare the class hierarchy descriptor
|
2014-07-07 16:09:15 +08:00
|
|
|
auto Type = ABI.getClassHierarchyDescriptorType();
|
2014-07-07 14:20:47 +08:00
|
|
|
auto CHD = new llvm::GlobalVariable(Module, Type, /*Constant=*/true, Linkage,
|
|
|
|
/*Initializer=*/nullptr,
|
2015-03-05 08:46:22 +08:00
|
|
|
StringRef(MangledName));
|
2015-01-17 03:23:42 +08:00
|
|
|
if (CHD->isWeakForLinker())
|
|
|
|
CHD->setComdat(CGM.getModule().getOrInsertComdat(CHD->getName()));
|
2014-07-07 14:20:47 +08:00
|
|
|
|
|
|
|
// Initialize the base class ClassHierarchyDescriptor.
|
|
|
|
llvm::Constant *Fields[] = {
|
2014-07-07 16:09:15 +08:00
|
|
|
llvm::ConstantInt::get(CGM.IntTy, 0), // Unknown
|
|
|
|
llvm::ConstantInt::get(CGM.IntTy, Flags),
|
|
|
|
llvm::ConstantInt::get(CGM.IntTy, Classes.size()),
|
|
|
|
ABI.getImageRelativeConstant(llvm::ConstantExpr::getInBoundsGetElementPtr(
|
|
|
|
getBaseClassArray(Classes),
|
|
|
|
llvm::ArrayRef<llvm::Value *>(GEPIndices))),
|
2014-07-07 14:20:47 +08:00
|
|
|
};
|
|
|
|
CHD->setInitializer(llvm::ConstantStruct::get(Type, Fields));
|
|
|
|
return CHD;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::GlobalVariable *
|
|
|
|
MSRTTIBuilder::getBaseClassArray(SmallVectorImpl<MSRTTIClass> &Classes) {
|
|
|
|
SmallString<256> MangledName;
|
|
|
|
{
|
|
|
|
llvm::raw_svector_ostream Out(MangledName);
|
2014-07-07 16:09:15 +08:00
|
|
|
ABI.getMangleContext().mangleCXXRTTIBaseClassArray(RD, Out);
|
2014-07-07 14:20:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Forward-declare the base class array.
|
|
|
|
// cl.exe pads the base class array with 1 (in 32 bit mode) or 4 (in 64 bit
|
|
|
|
// mode) bytes of padding. We provide a pointer sized amount of padding by
|
|
|
|
// adding +1 to Classes.size(). The sections have pointer alignment and are
|
|
|
|
// marked pick-any so it shouldn't matter.
|
2014-07-07 23:29:10 +08:00
|
|
|
llvm::Type *PtrType = ABI.getImageRelativeType(
|
2014-07-07 16:09:15 +08:00
|
|
|
ABI.getBaseClassDescriptorType()->getPointerTo());
|
2014-07-07 23:29:10 +08:00
|
|
|
auto *ArrType = llvm::ArrayType::get(PtrType, Classes.size() + 1);
|
2015-03-05 08:46:22 +08:00
|
|
|
auto *BCA =
|
|
|
|
new llvm::GlobalVariable(Module, ArrType,
|
|
|
|
/*Constant=*/true, Linkage,
|
|
|
|
/*Initializer=*/nullptr, StringRef(MangledName));
|
2015-01-17 03:23:42 +08:00
|
|
|
if (BCA->isWeakForLinker())
|
|
|
|
BCA->setComdat(CGM.getModule().getOrInsertComdat(BCA->getName()));
|
2014-07-07 14:20:47 +08:00
|
|
|
|
|
|
|
// Initialize the BaseClassArray.
|
|
|
|
SmallVector<llvm::Constant *, 8> BaseClassArrayData;
|
|
|
|
for (MSRTTIClass &Class : Classes)
|
|
|
|
BaseClassArrayData.push_back(
|
2014-07-07 16:09:15 +08:00
|
|
|
ABI.getImageRelativeConstant(getBaseClassDescriptor(Class)));
|
2014-07-07 14:20:47 +08:00
|
|
|
BaseClassArrayData.push_back(llvm::Constant::getNullValue(PtrType));
|
2014-07-07 23:29:10 +08:00
|
|
|
BCA->setInitializer(llvm::ConstantArray::get(ArrType, BaseClassArrayData));
|
2014-07-07 14:20:47 +08:00
|
|
|
return BCA;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::GlobalVariable *
|
|
|
|
MSRTTIBuilder::getBaseClassDescriptor(const MSRTTIClass &Class) {
|
|
|
|
// Compute the fields for the BaseClassDescriptor. They are computed up front
|
|
|
|
// because they are mangled into the name of the object.
|
|
|
|
uint32_t OffsetInVBTable = 0;
|
|
|
|
int32_t VBPtrOffset = -1;
|
|
|
|
if (Class.VirtualRoot) {
|
|
|
|
auto &VTableContext = CGM.getMicrosoftVTableContext();
|
|
|
|
OffsetInVBTable = VTableContext.getVBTableIndex(RD, Class.VirtualRoot) * 4;
|
|
|
|
VBPtrOffset = Context.getASTRecordLayout(RD).getVBPtrOffset().getQuantity();
|
|
|
|
}
|
|
|
|
|
|
|
|
SmallString<256> MangledName;
|
|
|
|
{
|
|
|
|
llvm::raw_svector_ostream Out(MangledName);
|
2014-07-07 16:09:15 +08:00
|
|
|
ABI.getMangleContext().mangleCXXRTTIBaseClassDescriptor(
|
|
|
|
Class.RD, Class.OffsetInVBase, VBPtrOffset, OffsetInVBTable,
|
|
|
|
Class.Flags, Out);
|
2014-07-07 14:20:47 +08:00
|
|
|
}
|
|
|
|
|
2014-07-07 23:29:10 +08:00
|
|
|
// Check to see if we've already declared this object.
|
2014-07-07 14:20:47 +08:00
|
|
|
if (auto BCD = Module.getNamedGlobal(MangledName))
|
|
|
|
return BCD;
|
|
|
|
|
|
|
|
// Forward-declare the base class descriptor.
|
2014-07-07 16:09:15 +08:00
|
|
|
auto Type = ABI.getBaseClassDescriptorType();
|
2015-03-05 08:46:22 +08:00
|
|
|
auto BCD =
|
|
|
|
new llvm::GlobalVariable(Module, Type, /*Constant=*/true, Linkage,
|
|
|
|
/*Initializer=*/nullptr, StringRef(MangledName));
|
2015-01-17 03:23:42 +08:00
|
|
|
if (BCD->isWeakForLinker())
|
|
|
|
BCD->setComdat(CGM.getModule().getOrInsertComdat(BCD->getName()));
|
2014-07-07 14:20:47 +08:00
|
|
|
|
|
|
|
// Initialize the BaseClassDescriptor.
|
|
|
|
llvm::Constant *Fields[] = {
|
2014-07-07 16:09:15 +08:00
|
|
|
ABI.getImageRelativeConstant(
|
2015-03-18 04:35:00 +08:00
|
|
|
ABI.getAddrOfRTTIDescriptor(Context.getTypeDeclType(Class.RD))),
|
2014-07-07 16:09:15 +08:00
|
|
|
llvm::ConstantInt::get(CGM.IntTy, Class.NumBases),
|
|
|
|
llvm::ConstantInt::get(CGM.IntTy, Class.OffsetInVBase),
|
|
|
|
llvm::ConstantInt::get(CGM.IntTy, VBPtrOffset),
|
|
|
|
llvm::ConstantInt::get(CGM.IntTy, OffsetInVBTable),
|
|
|
|
llvm::ConstantInt::get(CGM.IntTy, Class.Flags),
|
|
|
|
ABI.getImageRelativeConstant(
|
|
|
|
MSRTTIBuilder(ABI, Class.RD).getClassHierarchyDescriptor()),
|
2014-07-07 14:20:47 +08:00
|
|
|
};
|
|
|
|
BCD->setInitializer(llvm::ConstantStruct::get(Type, Fields));
|
|
|
|
return BCD;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::GlobalVariable *
|
|
|
|
MSRTTIBuilder::getCompleteObjectLocator(const VPtrInfo *Info) {
|
|
|
|
SmallString<256> MangledName;
|
|
|
|
{
|
|
|
|
llvm::raw_svector_ostream Out(MangledName);
|
2014-07-07 16:09:15 +08:00
|
|
|
ABI.getMangleContext().mangleCXXRTTICompleteObjectLocator(RD, Info->MangledPath, Out);
|
2014-07-07 14:20:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check to see if we've already computed this complete object locator.
|
|
|
|
if (auto COL = Module.getNamedGlobal(MangledName))
|
|
|
|
return COL;
|
|
|
|
|
|
|
|
// Compute the fields of the complete object locator.
|
|
|
|
int OffsetToTop = Info->FullOffsetInMDC.getQuantity();
|
|
|
|
int VFPtrOffset = 0;
|
|
|
|
// The offset includes the vtordisp if one exists.
|
|
|
|
if (const CXXRecordDecl *VBase = Info->getVBaseWithVPtr())
|
|
|
|
if (Context.getASTRecordLayout(RD)
|
|
|
|
.getVBaseOffsetsMap()
|
|
|
|
.find(VBase)
|
|
|
|
->second.hasVtorDisp())
|
|
|
|
VFPtrOffset = Info->NonVirtualOffset.getQuantity() + 4;
|
|
|
|
|
|
|
|
// Forward-declare the complete object locator.
|
2014-07-07 16:09:15 +08:00
|
|
|
llvm::StructType *Type = ABI.getCompleteObjectLocatorType();
|
2014-07-07 14:20:47 +08:00
|
|
|
auto COL = new llvm::GlobalVariable(Module, Type, /*Constant=*/true, Linkage,
|
2015-03-05 08:46:22 +08:00
|
|
|
/*Initializer=*/nullptr, StringRef(MangledName));
|
2014-07-07 14:20:47 +08:00
|
|
|
|
|
|
|
// Initialize the CompleteObjectLocator.
|
|
|
|
llvm::Constant *Fields[] = {
|
2014-07-07 16:09:15 +08:00
|
|
|
llvm::ConstantInt::get(CGM.IntTy, ABI.isImageRelative()),
|
|
|
|
llvm::ConstantInt::get(CGM.IntTy, OffsetToTop),
|
|
|
|
llvm::ConstantInt::get(CGM.IntTy, VFPtrOffset),
|
|
|
|
ABI.getImageRelativeConstant(
|
|
|
|
CGM.GetAddrOfRTTIDescriptor(Context.getTypeDeclType(RD))),
|
|
|
|
ABI.getImageRelativeConstant(getClassHierarchyDescriptor()),
|
|
|
|
ABI.getImageRelativeConstant(COL),
|
2014-07-07 14:20:47 +08:00
|
|
|
};
|
|
|
|
llvm::ArrayRef<llvm::Constant *> FieldsRef(Fields);
|
2014-07-07 16:09:15 +08:00
|
|
|
if (!ABI.isImageRelative())
|
|
|
|
FieldsRef = FieldsRef.drop_back();
|
2014-07-07 14:20:47 +08:00
|
|
|
COL->setInitializer(llvm::ConstantStruct::get(Type, FieldsRef));
|
2015-01-17 03:23:42 +08:00
|
|
|
if (COL->isWeakForLinker())
|
|
|
|
COL->setComdat(CGM.getModule().getOrInsertComdat(COL->getName()));
|
2014-07-07 14:20:47 +08:00
|
|
|
return COL;
|
|
|
|
}
|
|
|
|
|
2015-03-15 15:10:01 +08:00
|
|
|
static QualType decomposeTypeForEH(ASTContext &Context, QualType T,
|
|
|
|
bool &IsConst, bool &IsVolatile) {
|
|
|
|
T = Context.getExceptionObjectType(T);
|
|
|
|
|
|
|
|
// C++14 [except.handle]p3:
|
|
|
|
// A handler is a match for an exception object of type E if [...]
|
|
|
|
// - the handler is of type cv T or const T& where T is a pointer type and
|
|
|
|
// E is a pointer type that can be converted to T by [...]
|
|
|
|
// - a qualification conversion
|
|
|
|
IsConst = false;
|
|
|
|
IsVolatile = false;
|
|
|
|
QualType PointeeType = T->getPointeeType();
|
|
|
|
if (!PointeeType.isNull()) {
|
|
|
|
IsConst = PointeeType.isConstQualified();
|
|
|
|
IsVolatile = PointeeType.isVolatileQualified();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Member pointer types like "const int A::*" are represented by having RTTI
|
|
|
|
// for "int A::*" and separately storing the const qualifier.
|
|
|
|
if (const auto *MPTy = T->getAs<MemberPointerType>())
|
|
|
|
T = Context.getMemberPointerType(PointeeType.getUnqualifiedType(),
|
|
|
|
MPTy->getClass());
|
|
|
|
|
|
|
|
// Pointer types like "const int * const *" are represented by having RTTI
|
|
|
|
// for "const int **" and separately storing the const qualifier.
|
|
|
|
if (T->isPointerType())
|
|
|
|
T = Context.getPointerType(PointeeType.getUnqualifiedType());
|
|
|
|
|
|
|
|
return T;
|
|
|
|
}
|
|
|
|
|
2015-03-18 04:35:05 +08:00
|
|
|
llvm::Constant *
|
2015-03-30 05:55:10 +08:00
|
|
|
MicrosoftCXXABI::getAddrOfCXXCatchHandlerType(QualType Type,
|
|
|
|
QualType CatchHandlerType) {
|
2015-03-18 04:35:05 +08:00
|
|
|
// TypeDescriptors for exceptions never have qualified pointer types,
|
2015-03-15 15:10:01 +08:00
|
|
|
// qualifiers are stored seperately in order to support qualification
|
|
|
|
// conversions.
|
2015-03-18 04:35:00 +08:00
|
|
|
bool IsConst, IsVolatile;
|
|
|
|
Type = decomposeTypeForEH(getContext(), Type, IsConst, IsVolatile);
|
2015-03-15 15:10:01 +08:00
|
|
|
|
2015-03-18 04:35:05 +08:00
|
|
|
bool IsReference = CatchHandlerType->isReferenceType();
|
|
|
|
|
|
|
|
uint32_t Flags = 0;
|
|
|
|
if (IsConst)
|
|
|
|
Flags |= 1;
|
|
|
|
if (IsVolatile)
|
|
|
|
Flags |= 2;
|
|
|
|
if (IsReference)
|
|
|
|
Flags |= 8;
|
|
|
|
|
2015-03-30 05:55:10 +08:00
|
|
|
SmallString<256> MangledName;
|
|
|
|
{
|
|
|
|
llvm::raw_svector_ostream Out(MangledName);
|
|
|
|
getMangleContext().mangleCXXCatchHandlerType(Type, Flags, Out);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(MangledName))
|
|
|
|
return llvm::ConstantExpr::getBitCast(GV, CGM.Int8PtrTy);
|
|
|
|
|
2015-03-18 04:35:05 +08:00
|
|
|
llvm::Constant *Fields[] = {
|
2015-03-30 05:55:10 +08:00
|
|
|
llvm::ConstantInt::get(CGM.IntTy, Flags), // Flags
|
|
|
|
getAddrOfRTTIDescriptor(Type), // TypeDescriptor
|
2015-03-18 04:35:05 +08:00
|
|
|
};
|
2015-03-30 05:55:10 +08:00
|
|
|
llvm::StructType *CatchHandlerTypeType = getCatchHandlerTypeType();
|
2015-03-18 04:35:05 +08:00
|
|
|
auto *Var = new llvm::GlobalVariable(
|
2015-03-30 05:55:10 +08:00
|
|
|
CGM.getModule(), CatchHandlerTypeType, /*Constant=*/true,
|
2015-03-18 04:35:05 +08:00
|
|
|
llvm::GlobalValue::PrivateLinkage,
|
2015-03-30 05:55:10 +08:00
|
|
|
llvm::ConstantStruct::get(CatchHandlerTypeType, Fields),
|
2015-03-18 04:35:05 +08:00
|
|
|
StringRef(MangledName));
|
|
|
|
Var->setUnnamedAddr(true);
|
|
|
|
Var->setSection("llvm.metadata");
|
|
|
|
return Var;
|
2015-03-18 04:35:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Gets a TypeDescriptor. Returns a llvm::Constant * rather than a
|
|
|
|
/// llvm::GlobalVariable * because different type descriptors have different
|
|
|
|
/// types, and need to be abstracted. They are abstracting by casting the
|
|
|
|
/// address to an Int8PtrTy.
|
|
|
|
llvm::Constant *MicrosoftCXXABI::getAddrOfRTTIDescriptor(QualType Type) {
|
2015-03-18 04:35:05 +08:00
|
|
|
SmallString<256> MangledName;
|
2014-07-07 14:20:47 +08:00
|
|
|
{
|
|
|
|
llvm::raw_svector_ostream Out(MangledName);
|
2014-07-07 16:09:15 +08:00
|
|
|
getMangleContext().mangleCXXRTTI(Type, Out);
|
2014-07-07 14:20:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check to see if we've already declared this TypeDescriptor.
|
|
|
|
if (llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(MangledName))
|
|
|
|
return llvm::ConstantExpr::getBitCast(GV, CGM.Int8PtrTy);
|
|
|
|
|
|
|
|
// Compute the fields for the TypeDescriptor.
|
2015-03-18 04:35:05 +08:00
|
|
|
SmallString<256> TypeInfoString;
|
2014-07-07 14:20:47 +08:00
|
|
|
{
|
|
|
|
llvm::raw_svector_ostream Out(TypeInfoString);
|
2014-07-07 16:09:15 +08:00
|
|
|
getMangleContext().mangleCXXRTTIName(Type, Out);
|
2014-07-07 14:20:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Declare and initialize the TypeDescriptor.
|
|
|
|
llvm::Constant *Fields[] = {
|
|
|
|
getTypeInfoVTable(CGM), // VFPtr
|
|
|
|
llvm::ConstantPointerNull::get(CGM.Int8PtrTy), // Runtime data
|
|
|
|
llvm::ConstantDataArray::getString(CGM.getLLVMContext(), TypeInfoString)};
|
|
|
|
llvm::StructType *TypeDescriptorType =
|
2014-07-07 16:09:15 +08:00
|
|
|
getTypeDescriptorType(TypeInfoString);
|
2015-01-17 03:23:42 +08:00
|
|
|
auto *Var = new llvm::GlobalVariable(
|
|
|
|
CGM.getModule(), TypeDescriptorType, /*Constant=*/false,
|
|
|
|
getLinkageForRTTI(Type),
|
|
|
|
llvm::ConstantStruct::get(TypeDescriptorType, Fields),
|
2015-03-05 08:46:22 +08:00
|
|
|
StringRef(MangledName));
|
2015-01-17 03:23:42 +08:00
|
|
|
if (Var->isWeakForLinker())
|
|
|
|
Var->setComdat(CGM.getModule().getOrInsertComdat(Var->getName()));
|
|
|
|
return llvm::ConstantExpr::getBitCast(Var, CGM.Int8PtrTy);
|
2014-07-07 14:20:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Gets or a creates a Microsoft CompleteObjectLocator.
|
|
|
|
llvm::GlobalVariable *
|
|
|
|
MicrosoftCXXABI::getMSCompleteObjectLocator(const CXXRecordDecl *RD,
|
|
|
|
const VPtrInfo *Info) {
|
2014-07-07 16:09:15 +08:00
|
|
|
return MSRTTIBuilder(*this, RD).getCompleteObjectLocator(Info);
|
2014-07-07 14:20:47 +08:00
|
|
|
}
|
2014-09-16 03:20:10 +08:00
|
|
|
|
|
|
|
static void emitCXXConstructor(CodeGenModule &CGM,
|
|
|
|
const CXXConstructorDecl *ctor,
|
|
|
|
StructorType ctorType) {
|
2014-09-16 03:24:44 +08:00
|
|
|
// There are no constructor variants, always emit the complete destructor.
|
2015-01-16 23:37:11 +08:00
|
|
|
llvm::Function *Fn = CGM.codegenCXXStructor(ctor, StructorType::Complete);
|
|
|
|
CGM.maybeSetTrivialComdat(*ctor, *Fn);
|
2014-09-16 03:20:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void emitCXXDestructor(CodeGenModule &CGM, const CXXDestructorDecl *dtor,
|
|
|
|
StructorType dtorType) {
|
|
|
|
// The complete destructor is equivalent to the base destructor for
|
|
|
|
// classes with no virtual bases, so try to emit it as an alias.
|
|
|
|
if (!dtor->getParent()->getNumVBases() &&
|
|
|
|
(dtorType == StructorType::Complete || dtorType == StructorType::Base)) {
|
|
|
|
bool ProducedAlias = !CGM.TryEmitDefinitionAsAlias(
|
|
|
|
GlobalDecl(dtor, Dtor_Complete), GlobalDecl(dtor, Dtor_Base), true);
|
|
|
|
if (ProducedAlias) {
|
|
|
|
if (dtorType == StructorType::Complete)
|
|
|
|
return;
|
|
|
|
if (dtor->isVirtual())
|
|
|
|
CGM.getVTables().EmitThunks(GlobalDecl(dtor, Dtor_Complete));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The base destructor is equivalent to the base destructor of its
|
|
|
|
// base class if there is exactly one non-virtual base class with a
|
|
|
|
// non-trivial destructor, there are no fields with a non-trivial
|
|
|
|
// destructor, and the body of the destructor is trivial.
|
|
|
|
if (dtorType == StructorType::Base && !CGM.TryEmitBaseDestructorAsAlias(dtor))
|
|
|
|
return;
|
|
|
|
|
2015-01-16 23:37:11 +08:00
|
|
|
llvm::Function *Fn = CGM.codegenCXXStructor(dtor, dtorType);
|
2015-01-17 09:47:39 +08:00
|
|
|
if (Fn->isWeakForLinker())
|
|
|
|
Fn->setComdat(CGM.getModule().getOrInsertComdat(Fn->getName()));
|
2014-09-16 03:20:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MicrosoftCXXABI::emitCXXStructor(const CXXMethodDecl *MD,
|
|
|
|
StructorType Type) {
|
|
|
|
if (auto *CD = dyn_cast<CXXConstructorDecl>(MD)) {
|
|
|
|
emitCXXConstructor(CGM, CD, Type);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
emitCXXDestructor(CGM, cast<CXXDestructorDecl>(MD), Type);
|
|
|
|
}
|
2015-03-05 08:46:22 +08:00
|
|
|
|
2015-03-12 02:36:39 +08:00
|
|
|
llvm::Function *
|
2015-03-14 06:36:55 +08:00
|
|
|
MicrosoftCXXABI::getAddrOfCXXCtorClosure(const CXXConstructorDecl *CD,
|
|
|
|
CXXCtorType CT) {
|
|
|
|
assert(CT == Ctor_CopyingClosure || CT == Ctor_DefaultClosure);
|
|
|
|
|
2015-03-12 02:36:39 +08:00
|
|
|
// Calculate the mangled name.
|
|
|
|
SmallString<256> ThunkName;
|
|
|
|
llvm::raw_svector_ostream Out(ThunkName);
|
2015-03-14 06:36:55 +08:00
|
|
|
getMangleContext().mangleCXXCtor(CD, CT, Out);
|
2015-03-12 02:36:39 +08:00
|
|
|
Out.flush();
|
|
|
|
|
|
|
|
// If the thunk has been generated previously, just return it.
|
|
|
|
if (llvm::GlobalValue *GV = CGM.getModule().getNamedValue(ThunkName))
|
|
|
|
return cast<llvm::Function>(GV);
|
|
|
|
|
|
|
|
// Create the llvm::Function.
|
2015-03-14 06:36:55 +08:00
|
|
|
const CGFunctionInfo &FnInfo = CGM.getTypes().arrangeMSCtorClosure(CD, CT);
|
2015-03-12 02:36:39 +08:00
|
|
|
llvm::FunctionType *ThunkTy = CGM.getTypes().GetFunctionType(FnInfo);
|
|
|
|
const CXXRecordDecl *RD = CD->getParent();
|
|
|
|
QualType RecordTy = getContext().getRecordType(RD);
|
|
|
|
llvm::Function *ThunkFn = llvm::Function::Create(
|
|
|
|
ThunkTy, getLinkageForRTTI(RecordTy), ThunkName.str(), &CGM.getModule());
|
2015-03-18 03:00:50 +08:00
|
|
|
ThunkFn->setCallingConv(static_cast<llvm::CallingConv::ID>(
|
|
|
|
FnInfo.getEffectiveCallingConvention()));
|
2015-03-14 06:36:55 +08:00
|
|
|
bool IsCopy = CT == Ctor_CopyingClosure;
|
2015-03-12 02:36:39 +08:00
|
|
|
|
|
|
|
// Start codegen.
|
|
|
|
CodeGenFunction CGF(CGM);
|
|
|
|
CGF.CurGD = GlobalDecl(CD, Ctor_Complete);
|
|
|
|
|
|
|
|
// Build FunctionArgs.
|
|
|
|
FunctionArgList FunctionArgs;
|
|
|
|
|
2015-03-14 06:36:55 +08:00
|
|
|
// A constructor always starts with a 'this' pointer as its first argument.
|
2015-03-12 02:36:39 +08:00
|
|
|
buildThisParam(CGF, FunctionArgs);
|
|
|
|
|
|
|
|
// Following the 'this' pointer is a reference to the source object that we
|
|
|
|
// are copying from.
|
|
|
|
ImplicitParamDecl SrcParam(
|
|
|
|
getContext(), nullptr, SourceLocation(), &getContext().Idents.get("src"),
|
|
|
|
getContext().getLValueReferenceType(RecordTy,
|
|
|
|
/*SpelledAsLValue=*/true));
|
2015-03-14 06:36:55 +08:00
|
|
|
if (IsCopy)
|
|
|
|
FunctionArgs.push_back(&SrcParam);
|
2015-03-12 02:36:39 +08:00
|
|
|
|
2015-03-14 06:36:55 +08:00
|
|
|
// Constructors for classes which utilize virtual bases have an additional
|
|
|
|
// parameter which indicates whether or not it is being delegated to by a more
|
|
|
|
// derived constructor.
|
2015-03-12 02:36:39 +08:00
|
|
|
ImplicitParamDecl IsMostDerived(getContext(), nullptr, SourceLocation(),
|
|
|
|
&getContext().Idents.get("is_most_derived"),
|
|
|
|
getContext().IntTy);
|
|
|
|
// Only add the parameter to the list if thie class has virtual bases.
|
|
|
|
if (RD->getNumVBases() > 0)
|
|
|
|
FunctionArgs.push_back(&IsMostDerived);
|
|
|
|
|
|
|
|
// Start defining the function.
|
|
|
|
CGF.StartFunction(GlobalDecl(), FnInfo.getReturnType(), ThunkFn, FnInfo,
|
|
|
|
FunctionArgs, CD->getLocation(), SourceLocation());
|
|
|
|
EmitThisParam(CGF);
|
|
|
|
llvm::Value *This = getThisValue(CGF);
|
|
|
|
|
|
|
|
llvm::Value *SrcVal =
|
2015-03-14 06:36:55 +08:00
|
|
|
IsCopy ? CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(&SrcParam), "src")
|
|
|
|
: nullptr;
|
2015-03-12 02:36:39 +08:00
|
|
|
|
|
|
|
CallArgList Args;
|
|
|
|
|
|
|
|
// Push the this ptr.
|
|
|
|
Args.add(RValue::get(This), CD->getThisType(getContext()));
|
|
|
|
|
|
|
|
// Push the src ptr.
|
2015-03-14 06:36:55 +08:00
|
|
|
if (SrcVal)
|
|
|
|
Args.add(RValue::get(SrcVal), SrcParam.getType());
|
2015-03-12 02:36:39 +08:00
|
|
|
|
|
|
|
// Add the rest of the default arguments.
|
|
|
|
std::vector<Stmt *> ArgVec;
|
2015-03-18 05:51:43 +08:00
|
|
|
for (unsigned I = IsCopy ? 1 : 0, E = CD->getNumParams(); I != E; ++I) {
|
|
|
|
Stmt *DefaultArg = getContext().getDefaultArgExprForConstructor(CD, I);
|
|
|
|
assert(DefaultArg && "sema forgot to instantiate default args");
|
|
|
|
ArgVec.push_back(DefaultArg);
|
|
|
|
}
|
2015-03-12 02:36:39 +08:00
|
|
|
|
|
|
|
CodeGenFunction::RunCleanupsScope Cleanups(CGF);
|
|
|
|
|
|
|
|
const auto *FPT = CD->getType()->castAs<FunctionProtoType>();
|
2015-03-12 21:49:45 +08:00
|
|
|
ConstExprIterator ArgBegin(ArgVec.data()),
|
|
|
|
ArgEnd(ArgVec.data() + ArgVec.size());
|
2015-03-14 06:36:55 +08:00
|
|
|
CGF.EmitCallArgs(Args, FPT, ArgBegin, ArgEnd, CD, IsCopy ? 1 : 0);
|
2015-03-12 02:36:39 +08:00
|
|
|
|
|
|
|
// Insert any ABI-specific implicit constructor arguments.
|
|
|
|
unsigned ExtraArgs = addImplicitConstructorArgs(CGF, CD, Ctor_Complete,
|
|
|
|
/*ForVirtualBase=*/false,
|
|
|
|
/*Delegating=*/false, Args);
|
|
|
|
|
|
|
|
// Call the destructor with our arguments.
|
|
|
|
llvm::Value *CalleeFn = CGM.getAddrOfCXXStructor(CD, StructorType::Complete);
|
|
|
|
const CGFunctionInfo &CalleeInfo = CGM.getTypes().arrangeCXXConstructorCall(
|
|
|
|
Args, CD, Ctor_Complete, ExtraArgs);
|
|
|
|
CGF.EmitCall(CalleeInfo, CalleeFn, ReturnValueSlot(), Args, CD);
|
|
|
|
|
|
|
|
Cleanups.ForceCleanup();
|
|
|
|
|
|
|
|
// Emit the ret instruction, remove any temporary instructions created for the
|
|
|
|
// aid of CodeGen.
|
|
|
|
CGF.FinishFunction(SourceLocation());
|
|
|
|
|
|
|
|
return ThunkFn;
|
|
|
|
}
|
|
|
|
|
2015-03-05 08:46:22 +08:00
|
|
|
llvm::Constant *MicrosoftCXXABI::getCatchableType(QualType T,
|
|
|
|
uint32_t NVOffset,
|
|
|
|
int32_t VBPtrOffset,
|
|
|
|
uint32_t VBIndex) {
|
|
|
|
assert(!T->isReferenceType());
|
|
|
|
|
2015-03-07 02:53:55 +08:00
|
|
|
CXXRecordDecl *RD = T->getAsCXXRecordDecl();
|
|
|
|
const CXXConstructorDecl *CD =
|
|
|
|
RD ? CGM.getContext().getCopyConstructorForExceptionObject(RD) : nullptr;
|
2015-03-12 02:36:39 +08:00
|
|
|
CXXCtorType CT = Ctor_Complete;
|
2015-03-14 06:36:55 +08:00
|
|
|
if (CD)
|
|
|
|
if (!hasDefaultCXXMethodCC(getContext(), CD) || CD->getNumParams() != 1)
|
2015-03-12 02:36:39 +08:00
|
|
|
CT = Ctor_CopyingClosure;
|
|
|
|
|
2015-03-05 08:46:22 +08:00
|
|
|
uint32_t Size = getContext().getTypeSizeInChars(T).getQuantity();
|
|
|
|
SmallString<256> MangledName;
|
|
|
|
{
|
|
|
|
llvm::raw_svector_ostream Out(MangledName);
|
2015-03-12 02:36:39 +08:00
|
|
|
getMangleContext().mangleCXXCatchableType(T, CD, CT, Size, NVOffset,
|
2015-03-11 03:01:51 +08:00
|
|
|
VBPtrOffset, VBIndex, Out);
|
2015-03-05 08:46:22 +08:00
|
|
|
}
|
|
|
|
if (llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(MangledName))
|
|
|
|
return getImageRelativeConstant(GV);
|
|
|
|
|
2015-03-12 02:36:39 +08:00
|
|
|
// The TypeDescriptor is used by the runtime to determine if a catch handler
|
2015-03-05 08:46:22 +08:00
|
|
|
// is appropriate for the exception object.
|
2015-03-18 04:35:00 +08:00
|
|
|
llvm::Constant *TD = getImageRelativeConstant(getAddrOfRTTIDescriptor(T));
|
2015-03-05 08:46:22 +08:00
|
|
|
|
|
|
|
// The runtime is responsible for calling the copy constructor if the
|
|
|
|
// exception is caught by value.
|
2015-03-12 02:36:39 +08:00
|
|
|
llvm::Constant *CopyCtor;
|
|
|
|
if (CD) {
|
|
|
|
if (CT == Ctor_CopyingClosure)
|
2015-03-14 06:36:55 +08:00
|
|
|
CopyCtor = getAddrOfCXXCtorClosure(CD, Ctor_CopyingClosure);
|
2015-03-12 02:36:39 +08:00
|
|
|
else
|
|
|
|
CopyCtor = CGM.getAddrOfCXXStructor(CD, StructorType::Complete);
|
|
|
|
|
|
|
|
CopyCtor = llvm::ConstantExpr::getBitCast(CopyCtor, CGM.Int8PtrTy);
|
|
|
|
} else {
|
|
|
|
CopyCtor = llvm::Constant::getNullValue(CGM.Int8PtrTy);
|
|
|
|
}
|
2015-03-07 02:53:55 +08:00
|
|
|
CopyCtor = getImageRelativeConstant(CopyCtor);
|
2015-03-05 08:46:22 +08:00
|
|
|
|
2015-03-07 02:53:55 +08:00
|
|
|
bool IsScalar = !RD;
|
2015-03-05 08:46:22 +08:00
|
|
|
bool HasVirtualBases = false;
|
|
|
|
bool IsStdBadAlloc = false; // std::bad_alloc is special for some reason.
|
|
|
|
QualType PointeeType = T;
|
|
|
|
if (T->isPointerType())
|
|
|
|
PointeeType = T->getPointeeType();
|
|
|
|
if (const CXXRecordDecl *RD = PointeeType->getAsCXXRecordDecl()) {
|
|
|
|
HasVirtualBases = RD->getNumVBases() > 0;
|
|
|
|
if (IdentifierInfo *II = RD->getIdentifier())
|
|
|
|
IsStdBadAlloc = II->isStr("bad_alloc") && RD->isInStdNamespace();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encode the relevant CatchableType properties into the Flags bitfield.
|
|
|
|
// FIXME: Figure out how bits 2 or 8 can get set.
|
|
|
|
uint32_t Flags = 0;
|
|
|
|
if (IsScalar)
|
|
|
|
Flags |= 1;
|
|
|
|
if (HasVirtualBases)
|
|
|
|
Flags |= 4;
|
|
|
|
if (IsStdBadAlloc)
|
|
|
|
Flags |= 16;
|
|
|
|
|
|
|
|
llvm::Constant *Fields[] = {
|
|
|
|
llvm::ConstantInt::get(CGM.IntTy, Flags), // Flags
|
|
|
|
TD, // TypeDescriptor
|
|
|
|
llvm::ConstantInt::get(CGM.IntTy, NVOffset), // NonVirtualAdjustment
|
|
|
|
llvm::ConstantInt::get(CGM.IntTy, VBPtrOffset), // OffsetToVBPtr
|
|
|
|
llvm::ConstantInt::get(CGM.IntTy, VBIndex), // VBTableIndex
|
|
|
|
llvm::ConstantInt::get(CGM.IntTy, Size), // Size
|
|
|
|
CopyCtor // CopyCtor
|
|
|
|
};
|
|
|
|
llvm::StructType *CTType = getCatchableTypeType();
|
|
|
|
auto *GV = new llvm::GlobalVariable(
|
|
|
|
CGM.getModule(), CTType, /*Constant=*/true, getLinkageForRTTI(T),
|
|
|
|
llvm::ConstantStruct::get(CTType, Fields), StringRef(MangledName));
|
2015-03-07 07:45:23 +08:00
|
|
|
GV->setUnnamedAddr(true);
|
|
|
|
GV->setSection(".xdata");
|
2015-03-05 08:46:22 +08:00
|
|
|
if (GV->isWeakForLinker())
|
|
|
|
GV->setComdat(CGM.getModule().getOrInsertComdat(GV->getName()));
|
|
|
|
return getImageRelativeConstant(GV);
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::GlobalVariable *MicrosoftCXXABI::getCatchableTypeArray(QualType T) {
|
|
|
|
assert(!T->isReferenceType());
|
|
|
|
|
|
|
|
// See if we've already generated a CatchableTypeArray for this type before.
|
|
|
|
llvm::GlobalVariable *&CTA = CatchableTypeArrays[T];
|
|
|
|
if (CTA)
|
|
|
|
return CTA;
|
|
|
|
|
|
|
|
// Ensure that we don't have duplicate entries in our CatchableTypeArray by
|
|
|
|
// using a SmallSetVector. Duplicates may arise due to virtual bases
|
|
|
|
// occurring more than once in the hierarchy.
|
|
|
|
llvm::SmallSetVector<llvm::Constant *, 2> CatchableTypes;
|
|
|
|
|
|
|
|
// C++14 [except.handle]p3:
|
|
|
|
// A handler is a match for an exception object of type E if [...]
|
|
|
|
// - the handler is of type cv T or cv T& and T is an unambiguous public
|
|
|
|
// base class of E, or
|
|
|
|
// - the handler is of type cv T or const T& where T is a pointer type and
|
|
|
|
// E is a pointer type that can be converted to T by [...]
|
|
|
|
// - a standard pointer conversion (4.10) not involving conversions to
|
|
|
|
// pointers to private or protected or ambiguous classes
|
|
|
|
const CXXRecordDecl *MostDerivedClass = nullptr;
|
|
|
|
bool IsPointer = T->isPointerType();
|
|
|
|
if (IsPointer)
|
|
|
|
MostDerivedClass = T->getPointeeType()->getAsCXXRecordDecl();
|
|
|
|
else
|
|
|
|
MostDerivedClass = T->getAsCXXRecordDecl();
|
|
|
|
|
|
|
|
// Collect all the unambiguous public bases of the MostDerivedClass.
|
|
|
|
if (MostDerivedClass) {
|
2015-03-15 07:44:48 +08:00
|
|
|
const ASTContext &Context = getContext();
|
2015-03-05 08:46:22 +08:00
|
|
|
const ASTRecordLayout &MostDerivedLayout =
|
|
|
|
Context.getASTRecordLayout(MostDerivedClass);
|
|
|
|
MicrosoftVTableContext &VTableContext = CGM.getMicrosoftVTableContext();
|
|
|
|
SmallVector<MSRTTIClass, 8> Classes;
|
|
|
|
serializeClassHierarchy(Classes, MostDerivedClass);
|
|
|
|
Classes.front().initialize(/*Parent=*/nullptr, /*Specifier=*/nullptr);
|
|
|
|
detectAmbiguousBases(Classes);
|
|
|
|
for (const MSRTTIClass &Class : Classes) {
|
|
|
|
// Skip any ambiguous or private bases.
|
|
|
|
if (Class.Flags &
|
|
|
|
(MSRTTIClass::IsPrivateOnPath | MSRTTIClass::IsAmbiguous))
|
|
|
|
continue;
|
|
|
|
// Write down how to convert from a derived pointer to a base pointer.
|
|
|
|
uint32_t OffsetInVBTable = 0;
|
|
|
|
int32_t VBPtrOffset = -1;
|
|
|
|
if (Class.VirtualRoot) {
|
|
|
|
OffsetInVBTable =
|
|
|
|
VTableContext.getVBTableIndex(MostDerivedClass, Class.VirtualRoot)*4;
|
|
|
|
VBPtrOffset = MostDerivedLayout.getVBPtrOffset().getQuantity();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Turn our record back into a pointer if the exception object is a
|
|
|
|
// pointer.
|
|
|
|
QualType RTTITy = QualType(Class.RD->getTypeForDecl(), 0);
|
|
|
|
if (IsPointer)
|
|
|
|
RTTITy = Context.getPointerType(RTTITy);
|
|
|
|
CatchableTypes.insert(getCatchableType(RTTITy, Class.OffsetInVBase,
|
|
|
|
VBPtrOffset, OffsetInVBTable));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// C++14 [except.handle]p3:
|
|
|
|
// A handler is a match for an exception object of type E if
|
|
|
|
// - The handler is of type cv T or cv T& and E and T are the same type
|
|
|
|
// (ignoring the top-level cv-qualifiers)
|
|
|
|
CatchableTypes.insert(getCatchableType(T));
|
|
|
|
|
|
|
|
// C++14 [except.handle]p3:
|
|
|
|
// A handler is a match for an exception object of type E if
|
|
|
|
// - the handler is of type cv T or const T& where T is a pointer type and
|
|
|
|
// E is a pointer type that can be converted to T by [...]
|
|
|
|
// - a standard pointer conversion (4.10) not involving conversions to
|
|
|
|
// pointers to private or protected or ambiguous classes
|
|
|
|
//
|
2015-03-07 07:45:20 +08:00
|
|
|
// All pointers are convertible to pointer-to-void so ensure that it is in the
|
2015-03-05 08:46:22 +08:00
|
|
|
// CatchableTypeArray.
|
|
|
|
if (IsPointer)
|
|
|
|
CatchableTypes.insert(getCatchableType(getContext().VoidPtrTy));
|
|
|
|
|
2015-03-13 01:44:49 +08:00
|
|
|
// C++14 [except.handle]p3:
|
|
|
|
// A handler is a match for an exception object of type E if [...]
|
|
|
|
// - the handler is of type cv T or const T& where T is a pointer or
|
|
|
|
// pointer to member type and E is std::nullptr_t.
|
|
|
|
//
|
|
|
|
// We cannot possibly list all possible pointer types here, making this
|
|
|
|
// implementation incompatible with the standard. However, MSVC includes an
|
|
|
|
// entry for pointer-to-void in this case. Let's do the same.
|
|
|
|
if (T->isNullPtrType())
|
|
|
|
CatchableTypes.insert(getCatchableType(getContext().VoidPtrTy));
|
|
|
|
|
2015-03-05 08:46:22 +08:00
|
|
|
uint32_t NumEntries = CatchableTypes.size();
|
|
|
|
llvm::Type *CTType =
|
|
|
|
getImageRelativeType(getCatchableTypeType()->getPointerTo());
|
|
|
|
llvm::ArrayType *AT = llvm::ArrayType::get(CTType, NumEntries);
|
|
|
|
llvm::StructType *CTAType = getCatchableTypeArrayType(NumEntries);
|
|
|
|
llvm::Constant *Fields[] = {
|
|
|
|
llvm::ConstantInt::get(CGM.IntTy, NumEntries), // NumEntries
|
|
|
|
llvm::ConstantArray::get(
|
|
|
|
AT, llvm::makeArrayRef(CatchableTypes.begin(),
|
|
|
|
CatchableTypes.end())) // CatchableTypes
|
|
|
|
};
|
|
|
|
SmallString<256> MangledName;
|
|
|
|
{
|
|
|
|
llvm::raw_svector_ostream Out(MangledName);
|
|
|
|
getMangleContext().mangleCXXCatchableTypeArray(T, NumEntries, Out);
|
|
|
|
}
|
|
|
|
CTA = new llvm::GlobalVariable(
|
|
|
|
CGM.getModule(), CTAType, /*Constant=*/true, getLinkageForRTTI(T),
|
|
|
|
llvm::ConstantStruct::get(CTAType, Fields), StringRef(MangledName));
|
2015-03-07 07:45:23 +08:00
|
|
|
CTA->setUnnamedAddr(true);
|
|
|
|
CTA->setSection(".xdata");
|
2015-03-05 08:46:22 +08:00
|
|
|
if (CTA->isWeakForLinker())
|
|
|
|
CTA->setComdat(CGM.getModule().getOrInsertComdat(CTA->getName()));
|
|
|
|
return CTA;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::GlobalVariable *MicrosoftCXXABI::getThrowInfo(QualType T) {
|
2015-03-15 15:10:01 +08:00
|
|
|
bool IsConst, IsVolatile;
|
|
|
|
T = decomposeTypeForEH(getContext(), T, IsConst, IsVolatile);
|
2015-03-05 08:46:22 +08:00
|
|
|
|
|
|
|
// The CatchableTypeArray enumerates the various (CV-unqualified) types that
|
|
|
|
// the exception object may be caught as.
|
|
|
|
llvm::GlobalVariable *CTA = getCatchableTypeArray(T);
|
|
|
|
// The first field in a CatchableTypeArray is the number of CatchableTypes.
|
|
|
|
// This is used as a component of the mangled name which means that we need to
|
|
|
|
// know what it is in order to see if we have previously generated the
|
|
|
|
// ThrowInfo.
|
|
|
|
uint32_t NumEntries =
|
|
|
|
cast<llvm::ConstantInt>(CTA->getInitializer()->getAggregateElement(0U))
|
|
|
|
->getLimitedValue();
|
|
|
|
|
|
|
|
SmallString<256> MangledName;
|
|
|
|
{
|
|
|
|
llvm::raw_svector_ostream Out(MangledName);
|
|
|
|
getMangleContext().mangleCXXThrowInfo(T, IsConst, IsVolatile, NumEntries,
|
|
|
|
Out);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reuse a previously generated ThrowInfo if we have generated an appropriate
|
|
|
|
// one before.
|
|
|
|
if (llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(MangledName))
|
|
|
|
return GV;
|
|
|
|
|
|
|
|
// The RTTI TypeDescriptor uses an unqualified type but catch clauses must
|
|
|
|
// be at least as CV qualified. Encode this requirement into the Flags
|
|
|
|
// bitfield.
|
|
|
|
uint32_t Flags = 0;
|
|
|
|
if (IsConst)
|
|
|
|
Flags |= 1;
|
|
|
|
if (IsVolatile)
|
|
|
|
Flags |= 2;
|
|
|
|
|
|
|
|
// The cleanup-function (a destructor) must be called when the exception
|
|
|
|
// object's lifetime ends.
|
|
|
|
llvm::Constant *CleanupFn = llvm::Constant::getNullValue(CGM.Int8PtrTy);
|
|
|
|
if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl())
|
|
|
|
if (CXXDestructorDecl *DtorD = RD->getDestructor())
|
|
|
|
if (!DtorD->isTrivial())
|
|
|
|
CleanupFn = llvm::ConstantExpr::getBitCast(
|
|
|
|
CGM.getAddrOfCXXStructor(DtorD, StructorType::Complete),
|
|
|
|
CGM.Int8PtrTy);
|
|
|
|
// This is unused as far as we can tell, initialize it to null.
|
|
|
|
llvm::Constant *ForwardCompat =
|
|
|
|
getImageRelativeConstant(llvm::Constant::getNullValue(CGM.Int8PtrTy));
|
|
|
|
llvm::Constant *PointerToCatchableTypes = getImageRelativeConstant(
|
|
|
|
llvm::ConstantExpr::getBitCast(CTA, CGM.Int8PtrTy));
|
|
|
|
llvm::StructType *TIType = getThrowInfoType();
|
|
|
|
llvm::Constant *Fields[] = {
|
|
|
|
llvm::ConstantInt::get(CGM.IntTy, Flags), // Flags
|
|
|
|
getImageRelativeConstant(CleanupFn), // CleanupFn
|
|
|
|
ForwardCompat, // ForwardCompat
|
|
|
|
PointerToCatchableTypes // CatchableTypeArray
|
|
|
|
};
|
|
|
|
auto *GV = new llvm::GlobalVariable(
|
|
|
|
CGM.getModule(), TIType, /*Constant=*/true, getLinkageForRTTI(T),
|
|
|
|
llvm::ConstantStruct::get(TIType, Fields), StringRef(MangledName));
|
2015-03-07 07:45:23 +08:00
|
|
|
GV->setUnnamedAddr(true);
|
|
|
|
GV->setSection(".xdata");
|
2015-03-05 08:46:22 +08:00
|
|
|
if (GV->isWeakForLinker())
|
|
|
|
GV->setComdat(CGM.getModule().getOrInsertComdat(GV->getName()));
|
|
|
|
return GV;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MicrosoftCXXABI::emitThrow(CodeGenFunction &CGF, const CXXThrowExpr *E) {
|
|
|
|
const Expr *SubExpr = E->getSubExpr();
|
|
|
|
QualType ThrowType = SubExpr->getType();
|
|
|
|
// The exception object lives on the stack and it's address is passed to the
|
|
|
|
// runtime function.
|
|
|
|
llvm::AllocaInst *AI = CGF.CreateMemTemp(ThrowType);
|
|
|
|
CGF.EmitAnyExprToMem(SubExpr, AI, ThrowType.getQualifiers(),
|
|
|
|
/*IsInit=*/true);
|
|
|
|
|
|
|
|
// The so-called ThrowInfo is used to describe how the exception object may be
|
|
|
|
// caught.
|
|
|
|
llvm::GlobalVariable *TI = getThrowInfo(ThrowType);
|
|
|
|
|
|
|
|
// Call into the runtime to throw the exception.
|
|
|
|
llvm::Value *Args[] = {CGF.Builder.CreateBitCast(AI, CGM.Int8PtrTy), TI};
|
|
|
|
CGF.EmitNoreturnRuntimeCallOrInvoke(getThrowFn(), Args);
|
|
|
|
}
|