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
19 #include "clang/AST/CanonicalType.h"
20 #include "clang/AST/Type.h"
21 #include "llvm/ADT/FoldingSet.h"
22 #include "llvm/Value.h"
24 // FIXME: Restructure so we don't have to expose so much stuff.
28 struct AttributeWithIndex;
33 template<typename T, unsigned> class SmallVector;
44 typedef SmallVector<llvm::AttributeWithIndex, 8> AttributeListType;
50 CallArg(RValue rv, QualType ty, bool needscopy)
51 : RV(rv), Ty(ty), NeedsCopy(needscopy)
55 /// CallArgList - Type for representing both the value and type of
56 /// arguments in a call.
58 public SmallVector<CallArg, 16> {
61 /// The original argument.
64 /// The pointee type of the original argument.
67 /// The temporary alloca.
68 llvm::Value *Temporary;
71 void add(RValue rvalue, QualType type, bool needscopy = false) {
72 push_back(CallArg(rvalue, type, needscopy));
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());
81 void addWriteback(llvm::Value *address, QualType addressType,
82 llvm::Value *temporary) {
84 writeback.Address = address;
85 writeback.AddressType = addressType;
86 writeback.Temporary = temporary;
87 Writebacks.push_back(writeback);
90 bool hasWritebacks() const { return !Writebacks.empty(); }
92 typedef SmallVectorImpl<Writeback>::const_iterator writeback_iterator;
93 writeback_iterator writeback_begin() const { return Writebacks.begin(); }
94 writeback_iterator writeback_end() const { return Writebacks.end(); }
97 SmallVector<Writeback, 1> Writebacks;
100 /// A class for recording the number of arguments that a function
101 /// signature requires.
103 /// The number of required arguments, or ~0 if the signature does
104 /// not permit optional arguments.
105 unsigned NumRequired;
109 RequiredArgs(All_t _) : NumRequired(~0U) {}
110 explicit RequiredArgs(unsigned n) : NumRequired(n) {
114 /// Compute the arguments required by the given formal prototype,
115 /// given that there may be some additional, non-formal arguments
117 static RequiredArgs forPrototypePlus(const FunctionProtoType *prototype,
118 unsigned additional) {
119 if (!prototype->isVariadic()) return All;
120 return RequiredArgs(prototype->getNumArgs() + additional);
123 static RequiredArgs forPrototype(const FunctionProtoType *prototype) {
124 return forPrototypePlus(prototype, 0);
127 static RequiredArgs forPrototype(CanQual<FunctionProtoType> prototype) {
128 return forPrototype(prototype.getTypePtr());
131 static RequiredArgs forPrototypePlus(CanQual<FunctionProtoType> prototype,
132 unsigned additional) {
133 return forPrototypePlus(prototype.getTypePtr(), additional);
136 bool allowsOptionalArgs() const { return NumRequired != ~0U; }
137 bool getNumRequiredArgs() const {
138 assert(allowsOptionalArgs());
142 unsigned getOpaqueData() const { return NumRequired; }
143 static RequiredArgs getFromOpaqueData(unsigned value) {
144 if (value == ~0U) return All;
145 return RequiredArgs(value);
149 /// 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.
152 class FunctionArgList : public SmallVector<const VarDecl*, 16> {
155 /// CGFunctionInfo - Class to encapsulate the information about a
156 /// function definition.
157 class CGFunctionInfo : public llvm::FoldingSetNode {
163 /// The LLVM::CallingConv to use for this function (as specified by the
165 unsigned CallingConvention : 8;
167 /// The LLVM::CallingConv to actually use for this function, which may
168 /// depend on the ABI.
169 unsigned EffectiveCallingConvention : 8;
171 /// The clang::CallingConv that this was originally created with.
172 unsigned ASTCallingConvention : 8;
174 /// Whether this function is noreturn.
175 unsigned NoReturn : 1;
177 /// Whether this function is returns-retained.
178 unsigned ReturnsRetained : 1;
180 /// How many arguments to pass inreg.
181 unsigned HasRegParm : 1;
182 unsigned RegParm : 4;
184 RequiredArgs Required;
187 ArgInfo *getArgsBuffer() {
188 return reinterpret_cast<ArgInfo*>(this+1);
190 const ArgInfo *getArgsBuffer() const {
191 return reinterpret_cast<const ArgInfo*>(this + 1);
194 CGFunctionInfo() : Required(RequiredArgs::All) {}
197 static CGFunctionInfo *create(unsigned llvmCC,
198 const FunctionType::ExtInfo &extInfo,
199 CanQualType resultType,
200 ArrayRef<CanQualType> argTypes,
201 RequiredArgs required);
203 typedef const ArgInfo *const_arg_iterator;
204 typedef ArgInfo *arg_iterator;
206 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; }
211 unsigned arg_size() const { return NumArgs; }
213 bool isVariadic() const { return Required.allowsOptionalArgs(); }
214 RequiredArgs getRequiredArgs() const { return Required; }
216 bool isNoReturn() const { return NoReturn; }
218 /// In ARC, whether this function retains its return value. This
219 /// is not always reliable for call sites.
220 bool isReturnsRetained() const { return ReturnsRetained; }
222 /// getASTCallingConvention() - Return the AST-specified calling
224 CallingConv getASTCallingConvention() const {
225 return CallingConv(ASTCallingConvention);
228 /// getCallingConvention - Return the user specified calling
229 /// convention, which has been translated into an LLVM CC.
230 unsigned getCallingConvention() const { return CallingConvention; }
232 /// getEffectiveCallingConvention - Return the actual calling convention to
233 /// use, which may depend on the ABI.
234 unsigned getEffectiveCallingConvention() const {
235 return EffectiveCallingConvention;
237 void setEffectiveCallingConvention(unsigned Value) {
238 EffectiveCallingConvention = Value;
241 bool getHasRegParm() const { return HasRegParm; }
242 unsigned getRegParm() const { return RegParm; }
244 FunctionType::ExtInfo getExtInfo() const {
245 return FunctionType::ExtInfo(isNoReturn(),
246 getHasRegParm(), getRegParm(),
247 getASTCallingConvention(),
248 isReturnsRetained());
251 CanQualType getReturnType() const { return getArgsBuffer()[0].type; }
253 ABIArgInfo &getReturnInfo() { return getArgsBuffer()[0].info; }
254 const ABIArgInfo &getReturnInfo() const { return getArgsBuffer()[0].info; }
256 void Profile(llvm::FoldingSetNodeID &ID) {
257 ID.AddInteger(getASTCallingConvention());
258 ID.AddBoolean(NoReturn);
259 ID.AddBoolean(ReturnsRetained);
260 ID.AddBoolean(HasRegParm);
261 ID.AddInteger(RegParm);
262 ID.AddInteger(Required.getOpaqueData());
263 getReturnType().Profile(ID);
264 for (arg_iterator it = arg_begin(), ie = arg_end(); it != ie; ++it)
265 it->type.Profile(ID);
267 static void Profile(llvm::FoldingSetNodeID &ID,
268 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) {
286 /// ReturnValueSlot - Contains the address where the return value of a
287 /// function can be stored, and whether the address is volatile or not.
288 class ReturnValueSlot {
289 llvm::PointerIntPair<llvm::Value *, 1, bool> Value;
293 ReturnValueSlot(llvm::Value *Value, bool IsVolatile)
294 : Value(Value, IsVolatile) {}
296 bool isNull() const { return !getValue(); }
298 bool isVolatile() const { return Value.getInt(); }
299 llvm::Value *getValue() const { return Value.getPointer(); }
302 } // end namespace CodeGen
303 } // end namespace clang