blob: fa967590bad1a39ddb1b32c5681137116b31835d [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
90const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
91 const llvm::SmallVector<QualType, 16> &ArgTys) {
Daniel Dunbar40a6be62009-02-03 00:07:12 +000092 // Lookup or create unique function info.
93 llvm::FoldingSetNodeID ID;
94 CGFunctionInfo::Profile(ID, ResTy, ArgTys.begin(), ArgTys.end());
95
96 void *InsertPos = 0;
97 CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, InsertPos);
98 if (FI)
99 return *FI;
100
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000101 // Construct the function info.
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000102 FI = new CGFunctionInfo(ResTy, ArgTys);
103 FunctionInfos.InsertNode(FI, InsertPos);
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000104
105 // Compute ABI information.
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000106 getABIInfo().computeInfo(*FI, getContext());
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000107
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000108 return *FI;
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000109}
110
111/***/
112
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000113ABIInfo::~ABIInfo() {}
114
Daniel Dunbar834af452008-09-17 21:22:33 +0000115/// isEmptyStruct - Return true iff a structure has no non-empty
116/// members. Note that a structure with a flexible array member is not
117/// considered empty.
118static bool isEmptyStruct(QualType T) {
119 const RecordType *RT = T->getAsStructureType();
120 if (!RT)
121 return 0;
122 const RecordDecl *RD = RT->getDecl();
123 if (RD->hasFlexibleArrayMember())
124 return false;
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000125 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar834af452008-09-17 21:22:33 +0000126 e = RD->field_end(); i != e; ++i) {
127 const FieldDecl *FD = *i;
128 if (!isEmptyStruct(FD->getType()))
129 return false;
130 }
131 return true;
132}
133
134/// isSingleElementStruct - Determine if a structure is a "single
135/// element struct", i.e. it has exactly one non-empty field or
136/// exactly one field which is itself a single element
137/// struct. Structures with flexible array members are never
138/// considered single element structs.
139///
140/// \return The field declaration for the single non-empty field, if
141/// it exists.
142static const FieldDecl *isSingleElementStruct(QualType T) {
143 const RecordType *RT = T->getAsStructureType();
144 if (!RT)
145 return 0;
146
147 const RecordDecl *RD = RT->getDecl();
148 if (RD->hasFlexibleArrayMember())
149 return 0;
150
151 const FieldDecl *Found = 0;
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000152 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar834af452008-09-17 21:22:33 +0000153 e = RD->field_end(); i != e; ++i) {
154 const FieldDecl *FD = *i;
155 QualType FT = FD->getType();
156
157 if (isEmptyStruct(FT)) {
158 // Ignore
159 } else if (Found) {
160 return 0;
161 } else if (!CodeGenFunction::hasAggregateLLVMType(FT)) {
162 Found = FD;
163 } else {
164 Found = isSingleElementStruct(FT);
165 if (!Found)
166 return 0;
167 }
168 }
169
170 return Found;
171}
172
173static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
174 if (!Ty->getAsBuiltinType() && !Ty->isPointerType())
175 return false;
176
177 uint64_t Size = Context.getTypeSize(Ty);
178 return Size == 32 || Size == 64;
179}
180
181static bool areAllFields32Or64BitBasicType(const RecordDecl *RD,
182 ASTContext &Context) {
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000183 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar834af452008-09-17 21:22:33 +0000184 e = RD->field_end(); i != e; ++i) {
185 const FieldDecl *FD = *i;
186
187 if (!is32Or64BitBasicType(FD->getType(), Context))
188 return false;
189
190 // If this is a bit-field we need to make sure it is still a
191 // 32-bit or 64-bit type.
192 if (Expr *BW = FD->getBitWidth()) {
193 unsigned Width = BW->getIntegerConstantExprValue(Context).getZExtValue();
194 if (Width <= 16)
195 return false;
196 }
197 }
198 return true;
199}
200
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000201namespace {
202/// DefaultABIInfo - The default implementation for ABI specific
203/// details. This implementation provides information which results in
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000204/// self-consistent and sensible LLVM IR generation, but does not
205/// conform to any particular ABI.
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000206class DefaultABIInfo : public ABIInfo {
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000207 ABIArgInfo classifyReturnType(QualType RetTy,
208 ASTContext &Context) const;
209
210 ABIArgInfo classifyArgumentType(QualType RetTy,
211 ASTContext &Context) const;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000212
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000213 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
214 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
215 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
216 it != ie; ++it)
217 it->info = classifyArgumentType(it->type, Context);
218 }
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000219};
220
221/// X86_32ABIInfo - The X86-32 ABI information.
222class X86_32ABIInfo : public ABIInfo {
223public:
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000224 ABIArgInfo classifyReturnType(QualType RetTy,
225 ASTContext &Context) const;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000226
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000227 ABIArgInfo classifyArgumentType(QualType RetTy,
228 ASTContext &Context) const;
229
230 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
231 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
232 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
233 it != ie; ++it)
234 it->info = classifyArgumentType(it->type, Context);
235 }
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000236};
237}
238
239ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
240 ASTContext &Context) const {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000241 if (RetTy->isVoidType()) {
242 return ABIArgInfo::getIgnore();
243 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar834af452008-09-17 21:22:33 +0000244 // Classify "single element" structs as their element type.
245 const FieldDecl *SeltFD = isSingleElementStruct(RetTy);
246 if (SeltFD) {
247 QualType SeltTy = SeltFD->getType()->getDesugaredType();
248 if (const BuiltinType *BT = SeltTy->getAsBuiltinType()) {
249 // FIXME: This is gross, it would be nice if we could just
250 // pass back SeltTy and have clients deal with it. Is it worth
251 // supporting coerce to both LLVM and clang Types?
252 if (BT->isIntegerType()) {
253 uint64_t Size = Context.getTypeSize(SeltTy);
254 return ABIArgInfo::getCoerce(llvm::IntegerType::get((unsigned) Size));
255 } else if (BT->getKind() == BuiltinType::Float) {
256 return ABIArgInfo::getCoerce(llvm::Type::FloatTy);
257 } else if (BT->getKind() == BuiltinType::Double) {
258 return ABIArgInfo::getCoerce(llvm::Type::DoubleTy);
259 }
260 } else if (SeltTy->isPointerType()) {
261 // FIXME: It would be really nice if this could come out as
262 // the proper pointer type.
263 llvm::Type *PtrTy =
264 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
265 return ABIArgInfo::getCoerce(PtrTy);
266 }
267 }
268
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000269 uint64_t Size = Context.getTypeSize(RetTy);
270 if (Size == 8) {
271 return ABIArgInfo::getCoerce(llvm::Type::Int8Ty);
272 } else if (Size == 16) {
273 return ABIArgInfo::getCoerce(llvm::Type::Int16Ty);
274 } else if (Size == 32) {
275 return ABIArgInfo::getCoerce(llvm::Type::Int32Ty);
276 } else if (Size == 64) {
277 return ABIArgInfo::getCoerce(llvm::Type::Int64Ty);
278 } else {
279 return ABIArgInfo::getStructRet();
280 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000281 } else {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000282 return ABIArgInfo::getDirect();
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000283 }
284}
285
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000286ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
287 ASTContext &Context) const {
Daniel Dunbarf0357382008-09-17 20:11:04 +0000288 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar834af452008-09-17 21:22:33 +0000289 // Structures with flexible arrays are always byval.
290 if (const RecordType *RT = Ty->getAsStructureType())
291 if (RT->getDecl()->hasFlexibleArrayMember())
292 return ABIArgInfo::getByVal(0);
293
294 // Expand empty structs (i.e. ignore)
295 uint64_t Size = Context.getTypeSize(Ty);
296 if (Ty->isStructureType() && Size == 0)
297 return ABIArgInfo::getExpand();
298
299 // Expand structs with size <= 128-bits which consist only of
300 // basic types (int, long long, float, double, xxx*). This is
301 // non-recursive and does not ignore empty fields.
302 if (const RecordType *RT = Ty->getAsStructureType()) {
303 if (Context.getTypeSize(Ty) <= 4*32 &&
304 areAllFields32Or64BitBasicType(RT->getDecl(), Context))
305 return ABIArgInfo::getExpand();
306 }
307
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000308 return ABIArgInfo::getByVal(0);
309 } else {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000310 return ABIArgInfo::getDirect();
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000311 }
312}
313
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000314namespace {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000315/// X86_64ABIInfo - The X86_64 ABI information.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000316class X86_64ABIInfo : public ABIInfo {
317 enum Class {
318 Integer = 0,
319 SSE,
320 SSEUp,
321 X87,
322 X87Up,
323 ComplexX87,
324 NoClass,
325 Memory
326 };
327
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000328 /// merge - Implement the X86_64 ABI merging algorithm.
329 ///
Daniel Dunbarc4503572009-01-31 00:06:58 +0000330 /// Merge an accumulating classification \arg Accum with a field
331 /// classification \arg Field.
332 ///
333 /// \param Accum - The accumulating classification. This should
334 /// always be either NoClass or the result of a previous merge
335 /// call. In addition, this should never be Memory (the caller
336 /// should just return Memory for the aggregate).
337 Class merge(Class Accum, Class Field) const;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000338
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000339 /// classify - Determine the x86_64 register classes in which the
340 /// given type T should be passed.
341 ///
Daniel Dunbarc4503572009-01-31 00:06:58 +0000342 /// \param Lo - The classification for the parts of the type
343 /// residing in the low word of the containing object.
344 ///
345 /// \param Hi - The classification for the parts of the type
346 /// residing in the high word of the containing object.
347 ///
348 /// \param OffsetBase - The bit offset of this type in the
Daniel Dunbarcdf920e2009-01-30 22:40:15 +0000349 /// containing object. Some parameters are classified different
350 /// depending on whether they straddle an eightbyte boundary.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000351 ///
352 /// If a word is unused its result will be NoClass; if a type should
353 /// be passed in Memory then at least the classification of \arg Lo
354 /// will be Memory.
355 ///
356 /// The \arg Lo class will be NoClass iff the argument is ignored.
357 ///
358 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
359 /// be NoClass.
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000360 void classify(QualType T, ASTContext &Context, uint64_t OffsetBase,
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000361 Class &Lo, Class &Hi) const;
Daniel Dunbarc4503572009-01-31 00:06:58 +0000362
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000363public:
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000364 ABIArgInfo classifyReturnType(QualType RetTy,
365 ASTContext &Context) const;
366
367 ABIArgInfo classifyArgumentType(QualType RetTy,
368 ASTContext &Context) const;
369
370 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
371 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
372 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
373 it != ie; ++it)
374 it->info = classifyArgumentType(it->type, Context);
375 }
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000376};
377}
378
Daniel Dunbarc4503572009-01-31 00:06:58 +0000379X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum,
380 Class Field) const {
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000381 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
382 // classified recursively so that always two fields are
383 // considered. The resulting class is calculated according to
384 // the classes of the fields in the eightbyte:
385 //
386 // (a) If both classes are equal, this is the resulting class.
387 //
388 // (b) If one of the classes is NO_CLASS, the resulting class is
389 // the other class.
390 //
391 // (c) If one of the classes is MEMORY, the result is the MEMORY
392 // class.
393 //
394 // (d) If one of the classes is INTEGER, the result is the
395 // INTEGER.
396 //
397 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
398 // MEMORY is used as class.
399 //
400 // (f) Otherwise class SSE is used.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000401 assert((Accum == NoClass || Accum == Integer ||
402 Accum == SSE || Accum == SSEUp) &&
403 "Invalid accumulated classification during merge.");
404 if (Accum == Field || Field == NoClass)
405 return Accum;
406 else if (Field == Memory)
407 return Memory;
408 else if (Accum == NoClass)
409 return Field;
410 else if (Accum == Integer || Field == Integer)
411 return Integer;
412 else if (Field == X87 || Field == X87Up || Field == ComplexX87)
413 return Memory;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000414 else
Daniel Dunbarc4503572009-01-31 00:06:58 +0000415 return SSE;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000416}
417
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000418void X86_64ABIInfo::classify(QualType Ty,
419 ASTContext &Context,
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000420 uint64_t OffsetBase,
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000421 Class &Lo, Class &Hi) const {
Daniel Dunbar9a82b522009-02-02 18:06:39 +0000422 // FIXME: This code can be simplified by introducing a simple value
423 // class for Class pairs with appropriate constructor methods for
424 // the various situations.
425
Daniel Dunbarc4503572009-01-31 00:06:58 +0000426 Lo = Hi = NoClass;
427
428 Class &Current = OffsetBase < 64 ? Lo : Hi;
429 Current = Memory;
430
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000431 if (const BuiltinType *BT = Ty->getAsBuiltinType()) {
432 BuiltinType::Kind k = BT->getKind();
433
Daniel Dunbar11434922009-01-26 21:26:08 +0000434 if (k == BuiltinType::Void) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000435 Current = NoClass;
Daniel Dunbar11434922009-01-26 21:26:08 +0000436 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000437 Current = Integer;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000438 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000439 Current = SSE;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000440 } else if (k == BuiltinType::LongDouble) {
441 Lo = X87;
442 Hi = X87Up;
443 }
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000444 // FIXME: _Decimal32 and _Decimal64 are SSE.
445 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000446 // FIXME: __int128 is (Integer, Integer).
447 } else if (Ty->isPointerLikeType() || Ty->isBlockPointerType() ||
448 Ty->isObjCQualifiedInterfaceType()) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000449 Current = Integer;
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000450 } else if (const VectorType *VT = Ty->getAsVectorType()) {
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000451 uint64_t Size = Context.getTypeSize(VT);
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000452 if (Size == 64) {
Daniel Dunbard4cd1b02009-01-30 19:38:39 +0000453 // gcc passes <1 x double> in memory.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000454 if (VT->getElementType() == Context.DoubleTy)
Daniel Dunbard4cd1b02009-01-30 19:38:39 +0000455 return;
Daniel Dunbard4cd1b02009-01-30 19:38:39 +0000456
Daniel Dunbarc4503572009-01-31 00:06:58 +0000457 Current = SSE;
Daniel Dunbare33edf12009-01-30 18:40:10 +0000458
459 // If this type crosses an eightbyte boundary, it should be
460 // split.
Daniel Dunbarcdf920e2009-01-30 22:40:15 +0000461 if (OffsetBase && OffsetBase != 64)
Daniel Dunbare33edf12009-01-30 18:40:10 +0000462 Hi = Lo;
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000463 } else if (Size == 128) {
464 Lo = SSE;
465 Hi = SSEUp;
466 }
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000467 } else if (const ComplexType *CT = Ty->getAsComplexType()) {
468 QualType ET = CT->getElementType();
469
Daniel Dunbare33edf12009-01-30 18:40:10 +0000470 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbareac48dc2009-01-29 07:22:20 +0000471 if (ET->isIntegerType()) {
Daniel Dunbareac48dc2009-01-29 07:22:20 +0000472 if (Size <= 64)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000473 Current = Integer;
Daniel Dunbareac48dc2009-01-29 07:22:20 +0000474 else if (Size <= 128)
475 Lo = Hi = Integer;
476 } else if (ET == Context.FloatTy)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000477 Current = SSE;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000478 else if (ET == Context.DoubleTy)
479 Lo = Hi = SSE;
480 else if (ET == Context.LongDoubleTy)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000481 Current = ComplexX87;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000482
483 // If this complex type crosses an eightbyte boundary then it
484 // should be split.
Daniel Dunbarcdf920e2009-01-30 22:40:15 +0000485 uint64_t EB_Real = (OffsetBase) / 64;
486 uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000487 if (Hi == NoClass && EB_Real != EB_Imag)
488 Hi = Lo;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000489 } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
490 // Arrays are treated like structures.
491
492 uint64_t Size = Context.getTypeSize(Ty);
493
494 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
495 // than two eightbytes, ..., it has class MEMORY.
496 if (Size > 128)
497 return;
498
499 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
500 // fields, it has class MEMORY.
501 //
502 // Only need to check alignment of array base.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000503 if (OffsetBase % Context.getTypeAlign(AT->getElementType()))
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000504 return;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000505
506 // Otherwise implement simplified merge. We could be smarter about
507 // this, but it isn't worth it and would be harder to verify.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000508 Current = NoClass;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000509 uint64_t EltSize = Context.getTypeSize(AT->getElementType());
510 uint64_t ArraySize = AT->getSize().getZExtValue();
511 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
512 Class FieldLo, FieldHi;
513 classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbarc4503572009-01-31 00:06:58 +0000514 Lo = merge(Lo, FieldLo);
515 Hi = merge(Hi, FieldHi);
516 if (Lo == Memory || Hi == Memory)
517 break;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000518 }
Daniel Dunbarc4503572009-01-31 00:06:58 +0000519
520 // Do post merger cleanup (see below). Only case we worry about is Memory.
521 if (Hi == Memory)
522 Lo = Memory;
523 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Daniel Dunbar99037e52009-01-29 08:13:58 +0000524 } else if (const RecordType *RT = Ty->getAsRecordType()) {
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000525 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbar99037e52009-01-29 08:13:58 +0000526
527 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
528 // than two eightbytes, ..., it has class MEMORY.
529 if (Size > 128)
530 return;
531
532 const RecordDecl *RD = RT->getDecl();
533
534 // Assume variable sized types are passed in memory.
535 if (RD->hasFlexibleArrayMember())
536 return;
537
538 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
539
540 // Reset Lo class, this will be recomputed.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000541 Current = NoClass;
Daniel Dunbar99037e52009-01-29 08:13:58 +0000542 unsigned idx = 0;
543 for (RecordDecl::field_iterator i = RD->field_begin(),
544 e = RD->field_end(); i != e; ++i, ++idx) {
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000545 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Daniel Dunbar99037e52009-01-29 08:13:58 +0000546
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000547 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
548 // fields, it has class MEMORY.
Daniel Dunbar99037e52009-01-29 08:13:58 +0000549 if (Offset % Context.getTypeAlign(i->getType())) {
550 Lo = Memory;
551 return;
552 }
553
Daniel Dunbar99037e52009-01-29 08:13:58 +0000554 // Classify this field.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000555 //
556 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
557 // exceeds a single eightbyte, each is classified
558 // separately. Each eightbyte gets initialized to class
559 // NO_CLASS.
Daniel Dunbar99037e52009-01-29 08:13:58 +0000560 Class FieldLo, FieldHi;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000561 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbarc4503572009-01-31 00:06:58 +0000562 Lo = merge(Lo, FieldLo);
563 Hi = merge(Hi, FieldHi);
564 if (Lo == Memory || Hi == Memory)
565 break;
Daniel Dunbar99037e52009-01-29 08:13:58 +0000566 }
567
568 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
569 //
570 // (a) If one of the classes is MEMORY, the whole argument is
571 // passed in memory.
572 //
573 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
574
575 // The first of these conditions is guaranteed by how we implement
Daniel Dunbarc4503572009-01-31 00:06:58 +0000576 // the merge (just bail).
577 //
578 // The second condition occurs in the case of unions; for example
579 // union { _Complex double; unsigned; }.
580 if (Hi == Memory)
581 Lo = Memory;
Daniel Dunbar99037e52009-01-29 08:13:58 +0000582 if (Hi == SSEUp && Lo != SSE)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000583 Hi = SSE;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000584 }
585}
586
Daniel Dunbarc4503572009-01-31 00:06:58 +0000587
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000588ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
589 ASTContext &Context) const {
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000590 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
591 // classification algorithm.
592 X86_64ABIInfo::Class Lo, Hi;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000593 classify(RetTy, Context, 0, Lo, Hi);
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000594
Daniel Dunbarc4503572009-01-31 00:06:58 +0000595 // Check some invariants.
596 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
597 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
598 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
599
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000600 const llvm::Type *ResType = 0;
601 switch (Lo) {
602 case NoClass:
Daniel Dunbar11434922009-01-26 21:26:08 +0000603 return ABIArgInfo::getIgnore();
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000604
605 case SSEUp:
606 case X87Up:
607 assert(0 && "Invalid classification for lo word.");
608
Daniel Dunbarc4503572009-01-31 00:06:58 +0000609 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
610 // hidden argument, i.e. structret.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000611 case Memory:
612 return ABIArgInfo::getStructRet();
613
614 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
615 // available register of the sequence %rax, %rdx is used.
616 case Integer:
617 ResType = llvm::Type::Int64Ty; break;
618
619 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
620 // available SSE register of the sequence %xmm0, %xmm1 is used.
621 case SSE:
622 ResType = llvm::Type::DoubleTy; break;
623
624 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
625 // returned on the X87 stack in %st0 as 80-bit x87 number.
626 case X87:
627 ResType = llvm::Type::X86_FP80Ty; break;
628
Daniel Dunbarc4503572009-01-31 00:06:58 +0000629 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
630 // part of the value is returned in %st0 and the imaginary part in
631 // %st1.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000632 case ComplexX87:
633 assert(Hi == NoClass && "Unexpected ComplexX87 classification.");
634 ResType = llvm::VectorType::get(llvm::Type::X86_FP80Ty, 2);
635 break;
636 }
637
638 switch (Hi) {
639 // Memory was handled previously, and ComplexX87 and X87 should
640 // never occur as hi classes.
641 case Memory:
642 case X87:
643 case ComplexX87:
644 assert(0 && "Invalid classification for hi word.");
645
646 case NoClass: break;
647 case Integer:
Daniel Dunbarb0e14f22009-01-29 07:36:07 +0000648 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
649 break;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000650 case SSE:
Daniel Dunbarb0e14f22009-01-29 07:36:07 +0000651 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
652 break;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000653
654 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
655 // is passed in the upper half of the last used SSE register.
656 //
657 // SSEUP should always be preceeded by SSE, just widen.
658 case SSEUp:
659 assert(Lo == SSE && "Unexpected SSEUp classification.");
660 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
661 break;
662
663 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
Daniel Dunbarb0e14f22009-01-29 07:36:07 +0000664 // returned together with the previous X87 value in %st0.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000665 //
666 // X87UP should always be preceeded by X87, so we don't need to do
667 // anything here.
668 case X87Up:
669 assert(Lo == X87 && "Unexpected X87Up classification.");
670 break;
671 }
672
673 return ABIArgInfo::getCoerce(ResType);
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000674}
675
676ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty,
677 ASTContext &Context) const {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000678 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
679 return ABIArgInfo::getByVal(0);
680 } else {
681 return ABIArgInfo::getDirect();
682 }
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000683}
684
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000685ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
686 ASTContext &Context) const {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000687 if (RetTy->isVoidType()) {
688 return ABIArgInfo::getIgnore();
689 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
690 return ABIArgInfo::getStructRet();
691 } else {
692 return ABIArgInfo::getDirect();
693 }
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000694}
695
696ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
697 ASTContext &Context) const {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000698 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
699 return ABIArgInfo::getByVal(0);
700 } else {
701 return ABIArgInfo::getDirect();
702 }
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000703}
704
705const ABIInfo &CodeGenTypes::getABIInfo() const {
706 if (TheABIInfo)
707 return *TheABIInfo;
708
709 // For now we just cache this in the CodeGenTypes and don't bother
710 // to free it.
711 const char *TargetPrefix = getContext().Target.getTargetPrefix();
712 if (strcmp(TargetPrefix, "x86") == 0) {
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000713 switch (getContext().Target.getPointerWidth(0)) {
714 case 32:
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000715 return *(TheABIInfo = new X86_32ABIInfo());
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000716 case 64:
Daniel Dunbar11a76ed2009-01-30 18:47:53 +0000717 return *(TheABIInfo = new X86_64ABIInfo());
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000718 }
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000719 }
720
721 return *(TheABIInfo = new DefaultABIInfo);
722}
723
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000724/***/
725
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000726CGFunctionInfo::CGFunctionInfo(QualType ResTy,
727 const llvm::SmallVector<QualType, 16> &ArgTys) {
728 NumArgs = ArgTys.size();
729 Args = new ArgInfo[1 + NumArgs];
730 Args[0].type = ResTy;
731 for (unsigned i = 0; i < NumArgs; ++i)
732 Args[1 + i].type = ArgTys[i];
733}
734
735/***/
736
Daniel Dunbar56273772008-09-17 00:51:38 +0000737void CodeGenTypes::GetExpandedTypes(QualType Ty,
738 std::vector<const llvm::Type*> &ArgTys) {
739 const RecordType *RT = Ty->getAsStructureType();
740 assert(RT && "Can only expand structure types.");
741 const RecordDecl *RD = RT->getDecl();
742 assert(!RD->hasFlexibleArrayMember() &&
743 "Cannot expand structure with flexible array.");
744
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000745 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar56273772008-09-17 00:51:38 +0000746 e = RD->field_end(); i != e; ++i) {
747 const FieldDecl *FD = *i;
748 assert(!FD->isBitField() &&
749 "Cannot expand structure with bit-field members.");
750
751 QualType FT = FD->getType();
752 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
753 GetExpandedTypes(FT, ArgTys);
754 } else {
755 ArgTys.push_back(ConvertType(FT));
756 }
757 }
758}
759
760llvm::Function::arg_iterator
761CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
762 llvm::Function::arg_iterator AI) {
763 const RecordType *RT = Ty->getAsStructureType();
764 assert(RT && "Can only expand structure types.");
765
766 RecordDecl *RD = RT->getDecl();
767 assert(LV.isSimple() &&
768 "Unexpected non-simple lvalue during struct expansion.");
769 llvm::Value *Addr = LV.getAddress();
770 for (RecordDecl::field_iterator i = RD->field_begin(),
771 e = RD->field_end(); i != e; ++i) {
772 FieldDecl *FD = *i;
773 QualType FT = FD->getType();
774
775 // FIXME: What are the right qualifiers here?
776 LValue LV = EmitLValueForField(Addr, FD, false, 0);
777 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
778 AI = ExpandTypeFromArgs(FT, LV, AI);
779 } else {
780 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
781 ++AI;
782 }
783 }
784
785 return AI;
786}
787
788void
789CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
790 llvm::SmallVector<llvm::Value*, 16> &Args) {
791 const RecordType *RT = Ty->getAsStructureType();
792 assert(RT && "Can only expand structure types.");
793
794 RecordDecl *RD = RT->getDecl();
795 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
796 llvm::Value *Addr = RV.getAggregateAddr();
797 for (RecordDecl::field_iterator i = RD->field_begin(),
798 e = RD->field_end(); i != e; ++i) {
799 FieldDecl *FD = *i;
800 QualType FT = FD->getType();
801
802 // FIXME: What are the right qualifiers here?
803 LValue LV = EmitLValueForField(Addr, FD, false, 0);
804 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
805 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
806 } else {
807 RValue RV = EmitLoadOfLValue(LV, FT);
808 assert(RV.isScalar() &&
809 "Unexpected non-scalar rvalue during struct expansion.");
810 Args.push_back(RV.getScalarVal());
811 }
812 }
813}
814
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000815/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
816/// a pointer to an object of type \arg Ty.
817///
818/// This safely handles the case when the src type is smaller than the
819/// destination type; in this situation the values of bits which not
820/// present in the src are undefined.
821static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
822 const llvm::Type *Ty,
823 CodeGenFunction &CGF) {
824 const llvm::Type *SrcTy =
825 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
826 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
827 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(Ty);
828
Daniel Dunbarb225be42009-02-03 05:59:18 +0000829 // If load is legal, just bitcast the src pointer.
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000830 if (SrcSize == DstSize) {
831 llvm::Value *Casted =
832 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
833 return CGF.Builder.CreateLoad(Casted);
834 } else {
835 assert(SrcSize < DstSize && "Coercion is losing source bits!");
836
837 // Otherwise do coercion through memory. This is stupid, but
838 // simple.
839 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
840 llvm::Value *Casted =
841 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
842 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
843 return CGF.Builder.CreateLoad(Tmp);
844 }
845}
846
847/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
848/// where the source and destination may have different types.
849///
850/// This safely handles the case when the src type is larger than the
851/// destination type; the upper bits of the src will be lost.
852static void CreateCoercedStore(llvm::Value *Src,
853 llvm::Value *DstPtr,
854 CodeGenFunction &CGF) {
855 const llvm::Type *SrcTy = Src->getType();
856 const llvm::Type *DstTy =
857 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
858
859 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
860 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(DstTy);
861
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000862 // If store is legal, just bitcast the src pointer.
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000863 if (SrcSize == DstSize) {
864 llvm::Value *Casted =
865 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
866 CGF.Builder.CreateStore(Src, Casted);
867 } else {
868 assert(SrcSize > DstSize && "Coercion is missing bits!");
869
870 // Otherwise do coercion through memory. This is stupid, but
871 // simple.
872 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
873 CGF.Builder.CreateStore(Src, Tmp);
874 llvm::Value *Casted =
875 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
876 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(Casted), DstPtr);
877 }
878}
879
Daniel Dunbar56273772008-09-17 00:51:38 +0000880/***/
881
Daniel Dunbar88b53962009-02-02 22:03:45 +0000882bool CodeGenModule::ReturnTypeUsesSret(const CGFunctionInfo &FI) {
Daniel Dunbarb225be42009-02-03 05:59:18 +0000883 return FI.getReturnInfo().isStructRet();
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000884}
885
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000886const llvm::FunctionType *
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000887CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic) {
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000888 std::vector<const llvm::Type*> ArgTys;
889
890 const llvm::Type *ResultType = 0;
891
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000892 QualType RetTy = FI.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000893 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000894 switch (RetAI.getKind()) {
895 case ABIArgInfo::ByVal:
896 case ABIArgInfo::Expand:
897 assert(0 && "Invalid ABI kind for return argument");
898
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000899 case ABIArgInfo::Direct:
900 ResultType = ConvertType(RetTy);
901 break;
902
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000903 case ABIArgInfo::StructRet: {
904 ResultType = llvm::Type::VoidTy;
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000905 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000906 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
907 break;
908 }
909
Daniel Dunbar11434922009-01-26 21:26:08 +0000910 case ABIArgInfo::Ignore:
911 ResultType = llvm::Type::VoidTy;
912 break;
913
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000914 case ABIArgInfo::Coerce:
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000915 ResultType = RetAI.getCoerceToType();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000916 break;
917 }
918
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000919 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
920 ie = FI.arg_end(); it != ie; ++it) {
921 const ABIArgInfo &AI = it->info;
922 const llvm::Type *Ty = ConvertType(it->type);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000923
924 switch (AI.getKind()) {
Daniel Dunbar11434922009-01-26 21:26:08 +0000925 case ABIArgInfo::Ignore:
926 break;
927
Daniel Dunbar56273772008-09-17 00:51:38 +0000928 case ABIArgInfo::Coerce:
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000929 ArgTys.push_back(AI.getCoerceToType());
930 break;
931
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000932 case ABIArgInfo::StructRet:
933 assert(0 && "Invalid ABI kind for non-return argument");
934
935 case ABIArgInfo::ByVal:
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000936 // byval arguments are always on the stack, which is addr space #0.
937 ArgTys.push_back(llvm::PointerType::getUnqual(Ty));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000938 assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled");
939 break;
940
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000941 case ABIArgInfo::Direct:
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000942 ArgTys.push_back(Ty);
943 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000944
945 case ABIArgInfo::Expand:
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000946 GetExpandedTypes(it->type, ArgTys);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000947 break;
948 }
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000949 }
950
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000951 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar3913f182008-09-09 23:48:28 +0000952}
953
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000954void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
Daniel Dunbar88b53962009-02-02 22:03:45 +0000955 const Decl *TargetDecl,
Devang Patel761d7f72008-09-25 21:02:23 +0000956 AttributeListType &PAL) {
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000957 unsigned FuncAttrs = 0;
Devang Patela2c69122008-09-26 22:53:57 +0000958 unsigned RetAttrs = 0;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000959
960 if (TargetDecl) {
961 if (TargetDecl->getAttr<NoThrowAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +0000962 FuncAttrs |= llvm::Attribute::NoUnwind;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000963 if (TargetDecl->getAttr<NoReturnAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +0000964 FuncAttrs |= llvm::Attribute::NoReturn;
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000965 if (TargetDecl->getAttr<PureAttr>())
966 FuncAttrs |= llvm::Attribute::ReadOnly;
967 if (TargetDecl->getAttr<ConstAttr>())
968 FuncAttrs |= llvm::Attribute::ReadNone;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000969 }
970
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000971 QualType RetTy = FI.getReturnType();
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000972 unsigned Index = 1;
Daniel Dunbarb225be42009-02-03 05:59:18 +0000973 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000974 switch (RetAI.getKind()) {
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:
1011 assert(0 && "Invalid ABI kind for non-return argument");
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001012
1013 case ABIArgInfo::Coerce:
1014 break;
1015
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001016 case ABIArgInfo::ByVal:
Devang Patel761d7f72008-09-25 21:02:23 +00001017 Attributes |= llvm::Attribute::ByVal;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001018 assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled");
1019 break;
1020
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001021 case ABIArgInfo::Direct:
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001022 if (ParamType->isPromotableIntegerType()) {
1023 if (ParamType->isSignedIntegerType()) {
Devang Patel761d7f72008-09-25 21:02:23 +00001024 Attributes |= llvm::Attribute::SExt;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001025 } else if (ParamType->isUnsignedIntegerType()) {
Devang Patel761d7f72008-09-25 21:02:23 +00001026 Attributes |= llvm::Attribute::ZExt;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001027 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001028 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001029 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001030
Daniel Dunbar11434922009-01-26 21:26:08 +00001031 case ABIArgInfo::Ignore:
1032 // Skip increment, no matching LLVM parameter.
1033 continue;
1034
Daniel Dunbar56273772008-09-17 00:51:38 +00001035 case ABIArgInfo::Expand: {
1036 std::vector<const llvm::Type*> Tys;
1037 // FIXME: This is rather inefficient. Do we ever actually need
1038 // to do anything here? The result should be just reconstructed
1039 // on the other side, so extension should be a non-issue.
1040 getTypes().GetExpandedTypes(ParamType, Tys);
1041 Index += Tys.size();
1042 continue;
1043 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001044 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001045
Devang Patel761d7f72008-09-25 21:02:23 +00001046 if (Attributes)
1047 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar56273772008-09-17 00:51:38 +00001048 ++Index;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001049 }
Devang Patela2c69122008-09-26 22:53:57 +00001050 if (FuncAttrs)
1051 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
1052
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001053}
1054
Daniel Dunbar88b53962009-02-02 22:03:45 +00001055void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
1056 llvm::Function *Fn,
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001057 const FunctionArgList &Args) {
Daniel Dunbar5251afa2009-02-03 06:02:10 +00001058 // FIXME: We no longer need the types from FunctionArgList; lift up
1059 // and simplify.
1060
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001061 // Emit allocs for param decls. Give the LLVM Argument nodes names.
1062 llvm::Function::arg_iterator AI = Fn->arg_begin();
1063
1064 // Name the struct return argument.
Daniel Dunbar88b53962009-02-02 22:03:45 +00001065 if (CGM.ReturnTypeUsesSret(FI)) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001066 AI->setName("agg.result");
1067 ++AI;
1068 }
Daniel Dunbarb225be42009-02-03 05:59:18 +00001069
1070 CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001071 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001072 i != e; ++i, ++info_it) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001073 const VarDecl *Arg = i->first;
Daniel Dunbarb225be42009-02-03 05:59:18 +00001074 QualType Ty = info_it->type;
1075 const ABIArgInfo &ArgI = info_it->info;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001076
1077 switch (ArgI.getKind()) {
1078 case ABIArgInfo::ByVal:
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001079 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 Dunbar89c9d8e2009-02-03 19:12:28 +00001113 case ABIArgInfo::Coerce: {
1114 assert(AI != Fn->arg_end() && "Argument mismatch!");
1115 // FIXME: This is very wasteful; EmitParmDecl is just going to
1116 // drop the result in a new alloca anyway, so we could just
1117 // store into that directly if we broke the abstraction down
1118 // more.
1119 llvm::Value *V = CreateTempAlloca(ConvertType(Ty), "coerce");
1120 CreateCoercedStore(AI, V, *this);
1121 // Match to what EmitParmDecl is expecting for this type.
1122 if (!CodeGenFunction::hasAggregateLLVMType(Ty))
1123 V = Builder.CreateLoad(V);
1124 EmitParmDecl(*Arg, V);
1125 break;
1126 }
1127
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001128 case ABIArgInfo::StructRet:
1129 assert(0 && "Invalid ABI kind for non-return argument");
1130 }
Daniel Dunbar56273772008-09-17 00:51:38 +00001131
1132 ++AI;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001133 }
1134 assert(AI == Fn->arg_end() && "Argument mismatch!");
1135}
1136
Daniel Dunbar88b53962009-02-02 22:03:45 +00001137void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI,
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001138 llvm::Value *ReturnValue) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001139 llvm::Value *RV = 0;
1140
1141 // Functions with no result always return void.
1142 if (ReturnValue) {
Daniel Dunbar88b53962009-02-02 22:03:45 +00001143 QualType RetTy = FI.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001144 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001145
1146 switch (RetAI.getKind()) {
1147 case ABIArgInfo::StructRet:
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001148 if (RetTy->isAnyComplexType()) {
1149 // FIXME: Volatile
1150 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
1151 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
1152 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1153 EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy);
1154 } else {
1155 Builder.CreateStore(Builder.CreateLoad(ReturnValue),
1156 CurFn->arg_begin());
1157 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001158 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001159
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001160 case ABIArgInfo::Direct:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001161 RV = Builder.CreateLoad(ReturnValue);
1162 break;
1163
Daniel Dunbar11434922009-01-26 21:26:08 +00001164 case ABIArgInfo::Ignore:
1165 break;
1166
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001167 case ABIArgInfo::Coerce: {
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +00001168 RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001169 break;
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001170 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001171
1172 case ABIArgInfo::ByVal:
1173 case ABIArgInfo::Expand:
1174 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001175 }
1176 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001177
1178 if (RV) {
1179 Builder.CreateRet(RV);
1180 } else {
1181 Builder.CreateRetVoid();
1182 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001183}
1184
Daniel Dunbar88b53962009-02-02 22:03:45 +00001185RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
1186 llvm::Value *Callee,
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001187 const CallArgList &CallArgs) {
Daniel Dunbar5251afa2009-02-03 06:02:10 +00001188 // FIXME: We no longer need the types from CallArgs; lift up and
1189 // simplify.
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001190 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001191
1192 // Handle struct-return functions by passing a pointer to the
1193 // location that we would like to return into.
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001194 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001195 const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001196 switch (RetAI.getKind()) {
1197 case ABIArgInfo::StructRet:
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001198 // Create a temporary alloca to hold the result of the call. :(
Daniel Dunbar56273772008-09-17 00:51:38 +00001199 Args.push_back(CreateTempAlloca(ConvertType(RetTy)));
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001200 break;
1201
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001202 case ABIArgInfo::Direct:
Daniel Dunbar11434922009-01-26 21:26:08 +00001203 case ABIArgInfo::Ignore:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001204 case ABIArgInfo::Coerce:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001205 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001206
1207 case ABIArgInfo::ByVal:
1208 case ABIArgInfo::Expand:
Daniel Dunbarb225be42009-02-03 05:59:18 +00001209 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001210 }
1211
Daniel Dunbarb225be42009-02-03 05:59:18 +00001212 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001213 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001214 I != E; ++I, ++info_it) {
1215 const ABIArgInfo &ArgInfo = info_it->info;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001216 RValue RV = I->first;
Daniel Dunbar56273772008-09-17 00:51:38 +00001217
1218 switch (ArgInfo.getKind()) {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001219 case ABIArgInfo::ByVal: // Direct is byval
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001220 case ABIArgInfo::Direct:
Daniel Dunbar56273772008-09-17 00:51:38 +00001221 if (RV.isScalar()) {
1222 Args.push_back(RV.getScalarVal());
1223 } else if (RV.isComplex()) {
1224 // Make a temporary alloca to pass the argument.
1225 Args.push_back(CreateTempAlloca(ConvertType(I->second)));
1226 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
1227 } else {
1228 Args.push_back(RV.getAggregateAddr());
1229 }
1230 break;
1231
Daniel Dunbar11434922009-01-26 21:26:08 +00001232 case ABIArgInfo::Ignore:
1233 break;
1234
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001235 case ABIArgInfo::Coerce: {
1236 // FIXME: Avoid the conversion through memory if possible.
1237 llvm::Value *SrcPtr;
1238 if (RV.isScalar()) {
1239 SrcPtr = CreateTempAlloca(ConvertType(I->second), "coerce");
1240 Builder.CreateStore(RV.getScalarVal(), SrcPtr);
1241 } else if (RV.isComplex()) {
1242 SrcPtr = CreateTempAlloca(ConvertType(I->second), "coerce");
1243 StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
1244 } else
1245 SrcPtr = RV.getAggregateAddr();
1246 Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
1247 *this));
1248 break;
1249 }
1250
Daniel Dunbar56273772008-09-17 00:51:38 +00001251 case ABIArgInfo::StructRet:
Daniel Dunbar56273772008-09-17 00:51:38 +00001252 assert(0 && "Invalid ABI kind for non-return argument");
1253 break;
1254
1255 case ABIArgInfo::Expand:
1256 ExpandTypeToArgs(I->second, RV, Args);
1257 break;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001258 }
1259 }
1260
1261 llvm::CallInst *CI = Builder.CreateCall(Callee,&Args[0],&Args[0]+Args.size());
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001262
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001263 // FIXME: Provide TargetDecl so nounwind, noreturn, etc, etc get set.
Devang Patel761d7f72008-09-25 21:02:23 +00001264 CodeGen::AttributeListType AttributeList;
Daniel Dunbar88b53962009-02-02 22:03:45 +00001265 CGM.ConstructAttributeList(CallInfo, 0, AttributeList);
Devang Patel761d7f72008-09-25 21:02:23 +00001266 CI->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(),
Daniel Dunbar725ad312009-01-31 02:19:00 +00001267 AttributeList.size()));
1268
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001269 if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee))
1270 CI->setCallingConv(F->getCallingConv());
1271 if (CI->getType() != llvm::Type::VoidTy)
1272 CI->setName("call");
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001273
1274 switch (RetAI.getKind()) {
1275 case ABIArgInfo::StructRet:
1276 if (RetTy->isAnyComplexType())
Daniel Dunbar56273772008-09-17 00:51:38 +00001277 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001278 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar56273772008-09-17 00:51:38 +00001279 return RValue::getAggregate(Args[0]);
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001280 else
1281 return RValue::get(Builder.CreateLoad(Args[0]));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001282
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001283 case ABIArgInfo::Direct:
1284 assert((!RetTy->isAnyComplexType() &&
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001285 !CodeGenFunction::hasAggregateLLVMType(RetTy)) &&
1286 "FIXME: Implement return for non-scalar direct types.");
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001287 return RValue::get(CI);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001288
Daniel Dunbar11434922009-01-26 21:26:08 +00001289 case ABIArgInfo::Ignore:
Daniel Dunbarcc039fe2009-01-29 08:24:57 +00001290 if (RetTy->isVoidType())
1291 return RValue::get(0);
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001292
1293 // If we are ignoring an argument that had a result, make sure to
1294 // construct the appropriate return value for our caller.
Daniel Dunbarcc039fe2009-01-29 08:24:57 +00001295 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1296 llvm::Value *Res =
1297 llvm::UndefValue::get(llvm::PointerType::getUnqual(ConvertType(RetTy)));
1298 return RValue::getAggregate(Res);
1299 }
1300 return RValue::get(llvm::UndefValue::get(ConvertType(RetTy)));
Daniel Dunbar11434922009-01-26 21:26:08 +00001301
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001302 case ABIArgInfo::Coerce: {
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001303 // FIXME: Avoid the conversion through memory if possible.
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +00001304 llvm::Value *V = CreateTempAlloca(ConvertType(RetTy), "coerce");
1305 CreateCoercedStore(CI, V, *this);
Anders Carlssonad3d6912008-11-25 22:21:48 +00001306 if (RetTy->isAnyComplexType())
1307 return RValue::getComplex(LoadComplexFromAddr(V, false));
Daniel Dunbar11434922009-01-26 21:26:08 +00001308 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssonad3d6912008-11-25 22:21:48 +00001309 return RValue::getAggregate(V);
Daniel Dunbar11434922009-01-26 21:26:08 +00001310 else
1311 return RValue::get(Builder.CreateLoad(V));
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001312 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001313
1314 case ABIArgInfo::ByVal:
1315 case ABIArgInfo::Expand:
1316 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001317 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001318
1319 assert(0 && "Unhandled ABIArgInfo::Kind");
1320 return RValue::get(0);
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001321}