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