2013-10-02 05:51:38 +08:00
|
|
|
//===----- CGCXXABI.cpp - Interface to C++ ABIs ---------------------------===//
|
2010-11-29 01:46:52 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This provides an abstract class for C++ code generation. Concrete subclasses
|
|
|
|
// of this implement code generation for specific C++ ABIs.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "CGCXXABI.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
2010-11-29 01:49:03 +08:00
|
|
|
using namespace CodeGen;
|
2010-11-29 01:46:52 +08:00
|
|
|
|
2010-11-29 01:50:09 +08:00
|
|
|
CGCXXABI::~CGCXXABI() { }
|
2010-11-29 01:49:03 +08:00
|
|
|
|
2013-03-23 03:02:54 +08:00
|
|
|
void CGCXXABI::ErrorUnsupportedABI(CodeGenFunction &CGF, StringRef S) {
|
2011-09-26 07:23:43 +08:00
|
|
|
DiagnosticsEngine &Diags = CGF.CGM.getDiags();
|
|
|
|
unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
|
2012-06-29 09:14:21 +08:00
|
|
|
"cannot yet compile %0 in this ABI");
|
2010-11-29 01:49:03 +08:00
|
|
|
Diags.Report(CGF.getContext().getFullLoc(CGF.CurCodeDecl->getLocation()),
|
|
|
|
DiagID)
|
|
|
|
<< S;
|
|
|
|
}
|
|
|
|
|
2014-05-15 00:02:09 +08:00
|
|
|
bool CGCXXABI::canCopyArgument(const CXXRecordDecl *RD) const {
|
|
|
|
// If RD has a non-trivial move or copy constructor, we cannot copy the
|
|
|
|
// argument.
|
|
|
|
if (RD->hasNonTrivialCopyConstructor() || RD->hasNonTrivialMoveConstructor())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// If RD has a non-trivial destructor, we cannot copy the argument.
|
|
|
|
if (RD->hasNonTrivialDestructor())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// We can only copy the argument if there exists at least one trivial,
|
|
|
|
// non-deleted copy or move constructor.
|
|
|
|
// FIXME: This assumes that all lazily declared copy and move constructors are
|
|
|
|
// not deleted. This assumption might not be true in some corner cases.
|
2014-05-15 09:26:32 +08:00
|
|
|
bool CopyDeleted = false;
|
|
|
|
bool MoveDeleted = false;
|
2014-05-15 00:02:09 +08:00
|
|
|
for (const CXXConstructorDecl *CD : RD->ctors()) {
|
|
|
|
if (CD->isCopyConstructor() || CD->isMoveConstructor()) {
|
|
|
|
assert(CD->isTrivial());
|
|
|
|
// We had at least one undeleted trivial copy or move ctor. Return
|
|
|
|
// directly.
|
|
|
|
if (!CD->isDeleted())
|
|
|
|
return true;
|
2014-05-15 09:26:32 +08:00
|
|
|
if (CD->isCopyConstructor())
|
|
|
|
CopyDeleted = true;
|
|
|
|
else
|
|
|
|
MoveDeleted = true;
|
2014-05-15 00:02:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If all trivial copy and move constructors are deleted, we cannot copy the
|
|
|
|
// argument.
|
2014-05-15 09:26:32 +08:00
|
|
|
return !(CopyDeleted && MoveDeleted);
|
2014-05-15 00:02:09 +08:00
|
|
|
}
|
|
|
|
|
2013-03-23 03:02:54 +08:00
|
|
|
llvm::Constant *CGCXXABI::GetBogusMemberPointer(QualType T) {
|
2010-11-29 01:49:03 +08:00
|
|
|
return llvm::Constant::getNullValue(CGM.getTypes().ConvertType(T));
|
|
|
|
}
|
|
|
|
|
2011-07-10 01:41:47 +08:00
|
|
|
llvm::Type *
|
2010-11-29 01:49:03 +08:00
|
|
|
CGCXXABI::ConvertMemberPointerType(const MemberPointerType *MPT) {
|
|
|
|
return CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType());
|
|
|
|
}
|
|
|
|
|
2014-02-21 07:22:07 +08:00
|
|
|
llvm::Value *CGCXXABI::EmitLoadOfMemberFunctionPointer(
|
|
|
|
CodeGenFunction &CGF, const Expr *E, llvm::Value *&This,
|
|
|
|
llvm::Value *MemPtr, const MemberPointerType *MPT) {
|
2010-11-29 01:49:03 +08:00
|
|
|
ErrorUnsupportedABI(CGF, "calls through member pointers");
|
|
|
|
|
|
|
|
const FunctionProtoType *FPT =
|
|
|
|
MPT->getPointeeType()->getAs<FunctionProtoType>();
|
|
|
|
const CXXRecordDecl *RD =
|
|
|
|
cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl());
|
2012-02-17 11:33:10 +08:00
|
|
|
llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(
|
|
|
|
CGM.getTypes().arrangeCXXMethodType(RD, FPT));
|
2010-11-29 01:49:03 +08:00
|
|
|
return llvm::Constant::getNullValue(FTy->getPointerTo());
|
|
|
|
}
|
|
|
|
|
2014-02-21 07:22:07 +08:00
|
|
|
llvm::Value *
|
|
|
|
CGCXXABI::EmitMemberDataPointerAddress(CodeGenFunction &CGF, const Expr *E,
|
|
|
|
llvm::Value *Base, llvm::Value *MemPtr,
|
|
|
|
const MemberPointerType *MPT) {
|
2010-11-29 01:49:03 +08:00
|
|
|
ErrorUnsupportedABI(CGF, "loads of member pointers");
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *Ty = CGF.ConvertType(MPT->getPointeeType())->getPointerTo();
|
2010-11-29 01:49:03 +08:00
|
|
|
return llvm::Constant::getNullValue(Ty);
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Value *CGCXXABI::EmitMemberPointerConversion(CodeGenFunction &CGF,
|
|
|
|
const CastExpr *E,
|
|
|
|
llvm::Value *Src) {
|
|
|
|
ErrorUnsupportedABI(CGF, "member function pointer conversions");
|
2013-03-23 03:02:54 +08:00
|
|
|
return GetBogusMemberPointer(E->getType());
|
2010-11-29 01:49:03 +08:00
|
|
|
}
|
|
|
|
|
2012-02-15 09:22:51 +08:00
|
|
|
llvm::Constant *CGCXXABI::EmitMemberPointerConversion(const CastExpr *E,
|
|
|
|
llvm::Constant *Src) {
|
2013-03-23 03:02:54 +08:00
|
|
|
return GetBogusMemberPointer(E->getType());
|
2012-02-15 09:22:51 +08:00
|
|
|
}
|
|
|
|
|
2010-11-29 01:49:03 +08:00
|
|
|
llvm::Value *
|
|
|
|
CGCXXABI::EmitMemberPointerComparison(CodeGenFunction &CGF,
|
|
|
|
llvm::Value *L,
|
|
|
|
llvm::Value *R,
|
|
|
|
const MemberPointerType *MPT,
|
|
|
|
bool Inequality) {
|
|
|
|
ErrorUnsupportedABI(CGF, "member function pointer comparison");
|
|
|
|
return CGF.Builder.getFalse();
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Value *
|
|
|
|
CGCXXABI::EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
|
|
|
|
llvm::Value *MemPtr,
|
|
|
|
const MemberPointerType *MPT) {
|
|
|
|
ErrorUnsupportedABI(CGF, "member function pointer null testing");
|
|
|
|
return CGF.Builder.getFalse();
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Constant *
|
|
|
|
CGCXXABI::EmitNullMemberPointer(const MemberPointerType *MPT) {
|
2013-03-23 03:02:54 +08:00
|
|
|
return GetBogusMemberPointer(QualType(MPT, 0));
|
2010-11-29 01:49:03 +08:00
|
|
|
}
|
|
|
|
|
2011-04-12 08:42:48 +08:00
|
|
|
llvm::Constant *CGCXXABI::EmitMemberPointer(const CXXMethodDecl *MD) {
|
2013-03-23 03:02:54 +08:00
|
|
|
return GetBogusMemberPointer(
|
2010-11-29 01:49:03 +08:00
|
|
|
CGM.getContext().getMemberPointerType(MD->getType(),
|
|
|
|
MD->getParent()->getTypeForDecl()));
|
|
|
|
}
|
|
|
|
|
2011-02-03 16:15:49 +08:00
|
|
|
llvm::Constant *CGCXXABI::EmitMemberDataPointer(const MemberPointerType *MPT,
|
|
|
|
CharUnits offset) {
|
2013-03-23 03:02:54 +08:00
|
|
|
return GetBogusMemberPointer(QualType(MPT, 0));
|
2010-11-29 01:49:03 +08:00
|
|
|
}
|
|
|
|
|
2012-01-14 12:30:29 +08:00
|
|
|
llvm::Constant *CGCXXABI::EmitMemberPointer(const APValue &MP, QualType MPT) {
|
2013-03-23 03:02:54 +08:00
|
|
|
return GetBogusMemberPointer(MPT);
|
2012-01-14 12:30:29 +08:00
|
|
|
}
|
|
|
|
|
2010-11-29 01:49:03 +08:00
|
|
|
bool CGCXXABI::isZeroInitializable(const MemberPointerType *MPT) {
|
|
|
|
// Fake answer.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-12-18 03:46:40 +08:00
|
|
|
void CGCXXABI::buildThisParam(CodeGenFunction &CGF, FunctionArgList ¶ms) {
|
2010-11-29 01:49:03 +08:00
|
|
|
const CXXMethodDecl *MD = cast<CXXMethodDecl>(CGF.CurGD.getDecl());
|
|
|
|
|
|
|
|
// FIXME: I'm not entirely sure I like using a fake decl just for code
|
|
|
|
// generation. Maybe we can come up with a better way?
|
|
|
|
ImplicitParamDecl *ThisDecl
|
2014-05-21 13:09:00 +08:00
|
|
|
= ImplicitParamDecl::Create(CGM.getContext(), nullptr, MD->getLocation(),
|
2010-11-29 01:49:03 +08:00
|
|
|
&CGM.getContext().Idents.get("this"),
|
|
|
|
MD->getThisType(CGM.getContext()));
|
2011-03-09 12:27:21 +08:00
|
|
|
params.push_back(ThisDecl);
|
2010-11-29 01:49:03 +08:00
|
|
|
getThisDecl(CGF) = ThisDecl;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CGCXXABI::EmitThisParam(CodeGenFunction &CGF) {
|
|
|
|
/// Initialize the 'this' slot.
|
|
|
|
assert(getThisDecl(CGF) && "no 'this' variable for function");
|
|
|
|
getThisValue(CGF)
|
|
|
|
= CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(getThisDecl(CGF)),
|
|
|
|
"this");
|
|
|
|
}
|
|
|
|
|
|
|
|
void CGCXXABI::EmitReturnFromThunk(CodeGenFunction &CGF,
|
|
|
|
RValue RV, QualType ResultType) {
|
|
|
|
CGF.EmitReturnOfRValue(RV, ResultType);
|
|
|
|
}
|
|
|
|
|
2011-01-27 17:37:56 +08:00
|
|
|
CharUnits CGCXXABI::GetArrayCookieSize(const CXXNewExpr *expr) {
|
2012-05-01 13:23:51 +08:00
|
|
|
if (!requiresArrayCookie(expr))
|
|
|
|
return CharUnits::Zero();
|
|
|
|
return getArrayCookieSizeImpl(expr->getAllocatedType());
|
|
|
|
}
|
|
|
|
|
|
|
|
CharUnits CGCXXABI::getArrayCookieSizeImpl(QualType elementType) {
|
|
|
|
// BOGUS
|
2010-11-29 01:49:03 +08:00
|
|
|
return CharUnits::Zero();
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Value *CGCXXABI::InitializeArrayCookie(CodeGenFunction &CGF,
|
|
|
|
llvm::Value *NewPtr,
|
|
|
|
llvm::Value *NumElements,
|
2011-01-27 17:37:56 +08:00
|
|
|
const CXXNewExpr *expr,
|
2010-11-29 01:49:03 +08:00
|
|
|
QualType ElementType) {
|
|
|
|
// Should never be called.
|
|
|
|
ErrorUnsupportedABI(CGF, "array cookie initialization");
|
2014-05-21 13:09:00 +08:00
|
|
|
return nullptr;
|
2010-11-29 01:49:03 +08:00
|
|
|
}
|
|
|
|
|
2012-05-01 13:23:51 +08:00
|
|
|
bool CGCXXABI::requiresArrayCookie(const CXXDeleteExpr *expr,
|
|
|
|
QualType elementType) {
|
|
|
|
// If the class's usual deallocation function takes two arguments,
|
|
|
|
// it needs a cookie.
|
|
|
|
if (expr->doesUsualArrayDeleteWantSize())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return elementType.isDestructedType();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CGCXXABI::requiresArrayCookie(const CXXNewExpr *expr) {
|
|
|
|
// If the class's usual deallocation function takes two arguments,
|
|
|
|
// it needs a cookie.
|
|
|
|
if (expr->doesUsualArrayDeleteWantSize())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return expr->getAllocatedType().isDestructedType();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CGCXXABI::ReadArrayCookie(CodeGenFunction &CGF, llvm::Value *ptr,
|
|
|
|
const CXXDeleteExpr *expr, QualType eltTy,
|
|
|
|
llvm::Value *&numElements,
|
|
|
|
llvm::Value *&allocPtr, CharUnits &cookieSize) {
|
|
|
|
// Derive a char* in the same address space as the pointer.
|
2012-10-25 23:39:14 +08:00
|
|
|
unsigned AS = ptr->getType()->getPointerAddressSpace();
|
2012-05-01 13:23:51 +08:00
|
|
|
llvm::Type *charPtrTy = CGF.Int8Ty->getPointerTo(AS);
|
|
|
|
ptr = CGF.Builder.CreateBitCast(ptr, charPtrTy);
|
|
|
|
|
|
|
|
// If we don't need an array cookie, bail out early.
|
|
|
|
if (!requiresArrayCookie(expr, eltTy)) {
|
|
|
|
allocPtr = ptr;
|
2014-05-21 13:09:00 +08:00
|
|
|
numElements = nullptr;
|
2012-05-01 13:23:51 +08:00
|
|
|
cookieSize = CharUnits::Zero();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
cookieSize = getArrayCookieSizeImpl(eltTy);
|
|
|
|
allocPtr = CGF.Builder.CreateConstInBoundsGEP1_64(ptr,
|
|
|
|
-cookieSize.getQuantity());
|
|
|
|
numElements = readArrayCookieImpl(CGF, allocPtr, cookieSize);
|
|
|
|
}
|
2010-11-29 01:49:03 +08:00
|
|
|
|
2012-05-01 13:23:51 +08:00
|
|
|
llvm::Value *CGCXXABI::readArrayCookieImpl(CodeGenFunction &CGF,
|
|
|
|
llvm::Value *ptr,
|
|
|
|
CharUnits cookieSize) {
|
|
|
|
ErrorUnsupportedABI(CGF, "reading a new[] cookie");
|
|
|
|
return llvm::ConstantInt::get(CGF.SizeTy, 0);
|
2010-11-29 01:49:03 +08:00
|
|
|
}
|
|
|
|
|
2012-05-01 14:13:13 +08:00
|
|
|
void CGCXXABI::registerGlobalDtor(CodeGenFunction &CGF,
|
2013-04-15 07:01:42 +08:00
|
|
|
const VarDecl &D,
|
2012-05-01 14:13:13 +08:00
|
|
|
llvm::Constant *dtor,
|
|
|
|
llvm::Constant *addr) {
|
2013-04-15 07:01:42 +08:00
|
|
|
if (D.getTLSKind())
|
|
|
|
CGM.ErrorUnsupported(&D, "non-trivial TLS destruction");
|
|
|
|
|
2012-05-01 14:13:13 +08:00
|
|
|
// The default behavior is to use atexit.
|
2013-08-28 07:57:18 +08:00
|
|
|
CGF.registerGlobalDtorWithAtExit(D, dtor, addr);
|
2012-05-01 14:13:13 +08:00
|
|
|
}
|
|
|
|
|
2012-02-15 09:22:51 +08:00
|
|
|
/// Returns the adjustment, in bytes, required for the given
|
|
|
|
/// member-pointer operation. Returns null if no adjustment is
|
|
|
|
/// required.
|
|
|
|
llvm::Constant *CGCXXABI::getMemberPointerAdjustment(const CastExpr *E) {
|
|
|
|
assert(E->getCastKind() == CK_DerivedToBaseMemberPointer ||
|
|
|
|
E->getCastKind() == CK_BaseToDerivedMemberPointer);
|
|
|
|
|
|
|
|
QualType derivedType;
|
|
|
|
if (E->getCastKind() == CK_DerivedToBaseMemberPointer)
|
|
|
|
derivedType = E->getSubExpr()->getType();
|
|
|
|
else
|
|
|
|
derivedType = E->getType();
|
|
|
|
|
|
|
|
const CXXRecordDecl *derivedClass =
|
|
|
|
derivedType->castAs<MemberPointerType>()->getClass()->getAsCXXRecordDecl();
|
|
|
|
|
|
|
|
return CGM.GetNonVirtualBaseClassOffset(derivedClass,
|
|
|
|
E->path_begin(),
|
|
|
|
E->path_end());
|
|
|
|
}
|
2013-02-27 21:46:31 +08:00
|
|
|
|
2013-05-10 05:01:17 +08:00
|
|
|
CharUnits CGCXXABI::getMemberPointerPathAdjustment(const APValue &MP) {
|
|
|
|
// TODO: Store base specifiers in APValue member pointer paths so we can
|
|
|
|
// easily reuse CGM.GetNonVirtualBaseClassOffset().
|
|
|
|
const ValueDecl *MPD = MP.getMemberPointerDecl();
|
|
|
|
CharUnits ThisAdjustment = CharUnits::Zero();
|
|
|
|
ArrayRef<const CXXRecordDecl*> Path = MP.getMemberPointerPath();
|
|
|
|
bool DerivedMember = MP.isMemberPointerToDerivedMember();
|
|
|
|
const CXXRecordDecl *RD = cast<CXXRecordDecl>(MPD->getDeclContext());
|
|
|
|
for (unsigned I = 0, N = Path.size(); I != N; ++I) {
|
|
|
|
const CXXRecordDecl *Base = RD;
|
|
|
|
const CXXRecordDecl *Derived = Path[I];
|
|
|
|
if (DerivedMember)
|
|
|
|
std::swap(Base, Derived);
|
|
|
|
ThisAdjustment +=
|
|
|
|
getContext().getASTRecordLayout(Derived).getBaseClassOffset(Base);
|
|
|
|
RD = Path[I];
|
|
|
|
}
|
|
|
|
if (DerivedMember)
|
|
|
|
ThisAdjustment = -ThisAdjustment;
|
|
|
|
return ThisAdjustment;
|
|
|
|
}
|
|
|
|
|
2013-06-19 23:20:38 +08:00
|
|
|
llvm::BasicBlock *
|
|
|
|
CGCXXABI::EmitCtorCompleteObjectHandler(CodeGenFunction &CGF,
|
|
|
|
const CXXRecordDecl *RD) {
|
2013-02-27 21:46:31 +08:00
|
|
|
if (CGM.getTarget().getCXXABI().hasConstructorVariants())
|
|
|
|
llvm_unreachable("shouldn't be called in this ABI");
|
|
|
|
|
|
|
|
ErrorUnsupportedABI(CGF, "complete object detection in ctor");
|
2014-05-21 13:09:00 +08:00
|
|
|
return nullptr;
|
2013-02-27 21:46:31 +08:00
|
|
|
}
|
2013-04-20 00:42:07 +08:00
|
|
|
|
|
|
|
void CGCXXABI::EmitThreadLocalInitFuncs(
|
2014-06-29 07:22:23 +08:00
|
|
|
ArrayRef<std::pair<const VarDecl *, llvm::GlobalVariable *> > Decls,
|
2013-04-20 00:42:07 +08:00
|
|
|
llvm::Function *InitFunc) {
|
|
|
|
}
|
|
|
|
|
2014-03-27 06:48:22 +08:00
|
|
|
LValue CGCXXABI::EmitThreadLocalVarDeclLValue(CodeGenFunction &CGF,
|
|
|
|
const VarDecl *VD,
|
|
|
|
QualType LValType) {
|
2013-04-20 00:42:07 +08:00
|
|
|
ErrorUnsupportedABI(CGF, "odr-use of thread_local global");
|
|
|
|
return LValue();
|
|
|
|
}
|
2013-06-29 04:45:28 +08:00
|
|
|
|
|
|
|
bool CGCXXABI::NeedsVTTParameter(GlobalDecl GD) {
|
|
|
|
return false;
|
|
|
|
}
|