blob: 734ba69c8778a26e9a94179afa3dac5fc96e9a6a [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 {
Devang Patel761d7f72008-09-25 21:02:23 +000028 struct AttributeWithIndex;
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 {
Chris Lattner686775d2011-07-20 06:58:45 +000042 typedef SmallVector<llvm::AttributeWithIndex, 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 {
59 /// The original argument.
60 llvm::Value *Address;
61
62 /// The pointee type of the original argument.
63 QualType AddressType;
64
65 /// The temporary alloca.
66 llvm::Value *Temporary;
67 };
68
Eli Friedman55d48482011-05-26 00:10:27 +000069 void add(RValue rvalue, QualType type, bool needscopy = false) {
70 push_back(CallArg(rvalue, type, needscopy));
John McCall413ebdb2011-03-11 20:59:21 +000071 }
John McCallf85e1932011-06-15 23:02:42 +000072
73 void addFrom(const CallArgList &other) {
74 insert(end(), other.begin(), other.end());
75 Writebacks.insert(Writebacks.end(),
76 other.Writebacks.begin(), other.Writebacks.end());
77 }
78
79 void addWriteback(llvm::Value *address, QualType addressType,
80 llvm::Value *temporary) {
81 Writeback writeback;
82 writeback.Address = address;
83 writeback.AddressType = addressType;
84 writeback.Temporary = temporary;
85 Writebacks.push_back(writeback);
86 }
87
88 bool hasWritebacks() const { return !Writebacks.empty(); }
89
Chris Lattner686775d2011-07-20 06:58:45 +000090 typedef SmallVectorImpl<Writeback>::const_iterator writeback_iterator;
John McCallf85e1932011-06-15 23:02:42 +000091 writeback_iterator writeback_begin() const { return Writebacks.begin(); }
92 writeback_iterator writeback_end() const { return Writebacks.end(); }
93
94 private:
Chris Lattner686775d2011-07-20 06:58:45 +000095 SmallVector<Writeback, 1> Writebacks;
John McCalld26bc762011-03-09 04:27:21 +000096 };
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000097
John McCallde5d3c72012-02-17 03:33:10 +000098 /// A class for recording the number of arguments that a function
99 /// signature requires.
100 class RequiredArgs {
101 /// The number of required arguments, or ~0 if the signature does
102 /// not permit optional arguments.
103 unsigned NumRequired;
104 public:
105 enum All_t { All };
106
107 RequiredArgs(All_t _) : NumRequired(~0U) {}
108 explicit RequiredArgs(unsigned n) : NumRequired(n) {
109 assert(n != ~0U);
110 }
111
112 /// Compute the arguments required by the given formal prototype,
113 /// given that there may be some additional, non-formal arguments
114 /// in play.
115 static RequiredArgs forPrototypePlus(const FunctionProtoType *prototype,
116 unsigned additional) {
117 if (!prototype->isVariadic()) return All;
118 return RequiredArgs(prototype->getNumArgs() + additional);
119 }
120
121 static RequiredArgs forPrototype(const FunctionProtoType *prototype) {
122 return forPrototypePlus(prototype, 0);
123 }
124
125 static RequiredArgs forPrototype(CanQual<FunctionProtoType> prototype) {
126 return forPrototype(prototype.getTypePtr());
127 }
128
129 static RequiredArgs forPrototypePlus(CanQual<FunctionProtoType> prototype,
130 unsigned additional) {
131 return forPrototypePlus(prototype.getTypePtr(), additional);
132 }
133
134 bool allowsOptionalArgs() const { return NumRequired != ~0U; }
John McCalle56bb362012-12-07 07:03:17 +0000135 unsigned getNumRequiredArgs() const {
John McCallde5d3c72012-02-17 03:33:10 +0000136 assert(allowsOptionalArgs());
137 return NumRequired;
138 }
139
140 unsigned getOpaqueData() const { return NumRequired; }
141 static RequiredArgs getFromOpaqueData(unsigned value) {
142 if (value == ~0U) return All;
143 return RequiredArgs(value);
144 }
145 };
146
Daniel Dunbar7c086512008-09-09 23:14:03 +0000147 /// FunctionArgList - Type for representing both the decl and type
148 /// of parameters to a function. The decl must be either a
149 /// ParmVarDecl or ImplicitParamDecl.
Chris Lattner686775d2011-07-20 06:58:45 +0000150 class FunctionArgList : public SmallVector<const VarDecl*, 16> {
John McCalld26bc762011-03-09 04:27:21 +0000151 };
Mike Stump1eb44332009-09-09 15:08:12 +0000152
Daniel Dunbar0dbe2272008-09-08 21:33:45 +0000153 /// CGFunctionInfo - Class to encapsulate the information about a
154 /// function definition.
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000155 class CGFunctionInfo : public llvm::FoldingSetNode {
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000156 struct ArgInfo {
John McCallead608a2010-02-26 00:48:12 +0000157 CanQualType type;
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000158 ABIArgInfo info;
159 };
160
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000161 /// The LLVM::CallingConv to use for this function (as specified by the
162 /// user).
John McCallde5d3c72012-02-17 03:33:10 +0000163 unsigned CallingConvention : 8;
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000164
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000165 /// The LLVM::CallingConv to actually use for this function, which may
166 /// depend on the ABI.
John McCallde5d3c72012-02-17 03:33:10 +0000167 unsigned EffectiveCallingConvention : 8;
168
169 /// The clang::CallingConv that this was originally created with.
170 unsigned ASTCallingConvention : 8;
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000171
John McCall04a67a62010-02-05 21:31:56 +0000172 /// Whether this function is noreturn.
John McCallde5d3c72012-02-17 03:33:10 +0000173 unsigned NoReturn : 1;
John McCall04a67a62010-02-05 21:31:56 +0000174
John McCallf85e1932011-06-15 23:02:42 +0000175 /// Whether this function is returns-retained.
John McCallde5d3c72012-02-17 03:33:10 +0000176 unsigned ReturnsRetained : 1;
Daniel Dunbar0dbe2272008-09-08 21:33:45 +0000177
Rafael Espindola425ef722010-03-30 22:15:11 +0000178 /// How many arguments to pass inreg.
John McCallde5d3c72012-02-17 03:33:10 +0000179 unsigned HasRegParm : 1;
180 unsigned RegParm : 4;
181
182 RequiredArgs Required;
183
184 unsigned NumArgs;
185 ArgInfo *getArgsBuffer() {
186 return reinterpret_cast<ArgInfo*>(this+1);
187 }
188 const ArgInfo *getArgsBuffer() const {
189 return reinterpret_cast<const ArgInfo*>(this + 1);
190 }
191
192 CGFunctionInfo() : Required(RequiredArgs::All) {}
Rafael Espindola425ef722010-03-30 22:15:11 +0000193
Daniel Dunbar0dbe2272008-09-08 21:33:45 +0000194 public:
John McCallde5d3c72012-02-17 03:33:10 +0000195 static CGFunctionInfo *create(unsigned llvmCC,
196 const FunctionType::ExtInfo &extInfo,
197 CanQualType resultType,
198 ArrayRef<CanQualType> argTypes,
199 RequiredArgs required);
200
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000201 typedef const ArgInfo *const_arg_iterator;
202 typedef ArgInfo *arg_iterator;
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000203
John McCallde5d3c72012-02-17 03:33:10 +0000204 const_arg_iterator arg_begin() const { return getArgsBuffer() + 1; }
205 const_arg_iterator arg_end() const { return getArgsBuffer() + 1 + NumArgs; }
206 arg_iterator arg_begin() { return getArgsBuffer() + 1; }
207 arg_iterator arg_end() { return getArgsBuffer() + 1 + NumArgs; }
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000208
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +0000209 unsigned arg_size() const { return NumArgs; }
210
John McCallde5d3c72012-02-17 03:33:10 +0000211 bool isVariadic() const { return Required.allowsOptionalArgs(); }
212 RequiredArgs getRequiredArgs() const { return Required; }
213
John McCall04a67a62010-02-05 21:31:56 +0000214 bool isNoReturn() const { return NoReturn; }
215
John McCallde5d3c72012-02-17 03:33:10 +0000216 /// In ARC, whether this function retains its return value. This
John McCallf85e1932011-06-15 23:02:42 +0000217 /// is not always reliable for call sites.
218 bool isReturnsRetained() const { return ReturnsRetained; }
219
John McCallde5d3c72012-02-17 03:33:10 +0000220 /// getASTCallingConvention() - Return the AST-specified calling
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000221 /// convention.
John McCallde5d3c72012-02-17 03:33:10 +0000222 CallingConv getASTCallingConvention() const {
223 return CallingConv(ASTCallingConvention);
224 }
225
226 /// getCallingConvention - Return the user specified calling
227 /// convention, which has been translated into an LLVM CC.
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000228 unsigned getCallingConvention() const { return CallingConvention; }
229
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000230 /// getEffectiveCallingConvention - Return the actual calling convention to
231 /// use, which may depend on the ABI.
232 unsigned getEffectiveCallingConvention() const {
233 return EffectiveCallingConvention;
234 }
235 void setEffectiveCallingConvention(unsigned Value) {
236 EffectiveCallingConvention = Value;
237 }
238
Eli Friedmana49218e2011-04-09 08:18:08 +0000239 bool getHasRegParm() const { return HasRegParm; }
Rafael Espindola425ef722010-03-30 22:15:11 +0000240 unsigned getRegParm() const { return RegParm; }
241
John McCallde5d3c72012-02-17 03:33:10 +0000242 FunctionType::ExtInfo getExtInfo() const {
243 return FunctionType::ExtInfo(isNoReturn(),
244 getHasRegParm(), getRegParm(),
245 getASTCallingConvention(),
246 isReturnsRetained());
247 }
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000248
John McCallde5d3c72012-02-17 03:33:10 +0000249 CanQualType getReturnType() const { return getArgsBuffer()[0].type; }
250
251 ABIArgInfo &getReturnInfo() { return getArgsBuffer()[0].info; }
252 const ABIArgInfo &getReturnInfo() const { return getArgsBuffer()[0].info; }
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000253
254 void Profile(llvm::FoldingSetNodeID &ID) {
John McCallde5d3c72012-02-17 03:33:10 +0000255 ID.AddInteger(getASTCallingConvention());
John McCall04a67a62010-02-05 21:31:56 +0000256 ID.AddBoolean(NoReturn);
John McCallf85e1932011-06-15 23:02:42 +0000257 ID.AddBoolean(ReturnsRetained);
Eli Friedmana49218e2011-04-09 08:18:08 +0000258 ID.AddBoolean(HasRegParm);
Rafael Espindola425ef722010-03-30 22:15:11 +0000259 ID.AddInteger(RegParm);
John McCallde5d3c72012-02-17 03:33:10 +0000260 ID.AddInteger(Required.getOpaqueData());
Daniel Dunbar35e67d42009-02-05 00:00:23 +0000261 getReturnType().Profile(ID);
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000262 for (arg_iterator it = arg_begin(), ie = arg_end(); it != ie; ++it)
263 it->type.Profile(ID);
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000264 }
Mike Stump1eb44332009-09-09 15:08:12 +0000265 static void Profile(llvm::FoldingSetNodeID &ID,
John McCallde5d3c72012-02-17 03:33:10 +0000266 const FunctionType::ExtInfo &info,
267 RequiredArgs required,
268 CanQualType resultType,
269 ArrayRef<CanQualType> argTypes) {
270 ID.AddInteger(info.getCC());
271 ID.AddBoolean(info.getNoReturn());
272 ID.AddBoolean(info.getProducesResult());
273 ID.AddBoolean(info.getHasRegParm());
274 ID.AddInteger(info.getRegParm());
275 ID.AddInteger(required.getOpaqueData());
276 resultType.Profile(ID);
277 for (ArrayRef<CanQualType>::iterator
278 i = argTypes.begin(), e = argTypes.end(); i != e; ++i) {
279 i->Profile(ID);
John McCallead608a2010-02-26 00:48:12 +0000280 }
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000281 }
Daniel Dunbar0dbe2272008-09-08 21:33:45 +0000282 };
Anders Carlsson31777a22009-12-24 19:08:58 +0000283
Anders Carlssond2490a92009-12-24 20:40:36 +0000284 /// ReturnValueSlot - Contains the address where the return value of a
285 /// function can be stored, and whether the address is volatile or not.
Anders Carlsson31777a22009-12-24 19:08:58 +0000286 class ReturnValueSlot {
287 llvm::PointerIntPair<llvm::Value *, 1, bool> Value;
288
289 public:
290 ReturnValueSlot() {}
291 ReturnValueSlot(llvm::Value *Value, bool IsVolatile)
292 : Value(Value, IsVolatile) {}
293
294 bool isNull() const { return !getValue(); }
295
296 bool isVolatile() const { return Value.getInt(); }
297 llvm::Value *getValue() const { return Value.getPointer(); }
298 };
299
Daniel Dunbar0dbe2272008-09-08 21:33:45 +0000300} // end namespace CodeGen
301} // end namespace clang
302
303#endif