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-10-14 01:02:26 +08:00
|
|
|
#include "clang/Basic/TargetInfo.h"
|
2008-09-09 05:33:45 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/AST/Decl.h"
|
|
|
|
#include "clang/AST/DeclObjC.h"
|
2009-01-29 16:13:58 +08:00
|
|
|
#include "clang/AST/RecordLayout.h"
|
2008-09-17 08:51:38 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2008-09-24 09:01:36 +08:00
|
|
|
#include "llvm/Attributes.h"
|
2009-01-24 16:32:22 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2009-01-27 09:36:03 +08:00
|
|
|
#include "llvm/Target/TargetData.h"
|
2009-02-03 09:05:53 +08:00
|
|
|
|
|
|
|
#include "ABIInfo.h"
|
|
|
|
|
2008-09-09 05:33:45 +08:00
|
|
|
using namespace clang;
|
|
|
|
using namespace CodeGen;
|
|
|
|
|
|
|
|
/***/
|
|
|
|
|
|
|
|
// FIXME: Use iterator and sidestep silly type array creation.
|
|
|
|
|
2009-02-03 07:23:47 +08:00
|
|
|
const
|
|
|
|
CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionTypeNoProto *FTNP) {
|
|
|
|
return getFunctionInfo(FTNP->getResultType(),
|
|
|
|
llvm::SmallVector<QualType, 16>());
|
2008-09-10 12:01:49 +08:00
|
|
|
}
|
|
|
|
|
2009-02-03 07:23:47 +08:00
|
|
|
const
|
|
|
|
CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionTypeProto *FTP) {
|
|
|
|
llvm::SmallVector<QualType, 16> ArgTys;
|
|
|
|
// FIXME: Kill copy.
|
2008-09-10 12:01:49 +08:00
|
|
|
for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
|
2009-02-03 07:23:47 +08:00
|
|
|
ArgTys.push_back(FTP->getArgType(i));
|
|
|
|
return getFunctionInfo(FTP->getResultType(), ArgTys);
|
2008-09-10 12:01:49 +08:00
|
|
|
}
|
|
|
|
|
2009-02-03 07:23:47 +08:00
|
|
|
const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionDecl *FD) {
|
2008-09-09 05:33:45 +08:00
|
|
|
const FunctionType *FTy = FD->getType()->getAsFunctionType();
|
2009-02-03 07:23:47 +08:00
|
|
|
if (const FunctionTypeProto *FTP = dyn_cast<FunctionTypeProto>(FTy))
|
|
|
|
return getFunctionInfo(FTP);
|
|
|
|
return getFunctionInfo(cast<FunctionTypeNoProto>(FTy));
|
2008-09-09 05:33:45 +08:00
|
|
|
}
|
|
|
|
|
2009-02-03 07:23:47 +08:00
|
|
|
const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const ObjCMethodDecl *MD) {
|
|
|
|
llvm::SmallVector<QualType, 16> ArgTys;
|
|
|
|
ArgTys.push_back(MD->getSelfDecl()->getType());
|
|
|
|
ArgTys.push_back(Context.getObjCSelType());
|
|
|
|
// FIXME: Kill copy?
|
2008-09-09 05:33:45 +08:00
|
|
|
for (ObjCMethodDecl::param_const_iterator i = MD->param_begin(),
|
|
|
|
e = MD->param_end(); i != e; ++i)
|
2009-02-03 07:23:47 +08:00
|
|
|
ArgTys.push_back((*i)->getType());
|
|
|
|
return getFunctionInfo(MD->getResultType(), ArgTys);
|
2008-09-09 05:33:45 +08:00
|
|
|
}
|
|
|
|
|
2009-02-03 07:23:47 +08:00
|
|
|
const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
|
|
|
|
const CallArgList &Args) {
|
|
|
|
// FIXME: Kill copy.
|
|
|
|
llvm::SmallVector<QualType, 16> ArgTys;
|
2009-01-31 10:19:00 +08:00
|
|
|
for (CallArgList::const_iterator i = Args.begin(), e = Args.end();
|
|
|
|
i != e; ++i)
|
2009-02-03 07:23:47 +08:00
|
|
|
ArgTys.push_back(i->second);
|
|
|
|
return getFunctionInfo(ResTy, ArgTys);
|
2008-09-09 05:33:45 +08:00
|
|
|
}
|
|
|
|
|
2009-02-03 07:23:47 +08:00
|
|
|
const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
|
|
|
|
const FunctionArgList &Args) {
|
|
|
|
// FIXME: Kill copy.
|
|
|
|
llvm::SmallVector<QualType, 16> ArgTys;
|
2009-02-03 05:43:58 +08:00
|
|
|
for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
|
|
|
|
i != e; ++i)
|
2009-02-03 07:23:47 +08:00
|
|
|
ArgTys.push_back(i->second);
|
|
|
|
return getFunctionInfo(ResTy, ArgTys);
|
|
|
|
}
|
|
|
|
|
|
|
|
const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
|
|
|
|
const llvm::SmallVector<QualType, 16> &ArgTys) {
|
2009-02-03 08:07:12 +08:00
|
|
|
// Lookup or create unique function info.
|
|
|
|
llvm::FoldingSetNodeID ID;
|
|
|
|
CGFunctionInfo::Profile(ID, ResTy, ArgTys.begin(), ArgTys.end());
|
|
|
|
|
|
|
|
void *InsertPos = 0;
|
|
|
|
CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, InsertPos);
|
|
|
|
if (FI)
|
|
|
|
return *FI;
|
|
|
|
|
|
|
|
FI = new CGFunctionInfo(ResTy, ArgTys);
|
|
|
|
FunctionInfos.InsertNode(FI, InsertPos);
|
|
|
|
return *FI;
|
2009-02-03 07:23:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/***/
|
|
|
|
|
|
|
|
CGFunctionInfo::CGFunctionInfo(QualType ResTy,
|
|
|
|
const llvm::SmallVector<QualType, 16> &ArgTys) {
|
|
|
|
ArgTypes.push_back(ResTy);
|
|
|
|
ArgTypes.insert(ArgTypes.end(), ArgTys.begin(), ArgTys.end());
|
2009-02-03 05:43:58 +08:00
|
|
|
}
|
|
|
|
|
2009-02-03 07:43:58 +08:00
|
|
|
CGFunctionInfo::arg_iterator CGFunctionInfo::arg_begin() const {
|
|
|
|
return ArgTypes.begin()+1;
|
2008-09-10 08:32:18 +08:00
|
|
|
}
|
|
|
|
|
2009-02-03 07:43:58 +08:00
|
|
|
CGFunctionInfo::arg_iterator CGFunctionInfo::arg_end() const {
|
2008-09-10 08:32:18 +08:00
|
|
|
return ArgTypes.end();
|
2008-09-09 05:33:45 +08:00
|
|
|
}
|
2008-09-10 07:27:19 +08:00
|
|
|
|
|
|
|
/***/
|
|
|
|
|
2008-10-14 01:02:26 +08:00
|
|
|
ABIInfo::~ABIInfo() {}
|
|
|
|
|
2008-09-18 05:22:33 +08:00
|
|
|
/// isEmptyStruct - Return true iff a structure has no non-empty
|
|
|
|
/// members. Note that a structure with a flexible array member is not
|
|
|
|
/// considered empty.
|
|
|
|
static bool isEmptyStruct(QualType T) {
|
|
|
|
const RecordType *RT = T->getAsStructureType();
|
|
|
|
if (!RT)
|
|
|
|
return 0;
|
|
|
|
const RecordDecl *RD = RT->getDecl();
|
|
|
|
if (RD->hasFlexibleArrayMember())
|
|
|
|
return false;
|
2009-01-10 01:18:27 +08:00
|
|
|
for (RecordDecl::field_iterator i = RD->field_begin(),
|
2008-09-18 05:22:33 +08:00
|
|
|
e = RD->field_end(); i != e; ++i) {
|
|
|
|
const FieldDecl *FD = *i;
|
|
|
|
if (!isEmptyStruct(FD->getType()))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// isSingleElementStruct - Determine if a structure is a "single
|
|
|
|
/// element struct", i.e. it has exactly one non-empty field or
|
|
|
|
/// exactly one field which is itself a single element
|
|
|
|
/// struct. Structures with flexible array members are never
|
|
|
|
/// considered single element structs.
|
|
|
|
///
|
|
|
|
/// \return The field declaration for the single non-empty field, if
|
|
|
|
/// it exists.
|
|
|
|
static const FieldDecl *isSingleElementStruct(QualType T) {
|
|
|
|
const RecordType *RT = T->getAsStructureType();
|
|
|
|
if (!RT)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
const RecordDecl *RD = RT->getDecl();
|
|
|
|
if (RD->hasFlexibleArrayMember())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
const FieldDecl *Found = 0;
|
2009-01-10 01:18:27 +08:00
|
|
|
for (RecordDecl::field_iterator i = RD->field_begin(),
|
2008-09-18 05:22:33 +08:00
|
|
|
e = RD->field_end(); i != e; ++i) {
|
|
|
|
const FieldDecl *FD = *i;
|
|
|
|
QualType FT = FD->getType();
|
|
|
|
|
|
|
|
if (isEmptyStruct(FT)) {
|
|
|
|
// Ignore
|
|
|
|
} else if (Found) {
|
|
|
|
return 0;
|
|
|
|
} else if (!CodeGenFunction::hasAggregateLLVMType(FT)) {
|
|
|
|
Found = FD;
|
|
|
|
} else {
|
|
|
|
Found = isSingleElementStruct(FT);
|
|
|
|
if (!Found)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Found;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
|
|
|
|
if (!Ty->getAsBuiltinType() && !Ty->isPointerType())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
uint64_t Size = Context.getTypeSize(Ty);
|
|
|
|
return Size == 32 || Size == 64;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool areAllFields32Or64BitBasicType(const RecordDecl *RD,
|
|
|
|
ASTContext &Context) {
|
2009-01-10 01:18:27 +08:00
|
|
|
for (RecordDecl::field_iterator i = RD->field_begin(),
|
2008-09-18 05:22:33 +08:00
|
|
|
e = RD->field_end(); i != e; ++i) {
|
|
|
|
const FieldDecl *FD = *i;
|
|
|
|
|
|
|
|
if (!is32Or64BitBasicType(FD->getType(), Context))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// If this is a bit-field we need to make sure it is still a
|
|
|
|
// 32-bit or 64-bit type.
|
|
|
|
if (Expr *BW = FD->getBitWidth()) {
|
|
|
|
unsigned Width = BW->getIntegerConstantExprValue(Context).getZExtValue();
|
|
|
|
if (Width <= 16)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-10-14 01:02:26 +08:00
|
|
|
namespace {
|
|
|
|
/// DefaultABIInfo - The default implementation for ABI specific
|
|
|
|
/// details. This implementation provides information which results in
|
|
|
|
/// sensible LLVM IR generation, but does not conform to any
|
|
|
|
/// particular ABI.
|
|
|
|
class DefaultABIInfo : public ABIInfo {
|
|
|
|
virtual ABIArgInfo classifyReturnType(QualType RetTy,
|
|
|
|
ASTContext &Context) const;
|
|
|
|
|
|
|
|
virtual ABIArgInfo classifyArgumentType(QualType RetTy,
|
|
|
|
ASTContext &Context) const;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// X86_32ABIInfo - The X86-32 ABI information.
|
|
|
|
class X86_32ABIInfo : public ABIInfo {
|
|
|
|
public:
|
|
|
|
virtual ABIArgInfo classifyReturnType(QualType RetTy,
|
|
|
|
ASTContext &Context) const;
|
|
|
|
|
|
|
|
virtual ABIArgInfo classifyArgumentType(QualType RetTy,
|
|
|
|
ASTContext &Context) const;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
|
|
|
|
ASTContext &Context) const {
|
2008-09-10 10:41:04 +08:00
|
|
|
if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
|
2008-09-18 05:22:33 +08:00
|
|
|
// Classify "single element" structs as their element type.
|
|
|
|
const FieldDecl *SeltFD = isSingleElementStruct(RetTy);
|
|
|
|
if (SeltFD) {
|
|
|
|
QualType SeltTy = SeltFD->getType()->getDesugaredType();
|
|
|
|
if (const BuiltinType *BT = SeltTy->getAsBuiltinType()) {
|
|
|
|
// FIXME: This is gross, it would be nice if we could just
|
|
|
|
// pass back SeltTy and have clients deal with it. Is it worth
|
|
|
|
// supporting coerce to both LLVM and clang Types?
|
|
|
|
if (BT->isIntegerType()) {
|
|
|
|
uint64_t Size = Context.getTypeSize(SeltTy);
|
|
|
|
return ABIArgInfo::getCoerce(llvm::IntegerType::get((unsigned) Size));
|
|
|
|
} else if (BT->getKind() == BuiltinType::Float) {
|
|
|
|
return ABIArgInfo::getCoerce(llvm::Type::FloatTy);
|
|
|
|
} else if (BT->getKind() == BuiltinType::Double) {
|
|
|
|
return ABIArgInfo::getCoerce(llvm::Type::DoubleTy);
|
|
|
|
}
|
|
|
|
} else if (SeltTy->isPointerType()) {
|
|
|
|
// FIXME: It would be really nice if this could come out as
|
|
|
|
// the proper pointer type.
|
|
|
|
llvm::Type *PtrTy =
|
|
|
|
llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
|
|
|
|
return ABIArgInfo::getCoerce(PtrTy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-10-14 01:02:26 +08:00
|
|
|
ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
|
|
|
|
ASTContext &Context) const {
|
2008-09-18 04:11:04 +08:00
|
|
|
if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
|
2008-09-18 05:22:33 +08:00
|
|
|
// Structures with flexible arrays are always byval.
|
|
|
|
if (const RecordType *RT = Ty->getAsStructureType())
|
|
|
|
if (RT->getDecl()->hasFlexibleArrayMember())
|
|
|
|
return ABIArgInfo::getByVal(0);
|
|
|
|
|
|
|
|
// Expand empty structs (i.e. ignore)
|
|
|
|
uint64_t Size = Context.getTypeSize(Ty);
|
|
|
|
if (Ty->isStructureType() && Size == 0)
|
|
|
|
return ABIArgInfo::getExpand();
|
|
|
|
|
|
|
|
// Expand structs with size <= 128-bits which consist only of
|
|
|
|
// basic types (int, long long, float, double, xxx*). This is
|
|
|
|
// non-recursive and does not ignore empty fields.
|
|
|
|
if (const RecordType *RT = Ty->getAsStructureType()) {
|
|
|
|
if (Context.getTypeSize(Ty) <= 4*32 &&
|
|
|
|
areAllFields32Or64BitBasicType(RT->getDecl(), Context))
|
|
|
|
return ABIArgInfo::getExpand();
|
|
|
|
}
|
|
|
|
|
2008-09-11 09:48:57 +08:00
|
|
|
return ABIArgInfo::getByVal(0);
|
|
|
|
} else {
|
|
|
|
return ABIArgInfo::getDefault();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-24 16:32:22 +08:00
|
|
|
namespace {
|
2009-01-31 08:06:58 +08:00
|
|
|
/// X86_64ABIInfo - The X86_64 ABI information.
|
2009-01-24 16:32:22 +08:00
|
|
|
class X86_64ABIInfo : public ABIInfo {
|
|
|
|
enum Class {
|
|
|
|
Integer = 0,
|
|
|
|
SSE,
|
|
|
|
SSEUp,
|
|
|
|
X87,
|
|
|
|
X87Up,
|
|
|
|
ComplexX87,
|
|
|
|
NoClass,
|
|
|
|
Memory
|
|
|
|
};
|
|
|
|
|
2009-01-30 16:09:32 +08:00
|
|
|
/// merge - Implement the X86_64 ABI merging algorithm.
|
|
|
|
///
|
2009-01-31 08:06:58 +08:00
|
|
|
/// Merge an accumulating classification \arg Accum with a field
|
|
|
|
/// classification \arg Field.
|
|
|
|
///
|
|
|
|
/// \param Accum - The accumulating classification. This should
|
|
|
|
/// always be either NoClass or the result of a previous merge
|
|
|
|
/// call. In addition, this should never be Memory (the caller
|
|
|
|
/// should just return Memory for the aggregate).
|
|
|
|
Class merge(Class Accum, Class Field) const;
|
2009-01-30 16:09:32 +08:00
|
|
|
|
2009-01-24 16:32:22 +08:00
|
|
|
/// classify - Determine the x86_64 register classes in which the
|
|
|
|
/// given type T should be passed.
|
|
|
|
///
|
2009-01-31 08:06:58 +08:00
|
|
|
/// \param Lo - The classification for the parts of the type
|
|
|
|
/// residing in the low word of the containing object.
|
|
|
|
///
|
|
|
|
/// \param Hi - The classification for the parts of the type
|
|
|
|
/// residing in the high word of the containing object.
|
|
|
|
///
|
|
|
|
/// \param OffsetBase - The bit offset of this type in the
|
2009-01-31 06:40:15 +08:00
|
|
|
/// containing object. Some parameters are classified different
|
|
|
|
/// depending on whether they straddle an eightbyte boundary.
|
2009-01-24 16:32:22 +08:00
|
|
|
///
|
|
|
|
/// If a word is unused its result will be NoClass; if a type should
|
|
|
|
/// be passed in Memory then at least the classification of \arg Lo
|
|
|
|
/// will be Memory.
|
|
|
|
///
|
|
|
|
/// The \arg Lo class will be NoClass iff the argument is ignored.
|
|
|
|
///
|
|
|
|
/// If the \arg Lo class is ComplexX87, then the \arg Hi class will
|
|
|
|
/// be NoClass.
|
2009-01-30 08:47:38 +08:00
|
|
|
void classify(QualType T, ASTContext &Context, uint64_t OffsetBase,
|
2009-01-24 16:32:22 +08:00
|
|
|
Class &Lo, Class &Hi) const;
|
2009-01-31 08:06:58 +08:00
|
|
|
|
2009-01-24 16:32:22 +08:00
|
|
|
public:
|
|
|
|
virtual ABIArgInfo classifyReturnType(QualType RetTy,
|
|
|
|
ASTContext &Context) const;
|
|
|
|
|
|
|
|
virtual ABIArgInfo classifyArgumentType(QualType RetTy,
|
|
|
|
ASTContext &Context) const;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2009-01-31 08:06:58 +08:00
|
|
|
X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum,
|
|
|
|
Class Field) const {
|
2009-01-30 16:09:32 +08:00
|
|
|
// AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
|
|
|
|
// classified recursively so that always two fields are
|
|
|
|
// considered. The resulting class is calculated according to
|
|
|
|
// the classes of the fields in the eightbyte:
|
|
|
|
//
|
|
|
|
// (a) If both classes are equal, this is the resulting class.
|
|
|
|
//
|
|
|
|
// (b) If one of the classes is NO_CLASS, the resulting class is
|
|
|
|
// the other class.
|
|
|
|
//
|
|
|
|
// (c) If one of the classes is MEMORY, the result is the MEMORY
|
|
|
|
// class.
|
|
|
|
//
|
|
|
|
// (d) If one of the classes is INTEGER, the result is the
|
|
|
|
// INTEGER.
|
|
|
|
//
|
|
|
|
// (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
|
|
|
|
// MEMORY is used as class.
|
|
|
|
//
|
|
|
|
// (f) Otherwise class SSE is used.
|
2009-01-31 08:06:58 +08:00
|
|
|
assert((Accum == NoClass || Accum == Integer ||
|
|
|
|
Accum == SSE || Accum == SSEUp) &&
|
|
|
|
"Invalid accumulated classification during merge.");
|
|
|
|
if (Accum == Field || Field == NoClass)
|
|
|
|
return Accum;
|
|
|
|
else if (Field == Memory)
|
|
|
|
return Memory;
|
|
|
|
else if (Accum == NoClass)
|
|
|
|
return Field;
|
|
|
|
else if (Accum == Integer || Field == Integer)
|
|
|
|
return Integer;
|
|
|
|
else if (Field == X87 || Field == X87Up || Field == ComplexX87)
|
|
|
|
return Memory;
|
2009-01-30 16:09:32 +08:00
|
|
|
else
|
2009-01-31 08:06:58 +08:00
|
|
|
return SSE;
|
2009-01-30 16:09:32 +08:00
|
|
|
}
|
|
|
|
|
2009-01-24 16:32:22 +08:00
|
|
|
void X86_64ABIInfo::classify(QualType Ty,
|
|
|
|
ASTContext &Context,
|
2009-01-30 08:47:38 +08:00
|
|
|
uint64_t OffsetBase,
|
2009-01-24 16:32:22 +08:00
|
|
|
Class &Lo, Class &Hi) const {
|
2009-02-03 02:06:39 +08:00
|
|
|
// FIXME: This code can be simplified by introducing a simple value
|
|
|
|
// class for Class pairs with appropriate constructor methods for
|
|
|
|
// the various situations.
|
|
|
|
|
2009-01-31 08:06:58 +08:00
|
|
|
Lo = Hi = NoClass;
|
|
|
|
|
|
|
|
Class &Current = OffsetBase < 64 ? Lo : Hi;
|
|
|
|
Current = Memory;
|
|
|
|
|
2009-01-24 16:32:22 +08:00
|
|
|
if (const BuiltinType *BT = Ty->getAsBuiltinType()) {
|
|
|
|
BuiltinType::Kind k = BT->getKind();
|
|
|
|
|
2009-01-27 05:26:08 +08:00
|
|
|
if (k == BuiltinType::Void) {
|
2009-01-31 08:06:58 +08:00
|
|
|
Current = NoClass;
|
2009-01-27 05:26:08 +08:00
|
|
|
} else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
|
2009-01-31 08:06:58 +08:00
|
|
|
Current = Integer;
|
2009-01-24 16:32:22 +08:00
|
|
|
} else if (k == BuiltinType::Float || k == BuiltinType::Double) {
|
2009-01-31 08:06:58 +08:00
|
|
|
Current = SSE;
|
2009-01-24 16:32:22 +08:00
|
|
|
} else if (k == BuiltinType::LongDouble) {
|
|
|
|
Lo = X87;
|
|
|
|
Hi = X87Up;
|
|
|
|
}
|
2009-01-27 10:01:34 +08:00
|
|
|
// FIXME: _Decimal32 and _Decimal64 are SSE.
|
|
|
|
// FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
|
2009-01-24 16:32:22 +08:00
|
|
|
// FIXME: __int128 is (Integer, Integer).
|
|
|
|
} else if (Ty->isPointerLikeType() || Ty->isBlockPointerType() ||
|
|
|
|
Ty->isObjCQualifiedInterfaceType()) {
|
2009-01-31 08:06:58 +08:00
|
|
|
Current = Integer;
|
2009-01-27 10:01:34 +08:00
|
|
|
} else if (const VectorType *VT = Ty->getAsVectorType()) {
|
2009-01-30 08:47:38 +08:00
|
|
|
uint64_t Size = Context.getTypeSize(VT);
|
2009-01-27 10:01:34 +08:00
|
|
|
if (Size == 64) {
|
2009-01-31 03:38:39 +08:00
|
|
|
// gcc passes <1 x double> in memory.
|
2009-01-31 08:06:58 +08:00
|
|
|
if (VT->getElementType() == Context.DoubleTy)
|
2009-01-31 03:38:39 +08:00
|
|
|
return;
|
|
|
|
|
2009-01-31 08:06:58 +08:00
|
|
|
Current = SSE;
|
2009-01-31 02:40:10 +08:00
|
|
|
|
|
|
|
// If this type crosses an eightbyte boundary, it should be
|
|
|
|
// split.
|
2009-01-31 06:40:15 +08:00
|
|
|
if (OffsetBase && OffsetBase != 64)
|
2009-01-31 02:40:10 +08:00
|
|
|
Hi = Lo;
|
2009-01-27 10:01:34 +08:00
|
|
|
} else if (Size == 128) {
|
|
|
|
Lo = SSE;
|
|
|
|
Hi = SSEUp;
|
|
|
|
}
|
2009-01-24 16:32:22 +08:00
|
|
|
} else if (const ComplexType *CT = Ty->getAsComplexType()) {
|
|
|
|
QualType ET = CT->getElementType();
|
|
|
|
|
2009-01-31 02:40:10 +08:00
|
|
|
uint64_t Size = Context.getTypeSize(Ty);
|
2009-01-29 15:22:20 +08:00
|
|
|
if (ET->isIntegerType()) {
|
|
|
|
if (Size <= 64)
|
2009-01-31 08:06:58 +08:00
|
|
|
Current = Integer;
|
2009-01-29 15:22:20 +08:00
|
|
|
else if (Size <= 128)
|
|
|
|
Lo = Hi = Integer;
|
|
|
|
} else if (ET == Context.FloatTy)
|
2009-01-31 08:06:58 +08:00
|
|
|
Current = SSE;
|
2009-01-24 16:32:22 +08:00
|
|
|
else if (ET == Context.DoubleTy)
|
|
|
|
Lo = Hi = SSE;
|
|
|
|
else if (ET == Context.LongDoubleTy)
|
2009-01-31 08:06:58 +08:00
|
|
|
Current = ComplexX87;
|
2009-01-29 17:42:07 +08:00
|
|
|
|
|
|
|
// If this complex type crosses an eightbyte boundary then it
|
|
|
|
// should be split.
|
2009-01-31 06:40:15 +08:00
|
|
|
uint64_t EB_Real = (OffsetBase) / 64;
|
|
|
|
uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64;
|
2009-01-29 17:42:07 +08:00
|
|
|
if (Hi == NoClass && EB_Real != EB_Imag)
|
|
|
|
Hi = Lo;
|
2009-01-30 16:09:32 +08:00
|
|
|
} else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
|
|
|
|
// Arrays are treated like structures.
|
|
|
|
|
|
|
|
uint64_t Size = Context.getTypeSize(Ty);
|
|
|
|
|
|
|
|
// AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
|
|
|
|
// than two eightbytes, ..., it has class MEMORY.
|
|
|
|
if (Size > 128)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
|
|
|
|
// fields, it has class MEMORY.
|
|
|
|
//
|
|
|
|
// Only need to check alignment of array base.
|
2009-01-31 08:06:58 +08:00
|
|
|
if (OffsetBase % Context.getTypeAlign(AT->getElementType()))
|
2009-01-30 16:09:32 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Otherwise implement simplified merge. We could be smarter about
|
|
|
|
// this, but it isn't worth it and would be harder to verify.
|
2009-01-31 08:06:58 +08:00
|
|
|
Current = NoClass;
|
2009-01-30 16:09:32 +08:00
|
|
|
uint64_t EltSize = Context.getTypeSize(AT->getElementType());
|
|
|
|
uint64_t ArraySize = AT->getSize().getZExtValue();
|
|
|
|
for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
|
|
|
|
Class FieldLo, FieldHi;
|
|
|
|
classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi);
|
2009-01-31 08:06:58 +08:00
|
|
|
Lo = merge(Lo, FieldLo);
|
|
|
|
Hi = merge(Hi, FieldHi);
|
|
|
|
if (Lo == Memory || Hi == Memory)
|
|
|
|
break;
|
2009-01-30 16:09:32 +08:00
|
|
|
}
|
2009-01-31 08:06:58 +08:00
|
|
|
|
|
|
|
// Do post merger cleanup (see below). Only case we worry about is Memory.
|
|
|
|
if (Hi == Memory)
|
|
|
|
Lo = Memory;
|
|
|
|
assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
|
2009-01-29 16:13:58 +08:00
|
|
|
} else if (const RecordType *RT = Ty->getAsRecordType()) {
|
2009-01-30 08:47:38 +08:00
|
|
|
uint64_t Size = Context.getTypeSize(Ty);
|
2009-01-29 16:13:58 +08:00
|
|
|
|
|
|
|
// AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
|
|
|
|
// than two eightbytes, ..., it has class MEMORY.
|
|
|
|
if (Size > 128)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const RecordDecl *RD = RT->getDecl();
|
|
|
|
|
|
|
|
// Assume variable sized types are passed in memory.
|
|
|
|
if (RD->hasFlexibleArrayMember())
|
|
|
|
return;
|
|
|
|
|
|
|
|
const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
|
|
|
|
|
|
|
|
// Reset Lo class, this will be recomputed.
|
2009-01-31 08:06:58 +08:00
|
|
|
Current = NoClass;
|
2009-01-29 16:13:58 +08:00
|
|
|
unsigned idx = 0;
|
|
|
|
for (RecordDecl::field_iterator i = RD->field_begin(),
|
|
|
|
e = RD->field_end(); i != e; ++i, ++idx) {
|
2009-01-30 16:09:32 +08:00
|
|
|
uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
|
2009-01-29 16:13:58 +08:00
|
|
|
|
2009-01-30 16:09:32 +08:00
|
|
|
// AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
|
|
|
|
// fields, it has class MEMORY.
|
2009-01-29 16:13:58 +08:00
|
|
|
if (Offset % Context.getTypeAlign(i->getType())) {
|
|
|
|
Lo = Memory;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Classify this field.
|
2009-01-31 08:06:58 +08:00
|
|
|
//
|
|
|
|
// AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
|
|
|
|
// exceeds a single eightbyte, each is classified
|
|
|
|
// separately. Each eightbyte gets initialized to class
|
|
|
|
// NO_CLASS.
|
2009-01-29 16:13:58 +08:00
|
|
|
Class FieldLo, FieldHi;
|
2009-01-29 17:42:07 +08:00
|
|
|
classify(i->getType(), Context, Offset, FieldLo, FieldHi);
|
2009-01-31 08:06:58 +08:00
|
|
|
Lo = merge(Lo, FieldLo);
|
|
|
|
Hi = merge(Hi, FieldHi);
|
|
|
|
if (Lo == Memory || Hi == Memory)
|
|
|
|
break;
|
2009-01-29 16:13:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
|
|
|
|
//
|
|
|
|
// (a) If one of the classes is MEMORY, the whole argument is
|
|
|
|
// passed in memory.
|
|
|
|
//
|
|
|
|
// (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
|
|
|
|
|
|
|
|
// The first of these conditions is guaranteed by how we implement
|
2009-01-31 08:06:58 +08:00
|
|
|
// the merge (just bail).
|
|
|
|
//
|
|
|
|
// The second condition occurs in the case of unions; for example
|
|
|
|
// union { _Complex double; unsigned; }.
|
|
|
|
if (Hi == Memory)
|
|
|
|
Lo = Memory;
|
2009-01-29 16:13:58 +08:00
|
|
|
if (Hi == SSEUp && Lo != SSE)
|
2009-01-31 08:06:58 +08:00
|
|
|
Hi = SSE;
|
2009-01-24 16:32:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-31 08:06:58 +08:00
|
|
|
|
2009-01-16 02:18:40 +08:00
|
|
|
ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
|
|
|
|
ASTContext &Context) const {
|
2009-01-24 16:32:22 +08:00
|
|
|
// AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
|
|
|
|
// classification algorithm.
|
|
|
|
X86_64ABIInfo::Class Lo, Hi;
|
2009-01-29 17:42:07 +08:00
|
|
|
classify(RetTy, Context, 0, Lo, Hi);
|
2009-01-24 16:32:22 +08:00
|
|
|
|
2009-01-31 08:06:58 +08:00
|
|
|
// Check some invariants.
|
|
|
|
assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
|
|
|
|
assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
|
|
|
|
assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
|
|
|
|
|
2009-01-24 16:32:22 +08:00
|
|
|
const llvm::Type *ResType = 0;
|
|
|
|
switch (Lo) {
|
|
|
|
case NoClass:
|
2009-01-27 05:26:08 +08:00
|
|
|
return ABIArgInfo::getIgnore();
|
2009-01-24 16:32:22 +08:00
|
|
|
|
|
|
|
case SSEUp:
|
|
|
|
case X87Up:
|
|
|
|
assert(0 && "Invalid classification for lo word.");
|
|
|
|
|
2009-01-31 08:06:58 +08:00
|
|
|
// AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
|
|
|
|
// hidden argument, i.e. structret.
|
2009-01-24 16:32:22 +08:00
|
|
|
case Memory:
|
|
|
|
return ABIArgInfo::getStructRet();
|
|
|
|
|
|
|
|
// AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
|
|
|
|
// available register of the sequence %rax, %rdx is used.
|
|
|
|
case Integer:
|
|
|
|
ResType = llvm::Type::Int64Ty; break;
|
|
|
|
|
|
|
|
// AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
|
|
|
|
// available SSE register of the sequence %xmm0, %xmm1 is used.
|
|
|
|
case SSE:
|
|
|
|
ResType = llvm::Type::DoubleTy; break;
|
|
|
|
|
|
|
|
// AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
|
|
|
|
// returned on the X87 stack in %st0 as 80-bit x87 number.
|
|
|
|
case X87:
|
|
|
|
ResType = llvm::Type::X86_FP80Ty; break;
|
|
|
|
|
2009-01-31 08:06:58 +08:00
|
|
|
// AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
|
|
|
|
// part of the value is returned in %st0 and the imaginary part in
|
|
|
|
// %st1.
|
2009-01-24 16:32:22 +08:00
|
|
|
case ComplexX87:
|
|
|
|
assert(Hi == NoClass && "Unexpected ComplexX87 classification.");
|
|
|
|
ResType = llvm::VectorType::get(llvm::Type::X86_FP80Ty, 2);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (Hi) {
|
|
|
|
// Memory was handled previously, and ComplexX87 and X87 should
|
|
|
|
// never occur as hi classes.
|
|
|
|
case Memory:
|
|
|
|
case X87:
|
|
|
|
case ComplexX87:
|
|
|
|
assert(0 && "Invalid classification for hi word.");
|
|
|
|
|
|
|
|
case NoClass: break;
|
|
|
|
case Integer:
|
2009-01-29 15:36:07 +08:00
|
|
|
ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
|
|
|
|
break;
|
2009-01-24 16:32:22 +08:00
|
|
|
case SSE:
|
2009-01-29 15:36:07 +08:00
|
|
|
ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
|
|
|
|
break;
|
2009-01-24 16:32:22 +08:00
|
|
|
|
|
|
|
// AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
|
|
|
|
// is passed in the upper half of the last used SSE register.
|
|
|
|
//
|
|
|
|
// SSEUP should always be preceeded by SSE, just widen.
|
|
|
|
case SSEUp:
|
|
|
|
assert(Lo == SSE && "Unexpected SSEUp classification.");
|
|
|
|
ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
|
|
|
|
break;
|
|
|
|
|
|
|
|
// AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
|
2009-01-29 15:36:07 +08:00
|
|
|
// returned together with the previous X87 value in %st0.
|
2009-01-24 16:32:22 +08:00
|
|
|
//
|
|
|
|
// X87UP should always be preceeded by X87, so we don't need to do
|
|
|
|
// anything here.
|
|
|
|
case X87Up:
|
|
|
|
assert(Lo == X87 && "Unexpected X87Up classification.");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ABIArgInfo::getCoerce(ResType);
|
2009-01-16 02:18:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty,
|
|
|
|
ASTContext &Context) const {
|
|
|
|
return ABIArgInfo::getDefault();
|
|
|
|
}
|
|
|
|
|
2008-10-14 01:02:26 +08:00
|
|
|
ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
|
|
|
|
ASTContext &Context) const {
|
|
|
|
return ABIArgInfo::getDefault();
|
|
|
|
}
|
|
|
|
|
|
|
|
ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
|
|
|
|
ASTContext &Context) const {
|
|
|
|
return ABIArgInfo::getDefault();
|
|
|
|
}
|
|
|
|
|
|
|
|
const ABIInfo &CodeGenTypes::getABIInfo() const {
|
|
|
|
if (TheABIInfo)
|
|
|
|
return *TheABIInfo;
|
|
|
|
|
|
|
|
// For now we just cache this in the CodeGenTypes and don't bother
|
|
|
|
// to free it.
|
|
|
|
const char *TargetPrefix = getContext().Target.getTargetPrefix();
|
|
|
|
if (strcmp(TargetPrefix, "x86") == 0) {
|
2009-01-16 02:18:40 +08:00
|
|
|
switch (getContext().Target.getPointerWidth(0)) {
|
|
|
|
case 32:
|
2008-10-14 01:02:26 +08:00
|
|
|
return *(TheABIInfo = new X86_32ABIInfo());
|
2009-01-16 02:18:40 +08:00
|
|
|
case 64:
|
2009-01-31 02:47:53 +08:00
|
|
|
return *(TheABIInfo = new X86_64ABIInfo());
|
2009-01-16 02:18:40 +08:00
|
|
|
}
|
2008-10-14 01:02:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return *(TheABIInfo = new DefaultABIInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
// getABIReturnInfo - Wrap the ABIInfo getABIReturnInfo, altering
|
|
|
|
// "default" types to StructRet when appropriate for simplicity.
|
|
|
|
static ABIArgInfo getABIReturnInfo(QualType Ty, CodeGenTypes &CGT) {
|
|
|
|
assert(!Ty->isArrayType() &&
|
|
|
|
"Array types cannot be passed directly.");
|
|
|
|
ABIArgInfo Info = CGT.getABIInfo().classifyReturnType(Ty, CGT.getContext());
|
2008-09-18 04:11:04 +08:00
|
|
|
// Ensure default on aggregate types is StructRet.
|
|
|
|
if (Info.isDefault() && CodeGenFunction::hasAggregateLLVMType(Ty))
|
|
|
|
return ABIArgInfo::getStructRet();
|
|
|
|
return Info;
|
|
|
|
}
|
|
|
|
|
2008-10-14 01:02:26 +08:00
|
|
|
// getABIArgumentInfo - Wrap the ABIInfo getABIReturnInfo, altering
|
|
|
|
// "default" types to ByVal when appropriate for simplicity.
|
|
|
|
static ABIArgInfo getABIArgumentInfo(QualType Ty, CodeGenTypes &CGT) {
|
|
|
|
assert(!Ty->isArrayType() &&
|
|
|
|
"Array types cannot be passed directly.");
|
|
|
|
ABIArgInfo Info = CGT.getABIInfo().classifyArgumentType(Ty, CGT.getContext());
|
2008-09-18 04:11:04 +08:00
|
|
|
// 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.");
|
|
|
|
|
2009-01-10 01:18:27 +08:00
|
|
|
for (RecordDecl::field_iterator i = RD->field_begin(),
|
2008-09-17 08:51:38 +08:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-03 03:06:38 +08:00
|
|
|
/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
|
|
|
|
/// a pointer to an object of type \arg Ty.
|
|
|
|
///
|
|
|
|
/// This safely handles the case when the src type is smaller than the
|
|
|
|
/// destination type; in this situation the values of bits which not
|
|
|
|
/// present in the src are undefined.
|
|
|
|
static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
|
|
|
|
const llvm::Type *Ty,
|
|
|
|
CodeGenFunction &CGF) {
|
|
|
|
const llvm::Type *SrcTy =
|
|
|
|
cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
|
|
|
|
uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
|
|
|
|
uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(Ty);
|
|
|
|
|
|
|
|
// If load is legal, just bitcase the src pointer.
|
|
|
|
if (SrcSize == DstSize) {
|
|
|
|
llvm::Value *Casted =
|
|
|
|
CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
|
|
|
|
return CGF.Builder.CreateLoad(Casted);
|
|
|
|
} else {
|
|
|
|
assert(SrcSize < DstSize && "Coercion is losing source bits!");
|
|
|
|
|
|
|
|
// Otherwise do coercion through memory. This is stupid, but
|
|
|
|
// simple.
|
|
|
|
llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
|
|
|
|
llvm::Value *Casted =
|
|
|
|
CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
|
|
|
|
CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
|
|
|
|
return CGF.Builder.CreateLoad(Tmp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
|
|
|
|
/// where the source and destination may have different types.
|
|
|
|
///
|
|
|
|
/// This safely handles the case when the src type is larger than the
|
|
|
|
/// destination type; the upper bits of the src will be lost.
|
|
|
|
static void CreateCoercedStore(llvm::Value *Src,
|
|
|
|
llvm::Value *DstPtr,
|
|
|
|
CodeGenFunction &CGF) {
|
|
|
|
const llvm::Type *SrcTy = Src->getType();
|
|
|
|
const llvm::Type *DstTy =
|
|
|
|
cast<llvm::PointerType>(DstPtr->getType())->getElementType();
|
|
|
|
|
|
|
|
uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
|
|
|
|
uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(DstTy);
|
|
|
|
|
|
|
|
// If store is legal, just bitcase the src pointer.
|
|
|
|
if (SrcSize == DstSize) {
|
|
|
|
llvm::Value *Casted =
|
|
|
|
CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
|
|
|
|
CGF.Builder.CreateStore(Src, Casted);
|
|
|
|
} else {
|
|
|
|
assert(SrcSize > DstSize && "Coercion is missing bits!");
|
|
|
|
|
|
|
|
// Otherwise do coercion through memory. This is stupid, but
|
|
|
|
// simple.
|
|
|
|
llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
|
|
|
|
CGF.Builder.CreateStore(Src, Tmp);
|
|
|
|
llvm::Value *Casted =
|
|
|
|
CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
|
|
|
|
CGF.Builder.CreateStore(CGF.Builder.CreateLoad(Casted), DstPtr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-17 08:51:38 +08:00
|
|
|
/***/
|
|
|
|
|
2009-02-03 06:03:45 +08:00
|
|
|
bool CodeGenModule::ReturnTypeUsesSret(const CGFunctionInfo &FI) {
|
|
|
|
return getABIReturnInfo(FI.getReturnType(), getTypes()).isStructRet();
|
2009-02-03 05:43:58 +08:00
|
|
|
}
|
|
|
|
|
2008-09-10 12:01:49 +08:00
|
|
|
const llvm::FunctionType *
|
2009-02-03 05:43:58 +08:00
|
|
|
CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic) {
|
2008-09-10 12:01:49 +08:00
|
|
|
std::vector<const llvm::Type*> ArgTys;
|
|
|
|
|
|
|
|
const llvm::Type *ResultType = 0;
|
|
|
|
|
2009-02-03 07:43:58 +08:00
|
|
|
QualType RetTy = FI.getReturnType();
|
2008-10-14 01:02:26 +08:00
|
|
|
ABIArgInfo RetAI = getABIReturnInfo(RetTy, *this);
|
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;
|
|
|
|
}
|
|
|
|
|
2009-01-27 05:26:08 +08:00
|
|
|
case ABIArgInfo::Ignore:
|
|
|
|
ResultType = llvm::Type::VoidTy;
|
|
|
|
break;
|
|
|
|
|
2008-09-10 12:01:49 +08:00
|
|
|
case ABIArgInfo::Coerce:
|
2008-09-10 15:04:09 +08:00
|
|
|
ResultType = RetAI.getCoerceToType();
|
2008-09-10 12:01:49 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-02-03 07:43:58 +08:00
|
|
|
for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
|
|
|
|
it != ie; ++it) {
|
|
|
|
ABIArgInfo AI = getABIArgumentInfo(*it, *this);
|
|
|
|
const llvm::Type *Ty = ConvertType(*it);
|
2008-09-11 09:48:57 +08:00
|
|
|
|
|
|
|
switch (AI.getKind()) {
|
2009-01-27 05:26:08 +08:00
|
|
|
case ABIArgInfo::Ignore:
|
|
|
|
break;
|
|
|
|
|
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:
|
2009-02-03 07:43:58 +08:00
|
|
|
GetExpandedTypes(*it, ArgTys);
|
2008-09-11 09:48:57 +08:00
|
|
|
break;
|
|
|
|
}
|
2008-09-10 12:01:49 +08:00
|
|
|
}
|
|
|
|
|
2009-02-03 05:43:58 +08:00
|
|
|
return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
|
2008-09-10 07:48:28 +08:00
|
|
|
}
|
|
|
|
|
2009-02-03 07:43:58 +08:00
|
|
|
void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
|
2009-02-03 06:03:45 +08:00
|
|
|
const Decl *TargetDecl,
|
2008-09-26 05:02:23 +08:00
|
|
|
AttributeListType &PAL) {
|
2008-09-10 08:32:18 +08:00
|
|
|
unsigned FuncAttrs = 0;
|
2008-09-27 06:53:57 +08:00
|
|
|
unsigned RetAttrs = 0;
|
2008-09-10 08:32:18 +08:00
|
|
|
|
|
|
|
if (TargetDecl) {
|
|
|
|
if (TargetDecl->getAttr<NoThrowAttr>())
|
2008-09-26 05:02:23 +08:00
|
|
|
FuncAttrs |= llvm::Attribute::NoUnwind;
|
2008-09-10 08:32:18 +08:00
|
|
|
if (TargetDecl->getAttr<NoReturnAttr>())
|
2008-09-26 05:02:23 +08:00
|
|
|
FuncAttrs |= llvm::Attribute::NoReturn;
|
2008-10-06 07:32:53 +08:00
|
|
|
if (TargetDecl->getAttr<PureAttr>())
|
|
|
|
FuncAttrs |= llvm::Attribute::ReadOnly;
|
|
|
|
if (TargetDecl->getAttr<ConstAttr>())
|
|
|
|
FuncAttrs |= llvm::Attribute::ReadNone;
|
2008-09-10 08:32:18 +08:00
|
|
|
}
|
|
|
|
|
2009-02-03 07:43:58 +08:00
|
|
|
QualType RetTy = FI.getReturnType();
|
2008-09-10 08:32:18 +08:00
|
|
|
unsigned Index = 1;
|
2008-10-14 01:02:26 +08:00
|
|
|
ABIArgInfo RetAI = getABIReturnInfo(RetTy, getTypes());
|
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()) {
|
2008-09-27 06:53:57 +08:00
|
|
|
RetAttrs |= llvm::Attribute::SExt;
|
2008-09-10 10:41:04 +08:00
|
|
|
} else if (RetTy->isUnsignedIntegerType()) {
|
2008-09-27 06:53:57 +08:00
|
|
|
RetAttrs |= llvm::Attribute::ZExt;
|
2008-09-10 10:41:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ABIArgInfo::StructRet:
|
2008-09-26 05:02:23 +08:00
|
|
|
PAL.push_back(llvm::AttributeWithIndex::get(Index,
|
2009-01-31 10:19:00 +08:00
|
|
|
llvm::Attribute::StructRet |
|
|
|
|
llvm::Attribute::NoAlias));
|
2008-09-10 08:32:18 +08:00
|
|
|
++Index;
|
2008-09-10 10:41:04 +08:00
|
|
|
break;
|
|
|
|
|
2009-01-27 05:26:08 +08:00
|
|
|
case ABIArgInfo::Ignore:
|
2008-09-10 10:41:04 +08:00
|
|
|
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-27 06:53:57 +08:00
|
|
|
if (RetAttrs)
|
|
|
|
PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
|
2009-02-03 07:43:58 +08:00
|
|
|
for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
|
|
|
|
it != ie; ++it) {
|
|
|
|
QualType ParamType = *it;
|
2008-09-26 05:02:23 +08:00
|
|
|
unsigned Attributes = 0;
|
2008-10-14 01:02:26 +08:00
|
|
|
ABIArgInfo AI = getABIArgumentInfo(ParamType, getTypes());
|
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-26 05:02:23 +08:00
|
|
|
Attributes |= llvm::Attribute::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()) {
|
2008-09-26 05:02:23 +08:00
|
|
|
Attributes |= llvm::Attribute::SExt;
|
2008-09-11 09:48:57 +08:00
|
|
|
} else if (ParamType->isUnsignedIntegerType()) {
|
2008-09-26 05:02:23 +08:00
|
|
|
Attributes |= llvm::Attribute::ZExt;
|
2008-09-11 09:48:57 +08:00
|
|
|
}
|
2008-09-10 08:32:18 +08:00
|
|
|
}
|
2008-09-11 09:48:57 +08:00
|
|
|
break;
|
|
|
|
|
2009-01-27 05:26:08 +08:00
|
|
|
case ABIArgInfo::Ignore:
|
|
|
|
// Skip increment, no matching LLVM parameter.
|
|
|
|
continue;
|
|
|
|
|
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-26 05:02:23 +08:00
|
|
|
if (Attributes)
|
|
|
|
PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
|
2008-09-17 08:51:38 +08:00
|
|
|
++Index;
|
2008-09-10 08:32:18 +08:00
|
|
|
}
|
2008-09-27 06:53:57 +08:00
|
|
|
if (FuncAttrs)
|
|
|
|
PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
|
|
|
|
|
2008-09-10 08:32:18 +08:00
|
|
|
}
|
|
|
|
|
2009-02-03 06:03:45 +08:00
|
|
|
void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
|
|
|
|
llvm::Function *Fn,
|
2008-09-10 07:27:19 +08:00
|
|
|
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.
|
2009-02-03 06:03:45 +08:00
|
|
|
if (CGM.ReturnTypeUsesSret(FI)) {
|
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-10-14 01:02:26 +08:00
|
|
|
ABIArgInfo ArgI = getABIArgumentInfo(Ty, CGM.getTypes());
|
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.
|
2008-11-24 12:00:27 +08:00
|
|
|
std::string Name = Arg->getNameAsString();
|
2008-09-17 08:51:38 +08:00
|
|
|
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;
|
|
|
|
}
|
2009-01-27 05:26:08 +08:00
|
|
|
|
|
|
|
case ABIArgInfo::Ignore:
|
|
|
|
break;
|
|
|
|
|
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!");
|
|
|
|
}
|
|
|
|
|
2009-02-03 06:03:45 +08:00
|
|
|
void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI,
|
2008-09-10 07:27:19 +08:00
|
|
|
llvm::Value *ReturnValue) {
|
2008-09-10 10:41:04 +08:00
|
|
|
llvm::Value *RV = 0;
|
|
|
|
|
|
|
|
// Functions with no result always return void.
|
|
|
|
if (ReturnValue) {
|
2009-02-03 06:03:45 +08:00
|
|
|
QualType RetTy = FI.getReturnType();
|
2008-10-14 01:02:26 +08:00
|
|
|
ABIArgInfo RetAI = getABIReturnInfo(RetTy, CGM.getTypes());
|
2008-09-10 10:41:04 +08:00
|
|
|
|
|
|
|
switch (RetAI.getKind()) {
|
|
|
|
case ABIArgInfo::StructRet:
|
2008-12-18 12:52:14 +08:00
|
|
|
if (RetTy->isAnyComplexType()) {
|
|
|
|
// FIXME: Volatile
|
|
|
|
ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
|
|
|
|
StoreComplexToAddr(RT, CurFn->arg_begin(), false);
|
|
|
|
} else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
|
|
|
|
EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy);
|
|
|
|
} else {
|
|
|
|
Builder.CreateStore(Builder.CreateLoad(ReturnValue),
|
|
|
|
CurFn->arg_begin());
|
|
|
|
}
|
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;
|
|
|
|
|
2009-01-27 05:26:08 +08:00
|
|
|
case ABIArgInfo::Ignore:
|
|
|
|
break;
|
|
|
|
|
2008-09-10 15:04:09 +08:00
|
|
|
case ABIArgInfo::Coerce: {
|
2009-01-27 09:36:03 +08:00
|
|
|
RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
|
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
|
|
|
}
|
|
|
|
|
2009-02-03 06:03:45 +08:00
|
|
|
RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
|
|
|
|
llvm::Value *Callee,
|
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.
|
2009-02-03 05:43:58 +08:00
|
|
|
QualType RetTy = CallInfo.getReturnType();
|
2008-10-14 01:02:26 +08:00
|
|
|
ABIArgInfo RetAI = getABIReturnInfo(RetTy, CGM.getTypes());
|
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:
|
2009-01-27 05:26:08 +08:00
|
|
|
case ABIArgInfo::Ignore:
|
2008-09-10 10:41:04 +08:00
|
|
|
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-10-14 01:02:26 +08:00
|
|
|
ABIArgInfo ArgInfo = getABIArgumentInfo(I->second, CGM.getTypes());
|
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;
|
|
|
|
|
2009-01-27 05:26:08 +08:00
|
|
|
case ABIArgInfo::Ignore:
|
|
|
|
break;
|
|
|
|
|
2008-09-17 08:51:38 +08:00
|
|
|
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 08:32:18 +08:00
|
|
|
// FIXME: Provide TargetDecl so nounwind, noreturn, etc, etc get set.
|
2008-09-26 05:02:23 +08:00
|
|
|
CodeGen::AttributeListType AttributeList;
|
2009-02-03 06:03:45 +08:00
|
|
|
CGM.ConstructAttributeList(CallInfo, 0, AttributeList);
|
2008-09-26 05:02:23 +08:00
|
|
|
CI->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(),
|
2009-01-31 10:19:00 +08:00
|
|
|
AttributeList.size()));
|
|
|
|
|
2008-09-10 07:27:19 +08:00
|
|
|
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-12-18 12:52:14 +08:00
|
|
|
else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
|
2008-09-17 08:51:38 +08:00
|
|
|
return RValue::getAggregate(Args[0]);
|
2008-12-18 12:52:14 +08:00
|
|
|
else
|
|
|
|
return RValue::get(Builder.CreateLoad(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);
|
|
|
|
|
2009-01-27 05:26:08 +08:00
|
|
|
case ABIArgInfo::Ignore:
|
2009-01-29 16:24:57 +08:00
|
|
|
if (RetTy->isVoidType())
|
|
|
|
return RValue::get(0);
|
|
|
|
if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
|
|
|
|
llvm::Value *Res =
|
|
|
|
llvm::UndefValue::get(llvm::PointerType::getUnqual(ConvertType(RetTy)));
|
|
|
|
return RValue::getAggregate(Res);
|
|
|
|
}
|
|
|
|
return RValue::get(llvm::UndefValue::get(ConvertType(RetTy)));
|
2009-01-27 05:26:08 +08:00
|
|
|
|
2008-09-10 15:04:09 +08:00
|
|
|
case ABIArgInfo::Coerce: {
|
2009-01-27 09:36:03 +08:00
|
|
|
llvm::Value *V = CreateTempAlloca(ConvertType(RetTy), "coerce");
|
|
|
|
CreateCoercedStore(CI, V, *this);
|
2008-11-26 06:21:48 +08:00
|
|
|
if (RetTy->isAnyComplexType())
|
|
|
|
return RValue::getComplex(LoadComplexFromAddr(V, false));
|
2009-01-27 05:26:08 +08:00
|
|
|
else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
|
2008-11-26 06:21:48 +08:00
|
|
|
return RValue::getAggregate(V);
|
2009-01-27 05:26:08 +08:00
|
|
|
else
|
|
|
|
return RValue::get(Builder.CreateLoad(V));
|
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
|
|
|
|
|
|
|
assert(0 && "Unhandled ABIArgInfo::Kind");
|
|
|
|
return RValue::get(0);
|
2008-09-10 07:27:19 +08:00
|
|
|
}
|