blob: 898c780aaeccc2357da489be89078aba8ba12ce7 [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 Dunbar541b63b2009-02-02 23:23:47 +000034const
35CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionTypeNoProto *FTNP) {
36 return getFunctionInfo(FTNP->getResultType(),
37 llvm::SmallVector<QualType, 16>());
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000038}
39
Daniel Dunbar541b63b2009-02-02 23:23:47 +000040const
41CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionTypeProto *FTP) {
42 llvm::SmallVector<QualType, 16> ArgTys;
43 // FIXME: Kill copy.
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000044 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +000045 ArgTys.push_back(FTP->getArgType(i));
46 return getFunctionInfo(FTP->getResultType(), ArgTys);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000047}
48
Daniel Dunbar541b63b2009-02-02 23:23:47 +000049const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionDecl *FD) {
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000050 const FunctionType *FTy = FD->getType()->getAsFunctionType();
Daniel Dunbar541b63b2009-02-02 23:23:47 +000051 if (const FunctionTypeProto *FTP = dyn_cast<FunctionTypeProto>(FTy))
52 return getFunctionInfo(FTP);
53 return getFunctionInfo(cast<FunctionTypeNoProto>(FTy));
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000054}
55
Daniel Dunbar541b63b2009-02-02 23:23:47 +000056const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const ObjCMethodDecl *MD) {
57 llvm::SmallVector<QualType, 16> ArgTys;
58 ArgTys.push_back(MD->getSelfDecl()->getType());
59 ArgTys.push_back(Context.getObjCSelType());
60 // FIXME: Kill copy?
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000061 for (ObjCMethodDecl::param_const_iterator i = MD->param_begin(),
62 e = MD->param_end(); i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +000063 ArgTys.push_back((*i)->getType());
64 return getFunctionInfo(MD->getResultType(), ArgTys);
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000065}
66
Daniel Dunbar541b63b2009-02-02 23:23:47 +000067const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
68 const CallArgList &Args) {
69 // FIXME: Kill copy.
70 llvm::SmallVector<QualType, 16> ArgTys;
Daniel Dunbar725ad312009-01-31 02:19:00 +000071 for (CallArgList::const_iterator i = Args.begin(), e = Args.end();
72 i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +000073 ArgTys.push_back(i->second);
74 return getFunctionInfo(ResTy, ArgTys);
Daniel Dunbar725ad312009-01-31 02:19:00 +000075}
76
Daniel Dunbar541b63b2009-02-02 23:23:47 +000077const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
78 const FunctionArgList &Args) {
79 // FIXME: Kill copy.
80 llvm::SmallVector<QualType, 16> ArgTys;
Daniel Dunbarbb36d332009-02-02 21:43:58 +000081 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
82 i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +000083 ArgTys.push_back(i->second);
84 return getFunctionInfo(ResTy, ArgTys);
85}
86
87const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
88 const llvm::SmallVector<QualType, 16> &ArgTys) {
89 return *new CGFunctionInfo(ResTy, ArgTys);
90}
91
92/***/
93
94CGFunctionInfo::CGFunctionInfo(QualType ResTy,
95 const llvm::SmallVector<QualType, 16> &ArgTys) {
96 ArgTypes.push_back(ResTy);
97 ArgTypes.insert(ArgTypes.end(), ArgTys.begin(), ArgTys.end());
Daniel Dunbarbb36d332009-02-02 21:43:58 +000098}
99
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000100CGFunctionInfo::arg_iterator CGFunctionInfo::arg_begin() const {
101 return ArgTypes.begin()+1;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000102}
103
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000104CGFunctionInfo::arg_iterator CGFunctionInfo::arg_end() const {
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000105 return ArgTypes.end();
Daniel Dunbar0dbe2272008-09-08 21:33:45 +0000106}
107
108/***/
109
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000110/// ABIArgInfo - Helper class to encapsulate information about how a
111/// specific C type should be passed to or returned from a function.
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000112class ABIArgInfo {
113public:
114 enum Kind {
115 Default,
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +0000116 StructRet, /// Only valid for return values. The return value
117 /// should be passed through a pointer to a caller
118 /// allocated location passed as an implicit first
119 /// argument to the function.
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000120
Daniel Dunbar11434922009-01-26 21:26:08 +0000121 Ignore, /// Ignore the argument (treat as void). Useful for
122 /// void and empty structs.
123
Daniel Dunbar56273772008-09-17 00:51:38 +0000124 Coerce, /// Only valid for aggregate return types, the argument
125 /// should be accessed by coercion to a provided type.
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000126
127 ByVal, /// Only valid for aggregate argument types. The
128 /// structure should be passed "byval" with the
129 /// specified alignment (0 indicates default
130 /// alignment).
131
132 Expand, /// Only valid for aggregate argument types. The
133 /// structure should be expanded into consecutive
Daniel Dunbar56273772008-09-17 00:51:38 +0000134 /// arguments for its constituent fields. Currently
135 /// expand is only allowed on structures whose fields
136 /// are all scalar types or are themselves expandable
137 /// types.
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000138
139 KindFirst=Default, KindLast=Expand
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000140 };
141
142private:
143 Kind TheKind;
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000144 const llvm::Type *TypeData;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000145 unsigned UIntData;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000146
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000147 ABIArgInfo(Kind K, const llvm::Type *TD=0,
148 unsigned UI=0) : TheKind(K),
149 TypeData(TD),
150 UIntData(0) {}
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000151public:
152 static ABIArgInfo getDefault() {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000153 return ABIArgInfo(Default);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000154 }
155 static ABIArgInfo getStructRet() {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000156 return ABIArgInfo(StructRet);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000157 }
Daniel Dunbar11434922009-01-26 21:26:08 +0000158 static ABIArgInfo getIgnore() {
159 return ABIArgInfo(Ignore);
160 }
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000161 static ABIArgInfo getCoerce(const llvm::Type *T) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000162 return ABIArgInfo(Coerce, T);
163 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000164 static ABIArgInfo getByVal(unsigned Alignment) {
165 return ABIArgInfo(ByVal, 0, Alignment);
166 }
Daniel Dunbar56273772008-09-17 00:51:38 +0000167 static ABIArgInfo getExpand() {
168 return ABIArgInfo(Expand);
169 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000170
171 Kind getKind() const { return TheKind; }
172 bool isDefault() const { return TheKind == Default; }
173 bool isStructRet() const { return TheKind == StructRet; }
Daniel Dunbar11434922009-01-26 21:26:08 +0000174 bool isIgnore() const { return TheKind == Ignore; }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000175 bool isCoerce() const { return TheKind == Coerce; }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000176 bool isByVal() const { return TheKind == ByVal; }
Daniel Dunbar56273772008-09-17 00:51:38 +0000177 bool isExpand() const { return TheKind == Expand; }
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000178
179 // Coerce accessors
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000180 const llvm::Type *getCoerceToType() const {
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000181 assert(TheKind == Coerce && "Invalid kind!");
182 return TypeData;
183 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000184
185 // ByVal accessors
186 unsigned getByValAlignment() const {
187 assert(TheKind == ByVal && "Invalid kind!");
188 return UIntData;
189 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000190};
191
192/***/
193
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000194/* FIXME: All of this stuff should be part of the target interface
195 somehow. It is currently here because it is not clear how to factor
196 the targets to support this, since the Targets currently live in a
197 layer below types n'stuff.
198 */
199
200/// ABIInfo - Target specific hooks for defining how a type should be
201/// passed or returned from functions.
202class clang::ABIInfo {
203public:
204 virtual ~ABIInfo();
205
206 virtual ABIArgInfo classifyReturnType(QualType RetTy,
207 ASTContext &Context) const = 0;
208
209 virtual ABIArgInfo classifyArgumentType(QualType Ty,
210 ASTContext &Context) const = 0;
211};
212
213ABIInfo::~ABIInfo() {}
214
Daniel Dunbar834af452008-09-17 21:22:33 +0000215/// isEmptyStruct - Return true iff a structure has no non-empty
216/// members. Note that a structure with a flexible array member is not
217/// considered empty.
218static bool isEmptyStruct(QualType T) {
219 const RecordType *RT = T->getAsStructureType();
220 if (!RT)
221 return 0;
222 const RecordDecl *RD = RT->getDecl();
223 if (RD->hasFlexibleArrayMember())
224 return false;
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000225 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar834af452008-09-17 21:22:33 +0000226 e = RD->field_end(); i != e; ++i) {
227 const FieldDecl *FD = *i;
228 if (!isEmptyStruct(FD->getType()))
229 return false;
230 }
231 return true;
232}
233
234/// isSingleElementStruct - Determine if a structure is a "single
235/// element struct", i.e. it has exactly one non-empty field or
236/// exactly one field which is itself a single element
237/// struct. Structures with flexible array members are never
238/// considered single element structs.
239///
240/// \return The field declaration for the single non-empty field, if
241/// it exists.
242static const FieldDecl *isSingleElementStruct(QualType T) {
243 const RecordType *RT = T->getAsStructureType();
244 if (!RT)
245 return 0;
246
247 const RecordDecl *RD = RT->getDecl();
248 if (RD->hasFlexibleArrayMember())
249 return 0;
250
251 const FieldDecl *Found = 0;
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000252 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar834af452008-09-17 21:22:33 +0000253 e = RD->field_end(); i != e; ++i) {
254 const FieldDecl *FD = *i;
255 QualType FT = FD->getType();
256
257 if (isEmptyStruct(FT)) {
258 // Ignore
259 } else if (Found) {
260 return 0;
261 } else if (!CodeGenFunction::hasAggregateLLVMType(FT)) {
262 Found = FD;
263 } else {
264 Found = isSingleElementStruct(FT);
265 if (!Found)
266 return 0;
267 }
268 }
269
270 return Found;
271}
272
273static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
274 if (!Ty->getAsBuiltinType() && !Ty->isPointerType())
275 return false;
276
277 uint64_t Size = Context.getTypeSize(Ty);
278 return Size == 32 || Size == 64;
279}
280
281static bool areAllFields32Or64BitBasicType(const RecordDecl *RD,
282 ASTContext &Context) {
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000283 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar834af452008-09-17 21:22:33 +0000284 e = RD->field_end(); i != e; ++i) {
285 const FieldDecl *FD = *i;
286
287 if (!is32Or64BitBasicType(FD->getType(), Context))
288 return false;
289
290 // If this is a bit-field we need to make sure it is still a
291 // 32-bit or 64-bit type.
292 if (Expr *BW = FD->getBitWidth()) {
293 unsigned Width = BW->getIntegerConstantExprValue(Context).getZExtValue();
294 if (Width <= 16)
295 return false;
296 }
297 }
298 return true;
299}
300
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000301namespace {
302/// DefaultABIInfo - The default implementation for ABI specific
303/// details. This implementation provides information which results in
304/// sensible LLVM IR generation, but does not conform to any
305/// particular ABI.
306class DefaultABIInfo : public ABIInfo {
307 virtual ABIArgInfo classifyReturnType(QualType RetTy,
308 ASTContext &Context) const;
309
310 virtual ABIArgInfo classifyArgumentType(QualType RetTy,
311 ASTContext &Context) const;
312};
313
314/// X86_32ABIInfo - The X86-32 ABI information.
315class X86_32ABIInfo : public ABIInfo {
316public:
317 virtual ABIArgInfo classifyReturnType(QualType RetTy,
318 ASTContext &Context) const;
319
320 virtual ABIArgInfo classifyArgumentType(QualType RetTy,
321 ASTContext &Context) const;
322};
323}
324
325ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
326 ASTContext &Context) const {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000327 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar834af452008-09-17 21:22:33 +0000328 // Classify "single element" structs as their element type.
329 const FieldDecl *SeltFD = isSingleElementStruct(RetTy);
330 if (SeltFD) {
331 QualType SeltTy = SeltFD->getType()->getDesugaredType();
332 if (const BuiltinType *BT = SeltTy->getAsBuiltinType()) {
333 // FIXME: This is gross, it would be nice if we could just
334 // pass back SeltTy and have clients deal with it. Is it worth
335 // supporting coerce to both LLVM and clang Types?
336 if (BT->isIntegerType()) {
337 uint64_t Size = Context.getTypeSize(SeltTy);
338 return ABIArgInfo::getCoerce(llvm::IntegerType::get((unsigned) Size));
339 } else if (BT->getKind() == BuiltinType::Float) {
340 return ABIArgInfo::getCoerce(llvm::Type::FloatTy);
341 } else if (BT->getKind() == BuiltinType::Double) {
342 return ABIArgInfo::getCoerce(llvm::Type::DoubleTy);
343 }
344 } else if (SeltTy->isPointerType()) {
345 // FIXME: It would be really nice if this could come out as
346 // the proper pointer type.
347 llvm::Type *PtrTy =
348 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
349 return ABIArgInfo::getCoerce(PtrTy);
350 }
351 }
352
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000353 uint64_t Size = Context.getTypeSize(RetTy);
354 if (Size == 8) {
355 return ABIArgInfo::getCoerce(llvm::Type::Int8Ty);
356 } else if (Size == 16) {
357 return ABIArgInfo::getCoerce(llvm::Type::Int16Ty);
358 } else if (Size == 32) {
359 return ABIArgInfo::getCoerce(llvm::Type::Int32Ty);
360 } else if (Size == 64) {
361 return ABIArgInfo::getCoerce(llvm::Type::Int64Ty);
362 } else {
363 return ABIArgInfo::getStructRet();
364 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000365 } else {
366 return ABIArgInfo::getDefault();
367 }
368}
369
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000370ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
371 ASTContext &Context) const {
Daniel Dunbarf0357382008-09-17 20:11:04 +0000372 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar834af452008-09-17 21:22:33 +0000373 // Structures with flexible arrays are always byval.
374 if (const RecordType *RT = Ty->getAsStructureType())
375 if (RT->getDecl()->hasFlexibleArrayMember())
376 return ABIArgInfo::getByVal(0);
377
378 // Expand empty structs (i.e. ignore)
379 uint64_t Size = Context.getTypeSize(Ty);
380 if (Ty->isStructureType() && Size == 0)
381 return ABIArgInfo::getExpand();
382
383 // Expand structs with size <= 128-bits which consist only of
384 // basic types (int, long long, float, double, xxx*). This is
385 // non-recursive and does not ignore empty fields.
386 if (const RecordType *RT = Ty->getAsStructureType()) {
387 if (Context.getTypeSize(Ty) <= 4*32 &&
388 areAllFields32Or64BitBasicType(RT->getDecl(), Context))
389 return ABIArgInfo::getExpand();
390 }
391
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000392 return ABIArgInfo::getByVal(0);
393 } else {
394 return ABIArgInfo::getDefault();
395 }
396}
397
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000398namespace {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000399/// X86_64ABIInfo - The X86_64 ABI information.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000400class X86_64ABIInfo : public ABIInfo {
401 enum Class {
402 Integer = 0,
403 SSE,
404 SSEUp,
405 X87,
406 X87Up,
407 ComplexX87,
408 NoClass,
409 Memory
410 };
411
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000412 /// merge - Implement the X86_64 ABI merging algorithm.
413 ///
Daniel Dunbarc4503572009-01-31 00:06:58 +0000414 /// Merge an accumulating classification \arg Accum with a field
415 /// classification \arg Field.
416 ///
417 /// \param Accum - The accumulating classification. This should
418 /// always be either NoClass or the result of a previous merge
419 /// call. In addition, this should never be Memory (the caller
420 /// should just return Memory for the aggregate).
421 Class merge(Class Accum, Class Field) const;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000422
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000423 /// classify - Determine the x86_64 register classes in which the
424 /// given type T should be passed.
425 ///
Daniel Dunbarc4503572009-01-31 00:06:58 +0000426 /// \param Lo - The classification for the parts of the type
427 /// residing in the low word of the containing object.
428 ///
429 /// \param Hi - The classification for the parts of the type
430 /// residing in the high word of the containing object.
431 ///
432 /// \param OffsetBase - The bit offset of this type in the
Daniel Dunbarcdf920e2009-01-30 22:40:15 +0000433 /// containing object. Some parameters are classified different
434 /// depending on whether they straddle an eightbyte boundary.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000435 ///
436 /// If a word is unused its result will be NoClass; if a type should
437 /// be passed in Memory then at least the classification of \arg Lo
438 /// will be Memory.
439 ///
440 /// The \arg Lo class will be NoClass iff the argument is ignored.
441 ///
442 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
443 /// be NoClass.
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000444 void classify(QualType T, ASTContext &Context, uint64_t OffsetBase,
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000445 Class &Lo, Class &Hi) const;
Daniel Dunbarc4503572009-01-31 00:06:58 +0000446
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000447public:
448 virtual ABIArgInfo classifyReturnType(QualType RetTy,
449 ASTContext &Context) const;
450
451 virtual ABIArgInfo classifyArgumentType(QualType RetTy,
452 ASTContext &Context) const;
453};
454}
455
Daniel Dunbarc4503572009-01-31 00:06:58 +0000456X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum,
457 Class Field) const {
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000458 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
459 // classified recursively so that always two fields are
460 // considered. The resulting class is calculated according to
461 // the classes of the fields in the eightbyte:
462 //
463 // (a) If both classes are equal, this is the resulting class.
464 //
465 // (b) If one of the classes is NO_CLASS, the resulting class is
466 // the other class.
467 //
468 // (c) If one of the classes is MEMORY, the result is the MEMORY
469 // class.
470 //
471 // (d) If one of the classes is INTEGER, the result is the
472 // INTEGER.
473 //
474 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
475 // MEMORY is used as class.
476 //
477 // (f) Otherwise class SSE is used.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000478 assert((Accum == NoClass || Accum == Integer ||
479 Accum == SSE || Accum == SSEUp) &&
480 "Invalid accumulated classification during merge.");
481 if (Accum == Field || Field == NoClass)
482 return Accum;
483 else if (Field == Memory)
484 return Memory;
485 else if (Accum == NoClass)
486 return Field;
487 else if (Accum == Integer || Field == Integer)
488 return Integer;
489 else if (Field == X87 || Field == X87Up || Field == ComplexX87)
490 return Memory;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000491 else
Daniel Dunbarc4503572009-01-31 00:06:58 +0000492 return SSE;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000493}
494
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000495void X86_64ABIInfo::classify(QualType Ty,
496 ASTContext &Context,
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000497 uint64_t OffsetBase,
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000498 Class &Lo, Class &Hi) const {
Daniel Dunbar9a82b522009-02-02 18:06:39 +0000499 // FIXME: This code can be simplified by introducing a simple value
500 // class for Class pairs with appropriate constructor methods for
501 // the various situations.
502
Daniel Dunbarc4503572009-01-31 00:06:58 +0000503 Lo = Hi = NoClass;
504
505 Class &Current = OffsetBase < 64 ? Lo : Hi;
506 Current = Memory;
507
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000508 if (const BuiltinType *BT = Ty->getAsBuiltinType()) {
509 BuiltinType::Kind k = BT->getKind();
510
Daniel Dunbar11434922009-01-26 21:26:08 +0000511 if (k == BuiltinType::Void) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000512 Current = NoClass;
Daniel Dunbar11434922009-01-26 21:26:08 +0000513 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000514 Current = Integer;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000515 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000516 Current = SSE;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000517 } else if (k == BuiltinType::LongDouble) {
518 Lo = X87;
519 Hi = X87Up;
520 }
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000521 // FIXME: _Decimal32 and _Decimal64 are SSE.
522 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000523 // FIXME: __int128 is (Integer, Integer).
524 } else if (Ty->isPointerLikeType() || Ty->isBlockPointerType() ||
525 Ty->isObjCQualifiedInterfaceType()) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000526 Current = Integer;
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000527 } else if (const VectorType *VT = Ty->getAsVectorType()) {
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000528 uint64_t Size = Context.getTypeSize(VT);
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000529 if (Size == 64) {
Daniel Dunbard4cd1b02009-01-30 19:38:39 +0000530 // gcc passes <1 x double> in memory.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000531 if (VT->getElementType() == Context.DoubleTy)
Daniel Dunbard4cd1b02009-01-30 19:38:39 +0000532 return;
Daniel Dunbard4cd1b02009-01-30 19:38:39 +0000533
Daniel Dunbarc4503572009-01-31 00:06:58 +0000534 Current = SSE;
Daniel Dunbare33edf12009-01-30 18:40:10 +0000535
536 // If this type crosses an eightbyte boundary, it should be
537 // split.
Daniel Dunbarcdf920e2009-01-30 22:40:15 +0000538 if (OffsetBase && OffsetBase != 64)
Daniel Dunbare33edf12009-01-30 18:40:10 +0000539 Hi = Lo;
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000540 } else if (Size == 128) {
541 Lo = SSE;
542 Hi = SSEUp;
543 }
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000544 } else if (const ComplexType *CT = Ty->getAsComplexType()) {
545 QualType ET = CT->getElementType();
546
Daniel Dunbare33edf12009-01-30 18:40:10 +0000547 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbareac48dc2009-01-29 07:22:20 +0000548 if (ET->isIntegerType()) {
Daniel Dunbareac48dc2009-01-29 07:22:20 +0000549 if (Size <= 64)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000550 Current = Integer;
Daniel Dunbareac48dc2009-01-29 07:22:20 +0000551 else if (Size <= 128)
552 Lo = Hi = Integer;
553 } else if (ET == Context.FloatTy)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000554 Current = SSE;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000555 else if (ET == Context.DoubleTy)
556 Lo = Hi = SSE;
557 else if (ET == Context.LongDoubleTy)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000558 Current = ComplexX87;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000559
560 // If this complex type crosses an eightbyte boundary then it
561 // should be split.
Daniel Dunbarcdf920e2009-01-30 22:40:15 +0000562 uint64_t EB_Real = (OffsetBase) / 64;
563 uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000564 if (Hi == NoClass && EB_Real != EB_Imag)
565 Hi = Lo;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000566 } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
567 // Arrays are treated like structures.
568
569 uint64_t Size = Context.getTypeSize(Ty);
570
571 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
572 // than two eightbytes, ..., it has class MEMORY.
573 if (Size > 128)
574 return;
575
576 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
577 // fields, it has class MEMORY.
578 //
579 // Only need to check alignment of array base.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000580 if (OffsetBase % Context.getTypeAlign(AT->getElementType()))
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000581 return;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000582
583 // Otherwise implement simplified merge. We could be smarter about
584 // this, but it isn't worth it and would be harder to verify.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000585 Current = NoClass;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000586 uint64_t EltSize = Context.getTypeSize(AT->getElementType());
587 uint64_t ArraySize = AT->getSize().getZExtValue();
588 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
589 Class FieldLo, FieldHi;
590 classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbarc4503572009-01-31 00:06:58 +0000591 Lo = merge(Lo, FieldLo);
592 Hi = merge(Hi, FieldHi);
593 if (Lo == Memory || Hi == Memory)
594 break;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000595 }
Daniel Dunbarc4503572009-01-31 00:06:58 +0000596
597 // Do post merger cleanup (see below). Only case we worry about is Memory.
598 if (Hi == Memory)
599 Lo = Memory;
600 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Daniel Dunbar99037e52009-01-29 08:13:58 +0000601 } else if (const RecordType *RT = Ty->getAsRecordType()) {
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000602 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbar99037e52009-01-29 08:13:58 +0000603
604 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
605 // than two eightbytes, ..., it has class MEMORY.
606 if (Size > 128)
607 return;
608
609 const RecordDecl *RD = RT->getDecl();
610
611 // Assume variable sized types are passed in memory.
612 if (RD->hasFlexibleArrayMember())
613 return;
614
615 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
616
617 // Reset Lo class, this will be recomputed.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000618 Current = NoClass;
Daniel Dunbar99037e52009-01-29 08:13:58 +0000619 unsigned idx = 0;
620 for (RecordDecl::field_iterator i = RD->field_begin(),
621 e = RD->field_end(); i != e; ++i, ++idx) {
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000622 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Daniel Dunbar99037e52009-01-29 08:13:58 +0000623
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000624 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
625 // fields, it has class MEMORY.
Daniel Dunbar99037e52009-01-29 08:13:58 +0000626 if (Offset % Context.getTypeAlign(i->getType())) {
627 Lo = Memory;
628 return;
629 }
630
Daniel Dunbar99037e52009-01-29 08:13:58 +0000631 // Classify this field.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000632 //
633 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
634 // exceeds a single eightbyte, each is classified
635 // separately. Each eightbyte gets initialized to class
636 // NO_CLASS.
Daniel Dunbar99037e52009-01-29 08:13:58 +0000637 Class FieldLo, FieldHi;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000638 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbarc4503572009-01-31 00:06:58 +0000639 Lo = merge(Lo, FieldLo);
640 Hi = merge(Hi, FieldHi);
641 if (Lo == Memory || Hi == Memory)
642 break;
Daniel Dunbar99037e52009-01-29 08:13:58 +0000643 }
644
645 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
646 //
647 // (a) If one of the classes is MEMORY, the whole argument is
648 // passed in memory.
649 //
650 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
651
652 // The first of these conditions is guaranteed by how we implement
Daniel Dunbarc4503572009-01-31 00:06:58 +0000653 // the merge (just bail).
654 //
655 // The second condition occurs in the case of unions; for example
656 // union { _Complex double; unsigned; }.
657 if (Hi == Memory)
658 Lo = Memory;
Daniel Dunbar99037e52009-01-29 08:13:58 +0000659 if (Hi == SSEUp && Lo != SSE)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000660 Hi = SSE;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000661 }
662}
663
Daniel Dunbarc4503572009-01-31 00:06:58 +0000664
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000665ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
666 ASTContext &Context) const {
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000667 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
668 // classification algorithm.
669 X86_64ABIInfo::Class Lo, Hi;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000670 classify(RetTy, Context, 0, Lo, Hi);
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000671
Daniel Dunbarc4503572009-01-31 00:06:58 +0000672 // Check some invariants.
673 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
674 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
675 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
676
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000677 const llvm::Type *ResType = 0;
678 switch (Lo) {
679 case NoClass:
Daniel Dunbar11434922009-01-26 21:26:08 +0000680 return ABIArgInfo::getIgnore();
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000681
682 case SSEUp:
683 case X87Up:
684 assert(0 && "Invalid classification for lo word.");
685
Daniel Dunbarc4503572009-01-31 00:06:58 +0000686 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
687 // hidden argument, i.e. structret.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000688 case Memory:
689 return ABIArgInfo::getStructRet();
690
691 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
692 // available register of the sequence %rax, %rdx is used.
693 case Integer:
694 ResType = llvm::Type::Int64Ty; break;
695
696 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
697 // available SSE register of the sequence %xmm0, %xmm1 is used.
698 case SSE:
699 ResType = llvm::Type::DoubleTy; break;
700
701 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
702 // returned on the X87 stack in %st0 as 80-bit x87 number.
703 case X87:
704 ResType = llvm::Type::X86_FP80Ty; break;
705
Daniel Dunbarc4503572009-01-31 00:06:58 +0000706 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
707 // part of the value is returned in %st0 and the imaginary part in
708 // %st1.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000709 case ComplexX87:
710 assert(Hi == NoClass && "Unexpected ComplexX87 classification.");
711 ResType = llvm::VectorType::get(llvm::Type::X86_FP80Ty, 2);
712 break;
713 }
714
715 switch (Hi) {
716 // Memory was handled previously, and ComplexX87 and X87 should
717 // never occur as hi classes.
718 case Memory:
719 case X87:
720 case ComplexX87:
721 assert(0 && "Invalid classification for hi word.");
722
723 case NoClass: break;
724 case Integer:
Daniel Dunbarb0e14f22009-01-29 07:36:07 +0000725 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
726 break;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000727 case SSE:
Daniel Dunbarb0e14f22009-01-29 07:36:07 +0000728 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
729 break;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000730
731 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
732 // is passed in the upper half of the last used SSE register.
733 //
734 // SSEUP should always be preceeded by SSE, just widen.
735 case SSEUp:
736 assert(Lo == SSE && "Unexpected SSEUp classification.");
737 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
738 break;
739
740 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
Daniel Dunbarb0e14f22009-01-29 07:36:07 +0000741 // returned together with the previous X87 value in %st0.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000742 //
743 // X87UP should always be preceeded by X87, so we don't need to do
744 // anything here.
745 case X87Up:
746 assert(Lo == X87 && "Unexpected X87Up classification.");
747 break;
748 }
749
750 return ABIArgInfo::getCoerce(ResType);
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000751}
752
753ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty,
754 ASTContext &Context) const {
755 return ABIArgInfo::getDefault();
756}
757
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000758ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
759 ASTContext &Context) const {
760 return ABIArgInfo::getDefault();
761}
762
763ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
764 ASTContext &Context) const {
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000765 return ABIArgInfo::getDefault();
766}
767
768const ABIInfo &CodeGenTypes::getABIInfo() const {
769 if (TheABIInfo)
770 return *TheABIInfo;
771
772 // For now we just cache this in the CodeGenTypes and don't bother
773 // to free it.
774 const char *TargetPrefix = getContext().Target.getTargetPrefix();
775 if (strcmp(TargetPrefix, "x86") == 0) {
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000776 switch (getContext().Target.getPointerWidth(0)) {
777 case 32:
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000778 return *(TheABIInfo = new X86_32ABIInfo());
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000779 case 64:
Daniel Dunbar11a76ed2009-01-30 18:47:53 +0000780 return *(TheABIInfo = new X86_64ABIInfo());
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000781 }
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000782 }
783
784 return *(TheABIInfo = new DefaultABIInfo);
785}
786
787// getABIReturnInfo - Wrap the ABIInfo getABIReturnInfo, altering
788// "default" types to StructRet when appropriate for simplicity.
789static ABIArgInfo getABIReturnInfo(QualType Ty, CodeGenTypes &CGT) {
790 assert(!Ty->isArrayType() &&
791 "Array types cannot be passed directly.");
792 ABIArgInfo Info = CGT.getABIInfo().classifyReturnType(Ty, CGT.getContext());
Daniel Dunbarf0357382008-09-17 20:11:04 +0000793 // Ensure default on aggregate types is StructRet.
794 if (Info.isDefault() && CodeGenFunction::hasAggregateLLVMType(Ty))
795 return ABIArgInfo::getStructRet();
796 return Info;
797}
798
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000799// getABIArgumentInfo - Wrap the ABIInfo getABIReturnInfo, altering
800// "default" types to ByVal when appropriate for simplicity.
801static ABIArgInfo getABIArgumentInfo(QualType Ty, CodeGenTypes &CGT) {
802 assert(!Ty->isArrayType() &&
803 "Array types cannot be passed directly.");
804 ABIArgInfo Info = CGT.getABIInfo().classifyArgumentType(Ty, CGT.getContext());
Daniel Dunbarf0357382008-09-17 20:11:04 +0000805 // Ensure default on aggregate types is ByVal.
806 if (Info.isDefault() && CodeGenFunction::hasAggregateLLVMType(Ty))
807 return ABIArgInfo::getByVal(0);
808 return Info;
809}
810
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000811/***/
812
Daniel Dunbar56273772008-09-17 00:51:38 +0000813void CodeGenTypes::GetExpandedTypes(QualType Ty,
814 std::vector<const llvm::Type*> &ArgTys) {
815 const RecordType *RT = Ty->getAsStructureType();
816 assert(RT && "Can only expand structure types.");
817 const RecordDecl *RD = RT->getDecl();
818 assert(!RD->hasFlexibleArrayMember() &&
819 "Cannot expand structure with flexible array.");
820
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000821 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar56273772008-09-17 00:51:38 +0000822 e = RD->field_end(); i != e; ++i) {
823 const FieldDecl *FD = *i;
824 assert(!FD->isBitField() &&
825 "Cannot expand structure with bit-field members.");
826
827 QualType FT = FD->getType();
828 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
829 GetExpandedTypes(FT, ArgTys);
830 } else {
831 ArgTys.push_back(ConvertType(FT));
832 }
833 }
834}
835
836llvm::Function::arg_iterator
837CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
838 llvm::Function::arg_iterator AI) {
839 const RecordType *RT = Ty->getAsStructureType();
840 assert(RT && "Can only expand structure types.");
841
842 RecordDecl *RD = RT->getDecl();
843 assert(LV.isSimple() &&
844 "Unexpected non-simple lvalue during struct expansion.");
845 llvm::Value *Addr = LV.getAddress();
846 for (RecordDecl::field_iterator i = RD->field_begin(),
847 e = RD->field_end(); i != e; ++i) {
848 FieldDecl *FD = *i;
849 QualType FT = FD->getType();
850
851 // FIXME: What are the right qualifiers here?
852 LValue LV = EmitLValueForField(Addr, FD, false, 0);
853 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
854 AI = ExpandTypeFromArgs(FT, LV, AI);
855 } else {
856 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
857 ++AI;
858 }
859 }
860
861 return AI;
862}
863
864void
865CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
866 llvm::SmallVector<llvm::Value*, 16> &Args) {
867 const RecordType *RT = Ty->getAsStructureType();
868 assert(RT && "Can only expand structure types.");
869
870 RecordDecl *RD = RT->getDecl();
871 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
872 llvm::Value *Addr = RV.getAggregateAddr();
873 for (RecordDecl::field_iterator i = RD->field_begin(),
874 e = RD->field_end(); i != e; ++i) {
875 FieldDecl *FD = *i;
876 QualType FT = FD->getType();
877
878 // FIXME: What are the right qualifiers here?
879 LValue LV = EmitLValueForField(Addr, FD, false, 0);
880 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
881 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
882 } else {
883 RValue RV = EmitLoadOfLValue(LV, FT);
884 assert(RV.isScalar() &&
885 "Unexpected non-scalar rvalue during struct expansion.");
886 Args.push_back(RV.getScalarVal());
887 }
888 }
889}
890
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000891/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
892/// a pointer to an object of type \arg Ty.
893///
894/// This safely handles the case when the src type is smaller than the
895/// destination type; in this situation the values of bits which not
896/// present in the src are undefined.
897static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
898 const llvm::Type *Ty,
899 CodeGenFunction &CGF) {
900 const llvm::Type *SrcTy =
901 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
902 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
903 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(Ty);
904
905 // If load is legal, just bitcase the src pointer.
906 if (SrcSize == DstSize) {
907 llvm::Value *Casted =
908 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
909 return CGF.Builder.CreateLoad(Casted);
910 } else {
911 assert(SrcSize < DstSize && "Coercion is losing source bits!");
912
913 // Otherwise do coercion through memory. This is stupid, but
914 // simple.
915 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
916 llvm::Value *Casted =
917 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
918 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
919 return CGF.Builder.CreateLoad(Tmp);
920 }
921}
922
923/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
924/// where the source and destination may have different types.
925///
926/// This safely handles the case when the src type is larger than the
927/// destination type; the upper bits of the src will be lost.
928static void CreateCoercedStore(llvm::Value *Src,
929 llvm::Value *DstPtr,
930 CodeGenFunction &CGF) {
931 const llvm::Type *SrcTy = Src->getType();
932 const llvm::Type *DstTy =
933 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
934
935 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
936 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(DstTy);
937
938 // If store is legal, just bitcase the src pointer.
939 if (SrcSize == DstSize) {
940 llvm::Value *Casted =
941 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
942 CGF.Builder.CreateStore(Src, Casted);
943 } else {
944 assert(SrcSize > DstSize && "Coercion is missing bits!");
945
946 // Otherwise do coercion through memory. This is stupid, but
947 // simple.
948 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
949 CGF.Builder.CreateStore(Src, Tmp);
950 llvm::Value *Casted =
951 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
952 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(Casted), DstPtr);
953 }
954}
955
Daniel Dunbar56273772008-09-17 00:51:38 +0000956/***/
957
Daniel Dunbar88b53962009-02-02 22:03:45 +0000958bool CodeGenModule::ReturnTypeUsesSret(const CGFunctionInfo &FI) {
959 return getABIReturnInfo(FI.getReturnType(), getTypes()).isStructRet();
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000960}
961
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000962const llvm::FunctionType *
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000963CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic) {
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000964 std::vector<const llvm::Type*> ArgTys;
965
966 const llvm::Type *ResultType = 0;
967
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000968 QualType RetTy = FI.getReturnType();
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000969 ABIArgInfo RetAI = getABIReturnInfo(RetTy, *this);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000970 switch (RetAI.getKind()) {
971 case ABIArgInfo::ByVal:
972 case ABIArgInfo::Expand:
973 assert(0 && "Invalid ABI kind for return argument");
974
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000975 case ABIArgInfo::Default:
976 if (RetTy->isVoidType()) {
977 ResultType = llvm::Type::VoidTy;
978 } else {
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000979 ResultType = ConvertType(RetTy);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000980 }
981 break;
982
983 case ABIArgInfo::StructRet: {
984 ResultType = llvm::Type::VoidTy;
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000985 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000986 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
987 break;
988 }
989
Daniel Dunbar11434922009-01-26 21:26:08 +0000990 case ABIArgInfo::Ignore:
991 ResultType = llvm::Type::VoidTy;
992 break;
993
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000994 case ABIArgInfo::Coerce:
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000995 ResultType = RetAI.getCoerceToType();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000996 break;
997 }
998
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000999 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1000 it != ie; ++it) {
1001 ABIArgInfo AI = getABIArgumentInfo(*it, *this);
1002 const llvm::Type *Ty = ConvertType(*it);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001003
1004 switch (AI.getKind()) {
Daniel Dunbar11434922009-01-26 21:26:08 +00001005 case ABIArgInfo::Ignore:
1006 break;
1007
Daniel Dunbar56273772008-09-17 00:51:38 +00001008 case ABIArgInfo::Coerce:
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001009 case ABIArgInfo::StructRet:
1010 assert(0 && "Invalid ABI kind for non-return argument");
1011
1012 case ABIArgInfo::ByVal:
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001013 // byval arguments are always on the stack, which is addr space #0.
1014 ArgTys.push_back(llvm::PointerType::getUnqual(Ty));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001015 assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled");
1016 break;
1017
1018 case ABIArgInfo::Default:
1019 ArgTys.push_back(Ty);
1020 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001021
1022 case ABIArgInfo::Expand:
Daniel Dunbara0a99e02009-02-02 23:43:58 +00001023 GetExpandedTypes(*it, ArgTys);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001024 break;
1025 }
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001026 }
1027
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001028 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar3913f182008-09-09 23:48:28 +00001029}
1030
Daniel Dunbara0a99e02009-02-02 23:43:58 +00001031void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
Daniel Dunbar88b53962009-02-02 22:03:45 +00001032 const Decl *TargetDecl,
Devang Patel761d7f72008-09-25 21:02:23 +00001033 AttributeListType &PAL) {
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001034 unsigned FuncAttrs = 0;
Devang Patela2c69122008-09-26 22:53:57 +00001035 unsigned RetAttrs = 0;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001036
1037 if (TargetDecl) {
1038 if (TargetDecl->getAttr<NoThrowAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +00001039 FuncAttrs |= llvm::Attribute::NoUnwind;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001040 if (TargetDecl->getAttr<NoReturnAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +00001041 FuncAttrs |= llvm::Attribute::NoReturn;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001042 if (TargetDecl->getAttr<PureAttr>())
1043 FuncAttrs |= llvm::Attribute::ReadOnly;
1044 if (TargetDecl->getAttr<ConstAttr>())
1045 FuncAttrs |= llvm::Attribute::ReadNone;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001046 }
1047
Daniel Dunbara0a99e02009-02-02 23:43:58 +00001048 QualType RetTy = FI.getReturnType();
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001049 unsigned Index = 1;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001050 ABIArgInfo RetAI = getABIReturnInfo(RetTy, getTypes());
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001051 switch (RetAI.getKind()) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001052 case ABIArgInfo::Default:
1053 if (RetTy->isPromotableIntegerType()) {
1054 if (RetTy->isSignedIntegerType()) {
Devang Patela2c69122008-09-26 22:53:57 +00001055 RetAttrs |= llvm::Attribute::SExt;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001056 } else if (RetTy->isUnsignedIntegerType()) {
Devang Patela2c69122008-09-26 22:53:57 +00001057 RetAttrs |= llvm::Attribute::ZExt;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001058 }
1059 }
1060 break;
1061
1062 case ABIArgInfo::StructRet:
Devang Patel761d7f72008-09-25 21:02:23 +00001063 PAL.push_back(llvm::AttributeWithIndex::get(Index,
Daniel Dunbar725ad312009-01-31 02:19:00 +00001064 llvm::Attribute::StructRet |
1065 llvm::Attribute::NoAlias));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001066 ++Index;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001067 break;
1068
Daniel Dunbar11434922009-01-26 21:26:08 +00001069 case ABIArgInfo::Ignore:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001070 case ABIArgInfo::Coerce:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001071 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001072
1073 case ABIArgInfo::ByVal:
1074 case ABIArgInfo::Expand:
1075 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001076 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001077
Devang Patela2c69122008-09-26 22:53:57 +00001078 if (RetAttrs)
1079 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Daniel Dunbara0a99e02009-02-02 23:43:58 +00001080 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1081 it != ie; ++it) {
1082 QualType ParamType = *it;
Devang Patel761d7f72008-09-25 21:02:23 +00001083 unsigned Attributes = 0;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001084 ABIArgInfo AI = getABIArgumentInfo(ParamType, getTypes());
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001085
1086 switch (AI.getKind()) {
1087 case ABIArgInfo::StructRet:
Daniel Dunbar56273772008-09-17 00:51:38 +00001088 case ABIArgInfo::Coerce:
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001089 assert(0 && "Invalid ABI kind for non-return argument");
1090
1091 case ABIArgInfo::ByVal:
Devang Patel761d7f72008-09-25 21:02:23 +00001092 Attributes |= llvm::Attribute::ByVal;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001093 assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled");
1094 break;
1095
1096 case ABIArgInfo::Default:
1097 if (ParamType->isPromotableIntegerType()) {
1098 if (ParamType->isSignedIntegerType()) {
Devang Patel761d7f72008-09-25 21:02:23 +00001099 Attributes |= llvm::Attribute::SExt;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001100 } else if (ParamType->isUnsignedIntegerType()) {
Devang Patel761d7f72008-09-25 21:02:23 +00001101 Attributes |= llvm::Attribute::ZExt;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001102 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001103 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001104 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001105
Daniel Dunbar11434922009-01-26 21:26:08 +00001106 case ABIArgInfo::Ignore:
1107 // Skip increment, no matching LLVM parameter.
1108 continue;
1109
Daniel Dunbar56273772008-09-17 00:51:38 +00001110 case ABIArgInfo::Expand: {
1111 std::vector<const llvm::Type*> Tys;
1112 // FIXME: This is rather inefficient. Do we ever actually need
1113 // to do anything here? The result should be just reconstructed
1114 // on the other side, so extension should be a non-issue.
1115 getTypes().GetExpandedTypes(ParamType, Tys);
1116 Index += Tys.size();
1117 continue;
1118 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001119 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001120
Devang Patel761d7f72008-09-25 21:02:23 +00001121 if (Attributes)
1122 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar56273772008-09-17 00:51:38 +00001123 ++Index;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001124 }
Devang Patela2c69122008-09-26 22:53:57 +00001125 if (FuncAttrs)
1126 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
1127
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001128}
1129
Daniel Dunbar88b53962009-02-02 22:03:45 +00001130void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
1131 llvm::Function *Fn,
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001132 const FunctionArgList &Args) {
1133 // Emit allocs for param decls. Give the LLVM Argument nodes names.
1134 llvm::Function::arg_iterator AI = Fn->arg_begin();
1135
1136 // Name the struct return argument.
Daniel Dunbar88b53962009-02-02 22:03:45 +00001137 if (CGM.ReturnTypeUsesSret(FI)) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001138 AI->setName("agg.result");
1139 ++AI;
1140 }
1141
1142 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbar56273772008-09-17 00:51:38 +00001143 i != e; ++i) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001144 const VarDecl *Arg = i->first;
Daniel Dunbar56273772008-09-17 00:51:38 +00001145 QualType Ty = i->second;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001146 ABIArgInfo ArgI = getABIArgumentInfo(Ty, CGM.getTypes());
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001147
1148 switch (ArgI.getKind()) {
1149 case ABIArgInfo::ByVal:
1150 case ABIArgInfo::Default: {
1151 assert(AI != Fn->arg_end() && "Argument mismatch!");
1152 llvm::Value* V = AI;
Daniel Dunbar56273772008-09-17 00:51:38 +00001153 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001154 // This must be a promotion, for something like
1155 // "void a(x) short x; {..."
Daniel Dunbar56273772008-09-17 00:51:38 +00001156 V = EmitScalarConversion(V, Ty, Arg->getType());
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001157 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001158 EmitParmDecl(*Arg, V);
1159 break;
1160 }
Daniel Dunbar56273772008-09-17 00:51:38 +00001161
1162 case ABIArgInfo::Expand: {
1163 // If this was structure was expand into multiple arguments then
1164 // we need to create a temporary and reconstruct it from the
1165 // arguments.
Chris Lattner39f34e92008-11-24 04:00:27 +00001166 std::string Name = Arg->getNameAsString();
Daniel Dunbar56273772008-09-17 00:51:38 +00001167 llvm::Value *Temp = CreateTempAlloca(ConvertType(Ty),
1168 (Name + ".addr").c_str());
1169 // FIXME: What are the right qualifiers here?
1170 llvm::Function::arg_iterator End =
1171 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI);
1172 EmitParmDecl(*Arg, Temp);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001173
Daniel Dunbar56273772008-09-17 00:51:38 +00001174 // Name the arguments used in expansion and increment AI.
1175 unsigned Index = 0;
1176 for (; AI != End; ++AI, ++Index)
1177 AI->setName(Name + "." + llvm::utostr(Index));
1178 continue;
1179 }
Daniel Dunbar11434922009-01-26 21:26:08 +00001180
1181 case ABIArgInfo::Ignore:
1182 break;
1183
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001184 case ABIArgInfo::Coerce:
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001185 case ABIArgInfo::StructRet:
1186 assert(0 && "Invalid ABI kind for non-return argument");
1187 }
Daniel Dunbar56273772008-09-17 00:51:38 +00001188
1189 ++AI;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001190 }
1191 assert(AI == Fn->arg_end() && "Argument mismatch!");
1192}
1193
Daniel Dunbar88b53962009-02-02 22:03:45 +00001194void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI,
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001195 llvm::Value *ReturnValue) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001196 llvm::Value *RV = 0;
1197
1198 // Functions with no result always return void.
1199 if (ReturnValue) {
Daniel Dunbar88b53962009-02-02 22:03:45 +00001200 QualType RetTy = FI.getReturnType();
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001201 ABIArgInfo RetAI = getABIReturnInfo(RetTy, CGM.getTypes());
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001202
1203 switch (RetAI.getKind()) {
1204 case ABIArgInfo::StructRet:
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001205 if (RetTy->isAnyComplexType()) {
1206 // FIXME: Volatile
1207 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
1208 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
1209 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1210 EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy);
1211 } else {
1212 Builder.CreateStore(Builder.CreateLoad(ReturnValue),
1213 CurFn->arg_begin());
1214 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001215 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001216
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001217 case ABIArgInfo::Default:
1218 RV = Builder.CreateLoad(ReturnValue);
1219 break;
1220
Daniel Dunbar11434922009-01-26 21:26:08 +00001221 case ABIArgInfo::Ignore:
1222 break;
1223
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001224 case ABIArgInfo::Coerce: {
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +00001225 RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001226 break;
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001227 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001228
1229 case ABIArgInfo::ByVal:
1230 case ABIArgInfo::Expand:
1231 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001232 }
1233 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001234
1235 if (RV) {
1236 Builder.CreateRet(RV);
1237 } else {
1238 Builder.CreateRetVoid();
1239 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001240}
1241
Daniel Dunbar88b53962009-02-02 22:03:45 +00001242RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
1243 llvm::Value *Callee,
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001244 const CallArgList &CallArgs) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001245 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001246
1247 // Handle struct-return functions by passing a pointer to the
1248 // location that we would like to return into.
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001249 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001250 ABIArgInfo RetAI = getABIReturnInfo(RetTy, CGM.getTypes());
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001251 switch (RetAI.getKind()) {
1252 case ABIArgInfo::StructRet:
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001253 // Create a temporary alloca to hold the result of the call. :(
Daniel Dunbar56273772008-09-17 00:51:38 +00001254 Args.push_back(CreateTempAlloca(ConvertType(RetTy)));
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001255 break;
1256
1257 case ABIArgInfo::Default:
Daniel Dunbar11434922009-01-26 21:26:08 +00001258 case ABIArgInfo::Ignore:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001259 case ABIArgInfo::Coerce:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001260 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001261
1262 case ABIArgInfo::ByVal:
1263 case ABIArgInfo::Expand:
1264 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001265 }
1266
1267 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
1268 I != E; ++I) {
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001269 ABIArgInfo ArgInfo = getABIArgumentInfo(I->second, CGM.getTypes());
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001270 RValue RV = I->first;
Daniel Dunbar56273772008-09-17 00:51:38 +00001271
1272 switch (ArgInfo.getKind()) {
1273 case ABIArgInfo::ByVal: // Default is byval
1274 case ABIArgInfo::Default:
1275 if (RV.isScalar()) {
1276 Args.push_back(RV.getScalarVal());
1277 } else if (RV.isComplex()) {
1278 // Make a temporary alloca to pass the argument.
1279 Args.push_back(CreateTempAlloca(ConvertType(I->second)));
1280 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
1281 } else {
1282 Args.push_back(RV.getAggregateAddr());
1283 }
1284 break;
1285
Daniel Dunbar11434922009-01-26 21:26:08 +00001286 case ABIArgInfo::Ignore:
1287 break;
1288
Daniel Dunbar56273772008-09-17 00:51:38 +00001289 case ABIArgInfo::StructRet:
1290 case ABIArgInfo::Coerce:
1291 assert(0 && "Invalid ABI kind for non-return argument");
1292 break;
1293
1294 case ABIArgInfo::Expand:
1295 ExpandTypeToArgs(I->second, RV, Args);
1296 break;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001297 }
1298 }
1299
1300 llvm::CallInst *CI = Builder.CreateCall(Callee,&Args[0],&Args[0]+Args.size());
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001301
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001302 // FIXME: Provide TargetDecl so nounwind, noreturn, etc, etc get set.
Devang Patel761d7f72008-09-25 21:02:23 +00001303 CodeGen::AttributeListType AttributeList;
Daniel Dunbar88b53962009-02-02 22:03:45 +00001304 CGM.ConstructAttributeList(CallInfo, 0, AttributeList);
Devang Patel761d7f72008-09-25 21:02:23 +00001305 CI->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(),
Daniel Dunbar725ad312009-01-31 02:19:00 +00001306 AttributeList.size()));
1307
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001308 if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee))
1309 CI->setCallingConv(F->getCallingConv());
1310 if (CI->getType() != llvm::Type::VoidTy)
1311 CI->setName("call");
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001312
1313 switch (RetAI.getKind()) {
1314 case ABIArgInfo::StructRet:
1315 if (RetTy->isAnyComplexType())
Daniel Dunbar56273772008-09-17 00:51:38 +00001316 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001317 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar56273772008-09-17 00:51:38 +00001318 return RValue::getAggregate(Args[0]);
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001319 else
1320 return RValue::get(Builder.CreateLoad(Args[0]));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001321
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001322 case ABIArgInfo::Default:
1323 return RValue::get(RetTy->isVoidType() ? 0 : CI);
1324
Daniel Dunbar11434922009-01-26 21:26:08 +00001325 case ABIArgInfo::Ignore:
Daniel Dunbarcc039fe2009-01-29 08:24:57 +00001326 if (RetTy->isVoidType())
1327 return RValue::get(0);
1328 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1329 llvm::Value *Res =
1330 llvm::UndefValue::get(llvm::PointerType::getUnqual(ConvertType(RetTy)));
1331 return RValue::getAggregate(Res);
1332 }
1333 return RValue::get(llvm::UndefValue::get(ConvertType(RetTy)));
Daniel Dunbar11434922009-01-26 21:26:08 +00001334
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001335 case ABIArgInfo::Coerce: {
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +00001336 llvm::Value *V = CreateTempAlloca(ConvertType(RetTy), "coerce");
1337 CreateCoercedStore(CI, V, *this);
Anders Carlssonad3d6912008-11-25 22:21:48 +00001338 if (RetTy->isAnyComplexType())
1339 return RValue::getComplex(LoadComplexFromAddr(V, false));
Daniel Dunbar11434922009-01-26 21:26:08 +00001340 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssonad3d6912008-11-25 22:21:48 +00001341 return RValue::getAggregate(V);
Daniel Dunbar11434922009-01-26 21:26:08 +00001342 else
1343 return RValue::get(Builder.CreateLoad(V));
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001344 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001345
1346 case ABIArgInfo::ByVal:
1347 case ABIArgInfo::Expand:
1348 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001349 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001350
1351 assert(0 && "Unhandled ABIArgInfo::Kind");
1352 return RValue::get(0);
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001353}