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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2011-04-15 13:22:18 +08:00
|
|
|
// This provides C++ code generation targeting the Itanium C++ ABI. The class
|
2010-05-26 03:52:27 +08:00
|
|
|
// 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"
|
2012-06-24 07:44:00 +08:00
|
|
|
#include "CGVTables.h"
|
2010-08-22 08:05:51 +08:00
|
|
|
#include "CodeGenFunction.h"
|
2010-05-26 03:52:27 +08:00
|
|
|
#include "CodeGenModule.h"
|
2012-09-16 02:47:51 +08:00
|
|
|
#include "clang/AST/Mangle.h"
|
|
|
|
#include "clang/AST/Type.h"
|
|
|
|
#include "llvm/Intrinsics.h"
|
2012-10-09 00:25:52 +08:00
|
|
|
#include "llvm/DataLayout.h"
|
2012-09-16 02:47: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:
|
2011-07-10 01:41:47 +08:00
|
|
|
llvm::IntegerType *PtrDiffTy;
|
2010-08-22 08:05:51 +08:00
|
|
|
protected:
|
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.
|
2011-07-10 01:41:47 +08:00
|
|
|
llvm::IntegerType *getPtrDiffTy() {
|
2010-08-23 09:21:21 +08:00
|
|
|
if (!PtrDiffTy) {
|
2010-09-02 18:25:57 +08:00
|
|
|
QualType T = getContext().getPointerDiffType();
|
2011-07-10 01:41:47 +08:00
|
|
|
llvm::Type *Ty = CGM.getTypes().ConvertType(T);
|
2010-08-23 09:21:21 +08:00
|
|
|
PtrDiffTy = cast<llvm::IntegerType>(Ty);
|
|
|
|
}
|
|
|
|
return PtrDiffTy;
|
|
|
|
}
|
|
|
|
|
2010-05-26 03:52:27 +08:00
|
|
|
public:
|
2010-08-22 08:59:17 +08:00
|
|
|
ItaniumCXXABI(CodeGen::CodeGenModule &CGM, bool IsARM = false) :
|
2011-01-14 02:57:25 +08:00
|
|
|
CGCXXABI(CGM), PtrDiffTy(0), IsARM(IsARM) { }
|
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
|
|
|
|
2011-07-10 01:41:47 +08:00
|
|
|
llvm::Type *ConvertMemberPointerType(const MemberPointerType *MPT);
|
2010-08-23 09:21:21 +08:00
|
|
|
|
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);
|
2012-02-15 09:22:51 +08:00
|
|
|
llvm::Constant *EmitMemberPointerConversion(const CastExpr *E,
|
|
|
|
llvm::Constant *Src);
|
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
|
|
|
|
2011-04-12 08:42:48 +08:00
|
|
|
llvm::Constant *EmitMemberPointer(const CXXMethodDecl *MD);
|
2011-02-03 16:15:49 +08:00
|
|
|
llvm::Constant *EmitMemberDataPointer(const MemberPointerType *MPT,
|
|
|
|
CharUnits offset);
|
2012-01-14 12:30:29 +08:00
|
|
|
llvm::Constant *EmitMemberPointer(const APValue &MP, QualType MPT);
|
|
|
|
llvm::Constant *BuildMemberPointer(const CXXMethodDecl *MD,
|
|
|
|
CharUnits ThisAdjustment);
|
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
|
|
|
|
2012-09-25 18:10:39 +08:00
|
|
|
llvm::Value *adjustToCompleteObject(CodeGenFunction &CGF,
|
|
|
|
llvm::Value *ptr,
|
|
|
|
QualType type);
|
|
|
|
|
2010-08-31 15:33:07 +08:00
|
|
|
void BuildConstructorSignature(const CXXConstructorDecl *Ctor,
|
|
|
|
CXXCtorType T,
|
|
|
|
CanQualType &ResTy,
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVectorImpl<CanQualType> &ArgTys);
|
2010-08-31 15:33:07 +08:00
|
|
|
|
|
|
|
void BuildDestructorSignature(const CXXDestructorDecl *Dtor,
|
|
|
|
CXXDtorType T,
|
|
|
|
CanQualType &ResTy,
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVectorImpl<CanQualType> &ArgTys);
|
2010-08-31 15:33:07 +08:00
|
|
|
|
|
|
|
void BuildInstanceFunctionParams(CodeGenFunction &CGF,
|
|
|
|
QualType &ResTy,
|
|
|
|
FunctionArgList &Params);
|
|
|
|
|
|
|
|
void EmitInstanceFunctionProlog(CodeGenFunction &CGF);
|
2010-09-02 17:58:18 +08:00
|
|
|
|
2012-07-18 01:10:11 +08:00
|
|
|
StringRef GetPureVirtualCallName() { return "__cxa_pure_virtual"; }
|
2012-10-17 06:56:05 +08:00
|
|
|
StringRef GetDeletedVirtualCallName() { return "__cxa_deleted_virtual"; }
|
2012-07-18 01:10:11 +08:00
|
|
|
|
2012-05-01 13:23:51 +08:00
|
|
|
CharUnits getArrayCookieSizeImpl(QualType elementType);
|
2010-09-02 17:58:18 +08:00
|
|
|
llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF,
|
|
|
|
llvm::Value *NewPtr,
|
|
|
|
llvm::Value *NumElements,
|
2011-01-27 17:37:56 +08:00
|
|
|
const CXXNewExpr *expr,
|
2010-09-02 17:58:18 +08:00
|
|
|
QualType ElementType);
|
2012-05-01 13:23:51 +08:00
|
|
|
llvm::Value *readArrayCookieImpl(CodeGenFunction &CGF,
|
|
|
|
llvm::Value *allocPtr,
|
|
|
|
CharUnits cookieSize);
|
2010-09-08 09:44:27 +08:00
|
|
|
|
2010-11-06 17:44:32 +08:00
|
|
|
void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D,
|
2012-03-31 03:44:53 +08:00
|
|
|
llvm::GlobalVariable *DeclPtr, bool PerformInit);
|
2012-05-01 14:13:13 +08:00
|
|
|
void registerGlobalDtor(CodeGenFunction &CGF, llvm::Constant *dtor,
|
|
|
|
llvm::Constant *addr);
|
2012-06-24 07:44:00 +08:00
|
|
|
|
|
|
|
void EmitVTables(const CXXRecordDecl *Class);
|
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,
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVectorImpl<CanQualType> &ArgTys);
|
2010-08-31 15:33:07 +08:00
|
|
|
|
|
|
|
void BuildDestructorSignature(const CXXDestructorDecl *Dtor,
|
|
|
|
CXXDtorType T,
|
|
|
|
CanQualType &ResTy,
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVectorImpl<CanQualType> &ArgTys);
|
2010-08-31 15:33:07 +08:00
|
|
|
|
|
|
|
void BuildInstanceFunctionParams(CodeGenFunction &CGF,
|
|
|
|
QualType &ResTy,
|
|
|
|
FunctionArgList &Params);
|
|
|
|
|
|
|
|
void EmitInstanceFunctionProlog(CodeGenFunction &CGF);
|
|
|
|
|
|
|
|
void EmitReturnFromThunk(CodeGenFunction &CGF, RValue RV, QualType ResTy);
|
|
|
|
|
2012-05-01 13:23:51 +08:00
|
|
|
CharUnits getArrayCookieSizeImpl(QualType elementType);
|
2010-09-02 17:58:18 +08:00
|
|
|
llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF,
|
|
|
|
llvm::Value *NewPtr,
|
|
|
|
llvm::Value *NumElements,
|
2011-01-27 17:37:56 +08:00
|
|
|
const CXXNewExpr *expr,
|
2010-09-02 17:58:18 +08:00
|
|
|
QualType ElementType);
|
2012-05-01 13:23:51 +08:00
|
|
|
llvm::Value *readArrayCookieImpl(CodeGenFunction &CGF, 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);
|
|
|
|
}
|
|
|
|
|
2011-07-10 01:41:47 +08:00
|
|
|
llvm::Type *
|
2010-08-23 09:21:21 +08:00
|
|
|
ItaniumCXXABI::ConvertMemberPointerType(const MemberPointerType *MPT) {
|
|
|
|
if (MPT->isMemberDataPointer())
|
|
|
|
return getPtrDiffTy();
|
2011-06-19 06:49:11 +08:00
|
|
|
return llvm::StructType::get(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());
|
|
|
|
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::FunctionType *FTy =
|
2012-02-17 11:33:10 +08:00
|
|
|
CGM.getTypes().GetFunctionType(
|
|
|
|
CGM.getTypes().arrangeCXXMethodType(RD, FPT));
|
2010-08-22 08:05:51 +08:00
|
|
|
|
2011-07-18 12:24:23 +08:00
|
|
|
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.
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *VTableTy = Builder.getInt8PtrTy();
|
2010-08-22 08:05:51 +08:00
|
|
|
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);
|
2011-03-30 19:28:58 +08:00
|
|
|
llvm::PHINode *Callee = Builder.CreatePHI(FTy->getPointerTo(), 2);
|
2010-08-22 08:05:51 +08:00
|
|
|
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;
|
|
|
|
|
2012-10-25 23:39:14 +08:00
|
|
|
unsigned AS = Base->getType()->getPointerAddressSpace();
|
2010-09-01 05:07:20 +08:00
|
|
|
|
|
|
|
// 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.
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *PType
|
2010-09-03 01:38:50 +08:00
|
|
|
= CGF.ConvertTypeForMem(MPT->getPointeeType())->getPointerTo(AS);
|
2010-09-01 05:07:20 +08:00
|
|
|
return Builder.CreateBitCast(Addr, PType);
|
|
|
|
}
|
|
|
|
|
2012-02-15 09:22:51 +08:00
|
|
|
/// Perform a bitcast, derived-to-base, or base-to-derived member pointer
|
|
|
|
/// conversion.
|
|
|
|
///
|
|
|
|
/// Bitcast conversions are always a no-op under Itanium.
|
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,
|
2012-02-15 09:22:51 +08:00
|
|
|
llvm::Value *src) {
|
2010-08-25 19:45:40 +08:00
|
|
|
assert(E->getCastKind() == CK_DerivedToBaseMemberPointer ||
|
2012-02-15 09:22:51 +08:00
|
|
|
E->getCastKind() == CK_BaseToDerivedMemberPointer ||
|
|
|
|
E->getCastKind() == CK_ReinterpretMemberPointer);
|
|
|
|
|
|
|
|
// Under Itanium, reinterprets don't require any additional processing.
|
|
|
|
if (E->getCastKind() == CK_ReinterpretMemberPointer) return src;
|
|
|
|
|
|
|
|
// Use constant emission if we can.
|
|
|
|
if (isa<llvm::Constant>(src))
|
|
|
|
return EmitMemberPointerConversion(E, cast<llvm::Constant>(src));
|
|
|
|
|
|
|
|
llvm::Constant *adj = getMemberPointerAdjustment(E);
|
|
|
|
if (!adj) return src;
|
2010-08-22 11:04:22 +08:00
|
|
|
|
|
|
|
CGBuilderTy &Builder = CGF.Builder;
|
2012-02-15 09:22:51 +08:00
|
|
|
bool isDerivedToBase = (E->getCastKind() == CK_DerivedToBaseMemberPointer);
|
|
|
|
|
|
|
|
const MemberPointerType *destTy =
|
|
|
|
E->getType()->castAs<MemberPointerType>();
|
2010-08-22 11:04:22 +08:00
|
|
|
|
2012-02-15 09:22:51 +08:00
|
|
|
// For member data pointers, this is just a matter of adding the
|
|
|
|
// offset if the source is non-null.
|
|
|
|
if (destTy->isMemberDataPointer()) {
|
|
|
|
llvm::Value *dst;
|
|
|
|
if (isDerivedToBase)
|
|
|
|
dst = Builder.CreateNSWSub(src, adj, "adj");
|
|
|
|
else
|
|
|
|
dst = Builder.CreateNSWAdd(src, adj, "adj");
|
2010-08-22 11:04:22 +08:00
|
|
|
|
2012-02-15 09:22:51 +08:00
|
|
|
// 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 11:04:22 +08:00
|
|
|
|
2012-02-15 09:22:51 +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 11:04:22 +08:00
|
|
|
|
2012-02-15 09:22:51 +08:00
|
|
|
llvm::Value *srcAdj = Builder.CreateExtractValue(src, 1, "src.adj");
|
|
|
|
llvm::Value *dstAdj;
|
|
|
|
if (isDerivedToBase)
|
|
|
|
dstAdj = Builder.CreateNSWSub(srcAdj, adj, "adj");
|
2010-08-22 11:04:22 +08:00
|
|
|
else
|
2012-02-15 09:22:51 +08:00
|
|
|
dstAdj = Builder.CreateNSWAdd(srcAdj, adj, "adj");
|
|
|
|
|
|
|
|
return Builder.CreateInsertValue(src, dstAdj, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Constant *
|
|
|
|
ItaniumCXXABI::EmitMemberPointerConversion(const CastExpr *E,
|
|
|
|
llvm::Constant *src) {
|
|
|
|
assert(E->getCastKind() == CK_DerivedToBaseMemberPointer ||
|
|
|
|
E->getCastKind() == CK_BaseToDerivedMemberPointer ||
|
|
|
|
E->getCastKind() == CK_ReinterpretMemberPointer);
|
2010-08-22 11:04:22 +08:00
|
|
|
|
2012-02-15 09:22:51 +08:00
|
|
|
// Under Itanium, reinterprets don't require any additional processing.
|
|
|
|
if (E->getCastKind() == CK_ReinterpretMemberPointer) return src;
|
|
|
|
|
|
|
|
// If the adjustment is trivial, we don't need to do anything.
|
|
|
|
llvm::Constant *adj = getMemberPointerAdjustment(E);
|
|
|
|
if (!adj) return src;
|
|
|
|
|
|
|
|
bool isDerivedToBase = (E->getCastKind() == CK_DerivedToBaseMemberPointer);
|
|
|
|
|
|
|
|
const MemberPointerType *destTy =
|
|
|
|
E->getType()->castAs<MemberPointerType>();
|
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.
|
2012-02-15 09:22:51 +08:00
|
|
|
if (destTy->isMemberDataPointer()) {
|
|
|
|
// null maps to null.
|
|
|
|
if (src->isAllOnesValue()) return src;
|
2010-08-23 09:21:21 +08:00
|
|
|
|
2012-02-15 09:22:51 +08:00
|
|
|
if (isDerivedToBase)
|
|
|
|
return llvm::ConstantExpr::getNSWSub(src, adj);
|
|
|
|
else
|
|
|
|
return llvm::ConstantExpr::getNSWAdd(src, adj);
|
2010-08-23 09:21:21 +08:00
|
|
|
}
|
|
|
|
|
2010-08-22 18:59:02 +08:00
|
|
|
// The this-adjustment is left-shifted by 1 on ARM.
|
|
|
|
if (IsARM) {
|
2012-02-15 09:22:51 +08:00
|
|
|
uint64_t offset = cast<llvm::ConstantInt>(adj)->getZExtValue();
|
|
|
|
offset <<= 1;
|
|
|
|
adj = llvm::ConstantInt::get(adj->getType(), offset);
|
2010-08-22 18:59:02 +08:00
|
|
|
}
|
|
|
|
|
2012-02-15 09:22:51 +08:00
|
|
|
llvm::Constant *srcAdj = llvm::ConstantExpr::getExtractValue(src, 1);
|
|
|
|
llvm::Constant *dstAdj;
|
|
|
|
if (isDerivedToBase)
|
|
|
|
dstAdj = llvm::ConstantExpr::getNSWSub(srcAdj, adj);
|
2010-08-22 18:59:02 +08:00
|
|
|
else
|
2012-02-15 09:22:51 +08:00
|
|
|
dstAdj = llvm::ConstantExpr::getNSWAdd(srcAdj, adj);
|
2010-08-22 18:59:02 +08:00
|
|
|
|
2012-02-15 09:22:51 +08:00
|
|
|
return llvm::ConstantExpr::getInsertValue(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::EmitNullMemberPointer(const MemberPointerType *MPT) {
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *ptrdiff_t = getPtrDiffTy();
|
2010-08-23 09:21:21 +08:00
|
|
|
|
|
|
|
// 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 };
|
2011-06-20 12:01:35 +08:00
|
|
|
return llvm::ConstantStruct::getAnon(Values);
|
2010-08-22 12:16:24 +08:00
|
|
|
}
|
|
|
|
|
2011-02-03 16:15:49 +08:00
|
|
|
llvm::Constant *
|
|
|
|
ItaniumCXXABI::EmitMemberDataPointer(const MemberPointerType *MPT,
|
|
|
|
CharUnits offset) {
|
2010-08-23 09:21:21 +08:00
|
|
|
// 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
|
2011-02-03 16:15:49 +08:00
|
|
|
return llvm::ConstantInt::get(getPtrDiffTy(), offset.getQuantity());
|
2010-08-23 09:21:21 +08:00
|
|
|
}
|
|
|
|
|
2011-04-12 08:42:48 +08:00
|
|
|
llvm::Constant *ItaniumCXXABI::EmitMemberPointer(const CXXMethodDecl *MD) {
|
2012-01-14 12:30:29 +08:00
|
|
|
return BuildMemberPointer(MD, CharUnits::Zero());
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Constant *ItaniumCXXABI::BuildMemberPointer(const CXXMethodDecl *MD,
|
|
|
|
CharUnits ThisAdjustment) {
|
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();
|
2011-07-18 12:24:23 +08:00
|
|
|
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()) {
|
2011-09-26 09:56:30 +08:00
|
|
|
uint64_t Index = CGM.getVTableContext().getMethodVTableIndex(MD);
|
2010-08-22 14:43:33 +08:00
|
|
|
|
2011-04-09 09:30:02 +08:00
|
|
|
const ASTContext &Context = getContext();
|
|
|
|
CharUnits PointerWidth =
|
2011-09-02 08:18:52 +08:00
|
|
|
Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0));
|
2011-04-09 09:30:02 +08:00
|
|
|
uint64_t VTableOffset = (Index * PointerWidth.getQuantity());
|
2010-08-22 18:59:02 +08:00
|
|
|
|
|
|
|
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);
|
2012-01-14 12:30:29 +08:00
|
|
|
MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t,
|
|
|
|
2 * ThisAdjustment.getQuantity() + 1);
|
2010-08-22 18:59:02 +08:00
|
|
|
} 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);
|
2012-01-14 12:30:29 +08:00
|
|
|
MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t,
|
|
|
|
ThisAdjustment.getQuantity());
|
2010-08-22 18:59:02 +08:00
|
|
|
}
|
|
|
|
} else {
|
2011-04-12 08:42:48 +08:00
|
|
|
const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>();
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *Ty;
|
2011-04-12 08:42:48 +08:00
|
|
|
// Check whether the function has a computable LLVM signature.
|
2011-07-10 08:18:59 +08:00
|
|
|
if (Types.isFuncTypeConvertible(FPT)) {
|
2011-04-12 08:42:48 +08:00
|
|
|
// The function has a computable LLVM signature; use the correct type.
|
2012-02-17 11:33:10 +08:00
|
|
|
Ty = Types.GetFunctionType(Types.arrangeCXXMethodDeclaration(MD));
|
2010-08-22 18:59:02 +08:00
|
|
|
} else {
|
2011-04-12 08:42:48 +08:00
|
|
|
// Use an arbitrary non-function type to tell GetAddrOfFunction that the
|
|
|
|
// function type is incomplete.
|
|
|
|
Ty = ptrdiff_t;
|
2010-08-22 18:59:02 +08:00
|
|
|
}
|
2011-04-12 08:42:48 +08:00
|
|
|
llvm::Constant *addr = CGM.GetAddrOfFunction(MD, Ty);
|
2010-08-22 18:59:02 +08:00
|
|
|
|
2011-04-11 15:02:50 +08:00
|
|
|
MemPtr[0] = llvm::ConstantExpr::getPtrToInt(addr, ptrdiff_t);
|
2012-01-14 12:30:29 +08:00
|
|
|
MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t, (IsARM ? 2 : 1) *
|
|
|
|
ThisAdjustment.getQuantity());
|
2010-08-22 18:59:02 +08:00
|
|
|
}
|
|
|
|
|
2011-06-20 12:01:35 +08:00
|
|
|
return llvm::ConstantStruct::getAnon(MemPtr);
|
2010-08-22 14:43:33 +08:00
|
|
|
}
|
|
|
|
|
2012-01-14 12:30:29 +08:00
|
|
|
llvm::Constant *ItaniumCXXABI::EmitMemberPointer(const APValue &MP,
|
|
|
|
QualType MPType) {
|
|
|
|
const MemberPointerType *MPT = MPType->castAs<MemberPointerType>();
|
|
|
|
const ValueDecl *MPD = MP.getMemberPointerDecl();
|
|
|
|
if (!MPD)
|
|
|
|
return EmitNullMemberPointer(MPT);
|
|
|
|
|
|
|
|
// Compute the this-adjustment.
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MPD))
|
|
|
|
return BuildMemberPointer(MD, ThisAdjustment);
|
|
|
|
|
|
|
|
CharUnits FieldOffset =
|
|
|
|
getContext().toCharUnitsFromBits(getContext().getFieldOffset(MPD));
|
|
|
|
return EmitMemberDataPointer(MPT, ThisAdjustment + FieldOffset);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2011-04-20 07:10:47 +08:00
|
|
|
// In Itanium, a member function pointer is not null if 'ptr' is not 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");
|
|
|
|
|
2011-04-20 07:10:47 +08:00
|
|
|
// On ARM, a member function pointer is also non-null if the low bit of 'adj'
|
|
|
|
// (the virtual bit) is set.
|
2010-08-22 16:30:07 +08:00
|
|
|
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");
|
2011-04-20 07:10:47 +08:00
|
|
|
llvm::Value *IsVirtual = Builder.CreateICmpNE(VirtualBit, Zero,
|
|
|
|
"memptr.isvirtual");
|
|
|
|
Result = Builder.CreateOr(Result, IsVirtual);
|
2010-08-22 16:30:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2012-09-25 18:10:39 +08:00
|
|
|
/// The Itanium ABI always places an offset to the complete object
|
|
|
|
/// at entry -2 in the vtable.
|
|
|
|
llvm::Value *ItaniumCXXABI::adjustToCompleteObject(CodeGenFunction &CGF,
|
|
|
|
llvm::Value *ptr,
|
|
|
|
QualType type) {
|
|
|
|
// Grab the vtable pointer as an intptr_t*.
|
|
|
|
llvm::Value *vtable = CGF.GetVTablePtr(ptr, CGF.IntPtrTy->getPointerTo());
|
|
|
|
|
|
|
|
// Track back to entry -2 and pull out the offset there.
|
|
|
|
llvm::Value *offsetPtr =
|
|
|
|
CGF.Builder.CreateConstInBoundsGEP1_64(vtable, -2, "complete-offset.ptr");
|
|
|
|
llvm::LoadInst *offset = CGF.Builder.CreateLoad(offsetPtr);
|
|
|
|
offset->setAlignment(CGF.PointerAlignInBytes);
|
|
|
|
|
|
|
|
// Apply the offset.
|
|
|
|
ptr = CGF.Builder.CreateBitCast(ptr, CGF.Int8PtrTy);
|
|
|
|
return CGF.Builder.CreateInBoundsGEP(ptr, offset);
|
|
|
|
}
|
|
|
|
|
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,
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVectorImpl<CanQualType> &ArgTys) {
|
2010-09-02 18:25:57 +08:00
|
|
|
ASTContext &Context = getContext();
|
2010-08-31 15:33:07 +08:00
|
|
|
|
|
|
|
// '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,
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVectorImpl<CanQualType> &ArgTys) {
|
2010-08-31 15:33:07 +08:00
|
|
|
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,
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVectorImpl<CanQualType> &ArgTys) {
|
2010-09-02 18:25:57 +08:00
|
|
|
ASTContext &Context = getContext();
|
2010-08-31 15:33:07 +08:00
|
|
|
|
|
|
|
// '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,
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVectorImpl<CanQualType> &ArgTys) {
|
2010-08-31 15:33:07 +08:00
|
|
|
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)) {
|
2010-09-02 18:25:57 +08:00
|
|
|
ASTContext &Context = getContext();
|
2010-08-31 15:33:07 +08:00
|
|
|
|
|
|
|
// FIXME: avoid the fake decl
|
|
|
|
QualType T = Context.getPointerType(Context.VoidPtrTy);
|
|
|
|
ImplicitParamDecl *VTTDecl
|
|
|
|
= ImplicitParamDecl::Create(Context, 0, MD->getLocation(),
|
|
|
|
&Context.Idents.get("vtt"), T);
|
2011-03-09 12:27:21 +08:00
|
|
|
Params.push_back(VTTDecl);
|
2010-08-31 15:33:07 +08:00
|
|
|
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))
|
2011-03-09 12:27:21 +08:00
|
|
|
ResTy = Params[0]->getType();
|
2010-08-31 15:33:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
2012-02-11 10:57:39 +08:00
|
|
|
CGF.Builder.CreateStore(getThisValue(CGF), CGF.ReturnValue);
|
2010-08-31 15:33:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *T =
|
2010-08-31 15:33:07 +08:00
|
|
|
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 **************************/
|
|
|
|
|
2012-05-01 13:23:51 +08:00
|
|
|
CharUnits ItaniumCXXABI::getArrayCookieSizeImpl(QualType elementType) {
|
|
|
|
// The array cookie is a size_t; pad that up to the element alignment.
|
|
|
|
// The cookie is actually right-justified in that space.
|
|
|
|
return std::max(CharUnits::fromQuantity(CGM.SizeSizeInBytes),
|
|
|
|
CGM.getContext().getTypeAlignInChars(elementType));
|
2010-09-02 17:58:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Value *ItaniumCXXABI::InitializeArrayCookie(CodeGenFunction &CGF,
|
|
|
|
llvm::Value *NewPtr,
|
|
|
|
llvm::Value *NumElements,
|
2011-01-27 17:37:56 +08:00
|
|
|
const CXXNewExpr *expr,
|
2010-09-02 17:58:18 +08:00
|
|
|
QualType ElementType) {
|
2012-05-01 13:23:51 +08:00
|
|
|
assert(requiresArrayCookie(expr));
|
2010-09-02 17:58:18 +08:00
|
|
|
|
2012-10-25 23:39:14 +08:00
|
|
|
unsigned AS = NewPtr->getType()->getPointerAddressSpace();
|
2010-09-02 17:58:18 +08:00
|
|
|
|
2010-09-02 18:25:57 +08:00
|
|
|
ASTContext &Ctx = getContext();
|
2010-09-02 17:58:18 +08:00
|
|
|
QualType SizeTy = Ctx.getSizeType();
|
|
|
|
CharUnits SizeSize = Ctx.getTypeSizeInChars(SizeTy);
|
|
|
|
|
|
|
|
// The size of the cookie.
|
|
|
|
CharUnits CookieSize =
|
|
|
|
std::max(SizeSize, Ctx.getTypeAlignInChars(ElementType));
|
2012-05-01 13:23:51 +08:00
|
|
|
assert(CookieSize == getArrayCookieSizeImpl(ElementType));
|
2010-09-02 17:58:18 +08:00
|
|
|
|
|
|
|
// 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());
|
|
|
|
}
|
|
|
|
|
2012-05-01 13:23:51 +08:00
|
|
|
llvm::Value *ItaniumCXXABI::readArrayCookieImpl(CodeGenFunction &CGF,
|
|
|
|
llvm::Value *allocPtr,
|
|
|
|
CharUnits cookieSize) {
|
|
|
|
// The element size is right-justified in the cookie.
|
|
|
|
llvm::Value *numElementsPtr = allocPtr;
|
|
|
|
CharUnits numElementsOffset =
|
|
|
|
cookieSize - CharUnits::fromQuantity(CGF.SizeSizeInBytes);
|
|
|
|
if (!numElementsOffset.isZero())
|
|
|
|
numElementsPtr =
|
|
|
|
CGF.Builder.CreateConstInBoundsGEP1_64(numElementsPtr,
|
|
|
|
numElementsOffset.getQuantity());
|
2010-09-02 17:58:18 +08:00
|
|
|
|
2012-10-25 23:39:14 +08:00
|
|
|
unsigned AS = allocPtr->getType()->getPointerAddressSpace();
|
2012-05-01 13:23:51 +08:00
|
|
|
numElementsPtr =
|
|
|
|
CGF.Builder.CreateBitCast(numElementsPtr, CGF.SizeTy->getPointerTo(AS));
|
|
|
|
return CGF.Builder.CreateLoad(numElementsPtr);
|
2010-09-02 17:58:18 +08:00
|
|
|
}
|
|
|
|
|
2012-05-01 13:23:51 +08:00
|
|
|
CharUnits ARMCXXABI::getArrayCookieSizeImpl(QualType elementType) {
|
2010-09-02 17:58:18 +08:00
|
|
|
// 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?
|
2012-05-01 13:23:51 +08:00
|
|
|
return CharUnits::fromQuantity(2 * CGM.SizeSizeInBytes);
|
2010-09-02 17:58:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Value *ARMCXXABI::InitializeArrayCookie(CodeGenFunction &CGF,
|
|
|
|
llvm::Value *NewPtr,
|
|
|
|
llvm::Value *NumElements,
|
2011-01-27 17:37:56 +08:00
|
|
|
const CXXNewExpr *expr,
|
2010-09-02 17:58:18 +08:00
|
|
|
QualType ElementType) {
|
2012-05-01 13:23:51 +08:00
|
|
|
assert(requiresArrayCookie(expr));
|
2010-09-02 17:58:18 +08:00
|
|
|
|
|
|
|
// NewPtr is a char*.
|
|
|
|
|
2012-10-25 23:39:14 +08:00
|
|
|
unsigned AS = NewPtr->getType()->getPointerAddressSpace();
|
2010-09-02 17:58:18 +08:00
|
|
|
|
2010-09-02 18:25:57 +08:00
|
|
|
ASTContext &Ctx = getContext();
|
2010-09-02 17:58:18 +08:00
|
|
|
CharUnits SizeSize = Ctx.getTypeSizeInChars(Ctx.getSizeType());
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::IntegerType *SizeTy =
|
2010-09-02 17:58:18 +08:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2012-05-01 13:23:51 +08:00
|
|
|
llvm::Value *ARMCXXABI::readArrayCookieImpl(CodeGenFunction &CGF,
|
|
|
|
llvm::Value *allocPtr,
|
|
|
|
CharUnits cookieSize) {
|
|
|
|
// The number of elements is at offset sizeof(size_t) relative to
|
|
|
|
// the allocated pointer.
|
|
|
|
llvm::Value *numElementsPtr
|
|
|
|
= CGF.Builder.CreateConstInBoundsGEP1_64(allocPtr, CGF.SizeSizeInBytes);
|
2010-09-02 17:58:18 +08:00
|
|
|
|
2012-10-25 23:39:14 +08:00
|
|
|
unsigned AS = allocPtr->getType()->getPointerAddressSpace();
|
2012-05-01 13:23:51 +08:00
|
|
|
numElementsPtr =
|
|
|
|
CGF.Builder.CreateBitCast(numElementsPtr, CGF.SizeTy->getPointerTo(AS));
|
|
|
|
return CGF.Builder.CreateLoad(numElementsPtr);
|
2010-09-02 17:58:18 +08:00
|
|
|
}
|
|
|
|
|
2010-09-08 09:44:27 +08:00
|
|
|
/*********************** Static local initialization **************************/
|
|
|
|
|
|
|
|
static llvm::Constant *getGuardAcquireFn(CodeGenModule &CGM,
|
2011-07-10 01:41:47 +08:00
|
|
|
llvm::PointerType *GuardPtrTy) {
|
2010-09-08 09:44:27 +08:00
|
|
|
// int __cxa_guard_acquire(__guard *guard_object);
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::FunctionType *FTy =
|
2010-09-08 09:44:27 +08:00
|
|
|
llvm::FunctionType::get(CGM.getTypes().ConvertType(CGM.getContext().IntTy),
|
2011-07-29 21:56:53 +08:00
|
|
|
GuardPtrTy, /*isVarArg=*/false);
|
2012-02-14 07:45:02 +08:00
|
|
|
return CGM.CreateRuntimeFunction(FTy, "__cxa_guard_acquire",
|
2012-10-15 12:47:45 +08:00
|
|
|
llvm::Attributes::get(CGM.getLLVMContext(),
|
2012-10-16 13:23:44 +08:00
|
|
|
llvm::Attributes::NoUnwind));
|
2010-09-08 09:44:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static llvm::Constant *getGuardReleaseFn(CodeGenModule &CGM,
|
2011-07-10 01:41:47 +08:00
|
|
|
llvm::PointerType *GuardPtrTy) {
|
2010-09-08 09:44:27 +08:00
|
|
|
// void __cxa_guard_release(__guard *guard_object);
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::FunctionType *FTy =
|
2012-02-07 08:39:47 +08:00
|
|
|
llvm::FunctionType::get(CGM.VoidTy, GuardPtrTy, /*isVarArg=*/false);
|
2012-02-14 07:45:02 +08:00
|
|
|
return CGM.CreateRuntimeFunction(FTy, "__cxa_guard_release",
|
2012-10-15 12:47:45 +08:00
|
|
|
llvm::Attributes::get(CGM.getLLVMContext(),
|
2012-10-16 13:23:44 +08:00
|
|
|
llvm::Attributes::NoUnwind));
|
2010-09-08 09:44:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static llvm::Constant *getGuardAbortFn(CodeGenModule &CGM,
|
2011-07-10 01:41:47 +08:00
|
|
|
llvm::PointerType *GuardPtrTy) {
|
2010-09-08 09:44:27 +08:00
|
|
|
// void __cxa_guard_abort(__guard *guard_object);
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::FunctionType *FTy =
|
2012-02-07 08:39:47 +08:00
|
|
|
llvm::FunctionType::get(CGM.VoidTy, GuardPtrTy, /*isVarArg=*/false);
|
2012-02-14 07:45:02 +08:00
|
|
|
return CGM.CreateRuntimeFunction(FTy, "__cxa_guard_abort",
|
2012-10-15 12:47:45 +08:00
|
|
|
llvm::Attributes::get(CGM.getLLVMContext(),
|
2012-10-16 13:23:44 +08:00
|
|
|
llvm::Attributes::NoUnwind));
|
2010-09-08 09:44:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
struct CallGuardAbort : EHScopeStack::Cleanup {
|
|
|
|
llvm::GlobalVariable *Guard;
|
2012-03-31 03:44:53 +08:00
|
|
|
CallGuardAbort(llvm::GlobalVariable *Guard) : Guard(Guard) {}
|
2010-09-08 09:44:27 +08:00
|
|
|
|
2011-07-13 04:27:29 +08:00
|
|
|
void Emit(CodeGenFunction &CGF, Flags flags) {
|
2010-09-08 09:44:27 +08:00
|
|
|
CGF.Builder.CreateCall(getGuardAbortFn(CGF.CGM, Guard->getType()), Guard)
|
|
|
|
->setDoesNotThrow();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The ARM code here follows the Itanium code closely enough that we
|
|
|
|
/// just special-case it at particular places.
|
2010-11-06 17:44:32 +08:00
|
|
|
void ItaniumCXXABI::EmitGuardedInit(CodeGenFunction &CGF,
|
|
|
|
const VarDecl &D,
|
2012-03-31 05:00:39 +08:00
|
|
|
llvm::GlobalVariable *var,
|
|
|
|
bool shouldPerformInit) {
|
2010-09-08 09:44:27 +08:00
|
|
|
CGBuilderTy &Builder = CGF.Builder;
|
2010-11-06 17:44:32 +08:00
|
|
|
|
|
|
|
// We only need to use thread-safe statics for local variables;
|
|
|
|
// global initialization is always single-threaded.
|
2011-06-17 15:33:57 +08:00
|
|
|
bool threadsafe =
|
2012-03-11 15:00:24 +08:00
|
|
|
(getContext().getLangOpts().ThreadsafeStatics && D.isLocalVarDecl());
|
2011-04-27 12:37:08 +08:00
|
|
|
|
|
|
|
// If we have a global variable with internal linkage and thread-safe statics
|
|
|
|
// are disabled, we can just let the guard variable be of type i8.
|
2012-03-31 05:00:39 +08:00
|
|
|
bool useInt8GuardVariable = !threadsafe && var->hasInternalLinkage();
|
|
|
|
|
|
|
|
llvm::IntegerType *guardTy;
|
2011-06-17 15:33:57 +08:00
|
|
|
if (useInt8GuardVariable) {
|
2012-03-31 05:00:39 +08:00
|
|
|
guardTy = CGF.Int8Ty;
|
2011-06-17 15:33:57 +08:00
|
|
|
} else {
|
2011-04-27 12:37:08 +08:00
|
|
|
// Guard variables are 64 bits in the generic ABI and 32 bits on ARM.
|
2012-03-31 05:00:39 +08:00
|
|
|
guardTy = (IsARM ? CGF.Int32Ty : CGF.Int64Ty);
|
|
|
|
}
|
|
|
|
llvm::PointerType *guardPtrTy = guardTy->getPointerTo();
|
|
|
|
|
|
|
|
// Create the guard variable if we don't already have it (as we
|
|
|
|
// might if we're double-emitting this function body).
|
|
|
|
llvm::GlobalVariable *guard = CGM.getStaticLocalDeclGuardAddress(&D);
|
|
|
|
if (!guard) {
|
|
|
|
// Mangle the name for the guard.
|
|
|
|
SmallString<256> guardName;
|
|
|
|
{
|
|
|
|
llvm::raw_svector_ostream out(guardName);
|
|
|
|
getMangleContext().mangleItaniumGuardVariable(&D, out);
|
|
|
|
out.flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the guard variable with a zero-initializer.
|
|
|
|
// Just absorb linkage and visibility from the guarded variable.
|
|
|
|
guard = new llvm::GlobalVariable(CGM.getModule(), guardTy,
|
|
|
|
false, var->getLinkage(),
|
|
|
|
llvm::ConstantInt::get(guardTy, 0),
|
|
|
|
guardName.str());
|
|
|
|
guard->setVisibility(var->getVisibility());
|
|
|
|
|
|
|
|
CGM.setStaticLocalDeclGuardAddress(&D, guard);
|
2011-04-27 12:37:08 +08:00
|
|
|
}
|
2010-09-08 09:44:27 +08:00
|
|
|
|
|
|
|
// Test whether the variable has completed initialization.
|
2012-03-31 05:00:39 +08:00
|
|
|
llvm::Value *isInitialized;
|
2010-09-08 09:44:27 +08:00
|
|
|
|
|
|
|
// ARM C++ ABI 3.2.3.1:
|
|
|
|
// To support the potential use of initialization guard variables
|
|
|
|
// as semaphores that are the target of ARM SWP and LDREX/STREX
|
|
|
|
// synchronizing instructions we define a static initialization
|
|
|
|
// guard variable to be a 4-byte aligned, 4- byte word with the
|
|
|
|
// following inline access protocol.
|
|
|
|
// #define INITIALIZED 1
|
|
|
|
// if ((obj_guard & INITIALIZED) != INITIALIZED) {
|
|
|
|
// if (__cxa_guard_acquire(&obj_guard))
|
|
|
|
// ...
|
|
|
|
// }
|
2011-06-17 15:33:57 +08:00
|
|
|
if (IsARM && !useInt8GuardVariable) {
|
2012-03-31 05:00:39 +08:00
|
|
|
llvm::Value *V = Builder.CreateLoad(guard);
|
2010-09-08 09:44:27 +08:00
|
|
|
V = Builder.CreateAnd(V, Builder.getInt32(1));
|
2012-03-31 05:00:39 +08:00
|
|
|
isInitialized = Builder.CreateIsNull(V, "guard.uninitialized");
|
2010-09-08 09:44:27 +08:00
|
|
|
|
|
|
|
// Itanium C++ ABI 3.3.2:
|
|
|
|
// The following is pseudo-code showing how these functions can be used:
|
|
|
|
// if (obj_guard.first_byte == 0) {
|
|
|
|
// if ( __cxa_guard_acquire (&obj_guard) ) {
|
|
|
|
// try {
|
|
|
|
// ... initialize the object ...;
|
|
|
|
// } catch (...) {
|
|
|
|
// __cxa_guard_abort (&obj_guard);
|
|
|
|
// throw;
|
|
|
|
// }
|
|
|
|
// ... queue object destructor with __cxa_atexit() ...;
|
|
|
|
// __cxa_guard_release (&obj_guard);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
} else {
|
|
|
|
// Load the first byte of the guard variable.
|
2012-03-31 03:44:53 +08:00
|
|
|
llvm::LoadInst *LI =
|
2012-03-31 05:00:39 +08:00
|
|
|
Builder.CreateLoad(Builder.CreateBitCast(guard, CGM.Int8PtrTy));
|
2012-03-31 03:44:53 +08:00
|
|
|
LI->setAlignment(1);
|
2011-09-14 06:21:56 +08:00
|
|
|
|
|
|
|
// Itanium ABI:
|
|
|
|
// An implementation supporting thread-safety on multiprocessor
|
|
|
|
// systems must also guarantee that references to the initialized
|
|
|
|
// object do not occur before the load of the initialization flag.
|
|
|
|
//
|
|
|
|
// In LLVM, we do this by marking the load Acquire.
|
|
|
|
if (threadsafe)
|
2012-03-31 03:44:53 +08:00
|
|
|
LI->setAtomic(llvm::Acquire);
|
2011-09-14 06:21:56 +08:00
|
|
|
|
2012-03-31 05:00:39 +08:00
|
|
|
isInitialized = Builder.CreateIsNull(LI, "guard.uninitialized");
|
2010-09-08 09:44:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm::BasicBlock *InitCheckBlock = CGF.createBasicBlock("init.check");
|
|
|
|
llvm::BasicBlock *EndBlock = CGF.createBasicBlock("init.end");
|
|
|
|
|
|
|
|
// Check if the first byte of the guard variable is zero.
|
2012-03-31 05:00:39 +08:00
|
|
|
Builder.CreateCondBr(isInitialized, InitCheckBlock, EndBlock);
|
2010-09-08 09:44:27 +08:00
|
|
|
|
|
|
|
CGF.EmitBlock(InitCheckBlock);
|
|
|
|
|
|
|
|
// Variables used when coping with thread-safe statics and exceptions.
|
2011-06-17 15:33:57 +08:00
|
|
|
if (threadsafe) {
|
2010-09-08 09:44:27 +08:00
|
|
|
// Call __cxa_guard_acquire.
|
|
|
|
llvm::Value *V
|
2012-03-31 05:00:39 +08:00
|
|
|
= Builder.CreateCall(getGuardAcquireFn(CGM, guardPtrTy), guard);
|
2010-09-08 09:44:27 +08:00
|
|
|
|
|
|
|
llvm::BasicBlock *InitBlock = CGF.createBasicBlock("init");
|
|
|
|
|
|
|
|
Builder.CreateCondBr(Builder.CreateIsNotNull(V, "tobool"),
|
|
|
|
InitBlock, EndBlock);
|
|
|
|
|
|
|
|
// Call __cxa_guard_abort along the exceptional edge.
|
2012-03-31 05:00:39 +08:00
|
|
|
CGF.EHStack.pushCleanup<CallGuardAbort>(EHCleanup, guard);
|
2010-09-08 09:44:27 +08:00
|
|
|
|
|
|
|
CGF.EmitBlock(InitBlock);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Emit the initializer and add a global destructor if appropriate.
|
2012-03-31 05:00:39 +08:00
|
|
|
CGF.EmitCXXGlobalVarDeclInit(D, var, shouldPerformInit);
|
2010-09-08 09:44:27 +08:00
|
|
|
|
2011-06-17 15:33:57 +08:00
|
|
|
if (threadsafe) {
|
2010-09-08 09:44:27 +08:00
|
|
|
// Pop the guard-abort cleanup if we pushed one.
|
|
|
|
CGF.PopCleanupBlock();
|
|
|
|
|
|
|
|
// Call __cxa_guard_release. This cannot throw.
|
2012-03-31 05:00:39 +08:00
|
|
|
Builder.CreateCall(getGuardReleaseFn(CGM, guardPtrTy), guard);
|
2010-09-08 09:44:27 +08:00
|
|
|
} else {
|
2012-03-31 05:00:39 +08:00
|
|
|
Builder.CreateStore(llvm::ConstantInt::get(guardTy, 1), guard);
|
2010-09-08 09:44:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CGF.EmitBlock(EndBlock);
|
|
|
|
}
|
2012-05-01 14:13:13 +08:00
|
|
|
|
|
|
|
/// Register a global destructor using __cxa_atexit.
|
|
|
|
static void emitGlobalDtorWithCXAAtExit(CodeGenFunction &CGF,
|
|
|
|
llvm::Constant *dtor,
|
|
|
|
llvm::Constant *addr) {
|
|
|
|
// We're assuming that the destructor function is something we can
|
|
|
|
// reasonably call with the default CC. Go ahead and cast it to the
|
|
|
|
// right prototype.
|
|
|
|
llvm::Type *dtorTy =
|
|
|
|
llvm::FunctionType::get(CGF.VoidTy, CGF.Int8PtrTy, false)->getPointerTo();
|
|
|
|
|
|
|
|
// extern "C" int __cxa_atexit(void (*f)(void *), void *p, void *d);
|
|
|
|
llvm::Type *paramTys[] = { dtorTy, CGF.Int8PtrTy, CGF.Int8PtrTy };
|
|
|
|
llvm::FunctionType *atexitTy =
|
|
|
|
llvm::FunctionType::get(CGF.IntTy, paramTys, false);
|
|
|
|
|
|
|
|
// Fetch the actual function.
|
|
|
|
llvm::Constant *atexit =
|
|
|
|
CGF.CGM.CreateRuntimeFunction(atexitTy, "__cxa_atexit");
|
|
|
|
if (llvm::Function *fn = dyn_cast<llvm::Function>(atexit))
|
|
|
|
fn->setDoesNotThrow();
|
|
|
|
|
|
|
|
// Create a variable that binds the atexit to this shared object.
|
|
|
|
llvm::Constant *handle =
|
|
|
|
CGF.CGM.CreateRuntimeVariable(CGF.Int8Ty, "__dso_handle");
|
|
|
|
|
|
|
|
llvm::Value *args[] = {
|
|
|
|
llvm::ConstantExpr::getBitCast(dtor, dtorTy),
|
|
|
|
llvm::ConstantExpr::getBitCast(addr, CGF.Int8PtrTy),
|
|
|
|
handle
|
|
|
|
};
|
|
|
|
CGF.Builder.CreateCall(atexit, args)->setDoesNotThrow();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Register a global destructor as best as we know how.
|
|
|
|
void ItaniumCXXABI::registerGlobalDtor(CodeGenFunction &CGF,
|
|
|
|
llvm::Constant *dtor,
|
|
|
|
llvm::Constant *addr) {
|
|
|
|
// Use __cxa_atexit if available.
|
|
|
|
if (CGM.getCodeGenOpts().CXAAtExit) {
|
|
|
|
return emitGlobalDtorWithCXAAtExit(CGF, dtor, addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
// In Apple kexts, we want to add a global destructor entry.
|
|
|
|
// FIXME: shouldn't this be guarded by some variable?
|
2012-11-02 06:30:59 +08:00
|
|
|
if (CGM.getLangOpts().AppleKext) {
|
2012-05-01 14:13:13 +08:00
|
|
|
// Generate a global destructor entry.
|
|
|
|
return CGM.AddCXXDtorEntry(dtor, addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
CGF.registerGlobalDtorWithAtExit(dtor, addr);
|
|
|
|
}
|
2012-06-24 07:44:00 +08:00
|
|
|
|
|
|
|
/// Generate and emit virtual tables for the given class.
|
|
|
|
void ItaniumCXXABI::EmitVTables(const CXXRecordDecl *Class) {
|
|
|
|
CGM.getVTables().GenerateClassData(CGM.getVTableLinkage(Class), Class);
|
|
|
|
}
|