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