blob: 30257d2699e472d64fdc2296ddf947e956026175 [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) {
Daniel Dunbar40a6be62009-02-03 00:07:12 +000089 // Lookup or create unique function info.
90 llvm::FoldingSetNodeID ID;
91 CGFunctionInfo::Profile(ID, ResTy, ArgTys.begin(), ArgTys.end());
92
93 void *InsertPos = 0;
94 CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, InsertPos);
95 if (FI)
96 return *FI;
97
98 FI = new CGFunctionInfo(ResTy, ArgTys);
99 FunctionInfos.InsertNode(FI, InsertPos);
100 return *FI;
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000101}
102
103/***/
104
105CGFunctionInfo::CGFunctionInfo(QualType ResTy,
106 const llvm::SmallVector<QualType, 16> &ArgTys) {
107 ArgTypes.push_back(ResTy);
108 ArgTypes.insert(ArgTypes.end(), ArgTys.begin(), ArgTys.end());
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000109}
110
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000111CGFunctionInfo::arg_iterator CGFunctionInfo::arg_begin() const {
112 return ArgTypes.begin()+1;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000113}
114
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000115CGFunctionInfo::arg_iterator CGFunctionInfo::arg_end() const {
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000116 return ArgTypes.end();
Daniel Dunbar0dbe2272008-09-08 21:33:45 +0000117}
118
119/***/
120
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000121/// ABIArgInfo - Helper class to encapsulate information about how a
122/// specific C type should be passed to or returned from a function.
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000123class ABIArgInfo {
124public:
125 enum Kind {
126 Default,
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +0000127 StructRet, /// Only valid for return values. The return value
128 /// should be passed through a pointer to a caller
129 /// allocated location passed as an implicit first
130 /// argument to the function.
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000131
Daniel Dunbar11434922009-01-26 21:26:08 +0000132 Ignore, /// Ignore the argument (treat as void). Useful for
133 /// void and empty structs.
134
Daniel Dunbar56273772008-09-17 00:51:38 +0000135 Coerce, /// Only valid for aggregate return types, the argument
136 /// should be accessed by coercion to a provided type.
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000137
138 ByVal, /// Only valid for aggregate argument types. The
139 /// structure should be passed "byval" with the
140 /// specified alignment (0 indicates default
141 /// alignment).
142
143 Expand, /// Only valid for aggregate argument types. The
144 /// structure should be expanded into consecutive
Daniel Dunbar56273772008-09-17 00:51:38 +0000145 /// arguments for its constituent fields. Currently
146 /// expand is only allowed on structures whose fields
147 /// are all scalar types or are themselves expandable
148 /// types.
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000149
150 KindFirst=Default, KindLast=Expand
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000151 };
152
153private:
154 Kind TheKind;
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000155 const llvm::Type *TypeData;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000156 unsigned UIntData;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000157
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000158 ABIArgInfo(Kind K, const llvm::Type *TD=0,
159 unsigned UI=0) : TheKind(K),
160 TypeData(TD),
161 UIntData(0) {}
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000162public:
163 static ABIArgInfo getDefault() {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000164 return ABIArgInfo(Default);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000165 }
166 static ABIArgInfo getStructRet() {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000167 return ABIArgInfo(StructRet);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000168 }
Daniel Dunbar11434922009-01-26 21:26:08 +0000169 static ABIArgInfo getIgnore() {
170 return ABIArgInfo(Ignore);
171 }
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000172 static ABIArgInfo getCoerce(const llvm::Type *T) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000173 return ABIArgInfo(Coerce, T);
174 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000175 static ABIArgInfo getByVal(unsigned Alignment) {
176 return ABIArgInfo(ByVal, 0, Alignment);
177 }
Daniel Dunbar56273772008-09-17 00:51:38 +0000178 static ABIArgInfo getExpand() {
179 return ABIArgInfo(Expand);
180 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000181
182 Kind getKind() const { return TheKind; }
183 bool isDefault() const { return TheKind == Default; }
184 bool isStructRet() const { return TheKind == StructRet; }
Daniel Dunbar11434922009-01-26 21:26:08 +0000185 bool isIgnore() const { return TheKind == Ignore; }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000186 bool isCoerce() const { return TheKind == Coerce; }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000187 bool isByVal() const { return TheKind == ByVal; }
Daniel Dunbar56273772008-09-17 00:51:38 +0000188 bool isExpand() const { return TheKind == Expand; }
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000189
190 // Coerce accessors
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000191 const llvm::Type *getCoerceToType() const {
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000192 assert(TheKind == Coerce && "Invalid kind!");
193 return TypeData;
194 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000195
196 // ByVal accessors
197 unsigned getByValAlignment() const {
198 assert(TheKind == ByVal && "Invalid kind!");
199 return UIntData;
200 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000201};
202
203/***/
204
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000205/* FIXME: All of this stuff should be part of the target interface
206 somehow. It is currently here because it is not clear how to factor
207 the targets to support this, since the Targets currently live in a
208 layer below types n'stuff.
209 */
210
211/// ABIInfo - Target specific hooks for defining how a type should be
212/// passed or returned from functions.
213class clang::ABIInfo {
214public:
215 virtual ~ABIInfo();
216
217 virtual ABIArgInfo classifyReturnType(QualType RetTy,
218 ASTContext &Context) const = 0;
219
220 virtual ABIArgInfo classifyArgumentType(QualType Ty,
221 ASTContext &Context) const = 0;
222};
223
224ABIInfo::~ABIInfo() {}
225
Daniel Dunbar834af452008-09-17 21:22:33 +0000226/// isEmptyStruct - Return true iff a structure has no non-empty
227/// members. Note that a structure with a flexible array member is not
228/// considered empty.
229static bool isEmptyStruct(QualType T) {
230 const RecordType *RT = T->getAsStructureType();
231 if (!RT)
232 return 0;
233 const RecordDecl *RD = RT->getDecl();
234 if (RD->hasFlexibleArrayMember())
235 return false;
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000236 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar834af452008-09-17 21:22:33 +0000237 e = RD->field_end(); i != e; ++i) {
238 const FieldDecl *FD = *i;
239 if (!isEmptyStruct(FD->getType()))
240 return false;
241 }
242 return true;
243}
244
245/// isSingleElementStruct - Determine if a structure is a "single
246/// element struct", i.e. it has exactly one non-empty field or
247/// exactly one field which is itself a single element
248/// struct. Structures with flexible array members are never
249/// considered single element structs.
250///
251/// \return The field declaration for the single non-empty field, if
252/// it exists.
253static const FieldDecl *isSingleElementStruct(QualType T) {
254 const RecordType *RT = T->getAsStructureType();
255 if (!RT)
256 return 0;
257
258 const RecordDecl *RD = RT->getDecl();
259 if (RD->hasFlexibleArrayMember())
260 return 0;
261
262 const FieldDecl *Found = 0;
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000263 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar834af452008-09-17 21:22:33 +0000264 e = RD->field_end(); i != e; ++i) {
265 const FieldDecl *FD = *i;
266 QualType FT = FD->getType();
267
268 if (isEmptyStruct(FT)) {
269 // Ignore
270 } else if (Found) {
271 return 0;
272 } else if (!CodeGenFunction::hasAggregateLLVMType(FT)) {
273 Found = FD;
274 } else {
275 Found = isSingleElementStruct(FT);
276 if (!Found)
277 return 0;
278 }
279 }
280
281 return Found;
282}
283
284static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
285 if (!Ty->getAsBuiltinType() && !Ty->isPointerType())
286 return false;
287
288 uint64_t Size = Context.getTypeSize(Ty);
289 return Size == 32 || Size == 64;
290}
291
292static bool areAllFields32Or64BitBasicType(const RecordDecl *RD,
293 ASTContext &Context) {
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000294 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar834af452008-09-17 21:22:33 +0000295 e = RD->field_end(); i != e; ++i) {
296 const FieldDecl *FD = *i;
297
298 if (!is32Or64BitBasicType(FD->getType(), Context))
299 return false;
300
301 // If this is a bit-field we need to make sure it is still a
302 // 32-bit or 64-bit type.
303 if (Expr *BW = FD->getBitWidth()) {
304 unsigned Width = BW->getIntegerConstantExprValue(Context).getZExtValue();
305 if (Width <= 16)
306 return false;
307 }
308 }
309 return true;
310}
311
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000312namespace {
313/// DefaultABIInfo - The default implementation for ABI specific
314/// details. This implementation provides information which results in
315/// sensible LLVM IR generation, but does not conform to any
316/// particular ABI.
317class DefaultABIInfo : public ABIInfo {
318 virtual ABIArgInfo classifyReturnType(QualType RetTy,
319 ASTContext &Context) const;
320
321 virtual ABIArgInfo classifyArgumentType(QualType RetTy,
322 ASTContext &Context) const;
323};
324
325/// X86_32ABIInfo - The X86-32 ABI information.
326class X86_32ABIInfo : public ABIInfo {
327public:
328 virtual ABIArgInfo classifyReturnType(QualType RetTy,
329 ASTContext &Context) const;
330
331 virtual ABIArgInfo classifyArgumentType(QualType RetTy,
332 ASTContext &Context) const;
333};
334}
335
336ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
337 ASTContext &Context) const {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000338 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar834af452008-09-17 21:22:33 +0000339 // Classify "single element" structs as their element type.
340 const FieldDecl *SeltFD = isSingleElementStruct(RetTy);
341 if (SeltFD) {
342 QualType SeltTy = SeltFD->getType()->getDesugaredType();
343 if (const BuiltinType *BT = SeltTy->getAsBuiltinType()) {
344 // FIXME: This is gross, it would be nice if we could just
345 // pass back SeltTy and have clients deal with it. Is it worth
346 // supporting coerce to both LLVM and clang Types?
347 if (BT->isIntegerType()) {
348 uint64_t Size = Context.getTypeSize(SeltTy);
349 return ABIArgInfo::getCoerce(llvm::IntegerType::get((unsigned) Size));
350 } else if (BT->getKind() == BuiltinType::Float) {
351 return ABIArgInfo::getCoerce(llvm::Type::FloatTy);
352 } else if (BT->getKind() == BuiltinType::Double) {
353 return ABIArgInfo::getCoerce(llvm::Type::DoubleTy);
354 }
355 } else if (SeltTy->isPointerType()) {
356 // FIXME: It would be really nice if this could come out as
357 // the proper pointer type.
358 llvm::Type *PtrTy =
359 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
360 return ABIArgInfo::getCoerce(PtrTy);
361 }
362 }
363
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000364 uint64_t Size = Context.getTypeSize(RetTy);
365 if (Size == 8) {
366 return ABIArgInfo::getCoerce(llvm::Type::Int8Ty);
367 } else if (Size == 16) {
368 return ABIArgInfo::getCoerce(llvm::Type::Int16Ty);
369 } else if (Size == 32) {
370 return ABIArgInfo::getCoerce(llvm::Type::Int32Ty);
371 } else if (Size == 64) {
372 return ABIArgInfo::getCoerce(llvm::Type::Int64Ty);
373 } else {
374 return ABIArgInfo::getStructRet();
375 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000376 } else {
377 return ABIArgInfo::getDefault();
378 }
379}
380
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000381ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
382 ASTContext &Context) const {
Daniel Dunbarf0357382008-09-17 20:11:04 +0000383 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar834af452008-09-17 21:22:33 +0000384 // Structures with flexible arrays are always byval.
385 if (const RecordType *RT = Ty->getAsStructureType())
386 if (RT->getDecl()->hasFlexibleArrayMember())
387 return ABIArgInfo::getByVal(0);
388
389 // Expand empty structs (i.e. ignore)
390 uint64_t Size = Context.getTypeSize(Ty);
391 if (Ty->isStructureType() && Size == 0)
392 return ABIArgInfo::getExpand();
393
394 // Expand structs with size <= 128-bits which consist only of
395 // basic types (int, long long, float, double, xxx*). This is
396 // non-recursive and does not ignore empty fields.
397 if (const RecordType *RT = Ty->getAsStructureType()) {
398 if (Context.getTypeSize(Ty) <= 4*32 &&
399 areAllFields32Or64BitBasicType(RT->getDecl(), Context))
400 return ABIArgInfo::getExpand();
401 }
402
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000403 return ABIArgInfo::getByVal(0);
404 } else {
405 return ABIArgInfo::getDefault();
406 }
407}
408
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000409namespace {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000410/// X86_64ABIInfo - The X86_64 ABI information.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000411class X86_64ABIInfo : public ABIInfo {
412 enum Class {
413 Integer = 0,
414 SSE,
415 SSEUp,
416 X87,
417 X87Up,
418 ComplexX87,
419 NoClass,
420 Memory
421 };
422
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000423 /// merge - Implement the X86_64 ABI merging algorithm.
424 ///
Daniel Dunbarc4503572009-01-31 00:06:58 +0000425 /// Merge an accumulating classification \arg Accum with a field
426 /// classification \arg Field.
427 ///
428 /// \param Accum - The accumulating classification. This should
429 /// always be either NoClass or the result of a previous merge
430 /// call. In addition, this should never be Memory (the caller
431 /// should just return Memory for the aggregate).
432 Class merge(Class Accum, Class Field) const;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000433
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000434 /// classify - Determine the x86_64 register classes in which the
435 /// given type T should be passed.
436 ///
Daniel Dunbarc4503572009-01-31 00:06:58 +0000437 /// \param Lo - The classification for the parts of the type
438 /// residing in the low word of the containing object.
439 ///
440 /// \param Hi - The classification for the parts of the type
441 /// residing in the high word of the containing object.
442 ///
443 /// \param OffsetBase - The bit offset of this type in the
Daniel Dunbarcdf920e2009-01-30 22:40:15 +0000444 /// containing object. Some parameters are classified different
445 /// depending on whether they straddle an eightbyte boundary.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000446 ///
447 /// If a word is unused its result will be NoClass; if a type should
448 /// be passed in Memory then at least the classification of \arg Lo
449 /// will be Memory.
450 ///
451 /// The \arg Lo class will be NoClass iff the argument is ignored.
452 ///
453 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
454 /// be NoClass.
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000455 void classify(QualType T, ASTContext &Context, uint64_t OffsetBase,
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000456 Class &Lo, Class &Hi) const;
Daniel Dunbarc4503572009-01-31 00:06:58 +0000457
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000458public:
459 virtual ABIArgInfo classifyReturnType(QualType RetTy,
460 ASTContext &Context) const;
461
462 virtual ABIArgInfo classifyArgumentType(QualType RetTy,
463 ASTContext &Context) const;
464};
465}
466
Daniel Dunbarc4503572009-01-31 00:06:58 +0000467X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum,
468 Class Field) const {
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000469 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
470 // classified recursively so that always two fields are
471 // considered. The resulting class is calculated according to
472 // the classes of the fields in the eightbyte:
473 //
474 // (a) If both classes are equal, this is the resulting class.
475 //
476 // (b) If one of the classes is NO_CLASS, the resulting class is
477 // the other class.
478 //
479 // (c) If one of the classes is MEMORY, the result is the MEMORY
480 // class.
481 //
482 // (d) If one of the classes is INTEGER, the result is the
483 // INTEGER.
484 //
485 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
486 // MEMORY is used as class.
487 //
488 // (f) Otherwise class SSE is used.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000489 assert((Accum == NoClass || Accum == Integer ||
490 Accum == SSE || Accum == SSEUp) &&
491 "Invalid accumulated classification during merge.");
492 if (Accum == Field || Field == NoClass)
493 return Accum;
494 else if (Field == Memory)
495 return Memory;
496 else if (Accum == NoClass)
497 return Field;
498 else if (Accum == Integer || Field == Integer)
499 return Integer;
500 else if (Field == X87 || Field == X87Up || Field == ComplexX87)
501 return Memory;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000502 else
Daniel Dunbarc4503572009-01-31 00:06:58 +0000503 return SSE;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000504}
505
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000506void X86_64ABIInfo::classify(QualType Ty,
507 ASTContext &Context,
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000508 uint64_t OffsetBase,
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000509 Class &Lo, Class &Hi) const {
Daniel Dunbar9a82b522009-02-02 18:06:39 +0000510 // FIXME: This code can be simplified by introducing a simple value
511 // class for Class pairs with appropriate constructor methods for
512 // the various situations.
513
Daniel Dunbarc4503572009-01-31 00:06:58 +0000514 Lo = Hi = NoClass;
515
516 Class &Current = OffsetBase < 64 ? Lo : Hi;
517 Current = Memory;
518
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000519 if (const BuiltinType *BT = Ty->getAsBuiltinType()) {
520 BuiltinType::Kind k = BT->getKind();
521
Daniel Dunbar11434922009-01-26 21:26:08 +0000522 if (k == BuiltinType::Void) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000523 Current = NoClass;
Daniel Dunbar11434922009-01-26 21:26:08 +0000524 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000525 Current = Integer;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000526 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000527 Current = SSE;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000528 } else if (k == BuiltinType::LongDouble) {
529 Lo = X87;
530 Hi = X87Up;
531 }
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000532 // FIXME: _Decimal32 and _Decimal64 are SSE.
533 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000534 // FIXME: __int128 is (Integer, Integer).
535 } else if (Ty->isPointerLikeType() || Ty->isBlockPointerType() ||
536 Ty->isObjCQualifiedInterfaceType()) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000537 Current = Integer;
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000538 } else if (const VectorType *VT = Ty->getAsVectorType()) {
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000539 uint64_t Size = Context.getTypeSize(VT);
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000540 if (Size == 64) {
Daniel Dunbard4cd1b02009-01-30 19:38:39 +0000541 // gcc passes <1 x double> in memory.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000542 if (VT->getElementType() == Context.DoubleTy)
Daniel Dunbard4cd1b02009-01-30 19:38:39 +0000543 return;
Daniel Dunbard4cd1b02009-01-30 19:38:39 +0000544
Daniel Dunbarc4503572009-01-31 00:06:58 +0000545 Current = SSE;
Daniel Dunbare33edf12009-01-30 18:40:10 +0000546
547 // If this type crosses an eightbyte boundary, it should be
548 // split.
Daniel Dunbarcdf920e2009-01-30 22:40:15 +0000549 if (OffsetBase && OffsetBase != 64)
Daniel Dunbare33edf12009-01-30 18:40:10 +0000550 Hi = Lo;
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000551 } else if (Size == 128) {
552 Lo = SSE;
553 Hi = SSEUp;
554 }
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000555 } else if (const ComplexType *CT = Ty->getAsComplexType()) {
556 QualType ET = CT->getElementType();
557
Daniel Dunbare33edf12009-01-30 18:40:10 +0000558 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbareac48dc2009-01-29 07:22:20 +0000559 if (ET->isIntegerType()) {
Daniel Dunbareac48dc2009-01-29 07:22:20 +0000560 if (Size <= 64)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000561 Current = Integer;
Daniel Dunbareac48dc2009-01-29 07:22:20 +0000562 else if (Size <= 128)
563 Lo = Hi = Integer;
564 } else if (ET == Context.FloatTy)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000565 Current = SSE;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000566 else if (ET == Context.DoubleTy)
567 Lo = Hi = SSE;
568 else if (ET == Context.LongDoubleTy)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000569 Current = ComplexX87;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000570
571 // If this complex type crosses an eightbyte boundary then it
572 // should be split.
Daniel Dunbarcdf920e2009-01-30 22:40:15 +0000573 uint64_t EB_Real = (OffsetBase) / 64;
574 uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000575 if (Hi == NoClass && EB_Real != EB_Imag)
576 Hi = Lo;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000577 } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
578 // Arrays are treated like structures.
579
580 uint64_t Size = Context.getTypeSize(Ty);
581
582 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
583 // than two eightbytes, ..., it has class MEMORY.
584 if (Size > 128)
585 return;
586
587 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
588 // fields, it has class MEMORY.
589 //
590 // Only need to check alignment of array base.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000591 if (OffsetBase % Context.getTypeAlign(AT->getElementType()))
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000592 return;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000593
594 // Otherwise implement simplified merge. We could be smarter about
595 // this, but it isn't worth it and would be harder to verify.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000596 Current = NoClass;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000597 uint64_t EltSize = Context.getTypeSize(AT->getElementType());
598 uint64_t ArraySize = AT->getSize().getZExtValue();
599 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
600 Class FieldLo, FieldHi;
601 classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbarc4503572009-01-31 00:06:58 +0000602 Lo = merge(Lo, FieldLo);
603 Hi = merge(Hi, FieldHi);
604 if (Lo == Memory || Hi == Memory)
605 break;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000606 }
Daniel Dunbarc4503572009-01-31 00:06:58 +0000607
608 // Do post merger cleanup (see below). Only case we worry about is Memory.
609 if (Hi == Memory)
610 Lo = Memory;
611 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Daniel Dunbar99037e52009-01-29 08:13:58 +0000612 } else if (const RecordType *RT = Ty->getAsRecordType()) {
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000613 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbar99037e52009-01-29 08:13:58 +0000614
615 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
616 // than two eightbytes, ..., it has class MEMORY.
617 if (Size > 128)
618 return;
619
620 const RecordDecl *RD = RT->getDecl();
621
622 // Assume variable sized types are passed in memory.
623 if (RD->hasFlexibleArrayMember())
624 return;
625
626 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
627
628 // Reset Lo class, this will be recomputed.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000629 Current = NoClass;
Daniel Dunbar99037e52009-01-29 08:13:58 +0000630 unsigned idx = 0;
631 for (RecordDecl::field_iterator i = RD->field_begin(),
632 e = RD->field_end(); i != e; ++i, ++idx) {
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000633 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Daniel Dunbar99037e52009-01-29 08:13:58 +0000634
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000635 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
636 // fields, it has class MEMORY.
Daniel Dunbar99037e52009-01-29 08:13:58 +0000637 if (Offset % Context.getTypeAlign(i->getType())) {
638 Lo = Memory;
639 return;
640 }
641
Daniel Dunbar99037e52009-01-29 08:13:58 +0000642 // Classify this field.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000643 //
644 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
645 // exceeds a single eightbyte, each is classified
646 // separately. Each eightbyte gets initialized to class
647 // NO_CLASS.
Daniel Dunbar99037e52009-01-29 08:13:58 +0000648 Class FieldLo, FieldHi;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000649 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbarc4503572009-01-31 00:06:58 +0000650 Lo = merge(Lo, FieldLo);
651 Hi = merge(Hi, FieldHi);
652 if (Lo == Memory || Hi == Memory)
653 break;
Daniel Dunbar99037e52009-01-29 08:13:58 +0000654 }
655
656 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
657 //
658 // (a) If one of the classes is MEMORY, the whole argument is
659 // passed in memory.
660 //
661 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
662
663 // The first of these conditions is guaranteed by how we implement
Daniel Dunbarc4503572009-01-31 00:06:58 +0000664 // the merge (just bail).
665 //
666 // The second condition occurs in the case of unions; for example
667 // union { _Complex double; unsigned; }.
668 if (Hi == Memory)
669 Lo = Memory;
Daniel Dunbar99037e52009-01-29 08:13:58 +0000670 if (Hi == SSEUp && Lo != SSE)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000671 Hi = SSE;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000672 }
673}
674
Daniel Dunbarc4503572009-01-31 00:06:58 +0000675
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000676ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
677 ASTContext &Context) const {
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000678 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
679 // classification algorithm.
680 X86_64ABIInfo::Class Lo, Hi;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000681 classify(RetTy, Context, 0, Lo, Hi);
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000682
Daniel Dunbarc4503572009-01-31 00:06:58 +0000683 // Check some invariants.
684 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
685 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
686 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
687
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000688 const llvm::Type *ResType = 0;
689 switch (Lo) {
690 case NoClass:
Daniel Dunbar11434922009-01-26 21:26:08 +0000691 return ABIArgInfo::getIgnore();
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000692
693 case SSEUp:
694 case X87Up:
695 assert(0 && "Invalid classification for lo word.");
696
Daniel Dunbarc4503572009-01-31 00:06:58 +0000697 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
698 // hidden argument, i.e. structret.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000699 case Memory:
700 return ABIArgInfo::getStructRet();
701
702 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
703 // available register of the sequence %rax, %rdx is used.
704 case Integer:
705 ResType = llvm::Type::Int64Ty; break;
706
707 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
708 // available SSE register of the sequence %xmm0, %xmm1 is used.
709 case SSE:
710 ResType = llvm::Type::DoubleTy; break;
711
712 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
713 // returned on the X87 stack in %st0 as 80-bit x87 number.
714 case X87:
715 ResType = llvm::Type::X86_FP80Ty; break;
716
Daniel Dunbarc4503572009-01-31 00:06:58 +0000717 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
718 // part of the value is returned in %st0 and the imaginary part in
719 // %st1.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000720 case ComplexX87:
721 assert(Hi == NoClass && "Unexpected ComplexX87 classification.");
722 ResType = llvm::VectorType::get(llvm::Type::X86_FP80Ty, 2);
723 break;
724 }
725
726 switch (Hi) {
727 // Memory was handled previously, and ComplexX87 and X87 should
728 // never occur as hi classes.
729 case Memory:
730 case X87:
731 case ComplexX87:
732 assert(0 && "Invalid classification for hi word.");
733
734 case NoClass: break;
735 case Integer:
Daniel Dunbarb0e14f22009-01-29 07:36:07 +0000736 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
737 break;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000738 case SSE:
Daniel Dunbarb0e14f22009-01-29 07:36:07 +0000739 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
740 break;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000741
742 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
743 // is passed in the upper half of the last used SSE register.
744 //
745 // SSEUP should always be preceeded by SSE, just widen.
746 case SSEUp:
747 assert(Lo == SSE && "Unexpected SSEUp classification.");
748 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
749 break;
750
751 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
Daniel Dunbarb0e14f22009-01-29 07:36:07 +0000752 // returned together with the previous X87 value in %st0.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000753 //
754 // X87UP should always be preceeded by X87, so we don't need to do
755 // anything here.
756 case X87Up:
757 assert(Lo == X87 && "Unexpected X87Up classification.");
758 break;
759 }
760
761 return ABIArgInfo::getCoerce(ResType);
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000762}
763
764ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty,
765 ASTContext &Context) const {
766 return ABIArgInfo::getDefault();
767}
768
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000769ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
770 ASTContext &Context) const {
771 return ABIArgInfo::getDefault();
772}
773
774ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
775 ASTContext &Context) const {
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000776 return ABIArgInfo::getDefault();
777}
778
779const ABIInfo &CodeGenTypes::getABIInfo() const {
780 if (TheABIInfo)
781 return *TheABIInfo;
782
783 // For now we just cache this in the CodeGenTypes and don't bother
784 // to free it.
785 const char *TargetPrefix = getContext().Target.getTargetPrefix();
786 if (strcmp(TargetPrefix, "x86") == 0) {
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000787 switch (getContext().Target.getPointerWidth(0)) {
788 case 32:
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000789 return *(TheABIInfo = new X86_32ABIInfo());
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000790 case 64:
Daniel Dunbar11a76ed2009-01-30 18:47:53 +0000791 return *(TheABIInfo = new X86_64ABIInfo());
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000792 }
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000793 }
794
795 return *(TheABIInfo = new DefaultABIInfo);
796}
797
798// getABIReturnInfo - Wrap the ABIInfo getABIReturnInfo, altering
799// "default" types to StructRet when appropriate for simplicity.
800static ABIArgInfo getABIReturnInfo(QualType Ty, CodeGenTypes &CGT) {
801 assert(!Ty->isArrayType() &&
802 "Array types cannot be passed directly.");
803 ABIArgInfo Info = CGT.getABIInfo().classifyReturnType(Ty, CGT.getContext());
Daniel Dunbarf0357382008-09-17 20:11:04 +0000804 // Ensure default on aggregate types is StructRet.
805 if (Info.isDefault() && CodeGenFunction::hasAggregateLLVMType(Ty))
806 return ABIArgInfo::getStructRet();
807 return Info;
808}
809
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000810// getABIArgumentInfo - Wrap the ABIInfo getABIReturnInfo, altering
811// "default" types to ByVal when appropriate for simplicity.
812static ABIArgInfo getABIArgumentInfo(QualType Ty, CodeGenTypes &CGT) {
813 assert(!Ty->isArrayType() &&
814 "Array types cannot be passed directly.");
815 ABIArgInfo Info = CGT.getABIInfo().classifyArgumentType(Ty, CGT.getContext());
Daniel Dunbarf0357382008-09-17 20:11:04 +0000816 // Ensure default on aggregate types is ByVal.
817 if (Info.isDefault() && CodeGenFunction::hasAggregateLLVMType(Ty))
818 return ABIArgInfo::getByVal(0);
819 return Info;
820}
821
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000822/***/
823
Daniel Dunbar56273772008-09-17 00:51:38 +0000824void CodeGenTypes::GetExpandedTypes(QualType Ty,
825 std::vector<const llvm::Type*> &ArgTys) {
826 const RecordType *RT = Ty->getAsStructureType();
827 assert(RT && "Can only expand structure types.");
828 const RecordDecl *RD = RT->getDecl();
829 assert(!RD->hasFlexibleArrayMember() &&
830 "Cannot expand structure with flexible array.");
831
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000832 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar56273772008-09-17 00:51:38 +0000833 e = RD->field_end(); i != e; ++i) {
834 const FieldDecl *FD = *i;
835 assert(!FD->isBitField() &&
836 "Cannot expand structure with bit-field members.");
837
838 QualType FT = FD->getType();
839 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
840 GetExpandedTypes(FT, ArgTys);
841 } else {
842 ArgTys.push_back(ConvertType(FT));
843 }
844 }
845}
846
847llvm::Function::arg_iterator
848CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
849 llvm::Function::arg_iterator AI) {
850 const RecordType *RT = Ty->getAsStructureType();
851 assert(RT && "Can only expand structure types.");
852
853 RecordDecl *RD = RT->getDecl();
854 assert(LV.isSimple() &&
855 "Unexpected non-simple lvalue during struct expansion.");
856 llvm::Value *Addr = LV.getAddress();
857 for (RecordDecl::field_iterator i = RD->field_begin(),
858 e = RD->field_end(); i != e; ++i) {
859 FieldDecl *FD = *i;
860 QualType FT = FD->getType();
861
862 // FIXME: What are the right qualifiers here?
863 LValue LV = EmitLValueForField(Addr, FD, false, 0);
864 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
865 AI = ExpandTypeFromArgs(FT, LV, AI);
866 } else {
867 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
868 ++AI;
869 }
870 }
871
872 return AI;
873}
874
875void
876CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
877 llvm::SmallVector<llvm::Value*, 16> &Args) {
878 const RecordType *RT = Ty->getAsStructureType();
879 assert(RT && "Can only expand structure types.");
880
881 RecordDecl *RD = RT->getDecl();
882 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
883 llvm::Value *Addr = RV.getAggregateAddr();
884 for (RecordDecl::field_iterator i = RD->field_begin(),
885 e = RD->field_end(); i != e; ++i) {
886 FieldDecl *FD = *i;
887 QualType FT = FD->getType();
888
889 // FIXME: What are the right qualifiers here?
890 LValue LV = EmitLValueForField(Addr, FD, false, 0);
891 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
892 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
893 } else {
894 RValue RV = EmitLoadOfLValue(LV, FT);
895 assert(RV.isScalar() &&
896 "Unexpected non-scalar rvalue during struct expansion.");
897 Args.push_back(RV.getScalarVal());
898 }
899 }
900}
901
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000902/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
903/// a pointer to an object of type \arg Ty.
904///
905/// This safely handles the case when the src type is smaller than the
906/// destination type; in this situation the values of bits which not
907/// present in the src are undefined.
908static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
909 const llvm::Type *Ty,
910 CodeGenFunction &CGF) {
911 const llvm::Type *SrcTy =
912 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
913 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
914 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(Ty);
915
916 // If load is legal, just bitcase the src pointer.
917 if (SrcSize == DstSize) {
918 llvm::Value *Casted =
919 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
920 return CGF.Builder.CreateLoad(Casted);
921 } else {
922 assert(SrcSize < DstSize && "Coercion is losing source bits!");
923
924 // Otherwise do coercion through memory. This is stupid, but
925 // simple.
926 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
927 llvm::Value *Casted =
928 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
929 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
930 return CGF.Builder.CreateLoad(Tmp);
931 }
932}
933
934/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
935/// where the source and destination may have different types.
936///
937/// This safely handles the case when the src type is larger than the
938/// destination type; the upper bits of the src will be lost.
939static void CreateCoercedStore(llvm::Value *Src,
940 llvm::Value *DstPtr,
941 CodeGenFunction &CGF) {
942 const llvm::Type *SrcTy = Src->getType();
943 const llvm::Type *DstTy =
944 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
945
946 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
947 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(DstTy);
948
949 // If store is legal, just bitcase the src pointer.
950 if (SrcSize == DstSize) {
951 llvm::Value *Casted =
952 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
953 CGF.Builder.CreateStore(Src, Casted);
954 } else {
955 assert(SrcSize > DstSize && "Coercion is missing bits!");
956
957 // Otherwise do coercion through memory. This is stupid, but
958 // simple.
959 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
960 CGF.Builder.CreateStore(Src, Tmp);
961 llvm::Value *Casted =
962 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
963 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(Casted), DstPtr);
964 }
965}
966
Daniel Dunbar56273772008-09-17 00:51:38 +0000967/***/
968
Daniel Dunbar88b53962009-02-02 22:03:45 +0000969bool CodeGenModule::ReturnTypeUsesSret(const CGFunctionInfo &FI) {
970 return getABIReturnInfo(FI.getReturnType(), getTypes()).isStructRet();
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000971}
972
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000973const llvm::FunctionType *
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000974CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic) {
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000975 std::vector<const llvm::Type*> ArgTys;
976
977 const llvm::Type *ResultType = 0;
978
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000979 QualType RetTy = FI.getReturnType();
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000980 ABIArgInfo RetAI = getABIReturnInfo(RetTy, *this);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000981 switch (RetAI.getKind()) {
982 case ABIArgInfo::ByVal:
983 case ABIArgInfo::Expand:
984 assert(0 && "Invalid ABI kind for return argument");
985
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000986 case ABIArgInfo::Default:
987 if (RetTy->isVoidType()) {
988 ResultType = llvm::Type::VoidTy;
989 } else {
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000990 ResultType = ConvertType(RetTy);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000991 }
992 break;
993
994 case ABIArgInfo::StructRet: {
995 ResultType = llvm::Type::VoidTy;
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000996 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000997 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
998 break;
999 }
1000
Daniel Dunbar11434922009-01-26 21:26:08 +00001001 case ABIArgInfo::Ignore:
1002 ResultType = llvm::Type::VoidTy;
1003 break;
1004
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001005 case ABIArgInfo::Coerce:
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001006 ResultType = RetAI.getCoerceToType();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001007 break;
1008 }
1009
Daniel Dunbara0a99e02009-02-02 23:43:58 +00001010 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1011 it != ie; ++it) {
1012 ABIArgInfo AI = getABIArgumentInfo(*it, *this);
1013 const llvm::Type *Ty = ConvertType(*it);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001014
1015 switch (AI.getKind()) {
Daniel Dunbar11434922009-01-26 21:26:08 +00001016 case ABIArgInfo::Ignore:
1017 break;
1018
Daniel Dunbar56273772008-09-17 00:51:38 +00001019 case ABIArgInfo::Coerce:
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001020 case ABIArgInfo::StructRet:
1021 assert(0 && "Invalid ABI kind for non-return argument");
1022
1023 case ABIArgInfo::ByVal:
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001024 // byval arguments are always on the stack, which is addr space #0.
1025 ArgTys.push_back(llvm::PointerType::getUnqual(Ty));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001026 assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled");
1027 break;
1028
1029 case ABIArgInfo::Default:
1030 ArgTys.push_back(Ty);
1031 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001032
1033 case ABIArgInfo::Expand:
Daniel Dunbara0a99e02009-02-02 23:43:58 +00001034 GetExpandedTypes(*it, ArgTys);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001035 break;
1036 }
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001037 }
1038
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001039 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar3913f182008-09-09 23:48:28 +00001040}
1041
Daniel Dunbara0a99e02009-02-02 23:43:58 +00001042void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
Daniel Dunbar88b53962009-02-02 22:03:45 +00001043 const Decl *TargetDecl,
Devang Patel761d7f72008-09-25 21:02:23 +00001044 AttributeListType &PAL) {
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001045 unsigned FuncAttrs = 0;
Devang Patela2c69122008-09-26 22:53:57 +00001046 unsigned RetAttrs = 0;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001047
1048 if (TargetDecl) {
1049 if (TargetDecl->getAttr<NoThrowAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +00001050 FuncAttrs |= llvm::Attribute::NoUnwind;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001051 if (TargetDecl->getAttr<NoReturnAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +00001052 FuncAttrs |= llvm::Attribute::NoReturn;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001053 if (TargetDecl->getAttr<PureAttr>())
1054 FuncAttrs |= llvm::Attribute::ReadOnly;
1055 if (TargetDecl->getAttr<ConstAttr>())
1056 FuncAttrs |= llvm::Attribute::ReadNone;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001057 }
1058
Daniel Dunbara0a99e02009-02-02 23:43:58 +00001059 QualType RetTy = FI.getReturnType();
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001060 unsigned Index = 1;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001061 ABIArgInfo RetAI = getABIReturnInfo(RetTy, getTypes());
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001062 switch (RetAI.getKind()) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001063 case ABIArgInfo::Default:
1064 if (RetTy->isPromotableIntegerType()) {
1065 if (RetTy->isSignedIntegerType()) {
Devang Patela2c69122008-09-26 22:53:57 +00001066 RetAttrs |= llvm::Attribute::SExt;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001067 } else if (RetTy->isUnsignedIntegerType()) {
Devang Patela2c69122008-09-26 22:53:57 +00001068 RetAttrs |= llvm::Attribute::ZExt;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001069 }
1070 }
1071 break;
1072
1073 case ABIArgInfo::StructRet:
Devang Patel761d7f72008-09-25 21:02:23 +00001074 PAL.push_back(llvm::AttributeWithIndex::get(Index,
Daniel Dunbar725ad312009-01-31 02:19:00 +00001075 llvm::Attribute::StructRet |
1076 llvm::Attribute::NoAlias));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001077 ++Index;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001078 break;
1079
Daniel Dunbar11434922009-01-26 21:26:08 +00001080 case ABIArgInfo::Ignore:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001081 case ABIArgInfo::Coerce:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001082 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001083
1084 case ABIArgInfo::ByVal:
1085 case ABIArgInfo::Expand:
1086 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001087 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001088
Devang Patela2c69122008-09-26 22:53:57 +00001089 if (RetAttrs)
1090 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Daniel Dunbara0a99e02009-02-02 23:43:58 +00001091 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1092 it != ie; ++it) {
1093 QualType ParamType = *it;
Devang Patel761d7f72008-09-25 21:02:23 +00001094 unsigned Attributes = 0;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001095 ABIArgInfo AI = getABIArgumentInfo(ParamType, getTypes());
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001096
1097 switch (AI.getKind()) {
1098 case ABIArgInfo::StructRet:
Daniel Dunbar56273772008-09-17 00:51:38 +00001099 case ABIArgInfo::Coerce:
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001100 assert(0 && "Invalid ABI kind for non-return argument");
1101
1102 case ABIArgInfo::ByVal:
Devang Patel761d7f72008-09-25 21:02:23 +00001103 Attributes |= llvm::Attribute::ByVal;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001104 assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled");
1105 break;
1106
1107 case ABIArgInfo::Default:
1108 if (ParamType->isPromotableIntegerType()) {
1109 if (ParamType->isSignedIntegerType()) {
Devang Patel761d7f72008-09-25 21:02:23 +00001110 Attributes |= llvm::Attribute::SExt;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001111 } else if (ParamType->isUnsignedIntegerType()) {
Devang Patel761d7f72008-09-25 21:02:23 +00001112 Attributes |= llvm::Attribute::ZExt;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001113 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001114 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001115 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001116
Daniel Dunbar11434922009-01-26 21:26:08 +00001117 case ABIArgInfo::Ignore:
1118 // Skip increment, no matching LLVM parameter.
1119 continue;
1120
Daniel Dunbar56273772008-09-17 00:51:38 +00001121 case ABIArgInfo::Expand: {
1122 std::vector<const llvm::Type*> Tys;
1123 // FIXME: This is rather inefficient. Do we ever actually need
1124 // to do anything here? The result should be just reconstructed
1125 // on the other side, so extension should be a non-issue.
1126 getTypes().GetExpandedTypes(ParamType, Tys);
1127 Index += Tys.size();
1128 continue;
1129 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001130 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001131
Devang Patel761d7f72008-09-25 21:02:23 +00001132 if (Attributes)
1133 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar56273772008-09-17 00:51:38 +00001134 ++Index;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001135 }
Devang Patela2c69122008-09-26 22:53:57 +00001136 if (FuncAttrs)
1137 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
1138
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001139}
1140
Daniel Dunbar88b53962009-02-02 22:03:45 +00001141void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
1142 llvm::Function *Fn,
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001143 const FunctionArgList &Args) {
1144 // Emit allocs for param decls. Give the LLVM Argument nodes names.
1145 llvm::Function::arg_iterator AI = Fn->arg_begin();
1146
1147 // Name the struct return argument.
Daniel Dunbar88b53962009-02-02 22:03:45 +00001148 if (CGM.ReturnTypeUsesSret(FI)) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001149 AI->setName("agg.result");
1150 ++AI;
1151 }
1152
1153 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbar56273772008-09-17 00:51:38 +00001154 i != e; ++i) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001155 const VarDecl *Arg = i->first;
Daniel Dunbar56273772008-09-17 00:51:38 +00001156 QualType Ty = i->second;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001157 ABIArgInfo ArgI = getABIArgumentInfo(Ty, CGM.getTypes());
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001158
1159 switch (ArgI.getKind()) {
1160 case ABIArgInfo::ByVal:
1161 case ABIArgInfo::Default: {
1162 assert(AI != Fn->arg_end() && "Argument mismatch!");
1163 llvm::Value* V = AI;
Daniel Dunbar56273772008-09-17 00:51:38 +00001164 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001165 // This must be a promotion, for something like
1166 // "void a(x) short x; {..."
Daniel Dunbar56273772008-09-17 00:51:38 +00001167 V = EmitScalarConversion(V, Ty, Arg->getType());
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001168 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001169 EmitParmDecl(*Arg, V);
1170 break;
1171 }
Daniel Dunbar56273772008-09-17 00:51:38 +00001172
1173 case ABIArgInfo::Expand: {
1174 // If this was structure was expand into multiple arguments then
1175 // we need to create a temporary and reconstruct it from the
1176 // arguments.
Chris Lattner39f34e92008-11-24 04:00:27 +00001177 std::string Name = Arg->getNameAsString();
Daniel Dunbar56273772008-09-17 00:51:38 +00001178 llvm::Value *Temp = CreateTempAlloca(ConvertType(Ty),
1179 (Name + ".addr").c_str());
1180 // FIXME: What are the right qualifiers here?
1181 llvm::Function::arg_iterator End =
1182 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI);
1183 EmitParmDecl(*Arg, Temp);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001184
Daniel Dunbar56273772008-09-17 00:51:38 +00001185 // Name the arguments used in expansion and increment AI.
1186 unsigned Index = 0;
1187 for (; AI != End; ++AI, ++Index)
1188 AI->setName(Name + "." + llvm::utostr(Index));
1189 continue;
1190 }
Daniel Dunbar11434922009-01-26 21:26:08 +00001191
1192 case ABIArgInfo::Ignore:
1193 break;
1194
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001195 case ABIArgInfo::Coerce:
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001196 case ABIArgInfo::StructRet:
1197 assert(0 && "Invalid ABI kind for non-return argument");
1198 }
Daniel Dunbar56273772008-09-17 00:51:38 +00001199
1200 ++AI;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001201 }
1202 assert(AI == Fn->arg_end() && "Argument mismatch!");
1203}
1204
Daniel Dunbar88b53962009-02-02 22:03:45 +00001205void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI,
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001206 llvm::Value *ReturnValue) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001207 llvm::Value *RV = 0;
1208
1209 // Functions with no result always return void.
1210 if (ReturnValue) {
Daniel Dunbar88b53962009-02-02 22:03:45 +00001211 QualType RetTy = FI.getReturnType();
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001212 ABIArgInfo RetAI = getABIReturnInfo(RetTy, CGM.getTypes());
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001213
1214 switch (RetAI.getKind()) {
1215 case ABIArgInfo::StructRet:
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001216 if (RetTy->isAnyComplexType()) {
1217 // FIXME: Volatile
1218 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
1219 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
1220 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1221 EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy);
1222 } else {
1223 Builder.CreateStore(Builder.CreateLoad(ReturnValue),
1224 CurFn->arg_begin());
1225 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001226 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001227
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001228 case ABIArgInfo::Default:
1229 RV = Builder.CreateLoad(ReturnValue);
1230 break;
1231
Daniel Dunbar11434922009-01-26 21:26:08 +00001232 case ABIArgInfo::Ignore:
1233 break;
1234
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001235 case ABIArgInfo::Coerce: {
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +00001236 RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001237 break;
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001238 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001239
1240 case ABIArgInfo::ByVal:
1241 case ABIArgInfo::Expand:
1242 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001243 }
1244 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001245
1246 if (RV) {
1247 Builder.CreateRet(RV);
1248 } else {
1249 Builder.CreateRetVoid();
1250 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001251}
1252
Daniel Dunbar88b53962009-02-02 22:03:45 +00001253RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
1254 llvm::Value *Callee,
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001255 const CallArgList &CallArgs) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001256 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001257
1258 // Handle struct-return functions by passing a pointer to the
1259 // location that we would like to return into.
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001260 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001261 ABIArgInfo RetAI = getABIReturnInfo(RetTy, CGM.getTypes());
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001262 switch (RetAI.getKind()) {
1263 case ABIArgInfo::StructRet:
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001264 // Create a temporary alloca to hold the result of the call. :(
Daniel Dunbar56273772008-09-17 00:51:38 +00001265 Args.push_back(CreateTempAlloca(ConvertType(RetTy)));
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001266 break;
1267
1268 case ABIArgInfo::Default:
Daniel Dunbar11434922009-01-26 21:26:08 +00001269 case ABIArgInfo::Ignore:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001270 case ABIArgInfo::Coerce:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001271 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001272
1273 case ABIArgInfo::ByVal:
1274 case ABIArgInfo::Expand:
1275 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001276 }
1277
1278 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
1279 I != E; ++I) {
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001280 ABIArgInfo ArgInfo = getABIArgumentInfo(I->second, CGM.getTypes());
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001281 RValue RV = I->first;
Daniel Dunbar56273772008-09-17 00:51:38 +00001282
1283 switch (ArgInfo.getKind()) {
1284 case ABIArgInfo::ByVal: // Default is byval
1285 case ABIArgInfo::Default:
1286 if (RV.isScalar()) {
1287 Args.push_back(RV.getScalarVal());
1288 } else if (RV.isComplex()) {
1289 // Make a temporary alloca to pass the argument.
1290 Args.push_back(CreateTempAlloca(ConvertType(I->second)));
1291 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
1292 } else {
1293 Args.push_back(RV.getAggregateAddr());
1294 }
1295 break;
1296
Daniel Dunbar11434922009-01-26 21:26:08 +00001297 case ABIArgInfo::Ignore:
1298 break;
1299
Daniel Dunbar56273772008-09-17 00:51:38 +00001300 case ABIArgInfo::StructRet:
1301 case ABIArgInfo::Coerce:
1302 assert(0 && "Invalid ABI kind for non-return argument");
1303 break;
1304
1305 case ABIArgInfo::Expand:
1306 ExpandTypeToArgs(I->second, RV, Args);
1307 break;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001308 }
1309 }
1310
1311 llvm::CallInst *CI = Builder.CreateCall(Callee,&Args[0],&Args[0]+Args.size());
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001312
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001313 // FIXME: Provide TargetDecl so nounwind, noreturn, etc, etc get set.
Devang Patel761d7f72008-09-25 21:02:23 +00001314 CodeGen::AttributeListType AttributeList;
Daniel Dunbar88b53962009-02-02 22:03:45 +00001315 CGM.ConstructAttributeList(CallInfo, 0, AttributeList);
Devang Patel761d7f72008-09-25 21:02:23 +00001316 CI->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(),
Daniel Dunbar725ad312009-01-31 02:19:00 +00001317 AttributeList.size()));
1318
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001319 if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee))
1320 CI->setCallingConv(F->getCallingConv());
1321 if (CI->getType() != llvm::Type::VoidTy)
1322 CI->setName("call");
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001323
1324 switch (RetAI.getKind()) {
1325 case ABIArgInfo::StructRet:
1326 if (RetTy->isAnyComplexType())
Daniel Dunbar56273772008-09-17 00:51:38 +00001327 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001328 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar56273772008-09-17 00:51:38 +00001329 return RValue::getAggregate(Args[0]);
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001330 else
1331 return RValue::get(Builder.CreateLoad(Args[0]));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001332
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001333 case ABIArgInfo::Default:
1334 return RValue::get(RetTy->isVoidType() ? 0 : CI);
1335
Daniel Dunbar11434922009-01-26 21:26:08 +00001336 case ABIArgInfo::Ignore:
Daniel Dunbarcc039fe2009-01-29 08:24:57 +00001337 if (RetTy->isVoidType())
1338 return RValue::get(0);
1339 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1340 llvm::Value *Res =
1341 llvm::UndefValue::get(llvm::PointerType::getUnqual(ConvertType(RetTy)));
1342 return RValue::getAggregate(Res);
1343 }
1344 return RValue::get(llvm::UndefValue::get(ConvertType(RetTy)));
Daniel Dunbar11434922009-01-26 21:26:08 +00001345
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001346 case ABIArgInfo::Coerce: {
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +00001347 llvm::Value *V = CreateTempAlloca(ConvertType(RetTy), "coerce");
1348 CreateCoercedStore(CI, V, *this);
Anders Carlssonad3d6912008-11-25 22:21:48 +00001349 if (RetTy->isAnyComplexType())
1350 return RValue::getComplex(LoadComplexFromAddr(V, false));
Daniel Dunbar11434922009-01-26 21:26:08 +00001351 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssonad3d6912008-11-25 22:21:48 +00001352 return RValue::getAggregate(V);
Daniel Dunbar11434922009-01-26 21:26:08 +00001353 else
1354 return RValue::get(Builder.CreateLoad(V));
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001355 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001356
1357 case ABIArgInfo::ByVal:
1358 case ABIArgInfo::Expand:
1359 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001360 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001361
1362 assert(0 && "Unhandled ABIArgInfo::Kind");
1363 return RValue::get(0);
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001364}