blob: 0535a667bc1e0ca133e89c168941acf13574654c [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 Dunbar54d1ccb2009-01-27 01:36:03 +000026#include "llvm/Target/TargetData.h"
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +000027
28#include "ABIInfo.h"
29
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000030using namespace clang;
31using namespace CodeGen;
32
33/***/
34
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000035// FIXME: Use iterator and sidestep silly type array creation.
36
Daniel Dunbar541b63b2009-02-02 23:23:47 +000037const
38CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionTypeNoProto *FTNP) {
39 return getFunctionInfo(FTNP->getResultType(),
40 llvm::SmallVector<QualType, 16>());
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000041}
42
Daniel Dunbar541b63b2009-02-02 23:23:47 +000043const
44CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionTypeProto *FTP) {
45 llvm::SmallVector<QualType, 16> ArgTys;
46 // FIXME: Kill copy.
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000047 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +000048 ArgTys.push_back(FTP->getArgType(i));
49 return getFunctionInfo(FTP->getResultType(), ArgTys);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000050}
51
Daniel Dunbar541b63b2009-02-02 23:23:47 +000052const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionDecl *FD) {
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000053 const FunctionType *FTy = FD->getType()->getAsFunctionType();
Daniel Dunbar541b63b2009-02-02 23:23:47 +000054 if (const FunctionTypeProto *FTP = dyn_cast<FunctionTypeProto>(FTy))
55 return getFunctionInfo(FTP);
56 return getFunctionInfo(cast<FunctionTypeNoProto>(FTy));
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000057}
58
Daniel Dunbar541b63b2009-02-02 23:23:47 +000059const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const ObjCMethodDecl *MD) {
60 llvm::SmallVector<QualType, 16> ArgTys;
61 ArgTys.push_back(MD->getSelfDecl()->getType());
62 ArgTys.push_back(Context.getObjCSelType());
63 // FIXME: Kill copy?
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000064 for (ObjCMethodDecl::param_const_iterator i = MD->param_begin(),
65 e = MD->param_end(); i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +000066 ArgTys.push_back((*i)->getType());
67 return getFunctionInfo(MD->getResultType(), ArgTys);
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000068}
69
Daniel Dunbar541b63b2009-02-02 23:23:47 +000070const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
71 const CallArgList &Args) {
72 // FIXME: Kill copy.
73 llvm::SmallVector<QualType, 16> ArgTys;
Daniel Dunbar725ad312009-01-31 02:19:00 +000074 for (CallArgList::const_iterator i = Args.begin(), e = Args.end();
75 i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +000076 ArgTys.push_back(i->second);
77 return getFunctionInfo(ResTy, ArgTys);
Daniel Dunbar725ad312009-01-31 02:19:00 +000078}
79
Daniel Dunbar541b63b2009-02-02 23:23:47 +000080const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
81 const FunctionArgList &Args) {
82 // FIXME: Kill copy.
83 llvm::SmallVector<QualType, 16> ArgTys;
Daniel Dunbarbb36d332009-02-02 21:43:58 +000084 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
85 i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +000086 ArgTys.push_back(i->second);
87 return getFunctionInfo(ResTy, ArgTys);
88}
89
90const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
91 const llvm::SmallVector<QualType, 16> &ArgTys) {
Daniel Dunbar40a6be62009-02-03 00:07:12 +000092 // Lookup or create unique function info.
93 llvm::FoldingSetNodeID ID;
94 CGFunctionInfo::Profile(ID, ResTy, ArgTys.begin(), ArgTys.end());
95
96 void *InsertPos = 0;
97 CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, InsertPos);
98 if (FI)
99 return *FI;
100
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000101 // Construct the function info.
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000102 FI = new CGFunctionInfo(ResTy, ArgTys);
103 FunctionInfos.InsertNode(FI, InsertPos);
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000104
105 // Compute ABI information.
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000106 getABIInfo().computeInfo(*FI, getContext());
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000107
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000108 return *FI;
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000109}
110
111/***/
112
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000113ABIInfo::~ABIInfo() {}
114
Daniel Dunbar834af452008-09-17 21:22:33 +0000115/// isEmptyStruct - Return true iff a structure has no non-empty
116/// members. Note that a structure with a flexible array member is not
117/// considered empty.
118static bool isEmptyStruct(QualType T) {
119 const RecordType *RT = T->getAsStructureType();
120 if (!RT)
121 return 0;
122 const RecordDecl *RD = RT->getDecl();
123 if (RD->hasFlexibleArrayMember())
124 return false;
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000125 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar834af452008-09-17 21:22:33 +0000126 e = RD->field_end(); i != e; ++i) {
127 const FieldDecl *FD = *i;
128 if (!isEmptyStruct(FD->getType()))
129 return false;
130 }
131 return true;
132}
133
134/// isSingleElementStruct - Determine if a structure is a "single
135/// element struct", i.e. it has exactly one non-empty field or
136/// exactly one field which is itself a single element
137/// struct. Structures with flexible array members are never
138/// considered single element structs.
139///
140/// \return The field declaration for the single non-empty field, if
141/// it exists.
142static const FieldDecl *isSingleElementStruct(QualType T) {
143 const RecordType *RT = T->getAsStructureType();
144 if (!RT)
145 return 0;
146
147 const RecordDecl *RD = RT->getDecl();
148 if (RD->hasFlexibleArrayMember())
149 return 0;
150
151 const FieldDecl *Found = 0;
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000152 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar834af452008-09-17 21:22:33 +0000153 e = RD->field_end(); i != e; ++i) {
154 const FieldDecl *FD = *i;
155 QualType FT = FD->getType();
156
157 if (isEmptyStruct(FT)) {
158 // Ignore
159 } else if (Found) {
160 return 0;
161 } else if (!CodeGenFunction::hasAggregateLLVMType(FT)) {
162 Found = FD;
163 } else {
164 Found = isSingleElementStruct(FT);
165 if (!Found)
166 return 0;
167 }
168 }
169
170 return Found;
171}
172
173static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
174 if (!Ty->getAsBuiltinType() && !Ty->isPointerType())
175 return false;
176
177 uint64_t Size = Context.getTypeSize(Ty);
178 return Size == 32 || Size == 64;
179}
180
181static bool areAllFields32Or64BitBasicType(const RecordDecl *RD,
182 ASTContext &Context) {
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000183 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar834af452008-09-17 21:22:33 +0000184 e = RD->field_end(); i != e; ++i) {
185 const FieldDecl *FD = *i;
186
187 if (!is32Or64BitBasicType(FD->getType(), Context))
188 return false;
189
190 // If this is a bit-field we need to make sure it is still a
191 // 32-bit or 64-bit type.
192 if (Expr *BW = FD->getBitWidth()) {
193 unsigned Width = BW->getIntegerConstantExprValue(Context).getZExtValue();
194 if (Width <= 16)
195 return false;
196 }
197 }
198 return true;
199}
200
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000201namespace {
202/// DefaultABIInfo - The default implementation for ABI specific
203/// details. This implementation provides information which results in
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000204/// self-consistent and sensible LLVM IR generation, but does not
205/// conform to any particular ABI.
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000206class DefaultABIInfo : public ABIInfo {
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000207 ABIArgInfo classifyReturnType(QualType RetTy,
208 ASTContext &Context) const;
209
210 ABIArgInfo classifyArgumentType(QualType RetTy,
211 ASTContext &Context) const;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000212
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000213 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
214 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
215 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
216 it != ie; ++it)
217 it->info = classifyArgumentType(it->type, Context);
218 }
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000219};
220
221/// X86_32ABIInfo - The X86-32 ABI information.
222class X86_32ABIInfo : public ABIInfo {
223public:
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000224 ABIArgInfo classifyReturnType(QualType RetTy,
225 ASTContext &Context) const;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000226
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000227 ABIArgInfo classifyArgumentType(QualType RetTy,
228 ASTContext &Context) const;
229
230 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
231 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
232 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
233 it != ie; ++it)
234 it->info = classifyArgumentType(it->type, Context);
235 }
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000236};
237}
238
239ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
240 ASTContext &Context) const {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000241 if (RetTy->isVoidType()) {
242 return ABIArgInfo::getIgnore();
243 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar834af452008-09-17 21:22:33 +0000244 // Classify "single element" structs as their element type.
245 const FieldDecl *SeltFD = isSingleElementStruct(RetTy);
246 if (SeltFD) {
247 QualType SeltTy = SeltFD->getType()->getDesugaredType();
248 if (const BuiltinType *BT = SeltTy->getAsBuiltinType()) {
249 // FIXME: This is gross, it would be nice if we could just
250 // pass back SeltTy and have clients deal with it. Is it worth
251 // supporting coerce to both LLVM and clang Types?
252 if (BT->isIntegerType()) {
253 uint64_t Size = Context.getTypeSize(SeltTy);
254 return ABIArgInfo::getCoerce(llvm::IntegerType::get((unsigned) Size));
255 } else if (BT->getKind() == BuiltinType::Float) {
256 return ABIArgInfo::getCoerce(llvm::Type::FloatTy);
257 } else if (BT->getKind() == BuiltinType::Double) {
258 return ABIArgInfo::getCoerce(llvm::Type::DoubleTy);
259 }
260 } else if (SeltTy->isPointerType()) {
261 // FIXME: It would be really nice if this could come out as
262 // the proper pointer type.
263 llvm::Type *PtrTy =
264 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
265 return ABIArgInfo::getCoerce(PtrTy);
266 }
267 }
268
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000269 uint64_t Size = Context.getTypeSize(RetTy);
270 if (Size == 8) {
271 return ABIArgInfo::getCoerce(llvm::Type::Int8Ty);
272 } else if (Size == 16) {
273 return ABIArgInfo::getCoerce(llvm::Type::Int16Ty);
274 } else if (Size == 32) {
275 return ABIArgInfo::getCoerce(llvm::Type::Int32Ty);
276 } else if (Size == 64) {
277 return ABIArgInfo::getCoerce(llvm::Type::Int64Ty);
278 } else {
279 return ABIArgInfo::getStructRet();
280 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000281 } else {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000282 return ABIArgInfo::getDirect();
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000283 }
284}
285
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000286ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
287 ASTContext &Context) const {
Daniel Dunbarf0357382008-09-17 20:11:04 +0000288 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar834af452008-09-17 21:22:33 +0000289 // Structures with flexible arrays are always byval.
290 if (const RecordType *RT = Ty->getAsStructureType())
291 if (RT->getDecl()->hasFlexibleArrayMember())
292 return ABIArgInfo::getByVal(0);
293
294 // Expand empty structs (i.e. ignore)
295 uint64_t Size = Context.getTypeSize(Ty);
296 if (Ty->isStructureType() && Size == 0)
297 return ABIArgInfo::getExpand();
298
299 // Expand structs with size <= 128-bits which consist only of
300 // basic types (int, long long, float, double, xxx*). This is
301 // non-recursive and does not ignore empty fields.
302 if (const RecordType *RT = Ty->getAsStructureType()) {
303 if (Context.getTypeSize(Ty) <= 4*32 &&
304 areAllFields32Or64BitBasicType(RT->getDecl(), Context))
305 return ABIArgInfo::getExpand();
306 }
307
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000308 return ABIArgInfo::getByVal(0);
309 } else {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000310 return ABIArgInfo::getDirect();
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000311 }
312}
313
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000314namespace {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000315/// X86_64ABIInfo - The X86_64 ABI information.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000316class X86_64ABIInfo : public ABIInfo {
317 enum Class {
318 Integer = 0,
319 SSE,
320 SSEUp,
321 X87,
322 X87Up,
323 ComplexX87,
324 NoClass,
325 Memory
326 };
327
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000328 /// merge - Implement the X86_64 ABI merging algorithm.
329 ///
Daniel Dunbarc4503572009-01-31 00:06:58 +0000330 /// Merge an accumulating classification \arg Accum with a field
331 /// classification \arg Field.
332 ///
333 /// \param Accum - The accumulating classification. This should
334 /// always be either NoClass or the result of a previous merge
335 /// call. In addition, this should never be Memory (the caller
336 /// should just return Memory for the aggregate).
337 Class merge(Class Accum, Class Field) const;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000338
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000339 /// classify - Determine the x86_64 register classes in which the
340 /// given type T should be passed.
341 ///
Daniel Dunbarc4503572009-01-31 00:06:58 +0000342 /// \param Lo - The classification for the parts of the type
343 /// residing in the low word of the containing object.
344 ///
345 /// \param Hi - The classification for the parts of the type
346 /// residing in the high word of the containing object.
347 ///
348 /// \param OffsetBase - The bit offset of this type in the
Daniel Dunbarcdf920e2009-01-30 22:40:15 +0000349 /// containing object. Some parameters are classified different
350 /// depending on whether they straddle an eightbyte boundary.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000351 ///
352 /// If a word is unused its result will be NoClass; if a type should
353 /// be passed in Memory then at least the classification of \arg Lo
354 /// will be Memory.
355 ///
356 /// The \arg Lo class will be NoClass iff the argument is ignored.
357 ///
358 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
359 /// be NoClass.
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000360 void classify(QualType T, ASTContext &Context, uint64_t OffsetBase,
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000361 Class &Lo, Class &Hi) const;
Daniel Dunbarc4503572009-01-31 00:06:58 +0000362
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000363 ABIArgInfo classifyReturnType(QualType RetTy,
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000364 ASTContext &Context) const;
365
366 ABIArgInfo classifyArgumentType(QualType Ty,
367 ASTContext &Context,
368 unsigned &freeIntRegs,
369 unsigned &freeSSERegs) const;
370
371public:
372 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000373};
374}
375
Daniel Dunbarc4503572009-01-31 00:06:58 +0000376X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum,
377 Class Field) const {
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000378 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
379 // classified recursively so that always two fields are
380 // considered. The resulting class is calculated according to
381 // the classes of the fields in the eightbyte:
382 //
383 // (a) If both classes are equal, this is the resulting class.
384 //
385 // (b) If one of the classes is NO_CLASS, the resulting class is
386 // the other class.
387 //
388 // (c) If one of the classes is MEMORY, the result is the MEMORY
389 // class.
390 //
391 // (d) If one of the classes is INTEGER, the result is the
392 // INTEGER.
393 //
394 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
395 // MEMORY is used as class.
396 //
397 // (f) Otherwise class SSE is used.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000398 assert((Accum == NoClass || Accum == Integer ||
399 Accum == SSE || Accum == SSEUp) &&
400 "Invalid accumulated classification during merge.");
401 if (Accum == Field || Field == NoClass)
402 return Accum;
403 else if (Field == Memory)
404 return Memory;
405 else if (Accum == NoClass)
406 return Field;
407 else if (Accum == Integer || Field == Integer)
408 return Integer;
409 else if (Field == X87 || Field == X87Up || Field == ComplexX87)
410 return Memory;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000411 else
Daniel Dunbarc4503572009-01-31 00:06:58 +0000412 return SSE;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000413}
414
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000415void X86_64ABIInfo::classify(QualType Ty,
416 ASTContext &Context,
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000417 uint64_t OffsetBase,
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000418 Class &Lo, Class &Hi) const {
Daniel Dunbar9a82b522009-02-02 18:06:39 +0000419 // FIXME: This code can be simplified by introducing a simple value
420 // class for Class pairs with appropriate constructor methods for
421 // the various situations.
422
Daniel Dunbarc4503572009-01-31 00:06:58 +0000423 Lo = Hi = NoClass;
424
425 Class &Current = OffsetBase < 64 ? Lo : Hi;
426 Current = Memory;
427
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000428 if (const BuiltinType *BT = Ty->getAsBuiltinType()) {
429 BuiltinType::Kind k = BT->getKind();
430
Daniel Dunbar11434922009-01-26 21:26:08 +0000431 if (k == BuiltinType::Void) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000432 Current = NoClass;
Daniel Dunbar11434922009-01-26 21:26:08 +0000433 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000434 Current = Integer;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000435 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000436 Current = SSE;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000437 } else if (k == BuiltinType::LongDouble) {
438 Lo = X87;
439 Hi = X87Up;
440 }
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000441 // FIXME: _Decimal32 and _Decimal64 are SSE.
442 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000443 // FIXME: __int128 is (Integer, Integer).
444 } else if (Ty->isPointerLikeType() || Ty->isBlockPointerType() ||
445 Ty->isObjCQualifiedInterfaceType()) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000446 Current = Integer;
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000447 } else if (const VectorType *VT = Ty->getAsVectorType()) {
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000448 uint64_t Size = Context.getTypeSize(VT);
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000449 if (Size == 64) {
Daniel Dunbard4cd1b02009-01-30 19:38:39 +0000450 // gcc passes <1 x double> in memory.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000451 if (VT->getElementType() == Context.DoubleTy)
Daniel Dunbard4cd1b02009-01-30 19:38:39 +0000452 return;
Daniel Dunbard4cd1b02009-01-30 19:38:39 +0000453
Daniel Dunbarc4503572009-01-31 00:06:58 +0000454 Current = SSE;
Daniel Dunbare33edf12009-01-30 18:40:10 +0000455
456 // If this type crosses an eightbyte boundary, it should be
457 // split.
Daniel Dunbarcdf920e2009-01-30 22:40:15 +0000458 if (OffsetBase && OffsetBase != 64)
Daniel Dunbare33edf12009-01-30 18:40:10 +0000459 Hi = Lo;
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000460 } else if (Size == 128) {
461 Lo = SSE;
462 Hi = SSEUp;
463 }
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000464 } else if (const ComplexType *CT = Ty->getAsComplexType()) {
465 QualType ET = CT->getElementType();
466
Daniel Dunbare33edf12009-01-30 18:40:10 +0000467 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbareac48dc2009-01-29 07:22:20 +0000468 if (ET->isIntegerType()) {
Daniel Dunbareac48dc2009-01-29 07:22:20 +0000469 if (Size <= 64)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000470 Current = Integer;
Daniel Dunbareac48dc2009-01-29 07:22:20 +0000471 else if (Size <= 128)
472 Lo = Hi = Integer;
473 } else if (ET == Context.FloatTy)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000474 Current = SSE;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000475 else if (ET == Context.DoubleTy)
476 Lo = Hi = SSE;
477 else if (ET == Context.LongDoubleTy)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000478 Current = ComplexX87;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000479
480 // If this complex type crosses an eightbyte boundary then it
481 // should be split.
Daniel Dunbarcdf920e2009-01-30 22:40:15 +0000482 uint64_t EB_Real = (OffsetBase) / 64;
483 uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000484 if (Hi == NoClass && EB_Real != EB_Imag)
485 Hi = Lo;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000486 } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
487 // Arrays are treated like structures.
488
489 uint64_t Size = Context.getTypeSize(Ty);
490
491 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
492 // than two eightbytes, ..., it has class MEMORY.
493 if (Size > 128)
494 return;
495
496 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
497 // fields, it has class MEMORY.
498 //
499 // Only need to check alignment of array base.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000500 if (OffsetBase % Context.getTypeAlign(AT->getElementType()))
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000501 return;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000502
503 // Otherwise implement simplified merge. We could be smarter about
504 // this, but it isn't worth it and would be harder to verify.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000505 Current = NoClass;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000506 uint64_t EltSize = Context.getTypeSize(AT->getElementType());
507 uint64_t ArraySize = AT->getSize().getZExtValue();
508 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
509 Class FieldLo, FieldHi;
510 classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbarc4503572009-01-31 00:06:58 +0000511 Lo = merge(Lo, FieldLo);
512 Hi = merge(Hi, FieldHi);
513 if (Lo == Memory || Hi == Memory)
514 break;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000515 }
Daniel Dunbarc4503572009-01-31 00:06:58 +0000516
517 // Do post merger cleanup (see below). Only case we worry about is Memory.
518 if (Hi == Memory)
519 Lo = Memory;
520 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Daniel Dunbar99037e52009-01-29 08:13:58 +0000521 } else if (const RecordType *RT = Ty->getAsRecordType()) {
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000522 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbar99037e52009-01-29 08:13:58 +0000523
524 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
525 // than two eightbytes, ..., it has class MEMORY.
526 if (Size > 128)
527 return;
528
529 const RecordDecl *RD = RT->getDecl();
530
531 // Assume variable sized types are passed in memory.
532 if (RD->hasFlexibleArrayMember())
533 return;
534
535 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
536
537 // Reset Lo class, this will be recomputed.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000538 Current = NoClass;
Daniel Dunbar99037e52009-01-29 08:13:58 +0000539 unsigned idx = 0;
540 for (RecordDecl::field_iterator i = RD->field_begin(),
541 e = RD->field_end(); i != e; ++i, ++idx) {
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000542 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Daniel Dunbar99037e52009-01-29 08:13:58 +0000543
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000544 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
545 // fields, it has class MEMORY.
Daniel Dunbar99037e52009-01-29 08:13:58 +0000546 if (Offset % Context.getTypeAlign(i->getType())) {
547 Lo = Memory;
548 return;
549 }
550
Daniel Dunbar99037e52009-01-29 08:13:58 +0000551 // Classify this field.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000552 //
553 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
554 // exceeds a single eightbyte, each is classified
555 // separately. Each eightbyte gets initialized to class
556 // NO_CLASS.
Daniel Dunbar99037e52009-01-29 08:13:58 +0000557 Class FieldLo, FieldHi;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000558 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbarc4503572009-01-31 00:06:58 +0000559 Lo = merge(Lo, FieldLo);
560 Hi = merge(Hi, FieldHi);
561 if (Lo == Memory || Hi == Memory)
562 break;
Daniel Dunbar99037e52009-01-29 08:13:58 +0000563 }
564
565 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
566 //
567 // (a) If one of the classes is MEMORY, the whole argument is
568 // passed in memory.
569 //
570 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
571
572 // The first of these conditions is guaranteed by how we implement
Daniel Dunbarc4503572009-01-31 00:06:58 +0000573 // the merge (just bail).
574 //
575 // The second condition occurs in the case of unions; for example
576 // union { _Complex double; unsigned; }.
577 if (Hi == Memory)
578 Lo = Memory;
Daniel Dunbar99037e52009-01-29 08:13:58 +0000579 if (Hi == SSEUp && Lo != SSE)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000580 Hi = SSE;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000581 }
582}
583
Daniel Dunbarc4503572009-01-31 00:06:58 +0000584
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000585ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
586 ASTContext &Context) const {
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000587 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
588 // classification algorithm.
589 X86_64ABIInfo::Class Lo, Hi;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000590 classify(RetTy, Context, 0, Lo, Hi);
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000591
Daniel Dunbarc4503572009-01-31 00:06:58 +0000592 // Check some invariants.
593 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
594 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
595 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
596
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000597 const llvm::Type *ResType = 0;
598 switch (Lo) {
599 case NoClass:
Daniel Dunbar11434922009-01-26 21:26:08 +0000600 return ABIArgInfo::getIgnore();
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000601
602 case SSEUp:
603 case X87Up:
604 assert(0 && "Invalid classification for lo word.");
605
Daniel Dunbarc4503572009-01-31 00:06:58 +0000606 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
607 // hidden argument, i.e. structret.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000608 case Memory:
609 return ABIArgInfo::getStructRet();
610
611 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
612 // available register of the sequence %rax, %rdx is used.
613 case Integer:
614 ResType = llvm::Type::Int64Ty; break;
615
616 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
617 // available SSE register of the sequence %xmm0, %xmm1 is used.
618 case SSE:
619 ResType = llvm::Type::DoubleTy; break;
620
621 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
622 // returned on the X87 stack in %st0 as 80-bit x87 number.
623 case X87:
624 ResType = llvm::Type::X86_FP80Ty; break;
625
Daniel Dunbarc4503572009-01-31 00:06:58 +0000626 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
627 // part of the value is returned in %st0 and the imaginary part in
628 // %st1.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000629 case ComplexX87:
630 assert(Hi == NoClass && "Unexpected ComplexX87 classification.");
631 ResType = llvm::VectorType::get(llvm::Type::X86_FP80Ty, 2);
632 break;
633 }
634
635 switch (Hi) {
636 // Memory was handled previously, and ComplexX87 and X87 should
637 // never occur as hi classes.
638 case Memory:
639 case X87:
640 case ComplexX87:
641 assert(0 && "Invalid classification for hi word.");
642
643 case NoClass: break;
644 case Integer:
Daniel Dunbarb0e14f22009-01-29 07:36:07 +0000645 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
646 break;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000647 case SSE:
Daniel Dunbarb0e14f22009-01-29 07:36:07 +0000648 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
649 break;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000650
651 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
652 // is passed in the upper half of the last used SSE register.
653 //
654 // SSEUP should always be preceeded by SSE, just widen.
655 case SSEUp:
656 assert(Lo == SSE && "Unexpected SSEUp classification.");
657 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
658 break;
659
660 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
Daniel Dunbarb0e14f22009-01-29 07:36:07 +0000661 // returned together with the previous X87 value in %st0.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000662 //
663 // X87UP should always be preceeded by X87, so we don't need to do
664 // anything here.
665 case X87Up:
666 assert(Lo == X87 && "Unexpected X87Up classification.");
667 break;
668 }
669
670 return ABIArgInfo::getCoerce(ResType);
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000671}
672
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000673ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, ASTContext &Context,
674 unsigned &freeIntRegs,
675 unsigned &freeSSERegs) const {
676 X86_64ABIInfo::Class Lo, Hi;
677 classify(Ty, Context, 0, Lo, Hi);
678
679 // Check some invariants.
680 // FIXME: Enforce these by construction.
681 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
682 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
683 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
684
685 unsigned neededInt = 0, neededSSE = 0;
686 const llvm::Type *ResType = 0;
687 switch (Lo) {
688 case NoClass:
689 return ABIArgInfo::getIgnore();
690
691 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
692 // on the stack.
693 case Memory:
694
695 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
696 // COMPLEX_X87, it is passed in memory.
697 case X87:
698 case ComplexX87:
699 // Choose appropriate in memory type.
700 if (CodeGenFunction::hasAggregateLLVMType(Ty))
701 return ABIArgInfo::getByVal(0);
702 else
703 return ABIArgInfo::getDirect();
704
705 case SSEUp:
706 case X87Up:
707 assert(0 && "Invalid classification for lo word.");
708
709 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
710 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
711 // and %r9 is used.
712 case Integer:
713 ++neededInt;
714 ResType = llvm::Type::Int64Ty;
715 break;
716
717 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
718 // available SSE register is used, the registers are taken in the
719 // order from %xmm0 to %xmm7.
720 case SSE:
721 ++neededSSE;
722 ResType = llvm::Type::DoubleTy;
723 break;
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000724 }
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000725
726 switch (Hi) {
727 // Memory was handled previously, ComplexX87 and X87 should
728 // never occur as hi classes, and X87Up must be preceed by X87,
729 // which is passed in memory.
730 case Memory:
731 case X87:
732 case X87Up:
733 case ComplexX87:
734 assert(0 && "Invalid classification for hi word.");
735
736 case NoClass: break;
737 case Integer:
738 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
739 ++neededInt;
740 break;
741 case SSE:
742 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
743 ++neededSSE;
744 break;
745
746 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
747 // eightbyte is passed in the upper half of the last used SSE
748 // register.
749 case SSEUp:
750 assert(Lo == SSE && "Unexpected SSEUp classification.");
751 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
752 break;
753 }
754
755 // AMD64-ABI 3.2.3p3: If there are no registers available for any
756 // eightbyte of an argument, the whole argument is passed on the
757 // stack. If registers have already been assigned for some
758 // eightbytes of such an argument, the assignments get reverted.
759 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
760 freeIntRegs -= neededInt;
761 freeSSERegs -= neededSSE;
762 return ABIArgInfo::getCoerce(ResType);
763 } else {
764 // Choose appropriate in memory type.
765 if (CodeGenFunction::hasAggregateLLVMType(Ty))
766 return ABIArgInfo::getByVal(0);
767 else
768 return ABIArgInfo::getDirect();
769 }
770}
771
772void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
773 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
774
775 // Keep track of the number of assigned registers.
776 unsigned freeIntRegs = 6, freeSSERegs = 8;
777
778 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
779 // get assigned (in left-to-right order) for passing as follows...
780 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
781 it != ie; ++it)
782 it->info = classifyArgumentType(it->type, Context, freeIntRegs, freeSSERegs);
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000783}
784
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000785ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
786 ASTContext &Context) const {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000787 if (RetTy->isVoidType()) {
788 return ABIArgInfo::getIgnore();
789 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
790 return ABIArgInfo::getStructRet();
791 } else {
792 return ABIArgInfo::getDirect();
793 }
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000794}
795
796ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
797 ASTContext &Context) const {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000798 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
799 return ABIArgInfo::getByVal(0);
800 } else {
801 return ABIArgInfo::getDirect();
802 }
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000803}
804
805const ABIInfo &CodeGenTypes::getABIInfo() const {
806 if (TheABIInfo)
807 return *TheABIInfo;
808
809 // For now we just cache this in the CodeGenTypes and don't bother
810 // to free it.
811 const char *TargetPrefix = getContext().Target.getTargetPrefix();
812 if (strcmp(TargetPrefix, "x86") == 0) {
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000813 switch (getContext().Target.getPointerWidth(0)) {
814 case 32:
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000815 return *(TheABIInfo = new X86_32ABIInfo());
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000816 case 64:
Daniel Dunbar11a76ed2009-01-30 18:47:53 +0000817 return *(TheABIInfo = new X86_64ABIInfo());
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000818 }
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000819 }
820
821 return *(TheABIInfo = new DefaultABIInfo);
822}
823
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000824/***/
825
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000826CGFunctionInfo::CGFunctionInfo(QualType ResTy,
827 const llvm::SmallVector<QualType, 16> &ArgTys) {
828 NumArgs = ArgTys.size();
829 Args = new ArgInfo[1 + NumArgs];
830 Args[0].type = ResTy;
831 for (unsigned i = 0; i < NumArgs; ++i)
832 Args[1 + i].type = ArgTys[i];
833}
834
835/***/
836
Daniel Dunbar56273772008-09-17 00:51:38 +0000837void CodeGenTypes::GetExpandedTypes(QualType Ty,
838 std::vector<const llvm::Type*> &ArgTys) {
839 const RecordType *RT = Ty->getAsStructureType();
840 assert(RT && "Can only expand structure types.");
841 const RecordDecl *RD = RT->getDecl();
842 assert(!RD->hasFlexibleArrayMember() &&
843 "Cannot expand structure with flexible array.");
844
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000845 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar56273772008-09-17 00:51:38 +0000846 e = RD->field_end(); i != e; ++i) {
847 const FieldDecl *FD = *i;
848 assert(!FD->isBitField() &&
849 "Cannot expand structure with bit-field members.");
850
851 QualType FT = FD->getType();
852 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
853 GetExpandedTypes(FT, ArgTys);
854 } else {
855 ArgTys.push_back(ConvertType(FT));
856 }
857 }
858}
859
860llvm::Function::arg_iterator
861CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
862 llvm::Function::arg_iterator AI) {
863 const RecordType *RT = Ty->getAsStructureType();
864 assert(RT && "Can only expand structure types.");
865
866 RecordDecl *RD = RT->getDecl();
867 assert(LV.isSimple() &&
868 "Unexpected non-simple lvalue during struct expansion.");
869 llvm::Value *Addr = LV.getAddress();
870 for (RecordDecl::field_iterator i = RD->field_begin(),
871 e = RD->field_end(); i != e; ++i) {
872 FieldDecl *FD = *i;
873 QualType FT = FD->getType();
874
875 // FIXME: What are the right qualifiers here?
876 LValue LV = EmitLValueForField(Addr, FD, false, 0);
877 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
878 AI = ExpandTypeFromArgs(FT, LV, AI);
879 } else {
880 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
881 ++AI;
882 }
883 }
884
885 return AI;
886}
887
888void
889CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
890 llvm::SmallVector<llvm::Value*, 16> &Args) {
891 const RecordType *RT = Ty->getAsStructureType();
892 assert(RT && "Can only expand structure types.");
893
894 RecordDecl *RD = RT->getDecl();
895 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
896 llvm::Value *Addr = RV.getAggregateAddr();
897 for (RecordDecl::field_iterator i = RD->field_begin(),
898 e = RD->field_end(); i != e; ++i) {
899 FieldDecl *FD = *i;
900 QualType FT = FD->getType();
901
902 // FIXME: What are the right qualifiers here?
903 LValue LV = EmitLValueForField(Addr, FD, false, 0);
904 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
905 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
906 } else {
907 RValue RV = EmitLoadOfLValue(LV, FT);
908 assert(RV.isScalar() &&
909 "Unexpected non-scalar rvalue during struct expansion.");
910 Args.push_back(RV.getScalarVal());
911 }
912 }
913}
914
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000915/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
916/// a pointer to an object of type \arg Ty.
917///
918/// This safely handles the case when the src type is smaller than the
919/// destination type; in this situation the values of bits which not
920/// present in the src are undefined.
921static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
922 const llvm::Type *Ty,
923 CodeGenFunction &CGF) {
924 const llvm::Type *SrcTy =
925 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
926 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
927 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(Ty);
928
Daniel Dunbarb225be42009-02-03 05:59:18 +0000929 // If load is legal, just bitcast the src pointer.
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000930 if (SrcSize == DstSize) {
931 llvm::Value *Casted =
932 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
933 return CGF.Builder.CreateLoad(Casted);
934 } else {
935 assert(SrcSize < DstSize && "Coercion is losing source bits!");
936
937 // Otherwise do coercion through memory. This is stupid, but
938 // simple.
939 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
940 llvm::Value *Casted =
941 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
942 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
943 return CGF.Builder.CreateLoad(Tmp);
944 }
945}
946
947/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
948/// where the source and destination may have different types.
949///
950/// This safely handles the case when the src type is larger than the
951/// destination type; the upper bits of the src will be lost.
952static void CreateCoercedStore(llvm::Value *Src,
953 llvm::Value *DstPtr,
954 CodeGenFunction &CGF) {
955 const llvm::Type *SrcTy = Src->getType();
956 const llvm::Type *DstTy =
957 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
958
959 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
960 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(DstTy);
961
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000962 // If store is legal, just bitcast the src pointer.
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000963 if (SrcSize == DstSize) {
964 llvm::Value *Casted =
965 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
966 CGF.Builder.CreateStore(Src, Casted);
967 } else {
968 assert(SrcSize > DstSize && "Coercion is missing bits!");
969
970 // Otherwise do coercion through memory. This is stupid, but
971 // simple.
972 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
973 CGF.Builder.CreateStore(Src, Tmp);
974 llvm::Value *Casted =
975 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
976 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(Casted), DstPtr);
977 }
978}
979
Daniel Dunbar56273772008-09-17 00:51:38 +0000980/***/
981
Daniel Dunbar88b53962009-02-02 22:03:45 +0000982bool CodeGenModule::ReturnTypeUsesSret(const CGFunctionInfo &FI) {
Daniel Dunbarb225be42009-02-03 05:59:18 +0000983 return FI.getReturnInfo().isStructRet();
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000984}
985
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000986const llvm::FunctionType *
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000987CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic) {
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000988 std::vector<const llvm::Type*> ArgTys;
989
990 const llvm::Type *ResultType = 0;
991
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000992 QualType RetTy = FI.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000993 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000994 switch (RetAI.getKind()) {
995 case ABIArgInfo::ByVal:
996 case ABIArgInfo::Expand:
997 assert(0 && "Invalid ABI kind for return argument");
998
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000999 case ABIArgInfo::Direct:
1000 ResultType = ConvertType(RetTy);
1001 break;
1002
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001003 case ABIArgInfo::StructRet: {
1004 ResultType = llvm::Type::VoidTy;
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +00001005 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001006 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
1007 break;
1008 }
1009
Daniel Dunbar11434922009-01-26 21:26:08 +00001010 case ABIArgInfo::Ignore:
1011 ResultType = llvm::Type::VoidTy;
1012 break;
1013
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001014 case ABIArgInfo::Coerce:
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001015 ResultType = RetAI.getCoerceToType();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001016 break;
1017 }
1018
Daniel Dunbar88c2fa92009-02-03 05:31:23 +00001019 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1020 ie = FI.arg_end(); it != ie; ++it) {
1021 const ABIArgInfo &AI = it->info;
1022 const llvm::Type *Ty = ConvertType(it->type);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001023
1024 switch (AI.getKind()) {
Daniel Dunbar11434922009-01-26 21:26:08 +00001025 case ABIArgInfo::Ignore:
1026 break;
1027
Daniel Dunbar56273772008-09-17 00:51:38 +00001028 case ABIArgInfo::Coerce:
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001029 ArgTys.push_back(AI.getCoerceToType());
1030 break;
1031
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001032 case ABIArgInfo::StructRet:
1033 assert(0 && "Invalid ABI kind for non-return argument");
1034
1035 case ABIArgInfo::ByVal:
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001036 // byval arguments are always on the stack, which is addr space #0.
1037 ArgTys.push_back(llvm::PointerType::getUnqual(Ty));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001038 assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled");
1039 break;
1040
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001041 case ABIArgInfo::Direct:
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001042 ArgTys.push_back(Ty);
1043 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001044
1045 case ABIArgInfo::Expand:
Daniel Dunbar88c2fa92009-02-03 05:31:23 +00001046 GetExpandedTypes(it->type, ArgTys);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001047 break;
1048 }
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001049 }
1050
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001051 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar3913f182008-09-09 23:48:28 +00001052}
1053
Daniel Dunbara0a99e02009-02-02 23:43:58 +00001054void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
Daniel Dunbar88b53962009-02-02 22:03:45 +00001055 const Decl *TargetDecl,
Devang Patel761d7f72008-09-25 21:02:23 +00001056 AttributeListType &PAL) {
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001057 unsigned FuncAttrs = 0;
Devang Patela2c69122008-09-26 22:53:57 +00001058 unsigned RetAttrs = 0;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001059
1060 if (TargetDecl) {
1061 if (TargetDecl->getAttr<NoThrowAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +00001062 FuncAttrs |= llvm::Attribute::NoUnwind;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001063 if (TargetDecl->getAttr<NoReturnAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +00001064 FuncAttrs |= llvm::Attribute::NoReturn;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001065 if (TargetDecl->getAttr<PureAttr>())
1066 FuncAttrs |= llvm::Attribute::ReadOnly;
1067 if (TargetDecl->getAttr<ConstAttr>())
1068 FuncAttrs |= llvm::Attribute::ReadNone;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001069 }
1070
Daniel Dunbara0a99e02009-02-02 23:43:58 +00001071 QualType RetTy = FI.getReturnType();
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001072 unsigned Index = 1;
Daniel Dunbarb225be42009-02-03 05:59:18 +00001073 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001074 switch (RetAI.getKind()) {
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001075 case ABIArgInfo::Direct:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001076 if (RetTy->isPromotableIntegerType()) {
1077 if (RetTy->isSignedIntegerType()) {
Devang Patela2c69122008-09-26 22:53:57 +00001078 RetAttrs |= llvm::Attribute::SExt;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001079 } else if (RetTy->isUnsignedIntegerType()) {
Devang Patela2c69122008-09-26 22:53:57 +00001080 RetAttrs |= llvm::Attribute::ZExt;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001081 }
1082 }
1083 break;
1084
1085 case ABIArgInfo::StructRet:
Devang Patel761d7f72008-09-25 21:02:23 +00001086 PAL.push_back(llvm::AttributeWithIndex::get(Index,
Daniel Dunbar725ad312009-01-31 02:19:00 +00001087 llvm::Attribute::StructRet |
1088 llvm::Attribute::NoAlias));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001089 ++Index;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001090 break;
1091
Daniel Dunbar11434922009-01-26 21:26:08 +00001092 case ABIArgInfo::Ignore:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001093 case ABIArgInfo::Coerce:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001094 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001095
1096 case ABIArgInfo::ByVal:
1097 case ABIArgInfo::Expand:
1098 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001099 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001100
Devang Patela2c69122008-09-26 22:53:57 +00001101 if (RetAttrs)
1102 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Daniel Dunbar88c2fa92009-02-03 05:31:23 +00001103 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1104 ie = FI.arg_end(); it != ie; ++it) {
1105 QualType ParamType = it->type;
1106 const ABIArgInfo &AI = it->info;
Devang Patel761d7f72008-09-25 21:02:23 +00001107 unsigned Attributes = 0;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001108
1109 switch (AI.getKind()) {
1110 case ABIArgInfo::StructRet:
1111 assert(0 && "Invalid ABI kind for non-return argument");
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001112
1113 case ABIArgInfo::Coerce:
1114 break;
1115
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001116 case ABIArgInfo::ByVal:
Devang Patel761d7f72008-09-25 21:02:23 +00001117 Attributes |= llvm::Attribute::ByVal;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001118 assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled");
1119 break;
1120
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001121 case ABIArgInfo::Direct:
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001122 if (ParamType->isPromotableIntegerType()) {
1123 if (ParamType->isSignedIntegerType()) {
Devang Patel761d7f72008-09-25 21:02:23 +00001124 Attributes |= llvm::Attribute::SExt;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001125 } else if (ParamType->isUnsignedIntegerType()) {
Devang Patel761d7f72008-09-25 21:02:23 +00001126 Attributes |= llvm::Attribute::ZExt;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001127 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001128 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001129 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001130
Daniel Dunbar11434922009-01-26 21:26:08 +00001131 case ABIArgInfo::Ignore:
1132 // Skip increment, no matching LLVM parameter.
1133 continue;
1134
Daniel Dunbar56273772008-09-17 00:51:38 +00001135 case ABIArgInfo::Expand: {
1136 std::vector<const llvm::Type*> Tys;
1137 // FIXME: This is rather inefficient. Do we ever actually need
1138 // to do anything here? The result should be just reconstructed
1139 // on the other side, so extension should be a non-issue.
1140 getTypes().GetExpandedTypes(ParamType, Tys);
1141 Index += Tys.size();
1142 continue;
1143 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001144 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001145
Devang Patel761d7f72008-09-25 21:02:23 +00001146 if (Attributes)
1147 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar56273772008-09-17 00:51:38 +00001148 ++Index;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001149 }
Devang Patela2c69122008-09-26 22:53:57 +00001150 if (FuncAttrs)
1151 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
1152
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001153}
1154
Daniel Dunbar88b53962009-02-02 22:03:45 +00001155void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
1156 llvm::Function *Fn,
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001157 const FunctionArgList &Args) {
Daniel Dunbar5251afa2009-02-03 06:02:10 +00001158 // FIXME: We no longer need the types from FunctionArgList; lift up
1159 // and simplify.
1160
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001161 // Emit allocs for param decls. Give the LLVM Argument nodes names.
1162 llvm::Function::arg_iterator AI = Fn->arg_begin();
1163
1164 // Name the struct return argument.
Daniel Dunbar88b53962009-02-02 22:03:45 +00001165 if (CGM.ReturnTypeUsesSret(FI)) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001166 AI->setName("agg.result");
1167 ++AI;
1168 }
Daniel Dunbarb225be42009-02-03 05:59:18 +00001169
1170 CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001171 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001172 i != e; ++i, ++info_it) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001173 const VarDecl *Arg = i->first;
Daniel Dunbarb225be42009-02-03 05:59:18 +00001174 QualType Ty = info_it->type;
1175 const ABIArgInfo &ArgI = info_it->info;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001176
1177 switch (ArgI.getKind()) {
1178 case ABIArgInfo::ByVal:
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001179 case ABIArgInfo::Direct: {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001180 assert(AI != Fn->arg_end() && "Argument mismatch!");
1181 llvm::Value* V = AI;
Daniel Dunbar56273772008-09-17 00:51:38 +00001182 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001183 // This must be a promotion, for something like
1184 // "void a(x) short x; {..."
Daniel Dunbar56273772008-09-17 00:51:38 +00001185 V = EmitScalarConversion(V, Ty, Arg->getType());
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001186 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001187 EmitParmDecl(*Arg, V);
1188 break;
1189 }
Daniel Dunbar56273772008-09-17 00:51:38 +00001190
1191 case ABIArgInfo::Expand: {
Daniel Dunbarb225be42009-02-03 05:59:18 +00001192 // If this structure was expanded into multiple arguments then
Daniel Dunbar56273772008-09-17 00:51:38 +00001193 // we need to create a temporary and reconstruct it from the
1194 // arguments.
Chris Lattner39f34e92008-11-24 04:00:27 +00001195 std::string Name = Arg->getNameAsString();
Daniel Dunbar56273772008-09-17 00:51:38 +00001196 llvm::Value *Temp = CreateTempAlloca(ConvertType(Ty),
1197 (Name + ".addr").c_str());
1198 // FIXME: What are the right qualifiers here?
1199 llvm::Function::arg_iterator End =
1200 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI);
1201 EmitParmDecl(*Arg, Temp);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001202
Daniel Dunbar56273772008-09-17 00:51:38 +00001203 // Name the arguments used in expansion and increment AI.
1204 unsigned Index = 0;
1205 for (; AI != End; ++AI, ++Index)
1206 AI->setName(Name + "." + llvm::utostr(Index));
1207 continue;
1208 }
Daniel Dunbar11434922009-01-26 21:26:08 +00001209
1210 case ABIArgInfo::Ignore:
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +00001211 // Skip increment, no matching LLVM parameter.
1212 continue;
Daniel Dunbar11434922009-01-26 21:26:08 +00001213
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001214 case ABIArgInfo::Coerce: {
1215 assert(AI != Fn->arg_end() && "Argument mismatch!");
1216 // FIXME: This is very wasteful; EmitParmDecl is just going to
1217 // drop the result in a new alloca anyway, so we could just
1218 // store into that directly if we broke the abstraction down
1219 // more.
1220 llvm::Value *V = CreateTempAlloca(ConvertType(Ty), "coerce");
1221 CreateCoercedStore(AI, V, *this);
1222 // Match to what EmitParmDecl is expecting for this type.
1223 if (!CodeGenFunction::hasAggregateLLVMType(Ty))
1224 V = Builder.CreateLoad(V);
1225 EmitParmDecl(*Arg, V);
1226 break;
1227 }
1228
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001229 case ABIArgInfo::StructRet:
1230 assert(0 && "Invalid ABI kind for non-return argument");
1231 }
Daniel Dunbar56273772008-09-17 00:51:38 +00001232
1233 ++AI;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001234 }
1235 assert(AI == Fn->arg_end() && "Argument mismatch!");
1236}
1237
Daniel Dunbar88b53962009-02-02 22:03:45 +00001238void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI,
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001239 llvm::Value *ReturnValue) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001240 llvm::Value *RV = 0;
1241
1242 // Functions with no result always return void.
1243 if (ReturnValue) {
Daniel Dunbar88b53962009-02-02 22:03:45 +00001244 QualType RetTy = FI.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001245 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001246
1247 switch (RetAI.getKind()) {
1248 case ABIArgInfo::StructRet:
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001249 if (RetTy->isAnyComplexType()) {
1250 // FIXME: Volatile
1251 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
1252 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
1253 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1254 EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy);
1255 } else {
1256 Builder.CreateStore(Builder.CreateLoad(ReturnValue),
1257 CurFn->arg_begin());
1258 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001259 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001260
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001261 case ABIArgInfo::Direct:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001262 RV = Builder.CreateLoad(ReturnValue);
1263 break;
1264
Daniel Dunbar11434922009-01-26 21:26:08 +00001265 case ABIArgInfo::Ignore:
1266 break;
1267
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001268 case ABIArgInfo::Coerce: {
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +00001269 RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001270 break;
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001271 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001272
1273 case ABIArgInfo::ByVal:
1274 case ABIArgInfo::Expand:
1275 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001276 }
1277 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001278
1279 if (RV) {
1280 Builder.CreateRet(RV);
1281 } else {
1282 Builder.CreateRetVoid();
1283 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001284}
1285
Daniel Dunbar88b53962009-02-02 22:03:45 +00001286RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
1287 llvm::Value *Callee,
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001288 const CallArgList &CallArgs) {
Daniel Dunbar5251afa2009-02-03 06:02:10 +00001289 // FIXME: We no longer need the types from CallArgs; lift up and
1290 // simplify.
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001291 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001292
1293 // Handle struct-return functions by passing a pointer to the
1294 // location that we would like to return into.
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001295 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001296 const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001297 switch (RetAI.getKind()) {
1298 case ABIArgInfo::StructRet:
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001299 // Create a temporary alloca to hold the result of the call. :(
Daniel Dunbar56273772008-09-17 00:51:38 +00001300 Args.push_back(CreateTempAlloca(ConvertType(RetTy)));
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001301 break;
1302
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001303 case ABIArgInfo::Direct:
Daniel Dunbar11434922009-01-26 21:26:08 +00001304 case ABIArgInfo::Ignore:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001305 case ABIArgInfo::Coerce:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001306 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001307
1308 case ABIArgInfo::ByVal:
1309 case ABIArgInfo::Expand:
Daniel Dunbarb225be42009-02-03 05:59:18 +00001310 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001311 }
1312
Daniel Dunbarb225be42009-02-03 05:59:18 +00001313 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001314 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001315 I != E; ++I, ++info_it) {
1316 const ABIArgInfo &ArgInfo = info_it->info;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001317 RValue RV = I->first;
Daniel Dunbar56273772008-09-17 00:51:38 +00001318
1319 switch (ArgInfo.getKind()) {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001320 case ABIArgInfo::ByVal: // Direct is byval
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001321 case ABIArgInfo::Direct:
Daniel Dunbar56273772008-09-17 00:51:38 +00001322 if (RV.isScalar()) {
1323 Args.push_back(RV.getScalarVal());
1324 } else if (RV.isComplex()) {
1325 // Make a temporary alloca to pass the argument.
1326 Args.push_back(CreateTempAlloca(ConvertType(I->second)));
1327 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
1328 } else {
1329 Args.push_back(RV.getAggregateAddr());
1330 }
1331 break;
1332
Daniel Dunbar11434922009-01-26 21:26:08 +00001333 case ABIArgInfo::Ignore:
1334 break;
1335
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001336 case ABIArgInfo::Coerce: {
1337 // FIXME: Avoid the conversion through memory if possible.
1338 llvm::Value *SrcPtr;
1339 if (RV.isScalar()) {
1340 SrcPtr = CreateTempAlloca(ConvertType(I->second), "coerce");
1341 Builder.CreateStore(RV.getScalarVal(), SrcPtr);
1342 } else if (RV.isComplex()) {
1343 SrcPtr = CreateTempAlloca(ConvertType(I->second), "coerce");
1344 StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
1345 } else
1346 SrcPtr = RV.getAggregateAddr();
1347 Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
1348 *this));
1349 break;
1350 }
1351
Daniel Dunbar56273772008-09-17 00:51:38 +00001352 case ABIArgInfo::StructRet:
Daniel Dunbar56273772008-09-17 00:51:38 +00001353 assert(0 && "Invalid ABI kind for non-return argument");
1354 break;
1355
1356 case ABIArgInfo::Expand:
1357 ExpandTypeToArgs(I->second, RV, Args);
1358 break;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001359 }
1360 }
1361
1362 llvm::CallInst *CI = Builder.CreateCall(Callee,&Args[0],&Args[0]+Args.size());
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001363
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001364 // FIXME: Provide TargetDecl so nounwind, noreturn, etc, etc get set.
Devang Patel761d7f72008-09-25 21:02:23 +00001365 CodeGen::AttributeListType AttributeList;
Daniel Dunbar88b53962009-02-02 22:03:45 +00001366 CGM.ConstructAttributeList(CallInfo, 0, AttributeList);
Devang Patel761d7f72008-09-25 21:02:23 +00001367 CI->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(),
Daniel Dunbar725ad312009-01-31 02:19:00 +00001368 AttributeList.size()));
1369
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001370 if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee))
1371 CI->setCallingConv(F->getCallingConv());
1372 if (CI->getType() != llvm::Type::VoidTy)
1373 CI->setName("call");
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001374
1375 switch (RetAI.getKind()) {
1376 case ABIArgInfo::StructRet:
1377 if (RetTy->isAnyComplexType())
Daniel Dunbar56273772008-09-17 00:51:38 +00001378 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001379 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar56273772008-09-17 00:51:38 +00001380 return RValue::getAggregate(Args[0]);
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001381 else
1382 return RValue::get(Builder.CreateLoad(Args[0]));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001383
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001384 case ABIArgInfo::Direct:
1385 assert((!RetTy->isAnyComplexType() &&
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001386 !CodeGenFunction::hasAggregateLLVMType(RetTy)) &&
1387 "FIXME: Implement return for non-scalar direct types.");
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001388 return RValue::get(CI);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001389
Daniel Dunbar11434922009-01-26 21:26:08 +00001390 case ABIArgInfo::Ignore:
Daniel Dunbarcc039fe2009-01-29 08:24:57 +00001391 if (RetTy->isVoidType())
1392 return RValue::get(0);
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001393
1394 // If we are ignoring an argument that had a result, make sure to
1395 // construct the appropriate return value for our caller.
Daniel Dunbarcc039fe2009-01-29 08:24:57 +00001396 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1397 llvm::Value *Res =
1398 llvm::UndefValue::get(llvm::PointerType::getUnqual(ConvertType(RetTy)));
1399 return RValue::getAggregate(Res);
1400 }
1401 return RValue::get(llvm::UndefValue::get(ConvertType(RetTy)));
Daniel Dunbar11434922009-01-26 21:26:08 +00001402
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001403 case ABIArgInfo::Coerce: {
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001404 // FIXME: Avoid the conversion through memory if possible.
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +00001405 llvm::Value *V = CreateTempAlloca(ConvertType(RetTy), "coerce");
1406 CreateCoercedStore(CI, V, *this);
Anders Carlssonad3d6912008-11-25 22:21:48 +00001407 if (RetTy->isAnyComplexType())
1408 return RValue::getComplex(LoadComplexFromAddr(V, false));
Daniel Dunbar11434922009-01-26 21:26:08 +00001409 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssonad3d6912008-11-25 22:21:48 +00001410 return RValue::getAggregate(V);
Daniel Dunbar11434922009-01-26 21:26:08 +00001411 else
1412 return RValue::get(Builder.CreateLoad(V));
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001413 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001414
1415 case ABIArgInfo::ByVal:
1416 case ABIArgInfo::Expand:
1417 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001418 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001419
1420 assert(0 && "Unhandled ABIArgInfo::Kind");
1421 return RValue::get(0);
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001422}