1 //===----- CGCall.h - Encapsulate calling convention details ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // These classes wrap the information about a call or function
11 // definition used to handle ABI compliancy.
13 //===----------------------------------------------------------------------===//
15 #ifndef CLANG_CODEGEN_CGCALL_H
16 #define CLANG_CODEGEN_CGCALL_H
18 #include "llvm/ADT/FoldingSet.h"
19 #include "llvm/Value.h"
20 #include "clang/AST/Type.h"
21 #include "clang/AST/CanonicalType.h"
25 // FIXME: Restructure so we don't have to expose so much stuff.
29 struct AttributeWithIndex;
34 template<typename T, unsigned> class SmallVector;
45 typedef SmallVector<llvm::AttributeWithIndex, 8> AttributeListType;
51 CallArg(RValue rv, QualType ty, bool needscopy)
52 : RV(rv), Ty(ty), NeedsCopy(needscopy)
56 /// CallArgList - Type for representing both the value and type of
57 /// arguments in a call.
59 public SmallVector<CallArg, 16> {
62 /// The original argument.
65 /// The pointee type of the original argument.
68 /// The temporary alloca.
69 llvm::Value *Temporary;
72 void add(RValue rvalue, QualType type, bool needscopy = false) {
73 push_back(CallArg(rvalue, type, needscopy));
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());
82 void addWriteback(llvm::Value *address, QualType addressType,
83 llvm::Value *temporary) {
85 writeback.Address = address;
86 writeback.AddressType = addressType;
87 writeback.Temporary = temporary;
88 Writebacks.push_back(writeback);
91 bool hasWritebacks() const { return !Writebacks.empty(); }
93 typedef SmallVectorImpl<Writeback>::const_iterator writeback_iterator;
94 writeback_iterator writeback_begin() const { return Writebacks.begin(); }
95 writeback_iterator writeback_end() const { return Writebacks.end(); }
98 SmallVector<Writeback, 1> Writebacks;
101 /// A class for recording the number of arguments that a function
102 /// signature requires.
104 /// The number of required arguments, or ~0 if the signature does
105 /// not permit optional arguments.
106 unsigned NumRequired;
110 RequiredArgs(All_t _) : NumRequired(~0U) {}
111 explicit RequiredArgs(unsigned n) : NumRequired(n) {
115 /// Compute the arguments required by the given formal prototype,
116 /// given that there may be some additional, non-formal arguments
118 static RequiredArgs forPrototypePlus(const FunctionProtoType *prototype,
119 unsigned additional) {
120 if (!prototype->isVariadic()) return All;
121 return RequiredArgs(prototype->getNumArgs() + additional);
124 static RequiredArgs forPrototype(const FunctionProtoType *prototype) {
125 return forPrototypePlus(prototype, 0);
128 static RequiredArgs forPrototype(CanQual<FunctionProtoType> prototype) {
129 return forPrototype(prototype.getTypePtr());
132 static RequiredArgs forPrototypePlus(CanQual<FunctionProtoType> prototype,
133 unsigned additional) {
134 return forPrototypePlus(prototype.getTypePtr(), additional);
137 bool allowsOptionalArgs() const { return NumRequired != ~0U; }
138 bool getNumRequiredArgs() const {
139 assert(allowsOptionalArgs());
143 unsigned getOpaqueData() const { return NumRequired; }
144 static RequiredArgs getFromOpaqueData(unsigned value) {
145 if (value == ~0U) return All;
146 return RequiredArgs(value);
150 /// 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.
153 class FunctionArgList : public SmallVector<const VarDecl*, 16> {
156 /// CGFunctionInfo - Class to encapsulate the information about a
157 /// function definition.
158 class CGFunctionInfo : public llvm::FoldingSetNode {
164 /// The LLVM::CallingConv to use for this function (as specified by the
166 unsigned CallingConvention : 8;
168 /// The LLVM::CallingConv to actually use for this function, which may
169 /// depend on the ABI.
170 unsigned EffectiveCallingConvention : 8;
172 /// The clang::CallingConv that this was originally created with.
173 unsigned ASTCallingConvention : 8;
175 /// Whether this function is noreturn.
176 unsigned NoReturn : 1;
178 /// Whether this function is returns-retained.
179 unsigned ReturnsRetained : 1;
181 /// How many arguments to pass inreg.
182 unsigned HasRegParm : 1;
183 unsigned RegParm : 4;
185 RequiredArgs Required;
188 ArgInfo *getArgsBuffer() {
189 return reinterpret_cast<ArgInfo*>(this+1);
191 const ArgInfo *getArgsBuffer() const {
192 return reinterpret_cast<const ArgInfo*>(this + 1);
195 CGFunctionInfo() : Required(RequiredArgs::All) {}
198 static CGFunctionInfo *create(unsigned llvmCC,
199 const FunctionType::ExtInfo &extInfo,
200 CanQualType resultType,
201 ArrayRef<CanQualType> argTypes,
202 RequiredArgs required);
204 typedef const ArgInfo *const_arg_iterator;
205 typedef ArgInfo *arg_iterator;
207 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; }
212 unsigned arg_size() const { return NumArgs; }
214 bool isVariadic() const { return Required.allowsOptionalArgs(); }
215 RequiredArgs getRequiredArgs() const { return Required; }
217 bool isNoReturn() const { return NoReturn; }
219 /// In ARC, whether this function retains its return value. This
220 /// is not always reliable for call sites.
221 bool isReturnsRetained() const { return ReturnsRetained; }
223 /// getASTCallingConvention() - Return the AST-specified calling
225 CallingConv getASTCallingConvention() const {
226 return CallingConv(ASTCallingConvention);
229 /// getCallingConvention - Return the user specified calling
230 /// convention, which has been translated into an LLVM CC.
231 unsigned getCallingConvention() const { return CallingConvention; }
233 /// getEffectiveCallingConvention - Return the actual calling convention to
234 /// use, which may depend on the ABI.
235 unsigned getEffectiveCallingConvention() const {
236 return EffectiveCallingConvention;
238 void setEffectiveCallingConvention(unsigned Value) {
239 EffectiveCallingConvention = Value;
242 bool getHasRegParm() const { return HasRegParm; }
243 unsigned getRegParm() const { return RegParm; }
245 FunctionType::ExtInfo getExtInfo() const {
246 return FunctionType::ExtInfo(isNoReturn(),
247 getHasRegParm(), getRegParm(),
248 getASTCallingConvention(),
249 isReturnsRetained());
252 CanQualType getReturnType() const { return getArgsBuffer()[0].type; }
254 ABIArgInfo &getReturnInfo() { return getArgsBuffer()[0].info; }
255 const ABIArgInfo &getReturnInfo() const { return getArgsBuffer()[0].info; }
257 void Profile(llvm::FoldingSetNodeID &ID) {
258 ID.AddInteger(getASTCallingConvention());
259 ID.AddBoolean(NoReturn);
260 ID.AddBoolean(ReturnsRetained);
261 ID.AddBoolean(HasRegParm);
262 ID.AddInteger(RegParm);
263 ID.AddInteger(Required.getOpaqueData());
264 getReturnType().Profile(ID);
265 for (arg_iterator it = arg_begin(), ie = arg_end(); it != ie; ++it)
266 it->type.Profile(ID);
268 static void Profile(llvm::FoldingSetNodeID &ID,
269 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) {
287 /// ReturnValueSlot - Contains the address where the return value of a
288 /// function can be stored, and whether the address is volatile or not.
289 class ReturnValueSlot {
290 llvm::PointerIntPair<llvm::Value *, 1, bool> Value;
294 ReturnValueSlot(llvm::Value *Value, bool IsVolatile)
295 : Value(Value, IsVolatile) {}
297 bool isNull() const { return !getValue(); }
299 bool isVolatile() const { return Value.getInt(); }
300 llvm::Value *getValue() const { return Value.getPointer(); }
303 } // end namespace CodeGen
304 } // end namespace clang