blob: ab775131886744823414151867c33daffc21af13 [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;
Daniel Dunbar6f7279b2009-02-04 23:24:38 +0000122 case Ignore:
123 fprintf(stderr, "Ignore");
124 break;
125 case Coerce:
126 fprintf(stderr, "Coerce Type=");
127 getCoerceToType()->print(llvm::errs());
128 // FIXME: This is ridiculous.
129 llvm::errs().flush();
130 break;
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000131 case Indirect:
132 fprintf(stderr, "Indirect Align=%d", getIndirectAlign());
Daniel Dunbar6f7279b2009-02-04 23:24:38 +0000133 break;
134 case Expand:
135 fprintf(stderr, "Expand");
136 break;
137 }
138 fprintf(stderr, ")\n");
139}
140
141/***/
142
Daniel Dunbar834af452008-09-17 21:22:33 +0000143/// isEmptyStruct - Return true iff a structure has no non-empty
144/// members. Note that a structure with a flexible array member is not
145/// considered empty.
146static bool isEmptyStruct(QualType T) {
147 const RecordType *RT = T->getAsStructureType();
148 if (!RT)
149 return 0;
150 const RecordDecl *RD = RT->getDecl();
151 if (RD->hasFlexibleArrayMember())
152 return false;
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000153 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar834af452008-09-17 21:22:33 +0000154 e = RD->field_end(); i != e; ++i) {
155 const FieldDecl *FD = *i;
156 if (!isEmptyStruct(FD->getType()))
157 return false;
158 }
159 return true;
160}
161
162/// isSingleElementStruct - Determine if a structure is a "single
163/// element struct", i.e. it has exactly one non-empty field or
164/// exactly one field which is itself a single element
165/// struct. Structures with flexible array members are never
166/// considered single element structs.
167///
168/// \return The field declaration for the single non-empty field, if
169/// it exists.
170static const FieldDecl *isSingleElementStruct(QualType T) {
171 const RecordType *RT = T->getAsStructureType();
172 if (!RT)
173 return 0;
174
175 const RecordDecl *RD = RT->getDecl();
176 if (RD->hasFlexibleArrayMember())
177 return 0;
178
179 const FieldDecl *Found = 0;
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000180 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar834af452008-09-17 21:22:33 +0000181 e = RD->field_end(); i != e; ++i) {
182 const FieldDecl *FD = *i;
183 QualType FT = FD->getType();
184
185 if (isEmptyStruct(FT)) {
186 // Ignore
187 } else if (Found) {
188 return 0;
189 } else if (!CodeGenFunction::hasAggregateLLVMType(FT)) {
190 Found = FD;
191 } else {
192 Found = isSingleElementStruct(FT);
193 if (!Found)
194 return 0;
195 }
196 }
197
198 return Found;
199}
200
201static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
202 if (!Ty->getAsBuiltinType() && !Ty->isPointerType())
203 return false;
204
205 uint64_t Size = Context.getTypeSize(Ty);
206 return Size == 32 || Size == 64;
207}
208
209static bool areAllFields32Or64BitBasicType(const RecordDecl *RD,
210 ASTContext &Context) {
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000211 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar834af452008-09-17 21:22:33 +0000212 e = RD->field_end(); i != e; ++i) {
213 const FieldDecl *FD = *i;
214
215 if (!is32Or64BitBasicType(FD->getType(), Context))
216 return false;
217
218 // If this is a bit-field we need to make sure it is still a
219 // 32-bit or 64-bit type.
220 if (Expr *BW = FD->getBitWidth()) {
221 unsigned Width = BW->getIntegerConstantExprValue(Context).getZExtValue();
222 if (Width <= 16)
223 return false;
224 }
225 }
226 return true;
227}
228
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000229namespace {
230/// DefaultABIInfo - The default implementation for ABI specific
231/// details. This implementation provides information which results in
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000232/// self-consistent and sensible LLVM IR generation, but does not
233/// conform to any particular ABI.
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000234class DefaultABIInfo : public ABIInfo {
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000235 ABIArgInfo classifyReturnType(QualType RetTy,
236 ASTContext &Context) const;
237
238 ABIArgInfo classifyArgumentType(QualType RetTy,
239 ASTContext &Context) const;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000240
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000241 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
242 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
243 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
244 it != ie; ++it)
245 it->info = classifyArgumentType(it->type, Context);
246 }
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000247};
248
249/// X86_32ABIInfo - The X86-32 ABI information.
250class X86_32ABIInfo : public ABIInfo {
251public:
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000252 ABIArgInfo classifyReturnType(QualType RetTy,
253 ASTContext &Context) const;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000254
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000255 ABIArgInfo classifyArgumentType(QualType RetTy,
256 ASTContext &Context) const;
257
258 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
259 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
260 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
261 it != ie; ++it)
262 it->info = classifyArgumentType(it->type, Context);
263 }
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000264};
265}
266
267ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
268 ASTContext &Context) const {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000269 if (RetTy->isVoidType()) {
270 return ABIArgInfo::getIgnore();
271 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar834af452008-09-17 21:22:33 +0000272 // Classify "single element" structs as their element type.
273 const FieldDecl *SeltFD = isSingleElementStruct(RetTy);
274 if (SeltFD) {
275 QualType SeltTy = SeltFD->getType()->getDesugaredType();
276 if (const BuiltinType *BT = SeltTy->getAsBuiltinType()) {
277 // FIXME: This is gross, it would be nice if we could just
278 // pass back SeltTy and have clients deal with it. Is it worth
279 // supporting coerce to both LLVM and clang Types?
280 if (BT->isIntegerType()) {
281 uint64_t Size = Context.getTypeSize(SeltTy);
282 return ABIArgInfo::getCoerce(llvm::IntegerType::get((unsigned) Size));
283 } else if (BT->getKind() == BuiltinType::Float) {
284 return ABIArgInfo::getCoerce(llvm::Type::FloatTy);
285 } else if (BT->getKind() == BuiltinType::Double) {
286 return ABIArgInfo::getCoerce(llvm::Type::DoubleTy);
287 }
288 } else if (SeltTy->isPointerType()) {
289 // FIXME: It would be really nice if this could come out as
290 // the proper pointer type.
291 llvm::Type *PtrTy =
292 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
293 return ABIArgInfo::getCoerce(PtrTy);
294 }
295 }
296
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000297 uint64_t Size = Context.getTypeSize(RetTy);
298 if (Size == 8) {
299 return ABIArgInfo::getCoerce(llvm::Type::Int8Ty);
300 } else if (Size == 16) {
301 return ABIArgInfo::getCoerce(llvm::Type::Int16Ty);
302 } else if (Size == 32) {
303 return ABIArgInfo::getCoerce(llvm::Type::Int32Ty);
304 } else if (Size == 64) {
305 return ABIArgInfo::getCoerce(llvm::Type::Int64Ty);
306 } else {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000307 return ABIArgInfo::getIndirect(0);
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000308 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000309 } else {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000310 return ABIArgInfo::getDirect();
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000311 }
312}
313
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000314ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
315 ASTContext &Context) const {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000316 // FIXME: Set alignment on indirect arguments.
Daniel Dunbarf0357382008-09-17 20:11:04 +0000317 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000318 // Structures with flexible arrays are always indirect.
Daniel Dunbar834af452008-09-17 21:22:33 +0000319 if (const RecordType *RT = Ty->getAsStructureType())
320 if (RT->getDecl()->hasFlexibleArrayMember())
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000321 return ABIArgInfo::getIndirect(0);
Daniel Dunbar834af452008-09-17 21:22:33 +0000322
Daniel Dunbar3170c932009-02-05 01:50:07 +0000323 // Ignore empty structs.
Daniel Dunbar834af452008-09-17 21:22:33 +0000324 uint64_t Size = Context.getTypeSize(Ty);
325 if (Ty->isStructureType() && Size == 0)
Daniel Dunbar3170c932009-02-05 01:50:07 +0000326 return ABIArgInfo::getIgnore();
Daniel Dunbar834af452008-09-17 21:22:33 +0000327
328 // Expand structs with size <= 128-bits which consist only of
329 // basic types (int, long long, float, double, xxx*). This is
330 // non-recursive and does not ignore empty fields.
331 if (const RecordType *RT = Ty->getAsStructureType()) {
332 if (Context.getTypeSize(Ty) <= 4*32 &&
333 areAllFields32Or64BitBasicType(RT->getDecl(), Context))
334 return ABIArgInfo::getExpand();
335 }
336
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000337 return ABIArgInfo::getIndirect(0);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000338 } else {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000339 return ABIArgInfo::getDirect();
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000340 }
341}
342
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000343namespace {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000344/// X86_64ABIInfo - The X86_64 ABI information.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000345class X86_64ABIInfo : public ABIInfo {
346 enum Class {
347 Integer = 0,
348 SSE,
349 SSEUp,
350 X87,
351 X87Up,
352 ComplexX87,
353 NoClass,
354 Memory
355 };
356
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000357 /// merge - Implement the X86_64 ABI merging algorithm.
358 ///
Daniel Dunbarc4503572009-01-31 00:06:58 +0000359 /// Merge an accumulating classification \arg Accum with a field
360 /// classification \arg Field.
361 ///
362 /// \param Accum - The accumulating classification. This should
363 /// always be either NoClass or the result of a previous merge
364 /// call. In addition, this should never be Memory (the caller
365 /// should just return Memory for the aggregate).
366 Class merge(Class Accum, Class Field) const;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000367
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000368 /// classify - Determine the x86_64 register classes in which the
369 /// given type T should be passed.
370 ///
Daniel Dunbarc4503572009-01-31 00:06:58 +0000371 /// \param Lo - The classification for the parts of the type
372 /// residing in the low word of the containing object.
373 ///
374 /// \param Hi - The classification for the parts of the type
375 /// residing in the high word of the containing object.
376 ///
377 /// \param OffsetBase - The bit offset of this type in the
Daniel Dunbarcdf920e2009-01-30 22:40:15 +0000378 /// containing object. Some parameters are classified different
379 /// depending on whether they straddle an eightbyte boundary.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000380 ///
381 /// If a word is unused its result will be NoClass; if a type should
382 /// be passed in Memory then at least the classification of \arg Lo
383 /// will be Memory.
384 ///
385 /// The \arg Lo class will be NoClass iff the argument is ignored.
386 ///
387 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
388 /// be NoClass.
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000389 void classify(QualType T, ASTContext &Context, uint64_t OffsetBase,
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000390 Class &Lo, Class &Hi) const;
Daniel Dunbarc4503572009-01-31 00:06:58 +0000391
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000392 ABIArgInfo classifyReturnType(QualType RetTy,
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000393 ASTContext &Context) const;
394
395 ABIArgInfo classifyArgumentType(QualType Ty,
396 ASTContext &Context,
397 unsigned &freeIntRegs,
398 unsigned &freeSSERegs) const;
399
400public:
401 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000402};
403}
404
Daniel Dunbarc4503572009-01-31 00:06:58 +0000405X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum,
406 Class Field) const {
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000407 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
408 // classified recursively so that always two fields are
409 // considered. The resulting class is calculated according to
410 // the classes of the fields in the eightbyte:
411 //
412 // (a) If both classes are equal, this is the resulting class.
413 //
414 // (b) If one of the classes is NO_CLASS, the resulting class is
415 // the other class.
416 //
417 // (c) If one of the classes is MEMORY, the result is the MEMORY
418 // class.
419 //
420 // (d) If one of the classes is INTEGER, the result is the
421 // INTEGER.
422 //
423 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
424 // MEMORY is used as class.
425 //
426 // (f) Otherwise class SSE is used.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000427 assert((Accum == NoClass || Accum == Integer ||
428 Accum == SSE || Accum == SSEUp) &&
429 "Invalid accumulated classification during merge.");
430 if (Accum == Field || Field == NoClass)
431 return Accum;
432 else if (Field == Memory)
433 return Memory;
434 else if (Accum == NoClass)
435 return Field;
436 else if (Accum == Integer || Field == Integer)
437 return Integer;
438 else if (Field == X87 || Field == X87Up || Field == ComplexX87)
439 return Memory;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000440 else
Daniel Dunbarc4503572009-01-31 00:06:58 +0000441 return SSE;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000442}
443
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000444void X86_64ABIInfo::classify(QualType Ty,
445 ASTContext &Context,
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000446 uint64_t OffsetBase,
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000447 Class &Lo, Class &Hi) const {
Daniel Dunbar9a82b522009-02-02 18:06:39 +0000448 // FIXME: This code can be simplified by introducing a simple value
449 // class for Class pairs with appropriate constructor methods for
450 // the various situations.
451
Daniel Dunbarc4503572009-01-31 00:06:58 +0000452 Lo = Hi = NoClass;
453
454 Class &Current = OffsetBase < 64 ? Lo : Hi;
455 Current = Memory;
456
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000457 if (const BuiltinType *BT = Ty->getAsBuiltinType()) {
458 BuiltinType::Kind k = BT->getKind();
459
Daniel Dunbar11434922009-01-26 21:26:08 +0000460 if (k == BuiltinType::Void) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000461 Current = NoClass;
Daniel Dunbar11434922009-01-26 21:26:08 +0000462 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000463 Current = Integer;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000464 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000465 Current = SSE;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000466 } else if (k == BuiltinType::LongDouble) {
467 Lo = X87;
468 Hi = X87Up;
469 }
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000470 // FIXME: _Decimal32 and _Decimal64 are SSE.
471 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000472 // FIXME: __int128 is (Integer, Integer).
473 } else if (Ty->isPointerLikeType() || Ty->isBlockPointerType() ||
474 Ty->isObjCQualifiedInterfaceType()) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000475 Current = Integer;
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000476 } else if (const VectorType *VT = Ty->getAsVectorType()) {
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000477 uint64_t Size = Context.getTypeSize(VT);
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000478 if (Size == 64) {
Daniel Dunbard4cd1b02009-01-30 19:38:39 +0000479 // gcc passes <1 x double> in memory.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000480 if (VT->getElementType() == Context.DoubleTy)
Daniel Dunbard4cd1b02009-01-30 19:38:39 +0000481 return;
Daniel Dunbard4cd1b02009-01-30 19:38:39 +0000482
Daniel Dunbarc4503572009-01-31 00:06:58 +0000483 Current = SSE;
Daniel Dunbare33edf12009-01-30 18:40:10 +0000484
485 // If this type crosses an eightbyte boundary, it should be
486 // split.
Daniel Dunbarcdf920e2009-01-30 22:40:15 +0000487 if (OffsetBase && OffsetBase != 64)
Daniel Dunbare33edf12009-01-30 18:40:10 +0000488 Hi = Lo;
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000489 } else if (Size == 128) {
490 Lo = SSE;
491 Hi = SSEUp;
492 }
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000493 } else if (const ComplexType *CT = Ty->getAsComplexType()) {
494 QualType ET = CT->getElementType();
495
Daniel Dunbare33edf12009-01-30 18:40:10 +0000496 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbareac48dc2009-01-29 07:22:20 +0000497 if (ET->isIntegerType()) {
Daniel Dunbareac48dc2009-01-29 07:22:20 +0000498 if (Size <= 64)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000499 Current = Integer;
Daniel Dunbareac48dc2009-01-29 07:22:20 +0000500 else if (Size <= 128)
501 Lo = Hi = Integer;
502 } else if (ET == Context.FloatTy)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000503 Current = SSE;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000504 else if (ET == Context.DoubleTy)
505 Lo = Hi = SSE;
506 else if (ET == Context.LongDoubleTy)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000507 Current = ComplexX87;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000508
509 // If this complex type crosses an eightbyte boundary then it
510 // should be split.
Daniel Dunbarcdf920e2009-01-30 22:40:15 +0000511 uint64_t EB_Real = (OffsetBase) / 64;
512 uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000513 if (Hi == NoClass && EB_Real != EB_Imag)
514 Hi = Lo;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000515 } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
516 // Arrays are treated like structures.
517
518 uint64_t Size = Context.getTypeSize(Ty);
519
520 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
521 // than two eightbytes, ..., it has class MEMORY.
522 if (Size > 128)
523 return;
524
525 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
526 // fields, it has class MEMORY.
527 //
528 // Only need to check alignment of array base.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000529 if (OffsetBase % Context.getTypeAlign(AT->getElementType()))
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000530 return;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000531
532 // Otherwise implement simplified merge. We could be smarter about
533 // this, but it isn't worth it and would be harder to verify.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000534 Current = NoClass;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000535 uint64_t EltSize = Context.getTypeSize(AT->getElementType());
536 uint64_t ArraySize = AT->getSize().getZExtValue();
537 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
538 Class FieldLo, FieldHi;
539 classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbarc4503572009-01-31 00:06:58 +0000540 Lo = merge(Lo, FieldLo);
541 Hi = merge(Hi, FieldHi);
542 if (Lo == Memory || Hi == Memory)
543 break;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000544 }
Daniel Dunbarc4503572009-01-31 00:06:58 +0000545
546 // Do post merger cleanup (see below). Only case we worry about is Memory.
547 if (Hi == Memory)
548 Lo = Memory;
549 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Daniel Dunbar99037e52009-01-29 08:13:58 +0000550 } else if (const RecordType *RT = Ty->getAsRecordType()) {
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000551 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbar99037e52009-01-29 08:13:58 +0000552
553 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
554 // than two eightbytes, ..., it has class MEMORY.
555 if (Size > 128)
556 return;
557
558 const RecordDecl *RD = RT->getDecl();
559
560 // Assume variable sized types are passed in memory.
561 if (RD->hasFlexibleArrayMember())
562 return;
563
564 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
565
566 // Reset Lo class, this will be recomputed.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000567 Current = NoClass;
Daniel Dunbar99037e52009-01-29 08:13:58 +0000568 unsigned idx = 0;
569 for (RecordDecl::field_iterator i = RD->field_begin(),
570 e = RD->field_end(); i != e; ++i, ++idx) {
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000571 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Daniel Dunbar99037e52009-01-29 08:13:58 +0000572
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000573 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
574 // fields, it has class MEMORY.
Daniel Dunbar99037e52009-01-29 08:13:58 +0000575 if (Offset % Context.getTypeAlign(i->getType())) {
576 Lo = Memory;
577 return;
578 }
579
Daniel Dunbar99037e52009-01-29 08:13:58 +0000580 // Classify this field.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000581 //
582 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
583 // exceeds a single eightbyte, each is classified
584 // separately. Each eightbyte gets initialized to class
585 // NO_CLASS.
Daniel Dunbar99037e52009-01-29 08:13:58 +0000586 Class FieldLo, FieldHi;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000587 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbarc4503572009-01-31 00:06:58 +0000588 Lo = merge(Lo, FieldLo);
589 Hi = merge(Hi, FieldHi);
590 if (Lo == Memory || Hi == Memory)
591 break;
Daniel Dunbar99037e52009-01-29 08:13:58 +0000592 }
593
594 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
595 //
596 // (a) If one of the classes is MEMORY, the whole argument is
597 // passed in memory.
598 //
599 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
600
601 // The first of these conditions is guaranteed by how we implement
Daniel Dunbarc4503572009-01-31 00:06:58 +0000602 // the merge (just bail).
603 //
604 // The second condition occurs in the case of unions; for example
605 // union { _Complex double; unsigned; }.
606 if (Hi == Memory)
607 Lo = Memory;
Daniel Dunbar99037e52009-01-29 08:13:58 +0000608 if (Hi == SSEUp && Lo != SSE)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000609 Hi = SSE;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000610 }
611}
612
Daniel Dunbarc4503572009-01-31 00:06:58 +0000613
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000614ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
615 ASTContext &Context) const {
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000616 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
617 // classification algorithm.
618 X86_64ABIInfo::Class Lo, Hi;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000619 classify(RetTy, Context, 0, Lo, Hi);
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000620
Daniel Dunbarc4503572009-01-31 00:06:58 +0000621 // Check some invariants.
622 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
623 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
624 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
625
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000626 const llvm::Type *ResType = 0;
627 switch (Lo) {
628 case NoClass:
Daniel Dunbar11434922009-01-26 21:26:08 +0000629 return ABIArgInfo::getIgnore();
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000630
631 case SSEUp:
632 case X87Up:
633 assert(0 && "Invalid classification for lo word.");
634
Daniel Dunbarc4503572009-01-31 00:06:58 +0000635 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000636 // hidden argument.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000637 case Memory:
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000638 return ABIArgInfo::getIndirect(0);
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000639
640 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
641 // available register of the sequence %rax, %rdx is used.
642 case Integer:
643 ResType = llvm::Type::Int64Ty; break;
644
645 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
646 // available SSE register of the sequence %xmm0, %xmm1 is used.
647 case SSE:
648 ResType = llvm::Type::DoubleTy; break;
649
650 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
651 // returned on the X87 stack in %st0 as 80-bit x87 number.
652 case X87:
653 ResType = llvm::Type::X86_FP80Ty; break;
654
Daniel Dunbarc4503572009-01-31 00:06:58 +0000655 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
656 // part of the value is returned in %st0 and the imaginary part in
657 // %st1.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000658 case ComplexX87:
659 assert(Hi == NoClass && "Unexpected ComplexX87 classification.");
660 ResType = llvm::VectorType::get(llvm::Type::X86_FP80Ty, 2);
661 break;
662 }
663
664 switch (Hi) {
665 // Memory was handled previously, and ComplexX87 and X87 should
666 // never occur as hi classes.
667 case Memory:
668 case X87:
669 case ComplexX87:
670 assert(0 && "Invalid classification for hi word.");
671
672 case NoClass: break;
673 case Integer:
Daniel Dunbarb0e14f22009-01-29 07:36:07 +0000674 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
675 break;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000676 case SSE:
Daniel Dunbarb0e14f22009-01-29 07:36:07 +0000677 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
678 break;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000679
680 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
681 // is passed in the upper half of the last used SSE register.
682 //
683 // SSEUP should always be preceeded by SSE, just widen.
684 case SSEUp:
685 assert(Lo == SSE && "Unexpected SSEUp classification.");
686 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
687 break;
688
689 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
Daniel Dunbarb0e14f22009-01-29 07:36:07 +0000690 // returned together with the previous X87 value in %st0.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000691 //
692 // X87UP should always be preceeded by X87, so we don't need to do
693 // anything here.
694 case X87Up:
695 assert(Lo == X87 && "Unexpected X87Up classification.");
696 break;
697 }
698
699 return ABIArgInfo::getCoerce(ResType);
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000700}
701
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000702ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, ASTContext &Context,
703 unsigned &freeIntRegs,
704 unsigned &freeSSERegs) const {
705 X86_64ABIInfo::Class Lo, Hi;
706 classify(Ty, Context, 0, Lo, Hi);
707
708 // Check some invariants.
709 // FIXME: Enforce these by construction.
710 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
711 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
712 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
713
714 unsigned neededInt = 0, neededSSE = 0;
715 const llvm::Type *ResType = 0;
716 switch (Lo) {
717 case NoClass:
718 return ABIArgInfo::getIgnore();
719
720 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
721 // on the stack.
722 case Memory:
723
724 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
725 // COMPLEX_X87, it is passed in memory.
726 case X87:
727 case ComplexX87:
728 // Choose appropriate in memory type.
729 if (CodeGenFunction::hasAggregateLLVMType(Ty))
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000730 return ABIArgInfo::getIndirect(0);
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000731 else
732 return ABIArgInfo::getDirect();
733
734 case SSEUp:
735 case X87Up:
736 assert(0 && "Invalid classification for lo word.");
737
738 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
739 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
740 // and %r9 is used.
741 case Integer:
742 ++neededInt;
743 ResType = llvm::Type::Int64Ty;
744 break;
745
746 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
747 // available SSE register is used, the registers are taken in the
748 // order from %xmm0 to %xmm7.
749 case SSE:
750 ++neededSSE;
751 ResType = llvm::Type::DoubleTy;
752 break;
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000753 }
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000754
755 switch (Hi) {
756 // Memory was handled previously, ComplexX87 and X87 should
757 // never occur as hi classes, and X87Up must be preceed by X87,
758 // which is passed in memory.
759 case Memory:
760 case X87:
761 case X87Up:
762 case ComplexX87:
763 assert(0 && "Invalid classification for hi word.");
764
765 case NoClass: break;
766 case Integer:
767 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
768 ++neededInt;
769 break;
770 case SSE:
771 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
772 ++neededSSE;
773 break;
774
775 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
776 // eightbyte is passed in the upper half of the last used SSE
777 // register.
778 case SSEUp:
779 assert(Lo == SSE && "Unexpected SSEUp classification.");
780 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
781 break;
782 }
783
784 // AMD64-ABI 3.2.3p3: If there are no registers available for any
785 // eightbyte of an argument, the whole argument is passed on the
786 // stack. If registers have already been assigned for some
787 // eightbytes of such an argument, the assignments get reverted.
788 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
789 freeIntRegs -= neededInt;
790 freeSSERegs -= neededSSE;
791 return ABIArgInfo::getCoerce(ResType);
792 } else {
793 // Choose appropriate in memory type.
794 if (CodeGenFunction::hasAggregateLLVMType(Ty))
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000795 return ABIArgInfo::getIndirect(0);
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000796 else
797 return ABIArgInfo::getDirect();
798 }
799}
800
801void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
802 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
803
804 // Keep track of the number of assigned registers.
805 unsigned freeIntRegs = 6, freeSSERegs = 8;
806
807 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
808 // get assigned (in left-to-right order) for passing as follows...
809 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
810 it != ie; ++it)
811 it->info = classifyArgumentType(it->type, Context, freeIntRegs, freeSSERegs);
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000812}
813
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000814ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
815 ASTContext &Context) const {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000816 if (RetTy->isVoidType()) {
817 return ABIArgInfo::getIgnore();
818 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000819 return ABIArgInfo::getIndirect(0);
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000820 } else {
821 return ABIArgInfo::getDirect();
822 }
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000823}
824
825ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
826 ASTContext &Context) const {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000827 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000828 return ABIArgInfo::getIndirect(0);
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000829 } else {
830 return ABIArgInfo::getDirect();
831 }
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000832}
833
834const ABIInfo &CodeGenTypes::getABIInfo() const {
835 if (TheABIInfo)
836 return *TheABIInfo;
837
838 // For now we just cache this in the CodeGenTypes and don't bother
839 // to free it.
840 const char *TargetPrefix = getContext().Target.getTargetPrefix();
841 if (strcmp(TargetPrefix, "x86") == 0) {
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000842 switch (getContext().Target.getPointerWidth(0)) {
843 case 32:
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000844 return *(TheABIInfo = new X86_32ABIInfo());
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000845 case 64:
Daniel Dunbar11a76ed2009-01-30 18:47:53 +0000846 return *(TheABIInfo = new X86_64ABIInfo());
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000847 }
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000848 }
849
850 return *(TheABIInfo = new DefaultABIInfo);
851}
852
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000853/***/
854
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000855CGFunctionInfo::CGFunctionInfo(QualType ResTy,
856 const llvm::SmallVector<QualType, 16> &ArgTys) {
857 NumArgs = ArgTys.size();
858 Args = new ArgInfo[1 + NumArgs];
859 Args[0].type = ResTy;
860 for (unsigned i = 0; i < NumArgs; ++i)
861 Args[1 + i].type = ArgTys[i];
862}
863
864/***/
865
Daniel Dunbar56273772008-09-17 00:51:38 +0000866void CodeGenTypes::GetExpandedTypes(QualType Ty,
867 std::vector<const llvm::Type*> &ArgTys) {
868 const RecordType *RT = Ty->getAsStructureType();
869 assert(RT && "Can only expand structure types.");
870 const RecordDecl *RD = RT->getDecl();
871 assert(!RD->hasFlexibleArrayMember() &&
872 "Cannot expand structure with flexible array.");
873
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000874 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar56273772008-09-17 00:51:38 +0000875 e = RD->field_end(); i != e; ++i) {
876 const FieldDecl *FD = *i;
877 assert(!FD->isBitField() &&
878 "Cannot expand structure with bit-field members.");
879
880 QualType FT = FD->getType();
881 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
882 GetExpandedTypes(FT, ArgTys);
883 } else {
884 ArgTys.push_back(ConvertType(FT));
885 }
886 }
887}
888
889llvm::Function::arg_iterator
890CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
891 llvm::Function::arg_iterator AI) {
892 const RecordType *RT = Ty->getAsStructureType();
893 assert(RT && "Can only expand structure types.");
894
895 RecordDecl *RD = RT->getDecl();
896 assert(LV.isSimple() &&
897 "Unexpected non-simple lvalue during struct expansion.");
898 llvm::Value *Addr = LV.getAddress();
899 for (RecordDecl::field_iterator i = RD->field_begin(),
900 e = RD->field_end(); i != e; ++i) {
901 FieldDecl *FD = *i;
902 QualType FT = FD->getType();
903
904 // FIXME: What are the right qualifiers here?
905 LValue LV = EmitLValueForField(Addr, FD, false, 0);
906 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
907 AI = ExpandTypeFromArgs(FT, LV, AI);
908 } else {
909 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
910 ++AI;
911 }
912 }
913
914 return AI;
915}
916
917void
918CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
919 llvm::SmallVector<llvm::Value*, 16> &Args) {
920 const RecordType *RT = Ty->getAsStructureType();
921 assert(RT && "Can only expand structure types.");
922
923 RecordDecl *RD = RT->getDecl();
924 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
925 llvm::Value *Addr = RV.getAggregateAddr();
926 for (RecordDecl::field_iterator i = RD->field_begin(),
927 e = RD->field_end(); i != e; ++i) {
928 FieldDecl *FD = *i;
929 QualType FT = FD->getType();
930
931 // FIXME: What are the right qualifiers here?
932 LValue LV = EmitLValueForField(Addr, FD, false, 0);
933 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
934 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
935 } else {
936 RValue RV = EmitLoadOfLValue(LV, FT);
937 assert(RV.isScalar() &&
938 "Unexpected non-scalar rvalue during struct expansion.");
939 Args.push_back(RV.getScalarVal());
940 }
941 }
942}
943
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000944/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
945/// a pointer to an object of type \arg Ty.
946///
947/// This safely handles the case when the src type is smaller than the
948/// destination type; in this situation the values of bits which not
949/// present in the src are undefined.
950static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
951 const llvm::Type *Ty,
952 CodeGenFunction &CGF) {
953 const llvm::Type *SrcTy =
954 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
955 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
956 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(Ty);
957
Daniel Dunbarb225be42009-02-03 05:59:18 +0000958 // If load is legal, just bitcast the src pointer.
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000959 if (SrcSize == DstSize) {
960 llvm::Value *Casted =
961 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
962 return CGF.Builder.CreateLoad(Casted);
963 } else {
964 assert(SrcSize < DstSize && "Coercion is losing source bits!");
965
966 // Otherwise do coercion through memory. This is stupid, but
967 // simple.
968 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
969 llvm::Value *Casted =
970 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
971 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
972 return CGF.Builder.CreateLoad(Tmp);
973 }
974}
975
976/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
977/// where the source and destination may have different types.
978///
979/// This safely handles the case when the src type is larger than the
980/// destination type; the upper bits of the src will be lost.
981static void CreateCoercedStore(llvm::Value *Src,
982 llvm::Value *DstPtr,
983 CodeGenFunction &CGF) {
984 const llvm::Type *SrcTy = Src->getType();
985 const llvm::Type *DstTy =
986 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
987
988 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
989 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(DstTy);
990
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000991 // If store is legal, just bitcast the src pointer.
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000992 if (SrcSize == DstSize) {
993 llvm::Value *Casted =
994 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
995 CGF.Builder.CreateStore(Src, Casted);
996 } else {
997 assert(SrcSize > DstSize && "Coercion is missing bits!");
998
999 // Otherwise do coercion through memory. This is stupid, but
1000 // simple.
1001 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
1002 CGF.Builder.CreateStore(Src, Tmp);
1003 llvm::Value *Casted =
1004 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
1005 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(Casted), DstPtr);
1006 }
1007}
1008
Daniel Dunbar56273772008-09-17 00:51:38 +00001009/***/
1010
Daniel Dunbar88b53962009-02-02 22:03:45 +00001011bool CodeGenModule::ReturnTypeUsesSret(const CGFunctionInfo &FI) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001012 return FI.getReturnInfo().isIndirect();
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001013}
1014
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001015const llvm::FunctionType *
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001016CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic) {
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001017 std::vector<const llvm::Type*> ArgTys;
1018
1019 const llvm::Type *ResultType = 0;
1020
Daniel Dunbara0a99e02009-02-02 23:43:58 +00001021 QualType RetTy = FI.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001022 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001023 switch (RetAI.getKind()) {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001024 case ABIArgInfo::Expand:
1025 assert(0 && "Invalid ABI kind for return argument");
1026
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001027 case ABIArgInfo::Direct:
1028 ResultType = ConvertType(RetTy);
1029 break;
1030
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001031 case ABIArgInfo::Indirect: {
1032 assert(!RetAI.getIndirectAlign() && "Align unused on indirect return.");
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001033 ResultType = llvm::Type::VoidTy;
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +00001034 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001035 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
1036 break;
1037 }
1038
Daniel Dunbar11434922009-01-26 21:26:08 +00001039 case ABIArgInfo::Ignore:
1040 ResultType = llvm::Type::VoidTy;
1041 break;
1042
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001043 case ABIArgInfo::Coerce:
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001044 ResultType = RetAI.getCoerceToType();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001045 break;
1046 }
1047
Daniel Dunbar88c2fa92009-02-03 05:31:23 +00001048 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1049 ie = FI.arg_end(); it != ie; ++it) {
1050 const ABIArgInfo &AI = it->info;
1051 const llvm::Type *Ty = ConvertType(it->type);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001052
1053 switch (AI.getKind()) {
Daniel Dunbar11434922009-01-26 21:26:08 +00001054 case ABIArgInfo::Ignore:
1055 break;
1056
Daniel Dunbar56273772008-09-17 00:51:38 +00001057 case ABIArgInfo::Coerce:
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001058 ArgTys.push_back(AI.getCoerceToType());
1059 break;
1060
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001061 case ABIArgInfo::Indirect:
1062 // indirect arguments are always on the stack, which is addr space #0.
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001063 ArgTys.push_back(llvm::PointerType::getUnqual(Ty));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001064 break;
1065
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001066 case ABIArgInfo::Direct:
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001067 ArgTys.push_back(Ty);
1068 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001069
1070 case ABIArgInfo::Expand:
Daniel Dunbar88c2fa92009-02-03 05:31:23 +00001071 GetExpandedTypes(it->type, ArgTys);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001072 break;
1073 }
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001074 }
1075
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001076 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar3913f182008-09-09 23:48:28 +00001077}
1078
Daniel Dunbara0a99e02009-02-02 23:43:58 +00001079void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
Daniel Dunbar88b53962009-02-02 22:03:45 +00001080 const Decl *TargetDecl,
Devang Patel761d7f72008-09-25 21:02:23 +00001081 AttributeListType &PAL) {
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001082 unsigned FuncAttrs = 0;
Devang Patela2c69122008-09-26 22:53:57 +00001083 unsigned RetAttrs = 0;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001084
1085 if (TargetDecl) {
1086 if (TargetDecl->getAttr<NoThrowAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +00001087 FuncAttrs |= llvm::Attribute::NoUnwind;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001088 if (TargetDecl->getAttr<NoReturnAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +00001089 FuncAttrs |= llvm::Attribute::NoReturn;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001090 if (TargetDecl->getAttr<PureAttr>())
1091 FuncAttrs |= llvm::Attribute::ReadOnly;
1092 if (TargetDecl->getAttr<ConstAttr>())
1093 FuncAttrs |= llvm::Attribute::ReadNone;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001094 }
1095
Daniel Dunbara0a99e02009-02-02 23:43:58 +00001096 QualType RetTy = FI.getReturnType();
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001097 unsigned Index = 1;
Daniel Dunbarb225be42009-02-03 05:59:18 +00001098 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001099 switch (RetAI.getKind()) {
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001100 case ABIArgInfo::Direct:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001101 if (RetTy->isPromotableIntegerType()) {
1102 if (RetTy->isSignedIntegerType()) {
Devang Patela2c69122008-09-26 22:53:57 +00001103 RetAttrs |= llvm::Attribute::SExt;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001104 } else if (RetTy->isUnsignedIntegerType()) {
Devang Patela2c69122008-09-26 22:53:57 +00001105 RetAttrs |= llvm::Attribute::ZExt;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001106 }
1107 }
1108 break;
1109
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001110 case ABIArgInfo::Indirect:
Devang Patel761d7f72008-09-25 21:02:23 +00001111 PAL.push_back(llvm::AttributeWithIndex::get(Index,
Daniel Dunbar725ad312009-01-31 02:19:00 +00001112 llvm::Attribute::StructRet |
1113 llvm::Attribute::NoAlias));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001114 ++Index;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001115 break;
1116
Daniel Dunbar11434922009-01-26 21:26:08 +00001117 case ABIArgInfo::Ignore:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001118 case ABIArgInfo::Coerce:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001119 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001120
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001121 case ABIArgInfo::Expand:
1122 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001123 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001124
Devang Patela2c69122008-09-26 22:53:57 +00001125 if (RetAttrs)
1126 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Daniel Dunbar88c2fa92009-02-03 05:31:23 +00001127 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1128 ie = FI.arg_end(); it != ie; ++it) {
1129 QualType ParamType = it->type;
1130 const ABIArgInfo &AI = it->info;
Devang Patel761d7f72008-09-25 21:02:23 +00001131 unsigned Attributes = 0;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001132
1133 switch (AI.getKind()) {
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001134 case ABIArgInfo::Coerce:
1135 break;
1136
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001137 case ABIArgInfo::Indirect:
Devang Patel761d7f72008-09-25 21:02:23 +00001138 Attributes |= llvm::Attribute::ByVal;
Daniel Dunbarca008822009-02-05 01:31:19 +00001139 Attributes |=
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001140 llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign());
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001141 break;
1142
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001143 case ABIArgInfo::Direct:
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001144 if (ParamType->isPromotableIntegerType()) {
1145 if (ParamType->isSignedIntegerType()) {
Devang Patel761d7f72008-09-25 21:02:23 +00001146 Attributes |= llvm::Attribute::SExt;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001147 } else if (ParamType->isUnsignedIntegerType()) {
Devang Patel761d7f72008-09-25 21:02:23 +00001148 Attributes |= llvm::Attribute::ZExt;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001149 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001150 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001151 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001152
Daniel Dunbar11434922009-01-26 21:26:08 +00001153 case ABIArgInfo::Ignore:
1154 // Skip increment, no matching LLVM parameter.
1155 continue;
1156
Daniel Dunbar56273772008-09-17 00:51:38 +00001157 case ABIArgInfo::Expand: {
1158 std::vector<const llvm::Type*> Tys;
1159 // FIXME: This is rather inefficient. Do we ever actually need
1160 // to do anything here? The result should be just reconstructed
1161 // on the other side, so extension should be a non-issue.
1162 getTypes().GetExpandedTypes(ParamType, Tys);
1163 Index += Tys.size();
1164 continue;
1165 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001166 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001167
Devang Patel761d7f72008-09-25 21:02:23 +00001168 if (Attributes)
1169 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar56273772008-09-17 00:51:38 +00001170 ++Index;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001171 }
Devang Patela2c69122008-09-26 22:53:57 +00001172 if (FuncAttrs)
1173 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
1174
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001175}
1176
Daniel Dunbar88b53962009-02-02 22:03:45 +00001177void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
1178 llvm::Function *Fn,
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001179 const FunctionArgList &Args) {
Daniel Dunbar5251afa2009-02-03 06:02:10 +00001180 // FIXME: We no longer need the types from FunctionArgList; lift up
1181 // and simplify.
1182
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001183 // Emit allocs for param decls. Give the LLVM Argument nodes names.
1184 llvm::Function::arg_iterator AI = Fn->arg_begin();
1185
1186 // Name the struct return argument.
Daniel Dunbar88b53962009-02-02 22:03:45 +00001187 if (CGM.ReturnTypeUsesSret(FI)) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001188 AI->setName("agg.result");
1189 ++AI;
1190 }
Daniel Dunbarb225be42009-02-03 05:59:18 +00001191
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +00001192 assert(FI.arg_size() == Args.size() &&
1193 "Mismatch between function signature & arguments.");
Daniel Dunbarb225be42009-02-03 05:59:18 +00001194 CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001195 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001196 i != e; ++i, ++info_it) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001197 const VarDecl *Arg = i->first;
Daniel Dunbarb225be42009-02-03 05:59:18 +00001198 QualType Ty = info_it->type;
1199 const ABIArgInfo &ArgI = info_it->info;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001200
1201 switch (ArgI.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001202 // FIXME: Implement correct [in]direct semantics.
1203 case ABIArgInfo::Indirect:
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001204 case ABIArgInfo::Direct: {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001205 assert(AI != Fn->arg_end() && "Argument mismatch!");
1206 llvm::Value* V = AI;
Daniel Dunbar56273772008-09-17 00:51:38 +00001207 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001208 // This must be a promotion, for something like
1209 // "void a(x) short x; {..."
Daniel Dunbar56273772008-09-17 00:51:38 +00001210 V = EmitScalarConversion(V, Ty, Arg->getType());
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001211 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001212 EmitParmDecl(*Arg, V);
1213 break;
1214 }
Daniel Dunbar56273772008-09-17 00:51:38 +00001215
1216 case ABIArgInfo::Expand: {
Daniel Dunbarb225be42009-02-03 05:59:18 +00001217 // If this structure was expanded into multiple arguments then
Daniel Dunbar56273772008-09-17 00:51:38 +00001218 // we need to create a temporary and reconstruct it from the
1219 // arguments.
Chris Lattner39f34e92008-11-24 04:00:27 +00001220 std::string Name = Arg->getNameAsString();
Daniel Dunbar56273772008-09-17 00:51:38 +00001221 llvm::Value *Temp = CreateTempAlloca(ConvertType(Ty),
1222 (Name + ".addr").c_str());
1223 // FIXME: What are the right qualifiers here?
1224 llvm::Function::arg_iterator End =
1225 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI);
1226 EmitParmDecl(*Arg, Temp);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001227
Daniel Dunbar56273772008-09-17 00:51:38 +00001228 // Name the arguments used in expansion and increment AI.
1229 unsigned Index = 0;
1230 for (; AI != End; ++AI, ++Index)
1231 AI->setName(Name + "." + llvm::utostr(Index));
1232 continue;
1233 }
Daniel Dunbar11434922009-01-26 21:26:08 +00001234
1235 case ABIArgInfo::Ignore:
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +00001236 // Skip increment, no matching LLVM parameter.
1237 continue;
Daniel Dunbar11434922009-01-26 21:26:08 +00001238
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001239 case ABIArgInfo::Coerce: {
1240 assert(AI != Fn->arg_end() && "Argument mismatch!");
1241 // FIXME: This is very wasteful; EmitParmDecl is just going to
1242 // drop the result in a new alloca anyway, so we could just
1243 // store into that directly if we broke the abstraction down
1244 // more.
1245 llvm::Value *V = CreateTempAlloca(ConvertType(Ty), "coerce");
1246 CreateCoercedStore(AI, V, *this);
1247 // Match to what EmitParmDecl is expecting for this type.
Daniel Dunbar8b29a382009-02-04 07:22:24 +00001248 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001249 V = Builder.CreateLoad(V);
Daniel Dunbar8b29a382009-02-04 07:22:24 +00001250 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1251 // This must be a promotion, for something like
1252 // "void a(x) short x; {..."
1253 V = EmitScalarConversion(V, Ty, Arg->getType());
1254 }
1255 }
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001256 EmitParmDecl(*Arg, V);
1257 break;
1258 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001259 }
Daniel Dunbar56273772008-09-17 00:51:38 +00001260
1261 ++AI;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001262 }
1263 assert(AI == Fn->arg_end() && "Argument mismatch!");
1264}
1265
Daniel Dunbar88b53962009-02-02 22:03:45 +00001266void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI,
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001267 llvm::Value *ReturnValue) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001268 llvm::Value *RV = 0;
1269
1270 // Functions with no result always return void.
1271 if (ReturnValue) {
Daniel Dunbar88b53962009-02-02 22:03:45 +00001272 QualType RetTy = FI.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001273 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001274
1275 switch (RetAI.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001276 // FIXME: Implement correct [in]direct semantics.
1277 case ABIArgInfo::Indirect:
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001278 if (RetTy->isAnyComplexType()) {
1279 // FIXME: Volatile
1280 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
1281 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
1282 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1283 EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy);
1284 } else {
1285 Builder.CreateStore(Builder.CreateLoad(ReturnValue),
1286 CurFn->arg_begin());
1287 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001288 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001289
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001290 case ABIArgInfo::Direct:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001291 RV = Builder.CreateLoad(ReturnValue);
1292 break;
1293
Daniel Dunbar11434922009-01-26 21:26:08 +00001294 case ABIArgInfo::Ignore:
1295 break;
1296
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001297 case ABIArgInfo::Coerce: {
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +00001298 RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001299 break;
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001300 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001301
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001302 case ABIArgInfo::Expand:
1303 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001304 }
1305 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001306
1307 if (RV) {
1308 Builder.CreateRet(RV);
1309 } else {
1310 Builder.CreateRetVoid();
1311 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001312}
1313
Daniel Dunbar88b53962009-02-02 22:03:45 +00001314RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
1315 llvm::Value *Callee,
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001316 const CallArgList &CallArgs) {
Daniel Dunbar5251afa2009-02-03 06:02:10 +00001317 // FIXME: We no longer need the types from CallArgs; lift up and
1318 // simplify.
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001319 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001320
1321 // Handle struct-return functions by passing a pointer to the
1322 // location that we would like to return into.
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001323 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001324 const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001325 switch (RetAI.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001326 case ABIArgInfo::Indirect:
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001327 // Create a temporary alloca to hold the result of the call. :(
Daniel Dunbar56273772008-09-17 00:51:38 +00001328 Args.push_back(CreateTempAlloca(ConvertType(RetTy)));
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001329 break;
1330
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001331 case ABIArgInfo::Direct:
Daniel Dunbar11434922009-01-26 21:26:08 +00001332 case ABIArgInfo::Ignore:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001333 case ABIArgInfo::Coerce:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001334 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001335
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001336 case ABIArgInfo::Expand:
Daniel Dunbarb225be42009-02-03 05:59:18 +00001337 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001338 }
1339
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +00001340 assert(CallInfo.arg_size() == CallArgs.size() &&
1341 "Mismatch between function signature & arguments.");
Daniel Dunbarb225be42009-02-03 05:59:18 +00001342 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001343 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001344 I != E; ++I, ++info_it) {
1345 const ABIArgInfo &ArgInfo = info_it->info;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001346 RValue RV = I->first;
Daniel Dunbar56273772008-09-17 00:51:38 +00001347
1348 switch (ArgInfo.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001349 // FIXME: Implement correct [in]direct semantics.
1350 case ABIArgInfo::Indirect:
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001351 case ABIArgInfo::Direct:
Daniel Dunbar56273772008-09-17 00:51:38 +00001352 if (RV.isScalar()) {
1353 Args.push_back(RV.getScalarVal());
1354 } else if (RV.isComplex()) {
1355 // Make a temporary alloca to pass the argument.
1356 Args.push_back(CreateTempAlloca(ConvertType(I->second)));
1357 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
1358 } else {
1359 Args.push_back(RV.getAggregateAddr());
1360 }
1361 break;
1362
Daniel Dunbar11434922009-01-26 21:26:08 +00001363 case ABIArgInfo::Ignore:
1364 break;
1365
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001366 case ABIArgInfo::Coerce: {
1367 // FIXME: Avoid the conversion through memory if possible.
1368 llvm::Value *SrcPtr;
1369 if (RV.isScalar()) {
Daniel Dunbar5a1be6e2009-02-03 23:04:57 +00001370 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001371 Builder.CreateStore(RV.getScalarVal(), SrcPtr);
1372 } else if (RV.isComplex()) {
1373 SrcPtr = CreateTempAlloca(ConvertType(I->second), "coerce");
1374 StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
1375 } else
1376 SrcPtr = RV.getAggregateAddr();
1377 Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
1378 *this));
1379 break;
1380 }
1381
Daniel Dunbar56273772008-09-17 00:51:38 +00001382 case ABIArgInfo::Expand:
1383 ExpandTypeToArgs(I->second, RV, Args);
1384 break;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001385 }
1386 }
1387
1388 llvm::CallInst *CI = Builder.CreateCall(Callee,&Args[0],&Args[0]+Args.size());
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001389
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001390 // FIXME: Provide TargetDecl so nounwind, noreturn, etc, etc get set.
Devang Patel761d7f72008-09-25 21:02:23 +00001391 CodeGen::AttributeListType AttributeList;
Daniel Dunbar88b53962009-02-02 22:03:45 +00001392 CGM.ConstructAttributeList(CallInfo, 0, AttributeList);
Devang Patel761d7f72008-09-25 21:02:23 +00001393 CI->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(),
Daniel Dunbar725ad312009-01-31 02:19:00 +00001394 AttributeList.size()));
1395
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001396 if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee))
1397 CI->setCallingConv(F->getCallingConv());
1398 if (CI->getType() != llvm::Type::VoidTy)
1399 CI->setName("call");
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001400
1401 switch (RetAI.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001402 // FIXME: Implement correct [in]direct semantics.
1403 case ABIArgInfo::Indirect:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001404 if (RetTy->isAnyComplexType())
Daniel Dunbar56273772008-09-17 00:51:38 +00001405 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001406 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar56273772008-09-17 00:51:38 +00001407 return RValue::getAggregate(Args[0]);
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001408 else
1409 return RValue::get(Builder.CreateLoad(Args[0]));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001410
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001411 case ABIArgInfo::Direct:
1412 assert((!RetTy->isAnyComplexType() &&
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001413 !CodeGenFunction::hasAggregateLLVMType(RetTy)) &&
1414 "FIXME: Implement return for non-scalar direct types.");
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001415 return RValue::get(CI);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001416
Daniel Dunbar11434922009-01-26 21:26:08 +00001417 case ABIArgInfo::Ignore:
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001418 // If we are ignoring an argument that had a result, make sure to
1419 // construct the appropriate return value for our caller.
Daniel Dunbar13e81732009-02-05 07:09:07 +00001420 return GetUndefRValue(RetTy);
1421 if (RetTy->isVoidType())
1422 return RValue::get(0);
Daniel Dunbar11434922009-01-26 21:26:08 +00001423
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001424 case ABIArgInfo::Coerce: {
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001425 // FIXME: Avoid the conversion through memory if possible.
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +00001426 llvm::Value *V = CreateTempAlloca(ConvertType(RetTy), "coerce");
1427 CreateCoercedStore(CI, V, *this);
Anders Carlssonad3d6912008-11-25 22:21:48 +00001428 if (RetTy->isAnyComplexType())
1429 return RValue::getComplex(LoadComplexFromAddr(V, false));
Daniel Dunbar11434922009-01-26 21:26:08 +00001430 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssonad3d6912008-11-25 22:21:48 +00001431 return RValue::getAggregate(V);
Daniel Dunbar11434922009-01-26 21:26:08 +00001432 else
1433 return RValue::get(Builder.CreateLoad(V));
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001434 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001435
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001436 case ABIArgInfo::Expand:
1437 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001438 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001439
1440 assert(0 && "Unhandled ABIArgInfo::Kind");
1441 return RValue::get(0);
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001442}