blob: 9e632c0e272eadba365516c79a93e9cdd3d3699d [file] [log] [blame]
Daniel Dunbar0dbe2272008-09-08 21:33:45 +00001//===----- CGCall.h - Encapsulate calling convention details ----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// These classes wrap the information about a call or function
11// definition used to handle ABI compliancy.
12//
13//===----------------------------------------------------------------------===//
14
15#include "CGCall.h"
16#include "CodeGenFunction.h"
Daniel Dunbarb7688072008-09-10 00:41:16 +000017#include "CodeGenModule.h"
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +000018#include "clang/Basic/TargetInfo.h"
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000019#include "clang/AST/ASTContext.h"
20#include "clang/AST/Decl.h"
21#include "clang/AST/DeclObjC.h"
Daniel Dunbar56273772008-09-17 00:51:38 +000022#include "llvm/ADT/StringExtras.h"
Devang Pateld0646bd2008-09-24 01:01:36 +000023#include "llvm/Attributes.h"
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +000024#include "llvm/Support/CommandLine.h"
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +000025#include "llvm/Target/TargetData.h"
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000026using namespace clang;
27using namespace CodeGen;
28
Daniel Dunbar6f3e7fa2009-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 Dunbar0dbe2272008-09-08 21:33:45 +000034/***/
35
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000036// FIXME: Use iterator and sidestep silly type array creation.
37
Daniel Dunbar45c25ba2008-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 Dunbar0dbe2272008-09-08 21:33:45 +000053CGFunctionInfo::CGFunctionInfo(const FunctionDecl *FD)
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000054{
55 const FunctionType *FTy = FD->getType()->getAsFunctionType();
56 const FunctionTypeProto *FTP = dyn_cast<FunctionTypeProto>(FTy);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000057
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000058 ArgTypes.push_back(FTy->getResultType());
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000059 if (FTP) {
60 IsVariadic = FTP->isVariadic();
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000061 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
62 ArgTypes.push_back(FTP->getArgType(i));
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000063 } else {
64 IsVariadic = true;
65 }
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000066}
67
68CGFunctionInfo::CGFunctionInfo(const ObjCMethodDecl *MD,
69 const ASTContext &Context)
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000070 : IsVariadic(MD->isVariadic())
Daniel Dunbar0dbe2272008-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 Dunbar5323a4b2008-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 Dunbar0dbe2272008-09-08 21:33:45 +000086}
87
88/***/
89
Daniel Dunbar5323a4b2008-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 Dunbar0dbe2272008-09-08 21:33:45 +000093 ArgTypes.push_back(i->second);
94}
95
Daniel Dunbar5323a4b2008-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 Dunbar0dbe2272008-09-08 21:33:45 +0000102}
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000103
104/***/
105
Daniel Dunbar8951dbd2008-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 Dunbar2c8e0f32008-09-10 02:41:04 +0000108class ABIArgInfo {
109public:
110 enum Kind {
111 Default,
Daniel Dunbar3aea8ca2008-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 Dunbar8951dbd2008-09-11 01:48:57 +0000116
Daniel Dunbar11434922009-01-26 21:26:08 +0000117 Ignore, /// Ignore the argument (treat as void). Useful for
118 /// void and empty structs.
119
Daniel Dunbar56273772008-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 Dunbar8951dbd2008-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 Dunbar56273772008-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 Dunbar8951dbd2008-09-11 01:48:57 +0000134
135 KindFirst=Default, KindLast=Expand
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000136 };
137
138private:
139 Kind TheKind;
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000140 const llvm::Type *TypeData;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000141 unsigned UIntData;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000142
Daniel Dunbar8951dbd2008-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 Dunbar2c8e0f32008-09-10 02:41:04 +0000147public:
148 static ABIArgInfo getDefault() {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000149 return ABIArgInfo(Default);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000150 }
151 static ABIArgInfo getStructRet() {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000152 return ABIArgInfo(StructRet);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000153 }
Daniel Dunbar11434922009-01-26 21:26:08 +0000154 static ABIArgInfo getIgnore() {
155 return ABIArgInfo(Ignore);
156 }
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000157 static ABIArgInfo getCoerce(const llvm::Type *T) {
158 assert(T->isSingleValueType() && "Can only coerce to simple types");
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000159 return ABIArgInfo(Coerce, T);
160 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000161 static ABIArgInfo getByVal(unsigned Alignment) {
162 return ABIArgInfo(ByVal, 0, Alignment);
163 }
Daniel Dunbar56273772008-09-17 00:51:38 +0000164 static ABIArgInfo getExpand() {
165 return ABIArgInfo(Expand);
166 }
Daniel Dunbar2c8e0f32008-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 Dunbar11434922009-01-26 21:26:08 +0000171 bool isIgnore() const { return TheKind == Ignore; }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000172 bool isCoerce() const { return TheKind == Coerce; }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000173 bool isByVal() const { return TheKind == ByVal; }
Daniel Dunbar56273772008-09-17 00:51:38 +0000174 bool isExpand() const { return TheKind == Expand; }
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000175
176 // Coerce accessors
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000177 const llvm::Type *getCoerceToType() const {
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000178 assert(TheKind == Coerce && "Invalid kind!");
179 return TypeData;
180 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000181
182 // ByVal accessors
183 unsigned getByValAlignment() const {
184 assert(TheKind == ByVal && "Invalid kind!");
185 return UIntData;
186 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000187};
188
189/***/
190
Daniel Dunbar6b1da0e2008-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 Dunbar834af452008-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 Gregorf8d49f62009-01-09 17:18:27 +0000222 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar834af452008-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 Gregorf8d49f62009-01-09 17:18:27 +0000249 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar834af452008-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 Gregorf8d49f62009-01-09 17:18:27 +0000280 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar834af452008-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 Dunbar6b1da0e2008-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 Dunbar2c8e0f32008-09-10 02:41:04 +0000324 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar834af452008-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 Dunbar639ffe42008-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 Dunbar2c8e0f32008-09-10 02:41:04 +0000362 } else {
363 return ABIArgInfo::getDefault();
364 }
365}
366
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000367ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
368 ASTContext &Context) const {
Daniel Dunbarf0357382008-09-17 20:11:04 +0000369 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar834af452008-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 Dunbar8951dbd2008-09-11 01:48:57 +0000389 return ABIArgInfo::getByVal(0);
390 } else {
391 return ABIArgInfo::getDefault();
392 }
393}
394
Daniel Dunbar6f3e7fa2009-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 Dunbar11434922009-01-26 21:26:08 +0000443 if (k == BuiltinType::Void) {
444 Lo = NoClass;
445 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
Daniel Dunbar6f3e7fa2009-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 }
453 // FIXME: _Decimal32, _Decimal64, and __m64 are SSE.
454 // FIXME: _float128, _Decimal128, and __m128 are (SSE, SSEUp).
455 // FIXME: __int128 is (Integer, Integer).
456 } else if (Ty->isPointerLikeType() || Ty->isBlockPointerType() ||
457 Ty->isObjCQualifiedInterfaceType()) {
458 Lo = Integer;
459 } else if (const ComplexType *CT = Ty->getAsComplexType()) {
460 QualType ET = CT->getElementType();
461
462 if (ET == Context.FloatTy)
463 Lo = SSE;
464 else if (ET == Context.DoubleTy)
465 Lo = Hi = SSE;
466 else if (ET == Context.LongDoubleTy)
467 Lo = ComplexX87;
468 }
469}
470
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000471ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
472 ASTContext &Context) const {
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000473 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
474 // classification algorithm.
475 X86_64ABIInfo::Class Lo, Hi;
476 classify(RetTy, Context, Lo, Hi);
477
478 const llvm::Type *ResType = 0;
479 switch (Lo) {
480 case NoClass:
Daniel Dunbar11434922009-01-26 21:26:08 +0000481 return ABIArgInfo::getIgnore();
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000482
483 case SSEUp:
484 case X87Up:
485 assert(0 && "Invalid classification for lo word.");
486
487 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
488 // hidden argument, i.e. structret.
489 case Memory:
490 return ABIArgInfo::getStructRet();
491
492 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
493 // available register of the sequence %rax, %rdx is used.
494 case Integer:
495 ResType = llvm::Type::Int64Ty; break;
496
497 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
498 // available SSE register of the sequence %xmm0, %xmm1 is used.
499 case SSE:
500 ResType = llvm::Type::DoubleTy; break;
501
502 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
503 // returned on the X87 stack in %st0 as 80-bit x87 number.
504 case X87:
505 ResType = llvm::Type::X86_FP80Ty; break;
506
507 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
508 // part of the value is returned in %st0 and the imaginary part in
509 // %st1.
510 case ComplexX87:
511 assert(Hi == NoClass && "Unexpected ComplexX87 classification.");
512 ResType = llvm::VectorType::get(llvm::Type::X86_FP80Ty, 2);
513 break;
514 }
515
516 switch (Hi) {
517 // Memory was handled previously, and ComplexX87 and X87 should
518 // never occur as hi classes.
519 case Memory:
520 case X87:
521 case ComplexX87:
522 assert(0 && "Invalid classification for hi word.");
523
524 case NoClass: break;
525 case Integer:
526 assert(0 && "FIXME: Implement MRV"); break;
527 case SSE:
528 assert(0 && "FIXME: Implement MRV"); break;
529
530 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
531 // is passed in the upper half of the last used SSE register.
532 //
533 // SSEUP should always be preceeded by SSE, just widen.
534 case SSEUp:
535 assert(Lo == SSE && "Unexpected SSEUp classification.");
536 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
537 break;
538
539 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
540 // returned together with the previos X87 value in %st0.
541 //
542 // X87UP should always be preceeded by X87, so we don't need to do
543 // anything here.
544 case X87Up:
545 assert(Lo == X87 && "Unexpected X87Up classification.");
546 break;
547 }
548
549 return ABIArgInfo::getCoerce(ResType);
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000550}
551
552ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty,
553 ASTContext &Context) const {
554 return ABIArgInfo::getDefault();
555}
556
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000557ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
558 ASTContext &Context) const {
559 return ABIArgInfo::getDefault();
560}
561
562ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
563 ASTContext &Context) const {
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000564 return ABIArgInfo::getDefault();
565}
566
567const ABIInfo &CodeGenTypes::getABIInfo() const {
568 if (TheABIInfo)
569 return *TheABIInfo;
570
571 // For now we just cache this in the CodeGenTypes and don't bother
572 // to free it.
573 const char *TargetPrefix = getContext().Target.getTargetPrefix();
574 if (strcmp(TargetPrefix, "x86") == 0) {
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000575 switch (getContext().Target.getPointerWidth(0)) {
576 case 32:
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000577 return *(TheABIInfo = new X86_32ABIInfo());
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000578 case 64:
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000579 if (UseX86_64ABI)
580 return *(TheABIInfo = new X86_64ABIInfo());
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000581 }
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000582 }
583
584 return *(TheABIInfo = new DefaultABIInfo);
585}
586
587// getABIReturnInfo - Wrap the ABIInfo getABIReturnInfo, altering
588// "default" types to StructRet when appropriate for simplicity.
589static ABIArgInfo getABIReturnInfo(QualType Ty, CodeGenTypes &CGT) {
590 assert(!Ty->isArrayType() &&
591 "Array types cannot be passed directly.");
592 ABIArgInfo Info = CGT.getABIInfo().classifyReturnType(Ty, CGT.getContext());
Daniel Dunbarf0357382008-09-17 20:11:04 +0000593 // Ensure default on aggregate types is StructRet.
594 if (Info.isDefault() && CodeGenFunction::hasAggregateLLVMType(Ty))
595 return ABIArgInfo::getStructRet();
596 return Info;
597}
598
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000599// getABIArgumentInfo - Wrap the ABIInfo getABIReturnInfo, altering
600// "default" types to ByVal when appropriate for simplicity.
601static ABIArgInfo getABIArgumentInfo(QualType Ty, CodeGenTypes &CGT) {
602 assert(!Ty->isArrayType() &&
603 "Array types cannot be passed directly.");
604 ABIArgInfo Info = CGT.getABIInfo().classifyArgumentType(Ty, CGT.getContext());
Daniel Dunbarf0357382008-09-17 20:11:04 +0000605 // Ensure default on aggregate types is ByVal.
606 if (Info.isDefault() && CodeGenFunction::hasAggregateLLVMType(Ty))
607 return ABIArgInfo::getByVal(0);
608 return Info;
609}
610
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000611/***/
612
Daniel Dunbar56273772008-09-17 00:51:38 +0000613void CodeGenTypes::GetExpandedTypes(QualType Ty,
614 std::vector<const llvm::Type*> &ArgTys) {
615 const RecordType *RT = Ty->getAsStructureType();
616 assert(RT && "Can only expand structure types.");
617 const RecordDecl *RD = RT->getDecl();
618 assert(!RD->hasFlexibleArrayMember() &&
619 "Cannot expand structure with flexible array.");
620
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000621 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar56273772008-09-17 00:51:38 +0000622 e = RD->field_end(); i != e; ++i) {
623 const FieldDecl *FD = *i;
624 assert(!FD->isBitField() &&
625 "Cannot expand structure with bit-field members.");
626
627 QualType FT = FD->getType();
628 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
629 GetExpandedTypes(FT, ArgTys);
630 } else {
631 ArgTys.push_back(ConvertType(FT));
632 }
633 }
634}
635
636llvm::Function::arg_iterator
637CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
638 llvm::Function::arg_iterator AI) {
639 const RecordType *RT = Ty->getAsStructureType();
640 assert(RT && "Can only expand structure types.");
641
642 RecordDecl *RD = RT->getDecl();
643 assert(LV.isSimple() &&
644 "Unexpected non-simple lvalue during struct expansion.");
645 llvm::Value *Addr = LV.getAddress();
646 for (RecordDecl::field_iterator i = RD->field_begin(),
647 e = RD->field_end(); i != e; ++i) {
648 FieldDecl *FD = *i;
649 QualType FT = FD->getType();
650
651 // FIXME: What are the right qualifiers here?
652 LValue LV = EmitLValueForField(Addr, FD, false, 0);
653 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
654 AI = ExpandTypeFromArgs(FT, LV, AI);
655 } else {
656 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
657 ++AI;
658 }
659 }
660
661 return AI;
662}
663
664void
665CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
666 llvm::SmallVector<llvm::Value*, 16> &Args) {
667 const RecordType *RT = Ty->getAsStructureType();
668 assert(RT && "Can only expand structure types.");
669
670 RecordDecl *RD = RT->getDecl();
671 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
672 llvm::Value *Addr = RV.getAggregateAddr();
673 for (RecordDecl::field_iterator i = RD->field_begin(),
674 e = RD->field_end(); i != e; ++i) {
675 FieldDecl *FD = *i;
676 QualType FT = FD->getType();
677
678 // FIXME: What are the right qualifiers here?
679 LValue LV = EmitLValueForField(Addr, FD, false, 0);
680 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
681 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
682 } else {
683 RValue RV = EmitLoadOfLValue(LV, FT);
684 assert(RV.isScalar() &&
685 "Unexpected non-scalar rvalue during struct expansion.");
686 Args.push_back(RV.getScalarVal());
687 }
688 }
689}
690
691/***/
692
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000693const llvm::FunctionType *
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000694CodeGenTypes::GetFunctionType(const CGCallInfo &CI, bool IsVariadic) {
695 return GetFunctionType(CI.argtypes_begin(), CI.argtypes_end(), IsVariadic);
696}
697
698const llvm::FunctionType *
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000699CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI) {
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000700 return GetFunctionType(FI.argtypes_begin(), FI.argtypes_end(), FI.isVariadic());
701}
702
703const llvm::FunctionType *
704CodeGenTypes::GetFunctionType(ArgTypeIterator begin, ArgTypeIterator end,
705 bool IsVariadic) {
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000706 std::vector<const llvm::Type*> ArgTys;
707
708 const llvm::Type *ResultType = 0;
709
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000710 QualType RetTy = *begin;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000711 ABIArgInfo RetAI = getABIReturnInfo(RetTy, *this);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000712 switch (RetAI.getKind()) {
713 case ABIArgInfo::ByVal:
714 case ABIArgInfo::Expand:
715 assert(0 && "Invalid ABI kind for return argument");
716
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000717 case ABIArgInfo::Default:
718 if (RetTy->isVoidType()) {
719 ResultType = llvm::Type::VoidTy;
720 } else {
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000721 ResultType = ConvertType(RetTy);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000722 }
723 break;
724
725 case ABIArgInfo::StructRet: {
726 ResultType = llvm::Type::VoidTy;
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000727 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000728 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
729 break;
730 }
731
Daniel Dunbar11434922009-01-26 21:26:08 +0000732 case ABIArgInfo::Ignore:
733 ResultType = llvm::Type::VoidTy;
734 break;
735
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000736 case ABIArgInfo::Coerce:
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000737 ResultType = RetAI.getCoerceToType();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000738 break;
739 }
740
741 for (++begin; begin != end; ++begin) {
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000742 ABIArgInfo AI = getABIArgumentInfo(*begin, *this);
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000743 const llvm::Type *Ty = ConvertType(*begin);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000744
745 switch (AI.getKind()) {
Daniel Dunbar11434922009-01-26 21:26:08 +0000746 case ABIArgInfo::Ignore:
747 break;
748
Daniel Dunbar56273772008-09-17 00:51:38 +0000749 case ABIArgInfo::Coerce:
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000750 case ABIArgInfo::StructRet:
751 assert(0 && "Invalid ABI kind for non-return argument");
752
753 case ABIArgInfo::ByVal:
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000754 // byval arguments are always on the stack, which is addr space #0.
755 ArgTys.push_back(llvm::PointerType::getUnqual(Ty));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000756 assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled");
757 break;
758
759 case ABIArgInfo::Default:
760 ArgTys.push_back(Ty);
761 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000762
763 case ABIArgInfo::Expand:
Daniel Dunbar56273772008-09-17 00:51:38 +0000764 GetExpandedTypes(*begin, ArgTys);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000765 break;
766 }
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000767 }
768
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000769 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000770}
771
Daniel Dunbarb7688072008-09-10 00:41:16 +0000772bool CodeGenModule::ReturnTypeUsesSret(QualType RetTy) {
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000773 return getABIReturnInfo(RetTy, getTypes()).isStructRet();
Daniel Dunbar3913f182008-09-09 23:48:28 +0000774}
775
Devang Patel761d7f72008-09-25 21:02:23 +0000776void CodeGenModule::ConstructAttributeList(const Decl *TargetDecl,
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000777 ArgTypeIterator begin,
778 ArgTypeIterator end,
Devang Patel761d7f72008-09-25 21:02:23 +0000779 AttributeListType &PAL) {
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000780 unsigned FuncAttrs = 0;
Devang Patela2c69122008-09-26 22:53:57 +0000781 unsigned RetAttrs = 0;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000782
783 if (TargetDecl) {
784 if (TargetDecl->getAttr<NoThrowAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +0000785 FuncAttrs |= llvm::Attribute::NoUnwind;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000786 if (TargetDecl->getAttr<NoReturnAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +0000787 FuncAttrs |= llvm::Attribute::NoReturn;
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000788 if (TargetDecl->getAttr<PureAttr>())
789 FuncAttrs |= llvm::Attribute::ReadOnly;
790 if (TargetDecl->getAttr<ConstAttr>())
791 FuncAttrs |= llvm::Attribute::ReadNone;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000792 }
793
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000794 QualType RetTy = *begin;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000795 unsigned Index = 1;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000796 ABIArgInfo RetAI = getABIReturnInfo(RetTy, getTypes());
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000797 switch (RetAI.getKind()) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000798 case ABIArgInfo::Default:
799 if (RetTy->isPromotableIntegerType()) {
800 if (RetTy->isSignedIntegerType()) {
Devang Patela2c69122008-09-26 22:53:57 +0000801 RetAttrs |= llvm::Attribute::SExt;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000802 } else if (RetTy->isUnsignedIntegerType()) {
Devang Patela2c69122008-09-26 22:53:57 +0000803 RetAttrs |= llvm::Attribute::ZExt;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000804 }
805 }
806 break;
807
808 case ABIArgInfo::StructRet:
Devang Patel761d7f72008-09-25 21:02:23 +0000809 PAL.push_back(llvm::AttributeWithIndex::get(Index,
810 llvm::Attribute::StructRet|
811 llvm::Attribute::NoAlias));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000812 ++Index;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000813 break;
814
Daniel Dunbar11434922009-01-26 21:26:08 +0000815 case ABIArgInfo::Ignore:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000816 case ABIArgInfo::Coerce:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000817 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000818
819 case ABIArgInfo::ByVal:
820 case ABIArgInfo::Expand:
821 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000822 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000823
Devang Patela2c69122008-09-26 22:53:57 +0000824 if (RetAttrs)
825 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Daniel Dunbar56273772008-09-17 00:51:38 +0000826 for (++begin; begin != end; ++begin) {
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000827 QualType ParamType = *begin;
Devang Patel761d7f72008-09-25 21:02:23 +0000828 unsigned Attributes = 0;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000829 ABIArgInfo AI = getABIArgumentInfo(ParamType, getTypes());
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000830
831 switch (AI.getKind()) {
832 case ABIArgInfo::StructRet:
Daniel Dunbar56273772008-09-17 00:51:38 +0000833 case ABIArgInfo::Coerce:
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000834 assert(0 && "Invalid ABI kind for non-return argument");
835
836 case ABIArgInfo::ByVal:
Devang Patel761d7f72008-09-25 21:02:23 +0000837 Attributes |= llvm::Attribute::ByVal;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000838 assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled");
839 break;
840
841 case ABIArgInfo::Default:
842 if (ParamType->isPromotableIntegerType()) {
843 if (ParamType->isSignedIntegerType()) {
Devang Patel761d7f72008-09-25 21:02:23 +0000844 Attributes |= llvm::Attribute::SExt;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000845 } else if (ParamType->isUnsignedIntegerType()) {
Devang Patel761d7f72008-09-25 21:02:23 +0000846 Attributes |= llvm::Attribute::ZExt;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000847 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000848 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000849 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000850
Daniel Dunbar11434922009-01-26 21:26:08 +0000851 case ABIArgInfo::Ignore:
852 // Skip increment, no matching LLVM parameter.
853 continue;
854
Daniel Dunbar56273772008-09-17 00:51:38 +0000855 case ABIArgInfo::Expand: {
856 std::vector<const llvm::Type*> Tys;
857 // FIXME: This is rather inefficient. Do we ever actually need
858 // to do anything here? The result should be just reconstructed
859 // on the other side, so extension should be a non-issue.
860 getTypes().GetExpandedTypes(ParamType, Tys);
861 Index += Tys.size();
862 continue;
863 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000864 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000865
Devang Patel761d7f72008-09-25 21:02:23 +0000866 if (Attributes)
867 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar56273772008-09-17 00:51:38 +0000868 ++Index;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000869 }
Devang Patela2c69122008-09-26 22:53:57 +0000870 if (FuncAttrs)
871 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
872
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000873}
874
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000875void CodeGenFunction::EmitFunctionProlog(llvm::Function *Fn,
876 QualType RetTy,
877 const FunctionArgList &Args) {
878 // Emit allocs for param decls. Give the LLVM Argument nodes names.
879 llvm::Function::arg_iterator AI = Fn->arg_begin();
880
881 // Name the struct return argument.
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000882 if (CGM.ReturnTypeUsesSret(RetTy)) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000883 AI->setName("agg.result");
884 ++AI;
885 }
886
887 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbar56273772008-09-17 00:51:38 +0000888 i != e; ++i) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000889 const VarDecl *Arg = i->first;
Daniel Dunbar56273772008-09-17 00:51:38 +0000890 QualType Ty = i->second;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000891 ABIArgInfo ArgI = getABIArgumentInfo(Ty, CGM.getTypes());
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000892
893 switch (ArgI.getKind()) {
894 case ABIArgInfo::ByVal:
895 case ABIArgInfo::Default: {
896 assert(AI != Fn->arg_end() && "Argument mismatch!");
897 llvm::Value* V = AI;
Daniel Dunbar56273772008-09-17 00:51:38 +0000898 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000899 // This must be a promotion, for something like
900 // "void a(x) short x; {..."
Daniel Dunbar56273772008-09-17 00:51:38 +0000901 V = EmitScalarConversion(V, Ty, Arg->getType());
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000902 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000903 EmitParmDecl(*Arg, V);
904 break;
905 }
Daniel Dunbar56273772008-09-17 00:51:38 +0000906
907 case ABIArgInfo::Expand: {
908 // If this was structure was expand into multiple arguments then
909 // we need to create a temporary and reconstruct it from the
910 // arguments.
Chris Lattner39f34e92008-11-24 04:00:27 +0000911 std::string Name = Arg->getNameAsString();
Daniel Dunbar56273772008-09-17 00:51:38 +0000912 llvm::Value *Temp = CreateTempAlloca(ConvertType(Ty),
913 (Name + ".addr").c_str());
914 // FIXME: What are the right qualifiers here?
915 llvm::Function::arg_iterator End =
916 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI);
917 EmitParmDecl(*Arg, Temp);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000918
Daniel Dunbar56273772008-09-17 00:51:38 +0000919 // Name the arguments used in expansion and increment AI.
920 unsigned Index = 0;
921 for (; AI != End; ++AI, ++Index)
922 AI->setName(Name + "." + llvm::utostr(Index));
923 continue;
924 }
Daniel Dunbar11434922009-01-26 21:26:08 +0000925
926 case ABIArgInfo::Ignore:
927 break;
928
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000929 case ABIArgInfo::Coerce:
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000930 case ABIArgInfo::StructRet:
931 assert(0 && "Invalid ABI kind for non-return argument");
932 }
Daniel Dunbar56273772008-09-17 00:51:38 +0000933
934 ++AI;
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000935 }
936 assert(AI == Fn->arg_end() && "Argument mismatch!");
937}
938
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +0000939/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
940/// a pointer to an object of type \arg Ty.
941///
942/// This safely handles the case when the src type is smaller than the
943/// destination type; in this situation the values of bits which not
944/// present in the src are undefined.
945static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
946 const llvm::Type *Ty,
947 CodeGenFunction &CGF) {
948 const llvm::Type *SrcTy =
949 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
950 unsigned SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
951 unsigned DstSize = CGF.CGM.getTargetData().getTypePaddedSize(Ty);
952
953 // If load is legal, just bitcase the src pointer.
954 if (SrcSize == DstSize) {
955 llvm::Value *Casted =
956 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
957 return CGF.Builder.CreateLoad(Casted);
958 } else {
959 assert(SrcSize < DstSize && "Coercion is losing source bits!");
960
961 // Otherwise do coercion through memory. This is stupid, but
962 // simple.
963 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
964 llvm::Value *Casted =
965 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
966 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
967 return CGF.Builder.CreateLoad(Tmp);
968 }
969}
970
971/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
972/// where the source and destination may have different types.
973///
974/// This safely handles the case when the src type is larger than the
975/// destination type; the upper bits of the src will be lost.
976static void CreateCoercedStore(llvm::Value *Src,
977 llvm::Value *DstPtr,
978 CodeGenFunction &CGF) {
979 const llvm::Type *SrcTy = Src->getType();
980 const llvm::Type *DstTy =
981 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
982
983 unsigned SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
984 unsigned DstSize = CGF.CGM.getTargetData().getTypePaddedSize(DstTy);
985
986 // If store is legal, just bitcase the src pointer.
987 if (SrcSize == DstSize) {
988 llvm::Value *Casted =
989 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
990 CGF.Builder.CreateStore(Src, Casted);
991 } else {
992 assert(SrcSize > DstSize && "Coercion is missing bits!");
993
994 // Otherwise do coercion through memory. This is stupid, but
995 // simple.
996 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
997 CGF.Builder.CreateStore(Src, Tmp);
998 llvm::Value *Casted =
999 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
1000 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(Casted), DstPtr);
1001 }
1002}
1003
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001004void CodeGenFunction::EmitFunctionEpilog(QualType RetTy,
1005 llvm::Value *ReturnValue) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001006 llvm::Value *RV = 0;
1007
1008 // Functions with no result always return void.
1009 if (ReturnValue) {
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001010 ABIArgInfo RetAI = getABIReturnInfo(RetTy, CGM.getTypes());
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001011
1012 switch (RetAI.getKind()) {
1013 case ABIArgInfo::StructRet:
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001014 if (RetTy->isAnyComplexType()) {
1015 // FIXME: Volatile
1016 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
1017 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
1018 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1019 EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy);
1020 } else {
1021 Builder.CreateStore(Builder.CreateLoad(ReturnValue),
1022 CurFn->arg_begin());
1023 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001024 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001025
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001026 case ABIArgInfo::Default:
1027 RV = Builder.CreateLoad(ReturnValue);
1028 break;
1029
Daniel Dunbar11434922009-01-26 21:26:08 +00001030 case ABIArgInfo::Ignore:
1031 break;
1032
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001033 case ABIArgInfo::Coerce: {
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +00001034 RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001035 break;
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001036 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001037
1038 case ABIArgInfo::ByVal:
1039 case ABIArgInfo::Expand:
1040 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001041 }
1042 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001043
1044 if (RV) {
1045 Builder.CreateRet(RV);
1046 } else {
1047 Builder.CreateRetVoid();
1048 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001049}
1050
1051RValue CodeGenFunction::EmitCall(llvm::Value *Callee,
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001052 QualType RetTy,
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001053 const CallArgList &CallArgs) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001054 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001055
1056 // Handle struct-return functions by passing a pointer to the
1057 // location that we would like to return into.
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001058 ABIArgInfo RetAI = getABIReturnInfo(RetTy, CGM.getTypes());
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001059 switch (RetAI.getKind()) {
1060 case ABIArgInfo::StructRet:
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001061 // Create a temporary alloca to hold the result of the call. :(
Daniel Dunbar56273772008-09-17 00:51:38 +00001062 Args.push_back(CreateTempAlloca(ConvertType(RetTy)));
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001063 break;
1064
1065 case ABIArgInfo::Default:
Daniel Dunbar11434922009-01-26 21:26:08 +00001066 case ABIArgInfo::Ignore:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001067 case ABIArgInfo::Coerce:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001068 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001069
1070 case ABIArgInfo::ByVal:
1071 case ABIArgInfo::Expand:
1072 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001073 }
1074
1075 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
1076 I != E; ++I) {
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001077 ABIArgInfo ArgInfo = getABIArgumentInfo(I->second, CGM.getTypes());
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001078 RValue RV = I->first;
Daniel Dunbar56273772008-09-17 00:51:38 +00001079
1080 switch (ArgInfo.getKind()) {
1081 case ABIArgInfo::ByVal: // Default is byval
1082 case ABIArgInfo::Default:
1083 if (RV.isScalar()) {
1084 Args.push_back(RV.getScalarVal());
1085 } else if (RV.isComplex()) {
1086 // Make a temporary alloca to pass the argument.
1087 Args.push_back(CreateTempAlloca(ConvertType(I->second)));
1088 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
1089 } else {
1090 Args.push_back(RV.getAggregateAddr());
1091 }
1092 break;
1093
Daniel Dunbar11434922009-01-26 21:26:08 +00001094 case ABIArgInfo::Ignore:
1095 break;
1096
Daniel Dunbar56273772008-09-17 00:51:38 +00001097 case ABIArgInfo::StructRet:
1098 case ABIArgInfo::Coerce:
1099 assert(0 && "Invalid ABI kind for non-return argument");
1100 break;
1101
1102 case ABIArgInfo::Expand:
1103 ExpandTypeToArgs(I->second, RV, Args);
1104 break;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001105 }
1106 }
1107
1108 llvm::CallInst *CI = Builder.CreateCall(Callee,&Args[0],&Args[0]+Args.size());
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001109 CGCallInfo CallInfo(RetTy, CallArgs);
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001110
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001111 // FIXME: Provide TargetDecl so nounwind, noreturn, etc, etc get set.
Devang Patel761d7f72008-09-25 21:02:23 +00001112 CodeGen::AttributeListType AttributeList;
1113 CGM.ConstructAttributeList(0,
Daniel Dunbarb7688072008-09-10 00:41:16 +00001114 CallInfo.argtypes_begin(), CallInfo.argtypes_end(),
Devang Patel761d7f72008-09-25 21:02:23 +00001115 AttributeList);
1116 CI->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(),
1117 AttributeList.size()));
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001118
1119 if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee))
1120 CI->setCallingConv(F->getCallingConv());
1121 if (CI->getType() != llvm::Type::VoidTy)
1122 CI->setName("call");
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001123
1124 switch (RetAI.getKind()) {
1125 case ABIArgInfo::StructRet:
1126 if (RetTy->isAnyComplexType())
Daniel Dunbar56273772008-09-17 00:51:38 +00001127 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001128 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar56273772008-09-17 00:51:38 +00001129 return RValue::getAggregate(Args[0]);
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001130 else
1131 return RValue::get(Builder.CreateLoad(Args[0]));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001132
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001133 case ABIArgInfo::Default:
1134 return RValue::get(RetTy->isVoidType() ? 0 : CI);
1135
Daniel Dunbar11434922009-01-26 21:26:08 +00001136 case ABIArgInfo::Ignore:
1137 return RValue::get(0);
1138
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001139 case ABIArgInfo::Coerce: {
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +00001140 llvm::Value *V = CreateTempAlloca(ConvertType(RetTy), "coerce");
1141 CreateCoercedStore(CI, V, *this);
Anders Carlssonad3d6912008-11-25 22:21:48 +00001142 if (RetTy->isAnyComplexType())
1143 return RValue::getComplex(LoadComplexFromAddr(V, false));
Daniel Dunbar11434922009-01-26 21:26:08 +00001144 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssonad3d6912008-11-25 22:21:48 +00001145 return RValue::getAggregate(V);
Daniel Dunbar11434922009-01-26 21:26:08 +00001146 else
1147 return RValue::get(Builder.CreateLoad(V));
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001148 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001149
1150 case ABIArgInfo::ByVal:
1151 case ABIArgInfo::Expand:
1152 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001153 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001154
1155 assert(0 && "Unhandled ABIArgInfo::Kind");
1156 return RValue::get(0);
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001157}