blob: 72ba7f879af52a45aa283be70a376132a6fbe17b [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) {
158 assert(T->isSingleValueType() && "Can only coerce to simple types");
Daniel Dunbare126ab12008-09-10 02:41:04 +0000159 return ABIArgInfo(Coerce, T);
160 }
Daniel Dunbar22e30052008-09-11 01:48:57 +0000161 static ABIArgInfo getByVal(unsigned Alignment) {
162 return ABIArgInfo(ByVal, 0, Alignment);
163 }
Daniel Dunbar04d35782008-09-17 00:51:38 +0000164 static ABIArgInfo getExpand() {
165 return ABIArgInfo(Expand);
166 }
Daniel Dunbare126ab12008-09-10 02:41:04 +0000167
168 Kind getKind() const { return TheKind; }
169 bool isDefault() const { return TheKind == Default; }
170 bool isStructRet() const { return TheKind == StructRet; }
Daniel Dunbar1358b202009-01-26 21:26:08 +0000171 bool isIgnore() const { return TheKind == Ignore; }
Daniel Dunbare126ab12008-09-10 02:41:04 +0000172 bool isCoerce() const { return TheKind == Coerce; }
Daniel Dunbar22e30052008-09-11 01:48:57 +0000173 bool isByVal() const { return TheKind == ByVal; }
Daniel Dunbar04d35782008-09-17 00:51:38 +0000174 bool isExpand() const { return TheKind == Expand; }
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000175
176 // Coerce accessors
Daniel Dunbar73d66602008-09-10 07:04:09 +0000177 const llvm::Type *getCoerceToType() const {
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000178 assert(TheKind == Coerce && "Invalid kind!");
179 return TypeData;
180 }
Daniel Dunbar22e30052008-09-11 01:48:57 +0000181
182 // ByVal accessors
183 unsigned getByValAlignment() const {
184 assert(TheKind == ByVal && "Invalid kind!");
185 return UIntData;
186 }
Daniel Dunbare126ab12008-09-10 02:41:04 +0000187};
188
189/***/
190
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000191/* FIXME: All of this stuff should be part of the target interface
192 somehow. It is currently here because it is not clear how to factor
193 the targets to support this, since the Targets currently live in a
194 layer below types n'stuff.
195 */
196
197/// ABIInfo - Target specific hooks for defining how a type should be
198/// passed or returned from functions.
199class clang::ABIInfo {
200public:
201 virtual ~ABIInfo();
202
203 virtual ABIArgInfo classifyReturnType(QualType RetTy,
204 ASTContext &Context) const = 0;
205
206 virtual ABIArgInfo classifyArgumentType(QualType Ty,
207 ASTContext &Context) const = 0;
208};
209
210ABIInfo::~ABIInfo() {}
211
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000212/// isEmptyStruct - Return true iff a structure has no non-empty
213/// members. Note that a structure with a flexible array member is not
214/// considered empty.
215static bool isEmptyStruct(QualType T) {
216 const RecordType *RT = T->getAsStructureType();
217 if (!RT)
218 return 0;
219 const RecordDecl *RD = RT->getDecl();
220 if (RD->hasFlexibleArrayMember())
221 return false;
Douglas Gregor5d764842009-01-09 17:18:27 +0000222 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000223 e = RD->field_end(); i != e; ++i) {
224 const FieldDecl *FD = *i;
225 if (!isEmptyStruct(FD->getType()))
226 return false;
227 }
228 return true;
229}
230
231/// isSingleElementStruct - Determine if a structure is a "single
232/// element struct", i.e. it has exactly one non-empty field or
233/// exactly one field which is itself a single element
234/// struct. Structures with flexible array members are never
235/// considered single element structs.
236///
237/// \return The field declaration for the single non-empty field, if
238/// it exists.
239static const FieldDecl *isSingleElementStruct(QualType T) {
240 const RecordType *RT = T->getAsStructureType();
241 if (!RT)
242 return 0;
243
244 const RecordDecl *RD = RT->getDecl();
245 if (RD->hasFlexibleArrayMember())
246 return 0;
247
248 const FieldDecl *Found = 0;
Douglas Gregor5d764842009-01-09 17:18:27 +0000249 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000250 e = RD->field_end(); i != e; ++i) {
251 const FieldDecl *FD = *i;
252 QualType FT = FD->getType();
253
254 if (isEmptyStruct(FT)) {
255 // Ignore
256 } else if (Found) {
257 return 0;
258 } else if (!CodeGenFunction::hasAggregateLLVMType(FT)) {
259 Found = FD;
260 } else {
261 Found = isSingleElementStruct(FT);
262 if (!Found)
263 return 0;
264 }
265 }
266
267 return Found;
268}
269
270static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
271 if (!Ty->getAsBuiltinType() && !Ty->isPointerType())
272 return false;
273
274 uint64_t Size = Context.getTypeSize(Ty);
275 return Size == 32 || Size == 64;
276}
277
278static bool areAllFields32Or64BitBasicType(const RecordDecl *RD,
279 ASTContext &Context) {
Douglas Gregor5d764842009-01-09 17:18:27 +0000280 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000281 e = RD->field_end(); i != e; ++i) {
282 const FieldDecl *FD = *i;
283
284 if (!is32Or64BitBasicType(FD->getType(), Context))
285 return false;
286
287 // If this is a bit-field we need to make sure it is still a
288 // 32-bit or 64-bit type.
289 if (Expr *BW = FD->getBitWidth()) {
290 unsigned Width = BW->getIntegerConstantExprValue(Context).getZExtValue();
291 if (Width <= 16)
292 return false;
293 }
294 }
295 return true;
296}
297
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000298namespace {
299/// DefaultABIInfo - The default implementation for ABI specific
300/// details. This implementation provides information which results in
301/// sensible LLVM IR generation, but does not conform to any
302/// particular ABI.
303class DefaultABIInfo : public ABIInfo {
304 virtual ABIArgInfo classifyReturnType(QualType RetTy,
305 ASTContext &Context) const;
306
307 virtual ABIArgInfo classifyArgumentType(QualType RetTy,
308 ASTContext &Context) const;
309};
310
311/// X86_32ABIInfo - The X86-32 ABI information.
312class X86_32ABIInfo : public ABIInfo {
313public:
314 virtual ABIArgInfo classifyReturnType(QualType RetTy,
315 ASTContext &Context) const;
316
317 virtual ABIArgInfo classifyArgumentType(QualType RetTy,
318 ASTContext &Context) const;
319};
320}
321
322ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
323 ASTContext &Context) const {
Daniel Dunbare126ab12008-09-10 02:41:04 +0000324 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000325 // Classify "single element" structs as their element type.
326 const FieldDecl *SeltFD = isSingleElementStruct(RetTy);
327 if (SeltFD) {
328 QualType SeltTy = SeltFD->getType()->getDesugaredType();
329 if (const BuiltinType *BT = SeltTy->getAsBuiltinType()) {
330 // FIXME: This is gross, it would be nice if we could just
331 // pass back SeltTy and have clients deal with it. Is it worth
332 // supporting coerce to both LLVM and clang Types?
333 if (BT->isIntegerType()) {
334 uint64_t Size = Context.getTypeSize(SeltTy);
335 return ABIArgInfo::getCoerce(llvm::IntegerType::get((unsigned) Size));
336 } else if (BT->getKind() == BuiltinType::Float) {
337 return ABIArgInfo::getCoerce(llvm::Type::FloatTy);
338 } else if (BT->getKind() == BuiltinType::Double) {
339 return ABIArgInfo::getCoerce(llvm::Type::DoubleTy);
340 }
341 } else if (SeltTy->isPointerType()) {
342 // FIXME: It would be really nice if this could come out as
343 // the proper pointer type.
344 llvm::Type *PtrTy =
345 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
346 return ABIArgInfo::getCoerce(PtrTy);
347 }
348 }
349
Daniel Dunbar73d66602008-09-10 07:04:09 +0000350 uint64_t Size = Context.getTypeSize(RetTy);
351 if (Size == 8) {
352 return ABIArgInfo::getCoerce(llvm::Type::Int8Ty);
353 } else if (Size == 16) {
354 return ABIArgInfo::getCoerce(llvm::Type::Int16Ty);
355 } else if (Size == 32) {
356 return ABIArgInfo::getCoerce(llvm::Type::Int32Ty);
357 } else if (Size == 64) {
358 return ABIArgInfo::getCoerce(llvm::Type::Int64Ty);
359 } else {
360 return ABIArgInfo::getStructRet();
361 }
Daniel Dunbare126ab12008-09-10 02:41:04 +0000362 } else {
363 return ABIArgInfo::getDefault();
364 }
365}
366
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000367ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
368 ASTContext &Context) const {
Daniel Dunbar3158c592008-09-17 20:11:04 +0000369 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000370 // Structures with flexible arrays are always byval.
371 if (const RecordType *RT = Ty->getAsStructureType())
372 if (RT->getDecl()->hasFlexibleArrayMember())
373 return ABIArgInfo::getByVal(0);
374
375 // Expand empty structs (i.e. ignore)
376 uint64_t Size = Context.getTypeSize(Ty);
377 if (Ty->isStructureType() && Size == 0)
378 return ABIArgInfo::getExpand();
379
380 // Expand structs with size <= 128-bits which consist only of
381 // basic types (int, long long, float, double, xxx*). This is
382 // non-recursive and does not ignore empty fields.
383 if (const RecordType *RT = Ty->getAsStructureType()) {
384 if (Context.getTypeSize(Ty) <= 4*32 &&
385 areAllFields32Or64BitBasicType(RT->getDecl(), Context))
386 return ABIArgInfo::getExpand();
387 }
388
Daniel Dunbar22e30052008-09-11 01:48:57 +0000389 return ABIArgInfo::getByVal(0);
390 } else {
391 return ABIArgInfo::getDefault();
392 }
393}
394
Daniel Dunbare09a9692009-01-24 08:32:22 +0000395namespace {
396/// X86_32ABIInfo - The X86_64 ABI information.
397class X86_64ABIInfo : public ABIInfo {
398 enum Class {
399 Integer = 0,
400 SSE,
401 SSEUp,
402 X87,
403 X87Up,
404 ComplexX87,
405 NoClass,
406 Memory
407 };
408
409 /// classify - Determine the x86_64 register classes in which the
410 /// given type T should be passed.
411 ///
412 /// \param Lo - The classification for the low word of the type.
413 /// \param Hi - The classification for the high word of the type.
414 ///
415 /// If a word is unused its result will be NoClass; if a type should
416 /// be passed in Memory then at least the classification of \arg Lo
417 /// will be Memory.
418 ///
419 /// The \arg Lo class will be NoClass iff the argument is ignored.
420 ///
421 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
422 /// be NoClass.
423 void classify(QualType T, ASTContext &Context,
424 Class &Lo, Class &Hi) const;
425
426public:
427 virtual ABIArgInfo classifyReturnType(QualType RetTy,
428 ASTContext &Context) const;
429
430 virtual ABIArgInfo classifyArgumentType(QualType RetTy,
431 ASTContext &Context) const;
432};
433}
434
435void X86_64ABIInfo::classify(QualType Ty,
436 ASTContext &Context,
437 Class &Lo, Class &Hi) const {
438 Lo = Memory;
439 Hi = NoClass;
440 if (const BuiltinType *BT = Ty->getAsBuiltinType()) {
441 BuiltinType::Kind k = BT->getKind();
442
Daniel Dunbar1358b202009-01-26 21:26:08 +0000443 if (k == BuiltinType::Void) {
444 Lo = NoClass;
445 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
Daniel Dunbare09a9692009-01-24 08:32:22 +0000446 Lo = Integer;
447 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
448 Lo = SSE;
449 } else if (k == BuiltinType::LongDouble) {
450 Lo = X87;
451 Hi = X87Up;
452 }
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000453
454 // FIXME: _Decimal32 and _Decimal64 are SSE.
455 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Daniel Dunbare09a9692009-01-24 08:32:22 +0000456 // FIXME: __int128 is (Integer, Integer).
457 } else if (Ty->isPointerLikeType() || Ty->isBlockPointerType() ||
458 Ty->isObjCQualifiedInterfaceType()) {
459 Lo = Integer;
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000460 } else if (const VectorType *VT = Ty->getAsVectorType()) {
461 unsigned Size = Context.getTypeSize(VT);
462 if (Size == 64) {
463 // FIXME: For some reason, gcc appears to be treating <1 x
464 // double> as INTEGER; this seems wrong, but we will match for
465 // now (icc rejects <1 x double>, so...).
466 Lo = (VT->getElementType() == Context.DoubleTy) ? Integer : SSE;
467 } else if (Size == 128) {
468 Lo = SSE;
469 Hi = SSEUp;
470 }
Daniel Dunbare09a9692009-01-24 08:32:22 +0000471 } else if (const ComplexType *CT = Ty->getAsComplexType()) {
472 QualType ET = CT->getElementType();
473
474 if (ET == Context.FloatTy)
475 Lo = SSE;
476 else if (ET == Context.DoubleTy)
477 Lo = Hi = SSE;
478 else if (ET == Context.LongDoubleTy)
479 Lo = ComplexX87;
480 }
481}
482
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000483ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
484 ASTContext &Context) const {
Daniel Dunbare09a9692009-01-24 08:32:22 +0000485 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
486 // classification algorithm.
487 X86_64ABIInfo::Class Lo, Hi;
488 classify(RetTy, Context, Lo, Hi);
489
490 const llvm::Type *ResType = 0;
491 switch (Lo) {
492 case NoClass:
Daniel Dunbar1358b202009-01-26 21:26:08 +0000493 return ABIArgInfo::getIgnore();
Daniel Dunbare09a9692009-01-24 08:32:22 +0000494
495 case SSEUp:
496 case X87Up:
497 assert(0 && "Invalid classification for lo word.");
498
499 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
500 // hidden argument, i.e. structret.
501 case Memory:
502 return ABIArgInfo::getStructRet();
503
504 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
505 // available register of the sequence %rax, %rdx is used.
506 case Integer:
507 ResType = llvm::Type::Int64Ty; break;
508
509 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
510 // available SSE register of the sequence %xmm0, %xmm1 is used.
511 case SSE:
512 ResType = llvm::Type::DoubleTy; break;
513
514 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
515 // returned on the X87 stack in %st0 as 80-bit x87 number.
516 case X87:
517 ResType = llvm::Type::X86_FP80Ty; break;
518
519 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
520 // part of the value is returned in %st0 and the imaginary part in
521 // %st1.
522 case ComplexX87:
523 assert(Hi == NoClass && "Unexpected ComplexX87 classification.");
524 ResType = llvm::VectorType::get(llvm::Type::X86_FP80Ty, 2);
525 break;
526 }
527
528 switch (Hi) {
529 // Memory was handled previously, and ComplexX87 and X87 should
530 // never occur as hi classes.
531 case Memory:
532 case X87:
533 case ComplexX87:
534 assert(0 && "Invalid classification for hi word.");
535
536 case NoClass: break;
537 case Integer:
538 assert(0 && "FIXME: Implement MRV"); break;
539 case SSE:
540 assert(0 && "FIXME: Implement MRV"); break;
541
542 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
543 // is passed in the upper half of the last used SSE register.
544 //
545 // SSEUP should always be preceeded by SSE, just widen.
546 case SSEUp:
547 assert(Lo == SSE && "Unexpected SSEUp classification.");
548 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
549 break;
550
551 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
552 // returned together with the previos X87 value in %st0.
553 //
554 // X87UP should always be preceeded by X87, so we don't need to do
555 // anything here.
556 case X87Up:
557 assert(Lo == X87 && "Unexpected X87Up classification.");
558 break;
559 }
560
561 return ABIArgInfo::getCoerce(ResType);
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000562}
563
564ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty,
565 ASTContext &Context) const {
566 return ABIArgInfo::getDefault();
567}
568
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000569ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
570 ASTContext &Context) const {
571 return ABIArgInfo::getDefault();
572}
573
574ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
575 ASTContext &Context) const {
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000576 return ABIArgInfo::getDefault();
577}
578
579const ABIInfo &CodeGenTypes::getABIInfo() const {
580 if (TheABIInfo)
581 return *TheABIInfo;
582
583 // For now we just cache this in the CodeGenTypes and don't bother
584 // to free it.
585 const char *TargetPrefix = getContext().Target.getTargetPrefix();
586 if (strcmp(TargetPrefix, "x86") == 0) {
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000587 switch (getContext().Target.getPointerWidth(0)) {
588 case 32:
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000589 return *(TheABIInfo = new X86_32ABIInfo());
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000590 case 64:
Daniel Dunbare09a9692009-01-24 08:32:22 +0000591 if (UseX86_64ABI)
592 return *(TheABIInfo = new X86_64ABIInfo());
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000593 }
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000594 }
595
596 return *(TheABIInfo = new DefaultABIInfo);
597}
598
599// getABIReturnInfo - Wrap the ABIInfo getABIReturnInfo, altering
600// "default" types to StructRet when appropriate for simplicity.
601static ABIArgInfo getABIReturnInfo(QualType Ty, CodeGenTypes &CGT) {
602 assert(!Ty->isArrayType() &&
603 "Array types cannot be passed directly.");
604 ABIArgInfo Info = CGT.getABIInfo().classifyReturnType(Ty, CGT.getContext());
Daniel Dunbar3158c592008-09-17 20:11:04 +0000605 // Ensure default on aggregate types is StructRet.
606 if (Info.isDefault() && CodeGenFunction::hasAggregateLLVMType(Ty))
607 return ABIArgInfo::getStructRet();
608 return Info;
609}
610
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000611// getABIArgumentInfo - Wrap the ABIInfo getABIReturnInfo, altering
612// "default" types to ByVal when appropriate for simplicity.
613static ABIArgInfo getABIArgumentInfo(QualType Ty, CodeGenTypes &CGT) {
614 assert(!Ty->isArrayType() &&
615 "Array types cannot be passed directly.");
616 ABIArgInfo Info = CGT.getABIInfo().classifyArgumentType(Ty, CGT.getContext());
Daniel Dunbar3158c592008-09-17 20:11:04 +0000617 // Ensure default on aggregate types is ByVal.
618 if (Info.isDefault() && CodeGenFunction::hasAggregateLLVMType(Ty))
619 return ABIArgInfo::getByVal(0);
620 return Info;
621}
622
Daniel Dunbare126ab12008-09-10 02:41:04 +0000623/***/
624
Daniel Dunbar04d35782008-09-17 00:51:38 +0000625void CodeGenTypes::GetExpandedTypes(QualType Ty,
626 std::vector<const llvm::Type*> &ArgTys) {
627 const RecordType *RT = Ty->getAsStructureType();
628 assert(RT && "Can only expand structure types.");
629 const RecordDecl *RD = RT->getDecl();
630 assert(!RD->hasFlexibleArrayMember() &&
631 "Cannot expand structure with flexible array.");
632
Douglas Gregor5d764842009-01-09 17:18:27 +0000633 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar04d35782008-09-17 00:51:38 +0000634 e = RD->field_end(); i != e; ++i) {
635 const FieldDecl *FD = *i;
636 assert(!FD->isBitField() &&
637 "Cannot expand structure with bit-field members.");
638
639 QualType FT = FD->getType();
640 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
641 GetExpandedTypes(FT, ArgTys);
642 } else {
643 ArgTys.push_back(ConvertType(FT));
644 }
645 }
646}
647
648llvm::Function::arg_iterator
649CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
650 llvm::Function::arg_iterator AI) {
651 const RecordType *RT = Ty->getAsStructureType();
652 assert(RT && "Can only expand structure types.");
653
654 RecordDecl *RD = RT->getDecl();
655 assert(LV.isSimple() &&
656 "Unexpected non-simple lvalue during struct expansion.");
657 llvm::Value *Addr = LV.getAddress();
658 for (RecordDecl::field_iterator i = RD->field_begin(),
659 e = RD->field_end(); i != e; ++i) {
660 FieldDecl *FD = *i;
661 QualType FT = FD->getType();
662
663 // FIXME: What are the right qualifiers here?
664 LValue LV = EmitLValueForField(Addr, FD, false, 0);
665 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
666 AI = ExpandTypeFromArgs(FT, LV, AI);
667 } else {
668 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
669 ++AI;
670 }
671 }
672
673 return AI;
674}
675
676void
677CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
678 llvm::SmallVector<llvm::Value*, 16> &Args) {
679 const RecordType *RT = Ty->getAsStructureType();
680 assert(RT && "Can only expand structure types.");
681
682 RecordDecl *RD = RT->getDecl();
683 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
684 llvm::Value *Addr = RV.getAggregateAddr();
685 for (RecordDecl::field_iterator i = RD->field_begin(),
686 e = RD->field_end(); i != e; ++i) {
687 FieldDecl *FD = *i;
688 QualType FT = FD->getType();
689
690 // FIXME: What are the right qualifiers here?
691 LValue LV = EmitLValueForField(Addr, FD, false, 0);
692 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
693 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
694 } else {
695 RValue RV = EmitLoadOfLValue(LV, FT);
696 assert(RV.isScalar() &&
697 "Unexpected non-scalar rvalue during struct expansion.");
698 Args.push_back(RV.getScalarVal());
699 }
700 }
701}
702
703/***/
704
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000705const llvm::FunctionType *
Daniel Dunbara9976a22008-09-10 07:00:50 +0000706CodeGenTypes::GetFunctionType(const CGCallInfo &CI, bool IsVariadic) {
707 return GetFunctionType(CI.argtypes_begin(), CI.argtypes_end(), IsVariadic);
708}
709
710const llvm::FunctionType *
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000711CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI) {
Daniel Dunbara9976a22008-09-10 07:00:50 +0000712 return GetFunctionType(FI.argtypes_begin(), FI.argtypes_end(), FI.isVariadic());
713}
714
715const llvm::FunctionType *
716CodeGenTypes::GetFunctionType(ArgTypeIterator begin, ArgTypeIterator end,
717 bool IsVariadic) {
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000718 std::vector<const llvm::Type*> ArgTys;
719
720 const llvm::Type *ResultType = 0;
721
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000722 QualType RetTy = *begin;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000723 ABIArgInfo RetAI = getABIReturnInfo(RetTy, *this);
Daniel Dunbar22e30052008-09-11 01:48:57 +0000724 switch (RetAI.getKind()) {
725 case ABIArgInfo::ByVal:
726 case ABIArgInfo::Expand:
727 assert(0 && "Invalid ABI kind for return argument");
728
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000729 case ABIArgInfo::Default:
730 if (RetTy->isVoidType()) {
731 ResultType = llvm::Type::VoidTy;
732 } else {
Daniel Dunbara9976a22008-09-10 07:00:50 +0000733 ResultType = ConvertType(RetTy);
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000734 }
735 break;
736
737 case ABIArgInfo::StructRet: {
738 ResultType = llvm::Type::VoidTy;
Daniel Dunbara9976a22008-09-10 07:00:50 +0000739 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000740 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
741 break;
742 }
743
Daniel Dunbar1358b202009-01-26 21:26:08 +0000744 case ABIArgInfo::Ignore:
745 ResultType = llvm::Type::VoidTy;
746 break;
747
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000748 case ABIArgInfo::Coerce:
Daniel Dunbar73d66602008-09-10 07:04:09 +0000749 ResultType = RetAI.getCoerceToType();
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000750 break;
751 }
752
753 for (++begin; begin != end; ++begin) {
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000754 ABIArgInfo AI = getABIArgumentInfo(*begin, *this);
Daniel Dunbara9976a22008-09-10 07:00:50 +0000755 const llvm::Type *Ty = ConvertType(*begin);
Daniel Dunbar22e30052008-09-11 01:48:57 +0000756
757 switch (AI.getKind()) {
Daniel Dunbar1358b202009-01-26 21:26:08 +0000758 case ABIArgInfo::Ignore:
759 break;
760
Daniel Dunbar04d35782008-09-17 00:51:38 +0000761 case ABIArgInfo::Coerce:
Daniel Dunbar22e30052008-09-11 01:48:57 +0000762 case ABIArgInfo::StructRet:
763 assert(0 && "Invalid ABI kind for non-return argument");
764
765 case ABIArgInfo::ByVal:
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000766 // byval arguments are always on the stack, which is addr space #0.
767 ArgTys.push_back(llvm::PointerType::getUnqual(Ty));
Daniel Dunbar22e30052008-09-11 01:48:57 +0000768 assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled");
769 break;
770
771 case ABIArgInfo::Default:
772 ArgTys.push_back(Ty);
773 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +0000774
775 case ABIArgInfo::Expand:
Daniel Dunbar04d35782008-09-17 00:51:38 +0000776 GetExpandedTypes(*begin, ArgTys);
Daniel Dunbar22e30052008-09-11 01:48:57 +0000777 break;
778 }
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000779 }
780
Daniel Dunbara9976a22008-09-10 07:00:50 +0000781 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000782}
783
Daniel Dunbar3ef2e852008-09-10 00:41:16 +0000784bool CodeGenModule::ReturnTypeUsesSret(QualType RetTy) {
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000785 return getABIReturnInfo(RetTy, getTypes()).isStructRet();
Daniel Dunbar49f5a0d2008-09-09 23:48:28 +0000786}
787
Devang Patela85a9ef2008-09-25 21:02:23 +0000788void CodeGenModule::ConstructAttributeList(const Decl *TargetDecl,
Daniel Dunbare126ab12008-09-10 02:41:04 +0000789 ArgTypeIterator begin,
790 ArgTypeIterator end,
Devang Patela85a9ef2008-09-25 21:02:23 +0000791 AttributeListType &PAL) {
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000792 unsigned FuncAttrs = 0;
Devang Patel2bb6eb82008-09-26 22:53:57 +0000793 unsigned RetAttrs = 0;
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000794
795 if (TargetDecl) {
796 if (TargetDecl->getAttr<NoThrowAttr>())
Devang Patela85a9ef2008-09-25 21:02:23 +0000797 FuncAttrs |= llvm::Attribute::NoUnwind;
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000798 if (TargetDecl->getAttr<NoReturnAttr>())
Devang Patela85a9ef2008-09-25 21:02:23 +0000799 FuncAttrs |= llvm::Attribute::NoReturn;
Anders Carlssondd6791c2008-10-05 23:32:53 +0000800 if (TargetDecl->getAttr<PureAttr>())
801 FuncAttrs |= llvm::Attribute::ReadOnly;
802 if (TargetDecl->getAttr<ConstAttr>())
803 FuncAttrs |= llvm::Attribute::ReadNone;
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000804 }
805
Daniel Dunbare126ab12008-09-10 02:41:04 +0000806 QualType RetTy = *begin;
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000807 unsigned Index = 1;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000808 ABIArgInfo RetAI = getABIReturnInfo(RetTy, getTypes());
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000809 switch (RetAI.getKind()) {
Daniel Dunbare126ab12008-09-10 02:41:04 +0000810 case ABIArgInfo::Default:
811 if (RetTy->isPromotableIntegerType()) {
812 if (RetTy->isSignedIntegerType()) {
Devang Patel2bb6eb82008-09-26 22:53:57 +0000813 RetAttrs |= llvm::Attribute::SExt;
Daniel Dunbare126ab12008-09-10 02:41:04 +0000814 } else if (RetTy->isUnsignedIntegerType()) {
Devang Patel2bb6eb82008-09-26 22:53:57 +0000815 RetAttrs |= llvm::Attribute::ZExt;
Daniel Dunbare126ab12008-09-10 02:41:04 +0000816 }
817 }
818 break;
819
820 case ABIArgInfo::StructRet:
Devang Patela85a9ef2008-09-25 21:02:23 +0000821 PAL.push_back(llvm::AttributeWithIndex::get(Index,
822 llvm::Attribute::StructRet|
823 llvm::Attribute::NoAlias));
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000824 ++Index;
Daniel Dunbare126ab12008-09-10 02:41:04 +0000825 break;
826
Daniel Dunbar1358b202009-01-26 21:26:08 +0000827 case ABIArgInfo::Ignore:
Daniel Dunbare126ab12008-09-10 02:41:04 +0000828 case ABIArgInfo::Coerce:
Daniel Dunbare126ab12008-09-10 02:41:04 +0000829 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +0000830
831 case ABIArgInfo::ByVal:
832 case ABIArgInfo::Expand:
833 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000834 }
Daniel Dunbare126ab12008-09-10 02:41:04 +0000835
Devang Patel2bb6eb82008-09-26 22:53:57 +0000836 if (RetAttrs)
837 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Daniel Dunbar04d35782008-09-17 00:51:38 +0000838 for (++begin; begin != end; ++begin) {
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000839 QualType ParamType = *begin;
Devang Patela85a9ef2008-09-25 21:02:23 +0000840 unsigned Attributes = 0;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000841 ABIArgInfo AI = getABIArgumentInfo(ParamType, getTypes());
Daniel Dunbar22e30052008-09-11 01:48:57 +0000842
843 switch (AI.getKind()) {
844 case ABIArgInfo::StructRet:
Daniel Dunbar04d35782008-09-17 00:51:38 +0000845 case ABIArgInfo::Coerce:
Daniel Dunbar22e30052008-09-11 01:48:57 +0000846 assert(0 && "Invalid ABI kind for non-return argument");
847
848 case ABIArgInfo::ByVal:
Devang Patela85a9ef2008-09-25 21:02:23 +0000849 Attributes |= llvm::Attribute::ByVal;
Daniel Dunbar22e30052008-09-11 01:48:57 +0000850 assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled");
851 break;
852
853 case ABIArgInfo::Default:
854 if (ParamType->isPromotableIntegerType()) {
855 if (ParamType->isSignedIntegerType()) {
Devang Patela85a9ef2008-09-25 21:02:23 +0000856 Attributes |= llvm::Attribute::SExt;
Daniel Dunbar22e30052008-09-11 01:48:57 +0000857 } else if (ParamType->isUnsignedIntegerType()) {
Devang Patela85a9ef2008-09-25 21:02:23 +0000858 Attributes |= llvm::Attribute::ZExt;
Daniel Dunbar22e30052008-09-11 01:48:57 +0000859 }
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000860 }
Daniel Dunbar22e30052008-09-11 01:48:57 +0000861 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +0000862
Daniel Dunbar1358b202009-01-26 21:26:08 +0000863 case ABIArgInfo::Ignore:
864 // Skip increment, no matching LLVM parameter.
865 continue;
866
Daniel Dunbar04d35782008-09-17 00:51:38 +0000867 case ABIArgInfo::Expand: {
868 std::vector<const llvm::Type*> Tys;
869 // FIXME: This is rather inefficient. Do we ever actually need
870 // to do anything here? The result should be just reconstructed
871 // on the other side, so extension should be a non-issue.
872 getTypes().GetExpandedTypes(ParamType, Tys);
873 Index += Tys.size();
874 continue;
875 }
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000876 }
Daniel Dunbar22e30052008-09-11 01:48:57 +0000877
Devang Patela85a9ef2008-09-25 21:02:23 +0000878 if (Attributes)
879 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar04d35782008-09-17 00:51:38 +0000880 ++Index;
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000881 }
Devang Patel2bb6eb82008-09-26 22:53:57 +0000882 if (FuncAttrs)
883 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
884
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000885}
886
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000887void CodeGenFunction::EmitFunctionProlog(llvm::Function *Fn,
888 QualType RetTy,
889 const FunctionArgList &Args) {
890 // Emit allocs for param decls. Give the LLVM Argument nodes names.
891 llvm::Function::arg_iterator AI = Fn->arg_begin();
892
893 // Name the struct return argument.
Daniel Dunbare126ab12008-09-10 02:41:04 +0000894 if (CGM.ReturnTypeUsesSret(RetTy)) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000895 AI->setName("agg.result");
896 ++AI;
897 }
898
899 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbar04d35782008-09-17 00:51:38 +0000900 i != e; ++i) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000901 const VarDecl *Arg = i->first;
Daniel Dunbar04d35782008-09-17 00:51:38 +0000902 QualType Ty = i->second;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000903 ABIArgInfo ArgI = getABIArgumentInfo(Ty, CGM.getTypes());
Daniel Dunbar22e30052008-09-11 01:48:57 +0000904
905 switch (ArgI.getKind()) {
906 case ABIArgInfo::ByVal:
907 case ABIArgInfo::Default: {
908 assert(AI != Fn->arg_end() && "Argument mismatch!");
909 llvm::Value* V = AI;
Daniel Dunbar04d35782008-09-17 00:51:38 +0000910 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
Daniel Dunbar22e30052008-09-11 01:48:57 +0000911 // This must be a promotion, for something like
912 // "void a(x) short x; {..."
Daniel Dunbar04d35782008-09-17 00:51:38 +0000913 V = EmitScalarConversion(V, Ty, Arg->getType());
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000914 }
Daniel Dunbar22e30052008-09-11 01:48:57 +0000915 EmitParmDecl(*Arg, V);
916 break;
917 }
Daniel Dunbar04d35782008-09-17 00:51:38 +0000918
919 case ABIArgInfo::Expand: {
920 // If this was structure was expand into multiple arguments then
921 // we need to create a temporary and reconstruct it from the
922 // arguments.
Chris Lattner6c5ec622008-11-24 04:00:27 +0000923 std::string Name = Arg->getNameAsString();
Daniel Dunbar04d35782008-09-17 00:51:38 +0000924 llvm::Value *Temp = CreateTempAlloca(ConvertType(Ty),
925 (Name + ".addr").c_str());
926 // FIXME: What are the right qualifiers here?
927 llvm::Function::arg_iterator End =
928 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI);
929 EmitParmDecl(*Arg, Temp);
Daniel Dunbar22e30052008-09-11 01:48:57 +0000930
Daniel Dunbar04d35782008-09-17 00:51:38 +0000931 // Name the arguments used in expansion and increment AI.
932 unsigned Index = 0;
933 for (; AI != End; ++AI, ++Index)
934 AI->setName(Name + "." + llvm::utostr(Index));
935 continue;
936 }
Daniel Dunbar1358b202009-01-26 21:26:08 +0000937
938 case ABIArgInfo::Ignore:
939 break;
940
Daniel Dunbar22e30052008-09-11 01:48:57 +0000941 case ABIArgInfo::Coerce:
Daniel Dunbar22e30052008-09-11 01:48:57 +0000942 case ABIArgInfo::StructRet:
943 assert(0 && "Invalid ABI kind for non-return argument");
944 }
Daniel Dunbar04d35782008-09-17 00:51:38 +0000945
946 ++AI;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000947 }
948 assert(AI == Fn->arg_end() && "Argument mismatch!");
949}
950
Daniel Dunbar708d8a82009-01-27 01:36:03 +0000951/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
952/// a pointer to an object of type \arg Ty.
953///
954/// This safely handles the case when the src type is smaller than the
955/// destination type; in this situation the values of bits which not
956/// present in the src are undefined.
957static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
958 const llvm::Type *Ty,
959 CodeGenFunction &CGF) {
960 const llvm::Type *SrcTy =
961 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
962 unsigned SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
963 unsigned DstSize = CGF.CGM.getTargetData().getTypePaddedSize(Ty);
964
965 // If load is legal, just bitcase the src pointer.
966 if (SrcSize == DstSize) {
967 llvm::Value *Casted =
968 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
969 return CGF.Builder.CreateLoad(Casted);
970 } else {
971 assert(SrcSize < DstSize && "Coercion is losing source bits!");
972
973 // Otherwise do coercion through memory. This is stupid, but
974 // simple.
975 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
976 llvm::Value *Casted =
977 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
978 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
979 return CGF.Builder.CreateLoad(Tmp);
980 }
981}
982
983/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
984/// where the source and destination may have different types.
985///
986/// This safely handles the case when the src type is larger than the
987/// destination type; the upper bits of the src will be lost.
988static void CreateCoercedStore(llvm::Value *Src,
989 llvm::Value *DstPtr,
990 CodeGenFunction &CGF) {
991 const llvm::Type *SrcTy = Src->getType();
992 const llvm::Type *DstTy =
993 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
994
995 unsigned SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
996 unsigned DstSize = CGF.CGM.getTargetData().getTypePaddedSize(DstTy);
997
998 // If store is legal, just bitcase the src pointer.
999 if (SrcSize == DstSize) {
1000 llvm::Value *Casted =
1001 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
1002 CGF.Builder.CreateStore(Src, Casted);
1003 } else {
1004 assert(SrcSize > DstSize && "Coercion is missing bits!");
1005
1006 // Otherwise do coercion through memory. This is stupid, but
1007 // simple.
1008 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
1009 CGF.Builder.CreateStore(Src, Tmp);
1010 llvm::Value *Casted =
1011 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
1012 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(Casted), DstPtr);
1013 }
1014}
1015
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001016void CodeGenFunction::EmitFunctionEpilog(QualType RetTy,
1017 llvm::Value *ReturnValue) {
Daniel Dunbare126ab12008-09-10 02:41:04 +00001018 llvm::Value *RV = 0;
1019
1020 // Functions with no result always return void.
1021 if (ReturnValue) {
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001022 ABIArgInfo RetAI = getABIReturnInfo(RetTy, CGM.getTypes());
Daniel Dunbare126ab12008-09-10 02:41:04 +00001023
1024 switch (RetAI.getKind()) {
1025 case ABIArgInfo::StructRet:
Daniel Dunbar17d35372008-12-18 04:52:14 +00001026 if (RetTy->isAnyComplexType()) {
1027 // FIXME: Volatile
1028 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
1029 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
1030 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1031 EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy);
1032 } else {
1033 Builder.CreateStore(Builder.CreateLoad(ReturnValue),
1034 CurFn->arg_begin());
1035 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001036 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001037
Daniel Dunbare126ab12008-09-10 02:41:04 +00001038 case ABIArgInfo::Default:
1039 RV = Builder.CreateLoad(ReturnValue);
1040 break;
1041
Daniel Dunbar1358b202009-01-26 21:26:08 +00001042 case ABIArgInfo::Ignore:
1043 break;
1044
Daniel Dunbar73d66602008-09-10 07:04:09 +00001045 case ABIArgInfo::Coerce: {
Daniel Dunbar708d8a82009-01-27 01:36:03 +00001046 RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001047 break;
Daniel Dunbar73d66602008-09-10 07:04:09 +00001048 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001049
1050 case ABIArgInfo::ByVal:
1051 case ABIArgInfo::Expand:
1052 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001053 }
1054 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001055
1056 if (RV) {
1057 Builder.CreateRet(RV);
1058 } else {
1059 Builder.CreateRetVoid();
1060 }
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001061}
1062
1063RValue CodeGenFunction::EmitCall(llvm::Value *Callee,
Daniel Dunbare126ab12008-09-10 02:41:04 +00001064 QualType RetTy,
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001065 const CallArgList &CallArgs) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001066 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001067
1068 // Handle struct-return functions by passing a pointer to the
1069 // location that we would like to return into.
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001070 ABIArgInfo RetAI = getABIReturnInfo(RetTy, CGM.getTypes());
Daniel Dunbare126ab12008-09-10 02:41:04 +00001071 switch (RetAI.getKind()) {
1072 case ABIArgInfo::StructRet:
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001073 // Create a temporary alloca to hold the result of the call. :(
Daniel Dunbar04d35782008-09-17 00:51:38 +00001074 Args.push_back(CreateTempAlloca(ConvertType(RetTy)));
Daniel Dunbare126ab12008-09-10 02:41:04 +00001075 break;
1076
1077 case ABIArgInfo::Default:
Daniel Dunbar1358b202009-01-26 21:26:08 +00001078 case ABIArgInfo::Ignore:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001079 case ABIArgInfo::Coerce:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001080 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001081
1082 case ABIArgInfo::ByVal:
1083 case ABIArgInfo::Expand:
1084 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001085 }
1086
1087 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
1088 I != E; ++I) {
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001089 ABIArgInfo ArgInfo = getABIArgumentInfo(I->second, CGM.getTypes());
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001090 RValue RV = I->first;
Daniel Dunbar04d35782008-09-17 00:51:38 +00001091
1092 switch (ArgInfo.getKind()) {
1093 case ABIArgInfo::ByVal: // Default is byval
1094 case ABIArgInfo::Default:
1095 if (RV.isScalar()) {
1096 Args.push_back(RV.getScalarVal());
1097 } else if (RV.isComplex()) {
1098 // Make a temporary alloca to pass the argument.
1099 Args.push_back(CreateTempAlloca(ConvertType(I->second)));
1100 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
1101 } else {
1102 Args.push_back(RV.getAggregateAddr());
1103 }
1104 break;
1105
Daniel Dunbar1358b202009-01-26 21:26:08 +00001106 case ABIArgInfo::Ignore:
1107 break;
1108
Daniel Dunbar04d35782008-09-17 00:51:38 +00001109 case ABIArgInfo::StructRet:
1110 case ABIArgInfo::Coerce:
1111 assert(0 && "Invalid ABI kind for non-return argument");
1112 break;
1113
1114 case ABIArgInfo::Expand:
1115 ExpandTypeToArgs(I->second, RV, Args);
1116 break;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001117 }
1118 }
1119
1120 llvm::CallInst *CI = Builder.CreateCall(Callee,&Args[0],&Args[0]+Args.size());
Daniel Dunbare126ab12008-09-10 02:41:04 +00001121 CGCallInfo CallInfo(RetTy, CallArgs);
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001122
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001123 // FIXME: Provide TargetDecl so nounwind, noreturn, etc, etc get set.
Devang Patela85a9ef2008-09-25 21:02:23 +00001124 CodeGen::AttributeListType AttributeList;
1125 CGM.ConstructAttributeList(0,
Daniel Dunbar3ef2e852008-09-10 00:41:16 +00001126 CallInfo.argtypes_begin(), CallInfo.argtypes_end(),
Devang Patela85a9ef2008-09-25 21:02:23 +00001127 AttributeList);
1128 CI->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(),
1129 AttributeList.size()));
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001130
1131 if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee))
1132 CI->setCallingConv(F->getCallingConv());
1133 if (CI->getType() != llvm::Type::VoidTy)
1134 CI->setName("call");
Daniel Dunbare126ab12008-09-10 02:41:04 +00001135
1136 switch (RetAI.getKind()) {
1137 case ABIArgInfo::StructRet:
1138 if (RetTy->isAnyComplexType())
Daniel Dunbar04d35782008-09-17 00:51:38 +00001139 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Daniel Dunbar17d35372008-12-18 04:52:14 +00001140 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar04d35782008-09-17 00:51:38 +00001141 return RValue::getAggregate(Args[0]);
Daniel Dunbar17d35372008-12-18 04:52:14 +00001142 else
1143 return RValue::get(Builder.CreateLoad(Args[0]));
Daniel Dunbar22e30052008-09-11 01:48:57 +00001144
Daniel Dunbare126ab12008-09-10 02:41:04 +00001145 case ABIArgInfo::Default:
1146 return RValue::get(RetTy->isVoidType() ? 0 : CI);
1147
Daniel Dunbar1358b202009-01-26 21:26:08 +00001148 case ABIArgInfo::Ignore:
1149 return RValue::get(0);
1150
Daniel Dunbar73d66602008-09-10 07:04:09 +00001151 case ABIArgInfo::Coerce: {
Daniel Dunbar708d8a82009-01-27 01:36:03 +00001152 llvm::Value *V = CreateTempAlloca(ConvertType(RetTy), "coerce");
1153 CreateCoercedStore(CI, V, *this);
Anders Carlssonfccf7472008-11-25 22:21:48 +00001154 if (RetTy->isAnyComplexType())
1155 return RValue::getComplex(LoadComplexFromAddr(V, false));
Daniel Dunbar1358b202009-01-26 21:26:08 +00001156 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssonfccf7472008-11-25 22:21:48 +00001157 return RValue::getAggregate(V);
Daniel Dunbar1358b202009-01-26 21:26:08 +00001158 else
1159 return RValue::get(Builder.CreateLoad(V));
Daniel Dunbar73d66602008-09-10 07:04:09 +00001160 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001161
1162 case ABIArgInfo::ByVal:
1163 case ABIArgInfo::Expand:
1164 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001165 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001166
1167 assert(0 && "Unhandled ABIArgInfo::Kind");
1168 return RValue::get(0);
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001169}