CGCall.cpp revision a0a99e02f5b2de3817706071077298ef040634fe
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"
2299037e5a2118bc194251a8033a0885810bf61c95Daniel Dunbar#include "clang/AST/RecordLayout.h"
235627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar#include "llvm/ADT/StringExtras.h"
24d0646bd7c11c12b34971b55e5c1bdd8439401b4cDevang Patel#include "llvm/Attributes.h"
256f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar#include "llvm/Support/CommandLine.h"
2654d1ccbfcf19ddf39444f1b4018dd79487cc847bDaniel Dunbar#include "llvm/Target/TargetData.h"
270dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbarusing namespace clang;
280dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbarusing namespace CodeGen;
290dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar
300dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar/***/
310dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar
320dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar// FIXME: Use iterator and sidestep silly type array creation.
330dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar
34541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbarconst
35541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel DunbarCGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionTypeNoProto *FTNP) {
36541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar  return getFunctionInfo(FTNP->getResultType(),
37541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar                         llvm::SmallVector<QualType, 16>());
3845c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar}
3945c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar
40541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbarconst
41541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel DunbarCGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionTypeProto *FTP) {
42541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar  llvm::SmallVector<QualType, 16> ArgTys;
43541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar  // FIXME: Kill copy.
4445c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar  for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
45541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar    ArgTys.push_back(FTP->getArgType(i));
46541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar  return getFunctionInfo(FTP->getResultType(), ArgTys);
4745c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar}
4845c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar
49541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbarconst CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionDecl *FD) {
500dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar  const FunctionType *FTy = FD->getType()->getAsFunctionType();
51541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar  if (const FunctionTypeProto *FTP = dyn_cast<FunctionTypeProto>(FTy))
52541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar    return getFunctionInfo(FTP);
53541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar  return getFunctionInfo(cast<FunctionTypeNoProto>(FTy));
540dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar}
550dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar
56541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbarconst CGFunctionInfo &CodeGenTypes::getFunctionInfo(const ObjCMethodDecl *MD) {
57541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar  llvm::SmallVector<QualType, 16> ArgTys;
58541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar  ArgTys.push_back(MD->getSelfDecl()->getType());
59541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar  ArgTys.push_back(Context.getObjCSelType());
60541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar  // FIXME: Kill copy?
610dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar  for (ObjCMethodDecl::param_const_iterator i = MD->param_begin(),
620dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar         e = MD->param_end(); i != e; ++i)
63541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar    ArgTys.push_back((*i)->getType());
64541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar  return getFunctionInfo(MD->getResultType(), ArgTys);
650dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar}
660dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar
67541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbarconst CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
68541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar                                                    const CallArgList &Args) {
69541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar  // FIXME: Kill copy.
70541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar  llvm::SmallVector<QualType, 16> ArgTys;
71725ad31086e3d6c41afa10c43db44f2e7060a961Daniel Dunbar  for (CallArgList::const_iterator i = Args.begin(), e = Args.end();
72725ad31086e3d6c41afa10c43db44f2e7060a961Daniel Dunbar       i != e; ++i)
73541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar    ArgTys.push_back(i->second);
74541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar  return getFunctionInfo(ResTy, ArgTys);
750dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar}
760dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar
77541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbarconst CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
78541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar                                                  const FunctionArgList &Args) {
79541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar  // FIXME: Kill copy.
80541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar  llvm::SmallVector<QualType, 16> ArgTys;
81bb36d331f439f49859efcfb4435c61762fbba6f9Daniel Dunbar  for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
82bb36d331f439f49859efcfb4435c61762fbba6f9Daniel Dunbar       i != e; ++i)
83541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar    ArgTys.push_back(i->second);
84541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar  return getFunctionInfo(ResTy, ArgTys);
85541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar}
86541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar
87541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbarconst CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
88541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar                               const llvm::SmallVector<QualType, 16> &ArgTys) {
89541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar  return *new CGFunctionInfo(ResTy, ArgTys);
90541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar}
91541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar
92541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar/***/
93541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar
94541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel DunbarCGFunctionInfo::CGFunctionInfo(QualType ResTy,
95541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar                               const llvm::SmallVector<QualType, 16> &ArgTys) {
96541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar  ArgTypes.push_back(ResTy);
97541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar  ArgTypes.insert(ArgTypes.end(), ArgTys.begin(), ArgTys.end());
98bb36d331f439f49859efcfb4435c61762fbba6f9Daniel Dunbar}
99bb36d331f439f49859efcfb4435c61762fbba6f9Daniel Dunbar
100a0a99e02f5b2de3817706071077298ef040634feDaniel DunbarCGFunctionInfo::arg_iterator CGFunctionInfo::arg_begin() const {
101a0a99e02f5b2de3817706071077298ef040634feDaniel Dunbar  return ArgTypes.begin()+1;
1025323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar}
1035323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar
104a0a99e02f5b2de3817706071077298ef040634feDaniel DunbarCGFunctionInfo::arg_iterator CGFunctionInfo::arg_end() const {
1055323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar  return ArgTypes.end();
1060dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar}
10717b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar
10817b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar/***/
10917b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar
1108951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar/// ABIArgInfo - Helper class to encapsulate information about how a
1118951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar/// specific C type should be passed to or returned from a function.
1122c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbarclass ABIArgInfo {
1132c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbarpublic:
1142c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  enum Kind {
1152c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar    Default,
1163aea8cac3ec9ad03b1d2b002f38a0d42a70483c8Daniel Dunbar    StructRet, /// Only valid for return values. The return value
1173aea8cac3ec9ad03b1d2b002f38a0d42a70483c8Daniel Dunbar               /// should be passed through a pointer to a caller
1183aea8cac3ec9ad03b1d2b002f38a0d42a70483c8Daniel Dunbar               /// allocated location passed as an implicit first
1193aea8cac3ec9ad03b1d2b002f38a0d42a70483c8Daniel Dunbar               /// argument to the function.
1208951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
12111434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar    Ignore,    /// Ignore the argument (treat as void). Useful for
12211434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar               /// void and empty structs.
12311434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar
1245627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    Coerce,    /// Only valid for aggregate return types, the argument
1255627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar               /// should be accessed by coercion to a provided type.
1268951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
1278951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    ByVal,     /// Only valid for aggregate argument types. The
1288951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar               /// structure should be passed "byval" with the
1298951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar               /// specified alignment (0 indicates default
1308951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar               /// alignment).
1318951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
1328951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    Expand,    /// Only valid for aggregate argument types. The
1338951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar               /// structure should be expanded into consecutive
1345627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar               /// arguments for its constituent fields. Currently
1355627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar               /// expand is only allowed on structures whose fields
1365627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar               /// are all scalar types or are themselves expandable
1375627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar               /// types.
1388951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
1398951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    KindFirst=Default, KindLast=Expand
1402c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  };
1412c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar
1422c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbarprivate:
1432c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  Kind TheKind;
144639ffe47097d01249b98b7acd102aaad6697b43aDaniel Dunbar  const llvm::Type *TypeData;
1458951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar  unsigned UIntData;
1462c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar
1478951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar  ABIArgInfo(Kind K, const llvm::Type *TD=0,
1488951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar             unsigned UI=0) : TheKind(K),
1498951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar                              TypeData(TD),
1508951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar                              UIntData(0) {}
1512c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbarpublic:
1522c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  static ABIArgInfo getDefault() {
1538951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    return ABIArgInfo(Default);
1542c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  }
1552c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  static ABIArgInfo getStructRet() {
1568951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    return ABIArgInfo(StructRet);
1572c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  }
15811434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar  static ABIArgInfo getIgnore() {
15911434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar    return ABIArgInfo(Ignore);
16011434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar  }
161639ffe47097d01249b98b7acd102aaad6697b43aDaniel Dunbar  static ABIArgInfo getCoerce(const llvm::Type *T) {
1622c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar    return ABIArgInfo(Coerce, T);
1632c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  }
1648951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar  static ABIArgInfo getByVal(unsigned Alignment) {
1658951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    return ABIArgInfo(ByVal, 0, Alignment);
1668951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar  }
1675627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  static ABIArgInfo getExpand() {
1685627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    return ABIArgInfo(Expand);
1695627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  }
1702c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar
1712c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  Kind getKind() const { return TheKind; }
1722c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  bool isDefault() const { return TheKind == Default; }
1732c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  bool isStructRet() const { return TheKind == StructRet; }
17411434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar  bool isIgnore() const { return TheKind == Ignore; }
1752c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  bool isCoerce() const { return TheKind == Coerce; }
1768951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar  bool isByVal() const { return TheKind == ByVal; }
1775627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  bool isExpand() const { return TheKind == Expand; }
17845c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar
17945c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar  // Coerce accessors
180639ffe47097d01249b98b7acd102aaad6697b43aDaniel Dunbar  const llvm::Type *getCoerceToType() const {
18145c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar    assert(TheKind == Coerce && "Invalid kind!");
18245c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar    return TypeData;
18345c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar  }
1848951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
1858951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar  // ByVal accessors
1868951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar  unsigned getByValAlignment() const {
1878951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    assert(TheKind == ByVal && "Invalid kind!");
1888951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    return UIntData;
1898951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar  }
1902c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar};
1912c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar
1922c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar/***/
1932c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar
1946b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar/* FIXME: All of this stuff should be part of the target interface
1956b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar   somehow. It is currently here because it is not clear how to factor
1966b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar   the targets to support this, since the Targets currently live in a
1976b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar   layer below types n'stuff.
1986b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar */
1996b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar
2006b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar/// ABIInfo - Target specific hooks for defining how a type should be
2016b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar/// passed or returned from functions.
2026b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbarclass clang::ABIInfo {
2036b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbarpublic:
2046b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar  virtual ~ABIInfo();
2056b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar
2066b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar  virtual ABIArgInfo classifyReturnType(QualType RetTy,
2076b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar                                        ASTContext &Context) const = 0;
2086b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar
2096b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar  virtual ABIArgInfo classifyArgumentType(QualType Ty,
2106b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar                                          ASTContext &Context) const = 0;
2116b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar};
2126b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar
2136b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel DunbarABIInfo::~ABIInfo() {}
2146b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar
215834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar/// isEmptyStruct - Return true iff a structure has no non-empty
216834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar/// members. Note that a structure with a flexible array member is not
217834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar/// considered empty.
218834af456588587194337bb32671fb9329e73a7f7Daniel Dunbarstatic bool isEmptyStruct(QualType T) {
219834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar  const RecordType *RT = T->getAsStructureType();
220834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar  if (!RT)
221834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar    return 0;
222834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar  const RecordDecl *RD = RT->getDecl();
223834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar  if (RD->hasFlexibleArrayMember())
224834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar    return false;
225f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor  for (RecordDecl::field_iterator i = RD->field_begin(),
226834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar         e = RD->field_end(); i != e; ++i) {
227834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar    const FieldDecl *FD = *i;
228834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar    if (!isEmptyStruct(FD->getType()))
229834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar      return false;
230834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar  }
231834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar  return true;
232834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar}
233834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar
234834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar/// isSingleElementStruct - Determine if a structure is a "single
235834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar/// element struct", i.e. it has exactly one non-empty field or
236834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar/// exactly one field which is itself a single element
237834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar/// struct. Structures with flexible array members are never
238834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar/// considered single element structs.
239834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar///
240834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar/// \return The field declaration for the single non-empty field, if
241834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar/// it exists.
242834af456588587194337bb32671fb9329e73a7f7Daniel Dunbarstatic const FieldDecl *isSingleElementStruct(QualType T) {
243834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar  const RecordType *RT = T->getAsStructureType();
244834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar  if (!RT)
245834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar    return 0;
246834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar
247834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar  const RecordDecl *RD = RT->getDecl();
248834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar  if (RD->hasFlexibleArrayMember())
249834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar    return 0;
250834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar
251834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar  const FieldDecl *Found = 0;
252f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor  for (RecordDecl::field_iterator i = RD->field_begin(),
253834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar         e = RD->field_end(); i != e; ++i) {
254834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar    const FieldDecl *FD = *i;
255834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar    QualType FT = FD->getType();
256834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar
257834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar    if (isEmptyStruct(FT)) {
258834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar      // Ignore
259834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar    } else if (Found) {
260834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar      return 0;
261834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar    } else if (!CodeGenFunction::hasAggregateLLVMType(FT)) {
262834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar      Found = FD;
263834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar    } else {
264834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar      Found = isSingleElementStruct(FT);
265834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar      if (!Found)
266834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar        return 0;
267834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar    }
268834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar  }
269834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar
270834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar  return Found;
271834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar}
272834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar
273834af456588587194337bb32671fb9329e73a7f7Daniel Dunbarstatic bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
274834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar  if (!Ty->getAsBuiltinType() && !Ty->isPointerType())
275834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar    return false;
276834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar
277834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar  uint64_t Size = Context.getTypeSize(Ty);
278834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar  return Size == 32 || Size == 64;
279834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar}
280834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar
281834af456588587194337bb32671fb9329e73a7f7Daniel Dunbarstatic bool areAllFields32Or64BitBasicType(const RecordDecl *RD,
282834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar                                           ASTContext &Context) {
283f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor  for (RecordDecl::field_iterator i = RD->field_begin(),
284834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar         e = RD->field_end(); i != e; ++i) {
285834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar    const FieldDecl *FD = *i;
286834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar
287834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar    if (!is32Or64BitBasicType(FD->getType(), Context))
288834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar      return false;
289834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar
290834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar    // If this is a bit-field we need to make sure it is still a
291834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar    // 32-bit or 64-bit type.
292834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar    if (Expr *BW = FD->getBitWidth()) {
293834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar      unsigned Width = BW->getIntegerConstantExprValue(Context).getZExtValue();
294834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar      if (Width <= 16)
295834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar        return false;
296834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar    }
297834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar  }
298834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar  return true;
299834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar}
300834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar
3016b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbarnamespace {
3026b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar/// DefaultABIInfo - The default implementation for ABI specific
3036b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar/// details. This implementation provides information which results in
3046b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar/// sensible LLVM IR generation, but does not conform to any
3056b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar/// particular ABI.
3066b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbarclass DefaultABIInfo : public ABIInfo {
3076b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar  virtual ABIArgInfo classifyReturnType(QualType RetTy,
3086b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar                                        ASTContext &Context) const;
3096b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar
3106b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar  virtual ABIArgInfo classifyArgumentType(QualType RetTy,
3116b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar                                          ASTContext &Context) const;
3126b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar};
3136b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar
3146b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar/// X86_32ABIInfo - The X86-32 ABI information.
3156b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbarclass X86_32ABIInfo : public ABIInfo {
3166b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbarpublic:
3176b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar  virtual ABIArgInfo classifyReturnType(QualType RetTy,
3186b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar                                        ASTContext &Context) const;
3196b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar
3206b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar  virtual ABIArgInfo classifyArgumentType(QualType RetTy,
3216b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar                                          ASTContext &Context) const;
3226b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar};
3236b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar}
3246b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar
3256b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel DunbarABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
3266b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar                                            ASTContext &Context) const {
3272c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
328834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar    // Classify "single element" structs as their element type.
329834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar    const FieldDecl *SeltFD = isSingleElementStruct(RetTy);
330834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar    if (SeltFD) {
331834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar      QualType SeltTy = SeltFD->getType()->getDesugaredType();
332834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar      if (const BuiltinType *BT = SeltTy->getAsBuiltinType()) {
333834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar        // FIXME: This is gross, it would be nice if we could just
334834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar        // pass back SeltTy and have clients deal with it. Is it worth
335834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar        // supporting coerce to both LLVM and clang Types?
336834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar        if (BT->isIntegerType()) {
337834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar          uint64_t Size = Context.getTypeSize(SeltTy);
338834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar          return ABIArgInfo::getCoerce(llvm::IntegerType::get((unsigned) Size));
339834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar        } else if (BT->getKind() == BuiltinType::Float) {
340834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar          return ABIArgInfo::getCoerce(llvm::Type::FloatTy);
341834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar        } else if (BT->getKind() == BuiltinType::Double) {
342834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar          return ABIArgInfo::getCoerce(llvm::Type::DoubleTy);
343834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar        }
344834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar      } else if (SeltTy->isPointerType()) {
345834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar        // FIXME: It would be really nice if this could come out as
346834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar        // the proper pointer type.
347834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar        llvm::Type *PtrTy =
348834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar          llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
349834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar        return ABIArgInfo::getCoerce(PtrTy);
350834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar      }
351834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar    }
352834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar
353639ffe47097d01249b98b7acd102aaad6697b43aDaniel Dunbar    uint64_t Size = Context.getTypeSize(RetTy);
354639ffe47097d01249b98b7acd102aaad6697b43aDaniel Dunbar    if (Size == 8) {
355639ffe47097d01249b98b7acd102aaad6697b43aDaniel Dunbar      return ABIArgInfo::getCoerce(llvm::Type::Int8Ty);
356639ffe47097d01249b98b7acd102aaad6697b43aDaniel Dunbar    } else if (Size == 16) {
357639ffe47097d01249b98b7acd102aaad6697b43aDaniel Dunbar      return ABIArgInfo::getCoerce(llvm::Type::Int16Ty);
358639ffe47097d01249b98b7acd102aaad6697b43aDaniel Dunbar    } else if (Size == 32) {
359639ffe47097d01249b98b7acd102aaad6697b43aDaniel Dunbar      return ABIArgInfo::getCoerce(llvm::Type::Int32Ty);
360639ffe47097d01249b98b7acd102aaad6697b43aDaniel Dunbar    } else if (Size == 64) {
361639ffe47097d01249b98b7acd102aaad6697b43aDaniel Dunbar      return ABIArgInfo::getCoerce(llvm::Type::Int64Ty);
362639ffe47097d01249b98b7acd102aaad6697b43aDaniel Dunbar    } else {
363639ffe47097d01249b98b7acd102aaad6697b43aDaniel Dunbar      return ABIArgInfo::getStructRet();
364639ffe47097d01249b98b7acd102aaad6697b43aDaniel Dunbar    }
3652c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  } else {
3662c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar    return ABIArgInfo::getDefault();
3672c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  }
3682c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar}
3692c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar
3706b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel DunbarABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
3716b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar                                              ASTContext &Context) const {
372f035738e346da0a82538ab236aef218a27373635Daniel Dunbar  if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
373834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar    // Structures with flexible arrays are always byval.
374834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar    if (const RecordType *RT = Ty->getAsStructureType())
375834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar      if (RT->getDecl()->hasFlexibleArrayMember())
376834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar        return ABIArgInfo::getByVal(0);
377834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar
378834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar    // Expand empty structs (i.e. ignore)
379834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar    uint64_t Size = Context.getTypeSize(Ty);
380834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar    if (Ty->isStructureType() && Size == 0)
381834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar      return ABIArgInfo::getExpand();
382834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar
383834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar    // Expand structs with size <= 128-bits which consist only of
384834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar    // basic types (int, long long, float, double, xxx*). This is
385834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar    // non-recursive and does not ignore empty fields.
386834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar    if (const RecordType *RT = Ty->getAsStructureType()) {
387834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar      if (Context.getTypeSize(Ty) <= 4*32 &&
388834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar          areAllFields32Or64BitBasicType(RT->getDecl(), Context))
389834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar        return ABIArgInfo::getExpand();
390834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar    }
391834af456588587194337bb32671fb9329e73a7f7Daniel Dunbar
3928951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    return ABIArgInfo::getByVal(0);
3938951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar  } else {
3948951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    return ABIArgInfo::getDefault();
3958951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar  }
3968951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar}
3978951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
3986f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbarnamespace {
399c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar/// X86_64ABIInfo - The X86_64 ABI information.
4006f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbarclass X86_64ABIInfo : public ABIInfo {
4016f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar  enum Class {
4026f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar    Integer = 0,
4036f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar    SSE,
4046f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar    SSEUp,
4056f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar    X87,
4066f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar    X87Up,
4076f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar    ComplexX87,
4086f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar    NoClass,
4096f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar    Memory
4106f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar  };
4116f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar
4128562ae74ae0ff0a64b46ef5ebea8e7ce0f76fa00Daniel Dunbar  /// merge - Implement the X86_64 ABI merging algorithm.
4138562ae74ae0ff0a64b46ef5ebea8e7ce0f76fa00Daniel Dunbar  ///
414c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar  /// Merge an accumulating classification \arg Accum with a field
415c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar  /// classification \arg Field.
416c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar  ///
417c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar  /// \param Accum - The accumulating classification. This should
418c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar  /// always be either NoClass or the result of a previous merge
419c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar  /// call. In addition, this should never be Memory (the caller
420c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar  /// should just return Memory for the aggregate).
421c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar  Class merge(Class Accum, Class Field) const;
4228562ae74ae0ff0a64b46ef5ebea8e7ce0f76fa00Daniel Dunbar
4236f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar  /// classify - Determine the x86_64 register classes in which the
4246f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar  /// given type T should be passed.
4256f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar  ///
426c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar  /// \param Lo - The classification for the parts of the type
427c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar  /// residing in the low word of the containing object.
428c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar  ///
429c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar  /// \param Hi - The classification for the parts of the type
430c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar  /// residing in the high word of the containing object.
431c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar  ///
432c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar  /// \param OffsetBase - The bit offset of this type in the
433cdf920ed6c610e21fc899ada93370a68c900d180Daniel Dunbar  /// containing object.  Some parameters are classified different
434cdf920ed6c610e21fc899ada93370a68c900d180Daniel Dunbar  /// depending on whether they straddle an eightbyte boundary.
4356f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar  ///
4366f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar  /// If a word is unused its result will be NoClass; if a type should
4376f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar  /// be passed in Memory then at least the classification of \arg Lo
4386f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar  /// will be Memory.
4396f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar  ///
4406f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar  /// The \arg Lo class will be NoClass iff the argument is ignored.
4416f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar  ///
4426f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar  /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
4436f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar  /// be NoClass.
444e620ecdd4104285e09fe585e90f720459b80259bDaniel Dunbar  void classify(QualType T, ASTContext &Context, uint64_t OffsetBase,
4456f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar                Class &Lo, Class &Hi) const;
446c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar
4476f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbarpublic:
4486f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar  virtual ABIArgInfo classifyReturnType(QualType RetTy,
4496f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar                                        ASTContext &Context) const;
4506f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar
4516f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar  virtual ABIArgInfo classifyArgumentType(QualType RetTy,
4526f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar                                          ASTContext &Context) const;
4536f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar};
4546f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar}
4556f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar
456c450357c436c05aa02ef88d406de962d98ac50beDaniel DunbarX86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum,
457c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar                                          Class Field) const {
4588562ae74ae0ff0a64b46ef5ebea8e7ce0f76fa00Daniel Dunbar  // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
4598562ae74ae0ff0a64b46ef5ebea8e7ce0f76fa00Daniel Dunbar  // classified recursively so that always two fields are
4608562ae74ae0ff0a64b46ef5ebea8e7ce0f76fa00Daniel Dunbar  // considered. The resulting class is calculated according to
4618562ae74ae0ff0a64b46ef5ebea8e7ce0f76fa00Daniel Dunbar  // the classes of the fields in the eightbyte:
4628562ae74ae0ff0a64b46ef5ebea8e7ce0f76fa00Daniel Dunbar  //
4638562ae74ae0ff0a64b46ef5ebea8e7ce0f76fa00Daniel Dunbar  // (a) If both classes are equal, this is the resulting class.
4648562ae74ae0ff0a64b46ef5ebea8e7ce0f76fa00Daniel Dunbar  //
4658562ae74ae0ff0a64b46ef5ebea8e7ce0f76fa00Daniel Dunbar  // (b) If one of the classes is NO_CLASS, the resulting class is
4668562ae74ae0ff0a64b46ef5ebea8e7ce0f76fa00Daniel Dunbar  // the other class.
4678562ae74ae0ff0a64b46ef5ebea8e7ce0f76fa00Daniel Dunbar  //
4688562ae74ae0ff0a64b46ef5ebea8e7ce0f76fa00Daniel Dunbar  // (c) If one of the classes is MEMORY, the result is the MEMORY
4698562ae74ae0ff0a64b46ef5ebea8e7ce0f76fa00Daniel Dunbar  // class.
4708562ae74ae0ff0a64b46ef5ebea8e7ce0f76fa00Daniel Dunbar  //
4718562ae74ae0ff0a64b46ef5ebea8e7ce0f76fa00Daniel Dunbar  // (d) If one of the classes is INTEGER, the result is the
4728562ae74ae0ff0a64b46ef5ebea8e7ce0f76fa00Daniel Dunbar  // INTEGER.
4738562ae74ae0ff0a64b46ef5ebea8e7ce0f76fa00Daniel Dunbar  //
4748562ae74ae0ff0a64b46ef5ebea8e7ce0f76fa00Daniel Dunbar  // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
4758562ae74ae0ff0a64b46ef5ebea8e7ce0f76fa00Daniel Dunbar  // MEMORY is used as class.
4768562ae74ae0ff0a64b46ef5ebea8e7ce0f76fa00Daniel Dunbar  //
4778562ae74ae0ff0a64b46ef5ebea8e7ce0f76fa00Daniel Dunbar  // (f) Otherwise class SSE is used.
478c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar  assert((Accum == NoClass || Accum == Integer ||
479c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar          Accum == SSE || Accum == SSEUp) &&
480c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar         "Invalid accumulated classification during merge.");
481c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar  if (Accum == Field || Field == NoClass)
482c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar    return Accum;
483c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar  else if (Field == Memory)
484c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar    return Memory;
485c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar  else if (Accum == NoClass)
486c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar    return Field;
487c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar  else if (Accum == Integer || Field == Integer)
488c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar    return Integer;
489c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar  else if (Field == X87 || Field == X87Up || Field == ComplexX87)
490c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar    return Memory;
4918562ae74ae0ff0a64b46ef5ebea8e7ce0f76fa00Daniel Dunbar  else
492c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar    return SSE;
4938562ae74ae0ff0a64b46ef5ebea8e7ce0f76fa00Daniel Dunbar}
4948562ae74ae0ff0a64b46ef5ebea8e7ce0f76fa00Daniel Dunbar
4956f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbarvoid X86_64ABIInfo::classify(QualType Ty,
4966f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar                             ASTContext &Context,
497e620ecdd4104285e09fe585e90f720459b80259bDaniel Dunbar                             uint64_t OffsetBase,
4986f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar                             Class &Lo, Class &Hi) const {
4999a82b52ae83fa1c09266b2fa5f0375392f7d127fDaniel Dunbar  // FIXME: This code can be simplified by introducing a simple value
5009a82b52ae83fa1c09266b2fa5f0375392f7d127fDaniel Dunbar  // class for Class pairs with appropriate constructor methods for
5019a82b52ae83fa1c09266b2fa5f0375392f7d127fDaniel Dunbar  // the various situations.
5029a82b52ae83fa1c09266b2fa5f0375392f7d127fDaniel Dunbar
503c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar  Lo = Hi = NoClass;
504c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar
505c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar  Class &Current = OffsetBase < 64 ? Lo : Hi;
506c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar  Current = Memory;
507c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar
5086f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar  if (const BuiltinType *BT = Ty->getAsBuiltinType()) {
5096f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar    BuiltinType::Kind k = BT->getKind();
5106f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar
51111434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar    if (k == BuiltinType::Void) {
512c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar      Current = NoClass;
51311434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar    } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
514c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar      Current = Integer;
5156f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar    } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
516c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar      Current = SSE;
5176f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar    } else if (k == BuiltinType::LongDouble) {
5186f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar      Lo = X87;
5196f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar      Hi = X87Up;
5206f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar    }
5217a6605d8be0fc30f1846657ee8133387b1b85296Daniel Dunbar    // FIXME: _Decimal32 and _Decimal64 are SSE.
5227a6605d8be0fc30f1846657ee8133387b1b85296Daniel Dunbar    // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
5236f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar    // FIXME: __int128 is (Integer, Integer).
5246f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar  } else if (Ty->isPointerLikeType() || Ty->isBlockPointerType() ||
5256f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar             Ty->isObjCQualifiedInterfaceType()) {
526c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar    Current = Integer;
5277a6605d8be0fc30f1846657ee8133387b1b85296Daniel Dunbar  } else if (const VectorType *VT = Ty->getAsVectorType()) {
528e620ecdd4104285e09fe585e90f720459b80259bDaniel Dunbar    uint64_t Size = Context.getTypeSize(VT);
5297a6605d8be0fc30f1846657ee8133387b1b85296Daniel Dunbar    if (Size == 64) {
530d4cd1b07169c698bd003be102bd73a7da7e24b25Daniel Dunbar      // gcc passes <1 x double> in memory.
531c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar      if (VT->getElementType() == Context.DoubleTy)
532d4cd1b07169c698bd003be102bd73a7da7e24b25Daniel Dunbar        return;
533d4cd1b07169c698bd003be102bd73a7da7e24b25Daniel Dunbar
534c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar      Current = SSE;
535e33edf150adc44a34c5f6c8ed6109f6dcd2f70c6Daniel Dunbar
536e33edf150adc44a34c5f6c8ed6109f6dcd2f70c6Daniel Dunbar      // If this type crosses an eightbyte boundary, it should be
537e33edf150adc44a34c5f6c8ed6109f6dcd2f70c6Daniel Dunbar      // split.
538cdf920ed6c610e21fc899ada93370a68c900d180Daniel Dunbar      if (OffsetBase && OffsetBase != 64)
539e33edf150adc44a34c5f6c8ed6109f6dcd2f70c6Daniel Dunbar        Hi = Lo;
5407a6605d8be0fc30f1846657ee8133387b1b85296Daniel Dunbar    } else if (Size == 128) {
5417a6605d8be0fc30f1846657ee8133387b1b85296Daniel Dunbar      Lo = SSE;
5427a6605d8be0fc30f1846657ee8133387b1b85296Daniel Dunbar      Hi = SSEUp;
5437a6605d8be0fc30f1846657ee8133387b1b85296Daniel Dunbar    }
5446f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar  } else if (const ComplexType *CT = Ty->getAsComplexType()) {
5456f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar    QualType ET = CT->getElementType();
5466f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar
547e33edf150adc44a34c5f6c8ed6109f6dcd2f70c6Daniel Dunbar    uint64_t Size = Context.getTypeSize(Ty);
548eac48dc189154eea5067310c5dd257dfe07c29aaDaniel Dunbar    if (ET->isIntegerType()) {
549eac48dc189154eea5067310c5dd257dfe07c29aaDaniel Dunbar      if (Size <= 64)
550c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar        Current = Integer;
551eac48dc189154eea5067310c5dd257dfe07c29aaDaniel Dunbar      else if (Size <= 128)
552eac48dc189154eea5067310c5dd257dfe07c29aaDaniel Dunbar        Lo = Hi = Integer;
553eac48dc189154eea5067310c5dd257dfe07c29aaDaniel Dunbar    } else if (ET == Context.FloatTy)
554c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar      Current = SSE;
5556f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar    else if (ET == Context.DoubleTy)
5566f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar      Lo = Hi = SSE;
5576f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar    else if (ET == Context.LongDoubleTy)
558c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar      Current = ComplexX87;
559f04d69bbb25eb681fff1a108f13f67c6ca70cf6aDaniel Dunbar
560f04d69bbb25eb681fff1a108f13f67c6ca70cf6aDaniel Dunbar    // If this complex type crosses an eightbyte boundary then it
561f04d69bbb25eb681fff1a108f13f67c6ca70cf6aDaniel Dunbar    // should be split.
562cdf920ed6c610e21fc899ada93370a68c900d180Daniel Dunbar    uint64_t EB_Real = (OffsetBase) / 64;
563cdf920ed6c610e21fc899ada93370a68c900d180Daniel Dunbar    uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64;
564f04d69bbb25eb681fff1a108f13f67c6ca70cf6aDaniel Dunbar    if (Hi == NoClass && EB_Real != EB_Imag)
565f04d69bbb25eb681fff1a108f13f67c6ca70cf6aDaniel Dunbar      Hi = Lo;
5668562ae74ae0ff0a64b46ef5ebea8e7ce0f76fa00Daniel Dunbar  } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
5678562ae74ae0ff0a64b46ef5ebea8e7ce0f76fa00Daniel Dunbar    // Arrays are treated like structures.
5688562ae74ae0ff0a64b46ef5ebea8e7ce0f76fa00Daniel Dunbar
5698562ae74ae0ff0a64b46ef5ebea8e7ce0f76fa00Daniel Dunbar    uint64_t Size = Context.getTypeSize(Ty);
5708562ae74ae0ff0a64b46ef5ebea8e7ce0f76fa00Daniel Dunbar
5718562ae74ae0ff0a64b46ef5ebea8e7ce0f76fa00Daniel Dunbar    // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
5728562ae74ae0ff0a64b46ef5ebea8e7ce0f76fa00Daniel Dunbar    // than two eightbytes, ..., it has class MEMORY.
5738562ae74ae0ff0a64b46ef5ebea8e7ce0f76fa00Daniel Dunbar    if (Size > 128)
5748562ae74ae0ff0a64b46ef5ebea8e7ce0f76fa00Daniel Dunbar      return;
5758562ae74ae0ff0a64b46ef5ebea8e7ce0f76fa00Daniel Dunbar
5768562ae74ae0ff0a64b46ef5ebea8e7ce0f76fa00Daniel Dunbar    // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
5778562ae74ae0ff0a64b46ef5ebea8e7ce0f76fa00Daniel Dunbar    // fields, it has class MEMORY.
5788562ae74ae0ff0a64b46ef5ebea8e7ce0f76fa00Daniel Dunbar    //
5798562ae74ae0ff0a64b46ef5ebea8e7ce0f76fa00Daniel Dunbar    // Only need to check alignment of array base.
580c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar    if (OffsetBase % Context.getTypeAlign(AT->getElementType()))
5818562ae74ae0ff0a64b46ef5ebea8e7ce0f76fa00Daniel Dunbar      return;
5828562ae74ae0ff0a64b46ef5ebea8e7ce0f76fa00Daniel Dunbar
5838562ae74ae0ff0a64b46ef5ebea8e7ce0f76fa00Daniel Dunbar    // Otherwise implement simplified merge. We could be smarter about
5848562ae74ae0ff0a64b46ef5ebea8e7ce0f76fa00Daniel Dunbar    // this, but it isn't worth it and would be harder to verify.
585c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar    Current = NoClass;
5868562ae74ae0ff0a64b46ef5ebea8e7ce0f76fa00Daniel Dunbar    uint64_t EltSize = Context.getTypeSize(AT->getElementType());
5878562ae74ae0ff0a64b46ef5ebea8e7ce0f76fa00Daniel Dunbar    uint64_t ArraySize = AT->getSize().getZExtValue();
5888562ae74ae0ff0a64b46ef5ebea8e7ce0f76fa00Daniel Dunbar    for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
5898562ae74ae0ff0a64b46ef5ebea8e7ce0f76fa00Daniel Dunbar      Class FieldLo, FieldHi;
5908562ae74ae0ff0a64b46ef5ebea8e7ce0f76fa00Daniel Dunbar      classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi);
591c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar      Lo = merge(Lo, FieldLo);
592c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar      Hi = merge(Hi, FieldHi);
593c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar      if (Lo == Memory || Hi == Memory)
594c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar        break;
5958562ae74ae0ff0a64b46ef5ebea8e7ce0f76fa00Daniel Dunbar    }
596c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar
597c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar    // Do post merger cleanup (see below). Only case we worry about is Memory.
598c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar    if (Hi == Memory)
599c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar      Lo = Memory;
600c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar    assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
60199037e5a2118bc194251a8033a0885810bf61c95Daniel Dunbar  } else if (const RecordType *RT = Ty->getAsRecordType()) {
602e620ecdd4104285e09fe585e90f720459b80259bDaniel Dunbar    uint64_t Size = Context.getTypeSize(Ty);
60399037e5a2118bc194251a8033a0885810bf61c95Daniel Dunbar
60499037e5a2118bc194251a8033a0885810bf61c95Daniel Dunbar    // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
60599037e5a2118bc194251a8033a0885810bf61c95Daniel Dunbar    // than two eightbytes, ..., it has class MEMORY.
60699037e5a2118bc194251a8033a0885810bf61c95Daniel Dunbar    if (Size > 128)
60799037e5a2118bc194251a8033a0885810bf61c95Daniel Dunbar      return;
60899037e5a2118bc194251a8033a0885810bf61c95Daniel Dunbar
60999037e5a2118bc194251a8033a0885810bf61c95Daniel Dunbar    const RecordDecl *RD = RT->getDecl();
61099037e5a2118bc194251a8033a0885810bf61c95Daniel Dunbar
61199037e5a2118bc194251a8033a0885810bf61c95Daniel Dunbar    // Assume variable sized types are passed in memory.
61299037e5a2118bc194251a8033a0885810bf61c95Daniel Dunbar    if (RD->hasFlexibleArrayMember())
61399037e5a2118bc194251a8033a0885810bf61c95Daniel Dunbar      return;
61499037e5a2118bc194251a8033a0885810bf61c95Daniel Dunbar
61599037e5a2118bc194251a8033a0885810bf61c95Daniel Dunbar    const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
61699037e5a2118bc194251a8033a0885810bf61c95Daniel Dunbar
61799037e5a2118bc194251a8033a0885810bf61c95Daniel Dunbar    // Reset Lo class, this will be recomputed.
618c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar    Current = NoClass;
61999037e5a2118bc194251a8033a0885810bf61c95Daniel Dunbar    unsigned idx = 0;
62099037e5a2118bc194251a8033a0885810bf61c95Daniel Dunbar    for (RecordDecl::field_iterator i = RD->field_begin(),
62199037e5a2118bc194251a8033a0885810bf61c95Daniel Dunbar           e = RD->field_end(); i != e; ++i, ++idx) {
6228562ae74ae0ff0a64b46ef5ebea8e7ce0f76fa00Daniel Dunbar      uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
62399037e5a2118bc194251a8033a0885810bf61c95Daniel Dunbar
6248562ae74ae0ff0a64b46ef5ebea8e7ce0f76fa00Daniel Dunbar      // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
6258562ae74ae0ff0a64b46ef5ebea8e7ce0f76fa00Daniel Dunbar      // fields, it has class MEMORY.
62699037e5a2118bc194251a8033a0885810bf61c95Daniel Dunbar      if (Offset % Context.getTypeAlign(i->getType())) {
62799037e5a2118bc194251a8033a0885810bf61c95Daniel Dunbar        Lo = Memory;
62899037e5a2118bc194251a8033a0885810bf61c95Daniel Dunbar        return;
62999037e5a2118bc194251a8033a0885810bf61c95Daniel Dunbar      }
63099037e5a2118bc194251a8033a0885810bf61c95Daniel Dunbar
63199037e5a2118bc194251a8033a0885810bf61c95Daniel Dunbar      // Classify this field.
632c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar      //
633c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar      // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
634c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar      // exceeds a single eightbyte, each is classified
635c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar      // separately. Each eightbyte gets initialized to class
636c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar      // NO_CLASS.
63799037e5a2118bc194251a8033a0885810bf61c95Daniel Dunbar      Class FieldLo, FieldHi;
638f04d69bbb25eb681fff1a108f13f67c6ca70cf6aDaniel Dunbar      classify(i->getType(), Context, Offset, FieldLo, FieldHi);
639c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar      Lo = merge(Lo, FieldLo);
640c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar      Hi = merge(Hi, FieldHi);
641c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar      if (Lo == Memory || Hi == Memory)
642c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar        break;
64399037e5a2118bc194251a8033a0885810bf61c95Daniel Dunbar    }
64499037e5a2118bc194251a8033a0885810bf61c95Daniel Dunbar
64599037e5a2118bc194251a8033a0885810bf61c95Daniel Dunbar    // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
64699037e5a2118bc194251a8033a0885810bf61c95Daniel Dunbar    //
64799037e5a2118bc194251a8033a0885810bf61c95Daniel Dunbar    // (a) If one of the classes is MEMORY, the whole argument is
64899037e5a2118bc194251a8033a0885810bf61c95Daniel Dunbar    // passed in memory.
64999037e5a2118bc194251a8033a0885810bf61c95Daniel Dunbar    //
65099037e5a2118bc194251a8033a0885810bf61c95Daniel Dunbar    // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
65199037e5a2118bc194251a8033a0885810bf61c95Daniel Dunbar
65299037e5a2118bc194251a8033a0885810bf61c95Daniel Dunbar    // The first of these conditions is guaranteed by how we implement
653c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar    // the merge (just bail).
654c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar    //
655c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar    // The second condition occurs in the case of unions; for example
656c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar    // union { _Complex double; unsigned; }.
657c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar    if (Hi == Memory)
658c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar      Lo = Memory;
65999037e5a2118bc194251a8033a0885810bf61c95Daniel Dunbar    if (Hi == SSEUp && Lo != SSE)
660c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar      Hi = SSE;
6616f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar  }
6626f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar}
6636f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar
664c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar
665d4edfe4746c66d2ac452c8a8d7cac17192283e75Daniel DunbarABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
666d4edfe4746c66d2ac452c8a8d7cac17192283e75Daniel Dunbar                                            ASTContext &Context) const {
6676f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar  // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
6686f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar  // classification algorithm.
6696f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar  X86_64ABIInfo::Class Lo, Hi;
670f04d69bbb25eb681fff1a108f13f67c6ca70cf6aDaniel Dunbar  classify(RetTy, Context, 0, Lo, Hi);
6716f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar
672c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar  // Check some invariants.
673c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar  assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
674c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar  assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
675c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar  assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
676c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar
6776f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar  const llvm::Type *ResType = 0;
6786f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar  switch (Lo) {
6796f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar  case NoClass:
68011434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar    return ABIArgInfo::getIgnore();
6816f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar
6826f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar  case SSEUp:
6836f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar  case X87Up:
6846f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar    assert(0 && "Invalid classification for lo word.");
6856f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar
686c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar    // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
687c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar    // hidden argument, i.e. structret.
6886f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar  case Memory:
6896f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar    return ABIArgInfo::getStructRet();
6906f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar
6916f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar    // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
6926f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar    // available register of the sequence %rax, %rdx is used.
6936f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar  case Integer:
6946f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar    ResType = llvm::Type::Int64Ty; break;
6956f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar
6966f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar    // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
6976f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar    // available SSE register of the sequence %xmm0, %xmm1 is used.
6986f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar  case SSE:
6996f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar    ResType = llvm::Type::DoubleTy; break;
7006f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar
7016f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar    // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
7026f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar    // returned on the X87 stack in %st0 as 80-bit x87 number.
7036f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar  case X87:
7046f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar    ResType = llvm::Type::X86_FP80Ty; break;
7056f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar
706c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar    // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
707c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar    // part of the value is returned in %st0 and the imaginary part in
708c450357c436c05aa02ef88d406de962d98ac50beDaniel Dunbar    // %st1.
7096f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar  case ComplexX87:
7106f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar    assert(Hi == NoClass && "Unexpected ComplexX87 classification.");
7116f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar    ResType = llvm::VectorType::get(llvm::Type::X86_FP80Ty, 2);
7126f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar    break;
7136f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar  }
7146f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar
7156f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar  switch (Hi) {
7166f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar    // Memory was handled previously, and ComplexX87 and X87 should
7176f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar    // never occur as hi classes.
7186f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar  case Memory:
7196f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar  case X87:
7206f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar  case ComplexX87:
7216f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar    assert(0 && "Invalid classification for hi word.");
7226f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar
7236f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar  case NoClass: break;
7246f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar  case Integer:
725b0e14f2a878c1a823341d34ca8f8fe60419fb9efDaniel Dunbar    ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
726b0e14f2a878c1a823341d34ca8f8fe60419fb9efDaniel Dunbar    break;
7276f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar  case SSE:
728b0e14f2a878c1a823341d34ca8f8fe60419fb9efDaniel Dunbar    ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
729b0e14f2a878c1a823341d34ca8f8fe60419fb9efDaniel Dunbar    break;
7306f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar
7316f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar    // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
7326f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar    // is passed in the upper half of the last used SSE register.
7336f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar    //
7346f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar    // SSEUP should always be preceeded by SSE, just widen.
7356f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar  case SSEUp:
7366f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar    assert(Lo == SSE && "Unexpected SSEUp classification.");
7376f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar    ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
7386f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar    break;
7396f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar
7406f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar    // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
741b0e14f2a878c1a823341d34ca8f8fe60419fb9efDaniel Dunbar    // returned together with the previous X87 value in %st0.
7426f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar    //
7436f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar    // X87UP should always be preceeded by X87, so we don't need to do
7446f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar    // anything here.
7456f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar  case X87Up:
7466f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar    assert(Lo == X87 && "Unexpected X87Up classification.");
7476f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar    break;
7486f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar  }
7496f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar
7506f3e7fac422fb0c50e1579edf49a797797c821ccDaniel Dunbar  return ABIArgInfo::getCoerce(ResType);
751d4edfe4746c66d2ac452c8a8d7cac17192283e75Daniel Dunbar}
752d4edfe4746c66d2ac452c8a8d7cac17192283e75Daniel Dunbar
753d4edfe4746c66d2ac452c8a8d7cac17192283e75Daniel DunbarABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty,
754d4edfe4746c66d2ac452c8a8d7cac17192283e75Daniel Dunbar                                              ASTContext &Context) const {
755d4edfe4746c66d2ac452c8a8d7cac17192283e75Daniel Dunbar  return ABIArgInfo::getDefault();
756d4edfe4746c66d2ac452c8a8d7cac17192283e75Daniel Dunbar}
757d4edfe4746c66d2ac452c8a8d7cac17192283e75Daniel Dunbar
7586b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel DunbarABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
7596b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar                                            ASTContext &Context) const {
7606b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar  return ABIArgInfo::getDefault();
7616b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar}
7626b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar
7636b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel DunbarABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
7646b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar                                              ASTContext &Context) const {
7656b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar  return ABIArgInfo::getDefault();
7666b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar}
7676b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar
7686b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbarconst ABIInfo &CodeGenTypes::getABIInfo() const {
7696b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar  if (TheABIInfo)
7706b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar    return *TheABIInfo;
7716b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar
7726b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar  // For now we just cache this in the CodeGenTypes and don't bother
7736b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar  // to free it.
7746b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar  const char *TargetPrefix = getContext().Target.getTargetPrefix();
7756b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar  if (strcmp(TargetPrefix, "x86") == 0) {
776d4edfe4746c66d2ac452c8a8d7cac17192283e75Daniel Dunbar    switch (getContext().Target.getPointerWidth(0)) {
777d4edfe4746c66d2ac452c8a8d7cac17192283e75Daniel Dunbar    case 32:
7786b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar      return *(TheABIInfo = new X86_32ABIInfo());
779d4edfe4746c66d2ac452c8a8d7cac17192283e75Daniel Dunbar    case 64:
78011a76ed8e34901f2b997896a8c0833c6c17bc586Daniel Dunbar      return *(TheABIInfo = new X86_64ABIInfo());
781d4edfe4746c66d2ac452c8a8d7cac17192283e75Daniel Dunbar    }
7826b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar  }
7836b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar
7846b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar  return *(TheABIInfo = new DefaultABIInfo);
7856b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar}
7866b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar
7876b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar// getABIReturnInfo - Wrap the ABIInfo getABIReturnInfo, altering
7886b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar// "default" types to StructRet when appropriate for simplicity.
7896b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbarstatic ABIArgInfo getABIReturnInfo(QualType Ty, CodeGenTypes &CGT) {
7906b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar  assert(!Ty->isArrayType() &&
7916b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar         "Array types cannot be passed directly.");
7926b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar  ABIArgInfo Info = CGT.getABIInfo().classifyReturnType(Ty, CGT.getContext());
793f035738e346da0a82538ab236aef218a27373635Daniel Dunbar  // Ensure default on aggregate types is StructRet.
794f035738e346da0a82538ab236aef218a27373635Daniel Dunbar  if (Info.isDefault() && CodeGenFunction::hasAggregateLLVMType(Ty))
795f035738e346da0a82538ab236aef218a27373635Daniel Dunbar    return ABIArgInfo::getStructRet();
796f035738e346da0a82538ab236aef218a27373635Daniel Dunbar  return Info;
797f035738e346da0a82538ab236aef218a27373635Daniel Dunbar}
798f035738e346da0a82538ab236aef218a27373635Daniel Dunbar
7996b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar// getABIArgumentInfo - Wrap the ABIInfo getABIReturnInfo, altering
8006b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar// "default" types to ByVal when appropriate for simplicity.
8016b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbarstatic ABIArgInfo getABIArgumentInfo(QualType Ty, CodeGenTypes &CGT) {
8026b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar  assert(!Ty->isArrayType() &&
8036b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar         "Array types cannot be passed directly.");
8046b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar  ABIArgInfo Info = CGT.getABIInfo().classifyArgumentType(Ty, CGT.getContext());
805f035738e346da0a82538ab236aef218a27373635Daniel Dunbar  // Ensure default on aggregate types is ByVal.
806f035738e346da0a82538ab236aef218a27373635Daniel Dunbar  if (Info.isDefault() && CodeGenFunction::hasAggregateLLVMType(Ty))
807f035738e346da0a82538ab236aef218a27373635Daniel Dunbar    return ABIArgInfo::getByVal(0);
808f035738e346da0a82538ab236aef218a27373635Daniel Dunbar  return Info;
809f035738e346da0a82538ab236aef218a27373635Daniel Dunbar}
810f035738e346da0a82538ab236aef218a27373635Daniel Dunbar
8112c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar/***/
8122c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar
8135627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbarvoid CodeGenTypes::GetExpandedTypes(QualType Ty,
8145627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar                                    std::vector<const llvm::Type*> &ArgTys) {
8155627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  const RecordType *RT = Ty->getAsStructureType();
8165627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  assert(RT && "Can only expand structure types.");
8175627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  const RecordDecl *RD = RT->getDecl();
8185627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  assert(!RD->hasFlexibleArrayMember() &&
8195627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar         "Cannot expand structure with flexible array.");
8205627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar
821f8d49f64ef6ab7e632717a31631fc289aab69428Douglas Gregor  for (RecordDecl::field_iterator i = RD->field_begin(),
8225627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar         e = RD->field_end(); i != e; ++i) {
8235627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    const FieldDecl *FD = *i;
8245627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    assert(!FD->isBitField() &&
8255627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar           "Cannot expand structure with bit-field members.");
8265627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar
8275627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    QualType FT = FD->getType();
8285627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    if (CodeGenFunction::hasAggregateLLVMType(FT)) {
8295627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      GetExpandedTypes(FT, ArgTys);
8305627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    } else {
8315627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      ArgTys.push_back(ConvertType(FT));
8325627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    }
8335627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  }
8345627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar}
8355627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar
8365627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbarllvm::Function::arg_iterator
8375627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel DunbarCodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
8385627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar                                    llvm::Function::arg_iterator AI) {
8395627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  const RecordType *RT = Ty->getAsStructureType();
8405627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  assert(RT && "Can only expand structure types.");
8415627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar
8425627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  RecordDecl *RD = RT->getDecl();
8435627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  assert(LV.isSimple() &&
8445627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar         "Unexpected non-simple lvalue during struct expansion.");
8455627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  llvm::Value *Addr = LV.getAddress();
8465627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  for (RecordDecl::field_iterator i = RD->field_begin(),
8475627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar         e = RD->field_end(); i != e; ++i) {
8485627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    FieldDecl *FD = *i;
8495627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    QualType FT = FD->getType();
8505627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar
8515627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    // FIXME: What are the right qualifiers here?
8525627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    LValue LV = EmitLValueForField(Addr, FD, false, 0);
8535627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    if (CodeGenFunction::hasAggregateLLVMType(FT)) {
8545627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      AI = ExpandTypeFromArgs(FT, LV, AI);
8555627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    } else {
8565627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      EmitStoreThroughLValue(RValue::get(AI), LV, FT);
8575627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      ++AI;
8585627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    }
8595627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  }
8605627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar
8615627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  return AI;
8625627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar}
8635627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar
8645627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbarvoid
8655627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel DunbarCodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
8665627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar                                  llvm::SmallVector<llvm::Value*, 16> &Args) {
8675627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  const RecordType *RT = Ty->getAsStructureType();
8685627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  assert(RT && "Can only expand structure types.");
8695627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar
8705627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  RecordDecl *RD = RT->getDecl();
8715627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
8725627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  llvm::Value *Addr = RV.getAggregateAddr();
8735627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  for (RecordDecl::field_iterator i = RD->field_begin(),
8745627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar         e = RD->field_end(); i != e; ++i) {
8755627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    FieldDecl *FD = *i;
8765627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    QualType FT = FD->getType();
8775627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar
8785627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    // FIXME: What are the right qualifiers here?
8795627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    LValue LV = EmitLValueForField(Addr, FD, false, 0);
8805627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    if (CodeGenFunction::hasAggregateLLVMType(FT)) {
8815627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
8825627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    } else {
8835627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      RValue RV = EmitLoadOfLValue(LV, FT);
8845627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      assert(RV.isScalar() &&
8855627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar             "Unexpected non-scalar rvalue during struct expansion.");
8865627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      Args.push_back(RV.getScalarVal());
8875627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    }
8885627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  }
8895627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar}
8905627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar
891275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
892275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar/// a pointer to an object of type \arg Ty.
893275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar///
894275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar/// This safely handles the case when the src type is smaller than the
895275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar/// destination type; in this situation the values of bits which not
896275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar/// present in the src are undefined.
897275e10d62af4a129a8559253958296d8659684c9Daniel Dunbarstatic llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
898275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar                                      const llvm::Type *Ty,
899275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar                                      CodeGenFunction &CGF) {
900275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar  const llvm::Type *SrcTy =
901275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar    cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
902275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar  uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
903275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar  uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(Ty);
904275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar
905275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar  // If load is legal, just bitcase the src pointer.
906275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar  if (SrcSize == DstSize) {
907275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar    llvm::Value *Casted =
908275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar      CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
909275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar    return CGF.Builder.CreateLoad(Casted);
910275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar  } else {
911275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar    assert(SrcSize < DstSize && "Coercion is losing source bits!");
912275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar
913275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar    // Otherwise do coercion through memory. This is stupid, but
914275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar    // simple.
915275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar    llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
916275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar    llvm::Value *Casted =
917275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar      CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
918275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar    CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
919275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar    return CGF.Builder.CreateLoad(Tmp);
920275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar  }
921275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar}
922275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar
923275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
924275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar/// where the source and destination may have different types.
925275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar///
926275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar/// This safely handles the case when the src type is larger than the
927275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar/// destination type; the upper bits of the src will be lost.
928275e10d62af4a129a8559253958296d8659684c9Daniel Dunbarstatic void CreateCoercedStore(llvm::Value *Src,
929275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar                               llvm::Value *DstPtr,
930275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar                               CodeGenFunction &CGF) {
931275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar  const llvm::Type *SrcTy = Src->getType();
932275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar  const llvm::Type *DstTy =
933275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar    cast<llvm::PointerType>(DstPtr->getType())->getElementType();
934275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar
935275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar  uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
936275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar  uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(DstTy);
937275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar
938275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar  // If store is legal, just bitcase the src pointer.
939275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar  if (SrcSize == DstSize) {
940275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar    llvm::Value *Casted =
941275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar      CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
942275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar    CGF.Builder.CreateStore(Src, Casted);
943275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar  } else {
944275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar    assert(SrcSize > DstSize && "Coercion is missing bits!");
945275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar
946275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar    // Otherwise do coercion through memory. This is stupid, but
947275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar    // simple.
948275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar    llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
949275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar    CGF.Builder.CreateStore(Src, Tmp);
950275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar    llvm::Value *Casted =
951275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar      CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
952275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar    CGF.Builder.CreateStore(CGF.Builder.CreateLoad(Casted), DstPtr);
953275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar  }
954275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar}
955275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar
9565627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar/***/
9575627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar
95888b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbarbool CodeGenModule::ReturnTypeUsesSret(const CGFunctionInfo &FI) {
95988b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar  return getABIReturnInfo(FI.getReturnType(), getTypes()).isStructRet();
960bb36d331f439f49859efcfb4435c61762fbba6f9Daniel Dunbar}
961bb36d331f439f49859efcfb4435c61762fbba6f9Daniel Dunbar
96245c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbarconst llvm::FunctionType *
963bb36d331f439f49859efcfb4435c61762fbba6f9Daniel DunbarCodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic) {
96445c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar  std::vector<const llvm::Type*> ArgTys;
96545c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar
96645c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar  const llvm::Type *ResultType = 0;
96745c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar
968a0a99e02f5b2de3817706071077298ef040634feDaniel Dunbar  QualType RetTy = FI.getReturnType();
9696b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar  ABIArgInfo RetAI = getABIReturnInfo(RetTy, *this);
9708951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar  switch (RetAI.getKind()) {
9718951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar  case ABIArgInfo::ByVal:
9728951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar  case ABIArgInfo::Expand:
9738951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    assert(0 && "Invalid ABI kind for return argument");
9748951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
97545c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar  case ABIArgInfo::Default:
97645c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar    if (RetTy->isVoidType()) {
97745c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar      ResultType = llvm::Type::VoidTy;
97845c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar    } else {
97962d5c1b5038cdaa4a887a03c37fe1e8d00166ea0Daniel Dunbar      ResultType = ConvertType(RetTy);
98045c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar    }
98145c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar    break;
98245c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar
98345c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar  case ABIArgInfo::StructRet: {
98445c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar    ResultType = llvm::Type::VoidTy;
98562d5c1b5038cdaa4a887a03c37fe1e8d00166ea0Daniel Dunbar    const llvm::Type *STy = ConvertType(RetTy);
98645c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar    ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
98745c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar    break;
98845c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar  }
98945c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar
99011434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar  case ABIArgInfo::Ignore:
99111434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar    ResultType = llvm::Type::VoidTy;
99211434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar    break;
99311434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar
99445c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar  case ABIArgInfo::Coerce:
995639ffe47097d01249b98b7acd102aaad6697b43aDaniel Dunbar    ResultType = RetAI.getCoerceToType();
99645c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar    break;
99745c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar  }
99845c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar
999a0a99e02f5b2de3817706071077298ef040634feDaniel Dunbar  for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1000a0a99e02f5b2de3817706071077298ef040634feDaniel Dunbar       it != ie; ++it) {
1001a0a99e02f5b2de3817706071077298ef040634feDaniel Dunbar    ABIArgInfo AI = getABIArgumentInfo(*it, *this);
1002a0a99e02f5b2de3817706071077298ef040634feDaniel Dunbar    const llvm::Type *Ty = ConvertType(*it);
10038951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
10048951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    switch (AI.getKind()) {
100511434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar    case ABIArgInfo::Ignore:
100611434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar      break;
100711434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar
10085627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    case ABIArgInfo::Coerce:
10098951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    case ABIArgInfo::StructRet:
10108951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar      assert(0 && "Invalid ABI kind for non-return argument");
10118951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
10128951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    case ABIArgInfo::ByVal:
101345c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar      // byval arguments are always on the stack, which is addr space #0.
101445c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar      ArgTys.push_back(llvm::PointerType::getUnqual(Ty));
10158951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar      assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled");
10168951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar      break;
10178951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
10188951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    case ABIArgInfo::Default:
10198951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar      ArgTys.push_back(Ty);
10208951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar      break;
10218951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
10228951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    case ABIArgInfo::Expand:
1023a0a99e02f5b2de3817706071077298ef040634feDaniel Dunbar      GetExpandedTypes(*it, ArgTys);
10248951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar      break;
10258951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    }
102645c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar  }
102745c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar
1028bb36d331f439f49859efcfb4435c61762fbba6f9Daniel Dunbar  return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
10293913f184c84135fb4612743f1faa6c1edd2dd055Daniel Dunbar}
10303913f184c84135fb4612743f1faa6c1edd2dd055Daniel Dunbar
1031a0a99e02f5b2de3817706071077298ef040634feDaniel Dunbarvoid CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
103288b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar                                           const Decl *TargetDecl,
1033761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel                                           AttributeListType &PAL) {
10345323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar  unsigned FuncAttrs = 0;
1035a2c6912c416c2d9f79d18f3a88ab0ae2609286c3Devang Patel  unsigned RetAttrs = 0;
10365323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar
10375323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar  if (TargetDecl) {
10385323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar    if (TargetDecl->getAttr<NoThrowAttr>())
1039761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel      FuncAttrs |= llvm::Attribute::NoUnwind;
10405323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar    if (TargetDecl->getAttr<NoReturnAttr>())
1041761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel      FuncAttrs |= llvm::Attribute::NoReturn;
1042232eb7d33b96ad8f99de3b5ae840421b3a7c6cb7Anders Carlsson    if (TargetDecl->getAttr<PureAttr>())
1043232eb7d33b96ad8f99de3b5ae840421b3a7c6cb7Anders Carlsson      FuncAttrs |= llvm::Attribute::ReadOnly;
1044232eb7d33b96ad8f99de3b5ae840421b3a7c6cb7Anders Carlsson    if (TargetDecl->getAttr<ConstAttr>())
1045232eb7d33b96ad8f99de3b5ae840421b3a7c6cb7Anders Carlsson      FuncAttrs |= llvm::Attribute::ReadNone;
10465323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar  }
10475323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar
1048a0a99e02f5b2de3817706071077298ef040634feDaniel Dunbar  QualType RetTy = FI.getReturnType();
10495323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar  unsigned Index = 1;
10506b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar  ABIArgInfo RetAI = getABIReturnInfo(RetTy, getTypes());
105145c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar  switch (RetAI.getKind()) {
10522c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  case ABIArgInfo::Default:
10532c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar    if (RetTy->isPromotableIntegerType()) {
10542c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar      if (RetTy->isSignedIntegerType()) {
1055a2c6912c416c2d9f79d18f3a88ab0ae2609286c3Devang Patel        RetAttrs |= llvm::Attribute::SExt;
10562c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar      } else if (RetTy->isUnsignedIntegerType()) {
1057a2c6912c416c2d9f79d18f3a88ab0ae2609286c3Devang Patel        RetAttrs |= llvm::Attribute::ZExt;
10582c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar      }
10592c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar    }
10602c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar    break;
10612c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar
10622c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  case ABIArgInfo::StructRet:
1063761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel    PAL.push_back(llvm::AttributeWithIndex::get(Index,
1064725ad31086e3d6c41afa10c43db44f2e7060a961Daniel Dunbar                                                llvm::Attribute::StructRet |
1065725ad31086e3d6c41afa10c43db44f2e7060a961Daniel Dunbar                                                llvm::Attribute::NoAlias));
10665323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar    ++Index;
10672c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar    break;
10682c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar
106911434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar  case ABIArgInfo::Ignore:
10702c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  case ABIArgInfo::Coerce:
10712c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar    break;
10728951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
10738951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar  case ABIArgInfo::ByVal:
10748951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar  case ABIArgInfo::Expand:
10758951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    assert(0 && "Invalid ABI kind for return argument");
10765323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar  }
10772c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar
1078a2c6912c416c2d9f79d18f3a88ab0ae2609286c3Devang Patel  if (RetAttrs)
1079a2c6912c416c2d9f79d18f3a88ab0ae2609286c3Devang Patel    PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
1080a0a99e02f5b2de3817706071077298ef040634feDaniel Dunbar  for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1081a0a99e02f5b2de3817706071077298ef040634feDaniel Dunbar       it != ie; ++it) {
1082a0a99e02f5b2de3817706071077298ef040634feDaniel Dunbar    QualType ParamType = *it;
1083761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel    unsigned Attributes = 0;
10846b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar    ABIArgInfo AI = getABIArgumentInfo(ParamType, getTypes());
10858951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
10868951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    switch (AI.getKind()) {
10878951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    case ABIArgInfo::StructRet:
10885627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    case ABIArgInfo::Coerce:
10898951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar      assert(0 && "Invalid ABI kind for non-return argument");
10908951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
10918951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    case ABIArgInfo::ByVal:
1092761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel      Attributes |= llvm::Attribute::ByVal;
10938951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar      assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled");
10948951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar      break;
10958951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
10968951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    case ABIArgInfo::Default:
10978951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar      if (ParamType->isPromotableIntegerType()) {
10988951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar        if (ParamType->isSignedIntegerType()) {
1099761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel          Attributes |= llvm::Attribute::SExt;
11008951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar        } else if (ParamType->isUnsignedIntegerType()) {
1101761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel          Attributes |= llvm::Attribute::ZExt;
11028951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar        }
11035323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar      }
11048951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar      break;
11058951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
110611434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar    case ABIArgInfo::Ignore:
110711434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar      // Skip increment, no matching LLVM parameter.
110811434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar      continue;
110911434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar
11105627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    case ABIArgInfo::Expand: {
11115627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      std::vector<const llvm::Type*> Tys;
11125627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      // FIXME: This is rather inefficient. Do we ever actually need
11135627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      // to do anything here? The result should be just reconstructed
11145627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      // on the other side, so extension should be a non-issue.
11155627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      getTypes().GetExpandedTypes(ParamType, Tys);
11165627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      Index += Tys.size();
11175627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      continue;
11185627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    }
11195323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar    }
11208951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
1121761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel    if (Attributes)
1122761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel      PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
11235627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    ++Index;
11245323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar  }
1125a2c6912c416c2d9f79d18f3a88ab0ae2609286c3Devang Patel  if (FuncAttrs)
1126a2c6912c416c2d9f79d18f3a88ab0ae2609286c3Devang Patel    PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
1127a2c6912c416c2d9f79d18f3a88ab0ae2609286c3Devang Patel
11285323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar}
11295323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar
113088b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbarvoid CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
113188b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar                                         llvm::Function *Fn,
113217b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar                                         const FunctionArgList &Args) {
113317b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  // Emit allocs for param decls.  Give the LLVM Argument nodes names.
113417b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  llvm::Function::arg_iterator AI = Fn->arg_begin();
113517b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar
113617b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  // Name the struct return argument.
113788b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar  if (CGM.ReturnTypeUsesSret(FI)) {
113817b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar    AI->setName("agg.result");
113917b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar    ++AI;
114017b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  }
114117b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar
114217b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
11435627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar       i != e; ++i) {
114417b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar    const VarDecl *Arg = i->first;
11455627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    QualType Ty = i->second;
11466b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar    ABIArgInfo ArgI = getABIArgumentInfo(Ty, CGM.getTypes());
11478951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
11488951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    switch (ArgI.getKind()) {
11498951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    case ABIArgInfo::ByVal:
11508951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    case ABIArgInfo::Default: {
11518951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar      assert(AI != Fn->arg_end() && "Argument mismatch!");
11528951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar      llvm::Value* V = AI;
11535627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
11548951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar        // This must be a promotion, for something like
11558951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar        // "void a(x) short x; {..."
11565627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar        V = EmitScalarConversion(V, Ty, Arg->getType());
115717b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar      }
11588951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar      EmitParmDecl(*Arg, V);
11598951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar      break;
11608951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    }
11618951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
11625627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    case ABIArgInfo::Expand: {
11635627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      // If this was structure was expand into multiple arguments then
11645627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      // we need to create a temporary and reconstruct it from the
11655627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      // arguments.
116639f34e97d6a468f0a7dfa5664c61217cffc65b74Chris Lattner      std::string Name = Arg->getNameAsString();
11675627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      llvm::Value *Temp = CreateTempAlloca(ConvertType(Ty),
11685627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar                                           (Name + ".addr").c_str());
11695627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      // FIXME: What are the right qualifiers here?
11705627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      llvm::Function::arg_iterator End =
11715627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar        ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI);
11725627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      EmitParmDecl(*Arg, Temp);
11735627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar
11745627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      // Name the arguments used in expansion and increment AI.
11755627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      unsigned Index = 0;
11765627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      for (; AI != End; ++AI, ++Index)
11775627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar        AI->setName(Name + "." + llvm::utostr(Index));
11785627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      continue;
11795627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    }
118011434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar
118111434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar    case ABIArgInfo::Ignore:
118211434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar      break;
118311434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar
11845627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    case ABIArgInfo::Coerce:
11858951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    case ABIArgInfo::StructRet:
11868951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar      assert(0 && "Invalid ABI kind for non-return argument");
11878951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    }
11885627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar
11895627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    ++AI;
119017b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  }
119117b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  assert(AI == Fn->arg_end() && "Argument mismatch!");
119217b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar}
119317b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar
119488b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbarvoid CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI,
119517b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar                                         llvm::Value *ReturnValue) {
11962c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  llvm::Value *RV = 0;
11972c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar
11982c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  // Functions with no result always return void.
11992c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  if (ReturnValue) {
120088b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar    QualType RetTy = FI.getReturnType();
12016b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar    ABIArgInfo RetAI = getABIReturnInfo(RetTy, CGM.getTypes());
12022c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar
12032c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar    switch (RetAI.getKind()) {
12042c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar    case ABIArgInfo::StructRet:
12053aea8cac3ec9ad03b1d2b002f38a0d42a70483c8Daniel Dunbar      if (RetTy->isAnyComplexType()) {
12063aea8cac3ec9ad03b1d2b002f38a0d42a70483c8Daniel Dunbar        // FIXME: Volatile
12073aea8cac3ec9ad03b1d2b002f38a0d42a70483c8Daniel Dunbar        ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
12083aea8cac3ec9ad03b1d2b002f38a0d42a70483c8Daniel Dunbar        StoreComplexToAddr(RT, CurFn->arg_begin(), false);
12093aea8cac3ec9ad03b1d2b002f38a0d42a70483c8Daniel Dunbar      } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
12103aea8cac3ec9ad03b1d2b002f38a0d42a70483c8Daniel Dunbar        EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy);
12113aea8cac3ec9ad03b1d2b002f38a0d42a70483c8Daniel Dunbar      } else {
12123aea8cac3ec9ad03b1d2b002f38a0d42a70483c8Daniel Dunbar        Builder.CreateStore(Builder.CreateLoad(ReturnValue),
12133aea8cac3ec9ad03b1d2b002f38a0d42a70483c8Daniel Dunbar                            CurFn->arg_begin());
12143aea8cac3ec9ad03b1d2b002f38a0d42a70483c8Daniel Dunbar      }
12152c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar      break;
12168951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
12172c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar    case ABIArgInfo::Default:
12182c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar      RV = Builder.CreateLoad(ReturnValue);
12192c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar      break;
12202c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar
122111434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar    case ABIArgInfo::Ignore:
122211434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar      break;
122311434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar
1224639ffe47097d01249b98b7acd102aaad6697b43aDaniel Dunbar    case ABIArgInfo::Coerce: {
122554d1ccbfcf19ddf39444f1b4018dd79487cc847bDaniel Dunbar      RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
12268951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar      break;
1227639ffe47097d01249b98b7acd102aaad6697b43aDaniel Dunbar    }
12288951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
12298951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    case ABIArgInfo::ByVal:
12308951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    case ABIArgInfo::Expand:
12318951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar      assert(0 && "Invalid ABI kind for return argument");
123217b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar    }
123317b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  }
12342c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar
12352c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  if (RV) {
12362c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar    Builder.CreateRet(RV);
12372c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  } else {
12382c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar    Builder.CreateRetVoid();
12392c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  }
124017b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar}
124117b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar
124288b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel DunbarRValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
124388b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar                                 llvm::Value *Callee,
124417b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar                                 const CallArgList &CallArgs) {
124517b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  llvm::SmallVector<llvm::Value*, 16> Args;
124617b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar
124717b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  // Handle struct-return functions by passing a pointer to the
124817b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  // location that we would like to return into.
1249bb36d331f439f49859efcfb4435c61762fbba6f9Daniel Dunbar  QualType RetTy = CallInfo.getReturnType();
12506b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar  ABIArgInfo RetAI = getABIReturnInfo(RetTy, CGM.getTypes());
12512c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  switch (RetAI.getKind()) {
12522c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  case ABIArgInfo::StructRet:
125317b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar    // Create a temporary alloca to hold the result of the call. :(
12545627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    Args.push_back(CreateTempAlloca(ConvertType(RetTy)));
12552c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar    break;
12562c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar
12572c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  case ABIArgInfo::Default:
125811434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar  case ABIArgInfo::Ignore:
12592c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  case ABIArgInfo::Coerce:
12602c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar    break;
12618951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
12628951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar  case ABIArgInfo::ByVal:
12638951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar  case ABIArgInfo::Expand:
12648951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    assert(0 && "Invalid ABI kind for return argument");
126517b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  }
126617b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar
126717b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
126817b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar       I != E; ++I) {
12696b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar    ABIArgInfo ArgInfo = getABIArgumentInfo(I->second, CGM.getTypes());
127017b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar    RValue RV = I->first;
12715627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar
12725627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    switch (ArgInfo.getKind()) {
12735627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    case ABIArgInfo::ByVal: // Default is byval
12745627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    case ABIArgInfo::Default:
12755627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      if (RV.isScalar()) {
12765627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar        Args.push_back(RV.getScalarVal());
12775627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      } else if (RV.isComplex()) {
12785627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar        // Make a temporary alloca to pass the argument.
12795627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar        Args.push_back(CreateTempAlloca(ConvertType(I->second)));
12805627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar        StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
12815627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      } else {
12825627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar        Args.push_back(RV.getAggregateAddr());
12835627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      }
12845627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      break;
12855627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar
128611434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar    case ABIArgInfo::Ignore:
128711434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar      break;
128811434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar
12895627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    case ABIArgInfo::StructRet:
12905627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    case ABIArgInfo::Coerce:
12915627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      assert(0 && "Invalid ABI kind for non-return argument");
12925627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      break;
12935627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar
12945627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    case ABIArgInfo::Expand:
12955627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      ExpandTypeToArgs(I->second, RV, Args);
12965627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      break;
129717b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar    }
129817b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  }
129917b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar
130017b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  llvm::CallInst *CI = Builder.CreateCall(Callee,&Args[0],&Args[0]+Args.size());
130117b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar
13025323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar  // FIXME: Provide TargetDecl so nounwind, noreturn, etc, etc get set.
1303761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel  CodeGen::AttributeListType AttributeList;
130488b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar  CGM.ConstructAttributeList(CallInfo, 0, AttributeList);
1305761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel  CI->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(),
1306725ad31086e3d6c41afa10c43db44f2e7060a961Daniel Dunbar                                           AttributeList.size()));
1307725ad31086e3d6c41afa10c43db44f2e7060a961Daniel Dunbar
130817b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee))
130917b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar    CI->setCallingConv(F->getCallingConv());
131017b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  if (CI->getType() != llvm::Type::VoidTy)
131117b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar    CI->setName("call");
13122c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar
13132c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  switch (RetAI.getKind()) {
13142c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  case ABIArgInfo::StructRet:
13152c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar    if (RetTy->isAnyComplexType())
13165627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
13173aea8cac3ec9ad03b1d2b002f38a0d42a70483c8Daniel Dunbar    else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
13185627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      return RValue::getAggregate(Args[0]);
13193aea8cac3ec9ad03b1d2b002f38a0d42a70483c8Daniel Dunbar    else
13203aea8cac3ec9ad03b1d2b002f38a0d42a70483c8Daniel Dunbar      return RValue::get(Builder.CreateLoad(Args[0]));
13218951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
13222c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  case ABIArgInfo::Default:
13232c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar    return RValue::get(RetTy->isVoidType() ? 0 : CI);
13242c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar
132511434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar  case ABIArgInfo::Ignore:
1326cc039fea2e985214e00387ce7404d358f56cf301Daniel Dunbar    if (RetTy->isVoidType())
1327cc039fea2e985214e00387ce7404d358f56cf301Daniel Dunbar      return RValue::get(0);
1328cc039fea2e985214e00387ce7404d358f56cf301Daniel Dunbar    if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1329cc039fea2e985214e00387ce7404d358f56cf301Daniel Dunbar      llvm::Value *Res =
1330cc039fea2e985214e00387ce7404d358f56cf301Daniel Dunbar        llvm::UndefValue::get(llvm::PointerType::getUnqual(ConvertType(RetTy)));
1331cc039fea2e985214e00387ce7404d358f56cf301Daniel Dunbar      return RValue::getAggregate(Res);
1332cc039fea2e985214e00387ce7404d358f56cf301Daniel Dunbar    }
1333cc039fea2e985214e00387ce7404d358f56cf301Daniel Dunbar    return RValue::get(llvm::UndefValue::get(ConvertType(RetTy)));
133411434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar
1335639ffe47097d01249b98b7acd102aaad6697b43aDaniel Dunbar  case ABIArgInfo::Coerce: {
133654d1ccbfcf19ddf39444f1b4018dd79487cc847bDaniel Dunbar    llvm::Value *V = CreateTempAlloca(ConvertType(RetTy), "coerce");
133754d1ccbfcf19ddf39444f1b4018dd79487cc847bDaniel Dunbar    CreateCoercedStore(CI, V, *this);
1338ad3d6917dabbdab3399ff8307240aad58247d2e3Anders Carlsson    if (RetTy->isAnyComplexType())
1339ad3d6917dabbdab3399ff8307240aad58247d2e3Anders Carlsson      return RValue::getComplex(LoadComplexFromAddr(V, false));
134011434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar    else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
1341ad3d6917dabbdab3399ff8307240aad58247d2e3Anders Carlsson      return RValue::getAggregate(V);
134211434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar    else
134311434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar      return RValue::get(Builder.CreateLoad(V));
1344639ffe47097d01249b98b7acd102aaad6697b43aDaniel Dunbar  }
13458951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
13468951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar  case ABIArgInfo::ByVal:
13478951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar  case ABIArgInfo::Expand:
13488951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    assert(0 && "Invalid ABI kind for return argument");
134917b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  }
13502c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar
13512c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  assert(0 && "Unhandled ABIArgInfo::Kind");
13522c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  return RValue::get(0);
135317b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar}
1354