blob: 7d7c5a5a33cbb9c7be36de91f08acb30308ff1a9 [file] [log] [blame]
Ian Rogers848871b2013-08-05 10:56:33 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Mathieu Chartiere401d142015-04-22 13:56:20 -070017#include "art_method-inl.h"
Andreas Gampe8228cdf2017-05-30 15:03:54 -070018#include "base/callee_save_type.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070019#include "base/enums.h"
Ian Rogers848871b2013-08-05 10:56:33 -070020#include "callee_save_frame.h"
Dragos Sbirleabd136a22013-08-13 18:07:04 -070021#include "common_throws.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070022#include "debugger.h"
Ian Rogers848871b2013-08-05 10:56:33 -070023#include "dex_file-inl.h"
24#include "dex_instruction-inl.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070025#include "entrypoints/entrypoint_utils-inl.h"
Ian Rogers6f3dbba2014-10-14 17:41:57 -070026#include "entrypoints/runtime_asm_entrypoints.h"
Ian Rogers83883d72013-10-21 21:07:24 -070027#include "gc/accounting/card_table-inl.h"
Andreas Gampe75a7db62016-09-26 12:04:26 -070028#include "imt_conflict_table.h"
29#include "imtable-inl.h"
Alex Lightb7edcda2017-04-27 13:20:31 -070030#include "instrumentation.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070031#include "interpreter/interpreter.h"
Nicolas Geoffray796d6302016-03-13 22:22:31 +000032#include "linear_alloc.h"
Vladimir Marko0eb882b2017-05-15 13:39:18 +010033#include "method_bss_mapping.h"
Orion Hodsonac141392017-01-13 11:53:47 +000034#include "method_handles.h"
Ian Rogerse0a02da2014-12-02 14:10:53 -080035#include "method_reference.h"
Ian Rogers848871b2013-08-05 10:56:33 -070036#include "mirror/class-inl.h"
Mathieu Chartier5f3ded42014-04-03 15:25:30 -070037#include "mirror/dex_cache-inl.h"
Mathieu Chartierfc58af42015-04-16 18:00:39 -070038#include "mirror/method.h"
Orion Hodsonac141392017-01-13 11:53:47 +000039#include "mirror/method_handle_impl.h"
Ian Rogers848871b2013-08-05 10:56:33 -070040#include "mirror/object-inl.h"
41#include "mirror/object_array-inl.h"
Vladimir Marko0eb882b2017-05-15 13:39:18 +010042#include "oat_file.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010043#include "oat_quick_method_header.h"
Andreas Gampe639bdd12015-06-03 11:22:45 -070044#include "quick_exception_handler.h"
Ian Rogers848871b2013-08-05 10:56:33 -070045#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070046#include "scoped_thread_state_change-inl.h"
Andreas Gampeb3025922015-09-01 14:45:00 -070047#include "stack.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070048#include "thread-inl.h"
Orion Hodsonac141392017-01-13 11:53:47 +000049#include "well_known_classes.h"
Ian Rogers848871b2013-08-05 10:56:33 -070050
51namespace art {
52
Andreas Gampe8228cdf2017-05-30 15:03:54 -070053// Visits the arguments as saved to the stack by a CalleeSaveType::kRefAndArgs callee save frame.
Ian Rogers848871b2013-08-05 10:56:33 -070054class QuickArgumentVisitor {
Ian Rogers936b37f2014-02-14 00:52:24 -080055 // Number of bytes for each out register in the caller method's frame.
56 static constexpr size_t kBytesStackArgLocation = 4;
Alexei Zavjalov41c507a2014-05-15 16:02:46 +070057 // Frame size in bytes of a callee-save frame for RefsAndArgs.
58 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize =
Andreas Gampe8228cdf2017-05-30 15:03:54 -070059 GetCalleeSaveFrameSize(kRuntimeISA, CalleeSaveType::kSaveRefsAndArgs);
Ian Rogers848871b2013-08-05 10:56:33 -070060#if defined(__arm__)
61 // The callee save frame is pointed to by SP.
62 // | argN | |
63 // | ... | |
64 // | arg4 | |
65 // | arg3 spill | | Caller's frame
66 // | arg2 spill | |
67 // | arg1 spill | |
68 // | Method* | ---
69 // | LR |
Zheng Xu5667fdb2014-10-23 18:29:55 +080070 // | ... | 4x6 bytes callee saves
71 // | R3 |
72 // | R2 |
73 // | R1 |
74 // | S15 |
75 // | : |
76 // | S0 |
77 // | | 4x2 bytes padding
Ian Rogers848871b2013-08-05 10:56:33 -070078 // | Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -050079 static constexpr bool kSplitPairAcrossRegisterAndStack = kArm32QuickCodeUseSoftFloat;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +000080 static constexpr bool kAlignPairRegister = !kArm32QuickCodeUseSoftFloat;
Zheng Xu5667fdb2014-10-23 18:29:55 +080081 static constexpr bool kQuickSoftFloatAbi = kArm32QuickCodeUseSoftFloat;
82 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = !kArm32QuickCodeUseSoftFloat;
Goran Jakovljevicff734982015-08-24 12:58:55 +000083 static constexpr bool kQuickSkipOddFpRegisters = false;
Zheng Xu5667fdb2014-10-23 18:29:55 +080084 static constexpr size_t kNumQuickGprArgs = 3;
85 static constexpr size_t kNumQuickFprArgs = kArm32QuickCodeUseSoftFloat ? 0 : 16;
Andreas Gampe1a5c4062015-01-15 12:10:47 -080086 static constexpr bool kGprFprLockstep = false;
Zheng Xub551fdc2014-07-25 11:49:42 +080087 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset =
Andreas Gampe8228cdf2017-05-30 15:03:54 -070088 arm::ArmCalleeSaveFpr1Offset(CalleeSaveType::kSaveRefsAndArgs); // Offset of first FPR arg.
Zheng Xub551fdc2014-07-25 11:49:42 +080089 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset =
Andreas Gampe8228cdf2017-05-30 15:03:54 -070090 arm::ArmCalleeSaveGpr1Offset(CalleeSaveType::kSaveRefsAndArgs); // Offset of first GPR arg.
Zheng Xub551fdc2014-07-25 11:49:42 +080091 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset =
Andreas Gampe8228cdf2017-05-30 15:03:54 -070092 arm::ArmCalleeSaveLrOffset(CalleeSaveType::kSaveRefsAndArgs); // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -080093 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +000094 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -080095 }
Stuart Monteithb95a5342014-03-12 13:32:32 +000096#elif defined(__aarch64__)
97 // The callee save frame is pointed to by SP.
98 // | argN | |
99 // | ... | |
100 // | arg4 | |
101 // | arg3 spill | | Caller's frame
102 // | arg2 spill | |
103 // | arg1 spill | |
104 // | Method* | ---
105 // | LR |
Zheng Xub551fdc2014-07-25 11:49:42 +0800106 // | X29 |
Stuart Monteithb95a5342014-03-12 13:32:32 +0000107 // | : |
Serban Constantinescu9bd88b02015-04-22 16:24:46 +0100108 // | X20 |
Stuart Monteithb95a5342014-03-12 13:32:32 +0000109 // | X7 |
110 // | : |
111 // | X1 |
Zheng Xub551fdc2014-07-25 11:49:42 +0800112 // | D7 |
Stuart Monteithb95a5342014-03-12 13:32:32 +0000113 // | : |
114 // | D0 |
115 // | | padding
116 // | Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500117 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000118 static constexpr bool kAlignPairRegister = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000119 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800120 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000121 static constexpr bool kQuickSkipOddFpRegisters = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000122 static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs.
123 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800124 static constexpr bool kGprFprLockstep = false;
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700125 // Offset of first FPR arg.
Zheng Xub551fdc2014-07-25 11:49:42 +0800126 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset =
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700127 arm64::Arm64CalleeSaveFpr1Offset(CalleeSaveType::kSaveRefsAndArgs);
128 // Offset of first GPR arg.
Zheng Xub551fdc2014-07-25 11:49:42 +0800129 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset =
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700130 arm64::Arm64CalleeSaveGpr1Offset(CalleeSaveType::kSaveRefsAndArgs);
131 // Offset of return address.
Zheng Xub551fdc2014-07-25 11:49:42 +0800132 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset =
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700133 arm64::Arm64CalleeSaveLrOffset(CalleeSaveType::kSaveRefsAndArgs);
Stuart Monteithb95a5342014-03-12 13:32:32 +0000134 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000135 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Stuart Monteithb95a5342014-03-12 13:32:32 +0000136 }
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800137#elif defined(__mips__) && !defined(__LP64__)
Ian Rogers848871b2013-08-05 10:56:33 -0700138 // The callee save frame is pointed to by SP.
139 // | argN | |
140 // | ... | |
141 // | arg4 | |
142 // | arg3 spill | | Caller's frame
143 // | arg2 spill | |
144 // | arg1 spill | |
145 // | Method* | ---
146 // | RA |
147 // | ... | callee saves
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800148 // | T1 | arg5
149 // | T0 | arg4
Ian Rogers848871b2013-08-05 10:56:33 -0700150 // | A3 | arg3
151 // | A2 | arg2
152 // | A1 | arg1
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800153 // | F19 |
154 // | F18 | f_arg5
155 // | F17 |
156 // | F16 | f_arg4
Goran Jakovljevicff734982015-08-24 12:58:55 +0000157 // | F15 |
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800158 // | F14 | f_arg3
Goran Jakovljevicff734982015-08-24 12:58:55 +0000159 // | F13 |
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800160 // | F12 | f_arg2
161 // | F11 |
162 // | F10 | f_arg1
163 // | F9 |
164 // | F8 | f_arg0
Goran Jakovljevicff734982015-08-24 12:58:55 +0000165 // | | padding
Ian Rogers848871b2013-08-05 10:56:33 -0700166 // | A0/Method* | <- sp
Goran Jakovljevicff734982015-08-24 12:58:55 +0000167 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
168 static constexpr bool kAlignPairRegister = true;
169 static constexpr bool kQuickSoftFloatAbi = false;
Zheng Xu5667fdb2014-10-23 18:29:55 +0800170 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000171 static constexpr bool kQuickSkipOddFpRegisters = true;
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800172 static constexpr size_t kNumQuickGprArgs = 5; // 5 arguments passed in GPRs.
173 static constexpr size_t kNumQuickFprArgs = 12; // 6 arguments passed in FPRs. Floats can be
174 // passed only in even numbered registers and each
175 // double occupies two registers.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800176 static constexpr bool kGprFprLockstep = false;
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800177 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 8; // Offset of first FPR arg.
178 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 56; // Offset of first GPR arg.
179 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 108; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800180 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000181 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800182 }
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800183#elif defined(__mips__) && defined(__LP64__)
184 // The callee save frame is pointed to by SP.
185 // | argN | |
186 // | ... | |
187 // | arg4 | |
188 // | arg3 spill | | Caller's frame
189 // | arg2 spill | |
190 // | arg1 spill | |
191 // | Method* | ---
192 // | RA |
193 // | ... | callee saves
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800194 // | A7 | arg7
195 // | A6 | arg6
196 // | A5 | arg5
197 // | A4 | arg4
198 // | A3 | arg3
199 // | A2 | arg2
200 // | A1 | arg1
Goran Jakovljevicff734982015-08-24 12:58:55 +0000201 // | F19 | f_arg7
202 // | F18 | f_arg6
203 // | F17 | f_arg5
204 // | F16 | f_arg4
205 // | F15 | f_arg3
206 // | F14 | f_arg2
207 // | F13 | f_arg1
208 // | F12 | f_arg0
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800209 // | | padding
210 // | A0/Method* | <- sp
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800211 // NOTE: for Mip64, when A0 is skipped, F12 is also skipped.
Douglas Leungd18e0832015-02-09 15:22:26 -0800212 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800213 static constexpr bool kAlignPairRegister = false;
214 static constexpr bool kQuickSoftFloatAbi = false;
215 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000216 static constexpr bool kQuickSkipOddFpRegisters = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800217 static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs.
218 static constexpr size_t kNumQuickFprArgs = 7; // 7 arguments passed in FPRs.
219 static constexpr bool kGprFprLockstep = true;
220
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800221 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 24; // Offset of first FPR arg (F13).
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800222 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 80; // Offset of first GPR arg (A1).
223 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 200; // Offset of return address.
224 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
225 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
226 }
Ian Rogers848871b2013-08-05 10:56:33 -0700227#elif defined(__i386__)
228 // The callee save frame is pointed to by SP.
229 // | argN | |
230 // | ... | |
231 // | arg4 | |
232 // | arg3 spill | | Caller's frame
233 // | arg2 spill | |
234 // | arg1 spill | |
235 // | Method* | ---
236 // | Return |
237 // | EBP,ESI,EDI | callee saves
238 // | EBX | arg3
239 // | EDX | arg2
240 // | ECX | arg1
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000241 // | XMM3 | float arg 4
242 // | XMM2 | float arg 3
243 // | XMM1 | float arg 2
244 // | XMM0 | float arg 1
Ian Rogers848871b2013-08-05 10:56:33 -0700245 // | EAX/Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500246 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000247 static constexpr bool kAlignPairRegister = false;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000248 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800249 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000250 static constexpr bool kQuickSkipOddFpRegisters = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800251 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs.
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000252 static constexpr size_t kNumQuickFprArgs = 4; // 4 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800253 static constexpr bool kGprFprLockstep = false;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000254 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 4; // Offset of first FPR arg.
255 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 4 + 4*8; // Offset of first GPR arg.
256 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 28 + 4*8; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800257 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000258 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800259 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800260#elif defined(__x86_64__)
Ian Rogers936b37f2014-02-14 00:52:24 -0800261 // The callee save frame is pointed to by SP.
262 // | argN | |
263 // | ... | |
264 // | reg. arg spills | | Caller's frame
265 // | Method* | ---
266 // | Return |
267 // | R15 | callee save
268 // | R14 | callee save
269 // | R13 | callee save
270 // | R12 | callee save
271 // | R9 | arg5
272 // | R8 | arg4
273 // | RSI/R6 | arg1
274 // | RBP/R5 | callee save
275 // | RBX/R3 | callee save
276 // | RDX/R2 | arg2
277 // | RCX/R1 | arg3
278 // | XMM7 | float arg 8
279 // | XMM6 | float arg 7
280 // | XMM5 | float arg 6
281 // | XMM4 | float arg 5
282 // | XMM3 | float arg 4
283 // | XMM2 | float arg 3
284 // | XMM1 | float arg 2
285 // | XMM0 | float arg 1
286 // | Padding |
287 // | RDI/Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500288 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000289 static constexpr bool kAlignPairRegister = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800290 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800291 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000292 static constexpr bool kQuickSkipOddFpRegisters = false;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700293 static constexpr size_t kNumQuickGprArgs = 5; // 5 arguments passed in GPRs.
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700294 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800295 static constexpr bool kGprFprLockstep = false;
Ian Rogers936b37f2014-02-14 00:52:24 -0800296 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 16; // Offset of first FPR arg.
Serguei Katkovc3801912014-07-08 17:21:53 +0700297 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 80 + 4*8; // Offset of first GPR arg.
298 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 168 + 4*8; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800299 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
300 switch (gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000301 case 0: return (4 * GetBytesPerGprSpillLocation(kRuntimeISA));
302 case 1: return (1 * GetBytesPerGprSpillLocation(kRuntimeISA));
303 case 2: return (0 * GetBytesPerGprSpillLocation(kRuntimeISA));
304 case 3: return (5 * GetBytesPerGprSpillLocation(kRuntimeISA));
305 case 4: return (6 * GetBytesPerGprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800306 default:
Andreas Gampec200a4a2014-06-16 18:39:09 -0700307 LOG(FATAL) << "Unexpected GPR index: " << gpr_index;
308 return 0;
Ian Rogers936b37f2014-02-14 00:52:24 -0800309 }
310 }
Ian Rogers848871b2013-08-05 10:56:33 -0700311#else
312#error "Unsupported architecture"
Ian Rogers848871b2013-08-05 10:56:33 -0700313#endif
314
Ian Rogers936b37f2014-02-14 00:52:24 -0800315 public:
Sebastien Hertza836bc92014-11-25 16:30:53 +0100316 // Special handling for proxy methods. Proxy methods are instance methods so the
317 // 'this' object is the 1st argument. They also have the same frame layout as the
318 // kRefAndArgs runtime method. Since 'this' is a reference, it is located in the
319 // 1st GPR.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700320 static mirror::Object* GetProxyThisObject(ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700321 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000322 CHECK((*sp)->IsProxyMethod());
Sebastien Hertza836bc92014-11-25 16:30:53 +0100323 CHECK_GT(kNumQuickGprArgs, 0u);
324 constexpr uint32_t kThisGprIndex = 0u; // 'this' is in the 1st GPR.
325 size_t this_arg_offset = kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset +
326 GprIndexToGprOffset(kThisGprIndex);
327 uint8_t* this_arg_address = reinterpret_cast<uint8_t*>(sp) + this_arg_offset;
328 return reinterpret_cast<StackReference<mirror::Object>*>(this_arg_address)->AsMirrorPtr();
329 }
330
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700331 static ArtMethod* GetCallingMethod(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700332 DCHECK((*sp)->IsCalleeSaveMethod());
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700333 return GetCalleeSaveMethodCaller(sp, CalleeSaveType::kSaveRefsAndArgs);
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +0100334 }
335
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700336 static ArtMethod* GetOuterMethod(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700337 DCHECK((*sp)->IsCalleeSaveMethod());
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100338 uint8_t* previous_sp =
339 reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700340 return *reinterpret_cast<ArtMethod**>(previous_sp);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100341 }
342
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700343 static uint32_t GetCallingDexPc(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700344 DCHECK((*sp)->IsCalleeSaveMethod());
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700345 const size_t callee_frame_size = GetCalleeSaveFrameSize(kRuntimeISA,
346 CalleeSaveType::kSaveRefsAndArgs);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700347 ArtMethod** caller_sp = reinterpret_cast<ArtMethod**>(
348 reinterpret_cast<uintptr_t>(sp) + callee_frame_size);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100349 uintptr_t outer_pc = QuickArgumentVisitor::GetCallingPc(sp);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100350 const OatQuickMethodHeader* current_code = (*caller_sp)->GetOatQuickMethodHeader(outer_pc);
351 uintptr_t outer_pc_offset = current_code->NativeQuickPcOffset(outer_pc);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100352
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100353 if (current_code->IsOptimized()) {
354 CodeInfo code_info = current_code->GetOptimizedCodeInfo();
David Srbecky09ed0982016-02-12 21:58:43 +0000355 CodeInfoEncoding encoding = code_info.ExtractEncoding();
David Brazdilf677ebf2015-05-29 16:29:43 +0100356 StackMap stack_map = code_info.GetStackMapForNativePcOffset(outer_pc_offset, encoding);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100357 DCHECK(stack_map.IsValid());
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800358 if (stack_map.HasInlineInfo(encoding.stack_map.encoding)) {
David Brazdilf677ebf2015-05-29 16:29:43 +0100359 InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map, encoding);
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800360 return inline_info.GetDexPcAtDepth(encoding.inline_info.encoding,
361 inline_info.GetDepth(encoding.inline_info.encoding)-1);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100362 } else {
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800363 return stack_map.GetDexPc(encoding.stack_map.encoding);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100364 }
365 } else {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100366 return current_code->ToDexPc(*caller_sp, outer_pc);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100367 }
Ian Rogers848871b2013-08-05 10:56:33 -0700368 }
369
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800370 static bool GetInvokeType(ArtMethod** sp, InvokeType* invoke_type, uint32_t* dex_method_index)
371 REQUIRES_SHARED(Locks::mutator_lock_) {
372 DCHECK((*sp)->IsCalleeSaveMethod());
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700373 const size_t callee_frame_size = GetCalleeSaveFrameSize(kRuntimeISA,
374 CalleeSaveType::kSaveRefsAndArgs);
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800375 ArtMethod** caller_sp = reinterpret_cast<ArtMethod**>(
376 reinterpret_cast<uintptr_t>(sp) + callee_frame_size);
377 uintptr_t outer_pc = QuickArgumentVisitor::GetCallingPc(sp);
378 const OatQuickMethodHeader* current_code = (*caller_sp)->GetOatQuickMethodHeader(outer_pc);
379 if (!current_code->IsOptimized()) {
380 return false;
381 }
382 uintptr_t outer_pc_offset = current_code->NativeQuickPcOffset(outer_pc);
383 CodeInfo code_info = current_code->GetOptimizedCodeInfo();
384 CodeInfoEncoding encoding = code_info.ExtractEncoding();
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700385 MethodInfo method_info = current_code->GetOptimizedMethodInfo();
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800386 InvokeInfo invoke(code_info.GetInvokeInfoForNativePcOffset(outer_pc_offset, encoding));
387 if (invoke.IsValid()) {
388 *invoke_type = static_cast<InvokeType>(invoke.GetInvokeType(encoding.invoke_info.encoding));
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700389 *dex_method_index = invoke.GetMethodIndex(encoding.invoke_info.encoding, method_info);
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800390 return true;
391 }
392 return false;
393 }
394
Ian Rogers936b37f2014-02-14 00:52:24 -0800395 // For the given quick ref and args quick frame, return the caller's PC.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700396 static uintptr_t GetCallingPc(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700397 DCHECK((*sp)->IsCalleeSaveMethod());
Ian Rogers13735952014-10-08 12:43:28 -0700398 uint8_t* lr = reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_LrOffset;
Ian Rogers848871b2013-08-05 10:56:33 -0700399 return *reinterpret_cast<uintptr_t*>(lr);
400 }
401
Mathieu Chartiere401d142015-04-22 13:56:20 -0700402 QuickArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700403 uint32_t shorty_len) REQUIRES_SHARED(Locks::mutator_lock_) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700404 is_static_(is_static), shorty_(shorty), shorty_len_(shorty_len),
Ian Rogers13735952014-10-08 12:43:28 -0700405 gpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset),
406 fpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset),
407 stack_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize
Mathieu Chartiere401d142015-04-22 13:56:20 -0700408 + sizeof(ArtMethod*)), // Skip ArtMethod*.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800409 gpr_index_(0), fpr_index_(0), fpr_double_index_(0), stack_index_(0),
410 cur_type_(Primitive::kPrimVoid), is_split_long_or_double_(false) {
Andreas Gampe575e78c2014-11-03 23:41:03 -0800411 static_assert(kQuickSoftFloatAbi == (kNumQuickFprArgs == 0),
412 "Number of Quick FPR arguments unexpected");
413 static_assert(!(kQuickSoftFloatAbi && kQuickDoubleRegAlignedFloatBackFilled),
414 "Double alignment unexpected");
Zheng Xu5667fdb2014-10-23 18:29:55 +0800415 // For register alignment, we want to assume that counters(fpr_double_index_) are even if the
416 // next register is even.
Andreas Gampe575e78c2014-11-03 23:41:03 -0800417 static_assert(!kQuickDoubleRegAlignedFloatBackFilled || kNumQuickFprArgs % 2 == 0,
418 "Number of Quick FPR arguments not even");
Andreas Gampe542451c2016-07-26 09:02:02 -0700419 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Zheng Xu5667fdb2014-10-23 18:29:55 +0800420 }
Ian Rogers848871b2013-08-05 10:56:33 -0700421
422 virtual ~QuickArgumentVisitor() {}
423
424 virtual void Visit() = 0;
425
Ian Rogers936b37f2014-02-14 00:52:24 -0800426 Primitive::Type GetParamPrimitiveType() const {
427 return cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700428 }
429
Ian Rogers13735952014-10-08 12:43:28 -0700430 uint8_t* GetParamAddress() const {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800431 if (!kQuickSoftFloatAbi) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800432 Primitive::Type type = GetParamPrimitiveType();
433 if (UNLIKELY((type == Primitive::kPrimDouble) || (type == Primitive::kPrimFloat))) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800434 if (type == Primitive::kPrimDouble && kQuickDoubleRegAlignedFloatBackFilled) {
435 if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) {
436 return fpr_args_ + (fpr_double_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
437 }
438 } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000439 return fpr_args_ + (fpr_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800440 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700441 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers936b37f2014-02-14 00:52:24 -0800442 }
443 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800444 if (gpr_index_ < kNumQuickGprArgs) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800445 return gpr_args_ + GprIndexToGprOffset(gpr_index_);
446 }
447 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700448 }
449
450 bool IsSplitLongOrDouble() const {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700451 if ((GetBytesPerGprSpillLocation(kRuntimeISA) == 4) ||
452 (GetBytesPerFprSpillLocation(kRuntimeISA) == 4)) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800453 return is_split_long_or_double_;
454 } else {
455 return false; // An optimization for when GPR and FPRs are 64bit.
456 }
Ian Rogers848871b2013-08-05 10:56:33 -0700457 }
458
Ian Rogers936b37f2014-02-14 00:52:24 -0800459 bool IsParamAReference() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700460 return GetParamPrimitiveType() == Primitive::kPrimNot;
461 }
462
Ian Rogers936b37f2014-02-14 00:52:24 -0800463 bool IsParamALongOrDouble() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700464 Primitive::Type type = GetParamPrimitiveType();
465 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
466 }
467
468 uint64_t ReadSplitLongParam() const {
Nicolas Geoffray425f2392015-01-08 14:52:29 +0000469 // The splitted long is always available through the stack.
470 return *reinterpret_cast<uint64_t*>(stack_args_
471 + stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700472 }
473
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800474 void IncGprIndex() {
475 gpr_index_++;
476 if (kGprFprLockstep) {
477 fpr_index_++;
478 }
479 }
480
481 void IncFprIndex() {
482 fpr_index_++;
483 if (kGprFprLockstep) {
484 gpr_index_++;
485 }
486 }
487
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700488 void VisitArguments() REQUIRES_SHARED(Locks::mutator_lock_) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800489 // (a) 'stack_args_' should point to the first method's argument
490 // (b) whatever the argument type it is, the 'stack_index_' should
491 // be moved forward along with every visiting.
Ian Rogers936b37f2014-02-14 00:52:24 -0800492 gpr_index_ = 0;
493 fpr_index_ = 0;
Zheng Xu5667fdb2014-10-23 18:29:55 +0800494 if (kQuickDoubleRegAlignedFloatBackFilled) {
495 fpr_double_index_ = 0;
496 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800497 stack_index_ = 0;
498 if (!is_static_) { // Handle this.
499 cur_type_ = Primitive::kPrimNot;
500 is_split_long_or_double_ = false;
Ian Rogers848871b2013-08-05 10:56:33 -0700501 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800502 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800503 if (kNumQuickGprArgs > 0) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800504 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800505 }
Ian Rogers848871b2013-08-05 10:56:33 -0700506 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800507 for (uint32_t shorty_index = 1; shorty_index < shorty_len_; ++shorty_index) {
508 cur_type_ = Primitive::GetType(shorty_[shorty_index]);
509 switch (cur_type_) {
510 case Primitive::kPrimNot:
511 case Primitive::kPrimBoolean:
512 case Primitive::kPrimByte:
513 case Primitive::kPrimChar:
514 case Primitive::kPrimShort:
515 case Primitive::kPrimInt:
516 is_split_long_or_double_ = false;
517 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800518 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800519 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800520 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800521 }
522 break;
523 case Primitive::kPrimFloat:
524 is_split_long_or_double_ = false;
525 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800526 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800527 if (kQuickSoftFloatAbi) {
528 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800529 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800530 }
531 } else {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800532 if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800533 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800534 if (kQuickDoubleRegAlignedFloatBackFilled) {
535 // Double should not overlap with float.
536 // For example, if fpr_index_ = 3, fpr_double_index_ should be at least 4.
537 fpr_double_index_ = std::max(fpr_double_index_, RoundUp(fpr_index_, 2));
538 // Float should not overlap with double.
539 if (fpr_index_ % 2 == 0) {
540 fpr_index_ = std::max(fpr_double_index_, fpr_index_);
541 }
Goran Jakovljevicff734982015-08-24 12:58:55 +0000542 } else if (kQuickSkipOddFpRegisters) {
543 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800544 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800545 }
546 }
547 break;
548 case Primitive::kPrimDouble:
549 case Primitive::kPrimLong:
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800550 if (kQuickSoftFloatAbi || (cur_type_ == Primitive::kPrimLong)) {
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800551 if (cur_type_ == Primitive::kPrimLong &&
552#if defined(__mips__) && !defined(__LP64__)
553 (gpr_index_ == 0 || gpr_index_ == 2) &&
554#else
555 gpr_index_ == 0 &&
556#endif
557 kAlignPairRegister) {
558 // Currently, this is only for ARM and MIPS, where we align long parameters with
559 // even-numbered registers by skipping R1 (on ARM) or A1(A3) (on MIPS) and using
560 // R2 (on ARM) or A2(T0) (on MIPS) instead.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800561 IncGprIndex();
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000562 }
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000563 is_split_long_or_double_ = (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) &&
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800564 ((gpr_index_ + 1) == kNumQuickGprArgs);
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500565 if (!kSplitPairAcrossRegisterAndStack && is_split_long_or_double_) {
566 // We don't want to split this. Pass over this register.
567 gpr_index_++;
568 is_split_long_or_double_ = false;
569 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800570 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800571 if (kBytesStackArgLocation == 4) {
572 stack_index_+= 2;
573 } else {
574 CHECK_EQ(kBytesStackArgLocation, 8U);
575 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800576 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700577 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800578 IncGprIndex();
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000579 if (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700580 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800581 IncGprIndex();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700582 }
583 }
584 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800585 } else {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000586 is_split_long_or_double_ = (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) &&
Zheng Xu5667fdb2014-10-23 18:29:55 +0800587 ((fpr_index_ + 1) == kNumQuickFprArgs) && !kQuickDoubleRegAlignedFloatBackFilled;
Ian Rogers936b37f2014-02-14 00:52:24 -0800588 Visit();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700589 if (kBytesStackArgLocation == 4) {
590 stack_index_+= 2;
Ian Rogers936b37f2014-02-14 00:52:24 -0800591 } else {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700592 CHECK_EQ(kBytesStackArgLocation, 8U);
593 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800594 }
Zheng Xu5667fdb2014-10-23 18:29:55 +0800595 if (kQuickDoubleRegAlignedFloatBackFilled) {
596 if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) {
597 fpr_double_index_ += 2;
598 // Float should not overlap with double.
599 if (fpr_index_ % 2 == 0) {
600 fpr_index_ = std::max(fpr_double_index_, fpr_index_);
601 }
602 }
603 } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800604 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800605 if (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) {
606 if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800607 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800608 }
609 }
610 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800611 }
612 break;
613 default:
614 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty_;
615 }
Ian Rogers848871b2013-08-05 10:56:33 -0700616 }
617 }
618
Andreas Gampec200a4a2014-06-16 18:39:09 -0700619 protected:
Ian Rogers848871b2013-08-05 10:56:33 -0700620 const bool is_static_;
621 const char* const shorty_;
622 const uint32_t shorty_len_;
Andreas Gampec200a4a2014-06-16 18:39:09 -0700623
624 private:
Ian Rogers13735952014-10-08 12:43:28 -0700625 uint8_t* const gpr_args_; // Address of GPR arguments in callee save frame.
626 uint8_t* const fpr_args_; // Address of FPR arguments in callee save frame.
627 uint8_t* const stack_args_; // Address of stack arguments in caller's frame.
Ian Rogers936b37f2014-02-14 00:52:24 -0800628 uint32_t gpr_index_; // Index into spilled GPRs.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800629 // Index into spilled FPRs.
630 // In case kQuickDoubleRegAlignedFloatBackFilled, it may index a hole while fpr_double_index_
631 // holds a higher register number.
632 uint32_t fpr_index_;
633 // Index into spilled FPRs for aligned double.
634 // Only used when kQuickDoubleRegAlignedFloatBackFilled. Next available double register indexed in
635 // terms of singles, may be behind fpr_index.
636 uint32_t fpr_double_index_;
Ian Rogers936b37f2014-02-14 00:52:24 -0800637 uint32_t stack_index_; // Index into arguments on the stack.
638 // The current type of argument during VisitArguments.
639 Primitive::Type cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700640 // Does a 64bit parameter straddle the register and stack arguments?
641 bool is_split_long_or_double_;
642};
643
Sebastien Hertza836bc92014-11-25 16:30:53 +0100644// Returns the 'this' object of a proxy method. This function is only used by StackVisitor. It
645// allows to use the QuickArgumentVisitor constants without moving all the code in its own module.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700646extern "C" mirror::Object* artQuickGetProxyThisObject(ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700647 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertza836bc92014-11-25 16:30:53 +0100648 return QuickArgumentVisitor::GetProxyThisObject(sp);
649}
650
Ian Rogers848871b2013-08-05 10:56:33 -0700651// Visits arguments on the stack placing them into the shadow frame.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800652class BuildQuickShadowFrameVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700653 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700654 BuildQuickShadowFrameVisitor(ArtMethod** sp, bool is_static, const char* shorty,
655 uint32_t shorty_len, ShadowFrame* sf, size_t first_arg_reg) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700656 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), sf_(sf), cur_reg_(first_arg_reg) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700657
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700658 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700659
660 private:
Ian Rogers936b37f2014-02-14 00:52:24 -0800661 ShadowFrame* const sf_;
662 uint32_t cur_reg_;
Ian Rogers848871b2013-08-05 10:56:33 -0700663
Dragos Sbirleabd136a22013-08-13 18:07:04 -0700664 DISALLOW_COPY_AND_ASSIGN(BuildQuickShadowFrameVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700665};
666
Andreas Gampec200a4a2014-06-16 18:39:09 -0700667void BuildQuickShadowFrameVisitor::Visit() {
Ian Rogers9758f792014-03-13 09:02:55 -0700668 Primitive::Type type = GetParamPrimitiveType();
669 switch (type) {
670 case Primitive::kPrimLong: // Fall-through.
671 case Primitive::kPrimDouble:
672 if (IsSplitLongOrDouble()) {
673 sf_->SetVRegLong(cur_reg_, ReadSplitLongParam());
674 } else {
675 sf_->SetVRegLong(cur_reg_, *reinterpret_cast<jlong*>(GetParamAddress()));
676 }
677 ++cur_reg_;
678 break;
679 case Primitive::kPrimNot: {
680 StackReference<mirror::Object>* stack_ref =
681 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
682 sf_->SetVRegReference(cur_reg_, stack_ref->AsMirrorPtr());
683 }
684 break;
685 case Primitive::kPrimBoolean: // Fall-through.
686 case Primitive::kPrimByte: // Fall-through.
687 case Primitive::kPrimChar: // Fall-through.
688 case Primitive::kPrimShort: // Fall-through.
689 case Primitive::kPrimInt: // Fall-through.
690 case Primitive::kPrimFloat:
691 sf_->SetVReg(cur_reg_, *reinterpret_cast<jint*>(GetParamAddress()));
692 break;
693 case Primitive::kPrimVoid:
694 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700695 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -0700696 }
697 ++cur_reg_;
698}
699
Mathieu Chartiere401d142015-04-22 13:56:20 -0700700extern "C" uint64_t artQuickToInterpreterBridge(ArtMethod* method, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700701 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers848871b2013-08-05 10:56:33 -0700702 // Ensure we don't get thread suspension until the object arguments are safely in the shadow
703 // frame.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700704 ScopedQuickEntrypointChecks sqec(self);
Ian Rogers848871b2013-08-05 10:56:33 -0700705
Alex Light9139e002015-10-09 15:59:48 -0700706 if (UNLIKELY(!method->IsInvokable())) {
707 method->ThrowInvocationTimeError();
Ian Rogers848871b2013-08-05 10:56:33 -0700708 return 0;
Andreas Gampe639bdd12015-06-03 11:22:45 -0700709 }
710
711 JValue tmp_value;
712 ShadowFrame* deopt_frame = self->PopStackedShadowFrame(
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700713 StackedShadowFrameType::kDeoptimizationShadowFrame, false);
Andreas Gampe639bdd12015-06-03 11:22:45 -0700714 ManagedStack fragment;
715
David Sehr709b0702016-10-13 09:12:37 -0700716 DCHECK(!method->IsNative()) << method->PrettyMethod();
Andreas Gampe639bdd12015-06-03 11:22:45 -0700717 uint32_t shorty_len = 0;
Andreas Gampe542451c2016-07-26 09:02:02 -0700718 ArtMethod* non_proxy_method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Alex Lighte9dd04f2016-03-16 16:09:45 -0700719 const DexFile::CodeItem* code_item = non_proxy_method->GetCodeItem();
David Sehr709b0702016-10-13 09:12:37 -0700720 DCHECK(code_item != nullptr) << method->PrettyMethod();
Andreas Gampe639bdd12015-06-03 11:22:45 -0700721 const char* shorty = non_proxy_method->GetShorty(&shorty_len);
722
723 JValue result;
724
725 if (deopt_frame != nullptr) {
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700726 // Coming from partial-fragment deopt.
Andreas Gampe639bdd12015-06-03 11:22:45 -0700727
728 if (kIsDebugBuild) {
729 // Sanity-check: are the methods as expected? We check that the last shadow frame (the bottom
730 // of the call-stack) corresponds to the called method.
731 ShadowFrame* linked = deopt_frame;
732 while (linked->GetLink() != nullptr) {
733 linked = linked->GetLink();
734 }
David Sehr709b0702016-10-13 09:12:37 -0700735 CHECK_EQ(method, linked->GetMethod()) << method->PrettyMethod() << " "
736 << ArtMethod::PrettyMethod(linked->GetMethod());
Andreas Gampe639bdd12015-06-03 11:22:45 -0700737 }
738
739 if (VLOG_IS_ON(deopt)) {
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700740 // Print out the stack to verify that it was a partial-fragment deopt.
Andreas Gampe639bdd12015-06-03 11:22:45 -0700741 LOG(INFO) << "Continue-ing from deopt. Stack is:";
742 QuickExceptionHandler::DumpFramesWithType(self, true);
743 }
744
Mathieu Chartierf5769e12017-01-10 15:54:41 -0800745 ObjPtr<mirror::Throwable> pending_exception;
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100746 bool from_code = false;
Mingyao Yang2ee17902017-08-30 11:37:08 -0700747 DeoptimizationMethodType method_type;
748 self->PopDeoptimizationContext(/* out */ &result,
749 /* out */ &pending_exception,
750 /* out */ &from_code,
751 /* out */ &method_type);
Andreas Gampe639bdd12015-06-03 11:22:45 -0700752
753 // Push a transition back into managed code onto the linked list in thread.
754 self->PushManagedStackFragment(&fragment);
755
756 // Ensure that the stack is still in order.
757 if (kIsDebugBuild) {
758 class DummyStackVisitor : public StackVisitor {
759 public:
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700760 explicit DummyStackVisitor(Thread* self_in) REQUIRES_SHARED(Locks::mutator_lock_)
Andreas Gampe639bdd12015-06-03 11:22:45 -0700761 : StackVisitor(self_in, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {}
762
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700763 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe639bdd12015-06-03 11:22:45 -0700764 // Nothing to do here. In a debug build, SanityCheckFrame will do the work in the walking
765 // logic. Just always say we want to continue.
766 return true;
767 }
768 };
769 DummyStackVisitor dsv(self);
770 dsv.WalkStack();
771 }
772
773 // Restore the exception that was pending before deoptimization then interpret the
774 // deoptimized frames.
775 if (pending_exception != nullptr) {
776 self->SetException(pending_exception);
777 }
Mingyao Yang2ee17902017-08-30 11:37:08 -0700778 interpreter::EnterInterpreterFromDeoptimize(self,
779 deopt_frame,
780 &result,
781 from_code,
782 DeoptimizationMethodType::kDefault);
Ian Rogers848871b2013-08-05 10:56:33 -0700783 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -0700784 const char* old_cause = self->StartAssertNoThreadSuspension(
785 "Building interpreter shadow frame");
Ian Rogers848871b2013-08-05 10:56:33 -0700786 uint16_t num_regs = code_item->registers_size_;
Andreas Gampec200a4a2014-06-16 18:39:09 -0700787 // No last shadow coming from quick.
Andreas Gampeb3025922015-09-01 14:45:00 -0700788 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
Andreas Gampe03ec9302015-08-27 17:41:47 -0700789 CREATE_SHADOW_FRAME(num_regs, /* link */ nullptr, method, /* dex pc */ 0);
Andreas Gampeb3025922015-09-01 14:45:00 -0700790 ShadowFrame* shadow_frame = shadow_frame_unique_ptr.get();
Ian Rogers848871b2013-08-05 10:56:33 -0700791 size_t first_arg_reg = code_item->registers_size_ - code_item->ins_size_;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700792 BuildQuickShadowFrameVisitor shadow_frame_builder(sp, method->IsStatic(), shorty, shorty_len,
Ian Rogers936b37f2014-02-14 00:52:24 -0800793 shadow_frame, first_arg_reg);
Ian Rogers848871b2013-08-05 10:56:33 -0700794 shadow_frame_builder.VisitArguments();
Ian Rogerse94652f2014-12-02 11:13:19 -0800795 const bool needs_initialization =
796 method->IsStatic() && !method->GetDeclaringClass()->IsInitialized();
Ian Rogers848871b2013-08-05 10:56:33 -0700797 // Push a transition back into managed code onto the linked list in thread.
Ian Rogers848871b2013-08-05 10:56:33 -0700798 self->PushManagedStackFragment(&fragment);
799 self->PushShadowFrame(shadow_frame);
800 self->EndAssertNoThreadSuspension(old_cause);
801
Ian Rogerse94652f2014-12-02 11:13:19 -0800802 if (needs_initialization) {
Ian Rogers848871b2013-08-05 10:56:33 -0700803 // Ensure static method's class is initialized.
Ian Rogerse94652f2014-12-02 11:13:19 -0800804 StackHandleScope<1> hs(self);
805 Handle<mirror::Class> h_class(hs.NewHandle(shadow_frame->GetMethod()->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700806 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
David Sehr709b0702016-10-13 09:12:37 -0700807 DCHECK(Thread::Current()->IsExceptionPending())
808 << shadow_frame->GetMethod()->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700809 self->PopManagedStackFragment(fragment);
810 return 0;
811 }
812 }
Daniel Mihalyieb076692014-08-22 17:33:31 +0200813
Andreas Gampe639bdd12015-06-03 11:22:45 -0700814 result = interpreter::EnterInterpreterFromEntryPoint(self, code_item, shadow_frame);
Ian Rogers848871b2013-08-05 10:56:33 -0700815 }
Andreas Gampe639bdd12015-06-03 11:22:45 -0700816
817 // Pop transition.
818 self->PopManagedStackFragment(fragment);
819
820 // Request a stack deoptimization if needed
821 ArtMethod* caller = QuickArgumentVisitor::GetCallingMethod(sp);
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700822 uintptr_t caller_pc = QuickArgumentVisitor::GetCallingPc(sp);
Mingyao Yanga3549d22016-06-02 17:01:02 -0700823 // If caller_pc is the instrumentation exit stub, the stub will check to see if deoptimization
824 // should be done and it knows the real return pc.
825 if (UNLIKELY(caller_pc != reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) &&
Nicolas Geoffray433b79a2017-01-30 20:54:45 +0000826 Dbg::IsForcedInterpreterNeededForUpcall(self, caller))) {
827 if (!Runtime::Current()->IsAsyncDeoptimizeable(caller_pc)) {
828 LOG(WARNING) << "Got a deoptimization request on un-deoptimizable method "
829 << caller->PrettyMethod();
830 } else {
831 // Push the context of the deoptimization stack so we can restore the return value and the
832 // exception before executing the deoptimized frames.
833 self->PushDeoptimizationContext(
Mingyao Yang2ee17902017-08-30 11:37:08 -0700834 result,
835 shorty[0] == 'L' || shorty[0] == '[', /* class or array */
836 self->GetException(),
837 false /* from_code */,
838 DeoptimizationMethodType::kDefault);
Andreas Gampe639bdd12015-06-03 11:22:45 -0700839
Nicolas Geoffray433b79a2017-01-30 20:54:45 +0000840 // Set special exception to cause deoptimization.
841 self->SetException(Thread::GetDeoptimizationException());
842 }
Andreas Gampe639bdd12015-06-03 11:22:45 -0700843 }
844
845 // No need to restore the args since the method has already been run by the interpreter.
846 return result.GetJ();
Ian Rogers848871b2013-08-05 10:56:33 -0700847}
848
849// Visits arguments on the stack placing them into the args vector, Object* arguments are converted
850// to jobjects.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800851class BuildQuickArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700852 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700853 BuildQuickArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty, uint32_t shorty_len,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700854 ScopedObjectAccessUnchecked* soa, std::vector<jvalue>* args) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700855 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa), args_(args) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700856
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700857 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700858
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700859 void FixupReferences() REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800860
Ian Rogers848871b2013-08-05 10:56:33 -0700861 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700862 ScopedObjectAccessUnchecked* const soa_;
863 std::vector<jvalue>* const args_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800864 // References which we must update when exiting in case the GC moved the objects.
Ian Rogers700a4022014-05-19 16:49:03 -0700865 std::vector<std::pair<jobject, StackReference<mirror::Object>*>> references_;
Ian Rogers9758f792014-03-13 09:02:55 -0700866
Ian Rogers848871b2013-08-05 10:56:33 -0700867 DISALLOW_COPY_AND_ASSIGN(BuildQuickArgumentVisitor);
868};
869
Ian Rogers9758f792014-03-13 09:02:55 -0700870void BuildQuickArgumentVisitor::Visit() {
871 jvalue val;
872 Primitive::Type type = GetParamPrimitiveType();
873 switch (type) {
874 case Primitive::kPrimNot: {
875 StackReference<mirror::Object>* stack_ref =
876 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
877 val.l = soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
878 references_.push_back(std::make_pair(val.l, stack_ref));
879 break;
880 }
881 case Primitive::kPrimLong: // Fall-through.
882 case Primitive::kPrimDouble:
883 if (IsSplitLongOrDouble()) {
884 val.j = ReadSplitLongParam();
885 } else {
886 val.j = *reinterpret_cast<jlong*>(GetParamAddress());
887 }
888 break;
889 case Primitive::kPrimBoolean: // Fall-through.
890 case Primitive::kPrimByte: // Fall-through.
891 case Primitive::kPrimChar: // Fall-through.
892 case Primitive::kPrimShort: // Fall-through.
893 case Primitive::kPrimInt: // Fall-through.
894 case Primitive::kPrimFloat:
895 val.i = *reinterpret_cast<jint*>(GetParamAddress());
896 break;
897 case Primitive::kPrimVoid:
898 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700899 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -0700900 }
901 args_->push_back(val);
902}
903
904void BuildQuickArgumentVisitor::FixupReferences() {
905 // Fixup any references which may have changed.
906 for (const auto& pair : references_) {
Mathieu Chartier1a5337f2016-10-13 13:48:23 -0700907 pair.second->Assign(soa_->Decode<mirror::Object>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -0700908 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -0700909 }
910}
Ian Rogers848871b2013-08-05 10:56:33 -0700911// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
912// which is responsible for recording callee save registers. We explicitly place into jobjects the
913// incoming reference arguments (so they survive GC). We invoke the invocation handler, which is a
914// field within the proxy object, which will box the primitive arguments and deal with error cases.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700915extern "C" uint64_t artQuickProxyInvokeHandler(
916 ArtMethod* proxy_method, mirror::Object* receiver, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700917 REQUIRES_SHARED(Locks::mutator_lock_) {
David Sehr709b0702016-10-13 09:12:37 -0700918 DCHECK(proxy_method->IsProxyMethod()) << proxy_method->PrettyMethod();
919 DCHECK(receiver->GetClass()->IsProxyClass()) << proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700920 // Ensure we don't get thread suspension until the object arguments are safely in jobjects.
921 const char* old_cause =
922 self->StartAssertNoThreadSuspension("Adding to IRT proxy object arguments");
923 // Register the top of the managed stack, making stack crawlable.
David Sehr709b0702016-10-13 09:12:37 -0700924 DCHECK_EQ((*sp), proxy_method) << proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700925 self->VerifyStack();
926 // Start new JNI local reference state.
927 JNIEnvExt* env = self->GetJniEnv();
928 ScopedObjectAccessUnchecked soa(env);
929 ScopedJniEnvLocalRefState env_state(env);
930 // Create local ref. copies of proxy method and the receiver.
931 jobject rcvr_jobj = soa.AddLocalReference<jobject>(receiver);
932
933 // Placing arguments into args vector and remove the receiver.
Andreas Gampe542451c2016-07-26 09:02:02 -0700934 ArtMethod* non_proxy_method = proxy_method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
David Sehr709b0702016-10-13 09:12:37 -0700935 CHECK(!non_proxy_method->IsStatic()) << proxy_method->PrettyMethod() << " "
936 << non_proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700937 std::vector<jvalue> args;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700938 uint32_t shorty_len = 0;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700939 const char* shorty = non_proxy_method->GetShorty(&shorty_len);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700940 BuildQuickArgumentVisitor local_ref_visitor(sp, false, shorty, shorty_len, &soa, &args);
Brian Carlstromd3633d52013-08-20 21:06:26 -0700941
Ian Rogers848871b2013-08-05 10:56:33 -0700942 local_ref_visitor.VisitArguments();
David Sehr709b0702016-10-13 09:12:37 -0700943 DCHECK_GT(args.size(), 0U) << proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700944 args.erase(args.begin());
945
946 // Convert proxy method into expected interface method.
Andreas Gampe542451c2016-07-26 09:02:02 -0700947 ArtMethod* interface_method = proxy_method->FindOverriddenMethod(kRuntimePointerSize);
David Sehr709b0702016-10-13 09:12:37 -0700948 DCHECK(interface_method != nullptr) << proxy_method->PrettyMethod();
949 DCHECK(!interface_method->IsProxyMethod()) << interface_method->PrettyMethod();
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700950 self->EndAssertNoThreadSuspension(old_cause);
Andreas Gampe542451c2016-07-26 09:02:02 -0700951 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Andreas Gampee01e3642016-07-25 13:06:04 -0700952 DCHECK(!Runtime::Current()->IsActiveTransaction());
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700953 jobject interface_method_jobj = soa.AddLocalReference<jobject>(
Andreas Gampe542451c2016-07-26 09:02:02 -0700954 mirror::Method::CreateFromArtMethod<kRuntimePointerSize, false>(soa.Self(),
955 interface_method));
Ian Rogers848871b2013-08-05 10:56:33 -0700956
957 // All naked Object*s should now be in jobjects, so its safe to go into the main invoke code
958 // that performs allocations.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700959 JValue result = InvokeProxyInvocationHandler(soa, shorty, rcvr_jobj, interface_method_jobj, args);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800960 // Restore references which might have moved.
961 local_ref_visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -0700962 return result.GetJ();
963}
964
965// Read object references held in arguments from quick frames and place in a JNI local references,
966// so they don't get garbage collected.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800967class RememberForGcArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700968 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700969 RememberForGcArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty,
970 uint32_t shorty_len, ScopedObjectAccessUnchecked* soa) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700971 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700972
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700973 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Mathieu Chartier07d447b2013-09-26 11:57:43 -0700974
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700975 void FixupReferences() REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers848871b2013-08-05 10:56:33 -0700976
977 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700978 ScopedObjectAccessUnchecked* const soa_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800979 // References which we must update when exiting in case the GC moved the objects.
Andreas Gampec200a4a2014-06-16 18:39:09 -0700980 std::vector<std::pair<jobject, StackReference<mirror::Object>*> > references_;
981
Mathieu Chartier590fee92013-09-13 13:46:47 -0700982 DISALLOW_COPY_AND_ASSIGN(RememberForGcArgumentVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700983};
984
Ian Rogers9758f792014-03-13 09:02:55 -0700985void RememberForGcArgumentVisitor::Visit() {
986 if (IsParamAReference()) {
987 StackReference<mirror::Object>* stack_ref =
988 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
989 jobject reference =
990 soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
991 references_.push_back(std::make_pair(reference, stack_ref));
992 }
993}
994
995void RememberForGcArgumentVisitor::FixupReferences() {
996 // Fixup any references which may have changed.
997 for (const auto& pair : references_) {
Mathieu Chartier1a5337f2016-10-13 13:48:23 -0700998 pair.second->Assign(soa_->Decode<mirror::Object>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -0700999 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -07001000 }
1001}
1002
Alex Lightb7edcda2017-04-27 13:20:31 -07001003extern "C" const void* artInstrumentationMethodEntryFromCode(ArtMethod* method,
1004 mirror::Object* this_object,
1005 Thread* self,
1006 ArtMethod** sp)
1007 REQUIRES_SHARED(Locks::mutator_lock_) {
1008 const void* result;
1009 // Instrumentation changes the stack. Thus, when exiting, the stack cannot be verified, so skip
1010 // that part.
1011 ScopedQuickEntrypointChecks sqec(self, kIsDebugBuild, false);
1012 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
1013 if (instrumentation->IsDeoptimized(method)) {
1014 result = GetQuickToInterpreterBridge();
1015 } else {
1016 result = instrumentation->GetQuickCodeFor(method, kRuntimePointerSize);
1017 DCHECK(!Runtime::Current()->GetClassLinker()->IsQuickToInterpreterBridge(result));
1018 }
1019
1020 bool interpreter_entry = (result == GetQuickToInterpreterBridge());
1021 bool is_static = method->IsStatic();
1022 uint32_t shorty_len;
1023 const char* shorty =
1024 method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty(&shorty_len);
1025
1026 ScopedObjectAccessUnchecked soa(self);
1027 RememberForGcArgumentVisitor visitor(sp, is_static, shorty, shorty_len, &soa);
1028 visitor.VisitArguments();
1029
1030 instrumentation->PushInstrumentationStackFrame(self,
1031 is_static ? nullptr : this_object,
1032 method,
1033 QuickArgumentVisitor::GetCallingPc(sp),
1034 interpreter_entry);
1035
1036 visitor.FixupReferences();
1037 if (UNLIKELY(self->IsExceptionPending())) {
1038 return nullptr;
1039 }
1040 CHECK(result != nullptr) << method->PrettyMethod();
1041 return result;
1042}
1043
1044extern "C" TwoWordReturn artInstrumentationMethodExitFromCode(Thread* self,
1045 ArtMethod** sp,
1046 uint64_t* gpr_result,
1047 uint64_t* fpr_result)
1048 REQUIRES_SHARED(Locks::mutator_lock_) {
1049 DCHECK_EQ(reinterpret_cast<uintptr_t>(self), reinterpret_cast<uintptr_t>(Thread::Current()));
1050 CHECK(gpr_result != nullptr);
1051 CHECK(fpr_result != nullptr);
1052 // Instrumentation exit stub must not be entered with a pending exception.
1053 CHECK(!self->IsExceptionPending()) << "Enter instrumentation exit stub with pending exception "
1054 << self->GetException()->Dump();
1055 // Compute address of return PC and sanity check that it currently holds 0.
Mingyao Yang2ee17902017-08-30 11:37:08 -07001056 size_t return_pc_offset = GetCalleeSaveReturnPcOffset(kRuntimeISA,
1057 CalleeSaveType::kSaveEverything);
Alex Lightb7edcda2017-04-27 13:20:31 -07001058 uintptr_t* return_pc = reinterpret_cast<uintptr_t*>(reinterpret_cast<uint8_t*>(sp) +
1059 return_pc_offset);
1060 CHECK_EQ(*return_pc, 0U);
1061
1062 // Pop the frame filling in the return pc. The low half of the return value is 0 when
1063 // deoptimization shouldn't be performed with the high-half having the return address. When
1064 // deoptimization should be performed the low half is zero and the high-half the address of the
1065 // deoptimization entry point.
1066 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
1067 TwoWordReturn return_or_deoptimize_pc = instrumentation->PopInstrumentationStackFrame(
1068 self, return_pc, gpr_result, fpr_result);
1069 if (self->IsExceptionPending()) {
1070 return GetTwoWordFailureValue();
1071 }
1072 return return_or_deoptimize_pc;
1073}
1074
Ian Rogers848871b2013-08-05 10:56:33 -07001075// Lazily resolve a method for quick. Called by stub code.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001076extern "C" const void* artQuickResolutionTrampoline(
1077 ArtMethod* called, mirror::Object* receiver, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001078 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe3b45ef22015-05-26 21:34:09 -07001079 // The resolution trampoline stashes the resolved method into the callee-save frame to transport
1080 // it. Thus, when exiting, the stack cannot be verified (as the resolved method most likely
1081 // does not have the same stack layout as the callee-save method).
1082 ScopedQuickEntrypointChecks sqec(self, kIsDebugBuild, false);
Ian Rogers848871b2013-08-05 10:56:33 -07001083 // Start new JNI local reference state
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001084 JNIEnvExt* env = self->GetJniEnv();
Ian Rogers848871b2013-08-05 10:56:33 -07001085 ScopedObjectAccessUnchecked soa(env);
1086 ScopedJniEnvLocalRefState env_state(env);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001087 const char* old_cause = self->StartAssertNoThreadSuspension("Quick method resolution set up");
Ian Rogers848871b2013-08-05 10:56:33 -07001088
1089 // Compute details about the called method (avoid GCs)
1090 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Ian Rogers848871b2013-08-05 10:56:33 -07001091 InvokeType invoke_type;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001092 MethodReference called_method(nullptr, 0);
1093 const bool called_method_known_on_entry = !called->IsRuntimeMethod();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001094 ArtMethod* caller = nullptr;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001095 if (!called_method_known_on_entry) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01001096 caller = QuickArgumentVisitor::GetCallingMethod(sp);
Ian Rogerse0a02da2014-12-02 14:10:53 -08001097 called_method.dex_file = caller->GetDexFile();
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001098
1099 InvokeType stack_map_invoke_type;
1100 uint32_t stack_map_dex_method_idx;
1101 const bool found_stack_map = QuickArgumentVisitor::GetInvokeType(sp,
1102 &stack_map_invoke_type,
1103 &stack_map_dex_method_idx);
1104 // For debug builds, we make sure both of the paths are consistent by also looking at the dex
1105 // code.
1106 if (!found_stack_map || kIsDebugBuild) {
1107 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
1108 const DexFile::CodeItem* code;
1109 code = caller->GetCodeItem();
1110 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
1111 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
1112 Instruction::Code instr_code = instr->Opcode();
1113 bool is_range;
1114 switch (instr_code) {
1115 case Instruction::INVOKE_DIRECT:
1116 invoke_type = kDirect;
1117 is_range = false;
1118 break;
1119 case Instruction::INVOKE_DIRECT_RANGE:
1120 invoke_type = kDirect;
1121 is_range = true;
1122 break;
1123 case Instruction::INVOKE_STATIC:
1124 invoke_type = kStatic;
1125 is_range = false;
1126 break;
1127 case Instruction::INVOKE_STATIC_RANGE:
1128 invoke_type = kStatic;
1129 is_range = true;
1130 break;
1131 case Instruction::INVOKE_SUPER:
1132 invoke_type = kSuper;
1133 is_range = false;
1134 break;
1135 case Instruction::INVOKE_SUPER_RANGE:
1136 invoke_type = kSuper;
1137 is_range = true;
1138 break;
1139 case Instruction::INVOKE_VIRTUAL:
1140 invoke_type = kVirtual;
1141 is_range = false;
1142 break;
1143 case Instruction::INVOKE_VIRTUAL_RANGE:
1144 invoke_type = kVirtual;
1145 is_range = true;
1146 break;
1147 case Instruction::INVOKE_INTERFACE:
1148 invoke_type = kInterface;
1149 is_range = false;
1150 break;
1151 case Instruction::INVOKE_INTERFACE_RANGE:
1152 invoke_type = kInterface;
1153 is_range = true;
1154 break;
1155 default:
1156 LOG(FATAL) << "Unexpected call into trampoline: " << instr->DumpString(nullptr);
1157 UNREACHABLE();
1158 }
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001159 called_method.index = (is_range) ? instr->VRegB_3rc() : instr->VRegB_35c();
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001160 // Check that the invoke matches what we expected, note that this path only happens for debug
1161 // builds.
1162 if (found_stack_map) {
1163 DCHECK_EQ(stack_map_invoke_type, invoke_type);
1164 if (invoke_type != kSuper) {
1165 // Super may be sharpened.
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001166 DCHECK_EQ(stack_map_dex_method_idx, called_method.index)
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001167 << called_method.dex_file->PrettyMethod(stack_map_dex_method_idx) << " "
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001168 << called_method.PrettyMethod();
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001169 }
1170 } else {
Andreas Gampe9e6dee22017-04-11 13:50:23 -07001171 VLOG(dex) << "Accessed dex file for invoke " << invoke_type << " "
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001172 << called_method.index;
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001173 }
1174 } else {
1175 invoke_type = stack_map_invoke_type;
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001176 called_method.index = stack_map_dex_method_idx;
Ian Rogers848871b2013-08-05 10:56:33 -07001177 }
Ian Rogers848871b2013-08-05 10:56:33 -07001178 } else {
1179 invoke_type = kStatic;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001180 called_method.dex_file = called->GetDexFile();
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001181 called_method.index = called->GetDexMethodIndex();
Ian Rogers848871b2013-08-05 10:56:33 -07001182 }
1183 uint32_t shorty_len;
1184 const char* shorty =
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001185 called_method.dex_file->GetMethodShorty(called_method.GetMethodId(), &shorty_len);
Mathieu Chartier590fee92013-09-13 13:46:47 -07001186 RememberForGcArgumentVisitor visitor(sp, invoke_type == kStatic, shorty, shorty_len, &soa);
Ian Rogers848871b2013-08-05 10:56:33 -07001187 visitor.VisitArguments();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001188 self->EndAssertNoThreadSuspension(old_cause);
Ian Rogerse0a02da2014-12-02 14:10:53 -08001189 const bool virtual_or_interface = invoke_type == kVirtual || invoke_type == kInterface;
Ian Rogers848871b2013-08-05 10:56:33 -07001190 // Resolve method filling in dex cache.
Ian Rogerse0a02da2014-12-02 14:10:53 -08001191 if (!called_method_known_on_entry) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001192 StackHandleScope<1> hs(self);
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001193 mirror::Object* dummy = nullptr;
1194 HandleWrapper<mirror::Object> h_receiver(
1195 hs.NewHandleWrapper(virtual_or_interface ? &receiver : &dummy));
Ian Rogerse0a02da2014-12-02 14:10:53 -08001196 DCHECK_EQ(caller->GetDexFile(), called_method.dex_file);
Vladimir Markoba118822017-06-12 15:41:56 +01001197 called = linker->ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>(
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001198 self, called_method.index, caller, invoke_type);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001199
1200 // Update .bss entry in oat file if any.
1201 if (called != nullptr && called_method.dex_file->GetOatDexFile() != nullptr) {
1202 const MethodBssMapping* mapping =
1203 called_method.dex_file->GetOatDexFile()->GetMethodBssMapping();
1204 if (mapping != nullptr) {
1205 auto pp = std::partition_point(
1206 mapping->begin(),
1207 mapping->end(),
1208 [called_method](const MethodBssMappingEntry& entry) {
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001209 return entry.method_index < called_method.index;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001210 });
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001211 if (pp != mapping->end() && pp->CoversIndex(called_method.index)) {
1212 size_t bss_offset = pp->GetBssOffset(called_method.index,
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001213 static_cast<size_t>(kRuntimePointerSize));
1214 DCHECK_ALIGNED(bss_offset, static_cast<size_t>(kRuntimePointerSize));
1215 const OatFile* oat_file = called_method.dex_file->GetOatDexFile()->GetOatFile();
1216 ArtMethod** method_entry = reinterpret_cast<ArtMethod**>(const_cast<uint8_t*>(
1217 oat_file->BssBegin() + bss_offset));
1218 DCHECK_GE(method_entry, oat_file->GetBssMethods().data());
1219 DCHECK_LT(method_entry,
1220 oat_file->GetBssMethods().data() + oat_file->GetBssMethods().size());
1221 *method_entry = called;
1222 }
1223 }
1224 }
Ian Rogers848871b2013-08-05 10:56:33 -07001225 }
Ian Rogerse0a02da2014-12-02 14:10:53 -08001226 const void* code = nullptr;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001227 if (LIKELY(!self->IsExceptionPending())) {
Ian Rogers848871b2013-08-05 10:56:33 -07001228 // Incompatible class change should have been handled in resolve method.
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001229 CHECK(!called->CheckIncompatibleClassChange(invoke_type))
David Sehr709b0702016-10-13 09:12:37 -07001230 << called->PrettyMethod() << " " << invoke_type;
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001231 if (virtual_or_interface || invoke_type == kSuper) {
1232 // Refine called method based on receiver for kVirtual/kInterface, and
1233 // caller for kSuper.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001234 ArtMethod* orig_called = called;
Mathieu Chartier55871bf2014-02-27 10:24:50 -08001235 if (invoke_type == kVirtual) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001236 CHECK(receiver != nullptr) << invoke_type;
Andreas Gampe542451c2016-07-26 09:02:02 -07001237 called = receiver->GetClass()->FindVirtualMethodForVirtual(called, kRuntimePointerSize);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001238 } else if (invoke_type == kInterface) {
1239 CHECK(receiver != nullptr) << invoke_type;
Andreas Gampe542451c2016-07-26 09:02:02 -07001240 called = receiver->GetClass()->FindVirtualMethodForInterface(called, kRuntimePointerSize);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001241 } else {
1242 DCHECK_EQ(invoke_type, kSuper);
1243 CHECK(caller != nullptr) << invoke_type;
Nicolas Geoffray393fdb82016-04-25 14:58:06 +01001244 StackHandleScope<2> hs(self);
1245 Handle<mirror::DexCache> dex_cache(
1246 hs.NewHandle(caller->GetDeclaringClass()->GetDexCache()));
1247 Handle<mirror::ClassLoader> class_loader(
1248 hs.NewHandle(caller->GetDeclaringClass()->GetClassLoader()));
Alex Lightfedd91d2016-01-07 14:49:16 -08001249 // TODO Maybe put this into a mirror::Class function.
Vladimir Markoba118822017-06-12 15:41:56 +01001250 ObjPtr<mirror::Class> ref_class = linker->LookupResolvedType(
1251 *dex_cache->GetDexFile(),
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001252 dex_cache->GetDexFile()->GetMethodId(called_method.index).class_idx_,
Vladimir Markoba118822017-06-12 15:41:56 +01001253 dex_cache.Get(),
1254 class_loader.Get());
Alex Lightfedd91d2016-01-07 14:49:16 -08001255 if (ref_class->IsInterface()) {
Andreas Gampe542451c2016-07-26 09:02:02 -07001256 called = ref_class->FindVirtualMethodForInterfaceSuper(called, kRuntimePointerSize);
Alex Lightfedd91d2016-01-07 14:49:16 -08001257 } else {
1258 called = caller->GetDeclaringClass()->GetSuperClass()->GetVTableEntry(
Andreas Gampe542451c2016-07-26 09:02:02 -07001259 called->GetMethodIndex(), kRuntimePointerSize);
Alex Lightfedd91d2016-01-07 14:49:16 -08001260 }
Mathieu Chartier55871bf2014-02-27 10:24:50 -08001261 }
Mingyao Yangf4867782014-05-05 11:55:02 -07001262
David Sehr709b0702016-10-13 09:12:37 -07001263 CHECK(called != nullptr) << orig_called->PrettyMethod() << " "
1264 << mirror::Object::PrettyTypeOf(receiver) << " "
Mingyao Yangf4867782014-05-05 11:55:02 -07001265 << invoke_type << " " << orig_called->GetVtableIndex();
Ian Rogers83883d72013-10-21 21:07:24 -07001266 }
Daniel Mihalyieb076692014-08-22 17:33:31 +02001267
Ian Rogers848871b2013-08-05 10:56:33 -07001268 // Ensure that the called method's class is initialized.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001269 StackHandleScope<1> hs(soa.Self());
1270 Handle<mirror::Class> called_class(hs.NewHandle(called->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -07001271 linker->EnsureInitialized(soa.Self(), called_class, true, true);
Ian Rogers848871b2013-08-05 10:56:33 -07001272 if (LIKELY(called_class->IsInitialized())) {
Daniel Mihalyieb076692014-08-22 17:33:31 +02001273 if (UNLIKELY(Dbg::IsForcedInterpreterNeededForResolution(self, called))) {
1274 // If we are single-stepping or the called method is deoptimized (by a
1275 // breakpoint, for example), then we have to execute the called method
1276 // with the interpreter.
1277 code = GetQuickToInterpreterBridge();
1278 } else if (UNLIKELY(Dbg::IsForcedInstrumentationNeededForResolution(self, caller))) {
1279 // If the caller is deoptimized (by a breakpoint, for example), we have to
1280 // continue its execution with interpreter when returning from the called
1281 // method. Because we do not want to execute the called method with the
1282 // interpreter, we wrap its execution into the instrumentation stubs.
1283 // When the called method returns, it will execute the instrumentation
1284 // exit hook that will determine the need of the interpreter with a call
1285 // to Dbg::IsForcedInterpreterNeededForUpcall and deoptimize the stack if
1286 // it is needed.
1287 code = GetQuickInstrumentationEntryPoint();
1288 } else {
1289 code = called->GetEntryPointFromQuickCompiledCode();
1290 }
Ian Rogers848871b2013-08-05 10:56:33 -07001291 } else if (called_class->IsInitializing()) {
Daniel Mihalyieb076692014-08-22 17:33:31 +02001292 if (UNLIKELY(Dbg::IsForcedInterpreterNeededForResolution(self, called))) {
1293 // If we are single-stepping or the called method is deoptimized (by a
1294 // breakpoint, for example), then we have to execute the called method
1295 // with the interpreter.
1296 code = GetQuickToInterpreterBridge();
1297 } else if (invoke_type == kStatic) {
Ian Rogers848871b2013-08-05 10:56:33 -07001298 // Class is still initializing, go to oat and grab code (trampoline must be left in place
1299 // until class is initialized to stop races between threads).
Ian Rogersef7d42f2014-01-06 12:55:46 -08001300 code = linker->GetQuickOatCodeFor(called);
Ian Rogers848871b2013-08-05 10:56:33 -07001301 } else {
1302 // No trampoline for non-static methods.
Ian Rogersef7d42f2014-01-06 12:55:46 -08001303 code = called->GetEntryPointFromQuickCompiledCode();
Ian Rogers848871b2013-08-05 10:56:33 -07001304 }
1305 } else {
1306 DCHECK(called_class->IsErroneous());
1307 }
1308 }
Ian Rogerse0a02da2014-12-02 14:10:53 -08001309 CHECK_EQ(code == nullptr, self->IsExceptionPending());
Mathieu Chartier07d447b2013-09-26 11:57:43 -07001310 // Fixup any locally saved objects may have moved during a GC.
1311 visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -07001312 // Place called method in callee-save frame to be placed as first argument to quick method.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001313 *sp = called;
1314
Ian Rogers848871b2013-08-05 10:56:33 -07001315 return code;
1316}
1317
Andreas Gampec147b002014-03-06 18:11:06 -08001318/*
1319 * This class uses a couple of observations to unite the different calling conventions through
1320 * a few constants.
1321 *
1322 * 1) Number of registers used for passing is normally even, so counting down has no penalty for
1323 * possible alignment.
1324 * 2) Known 64b architectures store 8B units on the stack, both for integral and floating point
1325 * types, so using uintptr_t is OK. Also means that we can use kRegistersNeededX to denote
1326 * when we have to split things
1327 * 3) The only soft-float, Arm, is 32b, so no widening needs to be taken into account for floats
1328 * and we can use Int handling directly.
1329 * 4) Only 64b architectures widen, and their stack is aligned 8B anyways, so no padding code
1330 * necessary when widening. Also, widening of Ints will take place implicitly, and the
1331 * extension should be compatible with Aarch64, which mandates copying the available bits
1332 * into LSB and leaving the rest unspecified.
1333 * 5) Aligning longs and doubles is necessary on arm only, and it's the same in registers and on
1334 * the stack.
1335 * 6) There is only little endian.
1336 *
1337 *
1338 * Actual work is supposed to be done in a delegate of the template type. The interface is as
1339 * follows:
1340 *
1341 * void PushGpr(uintptr_t): Add a value for the next GPR
1342 *
1343 * void PushFpr4(float): Add a value for the next FPR of size 32b. Is only called if we need
1344 * padding, that is, think the architecture is 32b and aligns 64b.
1345 *
1346 * void PushFpr8(uint64_t): Push a double. We _will_ call this on 32b, it's the callee's job to
1347 * split this if necessary. The current state will have aligned, if
1348 * necessary.
1349 *
1350 * void PushStack(uintptr_t): Push a value to the stack.
1351 *
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001352 * uintptr_t PushHandleScope(mirror::Object* ref): Add a reference to the HandleScope. This _will_ have nullptr,
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001353 * as this might be important for null initialization.
Andreas Gampec147b002014-03-06 18:11:06 -08001354 * Must return the jobject, that is, the reference to the
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001355 * entry in the HandleScope (nullptr if necessary).
Andreas Gampec147b002014-03-06 18:11:06 -08001356 *
1357 */
Andreas Gampec200a4a2014-06-16 18:39:09 -07001358template<class T> class BuildNativeCallFrameStateMachine {
Andreas Gampec147b002014-03-06 18:11:06 -08001359 public:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001360#if defined(__arm__)
1361 // TODO: These are all dummy values!
Andreas Gampec147b002014-03-06 18:11:06 -08001362 static constexpr bool kNativeSoftFloatAbi = true;
1363 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs, r0-r3
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001364 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
1365
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001366 static constexpr size_t kRegistersNeededForLong = 2;
1367 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -08001368 static constexpr bool kMultiRegistersAligned = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001369 static constexpr bool kMultiFPRegistersWidened = false;
1370 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001371 static constexpr bool kAlignLongOnStack = true;
1372 static constexpr bool kAlignDoubleOnStack = true;
Stuart Monteithb95a5342014-03-12 13:32:32 +00001373#elif defined(__aarch64__)
1374 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
1375 static constexpr size_t kNumNativeGprArgs = 8; // 6 arguments passed in GPRs.
1376 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
1377
1378 static constexpr size_t kRegistersNeededForLong = 1;
1379 static constexpr size_t kRegistersNeededForDouble = 1;
1380 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001381 static constexpr bool kMultiFPRegistersWidened = false;
1382 static constexpr bool kMultiGPRegistersWidened = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +00001383 static constexpr bool kAlignLongOnStack = false;
1384 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001385#elif defined(__mips__) && !defined(__LP64__)
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001386 static constexpr bool kNativeSoftFloatAbi = true; // This is a hard float ABI.
Douglas Leung735b8552014-10-31 12:21:40 -07001387 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs.
1388 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001389
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001390 static constexpr size_t kRegistersNeededForLong = 2;
1391 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -08001392 static constexpr bool kMultiRegistersAligned = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001393 static constexpr bool kMultiFPRegistersWidened = true;
1394 static constexpr bool kMultiGPRegistersWidened = false;
Douglas Leung735b8552014-10-31 12:21:40 -07001395 static constexpr bool kAlignLongOnStack = true;
1396 static constexpr bool kAlignDoubleOnStack = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001397#elif defined(__mips__) && defined(__LP64__)
1398 // Let the code prepare GPRs only and we will load the FPRs with same data.
1399 static constexpr bool kNativeSoftFloatAbi = true;
1400 static constexpr size_t kNumNativeGprArgs = 8;
1401 static constexpr size_t kNumNativeFprArgs = 0;
1402
1403 static constexpr size_t kRegistersNeededForLong = 1;
1404 static constexpr size_t kRegistersNeededForDouble = 1;
1405 static constexpr bool kMultiRegistersAligned = false;
1406 static constexpr bool kMultiFPRegistersWidened = false;
1407 static constexpr bool kMultiGPRegistersWidened = true;
1408 static constexpr bool kAlignLongOnStack = false;
1409 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001410#elif defined(__i386__)
1411 // TODO: Check these!
Andreas Gampec147b002014-03-06 18:11:06 -08001412 static constexpr bool kNativeSoftFloatAbi = false; // Not using int registers for fp
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001413 static constexpr size_t kNumNativeGprArgs = 0; // 6 arguments passed in GPRs.
1414 static constexpr size_t kNumNativeFprArgs = 0; // 8 arguments passed in FPRs.
1415
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001416 static constexpr size_t kRegistersNeededForLong = 2;
1417 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001418 static constexpr bool kMultiRegistersAligned = false; // x86 not using regs, anyways
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001419 static constexpr bool kMultiFPRegistersWidened = false;
1420 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001421 static constexpr bool kAlignLongOnStack = false;
1422 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001423#elif defined(__x86_64__)
1424 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
1425 static constexpr size_t kNumNativeGprArgs = 6; // 6 arguments passed in GPRs.
1426 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
1427
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001428 static constexpr size_t kRegistersNeededForLong = 1;
1429 static constexpr size_t kRegistersNeededForDouble = 1;
Andreas Gampec147b002014-03-06 18:11:06 -08001430 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001431 static constexpr bool kMultiFPRegistersWidened = false;
1432 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001433 static constexpr bool kAlignLongOnStack = false;
1434 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001435#else
1436#error "Unsupported architecture"
1437#endif
1438
Andreas Gampec147b002014-03-06 18:11:06 -08001439 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001440 explicit BuildNativeCallFrameStateMachine(T* delegate)
1441 : gpr_index_(kNumNativeGprArgs),
1442 fpr_index_(kNumNativeFprArgs),
1443 stack_entries_(0),
1444 delegate_(delegate) {
Andreas Gampec147b002014-03-06 18:11:06 -08001445 // For register alignment, we want to assume that counters (gpr_index_, fpr_index_) are even iff
1446 // the next register is even; counting down is just to make the compiler happy...
Andreas Gampe575e78c2014-11-03 23:41:03 -08001447 static_assert(kNumNativeGprArgs % 2 == 0U, "Number of native GPR arguments not even");
1448 static_assert(kNumNativeFprArgs % 2 == 0U, "Number of native FPR arguments not even");
Andreas Gampec147b002014-03-06 18:11:06 -08001449 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001450
Andreas Gampec200a4a2014-06-16 18:39:09 -07001451 virtual ~BuildNativeCallFrameStateMachine() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001452
Ian Rogers1428dce2014-10-21 15:02:15 -07001453 bool HavePointerGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001454 return gpr_index_ > 0;
1455 }
1456
Andreas Gampec200a4a2014-06-16 18:39:09 -07001457 void AdvancePointer(const void* val) {
Andreas Gampec147b002014-03-06 18:11:06 -08001458 if (HavePointerGpr()) {
1459 gpr_index_--;
1460 PushGpr(reinterpret_cast<uintptr_t>(val));
1461 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001462 stack_entries_++; // TODO: have a field for pointer length as multiple of 32b
Andreas Gampec147b002014-03-06 18:11:06 -08001463 PushStack(reinterpret_cast<uintptr_t>(val));
1464 gpr_index_ = 0;
1465 }
1466 }
1467
Ian Rogers1428dce2014-10-21 15:02:15 -07001468 bool HaveHandleScopeGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001469 return gpr_index_ > 0;
1470 }
1471
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001472 void AdvanceHandleScope(mirror::Object* ptr) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001473 uintptr_t handle = PushHandle(ptr);
1474 if (HaveHandleScopeGpr()) {
Andreas Gampec147b002014-03-06 18:11:06 -08001475 gpr_index_--;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001476 PushGpr(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001477 } else {
1478 stack_entries_++;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001479 PushStack(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001480 gpr_index_ = 0;
1481 }
1482 }
1483
Ian Rogers1428dce2014-10-21 15:02:15 -07001484 bool HaveIntGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001485 return gpr_index_ > 0;
1486 }
1487
1488 void AdvanceInt(uint32_t val) {
1489 if (HaveIntGpr()) {
1490 gpr_index_--;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001491 if (kMultiGPRegistersWidened) {
1492 DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001493 PushGpr(static_cast<int64_t>(bit_cast<int32_t, uint32_t>(val)));
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001494 } else {
1495 PushGpr(val);
1496 }
Andreas Gampec147b002014-03-06 18:11:06 -08001497 } else {
1498 stack_entries_++;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001499 if (kMultiGPRegistersWidened) {
1500 DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001501 PushStack(static_cast<int64_t>(bit_cast<int32_t, uint32_t>(val)));
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001502 } else {
1503 PushStack(val);
1504 }
Andreas Gampec147b002014-03-06 18:11:06 -08001505 gpr_index_ = 0;
1506 }
1507 }
1508
Ian Rogers1428dce2014-10-21 15:02:15 -07001509 bool HaveLongGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001510 return gpr_index_ >= kRegistersNeededForLong + (LongGprNeedsPadding() ? 1 : 0);
1511 }
1512
Ian Rogers1428dce2014-10-21 15:02:15 -07001513 bool LongGprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001514 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1515 kAlignLongOnStack && // and when it needs alignment
1516 (gpr_index_ & 1) == 1; // counter is odd, see constructor
1517 }
1518
Ian Rogers1428dce2014-10-21 15:02:15 -07001519 bool LongStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001520 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1521 kAlignLongOnStack && // and when it needs 8B alignment
1522 (stack_entries_ & 1) == 1; // counter is odd
1523 }
1524
1525 void AdvanceLong(uint64_t val) {
1526 if (HaveLongGpr()) {
1527 if (LongGprNeedsPadding()) {
1528 PushGpr(0);
1529 gpr_index_--;
1530 }
1531 if (kRegistersNeededForLong == 1) {
1532 PushGpr(static_cast<uintptr_t>(val));
1533 } else {
1534 PushGpr(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1535 PushGpr(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1536 }
1537 gpr_index_ -= kRegistersNeededForLong;
1538 } else {
1539 if (LongStackNeedsPadding()) {
1540 PushStack(0);
1541 stack_entries_++;
1542 }
1543 if (kRegistersNeededForLong == 1) {
1544 PushStack(static_cast<uintptr_t>(val));
1545 stack_entries_++;
1546 } else {
1547 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1548 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1549 stack_entries_ += 2;
1550 }
1551 gpr_index_ = 0;
1552 }
1553 }
1554
Ian Rogers1428dce2014-10-21 15:02:15 -07001555 bool HaveFloatFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001556 return fpr_index_ > 0;
1557 }
1558
Andreas Gampec147b002014-03-06 18:11:06 -08001559 void AdvanceFloat(float val) {
1560 if (kNativeSoftFloatAbi) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001561 AdvanceInt(bit_cast<uint32_t, float>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001562 } else {
1563 if (HaveFloatFpr()) {
1564 fpr_index_--;
1565 if (kRegistersNeededForDouble == 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001566 if (kMultiFPRegistersWidened) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001567 PushFpr8(bit_cast<uint64_t, double>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001568 } else {
1569 // No widening, just use the bits.
Roland Levillainda4d79b2015-03-24 14:36:11 +00001570 PushFpr8(static_cast<uint64_t>(bit_cast<uint32_t, float>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001571 }
1572 } else {
1573 PushFpr4(val);
1574 }
1575 } else {
1576 stack_entries_++;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001577 if (kRegistersNeededForDouble == 1 && kMultiFPRegistersWidened) {
Andreas Gampec147b002014-03-06 18:11:06 -08001578 // Need to widen before storing: Note the "double" in the template instantiation.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001579 // Note: We need to jump through those hoops to make the compiler happy.
1580 DCHECK_EQ(sizeof(uintptr_t), sizeof(uint64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001581 PushStack(static_cast<uintptr_t>(bit_cast<uint64_t, double>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001582 } else {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001583 PushStack(static_cast<uintptr_t>(bit_cast<uint32_t, float>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001584 }
1585 fpr_index_ = 0;
1586 }
1587 }
1588 }
1589
Ian Rogers1428dce2014-10-21 15:02:15 -07001590 bool HaveDoubleFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001591 return fpr_index_ >= kRegistersNeededForDouble + (DoubleFprNeedsPadding() ? 1 : 0);
1592 }
1593
Ian Rogers1428dce2014-10-21 15:02:15 -07001594 bool DoubleFprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001595 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1596 kAlignDoubleOnStack && // and when it needs alignment
1597 (fpr_index_ & 1) == 1; // counter is odd, see constructor
1598 }
1599
Ian Rogers1428dce2014-10-21 15:02:15 -07001600 bool DoubleStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001601 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1602 kAlignDoubleOnStack && // and when it needs 8B alignment
1603 (stack_entries_ & 1) == 1; // counter is odd
1604 }
1605
1606 void AdvanceDouble(uint64_t val) {
1607 if (kNativeSoftFloatAbi) {
1608 AdvanceLong(val);
1609 } else {
1610 if (HaveDoubleFpr()) {
1611 if (DoubleFprNeedsPadding()) {
1612 PushFpr4(0);
1613 fpr_index_--;
1614 }
1615 PushFpr8(val);
1616 fpr_index_ -= kRegistersNeededForDouble;
1617 } else {
1618 if (DoubleStackNeedsPadding()) {
1619 PushStack(0);
1620 stack_entries_++;
1621 }
1622 if (kRegistersNeededForDouble == 1) {
1623 PushStack(static_cast<uintptr_t>(val));
1624 stack_entries_++;
1625 } else {
1626 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1627 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1628 stack_entries_ += 2;
1629 }
1630 fpr_index_ = 0;
1631 }
1632 }
1633 }
1634
Ian Rogers1428dce2014-10-21 15:02:15 -07001635 uint32_t GetStackEntries() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001636 return stack_entries_;
1637 }
1638
Ian Rogers1428dce2014-10-21 15:02:15 -07001639 uint32_t GetNumberOfUsedGprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001640 return kNumNativeGprArgs - gpr_index_;
1641 }
1642
Ian Rogers1428dce2014-10-21 15:02:15 -07001643 uint32_t GetNumberOfUsedFprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001644 return kNumNativeFprArgs - fpr_index_;
1645 }
1646
1647 private:
1648 void PushGpr(uintptr_t val) {
1649 delegate_->PushGpr(val);
1650 }
1651 void PushFpr4(float val) {
1652 delegate_->PushFpr4(val);
1653 }
1654 void PushFpr8(uint64_t val) {
1655 delegate_->PushFpr8(val);
1656 }
1657 void PushStack(uintptr_t val) {
1658 delegate_->PushStack(val);
1659 }
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001660 uintptr_t PushHandle(mirror::Object* ref) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001661 return delegate_->PushHandle(ref);
Andreas Gampec147b002014-03-06 18:11:06 -08001662 }
1663
1664 uint32_t gpr_index_; // Number of free GPRs
1665 uint32_t fpr_index_; // Number of free FPRs
1666 uint32_t stack_entries_; // Stack entries are in multiples of 32b, as floats are usually not
1667 // extended
Ian Rogers1428dce2014-10-21 15:02:15 -07001668 T* const delegate_; // What Push implementation gets called
Andreas Gampec147b002014-03-06 18:11:06 -08001669};
1670
Andreas Gampec200a4a2014-06-16 18:39:09 -07001671// Computes the sizes of register stacks and call stack area. Handling of references can be extended
1672// in subclasses.
1673//
1674// To handle native pointers, use "L" in the shorty for an object reference, which simulates
1675// them with handles.
1676class ComputeNativeCallFrameSize {
Andreas Gampec147b002014-03-06 18:11:06 -08001677 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001678 ComputeNativeCallFrameSize() : num_stack_entries_(0) {}
1679
1680 virtual ~ComputeNativeCallFrameSize() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001681
Ian Rogers1428dce2014-10-21 15:02:15 -07001682 uint32_t GetStackSize() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001683 return num_stack_entries_ * sizeof(uintptr_t);
1684 }
1685
Ian Rogers1428dce2014-10-21 15:02:15 -07001686 uint8_t* LayoutCallStack(uint8_t* sp8) const {
Andreas Gampec147b002014-03-06 18:11:06 -08001687 sp8 -= GetStackSize();
Andreas Gampe779f8c92014-06-09 18:29:38 -07001688 // Align by kStackAlignment.
1689 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
Andreas Gampec200a4a2014-06-16 18:39:09 -07001690 return sp8;
Andreas Gampec147b002014-03-06 18:11:06 -08001691 }
1692
Ian Rogers1428dce2014-10-21 15:02:15 -07001693 uint8_t* LayoutCallRegisterStacks(uint8_t* sp8, uintptr_t** start_gpr, uint32_t** start_fpr)
1694 const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001695 // Assumption is OK right now, as we have soft-float arm
1696 size_t fregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeFprArgs;
1697 sp8 -= fregs * sizeof(uintptr_t);
1698 *start_fpr = reinterpret_cast<uint32_t*>(sp8);
1699 size_t iregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeGprArgs;
1700 sp8 -= iregs * sizeof(uintptr_t);
1701 *start_gpr = reinterpret_cast<uintptr_t*>(sp8);
1702 return sp8;
1703 }
Andreas Gampec147b002014-03-06 18:11:06 -08001704
Andreas Gampec200a4a2014-06-16 18:39:09 -07001705 uint8_t* LayoutNativeCall(uint8_t* sp8, uintptr_t** start_stack, uintptr_t** start_gpr,
Ian Rogers1428dce2014-10-21 15:02:15 -07001706 uint32_t** start_fpr) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001707 // Native call stack.
1708 sp8 = LayoutCallStack(sp8);
1709 *start_stack = reinterpret_cast<uintptr_t*>(sp8);
Andreas Gampec147b002014-03-06 18:11:06 -08001710
Andreas Gampec200a4a2014-06-16 18:39:09 -07001711 // Put fprs and gprs below.
1712 sp8 = LayoutCallRegisterStacks(sp8, start_gpr, start_fpr);
Andreas Gampec147b002014-03-06 18:11:06 -08001713
Andreas Gampec200a4a2014-06-16 18:39:09 -07001714 // Return the new bottom.
1715 return sp8;
1716 }
1717
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001718 virtual void WalkHeader(
1719 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm ATTRIBUTE_UNUSED)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001720 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001721 }
Andreas Gampec200a4a2014-06-16 18:39:09 -07001722
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001723 void Walk(const char* shorty, uint32_t shorty_len) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001724 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize> sm(this);
1725
1726 WalkHeader(&sm);
Andreas Gampec147b002014-03-06 18:11:06 -08001727
1728 for (uint32_t i = 1; i < shorty_len; ++i) {
1729 Primitive::Type cur_type_ = Primitive::GetType(shorty[i]);
1730 switch (cur_type_) {
1731 case Primitive::kPrimNot:
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001732 // TODO: fix abuse of mirror types.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001733 sm.AdvanceHandleScope(
1734 reinterpret_cast<mirror::Object*>(0x12345678));
Andreas Gampec147b002014-03-06 18:11:06 -08001735 break;
1736
1737 case Primitive::kPrimBoolean:
1738 case Primitive::kPrimByte:
1739 case Primitive::kPrimChar:
1740 case Primitive::kPrimShort:
1741 case Primitive::kPrimInt:
1742 sm.AdvanceInt(0);
1743 break;
1744 case Primitive::kPrimFloat:
1745 sm.AdvanceFloat(0);
1746 break;
1747 case Primitive::kPrimDouble:
1748 sm.AdvanceDouble(0);
1749 break;
1750 case Primitive::kPrimLong:
1751 sm.AdvanceLong(0);
1752 break;
1753 default:
1754 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001755 UNREACHABLE();
Andreas Gampec147b002014-03-06 18:11:06 -08001756 }
1757 }
1758
Ian Rogers1428dce2014-10-21 15:02:15 -07001759 num_stack_entries_ = sm.GetStackEntries();
Andreas Gampec147b002014-03-06 18:11:06 -08001760 }
1761
1762 void PushGpr(uintptr_t /* val */) {
1763 // not optimizing registers, yet
1764 }
1765
1766 void PushFpr4(float /* val */) {
1767 // not optimizing registers, yet
1768 }
1769
1770 void PushFpr8(uint64_t /* val */) {
1771 // not optimizing registers, yet
1772 }
1773
1774 void PushStack(uintptr_t /* val */) {
1775 // counting is already done in the superclass
1776 }
1777
Andreas Gampec200a4a2014-06-16 18:39:09 -07001778 virtual uintptr_t PushHandle(mirror::Object* /* ptr */) {
Andreas Gampec147b002014-03-06 18:11:06 -08001779 return reinterpret_cast<uintptr_t>(nullptr);
1780 }
1781
Andreas Gampec200a4a2014-06-16 18:39:09 -07001782 protected:
Andreas Gampec147b002014-03-06 18:11:06 -08001783 uint32_t num_stack_entries_;
1784};
1785
Andreas Gampec200a4a2014-06-16 18:39:09 -07001786class ComputeGenericJniFrameSize FINAL : public ComputeNativeCallFrameSize {
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001787 public:
Igor Murashkin06a04e02016-09-13 15:57:37 -07001788 explicit ComputeGenericJniFrameSize(bool critical_native)
1789 : num_handle_scope_references_(0), critical_native_(critical_native) {}
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001790
Andreas Gampec200a4a2014-06-16 18:39:09 -07001791 // Lays out the callee-save frame. Assumes that the incorrect frame corresponding to RefsAndArgs
1792 // is at *m = sp. Will update to point to the bottom of the save frame.
1793 //
1794 // Note: assumes ComputeAll() has been run before.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001795 void LayoutCalleeSaveFrame(Thread* self, ArtMethod*** m, void* sp, HandleScope** handle_scope)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001796 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001797 ArtMethod* method = **m;
1798
Andreas Gampe542451c2016-07-26 09:02:02 -07001799 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001800
Andreas Gampec200a4a2014-06-16 18:39:09 -07001801 uint8_t* sp8 = reinterpret_cast<uint8_t*>(sp);
1802
1803 // First, fix up the layout of the callee-save frame.
1804 // We have to squeeze in the HandleScope, and relocate the method pointer.
1805
1806 // "Free" the slot for the method.
Ian Rogers13735952014-10-08 12:43:28 -07001807 sp8 += sizeof(void*); // In the callee-save frame we use a full pointer.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001808
1809 // Under the callee saves put handle scope and new method stack reference.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001810 size_t handle_scope_size = HandleScope::SizeOf(num_handle_scope_references_);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001811 size_t scope_and_method = handle_scope_size + sizeof(ArtMethod*);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001812
1813 sp8 -= scope_and_method;
1814 // Align by kStackAlignment.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001815 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
Andreas Gampec200a4a2014-06-16 18:39:09 -07001816
Mathieu Chartiere401d142015-04-22 13:56:20 -07001817 uint8_t* sp8_table = sp8 + sizeof(ArtMethod*);
Ian Rogers59c07062014-10-10 13:03:39 -07001818 *handle_scope = HandleScope::Create(sp8_table, self->GetTopHandleScope(),
1819 num_handle_scope_references_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001820
1821 // Add a slot for the method pointer, and fill it. Fix the pointer-pointer given to us.
1822 uint8_t* method_pointer = sp8;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001823 auto** new_method_ref = reinterpret_cast<ArtMethod**>(method_pointer);
1824 *new_method_ref = method;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001825 *m = new_method_ref;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001826 }
1827
Andreas Gampec200a4a2014-06-16 18:39:09 -07001828 // Adds space for the cookie. Note: may leave stack unaligned.
Ian Rogers1428dce2014-10-21 15:02:15 -07001829 void LayoutCookie(uint8_t** sp) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001830 // Reference cookie and padding
1831 *sp -= 8;
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001832 }
1833
Andreas Gampec200a4a2014-06-16 18:39:09 -07001834 // Re-layout the callee-save frame (insert a handle-scope). Then add space for the cookie.
1835 // Returns the new bottom. Note: this may be unaligned.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001836 uint8_t* LayoutJNISaveFrame(Thread* self, ArtMethod*** m, void* sp, HandleScope** handle_scope)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001837 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001838 // First, fix up the layout of the callee-save frame.
1839 // We have to squeeze in the HandleScope, and relocate the method pointer.
Ian Rogers59c07062014-10-10 13:03:39 -07001840 LayoutCalleeSaveFrame(self, m, sp, handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001841
1842 // The bottom of the callee-save frame is now where the method is, *m.
1843 uint8_t* sp8 = reinterpret_cast<uint8_t*>(*m);
1844
1845 // Add space for cookie.
1846 LayoutCookie(&sp8);
1847
1848 return sp8;
1849 }
1850
1851 // WARNING: After this, *sp won't be pointing to the method anymore!
Mathieu Chartiere401d142015-04-22 13:56:20 -07001852 uint8_t* ComputeLayout(Thread* self, ArtMethod*** m, const char* shorty, uint32_t shorty_len,
1853 HandleScope** handle_scope, uintptr_t** start_stack, uintptr_t** start_gpr,
1854 uint32_t** start_fpr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001855 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001856 Walk(shorty, shorty_len);
1857
1858 // JNI part.
Ian Rogers59c07062014-10-10 13:03:39 -07001859 uint8_t* sp8 = LayoutJNISaveFrame(self, m, reinterpret_cast<void*>(*m), handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001860
1861 sp8 = LayoutNativeCall(sp8, start_stack, start_gpr, start_fpr);
1862
1863 // Return the new bottom.
1864 return sp8;
1865 }
1866
1867 uintptr_t PushHandle(mirror::Object* /* ptr */) OVERRIDE;
1868
1869 // Add JNIEnv* and jobj/jclass before the shorty-derived elements.
1870 void WalkHeader(BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) OVERRIDE
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001871 REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001872
1873 private:
1874 uint32_t num_handle_scope_references_;
Igor Murashkin06a04e02016-09-13 15:57:37 -07001875 const bool critical_native_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001876};
1877
1878uintptr_t ComputeGenericJniFrameSize::PushHandle(mirror::Object* /* ptr */) {
1879 num_handle_scope_references_++;
1880 return reinterpret_cast<uintptr_t>(nullptr);
1881}
1882
1883void ComputeGenericJniFrameSize::WalkHeader(
1884 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) {
Igor Murashkin06a04e02016-09-13 15:57:37 -07001885 // First 2 parameters are always excluded for @CriticalNative.
1886 if (UNLIKELY(critical_native_)) {
1887 return;
1888 }
1889
Andreas Gampec200a4a2014-06-16 18:39:09 -07001890 // JNIEnv
1891 sm->AdvancePointer(nullptr);
1892
1893 // Class object or this as first argument
1894 sm->AdvanceHandleScope(reinterpret_cast<mirror::Object*>(0x12345678));
1895}
1896
1897// Class to push values to three separate regions. Used to fill the native call part. Adheres to
1898// the template requirements of BuildGenericJniFrameStateMachine.
1899class FillNativeCall {
1900 public:
1901 FillNativeCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) :
1902 cur_gpr_reg_(gpr_regs), cur_fpr_reg_(fpr_regs), cur_stack_arg_(stack_args) {}
1903
1904 virtual ~FillNativeCall() {}
1905
1906 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) {
1907 cur_gpr_reg_ = gpr_regs;
1908 cur_fpr_reg_ = fpr_regs;
1909 cur_stack_arg_ = stack_args;
Andreas Gampec147b002014-03-06 18:11:06 -08001910 }
1911
1912 void PushGpr(uintptr_t val) {
1913 *cur_gpr_reg_ = val;
1914 cur_gpr_reg_++;
1915 }
1916
1917 void PushFpr4(float val) {
1918 *cur_fpr_reg_ = val;
1919 cur_fpr_reg_++;
1920 }
1921
1922 void PushFpr8(uint64_t val) {
1923 uint64_t* tmp = reinterpret_cast<uint64_t*>(cur_fpr_reg_);
1924 *tmp = val;
1925 cur_fpr_reg_ += 2;
1926 }
1927
1928 void PushStack(uintptr_t val) {
1929 *cur_stack_arg_ = val;
1930 cur_stack_arg_++;
1931 }
1932
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001933 virtual uintptr_t PushHandle(mirror::Object*) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001934 LOG(FATAL) << "(Non-JNI) Native call does not use handles.";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001935 UNREACHABLE();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001936 }
1937
1938 private:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001939 uintptr_t* cur_gpr_reg_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001940 uint32_t* cur_fpr_reg_;
1941 uintptr_t* cur_stack_arg_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001942};
Andreas Gampec147b002014-03-06 18:11:06 -08001943
Andreas Gampec200a4a2014-06-16 18:39:09 -07001944// Visits arguments on the stack placing them into a region lower down the stack for the benefit
1945// of transitioning into native code.
1946class BuildGenericJniFrameVisitor FINAL : public QuickArgumentVisitor {
1947 public:
Igor Murashkin06a04e02016-09-13 15:57:37 -07001948 BuildGenericJniFrameVisitor(Thread* self,
1949 bool is_static,
1950 bool critical_native,
1951 const char* shorty,
1952 uint32_t shorty_len,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001953 ArtMethod*** sp)
Andreas Gampec200a4a2014-06-16 18:39:09 -07001954 : QuickArgumentVisitor(*sp, is_static, shorty, shorty_len),
Igor Murashkin06a04e02016-09-13 15:57:37 -07001955 jni_call_(nullptr, nullptr, nullptr, nullptr, critical_native),
1956 sm_(&jni_call_) {
1957 ComputeGenericJniFrameSize fsc(critical_native);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001958 uintptr_t* start_gpr_reg;
1959 uint32_t* start_fpr_reg;
1960 uintptr_t* start_stack_arg;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001961 bottom_of_used_area_ = fsc.ComputeLayout(self, sp, shorty, shorty_len,
Ian Rogers59c07062014-10-10 13:03:39 -07001962 &handle_scope_,
1963 &start_stack_arg,
Andreas Gampec200a4a2014-06-16 18:39:09 -07001964 &start_gpr_reg, &start_fpr_reg);
1965
Andreas Gampec200a4a2014-06-16 18:39:09 -07001966 jni_call_.Reset(start_gpr_reg, start_fpr_reg, start_stack_arg, handle_scope_);
1967
Igor Murashkin06a04e02016-09-13 15:57:37 -07001968 // First 2 parameters are always excluded for CriticalNative methods.
1969 if (LIKELY(!critical_native)) {
1970 // jni environment is always first argument
1971 sm_.AdvancePointer(self->GetJniEnv());
Andreas Gampec200a4a2014-06-16 18:39:09 -07001972
Igor Murashkin06a04e02016-09-13 15:57:37 -07001973 if (is_static) {
1974 sm_.AdvanceHandleScope((**sp)->GetDeclaringClass());
1975 } // else "this" reference is already handled by QuickArgumentVisitor.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001976 }
1977 }
1978
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001979 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001980
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001981 void FinalizeHandleScope(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001982
Vladimir Markof39745e2016-01-26 12:16:55 +00001983 StackReference<mirror::Object>* GetFirstHandleScopeEntry() {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001984 return handle_scope_->GetHandle(0).GetReference();
1985 }
1986
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001987 jobject GetFirstHandleScopeJObject() const REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001988 return handle_scope_->GetHandle(0).ToJObject();
1989 }
1990
Ian Rogers1428dce2014-10-21 15:02:15 -07001991 void* GetBottomOfUsedArea() const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001992 return bottom_of_used_area_;
1993 }
1994
1995 private:
1996 // A class to fill a JNI call. Adds reference/handle-scope management to FillNativeCall.
1997 class FillJniCall FINAL : public FillNativeCall {
1998 public:
1999 FillJniCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args,
Igor Murashkin06a04e02016-09-13 15:57:37 -07002000 HandleScope* handle_scope, bool critical_native)
2001 : FillNativeCall(gpr_regs, fpr_regs, stack_args),
2002 handle_scope_(handle_scope),
2003 cur_entry_(0),
2004 critical_native_(critical_native) {}
Andreas Gampec200a4a2014-06-16 18:39:09 -07002005
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002006 uintptr_t PushHandle(mirror::Object* ref) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002007
2008 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args, HandleScope* scope) {
2009 FillNativeCall::Reset(gpr_regs, fpr_regs, stack_args);
2010 handle_scope_ = scope;
2011 cur_entry_ = 0U;
2012 }
2013
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002014 void ResetRemainingScopeSlots() REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002015 // Initialize padding entries.
2016 size_t expected_slots = handle_scope_->NumberOfReferences();
2017 while (cur_entry_ < expected_slots) {
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07002018 handle_scope_->GetMutableHandle(cur_entry_++).Assign(nullptr);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002019 }
Igor Murashkin06a04e02016-09-13 15:57:37 -07002020
2021 if (!critical_native_) {
2022 // Non-critical natives have at least the self class (jclass) or this (jobject).
2023 DCHECK_NE(cur_entry_, 0U);
2024 }
Andreas Gampec200a4a2014-06-16 18:39:09 -07002025 }
2026
Mathieu Chartier1432a5b2016-10-04 15:41:42 -07002027 bool CriticalNative() const {
2028 return critical_native_;
2029 }
2030
Andreas Gampec200a4a2014-06-16 18:39:09 -07002031 private:
2032 HandleScope* handle_scope_;
2033 size_t cur_entry_;
Igor Murashkin06a04e02016-09-13 15:57:37 -07002034 const bool critical_native_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002035 };
2036
2037 HandleScope* handle_scope_;
2038 FillJniCall jni_call_;
2039 void* bottom_of_used_area_;
2040
2041 BuildNativeCallFrameStateMachine<FillJniCall> sm_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002042
2043 DISALLOW_COPY_AND_ASSIGN(BuildGenericJniFrameVisitor);
2044};
2045
Andreas Gampec200a4a2014-06-16 18:39:09 -07002046uintptr_t BuildGenericJniFrameVisitor::FillJniCall::PushHandle(mirror::Object* ref) {
2047 uintptr_t tmp;
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07002048 MutableHandle<mirror::Object> h = handle_scope_->GetMutableHandle(cur_entry_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002049 h.Assign(ref);
2050 tmp = reinterpret_cast<uintptr_t>(h.ToJObject());
2051 cur_entry_++;
2052 return tmp;
2053}
2054
Ian Rogers9758f792014-03-13 09:02:55 -07002055void BuildGenericJniFrameVisitor::Visit() {
2056 Primitive::Type type = GetParamPrimitiveType();
2057 switch (type) {
2058 case Primitive::kPrimLong: {
2059 jlong long_arg;
2060 if (IsSplitLongOrDouble()) {
2061 long_arg = ReadSplitLongParam();
2062 } else {
2063 long_arg = *reinterpret_cast<jlong*>(GetParamAddress());
2064 }
2065 sm_.AdvanceLong(long_arg);
2066 break;
2067 }
2068 case Primitive::kPrimDouble: {
2069 uint64_t double_arg;
2070 if (IsSplitLongOrDouble()) {
2071 // Read into union so that we don't case to a double.
2072 double_arg = ReadSplitLongParam();
2073 } else {
2074 double_arg = *reinterpret_cast<uint64_t*>(GetParamAddress());
2075 }
2076 sm_.AdvanceDouble(double_arg);
2077 break;
2078 }
2079 case Primitive::kPrimNot: {
2080 StackReference<mirror::Object>* stack_ref =
2081 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002082 sm_.AdvanceHandleScope(stack_ref->AsMirrorPtr());
Ian Rogers9758f792014-03-13 09:02:55 -07002083 break;
2084 }
2085 case Primitive::kPrimFloat:
2086 sm_.AdvanceFloat(*reinterpret_cast<float*>(GetParamAddress()));
2087 break;
2088 case Primitive::kPrimBoolean: // Fall-through.
2089 case Primitive::kPrimByte: // Fall-through.
2090 case Primitive::kPrimChar: // Fall-through.
2091 case Primitive::kPrimShort: // Fall-through.
2092 case Primitive::kPrimInt: // Fall-through.
2093 sm_.AdvanceInt(*reinterpret_cast<jint*>(GetParamAddress()));
2094 break;
2095 case Primitive::kPrimVoid:
2096 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -07002097 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -07002098 }
2099}
2100
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002101void BuildGenericJniFrameVisitor::FinalizeHandleScope(Thread* self) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002102 // Clear out rest of the scope.
2103 jni_call_.ResetRemainingScopeSlots();
Mathieu Chartier1432a5b2016-10-04 15:41:42 -07002104 if (!jni_call_.CriticalNative()) {
2105 // Install HandleScope.
2106 self->PushHandleScope(handle_scope_);
2107 }
Ian Rogers9758f792014-03-13 09:02:55 -07002108}
2109
Ian Rogers04c31d22014-07-07 21:44:06 -07002110#if defined(__arm__) || defined(__aarch64__)
Alex Lightd78ddec2017-04-18 15:20:38 -07002111extern "C" const void* artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07002112#else
Alex Lightd78ddec2017-04-18 15:20:38 -07002113extern "C" const void* artFindNativeMethod(Thread* self);
Ian Rogers04c31d22014-07-07 21:44:06 -07002114#endif
Andreas Gampe90546832014-03-12 18:07:19 -07002115
Igor Murashkin06a04e02016-09-13 15:57:37 -07002116static uint64_t artQuickGenericJniEndJNIRef(Thread* self,
2117 uint32_t cookie,
2118 bool fast_native ATTRIBUTE_UNUSED,
2119 jobject l,
2120 jobject lock) {
2121 // TODO: add entrypoints for @FastNative returning objects.
Andreas Gampead615172014-04-04 16:20:13 -07002122 if (lock != nullptr) {
2123 return reinterpret_cast<uint64_t>(JniMethodEndWithReferenceSynchronized(l, cookie, lock, self));
2124 } else {
2125 return reinterpret_cast<uint64_t>(JniMethodEndWithReference(l, cookie, self));
2126 }
2127}
2128
Igor Murashkin06a04e02016-09-13 15:57:37 -07002129static void artQuickGenericJniEndJNINonRef(Thread* self,
2130 uint32_t cookie,
2131 bool fast_native,
2132 jobject lock) {
Andreas Gampead615172014-04-04 16:20:13 -07002133 if (lock != nullptr) {
2134 JniMethodEndSynchronized(cookie, lock, self);
Igor Murashkin06a04e02016-09-13 15:57:37 -07002135 // Ignore "fast_native" here because synchronized functions aren't very fast.
Andreas Gampead615172014-04-04 16:20:13 -07002136 } else {
Igor Murashkin06a04e02016-09-13 15:57:37 -07002137 if (UNLIKELY(fast_native)) {
2138 JniMethodFastEnd(cookie, self);
2139 } else {
2140 JniMethodEnd(cookie, self);
2141 }
Andreas Gampead615172014-04-04 16:20:13 -07002142 }
2143}
2144
Andreas Gampec147b002014-03-06 18:11:06 -08002145/*
2146 * Initializes an alloca region assumed to be directly below sp for a native call:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002147 * Create a HandleScope and call stack and fill a mini stack with values to be pushed to registers.
Andreas Gampec147b002014-03-06 18:11:06 -08002148 * The final element on the stack is a pointer to the native code.
2149 *
Andreas Gampe36fea8d2014-03-10 13:37:40 -07002150 * On entry, the stack has a standard callee-save frame above sp, and an alloca below it.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002151 * We need to fix this, as the handle scope needs to go into the callee-save frame.
Andreas Gampe36fea8d2014-03-10 13:37:40 -07002152 *
Andreas Gampec147b002014-03-06 18:11:06 -08002153 * The return of this function denotes:
2154 * 1) How many bytes of the alloca can be released, if the value is non-negative.
2155 * 2) An error, if the value is negative.
2156 */
Mathieu Chartiere401d142015-04-22 13:56:20 -07002157extern "C" TwoWordReturn artQuickGenericJniTrampoline(Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002158 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002159 ArtMethod* called = *sp;
David Sehr709b0702016-10-13 09:12:37 -07002160 DCHECK(called->IsNative()) << called->PrettyMethod(true);
Roland Levillain0d2323e2017-06-26 18:14:39 +01002161 // Fix up a callee-save frame at the bottom of the stack (at `*sp`,
2162 // above the alloca region) while we check for optimization
2163 // annotations, thus allowing stack walking until the completion of
2164 // the JNI frame creation.
2165 //
2166 // Note however that the Generic JNI trampoline does not expect
2167 // exception being thrown at that stage.
2168 *sp = Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs);
2169 self->SetTopOfStack(sp);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002170 uint32_t shorty_len = 0;
2171 const char* shorty = called->GetShorty(&shorty_len);
Roland Levillain35e42f02017-06-26 18:14:39 +01002172 // Optimization annotations lookup does not try to resolve classes,
2173 // as this may throw an exception, which is not supported by the
2174 // Generic JNI trampoline at this stage; instead, method's
2175 // annotations' classes are looked up in the bootstrap class
2176 // loader's resolved types (which won't trigger an exception).
Igor Murashkin06a04e02016-09-13 15:57:37 -07002177 bool critical_native = called->IsAnnotatedWithCriticalNative();
Roland Levillain0d2323e2017-06-26 18:14:39 +01002178 // ArtMethod::IsAnnotatedWithCriticalNative should not throw
2179 // an exception; clear it if it happened anyway.
2180 // TODO: Revisit this code path and turn this into a CHECK(!self->IsExceptionPending()).
2181 if (self->IsExceptionPending()) {
2182 self->ClearException();
2183 }
Igor Murashkin06a04e02016-09-13 15:57:37 -07002184 bool fast_native = called->IsAnnotatedWithFastNative();
Roland Levillain0d2323e2017-06-26 18:14:39 +01002185 // ArtMethod::IsAnnotatedWithFastNative should not throw
2186 // an exception; clear it if it happened anyway.
2187 // TODO: Revisit this code path and turn this into a CHECK(!self->IsExceptionPending()).
2188 if (self->IsExceptionPending()) {
2189 self->ClearException();
2190 }
Igor Murashkin06a04e02016-09-13 15:57:37 -07002191 bool normal_native = !critical_native && !fast_native;
Roland Levillain0d2323e2017-06-26 18:14:39 +01002192 // Restore the initial ArtMethod pointer at `*sp`.
2193 *sp = called;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002194
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002195 // Run the visitor and update sp.
Igor Murashkin06a04e02016-09-13 15:57:37 -07002196 BuildGenericJniFrameVisitor visitor(self,
2197 called->IsStatic(),
2198 critical_native,
2199 shorty,
2200 shorty_len,
2201 &sp);
Mathieu Chartierbe08cf52016-09-13 13:41:24 -07002202 {
2203 ScopedAssertNoThreadSuspension sants(__FUNCTION__);
2204 visitor.VisitArguments();
2205 // FinalizeHandleScope pushes the handle scope on the thread.
2206 visitor.FinalizeHandleScope(self);
2207 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002208
Andreas Gampec200a4a2014-06-16 18:39:09 -07002209 // Fix up managed-stack things in Thread.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002210 self->SetTopOfStack(sp);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002211
Ian Rogerse0dcd462014-03-08 15:21:04 -08002212 self->VerifyStack();
2213
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002214 uint32_t cookie;
Igor Murashkin06a04e02016-09-13 15:57:37 -07002215 uint32_t* sp32;
2216 // Skip calling JniMethodStart for @CriticalNative.
2217 if (LIKELY(!critical_native)) {
2218 // Start JNI, save the cookie.
2219 if (called->IsSynchronized()) {
2220 DCHECK(normal_native) << " @FastNative and synchronize is not supported";
2221 cookie = JniMethodStartSynchronized(visitor.GetFirstHandleScopeJObject(), self);
2222 if (self->IsExceptionPending()) {
2223 self->PopHandleScope();
2224 // A negative value denotes an error.
2225 return GetTwoWordFailureValue();
2226 }
2227 } else {
2228 if (fast_native) {
2229 cookie = JniMethodFastStart(self);
2230 } else {
2231 DCHECK(normal_native);
2232 cookie = JniMethodStart(self);
2233 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002234 }
Igor Murashkin06a04e02016-09-13 15:57:37 -07002235 sp32 = reinterpret_cast<uint32_t*>(sp);
2236 *(sp32 - 1) = cookie;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002237 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002238
Andreas Gampe90546832014-03-12 18:07:19 -07002239 // Retrieve the stored native code.
Alex Lightd78ddec2017-04-18 15:20:38 -07002240 void const* nativeCode = called->GetEntryPointFromJni();
Andreas Gampe90546832014-03-12 18:07:19 -07002241
Andreas Gampe9a6a99a2014-03-14 07:52:20 -07002242 // There are two cases for the content of nativeCode:
2243 // 1) Pointer to the native function.
2244 // 2) Pointer to the trampoline for native code binding.
2245 // In the second case, we need to execute the binding and continue with the actual native function
2246 // pointer.
Andreas Gampe90546832014-03-12 18:07:19 -07002247 DCHECK(nativeCode != nullptr);
2248 if (nativeCode == GetJniDlsymLookupStub()) {
Ian Rogers04c31d22014-07-07 21:44:06 -07002249#if defined(__arm__) || defined(__aarch64__)
Andreas Gampe90546832014-03-12 18:07:19 -07002250 nativeCode = artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07002251#else
2252 nativeCode = artFindNativeMethod(self);
2253#endif
Andreas Gampe90546832014-03-12 18:07:19 -07002254
2255 if (nativeCode == nullptr) {
2256 DCHECK(self->IsExceptionPending()); // There should be an exception pending now.
Andreas Gampead615172014-04-04 16:20:13 -07002257
Igor Murashkin06a04e02016-09-13 15:57:37 -07002258 // @CriticalNative calls do not need to call back into JniMethodEnd.
2259 if (LIKELY(!critical_native)) {
2260 // End JNI, as the assembly will move to deliver the exception.
2261 jobject lock = called->IsSynchronized() ? visitor.GetFirstHandleScopeJObject() : nullptr;
2262 if (shorty[0] == 'L') {
2263 artQuickGenericJniEndJNIRef(self, cookie, fast_native, nullptr, lock);
2264 } else {
2265 artQuickGenericJniEndJNINonRef(self, cookie, fast_native, lock);
2266 }
Andreas Gampead615172014-04-04 16:20:13 -07002267 }
2268
Andreas Gampec200a4a2014-06-16 18:39:09 -07002269 return GetTwoWordFailureValue();
Andreas Gampe90546832014-03-12 18:07:19 -07002270 }
2271 // Note that the native code pointer will be automatically set by artFindNativeMethod().
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002272 }
2273
Alexey Frunze1b8464d2016-11-12 17:22:05 -08002274#if defined(__mips__) && !defined(__LP64__)
2275 // On MIPS32 if the first two arguments are floating-point, we need to know their types
2276 // so that art_quick_generic_jni_trampoline can correctly extract them from the stack
2277 // and load into floating-point registers.
2278 // Possible arrangements of first two floating-point arguments on the stack (32-bit FPU
2279 // view):
2280 // (1)
2281 // | DOUBLE | DOUBLE | other args, if any
2282 // | F12 | F13 | F14 | F15 |
2283 // | SP+0 | SP+4 | SP+8 | SP+12 | SP+16
2284 // (2)
2285 // | DOUBLE | FLOAT | (PAD) | other args, if any
2286 // | F12 | F13 | F14 | |
2287 // | SP+0 | SP+4 | SP+8 | SP+12 | SP+16
2288 // (3)
2289 // | FLOAT | (PAD) | DOUBLE | other args, if any
2290 // | F12 | | F14 | F15 |
2291 // | SP+0 | SP+4 | SP+8 | SP+12 | SP+16
2292 // (4)
2293 // | FLOAT | FLOAT | other args, if any
2294 // | F12 | F14 |
2295 // | SP+0 | SP+4 | SP+8
2296 // As you can see, only the last case (4) is special. In all others we can just
2297 // load F12/F13 and F14/F15 in the same manner.
2298 // Set bit 0 of the native code address to 1 in this case (valid code addresses
2299 // are always a multiple of 4 on MIPS32, so we have 2 spare bits available).
2300 if (nativeCode != nullptr &&
2301 shorty != nullptr &&
2302 shorty_len >= 3 &&
2303 shorty[1] == 'F' &&
2304 shorty[2] == 'F') {
2305 nativeCode = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(nativeCode) | 1);
2306 }
2307#endif
2308
Andreas Gampec200a4a2014-06-16 18:39:09 -07002309 // Return native code addr(lo) and bottom of alloca address(hi).
2310 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(visitor.GetBottomOfUsedArea()),
2311 reinterpret_cast<uintptr_t>(nativeCode));
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002312}
2313
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002314// Defined in quick_jni_entrypoints.cc.
2315extern uint64_t GenericJniMethodEnd(Thread* self, uint32_t saved_local_ref_cookie,
2316 jvalue result, uint64_t result_f, ArtMethod* called,
2317 HandleScope* handle_scope);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002318/*
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002319 * Is called after the native JNI code. Responsible for cleanup (handle scope, saved state) and
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002320 * unlocking.
2321 */
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002322extern "C" uint64_t artQuickGenericJniEndTrampoline(Thread* self,
2323 jvalue result,
2324 uint64_t result_f) {
2325 // We're here just back from a native call. We don't have the shared mutator lock at this point
2326 // yet until we call GoToRunnable() later in GenericJniMethodEnd(). Accessing objects or doing
2327 // anything that requires a mutator lock before that would cause problems as GC may have the
2328 // exclusive mutator lock and may be moving objects, etc.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002329 ArtMethod** sp = self->GetManagedStack()->GetTopQuickFrame();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002330 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002331 ArtMethod* called = *sp;
Ian Rogerse0dcd462014-03-08 15:21:04 -08002332 uint32_t cookie = *(sp32 - 1);
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002333 HandleScope* table = reinterpret_cast<HandleScope*>(reinterpret_cast<uint8_t*>(sp) + sizeof(*sp));
2334 return GenericJniMethodEnd(self, cookie, result, result_f, called, table);
Andreas Gampe2da88232014-02-27 12:26:20 -08002335}
2336
Andreas Gamped58342c2014-06-05 14:18:08 -07002337// We use TwoWordReturn to optimize scalar returns. We use the hi value for code, and the lo value
2338// for the method pointer.
Andreas Gampe51f76352014-05-21 08:28:48 -07002339//
Andreas Gamped58342c2014-06-05 14:18:08 -07002340// It is valid to use this, as at the usage points here (returns from C functions) we are assuming
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002341// to hold the mutator lock (see REQUIRES_SHARED(Locks::mutator_lock_) annotations).
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002342
Vladimir Markof79aa7f2017-07-04 16:58:55 +01002343template <InvokeType type, bool access_check>
Mathieu Chartieref41db72016-10-25 15:08:01 -07002344static TwoWordReturn artInvokeCommon(uint32_t method_idx,
2345 ObjPtr<mirror::Object> this_object,
2346 Thread* self,
Mathieu Chartiere401d142015-04-22 13:56:20 -07002347 ArtMethod** sp) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002348 ScopedQuickEntrypointChecks sqec(self);
Andreas Gampe8228cdf2017-05-30 15:03:54 -07002349 DCHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002350 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
Vladimir Markof79aa7f2017-07-04 16:58:55 +01002351 ArtMethod* method = FindMethodFast<type, access_check>(method_idx, this_object, caller_method);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002352 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002353 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()->GetDexFile();
2354 uint32_t shorty_len;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002355 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(method_idx), &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002356 {
2357 // Remember the args in case a GC happens in FindMethodFromCode.
2358 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
2359 RememberForGcArgumentVisitor visitor(sp, type == kStatic, shorty, shorty_len, &soa);
2360 visitor.VisitArguments();
Mathieu Chartieref41db72016-10-25 15:08:01 -07002361 method = FindMethodFromCode<type, access_check>(method_idx,
2362 &this_object,
2363 caller_method,
Mathieu Chartier0cd81352014-05-22 16:48:55 -07002364 self);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002365 visitor.FixupReferences();
2366 }
2367
Ian Rogerse0a02da2014-12-02 14:10:53 -08002368 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002369 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07002370 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002371 }
2372 }
2373 DCHECK(!self->IsExceptionPending());
2374 const void* code = method->GetEntryPointFromQuickCompiledCode();
2375
2376 // When we return, the caller will branch to this address, so it had better not be 0!
David Sehr709b0702016-10-13 09:12:37 -07002377 DCHECK(code != nullptr) << "Code was null in method: " << method->PrettyMethod()
Andreas Gampec200a4a2014-06-16 18:39:09 -07002378 << " location: "
2379 << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07002380
Andreas Gamped58342c2014-06-05 14:18:08 -07002381 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
2382 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002383}
2384
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01002385// Explicit artInvokeCommon template function declarations to please analysis tool.
2386#define EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(type, access_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002387 template REQUIRES_SHARED(Locks::mutator_lock_) \
Mathieu Chartiere401d142015-04-22 13:56:20 -07002388 TwoWordReturn artInvokeCommon<type, access_check>( \
Mathieu Chartieref41db72016-10-25 15:08:01 -07002389 uint32_t method_idx, ObjPtr<mirror::Object> his_object, Thread* self, ArtMethod** sp)
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01002390
2391EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, false);
2392EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, true);
2393EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, false);
2394EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, true);
2395EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, false);
2396EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, true);
2397EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, false);
2398EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, true);
2399EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, false);
2400EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, true);
2401#undef EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL
2402
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002403// See comments in runtime_support_asm.S
Andreas Gampec200a4a2014-06-16 18:39:09 -07002404extern "C" TwoWordReturn artInvokeInterfaceTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002405 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002406 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002407 return artInvokeCommon<kInterface, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002408}
2409
Andreas Gampec200a4a2014-06-16 18:39:09 -07002410extern "C" TwoWordReturn artInvokeDirectTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002411 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002412 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002413 return artInvokeCommon<kDirect, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002414}
2415
Andreas Gampec200a4a2014-06-16 18:39:09 -07002416extern "C" TwoWordReturn artInvokeStaticTrampolineWithAccessCheck(
Mathieu Chartieref41db72016-10-25 15:08:01 -07002417 uint32_t method_idx,
2418 mirror::Object* this_object ATTRIBUTE_UNUSED,
2419 Thread* self,
2420 ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
2421 // For static, this_object is not required and may be random garbage. Don't pass it down so that
2422 // it doesn't cause ObjPtr alignment failure check.
2423 return artInvokeCommon<kStatic, true>(method_idx, nullptr, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002424}
2425
Andreas Gampec200a4a2014-06-16 18:39:09 -07002426extern "C" TwoWordReturn artInvokeSuperTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002427 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002428 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002429 return artInvokeCommon<kSuper, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002430}
2431
Andreas Gampec200a4a2014-06-16 18:39:09 -07002432extern "C" TwoWordReturn artInvokeVirtualTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002433 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002434 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002435 return artInvokeCommon<kVirtual, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002436}
2437
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002438// Helper function for art_quick_imt_conflict_trampoline to look up the interface method.
2439extern "C" ArtMethod* artLookupResolvedMethod(uint32_t method_index, ArtMethod* referrer)
2440 REQUIRES_SHARED(Locks::mutator_lock_) {
2441 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
2442 DCHECK(!referrer->IsProxyMethod());
2443 ArtMethod* result = Runtime::Current()->GetClassLinker()->LookupResolvedMethod(
2444 method_index, referrer->GetDexCache(), referrer->GetClassLoader());
2445 DCHECK(result == nullptr ||
2446 result->GetDeclaringClass()->IsInterface() ||
2447 result->GetDeclaringClass() ==
2448 WellKnownClasses::ToClass(WellKnownClasses::java_lang_Object))
2449 << result->PrettyMethod();
2450 return result;
2451}
2452
Jeff Hao5667f562017-02-27 19:32:01 -08002453// Determine target of interface dispatch. The interface method and this object are known non-null.
2454// The interface method is the method returned by the dex cache in the conflict trampoline.
2455extern "C" TwoWordReturn artInvokeInterfaceTrampoline(ArtMethod* interface_method,
Mathieu Chartieref41db72016-10-25 15:08:01 -07002456 mirror::Object* raw_this_object,
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002457 Thread* self,
2458 ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002459 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002460 ScopedQuickEntrypointChecks sqec(self);
Vladimir Marko302f69c2017-07-25 15:27:15 +01002461 StackHandleScope<2> hs(self);
2462 Handle<mirror::Object> this_object = hs.NewHandle(raw_this_object);
2463 Handle<mirror::Class> cls = hs.NewHandle(this_object->GetClass());
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002464
Nicolas Geoffray5bf7bac2016-07-06 14:18:23 +00002465 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002466 ArtMethod* method = nullptr;
Andreas Gampe542451c2016-07-26 09:02:02 -07002467 ImTable* imt = cls->GetImt(kRuntimePointerSize);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002468
Vladimir Marko302f69c2017-07-25 15:27:15 +01002469 if (UNLIKELY(interface_method == nullptr)) {
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002470 // The interface method is unresolved, so resolve it in the dex file of the caller.
Jeff Hao5667f562017-02-27 19:32:01 -08002471 // Fetch the dex_method_idx of the target interface method from the caller.
2472 uint32_t dex_method_idx;
2473 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
2474 const DexFile::CodeItem* code_item = caller_method->GetCodeItem();
2475 DCHECK_LT(dex_pc, code_item->insns_size_in_code_units_);
2476 const Instruction* instr = Instruction::At(&code_item->insns_[dex_pc]);
2477 Instruction::Code instr_code = instr->Opcode();
2478 DCHECK(instr_code == Instruction::INVOKE_INTERFACE ||
2479 instr_code == Instruction::INVOKE_INTERFACE_RANGE)
2480 << "Unexpected call into interface trampoline: " << instr->DumpString(nullptr);
2481 if (instr_code == Instruction::INVOKE_INTERFACE) {
2482 dex_method_idx = instr->VRegB_35c();
2483 } else {
2484 DCHECK_EQ(instr_code, Instruction::INVOKE_INTERFACE_RANGE);
2485 dex_method_idx = instr->VRegB_3rc();
2486 }
2487
Vladimir Marko302f69c2017-07-25 15:27:15 +01002488 const DexFile& dex_file = caller_method->GetDeclaringClass()->GetDexFile();
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002489 uint32_t shorty_len;
Vladimir Marko302f69c2017-07-25 15:27:15 +01002490 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(dex_method_idx),
2491 &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002492 {
Vladimir Marko302f69c2017-07-25 15:27:15 +01002493 // Remember the args in case a GC happens in ClassLinker::ResolveMethod().
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002494 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
2495 RememberForGcArgumentVisitor visitor(sp, false, shorty, shorty_len, &soa);
2496 visitor.VisitArguments();
Vladimir Marko302f69c2017-07-25 15:27:15 +01002497 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
2498 interface_method = class_linker->ResolveMethod<ClassLinker::ResolveMode::kNoChecks>(
2499 self, dex_method_idx, caller_method, kInterface);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002500 visitor.FixupReferences();
2501 }
2502
Vladimir Marko302f69c2017-07-25 15:27:15 +01002503 if (UNLIKELY(interface_method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002504 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07002505 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002506 }
Vladimir Marko302f69c2017-07-25 15:27:15 +01002507 }
2508
2509 DCHECK(!interface_method->IsRuntimeMethod());
2510 // Look whether we have a match in the ImtConflictTable.
2511 uint32_t imt_index = ImTable::GetImtIndex(interface_method);
2512 ArtMethod* conflict_method = imt->Get(imt_index, kRuntimePointerSize);
2513 if (LIKELY(conflict_method->IsRuntimeMethod())) {
2514 ImtConflictTable* current_table = conflict_method->GetImtConflictTable(kRuntimePointerSize);
2515 DCHECK(current_table != nullptr);
2516 method = current_table->Lookup(interface_method, kRuntimePointerSize);
2517 } else {
2518 // It seems we aren't really a conflict method!
2519 if (kIsDebugBuild) {
2520 ArtMethod* m = cls->FindVirtualMethodForInterface(interface_method, kRuntimePointerSize);
2521 CHECK_EQ(conflict_method, m)
2522 << interface_method->PrettyMethod() << " / " << conflict_method->PrettyMethod() << " / "
2523 << " / " << ArtMethod::PrettyMethod(m) << " / " << cls->PrettyClass();
2524 }
2525 method = conflict_method;
2526 }
2527 if (method != nullptr) {
2528 return GetTwoWordSuccessValue(
2529 reinterpret_cast<uintptr_t>(method->GetEntryPointFromQuickCompiledCode()),
2530 reinterpret_cast<uintptr_t>(method));
2531 }
2532
2533 // No match, use the IfTable.
2534 method = cls->FindVirtualMethodForInterface(interface_method, kRuntimePointerSize);
2535 if (UNLIKELY(method == nullptr)) {
2536 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(
2537 interface_method, this_object.Get(), caller_method);
2538 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002539 }
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002540
2541 // We arrive here if we have found an implementation, and it is not in the ImtConflictTable.
2542 // We create a new table with the new pair { interface_method, method }.
Vladimir Marko302f69c2017-07-25 15:27:15 +01002543 DCHECK(conflict_method->IsRuntimeMethod());
2544 ArtMethod* new_conflict_method = Runtime::Current()->GetClassLinker()->AddMethodToConflictTable(
2545 cls.Get(),
2546 conflict_method,
2547 interface_method,
2548 method,
2549 /*force_new_conflict_method*/false);
2550 if (new_conflict_method != conflict_method) {
2551 // Update the IMT if we create a new conflict method. No fence needed here, as the
2552 // data is consistent.
2553 imt->Set(imt_index,
2554 new_conflict_method,
2555 kRuntimePointerSize);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002556 }
2557
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002558 const void* code = method->GetEntryPointFromQuickCompiledCode();
2559
2560 // When we return, the caller will branch to this address, so it had better not be 0!
David Sehr709b0702016-10-13 09:12:37 -07002561 DCHECK(code != nullptr) << "Code was null in method: " << method->PrettyMethod()
Andreas Gampec200a4a2014-06-16 18:39:09 -07002562 << " location: " << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07002563
Andreas Gamped58342c2014-06-05 14:18:08 -07002564 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
2565 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002566}
2567
Orion Hodsonac141392017-01-13 11:53:47 +00002568// Returns shorty type so the caller can determine how to put |result|
2569// into expected registers. The shorty type is static so the compiler
2570// could call different flavors of this code path depending on the
2571// shorty type though this would require different entry points for
2572// each type.
2573extern "C" uintptr_t artInvokePolymorphic(
2574 JValue* result,
2575 mirror::Object* raw_method_handle,
2576 Thread* self,
2577 ArtMethod** sp)
2578 REQUIRES_SHARED(Locks::mutator_lock_) {
2579 ScopedQuickEntrypointChecks sqec(self);
Andreas Gampe8228cdf2017-05-30 15:03:54 -07002580 DCHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs));
Orion Hodsonac141392017-01-13 11:53:47 +00002581
2582 // Start new JNI local reference state
2583 JNIEnvExt* env = self->GetJniEnv();
2584 ScopedObjectAccessUnchecked soa(env);
2585 ScopedJniEnvLocalRefState env_state(env);
2586 const char* old_cause = self->StartAssertNoThreadSuspension("Making stack arguments safe.");
2587
2588 // From the instruction, get the |callsite_shorty| and expose arguments on the stack to the GC.
2589 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
2590 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
2591 const DexFile::CodeItem* code = caller_method->GetCodeItem();
2592 const Instruction* inst = Instruction::At(&code->insns_[dex_pc]);
2593 DCHECK(inst->Opcode() == Instruction::INVOKE_POLYMORPHIC ||
2594 inst->Opcode() == Instruction::INVOKE_POLYMORPHIC_RANGE);
2595 const DexFile* dex_file = caller_method->GetDexFile();
2596 const uint32_t proto_idx = inst->VRegH();
2597 const char* shorty = dex_file->GetShorty(proto_idx);
2598 const size_t shorty_length = strlen(shorty);
2599 static const bool kMethodIsStatic = false; // invoke() and invokeExact() are not static.
2600 RememberForGcArgumentVisitor gc_visitor(sp, kMethodIsStatic, shorty, shorty_length, &soa);
Orion Hodsonfea84dd2017-01-16 13:52:20 +00002601 gc_visitor.VisitArguments();
Orion Hodsonac141392017-01-13 11:53:47 +00002602
2603 // Wrap raw_method_handle in a Handle for safety.
2604 StackHandleScope<5> hs(self);
Orion Hodsonc069a302017-01-18 09:23:12 +00002605 Handle<mirror::MethodHandle> method_handle(
2606 hs.NewHandle(ObjPtr<mirror::MethodHandle>::DownCast(MakeObjPtr(raw_method_handle))));
Orion Hodsonac141392017-01-13 11:53:47 +00002607 raw_method_handle = nullptr;
2608 self->EndAssertNoThreadSuspension(old_cause);
2609
2610 // Resolve method - it's either MethodHandle.invoke() or MethodHandle.invokeExact().
2611 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Vladimir Markoba118822017-06-12 15:41:56 +01002612 ArtMethod* resolved_method = linker->ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>(
2613 self, inst->VRegB(), caller_method, kVirtual);
Orion Hodsonac141392017-01-13 11:53:47 +00002614 DCHECK((resolved_method ==
2615 jni::DecodeArtMethod(WellKnownClasses::java_lang_invoke_MethodHandle_invokeExact)) ||
2616 (resolved_method ==
2617 jni::DecodeArtMethod(WellKnownClasses::java_lang_invoke_MethodHandle_invoke)));
2618 if (UNLIKELY(method_handle.IsNull())) {
2619 ThrowNullPointerExceptionForMethodAccess(resolved_method, InvokeType::kVirtual);
2620 return static_cast<uintptr_t>('V');
2621 }
2622
2623 Handle<mirror::Class> caller_class(hs.NewHandle(caller_method->GetDeclaringClass()));
2624 Handle<mirror::MethodType> method_type(hs.NewHandle(linker->ResolveMethodType(
2625 *dex_file, proto_idx,
2626 hs.NewHandle<mirror::DexCache>(caller_class->GetDexCache()),
2627 hs.NewHandle<mirror::ClassLoader>(caller_class->GetClassLoader()))));
2628 // This implies we couldn't resolve one or more types in this method handle.
2629 if (UNLIKELY(method_type.IsNull())) {
2630 CHECK(self->IsExceptionPending());
2631 return static_cast<uintptr_t>('V');
2632 }
2633
2634 DCHECK_EQ(ArtMethod::NumArgRegisters(shorty) + 1u, (uint32_t)inst->VRegA());
2635 DCHECK_EQ(resolved_method->IsStatic(), kMethodIsStatic);
2636
2637 // Fix references before constructing the shadow frame.
2638 gc_visitor.FixupReferences();
2639
2640 // Construct shadow frame placing arguments consecutively from |first_arg|.
2641 const bool is_range = (inst->Opcode() == Instruction::INVOKE_POLYMORPHIC_RANGE);
2642 const size_t num_vregs = is_range ? inst->VRegA_4rcc() : inst->VRegA_45cc();
2643 const size_t first_arg = 0;
2644 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
2645 CREATE_SHADOW_FRAME(num_vregs, /* link */ nullptr, resolved_method, dex_pc);
2646 ShadowFrame* shadow_frame = shadow_frame_unique_ptr.get();
2647 ScopedStackedShadowFramePusher
2648 frame_pusher(self, shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
2649 BuildQuickShadowFrameVisitor shadow_frame_builder(sp,
2650 kMethodIsStatic,
2651 shorty,
2652 strlen(shorty),
2653 shadow_frame,
2654 first_arg);
2655 shadow_frame_builder.VisitArguments();
2656
2657 // Push a transition back into managed code onto the linked list in thread.
2658 ManagedStack fragment;
2659 self->PushManagedStackFragment(&fragment);
2660
2661 // Call DoInvokePolymorphic with |is_range| = true, as shadow frame has argument registers in
2662 // consecutive order.
2663 uint32_t unused_args[Instruction::kMaxVarArgRegs] = {};
2664 uint32_t first_callee_arg = first_arg + 1;
Orion Hodsonc069a302017-01-18 09:23:12 +00002665 if (!DoInvokePolymorphic<true /* is_range */>(self,
2666 resolved_method,
2667 *shadow_frame,
2668 method_handle,
2669 method_type,
2670 unused_args,
2671 first_callee_arg,
2672 result)) {
Orion Hodsonac141392017-01-13 11:53:47 +00002673 DCHECK(self->IsExceptionPending());
2674 }
2675
2676 // Pop transition record.
2677 self->PopManagedStackFragment(fragment);
2678
2679 return static_cast<uintptr_t>(shorty[0]);
2680}
2681
Ian Rogers848871b2013-08-05 10:56:33 -07002682} // namespace art