blob: b040b7ff86e2a0a2b068c58da04de7785f852820 [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 Dunbar9eb5c6d2009-02-03 01:05:53 +000027
28#include "ABIInfo.h"
29
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000030using namespace clang;
31using namespace CodeGen;
32
33/***/
34
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000035// FIXME: Use iterator and sidestep silly type array creation.
36
Daniel Dunbar541b63b2009-02-02 23:23:47 +000037const
38CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionTypeNoProto *FTNP) {
39 return getFunctionInfo(FTNP->getResultType(),
40 llvm::SmallVector<QualType, 16>());
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000041}
42
Daniel Dunbar541b63b2009-02-02 23:23:47 +000043const
44CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionTypeProto *FTP) {
45 llvm::SmallVector<QualType, 16> ArgTys;
46 // FIXME: Kill copy.
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000047 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +000048 ArgTys.push_back(FTP->getArgType(i));
49 return getFunctionInfo(FTP->getResultType(), ArgTys);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000050}
51
Daniel Dunbar541b63b2009-02-02 23:23:47 +000052const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionDecl *FD) {
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000053 const FunctionType *FTy = FD->getType()->getAsFunctionType();
Daniel Dunbar541b63b2009-02-02 23:23:47 +000054 if (const FunctionTypeProto *FTP = dyn_cast<FunctionTypeProto>(FTy))
55 return getFunctionInfo(FTP);
56 return getFunctionInfo(cast<FunctionTypeNoProto>(FTy));
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000057}
58
Daniel Dunbar541b63b2009-02-02 23:23:47 +000059const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const ObjCMethodDecl *MD) {
60 llvm::SmallVector<QualType, 16> ArgTys;
61 ArgTys.push_back(MD->getSelfDecl()->getType());
62 ArgTys.push_back(Context.getObjCSelType());
63 // FIXME: Kill copy?
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000064 for (ObjCMethodDecl::param_const_iterator i = MD->param_begin(),
65 e = MD->param_end(); i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +000066 ArgTys.push_back((*i)->getType());
67 return getFunctionInfo(MD->getResultType(), ArgTys);
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000068}
69
Daniel Dunbar541b63b2009-02-02 23:23:47 +000070const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
71 const CallArgList &Args) {
72 // FIXME: Kill copy.
73 llvm::SmallVector<QualType, 16> ArgTys;
Daniel Dunbar725ad312009-01-31 02:19:00 +000074 for (CallArgList::const_iterator i = Args.begin(), e = Args.end();
75 i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +000076 ArgTys.push_back(i->second);
77 return getFunctionInfo(ResTy, ArgTys);
Daniel Dunbar725ad312009-01-31 02:19:00 +000078}
79
Daniel Dunbar541b63b2009-02-02 23:23:47 +000080const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
81 const FunctionArgList &Args) {
82 // FIXME: Kill copy.
83 llvm::SmallVector<QualType, 16> ArgTys;
Daniel Dunbarbb36d332009-02-02 21:43:58 +000084 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
85 i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +000086 ArgTys.push_back(i->second);
87 return getFunctionInfo(ResTy, ArgTys);
88}
89
Daniel Dunbar88c2fa92009-02-03 05:31:23 +000090static ABIArgInfo getABIReturnInfo(QualType Ty, CodeGenTypes &CGT);
91static ABIArgInfo getABIArgumentInfo(QualType Ty, CodeGenTypes &CGT);
92
Daniel Dunbar541b63b2009-02-02 23:23:47 +000093const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
94 const llvm::SmallVector<QualType, 16> &ArgTys) {
Daniel Dunbar40a6be62009-02-03 00:07:12 +000095 // Lookup or create unique function info.
96 llvm::FoldingSetNodeID ID;
97 CGFunctionInfo::Profile(ID, ResTy, ArgTys.begin(), ArgTys.end());
98
99 void *InsertPos = 0;
100 CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, InsertPos);
101 if (FI)
102 return *FI;
103
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000104 // Construct the function info.
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000105 FI = new CGFunctionInfo(ResTy, ArgTys);
106 FunctionInfos.InsertNode(FI, InsertPos);
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000107
108 // Compute ABI information.
109 FI->getReturnInfo() = getABIReturnInfo(ResTy, *this);
110 for (CGFunctionInfo::arg_iterator it = FI->arg_begin(), ie = FI->arg_end();
111 it != ie; ++it)
112 it->info = getABIArgumentInfo(it->type, *this);
113
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000114 return *FI;
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000115}
116
117/***/
118
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000119ABIInfo::~ABIInfo() {}
120
Daniel Dunbar834af452008-09-17 21:22:33 +0000121/// isEmptyStruct - Return true iff a structure has no non-empty
122/// members. Note that a structure with a flexible array member is not
123/// considered empty.
124static bool isEmptyStruct(QualType T) {
125 const RecordType *RT = T->getAsStructureType();
126 if (!RT)
127 return 0;
128 const RecordDecl *RD = RT->getDecl();
129 if (RD->hasFlexibleArrayMember())
130 return false;
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000131 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar834af452008-09-17 21:22:33 +0000132 e = RD->field_end(); i != e; ++i) {
133 const FieldDecl *FD = *i;
134 if (!isEmptyStruct(FD->getType()))
135 return false;
136 }
137 return true;
138}
139
140/// isSingleElementStruct - Determine if a structure is a "single
141/// element struct", i.e. it has exactly one non-empty field or
142/// exactly one field which is itself a single element
143/// struct. Structures with flexible array members are never
144/// considered single element structs.
145///
146/// \return The field declaration for the single non-empty field, if
147/// it exists.
148static const FieldDecl *isSingleElementStruct(QualType T) {
149 const RecordType *RT = T->getAsStructureType();
150 if (!RT)
151 return 0;
152
153 const RecordDecl *RD = RT->getDecl();
154 if (RD->hasFlexibleArrayMember())
155 return 0;
156
157 const FieldDecl *Found = 0;
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000158 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar834af452008-09-17 21:22:33 +0000159 e = RD->field_end(); i != e; ++i) {
160 const FieldDecl *FD = *i;
161 QualType FT = FD->getType();
162
163 if (isEmptyStruct(FT)) {
164 // Ignore
165 } else if (Found) {
166 return 0;
167 } else if (!CodeGenFunction::hasAggregateLLVMType(FT)) {
168 Found = FD;
169 } else {
170 Found = isSingleElementStruct(FT);
171 if (!Found)
172 return 0;
173 }
174 }
175
176 return Found;
177}
178
179static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
180 if (!Ty->getAsBuiltinType() && !Ty->isPointerType())
181 return false;
182
183 uint64_t Size = Context.getTypeSize(Ty);
184 return Size == 32 || Size == 64;
185}
186
187static bool areAllFields32Or64BitBasicType(const RecordDecl *RD,
188 ASTContext &Context) {
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000189 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar834af452008-09-17 21:22:33 +0000190 e = RD->field_end(); i != e; ++i) {
191 const FieldDecl *FD = *i;
192
193 if (!is32Or64BitBasicType(FD->getType(), Context))
194 return false;
195
196 // If this is a bit-field we need to make sure it is still a
197 // 32-bit or 64-bit type.
198 if (Expr *BW = FD->getBitWidth()) {
199 unsigned Width = BW->getIntegerConstantExprValue(Context).getZExtValue();
200 if (Width <= 16)
201 return false;
202 }
203 }
204 return true;
205}
206
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000207namespace {
208/// DefaultABIInfo - The default implementation for ABI specific
209/// details. This implementation provides information which results in
210/// sensible LLVM IR generation, but does not conform to any
211/// particular ABI.
212class DefaultABIInfo : public ABIInfo {
213 virtual ABIArgInfo classifyReturnType(QualType RetTy,
214 ASTContext &Context) const;
215
216 virtual ABIArgInfo classifyArgumentType(QualType RetTy,
217 ASTContext &Context) const;
218};
219
220/// X86_32ABIInfo - The X86-32 ABI information.
221class X86_32ABIInfo : public ABIInfo {
222public:
223 virtual ABIArgInfo classifyReturnType(QualType RetTy,
224 ASTContext &Context) const;
225
226 virtual ABIArgInfo classifyArgumentType(QualType RetTy,
227 ASTContext &Context) const;
228};
229}
230
231ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
232 ASTContext &Context) const {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000233 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar834af452008-09-17 21:22:33 +0000234 // Classify "single element" structs as their element type.
235 const FieldDecl *SeltFD = isSingleElementStruct(RetTy);
236 if (SeltFD) {
237 QualType SeltTy = SeltFD->getType()->getDesugaredType();
238 if (const BuiltinType *BT = SeltTy->getAsBuiltinType()) {
239 // FIXME: This is gross, it would be nice if we could just
240 // pass back SeltTy and have clients deal with it. Is it worth
241 // supporting coerce to both LLVM and clang Types?
242 if (BT->isIntegerType()) {
243 uint64_t Size = Context.getTypeSize(SeltTy);
244 return ABIArgInfo::getCoerce(llvm::IntegerType::get((unsigned) Size));
245 } else if (BT->getKind() == BuiltinType::Float) {
246 return ABIArgInfo::getCoerce(llvm::Type::FloatTy);
247 } else if (BT->getKind() == BuiltinType::Double) {
248 return ABIArgInfo::getCoerce(llvm::Type::DoubleTy);
249 }
250 } else if (SeltTy->isPointerType()) {
251 // FIXME: It would be really nice if this could come out as
252 // the proper pointer type.
253 llvm::Type *PtrTy =
254 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
255 return ABIArgInfo::getCoerce(PtrTy);
256 }
257 }
258
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000259 uint64_t Size = Context.getTypeSize(RetTy);
260 if (Size == 8) {
261 return ABIArgInfo::getCoerce(llvm::Type::Int8Ty);
262 } else if (Size == 16) {
263 return ABIArgInfo::getCoerce(llvm::Type::Int16Ty);
264 } else if (Size == 32) {
265 return ABIArgInfo::getCoerce(llvm::Type::Int32Ty);
266 } else if (Size == 64) {
267 return ABIArgInfo::getCoerce(llvm::Type::Int64Ty);
268 } else {
269 return ABIArgInfo::getStructRet();
270 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000271 } else {
272 return ABIArgInfo::getDefault();
273 }
274}
275
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000276ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
277 ASTContext &Context) const {
Daniel Dunbarf0357382008-09-17 20:11:04 +0000278 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar834af452008-09-17 21:22:33 +0000279 // Structures with flexible arrays are always byval.
280 if (const RecordType *RT = Ty->getAsStructureType())
281 if (RT->getDecl()->hasFlexibleArrayMember())
282 return ABIArgInfo::getByVal(0);
283
284 // Expand empty structs (i.e. ignore)
285 uint64_t Size = Context.getTypeSize(Ty);
286 if (Ty->isStructureType() && Size == 0)
287 return ABIArgInfo::getExpand();
288
289 // Expand structs with size <= 128-bits which consist only of
290 // basic types (int, long long, float, double, xxx*). This is
291 // non-recursive and does not ignore empty fields.
292 if (const RecordType *RT = Ty->getAsStructureType()) {
293 if (Context.getTypeSize(Ty) <= 4*32 &&
294 areAllFields32Or64BitBasicType(RT->getDecl(), Context))
295 return ABIArgInfo::getExpand();
296 }
297
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000298 return ABIArgInfo::getByVal(0);
299 } else {
300 return ABIArgInfo::getDefault();
301 }
302}
303
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000304namespace {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000305/// X86_64ABIInfo - The X86_64 ABI information.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000306class X86_64ABIInfo : public ABIInfo {
307 enum Class {
308 Integer = 0,
309 SSE,
310 SSEUp,
311 X87,
312 X87Up,
313 ComplexX87,
314 NoClass,
315 Memory
316 };
317
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000318 /// merge - Implement the X86_64 ABI merging algorithm.
319 ///
Daniel Dunbarc4503572009-01-31 00:06:58 +0000320 /// Merge an accumulating classification \arg Accum with a field
321 /// classification \arg Field.
322 ///
323 /// \param Accum - The accumulating classification. This should
324 /// always be either NoClass or the result of a previous merge
325 /// call. In addition, this should never be Memory (the caller
326 /// should just return Memory for the aggregate).
327 Class merge(Class Accum, Class Field) const;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000328
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000329 /// classify - Determine the x86_64 register classes in which the
330 /// given type T should be passed.
331 ///
Daniel Dunbarc4503572009-01-31 00:06:58 +0000332 /// \param Lo - The classification for the parts of the type
333 /// residing in the low word of the containing object.
334 ///
335 /// \param Hi - The classification for the parts of the type
336 /// residing in the high word of the containing object.
337 ///
338 /// \param OffsetBase - The bit offset of this type in the
Daniel Dunbarcdf920e2009-01-30 22:40:15 +0000339 /// containing object. Some parameters are classified different
340 /// depending on whether they straddle an eightbyte boundary.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000341 ///
342 /// If a word is unused its result will be NoClass; if a type should
343 /// be passed in Memory then at least the classification of \arg Lo
344 /// will be Memory.
345 ///
346 /// The \arg Lo class will be NoClass iff the argument is ignored.
347 ///
348 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
349 /// be NoClass.
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000350 void classify(QualType T, ASTContext &Context, uint64_t OffsetBase,
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000351 Class &Lo, Class &Hi) const;
Daniel Dunbarc4503572009-01-31 00:06:58 +0000352
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000353public:
354 virtual ABIArgInfo classifyReturnType(QualType RetTy,
355 ASTContext &Context) const;
356
357 virtual ABIArgInfo classifyArgumentType(QualType RetTy,
358 ASTContext &Context) const;
359};
360}
361
Daniel Dunbarc4503572009-01-31 00:06:58 +0000362X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum,
363 Class Field) const {
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000364 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
365 // classified recursively so that always two fields are
366 // considered. The resulting class is calculated according to
367 // the classes of the fields in the eightbyte:
368 //
369 // (a) If both classes are equal, this is the resulting class.
370 //
371 // (b) If one of the classes is NO_CLASS, the resulting class is
372 // the other class.
373 //
374 // (c) If one of the classes is MEMORY, the result is the MEMORY
375 // class.
376 //
377 // (d) If one of the classes is INTEGER, the result is the
378 // INTEGER.
379 //
380 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
381 // MEMORY is used as class.
382 //
383 // (f) Otherwise class SSE is used.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000384 assert((Accum == NoClass || Accum == Integer ||
385 Accum == SSE || Accum == SSEUp) &&
386 "Invalid accumulated classification during merge.");
387 if (Accum == Field || Field == NoClass)
388 return Accum;
389 else if (Field == Memory)
390 return Memory;
391 else if (Accum == NoClass)
392 return Field;
393 else if (Accum == Integer || Field == Integer)
394 return Integer;
395 else if (Field == X87 || Field == X87Up || Field == ComplexX87)
396 return Memory;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000397 else
Daniel Dunbarc4503572009-01-31 00:06:58 +0000398 return SSE;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000399}
400
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000401void X86_64ABIInfo::classify(QualType Ty,
402 ASTContext &Context,
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000403 uint64_t OffsetBase,
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000404 Class &Lo, Class &Hi) const {
Daniel Dunbar9a82b522009-02-02 18:06:39 +0000405 // FIXME: This code can be simplified by introducing a simple value
406 // class for Class pairs with appropriate constructor methods for
407 // the various situations.
408
Daniel Dunbarc4503572009-01-31 00:06:58 +0000409 Lo = Hi = NoClass;
410
411 Class &Current = OffsetBase < 64 ? Lo : Hi;
412 Current = Memory;
413
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000414 if (const BuiltinType *BT = Ty->getAsBuiltinType()) {
415 BuiltinType::Kind k = BT->getKind();
416
Daniel Dunbar11434922009-01-26 21:26:08 +0000417 if (k == BuiltinType::Void) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000418 Current = NoClass;
Daniel Dunbar11434922009-01-26 21:26:08 +0000419 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000420 Current = Integer;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000421 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000422 Current = SSE;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000423 } else if (k == BuiltinType::LongDouble) {
424 Lo = X87;
425 Hi = X87Up;
426 }
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000427 // FIXME: _Decimal32 and _Decimal64 are SSE.
428 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000429 // FIXME: __int128 is (Integer, Integer).
430 } else if (Ty->isPointerLikeType() || Ty->isBlockPointerType() ||
431 Ty->isObjCQualifiedInterfaceType()) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000432 Current = Integer;
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000433 } else if (const VectorType *VT = Ty->getAsVectorType()) {
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000434 uint64_t Size = Context.getTypeSize(VT);
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000435 if (Size == 64) {
Daniel Dunbard4cd1b02009-01-30 19:38:39 +0000436 // gcc passes <1 x double> in memory.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000437 if (VT->getElementType() == Context.DoubleTy)
Daniel Dunbard4cd1b02009-01-30 19:38:39 +0000438 return;
Daniel Dunbard4cd1b02009-01-30 19:38:39 +0000439
Daniel Dunbarc4503572009-01-31 00:06:58 +0000440 Current = SSE;
Daniel Dunbare33edf12009-01-30 18:40:10 +0000441
442 // If this type crosses an eightbyte boundary, it should be
443 // split.
Daniel Dunbarcdf920e2009-01-30 22:40:15 +0000444 if (OffsetBase && OffsetBase != 64)
Daniel Dunbare33edf12009-01-30 18:40:10 +0000445 Hi = Lo;
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000446 } else if (Size == 128) {
447 Lo = SSE;
448 Hi = SSEUp;
449 }
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000450 } else if (const ComplexType *CT = Ty->getAsComplexType()) {
451 QualType ET = CT->getElementType();
452
Daniel Dunbare33edf12009-01-30 18:40:10 +0000453 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbareac48dc2009-01-29 07:22:20 +0000454 if (ET->isIntegerType()) {
Daniel Dunbareac48dc2009-01-29 07:22:20 +0000455 if (Size <= 64)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000456 Current = Integer;
Daniel Dunbareac48dc2009-01-29 07:22:20 +0000457 else if (Size <= 128)
458 Lo = Hi = Integer;
459 } else if (ET == Context.FloatTy)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000460 Current = SSE;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000461 else if (ET == Context.DoubleTy)
462 Lo = Hi = SSE;
463 else if (ET == Context.LongDoubleTy)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000464 Current = ComplexX87;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000465
466 // If this complex type crosses an eightbyte boundary then it
467 // should be split.
Daniel Dunbarcdf920e2009-01-30 22:40:15 +0000468 uint64_t EB_Real = (OffsetBase) / 64;
469 uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000470 if (Hi == NoClass && EB_Real != EB_Imag)
471 Hi = Lo;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000472 } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
473 // Arrays are treated like structures.
474
475 uint64_t Size = Context.getTypeSize(Ty);
476
477 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
478 // than two eightbytes, ..., it has class MEMORY.
479 if (Size > 128)
480 return;
481
482 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
483 // fields, it has class MEMORY.
484 //
485 // Only need to check alignment of array base.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000486 if (OffsetBase % Context.getTypeAlign(AT->getElementType()))
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000487 return;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000488
489 // Otherwise implement simplified merge. We could be smarter about
490 // this, but it isn't worth it and would be harder to verify.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000491 Current = NoClass;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000492 uint64_t EltSize = Context.getTypeSize(AT->getElementType());
493 uint64_t ArraySize = AT->getSize().getZExtValue();
494 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
495 Class FieldLo, FieldHi;
496 classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbarc4503572009-01-31 00:06:58 +0000497 Lo = merge(Lo, FieldLo);
498 Hi = merge(Hi, FieldHi);
499 if (Lo == Memory || Hi == Memory)
500 break;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000501 }
Daniel Dunbarc4503572009-01-31 00:06:58 +0000502
503 // Do post merger cleanup (see below). Only case we worry about is Memory.
504 if (Hi == Memory)
505 Lo = Memory;
506 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Daniel Dunbar99037e52009-01-29 08:13:58 +0000507 } else if (const RecordType *RT = Ty->getAsRecordType()) {
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000508 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbar99037e52009-01-29 08:13:58 +0000509
510 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
511 // than two eightbytes, ..., it has class MEMORY.
512 if (Size > 128)
513 return;
514
515 const RecordDecl *RD = RT->getDecl();
516
517 // Assume variable sized types are passed in memory.
518 if (RD->hasFlexibleArrayMember())
519 return;
520
521 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
522
523 // Reset Lo class, this will be recomputed.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000524 Current = NoClass;
Daniel Dunbar99037e52009-01-29 08:13:58 +0000525 unsigned idx = 0;
526 for (RecordDecl::field_iterator i = RD->field_begin(),
527 e = RD->field_end(); i != e; ++i, ++idx) {
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000528 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Daniel Dunbar99037e52009-01-29 08:13:58 +0000529
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000530 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
531 // fields, it has class MEMORY.
Daniel Dunbar99037e52009-01-29 08:13:58 +0000532 if (Offset % Context.getTypeAlign(i->getType())) {
533 Lo = Memory;
534 return;
535 }
536
Daniel Dunbar99037e52009-01-29 08:13:58 +0000537 // Classify this field.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000538 //
539 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
540 // exceeds a single eightbyte, each is classified
541 // separately. Each eightbyte gets initialized to class
542 // NO_CLASS.
Daniel Dunbar99037e52009-01-29 08:13:58 +0000543 Class FieldLo, FieldHi;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000544 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbarc4503572009-01-31 00:06:58 +0000545 Lo = merge(Lo, FieldLo);
546 Hi = merge(Hi, FieldHi);
547 if (Lo == Memory || Hi == Memory)
548 break;
Daniel Dunbar99037e52009-01-29 08:13:58 +0000549 }
550
551 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
552 //
553 // (a) If one of the classes is MEMORY, the whole argument is
554 // passed in memory.
555 //
556 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
557
558 // The first of these conditions is guaranteed by how we implement
Daniel Dunbarc4503572009-01-31 00:06:58 +0000559 // the merge (just bail).
560 //
561 // The second condition occurs in the case of unions; for example
562 // union { _Complex double; unsigned; }.
563 if (Hi == Memory)
564 Lo = Memory;
Daniel Dunbar99037e52009-01-29 08:13:58 +0000565 if (Hi == SSEUp && Lo != SSE)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000566 Hi = SSE;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000567 }
568}
569
Daniel Dunbarc4503572009-01-31 00:06:58 +0000570
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000571ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
572 ASTContext &Context) const {
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000573 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
574 // classification algorithm.
575 X86_64ABIInfo::Class Lo, Hi;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000576 classify(RetTy, Context, 0, Lo, Hi);
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000577
Daniel Dunbarc4503572009-01-31 00:06:58 +0000578 // Check some invariants.
579 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
580 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
581 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
582
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000583 const llvm::Type *ResType = 0;
584 switch (Lo) {
585 case NoClass:
Daniel Dunbar11434922009-01-26 21:26:08 +0000586 return ABIArgInfo::getIgnore();
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000587
588 case SSEUp:
589 case X87Up:
590 assert(0 && "Invalid classification for lo word.");
591
Daniel Dunbarc4503572009-01-31 00:06:58 +0000592 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
593 // hidden argument, i.e. structret.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000594 case Memory:
595 return ABIArgInfo::getStructRet();
596
597 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
598 // available register of the sequence %rax, %rdx is used.
599 case Integer:
600 ResType = llvm::Type::Int64Ty; break;
601
602 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
603 // available SSE register of the sequence %xmm0, %xmm1 is used.
604 case SSE:
605 ResType = llvm::Type::DoubleTy; break;
606
607 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
608 // returned on the X87 stack in %st0 as 80-bit x87 number.
609 case X87:
610 ResType = llvm::Type::X86_FP80Ty; break;
611
Daniel Dunbarc4503572009-01-31 00:06:58 +0000612 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
613 // part of the value is returned in %st0 and the imaginary part in
614 // %st1.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000615 case ComplexX87:
616 assert(Hi == NoClass && "Unexpected ComplexX87 classification.");
617 ResType = llvm::VectorType::get(llvm::Type::X86_FP80Ty, 2);
618 break;
619 }
620
621 switch (Hi) {
622 // Memory was handled previously, and ComplexX87 and X87 should
623 // never occur as hi classes.
624 case Memory:
625 case X87:
626 case ComplexX87:
627 assert(0 && "Invalid classification for hi word.");
628
629 case NoClass: break;
630 case Integer:
Daniel Dunbarb0e14f22009-01-29 07:36:07 +0000631 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
632 break;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000633 case SSE:
Daniel Dunbarb0e14f22009-01-29 07:36:07 +0000634 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
635 break;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000636
637 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
638 // is passed in the upper half of the last used SSE register.
639 //
640 // SSEUP should always be preceeded by SSE, just widen.
641 case SSEUp:
642 assert(Lo == SSE && "Unexpected SSEUp classification.");
643 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
644 break;
645
646 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
Daniel Dunbarb0e14f22009-01-29 07:36:07 +0000647 // returned together with the previous X87 value in %st0.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000648 //
649 // X87UP should always be preceeded by X87, so we don't need to do
650 // anything here.
651 case X87Up:
652 assert(Lo == X87 && "Unexpected X87Up classification.");
653 break;
654 }
655
656 return ABIArgInfo::getCoerce(ResType);
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000657}
658
659ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty,
660 ASTContext &Context) const {
661 return ABIArgInfo::getDefault();
662}
663
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000664ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
665 ASTContext &Context) const {
666 return ABIArgInfo::getDefault();
667}
668
669ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
670 ASTContext &Context) const {
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000671 return ABIArgInfo::getDefault();
672}
673
674const ABIInfo &CodeGenTypes::getABIInfo() const {
675 if (TheABIInfo)
676 return *TheABIInfo;
677
678 // For now we just cache this in the CodeGenTypes and don't bother
679 // to free it.
680 const char *TargetPrefix = getContext().Target.getTargetPrefix();
681 if (strcmp(TargetPrefix, "x86") == 0) {
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000682 switch (getContext().Target.getPointerWidth(0)) {
683 case 32:
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000684 return *(TheABIInfo = new X86_32ABIInfo());
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000685 case 64:
Daniel Dunbar11a76ed2009-01-30 18:47:53 +0000686 return *(TheABIInfo = new X86_64ABIInfo());
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000687 }
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000688 }
689
690 return *(TheABIInfo = new DefaultABIInfo);
691}
692
693// getABIReturnInfo - Wrap the ABIInfo getABIReturnInfo, altering
694// "default" types to StructRet when appropriate for simplicity.
695static ABIArgInfo getABIReturnInfo(QualType Ty, CodeGenTypes &CGT) {
696 assert(!Ty->isArrayType() &&
697 "Array types cannot be passed directly.");
698 ABIArgInfo Info = CGT.getABIInfo().classifyReturnType(Ty, CGT.getContext());
Daniel Dunbarf0357382008-09-17 20:11:04 +0000699 // Ensure default on aggregate types is StructRet.
700 if (Info.isDefault() && CodeGenFunction::hasAggregateLLVMType(Ty))
701 return ABIArgInfo::getStructRet();
702 return Info;
703}
704
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000705// getABIArgumentInfo - Wrap the ABIInfo getABIReturnInfo, altering
706// "default" types to ByVal when appropriate for simplicity.
707static ABIArgInfo getABIArgumentInfo(QualType Ty, CodeGenTypes &CGT) {
708 assert(!Ty->isArrayType() &&
709 "Array types cannot be passed directly.");
710 ABIArgInfo Info = CGT.getABIInfo().classifyArgumentType(Ty, CGT.getContext());
Daniel Dunbarf0357382008-09-17 20:11:04 +0000711 // Ensure default on aggregate types is ByVal.
712 if (Info.isDefault() && CodeGenFunction::hasAggregateLLVMType(Ty))
713 return ABIArgInfo::getByVal(0);
714 return Info;
715}
716
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000717/***/
718
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000719CGFunctionInfo::CGFunctionInfo(QualType ResTy,
720 const llvm::SmallVector<QualType, 16> &ArgTys) {
721 NumArgs = ArgTys.size();
722 Args = new ArgInfo[1 + NumArgs];
723 Args[0].type = ResTy;
724 for (unsigned i = 0; i < NumArgs; ++i)
725 Args[1 + i].type = ArgTys[i];
726}
727
728/***/
729
Daniel Dunbar56273772008-09-17 00:51:38 +0000730void CodeGenTypes::GetExpandedTypes(QualType Ty,
731 std::vector<const llvm::Type*> &ArgTys) {
732 const RecordType *RT = Ty->getAsStructureType();
733 assert(RT && "Can only expand structure types.");
734 const RecordDecl *RD = RT->getDecl();
735 assert(!RD->hasFlexibleArrayMember() &&
736 "Cannot expand structure with flexible array.");
737
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000738 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar56273772008-09-17 00:51:38 +0000739 e = RD->field_end(); i != e; ++i) {
740 const FieldDecl *FD = *i;
741 assert(!FD->isBitField() &&
742 "Cannot expand structure with bit-field members.");
743
744 QualType FT = FD->getType();
745 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
746 GetExpandedTypes(FT, ArgTys);
747 } else {
748 ArgTys.push_back(ConvertType(FT));
749 }
750 }
751}
752
753llvm::Function::arg_iterator
754CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
755 llvm::Function::arg_iterator AI) {
756 const RecordType *RT = Ty->getAsStructureType();
757 assert(RT && "Can only expand structure types.");
758
759 RecordDecl *RD = RT->getDecl();
760 assert(LV.isSimple() &&
761 "Unexpected non-simple lvalue during struct expansion.");
762 llvm::Value *Addr = LV.getAddress();
763 for (RecordDecl::field_iterator i = RD->field_begin(),
764 e = RD->field_end(); i != e; ++i) {
765 FieldDecl *FD = *i;
766 QualType FT = FD->getType();
767
768 // FIXME: What are the right qualifiers here?
769 LValue LV = EmitLValueForField(Addr, FD, false, 0);
770 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
771 AI = ExpandTypeFromArgs(FT, LV, AI);
772 } else {
773 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
774 ++AI;
775 }
776 }
777
778 return AI;
779}
780
781void
782CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
783 llvm::SmallVector<llvm::Value*, 16> &Args) {
784 const RecordType *RT = Ty->getAsStructureType();
785 assert(RT && "Can only expand structure types.");
786
787 RecordDecl *RD = RT->getDecl();
788 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
789 llvm::Value *Addr = RV.getAggregateAddr();
790 for (RecordDecl::field_iterator i = RD->field_begin(),
791 e = RD->field_end(); i != e; ++i) {
792 FieldDecl *FD = *i;
793 QualType FT = FD->getType();
794
795 // FIXME: What are the right qualifiers here?
796 LValue LV = EmitLValueForField(Addr, FD, false, 0);
797 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
798 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
799 } else {
800 RValue RV = EmitLoadOfLValue(LV, FT);
801 assert(RV.isScalar() &&
802 "Unexpected non-scalar rvalue during struct expansion.");
803 Args.push_back(RV.getScalarVal());
804 }
805 }
806}
807
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000808/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
809/// a pointer to an object of type \arg Ty.
810///
811/// This safely handles the case when the src type is smaller than the
812/// destination type; in this situation the values of bits which not
813/// present in the src are undefined.
814static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
815 const llvm::Type *Ty,
816 CodeGenFunction &CGF) {
817 const llvm::Type *SrcTy =
818 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
819 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
820 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(Ty);
821
Daniel Dunbarb225be42009-02-03 05:59:18 +0000822 // If load is legal, just bitcast the src pointer.
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000823 if (SrcSize == DstSize) {
824 llvm::Value *Casted =
825 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
826 return CGF.Builder.CreateLoad(Casted);
827 } else {
828 assert(SrcSize < DstSize && "Coercion is losing source bits!");
829
830 // Otherwise do coercion through memory. This is stupid, but
831 // simple.
832 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
833 llvm::Value *Casted =
834 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
835 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
836 return CGF.Builder.CreateLoad(Tmp);
837 }
838}
839
840/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
841/// where the source and destination may have different types.
842///
843/// This safely handles the case when the src type is larger than the
844/// destination type; the upper bits of the src will be lost.
845static void CreateCoercedStore(llvm::Value *Src,
846 llvm::Value *DstPtr,
847 CodeGenFunction &CGF) {
848 const llvm::Type *SrcTy = Src->getType();
849 const llvm::Type *DstTy =
850 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
851
852 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
853 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(DstTy);
854
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000855 // If store is legal, just bitcast the src pointer.
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000856 if (SrcSize == DstSize) {
857 llvm::Value *Casted =
858 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
859 CGF.Builder.CreateStore(Src, Casted);
860 } else {
861 assert(SrcSize > DstSize && "Coercion is missing bits!");
862
863 // Otherwise do coercion through memory. This is stupid, but
864 // simple.
865 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
866 CGF.Builder.CreateStore(Src, Tmp);
867 llvm::Value *Casted =
868 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
869 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(Casted), DstPtr);
870 }
871}
872
Daniel Dunbar56273772008-09-17 00:51:38 +0000873/***/
874
Daniel Dunbar88b53962009-02-02 22:03:45 +0000875bool CodeGenModule::ReturnTypeUsesSret(const CGFunctionInfo &FI) {
Daniel Dunbarb225be42009-02-03 05:59:18 +0000876 return FI.getReturnInfo().isStructRet();
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000877}
878
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000879const llvm::FunctionType *
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000880CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic) {
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000881 std::vector<const llvm::Type*> ArgTys;
882
883 const llvm::Type *ResultType = 0;
884
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000885 QualType RetTy = FI.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000886 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000887 switch (RetAI.getKind()) {
888 case ABIArgInfo::ByVal:
889 case ABIArgInfo::Expand:
890 assert(0 && "Invalid ABI kind for return argument");
891
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000892 case ABIArgInfo::Default:
893 if (RetTy->isVoidType()) {
894 ResultType = llvm::Type::VoidTy;
895 } else {
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000896 ResultType = ConvertType(RetTy);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000897 }
898 break;
899
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000900 case ABIArgInfo::Direct:
901 ResultType = ConvertType(RetTy);
902 break;
903
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000904 case ABIArgInfo::StructRet: {
905 ResultType = llvm::Type::VoidTy;
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000906 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000907 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
908 break;
909 }
910
Daniel Dunbar11434922009-01-26 21:26:08 +0000911 case ABIArgInfo::Ignore:
912 ResultType = llvm::Type::VoidTy;
913 break;
914
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000915 case ABIArgInfo::Coerce:
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000916 ResultType = RetAI.getCoerceToType();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000917 break;
918 }
919
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000920 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
921 ie = FI.arg_end(); it != ie; ++it) {
922 const ABIArgInfo &AI = it->info;
923 const llvm::Type *Ty = ConvertType(it->type);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000924
925 switch (AI.getKind()) {
Daniel Dunbar11434922009-01-26 21:26:08 +0000926 case ABIArgInfo::Ignore:
927 break;
928
Daniel Dunbar56273772008-09-17 00:51:38 +0000929 case ABIArgInfo::Coerce:
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000930 case ABIArgInfo::StructRet:
931 assert(0 && "Invalid ABI kind for non-return argument");
932
933 case ABIArgInfo::ByVal:
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000934 // byval arguments are always on the stack, which is addr space #0.
935 ArgTys.push_back(llvm::PointerType::getUnqual(Ty));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000936 assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled");
937 break;
938
939 case ABIArgInfo::Default:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000940 case ABIArgInfo::Direct:
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000941 ArgTys.push_back(Ty);
942 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000943
944 case ABIArgInfo::Expand:
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000945 GetExpandedTypes(it->type, ArgTys);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000946 break;
947 }
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000948 }
949
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000950 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar3913f182008-09-09 23:48:28 +0000951}
952
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000953void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
Daniel Dunbar88b53962009-02-02 22:03:45 +0000954 const Decl *TargetDecl,
Devang Patel761d7f72008-09-25 21:02:23 +0000955 AttributeListType &PAL) {
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000956 unsigned FuncAttrs = 0;
Devang Patela2c69122008-09-26 22:53:57 +0000957 unsigned RetAttrs = 0;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000958
959 if (TargetDecl) {
960 if (TargetDecl->getAttr<NoThrowAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +0000961 FuncAttrs |= llvm::Attribute::NoUnwind;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000962 if (TargetDecl->getAttr<NoReturnAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +0000963 FuncAttrs |= llvm::Attribute::NoReturn;
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000964 if (TargetDecl->getAttr<PureAttr>())
965 FuncAttrs |= llvm::Attribute::ReadOnly;
966 if (TargetDecl->getAttr<ConstAttr>())
967 FuncAttrs |= llvm::Attribute::ReadNone;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000968 }
969
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000970 QualType RetTy = FI.getReturnType();
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000971 unsigned Index = 1;
Daniel Dunbarb225be42009-02-03 05:59:18 +0000972 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000973 switch (RetAI.getKind()) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000974 case ABIArgInfo::Default:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000975 case ABIArgInfo::Direct:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000976 if (RetTy->isPromotableIntegerType()) {
977 if (RetTy->isSignedIntegerType()) {
Devang Patela2c69122008-09-26 22:53:57 +0000978 RetAttrs |= llvm::Attribute::SExt;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000979 } else if (RetTy->isUnsignedIntegerType()) {
Devang Patela2c69122008-09-26 22:53:57 +0000980 RetAttrs |= llvm::Attribute::ZExt;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000981 }
982 }
983 break;
984
985 case ABIArgInfo::StructRet:
Devang Patel761d7f72008-09-25 21:02:23 +0000986 PAL.push_back(llvm::AttributeWithIndex::get(Index,
Daniel Dunbar725ad312009-01-31 02:19:00 +0000987 llvm::Attribute::StructRet |
988 llvm::Attribute::NoAlias));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000989 ++Index;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000990 break;
991
Daniel Dunbar11434922009-01-26 21:26:08 +0000992 case ABIArgInfo::Ignore:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000993 case ABIArgInfo::Coerce:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000994 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000995
996 case ABIArgInfo::ByVal:
997 case ABIArgInfo::Expand:
998 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000999 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001000
Devang Patela2c69122008-09-26 22:53:57 +00001001 if (RetAttrs)
1002 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Daniel Dunbar88c2fa92009-02-03 05:31:23 +00001003 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1004 ie = FI.arg_end(); it != ie; ++it) {
1005 QualType ParamType = it->type;
1006 const ABIArgInfo &AI = it->info;
Devang Patel761d7f72008-09-25 21:02:23 +00001007 unsigned Attributes = 0;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001008
1009 switch (AI.getKind()) {
1010 case ABIArgInfo::StructRet:
Daniel Dunbar56273772008-09-17 00:51:38 +00001011 case ABIArgInfo::Coerce:
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001012 assert(0 && "Invalid ABI kind for non-return argument");
1013
1014 case ABIArgInfo::ByVal:
Devang Patel761d7f72008-09-25 21:02:23 +00001015 Attributes |= llvm::Attribute::ByVal;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001016 assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled");
1017 break;
1018
1019 case ABIArgInfo::Default:
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001020 case ABIArgInfo::Direct:
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001021 if (ParamType->isPromotableIntegerType()) {
1022 if (ParamType->isSignedIntegerType()) {
Devang Patel761d7f72008-09-25 21:02:23 +00001023 Attributes |= llvm::Attribute::SExt;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001024 } else if (ParamType->isUnsignedIntegerType()) {
Devang Patel761d7f72008-09-25 21:02:23 +00001025 Attributes |= llvm::Attribute::ZExt;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001026 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001027 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001028 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001029
Daniel Dunbar11434922009-01-26 21:26:08 +00001030 case ABIArgInfo::Ignore:
1031 // Skip increment, no matching LLVM parameter.
1032 continue;
1033
Daniel Dunbar56273772008-09-17 00:51:38 +00001034 case ABIArgInfo::Expand: {
1035 std::vector<const llvm::Type*> Tys;
1036 // FIXME: This is rather inefficient. Do we ever actually need
1037 // to do anything here? The result should be just reconstructed
1038 // on the other side, so extension should be a non-issue.
1039 getTypes().GetExpandedTypes(ParamType, Tys);
1040 Index += Tys.size();
1041 continue;
1042 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001043 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001044
Devang Patel761d7f72008-09-25 21:02:23 +00001045 if (Attributes)
1046 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar56273772008-09-17 00:51:38 +00001047 ++Index;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001048 }
Devang Patela2c69122008-09-26 22:53:57 +00001049 if (FuncAttrs)
1050 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
1051
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001052}
1053
Daniel Dunbar88b53962009-02-02 22:03:45 +00001054void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
1055 llvm::Function *Fn,
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001056 const FunctionArgList &Args) {
Daniel Dunbar5251afa2009-02-03 06:02:10 +00001057 // FIXME: We no longer need the types from FunctionArgList; lift up
1058 // and simplify.
1059
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001060 // Emit allocs for param decls. Give the LLVM Argument nodes names.
1061 llvm::Function::arg_iterator AI = Fn->arg_begin();
1062
1063 // Name the struct return argument.
Daniel Dunbar88b53962009-02-02 22:03:45 +00001064 if (CGM.ReturnTypeUsesSret(FI)) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001065 AI->setName("agg.result");
1066 ++AI;
1067 }
Daniel Dunbarb225be42009-02-03 05:59:18 +00001068
1069 CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001070 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001071 i != e; ++i, ++info_it) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001072 const VarDecl *Arg = i->first;
Daniel Dunbarb225be42009-02-03 05:59:18 +00001073 QualType Ty = info_it->type;
1074 const ABIArgInfo &ArgI = info_it->info;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001075
1076 switch (ArgI.getKind()) {
1077 case ABIArgInfo::ByVal:
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001078 case ABIArgInfo::Default:
1079 case ABIArgInfo::Direct: {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001080 assert(AI != Fn->arg_end() && "Argument mismatch!");
1081 llvm::Value* V = AI;
Daniel Dunbar56273772008-09-17 00:51:38 +00001082 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001083 // This must be a promotion, for something like
1084 // "void a(x) short x; {..."
Daniel Dunbar56273772008-09-17 00:51:38 +00001085 V = EmitScalarConversion(V, Ty, Arg->getType());
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001086 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001087 EmitParmDecl(*Arg, V);
1088 break;
1089 }
Daniel Dunbar56273772008-09-17 00:51:38 +00001090
1091 case ABIArgInfo::Expand: {
Daniel Dunbarb225be42009-02-03 05:59:18 +00001092 // If this structure was expanded into multiple arguments then
Daniel Dunbar56273772008-09-17 00:51:38 +00001093 // we need to create a temporary and reconstruct it from the
1094 // arguments.
Chris Lattner39f34e92008-11-24 04:00:27 +00001095 std::string Name = Arg->getNameAsString();
Daniel Dunbar56273772008-09-17 00:51:38 +00001096 llvm::Value *Temp = CreateTempAlloca(ConvertType(Ty),
1097 (Name + ".addr").c_str());
1098 // FIXME: What are the right qualifiers here?
1099 llvm::Function::arg_iterator End =
1100 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI);
1101 EmitParmDecl(*Arg, Temp);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001102
Daniel Dunbar56273772008-09-17 00:51:38 +00001103 // Name the arguments used in expansion and increment AI.
1104 unsigned Index = 0;
1105 for (; AI != End; ++AI, ++Index)
1106 AI->setName(Name + "." + llvm::utostr(Index));
1107 continue;
1108 }
Daniel Dunbar11434922009-01-26 21:26:08 +00001109
1110 case ABIArgInfo::Ignore:
1111 break;
1112
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001113 case ABIArgInfo::Coerce:
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001114 case ABIArgInfo::StructRet:
1115 assert(0 && "Invalid ABI kind for non-return argument");
1116 }
Daniel Dunbar56273772008-09-17 00:51:38 +00001117
1118 ++AI;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001119 }
1120 assert(AI == Fn->arg_end() && "Argument mismatch!");
1121}
1122
Daniel Dunbar88b53962009-02-02 22:03:45 +00001123void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI,
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001124 llvm::Value *ReturnValue) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001125 llvm::Value *RV = 0;
1126
1127 // Functions with no result always return void.
1128 if (ReturnValue) {
Daniel Dunbar88b53962009-02-02 22:03:45 +00001129 QualType RetTy = FI.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001130 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001131
1132 switch (RetAI.getKind()) {
1133 case ABIArgInfo::StructRet:
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001134 if (RetTy->isAnyComplexType()) {
1135 // FIXME: Volatile
1136 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
1137 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
1138 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1139 EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy);
1140 } else {
1141 Builder.CreateStore(Builder.CreateLoad(ReturnValue),
1142 CurFn->arg_begin());
1143 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001144 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001145
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001146 case ABIArgInfo::Default:
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001147 case ABIArgInfo::Direct:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001148 RV = Builder.CreateLoad(ReturnValue);
1149 break;
1150
Daniel Dunbar11434922009-01-26 21:26:08 +00001151 case ABIArgInfo::Ignore:
1152 break;
1153
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001154 case ABIArgInfo::Coerce: {
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +00001155 RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001156 break;
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001157 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001158
1159 case ABIArgInfo::ByVal:
1160 case ABIArgInfo::Expand:
1161 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001162 }
1163 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001164
1165 if (RV) {
1166 Builder.CreateRet(RV);
1167 } else {
1168 Builder.CreateRetVoid();
1169 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001170}
1171
Daniel Dunbar88b53962009-02-02 22:03:45 +00001172RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
1173 llvm::Value *Callee,
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001174 const CallArgList &CallArgs) {
Daniel Dunbar5251afa2009-02-03 06:02:10 +00001175 // FIXME: We no longer need the types from CallArgs; lift up and
1176 // simplify.
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001177 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001178
1179 // Handle struct-return functions by passing a pointer to the
1180 // location that we would like to return into.
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001181 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001182 const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001183 switch (RetAI.getKind()) {
1184 case ABIArgInfo::StructRet:
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001185 // Create a temporary alloca to hold the result of the call. :(
Daniel Dunbar56273772008-09-17 00:51:38 +00001186 Args.push_back(CreateTempAlloca(ConvertType(RetTy)));
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001187 break;
1188
1189 case ABIArgInfo::Default:
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001190 case ABIArgInfo::Direct:
Daniel Dunbar11434922009-01-26 21:26:08 +00001191 case ABIArgInfo::Ignore:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001192 case ABIArgInfo::Coerce:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001193 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001194
1195 case ABIArgInfo::ByVal:
1196 case ABIArgInfo::Expand:
Daniel Dunbarb225be42009-02-03 05:59:18 +00001197 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001198 }
1199
Daniel Dunbarb225be42009-02-03 05:59:18 +00001200 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001201 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001202 I != E; ++I, ++info_it) {
1203 const ABIArgInfo &ArgInfo = info_it->info;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001204 RValue RV = I->first;
Daniel Dunbar56273772008-09-17 00:51:38 +00001205
1206 switch (ArgInfo.getKind()) {
1207 case ABIArgInfo::ByVal: // Default is byval
1208 case ABIArgInfo::Default:
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001209 case ABIArgInfo::Direct:
Daniel Dunbar56273772008-09-17 00:51:38 +00001210 if (RV.isScalar()) {
1211 Args.push_back(RV.getScalarVal());
1212 } else if (RV.isComplex()) {
1213 // Make a temporary alloca to pass the argument.
1214 Args.push_back(CreateTempAlloca(ConvertType(I->second)));
1215 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
1216 } else {
1217 Args.push_back(RV.getAggregateAddr());
1218 }
1219 break;
1220
Daniel Dunbar11434922009-01-26 21:26:08 +00001221 case ABIArgInfo::Ignore:
1222 break;
1223
Daniel Dunbar56273772008-09-17 00:51:38 +00001224 case ABIArgInfo::StructRet:
1225 case ABIArgInfo::Coerce:
1226 assert(0 && "Invalid ABI kind for non-return argument");
1227 break;
1228
1229 case ABIArgInfo::Expand:
1230 ExpandTypeToArgs(I->second, RV, Args);
1231 break;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001232 }
1233 }
1234
1235 llvm::CallInst *CI = Builder.CreateCall(Callee,&Args[0],&Args[0]+Args.size());
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001236
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001237 // FIXME: Provide TargetDecl so nounwind, noreturn, etc, etc get set.
Devang Patel761d7f72008-09-25 21:02:23 +00001238 CodeGen::AttributeListType AttributeList;
Daniel Dunbar88b53962009-02-02 22:03:45 +00001239 CGM.ConstructAttributeList(CallInfo, 0, AttributeList);
Devang Patel761d7f72008-09-25 21:02:23 +00001240 CI->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(),
Daniel Dunbar725ad312009-01-31 02:19:00 +00001241 AttributeList.size()));
1242
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001243 if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee))
1244 CI->setCallingConv(F->getCallingConv());
1245 if (CI->getType() != llvm::Type::VoidTy)
1246 CI->setName("call");
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001247
1248 switch (RetAI.getKind()) {
1249 case ABIArgInfo::StructRet:
1250 if (RetTy->isAnyComplexType())
Daniel Dunbar56273772008-09-17 00:51:38 +00001251 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001252 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar56273772008-09-17 00:51:38 +00001253 return RValue::getAggregate(Args[0]);
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001254 else
1255 return RValue::get(Builder.CreateLoad(Args[0]));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001256
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001257 case ABIArgInfo::Default:
1258 return RValue::get(RetTy->isVoidType() ? 0 : CI);
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001259
1260 case ABIArgInfo::Direct:
1261 assert((!RetTy->isAnyComplexType() &&
1262 CodeGenFunction::hasAggregateLLVMType(RetTy)) &&
1263 "FIXME: Implemented return for non-scalar direct types.");
1264 return RValue::get(CI);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001265
Daniel Dunbar11434922009-01-26 21:26:08 +00001266 case ABIArgInfo::Ignore:
Daniel Dunbarcc039fe2009-01-29 08:24:57 +00001267 if (RetTy->isVoidType())
1268 return RValue::get(0);
1269 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1270 llvm::Value *Res =
1271 llvm::UndefValue::get(llvm::PointerType::getUnqual(ConvertType(RetTy)));
1272 return RValue::getAggregate(Res);
1273 }
1274 return RValue::get(llvm::UndefValue::get(ConvertType(RetTy)));
Daniel Dunbar11434922009-01-26 21:26:08 +00001275
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001276 case ABIArgInfo::Coerce: {
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +00001277 llvm::Value *V = CreateTempAlloca(ConvertType(RetTy), "coerce");
1278 CreateCoercedStore(CI, V, *this);
Anders Carlssonad3d6912008-11-25 22:21:48 +00001279 if (RetTy->isAnyComplexType())
1280 return RValue::getComplex(LoadComplexFromAddr(V, false));
Daniel Dunbar11434922009-01-26 21:26:08 +00001281 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssonad3d6912008-11-25 22:21:48 +00001282 return RValue::getAggregate(V);
Daniel Dunbar11434922009-01-26 21:26:08 +00001283 else
1284 return RValue::get(Builder.CreateLoad(V));
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001285 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001286
1287 case ABIArgInfo::ByVal:
1288 case ABIArgInfo::Expand:
1289 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001290 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001291
1292 assert(0 && "Unhandled ABIArgInfo::Kind");
1293 return RValue::get(0);
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001294}