blob: 6ffe8c328ca23b7d69192cf6d9369eb8ecdcc4ec [file] [log] [blame]
Daniel Dunbar3d7c90b2008-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 Dunbarc68897d2008-09-10 00:41:16 +000017#include "CodeGenModule.h"
Daniel Dunbard9eff3d2008-10-13 17:02:26 +000018#include "clang/Basic/TargetInfo.h"
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000019#include "clang/AST/ASTContext.h"
20#include "clang/AST/Decl.h"
21#include "clang/AST/DeclObjC.h"
Daniel Dunbar17aa9412009-01-29 08:13:58 +000022#include "clang/AST/RecordLayout.h"
Daniel Dunbar8fc81b02008-09-17 00:51:38 +000023#include "llvm/ADT/StringExtras.h"
Devang Patel3e1f51b2008-09-24 01:01:36 +000024#include "llvm/Attributes.h"
Daniel Dunbar3334a442009-01-24 08:32:22 +000025#include "llvm/Support/CommandLine.h"
Daniel Dunbar0f4aa3c2009-01-27 01:36:03 +000026#include "llvm/Target/TargetData.h"
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000027using namespace clang;
28using namespace CodeGen;
29
Daniel Dunbar3334a442009-01-24 08:32:22 +000030static llvm::cl::opt<bool>
31UseX86_64ABI("use-x86_64-abi",
32 llvm::cl::desc("Enable use of experimental x86_64 ABI."),
33 llvm::cl::init(false));
34
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000035/***/
36
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000037// FIXME: Use iterator and sidestep silly type array creation.
38
Daniel Dunbar7a95ca32008-09-10 04:01:49 +000039CGFunctionInfo::CGFunctionInfo(const FunctionTypeNoProto *FTNP)
40 : IsVariadic(true)
41{
42 ArgTypes.push_back(FTNP->getResultType());
43}
44
45CGFunctionInfo::CGFunctionInfo(const FunctionTypeProto *FTP)
46 : IsVariadic(FTP->isVariadic())
47{
48 ArgTypes.push_back(FTP->getResultType());
49 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
50 ArgTypes.push_back(FTP->getArgType(i));
51}
52
53// FIXME: Is there really any reason to have this still?
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000054CGFunctionInfo::CGFunctionInfo(const FunctionDecl *FD)
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000055{
56 const FunctionType *FTy = FD->getType()->getAsFunctionType();
57 const FunctionTypeProto *FTP = dyn_cast<FunctionTypeProto>(FTy);
Daniel Dunbar7a95ca32008-09-10 04:01:49 +000058
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000059 ArgTypes.push_back(FTy->getResultType());
Daniel Dunbar7a95ca32008-09-10 04:01:49 +000060 if (FTP) {
61 IsVariadic = FTP->isVariadic();
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000062 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
63 ArgTypes.push_back(FTP->getArgType(i));
Daniel Dunbar7a95ca32008-09-10 04:01:49 +000064 } else {
65 IsVariadic = true;
66 }
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000067}
68
69CGFunctionInfo::CGFunctionInfo(const ObjCMethodDecl *MD,
70 const ASTContext &Context)
Daniel Dunbar7a95ca32008-09-10 04:01:49 +000071 : IsVariadic(MD->isVariadic())
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000072{
73 ArgTypes.push_back(MD->getResultType());
74 ArgTypes.push_back(MD->getSelfDecl()->getType());
75 ArgTypes.push_back(Context.getObjCSelType());
76 for (ObjCMethodDecl::param_const_iterator i = MD->param_begin(),
77 e = MD->param_end(); i != e; ++i)
78 ArgTypes.push_back((*i)->getType());
79}
80
Daniel Dunbar76c8eb72008-09-10 00:32:18 +000081ArgTypeIterator CGFunctionInfo::argtypes_begin() const {
82 return ArgTypes.begin();
83}
84
85ArgTypeIterator CGFunctionInfo::argtypes_end() const {
86 return ArgTypes.end();
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000087}
88
89/***/
90
Daniel Dunbar76c8eb72008-09-10 00:32:18 +000091CGCallInfo::CGCallInfo(QualType _ResultType, const CallArgList &_Args) {
92 ArgTypes.push_back(_ResultType);
93 for (CallArgList::const_iterator i = _Args.begin(), e = _Args.end(); i!=e; ++i)
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000094 ArgTypes.push_back(i->second);
95}
96
Daniel Dunbar76c8eb72008-09-10 00:32:18 +000097ArgTypeIterator CGCallInfo::argtypes_begin() const {
98 return ArgTypes.begin();
99}
100
101ArgTypeIterator CGCallInfo::argtypes_end() const {
102 return ArgTypes.end();
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +0000103}
Daniel Dunbar613855c2008-09-09 23:27:19 +0000104
105/***/
106
Daniel Dunbard3674e62008-09-11 01:48:57 +0000107/// ABIArgInfo - Helper class to encapsulate information about how a
108/// specific C type should be passed to or returned from a function.
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000109class ABIArgInfo {
110public:
111 enum Kind {
112 Default,
Daniel Dunbar9ae0afd2008-12-18 04:52:14 +0000113 StructRet, /// Only valid for return values. The return value
114 /// should be passed through a pointer to a caller
115 /// allocated location passed as an implicit first
116 /// argument to the function.
Daniel Dunbard3674e62008-09-11 01:48:57 +0000117
Daniel Dunbar94a6f252009-01-26 21:26:08 +0000118 Ignore, /// Ignore the argument (treat as void). Useful for
119 /// void and empty structs.
120
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000121 Coerce, /// Only valid for aggregate return types, the argument
122 /// should be accessed by coercion to a provided type.
Daniel Dunbard3674e62008-09-11 01:48:57 +0000123
124 ByVal, /// Only valid for aggregate argument types. The
125 /// structure should be passed "byval" with the
126 /// specified alignment (0 indicates default
127 /// alignment).
128
129 Expand, /// Only valid for aggregate argument types. The
130 /// structure should be expanded into consecutive
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000131 /// arguments for its constituent fields. Currently
132 /// expand is only allowed on structures whose fields
133 /// are all scalar types or are themselves expandable
134 /// types.
Daniel Dunbard3674e62008-09-11 01:48:57 +0000135
136 KindFirst=Default, KindLast=Expand
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000137 };
138
139private:
140 Kind TheKind;
Daniel Dunbar573884e2008-09-10 07:04:09 +0000141 const llvm::Type *TypeData;
Daniel Dunbard3674e62008-09-11 01:48:57 +0000142 unsigned UIntData;
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000143
Daniel Dunbard3674e62008-09-11 01:48:57 +0000144 ABIArgInfo(Kind K, const llvm::Type *TD=0,
145 unsigned UI=0) : TheKind(K),
146 TypeData(TD),
147 UIntData(0) {}
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000148public:
149 static ABIArgInfo getDefault() {
Daniel Dunbard3674e62008-09-11 01:48:57 +0000150 return ABIArgInfo(Default);
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000151 }
152 static ABIArgInfo getStructRet() {
Daniel Dunbard3674e62008-09-11 01:48:57 +0000153 return ABIArgInfo(StructRet);
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000154 }
Daniel Dunbar94a6f252009-01-26 21:26:08 +0000155 static ABIArgInfo getIgnore() {
156 return ABIArgInfo(Ignore);
157 }
Daniel Dunbar573884e2008-09-10 07:04:09 +0000158 static ABIArgInfo getCoerce(const llvm::Type *T) {
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000159 return ABIArgInfo(Coerce, T);
160 }
Daniel Dunbard3674e62008-09-11 01:48:57 +0000161 static ABIArgInfo getByVal(unsigned Alignment) {
162 return ABIArgInfo(ByVal, 0, Alignment);
163 }
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000164 static ABIArgInfo getExpand() {
165 return ABIArgInfo(Expand);
166 }
Daniel Dunbara72d4ae2008-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 Dunbar94a6f252009-01-26 21:26:08 +0000171 bool isIgnore() const { return TheKind == Ignore; }
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000172 bool isCoerce() const { return TheKind == Coerce; }
Daniel Dunbard3674e62008-09-11 01:48:57 +0000173 bool isByVal() const { return TheKind == ByVal; }
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000174 bool isExpand() const { return TheKind == Expand; }
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000175
176 // Coerce accessors
Daniel Dunbar573884e2008-09-10 07:04:09 +0000177 const llvm::Type *getCoerceToType() const {
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000178 assert(TheKind == Coerce && "Invalid kind!");
179 return TypeData;
180 }
Daniel Dunbard3674e62008-09-11 01:48:57 +0000181
182 // ByVal accessors
183 unsigned getByValAlignment() const {
184 assert(TheKind == ByVal && "Invalid kind!");
185 return UIntData;
186 }
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000187};
188
189/***/
190
Daniel Dunbard9eff3d2008-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 Dunbar86d97c72008-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 Gregorffca3a22009-01-09 17:18:27 +0000222 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar86d97c72008-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 Gregorffca3a22009-01-09 17:18:27 +0000249 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar86d97c72008-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 Gregorffca3a22009-01-09 17:18:27 +0000280 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar86d97c72008-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 Dunbard9eff3d2008-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 Dunbara72d4ae2008-09-10 02:41:04 +0000324 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar86d97c72008-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 Dunbar573884e2008-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 Dunbara72d4ae2008-09-10 02:41:04 +0000362 } else {
363 return ABIArgInfo::getDefault();
364 }
365}
366
Daniel Dunbard9eff3d2008-10-13 17:02:26 +0000367ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
368 ASTContext &Context) const {
Daniel Dunbard34d6dc2008-09-17 20:11:04 +0000369 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar86d97c72008-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 Dunbard3674e62008-09-11 01:48:57 +0000389 return ABIArgInfo::getByVal(0);
390 } else {
391 return ABIArgInfo::getDefault();
392 }
393}
394
Daniel Dunbar3334a442009-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 Dunbar94a6f252009-01-26 21:26:08 +0000443 if (k == BuiltinType::Void) {
444 Lo = NoClass;
445 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
Daniel Dunbar3334a442009-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 Dunbard678d3d2009-01-27 02:01:34 +0000453
454 // FIXME: _Decimal32 and _Decimal64 are SSE.
455 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Daniel Dunbar3334a442009-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 Dunbard678d3d2009-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 Dunbar3334a442009-01-24 08:32:22 +0000471 } else if (const ComplexType *CT = Ty->getAsComplexType()) {
472 QualType ET = CT->getElementType();
473
Daniel Dunbar8e6652a2009-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 Dunbar3334a442009-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;
Daniel Dunbar17aa9412009-01-29 08:13:58 +0000486 } else if (const RecordType *RT = Ty->getAsRecordType()) {
487 unsigned Size = Context.getTypeSize(Ty);
488
489 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
490 // than two eightbytes, ..., it has class MEMORY.
491 if (Size > 128)
492 return;
493
494 const RecordDecl *RD = RT->getDecl();
495
496 // Assume variable sized types are passed in memory.
497 if (RD->hasFlexibleArrayMember())
498 return;
499
500 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
501
502 // Reset Lo class, this will be recomputed.
503 Lo = NoClass;
504 unsigned idx = 0;
505 for (RecordDecl::field_iterator i = RD->field_begin(),
506 e = RD->field_end(); i != e; ++i, ++idx) {
507 unsigned Offset = Layout.getFieldOffset(idx);
508
509 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
510 // fields, it has class MEMORY.
511 if (Offset % Context.getTypeAlign(i->getType())) {
512 Lo = Memory;
513 return;
514 }
515
516 // Determine which half of the structure we are classifying.
517 //
518 // AMD64-ABI 3.2.3p2: Rule 3. f the size of the aggregate
519 // exceeds a single eightbyte, each is classified
520 // separately. Each eightbyte gets initialized to class
521 // NO_CLASS.
522 Class &Target = Offset < 64 ? Lo : Hi;
523
524 // Classify this field.
525 Class FieldLo, FieldHi;
526 classify(i->getType(), Context, FieldLo, FieldHi);
527
528 // Merge the lo field classifcation.
529 //
530 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
531 // classified recursively so that always two fields are
532 // considered. The resulting class is calculated according to
533 // the classes of the fields in the eightbyte:
534 //
535 // (a) If both classes are equal, this is the resulting class.
536 //
537 // (b) If one of the classes is NO_CLASS, the resulting class is
538 // the other class.
539 //
540 // (c) If one of the classes is MEMORY, the result is the MEMORY
541 // class.
542 //
543 // (d) If one of the classes is INTEGER, the result is the
544 // INTEGER.
545 //
546 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
547 // MEMORY is used as class.
548 //
549 // (f) Otherwise class SSE is used.
550 if (Target == FieldLo || FieldLo == NoClass) ;
551 else if (Target == NoClass)
552 Target = FieldLo;
553 else if (FieldLo == Memory) {
554 // Memory is never over-ridden, just bail.
555 Lo = Memory;
556 return;
557 }
558 else if (Target == Integer || FieldLo == Integer)
559 Target = Integer;
560 else if (FieldLo == X87 || FieldLo == X87Up || FieldLo == ComplexX87) {
561 // As before, just bail once we generate a memory class.
562 Lo = Memory;
563 return;
564 } else
565 Target = SSE;
566
567 // It isn't clear from the ABI spec what the role of the high
568 // classification is here, but since this should only happen
569 // when we have a struct with a two eightbyte member, we can
570 // just push the field high class into the overall high class.
571 if (FieldHi != NoClass)
572 Hi = FieldHi;
573 }
574
575 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
576 //
577 // (a) If one of the classes is MEMORY, the whole argument is
578 // passed in memory.
579 //
580 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
581
582 // The first of these conditions is guaranteed by how we implement
583 // the merge (just bail). I don't believe the second is actually
584 // possible at all.
585 assert(Lo != Memory && "Unexpected memory classification.");
586 if (Hi == SSEUp && Lo != SSE)
587 Hi = SSE;
Daniel Dunbar3334a442009-01-24 08:32:22 +0000588 }
589}
590
Daniel Dunbar707f6432009-01-15 18:18:40 +0000591ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
592 ASTContext &Context) const {
Daniel Dunbar3334a442009-01-24 08:32:22 +0000593 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
594 // classification algorithm.
595 X86_64ABIInfo::Class Lo, Hi;
596 classify(RetTy, Context, Lo, Hi);
597
598 const llvm::Type *ResType = 0;
599 switch (Lo) {
600 case NoClass:
Daniel Dunbar94a6f252009-01-26 21:26:08 +0000601 return ABIArgInfo::getIgnore();
Daniel Dunbar3334a442009-01-24 08:32:22 +0000602
603 case SSEUp:
604 case X87Up:
605 assert(0 && "Invalid classification for lo word.");
606
607 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
608 // hidden argument, i.e. structret.
609 case Memory:
610 return ABIArgInfo::getStructRet();
611
612 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
613 // available register of the sequence %rax, %rdx is used.
614 case Integer:
615 ResType = llvm::Type::Int64Ty; break;
616
617 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
618 // available SSE register of the sequence %xmm0, %xmm1 is used.
619 case SSE:
620 ResType = llvm::Type::DoubleTy; break;
621
622 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
623 // returned on the X87 stack in %st0 as 80-bit x87 number.
624 case X87:
625 ResType = llvm::Type::X86_FP80Ty; break;
626
627 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
628 // part of the value is returned in %st0 and the imaginary part in
629 // %st1.
630 case ComplexX87:
631 assert(Hi == NoClass && "Unexpected ComplexX87 classification.");
632 ResType = llvm::VectorType::get(llvm::Type::X86_FP80Ty, 2);
633 break;
634 }
635
636 switch (Hi) {
637 // Memory was handled previously, and ComplexX87 and X87 should
638 // never occur as hi classes.
639 case Memory:
640 case X87:
641 case ComplexX87:
642 assert(0 && "Invalid classification for hi word.");
643
644 case NoClass: break;
645 case Integer:
Daniel Dunbar227e1a72009-01-29 07:36:07 +0000646 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
647 break;
Daniel Dunbar3334a442009-01-24 08:32:22 +0000648 case SSE:
Daniel Dunbar227e1a72009-01-29 07:36:07 +0000649 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
650 break;
Daniel Dunbar3334a442009-01-24 08:32:22 +0000651
652 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
653 // is passed in the upper half of the last used SSE register.
654 //
655 // SSEUP should always be preceeded by SSE, just widen.
656 case SSEUp:
657 assert(Lo == SSE && "Unexpected SSEUp classification.");
658 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
659 break;
660
661 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
Daniel Dunbar227e1a72009-01-29 07:36:07 +0000662 // returned together with the previous X87 value in %st0.
Daniel Dunbar3334a442009-01-24 08:32:22 +0000663 //
664 // X87UP should always be preceeded by X87, so we don't need to do
665 // anything here.
666 case X87Up:
667 assert(Lo == X87 && "Unexpected X87Up classification.");
668 break;
669 }
670
671 return ABIArgInfo::getCoerce(ResType);
Daniel Dunbar707f6432009-01-15 18:18:40 +0000672}
673
674ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty,
675 ASTContext &Context) const {
676 return ABIArgInfo::getDefault();
677}
678
Daniel Dunbard9eff3d2008-10-13 17:02:26 +0000679ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
680 ASTContext &Context) const {
681 return ABIArgInfo::getDefault();
682}
683
684ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
685 ASTContext &Context) const {
Daniel Dunbard9eff3d2008-10-13 17:02:26 +0000686 return ABIArgInfo::getDefault();
687}
688
689const ABIInfo &CodeGenTypes::getABIInfo() const {
690 if (TheABIInfo)
691 return *TheABIInfo;
692
693 // For now we just cache this in the CodeGenTypes and don't bother
694 // to free it.
695 const char *TargetPrefix = getContext().Target.getTargetPrefix();
696 if (strcmp(TargetPrefix, "x86") == 0) {
Daniel Dunbar707f6432009-01-15 18:18:40 +0000697 switch (getContext().Target.getPointerWidth(0)) {
698 case 32:
Daniel Dunbard9eff3d2008-10-13 17:02:26 +0000699 return *(TheABIInfo = new X86_32ABIInfo());
Daniel Dunbar707f6432009-01-15 18:18:40 +0000700 case 64:
Daniel Dunbar3334a442009-01-24 08:32:22 +0000701 if (UseX86_64ABI)
702 return *(TheABIInfo = new X86_64ABIInfo());
Daniel Dunbar707f6432009-01-15 18:18:40 +0000703 }
Daniel Dunbard9eff3d2008-10-13 17:02:26 +0000704 }
705
706 return *(TheABIInfo = new DefaultABIInfo);
707}
708
709// getABIReturnInfo - Wrap the ABIInfo getABIReturnInfo, altering
710// "default" types to StructRet when appropriate for simplicity.
711static ABIArgInfo getABIReturnInfo(QualType Ty, CodeGenTypes &CGT) {
712 assert(!Ty->isArrayType() &&
713 "Array types cannot be passed directly.");
714 ABIArgInfo Info = CGT.getABIInfo().classifyReturnType(Ty, CGT.getContext());
Daniel Dunbard34d6dc2008-09-17 20:11:04 +0000715 // Ensure default on aggregate types is StructRet.
716 if (Info.isDefault() && CodeGenFunction::hasAggregateLLVMType(Ty))
717 return ABIArgInfo::getStructRet();
718 return Info;
719}
720
Daniel Dunbard9eff3d2008-10-13 17:02:26 +0000721// getABIArgumentInfo - Wrap the ABIInfo getABIReturnInfo, altering
722// "default" types to ByVal when appropriate for simplicity.
723static ABIArgInfo getABIArgumentInfo(QualType Ty, CodeGenTypes &CGT) {
724 assert(!Ty->isArrayType() &&
725 "Array types cannot be passed directly.");
726 ABIArgInfo Info = CGT.getABIInfo().classifyArgumentType(Ty, CGT.getContext());
Daniel Dunbard34d6dc2008-09-17 20:11:04 +0000727 // Ensure default on aggregate types is ByVal.
728 if (Info.isDefault() && CodeGenFunction::hasAggregateLLVMType(Ty))
729 return ABIArgInfo::getByVal(0);
730 return Info;
731}
732
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000733/***/
734
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000735void CodeGenTypes::GetExpandedTypes(QualType Ty,
736 std::vector<const llvm::Type*> &ArgTys) {
737 const RecordType *RT = Ty->getAsStructureType();
738 assert(RT && "Can only expand structure types.");
739 const RecordDecl *RD = RT->getDecl();
740 assert(!RD->hasFlexibleArrayMember() &&
741 "Cannot expand structure with flexible array.");
742
Douglas Gregorffca3a22009-01-09 17:18:27 +0000743 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000744 e = RD->field_end(); i != e; ++i) {
745 const FieldDecl *FD = *i;
746 assert(!FD->isBitField() &&
747 "Cannot expand structure with bit-field members.");
748
749 QualType FT = FD->getType();
750 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
751 GetExpandedTypes(FT, ArgTys);
752 } else {
753 ArgTys.push_back(ConvertType(FT));
754 }
755 }
756}
757
758llvm::Function::arg_iterator
759CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
760 llvm::Function::arg_iterator AI) {
761 const RecordType *RT = Ty->getAsStructureType();
762 assert(RT && "Can only expand structure types.");
763
764 RecordDecl *RD = RT->getDecl();
765 assert(LV.isSimple() &&
766 "Unexpected non-simple lvalue during struct expansion.");
767 llvm::Value *Addr = LV.getAddress();
768 for (RecordDecl::field_iterator i = RD->field_begin(),
769 e = RD->field_end(); i != e; ++i) {
770 FieldDecl *FD = *i;
771 QualType FT = FD->getType();
772
773 // FIXME: What are the right qualifiers here?
774 LValue LV = EmitLValueForField(Addr, FD, false, 0);
775 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
776 AI = ExpandTypeFromArgs(FT, LV, AI);
777 } else {
778 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
779 ++AI;
780 }
781 }
782
783 return AI;
784}
785
786void
787CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
788 llvm::SmallVector<llvm::Value*, 16> &Args) {
789 const RecordType *RT = Ty->getAsStructureType();
790 assert(RT && "Can only expand structure types.");
791
792 RecordDecl *RD = RT->getDecl();
793 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
794 llvm::Value *Addr = RV.getAggregateAddr();
795 for (RecordDecl::field_iterator i = RD->field_begin(),
796 e = RD->field_end(); i != e; ++i) {
797 FieldDecl *FD = *i;
798 QualType FT = FD->getType();
799
800 // FIXME: What are the right qualifiers here?
801 LValue LV = EmitLValueForField(Addr, FD, false, 0);
802 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
803 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
804 } else {
805 RValue RV = EmitLoadOfLValue(LV, FT);
806 assert(RV.isScalar() &&
807 "Unexpected non-scalar rvalue during struct expansion.");
808 Args.push_back(RV.getScalarVal());
809 }
810 }
811}
812
813/***/
814
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000815const llvm::FunctionType *
Daniel Dunbarb8b47592008-09-10 07:00:50 +0000816CodeGenTypes::GetFunctionType(const CGCallInfo &CI, bool IsVariadic) {
817 return GetFunctionType(CI.argtypes_begin(), CI.argtypes_end(), IsVariadic);
818}
819
820const llvm::FunctionType *
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000821CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI) {
Daniel Dunbarb8b47592008-09-10 07:00:50 +0000822 return GetFunctionType(FI.argtypes_begin(), FI.argtypes_end(), FI.isVariadic());
823}
824
825const llvm::FunctionType *
826CodeGenTypes::GetFunctionType(ArgTypeIterator begin, ArgTypeIterator end,
827 bool IsVariadic) {
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000828 std::vector<const llvm::Type*> ArgTys;
829
830 const llvm::Type *ResultType = 0;
831
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000832 QualType RetTy = *begin;
Daniel Dunbard9eff3d2008-10-13 17:02:26 +0000833 ABIArgInfo RetAI = getABIReturnInfo(RetTy, *this);
Daniel Dunbard3674e62008-09-11 01:48:57 +0000834 switch (RetAI.getKind()) {
835 case ABIArgInfo::ByVal:
836 case ABIArgInfo::Expand:
837 assert(0 && "Invalid ABI kind for return argument");
838
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000839 case ABIArgInfo::Default:
840 if (RetTy->isVoidType()) {
841 ResultType = llvm::Type::VoidTy;
842 } else {
Daniel Dunbarb8b47592008-09-10 07:00:50 +0000843 ResultType = ConvertType(RetTy);
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000844 }
845 break;
846
847 case ABIArgInfo::StructRet: {
848 ResultType = llvm::Type::VoidTy;
Daniel Dunbarb8b47592008-09-10 07:00:50 +0000849 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000850 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
851 break;
852 }
853
Daniel Dunbar94a6f252009-01-26 21:26:08 +0000854 case ABIArgInfo::Ignore:
855 ResultType = llvm::Type::VoidTy;
856 break;
857
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000858 case ABIArgInfo::Coerce:
Daniel Dunbar573884e2008-09-10 07:04:09 +0000859 ResultType = RetAI.getCoerceToType();
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000860 break;
861 }
862
863 for (++begin; begin != end; ++begin) {
Daniel Dunbard9eff3d2008-10-13 17:02:26 +0000864 ABIArgInfo AI = getABIArgumentInfo(*begin, *this);
Daniel Dunbarb8b47592008-09-10 07:00:50 +0000865 const llvm::Type *Ty = ConvertType(*begin);
Daniel Dunbard3674e62008-09-11 01:48:57 +0000866
867 switch (AI.getKind()) {
Daniel Dunbar94a6f252009-01-26 21:26:08 +0000868 case ABIArgInfo::Ignore:
869 break;
870
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000871 case ABIArgInfo::Coerce:
Daniel Dunbard3674e62008-09-11 01:48:57 +0000872 case ABIArgInfo::StructRet:
873 assert(0 && "Invalid ABI kind for non-return argument");
874
875 case ABIArgInfo::ByVal:
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000876 // byval arguments are always on the stack, which is addr space #0.
877 ArgTys.push_back(llvm::PointerType::getUnqual(Ty));
Daniel Dunbard3674e62008-09-11 01:48:57 +0000878 assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled");
879 break;
880
881 case ABIArgInfo::Default:
882 ArgTys.push_back(Ty);
883 break;
Daniel Dunbard3674e62008-09-11 01:48:57 +0000884
885 case ABIArgInfo::Expand:
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000886 GetExpandedTypes(*begin, ArgTys);
Daniel Dunbard3674e62008-09-11 01:48:57 +0000887 break;
888 }
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000889 }
890
Daniel Dunbarb8b47592008-09-10 07:00:50 +0000891 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000892}
893
Daniel Dunbarc68897d2008-09-10 00:41:16 +0000894bool CodeGenModule::ReturnTypeUsesSret(QualType RetTy) {
Daniel Dunbard9eff3d2008-10-13 17:02:26 +0000895 return getABIReturnInfo(RetTy, getTypes()).isStructRet();
Daniel Dunbar81cf67f2008-09-09 23:48:28 +0000896}
897
Devang Patel322300d2008-09-25 21:02:23 +0000898void CodeGenModule::ConstructAttributeList(const Decl *TargetDecl,
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000899 ArgTypeIterator begin,
900 ArgTypeIterator end,
Devang Patel322300d2008-09-25 21:02:23 +0000901 AttributeListType &PAL) {
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000902 unsigned FuncAttrs = 0;
Devang Patel597e7082008-09-26 22:53:57 +0000903 unsigned RetAttrs = 0;
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000904
905 if (TargetDecl) {
906 if (TargetDecl->getAttr<NoThrowAttr>())
Devang Patel322300d2008-09-25 21:02:23 +0000907 FuncAttrs |= llvm::Attribute::NoUnwind;
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000908 if (TargetDecl->getAttr<NoReturnAttr>())
Devang Patel322300d2008-09-25 21:02:23 +0000909 FuncAttrs |= llvm::Attribute::NoReturn;
Anders Carlssonb8316282008-10-05 23:32:53 +0000910 if (TargetDecl->getAttr<PureAttr>())
911 FuncAttrs |= llvm::Attribute::ReadOnly;
912 if (TargetDecl->getAttr<ConstAttr>())
913 FuncAttrs |= llvm::Attribute::ReadNone;
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000914 }
915
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000916 QualType RetTy = *begin;
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000917 unsigned Index = 1;
Daniel Dunbard9eff3d2008-10-13 17:02:26 +0000918 ABIArgInfo RetAI = getABIReturnInfo(RetTy, getTypes());
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000919 switch (RetAI.getKind()) {
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000920 case ABIArgInfo::Default:
921 if (RetTy->isPromotableIntegerType()) {
922 if (RetTy->isSignedIntegerType()) {
Devang Patel597e7082008-09-26 22:53:57 +0000923 RetAttrs |= llvm::Attribute::SExt;
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000924 } else if (RetTy->isUnsignedIntegerType()) {
Devang Patel597e7082008-09-26 22:53:57 +0000925 RetAttrs |= llvm::Attribute::ZExt;
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000926 }
927 }
928 break;
929
930 case ABIArgInfo::StructRet:
Devang Patel322300d2008-09-25 21:02:23 +0000931 PAL.push_back(llvm::AttributeWithIndex::get(Index,
932 llvm::Attribute::StructRet|
933 llvm::Attribute::NoAlias));
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000934 ++Index;
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000935 break;
936
Daniel Dunbar94a6f252009-01-26 21:26:08 +0000937 case ABIArgInfo::Ignore:
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000938 case ABIArgInfo::Coerce:
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000939 break;
Daniel Dunbard3674e62008-09-11 01:48:57 +0000940
941 case ABIArgInfo::ByVal:
942 case ABIArgInfo::Expand:
943 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000944 }
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000945
Devang Patel597e7082008-09-26 22:53:57 +0000946 if (RetAttrs)
947 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000948 for (++begin; begin != end; ++begin) {
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000949 QualType ParamType = *begin;
Devang Patel322300d2008-09-25 21:02:23 +0000950 unsigned Attributes = 0;
Daniel Dunbard9eff3d2008-10-13 17:02:26 +0000951 ABIArgInfo AI = getABIArgumentInfo(ParamType, getTypes());
Daniel Dunbard3674e62008-09-11 01:48:57 +0000952
953 switch (AI.getKind()) {
954 case ABIArgInfo::StructRet:
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000955 case ABIArgInfo::Coerce:
Daniel Dunbard3674e62008-09-11 01:48:57 +0000956 assert(0 && "Invalid ABI kind for non-return argument");
957
958 case ABIArgInfo::ByVal:
Devang Patel322300d2008-09-25 21:02:23 +0000959 Attributes |= llvm::Attribute::ByVal;
Daniel Dunbard3674e62008-09-11 01:48:57 +0000960 assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled");
961 break;
962
963 case ABIArgInfo::Default:
964 if (ParamType->isPromotableIntegerType()) {
965 if (ParamType->isSignedIntegerType()) {
Devang Patel322300d2008-09-25 21:02:23 +0000966 Attributes |= llvm::Attribute::SExt;
Daniel Dunbard3674e62008-09-11 01:48:57 +0000967 } else if (ParamType->isUnsignedIntegerType()) {
Devang Patel322300d2008-09-25 21:02:23 +0000968 Attributes |= llvm::Attribute::ZExt;
Daniel Dunbard3674e62008-09-11 01:48:57 +0000969 }
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000970 }
Daniel Dunbard3674e62008-09-11 01:48:57 +0000971 break;
Daniel Dunbard3674e62008-09-11 01:48:57 +0000972
Daniel Dunbar94a6f252009-01-26 21:26:08 +0000973 case ABIArgInfo::Ignore:
974 // Skip increment, no matching LLVM parameter.
975 continue;
976
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000977 case ABIArgInfo::Expand: {
978 std::vector<const llvm::Type*> Tys;
979 // FIXME: This is rather inefficient. Do we ever actually need
980 // to do anything here? The result should be just reconstructed
981 // on the other side, so extension should be a non-issue.
982 getTypes().GetExpandedTypes(ParamType, Tys);
983 Index += Tys.size();
984 continue;
985 }
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000986 }
Daniel Dunbard3674e62008-09-11 01:48:57 +0000987
Devang Patel322300d2008-09-25 21:02:23 +0000988 if (Attributes)
989 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000990 ++Index;
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000991 }
Devang Patel597e7082008-09-26 22:53:57 +0000992 if (FuncAttrs)
993 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
994
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000995}
996
Daniel Dunbar613855c2008-09-09 23:27:19 +0000997void CodeGenFunction::EmitFunctionProlog(llvm::Function *Fn,
998 QualType RetTy,
999 const FunctionArgList &Args) {
1000 // Emit allocs for param decls. Give the LLVM Argument nodes names.
1001 llvm::Function::arg_iterator AI = Fn->arg_begin();
1002
1003 // Name the struct return argument.
Daniel Dunbara72d4ae2008-09-10 02:41:04 +00001004 if (CGM.ReturnTypeUsesSret(RetTy)) {
Daniel Dunbar613855c2008-09-09 23:27:19 +00001005 AI->setName("agg.result");
1006 ++AI;
1007 }
1008
1009 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbar8fc81b02008-09-17 00:51:38 +00001010 i != e; ++i) {
Daniel Dunbar613855c2008-09-09 23:27:19 +00001011 const VarDecl *Arg = i->first;
Daniel Dunbar8fc81b02008-09-17 00:51:38 +00001012 QualType Ty = i->second;
Daniel Dunbard9eff3d2008-10-13 17:02:26 +00001013 ABIArgInfo ArgI = getABIArgumentInfo(Ty, CGM.getTypes());
Daniel Dunbard3674e62008-09-11 01:48:57 +00001014
1015 switch (ArgI.getKind()) {
1016 case ABIArgInfo::ByVal:
1017 case ABIArgInfo::Default: {
1018 assert(AI != Fn->arg_end() && "Argument mismatch!");
1019 llvm::Value* V = AI;
Daniel Dunbar8fc81b02008-09-17 00:51:38 +00001020 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
Daniel Dunbard3674e62008-09-11 01:48:57 +00001021 // This must be a promotion, for something like
1022 // "void a(x) short x; {..."
Daniel Dunbar8fc81b02008-09-17 00:51:38 +00001023 V = EmitScalarConversion(V, Ty, Arg->getType());
Daniel Dunbar613855c2008-09-09 23:27:19 +00001024 }
Daniel Dunbard3674e62008-09-11 01:48:57 +00001025 EmitParmDecl(*Arg, V);
1026 break;
1027 }
Daniel Dunbar8fc81b02008-09-17 00:51:38 +00001028
1029 case ABIArgInfo::Expand: {
1030 // If this was structure was expand into multiple arguments then
1031 // we need to create a temporary and reconstruct it from the
1032 // arguments.
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001033 std::string Name = Arg->getNameAsString();
Daniel Dunbar8fc81b02008-09-17 00:51:38 +00001034 llvm::Value *Temp = CreateTempAlloca(ConvertType(Ty),
1035 (Name + ".addr").c_str());
1036 // FIXME: What are the right qualifiers here?
1037 llvm::Function::arg_iterator End =
1038 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI);
1039 EmitParmDecl(*Arg, Temp);
Daniel Dunbard3674e62008-09-11 01:48:57 +00001040
Daniel Dunbar8fc81b02008-09-17 00:51:38 +00001041 // Name the arguments used in expansion and increment AI.
1042 unsigned Index = 0;
1043 for (; AI != End; ++AI, ++Index)
1044 AI->setName(Name + "." + llvm::utostr(Index));
1045 continue;
1046 }
Daniel Dunbar94a6f252009-01-26 21:26:08 +00001047
1048 case ABIArgInfo::Ignore:
1049 break;
1050
Daniel Dunbard3674e62008-09-11 01:48:57 +00001051 case ABIArgInfo::Coerce:
Daniel Dunbard3674e62008-09-11 01:48:57 +00001052 case ABIArgInfo::StructRet:
1053 assert(0 && "Invalid ABI kind for non-return argument");
1054 }
Daniel Dunbar8fc81b02008-09-17 00:51:38 +00001055
1056 ++AI;
Daniel Dunbar613855c2008-09-09 23:27:19 +00001057 }
1058 assert(AI == Fn->arg_end() && "Argument mismatch!");
1059}
1060
Daniel Dunbar0f4aa3c2009-01-27 01:36:03 +00001061/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
1062/// a pointer to an object of type \arg Ty.
1063///
1064/// This safely handles the case when the src type is smaller than the
1065/// destination type; in this situation the values of bits which not
1066/// present in the src are undefined.
1067static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
1068 const llvm::Type *Ty,
1069 CodeGenFunction &CGF) {
1070 const llvm::Type *SrcTy =
1071 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
1072 unsigned SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
1073 unsigned DstSize = CGF.CGM.getTargetData().getTypePaddedSize(Ty);
1074
1075 // If load is legal, just bitcase the src pointer.
1076 if (SrcSize == DstSize) {
1077 llvm::Value *Casted =
1078 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
1079 return CGF.Builder.CreateLoad(Casted);
1080 } else {
1081 assert(SrcSize < DstSize && "Coercion is losing source bits!");
1082
1083 // Otherwise do coercion through memory. This is stupid, but
1084 // simple.
1085 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
1086 llvm::Value *Casted =
1087 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
1088 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
1089 return CGF.Builder.CreateLoad(Tmp);
1090 }
1091}
1092
1093/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
1094/// where the source and destination may have different types.
1095///
1096/// This safely handles the case when the src type is larger than the
1097/// destination type; the upper bits of the src will be lost.
1098static void CreateCoercedStore(llvm::Value *Src,
1099 llvm::Value *DstPtr,
1100 CodeGenFunction &CGF) {
1101 const llvm::Type *SrcTy = Src->getType();
1102 const llvm::Type *DstTy =
1103 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
1104
1105 unsigned SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
1106 unsigned DstSize = CGF.CGM.getTargetData().getTypePaddedSize(DstTy);
1107
1108 // If store is legal, just bitcase the src pointer.
1109 if (SrcSize == DstSize) {
1110 llvm::Value *Casted =
1111 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
1112 CGF.Builder.CreateStore(Src, Casted);
1113 } else {
1114 assert(SrcSize > DstSize && "Coercion is missing bits!");
1115
1116 // Otherwise do coercion through memory. This is stupid, but
1117 // simple.
1118 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
1119 CGF.Builder.CreateStore(Src, Tmp);
1120 llvm::Value *Casted =
1121 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
1122 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(Casted), DstPtr);
1123 }
1124}
1125
Daniel Dunbar613855c2008-09-09 23:27:19 +00001126void CodeGenFunction::EmitFunctionEpilog(QualType RetTy,
1127 llvm::Value *ReturnValue) {
Daniel Dunbara72d4ae2008-09-10 02:41:04 +00001128 llvm::Value *RV = 0;
1129
1130 // Functions with no result always return void.
1131 if (ReturnValue) {
Daniel Dunbard9eff3d2008-10-13 17:02:26 +00001132 ABIArgInfo RetAI = getABIReturnInfo(RetTy, CGM.getTypes());
Daniel Dunbara72d4ae2008-09-10 02:41:04 +00001133
1134 switch (RetAI.getKind()) {
1135 case ABIArgInfo::StructRet:
Daniel Dunbar9ae0afd2008-12-18 04:52:14 +00001136 if (RetTy->isAnyComplexType()) {
1137 // FIXME: Volatile
1138 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
1139 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
1140 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1141 EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy);
1142 } else {
1143 Builder.CreateStore(Builder.CreateLoad(ReturnValue),
1144 CurFn->arg_begin());
1145 }
Daniel Dunbara72d4ae2008-09-10 02:41:04 +00001146 break;
Daniel Dunbard3674e62008-09-11 01:48:57 +00001147
Daniel Dunbara72d4ae2008-09-10 02:41:04 +00001148 case ABIArgInfo::Default:
1149 RV = Builder.CreateLoad(ReturnValue);
1150 break;
1151
Daniel Dunbar94a6f252009-01-26 21:26:08 +00001152 case ABIArgInfo::Ignore:
1153 break;
1154
Daniel Dunbar573884e2008-09-10 07:04:09 +00001155 case ABIArgInfo::Coerce: {
Daniel Dunbar0f4aa3c2009-01-27 01:36:03 +00001156 RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
Daniel Dunbard3674e62008-09-11 01:48:57 +00001157 break;
Daniel Dunbar573884e2008-09-10 07:04:09 +00001158 }
Daniel Dunbard3674e62008-09-11 01:48:57 +00001159
1160 case ABIArgInfo::ByVal:
1161 case ABIArgInfo::Expand:
1162 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar613855c2008-09-09 23:27:19 +00001163 }
1164 }
Daniel Dunbara72d4ae2008-09-10 02:41:04 +00001165
1166 if (RV) {
1167 Builder.CreateRet(RV);
1168 } else {
1169 Builder.CreateRetVoid();
1170 }
Daniel Dunbar613855c2008-09-09 23:27:19 +00001171}
1172
1173RValue CodeGenFunction::EmitCall(llvm::Value *Callee,
Daniel Dunbara72d4ae2008-09-10 02:41:04 +00001174 QualType RetTy,
Daniel Dunbar613855c2008-09-09 23:27:19 +00001175 const CallArgList &CallArgs) {
Daniel Dunbar613855c2008-09-09 23:27:19 +00001176 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbar613855c2008-09-09 23:27:19 +00001177
1178 // Handle struct-return functions by passing a pointer to the
1179 // location that we would like to return into.
Daniel Dunbard9eff3d2008-10-13 17:02:26 +00001180 ABIArgInfo RetAI = getABIReturnInfo(RetTy, CGM.getTypes());
Daniel Dunbara72d4ae2008-09-10 02:41:04 +00001181 switch (RetAI.getKind()) {
1182 case ABIArgInfo::StructRet:
Daniel Dunbar613855c2008-09-09 23:27:19 +00001183 // Create a temporary alloca to hold the result of the call. :(
Daniel Dunbar8fc81b02008-09-17 00:51:38 +00001184 Args.push_back(CreateTempAlloca(ConvertType(RetTy)));
Daniel Dunbara72d4ae2008-09-10 02:41:04 +00001185 break;
1186
1187 case ABIArgInfo::Default:
Daniel Dunbar94a6f252009-01-26 21:26:08 +00001188 case ABIArgInfo::Ignore:
Daniel Dunbara72d4ae2008-09-10 02:41:04 +00001189 case ABIArgInfo::Coerce:
Daniel Dunbara72d4ae2008-09-10 02:41:04 +00001190 break;
Daniel Dunbard3674e62008-09-11 01:48:57 +00001191
1192 case ABIArgInfo::ByVal:
1193 case ABIArgInfo::Expand:
1194 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar613855c2008-09-09 23:27:19 +00001195 }
1196
1197 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
1198 I != E; ++I) {
Daniel Dunbard9eff3d2008-10-13 17:02:26 +00001199 ABIArgInfo ArgInfo = getABIArgumentInfo(I->second, CGM.getTypes());
Daniel Dunbar613855c2008-09-09 23:27:19 +00001200 RValue RV = I->first;
Daniel Dunbar8fc81b02008-09-17 00:51:38 +00001201
1202 switch (ArgInfo.getKind()) {
1203 case ABIArgInfo::ByVal: // Default is byval
1204 case ABIArgInfo::Default:
1205 if (RV.isScalar()) {
1206 Args.push_back(RV.getScalarVal());
1207 } else if (RV.isComplex()) {
1208 // Make a temporary alloca to pass the argument.
1209 Args.push_back(CreateTempAlloca(ConvertType(I->second)));
1210 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
1211 } else {
1212 Args.push_back(RV.getAggregateAddr());
1213 }
1214 break;
1215
Daniel Dunbar94a6f252009-01-26 21:26:08 +00001216 case ABIArgInfo::Ignore:
1217 break;
1218
Daniel Dunbar8fc81b02008-09-17 00:51:38 +00001219 case ABIArgInfo::StructRet:
1220 case ABIArgInfo::Coerce:
1221 assert(0 && "Invalid ABI kind for non-return argument");
1222 break;
1223
1224 case ABIArgInfo::Expand:
1225 ExpandTypeToArgs(I->second, RV, Args);
1226 break;
Daniel Dunbar613855c2008-09-09 23:27:19 +00001227 }
1228 }
1229
1230 llvm::CallInst *CI = Builder.CreateCall(Callee,&Args[0],&Args[0]+Args.size());
Daniel Dunbara72d4ae2008-09-10 02:41:04 +00001231 CGCallInfo CallInfo(RetTy, CallArgs);
Daniel Dunbar613855c2008-09-09 23:27:19 +00001232
Daniel Dunbar76c8eb72008-09-10 00:32:18 +00001233 // FIXME: Provide TargetDecl so nounwind, noreturn, etc, etc get set.
Devang Patel322300d2008-09-25 21:02:23 +00001234 CodeGen::AttributeListType AttributeList;
1235 CGM.ConstructAttributeList(0,
Daniel Dunbarc68897d2008-09-10 00:41:16 +00001236 CallInfo.argtypes_begin(), CallInfo.argtypes_end(),
Devang Patel322300d2008-09-25 21:02:23 +00001237 AttributeList);
1238 CI->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(),
1239 AttributeList.size()));
Daniel Dunbar613855c2008-09-09 23:27:19 +00001240
1241 if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee))
1242 CI->setCallingConv(F->getCallingConv());
1243 if (CI->getType() != llvm::Type::VoidTy)
1244 CI->setName("call");
Daniel Dunbara72d4ae2008-09-10 02:41:04 +00001245
1246 switch (RetAI.getKind()) {
1247 case ABIArgInfo::StructRet:
1248 if (RetTy->isAnyComplexType())
Daniel Dunbar8fc81b02008-09-17 00:51:38 +00001249 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Daniel Dunbar9ae0afd2008-12-18 04:52:14 +00001250 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar8fc81b02008-09-17 00:51:38 +00001251 return RValue::getAggregate(Args[0]);
Daniel Dunbar9ae0afd2008-12-18 04:52:14 +00001252 else
1253 return RValue::get(Builder.CreateLoad(Args[0]));
Daniel Dunbard3674e62008-09-11 01:48:57 +00001254
Daniel Dunbara72d4ae2008-09-10 02:41:04 +00001255 case ABIArgInfo::Default:
1256 return RValue::get(RetTy->isVoidType() ? 0 : CI);
1257
Daniel Dunbar94a6f252009-01-26 21:26:08 +00001258 case ABIArgInfo::Ignore:
Daniel Dunbar012468a2009-01-29 08:24:57 +00001259 if (RetTy->isVoidType())
1260 return RValue::get(0);
1261 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1262 llvm::Value *Res =
1263 llvm::UndefValue::get(llvm::PointerType::getUnqual(ConvertType(RetTy)));
1264 return RValue::getAggregate(Res);
1265 }
1266 return RValue::get(llvm::UndefValue::get(ConvertType(RetTy)));
Daniel Dunbar94a6f252009-01-26 21:26:08 +00001267
Daniel Dunbar573884e2008-09-10 07:04:09 +00001268 case ABIArgInfo::Coerce: {
Daniel Dunbar0f4aa3c2009-01-27 01:36:03 +00001269 llvm::Value *V = CreateTempAlloca(ConvertType(RetTy), "coerce");
1270 CreateCoercedStore(CI, V, *this);
Anders Carlsson32ef8ce2008-11-25 22:21:48 +00001271 if (RetTy->isAnyComplexType())
1272 return RValue::getComplex(LoadComplexFromAddr(V, false));
Daniel Dunbar94a6f252009-01-26 21:26:08 +00001273 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlsson32ef8ce2008-11-25 22:21:48 +00001274 return RValue::getAggregate(V);
Daniel Dunbar94a6f252009-01-26 21:26:08 +00001275 else
1276 return RValue::get(Builder.CreateLoad(V));
Daniel Dunbar573884e2008-09-10 07:04:09 +00001277 }
Daniel Dunbard3674e62008-09-11 01:48:57 +00001278
1279 case ABIArgInfo::ByVal:
1280 case ABIArgInfo::Expand:
1281 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar613855c2008-09-09 23:27:19 +00001282 }
Daniel Dunbara72d4ae2008-09-10 02:41:04 +00001283
1284 assert(0 && "Unhandled ABIArgInfo::Kind");
1285 return RValue::get(0);
Daniel Dunbar613855c2008-09-09 23:27:19 +00001286}