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 | |
Anders Carlsson | 31777a2 | 2009-12-24 19:08:58 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/FoldingSet.h" |
| 19 | #include "llvm/Value.h" |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 20 | #include "clang/AST/Type.h" |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 21 | #include "clang/AST/CanonicalType.h" |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 22 | |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 23 | #include "CGValue.h" |
| 24 | |
Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 25 | // FIXME: Restructure so we don't have to expose so much stuff. |
| 26 | #include "ABIInfo.h" |
| 27 | |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 28 | namespace llvm { |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 29 | struct AttributeWithIndex; |
Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 30 | class Function; |
| 31 | class Type; |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 32 | class Value; |
| 33 | |
| 34 | template<typename T, unsigned> class SmallVector; |
| 35 | } |
| 36 | |
| 37 | namespace clang { |
| 38 | class ASTContext; |
| 39 | class Decl; |
| 40 | class FunctionDecl; |
| 41 | class ObjCMethodDecl; |
Daniel Dunbar | 7c08651 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 42 | class VarDecl; |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 43 | |
| 44 | namespace CodeGen { |
Chris Lattner | 686775d | 2011-07-20 06:58:45 +0000 | [diff] [blame] | 45 | typedef SmallVector<llvm::AttributeWithIndex, 8> AttributeListType; |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 46 | |
Eli Friedman | c6d0782 | 2011-05-02 18:05:27 +0000 | [diff] [blame] | 47 | struct CallArg { |
| 48 | RValue RV; |
| 49 | QualType Ty; |
Eli Friedman | 55d4848 | 2011-05-26 00:10:27 +0000 | [diff] [blame] | 50 | bool NeedsCopy; |
| 51 | CallArg(RValue rv, QualType ty, bool needscopy) |
| 52 | : RV(rv), Ty(ty), NeedsCopy(needscopy) |
Eli Friedman | c6d0782 | 2011-05-02 18:05:27 +0000 | [diff] [blame] | 53 | { } |
| 54 | }; |
| 55 | |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 56 | /// CallArgList - Type for representing both the value and type of |
| 57 | /// arguments in a call. |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 58 | class CallArgList : |
Chris Lattner | 686775d | 2011-07-20 06:58:45 +0000 | [diff] [blame] | 59 | public SmallVector<CallArg, 16> { |
John McCall | 413ebdb | 2011-03-11 20:59:21 +0000 | [diff] [blame] | 60 | public: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 61 | struct Writeback { |
| 62 | /// The original argument. |
| 63 | llvm::Value *Address; |
| 64 | |
| 65 | /// The pointee type of the original argument. |
| 66 | QualType AddressType; |
| 67 | |
| 68 | /// The temporary alloca. |
| 69 | llvm::Value *Temporary; |
| 70 | }; |
| 71 | |
Eli Friedman | 55d4848 | 2011-05-26 00:10:27 +0000 | [diff] [blame] | 72 | void add(RValue rvalue, QualType type, bool needscopy = false) { |
| 73 | push_back(CallArg(rvalue, type, needscopy)); |
John McCall | 413ebdb | 2011-03-11 20:59:21 +0000 | [diff] [blame] | 74 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 75 | |
| 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()); |
| 80 | } |
| 81 | |
| 82 | void addWriteback(llvm::Value *address, QualType addressType, |
| 83 | llvm::Value *temporary) { |
| 84 | Writeback writeback; |
| 85 | writeback.Address = address; |
| 86 | writeback.AddressType = addressType; |
| 87 | writeback.Temporary = temporary; |
| 88 | Writebacks.push_back(writeback); |
| 89 | } |
| 90 | |
| 91 | bool hasWritebacks() const { return !Writebacks.empty(); } |
| 92 | |
Chris Lattner | 686775d | 2011-07-20 06:58:45 +0000 | [diff] [blame] | 93 | typedef SmallVectorImpl<Writeback>::const_iterator writeback_iterator; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 94 | writeback_iterator writeback_begin() const { return Writebacks.begin(); } |
| 95 | writeback_iterator writeback_end() const { return Writebacks.end(); } |
| 96 | |
| 97 | private: |
Chris Lattner | 686775d | 2011-07-20 06:58:45 +0000 | [diff] [blame] | 98 | SmallVector<Writeback, 1> Writebacks; |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 99 | }; |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 100 | |
Daniel Dunbar | 7c08651 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 101 | /// FunctionArgList - Type for representing both the decl and type |
| 102 | /// of parameters to a function. The decl must be either a |
| 103 | /// ParmVarDecl or ImplicitParamDecl. |
Chris Lattner | 686775d | 2011-07-20 06:58:45 +0000 | [diff] [blame] | 104 | class FunctionArgList : public SmallVector<const VarDecl*, 16> { |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 105 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 106 | |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 107 | /// CGFunctionInfo - Class to encapsulate the information about a |
| 108 | /// function definition. |
Daniel Dunbar | 40a6be6 | 2009-02-03 00:07:12 +0000 | [diff] [blame] | 109 | class CGFunctionInfo : public llvm::FoldingSetNode { |
Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 110 | struct ArgInfo { |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 111 | CanQualType type; |
Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 112 | ABIArgInfo info; |
| 113 | }; |
| 114 | |
Daniel Dunbar | ca6408c | 2009-09-12 00:59:20 +0000 | [diff] [blame] | 115 | /// The LLVM::CallingConv to use for this function (as specified by the |
| 116 | /// user). |
Daniel Dunbar | bac7c25 | 2009-09-11 22:24:53 +0000 | [diff] [blame] | 117 | unsigned CallingConvention; |
| 118 | |
Daniel Dunbar | ca6408c | 2009-09-12 00:59:20 +0000 | [diff] [blame] | 119 | /// The LLVM::CallingConv to actually use for this function, which may |
| 120 | /// depend on the ABI. |
| 121 | unsigned EffectiveCallingConvention; |
| 122 | |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 123 | /// Whether this function is noreturn. |
| 124 | bool NoReturn; |
| 125 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 126 | /// Whether this function is returns-retained. |
| 127 | bool ReturnsRetained; |
| 128 | |
Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 129 | unsigned NumArgs; |
| 130 | ArgInfo *Args; |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 131 | |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 132 | /// How many arguments to pass inreg. |
Eli Friedman | a49218e | 2011-04-09 08:18:08 +0000 | [diff] [blame] | 133 | bool HasRegParm; |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 134 | unsigned RegParm; |
| 135 | |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 136 | public: |
Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 137 | typedef const ArgInfo *const_arg_iterator; |
| 138 | typedef ArgInfo *arg_iterator; |
Daniel Dunbar | a0a99e0 | 2009-02-02 23:43:58 +0000 | [diff] [blame] | 139 | |
Chris Lattner | bb52114 | 2010-06-29 18:13:52 +0000 | [diff] [blame] | 140 | CGFunctionInfo(unsigned CallingConvention, bool NoReturn, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 141 | bool ReturnsRetained, bool HasRegParm, unsigned RegParm, |
| 142 | CanQualType ResTy, |
Chris Lattner | bb52114 | 2010-06-29 18:13:52 +0000 | [diff] [blame] | 143 | const CanQualType *ArgTys, unsigned NumArgTys); |
Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 144 | ~CGFunctionInfo() { delete[] Args; } |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 145 | |
Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 146 | const_arg_iterator arg_begin() const { return Args + 1; } |
| 147 | const_arg_iterator arg_end() const { return Args + 1 + NumArgs; } |
| 148 | arg_iterator arg_begin() { return Args + 1; } |
| 149 | arg_iterator arg_end() { return Args + 1 + NumArgs; } |
Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 150 | |
Daniel Dunbar | 4b5f0a4 | 2009-02-04 21:17:21 +0000 | [diff] [blame] | 151 | unsigned arg_size() const { return NumArgs; } |
| 152 | |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 153 | bool isNoReturn() const { return NoReturn; } |
| 154 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 155 | /// In ARR, whether this function retains its return value. This |
| 156 | /// is not always reliable for call sites. |
| 157 | bool isReturnsRetained() const { return ReturnsRetained; } |
| 158 | |
Daniel Dunbar | ca6408c | 2009-09-12 00:59:20 +0000 | [diff] [blame] | 159 | /// getCallingConvention - Return the user specified calling |
| 160 | /// convention. |
Daniel Dunbar | bac7c25 | 2009-09-11 22:24:53 +0000 | [diff] [blame] | 161 | unsigned getCallingConvention() const { return CallingConvention; } |
| 162 | |
Daniel Dunbar | ca6408c | 2009-09-12 00:59:20 +0000 | [diff] [blame] | 163 | /// getEffectiveCallingConvention - Return the actual calling convention to |
| 164 | /// use, which may depend on the ABI. |
| 165 | unsigned getEffectiveCallingConvention() const { |
| 166 | return EffectiveCallingConvention; |
| 167 | } |
| 168 | void setEffectiveCallingConvention(unsigned Value) { |
| 169 | EffectiveCallingConvention = Value; |
| 170 | } |
| 171 | |
Eli Friedman | a49218e | 2011-04-09 08:18:08 +0000 | [diff] [blame] | 172 | bool getHasRegParm() const { return HasRegParm; } |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 173 | unsigned getRegParm() const { return RegParm; } |
| 174 | |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 175 | CanQualType getReturnType() const { return Args[0].type; } |
Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 176 | |
| 177 | ABIArgInfo &getReturnInfo() { return Args[0].info; } |
| 178 | const ABIArgInfo &getReturnInfo() const { return Args[0].info; } |
Daniel Dunbar | 40a6be6 | 2009-02-03 00:07:12 +0000 | [diff] [blame] | 179 | |
| 180 | void Profile(llvm::FoldingSetNodeID &ID) { |
Daniel Dunbar | bac7c25 | 2009-09-11 22:24:53 +0000 | [diff] [blame] | 181 | ID.AddInteger(getCallingConvention()); |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 182 | ID.AddBoolean(NoReturn); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 183 | ID.AddBoolean(ReturnsRetained); |
Eli Friedman | a49218e | 2011-04-09 08:18:08 +0000 | [diff] [blame] | 184 | ID.AddBoolean(HasRegParm); |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 185 | ID.AddInteger(RegParm); |
Daniel Dunbar | 35e67d4 | 2009-02-05 00:00:23 +0000 | [diff] [blame] | 186 | getReturnType().Profile(ID); |
Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 187 | for (arg_iterator it = arg_begin(), ie = arg_end(); it != ie; ++it) |
| 188 | it->type.Profile(ID); |
Daniel Dunbar | 40a6be6 | 2009-02-03 00:07:12 +0000 | [diff] [blame] | 189 | } |
| 190 | template<class Iterator> |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 191 | static void Profile(llvm::FoldingSetNodeID &ID, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 192 | const FunctionType::ExtInfo &Info, |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 193 | CanQualType ResTy, |
Daniel Dunbar | 40a6be6 | 2009-02-03 00:07:12 +0000 | [diff] [blame] | 194 | Iterator begin, |
| 195 | Iterator end) { |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 196 | ID.AddInteger(Info.getCC()); |
| 197 | ID.AddBoolean(Info.getNoReturn()); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 198 | ID.AddBoolean(Info.getProducesResult()); |
Eli Friedman | a49218e | 2011-04-09 08:18:08 +0000 | [diff] [blame] | 199 | ID.AddBoolean(Info.getHasRegParm()); |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 200 | ID.AddInteger(Info.getRegParm()); |
Daniel Dunbar | 40a6be6 | 2009-02-03 00:07:12 +0000 | [diff] [blame] | 201 | ResTy.Profile(ID); |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 202 | for (; begin != end; ++begin) { |
| 203 | CanQualType T = *begin; // force iterator to be over canonical types |
| 204 | T.Profile(ID); |
| 205 | } |
Daniel Dunbar | 40a6be6 | 2009-02-03 00:07:12 +0000 | [diff] [blame] | 206 | } |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 207 | }; |
Anders Carlsson | 31777a2 | 2009-12-24 19:08:58 +0000 | [diff] [blame] | 208 | |
Anders Carlsson | d2490a9 | 2009-12-24 20:40:36 +0000 | [diff] [blame] | 209 | /// ReturnValueSlot - Contains the address where the return value of a |
| 210 | /// function can be stored, and whether the address is volatile or not. |
Anders Carlsson | 31777a2 | 2009-12-24 19:08:58 +0000 | [diff] [blame] | 211 | class ReturnValueSlot { |
| 212 | llvm::PointerIntPair<llvm::Value *, 1, bool> Value; |
| 213 | |
| 214 | public: |
| 215 | ReturnValueSlot() {} |
| 216 | ReturnValueSlot(llvm::Value *Value, bool IsVolatile) |
| 217 | : Value(Value, IsVolatile) {} |
| 218 | |
| 219 | bool isNull() const { return !getValue(); } |
| 220 | |
| 221 | bool isVolatile() const { return Value.getInt(); } |
| 222 | llvm::Value *getValue() const { return Value.getPointer(); } |
| 223 | }; |
| 224 | |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 225 | } // end namespace CodeGen |
| 226 | } // end namespace clang |
| 227 | |
| 228 | #endif |