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 | |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 663 | DCHECK(!method->IsNative()) << method->PrettyMethod(); |
Andreas Gampe | 639bdd1 | 2015-06-03 11:22:45 -0700 | [diff] [blame] | 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(); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 667 | DCHECK(code_item != nullptr) << method->PrettyMethod(); |
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 | } |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 682 | CHECK_EQ(method, linked->GetMethod()) << method->PrettyMethod() << " " |
| 683 | << ArtMethod::PrettyMethod(linked->GetMethod()); |
Andreas Gampe | 639bdd1 | 2015-06-03 11:22:45 -0700 | [diff] [blame] | 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)) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 746 | DCHECK(Thread::Current()->IsExceptionPending()) |
| 747 | << shadow_frame->GetMethod()->PrettyMethod(); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 748 | self->PopManagedStackFragment(fragment); |
| 749 | return 0; |
| 750 | } |
| 751 | } |
Daniel Mihalyi | eb07669 | 2014-08-22 17:33:31 +0200 | [diff] [blame] | 752 | |
Andreas Gampe | 639bdd1 | 2015-06-03 11:22:45 -0700 | [diff] [blame] | 753 | result = interpreter::EnterInterpreterFromEntryPoint(self, code_item, shadow_frame); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 754 | } |
Andreas Gampe | 639bdd1 | 2015-06-03 11:22:45 -0700 | [diff] [blame] | 755 | |
| 756 | // Pop transition. |
| 757 | self->PopManagedStackFragment(fragment); |
| 758 | |
| 759 | // Request a stack deoptimization if needed |
| 760 | ArtMethod* caller = QuickArgumentVisitor::GetCallingMethod(sp); |
Mingyao Yang | f711f2c | 2016-05-23 12:29:39 -0700 | [diff] [blame] | 761 | uintptr_t caller_pc = QuickArgumentVisitor::GetCallingPc(sp); |
Mingyao Yang | a3549d2 | 2016-06-02 17:01:02 -0700 | [diff] [blame] | 762 | // If caller_pc is the instrumentation exit stub, the stub will check to see if deoptimization |
| 763 | // should be done and it knows the real return pc. |
| 764 | if (UNLIKELY(caller_pc != reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) && |
| 765 | Dbg::IsForcedInterpreterNeededForUpcall(self, caller) && |
Mingyao Yang | f711f2c | 2016-05-23 12:29:39 -0700 | [diff] [blame] | 766 | Runtime::Current()->IsDeoptimizeable(caller_pc))) { |
Andreas Gampe | 639bdd1 | 2015-06-03 11:22:45 -0700 | [diff] [blame] | 767 | // Push the context of the deoptimization stack so we can restore the return value and the |
| 768 | // exception before executing the deoptimized frames. |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 769 | self->PushDeoptimizationContext( |
| 770 | result, shorty[0] == 'L', /* from_code */ false, self->GetException()); |
Andreas Gampe | 639bdd1 | 2015-06-03 11:22:45 -0700 | [diff] [blame] | 771 | |
| 772 | // Set special exception to cause deoptimization. |
| 773 | self->SetException(Thread::GetDeoptimizationException()); |
| 774 | } |
| 775 | |
| 776 | // No need to restore the args since the method has already been run by the interpreter. |
| 777 | return result.GetJ(); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | // Visits arguments on the stack placing them into the args vector, Object* arguments are converted |
| 781 | // to jobjects. |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 782 | class BuildQuickArgumentVisitor FINAL : public QuickArgumentVisitor { |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 783 | public: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 784 | 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] | 785 | ScopedObjectAccessUnchecked* soa, std::vector<jvalue>* args) : |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 786 | QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa), args_(args) {} |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 787 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 788 | void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE; |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 789 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 790 | void FixupReferences() REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | 5275bcb | 2014-02-20 17:16:42 -0800 | [diff] [blame] | 791 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 792 | private: |
Ian Rogers | 9758f79 | 2014-03-13 09:02:55 -0700 | [diff] [blame] | 793 | ScopedObjectAccessUnchecked* const soa_; |
| 794 | std::vector<jvalue>* const args_; |
Mathieu Chartier | 5275bcb | 2014-02-20 17:16:42 -0800 | [diff] [blame] | 795 | // 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] | 796 | std::vector<std::pair<jobject, StackReference<mirror::Object>*>> references_; |
Ian Rogers | 9758f79 | 2014-03-13 09:02:55 -0700 | [diff] [blame] | 797 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 798 | DISALLOW_COPY_AND_ASSIGN(BuildQuickArgumentVisitor); |
| 799 | }; |
| 800 | |
Ian Rogers | 9758f79 | 2014-03-13 09:02:55 -0700 | [diff] [blame] | 801 | void BuildQuickArgumentVisitor::Visit() { |
| 802 | jvalue val; |
| 803 | Primitive::Type type = GetParamPrimitiveType(); |
| 804 | switch (type) { |
| 805 | case Primitive::kPrimNot: { |
| 806 | StackReference<mirror::Object>* stack_ref = |
| 807 | reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress()); |
| 808 | val.l = soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr()); |
| 809 | references_.push_back(std::make_pair(val.l, stack_ref)); |
| 810 | break; |
| 811 | } |
| 812 | case Primitive::kPrimLong: // Fall-through. |
| 813 | case Primitive::kPrimDouble: |
| 814 | if (IsSplitLongOrDouble()) { |
| 815 | val.j = ReadSplitLongParam(); |
| 816 | } else { |
| 817 | val.j = *reinterpret_cast<jlong*>(GetParamAddress()); |
| 818 | } |
| 819 | break; |
| 820 | case Primitive::kPrimBoolean: // Fall-through. |
| 821 | case Primitive::kPrimByte: // Fall-through. |
| 822 | case Primitive::kPrimChar: // Fall-through. |
| 823 | case Primitive::kPrimShort: // Fall-through. |
| 824 | case Primitive::kPrimInt: // Fall-through. |
| 825 | case Primitive::kPrimFloat: |
| 826 | val.i = *reinterpret_cast<jint*>(GetParamAddress()); |
| 827 | break; |
| 828 | case Primitive::kPrimVoid: |
| 829 | LOG(FATAL) << "UNREACHABLE"; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 830 | UNREACHABLE(); |
Ian Rogers | 9758f79 | 2014-03-13 09:02:55 -0700 | [diff] [blame] | 831 | } |
| 832 | args_->push_back(val); |
| 833 | } |
| 834 | |
| 835 | void BuildQuickArgumentVisitor::FixupReferences() { |
| 836 | // Fixup any references which may have changed. |
| 837 | for (const auto& pair : references_) { |
Mathieu Chartier | 1a5337f | 2016-10-13 13:48:23 -0700 | [diff] [blame] | 838 | pair.second->Assign(soa_->Decode<mirror::Object>(pair.first)); |
Mathieu Chartier | 5f3ded4 | 2014-04-03 15:25:30 -0700 | [diff] [blame] | 839 | soa_->Env()->DeleteLocalRef(pair.first); |
Ian Rogers | 9758f79 | 2014-03-13 09:02:55 -0700 | [diff] [blame] | 840 | } |
| 841 | } |
| 842 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 843 | // Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method |
| 844 | // which is responsible for recording callee save registers. We explicitly place into jobjects the |
| 845 | // incoming reference arguments (so they survive GC). We invoke the invocation handler, which is a |
| 846 | // 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] | 847 | extern "C" uint64_t artQuickProxyInvokeHandler( |
| 848 | ArtMethod* proxy_method, mirror::Object* receiver, Thread* self, ArtMethod** sp) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 849 | REQUIRES_SHARED(Locks::mutator_lock_) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 850 | DCHECK(proxy_method->IsProxyMethod()) << proxy_method->PrettyMethod(); |
| 851 | DCHECK(receiver->GetClass()->IsProxyClass()) << proxy_method->PrettyMethod(); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 852 | // Ensure we don't get thread suspension until the object arguments are safely in jobjects. |
| 853 | const char* old_cause = |
| 854 | self->StartAssertNoThreadSuspension("Adding to IRT proxy object arguments"); |
| 855 | // Register the top of the managed stack, making stack crawlable. |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 856 | DCHECK_EQ((*sp), proxy_method) << proxy_method->PrettyMethod(); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 857 | self->VerifyStack(); |
| 858 | // Start new JNI local reference state. |
| 859 | JNIEnvExt* env = self->GetJniEnv(); |
| 860 | ScopedObjectAccessUnchecked soa(env); |
| 861 | ScopedJniEnvLocalRefState env_state(env); |
| 862 | // Create local ref. copies of proxy method and the receiver. |
| 863 | jobject rcvr_jobj = soa.AddLocalReference<jobject>(receiver); |
| 864 | |
| 865 | // Placing arguments into args vector and remove the receiver. |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 866 | ArtMethod* non_proxy_method = proxy_method->GetInterfaceMethodIfProxy(kRuntimePointerSize); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 867 | CHECK(!non_proxy_method->IsStatic()) << proxy_method->PrettyMethod() << " " |
| 868 | << non_proxy_method->PrettyMethod(); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 869 | std::vector<jvalue> args; |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 870 | uint32_t shorty_len = 0; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 871 | const char* shorty = non_proxy_method->GetShorty(&shorty_len); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 872 | BuildQuickArgumentVisitor local_ref_visitor(sp, false, shorty, shorty_len, &soa, &args); |
Brian Carlstrom | d3633d5 | 2013-08-20 21:06:26 -0700 | [diff] [blame] | 873 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 874 | local_ref_visitor.VisitArguments(); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 875 | DCHECK_GT(args.size(), 0U) << proxy_method->PrettyMethod(); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 876 | args.erase(args.begin()); |
| 877 | |
| 878 | // Convert proxy method into expected interface method. |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 879 | ArtMethod* interface_method = proxy_method->FindOverriddenMethod(kRuntimePointerSize); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 880 | DCHECK(interface_method != nullptr) << proxy_method->PrettyMethod(); |
| 881 | DCHECK(!interface_method->IsProxyMethod()) << interface_method->PrettyMethod(); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 882 | self->EndAssertNoThreadSuspension(old_cause); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 883 | DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize); |
Andreas Gampe | e01e364 | 2016-07-25 13:06:04 -0700 | [diff] [blame] | 884 | DCHECK(!Runtime::Current()->IsActiveTransaction()); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 885 | jobject interface_method_jobj = soa.AddLocalReference<jobject>( |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 886 | mirror::Method::CreateFromArtMethod<kRuntimePointerSize, false>(soa.Self(), |
| 887 | interface_method)); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 888 | |
| 889 | // All naked Object*s should now be in jobjects, so its safe to go into the main invoke code |
| 890 | // that performs allocations. |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 891 | JValue result = InvokeProxyInvocationHandler(soa, shorty, rcvr_jobj, interface_method_jobj, args); |
Mathieu Chartier | 5275bcb | 2014-02-20 17:16:42 -0800 | [diff] [blame] | 892 | // Restore references which might have moved. |
| 893 | local_ref_visitor.FixupReferences(); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 894 | return result.GetJ(); |
| 895 | } |
| 896 | |
| 897 | // Read object references held in arguments from quick frames and place in a JNI local references, |
| 898 | // so they don't get garbage collected. |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 899 | class RememberForGcArgumentVisitor FINAL : public QuickArgumentVisitor { |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 900 | public: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 901 | RememberForGcArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty, |
| 902 | uint32_t shorty_len, ScopedObjectAccessUnchecked* soa) : |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 903 | QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa) {} |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 904 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 905 | void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE; |
Mathieu Chartier | 07d447b | 2013-09-26 11:57:43 -0700 | [diff] [blame] | 906 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 907 | void FixupReferences() REQUIRES_SHARED(Locks::mutator_lock_); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 908 | |
| 909 | private: |
Ian Rogers | 9758f79 | 2014-03-13 09:02:55 -0700 | [diff] [blame] | 910 | ScopedObjectAccessUnchecked* const soa_; |
Mathieu Chartier | 5275bcb | 2014-02-20 17:16:42 -0800 | [diff] [blame] | 911 | // 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] | 912 | std::vector<std::pair<jobject, StackReference<mirror::Object>*> > references_; |
| 913 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 914 | DISALLOW_COPY_AND_ASSIGN(RememberForGcArgumentVisitor); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 915 | }; |
| 916 | |
Ian Rogers | 9758f79 | 2014-03-13 09:02:55 -0700 | [diff] [blame] | 917 | void RememberForGcArgumentVisitor::Visit() { |
| 918 | if (IsParamAReference()) { |
| 919 | StackReference<mirror::Object>* stack_ref = |
| 920 | reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress()); |
| 921 | jobject reference = |
| 922 | soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr()); |
| 923 | references_.push_back(std::make_pair(reference, stack_ref)); |
| 924 | } |
| 925 | } |
| 926 | |
| 927 | void RememberForGcArgumentVisitor::FixupReferences() { |
| 928 | // Fixup any references which may have changed. |
| 929 | for (const auto& pair : references_) { |
Mathieu Chartier | 1a5337f | 2016-10-13 13:48:23 -0700 | [diff] [blame] | 930 | pair.second->Assign(soa_->Decode<mirror::Object>(pair.first)); |
Mathieu Chartier | 5f3ded4 | 2014-04-03 15:25:30 -0700 | [diff] [blame] | 931 | soa_->Env()->DeleteLocalRef(pair.first); |
Ian Rogers | 9758f79 | 2014-03-13 09:02:55 -0700 | [diff] [blame] | 932 | } |
| 933 | } |
| 934 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 935 | // Lazily resolve a method for quick. Called by stub code. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 936 | extern "C" const void* artQuickResolutionTrampoline( |
| 937 | ArtMethod* called, mirror::Object* receiver, Thread* self, ArtMethod** sp) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 938 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Andreas Gampe | 3b45ef2 | 2015-05-26 21:34:09 -0700 | [diff] [blame] | 939 | // The resolution trampoline stashes the resolved method into the callee-save frame to transport |
| 940 | // it. Thus, when exiting, the stack cannot be verified (as the resolved method most likely |
| 941 | // does not have the same stack layout as the callee-save method). |
| 942 | ScopedQuickEntrypointChecks sqec(self, kIsDebugBuild, false); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 943 | // Start new JNI local reference state |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 944 | JNIEnvExt* env = self->GetJniEnv(); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 945 | ScopedObjectAccessUnchecked soa(env); |
| 946 | ScopedJniEnvLocalRefState env_state(env); |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 947 | const char* old_cause = self->StartAssertNoThreadSuspension("Quick method resolution set up"); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 948 | |
| 949 | // Compute details about the called method (avoid GCs) |
| 950 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 951 | InvokeType invoke_type; |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 952 | MethodReference called_method(nullptr, 0); |
| 953 | const bool called_method_known_on_entry = !called->IsRuntimeMethod(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 954 | ArtMethod* caller = nullptr; |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 955 | if (!called_method_known_on_entry) { |
Nicolas Geoffray | 7ea6a17 | 2015-05-19 18:58:54 +0100 | [diff] [blame] | 956 | caller = QuickArgumentVisitor::GetCallingMethod(sp); |
| 957 | uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 958 | const DexFile::CodeItem* code; |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 959 | called_method.dex_file = caller->GetDexFile(); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 960 | code = caller->GetCodeItem(); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 961 | CHECK_LT(dex_pc, code->insns_size_in_code_units_); |
| 962 | const Instruction* instr = Instruction::At(&code->insns_[dex_pc]); |
| 963 | Instruction::Code instr_code = instr->Opcode(); |
| 964 | bool is_range; |
| 965 | switch (instr_code) { |
| 966 | case Instruction::INVOKE_DIRECT: |
| 967 | invoke_type = kDirect; |
| 968 | is_range = false; |
| 969 | break; |
| 970 | case Instruction::INVOKE_DIRECT_RANGE: |
| 971 | invoke_type = kDirect; |
| 972 | is_range = true; |
| 973 | break; |
| 974 | case Instruction::INVOKE_STATIC: |
| 975 | invoke_type = kStatic; |
| 976 | is_range = false; |
| 977 | break; |
| 978 | case Instruction::INVOKE_STATIC_RANGE: |
| 979 | invoke_type = kStatic; |
| 980 | is_range = true; |
| 981 | break; |
| 982 | case Instruction::INVOKE_SUPER: |
| 983 | invoke_type = kSuper; |
| 984 | is_range = false; |
| 985 | break; |
| 986 | case Instruction::INVOKE_SUPER_RANGE: |
| 987 | invoke_type = kSuper; |
| 988 | is_range = true; |
| 989 | break; |
| 990 | case Instruction::INVOKE_VIRTUAL: |
| 991 | invoke_type = kVirtual; |
| 992 | is_range = false; |
| 993 | break; |
| 994 | case Instruction::INVOKE_VIRTUAL_RANGE: |
| 995 | invoke_type = kVirtual; |
| 996 | is_range = true; |
| 997 | break; |
| 998 | case Instruction::INVOKE_INTERFACE: |
| 999 | invoke_type = kInterface; |
| 1000 | is_range = false; |
| 1001 | break; |
| 1002 | case Instruction::INVOKE_INTERFACE_RANGE: |
| 1003 | invoke_type = kInterface; |
| 1004 | is_range = true; |
| 1005 | break; |
| 1006 | default: |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 1007 | LOG(FATAL) << "Unexpected call into trampoline: " << instr->DumpString(nullptr); |
| 1008 | UNREACHABLE(); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1009 | } |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 1010 | 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] | 1011 | } else { |
| 1012 | invoke_type = kStatic; |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 1013 | called_method.dex_file = called->GetDexFile(); |
| 1014 | called_method.dex_method_index = called->GetDexMethodIndex(); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1015 | } |
| 1016 | uint32_t shorty_len; |
| 1017 | const char* shorty = |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 1018 | called_method.dex_file->GetMethodShorty( |
| 1019 | called_method.dex_file->GetMethodId(called_method.dex_method_index), &shorty_len); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1020 | RememberForGcArgumentVisitor visitor(sp, invoke_type == kStatic, shorty, shorty_len, &soa); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1021 | visitor.VisitArguments(); |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1022 | self->EndAssertNoThreadSuspension(old_cause); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 1023 | const bool virtual_or_interface = invoke_type == kVirtual || invoke_type == kInterface; |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1024 | // Resolve method filling in dex cache. |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 1025 | if (!called_method_known_on_entry) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1026 | StackHandleScope<1> hs(self); |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 1027 | mirror::Object* dummy = nullptr; |
| 1028 | HandleWrapper<mirror::Object> h_receiver( |
| 1029 | hs.NewHandleWrapper(virtual_or_interface ? &receiver : &dummy)); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 1030 | DCHECK_EQ(caller->GetDexFile(), called_method.dex_file); |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 1031 | called = linker->ResolveMethod<ClassLinker::kForceICCECheck>( |
| 1032 | self, called_method.dex_method_index, caller, invoke_type); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1033 | } |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 1034 | const void* code = nullptr; |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1035 | if (LIKELY(!self->IsExceptionPending())) { |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1036 | // Incompatible class change should have been handled in resolve method. |
Brian Carlstrom | 2ec6520 | 2014-03-03 15:16:37 -0800 | [diff] [blame] | 1037 | CHECK(!called->CheckIncompatibleClassChange(invoke_type)) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1038 | << called->PrettyMethod() << " " << invoke_type; |
Nicolas Geoffray | e523423 | 2015-12-02 09:06:11 +0000 | [diff] [blame] | 1039 | if (virtual_or_interface || invoke_type == kSuper) { |
| 1040 | // Refine called method based on receiver for kVirtual/kInterface, and |
| 1041 | // caller for kSuper. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1042 | ArtMethod* orig_called = called; |
Mathieu Chartier | 55871bf | 2014-02-27 10:24:50 -0800 | [diff] [blame] | 1043 | if (invoke_type == kVirtual) { |
Nicolas Geoffray | e523423 | 2015-12-02 09:06:11 +0000 | [diff] [blame] | 1044 | CHECK(receiver != nullptr) << invoke_type; |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1045 | called = receiver->GetClass()->FindVirtualMethodForVirtual(called, kRuntimePointerSize); |
Nicolas Geoffray | e523423 | 2015-12-02 09:06:11 +0000 | [diff] [blame] | 1046 | } else if (invoke_type == kInterface) { |
| 1047 | CHECK(receiver != nullptr) << invoke_type; |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1048 | called = receiver->GetClass()->FindVirtualMethodForInterface(called, kRuntimePointerSize); |
Nicolas Geoffray | e523423 | 2015-12-02 09:06:11 +0000 | [diff] [blame] | 1049 | } else { |
| 1050 | DCHECK_EQ(invoke_type, kSuper); |
| 1051 | CHECK(caller != nullptr) << invoke_type; |
Nicolas Geoffray | 393fdb8 | 2016-04-25 14:58:06 +0100 | [diff] [blame] | 1052 | StackHandleScope<2> hs(self); |
| 1053 | Handle<mirror::DexCache> dex_cache( |
| 1054 | hs.NewHandle(caller->GetDeclaringClass()->GetDexCache())); |
| 1055 | Handle<mirror::ClassLoader> class_loader( |
| 1056 | hs.NewHandle(caller->GetDeclaringClass()->GetClassLoader())); |
Alex Light | fedd91d | 2016-01-07 14:49:16 -0800 | [diff] [blame] | 1057 | // TODO Maybe put this into a mirror::Class function. |
| 1058 | mirror::Class* ref_class = linker->ResolveReferencedClassOfMethod( |
Nicolas Geoffray | 393fdb8 | 2016-04-25 14:58:06 +0100 | [diff] [blame] | 1059 | called_method.dex_method_index, dex_cache, class_loader); |
Alex Light | fedd91d | 2016-01-07 14:49:16 -0800 | [diff] [blame] | 1060 | if (ref_class->IsInterface()) { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1061 | called = ref_class->FindVirtualMethodForInterfaceSuper(called, kRuntimePointerSize); |
Alex Light | fedd91d | 2016-01-07 14:49:16 -0800 | [diff] [blame] | 1062 | } else { |
| 1063 | called = caller->GetDeclaringClass()->GetSuperClass()->GetVTableEntry( |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1064 | called->GetMethodIndex(), kRuntimePointerSize); |
Alex Light | fedd91d | 2016-01-07 14:49:16 -0800 | [diff] [blame] | 1065 | } |
Mathieu Chartier | 55871bf | 2014-02-27 10:24:50 -0800 | [diff] [blame] | 1066 | } |
Mingyao Yang | f486778 | 2014-05-05 11:55:02 -0700 | [diff] [blame] | 1067 | |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1068 | CHECK(called != nullptr) << orig_called->PrettyMethod() << " " |
| 1069 | << mirror::Object::PrettyTypeOf(receiver) << " " |
Mingyao Yang | f486778 | 2014-05-05 11:55:02 -0700 | [diff] [blame] | 1070 | << invoke_type << " " << orig_called->GetVtableIndex(); |
| 1071 | |
Ian Rogers | 83883d7 | 2013-10-21 21:07:24 -0700 | [diff] [blame] | 1072 | // 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] | 1073 | // of the sharpened method avoiding dirtying the dex cache if possible. |
Ian Rogers | 00f1527 | 2014-12-02 16:55:46 -0800 | [diff] [blame] | 1074 | // Note, called_method.dex_method_index references the dex method before the |
| 1075 | // FindVirtualMethodFor... This is ok for FindDexMethodIndexInOtherDexFile that only cares |
| 1076 | // about the name and signature. |
| 1077 | uint32_t update_dex_cache_method_index = called->GetDexMethodIndex(); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1078 | if (!called->HasSameDexCacheResolvedMethods(caller, kRuntimePointerSize)) { |
Ian Rogers | 83883d7 | 2013-10-21 21:07:24 -0700 | [diff] [blame] | 1079 | // 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] | 1080 | // the caller's dex file. Since we get here only if the original called was a runtime |
| 1081 | // 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] | 1082 | DCHECK(!called_method_known_on_entry); |
| 1083 | DCHECK_EQ(caller->GetDexFile(), called_method.dex_file); |
| 1084 | const DexFile* caller_dex_file = called_method.dex_file; |
| 1085 | uint32_t caller_method_name_and_sig_index = called_method.dex_method_index; |
| 1086 | update_dex_cache_method_index = |
| 1087 | called->FindDexMethodIndexInOtherDexFile(*caller_dex_file, |
| 1088 | caller_method_name_and_sig_index); |
| 1089 | } |
| 1090 | if ((update_dex_cache_method_index != DexFile::kDexNoIndex) && |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1091 | (caller->GetDexCacheResolvedMethod( |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1092 | update_dex_cache_method_index, kRuntimePointerSize) != called)) { |
| 1093 | caller->SetDexCacheResolvedMethod(update_dex_cache_method_index, |
| 1094 | called, |
| 1095 | kRuntimePointerSize); |
Ian Rogers | 83883d7 | 2013-10-21 21:07:24 -0700 | [diff] [blame] | 1096 | } |
Mathieu Chartier | e4a91bb | 2015-01-28 13:11:44 -0800 | [diff] [blame] | 1097 | } else if (invoke_type == kStatic) { |
| 1098 | const auto called_dex_method_idx = called->GetDexMethodIndex(); |
| 1099 | // For static invokes, we may dispatch to the static method in the superclass but resolve |
| 1100 | // using the subclass. To prevent getting slow paths on each invoke, we force set the |
| 1101 | // resolved method for the super class dex method index if we are in the same dex file. |
| 1102 | // b/19175856 |
| 1103 | if (called->GetDexFile() == called_method.dex_file && |
| 1104 | called_method.dex_method_index != called_dex_method_idx) { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1105 | called->GetDexCache()->SetResolvedMethod(called_dex_method_idx, |
| 1106 | called, |
| 1107 | kRuntimePointerSize); |
Mathieu Chartier | e4a91bb | 2015-01-28 13:11:44 -0800 | [diff] [blame] | 1108 | } |
Ian Rogers | 83883d7 | 2013-10-21 21:07:24 -0700 | [diff] [blame] | 1109 | } |
Daniel Mihalyi | eb07669 | 2014-08-22 17:33:31 +0200 | [diff] [blame] | 1110 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1111 | // Ensure that the called method's class is initialized. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1112 | StackHandleScope<1> hs(soa.Self()); |
| 1113 | Handle<mirror::Class> called_class(hs.NewHandle(called->GetDeclaringClass())); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1114 | linker->EnsureInitialized(soa.Self(), called_class, true, true); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1115 | if (LIKELY(called_class->IsInitialized())) { |
Daniel Mihalyi | eb07669 | 2014-08-22 17:33:31 +0200 | [diff] [blame] | 1116 | if (UNLIKELY(Dbg::IsForcedInterpreterNeededForResolution(self, called))) { |
| 1117 | // If we are single-stepping or the called method is deoptimized (by a |
| 1118 | // breakpoint, for example), then we have to execute the called method |
| 1119 | // with the interpreter. |
| 1120 | code = GetQuickToInterpreterBridge(); |
| 1121 | } else if (UNLIKELY(Dbg::IsForcedInstrumentationNeededForResolution(self, caller))) { |
| 1122 | // If the caller is deoptimized (by a breakpoint, for example), we have to |
| 1123 | // continue its execution with interpreter when returning from the called |
| 1124 | // method. Because we do not want to execute the called method with the |
| 1125 | // interpreter, we wrap its execution into the instrumentation stubs. |
| 1126 | // When the called method returns, it will execute the instrumentation |
| 1127 | // exit hook that will determine the need of the interpreter with a call |
| 1128 | // to Dbg::IsForcedInterpreterNeededForUpcall and deoptimize the stack if |
| 1129 | // it is needed. |
| 1130 | code = GetQuickInstrumentationEntryPoint(); |
| 1131 | } else { |
| 1132 | code = called->GetEntryPointFromQuickCompiledCode(); |
| 1133 | } |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1134 | } else if (called_class->IsInitializing()) { |
Daniel Mihalyi | eb07669 | 2014-08-22 17:33:31 +0200 | [diff] [blame] | 1135 | if (UNLIKELY(Dbg::IsForcedInterpreterNeededForResolution(self, called))) { |
| 1136 | // If we are single-stepping or the called method is deoptimized (by a |
| 1137 | // breakpoint, for example), then we have to execute the called method |
| 1138 | // with the interpreter. |
| 1139 | code = GetQuickToInterpreterBridge(); |
| 1140 | } else if (invoke_type == kStatic) { |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1141 | // Class is still initializing, go to oat and grab code (trampoline must be left in place |
| 1142 | // until class is initialized to stop races between threads). |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1143 | code = linker->GetQuickOatCodeFor(called); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1144 | } else { |
| 1145 | // No trampoline for non-static methods. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1146 | code = called->GetEntryPointFromQuickCompiledCode(); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1147 | } |
| 1148 | } else { |
| 1149 | DCHECK(called_class->IsErroneous()); |
| 1150 | } |
| 1151 | } |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 1152 | CHECK_EQ(code == nullptr, self->IsExceptionPending()); |
Mathieu Chartier | 07d447b | 2013-09-26 11:57:43 -0700 | [diff] [blame] | 1153 | // Fixup any locally saved objects may have moved during a GC. |
| 1154 | visitor.FixupReferences(); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1155 | // 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] | 1156 | *sp = called; |
| 1157 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1158 | return code; |
| 1159 | } |
| 1160 | |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1161 | /* |
| 1162 | * This class uses a couple of observations to unite the different calling conventions through |
| 1163 | * a few constants. |
| 1164 | * |
| 1165 | * 1) Number of registers used for passing is normally even, so counting down has no penalty for |
| 1166 | * possible alignment. |
| 1167 | * 2) Known 64b architectures store 8B units on the stack, both for integral and floating point |
| 1168 | * types, so using uintptr_t is OK. Also means that we can use kRegistersNeededX to denote |
| 1169 | * when we have to split things |
| 1170 | * 3) The only soft-float, Arm, is 32b, so no widening needs to be taken into account for floats |
| 1171 | * and we can use Int handling directly. |
| 1172 | * 4) Only 64b architectures widen, and their stack is aligned 8B anyways, so no padding code |
| 1173 | * necessary when widening. Also, widening of Ints will take place implicitly, and the |
| 1174 | * extension should be compatible with Aarch64, which mandates copying the available bits |
| 1175 | * into LSB and leaving the rest unspecified. |
| 1176 | * 5) Aligning longs and doubles is necessary on arm only, and it's the same in registers and on |
| 1177 | * the stack. |
| 1178 | * 6) There is only little endian. |
| 1179 | * |
| 1180 | * |
| 1181 | * Actual work is supposed to be done in a delegate of the template type. The interface is as |
| 1182 | * follows: |
| 1183 | * |
| 1184 | * void PushGpr(uintptr_t): Add a value for the next GPR |
| 1185 | * |
| 1186 | * void PushFpr4(float): Add a value for the next FPR of size 32b. Is only called if we need |
| 1187 | * padding, that is, think the architecture is 32b and aligns 64b. |
| 1188 | * |
| 1189 | * void PushFpr8(uint64_t): Push a double. We _will_ call this on 32b, it's the callee's job to |
| 1190 | * split this if necessary. The current state will have aligned, if |
| 1191 | * necessary. |
| 1192 | * |
| 1193 | * void PushStack(uintptr_t): Push a value to the stack. |
| 1194 | * |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1195 | * 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] | 1196 | * as this might be important for null initialization. |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1197 | * Must return the jobject, that is, the reference to the |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1198 | * entry in the HandleScope (nullptr if necessary). |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1199 | * |
| 1200 | */ |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1201 | template<class T> class BuildNativeCallFrameStateMachine { |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1202 | public: |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1203 | #if defined(__arm__) |
| 1204 | // TODO: These are all dummy values! |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1205 | static constexpr bool kNativeSoftFloatAbi = true; |
| 1206 | 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] | 1207 | static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs. |
| 1208 | |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1209 | static constexpr size_t kRegistersNeededForLong = 2; |
| 1210 | static constexpr size_t kRegistersNeededForDouble = 2; |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1211 | static constexpr bool kMultiRegistersAligned = true; |
Andreas Gampe | 1a5c406 | 2015-01-15 12:10:47 -0800 | [diff] [blame] | 1212 | static constexpr bool kMultiFPRegistersWidened = false; |
| 1213 | static constexpr bool kMultiGPRegistersWidened = false; |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1214 | static constexpr bool kAlignLongOnStack = true; |
| 1215 | static constexpr bool kAlignDoubleOnStack = true; |
Stuart Monteith | b95a534 | 2014-03-12 13:32:32 +0000 | [diff] [blame] | 1216 | #elif defined(__aarch64__) |
| 1217 | static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI. |
| 1218 | static constexpr size_t kNumNativeGprArgs = 8; // 6 arguments passed in GPRs. |
| 1219 | static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs. |
| 1220 | |
| 1221 | static constexpr size_t kRegistersNeededForLong = 1; |
| 1222 | static constexpr size_t kRegistersNeededForDouble = 1; |
| 1223 | static constexpr bool kMultiRegistersAligned = false; |
Andreas Gampe | 1a5c406 | 2015-01-15 12:10:47 -0800 | [diff] [blame] | 1224 | static constexpr bool kMultiFPRegistersWidened = false; |
| 1225 | static constexpr bool kMultiGPRegistersWidened = false; |
Stuart Monteith | b95a534 | 2014-03-12 13:32:32 +0000 | [diff] [blame] | 1226 | static constexpr bool kAlignLongOnStack = false; |
| 1227 | static constexpr bool kAlignDoubleOnStack = false; |
Andreas Gampe | 1a5c406 | 2015-01-15 12:10:47 -0800 | [diff] [blame] | 1228 | #elif defined(__mips__) && !defined(__LP64__) |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1229 | static constexpr bool kNativeSoftFloatAbi = true; // This is a hard float ABI. |
Douglas Leung | 735b855 | 2014-10-31 12:21:40 -0700 | [diff] [blame] | 1230 | static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs. |
| 1231 | static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs. |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1232 | |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1233 | static constexpr size_t kRegistersNeededForLong = 2; |
| 1234 | static constexpr size_t kRegistersNeededForDouble = 2; |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1235 | static constexpr bool kMultiRegistersAligned = true; |
Andreas Gampe | 1a5c406 | 2015-01-15 12:10:47 -0800 | [diff] [blame] | 1236 | static constexpr bool kMultiFPRegistersWidened = true; |
| 1237 | static constexpr bool kMultiGPRegistersWidened = false; |
Douglas Leung | 735b855 | 2014-10-31 12:21:40 -0700 | [diff] [blame] | 1238 | static constexpr bool kAlignLongOnStack = true; |
| 1239 | static constexpr bool kAlignDoubleOnStack = true; |
Andreas Gampe | 1a5c406 | 2015-01-15 12:10:47 -0800 | [diff] [blame] | 1240 | #elif defined(__mips__) && defined(__LP64__) |
| 1241 | // Let the code prepare GPRs only and we will load the FPRs with same data. |
| 1242 | static constexpr bool kNativeSoftFloatAbi = true; |
| 1243 | static constexpr size_t kNumNativeGprArgs = 8; |
| 1244 | static constexpr size_t kNumNativeFprArgs = 0; |
| 1245 | |
| 1246 | static constexpr size_t kRegistersNeededForLong = 1; |
| 1247 | static constexpr size_t kRegistersNeededForDouble = 1; |
| 1248 | static constexpr bool kMultiRegistersAligned = false; |
| 1249 | static constexpr bool kMultiFPRegistersWidened = false; |
| 1250 | static constexpr bool kMultiGPRegistersWidened = true; |
| 1251 | static constexpr bool kAlignLongOnStack = false; |
| 1252 | static constexpr bool kAlignDoubleOnStack = false; |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1253 | #elif defined(__i386__) |
| 1254 | // TODO: Check these! |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1255 | static constexpr bool kNativeSoftFloatAbi = false; // Not using int registers for fp |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1256 | static constexpr size_t kNumNativeGprArgs = 0; // 6 arguments passed in GPRs. |
| 1257 | static constexpr size_t kNumNativeFprArgs = 0; // 8 arguments passed in FPRs. |
| 1258 | |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1259 | static constexpr size_t kRegistersNeededForLong = 2; |
| 1260 | static constexpr size_t kRegistersNeededForDouble = 2; |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1261 | static constexpr bool kMultiRegistersAligned = false; // x86 not using regs, anyways |
Andreas Gampe | 1a5c406 | 2015-01-15 12:10:47 -0800 | [diff] [blame] | 1262 | static constexpr bool kMultiFPRegistersWidened = false; |
| 1263 | static constexpr bool kMultiGPRegistersWidened = false; |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1264 | static constexpr bool kAlignLongOnStack = false; |
| 1265 | static constexpr bool kAlignDoubleOnStack = false; |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1266 | #elif defined(__x86_64__) |
| 1267 | static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI. |
| 1268 | static constexpr size_t kNumNativeGprArgs = 6; // 6 arguments passed in GPRs. |
| 1269 | static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs. |
| 1270 | |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1271 | static constexpr size_t kRegistersNeededForLong = 1; |
| 1272 | static constexpr size_t kRegistersNeededForDouble = 1; |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1273 | static constexpr bool kMultiRegistersAligned = false; |
Andreas Gampe | 1a5c406 | 2015-01-15 12:10:47 -0800 | [diff] [blame] | 1274 | static constexpr bool kMultiFPRegistersWidened = false; |
| 1275 | static constexpr bool kMultiGPRegistersWidened = false; |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1276 | static constexpr bool kAlignLongOnStack = false; |
| 1277 | static constexpr bool kAlignDoubleOnStack = false; |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1278 | #else |
| 1279 | #error "Unsupported architecture" |
| 1280 | #endif |
| 1281 | |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1282 | public: |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1283 | explicit BuildNativeCallFrameStateMachine(T* delegate) |
| 1284 | : gpr_index_(kNumNativeGprArgs), |
| 1285 | fpr_index_(kNumNativeFprArgs), |
| 1286 | stack_entries_(0), |
| 1287 | delegate_(delegate) { |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1288 | // For register alignment, we want to assume that counters (gpr_index_, fpr_index_) are even iff |
| 1289 | // 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] | 1290 | static_assert(kNumNativeGprArgs % 2 == 0U, "Number of native GPR arguments not even"); |
| 1291 | static_assert(kNumNativeFprArgs % 2 == 0U, "Number of native FPR arguments not even"); |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1292 | } |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1293 | |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1294 | virtual ~BuildNativeCallFrameStateMachine() {} |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1295 | |
Ian Rogers | 1428dce | 2014-10-21 15:02:15 -0700 | [diff] [blame] | 1296 | bool HavePointerGpr() const { |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1297 | return gpr_index_ > 0; |
| 1298 | } |
| 1299 | |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1300 | void AdvancePointer(const void* val) { |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1301 | if (HavePointerGpr()) { |
| 1302 | gpr_index_--; |
| 1303 | PushGpr(reinterpret_cast<uintptr_t>(val)); |
| 1304 | } else { |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1305 | 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] | 1306 | PushStack(reinterpret_cast<uintptr_t>(val)); |
| 1307 | gpr_index_ = 0; |
| 1308 | } |
| 1309 | } |
| 1310 | |
Ian Rogers | 1428dce | 2014-10-21 15:02:15 -0700 | [diff] [blame] | 1311 | bool HaveHandleScopeGpr() const { |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1312 | return gpr_index_ > 0; |
| 1313 | } |
| 1314 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1315 | void AdvanceHandleScope(mirror::Object* ptr) REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1316 | uintptr_t handle = PushHandle(ptr); |
| 1317 | if (HaveHandleScopeGpr()) { |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1318 | gpr_index_--; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1319 | PushGpr(handle); |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1320 | } else { |
| 1321 | stack_entries_++; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1322 | PushStack(handle); |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1323 | gpr_index_ = 0; |
| 1324 | } |
| 1325 | } |
| 1326 | |
Ian Rogers | 1428dce | 2014-10-21 15:02:15 -0700 | [diff] [blame] | 1327 | bool HaveIntGpr() const { |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1328 | return gpr_index_ > 0; |
| 1329 | } |
| 1330 | |
| 1331 | void AdvanceInt(uint32_t val) { |
| 1332 | if (HaveIntGpr()) { |
| 1333 | gpr_index_--; |
Andreas Gampe | 1a5c406 | 2015-01-15 12:10:47 -0800 | [diff] [blame] | 1334 | if (kMultiGPRegistersWidened) { |
| 1335 | DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t)); |
Roland Levillain | da4d79b | 2015-03-24 14:36:11 +0000 | [diff] [blame] | 1336 | PushGpr(static_cast<int64_t>(bit_cast<int32_t, uint32_t>(val))); |
Andreas Gampe | 1a5c406 | 2015-01-15 12:10:47 -0800 | [diff] [blame] | 1337 | } else { |
| 1338 | PushGpr(val); |
| 1339 | } |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1340 | } else { |
| 1341 | stack_entries_++; |
Andreas Gampe | 1a5c406 | 2015-01-15 12:10:47 -0800 | [diff] [blame] | 1342 | if (kMultiGPRegistersWidened) { |
| 1343 | DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t)); |
Roland Levillain | da4d79b | 2015-03-24 14:36:11 +0000 | [diff] [blame] | 1344 | PushStack(static_cast<int64_t>(bit_cast<int32_t, uint32_t>(val))); |
Andreas Gampe | 1a5c406 | 2015-01-15 12:10:47 -0800 | [diff] [blame] | 1345 | } else { |
| 1346 | PushStack(val); |
| 1347 | } |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1348 | gpr_index_ = 0; |
| 1349 | } |
| 1350 | } |
| 1351 | |
Ian Rogers | 1428dce | 2014-10-21 15:02:15 -0700 | [diff] [blame] | 1352 | bool HaveLongGpr() const { |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1353 | return gpr_index_ >= kRegistersNeededForLong + (LongGprNeedsPadding() ? 1 : 0); |
| 1354 | } |
| 1355 | |
Ian Rogers | 1428dce | 2014-10-21 15:02:15 -0700 | [diff] [blame] | 1356 | bool LongGprNeedsPadding() const { |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1357 | return kRegistersNeededForLong > 1 && // only pad when using multiple registers |
| 1358 | kAlignLongOnStack && // and when it needs alignment |
| 1359 | (gpr_index_ & 1) == 1; // counter is odd, see constructor |
| 1360 | } |
| 1361 | |
Ian Rogers | 1428dce | 2014-10-21 15:02:15 -0700 | [diff] [blame] | 1362 | bool LongStackNeedsPadding() const { |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1363 | return kRegistersNeededForLong > 1 && // only pad when using multiple registers |
| 1364 | kAlignLongOnStack && // and when it needs 8B alignment |
| 1365 | (stack_entries_ & 1) == 1; // counter is odd |
| 1366 | } |
| 1367 | |
| 1368 | void AdvanceLong(uint64_t val) { |
| 1369 | if (HaveLongGpr()) { |
| 1370 | if (LongGprNeedsPadding()) { |
| 1371 | PushGpr(0); |
| 1372 | gpr_index_--; |
| 1373 | } |
| 1374 | if (kRegistersNeededForLong == 1) { |
| 1375 | PushGpr(static_cast<uintptr_t>(val)); |
| 1376 | } else { |
| 1377 | PushGpr(static_cast<uintptr_t>(val & 0xFFFFFFFF)); |
| 1378 | PushGpr(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF)); |
| 1379 | } |
| 1380 | gpr_index_ -= kRegistersNeededForLong; |
| 1381 | } else { |
| 1382 | if (LongStackNeedsPadding()) { |
| 1383 | PushStack(0); |
| 1384 | stack_entries_++; |
| 1385 | } |
| 1386 | if (kRegistersNeededForLong == 1) { |
| 1387 | PushStack(static_cast<uintptr_t>(val)); |
| 1388 | stack_entries_++; |
| 1389 | } else { |
| 1390 | PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF)); |
| 1391 | PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF)); |
| 1392 | stack_entries_ += 2; |
| 1393 | } |
| 1394 | gpr_index_ = 0; |
| 1395 | } |
| 1396 | } |
| 1397 | |
Ian Rogers | 1428dce | 2014-10-21 15:02:15 -0700 | [diff] [blame] | 1398 | bool HaveFloatFpr() const { |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1399 | return fpr_index_ > 0; |
| 1400 | } |
| 1401 | |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1402 | void AdvanceFloat(float val) { |
| 1403 | if (kNativeSoftFloatAbi) { |
Roland Levillain | da4d79b | 2015-03-24 14:36:11 +0000 | [diff] [blame] | 1404 | AdvanceInt(bit_cast<uint32_t, float>(val)); |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1405 | } else { |
| 1406 | if (HaveFloatFpr()) { |
| 1407 | fpr_index_--; |
| 1408 | if (kRegistersNeededForDouble == 1) { |
Andreas Gampe | 1a5c406 | 2015-01-15 12:10:47 -0800 | [diff] [blame] | 1409 | if (kMultiFPRegistersWidened) { |
Roland Levillain | da4d79b | 2015-03-24 14:36:11 +0000 | [diff] [blame] | 1410 | PushFpr8(bit_cast<uint64_t, double>(val)); |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1411 | } else { |
| 1412 | // No widening, just use the bits. |
Roland Levillain | da4d79b | 2015-03-24 14:36:11 +0000 | [diff] [blame] | 1413 | PushFpr8(static_cast<uint64_t>(bit_cast<uint32_t, float>(val))); |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1414 | } |
| 1415 | } else { |
| 1416 | PushFpr4(val); |
| 1417 | } |
| 1418 | } else { |
| 1419 | stack_entries_++; |
Andreas Gampe | 1a5c406 | 2015-01-15 12:10:47 -0800 | [diff] [blame] | 1420 | if (kRegistersNeededForDouble == 1 && kMultiFPRegistersWidened) { |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1421 | // Need to widen before storing: Note the "double" in the template instantiation. |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1422 | // Note: We need to jump through those hoops to make the compiler happy. |
| 1423 | DCHECK_EQ(sizeof(uintptr_t), sizeof(uint64_t)); |
Roland Levillain | da4d79b | 2015-03-24 14:36:11 +0000 | [diff] [blame] | 1424 | PushStack(static_cast<uintptr_t>(bit_cast<uint64_t, double>(val))); |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1425 | } else { |
Roland Levillain | da4d79b | 2015-03-24 14:36:11 +0000 | [diff] [blame] | 1426 | PushStack(static_cast<uintptr_t>(bit_cast<uint32_t, float>(val))); |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1427 | } |
| 1428 | fpr_index_ = 0; |
| 1429 | } |
| 1430 | } |
| 1431 | } |
| 1432 | |
Ian Rogers | 1428dce | 2014-10-21 15:02:15 -0700 | [diff] [blame] | 1433 | bool HaveDoubleFpr() const { |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1434 | return fpr_index_ >= kRegistersNeededForDouble + (DoubleFprNeedsPadding() ? 1 : 0); |
| 1435 | } |
| 1436 | |
Ian Rogers | 1428dce | 2014-10-21 15:02:15 -0700 | [diff] [blame] | 1437 | bool DoubleFprNeedsPadding() const { |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1438 | return kRegistersNeededForDouble > 1 && // only pad when using multiple registers |
| 1439 | kAlignDoubleOnStack && // and when it needs alignment |
| 1440 | (fpr_index_ & 1) == 1; // counter is odd, see constructor |
| 1441 | } |
| 1442 | |
Ian Rogers | 1428dce | 2014-10-21 15:02:15 -0700 | [diff] [blame] | 1443 | bool DoubleStackNeedsPadding() const { |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1444 | return kRegistersNeededForDouble > 1 && // only pad when using multiple registers |
| 1445 | kAlignDoubleOnStack && // and when it needs 8B alignment |
| 1446 | (stack_entries_ & 1) == 1; // counter is odd |
| 1447 | } |
| 1448 | |
| 1449 | void AdvanceDouble(uint64_t val) { |
| 1450 | if (kNativeSoftFloatAbi) { |
| 1451 | AdvanceLong(val); |
| 1452 | } else { |
| 1453 | if (HaveDoubleFpr()) { |
| 1454 | if (DoubleFprNeedsPadding()) { |
| 1455 | PushFpr4(0); |
| 1456 | fpr_index_--; |
| 1457 | } |
| 1458 | PushFpr8(val); |
| 1459 | fpr_index_ -= kRegistersNeededForDouble; |
| 1460 | } else { |
| 1461 | if (DoubleStackNeedsPadding()) { |
| 1462 | PushStack(0); |
| 1463 | stack_entries_++; |
| 1464 | } |
| 1465 | if (kRegistersNeededForDouble == 1) { |
| 1466 | PushStack(static_cast<uintptr_t>(val)); |
| 1467 | stack_entries_++; |
| 1468 | } else { |
| 1469 | PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF)); |
| 1470 | PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF)); |
| 1471 | stack_entries_ += 2; |
| 1472 | } |
| 1473 | fpr_index_ = 0; |
| 1474 | } |
| 1475 | } |
| 1476 | } |
| 1477 | |
Ian Rogers | 1428dce | 2014-10-21 15:02:15 -0700 | [diff] [blame] | 1478 | uint32_t GetStackEntries() const { |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1479 | return stack_entries_; |
| 1480 | } |
| 1481 | |
Ian Rogers | 1428dce | 2014-10-21 15:02:15 -0700 | [diff] [blame] | 1482 | uint32_t GetNumberOfUsedGprs() const { |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1483 | return kNumNativeGprArgs - gpr_index_; |
| 1484 | } |
| 1485 | |
Ian Rogers | 1428dce | 2014-10-21 15:02:15 -0700 | [diff] [blame] | 1486 | uint32_t GetNumberOfUsedFprs() const { |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1487 | return kNumNativeFprArgs - fpr_index_; |
| 1488 | } |
| 1489 | |
| 1490 | private: |
| 1491 | void PushGpr(uintptr_t val) { |
| 1492 | delegate_->PushGpr(val); |
| 1493 | } |
| 1494 | void PushFpr4(float val) { |
| 1495 | delegate_->PushFpr4(val); |
| 1496 | } |
| 1497 | void PushFpr8(uint64_t val) { |
| 1498 | delegate_->PushFpr8(val); |
| 1499 | } |
| 1500 | void PushStack(uintptr_t val) { |
| 1501 | delegate_->PushStack(val); |
| 1502 | } |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1503 | uintptr_t PushHandle(mirror::Object* ref) REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1504 | return delegate_->PushHandle(ref); |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1505 | } |
| 1506 | |
| 1507 | uint32_t gpr_index_; // Number of free GPRs |
| 1508 | uint32_t fpr_index_; // Number of free FPRs |
| 1509 | uint32_t stack_entries_; // Stack entries are in multiples of 32b, as floats are usually not |
| 1510 | // extended |
Ian Rogers | 1428dce | 2014-10-21 15:02:15 -0700 | [diff] [blame] | 1511 | T* const delegate_; // What Push implementation gets called |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1512 | }; |
| 1513 | |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1514 | // Computes the sizes of register stacks and call stack area. Handling of references can be extended |
| 1515 | // in subclasses. |
| 1516 | // |
| 1517 | // To handle native pointers, use "L" in the shorty for an object reference, which simulates |
| 1518 | // them with handles. |
| 1519 | class ComputeNativeCallFrameSize { |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1520 | public: |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1521 | ComputeNativeCallFrameSize() : num_stack_entries_(0) {} |
| 1522 | |
| 1523 | virtual ~ComputeNativeCallFrameSize() {} |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1524 | |
Ian Rogers | 1428dce | 2014-10-21 15:02:15 -0700 | [diff] [blame] | 1525 | uint32_t GetStackSize() const { |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1526 | return num_stack_entries_ * sizeof(uintptr_t); |
| 1527 | } |
| 1528 | |
Ian Rogers | 1428dce | 2014-10-21 15:02:15 -0700 | [diff] [blame] | 1529 | uint8_t* LayoutCallStack(uint8_t* sp8) const { |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1530 | sp8 -= GetStackSize(); |
Andreas Gampe | 779f8c9 | 2014-06-09 18:29:38 -0700 | [diff] [blame] | 1531 | // Align by kStackAlignment. |
| 1532 | sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment)); |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1533 | return sp8; |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1534 | } |
| 1535 | |
Ian Rogers | 1428dce | 2014-10-21 15:02:15 -0700 | [diff] [blame] | 1536 | uint8_t* LayoutCallRegisterStacks(uint8_t* sp8, uintptr_t** start_gpr, uint32_t** start_fpr) |
| 1537 | const { |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1538 | // Assumption is OK right now, as we have soft-float arm |
| 1539 | size_t fregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeFprArgs; |
| 1540 | sp8 -= fregs * sizeof(uintptr_t); |
| 1541 | *start_fpr = reinterpret_cast<uint32_t*>(sp8); |
| 1542 | size_t iregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeGprArgs; |
| 1543 | sp8 -= iregs * sizeof(uintptr_t); |
| 1544 | *start_gpr = reinterpret_cast<uintptr_t*>(sp8); |
| 1545 | return sp8; |
| 1546 | } |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1547 | |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1548 | 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] | 1549 | uint32_t** start_fpr) const { |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1550 | // Native call stack. |
| 1551 | sp8 = LayoutCallStack(sp8); |
| 1552 | *start_stack = reinterpret_cast<uintptr_t*>(sp8); |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1553 | |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1554 | // Put fprs and gprs below. |
| 1555 | sp8 = LayoutCallRegisterStacks(sp8, start_gpr, start_fpr); |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1556 | |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1557 | // Return the new bottom. |
| 1558 | return sp8; |
| 1559 | } |
| 1560 | |
Roland Levillain | 4b8f1ec | 2015-08-26 18:34:03 +0100 | [diff] [blame] | 1561 | virtual void WalkHeader( |
| 1562 | BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm ATTRIBUTE_UNUSED) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1563 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1564 | } |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1565 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1566 | 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] | 1567 | BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize> sm(this); |
| 1568 | |
| 1569 | WalkHeader(&sm); |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1570 | |
| 1571 | for (uint32_t i = 1; i < shorty_len; ++i) { |
| 1572 | Primitive::Type cur_type_ = Primitive::GetType(shorty[i]); |
| 1573 | switch (cur_type_) { |
| 1574 | case Primitive::kPrimNot: |
Ian Rogers | 1d8cdbc | 2014-09-22 22:51:09 -0700 | [diff] [blame] | 1575 | // TODO: fix abuse of mirror types. |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1576 | sm.AdvanceHandleScope( |
| 1577 | reinterpret_cast<mirror::Object*>(0x12345678)); |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1578 | break; |
| 1579 | |
| 1580 | case Primitive::kPrimBoolean: |
| 1581 | case Primitive::kPrimByte: |
| 1582 | case Primitive::kPrimChar: |
| 1583 | case Primitive::kPrimShort: |
| 1584 | case Primitive::kPrimInt: |
| 1585 | sm.AdvanceInt(0); |
| 1586 | break; |
| 1587 | case Primitive::kPrimFloat: |
| 1588 | sm.AdvanceFloat(0); |
| 1589 | break; |
| 1590 | case Primitive::kPrimDouble: |
| 1591 | sm.AdvanceDouble(0); |
| 1592 | break; |
| 1593 | case Primitive::kPrimLong: |
| 1594 | sm.AdvanceLong(0); |
| 1595 | break; |
| 1596 | default: |
| 1597 | LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty; |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 1598 | UNREACHABLE(); |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1599 | } |
| 1600 | } |
| 1601 | |
Ian Rogers | 1428dce | 2014-10-21 15:02:15 -0700 | [diff] [blame] | 1602 | num_stack_entries_ = sm.GetStackEntries(); |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1603 | } |
| 1604 | |
| 1605 | void PushGpr(uintptr_t /* val */) { |
| 1606 | // not optimizing registers, yet |
| 1607 | } |
| 1608 | |
| 1609 | void PushFpr4(float /* val */) { |
| 1610 | // not optimizing registers, yet |
| 1611 | } |
| 1612 | |
| 1613 | void PushFpr8(uint64_t /* val */) { |
| 1614 | // not optimizing registers, yet |
| 1615 | } |
| 1616 | |
| 1617 | void PushStack(uintptr_t /* val */) { |
| 1618 | // counting is already done in the superclass |
| 1619 | } |
| 1620 | |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1621 | virtual uintptr_t PushHandle(mirror::Object* /* ptr */) { |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1622 | return reinterpret_cast<uintptr_t>(nullptr); |
| 1623 | } |
| 1624 | |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1625 | protected: |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1626 | uint32_t num_stack_entries_; |
| 1627 | }; |
| 1628 | |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1629 | class ComputeGenericJniFrameSize FINAL : public ComputeNativeCallFrameSize { |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1630 | public: |
Igor Murashkin | 06a04e0 | 2016-09-13 15:57:37 -0700 | [diff] [blame] | 1631 | explicit ComputeGenericJniFrameSize(bool critical_native) |
| 1632 | : num_handle_scope_references_(0), critical_native_(critical_native) {} |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1633 | |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1634 | // Lays out the callee-save frame. Assumes that the incorrect frame corresponding to RefsAndArgs |
| 1635 | // is at *m = sp. Will update to point to the bottom of the save frame. |
| 1636 | // |
| 1637 | // Note: assumes ComputeAll() has been run before. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1638 | void LayoutCalleeSaveFrame(Thread* self, ArtMethod*** m, void* sp, HandleScope** handle_scope) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1639 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1640 | ArtMethod* method = **m; |
| 1641 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1642 | DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize); |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1643 | |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1644 | uint8_t* sp8 = reinterpret_cast<uint8_t*>(sp); |
| 1645 | |
| 1646 | // First, fix up the layout of the callee-save frame. |
| 1647 | // We have to squeeze in the HandleScope, and relocate the method pointer. |
| 1648 | |
| 1649 | // "Free" the slot for the method. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1650 | 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] | 1651 | |
| 1652 | // Under the callee saves put handle scope and new method stack reference. |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1653 | size_t handle_scope_size = HandleScope::SizeOf(num_handle_scope_references_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1654 | size_t scope_and_method = handle_scope_size + sizeof(ArtMethod*); |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1655 | |
| 1656 | sp8 -= scope_and_method; |
| 1657 | // Align by kStackAlignment. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1658 | sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment)); |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1659 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1660 | uint8_t* sp8_table = sp8 + sizeof(ArtMethod*); |
Ian Rogers | 59c0706 | 2014-10-10 13:03:39 -0700 | [diff] [blame] | 1661 | *handle_scope = HandleScope::Create(sp8_table, self->GetTopHandleScope(), |
| 1662 | num_handle_scope_references_); |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1663 | |
| 1664 | // Add a slot for the method pointer, and fill it. Fix the pointer-pointer given to us. |
| 1665 | uint8_t* method_pointer = sp8; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1666 | auto** new_method_ref = reinterpret_cast<ArtMethod**>(method_pointer); |
| 1667 | *new_method_ref = method; |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1668 | *m = new_method_ref; |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1669 | } |
| 1670 | |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1671 | // Adds space for the cookie. Note: may leave stack unaligned. |
Ian Rogers | 1428dce | 2014-10-21 15:02:15 -0700 | [diff] [blame] | 1672 | void LayoutCookie(uint8_t** sp) const { |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1673 | // Reference cookie and padding |
| 1674 | *sp -= 8; |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 1675 | } |
| 1676 | |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1677 | // Re-layout the callee-save frame (insert a handle-scope). Then add space for the cookie. |
| 1678 | // Returns the new bottom. Note: this may be unaligned. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1679 | uint8_t* LayoutJNISaveFrame(Thread* self, ArtMethod*** m, void* sp, HandleScope** handle_scope) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1680 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1681 | // First, fix up the layout of the callee-save frame. |
| 1682 | // We have to squeeze in the HandleScope, and relocate the method pointer. |
Ian Rogers | 59c0706 | 2014-10-10 13:03:39 -0700 | [diff] [blame] | 1683 | LayoutCalleeSaveFrame(self, m, sp, handle_scope); |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1684 | |
| 1685 | // The bottom of the callee-save frame is now where the method is, *m. |
| 1686 | uint8_t* sp8 = reinterpret_cast<uint8_t*>(*m); |
| 1687 | |
| 1688 | // Add space for cookie. |
| 1689 | LayoutCookie(&sp8); |
| 1690 | |
| 1691 | return sp8; |
| 1692 | } |
| 1693 | |
| 1694 | // WARNING: After this, *sp won't be pointing to the method anymore! |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1695 | uint8_t* ComputeLayout(Thread* self, ArtMethod*** m, const char* shorty, uint32_t shorty_len, |
| 1696 | HandleScope** handle_scope, uintptr_t** start_stack, uintptr_t** start_gpr, |
| 1697 | uint32_t** start_fpr) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1698 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1699 | Walk(shorty, shorty_len); |
| 1700 | |
| 1701 | // JNI part. |
Ian Rogers | 59c0706 | 2014-10-10 13:03:39 -0700 | [diff] [blame] | 1702 | uint8_t* sp8 = LayoutJNISaveFrame(self, m, reinterpret_cast<void*>(*m), handle_scope); |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1703 | |
| 1704 | sp8 = LayoutNativeCall(sp8, start_stack, start_gpr, start_fpr); |
| 1705 | |
| 1706 | // Return the new bottom. |
| 1707 | return sp8; |
| 1708 | } |
| 1709 | |
| 1710 | uintptr_t PushHandle(mirror::Object* /* ptr */) OVERRIDE; |
| 1711 | |
| 1712 | // Add JNIEnv* and jobj/jclass before the shorty-derived elements. |
| 1713 | void WalkHeader(BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) OVERRIDE |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1714 | REQUIRES_SHARED(Locks::mutator_lock_); |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1715 | |
| 1716 | private: |
| 1717 | uint32_t num_handle_scope_references_; |
Igor Murashkin | 06a04e0 | 2016-09-13 15:57:37 -0700 | [diff] [blame] | 1718 | const bool critical_native_; |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1719 | }; |
| 1720 | |
| 1721 | uintptr_t ComputeGenericJniFrameSize::PushHandle(mirror::Object* /* ptr */) { |
| 1722 | num_handle_scope_references_++; |
| 1723 | return reinterpret_cast<uintptr_t>(nullptr); |
| 1724 | } |
| 1725 | |
| 1726 | void ComputeGenericJniFrameSize::WalkHeader( |
| 1727 | BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) { |
Igor Murashkin | 06a04e0 | 2016-09-13 15:57:37 -0700 | [diff] [blame] | 1728 | // First 2 parameters are always excluded for @CriticalNative. |
| 1729 | if (UNLIKELY(critical_native_)) { |
| 1730 | return; |
| 1731 | } |
| 1732 | |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1733 | // JNIEnv |
| 1734 | sm->AdvancePointer(nullptr); |
| 1735 | |
| 1736 | // Class object or this as first argument |
| 1737 | sm->AdvanceHandleScope(reinterpret_cast<mirror::Object*>(0x12345678)); |
| 1738 | } |
| 1739 | |
| 1740 | // Class to push values to three separate regions. Used to fill the native call part. Adheres to |
| 1741 | // the template requirements of BuildGenericJniFrameStateMachine. |
| 1742 | class FillNativeCall { |
| 1743 | public: |
| 1744 | FillNativeCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) : |
| 1745 | cur_gpr_reg_(gpr_regs), cur_fpr_reg_(fpr_regs), cur_stack_arg_(stack_args) {} |
| 1746 | |
| 1747 | virtual ~FillNativeCall() {} |
| 1748 | |
| 1749 | void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) { |
| 1750 | cur_gpr_reg_ = gpr_regs; |
| 1751 | cur_fpr_reg_ = fpr_regs; |
| 1752 | cur_stack_arg_ = stack_args; |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1753 | } |
| 1754 | |
| 1755 | void PushGpr(uintptr_t val) { |
| 1756 | *cur_gpr_reg_ = val; |
| 1757 | cur_gpr_reg_++; |
| 1758 | } |
| 1759 | |
| 1760 | void PushFpr4(float val) { |
| 1761 | *cur_fpr_reg_ = val; |
| 1762 | cur_fpr_reg_++; |
| 1763 | } |
| 1764 | |
| 1765 | void PushFpr8(uint64_t val) { |
| 1766 | uint64_t* tmp = reinterpret_cast<uint64_t*>(cur_fpr_reg_); |
| 1767 | *tmp = val; |
| 1768 | cur_fpr_reg_ += 2; |
| 1769 | } |
| 1770 | |
| 1771 | void PushStack(uintptr_t val) { |
| 1772 | *cur_stack_arg_ = val; |
| 1773 | cur_stack_arg_++; |
| 1774 | } |
| 1775 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1776 | virtual uintptr_t PushHandle(mirror::Object*) REQUIRES_SHARED(Locks::mutator_lock_) { |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1777 | LOG(FATAL) << "(Non-JNI) Native call does not use handles."; |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1778 | UNREACHABLE(); |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1779 | } |
| 1780 | |
| 1781 | private: |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1782 | uintptr_t* cur_gpr_reg_; |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1783 | uint32_t* cur_fpr_reg_; |
| 1784 | uintptr_t* cur_stack_arg_; |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1785 | }; |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1786 | |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1787 | // Visits arguments on the stack placing them into a region lower down the stack for the benefit |
| 1788 | // of transitioning into native code. |
| 1789 | class BuildGenericJniFrameVisitor FINAL : public QuickArgumentVisitor { |
| 1790 | public: |
Igor Murashkin | 06a04e0 | 2016-09-13 15:57:37 -0700 | [diff] [blame] | 1791 | BuildGenericJniFrameVisitor(Thread* self, |
| 1792 | bool is_static, |
| 1793 | bool critical_native, |
| 1794 | const char* shorty, |
| 1795 | uint32_t shorty_len, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1796 | ArtMethod*** sp) |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1797 | : QuickArgumentVisitor(*sp, is_static, shorty, shorty_len), |
Igor Murashkin | 06a04e0 | 2016-09-13 15:57:37 -0700 | [diff] [blame] | 1798 | jni_call_(nullptr, nullptr, nullptr, nullptr, critical_native), |
| 1799 | sm_(&jni_call_) { |
| 1800 | ComputeGenericJniFrameSize fsc(critical_native); |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1801 | uintptr_t* start_gpr_reg; |
| 1802 | uint32_t* start_fpr_reg; |
| 1803 | uintptr_t* start_stack_arg; |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1804 | bottom_of_used_area_ = fsc.ComputeLayout(self, sp, shorty, shorty_len, |
Ian Rogers | 59c0706 | 2014-10-10 13:03:39 -0700 | [diff] [blame] | 1805 | &handle_scope_, |
| 1806 | &start_stack_arg, |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1807 | &start_gpr_reg, &start_fpr_reg); |
| 1808 | |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1809 | jni_call_.Reset(start_gpr_reg, start_fpr_reg, start_stack_arg, handle_scope_); |
| 1810 | |
Igor Murashkin | 06a04e0 | 2016-09-13 15:57:37 -0700 | [diff] [blame] | 1811 | // First 2 parameters are always excluded for CriticalNative methods. |
| 1812 | if (LIKELY(!critical_native)) { |
| 1813 | // jni environment is always first argument |
| 1814 | sm_.AdvancePointer(self->GetJniEnv()); |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1815 | |
Igor Murashkin | 06a04e0 | 2016-09-13 15:57:37 -0700 | [diff] [blame] | 1816 | if (is_static) { |
| 1817 | sm_.AdvanceHandleScope((**sp)->GetDeclaringClass()); |
| 1818 | } // else "this" reference is already handled by QuickArgumentVisitor. |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1819 | } |
| 1820 | } |
| 1821 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1822 | void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE; |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1823 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1824 | void FinalizeHandleScope(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_); |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1825 | |
Vladimir Marko | f39745e | 2016-01-26 12:16:55 +0000 | [diff] [blame] | 1826 | StackReference<mirror::Object>* GetFirstHandleScopeEntry() { |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1827 | return handle_scope_->GetHandle(0).GetReference(); |
| 1828 | } |
| 1829 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1830 | jobject GetFirstHandleScopeJObject() const REQUIRES_SHARED(Locks::mutator_lock_) { |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1831 | return handle_scope_->GetHandle(0).ToJObject(); |
| 1832 | } |
| 1833 | |
Ian Rogers | 1428dce | 2014-10-21 15:02:15 -0700 | [diff] [blame] | 1834 | void* GetBottomOfUsedArea() const { |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1835 | return bottom_of_used_area_; |
| 1836 | } |
| 1837 | |
| 1838 | private: |
| 1839 | // A class to fill a JNI call. Adds reference/handle-scope management to FillNativeCall. |
| 1840 | class FillJniCall FINAL : public FillNativeCall { |
| 1841 | public: |
| 1842 | 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] | 1843 | HandleScope* handle_scope, bool critical_native) |
| 1844 | : FillNativeCall(gpr_regs, fpr_regs, stack_args), |
| 1845 | handle_scope_(handle_scope), |
| 1846 | cur_entry_(0), |
| 1847 | critical_native_(critical_native) {} |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1848 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1849 | uintptr_t PushHandle(mirror::Object* ref) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_); |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1850 | |
| 1851 | void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args, HandleScope* scope) { |
| 1852 | FillNativeCall::Reset(gpr_regs, fpr_regs, stack_args); |
| 1853 | handle_scope_ = scope; |
| 1854 | cur_entry_ = 0U; |
| 1855 | } |
| 1856 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1857 | void ResetRemainingScopeSlots() REQUIRES_SHARED(Locks::mutator_lock_) { |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1858 | // Initialize padding entries. |
| 1859 | size_t expected_slots = handle_scope_->NumberOfReferences(); |
| 1860 | while (cur_entry_ < expected_slots) { |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 1861 | handle_scope_->GetMutableHandle(cur_entry_++).Assign(nullptr); |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1862 | } |
Igor Murashkin | 06a04e0 | 2016-09-13 15:57:37 -0700 | [diff] [blame] | 1863 | |
| 1864 | if (!critical_native_) { |
| 1865 | // Non-critical natives have at least the self class (jclass) or this (jobject). |
| 1866 | DCHECK_NE(cur_entry_, 0U); |
| 1867 | } |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1868 | } |
| 1869 | |
Mathieu Chartier | 1432a5b | 2016-10-04 15:41:42 -0700 | [diff] [blame] | 1870 | bool CriticalNative() const { |
| 1871 | return critical_native_; |
| 1872 | } |
| 1873 | |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1874 | private: |
| 1875 | HandleScope* handle_scope_; |
| 1876 | size_t cur_entry_; |
Igor Murashkin | 06a04e0 | 2016-09-13 15:57:37 -0700 | [diff] [blame] | 1877 | const bool critical_native_; |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1878 | }; |
| 1879 | |
| 1880 | HandleScope* handle_scope_; |
| 1881 | FillJniCall jni_call_; |
| 1882 | void* bottom_of_used_area_; |
| 1883 | |
| 1884 | BuildNativeCallFrameStateMachine<FillJniCall> sm_; |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 1885 | |
| 1886 | DISALLOW_COPY_AND_ASSIGN(BuildGenericJniFrameVisitor); |
| 1887 | }; |
| 1888 | |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1889 | uintptr_t BuildGenericJniFrameVisitor::FillJniCall::PushHandle(mirror::Object* ref) { |
| 1890 | uintptr_t tmp; |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 1891 | MutableHandle<mirror::Object> h = handle_scope_->GetMutableHandle(cur_entry_); |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1892 | h.Assign(ref); |
| 1893 | tmp = reinterpret_cast<uintptr_t>(h.ToJObject()); |
| 1894 | cur_entry_++; |
| 1895 | return tmp; |
| 1896 | } |
| 1897 | |
Ian Rogers | 9758f79 | 2014-03-13 09:02:55 -0700 | [diff] [blame] | 1898 | void BuildGenericJniFrameVisitor::Visit() { |
| 1899 | Primitive::Type type = GetParamPrimitiveType(); |
| 1900 | switch (type) { |
| 1901 | case Primitive::kPrimLong: { |
| 1902 | jlong long_arg; |
| 1903 | if (IsSplitLongOrDouble()) { |
| 1904 | long_arg = ReadSplitLongParam(); |
| 1905 | } else { |
| 1906 | long_arg = *reinterpret_cast<jlong*>(GetParamAddress()); |
| 1907 | } |
| 1908 | sm_.AdvanceLong(long_arg); |
| 1909 | break; |
| 1910 | } |
| 1911 | case Primitive::kPrimDouble: { |
| 1912 | uint64_t double_arg; |
| 1913 | if (IsSplitLongOrDouble()) { |
| 1914 | // Read into union so that we don't case to a double. |
| 1915 | double_arg = ReadSplitLongParam(); |
| 1916 | } else { |
| 1917 | double_arg = *reinterpret_cast<uint64_t*>(GetParamAddress()); |
| 1918 | } |
| 1919 | sm_.AdvanceDouble(double_arg); |
| 1920 | break; |
| 1921 | } |
| 1922 | case Primitive::kPrimNot: { |
| 1923 | StackReference<mirror::Object>* stack_ref = |
| 1924 | reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1925 | sm_.AdvanceHandleScope(stack_ref->AsMirrorPtr()); |
Ian Rogers | 9758f79 | 2014-03-13 09:02:55 -0700 | [diff] [blame] | 1926 | break; |
| 1927 | } |
| 1928 | case Primitive::kPrimFloat: |
| 1929 | sm_.AdvanceFloat(*reinterpret_cast<float*>(GetParamAddress())); |
| 1930 | break; |
| 1931 | case Primitive::kPrimBoolean: // Fall-through. |
| 1932 | case Primitive::kPrimByte: // Fall-through. |
| 1933 | case Primitive::kPrimChar: // Fall-through. |
| 1934 | case Primitive::kPrimShort: // Fall-through. |
| 1935 | case Primitive::kPrimInt: // Fall-through. |
| 1936 | sm_.AdvanceInt(*reinterpret_cast<jint*>(GetParamAddress())); |
| 1937 | break; |
| 1938 | case Primitive::kPrimVoid: |
| 1939 | LOG(FATAL) << "UNREACHABLE"; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 1940 | UNREACHABLE(); |
Ian Rogers | 9758f79 | 2014-03-13 09:02:55 -0700 | [diff] [blame] | 1941 | } |
| 1942 | } |
| 1943 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1944 | void BuildGenericJniFrameVisitor::FinalizeHandleScope(Thread* self) { |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 1945 | // Clear out rest of the scope. |
| 1946 | jni_call_.ResetRemainingScopeSlots(); |
Mathieu Chartier | 1432a5b | 2016-10-04 15:41:42 -0700 | [diff] [blame] | 1947 | if (!jni_call_.CriticalNative()) { |
| 1948 | // Install HandleScope. |
| 1949 | self->PushHandleScope(handle_scope_); |
| 1950 | } |
Ian Rogers | 9758f79 | 2014-03-13 09:02:55 -0700 | [diff] [blame] | 1951 | } |
| 1952 | |
Ian Rogers | 04c31d2 | 2014-07-07 21:44:06 -0700 | [diff] [blame] | 1953 | #if defined(__arm__) || defined(__aarch64__) |
Andreas Gampe | 9054683 | 2014-03-12 18:07:19 -0700 | [diff] [blame] | 1954 | extern "C" void* artFindNativeMethod(); |
Ian Rogers | 04c31d2 | 2014-07-07 21:44:06 -0700 | [diff] [blame] | 1955 | #else |
| 1956 | extern "C" void* artFindNativeMethod(Thread* self); |
| 1957 | #endif |
Andreas Gampe | 9054683 | 2014-03-12 18:07:19 -0700 | [diff] [blame] | 1958 | |
Igor Murashkin | 06a04e0 | 2016-09-13 15:57:37 -0700 | [diff] [blame] | 1959 | static uint64_t artQuickGenericJniEndJNIRef(Thread* self, |
| 1960 | uint32_t cookie, |
| 1961 | bool fast_native ATTRIBUTE_UNUSED, |
| 1962 | jobject l, |
| 1963 | jobject lock) { |
| 1964 | // TODO: add entrypoints for @FastNative returning objects. |
Andreas Gampe | ad61517 | 2014-04-04 16:20:13 -0700 | [diff] [blame] | 1965 | if (lock != nullptr) { |
| 1966 | return reinterpret_cast<uint64_t>(JniMethodEndWithReferenceSynchronized(l, cookie, lock, self)); |
| 1967 | } else { |
| 1968 | return reinterpret_cast<uint64_t>(JniMethodEndWithReference(l, cookie, self)); |
| 1969 | } |
| 1970 | } |
| 1971 | |
Igor Murashkin | 06a04e0 | 2016-09-13 15:57:37 -0700 | [diff] [blame] | 1972 | static void artQuickGenericJniEndJNINonRef(Thread* self, |
| 1973 | uint32_t cookie, |
| 1974 | bool fast_native, |
| 1975 | jobject lock) { |
Andreas Gampe | ad61517 | 2014-04-04 16:20:13 -0700 | [diff] [blame] | 1976 | if (lock != nullptr) { |
| 1977 | JniMethodEndSynchronized(cookie, lock, self); |
Igor Murashkin | 06a04e0 | 2016-09-13 15:57:37 -0700 | [diff] [blame] | 1978 | // Ignore "fast_native" here because synchronized functions aren't very fast. |
Andreas Gampe | ad61517 | 2014-04-04 16:20:13 -0700 | [diff] [blame] | 1979 | } else { |
Igor Murashkin | 06a04e0 | 2016-09-13 15:57:37 -0700 | [diff] [blame] | 1980 | if (UNLIKELY(fast_native)) { |
| 1981 | JniMethodFastEnd(cookie, self); |
| 1982 | } else { |
| 1983 | JniMethodEnd(cookie, self); |
| 1984 | } |
Andreas Gampe | ad61517 | 2014-04-04 16:20:13 -0700 | [diff] [blame] | 1985 | } |
| 1986 | } |
| 1987 | |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1988 | /* |
| 1989 | * 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] | 1990 | * 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] | 1991 | * The final element on the stack is a pointer to the native code. |
| 1992 | * |
Andreas Gampe | 36fea8d | 2014-03-10 13:37:40 -0700 | [diff] [blame] | 1993 | * 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] | 1994 | * 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] | 1995 | * |
Andreas Gampe | c147b00 | 2014-03-06 18:11:06 -0800 | [diff] [blame] | 1996 | * The return of this function denotes: |
| 1997 | * 1) How many bytes of the alloca can be released, if the value is non-negative. |
| 1998 | * 2) An error, if the value is negative. |
| 1999 | */ |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2000 | extern "C" TwoWordReturn artQuickGenericJniTrampoline(Thread* self, ArtMethod** sp) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2001 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2002 | ArtMethod* called = *sp; |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 2003 | DCHECK(called->IsNative()) << called->PrettyMethod(true); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 2004 | uint32_t shorty_len = 0; |
| 2005 | const char* shorty = called->GetShorty(&shorty_len); |
Igor Murashkin | 06a04e0 | 2016-09-13 15:57:37 -0700 | [diff] [blame] | 2006 | bool critical_native = called->IsAnnotatedWithCriticalNative(); |
| 2007 | bool fast_native = called->IsAnnotatedWithFastNative(); |
| 2008 | bool normal_native = !critical_native && !fast_native; |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 2009 | |
Ian Rogers | 1d8cdbc | 2014-09-22 22:51:09 -0700 | [diff] [blame] | 2010 | // Run the visitor and update sp. |
Igor Murashkin | 06a04e0 | 2016-09-13 15:57:37 -0700 | [diff] [blame] | 2011 | BuildGenericJniFrameVisitor visitor(self, |
| 2012 | called->IsStatic(), |
| 2013 | critical_native, |
| 2014 | shorty, |
| 2015 | shorty_len, |
| 2016 | &sp); |
Mathieu Chartier | be08cf5 | 2016-09-13 13:41:24 -0700 | [diff] [blame] | 2017 | { |
| 2018 | ScopedAssertNoThreadSuspension sants(__FUNCTION__); |
| 2019 | visitor.VisitArguments(); |
| 2020 | // FinalizeHandleScope pushes the handle scope on the thread. |
| 2021 | visitor.FinalizeHandleScope(self); |
| 2022 | } |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 2023 | |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 2024 | // Fix up managed-stack things in Thread. |
Ian Rogers | 1d8cdbc | 2014-09-22 22:51:09 -0700 | [diff] [blame] | 2025 | self->SetTopOfStack(sp); |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 2026 | |
Ian Rogers | e0dcd46 | 2014-03-08 15:21:04 -0800 | [diff] [blame] | 2027 | self->VerifyStack(); |
| 2028 | |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 2029 | uint32_t cookie; |
Igor Murashkin | 06a04e0 | 2016-09-13 15:57:37 -0700 | [diff] [blame] | 2030 | uint32_t* sp32; |
| 2031 | // Skip calling JniMethodStart for @CriticalNative. |
| 2032 | if (LIKELY(!critical_native)) { |
| 2033 | // Start JNI, save the cookie. |
| 2034 | if (called->IsSynchronized()) { |
| 2035 | DCHECK(normal_native) << " @FastNative and synchronize is not supported"; |
| 2036 | cookie = JniMethodStartSynchronized(visitor.GetFirstHandleScopeJObject(), self); |
| 2037 | if (self->IsExceptionPending()) { |
| 2038 | self->PopHandleScope(); |
| 2039 | // A negative value denotes an error. |
| 2040 | return GetTwoWordFailureValue(); |
| 2041 | } |
| 2042 | } else { |
| 2043 | if (fast_native) { |
| 2044 | cookie = JniMethodFastStart(self); |
| 2045 | } else { |
| 2046 | DCHECK(normal_native); |
| 2047 | cookie = JniMethodStart(self); |
| 2048 | } |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 2049 | } |
Igor Murashkin | 06a04e0 | 2016-09-13 15:57:37 -0700 | [diff] [blame] | 2050 | sp32 = reinterpret_cast<uint32_t*>(sp); |
| 2051 | *(sp32 - 1) = cookie; |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 2052 | } |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 2053 | |
Andreas Gampe | 9054683 | 2014-03-12 18:07:19 -0700 | [diff] [blame] | 2054 | // Retrieve the stored native code. |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 2055 | void* nativeCode = called->GetEntryPointFromJni(); |
Andreas Gampe | 9054683 | 2014-03-12 18:07:19 -0700 | [diff] [blame] | 2056 | |
Andreas Gampe | 9a6a99a | 2014-03-14 07:52:20 -0700 | [diff] [blame] | 2057 | // There are two cases for the content of nativeCode: |
| 2058 | // 1) Pointer to the native function. |
| 2059 | // 2) Pointer to the trampoline for native code binding. |
| 2060 | // In the second case, we need to execute the binding and continue with the actual native function |
| 2061 | // pointer. |
Andreas Gampe | 9054683 | 2014-03-12 18:07:19 -0700 | [diff] [blame] | 2062 | DCHECK(nativeCode != nullptr); |
| 2063 | if (nativeCode == GetJniDlsymLookupStub()) { |
Ian Rogers | 04c31d2 | 2014-07-07 21:44:06 -0700 | [diff] [blame] | 2064 | #if defined(__arm__) || defined(__aarch64__) |
Andreas Gampe | 9054683 | 2014-03-12 18:07:19 -0700 | [diff] [blame] | 2065 | nativeCode = artFindNativeMethod(); |
Ian Rogers | 04c31d2 | 2014-07-07 21:44:06 -0700 | [diff] [blame] | 2066 | #else |
| 2067 | nativeCode = artFindNativeMethod(self); |
| 2068 | #endif |
Andreas Gampe | 9054683 | 2014-03-12 18:07:19 -0700 | [diff] [blame] | 2069 | |
| 2070 | if (nativeCode == nullptr) { |
| 2071 | DCHECK(self->IsExceptionPending()); // There should be an exception pending now. |
Andreas Gampe | ad61517 | 2014-04-04 16:20:13 -0700 | [diff] [blame] | 2072 | |
Igor Murashkin | 06a04e0 | 2016-09-13 15:57:37 -0700 | [diff] [blame] | 2073 | // @CriticalNative calls do not need to call back into JniMethodEnd. |
| 2074 | if (LIKELY(!critical_native)) { |
| 2075 | // End JNI, as the assembly will move to deliver the exception. |
| 2076 | jobject lock = called->IsSynchronized() ? visitor.GetFirstHandleScopeJObject() : nullptr; |
| 2077 | if (shorty[0] == 'L') { |
| 2078 | artQuickGenericJniEndJNIRef(self, cookie, fast_native, nullptr, lock); |
| 2079 | } else { |
| 2080 | artQuickGenericJniEndJNINonRef(self, cookie, fast_native, lock); |
| 2081 | } |
Andreas Gampe | ad61517 | 2014-04-04 16:20:13 -0700 | [diff] [blame] | 2082 | } |
| 2083 | |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 2084 | return GetTwoWordFailureValue(); |
Andreas Gampe | 9054683 | 2014-03-12 18:07:19 -0700 | [diff] [blame] | 2085 | } |
| 2086 | // Note that the native code pointer will be automatically set by artFindNativeMethod(). |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 2087 | } |
| 2088 | |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 2089 | // Return native code addr(lo) and bottom of alloca address(hi). |
| 2090 | return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(visitor.GetBottomOfUsedArea()), |
| 2091 | reinterpret_cast<uintptr_t>(nativeCode)); |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 2092 | } |
| 2093 | |
Hiroshi Yamauchi | a23b468 | 2015-09-28 17:47:32 -0700 | [diff] [blame] | 2094 | // Defined in quick_jni_entrypoints.cc. |
| 2095 | extern uint64_t GenericJniMethodEnd(Thread* self, uint32_t saved_local_ref_cookie, |
| 2096 | jvalue result, uint64_t result_f, ArtMethod* called, |
| 2097 | HandleScope* handle_scope); |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 2098 | /* |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2099 | * 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] | 2100 | * unlocking. |
| 2101 | */ |
Hiroshi Yamauchi | a23b468 | 2015-09-28 17:47:32 -0700 | [diff] [blame] | 2102 | extern "C" uint64_t artQuickGenericJniEndTrampoline(Thread* self, |
| 2103 | jvalue result, |
| 2104 | uint64_t result_f) { |
| 2105 | // We're here just back from a native call. We don't have the shared mutator lock at this point |
| 2106 | // yet until we call GoToRunnable() later in GenericJniMethodEnd(). Accessing objects or doing |
| 2107 | // anything that requires a mutator lock before that would cause problems as GC may have the |
| 2108 | // exclusive mutator lock and may be moving objects, etc. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2109 | ArtMethod** sp = self->GetManagedStack()->GetTopQuickFrame(); |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 2110 | uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2111 | ArtMethod* called = *sp; |
Ian Rogers | e0dcd46 | 2014-03-08 15:21:04 -0800 | [diff] [blame] | 2112 | uint32_t cookie = *(sp32 - 1); |
Hiroshi Yamauchi | a23b468 | 2015-09-28 17:47:32 -0700 | [diff] [blame] | 2113 | HandleScope* table = reinterpret_cast<HandleScope*>(reinterpret_cast<uint8_t*>(sp) + sizeof(*sp)); |
| 2114 | return GenericJniMethodEnd(self, cookie, result, result_f, called, table); |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 2115 | } |
| 2116 | |
Andreas Gampe | d58342c | 2014-06-05 14:18:08 -0700 | [diff] [blame] | 2117 | // We use TwoWordReturn to optimize scalar returns. We use the hi value for code, and the lo value |
| 2118 | // for the method pointer. |
Andreas Gampe | 51f7635 | 2014-05-21 08:28:48 -0700 | [diff] [blame] | 2119 | // |
Andreas Gampe | d58342c | 2014-06-05 14:18:08 -0700 | [diff] [blame] | 2120 | // 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] | 2121 | // to hold the mutator lock (see REQUIRES_SHARED(Locks::mutator_lock_) annotations). |
Mathieu Chartier | 5f3ded4 | 2014-04-03 15:25:30 -0700 | [diff] [blame] | 2122 | |
| 2123 | template<InvokeType type, bool access_check> |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 2124 | static TwoWordReturn artInvokeCommon(uint32_t method_idx, |
| 2125 | ObjPtr<mirror::Object> this_object, |
| 2126 | Thread* self, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2127 | ArtMethod** sp) { |
Ian Rogers | 1d8cdbc | 2014-09-22 22:51:09 -0700 | [diff] [blame] | 2128 | ScopedQuickEntrypointChecks sqec(self); |
Vladimir Marko | fd36f1f | 2016-08-03 18:49:58 +0100 | [diff] [blame] | 2129 | DCHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(Runtime::kSaveRefsAndArgs)); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2130 | ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp); |
| 2131 | ArtMethod* method = FindMethodFast(method_idx, this_object, caller_method, access_check, type); |
Mathieu Chartier | 5f3ded4 | 2014-04-03 15:25:30 -0700 | [diff] [blame] | 2132 | if (UNLIKELY(method == nullptr)) { |
Mathieu Chartier | 5f3ded4 | 2014-04-03 15:25:30 -0700 | [diff] [blame] | 2133 | const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()->GetDexFile(); |
| 2134 | uint32_t shorty_len; |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 2135 | 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] | 2136 | { |
| 2137 | // Remember the args in case a GC happens in FindMethodFromCode. |
| 2138 | ScopedObjectAccessUnchecked soa(self->GetJniEnv()); |
| 2139 | RememberForGcArgumentVisitor visitor(sp, type == kStatic, shorty, shorty_len, &soa); |
| 2140 | visitor.VisitArguments(); |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 2141 | method = FindMethodFromCode<type, access_check>(method_idx, |
| 2142 | &this_object, |
| 2143 | caller_method, |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 2144 | self); |
Mathieu Chartier | 5f3ded4 | 2014-04-03 15:25:30 -0700 | [diff] [blame] | 2145 | visitor.FixupReferences(); |
| 2146 | } |
| 2147 | |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 2148 | if (UNLIKELY(method == nullptr)) { |
Mathieu Chartier | 5f3ded4 | 2014-04-03 15:25:30 -0700 | [diff] [blame] | 2149 | CHECK(self->IsExceptionPending()); |
Andreas Gampe | d58342c | 2014-06-05 14:18:08 -0700 | [diff] [blame] | 2150 | return GetTwoWordFailureValue(); // Failure. |
Mathieu Chartier | 5f3ded4 | 2014-04-03 15:25:30 -0700 | [diff] [blame] | 2151 | } |
| 2152 | } |
| 2153 | DCHECK(!self->IsExceptionPending()); |
| 2154 | const void* code = method->GetEntryPointFromQuickCompiledCode(); |
| 2155 | |
| 2156 | // When we return, the caller will branch to this address, so it had better not be 0! |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 2157 | DCHECK(code != nullptr) << "Code was null in method: " << method->PrettyMethod() |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 2158 | << " location: " |
| 2159 | << method->GetDexFile()->GetLocation(); |
Andreas Gampe | 51f7635 | 2014-05-21 08:28:48 -0700 | [diff] [blame] | 2160 | |
Andreas Gampe | d58342c | 2014-06-05 14:18:08 -0700 | [diff] [blame] | 2161 | return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code), |
| 2162 | reinterpret_cast<uintptr_t>(method)); |
Mathieu Chartier | 5f3ded4 | 2014-04-03 15:25:30 -0700 | [diff] [blame] | 2163 | } |
| 2164 | |
Nicolas Geoffray | 8689a0a | 2014-04-04 09:26:24 +0100 | [diff] [blame] | 2165 | // Explicit artInvokeCommon template function declarations to please analysis tool. |
| 2166 | #define EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(type, access_check) \ |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2167 | template REQUIRES_SHARED(Locks::mutator_lock_) \ |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2168 | TwoWordReturn artInvokeCommon<type, access_check>( \ |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 2169 | uint32_t method_idx, ObjPtr<mirror::Object> his_object, Thread* self, ArtMethod** sp) |
Nicolas Geoffray | 8689a0a | 2014-04-04 09:26:24 +0100 | [diff] [blame] | 2170 | |
| 2171 | EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, false); |
| 2172 | EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, true); |
| 2173 | EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, false); |
| 2174 | EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, true); |
| 2175 | EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, false); |
| 2176 | EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, true); |
| 2177 | EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, false); |
| 2178 | EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, true); |
| 2179 | EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, false); |
| 2180 | EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, true); |
| 2181 | #undef EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL |
| 2182 | |
Mathieu Chartier | 5f3ded4 | 2014-04-03 15:25:30 -0700 | [diff] [blame] | 2183 | // See comments in runtime_support_asm.S |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 2184 | extern "C" TwoWordReturn artInvokeInterfaceTrampolineWithAccessCheck( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2185 | uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2186 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Nicolas Geoffray | 7ea6a17 | 2015-05-19 18:58:54 +0100 | [diff] [blame] | 2187 | return artInvokeCommon<kInterface, true>(method_idx, this_object, self, sp); |
Mathieu Chartier | 5f3ded4 | 2014-04-03 15:25:30 -0700 | [diff] [blame] | 2188 | } |
| 2189 | |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 2190 | extern "C" TwoWordReturn artInvokeDirectTrampolineWithAccessCheck( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2191 | uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2192 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Nicolas Geoffray | 7ea6a17 | 2015-05-19 18:58:54 +0100 | [diff] [blame] | 2193 | return artInvokeCommon<kDirect, true>(method_idx, this_object, self, sp); |
Mathieu Chartier | 5f3ded4 | 2014-04-03 15:25:30 -0700 | [diff] [blame] | 2194 | } |
| 2195 | |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 2196 | extern "C" TwoWordReturn artInvokeStaticTrampolineWithAccessCheck( |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 2197 | uint32_t method_idx, |
| 2198 | mirror::Object* this_object ATTRIBUTE_UNUSED, |
| 2199 | Thread* self, |
| 2200 | ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 2201 | // For static, this_object is not required and may be random garbage. Don't pass it down so that |
| 2202 | // it doesn't cause ObjPtr alignment failure check. |
| 2203 | return artInvokeCommon<kStatic, true>(method_idx, nullptr, self, sp); |
Mathieu Chartier | 5f3ded4 | 2014-04-03 15:25:30 -0700 | [diff] [blame] | 2204 | } |
| 2205 | |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 2206 | extern "C" TwoWordReturn artInvokeSuperTrampolineWithAccessCheck( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2207 | uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2208 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Nicolas Geoffray | 7ea6a17 | 2015-05-19 18:58:54 +0100 | [diff] [blame] | 2209 | return artInvokeCommon<kSuper, true>(method_idx, this_object, self, sp); |
Mathieu Chartier | 5f3ded4 | 2014-04-03 15:25:30 -0700 | [diff] [blame] | 2210 | } |
| 2211 | |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 2212 | extern "C" TwoWordReturn artInvokeVirtualTrampolineWithAccessCheck( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2213 | uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2214 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Nicolas Geoffray | 7ea6a17 | 2015-05-19 18:58:54 +0100 | [diff] [blame] | 2215 | return artInvokeCommon<kVirtual, true>(method_idx, this_object, self, sp); |
Mathieu Chartier | 5f3ded4 | 2014-04-03 15:25:30 -0700 | [diff] [blame] | 2216 | } |
| 2217 | |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 2218 | // Determine target of interface dispatch. This object is known non-null. First argument |
| 2219 | // is there for consistency but should not be used, as some architectures overwrite it |
| 2220 | // in the assembly trampoline. |
| 2221 | extern "C" TwoWordReturn artInvokeInterfaceTrampoline(uint32_t deadbeef ATTRIBUTE_UNUSED, |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 2222 | mirror::Object* raw_this_object, |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 2223 | Thread* self, |
| 2224 | ArtMethod** sp) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2225 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 2226 | ObjPtr<mirror::Object> this_object(raw_this_object); |
Ian Rogers | 1d8cdbc | 2014-09-22 22:51:09 -0700 | [diff] [blame] | 2227 | ScopedQuickEntrypointChecks sqec(self); |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 2228 | StackHandleScope<1> hs(self); |
| 2229 | Handle<mirror::Class> cls(hs.NewHandle(this_object->GetClass())); |
| 2230 | |
Nicolas Geoffray | 5bf7bac | 2016-07-06 14:18:23 +0000 | [diff] [blame] | 2231 | ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp); |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 2232 | |
| 2233 | // Fetch the dex_method_idx of the target interface method from the caller. |
| 2234 | uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp); |
| 2235 | |
| 2236 | const DexFile::CodeItem* code_item = caller_method->GetCodeItem(); |
| 2237 | CHECK_LT(dex_pc, code_item->insns_size_in_code_units_); |
| 2238 | const Instruction* instr = Instruction::At(&code_item->insns_[dex_pc]); |
| 2239 | Instruction::Code instr_code = instr->Opcode(); |
| 2240 | CHECK(instr_code == Instruction::INVOKE_INTERFACE || |
| 2241 | instr_code == Instruction::INVOKE_INTERFACE_RANGE) |
| 2242 | << "Unexpected call into interface trampoline: " << instr->DumpString(nullptr); |
| 2243 | uint32_t dex_method_idx; |
| 2244 | if (instr_code == Instruction::INVOKE_INTERFACE) { |
| 2245 | dex_method_idx = instr->VRegB_35c(); |
| 2246 | } else { |
| 2247 | CHECK_EQ(instr_code, Instruction::INVOKE_INTERFACE_RANGE); |
| 2248 | dex_method_idx = instr->VRegB_3rc(); |
| 2249 | } |
| 2250 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2251 | ArtMethod* interface_method = caller_method->GetDexCacheResolvedMethod( |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 2252 | dex_method_idx, kRuntimePointerSize); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 2253 | DCHECK(interface_method != nullptr) << dex_method_idx << " " << caller_method->PrettyMethod(); |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 2254 | ArtMethod* method = nullptr; |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 2255 | ImTable* imt = cls->GetImt(kRuntimePointerSize); |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 2256 | |
Mathieu Chartier | 5f3ded4 | 2014-04-03 15:25:30 -0700 | [diff] [blame] | 2257 | if (LIKELY(interface_method->GetDexMethodIndex() != DexFile::kDexNoIndex)) { |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 2258 | // If the dex cache already resolved the interface method, look whether we have |
| 2259 | // a match in the ImtConflictTable. |
Andreas Gampe | 75a7db6 | 2016-09-26 12:04:26 -0700 | [diff] [blame] | 2260 | ArtMethod* conflict_method = imt->Get(ImTable::GetImtIndex(interface_method), |
| 2261 | kRuntimePointerSize); |
Alex Light | 9fc547a | 2016-03-31 14:34:33 -0700 | [diff] [blame] | 2262 | if (LIKELY(conflict_method->IsRuntimeMethod())) { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 2263 | ImtConflictTable* current_table = conflict_method->GetImtConflictTable(kRuntimePointerSize); |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 2264 | DCHECK(current_table != nullptr); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 2265 | method = current_table->Lookup(interface_method, kRuntimePointerSize); |
Alex Light | 9fc547a | 2016-03-31 14:34:33 -0700 | [diff] [blame] | 2266 | } else { |
| 2267 | // It seems we aren't really a conflict method! |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 2268 | method = cls->FindVirtualMethodForInterface(interface_method, kRuntimePointerSize); |
Alex Light | 9fc547a | 2016-03-31 14:34:33 -0700 | [diff] [blame] | 2269 | } |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 2270 | if (method != nullptr) { |
| 2271 | return GetTwoWordSuccessValue( |
| 2272 | reinterpret_cast<uintptr_t>(method->GetEntryPointFromQuickCompiledCode()), |
| 2273 | reinterpret_cast<uintptr_t>(method)); |
| 2274 | } |
| 2275 | |
| 2276 | // No match, use the IfTable. |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 2277 | method = cls->FindVirtualMethodForInterface(interface_method, kRuntimePointerSize); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 2278 | if (UNLIKELY(method == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2279 | ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch( |
| 2280 | interface_method, this_object, caller_method); |
Andreas Gampe | d58342c | 2014-06-05 14:18:08 -0700 | [diff] [blame] | 2281 | return GetTwoWordFailureValue(); // Failure. |
Mathieu Chartier | 5f3ded4 | 2014-04-03 15:25:30 -0700 | [diff] [blame] | 2282 | } |
| 2283 | } else { |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 2284 | // The dex cache did not resolve the method, look it up in the dex file |
| 2285 | // of the caller, |
Mathieu Chartier | 4edd847 | 2015-06-01 10:47:36 -0700 | [diff] [blame] | 2286 | DCHECK_EQ(interface_method, Runtime::Current()->GetResolutionMethod()); |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 2287 | const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache() |
| 2288 | ->GetDexFile(); |
Mathieu Chartier | 5f3ded4 | 2014-04-03 15:25:30 -0700 | [diff] [blame] | 2289 | uint32_t shorty_len; |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 2290 | const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(dex_method_idx), |
| 2291 | &shorty_len); |
Mathieu Chartier | 5f3ded4 | 2014-04-03 15:25:30 -0700 | [diff] [blame] | 2292 | { |
| 2293 | // Remember the args in case a GC happens in FindMethodFromCode. |
| 2294 | ScopedObjectAccessUnchecked soa(self->GetJniEnv()); |
| 2295 | RememberForGcArgumentVisitor visitor(sp, false, shorty, shorty_len, &soa); |
| 2296 | visitor.VisitArguments(); |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 2297 | method = FindMethodFromCode<kInterface, false>(dex_method_idx, |
| 2298 | &this_object, |
| 2299 | caller_method, |
Mathieu Chartier | 5f3ded4 | 2014-04-03 15:25:30 -0700 | [diff] [blame] | 2300 | self); |
| 2301 | visitor.FixupReferences(); |
| 2302 | } |
| 2303 | |
| 2304 | if (UNLIKELY(method == nullptr)) { |
| 2305 | CHECK(self->IsExceptionPending()); |
Andreas Gampe | d58342c | 2014-06-05 14:18:08 -0700 | [diff] [blame] | 2306 | return GetTwoWordFailureValue(); // Failure. |
Mathieu Chartier | 5f3ded4 | 2014-04-03 15:25:30 -0700 | [diff] [blame] | 2307 | } |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 2308 | interface_method = |
| 2309 | caller_method->GetDexCacheResolvedMethod(dex_method_idx, kRuntimePointerSize); |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 2310 | DCHECK(!interface_method->IsRuntimeMethod()); |
Mathieu Chartier | 5f3ded4 | 2014-04-03 15:25:30 -0700 | [diff] [blame] | 2311 | } |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 2312 | |
| 2313 | // We arrive here if we have found an implementation, and it is not in the ImtConflictTable. |
| 2314 | // We create a new table with the new pair { interface_method, method }. |
Andreas Gampe | 75a7db6 | 2016-09-26 12:04:26 -0700 | [diff] [blame] | 2315 | uint32_t imt_index = ImTable::GetImtIndex(interface_method); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 2316 | ArtMethod* conflict_method = imt->Get(imt_index, kRuntimePointerSize); |
Alex Light | 9fc547a | 2016-03-31 14:34:33 -0700 | [diff] [blame] | 2317 | if (conflict_method->IsRuntimeMethod()) { |
Mathieu Chartier | 7f98c9a | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 2318 | ArtMethod* new_conflict_method = Runtime::Current()->GetClassLinker()->AddMethodToConflictTable( |
| 2319 | cls.Get(), |
| 2320 | conflict_method, |
| 2321 | interface_method, |
| 2322 | method, |
| 2323 | /*force_new_conflict_method*/false); |
| 2324 | if (new_conflict_method != conflict_method) { |
Alex Light | 9fc547a | 2016-03-31 14:34:33 -0700 | [diff] [blame] | 2325 | // Update the IMT if we create a new conflict method. No fence needed here, as the |
| 2326 | // data is consistent. |
Matthew Gharrity | 465ecc8 | 2016-07-19 21:32:52 +0000 | [diff] [blame] | 2327 | imt->Set(imt_index, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 2328 | new_conflict_method, |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 2329 | kRuntimePointerSize); |
Alex Light | 9fc547a | 2016-03-31 14:34:33 -0700 | [diff] [blame] | 2330 | } |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 2331 | } |
| 2332 | |
Mathieu Chartier | 5f3ded4 | 2014-04-03 15:25:30 -0700 | [diff] [blame] | 2333 | const void* code = method->GetEntryPointFromQuickCompiledCode(); |
| 2334 | |
| 2335 | // When we return, the caller will branch to this address, so it had better not be 0! |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 2336 | DCHECK(code != nullptr) << "Code was null in method: " << method->PrettyMethod() |
Andreas Gampe | c200a4a | 2014-06-16 18:39:09 -0700 | [diff] [blame] | 2337 | << " location: " << method->GetDexFile()->GetLocation(); |
Andreas Gampe | 51f7635 | 2014-05-21 08:28:48 -0700 | [diff] [blame] | 2338 | |
Andreas Gampe | d58342c | 2014-06-05 14:18:08 -0700 | [diff] [blame] | 2339 | return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code), |
| 2340 | reinterpret_cast<uintptr_t>(method)); |
Mathieu Chartier | 5f3ded4 | 2014-04-03 15:25:30 -0700 | [diff] [blame] | 2341 | } |
| 2342 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 2343 | } // namespace art |