blob: 7069fee4602326b7529e7e894eb6e0e880beb71e [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;
516
517 if (TargetDecl) {
518 if (TargetDecl->getAttr<NoThrowAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +0000519 FuncAttrs |= llvm::Attribute::NoUnwind;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000520 if (TargetDecl->getAttr<NoReturnAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +0000521 FuncAttrs |= llvm::Attribute::NoReturn;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000522 }
523
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000524 QualType RetTy = *begin;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000525 unsigned Index = 1;
Daniel Dunbarf0357382008-09-17 20:11:04 +0000526 ABIArgInfo RetAI = getABIReturnInfo(RetTy, getContext());
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000527 switch (RetAI.getKind()) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000528 case ABIArgInfo::Default:
529 if (RetTy->isPromotableIntegerType()) {
530 if (RetTy->isSignedIntegerType()) {
Devang Patel761d7f72008-09-25 21:02:23 +0000531 FuncAttrs |= llvm::Attribute::SExt;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000532 } else if (RetTy->isUnsignedIntegerType()) {
Devang Patel761d7f72008-09-25 21:02:23 +0000533 FuncAttrs |= llvm::Attribute::ZExt;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000534 }
535 }
536 break;
537
538 case ABIArgInfo::StructRet:
Devang Patel761d7f72008-09-25 21:02:23 +0000539 PAL.push_back(llvm::AttributeWithIndex::get(Index,
540 llvm::Attribute::StructRet|
541 llvm::Attribute::NoAlias));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000542 ++Index;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000543 break;
544
545 case ABIArgInfo::Coerce:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000546 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000547
548 case ABIArgInfo::ByVal:
549 case ABIArgInfo::Expand:
550 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000551 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000552
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000553 if (FuncAttrs)
Devang Patel761d7f72008-09-25 21:02:23 +0000554 PAL.push_back(llvm::AttributeWithIndex::get(0, FuncAttrs));
Daniel Dunbar56273772008-09-17 00:51:38 +0000555 for (++begin; begin != end; ++begin) {
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000556 QualType ParamType = *begin;
Devang Patel761d7f72008-09-25 21:02:23 +0000557 unsigned Attributes = 0;
Daniel Dunbarf0357382008-09-17 20:11:04 +0000558 ABIArgInfo AI = getABIArgumentInfo(ParamType, getContext());
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000559
560 switch (AI.getKind()) {
561 case ABIArgInfo::StructRet:
Daniel Dunbar56273772008-09-17 00:51:38 +0000562 case ABIArgInfo::Coerce:
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000563 assert(0 && "Invalid ABI kind for non-return argument");
564
565 case ABIArgInfo::ByVal:
Devang Patel761d7f72008-09-25 21:02:23 +0000566 Attributes |= llvm::Attribute::ByVal;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000567 assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled");
568 break;
569
570 case ABIArgInfo::Default:
571 if (ParamType->isPromotableIntegerType()) {
572 if (ParamType->isSignedIntegerType()) {
Devang Patel761d7f72008-09-25 21:02:23 +0000573 Attributes |= llvm::Attribute::SExt;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000574 } else if (ParamType->isUnsignedIntegerType()) {
Devang Patel761d7f72008-09-25 21:02:23 +0000575 Attributes |= llvm::Attribute::ZExt;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000576 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000577 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000578 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000579
Daniel Dunbar56273772008-09-17 00:51:38 +0000580 case ABIArgInfo::Expand: {
581 std::vector<const llvm::Type*> Tys;
582 // FIXME: This is rather inefficient. Do we ever actually need
583 // to do anything here? The result should be just reconstructed
584 // on the other side, so extension should be a non-issue.
585 getTypes().GetExpandedTypes(ParamType, Tys);
586 Index += Tys.size();
587 continue;
588 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000589 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000590
Devang Patel761d7f72008-09-25 21:02:23 +0000591 if (Attributes)
592 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar56273772008-09-17 00:51:38 +0000593 ++Index;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000594 }
595}
596
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000597void CodeGenFunction::EmitFunctionProlog(llvm::Function *Fn,
598 QualType RetTy,
599 const FunctionArgList &Args) {
600 // Emit allocs for param decls. Give the LLVM Argument nodes names.
601 llvm::Function::arg_iterator AI = Fn->arg_begin();
602
603 // Name the struct return argument.
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000604 if (CGM.ReturnTypeUsesSret(RetTy)) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000605 AI->setName("agg.result");
606 ++AI;
607 }
608
609 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbar56273772008-09-17 00:51:38 +0000610 i != e; ++i) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000611 const VarDecl *Arg = i->first;
Daniel Dunbar56273772008-09-17 00:51:38 +0000612 QualType Ty = i->second;
Daniel Dunbarf0357382008-09-17 20:11:04 +0000613 ABIArgInfo ArgI = getABIArgumentInfo(Ty, getContext());
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000614
615 switch (ArgI.getKind()) {
616 case ABIArgInfo::ByVal:
617 case ABIArgInfo::Default: {
618 assert(AI != Fn->arg_end() && "Argument mismatch!");
619 llvm::Value* V = AI;
Daniel Dunbar56273772008-09-17 00:51:38 +0000620 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000621 // This must be a promotion, for something like
622 // "void a(x) short x; {..."
Daniel Dunbar56273772008-09-17 00:51:38 +0000623 V = EmitScalarConversion(V, Ty, Arg->getType());
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000624 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000625 EmitParmDecl(*Arg, V);
626 break;
627 }
Daniel Dunbar56273772008-09-17 00:51:38 +0000628
629 case ABIArgInfo::Expand: {
630 // If this was structure was expand into multiple arguments then
631 // we need to create a temporary and reconstruct it from the
632 // arguments.
633 std::string Name(Arg->getName());
634 llvm::Value *Temp = CreateTempAlloca(ConvertType(Ty),
635 (Name + ".addr").c_str());
636 // FIXME: What are the right qualifiers here?
637 llvm::Function::arg_iterator End =
638 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI);
639 EmitParmDecl(*Arg, Temp);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000640
Daniel Dunbar56273772008-09-17 00:51:38 +0000641 // Name the arguments used in expansion and increment AI.
642 unsigned Index = 0;
643 for (; AI != End; ++AI, ++Index)
644 AI->setName(Name + "." + llvm::utostr(Index));
645 continue;
646 }
647
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000648 case ABIArgInfo::Coerce:
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000649 case ABIArgInfo::StructRet:
650 assert(0 && "Invalid ABI kind for non-return argument");
651 }
Daniel Dunbar56273772008-09-17 00:51:38 +0000652
653 ++AI;
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000654 }
655 assert(AI == Fn->arg_end() && "Argument mismatch!");
656}
657
658void CodeGenFunction::EmitFunctionEpilog(QualType RetTy,
659 llvm::Value *ReturnValue) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000660 llvm::Value *RV = 0;
661
662 // Functions with no result always return void.
663 if (ReturnValue) {
Daniel Dunbarf0357382008-09-17 20:11:04 +0000664 ABIArgInfo RetAI = getABIReturnInfo(RetTy, getContext());
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000665
666 switch (RetAI.getKind()) {
667 case ABIArgInfo::StructRet:
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000668 EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000669 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000670
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000671 case ABIArgInfo::Default:
672 RV = Builder.CreateLoad(ReturnValue);
673 break;
674
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000675 case ABIArgInfo::Coerce: {
676 const llvm::Type *CoerceToPTy =
677 llvm::PointerType::getUnqual(RetAI.getCoerceToType());
678 RV = Builder.CreateLoad(Builder.CreateBitCast(ReturnValue, CoerceToPTy));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000679 break;
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000680 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000681
682 case ABIArgInfo::ByVal:
683 case ABIArgInfo::Expand:
684 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000685 }
686 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000687
688 if (RV) {
689 Builder.CreateRet(RV);
690 } else {
691 Builder.CreateRetVoid();
692 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000693}
694
695RValue CodeGenFunction::EmitCall(llvm::Value *Callee,
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000696 QualType RetTy,
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000697 const CallArgList &CallArgs) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000698 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000699
700 // Handle struct-return functions by passing a pointer to the
701 // location that we would like to return into.
Daniel Dunbarf0357382008-09-17 20:11:04 +0000702 ABIArgInfo RetAI = getABIReturnInfo(RetTy, getContext());
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000703 switch (RetAI.getKind()) {
704 case ABIArgInfo::StructRet:
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000705 // Create a temporary alloca to hold the result of the call. :(
Daniel Dunbar56273772008-09-17 00:51:38 +0000706 Args.push_back(CreateTempAlloca(ConvertType(RetTy)));
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000707 break;
708
709 case ABIArgInfo::Default:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000710 case ABIArgInfo::Coerce:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000711 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000712
713 case ABIArgInfo::ByVal:
714 case ABIArgInfo::Expand:
715 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000716 }
717
718 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
719 I != E; ++I) {
Daniel Dunbarf0357382008-09-17 20:11:04 +0000720 ABIArgInfo ArgInfo = getABIArgumentInfo(I->second, getContext());
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000721 RValue RV = I->first;
Daniel Dunbar56273772008-09-17 00:51:38 +0000722
723 switch (ArgInfo.getKind()) {
724 case ABIArgInfo::ByVal: // Default is byval
725 case ABIArgInfo::Default:
726 if (RV.isScalar()) {
727 Args.push_back(RV.getScalarVal());
728 } else if (RV.isComplex()) {
729 // Make a temporary alloca to pass the argument.
730 Args.push_back(CreateTempAlloca(ConvertType(I->second)));
731 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
732 } else {
733 Args.push_back(RV.getAggregateAddr());
734 }
735 break;
736
737 case ABIArgInfo::StructRet:
738 case ABIArgInfo::Coerce:
739 assert(0 && "Invalid ABI kind for non-return argument");
740 break;
741
742 case ABIArgInfo::Expand:
743 ExpandTypeToArgs(I->second, RV, Args);
744 break;
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000745 }
746 }
747
748 llvm::CallInst *CI = Builder.CreateCall(Callee,&Args[0],&Args[0]+Args.size());
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000749 CGCallInfo CallInfo(RetTy, CallArgs);
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000750
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000751 // FIXME: Provide TargetDecl so nounwind, noreturn, etc, etc get set.
Devang Patel761d7f72008-09-25 21:02:23 +0000752 CodeGen::AttributeListType AttributeList;
753 CGM.ConstructAttributeList(0,
Daniel Dunbarb7688072008-09-10 00:41:16 +0000754 CallInfo.argtypes_begin(), CallInfo.argtypes_end(),
Devang Patel761d7f72008-09-25 21:02:23 +0000755 AttributeList);
756 CI->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(),
757 AttributeList.size()));
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000758
759 if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee))
760 CI->setCallingConv(F->getCallingConv());
761 if (CI->getType() != llvm::Type::VoidTy)
762 CI->setName("call");
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000763
764 switch (RetAI.getKind()) {
765 case ABIArgInfo::StructRet:
766 if (RetTy->isAnyComplexType())
Daniel Dunbar56273772008-09-17 00:51:38 +0000767 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000768 else
769 // Struct return.
Daniel Dunbar56273772008-09-17 00:51:38 +0000770 return RValue::getAggregate(Args[0]);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000771
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000772 case ABIArgInfo::Default:
773 return RValue::get(RetTy->isVoidType() ? 0 : CI);
774
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000775 case ABIArgInfo::Coerce: {
776 const llvm::Type *CoerceToPTy =
777 llvm::PointerType::getUnqual(RetAI.getCoerceToType());
778 llvm::Value *V = CreateTempAlloca(ConvertType(RetTy), "tmp");
779 Builder.CreateStore(CI, Builder.CreateBitCast(V, CoerceToPTy));
780 return RValue::getAggregate(V);
781 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000782
783 case ABIArgInfo::ByVal:
784 case ABIArgInfo::Expand:
785 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000786 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000787
788 assert(0 && "Unhandled ABIArgInfo::Kind");
789 return RValue::get(0);
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000790}