blob: b0a2f586ea3cca0bc182fcc06ef49889dad90499 [file] [log] [blame]
Daniel Dunbar0dbe2272008-09-08 21:33:45 +00001//===----- CGCall.h - Encapsulate calling convention details ----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// These classes wrap the information about a call or function
11// definition used to handle ABI compliancy.
12//
13//===----------------------------------------------------------------------===//
14
15#include "CGCall.h"
16#include "CodeGenFunction.h"
Daniel Dunbarb7688072008-09-10 00:41:16 +000017#include "CodeGenModule.h"
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +000018#include "clang/Basic/TargetInfo.h"
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000019#include "clang/AST/ASTContext.h"
20#include "clang/AST/Decl.h"
21#include "clang/AST/DeclObjC.h"
Daniel Dunbar99037e52009-01-29 08:13:58 +000022#include "clang/AST/RecordLayout.h"
Daniel Dunbar56273772008-09-17 00:51:38 +000023#include "llvm/ADT/StringExtras.h"
Devang Pateld0646bd2008-09-24 01:01:36 +000024#include "llvm/Attributes.h"
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +000025#include "llvm/Support/CommandLine.h"
Daniel Dunbar6f7279b2009-02-04 23:24:38 +000026#include "llvm/Support/raw_ostream.h"
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +000027#include "llvm/Target/TargetData.h"
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +000028
29#include "ABIInfo.h"
30
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000031using namespace clang;
32using namespace CodeGen;
33
34/***/
35
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000036// FIXME: Use iterator and sidestep silly type array creation.
37
Daniel Dunbar541b63b2009-02-02 23:23:47 +000038const
39CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionTypeNoProto *FTNP) {
40 return getFunctionInfo(FTNP->getResultType(),
41 llvm::SmallVector<QualType, 16>());
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000042}
43
Daniel Dunbar541b63b2009-02-02 23:23:47 +000044const
45CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionTypeProto *FTP) {
46 llvm::SmallVector<QualType, 16> ArgTys;
47 // FIXME: Kill copy.
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000048 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +000049 ArgTys.push_back(FTP->getArgType(i));
50 return getFunctionInfo(FTP->getResultType(), ArgTys);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000051}
52
Daniel Dunbar541b63b2009-02-02 23:23:47 +000053const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionDecl *FD) {
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000054 const FunctionType *FTy = FD->getType()->getAsFunctionType();
Daniel Dunbar541b63b2009-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 Dunbar0dbe2272008-09-08 21:33:45 +000058}
59
Daniel Dunbar541b63b2009-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 Dunbar0dbe2272008-09-08 21:33:45 +000065 for (ObjCMethodDecl::param_const_iterator i = MD->param_begin(),
66 e = MD->param_end(); i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +000067 ArgTys.push_back((*i)->getType());
68 return getFunctionInfo(MD->getResultType(), ArgTys);
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000069}
70
Daniel Dunbar541b63b2009-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 Dunbar725ad312009-01-31 02:19:00 +000075 for (CallArgList::const_iterator i = Args.begin(), e = Args.end();
76 i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +000077 ArgTys.push_back(i->second);
78 return getFunctionInfo(ResTy, ArgTys);
Daniel Dunbar725ad312009-01-31 02:19:00 +000079}
80
Daniel Dunbar541b63b2009-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 Dunbarbb36d332009-02-02 21:43:58 +000085 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
86 i != e; ++i)
Daniel Dunbar541b63b2009-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 Dunbar40a6be62009-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 Dunbar88c2fa92009-02-03 05:31:23 +0000102 // Construct the function info.
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000103 FI = new CGFunctionInfo(ResTy, ArgTys);
Daniel Dunbar35e67d42009-02-05 00:00:23 +0000104 FunctionInfos.InsertNode(FI, InsertPos);
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000105
106 // Compute ABI information.
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000107 getABIInfo().computeInfo(*FI, getContext());
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000108
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000109 return *FI;
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000110}
111
112/***/
113
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000114ABIInfo::~ABIInfo() {}
115
Daniel Dunbar6f7279b2009-02-04 23:24:38 +0000116void ABIArgInfo::dump() const {
117 fprintf(stderr, "(ABIArgInfo Kind=");
118 switch (TheKind) {
119 case Direct:
120 fprintf(stderr, "Direct");
121 break;
122 case StructRet:
123 fprintf(stderr, "StructRet");
124 break;
125 case Ignore:
126 fprintf(stderr, "Ignore");
127 break;
128 case Coerce:
129 fprintf(stderr, "Coerce Type=");
130 getCoerceToType()->print(llvm::errs());
131 // FIXME: This is ridiculous.
132 llvm::errs().flush();
133 break;
134 case ByVal:
135 fprintf(stderr, "ByVal Align=%d", getByValAlignment());
136 break;
137 case Expand:
138 fprintf(stderr, "Expand");
139 break;
140 }
141 fprintf(stderr, ")\n");
142}
143
144/***/
145
Daniel Dunbar834af452008-09-17 21:22:33 +0000146/// isEmptyStruct - Return true iff a structure has no non-empty
147/// members. Note that a structure with a flexible array member is not
148/// considered empty.
149static bool isEmptyStruct(QualType T) {
150 const RecordType *RT = T->getAsStructureType();
151 if (!RT)
152 return 0;
153 const RecordDecl *RD = RT->getDecl();
154 if (RD->hasFlexibleArrayMember())
155 return false;
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000156 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar834af452008-09-17 21:22:33 +0000157 e = RD->field_end(); i != e; ++i) {
158 const FieldDecl *FD = *i;
159 if (!isEmptyStruct(FD->getType()))
160 return false;
161 }
162 return true;
163}
164
165/// isSingleElementStruct - Determine if a structure is a "single
166/// element struct", i.e. it has exactly one non-empty field or
167/// exactly one field which is itself a single element
168/// struct. Structures with flexible array members are never
169/// considered single element structs.
170///
171/// \return The field declaration for the single non-empty field, if
172/// it exists.
173static const FieldDecl *isSingleElementStruct(QualType T) {
174 const RecordType *RT = T->getAsStructureType();
175 if (!RT)
176 return 0;
177
178 const RecordDecl *RD = RT->getDecl();
179 if (RD->hasFlexibleArrayMember())
180 return 0;
181
182 const FieldDecl *Found = 0;
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000183 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar834af452008-09-17 21:22:33 +0000184 e = RD->field_end(); i != e; ++i) {
185 const FieldDecl *FD = *i;
186 QualType FT = FD->getType();
187
188 if (isEmptyStruct(FT)) {
189 // Ignore
190 } else if (Found) {
191 return 0;
192 } else if (!CodeGenFunction::hasAggregateLLVMType(FT)) {
193 Found = FD;
194 } else {
195 Found = isSingleElementStruct(FT);
196 if (!Found)
197 return 0;
198 }
199 }
200
201 return Found;
202}
203
204static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
205 if (!Ty->getAsBuiltinType() && !Ty->isPointerType())
206 return false;
207
208 uint64_t Size = Context.getTypeSize(Ty);
209 return Size == 32 || Size == 64;
210}
211
212static bool areAllFields32Or64BitBasicType(const RecordDecl *RD,
213 ASTContext &Context) {
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000214 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar834af452008-09-17 21:22:33 +0000215 e = RD->field_end(); i != e; ++i) {
216 const FieldDecl *FD = *i;
217
218 if (!is32Or64BitBasicType(FD->getType(), Context))
219 return false;
220
221 // If this is a bit-field we need to make sure it is still a
222 // 32-bit or 64-bit type.
223 if (Expr *BW = FD->getBitWidth()) {
224 unsigned Width = BW->getIntegerConstantExprValue(Context).getZExtValue();
225 if (Width <= 16)
226 return false;
227 }
228 }
229 return true;
230}
231
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000232namespace {
233/// DefaultABIInfo - The default implementation for ABI specific
234/// details. This implementation provides information which results in
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000235/// self-consistent and sensible LLVM IR generation, but does not
236/// conform to any particular ABI.
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000237class DefaultABIInfo : public ABIInfo {
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000238 ABIArgInfo classifyReturnType(QualType RetTy,
239 ASTContext &Context) const;
240
241 ABIArgInfo classifyArgumentType(QualType RetTy,
242 ASTContext &Context) const;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000243
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000244 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
245 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
246 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
247 it != ie; ++it)
248 it->info = classifyArgumentType(it->type, Context);
249 }
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000250};
251
252/// X86_32ABIInfo - The X86-32 ABI information.
253class X86_32ABIInfo : public ABIInfo {
254public:
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000255 ABIArgInfo classifyReturnType(QualType RetTy,
256 ASTContext &Context) const;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000257
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000258 ABIArgInfo classifyArgumentType(QualType RetTy,
259 ASTContext &Context) const;
260
261 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
262 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
263 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
264 it != ie; ++it)
265 it->info = classifyArgumentType(it->type, Context);
266 }
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000267};
268}
269
270ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
271 ASTContext &Context) const {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000272 if (RetTy->isVoidType()) {
273 return ABIArgInfo::getIgnore();
274 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar834af452008-09-17 21:22:33 +0000275 // Classify "single element" structs as their element type.
276 const FieldDecl *SeltFD = isSingleElementStruct(RetTy);
277 if (SeltFD) {
278 QualType SeltTy = SeltFD->getType()->getDesugaredType();
279 if (const BuiltinType *BT = SeltTy->getAsBuiltinType()) {
280 // FIXME: This is gross, it would be nice if we could just
281 // pass back SeltTy and have clients deal with it. Is it worth
282 // supporting coerce to both LLVM and clang Types?
283 if (BT->isIntegerType()) {
284 uint64_t Size = Context.getTypeSize(SeltTy);
285 return ABIArgInfo::getCoerce(llvm::IntegerType::get((unsigned) Size));
286 } else if (BT->getKind() == BuiltinType::Float) {
287 return ABIArgInfo::getCoerce(llvm::Type::FloatTy);
288 } else if (BT->getKind() == BuiltinType::Double) {
289 return ABIArgInfo::getCoerce(llvm::Type::DoubleTy);
290 }
291 } else if (SeltTy->isPointerType()) {
292 // FIXME: It would be really nice if this could come out as
293 // the proper pointer type.
294 llvm::Type *PtrTy =
295 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
296 return ABIArgInfo::getCoerce(PtrTy);
297 }
298 }
299
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000300 uint64_t Size = Context.getTypeSize(RetTy);
301 if (Size == 8) {
302 return ABIArgInfo::getCoerce(llvm::Type::Int8Ty);
303 } else if (Size == 16) {
304 return ABIArgInfo::getCoerce(llvm::Type::Int16Ty);
305 } else if (Size == 32) {
306 return ABIArgInfo::getCoerce(llvm::Type::Int32Ty);
307 } else if (Size == 64) {
308 return ABIArgInfo::getCoerce(llvm::Type::Int64Ty);
309 } else {
310 return ABIArgInfo::getStructRet();
311 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000312 } else {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000313 return ABIArgInfo::getDirect();
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000314 }
315}
316
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000317ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
318 ASTContext &Context) const {
Daniel Dunbarca008822009-02-05 01:31:19 +0000319 // FIXME: Set alignment on byval arguments.
Daniel Dunbarf0357382008-09-17 20:11:04 +0000320 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar834af452008-09-17 21:22:33 +0000321 // Structures with flexible arrays are always byval.
322 if (const RecordType *RT = Ty->getAsStructureType())
323 if (RT->getDecl()->hasFlexibleArrayMember())
324 return ABIArgInfo::getByVal(0);
325
326 // Expand empty structs (i.e. ignore)
327 uint64_t Size = Context.getTypeSize(Ty);
328 if (Ty->isStructureType() && Size == 0)
329 return ABIArgInfo::getExpand();
330
331 // Expand structs with size <= 128-bits which consist only of
332 // basic types (int, long long, float, double, xxx*). This is
333 // non-recursive and does not ignore empty fields.
334 if (const RecordType *RT = Ty->getAsStructureType()) {
335 if (Context.getTypeSize(Ty) <= 4*32 &&
336 areAllFields32Or64BitBasicType(RT->getDecl(), Context))
337 return ABIArgInfo::getExpand();
338 }
339
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000340 return ABIArgInfo::getByVal(0);
341 } else {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000342 return ABIArgInfo::getDirect();
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000343 }
344}
345
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000346namespace {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000347/// X86_64ABIInfo - The X86_64 ABI information.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000348class X86_64ABIInfo : public ABIInfo {
349 enum Class {
350 Integer = 0,
351 SSE,
352 SSEUp,
353 X87,
354 X87Up,
355 ComplexX87,
356 NoClass,
357 Memory
358 };
359
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000360 /// merge - Implement the X86_64 ABI merging algorithm.
361 ///
Daniel Dunbarc4503572009-01-31 00:06:58 +0000362 /// Merge an accumulating classification \arg Accum with a field
363 /// classification \arg Field.
364 ///
365 /// \param Accum - The accumulating classification. This should
366 /// always be either NoClass or the result of a previous merge
367 /// call. In addition, this should never be Memory (the caller
368 /// should just return Memory for the aggregate).
369 Class merge(Class Accum, Class Field) const;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000370
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000371 /// classify - Determine the x86_64 register classes in which the
372 /// given type T should be passed.
373 ///
Daniel Dunbarc4503572009-01-31 00:06:58 +0000374 /// \param Lo - The classification for the parts of the type
375 /// residing in the low word of the containing object.
376 ///
377 /// \param Hi - The classification for the parts of the type
378 /// residing in the high word of the containing object.
379 ///
380 /// \param OffsetBase - The bit offset of this type in the
Daniel Dunbarcdf920e2009-01-30 22:40:15 +0000381 /// containing object. Some parameters are classified different
382 /// depending on whether they straddle an eightbyte boundary.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000383 ///
384 /// If a word is unused its result will be NoClass; if a type should
385 /// be passed in Memory then at least the classification of \arg Lo
386 /// will be Memory.
387 ///
388 /// The \arg Lo class will be NoClass iff the argument is ignored.
389 ///
390 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
391 /// be NoClass.
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000392 void classify(QualType T, ASTContext &Context, uint64_t OffsetBase,
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000393 Class &Lo, Class &Hi) const;
Daniel Dunbarc4503572009-01-31 00:06:58 +0000394
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000395 ABIArgInfo classifyReturnType(QualType RetTy,
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000396 ASTContext &Context) const;
397
398 ABIArgInfo classifyArgumentType(QualType Ty,
399 ASTContext &Context,
400 unsigned &freeIntRegs,
401 unsigned &freeSSERegs) const;
402
403public:
404 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000405};
406}
407
Daniel Dunbarc4503572009-01-31 00:06:58 +0000408X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum,
409 Class Field) const {
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000410 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
411 // classified recursively so that always two fields are
412 // considered. The resulting class is calculated according to
413 // the classes of the fields in the eightbyte:
414 //
415 // (a) If both classes are equal, this is the resulting class.
416 //
417 // (b) If one of the classes is NO_CLASS, the resulting class is
418 // the other class.
419 //
420 // (c) If one of the classes is MEMORY, the result is the MEMORY
421 // class.
422 //
423 // (d) If one of the classes is INTEGER, the result is the
424 // INTEGER.
425 //
426 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
427 // MEMORY is used as class.
428 //
429 // (f) Otherwise class SSE is used.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000430 assert((Accum == NoClass || Accum == Integer ||
431 Accum == SSE || Accum == SSEUp) &&
432 "Invalid accumulated classification during merge.");
433 if (Accum == Field || Field == NoClass)
434 return Accum;
435 else if (Field == Memory)
436 return Memory;
437 else if (Accum == NoClass)
438 return Field;
439 else if (Accum == Integer || Field == Integer)
440 return Integer;
441 else if (Field == X87 || Field == X87Up || Field == ComplexX87)
442 return Memory;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000443 else
Daniel Dunbarc4503572009-01-31 00:06:58 +0000444 return SSE;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000445}
446
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000447void X86_64ABIInfo::classify(QualType Ty,
448 ASTContext &Context,
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000449 uint64_t OffsetBase,
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000450 Class &Lo, Class &Hi) const {
Daniel Dunbar9a82b522009-02-02 18:06:39 +0000451 // FIXME: This code can be simplified by introducing a simple value
452 // class for Class pairs with appropriate constructor methods for
453 // the various situations.
454
Daniel Dunbarc4503572009-01-31 00:06:58 +0000455 Lo = Hi = NoClass;
456
457 Class &Current = OffsetBase < 64 ? Lo : Hi;
458 Current = Memory;
459
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000460 if (const BuiltinType *BT = Ty->getAsBuiltinType()) {
461 BuiltinType::Kind k = BT->getKind();
462
Daniel Dunbar11434922009-01-26 21:26:08 +0000463 if (k == BuiltinType::Void) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000464 Current = NoClass;
Daniel Dunbar11434922009-01-26 21:26:08 +0000465 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000466 Current = Integer;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000467 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000468 Current = SSE;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000469 } else if (k == BuiltinType::LongDouble) {
470 Lo = X87;
471 Hi = X87Up;
472 }
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000473 // FIXME: _Decimal32 and _Decimal64 are SSE.
474 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000475 // FIXME: __int128 is (Integer, Integer).
476 } else if (Ty->isPointerLikeType() || Ty->isBlockPointerType() ||
477 Ty->isObjCQualifiedInterfaceType()) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000478 Current = Integer;
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000479 } else if (const VectorType *VT = Ty->getAsVectorType()) {
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000480 uint64_t Size = Context.getTypeSize(VT);
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000481 if (Size == 64) {
Daniel Dunbard4cd1b02009-01-30 19:38:39 +0000482 // gcc passes <1 x double> in memory.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000483 if (VT->getElementType() == Context.DoubleTy)
Daniel Dunbard4cd1b02009-01-30 19:38:39 +0000484 return;
Daniel Dunbard4cd1b02009-01-30 19:38:39 +0000485
Daniel Dunbarc4503572009-01-31 00:06:58 +0000486 Current = SSE;
Daniel Dunbare33edf12009-01-30 18:40:10 +0000487
488 // If this type crosses an eightbyte boundary, it should be
489 // split.
Daniel Dunbarcdf920e2009-01-30 22:40:15 +0000490 if (OffsetBase && OffsetBase != 64)
Daniel Dunbare33edf12009-01-30 18:40:10 +0000491 Hi = Lo;
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000492 } else if (Size == 128) {
493 Lo = SSE;
494 Hi = SSEUp;
495 }
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000496 } else if (const ComplexType *CT = Ty->getAsComplexType()) {
497 QualType ET = CT->getElementType();
498
Daniel Dunbare33edf12009-01-30 18:40:10 +0000499 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbareac48dc2009-01-29 07:22:20 +0000500 if (ET->isIntegerType()) {
Daniel Dunbareac48dc2009-01-29 07:22:20 +0000501 if (Size <= 64)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000502 Current = Integer;
Daniel Dunbareac48dc2009-01-29 07:22:20 +0000503 else if (Size <= 128)
504 Lo = Hi = Integer;
505 } else if (ET == Context.FloatTy)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000506 Current = SSE;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000507 else if (ET == Context.DoubleTy)
508 Lo = Hi = SSE;
509 else if (ET == Context.LongDoubleTy)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000510 Current = ComplexX87;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000511
512 // If this complex type crosses an eightbyte boundary then it
513 // should be split.
Daniel Dunbarcdf920e2009-01-30 22:40:15 +0000514 uint64_t EB_Real = (OffsetBase) / 64;
515 uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000516 if (Hi == NoClass && EB_Real != EB_Imag)
517 Hi = Lo;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000518 } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
519 // Arrays are treated like structures.
520
521 uint64_t Size = Context.getTypeSize(Ty);
522
523 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
524 // than two eightbytes, ..., it has class MEMORY.
525 if (Size > 128)
526 return;
527
528 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
529 // fields, it has class MEMORY.
530 //
531 // Only need to check alignment of array base.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000532 if (OffsetBase % Context.getTypeAlign(AT->getElementType()))
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000533 return;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000534
535 // Otherwise implement simplified merge. We could be smarter about
536 // this, but it isn't worth it and would be harder to verify.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000537 Current = NoClass;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000538 uint64_t EltSize = Context.getTypeSize(AT->getElementType());
539 uint64_t ArraySize = AT->getSize().getZExtValue();
540 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
541 Class FieldLo, FieldHi;
542 classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbarc4503572009-01-31 00:06:58 +0000543 Lo = merge(Lo, FieldLo);
544 Hi = merge(Hi, FieldHi);
545 if (Lo == Memory || Hi == Memory)
546 break;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000547 }
Daniel Dunbarc4503572009-01-31 00:06:58 +0000548
549 // Do post merger cleanup (see below). Only case we worry about is Memory.
550 if (Hi == Memory)
551 Lo = Memory;
552 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Daniel Dunbar99037e52009-01-29 08:13:58 +0000553 } else if (const RecordType *RT = Ty->getAsRecordType()) {
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000554 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbar99037e52009-01-29 08:13:58 +0000555
556 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
557 // than two eightbytes, ..., it has class MEMORY.
558 if (Size > 128)
559 return;
560
561 const RecordDecl *RD = RT->getDecl();
562
563 // Assume variable sized types are passed in memory.
564 if (RD->hasFlexibleArrayMember())
565 return;
566
567 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
568
569 // Reset Lo class, this will be recomputed.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000570 Current = NoClass;
Daniel Dunbar99037e52009-01-29 08:13:58 +0000571 unsigned idx = 0;
572 for (RecordDecl::field_iterator i = RD->field_begin(),
573 e = RD->field_end(); i != e; ++i, ++idx) {
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000574 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Daniel Dunbar99037e52009-01-29 08:13:58 +0000575
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000576 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
577 // fields, it has class MEMORY.
Daniel Dunbar99037e52009-01-29 08:13:58 +0000578 if (Offset % Context.getTypeAlign(i->getType())) {
579 Lo = Memory;
580 return;
581 }
582
Daniel Dunbar99037e52009-01-29 08:13:58 +0000583 // Classify this field.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000584 //
585 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
586 // exceeds a single eightbyte, each is classified
587 // separately. Each eightbyte gets initialized to class
588 // NO_CLASS.
Daniel Dunbar99037e52009-01-29 08:13:58 +0000589 Class FieldLo, FieldHi;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000590 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbarc4503572009-01-31 00:06:58 +0000591 Lo = merge(Lo, FieldLo);
592 Hi = merge(Hi, FieldHi);
593 if (Lo == Memory || Hi == Memory)
594 break;
Daniel Dunbar99037e52009-01-29 08:13:58 +0000595 }
596
597 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
598 //
599 // (a) If one of the classes is MEMORY, the whole argument is
600 // passed in memory.
601 //
602 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
603
604 // The first of these conditions is guaranteed by how we implement
Daniel Dunbarc4503572009-01-31 00:06:58 +0000605 // the merge (just bail).
606 //
607 // The second condition occurs in the case of unions; for example
608 // union { _Complex double; unsigned; }.
609 if (Hi == Memory)
610 Lo = Memory;
Daniel Dunbar99037e52009-01-29 08:13:58 +0000611 if (Hi == SSEUp && Lo != SSE)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000612 Hi = SSE;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000613 }
614}
615
Daniel Dunbarc4503572009-01-31 00:06:58 +0000616
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000617ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
618 ASTContext &Context) const {
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000619 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
620 // classification algorithm.
621 X86_64ABIInfo::Class Lo, Hi;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000622 classify(RetTy, Context, 0, Lo, Hi);
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000623
Daniel Dunbarc4503572009-01-31 00:06:58 +0000624 // Check some invariants.
625 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
626 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
627 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
628
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000629 const llvm::Type *ResType = 0;
630 switch (Lo) {
631 case NoClass:
Daniel Dunbar11434922009-01-26 21:26:08 +0000632 return ABIArgInfo::getIgnore();
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000633
634 case SSEUp:
635 case X87Up:
636 assert(0 && "Invalid classification for lo word.");
637
Daniel Dunbarc4503572009-01-31 00:06:58 +0000638 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
639 // hidden argument, i.e. structret.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000640 case Memory:
641 return ABIArgInfo::getStructRet();
642
643 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
644 // available register of the sequence %rax, %rdx is used.
645 case Integer:
646 ResType = llvm::Type::Int64Ty; break;
647
648 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
649 // available SSE register of the sequence %xmm0, %xmm1 is used.
650 case SSE:
651 ResType = llvm::Type::DoubleTy; break;
652
653 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
654 // returned on the X87 stack in %st0 as 80-bit x87 number.
655 case X87:
656 ResType = llvm::Type::X86_FP80Ty; break;
657
Daniel Dunbarc4503572009-01-31 00:06:58 +0000658 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
659 // part of the value is returned in %st0 and the imaginary part in
660 // %st1.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000661 case ComplexX87:
662 assert(Hi == NoClass && "Unexpected ComplexX87 classification.");
663 ResType = llvm::VectorType::get(llvm::Type::X86_FP80Ty, 2);
664 break;
665 }
666
667 switch (Hi) {
668 // Memory was handled previously, and ComplexX87 and X87 should
669 // never occur as hi classes.
670 case Memory:
671 case X87:
672 case ComplexX87:
673 assert(0 && "Invalid classification for hi word.");
674
675 case NoClass: break;
676 case Integer:
Daniel Dunbarb0e14f22009-01-29 07:36:07 +0000677 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
678 break;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000679 case SSE:
Daniel Dunbarb0e14f22009-01-29 07:36:07 +0000680 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
681 break;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000682
683 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
684 // is passed in the upper half of the last used SSE register.
685 //
686 // SSEUP should always be preceeded by SSE, just widen.
687 case SSEUp:
688 assert(Lo == SSE && "Unexpected SSEUp classification.");
689 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
690 break;
691
692 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
Daniel Dunbarb0e14f22009-01-29 07:36:07 +0000693 // returned together with the previous X87 value in %st0.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000694 //
695 // X87UP should always be preceeded by X87, so we don't need to do
696 // anything here.
697 case X87Up:
698 assert(Lo == X87 && "Unexpected X87Up classification.");
699 break;
700 }
701
702 return ABIArgInfo::getCoerce(ResType);
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000703}
704
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000705ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, ASTContext &Context,
706 unsigned &freeIntRegs,
707 unsigned &freeSSERegs) const {
708 X86_64ABIInfo::Class Lo, Hi;
709 classify(Ty, Context, 0, Lo, Hi);
710
711 // Check some invariants.
712 // FIXME: Enforce these by construction.
713 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
714 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
715 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
716
717 unsigned neededInt = 0, neededSSE = 0;
718 const llvm::Type *ResType = 0;
719 switch (Lo) {
720 case NoClass:
721 return ABIArgInfo::getIgnore();
722
723 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
724 // on the stack.
725 case Memory:
726
727 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
728 // COMPLEX_X87, it is passed in memory.
729 case X87:
730 case ComplexX87:
731 // Choose appropriate in memory type.
732 if (CodeGenFunction::hasAggregateLLVMType(Ty))
733 return ABIArgInfo::getByVal(0);
734 else
735 return ABIArgInfo::getDirect();
736
737 case SSEUp:
738 case X87Up:
739 assert(0 && "Invalid classification for lo word.");
740
741 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
742 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
743 // and %r9 is used.
744 case Integer:
745 ++neededInt;
746 ResType = llvm::Type::Int64Ty;
747 break;
748
749 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
750 // available SSE register is used, the registers are taken in the
751 // order from %xmm0 to %xmm7.
752 case SSE:
753 ++neededSSE;
754 ResType = llvm::Type::DoubleTy;
755 break;
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000756 }
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000757
758 switch (Hi) {
759 // Memory was handled previously, ComplexX87 and X87 should
760 // never occur as hi classes, and X87Up must be preceed by X87,
761 // which is passed in memory.
762 case Memory:
763 case X87:
764 case X87Up:
765 case ComplexX87:
766 assert(0 && "Invalid classification for hi word.");
767
768 case NoClass: break;
769 case Integer:
770 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
771 ++neededInt;
772 break;
773 case SSE:
774 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
775 ++neededSSE;
776 break;
777
778 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
779 // eightbyte is passed in the upper half of the last used SSE
780 // register.
781 case SSEUp:
782 assert(Lo == SSE && "Unexpected SSEUp classification.");
783 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
784 break;
785 }
786
787 // AMD64-ABI 3.2.3p3: If there are no registers available for any
788 // eightbyte of an argument, the whole argument is passed on the
789 // stack. If registers have already been assigned for some
790 // eightbytes of such an argument, the assignments get reverted.
791 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
792 freeIntRegs -= neededInt;
793 freeSSERegs -= neededSSE;
794 return ABIArgInfo::getCoerce(ResType);
795 } else {
796 // Choose appropriate in memory type.
797 if (CodeGenFunction::hasAggregateLLVMType(Ty))
798 return ABIArgInfo::getByVal(0);
799 else
800 return ABIArgInfo::getDirect();
801 }
802}
803
804void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
805 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
806
807 // Keep track of the number of assigned registers.
808 unsigned freeIntRegs = 6, freeSSERegs = 8;
809
810 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
811 // get assigned (in left-to-right order) for passing as follows...
812 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
813 it != ie; ++it)
814 it->info = classifyArgumentType(it->type, Context, freeIntRegs, freeSSERegs);
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000815}
816
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000817ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
818 ASTContext &Context) const {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000819 if (RetTy->isVoidType()) {
820 return ABIArgInfo::getIgnore();
821 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
822 return ABIArgInfo::getStructRet();
823 } else {
824 return ABIArgInfo::getDirect();
825 }
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000826}
827
828ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
829 ASTContext &Context) const {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000830 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
831 return ABIArgInfo::getByVal(0);
832 } else {
833 return ABIArgInfo::getDirect();
834 }
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000835}
836
837const ABIInfo &CodeGenTypes::getABIInfo() const {
838 if (TheABIInfo)
839 return *TheABIInfo;
840
841 // For now we just cache this in the CodeGenTypes and don't bother
842 // to free it.
843 const char *TargetPrefix = getContext().Target.getTargetPrefix();
844 if (strcmp(TargetPrefix, "x86") == 0) {
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000845 switch (getContext().Target.getPointerWidth(0)) {
846 case 32:
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000847 return *(TheABIInfo = new X86_32ABIInfo());
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000848 case 64:
Daniel Dunbar11a76ed2009-01-30 18:47:53 +0000849 return *(TheABIInfo = new X86_64ABIInfo());
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000850 }
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000851 }
852
853 return *(TheABIInfo = new DefaultABIInfo);
854}
855
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000856/***/
857
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000858CGFunctionInfo::CGFunctionInfo(QualType ResTy,
859 const llvm::SmallVector<QualType, 16> &ArgTys) {
860 NumArgs = ArgTys.size();
861 Args = new ArgInfo[1 + NumArgs];
862 Args[0].type = ResTy;
863 for (unsigned i = 0; i < NumArgs; ++i)
864 Args[1 + i].type = ArgTys[i];
865}
866
867/***/
868
Daniel Dunbar56273772008-09-17 00:51:38 +0000869void CodeGenTypes::GetExpandedTypes(QualType Ty,
870 std::vector<const llvm::Type*> &ArgTys) {
871 const RecordType *RT = Ty->getAsStructureType();
872 assert(RT && "Can only expand structure types.");
873 const RecordDecl *RD = RT->getDecl();
874 assert(!RD->hasFlexibleArrayMember() &&
875 "Cannot expand structure with flexible array.");
876
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000877 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar56273772008-09-17 00:51:38 +0000878 e = RD->field_end(); i != e; ++i) {
879 const FieldDecl *FD = *i;
880 assert(!FD->isBitField() &&
881 "Cannot expand structure with bit-field members.");
882
883 QualType FT = FD->getType();
884 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
885 GetExpandedTypes(FT, ArgTys);
886 } else {
887 ArgTys.push_back(ConvertType(FT));
888 }
889 }
890}
891
892llvm::Function::arg_iterator
893CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
894 llvm::Function::arg_iterator AI) {
895 const RecordType *RT = Ty->getAsStructureType();
896 assert(RT && "Can only expand structure types.");
897
898 RecordDecl *RD = RT->getDecl();
899 assert(LV.isSimple() &&
900 "Unexpected non-simple lvalue during struct expansion.");
901 llvm::Value *Addr = LV.getAddress();
902 for (RecordDecl::field_iterator i = RD->field_begin(),
903 e = RD->field_end(); i != e; ++i) {
904 FieldDecl *FD = *i;
905 QualType FT = FD->getType();
906
907 // FIXME: What are the right qualifiers here?
908 LValue LV = EmitLValueForField(Addr, FD, false, 0);
909 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
910 AI = ExpandTypeFromArgs(FT, LV, AI);
911 } else {
912 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
913 ++AI;
914 }
915 }
916
917 return AI;
918}
919
920void
921CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
922 llvm::SmallVector<llvm::Value*, 16> &Args) {
923 const RecordType *RT = Ty->getAsStructureType();
924 assert(RT && "Can only expand structure types.");
925
926 RecordDecl *RD = RT->getDecl();
927 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
928 llvm::Value *Addr = RV.getAggregateAddr();
929 for (RecordDecl::field_iterator i = RD->field_begin(),
930 e = RD->field_end(); i != e; ++i) {
931 FieldDecl *FD = *i;
932 QualType FT = FD->getType();
933
934 // FIXME: What are the right qualifiers here?
935 LValue LV = EmitLValueForField(Addr, FD, false, 0);
936 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
937 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
938 } else {
939 RValue RV = EmitLoadOfLValue(LV, FT);
940 assert(RV.isScalar() &&
941 "Unexpected non-scalar rvalue during struct expansion.");
942 Args.push_back(RV.getScalarVal());
943 }
944 }
945}
946
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000947/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
948/// a pointer to an object of type \arg Ty.
949///
950/// This safely handles the case when the src type is smaller than the
951/// destination type; in this situation the values of bits which not
952/// present in the src are undefined.
953static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
954 const llvm::Type *Ty,
955 CodeGenFunction &CGF) {
956 const llvm::Type *SrcTy =
957 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
958 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
959 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(Ty);
960
Daniel Dunbarb225be42009-02-03 05:59:18 +0000961 // If load is legal, just bitcast the src pointer.
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000962 if (SrcSize == DstSize) {
963 llvm::Value *Casted =
964 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
965 return CGF.Builder.CreateLoad(Casted);
966 } else {
967 assert(SrcSize < DstSize && "Coercion is losing source bits!");
968
969 // Otherwise do coercion through memory. This is stupid, but
970 // simple.
971 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
972 llvm::Value *Casted =
973 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
974 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
975 return CGF.Builder.CreateLoad(Tmp);
976 }
977}
978
979/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
980/// where the source and destination may have different types.
981///
982/// This safely handles the case when the src type is larger than the
983/// destination type; the upper bits of the src will be lost.
984static void CreateCoercedStore(llvm::Value *Src,
985 llvm::Value *DstPtr,
986 CodeGenFunction &CGF) {
987 const llvm::Type *SrcTy = Src->getType();
988 const llvm::Type *DstTy =
989 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
990
991 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
992 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(DstTy);
993
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000994 // If store is legal, just bitcast the src pointer.
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000995 if (SrcSize == DstSize) {
996 llvm::Value *Casted =
997 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
998 CGF.Builder.CreateStore(Src, Casted);
999 } else {
1000 assert(SrcSize > DstSize && "Coercion is missing bits!");
1001
1002 // Otherwise do coercion through memory. This is stupid, but
1003 // simple.
1004 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
1005 CGF.Builder.CreateStore(Src, Tmp);
1006 llvm::Value *Casted =
1007 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
1008 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(Casted), DstPtr);
1009 }
1010}
1011
Daniel Dunbar56273772008-09-17 00:51:38 +00001012/***/
1013
Daniel Dunbar88b53962009-02-02 22:03:45 +00001014bool CodeGenModule::ReturnTypeUsesSret(const CGFunctionInfo &FI) {
Daniel Dunbarb225be42009-02-03 05:59:18 +00001015 return FI.getReturnInfo().isStructRet();
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001016}
1017
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001018const llvm::FunctionType *
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001019CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic) {
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001020 std::vector<const llvm::Type*> ArgTys;
1021
1022 const llvm::Type *ResultType = 0;
1023
Daniel Dunbara0a99e02009-02-02 23:43:58 +00001024 QualType RetTy = FI.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001025 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001026 switch (RetAI.getKind()) {
1027 case ABIArgInfo::ByVal:
1028 case ABIArgInfo::Expand:
1029 assert(0 && "Invalid ABI kind for return argument");
1030
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001031 case ABIArgInfo::Direct:
1032 ResultType = ConvertType(RetTy);
1033 break;
1034
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001035 case ABIArgInfo::StructRet: {
1036 ResultType = llvm::Type::VoidTy;
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +00001037 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001038 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
1039 break;
1040 }
1041
Daniel Dunbar11434922009-01-26 21:26:08 +00001042 case ABIArgInfo::Ignore:
1043 ResultType = llvm::Type::VoidTy;
1044 break;
1045
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001046 case ABIArgInfo::Coerce:
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001047 ResultType = RetAI.getCoerceToType();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001048 break;
1049 }
1050
Daniel Dunbar88c2fa92009-02-03 05:31:23 +00001051 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1052 ie = FI.arg_end(); it != ie; ++it) {
1053 const ABIArgInfo &AI = it->info;
1054 const llvm::Type *Ty = ConvertType(it->type);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001055
1056 switch (AI.getKind()) {
Daniel Dunbar11434922009-01-26 21:26:08 +00001057 case ABIArgInfo::Ignore:
1058 break;
1059
Daniel Dunbar56273772008-09-17 00:51:38 +00001060 case ABIArgInfo::Coerce:
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001061 ArgTys.push_back(AI.getCoerceToType());
1062 break;
1063
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001064 case ABIArgInfo::StructRet:
1065 assert(0 && "Invalid ABI kind for non-return argument");
1066
1067 case ABIArgInfo::ByVal:
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001068 // byval arguments are always on the stack, which is addr space #0.
1069 ArgTys.push_back(llvm::PointerType::getUnqual(Ty));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001070 break;
1071
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001072 case ABIArgInfo::Direct:
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001073 ArgTys.push_back(Ty);
1074 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001075
1076 case ABIArgInfo::Expand:
Daniel Dunbar88c2fa92009-02-03 05:31:23 +00001077 GetExpandedTypes(it->type, ArgTys);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001078 break;
1079 }
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001080 }
1081
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001082 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar3913f182008-09-09 23:48:28 +00001083}
1084
Daniel Dunbara0a99e02009-02-02 23:43:58 +00001085void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
Daniel Dunbar88b53962009-02-02 22:03:45 +00001086 const Decl *TargetDecl,
Devang Patel761d7f72008-09-25 21:02:23 +00001087 AttributeListType &PAL) {
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001088 unsigned FuncAttrs = 0;
Devang Patela2c69122008-09-26 22:53:57 +00001089 unsigned RetAttrs = 0;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001090
1091 if (TargetDecl) {
1092 if (TargetDecl->getAttr<NoThrowAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +00001093 FuncAttrs |= llvm::Attribute::NoUnwind;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001094 if (TargetDecl->getAttr<NoReturnAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +00001095 FuncAttrs |= llvm::Attribute::NoReturn;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001096 if (TargetDecl->getAttr<PureAttr>())
1097 FuncAttrs |= llvm::Attribute::ReadOnly;
1098 if (TargetDecl->getAttr<ConstAttr>())
1099 FuncAttrs |= llvm::Attribute::ReadNone;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001100 }
1101
Daniel Dunbara0a99e02009-02-02 23:43:58 +00001102 QualType RetTy = FI.getReturnType();
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001103 unsigned Index = 1;
Daniel Dunbarb225be42009-02-03 05:59:18 +00001104 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001105 switch (RetAI.getKind()) {
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001106 case ABIArgInfo::Direct:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001107 if (RetTy->isPromotableIntegerType()) {
1108 if (RetTy->isSignedIntegerType()) {
Devang Patela2c69122008-09-26 22:53:57 +00001109 RetAttrs |= llvm::Attribute::SExt;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001110 } else if (RetTy->isUnsignedIntegerType()) {
Devang Patela2c69122008-09-26 22:53:57 +00001111 RetAttrs |= llvm::Attribute::ZExt;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001112 }
1113 }
1114 break;
1115
1116 case ABIArgInfo::StructRet:
Devang Patel761d7f72008-09-25 21:02:23 +00001117 PAL.push_back(llvm::AttributeWithIndex::get(Index,
Daniel Dunbar725ad312009-01-31 02:19:00 +00001118 llvm::Attribute::StructRet |
1119 llvm::Attribute::NoAlias));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001120 ++Index;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001121 break;
1122
Daniel Dunbar11434922009-01-26 21:26:08 +00001123 case ABIArgInfo::Ignore:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001124 case ABIArgInfo::Coerce:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001125 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001126
1127 case ABIArgInfo::ByVal:
1128 case ABIArgInfo::Expand:
1129 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001130 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001131
Devang Patela2c69122008-09-26 22:53:57 +00001132 if (RetAttrs)
1133 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Daniel Dunbar88c2fa92009-02-03 05:31:23 +00001134 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1135 ie = FI.arg_end(); it != ie; ++it) {
1136 QualType ParamType = it->type;
1137 const ABIArgInfo &AI = it->info;
Devang Patel761d7f72008-09-25 21:02:23 +00001138 unsigned Attributes = 0;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001139
1140 switch (AI.getKind()) {
1141 case ABIArgInfo::StructRet:
1142 assert(0 && "Invalid ABI kind for non-return argument");
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001143
1144 case ABIArgInfo::Coerce:
1145 break;
1146
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001147 case ABIArgInfo::ByVal:
Devang Patel761d7f72008-09-25 21:02:23 +00001148 Attributes |= llvm::Attribute::ByVal;
Daniel Dunbarca008822009-02-05 01:31:19 +00001149 Attributes |=
1150 llvm::Attribute::constructAlignmentFromInt(AI.getByValAlignment());
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001151 break;
1152
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001153 case ABIArgInfo::Direct:
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001154 if (ParamType->isPromotableIntegerType()) {
1155 if (ParamType->isSignedIntegerType()) {
Devang Patel761d7f72008-09-25 21:02:23 +00001156 Attributes |= llvm::Attribute::SExt;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001157 } else if (ParamType->isUnsignedIntegerType()) {
Devang Patel761d7f72008-09-25 21:02:23 +00001158 Attributes |= llvm::Attribute::ZExt;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001159 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001160 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001161 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001162
Daniel Dunbar11434922009-01-26 21:26:08 +00001163 case ABIArgInfo::Ignore:
1164 // Skip increment, no matching LLVM parameter.
1165 continue;
1166
Daniel Dunbar56273772008-09-17 00:51:38 +00001167 case ABIArgInfo::Expand: {
1168 std::vector<const llvm::Type*> Tys;
1169 // FIXME: This is rather inefficient. Do we ever actually need
1170 // to do anything here? The result should be just reconstructed
1171 // on the other side, so extension should be a non-issue.
1172 getTypes().GetExpandedTypes(ParamType, Tys);
1173 Index += Tys.size();
1174 continue;
1175 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001176 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001177
Devang Patel761d7f72008-09-25 21:02:23 +00001178 if (Attributes)
1179 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar56273772008-09-17 00:51:38 +00001180 ++Index;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001181 }
Devang Patela2c69122008-09-26 22:53:57 +00001182 if (FuncAttrs)
1183 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
1184
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001185}
1186
Daniel Dunbar88b53962009-02-02 22:03:45 +00001187void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
1188 llvm::Function *Fn,
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001189 const FunctionArgList &Args) {
Daniel Dunbar5251afa2009-02-03 06:02:10 +00001190 // FIXME: We no longer need the types from FunctionArgList; lift up
1191 // and simplify.
1192
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001193 // Emit allocs for param decls. Give the LLVM Argument nodes names.
1194 llvm::Function::arg_iterator AI = Fn->arg_begin();
1195
1196 // Name the struct return argument.
Daniel Dunbar88b53962009-02-02 22:03:45 +00001197 if (CGM.ReturnTypeUsesSret(FI)) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001198 AI->setName("agg.result");
1199 ++AI;
1200 }
Daniel Dunbarb225be42009-02-03 05:59:18 +00001201
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +00001202 assert(FI.arg_size() == Args.size() &&
1203 "Mismatch between function signature & arguments.");
Daniel Dunbarb225be42009-02-03 05:59:18 +00001204 CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001205 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001206 i != e; ++i, ++info_it) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001207 const VarDecl *Arg = i->first;
Daniel Dunbarb225be42009-02-03 05:59:18 +00001208 QualType Ty = info_it->type;
1209 const ABIArgInfo &ArgI = info_it->info;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001210
1211 switch (ArgI.getKind()) {
1212 case ABIArgInfo::ByVal:
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001213 case ABIArgInfo::Direct: {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001214 assert(AI != Fn->arg_end() && "Argument mismatch!");
1215 llvm::Value* V = AI;
Daniel Dunbar56273772008-09-17 00:51:38 +00001216 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001217 // This must be a promotion, for something like
1218 // "void a(x) short x; {..."
Daniel Dunbar56273772008-09-17 00:51:38 +00001219 V = EmitScalarConversion(V, Ty, Arg->getType());
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001220 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001221 EmitParmDecl(*Arg, V);
1222 break;
1223 }
Daniel Dunbar56273772008-09-17 00:51:38 +00001224
1225 case ABIArgInfo::Expand: {
Daniel Dunbarb225be42009-02-03 05:59:18 +00001226 // If this structure was expanded into multiple arguments then
Daniel Dunbar56273772008-09-17 00:51:38 +00001227 // we need to create a temporary and reconstruct it from the
1228 // arguments.
Chris Lattner39f34e92008-11-24 04:00:27 +00001229 std::string Name = Arg->getNameAsString();
Daniel Dunbar56273772008-09-17 00:51:38 +00001230 llvm::Value *Temp = CreateTempAlloca(ConvertType(Ty),
1231 (Name + ".addr").c_str());
1232 // FIXME: What are the right qualifiers here?
1233 llvm::Function::arg_iterator End =
1234 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI);
1235 EmitParmDecl(*Arg, Temp);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001236
Daniel Dunbar56273772008-09-17 00:51:38 +00001237 // Name the arguments used in expansion and increment AI.
1238 unsigned Index = 0;
1239 for (; AI != End; ++AI, ++Index)
1240 AI->setName(Name + "." + llvm::utostr(Index));
1241 continue;
1242 }
Daniel Dunbar11434922009-01-26 21:26:08 +00001243
1244 case ABIArgInfo::Ignore:
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +00001245 // Skip increment, no matching LLVM parameter.
1246 continue;
Daniel Dunbar11434922009-01-26 21:26:08 +00001247
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001248 case ABIArgInfo::Coerce: {
1249 assert(AI != Fn->arg_end() && "Argument mismatch!");
1250 // FIXME: This is very wasteful; EmitParmDecl is just going to
1251 // drop the result in a new alloca anyway, so we could just
1252 // store into that directly if we broke the abstraction down
1253 // more.
1254 llvm::Value *V = CreateTempAlloca(ConvertType(Ty), "coerce");
1255 CreateCoercedStore(AI, V, *this);
1256 // Match to what EmitParmDecl is expecting for this type.
Daniel Dunbar8b29a382009-02-04 07:22:24 +00001257 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001258 V = Builder.CreateLoad(V);
Daniel Dunbar8b29a382009-02-04 07:22:24 +00001259 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1260 // This must be a promotion, for something like
1261 // "void a(x) short x; {..."
1262 V = EmitScalarConversion(V, Ty, Arg->getType());
1263 }
1264 }
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001265 EmitParmDecl(*Arg, V);
1266 break;
1267 }
1268
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001269 case ABIArgInfo::StructRet:
1270 assert(0 && "Invalid ABI kind for non-return argument");
1271 }
Daniel Dunbar56273772008-09-17 00:51:38 +00001272
1273 ++AI;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001274 }
1275 assert(AI == Fn->arg_end() && "Argument mismatch!");
1276}
1277
Daniel Dunbar88b53962009-02-02 22:03:45 +00001278void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI,
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001279 llvm::Value *ReturnValue) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001280 llvm::Value *RV = 0;
1281
1282 // Functions with no result always return void.
1283 if (ReturnValue) {
Daniel Dunbar88b53962009-02-02 22:03:45 +00001284 QualType RetTy = FI.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001285 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001286
1287 switch (RetAI.getKind()) {
1288 case ABIArgInfo::StructRet:
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001289 if (RetTy->isAnyComplexType()) {
1290 // FIXME: Volatile
1291 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
1292 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
1293 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1294 EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy);
1295 } else {
1296 Builder.CreateStore(Builder.CreateLoad(ReturnValue),
1297 CurFn->arg_begin());
1298 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001299 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001300
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001301 case ABIArgInfo::Direct:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001302 RV = Builder.CreateLoad(ReturnValue);
1303 break;
1304
Daniel Dunbar11434922009-01-26 21:26:08 +00001305 case ABIArgInfo::Ignore:
1306 break;
1307
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001308 case ABIArgInfo::Coerce: {
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +00001309 RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001310 break;
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001311 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001312
1313 case ABIArgInfo::ByVal:
1314 case ABIArgInfo::Expand:
1315 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001316 }
1317 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001318
1319 if (RV) {
1320 Builder.CreateRet(RV);
1321 } else {
1322 Builder.CreateRetVoid();
1323 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001324}
1325
Daniel Dunbar88b53962009-02-02 22:03:45 +00001326RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
1327 llvm::Value *Callee,
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001328 const CallArgList &CallArgs) {
Daniel Dunbar5251afa2009-02-03 06:02:10 +00001329 // FIXME: We no longer need the types from CallArgs; lift up and
1330 // simplify.
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001331 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001332
1333 // Handle struct-return functions by passing a pointer to the
1334 // location that we would like to return into.
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001335 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001336 const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001337 switch (RetAI.getKind()) {
1338 case ABIArgInfo::StructRet:
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001339 // Create a temporary alloca to hold the result of the call. :(
Daniel Dunbar56273772008-09-17 00:51:38 +00001340 Args.push_back(CreateTempAlloca(ConvertType(RetTy)));
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001341 break;
1342
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001343 case ABIArgInfo::Direct:
Daniel Dunbar11434922009-01-26 21:26:08 +00001344 case ABIArgInfo::Ignore:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001345 case ABIArgInfo::Coerce:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001346 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001347
1348 case ABIArgInfo::ByVal:
1349 case ABIArgInfo::Expand:
Daniel Dunbarb225be42009-02-03 05:59:18 +00001350 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001351 }
1352
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +00001353 assert(CallInfo.arg_size() == CallArgs.size() &&
1354 "Mismatch between function signature & arguments.");
Daniel Dunbarb225be42009-02-03 05:59:18 +00001355 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001356 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001357 I != E; ++I, ++info_it) {
1358 const ABIArgInfo &ArgInfo = info_it->info;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001359 RValue RV = I->first;
Daniel Dunbar56273772008-09-17 00:51:38 +00001360
1361 switch (ArgInfo.getKind()) {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001362 case ABIArgInfo::ByVal: // Direct is byval
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001363 case ABIArgInfo::Direct:
Daniel Dunbar56273772008-09-17 00:51:38 +00001364 if (RV.isScalar()) {
1365 Args.push_back(RV.getScalarVal());
1366 } else if (RV.isComplex()) {
1367 // Make a temporary alloca to pass the argument.
1368 Args.push_back(CreateTempAlloca(ConvertType(I->second)));
1369 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
1370 } else {
1371 Args.push_back(RV.getAggregateAddr());
1372 }
1373 break;
1374
Daniel Dunbar11434922009-01-26 21:26:08 +00001375 case ABIArgInfo::Ignore:
1376 break;
1377
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001378 case ABIArgInfo::Coerce: {
1379 // FIXME: Avoid the conversion through memory if possible.
1380 llvm::Value *SrcPtr;
1381 if (RV.isScalar()) {
Daniel Dunbar5a1be6e2009-02-03 23:04:57 +00001382 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001383 Builder.CreateStore(RV.getScalarVal(), SrcPtr);
1384 } else if (RV.isComplex()) {
1385 SrcPtr = CreateTempAlloca(ConvertType(I->second), "coerce");
1386 StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
1387 } else
1388 SrcPtr = RV.getAggregateAddr();
1389 Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
1390 *this));
1391 break;
1392 }
1393
Daniel Dunbar56273772008-09-17 00:51:38 +00001394 case ABIArgInfo::StructRet:
Daniel Dunbar56273772008-09-17 00:51:38 +00001395 assert(0 && "Invalid ABI kind for non-return argument");
1396 break;
1397
1398 case ABIArgInfo::Expand:
1399 ExpandTypeToArgs(I->second, RV, Args);
1400 break;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001401 }
1402 }
1403
1404 llvm::CallInst *CI = Builder.CreateCall(Callee,&Args[0],&Args[0]+Args.size());
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001405
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001406 // FIXME: Provide TargetDecl so nounwind, noreturn, etc, etc get set.
Devang Patel761d7f72008-09-25 21:02:23 +00001407 CodeGen::AttributeListType AttributeList;
Daniel Dunbar88b53962009-02-02 22:03:45 +00001408 CGM.ConstructAttributeList(CallInfo, 0, AttributeList);
Devang Patel761d7f72008-09-25 21:02:23 +00001409 CI->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(),
Daniel Dunbar725ad312009-01-31 02:19:00 +00001410 AttributeList.size()));
1411
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001412 if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee))
1413 CI->setCallingConv(F->getCallingConv());
1414 if (CI->getType() != llvm::Type::VoidTy)
1415 CI->setName("call");
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001416
1417 switch (RetAI.getKind()) {
1418 case ABIArgInfo::StructRet:
1419 if (RetTy->isAnyComplexType())
Daniel Dunbar56273772008-09-17 00:51:38 +00001420 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001421 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar56273772008-09-17 00:51:38 +00001422 return RValue::getAggregate(Args[0]);
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001423 else
1424 return RValue::get(Builder.CreateLoad(Args[0]));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001425
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001426 case ABIArgInfo::Direct:
1427 assert((!RetTy->isAnyComplexType() &&
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001428 !CodeGenFunction::hasAggregateLLVMType(RetTy)) &&
1429 "FIXME: Implement return for non-scalar direct types.");
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001430 return RValue::get(CI);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001431
Daniel Dunbar11434922009-01-26 21:26:08 +00001432 case ABIArgInfo::Ignore:
Daniel Dunbarcc039fe2009-01-29 08:24:57 +00001433 if (RetTy->isVoidType())
1434 return RValue::get(0);
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001435
1436 // If we are ignoring an argument that had a result, make sure to
1437 // construct the appropriate return value for our caller.
Daniel Dunbarcc039fe2009-01-29 08:24:57 +00001438 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1439 llvm::Value *Res =
1440 llvm::UndefValue::get(llvm::PointerType::getUnqual(ConvertType(RetTy)));
1441 return RValue::getAggregate(Res);
1442 }
1443 return RValue::get(llvm::UndefValue::get(ConvertType(RetTy)));
Daniel Dunbar11434922009-01-26 21:26:08 +00001444
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001445 case ABIArgInfo::Coerce: {
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001446 // FIXME: Avoid the conversion through memory if possible.
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +00001447 llvm::Value *V = CreateTempAlloca(ConvertType(RetTy), "coerce");
1448 CreateCoercedStore(CI, V, *this);
Anders Carlssonad3d6912008-11-25 22:21:48 +00001449 if (RetTy->isAnyComplexType())
1450 return RValue::getComplex(LoadComplexFromAddr(V, false));
Daniel Dunbar11434922009-01-26 21:26:08 +00001451 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssonad3d6912008-11-25 22:21:48 +00001452 return RValue::getAggregate(V);
Daniel Dunbar11434922009-01-26 21:26:08 +00001453 else
1454 return RValue::get(Builder.CreateLoad(V));
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001455 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001456
1457 case ABIArgInfo::ByVal:
1458 case ABIArgInfo::Expand:
1459 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001460 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001461
1462 assert(0 && "Unhandled ABIArgInfo::Kind");
1463 return RValue::get(0);
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001464}