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/IR/Value.h"
24 // FIXME: Restructure so we don't have to expose so much stuff.
42 typedef SmallVector<llvm::AttributeSet, 8> AttributeListType;
48 CallArg(RValue rv, QualType ty, bool needscopy)
49 : RV(rv), Ty(ty), NeedsCopy(needscopy)
53 /// CallArgList - Type for representing both the value and type of
54 /// arguments in a call.
56 public SmallVector<CallArg, 16> {
59 /// The original argument. Note that the argument l-value
60 /// is potentially null.
63 /// The temporary alloca.
64 llvm::Value *Temporary;
66 /// A value to "use" after the writeback, or null.
70 void add(RValue rvalue, QualType type, bool needscopy = false) {
71 push_back(CallArg(rvalue, type, needscopy));
74 void addFrom(const CallArgList &other) {
75 insert(end(), other.begin(), other.end());
76 Writebacks.insert(Writebacks.end(),
77 other.Writebacks.begin(), other.Writebacks.end());
80 void addWriteback(LValue srcLV, llvm::Value *temporary,
83 writeback.Source = srcLV;
84 writeback.Temporary = temporary;
85 writeback.ToUse = toUse;
86 Writebacks.push_back(writeback);
89 bool hasWritebacks() const { return !Writebacks.empty(); }
91 typedef SmallVectorImpl<Writeback>::const_iterator writeback_iterator;
92 writeback_iterator writeback_begin() const { return Writebacks.begin(); }
93 writeback_iterator writeback_end() const { return Writebacks.end(); }
96 SmallVector<Writeback, 1> Writebacks;
99 /// A class for recording the number of arguments that a function
100 /// signature requires.
102 /// The number of required arguments, or ~0 if the signature does
103 /// not permit optional arguments.
104 unsigned NumRequired;
108 RequiredArgs(All_t _) : NumRequired(~0U) {}
109 explicit RequiredArgs(unsigned n) : NumRequired(n) {
113 /// Compute the arguments required by the given formal prototype,
114 /// given that there may be some additional, non-formal arguments
116 static RequiredArgs forPrototypePlus(const FunctionProtoType *prototype,
117 unsigned additional) {
118 if (!prototype->isVariadic()) return All;
119 return RequiredArgs(prototype->getNumArgs() + additional);
122 static RequiredArgs forPrototype(const FunctionProtoType *prototype) {
123 return forPrototypePlus(prototype, 0);
126 static RequiredArgs forPrototype(CanQual<FunctionProtoType> prototype) {
127 return forPrototype(prototype.getTypePtr());
130 static RequiredArgs forPrototypePlus(CanQual<FunctionProtoType> prototype,
131 unsigned additional) {
132 return forPrototypePlus(prototype.getTypePtr(), additional);
135 bool allowsOptionalArgs() const { return NumRequired != ~0U; }
136 unsigned getNumRequiredArgs() const {
137 assert(allowsOptionalArgs());
141 unsigned getOpaqueData() const { return NumRequired; }
142 static RequiredArgs getFromOpaqueData(unsigned value) {
143 if (value == ~0U) return All;
144 return RequiredArgs(value);
148 /// FunctionArgList - Type for representing both the decl and type
149 /// of parameters to a function. The decl must be either a
150 /// ParmVarDecl or ImplicitParamDecl.
151 class FunctionArgList : public SmallVector<const VarDecl*, 16> {
154 /// CGFunctionInfo - Class to encapsulate the information about a
155 /// function definition.
156 class CGFunctionInfo : public llvm::FoldingSetNode {
162 /// The LLVM::CallingConv to use for this function (as specified by the
164 unsigned CallingConvention : 8;
166 /// The LLVM::CallingConv to actually use for this function, which may
167 /// depend on the ABI.
168 unsigned EffectiveCallingConvention : 8;
170 /// The clang::CallingConv that this was originally created with.
171 unsigned ASTCallingConvention : 8;
173 /// Whether this function is noreturn.
174 unsigned NoReturn : 1;
176 /// Whether this function is returns-retained.
177 unsigned ReturnsRetained : 1;
179 /// How many arguments to pass inreg.
180 unsigned HasRegParm : 1;
181 unsigned RegParm : 4;
183 RequiredArgs Required;
186 ArgInfo *getArgsBuffer() {
187 return reinterpret_cast<ArgInfo*>(this+1);
189 const ArgInfo *getArgsBuffer() const {
190 return reinterpret_cast<const ArgInfo*>(this + 1);
193 CGFunctionInfo() : Required(RequiredArgs::All) {}
196 static CGFunctionInfo *create(unsigned llvmCC,
197 const FunctionType::ExtInfo &extInfo,
198 CanQualType resultType,
199 ArrayRef<CanQualType> argTypes,
200 RequiredArgs required);
202 typedef const ArgInfo *const_arg_iterator;
203 typedef ArgInfo *arg_iterator;
205 const_arg_iterator arg_begin() const { return getArgsBuffer() + 1; }
206 const_arg_iterator arg_end() const { return getArgsBuffer() + 1 + NumArgs; }
207 arg_iterator arg_begin() { return getArgsBuffer() + 1; }
208 arg_iterator arg_end() { return getArgsBuffer() + 1 + NumArgs; }
210 unsigned arg_size() const { return NumArgs; }
212 bool isVariadic() const { return Required.allowsOptionalArgs(); }
213 RequiredArgs getRequiredArgs() const { return Required; }
215 bool isNoReturn() const { return NoReturn; }
217 /// In ARC, whether this function retains its return value. This
218 /// is not always reliable for call sites.
219 bool isReturnsRetained() const { return ReturnsRetained; }
221 /// getASTCallingConvention() - Return the AST-specified calling
223 CallingConv getASTCallingConvention() const {
224 return CallingConv(ASTCallingConvention);
227 /// getCallingConvention - Return the user specified calling
228 /// convention, which has been translated into an LLVM CC.
229 unsigned getCallingConvention() const { return CallingConvention; }
231 /// getEffectiveCallingConvention - Return the actual calling convention to
232 /// use, which may depend on the ABI.
233 unsigned getEffectiveCallingConvention() const {
234 return EffectiveCallingConvention;
236 void setEffectiveCallingConvention(unsigned Value) {
237 EffectiveCallingConvention = Value;
240 bool getHasRegParm() const { return HasRegParm; }
241 unsigned getRegParm() const { return RegParm; }
243 FunctionType::ExtInfo getExtInfo() const {
244 return FunctionType::ExtInfo(isNoReturn(),
245 getHasRegParm(), getRegParm(),
246 getASTCallingConvention(),
247 isReturnsRetained());
250 CanQualType getReturnType() const { return getArgsBuffer()[0].type; }
252 ABIArgInfo &getReturnInfo() { return getArgsBuffer()[0].info; }
253 const ABIArgInfo &getReturnInfo() const { return getArgsBuffer()[0].info; }
255 void Profile(llvm::FoldingSetNodeID &ID) {
256 ID.AddInteger(getASTCallingConvention());
257 ID.AddBoolean(NoReturn);
258 ID.AddBoolean(ReturnsRetained);
259 ID.AddBoolean(HasRegParm);
260 ID.AddInteger(RegParm);
261 ID.AddInteger(Required.getOpaqueData());
262 getReturnType().Profile(ID);
263 for (arg_iterator it = arg_begin(), ie = arg_end(); it != ie; ++it)
264 it->type.Profile(ID);
266 static void Profile(llvm::FoldingSetNodeID &ID,
267 const FunctionType::ExtInfo &info,
268 RequiredArgs required,
269 CanQualType resultType,
270 ArrayRef<CanQualType> argTypes) {
271 ID.AddInteger(info.getCC());
272 ID.AddBoolean(info.getNoReturn());
273 ID.AddBoolean(info.getProducesResult());
274 ID.AddBoolean(info.getHasRegParm());
275 ID.AddInteger(info.getRegParm());
276 ID.AddInteger(required.getOpaqueData());
277 resultType.Profile(ID);
278 for (ArrayRef<CanQualType>::iterator
279 i = argTypes.begin(), e = argTypes.end(); i != e; ++i) {
285 /// ReturnValueSlot - Contains the address where the return value of a
286 /// function can be stored, and whether the address is volatile or not.
287 class ReturnValueSlot {
288 llvm::PointerIntPair<llvm::Value *, 1, bool> Value;
292 ReturnValueSlot(llvm::Value *Value, bool IsVolatile)
293 : Value(Value, IsVolatile) {}
295 bool isNull() const { return !getValue(); }
297 bool isVolatile() const { return Value.getInt(); }
298 llvm::Value *getValue() const { return Value.getPointer(); }
301 } // end namespace CodeGen
302 } // end namespace clang