blob: 08d5da30fa35fac2a2a38f7e03911858dbf3cacd [file] [log] [blame]
Daniel Dunbara8f02052008-09-08 21:33:45 +00001//===----- CGCall.h - Encapsulate calling convention details ----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// These classes wrap the information about a call or function
11// definition used to handle ABI compliancy.
12//
13//===----------------------------------------------------------------------===//
14
15#include "CGCall.h"
16#include "CodeGenFunction.h"
Daniel Dunbar3ef2e852008-09-10 00:41:16 +000017#include "CodeGenModule.h"
Daniel Dunbarf98eeff2008-10-13 17:02:26 +000018#include "clang/Basic/TargetInfo.h"
Daniel Dunbara8f02052008-09-08 21:33:45 +000019#include "clang/AST/ASTContext.h"
20#include "clang/AST/Decl.h"
21#include "clang/AST/DeclObjC.h"
Daniel Dunbar51a2d192009-01-29 08:13:58 +000022#include "clang/AST/RecordLayout.h"
Daniel Dunbar04d35782008-09-17 00:51:38 +000023#include "llvm/ADT/StringExtras.h"
Devang Patel98bfe502008-09-24 01:01:36 +000024#include "llvm/Attributes.h"
Daniel Dunbare09a9692009-01-24 08:32:22 +000025#include "llvm/Support/CommandLine.h"
Daniel Dunbar708d8a82009-01-27 01:36:03 +000026#include "llvm/Target/TargetData.h"
Daniel Dunbara8f02052008-09-08 21:33:45 +000027using namespace clang;
28using namespace CodeGen;
29
30/***/
31
Daniel Dunbara8f02052008-09-08 21:33:45 +000032// FIXME: Use iterator and sidestep silly type array creation.
33
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000034CGFunctionInfo::CGFunctionInfo(const FunctionTypeNoProto *FTNP)
35 : IsVariadic(true)
36{
37 ArgTypes.push_back(FTNP->getResultType());
38}
39
40CGFunctionInfo::CGFunctionInfo(const FunctionTypeProto *FTP)
41 : IsVariadic(FTP->isVariadic())
42{
43 ArgTypes.push_back(FTP->getResultType());
44 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
45 ArgTypes.push_back(FTP->getArgType(i));
46}
47
48// FIXME: Is there really any reason to have this still?
Daniel Dunbara8f02052008-09-08 21:33:45 +000049CGFunctionInfo::CGFunctionInfo(const FunctionDecl *FD)
Daniel Dunbara8f02052008-09-08 21:33:45 +000050{
51 const FunctionType *FTy = FD->getType()->getAsFunctionType();
52 const FunctionTypeProto *FTP = dyn_cast<FunctionTypeProto>(FTy);
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000053
Daniel Dunbara8f02052008-09-08 21:33:45 +000054 ArgTypes.push_back(FTy->getResultType());
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000055 if (FTP) {
56 IsVariadic = FTP->isVariadic();
Daniel Dunbara8f02052008-09-08 21:33:45 +000057 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
58 ArgTypes.push_back(FTP->getArgType(i));
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000059 } else {
60 IsVariadic = true;
61 }
Daniel Dunbara8f02052008-09-08 21:33:45 +000062}
63
64CGFunctionInfo::CGFunctionInfo(const ObjCMethodDecl *MD,
65 const ASTContext &Context)
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000066 : IsVariadic(MD->isVariadic())
Daniel Dunbara8f02052008-09-08 21:33:45 +000067{
68 ArgTypes.push_back(MD->getResultType());
69 ArgTypes.push_back(MD->getSelfDecl()->getType());
70 ArgTypes.push_back(Context.getObjCSelType());
71 for (ObjCMethodDecl::param_const_iterator i = MD->param_begin(),
72 e = MD->param_end(); i != e; ++i)
73 ArgTypes.push_back((*i)->getType());
74}
75
Daniel Dunbarebbb8f32009-01-31 02:19:00 +000076CGFunctionInfo::CGFunctionInfo(QualType ResTy, const CallArgList &Args,
77 bool _IsVariadic)
78 : IsVariadic(_IsVariadic)
79{
80 ArgTypes.push_back(ResTy);
81 for (CallArgList::const_iterator i = Args.begin(), e = Args.end();
82 i != e; ++i)
83 ArgTypes.push_back(i->second);
84}
85
Daniel Dunbarbccb0682008-09-10 00:32:18 +000086ArgTypeIterator CGFunctionInfo::argtypes_begin() const {
87 return ArgTypes.begin();
88}
89
90ArgTypeIterator CGFunctionInfo::argtypes_end() const {
91 return ArgTypes.end();
Daniel Dunbara8f02052008-09-08 21:33:45 +000092}
93
94/***/
95
Daniel Dunbar22e30052008-09-11 01:48:57 +000096/// ABIArgInfo - Helper class to encapsulate information about how a
97/// specific C type should be passed to or returned from a function.
Daniel Dunbare126ab12008-09-10 02:41:04 +000098class ABIArgInfo {
99public:
100 enum Kind {
101 Default,
Daniel Dunbar17d35372008-12-18 04:52:14 +0000102 StructRet, /// Only valid for return values. The return value
103 /// should be passed through a pointer to a caller
104 /// allocated location passed as an implicit first
105 /// argument to the function.
Daniel Dunbar22e30052008-09-11 01:48:57 +0000106
Daniel Dunbar1358b202009-01-26 21:26:08 +0000107 Ignore, /// Ignore the argument (treat as void). Useful for
108 /// void and empty structs.
109
Daniel Dunbar04d35782008-09-17 00:51:38 +0000110 Coerce, /// Only valid for aggregate return types, the argument
111 /// should be accessed by coercion to a provided type.
Daniel Dunbar22e30052008-09-11 01:48:57 +0000112
113 ByVal, /// Only valid for aggregate argument types. The
114 /// structure should be passed "byval" with the
115 /// specified alignment (0 indicates default
116 /// alignment).
117
118 Expand, /// Only valid for aggregate argument types. The
119 /// structure should be expanded into consecutive
Daniel Dunbar04d35782008-09-17 00:51:38 +0000120 /// arguments for its constituent fields. Currently
121 /// expand is only allowed on structures whose fields
122 /// are all scalar types or are themselves expandable
123 /// types.
Daniel Dunbar22e30052008-09-11 01:48:57 +0000124
125 KindFirst=Default, KindLast=Expand
Daniel Dunbare126ab12008-09-10 02:41:04 +0000126 };
127
128private:
129 Kind TheKind;
Daniel Dunbar73d66602008-09-10 07:04:09 +0000130 const llvm::Type *TypeData;
Daniel Dunbar22e30052008-09-11 01:48:57 +0000131 unsigned UIntData;
Daniel Dunbare126ab12008-09-10 02:41:04 +0000132
Daniel Dunbar22e30052008-09-11 01:48:57 +0000133 ABIArgInfo(Kind K, const llvm::Type *TD=0,
134 unsigned UI=0) : TheKind(K),
135 TypeData(TD),
136 UIntData(0) {}
Daniel Dunbare126ab12008-09-10 02:41:04 +0000137public:
138 static ABIArgInfo getDefault() {
Daniel Dunbar22e30052008-09-11 01:48:57 +0000139 return ABIArgInfo(Default);
Daniel Dunbare126ab12008-09-10 02:41:04 +0000140 }
141 static ABIArgInfo getStructRet() {
Daniel Dunbar22e30052008-09-11 01:48:57 +0000142 return ABIArgInfo(StructRet);
Daniel Dunbare126ab12008-09-10 02:41:04 +0000143 }
Daniel Dunbar1358b202009-01-26 21:26:08 +0000144 static ABIArgInfo getIgnore() {
145 return ABIArgInfo(Ignore);
146 }
Daniel Dunbar73d66602008-09-10 07:04:09 +0000147 static ABIArgInfo getCoerce(const llvm::Type *T) {
Daniel Dunbare126ab12008-09-10 02:41:04 +0000148 return ABIArgInfo(Coerce, T);
149 }
Daniel Dunbar22e30052008-09-11 01:48:57 +0000150 static ABIArgInfo getByVal(unsigned Alignment) {
151 return ABIArgInfo(ByVal, 0, Alignment);
152 }
Daniel Dunbar04d35782008-09-17 00:51:38 +0000153 static ABIArgInfo getExpand() {
154 return ABIArgInfo(Expand);
155 }
Daniel Dunbare126ab12008-09-10 02:41:04 +0000156
157 Kind getKind() const { return TheKind; }
158 bool isDefault() const { return TheKind == Default; }
159 bool isStructRet() const { return TheKind == StructRet; }
Daniel Dunbar1358b202009-01-26 21:26:08 +0000160 bool isIgnore() const { return TheKind == Ignore; }
Daniel Dunbare126ab12008-09-10 02:41:04 +0000161 bool isCoerce() const { return TheKind == Coerce; }
Daniel Dunbar22e30052008-09-11 01:48:57 +0000162 bool isByVal() const { return TheKind == ByVal; }
Daniel Dunbar04d35782008-09-17 00:51:38 +0000163 bool isExpand() const { return TheKind == Expand; }
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000164
165 // Coerce accessors
Daniel Dunbar73d66602008-09-10 07:04:09 +0000166 const llvm::Type *getCoerceToType() const {
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000167 assert(TheKind == Coerce && "Invalid kind!");
168 return TypeData;
169 }
Daniel Dunbar22e30052008-09-11 01:48:57 +0000170
171 // ByVal accessors
172 unsigned getByValAlignment() const {
173 assert(TheKind == ByVal && "Invalid kind!");
174 return UIntData;
175 }
Daniel Dunbare126ab12008-09-10 02:41:04 +0000176};
177
178/***/
179
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000180/* FIXME: All of this stuff should be part of the target interface
181 somehow. It is currently here because it is not clear how to factor
182 the targets to support this, since the Targets currently live in a
183 layer below types n'stuff.
184 */
185
186/// ABIInfo - Target specific hooks for defining how a type should be
187/// passed or returned from functions.
188class clang::ABIInfo {
189public:
190 virtual ~ABIInfo();
191
192 virtual ABIArgInfo classifyReturnType(QualType RetTy,
193 ASTContext &Context) const = 0;
194
195 virtual ABIArgInfo classifyArgumentType(QualType Ty,
196 ASTContext &Context) const = 0;
197};
198
199ABIInfo::~ABIInfo() {}
200
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000201/// isEmptyStruct - Return true iff a structure has no non-empty
202/// members. Note that a structure with a flexible array member is not
203/// considered empty.
204static bool isEmptyStruct(QualType T) {
205 const RecordType *RT = T->getAsStructureType();
206 if (!RT)
207 return 0;
208 const RecordDecl *RD = RT->getDecl();
209 if (RD->hasFlexibleArrayMember())
210 return false;
Douglas Gregor5d764842009-01-09 17:18:27 +0000211 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000212 e = RD->field_end(); i != e; ++i) {
213 const FieldDecl *FD = *i;
214 if (!isEmptyStruct(FD->getType()))
215 return false;
216 }
217 return true;
218}
219
220/// isSingleElementStruct - Determine if a structure is a "single
221/// element struct", i.e. it has exactly one non-empty field or
222/// exactly one field which is itself a single element
223/// struct. Structures with flexible array members are never
224/// considered single element structs.
225///
226/// \return The field declaration for the single non-empty field, if
227/// it exists.
228static const FieldDecl *isSingleElementStruct(QualType T) {
229 const RecordType *RT = T->getAsStructureType();
230 if (!RT)
231 return 0;
232
233 const RecordDecl *RD = RT->getDecl();
234 if (RD->hasFlexibleArrayMember())
235 return 0;
236
237 const FieldDecl *Found = 0;
Douglas Gregor5d764842009-01-09 17:18:27 +0000238 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000239 e = RD->field_end(); i != e; ++i) {
240 const FieldDecl *FD = *i;
241 QualType FT = FD->getType();
242
243 if (isEmptyStruct(FT)) {
244 // Ignore
245 } else if (Found) {
246 return 0;
247 } else if (!CodeGenFunction::hasAggregateLLVMType(FT)) {
248 Found = FD;
249 } else {
250 Found = isSingleElementStruct(FT);
251 if (!Found)
252 return 0;
253 }
254 }
255
256 return Found;
257}
258
259static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
260 if (!Ty->getAsBuiltinType() && !Ty->isPointerType())
261 return false;
262
263 uint64_t Size = Context.getTypeSize(Ty);
264 return Size == 32 || Size == 64;
265}
266
267static bool areAllFields32Or64BitBasicType(const RecordDecl *RD,
268 ASTContext &Context) {
Douglas Gregor5d764842009-01-09 17:18:27 +0000269 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000270 e = RD->field_end(); i != e; ++i) {
271 const FieldDecl *FD = *i;
272
273 if (!is32Or64BitBasicType(FD->getType(), Context))
274 return false;
275
276 // If this is a bit-field we need to make sure it is still a
277 // 32-bit or 64-bit type.
278 if (Expr *BW = FD->getBitWidth()) {
279 unsigned Width = BW->getIntegerConstantExprValue(Context).getZExtValue();
280 if (Width <= 16)
281 return false;
282 }
283 }
284 return true;
285}
286
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000287namespace {
288/// DefaultABIInfo - The default implementation for ABI specific
289/// details. This implementation provides information which results in
290/// sensible LLVM IR generation, but does not conform to any
291/// particular ABI.
292class DefaultABIInfo : public ABIInfo {
293 virtual ABIArgInfo classifyReturnType(QualType RetTy,
294 ASTContext &Context) const;
295
296 virtual ABIArgInfo classifyArgumentType(QualType RetTy,
297 ASTContext &Context) const;
298};
299
300/// X86_32ABIInfo - The X86-32 ABI information.
301class X86_32ABIInfo : public ABIInfo {
302public:
303 virtual ABIArgInfo classifyReturnType(QualType RetTy,
304 ASTContext &Context) const;
305
306 virtual ABIArgInfo classifyArgumentType(QualType RetTy,
307 ASTContext &Context) const;
308};
309}
310
311ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
312 ASTContext &Context) const {
Daniel Dunbare126ab12008-09-10 02:41:04 +0000313 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000314 // Classify "single element" structs as their element type.
315 const FieldDecl *SeltFD = isSingleElementStruct(RetTy);
316 if (SeltFD) {
317 QualType SeltTy = SeltFD->getType()->getDesugaredType();
318 if (const BuiltinType *BT = SeltTy->getAsBuiltinType()) {
319 // FIXME: This is gross, it would be nice if we could just
320 // pass back SeltTy and have clients deal with it. Is it worth
321 // supporting coerce to both LLVM and clang Types?
322 if (BT->isIntegerType()) {
323 uint64_t Size = Context.getTypeSize(SeltTy);
324 return ABIArgInfo::getCoerce(llvm::IntegerType::get((unsigned) Size));
325 } else if (BT->getKind() == BuiltinType::Float) {
326 return ABIArgInfo::getCoerce(llvm::Type::FloatTy);
327 } else if (BT->getKind() == BuiltinType::Double) {
328 return ABIArgInfo::getCoerce(llvm::Type::DoubleTy);
329 }
330 } else if (SeltTy->isPointerType()) {
331 // FIXME: It would be really nice if this could come out as
332 // the proper pointer type.
333 llvm::Type *PtrTy =
334 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
335 return ABIArgInfo::getCoerce(PtrTy);
336 }
337 }
338
Daniel Dunbar73d66602008-09-10 07:04:09 +0000339 uint64_t Size = Context.getTypeSize(RetTy);
340 if (Size == 8) {
341 return ABIArgInfo::getCoerce(llvm::Type::Int8Ty);
342 } else if (Size == 16) {
343 return ABIArgInfo::getCoerce(llvm::Type::Int16Ty);
344 } else if (Size == 32) {
345 return ABIArgInfo::getCoerce(llvm::Type::Int32Ty);
346 } else if (Size == 64) {
347 return ABIArgInfo::getCoerce(llvm::Type::Int64Ty);
348 } else {
349 return ABIArgInfo::getStructRet();
350 }
Daniel Dunbare126ab12008-09-10 02:41:04 +0000351 } else {
352 return ABIArgInfo::getDefault();
353 }
354}
355
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000356ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
357 ASTContext &Context) const {
Daniel Dunbar3158c592008-09-17 20:11:04 +0000358 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000359 // Structures with flexible arrays are always byval.
360 if (const RecordType *RT = Ty->getAsStructureType())
361 if (RT->getDecl()->hasFlexibleArrayMember())
362 return ABIArgInfo::getByVal(0);
363
364 // Expand empty structs (i.e. ignore)
365 uint64_t Size = Context.getTypeSize(Ty);
366 if (Ty->isStructureType() && Size == 0)
367 return ABIArgInfo::getExpand();
368
369 // Expand structs with size <= 128-bits which consist only of
370 // basic types (int, long long, float, double, xxx*). This is
371 // non-recursive and does not ignore empty fields.
372 if (const RecordType *RT = Ty->getAsStructureType()) {
373 if (Context.getTypeSize(Ty) <= 4*32 &&
374 areAllFields32Or64BitBasicType(RT->getDecl(), Context))
375 return ABIArgInfo::getExpand();
376 }
377
Daniel Dunbar22e30052008-09-11 01:48:57 +0000378 return ABIArgInfo::getByVal(0);
379 } else {
380 return ABIArgInfo::getDefault();
381 }
382}
383
Daniel Dunbare09a9692009-01-24 08:32:22 +0000384namespace {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000385/// X86_64ABIInfo - The X86_64 ABI information.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000386class X86_64ABIInfo : public ABIInfo {
387 enum Class {
388 Integer = 0,
389 SSE,
390 SSEUp,
391 X87,
392 X87Up,
393 ComplexX87,
394 NoClass,
395 Memory
396 };
397
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000398 /// merge - Implement the X86_64 ABI merging algorithm.
399 ///
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000400 /// Merge an accumulating classification \arg Accum with a field
401 /// classification \arg Field.
402 ///
403 /// \param Accum - The accumulating classification. This should
404 /// always be either NoClass or the result of a previous merge
405 /// call. In addition, this should never be Memory (the caller
406 /// should just return Memory for the aggregate).
407 Class merge(Class Accum, Class Field) const;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000408
Daniel Dunbare09a9692009-01-24 08:32:22 +0000409 /// classify - Determine the x86_64 register classes in which the
410 /// given type T should be passed.
411 ///
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000412 /// \param Lo - The classification for the parts of the type
413 /// residing in the low word of the containing object.
414 ///
415 /// \param Hi - The classification for the parts of the type
416 /// residing in the high word of the containing object.
417 ///
418 /// \param OffsetBase - The bit offset of this type in the
Daniel Dunbar2a2dce32009-01-30 22:40:15 +0000419 /// containing object. Some parameters are classified different
420 /// depending on whether they straddle an eightbyte boundary.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000421 ///
422 /// If a word is unused its result will be NoClass; if a type should
423 /// be passed in Memory then at least the classification of \arg Lo
424 /// will be Memory.
425 ///
426 /// The \arg Lo class will be NoClass iff the argument is ignored.
427 ///
428 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
429 /// be NoClass.
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000430 void classify(QualType T, ASTContext &Context, uint64_t OffsetBase,
Daniel Dunbare09a9692009-01-24 08:32:22 +0000431 Class &Lo, Class &Hi) const;
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000432
Daniel Dunbare09a9692009-01-24 08:32:22 +0000433public:
434 virtual ABIArgInfo classifyReturnType(QualType RetTy,
435 ASTContext &Context) const;
436
437 virtual ABIArgInfo classifyArgumentType(QualType RetTy,
438 ASTContext &Context) const;
439};
440}
441
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000442X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum,
443 Class Field) const {
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000444 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
445 // classified recursively so that always two fields are
446 // considered. The resulting class is calculated according to
447 // the classes of the fields in the eightbyte:
448 //
449 // (a) If both classes are equal, this is the resulting class.
450 //
451 // (b) If one of the classes is NO_CLASS, the resulting class is
452 // the other class.
453 //
454 // (c) If one of the classes is MEMORY, the result is the MEMORY
455 // class.
456 //
457 // (d) If one of the classes is INTEGER, the result is the
458 // INTEGER.
459 //
460 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
461 // MEMORY is used as class.
462 //
463 // (f) Otherwise class SSE is used.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000464 assert((Accum == NoClass || Accum == Integer ||
465 Accum == SSE || Accum == SSEUp) &&
466 "Invalid accumulated classification during merge.");
467 if (Accum == Field || Field == NoClass)
468 return Accum;
469 else if (Field == Memory)
470 return Memory;
471 else if (Accum == NoClass)
472 return Field;
473 else if (Accum == Integer || Field == Integer)
474 return Integer;
475 else if (Field == X87 || Field == X87Up || Field == ComplexX87)
476 return Memory;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000477 else
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000478 return SSE;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000479}
480
Daniel Dunbare09a9692009-01-24 08:32:22 +0000481void X86_64ABIInfo::classify(QualType Ty,
482 ASTContext &Context,
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000483 uint64_t OffsetBase,
Daniel Dunbare09a9692009-01-24 08:32:22 +0000484 Class &Lo, Class &Hi) const {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000485 Lo = Hi = NoClass;
486
487 Class &Current = OffsetBase < 64 ? Lo : Hi;
488 Current = Memory;
489
Daniel Dunbare09a9692009-01-24 08:32:22 +0000490 if (const BuiltinType *BT = Ty->getAsBuiltinType()) {
491 BuiltinType::Kind k = BT->getKind();
492
Daniel Dunbar1358b202009-01-26 21:26:08 +0000493 if (k == BuiltinType::Void) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000494 Current = NoClass;
Daniel Dunbar1358b202009-01-26 21:26:08 +0000495 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000496 Current = Integer;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000497 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000498 Current = SSE;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000499 } else if (k == BuiltinType::LongDouble) {
500 Lo = X87;
501 Hi = X87Up;
502 }
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000503 // FIXME: _Decimal32 and _Decimal64 are SSE.
504 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Daniel Dunbare09a9692009-01-24 08:32:22 +0000505 // FIXME: __int128 is (Integer, Integer).
506 } else if (Ty->isPointerLikeType() || Ty->isBlockPointerType() ||
507 Ty->isObjCQualifiedInterfaceType()) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000508 Current = Integer;
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000509 } else if (const VectorType *VT = Ty->getAsVectorType()) {
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000510 uint64_t Size = Context.getTypeSize(VT);
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000511 if (Size == 64) {
Daniel Dunbarcdf91e82009-01-30 19:38:39 +0000512 // gcc passes <1 x double> in memory.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000513 if (VT->getElementType() == Context.DoubleTy)
Daniel Dunbarcdf91e82009-01-30 19:38:39 +0000514 return;
Daniel Dunbarcdf91e82009-01-30 19:38:39 +0000515
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000516 Current = SSE;
Daniel Dunbare413f532009-01-30 18:40:10 +0000517
518 // If this type crosses an eightbyte boundary, it should be
519 // split.
Daniel Dunbar2a2dce32009-01-30 22:40:15 +0000520 if (OffsetBase && OffsetBase != 64)
Daniel Dunbare413f532009-01-30 18:40:10 +0000521 Hi = Lo;
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000522 } else if (Size == 128) {
523 Lo = SSE;
524 Hi = SSEUp;
525 }
Daniel Dunbare09a9692009-01-24 08:32:22 +0000526 } else if (const ComplexType *CT = Ty->getAsComplexType()) {
527 QualType ET = CT->getElementType();
528
Daniel Dunbare413f532009-01-30 18:40:10 +0000529 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbar28770fc2009-01-29 07:22:20 +0000530 if (ET->isIntegerType()) {
Daniel Dunbar28770fc2009-01-29 07:22:20 +0000531 if (Size <= 64)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000532 Current = Integer;
Daniel Dunbar28770fc2009-01-29 07:22:20 +0000533 else if (Size <= 128)
534 Lo = Hi = Integer;
535 } else if (ET == Context.FloatTy)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000536 Current = SSE;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000537 else if (ET == Context.DoubleTy)
538 Lo = Hi = SSE;
539 else if (ET == Context.LongDoubleTy)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000540 Current = ComplexX87;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000541
542 // If this complex type crosses an eightbyte boundary then it
543 // should be split.
Daniel Dunbar2a2dce32009-01-30 22:40:15 +0000544 uint64_t EB_Real = (OffsetBase) / 64;
545 uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000546 if (Hi == NoClass && EB_Real != EB_Imag)
547 Hi = Lo;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000548 } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
549 // Arrays are treated like structures.
550
551 uint64_t Size = Context.getTypeSize(Ty);
552
553 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
554 // than two eightbytes, ..., it has class MEMORY.
555 if (Size > 128)
556 return;
557
558 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
559 // fields, it has class MEMORY.
560 //
561 // Only need to check alignment of array base.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000562 if (OffsetBase % Context.getTypeAlign(AT->getElementType()))
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000563 return;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000564
565 // Otherwise implement simplified merge. We could be smarter about
566 // this, but it isn't worth it and would be harder to verify.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000567 Current = NoClass;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000568 uint64_t EltSize = Context.getTypeSize(AT->getElementType());
569 uint64_t ArraySize = AT->getSize().getZExtValue();
570 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
571 Class FieldLo, FieldHi;
572 classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000573 Lo = merge(Lo, FieldLo);
574 Hi = merge(Hi, FieldHi);
575 if (Lo == Memory || Hi == Memory)
576 break;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000577 }
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000578
579 // Do post merger cleanup (see below). Only case we worry about is Memory.
580 if (Hi == Memory)
581 Lo = Memory;
582 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000583 } else if (const RecordType *RT = Ty->getAsRecordType()) {
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000584 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000585
586 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
587 // than two eightbytes, ..., it has class MEMORY.
588 if (Size > 128)
589 return;
590
591 const RecordDecl *RD = RT->getDecl();
592
593 // Assume variable sized types are passed in memory.
594 if (RD->hasFlexibleArrayMember())
595 return;
596
597 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
598
599 // Reset Lo class, this will be recomputed.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000600 Current = NoClass;
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000601 unsigned idx = 0;
602 for (RecordDecl::field_iterator i = RD->field_begin(),
603 e = RD->field_end(); i != e; ++i, ++idx) {
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000604 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000605
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000606 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
607 // fields, it has class MEMORY.
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000608 if (Offset % Context.getTypeAlign(i->getType())) {
609 Lo = Memory;
610 return;
611 }
612
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000613 // Classify this field.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000614 //
615 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
616 // exceeds a single eightbyte, each is classified
617 // separately. Each eightbyte gets initialized to class
618 // NO_CLASS.
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000619 Class FieldLo, FieldHi;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000620 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000621 Lo = merge(Lo, FieldLo);
622 Hi = merge(Hi, FieldHi);
623 if (Lo == Memory || Hi == Memory)
624 break;
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000625 }
626
627 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
628 //
629 // (a) If one of the classes is MEMORY, the whole argument is
630 // passed in memory.
631 //
632 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
633
634 // The first of these conditions is guaranteed by how we implement
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000635 // the merge (just bail).
636 //
637 // The second condition occurs in the case of unions; for example
638 // union { _Complex double; unsigned; }.
639 if (Hi == Memory)
640 Lo = Memory;
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000641 if (Hi == SSEUp && Lo != SSE)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000642 Hi = SSE;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000643 }
644}
645
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000646
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000647ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
648 ASTContext &Context) const {
Daniel Dunbare09a9692009-01-24 08:32:22 +0000649 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
650 // classification algorithm.
651 X86_64ABIInfo::Class Lo, Hi;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000652 classify(RetTy, Context, 0, Lo, Hi);
Daniel Dunbare09a9692009-01-24 08:32:22 +0000653
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000654 // Check some invariants.
655 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
656 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
657 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
658
Daniel Dunbare09a9692009-01-24 08:32:22 +0000659 const llvm::Type *ResType = 0;
660 switch (Lo) {
661 case NoClass:
Daniel Dunbar1358b202009-01-26 21:26:08 +0000662 return ABIArgInfo::getIgnore();
Daniel Dunbare09a9692009-01-24 08:32:22 +0000663
664 case SSEUp:
665 case X87Up:
666 assert(0 && "Invalid classification for lo word.");
667
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000668 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
669 // hidden argument, i.e. structret.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000670 case Memory:
671 return ABIArgInfo::getStructRet();
672
673 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
674 // available register of the sequence %rax, %rdx is used.
675 case Integer:
676 ResType = llvm::Type::Int64Ty; break;
677
678 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
679 // available SSE register of the sequence %xmm0, %xmm1 is used.
680 case SSE:
681 ResType = llvm::Type::DoubleTy; break;
682
683 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
684 // returned on the X87 stack in %st0 as 80-bit x87 number.
685 case X87:
686 ResType = llvm::Type::X86_FP80Ty; break;
687
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000688 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
689 // part of the value is returned in %st0 and the imaginary part in
690 // %st1.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000691 case ComplexX87:
692 assert(Hi == NoClass && "Unexpected ComplexX87 classification.");
693 ResType = llvm::VectorType::get(llvm::Type::X86_FP80Ty, 2);
694 break;
695 }
696
697 switch (Hi) {
698 // Memory was handled previously, and ComplexX87 and X87 should
699 // never occur as hi classes.
700 case Memory:
701 case X87:
702 case ComplexX87:
703 assert(0 && "Invalid classification for hi word.");
704
705 case NoClass: break;
706 case Integer:
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000707 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
708 break;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000709 case SSE:
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000710 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
711 break;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000712
713 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
714 // is passed in the upper half of the last used SSE register.
715 //
716 // SSEUP should always be preceeded by SSE, just widen.
717 case SSEUp:
718 assert(Lo == SSE && "Unexpected SSEUp classification.");
719 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
720 break;
721
722 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000723 // returned together with the previous X87 value in %st0.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000724 //
725 // X87UP should always be preceeded by X87, so we don't need to do
726 // anything here.
727 case X87Up:
728 assert(Lo == X87 && "Unexpected X87Up classification.");
729 break;
730 }
731
732 return ABIArgInfo::getCoerce(ResType);
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000733}
734
735ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty,
736 ASTContext &Context) const {
737 return ABIArgInfo::getDefault();
738}
739
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000740ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
741 ASTContext &Context) const {
742 return ABIArgInfo::getDefault();
743}
744
745ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
746 ASTContext &Context) const {
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000747 return ABIArgInfo::getDefault();
748}
749
750const ABIInfo &CodeGenTypes::getABIInfo() const {
751 if (TheABIInfo)
752 return *TheABIInfo;
753
754 // For now we just cache this in the CodeGenTypes and don't bother
755 // to free it.
756 const char *TargetPrefix = getContext().Target.getTargetPrefix();
757 if (strcmp(TargetPrefix, "x86") == 0) {
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000758 switch (getContext().Target.getPointerWidth(0)) {
759 case 32:
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000760 return *(TheABIInfo = new X86_32ABIInfo());
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000761 case 64:
Daniel Dunbar56555952009-01-30 18:47:53 +0000762 return *(TheABIInfo = new X86_64ABIInfo());
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000763 }
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000764 }
765
766 return *(TheABIInfo = new DefaultABIInfo);
767}
768
769// getABIReturnInfo - Wrap the ABIInfo getABIReturnInfo, altering
770// "default" types to StructRet when appropriate for simplicity.
771static ABIArgInfo getABIReturnInfo(QualType Ty, CodeGenTypes &CGT) {
772 assert(!Ty->isArrayType() &&
773 "Array types cannot be passed directly.");
774 ABIArgInfo Info = CGT.getABIInfo().classifyReturnType(Ty, CGT.getContext());
Daniel Dunbar3158c592008-09-17 20:11:04 +0000775 // Ensure default on aggregate types is StructRet.
776 if (Info.isDefault() && CodeGenFunction::hasAggregateLLVMType(Ty))
777 return ABIArgInfo::getStructRet();
778 return Info;
779}
780
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000781// getABIArgumentInfo - Wrap the ABIInfo getABIReturnInfo, altering
782// "default" types to ByVal when appropriate for simplicity.
783static ABIArgInfo getABIArgumentInfo(QualType Ty, CodeGenTypes &CGT) {
784 assert(!Ty->isArrayType() &&
785 "Array types cannot be passed directly.");
786 ABIArgInfo Info = CGT.getABIInfo().classifyArgumentType(Ty, CGT.getContext());
Daniel Dunbar3158c592008-09-17 20:11:04 +0000787 // Ensure default on aggregate types is ByVal.
788 if (Info.isDefault() && CodeGenFunction::hasAggregateLLVMType(Ty))
789 return ABIArgInfo::getByVal(0);
790 return Info;
791}
792
Daniel Dunbare126ab12008-09-10 02:41:04 +0000793/***/
794
Daniel Dunbar04d35782008-09-17 00:51:38 +0000795void CodeGenTypes::GetExpandedTypes(QualType Ty,
796 std::vector<const llvm::Type*> &ArgTys) {
797 const RecordType *RT = Ty->getAsStructureType();
798 assert(RT && "Can only expand structure types.");
799 const RecordDecl *RD = RT->getDecl();
800 assert(!RD->hasFlexibleArrayMember() &&
801 "Cannot expand structure with flexible array.");
802
Douglas Gregor5d764842009-01-09 17:18:27 +0000803 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar04d35782008-09-17 00:51:38 +0000804 e = RD->field_end(); i != e; ++i) {
805 const FieldDecl *FD = *i;
806 assert(!FD->isBitField() &&
807 "Cannot expand structure with bit-field members.");
808
809 QualType FT = FD->getType();
810 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
811 GetExpandedTypes(FT, ArgTys);
812 } else {
813 ArgTys.push_back(ConvertType(FT));
814 }
815 }
816}
817
818llvm::Function::arg_iterator
819CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
820 llvm::Function::arg_iterator AI) {
821 const RecordType *RT = Ty->getAsStructureType();
822 assert(RT && "Can only expand structure types.");
823
824 RecordDecl *RD = RT->getDecl();
825 assert(LV.isSimple() &&
826 "Unexpected non-simple lvalue during struct expansion.");
827 llvm::Value *Addr = LV.getAddress();
828 for (RecordDecl::field_iterator i = RD->field_begin(),
829 e = RD->field_end(); i != e; ++i) {
830 FieldDecl *FD = *i;
831 QualType FT = FD->getType();
832
833 // FIXME: What are the right qualifiers here?
834 LValue LV = EmitLValueForField(Addr, FD, false, 0);
835 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
836 AI = ExpandTypeFromArgs(FT, LV, AI);
837 } else {
838 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
839 ++AI;
840 }
841 }
842
843 return AI;
844}
845
846void
847CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
848 llvm::SmallVector<llvm::Value*, 16> &Args) {
849 const RecordType *RT = Ty->getAsStructureType();
850 assert(RT && "Can only expand structure types.");
851
852 RecordDecl *RD = RT->getDecl();
853 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
854 llvm::Value *Addr = RV.getAggregateAddr();
855 for (RecordDecl::field_iterator i = RD->field_begin(),
856 e = RD->field_end(); i != e; ++i) {
857 FieldDecl *FD = *i;
858 QualType FT = FD->getType();
859
860 // FIXME: What are the right qualifiers here?
861 LValue LV = EmitLValueForField(Addr, FD, false, 0);
862 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
863 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
864 } else {
865 RValue RV = EmitLoadOfLValue(LV, FT);
866 assert(RV.isScalar() &&
867 "Unexpected non-scalar rvalue during struct expansion.");
868 Args.push_back(RV.getScalarVal());
869 }
870 }
871}
872
873/***/
874
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000875const llvm::FunctionType *
876CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI) {
Daniel Dunbarebbb8f32009-01-31 02:19:00 +0000877 return GetFunctionType(FI.argtypes_begin(), FI.argtypes_end(),
878 FI.isVariadic());
Daniel Dunbara9976a22008-09-10 07:00:50 +0000879}
880
881const llvm::FunctionType *
882CodeGenTypes::GetFunctionType(ArgTypeIterator begin, ArgTypeIterator end,
883 bool IsVariadic) {
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000884 std::vector<const llvm::Type*> ArgTys;
885
886 const llvm::Type *ResultType = 0;
887
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000888 QualType RetTy = *begin;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000889 ABIArgInfo RetAI = getABIReturnInfo(RetTy, *this);
Daniel Dunbar22e30052008-09-11 01:48:57 +0000890 switch (RetAI.getKind()) {
891 case ABIArgInfo::ByVal:
892 case ABIArgInfo::Expand:
893 assert(0 && "Invalid ABI kind for return argument");
894
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000895 case ABIArgInfo::Default:
896 if (RetTy->isVoidType()) {
897 ResultType = llvm::Type::VoidTy;
898 } else {
Daniel Dunbara9976a22008-09-10 07:00:50 +0000899 ResultType = ConvertType(RetTy);
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000900 }
901 break;
902
903 case ABIArgInfo::StructRet: {
904 ResultType = llvm::Type::VoidTy;
Daniel Dunbara9976a22008-09-10 07:00:50 +0000905 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000906 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
907 break;
908 }
909
Daniel Dunbar1358b202009-01-26 21:26:08 +0000910 case ABIArgInfo::Ignore:
911 ResultType = llvm::Type::VoidTy;
912 break;
913
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000914 case ABIArgInfo::Coerce:
Daniel Dunbar73d66602008-09-10 07:04:09 +0000915 ResultType = RetAI.getCoerceToType();
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000916 break;
917 }
918
919 for (++begin; begin != end; ++begin) {
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000920 ABIArgInfo AI = getABIArgumentInfo(*begin, *this);
Daniel Dunbara9976a22008-09-10 07:00:50 +0000921 const llvm::Type *Ty = ConvertType(*begin);
Daniel Dunbar22e30052008-09-11 01:48:57 +0000922
923 switch (AI.getKind()) {
Daniel Dunbar1358b202009-01-26 21:26:08 +0000924 case ABIArgInfo::Ignore:
925 break;
926
Daniel Dunbar04d35782008-09-17 00:51:38 +0000927 case ABIArgInfo::Coerce:
Daniel Dunbar22e30052008-09-11 01:48:57 +0000928 case ABIArgInfo::StructRet:
929 assert(0 && "Invalid ABI kind for non-return argument");
930
931 case ABIArgInfo::ByVal:
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000932 // byval arguments are always on the stack, which is addr space #0.
933 ArgTys.push_back(llvm::PointerType::getUnqual(Ty));
Daniel Dunbar22e30052008-09-11 01:48:57 +0000934 assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled");
935 break;
936
937 case ABIArgInfo::Default:
938 ArgTys.push_back(Ty);
939 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +0000940
941 case ABIArgInfo::Expand:
Daniel Dunbar04d35782008-09-17 00:51:38 +0000942 GetExpandedTypes(*begin, ArgTys);
Daniel Dunbar22e30052008-09-11 01:48:57 +0000943 break;
944 }
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000945 }
946
Daniel Dunbara9976a22008-09-10 07:00:50 +0000947 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000948}
949
Daniel Dunbar3ef2e852008-09-10 00:41:16 +0000950bool CodeGenModule::ReturnTypeUsesSret(QualType RetTy) {
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000951 return getABIReturnInfo(RetTy, getTypes()).isStructRet();
Daniel Dunbar49f5a0d2008-09-09 23:48:28 +0000952}
953
Devang Patela85a9ef2008-09-25 21:02:23 +0000954void CodeGenModule::ConstructAttributeList(const Decl *TargetDecl,
Daniel Dunbarebbb8f32009-01-31 02:19:00 +0000955 const CGFunctionInfo &Info,
Devang Patela85a9ef2008-09-25 21:02:23 +0000956 AttributeListType &PAL) {
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000957 unsigned FuncAttrs = 0;
Devang Patel2bb6eb82008-09-26 22:53:57 +0000958 unsigned RetAttrs = 0;
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000959
960 if (TargetDecl) {
961 if (TargetDecl->getAttr<NoThrowAttr>())
Devang Patela85a9ef2008-09-25 21:02:23 +0000962 FuncAttrs |= llvm::Attribute::NoUnwind;
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000963 if (TargetDecl->getAttr<NoReturnAttr>())
Devang Patela85a9ef2008-09-25 21:02:23 +0000964 FuncAttrs |= llvm::Attribute::NoReturn;
Anders Carlssondd6791c2008-10-05 23:32:53 +0000965 if (TargetDecl->getAttr<PureAttr>())
966 FuncAttrs |= llvm::Attribute::ReadOnly;
967 if (TargetDecl->getAttr<ConstAttr>())
968 FuncAttrs |= llvm::Attribute::ReadNone;
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000969 }
970
Daniel Dunbarebbb8f32009-01-31 02:19:00 +0000971 ArgTypeIterator begin = Info.argtypes_begin(), end = Info.argtypes_end();
Daniel Dunbare126ab12008-09-10 02:41:04 +0000972 QualType RetTy = *begin;
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000973 unsigned Index = 1;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000974 ABIArgInfo RetAI = getABIReturnInfo(RetTy, getTypes());
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000975 switch (RetAI.getKind()) {
Daniel Dunbare126ab12008-09-10 02:41:04 +0000976 case ABIArgInfo::Default:
977 if (RetTy->isPromotableIntegerType()) {
978 if (RetTy->isSignedIntegerType()) {
Devang Patel2bb6eb82008-09-26 22:53:57 +0000979 RetAttrs |= llvm::Attribute::SExt;
Daniel Dunbare126ab12008-09-10 02:41:04 +0000980 } else if (RetTy->isUnsignedIntegerType()) {
Devang Patel2bb6eb82008-09-26 22:53:57 +0000981 RetAttrs |= llvm::Attribute::ZExt;
Daniel Dunbare126ab12008-09-10 02:41:04 +0000982 }
983 }
984 break;
985
986 case ABIArgInfo::StructRet:
Devang Patela85a9ef2008-09-25 21:02:23 +0000987 PAL.push_back(llvm::AttributeWithIndex::get(Index,
Daniel Dunbarebbb8f32009-01-31 02:19:00 +0000988 llvm::Attribute::StructRet |
989 llvm::Attribute::NoAlias));
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000990 ++Index;
Daniel Dunbare126ab12008-09-10 02:41:04 +0000991 break;
992
Daniel Dunbar1358b202009-01-26 21:26:08 +0000993 case ABIArgInfo::Ignore:
Daniel Dunbare126ab12008-09-10 02:41:04 +0000994 case ABIArgInfo::Coerce:
Daniel Dunbare126ab12008-09-10 02:41:04 +0000995 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +0000996
997 case ABIArgInfo::ByVal:
998 case ABIArgInfo::Expand:
999 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001000 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001001
Devang Patel2bb6eb82008-09-26 22:53:57 +00001002 if (RetAttrs)
1003 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Daniel Dunbar04d35782008-09-17 00:51:38 +00001004 for (++begin; begin != end; ++begin) {
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001005 QualType ParamType = *begin;
Devang Patela85a9ef2008-09-25 21:02:23 +00001006 unsigned Attributes = 0;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001007 ABIArgInfo AI = getABIArgumentInfo(ParamType, getTypes());
Daniel Dunbar22e30052008-09-11 01:48:57 +00001008
1009 switch (AI.getKind()) {
1010 case ABIArgInfo::StructRet:
Daniel Dunbar04d35782008-09-17 00:51:38 +00001011 case ABIArgInfo::Coerce:
Daniel Dunbar22e30052008-09-11 01:48:57 +00001012 assert(0 && "Invalid ABI kind for non-return argument");
1013
1014 case ABIArgInfo::ByVal:
Devang Patela85a9ef2008-09-25 21:02:23 +00001015 Attributes |= llvm::Attribute::ByVal;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001016 assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled");
1017 break;
1018
1019 case ABIArgInfo::Default:
1020 if (ParamType->isPromotableIntegerType()) {
1021 if (ParamType->isSignedIntegerType()) {
Devang Patela85a9ef2008-09-25 21:02:23 +00001022 Attributes |= llvm::Attribute::SExt;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001023 } else if (ParamType->isUnsignedIntegerType()) {
Devang Patela85a9ef2008-09-25 21:02:23 +00001024 Attributes |= llvm::Attribute::ZExt;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001025 }
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001026 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001027 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001028
Daniel Dunbar1358b202009-01-26 21:26:08 +00001029 case ABIArgInfo::Ignore:
1030 // Skip increment, no matching LLVM parameter.
1031 continue;
1032
Daniel Dunbar04d35782008-09-17 00:51:38 +00001033 case ABIArgInfo::Expand: {
1034 std::vector<const llvm::Type*> Tys;
1035 // FIXME: This is rather inefficient. Do we ever actually need
1036 // to do anything here? The result should be just reconstructed
1037 // on the other side, so extension should be a non-issue.
1038 getTypes().GetExpandedTypes(ParamType, Tys);
1039 Index += Tys.size();
1040 continue;
1041 }
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001042 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001043
Devang Patela85a9ef2008-09-25 21:02:23 +00001044 if (Attributes)
1045 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar04d35782008-09-17 00:51:38 +00001046 ++Index;
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001047 }
Devang Patel2bb6eb82008-09-26 22:53:57 +00001048 if (FuncAttrs)
1049 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
1050
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001051}
1052
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001053void CodeGenFunction::EmitFunctionProlog(llvm::Function *Fn,
1054 QualType RetTy,
1055 const FunctionArgList &Args) {
1056 // Emit allocs for param decls. Give the LLVM Argument nodes names.
1057 llvm::Function::arg_iterator AI = Fn->arg_begin();
1058
1059 // Name the struct return argument.
Daniel Dunbare126ab12008-09-10 02:41:04 +00001060 if (CGM.ReturnTypeUsesSret(RetTy)) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001061 AI->setName("agg.result");
1062 ++AI;
1063 }
1064
1065 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbar04d35782008-09-17 00:51:38 +00001066 i != e; ++i) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001067 const VarDecl *Arg = i->first;
Daniel Dunbar04d35782008-09-17 00:51:38 +00001068 QualType Ty = i->second;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001069 ABIArgInfo ArgI = getABIArgumentInfo(Ty, CGM.getTypes());
Daniel Dunbar22e30052008-09-11 01:48:57 +00001070
1071 switch (ArgI.getKind()) {
1072 case ABIArgInfo::ByVal:
1073 case ABIArgInfo::Default: {
1074 assert(AI != Fn->arg_end() && "Argument mismatch!");
1075 llvm::Value* V = AI;
Daniel Dunbar04d35782008-09-17 00:51:38 +00001076 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
Daniel Dunbar22e30052008-09-11 01:48:57 +00001077 // This must be a promotion, for something like
1078 // "void a(x) short x; {..."
Daniel Dunbar04d35782008-09-17 00:51:38 +00001079 V = EmitScalarConversion(V, Ty, Arg->getType());
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001080 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001081 EmitParmDecl(*Arg, V);
1082 break;
1083 }
Daniel Dunbar04d35782008-09-17 00:51:38 +00001084
1085 case ABIArgInfo::Expand: {
1086 // If this was structure was expand into multiple arguments then
1087 // we need to create a temporary and reconstruct it from the
1088 // arguments.
Chris Lattner6c5ec622008-11-24 04:00:27 +00001089 std::string Name = Arg->getNameAsString();
Daniel Dunbar04d35782008-09-17 00:51:38 +00001090 llvm::Value *Temp = CreateTempAlloca(ConvertType(Ty),
1091 (Name + ".addr").c_str());
1092 // FIXME: What are the right qualifiers here?
1093 llvm::Function::arg_iterator End =
1094 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI);
1095 EmitParmDecl(*Arg, Temp);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001096
Daniel Dunbar04d35782008-09-17 00:51:38 +00001097 // Name the arguments used in expansion and increment AI.
1098 unsigned Index = 0;
1099 for (; AI != End; ++AI, ++Index)
1100 AI->setName(Name + "." + llvm::utostr(Index));
1101 continue;
1102 }
Daniel Dunbar1358b202009-01-26 21:26:08 +00001103
1104 case ABIArgInfo::Ignore:
1105 break;
1106
Daniel Dunbar22e30052008-09-11 01:48:57 +00001107 case ABIArgInfo::Coerce:
Daniel Dunbar22e30052008-09-11 01:48:57 +00001108 case ABIArgInfo::StructRet:
1109 assert(0 && "Invalid ABI kind for non-return argument");
1110 }
Daniel Dunbar04d35782008-09-17 00:51:38 +00001111
1112 ++AI;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001113 }
1114 assert(AI == Fn->arg_end() && "Argument mismatch!");
1115}
1116
Daniel Dunbar708d8a82009-01-27 01:36:03 +00001117/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
1118/// a pointer to an object of type \arg Ty.
1119///
1120/// This safely handles the case when the src type is smaller than the
1121/// destination type; in this situation the values of bits which not
1122/// present in the src are undefined.
1123static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
1124 const llvm::Type *Ty,
1125 CodeGenFunction &CGF) {
1126 const llvm::Type *SrcTy =
1127 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
Daniel Dunbar1aa2be92009-01-30 00:47:38 +00001128 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
1129 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(Ty);
Daniel Dunbar708d8a82009-01-27 01:36:03 +00001130
1131 // If load is legal, just bitcase the src pointer.
1132 if (SrcSize == DstSize) {
1133 llvm::Value *Casted =
1134 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
1135 return CGF.Builder.CreateLoad(Casted);
1136 } else {
1137 assert(SrcSize < DstSize && "Coercion is losing source bits!");
1138
1139 // Otherwise do coercion through memory. This is stupid, but
1140 // simple.
1141 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
1142 llvm::Value *Casted =
1143 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
1144 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
1145 return CGF.Builder.CreateLoad(Tmp);
1146 }
1147}
1148
1149/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
1150/// where the source and destination may have different types.
1151///
1152/// This safely handles the case when the src type is larger than the
1153/// destination type; the upper bits of the src will be lost.
1154static void CreateCoercedStore(llvm::Value *Src,
1155 llvm::Value *DstPtr,
1156 CodeGenFunction &CGF) {
1157 const llvm::Type *SrcTy = Src->getType();
1158 const llvm::Type *DstTy =
1159 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
1160
Daniel Dunbar1aa2be92009-01-30 00:47:38 +00001161 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
1162 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(DstTy);
Daniel Dunbar708d8a82009-01-27 01:36:03 +00001163
1164 // If store is legal, just bitcase the src pointer.
1165 if (SrcSize == DstSize) {
1166 llvm::Value *Casted =
1167 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
1168 CGF.Builder.CreateStore(Src, Casted);
1169 } else {
1170 assert(SrcSize > DstSize && "Coercion is missing bits!");
1171
1172 // Otherwise do coercion through memory. This is stupid, but
1173 // simple.
1174 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
1175 CGF.Builder.CreateStore(Src, Tmp);
1176 llvm::Value *Casted =
1177 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
1178 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(Casted), DstPtr);
1179 }
1180}
1181
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001182void CodeGenFunction::EmitFunctionEpilog(QualType RetTy,
1183 llvm::Value *ReturnValue) {
Daniel Dunbare126ab12008-09-10 02:41:04 +00001184 llvm::Value *RV = 0;
1185
1186 // Functions with no result always return void.
1187 if (ReturnValue) {
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001188 ABIArgInfo RetAI = getABIReturnInfo(RetTy, CGM.getTypes());
Daniel Dunbare126ab12008-09-10 02:41:04 +00001189
1190 switch (RetAI.getKind()) {
1191 case ABIArgInfo::StructRet:
Daniel Dunbar17d35372008-12-18 04:52:14 +00001192 if (RetTy->isAnyComplexType()) {
1193 // FIXME: Volatile
1194 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
1195 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
1196 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1197 EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy);
1198 } else {
1199 Builder.CreateStore(Builder.CreateLoad(ReturnValue),
1200 CurFn->arg_begin());
1201 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001202 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001203
Daniel Dunbare126ab12008-09-10 02:41:04 +00001204 case ABIArgInfo::Default:
1205 RV = Builder.CreateLoad(ReturnValue);
1206 break;
1207
Daniel Dunbar1358b202009-01-26 21:26:08 +00001208 case ABIArgInfo::Ignore:
1209 break;
1210
Daniel Dunbar73d66602008-09-10 07:04:09 +00001211 case ABIArgInfo::Coerce: {
Daniel Dunbar708d8a82009-01-27 01:36:03 +00001212 RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001213 break;
Daniel Dunbar73d66602008-09-10 07:04:09 +00001214 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001215
1216 case ABIArgInfo::ByVal:
1217 case ABIArgInfo::Expand:
1218 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001219 }
1220 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001221
1222 if (RV) {
1223 Builder.CreateRet(RV);
1224 } else {
1225 Builder.CreateRetVoid();
1226 }
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001227}
1228
1229RValue CodeGenFunction::EmitCall(llvm::Value *Callee,
Daniel Dunbare126ab12008-09-10 02:41:04 +00001230 QualType RetTy,
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001231 const CallArgList &CallArgs) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001232 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001233
1234 // Handle struct-return functions by passing a pointer to the
1235 // location that we would like to return into.
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001236 ABIArgInfo RetAI = getABIReturnInfo(RetTy, CGM.getTypes());
Daniel Dunbare126ab12008-09-10 02:41:04 +00001237 switch (RetAI.getKind()) {
1238 case ABIArgInfo::StructRet:
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001239 // Create a temporary alloca to hold the result of the call. :(
Daniel Dunbar04d35782008-09-17 00:51:38 +00001240 Args.push_back(CreateTempAlloca(ConvertType(RetTy)));
Daniel Dunbare126ab12008-09-10 02:41:04 +00001241 break;
1242
1243 case ABIArgInfo::Default:
Daniel Dunbar1358b202009-01-26 21:26:08 +00001244 case ABIArgInfo::Ignore:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001245 case ABIArgInfo::Coerce:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001246 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001247
1248 case ABIArgInfo::ByVal:
1249 case ABIArgInfo::Expand:
1250 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001251 }
1252
1253 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
1254 I != E; ++I) {
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001255 ABIArgInfo ArgInfo = getABIArgumentInfo(I->second, CGM.getTypes());
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001256 RValue RV = I->first;
Daniel Dunbar04d35782008-09-17 00:51:38 +00001257
1258 switch (ArgInfo.getKind()) {
1259 case ABIArgInfo::ByVal: // Default is byval
1260 case ABIArgInfo::Default:
1261 if (RV.isScalar()) {
1262 Args.push_back(RV.getScalarVal());
1263 } else if (RV.isComplex()) {
1264 // Make a temporary alloca to pass the argument.
1265 Args.push_back(CreateTempAlloca(ConvertType(I->second)));
1266 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
1267 } else {
1268 Args.push_back(RV.getAggregateAddr());
1269 }
1270 break;
1271
Daniel Dunbar1358b202009-01-26 21:26:08 +00001272 case ABIArgInfo::Ignore:
1273 break;
1274
Daniel Dunbar04d35782008-09-17 00:51:38 +00001275 case ABIArgInfo::StructRet:
1276 case ABIArgInfo::Coerce:
1277 assert(0 && "Invalid ABI kind for non-return argument");
1278 break;
1279
1280 case ABIArgInfo::Expand:
1281 ExpandTypeToArgs(I->second, RV, Args);
1282 break;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001283 }
1284 }
1285
1286 llvm::CallInst *CI = Builder.CreateCall(Callee,&Args[0],&Args[0]+Args.size());
Daniel Dunbar1c8d1ba2009-01-31 02:54:56 +00001287 const llvm::Type *FnType =
1288 cast<llvm::PointerType>(Callee->getType())->getElementType();
1289 CGFunctionInfo CallInfo(RetTy, CallArgs,
1290 cast<llvm::FunctionType>(FnType)->isVarArg());
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001291
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001292 // FIXME: Provide TargetDecl so nounwind, noreturn, etc, etc get set.
Devang Patela85a9ef2008-09-25 21:02:23 +00001293 CodeGen::AttributeListType AttributeList;
Daniel Dunbarebbb8f32009-01-31 02:19:00 +00001294 CGM.ConstructAttributeList(0, CallInfo, AttributeList);
Devang Patela85a9ef2008-09-25 21:02:23 +00001295 CI->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(),
Daniel Dunbarebbb8f32009-01-31 02:19:00 +00001296 AttributeList.size()));
1297
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001298 if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee))
1299 CI->setCallingConv(F->getCallingConv());
1300 if (CI->getType() != llvm::Type::VoidTy)
1301 CI->setName("call");
Daniel Dunbare126ab12008-09-10 02:41:04 +00001302
1303 switch (RetAI.getKind()) {
1304 case ABIArgInfo::StructRet:
1305 if (RetTy->isAnyComplexType())
Daniel Dunbar04d35782008-09-17 00:51:38 +00001306 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Daniel Dunbar17d35372008-12-18 04:52:14 +00001307 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar04d35782008-09-17 00:51:38 +00001308 return RValue::getAggregate(Args[0]);
Daniel Dunbar17d35372008-12-18 04:52:14 +00001309 else
1310 return RValue::get(Builder.CreateLoad(Args[0]));
Daniel Dunbar22e30052008-09-11 01:48:57 +00001311
Daniel Dunbare126ab12008-09-10 02:41:04 +00001312 case ABIArgInfo::Default:
1313 return RValue::get(RetTy->isVoidType() ? 0 : CI);
1314
Daniel Dunbar1358b202009-01-26 21:26:08 +00001315 case ABIArgInfo::Ignore:
Daniel Dunbar5c9c7f02009-01-29 08:24:57 +00001316 if (RetTy->isVoidType())
1317 return RValue::get(0);
1318 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1319 llvm::Value *Res =
1320 llvm::UndefValue::get(llvm::PointerType::getUnqual(ConvertType(RetTy)));
1321 return RValue::getAggregate(Res);
1322 }
1323 return RValue::get(llvm::UndefValue::get(ConvertType(RetTy)));
Daniel Dunbar1358b202009-01-26 21:26:08 +00001324
Daniel Dunbar73d66602008-09-10 07:04:09 +00001325 case ABIArgInfo::Coerce: {
Daniel Dunbar708d8a82009-01-27 01:36:03 +00001326 llvm::Value *V = CreateTempAlloca(ConvertType(RetTy), "coerce");
1327 CreateCoercedStore(CI, V, *this);
Anders Carlssonfccf7472008-11-25 22:21:48 +00001328 if (RetTy->isAnyComplexType())
1329 return RValue::getComplex(LoadComplexFromAddr(V, false));
Daniel Dunbar1358b202009-01-26 21:26:08 +00001330 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssonfccf7472008-11-25 22:21:48 +00001331 return RValue::getAggregate(V);
Daniel Dunbar1358b202009-01-26 21:26:08 +00001332 else
1333 return RValue::get(Builder.CreateLoad(V));
Daniel Dunbar73d66602008-09-10 07:04:09 +00001334 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001335
1336 case ABIArgInfo::ByVal:
1337 case ABIArgInfo::Expand:
1338 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001339 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001340
1341 assert(0 && "Unhandled ABIArgInfo::Kind");
1342 return RValue::get(0);
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001343}