blob: 65d70385f08936e712ef8c96df210eefb050dad8 [file] [log] [blame]
Daniel Dunbar0dbe2272008-09-08 21:33:45 +00001//===----- CGCall.h - Encapsulate calling convention details ----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// These classes wrap the information about a call or function
11// definition used to handle ABI compliancy.
12//
13//===----------------------------------------------------------------------===//
14
15#include "CGCall.h"
16#include "CodeGenFunction.h"
Daniel Dunbarb7688072008-09-10 00:41:16 +000017#include "CodeGenModule.h"
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +000018#include "clang/Basic/TargetInfo.h"
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000019#include "clang/AST/ASTContext.h"
20#include "clang/AST/Decl.h"
21#include "clang/AST/DeclObjC.h"
Daniel Dunbar99037e52009-01-29 08:13:58 +000022#include "clang/AST/RecordLayout.h"
Daniel Dunbar56273772008-09-17 00:51:38 +000023#include "llvm/ADT/StringExtras.h"
Devang Pateld0646bd2008-09-24 01:01:36 +000024#include "llvm/Attributes.h"
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +000025#include "llvm/Support/CommandLine.h"
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +000026#include "llvm/Target/TargetData.h"
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000027using namespace clang;
28using namespace CodeGen;
29
Daniel Dunbar6f3e7fa2009-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 Dunbar0dbe2272008-09-08 21:33:45 +000035/***/
36
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000037// FIXME: Use iterator and sidestep silly type array creation.
38
Daniel Dunbar45c25ba2008-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 Dunbar0dbe2272008-09-08 21:33:45 +000054CGFunctionInfo::CGFunctionInfo(const FunctionDecl *FD)
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000055{
56 const FunctionType *FTy = FD->getType()->getAsFunctionType();
57 const FunctionTypeProto *FTP = dyn_cast<FunctionTypeProto>(FTy);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000058
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000059 ArgTypes.push_back(FTy->getResultType());
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000060 if (FTP) {
61 IsVariadic = FTP->isVariadic();
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000062 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
63 ArgTypes.push_back(FTP->getArgType(i));
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000064 } else {
65 IsVariadic = true;
66 }
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000067}
68
69CGFunctionInfo::CGFunctionInfo(const ObjCMethodDecl *MD,
70 const ASTContext &Context)
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000071 : IsVariadic(MD->isVariadic())
Daniel Dunbar0dbe2272008-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 Dunbar5323a4b2008-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 Dunbar0dbe2272008-09-08 21:33:45 +000087}
88
89/***/
90
Daniel Dunbar5323a4b2008-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 Dunbar0dbe2272008-09-08 21:33:45 +000094 ArgTypes.push_back(i->second);
95}
96
Daniel Dunbar5323a4b2008-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 Dunbar0dbe2272008-09-08 21:33:45 +0000103}
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000104
105/***/
106
Daniel Dunbar8951dbd2008-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 Dunbar2c8e0f32008-09-10 02:41:04 +0000109class ABIArgInfo {
110public:
111 enum Kind {
112 Default,
Daniel Dunbar3aea8ca2008-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 Dunbar8951dbd2008-09-11 01:48:57 +0000117
Daniel Dunbar11434922009-01-26 21:26:08 +0000118 Ignore, /// Ignore the argument (treat as void). Useful for
119 /// void and empty structs.
120
Daniel Dunbar56273772008-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 Dunbar8951dbd2008-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 Dunbar56273772008-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 Dunbar8951dbd2008-09-11 01:48:57 +0000135
136 KindFirst=Default, KindLast=Expand
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000137 };
138
139private:
140 Kind TheKind;
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000141 const llvm::Type *TypeData;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000142 unsigned UIntData;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000143
Daniel Dunbar8951dbd2008-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 Dunbar2c8e0f32008-09-10 02:41:04 +0000148public:
149 static ABIArgInfo getDefault() {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000150 return ABIArgInfo(Default);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000151 }
152 static ABIArgInfo getStructRet() {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000153 return ABIArgInfo(StructRet);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000154 }
Daniel Dunbar11434922009-01-26 21:26:08 +0000155 static ABIArgInfo getIgnore() {
156 return ABIArgInfo(Ignore);
157 }
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000158 static ABIArgInfo getCoerce(const llvm::Type *T) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000159 return ABIArgInfo(Coerce, T);
160 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000161 static ABIArgInfo getByVal(unsigned Alignment) {
162 return ABIArgInfo(ByVal, 0, Alignment);
163 }
Daniel Dunbar56273772008-09-17 00:51:38 +0000164 static ABIArgInfo getExpand() {
165 return ABIArgInfo(Expand);
166 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000167
168 Kind getKind() const { return TheKind; }
169 bool isDefault() const { return TheKind == Default; }
170 bool isStructRet() const { return TheKind == StructRet; }
Daniel Dunbar11434922009-01-26 21:26:08 +0000171 bool isIgnore() const { return TheKind == Ignore; }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000172 bool isCoerce() const { return TheKind == Coerce; }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000173 bool isByVal() const { return TheKind == ByVal; }
Daniel Dunbar56273772008-09-17 00:51:38 +0000174 bool isExpand() const { return TheKind == Expand; }
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000175
176 // Coerce accessors
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000177 const llvm::Type *getCoerceToType() const {
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000178 assert(TheKind == Coerce && "Invalid kind!");
179 return TypeData;
180 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000181
182 // ByVal accessors
183 unsigned getByValAlignment() const {
184 assert(TheKind == ByVal && "Invalid kind!");
185 return UIntData;
186 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000187};
188
189/***/
190
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000191/* FIXME: All of this stuff should be part of the target interface
192 somehow. It is currently here because it is not clear how to factor
193 the targets to support this, since the Targets currently live in a
194 layer below types n'stuff.
195 */
196
197/// ABIInfo - Target specific hooks for defining how a type should be
198/// passed or returned from functions.
199class clang::ABIInfo {
200public:
201 virtual ~ABIInfo();
202
203 virtual ABIArgInfo classifyReturnType(QualType RetTy,
204 ASTContext &Context) const = 0;
205
206 virtual ABIArgInfo classifyArgumentType(QualType Ty,
207 ASTContext &Context) const = 0;
208};
209
210ABIInfo::~ABIInfo() {}
211
Daniel Dunbar834af452008-09-17 21:22:33 +0000212/// isEmptyStruct - Return true iff a structure has no non-empty
213/// members. Note that a structure with a flexible array member is not
214/// considered empty.
215static bool isEmptyStruct(QualType T) {
216 const RecordType *RT = T->getAsStructureType();
217 if (!RT)
218 return 0;
219 const RecordDecl *RD = RT->getDecl();
220 if (RD->hasFlexibleArrayMember())
221 return false;
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000222 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar834af452008-09-17 21:22:33 +0000223 e = RD->field_end(); i != e; ++i) {
224 const FieldDecl *FD = *i;
225 if (!isEmptyStruct(FD->getType()))
226 return false;
227 }
228 return true;
229}
230
231/// isSingleElementStruct - Determine if a structure is a "single
232/// element struct", i.e. it has exactly one non-empty field or
233/// exactly one field which is itself a single element
234/// struct. Structures with flexible array members are never
235/// considered single element structs.
236///
237/// \return The field declaration for the single non-empty field, if
238/// it exists.
239static const FieldDecl *isSingleElementStruct(QualType T) {
240 const RecordType *RT = T->getAsStructureType();
241 if (!RT)
242 return 0;
243
244 const RecordDecl *RD = RT->getDecl();
245 if (RD->hasFlexibleArrayMember())
246 return 0;
247
248 const FieldDecl *Found = 0;
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000249 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar834af452008-09-17 21:22:33 +0000250 e = RD->field_end(); i != e; ++i) {
251 const FieldDecl *FD = *i;
252 QualType FT = FD->getType();
253
254 if (isEmptyStruct(FT)) {
255 // Ignore
256 } else if (Found) {
257 return 0;
258 } else if (!CodeGenFunction::hasAggregateLLVMType(FT)) {
259 Found = FD;
260 } else {
261 Found = isSingleElementStruct(FT);
262 if (!Found)
263 return 0;
264 }
265 }
266
267 return Found;
268}
269
270static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
271 if (!Ty->getAsBuiltinType() && !Ty->isPointerType())
272 return false;
273
274 uint64_t Size = Context.getTypeSize(Ty);
275 return Size == 32 || Size == 64;
276}
277
278static bool areAllFields32Or64BitBasicType(const RecordDecl *RD,
279 ASTContext &Context) {
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000280 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar834af452008-09-17 21:22:33 +0000281 e = RD->field_end(); i != e; ++i) {
282 const FieldDecl *FD = *i;
283
284 if (!is32Or64BitBasicType(FD->getType(), Context))
285 return false;
286
287 // If this is a bit-field we need to make sure it is still a
288 // 32-bit or 64-bit type.
289 if (Expr *BW = FD->getBitWidth()) {
290 unsigned Width = BW->getIntegerConstantExprValue(Context).getZExtValue();
291 if (Width <= 16)
292 return false;
293 }
294 }
295 return true;
296}
297
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000298namespace {
299/// DefaultABIInfo - The default implementation for ABI specific
300/// details. This implementation provides information which results in
301/// sensible LLVM IR generation, but does not conform to any
302/// particular ABI.
303class DefaultABIInfo : public ABIInfo {
304 virtual ABIArgInfo classifyReturnType(QualType RetTy,
305 ASTContext &Context) const;
306
307 virtual ABIArgInfo classifyArgumentType(QualType RetTy,
308 ASTContext &Context) const;
309};
310
311/// X86_32ABIInfo - The X86-32 ABI information.
312class X86_32ABIInfo : public ABIInfo {
313public:
314 virtual ABIArgInfo classifyReturnType(QualType RetTy,
315 ASTContext &Context) const;
316
317 virtual ABIArgInfo classifyArgumentType(QualType RetTy,
318 ASTContext &Context) const;
319};
320}
321
322ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
323 ASTContext &Context) const {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000324 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar834af452008-09-17 21:22:33 +0000325 // Classify "single element" structs as their element type.
326 const FieldDecl *SeltFD = isSingleElementStruct(RetTy);
327 if (SeltFD) {
328 QualType SeltTy = SeltFD->getType()->getDesugaredType();
329 if (const BuiltinType *BT = SeltTy->getAsBuiltinType()) {
330 // FIXME: This is gross, it would be nice if we could just
331 // pass back SeltTy and have clients deal with it. Is it worth
332 // supporting coerce to both LLVM and clang Types?
333 if (BT->isIntegerType()) {
334 uint64_t Size = Context.getTypeSize(SeltTy);
335 return ABIArgInfo::getCoerce(llvm::IntegerType::get((unsigned) Size));
336 } else if (BT->getKind() == BuiltinType::Float) {
337 return ABIArgInfo::getCoerce(llvm::Type::FloatTy);
338 } else if (BT->getKind() == BuiltinType::Double) {
339 return ABIArgInfo::getCoerce(llvm::Type::DoubleTy);
340 }
341 } else if (SeltTy->isPointerType()) {
342 // FIXME: It would be really nice if this could come out as
343 // the proper pointer type.
344 llvm::Type *PtrTy =
345 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
346 return ABIArgInfo::getCoerce(PtrTy);
347 }
348 }
349
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000350 uint64_t Size = Context.getTypeSize(RetTy);
351 if (Size == 8) {
352 return ABIArgInfo::getCoerce(llvm::Type::Int8Ty);
353 } else if (Size == 16) {
354 return ABIArgInfo::getCoerce(llvm::Type::Int16Ty);
355 } else if (Size == 32) {
356 return ABIArgInfo::getCoerce(llvm::Type::Int32Ty);
357 } else if (Size == 64) {
358 return ABIArgInfo::getCoerce(llvm::Type::Int64Ty);
359 } else {
360 return ABIArgInfo::getStructRet();
361 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000362 } else {
363 return ABIArgInfo::getDefault();
364 }
365}
366
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000367ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
368 ASTContext &Context) const {
Daniel Dunbarf0357382008-09-17 20:11:04 +0000369 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar834af452008-09-17 21:22:33 +0000370 // Structures with flexible arrays are always byval.
371 if (const RecordType *RT = Ty->getAsStructureType())
372 if (RT->getDecl()->hasFlexibleArrayMember())
373 return ABIArgInfo::getByVal(0);
374
375 // Expand empty structs (i.e. ignore)
376 uint64_t Size = Context.getTypeSize(Ty);
377 if (Ty->isStructureType() && Size == 0)
378 return ABIArgInfo::getExpand();
379
380 // Expand structs with size <= 128-bits which consist only of
381 // basic types (int, long long, float, double, xxx*). This is
382 // non-recursive and does not ignore empty fields.
383 if (const RecordType *RT = Ty->getAsStructureType()) {
384 if (Context.getTypeSize(Ty) <= 4*32 &&
385 areAllFields32Or64BitBasicType(RT->getDecl(), Context))
386 return ABIArgInfo::getExpand();
387 }
388
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000389 return ABIArgInfo::getByVal(0);
390 } else {
391 return ABIArgInfo::getDefault();
392 }
393}
394
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000395namespace {
396/// X86_32ABIInfo - The X86_64 ABI information.
397class X86_64ABIInfo : public ABIInfo {
398 enum Class {
399 Integer = 0,
400 SSE,
401 SSEUp,
402 X87,
403 X87Up,
404 ComplexX87,
405 NoClass,
406 Memory
407 };
408
409 /// classify - Determine the x86_64 register classes in which the
410 /// given type T should be passed.
411 ///
412 /// \param Lo - The classification for the low word of the type.
413 /// \param Hi - The classification for the high word of the type.
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000414 /// \param OffsetBase - The byte position of the type in the root
415 /// structure. Some parameters are classified different depending on
416 /// whether they straddle an eightbyte boundary.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000417 ///
418 /// If a word is unused its result will be NoClass; if a type should
419 /// be passed in Memory then at least the classification of \arg Lo
420 /// will be Memory.
421 ///
422 /// The \arg Lo class will be NoClass iff the argument is ignored.
423 ///
424 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
425 /// be NoClass.
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000426 void classify(QualType T, ASTContext &Context, uint64_t OffsetBase,
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000427 Class &Lo, Class &Hi) const;
428
429public:
430 virtual ABIArgInfo classifyReturnType(QualType RetTy,
431 ASTContext &Context) const;
432
433 virtual ABIArgInfo classifyArgumentType(QualType RetTy,
434 ASTContext &Context) const;
435};
436}
437
438void X86_64ABIInfo::classify(QualType Ty,
439 ASTContext &Context,
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000440 uint64_t OffsetBase,
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000441 Class &Lo, Class &Hi) const {
442 Lo = Memory;
443 Hi = NoClass;
444 if (const BuiltinType *BT = Ty->getAsBuiltinType()) {
445 BuiltinType::Kind k = BT->getKind();
446
Daniel Dunbar11434922009-01-26 21:26:08 +0000447 if (k == BuiltinType::Void) {
448 Lo = NoClass;
449 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000450 Lo = Integer;
451 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
452 Lo = SSE;
453 } else if (k == BuiltinType::LongDouble) {
454 Lo = X87;
455 Hi = X87Up;
456 }
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000457
458 // FIXME: _Decimal32 and _Decimal64 are SSE.
459 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000460 // FIXME: __int128 is (Integer, Integer).
461 } else if (Ty->isPointerLikeType() || Ty->isBlockPointerType() ||
462 Ty->isObjCQualifiedInterfaceType()) {
463 Lo = Integer;
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000464 } else if (const VectorType *VT = Ty->getAsVectorType()) {
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000465 uint64_t Size = Context.getTypeSize(VT);
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000466 if (Size == 64) {
467 // FIXME: For some reason, gcc appears to be treating <1 x
468 // double> as INTEGER; this seems wrong, but we will match for
469 // now (icc rejects <1 x double>, so...).
470 Lo = (VT->getElementType() == Context.DoubleTy) ? Integer : SSE;
471 } else if (Size == 128) {
472 Lo = SSE;
473 Hi = SSEUp;
474 }
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000475 } else if (const ComplexType *CT = Ty->getAsComplexType()) {
476 QualType ET = CT->getElementType();
477
Daniel Dunbareac48dc2009-01-29 07:22:20 +0000478 if (ET->isIntegerType()) {
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000479 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbareac48dc2009-01-29 07:22:20 +0000480 if (Size <= 64)
481 Lo = Integer;
482 else if (Size <= 128)
483 Lo = Hi = Integer;
484 } else if (ET == Context.FloatTy)
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000485 Lo = SSE;
486 else if (ET == Context.DoubleTy)
487 Lo = Hi = SSE;
488 else if (ET == Context.LongDoubleTy)
489 Lo = ComplexX87;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000490
491 // If this complex type crosses an eightbyte boundary then it
492 // should be split.
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000493 uint64_t EB_Real = (OffsetBase) >> 3;
494 uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) >> 3;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000495 if (Hi == NoClass && EB_Real != EB_Imag)
496 Hi = Lo;
Daniel Dunbar99037e52009-01-29 08:13:58 +0000497 } else if (const RecordType *RT = Ty->getAsRecordType()) {
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000498 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbar99037e52009-01-29 08:13:58 +0000499
500 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
501 // than two eightbytes, ..., it has class MEMORY.
502 if (Size > 128)
503 return;
504
505 const RecordDecl *RD = RT->getDecl();
506
507 // Assume variable sized types are passed in memory.
508 if (RD->hasFlexibleArrayMember())
509 return;
510
511 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
512
513 // Reset Lo class, this will be recomputed.
514 Lo = NoClass;
515 unsigned idx = 0;
516 for (RecordDecl::field_iterator i = RD->field_begin(),
517 e = RD->field_end(); i != e; ++i, ++idx) {
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000518 unsigned Offset = OffsetBase + Layout.getFieldOffset(idx);
Daniel Dunbar99037e52009-01-29 08:13:58 +0000519
520 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
521 // fields, it has class MEMORY.
522 if (Offset % Context.getTypeAlign(i->getType())) {
523 Lo = Memory;
524 return;
525 }
526
527 // Determine which half of the structure we are classifying.
528 //
529 // AMD64-ABI 3.2.3p2: Rule 3. f the size of the aggregate
530 // exceeds a single eightbyte, each is classified
531 // separately. Each eightbyte gets initialized to class
532 // NO_CLASS.
533 Class &Target = Offset < 64 ? Lo : Hi;
534
535 // Classify this field.
536 Class FieldLo, FieldHi;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000537 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbar99037e52009-01-29 08:13:58 +0000538
539 // Merge the lo field classifcation.
540 //
541 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
542 // classified recursively so that always two fields are
543 // considered. The resulting class is calculated according to
544 // the classes of the fields in the eightbyte:
545 //
546 // (a) If both classes are equal, this is the resulting class.
547 //
548 // (b) If one of the classes is NO_CLASS, the resulting class is
549 // the other class.
550 //
551 // (c) If one of the classes is MEMORY, the result is the MEMORY
552 // class.
553 //
554 // (d) If one of the classes is INTEGER, the result is the
555 // INTEGER.
556 //
557 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
558 // MEMORY is used as class.
559 //
560 // (f) Otherwise class SSE is used.
561 if (Target == FieldLo || FieldLo == NoClass) ;
Daniel Dunbar99037e52009-01-29 08:13:58 +0000562 else if (FieldLo == Memory) {
563 // Memory is never over-ridden, just bail.
564 Lo = Memory;
565 return;
566 }
Daniel Dunbar7f102f82009-01-29 08:35:40 +0000567 else if (Target == NoClass)
568 Target = FieldLo;
Daniel Dunbar99037e52009-01-29 08:13:58 +0000569 else if (Target == Integer || FieldLo == Integer)
570 Target = Integer;
571 else if (FieldLo == X87 || FieldLo == X87Up || FieldLo == ComplexX87) {
572 // As before, just bail once we generate a memory class.
573 Lo = Memory;
574 return;
575 } else
576 Target = SSE;
577
578 // It isn't clear from the ABI spec what the role of the high
579 // classification is here, but since this should only happen
580 // when we have a struct with a two eightbyte member, we can
581 // just push the field high class into the overall high class.
582 if (FieldHi != NoClass)
583 Hi = FieldHi;
584 }
585
586 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
587 //
588 // (a) If one of the classes is MEMORY, the whole argument is
589 // passed in memory.
590 //
591 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
592
593 // The first of these conditions is guaranteed by how we implement
594 // the merge (just bail). I don't believe the second is actually
595 // possible at all.
596 assert(Lo != Memory && "Unexpected memory classification.");
597 if (Hi == SSEUp && Lo != SSE)
598 Hi = SSE;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000599 }
600}
601
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000602ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
603 ASTContext &Context) const {
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000604 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
605 // classification algorithm.
606 X86_64ABIInfo::Class Lo, Hi;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000607 classify(RetTy, Context, 0, Lo, Hi);
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000608
609 const llvm::Type *ResType = 0;
610 switch (Lo) {
611 case NoClass:
Daniel Dunbar11434922009-01-26 21:26:08 +0000612 return ABIArgInfo::getIgnore();
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000613
614 case SSEUp:
615 case X87Up:
616 assert(0 && "Invalid classification for lo word.");
617
618 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
619 // hidden argument, i.e. structret.
620 case Memory:
621 return ABIArgInfo::getStructRet();
622
623 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
624 // available register of the sequence %rax, %rdx is used.
625 case Integer:
626 ResType = llvm::Type::Int64Ty; break;
627
628 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
629 // available SSE register of the sequence %xmm0, %xmm1 is used.
630 case SSE:
631 ResType = llvm::Type::DoubleTy; break;
632
633 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
634 // returned on the X87 stack in %st0 as 80-bit x87 number.
635 case X87:
636 ResType = llvm::Type::X86_FP80Ty; break;
637
638 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
639 // part of the value is returned in %st0 and the imaginary part in
640 // %st1.
641 case ComplexX87:
642 assert(Hi == NoClass && "Unexpected ComplexX87 classification.");
643 ResType = llvm::VectorType::get(llvm::Type::X86_FP80Ty, 2);
644 break;
645 }
646
647 switch (Hi) {
648 // Memory was handled previously, and ComplexX87 and X87 should
649 // never occur as hi classes.
650 case Memory:
651 case X87:
652 case ComplexX87:
653 assert(0 && "Invalid classification for hi word.");
654
655 case NoClass: break;
656 case Integer:
Daniel Dunbarb0e14f22009-01-29 07:36:07 +0000657 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
658 break;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000659 case SSE:
Daniel Dunbarb0e14f22009-01-29 07:36:07 +0000660 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
661 break;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000662
663 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
664 // is passed in the upper half of the last used SSE register.
665 //
666 // SSEUP should always be preceeded by SSE, just widen.
667 case SSEUp:
668 assert(Lo == SSE && "Unexpected SSEUp classification.");
669 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
670 break;
671
672 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
Daniel Dunbarb0e14f22009-01-29 07:36:07 +0000673 // returned together with the previous X87 value in %st0.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000674 //
675 // X87UP should always be preceeded by X87, so we don't need to do
676 // anything here.
677 case X87Up:
678 assert(Lo == X87 && "Unexpected X87Up classification.");
679 break;
680 }
681
682 return ABIArgInfo::getCoerce(ResType);
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000683}
684
685ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty,
686 ASTContext &Context) const {
687 return ABIArgInfo::getDefault();
688}
689
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000690ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
691 ASTContext &Context) const {
692 return ABIArgInfo::getDefault();
693}
694
695ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
696 ASTContext &Context) const {
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000697 return ABIArgInfo::getDefault();
698}
699
700const ABIInfo &CodeGenTypes::getABIInfo() const {
701 if (TheABIInfo)
702 return *TheABIInfo;
703
704 // For now we just cache this in the CodeGenTypes and don't bother
705 // to free it.
706 const char *TargetPrefix = getContext().Target.getTargetPrefix();
707 if (strcmp(TargetPrefix, "x86") == 0) {
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000708 switch (getContext().Target.getPointerWidth(0)) {
709 case 32:
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000710 return *(TheABIInfo = new X86_32ABIInfo());
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000711 case 64:
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000712 if (UseX86_64ABI)
713 return *(TheABIInfo = new X86_64ABIInfo());
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000714 }
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000715 }
716
717 return *(TheABIInfo = new DefaultABIInfo);
718}
719
720// getABIReturnInfo - Wrap the ABIInfo getABIReturnInfo, altering
721// "default" types to StructRet when appropriate for simplicity.
722static ABIArgInfo getABIReturnInfo(QualType Ty, CodeGenTypes &CGT) {
723 assert(!Ty->isArrayType() &&
724 "Array types cannot be passed directly.");
725 ABIArgInfo Info = CGT.getABIInfo().classifyReturnType(Ty, CGT.getContext());
Daniel Dunbarf0357382008-09-17 20:11:04 +0000726 // Ensure default on aggregate types is StructRet.
727 if (Info.isDefault() && CodeGenFunction::hasAggregateLLVMType(Ty))
728 return ABIArgInfo::getStructRet();
729 return Info;
730}
731
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000732// getABIArgumentInfo - Wrap the ABIInfo getABIReturnInfo, altering
733// "default" types to ByVal when appropriate for simplicity.
734static ABIArgInfo getABIArgumentInfo(QualType Ty, CodeGenTypes &CGT) {
735 assert(!Ty->isArrayType() &&
736 "Array types cannot be passed directly.");
737 ABIArgInfo Info = CGT.getABIInfo().classifyArgumentType(Ty, CGT.getContext());
Daniel Dunbarf0357382008-09-17 20:11:04 +0000738 // Ensure default on aggregate types is ByVal.
739 if (Info.isDefault() && CodeGenFunction::hasAggregateLLVMType(Ty))
740 return ABIArgInfo::getByVal(0);
741 return Info;
742}
743
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000744/***/
745
Daniel Dunbar56273772008-09-17 00:51:38 +0000746void CodeGenTypes::GetExpandedTypes(QualType Ty,
747 std::vector<const llvm::Type*> &ArgTys) {
748 const RecordType *RT = Ty->getAsStructureType();
749 assert(RT && "Can only expand structure types.");
750 const RecordDecl *RD = RT->getDecl();
751 assert(!RD->hasFlexibleArrayMember() &&
752 "Cannot expand structure with flexible array.");
753
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000754 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar56273772008-09-17 00:51:38 +0000755 e = RD->field_end(); i != e; ++i) {
756 const FieldDecl *FD = *i;
757 assert(!FD->isBitField() &&
758 "Cannot expand structure with bit-field members.");
759
760 QualType FT = FD->getType();
761 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
762 GetExpandedTypes(FT, ArgTys);
763 } else {
764 ArgTys.push_back(ConvertType(FT));
765 }
766 }
767}
768
769llvm::Function::arg_iterator
770CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
771 llvm::Function::arg_iterator AI) {
772 const RecordType *RT = Ty->getAsStructureType();
773 assert(RT && "Can only expand structure types.");
774
775 RecordDecl *RD = RT->getDecl();
776 assert(LV.isSimple() &&
777 "Unexpected non-simple lvalue during struct expansion.");
778 llvm::Value *Addr = LV.getAddress();
779 for (RecordDecl::field_iterator i = RD->field_begin(),
780 e = RD->field_end(); i != e; ++i) {
781 FieldDecl *FD = *i;
782 QualType FT = FD->getType();
783
784 // FIXME: What are the right qualifiers here?
785 LValue LV = EmitLValueForField(Addr, FD, false, 0);
786 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
787 AI = ExpandTypeFromArgs(FT, LV, AI);
788 } else {
789 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
790 ++AI;
791 }
792 }
793
794 return AI;
795}
796
797void
798CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
799 llvm::SmallVector<llvm::Value*, 16> &Args) {
800 const RecordType *RT = Ty->getAsStructureType();
801 assert(RT && "Can only expand structure types.");
802
803 RecordDecl *RD = RT->getDecl();
804 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
805 llvm::Value *Addr = RV.getAggregateAddr();
806 for (RecordDecl::field_iterator i = RD->field_begin(),
807 e = RD->field_end(); i != e; ++i) {
808 FieldDecl *FD = *i;
809 QualType FT = FD->getType();
810
811 // FIXME: What are the right qualifiers here?
812 LValue LV = EmitLValueForField(Addr, FD, false, 0);
813 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
814 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
815 } else {
816 RValue RV = EmitLoadOfLValue(LV, FT);
817 assert(RV.isScalar() &&
818 "Unexpected non-scalar rvalue during struct expansion.");
819 Args.push_back(RV.getScalarVal());
820 }
821 }
822}
823
824/***/
825
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000826const llvm::FunctionType *
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000827CodeGenTypes::GetFunctionType(const CGCallInfo &CI, bool IsVariadic) {
828 return GetFunctionType(CI.argtypes_begin(), CI.argtypes_end(), IsVariadic);
829}
830
831const llvm::FunctionType *
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000832CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI) {
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000833 return GetFunctionType(FI.argtypes_begin(), FI.argtypes_end(), FI.isVariadic());
834}
835
836const llvm::FunctionType *
837CodeGenTypes::GetFunctionType(ArgTypeIterator begin, ArgTypeIterator end,
838 bool IsVariadic) {
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000839 std::vector<const llvm::Type*> ArgTys;
840
841 const llvm::Type *ResultType = 0;
842
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000843 QualType RetTy = *begin;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000844 ABIArgInfo RetAI = getABIReturnInfo(RetTy, *this);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000845 switch (RetAI.getKind()) {
846 case ABIArgInfo::ByVal:
847 case ABIArgInfo::Expand:
848 assert(0 && "Invalid ABI kind for return argument");
849
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000850 case ABIArgInfo::Default:
851 if (RetTy->isVoidType()) {
852 ResultType = llvm::Type::VoidTy;
853 } else {
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000854 ResultType = ConvertType(RetTy);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000855 }
856 break;
857
858 case ABIArgInfo::StructRet: {
859 ResultType = llvm::Type::VoidTy;
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000860 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000861 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
862 break;
863 }
864
Daniel Dunbar11434922009-01-26 21:26:08 +0000865 case ABIArgInfo::Ignore:
866 ResultType = llvm::Type::VoidTy;
867 break;
868
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000869 case ABIArgInfo::Coerce:
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000870 ResultType = RetAI.getCoerceToType();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000871 break;
872 }
873
874 for (++begin; begin != end; ++begin) {
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000875 ABIArgInfo AI = getABIArgumentInfo(*begin, *this);
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000876 const llvm::Type *Ty = ConvertType(*begin);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000877
878 switch (AI.getKind()) {
Daniel Dunbar11434922009-01-26 21:26:08 +0000879 case ABIArgInfo::Ignore:
880 break;
881
Daniel Dunbar56273772008-09-17 00:51:38 +0000882 case ABIArgInfo::Coerce:
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000883 case ABIArgInfo::StructRet:
884 assert(0 && "Invalid ABI kind for non-return argument");
885
886 case ABIArgInfo::ByVal:
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000887 // byval arguments are always on the stack, which is addr space #0.
888 ArgTys.push_back(llvm::PointerType::getUnqual(Ty));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000889 assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled");
890 break;
891
892 case ABIArgInfo::Default:
893 ArgTys.push_back(Ty);
894 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000895
896 case ABIArgInfo::Expand:
Daniel Dunbar56273772008-09-17 00:51:38 +0000897 GetExpandedTypes(*begin, ArgTys);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000898 break;
899 }
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000900 }
901
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000902 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000903}
904
Daniel Dunbarb7688072008-09-10 00:41:16 +0000905bool CodeGenModule::ReturnTypeUsesSret(QualType RetTy) {
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000906 return getABIReturnInfo(RetTy, getTypes()).isStructRet();
Daniel Dunbar3913f182008-09-09 23:48:28 +0000907}
908
Devang Patel761d7f72008-09-25 21:02:23 +0000909void CodeGenModule::ConstructAttributeList(const Decl *TargetDecl,
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000910 ArgTypeIterator begin,
911 ArgTypeIterator end,
Devang Patel761d7f72008-09-25 21:02:23 +0000912 AttributeListType &PAL) {
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000913 unsigned FuncAttrs = 0;
Devang Patela2c69122008-09-26 22:53:57 +0000914 unsigned RetAttrs = 0;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000915
916 if (TargetDecl) {
917 if (TargetDecl->getAttr<NoThrowAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +0000918 FuncAttrs |= llvm::Attribute::NoUnwind;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000919 if (TargetDecl->getAttr<NoReturnAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +0000920 FuncAttrs |= llvm::Attribute::NoReturn;
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000921 if (TargetDecl->getAttr<PureAttr>())
922 FuncAttrs |= llvm::Attribute::ReadOnly;
923 if (TargetDecl->getAttr<ConstAttr>())
924 FuncAttrs |= llvm::Attribute::ReadNone;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000925 }
926
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000927 QualType RetTy = *begin;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000928 unsigned Index = 1;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000929 ABIArgInfo RetAI = getABIReturnInfo(RetTy, getTypes());
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000930 switch (RetAI.getKind()) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000931 case ABIArgInfo::Default:
932 if (RetTy->isPromotableIntegerType()) {
933 if (RetTy->isSignedIntegerType()) {
Devang Patela2c69122008-09-26 22:53:57 +0000934 RetAttrs |= llvm::Attribute::SExt;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000935 } else if (RetTy->isUnsignedIntegerType()) {
Devang Patela2c69122008-09-26 22:53:57 +0000936 RetAttrs |= llvm::Attribute::ZExt;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000937 }
938 }
939 break;
940
941 case ABIArgInfo::StructRet:
Devang Patel761d7f72008-09-25 21:02:23 +0000942 PAL.push_back(llvm::AttributeWithIndex::get(Index,
943 llvm::Attribute::StructRet|
944 llvm::Attribute::NoAlias));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000945 ++Index;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000946 break;
947
Daniel Dunbar11434922009-01-26 21:26:08 +0000948 case ABIArgInfo::Ignore:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000949 case ABIArgInfo::Coerce:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000950 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000951
952 case ABIArgInfo::ByVal:
953 case ABIArgInfo::Expand:
954 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000955 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000956
Devang Patela2c69122008-09-26 22:53:57 +0000957 if (RetAttrs)
958 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Daniel Dunbar56273772008-09-17 00:51:38 +0000959 for (++begin; begin != end; ++begin) {
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000960 QualType ParamType = *begin;
Devang Patel761d7f72008-09-25 21:02:23 +0000961 unsigned Attributes = 0;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000962 ABIArgInfo AI = getABIArgumentInfo(ParamType, getTypes());
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000963
964 switch (AI.getKind()) {
965 case ABIArgInfo::StructRet:
Daniel Dunbar56273772008-09-17 00:51:38 +0000966 case ABIArgInfo::Coerce:
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000967 assert(0 && "Invalid ABI kind for non-return argument");
968
969 case ABIArgInfo::ByVal:
Devang Patel761d7f72008-09-25 21:02:23 +0000970 Attributes |= llvm::Attribute::ByVal;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000971 assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled");
972 break;
973
974 case ABIArgInfo::Default:
975 if (ParamType->isPromotableIntegerType()) {
976 if (ParamType->isSignedIntegerType()) {
Devang Patel761d7f72008-09-25 21:02:23 +0000977 Attributes |= llvm::Attribute::SExt;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000978 } else if (ParamType->isUnsignedIntegerType()) {
Devang Patel761d7f72008-09-25 21:02:23 +0000979 Attributes |= llvm::Attribute::ZExt;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000980 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000981 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000982 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000983
Daniel Dunbar11434922009-01-26 21:26:08 +0000984 case ABIArgInfo::Ignore:
985 // Skip increment, no matching LLVM parameter.
986 continue;
987
Daniel Dunbar56273772008-09-17 00:51:38 +0000988 case ABIArgInfo::Expand: {
989 std::vector<const llvm::Type*> Tys;
990 // FIXME: This is rather inefficient. Do we ever actually need
991 // to do anything here? The result should be just reconstructed
992 // on the other side, so extension should be a non-issue.
993 getTypes().GetExpandedTypes(ParamType, Tys);
994 Index += Tys.size();
995 continue;
996 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000997 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000998
Devang Patel761d7f72008-09-25 21:02:23 +0000999 if (Attributes)
1000 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar56273772008-09-17 00:51:38 +00001001 ++Index;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001002 }
Devang Patela2c69122008-09-26 22:53:57 +00001003 if (FuncAttrs)
1004 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
1005
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001006}
1007
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001008void CodeGenFunction::EmitFunctionProlog(llvm::Function *Fn,
1009 QualType RetTy,
1010 const FunctionArgList &Args) {
1011 // Emit allocs for param decls. Give the LLVM Argument nodes names.
1012 llvm::Function::arg_iterator AI = Fn->arg_begin();
1013
1014 // Name the struct return argument.
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001015 if (CGM.ReturnTypeUsesSret(RetTy)) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001016 AI->setName("agg.result");
1017 ++AI;
1018 }
1019
1020 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbar56273772008-09-17 00:51:38 +00001021 i != e; ++i) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001022 const VarDecl *Arg = i->first;
Daniel Dunbar56273772008-09-17 00:51:38 +00001023 QualType Ty = i->second;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001024 ABIArgInfo ArgI = getABIArgumentInfo(Ty, CGM.getTypes());
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001025
1026 switch (ArgI.getKind()) {
1027 case ABIArgInfo::ByVal:
1028 case ABIArgInfo::Default: {
1029 assert(AI != Fn->arg_end() && "Argument mismatch!");
1030 llvm::Value* V = AI;
Daniel Dunbar56273772008-09-17 00:51:38 +00001031 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001032 // This must be a promotion, for something like
1033 // "void a(x) short x; {..."
Daniel Dunbar56273772008-09-17 00:51:38 +00001034 V = EmitScalarConversion(V, Ty, Arg->getType());
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001035 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001036 EmitParmDecl(*Arg, V);
1037 break;
1038 }
Daniel Dunbar56273772008-09-17 00:51:38 +00001039
1040 case ABIArgInfo::Expand: {
1041 // If this was structure was expand into multiple arguments then
1042 // we need to create a temporary and reconstruct it from the
1043 // arguments.
Chris Lattner39f34e92008-11-24 04:00:27 +00001044 std::string Name = Arg->getNameAsString();
Daniel Dunbar56273772008-09-17 00:51:38 +00001045 llvm::Value *Temp = CreateTempAlloca(ConvertType(Ty),
1046 (Name + ".addr").c_str());
1047 // FIXME: What are the right qualifiers here?
1048 llvm::Function::arg_iterator End =
1049 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI);
1050 EmitParmDecl(*Arg, Temp);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001051
Daniel Dunbar56273772008-09-17 00:51:38 +00001052 // Name the arguments used in expansion and increment AI.
1053 unsigned Index = 0;
1054 for (; AI != End; ++AI, ++Index)
1055 AI->setName(Name + "." + llvm::utostr(Index));
1056 continue;
1057 }
Daniel Dunbar11434922009-01-26 21:26:08 +00001058
1059 case ABIArgInfo::Ignore:
1060 break;
1061
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001062 case ABIArgInfo::Coerce:
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001063 case ABIArgInfo::StructRet:
1064 assert(0 && "Invalid ABI kind for non-return argument");
1065 }
Daniel Dunbar56273772008-09-17 00:51:38 +00001066
1067 ++AI;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001068 }
1069 assert(AI == Fn->arg_end() && "Argument mismatch!");
1070}
1071
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +00001072/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
1073/// a pointer to an object of type \arg Ty.
1074///
1075/// This safely handles the case when the src type is smaller than the
1076/// destination type; in this situation the values of bits which not
1077/// present in the src are undefined.
1078static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
1079 const llvm::Type *Ty,
1080 CodeGenFunction &CGF) {
1081 const llvm::Type *SrcTy =
1082 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
Daniel Dunbare620ecd2009-01-30 00:47:38 +00001083 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
1084 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(Ty);
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +00001085
1086 // If load is legal, just bitcase the src pointer.
1087 if (SrcSize == DstSize) {
1088 llvm::Value *Casted =
1089 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
1090 return CGF.Builder.CreateLoad(Casted);
1091 } else {
1092 assert(SrcSize < DstSize && "Coercion is losing source bits!");
1093
1094 // Otherwise do coercion through memory. This is stupid, but
1095 // simple.
1096 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
1097 llvm::Value *Casted =
1098 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
1099 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
1100 return CGF.Builder.CreateLoad(Tmp);
1101 }
1102}
1103
1104/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
1105/// where the source and destination may have different types.
1106///
1107/// This safely handles the case when the src type is larger than the
1108/// destination type; the upper bits of the src will be lost.
1109static void CreateCoercedStore(llvm::Value *Src,
1110 llvm::Value *DstPtr,
1111 CodeGenFunction &CGF) {
1112 const llvm::Type *SrcTy = Src->getType();
1113 const llvm::Type *DstTy =
1114 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
1115
Daniel Dunbare620ecd2009-01-30 00:47:38 +00001116 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
1117 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(DstTy);
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +00001118
1119 // If store is legal, just bitcase the src pointer.
1120 if (SrcSize == DstSize) {
1121 llvm::Value *Casted =
1122 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
1123 CGF.Builder.CreateStore(Src, Casted);
1124 } else {
1125 assert(SrcSize > DstSize && "Coercion is missing bits!");
1126
1127 // Otherwise do coercion through memory. This is stupid, but
1128 // simple.
1129 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
1130 CGF.Builder.CreateStore(Src, Tmp);
1131 llvm::Value *Casted =
1132 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
1133 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(Casted), DstPtr);
1134 }
1135}
1136
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001137void CodeGenFunction::EmitFunctionEpilog(QualType RetTy,
1138 llvm::Value *ReturnValue) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001139 llvm::Value *RV = 0;
1140
1141 // Functions with no result always return void.
1142 if (ReturnValue) {
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001143 ABIArgInfo RetAI = getABIReturnInfo(RetTy, CGM.getTypes());
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001144
1145 switch (RetAI.getKind()) {
1146 case ABIArgInfo::StructRet:
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001147 if (RetTy->isAnyComplexType()) {
1148 // FIXME: Volatile
1149 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
1150 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
1151 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1152 EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy);
1153 } else {
1154 Builder.CreateStore(Builder.CreateLoad(ReturnValue),
1155 CurFn->arg_begin());
1156 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001157 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001158
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001159 case ABIArgInfo::Default:
1160 RV = Builder.CreateLoad(ReturnValue);
1161 break;
1162
Daniel Dunbar11434922009-01-26 21:26:08 +00001163 case ABIArgInfo::Ignore:
1164 break;
1165
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001166 case ABIArgInfo::Coerce: {
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +00001167 RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001168 break;
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001169 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001170
1171 case ABIArgInfo::ByVal:
1172 case ABIArgInfo::Expand:
1173 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001174 }
1175 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001176
1177 if (RV) {
1178 Builder.CreateRet(RV);
1179 } else {
1180 Builder.CreateRetVoid();
1181 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001182}
1183
1184RValue CodeGenFunction::EmitCall(llvm::Value *Callee,
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001185 QualType RetTy,
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001186 const CallArgList &CallArgs) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001187 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001188
1189 // Handle struct-return functions by passing a pointer to the
1190 // location that we would like to return into.
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001191 ABIArgInfo RetAI = getABIReturnInfo(RetTy, CGM.getTypes());
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001192 switch (RetAI.getKind()) {
1193 case ABIArgInfo::StructRet:
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001194 // Create a temporary alloca to hold the result of the call. :(
Daniel Dunbar56273772008-09-17 00:51:38 +00001195 Args.push_back(CreateTempAlloca(ConvertType(RetTy)));
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001196 break;
1197
1198 case ABIArgInfo::Default:
Daniel Dunbar11434922009-01-26 21:26:08 +00001199 case ABIArgInfo::Ignore:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001200 case ABIArgInfo::Coerce:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001201 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001202
1203 case ABIArgInfo::ByVal:
1204 case ABIArgInfo::Expand:
1205 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001206 }
1207
1208 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
1209 I != E; ++I) {
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +00001210 ABIArgInfo ArgInfo = getABIArgumentInfo(I->second, CGM.getTypes());
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001211 RValue RV = I->first;
Daniel Dunbar56273772008-09-17 00:51:38 +00001212
1213 switch (ArgInfo.getKind()) {
1214 case ABIArgInfo::ByVal: // Default is byval
1215 case ABIArgInfo::Default:
1216 if (RV.isScalar()) {
1217 Args.push_back(RV.getScalarVal());
1218 } else if (RV.isComplex()) {
1219 // Make a temporary alloca to pass the argument.
1220 Args.push_back(CreateTempAlloca(ConvertType(I->second)));
1221 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
1222 } else {
1223 Args.push_back(RV.getAggregateAddr());
1224 }
1225 break;
1226
Daniel Dunbar11434922009-01-26 21:26:08 +00001227 case ABIArgInfo::Ignore:
1228 break;
1229
Daniel Dunbar56273772008-09-17 00:51:38 +00001230 case ABIArgInfo::StructRet:
1231 case ABIArgInfo::Coerce:
1232 assert(0 && "Invalid ABI kind for non-return argument");
1233 break;
1234
1235 case ABIArgInfo::Expand:
1236 ExpandTypeToArgs(I->second, RV, Args);
1237 break;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001238 }
1239 }
1240
1241 llvm::CallInst *CI = Builder.CreateCall(Callee,&Args[0],&Args[0]+Args.size());
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001242 CGCallInfo CallInfo(RetTy, CallArgs);
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001243
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001244 // FIXME: Provide TargetDecl so nounwind, noreturn, etc, etc get set.
Devang Patel761d7f72008-09-25 21:02:23 +00001245 CodeGen::AttributeListType AttributeList;
1246 CGM.ConstructAttributeList(0,
Daniel Dunbarb7688072008-09-10 00:41:16 +00001247 CallInfo.argtypes_begin(), CallInfo.argtypes_end(),
Devang Patel761d7f72008-09-25 21:02:23 +00001248 AttributeList);
1249 CI->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(),
1250 AttributeList.size()));
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001251
1252 if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee))
1253 CI->setCallingConv(F->getCallingConv());
1254 if (CI->getType() != llvm::Type::VoidTy)
1255 CI->setName("call");
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001256
1257 switch (RetAI.getKind()) {
1258 case ABIArgInfo::StructRet:
1259 if (RetTy->isAnyComplexType())
Daniel Dunbar56273772008-09-17 00:51:38 +00001260 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001261 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar56273772008-09-17 00:51:38 +00001262 return RValue::getAggregate(Args[0]);
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001263 else
1264 return RValue::get(Builder.CreateLoad(Args[0]));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001265
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001266 case ABIArgInfo::Default:
1267 return RValue::get(RetTy->isVoidType() ? 0 : CI);
1268
Daniel Dunbar11434922009-01-26 21:26:08 +00001269 case ABIArgInfo::Ignore:
Daniel Dunbarcc039fe2009-01-29 08:24:57 +00001270 if (RetTy->isVoidType())
1271 return RValue::get(0);
1272 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1273 llvm::Value *Res =
1274 llvm::UndefValue::get(llvm::PointerType::getUnqual(ConvertType(RetTy)));
1275 return RValue::getAggregate(Res);
1276 }
1277 return RValue::get(llvm::UndefValue::get(ConvertType(RetTy)));
Daniel Dunbar11434922009-01-26 21:26:08 +00001278
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001279 case ABIArgInfo::Coerce: {
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +00001280 llvm::Value *V = CreateTempAlloca(ConvertType(RetTy), "coerce");
1281 CreateCoercedStore(CI, V, *this);
Anders Carlssonad3d6912008-11-25 22:21:48 +00001282 if (RetTy->isAnyComplexType())
1283 return RValue::getComplex(LoadComplexFromAddr(V, false));
Daniel Dunbar11434922009-01-26 21:26:08 +00001284 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssonad3d6912008-11-25 22:21:48 +00001285 return RValue::getAggregate(V);
Daniel Dunbar11434922009-01-26 21:26:08 +00001286 else
1287 return RValue::get(Builder.CreateLoad(V));
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001288 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001289
1290 case ABIArgInfo::ByVal:
1291 case ABIArgInfo::Expand:
1292 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001293 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001294
1295 assert(0 && "Unhandled ABIArgInfo::Kind");
1296 return RValue::get(0);
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001297}