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