blob: 22afb5d5487c74f9aff06bb7fc62018a24f19bd7 [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 Dunbar6f7279b2009-02-04 23:24:38 +000026#include "llvm/Support/raw_ostream.h"
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +000027#include "llvm/Target/TargetData.h"
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +000028
29#include "ABIInfo.h"
30
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000031using namespace clang;
32using namespace CodeGen;
33
34/***/
35
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000036// FIXME: Use iterator and sidestep silly type array creation.
37
Daniel Dunbar541b63b2009-02-02 23:23:47 +000038const
39CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionTypeNoProto *FTNP) {
40 return getFunctionInfo(FTNP->getResultType(),
41 llvm::SmallVector<QualType, 16>());
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000042}
43
Daniel Dunbar541b63b2009-02-02 23:23:47 +000044const
45CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionTypeProto *FTP) {
46 llvm::SmallVector<QualType, 16> ArgTys;
47 // FIXME: Kill copy.
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000048 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +000049 ArgTys.push_back(FTP->getArgType(i));
50 return getFunctionInfo(FTP->getResultType(), ArgTys);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000051}
52
Daniel Dunbar541b63b2009-02-02 23:23:47 +000053const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionDecl *FD) {
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000054 const FunctionType *FTy = FD->getType()->getAsFunctionType();
Daniel Dunbar541b63b2009-02-02 23:23:47 +000055 if (const FunctionTypeProto *FTP = dyn_cast<FunctionTypeProto>(FTy))
56 return getFunctionInfo(FTP);
57 return getFunctionInfo(cast<FunctionTypeNoProto>(FTy));
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000058}
59
Daniel Dunbar541b63b2009-02-02 23:23:47 +000060const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const ObjCMethodDecl *MD) {
61 llvm::SmallVector<QualType, 16> ArgTys;
62 ArgTys.push_back(MD->getSelfDecl()->getType());
63 ArgTys.push_back(Context.getObjCSelType());
64 // FIXME: Kill copy?
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000065 for (ObjCMethodDecl::param_const_iterator i = MD->param_begin(),
66 e = MD->param_end(); i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +000067 ArgTys.push_back((*i)->getType());
68 return getFunctionInfo(MD->getResultType(), ArgTys);
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000069}
70
Daniel Dunbar541b63b2009-02-02 23:23:47 +000071const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
72 const CallArgList &Args) {
73 // FIXME: Kill copy.
74 llvm::SmallVector<QualType, 16> ArgTys;
Daniel Dunbar725ad312009-01-31 02:19:00 +000075 for (CallArgList::const_iterator i = Args.begin(), e = Args.end();
76 i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +000077 ArgTys.push_back(i->second);
78 return getFunctionInfo(ResTy, ArgTys);
Daniel Dunbar725ad312009-01-31 02:19:00 +000079}
80
Daniel Dunbar541b63b2009-02-02 23:23:47 +000081const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
82 const FunctionArgList &Args) {
83 // FIXME: Kill copy.
84 llvm::SmallVector<QualType, 16> ArgTys;
Daniel Dunbarbb36d332009-02-02 21:43:58 +000085 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
86 i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +000087 ArgTys.push_back(i->second);
88 return getFunctionInfo(ResTy, ArgTys);
89}
90
91const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
92 const llvm::SmallVector<QualType, 16> &ArgTys) {
Daniel Dunbar40a6be62009-02-03 00:07:12 +000093 // Lookup or create unique function info.
94 llvm::FoldingSetNodeID ID;
95 CGFunctionInfo::Profile(ID, ResTy, ArgTys.begin(), ArgTys.end());
96
97 void *InsertPos = 0;
98 CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, InsertPos);
99 if (FI)
100 return *FI;
101
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000102 // Construct the function info.
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000103 FI = new CGFunctionInfo(ResTy, ArgTys);
Daniel Dunbar35e67d42009-02-05 00:00:23 +0000104 FunctionInfos.InsertNode(FI, InsertPos);
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000105
106 // Compute ABI information.
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000107 getABIInfo().computeInfo(*FI, getContext());
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000108
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000109 return *FI;
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000110}
111
112/***/
113
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000114ABIInfo::~ABIInfo() {}
115
Daniel Dunbar6f7279b2009-02-04 23:24:38 +0000116void ABIArgInfo::dump() const {
117 fprintf(stderr, "(ABIArgInfo Kind=");
118 switch (TheKind) {
119 case Direct:
120 fprintf(stderr, "Direct");
121 break;
Daniel Dunbar6f7279b2009-02-04 23:24:38 +0000122 case Ignore:
123 fprintf(stderr, "Ignore");
124 break;
125 case Coerce:
126 fprintf(stderr, "Coerce Type=");
127 getCoerceToType()->print(llvm::errs());
128 // FIXME: This is ridiculous.
129 llvm::errs().flush();
130 break;
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000131 case Indirect:
132 fprintf(stderr, "Indirect Align=%d", getIndirectAlign());
Daniel Dunbar6f7279b2009-02-04 23:24:38 +0000133 break;
134 case Expand:
135 fprintf(stderr, "Expand");
136 break;
137 }
138 fprintf(stderr, ")\n");
139}
140
141/***/
142
Daniel Dunbar834af452008-09-17 21:22:33 +0000143/// isEmptyStruct - Return true iff a structure has no non-empty
144/// members. Note that a structure with a flexible array member is not
145/// considered empty.
146static bool isEmptyStruct(QualType T) {
147 const RecordType *RT = T->getAsStructureType();
148 if (!RT)
149 return 0;
150 const RecordDecl *RD = RT->getDecl();
151 if (RD->hasFlexibleArrayMember())
152 return false;
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000153 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar834af452008-09-17 21:22:33 +0000154 e = RD->field_end(); i != e; ++i) {
155 const FieldDecl *FD = *i;
156 if (!isEmptyStruct(FD->getType()))
157 return false;
158 }
159 return true;
160}
161
162/// isSingleElementStruct - Determine if a structure is a "single
163/// element struct", i.e. it has exactly one non-empty field or
164/// exactly one field which is itself a single element
165/// struct. Structures with flexible array members are never
166/// considered single element structs.
167///
168/// \return The field declaration for the single non-empty field, if
169/// it exists.
170static const FieldDecl *isSingleElementStruct(QualType T) {
171 const RecordType *RT = T->getAsStructureType();
172 if (!RT)
173 return 0;
174
175 const RecordDecl *RD = RT->getDecl();
176 if (RD->hasFlexibleArrayMember())
177 return 0;
178
179 const FieldDecl *Found = 0;
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000180 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar834af452008-09-17 21:22:33 +0000181 e = RD->field_end(); i != e; ++i) {
182 const FieldDecl *FD = *i;
183 QualType FT = FD->getType();
184
185 if (isEmptyStruct(FT)) {
186 // Ignore
187 } else if (Found) {
188 return 0;
189 } else if (!CodeGenFunction::hasAggregateLLVMType(FT)) {
190 Found = FD;
191 } else {
192 Found = isSingleElementStruct(FT);
193 if (!Found)
194 return 0;
195 }
196 }
197
198 return Found;
199}
200
201static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
202 if (!Ty->getAsBuiltinType() && !Ty->isPointerType())
203 return false;
204
205 uint64_t Size = Context.getTypeSize(Ty);
206 return Size == 32 || Size == 64;
207}
208
209static bool areAllFields32Or64BitBasicType(const RecordDecl *RD,
210 ASTContext &Context) {
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000211 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar834af452008-09-17 21:22:33 +0000212 e = RD->field_end(); i != e; ++i) {
213 const FieldDecl *FD = *i;
214
215 if (!is32Or64BitBasicType(FD->getType(), Context))
216 return false;
217
218 // If this is a bit-field we need to make sure it is still a
219 // 32-bit or 64-bit type.
220 if (Expr *BW = FD->getBitWidth()) {
221 unsigned Width = BW->getIntegerConstantExprValue(Context).getZExtValue();
222 if (Width <= 16)
223 return false;
224 }
225 }
226 return true;
227}
228
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000229namespace {
230/// DefaultABIInfo - The default implementation for ABI specific
231/// details. This implementation provides information which results in
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000232/// self-consistent and sensible LLVM IR generation, but does not
233/// conform to any particular ABI.
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000234class DefaultABIInfo : public ABIInfo {
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000235 ABIArgInfo classifyReturnType(QualType RetTy,
236 ASTContext &Context) const;
237
238 ABIArgInfo classifyArgumentType(QualType RetTy,
239 ASTContext &Context) const;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000240
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000241 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
242 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
243 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
244 it != ie; ++it)
245 it->info = classifyArgumentType(it->type, Context);
246 }
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000247
248 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
249 CodeGenFunction &CGF) const;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000250};
251
252/// X86_32ABIInfo - The X86-32 ABI information.
253class X86_32ABIInfo : public ABIInfo {
254public:
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000255 ABIArgInfo classifyReturnType(QualType RetTy,
256 ASTContext &Context) const;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000257
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000258 ABIArgInfo classifyArgumentType(QualType RetTy,
259 ASTContext &Context) const;
260
261 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
262 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
263 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
264 it != ie; ++it)
265 it->info = classifyArgumentType(it->type, Context);
266 }
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000267
268 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
269 CodeGenFunction &CGF) const;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000270};
271}
272
273ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
274 ASTContext &Context) const {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000275 if (RetTy->isVoidType()) {
276 return ABIArgInfo::getIgnore();
277 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar834af452008-09-17 21:22:33 +0000278 // Classify "single element" structs as their element type.
279 const FieldDecl *SeltFD = isSingleElementStruct(RetTy);
280 if (SeltFD) {
281 QualType SeltTy = SeltFD->getType()->getDesugaredType();
282 if (const BuiltinType *BT = SeltTy->getAsBuiltinType()) {
283 // FIXME: This is gross, it would be nice if we could just
284 // pass back SeltTy and have clients deal with it. Is it worth
285 // supporting coerce to both LLVM and clang Types?
286 if (BT->isIntegerType()) {
287 uint64_t Size = Context.getTypeSize(SeltTy);
288 return ABIArgInfo::getCoerce(llvm::IntegerType::get((unsigned) Size));
289 } else if (BT->getKind() == BuiltinType::Float) {
290 return ABIArgInfo::getCoerce(llvm::Type::FloatTy);
291 } else if (BT->getKind() == BuiltinType::Double) {
292 return ABIArgInfo::getCoerce(llvm::Type::DoubleTy);
293 }
294 } else if (SeltTy->isPointerType()) {
295 // FIXME: It would be really nice if this could come out as
296 // the proper pointer type.
297 llvm::Type *PtrTy =
298 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
299 return ABIArgInfo::getCoerce(PtrTy);
300 }
301 }
302
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000303 uint64_t Size = Context.getTypeSize(RetTy);
304 if (Size == 8) {
305 return ABIArgInfo::getCoerce(llvm::Type::Int8Ty);
306 } else if (Size == 16) {
307 return ABIArgInfo::getCoerce(llvm::Type::Int16Ty);
308 } else if (Size == 32) {
309 return ABIArgInfo::getCoerce(llvm::Type::Int32Ty);
310 } else if (Size == 64) {
311 return ABIArgInfo::getCoerce(llvm::Type::Int64Ty);
312 } else {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000313 return ABIArgInfo::getIndirect(0);
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000314 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000315 } else {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000316 return ABIArgInfo::getDirect();
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000317 }
318}
319
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000320ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000321 ASTContext &Context) const {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000322 // FIXME: Set alignment on indirect arguments.
Daniel Dunbarf0357382008-09-17 20:11:04 +0000323 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000324 // Structures with flexible arrays are always indirect.
Daniel Dunbar834af452008-09-17 21:22:33 +0000325 if (const RecordType *RT = Ty->getAsStructureType())
326 if (RT->getDecl()->hasFlexibleArrayMember())
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000327 return ABIArgInfo::getIndirect(0);
Daniel Dunbar834af452008-09-17 21:22:33 +0000328
Daniel Dunbar3170c932009-02-05 01:50:07 +0000329 // Ignore empty structs.
Daniel Dunbar834af452008-09-17 21:22:33 +0000330 uint64_t Size = Context.getTypeSize(Ty);
331 if (Ty->isStructureType() && Size == 0)
Daniel Dunbar3170c932009-02-05 01:50:07 +0000332 return ABIArgInfo::getIgnore();
Daniel Dunbar834af452008-09-17 21:22:33 +0000333
334 // Expand structs with size <= 128-bits which consist only of
335 // basic types (int, long long, float, double, xxx*). This is
336 // non-recursive and does not ignore empty fields.
337 if (const RecordType *RT = Ty->getAsStructureType()) {
338 if (Context.getTypeSize(Ty) <= 4*32 &&
339 areAllFields32Or64BitBasicType(RT->getDecl(), Context))
340 return ABIArgInfo::getExpand();
341 }
342
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000343 return ABIArgInfo::getIndirect(0);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000344 } else {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000345 return ABIArgInfo::getDirect();
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000346 }
347}
348
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000349llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
350 CodeGenFunction &CGF) const {
351 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
352 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
353
354 CGBuilderTy &Builder = CGF.Builder;
355 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
356 "ap");
357 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
358 llvm::Type *PTy =
359 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
360 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
361
362 uint64_t SizeInBytes = CGF.getContext().getTypeSize(Ty) / 8;
363 const unsigned ArgumentSizeInBytes = 4;
364 if (SizeInBytes < ArgumentSizeInBytes)
365 SizeInBytes = ArgumentSizeInBytes;
366
367 llvm::Value *NextAddr =
368 Builder.CreateGEP(Addr,
369 llvm::ConstantInt::get(llvm::Type::Int32Ty, SizeInBytes),
370 "ap.next");
371 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
372
373 return AddrTyped;
374}
375
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000376namespace {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000377/// X86_64ABIInfo - The X86_64 ABI information.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000378class X86_64ABIInfo : public ABIInfo {
379 enum Class {
380 Integer = 0,
381 SSE,
382 SSEUp,
383 X87,
384 X87Up,
385 ComplexX87,
386 NoClass,
387 Memory
388 };
389
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000390 /// merge - Implement the X86_64 ABI merging algorithm.
391 ///
Daniel Dunbarc4503572009-01-31 00:06:58 +0000392 /// Merge an accumulating classification \arg Accum with a field
393 /// classification \arg Field.
394 ///
395 /// \param Accum - The accumulating classification. This should
396 /// always be either NoClass or the result of a previous merge
397 /// call. In addition, this should never be Memory (the caller
398 /// should just return Memory for the aggregate).
399 Class merge(Class Accum, Class Field) const;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000400
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000401 /// classify - Determine the x86_64 register classes in which the
402 /// given type T should be passed.
403 ///
Daniel Dunbarc4503572009-01-31 00:06:58 +0000404 /// \param Lo - The classification for the parts of the type
405 /// residing in the low word of the containing object.
406 ///
407 /// \param Hi - The classification for the parts of the type
408 /// residing in the high word of the containing object.
409 ///
410 /// \param OffsetBase - The bit offset of this type in the
Daniel Dunbarcdf920e2009-01-30 22:40:15 +0000411 /// containing object. Some parameters are classified different
412 /// depending on whether they straddle an eightbyte boundary.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000413 ///
414 /// If a word is unused its result will be NoClass; if a type should
415 /// be passed in Memory then at least the classification of \arg Lo
416 /// will be Memory.
417 ///
418 /// The \arg Lo class will be NoClass iff the argument is ignored.
419 ///
420 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
421 /// be NoClass.
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000422 void classify(QualType T, ASTContext &Context, uint64_t OffsetBase,
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000423 Class &Lo, Class &Hi) const;
Daniel Dunbarc4503572009-01-31 00:06:58 +0000424
Daniel Dunbar6bad2652009-02-03 06:51:18 +0000425 ABIArgInfo classifyReturnType(QualType RetTy,
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000426 ASTContext &Context) const;
427
428 ABIArgInfo classifyArgumentType(QualType Ty,
429 ASTContext &Context,
Daniel Dunbar3b4e9cd2009-02-10 17:06:09 +0000430 unsigned &neededInt,
431 unsigned &neededSSE) const;
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000432
433public:
434 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const;
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000435
436 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
437 CodeGenFunction &CGF) const;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000438};
439}
440
Daniel Dunbarc4503572009-01-31 00:06:58 +0000441X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum,
442 Class Field) const {
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000443 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
444 // classified recursively so that always two fields are
445 // considered. The resulting class is calculated according to
446 // the classes of the fields in the eightbyte:
447 //
448 // (a) If both classes are equal, this is the resulting class.
449 //
450 // (b) If one of the classes is NO_CLASS, the resulting class is
451 // the other class.
452 //
453 // (c) If one of the classes is MEMORY, the result is the MEMORY
454 // class.
455 //
456 // (d) If one of the classes is INTEGER, the result is the
457 // INTEGER.
458 //
459 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
460 // MEMORY is used as class.
461 //
462 // (f) Otherwise class SSE is used.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000463 assert((Accum == NoClass || Accum == Integer ||
464 Accum == SSE || Accum == SSEUp) &&
465 "Invalid accumulated classification during merge.");
466 if (Accum == Field || Field == NoClass)
467 return Accum;
468 else if (Field == Memory)
469 return Memory;
470 else if (Accum == NoClass)
471 return Field;
472 else if (Accum == Integer || Field == Integer)
473 return Integer;
474 else if (Field == X87 || Field == X87Up || Field == ComplexX87)
475 return Memory;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000476 else
Daniel Dunbarc4503572009-01-31 00:06:58 +0000477 return SSE;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000478}
479
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000480void X86_64ABIInfo::classify(QualType Ty,
481 ASTContext &Context,
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000482 uint64_t OffsetBase,
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000483 Class &Lo, Class &Hi) const {
Daniel Dunbar9a82b522009-02-02 18:06:39 +0000484 // FIXME: This code can be simplified by introducing a simple value
485 // class for Class pairs with appropriate constructor methods for
486 // the various situations.
487
Daniel Dunbarc4503572009-01-31 00:06:58 +0000488 Lo = Hi = NoClass;
489
490 Class &Current = OffsetBase < 64 ? Lo : Hi;
491 Current = Memory;
492
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000493 if (const BuiltinType *BT = Ty->getAsBuiltinType()) {
494 BuiltinType::Kind k = BT->getKind();
495
Daniel Dunbar11434922009-01-26 21:26:08 +0000496 if (k == BuiltinType::Void) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000497 Current = NoClass;
Daniel Dunbar11434922009-01-26 21:26:08 +0000498 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000499 Current = Integer;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000500 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000501 Current = SSE;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000502 } else if (k == BuiltinType::LongDouble) {
503 Lo = X87;
504 Hi = X87Up;
505 }
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000506 // FIXME: _Decimal32 and _Decimal64 are SSE.
507 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000508 // FIXME: __int128 is (Integer, Integer).
509 } else if (Ty->isPointerLikeType() || Ty->isBlockPointerType() ||
510 Ty->isObjCQualifiedInterfaceType()) {
Daniel Dunbarc4503572009-01-31 00:06:58 +0000511 Current = Integer;
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000512 } else if (const VectorType *VT = Ty->getAsVectorType()) {
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000513 uint64_t Size = Context.getTypeSize(VT);
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000514 if (Size == 64) {
Daniel Dunbard4cd1b02009-01-30 19:38:39 +0000515 // gcc passes <1 x double> in memory.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000516 if (VT->getElementType() == Context.DoubleTy)
Daniel Dunbard4cd1b02009-01-30 19:38:39 +0000517 return;
Daniel Dunbard4cd1b02009-01-30 19:38:39 +0000518
Daniel Dunbarc4503572009-01-31 00:06:58 +0000519 Current = SSE;
Daniel Dunbare33edf12009-01-30 18:40:10 +0000520
521 // If this type crosses an eightbyte boundary, it should be
522 // split.
Daniel Dunbarcdf920e2009-01-30 22:40:15 +0000523 if (OffsetBase && OffsetBase != 64)
Daniel Dunbare33edf12009-01-30 18:40:10 +0000524 Hi = Lo;
Daniel Dunbar7a6605d2009-01-27 02:01:34 +0000525 } else if (Size == 128) {
526 Lo = SSE;
527 Hi = SSEUp;
528 }
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000529 } else if (const ComplexType *CT = Ty->getAsComplexType()) {
530 QualType ET = CT->getElementType();
531
Daniel Dunbare33edf12009-01-30 18:40:10 +0000532 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbareac48dc2009-01-29 07:22:20 +0000533 if (ET->isIntegerType()) {
Daniel Dunbareac48dc2009-01-29 07:22:20 +0000534 if (Size <= 64)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000535 Current = Integer;
Daniel Dunbareac48dc2009-01-29 07:22:20 +0000536 else if (Size <= 128)
537 Lo = Hi = Integer;
538 } else if (ET == Context.FloatTy)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000539 Current = SSE;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000540 else if (ET == Context.DoubleTy)
541 Lo = Hi = SSE;
542 else if (ET == Context.LongDoubleTy)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000543 Current = ComplexX87;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000544
545 // If this complex type crosses an eightbyte boundary then it
546 // should be split.
Daniel Dunbarcdf920e2009-01-30 22:40:15 +0000547 uint64_t EB_Real = (OffsetBase) / 64;
548 uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000549 if (Hi == NoClass && EB_Real != EB_Imag)
550 Hi = Lo;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000551 } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
552 // Arrays are treated like structures.
553
554 uint64_t Size = Context.getTypeSize(Ty);
555
556 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
557 // than two eightbytes, ..., it has class MEMORY.
558 if (Size > 128)
559 return;
560
561 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
562 // fields, it has class MEMORY.
563 //
564 // Only need to check alignment of array base.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000565 if (OffsetBase % Context.getTypeAlign(AT->getElementType()))
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000566 return;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000567
568 // Otherwise implement simplified merge. We could be smarter about
569 // this, but it isn't worth it and would be harder to verify.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000570 Current = NoClass;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000571 uint64_t EltSize = Context.getTypeSize(AT->getElementType());
572 uint64_t ArraySize = AT->getSize().getZExtValue();
573 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
574 Class FieldLo, FieldHi;
575 classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbarc4503572009-01-31 00:06:58 +0000576 Lo = merge(Lo, FieldLo);
577 Hi = merge(Hi, FieldHi);
578 if (Lo == Memory || Hi == Memory)
579 break;
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000580 }
Daniel Dunbarc4503572009-01-31 00:06:58 +0000581
582 // Do post merger cleanup (see below). Only case we worry about is Memory.
583 if (Hi == Memory)
584 Lo = Memory;
585 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Daniel Dunbar99037e52009-01-29 08:13:58 +0000586 } else if (const RecordType *RT = Ty->getAsRecordType()) {
Daniel Dunbare620ecd2009-01-30 00:47:38 +0000587 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbar99037e52009-01-29 08:13:58 +0000588
589 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
590 // than two eightbytes, ..., it has class MEMORY.
591 if (Size > 128)
592 return;
593
594 const RecordDecl *RD = RT->getDecl();
595
596 // Assume variable sized types are passed in memory.
597 if (RD->hasFlexibleArrayMember())
598 return;
599
600 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
601
602 // Reset Lo class, this will be recomputed.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000603 Current = NoClass;
Daniel Dunbar99037e52009-01-29 08:13:58 +0000604 unsigned idx = 0;
605 for (RecordDecl::field_iterator i = RD->field_begin(),
606 e = RD->field_end(); i != e; ++i, ++idx) {
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000607 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Daniel Dunbar99037e52009-01-29 08:13:58 +0000608
Daniel Dunbar8562ae72009-01-30 08:09:32 +0000609 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
610 // fields, it has class MEMORY.
Daniel Dunbar99037e52009-01-29 08:13:58 +0000611 if (Offset % Context.getTypeAlign(i->getType())) {
612 Lo = Memory;
613 return;
614 }
615
Daniel Dunbar99037e52009-01-29 08:13:58 +0000616 // Classify this field.
Daniel Dunbarc4503572009-01-31 00:06:58 +0000617 //
618 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
619 // exceeds a single eightbyte, each is classified
620 // separately. Each eightbyte gets initialized to class
621 // NO_CLASS.
Daniel Dunbar99037e52009-01-29 08:13:58 +0000622 Class FieldLo, FieldHi;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000623 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbarc4503572009-01-31 00:06:58 +0000624 Lo = merge(Lo, FieldLo);
625 Hi = merge(Hi, FieldHi);
626 if (Lo == Memory || Hi == Memory)
627 break;
Daniel Dunbar99037e52009-01-29 08:13:58 +0000628 }
629
630 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
631 //
632 // (a) If one of the classes is MEMORY, the whole argument is
633 // passed in memory.
634 //
635 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
636
637 // The first of these conditions is guaranteed by how we implement
Daniel Dunbarc4503572009-01-31 00:06:58 +0000638 // the merge (just bail).
639 //
640 // The second condition occurs in the case of unions; for example
641 // union { _Complex double; unsigned; }.
642 if (Hi == Memory)
643 Lo = Memory;
Daniel Dunbar99037e52009-01-29 08:13:58 +0000644 if (Hi == SSEUp && Lo != SSE)
Daniel Dunbarc4503572009-01-31 00:06:58 +0000645 Hi = SSE;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000646 }
647}
648
Daniel Dunbarc4503572009-01-31 00:06:58 +0000649
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000650ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
651 ASTContext &Context) const {
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000652 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
653 // classification algorithm.
654 X86_64ABIInfo::Class Lo, Hi;
Daniel Dunbarf04d69b2009-01-29 09:42:07 +0000655 classify(RetTy, Context, 0, Lo, Hi);
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000656
Daniel Dunbarc4503572009-01-31 00:06:58 +0000657 // Check some invariants.
658 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
659 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
660 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
661
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000662 const llvm::Type *ResType = 0;
663 switch (Lo) {
664 case NoClass:
Daniel Dunbar11434922009-01-26 21:26:08 +0000665 return ABIArgInfo::getIgnore();
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000666
667 case SSEUp:
668 case X87Up:
669 assert(0 && "Invalid classification for lo word.");
670
Daniel Dunbarc4503572009-01-31 00:06:58 +0000671 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000672 // hidden argument.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000673 case Memory:
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000674 return ABIArgInfo::getIndirect(0);
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000675
676 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
677 // available register of the sequence %rax, %rdx is used.
678 case Integer:
679 ResType = llvm::Type::Int64Ty; break;
680
681 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
682 // available SSE register of the sequence %xmm0, %xmm1 is used.
683 case SSE:
684 ResType = llvm::Type::DoubleTy; break;
685
686 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
687 // returned on the X87 stack in %st0 as 80-bit x87 number.
688 case X87:
689 ResType = llvm::Type::X86_FP80Ty; break;
690
Daniel Dunbarc4503572009-01-31 00:06:58 +0000691 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
692 // part of the value is returned in %st0 and the imaginary part in
693 // %st1.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000694 case ComplexX87:
695 assert(Hi == NoClass && "Unexpected ComplexX87 classification.");
696 ResType = llvm::VectorType::get(llvm::Type::X86_FP80Ty, 2);
697 break;
698 }
699
700 switch (Hi) {
701 // Memory was handled previously, and ComplexX87 and X87 should
702 // never occur as hi classes.
703 case Memory:
704 case X87:
705 case ComplexX87:
706 assert(0 && "Invalid classification for hi word.");
707
708 case NoClass: break;
709 case Integer:
Daniel Dunbarb0e14f22009-01-29 07:36:07 +0000710 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
711 break;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000712 case SSE:
Daniel Dunbarb0e14f22009-01-29 07:36:07 +0000713 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
714 break;
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000715
716 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
717 // is passed in the upper half of the last used SSE register.
718 //
719 // SSEUP should always be preceeded by SSE, just widen.
720 case SSEUp:
721 assert(Lo == SSE && "Unexpected SSEUp classification.");
722 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
723 break;
724
725 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
Daniel Dunbarb0e14f22009-01-29 07:36:07 +0000726 // returned together with the previous X87 value in %st0.
Daniel Dunbar6f3e7fa2009-01-24 08:32:22 +0000727 //
728 // X87UP should always be preceeded by X87, so we don't need to do
729 // anything here.
730 case X87Up:
731 assert(Lo == X87 && "Unexpected X87Up classification.");
732 break;
733 }
734
735 return ABIArgInfo::getCoerce(ResType);
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000736}
737
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000738ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, ASTContext &Context,
Daniel Dunbar3b4e9cd2009-02-10 17:06:09 +0000739 unsigned &neededInt,
740 unsigned &neededSSE) const {
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000741 X86_64ABIInfo::Class Lo, Hi;
742 classify(Ty, Context, 0, Lo, Hi);
743
744 // Check some invariants.
745 // FIXME: Enforce these by construction.
746 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
747 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
748 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
749
Daniel Dunbar3b4e9cd2009-02-10 17:06:09 +0000750 neededInt = 0;
751 neededSSE = 0;
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000752 const llvm::Type *ResType = 0;
753 switch (Lo) {
754 case NoClass:
755 return ABIArgInfo::getIgnore();
756
757 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
758 // on the stack.
759 case Memory:
760
761 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
762 // COMPLEX_X87, it is passed in memory.
763 case X87:
764 case ComplexX87:
765 // Choose appropriate in memory type.
766 if (CodeGenFunction::hasAggregateLLVMType(Ty))
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000767 return ABIArgInfo::getIndirect(0);
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000768 else
769 return ABIArgInfo::getDirect();
770
771 case SSEUp:
772 case X87Up:
773 assert(0 && "Invalid classification for lo word.");
774
775 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
776 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
777 // and %r9 is used.
778 case Integer:
779 ++neededInt;
780 ResType = llvm::Type::Int64Ty;
781 break;
782
783 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
784 // available SSE register is used, the registers are taken in the
785 // order from %xmm0 to %xmm7.
786 case SSE:
787 ++neededSSE;
788 ResType = llvm::Type::DoubleTy;
789 break;
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000790 }
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000791
792 switch (Hi) {
793 // Memory was handled previously, ComplexX87 and X87 should
794 // never occur as hi classes, and X87Up must be preceed by X87,
795 // which is passed in memory.
796 case Memory:
797 case X87:
798 case X87Up:
799 case ComplexX87:
800 assert(0 && "Invalid classification for hi word.");
801
802 case NoClass: break;
803 case Integer:
804 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
805 ++neededInt;
806 break;
807 case SSE:
808 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
809 ++neededSSE;
810 break;
811
812 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
813 // eightbyte is passed in the upper half of the last used SSE
814 // register.
815 case SSEUp:
816 assert(Lo == SSE && "Unexpected SSEUp classification.");
817 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
818 break;
819 }
820
Daniel Dunbar3b4e9cd2009-02-10 17:06:09 +0000821 return ABIArgInfo::getCoerce(ResType);
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000822}
823
824void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
825 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
826
827 // Keep track of the number of assigned registers.
828 unsigned freeIntRegs = 6, freeSSERegs = 8;
829
830 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
831 // get assigned (in left-to-right order) for passing as follows...
832 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Daniel Dunbar3b4e9cd2009-02-10 17:06:09 +0000833 it != ie; ++it) {
834 unsigned neededInt, neededSSE;
835 it->info = classifyArgumentType(it->type, Context, neededInt, neededSSE);
836
837 // AMD64-ABI 3.2.3p3: If there are no registers available for any
838 // eightbyte of an argument, the whole argument is passed on the
839 // stack. If registers have already been assigned for some
840 // eightbytes of such an argument, the assignments get reverted.
841 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
842 freeIntRegs -= neededInt;
843 freeSSERegs -= neededSSE;
844 } else {
845 // Choose appropriate in memory type.
846 if (CodeGenFunction::hasAggregateLLVMType(it->type))
847 it->info = ABIArgInfo::getIndirect(0);
848 else
849 it->info = ABIArgInfo::getDirect();
850 }
851 }
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000852}
853
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000854llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
855 CodeGenFunction &CGF) const {
856 return 0;
857}
858
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000859ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000860 ASTContext &Context) const {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000861 if (RetTy->isVoidType()) {
862 return ABIArgInfo::getIgnore();
863 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000864 return ABIArgInfo::getIndirect(0);
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000865 } else {
866 return ABIArgInfo::getDirect();
867 }
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000868}
869
870ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000871 ASTContext &Context) const {
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000872 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000873 return ABIArgInfo::getIndirect(0);
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000874 } else {
875 return ABIArgInfo::getDirect();
876 }
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000877}
878
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000879llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
880 CodeGenFunction &CGF) const {
881 return 0;
882}
883
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000884const ABIInfo &CodeGenTypes::getABIInfo() const {
885 if (TheABIInfo)
886 return *TheABIInfo;
887
888 // For now we just cache this in the CodeGenTypes and don't bother
889 // to free it.
890 const char *TargetPrefix = getContext().Target.getTargetPrefix();
891 if (strcmp(TargetPrefix, "x86") == 0) {
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000892 switch (getContext().Target.getPointerWidth(0)) {
893 case 32:
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000894 return *(TheABIInfo = new X86_32ABIInfo());
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000895 case 64:
Daniel Dunbar11a76ed2009-01-30 18:47:53 +0000896 return *(TheABIInfo = new X86_64ABIInfo());
Daniel Dunbard4edfe42009-01-15 18:18:40 +0000897 }
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000898 }
899
900 return *(TheABIInfo = new DefaultABIInfo);
901}
902
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000903/***/
904
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000905CGFunctionInfo::CGFunctionInfo(QualType ResTy,
906 const llvm::SmallVector<QualType, 16> &ArgTys) {
907 NumArgs = ArgTys.size();
908 Args = new ArgInfo[1 + NumArgs];
909 Args[0].type = ResTy;
910 for (unsigned i = 0; i < NumArgs; ++i)
911 Args[1 + i].type = ArgTys[i];
912}
913
914/***/
915
Daniel Dunbar56273772008-09-17 00:51:38 +0000916void CodeGenTypes::GetExpandedTypes(QualType Ty,
917 std::vector<const llvm::Type*> &ArgTys) {
918 const RecordType *RT = Ty->getAsStructureType();
919 assert(RT && "Can only expand structure types.");
920 const RecordDecl *RD = RT->getDecl();
921 assert(!RD->hasFlexibleArrayMember() &&
922 "Cannot expand structure with flexible array.");
923
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000924 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar56273772008-09-17 00:51:38 +0000925 e = RD->field_end(); i != e; ++i) {
926 const FieldDecl *FD = *i;
927 assert(!FD->isBitField() &&
928 "Cannot expand structure with bit-field members.");
929
930 QualType FT = FD->getType();
931 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
932 GetExpandedTypes(FT, ArgTys);
933 } else {
934 ArgTys.push_back(ConvertType(FT));
935 }
936 }
937}
938
939llvm::Function::arg_iterator
940CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
941 llvm::Function::arg_iterator AI) {
942 const RecordType *RT = Ty->getAsStructureType();
943 assert(RT && "Can only expand structure types.");
944
945 RecordDecl *RD = RT->getDecl();
946 assert(LV.isSimple() &&
947 "Unexpected non-simple lvalue during struct expansion.");
948 llvm::Value *Addr = LV.getAddress();
949 for (RecordDecl::field_iterator i = RD->field_begin(),
950 e = RD->field_end(); i != e; ++i) {
951 FieldDecl *FD = *i;
952 QualType FT = FD->getType();
953
954 // FIXME: What are the right qualifiers here?
955 LValue LV = EmitLValueForField(Addr, FD, false, 0);
956 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
957 AI = ExpandTypeFromArgs(FT, LV, AI);
958 } else {
959 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
960 ++AI;
961 }
962 }
963
964 return AI;
965}
966
967void
968CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
969 llvm::SmallVector<llvm::Value*, 16> &Args) {
970 const RecordType *RT = Ty->getAsStructureType();
971 assert(RT && "Can only expand structure types.");
972
973 RecordDecl *RD = RT->getDecl();
974 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
975 llvm::Value *Addr = RV.getAggregateAddr();
976 for (RecordDecl::field_iterator i = RD->field_begin(),
977 e = RD->field_end(); i != e; ++i) {
978 FieldDecl *FD = *i;
979 QualType FT = FD->getType();
980
981 // FIXME: What are the right qualifiers here?
982 LValue LV = EmitLValueForField(Addr, FD, false, 0);
983 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
984 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
985 } else {
986 RValue RV = EmitLoadOfLValue(LV, FT);
987 assert(RV.isScalar() &&
988 "Unexpected non-scalar rvalue during struct expansion.");
989 Args.push_back(RV.getScalarVal());
990 }
991 }
992}
993
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000994/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
995/// a pointer to an object of type \arg Ty.
996///
997/// This safely handles the case when the src type is smaller than the
998/// destination type; in this situation the values of bits which not
999/// present in the src are undefined.
1000static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
1001 const llvm::Type *Ty,
1002 CodeGenFunction &CGF) {
1003 const llvm::Type *SrcTy =
1004 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
1005 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
1006 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(Ty);
1007
Daniel Dunbarb225be42009-02-03 05:59:18 +00001008 // If load is legal, just bitcast the src pointer.
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001009 if (SrcSize == DstSize) {
1010 llvm::Value *Casted =
1011 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
Daniel Dunbar386621f2009-02-07 02:46:03 +00001012 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
1013 // FIXME: Use better alignment / avoid requiring aligned load.
1014 Load->setAlignment(1);
1015 return Load;
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001016 } else {
1017 assert(SrcSize < DstSize && "Coercion is losing source bits!");
1018
1019 // Otherwise do coercion through memory. This is stupid, but
1020 // simple.
1021 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
1022 llvm::Value *Casted =
1023 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar386621f2009-02-07 02:46:03 +00001024 llvm::StoreInst *Store =
1025 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
1026 // FIXME: Use better alignment / avoid requiring aligned store.
1027 Store->setAlignment(1);
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001028 return CGF.Builder.CreateLoad(Tmp);
1029 }
1030}
1031
1032/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
1033/// where the source and destination may have different types.
1034///
1035/// This safely handles the case when the src type is larger than the
1036/// destination type; the upper bits of the src will be lost.
1037static void CreateCoercedStore(llvm::Value *Src,
1038 llvm::Value *DstPtr,
1039 CodeGenFunction &CGF) {
1040 const llvm::Type *SrcTy = Src->getType();
1041 const llvm::Type *DstTy =
1042 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
1043
1044 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
1045 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(DstTy);
1046
Daniel Dunbar88c2fa92009-02-03 05:31:23 +00001047 // If store is legal, just bitcast the src pointer.
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001048 if (SrcSize == DstSize) {
1049 llvm::Value *Casted =
1050 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar386621f2009-02-07 02:46:03 +00001051 // FIXME: Use better alignment / avoid requiring aligned store.
1052 CGF.Builder.CreateStore(Src, Casted)->setAlignment(1);
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001053 } else {
1054 assert(SrcSize > DstSize && "Coercion is missing bits!");
1055
1056 // Otherwise do coercion through memory. This is stupid, but
1057 // simple.
1058 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
1059 CGF.Builder.CreateStore(Src, Tmp);
1060 llvm::Value *Casted =
1061 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
Daniel Dunbar386621f2009-02-07 02:46:03 +00001062 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
1063 // FIXME: Use better alignment / avoid requiring aligned load.
1064 Load->setAlignment(1);
1065 CGF.Builder.CreateStore(Load, DstPtr);
Daniel Dunbar275e10d2009-02-02 19:06:38 +00001066 }
1067}
1068
Daniel Dunbar56273772008-09-17 00:51:38 +00001069/***/
1070
Daniel Dunbar88b53962009-02-02 22:03:45 +00001071bool CodeGenModule::ReturnTypeUsesSret(const CGFunctionInfo &FI) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001072 return FI.getReturnInfo().isIndirect();
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001073}
1074
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001075const llvm::FunctionType *
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001076CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic) {
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001077 std::vector<const llvm::Type*> ArgTys;
1078
1079 const llvm::Type *ResultType = 0;
1080
Daniel Dunbara0a99e02009-02-02 23:43:58 +00001081 QualType RetTy = FI.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001082 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001083 switch (RetAI.getKind()) {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001084 case ABIArgInfo::Expand:
1085 assert(0 && "Invalid ABI kind for return argument");
1086
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001087 case ABIArgInfo::Direct:
1088 ResultType = ConvertType(RetTy);
1089 break;
1090
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001091 case ABIArgInfo::Indirect: {
1092 assert(!RetAI.getIndirectAlign() && "Align unused on indirect return.");
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001093 ResultType = llvm::Type::VoidTy;
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +00001094 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001095 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
1096 break;
1097 }
1098
Daniel Dunbar11434922009-01-26 21:26:08 +00001099 case ABIArgInfo::Ignore:
1100 ResultType = llvm::Type::VoidTy;
1101 break;
1102
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001103 case ABIArgInfo::Coerce:
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001104 ResultType = RetAI.getCoerceToType();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001105 break;
1106 }
1107
Daniel Dunbar88c2fa92009-02-03 05:31:23 +00001108 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1109 ie = FI.arg_end(); it != ie; ++it) {
1110 const ABIArgInfo &AI = it->info;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001111
1112 switch (AI.getKind()) {
Daniel Dunbar11434922009-01-26 21:26:08 +00001113 case ABIArgInfo::Ignore:
1114 break;
1115
Daniel Dunbar56273772008-09-17 00:51:38 +00001116 case ABIArgInfo::Coerce:
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001117 ArgTys.push_back(AI.getCoerceToType());
1118 break;
1119
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001120 case ABIArgInfo::Indirect: {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001121 // indirect arguments are always on the stack, which is addr space #0.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001122 const llvm::Type *LTy = ConvertTypeForMem(it->type);
1123 ArgTys.push_back(llvm::PointerType::getUnqual(LTy));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001124 break;
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001125 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001126
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001127 case ABIArgInfo::Direct:
Daniel Dunbar1f745982009-02-05 09:16:39 +00001128 ArgTys.push_back(ConvertType(it->type));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001129 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001130
1131 case ABIArgInfo::Expand:
Daniel Dunbar88c2fa92009-02-03 05:31:23 +00001132 GetExpandedTypes(it->type, ArgTys);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001133 break;
1134 }
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001135 }
1136
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001137 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar3913f182008-09-09 23:48:28 +00001138}
1139
Daniel Dunbara0a99e02009-02-02 23:43:58 +00001140void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
Daniel Dunbar88b53962009-02-02 22:03:45 +00001141 const Decl *TargetDecl,
Devang Patel761d7f72008-09-25 21:02:23 +00001142 AttributeListType &PAL) {
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001143 unsigned FuncAttrs = 0;
Devang Patela2c69122008-09-26 22:53:57 +00001144 unsigned RetAttrs = 0;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001145
1146 if (TargetDecl) {
1147 if (TargetDecl->getAttr<NoThrowAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +00001148 FuncAttrs |= llvm::Attribute::NoUnwind;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001149 if (TargetDecl->getAttr<NoReturnAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +00001150 FuncAttrs |= llvm::Attribute::NoReturn;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001151 if (TargetDecl->getAttr<PureAttr>())
1152 FuncAttrs |= llvm::Attribute::ReadOnly;
1153 if (TargetDecl->getAttr<ConstAttr>())
1154 FuncAttrs |= llvm::Attribute::ReadNone;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001155 }
1156
Daniel Dunbara0a99e02009-02-02 23:43:58 +00001157 QualType RetTy = FI.getReturnType();
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001158 unsigned Index = 1;
Daniel Dunbarb225be42009-02-03 05:59:18 +00001159 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001160 switch (RetAI.getKind()) {
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001161 case ABIArgInfo::Direct:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001162 if (RetTy->isPromotableIntegerType()) {
1163 if (RetTy->isSignedIntegerType()) {
Devang Patela2c69122008-09-26 22:53:57 +00001164 RetAttrs |= llvm::Attribute::SExt;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001165 } else if (RetTy->isUnsignedIntegerType()) {
Devang Patela2c69122008-09-26 22:53:57 +00001166 RetAttrs |= llvm::Attribute::ZExt;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001167 }
1168 }
1169 break;
1170
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001171 case ABIArgInfo::Indirect:
Devang Patel761d7f72008-09-25 21:02:23 +00001172 PAL.push_back(llvm::AttributeWithIndex::get(Index,
Daniel Dunbar725ad312009-01-31 02:19:00 +00001173 llvm::Attribute::StructRet |
1174 llvm::Attribute::NoAlias));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001175 ++Index;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001176 break;
1177
Daniel Dunbar11434922009-01-26 21:26:08 +00001178 case ABIArgInfo::Ignore:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001179 case ABIArgInfo::Coerce:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001180 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001181
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001182 case ABIArgInfo::Expand:
1183 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001184 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001185
Devang Patela2c69122008-09-26 22:53:57 +00001186 if (RetAttrs)
1187 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Daniel Dunbar88c2fa92009-02-03 05:31:23 +00001188 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1189 ie = FI.arg_end(); it != ie; ++it) {
1190 QualType ParamType = it->type;
1191 const ABIArgInfo &AI = it->info;
Devang Patel761d7f72008-09-25 21:02:23 +00001192 unsigned Attributes = 0;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001193
1194 switch (AI.getKind()) {
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001195 case ABIArgInfo::Coerce:
1196 break;
1197
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001198 case ABIArgInfo::Indirect:
Devang Patel761d7f72008-09-25 21:02:23 +00001199 Attributes |= llvm::Attribute::ByVal;
Daniel Dunbarca008822009-02-05 01:31:19 +00001200 Attributes |=
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001201 llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign());
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001202 break;
1203
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001204 case ABIArgInfo::Direct:
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001205 if (ParamType->isPromotableIntegerType()) {
1206 if (ParamType->isSignedIntegerType()) {
Devang Patel761d7f72008-09-25 21:02:23 +00001207 Attributes |= llvm::Attribute::SExt;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001208 } else if (ParamType->isUnsignedIntegerType()) {
Devang Patel761d7f72008-09-25 21:02:23 +00001209 Attributes |= llvm::Attribute::ZExt;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001210 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001211 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001212 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001213
Daniel Dunbar11434922009-01-26 21:26:08 +00001214 case ABIArgInfo::Ignore:
1215 // Skip increment, no matching LLVM parameter.
1216 continue;
1217
Daniel Dunbar56273772008-09-17 00:51:38 +00001218 case ABIArgInfo::Expand: {
1219 std::vector<const llvm::Type*> Tys;
1220 // FIXME: This is rather inefficient. Do we ever actually need
1221 // to do anything here? The result should be just reconstructed
1222 // on the other side, so extension should be a non-issue.
1223 getTypes().GetExpandedTypes(ParamType, Tys);
1224 Index += Tys.size();
1225 continue;
1226 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001227 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001228
Devang Patel761d7f72008-09-25 21:02:23 +00001229 if (Attributes)
1230 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar56273772008-09-17 00:51:38 +00001231 ++Index;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001232 }
Devang Patela2c69122008-09-26 22:53:57 +00001233 if (FuncAttrs)
1234 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
1235
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001236}
1237
Daniel Dunbar88b53962009-02-02 22:03:45 +00001238void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
1239 llvm::Function *Fn,
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001240 const FunctionArgList &Args) {
Daniel Dunbar5251afa2009-02-03 06:02:10 +00001241 // FIXME: We no longer need the types from FunctionArgList; lift up
1242 // and simplify.
1243
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001244 // Emit allocs for param decls. Give the LLVM Argument nodes names.
1245 llvm::Function::arg_iterator AI = Fn->arg_begin();
1246
1247 // Name the struct return argument.
Daniel Dunbar88b53962009-02-02 22:03:45 +00001248 if (CGM.ReturnTypeUsesSret(FI)) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001249 AI->setName("agg.result");
1250 ++AI;
1251 }
Daniel Dunbarb225be42009-02-03 05:59:18 +00001252
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +00001253 assert(FI.arg_size() == Args.size() &&
1254 "Mismatch between function signature & arguments.");
Daniel Dunbarb225be42009-02-03 05:59:18 +00001255 CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001256 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001257 i != e; ++i, ++info_it) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001258 const VarDecl *Arg = i->first;
Daniel Dunbarb225be42009-02-03 05:59:18 +00001259 QualType Ty = info_it->type;
1260 const ABIArgInfo &ArgI = info_it->info;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001261
1262 switch (ArgI.getKind()) {
Daniel Dunbar1f745982009-02-05 09:16:39 +00001263 case ABIArgInfo::Indirect: {
1264 llvm::Value* V = AI;
1265 if (hasAggregateLLVMType(Ty)) {
1266 // Do nothing, aggregates and complex variables are accessed by
1267 // reference.
1268 } else {
1269 // Load scalar value from indirect argument.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001270 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar1f745982009-02-05 09:16:39 +00001271 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1272 // This must be a promotion, for something like
1273 // "void a(x) short x; {..."
1274 V = EmitScalarConversion(V, Ty, Arg->getType());
1275 }
1276 }
1277 EmitParmDecl(*Arg, V);
1278 break;
1279 }
1280
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001281 case ABIArgInfo::Direct: {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001282 assert(AI != Fn->arg_end() && "Argument mismatch!");
1283 llvm::Value* V = AI;
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00001284 if (hasAggregateLLVMType(Ty)) {
1285 // Create a temporary alloca to hold the argument; the rest of
1286 // codegen expects to access aggregates & complex values by
1287 // reference.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001288 V = CreateTempAlloca(ConvertTypeForMem(Ty));
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00001289 Builder.CreateStore(AI, V);
1290 } else {
1291 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1292 // This must be a promotion, for something like
1293 // "void a(x) short x; {..."
1294 V = EmitScalarConversion(V, Ty, Arg->getType());
1295 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001296 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001297 EmitParmDecl(*Arg, V);
1298 break;
1299 }
Daniel Dunbar56273772008-09-17 00:51:38 +00001300
1301 case ABIArgInfo::Expand: {
Daniel Dunbarb225be42009-02-03 05:59:18 +00001302 // If this structure was expanded into multiple arguments then
Daniel Dunbar56273772008-09-17 00:51:38 +00001303 // we need to create a temporary and reconstruct it from the
1304 // arguments.
Chris Lattner39f34e92008-11-24 04:00:27 +00001305 std::string Name = Arg->getNameAsString();
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001306 llvm::Value *Temp = CreateTempAlloca(ConvertTypeForMem(Ty),
Daniel Dunbar56273772008-09-17 00:51:38 +00001307 (Name + ".addr").c_str());
1308 // FIXME: What are the right qualifiers here?
1309 llvm::Function::arg_iterator End =
1310 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI);
1311 EmitParmDecl(*Arg, Temp);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001312
Daniel Dunbar56273772008-09-17 00:51:38 +00001313 // Name the arguments used in expansion and increment AI.
1314 unsigned Index = 0;
1315 for (; AI != End; ++AI, ++Index)
1316 AI->setName(Name + "." + llvm::utostr(Index));
1317 continue;
1318 }
Daniel Dunbar11434922009-01-26 21:26:08 +00001319
1320 case ABIArgInfo::Ignore:
Daniel Dunbar8b979d92009-02-10 00:06:49 +00001321 // Initialize the local variable appropriately.
1322 if (hasAggregateLLVMType(Ty)) {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001323 EmitParmDecl(*Arg, CreateTempAlloca(ConvertTypeForMem(Ty)));
Daniel Dunbar8b979d92009-02-10 00:06:49 +00001324 } else {
1325 EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())));
1326 }
1327
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +00001328 // Skip increment, no matching LLVM parameter.
1329 continue;
Daniel Dunbar11434922009-01-26 21:26:08 +00001330
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001331 case ABIArgInfo::Coerce: {
1332 assert(AI != Fn->arg_end() && "Argument mismatch!");
1333 // FIXME: This is very wasteful; EmitParmDecl is just going to
1334 // drop the result in a new alloca anyway, so we could just
1335 // store into that directly if we broke the abstraction down
1336 // more.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001337 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(Ty), "coerce");
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001338 CreateCoercedStore(AI, V, *this);
1339 // Match to what EmitParmDecl is expecting for this type.
Daniel Dunbar8b29a382009-02-04 07:22:24 +00001340 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001341 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar8b29a382009-02-04 07:22:24 +00001342 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1343 // This must be a promotion, for something like
1344 // "void a(x) short x; {..."
1345 V = EmitScalarConversion(V, Ty, Arg->getType());
1346 }
1347 }
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001348 EmitParmDecl(*Arg, V);
1349 break;
1350 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001351 }
Daniel Dunbar56273772008-09-17 00:51:38 +00001352
1353 ++AI;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001354 }
1355 assert(AI == Fn->arg_end() && "Argument mismatch!");
1356}
1357
Daniel Dunbar88b53962009-02-02 22:03:45 +00001358void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI,
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001359 llvm::Value *ReturnValue) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001360 llvm::Value *RV = 0;
1361
1362 // Functions with no result always return void.
1363 if (ReturnValue) {
Daniel Dunbar88b53962009-02-02 22:03:45 +00001364 QualType RetTy = FI.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001365 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001366
1367 switch (RetAI.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001368 case ABIArgInfo::Indirect:
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001369 if (RetTy->isAnyComplexType()) {
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001370 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
1371 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
1372 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1373 EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy);
1374 } else {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001375 EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(),
1376 false);
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001377 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001378 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001379
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001380 case ABIArgInfo::Direct:
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00001381 // The internal return value temp always will have
1382 // pointer-to-return-type type.
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001383 RV = Builder.CreateLoad(ReturnValue);
1384 break;
1385
Daniel Dunbar11434922009-01-26 21:26:08 +00001386 case ABIArgInfo::Ignore:
1387 break;
1388
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001389 case ABIArgInfo::Coerce:
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +00001390 RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001391 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001392
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001393 case ABIArgInfo::Expand:
1394 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001395 }
1396 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001397
1398 if (RV) {
1399 Builder.CreateRet(RV);
1400 } else {
1401 Builder.CreateRetVoid();
1402 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001403}
1404
Daniel Dunbar88b53962009-02-02 22:03:45 +00001405RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
1406 llvm::Value *Callee,
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001407 const CallArgList &CallArgs) {
Daniel Dunbar5251afa2009-02-03 06:02:10 +00001408 // FIXME: We no longer need the types from CallArgs; lift up and
1409 // simplify.
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001410 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001411
1412 // Handle struct-return functions by passing a pointer to the
1413 // location that we would like to return into.
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001414 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001415 const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
Daniel Dunbar2969a022009-02-05 09:24:53 +00001416 if (CGM.ReturnTypeUsesSret(CallInfo)) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001417 // Create a temporary alloca to hold the result of the call. :(
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001418 Args.push_back(CreateTempAlloca(ConvertTypeForMem(RetTy)));
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001419 }
1420
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +00001421 assert(CallInfo.arg_size() == CallArgs.size() &&
1422 "Mismatch between function signature & arguments.");
Daniel Dunbarb225be42009-02-03 05:59:18 +00001423 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001424 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001425 I != E; ++I, ++info_it) {
1426 const ABIArgInfo &ArgInfo = info_it->info;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001427 RValue RV = I->first;
Daniel Dunbar56273772008-09-17 00:51:38 +00001428
1429 switch (ArgInfo.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001430 case ABIArgInfo::Indirect:
Daniel Dunbar1f745982009-02-05 09:16:39 +00001431 if (RV.isScalar() || RV.isComplex()) {
1432 // Make a temporary alloca to pass the argument.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001433 Args.push_back(CreateTempAlloca(ConvertTypeForMem(I->second)));
Daniel Dunbar1f745982009-02-05 09:16:39 +00001434 if (RV.isScalar())
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001435 EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false);
Daniel Dunbar1f745982009-02-05 09:16:39 +00001436 else
1437 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
1438 } else {
1439 Args.push_back(RV.getAggregateAddr());
1440 }
1441 break;
1442
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001443 case ABIArgInfo::Direct:
Daniel Dunbar56273772008-09-17 00:51:38 +00001444 if (RV.isScalar()) {
1445 Args.push_back(RV.getScalarVal());
1446 } else if (RV.isComplex()) {
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00001447 llvm::Value *Tmp = llvm::UndefValue::get(ConvertType(I->second));
1448 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().first, 0);
1449 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().second, 1);
1450 Args.push_back(Tmp);
Daniel Dunbar56273772008-09-17 00:51:38 +00001451 } else {
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00001452 Args.push_back(Builder.CreateLoad(RV.getAggregateAddr()));
Daniel Dunbar56273772008-09-17 00:51:38 +00001453 }
1454 break;
1455
Daniel Dunbar11434922009-01-26 21:26:08 +00001456 case ABIArgInfo::Ignore:
1457 break;
1458
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001459 case ABIArgInfo::Coerce: {
1460 // FIXME: Avoid the conversion through memory if possible.
1461 llvm::Value *SrcPtr;
1462 if (RV.isScalar()) {
Daniel Dunbar5a1be6e2009-02-03 23:04:57 +00001463 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001464 EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false);
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001465 } else if (RV.isComplex()) {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001466 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001467 StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
1468 } else
1469 SrcPtr = RV.getAggregateAddr();
1470 Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
1471 *this));
1472 break;
1473 }
1474
Daniel Dunbar56273772008-09-17 00:51:38 +00001475 case ABIArgInfo::Expand:
1476 ExpandTypeToArgs(I->second, RV, Args);
1477 break;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001478 }
1479 }
1480
1481 llvm::CallInst *CI = Builder.CreateCall(Callee,&Args[0],&Args[0]+Args.size());
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001482
Daniel Dunbar5323a4b2008-09-10 00:32:18 +00001483 // FIXME: Provide TargetDecl so nounwind, noreturn, etc, etc get set.
Devang Patel761d7f72008-09-25 21:02:23 +00001484 CodeGen::AttributeListType AttributeList;
Daniel Dunbar88b53962009-02-02 22:03:45 +00001485 CGM.ConstructAttributeList(CallInfo, 0, AttributeList);
Devang Patel761d7f72008-09-25 21:02:23 +00001486 CI->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(),
Daniel Dunbar725ad312009-01-31 02:19:00 +00001487 AttributeList.size()));
1488
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001489 if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee))
1490 CI->setCallingConv(F->getCallingConv());
1491 if (CI->getType() != llvm::Type::VoidTy)
1492 CI->setName("call");
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001493
1494 switch (RetAI.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001495 case ABIArgInfo::Indirect:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001496 if (RetTy->isAnyComplexType())
Daniel Dunbar56273772008-09-17 00:51:38 +00001497 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001498 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar56273772008-09-17 00:51:38 +00001499 return RValue::getAggregate(Args[0]);
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +00001500 else
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001501 return RValue::get(EmitLoadOfScalar(Args[0], false, RetTy));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001502
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001503 case ABIArgInfo::Direct:
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00001504 if (RetTy->isAnyComplexType()) {
1505 llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
1506 llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
1507 return RValue::getComplex(std::make_pair(Real, Imag));
1508 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001509 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(RetTy), "agg.tmp");
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00001510 Builder.CreateStore(CI, V);
1511 return RValue::getAggregate(V);
1512 } else
1513 return RValue::get(CI);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001514
Daniel Dunbar11434922009-01-26 21:26:08 +00001515 case ABIArgInfo::Ignore:
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001516 // If we are ignoring an argument that had a result, make sure to
1517 // construct the appropriate return value for our caller.
Daniel Dunbar13e81732009-02-05 07:09:07 +00001518 return GetUndefRValue(RetTy);
Daniel Dunbar11434922009-01-26 21:26:08 +00001519
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001520 case ABIArgInfo::Coerce: {
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001521 // FIXME: Avoid the conversion through memory if possible.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001522 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(RetTy), "coerce");
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +00001523 CreateCoercedStore(CI, V, *this);
Anders Carlssonad3d6912008-11-25 22:21:48 +00001524 if (RetTy->isAnyComplexType())
1525 return RValue::getComplex(LoadComplexFromAddr(V, false));
Daniel Dunbar11434922009-01-26 21:26:08 +00001526 else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssonad3d6912008-11-25 22:21:48 +00001527 return RValue::getAggregate(V);
Daniel Dunbar11434922009-01-26 21:26:08 +00001528 else
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +00001529 return RValue::get(EmitLoadOfScalar(V, false, RetTy));
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001530 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001531
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001532 case ABIArgInfo::Expand:
1533 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001534 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001535
1536 assert(0 && "Unhandled ABIArgInfo::Kind");
1537 return RValue::get(0);
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001538}
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00001539
1540/* VarArg handling */
1541
1542llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) {
1543 return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this);
1544}