blob: 55af4e5fbfa0ed3bb74fac3a911719d4d58bb254 [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;
32
33 template<typename T, unsigned> class SmallVector;
34}
35
36namespace clang {
37 class ASTContext;
38 class Decl;
39 class FunctionDecl;
40 class ObjCMethodDecl;
Daniel Dunbar7c086512008-09-09 23:14:03 +000041 class VarDecl;
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000042
43namespace CodeGen {
Chris Lattner686775d2011-07-20 06:58:45 +000044 typedef SmallVector<llvm::AttributeWithIndex, 8> AttributeListType;
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000045
Eli Friedmanc6d07822011-05-02 18:05:27 +000046 struct CallArg {
47 RValue RV;
48 QualType Ty;
Eli Friedman55d48482011-05-26 00:10:27 +000049 bool NeedsCopy;
50 CallArg(RValue rv, QualType ty, bool needscopy)
51 : RV(rv), Ty(ty), NeedsCopy(needscopy)
Eli Friedmanc6d07822011-05-02 18:05:27 +000052 { }
53 };
54
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000055 /// CallArgList - Type for representing both the value and type of
56 /// arguments in a call.
John McCalld26bc762011-03-09 04:27:21 +000057 class CallArgList :
Chris Lattner686775d2011-07-20 06:58:45 +000058 public SmallVector<CallArg, 16> {
John McCall413ebdb2011-03-11 20:59:21 +000059 public:
John McCallf85e1932011-06-15 23:02:42 +000060 struct Writeback {
61 /// The original argument.
62 llvm::Value *Address;
63
64 /// The pointee type of the original argument.
65 QualType AddressType;
66
67 /// The temporary alloca.
68 llvm::Value *Temporary;
69 };
70
Eli Friedman55d48482011-05-26 00:10:27 +000071 void add(RValue rvalue, QualType type, bool needscopy = false) {
72 push_back(CallArg(rvalue, type, needscopy));
John McCall413ebdb2011-03-11 20:59:21 +000073 }
John McCallf85e1932011-06-15 23:02:42 +000074
75 void addFrom(const CallArgList &other) {
76 insert(end(), other.begin(), other.end());
77 Writebacks.insert(Writebacks.end(),
78 other.Writebacks.begin(), other.Writebacks.end());
79 }
80
81 void addWriteback(llvm::Value *address, QualType addressType,
82 llvm::Value *temporary) {
83 Writeback writeback;
84 writeback.Address = address;
85 writeback.AddressType = addressType;
86 writeback.Temporary = temporary;
87 Writebacks.push_back(writeback);
88 }
89
90 bool hasWritebacks() const { return !Writebacks.empty(); }
91
Chris Lattner686775d2011-07-20 06:58:45 +000092 typedef SmallVectorImpl<Writeback>::const_iterator writeback_iterator;
John McCallf85e1932011-06-15 23:02:42 +000093 writeback_iterator writeback_begin() const { return Writebacks.begin(); }
94 writeback_iterator writeback_end() const { return Writebacks.end(); }
95
96 private:
Chris Lattner686775d2011-07-20 06:58:45 +000097 SmallVector<Writeback, 1> Writebacks;
John McCalld26bc762011-03-09 04:27:21 +000098 };
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000099
John McCallde5d3c72012-02-17 03:33:10 +0000100 /// A class for recording the number of arguments that a function
101 /// signature requires.
102 class RequiredArgs {
103 /// The number of required arguments, or ~0 if the signature does
104 /// not permit optional arguments.
105 unsigned NumRequired;
106 public:
107 enum All_t { All };
108
109 RequiredArgs(All_t _) : NumRequired(~0U) {}
110 explicit RequiredArgs(unsigned n) : NumRequired(n) {
111 assert(n != ~0U);
112 }
113
114 /// Compute the arguments required by the given formal prototype,
115 /// given that there may be some additional, non-formal arguments
116 /// in play.
117 static RequiredArgs forPrototypePlus(const FunctionProtoType *prototype,
118 unsigned additional) {
119 if (!prototype->isVariadic()) return All;
120 return RequiredArgs(prototype->getNumArgs() + additional);
121 }
122
123 static RequiredArgs forPrototype(const FunctionProtoType *prototype) {
124 return forPrototypePlus(prototype, 0);
125 }
126
127 static RequiredArgs forPrototype(CanQual<FunctionProtoType> prototype) {
128 return forPrototype(prototype.getTypePtr());
129 }
130
131 static RequiredArgs forPrototypePlus(CanQual<FunctionProtoType> prototype,
132 unsigned additional) {
133 return forPrototypePlus(prototype.getTypePtr(), additional);
134 }
135
136 bool allowsOptionalArgs() const { return NumRequired != ~0U; }
John McCalle56bb362012-12-07 07:03:17 +0000137 unsigned getNumRequiredArgs() const {
John McCallde5d3c72012-02-17 03:33:10 +0000138 assert(allowsOptionalArgs());
139 return NumRequired;
140 }
141
142 unsigned getOpaqueData() const { return NumRequired; }
143 static RequiredArgs getFromOpaqueData(unsigned value) {
144 if (value == ~0U) return All;
145 return RequiredArgs(value);
146 }
147 };
148
Daniel Dunbar7c086512008-09-09 23:14:03 +0000149 /// FunctionArgList - Type for representing both the decl and type
150 /// of parameters to a function. The decl must be either a
151 /// ParmVarDecl or ImplicitParamDecl.
Chris Lattner686775d2011-07-20 06:58:45 +0000152 class FunctionArgList : public SmallVector<const VarDecl*, 16> {
John McCalld26bc762011-03-09 04:27:21 +0000153 };
Mike Stump1eb44332009-09-09 15:08:12 +0000154
Daniel Dunbar0dbe2272008-09-08 21:33:45 +0000155 /// CGFunctionInfo - Class to encapsulate the information about a
156 /// function definition.
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000157 class CGFunctionInfo : public llvm::FoldingSetNode {
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000158 struct ArgInfo {
John McCallead608a2010-02-26 00:48:12 +0000159 CanQualType type;
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000160 ABIArgInfo info;
161 };
162
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000163 /// The LLVM::CallingConv to use for this function (as specified by the
164 /// user).
John McCallde5d3c72012-02-17 03:33:10 +0000165 unsigned CallingConvention : 8;
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000166
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000167 /// The LLVM::CallingConv to actually use for this function, which may
168 /// depend on the ABI.
John McCallde5d3c72012-02-17 03:33:10 +0000169 unsigned EffectiveCallingConvention : 8;
170
171 /// The clang::CallingConv that this was originally created with.
172 unsigned ASTCallingConvention : 8;
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000173
John McCall04a67a62010-02-05 21:31:56 +0000174 /// Whether this function is noreturn.
John McCallde5d3c72012-02-17 03:33:10 +0000175 unsigned NoReturn : 1;
John McCall04a67a62010-02-05 21:31:56 +0000176
John McCallf85e1932011-06-15 23:02:42 +0000177 /// Whether this function is returns-retained.
John McCallde5d3c72012-02-17 03:33:10 +0000178 unsigned ReturnsRetained : 1;
Daniel Dunbar0dbe2272008-09-08 21:33:45 +0000179
Rafael Espindola425ef722010-03-30 22:15:11 +0000180 /// How many arguments to pass inreg.
John McCallde5d3c72012-02-17 03:33:10 +0000181 unsigned HasRegParm : 1;
182 unsigned RegParm : 4;
183
184 RequiredArgs Required;
185
186 unsigned NumArgs;
187 ArgInfo *getArgsBuffer() {
188 return reinterpret_cast<ArgInfo*>(this+1);
189 }
190 const ArgInfo *getArgsBuffer() const {
191 return reinterpret_cast<const ArgInfo*>(this + 1);
192 }
193
194 CGFunctionInfo() : Required(RequiredArgs::All) {}
Rafael Espindola425ef722010-03-30 22:15:11 +0000195
Daniel Dunbar0dbe2272008-09-08 21:33:45 +0000196 public:
John McCallde5d3c72012-02-17 03:33:10 +0000197 static CGFunctionInfo *create(unsigned llvmCC,
198 const FunctionType::ExtInfo &extInfo,
199 CanQualType resultType,
200 ArrayRef<CanQualType> argTypes,
201 RequiredArgs required);
202
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000203 typedef const ArgInfo *const_arg_iterator;
204 typedef ArgInfo *arg_iterator;
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000205
John McCallde5d3c72012-02-17 03:33:10 +0000206 const_arg_iterator arg_begin() const { return getArgsBuffer() + 1; }
207 const_arg_iterator arg_end() const { return getArgsBuffer() + 1 + NumArgs; }
208 arg_iterator arg_begin() { return getArgsBuffer() + 1; }
209 arg_iterator arg_end() { return getArgsBuffer() + 1 + NumArgs; }
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000210
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +0000211 unsigned arg_size() const { return NumArgs; }
212
John McCallde5d3c72012-02-17 03:33:10 +0000213 bool isVariadic() const { return Required.allowsOptionalArgs(); }
214 RequiredArgs getRequiredArgs() const { return Required; }
215
John McCall04a67a62010-02-05 21:31:56 +0000216 bool isNoReturn() const { return NoReturn; }
217
John McCallde5d3c72012-02-17 03:33:10 +0000218 /// In ARC, whether this function retains its return value. This
John McCallf85e1932011-06-15 23:02:42 +0000219 /// is not always reliable for call sites.
220 bool isReturnsRetained() const { return ReturnsRetained; }
221
John McCallde5d3c72012-02-17 03:33:10 +0000222 /// getASTCallingConvention() - Return the AST-specified calling
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000223 /// convention.
John McCallde5d3c72012-02-17 03:33:10 +0000224 CallingConv getASTCallingConvention() const {
225 return CallingConv(ASTCallingConvention);
226 }
227
228 /// getCallingConvention - Return the user specified calling
229 /// convention, which has been translated into an LLVM CC.
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000230 unsigned getCallingConvention() const { return CallingConvention; }
231
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000232 /// getEffectiveCallingConvention - Return the actual calling convention to
233 /// use, which may depend on the ABI.
234 unsigned getEffectiveCallingConvention() const {
235 return EffectiveCallingConvention;
236 }
237 void setEffectiveCallingConvention(unsigned Value) {
238 EffectiveCallingConvention = Value;
239 }
240
Eli Friedmana49218e2011-04-09 08:18:08 +0000241 bool getHasRegParm() const { return HasRegParm; }
Rafael Espindola425ef722010-03-30 22:15:11 +0000242 unsigned getRegParm() const { return RegParm; }
243
John McCallde5d3c72012-02-17 03:33:10 +0000244 FunctionType::ExtInfo getExtInfo() const {
245 return FunctionType::ExtInfo(isNoReturn(),
246 getHasRegParm(), getRegParm(),
247 getASTCallingConvention(),
248 isReturnsRetained());
249 }
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000250
John McCallde5d3c72012-02-17 03:33:10 +0000251 CanQualType getReturnType() const { return getArgsBuffer()[0].type; }
252
253 ABIArgInfo &getReturnInfo() { return getArgsBuffer()[0].info; }
254 const ABIArgInfo &getReturnInfo() const { return getArgsBuffer()[0].info; }
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000255
256 void Profile(llvm::FoldingSetNodeID &ID) {
John McCallde5d3c72012-02-17 03:33:10 +0000257 ID.AddInteger(getASTCallingConvention());
John McCall04a67a62010-02-05 21:31:56 +0000258 ID.AddBoolean(NoReturn);
John McCallf85e1932011-06-15 23:02:42 +0000259 ID.AddBoolean(ReturnsRetained);
Eli Friedmana49218e2011-04-09 08:18:08 +0000260 ID.AddBoolean(HasRegParm);
Rafael Espindola425ef722010-03-30 22:15:11 +0000261 ID.AddInteger(RegParm);
John McCallde5d3c72012-02-17 03:33:10 +0000262 ID.AddInteger(Required.getOpaqueData());
Daniel Dunbar35e67d42009-02-05 00:00:23 +0000263 getReturnType().Profile(ID);
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000264 for (arg_iterator it = arg_begin(), ie = arg_end(); it != ie; ++it)
265 it->type.Profile(ID);
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000266 }
Mike Stump1eb44332009-09-09 15:08:12 +0000267 static void Profile(llvm::FoldingSetNodeID &ID,
John McCallde5d3c72012-02-17 03:33:10 +0000268 const FunctionType::ExtInfo &info,
269 RequiredArgs required,
270 CanQualType resultType,
271 ArrayRef<CanQualType> argTypes) {
272 ID.AddInteger(info.getCC());
273 ID.AddBoolean(info.getNoReturn());
274 ID.AddBoolean(info.getProducesResult());
275 ID.AddBoolean(info.getHasRegParm());
276 ID.AddInteger(info.getRegParm());
277 ID.AddInteger(required.getOpaqueData());
278 resultType.Profile(ID);
279 for (ArrayRef<CanQualType>::iterator
280 i = argTypes.begin(), e = argTypes.end(); i != e; ++i) {
281 i->Profile(ID);
John McCallead608a2010-02-26 00:48:12 +0000282 }
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000283 }
Daniel Dunbar0dbe2272008-09-08 21:33:45 +0000284 };
Anders Carlsson31777a22009-12-24 19:08:58 +0000285
Anders Carlssond2490a92009-12-24 20:40:36 +0000286 /// ReturnValueSlot - Contains the address where the return value of a
287 /// function can be stored, and whether the address is volatile or not.
Anders Carlsson31777a22009-12-24 19:08:58 +0000288 class ReturnValueSlot {
289 llvm::PointerIntPair<llvm::Value *, 1, bool> Value;
290
291 public:
292 ReturnValueSlot() {}
293 ReturnValueSlot(llvm::Value *Value, bool IsVolatile)
294 : Value(Value, IsVolatile) {}
295
296 bool isNull() const { return !getValue(); }
297
298 bool isVolatile() const { return Value.getInt(); }
299 llvm::Value *getValue() const { return Value.getPointer(); }
300 };
301
Daniel Dunbar0dbe2272008-09-08 21:33:45 +0000302} // end namespace CodeGen
303} // end namespace clang
304
305#endif