2010-05-26 03:52:27 +08:00
|
|
|
//===----- CGCXXABI.h - Interface to C++ ABIs -------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This provides an abstract class for C++ code generation. Concrete subclasses
|
|
|
|
// of this implement code generation for specific C++ ABIs.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef CLANG_CODEGEN_CXXABI_H
|
|
|
|
#define CLANG_CODEGEN_CXXABI_H
|
|
|
|
|
2011-07-23 18:35:09 +08:00
|
|
|
#include "clang/Basic/LLVM.h"
|
|
|
|
|
2010-08-31 15:33:07 +08:00
|
|
|
#include "CodeGenFunction.h"
|
|
|
|
|
2010-08-22 08:05:51 +08:00
|
|
|
namespace llvm {
|
2010-08-22 12:16:24 +08:00
|
|
|
class Constant;
|
2010-08-23 09:21:21 +08:00
|
|
|
class Type;
|
2010-08-22 08:05:51 +08:00
|
|
|
class Value;
|
|
|
|
}
|
|
|
|
|
2010-05-26 03:52:27 +08:00
|
|
|
namespace clang {
|
2010-08-22 11:04:22 +08:00
|
|
|
class CastExpr;
|
2010-08-31 15:33:07 +08:00
|
|
|
class CXXConstructorDecl;
|
|
|
|
class CXXDestructorDecl;
|
2010-08-22 14:43:33 +08:00
|
|
|
class CXXMethodDecl;
|
2010-08-22 12:16:24 +08:00
|
|
|
class CXXRecordDecl;
|
2010-08-23 09:21:21 +08:00
|
|
|
class FieldDecl;
|
2011-01-14 02:57:25 +08:00
|
|
|
class MangleContext;
|
2010-08-22 08:05:51 +08:00
|
|
|
|
2010-05-26 03:52:27 +08:00
|
|
|
namespace CodeGen {
|
2010-08-22 08:05:51 +08:00
|
|
|
class CodeGenFunction;
|
2010-05-26 03:52:27 +08:00
|
|
|
class CodeGenModule;
|
|
|
|
|
2012-06-15 15:35:42 +08:00
|
|
|
/// \brief Implements C++ ABI-specific code generation functions.
|
2010-08-16 11:33:14 +08:00
|
|
|
class CGCXXABI {
|
2010-08-22 18:59:02 +08:00
|
|
|
protected:
|
|
|
|
CodeGenModule &CGM;
|
2012-02-05 10:12:40 +08:00
|
|
|
OwningPtr<MangleContext> MangleCtx;
|
2010-08-22 18:59:02 +08:00
|
|
|
|
2011-01-14 02:57:25 +08:00
|
|
|
CGCXXABI(CodeGenModule &CGM)
|
|
|
|
: CGM(CGM), MangleCtx(CGM.getContext().createMangleContext()) {}
|
2010-08-22 18:59:02 +08:00
|
|
|
|
2010-08-31 15:33:07 +08:00
|
|
|
protected:
|
|
|
|
ImplicitParamDecl *&getThisDecl(CodeGenFunction &CGF) {
|
2012-02-11 10:57:39 +08:00
|
|
|
return CGF.CXXABIThisDecl;
|
2010-08-31 15:33:07 +08:00
|
|
|
}
|
|
|
|
llvm::Value *&getThisValue(CodeGenFunction &CGF) {
|
2012-02-11 10:57:39 +08:00
|
|
|
return CGF.CXXABIThisValue;
|
2010-08-31 15:33:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ImplicitParamDecl *&getVTTDecl(CodeGenFunction &CGF) {
|
|
|
|
return CGF.CXXVTTDecl;
|
|
|
|
}
|
|
|
|
llvm::Value *&getVTTValue(CodeGenFunction &CGF) {
|
|
|
|
return CGF.CXXVTTValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Build a parameter variable suitable for 'this'.
|
|
|
|
void BuildThisParam(CodeGenFunction &CGF, FunctionArgList &Params);
|
|
|
|
|
|
|
|
/// Perform prolog initialization of the parameter variable suitable
|
|
|
|
/// for 'this' emitted by BuildThisParam.
|
|
|
|
void EmitThisParam(CodeGenFunction &CGF);
|
|
|
|
|
2010-09-02 17:58:18 +08:00
|
|
|
ASTContext &getContext() const { return CGM.getContext(); }
|
|
|
|
|
2012-05-01 13:23:51 +08:00
|
|
|
virtual bool requiresArrayCookie(const CXXDeleteExpr *E, QualType eltType);
|
|
|
|
virtual bool requiresArrayCookie(const CXXNewExpr *E);
|
|
|
|
|
2010-05-26 03:52:27 +08:00
|
|
|
public:
|
2010-08-22 18:59:02 +08:00
|
|
|
|
2010-11-29 01:50:09 +08:00
|
|
|
virtual ~CGCXXABI();
|
2010-05-26 03:52:27 +08:00
|
|
|
|
|
|
|
/// Gets the mangle context.
|
2011-01-14 02:57:25 +08:00
|
|
|
MangleContext &getMangleContext() {
|
|
|
|
return *MangleCtx;
|
|
|
|
}
|
2010-08-22 08:05:51 +08:00
|
|
|
|
2010-08-23 09:21:21 +08:00
|
|
|
/// Find the LLVM type used to represent the given member pointer
|
|
|
|
/// type.
|
2011-07-10 01:41:47 +08:00
|
|
|
virtual llvm::Type *
|
2010-08-23 09:21:21 +08:00
|
|
|
ConvertMemberPointerType(const MemberPointerType *MPT);
|
|
|
|
|
|
|
|
/// Load a member function from an object and a member function
|
|
|
|
/// pointer. Apply the this-adjustment and set 'This' to the
|
|
|
|
/// adjusted value.
|
2010-08-22 08:05:51 +08:00
|
|
|
virtual llvm::Value *
|
|
|
|
EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF,
|
|
|
|
llvm::Value *&This,
|
|
|
|
llvm::Value *MemPtr,
|
|
|
|
const MemberPointerType *MPT);
|
2010-08-22 11:04:22 +08:00
|
|
|
|
2010-09-01 05:07:20 +08:00
|
|
|
/// Calculate an l-value from an object and a data member pointer.
|
|
|
|
virtual llvm::Value *EmitMemberDataPointerAddress(CodeGenFunction &CGF,
|
|
|
|
llvm::Value *Base,
|
|
|
|
llvm::Value *MemPtr,
|
|
|
|
const MemberPointerType *MPT);
|
|
|
|
|
2012-02-15 09:22:51 +08:00
|
|
|
/// Perform a derived-to-base, base-to-derived, or bitcast member
|
|
|
|
/// pointer conversion.
|
2010-08-23 09:21:21 +08:00
|
|
|
virtual llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF,
|
|
|
|
const CastExpr *E,
|
|
|
|
llvm::Value *Src);
|
2010-08-22 18:59:02 +08:00
|
|
|
|
2012-02-15 09:22:51 +08:00
|
|
|
/// Perform a derived-to-base, base-to-derived, or bitcast member
|
|
|
|
/// pointer conversion on a constant value.
|
|
|
|
virtual llvm::Constant *EmitMemberPointerConversion(const CastExpr *E,
|
|
|
|
llvm::Constant *Src);
|
|
|
|
|
2010-08-23 09:21:21 +08:00
|
|
|
/// Return true if the given member pointer can be zero-initialized
|
|
|
|
/// (in the C++ sense) with an LLVM zeroinitializer.
|
2010-08-23 05:01:12 +08:00
|
|
|
virtual bool isZeroInitializable(const MemberPointerType *MPT);
|
2010-08-22 12:16:24 +08:00
|
|
|
|
2010-08-23 09:21:21 +08:00
|
|
|
/// Create a null member pointer of the given type.
|
|
|
|
virtual llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT);
|
2010-08-22 12:16:24 +08:00
|
|
|
|
2010-08-23 09:21:21 +08:00
|
|
|
/// Create a member pointer for the given method.
|
2011-04-12 08:42:48 +08:00
|
|
|
virtual llvm::Constant *EmitMemberPointer(const CXXMethodDecl *MD);
|
2010-08-22 14:43:33 +08:00
|
|
|
|
2010-08-23 09:21:21 +08:00
|
|
|
/// Create a member pointer for the given field.
|
2011-02-03 16:15:49 +08:00
|
|
|
virtual llvm::Constant *EmitMemberDataPointer(const MemberPointerType *MPT,
|
|
|
|
CharUnits offset);
|
2010-08-22 16:30:07 +08:00
|
|
|
|
2012-01-14 12:30:29 +08:00
|
|
|
/// Create a member pointer for the given member pointer constant.
|
|
|
|
virtual llvm::Constant *EmitMemberPointer(const APValue &MP, QualType MPT);
|
|
|
|
|
2010-08-23 09:21:21 +08:00
|
|
|
/// Emit a comparison between two member pointers. Returns an i1.
|
2010-08-22 16:30:07 +08:00
|
|
|
virtual llvm::Value *
|
2010-08-23 09:21:21 +08:00
|
|
|
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
|
|
|
/// Determine if a member pointer is non-null. Returns an i1.
|
2010-08-22 16:30:07 +08:00
|
|
|
virtual llvm::Value *
|
2010-08-23 09:21:21 +08:00
|
|
|
EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
|
|
|
|
llvm::Value *MemPtr,
|
|
|
|
const MemberPointerType *MPT);
|
2010-08-31 15:33:07 +08:00
|
|
|
|
2012-02-15 09:22:51 +08:00
|
|
|
protected:
|
|
|
|
/// A utility method for computing the offset required for the given
|
|
|
|
/// base-to-derived or derived-to-base member-pointer conversion.
|
|
|
|
/// Does not handle virtual conversions (in case we ever fully
|
|
|
|
/// support an ABI that allows this). Returns null if no adjustment
|
|
|
|
/// is required.
|
|
|
|
llvm::Constant *getMemberPointerAdjustment(const CastExpr *E);
|
|
|
|
|
|
|
|
public:
|
2012-09-25 18:10:39 +08:00
|
|
|
/// Adjust the given non-null pointer to an object of polymorphic
|
|
|
|
/// type to point to the complete object.
|
|
|
|
///
|
|
|
|
/// The IR type of the result should be a pointer but is otherwise
|
|
|
|
/// irrelevant.
|
|
|
|
virtual llvm::Value *adjustToCompleteObject(CodeGenFunction &CGF,
|
|
|
|
llvm::Value *ptr,
|
|
|
|
QualType type) = 0;
|
|
|
|
|
2010-08-31 15:33:07 +08:00
|
|
|
/// Build the signature of the given constructor variant by adding
|
|
|
|
/// any required parameters. For convenience, ResTy has been
|
|
|
|
/// initialized to 'void', and ArgTys has been initialized with the
|
|
|
|
/// type of 'this' (although this may be changed by the ABI) and
|
|
|
|
/// will have the formal parameters added to it afterwards.
|
|
|
|
///
|
|
|
|
/// If there are ever any ABIs where the implicit parameters are
|
|
|
|
/// intermixed with the formal parameters, we can address those
|
|
|
|
/// then.
|
|
|
|
virtual void BuildConstructorSignature(const CXXConstructorDecl *Ctor,
|
|
|
|
CXXCtorType T,
|
|
|
|
CanQualType &ResTy,
|
2011-07-20 14:58:45 +08:00
|
|
|
SmallVectorImpl<CanQualType> &ArgTys) = 0;
|
2010-08-31 15:33:07 +08:00
|
|
|
|
|
|
|
/// Build the signature of the given destructor variant by adding
|
|
|
|
/// any required parameters. For convenience, ResTy has been
|
|
|
|
/// initialized to 'void' and ArgTys has been initialized with the
|
|
|
|
/// type of 'this' (although this may be changed by the ABI).
|
|
|
|
virtual void BuildDestructorSignature(const CXXDestructorDecl *Dtor,
|
|
|
|
CXXDtorType T,
|
|
|
|
CanQualType &ResTy,
|
2011-07-20 14:58:45 +08:00
|
|
|
SmallVectorImpl<CanQualType> &ArgTys) = 0;
|
2010-08-31 15:33:07 +08:00
|
|
|
|
|
|
|
/// Build the ABI-specific portion of the parameter list for a
|
|
|
|
/// function. This generally involves a 'this' parameter and
|
|
|
|
/// possibly some extra data for constructors and destructors.
|
|
|
|
///
|
|
|
|
/// ABIs may also choose to override the return type, which has been
|
|
|
|
/// initialized with the formal return type of the function.
|
|
|
|
virtual void BuildInstanceFunctionParams(CodeGenFunction &CGF,
|
|
|
|
QualType &ResTy,
|
|
|
|
FunctionArgList &Params) = 0;
|
|
|
|
|
|
|
|
/// Emit the ABI-specific prolog for the function.
|
|
|
|
virtual void EmitInstanceFunctionProlog(CodeGenFunction &CGF) = 0;
|
|
|
|
|
|
|
|
virtual void EmitReturnFromThunk(CodeGenFunction &CGF,
|
|
|
|
RValue RV, QualType ResultType);
|
2010-09-02 17:58:18 +08:00
|
|
|
|
2012-07-18 01:10:11 +08:00
|
|
|
/// Gets the pure virtual member call function.
|
|
|
|
virtual StringRef GetPureVirtualCallName() = 0;
|
|
|
|
|
2012-10-17 06:56:05 +08:00
|
|
|
/// Gets the deleted virtual member call name.
|
|
|
|
virtual StringRef GetDeletedVirtualCallName() = 0;
|
|
|
|
|
2010-09-02 17:58:18 +08:00
|
|
|
/**************************** Array cookies ******************************/
|
|
|
|
|
|
|
|
/// Returns the extra size required in order to store the array
|
2012-06-22 18:16:05 +08:00
|
|
|
/// cookie for the given new-expression. May return 0 to indicate that no
|
2010-09-02 17:58:18 +08:00
|
|
|
/// array cookie is required.
|
|
|
|
///
|
|
|
|
/// Several cases are filtered out before this method is called:
|
|
|
|
/// - non-array allocations never need a cookie
|
2012-06-20 08:57:15 +08:00
|
|
|
/// - calls to \::operator new(size_t, void*) never need a cookie
|
2010-09-02 17:58:18 +08:00
|
|
|
///
|
2012-06-22 18:16:05 +08:00
|
|
|
/// \param expr - the new-expression being allocated.
|
2011-01-27 17:37:56 +08:00
|
|
|
virtual CharUnits GetArrayCookieSize(const CXXNewExpr *expr);
|
2010-09-02 17:58:18 +08:00
|
|
|
|
|
|
|
/// Initialize the array cookie for the given allocation.
|
|
|
|
///
|
|
|
|
/// \param NewPtr - a char* which is the presumed-non-null
|
|
|
|
/// return value of the allocation function
|
|
|
|
/// \param NumElements - the computed number of elements,
|
2012-05-01 13:23:51 +08:00
|
|
|
/// potentially collapsed from the multidimensional array case;
|
|
|
|
/// always a size_t
|
2010-09-02 17:58:18 +08:00
|
|
|
/// \param ElementType - the base element allocated type,
|
|
|
|
/// i.e. the allocated type after stripping all array types
|
|
|
|
virtual 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);
|
|
|
|
|
|
|
|
/// Reads the array cookie associated with the given pointer,
|
|
|
|
/// if it has one.
|
|
|
|
///
|
|
|
|
/// \param Ptr - a pointer to the first element in the array
|
|
|
|
/// \param ElementType - the base element type of elements of the array
|
|
|
|
/// \param NumElements - an out parameter which will be initialized
|
|
|
|
/// with the number of elements allocated, or zero if there is no
|
|
|
|
/// cookie
|
|
|
|
/// \param AllocPtr - an out parameter which will be initialized
|
|
|
|
/// with a char* pointing to the address returned by the allocation
|
|
|
|
/// function
|
|
|
|
/// \param CookieSize - an out parameter which will be initialized
|
|
|
|
/// with the size of the cookie, or zero if there is no cookie
|
|
|
|
virtual void ReadArrayCookie(CodeGenFunction &CGF, llvm::Value *Ptr,
|
2011-01-27 17:37:56 +08:00
|
|
|
const CXXDeleteExpr *expr,
|
2010-09-02 17:58:18 +08:00
|
|
|
QualType ElementType, llvm::Value *&NumElements,
|
|
|
|
llvm::Value *&AllocPtr, CharUnits &CookieSize);
|
|
|
|
|
2012-05-01 13:23:51 +08:00
|
|
|
protected:
|
|
|
|
/// Returns the extra size required in order to store the array
|
|
|
|
/// cookie for the given type. Assumes that an array cookie is
|
|
|
|
/// required.
|
|
|
|
virtual CharUnits getArrayCookieSizeImpl(QualType elementType);
|
|
|
|
|
|
|
|
/// Reads the array cookie for an allocation which is known to have one.
|
|
|
|
/// This is called by the standard implementation of ReadArrayCookie.
|
|
|
|
///
|
|
|
|
/// \param ptr - a pointer to the allocation made for an array, as a char*
|
|
|
|
/// \param cookieSize - the computed cookie size of an array
|
2012-06-15 15:35:42 +08:00
|
|
|
///
|
2012-05-01 13:23:51 +08:00
|
|
|
/// Other parameters are as above.
|
2012-06-15 15:35:42 +08:00
|
|
|
///
|
2012-05-01 13:23:51 +08:00
|
|
|
/// \return a size_t
|
|
|
|
virtual llvm::Value *readArrayCookieImpl(CodeGenFunction &IGF,
|
|
|
|
llvm::Value *ptr,
|
|
|
|
CharUnits cookieSize);
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
2010-09-08 09:44:27 +08:00
|
|
|
/*************************** Static local guards ****************************/
|
|
|
|
|
2010-11-06 17:44:32 +08:00
|
|
|
/// Emits the guarded initializer and destructor setup for the given
|
|
|
|
/// variable, given that it couldn't be emitted as a constant.
|
2012-02-14 06:16:19 +08:00
|
|
|
/// If \p PerformInit is false, the initialization has been folded to a
|
|
|
|
/// constant and should not be performed.
|
2010-11-06 17:44:32 +08:00
|
|
|
///
|
|
|
|
/// The variable may be:
|
|
|
|
/// - a static local variable
|
|
|
|
/// - a static data member of a class template instantiation
|
|
|
|
virtual void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D,
|
2012-03-31 03:44:53 +08:00
|
|
|
llvm::GlobalVariable *DeclPtr, bool PerformInit);
|
2010-09-08 09:44:27 +08:00
|
|
|
|
2012-05-01 14:13:13 +08:00
|
|
|
/// Emit code to force the execution of a destructor during global
|
|
|
|
/// teardown. The default implementation of this uses atexit.
|
|
|
|
///
|
|
|
|
/// \param dtor - a function taking a single pointer argument
|
|
|
|
/// \param addr - a pointer to pass to the destructor function.
|
|
|
|
virtual void registerGlobalDtor(CodeGenFunction &CGF, llvm::Constant *dtor,
|
|
|
|
llvm::Constant *addr);
|
2012-06-24 07:44:00 +08:00
|
|
|
|
|
|
|
/***************************** Virtual Tables *******************************/
|
|
|
|
|
|
|
|
/// Generates and emits the virtual tables for a class.
|
|
|
|
virtual void EmitVTables(const CXXRecordDecl *Class) = 0;
|
2010-05-26 03:52:27 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Creates an instance of a C++ ABI class.
|
2010-08-22 06:46:04 +08:00
|
|
|
CGCXXABI *CreateARMCXXABI(CodeGenModule &CGM);
|
2010-08-16 11:33:14 +08:00
|
|
|
CGCXXABI *CreateItaniumCXXABI(CodeGenModule &CGM);
|
|
|
|
CGCXXABI *CreateMicrosoftCXXABI(CodeGenModule &CGM);
|
2010-08-22 08:05:51 +08:00
|
|
|
|
2010-05-26 03:52:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|