Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 2 | |
| 3 | #ifndef ART_SRC_CALLING_CONVENTION_H_ |
| 4 | #define ART_SRC_CALLING_CONVENTION_H_ |
| 5 | |
Ian Rogers | 0d666d8 | 2011-08-14 16:03:46 -0700 | [diff] [blame] | 6 | #include <vector> |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 7 | #include "managed_register.h" |
| 8 | #include "object.h" |
| 9 | #include "thread.h" |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 10 | |
| 11 | namespace art { |
| 12 | |
| 13 | // Top-level abstraction for different calling conventions |
| 14 | class CallingConvention { |
| 15 | public: |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 16 | bool IsReturnAReference() const { return method_->IsReturnAReference(); } |
| 17 | |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 18 | size_t SizeOfReturnValue() const { return method_->ReturnSize(); } |
| 19 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 20 | // Register that holds result of this method |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 21 | virtual ManagedRegister ReturnRegister() = 0; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 22 | // Register reserved for scratch usage during procedure calls |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 23 | virtual ManagedRegister InterproceduralScratchRegister() = 0; |
Shih-wei Liao | 668512a | 2011-09-01 14:18:34 -0700 | [diff] [blame] | 24 | |
Carl Shapiro | e2d373e | 2011-07-25 15:20:06 -0700 | [diff] [blame] | 25 | // Offset of Method within the frame |
| 26 | FrameOffset MethodStackOffset(); |
| 27 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 28 | // Iterator interface |
| 29 | |
| 30 | // Place iterator at start of arguments. The displacement is applied to |
| 31 | // frame offset methods to account for frames which may be on the stack |
| 32 | // below the one being iterated over. |
| 33 | void ResetIterator(FrameOffset displacement) { |
| 34 | displacement_ = displacement; |
Shih-wei Liao | 5381cf9 | 2011-07-27 00:28:04 -0700 | [diff] [blame] | 35 | itr_slots_ = 0; |
| 36 | itr_args_ = 0; |
Shih-wei Liao | 668512a | 2011-09-01 14:18:34 -0700 | [diff] [blame] | 37 | itr_refs_ = 0; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 38 | itr_longs_and_doubles_ = 0; |
| 39 | } |
| 40 | |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 41 | virtual ~CallingConvention() {} |
| 42 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 43 | protected: |
| 44 | explicit CallingConvention(Method* method) : displacement_(0), |
| 45 | method_(method) {} |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 46 | |
| 47 | Method* GetMethod() const { return method_; } |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 48 | |
Shih-wei Liao | 5381cf9 | 2011-07-27 00:28:04 -0700 | [diff] [blame] | 49 | // The slot number for current calling_convention argument. |
| 50 | // Note that each slot is 32-bit. When the current argument is bigger |
| 51 | // than 32 bits, return the first slot number for this argument. |
| 52 | unsigned int itr_slots_; |
Ian Rogers | 7a99c11 | 2011-09-07 12:48:27 -0700 | [diff] [blame] | 53 | // The number of references iterated past |
| 54 | unsigned int itr_refs_; |
Shih-wei Liao | 5381cf9 | 2011-07-27 00:28:04 -0700 | [diff] [blame] | 55 | // The argument number along argument list for current argument |
| 56 | unsigned int itr_args_; |
| 57 | // Number of longs and doubles seen along argument list |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 58 | unsigned int itr_longs_and_doubles_; |
| 59 | // Space for frames below this on the stack |
| 60 | FrameOffset displacement_; |
| 61 | |
| 62 | private: |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 63 | Method* method_; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | // Abstraction for managed code's calling conventions |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 67 | // | { Incoming stack args } | |
| 68 | // | { Prior Method* } | <-- Prior SP |
| 69 | // | { Return address } | |
| 70 | // | { Callee saves } | |
| 71 | // | { Spills ... } | |
| 72 | // | { Outgoing stack args } | |
| 73 | // | { Method* } | <-- SP |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 74 | class ManagedRuntimeCallingConvention : public CallingConvention { |
| 75 | public: |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 76 | static ManagedRuntimeCallingConvention* Create(Method* native_method, |
| 77 | InstructionSet instruction_set); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 78 | |
| 79 | size_t FrameSize(); |
| 80 | |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 81 | // Register that holds the incoming method argument |
| 82 | virtual ManagedRegister MethodRegister() = 0; |
| 83 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 84 | // Iterator interface |
| 85 | bool HasNext(); |
| 86 | void Next(); |
| 87 | bool IsCurrentParamAReference(); |
Ian Rogers | 7a99c11 | 2011-09-07 12:48:27 -0700 | [diff] [blame] | 88 | bool IsCurrentArgExplicit(); // ie a non-implict argument such as this |
| 89 | bool IsCurrentArgPossiblyNull(); |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 90 | size_t CurrentParamSize(); |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 91 | virtual bool IsCurrentParamInRegister() = 0; |
| 92 | virtual bool IsCurrentParamOnStack() = 0; |
| 93 | virtual ManagedRegister CurrentParamRegister() = 0; |
| 94 | virtual FrameOffset CurrentParamStackOffset() = 0; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 95 | |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 96 | virtual ~ManagedRuntimeCallingConvention() {} |
| 97 | |
| 98 | protected: |
| 99 | explicit ManagedRuntimeCallingConvention(Method* method) : |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 100 | CallingConvention(method) {} |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | // Abstraction for JNI calling conventions |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 104 | // | { Incoming stack args } | <-- Prior SP |
| 105 | // | { Return address } | |
| 106 | // | { Callee saves } | ([1]) |
| 107 | // | { Return value spill } | (live on return slow paths) |
| 108 | // | { Stack Indirect Ref. Table | |
| 109 | // | num. refs./link } | (here to prior SP is frame size) |
| 110 | // | { Method* } | <-- Anchor SP written to thread |
| 111 | // | { Outgoing stack args } | <-- SP at point of call |
| 112 | // | Native frame | |
| 113 | // |
| 114 | // [1] We must save all callee saves here to enable any exception throws to restore |
| 115 | // callee saves for frames above this one. |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 116 | class JniCallingConvention : public CallingConvention { |
| 117 | public: |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 118 | static JniCallingConvention* Create(Method* native_method, |
| 119 | InstructionSet instruction_set); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 120 | |
| 121 | // Size of frame excluding space for outgoing args (its assumed Method* is |
| 122 | // always at the bottom of a frame, but this doesn't work for outgoing |
| 123 | // native args). Includes alignment. |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 124 | virtual size_t FrameSize() = 0; |
Ian Rogers | 762400c | 2011-08-23 12:14:16 -0700 | [diff] [blame] | 125 | // Offset within the frame of the return pc |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 126 | virtual size_t ReturnPcOffset() = 0; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 127 | // Size of outgoing arguments, including alignment |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 128 | virtual size_t OutArgSize() = 0; |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 129 | // Number of references in stack indirect reference table |
| 130 | size_t ReferenceCount(); |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 131 | // Location where the return value of a call can be squirreled if another |
| 132 | // call is made following the native call |
| 133 | FrameOffset ReturnValueSaveLocation(); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 134 | |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 135 | // Callee save registers to spill prior to native code (which may clobber) |
| 136 | virtual const std::vector<ManagedRegister>& CalleeSaveRegisters() const = 0; |
| 137 | |
| 138 | // Spill mask values |
| 139 | virtual uint32_t CoreSpillMask() const = 0; |
| 140 | virtual uint32_t FpSpillMask() const = 0; |
Ian Rogers | 0d666d8 | 2011-08-14 16:03:46 -0700 | [diff] [blame] | 141 | |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 142 | // Returns true if the method register will have been clobbered during argument |
| 143 | // set up |
Ian Rogers | ad42e13 | 2011-09-17 20:23:33 -0700 | [diff] [blame] | 144 | virtual bool IsMethodRegisterClobberedPreCall() = 0; |
Carl Shapiro | e2d373e | 2011-07-25 15:20:06 -0700 | [diff] [blame] | 145 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 146 | // Iterator interface |
| 147 | bool HasNext(); |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 148 | virtual void Next(); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 149 | bool IsCurrentParamAReference(); |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 150 | size_t CurrentParamSize(); |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 151 | virtual bool IsCurrentParamInRegister() = 0; |
| 152 | virtual bool IsCurrentParamOnStack() = 0; |
| 153 | virtual ManagedRegister CurrentParamRegister() = 0; |
| 154 | virtual FrameOffset CurrentParamStackOffset() = 0; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 155 | |
| 156 | // Iterator interface extension for JNI |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 157 | FrameOffset CurrentParamSirtEntryOffset(); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 158 | |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 159 | // Position of SIRT and interior fields |
| 160 | FrameOffset SirtOffset() { |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 161 | return FrameOffset(displacement_.Int32Value() + |
| 162 | kPointerSize); // above Method* |
| 163 | } |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 164 | FrameOffset SirtNumRefsOffset() { |
| 165 | return FrameOffset(SirtOffset().Int32Value() + |
| 166 | StackIndirectReferenceTable::NumberOfReferencesOffset()); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 167 | } |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 168 | FrameOffset SirtLinkOffset() { |
| 169 | return FrameOffset(SirtOffset().Int32Value() + |
| 170 | StackIndirectReferenceTable::LinkOffset()); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 173 | virtual ~JniCallingConvention() {} |
| 174 | |
| 175 | protected: |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 176 | // Named iterator positions |
| 177 | enum IteratorPos { |
| 178 | kJniEnv = 0, |
| 179 | kObjectOrClass = 1 |
| 180 | }; |
| 181 | |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 182 | explicit JniCallingConvention(Method* native_method) : |
| 183 | CallingConvention(native_method) {} |
| 184 | |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 185 | // Number of stack slots for outgoing arguments, above which the SIRT is |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 186 | // located |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 187 | virtual size_t NumberOfOutgoingStackArgs() = 0; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 188 | |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 189 | protected: |
| 190 | static size_t NumberOfExtraArgumentsForJni(Method* method); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 191 | }; |
| 192 | |
| 193 | } // namespace art |
| 194 | |
| 195 | #endif // ART_SRC_CALLING_CONVENTION_H_ |