Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 1 | //===----- 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 Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 18 | #include "CGValue.h" |
| 19 | #include "clang/AST/CanonicalType.h" |
| 20 | #include "clang/AST/Type.h" |
Anders Carlsson | 31777a2 | 2009-12-24 19:08:58 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/FoldingSet.h" |
Chandler Carruth | 3b844ba | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 22 | #include "llvm/IR/Value.h" |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 23 | |
Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 24 | // FIXME: Restructure so we don't have to expose so much stuff. |
| 25 | #include "ABIInfo.h" |
| 26 | |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 27 | namespace llvm { |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 28 | struct AttributeWithIndex; |
Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 29 | class Function; |
| 30 | class Type; |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 31 | class Value; |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | namespace clang { |
| 35 | class ASTContext; |
| 36 | class Decl; |
| 37 | class FunctionDecl; |
| 38 | class ObjCMethodDecl; |
Daniel Dunbar | 7c08651 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 39 | class VarDecl; |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 40 | |
| 41 | namespace CodeGen { |
Chris Lattner | 686775d | 2011-07-20 06:58:45 +0000 | [diff] [blame] | 42 | typedef SmallVector<llvm::AttributeWithIndex, 8> AttributeListType; |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 43 | |
Eli Friedman | c6d0782 | 2011-05-02 18:05:27 +0000 | [diff] [blame] | 44 | struct CallArg { |
| 45 | RValue RV; |
| 46 | QualType Ty; |
Eli Friedman | 55d4848 | 2011-05-26 00:10:27 +0000 | [diff] [blame] | 47 | bool NeedsCopy; |
| 48 | CallArg(RValue rv, QualType ty, bool needscopy) |
| 49 | : RV(rv), Ty(ty), NeedsCopy(needscopy) |
Eli Friedman | c6d0782 | 2011-05-02 18:05:27 +0000 | [diff] [blame] | 50 | { } |
| 51 | }; |
| 52 | |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 53 | /// CallArgList - Type for representing both the value and type of |
| 54 | /// arguments in a call. |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 55 | class CallArgList : |
Chris Lattner | 686775d | 2011-07-20 06:58:45 +0000 | [diff] [blame] | 56 | public SmallVector<CallArg, 16> { |
John McCall | 413ebdb | 2011-03-11 20:59:21 +0000 | [diff] [blame] | 57 | public: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 58 | struct Writeback { |
| 59 | /// The original argument. |
| 60 | llvm::Value *Address; |
| 61 | |
| 62 | /// The pointee type of the original argument. |
| 63 | QualType AddressType; |
| 64 | |
| 65 | /// The temporary alloca. |
| 66 | llvm::Value *Temporary; |
| 67 | }; |
| 68 | |
Eli Friedman | 55d4848 | 2011-05-26 00:10:27 +0000 | [diff] [blame] | 69 | void add(RValue rvalue, QualType type, bool needscopy = false) { |
| 70 | push_back(CallArg(rvalue, type, needscopy)); |
John McCall | 413ebdb | 2011-03-11 20:59:21 +0000 | [diff] [blame] | 71 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 72 | |
| 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()); |
| 77 | } |
| 78 | |
| 79 | void addWriteback(llvm::Value *address, QualType addressType, |
| 80 | llvm::Value *temporary) { |
| 81 | Writeback writeback; |
| 82 | writeback.Address = address; |
| 83 | writeback.AddressType = addressType; |
| 84 | writeback.Temporary = temporary; |
| 85 | Writebacks.push_back(writeback); |
| 86 | } |
| 87 | |
| 88 | bool hasWritebacks() const { return !Writebacks.empty(); } |
| 89 | |
Chris Lattner | 686775d | 2011-07-20 06:58:45 +0000 | [diff] [blame] | 90 | typedef SmallVectorImpl<Writeback>::const_iterator writeback_iterator; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 91 | writeback_iterator writeback_begin() const { return Writebacks.begin(); } |
| 92 | writeback_iterator writeback_end() const { return Writebacks.end(); } |
| 93 | |
| 94 | private: |
Chris Lattner | 686775d | 2011-07-20 06:58:45 +0000 | [diff] [blame] | 95 | SmallVector<Writeback, 1> Writebacks; |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 96 | }; |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 97 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 98 | /// A class for recording the number of arguments that a function |
| 99 | /// signature requires. |
| 100 | class RequiredArgs { |
| 101 | /// The number of required arguments, or ~0 if the signature does |
| 102 | /// not permit optional arguments. |
| 103 | unsigned NumRequired; |
| 104 | public: |
| 105 | enum All_t { All }; |
| 106 | |
| 107 | RequiredArgs(All_t _) : NumRequired(~0U) {} |
| 108 | explicit RequiredArgs(unsigned n) : NumRequired(n) { |
| 109 | assert(n != ~0U); |
| 110 | } |
| 111 | |
| 112 | /// Compute the arguments required by the given formal prototype, |
| 113 | /// given that there may be some additional, non-formal arguments |
| 114 | /// in play. |
| 115 | static RequiredArgs forPrototypePlus(const FunctionProtoType *prototype, |
| 116 | unsigned additional) { |
| 117 | if (!prototype->isVariadic()) return All; |
| 118 | return RequiredArgs(prototype->getNumArgs() + additional); |
| 119 | } |
| 120 | |
| 121 | static RequiredArgs forPrototype(const FunctionProtoType *prototype) { |
| 122 | return forPrototypePlus(prototype, 0); |
| 123 | } |
| 124 | |
| 125 | static RequiredArgs forPrototype(CanQual<FunctionProtoType> prototype) { |
| 126 | return forPrototype(prototype.getTypePtr()); |
| 127 | } |
| 128 | |
| 129 | static RequiredArgs forPrototypePlus(CanQual<FunctionProtoType> prototype, |
| 130 | unsigned additional) { |
| 131 | return forPrototypePlus(prototype.getTypePtr(), additional); |
| 132 | } |
| 133 | |
| 134 | bool allowsOptionalArgs() const { return NumRequired != ~0U; } |
John McCall | e56bb36 | 2012-12-07 07:03:17 +0000 | [diff] [blame] | 135 | unsigned getNumRequiredArgs() const { |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 136 | assert(allowsOptionalArgs()); |
| 137 | return NumRequired; |
| 138 | } |
| 139 | |
| 140 | unsigned getOpaqueData() const { return NumRequired; } |
| 141 | static RequiredArgs getFromOpaqueData(unsigned value) { |
| 142 | if (value == ~0U) return All; |
| 143 | return RequiredArgs(value); |
| 144 | } |
| 145 | }; |
| 146 | |
Daniel Dunbar | 7c08651 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 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. |
Chris Lattner | 686775d | 2011-07-20 06:58:45 +0000 | [diff] [blame] | 150 | class FunctionArgList : public SmallVector<const VarDecl*, 16> { |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 151 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 152 | |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 153 | /// CGFunctionInfo - Class to encapsulate the information about a |
| 154 | /// function definition. |
Daniel Dunbar | 40a6be6 | 2009-02-03 00:07:12 +0000 | [diff] [blame] | 155 | class CGFunctionInfo : public llvm::FoldingSetNode { |
Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 156 | struct ArgInfo { |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 157 | CanQualType type; |
Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 158 | ABIArgInfo info; |
| 159 | }; |
| 160 | |
Daniel Dunbar | ca6408c | 2009-09-12 00:59:20 +0000 | [diff] [blame] | 161 | /// The LLVM::CallingConv to use for this function (as specified by the |
| 162 | /// user). |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 163 | unsigned CallingConvention : 8; |
Daniel Dunbar | bac7c25 | 2009-09-11 22:24:53 +0000 | [diff] [blame] | 164 | |
Daniel Dunbar | ca6408c | 2009-09-12 00:59:20 +0000 | [diff] [blame] | 165 | /// The LLVM::CallingConv to actually use for this function, which may |
| 166 | /// depend on the ABI. |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 167 | unsigned EffectiveCallingConvention : 8; |
| 168 | |
| 169 | /// The clang::CallingConv that this was originally created with. |
| 170 | unsigned ASTCallingConvention : 8; |
Daniel Dunbar | ca6408c | 2009-09-12 00:59:20 +0000 | [diff] [blame] | 171 | |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 172 | /// Whether this function is noreturn. |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 173 | unsigned NoReturn : 1; |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 174 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 175 | /// Whether this function is returns-retained. |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 176 | unsigned ReturnsRetained : 1; |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 177 | |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 178 | /// How many arguments to pass inreg. |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 179 | unsigned HasRegParm : 1; |
| 180 | unsigned RegParm : 4; |
| 181 | |
| 182 | RequiredArgs Required; |
| 183 | |
| 184 | unsigned NumArgs; |
| 185 | ArgInfo *getArgsBuffer() { |
| 186 | return reinterpret_cast<ArgInfo*>(this+1); |
| 187 | } |
| 188 | const ArgInfo *getArgsBuffer() const { |
| 189 | return reinterpret_cast<const ArgInfo*>(this + 1); |
| 190 | } |
| 191 | |
| 192 | CGFunctionInfo() : Required(RequiredArgs::All) {} |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 193 | |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 194 | public: |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 195 | static CGFunctionInfo *create(unsigned llvmCC, |
| 196 | const FunctionType::ExtInfo &extInfo, |
| 197 | CanQualType resultType, |
| 198 | ArrayRef<CanQualType> argTypes, |
| 199 | RequiredArgs required); |
| 200 | |
Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 201 | typedef const ArgInfo *const_arg_iterator; |
| 202 | typedef ArgInfo *arg_iterator; |
Daniel Dunbar | a0a99e0 | 2009-02-02 23:43:58 +0000 | [diff] [blame] | 203 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 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; } |
Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 208 | |
Daniel Dunbar | 4b5f0a4 | 2009-02-04 21:17:21 +0000 | [diff] [blame] | 209 | unsigned arg_size() const { return NumArgs; } |
| 210 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 211 | bool isVariadic() const { return Required.allowsOptionalArgs(); } |
| 212 | RequiredArgs getRequiredArgs() const { return Required; } |
| 213 | |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 214 | bool isNoReturn() const { return NoReturn; } |
| 215 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 216 | /// In ARC, whether this function retains its return value. This |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 217 | /// is not always reliable for call sites. |
| 218 | bool isReturnsRetained() const { return ReturnsRetained; } |
| 219 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 220 | /// getASTCallingConvention() - Return the AST-specified calling |
Daniel Dunbar | ca6408c | 2009-09-12 00:59:20 +0000 | [diff] [blame] | 221 | /// convention. |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 222 | CallingConv getASTCallingConvention() const { |
| 223 | return CallingConv(ASTCallingConvention); |
| 224 | } |
| 225 | |
| 226 | /// getCallingConvention - Return the user specified calling |
| 227 | /// convention, which has been translated into an LLVM CC. |
Daniel Dunbar | bac7c25 | 2009-09-11 22:24:53 +0000 | [diff] [blame] | 228 | unsigned getCallingConvention() const { return CallingConvention; } |
| 229 | |
Daniel Dunbar | ca6408c | 2009-09-12 00:59:20 +0000 | [diff] [blame] | 230 | /// getEffectiveCallingConvention - Return the actual calling convention to |
| 231 | /// use, which may depend on the ABI. |
| 232 | unsigned getEffectiveCallingConvention() const { |
| 233 | return EffectiveCallingConvention; |
| 234 | } |
| 235 | void setEffectiveCallingConvention(unsigned Value) { |
| 236 | EffectiveCallingConvention = Value; |
| 237 | } |
| 238 | |
Eli Friedman | a49218e | 2011-04-09 08:18:08 +0000 | [diff] [blame] | 239 | bool getHasRegParm() const { return HasRegParm; } |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 240 | unsigned getRegParm() const { return RegParm; } |
| 241 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 242 | FunctionType::ExtInfo getExtInfo() const { |
| 243 | return FunctionType::ExtInfo(isNoReturn(), |
| 244 | getHasRegParm(), getRegParm(), |
| 245 | getASTCallingConvention(), |
| 246 | isReturnsRetained()); |
| 247 | } |
Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 248 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 249 | CanQualType getReturnType() const { return getArgsBuffer()[0].type; } |
| 250 | |
| 251 | ABIArgInfo &getReturnInfo() { return getArgsBuffer()[0].info; } |
| 252 | const ABIArgInfo &getReturnInfo() const { return getArgsBuffer()[0].info; } |
Daniel Dunbar | 40a6be6 | 2009-02-03 00:07:12 +0000 | [diff] [blame] | 253 | |
| 254 | void Profile(llvm::FoldingSetNodeID &ID) { |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 255 | ID.AddInteger(getASTCallingConvention()); |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 256 | ID.AddBoolean(NoReturn); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 257 | ID.AddBoolean(ReturnsRetained); |
Eli Friedman | a49218e | 2011-04-09 08:18:08 +0000 | [diff] [blame] | 258 | ID.AddBoolean(HasRegParm); |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 259 | ID.AddInteger(RegParm); |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 260 | ID.AddInteger(Required.getOpaqueData()); |
Daniel Dunbar | 35e67d4 | 2009-02-05 00:00:23 +0000 | [diff] [blame] | 261 | getReturnType().Profile(ID); |
Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 262 | for (arg_iterator it = arg_begin(), ie = arg_end(); it != ie; ++it) |
| 263 | it->type.Profile(ID); |
Daniel Dunbar | 40a6be6 | 2009-02-03 00:07:12 +0000 | [diff] [blame] | 264 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 265 | static void Profile(llvm::FoldingSetNodeID &ID, |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 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) { |
| 279 | i->Profile(ID); |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 280 | } |
Daniel Dunbar | 40a6be6 | 2009-02-03 00:07:12 +0000 | [diff] [blame] | 281 | } |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 282 | }; |
Anders Carlsson | 31777a2 | 2009-12-24 19:08:58 +0000 | [diff] [blame] | 283 | |
Anders Carlsson | d2490a9 | 2009-12-24 20:40:36 +0000 | [diff] [blame] | 284 | /// ReturnValueSlot - Contains the address where the return value of a |
| 285 | /// function can be stored, and whether the address is volatile or not. |
Anders Carlsson | 31777a2 | 2009-12-24 19:08:58 +0000 | [diff] [blame] | 286 | class ReturnValueSlot { |
| 287 | llvm::PointerIntPair<llvm::Value *, 1, bool> Value; |
| 288 | |
| 289 | public: |
| 290 | ReturnValueSlot() {} |
| 291 | ReturnValueSlot(llvm::Value *Value, bool IsVolatile) |
| 292 | : Value(Value, IsVolatile) {} |
| 293 | |
| 294 | bool isNull() const { return !getValue(); } |
| 295 | |
| 296 | bool isVolatile() const { return Value.getInt(); } |
| 297 | llvm::Value *getValue() const { return Value.getPointer(); } |
| 298 | }; |
| 299 | |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 300 | } // end namespace CodeGen |
| 301 | } // end namespace clang |
| 302 | |
| 303 | #endif |