blob: dead7bd459106c7bd4f9fbcbde4d82f2a881d68b [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
Anders Carlsson31777a22009-12-24 19:08:58 +000018#include "llvm/ADT/FoldingSet.h"
19#include "llvm/Value.h"
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000020#include "clang/AST/Type.h"
John McCallead608a2010-02-26 00:48:12 +000021#include "clang/AST/CanonicalType.h"
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000022
Daniel Dunbar46f45b92008-09-09 01:06:48 +000023#include "CGValue.h"
24
Daniel Dunbar88c2fa92009-02-03 05:31:23 +000025// FIXME: Restructure so we don't have to expose so much stuff.
26#include "ABIInfo.h"
27
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000028namespace llvm {
Devang Patel761d7f72008-09-25 21:02:23 +000029 struct AttributeWithIndex;
Daniel Dunbar88c2fa92009-02-03 05:31:23 +000030 class Function;
31 class Type;
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000032 class Value;
33
34 template<typename T, unsigned> class SmallVector;
35}
36
37namespace clang {
38 class ASTContext;
39 class Decl;
40 class FunctionDecl;
41 class ObjCMethodDecl;
Daniel Dunbar7c086512008-09-09 23:14:03 +000042 class VarDecl;
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000043
44namespace CodeGen {
Chris Lattner686775d2011-07-20 06:58:45 +000045 typedef SmallVector<llvm::AttributeWithIndex, 8> AttributeListType;
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000046
Eli Friedmanc6d07822011-05-02 18:05:27 +000047 struct CallArg {
48 RValue RV;
49 QualType Ty;
Eli Friedman55d48482011-05-26 00:10:27 +000050 bool NeedsCopy;
51 CallArg(RValue rv, QualType ty, bool needscopy)
52 : RV(rv), Ty(ty), NeedsCopy(needscopy)
Eli Friedmanc6d07822011-05-02 18:05:27 +000053 { }
54 };
55
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000056 /// CallArgList - Type for representing both the value and type of
57 /// arguments in a call.
John McCalld26bc762011-03-09 04:27:21 +000058 class CallArgList :
Chris Lattner686775d2011-07-20 06:58:45 +000059 public SmallVector<CallArg, 16> {
John McCall413ebdb2011-03-11 20:59:21 +000060 public:
John McCallf85e1932011-06-15 23:02:42 +000061 struct Writeback {
62 /// The original argument.
63 llvm::Value *Address;
64
65 /// The pointee type of the original argument.
66 QualType AddressType;
67
68 /// The temporary alloca.
69 llvm::Value *Temporary;
70 };
71
Eli Friedman55d48482011-05-26 00:10:27 +000072 void add(RValue rvalue, QualType type, bool needscopy = false) {
73 push_back(CallArg(rvalue, type, needscopy));
John McCall413ebdb2011-03-11 20:59:21 +000074 }
John McCallf85e1932011-06-15 23:02:42 +000075
76 void addFrom(const CallArgList &other) {
77 insert(end(), other.begin(), other.end());
78 Writebacks.insert(Writebacks.end(),
79 other.Writebacks.begin(), other.Writebacks.end());
80 }
81
82 void addWriteback(llvm::Value *address, QualType addressType,
83 llvm::Value *temporary) {
84 Writeback writeback;
85 writeback.Address = address;
86 writeback.AddressType = addressType;
87 writeback.Temporary = temporary;
88 Writebacks.push_back(writeback);
89 }
90
91 bool hasWritebacks() const { return !Writebacks.empty(); }
92
Chris Lattner686775d2011-07-20 06:58:45 +000093 typedef SmallVectorImpl<Writeback>::const_iterator writeback_iterator;
John McCallf85e1932011-06-15 23:02:42 +000094 writeback_iterator writeback_begin() const { return Writebacks.begin(); }
95 writeback_iterator writeback_end() const { return Writebacks.end(); }
96
97 private:
Chris Lattner686775d2011-07-20 06:58:45 +000098 SmallVector<Writeback, 1> Writebacks;
John McCalld26bc762011-03-09 04:27:21 +000099 };
Daniel Dunbar0dbe2272008-09-08 21:33:45 +0000100
John McCallde5d3c72012-02-17 03:33:10 +0000101 /// A class for recording the number of arguments that a function
102 /// signature requires.
103 class RequiredArgs {
104 /// The number of required arguments, or ~0 if the signature does
105 /// not permit optional arguments.
106 unsigned NumRequired;
107 public:
108 enum All_t { All };
109
110 RequiredArgs(All_t _) : NumRequired(~0U) {}
111 explicit RequiredArgs(unsigned n) : NumRequired(n) {
112 assert(n != ~0U);
113 }
114
115 /// Compute the arguments required by the given formal prototype,
116 /// given that there may be some additional, non-formal arguments
117 /// in play.
118 static RequiredArgs forPrototypePlus(const FunctionProtoType *prototype,
119 unsigned additional) {
120 if (!prototype->isVariadic()) return All;
121 return RequiredArgs(prototype->getNumArgs() + additional);
122 }
123
124 static RequiredArgs forPrototype(const FunctionProtoType *prototype) {
125 return forPrototypePlus(prototype, 0);
126 }
127
128 static RequiredArgs forPrototype(CanQual<FunctionProtoType> prototype) {
129 return forPrototype(prototype.getTypePtr());
130 }
131
132 static RequiredArgs forPrototypePlus(CanQual<FunctionProtoType> prototype,
133 unsigned additional) {
134 return forPrototypePlus(prototype.getTypePtr(), additional);
135 }
136
137 bool allowsOptionalArgs() const { return NumRequired != ~0U; }
138 bool getNumRequiredArgs() const {
139 assert(allowsOptionalArgs());
140 return NumRequired;
141 }
142
143 unsigned getOpaqueData() const { return NumRequired; }
144 static RequiredArgs getFromOpaqueData(unsigned value) {
145 if (value == ~0U) return All;
146 return RequiredArgs(value);
147 }
148 };
149
Daniel Dunbar7c086512008-09-09 23:14:03 +0000150 /// FunctionArgList - Type for representing both the decl and type
151 /// of parameters to a function. The decl must be either a
152 /// ParmVarDecl or ImplicitParamDecl.
Chris Lattner686775d2011-07-20 06:58:45 +0000153 class FunctionArgList : public SmallVector<const VarDecl*, 16> {
John McCalld26bc762011-03-09 04:27:21 +0000154 };
Mike Stump1eb44332009-09-09 15:08:12 +0000155
Daniel Dunbar0dbe2272008-09-08 21:33:45 +0000156 /// CGFunctionInfo - Class to encapsulate the information about a
157 /// function definition.
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000158 class CGFunctionInfo : public llvm::FoldingSetNode {
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000159 struct ArgInfo {
John McCallead608a2010-02-26 00:48:12 +0000160 CanQualType type;
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000161 ABIArgInfo info;
162 };
163
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000164 /// The LLVM::CallingConv to use for this function (as specified by the
165 /// user).
John McCallde5d3c72012-02-17 03:33:10 +0000166 unsigned CallingConvention : 8;
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000167
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000168 /// The LLVM::CallingConv to actually use for this function, which may
169 /// depend on the ABI.
John McCallde5d3c72012-02-17 03:33:10 +0000170 unsigned EffectiveCallingConvention : 8;
171
172 /// The clang::CallingConv that this was originally created with.
173 unsigned ASTCallingConvention : 8;
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000174
John McCall04a67a62010-02-05 21:31:56 +0000175 /// Whether this function is noreturn.
John McCallde5d3c72012-02-17 03:33:10 +0000176 unsigned NoReturn : 1;
John McCall04a67a62010-02-05 21:31:56 +0000177
John McCallf85e1932011-06-15 23:02:42 +0000178 /// Whether this function is returns-retained.
John McCallde5d3c72012-02-17 03:33:10 +0000179 unsigned ReturnsRetained : 1;
Daniel Dunbar0dbe2272008-09-08 21:33:45 +0000180
Rafael Espindola425ef722010-03-30 22:15:11 +0000181 /// How many arguments to pass inreg.
John McCallde5d3c72012-02-17 03:33:10 +0000182 unsigned HasRegParm : 1;
183 unsigned RegParm : 4;
184
185 RequiredArgs Required;
186
187 unsigned NumArgs;
188 ArgInfo *getArgsBuffer() {
189 return reinterpret_cast<ArgInfo*>(this+1);
190 }
191 const ArgInfo *getArgsBuffer() const {
192 return reinterpret_cast<const ArgInfo*>(this + 1);
193 }
194
195 CGFunctionInfo() : Required(RequiredArgs::All) {}
Rafael Espindola425ef722010-03-30 22:15:11 +0000196
Daniel Dunbar0dbe2272008-09-08 21:33:45 +0000197 public:
John McCallde5d3c72012-02-17 03:33:10 +0000198 static CGFunctionInfo *create(unsigned llvmCC,
199 const FunctionType::ExtInfo &extInfo,
200 CanQualType resultType,
201 ArrayRef<CanQualType> argTypes,
202 RequiredArgs required);
203
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000204 typedef const ArgInfo *const_arg_iterator;
205 typedef ArgInfo *arg_iterator;
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000206
John McCallde5d3c72012-02-17 03:33:10 +0000207 const_arg_iterator arg_begin() const { return getArgsBuffer() + 1; }
208 const_arg_iterator arg_end() const { return getArgsBuffer() + 1 + NumArgs; }
209 arg_iterator arg_begin() { return getArgsBuffer() + 1; }
210 arg_iterator arg_end() { return getArgsBuffer() + 1 + NumArgs; }
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000211
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +0000212 unsigned arg_size() const { return NumArgs; }
213
John McCallde5d3c72012-02-17 03:33:10 +0000214 bool isVariadic() const { return Required.allowsOptionalArgs(); }
215 RequiredArgs getRequiredArgs() const { return Required; }
216
John McCall04a67a62010-02-05 21:31:56 +0000217 bool isNoReturn() const { return NoReturn; }
218
John McCallde5d3c72012-02-17 03:33:10 +0000219 /// In ARC, whether this function retains its return value. This
John McCallf85e1932011-06-15 23:02:42 +0000220 /// is not always reliable for call sites.
221 bool isReturnsRetained() const { return ReturnsRetained; }
222
John McCallde5d3c72012-02-17 03:33:10 +0000223 /// getASTCallingConvention() - Return the AST-specified calling
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000224 /// convention.
John McCallde5d3c72012-02-17 03:33:10 +0000225 CallingConv getASTCallingConvention() const {
226 return CallingConv(ASTCallingConvention);
227 }
228
229 /// getCallingConvention - Return the user specified calling
230 /// convention, which has been translated into an LLVM CC.
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000231 unsigned getCallingConvention() const { return CallingConvention; }
232
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000233 /// getEffectiveCallingConvention - Return the actual calling convention to
234 /// use, which may depend on the ABI.
235 unsigned getEffectiveCallingConvention() const {
236 return EffectiveCallingConvention;
237 }
238 void setEffectiveCallingConvention(unsigned Value) {
239 EffectiveCallingConvention = Value;
240 }
241
Eli Friedmana49218e2011-04-09 08:18:08 +0000242 bool getHasRegParm() const { return HasRegParm; }
Rafael Espindola425ef722010-03-30 22:15:11 +0000243 unsigned getRegParm() const { return RegParm; }
244
John McCallde5d3c72012-02-17 03:33:10 +0000245 FunctionType::ExtInfo getExtInfo() const {
246 return FunctionType::ExtInfo(isNoReturn(),
247 getHasRegParm(), getRegParm(),
248 getASTCallingConvention(),
249 isReturnsRetained());
250 }
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000251
John McCallde5d3c72012-02-17 03:33:10 +0000252 CanQualType getReturnType() const { return getArgsBuffer()[0].type; }
253
254 ABIArgInfo &getReturnInfo() { return getArgsBuffer()[0].info; }
255 const ABIArgInfo &getReturnInfo() const { return getArgsBuffer()[0].info; }
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000256
257 void Profile(llvm::FoldingSetNodeID &ID) {
John McCallde5d3c72012-02-17 03:33:10 +0000258 ID.AddInteger(getASTCallingConvention());
John McCall04a67a62010-02-05 21:31:56 +0000259 ID.AddBoolean(NoReturn);
John McCallf85e1932011-06-15 23:02:42 +0000260 ID.AddBoolean(ReturnsRetained);
Eli Friedmana49218e2011-04-09 08:18:08 +0000261 ID.AddBoolean(HasRegParm);
Rafael Espindola425ef722010-03-30 22:15:11 +0000262 ID.AddInteger(RegParm);
John McCallde5d3c72012-02-17 03:33:10 +0000263 ID.AddInteger(Required.getOpaqueData());
Daniel Dunbar35e67d42009-02-05 00:00:23 +0000264 getReturnType().Profile(ID);
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000265 for (arg_iterator it = arg_begin(), ie = arg_end(); it != ie; ++it)
266 it->type.Profile(ID);
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000267 }
Mike Stump1eb44332009-09-09 15:08:12 +0000268 static void Profile(llvm::FoldingSetNodeID &ID,
John McCallde5d3c72012-02-17 03:33:10 +0000269 const FunctionType::ExtInfo &info,
270 RequiredArgs required,
271 CanQualType resultType,
272 ArrayRef<CanQualType> argTypes) {
273 ID.AddInteger(info.getCC());
274 ID.AddBoolean(info.getNoReturn());
275 ID.AddBoolean(info.getProducesResult());
276 ID.AddBoolean(info.getHasRegParm());
277 ID.AddInteger(info.getRegParm());
278 ID.AddInteger(required.getOpaqueData());
279 resultType.Profile(ID);
280 for (ArrayRef<CanQualType>::iterator
281 i = argTypes.begin(), e = argTypes.end(); i != e; ++i) {
282 i->Profile(ID);
John McCallead608a2010-02-26 00:48:12 +0000283 }
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000284 }
Daniel Dunbar0dbe2272008-09-08 21:33:45 +0000285 };
Anders Carlsson31777a22009-12-24 19:08:58 +0000286
Anders Carlssond2490a92009-12-24 20:40:36 +0000287 /// ReturnValueSlot - Contains the address where the return value of a
288 /// function can be stored, and whether the address is volatile or not.
Anders Carlsson31777a22009-12-24 19:08:58 +0000289 class ReturnValueSlot {
290 llvm::PointerIntPair<llvm::Value *, 1, bool> Value;
291
292 public:
293 ReturnValueSlot() {}
294 ReturnValueSlot(llvm::Value *Value, bool IsVolatile)
295 : Value(Value, IsVolatile) {}
296
297 bool isNull() const { return !getValue(); }
298
299 bool isVolatile() const { return Value.getInt(); }
300 llvm::Value *getValue() const { return Value.getPointer(); }
301 };
302
Daniel Dunbar0dbe2272008-09-08 21:33:45 +0000303} // end namespace CodeGen
304} // end namespace clang
305
306#endif