blob: 90231e289e505d5e03144fd7489e02ec44b32714 [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 Gampe542451c2016-07-26 09:02:02 -070018#include "base/enums.h"
Ian Rogers848871b2013-08-05 10:56:33 -070019#include "callee_save_frame.h"
Dragos Sbirleabd136a22013-08-13 18:07:04 -070020#include "common_throws.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070021#include "debugger.h"
Ian Rogers848871b2013-08-05 10:56:33 -070022#include "dex_file-inl.h"
23#include "dex_instruction-inl.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070024#include "entrypoints/entrypoint_utils-inl.h"
Ian Rogers6f3dbba2014-10-14 17:41:57 -070025#include "entrypoints/runtime_asm_entrypoints.h"
Ian Rogers83883d72013-10-21 21:07:24 -070026#include "gc/accounting/card_table-inl.h"
Andreas Gampe75a7db62016-09-26 12:04:26 -070027#include "imt_conflict_table.h"
28#include "imtable-inl.h"
Ian Rogers848871b2013-08-05 10:56:33 -070029#include "interpreter/interpreter.h"
Nicolas Geoffray796d6302016-03-13 22:22:31 +000030#include "linear_alloc.h"
Orion Hodsonac141392017-01-13 11:53:47 +000031#include "method_handles.h"
Ian Rogerse0a02da2014-12-02 14:10:53 -080032#include "method_reference.h"
Ian Rogers848871b2013-08-05 10:56:33 -070033#include "mirror/class-inl.h"
Mathieu Chartier5f3ded42014-04-03 15:25:30 -070034#include "mirror/dex_cache-inl.h"
Mathieu Chartierfc58af42015-04-16 18:00:39 -070035#include "mirror/method.h"
Orion Hodsonac141392017-01-13 11:53:47 +000036#include "mirror/method_handle_impl.h"
Ian Rogers848871b2013-08-05 10:56:33 -070037#include "mirror/object-inl.h"
38#include "mirror/object_array-inl.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010039#include "oat_quick_method_header.h"
Andreas Gampe639bdd12015-06-03 11:22:45 -070040#include "quick_exception_handler.h"
Ian Rogers848871b2013-08-05 10:56:33 -070041#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070042#include "scoped_thread_state_change-inl.h"
Andreas Gampeb3025922015-09-01 14:45:00 -070043#include "stack.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070044#include "thread-inl.h"
Orion Hodsonac141392017-01-13 11:53:47 +000045#include "well_known_classes.h"
Ian Rogers848871b2013-08-05 10:56:33 -070046
47namespace art {
48
49// Visits the arguments as saved to the stack by a Runtime::kRefAndArgs callee save frame.
50class QuickArgumentVisitor {
Ian Rogers936b37f2014-02-14 00:52:24 -080051 // Number of bytes for each out register in the caller method's frame.
52 static constexpr size_t kBytesStackArgLocation = 4;
Alexei Zavjalov41c507a2014-05-15 16:02:46 +070053 // Frame size in bytes of a callee-save frame for RefsAndArgs.
54 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize =
Vladimir Markofd36f1f2016-08-03 18:49:58 +010055 GetCalleeSaveFrameSize(kRuntimeISA, Runtime::kSaveRefsAndArgs);
Ian Rogers848871b2013-08-05 10:56:33 -070056#if defined(__arm__)
57 // The callee save frame is pointed to by SP.
58 // | argN | |
59 // | ... | |
60 // | arg4 | |
61 // | arg3 spill | | Caller's frame
62 // | arg2 spill | |
63 // | arg1 spill | |
64 // | Method* | ---
65 // | LR |
Zheng Xu5667fdb2014-10-23 18:29:55 +080066 // | ... | 4x6 bytes callee saves
67 // | R3 |
68 // | R2 |
69 // | R1 |
70 // | S15 |
71 // | : |
72 // | S0 |
73 // | | 4x2 bytes padding
Ian Rogers848871b2013-08-05 10:56:33 -070074 // | Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -050075 static constexpr bool kSplitPairAcrossRegisterAndStack = kArm32QuickCodeUseSoftFloat;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +000076 static constexpr bool kAlignPairRegister = !kArm32QuickCodeUseSoftFloat;
Zheng Xu5667fdb2014-10-23 18:29:55 +080077 static constexpr bool kQuickSoftFloatAbi = kArm32QuickCodeUseSoftFloat;
78 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = !kArm32QuickCodeUseSoftFloat;
Goran Jakovljevicff734982015-08-24 12:58:55 +000079 static constexpr bool kQuickSkipOddFpRegisters = false;
Zheng Xu5667fdb2014-10-23 18:29:55 +080080 static constexpr size_t kNumQuickGprArgs = 3;
81 static constexpr size_t kNumQuickFprArgs = kArm32QuickCodeUseSoftFloat ? 0 : 16;
Andreas Gampe1a5c4062015-01-15 12:10:47 -080082 static constexpr bool kGprFprLockstep = false;
Zheng Xub551fdc2014-07-25 11:49:42 +080083 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset =
Vladimir Markofd36f1f2016-08-03 18:49:58 +010084 arm::ArmCalleeSaveFpr1Offset(Runtime::kSaveRefsAndArgs); // Offset of first FPR arg.
Zheng Xub551fdc2014-07-25 11:49:42 +080085 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset =
Vladimir Markofd36f1f2016-08-03 18:49:58 +010086 arm::ArmCalleeSaveGpr1Offset(Runtime::kSaveRefsAndArgs); // Offset of first GPR arg.
Zheng Xub551fdc2014-07-25 11:49:42 +080087 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset =
Vladimir Markofd36f1f2016-08-03 18:49:58 +010088 arm::ArmCalleeSaveLrOffset(Runtime::kSaveRefsAndArgs); // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -080089 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +000090 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -080091 }
Stuart Monteithb95a5342014-03-12 13:32:32 +000092#elif defined(__aarch64__)
93 // The callee save frame is pointed to by SP.
94 // | argN | |
95 // | ... | |
96 // | arg4 | |
97 // | arg3 spill | | Caller's frame
98 // | arg2 spill | |
99 // | arg1 spill | |
100 // | Method* | ---
101 // | LR |
Zheng Xub551fdc2014-07-25 11:49:42 +0800102 // | X29 |
Stuart Monteithb95a5342014-03-12 13:32:32 +0000103 // | : |
Serban Constantinescu9bd88b02015-04-22 16:24:46 +0100104 // | X20 |
Stuart Monteithb95a5342014-03-12 13:32:32 +0000105 // | X7 |
106 // | : |
107 // | X1 |
Zheng Xub551fdc2014-07-25 11:49:42 +0800108 // | D7 |
Stuart Monteithb95a5342014-03-12 13:32:32 +0000109 // | : |
110 // | D0 |
111 // | | padding
112 // | Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500113 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000114 static constexpr bool kAlignPairRegister = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000115 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800116 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000117 static constexpr bool kQuickSkipOddFpRegisters = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000118 static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs.
119 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800120 static constexpr bool kGprFprLockstep = false;
Zheng Xub551fdc2014-07-25 11:49:42 +0800121 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset =
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100122 arm64::Arm64CalleeSaveFpr1Offset(Runtime::kSaveRefsAndArgs); // Offset of first FPR arg.
Zheng Xub551fdc2014-07-25 11:49:42 +0800123 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset =
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100124 arm64::Arm64CalleeSaveGpr1Offset(Runtime::kSaveRefsAndArgs); // Offset of first GPR arg.
Zheng Xub551fdc2014-07-25 11:49:42 +0800125 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset =
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100126 arm64::Arm64CalleeSaveLrOffset(Runtime::kSaveRefsAndArgs); // Offset of return address.
Stuart Monteithb95a5342014-03-12 13:32:32 +0000127 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000128 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Stuart Monteithb95a5342014-03-12 13:32:32 +0000129 }
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800130#elif defined(__mips__) && !defined(__LP64__)
Ian Rogers848871b2013-08-05 10:56:33 -0700131 // The callee save frame is pointed to by SP.
132 // | argN | |
133 // | ... | |
134 // | arg4 | |
135 // | arg3 spill | | Caller's frame
136 // | arg2 spill | |
137 // | arg1 spill | |
138 // | Method* | ---
139 // | RA |
140 // | ... | callee saves
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800141 // | T1 | arg5
142 // | T0 | arg4
Ian Rogers848871b2013-08-05 10:56:33 -0700143 // | A3 | arg3
144 // | A2 | arg2
145 // | A1 | arg1
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800146 // | F19 |
147 // | F18 | f_arg5
148 // | F17 |
149 // | F16 | f_arg4
Goran Jakovljevicff734982015-08-24 12:58:55 +0000150 // | F15 |
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800151 // | F14 | f_arg3
Goran Jakovljevicff734982015-08-24 12:58:55 +0000152 // | F13 |
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800153 // | F12 | f_arg2
154 // | F11 |
155 // | F10 | f_arg1
156 // | F9 |
157 // | F8 | f_arg0
Goran Jakovljevicff734982015-08-24 12:58:55 +0000158 // | | padding
Ian Rogers848871b2013-08-05 10:56:33 -0700159 // | A0/Method* | <- sp
Goran Jakovljevicff734982015-08-24 12:58:55 +0000160 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
161 static constexpr bool kAlignPairRegister = true;
162 static constexpr bool kQuickSoftFloatAbi = false;
Zheng Xu5667fdb2014-10-23 18:29:55 +0800163 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000164 static constexpr bool kQuickSkipOddFpRegisters = true;
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800165 static constexpr size_t kNumQuickGprArgs = 5; // 5 arguments passed in GPRs.
166 static constexpr size_t kNumQuickFprArgs = 12; // 6 arguments passed in FPRs. Floats can be
167 // passed only in even numbered registers and each
168 // double occupies two registers.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800169 static constexpr bool kGprFprLockstep = false;
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800170 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 8; // Offset of first FPR arg.
171 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 56; // Offset of first GPR arg.
172 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 108; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800173 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000174 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800175 }
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800176#elif defined(__mips__) && defined(__LP64__)
177 // The callee save frame is pointed to by SP.
178 // | argN | |
179 // | ... | |
180 // | arg4 | |
181 // | arg3 spill | | Caller's frame
182 // | arg2 spill | |
183 // | arg1 spill | |
184 // | Method* | ---
185 // | RA |
186 // | ... | callee saves
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800187 // | A7 | arg7
188 // | A6 | arg6
189 // | A5 | arg5
190 // | A4 | arg4
191 // | A3 | arg3
192 // | A2 | arg2
193 // | A1 | arg1
Goran Jakovljevicff734982015-08-24 12:58:55 +0000194 // | F19 | f_arg7
195 // | F18 | f_arg6
196 // | F17 | f_arg5
197 // | F16 | f_arg4
198 // | F15 | f_arg3
199 // | F14 | f_arg2
200 // | F13 | f_arg1
201 // | F12 | f_arg0
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800202 // | | padding
203 // | A0/Method* | <- sp
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800204 // NOTE: for Mip64, when A0 is skipped, F12 is also skipped.
Douglas Leungd18e0832015-02-09 15:22:26 -0800205 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800206 static constexpr bool kAlignPairRegister = false;
207 static constexpr bool kQuickSoftFloatAbi = false;
208 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000209 static constexpr bool kQuickSkipOddFpRegisters = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800210 static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs.
211 static constexpr size_t kNumQuickFprArgs = 7; // 7 arguments passed in FPRs.
212 static constexpr bool kGprFprLockstep = true;
213
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800214 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 24; // Offset of first FPR arg (F13).
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800215 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 80; // Offset of first GPR arg (A1).
216 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 200; // Offset of return address.
217 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
218 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
219 }
Ian Rogers848871b2013-08-05 10:56:33 -0700220#elif defined(__i386__)
221 // The callee save frame is pointed to by SP.
222 // | argN | |
223 // | ... | |
224 // | arg4 | |
225 // | arg3 spill | | Caller's frame
226 // | arg2 spill | |
227 // | arg1 spill | |
228 // | Method* | ---
229 // | Return |
230 // | EBP,ESI,EDI | callee saves
231 // | EBX | arg3
232 // | EDX | arg2
233 // | ECX | arg1
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000234 // | XMM3 | float arg 4
235 // | XMM2 | float arg 3
236 // | XMM1 | float arg 2
237 // | XMM0 | float arg 1
Ian Rogers848871b2013-08-05 10:56:33 -0700238 // | EAX/Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500239 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000240 static constexpr bool kAlignPairRegister = false;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000241 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800242 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000243 static constexpr bool kQuickSkipOddFpRegisters = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800244 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs.
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000245 static constexpr size_t kNumQuickFprArgs = 4; // 4 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800246 static constexpr bool kGprFprLockstep = false;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000247 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 4; // Offset of first FPR arg.
248 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 4 + 4*8; // Offset of first GPR arg.
249 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 28 + 4*8; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800250 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000251 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800252 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800253#elif defined(__x86_64__)
Ian Rogers936b37f2014-02-14 00:52:24 -0800254 // The callee save frame is pointed to by SP.
255 // | argN | |
256 // | ... | |
257 // | reg. arg spills | | Caller's frame
258 // | Method* | ---
259 // | Return |
260 // | R15 | callee save
261 // | R14 | callee save
262 // | R13 | callee save
263 // | R12 | callee save
264 // | R9 | arg5
265 // | R8 | arg4
266 // | RSI/R6 | arg1
267 // | RBP/R5 | callee save
268 // | RBX/R3 | callee save
269 // | RDX/R2 | arg2
270 // | RCX/R1 | arg3
271 // | XMM7 | float arg 8
272 // | XMM6 | float arg 7
273 // | XMM5 | float arg 6
274 // | XMM4 | float arg 5
275 // | XMM3 | float arg 4
276 // | XMM2 | float arg 3
277 // | XMM1 | float arg 2
278 // | XMM0 | float arg 1
279 // | Padding |
280 // | RDI/Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500281 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000282 static constexpr bool kAlignPairRegister = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800283 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800284 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000285 static constexpr bool kQuickSkipOddFpRegisters = false;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700286 static constexpr size_t kNumQuickGprArgs = 5; // 5 arguments passed in GPRs.
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700287 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800288 static constexpr bool kGprFprLockstep = false;
Ian Rogers936b37f2014-02-14 00:52:24 -0800289 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 16; // Offset of first FPR arg.
Serguei Katkovc3801912014-07-08 17:21:53 +0700290 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 80 + 4*8; // Offset of first GPR arg.
291 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 168 + 4*8; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800292 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
293 switch (gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000294 case 0: return (4 * GetBytesPerGprSpillLocation(kRuntimeISA));
295 case 1: return (1 * GetBytesPerGprSpillLocation(kRuntimeISA));
296 case 2: return (0 * GetBytesPerGprSpillLocation(kRuntimeISA));
297 case 3: return (5 * GetBytesPerGprSpillLocation(kRuntimeISA));
298 case 4: return (6 * GetBytesPerGprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800299 default:
Andreas Gampec200a4a2014-06-16 18:39:09 -0700300 LOG(FATAL) << "Unexpected GPR index: " << gpr_index;
301 return 0;
Ian Rogers936b37f2014-02-14 00:52:24 -0800302 }
303 }
Ian Rogers848871b2013-08-05 10:56:33 -0700304#else
305#error "Unsupported architecture"
Ian Rogers848871b2013-08-05 10:56:33 -0700306#endif
307
Ian Rogers936b37f2014-02-14 00:52:24 -0800308 public:
Sebastien Hertza836bc92014-11-25 16:30:53 +0100309 // Special handling for proxy methods. Proxy methods are instance methods so the
310 // 'this' object is the 1st argument. They also have the same frame layout as the
311 // kRefAndArgs runtime method. Since 'this' is a reference, it is located in the
312 // 1st GPR.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700313 static mirror::Object* GetProxyThisObject(ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700314 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000315 CHECK((*sp)->IsProxyMethod());
Sebastien Hertza836bc92014-11-25 16:30:53 +0100316 CHECK_GT(kNumQuickGprArgs, 0u);
317 constexpr uint32_t kThisGprIndex = 0u; // 'this' is in the 1st GPR.
318 size_t this_arg_offset = kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset +
319 GprIndexToGprOffset(kThisGprIndex);
320 uint8_t* this_arg_address = reinterpret_cast<uint8_t*>(sp) + this_arg_offset;
321 return reinterpret_cast<StackReference<mirror::Object>*>(this_arg_address)->AsMirrorPtr();
322 }
323
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700324 static ArtMethod* GetCallingMethod(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700325 DCHECK((*sp)->IsCalleeSaveMethod());
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100326 return GetCalleeSaveMethodCaller(sp, Runtime::kSaveRefsAndArgs);
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +0100327 }
328
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700329 static ArtMethod* GetOuterMethod(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700330 DCHECK((*sp)->IsCalleeSaveMethod());
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100331 uint8_t* previous_sp =
332 reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700333 return *reinterpret_cast<ArtMethod**>(previous_sp);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100334 }
335
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700336 static uint32_t GetCallingDexPc(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700337 DCHECK((*sp)->IsCalleeSaveMethod());
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100338 const size_t callee_frame_size = GetCalleeSaveFrameSize(kRuntimeISA, Runtime::kSaveRefsAndArgs);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700339 ArtMethod** caller_sp = reinterpret_cast<ArtMethod**>(
340 reinterpret_cast<uintptr_t>(sp) + callee_frame_size);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100341 uintptr_t outer_pc = QuickArgumentVisitor::GetCallingPc(sp);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100342 const OatQuickMethodHeader* current_code = (*caller_sp)->GetOatQuickMethodHeader(outer_pc);
343 uintptr_t outer_pc_offset = current_code->NativeQuickPcOffset(outer_pc);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100344
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100345 if (current_code->IsOptimized()) {
346 CodeInfo code_info = current_code->GetOptimizedCodeInfo();
David Srbecky09ed0982016-02-12 21:58:43 +0000347 CodeInfoEncoding encoding = code_info.ExtractEncoding();
David Brazdilf677ebf2015-05-29 16:29:43 +0100348 StackMap stack_map = code_info.GetStackMapForNativePcOffset(outer_pc_offset, encoding);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100349 DCHECK(stack_map.IsValid());
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800350 if (stack_map.HasInlineInfo(encoding.stack_map.encoding)) {
David Brazdilf677ebf2015-05-29 16:29:43 +0100351 InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map, encoding);
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800352 return inline_info.GetDexPcAtDepth(encoding.inline_info.encoding,
353 inline_info.GetDepth(encoding.inline_info.encoding)-1);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100354 } else {
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800355 return stack_map.GetDexPc(encoding.stack_map.encoding);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100356 }
357 } else {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100358 return current_code->ToDexPc(*caller_sp, outer_pc);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100359 }
Ian Rogers848871b2013-08-05 10:56:33 -0700360 }
361
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800362 static bool GetInvokeType(ArtMethod** sp, InvokeType* invoke_type, uint32_t* dex_method_index)
363 REQUIRES_SHARED(Locks::mutator_lock_) {
364 DCHECK((*sp)->IsCalleeSaveMethod());
365 const size_t callee_frame_size = GetCalleeSaveFrameSize(kRuntimeISA, Runtime::kSaveRefsAndArgs);
366 ArtMethod** caller_sp = reinterpret_cast<ArtMethod**>(
367 reinterpret_cast<uintptr_t>(sp) + callee_frame_size);
368 uintptr_t outer_pc = QuickArgumentVisitor::GetCallingPc(sp);
369 const OatQuickMethodHeader* current_code = (*caller_sp)->GetOatQuickMethodHeader(outer_pc);
370 if (!current_code->IsOptimized()) {
371 return false;
372 }
373 uintptr_t outer_pc_offset = current_code->NativeQuickPcOffset(outer_pc);
374 CodeInfo code_info = current_code->GetOptimizedCodeInfo();
375 CodeInfoEncoding encoding = code_info.ExtractEncoding();
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700376 MethodInfo method_info = current_code->GetOptimizedMethodInfo();
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800377 InvokeInfo invoke(code_info.GetInvokeInfoForNativePcOffset(outer_pc_offset, encoding));
378 if (invoke.IsValid()) {
379 *invoke_type = static_cast<InvokeType>(invoke.GetInvokeType(encoding.invoke_info.encoding));
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700380 *dex_method_index = invoke.GetMethodIndex(encoding.invoke_info.encoding, method_info);
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800381 return true;
382 }
383 return false;
384 }
385
Ian Rogers936b37f2014-02-14 00:52:24 -0800386 // For the given quick ref and args quick frame, return the caller's PC.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700387 static uintptr_t GetCallingPc(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700388 DCHECK((*sp)->IsCalleeSaveMethod());
Ian Rogers13735952014-10-08 12:43:28 -0700389 uint8_t* lr = reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_LrOffset;
Ian Rogers848871b2013-08-05 10:56:33 -0700390 return *reinterpret_cast<uintptr_t*>(lr);
391 }
392
Mathieu Chartiere401d142015-04-22 13:56:20 -0700393 QuickArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700394 uint32_t shorty_len) REQUIRES_SHARED(Locks::mutator_lock_) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700395 is_static_(is_static), shorty_(shorty), shorty_len_(shorty_len),
Ian Rogers13735952014-10-08 12:43:28 -0700396 gpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset),
397 fpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset),
398 stack_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize
Mathieu Chartiere401d142015-04-22 13:56:20 -0700399 + sizeof(ArtMethod*)), // Skip ArtMethod*.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800400 gpr_index_(0), fpr_index_(0), fpr_double_index_(0), stack_index_(0),
401 cur_type_(Primitive::kPrimVoid), is_split_long_or_double_(false) {
Andreas Gampe575e78c2014-11-03 23:41:03 -0800402 static_assert(kQuickSoftFloatAbi == (kNumQuickFprArgs == 0),
403 "Number of Quick FPR arguments unexpected");
404 static_assert(!(kQuickSoftFloatAbi && kQuickDoubleRegAlignedFloatBackFilled),
405 "Double alignment unexpected");
Zheng Xu5667fdb2014-10-23 18:29:55 +0800406 // For register alignment, we want to assume that counters(fpr_double_index_) are even if the
407 // next register is even.
Andreas Gampe575e78c2014-11-03 23:41:03 -0800408 static_assert(!kQuickDoubleRegAlignedFloatBackFilled || kNumQuickFprArgs % 2 == 0,
409 "Number of Quick FPR arguments not even");
Andreas Gampe542451c2016-07-26 09:02:02 -0700410 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Zheng Xu5667fdb2014-10-23 18:29:55 +0800411 }
Ian Rogers848871b2013-08-05 10:56:33 -0700412
413 virtual ~QuickArgumentVisitor() {}
414
415 virtual void Visit() = 0;
416
Ian Rogers936b37f2014-02-14 00:52:24 -0800417 Primitive::Type GetParamPrimitiveType() const {
418 return cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700419 }
420
Ian Rogers13735952014-10-08 12:43:28 -0700421 uint8_t* GetParamAddress() const {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800422 if (!kQuickSoftFloatAbi) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800423 Primitive::Type type = GetParamPrimitiveType();
424 if (UNLIKELY((type == Primitive::kPrimDouble) || (type == Primitive::kPrimFloat))) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800425 if (type == Primitive::kPrimDouble && kQuickDoubleRegAlignedFloatBackFilled) {
426 if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) {
427 return fpr_args_ + (fpr_double_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
428 }
429 } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000430 return fpr_args_ + (fpr_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800431 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700432 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers936b37f2014-02-14 00:52:24 -0800433 }
434 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800435 if (gpr_index_ < kNumQuickGprArgs) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800436 return gpr_args_ + GprIndexToGprOffset(gpr_index_);
437 }
438 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700439 }
440
441 bool IsSplitLongOrDouble() const {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700442 if ((GetBytesPerGprSpillLocation(kRuntimeISA) == 4) ||
443 (GetBytesPerFprSpillLocation(kRuntimeISA) == 4)) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800444 return is_split_long_or_double_;
445 } else {
446 return false; // An optimization for when GPR and FPRs are 64bit.
447 }
Ian Rogers848871b2013-08-05 10:56:33 -0700448 }
449
Ian Rogers936b37f2014-02-14 00:52:24 -0800450 bool IsParamAReference() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700451 return GetParamPrimitiveType() == Primitive::kPrimNot;
452 }
453
Ian Rogers936b37f2014-02-14 00:52:24 -0800454 bool IsParamALongOrDouble() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700455 Primitive::Type type = GetParamPrimitiveType();
456 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
457 }
458
459 uint64_t ReadSplitLongParam() const {
Nicolas Geoffray425f2392015-01-08 14:52:29 +0000460 // The splitted long is always available through the stack.
461 return *reinterpret_cast<uint64_t*>(stack_args_
462 + stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700463 }
464
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800465 void IncGprIndex() {
466 gpr_index_++;
467 if (kGprFprLockstep) {
468 fpr_index_++;
469 }
470 }
471
472 void IncFprIndex() {
473 fpr_index_++;
474 if (kGprFprLockstep) {
475 gpr_index_++;
476 }
477 }
478
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700479 void VisitArguments() REQUIRES_SHARED(Locks::mutator_lock_) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800480 // (a) 'stack_args_' should point to the first method's argument
481 // (b) whatever the argument type it is, the 'stack_index_' should
482 // be moved forward along with every visiting.
Ian Rogers936b37f2014-02-14 00:52:24 -0800483 gpr_index_ = 0;
484 fpr_index_ = 0;
Zheng Xu5667fdb2014-10-23 18:29:55 +0800485 if (kQuickDoubleRegAlignedFloatBackFilled) {
486 fpr_double_index_ = 0;
487 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800488 stack_index_ = 0;
489 if (!is_static_) { // Handle this.
490 cur_type_ = Primitive::kPrimNot;
491 is_split_long_or_double_ = false;
Ian Rogers848871b2013-08-05 10:56:33 -0700492 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800493 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800494 if (kNumQuickGprArgs > 0) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800495 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800496 }
Ian Rogers848871b2013-08-05 10:56:33 -0700497 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800498 for (uint32_t shorty_index = 1; shorty_index < shorty_len_; ++shorty_index) {
499 cur_type_ = Primitive::GetType(shorty_[shorty_index]);
500 switch (cur_type_) {
501 case Primitive::kPrimNot:
502 case Primitive::kPrimBoolean:
503 case Primitive::kPrimByte:
504 case Primitive::kPrimChar:
505 case Primitive::kPrimShort:
506 case Primitive::kPrimInt:
507 is_split_long_or_double_ = false;
508 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800509 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800510 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800511 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800512 }
513 break;
514 case Primitive::kPrimFloat:
515 is_split_long_or_double_ = false;
516 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800517 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800518 if (kQuickSoftFloatAbi) {
519 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800520 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800521 }
522 } else {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800523 if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800524 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800525 if (kQuickDoubleRegAlignedFloatBackFilled) {
526 // Double should not overlap with float.
527 // For example, if fpr_index_ = 3, fpr_double_index_ should be at least 4.
528 fpr_double_index_ = std::max(fpr_double_index_, RoundUp(fpr_index_, 2));
529 // Float should not overlap with double.
530 if (fpr_index_ % 2 == 0) {
531 fpr_index_ = std::max(fpr_double_index_, fpr_index_);
532 }
Goran Jakovljevicff734982015-08-24 12:58:55 +0000533 } else if (kQuickSkipOddFpRegisters) {
534 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800535 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800536 }
537 }
538 break;
539 case Primitive::kPrimDouble:
540 case Primitive::kPrimLong:
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800541 if (kQuickSoftFloatAbi || (cur_type_ == Primitive::kPrimLong)) {
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800542 if (cur_type_ == Primitive::kPrimLong &&
543#if defined(__mips__) && !defined(__LP64__)
544 (gpr_index_ == 0 || gpr_index_ == 2) &&
545#else
546 gpr_index_ == 0 &&
547#endif
548 kAlignPairRegister) {
549 // Currently, this is only for ARM and MIPS, where we align long parameters with
550 // even-numbered registers by skipping R1 (on ARM) or A1(A3) (on MIPS) and using
551 // R2 (on ARM) or A2(T0) (on MIPS) instead.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800552 IncGprIndex();
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000553 }
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000554 is_split_long_or_double_ = (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) &&
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800555 ((gpr_index_ + 1) == kNumQuickGprArgs);
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500556 if (!kSplitPairAcrossRegisterAndStack && is_split_long_or_double_) {
557 // We don't want to split this. Pass over this register.
558 gpr_index_++;
559 is_split_long_or_double_ = false;
560 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800561 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800562 if (kBytesStackArgLocation == 4) {
563 stack_index_+= 2;
564 } else {
565 CHECK_EQ(kBytesStackArgLocation, 8U);
566 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800567 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700568 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800569 IncGprIndex();
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000570 if (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700571 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800572 IncGprIndex();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700573 }
574 }
575 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800576 } else {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000577 is_split_long_or_double_ = (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) &&
Zheng Xu5667fdb2014-10-23 18:29:55 +0800578 ((fpr_index_ + 1) == kNumQuickFprArgs) && !kQuickDoubleRegAlignedFloatBackFilled;
Ian Rogers936b37f2014-02-14 00:52:24 -0800579 Visit();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700580 if (kBytesStackArgLocation == 4) {
581 stack_index_+= 2;
Ian Rogers936b37f2014-02-14 00:52:24 -0800582 } else {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700583 CHECK_EQ(kBytesStackArgLocation, 8U);
584 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800585 }
Zheng Xu5667fdb2014-10-23 18:29:55 +0800586 if (kQuickDoubleRegAlignedFloatBackFilled) {
587 if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) {
588 fpr_double_index_ += 2;
589 // Float should not overlap with double.
590 if (fpr_index_ % 2 == 0) {
591 fpr_index_ = std::max(fpr_double_index_, fpr_index_);
592 }
593 }
594 } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800595 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800596 if (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) {
597 if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800598 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800599 }
600 }
601 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800602 }
603 break;
604 default:
605 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty_;
606 }
Ian Rogers848871b2013-08-05 10:56:33 -0700607 }
608 }
609
Andreas Gampec200a4a2014-06-16 18:39:09 -0700610 protected:
Ian Rogers848871b2013-08-05 10:56:33 -0700611 const bool is_static_;
612 const char* const shorty_;
613 const uint32_t shorty_len_;
Andreas Gampec200a4a2014-06-16 18:39:09 -0700614
615 private:
Ian Rogers13735952014-10-08 12:43:28 -0700616 uint8_t* const gpr_args_; // Address of GPR arguments in callee save frame.
617 uint8_t* const fpr_args_; // Address of FPR arguments in callee save frame.
618 uint8_t* const stack_args_; // Address of stack arguments in caller's frame.
Ian Rogers936b37f2014-02-14 00:52:24 -0800619 uint32_t gpr_index_; // Index into spilled GPRs.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800620 // Index into spilled FPRs.
621 // In case kQuickDoubleRegAlignedFloatBackFilled, it may index a hole while fpr_double_index_
622 // holds a higher register number.
623 uint32_t fpr_index_;
624 // Index into spilled FPRs for aligned double.
625 // Only used when kQuickDoubleRegAlignedFloatBackFilled. Next available double register indexed in
626 // terms of singles, may be behind fpr_index.
627 uint32_t fpr_double_index_;
Ian Rogers936b37f2014-02-14 00:52:24 -0800628 uint32_t stack_index_; // Index into arguments on the stack.
629 // The current type of argument during VisitArguments.
630 Primitive::Type cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700631 // Does a 64bit parameter straddle the register and stack arguments?
632 bool is_split_long_or_double_;
633};
634
Sebastien Hertza836bc92014-11-25 16:30:53 +0100635// Returns the 'this' object of a proxy method. This function is only used by StackVisitor. It
636// allows to use the QuickArgumentVisitor constants without moving all the code in its own module.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700637extern "C" mirror::Object* artQuickGetProxyThisObject(ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700638 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertza836bc92014-11-25 16:30:53 +0100639 return QuickArgumentVisitor::GetProxyThisObject(sp);
640}
641
Ian Rogers848871b2013-08-05 10:56:33 -0700642// Visits arguments on the stack placing them into the shadow frame.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800643class BuildQuickShadowFrameVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700644 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700645 BuildQuickShadowFrameVisitor(ArtMethod** sp, bool is_static, const char* shorty,
646 uint32_t shorty_len, ShadowFrame* sf, size_t first_arg_reg) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700647 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), sf_(sf), cur_reg_(first_arg_reg) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700648
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700649 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700650
651 private:
Ian Rogers936b37f2014-02-14 00:52:24 -0800652 ShadowFrame* const sf_;
653 uint32_t cur_reg_;
Ian Rogers848871b2013-08-05 10:56:33 -0700654
Dragos Sbirleabd136a22013-08-13 18:07:04 -0700655 DISALLOW_COPY_AND_ASSIGN(BuildQuickShadowFrameVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700656};
657
Andreas Gampec200a4a2014-06-16 18:39:09 -0700658void BuildQuickShadowFrameVisitor::Visit() {
Ian Rogers9758f792014-03-13 09:02:55 -0700659 Primitive::Type type = GetParamPrimitiveType();
660 switch (type) {
661 case Primitive::kPrimLong: // Fall-through.
662 case Primitive::kPrimDouble:
663 if (IsSplitLongOrDouble()) {
664 sf_->SetVRegLong(cur_reg_, ReadSplitLongParam());
665 } else {
666 sf_->SetVRegLong(cur_reg_, *reinterpret_cast<jlong*>(GetParamAddress()));
667 }
668 ++cur_reg_;
669 break;
670 case Primitive::kPrimNot: {
671 StackReference<mirror::Object>* stack_ref =
672 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
673 sf_->SetVRegReference(cur_reg_, stack_ref->AsMirrorPtr());
674 }
675 break;
676 case Primitive::kPrimBoolean: // Fall-through.
677 case Primitive::kPrimByte: // Fall-through.
678 case Primitive::kPrimChar: // Fall-through.
679 case Primitive::kPrimShort: // Fall-through.
680 case Primitive::kPrimInt: // Fall-through.
681 case Primitive::kPrimFloat:
682 sf_->SetVReg(cur_reg_, *reinterpret_cast<jint*>(GetParamAddress()));
683 break;
684 case Primitive::kPrimVoid:
685 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700686 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -0700687 }
688 ++cur_reg_;
689}
690
Mathieu Chartiere401d142015-04-22 13:56:20 -0700691extern "C" uint64_t artQuickToInterpreterBridge(ArtMethod* method, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700692 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers848871b2013-08-05 10:56:33 -0700693 // Ensure we don't get thread suspension until the object arguments are safely in the shadow
694 // frame.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700695 ScopedQuickEntrypointChecks sqec(self);
Ian Rogers848871b2013-08-05 10:56:33 -0700696
Alex Light9139e002015-10-09 15:59:48 -0700697 if (UNLIKELY(!method->IsInvokable())) {
698 method->ThrowInvocationTimeError();
Ian Rogers848871b2013-08-05 10:56:33 -0700699 return 0;
Andreas Gampe639bdd12015-06-03 11:22:45 -0700700 }
701
702 JValue tmp_value;
703 ShadowFrame* deopt_frame = self->PopStackedShadowFrame(
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700704 StackedShadowFrameType::kDeoptimizationShadowFrame, false);
Andreas Gampe639bdd12015-06-03 11:22:45 -0700705 ManagedStack fragment;
706
David Sehr709b0702016-10-13 09:12:37 -0700707 DCHECK(!method->IsNative()) << method->PrettyMethod();
Andreas Gampe639bdd12015-06-03 11:22:45 -0700708 uint32_t shorty_len = 0;
Andreas Gampe542451c2016-07-26 09:02:02 -0700709 ArtMethod* non_proxy_method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Alex Lighte9dd04f2016-03-16 16:09:45 -0700710 const DexFile::CodeItem* code_item = non_proxy_method->GetCodeItem();
David Sehr709b0702016-10-13 09:12:37 -0700711 DCHECK(code_item != nullptr) << method->PrettyMethod();
Andreas Gampe639bdd12015-06-03 11:22:45 -0700712 const char* shorty = non_proxy_method->GetShorty(&shorty_len);
713
714 JValue result;
715
716 if (deopt_frame != nullptr) {
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700717 // Coming from partial-fragment deopt.
Andreas Gampe639bdd12015-06-03 11:22:45 -0700718
719 if (kIsDebugBuild) {
720 // Sanity-check: are the methods as expected? We check that the last shadow frame (the bottom
721 // of the call-stack) corresponds to the called method.
722 ShadowFrame* linked = deopt_frame;
723 while (linked->GetLink() != nullptr) {
724 linked = linked->GetLink();
725 }
David Sehr709b0702016-10-13 09:12:37 -0700726 CHECK_EQ(method, linked->GetMethod()) << method->PrettyMethod() << " "
727 << ArtMethod::PrettyMethod(linked->GetMethod());
Andreas Gampe639bdd12015-06-03 11:22:45 -0700728 }
729
730 if (VLOG_IS_ON(deopt)) {
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700731 // Print out the stack to verify that it was a partial-fragment deopt.
Andreas Gampe639bdd12015-06-03 11:22:45 -0700732 LOG(INFO) << "Continue-ing from deopt. Stack is:";
733 QuickExceptionHandler::DumpFramesWithType(self, true);
734 }
735
Mathieu Chartierf5769e12017-01-10 15:54:41 -0800736 ObjPtr<mirror::Throwable> pending_exception;
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100737 bool from_code = false;
738 self->PopDeoptimizationContext(&result, &pending_exception, /* out */ &from_code);
Andreas Gampe639bdd12015-06-03 11:22:45 -0700739
740 // Push a transition back into managed code onto the linked list in thread.
741 self->PushManagedStackFragment(&fragment);
742
743 // Ensure that the stack is still in order.
744 if (kIsDebugBuild) {
745 class DummyStackVisitor : public StackVisitor {
746 public:
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700747 explicit DummyStackVisitor(Thread* self_in) REQUIRES_SHARED(Locks::mutator_lock_)
Andreas Gampe639bdd12015-06-03 11:22:45 -0700748 : StackVisitor(self_in, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {}
749
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700750 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe639bdd12015-06-03 11:22:45 -0700751 // Nothing to do here. In a debug build, SanityCheckFrame will do the work in the walking
752 // logic. Just always say we want to continue.
753 return true;
754 }
755 };
756 DummyStackVisitor dsv(self);
757 dsv.WalkStack();
758 }
759
760 // Restore the exception that was pending before deoptimization then interpret the
761 // deoptimized frames.
762 if (pending_exception != nullptr) {
763 self->SetException(pending_exception);
764 }
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100765 interpreter::EnterInterpreterFromDeoptimize(self, deopt_frame, from_code, &result);
Ian Rogers848871b2013-08-05 10:56:33 -0700766 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -0700767 const char* old_cause = self->StartAssertNoThreadSuspension(
768 "Building interpreter shadow frame");
Ian Rogers848871b2013-08-05 10:56:33 -0700769 uint16_t num_regs = code_item->registers_size_;
Andreas Gampec200a4a2014-06-16 18:39:09 -0700770 // No last shadow coming from quick.
Andreas Gampeb3025922015-09-01 14:45:00 -0700771 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
Andreas Gampe03ec9302015-08-27 17:41:47 -0700772 CREATE_SHADOW_FRAME(num_regs, /* link */ nullptr, method, /* dex pc */ 0);
Andreas Gampeb3025922015-09-01 14:45:00 -0700773 ShadowFrame* shadow_frame = shadow_frame_unique_ptr.get();
Ian Rogers848871b2013-08-05 10:56:33 -0700774 size_t first_arg_reg = code_item->registers_size_ - code_item->ins_size_;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700775 BuildQuickShadowFrameVisitor shadow_frame_builder(sp, method->IsStatic(), shorty, shorty_len,
Ian Rogers936b37f2014-02-14 00:52:24 -0800776 shadow_frame, first_arg_reg);
Ian Rogers848871b2013-08-05 10:56:33 -0700777 shadow_frame_builder.VisitArguments();
Ian Rogerse94652f2014-12-02 11:13:19 -0800778 const bool needs_initialization =
779 method->IsStatic() && !method->GetDeclaringClass()->IsInitialized();
Ian Rogers848871b2013-08-05 10:56:33 -0700780 // Push a transition back into managed code onto the linked list in thread.
Ian Rogers848871b2013-08-05 10:56:33 -0700781 self->PushManagedStackFragment(&fragment);
782 self->PushShadowFrame(shadow_frame);
783 self->EndAssertNoThreadSuspension(old_cause);
784
Ian Rogerse94652f2014-12-02 11:13:19 -0800785 if (needs_initialization) {
Ian Rogers848871b2013-08-05 10:56:33 -0700786 // Ensure static method's class is initialized.
Ian Rogerse94652f2014-12-02 11:13:19 -0800787 StackHandleScope<1> hs(self);
788 Handle<mirror::Class> h_class(hs.NewHandle(shadow_frame->GetMethod()->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700789 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
David Sehr709b0702016-10-13 09:12:37 -0700790 DCHECK(Thread::Current()->IsExceptionPending())
791 << shadow_frame->GetMethod()->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700792 self->PopManagedStackFragment(fragment);
793 return 0;
794 }
795 }
Daniel Mihalyieb076692014-08-22 17:33:31 +0200796
Andreas Gampe639bdd12015-06-03 11:22:45 -0700797 result = interpreter::EnterInterpreterFromEntryPoint(self, code_item, shadow_frame);
Ian Rogers848871b2013-08-05 10:56:33 -0700798 }
Andreas Gampe639bdd12015-06-03 11:22:45 -0700799
800 // Pop transition.
801 self->PopManagedStackFragment(fragment);
802
803 // Request a stack deoptimization if needed
804 ArtMethod* caller = QuickArgumentVisitor::GetCallingMethod(sp);
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700805 uintptr_t caller_pc = QuickArgumentVisitor::GetCallingPc(sp);
Mingyao Yanga3549d22016-06-02 17:01:02 -0700806 // If caller_pc is the instrumentation exit stub, the stub will check to see if deoptimization
807 // should be done and it knows the real return pc.
808 if (UNLIKELY(caller_pc != reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) &&
Nicolas Geoffray433b79a2017-01-30 20:54:45 +0000809 Dbg::IsForcedInterpreterNeededForUpcall(self, caller))) {
810 if (!Runtime::Current()->IsAsyncDeoptimizeable(caller_pc)) {
811 LOG(WARNING) << "Got a deoptimization request on un-deoptimizable method "
812 << caller->PrettyMethod();
813 } else {
814 // Push the context of the deoptimization stack so we can restore the return value and the
815 // exception before executing the deoptimized frames.
816 self->PushDeoptimizationContext(
817 result, shorty[0] == 'L', /* from_code */ false, self->GetException());
Andreas Gampe639bdd12015-06-03 11:22:45 -0700818
Nicolas Geoffray433b79a2017-01-30 20:54:45 +0000819 // Set special exception to cause deoptimization.
820 self->SetException(Thread::GetDeoptimizationException());
821 }
Andreas Gampe639bdd12015-06-03 11:22:45 -0700822 }
823
824 // No need to restore the args since the method has already been run by the interpreter.
825 return result.GetJ();
Ian Rogers848871b2013-08-05 10:56:33 -0700826}
827
828// Visits arguments on the stack placing them into the args vector, Object* arguments are converted
829// to jobjects.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800830class BuildQuickArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700831 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700832 BuildQuickArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty, uint32_t shorty_len,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700833 ScopedObjectAccessUnchecked* soa, std::vector<jvalue>* args) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700834 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa), args_(args) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700835
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700836 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700837
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700838 void FixupReferences() REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800839
Ian Rogers848871b2013-08-05 10:56:33 -0700840 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700841 ScopedObjectAccessUnchecked* const soa_;
842 std::vector<jvalue>* const args_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800843 // References which we must update when exiting in case the GC moved the objects.
Ian Rogers700a4022014-05-19 16:49:03 -0700844 std::vector<std::pair<jobject, StackReference<mirror::Object>*>> references_;
Ian Rogers9758f792014-03-13 09:02:55 -0700845
Ian Rogers848871b2013-08-05 10:56:33 -0700846 DISALLOW_COPY_AND_ASSIGN(BuildQuickArgumentVisitor);
847};
848
Ian Rogers9758f792014-03-13 09:02:55 -0700849void BuildQuickArgumentVisitor::Visit() {
850 jvalue val;
851 Primitive::Type type = GetParamPrimitiveType();
852 switch (type) {
853 case Primitive::kPrimNot: {
854 StackReference<mirror::Object>* stack_ref =
855 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
856 val.l = soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
857 references_.push_back(std::make_pair(val.l, stack_ref));
858 break;
859 }
860 case Primitive::kPrimLong: // Fall-through.
861 case Primitive::kPrimDouble:
862 if (IsSplitLongOrDouble()) {
863 val.j = ReadSplitLongParam();
864 } else {
865 val.j = *reinterpret_cast<jlong*>(GetParamAddress());
866 }
867 break;
868 case Primitive::kPrimBoolean: // Fall-through.
869 case Primitive::kPrimByte: // Fall-through.
870 case Primitive::kPrimChar: // Fall-through.
871 case Primitive::kPrimShort: // Fall-through.
872 case Primitive::kPrimInt: // Fall-through.
873 case Primitive::kPrimFloat:
874 val.i = *reinterpret_cast<jint*>(GetParamAddress());
875 break;
876 case Primitive::kPrimVoid:
877 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700878 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -0700879 }
880 args_->push_back(val);
881}
882
883void BuildQuickArgumentVisitor::FixupReferences() {
884 // Fixup any references which may have changed.
885 for (const auto& pair : references_) {
Mathieu Chartier1a5337f2016-10-13 13:48:23 -0700886 pair.second->Assign(soa_->Decode<mirror::Object>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -0700887 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -0700888 }
889}
890
Ian Rogers848871b2013-08-05 10:56:33 -0700891// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
892// which is responsible for recording callee save registers. We explicitly place into jobjects the
893// incoming reference arguments (so they survive GC). We invoke the invocation handler, which is a
894// field within the proxy object, which will box the primitive arguments and deal with error cases.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700895extern "C" uint64_t artQuickProxyInvokeHandler(
896 ArtMethod* proxy_method, mirror::Object* receiver, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700897 REQUIRES_SHARED(Locks::mutator_lock_) {
David Sehr709b0702016-10-13 09:12:37 -0700898 DCHECK(proxy_method->IsProxyMethod()) << proxy_method->PrettyMethod();
899 DCHECK(receiver->GetClass()->IsProxyClass()) << proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700900 // Ensure we don't get thread suspension until the object arguments are safely in jobjects.
901 const char* old_cause =
902 self->StartAssertNoThreadSuspension("Adding to IRT proxy object arguments");
903 // Register the top of the managed stack, making stack crawlable.
David Sehr709b0702016-10-13 09:12:37 -0700904 DCHECK_EQ((*sp), proxy_method) << proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700905 self->VerifyStack();
906 // Start new JNI local reference state.
907 JNIEnvExt* env = self->GetJniEnv();
908 ScopedObjectAccessUnchecked soa(env);
909 ScopedJniEnvLocalRefState env_state(env);
910 // Create local ref. copies of proxy method and the receiver.
911 jobject rcvr_jobj = soa.AddLocalReference<jobject>(receiver);
912
913 // Placing arguments into args vector and remove the receiver.
Andreas Gampe542451c2016-07-26 09:02:02 -0700914 ArtMethod* non_proxy_method = proxy_method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
David Sehr709b0702016-10-13 09:12:37 -0700915 CHECK(!non_proxy_method->IsStatic()) << proxy_method->PrettyMethod() << " "
916 << non_proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700917 std::vector<jvalue> args;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700918 uint32_t shorty_len = 0;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700919 const char* shorty = non_proxy_method->GetShorty(&shorty_len);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700920 BuildQuickArgumentVisitor local_ref_visitor(sp, false, shorty, shorty_len, &soa, &args);
Brian Carlstromd3633d52013-08-20 21:06:26 -0700921
Ian Rogers848871b2013-08-05 10:56:33 -0700922 local_ref_visitor.VisitArguments();
David Sehr709b0702016-10-13 09:12:37 -0700923 DCHECK_GT(args.size(), 0U) << proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700924 args.erase(args.begin());
925
926 // Convert proxy method into expected interface method.
Andreas Gampe542451c2016-07-26 09:02:02 -0700927 ArtMethod* interface_method = proxy_method->FindOverriddenMethod(kRuntimePointerSize);
David Sehr709b0702016-10-13 09:12:37 -0700928 DCHECK(interface_method != nullptr) << proxy_method->PrettyMethod();
929 DCHECK(!interface_method->IsProxyMethod()) << interface_method->PrettyMethod();
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700930 self->EndAssertNoThreadSuspension(old_cause);
Andreas Gampe542451c2016-07-26 09:02:02 -0700931 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Andreas Gampee01e3642016-07-25 13:06:04 -0700932 DCHECK(!Runtime::Current()->IsActiveTransaction());
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700933 jobject interface_method_jobj = soa.AddLocalReference<jobject>(
Andreas Gampe542451c2016-07-26 09:02:02 -0700934 mirror::Method::CreateFromArtMethod<kRuntimePointerSize, false>(soa.Self(),
935 interface_method));
Ian Rogers848871b2013-08-05 10:56:33 -0700936
937 // All naked Object*s should now be in jobjects, so its safe to go into the main invoke code
938 // that performs allocations.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700939 JValue result = InvokeProxyInvocationHandler(soa, shorty, rcvr_jobj, interface_method_jobj, args);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800940 // Restore references which might have moved.
941 local_ref_visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -0700942 return result.GetJ();
943}
944
945// Read object references held in arguments from quick frames and place in a JNI local references,
946// so they don't get garbage collected.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800947class RememberForGcArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700948 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700949 RememberForGcArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty,
950 uint32_t shorty_len, ScopedObjectAccessUnchecked* soa) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700951 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700952
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700953 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Mathieu Chartier07d447b2013-09-26 11:57:43 -0700954
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700955 void FixupReferences() REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers848871b2013-08-05 10:56:33 -0700956
957 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700958 ScopedObjectAccessUnchecked* const soa_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800959 // References which we must update when exiting in case the GC moved the objects.
Andreas Gampec200a4a2014-06-16 18:39:09 -0700960 std::vector<std::pair<jobject, StackReference<mirror::Object>*> > references_;
961
Mathieu Chartier590fee92013-09-13 13:46:47 -0700962 DISALLOW_COPY_AND_ASSIGN(RememberForGcArgumentVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700963};
964
Ian Rogers9758f792014-03-13 09:02:55 -0700965void RememberForGcArgumentVisitor::Visit() {
966 if (IsParamAReference()) {
967 StackReference<mirror::Object>* stack_ref =
968 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
969 jobject reference =
970 soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
971 references_.push_back(std::make_pair(reference, stack_ref));
972 }
973}
974
975void RememberForGcArgumentVisitor::FixupReferences() {
976 // Fixup any references which may have changed.
977 for (const auto& pair : references_) {
Mathieu Chartier1a5337f2016-10-13 13:48:23 -0700978 pair.second->Assign(soa_->Decode<mirror::Object>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -0700979 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -0700980 }
981}
982
Ian Rogers848871b2013-08-05 10:56:33 -0700983// Lazily resolve a method for quick. Called by stub code.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700984extern "C" const void* artQuickResolutionTrampoline(
985 ArtMethod* called, mirror::Object* receiver, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700986 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe3b45ef22015-05-26 21:34:09 -0700987 // The resolution trampoline stashes the resolved method into the callee-save frame to transport
988 // it. Thus, when exiting, the stack cannot be verified (as the resolved method most likely
989 // does not have the same stack layout as the callee-save method).
990 ScopedQuickEntrypointChecks sqec(self, kIsDebugBuild, false);
Ian Rogers848871b2013-08-05 10:56:33 -0700991 // Start new JNI local reference state
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800992 JNIEnvExt* env = self->GetJniEnv();
Ian Rogers848871b2013-08-05 10:56:33 -0700993 ScopedObjectAccessUnchecked soa(env);
994 ScopedJniEnvLocalRefState env_state(env);
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800995 const char* old_cause = self->StartAssertNoThreadSuspension("Quick method resolution set up");
Ian Rogers848871b2013-08-05 10:56:33 -0700996
997 // Compute details about the called method (avoid GCs)
998 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Ian Rogers848871b2013-08-05 10:56:33 -0700999 InvokeType invoke_type;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001000 MethodReference called_method(nullptr, 0);
1001 const bool called_method_known_on_entry = !called->IsRuntimeMethod();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001002 ArtMethod* caller = nullptr;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001003 if (!called_method_known_on_entry) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01001004 caller = QuickArgumentVisitor::GetCallingMethod(sp);
Ian Rogerse0a02da2014-12-02 14:10:53 -08001005 called_method.dex_file = caller->GetDexFile();
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001006
1007 InvokeType stack_map_invoke_type;
1008 uint32_t stack_map_dex_method_idx;
1009 const bool found_stack_map = QuickArgumentVisitor::GetInvokeType(sp,
1010 &stack_map_invoke_type,
1011 &stack_map_dex_method_idx);
1012 // For debug builds, we make sure both of the paths are consistent by also looking at the dex
1013 // code.
1014 if (!found_stack_map || kIsDebugBuild) {
1015 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
1016 const DexFile::CodeItem* code;
1017 code = caller->GetCodeItem();
1018 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
1019 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
1020 Instruction::Code instr_code = instr->Opcode();
1021 bool is_range;
1022 switch (instr_code) {
1023 case Instruction::INVOKE_DIRECT:
1024 invoke_type = kDirect;
1025 is_range = false;
1026 break;
1027 case Instruction::INVOKE_DIRECT_RANGE:
1028 invoke_type = kDirect;
1029 is_range = true;
1030 break;
1031 case Instruction::INVOKE_STATIC:
1032 invoke_type = kStatic;
1033 is_range = false;
1034 break;
1035 case Instruction::INVOKE_STATIC_RANGE:
1036 invoke_type = kStatic;
1037 is_range = true;
1038 break;
1039 case Instruction::INVOKE_SUPER:
1040 invoke_type = kSuper;
1041 is_range = false;
1042 break;
1043 case Instruction::INVOKE_SUPER_RANGE:
1044 invoke_type = kSuper;
1045 is_range = true;
1046 break;
1047 case Instruction::INVOKE_VIRTUAL:
1048 invoke_type = kVirtual;
1049 is_range = false;
1050 break;
1051 case Instruction::INVOKE_VIRTUAL_RANGE:
1052 invoke_type = kVirtual;
1053 is_range = true;
1054 break;
1055 case Instruction::INVOKE_INTERFACE:
1056 invoke_type = kInterface;
1057 is_range = false;
1058 break;
1059 case Instruction::INVOKE_INTERFACE_RANGE:
1060 invoke_type = kInterface;
1061 is_range = true;
1062 break;
1063 default:
1064 LOG(FATAL) << "Unexpected call into trampoline: " << instr->DumpString(nullptr);
1065 UNREACHABLE();
1066 }
1067 called_method.dex_method_index = (is_range) ? instr->VRegB_3rc() : instr->VRegB_35c();
1068 // Check that the invoke matches what we expected, note that this path only happens for debug
1069 // builds.
1070 if (found_stack_map) {
1071 DCHECK_EQ(stack_map_invoke_type, invoke_type);
1072 if (invoke_type != kSuper) {
1073 // Super may be sharpened.
1074 DCHECK_EQ(stack_map_dex_method_idx, called_method.dex_method_index)
1075 << called_method.dex_file->PrettyMethod(stack_map_dex_method_idx) << " "
1076 << called_method.dex_file->PrettyMethod(called_method.dex_method_index);
1077 }
1078 } else {
Andreas Gampe9e6dee22017-04-11 13:50:23 -07001079 VLOG(dex) << "Accessed dex file for invoke " << invoke_type << " "
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001080 << called_method.dex_method_index;
1081 }
1082 } else {
1083 invoke_type = stack_map_invoke_type;
1084 called_method.dex_method_index = stack_map_dex_method_idx;
Ian Rogers848871b2013-08-05 10:56:33 -07001085 }
Ian Rogers848871b2013-08-05 10:56:33 -07001086 } else {
1087 invoke_type = kStatic;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001088 called_method.dex_file = called->GetDexFile();
1089 called_method.dex_method_index = called->GetDexMethodIndex();
Ian Rogers848871b2013-08-05 10:56:33 -07001090 }
1091 uint32_t shorty_len;
1092 const char* shorty =
Ian Rogerse0a02da2014-12-02 14:10:53 -08001093 called_method.dex_file->GetMethodShorty(
1094 called_method.dex_file->GetMethodId(called_method.dex_method_index), &shorty_len);
Mathieu Chartier590fee92013-09-13 13:46:47 -07001095 RememberForGcArgumentVisitor visitor(sp, invoke_type == kStatic, shorty, shorty_len, &soa);
Ian Rogers848871b2013-08-05 10:56:33 -07001096 visitor.VisitArguments();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001097 self->EndAssertNoThreadSuspension(old_cause);
Ian Rogerse0a02da2014-12-02 14:10:53 -08001098 const bool virtual_or_interface = invoke_type == kVirtual || invoke_type == kInterface;
Ian Rogers848871b2013-08-05 10:56:33 -07001099 // Resolve method filling in dex cache.
Ian Rogerse0a02da2014-12-02 14:10:53 -08001100 if (!called_method_known_on_entry) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001101 StackHandleScope<1> hs(self);
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001102 mirror::Object* dummy = nullptr;
1103 HandleWrapper<mirror::Object> h_receiver(
1104 hs.NewHandleWrapper(virtual_or_interface ? &receiver : &dummy));
Ian Rogerse0a02da2014-12-02 14:10:53 -08001105 DCHECK_EQ(caller->GetDexFile(), called_method.dex_file);
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08001106 called = linker->ResolveMethod<ClassLinker::kForceICCECheck>(
1107 self, called_method.dex_method_index, caller, invoke_type);
Ian Rogers848871b2013-08-05 10:56:33 -07001108 }
Ian Rogerse0a02da2014-12-02 14:10:53 -08001109 const void* code = nullptr;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001110 if (LIKELY(!self->IsExceptionPending())) {
Ian Rogers848871b2013-08-05 10:56:33 -07001111 // Incompatible class change should have been handled in resolve method.
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001112 CHECK(!called->CheckIncompatibleClassChange(invoke_type))
David Sehr709b0702016-10-13 09:12:37 -07001113 << called->PrettyMethod() << " " << invoke_type;
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001114 if (virtual_or_interface || invoke_type == kSuper) {
1115 // Refine called method based on receiver for kVirtual/kInterface, and
1116 // caller for kSuper.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001117 ArtMethod* orig_called = called;
Mathieu Chartier55871bf2014-02-27 10:24:50 -08001118 if (invoke_type == kVirtual) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001119 CHECK(receiver != nullptr) << invoke_type;
Andreas Gampe542451c2016-07-26 09:02:02 -07001120 called = receiver->GetClass()->FindVirtualMethodForVirtual(called, kRuntimePointerSize);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001121 } else if (invoke_type == kInterface) {
1122 CHECK(receiver != nullptr) << invoke_type;
Andreas Gampe542451c2016-07-26 09:02:02 -07001123 called = receiver->GetClass()->FindVirtualMethodForInterface(called, kRuntimePointerSize);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001124 } else {
1125 DCHECK_EQ(invoke_type, kSuper);
1126 CHECK(caller != nullptr) << invoke_type;
Nicolas Geoffray393fdb82016-04-25 14:58:06 +01001127 StackHandleScope<2> hs(self);
1128 Handle<mirror::DexCache> dex_cache(
1129 hs.NewHandle(caller->GetDeclaringClass()->GetDexCache()));
1130 Handle<mirror::ClassLoader> class_loader(
1131 hs.NewHandle(caller->GetDeclaringClass()->GetClassLoader()));
Alex Lightfedd91d2016-01-07 14:49:16 -08001132 // TODO Maybe put this into a mirror::Class function.
1133 mirror::Class* ref_class = linker->ResolveReferencedClassOfMethod(
Nicolas Geoffray393fdb82016-04-25 14:58:06 +01001134 called_method.dex_method_index, dex_cache, class_loader);
Alex Lightfedd91d2016-01-07 14:49:16 -08001135 if (ref_class->IsInterface()) {
Andreas Gampe542451c2016-07-26 09:02:02 -07001136 called = ref_class->FindVirtualMethodForInterfaceSuper(called, kRuntimePointerSize);
Alex Lightfedd91d2016-01-07 14:49:16 -08001137 } else {
1138 called = caller->GetDeclaringClass()->GetSuperClass()->GetVTableEntry(
Andreas Gampe542451c2016-07-26 09:02:02 -07001139 called->GetMethodIndex(), kRuntimePointerSize);
Alex Lightfedd91d2016-01-07 14:49:16 -08001140 }
Mathieu Chartier55871bf2014-02-27 10:24:50 -08001141 }
Mingyao Yangf4867782014-05-05 11:55:02 -07001142
David Sehr709b0702016-10-13 09:12:37 -07001143 CHECK(called != nullptr) << orig_called->PrettyMethod() << " "
1144 << mirror::Object::PrettyTypeOf(receiver) << " "
Mingyao Yangf4867782014-05-05 11:55:02 -07001145 << invoke_type << " " << orig_called->GetVtableIndex();
1146
Ian Rogers83883d72013-10-21 21:07:24 -07001147 // We came here because of sharpening. Ensure the dex cache is up-to-date on the method index
Ian Rogerse0a02da2014-12-02 14:10:53 -08001148 // of the sharpened method avoiding dirtying the dex cache if possible.
Ian Rogers00f15272014-12-02 16:55:46 -08001149 // Note, called_method.dex_method_index references the dex method before the
1150 // FindVirtualMethodFor... This is ok for FindDexMethodIndexInOtherDexFile that only cares
1151 // about the name and signature.
1152 uint32_t update_dex_cache_method_index = called->GetDexMethodIndex();
Andreas Gampe542451c2016-07-26 09:02:02 -07001153 if (!called->HasSameDexCacheResolvedMethods(caller, kRuntimePointerSize)) {
Ian Rogers83883d72013-10-21 21:07:24 -07001154 // Calling from one dex file to another, need to compute the method index appropriate to
Vladimir Markobbcc0c02014-02-03 14:08:42 +00001155 // the caller's dex file. Since we get here only if the original called was a runtime
1156 // method, we've got the correct dex_file and a dex_method_idx from above.
Ian Rogerse0a02da2014-12-02 14:10:53 -08001157 DCHECK(!called_method_known_on_entry);
1158 DCHECK_EQ(caller->GetDexFile(), called_method.dex_file);
1159 const DexFile* caller_dex_file = called_method.dex_file;
1160 uint32_t caller_method_name_and_sig_index = called_method.dex_method_index;
1161 update_dex_cache_method_index =
1162 called->FindDexMethodIndexInOtherDexFile(*caller_dex_file,
1163 caller_method_name_and_sig_index);
1164 }
1165 if ((update_dex_cache_method_index != DexFile::kDexNoIndex) &&
Mathieu Chartiere401d142015-04-22 13:56:20 -07001166 (caller->GetDexCacheResolvedMethod(
Andreas Gampe542451c2016-07-26 09:02:02 -07001167 update_dex_cache_method_index, kRuntimePointerSize) != called)) {
1168 caller->SetDexCacheResolvedMethod(update_dex_cache_method_index,
1169 called,
1170 kRuntimePointerSize);
Ian Rogers83883d72013-10-21 21:07:24 -07001171 }
Mathieu Chartiere4a91bb2015-01-28 13:11:44 -08001172 } else if (invoke_type == kStatic) {
1173 const auto called_dex_method_idx = called->GetDexMethodIndex();
1174 // For static invokes, we may dispatch to the static method in the superclass but resolve
1175 // using the subclass. To prevent getting slow paths on each invoke, we force set the
1176 // resolved method for the super class dex method index if we are in the same dex file.
1177 // b/19175856
1178 if (called->GetDexFile() == called_method.dex_file &&
1179 called_method.dex_method_index != called_dex_method_idx) {
Andreas Gampe542451c2016-07-26 09:02:02 -07001180 called->GetDexCache()->SetResolvedMethod(called_dex_method_idx,
1181 called,
1182 kRuntimePointerSize);
Mathieu Chartiere4a91bb2015-01-28 13:11:44 -08001183 }
Ian Rogers83883d72013-10-21 21:07:24 -07001184 }
Daniel Mihalyieb076692014-08-22 17:33:31 +02001185
Ian Rogers848871b2013-08-05 10:56:33 -07001186 // Ensure that the called method's class is initialized.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001187 StackHandleScope<1> hs(soa.Self());
1188 Handle<mirror::Class> called_class(hs.NewHandle(called->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -07001189 linker->EnsureInitialized(soa.Self(), called_class, true, true);
Ian Rogers848871b2013-08-05 10:56:33 -07001190 if (LIKELY(called_class->IsInitialized())) {
Daniel Mihalyieb076692014-08-22 17:33:31 +02001191 if (UNLIKELY(Dbg::IsForcedInterpreterNeededForResolution(self, called))) {
1192 // If we are single-stepping or the called method is deoptimized (by a
1193 // breakpoint, for example), then we have to execute the called method
1194 // with the interpreter.
1195 code = GetQuickToInterpreterBridge();
1196 } else if (UNLIKELY(Dbg::IsForcedInstrumentationNeededForResolution(self, caller))) {
1197 // If the caller is deoptimized (by a breakpoint, for example), we have to
1198 // continue its execution with interpreter when returning from the called
1199 // method. Because we do not want to execute the called method with the
1200 // interpreter, we wrap its execution into the instrumentation stubs.
1201 // When the called method returns, it will execute the instrumentation
1202 // exit hook that will determine the need of the interpreter with a call
1203 // to Dbg::IsForcedInterpreterNeededForUpcall and deoptimize the stack if
1204 // it is needed.
1205 code = GetQuickInstrumentationEntryPoint();
1206 } else {
1207 code = called->GetEntryPointFromQuickCompiledCode();
1208 }
Ian Rogers848871b2013-08-05 10:56:33 -07001209 } else if (called_class->IsInitializing()) {
Daniel Mihalyieb076692014-08-22 17:33:31 +02001210 if (UNLIKELY(Dbg::IsForcedInterpreterNeededForResolution(self, called))) {
1211 // If we are single-stepping or the called method is deoptimized (by a
1212 // breakpoint, for example), then we have to execute the called method
1213 // with the interpreter.
1214 code = GetQuickToInterpreterBridge();
1215 } else if (invoke_type == kStatic) {
Ian Rogers848871b2013-08-05 10:56:33 -07001216 // Class is still initializing, go to oat and grab code (trampoline must be left in place
1217 // until class is initialized to stop races between threads).
Ian Rogersef7d42f2014-01-06 12:55:46 -08001218 code = linker->GetQuickOatCodeFor(called);
Ian Rogers848871b2013-08-05 10:56:33 -07001219 } else {
1220 // No trampoline for non-static methods.
Ian Rogersef7d42f2014-01-06 12:55:46 -08001221 code = called->GetEntryPointFromQuickCompiledCode();
Ian Rogers848871b2013-08-05 10:56:33 -07001222 }
1223 } else {
1224 DCHECK(called_class->IsErroneous());
1225 }
1226 }
Ian Rogerse0a02da2014-12-02 14:10:53 -08001227 CHECK_EQ(code == nullptr, self->IsExceptionPending());
Mathieu Chartier07d447b2013-09-26 11:57:43 -07001228 // Fixup any locally saved objects may have moved during a GC.
1229 visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -07001230 // Place called method in callee-save frame to be placed as first argument to quick method.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001231 *sp = called;
1232
Ian Rogers848871b2013-08-05 10:56:33 -07001233 return code;
1234}
1235
Andreas Gampec147b002014-03-06 18:11:06 -08001236/*
1237 * This class uses a couple of observations to unite the different calling conventions through
1238 * a few constants.
1239 *
1240 * 1) Number of registers used for passing is normally even, so counting down has no penalty for
1241 * possible alignment.
1242 * 2) Known 64b architectures store 8B units on the stack, both for integral and floating point
1243 * types, so using uintptr_t is OK. Also means that we can use kRegistersNeededX to denote
1244 * when we have to split things
1245 * 3) The only soft-float, Arm, is 32b, so no widening needs to be taken into account for floats
1246 * and we can use Int handling directly.
1247 * 4) Only 64b architectures widen, and their stack is aligned 8B anyways, so no padding code
1248 * necessary when widening. Also, widening of Ints will take place implicitly, and the
1249 * extension should be compatible with Aarch64, which mandates copying the available bits
1250 * into LSB and leaving the rest unspecified.
1251 * 5) Aligning longs and doubles is necessary on arm only, and it's the same in registers and on
1252 * the stack.
1253 * 6) There is only little endian.
1254 *
1255 *
1256 * Actual work is supposed to be done in a delegate of the template type. The interface is as
1257 * follows:
1258 *
1259 * void PushGpr(uintptr_t): Add a value for the next GPR
1260 *
1261 * void PushFpr4(float): Add a value for the next FPR of size 32b. Is only called if we need
1262 * padding, that is, think the architecture is 32b and aligns 64b.
1263 *
1264 * void PushFpr8(uint64_t): Push a double. We _will_ call this on 32b, it's the callee's job to
1265 * split this if necessary. The current state will have aligned, if
1266 * necessary.
1267 *
1268 * void PushStack(uintptr_t): Push a value to the stack.
1269 *
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001270 * uintptr_t PushHandleScope(mirror::Object* ref): Add a reference to the HandleScope. This _will_ have nullptr,
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001271 * as this might be important for null initialization.
Andreas Gampec147b002014-03-06 18:11:06 -08001272 * Must return the jobject, that is, the reference to the
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001273 * entry in the HandleScope (nullptr if necessary).
Andreas Gampec147b002014-03-06 18:11:06 -08001274 *
1275 */
Andreas Gampec200a4a2014-06-16 18:39:09 -07001276template<class T> class BuildNativeCallFrameStateMachine {
Andreas Gampec147b002014-03-06 18:11:06 -08001277 public:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001278#if defined(__arm__)
1279 // TODO: These are all dummy values!
Andreas Gampec147b002014-03-06 18:11:06 -08001280 static constexpr bool kNativeSoftFloatAbi = true;
1281 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs, r0-r3
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001282 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
1283
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001284 static constexpr size_t kRegistersNeededForLong = 2;
1285 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -08001286 static constexpr bool kMultiRegistersAligned = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001287 static constexpr bool kMultiFPRegistersWidened = false;
1288 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001289 static constexpr bool kAlignLongOnStack = true;
1290 static constexpr bool kAlignDoubleOnStack = true;
Stuart Monteithb95a5342014-03-12 13:32:32 +00001291#elif defined(__aarch64__)
1292 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
1293 static constexpr size_t kNumNativeGprArgs = 8; // 6 arguments passed in GPRs.
1294 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
1295
1296 static constexpr size_t kRegistersNeededForLong = 1;
1297 static constexpr size_t kRegistersNeededForDouble = 1;
1298 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001299 static constexpr bool kMultiFPRegistersWidened = false;
1300 static constexpr bool kMultiGPRegistersWidened = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +00001301 static constexpr bool kAlignLongOnStack = false;
1302 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001303#elif defined(__mips__) && !defined(__LP64__)
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001304 static constexpr bool kNativeSoftFloatAbi = true; // This is a hard float ABI.
Douglas Leung735b8552014-10-31 12:21:40 -07001305 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs.
1306 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001307
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001308 static constexpr size_t kRegistersNeededForLong = 2;
1309 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -08001310 static constexpr bool kMultiRegistersAligned = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001311 static constexpr bool kMultiFPRegistersWidened = true;
1312 static constexpr bool kMultiGPRegistersWidened = false;
Douglas Leung735b8552014-10-31 12:21:40 -07001313 static constexpr bool kAlignLongOnStack = true;
1314 static constexpr bool kAlignDoubleOnStack = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001315#elif defined(__mips__) && defined(__LP64__)
1316 // Let the code prepare GPRs only and we will load the FPRs with same data.
1317 static constexpr bool kNativeSoftFloatAbi = true;
1318 static constexpr size_t kNumNativeGprArgs = 8;
1319 static constexpr size_t kNumNativeFprArgs = 0;
1320
1321 static constexpr size_t kRegistersNeededForLong = 1;
1322 static constexpr size_t kRegistersNeededForDouble = 1;
1323 static constexpr bool kMultiRegistersAligned = false;
1324 static constexpr bool kMultiFPRegistersWidened = false;
1325 static constexpr bool kMultiGPRegistersWidened = true;
1326 static constexpr bool kAlignLongOnStack = false;
1327 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001328#elif defined(__i386__)
1329 // TODO: Check these!
Andreas Gampec147b002014-03-06 18:11:06 -08001330 static constexpr bool kNativeSoftFloatAbi = false; // Not using int registers for fp
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001331 static constexpr size_t kNumNativeGprArgs = 0; // 6 arguments passed in GPRs.
1332 static constexpr size_t kNumNativeFprArgs = 0; // 8 arguments passed in FPRs.
1333
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001334 static constexpr size_t kRegistersNeededForLong = 2;
1335 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001336 static constexpr bool kMultiRegistersAligned = false; // x86 not using regs, anyways
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001337 static constexpr bool kMultiFPRegistersWidened = false;
1338 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001339 static constexpr bool kAlignLongOnStack = false;
1340 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001341#elif defined(__x86_64__)
1342 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
1343 static constexpr size_t kNumNativeGprArgs = 6; // 6 arguments passed in GPRs.
1344 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
1345
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001346 static constexpr size_t kRegistersNeededForLong = 1;
1347 static constexpr size_t kRegistersNeededForDouble = 1;
Andreas Gampec147b002014-03-06 18:11:06 -08001348 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001349 static constexpr bool kMultiFPRegistersWidened = false;
1350 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001351 static constexpr bool kAlignLongOnStack = false;
1352 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001353#else
1354#error "Unsupported architecture"
1355#endif
1356
Andreas Gampec147b002014-03-06 18:11:06 -08001357 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001358 explicit BuildNativeCallFrameStateMachine(T* delegate)
1359 : gpr_index_(kNumNativeGprArgs),
1360 fpr_index_(kNumNativeFprArgs),
1361 stack_entries_(0),
1362 delegate_(delegate) {
Andreas Gampec147b002014-03-06 18:11:06 -08001363 // For register alignment, we want to assume that counters (gpr_index_, fpr_index_) are even iff
1364 // the next register is even; counting down is just to make the compiler happy...
Andreas Gampe575e78c2014-11-03 23:41:03 -08001365 static_assert(kNumNativeGprArgs % 2 == 0U, "Number of native GPR arguments not even");
1366 static_assert(kNumNativeFprArgs % 2 == 0U, "Number of native FPR arguments not even");
Andreas Gampec147b002014-03-06 18:11:06 -08001367 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001368
Andreas Gampec200a4a2014-06-16 18:39:09 -07001369 virtual ~BuildNativeCallFrameStateMachine() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001370
Ian Rogers1428dce2014-10-21 15:02:15 -07001371 bool HavePointerGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001372 return gpr_index_ > 0;
1373 }
1374
Andreas Gampec200a4a2014-06-16 18:39:09 -07001375 void AdvancePointer(const void* val) {
Andreas Gampec147b002014-03-06 18:11:06 -08001376 if (HavePointerGpr()) {
1377 gpr_index_--;
1378 PushGpr(reinterpret_cast<uintptr_t>(val));
1379 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001380 stack_entries_++; // TODO: have a field for pointer length as multiple of 32b
Andreas Gampec147b002014-03-06 18:11:06 -08001381 PushStack(reinterpret_cast<uintptr_t>(val));
1382 gpr_index_ = 0;
1383 }
1384 }
1385
Ian Rogers1428dce2014-10-21 15:02:15 -07001386 bool HaveHandleScopeGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001387 return gpr_index_ > 0;
1388 }
1389
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001390 void AdvanceHandleScope(mirror::Object* ptr) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001391 uintptr_t handle = PushHandle(ptr);
1392 if (HaveHandleScopeGpr()) {
Andreas Gampec147b002014-03-06 18:11:06 -08001393 gpr_index_--;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001394 PushGpr(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001395 } else {
1396 stack_entries_++;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001397 PushStack(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001398 gpr_index_ = 0;
1399 }
1400 }
1401
Ian Rogers1428dce2014-10-21 15:02:15 -07001402 bool HaveIntGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001403 return gpr_index_ > 0;
1404 }
1405
1406 void AdvanceInt(uint32_t val) {
1407 if (HaveIntGpr()) {
1408 gpr_index_--;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001409 if (kMultiGPRegistersWidened) {
1410 DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001411 PushGpr(static_cast<int64_t>(bit_cast<int32_t, uint32_t>(val)));
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001412 } else {
1413 PushGpr(val);
1414 }
Andreas Gampec147b002014-03-06 18:11:06 -08001415 } else {
1416 stack_entries_++;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001417 if (kMultiGPRegistersWidened) {
1418 DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001419 PushStack(static_cast<int64_t>(bit_cast<int32_t, uint32_t>(val)));
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001420 } else {
1421 PushStack(val);
1422 }
Andreas Gampec147b002014-03-06 18:11:06 -08001423 gpr_index_ = 0;
1424 }
1425 }
1426
Ian Rogers1428dce2014-10-21 15:02:15 -07001427 bool HaveLongGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001428 return gpr_index_ >= kRegistersNeededForLong + (LongGprNeedsPadding() ? 1 : 0);
1429 }
1430
Ian Rogers1428dce2014-10-21 15:02:15 -07001431 bool LongGprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001432 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1433 kAlignLongOnStack && // and when it needs alignment
1434 (gpr_index_ & 1) == 1; // counter is odd, see constructor
1435 }
1436
Ian Rogers1428dce2014-10-21 15:02:15 -07001437 bool LongStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001438 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1439 kAlignLongOnStack && // and when it needs 8B alignment
1440 (stack_entries_ & 1) == 1; // counter is odd
1441 }
1442
1443 void AdvanceLong(uint64_t val) {
1444 if (HaveLongGpr()) {
1445 if (LongGprNeedsPadding()) {
1446 PushGpr(0);
1447 gpr_index_--;
1448 }
1449 if (kRegistersNeededForLong == 1) {
1450 PushGpr(static_cast<uintptr_t>(val));
1451 } else {
1452 PushGpr(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1453 PushGpr(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1454 }
1455 gpr_index_ -= kRegistersNeededForLong;
1456 } else {
1457 if (LongStackNeedsPadding()) {
1458 PushStack(0);
1459 stack_entries_++;
1460 }
1461 if (kRegistersNeededForLong == 1) {
1462 PushStack(static_cast<uintptr_t>(val));
1463 stack_entries_++;
1464 } else {
1465 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1466 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1467 stack_entries_ += 2;
1468 }
1469 gpr_index_ = 0;
1470 }
1471 }
1472
Ian Rogers1428dce2014-10-21 15:02:15 -07001473 bool HaveFloatFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001474 return fpr_index_ > 0;
1475 }
1476
Andreas Gampec147b002014-03-06 18:11:06 -08001477 void AdvanceFloat(float val) {
1478 if (kNativeSoftFloatAbi) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001479 AdvanceInt(bit_cast<uint32_t, float>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001480 } else {
1481 if (HaveFloatFpr()) {
1482 fpr_index_--;
1483 if (kRegistersNeededForDouble == 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001484 if (kMultiFPRegistersWidened) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001485 PushFpr8(bit_cast<uint64_t, double>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001486 } else {
1487 // No widening, just use the bits.
Roland Levillainda4d79b2015-03-24 14:36:11 +00001488 PushFpr8(static_cast<uint64_t>(bit_cast<uint32_t, float>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001489 }
1490 } else {
1491 PushFpr4(val);
1492 }
1493 } else {
1494 stack_entries_++;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001495 if (kRegistersNeededForDouble == 1 && kMultiFPRegistersWidened) {
Andreas Gampec147b002014-03-06 18:11:06 -08001496 // Need to widen before storing: Note the "double" in the template instantiation.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001497 // Note: We need to jump through those hoops to make the compiler happy.
1498 DCHECK_EQ(sizeof(uintptr_t), sizeof(uint64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001499 PushStack(static_cast<uintptr_t>(bit_cast<uint64_t, double>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001500 } else {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001501 PushStack(static_cast<uintptr_t>(bit_cast<uint32_t, float>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001502 }
1503 fpr_index_ = 0;
1504 }
1505 }
1506 }
1507
Ian Rogers1428dce2014-10-21 15:02:15 -07001508 bool HaveDoubleFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001509 return fpr_index_ >= kRegistersNeededForDouble + (DoubleFprNeedsPadding() ? 1 : 0);
1510 }
1511
Ian Rogers1428dce2014-10-21 15:02:15 -07001512 bool DoubleFprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001513 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1514 kAlignDoubleOnStack && // and when it needs alignment
1515 (fpr_index_ & 1) == 1; // counter is odd, see constructor
1516 }
1517
Ian Rogers1428dce2014-10-21 15:02:15 -07001518 bool DoubleStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001519 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1520 kAlignDoubleOnStack && // and when it needs 8B alignment
1521 (stack_entries_ & 1) == 1; // counter is odd
1522 }
1523
1524 void AdvanceDouble(uint64_t val) {
1525 if (kNativeSoftFloatAbi) {
1526 AdvanceLong(val);
1527 } else {
1528 if (HaveDoubleFpr()) {
1529 if (DoubleFprNeedsPadding()) {
1530 PushFpr4(0);
1531 fpr_index_--;
1532 }
1533 PushFpr8(val);
1534 fpr_index_ -= kRegistersNeededForDouble;
1535 } else {
1536 if (DoubleStackNeedsPadding()) {
1537 PushStack(0);
1538 stack_entries_++;
1539 }
1540 if (kRegistersNeededForDouble == 1) {
1541 PushStack(static_cast<uintptr_t>(val));
1542 stack_entries_++;
1543 } else {
1544 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1545 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1546 stack_entries_ += 2;
1547 }
1548 fpr_index_ = 0;
1549 }
1550 }
1551 }
1552
Ian Rogers1428dce2014-10-21 15:02:15 -07001553 uint32_t GetStackEntries() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001554 return stack_entries_;
1555 }
1556
Ian Rogers1428dce2014-10-21 15:02:15 -07001557 uint32_t GetNumberOfUsedGprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001558 return kNumNativeGprArgs - gpr_index_;
1559 }
1560
Ian Rogers1428dce2014-10-21 15:02:15 -07001561 uint32_t GetNumberOfUsedFprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001562 return kNumNativeFprArgs - fpr_index_;
1563 }
1564
1565 private:
1566 void PushGpr(uintptr_t val) {
1567 delegate_->PushGpr(val);
1568 }
1569 void PushFpr4(float val) {
1570 delegate_->PushFpr4(val);
1571 }
1572 void PushFpr8(uint64_t val) {
1573 delegate_->PushFpr8(val);
1574 }
1575 void PushStack(uintptr_t val) {
1576 delegate_->PushStack(val);
1577 }
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001578 uintptr_t PushHandle(mirror::Object* ref) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001579 return delegate_->PushHandle(ref);
Andreas Gampec147b002014-03-06 18:11:06 -08001580 }
1581
1582 uint32_t gpr_index_; // Number of free GPRs
1583 uint32_t fpr_index_; // Number of free FPRs
1584 uint32_t stack_entries_; // Stack entries are in multiples of 32b, as floats are usually not
1585 // extended
Ian Rogers1428dce2014-10-21 15:02:15 -07001586 T* const delegate_; // What Push implementation gets called
Andreas Gampec147b002014-03-06 18:11:06 -08001587};
1588
Andreas Gampec200a4a2014-06-16 18:39:09 -07001589// Computes the sizes of register stacks and call stack area. Handling of references can be extended
1590// in subclasses.
1591//
1592// To handle native pointers, use "L" in the shorty for an object reference, which simulates
1593// them with handles.
1594class ComputeNativeCallFrameSize {
Andreas Gampec147b002014-03-06 18:11:06 -08001595 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001596 ComputeNativeCallFrameSize() : num_stack_entries_(0) {}
1597
1598 virtual ~ComputeNativeCallFrameSize() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001599
Ian Rogers1428dce2014-10-21 15:02:15 -07001600 uint32_t GetStackSize() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001601 return num_stack_entries_ * sizeof(uintptr_t);
1602 }
1603
Ian Rogers1428dce2014-10-21 15:02:15 -07001604 uint8_t* LayoutCallStack(uint8_t* sp8) const {
Andreas Gampec147b002014-03-06 18:11:06 -08001605 sp8 -= GetStackSize();
Andreas Gampe779f8c92014-06-09 18:29:38 -07001606 // Align by kStackAlignment.
1607 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
Andreas Gampec200a4a2014-06-16 18:39:09 -07001608 return sp8;
Andreas Gampec147b002014-03-06 18:11:06 -08001609 }
1610
Ian Rogers1428dce2014-10-21 15:02:15 -07001611 uint8_t* LayoutCallRegisterStacks(uint8_t* sp8, uintptr_t** start_gpr, uint32_t** start_fpr)
1612 const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001613 // Assumption is OK right now, as we have soft-float arm
1614 size_t fregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeFprArgs;
1615 sp8 -= fregs * sizeof(uintptr_t);
1616 *start_fpr = reinterpret_cast<uint32_t*>(sp8);
1617 size_t iregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeGprArgs;
1618 sp8 -= iregs * sizeof(uintptr_t);
1619 *start_gpr = reinterpret_cast<uintptr_t*>(sp8);
1620 return sp8;
1621 }
Andreas Gampec147b002014-03-06 18:11:06 -08001622
Andreas Gampec200a4a2014-06-16 18:39:09 -07001623 uint8_t* LayoutNativeCall(uint8_t* sp8, uintptr_t** start_stack, uintptr_t** start_gpr,
Ian Rogers1428dce2014-10-21 15:02:15 -07001624 uint32_t** start_fpr) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001625 // Native call stack.
1626 sp8 = LayoutCallStack(sp8);
1627 *start_stack = reinterpret_cast<uintptr_t*>(sp8);
Andreas Gampec147b002014-03-06 18:11:06 -08001628
Andreas Gampec200a4a2014-06-16 18:39:09 -07001629 // Put fprs and gprs below.
1630 sp8 = LayoutCallRegisterStacks(sp8, start_gpr, start_fpr);
Andreas Gampec147b002014-03-06 18:11:06 -08001631
Andreas Gampec200a4a2014-06-16 18:39:09 -07001632 // Return the new bottom.
1633 return sp8;
1634 }
1635
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001636 virtual void WalkHeader(
1637 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm ATTRIBUTE_UNUSED)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001638 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001639 }
Andreas Gampec200a4a2014-06-16 18:39:09 -07001640
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001641 void Walk(const char* shorty, uint32_t shorty_len) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001642 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize> sm(this);
1643
1644 WalkHeader(&sm);
Andreas Gampec147b002014-03-06 18:11:06 -08001645
1646 for (uint32_t i = 1; i < shorty_len; ++i) {
1647 Primitive::Type cur_type_ = Primitive::GetType(shorty[i]);
1648 switch (cur_type_) {
1649 case Primitive::kPrimNot:
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001650 // TODO: fix abuse of mirror types.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001651 sm.AdvanceHandleScope(
1652 reinterpret_cast<mirror::Object*>(0x12345678));
Andreas Gampec147b002014-03-06 18:11:06 -08001653 break;
1654
1655 case Primitive::kPrimBoolean:
1656 case Primitive::kPrimByte:
1657 case Primitive::kPrimChar:
1658 case Primitive::kPrimShort:
1659 case Primitive::kPrimInt:
1660 sm.AdvanceInt(0);
1661 break;
1662 case Primitive::kPrimFloat:
1663 sm.AdvanceFloat(0);
1664 break;
1665 case Primitive::kPrimDouble:
1666 sm.AdvanceDouble(0);
1667 break;
1668 case Primitive::kPrimLong:
1669 sm.AdvanceLong(0);
1670 break;
1671 default:
1672 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001673 UNREACHABLE();
Andreas Gampec147b002014-03-06 18:11:06 -08001674 }
1675 }
1676
Ian Rogers1428dce2014-10-21 15:02:15 -07001677 num_stack_entries_ = sm.GetStackEntries();
Andreas Gampec147b002014-03-06 18:11:06 -08001678 }
1679
1680 void PushGpr(uintptr_t /* val */) {
1681 // not optimizing registers, yet
1682 }
1683
1684 void PushFpr4(float /* val */) {
1685 // not optimizing registers, yet
1686 }
1687
1688 void PushFpr8(uint64_t /* val */) {
1689 // not optimizing registers, yet
1690 }
1691
1692 void PushStack(uintptr_t /* val */) {
1693 // counting is already done in the superclass
1694 }
1695
Andreas Gampec200a4a2014-06-16 18:39:09 -07001696 virtual uintptr_t PushHandle(mirror::Object* /* ptr */) {
Andreas Gampec147b002014-03-06 18:11:06 -08001697 return reinterpret_cast<uintptr_t>(nullptr);
1698 }
1699
Andreas Gampec200a4a2014-06-16 18:39:09 -07001700 protected:
Andreas Gampec147b002014-03-06 18:11:06 -08001701 uint32_t num_stack_entries_;
1702};
1703
Andreas Gampec200a4a2014-06-16 18:39:09 -07001704class ComputeGenericJniFrameSize FINAL : public ComputeNativeCallFrameSize {
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001705 public:
Igor Murashkin06a04e02016-09-13 15:57:37 -07001706 explicit ComputeGenericJniFrameSize(bool critical_native)
1707 : num_handle_scope_references_(0), critical_native_(critical_native) {}
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001708
Andreas Gampec200a4a2014-06-16 18:39:09 -07001709 // Lays out the callee-save frame. Assumes that the incorrect frame corresponding to RefsAndArgs
1710 // is at *m = sp. Will update to point to the bottom of the save frame.
1711 //
1712 // Note: assumes ComputeAll() has been run before.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001713 void LayoutCalleeSaveFrame(Thread* self, ArtMethod*** m, void* sp, HandleScope** handle_scope)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001714 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001715 ArtMethod* method = **m;
1716
Andreas Gampe542451c2016-07-26 09:02:02 -07001717 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001718
Andreas Gampec200a4a2014-06-16 18:39:09 -07001719 uint8_t* sp8 = reinterpret_cast<uint8_t*>(sp);
1720
1721 // First, fix up the layout of the callee-save frame.
1722 // We have to squeeze in the HandleScope, and relocate the method pointer.
1723
1724 // "Free" the slot for the method.
Ian Rogers13735952014-10-08 12:43:28 -07001725 sp8 += sizeof(void*); // In the callee-save frame we use a full pointer.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001726
1727 // Under the callee saves put handle scope and new method stack reference.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001728 size_t handle_scope_size = HandleScope::SizeOf(num_handle_scope_references_);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001729 size_t scope_and_method = handle_scope_size + sizeof(ArtMethod*);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001730
1731 sp8 -= scope_and_method;
1732 // Align by kStackAlignment.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001733 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
Andreas Gampec200a4a2014-06-16 18:39:09 -07001734
Mathieu Chartiere401d142015-04-22 13:56:20 -07001735 uint8_t* sp8_table = sp8 + sizeof(ArtMethod*);
Ian Rogers59c07062014-10-10 13:03:39 -07001736 *handle_scope = HandleScope::Create(sp8_table, self->GetTopHandleScope(),
1737 num_handle_scope_references_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001738
1739 // Add a slot for the method pointer, and fill it. Fix the pointer-pointer given to us.
1740 uint8_t* method_pointer = sp8;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001741 auto** new_method_ref = reinterpret_cast<ArtMethod**>(method_pointer);
1742 *new_method_ref = method;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001743 *m = new_method_ref;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001744 }
1745
Andreas Gampec200a4a2014-06-16 18:39:09 -07001746 // Adds space for the cookie. Note: may leave stack unaligned.
Ian Rogers1428dce2014-10-21 15:02:15 -07001747 void LayoutCookie(uint8_t** sp) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001748 // Reference cookie and padding
1749 *sp -= 8;
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001750 }
1751
Andreas Gampec200a4a2014-06-16 18:39:09 -07001752 // Re-layout the callee-save frame (insert a handle-scope). Then add space for the cookie.
1753 // Returns the new bottom. Note: this may be unaligned.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001754 uint8_t* LayoutJNISaveFrame(Thread* self, ArtMethod*** m, void* sp, HandleScope** handle_scope)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001755 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001756 // First, fix up the layout of the callee-save frame.
1757 // We have to squeeze in the HandleScope, and relocate the method pointer.
Ian Rogers59c07062014-10-10 13:03:39 -07001758 LayoutCalleeSaveFrame(self, m, sp, handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001759
1760 // The bottom of the callee-save frame is now where the method is, *m.
1761 uint8_t* sp8 = reinterpret_cast<uint8_t*>(*m);
1762
1763 // Add space for cookie.
1764 LayoutCookie(&sp8);
1765
1766 return sp8;
1767 }
1768
1769 // WARNING: After this, *sp won't be pointing to the method anymore!
Mathieu Chartiere401d142015-04-22 13:56:20 -07001770 uint8_t* ComputeLayout(Thread* self, ArtMethod*** m, const char* shorty, uint32_t shorty_len,
1771 HandleScope** handle_scope, uintptr_t** start_stack, uintptr_t** start_gpr,
1772 uint32_t** start_fpr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001773 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001774 Walk(shorty, shorty_len);
1775
1776 // JNI part.
Ian Rogers59c07062014-10-10 13:03:39 -07001777 uint8_t* sp8 = LayoutJNISaveFrame(self, m, reinterpret_cast<void*>(*m), handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001778
1779 sp8 = LayoutNativeCall(sp8, start_stack, start_gpr, start_fpr);
1780
1781 // Return the new bottom.
1782 return sp8;
1783 }
1784
1785 uintptr_t PushHandle(mirror::Object* /* ptr */) OVERRIDE;
1786
1787 // Add JNIEnv* and jobj/jclass before the shorty-derived elements.
1788 void WalkHeader(BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) OVERRIDE
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001789 REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001790
1791 private:
1792 uint32_t num_handle_scope_references_;
Igor Murashkin06a04e02016-09-13 15:57:37 -07001793 const bool critical_native_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001794};
1795
1796uintptr_t ComputeGenericJniFrameSize::PushHandle(mirror::Object* /* ptr */) {
1797 num_handle_scope_references_++;
1798 return reinterpret_cast<uintptr_t>(nullptr);
1799}
1800
1801void ComputeGenericJniFrameSize::WalkHeader(
1802 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) {
Igor Murashkin06a04e02016-09-13 15:57:37 -07001803 // First 2 parameters are always excluded for @CriticalNative.
1804 if (UNLIKELY(critical_native_)) {
1805 return;
1806 }
1807
Andreas Gampec200a4a2014-06-16 18:39:09 -07001808 // JNIEnv
1809 sm->AdvancePointer(nullptr);
1810
1811 // Class object or this as first argument
1812 sm->AdvanceHandleScope(reinterpret_cast<mirror::Object*>(0x12345678));
1813}
1814
1815// Class to push values to three separate regions. Used to fill the native call part. Adheres to
1816// the template requirements of BuildGenericJniFrameStateMachine.
1817class FillNativeCall {
1818 public:
1819 FillNativeCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) :
1820 cur_gpr_reg_(gpr_regs), cur_fpr_reg_(fpr_regs), cur_stack_arg_(stack_args) {}
1821
1822 virtual ~FillNativeCall() {}
1823
1824 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) {
1825 cur_gpr_reg_ = gpr_regs;
1826 cur_fpr_reg_ = fpr_regs;
1827 cur_stack_arg_ = stack_args;
Andreas Gampec147b002014-03-06 18:11:06 -08001828 }
1829
1830 void PushGpr(uintptr_t val) {
1831 *cur_gpr_reg_ = val;
1832 cur_gpr_reg_++;
1833 }
1834
1835 void PushFpr4(float val) {
1836 *cur_fpr_reg_ = val;
1837 cur_fpr_reg_++;
1838 }
1839
1840 void PushFpr8(uint64_t val) {
1841 uint64_t* tmp = reinterpret_cast<uint64_t*>(cur_fpr_reg_);
1842 *tmp = val;
1843 cur_fpr_reg_ += 2;
1844 }
1845
1846 void PushStack(uintptr_t val) {
1847 *cur_stack_arg_ = val;
1848 cur_stack_arg_++;
1849 }
1850
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001851 virtual uintptr_t PushHandle(mirror::Object*) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001852 LOG(FATAL) << "(Non-JNI) Native call does not use handles.";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001853 UNREACHABLE();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001854 }
1855
1856 private:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001857 uintptr_t* cur_gpr_reg_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001858 uint32_t* cur_fpr_reg_;
1859 uintptr_t* cur_stack_arg_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001860};
Andreas Gampec147b002014-03-06 18:11:06 -08001861
Andreas Gampec200a4a2014-06-16 18:39:09 -07001862// Visits arguments on the stack placing them into a region lower down the stack for the benefit
1863// of transitioning into native code.
1864class BuildGenericJniFrameVisitor FINAL : public QuickArgumentVisitor {
1865 public:
Igor Murashkin06a04e02016-09-13 15:57:37 -07001866 BuildGenericJniFrameVisitor(Thread* self,
1867 bool is_static,
1868 bool critical_native,
1869 const char* shorty,
1870 uint32_t shorty_len,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001871 ArtMethod*** sp)
Andreas Gampec200a4a2014-06-16 18:39:09 -07001872 : QuickArgumentVisitor(*sp, is_static, shorty, shorty_len),
Igor Murashkin06a04e02016-09-13 15:57:37 -07001873 jni_call_(nullptr, nullptr, nullptr, nullptr, critical_native),
1874 sm_(&jni_call_) {
1875 ComputeGenericJniFrameSize fsc(critical_native);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001876 uintptr_t* start_gpr_reg;
1877 uint32_t* start_fpr_reg;
1878 uintptr_t* start_stack_arg;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001879 bottom_of_used_area_ = fsc.ComputeLayout(self, sp, shorty, shorty_len,
Ian Rogers59c07062014-10-10 13:03:39 -07001880 &handle_scope_,
1881 &start_stack_arg,
Andreas Gampec200a4a2014-06-16 18:39:09 -07001882 &start_gpr_reg, &start_fpr_reg);
1883
Andreas Gampec200a4a2014-06-16 18:39:09 -07001884 jni_call_.Reset(start_gpr_reg, start_fpr_reg, start_stack_arg, handle_scope_);
1885
Igor Murashkin06a04e02016-09-13 15:57:37 -07001886 // First 2 parameters are always excluded for CriticalNative methods.
1887 if (LIKELY(!critical_native)) {
1888 // jni environment is always first argument
1889 sm_.AdvancePointer(self->GetJniEnv());
Andreas Gampec200a4a2014-06-16 18:39:09 -07001890
Igor Murashkin06a04e02016-09-13 15:57:37 -07001891 if (is_static) {
1892 sm_.AdvanceHandleScope((**sp)->GetDeclaringClass());
1893 } // else "this" reference is already handled by QuickArgumentVisitor.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001894 }
1895 }
1896
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001897 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001898
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001899 void FinalizeHandleScope(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001900
Vladimir Markof39745e2016-01-26 12:16:55 +00001901 StackReference<mirror::Object>* GetFirstHandleScopeEntry() {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001902 return handle_scope_->GetHandle(0).GetReference();
1903 }
1904
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001905 jobject GetFirstHandleScopeJObject() const REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001906 return handle_scope_->GetHandle(0).ToJObject();
1907 }
1908
Ian Rogers1428dce2014-10-21 15:02:15 -07001909 void* GetBottomOfUsedArea() const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001910 return bottom_of_used_area_;
1911 }
1912
1913 private:
1914 // A class to fill a JNI call. Adds reference/handle-scope management to FillNativeCall.
1915 class FillJniCall FINAL : public FillNativeCall {
1916 public:
1917 FillJniCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args,
Igor Murashkin06a04e02016-09-13 15:57:37 -07001918 HandleScope* handle_scope, bool critical_native)
1919 : FillNativeCall(gpr_regs, fpr_regs, stack_args),
1920 handle_scope_(handle_scope),
1921 cur_entry_(0),
1922 critical_native_(critical_native) {}
Andreas Gampec200a4a2014-06-16 18:39:09 -07001923
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001924 uintptr_t PushHandle(mirror::Object* ref) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001925
1926 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args, HandleScope* scope) {
1927 FillNativeCall::Reset(gpr_regs, fpr_regs, stack_args);
1928 handle_scope_ = scope;
1929 cur_entry_ = 0U;
1930 }
1931
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001932 void ResetRemainingScopeSlots() REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001933 // Initialize padding entries.
1934 size_t expected_slots = handle_scope_->NumberOfReferences();
1935 while (cur_entry_ < expected_slots) {
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07001936 handle_scope_->GetMutableHandle(cur_entry_++).Assign(nullptr);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001937 }
Igor Murashkin06a04e02016-09-13 15:57:37 -07001938
1939 if (!critical_native_) {
1940 // Non-critical natives have at least the self class (jclass) or this (jobject).
1941 DCHECK_NE(cur_entry_, 0U);
1942 }
Andreas Gampec200a4a2014-06-16 18:39:09 -07001943 }
1944
Mathieu Chartier1432a5b2016-10-04 15:41:42 -07001945 bool CriticalNative() const {
1946 return critical_native_;
1947 }
1948
Andreas Gampec200a4a2014-06-16 18:39:09 -07001949 private:
1950 HandleScope* handle_scope_;
1951 size_t cur_entry_;
Igor Murashkin06a04e02016-09-13 15:57:37 -07001952 const bool critical_native_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001953 };
1954
1955 HandleScope* handle_scope_;
1956 FillJniCall jni_call_;
1957 void* bottom_of_used_area_;
1958
1959 BuildNativeCallFrameStateMachine<FillJniCall> sm_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001960
1961 DISALLOW_COPY_AND_ASSIGN(BuildGenericJniFrameVisitor);
1962};
1963
Andreas Gampec200a4a2014-06-16 18:39:09 -07001964uintptr_t BuildGenericJniFrameVisitor::FillJniCall::PushHandle(mirror::Object* ref) {
1965 uintptr_t tmp;
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07001966 MutableHandle<mirror::Object> h = handle_scope_->GetMutableHandle(cur_entry_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001967 h.Assign(ref);
1968 tmp = reinterpret_cast<uintptr_t>(h.ToJObject());
1969 cur_entry_++;
1970 return tmp;
1971}
1972
Ian Rogers9758f792014-03-13 09:02:55 -07001973void BuildGenericJniFrameVisitor::Visit() {
1974 Primitive::Type type = GetParamPrimitiveType();
1975 switch (type) {
1976 case Primitive::kPrimLong: {
1977 jlong long_arg;
1978 if (IsSplitLongOrDouble()) {
1979 long_arg = ReadSplitLongParam();
1980 } else {
1981 long_arg = *reinterpret_cast<jlong*>(GetParamAddress());
1982 }
1983 sm_.AdvanceLong(long_arg);
1984 break;
1985 }
1986 case Primitive::kPrimDouble: {
1987 uint64_t double_arg;
1988 if (IsSplitLongOrDouble()) {
1989 // Read into union so that we don't case to a double.
1990 double_arg = ReadSplitLongParam();
1991 } else {
1992 double_arg = *reinterpret_cast<uint64_t*>(GetParamAddress());
1993 }
1994 sm_.AdvanceDouble(double_arg);
1995 break;
1996 }
1997 case Primitive::kPrimNot: {
1998 StackReference<mirror::Object>* stack_ref =
1999 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002000 sm_.AdvanceHandleScope(stack_ref->AsMirrorPtr());
Ian Rogers9758f792014-03-13 09:02:55 -07002001 break;
2002 }
2003 case Primitive::kPrimFloat:
2004 sm_.AdvanceFloat(*reinterpret_cast<float*>(GetParamAddress()));
2005 break;
2006 case Primitive::kPrimBoolean: // Fall-through.
2007 case Primitive::kPrimByte: // Fall-through.
2008 case Primitive::kPrimChar: // Fall-through.
2009 case Primitive::kPrimShort: // Fall-through.
2010 case Primitive::kPrimInt: // Fall-through.
2011 sm_.AdvanceInt(*reinterpret_cast<jint*>(GetParamAddress()));
2012 break;
2013 case Primitive::kPrimVoid:
2014 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -07002015 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -07002016 }
2017}
2018
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002019void BuildGenericJniFrameVisitor::FinalizeHandleScope(Thread* self) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002020 // Clear out rest of the scope.
2021 jni_call_.ResetRemainingScopeSlots();
Mathieu Chartier1432a5b2016-10-04 15:41:42 -07002022 if (!jni_call_.CriticalNative()) {
2023 // Install HandleScope.
2024 self->PushHandleScope(handle_scope_);
2025 }
Ian Rogers9758f792014-03-13 09:02:55 -07002026}
2027
Ian Rogers04c31d22014-07-07 21:44:06 -07002028#if defined(__arm__) || defined(__aarch64__)
Alex Lightd78ddec2017-04-18 15:20:38 -07002029extern "C" const void* artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07002030#else
Alex Lightd78ddec2017-04-18 15:20:38 -07002031extern "C" const void* artFindNativeMethod(Thread* self);
Ian Rogers04c31d22014-07-07 21:44:06 -07002032#endif
Andreas Gampe90546832014-03-12 18:07:19 -07002033
Igor Murashkin06a04e02016-09-13 15:57:37 -07002034static uint64_t artQuickGenericJniEndJNIRef(Thread* self,
2035 uint32_t cookie,
2036 bool fast_native ATTRIBUTE_UNUSED,
2037 jobject l,
2038 jobject lock) {
2039 // TODO: add entrypoints for @FastNative returning objects.
Andreas Gampead615172014-04-04 16:20:13 -07002040 if (lock != nullptr) {
2041 return reinterpret_cast<uint64_t>(JniMethodEndWithReferenceSynchronized(l, cookie, lock, self));
2042 } else {
2043 return reinterpret_cast<uint64_t>(JniMethodEndWithReference(l, cookie, self));
2044 }
2045}
2046
Igor Murashkin06a04e02016-09-13 15:57:37 -07002047static void artQuickGenericJniEndJNINonRef(Thread* self,
2048 uint32_t cookie,
2049 bool fast_native,
2050 jobject lock) {
Andreas Gampead615172014-04-04 16:20:13 -07002051 if (lock != nullptr) {
2052 JniMethodEndSynchronized(cookie, lock, self);
Igor Murashkin06a04e02016-09-13 15:57:37 -07002053 // Ignore "fast_native" here because synchronized functions aren't very fast.
Andreas Gampead615172014-04-04 16:20:13 -07002054 } else {
Igor Murashkin06a04e02016-09-13 15:57:37 -07002055 if (UNLIKELY(fast_native)) {
2056 JniMethodFastEnd(cookie, self);
2057 } else {
2058 JniMethodEnd(cookie, self);
2059 }
Andreas Gampead615172014-04-04 16:20:13 -07002060 }
2061}
2062
Andreas Gampec147b002014-03-06 18:11:06 -08002063/*
2064 * Initializes an alloca region assumed to be directly below sp for a native call:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002065 * 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 -08002066 * The final element on the stack is a pointer to the native code.
2067 *
Andreas Gampe36fea8d2014-03-10 13:37:40 -07002068 * On entry, the stack has a standard callee-save frame above sp, and an alloca below it.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002069 * We need to fix this, as the handle scope needs to go into the callee-save frame.
Andreas Gampe36fea8d2014-03-10 13:37:40 -07002070 *
Andreas Gampec147b002014-03-06 18:11:06 -08002071 * The return of this function denotes:
2072 * 1) How many bytes of the alloca can be released, if the value is non-negative.
2073 * 2) An error, if the value is negative.
2074 */
Mathieu Chartiere401d142015-04-22 13:56:20 -07002075extern "C" TwoWordReturn artQuickGenericJniTrampoline(Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002076 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002077 ArtMethod* called = *sp;
David Sehr709b0702016-10-13 09:12:37 -07002078 DCHECK(called->IsNative()) << called->PrettyMethod(true);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002079 uint32_t shorty_len = 0;
2080 const char* shorty = called->GetShorty(&shorty_len);
Igor Murashkin06a04e02016-09-13 15:57:37 -07002081 bool critical_native = called->IsAnnotatedWithCriticalNative();
2082 bool fast_native = called->IsAnnotatedWithFastNative();
2083 bool normal_native = !critical_native && !fast_native;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002084
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002085 // Run the visitor and update sp.
Igor Murashkin06a04e02016-09-13 15:57:37 -07002086 BuildGenericJniFrameVisitor visitor(self,
2087 called->IsStatic(),
2088 critical_native,
2089 shorty,
2090 shorty_len,
2091 &sp);
Mathieu Chartierbe08cf52016-09-13 13:41:24 -07002092 {
2093 ScopedAssertNoThreadSuspension sants(__FUNCTION__);
2094 visitor.VisitArguments();
2095 // FinalizeHandleScope pushes the handle scope on the thread.
2096 visitor.FinalizeHandleScope(self);
2097 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002098
Andreas Gampec200a4a2014-06-16 18:39:09 -07002099 // Fix up managed-stack things in Thread.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002100 self->SetTopOfStack(sp);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002101
Ian Rogerse0dcd462014-03-08 15:21:04 -08002102 self->VerifyStack();
2103
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002104 uint32_t cookie;
Igor Murashkin06a04e02016-09-13 15:57:37 -07002105 uint32_t* sp32;
2106 // Skip calling JniMethodStart for @CriticalNative.
2107 if (LIKELY(!critical_native)) {
2108 // Start JNI, save the cookie.
2109 if (called->IsSynchronized()) {
2110 DCHECK(normal_native) << " @FastNative and synchronize is not supported";
2111 cookie = JniMethodStartSynchronized(visitor.GetFirstHandleScopeJObject(), self);
2112 if (self->IsExceptionPending()) {
2113 self->PopHandleScope();
2114 // A negative value denotes an error.
2115 return GetTwoWordFailureValue();
2116 }
2117 } else {
2118 if (fast_native) {
2119 cookie = JniMethodFastStart(self);
2120 } else {
2121 DCHECK(normal_native);
2122 cookie = JniMethodStart(self);
2123 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002124 }
Igor Murashkin06a04e02016-09-13 15:57:37 -07002125 sp32 = reinterpret_cast<uint32_t*>(sp);
2126 *(sp32 - 1) = cookie;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002127 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002128
Andreas Gampe90546832014-03-12 18:07:19 -07002129 // Retrieve the stored native code.
Alex Lightd78ddec2017-04-18 15:20:38 -07002130 void const* nativeCode = called->GetEntryPointFromJni();
Andreas Gampe90546832014-03-12 18:07:19 -07002131
Andreas Gampe9a6a99a2014-03-14 07:52:20 -07002132 // There are two cases for the content of nativeCode:
2133 // 1) Pointer to the native function.
2134 // 2) Pointer to the trampoline for native code binding.
2135 // In the second case, we need to execute the binding and continue with the actual native function
2136 // pointer.
Andreas Gampe90546832014-03-12 18:07:19 -07002137 DCHECK(nativeCode != nullptr);
2138 if (nativeCode == GetJniDlsymLookupStub()) {
Ian Rogers04c31d22014-07-07 21:44:06 -07002139#if defined(__arm__) || defined(__aarch64__)
Andreas Gampe90546832014-03-12 18:07:19 -07002140 nativeCode = artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07002141#else
2142 nativeCode = artFindNativeMethod(self);
2143#endif
Andreas Gampe90546832014-03-12 18:07:19 -07002144
2145 if (nativeCode == nullptr) {
2146 DCHECK(self->IsExceptionPending()); // There should be an exception pending now.
Andreas Gampead615172014-04-04 16:20:13 -07002147
Igor Murashkin06a04e02016-09-13 15:57:37 -07002148 // @CriticalNative calls do not need to call back into JniMethodEnd.
2149 if (LIKELY(!critical_native)) {
2150 // End JNI, as the assembly will move to deliver the exception.
2151 jobject lock = called->IsSynchronized() ? visitor.GetFirstHandleScopeJObject() : nullptr;
2152 if (shorty[0] == 'L') {
2153 artQuickGenericJniEndJNIRef(self, cookie, fast_native, nullptr, lock);
2154 } else {
2155 artQuickGenericJniEndJNINonRef(self, cookie, fast_native, lock);
2156 }
Andreas Gampead615172014-04-04 16:20:13 -07002157 }
2158
Andreas Gampec200a4a2014-06-16 18:39:09 -07002159 return GetTwoWordFailureValue();
Andreas Gampe90546832014-03-12 18:07:19 -07002160 }
2161 // Note that the native code pointer will be automatically set by artFindNativeMethod().
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002162 }
2163
Alexey Frunze1b8464d2016-11-12 17:22:05 -08002164#if defined(__mips__) && !defined(__LP64__)
2165 // On MIPS32 if the first two arguments are floating-point, we need to know their types
2166 // so that art_quick_generic_jni_trampoline can correctly extract them from the stack
2167 // and load into floating-point registers.
2168 // Possible arrangements of first two floating-point arguments on the stack (32-bit FPU
2169 // view):
2170 // (1)
2171 // | DOUBLE | DOUBLE | other args, if any
2172 // | F12 | F13 | F14 | F15 |
2173 // | SP+0 | SP+4 | SP+8 | SP+12 | SP+16
2174 // (2)
2175 // | DOUBLE | FLOAT | (PAD) | other args, if any
2176 // | F12 | F13 | F14 | |
2177 // | SP+0 | SP+4 | SP+8 | SP+12 | SP+16
2178 // (3)
2179 // | FLOAT | (PAD) | DOUBLE | other args, if any
2180 // | F12 | | F14 | F15 |
2181 // | SP+0 | SP+4 | SP+8 | SP+12 | SP+16
2182 // (4)
2183 // | FLOAT | FLOAT | other args, if any
2184 // | F12 | F14 |
2185 // | SP+0 | SP+4 | SP+8
2186 // As you can see, only the last case (4) is special. In all others we can just
2187 // load F12/F13 and F14/F15 in the same manner.
2188 // Set bit 0 of the native code address to 1 in this case (valid code addresses
2189 // are always a multiple of 4 on MIPS32, so we have 2 spare bits available).
2190 if (nativeCode != nullptr &&
2191 shorty != nullptr &&
2192 shorty_len >= 3 &&
2193 shorty[1] == 'F' &&
2194 shorty[2] == 'F') {
2195 nativeCode = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(nativeCode) | 1);
2196 }
2197#endif
2198
Andreas Gampec200a4a2014-06-16 18:39:09 -07002199 // Return native code addr(lo) and bottom of alloca address(hi).
2200 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(visitor.GetBottomOfUsedArea()),
2201 reinterpret_cast<uintptr_t>(nativeCode));
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002202}
2203
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002204// Defined in quick_jni_entrypoints.cc.
2205extern uint64_t GenericJniMethodEnd(Thread* self, uint32_t saved_local_ref_cookie,
2206 jvalue result, uint64_t result_f, ArtMethod* called,
2207 HandleScope* handle_scope);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002208/*
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002209 * Is called after the native JNI code. Responsible for cleanup (handle scope, saved state) and
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002210 * unlocking.
2211 */
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002212extern "C" uint64_t artQuickGenericJniEndTrampoline(Thread* self,
2213 jvalue result,
2214 uint64_t result_f) {
2215 // We're here just back from a native call. We don't have the shared mutator lock at this point
2216 // yet until we call GoToRunnable() later in GenericJniMethodEnd(). Accessing objects or doing
2217 // anything that requires a mutator lock before that would cause problems as GC may have the
2218 // exclusive mutator lock and may be moving objects, etc.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002219 ArtMethod** sp = self->GetManagedStack()->GetTopQuickFrame();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002220 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002221 ArtMethod* called = *sp;
Ian Rogerse0dcd462014-03-08 15:21:04 -08002222 uint32_t cookie = *(sp32 - 1);
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002223 HandleScope* table = reinterpret_cast<HandleScope*>(reinterpret_cast<uint8_t*>(sp) + sizeof(*sp));
2224 return GenericJniMethodEnd(self, cookie, result, result_f, called, table);
Andreas Gampe2da88232014-02-27 12:26:20 -08002225}
2226
Andreas Gamped58342c2014-06-05 14:18:08 -07002227// We use TwoWordReturn to optimize scalar returns. We use the hi value for code, and the lo value
2228// for the method pointer.
Andreas Gampe51f76352014-05-21 08:28:48 -07002229//
Andreas Gamped58342c2014-06-05 14:18:08 -07002230// 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 -07002231// to hold the mutator lock (see REQUIRES_SHARED(Locks::mutator_lock_) annotations).
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002232
2233template<InvokeType type, bool access_check>
Mathieu Chartieref41db72016-10-25 15:08:01 -07002234static TwoWordReturn artInvokeCommon(uint32_t method_idx,
2235 ObjPtr<mirror::Object> this_object,
2236 Thread* self,
Mathieu Chartiere401d142015-04-22 13:56:20 -07002237 ArtMethod** sp) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002238 ScopedQuickEntrypointChecks sqec(self);
Vladimir Markofd36f1f2016-08-03 18:49:58 +01002239 DCHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(Runtime::kSaveRefsAndArgs));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002240 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
2241 ArtMethod* method = FindMethodFast(method_idx, this_object, caller_method, access_check, type);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002242 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002243 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()->GetDexFile();
2244 uint32_t shorty_len;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002245 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(method_idx), &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002246 {
2247 // Remember the args in case a GC happens in FindMethodFromCode.
2248 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
2249 RememberForGcArgumentVisitor visitor(sp, type == kStatic, shorty, shorty_len, &soa);
2250 visitor.VisitArguments();
Mathieu Chartieref41db72016-10-25 15:08:01 -07002251 method = FindMethodFromCode<type, access_check>(method_idx,
2252 &this_object,
2253 caller_method,
Mathieu Chartier0cd81352014-05-22 16:48:55 -07002254 self);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002255 visitor.FixupReferences();
2256 }
2257
Ian Rogerse0a02da2014-12-02 14:10:53 -08002258 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002259 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07002260 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002261 }
2262 }
2263 DCHECK(!self->IsExceptionPending());
2264 const void* code = method->GetEntryPointFromQuickCompiledCode();
2265
2266 // When we return, the caller will branch to this address, so it had better not be 0!
David Sehr709b0702016-10-13 09:12:37 -07002267 DCHECK(code != nullptr) << "Code was null in method: " << method->PrettyMethod()
Andreas Gampec200a4a2014-06-16 18:39:09 -07002268 << " location: "
2269 << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07002270
Andreas Gamped58342c2014-06-05 14:18:08 -07002271 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
2272 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002273}
2274
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01002275// Explicit artInvokeCommon template function declarations to please analysis tool.
2276#define EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(type, access_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002277 template REQUIRES_SHARED(Locks::mutator_lock_) \
Mathieu Chartiere401d142015-04-22 13:56:20 -07002278 TwoWordReturn artInvokeCommon<type, access_check>( \
Mathieu Chartieref41db72016-10-25 15:08:01 -07002279 uint32_t method_idx, ObjPtr<mirror::Object> his_object, Thread* self, ArtMethod** sp)
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01002280
2281EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, false);
2282EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, true);
2283EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, false);
2284EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, true);
2285EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, false);
2286EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, true);
2287EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, false);
2288EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, true);
2289EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, false);
2290EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, true);
2291#undef EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL
2292
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002293// See comments in runtime_support_asm.S
Andreas Gampec200a4a2014-06-16 18:39:09 -07002294extern "C" TwoWordReturn artInvokeInterfaceTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002295 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002296 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002297 return artInvokeCommon<kInterface, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002298}
2299
Andreas Gampec200a4a2014-06-16 18:39:09 -07002300extern "C" TwoWordReturn artInvokeDirectTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002301 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002302 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002303 return artInvokeCommon<kDirect, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002304}
2305
Andreas Gampec200a4a2014-06-16 18:39:09 -07002306extern "C" TwoWordReturn artInvokeStaticTrampolineWithAccessCheck(
Mathieu Chartieref41db72016-10-25 15:08:01 -07002307 uint32_t method_idx,
2308 mirror::Object* this_object ATTRIBUTE_UNUSED,
2309 Thread* self,
2310 ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
2311 // For static, this_object is not required and may be random garbage. Don't pass it down so that
2312 // it doesn't cause ObjPtr alignment failure check.
2313 return artInvokeCommon<kStatic, true>(method_idx, nullptr, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002314}
2315
Andreas Gampec200a4a2014-06-16 18:39:09 -07002316extern "C" TwoWordReturn artInvokeSuperTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002317 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002318 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002319 return artInvokeCommon<kSuper, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002320}
2321
Andreas Gampec200a4a2014-06-16 18:39:09 -07002322extern "C" TwoWordReturn artInvokeVirtualTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002323 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002324 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002325 return artInvokeCommon<kVirtual, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002326}
2327
Jeff Hao5667f562017-02-27 19:32:01 -08002328// Determine target of interface dispatch. The interface method and this object are known non-null.
2329// The interface method is the method returned by the dex cache in the conflict trampoline.
2330extern "C" TwoWordReturn artInvokeInterfaceTrampoline(ArtMethod* interface_method,
Mathieu Chartieref41db72016-10-25 15:08:01 -07002331 mirror::Object* raw_this_object,
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002332 Thread* self,
2333 ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002334 REQUIRES_SHARED(Locks::mutator_lock_) {
Jeff Hao5667f562017-02-27 19:32:01 -08002335 CHECK(interface_method != nullptr);
Mathieu Chartieref41db72016-10-25 15:08:01 -07002336 ObjPtr<mirror::Object> this_object(raw_this_object);
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002337 ScopedQuickEntrypointChecks sqec(self);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002338 StackHandleScope<1> hs(self);
2339 Handle<mirror::Class> cls(hs.NewHandle(this_object->GetClass()));
2340
Nicolas Geoffray5bf7bac2016-07-06 14:18:23 +00002341 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002342 ArtMethod* method = nullptr;
Andreas Gampe542451c2016-07-26 09:02:02 -07002343 ImTable* imt = cls->GetImt(kRuntimePointerSize);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002344
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002345 if (LIKELY(interface_method->GetDexMethodIndex() != DexFile::kDexNoIndex)) {
Jeff Hao5667f562017-02-27 19:32:01 -08002346 // If the interface method is already resolved, look whether we have a match in the
2347 // ImtConflictTable.
Andreas Gampe75a7db62016-09-26 12:04:26 -07002348 ArtMethod* conflict_method = imt->Get(ImTable::GetImtIndex(interface_method),
2349 kRuntimePointerSize);
Alex Light9fc547a2016-03-31 14:34:33 -07002350 if (LIKELY(conflict_method->IsRuntimeMethod())) {
Andreas Gampe542451c2016-07-26 09:02:02 -07002351 ImtConflictTable* current_table = conflict_method->GetImtConflictTable(kRuntimePointerSize);
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002352 DCHECK(current_table != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -07002353 method = current_table->Lookup(interface_method, kRuntimePointerSize);
Alex Light9fc547a2016-03-31 14:34:33 -07002354 } else {
2355 // It seems we aren't really a conflict method!
Andreas Gampe542451c2016-07-26 09:02:02 -07002356 method = cls->FindVirtualMethodForInterface(interface_method, kRuntimePointerSize);
Alex Light9fc547a2016-03-31 14:34:33 -07002357 }
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002358 if (method != nullptr) {
2359 return GetTwoWordSuccessValue(
2360 reinterpret_cast<uintptr_t>(method->GetEntryPointFromQuickCompiledCode()),
2361 reinterpret_cast<uintptr_t>(method));
2362 }
2363
2364 // No match, use the IfTable.
Andreas Gampe542451c2016-07-26 09:02:02 -07002365 method = cls->FindVirtualMethodForInterface(interface_method, kRuntimePointerSize);
Ian Rogerse0a02da2014-12-02 14:10:53 -08002366 if (UNLIKELY(method == nullptr)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002367 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(
2368 interface_method, this_object, caller_method);
Andreas Gamped58342c2014-06-05 14:18:08 -07002369 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002370 }
2371 } else {
Jeff Hao5667f562017-02-27 19:32:01 -08002372 // The interface method is unresolved, so look it up in the dex file of the caller.
Mathieu Chartier4edd8472015-06-01 10:47:36 -07002373 DCHECK_EQ(interface_method, Runtime::Current()->GetResolutionMethod());
Jeff Hao5667f562017-02-27 19:32:01 -08002374
2375 // Fetch the dex_method_idx of the target interface method from the caller.
2376 uint32_t dex_method_idx;
2377 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
2378 const DexFile::CodeItem* code_item = caller_method->GetCodeItem();
2379 DCHECK_LT(dex_pc, code_item->insns_size_in_code_units_);
2380 const Instruction* instr = Instruction::At(&code_item->insns_[dex_pc]);
2381 Instruction::Code instr_code = instr->Opcode();
2382 DCHECK(instr_code == Instruction::INVOKE_INTERFACE ||
2383 instr_code == Instruction::INVOKE_INTERFACE_RANGE)
2384 << "Unexpected call into interface trampoline: " << instr->DumpString(nullptr);
2385 if (instr_code == Instruction::INVOKE_INTERFACE) {
2386 dex_method_idx = instr->VRegB_35c();
2387 } else {
2388 DCHECK_EQ(instr_code, Instruction::INVOKE_INTERFACE_RANGE);
2389 dex_method_idx = instr->VRegB_3rc();
2390 }
2391
Andreas Gampec200a4a2014-06-16 18:39:09 -07002392 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()
2393 ->GetDexFile();
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002394 uint32_t shorty_len;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002395 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(dex_method_idx),
2396 &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002397 {
2398 // Remember the args in case a GC happens in FindMethodFromCode.
2399 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
2400 RememberForGcArgumentVisitor visitor(sp, false, shorty, shorty_len, &soa);
2401 visitor.VisitArguments();
Mathieu Chartieref41db72016-10-25 15:08:01 -07002402 method = FindMethodFromCode<kInterface, false>(dex_method_idx,
2403 &this_object,
2404 caller_method,
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002405 self);
2406 visitor.FixupReferences();
2407 }
2408
2409 if (UNLIKELY(method == nullptr)) {
2410 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07002411 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002412 }
Andreas Gampe542451c2016-07-26 09:02:02 -07002413 interface_method =
2414 caller_method->GetDexCacheResolvedMethod(dex_method_idx, kRuntimePointerSize);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002415 DCHECK(!interface_method->IsRuntimeMethod());
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002416 }
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002417
2418 // We arrive here if we have found an implementation, and it is not in the ImtConflictTable.
2419 // We create a new table with the new pair { interface_method, method }.
Andreas Gampe75a7db62016-09-26 12:04:26 -07002420 uint32_t imt_index = ImTable::GetImtIndex(interface_method);
Andreas Gampe542451c2016-07-26 09:02:02 -07002421 ArtMethod* conflict_method = imt->Get(imt_index, kRuntimePointerSize);
Alex Light9fc547a2016-03-31 14:34:33 -07002422 if (conflict_method->IsRuntimeMethod()) {
Mathieu Chartier7f98c9a2016-04-14 10:49:19 -07002423 ArtMethod* new_conflict_method = Runtime::Current()->GetClassLinker()->AddMethodToConflictTable(
2424 cls.Get(),
2425 conflict_method,
2426 interface_method,
2427 method,
2428 /*force_new_conflict_method*/false);
2429 if (new_conflict_method != conflict_method) {
Alex Light9fc547a2016-03-31 14:34:33 -07002430 // Update the IMT if we create a new conflict method. No fence needed here, as the
2431 // data is consistent.
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002432 imt->Set(imt_index,
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002433 new_conflict_method,
Andreas Gampe542451c2016-07-26 09:02:02 -07002434 kRuntimePointerSize);
Alex Light9fc547a2016-03-31 14:34:33 -07002435 }
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002436 }
2437
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002438 const void* code = method->GetEntryPointFromQuickCompiledCode();
2439
2440 // When we return, the caller will branch to this address, so it had better not be 0!
David Sehr709b0702016-10-13 09:12:37 -07002441 DCHECK(code != nullptr) << "Code was null in method: " << method->PrettyMethod()
Andreas Gampec200a4a2014-06-16 18:39:09 -07002442 << " location: " << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07002443
Andreas Gamped58342c2014-06-05 14:18:08 -07002444 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
2445 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002446}
2447
Orion Hodsonac141392017-01-13 11:53:47 +00002448// Returns shorty type so the caller can determine how to put |result|
2449// into expected registers. The shorty type is static so the compiler
2450// could call different flavors of this code path depending on the
2451// shorty type though this would require different entry points for
2452// each type.
2453extern "C" uintptr_t artInvokePolymorphic(
2454 JValue* result,
2455 mirror::Object* raw_method_handle,
2456 Thread* self,
2457 ArtMethod** sp)
2458 REQUIRES_SHARED(Locks::mutator_lock_) {
2459 ScopedQuickEntrypointChecks sqec(self);
2460 DCHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(Runtime::kSaveRefsAndArgs));
2461
2462 // Start new JNI local reference state
2463 JNIEnvExt* env = self->GetJniEnv();
2464 ScopedObjectAccessUnchecked soa(env);
2465 ScopedJniEnvLocalRefState env_state(env);
2466 const char* old_cause = self->StartAssertNoThreadSuspension("Making stack arguments safe.");
2467
2468 // From the instruction, get the |callsite_shorty| and expose arguments on the stack to the GC.
2469 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
2470 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
2471 const DexFile::CodeItem* code = caller_method->GetCodeItem();
2472 const Instruction* inst = Instruction::At(&code->insns_[dex_pc]);
2473 DCHECK(inst->Opcode() == Instruction::INVOKE_POLYMORPHIC ||
2474 inst->Opcode() == Instruction::INVOKE_POLYMORPHIC_RANGE);
2475 const DexFile* dex_file = caller_method->GetDexFile();
2476 const uint32_t proto_idx = inst->VRegH();
2477 const char* shorty = dex_file->GetShorty(proto_idx);
2478 const size_t shorty_length = strlen(shorty);
2479 static const bool kMethodIsStatic = false; // invoke() and invokeExact() are not static.
2480 RememberForGcArgumentVisitor gc_visitor(sp, kMethodIsStatic, shorty, shorty_length, &soa);
Orion Hodsonfea84dd2017-01-16 13:52:20 +00002481 gc_visitor.VisitArguments();
Orion Hodsonac141392017-01-13 11:53:47 +00002482
2483 // Wrap raw_method_handle in a Handle for safety.
2484 StackHandleScope<5> hs(self);
Orion Hodsonc069a302017-01-18 09:23:12 +00002485 Handle<mirror::MethodHandle> method_handle(
2486 hs.NewHandle(ObjPtr<mirror::MethodHandle>::DownCast(MakeObjPtr(raw_method_handle))));
Orion Hodsonac141392017-01-13 11:53:47 +00002487 raw_method_handle = nullptr;
2488 self->EndAssertNoThreadSuspension(old_cause);
2489
2490 // Resolve method - it's either MethodHandle.invoke() or MethodHandle.invokeExact().
2491 ClassLinker* linker = Runtime::Current()->GetClassLinker();
2492 ArtMethod* resolved_method = linker->ResolveMethod<ClassLinker::kForceICCECheck>(self,
2493 inst->VRegB(),
2494 caller_method,
2495 kVirtual);
2496 DCHECK((resolved_method ==
2497 jni::DecodeArtMethod(WellKnownClasses::java_lang_invoke_MethodHandle_invokeExact)) ||
2498 (resolved_method ==
2499 jni::DecodeArtMethod(WellKnownClasses::java_lang_invoke_MethodHandle_invoke)));
2500 if (UNLIKELY(method_handle.IsNull())) {
2501 ThrowNullPointerExceptionForMethodAccess(resolved_method, InvokeType::kVirtual);
2502 return static_cast<uintptr_t>('V');
2503 }
2504
2505 Handle<mirror::Class> caller_class(hs.NewHandle(caller_method->GetDeclaringClass()));
2506 Handle<mirror::MethodType> method_type(hs.NewHandle(linker->ResolveMethodType(
2507 *dex_file, proto_idx,
2508 hs.NewHandle<mirror::DexCache>(caller_class->GetDexCache()),
2509 hs.NewHandle<mirror::ClassLoader>(caller_class->GetClassLoader()))));
2510 // This implies we couldn't resolve one or more types in this method handle.
2511 if (UNLIKELY(method_type.IsNull())) {
2512 CHECK(self->IsExceptionPending());
2513 return static_cast<uintptr_t>('V');
2514 }
2515
2516 DCHECK_EQ(ArtMethod::NumArgRegisters(shorty) + 1u, (uint32_t)inst->VRegA());
2517 DCHECK_EQ(resolved_method->IsStatic(), kMethodIsStatic);
2518
2519 // Fix references before constructing the shadow frame.
2520 gc_visitor.FixupReferences();
2521
2522 // Construct shadow frame placing arguments consecutively from |first_arg|.
2523 const bool is_range = (inst->Opcode() == Instruction::INVOKE_POLYMORPHIC_RANGE);
2524 const size_t num_vregs = is_range ? inst->VRegA_4rcc() : inst->VRegA_45cc();
2525 const size_t first_arg = 0;
2526 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
2527 CREATE_SHADOW_FRAME(num_vregs, /* link */ nullptr, resolved_method, dex_pc);
2528 ShadowFrame* shadow_frame = shadow_frame_unique_ptr.get();
2529 ScopedStackedShadowFramePusher
2530 frame_pusher(self, shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
2531 BuildQuickShadowFrameVisitor shadow_frame_builder(sp,
2532 kMethodIsStatic,
2533 shorty,
2534 strlen(shorty),
2535 shadow_frame,
2536 first_arg);
2537 shadow_frame_builder.VisitArguments();
2538
2539 // Push a transition back into managed code onto the linked list in thread.
2540 ManagedStack fragment;
2541 self->PushManagedStackFragment(&fragment);
2542
2543 // Call DoInvokePolymorphic with |is_range| = true, as shadow frame has argument registers in
2544 // consecutive order.
2545 uint32_t unused_args[Instruction::kMaxVarArgRegs] = {};
2546 uint32_t first_callee_arg = first_arg + 1;
Orion Hodsonc069a302017-01-18 09:23:12 +00002547 if (!DoInvokePolymorphic<true /* is_range */>(self,
2548 resolved_method,
2549 *shadow_frame,
2550 method_handle,
2551 method_type,
2552 unused_args,
2553 first_callee_arg,
2554 result)) {
Orion Hodsonac141392017-01-13 11:53:47 +00002555 DCHECK(self->IsExceptionPending());
2556 }
2557
2558 // Pop transition record.
2559 self->PopManagedStackFragment(fragment);
2560
2561 return static_cast<uintptr_t>(shorty[0]);
2562}
2563
Ian Rogers848871b2013-08-05 10:56:33 -07002564} // namespace art