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