Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 16 | |
| 17 | #ifndef ART_SRC_CALLING_CONVENTION_H_ |
| 18 | #define ART_SRC_CALLING_CONVENTION_H_ |
| 19 | |
Ian Rogers | 0d666d8 | 2011-08-14 16:03:46 -0700 | [diff] [blame] | 20 | #include <vector> |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 21 | #include "managed_register.h" |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 22 | #include "stack_indirect_reference_table.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 23 | #include "thread.h" |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 24 | |
| 25 | namespace art { |
| 26 | |
| 27 | // Top-level abstraction for different calling conventions |
| 28 | class CallingConvention { |
| 29 | public: |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 30 | bool IsReturnAReference() const { return shorty_[0] == 'L'; } |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 31 | |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 32 | size_t SizeOfReturnValue() const { |
| 33 | size_t result = Primitive::ComponentSize(Primitive::GetType(shorty_[0])); |
| 34 | if (result >= 1 && result < 4) { |
| 35 | result = 4; |
| 36 | } |
| 37 | return result; |
| 38 | } |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 39 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 40 | // Register that holds result of this method |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 41 | virtual ManagedRegister ReturnRegister() = 0; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 42 | // Register reserved for scratch usage during procedure calls |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 43 | virtual ManagedRegister InterproceduralScratchRegister() = 0; |
Shih-wei Liao | 668512a | 2011-09-01 14:18:34 -0700 | [diff] [blame] | 44 | |
Carl Shapiro | e2d373e | 2011-07-25 15:20:06 -0700 | [diff] [blame] | 45 | // Offset of Method within the frame |
| 46 | FrameOffset MethodStackOffset(); |
| 47 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 48 | // Iterator interface |
| 49 | |
| 50 | // Place iterator at start of arguments. The displacement is applied to |
| 51 | // frame offset methods to account for frames which may be on the stack |
| 52 | // below the one being iterated over. |
| 53 | void ResetIterator(FrameOffset displacement) { |
| 54 | displacement_ = displacement; |
Shih-wei Liao | 5381cf9 | 2011-07-27 00:28:04 -0700 | [diff] [blame] | 55 | itr_slots_ = 0; |
| 56 | itr_args_ = 0; |
Shih-wei Liao | 668512a | 2011-09-01 14:18:34 -0700 | [diff] [blame] | 57 | itr_refs_ = 0; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 58 | itr_longs_and_doubles_ = 0; |
| 59 | } |
| 60 | |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 61 | virtual ~CallingConvention() {} |
| 62 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 63 | protected: |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 64 | CallingConvention(bool is_static, bool is_synchronized, const char* shorty) |
| 65 | : displacement_(0), is_static_(is_static), is_synchronized_(is_synchronized), |
| 66 | shorty_(shorty) { |
| 67 | num_args_ = (is_static ? 0 : 1) + strlen(shorty) - 1; |
| 68 | num_ref_args_ = is_static ? 0 : 1; // The implicit this pointer. |
| 69 | num_long_or_double_args_ = 0; |
| 70 | for (size_t i = 1; i < strlen(shorty); i++) { |
| 71 | char ch = shorty_[i]; |
| 72 | if (ch == 'L') { |
| 73 | num_ref_args_++; |
| 74 | } else if ((ch == 'D') || (ch == 'J')) { |
| 75 | num_long_or_double_args_++; |
| 76 | } |
| 77 | } |
| 78 | } |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 79 | |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 80 | bool IsStatic() const { |
| 81 | return is_static_; |
| 82 | } |
| 83 | bool IsSynchronized() const { |
| 84 | return is_synchronized_; |
| 85 | } |
| 86 | bool IsParamALongOrDouble(unsigned int param) const { |
| 87 | DCHECK_LT(param, NumArgs()); |
| 88 | if (IsStatic()) { |
| 89 | param++; // 0th argument must skip return value at start of the shorty |
| 90 | } else if (param == 0) { |
| 91 | return false; // this argument |
| 92 | } |
| 93 | char ch = shorty_[param]; |
| 94 | return (ch == 'J' || ch == 'D'); |
| 95 | } |
| 96 | bool IsParamAReference(unsigned int param) const { |
| 97 | DCHECK_LT(param, NumArgs()); |
| 98 | if (IsStatic()) { |
| 99 | param++; // 0th argument must skip return value at start of the shorty |
| 100 | } else if (param == 0) { |
| 101 | return true; // this argument |
| 102 | } |
| 103 | return shorty_[param] == 'L'; |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 104 | } |
| 105 | size_t NumArgs() const { |
| 106 | return num_args_; |
| 107 | } |
| 108 | size_t NumLongOrDoubleArgs() const { |
| 109 | return num_long_or_double_args_; |
| 110 | } |
| 111 | size_t NumReferenceArgs() const { |
| 112 | return num_ref_args_; |
| 113 | } |
| 114 | size_t ParamSize(unsigned int param) const { |
| 115 | DCHECK_LT(param, NumArgs()); |
| 116 | if (IsStatic()) { |
| 117 | param++; // 0th argument must skip return value at start of the shorty |
| 118 | } else if (param == 0) { |
| 119 | return kPointerSize; // this argument |
| 120 | } |
| 121 | size_t result = Primitive::ComponentSize(Primitive::GetType(shorty_[param])); |
| 122 | if (result >= 1 && result < 4) { |
| 123 | result = 4; |
| 124 | } |
| 125 | return result; |
| 126 | } |
| 127 | const char* GetShorty() const { |
| 128 | return shorty_.c_str(); |
| 129 | } |
Shih-wei Liao | 5381cf9 | 2011-07-27 00:28:04 -0700 | [diff] [blame] | 130 | // The slot number for current calling_convention argument. |
| 131 | // Note that each slot is 32-bit. When the current argument is bigger |
| 132 | // than 32 bits, return the first slot number for this argument. |
| 133 | unsigned int itr_slots_; |
Ian Rogers | 7a99c11 | 2011-09-07 12:48:27 -0700 | [diff] [blame] | 134 | // The number of references iterated past |
| 135 | unsigned int itr_refs_; |
Shih-wei Liao | 5381cf9 | 2011-07-27 00:28:04 -0700 | [diff] [blame] | 136 | // The argument number along argument list for current argument |
| 137 | unsigned int itr_args_; |
| 138 | // Number of longs and doubles seen along argument list |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 139 | unsigned int itr_longs_and_doubles_; |
| 140 | // Space for frames below this on the stack |
| 141 | FrameOffset displacement_; |
| 142 | |
| 143 | private: |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 144 | const bool is_static_; |
| 145 | const bool is_synchronized_; |
| 146 | std::string shorty_; |
| 147 | size_t num_args_; |
| 148 | size_t num_ref_args_; |
| 149 | size_t num_long_or_double_args_; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 150 | }; |
| 151 | |
| 152 | // Abstraction for managed code's calling conventions |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 153 | // | { Incoming stack args } | |
| 154 | // | { Prior Method* } | <-- Prior SP |
| 155 | // | { Return address } | |
| 156 | // | { Callee saves } | |
| 157 | // | { Spills ... } | |
| 158 | // | { Outgoing stack args } | |
| 159 | // | { Method* } | <-- SP |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 160 | class ManagedRuntimeCallingConvention : public CallingConvention { |
| 161 | public: |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 162 | static ManagedRuntimeCallingConvention* Create(bool is_static, bool is_synchronized, |
| 163 | const char* shorty, |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 164 | InstructionSet instruction_set); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 165 | |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 166 | // Register that holds the incoming method argument |
| 167 | virtual ManagedRegister MethodRegister() = 0; |
| 168 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 169 | // Iterator interface |
| 170 | bool HasNext(); |
| 171 | void Next(); |
| 172 | bool IsCurrentParamAReference(); |
Ian Rogers | 7a99c11 | 2011-09-07 12:48:27 -0700 | [diff] [blame] | 173 | bool IsCurrentArgExplicit(); // ie a non-implict argument such as this |
| 174 | bool IsCurrentArgPossiblyNull(); |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 175 | size_t CurrentParamSize(); |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 176 | virtual bool IsCurrentParamInRegister() = 0; |
| 177 | virtual bool IsCurrentParamOnStack() = 0; |
| 178 | virtual ManagedRegister CurrentParamRegister() = 0; |
| 179 | virtual FrameOffset CurrentParamStackOffset() = 0; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 180 | |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 181 | virtual ~ManagedRuntimeCallingConvention() {} |
| 182 | |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame^] | 183 | // Registers to spill to caller's out registers on entry. |
| 184 | virtual const std::vector<ManagedRegister>& EntrySpills() = 0; |
| 185 | |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 186 | protected: |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 187 | ManagedRuntimeCallingConvention(bool is_static, bool is_synchronized, const char* shorty) : |
| 188 | CallingConvention(is_static, is_synchronized, shorty) {} |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 189 | }; |
| 190 | |
| 191 | // Abstraction for JNI calling conventions |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 192 | // | { Incoming stack args } | <-- Prior SP |
| 193 | // | { Return address } | |
| 194 | // | { Callee saves } | ([1]) |
| 195 | // | { Return value spill } | (live on return slow paths) |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 196 | // | { Local Ref. Table State } | |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 197 | // | { Stack Indirect Ref. Table | |
| 198 | // | num. refs./link } | (here to prior SP is frame size) |
| 199 | // | { Method* } | <-- Anchor SP written to thread |
| 200 | // | { Outgoing stack args } | <-- SP at point of call |
| 201 | // | Native frame | |
| 202 | // |
| 203 | // [1] We must save all callee saves here to enable any exception throws to restore |
| 204 | // callee saves for frames above this one. |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 205 | class JniCallingConvention : public CallingConvention { |
| 206 | public: |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 207 | static JniCallingConvention* Create(bool is_static, bool is_synchronized, const char* shorty, |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 208 | InstructionSet instruction_set); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 209 | |
| 210 | // Size of frame excluding space for outgoing args (its assumed Method* is |
| 211 | // always at the bottom of a frame, but this doesn't work for outgoing |
| 212 | // native args). Includes alignment. |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 213 | virtual size_t FrameSize() = 0; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 214 | // Size of outgoing arguments, including alignment |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 215 | virtual size_t OutArgSize() = 0; |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 216 | // Number of references in stack indirect reference table |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 217 | size_t ReferenceCount() const; |
| 218 | // Location where the segment state of the local indirect reference table is saved |
Ian Rogers | 5a7a74a | 2011-09-26 16:32:29 -0700 | [diff] [blame] | 219 | FrameOffset SavedLocalReferenceCookieOffset() const; |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 220 | // Location where the return value of a call can be squirreled if another |
| 221 | // call is made following the native call |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 222 | FrameOffset ReturnValueSaveLocation() const; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 223 | |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 224 | // Callee save registers to spill prior to native code (which may clobber) |
| 225 | virtual const std::vector<ManagedRegister>& CalleeSaveRegisters() const = 0; |
| 226 | |
| 227 | // Spill mask values |
| 228 | virtual uint32_t CoreSpillMask() const = 0; |
| 229 | virtual uint32_t FpSpillMask() const = 0; |
Ian Rogers | 0d666d8 | 2011-08-14 16:03:46 -0700 | [diff] [blame] | 230 | |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 231 | // Returns true if the method register will have been clobbered during argument |
| 232 | // set up |
Ian Rogers | ad42e13 | 2011-09-17 20:23:33 -0700 | [diff] [blame] | 233 | virtual bool IsMethodRegisterClobberedPreCall() = 0; |
Carl Shapiro | e2d373e | 2011-07-25 15:20:06 -0700 | [diff] [blame] | 234 | |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 235 | // An extra scratch register live after the call |
| 236 | virtual ManagedRegister ReturnScratchRegister() const = 0; |
| 237 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 238 | // Iterator interface |
| 239 | bool HasNext(); |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 240 | virtual void Next(); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 241 | bool IsCurrentParamAReference(); |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 242 | size_t CurrentParamSize(); |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 243 | virtual bool IsCurrentParamInRegister() = 0; |
| 244 | virtual bool IsCurrentParamOnStack() = 0; |
| 245 | virtual ManagedRegister CurrentParamRegister() = 0; |
| 246 | virtual FrameOffset CurrentParamStackOffset() = 0; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 247 | |
| 248 | // Iterator interface extension for JNI |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 249 | FrameOffset CurrentParamSirtEntryOffset(); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 250 | |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 251 | // Position of SIRT and interior fields |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 252 | FrameOffset SirtOffset() const { |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 253 | return FrameOffset(displacement_.Int32Value() + |
| 254 | kPointerSize); // above Method* |
| 255 | } |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 256 | FrameOffset SirtNumRefsOffset() const { |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 257 | return FrameOffset(SirtOffset().Int32Value() + |
| 258 | StackIndirectReferenceTable::NumberOfReferencesOffset()); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 259 | } |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 260 | FrameOffset SirtLinkOffset() const { |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 261 | return FrameOffset(SirtOffset().Int32Value() + |
| 262 | StackIndirectReferenceTable::LinkOffset()); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 263 | } |
| 264 | |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 265 | virtual ~JniCallingConvention() {} |
| 266 | |
| 267 | protected: |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 268 | // Named iterator positions |
| 269 | enum IteratorPos { |
| 270 | kJniEnv = 0, |
| 271 | kObjectOrClass = 1 |
| 272 | }; |
| 273 | |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 274 | explicit JniCallingConvention(bool is_static, bool is_synchronized, const char* shorty) : |
| 275 | CallingConvention(is_static, is_synchronized, shorty) {} |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 276 | |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 277 | // Number of stack slots for outgoing arguments, above which the SIRT is |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 278 | // located |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 279 | virtual size_t NumberOfOutgoingStackArgs() = 0; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 280 | |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 281 | protected: |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 282 | size_t NumberOfExtraArgumentsForJni(); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 283 | }; |
| 284 | |
| 285 | } // namespace art |
| 286 | |
| 287 | #endif // ART_SRC_CALLING_CONVENTION_H_ |