Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 | */ |
| 16 | |
| 17 | #include "callee_save_frame.h" |
Dragos Sbirlea | bd136a2 | 2013-08-13 18:07:04 -0700 | [diff] [blame] | 18 | #include "common_throws.h" |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 19 | #include "dex_file-inl.h" |
| 20 | #include "dex_instruction-inl.h" |
Dragos Sbirlea | bd136a2 | 2013-08-13 18:07:04 -0700 | [diff] [blame] | 21 | #include "entrypoints/entrypoint_utils.h" |
Ian Rogers | 83883d7 | 2013-10-21 21:07:24 -0700 | [diff] [blame] | 22 | #include "gc/accounting/card_table-inl.h" |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 23 | #include "interpreter/interpreter.h" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 24 | #include "mirror/art_method-inl.h" |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 25 | #include "mirror/class-inl.h" |
Mathieu Chartier | 5f3ded4 | 2014-04-03 15:25:30 -0700 | [diff] [blame] | 26 | #include "mirror/dex_cache-inl.h" |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 27 | #include "mirror/object-inl.h" |
| 28 | #include "mirror/object_array-inl.h" |
| 29 | #include "object_utils.h" |
| 30 | #include "runtime.h" |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 31 | #include "scoped_thread_state_change.h" |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 32 | |
| 33 | namespace art { |
| 34 | |
| 35 | // Visits the arguments as saved to the stack by a Runtime::kRefAndArgs callee save frame. |
| 36 | class QuickArgumentVisitor { |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 37 | // Number of bytes for each out register in the caller method's frame. |
| 38 | static constexpr size_t kBytesStackArgLocation = 4; |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 39 | #if defined(__arm__) |
| 40 | // The callee save frame is pointed to by SP. |
| 41 | // | argN | | |
| 42 | // | ... | | |
| 43 | // | arg4 | | |
| 44 | // | arg3 spill | | Caller's frame |
| 45 | // | arg2 spill | | |
| 46 | // | arg1 spill | | |
| 47 | // | Method* | --- |
| 48 | // | LR | |
| 49 | // | ... | callee saves |
| 50 | // | R3 | arg3 |
| 51 | // | R2 | arg2 |
| 52 | // | R1 | arg1 |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 53 | // | R0 | padding |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 54 | // | Method* | <- sp |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 55 | static constexpr bool kQuickSoftFloatAbi = true; // This is a soft float ABI. |
| 56 | static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs. |
| 57 | static constexpr size_t kNumQuickFprArgs = 0; // 0 arguments passed in FPRs. |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 58 | static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 0; // Offset of first FPR arg. |
| 59 | static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 8; // Offset of first GPR arg. |
| 60 | static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 44; // Offset of return address. |
| 61 | static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize = 48; // Frame size. |
| 62 | static size_t GprIndexToGprOffset(uint32_t gpr_index) { |
Nicolas Geoffray | 42fcd98 | 2014-04-22 11:03:52 +0000 | [diff] [blame] | 63 | return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA); |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 64 | } |
Stuart Monteith | b95a534 | 2014-03-12 13:32:32 +0000 | [diff] [blame] | 65 | #elif defined(__aarch64__) |
| 66 | // The callee save frame is pointed to by SP. |
| 67 | // | argN | | |
| 68 | // | ... | | |
| 69 | // | arg4 | | |
| 70 | // | arg3 spill | | Caller's frame |
| 71 | // | arg2 spill | | |
| 72 | // | arg1 spill | | |
| 73 | // | Method* | --- |
| 74 | // | LR | |
| 75 | // | X28 | |
| 76 | // | : | |
| 77 | // | X19 | |
| 78 | // | X7 | |
| 79 | // | : | |
| 80 | // | X1 | |
| 81 | // | D15 | |
| 82 | // | : | |
| 83 | // | D0 | |
| 84 | // | | padding |
| 85 | // | Method* | <- sp |
| 86 | static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI. |
| 87 | static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs. |
| 88 | static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs. |
Stuart Monteith | b95a534 | 2014-03-12 13:32:32 +0000 | [diff] [blame] | 89 | static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset =16; // Offset of first FPR arg. |
| 90 | static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 144; // Offset of first GPR arg. |
| 91 | static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 296; // Offset of return address. |
| 92 | static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize = 304; // Frame size. |
| 93 | static size_t GprIndexToGprOffset(uint32_t gpr_index) { |
Nicolas Geoffray | 42fcd98 | 2014-04-22 11:03:52 +0000 | [diff] [blame] | 94 | return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA); |
Stuart Monteith | b95a534 | 2014-03-12 13:32:32 +0000 | [diff] [blame] | 95 | } |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 96 | #elif defined(__mips__) |
| 97 | // The callee save frame is pointed to by SP. |
| 98 | // | argN | | |
| 99 | // | ... | | |
| 100 | // | arg4 | | |
| 101 | // | arg3 spill | | Caller's frame |
| 102 | // | arg2 spill | | |
| 103 | // | arg1 spill | | |
| 104 | // | Method* | --- |
| 105 | // | RA | |
| 106 | // | ... | callee saves |
| 107 | // | A3 | arg3 |
| 108 | // | A2 | arg2 |
| 109 | // | A1 | arg1 |
| 110 | // | A0/Method* | <- sp |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 111 | static constexpr bool kQuickSoftFloatAbi = true; // This is a soft float ABI. |
| 112 | static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs. |
| 113 | static constexpr size_t kNumQuickFprArgs = 0; // 0 arguments passed in FPRs. |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 114 | static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 0; // Offset of first FPR arg. |
| 115 | static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 4; // Offset of first GPR arg. |
| 116 | static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 60; // Offset of return address. |
| 117 | static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize = 64; // Frame size. |
| 118 | static size_t GprIndexToGprOffset(uint32_t gpr_index) { |
Nicolas Geoffray | 42fcd98 | 2014-04-22 11:03:52 +0000 | [diff] [blame] | 119 | return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA); |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 120 | } |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 121 | #elif defined(__i386__) |
| 122 | // The callee save frame is pointed to by SP. |
| 123 | // | argN | | |
| 124 | // | ... | | |
| 125 | // | arg4 | | |
| 126 | // | arg3 spill | | Caller's frame |
| 127 | // | arg2 spill | | |
| 128 | // | arg1 spill | | |
| 129 | // | Method* | --- |
| 130 | // | Return | |
| 131 | // | EBP,ESI,EDI | callee saves |
| 132 | // | EBX | arg3 |
| 133 | // | EDX | arg2 |
| 134 | // | ECX | arg1 |
| 135 | // | EAX/Method* | <- sp |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 136 | static constexpr bool kQuickSoftFloatAbi = true; // This is a soft float ABI. |
| 137 | static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs. |
| 138 | static constexpr size_t kNumQuickFprArgs = 0; // 0 arguments passed in FPRs. |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 139 | static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 0; // Offset of first FPR arg. |
| 140 | static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 4; // Offset of first GPR arg. |
| 141 | static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 28; // Offset of return address. |
| 142 | static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize = 32; // Frame size. |
| 143 | static size_t GprIndexToGprOffset(uint32_t gpr_index) { |
Nicolas Geoffray | 42fcd98 | 2014-04-22 11:03:52 +0000 | [diff] [blame] | 144 | return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA); |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 145 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 146 | #elif defined(__x86_64__) |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 147 | // The callee save frame is pointed to by SP. |
| 148 | // | argN | | |
| 149 | // | ... | | |
| 150 | // | reg. arg spills | | Caller's frame |
| 151 | // | Method* | --- |
| 152 | // | Return | |
| 153 | // | R15 | callee save |
| 154 | // | R14 | callee save |
| 155 | // | R13 | callee save |
| 156 | // | R12 | callee save |
| 157 | // | R9 | arg5 |
| 158 | // | R8 | arg4 |
| 159 | // | RSI/R6 | arg1 |
| 160 | // | RBP/R5 | callee save |
| 161 | // | RBX/R3 | callee save |
| 162 | // | RDX/R2 | arg2 |
| 163 | // | RCX/R1 | arg3 |
| 164 | // | XMM7 | float arg 8 |
| 165 | // | XMM6 | float arg 7 |
| 166 | // | XMM5 | float arg 6 |
| 167 | // | XMM4 | float arg 5 |
| 168 | // | XMM3 | float arg 4 |
| 169 | // | XMM2 | float arg 3 |
| 170 | // | XMM1 | float arg 2 |
| 171 | // | XMM0 | float arg 1 |
| 172 | // | Padding | |
| 173 | // | RDI/Method* | <- sp |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 174 | static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI. |
Dmitry Petrochenko | 58994cd | 2014-05-17 01:02:18 +0700 | [diff] [blame] | 175 | #ifdef TARGET_REX_SUPPORT |
| 176 | static constexpr size_t kNumQuickGprArgs = 5; // 5 arguments passed in GPRs. |
| 177 | #else |
| 178 | static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs if r8..r15 not enabled. |
| 179 | #endif |
| 180 | static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs. |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 181 | static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 16; // Offset of first FPR arg. |
| 182 | static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 80; // Offset of first GPR arg. |
| 183 | static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 168; // Offset of return address. |
| 184 | static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize = 176; // Frame size. |
| 185 | static size_t GprIndexToGprOffset(uint32_t gpr_index) { |
| 186 | switch (gpr_index) { |
Nicolas Geoffray | 42fcd98 | 2014-04-22 11:03:52 +0000 | [diff] [blame] | 187 | case 0: return (4 * GetBytesPerGprSpillLocation(kRuntimeISA)); |
| 188 | case 1: return (1 * GetBytesPerGprSpillLocation(kRuntimeISA)); |
| 189 | case 2: return (0 * GetBytesPerGprSpillLocation(kRuntimeISA)); |
| 190 | case 3: return (5 * GetBytesPerGprSpillLocation(kRuntimeISA)); |
| 191 | case 4: return (6 * GetBytesPerGprSpillLocation(kRuntimeISA)); |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 192 | default: |
| 193 | LOG(FATAL) << "Unexpected GPR index: " << gpr_index; |
| 194 | return 0; |
| 195 | } |
| 196 | } |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 197 | #else |
| 198 | #error "Unsupported architecture" |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 199 | #endif |
| 200 | |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 201 | public: |
Andreas Gampe | cf4035a | 2014-05-28 22:43:01 -0700 | [diff] [blame] | 202 | static mirror::ArtMethod* GetCallingMethod(StackReference<mirror::ArtMethod>* sp) |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 203 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Andreas Gampe | cf4035a | 2014-05-28 22:43:01 -0700 | [diff] [blame] | 204 | DCHECK(sp->AsMirrorPtr()->IsCalleeSaveMethod()); |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 205 | byte* previous_sp = reinterpret_cast<byte*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize; |
Andreas Gampe | cf4035a | 2014-05-28 22:43:01 -0700 | [diff] [blame] | 206 | return reinterpret_cast<StackReference<mirror::ArtMethod>*>(previous_sp)->AsMirrorPtr(); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 209 | // For the given quick ref and args quick frame, return the caller's PC. |
Andreas Gampe | cf4035a | 2014-05-28 22:43:01 -0700 | [diff] [blame] | 210 | static uintptr_t GetCallingPc(StackReference<mirror::ArtMethod>* sp) |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 211 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Andreas Gampe | cf4035a | 2014-05-28 22:43:01 -0700 | [diff] [blame] | 212 | DCHECK(sp->AsMirrorPtr()->IsCalleeSaveMethod()); |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 213 | byte* lr = reinterpret_cast<byte*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_LrOffset; |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 214 | return *reinterpret_cast<uintptr_t*>(lr); |
| 215 | } |
| 216 | |
Andreas Gampe | cf4035a | 2014-05-28 22:43:01 -0700 | [diff] [blame] | 217 | QuickArgumentVisitor(StackReference<mirror::ArtMethod>* sp, bool is_static, |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 218 | const char* shorty, uint32_t shorty_len) |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 219 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) : |
| 220 | is_static_(is_static), shorty_(shorty), shorty_len_(shorty_len), |
| 221 | gpr_args_(reinterpret_cast<byte*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset), |
| 222 | fpr_args_(reinterpret_cast<byte*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset), |
| 223 | stack_args_(reinterpret_cast<byte*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize |
| 224 | + StackArgumentStartFromShorty(is_static, shorty, shorty_len)), |
| 225 | gpr_index_(0), fpr_index_(0), stack_index_(0), cur_type_(Primitive::kPrimVoid), |
| 226 | is_split_long_or_double_(false) { |
| 227 | DCHECK_EQ(kQuickCalleeSaveFrame_RefAndArgs_FrameSize, |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 228 | Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes()); |
| 229 | } |
| 230 | |
| 231 | virtual ~QuickArgumentVisitor() {} |
| 232 | |
| 233 | virtual void Visit() = 0; |
| 234 | |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 235 | Primitive::Type GetParamPrimitiveType() const { |
| 236 | return cur_type_; |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | byte* GetParamAddress() const { |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 240 | if (!kQuickSoftFloatAbi) { |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 241 | Primitive::Type type = GetParamPrimitiveType(); |
| 242 | if (UNLIKELY((type == Primitive::kPrimDouble) || (type == Primitive::kPrimFloat))) { |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 243 | if ((kNumQuickFprArgs != 0) && (fpr_index_ + 1 < kNumQuickFprArgs + 1)) { |
Nicolas Geoffray | 42fcd98 | 2014-04-22 11:03:52 +0000 | [diff] [blame] | 244 | return fpr_args_ + (fpr_index_ * GetBytesPerFprSpillLocation(kRuntimeISA)); |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 245 | } |
Vladimir Kostyukov | 1dd61ba | 2014-04-02 18:42:20 +0700 | [diff] [blame] | 246 | return stack_args_ + (stack_index_ * kBytesStackArgLocation); |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 247 | } |
| 248 | } |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 249 | if (gpr_index_ < kNumQuickGprArgs) { |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 250 | return gpr_args_ + GprIndexToGprOffset(gpr_index_); |
| 251 | } |
| 252 | return stack_args_ + (stack_index_ * kBytesStackArgLocation); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | bool IsSplitLongOrDouble() const { |
Nicolas Geoffray | 42fcd98 | 2014-04-22 11:03:52 +0000 | [diff] [blame] | 256 | if ((GetBytesPerGprSpillLocation(kRuntimeISA) == 4) || (GetBytesPerFprSpillLocation(kRuntimeISA) == 4)) { |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 257 | return is_split_long_or_double_; |
| 258 | } else { |
| 259 | return false; // An optimization for when GPR and FPRs are 64bit. |
| 260 | } |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 263 | bool IsParamAReference() const { |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 264 | return GetParamPrimitiveType() == Primitive::kPrimNot; |
| 265 | } |
| 266 | |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 267 | bool IsParamALongOrDouble() const { |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 268 | Primitive::Type type = GetParamPrimitiveType(); |
| 269 | return type == Primitive::kPrimLong || type == Primitive::kPrimDouble; |
| 270 | } |
| 271 | |
| 272 | uint64_t ReadSplitLongParam() const { |
| 273 | DCHECK(IsSplitLongOrDouble()); |
| 274 | uint64_t low_half = *reinterpret_cast<uint32_t*>(GetParamAddress()); |
| 275 | uint64_t high_half = *reinterpret_cast<uint32_t*>(stack_args_); |
| 276 | return (low_half & 0xffffffffULL) | (high_half << 32); |
| 277 | } |
| 278 | |
| 279 | void VisitArguments() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Vladimir Kostyukov | 1dd61ba | 2014-04-02 18:42:20 +0700 | [diff] [blame] | 280 | // This implementation doesn't support reg-spill area for hard float |
| 281 | // ABI targets such as x86_64 and aarch64. So, for those targets whose |
| 282 | // 'kQuickSoftFloatAbi' is 'false': |
| 283 | // (a) 'stack_args_' should point to the first method's argument |
| 284 | // (b) whatever the argument type it is, the 'stack_index_' should |
| 285 | // be moved forward along with every visiting. |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 286 | gpr_index_ = 0; |
| 287 | fpr_index_ = 0; |
| 288 | stack_index_ = 0; |
| 289 | if (!is_static_) { // Handle this. |
| 290 | cur_type_ = Primitive::kPrimNot; |
| 291 | is_split_long_or_double_ = false; |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 292 | Visit(); |
Vladimir Kostyukov | 1dd61ba | 2014-04-02 18:42:20 +0700 | [diff] [blame] | 293 | if (!kQuickSoftFloatAbi || kNumQuickGprArgs == 0) { |
| 294 | stack_index_++; |
| 295 | } |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 296 | if (kNumQuickGprArgs > 0) { |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 297 | gpr_index_++; |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 298 | } |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 299 | } |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 300 | for (uint32_t shorty_index = 1; shorty_index < shorty_len_; ++shorty_index) { |
| 301 | cur_type_ = Primitive::GetType(shorty_[shorty_index]); |
| 302 | switch (cur_type_) { |
| 303 | case Primitive::kPrimNot: |
| 304 | case Primitive::kPrimBoolean: |
| 305 | case Primitive::kPrimByte: |
| 306 | case Primitive::kPrimChar: |
| 307 | case Primitive::kPrimShort: |
| 308 | case Primitive::kPrimInt: |
| 309 | is_split_long_or_double_ = false; |
| 310 | Visit(); |
Vladimir Kostyukov | 1dd61ba | 2014-04-02 18:42:20 +0700 | [diff] [blame] | 311 | if (!kQuickSoftFloatAbi || kNumQuickGprArgs == gpr_index_) { |
| 312 | stack_index_++; |
| 313 | } |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 314 | if (gpr_index_ < kNumQuickGprArgs) { |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 315 | gpr_index_++; |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 316 | } |
| 317 | break; |
| 318 | case Primitive::kPrimFloat: |
| 319 | is_split_long_or_double_ = false; |
| 320 | Visit(); |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 321 | if (kQuickSoftFloatAbi) { |
| 322 | if (gpr_index_ < kNumQuickGprArgs) { |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 323 | gpr_index_++; |
| 324 | } else { |
| 325 | stack_index_++; |
| 326 | } |
| 327 | } else { |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 328 | if ((kNumQuickFprArgs != 0) && (fpr_index_ + 1 < kNumQuickFprArgs + 1)) { |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 329 | fpr_index_++; |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 330 | } |
Vladimir Kostyukov | 1dd61ba | 2014-04-02 18:42:20 +0700 | [diff] [blame] | 331 | stack_index_++; |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 332 | } |
| 333 | break; |
| 334 | case Primitive::kPrimDouble: |
| 335 | case Primitive::kPrimLong: |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 336 | if (kQuickSoftFloatAbi || (cur_type_ == Primitive::kPrimLong)) { |
Nicolas Geoffray | 42fcd98 | 2014-04-22 11:03:52 +0000 | [diff] [blame] | 337 | is_split_long_or_double_ = (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) && |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 338 | ((gpr_index_ + 1) == kNumQuickGprArgs); |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 339 | Visit(); |
Vladimir Kostyukov | 1dd61ba | 2014-04-02 18:42:20 +0700 | [diff] [blame] | 340 | if (!kQuickSoftFloatAbi || kNumQuickGprArgs == gpr_index_) { |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 341 | if (kBytesStackArgLocation == 4) { |
| 342 | stack_index_+= 2; |
| 343 | } else { |
| 344 | CHECK_EQ(kBytesStackArgLocation, 8U); |
| 345 | stack_index_++; |
| 346 | } |
| 347 | } |
Vladimir Kostyukov | 1dd61ba | 2014-04-02 18:42:20 +0700 | [diff] [blame] | 348 | if (gpr_index_ < kNumQuickGprArgs) { |
| 349 | gpr_index_++; |
Nicolas Geoffray | 42fcd98 | 2014-04-22 11:03:52 +0000 | [diff] [blame] | 350 | if (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) { |
Vladimir Kostyukov | 1dd61ba | 2014-04-02 18:42:20 +0700 | [diff] [blame] | 351 | if (gpr_index_ < kNumQuickGprArgs) { |
| 352 | gpr_index_++; |
| 353 | } else if (kQuickSoftFloatAbi) { |
| 354 | stack_index_++; |
| 355 | } |
| 356 | } |
| 357 | } |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 358 | } else { |
Nicolas Geoffray | 42fcd98 | 2014-04-22 11:03:52 +0000 | [diff] [blame] | 359 | is_split_long_or_double_ = (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) && |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 360 | ((fpr_index_ + 1) == kNumQuickFprArgs); |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 361 | Visit(); |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 362 | if ((kNumQuickFprArgs != 0) && (fpr_index_ + 1 < kNumQuickFprArgs + 1)) { |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 363 | fpr_index_++; |
Nicolas Geoffray | 42fcd98 | 2014-04-22 11:03:52 +0000 | [diff] [blame] | 364 | if (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) { |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 365 | if ((kNumQuickFprArgs != 0) && (fpr_index_ + 1 < kNumQuickFprArgs + 1)) { |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 366 | fpr_index_++; |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 367 | } |
| 368 | } |
Vladimir Kostyukov | 1dd61ba | 2014-04-02 18:42:20 +0700 | [diff] [blame] | 369 | } |
| 370 | if (kBytesStackArgLocation == 4) { |
| 371 | stack_index_+= 2; |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 372 | } else { |
Vladimir Kostyukov | 1dd61ba | 2014-04-02 18:42:20 +0700 | [diff] [blame] | 373 | CHECK_EQ(kBytesStackArgLocation, 8U); |
| 374 | stack_index_++; |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 375 | } |
| 376 | } |
| 377 | break; |
| 378 | default: |
| 379 | LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty_; |
| 380 | } |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 381 | } |
| 382 | } |
| 383 | |
| 384 | private: |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 385 | static size_t StackArgumentStartFromShorty(bool is_static, const char* shorty, |
| 386 | uint32_t shorty_len) { |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 387 | if (kQuickSoftFloatAbi) { |
| 388 | CHECK_EQ(kNumQuickFprArgs, 0U); |
Nicolas Geoffray | 42fcd98 | 2014-04-22 11:03:52 +0000 | [diff] [blame] | 389 | return (kNumQuickGprArgs * GetBytesPerGprSpillLocation(kRuntimeISA)) |
Andreas Gampe | cf4035a | 2014-05-28 22:43:01 -0700 | [diff] [blame] | 390 | + sizeof(StackReference<mirror::ArtMethod>) /* StackReference<ArtMethod> */; |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 391 | } else { |
Vladimir Kostyukov | 1dd61ba | 2014-04-02 18:42:20 +0700 | [diff] [blame] | 392 | // For now, there is no reg-spill area for the targets with |
| 393 | // hard float ABI. So, the offset pointing to the first method's |
| 394 | // parameter ('this' for non-static methods) should be returned. |
Andreas Gampe | cf4035a | 2014-05-28 22:43:01 -0700 | [diff] [blame] | 395 | return sizeof(StackReference<mirror::ArtMethod>); // Skip StackReference<ArtMethod>. |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 396 | } |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | const bool is_static_; |
| 400 | const char* const shorty_; |
| 401 | const uint32_t shorty_len_; |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 402 | byte* const gpr_args_; // Address of GPR arguments in callee save frame. |
| 403 | byte* const fpr_args_; // Address of FPR arguments in callee save frame. |
| 404 | byte* const stack_args_; // Address of stack arguments in caller's frame. |
| 405 | uint32_t gpr_index_; // Index into spilled GPRs. |
| 406 | uint32_t fpr_index_; // Index into spilled FPRs. |
| 407 | uint32_t stack_index_; // Index into arguments on the stack. |
| 408 | // The current type of argument during VisitArguments. |
| 409 | Primitive::Type cur_type_; |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 410 | // Does a 64bit parameter straddle the register and stack arguments? |
| 411 | bool is_split_long_or_double_; |
| 412 | }; |
| 413 | |
| 414 | // Visits arguments on the stack placing them into the shadow frame. |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 415 | class BuildQuickShadowFrameVisitor FINAL : public QuickArgumentVisitor { |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 416 | public: |
Andreas Gampe | cf4035a | 2014-05-28 22:43:01 -0700 | [diff] [blame] | 417 | BuildQuickShadowFrameVisitor(StackReference<mirror::ArtMethod>* sp, bool is_static, |
| 418 | const char* shorty, uint32_t shorty_len, ShadowFrame* sf, |
| 419 | size_t first_arg_reg) : |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 420 | QuickArgumentVisitor(sp, is_static, shorty, shorty_len), sf_(sf), cur_reg_(first_arg_reg) {} |
| 421 | |
Ian Rogers | 9758f79 | 2014-03-13 09:02:55 -0700 | [diff] [blame] | 422 | void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE; |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 423 | |
| 424 | private: |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 425 | ShadowFrame* const sf_; |
| 426 | uint32_t cur_reg_; |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 427 | |
Dragos Sbirlea | bd136a2 | 2013-08-13 18:07:04 -0700 | [diff] [blame] | 428 | DISALLOW_COPY_AND_ASSIGN(BuildQuickShadowFrameVisitor); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 429 | }; |
| 430 | |
Ian Rogers | 9758f79 | 2014-03-13 09:02:55 -0700 | [diff] [blame] | 431 | void BuildQuickShadowFrameVisitor::Visit() { |
| 432 | Primitive::Type type = GetParamPrimitiveType(); |
| 433 | switch (type) { |
| 434 | case Primitive::kPrimLong: // Fall-through. |
| 435 | case Primitive::kPrimDouble: |
| 436 | if (IsSplitLongOrDouble()) { |
| 437 | sf_->SetVRegLong(cur_reg_, ReadSplitLongParam()); |
| 438 | } else { |
| 439 | sf_->SetVRegLong(cur_reg_, *reinterpret_cast<jlong*>(GetParamAddress())); |
| 440 | } |
| 441 | ++cur_reg_; |
| 442 | break; |
| 443 | case Primitive::kPrimNot: { |
| 444 | StackReference<mirror::Object>* stack_ref = |
| 445 | reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress()); |
| 446 | sf_->SetVRegReference(cur_reg_, stack_ref->AsMirrorPtr()); |
| 447 | } |
| 448 | break; |
| 449 | case Primitive::kPrimBoolean: // Fall-through. |
| 450 | case Primitive::kPrimByte: // Fall-through. |
| 451 | case Primitive::kPrimChar: // Fall-through. |
| 452 | case Primitive::kPrimShort: // Fall-through. |
| 453 | case Primitive::kPrimInt: // Fall-through. |
| 454 | case Primitive::kPrimFloat: |
| 455 | sf_->SetVReg(cur_reg_, *reinterpret_cast<jint*>(GetParamAddress())); |
| 456 | break; |
| 457 | case Primitive::kPrimVoid: |
| 458 | LOG(FATAL) << "UNREACHABLE"; |
| 459 | break; |
| 460 | } |
| 461 | ++cur_reg_; |
| 462 | } |
| 463 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 464 | extern "C" uint64_t artQuickToInterpreterBridge(mirror::ArtMethod* method, Thread* self, |
Andreas Gampe | cf4035a | 2014-05-28 22:43:01 -0700 | [diff] [blame] | 465 | StackReference<mirror::ArtMethod>* sp) |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 466 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 467 | // Ensure we don't get thread suspension until the object arguments are safely in the shadow |
| 468 | // frame. |
| 469 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs); |
| 470 | |
| 471 | if (method->IsAbstract()) { |
| 472 | ThrowAbstractMethodError(method); |
| 473 | return 0; |
| 474 | } else { |
Brian Carlstrom | 2ec6520 | 2014-03-03 15:16:37 -0800 | [diff] [blame] | 475 | DCHECK(!method->IsNative()) << PrettyMethod(method); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 476 | const char* old_cause = self->StartAssertNoThreadSuspension("Building interpreter shadow frame"); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame^] | 477 | const DexFile::CodeItem* code_item = method->GetCodeItem(); |
Brian Carlstrom | 2ec6520 | 2014-03-03 15:16:37 -0800 | [diff] [blame] | 478 | DCHECK(code_item != nullptr) << PrettyMethod(method); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 479 | uint16_t num_regs = code_item->registers_size_; |
| 480 | void* memory = alloca(ShadowFrame::ComputeSize(num_regs)); |
| 481 | ShadowFrame* shadow_frame(ShadowFrame::Create(num_regs, NULL, // No last shadow coming from quick. |
| 482 | method, 0, memory)); |
| 483 | size_t first_arg_reg = code_item->registers_size_ - code_item->ins_size_; |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame^] | 484 | uint32_t shorty_len = 0; |
| 485 | const char* shorty = method->GetShorty(&shorty_len); |
| 486 | BuildQuickShadowFrameVisitor shadow_frame_builder(sp, method->IsStatic(), shorty, shorty_len, |
Ian Rogers | 936b37f | 2014-02-14 00:52:24 -0800 | [diff] [blame] | 487 | shadow_frame, first_arg_reg); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 488 | shadow_frame_builder.VisitArguments(); |
| 489 | // Push a transition back into managed code onto the linked list in thread. |
| 490 | ManagedStack fragment; |
| 491 | self->PushManagedStackFragment(&fragment); |
| 492 | self->PushShadowFrame(shadow_frame); |
| 493 | self->EndAssertNoThreadSuspension(old_cause); |
| 494 | |
| 495 | if (method->IsStatic() && !method->GetDeclaringClass()->IsInitializing()) { |
| 496 | // Ensure static method's class is initialized. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 497 | StackHandleScope<1> hs(self); |
| 498 | Handle<mirror::Class> h_class(hs.NewHandle(method->GetDeclaringClass())); |
| 499 | if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(h_class, true, true)) { |
Brian Carlstrom | 2ec6520 | 2014-03-03 15:16:37 -0800 | [diff] [blame] | 500 | DCHECK(Thread::Current()->IsExceptionPending()) << PrettyMethod(method); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 501 | self->PopManagedStackFragment(fragment); |
| 502 | return 0; |
| 503 | } |
| 504 | } |
| 505 | |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame^] | 506 | StackHandleScope<1> hs(self); |
| 507 | MethodHelper mh(hs.NewHandle(method)); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 508 | JValue result = interpreter::EnterInterpreterFromStub(self, mh, code_item, *shadow_frame); |
| 509 | // Pop transition. |
| 510 | self->PopManagedStackFragment(fragment); |
Mathieu Chartier | 5275bcb | 2014-02-20 17:16:42 -0800 | [diff] [blame] | 511 | // No need to restore the args since the method has already been run by the interpreter. |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 512 | return result.GetJ(); |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | // Visits arguments on the stack placing them into the args vector, Object* arguments are converted |
| 517 | // to jobjects. |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 518 | class BuildQuickArgumentVisitor FINAL : public QuickArgumentVisitor { |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 519 | public: |
Andreas Gampe | cf4035a | 2014-05-28 22:43:01 -0700 | [diff] [blame] | 520 | BuildQuickArgumentVisitor(StackReference<mirror::ArtMethod>* sp, bool is_static, |
| 521 | const char* shorty, uint32_t shorty_len, |
| 522 | ScopedObjectAccessUnchecked* soa, std::vector<jvalue>* args) : |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 523 | QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa), args_(args) {} |
| 524 | |
Ian Rogers | 9758f79 | 2014-03-13 09:02:55 -0700 | [diff] [blame] | 525 | void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE; |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 526 | |
Ian Rogers | 9758f79 | 2014-03-13 09:02:55 -0700 | [diff] [blame] | 527 | void FixupReferences() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Mathieu Chartier | 5275bcb | 2014-02-20 17:16:42 -0800 | [diff] [blame] | 528 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 529 | private: |
Ian Rogers | 9758f79 | 2014-03-13 09:02:55 -0700 | [diff] [blame] | 530 | ScopedObjectAccessUnchecked* const soa_; |
| 531 | std::vector<jvalue>* const args_; |
Mathieu Chartier | 5275bcb | 2014-02-20 17:16:42 -0800 | [diff] [blame] | 532 | // References which we must update when exiting in case the GC moved the objects. |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 533 | std::vector<std::pair<jobject, StackReference<mirror::Object>*>> references_; |
Ian Rogers | 9758f79 | 2014-03-13 09:02:55 -0700 | [diff] [blame] | 534 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 535 | DISALLOW_COPY_AND_ASSIGN(BuildQuickArgumentVisitor); |
| 536 | }; |
| 537 | |
Ian Rogers | 9758f79 | 2014-03-13 09:02:55 -0700 | [diff] [blame] | 538 | void BuildQuickArgumentVisitor::Visit() { |
| 539 | jvalue val; |
| 540 | Primitive::Type type = GetParamPrimitiveType(); |
| 541 | switch (type) { |
| 542 | case Primitive::kPrimNot: { |
| 543 | StackReference<mirror::Object>* stack_ref = |
| 544 | reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress()); |
| 545 | val.l = soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr()); |
| 546 | references_.push_back(std::make_pair(val.l, stack_ref)); |
| 547 | break; |
| 548 | } |
| 549 | case Primitive::kPrimLong: // Fall-through. |
| 550 | case Primitive::kPrimDouble: |
| 551 | if (IsSplitLongOrDouble()) { |
| 552 | val.j = ReadSplitLongParam(); |
| 553 | } else { |
| 554 | val.j = *reinterpret_cast<jlong*>(GetParamAddress()); |
| 555 | } |
| 556 | break; |
| 557 | case Primitive::kPrimBoolean: // Fall-through. |
| 558 | case Primitive::kPrimByte: // Fall-through. |
| 559 | case Primitive::kPrimChar: // Fall-through. |
| 560 | case Primitive::kPrimShort: // Fall-through. |
| 561 | case Primitive::kPrimInt: // Fall-through. |
| 562 | case Primitive::kPrimFloat: |
| 563 | val.i = *reinterpret_cast<jint*>(GetParamAddress()); |
| 564 | break; |
| 565 | case Primitive::kPrimVoid: |
| 566 | LOG(FATAL) << "UNREACHABLE"; |
| 567 | val.j = 0; |
| 568 | break; |
| 569 | } |
| 570 | args_->push_back(val); |
| 571 | } |
| 572 | |
| 573 | void BuildQuickArgumentVisitor::FixupReferences() { |
| 574 | // Fixup any references which may have changed. |
| 575 | for (const auto& pair : references_) { |
| 576 | pair.second->Assign(soa_->Decode<mirror::Object*>(pair.first)); |
Mathieu Chartier | 5f3ded4 | 2014-04-03 15:25:30 -0700 | [diff] [blame] | 577 | soa_->Env()->DeleteLocalRef(pair.first); |
Ian Rogers | 9758f79 | 2014-03-13 09:02:55 -0700 | [diff] [blame] | 578 | } |
| 579 | } |
| 580 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 581 | // Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method |
| 582 | // which is responsible for recording callee save registers. We explicitly place into jobjects the |
| 583 | // incoming reference arguments (so they survive GC). We invoke the invocation handler, which is a |
| 584 | // field within the proxy object, which will box the primitive arguments and deal with error cases. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 585 | extern "C" uint64_t artQuickProxyInvokeHandler(mirror::ArtMethod* proxy_method, |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 586 | mirror::Object* receiver, |
Andreas Gampe | cf4035a | 2014-05-28 22:43:01 -0700 | [diff] [blame] | 587 | Thread* self, StackReference<mirror::ArtMethod>* sp) |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 588 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Brian Carlstrom | d3633d5 | 2013-08-20 21:06:26 -0700 | [diff] [blame] | 589 | DCHECK(proxy_method->IsProxyMethod()) << PrettyMethod(proxy_method); |
| 590 | DCHECK(receiver->GetClass()->IsProxyClass()) << PrettyMethod(proxy_method); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 591 | // Ensure we don't get thread suspension until the object arguments are safely in jobjects. |
| 592 | const char* old_cause = |
| 593 | self->StartAssertNoThreadSuspension("Adding to IRT proxy object arguments"); |
| 594 | // Register the top of the managed stack, making stack crawlable. |
Andreas Gampe | cf4035a | 2014-05-28 22:43:01 -0700 | [diff] [blame] | 595 | DCHECK_EQ(sp->AsMirrorPtr(), proxy_method) << PrettyMethod(proxy_method); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 596 | self->SetTopOfStack(sp, 0); |
| 597 | DCHECK_EQ(proxy_method->GetFrameSizeInBytes(), |
Brian Carlstrom | d3633d5 | 2013-08-20 21:06:26 -0700 | [diff] [blame] | 598 | Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes()) |
| 599 | << PrettyMethod(proxy_method); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 600 | self->VerifyStack(); |
| 601 | // Start new JNI local reference state. |
| 602 | JNIEnvExt* env = self->GetJniEnv(); |
| 603 | ScopedObjectAccessUnchecked soa(env); |
| 604 | ScopedJniEnvLocalRefState env_state(env); |
| 605 | // Create local ref. copies of proxy method and the receiver. |
| 606 | jobject rcvr_jobj = soa.AddLocalReference<jobject>(receiver); |
| 607 | |
| 608 | // Placing arguments into args vector and remove the receiver. |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame^] | 609 | mirror::ArtMethod* non_proxy_method = proxy_method->GetInterfaceMethodIfProxy(); |
| 610 | CHECK(!non_proxy_method->IsStatic()) << PrettyMethod(proxy_method) << " " |
| 611 | << PrettyMethod(non_proxy_method); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 612 | std::vector<jvalue> args; |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame^] | 613 | uint32_t shorty_len = 0; |
| 614 | const char* shorty = proxy_method->GetShorty(&shorty_len); |
| 615 | BuildQuickArgumentVisitor local_ref_visitor(sp, false, shorty, shorty_len, &soa, &args); |
Brian Carlstrom | d3633d5 | 2013-08-20 21:06:26 -0700 | [diff] [blame] | 616 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 617 | local_ref_visitor.VisitArguments(); |
Brian Carlstrom | d3633d5 | 2013-08-20 21:06:26 -0700 | [diff] [blame] | 618 | DCHECK_GT(args.size(), 0U) << PrettyMethod(proxy_method); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 619 | args.erase(args.begin()); |
| 620 | |
| 621 | // Convert proxy method into expected interface method. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 622 | mirror::ArtMethod* interface_method = proxy_method->FindOverriddenMethod(); |
Brian Carlstrom | d3633d5 | 2013-08-20 21:06:26 -0700 | [diff] [blame] | 623 | DCHECK(interface_method != NULL) << PrettyMethod(proxy_method); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 624 | DCHECK(!interface_method->IsProxyMethod()) << PrettyMethod(interface_method); |
| 625 | jobject interface_method_jobj = soa.AddLocalReference<jobject>(interface_method); |
| 626 | |
| 627 | // All naked Object*s should now be in jobjects, so its safe to go into the main invoke code |
| 628 | // that performs allocations. |
| 629 | self->EndAssertNoThreadSuspension(old_cause); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame^] | 630 | JValue result = InvokeProxyInvocationHandler(soa, shorty, rcvr_jobj, interface_method_jobj, args); |
Mathieu Chartier | 5275bcb | 2014-02-20 17:16:42 -0800 | [diff] [blame] | 631 | // Restore references which might have moved. |
| 632 | local_ref_visitor.FixupReferences(); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 633 | return result.GetJ(); |
| 634 | } |
| 635 | |
| 636 | // Read object references held in arguments from quick frames and place in a JNI local references, |
| 637 | // so they don't get garbage collected. |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 638 | class RememberForGcArgumentVisitor FINAL : public QuickArgumentVisitor { |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 639 | public: |
Andreas Gampe | cf4035a | 2014-05-28 22:43:01 -0700 | [diff] [blame] | 640 | RememberForGcArgumentVisitor(StackReference<mirror::ArtMethod>* sp, bool is_static, |
| 641 | const char* shorty, uint32_t shorty_len, |
| 642 | ScopedObjectAccessUnchecked* soa) : |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 643 | QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa) {} |
| 644 | |
Ian Rogers | 9758f79 | 2014-03-13 09:02:55 -0700 | [diff] [blame] | 645 | void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE; |
Mathieu Chartier | 07d447b | 2013-09-26 11:57:43 -0700 | [diff] [blame] | 646 | |
Ian Rogers | 9758f79 | 2014-03-13 09:02:55 -0700 | [diff] [blame] | 647 | void FixupReferences() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 648 | |
| 649 | private: |
Ian Rogers | 9758f79 | 2014-03-13 09:02:55 -0700 | [diff] [blame] | 650 | ScopedObjectAccessUnchecked* const soa_; |
Mathieu Chartier | 5275bcb | 2014-02-20 17:16:42 -0800 | [diff] [blame] | 651 | // References which we must update when exiting in case the GC moved the objects. |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 652 | std::vector<std::pair<jobject, StackReference<mirror::Object>*>> references_; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 653 | DISALLOW_COPY_AND_ASSIGN(RememberForGcArgumentVisitor); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 654 | }; |
| 655 | |
Ian Rogers | 9758f79 | 2014-03-13 09:02:55 -0700 | [diff] [blame] | 656 | void RememberForGcArgumentVisitor::Visit() { |
| 657 | if (IsParamAReference()) { |
| 658 | StackReference<mirror::Object>* stack_ref = |
| 659 | reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress()); |
| 660 | jobject reference = |
| 661 | soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr()); |
| 662 | references_.push_back(std::make_pair(reference, stack_ref)); |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | void RememberForGcArgumentVisitor::FixupReferences() { |
| 667 | // Fixup any references which may have changed. |
| 668 | for (const auto& pair : references_) { |
| 669 | pair.second->Assign(soa_->Decode<mirror::Object*>(pair.first)); |
Mathieu Chartier | 5f3ded4 | 2014-04-03 15:25:30 -0700 | [diff] [blame] | 670 | soa_->Env()->DeleteLocalRef(pair.first); |
Ian Rogers | 9758f79 | 2014-03-13 09:02:55 -0700 | [diff] [blame] | 671 | } |
| 672 | } |
| 673 | |
| 674 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 675 | // Lazily resolve a method for quick. Called by stub code. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 676 | extern "C" const void* artQuickResolutionTrampoline(mirror::ArtMethod* called, |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 677 | mirror::Object* receiver, |
Andreas Gampe | cf4035a | 2014-05-28 22:43:01 -0700 | [diff] [blame] | 678 | Thread* self, |
| 679 | StackReference<mirror::ArtMethod>* sp) |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 680 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 681 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 682 | // Start new JNI local reference state |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 683 | JNIEnvExt* env = self->GetJniEnv(); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 684 | ScopedObjectAccessUnchecked soa(env); |
| 685 | ScopedJniEnvLocalRefState env_state(env); |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 686 | const char* old_cause = self->StartAssertNoThreadSuspension("Quick method resolution set up"); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 687 | |
| 688 | // Compute details about the called method (avoid GCs) |
| 689 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 690 | mirror::ArtMethod* caller = QuickArgumentVisitor::GetCallingMethod(sp); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 691 | InvokeType invoke_type; |
| 692 | const DexFile* dex_file; |
| 693 | uint32_t dex_method_idx; |
| 694 | if (called->IsRuntimeMethod()) { |
| 695 | uint32_t dex_pc = caller->ToDexPc(QuickArgumentVisitor::GetCallingPc(sp)); |
| 696 | const DexFile::CodeItem* code; |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame^] | 697 | dex_file = caller->GetDexFile(); |
| 698 | code = caller->GetCodeItem(); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 699 | CHECK_LT(dex_pc, code->insns_size_in_code_units_); |
| 700 | const Instruction* instr = Instruction::At(&code->insns_[dex_pc]); |
| 701 | Instruction::Code instr_code = instr->Opcode(); |
| 702 | bool is_range; |
| 703 | switch (instr_code) { |
| 704 | case Instruction::INVOKE_DIRECT: |
| 705 | invoke_type = kDirect; |
| 706 | is_range = false; |
| 707 | break; |
| 708 | case Instruction::INVOKE_DIRECT_RANGE: |
| 709 | invoke_type = kDirect; |
| 710 | is_range = true; |
| 711 | break; |
| 712 | case Instruction::INVOKE_STATIC: |
| 713 | invoke_type = kStatic; |
| 714 | is_range = false; |
| 715 | break; |
| 716 | case Instruction::INVOKE_STATIC_RANGE: |
| 717 | invoke_type = kStatic; |
| 718 | is_range = true; |
| 719 | break; |
| 720 | case Instruction::INVOKE_SUPER: |
| 721 | invoke_type = kSuper; |
| 722 | is_range = false; |
| 723 | break; |
| 724 | case Instruction::INVOKE_SUPER_RANGE: |
| 725 | invoke_type = kSuper; |
| 726 | is_range = true; |
| 727 | break; |
| 728 | case Instruction::INVOKE_VIRTUAL: |
| 729 | invoke_type = kVirtual; |
| 730 | is_range = false; |
| 731 | break; |
| 732 | case Instruction::INVOKE_VIRTUAL_RANGE: |
| 733 | invoke_type = kVirtual; |
| 734 | is_range = true; |
| 735 | break; |
| 736 | case Instruction::INVOKE_INTERFACE: |
| 737 | invoke_type = kInterface; |
| 738 | is_range = false; |
| 739 | break; |
| 740 | case Instruction::INVOKE_INTERFACE_RANGE: |
| 741 | invoke_type = kInterface; |
| 742 | is_range = true; |
| 743 | break; |
| 744 | default: |
| 745 | LOG(FATAL) << "Unexpected call into trampoline: " << instr->DumpString(NULL); |
| 746 | // Avoid used uninitialized warnings. |
| 747 | invoke_type = kDirect; |
| 748 | is_range = false; |
| 749 | } |
| 750 | dex_method_idx = (is_range) ? instr->VRegB_3rc() : instr->VRegB_35c(); |
| 751 | |
| 752 | } else { |
| 753 | invoke_type = kStatic; |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame^] | 754 | dex_file = called->GetDexFile(); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 755 | dex_method_idx = called->GetDexMethodIndex(); |
| 756 | } |
| 757 | uint32_t shorty_len; |
| 758 | const char* shorty = |
| 759 | dex_file->GetMethodShorty(dex_file->GetMethodId(dex_method_idx), &shorty_len); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 760 | RememberForGcArgumentVisitor visitor(sp, invoke_type == kStatic, shorty, shorty_len, &soa); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 761 | visitor.VisitArguments(); |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 762 | self->EndAssertNoThreadSuspension(old_cause); |
Mathieu Chartier | 55871bf | 2014-02-27 10:24:50 -0800 | [diff] [blame] | 763 | bool virtual_or_interface = invoke_type == kVirtual || invoke_type == kInterface; |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 764 | // Resolve method filling in dex cache. |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 765 | if (UNLIKELY(called->IsRuntimeMethod())) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 766 | StackHandleScope<1> hs(self); |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 767 | mirror::Object* dummy = nullptr; |
| 768 | HandleWrapper<mirror::Object> h_receiver( |
| 769 | hs.NewHandleWrapper(virtual_or_interface ? &receiver : &dummy)); |
| 770 | called = linker->ResolveMethod(self, dex_method_idx, &caller, invoke_type); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 771 | } |
| 772 | const void* code = NULL; |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 773 | if (LIKELY(!self->IsExceptionPending())) { |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 774 | // Incompatible class change should have been handled in resolve method. |
Brian Carlstrom | 2ec6520 | 2014-03-03 15:16:37 -0800 | [diff] [blame] | 775 | CHECK(!called->CheckIncompatibleClassChange(invoke_type)) |
| 776 | << PrettyMethod(called) << " " << invoke_type; |
Mathieu Chartier | 55871bf | 2014-02-27 10:24:50 -0800 | [diff] [blame] | 777 | if (virtual_or_interface) { |
| 778 | // Refine called method based on receiver. |
| 779 | CHECK(receiver != nullptr) << invoke_type; |
Mingyao Yang | f486778 | 2014-05-05 11:55:02 -0700 | [diff] [blame] | 780 | |
| 781 | mirror::ArtMethod* orig_called = called; |
Mathieu Chartier | 55871bf | 2014-02-27 10:24:50 -0800 | [diff] [blame] | 782 | if (invoke_type == kVirtual) { |
| 783 | called = receiver->GetClass()->FindVirtualMethodForVirtual(called); |
| 784 | } else { |
| 785 | called = receiver->GetClass()->FindVirtualMethodForInterface(called); |
| 786 | } |
Mingyao Yang | f486778 | 2014-05-05 11:55:02 -0700 | [diff] [blame] | 787 | |
| 788 | CHECK(called != nullptr) << PrettyMethod(orig_called) << " " |
| 789 | << PrettyTypeOf(receiver) << " " |
| 790 | << invoke_type << " " << orig_called->GetVtableIndex(); |
| 791 | |
Ian Rogers | 83883d7 | 2013-10-21 21:07:24 -0700 | [diff] [blame] | 792 | // We came here because of sharpening. Ensure the dex cache is up-to-date on the method index |
| 793 | // of the sharpened method. |
| 794 | if (called->GetDexCacheResolvedMethods() == caller->GetDexCacheResolvedMethods()) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 795 | caller->GetDexCacheResolvedMethods()->Set<false>(called->GetDexMethodIndex(), called); |
Ian Rogers | 83883d7 | 2013-10-21 21:07:24 -0700 | [diff] [blame] | 796 | } else { |
| 797 | // Calling from one dex file to another, need to compute the method index appropriate to |
Vladimir Marko | bbcc0c0 | 2014-02-03 14:08:42 +0000 | [diff] [blame] | 798 | // the caller's dex file. Since we get here only if the original called was a runtime |
| 799 | // method, we've got the correct dex_file and a dex_method_idx from above. |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame^] | 800 | DCHECK_EQ(caller->GetDexFile(), dex_file); |
| 801 | StackHandleScope<1> hs(self); |
| 802 | MethodHelper mh(hs.NewHandle(called)); |
| 803 | uint32_t method_index = mh.FindDexMethodIndexInOtherDexFile(*dex_file, dex_method_idx); |
Ian Rogers | 83883d7 | 2013-10-21 21:07:24 -0700 | [diff] [blame] | 804 | if (method_index != DexFile::kDexNoIndex) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 805 | caller->GetDexCacheResolvedMethods()->Set<false>(method_index, called); |
Ian Rogers | 83883d7 | 2013-10-21 21:07:24 -0700 | [diff] [blame] | 806 | } |
| 807 | } |
| 808 | } |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 809 | // Ensure that the called method's class is initialized. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 810 | StackHandleScope<1> hs(soa.Self()); |
| 811 | Handle<mirror::Class> called_class(hs.NewHandle(called->GetDeclaringClass())); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 812 | linker->EnsureInitialized(called_class, true, true); |
| 813 | if (LIKELY(called_class->IsInitialized())) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 814 | code = called->GetEntryPointFromQuickCompiledCode(); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 815 | } else if (called_class->IsInitializing()) { |
| 816 | if (invoke_type == kStatic) { |
| 817 | // Class is still initializing, go to oat and grab code (trampoline must be left in place |
| 818 | // until class is initialized to stop races between threads). |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 819 | code = linker->GetQuickOatCodeFor(called); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 820 | } else { |
| 821 | // No trampoline for non-static methods. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 822 | code = called->GetEntryPointFromQuickCompiledCode(); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 823 | } |
| 824 | } else { |
| 825 | DCHECK(called_class->IsErroneous()); |
| 826 | } |
| 827 | } |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 828 | CHECK_EQ(code == NULL, self->IsExceptionPending()); |
Mathieu Chartier | 07d447b | 2013-09-26 11:57:43 -0700 | [diff] [blame] | 829 | // Fixup any locally saved objects may have moved during a GC. |
| 830 | visitor.FixupReferences(); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 831 | // Place called method in callee-save frame to be placed as first argument to quick method. |
Andreas Gampe | cf4035a | 2014-05-28 22:43:01 -0700 | [diff] [blame] | 832 | sp->Assign(called); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 833 | return code; |
| 834 | } |
| 835 | |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 836 | |
| 837 | |
| 838 | /* |
| 839 | * This class uses a couple of observations to unite the different calling conventions through |
| 840 | * a few constants. |
| 841 | * |
| 842 | * 1) Number of registers used for passing is normally even, so counting down has no penalty for |
| 843 | * possible alignment. |
| 844 | * 2) Known 64b architectures store 8B units on the stack, both for integral and floating point |
| 845 | * types, so using uintptr_t is OK. Also means that we can use kRegistersNeededX to denote |
| 846 | * when we have to split things |
| 847 | * 3) The only soft-float, Arm, is 32b, so no widening needs to be taken into account for floats |
| 848 | * and we can use Int handling directly. |
| 849 | * 4) Only 64b architectures widen, and their stack is aligned 8B anyways, so no padding code |
| 850 | * necessary when widening. Also, widening of Ints will take place implicitly, and the |
| 851 | * extension should be compatible with Aarch64, which mandates copying the available bits |
| 852 | * into LSB and leaving the rest unspecified. |
| 853 | * 5) Aligning longs and doubles is necessary on arm only, and it's the same in registers and on |
| 854 | * the stack. |
| 855 | * 6) There is only little endian. |
| 856 | * |
| 857 | * |
| 858 | * Actual work is supposed to be done in a delegate of the template type. The interface is as |
| 859 | * follows: |
| 860 | * |
| 861 | * void PushGpr(uintptr_t): Add a value for the next GPR |
| 862 | * |
| 863 | * void PushFpr4(float): Add a value for the next FPR of size 32b. Is only called if we need |
| 864 | * padding, that is, think the architecture is 32b and aligns 64b. |
| 865 | * |
| 866 | * void PushFpr8(uint64_t): Push a double. We _will_ call this on 32b, it's the callee's job to |
| 867 | * split this if necessary. The current state will have aligned, if |
| 868 | * necessary. |
| 869 | * |
| 870 | * void PushStack(uintptr_t): Push a value to the stack. |
| 871 | * |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 872 | * uintptr_t PushHandleScope(mirror::Object* ref): Add a reference to the HandleScope. This _will_ have nullptr, |
Andreas Gampe | 36fea8d | 2014-03-10 13:37:40 -0700 | [diff] [blame] | 873 | * as this might be important for null initialization. |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 874 | * Must return the jobject, that is, the reference to the |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 875 | * entry in the HandleScope (nullptr if necessary). |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 876 | * |
| 877 | */ |
| 878 | template <class T> class BuildGenericJniFrameStateMachine { |
| 879 | public: |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 880 | #if defined(__arm__) |
| 881 | // TODO: These are all dummy values! |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 882 | static constexpr bool kNativeSoftFloatAbi = true; |
| 883 | static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs, r0-r3 |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 884 | static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs. |
| 885 | |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 886 | static constexpr size_t kRegistersNeededForLong = 2; |
| 887 | static constexpr size_t kRegistersNeededForDouble = 2; |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 888 | static constexpr bool kMultiRegistersAligned = true; |
| 889 | static constexpr bool kMultiRegistersWidened = false; |
| 890 | static constexpr bool kAlignLongOnStack = true; |
| 891 | static constexpr bool kAlignDoubleOnStack = true; |
Stuart Monteith | b95a534 | 2014-03-12 13:32:32 +0000 | [diff] [blame] | 892 | #elif defined(__aarch64__) |
| 893 | static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI. |
| 894 | static constexpr size_t kNumNativeGprArgs = 8; // 6 arguments passed in GPRs. |
| 895 | static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs. |
| 896 | |
| 897 | static constexpr size_t kRegistersNeededForLong = 1; |
| 898 | static constexpr size_t kRegistersNeededForDouble = 1; |
| 899 | static constexpr bool kMultiRegistersAligned = false; |
| 900 | static constexpr bool kMultiRegistersWidened = false; |
| 901 | static constexpr bool kAlignLongOnStack = false; |
| 902 | static constexpr bool kAlignDoubleOnStack = false; |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 903 | #elif defined(__mips__) |
| 904 | // TODO: These are all dummy values! |
| 905 | static constexpr bool kNativeSoftFloatAbi = true; // This is a hard float ABI. |
| 906 | static constexpr size_t kNumNativeGprArgs = 0; // 6 arguments passed in GPRs. |
| 907 | static constexpr size_t kNumNativeFprArgs = 0; // 8 arguments passed in FPRs. |
| 908 | |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 909 | static constexpr size_t kRegistersNeededForLong = 2; |
| 910 | static constexpr size_t kRegistersNeededForDouble = 2; |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 911 | static constexpr bool kMultiRegistersAligned = true; |
| 912 | static constexpr bool kMultiRegistersWidened = true; |
| 913 | static constexpr bool kAlignLongOnStack = false; |
| 914 | static constexpr bool kAlignDoubleOnStack = false; |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 915 | #elif defined(__i386__) |
| 916 | // TODO: Check these! |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 917 | static constexpr bool kNativeSoftFloatAbi = false; // Not using int registers for fp |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 918 | static constexpr size_t kNumNativeGprArgs = 0; // 6 arguments passed in GPRs. |
| 919 | static constexpr size_t kNumNativeFprArgs = 0; // 8 arguments passed in FPRs. |
| 920 | |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 921 | static constexpr size_t kRegistersNeededForLong = 2; |
| 922 | static constexpr size_t kRegistersNeededForDouble = 2; |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 923 | static constexpr bool kMultiRegistersAligned = false; // x86 not using regs, anyways |
| 924 | static constexpr bool kMultiRegistersWidened = false; |
| 925 | static constexpr bool kAlignLongOnStack = false; |
| 926 | static constexpr bool kAlignDoubleOnStack = false; |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 927 | #elif defined(__x86_64__) |
| 928 | static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI. |
| 929 | static constexpr size_t kNumNativeGprArgs = 6; // 6 arguments passed in GPRs. |
| 930 | static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs. |
| 931 | |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 932 | static constexpr size_t kRegistersNeededForLong = 1; |
| 933 | static constexpr size_t kRegistersNeededForDouble = 1; |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 934 | static constexpr bool kMultiRegistersAligned = false; |
Andreas Gampe | 7a0e504 | 2014-03-07 13:03:19 -0800 | [diff] [blame] | 935 | static constexpr bool kMultiRegistersWidened = false; |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 936 | static constexpr bool kAlignLongOnStack = false; |
| 937 | static constexpr bool kAlignDoubleOnStack = false; |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 938 | #else |
| 939 | #error "Unsupported architecture" |
| 940 | #endif |
| 941 | |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 942 | public: |
| 943 | explicit BuildGenericJniFrameStateMachine(T* delegate) : gpr_index_(kNumNativeGprArgs), |
| 944 | fpr_index_(kNumNativeFprArgs), |
| 945 | stack_entries_(0), |
| 946 | delegate_(delegate) { |
| 947 | // For register alignment, we want to assume that counters (gpr_index_, fpr_index_) are even iff |
| 948 | // the next register is even; counting down is just to make the compiler happy... |
| 949 | CHECK_EQ(kNumNativeGprArgs % 2, 0U); |
| 950 | CHECK_EQ(kNumNativeFprArgs % 2, 0U); |
| 951 | } |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 952 | |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 953 | virtual ~BuildGenericJniFrameStateMachine() {} |
| 954 | |
| 955 | bool HavePointerGpr() { |
| 956 | return gpr_index_ > 0; |
| 957 | } |
| 958 | |
| 959 | void AdvancePointer(void* val) { |
| 960 | if (HavePointerGpr()) { |
| 961 | gpr_index_--; |
| 962 | PushGpr(reinterpret_cast<uintptr_t>(val)); |
| 963 | } else { |
| 964 | stack_entries_++; // TODO: have a field for pointer length as multiple of 32b |
| 965 | PushStack(reinterpret_cast<uintptr_t>(val)); |
| 966 | gpr_index_ = 0; |
| 967 | } |
| 968 | } |
| 969 | |
| 970 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 971 | bool HaveHandleScopeGpr() { |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 972 | return gpr_index_ > 0; |
| 973 | } |
| 974 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 975 | void AdvanceHandleScope(mirror::Object* ptr) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 976 | uintptr_t handle = PushHandle(ptr); |
| 977 | if (HaveHandleScopeGpr()) { |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 978 | gpr_index_--; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 979 | PushGpr(handle); |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 980 | } else { |
| 981 | stack_entries_++; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 982 | PushStack(handle); |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 983 | gpr_index_ = 0; |
| 984 | } |
| 985 | } |
| 986 | |
| 987 | |
| 988 | bool HaveIntGpr() { |
| 989 | return gpr_index_ > 0; |
| 990 | } |
| 991 | |
| 992 | void AdvanceInt(uint32_t val) { |
| 993 | if (HaveIntGpr()) { |
| 994 | gpr_index_--; |
| 995 | PushGpr(val); |
| 996 | } else { |
| 997 | stack_entries_++; |
| 998 | PushStack(val); |
| 999 | gpr_index_ = 0; |
| 1000 | } |
| 1001 | } |
| 1002 | |
| 1003 | |
| 1004 | bool HaveLongGpr() { |
| 1005 | return gpr_index_ >= kRegistersNeededForLong + (LongGprNeedsPadding() ? 1 : 0); |
| 1006 | } |
| 1007 | |
| 1008 | bool LongGprNeedsPadding() { |
| 1009 | return kRegistersNeededForLong > 1 && // only pad when using multiple registers |
| 1010 | kAlignLongOnStack && // and when it needs alignment |
| 1011 | (gpr_index_ & 1) == 1; // counter is odd, see constructor |
| 1012 | } |
| 1013 | |
| 1014 | bool LongStackNeedsPadding() { |
| 1015 | return kRegistersNeededForLong > 1 && // only pad when using multiple registers |
| 1016 | kAlignLongOnStack && // and when it needs 8B alignment |
| 1017 | (stack_entries_ & 1) == 1; // counter is odd |
| 1018 | } |
| 1019 | |
| 1020 | void AdvanceLong(uint64_t val) { |
| 1021 | if (HaveLongGpr()) { |
| 1022 | if (LongGprNeedsPadding()) { |
| 1023 | PushGpr(0); |
| 1024 | gpr_index_--; |
| 1025 | } |
| 1026 | if (kRegistersNeededForLong == 1) { |
| 1027 | PushGpr(static_cast<uintptr_t>(val)); |
| 1028 | } else { |
| 1029 | PushGpr(static_cast<uintptr_t>(val & 0xFFFFFFFF)); |
| 1030 | PushGpr(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF)); |
| 1031 | } |
| 1032 | gpr_index_ -= kRegistersNeededForLong; |
| 1033 | } else { |
| 1034 | if (LongStackNeedsPadding()) { |
| 1035 | PushStack(0); |
| 1036 | stack_entries_++; |
| 1037 | } |
| 1038 | if (kRegistersNeededForLong == 1) { |
| 1039 | PushStack(static_cast<uintptr_t>(val)); |
| 1040 | stack_entries_++; |
| 1041 | } else { |
| 1042 | PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF)); |
| 1043 | PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF)); |
| 1044 | stack_entries_ += 2; |
| 1045 | } |
| 1046 | gpr_index_ = 0; |
| 1047 | } |
| 1048 | } |
| 1049 | |
| 1050 | |
| 1051 | bool HaveFloatFpr() { |
| 1052 | return fpr_index_ > 0; |
| 1053 | } |
| 1054 | |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1055 | template <typename U, typename V> V convert(U in) { |
| 1056 | CHECK_LE(sizeof(U), sizeof(V)); |
| 1057 | union { U u; V v; } tmp; |
| 1058 | tmp.u = in; |
| 1059 | return tmp.v; |
| 1060 | } |
| 1061 | |
| 1062 | void AdvanceFloat(float val) { |
| 1063 | if (kNativeSoftFloatAbi) { |
| 1064 | AdvanceInt(convert<float, uint32_t>(val)); |
| 1065 | } else { |
| 1066 | if (HaveFloatFpr()) { |
| 1067 | fpr_index_--; |
| 1068 | if (kRegistersNeededForDouble == 1) { |
| 1069 | if (kMultiRegistersWidened) { |
| 1070 | PushFpr8(convert<double, uint64_t>(val)); |
| 1071 | } else { |
| 1072 | // No widening, just use the bits. |
| 1073 | PushFpr8(convert<float, uint64_t>(val)); |
| 1074 | } |
| 1075 | } else { |
| 1076 | PushFpr4(val); |
| 1077 | } |
| 1078 | } else { |
| 1079 | stack_entries_++; |
| 1080 | if (kRegistersNeededForDouble == 1 && kMultiRegistersWidened) { |
| 1081 | // Need to widen before storing: Note the "double" in the template instantiation. |
| 1082 | PushStack(convert<double, uintptr_t>(val)); |
| 1083 | } else { |
| 1084 | PushStack(convert<float, uintptr_t>(val)); |
| 1085 | } |
| 1086 | fpr_index_ = 0; |
| 1087 | } |
| 1088 | } |
| 1089 | } |
| 1090 | |
| 1091 | |
| 1092 | bool HaveDoubleFpr() { |
| 1093 | return fpr_index_ >= kRegistersNeededForDouble + (DoubleFprNeedsPadding() ? 1 : 0); |
| 1094 | } |
| 1095 | |
| 1096 | bool DoubleFprNeedsPadding() { |
| 1097 | return kRegistersNeededForDouble > 1 && // only pad when using multiple registers |
| 1098 | kAlignDoubleOnStack && // and when it needs alignment |
| 1099 | (fpr_index_ & 1) == 1; // counter is odd, see constructor |
| 1100 | } |
| 1101 | |
| 1102 | bool DoubleStackNeedsPadding() { |
| 1103 | return kRegistersNeededForDouble > 1 && // only pad when using multiple registers |
| 1104 | kAlignDoubleOnStack && // and when it needs 8B alignment |
| 1105 | (stack_entries_ & 1) == 1; // counter is odd |
| 1106 | } |
| 1107 | |
| 1108 | void AdvanceDouble(uint64_t val) { |
| 1109 | if (kNativeSoftFloatAbi) { |
| 1110 | AdvanceLong(val); |
| 1111 | } else { |
| 1112 | if (HaveDoubleFpr()) { |
| 1113 | if (DoubleFprNeedsPadding()) { |
| 1114 | PushFpr4(0); |
| 1115 | fpr_index_--; |
| 1116 | } |
| 1117 | PushFpr8(val); |
| 1118 | fpr_index_ -= kRegistersNeededForDouble; |
| 1119 | } else { |
| 1120 | if (DoubleStackNeedsPadding()) { |
| 1121 | PushStack(0); |
| 1122 | stack_entries_++; |
| 1123 | } |
| 1124 | if (kRegistersNeededForDouble == 1) { |
| 1125 | PushStack(static_cast<uintptr_t>(val)); |
| 1126 | stack_entries_++; |
| 1127 | } else { |
| 1128 | PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF)); |
| 1129 | PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF)); |
| 1130 | stack_entries_ += 2; |
| 1131 | } |
| 1132 | fpr_index_ = 0; |
| 1133 | } |
| 1134 | } |
| 1135 | } |
| 1136 | |
| 1137 | uint32_t getStackEntries() { |
| 1138 | return stack_entries_; |
| 1139 | } |
| 1140 | |
| 1141 | uint32_t getNumberOfUsedGprs() { |
| 1142 | return kNumNativeGprArgs - gpr_index_; |
| 1143 | } |
| 1144 | |
| 1145 | uint32_t getNumberOfUsedFprs() { |
| 1146 | return kNumNativeFprArgs - fpr_index_; |
| 1147 | } |
| 1148 | |
| 1149 | private: |
| 1150 | void PushGpr(uintptr_t val) { |
| 1151 | delegate_->PushGpr(val); |
| 1152 | } |
| 1153 | void PushFpr4(float val) { |
| 1154 | delegate_->PushFpr4(val); |
| 1155 | } |
| 1156 | void PushFpr8(uint64_t val) { |
| 1157 | delegate_->PushFpr8(val); |
| 1158 | } |
| 1159 | void PushStack(uintptr_t val) { |
| 1160 | delegate_->PushStack(val); |
| 1161 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1162 | uintptr_t PushHandle(mirror::Object* ref) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 1163 | return delegate_->PushHandle(ref); |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1164 | } |
| 1165 | |
| 1166 | uint32_t gpr_index_; // Number of free GPRs |
| 1167 | uint32_t fpr_index_; // Number of free FPRs |
| 1168 | uint32_t stack_entries_; // Stack entries are in multiples of 32b, as floats are usually not |
| 1169 | // extended |
| 1170 | T* delegate_; // What Push implementation gets called |
| 1171 | }; |
| 1172 | |
| 1173 | class ComputeGenericJniFrameSize FINAL { |
| 1174 | public: |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1175 | ComputeGenericJniFrameSize() : num_handle_scope_references_(0), num_stack_entries_(0) {} |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1176 | |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1177 | uint32_t GetStackSize() { |
| 1178 | return num_stack_entries_ * sizeof(uintptr_t); |
| 1179 | } |
| 1180 | |
Andreas Gampe | 36fea8d | 2014-03-10 13:37:40 -0700 | [diff] [blame] | 1181 | // WARNING: After this, *sp won't be pointing to the method anymore! |
Andreas Gampe | cf4035a | 2014-05-28 22:43:01 -0700 | [diff] [blame] | 1182 | void ComputeLayout(StackReference<mirror::ArtMethod>** m, bool is_static, const char* shorty, |
| 1183 | uint32_t shorty_len, void* sp, HandleScope** table, |
| 1184 | uint32_t* handle_scope_entries, uintptr_t** start_stack, uintptr_t** start_gpr, |
| 1185 | uint32_t** start_fpr, void** code_return, size_t* overall_size) |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1186 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 1187 | ComputeAll(is_static, shorty, shorty_len); |
| 1188 | |
Andreas Gampe | cf4035a | 2014-05-28 22:43:01 -0700 | [diff] [blame] | 1189 | mirror::ArtMethod* method = (*m)->AsMirrorPtr(); |
Andreas Gampe | 36fea8d | 2014-03-10 13:37:40 -0700 | [diff] [blame] | 1190 | |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1191 | uint8_t* sp8 = reinterpret_cast<uint8_t*>(sp); |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1192 | |
Andreas Gampe | 36fea8d | 2014-03-10 13:37:40 -0700 | [diff] [blame] | 1193 | // First, fix up the layout of the callee-save frame. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1194 | // We have to squeeze in the HandleScope, and relocate the method pointer. |
Andreas Gampe | 36fea8d | 2014-03-10 13:37:40 -0700 | [diff] [blame] | 1195 | |
| 1196 | // "Free" the slot for the method. |
Andreas Gampe | cf4035a | 2014-05-28 22:43:01 -0700 | [diff] [blame] | 1197 | sp8 += kPointerSize; // In the callee-save frame we use a full pointer. |
Andreas Gampe | 36fea8d | 2014-03-10 13:37:40 -0700 | [diff] [blame] | 1198 | |
Andreas Gampe | cf4035a | 2014-05-28 22:43:01 -0700 | [diff] [blame] | 1199 | // Under the callee saves put handle scope and new method stack reference. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1200 | *handle_scope_entries = num_handle_scope_references_; |
Andreas Gampe | cf4035a | 2014-05-28 22:43:01 -0700 | [diff] [blame] | 1201 | |
| 1202 | size_t handle_scope_size = HandleScope::SizeOf(num_handle_scope_references_); |
| 1203 | size_t scope_and_method = handle_scope_size + sizeof(StackReference<mirror::ArtMethod>); |
| 1204 | |
| 1205 | sp8 -= scope_and_method; |
| 1206 | // Align by kStackAlignment |
| 1207 | uintptr_t sp_to_align = reinterpret_cast<uintptr_t>(sp8); |
| 1208 | sp_to_align = RoundDown(sp_to_align, kStackAlignment); |
| 1209 | sp8 = reinterpret_cast<uint8_t*>(sp_to_align); |
| 1210 | |
| 1211 | uint8_t* sp8_table = sp8 + sizeof(StackReference<mirror::ArtMethod>); |
| 1212 | *table = reinterpret_cast<HandleScope*>(sp8_table); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1213 | (*table)->SetNumberOfReferences(num_handle_scope_references_); |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1214 | |
Andreas Gampe | 36fea8d | 2014-03-10 13:37:40 -0700 | [diff] [blame] | 1215 | // Add a slot for the method pointer, and fill it. Fix the pointer-pointer given to us. |
Andreas Gampe | 36fea8d | 2014-03-10 13:37:40 -0700 | [diff] [blame] | 1216 | uint8_t* method_pointer = sp8; |
Andreas Gampe | cf4035a | 2014-05-28 22:43:01 -0700 | [diff] [blame] | 1217 | StackReference<mirror::ArtMethod>* new_method_ref = |
| 1218 | reinterpret_cast<StackReference<mirror::ArtMethod>*>(method_pointer); |
| 1219 | new_method_ref->Assign(method); |
| 1220 | *m = new_method_ref; |
Andreas Gampe | 36fea8d | 2014-03-10 13:37:40 -0700 | [diff] [blame] | 1221 | |
| 1222 | // Reference cookie and padding |
| 1223 | sp8 -= 8; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1224 | // Store HandleScope size |
| 1225 | *reinterpret_cast<uint32_t*>(sp8) = static_cast<uint32_t>(handle_scope_size & 0xFFFFFFFF); |
Andreas Gampe | 36fea8d | 2014-03-10 13:37:40 -0700 | [diff] [blame] | 1226 | |
| 1227 | // Next comes the native call stack. |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1228 | sp8 -= GetStackSize(); |
Andreas Gampe | 36fea8d | 2014-03-10 13:37:40 -0700 | [diff] [blame] | 1229 | // Now align the call stack below. This aligns by 16, as AArch64 seems to require. |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1230 | uintptr_t mask = ~0x0F; |
| 1231 | sp8 = reinterpret_cast<uint8_t*>(reinterpret_cast<uintptr_t>(sp8) & mask); |
| 1232 | *start_stack = reinterpret_cast<uintptr_t*>(sp8); |
| 1233 | |
| 1234 | // put fprs and gprs below |
| 1235 | // Assumption is OK right now, as we have soft-float arm |
| 1236 | size_t fregs = BuildGenericJniFrameStateMachine<ComputeGenericJniFrameSize>::kNumNativeFprArgs; |
| 1237 | sp8 -= fregs * sizeof(uintptr_t); |
| 1238 | *start_fpr = reinterpret_cast<uint32_t*>(sp8); |
| 1239 | size_t iregs = BuildGenericJniFrameStateMachine<ComputeGenericJniFrameSize>::kNumNativeGprArgs; |
| 1240 | sp8 -= iregs * sizeof(uintptr_t); |
| 1241 | *start_gpr = reinterpret_cast<uintptr_t*>(sp8); |
| 1242 | |
| 1243 | // reserve space for the code pointer |
Andreas Gampe | 36fea8d | 2014-03-10 13:37:40 -0700 | [diff] [blame] | 1244 | sp8 -= kPointerSize; |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1245 | *code_return = reinterpret_cast<void*>(sp8); |
| 1246 | |
| 1247 | *overall_size = reinterpret_cast<uint8_t*>(sp) - sp8; |
Andreas Gampe | 36fea8d | 2014-03-10 13:37:40 -0700 | [diff] [blame] | 1248 | |
| 1249 | // The new SP is stored at the end of the alloca, so it can be immediately popped |
| 1250 | sp8 = reinterpret_cast<uint8_t*>(sp) - 5 * KB; |
| 1251 | *(reinterpret_cast<uint8_t**>(sp8)) = method_pointer; |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1252 | } |
| 1253 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1254 | void ComputeHandleScopeOffset() { } // nothing to do, static right now |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1255 | |
| 1256 | void ComputeAll(bool is_static, const char* shorty, uint32_t shorty_len) |
| 1257 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 1258 | BuildGenericJniFrameStateMachine<ComputeGenericJniFrameSize> sm(this); |
| 1259 | |
| 1260 | // JNIEnv |
| 1261 | sm.AdvancePointer(nullptr); |
| 1262 | |
| 1263 | // Class object or this as first argument |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1264 | sm.AdvanceHandleScope(reinterpret_cast<mirror::Object*>(0x12345678)); |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1265 | |
| 1266 | for (uint32_t i = 1; i < shorty_len; ++i) { |
| 1267 | Primitive::Type cur_type_ = Primitive::GetType(shorty[i]); |
| 1268 | switch (cur_type_) { |
| 1269 | case Primitive::kPrimNot: |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1270 | sm.AdvanceHandleScope(reinterpret_cast<mirror::Object*>(0x12345678)); |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1271 | break; |
| 1272 | |
| 1273 | case Primitive::kPrimBoolean: |
| 1274 | case Primitive::kPrimByte: |
| 1275 | case Primitive::kPrimChar: |
| 1276 | case Primitive::kPrimShort: |
| 1277 | case Primitive::kPrimInt: |
| 1278 | sm.AdvanceInt(0); |
| 1279 | break; |
| 1280 | case Primitive::kPrimFloat: |
| 1281 | sm.AdvanceFloat(0); |
| 1282 | break; |
| 1283 | case Primitive::kPrimDouble: |
| 1284 | sm.AdvanceDouble(0); |
| 1285 | break; |
| 1286 | case Primitive::kPrimLong: |
| 1287 | sm.AdvanceLong(0); |
| 1288 | break; |
| 1289 | default: |
| 1290 | LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty; |
| 1291 | } |
| 1292 | } |
| 1293 | |
| 1294 | num_stack_entries_ = sm.getStackEntries(); |
| 1295 | } |
| 1296 | |
| 1297 | void PushGpr(uintptr_t /* val */) { |
| 1298 | // not optimizing registers, yet |
| 1299 | } |
| 1300 | |
| 1301 | void PushFpr4(float /* val */) { |
| 1302 | // not optimizing registers, yet |
| 1303 | } |
| 1304 | |
| 1305 | void PushFpr8(uint64_t /* val */) { |
| 1306 | // not optimizing registers, yet |
| 1307 | } |
| 1308 | |
| 1309 | void PushStack(uintptr_t /* val */) { |
| 1310 | // counting is already done in the superclass |
| 1311 | } |
| 1312 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1313 | uintptr_t PushHandle(mirror::Object* /* ptr */) { |
| 1314 | num_handle_scope_references_++; |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1315 | return reinterpret_cast<uintptr_t>(nullptr); |
| 1316 | } |
| 1317 | |
| 1318 | private: |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1319 | uint32_t num_handle_scope_references_; |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1320 | uint32_t num_stack_entries_; |
| 1321 | }; |
| 1322 | |
| 1323 | // Visits arguments on the stack placing them into a region lower down the stack for the benefit |
| 1324 | // of transitioning into native code. |
| 1325 | class BuildGenericJniFrameVisitor FINAL : public QuickArgumentVisitor { |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1326 | public: |
Andreas Gampe | cf4035a | 2014-05-28 22:43:01 -0700 | [diff] [blame] | 1327 | BuildGenericJniFrameVisitor(StackReference<mirror::ArtMethod>** sp, bool is_static, |
| 1328 | const char* shorty, uint32_t shorty_len, Thread* self) : |
Andreas Gampe | 36fea8d | 2014-03-10 13:37:40 -0700 | [diff] [blame] | 1329 | QuickArgumentVisitor(*sp, is_static, shorty, shorty_len), sm_(this) { |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1330 | ComputeGenericJniFrameSize fsc; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1331 | fsc.ComputeLayout(sp, is_static, shorty, shorty_len, *sp, &handle_scope_, &handle_scope_expected_refs_, |
Andreas Gampe | 36fea8d | 2014-03-10 13:37:40 -0700 | [diff] [blame] | 1332 | &cur_stack_arg_, &cur_gpr_reg_, &cur_fpr_reg_, &code_return_, |
| 1333 | &alloca_used_size_); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1334 | handle_scope_number_of_references_ = 0; |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 1335 | cur_hs_entry_ = GetFirstHandleScopeEntry(); |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1336 | |
| 1337 | // jni environment is always first argument |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1338 | sm_.AdvancePointer(self->GetJniEnv()); |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1339 | |
| 1340 | if (is_static) { |
Andreas Gampe | cf4035a | 2014-05-28 22:43:01 -0700 | [diff] [blame] | 1341 | sm_.AdvanceHandleScope((*sp)->AsMirrorPtr()->GetDeclaringClass()); |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1342 | } |
| 1343 | } |
| 1344 | |
Ian Rogers | 9758f79 | 2014-03-13 09:02:55 -0700 | [diff] [blame] | 1345 | void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE; |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1346 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1347 | void FinalizeHandleScope(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1348 | |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 1349 | StackReference<mirror::Object>* GetFirstHandleScopeEntry() |
| 1350 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 1351 | return handle_scope_->GetHandle(0).GetReference(); |
| 1352 | } |
| 1353 | |
| 1354 | jobject GetFirstHandleScopeJObject() |
| 1355 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1356 | return handle_scope_->GetHandle(0).ToJObject(); |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1357 | } |
| 1358 | |
| 1359 | void PushGpr(uintptr_t val) { |
| 1360 | *cur_gpr_reg_ = val; |
| 1361 | cur_gpr_reg_++; |
| 1362 | } |
| 1363 | |
| 1364 | void PushFpr4(float val) { |
| 1365 | *cur_fpr_reg_ = val; |
| 1366 | cur_fpr_reg_++; |
| 1367 | } |
| 1368 | |
| 1369 | void PushFpr8(uint64_t val) { |
| 1370 | uint64_t* tmp = reinterpret_cast<uint64_t*>(cur_fpr_reg_); |
| 1371 | *tmp = val; |
| 1372 | cur_fpr_reg_ += 2; |
| 1373 | } |
| 1374 | |
| 1375 | void PushStack(uintptr_t val) { |
| 1376 | *cur_stack_arg_ = val; |
| 1377 | cur_stack_arg_++; |
| 1378 | } |
| 1379 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1380 | uintptr_t PushHandle(mirror::Object* ref) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Andreas Gampe | 36fea8d | 2014-03-10 13:37:40 -0700 | [diff] [blame] | 1381 | uintptr_t tmp; |
| 1382 | if (ref == nullptr) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1383 | *cur_hs_entry_ = StackReference<mirror::Object>(); |
Andreas Gampe | 36fea8d | 2014-03-10 13:37:40 -0700 | [diff] [blame] | 1384 | tmp = reinterpret_cast<uintptr_t>(nullptr); |
| 1385 | } else { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1386 | *cur_hs_entry_ = StackReference<mirror::Object>::FromMirrorPtr(ref); |
| 1387 | tmp = reinterpret_cast<uintptr_t>(cur_hs_entry_); |
Andreas Gampe | 36fea8d | 2014-03-10 13:37:40 -0700 | [diff] [blame] | 1388 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1389 | cur_hs_entry_++; |
| 1390 | handle_scope_number_of_references_++; |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1391 | return tmp; |
| 1392 | } |
| 1393 | |
| 1394 | // Size of the part of the alloca that we actually need. |
| 1395 | size_t GetAllocaUsedSize() { |
| 1396 | return alloca_used_size_; |
| 1397 | } |
| 1398 | |
| 1399 | void* GetCodeReturn() { |
| 1400 | return code_return_; |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1401 | } |
| 1402 | |
| 1403 | private: |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1404 | uint32_t handle_scope_number_of_references_; |
| 1405 | StackReference<mirror::Object>* cur_hs_entry_; |
| 1406 | HandleScope* handle_scope_; |
| 1407 | uint32_t handle_scope_expected_refs_; |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1408 | uintptr_t* cur_gpr_reg_; |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1409 | uint32_t* cur_fpr_reg_; |
| 1410 | uintptr_t* cur_stack_arg_; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1411 | // StackReference<mirror::Object>* top_of_handle_scope_; |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1412 | void* code_return_; |
| 1413 | size_t alloca_used_size_; |
| 1414 | |
| 1415 | BuildGenericJniFrameStateMachine<BuildGenericJniFrameVisitor> sm_; |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1416 | |
| 1417 | DISALLOW_COPY_AND_ASSIGN(BuildGenericJniFrameVisitor); |
| 1418 | }; |
| 1419 | |
Ian Rogers | 9758f79 | 2014-03-13 09:02:55 -0700 | [diff] [blame] | 1420 | void BuildGenericJniFrameVisitor::Visit() { |
| 1421 | Primitive::Type type = GetParamPrimitiveType(); |
| 1422 | switch (type) { |
| 1423 | case Primitive::kPrimLong: { |
| 1424 | jlong long_arg; |
| 1425 | if (IsSplitLongOrDouble()) { |
| 1426 | long_arg = ReadSplitLongParam(); |
| 1427 | } else { |
| 1428 | long_arg = *reinterpret_cast<jlong*>(GetParamAddress()); |
| 1429 | } |
| 1430 | sm_.AdvanceLong(long_arg); |
| 1431 | break; |
| 1432 | } |
| 1433 | case Primitive::kPrimDouble: { |
| 1434 | uint64_t double_arg; |
| 1435 | if (IsSplitLongOrDouble()) { |
| 1436 | // Read into union so that we don't case to a double. |
| 1437 | double_arg = ReadSplitLongParam(); |
| 1438 | } else { |
| 1439 | double_arg = *reinterpret_cast<uint64_t*>(GetParamAddress()); |
| 1440 | } |
| 1441 | sm_.AdvanceDouble(double_arg); |
| 1442 | break; |
| 1443 | } |
| 1444 | case Primitive::kPrimNot: { |
| 1445 | StackReference<mirror::Object>* stack_ref = |
| 1446 | reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1447 | sm_.AdvanceHandleScope(stack_ref->AsMirrorPtr()); |
Ian Rogers | 9758f79 | 2014-03-13 09:02:55 -0700 | [diff] [blame] | 1448 | break; |
| 1449 | } |
| 1450 | case Primitive::kPrimFloat: |
| 1451 | sm_.AdvanceFloat(*reinterpret_cast<float*>(GetParamAddress())); |
| 1452 | break; |
| 1453 | case Primitive::kPrimBoolean: // Fall-through. |
| 1454 | case Primitive::kPrimByte: // Fall-through. |
| 1455 | case Primitive::kPrimChar: // Fall-through. |
| 1456 | case Primitive::kPrimShort: // Fall-through. |
| 1457 | case Primitive::kPrimInt: // Fall-through. |
| 1458 | sm_.AdvanceInt(*reinterpret_cast<jint*>(GetParamAddress())); |
| 1459 | break; |
| 1460 | case Primitive::kPrimVoid: |
| 1461 | LOG(FATAL) << "UNREACHABLE"; |
| 1462 | break; |
| 1463 | } |
| 1464 | } |
| 1465 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1466 | void BuildGenericJniFrameVisitor::FinalizeHandleScope(Thread* self) { |
Ian Rogers | 9758f79 | 2014-03-13 09:02:55 -0700 | [diff] [blame] | 1467 | // Initialize padding entries. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1468 | while (handle_scope_number_of_references_ < handle_scope_expected_refs_) { |
| 1469 | *cur_hs_entry_ = StackReference<mirror::Object>(); |
| 1470 | cur_hs_entry_++; |
| 1471 | handle_scope_number_of_references_++; |
Ian Rogers | 9758f79 | 2014-03-13 09:02:55 -0700 | [diff] [blame] | 1472 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1473 | handle_scope_->SetNumberOfReferences(handle_scope_expected_refs_); |
| 1474 | DCHECK_NE(handle_scope_expected_refs_, 0U); |
| 1475 | // Install HandleScope. |
| 1476 | self->PushHandleScope(handle_scope_); |
Ian Rogers | 9758f79 | 2014-03-13 09:02:55 -0700 | [diff] [blame] | 1477 | } |
| 1478 | |
Andreas Gampe | 9054683 | 2014-03-12 18:07:19 -0700 | [diff] [blame] | 1479 | extern "C" void* artFindNativeMethod(); |
| 1480 | |
Andreas Gampe | ad61517 | 2014-04-04 16:20:13 -0700 | [diff] [blame] | 1481 | uint64_t artQuickGenericJniEndJNIRef(Thread* self, uint32_t cookie, jobject l, jobject lock) { |
| 1482 | if (lock != nullptr) { |
| 1483 | return reinterpret_cast<uint64_t>(JniMethodEndWithReferenceSynchronized(l, cookie, lock, self)); |
| 1484 | } else { |
| 1485 | return reinterpret_cast<uint64_t>(JniMethodEndWithReference(l, cookie, self)); |
| 1486 | } |
| 1487 | } |
| 1488 | |
| 1489 | void artQuickGenericJniEndJNINonRef(Thread* self, uint32_t cookie, jobject lock) { |
| 1490 | if (lock != nullptr) { |
| 1491 | JniMethodEndSynchronized(cookie, lock, self); |
| 1492 | } else { |
| 1493 | JniMethodEnd(cookie, self); |
| 1494 | } |
| 1495 | } |
| 1496 | |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1497 | /* |
| 1498 | * Initializes an alloca region assumed to be directly below sp for a native call: |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1499 | * Create a HandleScope and call stack and fill a mini stack with values to be pushed to registers. |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1500 | * The final element on the stack is a pointer to the native code. |
| 1501 | * |
Andreas Gampe | 36fea8d | 2014-03-10 13:37:40 -0700 | [diff] [blame] | 1502 | * On entry, the stack has a standard callee-save frame above sp, and an alloca below it. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1503 | * We need to fix this, as the handle scope needs to go into the callee-save frame. |
Andreas Gampe | 36fea8d | 2014-03-10 13:37:40 -0700 | [diff] [blame] | 1504 | * |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1505 | * The return of this function denotes: |
| 1506 | * 1) How many bytes of the alloca can be released, if the value is non-negative. |
| 1507 | * 2) An error, if the value is negative. |
| 1508 | */ |
Andreas Gampe | cf4035a | 2014-05-28 22:43:01 -0700 | [diff] [blame] | 1509 | extern "C" ssize_t artQuickGenericJniTrampoline(Thread* self, StackReference<mirror::ArtMethod>* sp) |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 1510 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Andreas Gampe | cf4035a | 2014-05-28 22:43:01 -0700 | [diff] [blame] | 1511 | mirror::ArtMethod* called = sp->AsMirrorPtr(); |
Andreas Gampe | 36fea8d | 2014-03-10 13:37:40 -0700 | [diff] [blame] | 1512 | DCHECK(called->IsNative()) << PrettyMethod(called, true); |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1513 | |
| 1514 | // run the visitor |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame^] | 1515 | uint32_t shorty_len = 0; |
| 1516 | const char* shorty = called->GetShorty(&shorty_len); |
| 1517 | BuildGenericJniFrameVisitor visitor(&sp, called->IsStatic(), shorty, shorty_len, self); |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1518 | visitor.VisitArguments(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1519 | visitor.FinalizeHandleScope(self); |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1520 | |
| 1521 | // fix up managed-stack things in Thread |
| 1522 | self->SetTopOfStack(sp, 0); |
| 1523 | |
Ian Rogers | e0dcd46 | 2014-03-08 15:21:04 -0800 | [diff] [blame] | 1524 | self->VerifyStack(); |
| 1525 | |
Andreas Gampe | 9054683 | 2014-03-12 18:07:19 -0700 | [diff] [blame] | 1526 | // Start JNI, save the cookie. |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1527 | uint32_t cookie; |
| 1528 | if (called->IsSynchronized()) { |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 1529 | cookie = JniMethodStartSynchronized(visitor.GetFirstHandleScopeJObject(), self); |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1530 | if (self->IsExceptionPending()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1531 | self->PopHandleScope(); |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1532 | // A negative value denotes an error. |
| 1533 | return -1; |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1534 | } |
| 1535 | } else { |
| 1536 | cookie = JniMethodStart(self); |
| 1537 | } |
Andreas Gampe | 36fea8d | 2014-03-10 13:37:40 -0700 | [diff] [blame] | 1538 | uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp); |
Ian Rogers | e0dcd46 | 2014-03-08 15:21:04 -0800 | [diff] [blame] | 1539 | *(sp32 - 1) = cookie; |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1540 | |
Andreas Gampe | 9054683 | 2014-03-12 18:07:19 -0700 | [diff] [blame] | 1541 | // Retrieve the stored native code. |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1542 | const void* nativeCode = called->GetNativeMethod(); |
Andreas Gampe | 9054683 | 2014-03-12 18:07:19 -0700 | [diff] [blame] | 1543 | |
Andreas Gampe | 9a6a99a | 2014-03-14 07:52:20 -0700 | [diff] [blame] | 1544 | // There are two cases for the content of nativeCode: |
| 1545 | // 1) Pointer to the native function. |
| 1546 | // 2) Pointer to the trampoline for native code binding. |
| 1547 | // In the second case, we need to execute the binding and continue with the actual native function |
| 1548 | // pointer. |
Andreas Gampe | 9054683 | 2014-03-12 18:07:19 -0700 | [diff] [blame] | 1549 | DCHECK(nativeCode != nullptr); |
| 1550 | if (nativeCode == GetJniDlsymLookupStub()) { |
| 1551 | nativeCode = artFindNativeMethod(); |
| 1552 | |
| 1553 | if (nativeCode == nullptr) { |
| 1554 | DCHECK(self->IsExceptionPending()); // There should be an exception pending now. |
Andreas Gampe | ad61517 | 2014-04-04 16:20:13 -0700 | [diff] [blame] | 1555 | |
| 1556 | // End JNI, as the assembly will move to deliver the exception. |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 1557 | jobject lock = called->IsSynchronized() ? visitor.GetFirstHandleScopeJObject() : nullptr; |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame^] | 1558 | if (shorty[0] == 'L') { |
Andreas Gampe | ad61517 | 2014-04-04 16:20:13 -0700 | [diff] [blame] | 1559 | artQuickGenericJniEndJNIRef(self, cookie, nullptr, lock); |
| 1560 | } else { |
| 1561 | artQuickGenericJniEndJNINonRef(self, cookie, lock); |
| 1562 | } |
| 1563 | |
Andreas Gampe | 9054683 | 2014-03-12 18:07:19 -0700 | [diff] [blame] | 1564 | return -1; |
| 1565 | } |
| 1566 | // Note that the native code pointer will be automatically set by artFindNativeMethod(). |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1567 | } |
| 1568 | |
Andreas Gampe | 9054683 | 2014-03-12 18:07:19 -0700 | [diff] [blame] | 1569 | // Store the native code pointer in the stack at the right location. |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1570 | uintptr_t* code_pointer = reinterpret_cast<uintptr_t*>(visitor.GetCodeReturn()); |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1571 | *code_pointer = reinterpret_cast<uintptr_t>(nativeCode); |
| 1572 | |
Andreas Gampe | 36fea8d | 2014-03-10 13:37:40 -0700 | [diff] [blame] | 1573 | // 5K reserved, window_size + frame pointer used. |
Andreas Gampe | 9054683 | 2014-03-12 18:07:19 -0700 | [diff] [blame] | 1574 | size_t window_size = visitor.GetAllocaUsedSize(); |
Andreas Gampe | 36fea8d | 2014-03-10 13:37:40 -0700 | [diff] [blame] | 1575 | return (5 * KB) - window_size - kPointerSize; |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1576 | } |
| 1577 | |
| 1578 | /* |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1579 | * Is called after the native JNI code. Responsible for cleanup (handle scope, saved state) and |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1580 | * unlocking. |
| 1581 | */ |
Andreas Gampe | cf4035a | 2014-05-28 22:43:01 -0700 | [diff] [blame] | 1582 | extern "C" uint64_t artQuickGenericJniEndTrampoline(Thread* self, |
| 1583 | StackReference<mirror::ArtMethod>* sp, |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1584 | jvalue result, uint64_t result_f) |
| 1585 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 1586 | uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp); |
Andreas Gampe | cf4035a | 2014-05-28 22:43:01 -0700 | [diff] [blame] | 1587 | mirror::ArtMethod* called = sp->AsMirrorPtr(); |
Ian Rogers | e0dcd46 | 2014-03-08 15:21:04 -0800 | [diff] [blame] | 1588 | uint32_t cookie = *(sp32 - 1); |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1589 | |
Andreas Gampe | ad61517 | 2014-04-04 16:20:13 -0700 | [diff] [blame] | 1590 | jobject lock = nullptr; |
| 1591 | if (called->IsSynchronized()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1592 | HandleScope* table = reinterpret_cast<HandleScope*>( |
Andreas Gampe | cf4035a | 2014-05-28 22:43:01 -0700 | [diff] [blame] | 1593 | reinterpret_cast<uint8_t*>(sp) + sizeof(StackReference<mirror::ArtMethod>)); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1594 | lock = table->GetHandle(0).ToJObject(); |
Andreas Gampe | ad61517 | 2014-04-04 16:20:13 -0700 | [diff] [blame] | 1595 | } |
| 1596 | |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame^] | 1597 | char return_shorty_char = called->GetShorty()[0]; |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1598 | |
| 1599 | if (return_shorty_char == 'L') { |
Andreas Gampe | ad61517 | 2014-04-04 16:20:13 -0700 | [diff] [blame] | 1600 | return artQuickGenericJniEndJNIRef(self, cookie, result.l, lock); |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1601 | } else { |
Andreas Gampe | ad61517 | 2014-04-04 16:20:13 -0700 | [diff] [blame] | 1602 | artQuickGenericJniEndJNINonRef(self, cookie, lock); |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1603 | |
| 1604 | switch (return_shorty_char) { |
| 1605 | case 'F': // Fall-through. |
| 1606 | case 'D': |
| 1607 | return result_f; |
| 1608 | case 'Z': |
| 1609 | return result.z; |
| 1610 | case 'B': |
| 1611 | return result.b; |
| 1612 | case 'C': |
| 1613 | return result.c; |
| 1614 | case 'S': |
| 1615 | return result.s; |
| 1616 | case 'I': |
| 1617 | return result.i; |
| 1618 | case 'J': |
| 1619 | return result.j; |
| 1620 | case 'V': |
| 1621 | return 0; |
| 1622 | default: |
| 1623 | LOG(FATAL) << "Unexpected return shorty character " << return_shorty_char; |
| 1624 | return 0; |
| 1625 | } |
| 1626 | } |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 1627 | } |
| 1628 | |
Andreas Gampe | 51f7635 | 2014-05-21 08:28:48 -0700 | [diff] [blame] | 1629 | // The following definitions create return types for two word-sized entities that will be passed |
| 1630 | // in registers so that memory operations for the interface trampolines can be avoided. The entities |
| 1631 | // are the resolved method and the pointer to the code to be invoked. |
| 1632 | // |
| 1633 | // On x86, ARM32 and MIPS, this is given for a *scalar* 64bit value. The definition thus *must* be |
| 1634 | // uint64_t or long long int. We use the upper 32b for code, and the lower 32b for the method. |
| 1635 | // |
| 1636 | // On x86_64 and ARM64, structs are decomposed for allocation, so we can create a structs of two |
| 1637 | // size_t-sized values. |
| 1638 | // |
| 1639 | // We need two operations: |
| 1640 | // |
| 1641 | // 1) A flag value that signals failure. The assembly stubs expect the method part to be "0". |
| 1642 | // GetFailureValue() will return a value that has method == 0. |
| 1643 | // |
| 1644 | // 2) A value that combines a code pointer and a method pointer. |
| 1645 | // GetSuccessValue() constructs this. |
| 1646 | |
| 1647 | #if defined(__i386__) || defined(__arm__) || defined(__mips__) |
| 1648 | typedef uint64_t MethodAndCode; |
| 1649 | |
| 1650 | // Encodes method_ptr==nullptr and code_ptr==nullptr |
| 1651 | static constexpr MethodAndCode GetFailureValue() { |
| 1652 | return 0; |
| 1653 | } |
| 1654 | |
| 1655 | // Use the lower 32b for the method pointer and the upper 32b for the code pointer. |
| 1656 | static MethodAndCode GetSuccessValue(const void* code, mirror::ArtMethod* method) { |
| 1657 | uint32_t method_uint = reinterpret_cast<uint32_t>(method); |
| 1658 | uint64_t code_uint = reinterpret_cast<uint32_t>(code); |
| 1659 | return ((code_uint << 32) | method_uint); |
| 1660 | } |
| 1661 | |
| 1662 | #elif defined(__x86_64__) || defined(__aarch64__) |
| 1663 | struct MethodAndCode { |
| 1664 | uintptr_t method; |
| 1665 | uintptr_t code; |
| 1666 | }; |
| 1667 | |
| 1668 | // Encodes method_ptr==nullptr. Leaves random value in code pointer. |
| 1669 | static MethodAndCode GetFailureValue() { |
| 1670 | MethodAndCode ret; |
| 1671 | ret.method = 0; |
| 1672 | return ret; |
| 1673 | } |
| 1674 | |
| 1675 | // Write values into their respective members. |
| 1676 | static MethodAndCode GetSuccessValue(const void* code, mirror::ArtMethod* method) { |
| 1677 | MethodAndCode ret; |
| 1678 | ret.method = reinterpret_cast<uintptr_t>(method); |
| 1679 | ret.code = reinterpret_cast<uintptr_t>(code); |
| 1680 | return ret; |
| 1681 | } |
| 1682 | #else |
| 1683 | #error "Unsupported architecture" |
| 1684 | #endif |
Mathieu Chartier | 5f3ded4 | 2014-04-03 15:25:30 -0700 | [diff] [blame] | 1685 | |
| 1686 | template<InvokeType type, bool access_check> |
Andreas Gampe | 51f7635 | 2014-05-21 08:28:48 -0700 | [diff] [blame] | 1687 | static MethodAndCode artInvokeCommon(uint32_t method_idx, mirror::Object* this_object, |
| 1688 | mirror::ArtMethod* caller_method, |
Andreas Gampe | cf4035a | 2014-05-28 22:43:01 -0700 | [diff] [blame] | 1689 | Thread* self, StackReference<mirror::ArtMethod>* sp); |
Andreas Gampe | 51f7635 | 2014-05-21 08:28:48 -0700 | [diff] [blame] | 1690 | |
| 1691 | template<InvokeType type, bool access_check> |
| 1692 | static MethodAndCode artInvokeCommon(uint32_t method_idx, mirror::Object* this_object, |
| 1693 | mirror::ArtMethod* caller_method, |
Andreas Gampe | cf4035a | 2014-05-28 22:43:01 -0700 | [diff] [blame] | 1694 | Thread* self, StackReference<mirror::ArtMethod>* sp) { |
Mathieu Chartier | 5f3ded4 | 2014-04-03 15:25:30 -0700 | [diff] [blame] | 1695 | mirror::ArtMethod* method = FindMethodFast(method_idx, this_object, caller_method, access_check, |
| 1696 | type); |
| 1697 | if (UNLIKELY(method == nullptr)) { |
| 1698 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs); |
| 1699 | const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()->GetDexFile(); |
| 1700 | uint32_t shorty_len; |
| 1701 | const char* shorty = |
| 1702 | dex_file->GetMethodShorty(dex_file->GetMethodId(method_idx), &shorty_len); |
| 1703 | { |
| 1704 | // Remember the args in case a GC happens in FindMethodFromCode. |
| 1705 | ScopedObjectAccessUnchecked soa(self->GetJniEnv()); |
| 1706 | RememberForGcArgumentVisitor visitor(sp, type == kStatic, shorty, shorty_len, &soa); |
| 1707 | visitor.VisitArguments(); |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 1708 | method = FindMethodFromCode<type, access_check>(method_idx, &this_object, &caller_method, |
| 1709 | self); |
Mathieu Chartier | 5f3ded4 | 2014-04-03 15:25:30 -0700 | [diff] [blame] | 1710 | visitor.FixupReferences(); |
| 1711 | } |
| 1712 | |
| 1713 | if (UNLIKELY(method == NULL)) { |
| 1714 | CHECK(self->IsExceptionPending()); |
Andreas Gampe | 51f7635 | 2014-05-21 08:28:48 -0700 | [diff] [blame] | 1715 | return GetFailureValue(); // Failure. |
Mathieu Chartier | 5f3ded4 | 2014-04-03 15:25:30 -0700 | [diff] [blame] | 1716 | } |
| 1717 | } |
| 1718 | DCHECK(!self->IsExceptionPending()); |
| 1719 | const void* code = method->GetEntryPointFromQuickCompiledCode(); |
| 1720 | |
| 1721 | // When we return, the caller will branch to this address, so it had better not be 0! |
| 1722 | DCHECK(code != nullptr) << "Code was NULL in method: " << PrettyMethod(method) << " location: " |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame^] | 1723 | << method->GetDexFile()->GetLocation(); |
Andreas Gampe | 51f7635 | 2014-05-21 08:28:48 -0700 | [diff] [blame] | 1724 | |
| 1725 | return GetSuccessValue(code, method); |
Mathieu Chartier | 5f3ded4 | 2014-04-03 15:25:30 -0700 | [diff] [blame] | 1726 | } |
| 1727 | |
Nicolas Geoffray | 8689a0a | 2014-04-04 09:26:24 +0100 | [diff] [blame] | 1728 | // Explicit artInvokeCommon template function declarations to please analysis tool. |
| 1729 | #define EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(type, access_check) \ |
| 1730 | template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) \ |
Andreas Gampe | 51f7635 | 2014-05-21 08:28:48 -0700 | [diff] [blame] | 1731 | MethodAndCode artInvokeCommon<type, access_check>(uint32_t method_idx, \ |
| 1732 | mirror::Object* this_object, \ |
| 1733 | mirror::ArtMethod* caller_method, \ |
Andreas Gampe | cf4035a | 2014-05-28 22:43:01 -0700 | [diff] [blame] | 1734 | Thread* self, \ |
| 1735 | StackReference<mirror::ArtMethod>* sp) \ |
Nicolas Geoffray | 8689a0a | 2014-04-04 09:26:24 +0100 | [diff] [blame] | 1736 | |
| 1737 | EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, false); |
| 1738 | EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, true); |
| 1739 | EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, false); |
| 1740 | EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, true); |
| 1741 | EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, false); |
| 1742 | EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, true); |
| 1743 | EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, false); |
| 1744 | EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, true); |
| 1745 | EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, false); |
| 1746 | EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, true); |
| 1747 | #undef EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL |
| 1748 | |
Mathieu Chartier | 5f3ded4 | 2014-04-03 15:25:30 -0700 | [diff] [blame] | 1749 | |
| 1750 | // See comments in runtime_support_asm.S |
Andreas Gampe | 51f7635 | 2014-05-21 08:28:48 -0700 | [diff] [blame] | 1751 | extern "C" MethodAndCode artInvokeInterfaceTrampolineWithAccessCheck(uint32_t method_idx, |
Andreas Gampe | cf4035a | 2014-05-28 22:43:01 -0700 | [diff] [blame] | 1752 | mirror::Object* this_object, |
| 1753 | mirror::ArtMethod* caller_method, |
| 1754 | Thread* self, |
| 1755 | StackReference<mirror::ArtMethod>* sp) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | 5f3ded4 | 2014-04-03 15:25:30 -0700 | [diff] [blame] | 1756 | return artInvokeCommon<kInterface, true>(method_idx, this_object, caller_method, self, sp); |
| 1757 | } |
| 1758 | |
| 1759 | |
Andreas Gampe | 51f7635 | 2014-05-21 08:28:48 -0700 | [diff] [blame] | 1760 | extern "C" MethodAndCode artInvokeDirectTrampolineWithAccessCheck(uint32_t method_idx, |
Andreas Gampe | cf4035a | 2014-05-28 22:43:01 -0700 | [diff] [blame] | 1761 | mirror::Object* this_object, |
| 1762 | mirror::ArtMethod* caller_method, |
| 1763 | Thread* self, |
| 1764 | StackReference<mirror::ArtMethod>* sp) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | 5f3ded4 | 2014-04-03 15:25:30 -0700 | [diff] [blame] | 1765 | return artInvokeCommon<kDirect, true>(method_idx, this_object, caller_method, self, sp); |
| 1766 | } |
| 1767 | |
Andreas Gampe | 51f7635 | 2014-05-21 08:28:48 -0700 | [diff] [blame] | 1768 | extern "C" MethodAndCode artInvokeStaticTrampolineWithAccessCheck(uint32_t method_idx, |
Andreas Gampe | cf4035a | 2014-05-28 22:43:01 -0700 | [diff] [blame] | 1769 | mirror::Object* this_object, |
| 1770 | mirror::ArtMethod* caller_method, |
| 1771 | Thread* self, |
| 1772 | StackReference<mirror::ArtMethod>* sp) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | 5f3ded4 | 2014-04-03 15:25:30 -0700 | [diff] [blame] | 1773 | return artInvokeCommon<kStatic, true>(method_idx, this_object, caller_method, self, sp); |
| 1774 | } |
| 1775 | |
Andreas Gampe | 51f7635 | 2014-05-21 08:28:48 -0700 | [diff] [blame] | 1776 | extern "C" MethodAndCode artInvokeSuperTrampolineWithAccessCheck(uint32_t method_idx, |
Andreas Gampe | cf4035a | 2014-05-28 22:43:01 -0700 | [diff] [blame] | 1777 | mirror::Object* this_object, |
| 1778 | mirror::ArtMethod* caller_method, |
| 1779 | Thread* self, |
| 1780 | StackReference<mirror::ArtMethod>* sp) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | 5f3ded4 | 2014-04-03 15:25:30 -0700 | [diff] [blame] | 1781 | return artInvokeCommon<kSuper, true>(method_idx, this_object, caller_method, self, sp); |
| 1782 | } |
| 1783 | |
Andreas Gampe | 51f7635 | 2014-05-21 08:28:48 -0700 | [diff] [blame] | 1784 | extern "C" MethodAndCode artInvokeVirtualTrampolineWithAccessCheck(uint32_t method_idx, |
Andreas Gampe | cf4035a | 2014-05-28 22:43:01 -0700 | [diff] [blame] | 1785 | mirror::Object* this_object, |
| 1786 | mirror::ArtMethod* caller_method, |
| 1787 | Thread* self, |
| 1788 | StackReference<mirror::ArtMethod>* sp) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | 5f3ded4 | 2014-04-03 15:25:30 -0700 | [diff] [blame] | 1789 | return artInvokeCommon<kVirtual, true>(method_idx, this_object, caller_method, self, sp); |
| 1790 | } |
| 1791 | |
| 1792 | // Determine target of interface dispatch. This object is known non-null. |
Andreas Gampe | 51f7635 | 2014-05-21 08:28:48 -0700 | [diff] [blame] | 1793 | extern "C" MethodAndCode artInvokeInterfaceTrampoline(mirror::ArtMethod* interface_method, |
| 1794 | mirror::Object* this_object, |
| 1795 | mirror::ArtMethod* caller_method, |
Andreas Gampe | cf4035a | 2014-05-28 22:43:01 -0700 | [diff] [blame] | 1796 | Thread* self, |
| 1797 | StackReference<mirror::ArtMethod>* sp) |
Mathieu Chartier | 5f3ded4 | 2014-04-03 15:25:30 -0700 | [diff] [blame] | 1798 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 1799 | mirror::ArtMethod* method; |
| 1800 | if (LIKELY(interface_method->GetDexMethodIndex() != DexFile::kDexNoIndex)) { |
| 1801 | method = this_object->GetClass()->FindVirtualMethodForInterface(interface_method); |
| 1802 | if (UNLIKELY(method == NULL)) { |
| 1803 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs); |
| 1804 | ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(interface_method, this_object, |
| 1805 | caller_method); |
Andreas Gampe | 51f7635 | 2014-05-21 08:28:48 -0700 | [diff] [blame] | 1806 | return GetFailureValue(); // Failure. |
Mathieu Chartier | 5f3ded4 | 2014-04-03 15:25:30 -0700 | [diff] [blame] | 1807 | } |
| 1808 | } else { |
| 1809 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs); |
| 1810 | DCHECK(interface_method == Runtime::Current()->GetResolutionMethod()); |
| 1811 | // Determine method index from calling dex instruction. |
| 1812 | #if defined(__arm__) |
| 1813 | // On entry the stack pointed by sp is: |
| 1814 | // | argN | | |
| 1815 | // | ... | | |
| 1816 | // | arg4 | | |
| 1817 | // | arg3 spill | | Caller's frame |
| 1818 | // | arg2 spill | | |
| 1819 | // | arg1 spill | | |
| 1820 | // | Method* | --- |
| 1821 | // | LR | |
| 1822 | // | ... | callee saves |
| 1823 | // | R3 | arg3 |
| 1824 | // | R2 | arg2 |
| 1825 | // | R1 | arg1 |
| 1826 | // | R0 | |
| 1827 | // | Method* | <- sp |
| 1828 | DCHECK_EQ(48U, Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes()); |
| 1829 | uintptr_t* regs = reinterpret_cast<uintptr_t*>(reinterpret_cast<byte*>(sp) + kPointerSize); |
| 1830 | uintptr_t caller_pc = regs[10]; |
| 1831 | #elif defined(__i386__) |
| 1832 | // On entry the stack pointed by sp is: |
| 1833 | // | argN | | |
| 1834 | // | ... | | |
| 1835 | // | arg4 | | |
| 1836 | // | arg3 spill | | Caller's frame |
| 1837 | // | arg2 spill | | |
| 1838 | // | arg1 spill | | |
| 1839 | // | Method* | --- |
| 1840 | // | Return | |
| 1841 | // | EBP,ESI,EDI | callee saves |
| 1842 | // | EBX | arg3 |
| 1843 | // | EDX | arg2 |
| 1844 | // | ECX | arg1 |
| 1845 | // | EAX/Method* | <- sp |
| 1846 | DCHECK_EQ(32U, Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes()); |
| 1847 | uintptr_t* regs = reinterpret_cast<uintptr_t*>(reinterpret_cast<byte*>(sp)); |
| 1848 | uintptr_t caller_pc = regs[7]; |
| 1849 | #elif defined(__mips__) |
| 1850 | // On entry the stack pointed by sp is: |
| 1851 | // | argN | | |
| 1852 | // | ... | | |
| 1853 | // | arg4 | | |
| 1854 | // | arg3 spill | | Caller's frame |
| 1855 | // | arg2 spill | | |
| 1856 | // | arg1 spill | | |
| 1857 | // | Method* | --- |
| 1858 | // | RA | |
| 1859 | // | ... | callee saves |
| 1860 | // | A3 | arg3 |
| 1861 | // | A2 | arg2 |
| 1862 | // | A1 | arg1 |
| 1863 | // | A0/Method* | <- sp |
| 1864 | DCHECK_EQ(64U, Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes()); |
| 1865 | uintptr_t* regs = reinterpret_cast<uintptr_t*>(reinterpret_cast<byte*>(sp)); |
| 1866 | uintptr_t caller_pc = regs[15]; |
| 1867 | #else |
| 1868 | UNIMPLEMENTED(FATAL); |
| 1869 | uintptr_t caller_pc = 0; |
| 1870 | #endif |
| 1871 | uint32_t dex_pc = caller_method->ToDexPc(caller_pc); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame^] | 1872 | const DexFile::CodeItem* code = caller_method->GetCodeItem(); |
Mathieu Chartier | 5f3ded4 | 2014-04-03 15:25:30 -0700 | [diff] [blame] | 1873 | CHECK_LT(dex_pc, code->insns_size_in_code_units_); |
| 1874 | const Instruction* instr = Instruction::At(&code->insns_[dex_pc]); |
| 1875 | Instruction::Code instr_code = instr->Opcode(); |
| 1876 | CHECK(instr_code == Instruction::INVOKE_INTERFACE || |
| 1877 | instr_code == Instruction::INVOKE_INTERFACE_RANGE) |
| 1878 | << "Unexpected call into interface trampoline: " << instr->DumpString(NULL); |
| 1879 | uint32_t dex_method_idx; |
| 1880 | if (instr_code == Instruction::INVOKE_INTERFACE) { |
| 1881 | dex_method_idx = instr->VRegB_35c(); |
| 1882 | } else { |
| 1883 | DCHECK_EQ(instr_code, Instruction::INVOKE_INTERFACE_RANGE); |
| 1884 | dex_method_idx = instr->VRegB_3rc(); |
| 1885 | } |
| 1886 | |
| 1887 | const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()->GetDexFile(); |
| 1888 | uint32_t shorty_len; |
| 1889 | const char* shorty = |
| 1890 | dex_file->GetMethodShorty(dex_file->GetMethodId(dex_method_idx), &shorty_len); |
| 1891 | { |
| 1892 | // Remember the args in case a GC happens in FindMethodFromCode. |
| 1893 | ScopedObjectAccessUnchecked soa(self->GetJniEnv()); |
| 1894 | RememberForGcArgumentVisitor visitor(sp, false, shorty, shorty_len, &soa); |
| 1895 | visitor.VisitArguments(); |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 1896 | method = FindMethodFromCode<kInterface, false>(dex_method_idx, &this_object, &caller_method, |
Mathieu Chartier | 5f3ded4 | 2014-04-03 15:25:30 -0700 | [diff] [blame] | 1897 | self); |
| 1898 | visitor.FixupReferences(); |
| 1899 | } |
| 1900 | |
| 1901 | if (UNLIKELY(method == nullptr)) { |
| 1902 | CHECK(self->IsExceptionPending()); |
Andreas Gampe | 51f7635 | 2014-05-21 08:28:48 -0700 | [diff] [blame] | 1903 | return GetFailureValue(); // Failure. |
Mathieu Chartier | 5f3ded4 | 2014-04-03 15:25:30 -0700 | [diff] [blame] | 1904 | } |
| 1905 | } |
| 1906 | const void* code = method->GetEntryPointFromQuickCompiledCode(); |
| 1907 | |
| 1908 | // When we return, the caller will branch to this address, so it had better not be 0! |
| 1909 | DCHECK(code != nullptr) << "Code was NULL in method: " << PrettyMethod(method) << " location: " |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame^] | 1910 | << method->GetDexFile()->GetLocation(); |
Andreas Gampe | 51f7635 | 2014-05-21 08:28:48 -0700 | [diff] [blame] | 1911 | |
| 1912 | return GetSuccessValue(code, method); |
Mathieu Chartier | 5f3ded4 | 2014-04-03 15:25:30 -0700 | [diff] [blame] | 1913 | } |
| 1914 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1915 | } // namespace art |