blob: ac0eb1878f0d9186d9f71df66611aa7450a2e083 [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 Dunbarf98eeff2008-10-13 17:02:26 +000018#include "clang/Basic/TargetInfo.h"
Daniel Dunbara8f02052008-09-08 21:33:45 +000019#include "clang/AST/ASTContext.h"
20#include "clang/AST/Decl.h"
21#include "clang/AST/DeclObjC.h"
Daniel Dunbar04d35782008-09-17 00:51:38 +000022#include "llvm/ADT/StringExtras.h"
Devang Patel98bfe502008-09-24 01:01:36 +000023#include "llvm/Attributes.h"
Daniel Dunbara8f02052008-09-08 21:33:45 +000024using namespace clang;
25using namespace CodeGen;
26
27/***/
28
Daniel Dunbara8f02052008-09-08 21:33:45 +000029// FIXME: Use iterator and sidestep silly type array creation.
30
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000031CGFunctionInfo::CGFunctionInfo(const FunctionTypeNoProto *FTNP)
32 : IsVariadic(true)
33{
34 ArgTypes.push_back(FTNP->getResultType());
35}
36
37CGFunctionInfo::CGFunctionInfo(const FunctionTypeProto *FTP)
38 : IsVariadic(FTP->isVariadic())
39{
40 ArgTypes.push_back(FTP->getResultType());
41 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
42 ArgTypes.push_back(FTP->getArgType(i));
43}
44
45// FIXME: Is there really any reason to have this still?
Daniel Dunbara8f02052008-09-08 21:33:45 +000046CGFunctionInfo::CGFunctionInfo(const FunctionDecl *FD)
Daniel Dunbara8f02052008-09-08 21:33:45 +000047{
48 const FunctionType *FTy = FD->getType()->getAsFunctionType();
49 const FunctionTypeProto *FTP = dyn_cast<FunctionTypeProto>(FTy);
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000050
Daniel Dunbara8f02052008-09-08 21:33:45 +000051 ArgTypes.push_back(FTy->getResultType());
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000052 if (FTP) {
53 IsVariadic = FTP->isVariadic();
Daniel Dunbara8f02052008-09-08 21:33:45 +000054 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
55 ArgTypes.push_back(FTP->getArgType(i));
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000056 } else {
57 IsVariadic = true;
58 }
Daniel Dunbara8f02052008-09-08 21:33:45 +000059}
60
61CGFunctionInfo::CGFunctionInfo(const ObjCMethodDecl *MD,
62 const ASTContext &Context)
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000063 : IsVariadic(MD->isVariadic())
Daniel Dunbara8f02052008-09-08 21:33:45 +000064{
65 ArgTypes.push_back(MD->getResultType());
66 ArgTypes.push_back(MD->getSelfDecl()->getType());
67 ArgTypes.push_back(Context.getObjCSelType());
68 for (ObjCMethodDecl::param_const_iterator i = MD->param_begin(),
69 e = MD->param_end(); i != e; ++i)
70 ArgTypes.push_back((*i)->getType());
71}
72
Daniel Dunbarbccb0682008-09-10 00:32:18 +000073ArgTypeIterator CGFunctionInfo::argtypes_begin() const {
74 return ArgTypes.begin();
75}
76
77ArgTypeIterator CGFunctionInfo::argtypes_end() const {
78 return ArgTypes.end();
Daniel Dunbara8f02052008-09-08 21:33:45 +000079}
80
81/***/
82
Daniel Dunbarbccb0682008-09-10 00:32:18 +000083CGCallInfo::CGCallInfo(QualType _ResultType, const CallArgList &_Args) {
84 ArgTypes.push_back(_ResultType);
85 for (CallArgList::const_iterator i = _Args.begin(), e = _Args.end(); i!=e; ++i)
Daniel Dunbara8f02052008-09-08 21:33:45 +000086 ArgTypes.push_back(i->second);
87}
88
Daniel Dunbarbccb0682008-09-10 00:32:18 +000089ArgTypeIterator CGCallInfo::argtypes_begin() const {
90 return ArgTypes.begin();
91}
92
93ArgTypeIterator CGCallInfo::argtypes_end() const {
94 return ArgTypes.end();
Daniel Dunbara8f02052008-09-08 21:33:45 +000095}
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +000096
97/***/
98
Daniel Dunbar22e30052008-09-11 01:48:57 +000099/// ABIArgInfo - Helper class to encapsulate information about how a
100/// specific C type should be passed to or returned from a function.
Daniel Dunbare126ab12008-09-10 02:41:04 +0000101class ABIArgInfo {
102public:
103 enum Kind {
104 Default,
Daniel Dunbar17d35372008-12-18 04:52:14 +0000105 StructRet, /// Only valid for return values. The return value
106 /// should be passed through a pointer to a caller
107 /// allocated location passed as an implicit first
108 /// argument to the function.
Daniel Dunbar22e30052008-09-11 01:48:57 +0000109
Daniel Dunbar04d35782008-09-17 00:51:38 +0000110 Coerce, /// Only valid for aggregate return types, the argument
111 /// should be accessed by coercion to a provided type.
Daniel Dunbar22e30052008-09-11 01:48:57 +0000112
113 ByVal, /// Only valid for aggregate argument types. The
114 /// structure should be passed "byval" with the
115 /// specified alignment (0 indicates default
116 /// alignment).
117
118 Expand, /// Only valid for aggregate argument types. The
119 /// structure should be expanded into consecutive
Daniel Dunbar04d35782008-09-17 00:51:38 +0000120 /// arguments for its constituent fields. Currently
121 /// expand is only allowed on structures whose fields
122 /// are all scalar types or are themselves expandable
123 /// types.
Daniel Dunbar22e30052008-09-11 01:48:57 +0000124
125 KindFirst=Default, KindLast=Expand
Daniel Dunbare126ab12008-09-10 02:41:04 +0000126 };
127
128private:
129 Kind TheKind;
Daniel Dunbar73d66602008-09-10 07:04:09 +0000130 const llvm::Type *TypeData;
Daniel Dunbar22e30052008-09-11 01:48:57 +0000131 unsigned UIntData;
Daniel Dunbare126ab12008-09-10 02:41:04 +0000132
Daniel Dunbar22e30052008-09-11 01:48:57 +0000133 ABIArgInfo(Kind K, const llvm::Type *TD=0,
134 unsigned UI=0) : TheKind(K),
135 TypeData(TD),
136 UIntData(0) {}
Daniel Dunbare126ab12008-09-10 02:41:04 +0000137public:
138 static ABIArgInfo getDefault() {
Daniel Dunbar22e30052008-09-11 01:48:57 +0000139 return ABIArgInfo(Default);
Daniel Dunbare126ab12008-09-10 02:41:04 +0000140 }
141 static ABIArgInfo getStructRet() {
Daniel Dunbar22e30052008-09-11 01:48:57 +0000142 return ABIArgInfo(StructRet);
Daniel Dunbare126ab12008-09-10 02:41:04 +0000143 }
Daniel Dunbar73d66602008-09-10 07:04:09 +0000144 static ABIArgInfo getCoerce(const llvm::Type *T) {
145 assert(T->isSingleValueType() && "Can only coerce to simple types");
Daniel Dunbare126ab12008-09-10 02:41:04 +0000146 return ABIArgInfo(Coerce, T);
147 }
Daniel Dunbar22e30052008-09-11 01:48:57 +0000148 static ABIArgInfo getByVal(unsigned Alignment) {
149 return ABIArgInfo(ByVal, 0, Alignment);
150 }
Daniel Dunbar04d35782008-09-17 00:51:38 +0000151 static ABIArgInfo getExpand() {
152 return ABIArgInfo(Expand);
153 }
Daniel Dunbare126ab12008-09-10 02:41:04 +0000154
155 Kind getKind() const { return TheKind; }
156 bool isDefault() const { return TheKind == Default; }
157 bool isStructRet() const { return TheKind == StructRet; }
158 bool isCoerce() const { return TheKind == Coerce; }
Daniel Dunbar22e30052008-09-11 01:48:57 +0000159 bool isByVal() const { return TheKind == ByVal; }
Daniel Dunbar04d35782008-09-17 00:51:38 +0000160 bool isExpand() const { return TheKind == Expand; }
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000161
162 // Coerce accessors
Daniel Dunbar73d66602008-09-10 07:04:09 +0000163 const llvm::Type *getCoerceToType() const {
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000164 assert(TheKind == Coerce && "Invalid kind!");
165 return TypeData;
166 }
Daniel Dunbar22e30052008-09-11 01:48:57 +0000167
168 // ByVal accessors
169 unsigned getByValAlignment() const {
170 assert(TheKind == ByVal && "Invalid kind!");
171 return UIntData;
172 }
Daniel Dunbare126ab12008-09-10 02:41:04 +0000173};
174
175/***/
176
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000177/* FIXME: All of this stuff should be part of the target interface
178 somehow. It is currently here because it is not clear how to factor
179 the targets to support this, since the Targets currently live in a
180 layer below types n'stuff.
181 */
182
183/// ABIInfo - Target specific hooks for defining how a type should be
184/// passed or returned from functions.
185class clang::ABIInfo {
186public:
187 virtual ~ABIInfo();
188
189 virtual ABIArgInfo classifyReturnType(QualType RetTy,
190 ASTContext &Context) const = 0;
191
192 virtual ABIArgInfo classifyArgumentType(QualType Ty,
193 ASTContext &Context) const = 0;
194};
195
196ABIInfo::~ABIInfo() {}
197
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000198/// isEmptyStruct - Return true iff a structure has no non-empty
199/// members. Note that a structure with a flexible array member is not
200/// considered empty.
201static bool isEmptyStruct(QualType T) {
202 const RecordType *RT = T->getAsStructureType();
203 if (!RT)
204 return 0;
205 const RecordDecl *RD = RT->getDecl();
206 if (RD->hasFlexibleArrayMember())
207 return false;
Douglas Gregor5d764842009-01-09 17:18:27 +0000208 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000209 e = RD->field_end(); i != e; ++i) {
210 const FieldDecl *FD = *i;
211 if (!isEmptyStruct(FD->getType()))
212 return false;
213 }
214 return true;
215}
216
217/// isSingleElementStruct - Determine if a structure is a "single
218/// element struct", i.e. it has exactly one non-empty field or
219/// exactly one field which is itself a single element
220/// struct. Structures with flexible array members are never
221/// considered single element structs.
222///
223/// \return The field declaration for the single non-empty field, if
224/// it exists.
225static const FieldDecl *isSingleElementStruct(QualType T) {
226 const RecordType *RT = T->getAsStructureType();
227 if (!RT)
228 return 0;
229
230 const RecordDecl *RD = RT->getDecl();
231 if (RD->hasFlexibleArrayMember())
232 return 0;
233
234 const FieldDecl *Found = 0;
Douglas Gregor5d764842009-01-09 17:18:27 +0000235 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000236 e = RD->field_end(); i != e; ++i) {
237 const FieldDecl *FD = *i;
238 QualType FT = FD->getType();
239
240 if (isEmptyStruct(FT)) {
241 // Ignore
242 } else if (Found) {
243 return 0;
244 } else if (!CodeGenFunction::hasAggregateLLVMType(FT)) {
245 Found = FD;
246 } else {
247 Found = isSingleElementStruct(FT);
248 if (!Found)
249 return 0;
250 }
251 }
252
253 return Found;
254}
255
256static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
257 if (!Ty->getAsBuiltinType() && !Ty->isPointerType())
258 return false;
259
260 uint64_t Size = Context.getTypeSize(Ty);
261 return Size == 32 || Size == 64;
262}
263
264static bool areAllFields32Or64BitBasicType(const RecordDecl *RD,
265 ASTContext &Context) {
Douglas Gregor5d764842009-01-09 17:18:27 +0000266 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000267 e = RD->field_end(); i != e; ++i) {
268 const FieldDecl *FD = *i;
269
270 if (!is32Or64BitBasicType(FD->getType(), Context))
271 return false;
272
273 // If this is a bit-field we need to make sure it is still a
274 // 32-bit or 64-bit type.
275 if (Expr *BW = FD->getBitWidth()) {
276 unsigned Width = BW->getIntegerConstantExprValue(Context).getZExtValue();
277 if (Width <= 16)
278 return false;
279 }
280 }
281 return true;
282}
283
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000284namespace {
285/// DefaultABIInfo - The default implementation for ABI specific
286/// details. This implementation provides information which results in
287/// sensible LLVM IR generation, but does not conform to any
288/// particular ABI.
289class DefaultABIInfo : public ABIInfo {
290 virtual ABIArgInfo classifyReturnType(QualType RetTy,
291 ASTContext &Context) const;
292
293 virtual ABIArgInfo classifyArgumentType(QualType RetTy,
294 ASTContext &Context) const;
295};
296
297/// X86_32ABIInfo - The X86-32 ABI information.
298class X86_32ABIInfo : public ABIInfo {
299public:
300 virtual ABIArgInfo classifyReturnType(QualType RetTy,
301 ASTContext &Context) const;
302
303 virtual ABIArgInfo classifyArgumentType(QualType RetTy,
304 ASTContext &Context) const;
305};
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000306
307
308/// X86_32ABIInfo - The X86_64 ABI information.
309class X86_64ABIInfo : public ABIInfo {
310public:
311 virtual ABIArgInfo classifyReturnType(QualType RetTy,
312 ASTContext &Context) const;
313
314 virtual ABIArgInfo classifyArgumentType(QualType RetTy,
315 ASTContext &Context) const;
316};
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000317}
318
319ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
320 ASTContext &Context) const {
Daniel Dunbare126ab12008-09-10 02:41:04 +0000321 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000322 // Classify "single element" structs as their element type.
323 const FieldDecl *SeltFD = isSingleElementStruct(RetTy);
324 if (SeltFD) {
325 QualType SeltTy = SeltFD->getType()->getDesugaredType();
326 if (const BuiltinType *BT = SeltTy->getAsBuiltinType()) {
327 // FIXME: This is gross, it would be nice if we could just
328 // pass back SeltTy and have clients deal with it. Is it worth
329 // supporting coerce to both LLVM and clang Types?
330 if (BT->isIntegerType()) {
331 uint64_t Size = Context.getTypeSize(SeltTy);
332 return ABIArgInfo::getCoerce(llvm::IntegerType::get((unsigned) Size));
333 } else if (BT->getKind() == BuiltinType::Float) {
334 return ABIArgInfo::getCoerce(llvm::Type::FloatTy);
335 } else if (BT->getKind() == BuiltinType::Double) {
336 return ABIArgInfo::getCoerce(llvm::Type::DoubleTy);
337 }
338 } else if (SeltTy->isPointerType()) {
339 // FIXME: It would be really nice if this could come out as
340 // the proper pointer type.
341 llvm::Type *PtrTy =
342 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
343 return ABIArgInfo::getCoerce(PtrTy);
344 }
345 }
346
Daniel Dunbar73d66602008-09-10 07:04:09 +0000347 uint64_t Size = Context.getTypeSize(RetTy);
348 if (Size == 8) {
349 return ABIArgInfo::getCoerce(llvm::Type::Int8Ty);
350 } else if (Size == 16) {
351 return ABIArgInfo::getCoerce(llvm::Type::Int16Ty);
352 } else if (Size == 32) {
353 return ABIArgInfo::getCoerce(llvm::Type::Int32Ty);
354 } else if (Size == 64) {
355 return ABIArgInfo::getCoerce(llvm::Type::Int64Ty);
356 } else {
357 return ABIArgInfo::getStructRet();
358 }
Daniel Dunbare126ab12008-09-10 02:41:04 +0000359 } else {
360 return ABIArgInfo::getDefault();
361 }
362}
363
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000364ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
365 ASTContext &Context) const {
Daniel Dunbar3158c592008-09-17 20:11:04 +0000366 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000367 // Structures with flexible arrays are always byval.
368 if (const RecordType *RT = Ty->getAsStructureType())
369 if (RT->getDecl()->hasFlexibleArrayMember())
370 return ABIArgInfo::getByVal(0);
371
372 // Expand empty structs (i.e. ignore)
373 uint64_t Size = Context.getTypeSize(Ty);
374 if (Ty->isStructureType() && Size == 0)
375 return ABIArgInfo::getExpand();
376
377 // Expand structs with size <= 128-bits which consist only of
378 // basic types (int, long long, float, double, xxx*). This is
379 // non-recursive and does not ignore empty fields.
380 if (const RecordType *RT = Ty->getAsStructureType()) {
381 if (Context.getTypeSize(Ty) <= 4*32 &&
382 areAllFields32Or64BitBasicType(RT->getDecl(), Context))
383 return ABIArgInfo::getExpand();
384 }
385
Daniel Dunbar22e30052008-09-11 01:48:57 +0000386 return ABIArgInfo::getByVal(0);
387 } else {
388 return ABIArgInfo::getDefault();
389 }
390}
391
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000392ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
393 ASTContext &Context) const {
394 return ABIArgInfo::getDefault();
395}
396
397ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty,
398 ASTContext &Context) const {
399 return ABIArgInfo::getDefault();
400}
401
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000402ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
403 ASTContext &Context) const {
404 return ABIArgInfo::getDefault();
405}
406
407ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
408 ASTContext &Context) const {
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000409 return ABIArgInfo::getDefault();
410}
411
412const ABIInfo &CodeGenTypes::getABIInfo() const {
413 if (TheABIInfo)
414 return *TheABIInfo;
415
416 // For now we just cache this in the CodeGenTypes and don't bother
417 // to free it.
418 const char *TargetPrefix = getContext().Target.getTargetPrefix();
419 if (strcmp(TargetPrefix, "x86") == 0) {
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000420 switch (getContext().Target.getPointerWidth(0)) {
421 case 32:
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000422 return *(TheABIInfo = new X86_32ABIInfo());
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000423 case 64:
424 return *(TheABIInfo = new X86_64ABIInfo());
425 }
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000426 }
427
428 return *(TheABIInfo = new DefaultABIInfo);
429}
430
431// getABIReturnInfo - Wrap the ABIInfo getABIReturnInfo, altering
432// "default" types to StructRet when appropriate for simplicity.
433static ABIArgInfo getABIReturnInfo(QualType Ty, CodeGenTypes &CGT) {
434 assert(!Ty->isArrayType() &&
435 "Array types cannot be passed directly.");
436 ABIArgInfo Info = CGT.getABIInfo().classifyReturnType(Ty, CGT.getContext());
Daniel Dunbar3158c592008-09-17 20:11:04 +0000437 // Ensure default on aggregate types is StructRet.
438 if (Info.isDefault() && CodeGenFunction::hasAggregateLLVMType(Ty))
439 return ABIArgInfo::getStructRet();
440 return Info;
441}
442
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000443// getABIArgumentInfo - Wrap the ABIInfo getABIReturnInfo, altering
444// "default" types to ByVal when appropriate for simplicity.
445static ABIArgInfo getABIArgumentInfo(QualType Ty, CodeGenTypes &CGT) {
446 assert(!Ty->isArrayType() &&
447 "Array types cannot be passed directly.");
448 ABIArgInfo Info = CGT.getABIInfo().classifyArgumentType(Ty, CGT.getContext());
Daniel Dunbar3158c592008-09-17 20:11:04 +0000449 // Ensure default on aggregate types is ByVal.
450 if (Info.isDefault() && CodeGenFunction::hasAggregateLLVMType(Ty))
451 return ABIArgInfo::getByVal(0);
452 return Info;
453}
454
Daniel Dunbare126ab12008-09-10 02:41:04 +0000455/***/
456
Daniel Dunbar04d35782008-09-17 00:51:38 +0000457void CodeGenTypes::GetExpandedTypes(QualType Ty,
458 std::vector<const llvm::Type*> &ArgTys) {
459 const RecordType *RT = Ty->getAsStructureType();
460 assert(RT && "Can only expand structure types.");
461 const RecordDecl *RD = RT->getDecl();
462 assert(!RD->hasFlexibleArrayMember() &&
463 "Cannot expand structure with flexible array.");
464
Douglas Gregor5d764842009-01-09 17:18:27 +0000465 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar04d35782008-09-17 00:51:38 +0000466 e = RD->field_end(); i != e; ++i) {
467 const FieldDecl *FD = *i;
468 assert(!FD->isBitField() &&
469 "Cannot expand structure with bit-field members.");
470
471 QualType FT = FD->getType();
472 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
473 GetExpandedTypes(FT, ArgTys);
474 } else {
475 ArgTys.push_back(ConvertType(FT));
476 }
477 }
478}
479
480llvm::Function::arg_iterator
481CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
482 llvm::Function::arg_iterator AI) {
483 const RecordType *RT = Ty->getAsStructureType();
484 assert(RT && "Can only expand structure types.");
485
486 RecordDecl *RD = RT->getDecl();
487 assert(LV.isSimple() &&
488 "Unexpected non-simple lvalue during struct expansion.");
489 llvm::Value *Addr = LV.getAddress();
490 for (RecordDecl::field_iterator i = RD->field_begin(),
491 e = RD->field_end(); i != e; ++i) {
492 FieldDecl *FD = *i;
493 QualType FT = FD->getType();
494
495 // FIXME: What are the right qualifiers here?
496 LValue LV = EmitLValueForField(Addr, FD, false, 0);
497 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
498 AI = ExpandTypeFromArgs(FT, LV, AI);
499 } else {
500 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
501 ++AI;
502 }
503 }
504
505 return AI;
506}
507
508void
509CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
510 llvm::SmallVector<llvm::Value*, 16> &Args) {
511 const RecordType *RT = Ty->getAsStructureType();
512 assert(RT && "Can only expand structure types.");
513
514 RecordDecl *RD = RT->getDecl();
515 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
516 llvm::Value *Addr = RV.getAggregateAddr();
517 for (RecordDecl::field_iterator i = RD->field_begin(),
518 e = RD->field_end(); i != e; ++i) {
519 FieldDecl *FD = *i;
520 QualType FT = FD->getType();
521
522 // FIXME: What are the right qualifiers here?
523 LValue LV = EmitLValueForField(Addr, FD, false, 0);
524 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
525 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
526 } else {
527 RValue RV = EmitLoadOfLValue(LV, FT);
528 assert(RV.isScalar() &&
529 "Unexpected non-scalar rvalue during struct expansion.");
530 Args.push_back(RV.getScalarVal());
531 }
532 }
533}
534
535/***/
536
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000537const llvm::FunctionType *
Daniel Dunbara9976a22008-09-10 07:00:50 +0000538CodeGenTypes::GetFunctionType(const CGCallInfo &CI, bool IsVariadic) {
539 return GetFunctionType(CI.argtypes_begin(), CI.argtypes_end(), IsVariadic);
540}
541
542const llvm::FunctionType *
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000543CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI) {
Daniel Dunbara9976a22008-09-10 07:00:50 +0000544 return GetFunctionType(FI.argtypes_begin(), FI.argtypes_end(), FI.isVariadic());
545}
546
547const llvm::FunctionType *
548CodeGenTypes::GetFunctionType(ArgTypeIterator begin, ArgTypeIterator end,
549 bool IsVariadic) {
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000550 std::vector<const llvm::Type*> ArgTys;
551
552 const llvm::Type *ResultType = 0;
553
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000554 QualType RetTy = *begin;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000555 ABIArgInfo RetAI = getABIReturnInfo(RetTy, *this);
Daniel Dunbar22e30052008-09-11 01:48:57 +0000556 switch (RetAI.getKind()) {
557 case ABIArgInfo::ByVal:
558 case ABIArgInfo::Expand:
559 assert(0 && "Invalid ABI kind for return argument");
560
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000561 case ABIArgInfo::Default:
562 if (RetTy->isVoidType()) {
563 ResultType = llvm::Type::VoidTy;
564 } else {
Daniel Dunbara9976a22008-09-10 07:00:50 +0000565 ResultType = ConvertType(RetTy);
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000566 }
567 break;
568
569 case ABIArgInfo::StructRet: {
570 ResultType = llvm::Type::VoidTy;
Daniel Dunbara9976a22008-09-10 07:00:50 +0000571 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000572 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
573 break;
574 }
575
576 case ABIArgInfo::Coerce:
Daniel Dunbar73d66602008-09-10 07:04:09 +0000577 ResultType = RetAI.getCoerceToType();
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000578 break;
579 }
580
581 for (++begin; begin != end; ++begin) {
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000582 ABIArgInfo AI = getABIArgumentInfo(*begin, *this);
Daniel Dunbara9976a22008-09-10 07:00:50 +0000583 const llvm::Type *Ty = ConvertType(*begin);
Daniel Dunbar22e30052008-09-11 01:48:57 +0000584
585 switch (AI.getKind()) {
Daniel Dunbar04d35782008-09-17 00:51:38 +0000586 case ABIArgInfo::Coerce:
Daniel Dunbar22e30052008-09-11 01:48:57 +0000587 case ABIArgInfo::StructRet:
588 assert(0 && "Invalid ABI kind for non-return argument");
589
590 case ABIArgInfo::ByVal:
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000591 // byval arguments are always on the stack, which is addr space #0.
592 ArgTys.push_back(llvm::PointerType::getUnqual(Ty));
Daniel Dunbar22e30052008-09-11 01:48:57 +0000593 assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled");
594 break;
595
596 case ABIArgInfo::Default:
597 ArgTys.push_back(Ty);
598 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +0000599
600 case ABIArgInfo::Expand:
Daniel Dunbar04d35782008-09-17 00:51:38 +0000601 GetExpandedTypes(*begin, ArgTys);
Daniel Dunbar22e30052008-09-11 01:48:57 +0000602 break;
603 }
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000604 }
605
Daniel Dunbara9976a22008-09-10 07:00:50 +0000606 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000607}
608
Daniel Dunbar3ef2e852008-09-10 00:41:16 +0000609bool CodeGenModule::ReturnTypeUsesSret(QualType RetTy) {
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000610 return getABIReturnInfo(RetTy, getTypes()).isStructRet();
Daniel Dunbar49f5a0d2008-09-09 23:48:28 +0000611}
612
Devang Patela85a9ef2008-09-25 21:02:23 +0000613void CodeGenModule::ConstructAttributeList(const Decl *TargetDecl,
Daniel Dunbare126ab12008-09-10 02:41:04 +0000614 ArgTypeIterator begin,
615 ArgTypeIterator end,
Devang Patela85a9ef2008-09-25 21:02:23 +0000616 AttributeListType &PAL) {
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000617 unsigned FuncAttrs = 0;
Devang Patel2bb6eb82008-09-26 22:53:57 +0000618 unsigned RetAttrs = 0;
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000619
620 if (TargetDecl) {
621 if (TargetDecl->getAttr<NoThrowAttr>())
Devang Patela85a9ef2008-09-25 21:02:23 +0000622 FuncAttrs |= llvm::Attribute::NoUnwind;
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000623 if (TargetDecl->getAttr<NoReturnAttr>())
Devang Patela85a9ef2008-09-25 21:02:23 +0000624 FuncAttrs |= llvm::Attribute::NoReturn;
Anders Carlssondd6791c2008-10-05 23:32:53 +0000625 if (TargetDecl->getAttr<PureAttr>())
626 FuncAttrs |= llvm::Attribute::ReadOnly;
627 if (TargetDecl->getAttr<ConstAttr>())
628 FuncAttrs |= llvm::Attribute::ReadNone;
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000629 }
630
Daniel Dunbare126ab12008-09-10 02:41:04 +0000631 QualType RetTy = *begin;
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000632 unsigned Index = 1;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000633 ABIArgInfo RetAI = getABIReturnInfo(RetTy, getTypes());
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000634 switch (RetAI.getKind()) {
Daniel Dunbare126ab12008-09-10 02:41:04 +0000635 case ABIArgInfo::Default:
636 if (RetTy->isPromotableIntegerType()) {
637 if (RetTy->isSignedIntegerType()) {
Devang Patel2bb6eb82008-09-26 22:53:57 +0000638 RetAttrs |= llvm::Attribute::SExt;
Daniel Dunbare126ab12008-09-10 02:41:04 +0000639 } else if (RetTy->isUnsignedIntegerType()) {
Devang Patel2bb6eb82008-09-26 22:53:57 +0000640 RetAttrs |= llvm::Attribute::ZExt;
Daniel Dunbare126ab12008-09-10 02:41:04 +0000641 }
642 }
643 break;
644
645 case ABIArgInfo::StructRet:
Devang Patela85a9ef2008-09-25 21:02:23 +0000646 PAL.push_back(llvm::AttributeWithIndex::get(Index,
647 llvm::Attribute::StructRet|
648 llvm::Attribute::NoAlias));
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000649 ++Index;
Daniel Dunbare126ab12008-09-10 02:41:04 +0000650 break;
651
652 case ABIArgInfo::Coerce:
Daniel Dunbare126ab12008-09-10 02:41:04 +0000653 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +0000654
655 case ABIArgInfo::ByVal:
656 case ABIArgInfo::Expand:
657 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000658 }
Daniel Dunbare126ab12008-09-10 02:41:04 +0000659
Devang Patel2bb6eb82008-09-26 22:53:57 +0000660 if (RetAttrs)
661 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Daniel Dunbar04d35782008-09-17 00:51:38 +0000662 for (++begin; begin != end; ++begin) {
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000663 QualType ParamType = *begin;
Devang Patela85a9ef2008-09-25 21:02:23 +0000664 unsigned Attributes = 0;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000665 ABIArgInfo AI = getABIArgumentInfo(ParamType, getTypes());
Daniel Dunbar22e30052008-09-11 01:48:57 +0000666
667 switch (AI.getKind()) {
668 case ABIArgInfo::StructRet:
Daniel Dunbar04d35782008-09-17 00:51:38 +0000669 case ABIArgInfo::Coerce:
Daniel Dunbar22e30052008-09-11 01:48:57 +0000670 assert(0 && "Invalid ABI kind for non-return argument");
671
672 case ABIArgInfo::ByVal:
Devang Patela85a9ef2008-09-25 21:02:23 +0000673 Attributes |= llvm::Attribute::ByVal;
Daniel Dunbar22e30052008-09-11 01:48:57 +0000674 assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled");
675 break;
676
677 case ABIArgInfo::Default:
678 if (ParamType->isPromotableIntegerType()) {
679 if (ParamType->isSignedIntegerType()) {
Devang Patela85a9ef2008-09-25 21:02:23 +0000680 Attributes |= llvm::Attribute::SExt;
Daniel Dunbar22e30052008-09-11 01:48:57 +0000681 } else if (ParamType->isUnsignedIntegerType()) {
Devang Patela85a9ef2008-09-25 21:02:23 +0000682 Attributes |= llvm::Attribute::ZExt;
Daniel Dunbar22e30052008-09-11 01:48:57 +0000683 }
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000684 }
Daniel Dunbar22e30052008-09-11 01:48:57 +0000685 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +0000686
Daniel Dunbar04d35782008-09-17 00:51:38 +0000687 case ABIArgInfo::Expand: {
688 std::vector<const llvm::Type*> Tys;
689 // FIXME: This is rather inefficient. Do we ever actually need
690 // to do anything here? The result should be just reconstructed
691 // on the other side, so extension should be a non-issue.
692 getTypes().GetExpandedTypes(ParamType, Tys);
693 Index += Tys.size();
694 continue;
695 }
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000696 }
Daniel Dunbar22e30052008-09-11 01:48:57 +0000697
Devang Patela85a9ef2008-09-25 21:02:23 +0000698 if (Attributes)
699 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar04d35782008-09-17 00:51:38 +0000700 ++Index;
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000701 }
Devang Patel2bb6eb82008-09-26 22:53:57 +0000702 if (FuncAttrs)
703 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
704
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000705}
706
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000707void CodeGenFunction::EmitFunctionProlog(llvm::Function *Fn,
708 QualType RetTy,
709 const FunctionArgList &Args) {
710 // Emit allocs for param decls. Give the LLVM Argument nodes names.
711 llvm::Function::arg_iterator AI = Fn->arg_begin();
712
713 // Name the struct return argument.
Daniel Dunbare126ab12008-09-10 02:41:04 +0000714 if (CGM.ReturnTypeUsesSret(RetTy)) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000715 AI->setName("agg.result");
716 ++AI;
717 }
718
719 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbar04d35782008-09-17 00:51:38 +0000720 i != e; ++i) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000721 const VarDecl *Arg = i->first;
Daniel Dunbar04d35782008-09-17 00:51:38 +0000722 QualType Ty = i->second;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000723 ABIArgInfo ArgI = getABIArgumentInfo(Ty, CGM.getTypes());
Daniel Dunbar22e30052008-09-11 01:48:57 +0000724
725 switch (ArgI.getKind()) {
726 case ABIArgInfo::ByVal:
727 case ABIArgInfo::Default: {
728 assert(AI != Fn->arg_end() && "Argument mismatch!");
729 llvm::Value* V = AI;
Daniel Dunbar04d35782008-09-17 00:51:38 +0000730 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
Daniel Dunbar22e30052008-09-11 01:48:57 +0000731 // This must be a promotion, for something like
732 // "void a(x) short x; {..."
Daniel Dunbar04d35782008-09-17 00:51:38 +0000733 V = EmitScalarConversion(V, Ty, Arg->getType());
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000734 }
Daniel Dunbar22e30052008-09-11 01:48:57 +0000735 EmitParmDecl(*Arg, V);
736 break;
737 }
Daniel Dunbar04d35782008-09-17 00:51:38 +0000738
739 case ABIArgInfo::Expand: {
740 // If this was structure was expand into multiple arguments then
741 // we need to create a temporary and reconstruct it from the
742 // arguments.
Chris Lattner6c5ec622008-11-24 04:00:27 +0000743 std::string Name = Arg->getNameAsString();
Daniel Dunbar04d35782008-09-17 00:51:38 +0000744 llvm::Value *Temp = CreateTempAlloca(ConvertType(Ty),
745 (Name + ".addr").c_str());
746 // FIXME: What are the right qualifiers here?
747 llvm::Function::arg_iterator End =
748 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI);
749 EmitParmDecl(*Arg, Temp);
Daniel Dunbar22e30052008-09-11 01:48:57 +0000750
Daniel Dunbar04d35782008-09-17 00:51:38 +0000751 // Name the arguments used in expansion and increment AI.
752 unsigned Index = 0;
753 for (; AI != End; ++AI, ++Index)
754 AI->setName(Name + "." + llvm::utostr(Index));
755 continue;
756 }
757
Daniel Dunbar22e30052008-09-11 01:48:57 +0000758 case ABIArgInfo::Coerce:
Daniel Dunbar22e30052008-09-11 01:48:57 +0000759 case ABIArgInfo::StructRet:
760 assert(0 && "Invalid ABI kind for non-return argument");
761 }
Daniel Dunbar04d35782008-09-17 00:51:38 +0000762
763 ++AI;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000764 }
765 assert(AI == Fn->arg_end() && "Argument mismatch!");
766}
767
768void CodeGenFunction::EmitFunctionEpilog(QualType RetTy,
769 llvm::Value *ReturnValue) {
Daniel Dunbare126ab12008-09-10 02:41:04 +0000770 llvm::Value *RV = 0;
771
772 // Functions with no result always return void.
773 if (ReturnValue) {
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000774 ABIArgInfo RetAI = getABIReturnInfo(RetTy, CGM.getTypes());
Daniel Dunbare126ab12008-09-10 02:41:04 +0000775
776 switch (RetAI.getKind()) {
777 case ABIArgInfo::StructRet:
Daniel Dunbar17d35372008-12-18 04:52:14 +0000778 if (RetTy->isAnyComplexType()) {
779 // FIXME: Volatile
780 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
781 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
782 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
783 EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy);
784 } else {
785 Builder.CreateStore(Builder.CreateLoad(ReturnValue),
786 CurFn->arg_begin());
787 }
Daniel Dunbare126ab12008-09-10 02:41:04 +0000788 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +0000789
Daniel Dunbare126ab12008-09-10 02:41:04 +0000790 case ABIArgInfo::Default:
791 RV = Builder.CreateLoad(ReturnValue);
792 break;
793
Daniel Dunbar73d66602008-09-10 07:04:09 +0000794 case ABIArgInfo::Coerce: {
795 const llvm::Type *CoerceToPTy =
796 llvm::PointerType::getUnqual(RetAI.getCoerceToType());
797 RV = Builder.CreateLoad(Builder.CreateBitCast(ReturnValue, CoerceToPTy));
Daniel Dunbar22e30052008-09-11 01:48:57 +0000798 break;
Daniel Dunbar73d66602008-09-10 07:04:09 +0000799 }
Daniel Dunbar22e30052008-09-11 01:48:57 +0000800
801 case ABIArgInfo::ByVal:
802 case ABIArgInfo::Expand:
803 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000804 }
805 }
Daniel Dunbare126ab12008-09-10 02:41:04 +0000806
807 if (RV) {
808 Builder.CreateRet(RV);
809 } else {
810 Builder.CreateRetVoid();
811 }
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000812}
813
814RValue CodeGenFunction::EmitCall(llvm::Value *Callee,
Daniel Dunbare126ab12008-09-10 02:41:04 +0000815 QualType RetTy,
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000816 const CallArgList &CallArgs) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000817 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000818
819 // Handle struct-return functions by passing a pointer to the
820 // location that we would like to return into.
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000821 ABIArgInfo RetAI = getABIReturnInfo(RetTy, CGM.getTypes());
Daniel Dunbare126ab12008-09-10 02:41:04 +0000822 switch (RetAI.getKind()) {
823 case ABIArgInfo::StructRet:
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000824 // Create a temporary alloca to hold the result of the call. :(
Daniel Dunbar04d35782008-09-17 00:51:38 +0000825 Args.push_back(CreateTempAlloca(ConvertType(RetTy)));
Daniel Dunbare126ab12008-09-10 02:41:04 +0000826 break;
827
828 case ABIArgInfo::Default:
Daniel Dunbare126ab12008-09-10 02:41:04 +0000829 case ABIArgInfo::Coerce:
Daniel Dunbare126ab12008-09-10 02:41:04 +0000830 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +0000831
832 case ABIArgInfo::ByVal:
833 case ABIArgInfo::Expand:
834 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000835 }
836
837 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
838 I != E; ++I) {
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000839 ABIArgInfo ArgInfo = getABIArgumentInfo(I->second, CGM.getTypes());
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000840 RValue RV = I->first;
Daniel Dunbar04d35782008-09-17 00:51:38 +0000841
842 switch (ArgInfo.getKind()) {
843 case ABIArgInfo::ByVal: // Default is byval
844 case ABIArgInfo::Default:
845 if (RV.isScalar()) {
846 Args.push_back(RV.getScalarVal());
847 } else if (RV.isComplex()) {
848 // Make a temporary alloca to pass the argument.
849 Args.push_back(CreateTempAlloca(ConvertType(I->second)));
850 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
851 } else {
852 Args.push_back(RV.getAggregateAddr());
853 }
854 break;
855
856 case ABIArgInfo::StructRet:
857 case ABIArgInfo::Coerce:
858 assert(0 && "Invalid ABI kind for non-return argument");
859 break;
860
861 case ABIArgInfo::Expand:
862 ExpandTypeToArgs(I->second, RV, Args);
863 break;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000864 }
865 }
866
867 llvm::CallInst *CI = Builder.CreateCall(Callee,&Args[0],&Args[0]+Args.size());
Daniel Dunbare126ab12008-09-10 02:41:04 +0000868 CGCallInfo CallInfo(RetTy, CallArgs);
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000869
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000870 // FIXME: Provide TargetDecl so nounwind, noreturn, etc, etc get set.
Devang Patela85a9ef2008-09-25 21:02:23 +0000871 CodeGen::AttributeListType AttributeList;
872 CGM.ConstructAttributeList(0,
Daniel Dunbar3ef2e852008-09-10 00:41:16 +0000873 CallInfo.argtypes_begin(), CallInfo.argtypes_end(),
Devang Patela85a9ef2008-09-25 21:02:23 +0000874 AttributeList);
875 CI->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(),
876 AttributeList.size()));
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000877
878 if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee))
879 CI->setCallingConv(F->getCallingConv());
880 if (CI->getType() != llvm::Type::VoidTy)
881 CI->setName("call");
Daniel Dunbare126ab12008-09-10 02:41:04 +0000882
883 switch (RetAI.getKind()) {
884 case ABIArgInfo::StructRet:
885 if (RetTy->isAnyComplexType())
Daniel Dunbar04d35782008-09-17 00:51:38 +0000886 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Daniel Dunbar17d35372008-12-18 04:52:14 +0000887 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar04d35782008-09-17 00:51:38 +0000888 return RValue::getAggregate(Args[0]);
Daniel Dunbar17d35372008-12-18 04:52:14 +0000889 else
890 return RValue::get(Builder.CreateLoad(Args[0]));
Daniel Dunbar22e30052008-09-11 01:48:57 +0000891
Daniel Dunbare126ab12008-09-10 02:41:04 +0000892 case ABIArgInfo::Default:
893 return RValue::get(RetTy->isVoidType() ? 0 : CI);
894
Daniel Dunbar73d66602008-09-10 07:04:09 +0000895 case ABIArgInfo::Coerce: {
896 const llvm::Type *CoerceToPTy =
897 llvm::PointerType::getUnqual(RetAI.getCoerceToType());
898 llvm::Value *V = CreateTempAlloca(ConvertType(RetTy), "tmp");
899 Builder.CreateStore(CI, Builder.CreateBitCast(V, CoerceToPTy));
Anders Carlssonfccf7472008-11-25 22:21:48 +0000900 if (RetTy->isAnyComplexType())
901 return RValue::getComplex(LoadComplexFromAddr(V, false));
902 else
903 return RValue::getAggregate(V);
Daniel Dunbar73d66602008-09-10 07:04:09 +0000904 }
Daniel Dunbar22e30052008-09-11 01:48:57 +0000905
906 case ABIArgInfo::ByVal:
907 case ABIArgInfo::Expand:
908 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000909 }
Daniel Dunbare126ab12008-09-10 02:41:04 +0000910
911 assert(0 && "Unhandled ABIArgInfo::Kind");
912 return RValue::get(0);
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000913}