blob: c7b2a5dc895f9fbe34546e1042bd3cd92fb15350 [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 Dunbar99037e52009-01-29 08:13:58 +000022#include "clang/AST/RecordLayout.h"
Daniel Dunbar56273772008-09-17 00:51:38 +000023#include "llvm/ADT/StringExtras.h"
Devang Pateld0646bd2008-09-24 01:01:36 +000024#include "llvm/Attributes.h"
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +000025#include "llvm/Support/CommandLine.h"
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +000026#include "llvm/Target/TargetData.h"
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000027using namespace clang;
28using namespace CodeGen;
29
30/***/
31
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000032// FIXME: Use iterator and sidestep silly type array creation.
33
Daniel Dunbarbb36d332009-02-02 21:43:58 +000034CGFunctionInfo::CGFunctionInfo(const FunctionTypeNoProto *FTNP) {
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000035 ArgTypes.push_back(FTNP->getResultType());
36}
37
Daniel Dunbarbb36d332009-02-02 21:43:58 +000038CGFunctionInfo::CGFunctionInfo(const FunctionTypeProto *FTP) {
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000039 ArgTypes.push_back(FTP->getResultType());
40 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
41 ArgTypes.push_back(FTP->getArgType(i));
42}
43
44// FIXME: Is there really any reason to have this still?
Daniel Dunbarbb36d332009-02-02 21:43:58 +000045CGFunctionInfo::CGFunctionInfo(const FunctionDecl *FD) {
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000046 const FunctionType *FTy = FD->getType()->getAsFunctionType();
47 const FunctionTypeProto *FTP = dyn_cast<FunctionTypeProto>(FTy);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000048
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000049 ArgTypes.push_back(FTy->getResultType());
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000050 if (FTP) {
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000051 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
52 ArgTypes.push_back(FTP->getArgType(i));
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000053 }
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000054}
55
56CGFunctionInfo::CGFunctionInfo(const ObjCMethodDecl *MD,
Daniel Dunbarbb36d332009-02-02 21:43:58 +000057 const ASTContext &Context) {
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000058 ArgTypes.push_back(MD->getResultType());
59 ArgTypes.push_back(MD->getSelfDecl()->getType());
60 ArgTypes.push_back(Context.getObjCSelType());
61 for (ObjCMethodDecl::param_const_iterator i = MD->param_begin(),
62 e = MD->param_end(); i != e; ++i)
63 ArgTypes.push_back((*i)->getType());
64}
65
Daniel Dunbarbb36d332009-02-02 21:43:58 +000066CGFunctionInfo::CGFunctionInfo(QualType ResTy, const CallArgList &Args) {
Daniel Dunbar725ad312009-01-31 02:19:00 +000067 ArgTypes.push_back(ResTy);
68 for (CallArgList::const_iterator i = Args.begin(), e = Args.end();
69 i != e; ++i)
70 ArgTypes.push_back(i->second);
71}
72
Daniel Dunbarbb36d332009-02-02 21:43:58 +000073CGFunctionInfo::CGFunctionInfo(QualType ResTy, const FunctionArgList &Args) {
74 ArgTypes.push_back(ResTy);
75 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
76 i != e; ++i)
77 ArgTypes.push_back(i->second);
78}
79
Daniel Dunbar5323a4b2008-09-10 00:32:18 +000080ArgTypeIterator CGFunctionInfo::argtypes_begin() const {
81 return ArgTypes.begin();
82}
83
84ArgTypeIterator CGFunctionInfo::argtypes_end() const {
85 return ArgTypes.end();
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000086}
87
88/***/
89
Daniel Dunbar8951dbd2008-09-11 01:48:57 +000090/// ABIArgInfo - Helper class to encapsulate information about how a
91/// specific C type should be passed to or returned from a function.
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +000092class ABIArgInfo {
93public:
94 enum Kind {
95 Default,
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +000096 StructRet, /// Only valid for return values. The return value
97 /// should be passed through a pointer to a caller
98 /// allocated location passed as an implicit first
99 /// argument to the function.
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000100
Daniel Dunbar11434922009-01-26 21:26:08 +0000101 Ignore, /// Ignore the argument (treat as void). Useful for
102 /// void and empty structs.
103
Daniel Dunbar56273772008-09-17 00:51:38 +0000104 Coerce, /// Only valid for aggregate return types, the argument
105 /// should be accessed by coercion to a provided type.
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000106
107 ByVal, /// Only valid for aggregate argument types. The
108 /// structure should be passed "byval" with the
109 /// specified alignment (0 indicates default
110 /// alignment).
111
112 Expand, /// Only valid for aggregate argument types. The
113 /// structure should be expanded into consecutive
Daniel Dunbar56273772008-09-17 00:51:38 +0000114 /// arguments for its constituent fields. Currently
115 /// expand is only allowed on structures whose fields
116 /// are all scalar types or are themselves expandable
117 /// types.
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000118
119 KindFirst=Default, KindLast=Expand
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000120 };
121
122private:
123 Kind TheKind;
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000124 const llvm::Type *TypeData;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000125 unsigned UIntData;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000126
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000127 ABIArgInfo(Kind K, const llvm::Type *TD=0,
128 unsigned UI=0) : TheKind(K),
129 TypeData(TD),
130 UIntData(0) {}
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000131public:
132 static ABIArgInfo getDefault() {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000133 return ABIArgInfo(Default);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000134 }
135 static ABIArgInfo getStructRet() {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000136 return ABIArgInfo(StructRet);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000137 }
Daniel Dunbar11434922009-01-26 21:26:08 +0000138 static ABIArgInfo getIgnore() {
139 return ABIArgInfo(Ignore);
140 }
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000141 static ABIArgInfo getCoerce(const llvm::Type *T) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000142 return ABIArgInfo(Coerce, T);
143 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000144 static ABIArgInfo getByVal(unsigned Alignment) {
145 return ABIArgInfo(ByVal, 0, Alignment);
146 }
Daniel Dunbar56273772008-09-17 00:51:38 +0000147 static ABIArgInfo getExpand() {
148 return ABIArgInfo(Expand);
149 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000150
151 Kind getKind() const { return TheKind; }
152 bool isDefault() const { return TheKind == Default; }
153 bool isStructRet() const { return TheKind == StructRet; }
Daniel Dunbar11434922009-01-26 21:26:08 +0000154 bool isIgnore() const { return TheKind == Ignore; }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000155 bool isCoerce() const { return TheKind == Coerce; }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000156 bool isByVal() const { return TheKind == ByVal; }
Daniel Dunbar56273772008-09-17 00:51:38 +0000157 bool isExpand() const { return TheKind == Expand; }
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000158
159 // Coerce accessors
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000160 const llvm::Type *getCoerceToType() const {
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000161 assert(TheKind == Coerce && "Invalid kind!");
162 return TypeData;
163 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000164
165 // ByVal accessors
166 unsigned getByValAlignment() const {
167 assert(TheKind == ByVal && "Invalid kind!");
168 return UIntData;
169 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000170};
171
172/***/
173
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000174/* FIXME: All of this stuff should be part of the target interface
175 somehow. It is currently here because it is not clear how to factor
176 the targets to support this, since the Targets currently live in a
177 layer below types n'stuff.
178 */
179
180/// ABIInfo - Target specific hooks for defining how a type should be
181/// passed or returned from functions.
182class clang::ABIInfo {
183public:
184 virtual ~ABIInfo();
185
186 virtual ABIArgInfo classifyReturnType(QualType RetTy,
187 ASTContext &Context) const = 0;
188
189 virtual ABIArgInfo classifyArgumentType(QualType Ty,
190 ASTContext &Context) const = 0;
191};
192
193ABIInfo::~ABIInfo() {}
194
Daniel Dunbar834af452008-09-17 21:22:33 +0000195/// isEmptyStruct - Return true iff a structure has no non-empty
196/// members. Note that a structure with a flexible array member is not
197/// considered empty.
198static bool isEmptyStruct(QualType T) {
199 const RecordType *RT = T->getAsStructureType();
200 if (!RT)
201 return 0;
202 const RecordDecl *RD = RT->getDecl();
203 if (RD->hasFlexibleArrayMember())
204 return false;
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000205 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar834af452008-09-17 21:22:33 +0000206 e = RD->field_end(); i != e; ++i) {
207 const FieldDecl *FD = *i;
208 if (!isEmptyStruct(FD->getType()))
209 return false;
210 }
211 return true;
212}
213
214/// isSingleElementStruct - Determine if a structure is a "single
215/// element struct", i.e. it has exactly one non-empty field or
216/// exactly one field which is itself a single element
217/// struct. Structures with flexible array members are never
218/// considered single element structs.
219///
220/// \return The field declaration for the single non-empty field, if
221/// it exists.
222static const FieldDecl *isSingleElementStruct(QualType T) {
223 const RecordType *RT = T->getAsStructureType();
224 if (!RT)
225 return 0;
226
227 const RecordDecl *RD = RT->getDecl();
228 if (RD->hasFlexibleArrayMember())
229 return 0;
230
231 const FieldDecl *Found = 0;
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000232 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar834af452008-09-17 21:22:33 +0000233 e = RD->field_end(); i != e; ++i) {
234 const FieldDecl *FD = *i;
235 QualType FT = FD->getType();
236
237 if (isEmptyStruct(FT)) {
238 // Ignore
239 } else if (Found) {
240 return 0;
241 } else if (!CodeGenFunction::hasAggregateLLVMType(FT)) {
242 Found = FD;
243 } else {
244 Found = isSingleElementStruct(FT);
245 if (!Found)
246 return 0;
247 }
248 }
249
250 return Found;
251}
252
253static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
254 if (!Ty->getAsBuiltinType() && !Ty->isPointerType())
255 return false;
256
257 uint64_t Size = Context.getTypeSize(Ty);
258 return Size == 32 || Size == 64;
259}
260
261static bool areAllFields32Or64BitBasicType(const RecordDecl *RD,
262 ASTContext &Context) {
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000263 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar834af452008-09-17 21:22:33 +0000264 e = RD->field_end(); i != e; ++i) {
265 const FieldDecl *FD = *i;
266
267 if (!is32Or64BitBasicType(FD->getType(), Context))
268 return false;
269
270 // If this is a bit-field we need to make sure it is still a
271 // 32-bit or 64-bit type.
272 if (Expr *BW = FD->getBitWidth()) {
273 unsigned Width = BW->getIntegerConstantExprValue(Context).getZExtValue();
274 if (Width <= 16)
275 return false;
276 }
277 }
278 return true;
279}
280
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000281namespace {
282/// DefaultABIInfo - The default implementation for ABI specific
283/// details. This implementation provides information which results in
284/// sensible LLVM IR generation, but does not conform to any
285/// particular ABI.
286class DefaultABIInfo : public ABIInfo {
287 virtual ABIArgInfo classifyReturnType(QualType RetTy,
288 ASTContext &Context) const;
289
290 virtual ABIArgInfo classifyArgumentType(QualType RetTy,
291 ASTContext &Context) const;
292};
293
294/// X86_32ABIInfo - The X86-32 ABI information.
295class X86_32ABIInfo : public ABIInfo {
296public:
297 virtual ABIArgInfo classifyReturnType(QualType RetTy,
298 ASTContext &Context) const;
299
300 virtual ABIArgInfo classifyArgumentType(QualType RetTy,
301 ASTContext &Context) const;
302};
303}
304
305ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
306 ASTContext &Context) const {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000307 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar834af452008-09-17 21:22:33 +0000308 // Classify "single element" structs as their element type.
309 const FieldDecl *SeltFD = isSingleElementStruct(RetTy);
310 if (SeltFD) {
311 QualType SeltTy = SeltFD->getType()->getDesugaredType();
312 if (const BuiltinType *BT = SeltTy->getAsBuiltinType()) {
313 // FIXME: This is gross, it would be nice if we could just
314 // pass back SeltTy and have clients deal with it. Is it worth
315 // supporting coerce to both LLVM and clang Types?
316 if (BT->isIntegerType()) {
317 uint64_t Size = Context.getTypeSize(SeltTy);
318 return ABIArgInfo::getCoerce(llvm::IntegerType::get((unsigned) Size));
319 } else if (BT->getKind() == BuiltinType::Float) {
320 return ABIArgInfo::getCoerce(llvm::Type::FloatTy);
321 } else if (BT->getKind() == BuiltinType::Double) {
322 return ABIArgInfo::getCoerce(llvm::Type::DoubleTy);
323 }
324 } else if (SeltTy->isPointerType()) {
325 // FIXME: It would be really nice if this could come out as
326 // the proper pointer type.
327 llvm::Type *PtrTy =
328 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
329 return ABIArgInfo::getCoerce(PtrTy);
330 }
331 }
332
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000333 uint64_t Size = Context.getTypeSize(RetTy);
334 if (Size == 8) {
335 return ABIArgInfo::getCoerce(llvm::Type::Int8Ty);
336 } else if (Size == 16) {
337 return ABIArgInfo::getCoerce(llvm::Type::Int16Ty);
338 } else if (Size == 32) {
339 return ABIArgInfo::getCoerce(llvm::Type::Int32Ty);
340 } else if (Size == 64) {
341 return ABIArgInfo::getCoerce(llvm::Type::Int64Ty);
342 } else {
343 return ABIArgInfo::getStructRet();
344 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000345 } else {
346 return ABIArgInfo::getDefault();
347 }
348}
349
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000350ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
351 ASTContext &Context) const {
Daniel Dunbarf0357382008-09-17 20:11:04 +0000352 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar834af452008-09-17 21:22:33 +0000353 // Structures with flexible arrays are always byval.
354 if (const RecordType *RT = Ty->getAsStructureType())
355 if (RT->getDecl()->hasFlexibleArrayMember())
356 return ABIArgInfo::getByVal(0);
357
358 // Expand empty structs (i.e. ignore)
359 uint64_t Size = Context.getTypeSize(Ty);
360 if (Ty->isStructureType() && Size == 0)
361 return ABIArgInfo::getExpand();
362
363 // Expand structs with size <= 128-bits which consist only of
364 // basic types (int, long long, float, double, xxx*). This is
365 // non-recursive and does not ignore empty fields.
366 if (const RecordType *RT = Ty->getAsStructureType()) {
367 if (Context.getTypeSize(Ty) <= 4*32 &&
368 areAllFields32Or64BitBasicType(RT->getDecl(), Context))
369 return ABIArgInfo::getExpand();
370 }
371
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000372 return ABIArgInfo::getByVal(0);
373 } else {
374 return ABIArgInfo::getDefault();
375 }
376}
377
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000378namespace {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000379/// X86_64ABIInfo - The X86_64 ABI information.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000380class X86_64ABIInfo : public ABIInfo {
381 enum Class {
382 Integer = 0,
383 SSE,
384 SSEUp,
385 X87,
386 X87Up,
387 ComplexX87,
388 NoClass,
389 Memory
390 };
391
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000392 /// merge - Implement the X86_64 ABI merging algorithm.
393 ///
Daniel Dunbarc4503572009-01-31 00:06:58 +0000394 /// Merge an accumulating classification \arg Accum with a field
395 /// classification \arg Field.
396 ///
397 /// \param Accum - The accumulating classification. This should
398 /// always be either NoClass or the result of a previous merge
399 /// call. In addition, this should never be Memory (the caller
400 /// should just return Memory for the aggregate).
401 Class merge(Class Accum, Class Field) const;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000402
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000403 /// classify - Determine the x86_64 register classes in which the
404 /// given type T should be passed.
405 ///
Daniel Dunbarc4503572009-01-31 00:06:58 +0000406 /// \param Lo - The classification for the parts of the type
407 /// residing in the low word of the containing object.
408 ///
409 /// \param Hi - The classification for the parts of the type
410 /// residing in the high word of the containing object.
411 ///
412 /// \param OffsetBase - The bit offset of this type in the
Daniel Dunbarcdf920e2009-01-30 22:40:15 +0000413 /// containing object. Some parameters are classified different
414 /// depending on whether they straddle an eightbyte boundary.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000415 ///
416 /// If a word is unused its result will be NoClass; if a type should
417 /// be passed in Memory then at least the classification of \arg Lo
418 /// will be Memory.
419 ///
420 /// The \arg Lo class will be NoClass iff the argument is ignored.
421 ///
422 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
423 /// be NoClass.
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000424 void classify(QualType T, ASTContext &Context, uint64_t OffsetBase,
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000425 Class &Lo, Class &Hi) const;
Daniel Dunbarc4503572009-01-31 00:06:58 +0000426
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000427public:
428 virtual ABIArgInfo classifyReturnType(QualType RetTy,
429 ASTContext &Context) const;
430
431 virtual ABIArgInfo classifyArgumentType(QualType RetTy,
432 ASTContext &Context) const;
433};
434}
435
Daniel Dunbarc4503572009-01-31 00:06:58 +0000436X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum,
437 Class Field) const {
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000438 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
439 // classified recursively so that always two fields are
440 // considered. The resulting class is calculated according to
441 // the classes of the fields in the eightbyte:
442 //
443 // (a) If both classes are equal, this is the resulting class.
444 //
445 // (b) If one of the classes is NO_CLASS, the resulting class is
446 // the other class.
447 //
448 // (c) If one of the classes is MEMORY, the result is the MEMORY
449 // class.
450 //
451 // (d) If one of the classes is INTEGER, the result is the
452 // INTEGER.
453 //
454 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
455 // MEMORY is used as class.
456 //
457 // (f) Otherwise class SSE is used.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000458 assert((Accum == NoClass || Accum == Integer ||
459 Accum == SSE || Accum == SSEUp) &&
460 "Invalid accumulated classification during merge.");
461 if (Accum == Field || Field == NoClass)
462 return Accum;
463 else if (Field == Memory)
464 return Memory;
465 else if (Accum == NoClass)
466 return Field;
467 else if (Accum == Integer || Field == Integer)
468 return Integer;
469 else if (Field == X87 || Field == X87Up || Field == ComplexX87)
470 return Memory;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000471 else
Daniel Dunbarc4503572009-01-31 00:06:58 +0000472 return SSE;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000473}
474
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000475void X86_64ABIInfo::classify(QualType Ty,
476 ASTContext &Context,
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000477 uint64_t OffsetBase,
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000478 Class &Lo, Class &Hi) const {
Daniel Dunbar9a82b522009-02-02 18:06:39 +0000479 // FIXME: This code can be simplified by introducing a simple value
480 // class for Class pairs with appropriate constructor methods for
481 // the various situations.
482
Daniel Dunbarc4503572009-01-31 00:06:58 +0000483 Lo = Hi = NoClass;
484
485 Class &Current = OffsetBase < 64 ? Lo : Hi;
486 Current = Memory;
487
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000488 if (const BuiltinType *BT = Ty->getAsBuiltinType()) {
489 BuiltinType::Kind k = BT->getKind();
490
Daniel Dunbar11434922009-01-26 21:26:08 +0000491 if (k == BuiltinType::Void) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000492 Current = NoClass;
Daniel Dunbar11434922009-01-26 21:26:08 +0000493 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000494 Current = Integer;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000495 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000496 Current = SSE;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000497 } else if (k == BuiltinType::LongDouble) {
498 Lo = X87;
499 Hi = X87Up;
500 }
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000501 // FIXME: _Decimal32 and _Decimal64 are SSE.
502 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000503 // FIXME: __int128 is (Integer, Integer).
504 } else if (Ty->isPointerLikeType() || Ty->isBlockPointerType() ||
505 Ty->isObjCQualifiedInterfaceType()) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000506 Current = Integer;
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000507 } else if (const VectorType *VT = Ty->getAsVectorType()) {
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000508 uint64_t Size = Context.getTypeSize(VT);
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000509 if (Size == 64) {
Daniel Dunbard4cd1b02009-01-30 19:38:39 +0000510 // gcc passes <1 x double> in memory.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000511 if (VT->getElementType() == Context.DoubleTy)
Daniel Dunbard4cd1b02009-01-30 19:38:39 +0000512 return;
Daniel Dunbard4cd1b02009-01-30 19:38:39 +0000513
Daniel Dunbarc4503572009-01-31 00:06:58 +0000514 Current = SSE;
Daniel Dunbare33edf12009-01-30 18:40:10 +0000515
516 // If this type crosses an eightbyte boundary, it should be
517 // split.
Daniel Dunbarcdf920e2009-01-30 22:40:15 +0000518 if (OffsetBase && OffsetBase != 64)
Daniel Dunbare33edf12009-01-30 18:40:10 +0000519 Hi = Lo;
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000520 } else if (Size == 128) {
521 Lo = SSE;
522 Hi = SSEUp;
523 }
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000524 } else if (const ComplexType *CT = Ty->getAsComplexType()) {
525 QualType ET = CT->getElementType();
526
Daniel Dunbare33edf12009-01-30 18:40:10 +0000527 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbareac48dc2009-01-29 07:22:20 +0000528 if (ET->isIntegerType()) {
Daniel Dunbareac48dc2009-01-29 07:22:20 +0000529 if (Size <= 64)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000530 Current = Integer;
Daniel Dunbareac48dc2009-01-29 07:22:20 +0000531 else if (Size <= 128)
532 Lo = Hi = Integer;
533 } else if (ET == Context.FloatTy)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000534 Current = SSE;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000535 else if (ET == Context.DoubleTy)
536 Lo = Hi = SSE;
537 else if (ET == Context.LongDoubleTy)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000538 Current = ComplexX87;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000539
540 // If this complex type crosses an eightbyte boundary then it
541 // should be split.
Daniel Dunbarcdf920e2009-01-30 22:40:15 +0000542 uint64_t EB_Real = (OffsetBase) / 64;
543 uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000544 if (Hi == NoClass && EB_Real != EB_Imag)
545 Hi = Lo;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000546 } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
547 // Arrays are treated like structures.
548
549 uint64_t Size = Context.getTypeSize(Ty);
550
551 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
552 // than two eightbytes, ..., it has class MEMORY.
553 if (Size > 128)
554 return;
555
556 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
557 // fields, it has class MEMORY.
558 //
559 // Only need to check alignment of array base.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000560 if (OffsetBase % Context.getTypeAlign(AT->getElementType()))
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000561 return;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000562
563 // Otherwise implement simplified merge. We could be smarter about
564 // this, but it isn't worth it and would be harder to verify.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000565 Current = NoClass;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000566 uint64_t EltSize = Context.getTypeSize(AT->getElementType());
567 uint64_t ArraySize = AT->getSize().getZExtValue();
568 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
569 Class FieldLo, FieldHi;
570 classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbarc4503572009-01-31 00:06:58 +0000571 Lo = merge(Lo, FieldLo);
572 Hi = merge(Hi, FieldHi);
573 if (Lo == Memory || Hi == Memory)
574 break;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000575 }
Daniel Dunbarc4503572009-01-31 00:06:58 +0000576
577 // Do post merger cleanup (see below). Only case we worry about is Memory.
578 if (Hi == Memory)
579 Lo = Memory;
580 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Daniel Dunbar99037e52009-01-29 08:13:58 +0000581 } else if (const RecordType *RT = Ty->getAsRecordType()) {
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000582 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbar99037e52009-01-29 08:13:58 +0000583
584 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
585 // than two eightbytes, ..., it has class MEMORY.
586 if (Size > 128)
587 return;
588
589 const RecordDecl *RD = RT->getDecl();
590
591 // Assume variable sized types are passed in memory.
592 if (RD->hasFlexibleArrayMember())
593 return;
594
595 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
596
597 // Reset Lo class, this will be recomputed.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000598 Current = NoClass;
Daniel Dunbar99037e52009-01-29 08:13:58 +0000599 unsigned idx = 0;
600 for (RecordDecl::field_iterator i = RD->field_begin(),
601 e = RD->field_end(); i != e; ++i, ++idx) {
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000602 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Daniel Dunbar99037e52009-01-29 08:13:58 +0000603
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000604 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
605 // fields, it has class MEMORY.
Daniel Dunbar99037e52009-01-29 08:13:58 +0000606 if (Offset % Context.getTypeAlign(i->getType())) {
607 Lo = Memory;
608 return;
609 }
610
Daniel Dunbar99037e52009-01-29 08:13:58 +0000611 // Classify this field.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000612 //
613 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
614 // exceeds a single eightbyte, each is classified
615 // separately. Each eightbyte gets initialized to class
616 // NO_CLASS.
Daniel Dunbar99037e52009-01-29 08:13:58 +0000617 Class FieldLo, FieldHi;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000618 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbarc4503572009-01-31 00:06:58 +0000619 Lo = merge(Lo, FieldLo);
620 Hi = merge(Hi, FieldHi);
621 if (Lo == Memory || Hi == Memory)
622 break;
Daniel Dunbar99037e52009-01-29 08:13:58 +0000623 }
624
625 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
626 //
627 // (a) If one of the classes is MEMORY, the whole argument is
628 // passed in memory.
629 //
630 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
631
632 // The first of these conditions is guaranteed by how we implement
Daniel Dunbarc4503572009-01-31 00:06:58 +0000633 // the merge (just bail).
634 //
635 // The second condition occurs in the case of unions; for example
636 // union { _Complex double; unsigned; }.
637 if (Hi == Memory)
638 Lo = Memory;
Daniel Dunbar99037e52009-01-29 08:13:58 +0000639 if (Hi == SSEUp && Lo != SSE)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000640 Hi = SSE;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000641 }
642}
643
Daniel Dunbarc4503572009-01-31 00:06:58 +0000644
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000645ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
646 ASTContext &Context) const {
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000647 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
648 // classification algorithm.
649 X86_64ABIInfo::Class Lo, Hi;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000650 classify(RetTy, Context, 0, Lo, Hi);
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000651
Daniel Dunbarc4503572009-01-31 00:06:58 +0000652 // Check some invariants.
653 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
654 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
655 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
656
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000657 const llvm::Type *ResType = 0;
658 switch (Lo) {
659 case NoClass:
Daniel Dunbar11434922009-01-26 21:26:08 +0000660 return ABIArgInfo::getIgnore();
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000661
662 case SSEUp:
663 case X87Up:
664 assert(0 && "Invalid classification for lo word.");
665
Daniel Dunbarc4503572009-01-31 00:06:58 +0000666 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
667 // hidden argument, i.e. structret.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000668 case Memory:
669 return ABIArgInfo::getStructRet();
670
671 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
672 // available register of the sequence %rax, %rdx is used.
673 case Integer:
674 ResType = llvm::Type::Int64Ty; break;
675
676 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
677 // available SSE register of the sequence %xmm0, %xmm1 is used.
678 case SSE:
679 ResType = llvm::Type::DoubleTy; break;
680
681 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
682 // returned on the X87 stack in %st0 as 80-bit x87 number.
683 case X87:
684 ResType = llvm::Type::X86_FP80Ty; break;
685
Daniel Dunbarc4503572009-01-31 00:06:58 +0000686 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
687 // part of the value is returned in %st0 and the imaginary part in
688 // %st1.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000689 case ComplexX87:
690 assert(Hi == NoClass && "Unexpected ComplexX87 classification.");
691 ResType = llvm::VectorType::get(llvm::Type::X86_FP80Ty, 2);
692 break;
693 }
694
695 switch (Hi) {
696 // Memory was handled previously, and ComplexX87 and X87 should
697 // never occur as hi classes.
698 case Memory:
699 case X87:
700 case ComplexX87:
701 assert(0 && "Invalid classification for hi word.");
702
703 case NoClass: break;
704 case Integer:
Daniel Dunbarb0e14f22009-01-29 07:36:07 +0000705 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
706 break;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000707 case SSE:
Daniel Dunbarb0e14f22009-01-29 07:36:07 +0000708 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
709 break;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000710
711 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
712 // is passed in the upper half of the last used SSE register.
713 //
714 // SSEUP should always be preceeded by SSE, just widen.
715 case SSEUp:
716 assert(Lo == SSE && "Unexpected SSEUp classification.");
717 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
718 break;
719
720 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
Daniel Dunbarb0e14f22009-01-29 07:36:07 +0000721 // returned together with the previous X87 value in %st0.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000722 //
723 // X87UP should always be preceeded by X87, so we don't need to do
724 // anything here.
725 case X87Up:
726 assert(Lo == X87 && "Unexpected X87Up classification.");
727 break;
728 }
729
730 return ABIArgInfo::getCoerce(ResType);
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000731}
732
733ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty,
734 ASTContext &Context) const {
735 return ABIArgInfo::getDefault();
736}
737
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000738ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
739 ASTContext &Context) const {
740 return ABIArgInfo::getDefault();
741}
742
743ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
744 ASTContext &Context) const {
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000745 return ABIArgInfo::getDefault();
746}
747
748const ABIInfo &CodeGenTypes::getABIInfo() const {
749 if (TheABIInfo)
750 return *TheABIInfo;
751
752 // For now we just cache this in the CodeGenTypes and don't bother
753 // to free it.
754 const char *TargetPrefix = getContext().Target.getTargetPrefix();
755 if (strcmp(TargetPrefix, "x86") == 0) {
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000756 switch (getContext().Target.getPointerWidth(0)) {
757 case 32:
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000758 return *(TheABIInfo = new X86_32ABIInfo());
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000759 case 64:
Daniel Dunbar11a76ed2009-01-30 18:47:53 +0000760 return *(TheABIInfo = new X86_64ABIInfo());
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000761 }
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000762 }
763
764 return *(TheABIInfo = new DefaultABIInfo);
765}
766
767// getABIReturnInfo - Wrap the ABIInfo getABIReturnInfo, altering
768// "default" types to StructRet when appropriate for simplicity.
769static ABIArgInfo getABIReturnInfo(QualType Ty, CodeGenTypes &CGT) {
770 assert(!Ty->isArrayType() &&
771 "Array types cannot be passed directly.");
772 ABIArgInfo Info = CGT.getABIInfo().classifyReturnType(Ty, CGT.getContext());
Daniel Dunbarf0357382008-09-17 20:11:04 +0000773 // Ensure default on aggregate types is StructRet.
774 if (Info.isDefault() && CodeGenFunction::hasAggregateLLVMType(Ty))
775 return ABIArgInfo::getStructRet();
776 return Info;
777}
778
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000779// getABIArgumentInfo - Wrap the ABIInfo getABIReturnInfo, altering
780// "default" types to ByVal when appropriate for simplicity.
781static ABIArgInfo getABIArgumentInfo(QualType Ty, CodeGenTypes &CGT) {
782 assert(!Ty->isArrayType() &&
783 "Array types cannot be passed directly.");
784 ABIArgInfo Info = CGT.getABIInfo().classifyArgumentType(Ty, CGT.getContext());
Daniel Dunbarf0357382008-09-17 20:11:04 +0000785 // Ensure default on aggregate types is ByVal.
786 if (Info.isDefault() && CodeGenFunction::hasAggregateLLVMType(Ty))
787 return ABIArgInfo::getByVal(0);
788 return Info;
789}
790
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000791/***/
792
Daniel Dunbar56273772008-09-17 00:51:38 +0000793void CodeGenTypes::GetExpandedTypes(QualType Ty,
794 std::vector<const llvm::Type*> &ArgTys) {
795 const RecordType *RT = Ty->getAsStructureType();
796 assert(RT && "Can only expand structure types.");
797 const RecordDecl *RD = RT->getDecl();
798 assert(!RD->hasFlexibleArrayMember() &&
799 "Cannot expand structure with flexible array.");
800
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000801 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar56273772008-09-17 00:51:38 +0000802 e = RD->field_end(); i != e; ++i) {
803 const FieldDecl *FD = *i;
804 assert(!FD->isBitField() &&
805 "Cannot expand structure with bit-field members.");
806
807 QualType FT = FD->getType();
808 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
809 GetExpandedTypes(FT, ArgTys);
810 } else {
811 ArgTys.push_back(ConvertType(FT));
812 }
813 }
814}
815
816llvm::Function::arg_iterator
817CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
818 llvm::Function::arg_iterator AI) {
819 const RecordType *RT = Ty->getAsStructureType();
820 assert(RT && "Can only expand structure types.");
821
822 RecordDecl *RD = RT->getDecl();
823 assert(LV.isSimple() &&
824 "Unexpected non-simple lvalue during struct expansion.");
825 llvm::Value *Addr = LV.getAddress();
826 for (RecordDecl::field_iterator i = RD->field_begin(),
827 e = RD->field_end(); i != e; ++i) {
828 FieldDecl *FD = *i;
829 QualType FT = FD->getType();
830
831 // FIXME: What are the right qualifiers here?
832 LValue LV = EmitLValueForField(Addr, FD, false, 0);
833 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
834 AI = ExpandTypeFromArgs(FT, LV, AI);
835 } else {
836 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
837 ++AI;
838 }
839 }
840
841 return AI;
842}
843
844void
845CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
846 llvm::SmallVector<llvm::Value*, 16> &Args) {
847 const RecordType *RT = Ty->getAsStructureType();
848 assert(RT && "Can only expand structure types.");
849
850 RecordDecl *RD = RT->getDecl();
851 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
852 llvm::Value *Addr = RV.getAggregateAddr();
853 for (RecordDecl::field_iterator i = RD->field_begin(),
854 e = RD->field_end(); i != e; ++i) {
855 FieldDecl *FD = *i;
856 QualType FT = FD->getType();
857
858 // FIXME: What are the right qualifiers here?
859 LValue LV = EmitLValueForField(Addr, FD, false, 0);
860 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
861 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
862 } else {
863 RValue RV = EmitLoadOfLValue(LV, FT);
864 assert(RV.isScalar() &&
865 "Unexpected non-scalar rvalue during struct expansion.");
866 Args.push_back(RV.getScalarVal());
867 }
868 }
869}
870
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000871/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
872/// a pointer to an object of type \arg Ty.
873///
874/// This safely handles the case when the src type is smaller than the
875/// destination type; in this situation the values of bits which not
876/// present in the src are undefined.
877static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
878 const llvm::Type *Ty,
879 CodeGenFunction &CGF) {
880 const llvm::Type *SrcTy =
881 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
882 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
883 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(Ty);
884
885 // If load is legal, just bitcase the src pointer.
886 if (SrcSize == DstSize) {
887 llvm::Value *Casted =
888 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
889 return CGF.Builder.CreateLoad(Casted);
890 } else {
891 assert(SrcSize < DstSize && "Coercion is losing source bits!");
892
893 // Otherwise do coercion through memory. This is stupid, but
894 // simple.
895 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
896 llvm::Value *Casted =
897 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
898 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
899 return CGF.Builder.CreateLoad(Tmp);
900 }
901}
902
903/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
904/// where the source and destination may have different types.
905///
906/// This safely handles the case when the src type is larger than the
907/// destination type; the upper bits of the src will be lost.
908static void CreateCoercedStore(llvm::Value *Src,
909 llvm::Value *DstPtr,
910 CodeGenFunction &CGF) {
911 const llvm::Type *SrcTy = Src->getType();
912 const llvm::Type *DstTy =
913 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
914
915 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
916 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(DstTy);
917
918 // If store is legal, just bitcase the src pointer.
919 if (SrcSize == DstSize) {
920 llvm::Value *Casted =
921 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
922 CGF.Builder.CreateStore(Src, Casted);
923 } else {
924 assert(SrcSize > DstSize && "Coercion is missing bits!");
925
926 // Otherwise do coercion through memory. This is stupid, but
927 // simple.
928 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
929 CGF.Builder.CreateStore(Src, Tmp);
930 llvm::Value *Casted =
931 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
932 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(Casted), DstPtr);
933 }
934}
935
Daniel Dunbar56273772008-09-17 00:51:38 +0000936/***/
937
Daniel Dunbar88b53962009-02-02 22:03:45 +0000938bool CodeGenModule::ReturnTypeUsesSret(const CGFunctionInfo &FI) {
939 return getABIReturnInfo(FI.getReturnType(), getTypes()).isStructRet();
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000940}
941
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000942const llvm::FunctionType *
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000943CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic) {
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000944 std::vector<const llvm::Type*> ArgTys;
945
946 const llvm::Type *ResultType = 0;
947
Daniel Dunbar36b5f5e2009-01-31 03:05:44 +0000948 ArgTypeIterator begin = FI.argtypes_begin(), end = FI.argtypes_end();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000949 QualType RetTy = *begin;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000950 ABIArgInfo RetAI = getABIReturnInfo(RetTy, *this);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000951 switch (RetAI.getKind()) {
952 case ABIArgInfo::ByVal:
953 case ABIArgInfo::Expand:
954 assert(0 && "Invalid ABI kind for return argument");
955
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000956 case ABIArgInfo::Default:
957 if (RetTy->isVoidType()) {
958 ResultType = llvm::Type::VoidTy;
959 } else {
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000960 ResultType = ConvertType(RetTy);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000961 }
962 break;
963
964 case ABIArgInfo::StructRet: {
965 ResultType = llvm::Type::VoidTy;
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000966 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000967 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
968 break;
969 }
970
Daniel Dunbar11434922009-01-26 21:26:08 +0000971 case ABIArgInfo::Ignore:
972 ResultType = llvm::Type::VoidTy;
973 break;
974
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000975 case ABIArgInfo::Coerce:
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000976 ResultType = RetAI.getCoerceToType();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000977 break;
978 }
979
980 for (++begin; begin != end; ++begin) {
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000981 ABIArgInfo AI = getABIArgumentInfo(*begin, *this);
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000982 const llvm::Type *Ty = ConvertType(*begin);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000983
984 switch (AI.getKind()) {
Daniel Dunbar11434922009-01-26 21:26:08 +0000985 case ABIArgInfo::Ignore:
986 break;
987
Daniel Dunbar56273772008-09-17 00:51:38 +0000988 case ABIArgInfo::Coerce:
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000989 case ABIArgInfo::StructRet:
990 assert(0 && "Invalid ABI kind for non-return argument");
991
992 case ABIArgInfo::ByVal:
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000993 // byval arguments are always on the stack, which is addr space #0.
994 ArgTys.push_back(llvm::PointerType::getUnqual(Ty));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000995 assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled");
996 break;
997
998 case ABIArgInfo::Default:
999 ArgTys.push_back(Ty);
1000 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001001
1002 case ABIArgInfo::Expand:
Daniel Dunbar56273772008-09-17 00:51:38 +00001003 GetExpandedTypes(*begin, ArgTys);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001004 break;
1005 }
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001006 }
1007
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001008 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar3913f182008-09-09 23:48:28 +00001009}
1010
Daniel Dunbar88b53962009-02-02 22:03:45 +00001011void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &Info,
1012 const Decl *TargetDecl,
Devang Patel761d7f72008-09-25 21:02:23 +00001013 AttributeListType &PAL) {
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001014 unsigned FuncAttrs = 0;
Devang Patela2c69122008-09-26 22:53:57 +00001015 unsigned RetAttrs = 0;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001016
1017 if (TargetDecl) {
1018 if (TargetDecl->getAttr<NoThrowAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +00001019 FuncAttrs |= llvm::Attribute::NoUnwind;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001020 if (TargetDecl->getAttr<NoReturnAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +00001021 FuncAttrs |= llvm::Attribute::NoReturn;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001022 if (TargetDecl->getAttr<PureAttr>())
1023 FuncAttrs |= llvm::Attribute::ReadOnly;
1024 if (TargetDecl->getAttr<ConstAttr>())
1025 FuncAttrs |= llvm::Attribute::ReadNone;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001026 }
1027
Daniel Dunbar725ad312009-01-31 02:19:00 +00001028 ArgTypeIterator begin = Info.argtypes_begin(), end = Info.argtypes_end();
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001029 QualType RetTy = *begin;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001030 unsigned Index = 1;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001031 ABIArgInfo RetAI = getABIReturnInfo(RetTy, getTypes());
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001032 switch (RetAI.getKind()) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001033 case ABIArgInfo::Default:
1034 if (RetTy->isPromotableIntegerType()) {
1035 if (RetTy->isSignedIntegerType()) {
Devang Patela2c69122008-09-26 22:53:57 +00001036 RetAttrs |= llvm::Attribute::SExt;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001037 } else if (RetTy->isUnsignedIntegerType()) {
Devang Patela2c69122008-09-26 22:53:57 +00001038 RetAttrs |= llvm::Attribute::ZExt;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001039 }
1040 }
1041 break;
1042
1043 case ABIArgInfo::StructRet:
Devang Patel761d7f72008-09-25 21:02:23 +00001044 PAL.push_back(llvm::AttributeWithIndex::get(Index,
Daniel Dunbar725ad312009-01-31 02:19:00 +00001045 llvm::Attribute::StructRet |
1046 llvm::Attribute::NoAlias));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001047 ++Index;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001048 break;
1049
Daniel Dunbar11434922009-01-26 21:26:08 +00001050 case ABIArgInfo::Ignore:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001051 case ABIArgInfo::Coerce:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001052 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001053
1054 case ABIArgInfo::ByVal:
1055 case ABIArgInfo::Expand:
1056 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001057 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001058
Devang Patela2c69122008-09-26 22:53:57 +00001059 if (RetAttrs)
1060 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Daniel Dunbar56273772008-09-17 00:51:38 +00001061 for (++begin; begin != end; ++begin) {
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001062 QualType ParamType = *begin;
Devang Patel761d7f72008-09-25 21:02:23 +00001063 unsigned Attributes = 0;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001064 ABIArgInfo AI = getABIArgumentInfo(ParamType, getTypes());
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001065
1066 switch (AI.getKind()) {
1067 case ABIArgInfo::StructRet:
Daniel Dunbar56273772008-09-17 00:51:38 +00001068 case ABIArgInfo::Coerce:
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001069 assert(0 && "Invalid ABI kind for non-return argument");
1070
1071 case ABIArgInfo::ByVal:
Devang Patel761d7f72008-09-25 21:02:23 +00001072 Attributes |= llvm::Attribute::ByVal;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001073 assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled");
1074 break;
1075
1076 case ABIArgInfo::Default:
1077 if (ParamType->isPromotableIntegerType()) {
1078 if (ParamType->isSignedIntegerType()) {
Devang Patel761d7f72008-09-25 21:02:23 +00001079 Attributes |= llvm::Attribute::SExt;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001080 } else if (ParamType->isUnsignedIntegerType()) {
Devang Patel761d7f72008-09-25 21:02:23 +00001081 Attributes |= llvm::Attribute::ZExt;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001082 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001083 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001084 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001085
Daniel Dunbar11434922009-01-26 21:26:08 +00001086 case ABIArgInfo::Ignore:
1087 // Skip increment, no matching LLVM parameter.
1088 continue;
1089
Daniel Dunbar56273772008-09-17 00:51:38 +00001090 case ABIArgInfo::Expand: {
1091 std::vector<const llvm::Type*> Tys;
1092 // FIXME: This is rather inefficient. Do we ever actually need
1093 // to do anything here? The result should be just reconstructed
1094 // on the other side, so extension should be a non-issue.
1095 getTypes().GetExpandedTypes(ParamType, Tys);
1096 Index += Tys.size();
1097 continue;
1098 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001099 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001100
Devang Patel761d7f72008-09-25 21:02:23 +00001101 if (Attributes)
1102 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar56273772008-09-17 00:51:38 +00001103 ++Index;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001104 }
Devang Patela2c69122008-09-26 22:53:57 +00001105 if (FuncAttrs)
1106 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
1107
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001108}
1109
Daniel Dunbar88b53962009-02-02 22:03:45 +00001110void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
1111 llvm::Function *Fn,
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001112 const FunctionArgList &Args) {
1113 // Emit allocs for param decls. Give the LLVM Argument nodes names.
1114 llvm::Function::arg_iterator AI = Fn->arg_begin();
1115
1116 // Name the struct return argument.
Daniel Dunbar88b53962009-02-02 22:03:45 +00001117 if (CGM.ReturnTypeUsesSret(FI)) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001118 AI->setName("agg.result");
1119 ++AI;
1120 }
1121
1122 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbar56273772008-09-17 00:51:38 +00001123 i != e; ++i) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001124 const VarDecl *Arg = i->first;
Daniel Dunbar56273772008-09-17 00:51:38 +00001125 QualType Ty = i->second;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001126 ABIArgInfo ArgI = getABIArgumentInfo(Ty, CGM.getTypes());
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001127
1128 switch (ArgI.getKind()) {
1129 case ABIArgInfo::ByVal:
1130 case ABIArgInfo::Default: {
1131 assert(AI != Fn->arg_end() && "Argument mismatch!");
1132 llvm::Value* V = AI;
Daniel Dunbar56273772008-09-17 00:51:38 +00001133 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001134 // This must be a promotion, for something like
1135 // "void a(x) short x; {..."
Daniel Dunbar56273772008-09-17 00:51:38 +00001136 V = EmitScalarConversion(V, Ty, Arg->getType());
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001137 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001138 EmitParmDecl(*Arg, V);
1139 break;
1140 }
Daniel Dunbar56273772008-09-17 00:51:38 +00001141
1142 case ABIArgInfo::Expand: {
1143 // If this was structure was expand into multiple arguments then
1144 // we need to create a temporary and reconstruct it from the
1145 // arguments.
Chris Lattner39f34e92008-11-24 04:00:27 +00001146 std::string Name = Arg->getNameAsString();
Daniel Dunbar56273772008-09-17 00:51:38 +00001147 llvm::Value *Temp = CreateTempAlloca(ConvertType(Ty),
1148 (Name + ".addr").c_str());
1149 // FIXME: What are the right qualifiers here?
1150 llvm::Function::arg_iterator End =
1151 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI);
1152 EmitParmDecl(*Arg, Temp);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001153
Daniel Dunbar56273772008-09-17 00:51:38 +00001154 // Name the arguments used in expansion and increment AI.
1155 unsigned Index = 0;
1156 for (; AI != End; ++AI, ++Index)
1157 AI->setName(Name + "." + llvm::utostr(Index));
1158 continue;
1159 }
Daniel Dunbar11434922009-01-26 21:26:08 +00001160
1161 case ABIArgInfo::Ignore:
1162 break;
1163
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001164 case ABIArgInfo::Coerce:
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001165 case ABIArgInfo::StructRet:
1166 assert(0 && "Invalid ABI kind for non-return argument");
1167 }
Daniel Dunbar56273772008-09-17 00:51:38 +00001168
1169 ++AI;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001170 }
1171 assert(AI == Fn->arg_end() && "Argument mismatch!");
1172}
1173
Daniel Dunbar88b53962009-02-02 22:03:45 +00001174void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI,
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001175 llvm::Value *ReturnValue) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001176 llvm::Value *RV = 0;
1177
1178 // Functions with no result always return void.
1179 if (ReturnValue) {
Daniel Dunbar88b53962009-02-02 22:03:45 +00001180 QualType RetTy = FI.getReturnType();
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001181 ABIArgInfo RetAI = getABIReturnInfo(RetTy, CGM.getTypes());
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001182
1183 switch (RetAI.getKind()) {
1184 case ABIArgInfo::StructRet:
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001185 if (RetTy->isAnyComplexType()) {
1186 // FIXME: Volatile
1187 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
1188 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
1189 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1190 EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy);
1191 } else {
1192 Builder.CreateStore(Builder.CreateLoad(ReturnValue),
1193 CurFn->arg_begin());
1194 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001195 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001196
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001197 case ABIArgInfo::Default:
1198 RV = Builder.CreateLoad(ReturnValue);
1199 break;
1200
Daniel Dunbar11434922009-01-26 21:26:08 +00001201 case ABIArgInfo::Ignore:
1202 break;
1203
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001204 case ABIArgInfo::Coerce: {
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +00001205 RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001206 break;
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001207 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001208
1209 case ABIArgInfo::ByVal:
1210 case ABIArgInfo::Expand:
1211 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001212 }
1213 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001214
1215 if (RV) {
1216 Builder.CreateRet(RV);
1217 } else {
1218 Builder.CreateRetVoid();
1219 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001220}
1221
Daniel Dunbar88b53962009-02-02 22:03:45 +00001222RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
1223 llvm::Value *Callee,
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001224 const CallArgList &CallArgs) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001225 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001226
1227 // Handle struct-return functions by passing a pointer to the
1228 // location that we would like to return into.
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001229 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001230 ABIArgInfo RetAI = getABIReturnInfo(RetTy, CGM.getTypes());
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001231 switch (RetAI.getKind()) {
1232 case ABIArgInfo::StructRet:
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001233 // Create a temporary alloca to hold the result of the call. :(
Daniel Dunbar56273772008-09-17 00:51:38 +00001234 Args.push_back(CreateTempAlloca(ConvertType(RetTy)));
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001235 break;
1236
1237 case ABIArgInfo::Default:
Daniel Dunbar11434922009-01-26 21:26:08 +00001238 case ABIArgInfo::Ignore:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001239 case ABIArgInfo::Coerce:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001240 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001241
1242 case ABIArgInfo::ByVal:
1243 case ABIArgInfo::Expand:
1244 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001245 }
1246
1247 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
1248 I != E; ++I) {
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001249 ABIArgInfo ArgInfo = getABIArgumentInfo(I->second, CGM.getTypes());
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001250 RValue RV = I->first;
Daniel Dunbar56273772008-09-17 00:51:38 +00001251
1252 switch (ArgInfo.getKind()) {
1253 case ABIArgInfo::ByVal: // Default is byval
1254 case ABIArgInfo::Default:
1255 if (RV.isScalar()) {
1256 Args.push_back(RV.getScalarVal());
1257 } else if (RV.isComplex()) {
1258 // Make a temporary alloca to pass the argument.
1259 Args.push_back(CreateTempAlloca(ConvertType(I->second)));
1260 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
1261 } else {
1262 Args.push_back(RV.getAggregateAddr());
1263 }
1264 break;
1265
Daniel Dunbar11434922009-01-26 21:26:08 +00001266 case ABIArgInfo::Ignore:
1267 break;
1268
Daniel Dunbar56273772008-09-17 00:51:38 +00001269 case ABIArgInfo::StructRet:
1270 case ABIArgInfo::Coerce:
1271 assert(0 && "Invalid ABI kind for non-return argument");
1272 break;
1273
1274 case ABIArgInfo::Expand:
1275 ExpandTypeToArgs(I->second, RV, Args);
1276 break;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001277 }
1278 }
1279
1280 llvm::CallInst *CI = Builder.CreateCall(Callee,&Args[0],&Args[0]+Args.size());
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001281
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001282 // FIXME: Provide TargetDecl so nounwind, noreturn, etc, etc get set.
Devang Patel761d7f72008-09-25 21:02:23 +00001283 CodeGen::AttributeListType AttributeList;
Daniel Dunbar88b53962009-02-02 22:03:45 +00001284 CGM.ConstructAttributeList(CallInfo, 0, AttributeList);
Devang Patel761d7f72008-09-25 21:02:23 +00001285 CI->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(),
Daniel Dunbar725ad312009-01-31 02:19:00 +00001286 AttributeList.size()));
1287
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001288 if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee))
1289 CI->setCallingConv(F->getCallingConv());
1290 if (CI->getType() != llvm::Type::VoidTy)
1291 CI->setName("call");
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001292
1293 switch (RetAI.getKind()) {
1294 case ABIArgInfo::StructRet:
1295 if (RetTy->isAnyComplexType())
Daniel Dunbar56273772008-09-17 00:51:38 +00001296 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001297 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar56273772008-09-17 00:51:38 +00001298 return RValue::getAggregate(Args[0]);
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001299 else
1300 return RValue::get(Builder.CreateLoad(Args[0]));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001301
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001302 case ABIArgInfo::Default:
1303 return RValue::get(RetTy->isVoidType() ? 0 : CI);
1304
Daniel Dunbar11434922009-01-26 21:26:08 +00001305 case ABIArgInfo::Ignore:
Daniel Dunbarcc039fe2009-01-29 08:24:57 +00001306 if (RetTy->isVoidType())
1307 return RValue::get(0);
1308 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1309 llvm::Value *Res =
1310 llvm::UndefValue::get(llvm::PointerType::getUnqual(ConvertType(RetTy)));
1311 return RValue::getAggregate(Res);
1312 }
1313 return RValue::get(llvm::UndefValue::get(ConvertType(RetTy)));
Daniel Dunbar11434922009-01-26 21:26:08 +00001314
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001315 case ABIArgInfo::Coerce: {
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +00001316 llvm::Value *V = CreateTempAlloca(ConvertType(RetTy), "coerce");
1317 CreateCoercedStore(CI, V, *this);
Anders Carlssonad3d6912008-11-25 22:21:48 +00001318 if (RetTy->isAnyComplexType())
1319 return RValue::getComplex(LoadComplexFromAddr(V, false));
Daniel Dunbar11434922009-01-26 21:26:08 +00001320 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssonad3d6912008-11-25 22:21:48 +00001321 return RValue::getAggregate(V);
Daniel Dunbar11434922009-01-26 21:26:08 +00001322 else
1323 return RValue::get(Builder.CreateLoad(V));
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001324 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001325
1326 case ABIArgInfo::ByVal:
1327 case ABIArgInfo::Expand:
1328 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001329 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001330
1331 assert(0 && "Unhandled ABIArgInfo::Kind");
1332 return RValue::get(0);
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001333}