blob: 168eaf37bdeb707de427ed19c6b16bd2b0e1dda8 [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"
21#include "llvm/ParameterAttributes.h"
22using namespace clang;
23using namespace CodeGen;
24
25/***/
26
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000027// FIXME: Use iterator and sidestep silly type array creation.
28
29CGFunctionInfo::CGFunctionInfo(const FunctionDecl *FD)
30 : TheDecl(FD)
31{
32 const FunctionType *FTy = FD->getType()->getAsFunctionType();
33 const FunctionTypeProto *FTP = dyn_cast<FunctionTypeProto>(FTy);
34
35 ArgTypes.push_back(FTy->getResultType());
36 if (FTP)
37 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
38 ArgTypes.push_back(FTP->getArgType(i));
39}
40
41CGFunctionInfo::CGFunctionInfo(const ObjCMethodDecl *MD,
42 const ASTContext &Context)
43 : TheDecl(MD)
44{
45 ArgTypes.push_back(MD->getResultType());
46 ArgTypes.push_back(MD->getSelfDecl()->getType());
47 ArgTypes.push_back(Context.getObjCSelType());
48 for (ObjCMethodDecl::param_const_iterator i = MD->param_begin(),
49 e = MD->param_end(); i != e; ++i)
50 ArgTypes.push_back((*i)->getType());
51}
52
Daniel Dunbar5323a4b2008-09-10 00:32:18 +000053ArgTypeIterator CGFunctionInfo::argtypes_begin() const {
54 return ArgTypes.begin();
55}
56
57ArgTypeIterator CGFunctionInfo::argtypes_end() const {
58 return ArgTypes.end();
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000059}
60
61/***/
62
Daniel Dunbar5323a4b2008-09-10 00:32:18 +000063CGCallInfo::CGCallInfo(QualType _ResultType, const CallArgList &_Args) {
64 ArgTypes.push_back(_ResultType);
65 for (CallArgList::const_iterator i = _Args.begin(), e = _Args.end(); i!=e; ++i)
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000066 ArgTypes.push_back(i->second);
67}
68
Daniel Dunbar5323a4b2008-09-10 00:32:18 +000069ArgTypeIterator CGCallInfo::argtypes_begin() const {
70 return ArgTypes.begin();
71}
72
73ArgTypeIterator CGCallInfo::argtypes_end() const {
74 return ArgTypes.end();
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000075}
Daniel Dunbar17b708d2008-09-09 23:27:19 +000076
77/***/
78
Daniel Dunbarb7688072008-09-10 00:41:16 +000079bool CodeGenModule::ReturnTypeUsesSret(QualType RetTy) {
80 return CodeGenFunction::hasAggregateLLVMType(RetTy);
Daniel Dunbar3913f182008-09-09 23:48:28 +000081}
82
Daniel Dunbarb7688072008-09-10 00:41:16 +000083void CodeGenModule::ConstructParamAttrList(const Decl *TargetDecl,
Daniel Dunbar5323a4b2008-09-10 00:32:18 +000084 ArgTypeIterator begin,
85 ArgTypeIterator end,
86 ParamAttrListType &PAL) {
87 unsigned FuncAttrs = 0;
88
89 if (TargetDecl) {
90 if (TargetDecl->getAttr<NoThrowAttr>())
91 FuncAttrs |= llvm::ParamAttr::NoUnwind;
92 if (TargetDecl->getAttr<NoReturnAttr>())
93 FuncAttrs |= llvm::ParamAttr::NoReturn;
94 }
95
96 QualType ResTy = *begin;
97 unsigned Index = 1;
Daniel Dunbarb7688072008-09-10 00:41:16 +000098 if (ReturnTypeUsesSret(ResTy)) {
Daniel Dunbar5323a4b2008-09-10 00:32:18 +000099 PAL.push_back(llvm::ParamAttrsWithIndex::get(Index,
100 llvm::ParamAttr::StructRet));
101 ++Index;
102 } else if (ResTy->isPromotableIntegerType()) {
103 if (ResTy->isSignedIntegerType()) {
104 FuncAttrs |= llvm::ParamAttr::SExt;
105 } else if (ResTy->isUnsignedIntegerType()) {
106 FuncAttrs |= llvm::ParamAttr::ZExt;
107 }
108 }
109 if (FuncAttrs)
110 PAL.push_back(llvm::ParamAttrsWithIndex::get(0, FuncAttrs));
111 for (++begin; begin != end; ++begin, ++Index) {
112 QualType ParamType = *begin;
113 unsigned ParamAttrs = 0;
114 if (ParamType->isRecordType())
115 ParamAttrs |= llvm::ParamAttr::ByVal;
116 if (ParamType->isPromotableIntegerType()) {
117 if (ParamType->isSignedIntegerType()) {
118 ParamAttrs |= llvm::ParamAttr::SExt;
119 } else if (ParamType->isUnsignedIntegerType()) {
120 ParamAttrs |= llvm::ParamAttr::ZExt;
121 }
122 }
123 if (ParamAttrs)
124 PAL.push_back(llvm::ParamAttrsWithIndex::get(Index, ParamAttrs));
125 }
126}
127
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000128void CodeGenFunction::EmitFunctionProlog(llvm::Function *Fn,
129 QualType RetTy,
130 const FunctionArgList &Args) {
131 // Emit allocs for param decls. Give the LLVM Argument nodes names.
132 llvm::Function::arg_iterator AI = Fn->arg_begin();
133
134 // Name the struct return argument.
135 if (hasAggregateLLVMType(RetTy)) {
136 AI->setName("agg.result");
137 ++AI;
138 }
139
140 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
141 i != e; ++i, ++AI) {
142 const VarDecl *Arg = i->first;
143 QualType T = i->second;
144 assert(AI != Fn->arg_end() && "Argument mismatch!");
145 llvm::Value* V = AI;
146 if (!getContext().typesAreCompatible(T, Arg->getType())) {
147 // This must be a promotion, for something like
148 // "void a(x) short x; {..."
149 V = EmitScalarConversion(V, T, Arg->getType());
150 }
151 EmitParmDecl(*Arg, V);
152 }
153 assert(AI == Fn->arg_end() && "Argument mismatch!");
154}
155
156void CodeGenFunction::EmitFunctionEpilog(QualType RetTy,
157 llvm::Value *ReturnValue) {
158 if (!ReturnValue) {
159 Builder.CreateRetVoid();
160 } else {
161 if (!hasAggregateLLVMType(RetTy)) {
162 Builder.CreateRet(Builder.CreateLoad(ReturnValue));
163 } else if (RetTy->isAnyComplexType()) {
164 EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy);
165 Builder.CreateRetVoid();
166 } else {
167 EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy);
168 Builder.CreateRetVoid();
169 }
170 }
171}
172
173RValue CodeGenFunction::EmitCall(llvm::Value *Callee,
174 QualType ResultType,
175 const CallArgList &CallArgs) {
176 // FIXME: Factor out code to load from args into locals into target.
177 llvm::SmallVector<llvm::Value*, 16> Args;
178 llvm::Value *TempArg0 = 0;
179
180 // Handle struct-return functions by passing a pointer to the
181 // location that we would like to return into.
182 if (hasAggregateLLVMType(ResultType)) {
183 // Create a temporary alloca to hold the result of the call. :(
184 TempArg0 = CreateTempAlloca(ConvertType(ResultType));
185 Args.push_back(TempArg0);
186 }
187
188 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
189 I != E; ++I) {
190 RValue RV = I->first;
191 if (RV.isScalar()) {
192 Args.push_back(RV.getScalarVal());
193 } else if (RV.isComplex()) {
194 // Make a temporary alloca to pass the argument.
195 Args.push_back(CreateTempAlloca(ConvertType(I->second)));
196 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
197 } else {
198 Args.push_back(RV.getAggregateAddr());
199 }
200 }
201
202 llvm::CallInst *CI = Builder.CreateCall(Callee,&Args[0],&Args[0]+Args.size());
203 CGCallInfo CallInfo(ResultType, CallArgs);
204
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000205 // FIXME: Provide TargetDecl so nounwind, noreturn, etc, etc get set.
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000206 CodeGen::ParamAttrListType ParamAttrList;
Daniel Dunbarb7688072008-09-10 00:41:16 +0000207 CGM.ConstructParamAttrList(0,
208 CallInfo.argtypes_begin(), CallInfo.argtypes_end(),
209 ParamAttrList);
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000210 CI->setParamAttrs(llvm::PAListPtr::get(ParamAttrList.begin(),
211 ParamAttrList.size()));
212
213 if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee))
214 CI->setCallingConv(F->getCallingConv());
215 if (CI->getType() != llvm::Type::VoidTy)
216 CI->setName("call");
217 else if (ResultType->isAnyComplexType())
218 return RValue::getComplex(LoadComplexFromAddr(TempArg0, false));
219 else if (hasAggregateLLVMType(ResultType))
220 // Struct return.
221 return RValue::getAggregate(TempArg0);
222 else {
223 // void return.
224 assert(ResultType->isVoidType() && "Should only have a void expr here");
225 CI = 0;
226 }
227
228 return RValue::get(CI);
229}