2010-05-26 03:52:27 +08:00
|
|
|
//===------- ItaniumCXXABI.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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This provides C++ code generation targetting the Itanium C++ ABI. The class
|
|
|
|
// in this file generates structures that follow the Itanium C++ ABI, which is
|
|
|
|
// documented at:
|
|
|
|
// http://www.codesourcery.com/public/cxx-abi/abi.html
|
|
|
|
// http://www.codesourcery.com/public/cxx-abi/abi-eh.html
|
2010-08-22 06:46:04 +08:00
|
|
|
//
|
|
|
|
// It also supports the closely-related ARM ABI, documented at:
|
|
|
|
// http://infocenter.arm.com/help/topic/com.arm.doc.ihi0041c/IHI0041C_cppabi.pdf
|
|
|
|
//
|
2010-05-26 03:52:27 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "CGCXXABI.h"
|
2010-08-23 09:21:21 +08:00
|
|
|
#include "CGRecordLayout.h"
|
2010-08-22 08:05:51 +08:00
|
|
|
#include "CodeGenFunction.h"
|
2010-05-26 03:52:27 +08:00
|
|
|
#include "CodeGenModule.h"
|
|
|
|
#include "Mangle.h"
|
2010-08-22 08:05:51 +08:00
|
|
|
#include <clang/AST/Type.h>
|
2010-08-23 09:21:21 +08:00
|
|
|
#include <llvm/Target/TargetData.h>
|
2010-08-22 08:05:51 +08:00
|
|
|
#include <llvm/Value.h>
|
2010-05-26 03:52:27 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
2010-08-22 08:05:51 +08:00
|
|
|
using namespace CodeGen;
|
2010-05-26 03:52:27 +08:00
|
|
|
|
|
|
|
namespace {
|
2010-08-16 11:33:14 +08:00
|
|
|
class ItaniumCXXABI : public CodeGen::CGCXXABI {
|
2010-08-23 09:21:21 +08:00
|
|
|
private:
|
|
|
|
const llvm::IntegerType *PtrDiffTy;
|
2010-08-22 08:05:51 +08:00
|
|
|
protected:
|
2010-05-26 03:52:27 +08:00
|
|
|
CodeGen::MangleContext MangleCtx;
|
2010-08-22 08:59:17 +08:00
|
|
|
bool IsARM;
|
2010-08-23 09:21:21 +08:00
|
|
|
|
|
|
|
// It's a little silly for us to cache this.
|
|
|
|
const llvm::IntegerType *getPtrDiffTy() {
|
|
|
|
if (!PtrDiffTy) {
|
|
|
|
QualType T = CGM.getContext().getPointerDiffType();
|
|
|
|
const llvm::Type *Ty = CGM.getTypes().ConvertTypeRecursive(T);
|
|
|
|
PtrDiffTy = cast<llvm::IntegerType>(Ty);
|
|
|
|
}
|
|
|
|
return PtrDiffTy;
|
|
|
|
}
|
|
|
|
|
2010-09-02 17:58:18 +08:00
|
|
|
bool NeedsArrayCookie(QualType ElementType);
|
|
|
|
|
2010-05-26 03:52:27 +08:00
|
|
|
public:
|
2010-08-22 08:59:17 +08:00
|
|
|
ItaniumCXXABI(CodeGen::CodeGenModule &CGM, bool IsARM = false) :
|
2010-08-23 09:21:21 +08:00
|
|
|
CGCXXABI(CGM), PtrDiffTy(0), MangleCtx(CGM.getContext(), CGM.getDiags()),
|
|
|
|
IsARM(IsARM) { }
|
2010-05-26 03:52:27 +08:00
|
|
|
|
|
|
|
CodeGen::MangleContext &getMangleContext() {
|
|
|
|
return MangleCtx;
|
|
|
|
}
|
2010-08-22 08:05:51 +08:00
|
|
|
|
2010-08-23 05:01:12 +08:00
|
|
|
bool isZeroInitializable(const MemberPointerType *MPT);
|
2010-08-22 12:16:24 +08:00
|
|
|
|
2010-08-23 09:21:21 +08:00
|
|
|
const llvm::Type *ConvertMemberPointerType(const MemberPointerType *MPT);
|
|
|
|
|
2010-08-22 08:05:51 +08:00
|
|
|
llvm::Value *EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF,
|
|
|
|
llvm::Value *&This,
|
|
|
|
llvm::Value *MemFnPtr,
|
|
|
|
const MemberPointerType *MPT);
|
2010-08-22 11:04:22 +08:00
|
|
|
|
2010-09-01 05:07:20 +08:00
|
|
|
llvm::Value *EmitMemberDataPointerAddress(CodeGenFunction &CGF,
|
|
|
|
llvm::Value *Base,
|
|
|
|
llvm::Value *MemPtr,
|
|
|
|
const MemberPointerType *MPT);
|
|
|
|
|
2010-08-23 09:21:21 +08:00
|
|
|
llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF,
|
|
|
|
const CastExpr *E,
|
|
|
|
llvm::Value *Src);
|
2010-08-22 12:16:24 +08:00
|
|
|
|
2010-08-23 09:21:21 +08:00
|
|
|
llvm::Constant *EmitMemberPointerConversion(llvm::Constant *C,
|
|
|
|
const CastExpr *E);
|
2010-08-22 12:16:24 +08:00
|
|
|
|
2010-08-23 09:21:21 +08:00
|
|
|
llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT);
|
2010-08-22 12:16:24 +08:00
|
|
|
|
2010-08-23 09:21:21 +08:00
|
|
|
llvm::Constant *EmitMemberPointer(const CXXMethodDecl *MD);
|
|
|
|
llvm::Constant *EmitMemberPointer(const FieldDecl *FD);
|
2010-08-22 14:43:33 +08:00
|
|
|
|
2010-08-23 09:21:21 +08:00
|
|
|
llvm::Value *EmitMemberPointerComparison(CodeGenFunction &CGF,
|
|
|
|
llvm::Value *L,
|
|
|
|
llvm::Value *R,
|
|
|
|
const MemberPointerType *MPT,
|
|
|
|
bool Inequality);
|
2010-08-22 16:30:07 +08:00
|
|
|
|
2010-08-23 09:21:21 +08:00
|
|
|
llvm::Value *EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
|
|
|
|
llvm::Value *Addr,
|
|
|
|
const MemberPointerType *MPT);
|
2010-08-31 15:33:07 +08:00
|
|
|
|
|
|
|
void BuildConstructorSignature(const CXXConstructorDecl *Ctor,
|
|
|
|
CXXCtorType T,
|
|
|
|
CanQualType &ResTy,
|
|
|
|
llvm::SmallVectorImpl<CanQualType> &ArgTys);
|
|
|
|
|
|
|
|
void BuildDestructorSignature(const CXXDestructorDecl *Dtor,
|
|
|
|
CXXDtorType T,
|
|
|
|
CanQualType &ResTy,
|
|
|
|
llvm::SmallVectorImpl<CanQualType> &ArgTys);
|
|
|
|
|
|
|
|
void BuildInstanceFunctionParams(CodeGenFunction &CGF,
|
|
|
|
QualType &ResTy,
|
|
|
|
FunctionArgList &Params);
|
|
|
|
|
|
|
|
void EmitInstanceFunctionProlog(CodeGenFunction &CGF);
|
2010-09-02 17:58:18 +08:00
|
|
|
|
|
|
|
CharUnits GetArrayCookieSize(QualType ElementType);
|
|
|
|
llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF,
|
|
|
|
llvm::Value *NewPtr,
|
|
|
|
llvm::Value *NumElements,
|
|
|
|
QualType ElementType);
|
|
|
|
void ReadArrayCookie(CodeGenFunction &CGF, llvm::Value *Ptr,
|
|
|
|
QualType ElementType, llvm::Value *&NumElements,
|
|
|
|
llvm::Value *&AllocPtr, CharUnits &CookieSize);
|
2010-05-26 03:52:27 +08:00
|
|
|
};
|
2010-08-22 06:46:04 +08:00
|
|
|
|
|
|
|
class ARMCXXABI : public ItaniumCXXABI {
|
|
|
|
public:
|
2010-08-22 08:59:17 +08:00
|
|
|
ARMCXXABI(CodeGen::CodeGenModule &CGM) : ItaniumCXXABI(CGM, /*ARM*/ true) {}
|
2010-08-31 15:33:07 +08:00
|
|
|
|
|
|
|
void BuildConstructorSignature(const CXXConstructorDecl *Ctor,
|
|
|
|
CXXCtorType T,
|
|
|
|
CanQualType &ResTy,
|
|
|
|
llvm::SmallVectorImpl<CanQualType> &ArgTys);
|
|
|
|
|
|
|
|
void BuildDestructorSignature(const CXXDestructorDecl *Dtor,
|
|
|
|
CXXDtorType T,
|
|
|
|
CanQualType &ResTy,
|
|
|
|
llvm::SmallVectorImpl<CanQualType> &ArgTys);
|
|
|
|
|
|
|
|
void BuildInstanceFunctionParams(CodeGenFunction &CGF,
|
|
|
|
QualType &ResTy,
|
|
|
|
FunctionArgList &Params);
|
|
|
|
|
|
|
|
void EmitInstanceFunctionProlog(CodeGenFunction &CGF);
|
|
|
|
|
|
|
|
void EmitReturnFromThunk(CodeGenFunction &CGF, RValue RV, QualType ResTy);
|
|
|
|
|
2010-09-02 17:58:18 +08:00
|
|
|
CharUnits GetArrayCookieSize(QualType ElementType);
|
|
|
|
llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF,
|
|
|
|
llvm::Value *NewPtr,
|
|
|
|
llvm::Value *NumElements,
|
|
|
|
QualType ElementType);
|
|
|
|
void ReadArrayCookie(CodeGenFunction &CGF, llvm::Value *Ptr,
|
|
|
|
QualType ElementType, llvm::Value *&NumElements,
|
|
|
|
llvm::Value *&AllocPtr, CharUnits &CookieSize);
|
2010-08-31 15:33:07 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
/// \brief Returns true if the given instance method is one of the
|
|
|
|
/// kinds that the ARM ABI says returns 'this'.
|
|
|
|
static bool HasThisReturn(GlobalDecl GD) {
|
|
|
|
const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
|
|
|
|
return ((isa<CXXDestructorDecl>(MD) && GD.getDtorType() != Dtor_Deleting) ||
|
|
|
|
(isa<CXXConstructorDecl>(MD)));
|
|
|
|
}
|
2010-08-22 06:46:04 +08:00
|
|
|
};
|
2010-05-26 03:52:27 +08:00
|
|
|
}
|
|
|
|
|
2010-08-16 11:33:14 +08:00
|
|
|
CodeGen::CGCXXABI *CodeGen::CreateItaniumCXXABI(CodeGenModule &CGM) {
|
2010-05-26 03:52:27 +08:00
|
|
|
return new ItaniumCXXABI(CGM);
|
|
|
|
}
|
|
|
|
|
2010-08-22 06:46:04 +08:00
|
|
|
CodeGen::CGCXXABI *CodeGen::CreateARMCXXABI(CodeGenModule &CGM) {
|
|
|
|
return new ARMCXXABI(CGM);
|
|
|
|
}
|
|
|
|
|
2010-08-23 09:21:21 +08:00
|
|
|
const llvm::Type *
|
|
|
|
ItaniumCXXABI::ConvertMemberPointerType(const MemberPointerType *MPT) {
|
|
|
|
if (MPT->isMemberDataPointer())
|
|
|
|
return getPtrDiffTy();
|
|
|
|
else
|
|
|
|
return llvm::StructType::get(CGM.getLLVMContext(),
|
|
|
|
getPtrDiffTy(), getPtrDiffTy(), NULL);
|
2010-08-22 14:43:33 +08:00
|
|
|
}
|
|
|
|
|
2010-08-22 08:59:17 +08:00
|
|
|
/// In the Itanium and ARM ABIs, method pointers have the form:
|
|
|
|
/// struct { ptrdiff_t ptr; ptrdiff_t adj; } memptr;
|
|
|
|
///
|
|
|
|
/// In the Itanium ABI:
|
|
|
|
/// - method pointers are virtual if (memptr.ptr & 1) is nonzero
|
|
|
|
/// - the this-adjustment is (memptr.adj)
|
|
|
|
/// - the virtual offset is (memptr.ptr - 1)
|
|
|
|
///
|
|
|
|
/// In the ARM ABI:
|
|
|
|
/// - method pointers are virtual if (memptr.adj & 1) is nonzero
|
|
|
|
/// - the this-adjustment is (memptr.adj >> 1)
|
|
|
|
/// - the virtual offset is (memptr.ptr)
|
|
|
|
/// ARM uses 'adj' for the virtual flag because Thumb functions
|
|
|
|
/// may be only single-byte aligned.
|
|
|
|
///
|
|
|
|
/// If the member is virtual, the adjusted 'this' pointer points
|
|
|
|
/// to a vtable pointer from which the virtual offset is applied.
|
|
|
|
///
|
|
|
|
/// If the member is non-virtual, memptr.ptr is the address of
|
|
|
|
/// the function to call.
|
2010-08-22 08:05:51 +08:00
|
|
|
llvm::Value *
|
|
|
|
ItaniumCXXABI::EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF,
|
|
|
|
llvm::Value *&This,
|
|
|
|
llvm::Value *MemFnPtr,
|
|
|
|
const MemberPointerType *MPT) {
|
|
|
|
CGBuilderTy &Builder = CGF.Builder;
|
|
|
|
|
|
|
|
const FunctionProtoType *FPT =
|
|
|
|
MPT->getPointeeType()->getAs<FunctionProtoType>();
|
|
|
|
const CXXRecordDecl *RD =
|
|
|
|
cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl());
|
|
|
|
|
|
|
|
const llvm::FunctionType *FTy =
|
|
|
|
CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(RD, FPT),
|
|
|
|
FPT->isVariadic());
|
|
|
|
|
2010-08-23 09:21:21 +08:00
|
|
|
const llvm::IntegerType *ptrdiff = getPtrDiffTy();
|
2010-08-22 08:59:17 +08:00
|
|
|
llvm::Constant *ptrdiff_1 = llvm::ConstantInt::get(ptrdiff, 1);
|
2010-08-22 08:05:51 +08:00
|
|
|
|
2010-08-22 08:59:17 +08:00
|
|
|
llvm::BasicBlock *FnVirtual = CGF.createBasicBlock("memptr.virtual");
|
|
|
|
llvm::BasicBlock *FnNonVirtual = CGF.createBasicBlock("memptr.nonvirtual");
|
|
|
|
llvm::BasicBlock *FnEnd = CGF.createBasicBlock("memptr.end");
|
|
|
|
|
2010-08-22 18:59:02 +08:00
|
|
|
// Extract memptr.adj, which is in the second field.
|
|
|
|
llvm::Value *RawAdj = Builder.CreateExtractValue(MemFnPtr, 1, "memptr.adj");
|
2010-08-22 08:59:17 +08:00
|
|
|
|
|
|
|
// Compute the true adjustment.
|
|
|
|
llvm::Value *Adj = RawAdj;
|
|
|
|
if (IsARM)
|
|
|
|
Adj = Builder.CreateAShr(Adj, ptrdiff_1, "memptr.adj.shifted");
|
2010-08-22 08:05:51 +08:00
|
|
|
|
|
|
|
// Apply the adjustment and cast back to the original struct type
|
|
|
|
// for consistency.
|
2010-08-22 08:59:17 +08:00
|
|
|
llvm::Value *Ptr = Builder.CreateBitCast(This, Builder.getInt8PtrTy());
|
|
|
|
Ptr = Builder.CreateInBoundsGEP(Ptr, Adj);
|
|
|
|
This = Builder.CreateBitCast(Ptr, This->getType(), "this.adjusted");
|
2010-08-22 08:05:51 +08:00
|
|
|
|
|
|
|
// Load the function pointer.
|
2010-08-22 18:59:02 +08:00
|
|
|
llvm::Value *FnAsInt = Builder.CreateExtractValue(MemFnPtr, 0, "memptr.ptr");
|
2010-08-22 08:05:51 +08:00
|
|
|
|
|
|
|
// If the LSB in the function pointer is 1, the function pointer points to
|
|
|
|
// a virtual function.
|
2010-08-22 08:59:17 +08:00
|
|
|
llvm::Value *IsVirtual;
|
|
|
|
if (IsARM)
|
|
|
|
IsVirtual = Builder.CreateAnd(RawAdj, ptrdiff_1);
|
|
|
|
else
|
|
|
|
IsVirtual = Builder.CreateAnd(FnAsInt, ptrdiff_1);
|
|
|
|
IsVirtual = Builder.CreateIsNotNull(IsVirtual, "memptr.isvirtual");
|
2010-08-22 08:05:51 +08:00
|
|
|
Builder.CreateCondBr(IsVirtual, FnVirtual, FnNonVirtual);
|
|
|
|
|
|
|
|
// In the virtual path, the adjustment left 'This' pointing to the
|
|
|
|
// vtable of the correct base subobject. The "function pointer" is an
|
2010-08-22 08:59:17 +08:00
|
|
|
// offset within the vtable (+1 for the virtual flag on non-ARM).
|
2010-08-22 08:05:51 +08:00
|
|
|
CGF.EmitBlock(FnVirtual);
|
|
|
|
|
|
|
|
// Cast the adjusted this to a pointer to vtable pointer and load.
|
|
|
|
const llvm::Type *VTableTy = Builder.getInt8PtrTy();
|
|
|
|
llvm::Value *VTable = Builder.CreateBitCast(This, VTableTy->getPointerTo());
|
2010-08-22 08:59:17 +08:00
|
|
|
VTable = Builder.CreateLoad(VTable, "memptr.vtable");
|
2010-08-22 08:05:51 +08:00
|
|
|
|
|
|
|
// Apply the offset.
|
2010-08-22 08:59:17 +08:00
|
|
|
llvm::Value *VTableOffset = FnAsInt;
|
|
|
|
if (!IsARM) VTableOffset = Builder.CreateSub(VTableOffset, ptrdiff_1);
|
|
|
|
VTable = Builder.CreateGEP(VTable, VTableOffset);
|
2010-08-22 08:05:51 +08:00
|
|
|
|
|
|
|
// Load the virtual function to call.
|
|
|
|
VTable = Builder.CreateBitCast(VTable, FTy->getPointerTo()->getPointerTo());
|
2010-08-22 08:59:17 +08:00
|
|
|
llvm::Value *VirtualFn = Builder.CreateLoad(VTable, "memptr.virtualfn");
|
2010-08-22 08:05:51 +08:00
|
|
|
CGF.EmitBranch(FnEnd);
|
|
|
|
|
|
|
|
// In the non-virtual path, the function pointer is actually a
|
|
|
|
// function pointer.
|
|
|
|
CGF.EmitBlock(FnNonVirtual);
|
|
|
|
llvm::Value *NonVirtualFn =
|
2010-08-22 08:59:17 +08:00
|
|
|
Builder.CreateIntToPtr(FnAsInt, FTy->getPointerTo(), "memptr.nonvirtualfn");
|
2010-08-22 08:05:51 +08:00
|
|
|
|
|
|
|
// We're done.
|
|
|
|
CGF.EmitBlock(FnEnd);
|
|
|
|
llvm::PHINode *Callee = Builder.CreatePHI(FTy->getPointerTo());
|
|
|
|
Callee->reserveOperandSpace(2);
|
|
|
|
Callee->addIncoming(VirtualFn, FnVirtual);
|
|
|
|
Callee->addIncoming(NonVirtualFn, FnNonVirtual);
|
|
|
|
return Callee;
|
|
|
|
}
|
2010-08-22 11:04:22 +08:00
|
|
|
|
2010-09-01 05:07:20 +08:00
|
|
|
/// Compute an l-value by applying the given pointer-to-member to a
|
|
|
|
/// base object.
|
|
|
|
llvm::Value *ItaniumCXXABI::EmitMemberDataPointerAddress(CodeGenFunction &CGF,
|
|
|
|
llvm::Value *Base,
|
|
|
|
llvm::Value *MemPtr,
|
|
|
|
const MemberPointerType *MPT) {
|
|
|
|
assert(MemPtr->getType() == getPtrDiffTy());
|
|
|
|
|
|
|
|
CGBuilderTy &Builder = CGF.Builder;
|
|
|
|
|
|
|
|
unsigned AS = cast<llvm::PointerType>(Base->getType())->getAddressSpace();
|
|
|
|
|
|
|
|
// Cast to char*.
|
|
|
|
Base = Builder.CreateBitCast(Base, Builder.getInt8Ty()->getPointerTo(AS));
|
|
|
|
|
|
|
|
// Apply the offset, which we assume is non-null.
|
|
|
|
llvm::Value *Addr = Builder.CreateInBoundsGEP(Base, MemPtr, "memptr.offset");
|
|
|
|
|
|
|
|
// Cast the address to the appropriate pointer type, adopting the
|
|
|
|
// address space of the base pointer.
|
|
|
|
const llvm::Type *PType
|
|
|
|
= CGF.ConvertType(MPT->getPointeeType())->getPointerTo(AS);
|
|
|
|
return Builder.CreateBitCast(Addr, PType);
|
|
|
|
}
|
|
|
|
|
2010-08-22 11:04:22 +08:00
|
|
|
/// Perform a derived-to-base or base-to-derived member pointer conversion.
|
2010-08-23 09:21:21 +08:00
|
|
|
///
|
|
|
|
/// Obligatory offset/adjustment diagram:
|
|
|
|
/// <-- offset --> <-- adjustment -->
|
|
|
|
/// |--------------------------|----------------------|--------------------|
|
|
|
|
/// ^Derived address point ^Base address point ^Member address point
|
|
|
|
///
|
|
|
|
/// So when converting a base member pointer to a derived member pointer,
|
|
|
|
/// we add the offset to the adjustment because the address point has
|
|
|
|
/// decreased; and conversely, when converting a derived MP to a base MP
|
|
|
|
/// we subtract the offset from the adjustment because the address point
|
|
|
|
/// has increased.
|
|
|
|
///
|
|
|
|
/// The standard forbids (at compile time) conversion to and from
|
|
|
|
/// virtual bases, which is why we don't have to consider them here.
|
|
|
|
///
|
|
|
|
/// The standard forbids (at run time) casting a derived MP to a base
|
|
|
|
/// MP when the derived MP does not point to a member of the base.
|
|
|
|
/// This is why -1 is a reasonable choice for null data member
|
|
|
|
/// pointers.
|
2010-08-22 18:59:02 +08:00
|
|
|
llvm::Value *
|
2010-08-23 09:21:21 +08:00
|
|
|
ItaniumCXXABI::EmitMemberPointerConversion(CodeGenFunction &CGF,
|
|
|
|
const CastExpr *E,
|
|
|
|
llvm::Value *Src) {
|
2010-08-25 19:45:40 +08:00
|
|
|
assert(E->getCastKind() == CK_DerivedToBaseMemberPointer ||
|
|
|
|
E->getCastKind() == CK_BaseToDerivedMemberPointer);
|
2010-08-22 11:04:22 +08:00
|
|
|
|
2010-08-22 18:59:02 +08:00
|
|
|
if (isa<llvm::Constant>(Src))
|
2010-08-23 09:21:21 +08:00
|
|
|
return EmitMemberPointerConversion(cast<llvm::Constant>(Src), E);
|
2010-08-22 18:59:02 +08:00
|
|
|
|
2010-08-22 11:04:22 +08:00
|
|
|
CGBuilderTy &Builder = CGF.Builder;
|
|
|
|
|
|
|
|
const MemberPointerType *SrcTy =
|
|
|
|
E->getSubExpr()->getType()->getAs<MemberPointerType>();
|
|
|
|
const MemberPointerType *DestTy = E->getType()->getAs<MemberPointerType>();
|
|
|
|
|
|
|
|
const CXXRecordDecl *SrcDecl = SrcTy->getClass()->getAsCXXRecordDecl();
|
|
|
|
const CXXRecordDecl *DestDecl = DestTy->getClass()->getAsCXXRecordDecl();
|
|
|
|
|
|
|
|
bool DerivedToBase =
|
2010-08-25 19:45:40 +08:00
|
|
|
E->getCastKind() == CK_DerivedToBaseMemberPointer;
|
2010-08-22 11:04:22 +08:00
|
|
|
|
|
|
|
const CXXRecordDecl *BaseDecl, *DerivedDecl;
|
|
|
|
if (DerivedToBase)
|
|
|
|
DerivedDecl = SrcDecl, BaseDecl = DestDecl;
|
|
|
|
else
|
|
|
|
BaseDecl = SrcDecl, DerivedDecl = DestDecl;
|
|
|
|
|
2010-08-22 18:59:02 +08:00
|
|
|
llvm::Constant *Adj =
|
|
|
|
CGF.CGM.GetNonVirtualBaseClassOffset(DerivedDecl,
|
|
|
|
E->path_begin(),
|
|
|
|
E->path_end());
|
|
|
|
if (!Adj) return Src;
|
2010-08-22 14:43:33 +08:00
|
|
|
|
2010-08-23 09:21:21 +08:00
|
|
|
// For member data pointers, this is just a matter of adding the
|
|
|
|
// offset if the source is non-null.
|
|
|
|
if (SrcTy->isMemberDataPointer()) {
|
|
|
|
llvm::Value *Dst;
|
|
|
|
if (DerivedToBase)
|
|
|
|
Dst = Builder.CreateNSWSub(Src, Adj, "adj");
|
|
|
|
else
|
|
|
|
Dst = Builder.CreateNSWAdd(Src, Adj, "adj");
|
|
|
|
|
|
|
|
// Null check.
|
|
|
|
llvm::Value *Null = llvm::Constant::getAllOnesValue(Src->getType());
|
|
|
|
llvm::Value *IsNull = Builder.CreateICmpEQ(Src, Null, "memptr.isnull");
|
|
|
|
return Builder.CreateSelect(IsNull, Src, Dst);
|
|
|
|
}
|
|
|
|
|
2010-08-22 18:59:02 +08:00
|
|
|
// The this-adjustment is left-shifted by 1 on ARM.
|
|
|
|
if (IsARM) {
|
|
|
|
uint64_t Offset = cast<llvm::ConstantInt>(Adj)->getZExtValue();
|
|
|
|
Offset <<= 1;
|
|
|
|
Adj = llvm::ConstantInt::get(Adj->getType(), Offset);
|
|
|
|
}
|
|
|
|
|
2010-08-22 19:04:31 +08:00
|
|
|
llvm::Value *SrcAdj = Builder.CreateExtractValue(Src, 1, "src.adj");
|
2010-08-22 18:59:02 +08:00
|
|
|
llvm::Value *DstAdj;
|
|
|
|
if (DerivedToBase)
|
2010-08-23 09:21:21 +08:00
|
|
|
DstAdj = Builder.CreateNSWSub(SrcAdj, Adj, "adj");
|
2010-08-22 18:59:02 +08:00
|
|
|
else
|
2010-08-23 09:21:21 +08:00
|
|
|
DstAdj = Builder.CreateNSWAdd(SrcAdj, Adj, "adj");
|
2010-08-22 18:59:02 +08:00
|
|
|
|
2010-08-22 19:04:31 +08:00
|
|
|
return Builder.CreateInsertValue(Src, DstAdj, 1);
|
2010-08-22 11:04:22 +08:00
|
|
|
}
|
2010-08-22 12:16:24 +08:00
|
|
|
|
|
|
|
llvm::Constant *
|
2010-08-23 09:21:21 +08:00
|
|
|
ItaniumCXXABI::EmitMemberPointerConversion(llvm::Constant *C,
|
|
|
|
const CastExpr *E) {
|
2010-08-22 12:16:24 +08:00
|
|
|
const MemberPointerType *SrcTy =
|
|
|
|
E->getSubExpr()->getType()->getAs<MemberPointerType>();
|
|
|
|
const MemberPointerType *DestTy =
|
|
|
|
E->getType()->getAs<MemberPointerType>();
|
|
|
|
|
|
|
|
bool DerivedToBase =
|
2010-08-25 19:45:40 +08:00
|
|
|
E->getCastKind() == CK_DerivedToBaseMemberPointer;
|
2010-08-22 12:16:24 +08:00
|
|
|
|
|
|
|
const CXXRecordDecl *DerivedDecl;
|
|
|
|
if (DerivedToBase)
|
|
|
|
DerivedDecl = SrcTy->getClass()->getAsCXXRecordDecl();
|
|
|
|
else
|
|
|
|
DerivedDecl = DestTy->getClass()->getAsCXXRecordDecl();
|
|
|
|
|
|
|
|
// Calculate the offset to the base class.
|
|
|
|
llvm::Constant *Offset =
|
|
|
|
CGM.GetNonVirtualBaseClassOffset(DerivedDecl,
|
|
|
|
E->path_begin(),
|
|
|
|
E->path_end());
|
|
|
|
// If there's no offset, we're done.
|
|
|
|
if (!Offset) return C;
|
|
|
|
|
2010-08-23 09:21:21 +08:00
|
|
|
// If the source is a member data pointer, we have to do a null
|
|
|
|
// check and then add the offset. In the common case, we can fold
|
|
|
|
// away the offset.
|
|
|
|
if (SrcTy->isMemberDataPointer()) {
|
|
|
|
assert(C->getType() == getPtrDiffTy());
|
|
|
|
|
|
|
|
// If it's a constant int, just create a new constant int.
|
|
|
|
if (llvm::ConstantInt *CI = dyn_cast<llvm::ConstantInt>(C)) {
|
|
|
|
int64_t Src = CI->getSExtValue();
|
|
|
|
|
|
|
|
// Null converts to null.
|
|
|
|
if (Src == -1) return CI;
|
|
|
|
|
|
|
|
// Otherwise, just add the offset.
|
|
|
|
int64_t OffsetV = cast<llvm::ConstantInt>(Offset)->getSExtValue();
|
|
|
|
int64_t Dst = (DerivedToBase ? Src - OffsetV : Src + OffsetV);
|
|
|
|
return llvm::ConstantInt::get(CI->getType(), Dst, /*signed*/ true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, we have to form a constant select expression.
|
|
|
|
llvm::Constant *Null = llvm::Constant::getAllOnesValue(C->getType());
|
|
|
|
|
|
|
|
llvm::Constant *IsNull =
|
|
|
|
llvm::ConstantExpr::getICmp(llvm::ICmpInst::ICMP_EQ, C, Null);
|
|
|
|
|
|
|
|
llvm::Constant *Dst;
|
|
|
|
if (DerivedToBase)
|
|
|
|
Dst = llvm::ConstantExpr::getNSWSub(C, Offset);
|
|
|
|
else
|
|
|
|
Dst = llvm::ConstantExpr::getNSWAdd(C, Offset);
|
|
|
|
|
|
|
|
return llvm::ConstantExpr::getSelect(IsNull, Null, Dst);
|
|
|
|
}
|
|
|
|
|
2010-08-22 14:43:33 +08:00
|
|
|
// The this-adjustment is left-shifted by 1 on ARM.
|
|
|
|
if (IsARM) {
|
2010-08-23 09:21:21 +08:00
|
|
|
int64_t OffsetV = cast<llvm::ConstantInt>(Offset)->getSExtValue();
|
2010-08-22 14:43:33 +08:00
|
|
|
OffsetV <<= 1;
|
|
|
|
Offset = llvm::ConstantInt::get(Offset->getType(), OffsetV);
|
|
|
|
}
|
|
|
|
|
2010-08-22 12:16:24 +08:00
|
|
|
llvm::ConstantStruct *CS = cast<llvm::ConstantStruct>(C);
|
|
|
|
|
2010-08-23 09:21:21 +08:00
|
|
|
llvm::Constant *Values[2] = { CS->getOperand(0), 0 };
|
|
|
|
if (DerivedToBase)
|
|
|
|
Values[1] = llvm::ConstantExpr::getSub(CS->getOperand(1), Offset);
|
|
|
|
else
|
|
|
|
Values[1] = llvm::ConstantExpr::getAdd(CS->getOperand(1), Offset);
|
|
|
|
|
2010-08-22 12:16:24 +08:00
|
|
|
return llvm::ConstantStruct::get(CGM.getLLVMContext(), Values, 2,
|
|
|
|
/*Packed=*/false);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
llvm::Constant *
|
2010-08-23 09:21:21 +08:00
|
|
|
ItaniumCXXABI::EmitNullMemberPointer(const MemberPointerType *MPT) {
|
|
|
|
const llvm::Type *ptrdiff_t = getPtrDiffTy();
|
|
|
|
|
|
|
|
// Itanium C++ ABI 2.3:
|
|
|
|
// A NULL pointer is represented as -1.
|
|
|
|
if (MPT->isMemberDataPointer())
|
|
|
|
return llvm::ConstantInt::get(ptrdiff_t, -1ULL, /*isSigned=*/true);
|
2010-08-22 18:59:02 +08:00
|
|
|
|
|
|
|
llvm::Constant *Zero = llvm::ConstantInt::get(ptrdiff_t, 0);
|
|
|
|
llvm::Constant *Values[2] = { Zero, Zero };
|
|
|
|
return llvm::ConstantStruct::get(CGM.getLLVMContext(), Values, 2,
|
|
|
|
/*Packed=*/false);
|
2010-08-22 12:16:24 +08:00
|
|
|
}
|
|
|
|
|
2010-08-23 09:21:21 +08:00
|
|
|
llvm::Constant *ItaniumCXXABI::EmitMemberPointer(const FieldDecl *FD) {
|
|
|
|
// Itanium C++ ABI 2.3:
|
|
|
|
// A pointer to data member is an offset from the base address of
|
|
|
|
// the class object containing it, represented as a ptrdiff_t
|
|
|
|
|
|
|
|
QualType ClassType = CGM.getContext().getTypeDeclType(FD->getParent());
|
|
|
|
const llvm::StructType *ClassLTy =
|
|
|
|
cast<llvm::StructType>(CGM.getTypes().ConvertType(ClassType));
|
|
|
|
|
|
|
|
const CGRecordLayout &RL = CGM.getTypes().getCGRecordLayout(FD->getParent());
|
|
|
|
unsigned FieldNo = RL.getLLVMFieldNo(FD);
|
|
|
|
uint64_t Offset =
|
|
|
|
CGM.getTargetData().getStructLayout(ClassLTy)->getElementOffset(FieldNo);
|
|
|
|
|
|
|
|
return llvm::ConstantInt::get(getPtrDiffTy(), Offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Constant *ItaniumCXXABI::EmitMemberPointer(const CXXMethodDecl *MD) {
|
2010-08-22 18:59:02 +08:00
|
|
|
assert(MD->isInstance() && "Member function must not be static!");
|
|
|
|
MD = MD->getCanonicalDecl();
|
2010-08-22 14:43:33 +08:00
|
|
|
|
2010-08-22 18:59:02 +08:00
|
|
|
CodeGenTypes &Types = CGM.getTypes();
|
2010-08-23 09:21:21 +08:00
|
|
|
const llvm::Type *ptrdiff_t = getPtrDiffTy();
|
2010-08-22 14:43:33 +08:00
|
|
|
|
2010-08-22 18:59:02 +08:00
|
|
|
// Get the function pointer (or index if this is a virtual function).
|
|
|
|
llvm::Constant *MemPtr[2];
|
|
|
|
if (MD->isVirtual()) {
|
|
|
|
uint64_t Index = CGM.getVTables().getMethodVTableIndex(MD);
|
2010-08-22 14:43:33 +08:00
|
|
|
|
2010-08-22 18:59:02 +08:00
|
|
|
// FIXME: We shouldn't use / 8 here.
|
|
|
|
uint64_t PointerWidthInBytes =
|
|
|
|
CGM.getContext().Target.getPointerWidth(0) / 8;
|
|
|
|
uint64_t VTableOffset = (Index * PointerWidthInBytes);
|
|
|
|
|
|
|
|
if (IsARM) {
|
|
|
|
// ARM C++ ABI 3.2.1:
|
|
|
|
// This ABI specifies that adj contains twice the this
|
|
|
|
// adjustment, plus 1 if the member function is virtual. The
|
|
|
|
// least significant bit of adj then makes exactly the same
|
|
|
|
// discrimination as the least significant bit of ptr does for
|
|
|
|
// Itanium.
|
|
|
|
MemPtr[0] = llvm::ConstantInt::get(ptrdiff_t, VTableOffset);
|
|
|
|
MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t, 1);
|
|
|
|
} else {
|
|
|
|
// Itanium C++ ABI 2.3:
|
|
|
|
// For a virtual function, [the pointer field] is 1 plus the
|
|
|
|
// virtual table offset (in bytes) of the function,
|
|
|
|
// represented as a ptrdiff_t.
|
|
|
|
MemPtr[0] = llvm::ConstantInt::get(ptrdiff_t, VTableOffset + 1);
|
|
|
|
MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t, 0);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
|
|
|
|
const llvm::Type *Ty;
|
|
|
|
// Check whether the function has a computable LLVM signature.
|
|
|
|
if (!CodeGenTypes::VerifyFuncTypeComplete(FPT)) {
|
|
|
|
// The function has a computable LLVM signature; use the correct type.
|
|
|
|
Ty = Types.GetFunctionType(Types.getFunctionInfo(MD), FPT->isVariadic());
|
|
|
|
} else {
|
|
|
|
// Use an arbitrary non-function type to tell GetAddrOfFunction that the
|
|
|
|
// function type is incomplete.
|
|
|
|
Ty = ptrdiff_t;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Constant *Addr = CGM.GetAddrOfFunction(MD, Ty);
|
|
|
|
MemPtr[0] = llvm::ConstantExpr::getPtrToInt(Addr, ptrdiff_t);
|
|
|
|
MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return llvm::ConstantStruct::get(CGM.getLLVMContext(),
|
|
|
|
MemPtr, 2, /*Packed=*/false);
|
2010-08-22 14:43:33 +08:00
|
|
|
}
|
|
|
|
|
2010-08-22 16:30:07 +08:00
|
|
|
/// The comparison algorithm is pretty easy: the member pointers are
|
|
|
|
/// the same if they're either bitwise identical *or* both null.
|
|
|
|
///
|
|
|
|
/// ARM is different here only because null-ness is more complicated.
|
|
|
|
llvm::Value *
|
2010-08-23 09:21:21 +08:00
|
|
|
ItaniumCXXABI::EmitMemberPointerComparison(CodeGenFunction &CGF,
|
|
|
|
llvm::Value *L,
|
|
|
|
llvm::Value *R,
|
|
|
|
const MemberPointerType *MPT,
|
|
|
|
bool Inequality) {
|
2010-08-22 16:30:07 +08:00
|
|
|
CGBuilderTy &Builder = CGF.Builder;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-08-23 09:21:21 +08:00
|
|
|
// Member data pointers are easy because there's a unique null
|
|
|
|
// value, so it just comes down to bitwise equality.
|
|
|
|
if (MPT->isMemberDataPointer())
|
|
|
|
return Builder.CreateICmp(Eq, L, R);
|
|
|
|
|
|
|
|
// For member function pointers, the tautologies are more complex.
|
|
|
|
// The Itanium tautology is:
|
2010-08-23 14:56:36 +08:00
|
|
|
// (L == R) <==> (L.ptr == R.ptr && (L.ptr == 0 || L.adj == R.adj))
|
2010-08-23 09:21:21 +08:00
|
|
|
// The ARM tautology is:
|
2010-08-23 14:56:36 +08:00
|
|
|
// (L == R) <==> (L.ptr == R.ptr &&
|
|
|
|
// (L.adj == R.adj ||
|
|
|
|
// (L.ptr == 0 && ((L.adj|R.adj) & 1) == 0)))
|
2010-08-23 09:21:21 +08:00
|
|
|
// The inequality tautologies have exactly the same structure, except
|
|
|
|
// applying De Morgan's laws.
|
|
|
|
|
|
|
|
llvm::Value *LPtr = Builder.CreateExtractValue(L, 0, "lhs.memptr.ptr");
|
|
|
|
llvm::Value *RPtr = Builder.CreateExtractValue(R, 0, "rhs.memptr.ptr");
|
|
|
|
|
2010-08-22 16:30:07 +08:00
|
|
|
// This condition tests whether L.ptr == R.ptr. This must always be
|
|
|
|
// true for equality to hold.
|
|
|
|
llvm::Value *PtrEq = Builder.CreateICmp(Eq, LPtr, RPtr, "cmp.ptr");
|
|
|
|
|
|
|
|
// This condition, together with the assumption that L.ptr == R.ptr,
|
|
|
|
// tests whether the pointers are both null. ARM imposes an extra
|
|
|
|
// condition.
|
|
|
|
llvm::Value *Zero = llvm::Constant::getNullValue(LPtr->getType());
|
|
|
|
llvm::Value *EqZero = Builder.CreateICmp(Eq, LPtr, Zero, "cmp.ptr.null");
|
|
|
|
|
|
|
|
// This condition tests whether L.adj == R.adj. If this isn't
|
|
|
|
// true, the pointers are unequal unless they're both null.
|
2010-08-22 18:59:02 +08:00
|
|
|
llvm::Value *LAdj = Builder.CreateExtractValue(L, 1, "lhs.memptr.adj");
|
|
|
|
llvm::Value *RAdj = Builder.CreateExtractValue(R, 1, "rhs.memptr.adj");
|
2010-08-22 16:30:07 +08:00
|
|
|
llvm::Value *AdjEq = Builder.CreateICmp(Eq, LAdj, RAdj, "cmp.adj");
|
|
|
|
|
|
|
|
// Null member function pointers on ARM clear the low bit of Adj,
|
|
|
|
// so the zero condition has to check that neither low bit is set.
|
|
|
|
if (IsARM) {
|
|
|
|
llvm::Value *One = llvm::ConstantInt::get(LPtr->getType(), 1);
|
|
|
|
|
|
|
|
// Compute (l.adj | r.adj) & 1 and test it against zero.
|
|
|
|
llvm::Value *OrAdj = Builder.CreateOr(LAdj, RAdj, "or.adj");
|
|
|
|
llvm::Value *OrAdjAnd1 = Builder.CreateAnd(OrAdj, One);
|
|
|
|
llvm::Value *OrAdjAnd1EqZero = Builder.CreateICmp(Eq, OrAdjAnd1, Zero,
|
|
|
|
"cmp.or.adj");
|
|
|
|
EqZero = Builder.CreateBinOp(And, EqZero, OrAdjAnd1EqZero);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tie together all our conditions.
|
|
|
|
llvm::Value *Result = Builder.CreateBinOp(Or, EqZero, AdjEq);
|
|
|
|
Result = Builder.CreateBinOp(And, PtrEq, Result,
|
|
|
|
Inequality ? "memptr.ne" : "memptr.eq");
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Value *
|
2010-08-23 09:21:21 +08:00
|
|
|
ItaniumCXXABI::EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
|
|
|
|
llvm::Value *MemPtr,
|
|
|
|
const MemberPointerType *MPT) {
|
2010-08-22 16:30:07 +08:00
|
|
|
CGBuilderTy &Builder = CGF.Builder;
|
2010-08-23 09:21:21 +08:00
|
|
|
|
|
|
|
/// For member data pointers, this is just a check against -1.
|
|
|
|
if (MPT->isMemberDataPointer()) {
|
|
|
|
assert(MemPtr->getType() == getPtrDiffTy());
|
|
|
|
llvm::Value *NegativeOne =
|
|
|
|
llvm::Constant::getAllOnesValue(MemPtr->getType());
|
|
|
|
return Builder.CreateICmpNE(MemPtr, NegativeOne, "memptr.tobool");
|
|
|
|
}
|
2010-08-22 16:30:07 +08:00
|
|
|
|
|
|
|
// In Itanium, a member function pointer is null if 'ptr' is null.
|
2010-08-22 18:59:02 +08:00
|
|
|
llvm::Value *Ptr = Builder.CreateExtractValue(MemPtr, 0, "memptr.ptr");
|
2010-08-22 16:30:07 +08:00
|
|
|
|
|
|
|
llvm::Constant *Zero = llvm::ConstantInt::get(Ptr->getType(), 0);
|
|
|
|
llvm::Value *Result = Builder.CreateICmpNE(Ptr, Zero, "memptr.tobool");
|
|
|
|
|
|
|
|
// In ARM, it's that, plus the low bit of 'adj' must be zero.
|
|
|
|
if (IsARM) {
|
|
|
|
llvm::Constant *One = llvm::ConstantInt::get(Ptr->getType(), 1);
|
2010-08-22 18:59:02 +08:00
|
|
|
llvm::Value *Adj = Builder.CreateExtractValue(MemPtr, 1, "memptr.adj");
|
2010-08-22 16:30:07 +08:00
|
|
|
llvm::Value *VirtualBit = Builder.CreateAnd(Adj, One, "memptr.virtualbit");
|
|
|
|
llvm::Value *IsNotVirtual = Builder.CreateICmpEQ(VirtualBit, Zero,
|
|
|
|
"memptr.notvirtual");
|
|
|
|
Result = Builder.CreateAnd(Result, IsNotVirtual);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Result;
|
|
|
|
}
|
2010-08-22 14:43:33 +08:00
|
|
|
|
2010-08-23 05:01:12 +08:00
|
|
|
/// The Itanium ABI requires non-zero initialization only for data
|
|
|
|
/// member pointers, for which '0' is a valid offset.
|
|
|
|
bool ItaniumCXXABI::isZeroInitializable(const MemberPointerType *MPT) {
|
|
|
|
return MPT->getPointeeType()->isFunctionType();
|
2010-08-22 12:16:24 +08:00
|
|
|
}
|
2010-08-31 15:33:07 +08:00
|
|
|
|
|
|
|
/// The generic ABI passes 'this', plus a VTT if it's initializing a
|
|
|
|
/// base subobject.
|
|
|
|
void ItaniumCXXABI::BuildConstructorSignature(const CXXConstructorDecl *Ctor,
|
|
|
|
CXXCtorType Type,
|
|
|
|
CanQualType &ResTy,
|
|
|
|
llvm::SmallVectorImpl<CanQualType> &ArgTys) {
|
|
|
|
ASTContext &Context = CGM.getContext();
|
|
|
|
|
|
|
|
// 'this' is already there.
|
|
|
|
|
|
|
|
// Check if we need to add a VTT parameter (which has type void **).
|
|
|
|
if (Type == Ctor_Base && Ctor->getParent()->getNumVBases() != 0)
|
|
|
|
ArgTys.push_back(Context.getPointerType(Context.VoidPtrTy));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The ARM ABI does the same as the Itanium ABI, but returns 'this'.
|
|
|
|
void ARMCXXABI::BuildConstructorSignature(const CXXConstructorDecl *Ctor,
|
|
|
|
CXXCtorType Type,
|
|
|
|
CanQualType &ResTy,
|
|
|
|
llvm::SmallVectorImpl<CanQualType> &ArgTys) {
|
|
|
|
ItaniumCXXABI::BuildConstructorSignature(Ctor, Type, ResTy, ArgTys);
|
|
|
|
ResTy = ArgTys[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The generic ABI passes 'this', plus a VTT if it's destroying a
|
|
|
|
/// base subobject.
|
|
|
|
void ItaniumCXXABI::BuildDestructorSignature(const CXXDestructorDecl *Dtor,
|
|
|
|
CXXDtorType Type,
|
|
|
|
CanQualType &ResTy,
|
|
|
|
llvm::SmallVectorImpl<CanQualType> &ArgTys) {
|
|
|
|
ASTContext &Context = CGM.getContext();
|
|
|
|
|
|
|
|
// 'this' is already there.
|
|
|
|
|
|
|
|
// Check if we need to add a VTT parameter (which has type void **).
|
|
|
|
if (Type == Dtor_Base && Dtor->getParent()->getNumVBases() != 0)
|
|
|
|
ArgTys.push_back(Context.getPointerType(Context.VoidPtrTy));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The ARM ABI does the same as the Itanium ABI, but returns 'this'
|
|
|
|
/// for non-deleting destructors.
|
|
|
|
void ARMCXXABI::BuildDestructorSignature(const CXXDestructorDecl *Dtor,
|
|
|
|
CXXDtorType Type,
|
|
|
|
CanQualType &ResTy,
|
|
|
|
llvm::SmallVectorImpl<CanQualType> &ArgTys) {
|
|
|
|
ItaniumCXXABI::BuildDestructorSignature(Dtor, Type, ResTy, ArgTys);
|
|
|
|
|
|
|
|
if (Type != Dtor_Deleting)
|
|
|
|
ResTy = ArgTys[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
void ItaniumCXXABI::BuildInstanceFunctionParams(CodeGenFunction &CGF,
|
|
|
|
QualType &ResTy,
|
|
|
|
FunctionArgList &Params) {
|
|
|
|
/// Create the 'this' variable.
|
|
|
|
BuildThisParam(CGF, Params);
|
|
|
|
|
|
|
|
const CXXMethodDecl *MD = cast<CXXMethodDecl>(CGF.CurGD.getDecl());
|
|
|
|
assert(MD->isInstance());
|
|
|
|
|
|
|
|
// Check if we need a VTT parameter as well.
|
|
|
|
if (CodeGenVTables::needsVTTParameter(CGF.CurGD)) {
|
|
|
|
ASTContext &Context = CGF.getContext();
|
|
|
|
|
|
|
|
// FIXME: avoid the fake decl
|
|
|
|
QualType T = Context.getPointerType(Context.VoidPtrTy);
|
|
|
|
ImplicitParamDecl *VTTDecl
|
|
|
|
= ImplicitParamDecl::Create(Context, 0, MD->getLocation(),
|
|
|
|
&Context.Idents.get("vtt"), T);
|
|
|
|
Params.push_back(std::make_pair(VTTDecl, VTTDecl->getType()));
|
|
|
|
getVTTDecl(CGF) = VTTDecl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ARMCXXABI::BuildInstanceFunctionParams(CodeGenFunction &CGF,
|
|
|
|
QualType &ResTy,
|
|
|
|
FunctionArgList &Params) {
|
|
|
|
ItaniumCXXABI::BuildInstanceFunctionParams(CGF, ResTy, Params);
|
|
|
|
|
|
|
|
// Return 'this' from certain constructors and destructors.
|
|
|
|
if (HasThisReturn(CGF.CurGD))
|
|
|
|
ResTy = Params[0].second;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ItaniumCXXABI::EmitInstanceFunctionProlog(CodeGenFunction &CGF) {
|
|
|
|
/// Initialize the 'this' slot.
|
|
|
|
EmitThisParam(CGF);
|
|
|
|
|
|
|
|
/// Initialize the 'vtt' slot if needed.
|
|
|
|
if (getVTTDecl(CGF)) {
|
|
|
|
getVTTValue(CGF)
|
|
|
|
= CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(getVTTDecl(CGF)),
|
|
|
|
"vtt");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ARMCXXABI::EmitInstanceFunctionProlog(CodeGenFunction &CGF) {
|
|
|
|
ItaniumCXXABI::EmitInstanceFunctionProlog(CGF);
|
|
|
|
|
|
|
|
/// Initialize the return slot to 'this' at the start of the
|
|
|
|
/// function.
|
|
|
|
if (HasThisReturn(CGF.CurGD))
|
|
|
|
CGF.Builder.CreateStore(CGF.LoadCXXThis(), CGF.ReturnValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ARMCXXABI::EmitReturnFromThunk(CodeGenFunction &CGF,
|
|
|
|
RValue RV, QualType ResultType) {
|
|
|
|
if (!isa<CXXDestructorDecl>(CGF.CurGD.getDecl()))
|
|
|
|
return ItaniumCXXABI::EmitReturnFromThunk(CGF, RV, ResultType);
|
|
|
|
|
|
|
|
// Destructor thunks in the ARM ABI have indeterminate results.
|
|
|
|
const llvm::Type *T =
|
|
|
|
cast<llvm::PointerType>(CGF.ReturnValue->getType())->getElementType();
|
|
|
|
RValue Undef = RValue::get(llvm::UndefValue::get(T));
|
|
|
|
return ItaniumCXXABI::EmitReturnFromThunk(CGF, Undef, ResultType);
|
|
|
|
}
|
2010-09-02 17:58:18 +08:00
|
|
|
|
|
|
|
/************************** Array allocation cookies **************************/
|
|
|
|
|
|
|
|
bool ItaniumCXXABI::NeedsArrayCookie(QualType ElementType) {
|
|
|
|
ElementType = CGM.getContext().getBaseElementType(ElementType);
|
|
|
|
const RecordType *RT = ElementType->getAs<RecordType>();
|
|
|
|
if (!RT) return false;
|
|
|
|
|
|
|
|
const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
|
|
|
|
|
|
|
|
// If the class has a non-trivial destructor, it always needs a cookie.
|
|
|
|
if (!RD->hasTrivialDestructor()) return true;
|
|
|
|
|
|
|
|
// If the class's usual deallocation function takes two arguments,
|
|
|
|
// it needs a cookie. Otherwise we don't need a cookie.
|
|
|
|
const CXXMethodDecl *UsualDeallocationFunction = 0;
|
|
|
|
|
|
|
|
// Usual deallocation functions of this form are always found on the
|
|
|
|
// class.
|
|
|
|
//
|
|
|
|
// FIXME: what exactly is this code supposed to do if there's an
|
|
|
|
// ambiguity? That's possible with using declarations.
|
|
|
|
DeclarationName OpName =
|
|
|
|
CGM.getContext().DeclarationNames.getCXXOperatorName(OO_Array_Delete);
|
|
|
|
DeclContext::lookup_const_iterator Op, OpEnd;
|
|
|
|
for (llvm::tie(Op, OpEnd) = RD->lookup(OpName); Op != OpEnd; ++Op) {
|
|
|
|
const CXXMethodDecl *Delete =
|
|
|
|
cast<CXXMethodDecl>((*Op)->getUnderlyingDecl());
|
|
|
|
|
|
|
|
if (Delete->isUsualDeallocationFunction()) {
|
|
|
|
UsualDeallocationFunction = Delete;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// No usual deallocation function, we don't need a cookie.
|
|
|
|
if (!UsualDeallocationFunction)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// The usual deallocation function doesn't take a size_t argument,
|
|
|
|
// so we don't need a cookie.
|
|
|
|
if (UsualDeallocationFunction->getNumParams() == 1)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
assert(UsualDeallocationFunction->getNumParams() == 2 &&
|
|
|
|
"Unexpected deallocation function type!");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
CharUnits ItaniumCXXABI::GetArrayCookieSize(QualType ElementType) {
|
|
|
|
if (!NeedsArrayCookie(ElementType))
|
|
|
|
return CharUnits::Zero();
|
|
|
|
|
|
|
|
// Padding is the maximum of sizeof(size_t) and alignof(ElementType)
|
|
|
|
ASTContext &Ctx = CGM.getContext();
|
|
|
|
return std::max(Ctx.getTypeSizeInChars(Ctx.getSizeType()),
|
|
|
|
Ctx.getTypeAlignInChars(ElementType));
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Value *ItaniumCXXABI::InitializeArrayCookie(CodeGenFunction &CGF,
|
|
|
|
llvm::Value *NewPtr,
|
|
|
|
llvm::Value *NumElements,
|
|
|
|
QualType ElementType) {
|
|
|
|
assert(NeedsArrayCookie(ElementType));
|
|
|
|
|
|
|
|
unsigned AS = cast<llvm::PointerType>(NewPtr->getType())->getAddressSpace();
|
|
|
|
|
|
|
|
ASTContext &Ctx = CGM.getContext();
|
|
|
|
QualType SizeTy = Ctx.getSizeType();
|
|
|
|
CharUnits SizeSize = Ctx.getTypeSizeInChars(SizeTy);
|
|
|
|
|
|
|
|
// The size of the cookie.
|
|
|
|
CharUnits CookieSize =
|
|
|
|
std::max(SizeSize, Ctx.getTypeAlignInChars(ElementType));
|
|
|
|
|
|
|
|
// Compute an offset to the cookie.
|
|
|
|
llvm::Value *CookiePtr = NewPtr;
|
|
|
|
CharUnits CookieOffset = CookieSize - SizeSize;
|
|
|
|
if (!CookieOffset.isZero())
|
|
|
|
CookiePtr = CGF.Builder.CreateConstInBoundsGEP1_64(CookiePtr,
|
|
|
|
CookieOffset.getQuantity());
|
|
|
|
|
|
|
|
// Write the number of elements into the appropriate slot.
|
|
|
|
llvm::Value *NumElementsPtr
|
|
|
|
= CGF.Builder.CreateBitCast(CookiePtr,
|
|
|
|
CGF.ConvertType(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());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ItaniumCXXABI::ReadArrayCookie(CodeGenFunction &CGF,
|
|
|
|
llvm::Value *Ptr,
|
|
|
|
QualType ElementType,
|
|
|
|
llvm::Value *&NumElements,
|
|
|
|
llvm::Value *&AllocPtr,
|
|
|
|
CharUnits &CookieSize) {
|
|
|
|
// Derive a char* in the same address space as the pointer.
|
|
|
|
unsigned AS = cast<llvm::PointerType>(Ptr->getType())->getAddressSpace();
|
|
|
|
const llvm::Type *CharPtrTy = CGF.Builder.getInt8Ty()->getPointerTo(AS);
|
|
|
|
|
|
|
|
// If we don't need an array cookie, bail out early.
|
|
|
|
if (!NeedsArrayCookie(ElementType)) {
|
|
|
|
AllocPtr = CGF.Builder.CreateBitCast(Ptr, CharPtrTy);
|
|
|
|
NumElements = 0;
|
|
|
|
CookieSize = CharUnits::Zero();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QualType SizeTy = CGF.getContext().getSizeType();
|
|
|
|
CharUnits SizeSize = CGF.getContext().getTypeSizeInChars(SizeTy);
|
|
|
|
const llvm::Type *SizeLTy = CGF.ConvertType(SizeTy);
|
|
|
|
|
|
|
|
CookieSize
|
|
|
|
= std::max(SizeSize, CGF.getContext().getTypeAlignInChars(ElementType));
|
|
|
|
|
|
|
|
CharUnits NumElementsOffset = CookieSize - SizeSize;
|
|
|
|
|
|
|
|
// Compute the allocated pointer.
|
|
|
|
AllocPtr = CGF.Builder.CreateBitCast(Ptr, CharPtrTy);
|
|
|
|
AllocPtr = CGF.Builder.CreateConstInBoundsGEP1_64(AllocPtr,
|
|
|
|
-CookieSize.getQuantity());
|
|
|
|
|
|
|
|
llvm::Value *NumElementsPtr = AllocPtr;
|
|
|
|
if (!NumElementsOffset.isZero())
|
|
|
|
NumElementsPtr =
|
|
|
|
CGF.Builder.CreateConstInBoundsGEP1_64(NumElementsPtr,
|
|
|
|
NumElementsOffset.getQuantity());
|
|
|
|
NumElementsPtr =
|
|
|
|
CGF.Builder.CreateBitCast(NumElementsPtr, SizeLTy->getPointerTo(AS));
|
|
|
|
NumElements = CGF.Builder.CreateLoad(NumElementsPtr);
|
|
|
|
}
|
|
|
|
|
|
|
|
CharUnits ARMCXXABI::GetArrayCookieSize(QualType ElementType) {
|
|
|
|
if (!NeedsArrayCookie(ElementType))
|
|
|
|
return CharUnits::Zero();
|
|
|
|
|
|
|
|
// On ARM, the cookie is always:
|
|
|
|
// struct array_cookie {
|
|
|
|
// std::size_t element_size; // element_size != 0
|
|
|
|
// std::size_t element_count;
|
|
|
|
// };
|
|
|
|
// TODO: what should we do if the allocated type actually wants
|
|
|
|
// greater alignment?
|
|
|
|
return getContext().getTypeSizeInChars(getContext().getSizeType()) * 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Value *ARMCXXABI::InitializeArrayCookie(CodeGenFunction &CGF,
|
|
|
|
llvm::Value *NewPtr,
|
|
|
|
llvm::Value *NumElements,
|
|
|
|
QualType ElementType) {
|
|
|
|
assert(NeedsArrayCookie(ElementType));
|
|
|
|
|
|
|
|
// NewPtr is a char*.
|
|
|
|
|
|
|
|
unsigned AS = cast<llvm::PointerType>(NewPtr->getType())->getAddressSpace();
|
|
|
|
|
|
|
|
ASTContext &Ctx = CGM.getContext();
|
|
|
|
CharUnits SizeSize = Ctx.getTypeSizeInChars(Ctx.getSizeType());
|
|
|
|
const llvm::IntegerType *SizeTy =
|
|
|
|
cast<llvm::IntegerType>(CGF.ConvertType(Ctx.getSizeType()));
|
|
|
|
|
|
|
|
// The cookie is always at the start of the buffer.
|
|
|
|
llvm::Value *CookiePtr = NewPtr;
|
|
|
|
|
|
|
|
// The first element is the element size.
|
|
|
|
CookiePtr = CGF.Builder.CreateBitCast(CookiePtr, SizeTy->getPointerTo(AS));
|
|
|
|
llvm::Value *ElementSize = llvm::ConstantInt::get(SizeTy,
|
|
|
|
Ctx.getTypeSizeInChars(ElementType).getQuantity());
|
|
|
|
CGF.Builder.CreateStore(ElementSize, CookiePtr);
|
|
|
|
|
|
|
|
// The second element is the element count.
|
|
|
|
CookiePtr = CGF.Builder.CreateConstInBoundsGEP1_32(CookiePtr, 1);
|
|
|
|
CGF.Builder.CreateStore(NumElements, CookiePtr);
|
|
|
|
|
|
|
|
// Finally, compute a pointer to the actual data buffer by skipping
|
|
|
|
// over the cookie completely.
|
|
|
|
CharUnits CookieSize = 2 * SizeSize;
|
|
|
|
return CGF.Builder.CreateConstInBoundsGEP1_64(NewPtr,
|
|
|
|
CookieSize.getQuantity());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ARMCXXABI::ReadArrayCookie(CodeGenFunction &CGF,
|
|
|
|
llvm::Value *Ptr,
|
|
|
|
QualType ElementType,
|
|
|
|
llvm::Value *&NumElements,
|
|
|
|
llvm::Value *&AllocPtr,
|
|
|
|
CharUnits &CookieSize) {
|
|
|
|
// Derive a char* in the same address space as the pointer.
|
|
|
|
unsigned AS = cast<llvm::PointerType>(Ptr->getType())->getAddressSpace();
|
|
|
|
const llvm::Type *CharPtrTy = CGF.Builder.getInt8Ty()->getPointerTo(AS);
|
|
|
|
|
|
|
|
// If we don't need an array cookie, bail out early.
|
|
|
|
if (!NeedsArrayCookie(ElementType)) {
|
|
|
|
AllocPtr = CGF.Builder.CreateBitCast(Ptr, CharPtrTy);
|
|
|
|
NumElements = 0;
|
|
|
|
CookieSize = CharUnits::Zero();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QualType SizeTy = CGF.getContext().getSizeType();
|
|
|
|
CharUnits SizeSize = CGF.getContext().getTypeSizeInChars(SizeTy);
|
|
|
|
const llvm::Type *SizeLTy = CGF.ConvertType(SizeTy);
|
|
|
|
|
|
|
|
// The cookie size is always 2 * sizeof(size_t).
|
|
|
|
CookieSize = 2 * SizeSize;
|
|
|
|
CharUnits NumElementsOffset = CookieSize - SizeSize;
|
|
|
|
|
|
|
|
// The allocated pointer is the input ptr, minus that amount.
|
|
|
|
AllocPtr = CGF.Builder.CreateBitCast(Ptr, CharPtrTy);
|
|
|
|
AllocPtr = CGF.Builder.CreateConstInBoundsGEP1_64(AllocPtr,
|
|
|
|
-CookieSize.getQuantity());
|
|
|
|
|
|
|
|
// The number of elements is at offset sizeof(size_t) relative to that.
|
|
|
|
llvm::Value *NumElementsPtr
|
|
|
|
= CGF.Builder.CreateConstInBoundsGEP1_64(AllocPtr,
|
|
|
|
SizeSize.getQuantity());
|
|
|
|
NumElementsPtr =
|
|
|
|
CGF.Builder.CreateBitCast(NumElementsPtr, SizeLTy->getPointerTo(AS));
|
|
|
|
NumElements = CGF.Builder.CreateLoad(NumElementsPtr);
|
|
|
|
}
|
|
|
|
|