Daniel Dunbar | 3d7c90b | 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 | 0435ed5 | 2009-12-24 19:08:58 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/FoldingSet.h" |
| 19 | #include "llvm/Value.h" |
Daniel Dunbar | 3d7c90b | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 20 | #include "clang/AST/Type.h" |
John McCall | 2da83a3 | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 21 | #include "clang/AST/CanonicalType.h" |
Daniel Dunbar | 3d7c90b | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 22 | |
Daniel Dunbar | 41cf9de | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 23 | #include "CGValue.h" |
| 24 | |
Daniel Dunbar | 313321e | 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 | 3d7c90b | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 28 | namespace llvm { |
Devang Patel | 322300d | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 29 | struct AttributeWithIndex; |
Daniel Dunbar | 313321e | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 30 | class Function; |
| 31 | class Type; |
Daniel Dunbar | 3d7c90b | 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 | bc915f4 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 42 | class VarDecl; |
Daniel Dunbar | 3d7c90b | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 43 | |
| 44 | namespace CodeGen { |
Devang Patel | 322300d | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 45 | typedef llvm::SmallVector<llvm::AttributeWithIndex, 8> AttributeListType; |
Daniel Dunbar | 3d7c90b | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 46 | |
Eli Friedman | f4258eb | 2011-05-02 18:05:27 +0000 | [diff] [blame] | 47 | struct CallArg { |
| 48 | RValue RV; |
| 49 | QualType Ty; |
Eli Friedman | df96819 | 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 | f4258eb | 2011-05-02 18:05:27 +0000 | [diff] [blame] | 53 | { } |
| 54 | }; |
| 55 | |
Daniel Dunbar | 3d7c90b | 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 | a738c25 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 58 | class CallArgList : |
Eli Friedman | f4258eb | 2011-05-02 18:05:27 +0000 | [diff] [blame] | 59 | public llvm::SmallVector<CallArg, 16> { |
John McCall | 32ea969 | 2011-03-11 20:59:21 +0000 | [diff] [blame] | 60 | public: |
Eli Friedman | df96819 | 2011-05-26 00:10:27 +0000 | [diff] [blame^] | 61 | void add(RValue rvalue, QualType type, bool needscopy = false) { |
| 62 | push_back(CallArg(rvalue, type, needscopy)); |
John McCall | 32ea969 | 2011-03-11 20:59:21 +0000 | [diff] [blame] | 63 | } |
John McCall | a738c25 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 64 | }; |
Daniel Dunbar | 3d7c90b | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 65 | |
Daniel Dunbar | bc915f4 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 66 | /// FunctionArgList - Type for representing both the decl and type |
| 67 | /// of parameters to a function. The decl must be either a |
| 68 | /// ParmVarDecl or ImplicitParamDecl. |
John McCall | a738c25 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 69 | class FunctionArgList : public llvm::SmallVector<const VarDecl*, 16> { |
| 70 | }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 71 | |
Daniel Dunbar | 3d7c90b | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 72 | /// CGFunctionInfo - Class to encapsulate the information about a |
| 73 | /// function definition. |
Daniel Dunbar | e0be829 | 2009-02-03 00:07:12 +0000 | [diff] [blame] | 74 | class CGFunctionInfo : public llvm::FoldingSetNode { |
Daniel Dunbar | 313321e | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 75 | struct ArgInfo { |
John McCall | 2da83a3 | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 76 | CanQualType type; |
Daniel Dunbar | 313321e | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 77 | ABIArgInfo info; |
| 78 | }; |
| 79 | |
Daniel Dunbar | 0ef3479 | 2009-09-12 00:59:20 +0000 | [diff] [blame] | 80 | /// The LLVM::CallingConv to use for this function (as specified by the |
| 81 | /// user). |
Daniel Dunbar | 7feafc7 | 2009-09-11 22:24:53 +0000 | [diff] [blame] | 82 | unsigned CallingConvention; |
| 83 | |
Daniel Dunbar | 0ef3479 | 2009-09-12 00:59:20 +0000 | [diff] [blame] | 84 | /// The LLVM::CallingConv to actually use for this function, which may |
| 85 | /// depend on the ABI. |
| 86 | unsigned EffectiveCallingConvention; |
| 87 | |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 88 | /// Whether this function is noreturn. |
| 89 | bool NoReturn; |
| 90 | |
Daniel Dunbar | 313321e | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 91 | unsigned NumArgs; |
| 92 | ArgInfo *Args; |
Daniel Dunbar | 3d7c90b | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 93 | |
Rafael Espindola | 49b85ab | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 94 | /// How many arguments to pass inreg. |
Eli Friedman | c5b20b5 | 2011-04-09 08:18:08 +0000 | [diff] [blame] | 95 | bool HasRegParm; |
Rafael Espindola | 49b85ab | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 96 | unsigned RegParm; |
| 97 | |
Daniel Dunbar | 3d7c90b | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 98 | public: |
Daniel Dunbar | 313321e | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 99 | typedef const ArgInfo *const_arg_iterator; |
| 100 | typedef ArgInfo *arg_iterator; |
Daniel Dunbar | 3668cb2 | 2009-02-02 23:43:58 +0000 | [diff] [blame] | 101 | |
Chris Lattner | 34d6281 | 2010-06-29 18:13:52 +0000 | [diff] [blame] | 102 | CGFunctionInfo(unsigned CallingConvention, bool NoReturn, |
Eli Friedman | c5b20b5 | 2011-04-09 08:18:08 +0000 | [diff] [blame] | 103 | bool HasRegParm, unsigned RegParm, CanQualType ResTy, |
Chris Lattner | 34d6281 | 2010-06-29 18:13:52 +0000 | [diff] [blame] | 104 | const CanQualType *ArgTys, unsigned NumArgTys); |
Daniel Dunbar | 313321e | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 105 | ~CGFunctionInfo() { delete[] Args; } |
Daniel Dunbar | 3d7c90b | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 106 | |
Daniel Dunbar | 313321e | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 107 | const_arg_iterator arg_begin() const { return Args + 1; } |
| 108 | const_arg_iterator arg_end() const { return Args + 1 + NumArgs; } |
| 109 | arg_iterator arg_begin() { return Args + 1; } |
| 110 | arg_iterator arg_end() { return Args + 1 + NumArgs; } |
Daniel Dunbar | 7633cbf | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 111 | |
Daniel Dunbar | a45bdbb | 2009-02-04 21:17:21 +0000 | [diff] [blame] | 112 | unsigned arg_size() const { return NumArgs; } |
| 113 | |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 114 | bool isNoReturn() const { return NoReturn; } |
| 115 | |
Daniel Dunbar | 0ef3479 | 2009-09-12 00:59:20 +0000 | [diff] [blame] | 116 | /// getCallingConvention - Return the user specified calling |
| 117 | /// convention. |
Daniel Dunbar | 7feafc7 | 2009-09-11 22:24:53 +0000 | [diff] [blame] | 118 | unsigned getCallingConvention() const { return CallingConvention; } |
| 119 | |
Daniel Dunbar | 0ef3479 | 2009-09-12 00:59:20 +0000 | [diff] [blame] | 120 | /// getEffectiveCallingConvention - Return the actual calling convention to |
| 121 | /// use, which may depend on the ABI. |
| 122 | unsigned getEffectiveCallingConvention() const { |
| 123 | return EffectiveCallingConvention; |
| 124 | } |
| 125 | void setEffectiveCallingConvention(unsigned Value) { |
| 126 | EffectiveCallingConvention = Value; |
| 127 | } |
| 128 | |
Eli Friedman | c5b20b5 | 2011-04-09 08:18:08 +0000 | [diff] [blame] | 129 | bool getHasRegParm() const { return HasRegParm; } |
Rafael Espindola | 49b85ab | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 130 | unsigned getRegParm() const { return RegParm; } |
| 131 | |
John McCall | 2da83a3 | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 132 | CanQualType getReturnType() const { return Args[0].type; } |
Daniel Dunbar | 313321e | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 133 | |
| 134 | ABIArgInfo &getReturnInfo() { return Args[0].info; } |
| 135 | const ABIArgInfo &getReturnInfo() const { return Args[0].info; } |
Daniel Dunbar | e0be829 | 2009-02-03 00:07:12 +0000 | [diff] [blame] | 136 | |
| 137 | void Profile(llvm::FoldingSetNodeID &ID) { |
Daniel Dunbar | 7feafc7 | 2009-09-11 22:24:53 +0000 | [diff] [blame] | 138 | ID.AddInteger(getCallingConvention()); |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 139 | ID.AddBoolean(NoReturn); |
Eli Friedman | c5b20b5 | 2011-04-09 08:18:08 +0000 | [diff] [blame] | 140 | ID.AddBoolean(HasRegParm); |
Rafael Espindola | 49b85ab | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 141 | ID.AddInteger(RegParm); |
Daniel Dunbar | fff09f3 | 2009-02-05 00:00:23 +0000 | [diff] [blame] | 142 | getReturnType().Profile(ID); |
Daniel Dunbar | 313321e | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 143 | for (arg_iterator it = arg_begin(), ie = arg_end(); it != ie; ++it) |
| 144 | it->type.Profile(ID); |
Daniel Dunbar | e0be829 | 2009-02-03 00:07:12 +0000 | [diff] [blame] | 145 | } |
| 146 | template<class Iterator> |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 147 | static void Profile(llvm::FoldingSetNodeID &ID, |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 148 | const FunctionType::ExtInfo &Info, |
John McCall | 2da83a3 | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 149 | CanQualType ResTy, |
Daniel Dunbar | e0be829 | 2009-02-03 00:07:12 +0000 | [diff] [blame] | 150 | Iterator begin, |
| 151 | Iterator end) { |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 152 | ID.AddInteger(Info.getCC()); |
| 153 | ID.AddBoolean(Info.getNoReturn()); |
Eli Friedman | c5b20b5 | 2011-04-09 08:18:08 +0000 | [diff] [blame] | 154 | ID.AddBoolean(Info.getHasRegParm()); |
Rafael Espindola | 49b85ab | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 155 | ID.AddInteger(Info.getRegParm()); |
Daniel Dunbar | e0be829 | 2009-02-03 00:07:12 +0000 | [diff] [blame] | 156 | ResTy.Profile(ID); |
John McCall | 2da83a3 | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 157 | for (; begin != end; ++begin) { |
| 158 | CanQualType T = *begin; // force iterator to be over canonical types |
| 159 | T.Profile(ID); |
| 160 | } |
Daniel Dunbar | e0be829 | 2009-02-03 00:07:12 +0000 | [diff] [blame] | 161 | } |
Daniel Dunbar | 3d7c90b | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 162 | }; |
Anders Carlsson | 0435ed5 | 2009-12-24 19:08:58 +0000 | [diff] [blame] | 163 | |
Anders Carlsson | 1749083 | 2009-12-24 20:40:36 +0000 | [diff] [blame] | 164 | /// ReturnValueSlot - Contains the address where the return value of a |
| 165 | /// function can be stored, and whether the address is volatile or not. |
Anders Carlsson | 0435ed5 | 2009-12-24 19:08:58 +0000 | [diff] [blame] | 166 | class ReturnValueSlot { |
| 167 | llvm::PointerIntPair<llvm::Value *, 1, bool> Value; |
| 168 | |
| 169 | public: |
| 170 | ReturnValueSlot() {} |
| 171 | ReturnValueSlot(llvm::Value *Value, bool IsVolatile) |
| 172 | : Value(Value, IsVolatile) {} |
| 173 | |
| 174 | bool isNull() const { return !getValue(); } |
| 175 | |
| 176 | bool isVolatile() const { return Value.getInt(); } |
| 177 | llvm::Value *getValue() const { return Value.getPointer(); } |
| 178 | }; |
| 179 | |
Daniel Dunbar | 3d7c90b | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 180 | } // end namespace CodeGen |
| 181 | } // end namespace clang |
| 182 | |
| 183 | #endif |