blob: 21eccdc2a363d508ae9b113d7bfea1b5ea75bfbc [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 Dunbarbccb0682008-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 Dunbara8f02052008-09-08 21:33:45 +000082}
83
84/***/
85
Daniel Dunbarbccb0682008-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 Dunbara8f02052008-09-08 21:33:45 +000089 ArgTypes.push_back(i->second);
90}
91
Daniel Dunbarbccb0682008-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 Dunbara8f02052008-09-08 21:33:45 +000098}
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +000099
100/***/
101
Daniel Dunbar22e30052008-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 Dunbare126ab12008-09-10 02:41:04 +0000104class ABIArgInfo {
105public:
106 enum Kind {
107 Default,
Daniel Dunbar17d35372008-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 Dunbar22e30052008-09-11 01:48:57 +0000112
Daniel Dunbar1358b202009-01-26 21:26:08 +0000113 Ignore, /// Ignore the argument (treat as void). Useful for
114 /// void and empty structs.
115
Daniel Dunbar04d35782008-09-17 00:51:38 +0000116 Coerce, /// Only valid for aggregate return types, the argument
117 /// should be accessed by coercion to a provided type.
Daniel Dunbar22e30052008-09-11 01:48:57 +0000118
119 ByVal, /// Only valid for aggregate argument types. The
120 /// structure should be passed "byval" with the
121 /// specified alignment (0 indicates default
122 /// alignment).
123
124 Expand, /// Only valid for aggregate argument types. The
125 /// structure should be expanded into consecutive
Daniel Dunbar04d35782008-09-17 00:51:38 +0000126 /// arguments for its constituent fields. Currently
127 /// expand is only allowed on structures whose fields
128 /// are all scalar types or are themselves expandable
129 /// types.
Daniel Dunbar22e30052008-09-11 01:48:57 +0000130
131 KindFirst=Default, KindLast=Expand
Daniel Dunbare126ab12008-09-10 02:41:04 +0000132 };
133
134private:
135 Kind TheKind;
Daniel Dunbar73d66602008-09-10 07:04:09 +0000136 const llvm::Type *TypeData;
Daniel Dunbar22e30052008-09-11 01:48:57 +0000137 unsigned UIntData;
Daniel Dunbare126ab12008-09-10 02:41:04 +0000138
Daniel Dunbar22e30052008-09-11 01:48:57 +0000139 ABIArgInfo(Kind K, const llvm::Type *TD=0,
140 unsigned UI=0) : TheKind(K),
141 TypeData(TD),
142 UIntData(0) {}
Daniel Dunbare126ab12008-09-10 02:41:04 +0000143public:
144 static ABIArgInfo getDefault() {
Daniel Dunbar22e30052008-09-11 01:48:57 +0000145 return ABIArgInfo(Default);
Daniel Dunbare126ab12008-09-10 02:41:04 +0000146 }
147 static ABIArgInfo getStructRet() {
Daniel Dunbar22e30052008-09-11 01:48:57 +0000148 return ABIArgInfo(StructRet);
Daniel Dunbare126ab12008-09-10 02:41:04 +0000149 }
Daniel Dunbar1358b202009-01-26 21:26:08 +0000150 static ABIArgInfo getIgnore() {
151 return ABIArgInfo(Ignore);
152 }
Daniel Dunbar73d66602008-09-10 07:04:09 +0000153 static ABIArgInfo getCoerce(const llvm::Type *T) {
Daniel Dunbare126ab12008-09-10 02:41:04 +0000154 return ABIArgInfo(Coerce, T);
155 }
Daniel Dunbar22e30052008-09-11 01:48:57 +0000156 static ABIArgInfo getByVal(unsigned Alignment) {
157 return ABIArgInfo(ByVal, 0, Alignment);
158 }
Daniel Dunbar04d35782008-09-17 00:51:38 +0000159 static ABIArgInfo getExpand() {
160 return ABIArgInfo(Expand);
161 }
Daniel Dunbare126ab12008-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 Dunbar1358b202009-01-26 21:26:08 +0000166 bool isIgnore() const { return TheKind == Ignore; }
Daniel Dunbare126ab12008-09-10 02:41:04 +0000167 bool isCoerce() const { return TheKind == Coerce; }
Daniel Dunbar22e30052008-09-11 01:48:57 +0000168 bool isByVal() const { return TheKind == ByVal; }
Daniel Dunbar04d35782008-09-17 00:51:38 +0000169 bool isExpand() const { return TheKind == Expand; }
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000170
171 // Coerce accessors
Daniel Dunbar73d66602008-09-10 07:04:09 +0000172 const llvm::Type *getCoerceToType() const {
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000173 assert(TheKind == Coerce && "Invalid kind!");
174 return TypeData;
175 }
Daniel Dunbar22e30052008-09-11 01:48:57 +0000176
177 // ByVal accessors
178 unsigned getByValAlignment() const {
179 assert(TheKind == ByVal && "Invalid kind!");
180 return UIntData;
181 }
Daniel Dunbare126ab12008-09-10 02:41:04 +0000182};
183
184/***/
185
Daniel Dunbarf98eeff2008-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 Dunbar99eebc62008-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 Gregor5d764842009-01-09 17:18:27 +0000217 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar99eebc62008-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 Gregor5d764842009-01-09 17:18:27 +0000244 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar99eebc62008-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 Gregor5d764842009-01-09 17:18:27 +0000275 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar99eebc62008-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 Dunbarf98eeff2008-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 Dunbare126ab12008-09-10 02:41:04 +0000319 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar99eebc62008-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 Dunbar73d66602008-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 Dunbare126ab12008-09-10 02:41:04 +0000357 } else {
358 return ABIArgInfo::getDefault();
359 }
360}
361
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000362ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
363 ASTContext &Context) const {
Daniel Dunbar3158c592008-09-17 20:11:04 +0000364 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar99eebc62008-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 Dunbar22e30052008-09-11 01:48:57 +0000384 return ABIArgInfo::getByVal(0);
385 } else {
386 return ABIArgInfo::getDefault();
387 }
388}
389
Daniel Dunbare09a9692009-01-24 08:32:22 +0000390namespace {
391/// X86_32ABIInfo - The X86_64 ABI information.
392class 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 Dunbar11dc6772009-01-30 08:09:32 +0000404 /// merge - Implement the X86_64 ABI merging algorithm.
405 ///
406 /// \param Offset - The offset of the current field.
407 /// \param FieldLo - The low classification of the current field.
408 /// \param FieldHi - The high classification of the current field.
409 /// \param Lo [in] [out] - The accumulated low classification.
410 /// \param Lo [in] [out] - The accumulated high classification.
411 void merge(uint64_t Offset, Class FieldLo, Class FieldHi,
412 Class &Lo, Class &Hi) const;
413
Daniel Dunbare09a9692009-01-24 08:32:22 +0000414 /// classify - Determine the x86_64 register classes in which the
415 /// given type T should be passed.
416 ///
417 /// \param Lo - The classification for the low word of the type.
418 /// \param Hi - The classification for the high word of the type.
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000419 /// \param OffsetBase - The byte position of the type in the root
420 /// structure. Some parameters are classified different depending on
421 /// whether they straddle an eightbyte boundary.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000422 ///
423 /// If a word is unused its result will be NoClass; if a type should
424 /// be passed in Memory then at least the classification of \arg Lo
425 /// will be Memory.
426 ///
427 /// The \arg Lo class will be NoClass iff the argument is ignored.
428 ///
429 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
430 /// be NoClass.
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000431 void classify(QualType T, ASTContext &Context, uint64_t OffsetBase,
Daniel Dunbare09a9692009-01-24 08:32:22 +0000432 Class &Lo, Class &Hi) const;
433
434public:
435 virtual ABIArgInfo classifyReturnType(QualType RetTy,
436 ASTContext &Context) const;
437
438 virtual ABIArgInfo classifyArgumentType(QualType RetTy,
439 ASTContext &Context) const;
440};
441}
442
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000443void X86_64ABIInfo::merge(uint64_t Offset, Class FieldLo, Class FieldHi,
444 Class &Lo, Class &Hi) const {
445 // Determine which half of the structure we are classifying.
446 //
447 // AMD64-ABI 3.2.3p2: Rule 3. f the size of the aggregate
448 // exceeds a single eightbyte, each is classified
449 // separately. Each eightbyte gets initialized to class
450 // NO_CLASS.
451 Class &Target = Offset < 64 ? Lo : Hi;
452
453 // Merge the lo field classifcation.
454 //
455 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
456 // classified recursively so that always two fields are
457 // considered. The resulting class is calculated according to
458 // the classes of the fields in the eightbyte:
459 //
460 // (a) If both classes are equal, this is the resulting class.
461 //
462 // (b) If one of the classes is NO_CLASS, the resulting class is
463 // the other class.
464 //
465 // (c) If one of the classes is MEMORY, the result is the MEMORY
466 // class.
467 //
468 // (d) If one of the classes is INTEGER, the result is the
469 // INTEGER.
470 //
471 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
472 // MEMORY is used as class.
473 //
474 // (f) Otherwise class SSE is used.
475 if (Target == FieldLo || FieldLo == NoClass) ;
476 else if (FieldLo == Memory)
477 Lo = Memory;
478 else if (Target == NoClass)
479 Target = FieldLo;
480 else if (Target == Integer || FieldLo == Integer)
481 Target = Integer;
482 else if (FieldLo == X87 || FieldLo == X87Up || FieldLo == ComplexX87)
483 Lo = Memory;
484 else
485 Target = SSE;
486
487 // It isn't clear from the ABI spec what the role of the high
488 // classification is here, but since this should only happen
489 // when we have a struct with a two eightbyte member, we can
490 // just push the field high class into the overall high class.
491 if (FieldHi != NoClass)
492 Hi = FieldHi;
493}
494
Daniel Dunbare09a9692009-01-24 08:32:22 +0000495void X86_64ABIInfo::classify(QualType Ty,
496 ASTContext &Context,
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000497 uint64_t OffsetBase,
Daniel Dunbare09a9692009-01-24 08:32:22 +0000498 Class &Lo, Class &Hi) const {
499 Lo = Memory;
500 Hi = NoClass;
501 if (const BuiltinType *BT = Ty->getAsBuiltinType()) {
502 BuiltinType::Kind k = BT->getKind();
503
Daniel Dunbar1358b202009-01-26 21:26:08 +0000504 if (k == BuiltinType::Void) {
505 Lo = NoClass;
506 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
Daniel Dunbare09a9692009-01-24 08:32:22 +0000507 Lo = Integer;
508 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
509 Lo = SSE;
510 } else if (k == BuiltinType::LongDouble) {
511 Lo = X87;
512 Hi = X87Up;
513 }
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000514
515 // FIXME: _Decimal32 and _Decimal64 are SSE.
516 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Daniel Dunbare09a9692009-01-24 08:32:22 +0000517 // FIXME: __int128 is (Integer, Integer).
518 } else if (Ty->isPointerLikeType() || Ty->isBlockPointerType() ||
519 Ty->isObjCQualifiedInterfaceType()) {
520 Lo = Integer;
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000521 } else if (const VectorType *VT = Ty->getAsVectorType()) {
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000522 uint64_t Size = Context.getTypeSize(VT);
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000523 if (Size == 64) {
524 // FIXME: For some reason, gcc appears to be treating <1 x
525 // double> as INTEGER; this seems wrong, but we will match for
526 // now (icc rejects <1 x double>, so...).
527 Lo = (VT->getElementType() == Context.DoubleTy) ? Integer : SSE;
Daniel Dunbare413f532009-01-30 18:40:10 +0000528
529 // If this type crosses an eightbyte boundary, it should be
530 // split.
531 if (OffsetBase && OffsetBase != 8)
532 Hi = Lo;
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000533 } else if (Size == 128) {
534 Lo = SSE;
535 Hi = SSEUp;
536 }
Daniel Dunbare09a9692009-01-24 08:32:22 +0000537 } else if (const ComplexType *CT = Ty->getAsComplexType()) {
538 QualType ET = CT->getElementType();
539
Daniel Dunbare413f532009-01-30 18:40:10 +0000540 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbar28770fc2009-01-29 07:22:20 +0000541 if (ET->isIntegerType()) {
Daniel Dunbar28770fc2009-01-29 07:22:20 +0000542 if (Size <= 64)
543 Lo = Integer;
544 else if (Size <= 128)
545 Lo = Hi = Integer;
546 } else if (ET == Context.FloatTy)
Daniel Dunbare09a9692009-01-24 08:32:22 +0000547 Lo = SSE;
548 else if (ET == Context.DoubleTy)
549 Lo = Hi = SSE;
550 else if (ET == Context.LongDoubleTy)
551 Lo = ComplexX87;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000552
553 // If this complex type crosses an eightbyte boundary then it
554 // should be split.
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000555 uint64_t EB_Real = (OffsetBase) >> 3;
Daniel Dunbare413f532009-01-30 18:40:10 +0000556 uint64_t EB_Imag = (OffsetBase + Size) >> 3;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000557 if (Hi == NoClass && EB_Real != EB_Imag)
558 Hi = Lo;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000559 } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
560 // Arrays are treated like structures.
561
562 uint64_t Size = Context.getTypeSize(Ty);
563
564 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
565 // than two eightbytes, ..., it has class MEMORY.
566 if (Size > 128)
567 return;
568
569 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
570 // fields, it has class MEMORY.
571 //
572 // Only need to check alignment of array base.
573 if (OffsetBase % Context.getTypeAlign(AT->getElementType())) {
574 Lo = Memory;
575 return;
576 }
577
578 // Otherwise implement simplified merge. We could be smarter about
579 // this, but it isn't worth it and would be harder to verify.
580 Lo = NoClass;
581 uint64_t EltSize = Context.getTypeSize(AT->getElementType());
582 uint64_t ArraySize = AT->getSize().getZExtValue();
583 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
584 Class FieldLo, FieldHi;
585 classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi);
586
587 merge(Offset, FieldLo, FieldHi, Lo, Hi);
588 // Memory is never over-ridden, exit early if we see it.
589 if (Lo == Memory)
590 return;
591 }
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000592 } else if (const RecordType *RT = Ty->getAsRecordType()) {
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000593 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000594
595 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
596 // than two eightbytes, ..., it has class MEMORY.
597 if (Size > 128)
598 return;
599
600 const RecordDecl *RD = RT->getDecl();
601
602 // Assume variable sized types are passed in memory.
603 if (RD->hasFlexibleArrayMember())
604 return;
605
606 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
607
608 // Reset Lo class, this will be recomputed.
609 Lo = NoClass;
610 unsigned idx = 0;
611 for (RecordDecl::field_iterator i = RD->field_begin(),
612 e = RD->field_end(); i != e; ++i, ++idx) {
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000613 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000614
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000615 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
616 // fields, it has class MEMORY.
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000617 if (Offset % Context.getTypeAlign(i->getType())) {
618 Lo = Memory;
619 return;
620 }
621
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000622 // Classify this field.
623 Class FieldLo, FieldHi;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000624 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000625
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000626 merge(Offset, FieldLo, FieldHi, Lo, Hi);
627 // Memory is never over-ridden, exit early if we see it.
628 if (Lo == Memory)
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000629 return;
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000630 }
631
632 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
633 //
634 // (a) If one of the classes is MEMORY, the whole argument is
635 // passed in memory.
636 //
637 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
638
639 // The first of these conditions is guaranteed by how we implement
640 // the merge (just bail). I don't believe the second is actually
641 // possible at all.
642 assert(Lo != Memory && "Unexpected memory classification.");
643 if (Hi == SSEUp && Lo != SSE)
644 Hi = SSE;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000645 }
646}
647
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000648ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
649 ASTContext &Context) const {
Daniel Dunbare09a9692009-01-24 08:32:22 +0000650 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
651 // classification algorithm.
652 X86_64ABIInfo::Class Lo, Hi;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000653 classify(RetTy, Context, 0, Lo, Hi);
Daniel Dunbare09a9692009-01-24 08:32:22 +0000654
655 const llvm::Type *ResType = 0;
656 switch (Lo) {
657 case NoClass:
Daniel Dunbar1358b202009-01-26 21:26:08 +0000658 return ABIArgInfo::getIgnore();
Daniel Dunbare09a9692009-01-24 08:32:22 +0000659
660 case SSEUp:
661 case X87Up:
662 assert(0 && "Invalid classification for lo word.");
663
664 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
665 // hidden argument, i.e. structret.
666 case Memory:
667 return ABIArgInfo::getStructRet();
668
669 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
670 // available register of the sequence %rax, %rdx is used.
671 case Integer:
672 ResType = llvm::Type::Int64Ty; break;
673
674 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
675 // available SSE register of the sequence %xmm0, %xmm1 is used.
676 case SSE:
677 ResType = llvm::Type::DoubleTy; break;
678
679 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
680 // returned on the X87 stack in %st0 as 80-bit x87 number.
681 case X87:
682 ResType = llvm::Type::X86_FP80Ty; break;
683
684 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
685 // part of the value is returned in %st0 and the imaginary part in
686 // %st1.
687 case ComplexX87:
688 assert(Hi == NoClass && "Unexpected ComplexX87 classification.");
689 ResType = llvm::VectorType::get(llvm::Type::X86_FP80Ty, 2);
690 break;
691 }
692
693 switch (Hi) {
694 // Memory was handled previously, and ComplexX87 and X87 should
695 // never occur as hi classes.
696 case Memory:
697 case X87:
698 case ComplexX87:
699 assert(0 && "Invalid classification for hi word.");
700
701 case NoClass: break;
702 case Integer:
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000703 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
704 break;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000705 case SSE:
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000706 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
707 break;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000708
709 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
710 // is passed in the upper half of the last used SSE register.
711 //
712 // SSEUP should always be preceeded by SSE, just widen.
713 case SSEUp:
714 assert(Lo == SSE && "Unexpected SSEUp classification.");
715 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
716 break;
717
718 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000719 // returned together with the previous X87 value in %st0.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000720 //
721 // X87UP should always be preceeded by X87, so we don't need to do
722 // anything here.
723 case X87Up:
724 assert(Lo == X87 && "Unexpected X87Up classification.");
725 break;
726 }
727
728 return ABIArgInfo::getCoerce(ResType);
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000729}
730
731ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty,
732 ASTContext &Context) const {
733 return ABIArgInfo::getDefault();
734}
735
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000736ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
737 ASTContext &Context) const {
738 return ABIArgInfo::getDefault();
739}
740
741ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
742 ASTContext &Context) const {
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000743 return ABIArgInfo::getDefault();
744}
745
746const ABIInfo &CodeGenTypes::getABIInfo() const {
747 if (TheABIInfo)
748 return *TheABIInfo;
749
750 // For now we just cache this in the CodeGenTypes and don't bother
751 // to free it.
752 const char *TargetPrefix = getContext().Target.getTargetPrefix();
753 if (strcmp(TargetPrefix, "x86") == 0) {
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000754 switch (getContext().Target.getPointerWidth(0)) {
755 case 32:
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000756 return *(TheABIInfo = new X86_32ABIInfo());
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000757 case 64:
Daniel Dunbar56555952009-01-30 18:47:53 +0000758 return *(TheABIInfo = new X86_64ABIInfo());
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000759 }
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000760 }
761
762 return *(TheABIInfo = new DefaultABIInfo);
763}
764
765// getABIReturnInfo - Wrap the ABIInfo getABIReturnInfo, altering
766// "default" types to StructRet when appropriate for simplicity.
767static ABIArgInfo getABIReturnInfo(QualType Ty, CodeGenTypes &CGT) {
768 assert(!Ty->isArrayType() &&
769 "Array types cannot be passed directly.");
770 ABIArgInfo Info = CGT.getABIInfo().classifyReturnType(Ty, CGT.getContext());
Daniel Dunbar3158c592008-09-17 20:11:04 +0000771 // Ensure default on aggregate types is StructRet.
772 if (Info.isDefault() && CodeGenFunction::hasAggregateLLVMType(Ty))
773 return ABIArgInfo::getStructRet();
774 return Info;
775}
776
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000777// getABIArgumentInfo - Wrap the ABIInfo getABIReturnInfo, altering
778// "default" types to ByVal when appropriate for simplicity.
779static ABIArgInfo getABIArgumentInfo(QualType Ty, CodeGenTypes &CGT) {
780 assert(!Ty->isArrayType() &&
781 "Array types cannot be passed directly.");
782 ABIArgInfo Info = CGT.getABIInfo().classifyArgumentType(Ty, CGT.getContext());
Daniel Dunbar3158c592008-09-17 20:11:04 +0000783 // Ensure default on aggregate types is ByVal.
784 if (Info.isDefault() && CodeGenFunction::hasAggregateLLVMType(Ty))
785 return ABIArgInfo::getByVal(0);
786 return Info;
787}
788
Daniel Dunbare126ab12008-09-10 02:41:04 +0000789/***/
790
Daniel Dunbar04d35782008-09-17 00:51:38 +0000791void CodeGenTypes::GetExpandedTypes(QualType Ty,
792 std::vector<const llvm::Type*> &ArgTys) {
793 const RecordType *RT = Ty->getAsStructureType();
794 assert(RT && "Can only expand structure types.");
795 const RecordDecl *RD = RT->getDecl();
796 assert(!RD->hasFlexibleArrayMember() &&
797 "Cannot expand structure with flexible array.");
798
Douglas Gregor5d764842009-01-09 17:18:27 +0000799 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar04d35782008-09-17 00:51:38 +0000800 e = RD->field_end(); i != e; ++i) {
801 const FieldDecl *FD = *i;
802 assert(!FD->isBitField() &&
803 "Cannot expand structure with bit-field members.");
804
805 QualType FT = FD->getType();
806 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
807 GetExpandedTypes(FT, ArgTys);
808 } else {
809 ArgTys.push_back(ConvertType(FT));
810 }
811 }
812}
813
814llvm::Function::arg_iterator
815CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
816 llvm::Function::arg_iterator AI) {
817 const RecordType *RT = Ty->getAsStructureType();
818 assert(RT && "Can only expand structure types.");
819
820 RecordDecl *RD = RT->getDecl();
821 assert(LV.isSimple() &&
822 "Unexpected non-simple lvalue during struct expansion.");
823 llvm::Value *Addr = LV.getAddress();
824 for (RecordDecl::field_iterator i = RD->field_begin(),
825 e = RD->field_end(); i != e; ++i) {
826 FieldDecl *FD = *i;
827 QualType FT = FD->getType();
828
829 // FIXME: What are the right qualifiers here?
830 LValue LV = EmitLValueForField(Addr, FD, false, 0);
831 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
832 AI = ExpandTypeFromArgs(FT, LV, AI);
833 } else {
834 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
835 ++AI;
836 }
837 }
838
839 return AI;
840}
841
842void
843CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
844 llvm::SmallVector<llvm::Value*, 16> &Args) {
845 const RecordType *RT = Ty->getAsStructureType();
846 assert(RT && "Can only expand structure types.");
847
848 RecordDecl *RD = RT->getDecl();
849 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
850 llvm::Value *Addr = RV.getAggregateAddr();
851 for (RecordDecl::field_iterator i = RD->field_begin(),
852 e = RD->field_end(); i != e; ++i) {
853 FieldDecl *FD = *i;
854 QualType FT = FD->getType();
855
856 // FIXME: What are the right qualifiers here?
857 LValue LV = EmitLValueForField(Addr, FD, false, 0);
858 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
859 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
860 } else {
861 RValue RV = EmitLoadOfLValue(LV, FT);
862 assert(RV.isScalar() &&
863 "Unexpected non-scalar rvalue during struct expansion.");
864 Args.push_back(RV.getScalarVal());
865 }
866 }
867}
868
869/***/
870
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000871const llvm::FunctionType *
Daniel Dunbara9976a22008-09-10 07:00:50 +0000872CodeGenTypes::GetFunctionType(const CGCallInfo &CI, bool IsVariadic) {
873 return GetFunctionType(CI.argtypes_begin(), CI.argtypes_end(), IsVariadic);
874}
875
876const llvm::FunctionType *
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000877CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI) {
Daniel Dunbara9976a22008-09-10 07:00:50 +0000878 return GetFunctionType(FI.argtypes_begin(), FI.argtypes_end(), FI.isVariadic());
879}
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 Dunbare126ab12008-09-10 02:41:04 +0000955 ArgTypeIterator begin,
956 ArgTypeIterator end,
Devang Patela85a9ef2008-09-25 21:02:23 +0000957 AttributeListType &PAL) {
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000958 unsigned FuncAttrs = 0;
Devang Patel2bb6eb82008-09-26 22:53:57 +0000959 unsigned RetAttrs = 0;
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000960
961 if (TargetDecl) {
962 if (TargetDecl->getAttr<NoThrowAttr>())
Devang Patela85a9ef2008-09-25 21:02:23 +0000963 FuncAttrs |= llvm::Attribute::NoUnwind;
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000964 if (TargetDecl->getAttr<NoReturnAttr>())
Devang Patela85a9ef2008-09-25 21:02:23 +0000965 FuncAttrs |= llvm::Attribute::NoReturn;
Anders Carlssondd6791c2008-10-05 23:32:53 +0000966 if (TargetDecl->getAttr<PureAttr>())
967 FuncAttrs |= llvm::Attribute::ReadOnly;
968 if (TargetDecl->getAttr<ConstAttr>())
969 FuncAttrs |= llvm::Attribute::ReadNone;
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000970 }
971
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,
988 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 Dunbare126ab12008-09-10 02:41:04 +00001287 CGCallInfo CallInfo(RetTy, CallArgs);
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001288
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001289 // FIXME: Provide TargetDecl so nounwind, noreturn, etc, etc get set.
Devang Patela85a9ef2008-09-25 21:02:23 +00001290 CodeGen::AttributeListType AttributeList;
1291 CGM.ConstructAttributeList(0,
Daniel Dunbar3ef2e852008-09-10 00:41:16 +00001292 CallInfo.argtypes_begin(), CallInfo.argtypes_end(),
Devang Patela85a9ef2008-09-25 21:02:23 +00001293 AttributeList);
1294 CI->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(),
1295 AttributeList.size()));
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001296
1297 if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee))
1298 CI->setCallingConv(F->getCallingConv());
1299 if (CI->getType() != llvm::Type::VoidTy)
1300 CI->setName("call");
Daniel Dunbare126ab12008-09-10 02:41:04 +00001301
1302 switch (RetAI.getKind()) {
1303 case ABIArgInfo::StructRet:
1304 if (RetTy->isAnyComplexType())
Daniel Dunbar04d35782008-09-17 00:51:38 +00001305 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Daniel Dunbar17d35372008-12-18 04:52:14 +00001306 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar04d35782008-09-17 00:51:38 +00001307 return RValue::getAggregate(Args[0]);
Daniel Dunbar17d35372008-12-18 04:52:14 +00001308 else
1309 return RValue::get(Builder.CreateLoad(Args[0]));
Daniel Dunbar22e30052008-09-11 01:48:57 +00001310
Daniel Dunbare126ab12008-09-10 02:41:04 +00001311 case ABIArgInfo::Default:
1312 return RValue::get(RetTy->isVoidType() ? 0 : CI);
1313
Daniel Dunbar1358b202009-01-26 21:26:08 +00001314 case ABIArgInfo::Ignore:
Daniel Dunbar5c9c7f02009-01-29 08:24:57 +00001315 if (RetTy->isVoidType())
1316 return RValue::get(0);
1317 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1318 llvm::Value *Res =
1319 llvm::UndefValue::get(llvm::PointerType::getUnqual(ConvertType(RetTy)));
1320 return RValue::getAggregate(Res);
1321 }
1322 return RValue::get(llvm::UndefValue::get(ConvertType(RetTy)));
Daniel Dunbar1358b202009-01-26 21:26:08 +00001323
Daniel Dunbar73d66602008-09-10 07:04:09 +00001324 case ABIArgInfo::Coerce: {
Daniel Dunbar708d8a82009-01-27 01:36:03 +00001325 llvm::Value *V = CreateTempAlloca(ConvertType(RetTy), "coerce");
1326 CreateCoercedStore(CI, V, *this);
Anders Carlssonfccf7472008-11-25 22:21:48 +00001327 if (RetTy->isAnyComplexType())
1328 return RValue::getComplex(LoadComplexFromAddr(V, false));
Daniel Dunbar1358b202009-01-26 21:26:08 +00001329 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssonfccf7472008-11-25 22:21:48 +00001330 return RValue::getAggregate(V);
Daniel Dunbar1358b202009-01-26 21:26:08 +00001331 else
1332 return RValue::get(Builder.CreateLoad(V));
Daniel Dunbar73d66602008-09-10 07:04:09 +00001333 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001334
1335 case ABIArgInfo::ByVal:
1336 case ABIArgInfo::Expand:
1337 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001338 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001339
1340 assert(0 && "Unhandled ABIArgInfo::Kind");
1341 return RValue::get(0);
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001342}