blob: 0807caff9beafe482e0d30db8a3ec0a80e35ce4b [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 Dunbar45c25ba2008-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 Dunbar0dbe2272008-09-08 21:33:45 +000049CGFunctionInfo::CGFunctionInfo(const FunctionDecl *FD)
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000050{
51 const FunctionType *FTy = FD->getType()->getAsFunctionType();
52 const FunctionTypeProto *FTP = dyn_cast<FunctionTypeProto>(FTy);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000053
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000054 ArgTypes.push_back(FTy->getResultType());
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000055 if (FTP) {
56 IsVariadic = FTP->isVariadic();
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000057 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
58 ArgTypes.push_back(FTP->getArgType(i));
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000059 } else {
60 IsVariadic = true;
61 }
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000062}
63
64CGFunctionInfo::CGFunctionInfo(const ObjCMethodDecl *MD,
65 const ASTContext &Context)
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000066 : IsVariadic(MD->isVariadic())
Daniel Dunbar0dbe2272008-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 Dunbar5323a4b2008-09-10 00:32:18 +000076ArgTypeIterator CGFunctionInfo::argtypes_begin() const {
77 return ArgTypes.begin();
78}
79
80ArgTypeIterator CGFunctionInfo::argtypes_end() const {
81 return ArgTypes.end();
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000082}
83
84/***/
85
Daniel Dunbar5323a4b2008-09-10 00:32:18 +000086CGCallInfo::CGCallInfo(QualType _ResultType, const CallArgList &_Args) {
87 ArgTypes.push_back(_ResultType);
88 for (CallArgList::const_iterator i = _Args.begin(), e = _Args.end(); i!=e; ++i)
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000089 ArgTypes.push_back(i->second);
90}
91
Daniel Dunbar5323a4b2008-09-10 00:32:18 +000092ArgTypeIterator CGCallInfo::argtypes_begin() const {
93 return ArgTypes.begin();
94}
95
96ArgTypeIterator CGCallInfo::argtypes_end() const {
97 return ArgTypes.end();
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000098}
Daniel Dunbar17b708d2008-09-09 23:27:19 +000099
100/***/
101
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000102/// ABIArgInfo - Helper class to encapsulate information about how a
103/// specific C type should be passed to or returned from a function.
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000104class ABIArgInfo {
105public:
106 enum Kind {
107 Default,
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +0000108 StructRet, /// Only valid for return values. The return value
109 /// should be passed through a pointer to a caller
110 /// allocated location passed as an implicit first
111 /// argument to the function.
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000112
Daniel Dunbar11434922009-01-26 21:26:08 +0000113 Ignore, /// Ignore the argument (treat as void). Useful for
114 /// void and empty structs.
115
Daniel Dunbar56273772008-09-17 00:51:38 +0000116 Coerce, /// Only valid for aggregate return types, the argument
117 /// should be accessed by coercion to a provided type.
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000118
119 ByVal, /// Only valid for aggregate argument types. The
120 /// structure should be passed "byval" with the
121 /// specified alignment (0 indicates default
122 /// alignment).
123
124 Expand, /// Only valid for aggregate argument types. The
125 /// structure should be expanded into consecutive
Daniel Dunbar56273772008-09-17 00:51:38 +0000126 /// arguments for its constituent fields. Currently
127 /// expand is only allowed on structures whose fields
128 /// are all scalar types or are themselves expandable
129 /// types.
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000130
131 KindFirst=Default, KindLast=Expand
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000132 };
133
134private:
135 Kind TheKind;
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000136 const llvm::Type *TypeData;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000137 unsigned UIntData;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000138
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000139 ABIArgInfo(Kind K, const llvm::Type *TD=0,
140 unsigned UI=0) : TheKind(K),
141 TypeData(TD),
142 UIntData(0) {}
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000143public:
144 static ABIArgInfo getDefault() {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000145 return ABIArgInfo(Default);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000146 }
147 static ABIArgInfo getStructRet() {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000148 return ABIArgInfo(StructRet);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000149 }
Daniel Dunbar11434922009-01-26 21:26:08 +0000150 static ABIArgInfo getIgnore() {
151 return ABIArgInfo(Ignore);
152 }
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000153 static ABIArgInfo getCoerce(const llvm::Type *T) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000154 return ABIArgInfo(Coerce, T);
155 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000156 static ABIArgInfo getByVal(unsigned Alignment) {
157 return ABIArgInfo(ByVal, 0, Alignment);
158 }
Daniel Dunbar56273772008-09-17 00:51:38 +0000159 static ABIArgInfo getExpand() {
160 return ABIArgInfo(Expand);
161 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000162
163 Kind getKind() const { return TheKind; }
164 bool isDefault() const { return TheKind == Default; }
165 bool isStructRet() const { return TheKind == StructRet; }
Daniel Dunbar11434922009-01-26 21:26:08 +0000166 bool isIgnore() const { return TheKind == Ignore; }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000167 bool isCoerce() const { return TheKind == Coerce; }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000168 bool isByVal() const { return TheKind == ByVal; }
Daniel Dunbar56273772008-09-17 00:51:38 +0000169 bool isExpand() const { return TheKind == Expand; }
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000170
171 // Coerce accessors
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000172 const llvm::Type *getCoerceToType() const {
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000173 assert(TheKind == Coerce && "Invalid kind!");
174 return TypeData;
175 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000176
177 // ByVal accessors
178 unsigned getByValAlignment() const {
179 assert(TheKind == ByVal && "Invalid kind!");
180 return UIntData;
181 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000182};
183
184/***/
185
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000186/* FIXME: All of this stuff should be part of the target interface
187 somehow. It is currently here because it is not clear how to factor
188 the targets to support this, since the Targets currently live in a
189 layer below types n'stuff.
190 */
191
192/// ABIInfo - Target specific hooks for defining how a type should be
193/// passed or returned from functions.
194class clang::ABIInfo {
195public:
196 virtual ~ABIInfo();
197
198 virtual ABIArgInfo classifyReturnType(QualType RetTy,
199 ASTContext &Context) const = 0;
200
201 virtual ABIArgInfo classifyArgumentType(QualType Ty,
202 ASTContext &Context) const = 0;
203};
204
205ABIInfo::~ABIInfo() {}
206
Daniel Dunbar834af452008-09-17 21:22:33 +0000207/// isEmptyStruct - Return true iff a structure has no non-empty
208/// members. Note that a structure with a flexible array member is not
209/// considered empty.
210static bool isEmptyStruct(QualType T) {
211 const RecordType *RT = T->getAsStructureType();
212 if (!RT)
213 return 0;
214 const RecordDecl *RD = RT->getDecl();
215 if (RD->hasFlexibleArrayMember())
216 return false;
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000217 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar834af452008-09-17 21:22:33 +0000218 e = RD->field_end(); i != e; ++i) {
219 const FieldDecl *FD = *i;
220 if (!isEmptyStruct(FD->getType()))
221 return false;
222 }
223 return true;
224}
225
226/// isSingleElementStruct - Determine if a structure is a "single
227/// element struct", i.e. it has exactly one non-empty field or
228/// exactly one field which is itself a single element
229/// struct. Structures with flexible array members are never
230/// considered single element structs.
231///
232/// \return The field declaration for the single non-empty field, if
233/// it exists.
234static const FieldDecl *isSingleElementStruct(QualType T) {
235 const RecordType *RT = T->getAsStructureType();
236 if (!RT)
237 return 0;
238
239 const RecordDecl *RD = RT->getDecl();
240 if (RD->hasFlexibleArrayMember())
241 return 0;
242
243 const FieldDecl *Found = 0;
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000244 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar834af452008-09-17 21:22:33 +0000245 e = RD->field_end(); i != e; ++i) {
246 const FieldDecl *FD = *i;
247 QualType FT = FD->getType();
248
249 if (isEmptyStruct(FT)) {
250 // Ignore
251 } else if (Found) {
252 return 0;
253 } else if (!CodeGenFunction::hasAggregateLLVMType(FT)) {
254 Found = FD;
255 } else {
256 Found = isSingleElementStruct(FT);
257 if (!Found)
258 return 0;
259 }
260 }
261
262 return Found;
263}
264
265static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
266 if (!Ty->getAsBuiltinType() && !Ty->isPointerType())
267 return false;
268
269 uint64_t Size = Context.getTypeSize(Ty);
270 return Size == 32 || Size == 64;
271}
272
273static bool areAllFields32Or64BitBasicType(const RecordDecl *RD,
274 ASTContext &Context) {
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000275 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar834af452008-09-17 21:22:33 +0000276 e = RD->field_end(); i != e; ++i) {
277 const FieldDecl *FD = *i;
278
279 if (!is32Or64BitBasicType(FD->getType(), Context))
280 return false;
281
282 // If this is a bit-field we need to make sure it is still a
283 // 32-bit or 64-bit type.
284 if (Expr *BW = FD->getBitWidth()) {
285 unsigned Width = BW->getIntegerConstantExprValue(Context).getZExtValue();
286 if (Width <= 16)
287 return false;
288 }
289 }
290 return true;
291}
292
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000293namespace {
294/// DefaultABIInfo - The default implementation for ABI specific
295/// details. This implementation provides information which results in
296/// sensible LLVM IR generation, but does not conform to any
297/// particular ABI.
298class DefaultABIInfo : public ABIInfo {
299 virtual ABIArgInfo classifyReturnType(QualType RetTy,
300 ASTContext &Context) const;
301
302 virtual ABIArgInfo classifyArgumentType(QualType RetTy,
303 ASTContext &Context) const;
304};
305
306/// X86_32ABIInfo - The X86-32 ABI information.
307class X86_32ABIInfo : public ABIInfo {
308public:
309 virtual ABIArgInfo classifyReturnType(QualType RetTy,
310 ASTContext &Context) const;
311
312 virtual ABIArgInfo classifyArgumentType(QualType RetTy,
313 ASTContext &Context) const;
314};
315}
316
317ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
318 ASTContext &Context) const {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000319 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar834af452008-09-17 21:22:33 +0000320 // Classify "single element" structs as their element type.
321 const FieldDecl *SeltFD = isSingleElementStruct(RetTy);
322 if (SeltFD) {
323 QualType SeltTy = SeltFD->getType()->getDesugaredType();
324 if (const BuiltinType *BT = SeltTy->getAsBuiltinType()) {
325 // FIXME: This is gross, it would be nice if we could just
326 // pass back SeltTy and have clients deal with it. Is it worth
327 // supporting coerce to both LLVM and clang Types?
328 if (BT->isIntegerType()) {
329 uint64_t Size = Context.getTypeSize(SeltTy);
330 return ABIArgInfo::getCoerce(llvm::IntegerType::get((unsigned) Size));
331 } else if (BT->getKind() == BuiltinType::Float) {
332 return ABIArgInfo::getCoerce(llvm::Type::FloatTy);
333 } else if (BT->getKind() == BuiltinType::Double) {
334 return ABIArgInfo::getCoerce(llvm::Type::DoubleTy);
335 }
336 } else if (SeltTy->isPointerType()) {
337 // FIXME: It would be really nice if this could come out as
338 // the proper pointer type.
339 llvm::Type *PtrTy =
340 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
341 return ABIArgInfo::getCoerce(PtrTy);
342 }
343 }
344
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000345 uint64_t Size = Context.getTypeSize(RetTy);
346 if (Size == 8) {
347 return ABIArgInfo::getCoerce(llvm::Type::Int8Ty);
348 } else if (Size == 16) {
349 return ABIArgInfo::getCoerce(llvm::Type::Int16Ty);
350 } else if (Size == 32) {
351 return ABIArgInfo::getCoerce(llvm::Type::Int32Ty);
352 } else if (Size == 64) {
353 return ABIArgInfo::getCoerce(llvm::Type::Int64Ty);
354 } else {
355 return ABIArgInfo::getStructRet();
356 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000357 } else {
358 return ABIArgInfo::getDefault();
359 }
360}
361
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000362ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
363 ASTContext &Context) const {
Daniel Dunbarf0357382008-09-17 20:11:04 +0000364 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar834af452008-09-17 21:22:33 +0000365 // Structures with flexible arrays are always byval.
366 if (const RecordType *RT = Ty->getAsStructureType())
367 if (RT->getDecl()->hasFlexibleArrayMember())
368 return ABIArgInfo::getByVal(0);
369
370 // Expand empty structs (i.e. ignore)
371 uint64_t Size = Context.getTypeSize(Ty);
372 if (Ty->isStructureType() && Size == 0)
373 return ABIArgInfo::getExpand();
374
375 // Expand structs with size <= 128-bits which consist only of
376 // basic types (int, long long, float, double, xxx*). This is
377 // non-recursive and does not ignore empty fields.
378 if (const RecordType *RT = Ty->getAsStructureType()) {
379 if (Context.getTypeSize(Ty) <= 4*32 &&
380 areAllFields32Or64BitBasicType(RT->getDecl(), Context))
381 return ABIArgInfo::getExpand();
382 }
383
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000384 return ABIArgInfo::getByVal(0);
385 } else {
386 return ABIArgInfo::getDefault();
387 }
388}
389
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000390namespace {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000391/// X86_64ABIInfo - The X86_64 ABI information.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000392class X86_64ABIInfo : public ABIInfo {
393 enum Class {
394 Integer = 0,
395 SSE,
396 SSEUp,
397 X87,
398 X87Up,
399 ComplexX87,
400 NoClass,
401 Memory
402 };
403
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000404 /// merge - Implement the X86_64 ABI merging algorithm.
405 ///
Daniel Dunbarc4503572009-01-31 00:06:58 +0000406 /// Merge an accumulating classification \arg Accum with a field
407 /// classification \arg Field.
408 ///
409 /// \param Accum - The accumulating classification. This should
410 /// always be either NoClass or the result of a previous merge
411 /// call. In addition, this should never be Memory (the caller
412 /// should just return Memory for the aggregate).
413 Class merge(Class Accum, Class Field) const;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000414
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000415 /// classify - Determine the x86_64 register classes in which the
416 /// given type T should be passed.
417 ///
Daniel Dunbarc4503572009-01-31 00:06:58 +0000418 /// \param Lo - The classification for the parts of the type
419 /// residing in the low word of the containing object.
420 ///
421 /// \param Hi - The classification for the parts of the type
422 /// residing in the high word of the containing object.
423 ///
424 /// \param OffsetBase - The bit offset of this type in the
Daniel Dunbarcdf920e2009-01-30 22:40:15 +0000425 /// containing object. Some parameters are classified different
426 /// depending on whether they straddle an eightbyte boundary.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000427 ///
428 /// If a word is unused its result will be NoClass; if a type should
429 /// be passed in Memory then at least the classification of \arg Lo
430 /// will be Memory.
431 ///
432 /// The \arg Lo class will be NoClass iff the argument is ignored.
433 ///
434 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
435 /// be NoClass.
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000436 void classify(QualType T, ASTContext &Context, uint64_t OffsetBase,
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000437 Class &Lo, Class &Hi) const;
Daniel Dunbarc4503572009-01-31 00:06:58 +0000438
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000439public:
440 virtual ABIArgInfo classifyReturnType(QualType RetTy,
441 ASTContext &Context) const;
442
443 virtual ABIArgInfo classifyArgumentType(QualType RetTy,
444 ASTContext &Context) const;
445};
446}
447
Daniel Dunbarc4503572009-01-31 00:06:58 +0000448X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum,
449 Class Field) const {
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000450 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
451 // classified recursively so that always two fields are
452 // considered. The resulting class is calculated according to
453 // the classes of the fields in the eightbyte:
454 //
455 // (a) If both classes are equal, this is the resulting class.
456 //
457 // (b) If one of the classes is NO_CLASS, the resulting class is
458 // the other class.
459 //
460 // (c) If one of the classes is MEMORY, the result is the MEMORY
461 // class.
462 //
463 // (d) If one of the classes is INTEGER, the result is the
464 // INTEGER.
465 //
466 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
467 // MEMORY is used as class.
468 //
469 // (f) Otherwise class SSE is used.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000470 assert((Accum == NoClass || Accum == Integer ||
471 Accum == SSE || Accum == SSEUp) &&
472 "Invalid accumulated classification during merge.");
473 if (Accum == Field || Field == NoClass)
474 return Accum;
475 else if (Field == Memory)
476 return Memory;
477 else if (Accum == NoClass)
478 return Field;
479 else if (Accum == Integer || Field == Integer)
480 return Integer;
481 else if (Field == X87 || Field == X87Up || Field == ComplexX87)
482 return Memory;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000483 else
Daniel Dunbarc4503572009-01-31 00:06:58 +0000484 return SSE;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000485}
486
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000487void X86_64ABIInfo::classify(QualType Ty,
488 ASTContext &Context,
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000489 uint64_t OffsetBase,
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000490 Class &Lo, Class &Hi) const {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000491 Lo = Hi = NoClass;
492
493 Class &Current = OffsetBase < 64 ? Lo : Hi;
494 Current = Memory;
495
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000496 if (const BuiltinType *BT = Ty->getAsBuiltinType()) {
497 BuiltinType::Kind k = BT->getKind();
498
Daniel Dunbar11434922009-01-26 21:26:08 +0000499 if (k == BuiltinType::Void) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000500 Current = NoClass;
Daniel Dunbar11434922009-01-26 21:26:08 +0000501 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000502 Current = Integer;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000503 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000504 Current = SSE;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000505 } else if (k == BuiltinType::LongDouble) {
506 Lo = X87;
507 Hi = X87Up;
508 }
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000509 // FIXME: _Decimal32 and _Decimal64 are SSE.
510 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000511 // FIXME: __int128 is (Integer, Integer).
512 } else if (Ty->isPointerLikeType() || Ty->isBlockPointerType() ||
513 Ty->isObjCQualifiedInterfaceType()) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000514 Current = Integer;
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000515 } else if (const VectorType *VT = Ty->getAsVectorType()) {
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000516 uint64_t Size = Context.getTypeSize(VT);
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000517 if (Size == 64) {
Daniel Dunbard4cd1b02009-01-30 19:38:39 +0000518 // gcc passes <1 x double> in memory.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000519 if (VT->getElementType() == Context.DoubleTy)
Daniel Dunbard4cd1b02009-01-30 19:38:39 +0000520 return;
Daniel Dunbard4cd1b02009-01-30 19:38:39 +0000521
Daniel Dunbarc4503572009-01-31 00:06:58 +0000522 Current = SSE;
Daniel Dunbare33edf12009-01-30 18:40:10 +0000523
524 // If this type crosses an eightbyte boundary, it should be
525 // split.
Daniel Dunbarcdf920e2009-01-30 22:40:15 +0000526 if (OffsetBase && OffsetBase != 64)
Daniel Dunbare33edf12009-01-30 18:40:10 +0000527 Hi = Lo;
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000528 } else if (Size == 128) {
529 Lo = SSE;
530 Hi = SSEUp;
531 }
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000532 } else if (const ComplexType *CT = Ty->getAsComplexType()) {
533 QualType ET = CT->getElementType();
534
Daniel Dunbare33edf12009-01-30 18:40:10 +0000535 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbareac48dc2009-01-29 07:22:20 +0000536 if (ET->isIntegerType()) {
Daniel Dunbareac48dc2009-01-29 07:22:20 +0000537 if (Size <= 64)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000538 Current = Integer;
Daniel Dunbareac48dc2009-01-29 07:22:20 +0000539 else if (Size <= 128)
540 Lo = Hi = Integer;
541 } else if (ET == Context.FloatTy)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000542 Current = SSE;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000543 else if (ET == Context.DoubleTy)
544 Lo = Hi = SSE;
545 else if (ET == Context.LongDoubleTy)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000546 Current = ComplexX87;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000547
548 // If this complex type crosses an eightbyte boundary then it
549 // should be split.
Daniel Dunbarcdf920e2009-01-30 22:40:15 +0000550 uint64_t EB_Real = (OffsetBase) / 64;
551 uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000552 if (Hi == NoClass && EB_Real != EB_Imag)
553 Hi = Lo;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000554 } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
555 // Arrays are treated like structures.
556
557 uint64_t Size = Context.getTypeSize(Ty);
558
559 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
560 // than two eightbytes, ..., it has class MEMORY.
561 if (Size > 128)
562 return;
563
564 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
565 // fields, it has class MEMORY.
566 //
567 // Only need to check alignment of array base.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000568 if (OffsetBase % Context.getTypeAlign(AT->getElementType()))
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000569 return;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000570
571 // Otherwise implement simplified merge. We could be smarter about
572 // this, but it isn't worth it and would be harder to verify.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000573 Current = NoClass;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000574 uint64_t EltSize = Context.getTypeSize(AT->getElementType());
575 uint64_t ArraySize = AT->getSize().getZExtValue();
576 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
577 Class FieldLo, FieldHi;
578 classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbarc4503572009-01-31 00:06:58 +0000579 Lo = merge(Lo, FieldLo);
580 Hi = merge(Hi, FieldHi);
581 if (Lo == Memory || Hi == Memory)
582 break;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000583 }
Daniel Dunbarc4503572009-01-31 00:06:58 +0000584
585 // Do post merger cleanup (see below). Only case we worry about is Memory.
586 if (Hi == Memory)
587 Lo = Memory;
588 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Daniel Dunbar99037e52009-01-29 08:13:58 +0000589 } else if (const RecordType *RT = Ty->getAsRecordType()) {
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000590 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbar99037e52009-01-29 08:13:58 +0000591
592 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
593 // than two eightbytes, ..., it has class MEMORY.
594 if (Size > 128)
595 return;
596
597 const RecordDecl *RD = RT->getDecl();
598
599 // Assume variable sized types are passed in memory.
600 if (RD->hasFlexibleArrayMember())
601 return;
602
603 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
604
605 // Reset Lo class, this will be recomputed.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000606 Current = NoClass;
Daniel Dunbar99037e52009-01-29 08:13:58 +0000607 unsigned idx = 0;
608 for (RecordDecl::field_iterator i = RD->field_begin(),
609 e = RD->field_end(); i != e; ++i, ++idx) {
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000610 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Daniel Dunbar99037e52009-01-29 08:13:58 +0000611
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000612 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
613 // fields, it has class MEMORY.
Daniel Dunbar99037e52009-01-29 08:13:58 +0000614 if (Offset % Context.getTypeAlign(i->getType())) {
615 Lo = Memory;
616 return;
617 }
618
Daniel Dunbar99037e52009-01-29 08:13:58 +0000619 // Classify this field.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000620 //
621 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
622 // exceeds a single eightbyte, each is classified
623 // separately. Each eightbyte gets initialized to class
624 // NO_CLASS.
Daniel Dunbar99037e52009-01-29 08:13:58 +0000625 Class FieldLo, FieldHi;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000626 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbarc4503572009-01-31 00:06:58 +0000627 Lo = merge(Lo, FieldLo);
628 Hi = merge(Hi, FieldHi);
629 if (Lo == Memory || Hi == Memory)
630 break;
Daniel Dunbar99037e52009-01-29 08:13:58 +0000631 }
632
633 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
634 //
635 // (a) If one of the classes is MEMORY, the whole argument is
636 // passed in memory.
637 //
638 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
639
640 // The first of these conditions is guaranteed by how we implement
Daniel Dunbarc4503572009-01-31 00:06:58 +0000641 // the merge (just bail).
642 //
643 // The second condition occurs in the case of unions; for example
644 // union { _Complex double; unsigned; }.
645 if (Hi == Memory)
646 Lo = Memory;
Daniel Dunbar99037e52009-01-29 08:13:58 +0000647 if (Hi == SSEUp && Lo != SSE)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000648 Hi = SSE;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000649 }
650}
651
Daniel Dunbarc4503572009-01-31 00:06:58 +0000652
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000653ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
654 ASTContext &Context) const {
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000655 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
656 // classification algorithm.
657 X86_64ABIInfo::Class Lo, Hi;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000658 classify(RetTy, Context, 0, Lo, Hi);
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000659
Daniel Dunbarc4503572009-01-31 00:06:58 +0000660 // Check some invariants.
661 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
662 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
663 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
664
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000665 const llvm::Type *ResType = 0;
666 switch (Lo) {
667 case NoClass:
Daniel Dunbar11434922009-01-26 21:26:08 +0000668 return ABIArgInfo::getIgnore();
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000669
670 case SSEUp:
671 case X87Up:
672 assert(0 && "Invalid classification for lo word.");
673
Daniel Dunbarc4503572009-01-31 00:06:58 +0000674 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
675 // hidden argument, i.e. structret.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000676 case Memory:
677 return ABIArgInfo::getStructRet();
678
679 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
680 // available register of the sequence %rax, %rdx is used.
681 case Integer:
682 ResType = llvm::Type::Int64Ty; break;
683
684 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
685 // available SSE register of the sequence %xmm0, %xmm1 is used.
686 case SSE:
687 ResType = llvm::Type::DoubleTy; break;
688
689 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
690 // returned on the X87 stack in %st0 as 80-bit x87 number.
691 case X87:
692 ResType = llvm::Type::X86_FP80Ty; break;
693
Daniel Dunbarc4503572009-01-31 00:06:58 +0000694 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
695 // part of the value is returned in %st0 and the imaginary part in
696 // %st1.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000697 case ComplexX87:
698 assert(Hi == NoClass && "Unexpected ComplexX87 classification.");
699 ResType = llvm::VectorType::get(llvm::Type::X86_FP80Ty, 2);
700 break;
701 }
702
703 switch (Hi) {
704 // Memory was handled previously, and ComplexX87 and X87 should
705 // never occur as hi classes.
706 case Memory:
707 case X87:
708 case ComplexX87:
709 assert(0 && "Invalid classification for hi word.");
710
711 case NoClass: break;
712 case Integer:
Daniel Dunbarb0e14f22009-01-29 07:36:07 +0000713 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
714 break;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000715 case SSE:
Daniel Dunbarb0e14f22009-01-29 07:36:07 +0000716 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
717 break;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000718
719 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
720 // is passed in the upper half of the last used SSE register.
721 //
722 // SSEUP should always be preceeded by SSE, just widen.
723 case SSEUp:
724 assert(Lo == SSE && "Unexpected SSEUp classification.");
725 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
726 break;
727
728 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
Daniel Dunbarb0e14f22009-01-29 07:36:07 +0000729 // returned together with the previous X87 value in %st0.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000730 //
731 // X87UP should always be preceeded by X87, so we don't need to do
732 // anything here.
733 case X87Up:
734 assert(Lo == X87 && "Unexpected X87Up classification.");
735 break;
736 }
737
738 return ABIArgInfo::getCoerce(ResType);
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000739}
740
741ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty,
742 ASTContext &Context) const {
743 return ABIArgInfo::getDefault();
744}
745
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000746ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
747 ASTContext &Context) const {
748 return ABIArgInfo::getDefault();
749}
750
751ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
752 ASTContext &Context) const {
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000753 return ABIArgInfo::getDefault();
754}
755
756const ABIInfo &CodeGenTypes::getABIInfo() const {
757 if (TheABIInfo)
758 return *TheABIInfo;
759
760 // For now we just cache this in the CodeGenTypes and don't bother
761 // to free it.
762 const char *TargetPrefix = getContext().Target.getTargetPrefix();
763 if (strcmp(TargetPrefix, "x86") == 0) {
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000764 switch (getContext().Target.getPointerWidth(0)) {
765 case 32:
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000766 return *(TheABIInfo = new X86_32ABIInfo());
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000767 case 64:
Daniel Dunbar11a76ed2009-01-30 18:47:53 +0000768 return *(TheABIInfo = new X86_64ABIInfo());
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000769 }
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000770 }
771
772 return *(TheABIInfo = new DefaultABIInfo);
773}
774
775// getABIReturnInfo - Wrap the ABIInfo getABIReturnInfo, altering
776// "default" types to StructRet when appropriate for simplicity.
777static ABIArgInfo getABIReturnInfo(QualType Ty, CodeGenTypes &CGT) {
778 assert(!Ty->isArrayType() &&
779 "Array types cannot be passed directly.");
780 ABIArgInfo Info = CGT.getABIInfo().classifyReturnType(Ty, CGT.getContext());
Daniel Dunbarf0357382008-09-17 20:11:04 +0000781 // Ensure default on aggregate types is StructRet.
782 if (Info.isDefault() && CodeGenFunction::hasAggregateLLVMType(Ty))
783 return ABIArgInfo::getStructRet();
784 return Info;
785}
786
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000787// getABIArgumentInfo - Wrap the ABIInfo getABIReturnInfo, altering
788// "default" types to ByVal when appropriate for simplicity.
789static ABIArgInfo getABIArgumentInfo(QualType Ty, CodeGenTypes &CGT) {
790 assert(!Ty->isArrayType() &&
791 "Array types cannot be passed directly.");
792 ABIArgInfo Info = CGT.getABIInfo().classifyArgumentType(Ty, CGT.getContext());
Daniel Dunbarf0357382008-09-17 20:11:04 +0000793 // Ensure default on aggregate types is ByVal.
794 if (Info.isDefault() && CodeGenFunction::hasAggregateLLVMType(Ty))
795 return ABIArgInfo::getByVal(0);
796 return Info;
797}
798
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000799/***/
800
Daniel Dunbar56273772008-09-17 00:51:38 +0000801void CodeGenTypes::GetExpandedTypes(QualType Ty,
802 std::vector<const llvm::Type*> &ArgTys) {
803 const RecordType *RT = Ty->getAsStructureType();
804 assert(RT && "Can only expand structure types.");
805 const RecordDecl *RD = RT->getDecl();
806 assert(!RD->hasFlexibleArrayMember() &&
807 "Cannot expand structure with flexible array.");
808
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000809 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar56273772008-09-17 00:51:38 +0000810 e = RD->field_end(); i != e; ++i) {
811 const FieldDecl *FD = *i;
812 assert(!FD->isBitField() &&
813 "Cannot expand structure with bit-field members.");
814
815 QualType FT = FD->getType();
816 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
817 GetExpandedTypes(FT, ArgTys);
818 } else {
819 ArgTys.push_back(ConvertType(FT));
820 }
821 }
822}
823
824llvm::Function::arg_iterator
825CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
826 llvm::Function::arg_iterator AI) {
827 const RecordType *RT = Ty->getAsStructureType();
828 assert(RT && "Can only expand structure types.");
829
830 RecordDecl *RD = RT->getDecl();
831 assert(LV.isSimple() &&
832 "Unexpected non-simple lvalue during struct expansion.");
833 llvm::Value *Addr = LV.getAddress();
834 for (RecordDecl::field_iterator i = RD->field_begin(),
835 e = RD->field_end(); i != e; ++i) {
836 FieldDecl *FD = *i;
837 QualType FT = FD->getType();
838
839 // FIXME: What are the right qualifiers here?
840 LValue LV = EmitLValueForField(Addr, FD, false, 0);
841 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
842 AI = ExpandTypeFromArgs(FT, LV, AI);
843 } else {
844 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
845 ++AI;
846 }
847 }
848
849 return AI;
850}
851
852void
853CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
854 llvm::SmallVector<llvm::Value*, 16> &Args) {
855 const RecordType *RT = Ty->getAsStructureType();
856 assert(RT && "Can only expand structure types.");
857
858 RecordDecl *RD = RT->getDecl();
859 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
860 llvm::Value *Addr = RV.getAggregateAddr();
861 for (RecordDecl::field_iterator i = RD->field_begin(),
862 e = RD->field_end(); i != e; ++i) {
863 FieldDecl *FD = *i;
864 QualType FT = FD->getType();
865
866 // FIXME: What are the right qualifiers here?
867 LValue LV = EmitLValueForField(Addr, FD, false, 0);
868 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
869 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
870 } else {
871 RValue RV = EmitLoadOfLValue(LV, FT);
872 assert(RV.isScalar() &&
873 "Unexpected non-scalar rvalue during struct expansion.");
874 Args.push_back(RV.getScalarVal());
875 }
876 }
877}
878
879/***/
880
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000881const llvm::FunctionType *
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000882CodeGenTypes::GetFunctionType(const CGCallInfo &CI, bool IsVariadic) {
883 return GetFunctionType(CI.argtypes_begin(), CI.argtypes_end(), IsVariadic);
884}
885
886const llvm::FunctionType *
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000887CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI) {
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000888 return GetFunctionType(FI.argtypes_begin(), FI.argtypes_end(), FI.isVariadic());
889}
890
891const llvm::FunctionType *
892CodeGenTypes::GetFunctionType(ArgTypeIterator begin, ArgTypeIterator end,
893 bool IsVariadic) {
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000894 std::vector<const llvm::Type*> ArgTys;
895
896 const llvm::Type *ResultType = 0;
897
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000898 QualType RetTy = *begin;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000899 ABIArgInfo RetAI = getABIReturnInfo(RetTy, *this);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000900 switch (RetAI.getKind()) {
901 case ABIArgInfo::ByVal:
902 case ABIArgInfo::Expand:
903 assert(0 && "Invalid ABI kind for return argument");
904
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000905 case ABIArgInfo::Default:
906 if (RetTy->isVoidType()) {
907 ResultType = llvm::Type::VoidTy;
908 } else {
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000909 ResultType = ConvertType(RetTy);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000910 }
911 break;
912
913 case ABIArgInfo::StructRet: {
914 ResultType = llvm::Type::VoidTy;
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000915 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000916 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
917 break;
918 }
919
Daniel Dunbar11434922009-01-26 21:26:08 +0000920 case ABIArgInfo::Ignore:
921 ResultType = llvm::Type::VoidTy;
922 break;
923
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000924 case ABIArgInfo::Coerce:
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000925 ResultType = RetAI.getCoerceToType();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000926 break;
927 }
928
929 for (++begin; begin != end; ++begin) {
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000930 ABIArgInfo AI = getABIArgumentInfo(*begin, *this);
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000931 const llvm::Type *Ty = ConvertType(*begin);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000932
933 switch (AI.getKind()) {
Daniel Dunbar11434922009-01-26 21:26:08 +0000934 case ABIArgInfo::Ignore:
935 break;
936
Daniel Dunbar56273772008-09-17 00:51:38 +0000937 case ABIArgInfo::Coerce:
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000938 case ABIArgInfo::StructRet:
939 assert(0 && "Invalid ABI kind for non-return argument");
940
941 case ABIArgInfo::ByVal:
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000942 // byval arguments are always on the stack, which is addr space #0.
943 ArgTys.push_back(llvm::PointerType::getUnqual(Ty));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000944 assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled");
945 break;
946
947 case ABIArgInfo::Default:
948 ArgTys.push_back(Ty);
949 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000950
951 case ABIArgInfo::Expand:
Daniel Dunbar56273772008-09-17 00:51:38 +0000952 GetExpandedTypes(*begin, ArgTys);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000953 break;
954 }
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000955 }
956
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000957 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000958}
959
Daniel Dunbarb7688072008-09-10 00:41:16 +0000960bool CodeGenModule::ReturnTypeUsesSret(QualType RetTy) {
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000961 return getABIReturnInfo(RetTy, getTypes()).isStructRet();
Daniel Dunbar3913f182008-09-09 23:48:28 +0000962}
963
Devang Patel761d7f72008-09-25 21:02:23 +0000964void CodeGenModule::ConstructAttributeList(const Decl *TargetDecl,
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000965 ArgTypeIterator begin,
966 ArgTypeIterator end,
Devang Patel761d7f72008-09-25 21:02:23 +0000967 AttributeListType &PAL) {
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000968 unsigned FuncAttrs = 0;
Devang Patela2c69122008-09-26 22:53:57 +0000969 unsigned RetAttrs = 0;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000970
971 if (TargetDecl) {
972 if (TargetDecl->getAttr<NoThrowAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +0000973 FuncAttrs |= llvm::Attribute::NoUnwind;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000974 if (TargetDecl->getAttr<NoReturnAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +0000975 FuncAttrs |= llvm::Attribute::NoReturn;
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000976 if (TargetDecl->getAttr<PureAttr>())
977 FuncAttrs |= llvm::Attribute::ReadOnly;
978 if (TargetDecl->getAttr<ConstAttr>())
979 FuncAttrs |= llvm::Attribute::ReadNone;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000980 }
981
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000982 QualType RetTy = *begin;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000983 unsigned Index = 1;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000984 ABIArgInfo RetAI = getABIReturnInfo(RetTy, getTypes());
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000985 switch (RetAI.getKind()) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000986 case ABIArgInfo::Default:
987 if (RetTy->isPromotableIntegerType()) {
988 if (RetTy->isSignedIntegerType()) {
Devang Patela2c69122008-09-26 22:53:57 +0000989 RetAttrs |= llvm::Attribute::SExt;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000990 } else if (RetTy->isUnsignedIntegerType()) {
Devang Patela2c69122008-09-26 22:53:57 +0000991 RetAttrs |= llvm::Attribute::ZExt;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000992 }
993 }
994 break;
995
996 case ABIArgInfo::StructRet:
Devang Patel761d7f72008-09-25 21:02:23 +0000997 PAL.push_back(llvm::AttributeWithIndex::get(Index,
998 llvm::Attribute::StructRet|
999 llvm::Attribute::NoAlias));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001000 ++Index;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001001 break;
1002
Daniel Dunbar11434922009-01-26 21:26:08 +00001003 case ABIArgInfo::Ignore:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001004 case ABIArgInfo::Coerce:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001005 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001006
1007 case ABIArgInfo::ByVal:
1008 case ABIArgInfo::Expand:
1009 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001010 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001011
Devang Patela2c69122008-09-26 22:53:57 +00001012 if (RetAttrs)
1013 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Daniel Dunbar56273772008-09-17 00:51:38 +00001014 for (++begin; begin != end; ++begin) {
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001015 QualType ParamType = *begin;
Devang Patel761d7f72008-09-25 21:02:23 +00001016 unsigned Attributes = 0;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001017 ABIArgInfo AI = getABIArgumentInfo(ParamType, getTypes());
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001018
1019 switch (AI.getKind()) {
1020 case ABIArgInfo::StructRet:
Daniel Dunbar56273772008-09-17 00:51:38 +00001021 case ABIArgInfo::Coerce:
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001022 assert(0 && "Invalid ABI kind for non-return argument");
1023
1024 case ABIArgInfo::ByVal:
Devang Patel761d7f72008-09-25 21:02:23 +00001025 Attributes |= llvm::Attribute::ByVal;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001026 assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled");
1027 break;
1028
1029 case ABIArgInfo::Default:
1030 if (ParamType->isPromotableIntegerType()) {
1031 if (ParamType->isSignedIntegerType()) {
Devang Patel761d7f72008-09-25 21:02:23 +00001032 Attributes |= llvm::Attribute::SExt;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001033 } else if (ParamType->isUnsignedIntegerType()) {
Devang Patel761d7f72008-09-25 21:02:23 +00001034 Attributes |= llvm::Attribute::ZExt;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001035 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001036 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001037 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001038
Daniel Dunbar11434922009-01-26 21:26:08 +00001039 case ABIArgInfo::Ignore:
1040 // Skip increment, no matching LLVM parameter.
1041 continue;
1042
Daniel Dunbar56273772008-09-17 00:51:38 +00001043 case ABIArgInfo::Expand: {
1044 std::vector<const llvm::Type*> Tys;
1045 // FIXME: This is rather inefficient. Do we ever actually need
1046 // to do anything here? The result should be just reconstructed
1047 // on the other side, so extension should be a non-issue.
1048 getTypes().GetExpandedTypes(ParamType, Tys);
1049 Index += Tys.size();
1050 continue;
1051 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001052 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001053
Devang Patel761d7f72008-09-25 21:02:23 +00001054 if (Attributes)
1055 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar56273772008-09-17 00:51:38 +00001056 ++Index;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001057 }
Devang Patela2c69122008-09-26 22:53:57 +00001058 if (FuncAttrs)
1059 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
1060
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001061}
1062
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001063void CodeGenFunction::EmitFunctionProlog(llvm::Function *Fn,
1064 QualType RetTy,
1065 const FunctionArgList &Args) {
1066 // Emit allocs for param decls. Give the LLVM Argument nodes names.
1067 llvm::Function::arg_iterator AI = Fn->arg_begin();
1068
1069 // Name the struct return argument.
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001070 if (CGM.ReturnTypeUsesSret(RetTy)) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001071 AI->setName("agg.result");
1072 ++AI;
1073 }
1074
1075 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbar56273772008-09-17 00:51:38 +00001076 i != e; ++i) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001077 const VarDecl *Arg = i->first;
Daniel Dunbar56273772008-09-17 00:51:38 +00001078 QualType Ty = i->second;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001079 ABIArgInfo ArgI = getABIArgumentInfo(Ty, CGM.getTypes());
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001080
1081 switch (ArgI.getKind()) {
1082 case ABIArgInfo::ByVal:
1083 case ABIArgInfo::Default: {
1084 assert(AI != Fn->arg_end() && "Argument mismatch!");
1085 llvm::Value* V = AI;
Daniel Dunbar56273772008-09-17 00:51:38 +00001086 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001087 // This must be a promotion, for something like
1088 // "void a(x) short x; {..."
Daniel Dunbar56273772008-09-17 00:51:38 +00001089 V = EmitScalarConversion(V, Ty, Arg->getType());
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001090 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001091 EmitParmDecl(*Arg, V);
1092 break;
1093 }
Daniel Dunbar56273772008-09-17 00:51:38 +00001094
1095 case ABIArgInfo::Expand: {
1096 // If this was structure was expand into multiple arguments then
1097 // we need to create a temporary and reconstruct it from the
1098 // arguments.
Chris Lattner39f34e92008-11-24 04:00:27 +00001099 std::string Name = Arg->getNameAsString();
Daniel Dunbar56273772008-09-17 00:51:38 +00001100 llvm::Value *Temp = CreateTempAlloca(ConvertType(Ty),
1101 (Name + ".addr").c_str());
1102 // FIXME: What are the right qualifiers here?
1103 llvm::Function::arg_iterator End =
1104 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI);
1105 EmitParmDecl(*Arg, Temp);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001106
Daniel Dunbar56273772008-09-17 00:51:38 +00001107 // Name the arguments used in expansion and increment AI.
1108 unsigned Index = 0;
1109 for (; AI != End; ++AI, ++Index)
1110 AI->setName(Name + "." + llvm::utostr(Index));
1111 continue;
1112 }
Daniel Dunbar11434922009-01-26 21:26:08 +00001113
1114 case ABIArgInfo::Ignore:
1115 break;
1116
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001117 case ABIArgInfo::Coerce:
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001118 case ABIArgInfo::StructRet:
1119 assert(0 && "Invalid ABI kind for non-return argument");
1120 }
Daniel Dunbar56273772008-09-17 00:51:38 +00001121
1122 ++AI;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001123 }
1124 assert(AI == Fn->arg_end() && "Argument mismatch!");
1125}
1126
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +00001127/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
1128/// a pointer to an object of type \arg Ty.
1129///
1130/// This safely handles the case when the src type is smaller than the
1131/// destination type; in this situation the values of bits which not
1132/// present in the src are undefined.
1133static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
1134 const llvm::Type *Ty,
1135 CodeGenFunction &CGF) {
1136 const llvm::Type *SrcTy =
1137 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
Daniel Dunbare620ecd2009-01-30 00:47:38 +00001138 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
1139 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(Ty);
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +00001140
1141 // If load is legal, just bitcase the src pointer.
1142 if (SrcSize == DstSize) {
1143 llvm::Value *Casted =
1144 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
1145 return CGF.Builder.CreateLoad(Casted);
1146 } else {
1147 assert(SrcSize < DstSize && "Coercion is losing source bits!");
1148
1149 // Otherwise do coercion through memory. This is stupid, but
1150 // simple.
1151 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
1152 llvm::Value *Casted =
1153 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
1154 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
1155 return CGF.Builder.CreateLoad(Tmp);
1156 }
1157}
1158
1159/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
1160/// where the source and destination may have different types.
1161///
1162/// This safely handles the case when the src type is larger than the
1163/// destination type; the upper bits of the src will be lost.
1164static void CreateCoercedStore(llvm::Value *Src,
1165 llvm::Value *DstPtr,
1166 CodeGenFunction &CGF) {
1167 const llvm::Type *SrcTy = Src->getType();
1168 const llvm::Type *DstTy =
1169 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
1170
Daniel Dunbare620ecd2009-01-30 00:47:38 +00001171 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
1172 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(DstTy);
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +00001173
1174 // If store is legal, just bitcase the src pointer.
1175 if (SrcSize == DstSize) {
1176 llvm::Value *Casted =
1177 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
1178 CGF.Builder.CreateStore(Src, Casted);
1179 } else {
1180 assert(SrcSize > DstSize && "Coercion is missing bits!");
1181
1182 // Otherwise do coercion through memory. This is stupid, but
1183 // simple.
1184 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
1185 CGF.Builder.CreateStore(Src, Tmp);
1186 llvm::Value *Casted =
1187 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
1188 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(Casted), DstPtr);
1189 }
1190}
1191
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001192void CodeGenFunction::EmitFunctionEpilog(QualType RetTy,
1193 llvm::Value *ReturnValue) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001194 llvm::Value *RV = 0;
1195
1196 // Functions with no result always return void.
1197 if (ReturnValue) {
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001198 ABIArgInfo RetAI = getABIReturnInfo(RetTy, CGM.getTypes());
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001199
1200 switch (RetAI.getKind()) {
1201 case ABIArgInfo::StructRet:
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001202 if (RetTy->isAnyComplexType()) {
1203 // FIXME: Volatile
1204 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
1205 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
1206 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1207 EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy);
1208 } else {
1209 Builder.CreateStore(Builder.CreateLoad(ReturnValue),
1210 CurFn->arg_begin());
1211 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001212 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001213
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001214 case ABIArgInfo::Default:
1215 RV = Builder.CreateLoad(ReturnValue);
1216 break;
1217
Daniel Dunbar11434922009-01-26 21:26:08 +00001218 case ABIArgInfo::Ignore:
1219 break;
1220
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001221 case ABIArgInfo::Coerce: {
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +00001222 RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001223 break;
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001224 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001225
1226 case ABIArgInfo::ByVal:
1227 case ABIArgInfo::Expand:
1228 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001229 }
1230 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001231
1232 if (RV) {
1233 Builder.CreateRet(RV);
1234 } else {
1235 Builder.CreateRetVoid();
1236 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001237}
1238
1239RValue CodeGenFunction::EmitCall(llvm::Value *Callee,
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001240 QualType RetTy,
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001241 const CallArgList &CallArgs) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001242 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001243
1244 // Handle struct-return functions by passing a pointer to the
1245 // location that we would like to return into.
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001246 ABIArgInfo RetAI = getABIReturnInfo(RetTy, CGM.getTypes());
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001247 switch (RetAI.getKind()) {
1248 case ABIArgInfo::StructRet:
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001249 // Create a temporary alloca to hold the result of the call. :(
Daniel Dunbar56273772008-09-17 00:51:38 +00001250 Args.push_back(CreateTempAlloca(ConvertType(RetTy)));
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001251 break;
1252
1253 case ABIArgInfo::Default:
Daniel Dunbar11434922009-01-26 21:26:08 +00001254 case ABIArgInfo::Ignore:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001255 case ABIArgInfo::Coerce:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001256 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001257
1258 case ABIArgInfo::ByVal:
1259 case ABIArgInfo::Expand:
1260 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001261 }
1262
1263 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
1264 I != E; ++I) {
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001265 ABIArgInfo ArgInfo = getABIArgumentInfo(I->second, CGM.getTypes());
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001266 RValue RV = I->first;
Daniel Dunbar56273772008-09-17 00:51:38 +00001267
1268 switch (ArgInfo.getKind()) {
1269 case ABIArgInfo::ByVal: // Default is byval
1270 case ABIArgInfo::Default:
1271 if (RV.isScalar()) {
1272 Args.push_back(RV.getScalarVal());
1273 } else if (RV.isComplex()) {
1274 // Make a temporary alloca to pass the argument.
1275 Args.push_back(CreateTempAlloca(ConvertType(I->second)));
1276 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
1277 } else {
1278 Args.push_back(RV.getAggregateAddr());
1279 }
1280 break;
1281
Daniel Dunbar11434922009-01-26 21:26:08 +00001282 case ABIArgInfo::Ignore:
1283 break;
1284
Daniel Dunbar56273772008-09-17 00:51:38 +00001285 case ABIArgInfo::StructRet:
1286 case ABIArgInfo::Coerce:
1287 assert(0 && "Invalid ABI kind for non-return argument");
1288 break;
1289
1290 case ABIArgInfo::Expand:
1291 ExpandTypeToArgs(I->second, RV, Args);
1292 break;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001293 }
1294 }
1295
1296 llvm::CallInst *CI = Builder.CreateCall(Callee,&Args[0],&Args[0]+Args.size());
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001297 CGCallInfo CallInfo(RetTy, CallArgs);
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001298
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001299 // FIXME: Provide TargetDecl so nounwind, noreturn, etc, etc get set.
Devang Patel761d7f72008-09-25 21:02:23 +00001300 CodeGen::AttributeListType AttributeList;
1301 CGM.ConstructAttributeList(0,
Daniel Dunbarb7688072008-09-10 00:41:16 +00001302 CallInfo.argtypes_begin(), CallInfo.argtypes_end(),
Devang Patel761d7f72008-09-25 21:02:23 +00001303 AttributeList);
1304 CI->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(),
1305 AttributeList.size()));
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001306
1307 if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee))
1308 CI->setCallingConv(F->getCallingConv());
1309 if (CI->getType() != llvm::Type::VoidTy)
1310 CI->setName("call");
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001311
1312 switch (RetAI.getKind()) {
1313 case ABIArgInfo::StructRet:
1314 if (RetTy->isAnyComplexType())
Daniel Dunbar56273772008-09-17 00:51:38 +00001315 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001316 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar56273772008-09-17 00:51:38 +00001317 return RValue::getAggregate(Args[0]);
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001318 else
1319 return RValue::get(Builder.CreateLoad(Args[0]));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001320
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001321 case ABIArgInfo::Default:
1322 return RValue::get(RetTy->isVoidType() ? 0 : CI);
1323
Daniel Dunbar11434922009-01-26 21:26:08 +00001324 case ABIArgInfo::Ignore:
Daniel Dunbarcc039fe2009-01-29 08:24:57 +00001325 if (RetTy->isVoidType())
1326 return RValue::get(0);
1327 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1328 llvm::Value *Res =
1329 llvm::UndefValue::get(llvm::PointerType::getUnqual(ConvertType(RetTy)));
1330 return RValue::getAggregate(Res);
1331 }
1332 return RValue::get(llvm::UndefValue::get(ConvertType(RetTy)));
Daniel Dunbar11434922009-01-26 21:26:08 +00001333
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001334 case ABIArgInfo::Coerce: {
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +00001335 llvm::Value *V = CreateTempAlloca(ConvertType(RetTy), "coerce");
1336 CreateCoercedStore(CI, V, *this);
Anders Carlssonad3d6912008-11-25 22:21:48 +00001337 if (RetTy->isAnyComplexType())
1338 return RValue::getComplex(LoadComplexFromAddr(V, false));
Daniel Dunbar11434922009-01-26 21:26:08 +00001339 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssonad3d6912008-11-25 22:21:48 +00001340 return RValue::getAggregate(V);
Daniel Dunbar11434922009-01-26 21:26:08 +00001341 else
1342 return RValue::get(Builder.CreateLoad(V));
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001343 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001344
1345 case ABIArgInfo::ByVal:
1346 case ABIArgInfo::Expand:
1347 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001348 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001349
1350 assert(0 && "Unhandled ABIArgInfo::Kind");
1351 return RValue::get(0);
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001352}