blob: 61c6653236df20aba4289794be507c8d06cf7e01 [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 Dunbarf0357382008-09-17 20:11:04 +0000319 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar834af452008-09-17 21:22:33 +0000320 // Structures with flexible arrays are always byval.
321 if (const RecordType *RT = Ty->getAsStructureType())
322 if (RT->getDecl()->hasFlexibleArrayMember())
323 return ABIArgInfo::getByVal(0);
324
325 // Expand empty structs (i.e. ignore)
326 uint64_t Size = Context.getTypeSize(Ty);
327 if (Ty->isStructureType() && Size == 0)
328 return ABIArgInfo::getExpand();
329
330 // Expand structs with size <= 128-bits which consist only of
331 // basic types (int, long long, float, double, xxx*). This is
332 // non-recursive and does not ignore empty fields.
333 if (const RecordType *RT = Ty->getAsStructureType()) {
334 if (Context.getTypeSize(Ty) <= 4*32 &&
335 areAllFields32Or64BitBasicType(RT->getDecl(), Context))
336 return ABIArgInfo::getExpand();
337 }
338
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000339 return ABIArgInfo::getByVal(0);
340 } else {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000341 return ABIArgInfo::getDirect();
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000342 }
343}
344
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000345namespace {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000346/// X86_64ABIInfo - The X86_64 ABI information.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000347class X86_64ABIInfo : public ABIInfo {
348 enum Class {
349 Integer = 0,
350 SSE,
351 SSEUp,
352 X87,
353 X87Up,
354 ComplexX87,
355 NoClass,
356 Memory
357 };
358
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000359 /// merge - Implement the X86_64 ABI merging algorithm.
360 ///
Daniel Dunbarc4503572009-01-31 00:06:58 +0000361 /// Merge an accumulating classification \arg Accum with a field
362 /// classification \arg Field.
363 ///
364 /// \param Accum - The accumulating classification. This should
365 /// always be either NoClass or the result of a previous merge
366 /// call. In addition, this should never be Memory (the caller
367 /// should just return Memory for the aggregate).
368 Class merge(Class Accum, Class Field) const;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000369
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000370 /// classify - Determine the x86_64 register classes in which the
371 /// given type T should be passed.
372 ///
Daniel Dunbarc4503572009-01-31 00:06:58 +0000373 /// \param Lo - The classification for the parts of the type
374 /// residing in the low word of the containing object.
375 ///
376 /// \param Hi - The classification for the parts of the type
377 /// residing in the high word of the containing object.
378 ///
379 /// \param OffsetBase - The bit offset of this type in the
Daniel Dunbarcdf920e2009-01-30 22:40:15 +0000380 /// containing object. Some parameters are classified different
381 /// depending on whether they straddle an eightbyte boundary.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000382 ///
383 /// If a word is unused its result will be NoClass; if a type should
384 /// be passed in Memory then at least the classification of \arg Lo
385 /// will be Memory.
386 ///
387 /// The \arg Lo class will be NoClass iff the argument is ignored.
388 ///
389 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
390 /// be NoClass.
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000391 void classify(QualType T, ASTContext &Context, uint64_t OffsetBase,
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000392 Class &Lo, Class &Hi) const;
Daniel Dunbarc4503572009-01-31 00:06:58 +0000393
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000394 ABIArgInfo classifyReturnType(QualType RetTy,
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000395 ASTContext &Context) const;
396
397 ABIArgInfo classifyArgumentType(QualType Ty,
398 ASTContext &Context,
399 unsigned &freeIntRegs,
400 unsigned &freeSSERegs) const;
401
402public:
403 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000404};
405}
406
Daniel Dunbarc4503572009-01-31 00:06:58 +0000407X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum,
408 Class Field) const {
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000409 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
410 // classified recursively so that always two fields are
411 // considered. The resulting class is calculated according to
412 // the classes of the fields in the eightbyte:
413 //
414 // (a) If both classes are equal, this is the resulting class.
415 //
416 // (b) If one of the classes is NO_CLASS, the resulting class is
417 // the other class.
418 //
419 // (c) If one of the classes is MEMORY, the result is the MEMORY
420 // class.
421 //
422 // (d) If one of the classes is INTEGER, the result is the
423 // INTEGER.
424 //
425 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
426 // MEMORY is used as class.
427 //
428 // (f) Otherwise class SSE is used.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000429 assert((Accum == NoClass || Accum == Integer ||
430 Accum == SSE || Accum == SSEUp) &&
431 "Invalid accumulated classification during merge.");
432 if (Accum == Field || Field == NoClass)
433 return Accum;
434 else if (Field == Memory)
435 return Memory;
436 else if (Accum == NoClass)
437 return Field;
438 else if (Accum == Integer || Field == Integer)
439 return Integer;
440 else if (Field == X87 || Field == X87Up || Field == ComplexX87)
441 return Memory;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000442 else
Daniel Dunbarc4503572009-01-31 00:06:58 +0000443 return SSE;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000444}
445
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000446void X86_64ABIInfo::classify(QualType Ty,
447 ASTContext &Context,
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000448 uint64_t OffsetBase,
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000449 Class &Lo, Class &Hi) const {
Daniel Dunbar9a82b522009-02-02 18:06:39 +0000450 // FIXME: This code can be simplified by introducing a simple value
451 // class for Class pairs with appropriate constructor methods for
452 // the various situations.
453
Daniel Dunbarc4503572009-01-31 00:06:58 +0000454 Lo = Hi = NoClass;
455
456 Class &Current = OffsetBase < 64 ? Lo : Hi;
457 Current = Memory;
458
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000459 if (const BuiltinType *BT = Ty->getAsBuiltinType()) {
460 BuiltinType::Kind k = BT->getKind();
461
Daniel Dunbar11434922009-01-26 21:26:08 +0000462 if (k == BuiltinType::Void) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000463 Current = NoClass;
Daniel Dunbar11434922009-01-26 21:26:08 +0000464 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000465 Current = Integer;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000466 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000467 Current = SSE;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000468 } else if (k == BuiltinType::LongDouble) {
469 Lo = X87;
470 Hi = X87Up;
471 }
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000472 // FIXME: _Decimal32 and _Decimal64 are SSE.
473 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000474 // FIXME: __int128 is (Integer, Integer).
475 } else if (Ty->isPointerLikeType() || Ty->isBlockPointerType() ||
476 Ty->isObjCQualifiedInterfaceType()) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000477 Current = Integer;
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000478 } else if (const VectorType *VT = Ty->getAsVectorType()) {
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000479 uint64_t Size = Context.getTypeSize(VT);
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000480 if (Size == 64) {
Daniel Dunbard4cd1b02009-01-30 19:38:39 +0000481 // gcc passes <1 x double> in memory.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000482 if (VT->getElementType() == Context.DoubleTy)
Daniel Dunbard4cd1b02009-01-30 19:38:39 +0000483 return;
Daniel Dunbard4cd1b02009-01-30 19:38:39 +0000484
Daniel Dunbarc4503572009-01-31 00:06:58 +0000485 Current = SSE;
Daniel Dunbare33edf12009-01-30 18:40:10 +0000486
487 // If this type crosses an eightbyte boundary, it should be
488 // split.
Daniel Dunbarcdf920e2009-01-30 22:40:15 +0000489 if (OffsetBase && OffsetBase != 64)
Daniel Dunbare33edf12009-01-30 18:40:10 +0000490 Hi = Lo;
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000491 } else if (Size == 128) {
492 Lo = SSE;
493 Hi = SSEUp;
494 }
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000495 } else if (const ComplexType *CT = Ty->getAsComplexType()) {
496 QualType ET = CT->getElementType();
497
Daniel Dunbare33edf12009-01-30 18:40:10 +0000498 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbareac48dc2009-01-29 07:22:20 +0000499 if (ET->isIntegerType()) {
Daniel Dunbareac48dc2009-01-29 07:22:20 +0000500 if (Size <= 64)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000501 Current = Integer;
Daniel Dunbareac48dc2009-01-29 07:22:20 +0000502 else if (Size <= 128)
503 Lo = Hi = Integer;
504 } else if (ET == Context.FloatTy)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000505 Current = SSE;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000506 else if (ET == Context.DoubleTy)
507 Lo = Hi = SSE;
508 else if (ET == Context.LongDoubleTy)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000509 Current = ComplexX87;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000510
511 // If this complex type crosses an eightbyte boundary then it
512 // should be split.
Daniel Dunbarcdf920e2009-01-30 22:40:15 +0000513 uint64_t EB_Real = (OffsetBase) / 64;
514 uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000515 if (Hi == NoClass && EB_Real != EB_Imag)
516 Hi = Lo;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000517 } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
518 // Arrays are treated like structures.
519
520 uint64_t Size = Context.getTypeSize(Ty);
521
522 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
523 // than two eightbytes, ..., it has class MEMORY.
524 if (Size > 128)
525 return;
526
527 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
528 // fields, it has class MEMORY.
529 //
530 // Only need to check alignment of array base.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000531 if (OffsetBase % Context.getTypeAlign(AT->getElementType()))
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000532 return;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000533
534 // Otherwise implement simplified merge. We could be smarter about
535 // this, but it isn't worth it and would be harder to verify.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000536 Current = NoClass;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000537 uint64_t EltSize = Context.getTypeSize(AT->getElementType());
538 uint64_t ArraySize = AT->getSize().getZExtValue();
539 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
540 Class FieldLo, FieldHi;
541 classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbarc4503572009-01-31 00:06:58 +0000542 Lo = merge(Lo, FieldLo);
543 Hi = merge(Hi, FieldHi);
544 if (Lo == Memory || Hi == Memory)
545 break;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000546 }
Daniel Dunbarc4503572009-01-31 00:06:58 +0000547
548 // Do post merger cleanup (see below). Only case we worry about is Memory.
549 if (Hi == Memory)
550 Lo = Memory;
551 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Daniel Dunbar99037e52009-01-29 08:13:58 +0000552 } else if (const RecordType *RT = Ty->getAsRecordType()) {
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000553 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbar99037e52009-01-29 08:13:58 +0000554
555 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
556 // than two eightbytes, ..., it has class MEMORY.
557 if (Size > 128)
558 return;
559
560 const RecordDecl *RD = RT->getDecl();
561
562 // Assume variable sized types are passed in memory.
563 if (RD->hasFlexibleArrayMember())
564 return;
565
566 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
567
568 // Reset Lo class, this will be recomputed.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000569 Current = NoClass;
Daniel Dunbar99037e52009-01-29 08:13:58 +0000570 unsigned idx = 0;
571 for (RecordDecl::field_iterator i = RD->field_begin(),
572 e = RD->field_end(); i != e; ++i, ++idx) {
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000573 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Daniel Dunbar99037e52009-01-29 08:13:58 +0000574
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000575 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
576 // fields, it has class MEMORY.
Daniel Dunbar99037e52009-01-29 08:13:58 +0000577 if (Offset % Context.getTypeAlign(i->getType())) {
578 Lo = Memory;
579 return;
580 }
581
Daniel Dunbar99037e52009-01-29 08:13:58 +0000582 // Classify this field.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000583 //
584 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
585 // exceeds a single eightbyte, each is classified
586 // separately. Each eightbyte gets initialized to class
587 // NO_CLASS.
Daniel Dunbar99037e52009-01-29 08:13:58 +0000588 Class FieldLo, FieldHi;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000589 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbarc4503572009-01-31 00:06:58 +0000590 Lo = merge(Lo, FieldLo);
591 Hi = merge(Hi, FieldHi);
592 if (Lo == Memory || Hi == Memory)
593 break;
Daniel Dunbar99037e52009-01-29 08:13:58 +0000594 }
595
596 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
597 //
598 // (a) If one of the classes is MEMORY, the whole argument is
599 // passed in memory.
600 //
601 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
602
603 // The first of these conditions is guaranteed by how we implement
Daniel Dunbarc4503572009-01-31 00:06:58 +0000604 // the merge (just bail).
605 //
606 // The second condition occurs in the case of unions; for example
607 // union { _Complex double; unsigned; }.
608 if (Hi == Memory)
609 Lo = Memory;
Daniel Dunbar99037e52009-01-29 08:13:58 +0000610 if (Hi == SSEUp && Lo != SSE)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000611 Hi = SSE;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000612 }
613}
614
Daniel Dunbarc4503572009-01-31 00:06:58 +0000615
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000616ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
617 ASTContext &Context) const {
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000618 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
619 // classification algorithm.
620 X86_64ABIInfo::Class Lo, Hi;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000621 classify(RetTy, Context, 0, Lo, Hi);
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000622
Daniel Dunbarc4503572009-01-31 00:06:58 +0000623 // Check some invariants.
624 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
625 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
626 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
627
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000628 const llvm::Type *ResType = 0;
629 switch (Lo) {
630 case NoClass:
Daniel Dunbar11434922009-01-26 21:26:08 +0000631 return ABIArgInfo::getIgnore();
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000632
633 case SSEUp:
634 case X87Up:
635 assert(0 && "Invalid classification for lo word.");
636
Daniel Dunbarc4503572009-01-31 00:06:58 +0000637 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
638 // hidden argument, i.e. structret.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000639 case Memory:
640 return ABIArgInfo::getStructRet();
641
642 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
643 // available register of the sequence %rax, %rdx is used.
644 case Integer:
645 ResType = llvm::Type::Int64Ty; break;
646
647 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
648 // available SSE register of the sequence %xmm0, %xmm1 is used.
649 case SSE:
650 ResType = llvm::Type::DoubleTy; break;
651
652 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
653 // returned on the X87 stack in %st0 as 80-bit x87 number.
654 case X87:
655 ResType = llvm::Type::X86_FP80Ty; break;
656
Daniel Dunbarc4503572009-01-31 00:06:58 +0000657 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
658 // part of the value is returned in %st0 and the imaginary part in
659 // %st1.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000660 case ComplexX87:
661 assert(Hi == NoClass && "Unexpected ComplexX87 classification.");
662 ResType = llvm::VectorType::get(llvm::Type::X86_FP80Ty, 2);
663 break;
664 }
665
666 switch (Hi) {
667 // Memory was handled previously, and ComplexX87 and X87 should
668 // never occur as hi classes.
669 case Memory:
670 case X87:
671 case ComplexX87:
672 assert(0 && "Invalid classification for hi word.");
673
674 case NoClass: break;
675 case Integer:
Daniel Dunbarb0e14f22009-01-29 07:36:07 +0000676 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
677 break;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000678 case SSE:
Daniel Dunbarb0e14f22009-01-29 07:36:07 +0000679 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
680 break;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000681
682 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
683 // is passed in the upper half of the last used SSE register.
684 //
685 // SSEUP should always be preceeded by SSE, just widen.
686 case SSEUp:
687 assert(Lo == SSE && "Unexpected SSEUp classification.");
688 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
689 break;
690
691 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
Daniel Dunbarb0e14f22009-01-29 07:36:07 +0000692 // returned together with the previous X87 value in %st0.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000693 //
694 // X87UP should always be preceeded by X87, so we don't need to do
695 // anything here.
696 case X87Up:
697 assert(Lo == X87 && "Unexpected X87Up classification.");
698 break;
699 }
700
701 return ABIArgInfo::getCoerce(ResType);
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000702}
703
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000704ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, ASTContext &Context,
705 unsigned &freeIntRegs,
706 unsigned &freeSSERegs) const {
707 X86_64ABIInfo::Class Lo, Hi;
708 classify(Ty, Context, 0, Lo, Hi);
709
710 // Check some invariants.
711 // FIXME: Enforce these by construction.
712 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
713 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
714 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
715
716 unsigned neededInt = 0, neededSSE = 0;
717 const llvm::Type *ResType = 0;
718 switch (Lo) {
719 case NoClass:
720 return ABIArgInfo::getIgnore();
721
722 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
723 // on the stack.
724 case Memory:
725
726 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
727 // COMPLEX_X87, it is passed in memory.
728 case X87:
729 case ComplexX87:
730 // Choose appropriate in memory type.
731 if (CodeGenFunction::hasAggregateLLVMType(Ty))
732 return ABIArgInfo::getByVal(0);
733 else
734 return ABIArgInfo::getDirect();
735
736 case SSEUp:
737 case X87Up:
738 assert(0 && "Invalid classification for lo word.");
739
740 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
741 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
742 // and %r9 is used.
743 case Integer:
744 ++neededInt;
745 ResType = llvm::Type::Int64Ty;
746 break;
747
748 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
749 // available SSE register is used, the registers are taken in the
750 // order from %xmm0 to %xmm7.
751 case SSE:
752 ++neededSSE;
753 ResType = llvm::Type::DoubleTy;
754 break;
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000755 }
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000756
757 switch (Hi) {
758 // Memory was handled previously, ComplexX87 and X87 should
759 // never occur as hi classes, and X87Up must be preceed by X87,
760 // which is passed in memory.
761 case Memory:
762 case X87:
763 case X87Up:
764 case ComplexX87:
765 assert(0 && "Invalid classification for hi word.");
766
767 case NoClass: break;
768 case Integer:
769 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
770 ++neededInt;
771 break;
772 case SSE:
773 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
774 ++neededSSE;
775 break;
776
777 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
778 // eightbyte is passed in the upper half of the last used SSE
779 // register.
780 case SSEUp:
781 assert(Lo == SSE && "Unexpected SSEUp classification.");
782 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
783 break;
784 }
785
786 // AMD64-ABI 3.2.3p3: If there are no registers available for any
787 // eightbyte of an argument, the whole argument is passed on the
788 // stack. If registers have already been assigned for some
789 // eightbytes of such an argument, the assignments get reverted.
790 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
791 freeIntRegs -= neededInt;
792 freeSSERegs -= neededSSE;
793 return ABIArgInfo::getCoerce(ResType);
794 } else {
795 // Choose appropriate in memory type.
796 if (CodeGenFunction::hasAggregateLLVMType(Ty))
797 return ABIArgInfo::getByVal(0);
798 else
799 return ABIArgInfo::getDirect();
800 }
801}
802
803void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
804 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
805
806 // Keep track of the number of assigned registers.
807 unsigned freeIntRegs = 6, freeSSERegs = 8;
808
809 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
810 // get assigned (in left-to-right order) for passing as follows...
811 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
812 it != ie; ++it)
813 it->info = classifyArgumentType(it->type, Context, freeIntRegs, freeSSERegs);
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000814}
815
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000816ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
817 ASTContext &Context) const {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000818 if (RetTy->isVoidType()) {
819 return ABIArgInfo::getIgnore();
820 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
821 return ABIArgInfo::getStructRet();
822 } else {
823 return ABIArgInfo::getDirect();
824 }
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000825}
826
827ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
828 ASTContext &Context) const {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000829 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
830 return ABIArgInfo::getByVal(0);
831 } else {
832 return ABIArgInfo::getDirect();
833 }
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000834}
835
836const ABIInfo &CodeGenTypes::getABIInfo() const {
837 if (TheABIInfo)
838 return *TheABIInfo;
839
840 // For now we just cache this in the CodeGenTypes and don't bother
841 // to free it.
842 const char *TargetPrefix = getContext().Target.getTargetPrefix();
843 if (strcmp(TargetPrefix, "x86") == 0) {
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000844 switch (getContext().Target.getPointerWidth(0)) {
845 case 32:
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000846 return *(TheABIInfo = new X86_32ABIInfo());
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000847 case 64:
Daniel Dunbar11a76ed2009-01-30 18:47:53 +0000848 return *(TheABIInfo = new X86_64ABIInfo());
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000849 }
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000850 }
851
852 return *(TheABIInfo = new DefaultABIInfo);
853}
854
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000855/***/
856
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000857CGFunctionInfo::CGFunctionInfo(QualType ResTy,
858 const llvm::SmallVector<QualType, 16> &ArgTys) {
859 NumArgs = ArgTys.size();
860 Args = new ArgInfo[1 + NumArgs];
861 Args[0].type = ResTy;
862 for (unsigned i = 0; i < NumArgs; ++i)
863 Args[1 + i].type = ArgTys[i];
864}
865
866/***/
867
Daniel Dunbar56273772008-09-17 00:51:38 +0000868void CodeGenTypes::GetExpandedTypes(QualType Ty,
869 std::vector<const llvm::Type*> &ArgTys) {
870 const RecordType *RT = Ty->getAsStructureType();
871 assert(RT && "Can only expand structure types.");
872 const RecordDecl *RD = RT->getDecl();
873 assert(!RD->hasFlexibleArrayMember() &&
874 "Cannot expand structure with flexible array.");
875
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000876 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar56273772008-09-17 00:51:38 +0000877 e = RD->field_end(); i != e; ++i) {
878 const FieldDecl *FD = *i;
879 assert(!FD->isBitField() &&
880 "Cannot expand structure with bit-field members.");
881
882 QualType FT = FD->getType();
883 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
884 GetExpandedTypes(FT, ArgTys);
885 } else {
886 ArgTys.push_back(ConvertType(FT));
887 }
888 }
889}
890
891llvm::Function::arg_iterator
892CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
893 llvm::Function::arg_iterator AI) {
894 const RecordType *RT = Ty->getAsStructureType();
895 assert(RT && "Can only expand structure types.");
896
897 RecordDecl *RD = RT->getDecl();
898 assert(LV.isSimple() &&
899 "Unexpected non-simple lvalue during struct expansion.");
900 llvm::Value *Addr = LV.getAddress();
901 for (RecordDecl::field_iterator i = RD->field_begin(),
902 e = RD->field_end(); i != e; ++i) {
903 FieldDecl *FD = *i;
904 QualType FT = FD->getType();
905
906 // FIXME: What are the right qualifiers here?
907 LValue LV = EmitLValueForField(Addr, FD, false, 0);
908 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
909 AI = ExpandTypeFromArgs(FT, LV, AI);
910 } else {
911 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
912 ++AI;
913 }
914 }
915
916 return AI;
917}
918
919void
920CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
921 llvm::SmallVector<llvm::Value*, 16> &Args) {
922 const RecordType *RT = Ty->getAsStructureType();
923 assert(RT && "Can only expand structure types.");
924
925 RecordDecl *RD = RT->getDecl();
926 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
927 llvm::Value *Addr = RV.getAggregateAddr();
928 for (RecordDecl::field_iterator i = RD->field_begin(),
929 e = RD->field_end(); i != e; ++i) {
930 FieldDecl *FD = *i;
931 QualType FT = FD->getType();
932
933 // FIXME: What are the right qualifiers here?
934 LValue LV = EmitLValueForField(Addr, FD, false, 0);
935 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
936 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
937 } else {
938 RValue RV = EmitLoadOfLValue(LV, FT);
939 assert(RV.isScalar() &&
940 "Unexpected non-scalar rvalue during struct expansion.");
941 Args.push_back(RV.getScalarVal());
942 }
943 }
944}
945
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000946/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
947/// a pointer to an object of type \arg Ty.
948///
949/// This safely handles the case when the src type is smaller than the
950/// destination type; in this situation the values of bits which not
951/// present in the src are undefined.
952static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
953 const llvm::Type *Ty,
954 CodeGenFunction &CGF) {
955 const llvm::Type *SrcTy =
956 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
957 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
958 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(Ty);
959
Daniel Dunbarb225be42009-02-03 05:59:18 +0000960 // If load is legal, just bitcast the src pointer.
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000961 if (SrcSize == DstSize) {
962 llvm::Value *Casted =
963 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
964 return CGF.Builder.CreateLoad(Casted);
965 } else {
966 assert(SrcSize < DstSize && "Coercion is losing source bits!");
967
968 // Otherwise do coercion through memory. This is stupid, but
969 // simple.
970 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
971 llvm::Value *Casted =
972 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
973 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
974 return CGF.Builder.CreateLoad(Tmp);
975 }
976}
977
978/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
979/// where the source and destination may have different types.
980///
981/// This safely handles the case when the src type is larger than the
982/// destination type; the upper bits of the src will be lost.
983static void CreateCoercedStore(llvm::Value *Src,
984 llvm::Value *DstPtr,
985 CodeGenFunction &CGF) {
986 const llvm::Type *SrcTy = Src->getType();
987 const llvm::Type *DstTy =
988 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
989
990 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
991 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(DstTy);
992
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000993 // If store is legal, just bitcast the src pointer.
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000994 if (SrcSize == DstSize) {
995 llvm::Value *Casted =
996 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
997 CGF.Builder.CreateStore(Src, Casted);
998 } else {
999 assert(SrcSize > DstSize && "Coercion is missing bits!");
1000
1001 // Otherwise do coercion through memory. This is stupid, but
1002 // simple.
1003 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
1004 CGF.Builder.CreateStore(Src, Tmp);
1005 llvm::Value *Casted =
1006 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
1007 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(Casted), DstPtr);
1008 }
1009}
1010
Daniel Dunbar56273772008-09-17 00:51:38 +00001011/***/
1012
Daniel Dunbar88b53962009-02-02 22:03:45 +00001013bool CodeGenModule::ReturnTypeUsesSret(const CGFunctionInfo &FI) {
Daniel Dunbarb225be42009-02-03 05:59:18 +00001014 return FI.getReturnInfo().isStructRet();
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001015}
1016
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001017const llvm::FunctionType *
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001018CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic) {
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001019 std::vector<const llvm::Type*> ArgTys;
1020
1021 const llvm::Type *ResultType = 0;
1022
Daniel Dunbara0a99e02009-02-02 23:43:58 +00001023 QualType RetTy = FI.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001024 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001025 switch (RetAI.getKind()) {
1026 case ABIArgInfo::ByVal:
1027 case ABIArgInfo::Expand:
1028 assert(0 && "Invalid ABI kind for return argument");
1029
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001030 case ABIArgInfo::Direct:
1031 ResultType = ConvertType(RetTy);
1032 break;
1033
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001034 case ABIArgInfo::StructRet: {
1035 ResultType = llvm::Type::VoidTy;
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +00001036 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001037 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
1038 break;
1039 }
1040
Daniel Dunbar11434922009-01-26 21:26:08 +00001041 case ABIArgInfo::Ignore:
1042 ResultType = llvm::Type::VoidTy;
1043 break;
1044
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001045 case ABIArgInfo::Coerce:
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001046 ResultType = RetAI.getCoerceToType();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001047 break;
1048 }
1049
Daniel Dunbar88c2fa92009-02-03 05:31:23 +00001050 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1051 ie = FI.arg_end(); it != ie; ++it) {
1052 const ABIArgInfo &AI = it->info;
1053 const llvm::Type *Ty = ConvertType(it->type);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001054
1055 switch (AI.getKind()) {
Daniel Dunbar11434922009-01-26 21:26:08 +00001056 case ABIArgInfo::Ignore:
1057 break;
1058
Daniel Dunbar56273772008-09-17 00:51:38 +00001059 case ABIArgInfo::Coerce:
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001060 ArgTys.push_back(AI.getCoerceToType());
1061 break;
1062
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001063 case ABIArgInfo::StructRet:
1064 assert(0 && "Invalid ABI kind for non-return argument");
1065
1066 case ABIArgInfo::ByVal:
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001067 // byval arguments are always on the stack, which is addr space #0.
1068 ArgTys.push_back(llvm::PointerType::getUnqual(Ty));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001069 assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled");
1070 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 Dunbar8951dbd2008-09-11 01:48:57 +00001149 assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled");
1150 break;
1151
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001152 case ABIArgInfo::Direct:
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001153 if (ParamType->isPromotableIntegerType()) {
1154 if (ParamType->isSignedIntegerType()) {
Devang Patel761d7f72008-09-25 21:02:23 +00001155 Attributes |= llvm::Attribute::SExt;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001156 } else if (ParamType->isUnsignedIntegerType()) {
Devang Patel761d7f72008-09-25 21:02:23 +00001157 Attributes |= llvm::Attribute::ZExt;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001158 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001159 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001160 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001161
Daniel Dunbar11434922009-01-26 21:26:08 +00001162 case ABIArgInfo::Ignore:
1163 // Skip increment, no matching LLVM parameter.
1164 continue;
1165
Daniel Dunbar56273772008-09-17 00:51:38 +00001166 case ABIArgInfo::Expand: {
1167 std::vector<const llvm::Type*> Tys;
1168 // FIXME: This is rather inefficient. Do we ever actually need
1169 // to do anything here? The result should be just reconstructed
1170 // on the other side, so extension should be a non-issue.
1171 getTypes().GetExpandedTypes(ParamType, Tys);
1172 Index += Tys.size();
1173 continue;
1174 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001175 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001176
Devang Patel761d7f72008-09-25 21:02:23 +00001177 if (Attributes)
1178 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar56273772008-09-17 00:51:38 +00001179 ++Index;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001180 }
Devang Patela2c69122008-09-26 22:53:57 +00001181 if (FuncAttrs)
1182 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
1183
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001184}
1185
Daniel Dunbar88b53962009-02-02 22:03:45 +00001186void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
1187 llvm::Function *Fn,
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001188 const FunctionArgList &Args) {
Daniel Dunbar5251afa2009-02-03 06:02:10 +00001189 // FIXME: We no longer need the types from FunctionArgList; lift up
1190 // and simplify.
1191
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001192 // Emit allocs for param decls. Give the LLVM Argument nodes names.
1193 llvm::Function::arg_iterator AI = Fn->arg_begin();
1194
1195 // Name the struct return argument.
Daniel Dunbar88b53962009-02-02 22:03:45 +00001196 if (CGM.ReturnTypeUsesSret(FI)) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001197 AI->setName("agg.result");
1198 ++AI;
1199 }
Daniel Dunbarb225be42009-02-03 05:59:18 +00001200
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +00001201 assert(FI.arg_size() == Args.size() &&
1202 "Mismatch between function signature & arguments.");
Daniel Dunbarb225be42009-02-03 05:59:18 +00001203 CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001204 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001205 i != e; ++i, ++info_it) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001206 const VarDecl *Arg = i->first;
Daniel Dunbarb225be42009-02-03 05:59:18 +00001207 QualType Ty = info_it->type;
1208 const ABIArgInfo &ArgI = info_it->info;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001209
1210 switch (ArgI.getKind()) {
1211 case ABIArgInfo::ByVal:
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001212 case ABIArgInfo::Direct: {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001213 assert(AI != Fn->arg_end() && "Argument mismatch!");
1214 llvm::Value* V = AI;
Daniel Dunbar56273772008-09-17 00:51:38 +00001215 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001216 // This must be a promotion, for something like
1217 // "void a(x) short x; {..."
Daniel Dunbar56273772008-09-17 00:51:38 +00001218 V = EmitScalarConversion(V, Ty, Arg->getType());
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001219 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001220 EmitParmDecl(*Arg, V);
1221 break;
1222 }
Daniel Dunbar56273772008-09-17 00:51:38 +00001223
1224 case ABIArgInfo::Expand: {
Daniel Dunbarb225be42009-02-03 05:59:18 +00001225 // If this structure was expanded into multiple arguments then
Daniel Dunbar56273772008-09-17 00:51:38 +00001226 // we need to create a temporary and reconstruct it from the
1227 // arguments.
Chris Lattner39f34e92008-11-24 04:00:27 +00001228 std::string Name = Arg->getNameAsString();
Daniel Dunbar56273772008-09-17 00:51:38 +00001229 llvm::Value *Temp = CreateTempAlloca(ConvertType(Ty),
1230 (Name + ".addr").c_str());
1231 // FIXME: What are the right qualifiers here?
1232 llvm::Function::arg_iterator End =
1233 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI);
1234 EmitParmDecl(*Arg, Temp);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001235
Daniel Dunbar56273772008-09-17 00:51:38 +00001236 // Name the arguments used in expansion and increment AI.
1237 unsigned Index = 0;
1238 for (; AI != End; ++AI, ++Index)
1239 AI->setName(Name + "." + llvm::utostr(Index));
1240 continue;
1241 }
Daniel Dunbar11434922009-01-26 21:26:08 +00001242
1243 case ABIArgInfo::Ignore:
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +00001244 // Skip increment, no matching LLVM parameter.
1245 continue;
Daniel Dunbar11434922009-01-26 21:26:08 +00001246
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001247 case ABIArgInfo::Coerce: {
1248 assert(AI != Fn->arg_end() && "Argument mismatch!");
1249 // FIXME: This is very wasteful; EmitParmDecl is just going to
1250 // drop the result in a new alloca anyway, so we could just
1251 // store into that directly if we broke the abstraction down
1252 // more.
1253 llvm::Value *V = CreateTempAlloca(ConvertType(Ty), "coerce");
1254 CreateCoercedStore(AI, V, *this);
1255 // Match to what EmitParmDecl is expecting for this type.
Daniel Dunbar8b29a382009-02-04 07:22:24 +00001256 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001257 V = Builder.CreateLoad(V);
Daniel Dunbar8b29a382009-02-04 07:22:24 +00001258 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1259 // This must be a promotion, for something like
1260 // "void a(x) short x; {..."
1261 V = EmitScalarConversion(V, Ty, Arg->getType());
1262 }
1263 }
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001264 EmitParmDecl(*Arg, V);
1265 break;
1266 }
1267
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001268 case ABIArgInfo::StructRet:
1269 assert(0 && "Invalid ABI kind for non-return argument");
1270 }
Daniel Dunbar56273772008-09-17 00:51:38 +00001271
1272 ++AI;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001273 }
1274 assert(AI == Fn->arg_end() && "Argument mismatch!");
1275}
1276
Daniel Dunbar88b53962009-02-02 22:03:45 +00001277void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI,
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001278 llvm::Value *ReturnValue) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001279 llvm::Value *RV = 0;
1280
1281 // Functions with no result always return void.
1282 if (ReturnValue) {
Daniel Dunbar88b53962009-02-02 22:03:45 +00001283 QualType RetTy = FI.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001284 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001285
1286 switch (RetAI.getKind()) {
1287 case ABIArgInfo::StructRet:
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001288 if (RetTy->isAnyComplexType()) {
1289 // FIXME: Volatile
1290 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
1291 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
1292 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1293 EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy);
1294 } else {
1295 Builder.CreateStore(Builder.CreateLoad(ReturnValue),
1296 CurFn->arg_begin());
1297 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001298 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001299
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001300 case ABIArgInfo::Direct:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001301 RV = Builder.CreateLoad(ReturnValue);
1302 break;
1303
Daniel Dunbar11434922009-01-26 21:26:08 +00001304 case ABIArgInfo::Ignore:
1305 break;
1306
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001307 case ABIArgInfo::Coerce: {
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +00001308 RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001309 break;
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001310 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001311
1312 case ABIArgInfo::ByVal:
1313 case ABIArgInfo::Expand:
1314 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001315 }
1316 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001317
1318 if (RV) {
1319 Builder.CreateRet(RV);
1320 } else {
1321 Builder.CreateRetVoid();
1322 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001323}
1324
Daniel Dunbar88b53962009-02-02 22:03:45 +00001325RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
1326 llvm::Value *Callee,
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001327 const CallArgList &CallArgs) {
Daniel Dunbar5251afa2009-02-03 06:02:10 +00001328 // FIXME: We no longer need the types from CallArgs; lift up and
1329 // simplify.
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001330 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001331
1332 // Handle struct-return functions by passing a pointer to the
1333 // location that we would like to return into.
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001334 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001335 const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001336 switch (RetAI.getKind()) {
1337 case ABIArgInfo::StructRet:
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001338 // Create a temporary alloca to hold the result of the call. :(
Daniel Dunbar56273772008-09-17 00:51:38 +00001339 Args.push_back(CreateTempAlloca(ConvertType(RetTy)));
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001340 break;
1341
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001342 case ABIArgInfo::Direct:
Daniel Dunbar11434922009-01-26 21:26:08 +00001343 case ABIArgInfo::Ignore:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001344 case ABIArgInfo::Coerce:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001345 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001346
1347 case ABIArgInfo::ByVal:
1348 case ABIArgInfo::Expand:
Daniel Dunbarb225be42009-02-03 05:59:18 +00001349 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001350 }
1351
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +00001352 assert(CallInfo.arg_size() == CallArgs.size() &&
1353 "Mismatch between function signature & arguments.");
Daniel Dunbarb225be42009-02-03 05:59:18 +00001354 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001355 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001356 I != E; ++I, ++info_it) {
1357 const ABIArgInfo &ArgInfo = info_it->info;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001358 RValue RV = I->first;
Daniel Dunbar56273772008-09-17 00:51:38 +00001359
1360 switch (ArgInfo.getKind()) {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001361 case ABIArgInfo::ByVal: // Direct is byval
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001362 case ABIArgInfo::Direct:
Daniel Dunbar56273772008-09-17 00:51:38 +00001363 if (RV.isScalar()) {
1364 Args.push_back(RV.getScalarVal());
1365 } else if (RV.isComplex()) {
1366 // Make a temporary alloca to pass the argument.
1367 Args.push_back(CreateTempAlloca(ConvertType(I->second)));
1368 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
1369 } else {
1370 Args.push_back(RV.getAggregateAddr());
1371 }
1372 break;
1373
Daniel Dunbar11434922009-01-26 21:26:08 +00001374 case ABIArgInfo::Ignore:
1375 break;
1376
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001377 case ABIArgInfo::Coerce: {
1378 // FIXME: Avoid the conversion through memory if possible.
1379 llvm::Value *SrcPtr;
1380 if (RV.isScalar()) {
Daniel Dunbar5a1be6e2009-02-03 23:04:57 +00001381 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001382 Builder.CreateStore(RV.getScalarVal(), SrcPtr);
1383 } else if (RV.isComplex()) {
1384 SrcPtr = CreateTempAlloca(ConvertType(I->second), "coerce");
1385 StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
1386 } else
1387 SrcPtr = RV.getAggregateAddr();
1388 Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
1389 *this));
1390 break;
1391 }
1392
Daniel Dunbar56273772008-09-17 00:51:38 +00001393 case ABIArgInfo::StructRet:
Daniel Dunbar56273772008-09-17 00:51:38 +00001394 assert(0 && "Invalid ABI kind for non-return argument");
1395 break;
1396
1397 case ABIArgInfo::Expand:
1398 ExpandTypeToArgs(I->second, RV, Args);
1399 break;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001400 }
1401 }
1402
1403 llvm::CallInst *CI = Builder.CreateCall(Callee,&Args[0],&Args[0]+Args.size());
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001404
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001405 // FIXME: Provide TargetDecl so nounwind, noreturn, etc, etc get set.
Devang Patel761d7f72008-09-25 21:02:23 +00001406 CodeGen::AttributeListType AttributeList;
Daniel Dunbar88b53962009-02-02 22:03:45 +00001407 CGM.ConstructAttributeList(CallInfo, 0, AttributeList);
Devang Patel761d7f72008-09-25 21:02:23 +00001408 CI->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(),
Daniel Dunbar725ad312009-01-31 02:19:00 +00001409 AttributeList.size()));
1410
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001411 if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee))
1412 CI->setCallingConv(F->getCallingConv());
1413 if (CI->getType() != llvm::Type::VoidTy)
1414 CI->setName("call");
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001415
1416 switch (RetAI.getKind()) {
1417 case ABIArgInfo::StructRet:
1418 if (RetTy->isAnyComplexType())
Daniel Dunbar56273772008-09-17 00:51:38 +00001419 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001420 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar56273772008-09-17 00:51:38 +00001421 return RValue::getAggregate(Args[0]);
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001422 else
1423 return RValue::get(Builder.CreateLoad(Args[0]));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001424
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001425 case ABIArgInfo::Direct:
1426 assert((!RetTy->isAnyComplexType() &&
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001427 !CodeGenFunction::hasAggregateLLVMType(RetTy)) &&
1428 "FIXME: Implement return for non-scalar direct types.");
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001429 return RValue::get(CI);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001430
Daniel Dunbar11434922009-01-26 21:26:08 +00001431 case ABIArgInfo::Ignore:
Daniel Dunbarcc039fe2009-01-29 08:24:57 +00001432 if (RetTy->isVoidType())
1433 return RValue::get(0);
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001434
1435 // If we are ignoring an argument that had a result, make sure to
1436 // construct the appropriate return value for our caller.
Daniel Dunbarcc039fe2009-01-29 08:24:57 +00001437 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1438 llvm::Value *Res =
1439 llvm::UndefValue::get(llvm::PointerType::getUnqual(ConvertType(RetTy)));
1440 return RValue::getAggregate(Res);
1441 }
1442 return RValue::get(llvm::UndefValue::get(ConvertType(RetTy)));
Daniel Dunbar11434922009-01-26 21:26:08 +00001443
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001444 case ABIArgInfo::Coerce: {
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001445 // FIXME: Avoid the conversion through memory if possible.
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +00001446 llvm::Value *V = CreateTempAlloca(ConvertType(RetTy), "coerce");
1447 CreateCoercedStore(CI, V, *this);
Anders Carlssonad3d6912008-11-25 22:21:48 +00001448 if (RetTy->isAnyComplexType())
1449 return RValue::getComplex(LoadComplexFromAddr(V, false));
Daniel Dunbar11434922009-01-26 21:26:08 +00001450 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssonad3d6912008-11-25 22:21:48 +00001451 return RValue::getAggregate(V);
Daniel Dunbar11434922009-01-26 21:26:08 +00001452 else
1453 return RValue::get(Builder.CreateLoad(V));
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001454 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001455
1456 case ABIArgInfo::ByVal:
1457 case ABIArgInfo::Expand:
1458 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001459 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001460
1461 assert(0 && "Unhandled ABIArgInfo::Kind");
1462 return RValue::get(0);
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001463}