CGCall.cpp revision 39f34e97d6a468f0a7dfa5664c61217cffc65b74
10dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar//===----- CGCall.h - Encapsulate calling convention details ----*- C++ -*-===//
20dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar//
30dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar//                     The LLVM Compiler Infrastructure
40dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar//
50dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar// This file is distributed under the University of Illinois Open Source
60dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar// License. See LICENSE.TXT for details.
70dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar//
80dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar//===----------------------------------------------------------------------===//
90dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar//
100dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar// These classes wrap the information about a call or function
110dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar// definition used to handle ABI compliancy.
120dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar//
130dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar//===----------------------------------------------------------------------===//
140dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar
150dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar#include "CGCall.h"
160dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar#include "CodeGenFunction.h"
17b768807c49a1c7085def099b848631856af766faDaniel Dunbar#include "CodeGenModule.h"
186b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar#include "clang/Basic/TargetInfo.h"
190dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar#include "clang/AST/ASTContext.h"
200dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar#include "clang/AST/Decl.h"
210dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar#include "clang/AST/DeclObjC.h"
225627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar#include "llvm/ADT/StringExtras.h"
23d0646bd7c11c12b34971b55e5c1bdd8439401b4cDevang Patel#include "llvm/Attributes.h"
240dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbarusing namespace clang;
250dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbarusing namespace CodeGen;
260dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar
270dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar/***/
280dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar
290dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar// FIXME: Use iterator and sidestep silly type array creation.
300dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar
3145c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel DunbarCGFunctionInfo::CGFunctionInfo(const FunctionTypeNoProto *FTNP)
3245c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar  : IsVariadic(true)
3345c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar{
3445c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar  ArgTypes.push_back(FTNP->getResultType());
3545c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar}
3645c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar
3745c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel DunbarCGFunctionInfo::CGFunctionInfo(const FunctionTypeProto *FTP)
3845c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar  : IsVariadic(FTP->isVariadic())
3945c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar{
4045c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar  ArgTypes.push_back(FTP->getResultType());
4145c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar  for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
4245c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar    ArgTypes.push_back(FTP->getArgType(i));
4345c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar}
4445c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar
4545c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar// FIXME: Is there really any reason to have this still?
460dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel DunbarCGFunctionInfo::CGFunctionInfo(const FunctionDecl *FD)
470dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar{
480dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar  const FunctionType *FTy = FD->getType()->getAsFunctionType();
490dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar  const FunctionTypeProto *FTP = dyn_cast<FunctionTypeProto>(FTy);
5045c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar
510dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar  ArgTypes.push_back(FTy->getResultType());
5245c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar  if (FTP) {
5345c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar    IsVariadic = FTP->isVariadic();
540dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar    for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
550dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar      ArgTypes.push_back(FTP->getArgType(i));
5645c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar  } else {
5745c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar    IsVariadic = true;
5845c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar  }
590dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar}
600dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar
610dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel DunbarCGFunctionInfo::CGFunctionInfo(const ObjCMethodDecl *MD,
620dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar                               const ASTContext &Context)
6345c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar  : IsVariadic(MD->isVariadic())
640dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar{
650dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar  ArgTypes.push_back(MD->getResultType());
660dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar  ArgTypes.push_back(MD->getSelfDecl()->getType());
670dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar  ArgTypes.push_back(Context.getObjCSelType());
680dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar  for (ObjCMethodDecl::param_const_iterator i = MD->param_begin(),
690dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar         e = MD->param_end(); i != e; ++i)
700dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar    ArgTypes.push_back((*i)->getType());
710dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar}
720dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar
735323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel DunbarArgTypeIterator CGFunctionInfo::argtypes_begin() const {
745323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar  return ArgTypes.begin();
755323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar}
765323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar
775323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel DunbarArgTypeIterator CGFunctionInfo::argtypes_end() const {
785323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar  return ArgTypes.end();
790dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar}
800dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar
810dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar/***/
820dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar
835323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel DunbarCGCallInfo::CGCallInfo(QualType _ResultType, const CallArgList &_Args) {
845323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar  ArgTypes.push_back(_ResultType);
855323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar  for (CallArgList::const_iterator i = _Args.begin(), e = _Args.end(); i!=e; ++i)
860dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar    ArgTypes.push_back(i->second);
870dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar}
880dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar
895323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel DunbarArgTypeIterator CGCallInfo::argtypes_begin() const {
905323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar  return ArgTypes.begin();
915323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar}
925323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar
935323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel DunbarArgTypeIterator CGCallInfo::argtypes_end() const {
945323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar  return ArgTypes.end();
950dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar}
9617b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar
9717b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar/***/
9817b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar
998951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar/// ABIArgInfo - Helper class to encapsulate information about how a
1008951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar/// specific C type should be passed to or returned from a function.
1012c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbarclass ABIArgInfo {
1022c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbarpublic:
1032c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  enum Kind {
1042c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar    Default,
1058951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    StructRet, /// Only valid for aggregate return types.
1068951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
1075627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    Coerce,    /// Only valid for aggregate return types, the argument
1085627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar               /// should be accessed by coercion to a provided type.
1098951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
1108951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    ByVal,     /// Only valid for aggregate argument types. The
1118951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar               /// structure should be passed "byval" with the
1128951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar               /// specified alignment (0 indicates default
1138951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar               /// alignment).
1148951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
1158951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    Expand,    /// Only valid for aggregate argument types. The
1168951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar               /// structure should be expanded into consecutive
1175627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar               /// arguments for its constituent fields. Currently
1185627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar               /// expand is only allowed on structures whose fields
1195627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar               /// are all scalar types or are themselves expandable
1205627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar               /// types.
1218951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
1228951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    KindFirst=Default, KindLast=Expand
1232c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  };
1242c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar
1252c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbarprivate:
1262c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  Kind TheKind;
127639ffe47097d01249b98b7acd102aaad6697b43aDaniel Dunbar  const llvm::Type *TypeData;
1288951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar  unsigned UIntData;
1292c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar
1308951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar  ABIArgInfo(Kind K, const llvm::Type *TD=0,
1318951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar             unsigned UI=0) : TheKind(K),
1328951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar                              TypeData(TD),
1338951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar                              UIntData(0) {}
1342c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbarpublic:
1352c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  static ABIArgInfo getDefault() {
1368951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    return ABIArgInfo(Default);
1372c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  }
1382c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  static ABIArgInfo getStructRet() {
1398951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    return ABIArgInfo(StructRet);
1402c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  }
141639ffe47097d01249b98b7acd102aaad6697b43aDaniel Dunbar  static ABIArgInfo getCoerce(const llvm::Type *T) {
142639ffe47097d01249b98b7acd102aaad6697b43aDaniel Dunbar    assert(T->isSingleValueType() && "Can only coerce to simple types");
1432c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar    return ABIArgInfo(Coerce, T);
1442c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  }
1458951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar  static ABIArgInfo getByVal(unsigned Alignment) {
1468951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    return ABIArgInfo(ByVal, 0, Alignment);
1478951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar  }
1485627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  static ABIArgInfo getExpand() {
1495627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    return ABIArgInfo(Expand);
1505627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  }
1512c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar
1522c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  Kind getKind() const { return TheKind; }
1532c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  bool isDefault() const { return TheKind == Default; }
1542c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  bool isStructRet() const { return TheKind == StructRet; }
1552c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  bool isCoerce() const { return TheKind == Coerce; }
1568951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar  bool isByVal() const { return TheKind == ByVal; }
1575627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  bool isExpand() const { return TheKind == Expand; }
15845c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar
15945c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar  // Coerce accessors
160639ffe47097d01249b98b7acd102aaad6697b43aDaniel Dunbar  const llvm::Type *getCoerceToType() const {
16145c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar    assert(TheKind == Coerce && "Invalid kind!");
16245c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar    return TypeData;
16345c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar  }
1648951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
1658951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar  // ByVal accessors
1668951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar  unsigned getByValAlignment() const {
1678951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    assert(TheKind == ByVal && "Invalid kind!");
1688951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    return UIntData;
1698951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar  }
1702c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar};
1712c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar
1722c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar/***/
1732c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar
1746b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar/* FIXME: All of this stuff should be part of the target interface
1756b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar   somehow. It is currently here because it is not clear how to factor
1766b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar   the targets to support this, since the Targets currently live in a
1776b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar   layer below types n'stuff.
1786b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar */
1796b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar
1806b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar/// ABIInfo - Target specific hooks for defining how a type should be
1816b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar/// passed or returned from functions.
1826b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbarclass clang::ABIInfo {
1836b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbarpublic:
1846b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar  virtual ~ABIInfo();
1856b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar
1866b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar  virtual ABIArgInfo classifyReturnType(QualType RetTy,
1876b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar                                        ASTContext &Context) const = 0;
1886b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar
1896b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar  virtual ABIArgInfo classifyArgumentType(QualType Ty,
1906b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar                                          ASTContext &Context) const = 0;
1916b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar};
1926b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar
1936b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel DunbarABIInfo::~ABIInfo() {}
1946b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar
195834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar/// isEmptyStruct - Return true iff a structure has no non-empty
196834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar/// members. Note that a structure with a flexible array member is not
197834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar/// considered empty.
198834af456588587194337bb32671fb9329e73a7f7Daniel Dunbarstatic bool isEmptyStruct(QualType T) {
199834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar  const RecordType *RT = T->getAsStructureType();
200834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar  if (!RT)
201834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar    return 0;
202834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar  const RecordDecl *RD = RT->getDecl();
203834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar  if (RD->hasFlexibleArrayMember())
204834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar    return false;
205834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar  for (RecordDecl::field_const_iterator i = RD->field_begin(),
206834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar         e = RD->field_end(); i != e; ++i) {
207834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar    const FieldDecl *FD = *i;
208834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar    if (!isEmptyStruct(FD->getType()))
209834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar      return false;
210834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar  }
211834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar  return true;
212834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar}
213834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar
214834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar/// isSingleElementStruct - Determine if a structure is a "single
215834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar/// element struct", i.e. it has exactly one non-empty field or
216834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar/// exactly one field which is itself a single element
217834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar/// struct. Structures with flexible array members are never
218834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar/// considered single element structs.
219834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar///
220834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar/// \return The field declaration for the single non-empty field, if
221834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar/// it exists.
222834af456588587194337bb32671fb9329e73a7f7Daniel Dunbarstatic const FieldDecl *isSingleElementStruct(QualType T) {
223834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar  const RecordType *RT = T->getAsStructureType();
224834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar  if (!RT)
225834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar    return 0;
226834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar
227834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar  const RecordDecl *RD = RT->getDecl();
228834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar  if (RD->hasFlexibleArrayMember())
229834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar    return 0;
230834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar
231834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar  const FieldDecl *Found = 0;
232834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar  for (RecordDecl::field_const_iterator i = RD->field_begin(),
233834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar         e = RD->field_end(); i != e; ++i) {
234834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar    const FieldDecl *FD = *i;
235834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar    QualType FT = FD->getType();
236834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar
237834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar    if (isEmptyStruct(FT)) {
238834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar      // Ignore
239834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar    } else if (Found) {
240834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar      return 0;
241834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar    } else if (!CodeGenFunction::hasAggregateLLVMType(FT)) {
242834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar      Found = FD;
243834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar    } else {
244834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar      Found = isSingleElementStruct(FT);
245834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar      if (!Found)
246834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar        return 0;
247834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar    }
248834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar  }
249834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar
250834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar  return Found;
251834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar}
252834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar
253834af456588587194337bb32671fb9329e73a7f7Daniel Dunbarstatic bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
254834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar  if (!Ty->getAsBuiltinType() && !Ty->isPointerType())
255834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar    return false;
256834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar
257834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar  uint64_t Size = Context.getTypeSize(Ty);
258834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar  return Size == 32 || Size == 64;
259834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar}
260834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar
261834af456588587194337bb32671fb9329e73a7f7Daniel Dunbarstatic bool areAllFields32Or64BitBasicType(const RecordDecl *RD,
262834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar                                           ASTContext &Context) {
263834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar  for (RecordDecl::field_const_iterator i = RD->field_begin(),
264834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar         e = RD->field_end(); i != e; ++i) {
265834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar    const FieldDecl *FD = *i;
266834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar
267834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar    if (!is32Or64BitBasicType(FD->getType(), Context))
268834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar      return false;
269834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar
270834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar    // If this is a bit-field we need to make sure it is still a
271834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar    // 32-bit or 64-bit type.
272834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar    if (Expr *BW = FD->getBitWidth()) {
273834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar      unsigned Width = BW->getIntegerConstantExprValue(Context).getZExtValue();
274834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar      if (Width <= 16)
275834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar        return false;
276834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar    }
277834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar  }
278834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar  return true;
279834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar}
280834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar
2816b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbarnamespace {
2826b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar/// DefaultABIInfo - The default implementation for ABI specific
2836b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar/// details. This implementation provides information which results in
2846b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar/// sensible LLVM IR generation, but does not conform to any
2856b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar/// particular ABI.
2866b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbarclass DefaultABIInfo : public ABIInfo {
2876b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar  virtual ABIArgInfo classifyReturnType(QualType RetTy,
2886b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar                                        ASTContext &Context) const;
2896b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar
2906b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar  virtual ABIArgInfo classifyArgumentType(QualType RetTy,
2916b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar                                          ASTContext &Context) const;
2926b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar};
2936b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar
2946b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar/// X86_32ABIInfo - The X86-32 ABI information.
2956b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbarclass X86_32ABIInfo : public ABIInfo {
2966b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbarpublic:
2976b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar  virtual ABIArgInfo classifyReturnType(QualType RetTy,
2986b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar                                        ASTContext &Context) const;
2996b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar
3006b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar  virtual ABIArgInfo classifyArgumentType(QualType RetTy,
3016b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar                                          ASTContext &Context) const;
3026b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar};
3036b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar}
3046b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar
3056b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel DunbarABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
3066b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar                                            ASTContext &Context) const {
3076660c8a4cc2115929d92be83bbc54c307002a321Daniel Dunbar  assert(!RetTy->isArrayType() &&
3086660c8a4cc2115929d92be83bbc54c307002a321Daniel Dunbar         "Array types cannot be passed directly.");
3092c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
310834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar    // Classify "single element" structs as their element type.
311834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar    const FieldDecl *SeltFD = isSingleElementStruct(RetTy);
312834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar    if (SeltFD) {
313834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar      QualType SeltTy = SeltFD->getType()->getDesugaredType();
314834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar      if (const BuiltinType *BT = SeltTy->getAsBuiltinType()) {
315834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar        // FIXME: This is gross, it would be nice if we could just
316834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar        // pass back SeltTy and have clients deal with it. Is it worth
317834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar        // supporting coerce to both LLVM and clang Types?
318834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar        if (BT->isIntegerType()) {
319834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar          uint64_t Size = Context.getTypeSize(SeltTy);
320834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar          return ABIArgInfo::getCoerce(llvm::IntegerType::get((unsigned) Size));
321834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar        } else if (BT->getKind() == BuiltinType::Float) {
322834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar          return ABIArgInfo::getCoerce(llvm::Type::FloatTy);
323834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar        } else if (BT->getKind() == BuiltinType::Double) {
324834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar          return ABIArgInfo::getCoerce(llvm::Type::DoubleTy);
325834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar        }
326834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar      } else if (SeltTy->isPointerType()) {
327834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar        // FIXME: It would be really nice if this could come out as
328834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar        // the proper pointer type.
329834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar        llvm::Type *PtrTy =
330834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar          llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
331834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar        return ABIArgInfo::getCoerce(PtrTy);
332834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar      }
333834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar    }
334834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar
335639ffe47097d01249b98b7acd102aaad6697b43aDaniel Dunbar    uint64_t Size = Context.getTypeSize(RetTy);
336639ffe47097d01249b98b7acd102aaad6697b43aDaniel Dunbar    if (Size == 8) {
337639ffe47097d01249b98b7acd102aaad6697b43aDaniel Dunbar      return ABIArgInfo::getCoerce(llvm::Type::Int8Ty);
338639ffe47097d01249b98b7acd102aaad6697b43aDaniel Dunbar    } else if (Size == 16) {
339639ffe47097d01249b98b7acd102aaad6697b43aDaniel Dunbar      return ABIArgInfo::getCoerce(llvm::Type::Int16Ty);
340639ffe47097d01249b98b7acd102aaad6697b43aDaniel Dunbar    } else if (Size == 32) {
341639ffe47097d01249b98b7acd102aaad6697b43aDaniel Dunbar      return ABIArgInfo::getCoerce(llvm::Type::Int32Ty);
342639ffe47097d01249b98b7acd102aaad6697b43aDaniel Dunbar    } else if (Size == 64) {
343639ffe47097d01249b98b7acd102aaad6697b43aDaniel Dunbar      return ABIArgInfo::getCoerce(llvm::Type::Int64Ty);
344639ffe47097d01249b98b7acd102aaad6697b43aDaniel Dunbar    } else {
345639ffe47097d01249b98b7acd102aaad6697b43aDaniel Dunbar      return ABIArgInfo::getStructRet();
346639ffe47097d01249b98b7acd102aaad6697b43aDaniel Dunbar    }
3472c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  } else {
3482c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar    return ABIArgInfo::getDefault();
3492c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  }
3502c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar}
3512c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar
3526b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel DunbarABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
3536b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar                                              ASTContext &Context) const {
3548951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar  assert(!Ty->isArrayType() && "Array types cannot be passed directly.");
355f035738e346da0a82538ab236aef218a27373635Daniel Dunbar  if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
356834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar    // Structures with flexible arrays are always byval.
357834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar    if (const RecordType *RT = Ty->getAsStructureType())
358834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar      if (RT->getDecl()->hasFlexibleArrayMember())
359834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar        return ABIArgInfo::getByVal(0);
360834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar
361834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar    // Expand empty structs (i.e. ignore)
362834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar    uint64_t Size = Context.getTypeSize(Ty);
363834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar    if (Ty->isStructureType() && Size == 0)
364834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar      return ABIArgInfo::getExpand();
365834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar
366834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar    // Expand structs with size <= 128-bits which consist only of
367834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar    // basic types (int, long long, float, double, xxx*). This is
368834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar    // non-recursive and does not ignore empty fields.
369834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar    if (const RecordType *RT = Ty->getAsStructureType()) {
370834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar      if (Context.getTypeSize(Ty) <= 4*32 &&
371834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar          areAllFields32Or64BitBasicType(RT->getDecl(), Context))
372834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar        return ABIArgInfo::getExpand();
373834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar    }
374834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar
3758951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    return ABIArgInfo::getByVal(0);
3768951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar  } else {
3778951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    return ABIArgInfo::getDefault();
3788951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar  }
3798951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar}
3808951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
3816b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel DunbarABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
3826b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar                                            ASTContext &Context) const {
3836b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar  return ABIArgInfo::getDefault();
3846b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar}
3856b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar
3866b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel DunbarABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
3876b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar                                              ASTContext &Context) const {
3886b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar  assert(!Ty->isArrayType() && "Array types cannot be passed directly.");
3896b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar  return ABIArgInfo::getDefault();
3906b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar}
3916b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar
3926b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbarconst ABIInfo &CodeGenTypes::getABIInfo() const {
3936b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar  if (TheABIInfo)
3946b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar    return *TheABIInfo;
3956b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar
3966b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar  // For now we just cache this in the CodeGenTypes and don't bother
3976b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar  // to free it.
3986b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar  const char *TargetPrefix = getContext().Target.getTargetPrefix();
3996b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar  if (strcmp(TargetPrefix, "x86") == 0) {
4006b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar    if (getContext().Target.getPointerWidth(0) == 32)
4016b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar      return *(TheABIInfo = new X86_32ABIInfo());
4026b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar  }
4036b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar
4046b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar  return *(TheABIInfo = new DefaultABIInfo);
4056b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar}
4066b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar
4076b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar// getABIReturnInfo - Wrap the ABIInfo getABIReturnInfo, altering
4086b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar// "default" types to StructRet when appropriate for simplicity.
4096b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbarstatic ABIArgInfo getABIReturnInfo(QualType Ty, CodeGenTypes &CGT) {
4106b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar  assert(!Ty->isArrayType() &&
4116b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar         "Array types cannot be passed directly.");
4126b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar  ABIArgInfo Info = CGT.getABIInfo().classifyReturnType(Ty, CGT.getContext());
413f035738e346da0a82538ab236aef218a27373635Daniel Dunbar  // Ensure default on aggregate types is StructRet.
414f035738e346da0a82538ab236aef218a27373635Daniel Dunbar  if (Info.isDefault() && CodeGenFunction::hasAggregateLLVMType(Ty))
415f035738e346da0a82538ab236aef218a27373635Daniel Dunbar    return ABIArgInfo::getStructRet();
416f035738e346da0a82538ab236aef218a27373635Daniel Dunbar  return Info;
417f035738e346da0a82538ab236aef218a27373635Daniel Dunbar}
418f035738e346da0a82538ab236aef218a27373635Daniel Dunbar
4196b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar// getABIArgumentInfo - Wrap the ABIInfo getABIReturnInfo, altering
4206b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar// "default" types to ByVal when appropriate for simplicity.
4216b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbarstatic ABIArgInfo getABIArgumentInfo(QualType Ty, CodeGenTypes &CGT) {
4226b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar  assert(!Ty->isArrayType() &&
4236b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar         "Array types cannot be passed directly.");
4246b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar  ABIArgInfo Info = CGT.getABIInfo().classifyArgumentType(Ty, CGT.getContext());
425f035738e346da0a82538ab236aef218a27373635Daniel Dunbar  // Ensure default on aggregate types is ByVal.
426f035738e346da0a82538ab236aef218a27373635Daniel Dunbar  if (Info.isDefault() && CodeGenFunction::hasAggregateLLVMType(Ty))
427f035738e346da0a82538ab236aef218a27373635Daniel Dunbar    return ABIArgInfo::getByVal(0);
428f035738e346da0a82538ab236aef218a27373635Daniel Dunbar  return Info;
429f035738e346da0a82538ab236aef218a27373635Daniel Dunbar}
430f035738e346da0a82538ab236aef218a27373635Daniel Dunbar
4312c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar/***/
4322c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar
4335627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbarvoid CodeGenTypes::GetExpandedTypes(QualType Ty,
4345627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar                                    std::vector<const llvm::Type*> &ArgTys) {
4355627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  const RecordType *RT = Ty->getAsStructureType();
4365627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  assert(RT && "Can only expand structure types.");
4375627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  const RecordDecl *RD = RT->getDecl();
4385627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  assert(!RD->hasFlexibleArrayMember() &&
4395627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar         "Cannot expand structure with flexible array.");
4405627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar
4415627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  for (RecordDecl::field_const_iterator i = RD->field_begin(),
4425627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar         e = RD->field_end(); i != e; ++i) {
4435627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    const FieldDecl *FD = *i;
4445627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    assert(!FD->isBitField() &&
4455627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar           "Cannot expand structure with bit-field members.");
4465627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar
4475627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    QualType FT = FD->getType();
4485627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    if (CodeGenFunction::hasAggregateLLVMType(FT)) {
4495627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      GetExpandedTypes(FT, ArgTys);
4505627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    } else {
4515627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      ArgTys.push_back(ConvertType(FT));
4525627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    }
4535627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  }
4545627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar}
4555627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar
4565627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbarllvm::Function::arg_iterator
4575627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel DunbarCodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
4585627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar                                    llvm::Function::arg_iterator AI) {
4595627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  const RecordType *RT = Ty->getAsStructureType();
4605627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  assert(RT && "Can only expand structure types.");
4615627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar
4625627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  RecordDecl *RD = RT->getDecl();
4635627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  assert(LV.isSimple() &&
4645627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar         "Unexpected non-simple lvalue during struct expansion.");
4655627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  llvm::Value *Addr = LV.getAddress();
4665627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  for (RecordDecl::field_iterator i = RD->field_begin(),
4675627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar         e = RD->field_end(); i != e; ++i) {
4685627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    FieldDecl *FD = *i;
4695627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    QualType FT = FD->getType();
4705627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar
4715627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    // FIXME: What are the right qualifiers here?
4725627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    LValue LV = EmitLValueForField(Addr, FD, false, 0);
4735627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    if (CodeGenFunction::hasAggregateLLVMType(FT)) {
4745627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      AI = ExpandTypeFromArgs(FT, LV, AI);
4755627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    } else {
4765627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      EmitStoreThroughLValue(RValue::get(AI), LV, FT);
4775627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      ++AI;
4785627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    }
4795627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  }
4805627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar
4815627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  return AI;
4825627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar}
4835627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar
4845627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbarvoid
4855627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel DunbarCodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
4865627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar                                  llvm::SmallVector<llvm::Value*, 16> &Args) {
4875627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  const RecordType *RT = Ty->getAsStructureType();
4885627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  assert(RT && "Can only expand structure types.");
4895627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar
4905627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  RecordDecl *RD = RT->getDecl();
4915627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
4925627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  llvm::Value *Addr = RV.getAggregateAddr();
4935627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  for (RecordDecl::field_iterator i = RD->field_begin(),
4945627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar         e = RD->field_end(); i != e; ++i) {
4955627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    FieldDecl *FD = *i;
4965627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    QualType FT = FD->getType();
4975627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar
4985627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    // FIXME: What are the right qualifiers here?
4995627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    LValue LV = EmitLValueForField(Addr, FD, false, 0);
5005627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    if (CodeGenFunction::hasAggregateLLVMType(FT)) {
5015627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
5025627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    } else {
5035627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      RValue RV = EmitLoadOfLValue(LV, FT);
5045627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      assert(RV.isScalar() &&
5055627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar             "Unexpected non-scalar rvalue during struct expansion.");
5065627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      Args.push_back(RV.getScalarVal());
5075627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    }
5085627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  }
5095627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar}
5105627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar
5115627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar/***/
5125627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar
51345c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbarconst llvm::FunctionType *
51462d5c1b5038cdaa4a887a03c37fe1e8d00166ea0Daniel DunbarCodeGenTypes::GetFunctionType(const CGCallInfo &CI, bool IsVariadic) {
51562d5c1b5038cdaa4a887a03c37fe1e8d00166ea0Daniel Dunbar  return GetFunctionType(CI.argtypes_begin(), CI.argtypes_end(), IsVariadic);
51662d5c1b5038cdaa4a887a03c37fe1e8d00166ea0Daniel Dunbar}
51762d5c1b5038cdaa4a887a03c37fe1e8d00166ea0Daniel Dunbar
51862d5c1b5038cdaa4a887a03c37fe1e8d00166ea0Daniel Dunbarconst llvm::FunctionType *
51945c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel DunbarCodeGenTypes::GetFunctionType(const CGFunctionInfo &FI) {
52062d5c1b5038cdaa4a887a03c37fe1e8d00166ea0Daniel Dunbar  return GetFunctionType(FI.argtypes_begin(), FI.argtypes_end(), FI.isVariadic());
52162d5c1b5038cdaa4a887a03c37fe1e8d00166ea0Daniel Dunbar}
52262d5c1b5038cdaa4a887a03c37fe1e8d00166ea0Daniel Dunbar
52362d5c1b5038cdaa4a887a03c37fe1e8d00166ea0Daniel Dunbarconst llvm::FunctionType *
52462d5c1b5038cdaa4a887a03c37fe1e8d00166ea0Daniel DunbarCodeGenTypes::GetFunctionType(ArgTypeIterator begin, ArgTypeIterator end,
52562d5c1b5038cdaa4a887a03c37fe1e8d00166ea0Daniel Dunbar                              bool IsVariadic) {
52645c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar  std::vector<const llvm::Type*> ArgTys;
52745c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar
52845c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar  const llvm::Type *ResultType = 0;
52945c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar
53045c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar  QualType RetTy = *begin;
5316b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar  ABIArgInfo RetAI = getABIReturnInfo(RetTy, *this);
5328951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar  switch (RetAI.getKind()) {
5338951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar  case ABIArgInfo::ByVal:
5348951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar  case ABIArgInfo::Expand:
5358951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    assert(0 && "Invalid ABI kind for return argument");
5368951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
53745c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar  case ABIArgInfo::Default:
53845c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar    if (RetTy->isVoidType()) {
53945c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar      ResultType = llvm::Type::VoidTy;
54045c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar    } else {
54162d5c1b5038cdaa4a887a03c37fe1e8d00166ea0Daniel Dunbar      ResultType = ConvertType(RetTy);
54245c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar    }
54345c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar    break;
54445c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar
54545c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar  case ABIArgInfo::StructRet: {
54645c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar    ResultType = llvm::Type::VoidTy;
54762d5c1b5038cdaa4a887a03c37fe1e8d00166ea0Daniel Dunbar    const llvm::Type *STy = ConvertType(RetTy);
54845c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar    ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
54945c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar    break;
55045c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar  }
55145c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar
55245c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar  case ABIArgInfo::Coerce:
553639ffe47097d01249b98b7acd102aaad6697b43aDaniel Dunbar    ResultType = RetAI.getCoerceToType();
55445c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar    break;
55545c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar  }
55645c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar
55745c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar  for (++begin; begin != end; ++begin) {
5586b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar    ABIArgInfo AI = getABIArgumentInfo(*begin, *this);
55962d5c1b5038cdaa4a887a03c37fe1e8d00166ea0Daniel Dunbar    const llvm::Type *Ty = ConvertType(*begin);
5608951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
5618951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    switch (AI.getKind()) {
5625627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    case ABIArgInfo::Coerce:
5638951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    case ABIArgInfo::StructRet:
5648951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar      assert(0 && "Invalid ABI kind for non-return argument");
5658951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
5668951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    case ABIArgInfo::ByVal:
56745c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar      // byval arguments are always on the stack, which is addr space #0.
56845c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar      ArgTys.push_back(llvm::PointerType::getUnqual(Ty));
5698951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar      assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled");
5708951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar      break;
5718951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
5728951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    case ABIArgInfo::Default:
5738951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar      ArgTys.push_back(Ty);
5748951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar      break;
5758951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
5768951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    case ABIArgInfo::Expand:
5775627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      GetExpandedTypes(*begin, ArgTys);
5788951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar      break;
5798951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    }
58045c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar  }
58145c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar
58262d5c1b5038cdaa4a887a03c37fe1e8d00166ea0Daniel Dunbar  return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
58345c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar}
58445c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar
585b768807c49a1c7085def099b848631856af766faDaniel Dunbarbool CodeGenModule::ReturnTypeUsesSret(QualType RetTy) {
5866b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar  return getABIReturnInfo(RetTy, getTypes()).isStructRet();
5873913f184c84135fb4612743f1faa6c1edd2dd055Daniel Dunbar}
5883913f184c84135fb4612743f1faa6c1edd2dd055Daniel Dunbar
589761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patelvoid CodeGenModule::ConstructAttributeList(const Decl *TargetDecl,
5902c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar                                           ArgTypeIterator begin,
5912c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar                                           ArgTypeIterator end,
592761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel                                           AttributeListType &PAL) {
5935323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar  unsigned FuncAttrs = 0;
594a2c6912c416c2d9f79d18f3a88ab0ae2609286c3Devang Patel  unsigned RetAttrs = 0;
5955323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar
5965323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar  if (TargetDecl) {
5975323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar    if (TargetDecl->getAttr<NoThrowAttr>())
598761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel      FuncAttrs |= llvm::Attribute::NoUnwind;
5995323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar    if (TargetDecl->getAttr<NoReturnAttr>())
600761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel      FuncAttrs |= llvm::Attribute::NoReturn;
601232eb7d33b96ad8f99de3b5ae840421b3a7c6cb7Anders Carlsson    if (TargetDecl->getAttr<PureAttr>())
602232eb7d33b96ad8f99de3b5ae840421b3a7c6cb7Anders Carlsson      FuncAttrs |= llvm::Attribute::ReadOnly;
603232eb7d33b96ad8f99de3b5ae840421b3a7c6cb7Anders Carlsson    if (TargetDecl->getAttr<ConstAttr>())
604232eb7d33b96ad8f99de3b5ae840421b3a7c6cb7Anders Carlsson      FuncAttrs |= llvm::Attribute::ReadNone;
6055323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar  }
6065323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar
6072c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  QualType RetTy = *begin;
6085323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar  unsigned Index = 1;
6096b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar  ABIArgInfo RetAI = getABIReturnInfo(RetTy, getTypes());
61045c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar  switch (RetAI.getKind()) {
6112c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  case ABIArgInfo::Default:
6122c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar    if (RetTy->isPromotableIntegerType()) {
6132c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar      if (RetTy->isSignedIntegerType()) {
614a2c6912c416c2d9f79d18f3a88ab0ae2609286c3Devang Patel        RetAttrs |= llvm::Attribute::SExt;
6152c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar      } else if (RetTy->isUnsignedIntegerType()) {
616a2c6912c416c2d9f79d18f3a88ab0ae2609286c3Devang Patel        RetAttrs |= llvm::Attribute::ZExt;
6172c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar      }
6182c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar    }
6192c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar    break;
6202c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar
6212c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  case ABIArgInfo::StructRet:
622761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel    PAL.push_back(llvm::AttributeWithIndex::get(Index,
623761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel                                                  llvm::Attribute::StructRet|
624761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel                                                  llvm::Attribute::NoAlias));
6255323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar    ++Index;
6262c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar    break;
6272c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar
6282c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  case ABIArgInfo::Coerce:
6292c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar    break;
6308951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
6318951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar  case ABIArgInfo::ByVal:
6328951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar  case ABIArgInfo::Expand:
6338951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    assert(0 && "Invalid ABI kind for return argument");
6345323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar  }
6352c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar
636a2c6912c416c2d9f79d18f3a88ab0ae2609286c3Devang Patel  if (RetAttrs)
637a2c6912c416c2d9f79d18f3a88ab0ae2609286c3Devang Patel    PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
6385627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  for (++begin; begin != end; ++begin) {
6395323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar    QualType ParamType = *begin;
640761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel    unsigned Attributes = 0;
6416b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar    ABIArgInfo AI = getABIArgumentInfo(ParamType, getTypes());
6428951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
6438951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    switch (AI.getKind()) {
6448951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    case ABIArgInfo::StructRet:
6455627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    case ABIArgInfo::Coerce:
6468951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar      assert(0 && "Invalid ABI kind for non-return argument");
6478951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
6488951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    case ABIArgInfo::ByVal:
649761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel      Attributes |= llvm::Attribute::ByVal;
6508951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar      assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled");
6518951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar      break;
6528951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
6538951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    case ABIArgInfo::Default:
6548951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar      if (ParamType->isPromotableIntegerType()) {
6558951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar        if (ParamType->isSignedIntegerType()) {
656761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel          Attributes |= llvm::Attribute::SExt;
6578951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar        } else if (ParamType->isUnsignedIntegerType()) {
658761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel          Attributes |= llvm::Attribute::ZExt;
6598951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar        }
6605323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar      }
6618951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar      break;
6628951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
6635627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    case ABIArgInfo::Expand: {
6645627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      std::vector<const llvm::Type*> Tys;
6655627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      // FIXME: This is rather inefficient. Do we ever actually need
6665627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      // to do anything here? The result should be just reconstructed
6675627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      // on the other side, so extension should be a non-issue.
6685627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      getTypes().GetExpandedTypes(ParamType, Tys);
6695627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      Index += Tys.size();
6705627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      continue;
6715627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    }
6725323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar    }
6738951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
674761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel    if (Attributes)
675761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel      PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
6765627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    ++Index;
6775323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar  }
678a2c6912c416c2d9f79d18f3a88ab0ae2609286c3Devang Patel  if (FuncAttrs)
679a2c6912c416c2d9f79d18f3a88ab0ae2609286c3Devang Patel    PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
680a2c6912c416c2d9f79d18f3a88ab0ae2609286c3Devang Patel
6815323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar}
6825323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar
68317b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbarvoid CodeGenFunction::EmitFunctionProlog(llvm::Function *Fn,
68417b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar                                         QualType RetTy,
68517b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar                                         const FunctionArgList &Args) {
68617b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  // Emit allocs for param decls.  Give the LLVM Argument nodes names.
68717b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  llvm::Function::arg_iterator AI = Fn->arg_begin();
68817b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar
68917b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  // Name the struct return argument.
6902c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  if (CGM.ReturnTypeUsesSret(RetTy)) {
69117b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar    AI->setName("agg.result");
69217b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar    ++AI;
69317b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  }
69417b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar
69517b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
6965627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar       i != e; ++i) {
69717b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar    const VarDecl *Arg = i->first;
6985627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    QualType Ty = i->second;
6996b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar    ABIArgInfo ArgI = getABIArgumentInfo(Ty, CGM.getTypes());
7008951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
7018951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    switch (ArgI.getKind()) {
7028951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    case ABIArgInfo::ByVal:
7038951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    case ABIArgInfo::Default: {
7048951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar      assert(AI != Fn->arg_end() && "Argument mismatch!");
7058951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar      llvm::Value* V = AI;
7065627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
7078951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar        // This must be a promotion, for something like
7088951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar        // "void a(x) short x; {..."
7095627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar        V = EmitScalarConversion(V, Ty, Arg->getType());
71017b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar      }
7118951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar      EmitParmDecl(*Arg, V);
7128951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar      break;
7138951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    }
7148951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
7155627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    case ABIArgInfo::Expand: {
7165627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      // If this was structure was expand into multiple arguments then
7175627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      // we need to create a temporary and reconstruct it from the
7185627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      // arguments.
71939f34e97d6a468f0a7dfa5664c61217cffc65b74Chris Lattner      std::string Name = Arg->getNameAsString();
7205627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      llvm::Value *Temp = CreateTempAlloca(ConvertType(Ty),
7215627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar                                           (Name + ".addr").c_str());
7225627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      // FIXME: What are the right qualifiers here?
7235627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      llvm::Function::arg_iterator End =
7245627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar        ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI);
7255627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      EmitParmDecl(*Arg, Temp);
7265627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar
7275627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      // Name the arguments used in expansion and increment AI.
7285627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      unsigned Index = 0;
7295627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      for (; AI != End; ++AI, ++Index)
7305627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar        AI->setName(Name + "." + llvm::utostr(Index));
7315627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      continue;
7325627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    }
7338951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
7345627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    case ABIArgInfo::Coerce:
7358951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    case ABIArgInfo::StructRet:
7368951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar      assert(0 && "Invalid ABI kind for non-return argument");
7378951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    }
7385627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar
7395627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    ++AI;
74017b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  }
74117b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  assert(AI == Fn->arg_end() && "Argument mismatch!");
74217b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar}
74317b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar
74417b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbarvoid CodeGenFunction::EmitFunctionEpilog(QualType RetTy,
74517b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar                                         llvm::Value *ReturnValue) {
7462c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  llvm::Value *RV = 0;
7472c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar
7482c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  // Functions with no result always return void.
7492c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  if (ReturnValue) {
7506b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar    ABIArgInfo RetAI = getABIReturnInfo(RetTy, CGM.getTypes());
7512c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar
7522c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar    switch (RetAI.getKind()) {
7532c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar    case ABIArgInfo::StructRet:
7540b685a5e4a473b180b7d2f433aaf09b91d3a0f78Anders Carlsson        if (RetTy->isAnyComplexType()) {
7550b685a5e4a473b180b7d2f433aaf09b91d3a0f78Anders Carlsson          // FIXME: Volatile
7560b685a5e4a473b180b7d2f433aaf09b91d3a0f78Anders Carlsson          ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
7571385f8e826bf9b65a81888888de82cd6b82a7768Anders Carlsson          StoreComplexToAddr(RT, CurFn->arg_begin(), false);
7580b685a5e4a473b180b7d2f433aaf09b91d3a0f78Anders Carlsson        } else
7590b685a5e4a473b180b7d2f433aaf09b91d3a0f78Anders Carlsson          EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy);
7602c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar      break;
7618951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
7622c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar    case ABIArgInfo::Default:
7632c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar      RV = Builder.CreateLoad(ReturnValue);
7642c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar      break;
7652c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar
766639ffe47097d01249b98b7acd102aaad6697b43aDaniel Dunbar    case ABIArgInfo::Coerce: {
767639ffe47097d01249b98b7acd102aaad6697b43aDaniel Dunbar      const llvm::Type *CoerceToPTy =
768639ffe47097d01249b98b7acd102aaad6697b43aDaniel Dunbar        llvm::PointerType::getUnqual(RetAI.getCoerceToType());
769639ffe47097d01249b98b7acd102aaad6697b43aDaniel Dunbar      RV = Builder.CreateLoad(Builder.CreateBitCast(ReturnValue, CoerceToPTy));
7708951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar      break;
771639ffe47097d01249b98b7acd102aaad6697b43aDaniel Dunbar    }
7728951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
7738951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    case ABIArgInfo::ByVal:
7748951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    case ABIArgInfo::Expand:
7758951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar      assert(0 && "Invalid ABI kind for return argument");
77617b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar    }
77717b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  }
7782c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar
7792c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  if (RV) {
7802c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar    Builder.CreateRet(RV);
7812c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  } else {
7822c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar    Builder.CreateRetVoid();
7832c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  }
78417b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar}
78517b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar
78617b708d61827cd86278e9580b041dd6cbadf07d3Daniel DunbarRValue CodeGenFunction::EmitCall(llvm::Value *Callee,
7872c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar                                 QualType RetTy,
78817b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar                                 const CallArgList &CallArgs) {
78917b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  llvm::SmallVector<llvm::Value*, 16> Args;
79017b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar
79117b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  // Handle struct-return functions by passing a pointer to the
79217b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  // location that we would like to return into.
7936b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar  ABIArgInfo RetAI = getABIReturnInfo(RetTy, CGM.getTypes());
7942c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  switch (RetAI.getKind()) {
7952c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  case ABIArgInfo::StructRet:
79617b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar    // Create a temporary alloca to hold the result of the call. :(
7975627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    Args.push_back(CreateTempAlloca(ConvertType(RetTy)));
7982c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar    break;
7992c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar
8002c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  case ABIArgInfo::Default:
8012c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  case ABIArgInfo::Coerce:
8022c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar    break;
8038951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
8048951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar  case ABIArgInfo::ByVal:
8058951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar  case ABIArgInfo::Expand:
8068951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    assert(0 && "Invalid ABI kind for return argument");
80717b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  }
80817b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar
80917b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
81017b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar       I != E; ++I) {
8116b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar    ABIArgInfo ArgInfo = getABIArgumentInfo(I->second, CGM.getTypes());
81217b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar    RValue RV = I->first;
8135627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar
8145627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    switch (ArgInfo.getKind()) {
8155627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    case ABIArgInfo::ByVal: // Default is byval
8165627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    case ABIArgInfo::Default:
8175627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      if (RV.isScalar()) {
8185627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar        Args.push_back(RV.getScalarVal());
8195627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      } else if (RV.isComplex()) {
8205627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar        // Make a temporary alloca to pass the argument.
8215627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar        Args.push_back(CreateTempAlloca(ConvertType(I->second)));
8225627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar        StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
8235627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      } else {
8245627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar        Args.push_back(RV.getAggregateAddr());
8255627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      }
8265627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      break;
8275627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar
8285627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    case ABIArgInfo::StructRet:
8295627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    case ABIArgInfo::Coerce:
8305627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      assert(0 && "Invalid ABI kind for non-return argument");
8315627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      break;
8325627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar
8335627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    case ABIArgInfo::Expand:
8345627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      ExpandTypeToArgs(I->second, RV, Args);
8355627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      break;
83617b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar    }
83717b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  }
83817b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar
83917b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  llvm::CallInst *CI = Builder.CreateCall(Callee,&Args[0],&Args[0]+Args.size());
8402c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  CGCallInfo CallInfo(RetTy, CallArgs);
84117b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar
8425323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar  // FIXME: Provide TargetDecl so nounwind, noreturn, etc, etc get set.
843761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel  CodeGen::AttributeListType AttributeList;
844761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel  CGM.ConstructAttributeList(0,
845b768807c49a1c7085def099b848631856af766faDaniel Dunbar                             CallInfo.argtypes_begin(), CallInfo.argtypes_end(),
846761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel                             AttributeList);
847761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel  CI->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(),
848761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel                                         AttributeList.size()));
84917b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar
85017b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee))
85117b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar    CI->setCallingConv(F->getCallingConv());
85217b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  if (CI->getType() != llvm::Type::VoidTy)
85317b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar    CI->setName("call");
8542c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar
8552c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  switch (RetAI.getKind()) {
8562c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  case ABIArgInfo::StructRet:
8572c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar    if (RetTy->isAnyComplexType())
8585627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
8592c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar    else
8602c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar      // Struct return.
8615627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      return RValue::getAggregate(Args[0]);
8628951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
8632c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  case ABIArgInfo::Default:
8642c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar    return RValue::get(RetTy->isVoidType() ? 0 : CI);
8652c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar
866639ffe47097d01249b98b7acd102aaad6697b43aDaniel Dunbar  case ABIArgInfo::Coerce: {
867639ffe47097d01249b98b7acd102aaad6697b43aDaniel Dunbar    const llvm::Type *CoerceToPTy =
868639ffe47097d01249b98b7acd102aaad6697b43aDaniel Dunbar      llvm::PointerType::getUnqual(RetAI.getCoerceToType());
869639ffe47097d01249b98b7acd102aaad6697b43aDaniel Dunbar    llvm::Value *V = CreateTempAlloca(ConvertType(RetTy), "tmp");
870639ffe47097d01249b98b7acd102aaad6697b43aDaniel Dunbar    Builder.CreateStore(CI, Builder.CreateBitCast(V, CoerceToPTy));
871639ffe47097d01249b98b7acd102aaad6697b43aDaniel Dunbar    return RValue::getAggregate(V);
872639ffe47097d01249b98b7acd102aaad6697b43aDaniel Dunbar  }
8738951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
8748951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar  case ABIArgInfo::ByVal:
8758951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar  case ABIArgInfo::Expand:
8768951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    assert(0 && "Invalid ABI kind for return argument");
87717b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  }
8782c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar
8792c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  assert(0 && "Unhandled ABIArgInfo::Kind");
8802c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  return RValue::get(0);
88117b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar}
882