blob: 6d21622b166fe6921ccce470d4590bd034d7fff0 [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
Daniel Dunbar28770fc2009-01-29 07:22:20 +0000474 if (ET->isIntegerType()) {
475 unsigned Size = Context.getTypeSize(Ty);
476 if (Size <= 64)
477 Lo = Integer;
478 else if (Size <= 128)
479 Lo = Hi = Integer;
480 } else if (ET == Context.FloatTy)
Daniel Dunbare09a9692009-01-24 08:32:22 +0000481 Lo = SSE;
482 else if (ET == Context.DoubleTy)
483 Lo = Hi = SSE;
484 else if (ET == Context.LongDoubleTy)
485 Lo = ComplexX87;
486 }
487}
488
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000489ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
490 ASTContext &Context) const {
Daniel Dunbare09a9692009-01-24 08:32:22 +0000491 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
492 // classification algorithm.
493 X86_64ABIInfo::Class Lo, Hi;
494 classify(RetTy, Context, Lo, Hi);
495
496 const llvm::Type *ResType = 0;
497 switch (Lo) {
498 case NoClass:
Daniel Dunbar1358b202009-01-26 21:26:08 +0000499 return ABIArgInfo::getIgnore();
Daniel Dunbare09a9692009-01-24 08:32:22 +0000500
501 case SSEUp:
502 case X87Up:
503 assert(0 && "Invalid classification for lo word.");
504
505 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
506 // hidden argument, i.e. structret.
507 case Memory:
508 return ABIArgInfo::getStructRet();
509
510 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
511 // available register of the sequence %rax, %rdx is used.
512 case Integer:
513 ResType = llvm::Type::Int64Ty; break;
514
515 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
516 // available SSE register of the sequence %xmm0, %xmm1 is used.
517 case SSE:
518 ResType = llvm::Type::DoubleTy; break;
519
520 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
521 // returned on the X87 stack in %st0 as 80-bit x87 number.
522 case X87:
523 ResType = llvm::Type::X86_FP80Ty; break;
524
525 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
526 // part of the value is returned in %st0 and the imaginary part in
527 // %st1.
528 case ComplexX87:
529 assert(Hi == NoClass && "Unexpected ComplexX87 classification.");
530 ResType = llvm::VectorType::get(llvm::Type::X86_FP80Ty, 2);
531 break;
532 }
533
534 switch (Hi) {
535 // Memory was handled previously, and ComplexX87 and X87 should
536 // never occur as hi classes.
537 case Memory:
538 case X87:
539 case ComplexX87:
540 assert(0 && "Invalid classification for hi word.");
541
542 case NoClass: break;
543 case Integer:
544 assert(0 && "FIXME: Implement MRV"); break;
545 case SSE:
546 assert(0 && "FIXME: Implement MRV"); break;
547
548 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
549 // is passed in the upper half of the last used SSE register.
550 //
551 // SSEUP should always be preceeded by SSE, just widen.
552 case SSEUp:
553 assert(Lo == SSE && "Unexpected SSEUp classification.");
554 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
555 break;
556
557 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
558 // returned together with the previos X87 value in %st0.
559 //
560 // X87UP should always be preceeded by X87, so we don't need to do
561 // anything here.
562 case X87Up:
563 assert(Lo == X87 && "Unexpected X87Up classification.");
564 break;
565 }
566
567 return ABIArgInfo::getCoerce(ResType);
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000568}
569
570ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty,
571 ASTContext &Context) const {
572 return ABIArgInfo::getDefault();
573}
574
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000575ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
576 ASTContext &Context) const {
577 return ABIArgInfo::getDefault();
578}
579
580ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
581 ASTContext &Context) const {
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000582 return ABIArgInfo::getDefault();
583}
584
585const ABIInfo &CodeGenTypes::getABIInfo() const {
586 if (TheABIInfo)
587 return *TheABIInfo;
588
589 // For now we just cache this in the CodeGenTypes and don't bother
590 // to free it.
591 const char *TargetPrefix = getContext().Target.getTargetPrefix();
592 if (strcmp(TargetPrefix, "x86") == 0) {
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000593 switch (getContext().Target.getPointerWidth(0)) {
594 case 32:
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000595 return *(TheABIInfo = new X86_32ABIInfo());
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000596 case 64:
Daniel Dunbare09a9692009-01-24 08:32:22 +0000597 if (UseX86_64ABI)
598 return *(TheABIInfo = new X86_64ABIInfo());
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000599 }
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000600 }
601
602 return *(TheABIInfo = new DefaultABIInfo);
603}
604
605// getABIReturnInfo - Wrap the ABIInfo getABIReturnInfo, altering
606// "default" types to StructRet when appropriate for simplicity.
607static ABIArgInfo getABIReturnInfo(QualType Ty, CodeGenTypes &CGT) {
608 assert(!Ty->isArrayType() &&
609 "Array types cannot be passed directly.");
610 ABIArgInfo Info = CGT.getABIInfo().classifyReturnType(Ty, CGT.getContext());
Daniel Dunbar3158c592008-09-17 20:11:04 +0000611 // Ensure default on aggregate types is StructRet.
612 if (Info.isDefault() && CodeGenFunction::hasAggregateLLVMType(Ty))
613 return ABIArgInfo::getStructRet();
614 return Info;
615}
616
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000617// getABIArgumentInfo - Wrap the ABIInfo getABIReturnInfo, altering
618// "default" types to ByVal when appropriate for simplicity.
619static ABIArgInfo getABIArgumentInfo(QualType Ty, CodeGenTypes &CGT) {
620 assert(!Ty->isArrayType() &&
621 "Array types cannot be passed directly.");
622 ABIArgInfo Info = CGT.getABIInfo().classifyArgumentType(Ty, CGT.getContext());
Daniel Dunbar3158c592008-09-17 20:11:04 +0000623 // Ensure default on aggregate types is ByVal.
624 if (Info.isDefault() && CodeGenFunction::hasAggregateLLVMType(Ty))
625 return ABIArgInfo::getByVal(0);
626 return Info;
627}
628
Daniel Dunbare126ab12008-09-10 02:41:04 +0000629/***/
630
Daniel Dunbar04d35782008-09-17 00:51:38 +0000631void CodeGenTypes::GetExpandedTypes(QualType Ty,
632 std::vector<const llvm::Type*> &ArgTys) {
633 const RecordType *RT = Ty->getAsStructureType();
634 assert(RT && "Can only expand structure types.");
635 const RecordDecl *RD = RT->getDecl();
636 assert(!RD->hasFlexibleArrayMember() &&
637 "Cannot expand structure with flexible array.");
638
Douglas Gregor5d764842009-01-09 17:18:27 +0000639 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar04d35782008-09-17 00:51:38 +0000640 e = RD->field_end(); i != e; ++i) {
641 const FieldDecl *FD = *i;
642 assert(!FD->isBitField() &&
643 "Cannot expand structure with bit-field members.");
644
645 QualType FT = FD->getType();
646 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
647 GetExpandedTypes(FT, ArgTys);
648 } else {
649 ArgTys.push_back(ConvertType(FT));
650 }
651 }
652}
653
654llvm::Function::arg_iterator
655CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
656 llvm::Function::arg_iterator AI) {
657 const RecordType *RT = Ty->getAsStructureType();
658 assert(RT && "Can only expand structure types.");
659
660 RecordDecl *RD = RT->getDecl();
661 assert(LV.isSimple() &&
662 "Unexpected non-simple lvalue during struct expansion.");
663 llvm::Value *Addr = LV.getAddress();
664 for (RecordDecl::field_iterator i = RD->field_begin(),
665 e = RD->field_end(); i != e; ++i) {
666 FieldDecl *FD = *i;
667 QualType FT = FD->getType();
668
669 // FIXME: What are the right qualifiers here?
670 LValue LV = EmitLValueForField(Addr, FD, false, 0);
671 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
672 AI = ExpandTypeFromArgs(FT, LV, AI);
673 } else {
674 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
675 ++AI;
676 }
677 }
678
679 return AI;
680}
681
682void
683CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
684 llvm::SmallVector<llvm::Value*, 16> &Args) {
685 const RecordType *RT = Ty->getAsStructureType();
686 assert(RT && "Can only expand structure types.");
687
688 RecordDecl *RD = RT->getDecl();
689 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
690 llvm::Value *Addr = RV.getAggregateAddr();
691 for (RecordDecl::field_iterator i = RD->field_begin(),
692 e = RD->field_end(); i != e; ++i) {
693 FieldDecl *FD = *i;
694 QualType FT = FD->getType();
695
696 // FIXME: What are the right qualifiers here?
697 LValue LV = EmitLValueForField(Addr, FD, false, 0);
698 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
699 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
700 } else {
701 RValue RV = EmitLoadOfLValue(LV, FT);
702 assert(RV.isScalar() &&
703 "Unexpected non-scalar rvalue during struct expansion.");
704 Args.push_back(RV.getScalarVal());
705 }
706 }
707}
708
709/***/
710
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000711const llvm::FunctionType *
Daniel Dunbara9976a22008-09-10 07:00:50 +0000712CodeGenTypes::GetFunctionType(const CGCallInfo &CI, bool IsVariadic) {
713 return GetFunctionType(CI.argtypes_begin(), CI.argtypes_end(), IsVariadic);
714}
715
716const llvm::FunctionType *
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000717CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI) {
Daniel Dunbara9976a22008-09-10 07:00:50 +0000718 return GetFunctionType(FI.argtypes_begin(), FI.argtypes_end(), FI.isVariadic());
719}
720
721const llvm::FunctionType *
722CodeGenTypes::GetFunctionType(ArgTypeIterator begin, ArgTypeIterator end,
723 bool IsVariadic) {
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000724 std::vector<const llvm::Type*> ArgTys;
725
726 const llvm::Type *ResultType = 0;
727
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000728 QualType RetTy = *begin;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000729 ABIArgInfo RetAI = getABIReturnInfo(RetTy, *this);
Daniel Dunbar22e30052008-09-11 01:48:57 +0000730 switch (RetAI.getKind()) {
731 case ABIArgInfo::ByVal:
732 case ABIArgInfo::Expand:
733 assert(0 && "Invalid ABI kind for return argument");
734
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000735 case ABIArgInfo::Default:
736 if (RetTy->isVoidType()) {
737 ResultType = llvm::Type::VoidTy;
738 } else {
Daniel Dunbara9976a22008-09-10 07:00:50 +0000739 ResultType = ConvertType(RetTy);
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000740 }
741 break;
742
743 case ABIArgInfo::StructRet: {
744 ResultType = llvm::Type::VoidTy;
Daniel Dunbara9976a22008-09-10 07:00:50 +0000745 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000746 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
747 break;
748 }
749
Daniel Dunbar1358b202009-01-26 21:26:08 +0000750 case ABIArgInfo::Ignore:
751 ResultType = llvm::Type::VoidTy;
752 break;
753
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000754 case ABIArgInfo::Coerce:
Daniel Dunbar73d66602008-09-10 07:04:09 +0000755 ResultType = RetAI.getCoerceToType();
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000756 break;
757 }
758
759 for (++begin; begin != end; ++begin) {
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000760 ABIArgInfo AI = getABIArgumentInfo(*begin, *this);
Daniel Dunbara9976a22008-09-10 07:00:50 +0000761 const llvm::Type *Ty = ConvertType(*begin);
Daniel Dunbar22e30052008-09-11 01:48:57 +0000762
763 switch (AI.getKind()) {
Daniel Dunbar1358b202009-01-26 21:26:08 +0000764 case ABIArgInfo::Ignore:
765 break;
766
Daniel Dunbar04d35782008-09-17 00:51:38 +0000767 case ABIArgInfo::Coerce:
Daniel Dunbar22e30052008-09-11 01:48:57 +0000768 case ABIArgInfo::StructRet:
769 assert(0 && "Invalid ABI kind for non-return argument");
770
771 case ABIArgInfo::ByVal:
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000772 // byval arguments are always on the stack, which is addr space #0.
773 ArgTys.push_back(llvm::PointerType::getUnqual(Ty));
Daniel Dunbar22e30052008-09-11 01:48:57 +0000774 assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled");
775 break;
776
777 case ABIArgInfo::Default:
778 ArgTys.push_back(Ty);
779 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +0000780
781 case ABIArgInfo::Expand:
Daniel Dunbar04d35782008-09-17 00:51:38 +0000782 GetExpandedTypes(*begin, ArgTys);
Daniel Dunbar22e30052008-09-11 01:48:57 +0000783 break;
784 }
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000785 }
786
Daniel Dunbara9976a22008-09-10 07:00:50 +0000787 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000788}
789
Daniel Dunbar3ef2e852008-09-10 00:41:16 +0000790bool CodeGenModule::ReturnTypeUsesSret(QualType RetTy) {
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000791 return getABIReturnInfo(RetTy, getTypes()).isStructRet();
Daniel Dunbar49f5a0d2008-09-09 23:48:28 +0000792}
793
Devang Patela85a9ef2008-09-25 21:02:23 +0000794void CodeGenModule::ConstructAttributeList(const Decl *TargetDecl,
Daniel Dunbare126ab12008-09-10 02:41:04 +0000795 ArgTypeIterator begin,
796 ArgTypeIterator end,
Devang Patela85a9ef2008-09-25 21:02:23 +0000797 AttributeListType &PAL) {
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000798 unsigned FuncAttrs = 0;
Devang Patel2bb6eb82008-09-26 22:53:57 +0000799 unsigned RetAttrs = 0;
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000800
801 if (TargetDecl) {
802 if (TargetDecl->getAttr<NoThrowAttr>())
Devang Patela85a9ef2008-09-25 21:02:23 +0000803 FuncAttrs |= llvm::Attribute::NoUnwind;
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000804 if (TargetDecl->getAttr<NoReturnAttr>())
Devang Patela85a9ef2008-09-25 21:02:23 +0000805 FuncAttrs |= llvm::Attribute::NoReturn;
Anders Carlssondd6791c2008-10-05 23:32:53 +0000806 if (TargetDecl->getAttr<PureAttr>())
807 FuncAttrs |= llvm::Attribute::ReadOnly;
808 if (TargetDecl->getAttr<ConstAttr>())
809 FuncAttrs |= llvm::Attribute::ReadNone;
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000810 }
811
Daniel Dunbare126ab12008-09-10 02:41:04 +0000812 QualType RetTy = *begin;
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000813 unsigned Index = 1;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000814 ABIArgInfo RetAI = getABIReturnInfo(RetTy, getTypes());
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000815 switch (RetAI.getKind()) {
Daniel Dunbare126ab12008-09-10 02:41:04 +0000816 case ABIArgInfo::Default:
817 if (RetTy->isPromotableIntegerType()) {
818 if (RetTy->isSignedIntegerType()) {
Devang Patel2bb6eb82008-09-26 22:53:57 +0000819 RetAttrs |= llvm::Attribute::SExt;
Daniel Dunbare126ab12008-09-10 02:41:04 +0000820 } else if (RetTy->isUnsignedIntegerType()) {
Devang Patel2bb6eb82008-09-26 22:53:57 +0000821 RetAttrs |= llvm::Attribute::ZExt;
Daniel Dunbare126ab12008-09-10 02:41:04 +0000822 }
823 }
824 break;
825
826 case ABIArgInfo::StructRet:
Devang Patela85a9ef2008-09-25 21:02:23 +0000827 PAL.push_back(llvm::AttributeWithIndex::get(Index,
828 llvm::Attribute::StructRet|
829 llvm::Attribute::NoAlias));
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000830 ++Index;
Daniel Dunbare126ab12008-09-10 02:41:04 +0000831 break;
832
Daniel Dunbar1358b202009-01-26 21:26:08 +0000833 case ABIArgInfo::Ignore:
Daniel Dunbare126ab12008-09-10 02:41:04 +0000834 case ABIArgInfo::Coerce:
Daniel Dunbare126ab12008-09-10 02:41:04 +0000835 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +0000836
837 case ABIArgInfo::ByVal:
838 case ABIArgInfo::Expand:
839 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000840 }
Daniel Dunbare126ab12008-09-10 02:41:04 +0000841
Devang Patel2bb6eb82008-09-26 22:53:57 +0000842 if (RetAttrs)
843 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Daniel Dunbar04d35782008-09-17 00:51:38 +0000844 for (++begin; begin != end; ++begin) {
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000845 QualType ParamType = *begin;
Devang Patela85a9ef2008-09-25 21:02:23 +0000846 unsigned Attributes = 0;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000847 ABIArgInfo AI = getABIArgumentInfo(ParamType, getTypes());
Daniel Dunbar22e30052008-09-11 01:48:57 +0000848
849 switch (AI.getKind()) {
850 case ABIArgInfo::StructRet:
Daniel Dunbar04d35782008-09-17 00:51:38 +0000851 case ABIArgInfo::Coerce:
Daniel Dunbar22e30052008-09-11 01:48:57 +0000852 assert(0 && "Invalid ABI kind for non-return argument");
853
854 case ABIArgInfo::ByVal:
Devang Patela85a9ef2008-09-25 21:02:23 +0000855 Attributes |= llvm::Attribute::ByVal;
Daniel Dunbar22e30052008-09-11 01:48:57 +0000856 assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled");
857 break;
858
859 case ABIArgInfo::Default:
860 if (ParamType->isPromotableIntegerType()) {
861 if (ParamType->isSignedIntegerType()) {
Devang Patela85a9ef2008-09-25 21:02:23 +0000862 Attributes |= llvm::Attribute::SExt;
Daniel Dunbar22e30052008-09-11 01:48:57 +0000863 } else if (ParamType->isUnsignedIntegerType()) {
Devang Patela85a9ef2008-09-25 21:02:23 +0000864 Attributes |= llvm::Attribute::ZExt;
Daniel Dunbar22e30052008-09-11 01:48:57 +0000865 }
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000866 }
Daniel Dunbar22e30052008-09-11 01:48:57 +0000867 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +0000868
Daniel Dunbar1358b202009-01-26 21:26:08 +0000869 case ABIArgInfo::Ignore:
870 // Skip increment, no matching LLVM parameter.
871 continue;
872
Daniel Dunbar04d35782008-09-17 00:51:38 +0000873 case ABIArgInfo::Expand: {
874 std::vector<const llvm::Type*> Tys;
875 // FIXME: This is rather inefficient. Do we ever actually need
876 // to do anything here? The result should be just reconstructed
877 // on the other side, so extension should be a non-issue.
878 getTypes().GetExpandedTypes(ParamType, Tys);
879 Index += Tys.size();
880 continue;
881 }
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000882 }
Daniel Dunbar22e30052008-09-11 01:48:57 +0000883
Devang Patela85a9ef2008-09-25 21:02:23 +0000884 if (Attributes)
885 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar04d35782008-09-17 00:51:38 +0000886 ++Index;
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000887 }
Devang Patel2bb6eb82008-09-26 22:53:57 +0000888 if (FuncAttrs)
889 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
890
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000891}
892
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000893void CodeGenFunction::EmitFunctionProlog(llvm::Function *Fn,
894 QualType RetTy,
895 const FunctionArgList &Args) {
896 // Emit allocs for param decls. Give the LLVM Argument nodes names.
897 llvm::Function::arg_iterator AI = Fn->arg_begin();
898
899 // Name the struct return argument.
Daniel Dunbare126ab12008-09-10 02:41:04 +0000900 if (CGM.ReturnTypeUsesSret(RetTy)) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000901 AI->setName("agg.result");
902 ++AI;
903 }
904
905 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbar04d35782008-09-17 00:51:38 +0000906 i != e; ++i) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000907 const VarDecl *Arg = i->first;
Daniel Dunbar04d35782008-09-17 00:51:38 +0000908 QualType Ty = i->second;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000909 ABIArgInfo ArgI = getABIArgumentInfo(Ty, CGM.getTypes());
Daniel Dunbar22e30052008-09-11 01:48:57 +0000910
911 switch (ArgI.getKind()) {
912 case ABIArgInfo::ByVal:
913 case ABIArgInfo::Default: {
914 assert(AI != Fn->arg_end() && "Argument mismatch!");
915 llvm::Value* V = AI;
Daniel Dunbar04d35782008-09-17 00:51:38 +0000916 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
Daniel Dunbar22e30052008-09-11 01:48:57 +0000917 // This must be a promotion, for something like
918 // "void a(x) short x; {..."
Daniel Dunbar04d35782008-09-17 00:51:38 +0000919 V = EmitScalarConversion(V, Ty, Arg->getType());
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000920 }
Daniel Dunbar22e30052008-09-11 01:48:57 +0000921 EmitParmDecl(*Arg, V);
922 break;
923 }
Daniel Dunbar04d35782008-09-17 00:51:38 +0000924
925 case ABIArgInfo::Expand: {
926 // If this was structure was expand into multiple arguments then
927 // we need to create a temporary and reconstruct it from the
928 // arguments.
Chris Lattner6c5ec622008-11-24 04:00:27 +0000929 std::string Name = Arg->getNameAsString();
Daniel Dunbar04d35782008-09-17 00:51:38 +0000930 llvm::Value *Temp = CreateTempAlloca(ConvertType(Ty),
931 (Name + ".addr").c_str());
932 // FIXME: What are the right qualifiers here?
933 llvm::Function::arg_iterator End =
934 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI);
935 EmitParmDecl(*Arg, Temp);
Daniel Dunbar22e30052008-09-11 01:48:57 +0000936
Daniel Dunbar04d35782008-09-17 00:51:38 +0000937 // Name the arguments used in expansion and increment AI.
938 unsigned Index = 0;
939 for (; AI != End; ++AI, ++Index)
940 AI->setName(Name + "." + llvm::utostr(Index));
941 continue;
942 }
Daniel Dunbar1358b202009-01-26 21:26:08 +0000943
944 case ABIArgInfo::Ignore:
945 break;
946
Daniel Dunbar22e30052008-09-11 01:48:57 +0000947 case ABIArgInfo::Coerce:
Daniel Dunbar22e30052008-09-11 01:48:57 +0000948 case ABIArgInfo::StructRet:
949 assert(0 && "Invalid ABI kind for non-return argument");
950 }
Daniel Dunbar04d35782008-09-17 00:51:38 +0000951
952 ++AI;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000953 }
954 assert(AI == Fn->arg_end() && "Argument mismatch!");
955}
956
Daniel Dunbar708d8a82009-01-27 01:36:03 +0000957/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
958/// a pointer to an object of type \arg Ty.
959///
960/// This safely handles the case when the src type is smaller than the
961/// destination type; in this situation the values of bits which not
962/// present in the src are undefined.
963static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
964 const llvm::Type *Ty,
965 CodeGenFunction &CGF) {
966 const llvm::Type *SrcTy =
967 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
968 unsigned SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
969 unsigned DstSize = CGF.CGM.getTargetData().getTypePaddedSize(Ty);
970
971 // If load is legal, just bitcase the src pointer.
972 if (SrcSize == DstSize) {
973 llvm::Value *Casted =
974 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
975 return CGF.Builder.CreateLoad(Casted);
976 } else {
977 assert(SrcSize < DstSize && "Coercion is losing source bits!");
978
979 // Otherwise do coercion through memory. This is stupid, but
980 // simple.
981 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
982 llvm::Value *Casted =
983 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
984 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
985 return CGF.Builder.CreateLoad(Tmp);
986 }
987}
988
989/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
990/// where the source and destination may have different types.
991///
992/// This safely handles the case when the src type is larger than the
993/// destination type; the upper bits of the src will be lost.
994static void CreateCoercedStore(llvm::Value *Src,
995 llvm::Value *DstPtr,
996 CodeGenFunction &CGF) {
997 const llvm::Type *SrcTy = Src->getType();
998 const llvm::Type *DstTy =
999 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
1000
1001 unsigned SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
1002 unsigned DstSize = CGF.CGM.getTargetData().getTypePaddedSize(DstTy);
1003
1004 // If store is legal, just bitcase the src pointer.
1005 if (SrcSize == DstSize) {
1006 llvm::Value *Casted =
1007 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
1008 CGF.Builder.CreateStore(Src, Casted);
1009 } else {
1010 assert(SrcSize > DstSize && "Coercion is missing bits!");
1011
1012 // Otherwise do coercion through memory. This is stupid, but
1013 // simple.
1014 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
1015 CGF.Builder.CreateStore(Src, Tmp);
1016 llvm::Value *Casted =
1017 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
1018 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(Casted), DstPtr);
1019 }
1020}
1021
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001022void CodeGenFunction::EmitFunctionEpilog(QualType RetTy,
1023 llvm::Value *ReturnValue) {
Daniel Dunbare126ab12008-09-10 02:41:04 +00001024 llvm::Value *RV = 0;
1025
1026 // Functions with no result always return void.
1027 if (ReturnValue) {
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001028 ABIArgInfo RetAI = getABIReturnInfo(RetTy, CGM.getTypes());
Daniel Dunbare126ab12008-09-10 02:41:04 +00001029
1030 switch (RetAI.getKind()) {
1031 case ABIArgInfo::StructRet:
Daniel Dunbar17d35372008-12-18 04:52:14 +00001032 if (RetTy->isAnyComplexType()) {
1033 // FIXME: Volatile
1034 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
1035 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
1036 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1037 EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy);
1038 } else {
1039 Builder.CreateStore(Builder.CreateLoad(ReturnValue),
1040 CurFn->arg_begin());
1041 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001042 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001043
Daniel Dunbare126ab12008-09-10 02:41:04 +00001044 case ABIArgInfo::Default:
1045 RV = Builder.CreateLoad(ReturnValue);
1046 break;
1047
Daniel Dunbar1358b202009-01-26 21:26:08 +00001048 case ABIArgInfo::Ignore:
1049 break;
1050
Daniel Dunbar73d66602008-09-10 07:04:09 +00001051 case ABIArgInfo::Coerce: {
Daniel Dunbar708d8a82009-01-27 01:36:03 +00001052 RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001053 break;
Daniel Dunbar73d66602008-09-10 07:04:09 +00001054 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001055
1056 case ABIArgInfo::ByVal:
1057 case ABIArgInfo::Expand:
1058 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001059 }
1060 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001061
1062 if (RV) {
1063 Builder.CreateRet(RV);
1064 } else {
1065 Builder.CreateRetVoid();
1066 }
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001067}
1068
1069RValue CodeGenFunction::EmitCall(llvm::Value *Callee,
Daniel Dunbare126ab12008-09-10 02:41:04 +00001070 QualType RetTy,
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001071 const CallArgList &CallArgs) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001072 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001073
1074 // Handle struct-return functions by passing a pointer to the
1075 // location that we would like to return into.
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001076 ABIArgInfo RetAI = getABIReturnInfo(RetTy, CGM.getTypes());
Daniel Dunbare126ab12008-09-10 02:41:04 +00001077 switch (RetAI.getKind()) {
1078 case ABIArgInfo::StructRet:
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001079 // Create a temporary alloca to hold the result of the call. :(
Daniel Dunbar04d35782008-09-17 00:51:38 +00001080 Args.push_back(CreateTempAlloca(ConvertType(RetTy)));
Daniel Dunbare126ab12008-09-10 02:41:04 +00001081 break;
1082
1083 case ABIArgInfo::Default:
Daniel Dunbar1358b202009-01-26 21:26:08 +00001084 case ABIArgInfo::Ignore:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001085 case ABIArgInfo::Coerce:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001086 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001087
1088 case ABIArgInfo::ByVal:
1089 case ABIArgInfo::Expand:
1090 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001091 }
1092
1093 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
1094 I != E; ++I) {
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001095 ABIArgInfo ArgInfo = getABIArgumentInfo(I->second, CGM.getTypes());
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001096 RValue RV = I->first;
Daniel Dunbar04d35782008-09-17 00:51:38 +00001097
1098 switch (ArgInfo.getKind()) {
1099 case ABIArgInfo::ByVal: // Default is byval
1100 case ABIArgInfo::Default:
1101 if (RV.isScalar()) {
1102 Args.push_back(RV.getScalarVal());
1103 } else if (RV.isComplex()) {
1104 // Make a temporary alloca to pass the argument.
1105 Args.push_back(CreateTempAlloca(ConvertType(I->second)));
1106 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
1107 } else {
1108 Args.push_back(RV.getAggregateAddr());
1109 }
1110 break;
1111
Daniel Dunbar1358b202009-01-26 21:26:08 +00001112 case ABIArgInfo::Ignore:
1113 break;
1114
Daniel Dunbar04d35782008-09-17 00:51:38 +00001115 case ABIArgInfo::StructRet:
1116 case ABIArgInfo::Coerce:
1117 assert(0 && "Invalid ABI kind for non-return argument");
1118 break;
1119
1120 case ABIArgInfo::Expand:
1121 ExpandTypeToArgs(I->second, RV, Args);
1122 break;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001123 }
1124 }
1125
1126 llvm::CallInst *CI = Builder.CreateCall(Callee,&Args[0],&Args[0]+Args.size());
Daniel Dunbare126ab12008-09-10 02:41:04 +00001127 CGCallInfo CallInfo(RetTy, CallArgs);
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001128
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001129 // FIXME: Provide TargetDecl so nounwind, noreturn, etc, etc get set.
Devang Patela85a9ef2008-09-25 21:02:23 +00001130 CodeGen::AttributeListType AttributeList;
1131 CGM.ConstructAttributeList(0,
Daniel Dunbar3ef2e852008-09-10 00:41:16 +00001132 CallInfo.argtypes_begin(), CallInfo.argtypes_end(),
Devang Patela85a9ef2008-09-25 21:02:23 +00001133 AttributeList);
1134 CI->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(),
1135 AttributeList.size()));
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001136
1137 if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee))
1138 CI->setCallingConv(F->getCallingConv());
1139 if (CI->getType() != llvm::Type::VoidTy)
1140 CI->setName("call");
Daniel Dunbare126ab12008-09-10 02:41:04 +00001141
1142 switch (RetAI.getKind()) {
1143 case ABIArgInfo::StructRet:
1144 if (RetTy->isAnyComplexType())
Daniel Dunbar04d35782008-09-17 00:51:38 +00001145 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Daniel Dunbar17d35372008-12-18 04:52:14 +00001146 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar04d35782008-09-17 00:51:38 +00001147 return RValue::getAggregate(Args[0]);
Daniel Dunbar17d35372008-12-18 04:52:14 +00001148 else
1149 return RValue::get(Builder.CreateLoad(Args[0]));
Daniel Dunbar22e30052008-09-11 01:48:57 +00001150
Daniel Dunbare126ab12008-09-10 02:41:04 +00001151 case ABIArgInfo::Default:
1152 return RValue::get(RetTy->isVoidType() ? 0 : CI);
1153
Daniel Dunbar1358b202009-01-26 21:26:08 +00001154 case ABIArgInfo::Ignore:
1155 return RValue::get(0);
1156
Daniel Dunbar73d66602008-09-10 07:04:09 +00001157 case ABIArgInfo::Coerce: {
Daniel Dunbar708d8a82009-01-27 01:36:03 +00001158 llvm::Value *V = CreateTempAlloca(ConvertType(RetTy), "coerce");
1159 CreateCoercedStore(CI, V, *this);
Anders Carlssonfccf7472008-11-25 22:21:48 +00001160 if (RetTy->isAnyComplexType())
1161 return RValue::getComplex(LoadComplexFromAddr(V, false));
Daniel Dunbar1358b202009-01-26 21:26:08 +00001162 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssonfccf7472008-11-25 22:21:48 +00001163 return RValue::getAggregate(V);
Daniel Dunbar1358b202009-01-26 21:26:08 +00001164 else
1165 return RValue::get(Builder.CreateLoad(V));
Daniel Dunbar73d66602008-09-10 07:04:09 +00001166 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001167
1168 case ABIArgInfo::ByVal:
1169 case ABIArgInfo::Expand:
1170 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001171 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001172
1173 assert(0 && "Unhandled ABIArgInfo::Kind");
1174 return RValue::get(0);
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001175}