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