2008-09-09 05:33:45 +08:00
|
|
|
//===----- CGCall.h - Encapsulate calling convention details ----*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// These classes wrap the information about a call or function
|
|
|
|
// definition used to handle ABI compliancy.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "CGCall.h"
|
|
|
|
#include "CodeGenFunction.h"
|
2008-09-10 08:41:16 +08:00
|
|
|
#include "CodeGenModule.h"
|
2008-09-09 05:33:45 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/AST/Decl.h"
|
|
|
|
#include "clang/AST/DeclObjC.h"
|
2008-09-17 08:51:38 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2008-09-09 05:33:45 +08:00
|
|
|
#include "llvm/ParameterAttributes.h"
|
|
|
|
using namespace clang;
|
|
|
|
using namespace CodeGen;
|
|
|
|
|
|
|
|
/***/
|
|
|
|
|
|
|
|
// FIXME: Use iterator and sidestep silly type array creation.
|
|
|
|
|
2008-09-10 12:01:49 +08:00
|
|
|
CGFunctionInfo::CGFunctionInfo(const FunctionTypeNoProto *FTNP)
|
|
|
|
: IsVariadic(true)
|
|
|
|
{
|
|
|
|
ArgTypes.push_back(FTNP->getResultType());
|
|
|
|
}
|
|
|
|
|
|
|
|
CGFunctionInfo::CGFunctionInfo(const FunctionTypeProto *FTP)
|
|
|
|
: IsVariadic(FTP->isVariadic())
|
|
|
|
{
|
|
|
|
ArgTypes.push_back(FTP->getResultType());
|
|
|
|
for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
|
|
|
|
ArgTypes.push_back(FTP->getArgType(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Is there really any reason to have this still?
|
2008-09-09 05:33:45 +08:00
|
|
|
CGFunctionInfo::CGFunctionInfo(const FunctionDecl *FD)
|
|
|
|
{
|
|
|
|
const FunctionType *FTy = FD->getType()->getAsFunctionType();
|
|
|
|
const FunctionTypeProto *FTP = dyn_cast<FunctionTypeProto>(FTy);
|
2008-09-10 12:01:49 +08:00
|
|
|
|
2008-09-09 05:33:45 +08:00
|
|
|
ArgTypes.push_back(FTy->getResultType());
|
2008-09-10 12:01:49 +08:00
|
|
|
if (FTP) {
|
|
|
|
IsVariadic = FTP->isVariadic();
|
2008-09-09 05:33:45 +08:00
|
|
|
for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
|
|
|
|
ArgTypes.push_back(FTP->getArgType(i));
|
2008-09-10 12:01:49 +08:00
|
|
|
} else {
|
|
|
|
IsVariadic = true;
|
|
|
|
}
|
2008-09-09 05:33:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CGFunctionInfo::CGFunctionInfo(const ObjCMethodDecl *MD,
|
|
|
|
const ASTContext &Context)
|
2008-09-10 12:01:49 +08:00
|
|
|
: IsVariadic(MD->isVariadic())
|
2008-09-09 05:33:45 +08:00
|
|
|
{
|
|
|
|
ArgTypes.push_back(MD->getResultType());
|
|
|
|
ArgTypes.push_back(MD->getSelfDecl()->getType());
|
|
|
|
ArgTypes.push_back(Context.getObjCSelType());
|
|
|
|
for (ObjCMethodDecl::param_const_iterator i = MD->param_begin(),
|
|
|
|
e = MD->param_end(); i != e; ++i)
|
|
|
|
ArgTypes.push_back((*i)->getType());
|
|
|
|
}
|
|
|
|
|
2008-09-10 08:32:18 +08:00
|
|
|
ArgTypeIterator CGFunctionInfo::argtypes_begin() const {
|
|
|
|
return ArgTypes.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
ArgTypeIterator CGFunctionInfo::argtypes_end() const {
|
|
|
|
return ArgTypes.end();
|
2008-09-09 05:33:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/***/
|
|
|
|
|
2008-09-10 08:32:18 +08:00
|
|
|
CGCallInfo::CGCallInfo(QualType _ResultType, const CallArgList &_Args) {
|
|
|
|
ArgTypes.push_back(_ResultType);
|
|
|
|
for (CallArgList::const_iterator i = _Args.begin(), e = _Args.end(); i!=e; ++i)
|
2008-09-09 05:33:45 +08:00
|
|
|
ArgTypes.push_back(i->second);
|
|
|
|
}
|
|
|
|
|
2008-09-10 08:32:18 +08:00
|
|
|
ArgTypeIterator CGCallInfo::argtypes_begin() const {
|
|
|
|
return ArgTypes.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
ArgTypeIterator CGCallInfo::argtypes_end() const {
|
|
|
|
return ArgTypes.end();
|
2008-09-09 05:33:45 +08:00
|
|
|
}
|
2008-09-10 07:27:19 +08:00
|
|
|
|
|
|
|
/***/
|
|
|
|
|
2008-09-11 09:48:57 +08:00
|
|
|
/// ABIArgInfo - Helper class to encapsulate information about how a
|
|
|
|
/// specific C type should be passed to or returned from a function.
|
2008-09-10 10:41:04 +08:00
|
|
|
class ABIArgInfo {
|
|
|
|
public:
|
|
|
|
enum Kind {
|
|
|
|
Default,
|
2008-09-11 09:48:57 +08:00
|
|
|
StructRet, /// Only valid for aggregate return types.
|
|
|
|
|
2008-09-17 08:51:38 +08:00
|
|
|
Coerce, /// Only valid for aggregate return types, the argument
|
|
|
|
/// should be accessed by coercion to a provided type.
|
2008-09-11 09:48:57 +08:00
|
|
|
|
|
|
|
ByVal, /// Only valid for aggregate argument types. The
|
|
|
|
/// structure should be passed "byval" with the
|
|
|
|
/// specified alignment (0 indicates default
|
|
|
|
/// alignment).
|
|
|
|
|
|
|
|
Expand, /// Only valid for aggregate argument types. The
|
|
|
|
/// structure should be expanded into consecutive
|
2008-09-17 08:51:38 +08:00
|
|
|
/// arguments for its constituent fields. Currently
|
|
|
|
/// expand is only allowed on structures whose fields
|
|
|
|
/// are all scalar types or are themselves expandable
|
|
|
|
/// types.
|
2008-09-11 09:48:57 +08:00
|
|
|
|
|
|
|
KindFirst=Default, KindLast=Expand
|
2008-09-10 10:41:04 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
Kind TheKind;
|
2008-09-10 15:04:09 +08:00
|
|
|
const llvm::Type *TypeData;
|
2008-09-11 09:48:57 +08:00
|
|
|
unsigned UIntData;
|
2008-09-10 10:41:04 +08:00
|
|
|
|
2008-09-11 09:48:57 +08:00
|
|
|
ABIArgInfo(Kind K, const llvm::Type *TD=0,
|
|
|
|
unsigned UI=0) : TheKind(K),
|
|
|
|
TypeData(TD),
|
|
|
|
UIntData(0) {}
|
2008-09-10 10:41:04 +08:00
|
|
|
public:
|
|
|
|
static ABIArgInfo getDefault() {
|
2008-09-11 09:48:57 +08:00
|
|
|
return ABIArgInfo(Default);
|
2008-09-10 10:41:04 +08:00
|
|
|
}
|
|
|
|
static ABIArgInfo getStructRet() {
|
2008-09-11 09:48:57 +08:00
|
|
|
return ABIArgInfo(StructRet);
|
2008-09-10 10:41:04 +08:00
|
|
|
}
|
2008-09-10 15:04:09 +08:00
|
|
|
static ABIArgInfo getCoerce(const llvm::Type *T) {
|
|
|
|
assert(T->isSingleValueType() && "Can only coerce to simple types");
|
2008-09-10 10:41:04 +08:00
|
|
|
return ABIArgInfo(Coerce, T);
|
|
|
|
}
|
2008-09-11 09:48:57 +08:00
|
|
|
static ABIArgInfo getByVal(unsigned Alignment) {
|
|
|
|
return ABIArgInfo(ByVal, 0, Alignment);
|
|
|
|
}
|
2008-09-17 08:51:38 +08:00
|
|
|
static ABIArgInfo getExpand() {
|
|
|
|
return ABIArgInfo(Expand);
|
|
|
|
}
|
2008-09-10 10:41:04 +08:00
|
|
|
|
|
|
|
Kind getKind() const { return TheKind; }
|
|
|
|
bool isDefault() const { return TheKind == Default; }
|
|
|
|
bool isStructRet() const { return TheKind == StructRet; }
|
|
|
|
bool isCoerce() const { return TheKind == Coerce; }
|
2008-09-11 09:48:57 +08:00
|
|
|
bool isByVal() const { return TheKind == ByVal; }
|
2008-09-17 08:51:38 +08:00
|
|
|
bool isExpand() const { return TheKind == Expand; }
|
2008-09-10 12:01:49 +08:00
|
|
|
|
|
|
|
// Coerce accessors
|
2008-09-10 15:04:09 +08:00
|
|
|
const llvm::Type *getCoerceToType() const {
|
2008-09-10 12:01:49 +08:00
|
|
|
assert(TheKind == Coerce && "Invalid kind!");
|
|
|
|
return TypeData;
|
|
|
|
}
|
2008-09-11 09:48:57 +08:00
|
|
|
|
|
|
|
// ByVal accessors
|
|
|
|
unsigned getByValAlignment() const {
|
|
|
|
assert(TheKind == ByVal && "Invalid kind!");
|
|
|
|
return UIntData;
|
|
|
|
}
|
2008-09-10 10:41:04 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/***/
|
|
|
|
|
2008-09-10 15:04:09 +08:00
|
|
|
static ABIArgInfo classifyReturnType(QualType RetTy,
|
|
|
|
ASTContext &Context) {
|
2008-09-11 08:04:36 +08:00
|
|
|
assert(!RetTy->isArrayType() &&
|
|
|
|
"Array types cannot be passed directly.");
|
2008-09-10 10:41:04 +08:00
|
|
|
if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
|
2008-09-10 15:04:09 +08:00
|
|
|
uint64_t Size = Context.getTypeSize(RetTy);
|
|
|
|
if (Size == 8) {
|
|
|
|
return ABIArgInfo::getCoerce(llvm::Type::Int8Ty);
|
|
|
|
} else if (Size == 16) {
|
|
|
|
return ABIArgInfo::getCoerce(llvm::Type::Int16Ty);
|
|
|
|
} else if (Size == 32) {
|
|
|
|
return ABIArgInfo::getCoerce(llvm::Type::Int32Ty);
|
|
|
|
} else if (Size == 64) {
|
|
|
|
return ABIArgInfo::getCoerce(llvm::Type::Int64Ty);
|
|
|
|
} else {
|
|
|
|
return ABIArgInfo::getStructRet();
|
|
|
|
}
|
2008-09-10 10:41:04 +08:00
|
|
|
} else {
|
|
|
|
return ABIArgInfo::getDefault();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-11 09:48:57 +08:00
|
|
|
static ABIArgInfo classifyArgumentType(QualType Ty,
|
2008-09-18 04:11:04 +08:00
|
|
|
ASTContext &Context) {
|
2008-09-11 09:48:57 +08:00
|
|
|
assert(!Ty->isArrayType() && "Array types cannot be passed directly.");
|
2008-09-18 04:11:04 +08:00
|
|
|
if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
|
2008-09-11 09:48:57 +08:00
|
|
|
return ABIArgInfo::getByVal(0);
|
|
|
|
} else {
|
|
|
|
return ABIArgInfo::getDefault();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-18 04:11:04 +08:00
|
|
|
static ABIArgInfo getABIReturnInfo(QualType Ty,
|
|
|
|
ASTContext &Context) {
|
|
|
|
ABIArgInfo Info = classifyReturnType(Ty, Context);
|
|
|
|
// Ensure default on aggregate types is StructRet.
|
|
|
|
if (Info.isDefault() && CodeGenFunction::hasAggregateLLVMType(Ty))
|
|
|
|
return ABIArgInfo::getStructRet();
|
|
|
|
return Info;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ABIArgInfo getABIArgumentInfo(QualType Ty,
|
|
|
|
ASTContext &Context) {
|
|
|
|
ABIArgInfo Info = classifyArgumentType(Ty, Context);
|
|
|
|
// Ensure default on aggregate types is ByVal.
|
|
|
|
if (Info.isDefault() && CodeGenFunction::hasAggregateLLVMType(Ty))
|
|
|
|
return ABIArgInfo::getByVal(0);
|
|
|
|
return Info;
|
|
|
|
}
|
|
|
|
|
2008-09-10 10:41:04 +08:00
|
|
|
/***/
|
|
|
|
|
2008-09-17 08:51:38 +08:00
|
|
|
void CodeGenTypes::GetExpandedTypes(QualType Ty,
|
|
|
|
std::vector<const llvm::Type*> &ArgTys) {
|
|
|
|
const RecordType *RT = Ty->getAsStructureType();
|
|
|
|
assert(RT && "Can only expand structure types.");
|
|
|
|
const RecordDecl *RD = RT->getDecl();
|
|
|
|
assert(!RD->hasFlexibleArrayMember() &&
|
|
|
|
"Cannot expand structure with flexible array.");
|
|
|
|
|
|
|
|
for (RecordDecl::field_const_iterator i = RD->field_begin(),
|
|
|
|
e = RD->field_end(); i != e; ++i) {
|
|
|
|
const FieldDecl *FD = *i;
|
|
|
|
assert(!FD->isBitField() &&
|
|
|
|
"Cannot expand structure with bit-field members.");
|
|
|
|
|
|
|
|
QualType FT = FD->getType();
|
|
|
|
if (CodeGenFunction::hasAggregateLLVMType(FT)) {
|
|
|
|
GetExpandedTypes(FT, ArgTys);
|
|
|
|
} else {
|
|
|
|
ArgTys.push_back(ConvertType(FT));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Function::arg_iterator
|
|
|
|
CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
|
|
|
|
llvm::Function::arg_iterator AI) {
|
|
|
|
const RecordType *RT = Ty->getAsStructureType();
|
|
|
|
assert(RT && "Can only expand structure types.");
|
|
|
|
|
|
|
|
RecordDecl *RD = RT->getDecl();
|
|
|
|
assert(LV.isSimple() &&
|
|
|
|
"Unexpected non-simple lvalue during struct expansion.");
|
|
|
|
llvm::Value *Addr = LV.getAddress();
|
|
|
|
for (RecordDecl::field_iterator i = RD->field_begin(),
|
|
|
|
e = RD->field_end(); i != e; ++i) {
|
|
|
|
FieldDecl *FD = *i;
|
|
|
|
QualType FT = FD->getType();
|
|
|
|
|
|
|
|
// FIXME: What are the right qualifiers here?
|
|
|
|
LValue LV = EmitLValueForField(Addr, FD, false, 0);
|
|
|
|
if (CodeGenFunction::hasAggregateLLVMType(FT)) {
|
|
|
|
AI = ExpandTypeFromArgs(FT, LV, AI);
|
|
|
|
} else {
|
|
|
|
EmitStoreThroughLValue(RValue::get(AI), LV, FT);
|
|
|
|
++AI;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return AI;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
|
|
|
|
llvm::SmallVector<llvm::Value*, 16> &Args) {
|
|
|
|
const RecordType *RT = Ty->getAsStructureType();
|
|
|
|
assert(RT && "Can only expand structure types.");
|
|
|
|
|
|
|
|
RecordDecl *RD = RT->getDecl();
|
|
|
|
assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
|
|
|
|
llvm::Value *Addr = RV.getAggregateAddr();
|
|
|
|
for (RecordDecl::field_iterator i = RD->field_begin(),
|
|
|
|
e = RD->field_end(); i != e; ++i) {
|
|
|
|
FieldDecl *FD = *i;
|
|
|
|
QualType FT = FD->getType();
|
|
|
|
|
|
|
|
// FIXME: What are the right qualifiers here?
|
|
|
|
LValue LV = EmitLValueForField(Addr, FD, false, 0);
|
|
|
|
if (CodeGenFunction::hasAggregateLLVMType(FT)) {
|
|
|
|
ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
|
|
|
|
} else {
|
|
|
|
RValue RV = EmitLoadOfLValue(LV, FT);
|
|
|
|
assert(RV.isScalar() &&
|
|
|
|
"Unexpected non-scalar rvalue during struct expansion.");
|
|
|
|
Args.push_back(RV.getScalarVal());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/***/
|
|
|
|
|
2008-09-10 15:00:50 +08:00
|
|
|
const llvm::FunctionType *
|
|
|
|
CodeGenTypes::GetFunctionType(const CGCallInfo &CI, bool IsVariadic) {
|
|
|
|
return GetFunctionType(CI.argtypes_begin(), CI.argtypes_end(), IsVariadic);
|
|
|
|
}
|
|
|
|
|
2008-09-10 12:01:49 +08:00
|
|
|
const llvm::FunctionType *
|
|
|
|
CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI) {
|
2008-09-10 15:00:50 +08:00
|
|
|
return GetFunctionType(FI.argtypes_begin(), FI.argtypes_end(), FI.isVariadic());
|
|
|
|
}
|
|
|
|
|
|
|
|
const llvm::FunctionType *
|
|
|
|
CodeGenTypes::GetFunctionType(ArgTypeIterator begin, ArgTypeIterator end,
|
|
|
|
bool IsVariadic) {
|
2008-09-10 12:01:49 +08:00
|
|
|
std::vector<const llvm::Type*> ArgTys;
|
|
|
|
|
|
|
|
const llvm::Type *ResultType = 0;
|
|
|
|
|
|
|
|
QualType RetTy = *begin;
|
2008-09-18 04:11:04 +08:00
|
|
|
ABIArgInfo RetAI = getABIReturnInfo(RetTy, getContext());
|
2008-09-11 09:48:57 +08:00
|
|
|
switch (RetAI.getKind()) {
|
|
|
|
case ABIArgInfo::ByVal:
|
|
|
|
case ABIArgInfo::Expand:
|
|
|
|
assert(0 && "Invalid ABI kind for return argument");
|
|
|
|
|
2008-09-10 12:01:49 +08:00
|
|
|
case ABIArgInfo::Default:
|
|
|
|
if (RetTy->isVoidType()) {
|
|
|
|
ResultType = llvm::Type::VoidTy;
|
|
|
|
} else {
|
2008-09-10 15:00:50 +08:00
|
|
|
ResultType = ConvertType(RetTy);
|
2008-09-10 12:01:49 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ABIArgInfo::StructRet: {
|
|
|
|
ResultType = llvm::Type::VoidTy;
|
2008-09-10 15:00:50 +08:00
|
|
|
const llvm::Type *STy = ConvertType(RetTy);
|
2008-09-10 12:01:49 +08:00
|
|
|
ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case ABIArgInfo::Coerce:
|
2008-09-10 15:04:09 +08:00
|
|
|
ResultType = RetAI.getCoerceToType();
|
2008-09-10 12:01:49 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (++begin; begin != end; ++begin) {
|
2008-09-18 04:11:04 +08:00
|
|
|
ABIArgInfo AI = getABIArgumentInfo(*begin, getContext());
|
2008-09-10 15:00:50 +08:00
|
|
|
const llvm::Type *Ty = ConvertType(*begin);
|
2008-09-11 09:48:57 +08:00
|
|
|
|
|
|
|
switch (AI.getKind()) {
|
2008-09-17 08:51:38 +08:00
|
|
|
case ABIArgInfo::Coerce:
|
2008-09-11 09:48:57 +08:00
|
|
|
case ABIArgInfo::StructRet:
|
|
|
|
assert(0 && "Invalid ABI kind for non-return argument");
|
|
|
|
|
|
|
|
case ABIArgInfo::ByVal:
|
2008-09-10 12:01:49 +08:00
|
|
|
// byval arguments are always on the stack, which is addr space #0.
|
|
|
|
ArgTys.push_back(llvm::PointerType::getUnqual(Ty));
|
2008-09-11 09:48:57 +08:00
|
|
|
assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ABIArgInfo::Default:
|
|
|
|
ArgTys.push_back(Ty);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ABIArgInfo::Expand:
|
2008-09-17 08:51:38 +08:00
|
|
|
GetExpandedTypes(*begin, ArgTys);
|
2008-09-11 09:48:57 +08:00
|
|
|
break;
|
|
|
|
}
|
2008-09-10 12:01:49 +08:00
|
|
|
}
|
|
|
|
|
2008-09-10 15:00:50 +08:00
|
|
|
return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
|
2008-09-10 12:01:49 +08:00
|
|
|
}
|
|
|
|
|
2008-09-10 08:41:16 +08:00
|
|
|
bool CodeGenModule::ReturnTypeUsesSret(QualType RetTy) {
|
2008-09-18 04:11:04 +08:00
|
|
|
return getABIReturnInfo(RetTy, getContext()).isStructRet();
|
2008-09-10 07:48:28 +08:00
|
|
|
}
|
|
|
|
|
2008-09-10 08:41:16 +08:00
|
|
|
void CodeGenModule::ConstructParamAttrList(const Decl *TargetDecl,
|
2008-09-10 10:41:04 +08:00
|
|
|
ArgTypeIterator begin,
|
|
|
|
ArgTypeIterator end,
|
|
|
|
ParamAttrListType &PAL) {
|
2008-09-10 08:32:18 +08:00
|
|
|
unsigned FuncAttrs = 0;
|
|
|
|
|
|
|
|
if (TargetDecl) {
|
|
|
|
if (TargetDecl->getAttr<NoThrowAttr>())
|
|
|
|
FuncAttrs |= llvm::ParamAttr::NoUnwind;
|
|
|
|
if (TargetDecl->getAttr<NoReturnAttr>())
|
|
|
|
FuncAttrs |= llvm::ParamAttr::NoReturn;
|
|
|
|
}
|
|
|
|
|
2008-09-10 10:41:04 +08:00
|
|
|
QualType RetTy = *begin;
|
2008-09-10 08:32:18 +08:00
|
|
|
unsigned Index = 1;
|
2008-09-18 04:11:04 +08:00
|
|
|
ABIArgInfo RetAI = getABIReturnInfo(RetTy, getContext());
|
2008-09-10 12:01:49 +08:00
|
|
|
switch (RetAI.getKind()) {
|
2008-09-10 10:41:04 +08:00
|
|
|
case ABIArgInfo::Default:
|
|
|
|
if (RetTy->isPromotableIntegerType()) {
|
|
|
|
if (RetTy->isSignedIntegerType()) {
|
|
|
|
FuncAttrs |= llvm::ParamAttr::SExt;
|
|
|
|
} else if (RetTy->isUnsignedIntegerType()) {
|
|
|
|
FuncAttrs |= llvm::ParamAttr::ZExt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ABIArgInfo::StructRet:
|
2008-09-10 08:32:18 +08:00
|
|
|
PAL.push_back(llvm::ParamAttrsWithIndex::get(Index,
|
|
|
|
llvm::ParamAttr::StructRet));
|
|
|
|
++Index;
|
2008-09-10 10:41:04 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ABIArgInfo::Coerce:
|
|
|
|
break;
|
2008-09-11 09:48:57 +08:00
|
|
|
|
|
|
|
case ABIArgInfo::ByVal:
|
|
|
|
case ABIArgInfo::Expand:
|
|
|
|
assert(0 && "Invalid ABI kind for return argument");
|
2008-09-10 08:32:18 +08:00
|
|
|
}
|
2008-09-10 10:41:04 +08:00
|
|
|
|
2008-09-10 08:32:18 +08:00
|
|
|
if (FuncAttrs)
|
|
|
|
PAL.push_back(llvm::ParamAttrsWithIndex::get(0, FuncAttrs));
|
2008-09-17 08:51:38 +08:00
|
|
|
for (++begin; begin != end; ++begin) {
|
2008-09-10 08:32:18 +08:00
|
|
|
QualType ParamType = *begin;
|
|
|
|
unsigned ParamAttrs = 0;
|
2008-09-18 04:11:04 +08:00
|
|
|
ABIArgInfo AI = getABIArgumentInfo(ParamType, getContext());
|
2008-09-11 09:48:57 +08:00
|
|
|
|
|
|
|
switch (AI.getKind()) {
|
|
|
|
case ABIArgInfo::StructRet:
|
2008-09-17 08:51:38 +08:00
|
|
|
case ABIArgInfo::Coerce:
|
2008-09-11 09:48:57 +08:00
|
|
|
assert(0 && "Invalid ABI kind for non-return argument");
|
|
|
|
|
|
|
|
case ABIArgInfo::ByVal:
|
2008-09-10 08:32:18 +08:00
|
|
|
ParamAttrs |= llvm::ParamAttr::ByVal;
|
2008-09-11 09:48:57 +08:00
|
|
|
assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ABIArgInfo::Default:
|
|
|
|
if (ParamType->isPromotableIntegerType()) {
|
|
|
|
if (ParamType->isSignedIntegerType()) {
|
|
|
|
ParamAttrs |= llvm::ParamAttr::SExt;
|
|
|
|
} else if (ParamType->isUnsignedIntegerType()) {
|
|
|
|
ParamAttrs |= llvm::ParamAttr::ZExt;
|
|
|
|
}
|
2008-09-10 08:32:18 +08:00
|
|
|
}
|
2008-09-11 09:48:57 +08:00
|
|
|
break;
|
|
|
|
|
2008-09-17 08:51:38 +08:00
|
|
|
case ABIArgInfo::Expand: {
|
|
|
|
std::vector<const llvm::Type*> Tys;
|
|
|
|
// FIXME: This is rather inefficient. Do we ever actually need
|
|
|
|
// to do anything here? The result should be just reconstructed
|
|
|
|
// on the other side, so extension should be a non-issue.
|
|
|
|
getTypes().GetExpandedTypes(ParamType, Tys);
|
|
|
|
Index += Tys.size();
|
|
|
|
continue;
|
|
|
|
}
|
2008-09-10 08:32:18 +08:00
|
|
|
}
|
2008-09-11 09:48:57 +08:00
|
|
|
|
2008-09-10 08:32:18 +08:00
|
|
|
if (ParamAttrs)
|
|
|
|
PAL.push_back(llvm::ParamAttrsWithIndex::get(Index, ParamAttrs));
|
2008-09-17 08:51:38 +08:00
|
|
|
++Index;
|
2008-09-10 08:32:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-10 07:27:19 +08:00
|
|
|
void CodeGenFunction::EmitFunctionProlog(llvm::Function *Fn,
|
|
|
|
QualType RetTy,
|
|
|
|
const FunctionArgList &Args) {
|
|
|
|
// Emit allocs for param decls. Give the LLVM Argument nodes names.
|
|
|
|
llvm::Function::arg_iterator AI = Fn->arg_begin();
|
|
|
|
|
|
|
|
// Name the struct return argument.
|
2008-09-10 10:41:04 +08:00
|
|
|
if (CGM.ReturnTypeUsesSret(RetTy)) {
|
2008-09-10 07:27:19 +08:00
|
|
|
AI->setName("agg.result");
|
|
|
|
++AI;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
|
2008-09-17 08:51:38 +08:00
|
|
|
i != e; ++i) {
|
2008-09-10 07:27:19 +08:00
|
|
|
const VarDecl *Arg = i->first;
|
2008-09-17 08:51:38 +08:00
|
|
|
QualType Ty = i->second;
|
2008-09-18 04:11:04 +08:00
|
|
|
ABIArgInfo ArgI = getABIArgumentInfo(Ty, getContext());
|
2008-09-11 09:48:57 +08:00
|
|
|
|
|
|
|
switch (ArgI.getKind()) {
|
|
|
|
case ABIArgInfo::ByVal:
|
|
|
|
case ABIArgInfo::Default: {
|
|
|
|
assert(AI != Fn->arg_end() && "Argument mismatch!");
|
|
|
|
llvm::Value* V = AI;
|
2008-09-17 08:51:38 +08:00
|
|
|
if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
|
2008-09-11 09:48:57 +08:00
|
|
|
// This must be a promotion, for something like
|
|
|
|
// "void a(x) short x; {..."
|
2008-09-17 08:51:38 +08:00
|
|
|
V = EmitScalarConversion(V, Ty, Arg->getType());
|
2008-09-10 07:27:19 +08:00
|
|
|
}
|
2008-09-11 09:48:57 +08:00
|
|
|
EmitParmDecl(*Arg, V);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-09-17 08:51:38 +08:00
|
|
|
case ABIArgInfo::Expand: {
|
|
|
|
// If this was structure was expand into multiple arguments then
|
|
|
|
// we need to create a temporary and reconstruct it from the
|
|
|
|
// arguments.
|
|
|
|
std::string Name(Arg->getName());
|
|
|
|
llvm::Value *Temp = CreateTempAlloca(ConvertType(Ty),
|
|
|
|
(Name + ".addr").c_str());
|
|
|
|
// FIXME: What are the right qualifiers here?
|
|
|
|
llvm::Function::arg_iterator End =
|
|
|
|
ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI);
|
|
|
|
EmitParmDecl(*Arg, Temp);
|
|
|
|
|
|
|
|
// Name the arguments used in expansion and increment AI.
|
|
|
|
unsigned Index = 0;
|
|
|
|
for (; AI != End; ++AI, ++Index)
|
|
|
|
AI->setName(Name + "." + llvm::utostr(Index));
|
|
|
|
continue;
|
|
|
|
}
|
2008-09-11 09:48:57 +08:00
|
|
|
|
2008-09-17 08:51:38 +08:00
|
|
|
case ABIArgInfo::Coerce:
|
2008-09-11 09:48:57 +08:00
|
|
|
case ABIArgInfo::StructRet:
|
|
|
|
assert(0 && "Invalid ABI kind for non-return argument");
|
|
|
|
}
|
2008-09-17 08:51:38 +08:00
|
|
|
|
|
|
|
++AI;
|
2008-09-10 07:27:19 +08:00
|
|
|
}
|
|
|
|
assert(AI == Fn->arg_end() && "Argument mismatch!");
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeGenFunction::EmitFunctionEpilog(QualType RetTy,
|
|
|
|
llvm::Value *ReturnValue) {
|
2008-09-10 10:41:04 +08:00
|
|
|
llvm::Value *RV = 0;
|
|
|
|
|
|
|
|
// Functions with no result always return void.
|
|
|
|
if (ReturnValue) {
|
2008-09-18 04:11:04 +08:00
|
|
|
ABIArgInfo RetAI = getABIReturnInfo(RetTy, getContext());
|
2008-09-10 10:41:04 +08:00
|
|
|
|
|
|
|
switch (RetAI.getKind()) {
|
|
|
|
case ABIArgInfo::StructRet:
|
2008-09-10 07:27:19 +08:00
|
|
|
EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy);
|
2008-09-10 10:41:04 +08:00
|
|
|
break;
|
2008-09-11 09:48:57 +08:00
|
|
|
|
2008-09-10 10:41:04 +08:00
|
|
|
case ABIArgInfo::Default:
|
|
|
|
RV = Builder.CreateLoad(ReturnValue);
|
|
|
|
break;
|
|
|
|
|
2008-09-10 15:04:09 +08:00
|
|
|
case ABIArgInfo::Coerce: {
|
|
|
|
const llvm::Type *CoerceToPTy =
|
|
|
|
llvm::PointerType::getUnqual(RetAI.getCoerceToType());
|
|
|
|
RV = Builder.CreateLoad(Builder.CreateBitCast(ReturnValue, CoerceToPTy));
|
2008-09-11 09:48:57 +08:00
|
|
|
break;
|
2008-09-10 15:04:09 +08:00
|
|
|
}
|
2008-09-11 09:48:57 +08:00
|
|
|
|
|
|
|
case ABIArgInfo::ByVal:
|
|
|
|
case ABIArgInfo::Expand:
|
|
|
|
assert(0 && "Invalid ABI kind for return argument");
|
2008-09-10 07:27:19 +08:00
|
|
|
}
|
|
|
|
}
|
2008-09-10 10:41:04 +08:00
|
|
|
|
|
|
|
if (RV) {
|
|
|
|
Builder.CreateRet(RV);
|
|
|
|
} else {
|
|
|
|
Builder.CreateRetVoid();
|
|
|
|
}
|
2008-09-10 07:27:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
RValue CodeGenFunction::EmitCall(llvm::Value *Callee,
|
2008-09-10 10:41:04 +08:00
|
|
|
QualType RetTy,
|
2008-09-10 07:27:19 +08:00
|
|
|
const CallArgList &CallArgs) {
|
|
|
|
llvm::SmallVector<llvm::Value*, 16> Args;
|
|
|
|
|
|
|
|
// Handle struct-return functions by passing a pointer to the
|
|
|
|
// location that we would like to return into.
|
2008-09-18 04:11:04 +08:00
|
|
|
ABIArgInfo RetAI = getABIReturnInfo(RetTy, getContext());
|
2008-09-10 10:41:04 +08:00
|
|
|
switch (RetAI.getKind()) {
|
|
|
|
case ABIArgInfo::StructRet:
|
2008-09-10 07:27:19 +08:00
|
|
|
// Create a temporary alloca to hold the result of the call. :(
|
2008-09-17 08:51:38 +08:00
|
|
|
Args.push_back(CreateTempAlloca(ConvertType(RetTy)));
|
2008-09-10 10:41:04 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ABIArgInfo::Default:
|
|
|
|
case ABIArgInfo::Coerce:
|
|
|
|
break;
|
2008-09-11 09:48:57 +08:00
|
|
|
|
|
|
|
case ABIArgInfo::ByVal:
|
|
|
|
case ABIArgInfo::Expand:
|
|
|
|
assert(0 && "Invalid ABI kind for return argument");
|
2008-09-10 07:27:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
|
|
|
|
I != E; ++I) {
|
2008-09-18 04:11:04 +08:00
|
|
|
ABIArgInfo ArgInfo = getABIArgumentInfo(I->second, getContext());
|
2008-09-10 07:27:19 +08:00
|
|
|
RValue RV = I->first;
|
2008-09-17 08:51:38 +08:00
|
|
|
|
|
|
|
switch (ArgInfo.getKind()) {
|
|
|
|
case ABIArgInfo::ByVal: // Default is byval
|
|
|
|
case ABIArgInfo::Default:
|
|
|
|
if (RV.isScalar()) {
|
|
|
|
Args.push_back(RV.getScalarVal());
|
|
|
|
} else if (RV.isComplex()) {
|
|
|
|
// Make a temporary alloca to pass the argument.
|
|
|
|
Args.push_back(CreateTempAlloca(ConvertType(I->second)));
|
|
|
|
StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
|
|
|
|
} else {
|
|
|
|
Args.push_back(RV.getAggregateAddr());
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ABIArgInfo::StructRet:
|
|
|
|
case ABIArgInfo::Coerce:
|
|
|
|
assert(0 && "Invalid ABI kind for non-return argument");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ABIArgInfo::Expand:
|
|
|
|
ExpandTypeToArgs(I->second, RV, Args);
|
|
|
|
break;
|
2008-09-10 07:27:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::CallInst *CI = Builder.CreateCall(Callee,&Args[0],&Args[0]+Args.size());
|
2008-09-10 10:41:04 +08:00
|
|
|
CGCallInfo CallInfo(RetTy, CallArgs);
|
2008-09-10 07:27:19 +08:00
|
|
|
|
2008-09-10 08:32:18 +08:00
|
|
|
// FIXME: Provide TargetDecl so nounwind, noreturn, etc, etc get set.
|
2008-09-10 07:27:19 +08:00
|
|
|
CodeGen::ParamAttrListType ParamAttrList;
|
2008-09-10 08:41:16 +08:00
|
|
|
CGM.ConstructParamAttrList(0,
|
|
|
|
CallInfo.argtypes_begin(), CallInfo.argtypes_end(),
|
|
|
|
ParamAttrList);
|
2008-09-10 07:27:19 +08:00
|
|
|
CI->setParamAttrs(llvm::PAListPtr::get(ParamAttrList.begin(),
|
|
|
|
ParamAttrList.size()));
|
|
|
|
|
|
|
|
if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee))
|
|
|
|
CI->setCallingConv(F->getCallingConv());
|
|
|
|
if (CI->getType() != llvm::Type::VoidTy)
|
|
|
|
CI->setName("call");
|
2008-09-10 10:41:04 +08:00
|
|
|
|
|
|
|
switch (RetAI.getKind()) {
|
|
|
|
case ABIArgInfo::StructRet:
|
|
|
|
if (RetTy->isAnyComplexType())
|
2008-09-17 08:51:38 +08:00
|
|
|
return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
|
2008-09-10 10:41:04 +08:00
|
|
|
else
|
|
|
|
// Struct return.
|
2008-09-17 08:51:38 +08:00
|
|
|
return RValue::getAggregate(Args[0]);
|
2008-09-11 09:48:57 +08:00
|
|
|
|
2008-09-10 10:41:04 +08:00
|
|
|
case ABIArgInfo::Default:
|
|
|
|
return RValue::get(RetTy->isVoidType() ? 0 : CI);
|
|
|
|
|
2008-09-10 15:04:09 +08:00
|
|
|
case ABIArgInfo::Coerce: {
|
|
|
|
const llvm::Type *CoerceToPTy =
|
|
|
|
llvm::PointerType::getUnqual(RetAI.getCoerceToType());
|
|
|
|
llvm::Value *V = CreateTempAlloca(ConvertType(RetTy), "tmp");
|
|
|
|
Builder.CreateStore(CI, Builder.CreateBitCast(V, CoerceToPTy));
|
|
|
|
return RValue::getAggregate(V);
|
|
|
|
}
|
2008-09-11 09:48:57 +08:00
|
|
|
|
|
|
|
case ABIArgInfo::ByVal:
|
|
|
|
case ABIArgInfo::Expand:
|
|
|
|
assert(0 && "Invalid ABI kind for return argument");
|
2008-09-10 07:27:19 +08:00
|
|
|
}
|
2008-09-10 10:41:04 +08:00
|
|
|
|
|
|
|
assert(0 && "Unhandled ABIArgInfo::Kind");
|
|
|
|
return RValue::get(0);
|
2008-09-10 07:27:19 +08:00
|
|
|
}
|