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.
62 /// The pointee type of the original argument.
65 /// The temporary alloca.
66 llvm::Value *Temporary;
69 void add(RValue rvalue, QualType type, bool needscopy = false) {
70 push_back(CallArg(rvalue, type, needscopy));
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());
79 void addWriteback(llvm::Value *address, QualType addressType,
80 llvm::Value *temporary) {
82 writeback.Address = address;
83 writeback.AddressType = addressType;
84 writeback.Temporary = temporary;
85 Writebacks.push_back(writeback);
88 bool hasWritebacks() const { return !Writebacks.empty(); }
90 typedef SmallVectorImpl<Writeback>::const_iterator writeback_iterator;
91 writeback_iterator writeback_begin() const { return Writebacks.begin(); }
92 writeback_iterator writeback_end() const { return Writebacks.end(); }
95 SmallVector<Writeback, 1> Writebacks;
98 /// A class for recording the number of arguments that a function
99 /// signature requires.
101 /// The number of required arguments, or ~0 if the signature does
102 /// not permit optional arguments.
103 unsigned NumRequired;
107 RequiredArgs(All_t _) : NumRequired(~0U) {}
108 explicit RequiredArgs(unsigned n) : NumRequired(n) {
112 /// Compute the arguments required by the given formal prototype,
113 /// given that there may be some additional, non-formal arguments
115 static RequiredArgs forPrototypePlus(const FunctionProtoType *prototype,
116 unsigned additional) {
117 if (!prototype->isVariadic()) return All;
118 return RequiredArgs(prototype->getNumArgs() + additional);
121 static RequiredArgs forPrototype(const FunctionProtoType *prototype) {
122 return forPrototypePlus(prototype, 0);
125 static RequiredArgs forPrototype(CanQual<FunctionProtoType> prototype) {
126 return forPrototype(prototype.getTypePtr());
129 static RequiredArgs forPrototypePlus(CanQual<FunctionProtoType> prototype,
130 unsigned additional) {
131 return forPrototypePlus(prototype.getTypePtr(), additional);
134 bool allowsOptionalArgs() const { return NumRequired != ~0U; }
135 unsigned getNumRequiredArgs() const {
136 assert(allowsOptionalArgs());
140 unsigned getOpaqueData() const { return NumRequired; }
141 static RequiredArgs getFromOpaqueData(unsigned value) {
142 if (value == ~0U) return All;
143 return RequiredArgs(value);
147 /// 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.
150 class FunctionArgList : public SmallVector<const VarDecl*, 16> {
153 /// CGFunctionInfo - Class to encapsulate the information about a
154 /// function definition.
155 class CGFunctionInfo : public llvm::FoldingSetNode {
161 /// The LLVM::CallingConv to use for this function (as specified by the
163 unsigned CallingConvention : 8;
165 /// The LLVM::CallingConv to actually use for this function, which may
166 /// depend on the ABI.
167 unsigned EffectiveCallingConvention : 8;
169 /// The clang::CallingConv that this was originally created with.
170 unsigned ASTCallingConvention : 8;
172 /// Whether this function is noreturn.
173 unsigned NoReturn : 1;
175 /// Whether this function is returns-retained.
176 unsigned ReturnsRetained : 1;
178 /// How many arguments to pass inreg.
179 unsigned HasRegParm : 1;
180 unsigned RegParm : 4;
182 RequiredArgs Required;
185 ArgInfo *getArgsBuffer() {
186 return reinterpret_cast<ArgInfo*>(this+1);
188 const ArgInfo *getArgsBuffer() const {
189 return reinterpret_cast<const ArgInfo*>(this + 1);
192 CGFunctionInfo() : Required(RequiredArgs::All) {}
195 static CGFunctionInfo *create(unsigned llvmCC,
196 const FunctionType::ExtInfo &extInfo,
197 CanQualType resultType,
198 ArrayRef<CanQualType> argTypes,
199 RequiredArgs required);
201 typedef const ArgInfo *const_arg_iterator;
202 typedef ArgInfo *arg_iterator;
204 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; }
209 unsigned arg_size() const { return NumArgs; }
211 bool isVariadic() const { return Required.allowsOptionalArgs(); }
212 RequiredArgs getRequiredArgs() const { return Required; }
214 bool isNoReturn() const { return NoReturn; }
216 /// In ARC, whether this function retains its return value. This
217 /// is not always reliable for call sites.
218 bool isReturnsRetained() const { return ReturnsRetained; }
220 /// getASTCallingConvention() - Return the AST-specified calling
222 CallingConv getASTCallingConvention() const {
223 return CallingConv(ASTCallingConvention);
226 /// getCallingConvention - Return the user specified calling
227 /// convention, which has been translated into an LLVM CC.
228 unsigned getCallingConvention() const { return CallingConvention; }
230 /// getEffectiveCallingConvention - Return the actual calling convention to
231 /// use, which may depend on the ABI.
232 unsigned getEffectiveCallingConvention() const {
233 return EffectiveCallingConvention;
235 void setEffectiveCallingConvention(unsigned Value) {
236 EffectiveCallingConvention = Value;
239 bool getHasRegParm() const { return HasRegParm; }
240 unsigned getRegParm() const { return RegParm; }
242 FunctionType::ExtInfo getExtInfo() const {
243 return FunctionType::ExtInfo(isNoReturn(),
244 getHasRegParm(), getRegParm(),
245 getASTCallingConvention(),
246 isReturnsRetained());
249 CanQualType getReturnType() const { return getArgsBuffer()[0].type; }
251 ABIArgInfo &getReturnInfo() { return getArgsBuffer()[0].info; }
252 const ABIArgInfo &getReturnInfo() const { return getArgsBuffer()[0].info; }
254 void Profile(llvm::FoldingSetNodeID &ID) {
255 ID.AddInteger(getASTCallingConvention());
256 ID.AddBoolean(NoReturn);
257 ID.AddBoolean(ReturnsRetained);
258 ID.AddBoolean(HasRegParm);
259 ID.AddInteger(RegParm);
260 ID.AddInteger(Required.getOpaqueData());
261 getReturnType().Profile(ID);
262 for (arg_iterator it = arg_begin(), ie = arg_end(); it != ie; ++it)
263 it->type.Profile(ID);
265 static void Profile(llvm::FoldingSetNodeID &ID,
266 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) {
284 /// ReturnValueSlot - Contains the address where the return value of a
285 /// function can be stored, and whether the address is volatile or not.
286 class ReturnValueSlot {
287 llvm::PointerIntPair<llvm::Value *, 1, bool> Value;
291 ReturnValueSlot(llvm::Value *Value, bool IsVolatile)
292 : Value(Value, IsVolatile) {}
294 bool isNull() const { return !getValue(); }
296 bool isVolatile() const { return Value.getInt(); }
297 llvm::Value *getValue() const { return Value.getPointer(); }
300 } // end namespace CodeGen
301 } // end namespace clang