blob: d05c9ff4b92aa7d66cf61664c009240a88828491 [file] [log] [blame]
Daniel Dunbara8f02052008-09-08 21:33:45 +00001//===----- CGCall.h - Encapsulate calling convention details ----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// These classes wrap the information about a call or function
11// definition used to handle ABI compliancy.
12//
13//===----------------------------------------------------------------------===//
14
15#include "CGCall.h"
16#include "CodeGenFunction.h"
Daniel Dunbar3ef2e852008-09-10 00:41:16 +000017#include "CodeGenModule.h"
Daniel Dunbarf98eeff2008-10-13 17:02:26 +000018#include "clang/Basic/TargetInfo.h"
Daniel Dunbara8f02052008-09-08 21:33:45 +000019#include "clang/AST/ASTContext.h"
20#include "clang/AST/Decl.h"
21#include "clang/AST/DeclObjC.h"
Daniel Dunbar51a2d192009-01-29 08:13:58 +000022#include "clang/AST/RecordLayout.h"
Daniel Dunbar04d35782008-09-17 00:51:38 +000023#include "llvm/ADT/StringExtras.h"
Devang Patel98bfe502008-09-24 01:01:36 +000024#include "llvm/Attributes.h"
Daniel Dunbare09a9692009-01-24 08:32:22 +000025#include "llvm/Support/CommandLine.h"
Daniel Dunbar3cfcec72009-02-12 09:04:14 +000026#include "llvm/Support/MathExtras.h"
Daniel Dunbar9f4874e2009-02-04 23:24:38 +000027#include "llvm/Support/raw_ostream.h"
Daniel Dunbar708d8a82009-01-27 01:36:03 +000028#include "llvm/Target/TargetData.h"
Daniel Dunbard283e632009-02-03 01:05:53 +000029
30#include "ABIInfo.h"
31
Daniel Dunbara8f02052008-09-08 21:33:45 +000032using namespace clang;
33using namespace CodeGen;
34
35/***/
36
Daniel Dunbara8f02052008-09-08 21:33:45 +000037// FIXME: Use iterator and sidestep silly type array creation.
38
Daniel Dunbar34bda882009-02-02 23:23:47 +000039const
40CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionTypeNoProto *FTNP) {
41 return getFunctionInfo(FTNP->getResultType(),
42 llvm::SmallVector<QualType, 16>());
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000043}
44
Daniel Dunbar34bda882009-02-02 23:23:47 +000045const
46CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionTypeProto *FTP) {
47 llvm::SmallVector<QualType, 16> ArgTys;
48 // FIXME: Kill copy.
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000049 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
Daniel Dunbar34bda882009-02-02 23:23:47 +000050 ArgTys.push_back(FTP->getArgType(i));
51 return getFunctionInfo(FTP->getResultType(), ArgTys);
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000052}
53
Daniel Dunbar34bda882009-02-02 23:23:47 +000054const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionDecl *FD) {
Daniel Dunbara8f02052008-09-08 21:33:45 +000055 const FunctionType *FTy = FD->getType()->getAsFunctionType();
Daniel Dunbar34bda882009-02-02 23:23:47 +000056 if (const FunctionTypeProto *FTP = dyn_cast<FunctionTypeProto>(FTy))
57 return getFunctionInfo(FTP);
58 return getFunctionInfo(cast<FunctionTypeNoProto>(FTy));
Daniel Dunbara8f02052008-09-08 21:33:45 +000059}
60
Daniel Dunbar34bda882009-02-02 23:23:47 +000061const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const ObjCMethodDecl *MD) {
62 llvm::SmallVector<QualType, 16> ArgTys;
63 ArgTys.push_back(MD->getSelfDecl()->getType());
64 ArgTys.push_back(Context.getObjCSelType());
65 // FIXME: Kill copy?
Daniel Dunbara8f02052008-09-08 21:33:45 +000066 for (ObjCMethodDecl::param_const_iterator i = MD->param_begin(),
67 e = MD->param_end(); i != e; ++i)
Daniel Dunbar34bda882009-02-02 23:23:47 +000068 ArgTys.push_back((*i)->getType());
69 return getFunctionInfo(MD->getResultType(), ArgTys);
Daniel Dunbara8f02052008-09-08 21:33:45 +000070}
71
Daniel Dunbar34bda882009-02-02 23:23:47 +000072const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
73 const CallArgList &Args) {
74 // FIXME: Kill copy.
75 llvm::SmallVector<QualType, 16> ArgTys;
Daniel Dunbarebbb8f32009-01-31 02:19:00 +000076 for (CallArgList::const_iterator i = Args.begin(), e = Args.end();
77 i != e; ++i)
Daniel Dunbar34bda882009-02-02 23:23:47 +000078 ArgTys.push_back(i->second);
79 return getFunctionInfo(ResTy, ArgTys);
Daniel Dunbarebbb8f32009-01-31 02:19:00 +000080}
81
Daniel Dunbar34bda882009-02-02 23:23:47 +000082const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
83 const FunctionArgList &Args) {
84 // FIXME: Kill copy.
85 llvm::SmallVector<QualType, 16> ArgTys;
Daniel Dunbar9fc15a82009-02-02 21:43:58 +000086 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
87 i != e; ++i)
Daniel Dunbar34bda882009-02-02 23:23:47 +000088 ArgTys.push_back(i->second);
89 return getFunctionInfo(ResTy, ArgTys);
90}
91
92const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
93 const llvm::SmallVector<QualType, 16> &ArgTys) {
Daniel Dunbardcf19d12009-02-03 00:07:12 +000094 // Lookup or create unique function info.
95 llvm::FoldingSetNodeID ID;
96 CGFunctionInfo::Profile(ID, ResTy, ArgTys.begin(), ArgTys.end());
97
98 void *InsertPos = 0;
99 CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, InsertPos);
100 if (FI)
101 return *FI;
102
Daniel Dunbare92e0ab2009-02-03 05:31:23 +0000103 // Construct the function info.
Daniel Dunbardcf19d12009-02-03 00:07:12 +0000104 FI = new CGFunctionInfo(ResTy, ArgTys);
Daniel Dunbarb944cc92009-02-05 00:00:23 +0000105 FunctionInfos.InsertNode(FI, InsertPos);
Daniel Dunbare92e0ab2009-02-03 05:31:23 +0000106
107 // Compute ABI information.
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000108 getABIInfo().computeInfo(*FI, getContext());
Daniel Dunbare92e0ab2009-02-03 05:31:23 +0000109
Daniel Dunbardcf19d12009-02-03 00:07:12 +0000110 return *FI;
Daniel Dunbar34bda882009-02-02 23:23:47 +0000111}
112
113/***/
114
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000115ABIInfo::~ABIInfo() {}
116
Daniel Dunbar9f4874e2009-02-04 23:24:38 +0000117void ABIArgInfo::dump() const {
118 fprintf(stderr, "(ABIArgInfo Kind=");
119 switch (TheKind) {
120 case Direct:
121 fprintf(stderr, "Direct");
122 break;
Daniel Dunbar9f4874e2009-02-04 23:24:38 +0000123 case Ignore:
124 fprintf(stderr, "Ignore");
125 break;
126 case Coerce:
127 fprintf(stderr, "Coerce Type=");
128 getCoerceToType()->print(llvm::errs());
129 // FIXME: This is ridiculous.
130 llvm::errs().flush();
131 break;
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000132 case Indirect:
133 fprintf(stderr, "Indirect Align=%d", getIndirectAlign());
Daniel Dunbar9f4874e2009-02-04 23:24:38 +0000134 break;
135 case Expand:
136 fprintf(stderr, "Expand");
137 break;
138 }
139 fprintf(stderr, ")\n");
140}
141
142/***/
143
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000144/// isEmptyStruct - Return true iff a structure has no non-empty
145/// members. Note that a structure with a flexible array member is not
146/// considered empty.
147static bool isEmptyStruct(QualType T) {
148 const RecordType *RT = T->getAsStructureType();
149 if (!RT)
150 return 0;
151 const RecordDecl *RD = RT->getDecl();
152 if (RD->hasFlexibleArrayMember())
153 return false;
Douglas Gregor5d764842009-01-09 17:18:27 +0000154 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000155 e = RD->field_end(); i != e; ++i) {
156 const FieldDecl *FD = *i;
157 if (!isEmptyStruct(FD->getType()))
158 return false;
159 }
160 return true;
161}
162
163/// isSingleElementStruct - Determine if a structure is a "single
164/// element struct", i.e. it has exactly one non-empty field or
165/// exactly one field which is itself a single element
166/// struct. Structures with flexible array members are never
167/// considered single element structs.
168///
169/// \return The field declaration for the single non-empty field, if
170/// it exists.
171static const FieldDecl *isSingleElementStruct(QualType T) {
172 const RecordType *RT = T->getAsStructureType();
173 if (!RT)
174 return 0;
175
176 const RecordDecl *RD = RT->getDecl();
177 if (RD->hasFlexibleArrayMember())
178 return 0;
179
180 const FieldDecl *Found = 0;
Douglas Gregor5d764842009-01-09 17:18:27 +0000181 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000182 e = RD->field_end(); i != e; ++i) {
183 const FieldDecl *FD = *i;
184 QualType FT = FD->getType();
185
186 if (isEmptyStruct(FT)) {
187 // Ignore
188 } else if (Found) {
189 return 0;
190 } else if (!CodeGenFunction::hasAggregateLLVMType(FT)) {
191 Found = FD;
192 } else {
193 Found = isSingleElementStruct(FT);
194 if (!Found)
195 return 0;
196 }
197 }
198
199 return Found;
200}
201
202static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
203 if (!Ty->getAsBuiltinType() && !Ty->isPointerType())
204 return false;
205
206 uint64_t Size = Context.getTypeSize(Ty);
207 return Size == 32 || Size == 64;
208}
209
210static bool areAllFields32Or64BitBasicType(const RecordDecl *RD,
211 ASTContext &Context) {
Douglas Gregor5d764842009-01-09 17:18:27 +0000212 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000213 e = RD->field_end(); i != e; ++i) {
214 const FieldDecl *FD = *i;
215
216 if (!is32Or64BitBasicType(FD->getType(), Context))
217 return false;
218
219 // If this is a bit-field we need to make sure it is still a
220 // 32-bit or 64-bit type.
221 if (Expr *BW = FD->getBitWidth()) {
222 unsigned Width = BW->getIntegerConstantExprValue(Context).getZExtValue();
223 if (Width <= 16)
224 return false;
225 }
226 }
227 return true;
228}
229
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000230namespace {
231/// DefaultABIInfo - The default implementation for ABI specific
232/// details. This implementation provides information which results in
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000233/// self-consistent and sensible LLVM IR generation, but does not
234/// conform to any particular ABI.
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000235class DefaultABIInfo : public ABIInfo {
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000236 ABIArgInfo classifyReturnType(QualType RetTy,
237 ASTContext &Context) const;
238
239 ABIArgInfo classifyArgumentType(QualType RetTy,
240 ASTContext &Context) const;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000241
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000242 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
243 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
244 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
245 it != ie; ++it)
246 it->info = classifyArgumentType(it->type, Context);
247 }
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000248
249 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
250 CodeGenFunction &CGF) const;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000251};
252
253/// X86_32ABIInfo - The X86-32 ABI information.
254class X86_32ABIInfo : public ABIInfo {
255public:
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000256 ABIArgInfo classifyReturnType(QualType RetTy,
257 ASTContext &Context) const;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000258
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000259 ABIArgInfo classifyArgumentType(QualType RetTy,
260 ASTContext &Context) const;
261
262 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
263 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
264 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
265 it != ie; ++it)
266 it->info = classifyArgumentType(it->type, Context);
267 }
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000268
269 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
270 CodeGenFunction &CGF) const;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000271};
272}
273
274ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
275 ASTContext &Context) const {
Daniel Dunbareec02622009-02-03 06:30:17 +0000276 if (RetTy->isVoidType()) {
277 return ABIArgInfo::getIgnore();
278 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000279 // Classify "single element" structs as their element type.
280 const FieldDecl *SeltFD = isSingleElementStruct(RetTy);
281 if (SeltFD) {
282 QualType SeltTy = SeltFD->getType()->getDesugaredType();
283 if (const BuiltinType *BT = SeltTy->getAsBuiltinType()) {
284 // FIXME: This is gross, it would be nice if we could just
285 // pass back SeltTy and have clients deal with it. Is it worth
286 // supporting coerce to both LLVM and clang Types?
287 if (BT->isIntegerType()) {
288 uint64_t Size = Context.getTypeSize(SeltTy);
289 return ABIArgInfo::getCoerce(llvm::IntegerType::get((unsigned) Size));
290 } else if (BT->getKind() == BuiltinType::Float) {
291 return ABIArgInfo::getCoerce(llvm::Type::FloatTy);
292 } else if (BT->getKind() == BuiltinType::Double) {
293 return ABIArgInfo::getCoerce(llvm::Type::DoubleTy);
294 }
295 } else if (SeltTy->isPointerType()) {
296 // FIXME: It would be really nice if this could come out as
297 // the proper pointer type.
298 llvm::Type *PtrTy =
299 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
300 return ABIArgInfo::getCoerce(PtrTy);
301 }
302 }
303
Daniel Dunbar73d66602008-09-10 07:04:09 +0000304 uint64_t Size = Context.getTypeSize(RetTy);
305 if (Size == 8) {
306 return ABIArgInfo::getCoerce(llvm::Type::Int8Ty);
307 } else if (Size == 16) {
308 return ABIArgInfo::getCoerce(llvm::Type::Int16Ty);
309 } else if (Size == 32) {
310 return ABIArgInfo::getCoerce(llvm::Type::Int32Ty);
311 } else if (Size == 64) {
312 return ABIArgInfo::getCoerce(llvm::Type::Int64Ty);
313 } else {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000314 return ABIArgInfo::getIndirect(0);
Daniel Dunbar73d66602008-09-10 07:04:09 +0000315 }
Daniel Dunbare126ab12008-09-10 02:41:04 +0000316 } else {
Daniel Dunbareec02622009-02-03 06:30:17 +0000317 return ABIArgInfo::getDirect();
Daniel Dunbare126ab12008-09-10 02:41:04 +0000318 }
319}
320
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000321ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000322 ASTContext &Context) const {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000323 // FIXME: Set alignment on indirect arguments.
Daniel Dunbar3158c592008-09-17 20:11:04 +0000324 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000325 // Structures with flexible arrays are always indirect.
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000326 if (const RecordType *RT = Ty->getAsStructureType())
327 if (RT->getDecl()->hasFlexibleArrayMember())
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000328 return ABIArgInfo::getIndirect(0);
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000329
Daniel Dunbar33b189a2009-02-05 01:50:07 +0000330 // Ignore empty structs.
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000331 uint64_t Size = Context.getTypeSize(Ty);
332 if (Ty->isStructureType() && Size == 0)
Daniel Dunbar33b189a2009-02-05 01:50:07 +0000333 return ABIArgInfo::getIgnore();
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000334
335 // Expand structs with size <= 128-bits which consist only of
336 // basic types (int, long long, float, double, xxx*). This is
337 // non-recursive and does not ignore empty fields.
338 if (const RecordType *RT = Ty->getAsStructureType()) {
339 if (Context.getTypeSize(Ty) <= 4*32 &&
340 areAllFields32Or64BitBasicType(RT->getDecl(), Context))
341 return ABIArgInfo::getExpand();
342 }
343
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000344 return ABIArgInfo::getIndirect(0);
Daniel Dunbar22e30052008-09-11 01:48:57 +0000345 } else {
Daniel Dunbareec02622009-02-03 06:30:17 +0000346 return ABIArgInfo::getDirect();
Daniel Dunbar22e30052008-09-11 01:48:57 +0000347 }
348}
349
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000350llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
351 CodeGenFunction &CGF) const {
352 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
353 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
354
355 CGBuilderTy &Builder = CGF.Builder;
356 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
357 "ap");
358 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
359 llvm::Type *PTy =
360 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
361 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
362
363 uint64_t SizeInBytes = CGF.getContext().getTypeSize(Ty) / 8;
364 const unsigned ArgumentSizeInBytes = 4;
365 if (SizeInBytes < ArgumentSizeInBytes)
366 SizeInBytes = ArgumentSizeInBytes;
367
368 llvm::Value *NextAddr =
369 Builder.CreateGEP(Addr,
370 llvm::ConstantInt::get(llvm::Type::Int32Ty, SizeInBytes),
371 "ap.next");
372 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
373
374 return AddrTyped;
375}
376
Daniel Dunbare09a9692009-01-24 08:32:22 +0000377namespace {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000378/// X86_64ABIInfo - The X86_64 ABI information.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000379class X86_64ABIInfo : public ABIInfo {
380 enum Class {
381 Integer = 0,
382 SSE,
383 SSEUp,
384 X87,
385 X87Up,
386 ComplexX87,
387 NoClass,
388 Memory
389 };
390
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000391 /// merge - Implement the X86_64 ABI merging algorithm.
392 ///
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000393 /// Merge an accumulating classification \arg Accum with a field
394 /// classification \arg Field.
395 ///
396 /// \param Accum - The accumulating classification. This should
397 /// always be either NoClass or the result of a previous merge
398 /// call. In addition, this should never be Memory (the caller
399 /// should just return Memory for the aggregate).
400 Class merge(Class Accum, Class Field) const;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000401
Daniel Dunbare09a9692009-01-24 08:32:22 +0000402 /// classify - Determine the x86_64 register classes in which the
403 /// given type T should be passed.
404 ///
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000405 /// \param Lo - The classification for the parts of the type
406 /// residing in the low word of the containing object.
407 ///
408 /// \param Hi - The classification for the parts of the type
409 /// residing in the high word of the containing object.
410 ///
411 /// \param OffsetBase - The bit offset of this type in the
Daniel Dunbar2a2dce32009-01-30 22:40:15 +0000412 /// containing object. Some parameters are classified different
413 /// depending on whether they straddle an eightbyte boundary.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000414 ///
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.
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000423 void classify(QualType T, ASTContext &Context, uint64_t OffsetBase,
Daniel Dunbare09a9692009-01-24 08:32:22 +0000424 Class &Lo, Class &Hi) const;
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000425
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000426 ABIArgInfo classifyReturnType(QualType RetTy,
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000427 ASTContext &Context) const;
428
429 ABIArgInfo classifyArgumentType(QualType Ty,
430 ASTContext &Context,
Daniel Dunbare978cb92009-02-10 17:06:09 +0000431 unsigned &neededInt,
432 unsigned &neededSSE) const;
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000433
434public:
435 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const;
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000436
437 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
438 CodeGenFunction &CGF) const;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000439};
440}
441
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000442X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum,
443 Class Field) const {
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000444 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
445 // classified recursively so that always two fields are
446 // considered. The resulting class is calculated according to
447 // the classes of the fields in the eightbyte:
448 //
449 // (a) If both classes are equal, this is the resulting class.
450 //
451 // (b) If one of the classes is NO_CLASS, the resulting class is
452 // the other class.
453 //
454 // (c) If one of the classes is MEMORY, the result is the MEMORY
455 // class.
456 //
457 // (d) If one of the classes is INTEGER, the result is the
458 // INTEGER.
459 //
460 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
461 // MEMORY is used as class.
462 //
463 // (f) Otherwise class SSE is used.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000464 assert((Accum == NoClass || Accum == Integer ||
465 Accum == SSE || Accum == SSEUp) &&
466 "Invalid accumulated classification during merge.");
467 if (Accum == Field || Field == NoClass)
468 return Accum;
469 else if (Field == Memory)
470 return Memory;
471 else if (Accum == NoClass)
472 return Field;
473 else if (Accum == Integer || Field == Integer)
474 return Integer;
475 else if (Field == X87 || Field == X87Up || Field == ComplexX87)
476 return Memory;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000477 else
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000478 return SSE;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000479}
480
Daniel Dunbare09a9692009-01-24 08:32:22 +0000481void X86_64ABIInfo::classify(QualType Ty,
482 ASTContext &Context,
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000483 uint64_t OffsetBase,
Daniel Dunbare09a9692009-01-24 08:32:22 +0000484 Class &Lo, Class &Hi) const {
Daniel Dunbar36b378e2009-02-02 18:06:39 +0000485 // FIXME: This code can be simplified by introducing a simple value
486 // class for Class pairs with appropriate constructor methods for
487 // the various situations.
488
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000489 Lo = Hi = NoClass;
490
491 Class &Current = OffsetBase < 64 ? Lo : Hi;
492 Current = Memory;
493
Daniel Dunbare09a9692009-01-24 08:32:22 +0000494 if (const BuiltinType *BT = Ty->getAsBuiltinType()) {
495 BuiltinType::Kind k = BT->getKind();
496
Daniel Dunbar1358b202009-01-26 21:26:08 +0000497 if (k == BuiltinType::Void) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000498 Current = NoClass;
Daniel Dunbar1358b202009-01-26 21:26:08 +0000499 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000500 Current = Integer;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000501 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000502 Current = SSE;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000503 } else if (k == BuiltinType::LongDouble) {
504 Lo = X87;
505 Hi = X87Up;
506 }
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000507 // FIXME: _Decimal32 and _Decimal64 are SSE.
508 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Daniel Dunbare09a9692009-01-24 08:32:22 +0000509 // FIXME: __int128 is (Integer, Integer).
510 } else if (Ty->isPointerLikeType() || Ty->isBlockPointerType() ||
511 Ty->isObjCQualifiedInterfaceType()) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000512 Current = Integer;
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000513 } else if (const VectorType *VT = Ty->getAsVectorType()) {
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000514 uint64_t Size = Context.getTypeSize(VT);
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000515 if (Size == 64) {
Daniel Dunbarcdf91e82009-01-30 19:38:39 +0000516 // gcc passes <1 x double> in memory.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000517 if (VT->getElementType() == Context.DoubleTy)
Daniel Dunbarcdf91e82009-01-30 19:38:39 +0000518 return;
Daniel Dunbarcdf91e82009-01-30 19:38:39 +0000519
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000520 Current = SSE;
Daniel Dunbare413f532009-01-30 18:40:10 +0000521
522 // If this type crosses an eightbyte boundary, it should be
523 // split.
Daniel Dunbar2a2dce32009-01-30 22:40:15 +0000524 if (OffsetBase && OffsetBase != 64)
Daniel Dunbare413f532009-01-30 18:40:10 +0000525 Hi = Lo;
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000526 } else if (Size == 128) {
527 Lo = SSE;
528 Hi = SSEUp;
529 }
Daniel Dunbare09a9692009-01-24 08:32:22 +0000530 } else if (const ComplexType *CT = Ty->getAsComplexType()) {
531 QualType ET = CT->getElementType();
532
Daniel Dunbare413f532009-01-30 18:40:10 +0000533 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbar28770fc2009-01-29 07:22:20 +0000534 if (ET->isIntegerType()) {
Daniel Dunbar28770fc2009-01-29 07:22:20 +0000535 if (Size <= 64)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000536 Current = Integer;
Daniel Dunbar28770fc2009-01-29 07:22:20 +0000537 else if (Size <= 128)
538 Lo = Hi = Integer;
539 } else if (ET == Context.FloatTy)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000540 Current = SSE;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000541 else if (ET == Context.DoubleTy)
542 Lo = Hi = SSE;
543 else if (ET == Context.LongDoubleTy)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000544 Current = ComplexX87;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000545
546 // If this complex type crosses an eightbyte boundary then it
547 // should be split.
Daniel Dunbar2a2dce32009-01-30 22:40:15 +0000548 uint64_t EB_Real = (OffsetBase) / 64;
549 uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000550 if (Hi == NoClass && EB_Real != EB_Imag)
551 Hi = Lo;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000552 } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
553 // Arrays are treated like structures.
554
555 uint64_t Size = Context.getTypeSize(Ty);
556
557 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
558 // than two eightbytes, ..., it has class MEMORY.
559 if (Size > 128)
560 return;
561
562 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
563 // fields, it has class MEMORY.
564 //
565 // Only need to check alignment of array base.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000566 if (OffsetBase % Context.getTypeAlign(AT->getElementType()))
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000567 return;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000568
569 // Otherwise implement simplified merge. We could be smarter about
570 // this, but it isn't worth it and would be harder to verify.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000571 Current = NoClass;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000572 uint64_t EltSize = Context.getTypeSize(AT->getElementType());
573 uint64_t ArraySize = AT->getSize().getZExtValue();
574 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
575 Class FieldLo, FieldHi;
576 classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000577 Lo = merge(Lo, FieldLo);
578 Hi = merge(Hi, FieldHi);
579 if (Lo == Memory || Hi == Memory)
580 break;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000581 }
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000582
583 // Do post merger cleanup (see below). Only case we worry about is Memory.
584 if (Hi == Memory)
585 Lo = Memory;
586 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000587 } else if (const RecordType *RT = Ty->getAsRecordType()) {
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000588 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000589
590 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
591 // than two eightbytes, ..., it has class MEMORY.
592 if (Size > 128)
593 return;
594
595 const RecordDecl *RD = RT->getDecl();
596
597 // Assume variable sized types are passed in memory.
598 if (RD->hasFlexibleArrayMember())
599 return;
600
601 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
602
603 // Reset Lo class, this will be recomputed.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000604 Current = NoClass;
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000605 unsigned idx = 0;
606 for (RecordDecl::field_iterator i = RD->field_begin(),
607 e = RD->field_end(); i != e; ++i, ++idx) {
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000608 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000609
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000610 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
611 // fields, it has class MEMORY.
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000612 if (Offset % Context.getTypeAlign(i->getType())) {
613 Lo = Memory;
614 return;
615 }
616
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000617 // Classify this field.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000618 //
619 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
620 // exceeds a single eightbyte, each is classified
621 // separately. Each eightbyte gets initialized to class
622 // NO_CLASS.
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000623 Class FieldLo, FieldHi;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000624 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000625 Lo = merge(Lo, FieldLo);
626 Hi = merge(Hi, FieldHi);
627 if (Lo == Memory || Hi == Memory)
628 break;
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000629 }
630
631 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
632 //
633 // (a) If one of the classes is MEMORY, the whole argument is
634 // passed in memory.
635 //
636 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
637
638 // The first of these conditions is guaranteed by how we implement
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000639 // the merge (just bail).
640 //
641 // The second condition occurs in the case of unions; for example
642 // union { _Complex double; unsigned; }.
643 if (Hi == Memory)
644 Lo = Memory;
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000645 if (Hi == SSEUp && Lo != SSE)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000646 Hi = SSE;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000647 }
648}
649
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000650
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000651ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
652 ASTContext &Context) const {
Daniel Dunbare09a9692009-01-24 08:32:22 +0000653 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
654 // classification algorithm.
655 X86_64ABIInfo::Class Lo, Hi;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000656 classify(RetTy, Context, 0, Lo, Hi);
Daniel Dunbare09a9692009-01-24 08:32:22 +0000657
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000658 // Check some invariants.
659 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
660 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
661 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
662
Daniel Dunbare09a9692009-01-24 08:32:22 +0000663 const llvm::Type *ResType = 0;
664 switch (Lo) {
665 case NoClass:
Daniel Dunbar1358b202009-01-26 21:26:08 +0000666 return ABIArgInfo::getIgnore();
Daniel Dunbare09a9692009-01-24 08:32:22 +0000667
668 case SSEUp:
669 case X87Up:
670 assert(0 && "Invalid classification for lo word.");
671
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000672 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000673 // hidden argument.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000674 case Memory:
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000675 return ABIArgInfo::getIndirect(0);
Daniel Dunbare09a9692009-01-24 08:32:22 +0000676
677 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
678 // available register of the sequence %rax, %rdx is used.
679 case Integer:
680 ResType = llvm::Type::Int64Ty; break;
681
682 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
683 // available SSE register of the sequence %xmm0, %xmm1 is used.
684 case SSE:
685 ResType = llvm::Type::DoubleTy; break;
686
687 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
688 // returned on the X87 stack in %st0 as 80-bit x87 number.
689 case X87:
690 ResType = llvm::Type::X86_FP80Ty; break;
691
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000692 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
693 // part of the value is returned in %st0 and the imaginary part in
694 // %st1.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000695 case ComplexX87:
696 assert(Hi == NoClass && "Unexpected ComplexX87 classification.");
697 ResType = llvm::VectorType::get(llvm::Type::X86_FP80Ty, 2);
698 break;
699 }
700
701 switch (Hi) {
702 // Memory was handled previously, and ComplexX87 and X87 should
703 // never occur as hi classes.
704 case Memory:
705 case X87:
706 case ComplexX87:
707 assert(0 && "Invalid classification for hi word.");
708
709 case NoClass: break;
710 case Integer:
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000711 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
712 break;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000713 case SSE:
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000714 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
715 break;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000716
717 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
718 // is passed in the upper half of the last used SSE register.
719 //
720 // SSEUP should always be preceeded by SSE, just widen.
721 case SSEUp:
722 assert(Lo == SSE && "Unexpected SSEUp classification.");
723 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
724 break;
725
726 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000727 // returned together with the previous X87 value in %st0.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000728 //
729 // X87UP should always be preceeded by X87, so we don't need to do
730 // anything here.
731 case X87Up:
732 assert(Lo == X87 && "Unexpected X87Up classification.");
733 break;
734 }
735
736 return ABIArgInfo::getCoerce(ResType);
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000737}
738
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000739ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, ASTContext &Context,
Daniel Dunbare978cb92009-02-10 17:06:09 +0000740 unsigned &neededInt,
741 unsigned &neededSSE) const {
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000742 X86_64ABIInfo::Class Lo, Hi;
743 classify(Ty, Context, 0, Lo, Hi);
744
745 // Check some invariants.
746 // FIXME: Enforce these by construction.
747 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
748 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
749 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
750
Daniel Dunbare978cb92009-02-10 17:06:09 +0000751 neededInt = 0;
752 neededSSE = 0;
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000753 const llvm::Type *ResType = 0;
754 switch (Lo) {
755 case NoClass:
756 return ABIArgInfo::getIgnore();
757
758 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
759 // on the stack.
760 case Memory:
761
762 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
763 // COMPLEX_X87, it is passed in memory.
764 case X87:
765 case ComplexX87:
766 // Choose appropriate in memory type.
767 if (CodeGenFunction::hasAggregateLLVMType(Ty))
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000768 return ABIArgInfo::getIndirect(0);
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000769 else
770 return ABIArgInfo::getDirect();
771
772 case SSEUp:
773 case X87Up:
774 assert(0 && "Invalid classification for lo word.");
775
776 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
777 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
778 // and %r9 is used.
779 case Integer:
780 ++neededInt;
781 ResType = llvm::Type::Int64Ty;
782 break;
783
784 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
785 // available SSE register is used, the registers are taken in the
786 // order from %xmm0 to %xmm7.
787 case SSE:
788 ++neededSSE;
789 ResType = llvm::Type::DoubleTy;
790 break;
Daniel Dunbareec02622009-02-03 06:30:17 +0000791 }
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000792
793 switch (Hi) {
794 // Memory was handled previously, ComplexX87 and X87 should
795 // never occur as hi classes, and X87Up must be preceed by X87,
796 // which is passed in memory.
797 case Memory:
798 case X87:
799 case X87Up:
800 case ComplexX87:
801 assert(0 && "Invalid classification for hi word.");
802
803 case NoClass: break;
804 case Integer:
805 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
806 ++neededInt;
807 break;
808 case SSE:
809 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
810 ++neededSSE;
811 break;
812
813 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
814 // eightbyte is passed in the upper half of the last used SSE
815 // register.
816 case SSEUp:
817 assert(Lo == SSE && "Unexpected SSEUp classification.");
818 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
819 break;
820 }
821
Daniel Dunbare978cb92009-02-10 17:06:09 +0000822 return ABIArgInfo::getCoerce(ResType);
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000823}
824
825void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
826 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
827
828 // Keep track of the number of assigned registers.
829 unsigned freeIntRegs = 6, freeSSERegs = 8;
830
831 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
832 // get assigned (in left-to-right order) for passing as follows...
833 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Daniel Dunbare978cb92009-02-10 17:06:09 +0000834 it != ie; ++it) {
835 unsigned neededInt, neededSSE;
836 it->info = classifyArgumentType(it->type, Context, neededInt, neededSSE);
837
838 // AMD64-ABI 3.2.3p3: If there are no registers available for any
839 // eightbyte of an argument, the whole argument is passed on the
840 // stack. If registers have already been assigned for some
841 // eightbytes of such an argument, the assignments get reverted.
842 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
843 freeIntRegs -= neededInt;
844 freeSSERegs -= neededSSE;
845 } else {
846 // Choose appropriate in memory type.
847 if (CodeGenFunction::hasAggregateLLVMType(it->type))
848 it->info = ABIArgInfo::getIndirect(0);
849 else
850 it->info = ABIArgInfo::getDirect();
851 }
852 }
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000853}
854
Daniel Dunbar3cfcec72009-02-12 09:04:14 +0000855static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
856 QualType Ty,
857 CodeGenFunction &CGF) {
858 llvm::Value *overflow_arg_area_p =
859 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
860 llvm::Value *overflow_arg_area =
861 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
862
863 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
864 // byte boundary if alignment needed by type exceeds 8 byte boundary.
865 uint64_t Align = llvm::NextPowerOf2(CGF.getContext().getTypeAlign(Ty) / 8);
866 if (Align > 8) {
867 // Note align to type alignment instead of assuming it must be 16.
868
869 // FIXME: Implement alignment in x86_64 va_arg.
870 }
871
872 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
873 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
874 llvm::Value *Res =
875 CGF.Builder.CreateBitCast(overflow_arg_area,
876 llvm::PointerType::getUnqual(LTy));
877
878 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
879 // l->overflow_arg_area + sizeof(type).
880 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
881 // an 8 byte boundary.
882
883 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
884 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
885 (SizeInBytes + 7) & ~7);
886 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
887 "overflow_arg_area.next");
888 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
889
890 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
891 return Res;
892}
893
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000894llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
895 CodeGenFunction &CGF) const {
Daniel Dunbar3cfcec72009-02-12 09:04:14 +0000896 // Assume that va_list type is correct; should be pointer to LLVM type:
897 // struct {
898 // i32 gp_offset;
899 // i32 fp_offset;
900 // i8* overflow_arg_area;
901 // i8* reg_save_area;
902 // };
903 unsigned neededInt, neededSSE;
904 ABIArgInfo AI = classifyArgumentType(Ty, CGF.getContext(),
905 neededInt, neededSSE);
906
907 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
908 // in the registers. If not go to step 7.
909 if (!neededInt && !neededSSE)
910 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
911
912 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
913 // general purpose registers needed to pass type and num_fp to hold
914 // the number of floating point registers needed.
915
916 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
917 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
918 // l->fp_offset > 304 - num_fp * 16 go to step 7.
919 //
920 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
921 // register save space).
922
923 llvm::Value *InRegs = 0;
924 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
925 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
926 if (neededInt) {
927 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
928 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
929 InRegs =
930 CGF.Builder.CreateICmpULE(gp_offset,
931 llvm::ConstantInt::get(llvm::Type::Int32Ty,
932 48 - neededInt * 8),
933 "fits_in_gp");
934 }
935
936 if (neededSSE) {
937 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
938 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
939 llvm::Value *FitsInFP =
940 CGF.Builder.CreateICmpULE(fp_offset,
941 llvm::ConstantInt::get(llvm::Type::Int32Ty,
942 176 - neededSSE * 8),
943 "fits_in_fp");
944 InRegs = InRegs ? CGF.Builder.CreateOr(InRegs, FitsInFP) : FitsInFP;
945 }
946
947 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
948 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
949 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
950 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
951
952 // Emit code to load the value if it was passed in registers.
953
954 CGF.EmitBlock(InRegBlock);
955
956 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
957 // an offset of l->gp_offset and/or l->fp_offset. This may require
958 // copying to a temporary location in case the parameter is passed
959 // in different register classes or requires an alignment greater
960 // than 8 for general purpose registers and 16 for XMM registers.
961 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
962 llvm::Value *RegAddr =
963 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
964 "reg_save_area");
965 if (neededInt && neededSSE) {
966 assert(0 && "FIXME: Implement support for va_arg in mixed regs");
967 } else if (neededInt) {
968 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
969 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
970 llvm::PointerType::getUnqual(LTy));
971 } else {
972 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
973 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
974 llvm::PointerType::getUnqual(LTy));
975 }
976
977 // AMD64-ABI 3.5.7p5: Step 5. Set:
978 // l->gp_offset = l->gp_offset + num_gp * 8
979 // l->fp_offset = l->fp_offset + num_fp * 16.
980 if (neededInt) {
981 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
982 neededInt * 8);
983 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
984 gp_offset_p);
985 }
986 if (neededSSE) {
987 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
988 neededSSE * 16);
989 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
990 fp_offset_p);
991 }
992 CGF.EmitBranch(ContBlock);
993
994 // Emit code to load the value if it was passed in memory.
995
996 CGF.EmitBlock(InMemBlock);
997 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
998
999 // Return the appropriate result.
1000
1001 CGF.EmitBlock(ContBlock);
1002 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(),
1003 "vaarg.addr");
1004 ResAddr->reserveOperandSpace(2);
1005 ResAddr->addIncoming(RegAddr, InRegBlock);
1006 ResAddr->addIncoming(MemAddr, InMemBlock);
1007
1008 return ResAddr;
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001009}
1010
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001011ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001012 ASTContext &Context) const {
Daniel Dunbareec02622009-02-03 06:30:17 +00001013 if (RetTy->isVoidType()) {
1014 return ABIArgInfo::getIgnore();
1015 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001016 return ABIArgInfo::getIndirect(0);
Daniel Dunbareec02622009-02-03 06:30:17 +00001017 } else {
1018 return ABIArgInfo::getDirect();
1019 }
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001020}
1021
1022ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001023 ASTContext &Context) const {
Daniel Dunbareec02622009-02-03 06:30:17 +00001024 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001025 return ABIArgInfo::getIndirect(0);
Daniel Dunbareec02622009-02-03 06:30:17 +00001026 } else {
1027 return ABIArgInfo::getDirect();
1028 }
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001029}
1030
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001031llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1032 CodeGenFunction &CGF) const {
1033 return 0;
1034}
1035
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001036const ABIInfo &CodeGenTypes::getABIInfo() const {
1037 if (TheABIInfo)
1038 return *TheABIInfo;
1039
1040 // For now we just cache this in the CodeGenTypes and don't bother
1041 // to free it.
1042 const char *TargetPrefix = getContext().Target.getTargetPrefix();
1043 if (strcmp(TargetPrefix, "x86") == 0) {
Daniel Dunbarb6d5c442009-01-15 18:18:40 +00001044 switch (getContext().Target.getPointerWidth(0)) {
1045 case 32:
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001046 return *(TheABIInfo = new X86_32ABIInfo());
Daniel Dunbarb6d5c442009-01-15 18:18:40 +00001047 case 64:
Daniel Dunbar56555952009-01-30 18:47:53 +00001048 return *(TheABIInfo = new X86_64ABIInfo());
Daniel Dunbarb6d5c442009-01-15 18:18:40 +00001049 }
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001050 }
1051
1052 return *(TheABIInfo = new DefaultABIInfo);
1053}
1054
Daniel Dunbare126ab12008-09-10 02:41:04 +00001055/***/
1056
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001057CGFunctionInfo::CGFunctionInfo(QualType ResTy,
1058 const llvm::SmallVector<QualType, 16> &ArgTys) {
1059 NumArgs = ArgTys.size();
1060 Args = new ArgInfo[1 + NumArgs];
1061 Args[0].type = ResTy;
1062 for (unsigned i = 0; i < NumArgs; ++i)
1063 Args[1 + i].type = ArgTys[i];
1064}
1065
1066/***/
1067
Daniel Dunbar04d35782008-09-17 00:51:38 +00001068void CodeGenTypes::GetExpandedTypes(QualType Ty,
1069 std::vector<const llvm::Type*> &ArgTys) {
1070 const RecordType *RT = Ty->getAsStructureType();
1071 assert(RT && "Can only expand structure types.");
1072 const RecordDecl *RD = RT->getDecl();
1073 assert(!RD->hasFlexibleArrayMember() &&
1074 "Cannot expand structure with flexible array.");
1075
Douglas Gregor5d764842009-01-09 17:18:27 +00001076 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar04d35782008-09-17 00:51:38 +00001077 e = RD->field_end(); i != e; ++i) {
1078 const FieldDecl *FD = *i;
1079 assert(!FD->isBitField() &&
1080 "Cannot expand structure with bit-field members.");
1081
1082 QualType FT = FD->getType();
1083 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1084 GetExpandedTypes(FT, ArgTys);
1085 } else {
1086 ArgTys.push_back(ConvertType(FT));
1087 }
1088 }
1089}
1090
1091llvm::Function::arg_iterator
1092CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
1093 llvm::Function::arg_iterator AI) {
1094 const RecordType *RT = Ty->getAsStructureType();
1095 assert(RT && "Can only expand structure types.");
1096
1097 RecordDecl *RD = RT->getDecl();
1098 assert(LV.isSimple() &&
1099 "Unexpected non-simple lvalue during struct expansion.");
1100 llvm::Value *Addr = LV.getAddress();
1101 for (RecordDecl::field_iterator i = RD->field_begin(),
1102 e = RD->field_end(); i != e; ++i) {
1103 FieldDecl *FD = *i;
1104 QualType FT = FD->getType();
1105
1106 // FIXME: What are the right qualifiers here?
1107 LValue LV = EmitLValueForField(Addr, FD, false, 0);
1108 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1109 AI = ExpandTypeFromArgs(FT, LV, AI);
1110 } else {
1111 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
1112 ++AI;
1113 }
1114 }
1115
1116 return AI;
1117}
1118
1119void
1120CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
1121 llvm::SmallVector<llvm::Value*, 16> &Args) {
1122 const RecordType *RT = Ty->getAsStructureType();
1123 assert(RT && "Can only expand structure types.");
1124
1125 RecordDecl *RD = RT->getDecl();
1126 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
1127 llvm::Value *Addr = RV.getAggregateAddr();
1128 for (RecordDecl::field_iterator i = RD->field_begin(),
1129 e = RD->field_end(); i != e; ++i) {
1130 FieldDecl *FD = *i;
1131 QualType FT = FD->getType();
1132
1133 // FIXME: What are the right qualifiers here?
1134 LValue LV = EmitLValueForField(Addr, FD, false, 0);
1135 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1136 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
1137 } else {
1138 RValue RV = EmitLoadOfLValue(LV, FT);
1139 assert(RV.isScalar() &&
1140 "Unexpected non-scalar rvalue during struct expansion.");
1141 Args.push_back(RV.getScalarVal());
1142 }
1143 }
1144}
1145
Daniel Dunbar84379912009-02-02 19:06:38 +00001146/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
1147/// a pointer to an object of type \arg Ty.
1148///
1149/// This safely handles the case when the src type is smaller than the
1150/// destination type; in this situation the values of bits which not
1151/// present in the src are undefined.
1152static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
1153 const llvm::Type *Ty,
1154 CodeGenFunction &CGF) {
1155 const llvm::Type *SrcTy =
1156 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
1157 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
1158 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(Ty);
1159
Daniel Dunbar77071992009-02-03 05:59:18 +00001160 // If load is legal, just bitcast the src pointer.
Daniel Dunbar84379912009-02-02 19:06:38 +00001161 if (SrcSize == DstSize) {
1162 llvm::Value *Casted =
1163 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
Daniel Dunbar3f062382009-02-07 02:46:03 +00001164 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
1165 // FIXME: Use better alignment / avoid requiring aligned load.
1166 Load->setAlignment(1);
1167 return Load;
Daniel Dunbar84379912009-02-02 19:06:38 +00001168 } else {
1169 assert(SrcSize < DstSize && "Coercion is losing source bits!");
1170
1171 // Otherwise do coercion through memory. This is stupid, but
1172 // simple.
1173 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
1174 llvm::Value *Casted =
1175 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar3f062382009-02-07 02:46:03 +00001176 llvm::StoreInst *Store =
1177 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
1178 // FIXME: Use better alignment / avoid requiring aligned store.
1179 Store->setAlignment(1);
Daniel Dunbar84379912009-02-02 19:06:38 +00001180 return CGF.Builder.CreateLoad(Tmp);
1181 }
1182}
1183
1184/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
1185/// where the source and destination may have different types.
1186///
1187/// This safely handles the case when the src type is larger than the
1188/// destination type; the upper bits of the src will be lost.
1189static void CreateCoercedStore(llvm::Value *Src,
1190 llvm::Value *DstPtr,
1191 CodeGenFunction &CGF) {
1192 const llvm::Type *SrcTy = Src->getType();
1193 const llvm::Type *DstTy =
1194 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
1195
1196 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
1197 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(DstTy);
1198
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001199 // If store is legal, just bitcast the src pointer.
Daniel Dunbar84379912009-02-02 19:06:38 +00001200 if (SrcSize == DstSize) {
1201 llvm::Value *Casted =
1202 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar3f062382009-02-07 02:46:03 +00001203 // FIXME: Use better alignment / avoid requiring aligned store.
1204 CGF.Builder.CreateStore(Src, Casted)->setAlignment(1);
Daniel Dunbar84379912009-02-02 19:06:38 +00001205 } else {
1206 assert(SrcSize > DstSize && "Coercion is missing bits!");
1207
1208 // Otherwise do coercion through memory. This is stupid, but
1209 // simple.
1210 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
1211 CGF.Builder.CreateStore(Src, Tmp);
1212 llvm::Value *Casted =
1213 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
Daniel Dunbar3f062382009-02-07 02:46:03 +00001214 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
1215 // FIXME: Use better alignment / avoid requiring aligned load.
1216 Load->setAlignment(1);
1217 CGF.Builder.CreateStore(Load, DstPtr);
Daniel Dunbar84379912009-02-02 19:06:38 +00001218 }
1219}
1220
Daniel Dunbar04d35782008-09-17 00:51:38 +00001221/***/
1222
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001223bool CodeGenModule::ReturnTypeUsesSret(const CGFunctionInfo &FI) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001224 return FI.getReturnInfo().isIndirect();
Daniel Dunbar9fc15a82009-02-02 21:43:58 +00001225}
1226
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001227const llvm::FunctionType *
Daniel Dunbar9fc15a82009-02-02 21:43:58 +00001228CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic) {
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001229 std::vector<const llvm::Type*> ArgTys;
1230
1231 const llvm::Type *ResultType = 0;
1232
Daniel Dunbar0b37ca82009-02-02 23:43:58 +00001233 QualType RetTy = FI.getReturnType();
Daniel Dunbar77071992009-02-03 05:59:18 +00001234 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar22e30052008-09-11 01:48:57 +00001235 switch (RetAI.getKind()) {
Daniel Dunbar22e30052008-09-11 01:48:57 +00001236 case ABIArgInfo::Expand:
1237 assert(0 && "Invalid ABI kind for return argument");
1238
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001239 case ABIArgInfo::Direct:
1240 ResultType = ConvertType(RetTy);
1241 break;
1242
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001243 case ABIArgInfo::Indirect: {
1244 assert(!RetAI.getIndirectAlign() && "Align unused on indirect return.");
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001245 ResultType = llvm::Type::VoidTy;
Daniel Dunbara9976a22008-09-10 07:00:50 +00001246 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001247 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
1248 break;
1249 }
1250
Daniel Dunbar1358b202009-01-26 21:26:08 +00001251 case ABIArgInfo::Ignore:
1252 ResultType = llvm::Type::VoidTy;
1253 break;
1254
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001255 case ABIArgInfo::Coerce:
Daniel Dunbar73d66602008-09-10 07:04:09 +00001256 ResultType = RetAI.getCoerceToType();
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001257 break;
1258 }
1259
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001260 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1261 ie = FI.arg_end(); it != ie; ++it) {
1262 const ABIArgInfo &AI = it->info;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001263
1264 switch (AI.getKind()) {
Daniel Dunbar1358b202009-01-26 21:26:08 +00001265 case ABIArgInfo::Ignore:
1266 break;
1267
Daniel Dunbar04d35782008-09-17 00:51:38 +00001268 case ABIArgInfo::Coerce:
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001269 ArgTys.push_back(AI.getCoerceToType());
1270 break;
1271
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001272 case ABIArgInfo::Indirect: {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001273 // indirect arguments are always on the stack, which is addr space #0.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001274 const llvm::Type *LTy = ConvertTypeForMem(it->type);
1275 ArgTys.push_back(llvm::PointerType::getUnqual(LTy));
Daniel Dunbar22e30052008-09-11 01:48:57 +00001276 break;
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001277 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001278
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001279 case ABIArgInfo::Direct:
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001280 ArgTys.push_back(ConvertType(it->type));
Daniel Dunbar22e30052008-09-11 01:48:57 +00001281 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001282
1283 case ABIArgInfo::Expand:
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001284 GetExpandedTypes(it->type, ArgTys);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001285 break;
1286 }
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001287 }
1288
Daniel Dunbar9fc15a82009-02-02 21:43:58 +00001289 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar49f5a0d2008-09-09 23:48:28 +00001290}
1291
Daniel Dunbar0b37ca82009-02-02 23:43:58 +00001292void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001293 const Decl *TargetDecl,
Devang Patela85a9ef2008-09-25 21:02:23 +00001294 AttributeListType &PAL) {
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001295 unsigned FuncAttrs = 0;
Devang Patel2bb6eb82008-09-26 22:53:57 +00001296 unsigned RetAttrs = 0;
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001297
1298 if (TargetDecl) {
1299 if (TargetDecl->getAttr<NoThrowAttr>())
Devang Patela85a9ef2008-09-25 21:02:23 +00001300 FuncAttrs |= llvm::Attribute::NoUnwind;
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001301 if (TargetDecl->getAttr<NoReturnAttr>())
Devang Patela85a9ef2008-09-25 21:02:23 +00001302 FuncAttrs |= llvm::Attribute::NoReturn;
Anders Carlssondd6791c2008-10-05 23:32:53 +00001303 if (TargetDecl->getAttr<PureAttr>())
1304 FuncAttrs |= llvm::Attribute::ReadOnly;
1305 if (TargetDecl->getAttr<ConstAttr>())
1306 FuncAttrs |= llvm::Attribute::ReadNone;
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001307 }
1308
Daniel Dunbar0b37ca82009-02-02 23:43:58 +00001309 QualType RetTy = FI.getReturnType();
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001310 unsigned Index = 1;
Daniel Dunbar77071992009-02-03 05:59:18 +00001311 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001312 switch (RetAI.getKind()) {
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001313 case ABIArgInfo::Direct:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001314 if (RetTy->isPromotableIntegerType()) {
1315 if (RetTy->isSignedIntegerType()) {
Devang Patel2bb6eb82008-09-26 22:53:57 +00001316 RetAttrs |= llvm::Attribute::SExt;
Daniel Dunbare126ab12008-09-10 02:41:04 +00001317 } else if (RetTy->isUnsignedIntegerType()) {
Devang Patel2bb6eb82008-09-26 22:53:57 +00001318 RetAttrs |= llvm::Attribute::ZExt;
Daniel Dunbare126ab12008-09-10 02:41:04 +00001319 }
1320 }
1321 break;
1322
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001323 case ABIArgInfo::Indirect:
Devang Patela85a9ef2008-09-25 21:02:23 +00001324 PAL.push_back(llvm::AttributeWithIndex::get(Index,
Daniel Dunbarebbb8f32009-01-31 02:19:00 +00001325 llvm::Attribute::StructRet |
1326 llvm::Attribute::NoAlias));
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001327 ++Index;
Daniel Dunbare126ab12008-09-10 02:41:04 +00001328 break;
1329
Daniel Dunbar1358b202009-01-26 21:26:08 +00001330 case ABIArgInfo::Ignore:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001331 case ABIArgInfo::Coerce:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001332 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001333
Daniel Dunbar22e30052008-09-11 01:48:57 +00001334 case ABIArgInfo::Expand:
1335 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001336 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001337
Devang Patel2bb6eb82008-09-26 22:53:57 +00001338 if (RetAttrs)
1339 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001340 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1341 ie = FI.arg_end(); it != ie; ++it) {
1342 QualType ParamType = it->type;
1343 const ABIArgInfo &AI = it->info;
Devang Patela85a9ef2008-09-25 21:02:23 +00001344 unsigned Attributes = 0;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001345
1346 switch (AI.getKind()) {
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001347 case ABIArgInfo::Coerce:
1348 break;
1349
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001350 case ABIArgInfo::Indirect:
Devang Patela85a9ef2008-09-25 21:02:23 +00001351 Attributes |= llvm::Attribute::ByVal;
Daniel Dunbarb3f651a2009-02-05 01:31:19 +00001352 Attributes |=
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001353 llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign());
Daniel Dunbar22e30052008-09-11 01:48:57 +00001354 break;
1355
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001356 case ABIArgInfo::Direct:
Daniel Dunbar22e30052008-09-11 01:48:57 +00001357 if (ParamType->isPromotableIntegerType()) {
1358 if (ParamType->isSignedIntegerType()) {
Devang Patela85a9ef2008-09-25 21:02:23 +00001359 Attributes |= llvm::Attribute::SExt;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001360 } else if (ParamType->isUnsignedIntegerType()) {
Devang Patela85a9ef2008-09-25 21:02:23 +00001361 Attributes |= llvm::Attribute::ZExt;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001362 }
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001363 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001364 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001365
Daniel Dunbar1358b202009-01-26 21:26:08 +00001366 case ABIArgInfo::Ignore:
1367 // Skip increment, no matching LLVM parameter.
1368 continue;
1369
Daniel Dunbar04d35782008-09-17 00:51:38 +00001370 case ABIArgInfo::Expand: {
1371 std::vector<const llvm::Type*> Tys;
1372 // FIXME: This is rather inefficient. Do we ever actually need
1373 // to do anything here? The result should be just reconstructed
1374 // on the other side, so extension should be a non-issue.
1375 getTypes().GetExpandedTypes(ParamType, Tys);
1376 Index += Tys.size();
1377 continue;
1378 }
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001379 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001380
Devang Patela85a9ef2008-09-25 21:02:23 +00001381 if (Attributes)
1382 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar04d35782008-09-17 00:51:38 +00001383 ++Index;
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001384 }
Devang Patel2bb6eb82008-09-26 22:53:57 +00001385 if (FuncAttrs)
1386 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
1387
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001388}
1389
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001390void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
1391 llvm::Function *Fn,
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001392 const FunctionArgList &Args) {
Daniel Dunbar5b7ac652009-02-03 06:02:10 +00001393 // FIXME: We no longer need the types from FunctionArgList; lift up
1394 // and simplify.
1395
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001396 // Emit allocs for param decls. Give the LLVM Argument nodes names.
1397 llvm::Function::arg_iterator AI = Fn->arg_begin();
1398
1399 // Name the struct return argument.
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001400 if (CGM.ReturnTypeUsesSret(FI)) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001401 AI->setName("agg.result");
1402 ++AI;
1403 }
Daniel Dunbar77071992009-02-03 05:59:18 +00001404
Daniel Dunbar14c884a2009-02-04 21:17:21 +00001405 assert(FI.arg_size() == Args.size() &&
1406 "Mismatch between function signature & arguments.");
Daniel Dunbar77071992009-02-03 05:59:18 +00001407 CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001408 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbar77071992009-02-03 05:59:18 +00001409 i != e; ++i, ++info_it) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001410 const VarDecl *Arg = i->first;
Daniel Dunbar77071992009-02-03 05:59:18 +00001411 QualType Ty = info_it->type;
1412 const ABIArgInfo &ArgI = info_it->info;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001413
1414 switch (ArgI.getKind()) {
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001415 case ABIArgInfo::Indirect: {
1416 llvm::Value* V = AI;
1417 if (hasAggregateLLVMType(Ty)) {
1418 // Do nothing, aggregates and complex variables are accessed by
1419 // reference.
1420 } else {
1421 // Load scalar value from indirect argument.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001422 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001423 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1424 // This must be a promotion, for something like
1425 // "void a(x) short x; {..."
1426 V = EmitScalarConversion(V, Ty, Arg->getType());
1427 }
1428 }
1429 EmitParmDecl(*Arg, V);
1430 break;
1431 }
1432
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001433 case ABIArgInfo::Direct: {
Daniel Dunbar22e30052008-09-11 01:48:57 +00001434 assert(AI != Fn->arg_end() && "Argument mismatch!");
1435 llvm::Value* V = AI;
Daniel Dunbarcc811502009-02-05 11:13:54 +00001436 if (hasAggregateLLVMType(Ty)) {
1437 // Create a temporary alloca to hold the argument; the rest of
1438 // codegen expects to access aggregates & complex values by
1439 // reference.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001440 V = CreateTempAlloca(ConvertTypeForMem(Ty));
Daniel Dunbarcc811502009-02-05 11:13:54 +00001441 Builder.CreateStore(AI, V);
1442 } else {
1443 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1444 // This must be a promotion, for something like
1445 // "void a(x) short x; {..."
1446 V = EmitScalarConversion(V, Ty, Arg->getType());
1447 }
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001448 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001449 EmitParmDecl(*Arg, V);
1450 break;
1451 }
Daniel Dunbar04d35782008-09-17 00:51:38 +00001452
1453 case ABIArgInfo::Expand: {
Daniel Dunbar77071992009-02-03 05:59:18 +00001454 // If this structure was expanded into multiple arguments then
Daniel Dunbar04d35782008-09-17 00:51:38 +00001455 // we need to create a temporary and reconstruct it from the
1456 // arguments.
Chris Lattner6c5ec622008-11-24 04:00:27 +00001457 std::string Name = Arg->getNameAsString();
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001458 llvm::Value *Temp = CreateTempAlloca(ConvertTypeForMem(Ty),
Daniel Dunbar04d35782008-09-17 00:51:38 +00001459 (Name + ".addr").c_str());
1460 // FIXME: What are the right qualifiers here?
1461 llvm::Function::arg_iterator End =
1462 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI);
1463 EmitParmDecl(*Arg, Temp);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001464
Daniel Dunbar04d35782008-09-17 00:51:38 +00001465 // Name the arguments used in expansion and increment AI.
1466 unsigned Index = 0;
1467 for (; AI != End; ++AI, ++Index)
1468 AI->setName(Name + "." + llvm::utostr(Index));
1469 continue;
1470 }
Daniel Dunbar1358b202009-01-26 21:26:08 +00001471
1472 case ABIArgInfo::Ignore:
Daniel Dunbar94b4fec2009-02-10 00:06:49 +00001473 // Initialize the local variable appropriately.
1474 if (hasAggregateLLVMType(Ty)) {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001475 EmitParmDecl(*Arg, CreateTempAlloca(ConvertTypeForMem(Ty)));
Daniel Dunbar94b4fec2009-02-10 00:06:49 +00001476 } else {
1477 EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())));
1478 }
1479
Daniel Dunbar015bc8e2009-02-03 20:00:13 +00001480 // Skip increment, no matching LLVM parameter.
1481 continue;
Daniel Dunbar1358b202009-01-26 21:26:08 +00001482
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001483 case ABIArgInfo::Coerce: {
1484 assert(AI != Fn->arg_end() && "Argument mismatch!");
1485 // FIXME: This is very wasteful; EmitParmDecl is just going to
1486 // drop the result in a new alloca anyway, so we could just
1487 // store into that directly if we broke the abstraction down
1488 // more.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001489 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(Ty), "coerce");
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001490 CreateCoercedStore(AI, V, *this);
1491 // Match to what EmitParmDecl is expecting for this type.
Daniel Dunbar99473cd2009-02-04 07:22:24 +00001492 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001493 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar99473cd2009-02-04 07:22:24 +00001494 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1495 // This must be a promotion, for something like
1496 // "void a(x) short x; {..."
1497 V = EmitScalarConversion(V, Ty, Arg->getType());
1498 }
1499 }
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001500 EmitParmDecl(*Arg, V);
1501 break;
1502 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001503 }
Daniel Dunbar04d35782008-09-17 00:51:38 +00001504
1505 ++AI;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001506 }
1507 assert(AI == Fn->arg_end() && "Argument mismatch!");
1508}
1509
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001510void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI,
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001511 llvm::Value *ReturnValue) {
Daniel Dunbare126ab12008-09-10 02:41:04 +00001512 llvm::Value *RV = 0;
1513
1514 // Functions with no result always return void.
1515 if (ReturnValue) {
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001516 QualType RetTy = FI.getReturnType();
Daniel Dunbar77071992009-02-03 05:59:18 +00001517 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbare126ab12008-09-10 02:41:04 +00001518
1519 switch (RetAI.getKind()) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001520 case ABIArgInfo::Indirect:
Daniel Dunbar17d35372008-12-18 04:52:14 +00001521 if (RetTy->isAnyComplexType()) {
Daniel Dunbar17d35372008-12-18 04:52:14 +00001522 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
1523 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
1524 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1525 EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy);
1526 } else {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001527 EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(),
1528 false);
Daniel Dunbar17d35372008-12-18 04:52:14 +00001529 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001530 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001531
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001532 case ABIArgInfo::Direct:
Daniel Dunbarcc811502009-02-05 11:13:54 +00001533 // The internal return value temp always will have
1534 // pointer-to-return-type type.
Daniel Dunbare126ab12008-09-10 02:41:04 +00001535 RV = Builder.CreateLoad(ReturnValue);
1536 break;
1537
Daniel Dunbar1358b202009-01-26 21:26:08 +00001538 case ABIArgInfo::Ignore:
1539 break;
1540
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001541 case ABIArgInfo::Coerce:
Daniel Dunbar708d8a82009-01-27 01:36:03 +00001542 RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001543 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001544
Daniel Dunbar22e30052008-09-11 01:48:57 +00001545 case ABIArgInfo::Expand:
1546 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001547 }
1548 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001549
1550 if (RV) {
1551 Builder.CreateRet(RV);
1552 } else {
1553 Builder.CreateRetVoid();
1554 }
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001555}
1556
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001557RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
1558 llvm::Value *Callee,
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001559 const CallArgList &CallArgs) {
Daniel Dunbar5b7ac652009-02-03 06:02:10 +00001560 // FIXME: We no longer need the types from CallArgs; lift up and
1561 // simplify.
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001562 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001563
1564 // Handle struct-return functions by passing a pointer to the
1565 // location that we would like to return into.
Daniel Dunbar9fc15a82009-02-02 21:43:58 +00001566 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbar77071992009-02-03 05:59:18 +00001567 const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
Daniel Dunbar32cae462009-02-05 09:24:53 +00001568 if (CGM.ReturnTypeUsesSret(CallInfo)) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001569 // Create a temporary alloca to hold the result of the call. :(
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001570 Args.push_back(CreateTempAlloca(ConvertTypeForMem(RetTy)));
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001571 }
1572
Daniel Dunbar14c884a2009-02-04 21:17:21 +00001573 assert(CallInfo.arg_size() == CallArgs.size() &&
1574 "Mismatch between function signature & arguments.");
Daniel Dunbar77071992009-02-03 05:59:18 +00001575 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001576 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
Daniel Dunbar77071992009-02-03 05:59:18 +00001577 I != E; ++I, ++info_it) {
1578 const ABIArgInfo &ArgInfo = info_it->info;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001579 RValue RV = I->first;
Daniel Dunbar04d35782008-09-17 00:51:38 +00001580
1581 switch (ArgInfo.getKind()) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001582 case ABIArgInfo::Indirect:
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001583 if (RV.isScalar() || RV.isComplex()) {
1584 // Make a temporary alloca to pass the argument.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001585 Args.push_back(CreateTempAlloca(ConvertTypeForMem(I->second)));
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001586 if (RV.isScalar())
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001587 EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false);
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001588 else
1589 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
1590 } else {
1591 Args.push_back(RV.getAggregateAddr());
1592 }
1593 break;
1594
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001595 case ABIArgInfo::Direct:
Daniel Dunbar04d35782008-09-17 00:51:38 +00001596 if (RV.isScalar()) {
1597 Args.push_back(RV.getScalarVal());
1598 } else if (RV.isComplex()) {
Daniel Dunbarcc811502009-02-05 11:13:54 +00001599 llvm::Value *Tmp = llvm::UndefValue::get(ConvertType(I->second));
1600 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().first, 0);
1601 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().second, 1);
1602 Args.push_back(Tmp);
Daniel Dunbar04d35782008-09-17 00:51:38 +00001603 } else {
Daniel Dunbarcc811502009-02-05 11:13:54 +00001604 Args.push_back(Builder.CreateLoad(RV.getAggregateAddr()));
Daniel Dunbar04d35782008-09-17 00:51:38 +00001605 }
1606 break;
1607
Daniel Dunbar1358b202009-01-26 21:26:08 +00001608 case ABIArgInfo::Ignore:
1609 break;
1610
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001611 case ABIArgInfo::Coerce: {
1612 // FIXME: Avoid the conversion through memory if possible.
1613 llvm::Value *SrcPtr;
1614 if (RV.isScalar()) {
Daniel Dunbar4ce351b2009-02-03 23:04:57 +00001615 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001616 EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false);
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001617 } else if (RV.isComplex()) {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001618 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001619 StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
1620 } else
1621 SrcPtr = RV.getAggregateAddr();
1622 Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
1623 *this));
1624 break;
1625 }
1626
Daniel Dunbar04d35782008-09-17 00:51:38 +00001627 case ABIArgInfo::Expand:
1628 ExpandTypeToArgs(I->second, RV, Args);
1629 break;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001630 }
1631 }
1632
1633 llvm::CallInst *CI = Builder.CreateCall(Callee,&Args[0],&Args[0]+Args.size());
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001634
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001635 // FIXME: Provide TargetDecl so nounwind, noreturn, etc, etc get set.
Devang Patela85a9ef2008-09-25 21:02:23 +00001636 CodeGen::AttributeListType AttributeList;
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001637 CGM.ConstructAttributeList(CallInfo, 0, AttributeList);
Devang Patela85a9ef2008-09-25 21:02:23 +00001638 CI->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(),
Daniel Dunbarebbb8f32009-01-31 02:19:00 +00001639 AttributeList.size()));
1640
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001641 if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee))
1642 CI->setCallingConv(F->getCallingConv());
1643 if (CI->getType() != llvm::Type::VoidTy)
1644 CI->setName("call");
Daniel Dunbare126ab12008-09-10 02:41:04 +00001645
1646 switch (RetAI.getKind()) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001647 case ABIArgInfo::Indirect:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001648 if (RetTy->isAnyComplexType())
Daniel Dunbar04d35782008-09-17 00:51:38 +00001649 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Daniel Dunbar17d35372008-12-18 04:52:14 +00001650 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar04d35782008-09-17 00:51:38 +00001651 return RValue::getAggregate(Args[0]);
Daniel Dunbar17d35372008-12-18 04:52:14 +00001652 else
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001653 return RValue::get(EmitLoadOfScalar(Args[0], false, RetTy));
Daniel Dunbar22e30052008-09-11 01:48:57 +00001654
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001655 case ABIArgInfo::Direct:
Daniel Dunbarcc811502009-02-05 11:13:54 +00001656 if (RetTy->isAnyComplexType()) {
1657 llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
1658 llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
1659 return RValue::getComplex(std::make_pair(Real, Imag));
1660 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001661 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(RetTy), "agg.tmp");
Daniel Dunbarcc811502009-02-05 11:13:54 +00001662 Builder.CreateStore(CI, V);
1663 return RValue::getAggregate(V);
1664 } else
1665 return RValue::get(CI);
Daniel Dunbare126ab12008-09-10 02:41:04 +00001666
Daniel Dunbar1358b202009-01-26 21:26:08 +00001667 case ABIArgInfo::Ignore:
Daniel Dunbareec02622009-02-03 06:30:17 +00001668 // If we are ignoring an argument that had a result, make sure to
1669 // construct the appropriate return value for our caller.
Daniel Dunbar900c85a2009-02-05 07:09:07 +00001670 return GetUndefRValue(RetTy);
Daniel Dunbar1358b202009-01-26 21:26:08 +00001671
Daniel Dunbar73d66602008-09-10 07:04:09 +00001672 case ABIArgInfo::Coerce: {
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001673 // FIXME: Avoid the conversion through memory if possible.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001674 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(RetTy), "coerce");
Daniel Dunbar708d8a82009-01-27 01:36:03 +00001675 CreateCoercedStore(CI, V, *this);
Anders Carlssonfccf7472008-11-25 22:21:48 +00001676 if (RetTy->isAnyComplexType())
1677 return RValue::getComplex(LoadComplexFromAddr(V, false));
Daniel Dunbar1358b202009-01-26 21:26:08 +00001678 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssonfccf7472008-11-25 22:21:48 +00001679 return RValue::getAggregate(V);
Daniel Dunbar1358b202009-01-26 21:26:08 +00001680 else
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001681 return RValue::get(EmitLoadOfScalar(V, false, RetTy));
Daniel Dunbar73d66602008-09-10 07:04:09 +00001682 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001683
Daniel Dunbar22e30052008-09-11 01:48:57 +00001684 case ABIArgInfo::Expand:
1685 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001686 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001687
1688 assert(0 && "Unhandled ABIArgInfo::Kind");
1689 return RValue::get(0);
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001690}
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001691
1692/* VarArg handling */
1693
1694llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) {
1695 return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this);
1696}