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