blob: f90d31cd0b0550e24414f3a0c8ab0e0ac107ebe7 [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));
Daniel Dunbar386621f2009-02-07 02:46:03 +0000962 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
963 // FIXME: Use better alignment / avoid requiring aligned load.
964 Load->setAlignment(1);
965 return Load;
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000966 } else {
967 assert(SrcSize < DstSize && "Coercion is losing source bits!");
968
969 // Otherwise do coercion through memory. This is stupid, but
970 // simple.
971 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
972 llvm::Value *Casted =
973 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar386621f2009-02-07 02:46:03 +0000974 llvm::StoreInst *Store =
975 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
976 // FIXME: Use better alignment / avoid requiring aligned store.
977 Store->setAlignment(1);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000978 return CGF.Builder.CreateLoad(Tmp);
979 }
980}
981
982/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
983/// where the source and destination may have different types.
984///
985/// This safely handles the case when the src type is larger than the
986/// destination type; the upper bits of the src will be lost.
987static void CreateCoercedStore(llvm::Value *Src,
988 llvm::Value *DstPtr,
989 CodeGenFunction &CGF) {
990 const llvm::Type *SrcTy = Src->getType();
991 const llvm::Type *DstTy =
992 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
993
994 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
995 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(DstTy);
996
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000997 // If store is legal, just bitcast the src pointer.
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000998 if (SrcSize == DstSize) {
999 llvm::Value *Casted =
1000 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar386621f2009-02-07 02:46:03 +00001001 // FIXME: Use better alignment / avoid requiring aligned store.
1002 CGF.Builder.CreateStore(Src, Casted)->setAlignment(1);
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001003 } else {
1004 assert(SrcSize > DstSize && "Coercion is missing bits!");
1005
1006 // Otherwise do coercion through memory. This is stupid, but
1007 // simple.
1008 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
1009 CGF.Builder.CreateStore(Src, Tmp);
1010 llvm::Value *Casted =
1011 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
Daniel Dunbar386621f2009-02-07 02:46:03 +00001012 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
1013 // FIXME: Use better alignment / avoid requiring aligned load.
1014 Load->setAlignment(1);
1015 CGF.Builder.CreateStore(Load, DstPtr);
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001016 }
1017}
1018
Daniel Dunbar56273772008-09-17 00:51:38 +00001019/***/
1020
Daniel Dunbar88b53962009-02-02 22:03:45 +00001021bool CodeGenModule::ReturnTypeUsesSret(const CGFunctionInfo &FI) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001022 return FI.getReturnInfo().isIndirect();
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001023}
1024
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001025const llvm::FunctionType *
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001026CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic) {
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001027 std::vector<const llvm::Type*> ArgTys;
1028
1029 const llvm::Type *ResultType = 0;
1030
Daniel Dunbara0a99e02009-02-02 23:43:58 +00001031 QualType RetTy = FI.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001032 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001033 switch (RetAI.getKind()) {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001034 case ABIArgInfo::Expand:
1035 assert(0 && "Invalid ABI kind for return argument");
1036
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001037 case ABIArgInfo::Direct:
1038 ResultType = ConvertType(RetTy);
1039 break;
1040
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001041 case ABIArgInfo::Indirect: {
1042 assert(!RetAI.getIndirectAlign() && "Align unused on indirect return.");
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001043 ResultType = llvm::Type::VoidTy;
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +00001044 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001045 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
1046 break;
1047 }
1048
Daniel Dunbar11434922009-01-26 21:26:08 +00001049 case ABIArgInfo::Ignore:
1050 ResultType = llvm::Type::VoidTy;
1051 break;
1052
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001053 case ABIArgInfo::Coerce:
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001054 ResultType = RetAI.getCoerceToType();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001055 break;
1056 }
1057
Daniel Dunbar88c2fa92009-02-03 05:31:23 +00001058 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1059 ie = FI.arg_end(); it != ie; ++it) {
1060 const ABIArgInfo &AI = it->info;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001061
1062 switch (AI.getKind()) {
Daniel Dunbar11434922009-01-26 21:26:08 +00001063 case ABIArgInfo::Ignore:
1064 break;
1065
Daniel Dunbar56273772008-09-17 00:51:38 +00001066 case ABIArgInfo::Coerce:
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001067 ArgTys.push_back(AI.getCoerceToType());
1068 break;
1069
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001070 case ABIArgInfo::Indirect:
1071 // indirect arguments are always on the stack, which is addr space #0.
Daniel Dunbar1f745982009-02-05 09:16:39 +00001072 ArgTys.push_back(llvm::PointerType::getUnqual(ConvertType(it->type)));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001073 break;
1074
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001075 case ABIArgInfo::Direct:
Daniel Dunbar1f745982009-02-05 09:16:39 +00001076 ArgTys.push_back(ConvertType(it->type));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001077 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001078
1079 case ABIArgInfo::Expand:
Daniel Dunbar88c2fa92009-02-03 05:31:23 +00001080 GetExpandedTypes(it->type, ArgTys);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001081 break;
1082 }
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001083 }
1084
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001085 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar3913f182008-09-09 23:48:28 +00001086}
1087
Daniel Dunbara0a99e02009-02-02 23:43:58 +00001088void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
Daniel Dunbar88b53962009-02-02 22:03:45 +00001089 const Decl *TargetDecl,
Devang Patel761d7f72008-09-25 21:02:23 +00001090 AttributeListType &PAL) {
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001091 unsigned FuncAttrs = 0;
Devang Patela2c69122008-09-26 22:53:57 +00001092 unsigned RetAttrs = 0;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001093
1094 if (TargetDecl) {
1095 if (TargetDecl->getAttr<NoThrowAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +00001096 FuncAttrs |= llvm::Attribute::NoUnwind;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001097 if (TargetDecl->getAttr<NoReturnAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +00001098 FuncAttrs |= llvm::Attribute::NoReturn;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001099 if (TargetDecl->getAttr<PureAttr>())
1100 FuncAttrs |= llvm::Attribute::ReadOnly;
1101 if (TargetDecl->getAttr<ConstAttr>())
1102 FuncAttrs |= llvm::Attribute::ReadNone;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001103 }
1104
Daniel Dunbara0a99e02009-02-02 23:43:58 +00001105 QualType RetTy = FI.getReturnType();
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001106 unsigned Index = 1;
Daniel Dunbarb225be42009-02-03 05:59:18 +00001107 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001108 switch (RetAI.getKind()) {
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001109 case ABIArgInfo::Direct:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001110 if (RetTy->isPromotableIntegerType()) {
1111 if (RetTy->isSignedIntegerType()) {
Devang Patela2c69122008-09-26 22:53:57 +00001112 RetAttrs |= llvm::Attribute::SExt;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001113 } else if (RetTy->isUnsignedIntegerType()) {
Devang Patela2c69122008-09-26 22:53:57 +00001114 RetAttrs |= llvm::Attribute::ZExt;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001115 }
1116 }
1117 break;
1118
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001119 case ABIArgInfo::Indirect:
Devang Patel761d7f72008-09-25 21:02:23 +00001120 PAL.push_back(llvm::AttributeWithIndex::get(Index,
Daniel Dunbar725ad312009-01-31 02:19:00 +00001121 llvm::Attribute::StructRet |
1122 llvm::Attribute::NoAlias));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001123 ++Index;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001124 break;
1125
Daniel Dunbar11434922009-01-26 21:26:08 +00001126 case ABIArgInfo::Ignore:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001127 case ABIArgInfo::Coerce:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001128 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001129
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001130 case ABIArgInfo::Expand:
1131 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001132 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001133
Devang Patela2c69122008-09-26 22:53:57 +00001134 if (RetAttrs)
1135 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Daniel Dunbar88c2fa92009-02-03 05:31:23 +00001136 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1137 ie = FI.arg_end(); it != ie; ++it) {
1138 QualType ParamType = it->type;
1139 const ABIArgInfo &AI = it->info;
Devang Patel761d7f72008-09-25 21:02:23 +00001140 unsigned Attributes = 0;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001141
1142 switch (AI.getKind()) {
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001143 case ABIArgInfo::Coerce:
1144 break;
1145
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001146 case ABIArgInfo::Indirect:
Devang Patel761d7f72008-09-25 21:02:23 +00001147 Attributes |= llvm::Attribute::ByVal;
Daniel Dunbarca008822009-02-05 01:31:19 +00001148 Attributes |=
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001149 llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign());
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001150 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()) {
Daniel Dunbar1f745982009-02-05 09:16:39 +00001211 case ABIArgInfo::Indirect: {
1212 llvm::Value* V = AI;
1213 if (hasAggregateLLVMType(Ty)) {
1214 // Do nothing, aggregates and complex variables are accessed by
1215 // reference.
1216 } else {
1217 // Load scalar value from indirect argument.
1218 V = Builder.CreateLoad(V);
1219 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1220 // This must be a promotion, for something like
1221 // "void a(x) short x; {..."
1222 V = EmitScalarConversion(V, Ty, Arg->getType());
1223 }
1224 }
1225 EmitParmDecl(*Arg, V);
1226 break;
1227 }
1228
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001229 case ABIArgInfo::Direct: {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001230 assert(AI != Fn->arg_end() && "Argument mismatch!");
1231 llvm::Value* V = AI;
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00001232 if (hasAggregateLLVMType(Ty)) {
1233 // Create a temporary alloca to hold the argument; the rest of
1234 // codegen expects to access aggregates & complex values by
1235 // reference.
1236 V = CreateTempAlloca(ConvertType(Ty));
1237 Builder.CreateStore(AI, V);
1238 } else {
1239 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1240 // This must be a promotion, for something like
1241 // "void a(x) short x; {..."
1242 V = EmitScalarConversion(V, Ty, Arg->getType());
1243 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001244 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001245 EmitParmDecl(*Arg, V);
1246 break;
1247 }
Daniel Dunbar56273772008-09-17 00:51:38 +00001248
1249 case ABIArgInfo::Expand: {
Daniel Dunbarb225be42009-02-03 05:59:18 +00001250 // If this structure was expanded into multiple arguments then
Daniel Dunbar56273772008-09-17 00:51:38 +00001251 // we need to create a temporary and reconstruct it from the
1252 // arguments.
Chris Lattner39f34e92008-11-24 04:00:27 +00001253 std::string Name = Arg->getNameAsString();
Daniel Dunbar56273772008-09-17 00:51:38 +00001254 llvm::Value *Temp = CreateTempAlloca(ConvertType(Ty),
1255 (Name + ".addr").c_str());
1256 // FIXME: What are the right qualifiers here?
1257 llvm::Function::arg_iterator End =
1258 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI);
1259 EmitParmDecl(*Arg, Temp);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001260
Daniel Dunbar56273772008-09-17 00:51:38 +00001261 // Name the arguments used in expansion and increment AI.
1262 unsigned Index = 0;
1263 for (; AI != End; ++AI, ++Index)
1264 AI->setName(Name + "." + llvm::utostr(Index));
1265 continue;
1266 }
Daniel Dunbar11434922009-01-26 21:26:08 +00001267
1268 case ABIArgInfo::Ignore:
Daniel Dunbar8b979d92009-02-10 00:06:49 +00001269 // Initialize the local variable appropriately.
1270 if (hasAggregateLLVMType(Ty)) {
1271 EmitParmDecl(*Arg, CreateTempAlloca(ConvertType(Ty)));
1272 } else {
1273 EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())));
1274 }
1275
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +00001276 // Skip increment, no matching LLVM parameter.
1277 continue;
Daniel Dunbar11434922009-01-26 21:26:08 +00001278
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001279 case ABIArgInfo::Coerce: {
1280 assert(AI != Fn->arg_end() && "Argument mismatch!");
1281 // FIXME: This is very wasteful; EmitParmDecl is just going to
1282 // drop the result in a new alloca anyway, so we could just
1283 // store into that directly if we broke the abstraction down
1284 // more.
1285 llvm::Value *V = CreateTempAlloca(ConvertType(Ty), "coerce");
1286 CreateCoercedStore(AI, V, *this);
1287 // Match to what EmitParmDecl is expecting for this type.
Daniel Dunbar8b29a382009-02-04 07:22:24 +00001288 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001289 V = Builder.CreateLoad(V);
Daniel Dunbar8b29a382009-02-04 07:22:24 +00001290 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1291 // This must be a promotion, for something like
1292 // "void a(x) short x; {..."
1293 V = EmitScalarConversion(V, Ty, Arg->getType());
1294 }
1295 }
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001296 EmitParmDecl(*Arg, V);
1297 break;
1298 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001299 }
Daniel Dunbar56273772008-09-17 00:51:38 +00001300
1301 ++AI;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001302 }
1303 assert(AI == Fn->arg_end() && "Argument mismatch!");
1304}
1305
Daniel Dunbar88b53962009-02-02 22:03:45 +00001306void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI,
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001307 llvm::Value *ReturnValue) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001308 llvm::Value *RV = 0;
1309
1310 // Functions with no result always return void.
1311 if (ReturnValue) {
Daniel Dunbar88b53962009-02-02 22:03:45 +00001312 QualType RetTy = FI.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001313 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001314
1315 switch (RetAI.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001316 case ABIArgInfo::Indirect:
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001317 if (RetTy->isAnyComplexType()) {
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001318 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
1319 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
1320 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1321 EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy);
1322 } else {
1323 Builder.CreateStore(Builder.CreateLoad(ReturnValue),
1324 CurFn->arg_begin());
1325 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001326 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001327
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001328 case ABIArgInfo::Direct:
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00001329 // The internal return value temp always will have
1330 // pointer-to-return-type type.
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001331 RV = Builder.CreateLoad(ReturnValue);
1332 break;
1333
Daniel Dunbar11434922009-01-26 21:26:08 +00001334 case ABIArgInfo::Ignore:
1335 break;
1336
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001337 case ABIArgInfo::Coerce: {
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +00001338 RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001339 break;
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001340 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001341
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001342 case ABIArgInfo::Expand:
1343 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001344 }
1345 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001346
1347 if (RV) {
1348 Builder.CreateRet(RV);
1349 } else {
1350 Builder.CreateRetVoid();
1351 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001352}
1353
Daniel Dunbar88b53962009-02-02 22:03:45 +00001354RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
1355 llvm::Value *Callee,
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001356 const CallArgList &CallArgs) {
Daniel Dunbar5251afa2009-02-03 06:02:10 +00001357 // FIXME: We no longer need the types from CallArgs; lift up and
1358 // simplify.
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001359 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001360
1361 // Handle struct-return functions by passing a pointer to the
1362 // location that we would like to return into.
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001363 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001364 const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
Daniel Dunbar2969a022009-02-05 09:24:53 +00001365 if (CGM.ReturnTypeUsesSret(CallInfo)) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001366 // Create a temporary alloca to hold the result of the call. :(
Daniel Dunbar56273772008-09-17 00:51:38 +00001367 Args.push_back(CreateTempAlloca(ConvertType(RetTy)));
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001368 }
1369
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +00001370 assert(CallInfo.arg_size() == CallArgs.size() &&
1371 "Mismatch between function signature & arguments.");
Daniel Dunbarb225be42009-02-03 05:59:18 +00001372 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001373 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001374 I != E; ++I, ++info_it) {
1375 const ABIArgInfo &ArgInfo = info_it->info;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001376 RValue RV = I->first;
Daniel Dunbar56273772008-09-17 00:51:38 +00001377
1378 switch (ArgInfo.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001379 case ABIArgInfo::Indirect:
Daniel Dunbar1f745982009-02-05 09:16:39 +00001380 if (RV.isScalar() || RV.isComplex()) {
1381 // Make a temporary alloca to pass the argument.
1382 Args.push_back(CreateTempAlloca(ConvertType(I->second)));
1383 if (RV.isScalar())
1384 Builder.CreateStore(RV.getScalarVal(), Args.back());
1385 else
1386 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
1387 } else {
1388 Args.push_back(RV.getAggregateAddr());
1389 }
1390 break;
1391
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001392 case ABIArgInfo::Direct:
Daniel Dunbar56273772008-09-17 00:51:38 +00001393 if (RV.isScalar()) {
1394 Args.push_back(RV.getScalarVal());
1395 } else if (RV.isComplex()) {
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00001396 llvm::Value *Tmp = llvm::UndefValue::get(ConvertType(I->second));
1397 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().first, 0);
1398 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().second, 1);
1399 Args.push_back(Tmp);
Daniel Dunbar56273772008-09-17 00:51:38 +00001400 } else {
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00001401 Args.push_back(Builder.CreateLoad(RV.getAggregateAddr()));
Daniel Dunbar56273772008-09-17 00:51:38 +00001402 }
1403 break;
1404
Daniel Dunbar11434922009-01-26 21:26:08 +00001405 case ABIArgInfo::Ignore:
1406 break;
1407
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001408 case ABIArgInfo::Coerce: {
1409 // FIXME: Avoid the conversion through memory if possible.
1410 llvm::Value *SrcPtr;
1411 if (RV.isScalar()) {
Daniel Dunbar5a1be6e2009-02-03 23:04:57 +00001412 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001413 Builder.CreateStore(RV.getScalarVal(), SrcPtr);
1414 } else if (RV.isComplex()) {
1415 SrcPtr = CreateTempAlloca(ConvertType(I->second), "coerce");
1416 StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
1417 } else
1418 SrcPtr = RV.getAggregateAddr();
1419 Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
1420 *this));
1421 break;
1422 }
1423
Daniel Dunbar56273772008-09-17 00:51:38 +00001424 case ABIArgInfo::Expand:
1425 ExpandTypeToArgs(I->second, RV, Args);
1426 break;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001427 }
1428 }
1429
1430 llvm::CallInst *CI = Builder.CreateCall(Callee,&Args[0],&Args[0]+Args.size());
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001431
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001432 // FIXME: Provide TargetDecl so nounwind, noreturn, etc, etc get set.
Devang Patel761d7f72008-09-25 21:02:23 +00001433 CodeGen::AttributeListType AttributeList;
Daniel Dunbar88b53962009-02-02 22:03:45 +00001434 CGM.ConstructAttributeList(CallInfo, 0, AttributeList);
Devang Patel761d7f72008-09-25 21:02:23 +00001435 CI->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(),
Daniel Dunbar725ad312009-01-31 02:19:00 +00001436 AttributeList.size()));
1437
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001438 if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee))
1439 CI->setCallingConv(F->getCallingConv());
1440 if (CI->getType() != llvm::Type::VoidTy)
1441 CI->setName("call");
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001442
1443 switch (RetAI.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001444 case ABIArgInfo::Indirect:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001445 if (RetTy->isAnyComplexType())
Daniel Dunbar56273772008-09-17 00:51:38 +00001446 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001447 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar56273772008-09-17 00:51:38 +00001448 return RValue::getAggregate(Args[0]);
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001449 else
1450 return RValue::get(Builder.CreateLoad(Args[0]));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001451
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001452 case ABIArgInfo::Direct:
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00001453 if (RetTy->isAnyComplexType()) {
1454 llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
1455 llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
1456 return RValue::getComplex(std::make_pair(Real, Imag));
1457 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1458 llvm::Value *V = CreateTempAlloca(ConvertType(RetTy), "agg.tmp");
1459 Builder.CreateStore(CI, V);
1460 return RValue::getAggregate(V);
1461 } else
1462 return RValue::get(CI);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001463
Daniel Dunbar11434922009-01-26 21:26:08 +00001464 case ABIArgInfo::Ignore:
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001465 // If we are ignoring an argument that had a result, make sure to
1466 // construct the appropriate return value for our caller.
Daniel Dunbar13e81732009-02-05 07:09:07 +00001467 return GetUndefRValue(RetTy);
Daniel Dunbar11434922009-01-26 21:26:08 +00001468
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001469 case ABIArgInfo::Coerce: {
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001470 // FIXME: Avoid the conversion through memory if possible.
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +00001471 llvm::Value *V = CreateTempAlloca(ConvertType(RetTy), "coerce");
1472 CreateCoercedStore(CI, V, *this);
Anders Carlssonad3d6912008-11-25 22:21:48 +00001473 if (RetTy->isAnyComplexType())
1474 return RValue::getComplex(LoadComplexFromAddr(V, false));
Daniel Dunbar11434922009-01-26 21:26:08 +00001475 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssonad3d6912008-11-25 22:21:48 +00001476 return RValue::getAggregate(V);
Daniel Dunbar11434922009-01-26 21:26:08 +00001477 else
1478 return RValue::get(Builder.CreateLoad(V));
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001479 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001480
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001481 case ABIArgInfo::Expand:
1482 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001483 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001484
1485 assert(0 && "Unhandled ABIArgInfo::Kind");
1486 return RValue::get(0);
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001487}