blob: a13b232571751d7af1fe813bd65f904618417e9e [file] [log] [blame]
Daniel Dunbara8f02052008-09-08 21:33:45 +00001//===----- CGCall.h - Encapsulate calling convention details ----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// These classes wrap the information about a call or function
11// definition used to handle ABI compliancy.
12//
13//===----------------------------------------------------------------------===//
14
15#include "CGCall.h"
16#include "CodeGenFunction.h"
Daniel Dunbar3ef2e852008-09-10 00:41:16 +000017#include "CodeGenModule.h"
Daniel Dunbarf98eeff2008-10-13 17:02:26 +000018#include "clang/Basic/TargetInfo.h"
Daniel Dunbara8f02052008-09-08 21:33:45 +000019#include "clang/AST/ASTContext.h"
20#include "clang/AST/Decl.h"
21#include "clang/AST/DeclObjC.h"
Daniel Dunbar51a2d192009-01-29 08:13:58 +000022#include "clang/AST/RecordLayout.h"
Daniel Dunbar04d35782008-09-17 00:51:38 +000023#include "llvm/ADT/StringExtras.h"
Devang Patel98bfe502008-09-24 01:01:36 +000024#include "llvm/Attributes.h"
Daniel Dunbare09a9692009-01-24 08:32:22 +000025#include "llvm/Support/CommandLine.h"
Daniel Dunbar708d8a82009-01-27 01:36:03 +000026#include "llvm/Target/TargetData.h"
Daniel Dunbard283e632009-02-03 01:05:53 +000027
28#include "ABIInfo.h"
29
Daniel Dunbara8f02052008-09-08 21:33:45 +000030using namespace clang;
31using namespace CodeGen;
32
33/***/
34
Daniel Dunbara8f02052008-09-08 21:33:45 +000035// FIXME: Use iterator and sidestep silly type array creation.
36
Daniel Dunbar34bda882009-02-02 23:23:47 +000037const
38CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionTypeNoProto *FTNP) {
39 return getFunctionInfo(FTNP->getResultType(),
40 llvm::SmallVector<QualType, 16>());
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000041}
42
Daniel Dunbar34bda882009-02-02 23:23:47 +000043const
44CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionTypeProto *FTP) {
45 llvm::SmallVector<QualType, 16> ArgTys;
46 // FIXME: Kill copy.
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000047 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
Daniel Dunbar34bda882009-02-02 23:23:47 +000048 ArgTys.push_back(FTP->getArgType(i));
49 return getFunctionInfo(FTP->getResultType(), ArgTys);
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000050}
51
Daniel Dunbar34bda882009-02-02 23:23:47 +000052const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionDecl *FD) {
Daniel Dunbara8f02052008-09-08 21:33:45 +000053 const FunctionType *FTy = FD->getType()->getAsFunctionType();
Daniel Dunbar34bda882009-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 Dunbara8f02052008-09-08 21:33:45 +000057}
58
Daniel Dunbar34bda882009-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 Dunbara8f02052008-09-08 21:33:45 +000064 for (ObjCMethodDecl::param_const_iterator i = MD->param_begin(),
65 e = MD->param_end(); i != e; ++i)
Daniel Dunbar34bda882009-02-02 23:23:47 +000066 ArgTys.push_back((*i)->getType());
67 return getFunctionInfo(MD->getResultType(), ArgTys);
Daniel Dunbara8f02052008-09-08 21:33:45 +000068}
69
Daniel Dunbar34bda882009-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 Dunbarebbb8f32009-01-31 02:19:00 +000074 for (CallArgList::const_iterator i = Args.begin(), e = Args.end();
75 i != e; ++i)
Daniel Dunbar34bda882009-02-02 23:23:47 +000076 ArgTys.push_back(i->second);
77 return getFunctionInfo(ResTy, ArgTys);
Daniel Dunbarebbb8f32009-01-31 02:19:00 +000078}
79
Daniel Dunbar34bda882009-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 Dunbar9fc15a82009-02-02 21:43:58 +000084 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
85 i != e; ++i)
Daniel Dunbar34bda882009-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 Dunbardcf19d12009-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 Dunbare92e0ab2009-02-03 05:31:23 +0000101 // Construct the function info.
Daniel Dunbardcf19d12009-02-03 00:07:12 +0000102 FI = new CGFunctionInfo(ResTy, ArgTys);
Daniel Dunbar5991d312009-02-04 21:36:22 +0000103
104 // FIXME: This is leaking like a sieve; please fix me.
105 // FunctionInfos.InsertNode(FI, InsertPos);
Daniel Dunbare92e0ab2009-02-03 05:31:23 +0000106
107 // Compute ABI information.
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000108 getABIInfo().computeInfo(*FI, getContext());
Daniel Dunbare92e0ab2009-02-03 05:31:23 +0000109
Daniel Dunbardcf19d12009-02-03 00:07:12 +0000110 return *FI;
Daniel Dunbar34bda882009-02-02 23:23:47 +0000111}
112
113/***/
114
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000115ABIInfo::~ABIInfo() {}
116
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000117/// isEmptyStruct - Return true iff a structure has no non-empty
118/// members. Note that a structure with a flexible array member is not
119/// considered empty.
120static bool isEmptyStruct(QualType T) {
121 const RecordType *RT = T->getAsStructureType();
122 if (!RT)
123 return 0;
124 const RecordDecl *RD = RT->getDecl();
125 if (RD->hasFlexibleArrayMember())
126 return false;
Douglas Gregor5d764842009-01-09 17:18:27 +0000127 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000128 e = RD->field_end(); i != e; ++i) {
129 const FieldDecl *FD = *i;
130 if (!isEmptyStruct(FD->getType()))
131 return false;
132 }
133 return true;
134}
135
136/// isSingleElementStruct - Determine if a structure is a "single
137/// element struct", i.e. it has exactly one non-empty field or
138/// exactly one field which is itself a single element
139/// struct. Structures with flexible array members are never
140/// considered single element structs.
141///
142/// \return The field declaration for the single non-empty field, if
143/// it exists.
144static const FieldDecl *isSingleElementStruct(QualType T) {
145 const RecordType *RT = T->getAsStructureType();
146 if (!RT)
147 return 0;
148
149 const RecordDecl *RD = RT->getDecl();
150 if (RD->hasFlexibleArrayMember())
151 return 0;
152
153 const FieldDecl *Found = 0;
Douglas Gregor5d764842009-01-09 17:18:27 +0000154 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000155 e = RD->field_end(); i != e; ++i) {
156 const FieldDecl *FD = *i;
157 QualType FT = FD->getType();
158
159 if (isEmptyStruct(FT)) {
160 // Ignore
161 } else if (Found) {
162 return 0;
163 } else if (!CodeGenFunction::hasAggregateLLVMType(FT)) {
164 Found = FD;
165 } else {
166 Found = isSingleElementStruct(FT);
167 if (!Found)
168 return 0;
169 }
170 }
171
172 return Found;
173}
174
175static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
176 if (!Ty->getAsBuiltinType() && !Ty->isPointerType())
177 return false;
178
179 uint64_t Size = Context.getTypeSize(Ty);
180 return Size == 32 || Size == 64;
181}
182
183static bool areAllFields32Or64BitBasicType(const RecordDecl *RD,
184 ASTContext &Context) {
Douglas Gregor5d764842009-01-09 17:18:27 +0000185 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000186 e = RD->field_end(); i != e; ++i) {
187 const FieldDecl *FD = *i;
188
189 if (!is32Or64BitBasicType(FD->getType(), Context))
190 return false;
191
192 // If this is a bit-field we need to make sure it is still a
193 // 32-bit or 64-bit type.
194 if (Expr *BW = FD->getBitWidth()) {
195 unsigned Width = BW->getIntegerConstantExprValue(Context).getZExtValue();
196 if (Width <= 16)
197 return false;
198 }
199 }
200 return true;
201}
202
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000203namespace {
204/// DefaultABIInfo - The default implementation for ABI specific
205/// details. This implementation provides information which results in
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000206/// self-consistent and sensible LLVM IR generation, but does not
207/// conform to any particular ABI.
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000208class DefaultABIInfo : public ABIInfo {
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000209 ABIArgInfo classifyReturnType(QualType RetTy,
210 ASTContext &Context) const;
211
212 ABIArgInfo classifyArgumentType(QualType RetTy,
213 ASTContext &Context) const;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000214
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000215 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
216 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
217 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
218 it != ie; ++it)
219 it->info = classifyArgumentType(it->type, Context);
220 }
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000221};
222
223/// X86_32ABIInfo - The X86-32 ABI information.
224class X86_32ABIInfo : public ABIInfo {
225public:
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000226 ABIArgInfo classifyReturnType(QualType RetTy,
227 ASTContext &Context) const;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000228
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000229 ABIArgInfo classifyArgumentType(QualType RetTy,
230 ASTContext &Context) const;
231
232 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
233 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
234 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
235 it != ie; ++it)
236 it->info = classifyArgumentType(it->type, Context);
237 }
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000238};
239}
240
241ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
242 ASTContext &Context) const {
Daniel Dunbareec02622009-02-03 06:30:17 +0000243 if (RetTy->isVoidType()) {
244 return ABIArgInfo::getIgnore();
245 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000246 // Classify "single element" structs as their element type.
247 const FieldDecl *SeltFD = isSingleElementStruct(RetTy);
248 if (SeltFD) {
249 QualType SeltTy = SeltFD->getType()->getDesugaredType();
250 if (const BuiltinType *BT = SeltTy->getAsBuiltinType()) {
251 // FIXME: This is gross, it would be nice if we could just
252 // pass back SeltTy and have clients deal with it. Is it worth
253 // supporting coerce to both LLVM and clang Types?
254 if (BT->isIntegerType()) {
255 uint64_t Size = Context.getTypeSize(SeltTy);
256 return ABIArgInfo::getCoerce(llvm::IntegerType::get((unsigned) Size));
257 } else if (BT->getKind() == BuiltinType::Float) {
258 return ABIArgInfo::getCoerce(llvm::Type::FloatTy);
259 } else if (BT->getKind() == BuiltinType::Double) {
260 return ABIArgInfo::getCoerce(llvm::Type::DoubleTy);
261 }
262 } else if (SeltTy->isPointerType()) {
263 // FIXME: It would be really nice if this could come out as
264 // the proper pointer type.
265 llvm::Type *PtrTy =
266 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
267 return ABIArgInfo::getCoerce(PtrTy);
268 }
269 }
270
Daniel Dunbar73d66602008-09-10 07:04:09 +0000271 uint64_t Size = Context.getTypeSize(RetTy);
272 if (Size == 8) {
273 return ABIArgInfo::getCoerce(llvm::Type::Int8Ty);
274 } else if (Size == 16) {
275 return ABIArgInfo::getCoerce(llvm::Type::Int16Ty);
276 } else if (Size == 32) {
277 return ABIArgInfo::getCoerce(llvm::Type::Int32Ty);
278 } else if (Size == 64) {
279 return ABIArgInfo::getCoerce(llvm::Type::Int64Ty);
280 } else {
281 return ABIArgInfo::getStructRet();
282 }
Daniel Dunbare126ab12008-09-10 02:41:04 +0000283 } else {
Daniel Dunbareec02622009-02-03 06:30:17 +0000284 return ABIArgInfo::getDirect();
Daniel Dunbare126ab12008-09-10 02:41:04 +0000285 }
286}
287
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000288ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
289 ASTContext &Context) const {
Daniel Dunbar3158c592008-09-17 20:11:04 +0000290 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000291 // Structures with flexible arrays are always byval.
292 if (const RecordType *RT = Ty->getAsStructureType())
293 if (RT->getDecl()->hasFlexibleArrayMember())
294 return ABIArgInfo::getByVal(0);
295
296 // Expand empty structs (i.e. ignore)
297 uint64_t Size = Context.getTypeSize(Ty);
298 if (Ty->isStructureType() && Size == 0)
299 return ABIArgInfo::getExpand();
300
301 // Expand structs with size <= 128-bits which consist only of
302 // basic types (int, long long, float, double, xxx*). This is
303 // non-recursive and does not ignore empty fields.
304 if (const RecordType *RT = Ty->getAsStructureType()) {
305 if (Context.getTypeSize(Ty) <= 4*32 &&
306 areAllFields32Or64BitBasicType(RT->getDecl(), Context))
307 return ABIArgInfo::getExpand();
308 }
309
Daniel Dunbar22e30052008-09-11 01:48:57 +0000310 return ABIArgInfo::getByVal(0);
311 } else {
Daniel Dunbareec02622009-02-03 06:30:17 +0000312 return ABIArgInfo::getDirect();
Daniel Dunbar22e30052008-09-11 01:48:57 +0000313 }
314}
315
Daniel Dunbare09a9692009-01-24 08:32:22 +0000316namespace {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000317/// X86_64ABIInfo - The X86_64 ABI information.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000318class X86_64ABIInfo : public ABIInfo {
319 enum Class {
320 Integer = 0,
321 SSE,
322 SSEUp,
323 X87,
324 X87Up,
325 ComplexX87,
326 NoClass,
327 Memory
328 };
329
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000330 /// merge - Implement the X86_64 ABI merging algorithm.
331 ///
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000332 /// Merge an accumulating classification \arg Accum with a field
333 /// classification \arg Field.
334 ///
335 /// \param Accum - The accumulating classification. This should
336 /// always be either NoClass or the result of a previous merge
337 /// call. In addition, this should never be Memory (the caller
338 /// should just return Memory for the aggregate).
339 Class merge(Class Accum, Class Field) const;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000340
Daniel Dunbare09a9692009-01-24 08:32:22 +0000341 /// classify - Determine the x86_64 register classes in which the
342 /// given type T should be passed.
343 ///
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000344 /// \param Lo - The classification for the parts of the type
345 /// residing in the low word of the containing object.
346 ///
347 /// \param Hi - The classification for the parts of the type
348 /// residing in the high word of the containing object.
349 ///
350 /// \param OffsetBase - The bit offset of this type in the
Daniel Dunbar2a2dce32009-01-30 22:40:15 +0000351 /// containing object. Some parameters are classified different
352 /// depending on whether they straddle an eightbyte boundary.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000353 ///
354 /// If a word is unused its result will be NoClass; if a type should
355 /// be passed in Memory then at least the classification of \arg Lo
356 /// will be Memory.
357 ///
358 /// The \arg Lo class will be NoClass iff the argument is ignored.
359 ///
360 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
361 /// be NoClass.
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000362 void classify(QualType T, ASTContext &Context, uint64_t OffsetBase,
Daniel Dunbare09a9692009-01-24 08:32:22 +0000363 Class &Lo, Class &Hi) const;
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000364
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000365 ABIArgInfo classifyReturnType(QualType RetTy,
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000366 ASTContext &Context) const;
367
368 ABIArgInfo classifyArgumentType(QualType Ty,
369 ASTContext &Context,
370 unsigned &freeIntRegs,
371 unsigned &freeSSERegs) const;
372
373public:
374 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000375};
376}
377
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000378X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum,
379 Class Field) const {
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000380 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
381 // classified recursively so that always two fields are
382 // considered. The resulting class is calculated according to
383 // the classes of the fields in the eightbyte:
384 //
385 // (a) If both classes are equal, this is the resulting class.
386 //
387 // (b) If one of the classes is NO_CLASS, the resulting class is
388 // the other class.
389 //
390 // (c) If one of the classes is MEMORY, the result is the MEMORY
391 // class.
392 //
393 // (d) If one of the classes is INTEGER, the result is the
394 // INTEGER.
395 //
396 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
397 // MEMORY is used as class.
398 //
399 // (f) Otherwise class SSE is used.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000400 assert((Accum == NoClass || Accum == Integer ||
401 Accum == SSE || Accum == SSEUp) &&
402 "Invalid accumulated classification during merge.");
403 if (Accum == Field || Field == NoClass)
404 return Accum;
405 else if (Field == Memory)
406 return Memory;
407 else if (Accum == NoClass)
408 return Field;
409 else if (Accum == Integer || Field == Integer)
410 return Integer;
411 else if (Field == X87 || Field == X87Up || Field == ComplexX87)
412 return Memory;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000413 else
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000414 return SSE;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000415}
416
Daniel Dunbare09a9692009-01-24 08:32:22 +0000417void X86_64ABIInfo::classify(QualType Ty,
418 ASTContext &Context,
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000419 uint64_t OffsetBase,
Daniel Dunbare09a9692009-01-24 08:32:22 +0000420 Class &Lo, Class &Hi) const {
Daniel Dunbar36b378e2009-02-02 18:06:39 +0000421 // FIXME: This code can be simplified by introducing a simple value
422 // class for Class pairs with appropriate constructor methods for
423 // the various situations.
424
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000425 Lo = Hi = NoClass;
426
427 Class &Current = OffsetBase < 64 ? Lo : Hi;
428 Current = Memory;
429
Daniel Dunbare09a9692009-01-24 08:32:22 +0000430 if (const BuiltinType *BT = Ty->getAsBuiltinType()) {
431 BuiltinType::Kind k = BT->getKind();
432
Daniel Dunbar1358b202009-01-26 21:26:08 +0000433 if (k == BuiltinType::Void) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000434 Current = NoClass;
Daniel Dunbar1358b202009-01-26 21:26:08 +0000435 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000436 Current = Integer;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000437 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000438 Current = SSE;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000439 } else if (k == BuiltinType::LongDouble) {
440 Lo = X87;
441 Hi = X87Up;
442 }
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000443 // FIXME: _Decimal32 and _Decimal64 are SSE.
444 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Daniel Dunbare09a9692009-01-24 08:32:22 +0000445 // FIXME: __int128 is (Integer, Integer).
446 } else if (Ty->isPointerLikeType() || Ty->isBlockPointerType() ||
447 Ty->isObjCQualifiedInterfaceType()) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000448 Current = Integer;
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000449 } else if (const VectorType *VT = Ty->getAsVectorType()) {
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000450 uint64_t Size = Context.getTypeSize(VT);
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000451 if (Size == 64) {
Daniel Dunbarcdf91e82009-01-30 19:38:39 +0000452 // gcc passes <1 x double> in memory.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000453 if (VT->getElementType() == Context.DoubleTy)
Daniel Dunbarcdf91e82009-01-30 19:38:39 +0000454 return;
Daniel Dunbarcdf91e82009-01-30 19:38:39 +0000455
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000456 Current = SSE;
Daniel Dunbare413f532009-01-30 18:40:10 +0000457
458 // If this type crosses an eightbyte boundary, it should be
459 // split.
Daniel Dunbar2a2dce32009-01-30 22:40:15 +0000460 if (OffsetBase && OffsetBase != 64)
Daniel Dunbare413f532009-01-30 18:40:10 +0000461 Hi = Lo;
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000462 } else if (Size == 128) {
463 Lo = SSE;
464 Hi = SSEUp;
465 }
Daniel Dunbare09a9692009-01-24 08:32:22 +0000466 } else if (const ComplexType *CT = Ty->getAsComplexType()) {
467 QualType ET = CT->getElementType();
468
Daniel Dunbare413f532009-01-30 18:40:10 +0000469 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbar28770fc2009-01-29 07:22:20 +0000470 if (ET->isIntegerType()) {
Daniel Dunbar28770fc2009-01-29 07:22:20 +0000471 if (Size <= 64)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000472 Current = Integer;
Daniel Dunbar28770fc2009-01-29 07:22:20 +0000473 else if (Size <= 128)
474 Lo = Hi = Integer;
475 } else if (ET == Context.FloatTy)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000476 Current = SSE;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000477 else if (ET == Context.DoubleTy)
478 Lo = Hi = SSE;
479 else if (ET == Context.LongDoubleTy)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000480 Current = ComplexX87;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000481
482 // If this complex type crosses an eightbyte boundary then it
483 // should be split.
Daniel Dunbar2a2dce32009-01-30 22:40:15 +0000484 uint64_t EB_Real = (OffsetBase) / 64;
485 uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000486 if (Hi == NoClass && EB_Real != EB_Imag)
487 Hi = Lo;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000488 } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
489 // Arrays are treated like structures.
490
491 uint64_t Size = Context.getTypeSize(Ty);
492
493 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
494 // than two eightbytes, ..., it has class MEMORY.
495 if (Size > 128)
496 return;
497
498 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
499 // fields, it has class MEMORY.
500 //
501 // Only need to check alignment of array base.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000502 if (OffsetBase % Context.getTypeAlign(AT->getElementType()))
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000503 return;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000504
505 // Otherwise implement simplified merge. We could be smarter about
506 // this, but it isn't worth it and would be harder to verify.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000507 Current = NoClass;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000508 uint64_t EltSize = Context.getTypeSize(AT->getElementType());
509 uint64_t ArraySize = AT->getSize().getZExtValue();
510 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
511 Class FieldLo, FieldHi;
512 classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000513 Lo = merge(Lo, FieldLo);
514 Hi = merge(Hi, FieldHi);
515 if (Lo == Memory || Hi == Memory)
516 break;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000517 }
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000518
519 // Do post merger cleanup (see below). Only case we worry about is Memory.
520 if (Hi == Memory)
521 Lo = Memory;
522 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000523 } else if (const RecordType *RT = Ty->getAsRecordType()) {
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000524 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000525
526 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
527 // than two eightbytes, ..., it has class MEMORY.
528 if (Size > 128)
529 return;
530
531 const RecordDecl *RD = RT->getDecl();
532
533 // Assume variable sized types are passed in memory.
534 if (RD->hasFlexibleArrayMember())
535 return;
536
537 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
538
539 // Reset Lo class, this will be recomputed.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000540 Current = NoClass;
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000541 unsigned idx = 0;
542 for (RecordDecl::field_iterator i = RD->field_begin(),
543 e = RD->field_end(); i != e; ++i, ++idx) {
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000544 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000545
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000546 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
547 // fields, it has class MEMORY.
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000548 if (Offset % Context.getTypeAlign(i->getType())) {
549 Lo = Memory;
550 return;
551 }
552
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000553 // Classify this field.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000554 //
555 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
556 // exceeds a single eightbyte, each is classified
557 // separately. Each eightbyte gets initialized to class
558 // NO_CLASS.
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000559 Class FieldLo, FieldHi;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000560 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000561 Lo = merge(Lo, FieldLo);
562 Hi = merge(Hi, FieldHi);
563 if (Lo == Memory || Hi == Memory)
564 break;
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000565 }
566
567 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
568 //
569 // (a) If one of the classes is MEMORY, the whole argument is
570 // passed in memory.
571 //
572 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
573
574 // The first of these conditions is guaranteed by how we implement
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000575 // the merge (just bail).
576 //
577 // The second condition occurs in the case of unions; for example
578 // union { _Complex double; unsigned; }.
579 if (Hi == Memory)
580 Lo = Memory;
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000581 if (Hi == SSEUp && Lo != SSE)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000582 Hi = SSE;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000583 }
584}
585
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000586
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000587ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
588 ASTContext &Context) const {
Daniel Dunbare09a9692009-01-24 08:32:22 +0000589 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
590 // classification algorithm.
591 X86_64ABIInfo::Class Lo, Hi;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000592 classify(RetTy, Context, 0, Lo, Hi);
Daniel Dunbare09a9692009-01-24 08:32:22 +0000593
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000594 // Check some invariants.
595 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
596 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
597 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
598
Daniel Dunbare09a9692009-01-24 08:32:22 +0000599 const llvm::Type *ResType = 0;
600 switch (Lo) {
601 case NoClass:
Daniel Dunbar1358b202009-01-26 21:26:08 +0000602 return ABIArgInfo::getIgnore();
Daniel Dunbare09a9692009-01-24 08:32:22 +0000603
604 case SSEUp:
605 case X87Up:
606 assert(0 && "Invalid classification for lo word.");
607
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000608 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
609 // hidden argument, i.e. structret.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000610 case Memory:
611 return ABIArgInfo::getStructRet();
612
613 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
614 // available register of the sequence %rax, %rdx is used.
615 case Integer:
616 ResType = llvm::Type::Int64Ty; break;
617
618 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
619 // available SSE register of the sequence %xmm0, %xmm1 is used.
620 case SSE:
621 ResType = llvm::Type::DoubleTy; break;
622
623 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
624 // returned on the X87 stack in %st0 as 80-bit x87 number.
625 case X87:
626 ResType = llvm::Type::X86_FP80Ty; break;
627
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000628 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
629 // part of the value is returned in %st0 and the imaginary part in
630 // %st1.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000631 case ComplexX87:
632 assert(Hi == NoClass && "Unexpected ComplexX87 classification.");
633 ResType = llvm::VectorType::get(llvm::Type::X86_FP80Ty, 2);
634 break;
635 }
636
637 switch (Hi) {
638 // Memory was handled previously, and ComplexX87 and X87 should
639 // never occur as hi classes.
640 case Memory:
641 case X87:
642 case ComplexX87:
643 assert(0 && "Invalid classification for hi word.");
644
645 case NoClass: break;
646 case Integer:
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000647 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
648 break;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000649 case SSE:
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000650 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
651 break;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000652
653 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
654 // is passed in the upper half of the last used SSE register.
655 //
656 // SSEUP should always be preceeded by SSE, just widen.
657 case SSEUp:
658 assert(Lo == SSE && "Unexpected SSEUp classification.");
659 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
660 break;
661
662 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000663 // returned together with the previous X87 value in %st0.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000664 //
665 // X87UP should always be preceeded by X87, so we don't need to do
666 // anything here.
667 case X87Up:
668 assert(Lo == X87 && "Unexpected X87Up classification.");
669 break;
670 }
671
672 return ABIArgInfo::getCoerce(ResType);
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000673}
674
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000675ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, ASTContext &Context,
676 unsigned &freeIntRegs,
677 unsigned &freeSSERegs) const {
678 X86_64ABIInfo::Class Lo, Hi;
679 classify(Ty, Context, 0, Lo, Hi);
680
681 // Check some invariants.
682 // FIXME: Enforce these by construction.
683 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
684 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
685 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
686
687 unsigned neededInt = 0, neededSSE = 0;
688 const llvm::Type *ResType = 0;
689 switch (Lo) {
690 case NoClass:
691 return ABIArgInfo::getIgnore();
692
693 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
694 // on the stack.
695 case Memory:
696
697 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
698 // COMPLEX_X87, it is passed in memory.
699 case X87:
700 case ComplexX87:
701 // Choose appropriate in memory type.
702 if (CodeGenFunction::hasAggregateLLVMType(Ty))
703 return ABIArgInfo::getByVal(0);
704 else
705 return ABIArgInfo::getDirect();
706
707 case SSEUp:
708 case X87Up:
709 assert(0 && "Invalid classification for lo word.");
710
711 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
712 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
713 // and %r9 is used.
714 case Integer:
715 ++neededInt;
716 ResType = llvm::Type::Int64Ty;
717 break;
718
719 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
720 // available SSE register is used, the registers are taken in the
721 // order from %xmm0 to %xmm7.
722 case SSE:
723 ++neededSSE;
724 ResType = llvm::Type::DoubleTy;
725 break;
Daniel Dunbareec02622009-02-03 06:30:17 +0000726 }
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000727
728 switch (Hi) {
729 // Memory was handled previously, ComplexX87 and X87 should
730 // never occur as hi classes, and X87Up must be preceed by X87,
731 // which is passed in memory.
732 case Memory:
733 case X87:
734 case X87Up:
735 case ComplexX87:
736 assert(0 && "Invalid classification for hi word.");
737
738 case NoClass: break;
739 case Integer:
740 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
741 ++neededInt;
742 break;
743 case SSE:
744 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
745 ++neededSSE;
746 break;
747
748 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
749 // eightbyte is passed in the upper half of the last used SSE
750 // register.
751 case SSEUp:
752 assert(Lo == SSE && "Unexpected SSEUp classification.");
753 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
754 break;
755 }
756
757 // AMD64-ABI 3.2.3p3: If there are no registers available for any
758 // eightbyte of an argument, the whole argument is passed on the
759 // stack. If registers have already been assigned for some
760 // eightbytes of such an argument, the assignments get reverted.
761 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
762 freeIntRegs -= neededInt;
763 freeSSERegs -= neededSSE;
764 return ABIArgInfo::getCoerce(ResType);
765 } else {
766 // Choose appropriate in memory type.
767 if (CodeGenFunction::hasAggregateLLVMType(Ty))
768 return ABIArgInfo::getByVal(0);
769 else
770 return ABIArgInfo::getDirect();
771 }
772}
773
774void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
775 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
776
777 // Keep track of the number of assigned registers.
778 unsigned freeIntRegs = 6, freeSSERegs = 8;
779
780 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
781 // get assigned (in left-to-right order) for passing as follows...
782 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
783 it != ie; ++it)
784 it->info = classifyArgumentType(it->type, Context, freeIntRegs, freeSSERegs);
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000785}
786
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000787ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
788 ASTContext &Context) const {
Daniel Dunbareec02622009-02-03 06:30:17 +0000789 if (RetTy->isVoidType()) {
790 return ABIArgInfo::getIgnore();
791 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
792 return ABIArgInfo::getStructRet();
793 } else {
794 return ABIArgInfo::getDirect();
795 }
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000796}
797
798ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
799 ASTContext &Context) const {
Daniel Dunbareec02622009-02-03 06:30:17 +0000800 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
801 return ABIArgInfo::getByVal(0);
802 } else {
803 return ABIArgInfo::getDirect();
804 }
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000805}
806
807const ABIInfo &CodeGenTypes::getABIInfo() const {
808 if (TheABIInfo)
809 return *TheABIInfo;
810
811 // For now we just cache this in the CodeGenTypes and don't bother
812 // to free it.
813 const char *TargetPrefix = getContext().Target.getTargetPrefix();
814 if (strcmp(TargetPrefix, "x86") == 0) {
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000815 switch (getContext().Target.getPointerWidth(0)) {
816 case 32:
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000817 return *(TheABIInfo = new X86_32ABIInfo());
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000818 case 64:
Daniel Dunbar56555952009-01-30 18:47:53 +0000819 return *(TheABIInfo = new X86_64ABIInfo());
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000820 }
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000821 }
822
823 return *(TheABIInfo = new DefaultABIInfo);
824}
825
Daniel Dunbare126ab12008-09-10 02:41:04 +0000826/***/
827
Daniel Dunbare92e0ab2009-02-03 05:31:23 +0000828CGFunctionInfo::CGFunctionInfo(QualType ResTy,
829 const llvm::SmallVector<QualType, 16> &ArgTys) {
830 NumArgs = ArgTys.size();
831 Args = new ArgInfo[1 + NumArgs];
832 Args[0].type = ResTy;
833 for (unsigned i = 0; i < NumArgs; ++i)
834 Args[1 + i].type = ArgTys[i];
835}
836
837/***/
838
Daniel Dunbar04d35782008-09-17 00:51:38 +0000839void CodeGenTypes::GetExpandedTypes(QualType Ty,
840 std::vector<const llvm::Type*> &ArgTys) {
841 const RecordType *RT = Ty->getAsStructureType();
842 assert(RT && "Can only expand structure types.");
843 const RecordDecl *RD = RT->getDecl();
844 assert(!RD->hasFlexibleArrayMember() &&
845 "Cannot expand structure with flexible array.");
846
Douglas Gregor5d764842009-01-09 17:18:27 +0000847 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar04d35782008-09-17 00:51:38 +0000848 e = RD->field_end(); i != e; ++i) {
849 const FieldDecl *FD = *i;
850 assert(!FD->isBitField() &&
851 "Cannot expand structure with bit-field members.");
852
853 QualType FT = FD->getType();
854 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
855 GetExpandedTypes(FT, ArgTys);
856 } else {
857 ArgTys.push_back(ConvertType(FT));
858 }
859 }
860}
861
862llvm::Function::arg_iterator
863CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
864 llvm::Function::arg_iterator AI) {
865 const RecordType *RT = Ty->getAsStructureType();
866 assert(RT && "Can only expand structure types.");
867
868 RecordDecl *RD = RT->getDecl();
869 assert(LV.isSimple() &&
870 "Unexpected non-simple lvalue during struct expansion.");
871 llvm::Value *Addr = LV.getAddress();
872 for (RecordDecl::field_iterator i = RD->field_begin(),
873 e = RD->field_end(); i != e; ++i) {
874 FieldDecl *FD = *i;
875 QualType FT = FD->getType();
876
877 // FIXME: What are the right qualifiers here?
878 LValue LV = EmitLValueForField(Addr, FD, false, 0);
879 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
880 AI = ExpandTypeFromArgs(FT, LV, AI);
881 } else {
882 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
883 ++AI;
884 }
885 }
886
887 return AI;
888}
889
890void
891CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
892 llvm::SmallVector<llvm::Value*, 16> &Args) {
893 const RecordType *RT = Ty->getAsStructureType();
894 assert(RT && "Can only expand structure types.");
895
896 RecordDecl *RD = RT->getDecl();
897 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
898 llvm::Value *Addr = RV.getAggregateAddr();
899 for (RecordDecl::field_iterator i = RD->field_begin(),
900 e = RD->field_end(); i != e; ++i) {
901 FieldDecl *FD = *i;
902 QualType FT = FD->getType();
903
904 // FIXME: What are the right qualifiers here?
905 LValue LV = EmitLValueForField(Addr, FD, false, 0);
906 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
907 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
908 } else {
909 RValue RV = EmitLoadOfLValue(LV, FT);
910 assert(RV.isScalar() &&
911 "Unexpected non-scalar rvalue during struct expansion.");
912 Args.push_back(RV.getScalarVal());
913 }
914 }
915}
916
Daniel Dunbar84379912009-02-02 19:06:38 +0000917/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
918/// a pointer to an object of type \arg Ty.
919///
920/// This safely handles the case when the src type is smaller than the
921/// destination type; in this situation the values of bits which not
922/// present in the src are undefined.
923static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
924 const llvm::Type *Ty,
925 CodeGenFunction &CGF) {
926 const llvm::Type *SrcTy =
927 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
928 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
929 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(Ty);
930
Daniel Dunbar77071992009-02-03 05:59:18 +0000931 // If load is legal, just bitcast the src pointer.
Daniel Dunbar84379912009-02-02 19:06:38 +0000932 if (SrcSize == DstSize) {
933 llvm::Value *Casted =
934 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
935 return CGF.Builder.CreateLoad(Casted);
936 } else {
937 assert(SrcSize < DstSize && "Coercion is losing source bits!");
938
939 // Otherwise do coercion through memory. This is stupid, but
940 // simple.
941 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
942 llvm::Value *Casted =
943 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
944 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
945 return CGF.Builder.CreateLoad(Tmp);
946 }
947}
948
949/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
950/// where the source and destination may have different types.
951///
952/// This safely handles the case when the src type is larger than the
953/// destination type; the upper bits of the src will be lost.
954static void CreateCoercedStore(llvm::Value *Src,
955 llvm::Value *DstPtr,
956 CodeGenFunction &CGF) {
957 const llvm::Type *SrcTy = Src->getType();
958 const llvm::Type *DstTy =
959 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
960
961 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
962 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(DstTy);
963
Daniel Dunbare92e0ab2009-02-03 05:31:23 +0000964 // If store is legal, just bitcast the src pointer.
Daniel Dunbar84379912009-02-02 19:06:38 +0000965 if (SrcSize == DstSize) {
966 llvm::Value *Casted =
967 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
968 CGF.Builder.CreateStore(Src, Casted);
969 } else {
970 assert(SrcSize > DstSize && "Coercion is missing bits!");
971
972 // Otherwise do coercion through memory. This is stupid, but
973 // simple.
974 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
975 CGF.Builder.CreateStore(Src, Tmp);
976 llvm::Value *Casted =
977 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
978 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(Casted), DstPtr);
979 }
980}
981
Daniel Dunbar04d35782008-09-17 00:51:38 +0000982/***/
983
Daniel Dunbar6ee022b2009-02-02 22:03:45 +0000984bool CodeGenModule::ReturnTypeUsesSret(const CGFunctionInfo &FI) {
Daniel Dunbar77071992009-02-03 05:59:18 +0000985 return FI.getReturnInfo().isStructRet();
Daniel Dunbar9fc15a82009-02-02 21:43:58 +0000986}
987
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000988const llvm::FunctionType *
Daniel Dunbar9fc15a82009-02-02 21:43:58 +0000989CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic) {
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000990 std::vector<const llvm::Type*> ArgTys;
991
992 const llvm::Type *ResultType = 0;
993
Daniel Dunbar0b37ca82009-02-02 23:43:58 +0000994 QualType RetTy = FI.getReturnType();
Daniel Dunbar77071992009-02-03 05:59:18 +0000995 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar22e30052008-09-11 01:48:57 +0000996 switch (RetAI.getKind()) {
997 case ABIArgInfo::ByVal:
998 case ABIArgInfo::Expand:
999 assert(0 && "Invalid ABI kind for return argument");
1000
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001001 case ABIArgInfo::Direct:
1002 ResultType = ConvertType(RetTy);
1003 break;
1004
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001005 case ABIArgInfo::StructRet: {
1006 ResultType = llvm::Type::VoidTy;
Daniel Dunbara9976a22008-09-10 07:00:50 +00001007 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001008 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
1009 break;
1010 }
1011
Daniel Dunbar1358b202009-01-26 21:26:08 +00001012 case ABIArgInfo::Ignore:
1013 ResultType = llvm::Type::VoidTy;
1014 break;
1015
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001016 case ABIArgInfo::Coerce:
Daniel Dunbar73d66602008-09-10 07:04:09 +00001017 ResultType = RetAI.getCoerceToType();
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001018 break;
1019 }
1020
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001021 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1022 ie = FI.arg_end(); it != ie; ++it) {
1023 const ABIArgInfo &AI = it->info;
1024 const llvm::Type *Ty = ConvertType(it->type);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001025
1026 switch (AI.getKind()) {
Daniel Dunbar1358b202009-01-26 21:26:08 +00001027 case ABIArgInfo::Ignore:
1028 break;
1029
Daniel Dunbar04d35782008-09-17 00:51:38 +00001030 case ABIArgInfo::Coerce:
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001031 ArgTys.push_back(AI.getCoerceToType());
1032 break;
1033
Daniel Dunbar22e30052008-09-11 01:48:57 +00001034 case ABIArgInfo::StructRet:
1035 assert(0 && "Invalid ABI kind for non-return argument");
1036
1037 case ABIArgInfo::ByVal:
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001038 // byval arguments are always on the stack, which is addr space #0.
1039 ArgTys.push_back(llvm::PointerType::getUnqual(Ty));
Daniel Dunbar22e30052008-09-11 01:48:57 +00001040 assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled");
1041 break;
1042
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001043 case ABIArgInfo::Direct:
Daniel Dunbar22e30052008-09-11 01:48:57 +00001044 ArgTys.push_back(Ty);
1045 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001046
1047 case ABIArgInfo::Expand:
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001048 GetExpandedTypes(it->type, ArgTys);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001049 break;
1050 }
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001051 }
1052
Daniel Dunbar9fc15a82009-02-02 21:43:58 +00001053 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar49f5a0d2008-09-09 23:48:28 +00001054}
1055
Daniel Dunbar0b37ca82009-02-02 23:43:58 +00001056void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001057 const Decl *TargetDecl,
Devang Patela85a9ef2008-09-25 21:02:23 +00001058 AttributeListType &PAL) {
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001059 unsigned FuncAttrs = 0;
Devang Patel2bb6eb82008-09-26 22:53:57 +00001060 unsigned RetAttrs = 0;
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001061
1062 if (TargetDecl) {
1063 if (TargetDecl->getAttr<NoThrowAttr>())
Devang Patela85a9ef2008-09-25 21:02:23 +00001064 FuncAttrs |= llvm::Attribute::NoUnwind;
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001065 if (TargetDecl->getAttr<NoReturnAttr>())
Devang Patela85a9ef2008-09-25 21:02:23 +00001066 FuncAttrs |= llvm::Attribute::NoReturn;
Anders Carlssondd6791c2008-10-05 23:32:53 +00001067 if (TargetDecl->getAttr<PureAttr>())
1068 FuncAttrs |= llvm::Attribute::ReadOnly;
1069 if (TargetDecl->getAttr<ConstAttr>())
1070 FuncAttrs |= llvm::Attribute::ReadNone;
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001071 }
1072
Daniel Dunbar0b37ca82009-02-02 23:43:58 +00001073 QualType RetTy = FI.getReturnType();
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001074 unsigned Index = 1;
Daniel Dunbar77071992009-02-03 05:59:18 +00001075 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001076 switch (RetAI.getKind()) {
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001077 case ABIArgInfo::Direct:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001078 if (RetTy->isPromotableIntegerType()) {
1079 if (RetTy->isSignedIntegerType()) {
Devang Patel2bb6eb82008-09-26 22:53:57 +00001080 RetAttrs |= llvm::Attribute::SExt;
Daniel Dunbare126ab12008-09-10 02:41:04 +00001081 } else if (RetTy->isUnsignedIntegerType()) {
Devang Patel2bb6eb82008-09-26 22:53:57 +00001082 RetAttrs |= llvm::Attribute::ZExt;
Daniel Dunbare126ab12008-09-10 02:41:04 +00001083 }
1084 }
1085 break;
1086
1087 case ABIArgInfo::StructRet:
Devang Patela85a9ef2008-09-25 21:02:23 +00001088 PAL.push_back(llvm::AttributeWithIndex::get(Index,
Daniel Dunbarebbb8f32009-01-31 02:19:00 +00001089 llvm::Attribute::StructRet |
1090 llvm::Attribute::NoAlias));
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001091 ++Index;
Daniel Dunbare126ab12008-09-10 02:41:04 +00001092 break;
1093
Daniel Dunbar1358b202009-01-26 21:26:08 +00001094 case ABIArgInfo::Ignore:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001095 case ABIArgInfo::Coerce:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001096 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001097
1098 case ABIArgInfo::ByVal:
1099 case ABIArgInfo::Expand:
1100 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001101 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001102
Devang Patel2bb6eb82008-09-26 22:53:57 +00001103 if (RetAttrs)
1104 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001105 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1106 ie = FI.arg_end(); it != ie; ++it) {
1107 QualType ParamType = it->type;
1108 const ABIArgInfo &AI = it->info;
Devang Patela85a9ef2008-09-25 21:02:23 +00001109 unsigned Attributes = 0;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001110
1111 switch (AI.getKind()) {
1112 case ABIArgInfo::StructRet:
1113 assert(0 && "Invalid ABI kind for non-return argument");
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001114
1115 case ABIArgInfo::Coerce:
1116 break;
1117
Daniel Dunbar22e30052008-09-11 01:48:57 +00001118 case ABIArgInfo::ByVal:
Devang Patela85a9ef2008-09-25 21:02:23 +00001119 Attributes |= llvm::Attribute::ByVal;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001120 assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled");
1121 break;
1122
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001123 case ABIArgInfo::Direct:
Daniel Dunbar22e30052008-09-11 01:48:57 +00001124 if (ParamType->isPromotableIntegerType()) {
1125 if (ParamType->isSignedIntegerType()) {
Devang Patela85a9ef2008-09-25 21:02:23 +00001126 Attributes |= llvm::Attribute::SExt;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001127 } else if (ParamType->isUnsignedIntegerType()) {
Devang Patela85a9ef2008-09-25 21:02:23 +00001128 Attributes |= llvm::Attribute::ZExt;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001129 }
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001130 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001131 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001132
Daniel Dunbar1358b202009-01-26 21:26:08 +00001133 case ABIArgInfo::Ignore:
1134 // Skip increment, no matching LLVM parameter.
1135 continue;
1136
Daniel Dunbar04d35782008-09-17 00:51:38 +00001137 case ABIArgInfo::Expand: {
1138 std::vector<const llvm::Type*> Tys;
1139 // FIXME: This is rather inefficient. Do we ever actually need
1140 // to do anything here? The result should be just reconstructed
1141 // on the other side, so extension should be a non-issue.
1142 getTypes().GetExpandedTypes(ParamType, Tys);
1143 Index += Tys.size();
1144 continue;
1145 }
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001146 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001147
Devang Patela85a9ef2008-09-25 21:02:23 +00001148 if (Attributes)
1149 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar04d35782008-09-17 00:51:38 +00001150 ++Index;
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001151 }
Devang Patel2bb6eb82008-09-26 22:53:57 +00001152 if (FuncAttrs)
1153 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
1154
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001155}
1156
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001157void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
1158 llvm::Function *Fn,
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001159 const FunctionArgList &Args) {
Daniel Dunbar5b7ac652009-02-03 06:02:10 +00001160 // FIXME: We no longer need the types from FunctionArgList; lift up
1161 // and simplify.
1162
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001163 // Emit allocs for param decls. Give the LLVM Argument nodes names.
1164 llvm::Function::arg_iterator AI = Fn->arg_begin();
1165
1166 // Name the struct return argument.
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001167 if (CGM.ReturnTypeUsesSret(FI)) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001168 AI->setName("agg.result");
1169 ++AI;
1170 }
Daniel Dunbar77071992009-02-03 05:59:18 +00001171
Daniel Dunbar14c884a2009-02-04 21:17:21 +00001172 assert(FI.arg_size() == Args.size() &&
1173 "Mismatch between function signature & arguments.");
Daniel Dunbar77071992009-02-03 05:59:18 +00001174 CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001175 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbar77071992009-02-03 05:59:18 +00001176 i != e; ++i, ++info_it) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001177 const VarDecl *Arg = i->first;
Daniel Dunbar77071992009-02-03 05:59:18 +00001178 QualType Ty = info_it->type;
1179 const ABIArgInfo &ArgI = info_it->info;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001180
1181 switch (ArgI.getKind()) {
1182 case ABIArgInfo::ByVal:
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001183 case ABIArgInfo::Direct: {
Daniel Dunbar22e30052008-09-11 01:48:57 +00001184 assert(AI != Fn->arg_end() && "Argument mismatch!");
1185 llvm::Value* V = AI;
Daniel Dunbar04d35782008-09-17 00:51:38 +00001186 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
Daniel Dunbar22e30052008-09-11 01:48:57 +00001187 // This must be a promotion, for something like
1188 // "void a(x) short x; {..."
Daniel Dunbar04d35782008-09-17 00:51:38 +00001189 V = EmitScalarConversion(V, Ty, Arg->getType());
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001190 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001191 EmitParmDecl(*Arg, V);
1192 break;
1193 }
Daniel Dunbar04d35782008-09-17 00:51:38 +00001194
1195 case ABIArgInfo::Expand: {
Daniel Dunbar77071992009-02-03 05:59:18 +00001196 // If this structure was expanded into multiple arguments then
Daniel Dunbar04d35782008-09-17 00:51:38 +00001197 // we need to create a temporary and reconstruct it from the
1198 // arguments.
Chris Lattner6c5ec622008-11-24 04:00:27 +00001199 std::string Name = Arg->getNameAsString();
Daniel Dunbar04d35782008-09-17 00:51:38 +00001200 llvm::Value *Temp = CreateTempAlloca(ConvertType(Ty),
1201 (Name + ".addr").c_str());
1202 // FIXME: What are the right qualifiers here?
1203 llvm::Function::arg_iterator End =
1204 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI);
1205 EmitParmDecl(*Arg, Temp);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001206
Daniel Dunbar04d35782008-09-17 00:51:38 +00001207 // Name the arguments used in expansion and increment AI.
1208 unsigned Index = 0;
1209 for (; AI != End; ++AI, ++Index)
1210 AI->setName(Name + "." + llvm::utostr(Index));
1211 continue;
1212 }
Daniel Dunbar1358b202009-01-26 21:26:08 +00001213
1214 case ABIArgInfo::Ignore:
Daniel Dunbar015bc8e2009-02-03 20:00:13 +00001215 // Skip increment, no matching LLVM parameter.
1216 continue;
Daniel Dunbar1358b202009-01-26 21:26:08 +00001217
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001218 case ABIArgInfo::Coerce: {
1219 assert(AI != Fn->arg_end() && "Argument mismatch!");
1220 // FIXME: This is very wasteful; EmitParmDecl is just going to
1221 // drop the result in a new alloca anyway, so we could just
1222 // store into that directly if we broke the abstraction down
1223 // more.
1224 llvm::Value *V = CreateTempAlloca(ConvertType(Ty), "coerce");
1225 CreateCoercedStore(AI, V, *this);
1226 // Match to what EmitParmDecl is expecting for this type.
Daniel Dunbar99473cd2009-02-04 07:22:24 +00001227 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001228 V = Builder.CreateLoad(V);
Daniel Dunbar99473cd2009-02-04 07:22:24 +00001229 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1230 // This must be a promotion, for something like
1231 // "void a(x) short x; {..."
1232 V = EmitScalarConversion(V, Ty, Arg->getType());
1233 }
1234 }
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001235 EmitParmDecl(*Arg, V);
1236 break;
1237 }
1238
Daniel Dunbar22e30052008-09-11 01:48:57 +00001239 case ABIArgInfo::StructRet:
1240 assert(0 && "Invalid ABI kind for non-return argument");
1241 }
Daniel Dunbar04d35782008-09-17 00:51:38 +00001242
1243 ++AI;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001244 }
1245 assert(AI == Fn->arg_end() && "Argument mismatch!");
1246}
1247
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001248void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI,
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001249 llvm::Value *ReturnValue) {
Daniel Dunbare126ab12008-09-10 02:41:04 +00001250 llvm::Value *RV = 0;
1251
1252 // Functions with no result always return void.
1253 if (ReturnValue) {
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001254 QualType RetTy = FI.getReturnType();
Daniel Dunbar77071992009-02-03 05:59:18 +00001255 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbare126ab12008-09-10 02:41:04 +00001256
1257 switch (RetAI.getKind()) {
1258 case ABIArgInfo::StructRet:
Daniel Dunbar17d35372008-12-18 04:52:14 +00001259 if (RetTy->isAnyComplexType()) {
1260 // FIXME: Volatile
1261 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
1262 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
1263 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1264 EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy);
1265 } else {
1266 Builder.CreateStore(Builder.CreateLoad(ReturnValue),
1267 CurFn->arg_begin());
1268 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001269 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001270
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001271 case ABIArgInfo::Direct:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001272 RV = Builder.CreateLoad(ReturnValue);
1273 break;
1274
Daniel Dunbar1358b202009-01-26 21:26:08 +00001275 case ABIArgInfo::Ignore:
1276 break;
1277
Daniel Dunbar73d66602008-09-10 07:04:09 +00001278 case ABIArgInfo::Coerce: {
Daniel Dunbar708d8a82009-01-27 01:36:03 +00001279 RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001280 break;
Daniel Dunbar73d66602008-09-10 07:04:09 +00001281 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001282
1283 case ABIArgInfo::ByVal:
1284 case ABIArgInfo::Expand:
1285 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001286 }
1287 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001288
1289 if (RV) {
1290 Builder.CreateRet(RV);
1291 } else {
1292 Builder.CreateRetVoid();
1293 }
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001294}
1295
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001296RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
1297 llvm::Value *Callee,
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001298 const CallArgList &CallArgs) {
Daniel Dunbar5b7ac652009-02-03 06:02:10 +00001299 // FIXME: We no longer need the types from CallArgs; lift up and
1300 // simplify.
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001301 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001302
1303 // Handle struct-return functions by passing a pointer to the
1304 // location that we would like to return into.
Daniel Dunbar9fc15a82009-02-02 21:43:58 +00001305 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbar77071992009-02-03 05:59:18 +00001306 const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
Daniel Dunbare126ab12008-09-10 02:41:04 +00001307 switch (RetAI.getKind()) {
1308 case ABIArgInfo::StructRet:
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001309 // Create a temporary alloca to hold the result of the call. :(
Daniel Dunbar04d35782008-09-17 00:51:38 +00001310 Args.push_back(CreateTempAlloca(ConvertType(RetTy)));
Daniel Dunbare126ab12008-09-10 02:41:04 +00001311 break;
1312
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001313 case ABIArgInfo::Direct:
Daniel Dunbar1358b202009-01-26 21:26:08 +00001314 case ABIArgInfo::Ignore:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001315 case ABIArgInfo::Coerce:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001316 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001317
1318 case ABIArgInfo::ByVal:
1319 case ABIArgInfo::Expand:
Daniel Dunbar77071992009-02-03 05:59:18 +00001320 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001321 }
1322
Daniel Dunbar14c884a2009-02-04 21:17:21 +00001323 assert(CallInfo.arg_size() == CallArgs.size() &&
1324 "Mismatch between function signature & arguments.");
Daniel Dunbar77071992009-02-03 05:59:18 +00001325 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001326 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
Daniel Dunbar77071992009-02-03 05:59:18 +00001327 I != E; ++I, ++info_it) {
1328 const ABIArgInfo &ArgInfo = info_it->info;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001329 RValue RV = I->first;
Daniel Dunbar04d35782008-09-17 00:51:38 +00001330
1331 switch (ArgInfo.getKind()) {
Daniel Dunbareec02622009-02-03 06:30:17 +00001332 case ABIArgInfo::ByVal: // Direct is byval
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001333 case ABIArgInfo::Direct:
Daniel Dunbar04d35782008-09-17 00:51:38 +00001334 if (RV.isScalar()) {
1335 Args.push_back(RV.getScalarVal());
1336 } else if (RV.isComplex()) {
1337 // Make a temporary alloca to pass the argument.
1338 Args.push_back(CreateTempAlloca(ConvertType(I->second)));
1339 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
1340 } else {
1341 Args.push_back(RV.getAggregateAddr());
1342 }
1343 break;
1344
Daniel Dunbar1358b202009-01-26 21:26:08 +00001345 case ABIArgInfo::Ignore:
1346 break;
1347
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001348 case ABIArgInfo::Coerce: {
1349 // FIXME: Avoid the conversion through memory if possible.
1350 llvm::Value *SrcPtr;
1351 if (RV.isScalar()) {
Daniel Dunbar4ce351b2009-02-03 23:04:57 +00001352 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001353 Builder.CreateStore(RV.getScalarVal(), SrcPtr);
1354 } else if (RV.isComplex()) {
1355 SrcPtr = CreateTempAlloca(ConvertType(I->second), "coerce");
1356 StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
1357 } else
1358 SrcPtr = RV.getAggregateAddr();
1359 Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
1360 *this));
1361 break;
1362 }
1363
Daniel Dunbar04d35782008-09-17 00:51:38 +00001364 case ABIArgInfo::StructRet:
Daniel Dunbar04d35782008-09-17 00:51:38 +00001365 assert(0 && "Invalid ABI kind for non-return argument");
1366 break;
1367
1368 case ABIArgInfo::Expand:
1369 ExpandTypeToArgs(I->second, RV, Args);
1370 break;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001371 }
1372 }
1373
1374 llvm::CallInst *CI = Builder.CreateCall(Callee,&Args[0],&Args[0]+Args.size());
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001375
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001376 // FIXME: Provide TargetDecl so nounwind, noreturn, etc, etc get set.
Devang Patela85a9ef2008-09-25 21:02:23 +00001377 CodeGen::AttributeListType AttributeList;
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001378 CGM.ConstructAttributeList(CallInfo, 0, AttributeList);
Devang Patela85a9ef2008-09-25 21:02:23 +00001379 CI->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(),
Daniel Dunbarebbb8f32009-01-31 02:19:00 +00001380 AttributeList.size()));
1381
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001382 if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee))
1383 CI->setCallingConv(F->getCallingConv());
1384 if (CI->getType() != llvm::Type::VoidTy)
1385 CI->setName("call");
Daniel Dunbare126ab12008-09-10 02:41:04 +00001386
1387 switch (RetAI.getKind()) {
1388 case ABIArgInfo::StructRet:
1389 if (RetTy->isAnyComplexType())
Daniel Dunbar04d35782008-09-17 00:51:38 +00001390 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Daniel Dunbar17d35372008-12-18 04:52:14 +00001391 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar04d35782008-09-17 00:51:38 +00001392 return RValue::getAggregate(Args[0]);
Daniel Dunbar17d35372008-12-18 04:52:14 +00001393 else
1394 return RValue::get(Builder.CreateLoad(Args[0]));
Daniel Dunbar22e30052008-09-11 01:48:57 +00001395
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001396 case ABIArgInfo::Direct:
1397 assert((!RetTy->isAnyComplexType() &&
Daniel Dunbareec02622009-02-03 06:30:17 +00001398 !CodeGenFunction::hasAggregateLLVMType(RetTy)) &&
1399 "FIXME: Implement return for non-scalar direct types.");
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001400 return RValue::get(CI);
Daniel Dunbare126ab12008-09-10 02:41:04 +00001401
Daniel Dunbar1358b202009-01-26 21:26:08 +00001402 case ABIArgInfo::Ignore:
Daniel Dunbar5c9c7f02009-01-29 08:24:57 +00001403 if (RetTy->isVoidType())
1404 return RValue::get(0);
Daniel Dunbareec02622009-02-03 06:30:17 +00001405
1406 // If we are ignoring an argument that had a result, make sure to
1407 // construct the appropriate return value for our caller.
Daniel Dunbar5c9c7f02009-01-29 08:24:57 +00001408 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1409 llvm::Value *Res =
1410 llvm::UndefValue::get(llvm::PointerType::getUnqual(ConvertType(RetTy)));
1411 return RValue::getAggregate(Res);
1412 }
1413 return RValue::get(llvm::UndefValue::get(ConvertType(RetTy)));
Daniel Dunbar1358b202009-01-26 21:26:08 +00001414
Daniel Dunbar73d66602008-09-10 07:04:09 +00001415 case ABIArgInfo::Coerce: {
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001416 // FIXME: Avoid the conversion through memory if possible.
Daniel Dunbar708d8a82009-01-27 01:36:03 +00001417 llvm::Value *V = CreateTempAlloca(ConvertType(RetTy), "coerce");
1418 CreateCoercedStore(CI, V, *this);
Anders Carlssonfccf7472008-11-25 22:21:48 +00001419 if (RetTy->isAnyComplexType())
1420 return RValue::getComplex(LoadComplexFromAddr(V, false));
Daniel Dunbar1358b202009-01-26 21:26:08 +00001421 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssonfccf7472008-11-25 22:21:48 +00001422 return RValue::getAggregate(V);
Daniel Dunbar1358b202009-01-26 21:26:08 +00001423 else
1424 return RValue::get(Builder.CreateLoad(V));
Daniel Dunbar73d66602008-09-10 07:04:09 +00001425 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001426
1427 case ABIArgInfo::ByVal:
1428 case ABIArgInfo::Expand:
1429 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001430 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001431
1432 assert(0 && "Unhandled ABIArgInfo::Kind");
1433 return RValue::get(0);
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001434}