blob: 13b9469ba3fdd30c1c2e10c49ae18365ff9225f0 [file] [log] [blame]
Daniel Dunbara8f02052008-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 Dunbar3ef2e852008-09-10 00:41:16 +000017#include "CodeGenModule.h"
Daniel Dunbara8f02052008-09-08 21:33:45 +000018#include "clang/AST/ASTContext.h"
19#include "clang/AST/Decl.h"
20#include "clang/AST/DeclObjC.h"
Daniel Dunbar04d35782008-09-17 00:51:38 +000021#include "llvm/ADT/StringExtras.h"
Daniel Dunbara8f02052008-09-08 21:33:45 +000022#include "llvm/ParameterAttributes.h"
23using namespace clang;
24using namespace CodeGen;
25
26/***/
27
Daniel Dunbara8f02052008-09-08 21:33:45 +000028// FIXME: Use iterator and sidestep silly type array creation.
29
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000030CGFunctionInfo::CGFunctionInfo(const FunctionTypeNoProto *FTNP)
31 : IsVariadic(true)
32{
33 ArgTypes.push_back(FTNP->getResultType());
34}
35
36CGFunctionInfo::CGFunctionInfo(const FunctionTypeProto *FTP)
37 : IsVariadic(FTP->isVariadic())
38{
39 ArgTypes.push_back(FTP->getResultType());
40 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
41 ArgTypes.push_back(FTP->getArgType(i));
42}
43
44// FIXME: Is there really any reason to have this still?
Daniel Dunbara8f02052008-09-08 21:33:45 +000045CGFunctionInfo::CGFunctionInfo(const FunctionDecl *FD)
Daniel Dunbara8f02052008-09-08 21:33:45 +000046{
47 const FunctionType *FTy = FD->getType()->getAsFunctionType();
48 const FunctionTypeProto *FTP = dyn_cast<FunctionTypeProto>(FTy);
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000049
Daniel Dunbara8f02052008-09-08 21:33:45 +000050 ArgTypes.push_back(FTy->getResultType());
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000051 if (FTP) {
52 IsVariadic = FTP->isVariadic();
Daniel Dunbara8f02052008-09-08 21:33:45 +000053 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
54 ArgTypes.push_back(FTP->getArgType(i));
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000055 } else {
56 IsVariadic = true;
57 }
Daniel Dunbara8f02052008-09-08 21:33:45 +000058}
59
60CGFunctionInfo::CGFunctionInfo(const ObjCMethodDecl *MD,
61 const ASTContext &Context)
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000062 : IsVariadic(MD->isVariadic())
Daniel Dunbara8f02052008-09-08 21:33:45 +000063{
64 ArgTypes.push_back(MD->getResultType());
65 ArgTypes.push_back(MD->getSelfDecl()->getType());
66 ArgTypes.push_back(Context.getObjCSelType());
67 for (ObjCMethodDecl::param_const_iterator i = MD->param_begin(),
68 e = MD->param_end(); i != e; ++i)
69 ArgTypes.push_back((*i)->getType());
70}
71
Daniel Dunbarbccb0682008-09-10 00:32:18 +000072ArgTypeIterator CGFunctionInfo::argtypes_begin() const {
73 return ArgTypes.begin();
74}
75
76ArgTypeIterator CGFunctionInfo::argtypes_end() const {
77 return ArgTypes.end();
Daniel Dunbara8f02052008-09-08 21:33:45 +000078}
79
80/***/
81
Daniel Dunbarbccb0682008-09-10 00:32:18 +000082CGCallInfo::CGCallInfo(QualType _ResultType, const CallArgList &_Args) {
83 ArgTypes.push_back(_ResultType);
84 for (CallArgList::const_iterator i = _Args.begin(), e = _Args.end(); i!=e; ++i)
Daniel Dunbara8f02052008-09-08 21:33:45 +000085 ArgTypes.push_back(i->second);
86}
87
Daniel Dunbarbccb0682008-09-10 00:32:18 +000088ArgTypeIterator CGCallInfo::argtypes_begin() const {
89 return ArgTypes.begin();
90}
91
92ArgTypeIterator CGCallInfo::argtypes_end() const {
93 return ArgTypes.end();
Daniel Dunbara8f02052008-09-08 21:33:45 +000094}
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +000095
96/***/
97
Daniel Dunbar22e30052008-09-11 01:48:57 +000098/// ABIArgInfo - Helper class to encapsulate information about how a
99/// specific C type should be passed to or returned from a function.
Daniel Dunbare126ab12008-09-10 02:41:04 +0000100class ABIArgInfo {
101public:
102 enum Kind {
103 Default,
Daniel Dunbar22e30052008-09-11 01:48:57 +0000104 StructRet, /// Only valid for aggregate return types.
105
Daniel Dunbar04d35782008-09-17 00:51:38 +0000106 Coerce, /// Only valid for aggregate return types, the argument
107 /// should be accessed by coercion to a provided type.
Daniel Dunbar22e30052008-09-11 01:48:57 +0000108
109 ByVal, /// Only valid for aggregate argument types. The
110 /// structure should be passed "byval" with the
111 /// specified alignment (0 indicates default
112 /// alignment).
113
114 Expand, /// Only valid for aggregate argument types. The
115 /// structure should be expanded into consecutive
Daniel Dunbar04d35782008-09-17 00:51:38 +0000116 /// arguments for its constituent fields. Currently
117 /// expand is only allowed on structures whose fields
118 /// are all scalar types or are themselves expandable
119 /// types.
Daniel Dunbar22e30052008-09-11 01:48:57 +0000120
121 KindFirst=Default, KindLast=Expand
Daniel Dunbare126ab12008-09-10 02:41:04 +0000122 };
123
124private:
125 Kind TheKind;
Daniel Dunbar73d66602008-09-10 07:04:09 +0000126 const llvm::Type *TypeData;
Daniel Dunbar22e30052008-09-11 01:48:57 +0000127 unsigned UIntData;
Daniel Dunbare126ab12008-09-10 02:41:04 +0000128
Daniel Dunbar22e30052008-09-11 01:48:57 +0000129 ABIArgInfo(Kind K, const llvm::Type *TD=0,
130 unsigned UI=0) : TheKind(K),
131 TypeData(TD),
132 UIntData(0) {}
Daniel Dunbare126ab12008-09-10 02:41:04 +0000133public:
134 static ABIArgInfo getDefault() {
Daniel Dunbar22e30052008-09-11 01:48:57 +0000135 return ABIArgInfo(Default);
Daniel Dunbare126ab12008-09-10 02:41:04 +0000136 }
137 static ABIArgInfo getStructRet() {
Daniel Dunbar22e30052008-09-11 01:48:57 +0000138 return ABIArgInfo(StructRet);
Daniel Dunbare126ab12008-09-10 02:41:04 +0000139 }
Daniel Dunbar73d66602008-09-10 07:04:09 +0000140 static ABIArgInfo getCoerce(const llvm::Type *T) {
141 assert(T->isSingleValueType() && "Can only coerce to simple types");
Daniel Dunbare126ab12008-09-10 02:41:04 +0000142 return ABIArgInfo(Coerce, T);
143 }
Daniel Dunbar22e30052008-09-11 01:48:57 +0000144 static ABIArgInfo getByVal(unsigned Alignment) {
145 return ABIArgInfo(ByVal, 0, Alignment);
146 }
Daniel Dunbar04d35782008-09-17 00:51:38 +0000147 static ABIArgInfo getExpand() {
148 return ABIArgInfo(Expand);
149 }
Daniel Dunbare126ab12008-09-10 02:41:04 +0000150
151 Kind getKind() const { return TheKind; }
152 bool isDefault() const { return TheKind == Default; }
153 bool isStructRet() const { return TheKind == StructRet; }
154 bool isCoerce() const { return TheKind == Coerce; }
Daniel Dunbar22e30052008-09-11 01:48:57 +0000155 bool isByVal() const { return TheKind == ByVal; }
Daniel Dunbar04d35782008-09-17 00:51:38 +0000156 bool isExpand() const { return TheKind == Expand; }
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000157
158 // Coerce accessors
Daniel Dunbar73d66602008-09-10 07:04:09 +0000159 const llvm::Type *getCoerceToType() const {
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000160 assert(TheKind == Coerce && "Invalid kind!");
161 return TypeData;
162 }
Daniel Dunbar22e30052008-09-11 01:48:57 +0000163
164 // ByVal accessors
165 unsigned getByValAlignment() const {
166 assert(TheKind == ByVal && "Invalid kind!");
167 return UIntData;
168 }
Daniel Dunbare126ab12008-09-10 02:41:04 +0000169};
170
171/***/
172
Daniel Dunbar73d66602008-09-10 07:04:09 +0000173static ABIArgInfo classifyReturnType(QualType RetTy,
174 ASTContext &Context) {
Daniel Dunbar753d1102008-09-11 00:04:36 +0000175 assert(!RetTy->isArrayType() &&
176 "Array types cannot be passed directly.");
Daniel Dunbare126ab12008-09-10 02:41:04 +0000177 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar73d66602008-09-10 07:04:09 +0000178 uint64_t Size = Context.getTypeSize(RetTy);
179 if (Size == 8) {
180 return ABIArgInfo::getCoerce(llvm::Type::Int8Ty);
181 } else if (Size == 16) {
182 return ABIArgInfo::getCoerce(llvm::Type::Int16Ty);
183 } else if (Size == 32) {
184 return ABIArgInfo::getCoerce(llvm::Type::Int32Ty);
185 } else if (Size == 64) {
186 return ABIArgInfo::getCoerce(llvm::Type::Int64Ty);
187 } else {
188 return ABIArgInfo::getStructRet();
189 }
Daniel Dunbare126ab12008-09-10 02:41:04 +0000190 } else {
191 return ABIArgInfo::getDefault();
192 }
193}
194
Daniel Dunbar22e30052008-09-11 01:48:57 +0000195static ABIArgInfo classifyArgumentType(QualType Ty,
Daniel Dunbar3158c592008-09-17 20:11:04 +0000196 ASTContext &Context) {
Daniel Dunbar22e30052008-09-11 01:48:57 +0000197 assert(!Ty->isArrayType() && "Array types cannot be passed directly.");
Daniel Dunbar3158c592008-09-17 20:11:04 +0000198 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar22e30052008-09-11 01:48:57 +0000199 return ABIArgInfo::getByVal(0);
200 } else {
201 return ABIArgInfo::getDefault();
202 }
203}
204
Daniel Dunbar3158c592008-09-17 20:11:04 +0000205static ABIArgInfo getABIReturnInfo(QualType Ty,
206 ASTContext &Context) {
207 ABIArgInfo Info = classifyReturnType(Ty, Context);
208 // Ensure default on aggregate types is StructRet.
209 if (Info.isDefault() && CodeGenFunction::hasAggregateLLVMType(Ty))
210 return ABIArgInfo::getStructRet();
211 return Info;
212}
213
214static ABIArgInfo getABIArgumentInfo(QualType Ty,
215 ASTContext &Context) {
216 ABIArgInfo Info = classifyArgumentType(Ty, Context);
217 // Ensure default on aggregate types is ByVal.
218 if (Info.isDefault() && CodeGenFunction::hasAggregateLLVMType(Ty))
219 return ABIArgInfo::getByVal(0);
220 return Info;
221}
222
Daniel Dunbare126ab12008-09-10 02:41:04 +0000223/***/
224
Daniel Dunbar04d35782008-09-17 00:51:38 +0000225void CodeGenTypes::GetExpandedTypes(QualType Ty,
226 std::vector<const llvm::Type*> &ArgTys) {
227 const RecordType *RT = Ty->getAsStructureType();
228 assert(RT && "Can only expand structure types.");
229 const RecordDecl *RD = RT->getDecl();
230 assert(!RD->hasFlexibleArrayMember() &&
231 "Cannot expand structure with flexible array.");
232
233 for (RecordDecl::field_const_iterator i = RD->field_begin(),
234 e = RD->field_end(); i != e; ++i) {
235 const FieldDecl *FD = *i;
236 assert(!FD->isBitField() &&
237 "Cannot expand structure with bit-field members.");
238
239 QualType FT = FD->getType();
240 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
241 GetExpandedTypes(FT, ArgTys);
242 } else {
243 ArgTys.push_back(ConvertType(FT));
244 }
245 }
246}
247
248llvm::Function::arg_iterator
249CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
250 llvm::Function::arg_iterator AI) {
251 const RecordType *RT = Ty->getAsStructureType();
252 assert(RT && "Can only expand structure types.");
253
254 RecordDecl *RD = RT->getDecl();
255 assert(LV.isSimple() &&
256 "Unexpected non-simple lvalue during struct expansion.");
257 llvm::Value *Addr = LV.getAddress();
258 for (RecordDecl::field_iterator i = RD->field_begin(),
259 e = RD->field_end(); i != e; ++i) {
260 FieldDecl *FD = *i;
261 QualType FT = FD->getType();
262
263 // FIXME: What are the right qualifiers here?
264 LValue LV = EmitLValueForField(Addr, FD, false, 0);
265 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
266 AI = ExpandTypeFromArgs(FT, LV, AI);
267 } else {
268 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
269 ++AI;
270 }
271 }
272
273 return AI;
274}
275
276void
277CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
278 llvm::SmallVector<llvm::Value*, 16> &Args) {
279 const RecordType *RT = Ty->getAsStructureType();
280 assert(RT && "Can only expand structure types.");
281
282 RecordDecl *RD = RT->getDecl();
283 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
284 llvm::Value *Addr = RV.getAggregateAddr();
285 for (RecordDecl::field_iterator i = RD->field_begin(),
286 e = RD->field_end(); i != e; ++i) {
287 FieldDecl *FD = *i;
288 QualType FT = FD->getType();
289
290 // FIXME: What are the right qualifiers here?
291 LValue LV = EmitLValueForField(Addr, FD, false, 0);
292 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
293 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
294 } else {
295 RValue RV = EmitLoadOfLValue(LV, FT);
296 assert(RV.isScalar() &&
297 "Unexpected non-scalar rvalue during struct expansion.");
298 Args.push_back(RV.getScalarVal());
299 }
300 }
301}
302
303/***/
304
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000305const llvm::FunctionType *
Daniel Dunbara9976a22008-09-10 07:00:50 +0000306CodeGenTypes::GetFunctionType(const CGCallInfo &CI, bool IsVariadic) {
307 return GetFunctionType(CI.argtypes_begin(), CI.argtypes_end(), IsVariadic);
308}
309
310const llvm::FunctionType *
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000311CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI) {
Daniel Dunbara9976a22008-09-10 07:00:50 +0000312 return GetFunctionType(FI.argtypes_begin(), FI.argtypes_end(), FI.isVariadic());
313}
314
315const llvm::FunctionType *
316CodeGenTypes::GetFunctionType(ArgTypeIterator begin, ArgTypeIterator end,
317 bool IsVariadic) {
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000318 std::vector<const llvm::Type*> ArgTys;
319
320 const llvm::Type *ResultType = 0;
321
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000322 QualType RetTy = *begin;
Daniel Dunbar3158c592008-09-17 20:11:04 +0000323 ABIArgInfo RetAI = getABIReturnInfo(RetTy, getContext());
Daniel Dunbar22e30052008-09-11 01:48:57 +0000324 switch (RetAI.getKind()) {
325 case ABIArgInfo::ByVal:
326 case ABIArgInfo::Expand:
327 assert(0 && "Invalid ABI kind for return argument");
328
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000329 case ABIArgInfo::Default:
330 if (RetTy->isVoidType()) {
331 ResultType = llvm::Type::VoidTy;
332 } else {
Daniel Dunbara9976a22008-09-10 07:00:50 +0000333 ResultType = ConvertType(RetTy);
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000334 }
335 break;
336
337 case ABIArgInfo::StructRet: {
338 ResultType = llvm::Type::VoidTy;
Daniel Dunbara9976a22008-09-10 07:00:50 +0000339 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000340 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
341 break;
342 }
343
344 case ABIArgInfo::Coerce:
Daniel Dunbar73d66602008-09-10 07:04:09 +0000345 ResultType = RetAI.getCoerceToType();
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000346 break;
347 }
348
349 for (++begin; begin != end; ++begin) {
Daniel Dunbar3158c592008-09-17 20:11:04 +0000350 ABIArgInfo AI = getABIArgumentInfo(*begin, getContext());
Daniel Dunbara9976a22008-09-10 07:00:50 +0000351 const llvm::Type *Ty = ConvertType(*begin);
Daniel Dunbar22e30052008-09-11 01:48:57 +0000352
353 switch (AI.getKind()) {
Daniel Dunbar04d35782008-09-17 00:51:38 +0000354 case ABIArgInfo::Coerce:
Daniel Dunbar22e30052008-09-11 01:48:57 +0000355 case ABIArgInfo::StructRet:
356 assert(0 && "Invalid ABI kind for non-return argument");
357
358 case ABIArgInfo::ByVal:
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000359 // byval arguments are always on the stack, which is addr space #0.
360 ArgTys.push_back(llvm::PointerType::getUnqual(Ty));
Daniel Dunbar22e30052008-09-11 01:48:57 +0000361 assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled");
362 break;
363
364 case ABIArgInfo::Default:
365 ArgTys.push_back(Ty);
366 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +0000367
368 case ABIArgInfo::Expand:
Daniel Dunbar04d35782008-09-17 00:51:38 +0000369 GetExpandedTypes(*begin, ArgTys);
Daniel Dunbar22e30052008-09-11 01:48:57 +0000370 break;
371 }
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000372 }
373
Daniel Dunbara9976a22008-09-10 07:00:50 +0000374 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000375}
376
Daniel Dunbar3ef2e852008-09-10 00:41:16 +0000377bool CodeGenModule::ReturnTypeUsesSret(QualType RetTy) {
Daniel Dunbar3158c592008-09-17 20:11:04 +0000378 return getABIReturnInfo(RetTy, getContext()).isStructRet();
Daniel Dunbar49f5a0d2008-09-09 23:48:28 +0000379}
380
Daniel Dunbar3ef2e852008-09-10 00:41:16 +0000381void CodeGenModule::ConstructParamAttrList(const Decl *TargetDecl,
Daniel Dunbare126ab12008-09-10 02:41:04 +0000382 ArgTypeIterator begin,
383 ArgTypeIterator end,
384 ParamAttrListType &PAL) {
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000385 unsigned FuncAttrs = 0;
386
387 if (TargetDecl) {
388 if (TargetDecl->getAttr<NoThrowAttr>())
389 FuncAttrs |= llvm::ParamAttr::NoUnwind;
390 if (TargetDecl->getAttr<NoReturnAttr>())
391 FuncAttrs |= llvm::ParamAttr::NoReturn;
392 }
393
Daniel Dunbare126ab12008-09-10 02:41:04 +0000394 QualType RetTy = *begin;
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000395 unsigned Index = 1;
Daniel Dunbar3158c592008-09-17 20:11:04 +0000396 ABIArgInfo RetAI = getABIReturnInfo(RetTy, getContext());
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000397 switch (RetAI.getKind()) {
Daniel Dunbare126ab12008-09-10 02:41:04 +0000398 case ABIArgInfo::Default:
399 if (RetTy->isPromotableIntegerType()) {
400 if (RetTy->isSignedIntegerType()) {
401 FuncAttrs |= llvm::ParamAttr::SExt;
402 } else if (RetTy->isUnsignedIntegerType()) {
403 FuncAttrs |= llvm::ParamAttr::ZExt;
404 }
405 }
406 break;
407
408 case ABIArgInfo::StructRet:
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000409 PAL.push_back(llvm::ParamAttrsWithIndex::get(Index,
410 llvm::ParamAttr::StructRet));
411 ++Index;
Daniel Dunbare126ab12008-09-10 02:41:04 +0000412 break;
413
414 case ABIArgInfo::Coerce:
Daniel Dunbare126ab12008-09-10 02:41:04 +0000415 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +0000416
417 case ABIArgInfo::ByVal:
418 case ABIArgInfo::Expand:
419 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000420 }
Daniel Dunbare126ab12008-09-10 02:41:04 +0000421
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000422 if (FuncAttrs)
423 PAL.push_back(llvm::ParamAttrsWithIndex::get(0, FuncAttrs));
Daniel Dunbar04d35782008-09-17 00:51:38 +0000424 for (++begin; begin != end; ++begin) {
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000425 QualType ParamType = *begin;
426 unsigned ParamAttrs = 0;
Daniel Dunbar3158c592008-09-17 20:11:04 +0000427 ABIArgInfo AI = getABIArgumentInfo(ParamType, getContext());
Daniel Dunbar22e30052008-09-11 01:48:57 +0000428
429 switch (AI.getKind()) {
430 case ABIArgInfo::StructRet:
Daniel Dunbar04d35782008-09-17 00:51:38 +0000431 case ABIArgInfo::Coerce:
Daniel Dunbar22e30052008-09-11 01:48:57 +0000432 assert(0 && "Invalid ABI kind for non-return argument");
433
434 case ABIArgInfo::ByVal:
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000435 ParamAttrs |= llvm::ParamAttr::ByVal;
Daniel Dunbar22e30052008-09-11 01:48:57 +0000436 assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled");
437 break;
438
439 case ABIArgInfo::Default:
440 if (ParamType->isPromotableIntegerType()) {
441 if (ParamType->isSignedIntegerType()) {
442 ParamAttrs |= llvm::ParamAttr::SExt;
443 } else if (ParamType->isUnsignedIntegerType()) {
444 ParamAttrs |= llvm::ParamAttr::ZExt;
445 }
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000446 }
Daniel Dunbar22e30052008-09-11 01:48:57 +0000447 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +0000448
Daniel Dunbar04d35782008-09-17 00:51:38 +0000449 case ABIArgInfo::Expand: {
450 std::vector<const llvm::Type*> Tys;
451 // FIXME: This is rather inefficient. Do we ever actually need
452 // to do anything here? The result should be just reconstructed
453 // on the other side, so extension should be a non-issue.
454 getTypes().GetExpandedTypes(ParamType, Tys);
455 Index += Tys.size();
456 continue;
457 }
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000458 }
Daniel Dunbar22e30052008-09-11 01:48:57 +0000459
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000460 if (ParamAttrs)
461 PAL.push_back(llvm::ParamAttrsWithIndex::get(Index, ParamAttrs));
Daniel Dunbar04d35782008-09-17 00:51:38 +0000462 ++Index;
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000463 }
464}
465
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000466void CodeGenFunction::EmitFunctionProlog(llvm::Function *Fn,
467 QualType RetTy,
468 const FunctionArgList &Args) {
469 // Emit allocs for param decls. Give the LLVM Argument nodes names.
470 llvm::Function::arg_iterator AI = Fn->arg_begin();
471
472 // Name the struct return argument.
Daniel Dunbare126ab12008-09-10 02:41:04 +0000473 if (CGM.ReturnTypeUsesSret(RetTy)) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000474 AI->setName("agg.result");
475 ++AI;
476 }
477
478 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbar04d35782008-09-17 00:51:38 +0000479 i != e; ++i) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000480 const VarDecl *Arg = i->first;
Daniel Dunbar04d35782008-09-17 00:51:38 +0000481 QualType Ty = i->second;
Daniel Dunbar3158c592008-09-17 20:11:04 +0000482 ABIArgInfo ArgI = getABIArgumentInfo(Ty, getContext());
Daniel Dunbar22e30052008-09-11 01:48:57 +0000483
484 switch (ArgI.getKind()) {
485 case ABIArgInfo::ByVal:
486 case ABIArgInfo::Default: {
487 assert(AI != Fn->arg_end() && "Argument mismatch!");
488 llvm::Value* V = AI;
Daniel Dunbar04d35782008-09-17 00:51:38 +0000489 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
Daniel Dunbar22e30052008-09-11 01:48:57 +0000490 // This must be a promotion, for something like
491 // "void a(x) short x; {..."
Daniel Dunbar04d35782008-09-17 00:51:38 +0000492 V = EmitScalarConversion(V, Ty, Arg->getType());
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000493 }
Daniel Dunbar22e30052008-09-11 01:48:57 +0000494 EmitParmDecl(*Arg, V);
495 break;
496 }
Daniel Dunbar04d35782008-09-17 00:51:38 +0000497
498 case ABIArgInfo::Expand: {
499 // If this was structure was expand into multiple arguments then
500 // we need to create a temporary and reconstruct it from the
501 // arguments.
502 std::string Name(Arg->getName());
503 llvm::Value *Temp = CreateTempAlloca(ConvertType(Ty),
504 (Name + ".addr").c_str());
505 // FIXME: What are the right qualifiers here?
506 llvm::Function::arg_iterator End =
507 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI);
508 EmitParmDecl(*Arg, Temp);
Daniel Dunbar22e30052008-09-11 01:48:57 +0000509
Daniel Dunbar04d35782008-09-17 00:51:38 +0000510 // Name the arguments used in expansion and increment AI.
511 unsigned Index = 0;
512 for (; AI != End; ++AI, ++Index)
513 AI->setName(Name + "." + llvm::utostr(Index));
514 continue;
515 }
516
Daniel Dunbar22e30052008-09-11 01:48:57 +0000517 case ABIArgInfo::Coerce:
Daniel Dunbar22e30052008-09-11 01:48:57 +0000518 case ABIArgInfo::StructRet:
519 assert(0 && "Invalid ABI kind for non-return argument");
520 }
Daniel Dunbar04d35782008-09-17 00:51:38 +0000521
522 ++AI;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000523 }
524 assert(AI == Fn->arg_end() && "Argument mismatch!");
525}
526
527void CodeGenFunction::EmitFunctionEpilog(QualType RetTy,
528 llvm::Value *ReturnValue) {
Daniel Dunbare126ab12008-09-10 02:41:04 +0000529 llvm::Value *RV = 0;
530
531 // Functions with no result always return void.
532 if (ReturnValue) {
Daniel Dunbar3158c592008-09-17 20:11:04 +0000533 ABIArgInfo RetAI = getABIReturnInfo(RetTy, getContext());
Daniel Dunbare126ab12008-09-10 02:41:04 +0000534
535 switch (RetAI.getKind()) {
536 case ABIArgInfo::StructRet:
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000537 EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy);
Daniel Dunbare126ab12008-09-10 02:41:04 +0000538 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +0000539
Daniel Dunbare126ab12008-09-10 02:41:04 +0000540 case ABIArgInfo::Default:
541 RV = Builder.CreateLoad(ReturnValue);
542 break;
543
Daniel Dunbar73d66602008-09-10 07:04:09 +0000544 case ABIArgInfo::Coerce: {
545 const llvm::Type *CoerceToPTy =
546 llvm::PointerType::getUnqual(RetAI.getCoerceToType());
547 RV = Builder.CreateLoad(Builder.CreateBitCast(ReturnValue, CoerceToPTy));
Daniel Dunbar22e30052008-09-11 01:48:57 +0000548 break;
Daniel Dunbar73d66602008-09-10 07:04:09 +0000549 }
Daniel Dunbar22e30052008-09-11 01:48:57 +0000550
551 case ABIArgInfo::ByVal:
552 case ABIArgInfo::Expand:
553 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000554 }
555 }
Daniel Dunbare126ab12008-09-10 02:41:04 +0000556
557 if (RV) {
558 Builder.CreateRet(RV);
559 } else {
560 Builder.CreateRetVoid();
561 }
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000562}
563
564RValue CodeGenFunction::EmitCall(llvm::Value *Callee,
Daniel Dunbare126ab12008-09-10 02:41:04 +0000565 QualType RetTy,
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000566 const CallArgList &CallArgs) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000567 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000568
569 // Handle struct-return functions by passing a pointer to the
570 // location that we would like to return into.
Daniel Dunbar3158c592008-09-17 20:11:04 +0000571 ABIArgInfo RetAI = getABIReturnInfo(RetTy, getContext());
Daniel Dunbare126ab12008-09-10 02:41:04 +0000572 switch (RetAI.getKind()) {
573 case ABIArgInfo::StructRet:
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000574 // Create a temporary alloca to hold the result of the call. :(
Daniel Dunbar04d35782008-09-17 00:51:38 +0000575 Args.push_back(CreateTempAlloca(ConvertType(RetTy)));
Daniel Dunbare126ab12008-09-10 02:41:04 +0000576 break;
577
578 case ABIArgInfo::Default:
Daniel Dunbare126ab12008-09-10 02:41:04 +0000579 case ABIArgInfo::Coerce:
Daniel Dunbare126ab12008-09-10 02:41:04 +0000580 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +0000581
582 case ABIArgInfo::ByVal:
583 case ABIArgInfo::Expand:
584 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000585 }
586
587 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
588 I != E; ++I) {
Daniel Dunbar3158c592008-09-17 20:11:04 +0000589 ABIArgInfo ArgInfo = getABIArgumentInfo(I->second, getContext());
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000590 RValue RV = I->first;
Daniel Dunbar04d35782008-09-17 00:51:38 +0000591
592 switch (ArgInfo.getKind()) {
593 case ABIArgInfo::ByVal: // Default is byval
594 case ABIArgInfo::Default:
595 if (RV.isScalar()) {
596 Args.push_back(RV.getScalarVal());
597 } else if (RV.isComplex()) {
598 // Make a temporary alloca to pass the argument.
599 Args.push_back(CreateTempAlloca(ConvertType(I->second)));
600 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
601 } else {
602 Args.push_back(RV.getAggregateAddr());
603 }
604 break;
605
606 case ABIArgInfo::StructRet:
607 case ABIArgInfo::Coerce:
608 assert(0 && "Invalid ABI kind for non-return argument");
609 break;
610
611 case ABIArgInfo::Expand:
612 ExpandTypeToArgs(I->second, RV, Args);
613 break;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000614 }
615 }
616
617 llvm::CallInst *CI = Builder.CreateCall(Callee,&Args[0],&Args[0]+Args.size());
Daniel Dunbare126ab12008-09-10 02:41:04 +0000618 CGCallInfo CallInfo(RetTy, CallArgs);
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000619
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000620 // FIXME: Provide TargetDecl so nounwind, noreturn, etc, etc get set.
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000621 CodeGen::ParamAttrListType ParamAttrList;
Daniel Dunbar3ef2e852008-09-10 00:41:16 +0000622 CGM.ConstructParamAttrList(0,
623 CallInfo.argtypes_begin(), CallInfo.argtypes_end(),
624 ParamAttrList);
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000625 CI->setParamAttrs(llvm::PAListPtr::get(ParamAttrList.begin(),
626 ParamAttrList.size()));
627
628 if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee))
629 CI->setCallingConv(F->getCallingConv());
630 if (CI->getType() != llvm::Type::VoidTy)
631 CI->setName("call");
Daniel Dunbare126ab12008-09-10 02:41:04 +0000632
633 switch (RetAI.getKind()) {
634 case ABIArgInfo::StructRet:
635 if (RetTy->isAnyComplexType())
Daniel Dunbar04d35782008-09-17 00:51:38 +0000636 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Daniel Dunbare126ab12008-09-10 02:41:04 +0000637 else
638 // Struct return.
Daniel Dunbar04d35782008-09-17 00:51:38 +0000639 return RValue::getAggregate(Args[0]);
Daniel Dunbar22e30052008-09-11 01:48:57 +0000640
Daniel Dunbare126ab12008-09-10 02:41:04 +0000641 case ABIArgInfo::Default:
642 return RValue::get(RetTy->isVoidType() ? 0 : CI);
643
Daniel Dunbar73d66602008-09-10 07:04:09 +0000644 case ABIArgInfo::Coerce: {
645 const llvm::Type *CoerceToPTy =
646 llvm::PointerType::getUnqual(RetAI.getCoerceToType());
647 llvm::Value *V = CreateTempAlloca(ConvertType(RetTy), "tmp");
648 Builder.CreateStore(CI, Builder.CreateBitCast(V, CoerceToPTy));
649 return RValue::getAggregate(V);
650 }
Daniel Dunbar22e30052008-09-11 01:48:57 +0000651
652 case ABIArgInfo::ByVal:
653 case ABIArgInfo::Expand:
654 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000655 }
Daniel Dunbare126ab12008-09-10 02:41:04 +0000656
657 assert(0 && "Unhandled ABIArgInfo::Kind");
658 return RValue::get(0);
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000659}