blob: cf0d4cc79bc57c96747115a42698b3c936611396 [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 Dunbare09a9692009-01-24 08:32:22 +000024#include "llvm/Support/CommandLine.h"
Daniel Dunbara8f02052008-09-08 21:33:45 +000025using namespace clang;
26using namespace CodeGen;
27
Daniel Dunbare09a9692009-01-24 08:32:22 +000028static llvm::cl::opt<bool>
29UseX86_64ABI("use-x86_64-abi",
30 llvm::cl::desc("Enable use of experimental x86_64 ABI."),
31 llvm::cl::init(false));
32
Daniel Dunbara8f02052008-09-08 21:33:45 +000033/***/
34
Daniel Dunbara8f02052008-09-08 21:33:45 +000035// FIXME: Use iterator and sidestep silly type array creation.
36
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000037CGFunctionInfo::CGFunctionInfo(const FunctionTypeNoProto *FTNP)
38 : IsVariadic(true)
39{
40 ArgTypes.push_back(FTNP->getResultType());
41}
42
43CGFunctionInfo::CGFunctionInfo(const FunctionTypeProto *FTP)
44 : IsVariadic(FTP->isVariadic())
45{
46 ArgTypes.push_back(FTP->getResultType());
47 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
48 ArgTypes.push_back(FTP->getArgType(i));
49}
50
51// FIXME: Is there really any reason to have this still?
Daniel Dunbara8f02052008-09-08 21:33:45 +000052CGFunctionInfo::CGFunctionInfo(const FunctionDecl *FD)
Daniel Dunbara8f02052008-09-08 21:33:45 +000053{
54 const FunctionType *FTy = FD->getType()->getAsFunctionType();
55 const FunctionTypeProto *FTP = dyn_cast<FunctionTypeProto>(FTy);
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000056
Daniel Dunbara8f02052008-09-08 21:33:45 +000057 ArgTypes.push_back(FTy->getResultType());
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000058 if (FTP) {
59 IsVariadic = FTP->isVariadic();
Daniel Dunbara8f02052008-09-08 21:33:45 +000060 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
61 ArgTypes.push_back(FTP->getArgType(i));
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000062 } else {
63 IsVariadic = true;
64 }
Daniel Dunbara8f02052008-09-08 21:33:45 +000065}
66
67CGFunctionInfo::CGFunctionInfo(const ObjCMethodDecl *MD,
68 const ASTContext &Context)
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000069 : IsVariadic(MD->isVariadic())
Daniel Dunbara8f02052008-09-08 21:33:45 +000070{
71 ArgTypes.push_back(MD->getResultType());
72 ArgTypes.push_back(MD->getSelfDecl()->getType());
73 ArgTypes.push_back(Context.getObjCSelType());
74 for (ObjCMethodDecl::param_const_iterator i = MD->param_begin(),
75 e = MD->param_end(); i != e; ++i)
76 ArgTypes.push_back((*i)->getType());
77}
78
Daniel Dunbarbccb0682008-09-10 00:32:18 +000079ArgTypeIterator CGFunctionInfo::argtypes_begin() const {
80 return ArgTypes.begin();
81}
82
83ArgTypeIterator CGFunctionInfo::argtypes_end() const {
84 return ArgTypes.end();
Daniel Dunbara8f02052008-09-08 21:33:45 +000085}
86
87/***/
88
Daniel Dunbarbccb0682008-09-10 00:32:18 +000089CGCallInfo::CGCallInfo(QualType _ResultType, const CallArgList &_Args) {
90 ArgTypes.push_back(_ResultType);
91 for (CallArgList::const_iterator i = _Args.begin(), e = _Args.end(); i!=e; ++i)
Daniel Dunbara8f02052008-09-08 21:33:45 +000092 ArgTypes.push_back(i->second);
93}
94
Daniel Dunbarbccb0682008-09-10 00:32:18 +000095ArgTypeIterator CGCallInfo::argtypes_begin() const {
96 return ArgTypes.begin();
97}
98
99ArgTypeIterator CGCallInfo::argtypes_end() const {
100 return ArgTypes.end();
Daniel Dunbara8f02052008-09-08 21:33:45 +0000101}
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000102
103/***/
104
Daniel Dunbar22e30052008-09-11 01:48:57 +0000105/// ABIArgInfo - Helper class to encapsulate information about how a
106/// specific C type should be passed to or returned from a function.
Daniel Dunbare126ab12008-09-10 02:41:04 +0000107class ABIArgInfo {
108public:
109 enum Kind {
110 Default,
Daniel Dunbar17d35372008-12-18 04:52:14 +0000111 StructRet, /// Only valid for return values. The return value
112 /// should be passed through a pointer to a caller
113 /// allocated location passed as an implicit first
114 /// argument to the function.
Daniel Dunbar22e30052008-09-11 01:48:57 +0000115
Daniel Dunbar04d35782008-09-17 00:51:38 +0000116 Coerce, /// Only valid for aggregate return types, the argument
117 /// should be accessed by coercion to a provided type.
Daniel Dunbar22e30052008-09-11 01:48:57 +0000118
119 ByVal, /// Only valid for aggregate argument types. The
120 /// structure should be passed "byval" with the
121 /// specified alignment (0 indicates default
122 /// alignment).
123
124 Expand, /// Only valid for aggregate argument types. The
125 /// structure should be expanded into consecutive
Daniel Dunbar04d35782008-09-17 00:51:38 +0000126 /// arguments for its constituent fields. Currently
127 /// expand is only allowed on structures whose fields
128 /// are all scalar types or are themselves expandable
129 /// types.
Daniel Dunbar22e30052008-09-11 01:48:57 +0000130
131 KindFirst=Default, KindLast=Expand
Daniel Dunbare126ab12008-09-10 02:41:04 +0000132 };
133
134private:
135 Kind TheKind;
Daniel Dunbar73d66602008-09-10 07:04:09 +0000136 const llvm::Type *TypeData;
Daniel Dunbar22e30052008-09-11 01:48:57 +0000137 unsigned UIntData;
Daniel Dunbare126ab12008-09-10 02:41:04 +0000138
Daniel Dunbar22e30052008-09-11 01:48:57 +0000139 ABIArgInfo(Kind K, const llvm::Type *TD=0,
140 unsigned UI=0) : TheKind(K),
141 TypeData(TD),
142 UIntData(0) {}
Daniel Dunbare126ab12008-09-10 02:41:04 +0000143public:
144 static ABIArgInfo getDefault() {
Daniel Dunbar22e30052008-09-11 01:48:57 +0000145 return ABIArgInfo(Default);
Daniel Dunbare126ab12008-09-10 02:41:04 +0000146 }
147 static ABIArgInfo getStructRet() {
Daniel Dunbar22e30052008-09-11 01:48:57 +0000148 return ABIArgInfo(StructRet);
Daniel Dunbare126ab12008-09-10 02:41:04 +0000149 }
Daniel Dunbar73d66602008-09-10 07:04:09 +0000150 static ABIArgInfo getCoerce(const llvm::Type *T) {
151 assert(T->isSingleValueType() && "Can only coerce to simple types");
Daniel Dunbare126ab12008-09-10 02:41:04 +0000152 return ABIArgInfo(Coerce, T);
153 }
Daniel Dunbar22e30052008-09-11 01:48:57 +0000154 static ABIArgInfo getByVal(unsigned Alignment) {
155 return ABIArgInfo(ByVal, 0, Alignment);
156 }
Daniel Dunbar04d35782008-09-17 00:51:38 +0000157 static ABIArgInfo getExpand() {
158 return ABIArgInfo(Expand);
159 }
Daniel Dunbare126ab12008-09-10 02:41:04 +0000160
161 Kind getKind() const { return TheKind; }
162 bool isDefault() const { return TheKind == Default; }
163 bool isStructRet() const { return TheKind == StructRet; }
164 bool isCoerce() const { return TheKind == Coerce; }
Daniel Dunbar22e30052008-09-11 01:48:57 +0000165 bool isByVal() const { return TheKind == ByVal; }
Daniel Dunbar04d35782008-09-17 00:51:38 +0000166 bool isExpand() const { return TheKind == Expand; }
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000167
168 // Coerce accessors
Daniel Dunbar73d66602008-09-10 07:04:09 +0000169 const llvm::Type *getCoerceToType() const {
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000170 assert(TheKind == Coerce && "Invalid kind!");
171 return TypeData;
172 }
Daniel Dunbar22e30052008-09-11 01:48:57 +0000173
174 // ByVal accessors
175 unsigned getByValAlignment() const {
176 assert(TheKind == ByVal && "Invalid kind!");
177 return UIntData;
178 }
Daniel Dunbare126ab12008-09-10 02:41:04 +0000179};
180
181/***/
182
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000183/* FIXME: All of this stuff should be part of the target interface
184 somehow. It is currently here because it is not clear how to factor
185 the targets to support this, since the Targets currently live in a
186 layer below types n'stuff.
187 */
188
189/// ABIInfo - Target specific hooks for defining how a type should be
190/// passed or returned from functions.
191class clang::ABIInfo {
192public:
193 virtual ~ABIInfo();
194
195 virtual ABIArgInfo classifyReturnType(QualType RetTy,
196 ASTContext &Context) const = 0;
197
198 virtual ABIArgInfo classifyArgumentType(QualType Ty,
199 ASTContext &Context) const = 0;
200};
201
202ABIInfo::~ABIInfo() {}
203
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000204/// isEmptyStruct - Return true iff a structure has no non-empty
205/// members. Note that a structure with a flexible array member is not
206/// considered empty.
207static bool isEmptyStruct(QualType T) {
208 const RecordType *RT = T->getAsStructureType();
209 if (!RT)
210 return 0;
211 const RecordDecl *RD = RT->getDecl();
212 if (RD->hasFlexibleArrayMember())
213 return false;
Douglas Gregor5d764842009-01-09 17:18:27 +0000214 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000215 e = RD->field_end(); i != e; ++i) {
216 const FieldDecl *FD = *i;
217 if (!isEmptyStruct(FD->getType()))
218 return false;
219 }
220 return true;
221}
222
223/// isSingleElementStruct - Determine if a structure is a "single
224/// element struct", i.e. it has exactly one non-empty field or
225/// exactly one field which is itself a single element
226/// struct. Structures with flexible array members are never
227/// considered single element structs.
228///
229/// \return The field declaration for the single non-empty field, if
230/// it exists.
231static const FieldDecl *isSingleElementStruct(QualType T) {
232 const RecordType *RT = T->getAsStructureType();
233 if (!RT)
234 return 0;
235
236 const RecordDecl *RD = RT->getDecl();
237 if (RD->hasFlexibleArrayMember())
238 return 0;
239
240 const FieldDecl *Found = 0;
Douglas Gregor5d764842009-01-09 17:18:27 +0000241 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000242 e = RD->field_end(); i != e; ++i) {
243 const FieldDecl *FD = *i;
244 QualType FT = FD->getType();
245
246 if (isEmptyStruct(FT)) {
247 // Ignore
248 } else if (Found) {
249 return 0;
250 } else if (!CodeGenFunction::hasAggregateLLVMType(FT)) {
251 Found = FD;
252 } else {
253 Found = isSingleElementStruct(FT);
254 if (!Found)
255 return 0;
256 }
257 }
258
259 return Found;
260}
261
262static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
263 if (!Ty->getAsBuiltinType() && !Ty->isPointerType())
264 return false;
265
266 uint64_t Size = Context.getTypeSize(Ty);
267 return Size == 32 || Size == 64;
268}
269
270static bool areAllFields32Or64BitBasicType(const RecordDecl *RD,
271 ASTContext &Context) {
Douglas Gregor5d764842009-01-09 17:18:27 +0000272 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000273 e = RD->field_end(); i != e; ++i) {
274 const FieldDecl *FD = *i;
275
276 if (!is32Or64BitBasicType(FD->getType(), Context))
277 return false;
278
279 // If this is a bit-field we need to make sure it is still a
280 // 32-bit or 64-bit type.
281 if (Expr *BW = FD->getBitWidth()) {
282 unsigned Width = BW->getIntegerConstantExprValue(Context).getZExtValue();
283 if (Width <= 16)
284 return false;
285 }
286 }
287 return true;
288}
289
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000290namespace {
291/// DefaultABIInfo - The default implementation for ABI specific
292/// details. This implementation provides information which results in
293/// sensible LLVM IR generation, but does not conform to any
294/// particular ABI.
295class DefaultABIInfo : public ABIInfo {
296 virtual ABIArgInfo classifyReturnType(QualType RetTy,
297 ASTContext &Context) const;
298
299 virtual ABIArgInfo classifyArgumentType(QualType RetTy,
300 ASTContext &Context) const;
301};
302
303/// X86_32ABIInfo - The X86-32 ABI information.
304class X86_32ABIInfo : public ABIInfo {
305public:
306 virtual ABIArgInfo classifyReturnType(QualType RetTy,
307 ASTContext &Context) const;
308
309 virtual ABIArgInfo classifyArgumentType(QualType RetTy,
310 ASTContext &Context) const;
311};
312}
313
314ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
315 ASTContext &Context) const {
Daniel Dunbare126ab12008-09-10 02:41:04 +0000316 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000317 // Classify "single element" structs as their element type.
318 const FieldDecl *SeltFD = isSingleElementStruct(RetTy);
319 if (SeltFD) {
320 QualType SeltTy = SeltFD->getType()->getDesugaredType();
321 if (const BuiltinType *BT = SeltTy->getAsBuiltinType()) {
322 // FIXME: This is gross, it would be nice if we could just
323 // pass back SeltTy and have clients deal with it. Is it worth
324 // supporting coerce to both LLVM and clang Types?
325 if (BT->isIntegerType()) {
326 uint64_t Size = Context.getTypeSize(SeltTy);
327 return ABIArgInfo::getCoerce(llvm::IntegerType::get((unsigned) Size));
328 } else if (BT->getKind() == BuiltinType::Float) {
329 return ABIArgInfo::getCoerce(llvm::Type::FloatTy);
330 } else if (BT->getKind() == BuiltinType::Double) {
331 return ABIArgInfo::getCoerce(llvm::Type::DoubleTy);
332 }
333 } else if (SeltTy->isPointerType()) {
334 // FIXME: It would be really nice if this could come out as
335 // the proper pointer type.
336 llvm::Type *PtrTy =
337 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
338 return ABIArgInfo::getCoerce(PtrTy);
339 }
340 }
341
Daniel Dunbar73d66602008-09-10 07:04:09 +0000342 uint64_t Size = Context.getTypeSize(RetTy);
343 if (Size == 8) {
344 return ABIArgInfo::getCoerce(llvm::Type::Int8Ty);
345 } else if (Size == 16) {
346 return ABIArgInfo::getCoerce(llvm::Type::Int16Ty);
347 } else if (Size == 32) {
348 return ABIArgInfo::getCoerce(llvm::Type::Int32Ty);
349 } else if (Size == 64) {
350 return ABIArgInfo::getCoerce(llvm::Type::Int64Ty);
351 } else {
352 return ABIArgInfo::getStructRet();
353 }
Daniel Dunbare126ab12008-09-10 02:41:04 +0000354 } else {
355 return ABIArgInfo::getDefault();
356 }
357}
358
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000359ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
360 ASTContext &Context) const {
Daniel Dunbar3158c592008-09-17 20:11:04 +0000361 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000362 // Structures with flexible arrays are always byval.
363 if (const RecordType *RT = Ty->getAsStructureType())
364 if (RT->getDecl()->hasFlexibleArrayMember())
365 return ABIArgInfo::getByVal(0);
366
367 // Expand empty structs (i.e. ignore)
368 uint64_t Size = Context.getTypeSize(Ty);
369 if (Ty->isStructureType() && Size == 0)
370 return ABIArgInfo::getExpand();
371
372 // Expand structs with size <= 128-bits which consist only of
373 // basic types (int, long long, float, double, xxx*). This is
374 // non-recursive and does not ignore empty fields.
375 if (const RecordType *RT = Ty->getAsStructureType()) {
376 if (Context.getTypeSize(Ty) <= 4*32 &&
377 areAllFields32Or64BitBasicType(RT->getDecl(), Context))
378 return ABIArgInfo::getExpand();
379 }
380
Daniel Dunbar22e30052008-09-11 01:48:57 +0000381 return ABIArgInfo::getByVal(0);
382 } else {
383 return ABIArgInfo::getDefault();
384 }
385}
386
Daniel Dunbare09a9692009-01-24 08:32:22 +0000387namespace {
388/// X86_32ABIInfo - The X86_64 ABI information.
389class X86_64ABIInfo : public ABIInfo {
390 enum Class {
391 Integer = 0,
392 SSE,
393 SSEUp,
394 X87,
395 X87Up,
396 ComplexX87,
397 NoClass,
398 Memory
399 };
400
401 /// classify - Determine the x86_64 register classes in which the
402 /// given type T should be passed.
403 ///
404 /// \param Lo - The classification for the low word of the type.
405 /// \param Hi - The classification for the high word of the type.
406 ///
407 /// If a word is unused its result will be NoClass; if a type should
408 /// be passed in Memory then at least the classification of \arg Lo
409 /// will be Memory.
410 ///
411 /// The \arg Lo class will be NoClass iff the argument is ignored.
412 ///
413 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
414 /// be NoClass.
415 void classify(QualType T, ASTContext &Context,
416 Class &Lo, Class &Hi) const;
417
418public:
419 virtual ABIArgInfo classifyReturnType(QualType RetTy,
420 ASTContext &Context) const;
421
422 virtual ABIArgInfo classifyArgumentType(QualType RetTy,
423 ASTContext &Context) const;
424};
425}
426
427void X86_64ABIInfo::classify(QualType Ty,
428 ASTContext &Context,
429 Class &Lo, Class &Hi) const {
430 Lo = Memory;
431 Hi = NoClass;
432 if (const BuiltinType *BT = Ty->getAsBuiltinType()) {
433 BuiltinType::Kind k = BT->getKind();
434
435 if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
436 Lo = Integer;
437 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
438 Lo = SSE;
439 } else if (k == BuiltinType::LongDouble) {
440 Lo = X87;
441 Hi = X87Up;
442 }
443 // FIXME: _Decimal32, _Decimal64, and __m64 are SSE.
444 // FIXME: _float128, _Decimal128, and __m128 are (SSE, SSEUp).
445 // FIXME: __int128 is (Integer, Integer).
446 } else if (Ty->isPointerLikeType() || Ty->isBlockPointerType() ||
447 Ty->isObjCQualifiedInterfaceType()) {
448 Lo = Integer;
449 } else if (const ComplexType *CT = Ty->getAsComplexType()) {
450 QualType ET = CT->getElementType();
451
452 if (ET == Context.FloatTy)
453 Lo = SSE;
454 else if (ET == Context.DoubleTy)
455 Lo = Hi = SSE;
456 else if (ET == Context.LongDoubleTy)
457 Lo = ComplexX87;
458 }
459}
460
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000461ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
462 ASTContext &Context) const {
Daniel Dunbare09a9692009-01-24 08:32:22 +0000463 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
464 // classification algorithm.
465 X86_64ABIInfo::Class Lo, Hi;
466 classify(RetTy, Context, Lo, Hi);
467
468 const llvm::Type *ResType = 0;
469 switch (Lo) {
470 case NoClass:
471 assert(0 && "FIXME: Handle ignored return values.");
472
473 case SSEUp:
474 case X87Up:
475 assert(0 && "Invalid classification for lo word.");
476
477 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
478 // hidden argument, i.e. structret.
479 case Memory:
480 return ABIArgInfo::getStructRet();
481
482 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
483 // available register of the sequence %rax, %rdx is used.
484 case Integer:
485 ResType = llvm::Type::Int64Ty; break;
486
487 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
488 // available SSE register of the sequence %xmm0, %xmm1 is used.
489 case SSE:
490 ResType = llvm::Type::DoubleTy; break;
491
492 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
493 // returned on the X87 stack in %st0 as 80-bit x87 number.
494 case X87:
495 ResType = llvm::Type::X86_FP80Ty; break;
496
497 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
498 // part of the value is returned in %st0 and the imaginary part in
499 // %st1.
500 case ComplexX87:
501 assert(Hi == NoClass && "Unexpected ComplexX87 classification.");
502 ResType = llvm::VectorType::get(llvm::Type::X86_FP80Ty, 2);
503 break;
504 }
505
506 switch (Hi) {
507 // Memory was handled previously, and ComplexX87 and X87 should
508 // never occur as hi classes.
509 case Memory:
510 case X87:
511 case ComplexX87:
512 assert(0 && "Invalid classification for hi word.");
513
514 case NoClass: break;
515 case Integer:
516 assert(0 && "FIXME: Implement MRV"); break;
517 case SSE:
518 assert(0 && "FIXME: Implement MRV"); break;
519
520 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
521 // is passed in the upper half of the last used SSE register.
522 //
523 // SSEUP should always be preceeded by SSE, just widen.
524 case SSEUp:
525 assert(Lo == SSE && "Unexpected SSEUp classification.");
526 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
527 break;
528
529 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
530 // returned together with the previos X87 value in %st0.
531 //
532 // X87UP should always be preceeded by X87, so we don't need to do
533 // anything here.
534 case X87Up:
535 assert(Lo == X87 && "Unexpected X87Up classification.");
536 break;
537 }
538
539 return ABIArgInfo::getCoerce(ResType);
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000540}
541
542ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty,
543 ASTContext &Context) const {
544 return ABIArgInfo::getDefault();
545}
546
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000547ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
548 ASTContext &Context) const {
549 return ABIArgInfo::getDefault();
550}
551
552ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
553 ASTContext &Context) const {
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000554 return ABIArgInfo::getDefault();
555}
556
557const ABIInfo &CodeGenTypes::getABIInfo() const {
558 if (TheABIInfo)
559 return *TheABIInfo;
560
561 // For now we just cache this in the CodeGenTypes and don't bother
562 // to free it.
563 const char *TargetPrefix = getContext().Target.getTargetPrefix();
564 if (strcmp(TargetPrefix, "x86") == 0) {
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000565 switch (getContext().Target.getPointerWidth(0)) {
566 case 32:
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000567 return *(TheABIInfo = new X86_32ABIInfo());
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000568 case 64:
Daniel Dunbare09a9692009-01-24 08:32:22 +0000569 if (UseX86_64ABI)
570 return *(TheABIInfo = new X86_64ABIInfo());
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000571 }
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000572 }
573
574 return *(TheABIInfo = new DefaultABIInfo);
575}
576
577// getABIReturnInfo - Wrap the ABIInfo getABIReturnInfo, altering
578// "default" types to StructRet when appropriate for simplicity.
579static ABIArgInfo getABIReturnInfo(QualType Ty, CodeGenTypes &CGT) {
580 assert(!Ty->isArrayType() &&
581 "Array types cannot be passed directly.");
582 ABIArgInfo Info = CGT.getABIInfo().classifyReturnType(Ty, CGT.getContext());
Daniel Dunbar3158c592008-09-17 20:11:04 +0000583 // Ensure default on aggregate types is StructRet.
584 if (Info.isDefault() && CodeGenFunction::hasAggregateLLVMType(Ty))
585 return ABIArgInfo::getStructRet();
586 return Info;
587}
588
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000589// getABIArgumentInfo - Wrap the ABIInfo getABIReturnInfo, altering
590// "default" types to ByVal when appropriate for simplicity.
591static ABIArgInfo getABIArgumentInfo(QualType Ty, CodeGenTypes &CGT) {
592 assert(!Ty->isArrayType() &&
593 "Array types cannot be passed directly.");
594 ABIArgInfo Info = CGT.getABIInfo().classifyArgumentType(Ty, CGT.getContext());
Daniel Dunbar3158c592008-09-17 20:11:04 +0000595 // Ensure default on aggregate types is ByVal.
596 if (Info.isDefault() && CodeGenFunction::hasAggregateLLVMType(Ty))
597 return ABIArgInfo::getByVal(0);
598 return Info;
599}
600
Daniel Dunbare126ab12008-09-10 02:41:04 +0000601/***/
602
Daniel Dunbar04d35782008-09-17 00:51:38 +0000603void CodeGenTypes::GetExpandedTypes(QualType Ty,
604 std::vector<const llvm::Type*> &ArgTys) {
605 const RecordType *RT = Ty->getAsStructureType();
606 assert(RT && "Can only expand structure types.");
607 const RecordDecl *RD = RT->getDecl();
608 assert(!RD->hasFlexibleArrayMember() &&
609 "Cannot expand structure with flexible array.");
610
Douglas Gregor5d764842009-01-09 17:18:27 +0000611 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar04d35782008-09-17 00:51:38 +0000612 e = RD->field_end(); i != e; ++i) {
613 const FieldDecl *FD = *i;
614 assert(!FD->isBitField() &&
615 "Cannot expand structure with bit-field members.");
616
617 QualType FT = FD->getType();
618 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
619 GetExpandedTypes(FT, ArgTys);
620 } else {
621 ArgTys.push_back(ConvertType(FT));
622 }
623 }
624}
625
626llvm::Function::arg_iterator
627CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
628 llvm::Function::arg_iterator AI) {
629 const RecordType *RT = Ty->getAsStructureType();
630 assert(RT && "Can only expand structure types.");
631
632 RecordDecl *RD = RT->getDecl();
633 assert(LV.isSimple() &&
634 "Unexpected non-simple lvalue during struct expansion.");
635 llvm::Value *Addr = LV.getAddress();
636 for (RecordDecl::field_iterator i = RD->field_begin(),
637 e = RD->field_end(); i != e; ++i) {
638 FieldDecl *FD = *i;
639 QualType FT = FD->getType();
640
641 // FIXME: What are the right qualifiers here?
642 LValue LV = EmitLValueForField(Addr, FD, false, 0);
643 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
644 AI = ExpandTypeFromArgs(FT, LV, AI);
645 } else {
646 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
647 ++AI;
648 }
649 }
650
651 return AI;
652}
653
654void
655CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
656 llvm::SmallVector<llvm::Value*, 16> &Args) {
657 const RecordType *RT = Ty->getAsStructureType();
658 assert(RT && "Can only expand structure types.");
659
660 RecordDecl *RD = RT->getDecl();
661 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
662 llvm::Value *Addr = RV.getAggregateAddr();
663 for (RecordDecl::field_iterator i = RD->field_begin(),
664 e = RD->field_end(); i != e; ++i) {
665 FieldDecl *FD = *i;
666 QualType FT = FD->getType();
667
668 // FIXME: What are the right qualifiers here?
669 LValue LV = EmitLValueForField(Addr, FD, false, 0);
670 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
671 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
672 } else {
673 RValue RV = EmitLoadOfLValue(LV, FT);
674 assert(RV.isScalar() &&
675 "Unexpected non-scalar rvalue during struct expansion.");
676 Args.push_back(RV.getScalarVal());
677 }
678 }
679}
680
681/***/
682
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000683const llvm::FunctionType *
Daniel Dunbara9976a22008-09-10 07:00:50 +0000684CodeGenTypes::GetFunctionType(const CGCallInfo &CI, bool IsVariadic) {
685 return GetFunctionType(CI.argtypes_begin(), CI.argtypes_end(), IsVariadic);
686}
687
688const llvm::FunctionType *
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000689CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI) {
Daniel Dunbara9976a22008-09-10 07:00:50 +0000690 return GetFunctionType(FI.argtypes_begin(), FI.argtypes_end(), FI.isVariadic());
691}
692
693const llvm::FunctionType *
694CodeGenTypes::GetFunctionType(ArgTypeIterator begin, ArgTypeIterator end,
695 bool IsVariadic) {
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000696 std::vector<const llvm::Type*> ArgTys;
697
698 const llvm::Type *ResultType = 0;
699
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000700 QualType RetTy = *begin;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000701 ABIArgInfo RetAI = getABIReturnInfo(RetTy, *this);
Daniel Dunbar22e30052008-09-11 01:48:57 +0000702 switch (RetAI.getKind()) {
703 case ABIArgInfo::ByVal:
704 case ABIArgInfo::Expand:
705 assert(0 && "Invalid ABI kind for return argument");
706
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000707 case ABIArgInfo::Default:
708 if (RetTy->isVoidType()) {
709 ResultType = llvm::Type::VoidTy;
710 } else {
Daniel Dunbara9976a22008-09-10 07:00:50 +0000711 ResultType = ConvertType(RetTy);
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000712 }
713 break;
714
715 case ABIArgInfo::StructRet: {
716 ResultType = llvm::Type::VoidTy;
Daniel Dunbara9976a22008-09-10 07:00:50 +0000717 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000718 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
719 break;
720 }
721
722 case ABIArgInfo::Coerce:
Daniel Dunbar73d66602008-09-10 07:04:09 +0000723 ResultType = RetAI.getCoerceToType();
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000724 break;
725 }
726
727 for (++begin; begin != end; ++begin) {
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000728 ABIArgInfo AI = getABIArgumentInfo(*begin, *this);
Daniel Dunbara9976a22008-09-10 07:00:50 +0000729 const llvm::Type *Ty = ConvertType(*begin);
Daniel Dunbar22e30052008-09-11 01:48:57 +0000730
731 switch (AI.getKind()) {
Daniel Dunbar04d35782008-09-17 00:51:38 +0000732 case ABIArgInfo::Coerce:
Daniel Dunbar22e30052008-09-11 01:48:57 +0000733 case ABIArgInfo::StructRet:
734 assert(0 && "Invalid ABI kind for non-return argument");
735
736 case ABIArgInfo::ByVal:
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000737 // byval arguments are always on the stack, which is addr space #0.
738 ArgTys.push_back(llvm::PointerType::getUnqual(Ty));
Daniel Dunbar22e30052008-09-11 01:48:57 +0000739 assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled");
740 break;
741
742 case ABIArgInfo::Default:
743 ArgTys.push_back(Ty);
744 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +0000745
746 case ABIArgInfo::Expand:
Daniel Dunbar04d35782008-09-17 00:51:38 +0000747 GetExpandedTypes(*begin, ArgTys);
Daniel Dunbar22e30052008-09-11 01:48:57 +0000748 break;
749 }
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000750 }
751
Daniel Dunbara9976a22008-09-10 07:00:50 +0000752 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000753}
754
Daniel Dunbar3ef2e852008-09-10 00:41:16 +0000755bool CodeGenModule::ReturnTypeUsesSret(QualType RetTy) {
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000756 return getABIReturnInfo(RetTy, getTypes()).isStructRet();
Daniel Dunbar49f5a0d2008-09-09 23:48:28 +0000757}
758
Devang Patela85a9ef2008-09-25 21:02:23 +0000759void CodeGenModule::ConstructAttributeList(const Decl *TargetDecl,
Daniel Dunbare126ab12008-09-10 02:41:04 +0000760 ArgTypeIterator begin,
761 ArgTypeIterator end,
Devang Patela85a9ef2008-09-25 21:02:23 +0000762 AttributeListType &PAL) {
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000763 unsigned FuncAttrs = 0;
Devang Patel2bb6eb82008-09-26 22:53:57 +0000764 unsigned RetAttrs = 0;
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000765
766 if (TargetDecl) {
767 if (TargetDecl->getAttr<NoThrowAttr>())
Devang Patela85a9ef2008-09-25 21:02:23 +0000768 FuncAttrs |= llvm::Attribute::NoUnwind;
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000769 if (TargetDecl->getAttr<NoReturnAttr>())
Devang Patela85a9ef2008-09-25 21:02:23 +0000770 FuncAttrs |= llvm::Attribute::NoReturn;
Anders Carlssondd6791c2008-10-05 23:32:53 +0000771 if (TargetDecl->getAttr<PureAttr>())
772 FuncAttrs |= llvm::Attribute::ReadOnly;
773 if (TargetDecl->getAttr<ConstAttr>())
774 FuncAttrs |= llvm::Attribute::ReadNone;
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000775 }
776
Daniel Dunbare126ab12008-09-10 02:41:04 +0000777 QualType RetTy = *begin;
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000778 unsigned Index = 1;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000779 ABIArgInfo RetAI = getABIReturnInfo(RetTy, getTypes());
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000780 switch (RetAI.getKind()) {
Daniel Dunbare126ab12008-09-10 02:41:04 +0000781 case ABIArgInfo::Default:
782 if (RetTy->isPromotableIntegerType()) {
783 if (RetTy->isSignedIntegerType()) {
Devang Patel2bb6eb82008-09-26 22:53:57 +0000784 RetAttrs |= llvm::Attribute::SExt;
Daniel Dunbare126ab12008-09-10 02:41:04 +0000785 } else if (RetTy->isUnsignedIntegerType()) {
Devang Patel2bb6eb82008-09-26 22:53:57 +0000786 RetAttrs |= llvm::Attribute::ZExt;
Daniel Dunbare126ab12008-09-10 02:41:04 +0000787 }
788 }
789 break;
790
791 case ABIArgInfo::StructRet:
Devang Patela85a9ef2008-09-25 21:02:23 +0000792 PAL.push_back(llvm::AttributeWithIndex::get(Index,
793 llvm::Attribute::StructRet|
794 llvm::Attribute::NoAlias));
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000795 ++Index;
Daniel Dunbare126ab12008-09-10 02:41:04 +0000796 break;
797
798 case ABIArgInfo::Coerce:
Daniel Dunbare126ab12008-09-10 02:41:04 +0000799 break;
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 Dunbarbccb0682008-09-10 00:32:18 +0000804 }
Daniel Dunbare126ab12008-09-10 02:41:04 +0000805
Devang Patel2bb6eb82008-09-26 22:53:57 +0000806 if (RetAttrs)
807 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Daniel Dunbar04d35782008-09-17 00:51:38 +0000808 for (++begin; begin != end; ++begin) {
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000809 QualType ParamType = *begin;
Devang Patela85a9ef2008-09-25 21:02:23 +0000810 unsigned Attributes = 0;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000811 ABIArgInfo AI = getABIArgumentInfo(ParamType, getTypes());
Daniel Dunbar22e30052008-09-11 01:48:57 +0000812
813 switch (AI.getKind()) {
814 case ABIArgInfo::StructRet:
Daniel Dunbar04d35782008-09-17 00:51:38 +0000815 case ABIArgInfo::Coerce:
Daniel Dunbar22e30052008-09-11 01:48:57 +0000816 assert(0 && "Invalid ABI kind for non-return argument");
817
818 case ABIArgInfo::ByVal:
Devang Patela85a9ef2008-09-25 21:02:23 +0000819 Attributes |= llvm::Attribute::ByVal;
Daniel Dunbar22e30052008-09-11 01:48:57 +0000820 assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled");
821 break;
822
823 case ABIArgInfo::Default:
824 if (ParamType->isPromotableIntegerType()) {
825 if (ParamType->isSignedIntegerType()) {
Devang Patela85a9ef2008-09-25 21:02:23 +0000826 Attributes |= llvm::Attribute::SExt;
Daniel Dunbar22e30052008-09-11 01:48:57 +0000827 } else if (ParamType->isUnsignedIntegerType()) {
Devang Patela85a9ef2008-09-25 21:02:23 +0000828 Attributes |= llvm::Attribute::ZExt;
Daniel Dunbar22e30052008-09-11 01:48:57 +0000829 }
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000830 }
Daniel Dunbar22e30052008-09-11 01:48:57 +0000831 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +0000832
Daniel Dunbar04d35782008-09-17 00:51:38 +0000833 case ABIArgInfo::Expand: {
834 std::vector<const llvm::Type*> Tys;
835 // FIXME: This is rather inefficient. Do we ever actually need
836 // to do anything here? The result should be just reconstructed
837 // on the other side, so extension should be a non-issue.
838 getTypes().GetExpandedTypes(ParamType, Tys);
839 Index += Tys.size();
840 continue;
841 }
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000842 }
Daniel Dunbar22e30052008-09-11 01:48:57 +0000843
Devang Patela85a9ef2008-09-25 21:02:23 +0000844 if (Attributes)
845 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar04d35782008-09-17 00:51:38 +0000846 ++Index;
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000847 }
Devang Patel2bb6eb82008-09-26 22:53:57 +0000848 if (FuncAttrs)
849 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
850
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000851}
852
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000853void CodeGenFunction::EmitFunctionProlog(llvm::Function *Fn,
854 QualType RetTy,
855 const FunctionArgList &Args) {
856 // Emit allocs for param decls. Give the LLVM Argument nodes names.
857 llvm::Function::arg_iterator AI = Fn->arg_begin();
858
859 // Name the struct return argument.
Daniel Dunbare126ab12008-09-10 02:41:04 +0000860 if (CGM.ReturnTypeUsesSret(RetTy)) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000861 AI->setName("agg.result");
862 ++AI;
863 }
864
865 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbar04d35782008-09-17 00:51:38 +0000866 i != e; ++i) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000867 const VarDecl *Arg = i->first;
Daniel Dunbar04d35782008-09-17 00:51:38 +0000868 QualType Ty = i->second;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000869 ABIArgInfo ArgI = getABIArgumentInfo(Ty, CGM.getTypes());
Daniel Dunbar22e30052008-09-11 01:48:57 +0000870
871 switch (ArgI.getKind()) {
872 case ABIArgInfo::ByVal:
873 case ABIArgInfo::Default: {
874 assert(AI != Fn->arg_end() && "Argument mismatch!");
875 llvm::Value* V = AI;
Daniel Dunbar04d35782008-09-17 00:51:38 +0000876 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
Daniel Dunbar22e30052008-09-11 01:48:57 +0000877 // This must be a promotion, for something like
878 // "void a(x) short x; {..."
Daniel Dunbar04d35782008-09-17 00:51:38 +0000879 V = EmitScalarConversion(V, Ty, Arg->getType());
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000880 }
Daniel Dunbar22e30052008-09-11 01:48:57 +0000881 EmitParmDecl(*Arg, V);
882 break;
883 }
Daniel Dunbar04d35782008-09-17 00:51:38 +0000884
885 case ABIArgInfo::Expand: {
886 // If this was structure was expand into multiple arguments then
887 // we need to create a temporary and reconstruct it from the
888 // arguments.
Chris Lattner6c5ec622008-11-24 04:00:27 +0000889 std::string Name = Arg->getNameAsString();
Daniel Dunbar04d35782008-09-17 00:51:38 +0000890 llvm::Value *Temp = CreateTempAlloca(ConvertType(Ty),
891 (Name + ".addr").c_str());
892 // FIXME: What are the right qualifiers here?
893 llvm::Function::arg_iterator End =
894 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI);
895 EmitParmDecl(*Arg, Temp);
Daniel Dunbar22e30052008-09-11 01:48:57 +0000896
Daniel Dunbar04d35782008-09-17 00:51:38 +0000897 // Name the arguments used in expansion and increment AI.
898 unsigned Index = 0;
899 for (; AI != End; ++AI, ++Index)
900 AI->setName(Name + "." + llvm::utostr(Index));
901 continue;
902 }
903
Daniel Dunbar22e30052008-09-11 01:48:57 +0000904 case ABIArgInfo::Coerce:
Daniel Dunbar22e30052008-09-11 01:48:57 +0000905 case ABIArgInfo::StructRet:
906 assert(0 && "Invalid ABI kind for non-return argument");
907 }
Daniel Dunbar04d35782008-09-17 00:51:38 +0000908
909 ++AI;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000910 }
911 assert(AI == Fn->arg_end() && "Argument mismatch!");
912}
913
914void CodeGenFunction::EmitFunctionEpilog(QualType RetTy,
915 llvm::Value *ReturnValue) {
Daniel Dunbare126ab12008-09-10 02:41:04 +0000916 llvm::Value *RV = 0;
917
918 // Functions with no result always return void.
919 if (ReturnValue) {
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000920 ABIArgInfo RetAI = getABIReturnInfo(RetTy, CGM.getTypes());
Daniel Dunbare126ab12008-09-10 02:41:04 +0000921
922 switch (RetAI.getKind()) {
923 case ABIArgInfo::StructRet:
Daniel Dunbar17d35372008-12-18 04:52:14 +0000924 if (RetTy->isAnyComplexType()) {
925 // FIXME: Volatile
926 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
927 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
928 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
929 EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy);
930 } else {
931 Builder.CreateStore(Builder.CreateLoad(ReturnValue),
932 CurFn->arg_begin());
933 }
Daniel Dunbare126ab12008-09-10 02:41:04 +0000934 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +0000935
Daniel Dunbare126ab12008-09-10 02:41:04 +0000936 case ABIArgInfo::Default:
937 RV = Builder.CreateLoad(ReturnValue);
938 break;
939
Daniel Dunbar73d66602008-09-10 07:04:09 +0000940 case ABIArgInfo::Coerce: {
941 const llvm::Type *CoerceToPTy =
942 llvm::PointerType::getUnqual(RetAI.getCoerceToType());
943 RV = Builder.CreateLoad(Builder.CreateBitCast(ReturnValue, CoerceToPTy));
Daniel Dunbar22e30052008-09-11 01:48:57 +0000944 break;
Daniel Dunbar73d66602008-09-10 07:04:09 +0000945 }
Daniel Dunbar22e30052008-09-11 01:48:57 +0000946
947 case ABIArgInfo::ByVal:
948 case ABIArgInfo::Expand:
949 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000950 }
951 }
Daniel Dunbare126ab12008-09-10 02:41:04 +0000952
953 if (RV) {
954 Builder.CreateRet(RV);
955 } else {
956 Builder.CreateRetVoid();
957 }
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000958}
959
960RValue CodeGenFunction::EmitCall(llvm::Value *Callee,
Daniel Dunbare126ab12008-09-10 02:41:04 +0000961 QualType RetTy,
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000962 const CallArgList &CallArgs) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000963 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000964
965 // Handle struct-return functions by passing a pointer to the
966 // location that we would like to return into.
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000967 ABIArgInfo RetAI = getABIReturnInfo(RetTy, CGM.getTypes());
Daniel Dunbare126ab12008-09-10 02:41:04 +0000968 switch (RetAI.getKind()) {
969 case ABIArgInfo::StructRet:
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000970 // Create a temporary alloca to hold the result of the call. :(
Daniel Dunbar04d35782008-09-17 00:51:38 +0000971 Args.push_back(CreateTempAlloca(ConvertType(RetTy)));
Daniel Dunbare126ab12008-09-10 02:41:04 +0000972 break;
973
974 case ABIArgInfo::Default:
Daniel Dunbare126ab12008-09-10 02:41:04 +0000975 case ABIArgInfo::Coerce:
Daniel Dunbare126ab12008-09-10 02:41:04 +0000976 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +0000977
978 case ABIArgInfo::ByVal:
979 case ABIArgInfo::Expand:
980 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000981 }
982
983 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
984 I != E; ++I) {
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000985 ABIArgInfo ArgInfo = getABIArgumentInfo(I->second, CGM.getTypes());
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000986 RValue RV = I->first;
Daniel Dunbar04d35782008-09-17 00:51:38 +0000987
988 switch (ArgInfo.getKind()) {
989 case ABIArgInfo::ByVal: // Default is byval
990 case ABIArgInfo::Default:
991 if (RV.isScalar()) {
992 Args.push_back(RV.getScalarVal());
993 } else if (RV.isComplex()) {
994 // Make a temporary alloca to pass the argument.
995 Args.push_back(CreateTempAlloca(ConvertType(I->second)));
996 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
997 } else {
998 Args.push_back(RV.getAggregateAddr());
999 }
1000 break;
1001
1002 case ABIArgInfo::StructRet:
1003 case ABIArgInfo::Coerce:
1004 assert(0 && "Invalid ABI kind for non-return argument");
1005 break;
1006
1007 case ABIArgInfo::Expand:
1008 ExpandTypeToArgs(I->second, RV, Args);
1009 break;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001010 }
1011 }
1012
1013 llvm::CallInst *CI = Builder.CreateCall(Callee,&Args[0],&Args[0]+Args.size());
Daniel Dunbare126ab12008-09-10 02:41:04 +00001014 CGCallInfo CallInfo(RetTy, CallArgs);
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001015
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001016 // FIXME: Provide TargetDecl so nounwind, noreturn, etc, etc get set.
Devang Patela85a9ef2008-09-25 21:02:23 +00001017 CodeGen::AttributeListType AttributeList;
1018 CGM.ConstructAttributeList(0,
Daniel Dunbar3ef2e852008-09-10 00:41:16 +00001019 CallInfo.argtypes_begin(), CallInfo.argtypes_end(),
Devang Patela85a9ef2008-09-25 21:02:23 +00001020 AttributeList);
1021 CI->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(),
1022 AttributeList.size()));
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001023
1024 if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee))
1025 CI->setCallingConv(F->getCallingConv());
1026 if (CI->getType() != llvm::Type::VoidTy)
1027 CI->setName("call");
Daniel Dunbare126ab12008-09-10 02:41:04 +00001028
1029 switch (RetAI.getKind()) {
1030 case ABIArgInfo::StructRet:
1031 if (RetTy->isAnyComplexType())
Daniel Dunbar04d35782008-09-17 00:51:38 +00001032 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Daniel Dunbar17d35372008-12-18 04:52:14 +00001033 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar04d35782008-09-17 00:51:38 +00001034 return RValue::getAggregate(Args[0]);
Daniel Dunbar17d35372008-12-18 04:52:14 +00001035 else
1036 return RValue::get(Builder.CreateLoad(Args[0]));
Daniel Dunbar22e30052008-09-11 01:48:57 +00001037
Daniel Dunbare126ab12008-09-10 02:41:04 +00001038 case ABIArgInfo::Default:
1039 return RValue::get(RetTy->isVoidType() ? 0 : CI);
1040
Daniel Dunbar73d66602008-09-10 07:04:09 +00001041 case ABIArgInfo::Coerce: {
1042 const llvm::Type *CoerceToPTy =
1043 llvm::PointerType::getUnqual(RetAI.getCoerceToType());
1044 llvm::Value *V = CreateTempAlloca(ConvertType(RetTy), "tmp");
1045 Builder.CreateStore(CI, Builder.CreateBitCast(V, CoerceToPTy));
Anders Carlssonfccf7472008-11-25 22:21:48 +00001046 if (RetTy->isAnyComplexType())
1047 return RValue::getComplex(LoadComplexFromAddr(V, false));
1048 else
1049 return RValue::getAggregate(V);
Daniel Dunbar73d66602008-09-10 07:04:09 +00001050 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001051
1052 case ABIArgInfo::ByVal:
1053 case ABIArgInfo::Expand:
1054 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001055 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001056
1057 assert(0 && "Unhandled ABIArgInfo::Kind");
1058 return RValue::get(0);
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001059}