blob: 6f7d6f696fcc41de2f0b214a656029530f46c018 [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
Daniel Dunbar88c2fa92009-02-03 05:31:23 +000090static ABIArgInfo getABIReturnInfo(QualType Ty, CodeGenTypes &CGT);
91static ABIArgInfo getABIArgumentInfo(QualType Ty, CodeGenTypes &CGT);
92
Daniel Dunbar541b63b2009-02-02 23:23:47 +000093const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
94 const llvm::SmallVector<QualType, 16> &ArgTys) {
Daniel Dunbar40a6be62009-02-03 00:07:12 +000095 // Lookup or create unique function info.
96 llvm::FoldingSetNodeID ID;
97 CGFunctionInfo::Profile(ID, ResTy, ArgTys.begin(), ArgTys.end());
98
99 void *InsertPos = 0;
100 CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, InsertPos);
101 if (FI)
102 return *FI;
103
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000104 // Construct the function info.
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000105 FI = new CGFunctionInfo(ResTy, ArgTys);
106 FunctionInfos.InsertNode(FI, InsertPos);
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000107
108 // Compute ABI information.
109 FI->getReturnInfo() = getABIReturnInfo(ResTy, *this);
110 for (CGFunctionInfo::arg_iterator it = FI->arg_begin(), ie = FI->arg_end();
111 it != ie; ++it)
112 it->info = getABIArgumentInfo(it->type, *this);
113
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000114 return *FI;
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000115}
116
117/***/
118
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000119ABIInfo::~ABIInfo() {}
120
Daniel Dunbar834af452008-09-17 21:22:33 +0000121/// isEmptyStruct - Return true iff a structure has no non-empty
122/// members. Note that a structure with a flexible array member is not
123/// considered empty.
124static bool isEmptyStruct(QualType T) {
125 const RecordType *RT = T->getAsStructureType();
126 if (!RT)
127 return 0;
128 const RecordDecl *RD = RT->getDecl();
129 if (RD->hasFlexibleArrayMember())
130 return false;
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000131 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar834af452008-09-17 21:22:33 +0000132 e = RD->field_end(); i != e; ++i) {
133 const FieldDecl *FD = *i;
134 if (!isEmptyStruct(FD->getType()))
135 return false;
136 }
137 return true;
138}
139
140/// isSingleElementStruct - Determine if a structure is a "single
141/// element struct", i.e. it has exactly one non-empty field or
142/// exactly one field which is itself a single element
143/// struct. Structures with flexible array members are never
144/// considered single element structs.
145///
146/// \return The field declaration for the single non-empty field, if
147/// it exists.
148static const FieldDecl *isSingleElementStruct(QualType T) {
149 const RecordType *RT = T->getAsStructureType();
150 if (!RT)
151 return 0;
152
153 const RecordDecl *RD = RT->getDecl();
154 if (RD->hasFlexibleArrayMember())
155 return 0;
156
157 const FieldDecl *Found = 0;
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000158 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar834af452008-09-17 21:22:33 +0000159 e = RD->field_end(); i != e; ++i) {
160 const FieldDecl *FD = *i;
161 QualType FT = FD->getType();
162
163 if (isEmptyStruct(FT)) {
164 // Ignore
165 } else if (Found) {
166 return 0;
167 } else if (!CodeGenFunction::hasAggregateLLVMType(FT)) {
168 Found = FD;
169 } else {
170 Found = isSingleElementStruct(FT);
171 if (!Found)
172 return 0;
173 }
174 }
175
176 return Found;
177}
178
179static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
180 if (!Ty->getAsBuiltinType() && !Ty->isPointerType())
181 return false;
182
183 uint64_t Size = Context.getTypeSize(Ty);
184 return Size == 32 || Size == 64;
185}
186
187static bool areAllFields32Or64BitBasicType(const RecordDecl *RD,
188 ASTContext &Context) {
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000189 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar834af452008-09-17 21:22:33 +0000190 e = RD->field_end(); i != e; ++i) {
191 const FieldDecl *FD = *i;
192
193 if (!is32Or64BitBasicType(FD->getType(), Context))
194 return false;
195
196 // If this is a bit-field we need to make sure it is still a
197 // 32-bit or 64-bit type.
198 if (Expr *BW = FD->getBitWidth()) {
199 unsigned Width = BW->getIntegerConstantExprValue(Context).getZExtValue();
200 if (Width <= 16)
201 return false;
202 }
203 }
204 return true;
205}
206
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000207namespace {
208/// DefaultABIInfo - The default implementation for ABI specific
209/// details. This implementation provides information which results in
210/// sensible LLVM IR generation, but does not conform to any
211/// particular ABI.
212class DefaultABIInfo : public ABIInfo {
213 virtual ABIArgInfo classifyReturnType(QualType RetTy,
214 ASTContext &Context) const;
215
216 virtual ABIArgInfo classifyArgumentType(QualType RetTy,
217 ASTContext &Context) const;
218};
219
220/// X86_32ABIInfo - The X86-32 ABI information.
221class X86_32ABIInfo : public ABIInfo {
222public:
223 virtual ABIArgInfo classifyReturnType(QualType RetTy,
224 ASTContext &Context) const;
225
226 virtual ABIArgInfo classifyArgumentType(QualType RetTy,
227 ASTContext &Context) const;
228};
229}
230
231ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
232 ASTContext &Context) const {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000233 if (RetTy->isVoidType()) {
234 return ABIArgInfo::getIgnore();
235 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar834af452008-09-17 21:22:33 +0000236 // Classify "single element" structs as their element type.
237 const FieldDecl *SeltFD = isSingleElementStruct(RetTy);
238 if (SeltFD) {
239 QualType SeltTy = SeltFD->getType()->getDesugaredType();
240 if (const BuiltinType *BT = SeltTy->getAsBuiltinType()) {
241 // FIXME: This is gross, it would be nice if we could just
242 // pass back SeltTy and have clients deal with it. Is it worth
243 // supporting coerce to both LLVM and clang Types?
244 if (BT->isIntegerType()) {
245 uint64_t Size = Context.getTypeSize(SeltTy);
246 return ABIArgInfo::getCoerce(llvm::IntegerType::get((unsigned) Size));
247 } else if (BT->getKind() == BuiltinType::Float) {
248 return ABIArgInfo::getCoerce(llvm::Type::FloatTy);
249 } else if (BT->getKind() == BuiltinType::Double) {
250 return ABIArgInfo::getCoerce(llvm::Type::DoubleTy);
251 }
252 } else if (SeltTy->isPointerType()) {
253 // FIXME: It would be really nice if this could come out as
254 // the proper pointer type.
255 llvm::Type *PtrTy =
256 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
257 return ABIArgInfo::getCoerce(PtrTy);
258 }
259 }
260
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000261 uint64_t Size = Context.getTypeSize(RetTy);
262 if (Size == 8) {
263 return ABIArgInfo::getCoerce(llvm::Type::Int8Ty);
264 } else if (Size == 16) {
265 return ABIArgInfo::getCoerce(llvm::Type::Int16Ty);
266 } else if (Size == 32) {
267 return ABIArgInfo::getCoerce(llvm::Type::Int32Ty);
268 } else if (Size == 64) {
269 return ABIArgInfo::getCoerce(llvm::Type::Int64Ty);
270 } else {
271 return ABIArgInfo::getStructRet();
272 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000273 } else {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000274 return ABIArgInfo::getDirect();
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000275 }
276}
277
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000278ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
279 ASTContext &Context) const {
Daniel Dunbarf0357382008-09-17 20:11:04 +0000280 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar834af452008-09-17 21:22:33 +0000281 // Structures with flexible arrays are always byval.
282 if (const RecordType *RT = Ty->getAsStructureType())
283 if (RT->getDecl()->hasFlexibleArrayMember())
284 return ABIArgInfo::getByVal(0);
285
286 // Expand empty structs (i.e. ignore)
287 uint64_t Size = Context.getTypeSize(Ty);
288 if (Ty->isStructureType() && Size == 0)
289 return ABIArgInfo::getExpand();
290
291 // Expand structs with size <= 128-bits which consist only of
292 // basic types (int, long long, float, double, xxx*). This is
293 // non-recursive and does not ignore empty fields.
294 if (const RecordType *RT = Ty->getAsStructureType()) {
295 if (Context.getTypeSize(Ty) <= 4*32 &&
296 areAllFields32Or64BitBasicType(RT->getDecl(), Context))
297 return ABIArgInfo::getExpand();
298 }
299
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000300 return ABIArgInfo::getByVal(0);
301 } else {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000302 return ABIArgInfo::getDirect();
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000303 }
304}
305
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000306namespace {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000307/// X86_64ABIInfo - The X86_64 ABI information.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000308class X86_64ABIInfo : public ABIInfo {
309 enum Class {
310 Integer = 0,
311 SSE,
312 SSEUp,
313 X87,
314 X87Up,
315 ComplexX87,
316 NoClass,
317 Memory
318 };
319
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000320 /// merge - Implement the X86_64 ABI merging algorithm.
321 ///
Daniel Dunbarc4503572009-01-31 00:06:58 +0000322 /// Merge an accumulating classification \arg Accum with a field
323 /// classification \arg Field.
324 ///
325 /// \param Accum - The accumulating classification. This should
326 /// always be either NoClass or the result of a previous merge
327 /// call. In addition, this should never be Memory (the caller
328 /// should just return Memory for the aggregate).
329 Class merge(Class Accum, Class Field) const;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000330
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000331 /// classify - Determine the x86_64 register classes in which the
332 /// given type T should be passed.
333 ///
Daniel Dunbarc4503572009-01-31 00:06:58 +0000334 /// \param Lo - The classification for the parts of the type
335 /// residing in the low word of the containing object.
336 ///
337 /// \param Hi - The classification for the parts of the type
338 /// residing in the high word of the containing object.
339 ///
340 /// \param OffsetBase - The bit offset of this type in the
Daniel Dunbarcdf920e2009-01-30 22:40:15 +0000341 /// containing object. Some parameters are classified different
342 /// depending on whether they straddle an eightbyte boundary.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000343 ///
344 /// If a word is unused its result will be NoClass; if a type should
345 /// be passed in Memory then at least the classification of \arg Lo
346 /// will be Memory.
347 ///
348 /// The \arg Lo class will be NoClass iff the argument is ignored.
349 ///
350 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
351 /// be NoClass.
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000352 void classify(QualType T, ASTContext &Context, uint64_t OffsetBase,
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000353 Class &Lo, Class &Hi) const;
Daniel Dunbarc4503572009-01-31 00:06:58 +0000354
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000355public:
356 virtual ABIArgInfo classifyReturnType(QualType RetTy,
357 ASTContext &Context) const;
358
359 virtual ABIArgInfo classifyArgumentType(QualType RetTy,
360 ASTContext &Context) const;
361};
362}
363
Daniel Dunbarc4503572009-01-31 00:06:58 +0000364X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum,
365 Class Field) const {
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000366 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
367 // classified recursively so that always two fields are
368 // considered. The resulting class is calculated according to
369 // the classes of the fields in the eightbyte:
370 //
371 // (a) If both classes are equal, this is the resulting class.
372 //
373 // (b) If one of the classes is NO_CLASS, the resulting class is
374 // the other class.
375 //
376 // (c) If one of the classes is MEMORY, the result is the MEMORY
377 // class.
378 //
379 // (d) If one of the classes is INTEGER, the result is the
380 // INTEGER.
381 //
382 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
383 // MEMORY is used as class.
384 //
385 // (f) Otherwise class SSE is used.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000386 assert((Accum == NoClass || Accum == Integer ||
387 Accum == SSE || Accum == SSEUp) &&
388 "Invalid accumulated classification during merge.");
389 if (Accum == Field || Field == NoClass)
390 return Accum;
391 else if (Field == Memory)
392 return Memory;
393 else if (Accum == NoClass)
394 return Field;
395 else if (Accum == Integer || Field == Integer)
396 return Integer;
397 else if (Field == X87 || Field == X87Up || Field == ComplexX87)
398 return Memory;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000399 else
Daniel Dunbarc4503572009-01-31 00:06:58 +0000400 return SSE;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000401}
402
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000403void X86_64ABIInfo::classify(QualType Ty,
404 ASTContext &Context,
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000405 uint64_t OffsetBase,
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000406 Class &Lo, Class &Hi) const {
Daniel Dunbar9a82b522009-02-02 18:06:39 +0000407 // FIXME: This code can be simplified by introducing a simple value
408 // class for Class pairs with appropriate constructor methods for
409 // the various situations.
410
Daniel Dunbarc4503572009-01-31 00:06:58 +0000411 Lo = Hi = NoClass;
412
413 Class &Current = OffsetBase < 64 ? Lo : Hi;
414 Current = Memory;
415
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000416 if (const BuiltinType *BT = Ty->getAsBuiltinType()) {
417 BuiltinType::Kind k = BT->getKind();
418
Daniel Dunbar11434922009-01-26 21:26:08 +0000419 if (k == BuiltinType::Void) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000420 Current = NoClass;
Daniel Dunbar11434922009-01-26 21:26:08 +0000421 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000422 Current = Integer;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000423 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000424 Current = SSE;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000425 } else if (k == BuiltinType::LongDouble) {
426 Lo = X87;
427 Hi = X87Up;
428 }
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000429 // FIXME: _Decimal32 and _Decimal64 are SSE.
430 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000431 // FIXME: __int128 is (Integer, Integer).
432 } else if (Ty->isPointerLikeType() || Ty->isBlockPointerType() ||
433 Ty->isObjCQualifiedInterfaceType()) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000434 Current = Integer;
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000435 } else if (const VectorType *VT = Ty->getAsVectorType()) {
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000436 uint64_t Size = Context.getTypeSize(VT);
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000437 if (Size == 64) {
Daniel Dunbard4cd1b02009-01-30 19:38:39 +0000438 // gcc passes <1 x double> in memory.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000439 if (VT->getElementType() == Context.DoubleTy)
Daniel Dunbard4cd1b02009-01-30 19:38:39 +0000440 return;
Daniel Dunbard4cd1b02009-01-30 19:38:39 +0000441
Daniel Dunbarc4503572009-01-31 00:06:58 +0000442 Current = SSE;
Daniel Dunbare33edf12009-01-30 18:40:10 +0000443
444 // If this type crosses an eightbyte boundary, it should be
445 // split.
Daniel Dunbarcdf920e2009-01-30 22:40:15 +0000446 if (OffsetBase && OffsetBase != 64)
Daniel Dunbare33edf12009-01-30 18:40:10 +0000447 Hi = Lo;
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000448 } else if (Size == 128) {
449 Lo = SSE;
450 Hi = SSEUp;
451 }
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000452 } else if (const ComplexType *CT = Ty->getAsComplexType()) {
453 QualType ET = CT->getElementType();
454
Daniel Dunbare33edf12009-01-30 18:40:10 +0000455 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbareac48dc2009-01-29 07:22:20 +0000456 if (ET->isIntegerType()) {
Daniel Dunbareac48dc2009-01-29 07:22:20 +0000457 if (Size <= 64)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000458 Current = Integer;
Daniel Dunbareac48dc2009-01-29 07:22:20 +0000459 else if (Size <= 128)
460 Lo = Hi = Integer;
461 } else if (ET == Context.FloatTy)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000462 Current = SSE;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000463 else if (ET == Context.DoubleTy)
464 Lo = Hi = SSE;
465 else if (ET == Context.LongDoubleTy)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000466 Current = ComplexX87;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000467
468 // If this complex type crosses an eightbyte boundary then it
469 // should be split.
Daniel Dunbarcdf920e2009-01-30 22:40:15 +0000470 uint64_t EB_Real = (OffsetBase) / 64;
471 uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000472 if (Hi == NoClass && EB_Real != EB_Imag)
473 Hi = Lo;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000474 } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
475 // Arrays are treated like structures.
476
477 uint64_t Size = Context.getTypeSize(Ty);
478
479 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
480 // than two eightbytes, ..., it has class MEMORY.
481 if (Size > 128)
482 return;
483
484 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
485 // fields, it has class MEMORY.
486 //
487 // Only need to check alignment of array base.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000488 if (OffsetBase % Context.getTypeAlign(AT->getElementType()))
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000489 return;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000490
491 // Otherwise implement simplified merge. We could be smarter about
492 // this, but it isn't worth it and would be harder to verify.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000493 Current = NoClass;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000494 uint64_t EltSize = Context.getTypeSize(AT->getElementType());
495 uint64_t ArraySize = AT->getSize().getZExtValue();
496 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
497 Class FieldLo, FieldHi;
498 classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbarc4503572009-01-31 00:06:58 +0000499 Lo = merge(Lo, FieldLo);
500 Hi = merge(Hi, FieldHi);
501 if (Lo == Memory || Hi == Memory)
502 break;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000503 }
Daniel Dunbarc4503572009-01-31 00:06:58 +0000504
505 // Do post merger cleanup (see below). Only case we worry about is Memory.
506 if (Hi == Memory)
507 Lo = Memory;
508 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Daniel Dunbar99037e52009-01-29 08:13:58 +0000509 } else if (const RecordType *RT = Ty->getAsRecordType()) {
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000510 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbar99037e52009-01-29 08:13:58 +0000511
512 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
513 // than two eightbytes, ..., it has class MEMORY.
514 if (Size > 128)
515 return;
516
517 const RecordDecl *RD = RT->getDecl();
518
519 // Assume variable sized types are passed in memory.
520 if (RD->hasFlexibleArrayMember())
521 return;
522
523 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
524
525 // Reset Lo class, this will be recomputed.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000526 Current = NoClass;
Daniel Dunbar99037e52009-01-29 08:13:58 +0000527 unsigned idx = 0;
528 for (RecordDecl::field_iterator i = RD->field_begin(),
529 e = RD->field_end(); i != e; ++i, ++idx) {
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000530 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Daniel Dunbar99037e52009-01-29 08:13:58 +0000531
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000532 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
533 // fields, it has class MEMORY.
Daniel Dunbar99037e52009-01-29 08:13:58 +0000534 if (Offset % Context.getTypeAlign(i->getType())) {
535 Lo = Memory;
536 return;
537 }
538
Daniel Dunbar99037e52009-01-29 08:13:58 +0000539 // Classify this field.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000540 //
541 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
542 // exceeds a single eightbyte, each is classified
543 // separately. Each eightbyte gets initialized to class
544 // NO_CLASS.
Daniel Dunbar99037e52009-01-29 08:13:58 +0000545 Class FieldLo, FieldHi;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000546 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbarc4503572009-01-31 00:06:58 +0000547 Lo = merge(Lo, FieldLo);
548 Hi = merge(Hi, FieldHi);
549 if (Lo == Memory || Hi == Memory)
550 break;
Daniel Dunbar99037e52009-01-29 08:13:58 +0000551 }
552
553 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
554 //
555 // (a) If one of the classes is MEMORY, the whole argument is
556 // passed in memory.
557 //
558 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
559
560 // The first of these conditions is guaranteed by how we implement
Daniel Dunbarc4503572009-01-31 00:06:58 +0000561 // the merge (just bail).
562 //
563 // The second condition occurs in the case of unions; for example
564 // union { _Complex double; unsigned; }.
565 if (Hi == Memory)
566 Lo = Memory;
Daniel Dunbar99037e52009-01-29 08:13:58 +0000567 if (Hi == SSEUp && Lo != SSE)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000568 Hi = SSE;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000569 }
570}
571
Daniel Dunbarc4503572009-01-31 00:06:58 +0000572
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000573ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
574 ASTContext &Context) const {
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000575 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
576 // classification algorithm.
577 X86_64ABIInfo::Class Lo, Hi;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000578 classify(RetTy, Context, 0, Lo, Hi);
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000579
Daniel Dunbarc4503572009-01-31 00:06:58 +0000580 // Check some invariants.
581 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
582 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
583 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
584
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000585 const llvm::Type *ResType = 0;
586 switch (Lo) {
587 case NoClass:
Daniel Dunbar11434922009-01-26 21:26:08 +0000588 return ABIArgInfo::getIgnore();
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000589
590 case SSEUp:
591 case X87Up:
592 assert(0 && "Invalid classification for lo word.");
593
Daniel Dunbarc4503572009-01-31 00:06:58 +0000594 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
595 // hidden argument, i.e. structret.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000596 case Memory:
597 return ABIArgInfo::getStructRet();
598
599 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
600 // available register of the sequence %rax, %rdx is used.
601 case Integer:
602 ResType = llvm::Type::Int64Ty; break;
603
604 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
605 // available SSE register of the sequence %xmm0, %xmm1 is used.
606 case SSE:
607 ResType = llvm::Type::DoubleTy; break;
608
609 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
610 // returned on the X87 stack in %st0 as 80-bit x87 number.
611 case X87:
612 ResType = llvm::Type::X86_FP80Ty; break;
613
Daniel Dunbarc4503572009-01-31 00:06:58 +0000614 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
615 // part of the value is returned in %st0 and the imaginary part in
616 // %st1.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000617 case ComplexX87:
618 assert(Hi == NoClass && "Unexpected ComplexX87 classification.");
619 ResType = llvm::VectorType::get(llvm::Type::X86_FP80Ty, 2);
620 break;
621 }
622
623 switch (Hi) {
624 // Memory was handled previously, and ComplexX87 and X87 should
625 // never occur as hi classes.
626 case Memory:
627 case X87:
628 case ComplexX87:
629 assert(0 && "Invalid classification for hi word.");
630
631 case NoClass: break;
632 case Integer:
Daniel Dunbarb0e14f22009-01-29 07:36:07 +0000633 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
634 break;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000635 case SSE:
Daniel Dunbarb0e14f22009-01-29 07:36:07 +0000636 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
637 break;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000638
639 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
640 // is passed in the upper half of the last used SSE register.
641 //
642 // SSEUP should always be preceeded by SSE, just widen.
643 case SSEUp:
644 assert(Lo == SSE && "Unexpected SSEUp classification.");
645 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
646 break;
647
648 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
Daniel Dunbarb0e14f22009-01-29 07:36:07 +0000649 // returned together with the previous X87 value in %st0.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000650 //
651 // X87UP should always be preceeded by X87, so we don't need to do
652 // anything here.
653 case X87Up:
654 assert(Lo == X87 && "Unexpected X87Up classification.");
655 break;
656 }
657
658 return ABIArgInfo::getCoerce(ResType);
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000659}
660
661ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty,
662 ASTContext &Context) const {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000663 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
664 return ABIArgInfo::getByVal(0);
665 } else {
666 return ABIArgInfo::getDirect();
667 }
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000668}
669
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000670ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
671 ASTContext &Context) const {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000672 if (RetTy->isVoidType()) {
673 return ABIArgInfo::getIgnore();
674 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
675 return ABIArgInfo::getStructRet();
676 } else {
677 return ABIArgInfo::getDirect();
678 }
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000679}
680
681ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
682 ASTContext &Context) const {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000683 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
684 return ABIArgInfo::getByVal(0);
685 } else {
686 return ABIArgInfo::getDirect();
687 }
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000688}
689
690const ABIInfo &CodeGenTypes::getABIInfo() const {
691 if (TheABIInfo)
692 return *TheABIInfo;
693
694 // For now we just cache this in the CodeGenTypes and don't bother
695 // to free it.
696 const char *TargetPrefix = getContext().Target.getTargetPrefix();
697 if (strcmp(TargetPrefix, "x86") == 0) {
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000698 switch (getContext().Target.getPointerWidth(0)) {
699 case 32:
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000700 return *(TheABIInfo = new X86_32ABIInfo());
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000701 case 64:
Daniel Dunbar11a76ed2009-01-30 18:47:53 +0000702 return *(TheABIInfo = new X86_64ABIInfo());
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000703 }
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000704 }
705
706 return *(TheABIInfo = new DefaultABIInfo);
707}
708
709// getABIReturnInfo - Wrap the ABIInfo getABIReturnInfo, altering
710// "default" types to StructRet when appropriate for simplicity.
711static ABIArgInfo getABIReturnInfo(QualType Ty, CodeGenTypes &CGT) {
712 assert(!Ty->isArrayType() &&
713 "Array types cannot be passed directly.");
714 ABIArgInfo Info = CGT.getABIInfo().classifyReturnType(Ty, CGT.getContext());
Daniel Dunbarf0357382008-09-17 20:11:04 +0000715 return Info;
716}
717
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000718// getABIArgumentInfo - Wrap the ABIInfo getABIReturnInfo, altering
719// "default" types to ByVal when appropriate for simplicity.
720static ABIArgInfo getABIArgumentInfo(QualType Ty, CodeGenTypes &CGT) {
721 assert(!Ty->isArrayType() &&
722 "Array types cannot be passed directly.");
723 ABIArgInfo Info = CGT.getABIInfo().classifyArgumentType(Ty, CGT.getContext());
Daniel Dunbarf0357382008-09-17 20:11:04 +0000724 return Info;
725}
726
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000727/***/
728
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000729CGFunctionInfo::CGFunctionInfo(QualType ResTy,
730 const llvm::SmallVector<QualType, 16> &ArgTys) {
731 NumArgs = ArgTys.size();
732 Args = new ArgInfo[1 + NumArgs];
733 Args[0].type = ResTy;
734 for (unsigned i = 0; i < NumArgs; ++i)
735 Args[1 + i].type = ArgTys[i];
736}
737
738/***/
739
Daniel Dunbar56273772008-09-17 00:51:38 +0000740void CodeGenTypes::GetExpandedTypes(QualType Ty,
741 std::vector<const llvm::Type*> &ArgTys) {
742 const RecordType *RT = Ty->getAsStructureType();
743 assert(RT && "Can only expand structure types.");
744 const RecordDecl *RD = RT->getDecl();
745 assert(!RD->hasFlexibleArrayMember() &&
746 "Cannot expand structure with flexible array.");
747
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000748 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar56273772008-09-17 00:51:38 +0000749 e = RD->field_end(); i != e; ++i) {
750 const FieldDecl *FD = *i;
751 assert(!FD->isBitField() &&
752 "Cannot expand structure with bit-field members.");
753
754 QualType FT = FD->getType();
755 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
756 GetExpandedTypes(FT, ArgTys);
757 } else {
758 ArgTys.push_back(ConvertType(FT));
759 }
760 }
761}
762
763llvm::Function::arg_iterator
764CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
765 llvm::Function::arg_iterator AI) {
766 const RecordType *RT = Ty->getAsStructureType();
767 assert(RT && "Can only expand structure types.");
768
769 RecordDecl *RD = RT->getDecl();
770 assert(LV.isSimple() &&
771 "Unexpected non-simple lvalue during struct expansion.");
772 llvm::Value *Addr = LV.getAddress();
773 for (RecordDecl::field_iterator i = RD->field_begin(),
774 e = RD->field_end(); i != e; ++i) {
775 FieldDecl *FD = *i;
776 QualType FT = FD->getType();
777
778 // FIXME: What are the right qualifiers here?
779 LValue LV = EmitLValueForField(Addr, FD, false, 0);
780 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
781 AI = ExpandTypeFromArgs(FT, LV, AI);
782 } else {
783 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
784 ++AI;
785 }
786 }
787
788 return AI;
789}
790
791void
792CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
793 llvm::SmallVector<llvm::Value*, 16> &Args) {
794 const RecordType *RT = Ty->getAsStructureType();
795 assert(RT && "Can only expand structure types.");
796
797 RecordDecl *RD = RT->getDecl();
798 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
799 llvm::Value *Addr = RV.getAggregateAddr();
800 for (RecordDecl::field_iterator i = RD->field_begin(),
801 e = RD->field_end(); i != e; ++i) {
802 FieldDecl *FD = *i;
803 QualType FT = FD->getType();
804
805 // FIXME: What are the right qualifiers here?
806 LValue LV = EmitLValueForField(Addr, FD, false, 0);
807 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
808 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
809 } else {
810 RValue RV = EmitLoadOfLValue(LV, FT);
811 assert(RV.isScalar() &&
812 "Unexpected non-scalar rvalue during struct expansion.");
813 Args.push_back(RV.getScalarVal());
814 }
815 }
816}
817
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000818/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
819/// a pointer to an object of type \arg Ty.
820///
821/// This safely handles the case when the src type is smaller than the
822/// destination type; in this situation the values of bits which not
823/// present in the src are undefined.
824static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
825 const llvm::Type *Ty,
826 CodeGenFunction &CGF) {
827 const llvm::Type *SrcTy =
828 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
829 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
830 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(Ty);
831
Daniel Dunbarb225be42009-02-03 05:59:18 +0000832 // If load is legal, just bitcast the src pointer.
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000833 if (SrcSize == DstSize) {
834 llvm::Value *Casted =
835 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
836 return CGF.Builder.CreateLoad(Casted);
837 } else {
838 assert(SrcSize < DstSize && "Coercion is losing source bits!");
839
840 // Otherwise do coercion through memory. This is stupid, but
841 // simple.
842 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
843 llvm::Value *Casted =
844 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
845 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
846 return CGF.Builder.CreateLoad(Tmp);
847 }
848}
849
850/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
851/// where the source and destination may have different types.
852///
853/// This safely handles the case when the src type is larger than the
854/// destination type; the upper bits of the src will be lost.
855static void CreateCoercedStore(llvm::Value *Src,
856 llvm::Value *DstPtr,
857 CodeGenFunction &CGF) {
858 const llvm::Type *SrcTy = Src->getType();
859 const llvm::Type *DstTy =
860 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
861
862 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
863 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(DstTy);
864
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000865 // If store is legal, just bitcast the src pointer.
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000866 if (SrcSize == DstSize) {
867 llvm::Value *Casted =
868 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
869 CGF.Builder.CreateStore(Src, Casted);
870 } else {
871 assert(SrcSize > DstSize && "Coercion is missing bits!");
872
873 // Otherwise do coercion through memory. This is stupid, but
874 // simple.
875 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
876 CGF.Builder.CreateStore(Src, Tmp);
877 llvm::Value *Casted =
878 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
879 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(Casted), DstPtr);
880 }
881}
882
Daniel Dunbar56273772008-09-17 00:51:38 +0000883/***/
884
Daniel Dunbar88b53962009-02-02 22:03:45 +0000885bool CodeGenModule::ReturnTypeUsesSret(const CGFunctionInfo &FI) {
Daniel Dunbarb225be42009-02-03 05:59:18 +0000886 return FI.getReturnInfo().isStructRet();
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000887}
888
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000889const llvm::FunctionType *
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000890CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic) {
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000891 std::vector<const llvm::Type*> ArgTys;
892
893 const llvm::Type *ResultType = 0;
894
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000895 QualType RetTy = FI.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000896 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000897 switch (RetAI.getKind()) {
898 case ABIArgInfo::ByVal:
899 case ABIArgInfo::Expand:
900 assert(0 && "Invalid ABI kind for return argument");
901
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000902 case ABIArgInfo::Direct:
903 ResultType = ConvertType(RetTy);
904 break;
905
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000906 case ABIArgInfo::StructRet: {
907 ResultType = llvm::Type::VoidTy;
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000908 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000909 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
910 break;
911 }
912
Daniel Dunbar11434922009-01-26 21:26:08 +0000913 case ABIArgInfo::Ignore:
914 ResultType = llvm::Type::VoidTy;
915 break;
916
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000917 case ABIArgInfo::Coerce:
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000918 ResultType = RetAI.getCoerceToType();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000919 break;
920 }
921
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000922 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
923 ie = FI.arg_end(); it != ie; ++it) {
924 const ABIArgInfo &AI = it->info;
925 const llvm::Type *Ty = ConvertType(it->type);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000926
927 switch (AI.getKind()) {
Daniel Dunbar11434922009-01-26 21:26:08 +0000928 case ABIArgInfo::Ignore:
929 break;
930
Daniel Dunbar56273772008-09-17 00:51:38 +0000931 case ABIArgInfo::Coerce:
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000932 case ABIArgInfo::StructRet:
933 assert(0 && "Invalid ABI kind for non-return argument");
934
935 case ABIArgInfo::ByVal:
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000936 // byval arguments are always on the stack, which is addr space #0.
937 ArgTys.push_back(llvm::PointerType::getUnqual(Ty));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000938 assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled");
939 break;
940
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000941 case ABIArgInfo::Direct:
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000942 ArgTys.push_back(Ty);
943 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000944
945 case ABIArgInfo::Expand:
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000946 GetExpandedTypes(it->type, ArgTys);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000947 break;
948 }
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000949 }
950
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000951 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar3913f182008-09-09 23:48:28 +0000952}
953
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000954void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
Daniel Dunbar88b53962009-02-02 22:03:45 +0000955 const Decl *TargetDecl,
Devang Patel761d7f72008-09-25 21:02:23 +0000956 AttributeListType &PAL) {
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000957 unsigned FuncAttrs = 0;
Devang Patela2c69122008-09-26 22:53:57 +0000958 unsigned RetAttrs = 0;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000959
960 if (TargetDecl) {
961 if (TargetDecl->getAttr<NoThrowAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +0000962 FuncAttrs |= llvm::Attribute::NoUnwind;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000963 if (TargetDecl->getAttr<NoReturnAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +0000964 FuncAttrs |= llvm::Attribute::NoReturn;
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000965 if (TargetDecl->getAttr<PureAttr>())
966 FuncAttrs |= llvm::Attribute::ReadOnly;
967 if (TargetDecl->getAttr<ConstAttr>())
968 FuncAttrs |= llvm::Attribute::ReadNone;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000969 }
970
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000971 QualType RetTy = FI.getReturnType();
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000972 unsigned Index = 1;
Daniel Dunbarb225be42009-02-03 05:59:18 +0000973 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000974 switch (RetAI.getKind()) {
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000975 case ABIArgInfo::Direct:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000976 if (RetTy->isPromotableIntegerType()) {
977 if (RetTy->isSignedIntegerType()) {
Devang Patela2c69122008-09-26 22:53:57 +0000978 RetAttrs |= llvm::Attribute::SExt;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000979 } else if (RetTy->isUnsignedIntegerType()) {
Devang Patela2c69122008-09-26 22:53:57 +0000980 RetAttrs |= llvm::Attribute::ZExt;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000981 }
982 }
983 break;
984
985 case ABIArgInfo::StructRet:
Devang Patel761d7f72008-09-25 21:02:23 +0000986 PAL.push_back(llvm::AttributeWithIndex::get(Index,
Daniel Dunbar725ad312009-01-31 02:19:00 +0000987 llvm::Attribute::StructRet |
988 llvm::Attribute::NoAlias));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000989 ++Index;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000990 break;
991
Daniel Dunbar11434922009-01-26 21:26:08 +0000992 case ABIArgInfo::Ignore:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000993 case ABIArgInfo::Coerce:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000994 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000995
996 case ABIArgInfo::ByVal:
997 case ABIArgInfo::Expand:
998 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000999 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001000
Devang Patela2c69122008-09-26 22:53:57 +00001001 if (RetAttrs)
1002 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Daniel Dunbar88c2fa92009-02-03 05:31:23 +00001003 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1004 ie = FI.arg_end(); it != ie; ++it) {
1005 QualType ParamType = it->type;
1006 const ABIArgInfo &AI = it->info;
Devang Patel761d7f72008-09-25 21:02:23 +00001007 unsigned Attributes = 0;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001008
1009 switch (AI.getKind()) {
1010 case ABIArgInfo::StructRet:
Daniel Dunbar56273772008-09-17 00:51:38 +00001011 case ABIArgInfo::Coerce:
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001012 assert(0 && "Invalid ABI kind for non-return argument");
1013
1014 case ABIArgInfo::ByVal:
Devang Patel761d7f72008-09-25 21:02:23 +00001015 Attributes |= llvm::Attribute::ByVal;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001016 assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled");
1017 break;
1018
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001019 case ABIArgInfo::Direct:
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001020 if (ParamType->isPromotableIntegerType()) {
1021 if (ParamType->isSignedIntegerType()) {
Devang Patel761d7f72008-09-25 21:02:23 +00001022 Attributes |= llvm::Attribute::SExt;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001023 } else if (ParamType->isUnsignedIntegerType()) {
Devang Patel761d7f72008-09-25 21:02:23 +00001024 Attributes |= llvm::Attribute::ZExt;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001025 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001026 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001027 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001028
Daniel Dunbar11434922009-01-26 21:26:08 +00001029 case ABIArgInfo::Ignore:
1030 // Skip increment, no matching LLVM parameter.
1031 continue;
1032
Daniel Dunbar56273772008-09-17 00:51:38 +00001033 case ABIArgInfo::Expand: {
1034 std::vector<const llvm::Type*> Tys;
1035 // FIXME: This is rather inefficient. Do we ever actually need
1036 // to do anything here? The result should be just reconstructed
1037 // on the other side, so extension should be a non-issue.
1038 getTypes().GetExpandedTypes(ParamType, Tys);
1039 Index += Tys.size();
1040 continue;
1041 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001042 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001043
Devang Patel761d7f72008-09-25 21:02:23 +00001044 if (Attributes)
1045 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar56273772008-09-17 00:51:38 +00001046 ++Index;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001047 }
Devang Patela2c69122008-09-26 22:53:57 +00001048 if (FuncAttrs)
1049 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
1050
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001051}
1052
Daniel Dunbar88b53962009-02-02 22:03:45 +00001053void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
1054 llvm::Function *Fn,
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001055 const FunctionArgList &Args) {
Daniel Dunbar5251afa2009-02-03 06:02:10 +00001056 // FIXME: We no longer need the types from FunctionArgList; lift up
1057 // and simplify.
1058
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001059 // Emit allocs for param decls. Give the LLVM Argument nodes names.
1060 llvm::Function::arg_iterator AI = Fn->arg_begin();
1061
1062 // Name the struct return argument.
Daniel Dunbar88b53962009-02-02 22:03:45 +00001063 if (CGM.ReturnTypeUsesSret(FI)) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001064 AI->setName("agg.result");
1065 ++AI;
1066 }
Daniel Dunbarb225be42009-02-03 05:59:18 +00001067
1068 CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001069 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001070 i != e; ++i, ++info_it) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001071 const VarDecl *Arg = i->first;
Daniel Dunbarb225be42009-02-03 05:59:18 +00001072 QualType Ty = info_it->type;
1073 const ABIArgInfo &ArgI = info_it->info;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001074
1075 switch (ArgI.getKind()) {
1076 case ABIArgInfo::ByVal:
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001077 case ABIArgInfo::Direct: {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001078 assert(AI != Fn->arg_end() && "Argument mismatch!");
1079 llvm::Value* V = AI;
Daniel Dunbar56273772008-09-17 00:51:38 +00001080 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001081 // This must be a promotion, for something like
1082 // "void a(x) short x; {..."
Daniel Dunbar56273772008-09-17 00:51:38 +00001083 V = EmitScalarConversion(V, Ty, Arg->getType());
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001084 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001085 EmitParmDecl(*Arg, V);
1086 break;
1087 }
Daniel Dunbar56273772008-09-17 00:51:38 +00001088
1089 case ABIArgInfo::Expand: {
Daniel Dunbarb225be42009-02-03 05:59:18 +00001090 // If this structure was expanded into multiple arguments then
Daniel Dunbar56273772008-09-17 00:51:38 +00001091 // we need to create a temporary and reconstruct it from the
1092 // arguments.
Chris Lattner39f34e92008-11-24 04:00:27 +00001093 std::string Name = Arg->getNameAsString();
Daniel Dunbar56273772008-09-17 00:51:38 +00001094 llvm::Value *Temp = CreateTempAlloca(ConvertType(Ty),
1095 (Name + ".addr").c_str());
1096 // FIXME: What are the right qualifiers here?
1097 llvm::Function::arg_iterator End =
1098 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI);
1099 EmitParmDecl(*Arg, Temp);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001100
Daniel Dunbar56273772008-09-17 00:51:38 +00001101 // Name the arguments used in expansion and increment AI.
1102 unsigned Index = 0;
1103 for (; AI != End; ++AI, ++Index)
1104 AI->setName(Name + "." + llvm::utostr(Index));
1105 continue;
1106 }
Daniel Dunbar11434922009-01-26 21:26:08 +00001107
1108 case ABIArgInfo::Ignore:
1109 break;
1110
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001111 case ABIArgInfo::Coerce:
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001112 case ABIArgInfo::StructRet:
1113 assert(0 && "Invalid ABI kind for non-return argument");
1114 }
Daniel Dunbar56273772008-09-17 00:51:38 +00001115
1116 ++AI;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001117 }
1118 assert(AI == Fn->arg_end() && "Argument mismatch!");
1119}
1120
Daniel Dunbar88b53962009-02-02 22:03:45 +00001121void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI,
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001122 llvm::Value *ReturnValue) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001123 llvm::Value *RV = 0;
1124
1125 // Functions with no result always return void.
1126 if (ReturnValue) {
Daniel Dunbar88b53962009-02-02 22:03:45 +00001127 QualType RetTy = FI.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001128 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001129
1130 switch (RetAI.getKind()) {
1131 case ABIArgInfo::StructRet:
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001132 if (RetTy->isAnyComplexType()) {
1133 // FIXME: Volatile
1134 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
1135 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
1136 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1137 EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy);
1138 } else {
1139 Builder.CreateStore(Builder.CreateLoad(ReturnValue),
1140 CurFn->arg_begin());
1141 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001142 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001143
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001144 case ABIArgInfo::Direct:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001145 RV = Builder.CreateLoad(ReturnValue);
1146 break;
1147
Daniel Dunbar11434922009-01-26 21:26:08 +00001148 case ABIArgInfo::Ignore:
1149 break;
1150
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001151 case ABIArgInfo::Coerce: {
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +00001152 RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001153 break;
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001154 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001155
1156 case ABIArgInfo::ByVal:
1157 case ABIArgInfo::Expand:
1158 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001159 }
1160 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001161
1162 if (RV) {
1163 Builder.CreateRet(RV);
1164 } else {
1165 Builder.CreateRetVoid();
1166 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001167}
1168
Daniel Dunbar88b53962009-02-02 22:03:45 +00001169RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
1170 llvm::Value *Callee,
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001171 const CallArgList &CallArgs) {
Daniel Dunbar5251afa2009-02-03 06:02:10 +00001172 // FIXME: We no longer need the types from CallArgs; lift up and
1173 // simplify.
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001174 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001175
1176 // Handle struct-return functions by passing a pointer to the
1177 // location that we would like to return into.
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001178 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001179 const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001180 switch (RetAI.getKind()) {
1181 case ABIArgInfo::StructRet:
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001182 // Create a temporary alloca to hold the result of the call. :(
Daniel Dunbar56273772008-09-17 00:51:38 +00001183 Args.push_back(CreateTempAlloca(ConvertType(RetTy)));
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001184 break;
1185
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001186 case ABIArgInfo::Direct:
Daniel Dunbar11434922009-01-26 21:26:08 +00001187 case ABIArgInfo::Ignore:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001188 case ABIArgInfo::Coerce:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001189 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001190
1191 case ABIArgInfo::ByVal:
1192 case ABIArgInfo::Expand:
Daniel Dunbarb225be42009-02-03 05:59:18 +00001193 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001194 }
1195
Daniel Dunbarb225be42009-02-03 05:59:18 +00001196 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001197 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001198 I != E; ++I, ++info_it) {
1199 const ABIArgInfo &ArgInfo = info_it->info;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001200 RValue RV = I->first;
Daniel Dunbar56273772008-09-17 00:51:38 +00001201
1202 switch (ArgInfo.getKind()) {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001203 case ABIArgInfo::ByVal: // Direct is byval
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001204 case ABIArgInfo::Direct:
Daniel Dunbar56273772008-09-17 00:51:38 +00001205 if (RV.isScalar()) {
1206 Args.push_back(RV.getScalarVal());
1207 } else if (RV.isComplex()) {
1208 // Make a temporary alloca to pass the argument.
1209 Args.push_back(CreateTempAlloca(ConvertType(I->second)));
1210 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
1211 } else {
1212 Args.push_back(RV.getAggregateAddr());
1213 }
1214 break;
1215
Daniel Dunbar11434922009-01-26 21:26:08 +00001216 case ABIArgInfo::Ignore:
1217 break;
1218
Daniel Dunbar56273772008-09-17 00:51:38 +00001219 case ABIArgInfo::StructRet:
1220 case ABIArgInfo::Coerce:
1221 assert(0 && "Invalid ABI kind for non-return argument");
1222 break;
1223
1224 case ABIArgInfo::Expand:
1225 ExpandTypeToArgs(I->second, RV, Args);
1226 break;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001227 }
1228 }
1229
1230 llvm::CallInst *CI = Builder.CreateCall(Callee,&Args[0],&Args[0]+Args.size());
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001231
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001232 // FIXME: Provide TargetDecl so nounwind, noreturn, etc, etc get set.
Devang Patel761d7f72008-09-25 21:02:23 +00001233 CodeGen::AttributeListType AttributeList;
Daniel Dunbar88b53962009-02-02 22:03:45 +00001234 CGM.ConstructAttributeList(CallInfo, 0, AttributeList);
Devang Patel761d7f72008-09-25 21:02:23 +00001235 CI->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(),
Daniel Dunbar725ad312009-01-31 02:19:00 +00001236 AttributeList.size()));
1237
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001238 if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee))
1239 CI->setCallingConv(F->getCallingConv());
1240 if (CI->getType() != llvm::Type::VoidTy)
1241 CI->setName("call");
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001242
1243 switch (RetAI.getKind()) {
1244 case ABIArgInfo::StructRet:
1245 if (RetTy->isAnyComplexType())
Daniel Dunbar56273772008-09-17 00:51:38 +00001246 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001247 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar56273772008-09-17 00:51:38 +00001248 return RValue::getAggregate(Args[0]);
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001249 else
1250 return RValue::get(Builder.CreateLoad(Args[0]));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001251
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001252 case ABIArgInfo::Direct:
1253 assert((!RetTy->isAnyComplexType() &&
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001254 !CodeGenFunction::hasAggregateLLVMType(RetTy)) &&
1255 "FIXME: Implement return for non-scalar direct types.");
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001256 return RValue::get(CI);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001257
Daniel Dunbar11434922009-01-26 21:26:08 +00001258 case ABIArgInfo::Ignore:
Daniel Dunbarcc039fe2009-01-29 08:24:57 +00001259 if (RetTy->isVoidType())
1260 return RValue::get(0);
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001261
1262 // If we are ignoring an argument that had a result, make sure to
1263 // construct the appropriate return value for our caller.
Daniel Dunbarcc039fe2009-01-29 08:24:57 +00001264 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1265 llvm::Value *Res =
1266 llvm::UndefValue::get(llvm::PointerType::getUnqual(ConvertType(RetTy)));
1267 return RValue::getAggregate(Res);
1268 }
1269 return RValue::get(llvm::UndefValue::get(ConvertType(RetTy)));
Daniel Dunbar11434922009-01-26 21:26:08 +00001270
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001271 case ABIArgInfo::Coerce: {
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +00001272 llvm::Value *V = CreateTempAlloca(ConvertType(RetTy), "coerce");
1273 CreateCoercedStore(CI, V, *this);
Anders Carlssonad3d6912008-11-25 22:21:48 +00001274 if (RetTy->isAnyComplexType())
1275 return RValue::getComplex(LoadComplexFromAddr(V, false));
Daniel Dunbar11434922009-01-26 21:26:08 +00001276 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssonad3d6912008-11-25 22:21:48 +00001277 return RValue::getAggregate(V);
Daniel Dunbar11434922009-01-26 21:26:08 +00001278 else
1279 return RValue::get(Builder.CreateLoad(V));
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001280 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001281
1282 case ABIArgInfo::ByVal:
1283 case ABIArgInfo::Expand:
1284 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001285 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001286
1287 assert(0 && "Unhandled ABIArgInfo::Kind");
1288 return RValue::get(0);
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001289}