blob: a3dd877ce58b59058a04a2c42523596bcd267569 [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 Dunbar9f4874e2009-02-04 23:24:38 +000026#include "llvm/Support/raw_ostream.h"
Daniel Dunbar708d8a82009-01-27 01:36:03 +000027#include "llvm/Target/TargetData.h"
Daniel Dunbard283e632009-02-03 01:05:53 +000028
29#include "ABIInfo.h"
30
Daniel Dunbara8f02052008-09-08 21:33:45 +000031using namespace clang;
32using namespace CodeGen;
33
34/***/
35
Daniel Dunbara8f02052008-09-08 21:33:45 +000036// FIXME: Use iterator and sidestep silly type array creation.
37
Daniel Dunbar34bda882009-02-02 23:23:47 +000038const
39CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionTypeNoProto *FTNP) {
40 return getFunctionInfo(FTNP->getResultType(),
41 llvm::SmallVector<QualType, 16>());
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000042}
43
Daniel Dunbar34bda882009-02-02 23:23:47 +000044const
45CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionTypeProto *FTP) {
46 llvm::SmallVector<QualType, 16> ArgTys;
47 // FIXME: Kill copy.
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000048 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
Daniel Dunbar34bda882009-02-02 23:23:47 +000049 ArgTys.push_back(FTP->getArgType(i));
50 return getFunctionInfo(FTP->getResultType(), ArgTys);
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000051}
52
Daniel Dunbar34bda882009-02-02 23:23:47 +000053const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionDecl *FD) {
Daniel Dunbara8f02052008-09-08 21:33:45 +000054 const FunctionType *FTy = FD->getType()->getAsFunctionType();
Daniel Dunbar34bda882009-02-02 23:23:47 +000055 if (const FunctionTypeProto *FTP = dyn_cast<FunctionTypeProto>(FTy))
56 return getFunctionInfo(FTP);
57 return getFunctionInfo(cast<FunctionTypeNoProto>(FTy));
Daniel Dunbara8f02052008-09-08 21:33:45 +000058}
59
Daniel Dunbar34bda882009-02-02 23:23:47 +000060const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const ObjCMethodDecl *MD) {
61 llvm::SmallVector<QualType, 16> ArgTys;
62 ArgTys.push_back(MD->getSelfDecl()->getType());
63 ArgTys.push_back(Context.getObjCSelType());
64 // FIXME: Kill copy?
Daniel Dunbara8f02052008-09-08 21:33:45 +000065 for (ObjCMethodDecl::param_const_iterator i = MD->param_begin(),
66 e = MD->param_end(); i != e; ++i)
Daniel Dunbar34bda882009-02-02 23:23:47 +000067 ArgTys.push_back((*i)->getType());
68 return getFunctionInfo(MD->getResultType(), ArgTys);
Daniel Dunbara8f02052008-09-08 21:33:45 +000069}
70
Daniel Dunbar34bda882009-02-02 23:23:47 +000071const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
72 const CallArgList &Args) {
73 // FIXME: Kill copy.
74 llvm::SmallVector<QualType, 16> ArgTys;
Daniel Dunbarebbb8f32009-01-31 02:19:00 +000075 for (CallArgList::const_iterator i = Args.begin(), e = Args.end();
76 i != e; ++i)
Daniel Dunbar34bda882009-02-02 23:23:47 +000077 ArgTys.push_back(i->second);
78 return getFunctionInfo(ResTy, ArgTys);
Daniel Dunbarebbb8f32009-01-31 02:19:00 +000079}
80
Daniel Dunbar34bda882009-02-02 23:23:47 +000081const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
82 const FunctionArgList &Args) {
83 // FIXME: Kill copy.
84 llvm::SmallVector<QualType, 16> ArgTys;
Daniel Dunbar9fc15a82009-02-02 21:43:58 +000085 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
86 i != e; ++i)
Daniel Dunbar34bda882009-02-02 23:23:47 +000087 ArgTys.push_back(i->second);
88 return getFunctionInfo(ResTy, ArgTys);
89}
90
91const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
92 const llvm::SmallVector<QualType, 16> &ArgTys) {
Daniel Dunbardcf19d12009-02-03 00:07:12 +000093 // Lookup or create unique function info.
94 llvm::FoldingSetNodeID ID;
95 CGFunctionInfo::Profile(ID, ResTy, ArgTys.begin(), ArgTys.end());
96
97 void *InsertPos = 0;
98 CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, InsertPos);
99 if (FI)
100 return *FI;
101
Daniel Dunbare92e0ab2009-02-03 05:31:23 +0000102 // Construct the function info.
Daniel Dunbardcf19d12009-02-03 00:07:12 +0000103 FI = new CGFunctionInfo(ResTy, ArgTys);
Daniel Dunbar5991d312009-02-04 21:36:22 +0000104
105 // FIXME: This is leaking like a sieve; please fix me.
106 // FunctionInfos.InsertNode(FI, InsertPos);
Daniel Dunbare92e0ab2009-02-03 05:31:23 +0000107
108 // Compute ABI information.
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000109 getABIInfo().computeInfo(*FI, getContext());
Daniel Dunbare92e0ab2009-02-03 05:31:23 +0000110
Daniel Dunbardcf19d12009-02-03 00:07:12 +0000111 return *FI;
Daniel Dunbar34bda882009-02-02 23:23:47 +0000112}
113
114/***/
115
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000116ABIInfo::~ABIInfo() {}
117
Daniel Dunbar9f4874e2009-02-04 23:24:38 +0000118void ABIArgInfo::dump() const {
119 fprintf(stderr, "(ABIArgInfo Kind=");
120 switch (TheKind) {
121 case Direct:
122 fprintf(stderr, "Direct");
123 break;
124 case StructRet:
125 fprintf(stderr, "StructRet");
126 break;
127 case Ignore:
128 fprintf(stderr, "Ignore");
129 break;
130 case Coerce:
131 fprintf(stderr, "Coerce Type=");
132 getCoerceToType()->print(llvm::errs());
133 // FIXME: This is ridiculous.
134 llvm::errs().flush();
135 break;
136 case ByVal:
137 fprintf(stderr, "ByVal Align=%d", getByValAlignment());
138 break;
139 case Expand:
140 fprintf(stderr, "Expand");
141 break;
142 }
143 fprintf(stderr, ")\n");
144}
145
146/***/
147
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000148/// isEmptyStruct - Return true iff a structure has no non-empty
149/// members. Note that a structure with a flexible array member is not
150/// considered empty.
151static bool isEmptyStruct(QualType T) {
152 const RecordType *RT = T->getAsStructureType();
153 if (!RT)
154 return 0;
155 const RecordDecl *RD = RT->getDecl();
156 if (RD->hasFlexibleArrayMember())
157 return false;
Douglas Gregor5d764842009-01-09 17:18:27 +0000158 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000159 e = RD->field_end(); i != e; ++i) {
160 const FieldDecl *FD = *i;
161 if (!isEmptyStruct(FD->getType()))
162 return false;
163 }
164 return true;
165}
166
167/// isSingleElementStruct - Determine if a structure is a "single
168/// element struct", i.e. it has exactly one non-empty field or
169/// exactly one field which is itself a single element
170/// struct. Structures with flexible array members are never
171/// considered single element structs.
172///
173/// \return The field declaration for the single non-empty field, if
174/// it exists.
175static const FieldDecl *isSingleElementStruct(QualType T) {
176 const RecordType *RT = T->getAsStructureType();
177 if (!RT)
178 return 0;
179
180 const RecordDecl *RD = RT->getDecl();
181 if (RD->hasFlexibleArrayMember())
182 return 0;
183
184 const FieldDecl *Found = 0;
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 QualType FT = FD->getType();
189
190 if (isEmptyStruct(FT)) {
191 // Ignore
192 } else if (Found) {
193 return 0;
194 } else if (!CodeGenFunction::hasAggregateLLVMType(FT)) {
195 Found = FD;
196 } else {
197 Found = isSingleElementStruct(FT);
198 if (!Found)
199 return 0;
200 }
201 }
202
203 return Found;
204}
205
206static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
207 if (!Ty->getAsBuiltinType() && !Ty->isPointerType())
208 return false;
209
210 uint64_t Size = Context.getTypeSize(Ty);
211 return Size == 32 || Size == 64;
212}
213
214static bool areAllFields32Or64BitBasicType(const RecordDecl *RD,
215 ASTContext &Context) {
Douglas Gregor5d764842009-01-09 17:18:27 +0000216 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000217 e = RD->field_end(); i != e; ++i) {
218 const FieldDecl *FD = *i;
219
220 if (!is32Or64BitBasicType(FD->getType(), Context))
221 return false;
222
223 // If this is a bit-field we need to make sure it is still a
224 // 32-bit or 64-bit type.
225 if (Expr *BW = FD->getBitWidth()) {
226 unsigned Width = BW->getIntegerConstantExprValue(Context).getZExtValue();
227 if (Width <= 16)
228 return false;
229 }
230 }
231 return true;
232}
233
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000234namespace {
235/// DefaultABIInfo - The default implementation for ABI specific
236/// details. This implementation provides information which results in
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000237/// self-consistent and sensible LLVM IR generation, but does not
238/// conform to any particular ABI.
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000239class DefaultABIInfo : public ABIInfo {
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000240 ABIArgInfo classifyReturnType(QualType RetTy,
241 ASTContext &Context) const;
242
243 ABIArgInfo classifyArgumentType(QualType RetTy,
244 ASTContext &Context) const;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000245
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000246 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
247 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
248 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
249 it != ie; ++it)
250 it->info = classifyArgumentType(it->type, Context);
251 }
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000252};
253
254/// X86_32ABIInfo - The X86-32 ABI information.
255class X86_32ABIInfo : public ABIInfo {
256public:
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000257 ABIArgInfo classifyReturnType(QualType RetTy,
258 ASTContext &Context) const;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000259
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000260 ABIArgInfo classifyArgumentType(QualType RetTy,
261 ASTContext &Context) const;
262
263 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
264 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
265 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
266 it != ie; ++it)
267 it->info = classifyArgumentType(it->type, Context);
268 }
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000269};
270}
271
272ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
273 ASTContext &Context) const {
Daniel Dunbareec02622009-02-03 06:30:17 +0000274 if (RetTy->isVoidType()) {
275 return ABIArgInfo::getIgnore();
276 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000277 // Classify "single element" structs as their element type.
278 const FieldDecl *SeltFD = isSingleElementStruct(RetTy);
279 if (SeltFD) {
280 QualType SeltTy = SeltFD->getType()->getDesugaredType();
281 if (const BuiltinType *BT = SeltTy->getAsBuiltinType()) {
282 // FIXME: This is gross, it would be nice if we could just
283 // pass back SeltTy and have clients deal with it. Is it worth
284 // supporting coerce to both LLVM and clang Types?
285 if (BT->isIntegerType()) {
286 uint64_t Size = Context.getTypeSize(SeltTy);
287 return ABIArgInfo::getCoerce(llvm::IntegerType::get((unsigned) Size));
288 } else if (BT->getKind() == BuiltinType::Float) {
289 return ABIArgInfo::getCoerce(llvm::Type::FloatTy);
290 } else if (BT->getKind() == BuiltinType::Double) {
291 return ABIArgInfo::getCoerce(llvm::Type::DoubleTy);
292 }
293 } else if (SeltTy->isPointerType()) {
294 // FIXME: It would be really nice if this could come out as
295 // the proper pointer type.
296 llvm::Type *PtrTy =
297 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
298 return ABIArgInfo::getCoerce(PtrTy);
299 }
300 }
301
Daniel Dunbar73d66602008-09-10 07:04:09 +0000302 uint64_t Size = Context.getTypeSize(RetTy);
303 if (Size == 8) {
304 return ABIArgInfo::getCoerce(llvm::Type::Int8Ty);
305 } else if (Size == 16) {
306 return ABIArgInfo::getCoerce(llvm::Type::Int16Ty);
307 } else if (Size == 32) {
308 return ABIArgInfo::getCoerce(llvm::Type::Int32Ty);
309 } else if (Size == 64) {
310 return ABIArgInfo::getCoerce(llvm::Type::Int64Ty);
311 } else {
312 return ABIArgInfo::getStructRet();
313 }
Daniel Dunbare126ab12008-09-10 02:41:04 +0000314 } else {
Daniel Dunbareec02622009-02-03 06:30:17 +0000315 return ABIArgInfo::getDirect();
Daniel Dunbare126ab12008-09-10 02:41:04 +0000316 }
317}
318
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000319ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
320 ASTContext &Context) const {
Daniel Dunbar3158c592008-09-17 20:11:04 +0000321 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000322 // Structures with flexible arrays are always byval.
323 if (const RecordType *RT = Ty->getAsStructureType())
324 if (RT->getDecl()->hasFlexibleArrayMember())
325 return ABIArgInfo::getByVal(0);
326
327 // Expand empty structs (i.e. ignore)
328 uint64_t Size = Context.getTypeSize(Ty);
329 if (Ty->isStructureType() && Size == 0)
330 return ABIArgInfo::getExpand();
331
332 // Expand structs with size <= 128-bits which consist only of
333 // basic types (int, long long, float, double, xxx*). This is
334 // non-recursive and does not ignore empty fields.
335 if (const RecordType *RT = Ty->getAsStructureType()) {
336 if (Context.getTypeSize(Ty) <= 4*32 &&
337 areAllFields32Or64BitBasicType(RT->getDecl(), Context))
338 return ABIArgInfo::getExpand();
339 }
340
Daniel Dunbar22e30052008-09-11 01:48:57 +0000341 return ABIArgInfo::getByVal(0);
342 } else {
Daniel Dunbareec02622009-02-03 06:30:17 +0000343 return ABIArgInfo::getDirect();
Daniel Dunbar22e30052008-09-11 01:48:57 +0000344 }
345}
346
Daniel Dunbare09a9692009-01-24 08:32:22 +0000347namespace {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000348/// X86_64ABIInfo - The X86_64 ABI information.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000349class X86_64ABIInfo : public ABIInfo {
350 enum Class {
351 Integer = 0,
352 SSE,
353 SSEUp,
354 X87,
355 X87Up,
356 ComplexX87,
357 NoClass,
358 Memory
359 };
360
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000361 /// merge - Implement the X86_64 ABI merging algorithm.
362 ///
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000363 /// Merge an accumulating classification \arg Accum with a field
364 /// classification \arg Field.
365 ///
366 /// \param Accum - The accumulating classification. This should
367 /// always be either NoClass or the result of a previous merge
368 /// call. In addition, this should never be Memory (the caller
369 /// should just return Memory for the aggregate).
370 Class merge(Class Accum, Class Field) const;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000371
Daniel Dunbare09a9692009-01-24 08:32:22 +0000372 /// classify - Determine the x86_64 register classes in which the
373 /// given type T should be passed.
374 ///
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000375 /// \param Lo - The classification for the parts of the type
376 /// residing in the low word of the containing object.
377 ///
378 /// \param Hi - The classification for the parts of the type
379 /// residing in the high word of the containing object.
380 ///
381 /// \param OffsetBase - The bit offset of this type in the
Daniel Dunbar2a2dce32009-01-30 22:40:15 +0000382 /// containing object. Some parameters are classified different
383 /// depending on whether they straddle an eightbyte boundary.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000384 ///
385 /// If a word is unused its result will be NoClass; if a type should
386 /// be passed in Memory then at least the classification of \arg Lo
387 /// will be Memory.
388 ///
389 /// The \arg Lo class will be NoClass iff the argument is ignored.
390 ///
391 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
392 /// be NoClass.
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000393 void classify(QualType T, ASTContext &Context, uint64_t OffsetBase,
Daniel Dunbare09a9692009-01-24 08:32:22 +0000394 Class &Lo, Class &Hi) const;
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000395
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000396 ABIArgInfo classifyReturnType(QualType RetTy,
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000397 ASTContext &Context) const;
398
399 ABIArgInfo classifyArgumentType(QualType Ty,
400 ASTContext &Context,
401 unsigned &freeIntRegs,
402 unsigned &freeSSERegs) const;
403
404public:
405 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000406};
407}
408
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000409X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum,
410 Class Field) const {
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000411 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
412 // classified recursively so that always two fields are
413 // considered. The resulting class is calculated according to
414 // the classes of the fields in the eightbyte:
415 //
416 // (a) If both classes are equal, this is the resulting class.
417 //
418 // (b) If one of the classes is NO_CLASS, the resulting class is
419 // the other class.
420 //
421 // (c) If one of the classes is MEMORY, the result is the MEMORY
422 // class.
423 //
424 // (d) If one of the classes is INTEGER, the result is the
425 // INTEGER.
426 //
427 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
428 // MEMORY is used as class.
429 //
430 // (f) Otherwise class SSE is used.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000431 assert((Accum == NoClass || Accum == Integer ||
432 Accum == SSE || Accum == SSEUp) &&
433 "Invalid accumulated classification during merge.");
434 if (Accum == Field || Field == NoClass)
435 return Accum;
436 else if (Field == Memory)
437 return Memory;
438 else if (Accum == NoClass)
439 return Field;
440 else if (Accum == Integer || Field == Integer)
441 return Integer;
442 else if (Field == X87 || Field == X87Up || Field == ComplexX87)
443 return Memory;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000444 else
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000445 return SSE;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000446}
447
Daniel Dunbare09a9692009-01-24 08:32:22 +0000448void X86_64ABIInfo::classify(QualType Ty,
449 ASTContext &Context,
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000450 uint64_t OffsetBase,
Daniel Dunbare09a9692009-01-24 08:32:22 +0000451 Class &Lo, Class &Hi) const {
Daniel Dunbar36b378e2009-02-02 18:06:39 +0000452 // FIXME: This code can be simplified by introducing a simple value
453 // class for Class pairs with appropriate constructor methods for
454 // the various situations.
455
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000456 Lo = Hi = NoClass;
457
458 Class &Current = OffsetBase < 64 ? Lo : Hi;
459 Current = Memory;
460
Daniel Dunbare09a9692009-01-24 08:32:22 +0000461 if (const BuiltinType *BT = Ty->getAsBuiltinType()) {
462 BuiltinType::Kind k = BT->getKind();
463
Daniel Dunbar1358b202009-01-26 21:26:08 +0000464 if (k == BuiltinType::Void) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000465 Current = NoClass;
Daniel Dunbar1358b202009-01-26 21:26:08 +0000466 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000467 Current = Integer;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000468 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000469 Current = SSE;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000470 } else if (k == BuiltinType::LongDouble) {
471 Lo = X87;
472 Hi = X87Up;
473 }
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000474 // FIXME: _Decimal32 and _Decimal64 are SSE.
475 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Daniel Dunbare09a9692009-01-24 08:32:22 +0000476 // FIXME: __int128 is (Integer, Integer).
477 } else if (Ty->isPointerLikeType() || Ty->isBlockPointerType() ||
478 Ty->isObjCQualifiedInterfaceType()) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000479 Current = Integer;
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000480 } else if (const VectorType *VT = Ty->getAsVectorType()) {
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000481 uint64_t Size = Context.getTypeSize(VT);
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000482 if (Size == 64) {
Daniel Dunbarcdf91e82009-01-30 19:38:39 +0000483 // gcc passes <1 x double> in memory.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000484 if (VT->getElementType() == Context.DoubleTy)
Daniel Dunbarcdf91e82009-01-30 19:38:39 +0000485 return;
Daniel Dunbarcdf91e82009-01-30 19:38:39 +0000486
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000487 Current = SSE;
Daniel Dunbare413f532009-01-30 18:40:10 +0000488
489 // If this type crosses an eightbyte boundary, it should be
490 // split.
Daniel Dunbar2a2dce32009-01-30 22:40:15 +0000491 if (OffsetBase && OffsetBase != 64)
Daniel Dunbare413f532009-01-30 18:40:10 +0000492 Hi = Lo;
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000493 } else if (Size == 128) {
494 Lo = SSE;
495 Hi = SSEUp;
496 }
Daniel Dunbare09a9692009-01-24 08:32:22 +0000497 } else if (const ComplexType *CT = Ty->getAsComplexType()) {
498 QualType ET = CT->getElementType();
499
Daniel Dunbare413f532009-01-30 18:40:10 +0000500 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbar28770fc2009-01-29 07:22:20 +0000501 if (ET->isIntegerType()) {
Daniel Dunbar28770fc2009-01-29 07:22:20 +0000502 if (Size <= 64)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000503 Current = Integer;
Daniel Dunbar28770fc2009-01-29 07:22:20 +0000504 else if (Size <= 128)
505 Lo = Hi = Integer;
506 } else if (ET == Context.FloatTy)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000507 Current = SSE;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000508 else if (ET == Context.DoubleTy)
509 Lo = Hi = SSE;
510 else if (ET == Context.LongDoubleTy)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000511 Current = ComplexX87;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000512
513 // If this complex type crosses an eightbyte boundary then it
514 // should be split.
Daniel Dunbar2a2dce32009-01-30 22:40:15 +0000515 uint64_t EB_Real = (OffsetBase) / 64;
516 uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000517 if (Hi == NoClass && EB_Real != EB_Imag)
518 Hi = Lo;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000519 } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
520 // Arrays are treated like structures.
521
522 uint64_t Size = Context.getTypeSize(Ty);
523
524 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
525 // than two eightbytes, ..., it has class MEMORY.
526 if (Size > 128)
527 return;
528
529 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
530 // fields, it has class MEMORY.
531 //
532 // Only need to check alignment of array base.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000533 if (OffsetBase % Context.getTypeAlign(AT->getElementType()))
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000534 return;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000535
536 // Otherwise implement simplified merge. We could be smarter about
537 // this, but it isn't worth it and would be harder to verify.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000538 Current = NoClass;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000539 uint64_t EltSize = Context.getTypeSize(AT->getElementType());
540 uint64_t ArraySize = AT->getSize().getZExtValue();
541 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
542 Class FieldLo, FieldHi;
543 classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000544 Lo = merge(Lo, FieldLo);
545 Hi = merge(Hi, FieldHi);
546 if (Lo == Memory || Hi == Memory)
547 break;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000548 }
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000549
550 // Do post merger cleanup (see below). Only case we worry about is Memory.
551 if (Hi == Memory)
552 Lo = Memory;
553 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000554 } else if (const RecordType *RT = Ty->getAsRecordType()) {
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000555 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000556
557 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
558 // than two eightbytes, ..., it has class MEMORY.
559 if (Size > 128)
560 return;
561
562 const RecordDecl *RD = RT->getDecl();
563
564 // Assume variable sized types are passed in memory.
565 if (RD->hasFlexibleArrayMember())
566 return;
567
568 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
569
570 // Reset Lo class, this will be recomputed.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000571 Current = NoClass;
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000572 unsigned idx = 0;
573 for (RecordDecl::field_iterator i = RD->field_begin(),
574 e = RD->field_end(); i != e; ++i, ++idx) {
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000575 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000576
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000577 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
578 // fields, it has class MEMORY.
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000579 if (Offset % Context.getTypeAlign(i->getType())) {
580 Lo = Memory;
581 return;
582 }
583
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000584 // Classify this field.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000585 //
586 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
587 // exceeds a single eightbyte, each is classified
588 // separately. Each eightbyte gets initialized to class
589 // NO_CLASS.
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000590 Class FieldLo, FieldHi;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000591 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000592 Lo = merge(Lo, FieldLo);
593 Hi = merge(Hi, FieldHi);
594 if (Lo == Memory || Hi == Memory)
595 break;
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000596 }
597
598 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
599 //
600 // (a) If one of the classes is MEMORY, the whole argument is
601 // passed in memory.
602 //
603 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
604
605 // The first of these conditions is guaranteed by how we implement
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000606 // the merge (just bail).
607 //
608 // The second condition occurs in the case of unions; for example
609 // union { _Complex double; unsigned; }.
610 if (Hi == Memory)
611 Lo = Memory;
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000612 if (Hi == SSEUp && Lo != SSE)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000613 Hi = SSE;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000614 }
615}
616
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000617
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000618ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
619 ASTContext &Context) const {
Daniel Dunbare09a9692009-01-24 08:32:22 +0000620 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
621 // classification algorithm.
622 X86_64ABIInfo::Class Lo, Hi;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000623 classify(RetTy, Context, 0, Lo, Hi);
Daniel Dunbare09a9692009-01-24 08:32:22 +0000624
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000625 // Check some invariants.
626 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
627 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
628 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
629
Daniel Dunbare09a9692009-01-24 08:32:22 +0000630 const llvm::Type *ResType = 0;
631 switch (Lo) {
632 case NoClass:
Daniel Dunbar1358b202009-01-26 21:26:08 +0000633 return ABIArgInfo::getIgnore();
Daniel Dunbare09a9692009-01-24 08:32:22 +0000634
635 case SSEUp:
636 case X87Up:
637 assert(0 && "Invalid classification for lo word.");
638
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000639 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
640 // hidden argument, i.e. structret.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000641 case Memory:
642 return ABIArgInfo::getStructRet();
643
644 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
645 // available register of the sequence %rax, %rdx is used.
646 case Integer:
647 ResType = llvm::Type::Int64Ty; break;
648
649 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
650 // available SSE register of the sequence %xmm0, %xmm1 is used.
651 case SSE:
652 ResType = llvm::Type::DoubleTy; break;
653
654 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
655 // returned on the X87 stack in %st0 as 80-bit x87 number.
656 case X87:
657 ResType = llvm::Type::X86_FP80Ty; break;
658
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000659 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
660 // part of the value is returned in %st0 and the imaginary part in
661 // %st1.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000662 case ComplexX87:
663 assert(Hi == NoClass && "Unexpected ComplexX87 classification.");
664 ResType = llvm::VectorType::get(llvm::Type::X86_FP80Ty, 2);
665 break;
666 }
667
668 switch (Hi) {
669 // Memory was handled previously, and ComplexX87 and X87 should
670 // never occur as hi classes.
671 case Memory:
672 case X87:
673 case ComplexX87:
674 assert(0 && "Invalid classification for hi word.");
675
676 case NoClass: break;
677 case Integer:
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000678 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
679 break;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000680 case SSE:
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000681 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
682 break;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000683
684 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
685 // is passed in the upper half of the last used SSE register.
686 //
687 // SSEUP should always be preceeded by SSE, just widen.
688 case SSEUp:
689 assert(Lo == SSE && "Unexpected SSEUp classification.");
690 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
691 break;
692
693 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000694 // returned together with the previous X87 value in %st0.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000695 //
696 // X87UP should always be preceeded by X87, so we don't need to do
697 // anything here.
698 case X87Up:
699 assert(Lo == X87 && "Unexpected X87Up classification.");
700 break;
701 }
702
703 return ABIArgInfo::getCoerce(ResType);
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000704}
705
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000706ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, ASTContext &Context,
707 unsigned &freeIntRegs,
708 unsigned &freeSSERegs) const {
709 X86_64ABIInfo::Class Lo, Hi;
710 classify(Ty, Context, 0, Lo, Hi);
711
712 // Check some invariants.
713 // FIXME: Enforce these by construction.
714 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
715 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
716 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
717
718 unsigned neededInt = 0, neededSSE = 0;
719 const llvm::Type *ResType = 0;
720 switch (Lo) {
721 case NoClass:
722 return ABIArgInfo::getIgnore();
723
724 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
725 // on the stack.
726 case Memory:
727
728 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
729 // COMPLEX_X87, it is passed in memory.
730 case X87:
731 case ComplexX87:
732 // Choose appropriate in memory type.
733 if (CodeGenFunction::hasAggregateLLVMType(Ty))
734 return ABIArgInfo::getByVal(0);
735 else
736 return ABIArgInfo::getDirect();
737
738 case SSEUp:
739 case X87Up:
740 assert(0 && "Invalid classification for lo word.");
741
742 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
743 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
744 // and %r9 is used.
745 case Integer:
746 ++neededInt;
747 ResType = llvm::Type::Int64Ty;
748 break;
749
750 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
751 // available SSE register is used, the registers are taken in the
752 // order from %xmm0 to %xmm7.
753 case SSE:
754 ++neededSSE;
755 ResType = llvm::Type::DoubleTy;
756 break;
Daniel Dunbareec02622009-02-03 06:30:17 +0000757 }
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000758
759 switch (Hi) {
760 // Memory was handled previously, ComplexX87 and X87 should
761 // never occur as hi classes, and X87Up must be preceed by X87,
762 // which is passed in memory.
763 case Memory:
764 case X87:
765 case X87Up:
766 case ComplexX87:
767 assert(0 && "Invalid classification for hi word.");
768
769 case NoClass: break;
770 case Integer:
771 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
772 ++neededInt;
773 break;
774 case SSE:
775 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
776 ++neededSSE;
777 break;
778
779 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
780 // eightbyte is passed in the upper half of the last used SSE
781 // register.
782 case SSEUp:
783 assert(Lo == SSE && "Unexpected SSEUp classification.");
784 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
785 break;
786 }
787
788 // AMD64-ABI 3.2.3p3: If there are no registers available for any
789 // eightbyte of an argument, the whole argument is passed on the
790 // stack. If registers have already been assigned for some
791 // eightbytes of such an argument, the assignments get reverted.
792 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
793 freeIntRegs -= neededInt;
794 freeSSERegs -= neededSSE;
795 return ABIArgInfo::getCoerce(ResType);
796 } else {
797 // Choose appropriate in memory type.
798 if (CodeGenFunction::hasAggregateLLVMType(Ty))
799 return ABIArgInfo::getByVal(0);
800 else
801 return ABIArgInfo::getDirect();
802 }
803}
804
805void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
806 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
807
808 // Keep track of the number of assigned registers.
809 unsigned freeIntRegs = 6, freeSSERegs = 8;
810
811 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
812 // get assigned (in left-to-right order) for passing as follows...
813 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
814 it != ie; ++it)
815 it->info = classifyArgumentType(it->type, Context, freeIntRegs, freeSSERegs);
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000816}
817
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000818ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
819 ASTContext &Context) const {
Daniel Dunbareec02622009-02-03 06:30:17 +0000820 if (RetTy->isVoidType()) {
821 return ABIArgInfo::getIgnore();
822 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
823 return ABIArgInfo::getStructRet();
824 } else {
825 return ABIArgInfo::getDirect();
826 }
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000827}
828
829ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
830 ASTContext &Context) const {
Daniel Dunbareec02622009-02-03 06:30:17 +0000831 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
832 return ABIArgInfo::getByVal(0);
833 } else {
834 return ABIArgInfo::getDirect();
835 }
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000836}
837
838const ABIInfo &CodeGenTypes::getABIInfo() const {
839 if (TheABIInfo)
840 return *TheABIInfo;
841
842 // For now we just cache this in the CodeGenTypes and don't bother
843 // to free it.
844 const char *TargetPrefix = getContext().Target.getTargetPrefix();
845 if (strcmp(TargetPrefix, "x86") == 0) {
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000846 switch (getContext().Target.getPointerWidth(0)) {
847 case 32:
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000848 return *(TheABIInfo = new X86_32ABIInfo());
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000849 case 64:
Daniel Dunbar56555952009-01-30 18:47:53 +0000850 return *(TheABIInfo = new X86_64ABIInfo());
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000851 }
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000852 }
853
854 return *(TheABIInfo = new DefaultABIInfo);
855}
856
Daniel Dunbare126ab12008-09-10 02:41:04 +0000857/***/
858
Daniel Dunbare92e0ab2009-02-03 05:31:23 +0000859CGFunctionInfo::CGFunctionInfo(QualType ResTy,
860 const llvm::SmallVector<QualType, 16> &ArgTys) {
861 NumArgs = ArgTys.size();
862 Args = new ArgInfo[1 + NumArgs];
863 Args[0].type = ResTy;
864 for (unsigned i = 0; i < NumArgs; ++i)
865 Args[1 + i].type = ArgTys[i];
866}
867
868/***/
869
Daniel Dunbar04d35782008-09-17 00:51:38 +0000870void CodeGenTypes::GetExpandedTypes(QualType Ty,
871 std::vector<const llvm::Type*> &ArgTys) {
872 const RecordType *RT = Ty->getAsStructureType();
873 assert(RT && "Can only expand structure types.");
874 const RecordDecl *RD = RT->getDecl();
875 assert(!RD->hasFlexibleArrayMember() &&
876 "Cannot expand structure with flexible array.");
877
Douglas Gregor5d764842009-01-09 17:18:27 +0000878 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar04d35782008-09-17 00:51:38 +0000879 e = RD->field_end(); i != e; ++i) {
880 const FieldDecl *FD = *i;
881 assert(!FD->isBitField() &&
882 "Cannot expand structure with bit-field members.");
883
884 QualType FT = FD->getType();
885 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
886 GetExpandedTypes(FT, ArgTys);
887 } else {
888 ArgTys.push_back(ConvertType(FT));
889 }
890 }
891}
892
893llvm::Function::arg_iterator
894CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
895 llvm::Function::arg_iterator AI) {
896 const RecordType *RT = Ty->getAsStructureType();
897 assert(RT && "Can only expand structure types.");
898
899 RecordDecl *RD = RT->getDecl();
900 assert(LV.isSimple() &&
901 "Unexpected non-simple lvalue during struct expansion.");
902 llvm::Value *Addr = LV.getAddress();
903 for (RecordDecl::field_iterator i = RD->field_begin(),
904 e = RD->field_end(); i != e; ++i) {
905 FieldDecl *FD = *i;
906 QualType FT = FD->getType();
907
908 // FIXME: What are the right qualifiers here?
909 LValue LV = EmitLValueForField(Addr, FD, false, 0);
910 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
911 AI = ExpandTypeFromArgs(FT, LV, AI);
912 } else {
913 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
914 ++AI;
915 }
916 }
917
918 return AI;
919}
920
921void
922CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
923 llvm::SmallVector<llvm::Value*, 16> &Args) {
924 const RecordType *RT = Ty->getAsStructureType();
925 assert(RT && "Can only expand structure types.");
926
927 RecordDecl *RD = RT->getDecl();
928 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
929 llvm::Value *Addr = RV.getAggregateAddr();
930 for (RecordDecl::field_iterator i = RD->field_begin(),
931 e = RD->field_end(); i != e; ++i) {
932 FieldDecl *FD = *i;
933 QualType FT = FD->getType();
934
935 // FIXME: What are the right qualifiers here?
936 LValue LV = EmitLValueForField(Addr, FD, false, 0);
937 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
938 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
939 } else {
940 RValue RV = EmitLoadOfLValue(LV, FT);
941 assert(RV.isScalar() &&
942 "Unexpected non-scalar rvalue during struct expansion.");
943 Args.push_back(RV.getScalarVal());
944 }
945 }
946}
947
Daniel Dunbar84379912009-02-02 19:06:38 +0000948/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
949/// a pointer to an object of type \arg Ty.
950///
951/// This safely handles the case when the src type is smaller than the
952/// destination type; in this situation the values of bits which not
953/// present in the src are undefined.
954static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
955 const llvm::Type *Ty,
956 CodeGenFunction &CGF) {
957 const llvm::Type *SrcTy =
958 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
959 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
960 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(Ty);
961
Daniel Dunbar77071992009-02-03 05:59:18 +0000962 // If load is legal, just bitcast the src pointer.
Daniel Dunbar84379912009-02-02 19:06:38 +0000963 if (SrcSize == DstSize) {
964 llvm::Value *Casted =
965 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
966 return CGF.Builder.CreateLoad(Casted);
967 } else {
968 assert(SrcSize < DstSize && "Coercion is losing source bits!");
969
970 // Otherwise do coercion through memory. This is stupid, but
971 // simple.
972 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
973 llvm::Value *Casted =
974 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
975 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
976 return CGF.Builder.CreateLoad(Tmp);
977 }
978}
979
980/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
981/// where the source and destination may have different types.
982///
983/// This safely handles the case when the src type is larger than the
984/// destination type; the upper bits of the src will be lost.
985static void CreateCoercedStore(llvm::Value *Src,
986 llvm::Value *DstPtr,
987 CodeGenFunction &CGF) {
988 const llvm::Type *SrcTy = Src->getType();
989 const llvm::Type *DstTy =
990 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
991
992 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
993 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(DstTy);
994
Daniel Dunbare92e0ab2009-02-03 05:31:23 +0000995 // If store is legal, just bitcast the src pointer.
Daniel Dunbar84379912009-02-02 19:06:38 +0000996 if (SrcSize == DstSize) {
997 llvm::Value *Casted =
998 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
999 CGF.Builder.CreateStore(Src, Casted);
1000 } else {
1001 assert(SrcSize > DstSize && "Coercion is missing bits!");
1002
1003 // Otherwise do coercion through memory. This is stupid, but
1004 // simple.
1005 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
1006 CGF.Builder.CreateStore(Src, Tmp);
1007 llvm::Value *Casted =
1008 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
1009 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(Casted), DstPtr);
1010 }
1011}
1012
Daniel Dunbar04d35782008-09-17 00:51:38 +00001013/***/
1014
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001015bool CodeGenModule::ReturnTypeUsesSret(const CGFunctionInfo &FI) {
Daniel Dunbar77071992009-02-03 05:59:18 +00001016 return FI.getReturnInfo().isStructRet();
Daniel Dunbar9fc15a82009-02-02 21:43:58 +00001017}
1018
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001019const llvm::FunctionType *
Daniel Dunbar9fc15a82009-02-02 21:43:58 +00001020CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic) {
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001021 std::vector<const llvm::Type*> ArgTys;
1022
1023 const llvm::Type *ResultType = 0;
1024
Daniel Dunbar0b37ca82009-02-02 23:43:58 +00001025 QualType RetTy = FI.getReturnType();
Daniel Dunbar77071992009-02-03 05:59:18 +00001026 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar22e30052008-09-11 01:48:57 +00001027 switch (RetAI.getKind()) {
1028 case ABIArgInfo::ByVal:
1029 case ABIArgInfo::Expand:
1030 assert(0 && "Invalid ABI kind for return argument");
1031
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001032 case ABIArgInfo::Direct:
1033 ResultType = ConvertType(RetTy);
1034 break;
1035
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001036 case ABIArgInfo::StructRet: {
1037 ResultType = llvm::Type::VoidTy;
Daniel Dunbara9976a22008-09-10 07:00:50 +00001038 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001039 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
1040 break;
1041 }
1042
Daniel Dunbar1358b202009-01-26 21:26:08 +00001043 case ABIArgInfo::Ignore:
1044 ResultType = llvm::Type::VoidTy;
1045 break;
1046
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001047 case ABIArgInfo::Coerce:
Daniel Dunbar73d66602008-09-10 07:04:09 +00001048 ResultType = RetAI.getCoerceToType();
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001049 break;
1050 }
1051
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001052 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1053 ie = FI.arg_end(); it != ie; ++it) {
1054 const ABIArgInfo &AI = it->info;
1055 const llvm::Type *Ty = ConvertType(it->type);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001056
1057 switch (AI.getKind()) {
Daniel Dunbar1358b202009-01-26 21:26:08 +00001058 case ABIArgInfo::Ignore:
1059 break;
1060
Daniel Dunbar04d35782008-09-17 00:51:38 +00001061 case ABIArgInfo::Coerce:
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001062 ArgTys.push_back(AI.getCoerceToType());
1063 break;
1064
Daniel Dunbar22e30052008-09-11 01:48:57 +00001065 case ABIArgInfo::StructRet:
1066 assert(0 && "Invalid ABI kind for non-return argument");
1067
1068 case ABIArgInfo::ByVal:
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001069 // byval arguments are always on the stack, which is addr space #0.
1070 ArgTys.push_back(llvm::PointerType::getUnqual(Ty));
Daniel Dunbar22e30052008-09-11 01:48:57 +00001071 assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled");
1072 break;
1073
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001074 case ABIArgInfo::Direct:
Daniel Dunbar22e30052008-09-11 01:48:57 +00001075 ArgTys.push_back(Ty);
1076 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001077
1078 case ABIArgInfo::Expand:
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001079 GetExpandedTypes(it->type, ArgTys);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001080 break;
1081 }
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001082 }
1083
Daniel Dunbar9fc15a82009-02-02 21:43:58 +00001084 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar49f5a0d2008-09-09 23:48:28 +00001085}
1086
Daniel Dunbar0b37ca82009-02-02 23:43:58 +00001087void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001088 const Decl *TargetDecl,
Devang Patela85a9ef2008-09-25 21:02:23 +00001089 AttributeListType &PAL) {
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001090 unsigned FuncAttrs = 0;
Devang Patel2bb6eb82008-09-26 22:53:57 +00001091 unsigned RetAttrs = 0;
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001092
1093 if (TargetDecl) {
1094 if (TargetDecl->getAttr<NoThrowAttr>())
Devang Patela85a9ef2008-09-25 21:02:23 +00001095 FuncAttrs |= llvm::Attribute::NoUnwind;
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001096 if (TargetDecl->getAttr<NoReturnAttr>())
Devang Patela85a9ef2008-09-25 21:02:23 +00001097 FuncAttrs |= llvm::Attribute::NoReturn;
Anders Carlssondd6791c2008-10-05 23:32:53 +00001098 if (TargetDecl->getAttr<PureAttr>())
1099 FuncAttrs |= llvm::Attribute::ReadOnly;
1100 if (TargetDecl->getAttr<ConstAttr>())
1101 FuncAttrs |= llvm::Attribute::ReadNone;
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001102 }
1103
Daniel Dunbar0b37ca82009-02-02 23:43:58 +00001104 QualType RetTy = FI.getReturnType();
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001105 unsigned Index = 1;
Daniel Dunbar77071992009-02-03 05:59:18 +00001106 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001107 switch (RetAI.getKind()) {
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001108 case ABIArgInfo::Direct:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001109 if (RetTy->isPromotableIntegerType()) {
1110 if (RetTy->isSignedIntegerType()) {
Devang Patel2bb6eb82008-09-26 22:53:57 +00001111 RetAttrs |= llvm::Attribute::SExt;
Daniel Dunbare126ab12008-09-10 02:41:04 +00001112 } else if (RetTy->isUnsignedIntegerType()) {
Devang Patel2bb6eb82008-09-26 22:53:57 +00001113 RetAttrs |= llvm::Attribute::ZExt;
Daniel Dunbare126ab12008-09-10 02:41:04 +00001114 }
1115 }
1116 break;
1117
1118 case ABIArgInfo::StructRet:
Devang Patela85a9ef2008-09-25 21:02:23 +00001119 PAL.push_back(llvm::AttributeWithIndex::get(Index,
Daniel Dunbarebbb8f32009-01-31 02:19:00 +00001120 llvm::Attribute::StructRet |
1121 llvm::Attribute::NoAlias));
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001122 ++Index;
Daniel Dunbare126ab12008-09-10 02:41:04 +00001123 break;
1124
Daniel Dunbar1358b202009-01-26 21:26:08 +00001125 case ABIArgInfo::Ignore:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001126 case ABIArgInfo::Coerce:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001127 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001128
1129 case ABIArgInfo::ByVal:
1130 case ABIArgInfo::Expand:
1131 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001132 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001133
Devang Patel2bb6eb82008-09-26 22:53:57 +00001134 if (RetAttrs)
1135 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001136 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1137 ie = FI.arg_end(); it != ie; ++it) {
1138 QualType ParamType = it->type;
1139 const ABIArgInfo &AI = it->info;
Devang Patela85a9ef2008-09-25 21:02:23 +00001140 unsigned Attributes = 0;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001141
1142 switch (AI.getKind()) {
1143 case ABIArgInfo::StructRet:
1144 assert(0 && "Invalid ABI kind for non-return argument");
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001145
1146 case ABIArgInfo::Coerce:
1147 break;
1148
Daniel Dunbar22e30052008-09-11 01:48:57 +00001149 case ABIArgInfo::ByVal:
Devang Patela85a9ef2008-09-25 21:02:23 +00001150 Attributes |= llvm::Attribute::ByVal;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001151 assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled");
1152 break;
1153
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001154 case ABIArgInfo::Direct:
Daniel Dunbar22e30052008-09-11 01:48:57 +00001155 if (ParamType->isPromotableIntegerType()) {
1156 if (ParamType->isSignedIntegerType()) {
Devang Patela85a9ef2008-09-25 21:02:23 +00001157 Attributes |= llvm::Attribute::SExt;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001158 } else if (ParamType->isUnsignedIntegerType()) {
Devang Patela85a9ef2008-09-25 21:02:23 +00001159 Attributes |= llvm::Attribute::ZExt;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001160 }
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001161 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001162 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001163
Daniel Dunbar1358b202009-01-26 21:26:08 +00001164 case ABIArgInfo::Ignore:
1165 // Skip increment, no matching LLVM parameter.
1166 continue;
1167
Daniel Dunbar04d35782008-09-17 00:51:38 +00001168 case ABIArgInfo::Expand: {
1169 std::vector<const llvm::Type*> Tys;
1170 // FIXME: This is rather inefficient. Do we ever actually need
1171 // to do anything here? The result should be just reconstructed
1172 // on the other side, so extension should be a non-issue.
1173 getTypes().GetExpandedTypes(ParamType, Tys);
1174 Index += Tys.size();
1175 continue;
1176 }
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001177 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001178
Devang Patela85a9ef2008-09-25 21:02:23 +00001179 if (Attributes)
1180 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar04d35782008-09-17 00:51:38 +00001181 ++Index;
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001182 }
Devang Patel2bb6eb82008-09-26 22:53:57 +00001183 if (FuncAttrs)
1184 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
1185
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001186}
1187
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001188void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
1189 llvm::Function *Fn,
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001190 const FunctionArgList &Args) {
Daniel Dunbar5b7ac652009-02-03 06:02:10 +00001191 // FIXME: We no longer need the types from FunctionArgList; lift up
1192 // and simplify.
1193
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001194 // Emit allocs for param decls. Give the LLVM Argument nodes names.
1195 llvm::Function::arg_iterator AI = Fn->arg_begin();
1196
1197 // Name the struct return argument.
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001198 if (CGM.ReturnTypeUsesSret(FI)) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001199 AI->setName("agg.result");
1200 ++AI;
1201 }
Daniel Dunbar77071992009-02-03 05:59:18 +00001202
Daniel Dunbar14c884a2009-02-04 21:17:21 +00001203 assert(FI.arg_size() == Args.size() &&
1204 "Mismatch between function signature & arguments.");
Daniel Dunbar77071992009-02-03 05:59:18 +00001205 CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001206 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbar77071992009-02-03 05:59:18 +00001207 i != e; ++i, ++info_it) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001208 const VarDecl *Arg = i->first;
Daniel Dunbar77071992009-02-03 05:59:18 +00001209 QualType Ty = info_it->type;
1210 const ABIArgInfo &ArgI = info_it->info;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001211
1212 switch (ArgI.getKind()) {
1213 case ABIArgInfo::ByVal:
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001214 case ABIArgInfo::Direct: {
Daniel Dunbar22e30052008-09-11 01:48:57 +00001215 assert(AI != Fn->arg_end() && "Argument mismatch!");
1216 llvm::Value* V = AI;
Daniel Dunbar04d35782008-09-17 00:51:38 +00001217 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
Daniel Dunbar22e30052008-09-11 01:48:57 +00001218 // This must be a promotion, for something like
1219 // "void a(x) short x; {..."
Daniel Dunbar04d35782008-09-17 00:51:38 +00001220 V = EmitScalarConversion(V, Ty, Arg->getType());
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001221 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001222 EmitParmDecl(*Arg, V);
1223 break;
1224 }
Daniel Dunbar04d35782008-09-17 00:51:38 +00001225
1226 case ABIArgInfo::Expand: {
Daniel Dunbar77071992009-02-03 05:59:18 +00001227 // If this structure was expanded into multiple arguments then
Daniel Dunbar04d35782008-09-17 00:51:38 +00001228 // we need to create a temporary and reconstruct it from the
1229 // arguments.
Chris Lattner6c5ec622008-11-24 04:00:27 +00001230 std::string Name = Arg->getNameAsString();
Daniel Dunbar04d35782008-09-17 00:51:38 +00001231 llvm::Value *Temp = CreateTempAlloca(ConvertType(Ty),
1232 (Name + ".addr").c_str());
1233 // FIXME: What are the right qualifiers here?
1234 llvm::Function::arg_iterator End =
1235 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI);
1236 EmitParmDecl(*Arg, Temp);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001237
Daniel Dunbar04d35782008-09-17 00:51:38 +00001238 // Name the arguments used in expansion and increment AI.
1239 unsigned Index = 0;
1240 for (; AI != End; ++AI, ++Index)
1241 AI->setName(Name + "." + llvm::utostr(Index));
1242 continue;
1243 }
Daniel Dunbar1358b202009-01-26 21:26:08 +00001244
1245 case ABIArgInfo::Ignore:
Daniel Dunbar015bc8e2009-02-03 20:00:13 +00001246 // Skip increment, no matching LLVM parameter.
1247 continue;
Daniel Dunbar1358b202009-01-26 21:26:08 +00001248
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001249 case ABIArgInfo::Coerce: {
1250 assert(AI != Fn->arg_end() && "Argument mismatch!");
1251 // FIXME: This is very wasteful; EmitParmDecl is just going to
1252 // drop the result in a new alloca anyway, so we could just
1253 // store into that directly if we broke the abstraction down
1254 // more.
1255 llvm::Value *V = CreateTempAlloca(ConvertType(Ty), "coerce");
1256 CreateCoercedStore(AI, V, *this);
1257 // Match to what EmitParmDecl is expecting for this type.
Daniel Dunbar99473cd2009-02-04 07:22:24 +00001258 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001259 V = Builder.CreateLoad(V);
Daniel Dunbar99473cd2009-02-04 07:22:24 +00001260 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1261 // This must be a promotion, for something like
1262 // "void a(x) short x; {..."
1263 V = EmitScalarConversion(V, Ty, Arg->getType());
1264 }
1265 }
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001266 EmitParmDecl(*Arg, V);
1267 break;
1268 }
1269
Daniel Dunbar22e30052008-09-11 01:48:57 +00001270 case ABIArgInfo::StructRet:
1271 assert(0 && "Invalid ABI kind for non-return argument");
1272 }
Daniel Dunbar04d35782008-09-17 00:51:38 +00001273
1274 ++AI;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001275 }
1276 assert(AI == Fn->arg_end() && "Argument mismatch!");
1277}
1278
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001279void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI,
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001280 llvm::Value *ReturnValue) {
Daniel Dunbare126ab12008-09-10 02:41:04 +00001281 llvm::Value *RV = 0;
1282
1283 // Functions with no result always return void.
1284 if (ReturnValue) {
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001285 QualType RetTy = FI.getReturnType();
Daniel Dunbar77071992009-02-03 05:59:18 +00001286 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbare126ab12008-09-10 02:41:04 +00001287
1288 switch (RetAI.getKind()) {
1289 case ABIArgInfo::StructRet:
Daniel Dunbar17d35372008-12-18 04:52:14 +00001290 if (RetTy->isAnyComplexType()) {
1291 // FIXME: Volatile
1292 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
1293 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
1294 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1295 EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy);
1296 } else {
1297 Builder.CreateStore(Builder.CreateLoad(ReturnValue),
1298 CurFn->arg_begin());
1299 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001300 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001301
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001302 case ABIArgInfo::Direct:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001303 RV = Builder.CreateLoad(ReturnValue);
1304 break;
1305
Daniel Dunbar1358b202009-01-26 21:26:08 +00001306 case ABIArgInfo::Ignore:
1307 break;
1308
Daniel Dunbar73d66602008-09-10 07:04:09 +00001309 case ABIArgInfo::Coerce: {
Daniel Dunbar708d8a82009-01-27 01:36:03 +00001310 RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001311 break;
Daniel Dunbar73d66602008-09-10 07:04:09 +00001312 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001313
1314 case ABIArgInfo::ByVal:
1315 case ABIArgInfo::Expand:
1316 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001317 }
1318 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001319
1320 if (RV) {
1321 Builder.CreateRet(RV);
1322 } else {
1323 Builder.CreateRetVoid();
1324 }
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001325}
1326
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001327RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
1328 llvm::Value *Callee,
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001329 const CallArgList &CallArgs) {
Daniel Dunbar5b7ac652009-02-03 06:02:10 +00001330 // FIXME: We no longer need the types from CallArgs; lift up and
1331 // simplify.
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001332 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001333
1334 // Handle struct-return functions by passing a pointer to the
1335 // location that we would like to return into.
Daniel Dunbar9fc15a82009-02-02 21:43:58 +00001336 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbar77071992009-02-03 05:59:18 +00001337 const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
Daniel Dunbare126ab12008-09-10 02:41:04 +00001338 switch (RetAI.getKind()) {
1339 case ABIArgInfo::StructRet:
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001340 // Create a temporary alloca to hold the result of the call. :(
Daniel Dunbar04d35782008-09-17 00:51:38 +00001341 Args.push_back(CreateTempAlloca(ConvertType(RetTy)));
Daniel Dunbare126ab12008-09-10 02:41:04 +00001342 break;
1343
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001344 case ABIArgInfo::Direct:
Daniel Dunbar1358b202009-01-26 21:26:08 +00001345 case ABIArgInfo::Ignore:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001346 case ABIArgInfo::Coerce:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001347 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001348
1349 case ABIArgInfo::ByVal:
1350 case ABIArgInfo::Expand:
Daniel Dunbar77071992009-02-03 05:59:18 +00001351 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001352 }
1353
Daniel Dunbar14c884a2009-02-04 21:17:21 +00001354 assert(CallInfo.arg_size() == CallArgs.size() &&
1355 "Mismatch between function signature & arguments.");
Daniel Dunbar77071992009-02-03 05:59:18 +00001356 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001357 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
Daniel Dunbar77071992009-02-03 05:59:18 +00001358 I != E; ++I, ++info_it) {
1359 const ABIArgInfo &ArgInfo = info_it->info;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001360 RValue RV = I->first;
Daniel Dunbar04d35782008-09-17 00:51:38 +00001361
1362 switch (ArgInfo.getKind()) {
Daniel Dunbareec02622009-02-03 06:30:17 +00001363 case ABIArgInfo::ByVal: // Direct is byval
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001364 case ABIArgInfo::Direct:
Daniel Dunbar04d35782008-09-17 00:51:38 +00001365 if (RV.isScalar()) {
1366 Args.push_back(RV.getScalarVal());
1367 } else if (RV.isComplex()) {
1368 // Make a temporary alloca to pass the argument.
1369 Args.push_back(CreateTempAlloca(ConvertType(I->second)));
1370 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
1371 } else {
1372 Args.push_back(RV.getAggregateAddr());
1373 }
1374 break;
1375
Daniel Dunbar1358b202009-01-26 21:26:08 +00001376 case ABIArgInfo::Ignore:
1377 break;
1378
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001379 case ABIArgInfo::Coerce: {
1380 // FIXME: Avoid the conversion through memory if possible.
1381 llvm::Value *SrcPtr;
1382 if (RV.isScalar()) {
Daniel Dunbar4ce351b2009-02-03 23:04:57 +00001383 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001384 Builder.CreateStore(RV.getScalarVal(), SrcPtr);
1385 } else if (RV.isComplex()) {
1386 SrcPtr = CreateTempAlloca(ConvertType(I->second), "coerce");
1387 StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
1388 } else
1389 SrcPtr = RV.getAggregateAddr();
1390 Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
1391 *this));
1392 break;
1393 }
1394
Daniel Dunbar04d35782008-09-17 00:51:38 +00001395 case ABIArgInfo::StructRet:
Daniel Dunbar04d35782008-09-17 00:51:38 +00001396 assert(0 && "Invalid ABI kind for non-return argument");
1397 break;
1398
1399 case ABIArgInfo::Expand:
1400 ExpandTypeToArgs(I->second, RV, Args);
1401 break;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001402 }
1403 }
1404
1405 llvm::CallInst *CI = Builder.CreateCall(Callee,&Args[0],&Args[0]+Args.size());
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001406
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001407 // FIXME: Provide TargetDecl so nounwind, noreturn, etc, etc get set.
Devang Patela85a9ef2008-09-25 21:02:23 +00001408 CodeGen::AttributeListType AttributeList;
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001409 CGM.ConstructAttributeList(CallInfo, 0, AttributeList);
Devang Patela85a9ef2008-09-25 21:02:23 +00001410 CI->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(),
Daniel Dunbarebbb8f32009-01-31 02:19:00 +00001411 AttributeList.size()));
1412
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001413 if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee))
1414 CI->setCallingConv(F->getCallingConv());
1415 if (CI->getType() != llvm::Type::VoidTy)
1416 CI->setName("call");
Daniel Dunbare126ab12008-09-10 02:41:04 +00001417
1418 switch (RetAI.getKind()) {
1419 case ABIArgInfo::StructRet:
1420 if (RetTy->isAnyComplexType())
Daniel Dunbar04d35782008-09-17 00:51:38 +00001421 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Daniel Dunbar17d35372008-12-18 04:52:14 +00001422 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar04d35782008-09-17 00:51:38 +00001423 return RValue::getAggregate(Args[0]);
Daniel Dunbar17d35372008-12-18 04:52:14 +00001424 else
1425 return RValue::get(Builder.CreateLoad(Args[0]));
Daniel Dunbar22e30052008-09-11 01:48:57 +00001426
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001427 case ABIArgInfo::Direct:
1428 assert((!RetTy->isAnyComplexType() &&
Daniel Dunbareec02622009-02-03 06:30:17 +00001429 !CodeGenFunction::hasAggregateLLVMType(RetTy)) &&
1430 "FIXME: Implement return for non-scalar direct types.");
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001431 return RValue::get(CI);
Daniel Dunbare126ab12008-09-10 02:41:04 +00001432
Daniel Dunbar1358b202009-01-26 21:26:08 +00001433 case ABIArgInfo::Ignore:
Daniel Dunbar5c9c7f02009-01-29 08:24:57 +00001434 if (RetTy->isVoidType())
1435 return RValue::get(0);
Daniel Dunbareec02622009-02-03 06:30:17 +00001436
1437 // If we are ignoring an argument that had a result, make sure to
1438 // construct the appropriate return value for our caller.
Daniel Dunbar5c9c7f02009-01-29 08:24:57 +00001439 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1440 llvm::Value *Res =
1441 llvm::UndefValue::get(llvm::PointerType::getUnqual(ConvertType(RetTy)));
1442 return RValue::getAggregate(Res);
1443 }
1444 return RValue::get(llvm::UndefValue::get(ConvertType(RetTy)));
Daniel Dunbar1358b202009-01-26 21:26:08 +00001445
Daniel Dunbar73d66602008-09-10 07:04:09 +00001446 case ABIArgInfo::Coerce: {
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001447 // FIXME: Avoid the conversion through memory if possible.
Daniel Dunbar708d8a82009-01-27 01:36:03 +00001448 llvm::Value *V = CreateTempAlloca(ConvertType(RetTy), "coerce");
1449 CreateCoercedStore(CI, V, *this);
Anders Carlssonfccf7472008-11-25 22:21:48 +00001450 if (RetTy->isAnyComplexType())
1451 return RValue::getComplex(LoadComplexFromAddr(V, false));
Daniel Dunbar1358b202009-01-26 21:26:08 +00001452 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssonfccf7472008-11-25 22:21:48 +00001453 return RValue::getAggregate(V);
Daniel Dunbar1358b202009-01-26 21:26:08 +00001454 else
1455 return RValue::get(Builder.CreateLoad(V));
Daniel Dunbar73d66602008-09-10 07:04:09 +00001456 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001457
1458 case ABIArgInfo::ByVal:
1459 case ABIArgInfo::Expand:
1460 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001461 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001462
1463 assert(0 && "Unhandled ABIArgInfo::Kind");
1464 return RValue::get(0);
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001465}