blob: 85c3320ec0eefbb92ae773c616cc3e3b6b5bee17 [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#ifndef CLANG_CODEGEN_CGCALL_H
16#define CLANG_CODEGEN_CGCALL_H
17
Chandler Carruth55fc8732012-12-04 09:13:33 +000018#include "CGValue.h"
19#include "clang/AST/CanonicalType.h"
20#include "clang/AST/Type.h"
Anders Carlsson31777a22009-12-24 19:08:58 +000021#include "llvm/ADT/FoldingSet.h"
Chandler Carruth3b844ba2013-01-02 11:45:17 +000022#include "llvm/IR/Value.h"
Daniel Dunbar46f45b92008-09-09 01:06:48 +000023
Daniel Dunbar88c2fa92009-02-03 05:31:23 +000024// FIXME: Restructure so we don't have to expose so much stuff.
25#include "ABIInfo.h"
26
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000027namespace llvm {
Bill Wendlingb263bdf2013-01-27 02:46:53 +000028 class AttributeSet;
Daniel Dunbar88c2fa92009-02-03 05:31:23 +000029 class Function;
30 class Type;
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000031 class Value;
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000032}
33
34namespace clang {
35 class ASTContext;
36 class Decl;
37 class FunctionDecl;
38 class ObjCMethodDecl;
Daniel Dunbar7c086512008-09-09 23:14:03 +000039 class VarDecl;
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000040
41namespace CodeGen {
Bill Wendlingb263bdf2013-01-27 02:46:53 +000042 typedef SmallVector<llvm::AttributeSet, 8> AttributeListType;
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000043
Eli Friedmanc6d07822011-05-02 18:05:27 +000044 struct CallArg {
45 RValue RV;
46 QualType Ty;
Eli Friedman55d48482011-05-26 00:10:27 +000047 bool NeedsCopy;
48 CallArg(RValue rv, QualType ty, bool needscopy)
49 : RV(rv), Ty(ty), NeedsCopy(needscopy)
Eli Friedmanc6d07822011-05-02 18:05:27 +000050 { }
51 };
52
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000053 /// CallArgList - Type for representing both the value and type of
54 /// arguments in a call.
John McCalld26bc762011-03-09 04:27:21 +000055 class CallArgList :
Chris Lattner686775d2011-07-20 06:58:45 +000056 public SmallVector<CallArg, 16> {
John McCall413ebdb2011-03-11 20:59:21 +000057 public:
John McCallf85e1932011-06-15 23:02:42 +000058 struct Writeback {
John McCallb6a60792013-03-23 02:35:54 +000059 /// The original argument. Note that the argument l-value
60 /// is potentially null.
61 LValue Source;
John McCallf85e1932011-06-15 23:02:42 +000062
63 /// The temporary alloca.
64 llvm::Value *Temporary;
John McCallb6a60792013-03-23 02:35:54 +000065
66 /// A value to "use" after the writeback, or null.
67 llvm::Value *ToUse;
John McCallf85e1932011-06-15 23:02:42 +000068 };
69
Eli Friedman55d48482011-05-26 00:10:27 +000070 void add(RValue rvalue, QualType type, bool needscopy = false) {
71 push_back(CallArg(rvalue, type, needscopy));
John McCall413ebdb2011-03-11 20:59:21 +000072 }
John McCallf85e1932011-06-15 23:02:42 +000073
74 void addFrom(const CallArgList &other) {
75 insert(end(), other.begin(), other.end());
76 Writebacks.insert(Writebacks.end(),
77 other.Writebacks.begin(), other.Writebacks.end());
78 }
79
John McCallb6a60792013-03-23 02:35:54 +000080 void addWriteback(LValue srcLV, llvm::Value *temporary,
81 llvm::Value *toUse) {
John McCallf85e1932011-06-15 23:02:42 +000082 Writeback writeback;
John McCallb6a60792013-03-23 02:35:54 +000083 writeback.Source = srcLV;
John McCallf85e1932011-06-15 23:02:42 +000084 writeback.Temporary = temporary;
John McCallb6a60792013-03-23 02:35:54 +000085 writeback.ToUse = toUse;
John McCallf85e1932011-06-15 23:02:42 +000086 Writebacks.push_back(writeback);
87 }
88
89 bool hasWritebacks() const { return !Writebacks.empty(); }
90
Chris Lattner686775d2011-07-20 06:58:45 +000091 typedef SmallVectorImpl<Writeback>::const_iterator writeback_iterator;
John McCallf85e1932011-06-15 23:02:42 +000092 writeback_iterator writeback_begin() const { return Writebacks.begin(); }
93 writeback_iterator writeback_end() const { return Writebacks.end(); }
94
95 private:
Chris Lattner686775d2011-07-20 06:58:45 +000096 SmallVector<Writeback, 1> Writebacks;
John McCalld26bc762011-03-09 04:27:21 +000097 };
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000098
John McCallde5d3c72012-02-17 03:33:10 +000099 /// A class for recording the number of arguments that a function
100 /// signature requires.
101 class RequiredArgs {
102 /// The number of required arguments, or ~0 if the signature does
103 /// not permit optional arguments.
104 unsigned NumRequired;
105 public:
106 enum All_t { All };
107
108 RequiredArgs(All_t _) : NumRequired(~0U) {}
109 explicit RequiredArgs(unsigned n) : NumRequired(n) {
110 assert(n != ~0U);
111 }
112
113 /// Compute the arguments required by the given formal prototype,
114 /// given that there may be some additional, non-formal arguments
115 /// in play.
116 static RequiredArgs forPrototypePlus(const FunctionProtoType *prototype,
117 unsigned additional) {
118 if (!prototype->isVariadic()) return All;
119 return RequiredArgs(prototype->getNumArgs() + additional);
120 }
121
122 static RequiredArgs forPrototype(const FunctionProtoType *prototype) {
123 return forPrototypePlus(prototype, 0);
124 }
125
126 static RequiredArgs forPrototype(CanQual<FunctionProtoType> prototype) {
127 return forPrototype(prototype.getTypePtr());
128 }
129
130 static RequiredArgs forPrototypePlus(CanQual<FunctionProtoType> prototype,
131 unsigned additional) {
132 return forPrototypePlus(prototype.getTypePtr(), additional);
133 }
134
135 bool allowsOptionalArgs() const { return NumRequired != ~0U; }
John McCalle56bb362012-12-07 07:03:17 +0000136 unsigned getNumRequiredArgs() const {
John McCallde5d3c72012-02-17 03:33:10 +0000137 assert(allowsOptionalArgs());
138 return NumRequired;
139 }
140
141 unsigned getOpaqueData() const { return NumRequired; }
142 static RequiredArgs getFromOpaqueData(unsigned value) {
143 if (value == ~0U) return All;
144 return RequiredArgs(value);
145 }
146 };
147
Daniel Dunbar7c086512008-09-09 23:14:03 +0000148 /// FunctionArgList - Type for representing both the decl and type
149 /// of parameters to a function. The decl must be either a
150 /// ParmVarDecl or ImplicitParamDecl.
Chris Lattner686775d2011-07-20 06:58:45 +0000151 class FunctionArgList : public SmallVector<const VarDecl*, 16> {
John McCalld26bc762011-03-09 04:27:21 +0000152 };
Mike Stump1eb44332009-09-09 15:08:12 +0000153
Daniel Dunbar0dbe2272008-09-08 21:33:45 +0000154 /// CGFunctionInfo - Class to encapsulate the information about a
155 /// function definition.
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000156 class CGFunctionInfo : public llvm::FoldingSetNode {
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000157 struct ArgInfo {
John McCallead608a2010-02-26 00:48:12 +0000158 CanQualType type;
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000159 ABIArgInfo info;
160 };
161
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000162 /// The LLVM::CallingConv to use for this function (as specified by the
163 /// user).
John McCallde5d3c72012-02-17 03:33:10 +0000164 unsigned CallingConvention : 8;
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000165
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000166 /// The LLVM::CallingConv to actually use for this function, which may
167 /// depend on the ABI.
John McCallde5d3c72012-02-17 03:33:10 +0000168 unsigned EffectiveCallingConvention : 8;
169
170 /// The clang::CallingConv that this was originally created with.
171 unsigned ASTCallingConvention : 8;
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000172
John McCall04a67a62010-02-05 21:31:56 +0000173 /// Whether this function is noreturn.
John McCallde5d3c72012-02-17 03:33:10 +0000174 unsigned NoReturn : 1;
John McCall04a67a62010-02-05 21:31:56 +0000175
John McCallf85e1932011-06-15 23:02:42 +0000176 /// Whether this function is returns-retained.
John McCallde5d3c72012-02-17 03:33:10 +0000177 unsigned ReturnsRetained : 1;
Daniel Dunbar0dbe2272008-09-08 21:33:45 +0000178
Rafael Espindola425ef722010-03-30 22:15:11 +0000179 /// How many arguments to pass inreg.
John McCallde5d3c72012-02-17 03:33:10 +0000180 unsigned HasRegParm : 1;
181 unsigned RegParm : 4;
182
183 RequiredArgs Required;
184
185 unsigned NumArgs;
186 ArgInfo *getArgsBuffer() {
187 return reinterpret_cast<ArgInfo*>(this+1);
188 }
189 const ArgInfo *getArgsBuffer() const {
190 return reinterpret_cast<const ArgInfo*>(this + 1);
191 }
192
193 CGFunctionInfo() : Required(RequiredArgs::All) {}
Rafael Espindola425ef722010-03-30 22:15:11 +0000194
Daniel Dunbar0dbe2272008-09-08 21:33:45 +0000195 public:
John McCallde5d3c72012-02-17 03:33:10 +0000196 static CGFunctionInfo *create(unsigned llvmCC,
197 const FunctionType::ExtInfo &extInfo,
198 CanQualType resultType,
199 ArrayRef<CanQualType> argTypes,
200 RequiredArgs required);
201
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000202 typedef const ArgInfo *const_arg_iterator;
203 typedef ArgInfo *arg_iterator;
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000204
John McCallde5d3c72012-02-17 03:33:10 +0000205 const_arg_iterator arg_begin() const { return getArgsBuffer() + 1; }
206 const_arg_iterator arg_end() const { return getArgsBuffer() + 1 + NumArgs; }
207 arg_iterator arg_begin() { return getArgsBuffer() + 1; }
208 arg_iterator arg_end() { return getArgsBuffer() + 1 + NumArgs; }
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000209
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +0000210 unsigned arg_size() const { return NumArgs; }
211
John McCallde5d3c72012-02-17 03:33:10 +0000212 bool isVariadic() const { return Required.allowsOptionalArgs(); }
213 RequiredArgs getRequiredArgs() const { return Required; }
214
John McCall04a67a62010-02-05 21:31:56 +0000215 bool isNoReturn() const { return NoReturn; }
216
John McCallde5d3c72012-02-17 03:33:10 +0000217 /// In ARC, whether this function retains its return value. This
John McCallf85e1932011-06-15 23:02:42 +0000218 /// is not always reliable for call sites.
219 bool isReturnsRetained() const { return ReturnsRetained; }
220
John McCallde5d3c72012-02-17 03:33:10 +0000221 /// getASTCallingConvention() - Return the AST-specified calling
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000222 /// convention.
John McCallde5d3c72012-02-17 03:33:10 +0000223 CallingConv getASTCallingConvention() const {
224 return CallingConv(ASTCallingConvention);
225 }
226
227 /// getCallingConvention - Return the user specified calling
228 /// convention, which has been translated into an LLVM CC.
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000229 unsigned getCallingConvention() const { return CallingConvention; }
230
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000231 /// getEffectiveCallingConvention - Return the actual calling convention to
232 /// use, which may depend on the ABI.
233 unsigned getEffectiveCallingConvention() const {
234 return EffectiveCallingConvention;
235 }
236 void setEffectiveCallingConvention(unsigned Value) {
237 EffectiveCallingConvention = Value;
238 }
239
Eli Friedmana49218e2011-04-09 08:18:08 +0000240 bool getHasRegParm() const { return HasRegParm; }
Rafael Espindola425ef722010-03-30 22:15:11 +0000241 unsigned getRegParm() const { return RegParm; }
242
John McCallde5d3c72012-02-17 03:33:10 +0000243 FunctionType::ExtInfo getExtInfo() const {
244 return FunctionType::ExtInfo(isNoReturn(),
245 getHasRegParm(), getRegParm(),
246 getASTCallingConvention(),
247 isReturnsRetained());
248 }
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000249
John McCallde5d3c72012-02-17 03:33:10 +0000250 CanQualType getReturnType() const { return getArgsBuffer()[0].type; }
251
252 ABIArgInfo &getReturnInfo() { return getArgsBuffer()[0].info; }
253 const ABIArgInfo &getReturnInfo() const { return getArgsBuffer()[0].info; }
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000254
255 void Profile(llvm::FoldingSetNodeID &ID) {
John McCallde5d3c72012-02-17 03:33:10 +0000256 ID.AddInteger(getASTCallingConvention());
John McCall04a67a62010-02-05 21:31:56 +0000257 ID.AddBoolean(NoReturn);
John McCallf85e1932011-06-15 23:02:42 +0000258 ID.AddBoolean(ReturnsRetained);
Eli Friedmana49218e2011-04-09 08:18:08 +0000259 ID.AddBoolean(HasRegParm);
Rafael Espindola425ef722010-03-30 22:15:11 +0000260 ID.AddInteger(RegParm);
John McCallde5d3c72012-02-17 03:33:10 +0000261 ID.AddInteger(Required.getOpaqueData());
Daniel Dunbar35e67d42009-02-05 00:00:23 +0000262 getReturnType().Profile(ID);
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000263 for (arg_iterator it = arg_begin(), ie = arg_end(); it != ie; ++it)
264 it->type.Profile(ID);
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000265 }
Mike Stump1eb44332009-09-09 15:08:12 +0000266 static void Profile(llvm::FoldingSetNodeID &ID,
John McCallde5d3c72012-02-17 03:33:10 +0000267 const FunctionType::ExtInfo &info,
268 RequiredArgs required,
269 CanQualType resultType,
270 ArrayRef<CanQualType> argTypes) {
271 ID.AddInteger(info.getCC());
272 ID.AddBoolean(info.getNoReturn());
273 ID.AddBoolean(info.getProducesResult());
274 ID.AddBoolean(info.getHasRegParm());
275 ID.AddInteger(info.getRegParm());
276 ID.AddInteger(required.getOpaqueData());
277 resultType.Profile(ID);
278 for (ArrayRef<CanQualType>::iterator
279 i = argTypes.begin(), e = argTypes.end(); i != e; ++i) {
280 i->Profile(ID);
John McCallead608a2010-02-26 00:48:12 +0000281 }
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000282 }
Daniel Dunbar0dbe2272008-09-08 21:33:45 +0000283 };
Anders Carlsson31777a22009-12-24 19:08:58 +0000284
Anders Carlssond2490a92009-12-24 20:40:36 +0000285 /// ReturnValueSlot - Contains the address where the return value of a
286 /// function can be stored, and whether the address is volatile or not.
Anders Carlsson31777a22009-12-24 19:08:58 +0000287 class ReturnValueSlot {
288 llvm::PointerIntPair<llvm::Value *, 1, bool> Value;
289
290 public:
291 ReturnValueSlot() {}
292 ReturnValueSlot(llvm::Value *Value, bool IsVolatile)
293 : Value(Value, IsVolatile) {}
294
295 bool isNull() const { return !getValue(); }
296
297 bool isVolatile() const { return Value.getInt(); }
298 llvm::Value *getValue() const { return Value.getPointer(); }
299 };
300
Daniel Dunbar0dbe2272008-09-08 21:33:45 +0000301} // end namespace CodeGen
302} // end namespace clang
303
304#endif