blob: a246ade82c2b9a9a752cb3ddac4f73cd9540d17b [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 Dunbar0dbe2272008-09-08 21:33:45 +000018#include "clang/AST/ASTContext.h"
19#include "clang/AST/Decl.h"
20#include "clang/AST/DeclObjC.h"
Daniel Dunbar56273772008-09-17 00:51:38 +000021#include "llvm/ADT/StringExtras.h"
Devang Pateld0646bd2008-09-24 01:01:36 +000022#include "llvm/Attributes.h"
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000023using namespace clang;
24using namespace CodeGen;
25
26/***/
27
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000028// FIXME: Use iterator and sidestep silly type array creation.
29
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000030CGFunctionInfo::CGFunctionInfo(const FunctionTypeNoProto *FTNP)
31 : IsVariadic(true)
32{
33 ArgTypes.push_back(FTNP->getResultType());
34}
35
36CGFunctionInfo::CGFunctionInfo(const FunctionTypeProto *FTP)
37 : IsVariadic(FTP->isVariadic())
38{
39 ArgTypes.push_back(FTP->getResultType());
40 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
41 ArgTypes.push_back(FTP->getArgType(i));
42}
43
44// FIXME: Is there really any reason to have this still?
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000045CGFunctionInfo::CGFunctionInfo(const FunctionDecl *FD)
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000046{
47 const FunctionType *FTy = FD->getType()->getAsFunctionType();
48 const FunctionTypeProto *FTP = dyn_cast<FunctionTypeProto>(FTy);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000049
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000050 ArgTypes.push_back(FTy->getResultType());
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000051 if (FTP) {
52 IsVariadic = FTP->isVariadic();
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000053 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
54 ArgTypes.push_back(FTP->getArgType(i));
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000055 } else {
56 IsVariadic = true;
57 }
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000058}
59
60CGFunctionInfo::CGFunctionInfo(const ObjCMethodDecl *MD,
61 const ASTContext &Context)
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000062 : IsVariadic(MD->isVariadic())
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000063{
64 ArgTypes.push_back(MD->getResultType());
65 ArgTypes.push_back(MD->getSelfDecl()->getType());
66 ArgTypes.push_back(Context.getObjCSelType());
67 for (ObjCMethodDecl::param_const_iterator i = MD->param_begin(),
68 e = MD->param_end(); i != e; ++i)
69 ArgTypes.push_back((*i)->getType());
70}
71
Daniel Dunbar5323a4b2008-09-10 00:32:18 +000072ArgTypeIterator CGFunctionInfo::argtypes_begin() const {
73 return ArgTypes.begin();
74}
75
76ArgTypeIterator CGFunctionInfo::argtypes_end() const {
77 return ArgTypes.end();
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000078}
79
80/***/
81
Daniel Dunbar5323a4b2008-09-10 00:32:18 +000082CGCallInfo::CGCallInfo(QualType _ResultType, const CallArgList &_Args) {
83 ArgTypes.push_back(_ResultType);
84 for (CallArgList::const_iterator i = _Args.begin(), e = _Args.end(); i!=e; ++i)
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000085 ArgTypes.push_back(i->second);
86}
87
Daniel Dunbar5323a4b2008-09-10 00:32:18 +000088ArgTypeIterator CGCallInfo::argtypes_begin() const {
89 return ArgTypes.begin();
90}
91
92ArgTypeIterator CGCallInfo::argtypes_end() const {
93 return ArgTypes.end();
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000094}
Daniel Dunbar17b708d2008-09-09 23:27:19 +000095
96/***/
97
Daniel Dunbar8951dbd2008-09-11 01:48:57 +000098/// ABIArgInfo - Helper class to encapsulate information about how a
99/// specific C type should be passed to or returned from a function.
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000100class ABIArgInfo {
101public:
102 enum Kind {
103 Default,
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000104 StructRet, /// Only valid for aggregate return types.
105
Daniel Dunbar56273772008-09-17 00:51:38 +0000106 Coerce, /// Only valid for aggregate return types, the argument
107 /// should be accessed by coercion to a provided type.
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000108
109 ByVal, /// Only valid for aggregate argument types. The
110 /// structure should be passed "byval" with the
111 /// specified alignment (0 indicates default
112 /// alignment).
113
114 Expand, /// Only valid for aggregate argument types. The
115 /// structure should be expanded into consecutive
Daniel Dunbar56273772008-09-17 00:51:38 +0000116 /// arguments for its constituent fields. Currently
117 /// expand is only allowed on structures whose fields
118 /// are all scalar types or are themselves expandable
119 /// types.
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000120
121 KindFirst=Default, KindLast=Expand
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000122 };
123
124private:
125 Kind TheKind;
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000126 const llvm::Type *TypeData;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000127 unsigned UIntData;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000128
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000129 ABIArgInfo(Kind K, const llvm::Type *TD=0,
130 unsigned UI=0) : TheKind(K),
131 TypeData(TD),
132 UIntData(0) {}
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000133public:
134 static ABIArgInfo getDefault() {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000135 return ABIArgInfo(Default);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000136 }
137 static ABIArgInfo getStructRet() {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000138 return ABIArgInfo(StructRet);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000139 }
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000140 static ABIArgInfo getCoerce(const llvm::Type *T) {
141 assert(T->isSingleValueType() && "Can only coerce to simple types");
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000142 return ABIArgInfo(Coerce, T);
143 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000144 static ABIArgInfo getByVal(unsigned Alignment) {
145 return ABIArgInfo(ByVal, 0, Alignment);
146 }
Daniel Dunbar56273772008-09-17 00:51:38 +0000147 static ABIArgInfo getExpand() {
148 return ABIArgInfo(Expand);
149 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000150
151 Kind getKind() const { return TheKind; }
152 bool isDefault() const { return TheKind == Default; }
153 bool isStructRet() const { return TheKind == StructRet; }
154 bool isCoerce() const { return TheKind == Coerce; }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000155 bool isByVal() const { return TheKind == ByVal; }
Daniel Dunbar56273772008-09-17 00:51:38 +0000156 bool isExpand() const { return TheKind == Expand; }
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000157
158 // Coerce accessors
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000159 const llvm::Type *getCoerceToType() const {
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000160 assert(TheKind == Coerce && "Invalid kind!");
161 return TypeData;
162 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000163
164 // ByVal accessors
165 unsigned getByValAlignment() const {
166 assert(TheKind == ByVal && "Invalid kind!");
167 return UIntData;
168 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000169};
170
171/***/
172
Daniel Dunbar834af452008-09-17 21:22:33 +0000173/// isEmptyStruct - Return true iff a structure has no non-empty
174/// members. Note that a structure with a flexible array member is not
175/// considered empty.
176static bool isEmptyStruct(QualType T) {
177 const RecordType *RT = T->getAsStructureType();
178 if (!RT)
179 return 0;
180 const RecordDecl *RD = RT->getDecl();
181 if (RD->hasFlexibleArrayMember())
182 return false;
183 for (RecordDecl::field_const_iterator i = RD->field_begin(),
184 e = RD->field_end(); i != e; ++i) {
185 const FieldDecl *FD = *i;
186 if (!isEmptyStruct(FD->getType()))
187 return false;
188 }
189 return true;
190}
191
192/// isSingleElementStruct - Determine if a structure is a "single
193/// element struct", i.e. it has exactly one non-empty field or
194/// exactly one field which is itself a single element
195/// struct. Structures with flexible array members are never
196/// considered single element structs.
197///
198/// \return The field declaration for the single non-empty field, if
199/// it exists.
200static const FieldDecl *isSingleElementStruct(QualType T) {
201 const RecordType *RT = T->getAsStructureType();
202 if (!RT)
203 return 0;
204
205 const RecordDecl *RD = RT->getDecl();
206 if (RD->hasFlexibleArrayMember())
207 return 0;
208
209 const FieldDecl *Found = 0;
210 for (RecordDecl::field_const_iterator i = RD->field_begin(),
211 e = RD->field_end(); i != e; ++i) {
212 const FieldDecl *FD = *i;
213 QualType FT = FD->getType();
214
215 if (isEmptyStruct(FT)) {
216 // Ignore
217 } else if (Found) {
218 return 0;
219 } else if (!CodeGenFunction::hasAggregateLLVMType(FT)) {
220 Found = FD;
221 } else {
222 Found = isSingleElementStruct(FT);
223 if (!Found)
224 return 0;
225 }
226 }
227
228 return Found;
229}
230
231static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
232 if (!Ty->getAsBuiltinType() && !Ty->isPointerType())
233 return false;
234
235 uint64_t Size = Context.getTypeSize(Ty);
236 return Size == 32 || Size == 64;
237}
238
239static bool areAllFields32Or64BitBasicType(const RecordDecl *RD,
240 ASTContext &Context) {
241 for (RecordDecl::field_const_iterator i = RD->field_begin(),
242 e = RD->field_end(); i != e; ++i) {
243 const FieldDecl *FD = *i;
244
245 if (!is32Or64BitBasicType(FD->getType(), Context))
246 return false;
247
248 // If this is a bit-field we need to make sure it is still a
249 // 32-bit or 64-bit type.
250 if (Expr *BW = FD->getBitWidth()) {
251 unsigned Width = BW->getIntegerConstantExprValue(Context).getZExtValue();
252 if (Width <= 16)
253 return false;
254 }
255 }
256 return true;
257}
258
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000259static ABIArgInfo classifyReturnType(QualType RetTy,
260 ASTContext &Context) {
Daniel Dunbar6660c8a2008-09-11 00:04:36 +0000261 assert(!RetTy->isArrayType() &&
262 "Array types cannot be passed directly.");
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000263 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar834af452008-09-17 21:22:33 +0000264 // Classify "single element" structs as their element type.
265 const FieldDecl *SeltFD = isSingleElementStruct(RetTy);
266 if (SeltFD) {
267 QualType SeltTy = SeltFD->getType()->getDesugaredType();
268 if (const BuiltinType *BT = SeltTy->getAsBuiltinType()) {
269 // FIXME: This is gross, it would be nice if we could just
270 // pass back SeltTy and have clients deal with it. Is it worth
271 // supporting coerce to both LLVM and clang Types?
272 if (BT->isIntegerType()) {
273 uint64_t Size = Context.getTypeSize(SeltTy);
274 return ABIArgInfo::getCoerce(llvm::IntegerType::get((unsigned) Size));
275 } else if (BT->getKind() == BuiltinType::Float) {
276 return ABIArgInfo::getCoerce(llvm::Type::FloatTy);
277 } else if (BT->getKind() == BuiltinType::Double) {
278 return ABIArgInfo::getCoerce(llvm::Type::DoubleTy);
279 }
280 } else if (SeltTy->isPointerType()) {
281 // FIXME: It would be really nice if this could come out as
282 // the proper pointer type.
283 llvm::Type *PtrTy =
284 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
285 return ABIArgInfo::getCoerce(PtrTy);
286 }
287 }
288
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000289 uint64_t Size = Context.getTypeSize(RetTy);
290 if (Size == 8) {
291 return ABIArgInfo::getCoerce(llvm::Type::Int8Ty);
292 } else if (Size == 16) {
293 return ABIArgInfo::getCoerce(llvm::Type::Int16Ty);
294 } else if (Size == 32) {
295 return ABIArgInfo::getCoerce(llvm::Type::Int32Ty);
296 } else if (Size == 64) {
297 return ABIArgInfo::getCoerce(llvm::Type::Int64Ty);
298 } else {
299 return ABIArgInfo::getStructRet();
300 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000301 } else {
302 return ABIArgInfo::getDefault();
303 }
304}
305
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000306static ABIArgInfo classifyArgumentType(QualType Ty,
Daniel Dunbarf0357382008-09-17 20:11:04 +0000307 ASTContext &Context) {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000308 assert(!Ty->isArrayType() && "Array types cannot be passed directly.");
Daniel Dunbarf0357382008-09-17 20:11:04 +0000309 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar834af452008-09-17 21:22:33 +0000310 // Structures with flexible arrays are always byval.
311 if (const RecordType *RT = Ty->getAsStructureType())
312 if (RT->getDecl()->hasFlexibleArrayMember())
313 return ABIArgInfo::getByVal(0);
314
315 // Expand empty structs (i.e. ignore)
316 uint64_t Size = Context.getTypeSize(Ty);
317 if (Ty->isStructureType() && Size == 0)
318 return ABIArgInfo::getExpand();
319
320 // Expand structs with size <= 128-bits which consist only of
321 // basic types (int, long long, float, double, xxx*). This is
322 // non-recursive and does not ignore empty fields.
323 if (const RecordType *RT = Ty->getAsStructureType()) {
324 if (Context.getTypeSize(Ty) <= 4*32 &&
325 areAllFields32Or64BitBasicType(RT->getDecl(), Context))
326 return ABIArgInfo::getExpand();
327 }
328
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000329 return ABIArgInfo::getByVal(0);
330 } else {
331 return ABIArgInfo::getDefault();
332 }
333}
334
Daniel Dunbarf0357382008-09-17 20:11:04 +0000335static ABIArgInfo getABIReturnInfo(QualType Ty,
336 ASTContext &Context) {
337 ABIArgInfo Info = classifyReturnType(Ty, Context);
338 // Ensure default on aggregate types is StructRet.
339 if (Info.isDefault() && CodeGenFunction::hasAggregateLLVMType(Ty))
340 return ABIArgInfo::getStructRet();
341 return Info;
342}
343
344static ABIArgInfo getABIArgumentInfo(QualType Ty,
345 ASTContext &Context) {
346 ABIArgInfo Info = classifyArgumentType(Ty, Context);
347 // Ensure default on aggregate types is ByVal.
348 if (Info.isDefault() && CodeGenFunction::hasAggregateLLVMType(Ty))
349 return ABIArgInfo::getByVal(0);
350 return Info;
351}
352
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000353/***/
354
Daniel Dunbar56273772008-09-17 00:51:38 +0000355void CodeGenTypes::GetExpandedTypes(QualType Ty,
356 std::vector<const llvm::Type*> &ArgTys) {
357 const RecordType *RT = Ty->getAsStructureType();
358 assert(RT && "Can only expand structure types.");
359 const RecordDecl *RD = RT->getDecl();
360 assert(!RD->hasFlexibleArrayMember() &&
361 "Cannot expand structure with flexible array.");
362
363 for (RecordDecl::field_const_iterator i = RD->field_begin(),
364 e = RD->field_end(); i != e; ++i) {
365 const FieldDecl *FD = *i;
366 assert(!FD->isBitField() &&
367 "Cannot expand structure with bit-field members.");
368
369 QualType FT = FD->getType();
370 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
371 GetExpandedTypes(FT, ArgTys);
372 } else {
373 ArgTys.push_back(ConvertType(FT));
374 }
375 }
376}
377
378llvm::Function::arg_iterator
379CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
380 llvm::Function::arg_iterator AI) {
381 const RecordType *RT = Ty->getAsStructureType();
382 assert(RT && "Can only expand structure types.");
383
384 RecordDecl *RD = RT->getDecl();
385 assert(LV.isSimple() &&
386 "Unexpected non-simple lvalue during struct expansion.");
387 llvm::Value *Addr = LV.getAddress();
388 for (RecordDecl::field_iterator i = RD->field_begin(),
389 e = RD->field_end(); i != e; ++i) {
390 FieldDecl *FD = *i;
391 QualType FT = FD->getType();
392
393 // FIXME: What are the right qualifiers here?
394 LValue LV = EmitLValueForField(Addr, FD, false, 0);
395 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
396 AI = ExpandTypeFromArgs(FT, LV, AI);
397 } else {
398 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
399 ++AI;
400 }
401 }
402
403 return AI;
404}
405
406void
407CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
408 llvm::SmallVector<llvm::Value*, 16> &Args) {
409 const RecordType *RT = Ty->getAsStructureType();
410 assert(RT && "Can only expand structure types.");
411
412 RecordDecl *RD = RT->getDecl();
413 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
414 llvm::Value *Addr = RV.getAggregateAddr();
415 for (RecordDecl::field_iterator i = RD->field_begin(),
416 e = RD->field_end(); i != e; ++i) {
417 FieldDecl *FD = *i;
418 QualType FT = FD->getType();
419
420 // FIXME: What are the right qualifiers here?
421 LValue LV = EmitLValueForField(Addr, FD, false, 0);
422 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
423 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
424 } else {
425 RValue RV = EmitLoadOfLValue(LV, FT);
426 assert(RV.isScalar() &&
427 "Unexpected non-scalar rvalue during struct expansion.");
428 Args.push_back(RV.getScalarVal());
429 }
430 }
431}
432
433/***/
434
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000435const llvm::FunctionType *
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000436CodeGenTypes::GetFunctionType(const CGCallInfo &CI, bool IsVariadic) {
437 return GetFunctionType(CI.argtypes_begin(), CI.argtypes_end(), IsVariadic);
438}
439
440const llvm::FunctionType *
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000441CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI) {
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000442 return GetFunctionType(FI.argtypes_begin(), FI.argtypes_end(), FI.isVariadic());
443}
444
445const llvm::FunctionType *
446CodeGenTypes::GetFunctionType(ArgTypeIterator begin, ArgTypeIterator end,
447 bool IsVariadic) {
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000448 std::vector<const llvm::Type*> ArgTys;
449
450 const llvm::Type *ResultType = 0;
451
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000452 QualType RetTy = *begin;
Daniel Dunbarf0357382008-09-17 20:11:04 +0000453 ABIArgInfo RetAI = getABIReturnInfo(RetTy, getContext());
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000454 switch (RetAI.getKind()) {
455 case ABIArgInfo::ByVal:
456 case ABIArgInfo::Expand:
457 assert(0 && "Invalid ABI kind for return argument");
458
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000459 case ABIArgInfo::Default:
460 if (RetTy->isVoidType()) {
461 ResultType = llvm::Type::VoidTy;
462 } else {
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000463 ResultType = ConvertType(RetTy);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000464 }
465 break;
466
467 case ABIArgInfo::StructRet: {
468 ResultType = llvm::Type::VoidTy;
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000469 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000470 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
471 break;
472 }
473
474 case ABIArgInfo::Coerce:
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000475 ResultType = RetAI.getCoerceToType();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000476 break;
477 }
478
479 for (++begin; begin != end; ++begin) {
Daniel Dunbarf0357382008-09-17 20:11:04 +0000480 ABIArgInfo AI = getABIArgumentInfo(*begin, getContext());
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000481 const llvm::Type *Ty = ConvertType(*begin);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000482
483 switch (AI.getKind()) {
Daniel Dunbar56273772008-09-17 00:51:38 +0000484 case ABIArgInfo::Coerce:
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000485 case ABIArgInfo::StructRet:
486 assert(0 && "Invalid ABI kind for non-return argument");
487
488 case ABIArgInfo::ByVal:
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000489 // byval arguments are always on the stack, which is addr space #0.
490 ArgTys.push_back(llvm::PointerType::getUnqual(Ty));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000491 assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled");
492 break;
493
494 case ABIArgInfo::Default:
495 ArgTys.push_back(Ty);
496 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000497
498 case ABIArgInfo::Expand:
Daniel Dunbar56273772008-09-17 00:51:38 +0000499 GetExpandedTypes(*begin, ArgTys);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000500 break;
501 }
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000502 }
503
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000504 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000505}
506
Daniel Dunbarb7688072008-09-10 00:41:16 +0000507bool CodeGenModule::ReturnTypeUsesSret(QualType RetTy) {
Daniel Dunbarf0357382008-09-17 20:11:04 +0000508 return getABIReturnInfo(RetTy, getContext()).isStructRet();
Daniel Dunbar3913f182008-09-09 23:48:28 +0000509}
510
Devang Patel761d7f72008-09-25 21:02:23 +0000511void CodeGenModule::ConstructAttributeList(const Decl *TargetDecl,
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000512 ArgTypeIterator begin,
513 ArgTypeIterator end,
Devang Patel761d7f72008-09-25 21:02:23 +0000514 AttributeListType &PAL) {
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000515 unsigned FuncAttrs = 0;
Devang Patela2c69122008-09-26 22:53:57 +0000516 unsigned RetAttrs = 0;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000517
518 if (TargetDecl) {
519 if (TargetDecl->getAttr<NoThrowAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +0000520 FuncAttrs |= llvm::Attribute::NoUnwind;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000521 if (TargetDecl->getAttr<NoReturnAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +0000522 FuncAttrs |= llvm::Attribute::NoReturn;
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000523 if (TargetDecl->getAttr<PureAttr>())
524 FuncAttrs |= llvm::Attribute::ReadOnly;
525 if (TargetDecl->getAttr<ConstAttr>())
526 FuncAttrs |= llvm::Attribute::ReadNone;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000527 }
528
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000529 QualType RetTy = *begin;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000530 unsigned Index = 1;
Daniel Dunbarf0357382008-09-17 20:11:04 +0000531 ABIArgInfo RetAI = getABIReturnInfo(RetTy, getContext());
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000532 switch (RetAI.getKind()) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000533 case ABIArgInfo::Default:
534 if (RetTy->isPromotableIntegerType()) {
535 if (RetTy->isSignedIntegerType()) {
Devang Patela2c69122008-09-26 22:53:57 +0000536 RetAttrs |= llvm::Attribute::SExt;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000537 } else if (RetTy->isUnsignedIntegerType()) {
Devang Patela2c69122008-09-26 22:53:57 +0000538 RetAttrs |= llvm::Attribute::ZExt;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000539 }
540 }
541 break;
542
543 case ABIArgInfo::StructRet:
Devang Patel761d7f72008-09-25 21:02:23 +0000544 PAL.push_back(llvm::AttributeWithIndex::get(Index,
545 llvm::Attribute::StructRet|
546 llvm::Attribute::NoAlias));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000547 ++Index;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000548 break;
549
550 case ABIArgInfo::Coerce:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000551 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000552
553 case ABIArgInfo::ByVal:
554 case ABIArgInfo::Expand:
555 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000556 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000557
Devang Patela2c69122008-09-26 22:53:57 +0000558 if (RetAttrs)
559 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Daniel Dunbar56273772008-09-17 00:51:38 +0000560 for (++begin; begin != end; ++begin) {
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000561 QualType ParamType = *begin;
Devang Patel761d7f72008-09-25 21:02:23 +0000562 unsigned Attributes = 0;
Daniel Dunbarf0357382008-09-17 20:11:04 +0000563 ABIArgInfo AI = getABIArgumentInfo(ParamType, getContext());
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000564
565 switch (AI.getKind()) {
566 case ABIArgInfo::StructRet:
Daniel Dunbar56273772008-09-17 00:51:38 +0000567 case ABIArgInfo::Coerce:
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000568 assert(0 && "Invalid ABI kind for non-return argument");
569
570 case ABIArgInfo::ByVal:
Devang Patel761d7f72008-09-25 21:02:23 +0000571 Attributes |= llvm::Attribute::ByVal;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000572 assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled");
573 break;
574
575 case ABIArgInfo::Default:
576 if (ParamType->isPromotableIntegerType()) {
577 if (ParamType->isSignedIntegerType()) {
Devang Patel761d7f72008-09-25 21:02:23 +0000578 Attributes |= llvm::Attribute::SExt;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000579 } else if (ParamType->isUnsignedIntegerType()) {
Devang Patel761d7f72008-09-25 21:02:23 +0000580 Attributes |= llvm::Attribute::ZExt;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000581 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000582 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000583 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000584
Daniel Dunbar56273772008-09-17 00:51:38 +0000585 case ABIArgInfo::Expand: {
586 std::vector<const llvm::Type*> Tys;
587 // FIXME: This is rather inefficient. Do we ever actually need
588 // to do anything here? The result should be just reconstructed
589 // on the other side, so extension should be a non-issue.
590 getTypes().GetExpandedTypes(ParamType, Tys);
591 Index += Tys.size();
592 continue;
593 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000594 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000595
Devang Patel761d7f72008-09-25 21:02:23 +0000596 if (Attributes)
597 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar56273772008-09-17 00:51:38 +0000598 ++Index;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000599 }
Devang Patela2c69122008-09-26 22:53:57 +0000600 if (FuncAttrs)
601 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
602
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000603}
604
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000605void CodeGenFunction::EmitFunctionProlog(llvm::Function *Fn,
606 QualType RetTy,
607 const FunctionArgList &Args) {
608 // Emit allocs for param decls. Give the LLVM Argument nodes names.
609 llvm::Function::arg_iterator AI = Fn->arg_begin();
610
611 // Name the struct return argument.
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000612 if (CGM.ReturnTypeUsesSret(RetTy)) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000613 AI->setName("agg.result");
614 ++AI;
615 }
616
617 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbar56273772008-09-17 00:51:38 +0000618 i != e; ++i) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000619 const VarDecl *Arg = i->first;
Daniel Dunbar56273772008-09-17 00:51:38 +0000620 QualType Ty = i->second;
Daniel Dunbarf0357382008-09-17 20:11:04 +0000621 ABIArgInfo ArgI = getABIArgumentInfo(Ty, getContext());
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000622
623 switch (ArgI.getKind()) {
624 case ABIArgInfo::ByVal:
625 case ABIArgInfo::Default: {
626 assert(AI != Fn->arg_end() && "Argument mismatch!");
627 llvm::Value* V = AI;
Daniel Dunbar56273772008-09-17 00:51:38 +0000628 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000629 // This must be a promotion, for something like
630 // "void a(x) short x; {..."
Daniel Dunbar56273772008-09-17 00:51:38 +0000631 V = EmitScalarConversion(V, Ty, Arg->getType());
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000632 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000633 EmitParmDecl(*Arg, V);
634 break;
635 }
Daniel Dunbar56273772008-09-17 00:51:38 +0000636
637 case ABIArgInfo::Expand: {
638 // If this was structure was expand into multiple arguments then
639 // we need to create a temporary and reconstruct it from the
640 // arguments.
641 std::string Name(Arg->getName());
642 llvm::Value *Temp = CreateTempAlloca(ConvertType(Ty),
643 (Name + ".addr").c_str());
644 // FIXME: What are the right qualifiers here?
645 llvm::Function::arg_iterator End =
646 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI);
647 EmitParmDecl(*Arg, Temp);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000648
Daniel Dunbar56273772008-09-17 00:51:38 +0000649 // Name the arguments used in expansion and increment AI.
650 unsigned Index = 0;
651 for (; AI != End; ++AI, ++Index)
652 AI->setName(Name + "." + llvm::utostr(Index));
653 continue;
654 }
655
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000656 case ABIArgInfo::Coerce:
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000657 case ABIArgInfo::StructRet:
658 assert(0 && "Invalid ABI kind for non-return argument");
659 }
Daniel Dunbar56273772008-09-17 00:51:38 +0000660
661 ++AI;
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000662 }
663 assert(AI == Fn->arg_end() && "Argument mismatch!");
664}
665
666void CodeGenFunction::EmitFunctionEpilog(QualType RetTy,
667 llvm::Value *ReturnValue) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000668 llvm::Value *RV = 0;
669
670 // Functions with no result always return void.
671 if (ReturnValue) {
Daniel Dunbarf0357382008-09-17 20:11:04 +0000672 ABIArgInfo RetAI = getABIReturnInfo(RetTy, getContext());
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000673
674 switch (RetAI.getKind()) {
675 case ABIArgInfo::StructRet:
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000676 EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000677 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000678
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000679 case ABIArgInfo::Default:
680 RV = Builder.CreateLoad(ReturnValue);
681 break;
682
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000683 case ABIArgInfo::Coerce: {
684 const llvm::Type *CoerceToPTy =
685 llvm::PointerType::getUnqual(RetAI.getCoerceToType());
686 RV = Builder.CreateLoad(Builder.CreateBitCast(ReturnValue, CoerceToPTy));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000687 break;
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000688 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000689
690 case ABIArgInfo::ByVal:
691 case ABIArgInfo::Expand:
692 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000693 }
694 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000695
696 if (RV) {
697 Builder.CreateRet(RV);
698 } else {
699 Builder.CreateRetVoid();
700 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000701}
702
703RValue CodeGenFunction::EmitCall(llvm::Value *Callee,
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000704 QualType RetTy,
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000705 const CallArgList &CallArgs) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000706 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000707
708 // Handle struct-return functions by passing a pointer to the
709 // location that we would like to return into.
Daniel Dunbarf0357382008-09-17 20:11:04 +0000710 ABIArgInfo RetAI = getABIReturnInfo(RetTy, getContext());
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000711 switch (RetAI.getKind()) {
712 case ABIArgInfo::StructRet:
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000713 // Create a temporary alloca to hold the result of the call. :(
Daniel Dunbar56273772008-09-17 00:51:38 +0000714 Args.push_back(CreateTempAlloca(ConvertType(RetTy)));
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000715 break;
716
717 case ABIArgInfo::Default:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000718 case ABIArgInfo::Coerce:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000719 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000720
721 case ABIArgInfo::ByVal:
722 case ABIArgInfo::Expand:
723 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000724 }
725
726 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
727 I != E; ++I) {
Daniel Dunbarf0357382008-09-17 20:11:04 +0000728 ABIArgInfo ArgInfo = getABIArgumentInfo(I->second, getContext());
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000729 RValue RV = I->first;
Daniel Dunbar56273772008-09-17 00:51:38 +0000730
731 switch (ArgInfo.getKind()) {
732 case ABIArgInfo::ByVal: // Default is byval
733 case ABIArgInfo::Default:
734 if (RV.isScalar()) {
735 Args.push_back(RV.getScalarVal());
736 } else if (RV.isComplex()) {
737 // Make a temporary alloca to pass the argument.
738 Args.push_back(CreateTempAlloca(ConvertType(I->second)));
739 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
740 } else {
741 Args.push_back(RV.getAggregateAddr());
742 }
743 break;
744
745 case ABIArgInfo::StructRet:
746 case ABIArgInfo::Coerce:
747 assert(0 && "Invalid ABI kind for non-return argument");
748 break;
749
750 case ABIArgInfo::Expand:
751 ExpandTypeToArgs(I->second, RV, Args);
752 break;
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000753 }
754 }
755
756 llvm::CallInst *CI = Builder.CreateCall(Callee,&Args[0],&Args[0]+Args.size());
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000757 CGCallInfo CallInfo(RetTy, CallArgs);
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000758
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000759 // FIXME: Provide TargetDecl so nounwind, noreturn, etc, etc get set.
Devang Patel761d7f72008-09-25 21:02:23 +0000760 CodeGen::AttributeListType AttributeList;
761 CGM.ConstructAttributeList(0,
Daniel Dunbarb7688072008-09-10 00:41:16 +0000762 CallInfo.argtypes_begin(), CallInfo.argtypes_end(),
Devang Patel761d7f72008-09-25 21:02:23 +0000763 AttributeList);
764 CI->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(),
765 AttributeList.size()));
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000766
767 if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee))
768 CI->setCallingConv(F->getCallingConv());
769 if (CI->getType() != llvm::Type::VoidTy)
770 CI->setName("call");
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000771
772 switch (RetAI.getKind()) {
773 case ABIArgInfo::StructRet:
774 if (RetTy->isAnyComplexType())
Daniel Dunbar56273772008-09-17 00:51:38 +0000775 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000776 else
777 // Struct return.
Daniel Dunbar56273772008-09-17 00:51:38 +0000778 return RValue::getAggregate(Args[0]);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000779
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000780 case ABIArgInfo::Default:
781 return RValue::get(RetTy->isVoidType() ? 0 : CI);
782
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000783 case ABIArgInfo::Coerce: {
784 const llvm::Type *CoerceToPTy =
785 llvm::PointerType::getUnqual(RetAI.getCoerceToType());
786 llvm::Value *V = CreateTempAlloca(ConvertType(RetTy), "tmp");
787 Builder.CreateStore(CI, Builder.CreateBitCast(V, CoerceToPTy));
788 return RValue::getAggregate(V);
789 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000790
791 case ABIArgInfo::ByVal:
792 case ABIArgInfo::Expand:
793 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000794 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000795
796 assert(0 && "Unhandled ABIArgInfo::Kind");
797 return RValue::get(0);
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000798}