blob: dcff31739e03601aaee4d04665f40f53577fd527 [file] [log] [blame]
Daniel Dunbara8f02052008-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 Dunbar3ef2e852008-09-10 00:41:16 +000017#include "CodeGenModule.h"
Daniel Dunbarf98eeff2008-10-13 17:02:26 +000018#include "clang/Basic/TargetInfo.h"
Daniel Dunbara8f02052008-09-08 21:33:45 +000019#include "clang/AST/ASTContext.h"
20#include "clang/AST/Decl.h"
21#include "clang/AST/DeclObjC.h"
Daniel Dunbar04d35782008-09-17 00:51:38 +000022#include "llvm/ADT/StringExtras.h"
Devang Patel98bfe502008-09-24 01:01:36 +000023#include "llvm/Attributes.h"
Daniel Dunbare09a9692009-01-24 08:32:22 +000024#include "llvm/Support/CommandLine.h"
Daniel Dunbar708d8a82009-01-27 01:36:03 +000025#include "llvm/Target/TargetData.h"
Daniel Dunbara8f02052008-09-08 21:33:45 +000026using namespace clang;
27using namespace CodeGen;
28
Daniel Dunbare09a9692009-01-24 08:32:22 +000029static llvm::cl::opt<bool>
30UseX86_64ABI("use-x86_64-abi",
31 llvm::cl::desc("Enable use of experimental x86_64 ABI."),
32 llvm::cl::init(false));
33
Daniel Dunbara8f02052008-09-08 21:33:45 +000034/***/
35
Daniel Dunbara8f02052008-09-08 21:33:45 +000036// FIXME: Use iterator and sidestep silly type array creation.
37
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000038CGFunctionInfo::CGFunctionInfo(const FunctionTypeNoProto *FTNP)
39 : IsVariadic(true)
40{
41 ArgTypes.push_back(FTNP->getResultType());
42}
43
44CGFunctionInfo::CGFunctionInfo(const FunctionTypeProto *FTP)
45 : IsVariadic(FTP->isVariadic())
46{
47 ArgTypes.push_back(FTP->getResultType());
48 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
49 ArgTypes.push_back(FTP->getArgType(i));
50}
51
52// FIXME: Is there really any reason to have this still?
Daniel Dunbara8f02052008-09-08 21:33:45 +000053CGFunctionInfo::CGFunctionInfo(const FunctionDecl *FD)
Daniel Dunbara8f02052008-09-08 21:33:45 +000054{
55 const FunctionType *FTy = FD->getType()->getAsFunctionType();
56 const FunctionTypeProto *FTP = dyn_cast<FunctionTypeProto>(FTy);
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000057
Daniel Dunbara8f02052008-09-08 21:33:45 +000058 ArgTypes.push_back(FTy->getResultType());
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000059 if (FTP) {
60 IsVariadic = FTP->isVariadic();
Daniel Dunbara8f02052008-09-08 21:33:45 +000061 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
62 ArgTypes.push_back(FTP->getArgType(i));
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000063 } else {
64 IsVariadic = true;
65 }
Daniel Dunbara8f02052008-09-08 21:33:45 +000066}
67
68CGFunctionInfo::CGFunctionInfo(const ObjCMethodDecl *MD,
69 const ASTContext &Context)
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000070 : IsVariadic(MD->isVariadic())
Daniel Dunbara8f02052008-09-08 21:33:45 +000071{
72 ArgTypes.push_back(MD->getResultType());
73 ArgTypes.push_back(MD->getSelfDecl()->getType());
74 ArgTypes.push_back(Context.getObjCSelType());
75 for (ObjCMethodDecl::param_const_iterator i = MD->param_begin(),
76 e = MD->param_end(); i != e; ++i)
77 ArgTypes.push_back((*i)->getType());
78}
79
Daniel Dunbarbccb0682008-09-10 00:32:18 +000080ArgTypeIterator CGFunctionInfo::argtypes_begin() const {
81 return ArgTypes.begin();
82}
83
84ArgTypeIterator CGFunctionInfo::argtypes_end() const {
85 return ArgTypes.end();
Daniel Dunbara8f02052008-09-08 21:33:45 +000086}
87
88/***/
89
Daniel Dunbarbccb0682008-09-10 00:32:18 +000090CGCallInfo::CGCallInfo(QualType _ResultType, const CallArgList &_Args) {
91 ArgTypes.push_back(_ResultType);
92 for (CallArgList::const_iterator i = _Args.begin(), e = _Args.end(); i!=e; ++i)
Daniel Dunbara8f02052008-09-08 21:33:45 +000093 ArgTypes.push_back(i->second);
94}
95
Daniel Dunbarbccb0682008-09-10 00:32:18 +000096ArgTypeIterator CGCallInfo::argtypes_begin() const {
97 return ArgTypes.begin();
98}
99
100ArgTypeIterator CGCallInfo::argtypes_end() const {
101 return ArgTypes.end();
Daniel Dunbara8f02052008-09-08 21:33:45 +0000102}
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000103
104/***/
105
Daniel Dunbar22e30052008-09-11 01:48:57 +0000106/// ABIArgInfo - Helper class to encapsulate information about how a
107/// specific C type should be passed to or returned from a function.
Daniel Dunbare126ab12008-09-10 02:41:04 +0000108class ABIArgInfo {
109public:
110 enum Kind {
111 Default,
Daniel Dunbar17d35372008-12-18 04:52:14 +0000112 StructRet, /// Only valid for return values. The return value
113 /// should be passed through a pointer to a caller
114 /// allocated location passed as an implicit first
115 /// argument to the function.
Daniel Dunbar22e30052008-09-11 01:48:57 +0000116
Daniel Dunbar1358b202009-01-26 21:26:08 +0000117 Ignore, /// Ignore the argument (treat as void). Useful for
118 /// void and empty structs.
119
Daniel Dunbar04d35782008-09-17 00:51:38 +0000120 Coerce, /// Only valid for aggregate return types, the argument
121 /// should be accessed by coercion to a provided type.
Daniel Dunbar22e30052008-09-11 01:48:57 +0000122
123 ByVal, /// Only valid for aggregate argument types. The
124 /// structure should be passed "byval" with the
125 /// specified alignment (0 indicates default
126 /// alignment).
127
128 Expand, /// Only valid for aggregate argument types. The
129 /// structure should be expanded into consecutive
Daniel Dunbar04d35782008-09-17 00:51:38 +0000130 /// arguments for its constituent fields. Currently
131 /// expand is only allowed on structures whose fields
132 /// are all scalar types or are themselves expandable
133 /// types.
Daniel Dunbar22e30052008-09-11 01:48:57 +0000134
135 KindFirst=Default, KindLast=Expand
Daniel Dunbare126ab12008-09-10 02:41:04 +0000136 };
137
138private:
139 Kind TheKind;
Daniel Dunbar73d66602008-09-10 07:04:09 +0000140 const llvm::Type *TypeData;
Daniel Dunbar22e30052008-09-11 01:48:57 +0000141 unsigned UIntData;
Daniel Dunbare126ab12008-09-10 02:41:04 +0000142
Daniel Dunbar22e30052008-09-11 01:48:57 +0000143 ABIArgInfo(Kind K, const llvm::Type *TD=0,
144 unsigned UI=0) : TheKind(K),
145 TypeData(TD),
146 UIntData(0) {}
Daniel Dunbare126ab12008-09-10 02:41:04 +0000147public:
148 static ABIArgInfo getDefault() {
Daniel Dunbar22e30052008-09-11 01:48:57 +0000149 return ABIArgInfo(Default);
Daniel Dunbare126ab12008-09-10 02:41:04 +0000150 }
151 static ABIArgInfo getStructRet() {
Daniel Dunbar22e30052008-09-11 01:48:57 +0000152 return ABIArgInfo(StructRet);
Daniel Dunbare126ab12008-09-10 02:41:04 +0000153 }
Daniel Dunbar1358b202009-01-26 21:26:08 +0000154 static ABIArgInfo getIgnore() {
155 return ABIArgInfo(Ignore);
156 }
Daniel Dunbar73d66602008-09-10 07:04:09 +0000157 static ABIArgInfo getCoerce(const llvm::Type *T) {
Daniel Dunbare126ab12008-09-10 02:41:04 +0000158 return ABIArgInfo(Coerce, T);
159 }
Daniel Dunbar22e30052008-09-11 01:48:57 +0000160 static ABIArgInfo getByVal(unsigned Alignment) {
161 return ABIArgInfo(ByVal, 0, Alignment);
162 }
Daniel Dunbar04d35782008-09-17 00:51:38 +0000163 static ABIArgInfo getExpand() {
164 return ABIArgInfo(Expand);
165 }
Daniel Dunbare126ab12008-09-10 02:41:04 +0000166
167 Kind getKind() const { return TheKind; }
168 bool isDefault() const { return TheKind == Default; }
169 bool isStructRet() const { return TheKind == StructRet; }
Daniel Dunbar1358b202009-01-26 21:26:08 +0000170 bool isIgnore() const { return TheKind == Ignore; }
Daniel Dunbare126ab12008-09-10 02:41:04 +0000171 bool isCoerce() const { return TheKind == Coerce; }
Daniel Dunbar22e30052008-09-11 01:48:57 +0000172 bool isByVal() const { return TheKind == ByVal; }
Daniel Dunbar04d35782008-09-17 00:51:38 +0000173 bool isExpand() const { return TheKind == Expand; }
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000174
175 // Coerce accessors
Daniel Dunbar73d66602008-09-10 07:04:09 +0000176 const llvm::Type *getCoerceToType() const {
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000177 assert(TheKind == Coerce && "Invalid kind!");
178 return TypeData;
179 }
Daniel Dunbar22e30052008-09-11 01:48:57 +0000180
181 // ByVal accessors
182 unsigned getByValAlignment() const {
183 assert(TheKind == ByVal && "Invalid kind!");
184 return UIntData;
185 }
Daniel Dunbare126ab12008-09-10 02:41:04 +0000186};
187
188/***/
189
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000190/* FIXME: All of this stuff should be part of the target interface
191 somehow. It is currently here because it is not clear how to factor
192 the targets to support this, since the Targets currently live in a
193 layer below types n'stuff.
194 */
195
196/// ABIInfo - Target specific hooks for defining how a type should be
197/// passed or returned from functions.
198class clang::ABIInfo {
199public:
200 virtual ~ABIInfo();
201
202 virtual ABIArgInfo classifyReturnType(QualType RetTy,
203 ASTContext &Context) const = 0;
204
205 virtual ABIArgInfo classifyArgumentType(QualType Ty,
206 ASTContext &Context) const = 0;
207};
208
209ABIInfo::~ABIInfo() {}
210
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000211/// isEmptyStruct - Return true iff a structure has no non-empty
212/// members. Note that a structure with a flexible array member is not
213/// considered empty.
214static bool isEmptyStruct(QualType T) {
215 const RecordType *RT = T->getAsStructureType();
216 if (!RT)
217 return 0;
218 const RecordDecl *RD = RT->getDecl();
219 if (RD->hasFlexibleArrayMember())
220 return false;
Douglas Gregor5d764842009-01-09 17:18:27 +0000221 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000222 e = RD->field_end(); i != e; ++i) {
223 const FieldDecl *FD = *i;
224 if (!isEmptyStruct(FD->getType()))
225 return false;
226 }
227 return true;
228}
229
230/// isSingleElementStruct - Determine if a structure is a "single
231/// element struct", i.e. it has exactly one non-empty field or
232/// exactly one field which is itself a single element
233/// struct. Structures with flexible array members are never
234/// considered single element structs.
235///
236/// \return The field declaration for the single non-empty field, if
237/// it exists.
238static const FieldDecl *isSingleElementStruct(QualType T) {
239 const RecordType *RT = T->getAsStructureType();
240 if (!RT)
241 return 0;
242
243 const RecordDecl *RD = RT->getDecl();
244 if (RD->hasFlexibleArrayMember())
245 return 0;
246
247 const FieldDecl *Found = 0;
Douglas Gregor5d764842009-01-09 17:18:27 +0000248 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000249 e = RD->field_end(); i != e; ++i) {
250 const FieldDecl *FD = *i;
251 QualType FT = FD->getType();
252
253 if (isEmptyStruct(FT)) {
254 // Ignore
255 } else if (Found) {
256 return 0;
257 } else if (!CodeGenFunction::hasAggregateLLVMType(FT)) {
258 Found = FD;
259 } else {
260 Found = isSingleElementStruct(FT);
261 if (!Found)
262 return 0;
263 }
264 }
265
266 return Found;
267}
268
269static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
270 if (!Ty->getAsBuiltinType() && !Ty->isPointerType())
271 return false;
272
273 uint64_t Size = Context.getTypeSize(Ty);
274 return Size == 32 || Size == 64;
275}
276
277static bool areAllFields32Or64BitBasicType(const RecordDecl *RD,
278 ASTContext &Context) {
Douglas Gregor5d764842009-01-09 17:18:27 +0000279 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000280 e = RD->field_end(); i != e; ++i) {
281 const FieldDecl *FD = *i;
282
283 if (!is32Or64BitBasicType(FD->getType(), Context))
284 return false;
285
286 // If this is a bit-field we need to make sure it is still a
287 // 32-bit or 64-bit type.
288 if (Expr *BW = FD->getBitWidth()) {
289 unsigned Width = BW->getIntegerConstantExprValue(Context).getZExtValue();
290 if (Width <= 16)
291 return false;
292 }
293 }
294 return true;
295}
296
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000297namespace {
298/// DefaultABIInfo - The default implementation for ABI specific
299/// details. This implementation provides information which results in
300/// sensible LLVM IR generation, but does not conform to any
301/// particular ABI.
302class DefaultABIInfo : public ABIInfo {
303 virtual ABIArgInfo classifyReturnType(QualType RetTy,
304 ASTContext &Context) const;
305
306 virtual ABIArgInfo classifyArgumentType(QualType RetTy,
307 ASTContext &Context) const;
308};
309
310/// X86_32ABIInfo - The X86-32 ABI information.
311class X86_32ABIInfo : public ABIInfo {
312public:
313 virtual ABIArgInfo classifyReturnType(QualType RetTy,
314 ASTContext &Context) const;
315
316 virtual ABIArgInfo classifyArgumentType(QualType RetTy,
317 ASTContext &Context) const;
318};
319}
320
321ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
322 ASTContext &Context) const {
Daniel Dunbare126ab12008-09-10 02:41:04 +0000323 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000324 // Classify "single element" structs as their element type.
325 const FieldDecl *SeltFD = isSingleElementStruct(RetTy);
326 if (SeltFD) {
327 QualType SeltTy = SeltFD->getType()->getDesugaredType();
328 if (const BuiltinType *BT = SeltTy->getAsBuiltinType()) {
329 // FIXME: This is gross, it would be nice if we could just
330 // pass back SeltTy and have clients deal with it. Is it worth
331 // supporting coerce to both LLVM and clang Types?
332 if (BT->isIntegerType()) {
333 uint64_t Size = Context.getTypeSize(SeltTy);
334 return ABIArgInfo::getCoerce(llvm::IntegerType::get((unsigned) Size));
335 } else if (BT->getKind() == BuiltinType::Float) {
336 return ABIArgInfo::getCoerce(llvm::Type::FloatTy);
337 } else if (BT->getKind() == BuiltinType::Double) {
338 return ABIArgInfo::getCoerce(llvm::Type::DoubleTy);
339 }
340 } else if (SeltTy->isPointerType()) {
341 // FIXME: It would be really nice if this could come out as
342 // the proper pointer type.
343 llvm::Type *PtrTy =
344 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
345 return ABIArgInfo::getCoerce(PtrTy);
346 }
347 }
348
Daniel Dunbar73d66602008-09-10 07:04:09 +0000349 uint64_t Size = Context.getTypeSize(RetTy);
350 if (Size == 8) {
351 return ABIArgInfo::getCoerce(llvm::Type::Int8Ty);
352 } else if (Size == 16) {
353 return ABIArgInfo::getCoerce(llvm::Type::Int16Ty);
354 } else if (Size == 32) {
355 return ABIArgInfo::getCoerce(llvm::Type::Int32Ty);
356 } else if (Size == 64) {
357 return ABIArgInfo::getCoerce(llvm::Type::Int64Ty);
358 } else {
359 return ABIArgInfo::getStructRet();
360 }
Daniel Dunbare126ab12008-09-10 02:41:04 +0000361 } else {
362 return ABIArgInfo::getDefault();
363 }
364}
365
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000366ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
367 ASTContext &Context) const {
Daniel Dunbar3158c592008-09-17 20:11:04 +0000368 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000369 // Structures with flexible arrays are always byval.
370 if (const RecordType *RT = Ty->getAsStructureType())
371 if (RT->getDecl()->hasFlexibleArrayMember())
372 return ABIArgInfo::getByVal(0);
373
374 // Expand empty structs (i.e. ignore)
375 uint64_t Size = Context.getTypeSize(Ty);
376 if (Ty->isStructureType() && Size == 0)
377 return ABIArgInfo::getExpand();
378
379 // Expand structs with size <= 128-bits which consist only of
380 // basic types (int, long long, float, double, xxx*). This is
381 // non-recursive and does not ignore empty fields.
382 if (const RecordType *RT = Ty->getAsStructureType()) {
383 if (Context.getTypeSize(Ty) <= 4*32 &&
384 areAllFields32Or64BitBasicType(RT->getDecl(), Context))
385 return ABIArgInfo::getExpand();
386 }
387
Daniel Dunbar22e30052008-09-11 01:48:57 +0000388 return ABIArgInfo::getByVal(0);
389 } else {
390 return ABIArgInfo::getDefault();
391 }
392}
393
Daniel Dunbare09a9692009-01-24 08:32:22 +0000394namespace {
395/// X86_32ABIInfo - The X86_64 ABI information.
396class X86_64ABIInfo : public ABIInfo {
397 enum Class {
398 Integer = 0,
399 SSE,
400 SSEUp,
401 X87,
402 X87Up,
403 ComplexX87,
404 NoClass,
405 Memory
406 };
407
408 /// classify - Determine the x86_64 register classes in which the
409 /// given type T should be passed.
410 ///
411 /// \param Lo - The classification for the low word of the type.
412 /// \param Hi - The classification for the high word of the type.
413 ///
414 /// If a word is unused its result will be NoClass; if a type should
415 /// be passed in Memory then at least the classification of \arg Lo
416 /// will be Memory.
417 ///
418 /// The \arg Lo class will be NoClass iff the argument is ignored.
419 ///
420 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
421 /// be NoClass.
422 void classify(QualType T, ASTContext &Context,
423 Class &Lo, Class &Hi) const;
424
425public:
426 virtual ABIArgInfo classifyReturnType(QualType RetTy,
427 ASTContext &Context) const;
428
429 virtual ABIArgInfo classifyArgumentType(QualType RetTy,
430 ASTContext &Context) const;
431};
432}
433
434void X86_64ABIInfo::classify(QualType Ty,
435 ASTContext &Context,
436 Class &Lo, Class &Hi) const {
437 Lo = Memory;
438 Hi = NoClass;
439 if (const BuiltinType *BT = Ty->getAsBuiltinType()) {
440 BuiltinType::Kind k = BT->getKind();
441
Daniel Dunbar1358b202009-01-26 21:26:08 +0000442 if (k == BuiltinType::Void) {
443 Lo = NoClass;
444 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
Daniel Dunbare09a9692009-01-24 08:32:22 +0000445 Lo = Integer;
446 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
447 Lo = SSE;
448 } else if (k == BuiltinType::LongDouble) {
449 Lo = X87;
450 Hi = X87Up;
451 }
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000452
453 // FIXME: _Decimal32 and _Decimal64 are SSE.
454 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Daniel Dunbare09a9692009-01-24 08:32:22 +0000455 // FIXME: __int128 is (Integer, Integer).
456 } else if (Ty->isPointerLikeType() || Ty->isBlockPointerType() ||
457 Ty->isObjCQualifiedInterfaceType()) {
458 Lo = Integer;
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000459 } else if (const VectorType *VT = Ty->getAsVectorType()) {
460 unsigned Size = Context.getTypeSize(VT);
461 if (Size == 64) {
462 // FIXME: For some reason, gcc appears to be treating <1 x
463 // double> as INTEGER; this seems wrong, but we will match for
464 // now (icc rejects <1 x double>, so...).
465 Lo = (VT->getElementType() == Context.DoubleTy) ? Integer : SSE;
466 } else if (Size == 128) {
467 Lo = SSE;
468 Hi = SSEUp;
469 }
Daniel Dunbare09a9692009-01-24 08:32:22 +0000470 } else if (const ComplexType *CT = Ty->getAsComplexType()) {
471 QualType ET = CT->getElementType();
472
Daniel Dunbar28770fc2009-01-29 07:22:20 +0000473 if (ET->isIntegerType()) {
474 unsigned Size = Context.getTypeSize(Ty);
475 if (Size <= 64)
476 Lo = Integer;
477 else if (Size <= 128)
478 Lo = Hi = Integer;
479 } else if (ET == Context.FloatTy)
Daniel Dunbare09a9692009-01-24 08:32:22 +0000480 Lo = SSE;
481 else if (ET == Context.DoubleTy)
482 Lo = Hi = SSE;
483 else if (ET == Context.LongDoubleTy)
484 Lo = ComplexX87;
485 }
486}
487
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000488ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
489 ASTContext &Context) const {
Daniel Dunbare09a9692009-01-24 08:32:22 +0000490 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
491 // classification algorithm.
492 X86_64ABIInfo::Class Lo, Hi;
493 classify(RetTy, Context, Lo, Hi);
494
495 const llvm::Type *ResType = 0;
496 switch (Lo) {
497 case NoClass:
Daniel Dunbar1358b202009-01-26 21:26:08 +0000498 return ABIArgInfo::getIgnore();
Daniel Dunbare09a9692009-01-24 08:32:22 +0000499
500 case SSEUp:
501 case X87Up:
502 assert(0 && "Invalid classification for lo word.");
503
504 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
505 // hidden argument, i.e. structret.
506 case Memory:
507 return ABIArgInfo::getStructRet();
508
509 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
510 // available register of the sequence %rax, %rdx is used.
511 case Integer:
512 ResType = llvm::Type::Int64Ty; break;
513
514 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
515 // available SSE register of the sequence %xmm0, %xmm1 is used.
516 case SSE:
517 ResType = llvm::Type::DoubleTy; break;
518
519 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
520 // returned on the X87 stack in %st0 as 80-bit x87 number.
521 case X87:
522 ResType = llvm::Type::X86_FP80Ty; break;
523
524 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
525 // part of the value is returned in %st0 and the imaginary part in
526 // %st1.
527 case ComplexX87:
528 assert(Hi == NoClass && "Unexpected ComplexX87 classification.");
529 ResType = llvm::VectorType::get(llvm::Type::X86_FP80Ty, 2);
530 break;
531 }
532
533 switch (Hi) {
534 // Memory was handled previously, and ComplexX87 and X87 should
535 // never occur as hi classes.
536 case Memory:
537 case X87:
538 case ComplexX87:
539 assert(0 && "Invalid classification for hi word.");
540
541 case NoClass: break;
542 case Integer:
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000543 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
544 break;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000545 case SSE:
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000546 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
547 break;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000548
549 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
550 // is passed in the upper half of the last used SSE register.
551 //
552 // SSEUP should always be preceeded by SSE, just widen.
553 case SSEUp:
554 assert(Lo == SSE && "Unexpected SSEUp classification.");
555 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
556 break;
557
558 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000559 // returned together with the previous X87 value in %st0.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000560 //
561 // X87UP should always be preceeded by X87, so we don't need to do
562 // anything here.
563 case X87Up:
564 assert(Lo == X87 && "Unexpected X87Up classification.");
565 break;
566 }
567
568 return ABIArgInfo::getCoerce(ResType);
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000569}
570
571ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty,
572 ASTContext &Context) const {
573 return ABIArgInfo::getDefault();
574}
575
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000576ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
577 ASTContext &Context) const {
578 return ABIArgInfo::getDefault();
579}
580
581ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
582 ASTContext &Context) const {
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000583 return ABIArgInfo::getDefault();
584}
585
586const ABIInfo &CodeGenTypes::getABIInfo() const {
587 if (TheABIInfo)
588 return *TheABIInfo;
589
590 // For now we just cache this in the CodeGenTypes and don't bother
591 // to free it.
592 const char *TargetPrefix = getContext().Target.getTargetPrefix();
593 if (strcmp(TargetPrefix, "x86") == 0) {
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000594 switch (getContext().Target.getPointerWidth(0)) {
595 case 32:
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000596 return *(TheABIInfo = new X86_32ABIInfo());
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000597 case 64:
Daniel Dunbare09a9692009-01-24 08:32:22 +0000598 if (UseX86_64ABI)
599 return *(TheABIInfo = new X86_64ABIInfo());
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000600 }
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000601 }
602
603 return *(TheABIInfo = new DefaultABIInfo);
604}
605
606// getABIReturnInfo - Wrap the ABIInfo getABIReturnInfo, altering
607// "default" types to StructRet when appropriate for simplicity.
608static ABIArgInfo getABIReturnInfo(QualType Ty, CodeGenTypes &CGT) {
609 assert(!Ty->isArrayType() &&
610 "Array types cannot be passed directly.");
611 ABIArgInfo Info = CGT.getABIInfo().classifyReturnType(Ty, CGT.getContext());
Daniel Dunbar3158c592008-09-17 20:11:04 +0000612 // Ensure default on aggregate types is StructRet.
613 if (Info.isDefault() && CodeGenFunction::hasAggregateLLVMType(Ty))
614 return ABIArgInfo::getStructRet();
615 return Info;
616}
617
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000618// getABIArgumentInfo - Wrap the ABIInfo getABIReturnInfo, altering
619// "default" types to ByVal when appropriate for simplicity.
620static ABIArgInfo getABIArgumentInfo(QualType Ty, CodeGenTypes &CGT) {
621 assert(!Ty->isArrayType() &&
622 "Array types cannot be passed directly.");
623 ABIArgInfo Info = CGT.getABIInfo().classifyArgumentType(Ty, CGT.getContext());
Daniel Dunbar3158c592008-09-17 20:11:04 +0000624 // Ensure default on aggregate types is ByVal.
625 if (Info.isDefault() && CodeGenFunction::hasAggregateLLVMType(Ty))
626 return ABIArgInfo::getByVal(0);
627 return Info;
628}
629
Daniel Dunbare126ab12008-09-10 02:41:04 +0000630/***/
631
Daniel Dunbar04d35782008-09-17 00:51:38 +0000632void CodeGenTypes::GetExpandedTypes(QualType Ty,
633 std::vector<const llvm::Type*> &ArgTys) {
634 const RecordType *RT = Ty->getAsStructureType();
635 assert(RT && "Can only expand structure types.");
636 const RecordDecl *RD = RT->getDecl();
637 assert(!RD->hasFlexibleArrayMember() &&
638 "Cannot expand structure with flexible array.");
639
Douglas Gregor5d764842009-01-09 17:18:27 +0000640 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar04d35782008-09-17 00:51:38 +0000641 e = RD->field_end(); i != e; ++i) {
642 const FieldDecl *FD = *i;
643 assert(!FD->isBitField() &&
644 "Cannot expand structure with bit-field members.");
645
646 QualType FT = FD->getType();
647 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
648 GetExpandedTypes(FT, ArgTys);
649 } else {
650 ArgTys.push_back(ConvertType(FT));
651 }
652 }
653}
654
655llvm::Function::arg_iterator
656CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
657 llvm::Function::arg_iterator AI) {
658 const RecordType *RT = Ty->getAsStructureType();
659 assert(RT && "Can only expand structure types.");
660
661 RecordDecl *RD = RT->getDecl();
662 assert(LV.isSimple() &&
663 "Unexpected non-simple lvalue during struct expansion.");
664 llvm::Value *Addr = LV.getAddress();
665 for (RecordDecl::field_iterator i = RD->field_begin(),
666 e = RD->field_end(); i != e; ++i) {
667 FieldDecl *FD = *i;
668 QualType FT = FD->getType();
669
670 // FIXME: What are the right qualifiers here?
671 LValue LV = EmitLValueForField(Addr, FD, false, 0);
672 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
673 AI = ExpandTypeFromArgs(FT, LV, AI);
674 } else {
675 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
676 ++AI;
677 }
678 }
679
680 return AI;
681}
682
683void
684CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
685 llvm::SmallVector<llvm::Value*, 16> &Args) {
686 const RecordType *RT = Ty->getAsStructureType();
687 assert(RT && "Can only expand structure types.");
688
689 RecordDecl *RD = RT->getDecl();
690 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
691 llvm::Value *Addr = RV.getAggregateAddr();
692 for (RecordDecl::field_iterator i = RD->field_begin(),
693 e = RD->field_end(); i != e; ++i) {
694 FieldDecl *FD = *i;
695 QualType FT = FD->getType();
696
697 // FIXME: What are the right qualifiers here?
698 LValue LV = EmitLValueForField(Addr, FD, false, 0);
699 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
700 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
701 } else {
702 RValue RV = EmitLoadOfLValue(LV, FT);
703 assert(RV.isScalar() &&
704 "Unexpected non-scalar rvalue during struct expansion.");
705 Args.push_back(RV.getScalarVal());
706 }
707 }
708}
709
710/***/
711
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000712const llvm::FunctionType *
Daniel Dunbara9976a22008-09-10 07:00:50 +0000713CodeGenTypes::GetFunctionType(const CGCallInfo &CI, bool IsVariadic) {
714 return GetFunctionType(CI.argtypes_begin(), CI.argtypes_end(), IsVariadic);
715}
716
717const llvm::FunctionType *
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000718CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI) {
Daniel Dunbara9976a22008-09-10 07:00:50 +0000719 return GetFunctionType(FI.argtypes_begin(), FI.argtypes_end(), FI.isVariadic());
720}
721
722const llvm::FunctionType *
723CodeGenTypes::GetFunctionType(ArgTypeIterator begin, ArgTypeIterator end,
724 bool IsVariadic) {
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000725 std::vector<const llvm::Type*> ArgTys;
726
727 const llvm::Type *ResultType = 0;
728
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000729 QualType RetTy = *begin;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000730 ABIArgInfo RetAI = getABIReturnInfo(RetTy, *this);
Daniel Dunbar22e30052008-09-11 01:48:57 +0000731 switch (RetAI.getKind()) {
732 case ABIArgInfo::ByVal:
733 case ABIArgInfo::Expand:
734 assert(0 && "Invalid ABI kind for return argument");
735
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000736 case ABIArgInfo::Default:
737 if (RetTy->isVoidType()) {
738 ResultType = llvm::Type::VoidTy;
739 } else {
Daniel Dunbara9976a22008-09-10 07:00:50 +0000740 ResultType = ConvertType(RetTy);
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000741 }
742 break;
743
744 case ABIArgInfo::StructRet: {
745 ResultType = llvm::Type::VoidTy;
Daniel Dunbara9976a22008-09-10 07:00:50 +0000746 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000747 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
748 break;
749 }
750
Daniel Dunbar1358b202009-01-26 21:26:08 +0000751 case ABIArgInfo::Ignore:
752 ResultType = llvm::Type::VoidTy;
753 break;
754
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000755 case ABIArgInfo::Coerce:
Daniel Dunbar73d66602008-09-10 07:04:09 +0000756 ResultType = RetAI.getCoerceToType();
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000757 break;
758 }
759
760 for (++begin; begin != end; ++begin) {
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000761 ABIArgInfo AI = getABIArgumentInfo(*begin, *this);
Daniel Dunbara9976a22008-09-10 07:00:50 +0000762 const llvm::Type *Ty = ConvertType(*begin);
Daniel Dunbar22e30052008-09-11 01:48:57 +0000763
764 switch (AI.getKind()) {
Daniel Dunbar1358b202009-01-26 21:26:08 +0000765 case ABIArgInfo::Ignore:
766 break;
767
Daniel Dunbar04d35782008-09-17 00:51:38 +0000768 case ABIArgInfo::Coerce:
Daniel Dunbar22e30052008-09-11 01:48:57 +0000769 case ABIArgInfo::StructRet:
770 assert(0 && "Invalid ABI kind for non-return argument");
771
772 case ABIArgInfo::ByVal:
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000773 // byval arguments are always on the stack, which is addr space #0.
774 ArgTys.push_back(llvm::PointerType::getUnqual(Ty));
Daniel Dunbar22e30052008-09-11 01:48:57 +0000775 assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled");
776 break;
777
778 case ABIArgInfo::Default:
779 ArgTys.push_back(Ty);
780 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +0000781
782 case ABIArgInfo::Expand:
Daniel Dunbar04d35782008-09-17 00:51:38 +0000783 GetExpandedTypes(*begin, ArgTys);
Daniel Dunbar22e30052008-09-11 01:48:57 +0000784 break;
785 }
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000786 }
787
Daniel Dunbara9976a22008-09-10 07:00:50 +0000788 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000789}
790
Daniel Dunbar3ef2e852008-09-10 00:41:16 +0000791bool CodeGenModule::ReturnTypeUsesSret(QualType RetTy) {
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000792 return getABIReturnInfo(RetTy, getTypes()).isStructRet();
Daniel Dunbar49f5a0d2008-09-09 23:48:28 +0000793}
794
Devang Patela85a9ef2008-09-25 21:02:23 +0000795void CodeGenModule::ConstructAttributeList(const Decl *TargetDecl,
Daniel Dunbare126ab12008-09-10 02:41:04 +0000796 ArgTypeIterator begin,
797 ArgTypeIterator end,
Devang Patela85a9ef2008-09-25 21:02:23 +0000798 AttributeListType &PAL) {
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000799 unsigned FuncAttrs = 0;
Devang Patel2bb6eb82008-09-26 22:53:57 +0000800 unsigned RetAttrs = 0;
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000801
802 if (TargetDecl) {
803 if (TargetDecl->getAttr<NoThrowAttr>())
Devang Patela85a9ef2008-09-25 21:02:23 +0000804 FuncAttrs |= llvm::Attribute::NoUnwind;
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000805 if (TargetDecl->getAttr<NoReturnAttr>())
Devang Patela85a9ef2008-09-25 21:02:23 +0000806 FuncAttrs |= llvm::Attribute::NoReturn;
Anders Carlssondd6791c2008-10-05 23:32:53 +0000807 if (TargetDecl->getAttr<PureAttr>())
808 FuncAttrs |= llvm::Attribute::ReadOnly;
809 if (TargetDecl->getAttr<ConstAttr>())
810 FuncAttrs |= llvm::Attribute::ReadNone;
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000811 }
812
Daniel Dunbare126ab12008-09-10 02:41:04 +0000813 QualType RetTy = *begin;
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000814 unsigned Index = 1;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000815 ABIArgInfo RetAI = getABIReturnInfo(RetTy, getTypes());
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000816 switch (RetAI.getKind()) {
Daniel Dunbare126ab12008-09-10 02:41:04 +0000817 case ABIArgInfo::Default:
818 if (RetTy->isPromotableIntegerType()) {
819 if (RetTy->isSignedIntegerType()) {
Devang Patel2bb6eb82008-09-26 22:53:57 +0000820 RetAttrs |= llvm::Attribute::SExt;
Daniel Dunbare126ab12008-09-10 02:41:04 +0000821 } else if (RetTy->isUnsignedIntegerType()) {
Devang Patel2bb6eb82008-09-26 22:53:57 +0000822 RetAttrs |= llvm::Attribute::ZExt;
Daniel Dunbare126ab12008-09-10 02:41:04 +0000823 }
824 }
825 break;
826
827 case ABIArgInfo::StructRet:
Devang Patela85a9ef2008-09-25 21:02:23 +0000828 PAL.push_back(llvm::AttributeWithIndex::get(Index,
829 llvm::Attribute::StructRet|
830 llvm::Attribute::NoAlias));
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000831 ++Index;
Daniel Dunbare126ab12008-09-10 02:41:04 +0000832 break;
833
Daniel Dunbar1358b202009-01-26 21:26:08 +0000834 case ABIArgInfo::Ignore:
Daniel Dunbare126ab12008-09-10 02:41:04 +0000835 case ABIArgInfo::Coerce:
Daniel Dunbare126ab12008-09-10 02:41:04 +0000836 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +0000837
838 case ABIArgInfo::ByVal:
839 case ABIArgInfo::Expand:
840 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000841 }
Daniel Dunbare126ab12008-09-10 02:41:04 +0000842
Devang Patel2bb6eb82008-09-26 22:53:57 +0000843 if (RetAttrs)
844 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Daniel Dunbar04d35782008-09-17 00:51:38 +0000845 for (++begin; begin != end; ++begin) {
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000846 QualType ParamType = *begin;
Devang Patela85a9ef2008-09-25 21:02:23 +0000847 unsigned Attributes = 0;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000848 ABIArgInfo AI = getABIArgumentInfo(ParamType, getTypes());
Daniel Dunbar22e30052008-09-11 01:48:57 +0000849
850 switch (AI.getKind()) {
851 case ABIArgInfo::StructRet:
Daniel Dunbar04d35782008-09-17 00:51:38 +0000852 case ABIArgInfo::Coerce:
Daniel Dunbar22e30052008-09-11 01:48:57 +0000853 assert(0 && "Invalid ABI kind for non-return argument");
854
855 case ABIArgInfo::ByVal:
Devang Patela85a9ef2008-09-25 21:02:23 +0000856 Attributes |= llvm::Attribute::ByVal;
Daniel Dunbar22e30052008-09-11 01:48:57 +0000857 assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled");
858 break;
859
860 case ABIArgInfo::Default:
861 if (ParamType->isPromotableIntegerType()) {
862 if (ParamType->isSignedIntegerType()) {
Devang Patela85a9ef2008-09-25 21:02:23 +0000863 Attributes |= llvm::Attribute::SExt;
Daniel Dunbar22e30052008-09-11 01:48:57 +0000864 } else if (ParamType->isUnsignedIntegerType()) {
Devang Patela85a9ef2008-09-25 21:02:23 +0000865 Attributes |= llvm::Attribute::ZExt;
Daniel Dunbar22e30052008-09-11 01:48:57 +0000866 }
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000867 }
Daniel Dunbar22e30052008-09-11 01:48:57 +0000868 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +0000869
Daniel Dunbar1358b202009-01-26 21:26:08 +0000870 case ABIArgInfo::Ignore:
871 // Skip increment, no matching LLVM parameter.
872 continue;
873
Daniel Dunbar04d35782008-09-17 00:51:38 +0000874 case ABIArgInfo::Expand: {
875 std::vector<const llvm::Type*> Tys;
876 // FIXME: This is rather inefficient. Do we ever actually need
877 // to do anything here? The result should be just reconstructed
878 // on the other side, so extension should be a non-issue.
879 getTypes().GetExpandedTypes(ParamType, Tys);
880 Index += Tys.size();
881 continue;
882 }
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000883 }
Daniel Dunbar22e30052008-09-11 01:48:57 +0000884
Devang Patela85a9ef2008-09-25 21:02:23 +0000885 if (Attributes)
886 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar04d35782008-09-17 00:51:38 +0000887 ++Index;
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000888 }
Devang Patel2bb6eb82008-09-26 22:53:57 +0000889 if (FuncAttrs)
890 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
891
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000892}
893
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000894void CodeGenFunction::EmitFunctionProlog(llvm::Function *Fn,
895 QualType RetTy,
896 const FunctionArgList &Args) {
897 // Emit allocs for param decls. Give the LLVM Argument nodes names.
898 llvm::Function::arg_iterator AI = Fn->arg_begin();
899
900 // Name the struct return argument.
Daniel Dunbare126ab12008-09-10 02:41:04 +0000901 if (CGM.ReturnTypeUsesSret(RetTy)) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000902 AI->setName("agg.result");
903 ++AI;
904 }
905
906 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbar04d35782008-09-17 00:51:38 +0000907 i != e; ++i) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000908 const VarDecl *Arg = i->first;
Daniel Dunbar04d35782008-09-17 00:51:38 +0000909 QualType Ty = i->second;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000910 ABIArgInfo ArgI = getABIArgumentInfo(Ty, CGM.getTypes());
Daniel Dunbar22e30052008-09-11 01:48:57 +0000911
912 switch (ArgI.getKind()) {
913 case ABIArgInfo::ByVal:
914 case ABIArgInfo::Default: {
915 assert(AI != Fn->arg_end() && "Argument mismatch!");
916 llvm::Value* V = AI;
Daniel Dunbar04d35782008-09-17 00:51:38 +0000917 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
Daniel Dunbar22e30052008-09-11 01:48:57 +0000918 // This must be a promotion, for something like
919 // "void a(x) short x; {..."
Daniel Dunbar04d35782008-09-17 00:51:38 +0000920 V = EmitScalarConversion(V, Ty, Arg->getType());
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000921 }
Daniel Dunbar22e30052008-09-11 01:48:57 +0000922 EmitParmDecl(*Arg, V);
923 break;
924 }
Daniel Dunbar04d35782008-09-17 00:51:38 +0000925
926 case ABIArgInfo::Expand: {
927 // If this was structure was expand into multiple arguments then
928 // we need to create a temporary and reconstruct it from the
929 // arguments.
Chris Lattner6c5ec622008-11-24 04:00:27 +0000930 std::string Name = Arg->getNameAsString();
Daniel Dunbar04d35782008-09-17 00:51:38 +0000931 llvm::Value *Temp = CreateTempAlloca(ConvertType(Ty),
932 (Name + ".addr").c_str());
933 // FIXME: What are the right qualifiers here?
934 llvm::Function::arg_iterator End =
935 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI);
936 EmitParmDecl(*Arg, Temp);
Daniel Dunbar22e30052008-09-11 01:48:57 +0000937
Daniel Dunbar04d35782008-09-17 00:51:38 +0000938 // Name the arguments used in expansion and increment AI.
939 unsigned Index = 0;
940 for (; AI != End; ++AI, ++Index)
941 AI->setName(Name + "." + llvm::utostr(Index));
942 continue;
943 }
Daniel Dunbar1358b202009-01-26 21:26:08 +0000944
945 case ABIArgInfo::Ignore:
946 break;
947
Daniel Dunbar22e30052008-09-11 01:48:57 +0000948 case ABIArgInfo::Coerce:
Daniel Dunbar22e30052008-09-11 01:48:57 +0000949 case ABIArgInfo::StructRet:
950 assert(0 && "Invalid ABI kind for non-return argument");
951 }
Daniel Dunbar04d35782008-09-17 00:51:38 +0000952
953 ++AI;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000954 }
955 assert(AI == Fn->arg_end() && "Argument mismatch!");
956}
957
Daniel Dunbar708d8a82009-01-27 01:36:03 +0000958/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
959/// a pointer to an object of type \arg Ty.
960///
961/// This safely handles the case when the src type is smaller than the
962/// destination type; in this situation the values of bits which not
963/// present in the src are undefined.
964static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
965 const llvm::Type *Ty,
966 CodeGenFunction &CGF) {
967 const llvm::Type *SrcTy =
968 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
969 unsigned SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
970 unsigned DstSize = CGF.CGM.getTargetData().getTypePaddedSize(Ty);
971
972 // If load is legal, just bitcase the src pointer.
973 if (SrcSize == DstSize) {
974 llvm::Value *Casted =
975 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
976 return CGF.Builder.CreateLoad(Casted);
977 } else {
978 assert(SrcSize < DstSize && "Coercion is losing source bits!");
979
980 // Otherwise do coercion through memory. This is stupid, but
981 // simple.
982 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
983 llvm::Value *Casted =
984 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
985 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
986 return CGF.Builder.CreateLoad(Tmp);
987 }
988}
989
990/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
991/// where the source and destination may have different types.
992///
993/// This safely handles the case when the src type is larger than the
994/// destination type; the upper bits of the src will be lost.
995static void CreateCoercedStore(llvm::Value *Src,
996 llvm::Value *DstPtr,
997 CodeGenFunction &CGF) {
998 const llvm::Type *SrcTy = Src->getType();
999 const llvm::Type *DstTy =
1000 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
1001
1002 unsigned SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
1003 unsigned DstSize = CGF.CGM.getTargetData().getTypePaddedSize(DstTy);
1004
1005 // If store is legal, just bitcase the src pointer.
1006 if (SrcSize == DstSize) {
1007 llvm::Value *Casted =
1008 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
1009 CGF.Builder.CreateStore(Src, Casted);
1010 } else {
1011 assert(SrcSize > DstSize && "Coercion is missing bits!");
1012
1013 // Otherwise do coercion through memory. This is stupid, but
1014 // simple.
1015 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
1016 CGF.Builder.CreateStore(Src, Tmp);
1017 llvm::Value *Casted =
1018 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
1019 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(Casted), DstPtr);
1020 }
1021}
1022
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001023void CodeGenFunction::EmitFunctionEpilog(QualType RetTy,
1024 llvm::Value *ReturnValue) {
Daniel Dunbare126ab12008-09-10 02:41:04 +00001025 llvm::Value *RV = 0;
1026
1027 // Functions with no result always return void.
1028 if (ReturnValue) {
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001029 ABIArgInfo RetAI = getABIReturnInfo(RetTy, CGM.getTypes());
Daniel Dunbare126ab12008-09-10 02:41:04 +00001030
1031 switch (RetAI.getKind()) {
1032 case ABIArgInfo::StructRet:
Daniel Dunbar17d35372008-12-18 04:52:14 +00001033 if (RetTy->isAnyComplexType()) {
1034 // FIXME: Volatile
1035 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
1036 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
1037 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1038 EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy);
1039 } else {
1040 Builder.CreateStore(Builder.CreateLoad(ReturnValue),
1041 CurFn->arg_begin());
1042 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001043 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001044
Daniel Dunbare126ab12008-09-10 02:41:04 +00001045 case ABIArgInfo::Default:
1046 RV = Builder.CreateLoad(ReturnValue);
1047 break;
1048
Daniel Dunbar1358b202009-01-26 21:26:08 +00001049 case ABIArgInfo::Ignore:
1050 break;
1051
Daniel Dunbar73d66602008-09-10 07:04:09 +00001052 case ABIArgInfo::Coerce: {
Daniel Dunbar708d8a82009-01-27 01:36:03 +00001053 RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001054 break;
Daniel Dunbar73d66602008-09-10 07:04:09 +00001055 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001056
1057 case ABIArgInfo::ByVal:
1058 case ABIArgInfo::Expand:
1059 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001060 }
1061 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001062
1063 if (RV) {
1064 Builder.CreateRet(RV);
1065 } else {
1066 Builder.CreateRetVoid();
1067 }
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001068}
1069
1070RValue CodeGenFunction::EmitCall(llvm::Value *Callee,
Daniel Dunbare126ab12008-09-10 02:41:04 +00001071 QualType RetTy,
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001072 const CallArgList &CallArgs) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001073 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001074
1075 // Handle struct-return functions by passing a pointer to the
1076 // location that we would like to return into.
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001077 ABIArgInfo RetAI = getABIReturnInfo(RetTy, CGM.getTypes());
Daniel Dunbare126ab12008-09-10 02:41:04 +00001078 switch (RetAI.getKind()) {
1079 case ABIArgInfo::StructRet:
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001080 // Create a temporary alloca to hold the result of the call. :(
Daniel Dunbar04d35782008-09-17 00:51:38 +00001081 Args.push_back(CreateTempAlloca(ConvertType(RetTy)));
Daniel Dunbare126ab12008-09-10 02:41:04 +00001082 break;
1083
1084 case ABIArgInfo::Default:
Daniel Dunbar1358b202009-01-26 21:26:08 +00001085 case ABIArgInfo::Ignore:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001086 case ABIArgInfo::Coerce:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001087 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001088
1089 case ABIArgInfo::ByVal:
1090 case ABIArgInfo::Expand:
1091 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001092 }
1093
1094 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
1095 I != E; ++I) {
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001096 ABIArgInfo ArgInfo = getABIArgumentInfo(I->second, CGM.getTypes());
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001097 RValue RV = I->first;
Daniel Dunbar04d35782008-09-17 00:51:38 +00001098
1099 switch (ArgInfo.getKind()) {
1100 case ABIArgInfo::ByVal: // Default is byval
1101 case ABIArgInfo::Default:
1102 if (RV.isScalar()) {
1103 Args.push_back(RV.getScalarVal());
1104 } else if (RV.isComplex()) {
1105 // Make a temporary alloca to pass the argument.
1106 Args.push_back(CreateTempAlloca(ConvertType(I->second)));
1107 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
1108 } else {
1109 Args.push_back(RV.getAggregateAddr());
1110 }
1111 break;
1112
Daniel Dunbar1358b202009-01-26 21:26:08 +00001113 case ABIArgInfo::Ignore:
1114 break;
1115
Daniel Dunbar04d35782008-09-17 00:51:38 +00001116 case ABIArgInfo::StructRet:
1117 case ABIArgInfo::Coerce:
1118 assert(0 && "Invalid ABI kind for non-return argument");
1119 break;
1120
1121 case ABIArgInfo::Expand:
1122 ExpandTypeToArgs(I->second, RV, Args);
1123 break;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001124 }
1125 }
1126
1127 llvm::CallInst *CI = Builder.CreateCall(Callee,&Args[0],&Args[0]+Args.size());
Daniel Dunbare126ab12008-09-10 02:41:04 +00001128 CGCallInfo CallInfo(RetTy, CallArgs);
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001129
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001130 // FIXME: Provide TargetDecl so nounwind, noreturn, etc, etc get set.
Devang Patela85a9ef2008-09-25 21:02:23 +00001131 CodeGen::AttributeListType AttributeList;
1132 CGM.ConstructAttributeList(0,
Daniel Dunbar3ef2e852008-09-10 00:41:16 +00001133 CallInfo.argtypes_begin(), CallInfo.argtypes_end(),
Devang Patela85a9ef2008-09-25 21:02:23 +00001134 AttributeList);
1135 CI->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(),
1136 AttributeList.size()));
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001137
1138 if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee))
1139 CI->setCallingConv(F->getCallingConv());
1140 if (CI->getType() != llvm::Type::VoidTy)
1141 CI->setName("call");
Daniel Dunbare126ab12008-09-10 02:41:04 +00001142
1143 switch (RetAI.getKind()) {
1144 case ABIArgInfo::StructRet:
1145 if (RetTy->isAnyComplexType())
Daniel Dunbar04d35782008-09-17 00:51:38 +00001146 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Daniel Dunbar17d35372008-12-18 04:52:14 +00001147 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar04d35782008-09-17 00:51:38 +00001148 return RValue::getAggregate(Args[0]);
Daniel Dunbar17d35372008-12-18 04:52:14 +00001149 else
1150 return RValue::get(Builder.CreateLoad(Args[0]));
Daniel Dunbar22e30052008-09-11 01:48:57 +00001151
Daniel Dunbare126ab12008-09-10 02:41:04 +00001152 case ABIArgInfo::Default:
1153 return RValue::get(RetTy->isVoidType() ? 0 : CI);
1154
Daniel Dunbar1358b202009-01-26 21:26:08 +00001155 case ABIArgInfo::Ignore:
1156 return RValue::get(0);
1157
Daniel Dunbar73d66602008-09-10 07:04:09 +00001158 case ABIArgInfo::Coerce: {
Daniel Dunbar708d8a82009-01-27 01:36:03 +00001159 llvm::Value *V = CreateTempAlloca(ConvertType(RetTy), "coerce");
1160 CreateCoercedStore(CI, V, *this);
Anders Carlssonfccf7472008-11-25 22:21:48 +00001161 if (RetTy->isAnyComplexType())
1162 return RValue::getComplex(LoadComplexFromAddr(V, false));
Daniel Dunbar1358b202009-01-26 21:26:08 +00001163 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssonfccf7472008-11-25 22:21:48 +00001164 return RValue::getAggregate(V);
Daniel Dunbar1358b202009-01-26 21:26:08 +00001165 else
1166 return RValue::get(Builder.CreateLoad(V));
Daniel Dunbar73d66602008-09-10 07:04:09 +00001167 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001168
1169 case ABIArgInfo::ByVal:
1170 case ABIArgInfo::Expand:
1171 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001172 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001173
1174 assert(0 && "Unhandled ABIArgInfo::Kind");
1175 return RValue::get(0);
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001176}