blob: c63dab16a139fdf129a900bcb7c10517b3b2ea7b [file] [log] [blame]
Daniel Dunbar0dbe2272008-09-08 21:33:45 +00001//===----- CGCall.h - Encapsulate calling convention details ----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// These classes wrap the information about a call or function
11// definition used to handle ABI compliancy.
12//
13//===----------------------------------------------------------------------===//
14
15#include "CGCall.h"
16#include "CodeGenFunction.h"
Daniel Dunbarb7688072008-09-10 00:41:16 +000017#include "CodeGenModule.h"
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +000018#include "clang/Basic/TargetInfo.h"
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000019#include "clang/AST/Decl.h"
Anders Carlssonf6f8ae52009-04-03 22:48:58 +000020#include "clang/AST/DeclCXX.h"
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000021#include "clang/AST/DeclObjC.h"
Devang Patel24095da2009-06-04 23:32:02 +000022#include "clang/Frontend/CompileOptions.h"
Devang Pateld0646bd2008-09-24 01:01:36 +000023#include "llvm/Attributes.h"
Daniel Dunbard14151d2009-03-02 04:32:35 +000024#include "llvm/Support/CallSite.h"
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +000025#include "llvm/Target/TargetData.h"
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +000026
27#include "ABIInfo.h"
28
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000029using namespace clang;
30using namespace CodeGen;
31
32/***/
33
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000034// FIXME: Use iterator and sidestep silly type array creation.
35
Mike Stump1eb44332009-09-09 15:08:12 +000036const
Douglas Gregor72564e72009-02-26 23:50:07 +000037CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionNoProtoType *FTNP) {
Daniel Dunbarbac7c252009-09-11 22:24:53 +000038 // FIXME: Set calling convention correctly, it needs to be associated with the
39 // type somehow.
Mike Stump1eb44332009-09-09 15:08:12 +000040 return getFunctionInfo(FTNP->getResultType(),
Daniel Dunbarbac7c252009-09-11 22:24:53 +000041 llvm::SmallVector<QualType, 16>(), 0);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000042}
43
Mike Stump1eb44332009-09-09 15:08:12 +000044const
Douglas Gregor72564e72009-02-26 23:50:07 +000045CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionProtoType *FTP) {
Daniel Dunbar541b63b2009-02-02 23:23:47 +000046 llvm::SmallVector<QualType, 16> ArgTys;
47 // FIXME: Kill copy.
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000048 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +000049 ArgTys.push_back(FTP->getArgType(i));
Daniel Dunbarbac7c252009-09-11 22:24:53 +000050 // FIXME: Set calling convention correctly, it needs to be associated with the
51 // type somehow.
52 return getFunctionInfo(FTP->getResultType(), ArgTys, 0);
53}
54
55static unsigned getCallingConventionForDecl(const Decl *D) {
56 // Set the appropriate calling convention for the Function.
57 if (D->hasAttr<StdCallAttr>())
58 return llvm::CallingConv::X86_StdCall;
59
60 if (D->hasAttr<FastCallAttr>())
61 return llvm::CallingConv::X86_FastCall;
62
63 return llvm::CallingConv::C;
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000064}
65
Anders Carlssonf6f8ae52009-04-03 22:48:58 +000066const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXMethodDecl *MD) {
67 llvm::SmallVector<QualType, 16> ArgTys;
Chris Lattner3eb67ca2009-05-12 20:27:19 +000068 // Add the 'this' pointer unless this is a static method.
69 if (MD->isInstance())
70 ArgTys.push_back(MD->getThisType(Context));
Mike Stump1eb44332009-09-09 15:08:12 +000071
Anders Carlssonf6f8ae52009-04-03 22:48:58 +000072 const FunctionProtoType *FTP = MD->getType()->getAsFunctionProtoType();
73 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
74 ArgTys.push_back(FTP->getArgType(i));
Daniel Dunbarbac7c252009-09-11 22:24:53 +000075 return getFunctionInfo(FTP->getResultType(), ArgTys,
76 getCallingConventionForDecl(MD));
Anders Carlssonf6f8ae52009-04-03 22:48:58 +000077}
78
Daniel Dunbar541b63b2009-02-02 23:23:47 +000079const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionDecl *FD) {
Chris Lattner3eb67ca2009-05-12 20:27:19 +000080 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD))
Anders Carlssonf6f8ae52009-04-03 22:48:58 +000081 if (MD->isInstance())
82 return getFunctionInfo(MD);
Mike Stump1eb44332009-09-09 15:08:12 +000083
Daniel Dunbarbac7c252009-09-11 22:24:53 +000084 unsigned CallingConvention = getCallingConventionForDecl(FD);
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000085 const FunctionType *FTy = FD->getType()->getAsFunctionType();
Daniel Dunbarbac7c252009-09-11 22:24:53 +000086 if (const FunctionNoProtoType *FNTP = dyn_cast<FunctionNoProtoType>(FTy))
87 return getFunctionInfo(FNTP->getResultType(),
88 llvm::SmallVector<QualType, 16>(),
89 CallingConvention);
90
91 const FunctionProtoType *FPT = cast<FunctionProtoType>(FTy);
92 llvm::SmallVector<QualType, 16> ArgTys;
93 // FIXME: Kill copy.
94 for (unsigned i = 0, e = FPT->getNumArgs(); i != e; ++i)
95 ArgTys.push_back(FPT->getArgType(i));
96 return getFunctionInfo(FPT->getResultType(), ArgTys, CallingConvention);
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000097}
98
Daniel Dunbar541b63b2009-02-02 23:23:47 +000099const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const ObjCMethodDecl *MD) {
100 llvm::SmallVector<QualType, 16> ArgTys;
101 ArgTys.push_back(MD->getSelfDecl()->getType());
102 ArgTys.push_back(Context.getObjCSelType());
103 // FIXME: Kill copy?
Chris Lattner20732162009-02-20 06:23:21 +0000104 for (ObjCMethodDecl::param_iterator i = MD->param_begin(),
Daniel Dunbar0dbe2272008-09-08 21:33:45 +0000105 e = MD->param_end(); i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000106 ArgTys.push_back((*i)->getType());
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000107 return getFunctionInfo(MD->getResultType(), ArgTys,
108 getCallingConventionForDecl(MD));
Daniel Dunbar0dbe2272008-09-08 21:33:45 +0000109}
110
Mike Stump1eb44332009-09-09 15:08:12 +0000111const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000112 const CallArgList &Args,
113 unsigned CallingConvention){
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000114 // FIXME: Kill copy.
115 llvm::SmallVector<QualType, 16> ArgTys;
Mike Stump1eb44332009-09-09 15:08:12 +0000116 for (CallArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbar725ad312009-01-31 02:19:00 +0000117 i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000118 ArgTys.push_back(i->second);
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000119 return getFunctionInfo(ResTy, ArgTys, CallingConvention);
Daniel Dunbar725ad312009-01-31 02:19:00 +0000120}
121
Mike Stump1eb44332009-09-09 15:08:12 +0000122const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000123 const FunctionArgList &Args,
124 unsigned CallingConvention){
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000125 // FIXME: Kill copy.
126 llvm::SmallVector<QualType, 16> ArgTys;
Mike Stump1eb44332009-09-09 15:08:12 +0000127 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000128 i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000129 ArgTys.push_back(i->second);
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000130 return getFunctionInfo(ResTy, ArgTys, CallingConvention);
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000131}
132
133const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000134 const llvm::SmallVector<QualType, 16> &ArgTys,
135 unsigned CallingConvention){
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000136 // Lookup or create unique function info.
137 llvm::FoldingSetNodeID ID;
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000138 CGFunctionInfo::Profile(ID, CallingConvention, ResTy,
139 ArgTys.begin(), ArgTys.end());
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000140
141 void *InsertPos = 0;
142 CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, InsertPos);
143 if (FI)
144 return *FI;
145
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000146 // Construct the function info.
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000147 FI = new CGFunctionInfo(CallingConvention, ResTy, ArgTys);
Daniel Dunbar35e67d42009-02-05 00:00:23 +0000148 FunctionInfos.InsertNode(FI, InsertPos);
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000149
150 // Compute ABI information.
Owen Andersona1cf15f2009-07-14 23:10:40 +0000151 getABIInfo().computeInfo(*FI, getContext(), TheModule.getContext());
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000152
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000153 return *FI;
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000154}
155
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000156CGFunctionInfo::CGFunctionInfo(unsigned _CallingConvention,
157 QualType ResTy,
158 const llvm::SmallVector<QualType, 16> &ArgTys)
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000159 : CallingConvention(_CallingConvention),
160 EffectiveCallingConvention(_CallingConvention)
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000161{
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000162 NumArgs = ArgTys.size();
163 Args = new ArgInfo[1 + NumArgs];
164 Args[0].type = ResTy;
165 for (unsigned i = 0; i < NumArgs; ++i)
166 Args[1 + i].type = ArgTys[i];
167}
168
169/***/
170
Mike Stump1eb44332009-09-09 15:08:12 +0000171void CodeGenTypes::GetExpandedTypes(QualType Ty,
Daniel Dunbar56273772008-09-17 00:51:38 +0000172 std::vector<const llvm::Type*> &ArgTys) {
173 const RecordType *RT = Ty->getAsStructureType();
174 assert(RT && "Can only expand structure types.");
175 const RecordDecl *RD = RT->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000176 assert(!RD->hasFlexibleArrayMember() &&
Daniel Dunbar56273772008-09-17 00:51:38 +0000177 "Cannot expand structure with flexible array.");
Mike Stump1eb44332009-09-09 15:08:12 +0000178
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000179 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
180 i != e; ++i) {
Daniel Dunbar56273772008-09-17 00:51:38 +0000181 const FieldDecl *FD = *i;
Mike Stump1eb44332009-09-09 15:08:12 +0000182 assert(!FD->isBitField() &&
Daniel Dunbar56273772008-09-17 00:51:38 +0000183 "Cannot expand structure with bit-field members.");
Mike Stump1eb44332009-09-09 15:08:12 +0000184
Daniel Dunbar56273772008-09-17 00:51:38 +0000185 QualType FT = FD->getType();
186 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
187 GetExpandedTypes(FT, ArgTys);
188 } else {
189 ArgTys.push_back(ConvertType(FT));
190 }
191 }
192}
193
Mike Stump1eb44332009-09-09 15:08:12 +0000194llvm::Function::arg_iterator
Daniel Dunbar56273772008-09-17 00:51:38 +0000195CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
196 llvm::Function::arg_iterator AI) {
197 const RecordType *RT = Ty->getAsStructureType();
198 assert(RT && "Can only expand structure types.");
199
200 RecordDecl *RD = RT->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000201 assert(LV.isSimple() &&
202 "Unexpected non-simple lvalue during struct expansion.");
Daniel Dunbar56273772008-09-17 00:51:38 +0000203 llvm::Value *Addr = LV.getAddress();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000204 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
205 i != e; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +0000206 FieldDecl *FD = *i;
Daniel Dunbar56273772008-09-17 00:51:38 +0000207 QualType FT = FD->getType();
208
209 // FIXME: What are the right qualifiers here?
210 LValue LV = EmitLValueForField(Addr, FD, false, 0);
211 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
212 AI = ExpandTypeFromArgs(FT, LV, AI);
213 } else {
214 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
215 ++AI;
216 }
217 }
218
219 return AI;
220}
221
Mike Stump1eb44332009-09-09 15:08:12 +0000222void
223CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
Daniel Dunbar56273772008-09-17 00:51:38 +0000224 llvm::SmallVector<llvm::Value*, 16> &Args) {
225 const RecordType *RT = Ty->getAsStructureType();
226 assert(RT && "Can only expand structure types.");
227
228 RecordDecl *RD = RT->getDecl();
229 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
230 llvm::Value *Addr = RV.getAggregateAddr();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000231 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
232 i != e; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +0000233 FieldDecl *FD = *i;
Daniel Dunbar56273772008-09-17 00:51:38 +0000234 QualType FT = FD->getType();
Mike Stump1eb44332009-09-09 15:08:12 +0000235
Daniel Dunbar56273772008-09-17 00:51:38 +0000236 // FIXME: What are the right qualifiers here?
237 LValue LV = EmitLValueForField(Addr, FD, false, 0);
238 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
239 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
240 } else {
241 RValue RV = EmitLoadOfLValue(LV, FT);
Mike Stump1eb44332009-09-09 15:08:12 +0000242 assert(RV.isScalar() &&
Daniel Dunbar56273772008-09-17 00:51:38 +0000243 "Unexpected non-scalar rvalue during struct expansion.");
244 Args.push_back(RV.getScalarVal());
245 }
246 }
247}
248
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000249/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
250/// a pointer to an object of type \arg Ty.
251///
252/// This safely handles the case when the src type is smaller than the
253/// destination type; in this situation the values of bits which not
254/// present in the src are undefined.
255static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
256 const llvm::Type *Ty,
257 CodeGenFunction &CGF) {
Mike Stump1eb44332009-09-09 15:08:12 +0000258 const llvm::Type *SrcTy =
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000259 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
Duncan Sands9408c452009-05-09 07:08:47 +0000260 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
261 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(Ty);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000262
Daniel Dunbarb225be42009-02-03 05:59:18 +0000263 // If load is legal, just bitcast the src pointer.
Daniel Dunbar7ef455b2009-05-13 18:54:26 +0000264 if (SrcSize >= DstSize) {
Mike Stumpf5408fe2009-05-16 07:57:57 +0000265 // Generally SrcSize is never greater than DstSize, since this means we are
266 // losing bits. However, this can happen in cases where the structure has
267 // additional padding, for example due to a user specified alignment.
Daniel Dunbar7ef455b2009-05-13 18:54:26 +0000268 //
Mike Stumpf5408fe2009-05-16 07:57:57 +0000269 // FIXME: Assert that we aren't truncating non-padding bits when have access
270 // to that information.
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000271 llvm::Value *Casted =
272 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
Daniel Dunbar386621f2009-02-07 02:46:03 +0000273 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
274 // FIXME: Use better alignment / avoid requiring aligned load.
275 Load->setAlignment(1);
276 return Load;
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000277 } else {
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000278 // Otherwise do coercion through memory. This is stupid, but
279 // simple.
280 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000281 llvm::Value *Casted =
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000282 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000283 llvm::StoreInst *Store =
Daniel Dunbar386621f2009-02-07 02:46:03 +0000284 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
285 // FIXME: Use better alignment / avoid requiring aligned store.
286 Store->setAlignment(1);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000287 return CGF.Builder.CreateLoad(Tmp);
288 }
289}
290
291/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
292/// where the source and destination may have different types.
293///
294/// This safely handles the case when the src type is larger than the
295/// destination type; the upper bits of the src will be lost.
296static void CreateCoercedStore(llvm::Value *Src,
297 llvm::Value *DstPtr,
298 CodeGenFunction &CGF) {
299 const llvm::Type *SrcTy = Src->getType();
Mike Stump1eb44332009-09-09 15:08:12 +0000300 const llvm::Type *DstTy =
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000301 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
302
Duncan Sands9408c452009-05-09 07:08:47 +0000303 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
304 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(DstTy);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000305
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000306 // If store is legal, just bitcast the src pointer.
Daniel Dunbarfdf49862009-06-05 07:58:54 +0000307 if (SrcSize <= DstSize) {
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000308 llvm::Value *Casted =
309 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar386621f2009-02-07 02:46:03 +0000310 // FIXME: Use better alignment / avoid requiring aligned store.
311 CGF.Builder.CreateStore(Src, Casted)->setAlignment(1);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000312 } else {
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000313 // Otherwise do coercion through memory. This is stupid, but
314 // simple.
Daniel Dunbarfdf49862009-06-05 07:58:54 +0000315
316 // Generally SrcSize is never greater than DstSize, since this means we are
317 // losing bits. However, this can happen in cases where the structure has
318 // additional padding, for example due to a user specified alignment.
319 //
320 // FIXME: Assert that we aren't truncating non-padding bits when have access
321 // to that information.
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000322 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
323 CGF.Builder.CreateStore(Src, Tmp);
Mike Stump1eb44332009-09-09 15:08:12 +0000324 llvm::Value *Casted =
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000325 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
Daniel Dunbar386621f2009-02-07 02:46:03 +0000326 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
327 // FIXME: Use better alignment / avoid requiring aligned load.
328 Load->setAlignment(1);
329 CGF.Builder.CreateStore(Load, DstPtr);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000330 }
331}
332
Daniel Dunbar56273772008-09-17 00:51:38 +0000333/***/
334
Daniel Dunbar88b53962009-02-02 22:03:45 +0000335bool CodeGenModule::ReturnTypeUsesSret(const CGFunctionInfo &FI) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000336 return FI.getReturnInfo().isIndirect();
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000337}
338
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000339const llvm::FunctionType *
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000340CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic) {
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000341 std::vector<const llvm::Type*> ArgTys;
342
343 const llvm::Type *ResultType = 0;
344
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000345 QualType RetTy = FI.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000346 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000347 switch (RetAI.getKind()) {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000348 case ABIArgInfo::Expand:
349 assert(0 && "Invalid ABI kind for return argument");
350
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000351 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000352 case ABIArgInfo::Direct:
353 ResultType = ConvertType(RetTy);
354 break;
355
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000356 case ABIArgInfo::Indirect: {
357 assert(!RetAI.getIndirectAlign() && "Align unused on indirect return.");
Owen Anderson0032b272009-08-13 21:57:51 +0000358 ResultType = llvm::Type::getVoidTy(getLLVMContext());
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000359 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000360 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
361 break;
362 }
363
Daniel Dunbar11434922009-01-26 21:26:08 +0000364 case ABIArgInfo::Ignore:
Owen Anderson0032b272009-08-13 21:57:51 +0000365 ResultType = llvm::Type::getVoidTy(getLLVMContext());
Daniel Dunbar11434922009-01-26 21:26:08 +0000366 break;
367
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000368 case ABIArgInfo::Coerce:
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000369 ResultType = RetAI.getCoerceToType();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000370 break;
371 }
Mike Stump1eb44332009-09-09 15:08:12 +0000372
373 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000374 ie = FI.arg_end(); it != ie; ++it) {
375 const ABIArgInfo &AI = it->info;
Mike Stump1eb44332009-09-09 15:08:12 +0000376
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000377 switch (AI.getKind()) {
Daniel Dunbar11434922009-01-26 21:26:08 +0000378 case ABIArgInfo::Ignore:
379 break;
380
Daniel Dunbar56273772008-09-17 00:51:38 +0000381 case ABIArgInfo::Coerce:
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000382 ArgTys.push_back(AI.getCoerceToType());
383 break;
384
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000385 case ABIArgInfo::Indirect: {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000386 // indirect arguments are always on the stack, which is addr space #0.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000387 const llvm::Type *LTy = ConvertTypeForMem(it->type);
388 ArgTys.push_back(llvm::PointerType::getUnqual(LTy));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000389 break;
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000390 }
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000391
392 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000393 case ABIArgInfo::Direct:
Daniel Dunbar1f745982009-02-05 09:16:39 +0000394 ArgTys.push_back(ConvertType(it->type));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000395 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000396
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000397 case ABIArgInfo::Expand:
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000398 GetExpandedTypes(it->type, ArgTys);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000399 break;
400 }
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000401 }
402
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000403 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar3913f182008-09-09 23:48:28 +0000404}
405
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000406void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
Daniel Dunbar88b53962009-02-02 22:03:45 +0000407 const Decl *TargetDecl,
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000408 AttributeListType &PAL,
409 unsigned &CallingConv) {
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000410 unsigned FuncAttrs = 0;
Devang Patela2c69122008-09-26 22:53:57 +0000411 unsigned RetAttrs = 0;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000412
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000413 CallingConv = FI.getEffectiveCallingConvention();
414
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000415 // FIXME: handle sseregparm someday...
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000416 if (TargetDecl) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000417 if (TargetDecl->hasAttr<NoThrowAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +0000418 FuncAttrs |= llvm::Attribute::NoUnwind;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000419 if (TargetDecl->hasAttr<NoReturnAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +0000420 FuncAttrs |= llvm::Attribute::NoReturn;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000421 if (TargetDecl->hasAttr<ConstAttr>())
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000422 FuncAttrs |= llvm::Attribute::ReadNone;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000423 else if (TargetDecl->hasAttr<PureAttr>())
Daniel Dunbar64c2e072009-04-10 22:14:52 +0000424 FuncAttrs |= llvm::Attribute::ReadOnly;
Ryan Flynn76168e22009-08-09 20:07:29 +0000425 if (TargetDecl->hasAttr<MallocAttr>())
426 RetAttrs |= llvm::Attribute::NoAlias;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000427 }
428
Devang Patel24095da2009-06-04 23:32:02 +0000429 if (CompileOpts.DisableRedZone)
430 FuncAttrs |= llvm::Attribute::NoRedZone;
Devang Patelacebb392009-06-05 22:05:48 +0000431 if (CompileOpts.NoImplicitFloat)
432 FuncAttrs |= llvm::Attribute::NoImplicitFloat;
Devang Patel24095da2009-06-04 23:32:02 +0000433
Bill Wendling4ebe3e42009-06-28 23:01:01 +0000434 if (Features.getStackProtectorMode() == LangOptions::SSPOn)
Bill Wendling45483f72009-06-28 07:36:13 +0000435 FuncAttrs |= llvm::Attribute::StackProtect;
Bill Wendling4ebe3e42009-06-28 23:01:01 +0000436 else if (Features.getStackProtectorMode() == LangOptions::SSPReq)
Bill Wendling45483f72009-06-28 07:36:13 +0000437 FuncAttrs |= llvm::Attribute::StackProtectReq;
438
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000439 QualType RetTy = FI.getReturnType();
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000440 unsigned Index = 1;
Daniel Dunbarb225be42009-02-03 05:59:18 +0000441 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000442 switch (RetAI.getKind()) {
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000443 case ABIArgInfo::Extend:
444 if (RetTy->isSignedIntegerType()) {
445 RetAttrs |= llvm::Attribute::SExt;
446 } else if (RetTy->isUnsignedIntegerType()) {
447 RetAttrs |= llvm::Attribute::ZExt;
448 }
449 // FALLTHROUGH
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000450 case ABIArgInfo::Direct:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000451 break;
452
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000453 case ABIArgInfo::Indirect:
Mike Stump1eb44332009-09-09 15:08:12 +0000454 PAL.push_back(llvm::AttributeWithIndex::get(Index,
Daniel Dunbar725ad312009-01-31 02:19:00 +0000455 llvm::Attribute::StructRet |
456 llvm::Attribute::NoAlias));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000457 ++Index;
Daniel Dunbar0ac86f02009-03-18 19:51:01 +0000458 // sret disables readnone and readonly
459 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
460 llvm::Attribute::ReadNone);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000461 break;
462
Daniel Dunbar11434922009-01-26 21:26:08 +0000463 case ABIArgInfo::Ignore:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000464 case ABIArgInfo::Coerce:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000465 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000466
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000467 case ABIArgInfo::Expand:
Mike Stump1eb44332009-09-09 15:08:12 +0000468 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000469 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000470
Devang Patela2c69122008-09-26 22:53:57 +0000471 if (RetAttrs)
472 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000473
474 // FIXME: we need to honour command line settings also...
475 // FIXME: RegParm should be reduced in case of nested functions and/or global
476 // register variable.
477 signed RegParm = 0;
478 if (TargetDecl)
Mike Stump1eb44332009-09-09 15:08:12 +0000479 if (const RegparmAttr *RegParmAttr
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000480 = TargetDecl->getAttr<RegparmAttr>())
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000481 RegParm = RegParmAttr->getNumParams();
482
483 unsigned PointerWidth = getContext().Target.getPointerWidth(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000484 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000485 ie = FI.arg_end(); it != ie; ++it) {
486 QualType ParamType = it->type;
487 const ABIArgInfo &AI = it->info;
Devang Patel761d7f72008-09-25 21:02:23 +0000488 unsigned Attributes = 0;
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000489
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000490 switch (AI.getKind()) {
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000491 case ABIArgInfo::Coerce:
492 break;
493
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000494 case ABIArgInfo::Indirect:
Devang Patel761d7f72008-09-25 21:02:23 +0000495 Attributes |= llvm::Attribute::ByVal;
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000496 Attributes |=
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000497 llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign());
Daniel Dunbar0ac86f02009-03-18 19:51:01 +0000498 // byval disables readnone and readonly.
499 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
500 llvm::Attribute::ReadNone);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000501 break;
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000502
503 case ABIArgInfo::Extend:
504 if (ParamType->isSignedIntegerType()) {
505 Attributes |= llvm::Attribute::SExt;
506 } else if (ParamType->isUnsignedIntegerType()) {
507 Attributes |= llvm::Attribute::ZExt;
508 }
509 // FALLS THROUGH
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000510 case ABIArgInfo::Direct:
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000511 if (RegParm > 0 &&
512 (ParamType->isIntegerType() || ParamType->isPointerType())) {
513 RegParm -=
514 (Context.getTypeSize(ParamType) + PointerWidth - 1) / PointerWidth;
515 if (RegParm >= 0)
516 Attributes |= llvm::Attribute::InReg;
517 }
518 // FIXME: handle sseregparm someday...
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000519 break;
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000520
Daniel Dunbar11434922009-01-26 21:26:08 +0000521 case ABIArgInfo::Ignore:
522 // Skip increment, no matching LLVM parameter.
Mike Stump1eb44332009-09-09 15:08:12 +0000523 continue;
Daniel Dunbar11434922009-01-26 21:26:08 +0000524
Daniel Dunbar56273772008-09-17 00:51:38 +0000525 case ABIArgInfo::Expand: {
Mike Stump1eb44332009-09-09 15:08:12 +0000526 std::vector<const llvm::Type*> Tys;
Mike Stumpf5408fe2009-05-16 07:57:57 +0000527 // FIXME: This is rather inefficient. Do we ever actually need to do
528 // anything here? The result should be just reconstructed on the other
529 // side, so extension should be a non-issue.
Daniel Dunbar56273772008-09-17 00:51:38 +0000530 getTypes().GetExpandedTypes(ParamType, Tys);
531 Index += Tys.size();
532 continue;
533 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000534 }
Mike Stump1eb44332009-09-09 15:08:12 +0000535
Devang Patel761d7f72008-09-25 21:02:23 +0000536 if (Attributes)
537 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar56273772008-09-17 00:51:38 +0000538 ++Index;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000539 }
Devang Patela2c69122008-09-26 22:53:57 +0000540 if (FuncAttrs)
541 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000542}
543
Daniel Dunbar88b53962009-02-02 22:03:45 +0000544void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
545 llvm::Function *Fn,
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000546 const FunctionArgList &Args) {
John McCall0cfeb632009-07-28 01:00:58 +0000547 // If this is an implicit-return-zero function, go ahead and
548 // initialize the return value. TODO: it might be nice to have
549 // a more general mechanism for this that didn't require synthesized
550 // return statements.
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000551 if (const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(CurFuncDecl)) {
John McCall0cfeb632009-07-28 01:00:58 +0000552 if (FD->hasImplicitReturnZero()) {
553 QualType RetTy = FD->getResultType().getUnqualifiedType();
554 const llvm::Type* LLVMTy = CGM.getTypes().ConvertType(RetTy);
Owen Andersonc9c88b42009-07-31 20:28:54 +0000555 llvm::Constant* Zero = llvm::Constant::getNullValue(LLVMTy);
John McCall0cfeb632009-07-28 01:00:58 +0000556 Builder.CreateStore(Zero, ReturnValue);
557 }
558 }
559
Mike Stumpf5408fe2009-05-16 07:57:57 +0000560 // FIXME: We no longer need the types from FunctionArgList; lift up and
561 // simplify.
Daniel Dunbar5251afa2009-02-03 06:02:10 +0000562
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000563 // Emit allocs for param decls. Give the LLVM Argument nodes names.
564 llvm::Function::arg_iterator AI = Fn->arg_begin();
Mike Stump1eb44332009-09-09 15:08:12 +0000565
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000566 // Name the struct return argument.
Daniel Dunbar88b53962009-02-02 22:03:45 +0000567 if (CGM.ReturnTypeUsesSret(FI)) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000568 AI->setName("agg.result");
569 ++AI;
570 }
Mike Stump1eb44332009-09-09 15:08:12 +0000571
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +0000572 assert(FI.arg_size() == Args.size() &&
573 "Mismatch between function signature & arguments.");
Daniel Dunbarb225be42009-02-03 05:59:18 +0000574 CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000575 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000576 i != e; ++i, ++info_it) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000577 const VarDecl *Arg = i->first;
Daniel Dunbarb225be42009-02-03 05:59:18 +0000578 QualType Ty = info_it->type;
579 const ABIArgInfo &ArgI = info_it->info;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000580
581 switch (ArgI.getKind()) {
Daniel Dunbar1f745982009-02-05 09:16:39 +0000582 case ABIArgInfo::Indirect: {
583 llvm::Value* V = AI;
584 if (hasAggregateLLVMType(Ty)) {
585 // Do nothing, aggregates and complex variables are accessed by
586 // reference.
587 } else {
588 // Load scalar value from indirect argument.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000589 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar1f745982009-02-05 09:16:39 +0000590 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
591 // This must be a promotion, for something like
592 // "void a(x) short x; {..."
593 V = EmitScalarConversion(V, Ty, Arg->getType());
594 }
595 }
Mike Stump1eb44332009-09-09 15:08:12 +0000596 EmitParmDecl(*Arg, V);
Daniel Dunbar1f745982009-02-05 09:16:39 +0000597 break;
598 }
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000599
600 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000601 case ABIArgInfo::Direct: {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000602 assert(AI != Fn->arg_end() && "Argument mismatch!");
603 llvm::Value* V = AI;
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000604 if (hasAggregateLLVMType(Ty)) {
605 // Create a temporary alloca to hold the argument; the rest of
606 // codegen expects to access aggregates & complex values by
607 // reference.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000608 V = CreateTempAlloca(ConvertTypeForMem(Ty));
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000609 Builder.CreateStore(AI, V);
610 } else {
611 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
612 // This must be a promotion, for something like
613 // "void a(x) short x; {..."
614 V = EmitScalarConversion(V, Ty, Arg->getType());
615 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000616 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000617 EmitParmDecl(*Arg, V);
618 break;
619 }
Mike Stump1eb44332009-09-09 15:08:12 +0000620
Daniel Dunbar56273772008-09-17 00:51:38 +0000621 case ABIArgInfo::Expand: {
Daniel Dunbarb225be42009-02-03 05:59:18 +0000622 // If this structure was expanded into multiple arguments then
Daniel Dunbar56273772008-09-17 00:51:38 +0000623 // we need to create a temporary and reconstruct it from the
624 // arguments.
Chris Lattner39f34e92008-11-24 04:00:27 +0000625 std::string Name = Arg->getNameAsString();
Mike Stump1eb44332009-09-09 15:08:12 +0000626 llvm::Value *Temp = CreateTempAlloca(ConvertTypeForMem(Ty),
Daniel Dunbar56273772008-09-17 00:51:38 +0000627 (Name + ".addr").c_str());
628 // FIXME: What are the right qualifiers here?
Mike Stump1eb44332009-09-09 15:08:12 +0000629 llvm::Function::arg_iterator End =
630 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI);
Daniel Dunbar56273772008-09-17 00:51:38 +0000631 EmitParmDecl(*Arg, Temp);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000632
Daniel Dunbar56273772008-09-17 00:51:38 +0000633 // Name the arguments used in expansion and increment AI.
634 unsigned Index = 0;
635 for (; AI != End; ++AI, ++Index)
Daniel Dunbar0ab9a252009-08-02 01:43:57 +0000636 AI->setName(Name + "." + llvm::Twine(Index));
Daniel Dunbar56273772008-09-17 00:51:38 +0000637 continue;
638 }
Daniel Dunbar11434922009-01-26 21:26:08 +0000639
640 case ABIArgInfo::Ignore:
Daniel Dunbar8b979d92009-02-10 00:06:49 +0000641 // Initialize the local variable appropriately.
Mike Stump1eb44332009-09-09 15:08:12 +0000642 if (hasAggregateLLVMType(Ty)) {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000643 EmitParmDecl(*Arg, CreateTempAlloca(ConvertTypeForMem(Ty)));
Daniel Dunbar8b979d92009-02-10 00:06:49 +0000644 } else {
645 EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())));
646 }
Mike Stump1eb44332009-09-09 15:08:12 +0000647
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000648 // Skip increment, no matching LLVM parameter.
Mike Stump1eb44332009-09-09 15:08:12 +0000649 continue;
Daniel Dunbar11434922009-01-26 21:26:08 +0000650
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000651 case ABIArgInfo::Coerce: {
652 assert(AI != Fn->arg_end() && "Argument mismatch!");
Mike Stumpf5408fe2009-05-16 07:57:57 +0000653 // FIXME: This is very wasteful; EmitParmDecl is just going to drop the
654 // result in a new alloca anyway, so we could just store into that
655 // directly if we broke the abstraction down more.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000656 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(Ty), "coerce");
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000657 CreateCoercedStore(AI, V, *this);
658 // Match to what EmitParmDecl is expecting for this type.
Daniel Dunbar8b29a382009-02-04 07:22:24 +0000659 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000660 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar8b29a382009-02-04 07:22:24 +0000661 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
662 // This must be a promotion, for something like
663 // "void a(x) short x; {..."
664 V = EmitScalarConversion(V, Ty, Arg->getType());
665 }
666 }
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000667 EmitParmDecl(*Arg, V);
668 break;
669 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000670 }
Daniel Dunbar56273772008-09-17 00:51:38 +0000671
672 ++AI;
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000673 }
674 assert(AI == Fn->arg_end() && "Argument mismatch!");
675}
676
Daniel Dunbar88b53962009-02-02 22:03:45 +0000677void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI,
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000678 llvm::Value *ReturnValue) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000679 llvm::Value *RV = 0;
680
681 // Functions with no result always return void.
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000682 if (ReturnValue) {
Daniel Dunbar88b53962009-02-02 22:03:45 +0000683 QualType RetTy = FI.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000684 const ABIArgInfo &RetAI = FI.getReturnInfo();
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000685
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000686 switch (RetAI.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000687 case ABIArgInfo::Indirect:
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +0000688 if (RetTy->isAnyComplexType()) {
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +0000689 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
690 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
691 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
692 EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy);
693 } else {
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000694 EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(),
Anders Carlssonb4aa4662009-05-19 18:50:41 +0000695 false, RetTy);
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +0000696 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000697 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000698
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000699 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000700 case ABIArgInfo::Direct:
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000701 // The internal return value temp always will have
702 // pointer-to-return-type type.
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000703 RV = Builder.CreateLoad(ReturnValue);
704 break;
705
Daniel Dunbar11434922009-01-26 21:26:08 +0000706 case ABIArgInfo::Ignore:
707 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000708
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000709 case ABIArgInfo::Coerce:
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +0000710 RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000711 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000712
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000713 case ABIArgInfo::Expand:
Mike Stump1eb44332009-09-09 15:08:12 +0000714 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000715 }
716 }
Mike Stump1eb44332009-09-09 15:08:12 +0000717
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000718 if (RV) {
719 Builder.CreateRet(RV);
720 } else {
721 Builder.CreateRetVoid();
722 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000723}
724
Anders Carlsson0139bb92009-04-08 20:47:54 +0000725RValue CodeGenFunction::EmitCallArg(const Expr *E, QualType ArgType) {
Anders Carlsson4029ca72009-05-20 00:24:07 +0000726 if (ArgType->isReferenceType())
727 return EmitReferenceBindingToExpr(E, ArgType);
Mike Stump1eb44332009-09-09 15:08:12 +0000728
Anders Carlsson0139bb92009-04-08 20:47:54 +0000729 return EmitAnyExprToTemp(E);
730}
731
Daniel Dunbar88b53962009-02-02 22:03:45 +0000732RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
Mike Stump1eb44332009-09-09 15:08:12 +0000733 llvm::Value *Callee,
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +0000734 const CallArgList &CallArgs,
735 const Decl *TargetDecl) {
Mike Stumpf5408fe2009-05-16 07:57:57 +0000736 // FIXME: We no longer need the types from CallArgs; lift up and simplify.
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000737 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000738
739 // Handle struct-return functions by passing a pointer to the
740 // location that we would like to return into.
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000741 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000742 const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000743
744
Chris Lattner5db7ae52009-06-13 00:26:38 +0000745 // If the call returns a temporary with struct return, create a temporary
746 // alloca to hold the result.
747 if (CGM.ReturnTypeUsesSret(CallInfo))
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000748 Args.push_back(CreateTempAlloca(ConvertTypeForMem(RetTy)));
Mike Stump1eb44332009-09-09 15:08:12 +0000749
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +0000750 assert(CallInfo.arg_size() == CallArgs.size() &&
751 "Mismatch between function signature & arguments.");
Daniel Dunbarb225be42009-02-03 05:59:18 +0000752 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
Mike Stump1eb44332009-09-09 15:08:12 +0000753 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000754 I != E; ++I, ++info_it) {
755 const ABIArgInfo &ArgInfo = info_it->info;
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000756 RValue RV = I->first;
Daniel Dunbar56273772008-09-17 00:51:38 +0000757
758 switch (ArgInfo.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000759 case ABIArgInfo::Indirect:
Daniel Dunbar1f745982009-02-05 09:16:39 +0000760 if (RV.isScalar() || RV.isComplex()) {
761 // Make a temporary alloca to pass the argument.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000762 Args.push_back(CreateTempAlloca(ConvertTypeForMem(I->second)));
Daniel Dunbar1f745982009-02-05 09:16:39 +0000763 if (RV.isScalar())
Anders Carlssonb4aa4662009-05-19 18:50:41 +0000764 EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false, I->second);
Daniel Dunbar1f745982009-02-05 09:16:39 +0000765 else
Mike Stump1eb44332009-09-09 15:08:12 +0000766 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
Daniel Dunbar1f745982009-02-05 09:16:39 +0000767 } else {
768 Args.push_back(RV.getAggregateAddr());
769 }
770 break;
771
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000772 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000773 case ABIArgInfo::Direct:
Daniel Dunbar56273772008-09-17 00:51:38 +0000774 if (RV.isScalar()) {
775 Args.push_back(RV.getScalarVal());
776 } else if (RV.isComplex()) {
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000777 llvm::Value *Tmp = llvm::UndefValue::get(ConvertType(I->second));
778 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().first, 0);
779 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().second, 1);
780 Args.push_back(Tmp);
Daniel Dunbar56273772008-09-17 00:51:38 +0000781 } else {
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000782 Args.push_back(Builder.CreateLoad(RV.getAggregateAddr()));
Daniel Dunbar56273772008-09-17 00:51:38 +0000783 }
784 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000785
Daniel Dunbar11434922009-01-26 21:26:08 +0000786 case ABIArgInfo::Ignore:
787 break;
788
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000789 case ABIArgInfo::Coerce: {
790 // FIXME: Avoid the conversion through memory if possible.
791 llvm::Value *SrcPtr;
792 if (RV.isScalar()) {
Daniel Dunbar5a1be6e2009-02-03 23:04:57 +0000793 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
Anders Carlssonb4aa4662009-05-19 18:50:41 +0000794 EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false, I->second);
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000795 } else if (RV.isComplex()) {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000796 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000797 StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000798 } else
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000799 SrcPtr = RV.getAggregateAddr();
Mike Stump1eb44332009-09-09 15:08:12 +0000800 Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000801 *this));
802 break;
803 }
804
Daniel Dunbar56273772008-09-17 00:51:38 +0000805 case ABIArgInfo::Expand:
806 ExpandTypeToArgs(I->second, RV, Args);
807 break;
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000808 }
809 }
Mike Stump1eb44332009-09-09 15:08:12 +0000810
Chris Lattner5db7ae52009-06-13 00:26:38 +0000811 // If the callee is a bitcast of a function to a varargs pointer to function
812 // type, check to see if we can remove the bitcast. This handles some cases
813 // with unprototyped functions.
814 if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Callee))
815 if (llvm::Function *CalleeF = dyn_cast<llvm::Function>(CE->getOperand(0))) {
816 const llvm::PointerType *CurPT=cast<llvm::PointerType>(Callee->getType());
817 const llvm::FunctionType *CurFT =
818 cast<llvm::FunctionType>(CurPT->getElementType());
819 const llvm::FunctionType *ActualFT = CalleeF->getFunctionType();
Mike Stump1eb44332009-09-09 15:08:12 +0000820
Chris Lattner5db7ae52009-06-13 00:26:38 +0000821 if (CE->getOpcode() == llvm::Instruction::BitCast &&
822 ActualFT->getReturnType() == CurFT->getReturnType() &&
Chris Lattnerd6bebbf2009-06-23 01:38:41 +0000823 ActualFT->getNumParams() == CurFT->getNumParams() &&
824 ActualFT->getNumParams() == Args.size()) {
Chris Lattner5db7ae52009-06-13 00:26:38 +0000825 bool ArgsMatch = true;
826 for (unsigned i = 0, e = ActualFT->getNumParams(); i != e; ++i)
827 if (ActualFT->getParamType(i) != CurFT->getParamType(i)) {
828 ArgsMatch = false;
829 break;
830 }
Mike Stump1eb44332009-09-09 15:08:12 +0000831
Chris Lattner5db7ae52009-06-13 00:26:38 +0000832 // Strip the cast if we can get away with it. This is a nice cleanup,
833 // but also allows us to inline the function at -O0 if it is marked
834 // always_inline.
835 if (ArgsMatch)
836 Callee = CalleeF;
837 }
838 }
Mike Stump1eb44332009-09-09 15:08:12 +0000839
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000840
Daniel Dunbar9834ffb2009-02-23 17:26:39 +0000841 llvm::BasicBlock *InvokeDest = getInvokeDest();
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000842 unsigned CallingConv;
Devang Patel761d7f72008-09-25 21:02:23 +0000843 CodeGen::AttributeListType AttributeList;
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000844 CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList, CallingConv);
Daniel Dunbar9834ffb2009-02-23 17:26:39 +0000845 llvm::AttrListPtr Attrs = llvm::AttrListPtr::get(AttributeList.begin(),
846 AttributeList.end());
Mike Stump1eb44332009-09-09 15:08:12 +0000847
Daniel Dunbard14151d2009-03-02 04:32:35 +0000848 llvm::CallSite CS;
849 if (!InvokeDest || (Attrs.getFnAttributes() & llvm::Attribute::NoUnwind)) {
Jay Foadbeaaccd2009-05-21 09:52:38 +0000850 CS = Builder.CreateCall(Callee, Args.data(), Args.data()+Args.size());
Daniel Dunbar9834ffb2009-02-23 17:26:39 +0000851 } else {
852 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Mike Stump1eb44332009-09-09 15:08:12 +0000853 CS = Builder.CreateInvoke(Callee, Cont, InvokeDest,
Jay Foadbeaaccd2009-05-21 09:52:38 +0000854 Args.data(), Args.data()+Args.size());
Daniel Dunbar9834ffb2009-02-23 17:26:39 +0000855 EmitBlock(Cont);
Daniel Dunbarf4fe0f02009-02-20 18:54:31 +0000856 }
857
Daniel Dunbard14151d2009-03-02 04:32:35 +0000858 CS.setAttributes(Attrs);
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000859 CS.setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
Daniel Dunbard14151d2009-03-02 04:32:35 +0000860
861 // If the call doesn't return, finish the basic block and clear the
862 // insertion point; this allows the rest of IRgen to discard
863 // unreachable code.
864 if (CS.doesNotReturn()) {
865 Builder.CreateUnreachable();
866 Builder.ClearInsertionPoint();
Mike Stump1eb44332009-09-09 15:08:12 +0000867
Mike Stumpf5408fe2009-05-16 07:57:57 +0000868 // FIXME: For now, emit a dummy basic block because expr emitters in
869 // generally are not ready to handle emitting expressions at unreachable
870 // points.
Daniel Dunbard14151d2009-03-02 04:32:35 +0000871 EnsureInsertPoint();
Mike Stump1eb44332009-09-09 15:08:12 +0000872
Daniel Dunbard14151d2009-03-02 04:32:35 +0000873 // Return a reasonable RValue.
874 return GetUndefRValue(RetTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000875 }
Daniel Dunbard14151d2009-03-02 04:32:35 +0000876
877 llvm::Instruction *CI = CS.getInstruction();
Owen Anderson0032b272009-08-13 21:57:51 +0000878 if (Builder.isNamePreserving() &&
879 CI->getType() != llvm::Type::getVoidTy(VMContext))
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000880 CI->setName("call");
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000881
882 switch (RetAI.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000883 case ABIArgInfo::Indirect:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000884 if (RetTy->isAnyComplexType())
Daniel Dunbar56273772008-09-17 00:51:38 +0000885 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Chris Lattner34030842009-03-22 00:32:22 +0000886 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar56273772008-09-17 00:51:38 +0000887 return RValue::getAggregate(Args[0]);
Chris Lattner34030842009-03-22 00:32:22 +0000888 return RValue::get(EmitLoadOfScalar(Args[0], false, RetTy));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000889
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000890 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000891 case ABIArgInfo::Direct:
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000892 if (RetTy->isAnyComplexType()) {
893 llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
894 llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
895 return RValue::getComplex(std::make_pair(Real, Imag));
Chris Lattner34030842009-03-22 00:32:22 +0000896 }
897 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000898 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(RetTy), "agg.tmp");
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000899 Builder.CreateStore(CI, V);
900 return RValue::getAggregate(V);
Chris Lattner34030842009-03-22 00:32:22 +0000901 }
902 return RValue::get(CI);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000903
Daniel Dunbar11434922009-01-26 21:26:08 +0000904 case ABIArgInfo::Ignore:
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000905 // If we are ignoring an argument that had a result, make sure to
906 // construct the appropriate return value for our caller.
Daniel Dunbar13e81732009-02-05 07:09:07 +0000907 return GetUndefRValue(RetTy);
Daniel Dunbar11434922009-01-26 21:26:08 +0000908
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000909 case ABIArgInfo::Coerce: {
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000910 // FIXME: Avoid the conversion through memory if possible.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000911 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(RetTy), "coerce");
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +0000912 CreateCoercedStore(CI, V, *this);
Anders Carlssonad3d6912008-11-25 22:21:48 +0000913 if (RetTy->isAnyComplexType())
914 return RValue::getComplex(LoadComplexFromAddr(V, false));
Chris Lattner34030842009-03-22 00:32:22 +0000915 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssonad3d6912008-11-25 22:21:48 +0000916 return RValue::getAggregate(V);
Chris Lattner34030842009-03-22 00:32:22 +0000917 return RValue::get(EmitLoadOfScalar(V, false, RetTy));
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000918 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000919
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000920 case ABIArgInfo::Expand:
Mike Stump1eb44332009-09-09 15:08:12 +0000921 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000922 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000923
924 assert(0 && "Unhandled ABIArgInfo::Kind");
925 return RValue::get(0);
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000926}
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000927
928/* VarArg handling */
929
930llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) {
931 return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this);
932}