blob: a57ee1de0d621ab0d86e5174435e209de6256d3f [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/ASTContext.h"
20#include "clang/AST/Decl.h"
21#include "clang/AST/DeclObjC.h"
Daniel Dunbar56273772008-09-17 00:51:38 +000022#include "llvm/ADT/StringExtras.h"
Devang Pateld0646bd2008-09-24 01:01:36 +000023#include "llvm/Attributes.h"
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000024using namespace clang;
25using namespace CodeGen;
26
27/***/
28
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000029// FIXME: Use iterator and sidestep silly type array creation.
30
Daniel Dunbar45c25ba2008-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 Dunbar0dbe2272008-09-08 21:33:45 +000046CGFunctionInfo::CGFunctionInfo(const FunctionDecl *FD)
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000047{
48 const FunctionType *FTy = FD->getType()->getAsFunctionType();
49 const FunctionTypeProto *FTP = dyn_cast<FunctionTypeProto>(FTy);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000050
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000051 ArgTypes.push_back(FTy->getResultType());
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000052 if (FTP) {
53 IsVariadic = FTP->isVariadic();
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000054 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
55 ArgTypes.push_back(FTP->getArgType(i));
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000056 } else {
57 IsVariadic = true;
58 }
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000059}
60
61CGFunctionInfo::CGFunctionInfo(const ObjCMethodDecl *MD,
62 const ASTContext &Context)
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000063 : IsVariadic(MD->isVariadic())
Daniel Dunbar0dbe2272008-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 Dunbar5323a4b2008-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 Dunbar0dbe2272008-09-08 21:33:45 +000079}
80
81/***/
82
Daniel Dunbar5323a4b2008-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 Dunbar0dbe2272008-09-08 21:33:45 +000086 ArgTypes.push_back(i->second);
87}
88
Daniel Dunbar5323a4b2008-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 Dunbar0dbe2272008-09-08 21:33:45 +000095}
Daniel Dunbar17b708d2008-09-09 23:27:19 +000096
97/***/
98
Daniel Dunbar8951dbd2008-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 Dunbar2c8e0f32008-09-10 02:41:04 +0000101class ABIArgInfo {
102public:
103 enum Kind {
104 Default,
Daniel Dunbar3aea8ca2008-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 Dunbar8951dbd2008-09-11 01:48:57 +0000109
Daniel Dunbar56273772008-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 Dunbar8951dbd2008-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 Dunbar56273772008-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 Dunbar8951dbd2008-09-11 01:48:57 +0000124
125 KindFirst=Default, KindLast=Expand
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000126 };
127
128private:
129 Kind TheKind;
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000130 const llvm::Type *TypeData;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000131 unsigned UIntData;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000132
Daniel Dunbar8951dbd2008-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 Dunbar2c8e0f32008-09-10 02:41:04 +0000137public:
138 static ABIArgInfo getDefault() {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000139 return ABIArgInfo(Default);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000140 }
141 static ABIArgInfo getStructRet() {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000142 return ABIArgInfo(StructRet);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000143 }
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000144 static ABIArgInfo getCoerce(const llvm::Type *T) {
145 assert(T->isSingleValueType() && "Can only coerce to simple types");
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000146 return ABIArgInfo(Coerce, T);
147 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000148 static ABIArgInfo getByVal(unsigned Alignment) {
149 return ABIArgInfo(ByVal, 0, Alignment);
150 }
Daniel Dunbar56273772008-09-17 00:51:38 +0000151 static ABIArgInfo getExpand() {
152 return ABIArgInfo(Expand);
153 }
Daniel Dunbar2c8e0f32008-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 Dunbar8951dbd2008-09-11 01:48:57 +0000159 bool isByVal() const { return TheKind == ByVal; }
Daniel Dunbar56273772008-09-17 00:51:38 +0000160 bool isExpand() const { return TheKind == Expand; }
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000161
162 // Coerce accessors
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000163 const llvm::Type *getCoerceToType() const {
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000164 assert(TheKind == Coerce && "Invalid kind!");
165 return TypeData;
166 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000167
168 // ByVal accessors
169 unsigned getByValAlignment() const {
170 assert(TheKind == ByVal && "Invalid kind!");
171 return UIntData;
172 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000173};
174
175/***/
176
Daniel Dunbar6b1da0e2008-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 Dunbar834af452008-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 Gregorf8d49f62009-01-09 17:18:27 +0000208 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar834af452008-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 Gregorf8d49f62009-01-09 17:18:27 +0000235 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar834af452008-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 Gregorf8d49f62009-01-09 17:18:27 +0000266 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar834af452008-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 Dunbar6b1da0e2008-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};
306}
307
308ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
309 ASTContext &Context) const {
Daniel Dunbar6660c8a2008-09-11 00:04:36 +0000310 assert(!RetTy->isArrayType() &&
311 "Array types cannot be passed directly.");
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000312 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar834af452008-09-17 21:22:33 +0000313 // Classify "single element" structs as their element type.
314 const FieldDecl *SeltFD = isSingleElementStruct(RetTy);
315 if (SeltFD) {
316 QualType SeltTy = SeltFD->getType()->getDesugaredType();
317 if (const BuiltinType *BT = SeltTy->getAsBuiltinType()) {
318 // FIXME: This is gross, it would be nice if we could just
319 // pass back SeltTy and have clients deal with it. Is it worth
320 // supporting coerce to both LLVM and clang Types?
321 if (BT->isIntegerType()) {
322 uint64_t Size = Context.getTypeSize(SeltTy);
323 return ABIArgInfo::getCoerce(llvm::IntegerType::get((unsigned) Size));
324 } else if (BT->getKind() == BuiltinType::Float) {
325 return ABIArgInfo::getCoerce(llvm::Type::FloatTy);
326 } else if (BT->getKind() == BuiltinType::Double) {
327 return ABIArgInfo::getCoerce(llvm::Type::DoubleTy);
328 }
329 } else if (SeltTy->isPointerType()) {
330 // FIXME: It would be really nice if this could come out as
331 // the proper pointer type.
332 llvm::Type *PtrTy =
333 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
334 return ABIArgInfo::getCoerce(PtrTy);
335 }
336 }
337
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000338 uint64_t Size = Context.getTypeSize(RetTy);
339 if (Size == 8) {
340 return ABIArgInfo::getCoerce(llvm::Type::Int8Ty);
341 } else if (Size == 16) {
342 return ABIArgInfo::getCoerce(llvm::Type::Int16Ty);
343 } else if (Size == 32) {
344 return ABIArgInfo::getCoerce(llvm::Type::Int32Ty);
345 } else if (Size == 64) {
346 return ABIArgInfo::getCoerce(llvm::Type::Int64Ty);
347 } else {
348 return ABIArgInfo::getStructRet();
349 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000350 } else {
351 return ABIArgInfo::getDefault();
352 }
353}
354
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000355ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
356 ASTContext &Context) const {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000357 assert(!Ty->isArrayType() && "Array types cannot be passed directly.");
Daniel Dunbarf0357382008-09-17 20:11:04 +0000358 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar834af452008-09-17 21:22:33 +0000359 // Structures with flexible arrays are always byval.
360 if (const RecordType *RT = Ty->getAsStructureType())
361 if (RT->getDecl()->hasFlexibleArrayMember())
362 return ABIArgInfo::getByVal(0);
363
364 // Expand empty structs (i.e. ignore)
365 uint64_t Size = Context.getTypeSize(Ty);
366 if (Ty->isStructureType() && Size == 0)
367 return ABIArgInfo::getExpand();
368
369 // Expand structs with size <= 128-bits which consist only of
370 // basic types (int, long long, float, double, xxx*). This is
371 // non-recursive and does not ignore empty fields.
372 if (const RecordType *RT = Ty->getAsStructureType()) {
373 if (Context.getTypeSize(Ty) <= 4*32 &&
374 areAllFields32Or64BitBasicType(RT->getDecl(), Context))
375 return ABIArgInfo::getExpand();
376 }
377
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000378 return ABIArgInfo::getByVal(0);
379 } else {
380 return ABIArgInfo::getDefault();
381 }
382}
383
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000384ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
385 ASTContext &Context) const {
386 return ABIArgInfo::getDefault();
387}
388
389ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
390 ASTContext &Context) const {
391 assert(!Ty->isArrayType() && "Array types cannot be passed directly.");
392 return ABIArgInfo::getDefault();
393}
394
395const ABIInfo &CodeGenTypes::getABIInfo() const {
396 if (TheABIInfo)
397 return *TheABIInfo;
398
399 // For now we just cache this in the CodeGenTypes and don't bother
400 // to free it.
401 const char *TargetPrefix = getContext().Target.getTargetPrefix();
402 if (strcmp(TargetPrefix, "x86") == 0) {
403 if (getContext().Target.getPointerWidth(0) == 32)
404 return *(TheABIInfo = new X86_32ABIInfo());
405 }
406
407 return *(TheABIInfo = new DefaultABIInfo);
408}
409
410// getABIReturnInfo - Wrap the ABIInfo getABIReturnInfo, altering
411// "default" types to StructRet when appropriate for simplicity.
412static ABIArgInfo getABIReturnInfo(QualType Ty, CodeGenTypes &CGT) {
413 assert(!Ty->isArrayType() &&
414 "Array types cannot be passed directly.");
415 ABIArgInfo Info = CGT.getABIInfo().classifyReturnType(Ty, CGT.getContext());
Daniel Dunbarf0357382008-09-17 20:11:04 +0000416 // Ensure default on aggregate types is StructRet.
417 if (Info.isDefault() && CodeGenFunction::hasAggregateLLVMType(Ty))
418 return ABIArgInfo::getStructRet();
419 return Info;
420}
421
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000422// getABIArgumentInfo - Wrap the ABIInfo getABIReturnInfo, altering
423// "default" types to ByVal when appropriate for simplicity.
424static ABIArgInfo getABIArgumentInfo(QualType Ty, CodeGenTypes &CGT) {
425 assert(!Ty->isArrayType() &&
426 "Array types cannot be passed directly.");
427 ABIArgInfo Info = CGT.getABIInfo().classifyArgumentType(Ty, CGT.getContext());
Daniel Dunbarf0357382008-09-17 20:11:04 +0000428 // Ensure default on aggregate types is ByVal.
429 if (Info.isDefault() && CodeGenFunction::hasAggregateLLVMType(Ty))
430 return ABIArgInfo::getByVal(0);
431 return Info;
432}
433
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000434/***/
435
Daniel Dunbar56273772008-09-17 00:51:38 +0000436void CodeGenTypes::GetExpandedTypes(QualType Ty,
437 std::vector<const llvm::Type*> &ArgTys) {
438 const RecordType *RT = Ty->getAsStructureType();
439 assert(RT && "Can only expand structure types.");
440 const RecordDecl *RD = RT->getDecl();
441 assert(!RD->hasFlexibleArrayMember() &&
442 "Cannot expand structure with flexible array.");
443
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000444 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar56273772008-09-17 00:51:38 +0000445 e = RD->field_end(); i != e; ++i) {
446 const FieldDecl *FD = *i;
447 assert(!FD->isBitField() &&
448 "Cannot expand structure with bit-field members.");
449
450 QualType FT = FD->getType();
451 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
452 GetExpandedTypes(FT, ArgTys);
453 } else {
454 ArgTys.push_back(ConvertType(FT));
455 }
456 }
457}
458
459llvm::Function::arg_iterator
460CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
461 llvm::Function::arg_iterator AI) {
462 const RecordType *RT = Ty->getAsStructureType();
463 assert(RT && "Can only expand structure types.");
464
465 RecordDecl *RD = RT->getDecl();
466 assert(LV.isSimple() &&
467 "Unexpected non-simple lvalue during struct expansion.");
468 llvm::Value *Addr = LV.getAddress();
469 for (RecordDecl::field_iterator i = RD->field_begin(),
470 e = RD->field_end(); i != e; ++i) {
471 FieldDecl *FD = *i;
472 QualType FT = FD->getType();
473
474 // FIXME: What are the right qualifiers here?
475 LValue LV = EmitLValueForField(Addr, FD, false, 0);
476 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
477 AI = ExpandTypeFromArgs(FT, LV, AI);
478 } else {
479 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
480 ++AI;
481 }
482 }
483
484 return AI;
485}
486
487void
488CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
489 llvm::SmallVector<llvm::Value*, 16> &Args) {
490 const RecordType *RT = Ty->getAsStructureType();
491 assert(RT && "Can only expand structure types.");
492
493 RecordDecl *RD = RT->getDecl();
494 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
495 llvm::Value *Addr = RV.getAggregateAddr();
496 for (RecordDecl::field_iterator i = RD->field_begin(),
497 e = RD->field_end(); i != e; ++i) {
498 FieldDecl *FD = *i;
499 QualType FT = FD->getType();
500
501 // FIXME: What are the right qualifiers here?
502 LValue LV = EmitLValueForField(Addr, FD, false, 0);
503 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
504 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
505 } else {
506 RValue RV = EmitLoadOfLValue(LV, FT);
507 assert(RV.isScalar() &&
508 "Unexpected non-scalar rvalue during struct expansion.");
509 Args.push_back(RV.getScalarVal());
510 }
511 }
512}
513
514/***/
515
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000516const llvm::FunctionType *
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000517CodeGenTypes::GetFunctionType(const CGCallInfo &CI, bool IsVariadic) {
518 return GetFunctionType(CI.argtypes_begin(), CI.argtypes_end(), IsVariadic);
519}
520
521const llvm::FunctionType *
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000522CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI) {
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000523 return GetFunctionType(FI.argtypes_begin(), FI.argtypes_end(), FI.isVariadic());
524}
525
526const llvm::FunctionType *
527CodeGenTypes::GetFunctionType(ArgTypeIterator begin, ArgTypeIterator end,
528 bool IsVariadic) {
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000529 std::vector<const llvm::Type*> ArgTys;
530
531 const llvm::Type *ResultType = 0;
532
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000533 QualType RetTy = *begin;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000534 ABIArgInfo RetAI = getABIReturnInfo(RetTy, *this);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000535 switch (RetAI.getKind()) {
536 case ABIArgInfo::ByVal:
537 case ABIArgInfo::Expand:
538 assert(0 && "Invalid ABI kind for return argument");
539
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000540 case ABIArgInfo::Default:
541 if (RetTy->isVoidType()) {
542 ResultType = llvm::Type::VoidTy;
543 } else {
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000544 ResultType = ConvertType(RetTy);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000545 }
546 break;
547
548 case ABIArgInfo::StructRet: {
549 ResultType = llvm::Type::VoidTy;
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000550 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000551 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
552 break;
553 }
554
555 case ABIArgInfo::Coerce:
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000556 ResultType = RetAI.getCoerceToType();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000557 break;
558 }
559
560 for (++begin; begin != end; ++begin) {
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000561 ABIArgInfo AI = getABIArgumentInfo(*begin, *this);
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000562 const llvm::Type *Ty = ConvertType(*begin);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000563
564 switch (AI.getKind()) {
Daniel Dunbar56273772008-09-17 00:51:38 +0000565 case ABIArgInfo::Coerce:
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000566 case ABIArgInfo::StructRet:
567 assert(0 && "Invalid ABI kind for non-return argument");
568
569 case ABIArgInfo::ByVal:
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000570 // byval arguments are always on the stack, which is addr space #0.
571 ArgTys.push_back(llvm::PointerType::getUnqual(Ty));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000572 assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled");
573 break;
574
575 case ABIArgInfo::Default:
576 ArgTys.push_back(Ty);
577 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000578
579 case ABIArgInfo::Expand:
Daniel Dunbar56273772008-09-17 00:51:38 +0000580 GetExpandedTypes(*begin, ArgTys);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000581 break;
582 }
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000583 }
584
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000585 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000586}
587
Daniel Dunbarb7688072008-09-10 00:41:16 +0000588bool CodeGenModule::ReturnTypeUsesSret(QualType RetTy) {
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000589 return getABIReturnInfo(RetTy, getTypes()).isStructRet();
Daniel Dunbar3913f182008-09-09 23:48:28 +0000590}
591
Devang Patel761d7f72008-09-25 21:02:23 +0000592void CodeGenModule::ConstructAttributeList(const Decl *TargetDecl,
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000593 ArgTypeIterator begin,
594 ArgTypeIterator end,
Devang Patel761d7f72008-09-25 21:02:23 +0000595 AttributeListType &PAL) {
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000596 unsigned FuncAttrs = 0;
Devang Patela2c69122008-09-26 22:53:57 +0000597 unsigned RetAttrs = 0;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000598
599 if (TargetDecl) {
600 if (TargetDecl->getAttr<NoThrowAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +0000601 FuncAttrs |= llvm::Attribute::NoUnwind;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000602 if (TargetDecl->getAttr<NoReturnAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +0000603 FuncAttrs |= llvm::Attribute::NoReturn;
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000604 if (TargetDecl->getAttr<PureAttr>())
605 FuncAttrs |= llvm::Attribute::ReadOnly;
606 if (TargetDecl->getAttr<ConstAttr>())
607 FuncAttrs |= llvm::Attribute::ReadNone;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000608 }
609
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000610 QualType RetTy = *begin;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000611 unsigned Index = 1;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000612 ABIArgInfo RetAI = getABIReturnInfo(RetTy, getTypes());
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000613 switch (RetAI.getKind()) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000614 case ABIArgInfo::Default:
615 if (RetTy->isPromotableIntegerType()) {
616 if (RetTy->isSignedIntegerType()) {
Devang Patela2c69122008-09-26 22:53:57 +0000617 RetAttrs |= llvm::Attribute::SExt;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000618 } else if (RetTy->isUnsignedIntegerType()) {
Devang Patela2c69122008-09-26 22:53:57 +0000619 RetAttrs |= llvm::Attribute::ZExt;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000620 }
621 }
622 break;
623
624 case ABIArgInfo::StructRet:
Devang Patel761d7f72008-09-25 21:02:23 +0000625 PAL.push_back(llvm::AttributeWithIndex::get(Index,
626 llvm::Attribute::StructRet|
627 llvm::Attribute::NoAlias));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000628 ++Index;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000629 break;
630
631 case ABIArgInfo::Coerce:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000632 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000633
634 case ABIArgInfo::ByVal:
635 case ABIArgInfo::Expand:
636 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000637 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000638
Devang Patela2c69122008-09-26 22:53:57 +0000639 if (RetAttrs)
640 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Daniel Dunbar56273772008-09-17 00:51:38 +0000641 for (++begin; begin != end; ++begin) {
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000642 QualType ParamType = *begin;
Devang Patel761d7f72008-09-25 21:02:23 +0000643 unsigned Attributes = 0;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000644 ABIArgInfo AI = getABIArgumentInfo(ParamType, getTypes());
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000645
646 switch (AI.getKind()) {
647 case ABIArgInfo::StructRet:
Daniel Dunbar56273772008-09-17 00:51:38 +0000648 case ABIArgInfo::Coerce:
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000649 assert(0 && "Invalid ABI kind for non-return argument");
650
651 case ABIArgInfo::ByVal:
Devang Patel761d7f72008-09-25 21:02:23 +0000652 Attributes |= llvm::Attribute::ByVal;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000653 assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled");
654 break;
655
656 case ABIArgInfo::Default:
657 if (ParamType->isPromotableIntegerType()) {
658 if (ParamType->isSignedIntegerType()) {
Devang Patel761d7f72008-09-25 21:02:23 +0000659 Attributes |= llvm::Attribute::SExt;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000660 } else if (ParamType->isUnsignedIntegerType()) {
Devang Patel761d7f72008-09-25 21:02:23 +0000661 Attributes |= llvm::Attribute::ZExt;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000662 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000663 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000664 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000665
Daniel Dunbar56273772008-09-17 00:51:38 +0000666 case ABIArgInfo::Expand: {
667 std::vector<const llvm::Type*> Tys;
668 // FIXME: This is rather inefficient. Do we ever actually need
669 // to do anything here? The result should be just reconstructed
670 // on the other side, so extension should be a non-issue.
671 getTypes().GetExpandedTypes(ParamType, Tys);
672 Index += Tys.size();
673 continue;
674 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000675 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000676
Devang Patel761d7f72008-09-25 21:02:23 +0000677 if (Attributes)
678 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar56273772008-09-17 00:51:38 +0000679 ++Index;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000680 }
Devang Patela2c69122008-09-26 22:53:57 +0000681 if (FuncAttrs)
682 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
683
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000684}
685
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000686void CodeGenFunction::EmitFunctionProlog(llvm::Function *Fn,
687 QualType RetTy,
688 const FunctionArgList &Args) {
689 // Emit allocs for param decls. Give the LLVM Argument nodes names.
690 llvm::Function::arg_iterator AI = Fn->arg_begin();
691
692 // Name the struct return argument.
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000693 if (CGM.ReturnTypeUsesSret(RetTy)) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000694 AI->setName("agg.result");
695 ++AI;
696 }
697
698 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbar56273772008-09-17 00:51:38 +0000699 i != e; ++i) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000700 const VarDecl *Arg = i->first;
Daniel Dunbar56273772008-09-17 00:51:38 +0000701 QualType Ty = i->second;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000702 ABIArgInfo ArgI = getABIArgumentInfo(Ty, CGM.getTypes());
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000703
704 switch (ArgI.getKind()) {
705 case ABIArgInfo::ByVal:
706 case ABIArgInfo::Default: {
707 assert(AI != Fn->arg_end() && "Argument mismatch!");
708 llvm::Value* V = AI;
Daniel Dunbar56273772008-09-17 00:51:38 +0000709 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000710 // This must be a promotion, for something like
711 // "void a(x) short x; {..."
Daniel Dunbar56273772008-09-17 00:51:38 +0000712 V = EmitScalarConversion(V, Ty, Arg->getType());
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000713 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000714 EmitParmDecl(*Arg, V);
715 break;
716 }
Daniel Dunbar56273772008-09-17 00:51:38 +0000717
718 case ABIArgInfo::Expand: {
719 // If this was structure was expand into multiple arguments then
720 // we need to create a temporary and reconstruct it from the
721 // arguments.
Chris Lattner39f34e92008-11-24 04:00:27 +0000722 std::string Name = Arg->getNameAsString();
Daniel Dunbar56273772008-09-17 00:51:38 +0000723 llvm::Value *Temp = CreateTempAlloca(ConvertType(Ty),
724 (Name + ".addr").c_str());
725 // FIXME: What are the right qualifiers here?
726 llvm::Function::arg_iterator End =
727 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI);
728 EmitParmDecl(*Arg, Temp);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000729
Daniel Dunbar56273772008-09-17 00:51:38 +0000730 // Name the arguments used in expansion and increment AI.
731 unsigned Index = 0;
732 for (; AI != End; ++AI, ++Index)
733 AI->setName(Name + "." + llvm::utostr(Index));
734 continue;
735 }
736
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000737 case ABIArgInfo::Coerce:
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000738 case ABIArgInfo::StructRet:
739 assert(0 && "Invalid ABI kind for non-return argument");
740 }
Daniel Dunbar56273772008-09-17 00:51:38 +0000741
742 ++AI;
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000743 }
744 assert(AI == Fn->arg_end() && "Argument mismatch!");
745}
746
747void CodeGenFunction::EmitFunctionEpilog(QualType RetTy,
748 llvm::Value *ReturnValue) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000749 llvm::Value *RV = 0;
750
751 // Functions with no result always return void.
752 if (ReturnValue) {
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000753 ABIArgInfo RetAI = getABIReturnInfo(RetTy, CGM.getTypes());
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000754
755 switch (RetAI.getKind()) {
756 case ABIArgInfo::StructRet:
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +0000757 if (RetTy->isAnyComplexType()) {
758 // FIXME: Volatile
759 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
760 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
761 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
762 EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy);
763 } else {
764 Builder.CreateStore(Builder.CreateLoad(ReturnValue),
765 CurFn->arg_begin());
766 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000767 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000768
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000769 case ABIArgInfo::Default:
770 RV = Builder.CreateLoad(ReturnValue);
771 break;
772
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000773 case ABIArgInfo::Coerce: {
774 const llvm::Type *CoerceToPTy =
775 llvm::PointerType::getUnqual(RetAI.getCoerceToType());
776 RV = Builder.CreateLoad(Builder.CreateBitCast(ReturnValue, CoerceToPTy));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000777 break;
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000778 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000779
780 case ABIArgInfo::ByVal:
781 case ABIArgInfo::Expand:
782 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000783 }
784 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000785
786 if (RV) {
787 Builder.CreateRet(RV);
788 } else {
789 Builder.CreateRetVoid();
790 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000791}
792
793RValue CodeGenFunction::EmitCall(llvm::Value *Callee,
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000794 QualType RetTy,
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000795 const CallArgList &CallArgs) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000796 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000797
798 // Handle struct-return functions by passing a pointer to the
799 // location that we would like to return into.
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000800 ABIArgInfo RetAI = getABIReturnInfo(RetTy, CGM.getTypes());
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000801 switch (RetAI.getKind()) {
802 case ABIArgInfo::StructRet:
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000803 // Create a temporary alloca to hold the result of the call. :(
Daniel Dunbar56273772008-09-17 00:51:38 +0000804 Args.push_back(CreateTempAlloca(ConvertType(RetTy)));
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000805 break;
806
807 case ABIArgInfo::Default:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000808 case ABIArgInfo::Coerce:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000809 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000810
811 case ABIArgInfo::ByVal:
812 case ABIArgInfo::Expand:
813 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000814 }
815
816 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
817 I != E; ++I) {
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000818 ABIArgInfo ArgInfo = getABIArgumentInfo(I->second, CGM.getTypes());
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000819 RValue RV = I->first;
Daniel Dunbar56273772008-09-17 00:51:38 +0000820
821 switch (ArgInfo.getKind()) {
822 case ABIArgInfo::ByVal: // Default is byval
823 case ABIArgInfo::Default:
824 if (RV.isScalar()) {
825 Args.push_back(RV.getScalarVal());
826 } else if (RV.isComplex()) {
827 // Make a temporary alloca to pass the argument.
828 Args.push_back(CreateTempAlloca(ConvertType(I->second)));
829 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
830 } else {
831 Args.push_back(RV.getAggregateAddr());
832 }
833 break;
834
835 case ABIArgInfo::StructRet:
836 case ABIArgInfo::Coerce:
837 assert(0 && "Invalid ABI kind for non-return argument");
838 break;
839
840 case ABIArgInfo::Expand:
841 ExpandTypeToArgs(I->second, RV, Args);
842 break;
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000843 }
844 }
845
846 llvm::CallInst *CI = Builder.CreateCall(Callee,&Args[0],&Args[0]+Args.size());
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000847 CGCallInfo CallInfo(RetTy, CallArgs);
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000848
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000849 // FIXME: Provide TargetDecl so nounwind, noreturn, etc, etc get set.
Devang Patel761d7f72008-09-25 21:02:23 +0000850 CodeGen::AttributeListType AttributeList;
851 CGM.ConstructAttributeList(0,
Daniel Dunbarb7688072008-09-10 00:41:16 +0000852 CallInfo.argtypes_begin(), CallInfo.argtypes_end(),
Devang Patel761d7f72008-09-25 21:02:23 +0000853 AttributeList);
854 CI->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(),
855 AttributeList.size()));
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000856
857 if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee))
858 CI->setCallingConv(F->getCallingConv());
859 if (CI->getType() != llvm::Type::VoidTy)
860 CI->setName("call");
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000861
862 switch (RetAI.getKind()) {
863 case ABIArgInfo::StructRet:
864 if (RetTy->isAnyComplexType())
Daniel Dunbar56273772008-09-17 00:51:38 +0000865 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +0000866 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar56273772008-09-17 00:51:38 +0000867 return RValue::getAggregate(Args[0]);
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +0000868 else
869 return RValue::get(Builder.CreateLoad(Args[0]));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000870
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000871 case ABIArgInfo::Default:
872 return RValue::get(RetTy->isVoidType() ? 0 : CI);
873
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000874 case ABIArgInfo::Coerce: {
875 const llvm::Type *CoerceToPTy =
876 llvm::PointerType::getUnqual(RetAI.getCoerceToType());
877 llvm::Value *V = CreateTempAlloca(ConvertType(RetTy), "tmp");
878 Builder.CreateStore(CI, Builder.CreateBitCast(V, CoerceToPTy));
Anders Carlssonad3d6912008-11-25 22:21:48 +0000879 if (RetTy->isAnyComplexType())
880 return RValue::getComplex(LoadComplexFromAddr(V, false));
881 else
882 return RValue::getAggregate(V);
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000883 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000884
885 case ABIArgInfo::ByVal:
886 case ABIArgInfo::Expand:
887 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000888 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000889
890 assert(0 && "Unhandled ABIArgInfo::Kind");
891 return RValue::get(0);
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000892}