blob: cd781781f9192a8423db8c93ac415670f9053589 [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 Dunbar6f3e7fa2009-01-24 08:32:22 +000024#include "llvm/Support/CommandLine.h"
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000025using namespace clang;
26using namespace CodeGen;
27
Daniel Dunbar6f3e7fa2009-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 Dunbar0dbe2272008-09-08 21:33:45 +000033/***/
34
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000035// FIXME: Use iterator and sidestep silly type array creation.
36
Daniel Dunbar45c25ba2008-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 Dunbar0dbe2272008-09-08 21:33:45 +000052CGFunctionInfo::CGFunctionInfo(const FunctionDecl *FD)
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000053{
54 const FunctionType *FTy = FD->getType()->getAsFunctionType();
55 const FunctionTypeProto *FTP = dyn_cast<FunctionTypeProto>(FTy);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000056
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000057 ArgTypes.push_back(FTy->getResultType());
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000058 if (FTP) {
59 IsVariadic = FTP->isVariadic();
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000060 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
61 ArgTypes.push_back(FTP->getArgType(i));
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000062 } else {
63 IsVariadic = true;
64 }
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000065}
66
67CGFunctionInfo::CGFunctionInfo(const ObjCMethodDecl *MD,
68 const ASTContext &Context)
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000069 : IsVariadic(MD->isVariadic())
Daniel Dunbar0dbe2272008-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 Dunbar5323a4b2008-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 Dunbar0dbe2272008-09-08 21:33:45 +000085}
86
87/***/
88
Daniel Dunbar5323a4b2008-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 Dunbar0dbe2272008-09-08 21:33:45 +000092 ArgTypes.push_back(i->second);
93}
94
Daniel Dunbar5323a4b2008-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 Dunbar0dbe2272008-09-08 21:33:45 +0000101}
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000102
103/***/
104
Daniel Dunbar8951dbd2008-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 Dunbar2c8e0f32008-09-10 02:41:04 +0000107class ABIArgInfo {
108public:
109 enum Kind {
110 Default,
Daniel Dunbar3aea8ca2008-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 Dunbar8951dbd2008-09-11 01:48:57 +0000115
Daniel Dunbar11434922009-01-26 21:26:08 +0000116 Ignore, /// Ignore the argument (treat as void). Useful for
117 /// void and empty structs.
118
Daniel Dunbar56273772008-09-17 00:51:38 +0000119 Coerce, /// Only valid for aggregate return types, the argument
120 /// should be accessed by coercion to a provided type.
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000121
122 ByVal, /// Only valid for aggregate argument types. The
123 /// structure should be passed "byval" with the
124 /// specified alignment (0 indicates default
125 /// alignment).
126
127 Expand, /// Only valid for aggregate argument types. The
128 /// structure should be expanded into consecutive
Daniel Dunbar56273772008-09-17 00:51:38 +0000129 /// arguments for its constituent fields. Currently
130 /// expand is only allowed on structures whose fields
131 /// are all scalar types or are themselves expandable
132 /// types.
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000133
134 KindFirst=Default, KindLast=Expand
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000135 };
136
137private:
138 Kind TheKind;
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000139 const llvm::Type *TypeData;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000140 unsigned UIntData;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000141
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000142 ABIArgInfo(Kind K, const llvm::Type *TD=0,
143 unsigned UI=0) : TheKind(K),
144 TypeData(TD),
145 UIntData(0) {}
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000146public:
147 static ABIArgInfo getDefault() {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000148 return ABIArgInfo(Default);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000149 }
150 static ABIArgInfo getStructRet() {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000151 return ABIArgInfo(StructRet);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000152 }
Daniel Dunbar11434922009-01-26 21:26:08 +0000153 static ABIArgInfo getIgnore() {
154 return ABIArgInfo(Ignore);
155 }
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000156 static ABIArgInfo getCoerce(const llvm::Type *T) {
157 assert(T->isSingleValueType() && "Can only coerce to simple types");
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000158 return ABIArgInfo(Coerce, T);
159 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000160 static ABIArgInfo getByVal(unsigned Alignment) {
161 return ABIArgInfo(ByVal, 0, Alignment);
162 }
Daniel Dunbar56273772008-09-17 00:51:38 +0000163 static ABIArgInfo getExpand() {
164 return ABIArgInfo(Expand);
165 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000166
167 Kind getKind() const { return TheKind; }
168 bool isDefault() const { return TheKind == Default; }
169 bool isStructRet() const { return TheKind == StructRet; }
Daniel Dunbar11434922009-01-26 21:26:08 +0000170 bool isIgnore() const { return TheKind == Ignore; }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000171 bool isCoerce() const { return TheKind == Coerce; }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000172 bool isByVal() const { return TheKind == ByVal; }
Daniel Dunbar56273772008-09-17 00:51:38 +0000173 bool isExpand() const { return TheKind == Expand; }
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000174
175 // Coerce accessors
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000176 const llvm::Type *getCoerceToType() const {
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000177 assert(TheKind == Coerce && "Invalid kind!");
178 return TypeData;
179 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000180
181 // ByVal accessors
182 unsigned getByValAlignment() const {
183 assert(TheKind == ByVal && "Invalid kind!");
184 return UIntData;
185 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000186};
187
188/***/
189
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000190/* FIXME: All of this stuff should be part of the target interface
191 somehow. It is currently here because it is not clear how to factor
192 the targets to support this, since the Targets currently live in a
193 layer below types n'stuff.
194 */
195
196/// ABIInfo - Target specific hooks for defining how a type should be
197/// passed or returned from functions.
198class clang::ABIInfo {
199public:
200 virtual ~ABIInfo();
201
202 virtual ABIArgInfo classifyReturnType(QualType RetTy,
203 ASTContext &Context) const = 0;
204
205 virtual ABIArgInfo classifyArgumentType(QualType Ty,
206 ASTContext &Context) const = 0;
207};
208
209ABIInfo::~ABIInfo() {}
210
Daniel Dunbar834af452008-09-17 21:22:33 +0000211/// isEmptyStruct - Return true iff a structure has no non-empty
212/// members. Note that a structure with a flexible array member is not
213/// considered empty.
214static bool isEmptyStruct(QualType T) {
215 const RecordType *RT = T->getAsStructureType();
216 if (!RT)
217 return 0;
218 const RecordDecl *RD = RT->getDecl();
219 if (RD->hasFlexibleArrayMember())
220 return false;
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000221 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar834af452008-09-17 21:22:33 +0000222 e = RD->field_end(); i != e; ++i) {
223 const FieldDecl *FD = *i;
224 if (!isEmptyStruct(FD->getType()))
225 return false;
226 }
227 return true;
228}
229
230/// isSingleElementStruct - Determine if a structure is a "single
231/// element struct", i.e. it has exactly one non-empty field or
232/// exactly one field which is itself a single element
233/// struct. Structures with flexible array members are never
234/// considered single element structs.
235///
236/// \return The field declaration for the single non-empty field, if
237/// it exists.
238static const FieldDecl *isSingleElementStruct(QualType T) {
239 const RecordType *RT = T->getAsStructureType();
240 if (!RT)
241 return 0;
242
243 const RecordDecl *RD = RT->getDecl();
244 if (RD->hasFlexibleArrayMember())
245 return 0;
246
247 const FieldDecl *Found = 0;
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000248 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar834af452008-09-17 21:22:33 +0000249 e = RD->field_end(); i != e; ++i) {
250 const FieldDecl *FD = *i;
251 QualType FT = FD->getType();
252
253 if (isEmptyStruct(FT)) {
254 // Ignore
255 } else if (Found) {
256 return 0;
257 } else if (!CodeGenFunction::hasAggregateLLVMType(FT)) {
258 Found = FD;
259 } else {
260 Found = isSingleElementStruct(FT);
261 if (!Found)
262 return 0;
263 }
264 }
265
266 return Found;
267}
268
269static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
270 if (!Ty->getAsBuiltinType() && !Ty->isPointerType())
271 return false;
272
273 uint64_t Size = Context.getTypeSize(Ty);
274 return Size == 32 || Size == 64;
275}
276
277static bool areAllFields32Or64BitBasicType(const RecordDecl *RD,
278 ASTContext &Context) {
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000279 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar834af452008-09-17 21:22:33 +0000280 e = RD->field_end(); i != e; ++i) {
281 const FieldDecl *FD = *i;
282
283 if (!is32Or64BitBasicType(FD->getType(), Context))
284 return false;
285
286 // If this is a bit-field we need to make sure it is still a
287 // 32-bit or 64-bit type.
288 if (Expr *BW = FD->getBitWidth()) {
289 unsigned Width = BW->getIntegerConstantExprValue(Context).getZExtValue();
290 if (Width <= 16)
291 return false;
292 }
293 }
294 return true;
295}
296
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000297namespace {
298/// DefaultABIInfo - The default implementation for ABI specific
299/// details. This implementation provides information which results in
300/// sensible LLVM IR generation, but does not conform to any
301/// particular ABI.
302class DefaultABIInfo : public ABIInfo {
303 virtual ABIArgInfo classifyReturnType(QualType RetTy,
304 ASTContext &Context) const;
305
306 virtual ABIArgInfo classifyArgumentType(QualType RetTy,
307 ASTContext &Context) const;
308};
309
310/// X86_32ABIInfo - The X86-32 ABI information.
311class X86_32ABIInfo : public ABIInfo {
312public:
313 virtual ABIArgInfo classifyReturnType(QualType RetTy,
314 ASTContext &Context) const;
315
316 virtual ABIArgInfo classifyArgumentType(QualType RetTy,
317 ASTContext &Context) const;
318};
319}
320
321ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
322 ASTContext &Context) const {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000323 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar834af452008-09-17 21:22:33 +0000324 // Classify "single element" structs as their element type.
325 const FieldDecl *SeltFD = isSingleElementStruct(RetTy);
326 if (SeltFD) {
327 QualType SeltTy = SeltFD->getType()->getDesugaredType();
328 if (const BuiltinType *BT = SeltTy->getAsBuiltinType()) {
329 // FIXME: This is gross, it would be nice if we could just
330 // pass back SeltTy and have clients deal with it. Is it worth
331 // supporting coerce to both LLVM and clang Types?
332 if (BT->isIntegerType()) {
333 uint64_t Size = Context.getTypeSize(SeltTy);
334 return ABIArgInfo::getCoerce(llvm::IntegerType::get((unsigned) Size));
335 } else if (BT->getKind() == BuiltinType::Float) {
336 return ABIArgInfo::getCoerce(llvm::Type::FloatTy);
337 } else if (BT->getKind() == BuiltinType::Double) {
338 return ABIArgInfo::getCoerce(llvm::Type::DoubleTy);
339 }
340 } else if (SeltTy->isPointerType()) {
341 // FIXME: It would be really nice if this could come out as
342 // the proper pointer type.
343 llvm::Type *PtrTy =
344 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
345 return ABIArgInfo::getCoerce(PtrTy);
346 }
347 }
348
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000349 uint64_t Size = Context.getTypeSize(RetTy);
350 if (Size == 8) {
351 return ABIArgInfo::getCoerce(llvm::Type::Int8Ty);
352 } else if (Size == 16) {
353 return ABIArgInfo::getCoerce(llvm::Type::Int16Ty);
354 } else if (Size == 32) {
355 return ABIArgInfo::getCoerce(llvm::Type::Int32Ty);
356 } else if (Size == 64) {
357 return ABIArgInfo::getCoerce(llvm::Type::Int64Ty);
358 } else {
359 return ABIArgInfo::getStructRet();
360 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000361 } else {
362 return ABIArgInfo::getDefault();
363 }
364}
365
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000366ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
367 ASTContext &Context) const {
Daniel Dunbarf0357382008-09-17 20:11:04 +0000368 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar834af452008-09-17 21:22:33 +0000369 // Structures with flexible arrays are always byval.
370 if (const RecordType *RT = Ty->getAsStructureType())
371 if (RT->getDecl()->hasFlexibleArrayMember())
372 return ABIArgInfo::getByVal(0);
373
374 // Expand empty structs (i.e. ignore)
375 uint64_t Size = Context.getTypeSize(Ty);
376 if (Ty->isStructureType() && Size == 0)
377 return ABIArgInfo::getExpand();
378
379 // Expand structs with size <= 128-bits which consist only of
380 // basic types (int, long long, float, double, xxx*). This is
381 // non-recursive and does not ignore empty fields.
382 if (const RecordType *RT = Ty->getAsStructureType()) {
383 if (Context.getTypeSize(Ty) <= 4*32 &&
384 areAllFields32Or64BitBasicType(RT->getDecl(), Context))
385 return ABIArgInfo::getExpand();
386 }
387
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000388 return ABIArgInfo::getByVal(0);
389 } else {
390 return ABIArgInfo::getDefault();
391 }
392}
393
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000394namespace {
395/// X86_32ABIInfo - The X86_64 ABI information.
396class X86_64ABIInfo : public ABIInfo {
397 enum Class {
398 Integer = 0,
399 SSE,
400 SSEUp,
401 X87,
402 X87Up,
403 ComplexX87,
404 NoClass,
405 Memory
406 };
407
408 /// classify - Determine the x86_64 register classes in which the
409 /// given type T should be passed.
410 ///
411 /// \param Lo - The classification for the low word of the type.
412 /// \param Hi - The classification for the high word of the type.
413 ///
414 /// If a word is unused its result will be NoClass; if a type should
415 /// be passed in Memory then at least the classification of \arg Lo
416 /// will be Memory.
417 ///
418 /// The \arg Lo class will be NoClass iff the argument is ignored.
419 ///
420 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
421 /// be NoClass.
422 void classify(QualType T, ASTContext &Context,
423 Class &Lo, Class &Hi) const;
424
425public:
426 virtual ABIArgInfo classifyReturnType(QualType RetTy,
427 ASTContext &Context) const;
428
429 virtual ABIArgInfo classifyArgumentType(QualType RetTy,
430 ASTContext &Context) const;
431};
432}
433
434void X86_64ABIInfo::classify(QualType Ty,
435 ASTContext &Context,
436 Class &Lo, Class &Hi) const {
437 Lo = Memory;
438 Hi = NoClass;
439 if (const BuiltinType *BT = Ty->getAsBuiltinType()) {
440 BuiltinType::Kind k = BT->getKind();
441
Daniel Dunbar11434922009-01-26 21:26:08 +0000442 if (k == BuiltinType::Void) {
443 Lo = NoClass;
444 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000445 Lo = Integer;
446 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
447 Lo = SSE;
448 } else if (k == BuiltinType::LongDouble) {
449 Lo = X87;
450 Hi = X87Up;
451 }
452 // FIXME: _Decimal32, _Decimal64, and __m64 are SSE.
453 // FIXME: _float128, _Decimal128, and __m128 are (SSE, SSEUp).
454 // FIXME: __int128 is (Integer, Integer).
455 } else if (Ty->isPointerLikeType() || Ty->isBlockPointerType() ||
456 Ty->isObjCQualifiedInterfaceType()) {
457 Lo = Integer;
458 } else if (const ComplexType *CT = Ty->getAsComplexType()) {
459 QualType ET = CT->getElementType();
460
461 if (ET == Context.FloatTy)
462 Lo = SSE;
463 else if (ET == Context.DoubleTy)
464 Lo = Hi = SSE;
465 else if (ET == Context.LongDoubleTy)
466 Lo = ComplexX87;
467 }
468}
469
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000470ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
471 ASTContext &Context) const {
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000472 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
473 // classification algorithm.
474 X86_64ABIInfo::Class Lo, Hi;
475 classify(RetTy, Context, Lo, Hi);
476
477 const llvm::Type *ResType = 0;
478 switch (Lo) {
479 case NoClass:
Daniel Dunbar11434922009-01-26 21:26:08 +0000480 return ABIArgInfo::getIgnore();
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000481
482 case SSEUp:
483 case X87Up:
484 assert(0 && "Invalid classification for lo word.");
485
486 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
487 // hidden argument, i.e. structret.
488 case Memory:
489 return ABIArgInfo::getStructRet();
490
491 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
492 // available register of the sequence %rax, %rdx is used.
493 case Integer:
494 ResType = llvm::Type::Int64Ty; break;
495
496 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
497 // available SSE register of the sequence %xmm0, %xmm1 is used.
498 case SSE:
499 ResType = llvm::Type::DoubleTy; break;
500
501 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
502 // returned on the X87 stack in %st0 as 80-bit x87 number.
503 case X87:
504 ResType = llvm::Type::X86_FP80Ty; break;
505
506 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
507 // part of the value is returned in %st0 and the imaginary part in
508 // %st1.
509 case ComplexX87:
510 assert(Hi == NoClass && "Unexpected ComplexX87 classification.");
511 ResType = llvm::VectorType::get(llvm::Type::X86_FP80Ty, 2);
512 break;
513 }
514
515 switch (Hi) {
516 // Memory was handled previously, and ComplexX87 and X87 should
517 // never occur as hi classes.
518 case Memory:
519 case X87:
520 case ComplexX87:
521 assert(0 && "Invalid classification for hi word.");
522
523 case NoClass: break;
524 case Integer:
525 assert(0 && "FIXME: Implement MRV"); break;
526 case SSE:
527 assert(0 && "FIXME: Implement MRV"); break;
528
529 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
530 // is passed in the upper half of the last used SSE register.
531 //
532 // SSEUP should always be preceeded by SSE, just widen.
533 case SSEUp:
534 assert(Lo == SSE && "Unexpected SSEUp classification.");
535 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
536 break;
537
538 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
539 // returned together with the previos X87 value in %st0.
540 //
541 // X87UP should always be preceeded by X87, so we don't need to do
542 // anything here.
543 case X87Up:
544 assert(Lo == X87 && "Unexpected X87Up classification.");
545 break;
546 }
547
548 return ABIArgInfo::getCoerce(ResType);
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000549}
550
551ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty,
552 ASTContext &Context) const {
553 return ABIArgInfo::getDefault();
554}
555
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000556ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
557 ASTContext &Context) const {
558 return ABIArgInfo::getDefault();
559}
560
561ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
562 ASTContext &Context) const {
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000563 return ABIArgInfo::getDefault();
564}
565
566const ABIInfo &CodeGenTypes::getABIInfo() const {
567 if (TheABIInfo)
568 return *TheABIInfo;
569
570 // For now we just cache this in the CodeGenTypes and don't bother
571 // to free it.
572 const char *TargetPrefix = getContext().Target.getTargetPrefix();
573 if (strcmp(TargetPrefix, "x86") == 0) {
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000574 switch (getContext().Target.getPointerWidth(0)) {
575 case 32:
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000576 return *(TheABIInfo = new X86_32ABIInfo());
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000577 case 64:
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000578 if (UseX86_64ABI)
579 return *(TheABIInfo = new X86_64ABIInfo());
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000580 }
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000581 }
582
583 return *(TheABIInfo = new DefaultABIInfo);
584}
585
586// getABIReturnInfo - Wrap the ABIInfo getABIReturnInfo, altering
587// "default" types to StructRet when appropriate for simplicity.
588static ABIArgInfo getABIReturnInfo(QualType Ty, CodeGenTypes &CGT) {
589 assert(!Ty->isArrayType() &&
590 "Array types cannot be passed directly.");
591 ABIArgInfo Info = CGT.getABIInfo().classifyReturnType(Ty, CGT.getContext());
Daniel Dunbarf0357382008-09-17 20:11:04 +0000592 // Ensure default on aggregate types is StructRet.
593 if (Info.isDefault() && CodeGenFunction::hasAggregateLLVMType(Ty))
594 return ABIArgInfo::getStructRet();
595 return Info;
596}
597
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000598// getABIArgumentInfo - Wrap the ABIInfo getABIReturnInfo, altering
599// "default" types to ByVal when appropriate for simplicity.
600static ABIArgInfo getABIArgumentInfo(QualType Ty, CodeGenTypes &CGT) {
601 assert(!Ty->isArrayType() &&
602 "Array types cannot be passed directly.");
603 ABIArgInfo Info = CGT.getABIInfo().classifyArgumentType(Ty, CGT.getContext());
Daniel Dunbarf0357382008-09-17 20:11:04 +0000604 // Ensure default on aggregate types is ByVal.
605 if (Info.isDefault() && CodeGenFunction::hasAggregateLLVMType(Ty))
606 return ABIArgInfo::getByVal(0);
607 return Info;
608}
609
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000610/***/
611
Daniel Dunbar56273772008-09-17 00:51:38 +0000612void CodeGenTypes::GetExpandedTypes(QualType Ty,
613 std::vector<const llvm::Type*> &ArgTys) {
614 const RecordType *RT = Ty->getAsStructureType();
615 assert(RT && "Can only expand structure types.");
616 const RecordDecl *RD = RT->getDecl();
617 assert(!RD->hasFlexibleArrayMember() &&
618 "Cannot expand structure with flexible array.");
619
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000620 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar56273772008-09-17 00:51:38 +0000621 e = RD->field_end(); i != e; ++i) {
622 const FieldDecl *FD = *i;
623 assert(!FD->isBitField() &&
624 "Cannot expand structure with bit-field members.");
625
626 QualType FT = FD->getType();
627 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
628 GetExpandedTypes(FT, ArgTys);
629 } else {
630 ArgTys.push_back(ConvertType(FT));
631 }
632 }
633}
634
635llvm::Function::arg_iterator
636CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
637 llvm::Function::arg_iterator AI) {
638 const RecordType *RT = Ty->getAsStructureType();
639 assert(RT && "Can only expand structure types.");
640
641 RecordDecl *RD = RT->getDecl();
642 assert(LV.isSimple() &&
643 "Unexpected non-simple lvalue during struct expansion.");
644 llvm::Value *Addr = LV.getAddress();
645 for (RecordDecl::field_iterator i = RD->field_begin(),
646 e = RD->field_end(); i != e; ++i) {
647 FieldDecl *FD = *i;
648 QualType FT = FD->getType();
649
650 // FIXME: What are the right qualifiers here?
651 LValue LV = EmitLValueForField(Addr, FD, false, 0);
652 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
653 AI = ExpandTypeFromArgs(FT, LV, AI);
654 } else {
655 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
656 ++AI;
657 }
658 }
659
660 return AI;
661}
662
663void
664CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
665 llvm::SmallVector<llvm::Value*, 16> &Args) {
666 const RecordType *RT = Ty->getAsStructureType();
667 assert(RT && "Can only expand structure types.");
668
669 RecordDecl *RD = RT->getDecl();
670 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
671 llvm::Value *Addr = RV.getAggregateAddr();
672 for (RecordDecl::field_iterator i = RD->field_begin(),
673 e = RD->field_end(); i != e; ++i) {
674 FieldDecl *FD = *i;
675 QualType FT = FD->getType();
676
677 // FIXME: What are the right qualifiers here?
678 LValue LV = EmitLValueForField(Addr, FD, false, 0);
679 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
680 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
681 } else {
682 RValue RV = EmitLoadOfLValue(LV, FT);
683 assert(RV.isScalar() &&
684 "Unexpected non-scalar rvalue during struct expansion.");
685 Args.push_back(RV.getScalarVal());
686 }
687 }
688}
689
690/***/
691
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000692const llvm::FunctionType *
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000693CodeGenTypes::GetFunctionType(const CGCallInfo &CI, bool IsVariadic) {
694 return GetFunctionType(CI.argtypes_begin(), CI.argtypes_end(), IsVariadic);
695}
696
697const llvm::FunctionType *
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000698CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI) {
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000699 return GetFunctionType(FI.argtypes_begin(), FI.argtypes_end(), FI.isVariadic());
700}
701
702const llvm::FunctionType *
703CodeGenTypes::GetFunctionType(ArgTypeIterator begin, ArgTypeIterator end,
704 bool IsVariadic) {
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000705 std::vector<const llvm::Type*> ArgTys;
706
707 const llvm::Type *ResultType = 0;
708
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000709 QualType RetTy = *begin;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000710 ABIArgInfo RetAI = getABIReturnInfo(RetTy, *this);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000711 switch (RetAI.getKind()) {
712 case ABIArgInfo::ByVal:
713 case ABIArgInfo::Expand:
714 assert(0 && "Invalid ABI kind for return argument");
715
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000716 case ABIArgInfo::Default:
717 if (RetTy->isVoidType()) {
718 ResultType = llvm::Type::VoidTy;
719 } else {
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000720 ResultType = ConvertType(RetTy);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000721 }
722 break;
723
724 case ABIArgInfo::StructRet: {
725 ResultType = llvm::Type::VoidTy;
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000726 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000727 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
728 break;
729 }
730
Daniel Dunbar11434922009-01-26 21:26:08 +0000731 case ABIArgInfo::Ignore:
732 ResultType = llvm::Type::VoidTy;
733 break;
734
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000735 case ABIArgInfo::Coerce:
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000736 ResultType = RetAI.getCoerceToType();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000737 break;
738 }
739
740 for (++begin; begin != end; ++begin) {
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000741 ABIArgInfo AI = getABIArgumentInfo(*begin, *this);
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000742 const llvm::Type *Ty = ConvertType(*begin);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000743
744 switch (AI.getKind()) {
Daniel Dunbar11434922009-01-26 21:26:08 +0000745 case ABIArgInfo::Ignore:
746 break;
747
Daniel Dunbar56273772008-09-17 00:51:38 +0000748 case ABIArgInfo::Coerce:
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000749 case ABIArgInfo::StructRet:
750 assert(0 && "Invalid ABI kind for non-return argument");
751
752 case ABIArgInfo::ByVal:
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000753 // byval arguments are always on the stack, which is addr space #0.
754 ArgTys.push_back(llvm::PointerType::getUnqual(Ty));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000755 assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled");
756 break;
757
758 case ABIArgInfo::Default:
759 ArgTys.push_back(Ty);
760 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000761
762 case ABIArgInfo::Expand:
Daniel Dunbar56273772008-09-17 00:51:38 +0000763 GetExpandedTypes(*begin, ArgTys);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000764 break;
765 }
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000766 }
767
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000768 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000769}
770
Daniel Dunbarb7688072008-09-10 00:41:16 +0000771bool CodeGenModule::ReturnTypeUsesSret(QualType RetTy) {
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000772 return getABIReturnInfo(RetTy, getTypes()).isStructRet();
Daniel Dunbar3913f182008-09-09 23:48:28 +0000773}
774
Devang Patel761d7f72008-09-25 21:02:23 +0000775void CodeGenModule::ConstructAttributeList(const Decl *TargetDecl,
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000776 ArgTypeIterator begin,
777 ArgTypeIterator end,
Devang Patel761d7f72008-09-25 21:02:23 +0000778 AttributeListType &PAL) {
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000779 unsigned FuncAttrs = 0;
Devang Patela2c69122008-09-26 22:53:57 +0000780 unsigned RetAttrs = 0;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000781
782 if (TargetDecl) {
783 if (TargetDecl->getAttr<NoThrowAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +0000784 FuncAttrs |= llvm::Attribute::NoUnwind;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000785 if (TargetDecl->getAttr<NoReturnAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +0000786 FuncAttrs |= llvm::Attribute::NoReturn;
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000787 if (TargetDecl->getAttr<PureAttr>())
788 FuncAttrs |= llvm::Attribute::ReadOnly;
789 if (TargetDecl->getAttr<ConstAttr>())
790 FuncAttrs |= llvm::Attribute::ReadNone;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000791 }
792
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000793 QualType RetTy = *begin;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000794 unsigned Index = 1;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000795 ABIArgInfo RetAI = getABIReturnInfo(RetTy, getTypes());
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000796 switch (RetAI.getKind()) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000797 case ABIArgInfo::Default:
798 if (RetTy->isPromotableIntegerType()) {
799 if (RetTy->isSignedIntegerType()) {
Devang Patela2c69122008-09-26 22:53:57 +0000800 RetAttrs |= llvm::Attribute::SExt;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000801 } else if (RetTy->isUnsignedIntegerType()) {
Devang Patela2c69122008-09-26 22:53:57 +0000802 RetAttrs |= llvm::Attribute::ZExt;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000803 }
804 }
805 break;
806
807 case ABIArgInfo::StructRet:
Devang Patel761d7f72008-09-25 21:02:23 +0000808 PAL.push_back(llvm::AttributeWithIndex::get(Index,
809 llvm::Attribute::StructRet|
810 llvm::Attribute::NoAlias));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000811 ++Index;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000812 break;
813
Daniel Dunbar11434922009-01-26 21:26:08 +0000814 case ABIArgInfo::Ignore:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000815 case ABIArgInfo::Coerce:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000816 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000817
818 case ABIArgInfo::ByVal:
819 case ABIArgInfo::Expand:
820 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000821 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000822
Devang Patela2c69122008-09-26 22:53:57 +0000823 if (RetAttrs)
824 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Daniel Dunbar56273772008-09-17 00:51:38 +0000825 for (++begin; begin != end; ++begin) {
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000826 QualType ParamType = *begin;
Devang Patel761d7f72008-09-25 21:02:23 +0000827 unsigned Attributes = 0;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000828 ABIArgInfo AI = getABIArgumentInfo(ParamType, getTypes());
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000829
830 switch (AI.getKind()) {
831 case ABIArgInfo::StructRet:
Daniel Dunbar56273772008-09-17 00:51:38 +0000832 case ABIArgInfo::Coerce:
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000833 assert(0 && "Invalid ABI kind for non-return argument");
834
835 case ABIArgInfo::ByVal:
Devang Patel761d7f72008-09-25 21:02:23 +0000836 Attributes |= llvm::Attribute::ByVal;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000837 assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled");
838 break;
839
840 case ABIArgInfo::Default:
841 if (ParamType->isPromotableIntegerType()) {
842 if (ParamType->isSignedIntegerType()) {
Devang Patel761d7f72008-09-25 21:02:23 +0000843 Attributes |= llvm::Attribute::SExt;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000844 } else if (ParamType->isUnsignedIntegerType()) {
Devang Patel761d7f72008-09-25 21:02:23 +0000845 Attributes |= llvm::Attribute::ZExt;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000846 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000847 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000848 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000849
Daniel Dunbar11434922009-01-26 21:26:08 +0000850 case ABIArgInfo::Ignore:
851 // Skip increment, no matching LLVM parameter.
852 continue;
853
Daniel Dunbar56273772008-09-17 00:51:38 +0000854 case ABIArgInfo::Expand: {
855 std::vector<const llvm::Type*> Tys;
856 // FIXME: This is rather inefficient. Do we ever actually need
857 // to do anything here? The result should be just reconstructed
858 // on the other side, so extension should be a non-issue.
859 getTypes().GetExpandedTypes(ParamType, Tys);
860 Index += Tys.size();
861 continue;
862 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000863 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000864
Devang Patel761d7f72008-09-25 21:02:23 +0000865 if (Attributes)
866 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar56273772008-09-17 00:51:38 +0000867 ++Index;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000868 }
Devang Patela2c69122008-09-26 22:53:57 +0000869 if (FuncAttrs)
870 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
871
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000872}
873
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000874void CodeGenFunction::EmitFunctionProlog(llvm::Function *Fn,
875 QualType RetTy,
876 const FunctionArgList &Args) {
877 // Emit allocs for param decls. Give the LLVM Argument nodes names.
878 llvm::Function::arg_iterator AI = Fn->arg_begin();
879
880 // Name the struct return argument.
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000881 if (CGM.ReturnTypeUsesSret(RetTy)) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000882 AI->setName("agg.result");
883 ++AI;
884 }
885
886 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbar56273772008-09-17 00:51:38 +0000887 i != e; ++i) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000888 const VarDecl *Arg = i->first;
Daniel Dunbar56273772008-09-17 00:51:38 +0000889 QualType Ty = i->second;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000890 ABIArgInfo ArgI = getABIArgumentInfo(Ty, CGM.getTypes());
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000891
892 switch (ArgI.getKind()) {
893 case ABIArgInfo::ByVal:
894 case ABIArgInfo::Default: {
895 assert(AI != Fn->arg_end() && "Argument mismatch!");
896 llvm::Value* V = AI;
Daniel Dunbar56273772008-09-17 00:51:38 +0000897 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000898 // This must be a promotion, for something like
899 // "void a(x) short x; {..."
Daniel Dunbar56273772008-09-17 00:51:38 +0000900 V = EmitScalarConversion(V, Ty, Arg->getType());
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000901 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000902 EmitParmDecl(*Arg, V);
903 break;
904 }
Daniel Dunbar56273772008-09-17 00:51:38 +0000905
906 case ABIArgInfo::Expand: {
907 // If this was structure was expand into multiple arguments then
908 // we need to create a temporary and reconstruct it from the
909 // arguments.
Chris Lattner39f34e92008-11-24 04:00:27 +0000910 std::string Name = Arg->getNameAsString();
Daniel Dunbar56273772008-09-17 00:51:38 +0000911 llvm::Value *Temp = CreateTempAlloca(ConvertType(Ty),
912 (Name + ".addr").c_str());
913 // FIXME: What are the right qualifiers here?
914 llvm::Function::arg_iterator End =
915 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI);
916 EmitParmDecl(*Arg, Temp);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000917
Daniel Dunbar56273772008-09-17 00:51:38 +0000918 // Name the arguments used in expansion and increment AI.
919 unsigned Index = 0;
920 for (; AI != End; ++AI, ++Index)
921 AI->setName(Name + "." + llvm::utostr(Index));
922 continue;
923 }
Daniel Dunbar11434922009-01-26 21:26:08 +0000924
925 case ABIArgInfo::Ignore:
926 break;
927
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000928 case ABIArgInfo::Coerce:
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000929 case ABIArgInfo::StructRet:
930 assert(0 && "Invalid ABI kind for non-return argument");
931 }
Daniel Dunbar56273772008-09-17 00:51:38 +0000932
933 ++AI;
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000934 }
935 assert(AI == Fn->arg_end() && "Argument mismatch!");
936}
937
938void CodeGenFunction::EmitFunctionEpilog(QualType RetTy,
939 llvm::Value *ReturnValue) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000940 llvm::Value *RV = 0;
941
942 // Functions with no result always return void.
943 if (ReturnValue) {
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000944 ABIArgInfo RetAI = getABIReturnInfo(RetTy, CGM.getTypes());
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000945
946 switch (RetAI.getKind()) {
947 case ABIArgInfo::StructRet:
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +0000948 if (RetTy->isAnyComplexType()) {
949 // FIXME: Volatile
950 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
951 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
952 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
953 EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy);
954 } else {
955 Builder.CreateStore(Builder.CreateLoad(ReturnValue),
956 CurFn->arg_begin());
957 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000958 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000959
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000960 case ABIArgInfo::Default:
961 RV = Builder.CreateLoad(ReturnValue);
962 break;
963
Daniel Dunbar11434922009-01-26 21:26:08 +0000964 case ABIArgInfo::Ignore:
965 break;
966
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000967 case ABIArgInfo::Coerce: {
968 const llvm::Type *CoerceToPTy =
969 llvm::PointerType::getUnqual(RetAI.getCoerceToType());
970 RV = Builder.CreateLoad(Builder.CreateBitCast(ReturnValue, CoerceToPTy));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000971 break;
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000972 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000973
974 case ABIArgInfo::ByVal:
975 case ABIArgInfo::Expand:
976 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000977 }
978 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000979
980 if (RV) {
981 Builder.CreateRet(RV);
982 } else {
983 Builder.CreateRetVoid();
984 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000985}
986
987RValue CodeGenFunction::EmitCall(llvm::Value *Callee,
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000988 QualType RetTy,
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000989 const CallArgList &CallArgs) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000990 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000991
992 // Handle struct-return functions by passing a pointer to the
993 // location that we would like to return into.
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000994 ABIArgInfo RetAI = getABIReturnInfo(RetTy, CGM.getTypes());
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000995 switch (RetAI.getKind()) {
996 case ABIArgInfo::StructRet:
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000997 // Create a temporary alloca to hold the result of the call. :(
Daniel Dunbar56273772008-09-17 00:51:38 +0000998 Args.push_back(CreateTempAlloca(ConvertType(RetTy)));
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000999 break;
1000
1001 case ABIArgInfo::Default:
Daniel Dunbar11434922009-01-26 21:26:08 +00001002 case ABIArgInfo::Ignore:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001003 case ABIArgInfo::Coerce:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001004 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001005
1006 case ABIArgInfo::ByVal:
1007 case ABIArgInfo::Expand:
1008 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001009 }
1010
1011 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
1012 I != E; ++I) {
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001013 ABIArgInfo ArgInfo = getABIArgumentInfo(I->second, CGM.getTypes());
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001014 RValue RV = I->first;
Daniel Dunbar56273772008-09-17 00:51:38 +00001015
1016 switch (ArgInfo.getKind()) {
1017 case ABIArgInfo::ByVal: // Default is byval
1018 case ABIArgInfo::Default:
1019 if (RV.isScalar()) {
1020 Args.push_back(RV.getScalarVal());
1021 } else if (RV.isComplex()) {
1022 // Make a temporary alloca to pass the argument.
1023 Args.push_back(CreateTempAlloca(ConvertType(I->second)));
1024 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
1025 } else {
1026 Args.push_back(RV.getAggregateAddr());
1027 }
1028 break;
1029
Daniel Dunbar11434922009-01-26 21:26:08 +00001030 case ABIArgInfo::Ignore:
1031 break;
1032
Daniel Dunbar56273772008-09-17 00:51:38 +00001033 case ABIArgInfo::StructRet:
1034 case ABIArgInfo::Coerce:
1035 assert(0 && "Invalid ABI kind for non-return argument");
1036 break;
1037
1038 case ABIArgInfo::Expand:
1039 ExpandTypeToArgs(I->second, RV, Args);
1040 break;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001041 }
1042 }
1043
1044 llvm::CallInst *CI = Builder.CreateCall(Callee,&Args[0],&Args[0]+Args.size());
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001045 CGCallInfo CallInfo(RetTy, CallArgs);
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001046
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001047 // FIXME: Provide TargetDecl so nounwind, noreturn, etc, etc get set.
Devang Patel761d7f72008-09-25 21:02:23 +00001048 CodeGen::AttributeListType AttributeList;
1049 CGM.ConstructAttributeList(0,
Daniel Dunbarb7688072008-09-10 00:41:16 +00001050 CallInfo.argtypes_begin(), CallInfo.argtypes_end(),
Devang Patel761d7f72008-09-25 21:02:23 +00001051 AttributeList);
1052 CI->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(),
1053 AttributeList.size()));
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001054
1055 if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee))
1056 CI->setCallingConv(F->getCallingConv());
1057 if (CI->getType() != llvm::Type::VoidTy)
1058 CI->setName("call");
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001059
1060 switch (RetAI.getKind()) {
1061 case ABIArgInfo::StructRet:
1062 if (RetTy->isAnyComplexType())
Daniel Dunbar56273772008-09-17 00:51:38 +00001063 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001064 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar56273772008-09-17 00:51:38 +00001065 return RValue::getAggregate(Args[0]);
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001066 else
1067 return RValue::get(Builder.CreateLoad(Args[0]));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001068
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001069 case ABIArgInfo::Default:
1070 return RValue::get(RetTy->isVoidType() ? 0 : CI);
1071
Daniel Dunbar11434922009-01-26 21:26:08 +00001072 case ABIArgInfo::Ignore:
1073 return RValue::get(0);
1074
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001075 case ABIArgInfo::Coerce: {
1076 const llvm::Type *CoerceToPTy =
1077 llvm::PointerType::getUnqual(RetAI.getCoerceToType());
1078 llvm::Value *V = CreateTempAlloca(ConvertType(RetTy), "tmp");
1079 Builder.CreateStore(CI, Builder.CreateBitCast(V, CoerceToPTy));
Anders Carlssonad3d6912008-11-25 22:21:48 +00001080 if (RetTy->isAnyComplexType())
1081 return RValue::getComplex(LoadComplexFromAddr(V, false));
Daniel Dunbar11434922009-01-26 21:26:08 +00001082 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssonad3d6912008-11-25 22:21:48 +00001083 return RValue::getAggregate(V);
Daniel Dunbar11434922009-01-26 21:26:08 +00001084 else
1085 return RValue::get(Builder.CreateLoad(V));
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001086 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001087
1088 case ABIArgInfo::ByVal:
1089 case ABIArgInfo::Expand:
1090 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001091 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001092
1093 assert(0 && "Unhandled ABIArgInfo::Kind");
1094 return RValue::get(0);
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001095}