blob: 2b349e39a04e11f099044d89afed796a840bcb7b [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"
Ian Rogers848871b2013-08-05 10:56:33 -070021#include "dex_file-inl.h"
22#include "dex_instruction-inl.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070023#include "entrypoints/entrypoint_utils-inl.h"
Ian Rogers6f3dbba2014-10-14 17:41:57 -070024#include "entrypoints/runtime_asm_entrypoints.h"
Ian Rogers83883d72013-10-21 21:07:24 -070025#include "gc/accounting/card_table-inl.h"
Andreas Gampe75a7db62016-09-26 12:04:26 -070026#include "imt_conflict_table.h"
27#include "imtable-inl.h"
Ian Rogers848871b2013-08-05 10:56:33 -070028#include "interpreter/interpreter.h"
Nicolas Geoffray796d6302016-03-13 22:22:31 +000029#include "linear_alloc.h"
Orion Hodsonac141392017-01-13 11:53:47 +000030#include "method_handles.h"
Ian Rogerse0a02da2014-12-02 14:10:53 -080031#include "method_reference.h"
Ian Rogers848871b2013-08-05 10:56:33 -070032#include "mirror/class-inl.h"
Mathieu Chartier5f3ded42014-04-03 15:25:30 -070033#include "mirror/dex_cache-inl.h"
Mathieu Chartierfc58af42015-04-16 18:00:39 -070034#include "mirror/method.h"
Orion Hodsonac141392017-01-13 11:53:47 +000035#include "mirror/method_handle_impl.h"
Ian Rogers848871b2013-08-05 10:56:33 -070036#include "mirror/object-inl.h"
37#include "mirror/object_array-inl.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010038#include "oat_quick_method_header.h"
Andreas Gampe639bdd12015-06-03 11:22:45 -070039#include "quick_exception_handler.h"
Ian Rogers848871b2013-08-05 10:56:33 -070040#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070041#include "scoped_thread_state_change-inl.h"
Andreas Gampeb3025922015-09-01 14:45:00 -070042#include "stack.h"
Daniel Mihalyieb076692014-08-22 17:33:31 +020043#include "debugger.h"
Orion Hodsonac141392017-01-13 11:53:47 +000044#include "well_known_classes.h"
Ian Rogers848871b2013-08-05 10:56:33 -070045
46namespace art {
47
48// Visits the arguments as saved to the stack by a Runtime::kRefAndArgs callee save frame.
49class QuickArgumentVisitor {
Ian Rogers936b37f2014-02-14 00:52:24 -080050 // Number of bytes for each out register in the caller method's frame.
51 static constexpr size_t kBytesStackArgLocation = 4;
Alexei Zavjalov41c507a2014-05-15 16:02:46 +070052 // Frame size in bytes of a callee-save frame for RefsAndArgs.
53 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize =
Vladimir Markofd36f1f2016-08-03 18:49:58 +010054 GetCalleeSaveFrameSize(kRuntimeISA, Runtime::kSaveRefsAndArgs);
Ian Rogers848871b2013-08-05 10:56:33 -070055#if defined(__arm__)
56 // The callee save frame is pointed to by SP.
57 // | argN | |
58 // | ... | |
59 // | arg4 | |
60 // | arg3 spill | | Caller's frame
61 // | arg2 spill | |
62 // | arg1 spill | |
63 // | Method* | ---
64 // | LR |
Zheng Xu5667fdb2014-10-23 18:29:55 +080065 // | ... | 4x6 bytes callee saves
66 // | R3 |
67 // | R2 |
68 // | R1 |
69 // | S15 |
70 // | : |
71 // | S0 |
72 // | | 4x2 bytes padding
Ian Rogers848871b2013-08-05 10:56:33 -070073 // | Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -050074 static constexpr bool kSplitPairAcrossRegisterAndStack = kArm32QuickCodeUseSoftFloat;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +000075 static constexpr bool kAlignPairRegister = !kArm32QuickCodeUseSoftFloat;
Zheng Xu5667fdb2014-10-23 18:29:55 +080076 static constexpr bool kQuickSoftFloatAbi = kArm32QuickCodeUseSoftFloat;
77 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = !kArm32QuickCodeUseSoftFloat;
Goran Jakovljevicff734982015-08-24 12:58:55 +000078 static constexpr bool kQuickSkipOddFpRegisters = false;
Zheng Xu5667fdb2014-10-23 18:29:55 +080079 static constexpr size_t kNumQuickGprArgs = 3;
80 static constexpr size_t kNumQuickFprArgs = kArm32QuickCodeUseSoftFloat ? 0 : 16;
Andreas Gampe1a5c4062015-01-15 12:10:47 -080081 static constexpr bool kGprFprLockstep = false;
Zheng Xub551fdc2014-07-25 11:49:42 +080082 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset =
Vladimir Markofd36f1f2016-08-03 18:49:58 +010083 arm::ArmCalleeSaveFpr1Offset(Runtime::kSaveRefsAndArgs); // Offset of first FPR arg.
Zheng Xub551fdc2014-07-25 11:49:42 +080084 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset =
Vladimir Markofd36f1f2016-08-03 18:49:58 +010085 arm::ArmCalleeSaveGpr1Offset(Runtime::kSaveRefsAndArgs); // Offset of first GPR arg.
Zheng Xub551fdc2014-07-25 11:49:42 +080086 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset =
Vladimir Markofd36f1f2016-08-03 18:49:58 +010087 arm::ArmCalleeSaveLrOffset(Runtime::kSaveRefsAndArgs); // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -080088 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +000089 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -080090 }
Stuart Monteithb95a5342014-03-12 13:32:32 +000091#elif defined(__aarch64__)
92 // The callee save frame is pointed to by SP.
93 // | argN | |
94 // | ... | |
95 // | arg4 | |
96 // | arg3 spill | | Caller's frame
97 // | arg2 spill | |
98 // | arg1 spill | |
99 // | Method* | ---
100 // | LR |
Zheng Xub551fdc2014-07-25 11:49:42 +0800101 // | X29 |
Stuart Monteithb95a5342014-03-12 13:32:32 +0000102 // | : |
Serban Constantinescu9bd88b02015-04-22 16:24:46 +0100103 // | X20 |
Stuart Monteithb95a5342014-03-12 13:32:32 +0000104 // | X7 |
105 // | : |
106 // | X1 |
Zheng Xub551fdc2014-07-25 11:49:42 +0800107 // | D7 |
Stuart Monteithb95a5342014-03-12 13:32:32 +0000108 // | : |
109 // | D0 |
110 // | | padding
111 // | Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500112 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000113 static constexpr bool kAlignPairRegister = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000114 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800115 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000116 static constexpr bool kQuickSkipOddFpRegisters = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000117 static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs.
118 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800119 static constexpr bool kGprFprLockstep = false;
Zheng Xub551fdc2014-07-25 11:49:42 +0800120 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset =
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100121 arm64::Arm64CalleeSaveFpr1Offset(Runtime::kSaveRefsAndArgs); // Offset of first FPR arg.
Zheng Xub551fdc2014-07-25 11:49:42 +0800122 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset =
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100123 arm64::Arm64CalleeSaveGpr1Offset(Runtime::kSaveRefsAndArgs); // Offset of first GPR arg.
Zheng Xub551fdc2014-07-25 11:49:42 +0800124 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset =
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100125 arm64::Arm64CalleeSaveLrOffset(Runtime::kSaveRefsAndArgs); // Offset of return address.
Stuart Monteithb95a5342014-03-12 13:32:32 +0000126 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000127 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Stuart Monteithb95a5342014-03-12 13:32:32 +0000128 }
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800129#elif defined(__mips__) && !defined(__LP64__)
Ian Rogers848871b2013-08-05 10:56:33 -0700130 // The callee save frame is pointed to by SP.
131 // | argN | |
132 // | ... | |
133 // | arg4 | |
134 // | arg3 spill | | Caller's frame
135 // | arg2 spill | |
136 // | arg1 spill | |
137 // | Method* | ---
138 // | RA |
139 // | ... | callee saves
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800140 // | T1 | arg5
141 // | T0 | arg4
Ian Rogers848871b2013-08-05 10:56:33 -0700142 // | A3 | arg3
143 // | A2 | arg2
144 // | A1 | arg1
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800145 // | F19 |
146 // | F18 | f_arg5
147 // | F17 |
148 // | F16 | f_arg4
Goran Jakovljevicff734982015-08-24 12:58:55 +0000149 // | F15 |
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800150 // | F14 | f_arg3
Goran Jakovljevicff734982015-08-24 12:58:55 +0000151 // | F13 |
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800152 // | F12 | f_arg2
153 // | F11 |
154 // | F10 | f_arg1
155 // | F9 |
156 // | F8 | f_arg0
Goran Jakovljevicff734982015-08-24 12:58:55 +0000157 // | | padding
Ian Rogers848871b2013-08-05 10:56:33 -0700158 // | A0/Method* | <- sp
Goran Jakovljevicff734982015-08-24 12:58:55 +0000159 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
160 static constexpr bool kAlignPairRegister = true;
161 static constexpr bool kQuickSoftFloatAbi = false;
Zheng Xu5667fdb2014-10-23 18:29:55 +0800162 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000163 static constexpr bool kQuickSkipOddFpRegisters = true;
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800164 static constexpr size_t kNumQuickGprArgs = 5; // 5 arguments passed in GPRs.
165 static constexpr size_t kNumQuickFprArgs = 12; // 6 arguments passed in FPRs. Floats can be
166 // passed only in even numbered registers and each
167 // double occupies two registers.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800168 static constexpr bool kGprFprLockstep = false;
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800169 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 8; // Offset of first FPR arg.
170 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 56; // Offset of first GPR arg.
171 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 108; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800172 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000173 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800174 }
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800175#elif defined(__mips__) && defined(__LP64__)
176 // The callee save frame is pointed to by SP.
177 // | argN | |
178 // | ... | |
179 // | arg4 | |
180 // | arg3 spill | | Caller's frame
181 // | arg2 spill | |
182 // | arg1 spill | |
183 // | Method* | ---
184 // | RA |
185 // | ... | callee saves
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800186 // | A7 | arg7
187 // | A6 | arg6
188 // | A5 | arg5
189 // | A4 | arg4
190 // | A3 | arg3
191 // | A2 | arg2
192 // | A1 | arg1
Goran Jakovljevicff734982015-08-24 12:58:55 +0000193 // | F19 | f_arg7
194 // | F18 | f_arg6
195 // | F17 | f_arg5
196 // | F16 | f_arg4
197 // | F15 | f_arg3
198 // | F14 | f_arg2
199 // | F13 | f_arg1
200 // | F12 | f_arg0
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800201 // | | padding
202 // | A0/Method* | <- sp
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800203 // NOTE: for Mip64, when A0 is skipped, F12 is also skipped.
Douglas Leungd18e0832015-02-09 15:22:26 -0800204 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800205 static constexpr bool kAlignPairRegister = false;
206 static constexpr bool kQuickSoftFloatAbi = false;
207 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000208 static constexpr bool kQuickSkipOddFpRegisters = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800209 static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs.
210 static constexpr size_t kNumQuickFprArgs = 7; // 7 arguments passed in FPRs.
211 static constexpr bool kGprFprLockstep = true;
212
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800213 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 24; // Offset of first FPR arg (F13).
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800214 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 80; // Offset of first GPR arg (A1).
215 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 200; // Offset of return address.
216 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
217 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
218 }
Ian Rogers848871b2013-08-05 10:56:33 -0700219#elif defined(__i386__)
220 // The callee save frame is pointed to by SP.
221 // | argN | |
222 // | ... | |
223 // | arg4 | |
224 // | arg3 spill | | Caller's frame
225 // | arg2 spill | |
226 // | arg1 spill | |
227 // | Method* | ---
228 // | Return |
229 // | EBP,ESI,EDI | callee saves
230 // | EBX | arg3
231 // | EDX | arg2
232 // | ECX | arg1
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000233 // | XMM3 | float arg 4
234 // | XMM2 | float arg 3
235 // | XMM1 | float arg 2
236 // | XMM0 | float arg 1
Ian Rogers848871b2013-08-05 10:56:33 -0700237 // | EAX/Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500238 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000239 static constexpr bool kAlignPairRegister = false;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000240 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800241 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000242 static constexpr bool kQuickSkipOddFpRegisters = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800243 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs.
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000244 static constexpr size_t kNumQuickFprArgs = 4; // 4 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800245 static constexpr bool kGprFprLockstep = false;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000246 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 4; // Offset of first FPR arg.
247 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 4 + 4*8; // Offset of first GPR arg.
248 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 28 + 4*8; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800249 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000250 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800251 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800252#elif defined(__x86_64__)
Ian Rogers936b37f2014-02-14 00:52:24 -0800253 // The callee save frame is pointed to by SP.
254 // | argN | |
255 // | ... | |
256 // | reg. arg spills | | Caller's frame
257 // | Method* | ---
258 // | Return |
259 // | R15 | callee save
260 // | R14 | callee save
261 // | R13 | callee save
262 // | R12 | callee save
263 // | R9 | arg5
264 // | R8 | arg4
265 // | RSI/R6 | arg1
266 // | RBP/R5 | callee save
267 // | RBX/R3 | callee save
268 // | RDX/R2 | arg2
269 // | RCX/R1 | arg3
270 // | XMM7 | float arg 8
271 // | XMM6 | float arg 7
272 // | XMM5 | float arg 6
273 // | XMM4 | float arg 5
274 // | XMM3 | float arg 4
275 // | XMM2 | float arg 3
276 // | XMM1 | float arg 2
277 // | XMM0 | float arg 1
278 // | Padding |
279 // | RDI/Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500280 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000281 static constexpr bool kAlignPairRegister = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800282 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800283 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000284 static constexpr bool kQuickSkipOddFpRegisters = false;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700285 static constexpr size_t kNumQuickGprArgs = 5; // 5 arguments passed in GPRs.
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700286 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800287 static constexpr bool kGprFprLockstep = false;
Ian Rogers936b37f2014-02-14 00:52:24 -0800288 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 16; // Offset of first FPR arg.
Serguei Katkovc3801912014-07-08 17:21:53 +0700289 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 80 + 4*8; // Offset of first GPR arg.
290 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 168 + 4*8; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800291 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
292 switch (gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000293 case 0: return (4 * GetBytesPerGprSpillLocation(kRuntimeISA));
294 case 1: return (1 * GetBytesPerGprSpillLocation(kRuntimeISA));
295 case 2: return (0 * GetBytesPerGprSpillLocation(kRuntimeISA));
296 case 3: return (5 * GetBytesPerGprSpillLocation(kRuntimeISA));
297 case 4: return (6 * GetBytesPerGprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800298 default:
Andreas Gampec200a4a2014-06-16 18:39:09 -0700299 LOG(FATAL) << "Unexpected GPR index: " << gpr_index;
300 return 0;
Ian Rogers936b37f2014-02-14 00:52:24 -0800301 }
302 }
Ian Rogers848871b2013-08-05 10:56:33 -0700303#else
304#error "Unsupported architecture"
Ian Rogers848871b2013-08-05 10:56:33 -0700305#endif
306
Ian Rogers936b37f2014-02-14 00:52:24 -0800307 public:
Sebastien Hertza836bc92014-11-25 16:30:53 +0100308 // Special handling for proxy methods. Proxy methods are instance methods so the
309 // 'this' object is the 1st argument. They also have the same frame layout as the
310 // kRefAndArgs runtime method. Since 'this' is a reference, it is located in the
311 // 1st GPR.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700312 static mirror::Object* GetProxyThisObject(ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700313 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000314 CHECK((*sp)->IsProxyMethod());
Sebastien Hertza836bc92014-11-25 16:30:53 +0100315 CHECK_GT(kNumQuickGprArgs, 0u);
316 constexpr uint32_t kThisGprIndex = 0u; // 'this' is in the 1st GPR.
317 size_t this_arg_offset = kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset +
318 GprIndexToGprOffset(kThisGprIndex);
319 uint8_t* this_arg_address = reinterpret_cast<uint8_t*>(sp) + this_arg_offset;
320 return reinterpret_cast<StackReference<mirror::Object>*>(this_arg_address)->AsMirrorPtr();
321 }
322
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700323 static ArtMethod* GetCallingMethod(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700324 DCHECK((*sp)->IsCalleeSaveMethod());
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100325 return GetCalleeSaveMethodCaller(sp, Runtime::kSaveRefsAndArgs);
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +0100326 }
327
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700328 static ArtMethod* GetOuterMethod(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700329 DCHECK((*sp)->IsCalleeSaveMethod());
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100330 uint8_t* previous_sp =
331 reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700332 return *reinterpret_cast<ArtMethod**>(previous_sp);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100333 }
334
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700335 static uint32_t GetCallingDexPc(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700336 DCHECK((*sp)->IsCalleeSaveMethod());
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100337 const size_t callee_frame_size = GetCalleeSaveFrameSize(kRuntimeISA, Runtime::kSaveRefsAndArgs);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700338 ArtMethod** caller_sp = reinterpret_cast<ArtMethod**>(
339 reinterpret_cast<uintptr_t>(sp) + callee_frame_size);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100340 uintptr_t outer_pc = QuickArgumentVisitor::GetCallingPc(sp);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100341 const OatQuickMethodHeader* current_code = (*caller_sp)->GetOatQuickMethodHeader(outer_pc);
342 uintptr_t outer_pc_offset = current_code->NativeQuickPcOffset(outer_pc);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100343
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100344 if (current_code->IsOptimized()) {
345 CodeInfo code_info = current_code->GetOptimizedCodeInfo();
David Srbecky09ed0982016-02-12 21:58:43 +0000346 CodeInfoEncoding encoding = code_info.ExtractEncoding();
David Brazdilf677ebf2015-05-29 16:29:43 +0100347 StackMap stack_map = code_info.GetStackMapForNativePcOffset(outer_pc_offset, encoding);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100348 DCHECK(stack_map.IsValid());
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800349 if (stack_map.HasInlineInfo(encoding.stack_map.encoding)) {
David Brazdilf677ebf2015-05-29 16:29:43 +0100350 InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map, encoding);
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800351 return inline_info.GetDexPcAtDepth(encoding.inline_info.encoding,
352 inline_info.GetDepth(encoding.inline_info.encoding)-1);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100353 } else {
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800354 return stack_map.GetDexPc(encoding.stack_map.encoding);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100355 }
356 } else {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100357 return current_code->ToDexPc(*caller_sp, outer_pc);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100358 }
Ian Rogers848871b2013-08-05 10:56:33 -0700359 }
360
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800361 static bool GetInvokeType(ArtMethod** sp, InvokeType* invoke_type, uint32_t* dex_method_index)
362 REQUIRES_SHARED(Locks::mutator_lock_) {
363 DCHECK((*sp)->IsCalleeSaveMethod());
364 const size_t callee_frame_size = GetCalleeSaveFrameSize(kRuntimeISA, Runtime::kSaveRefsAndArgs);
365 ArtMethod** caller_sp = reinterpret_cast<ArtMethod**>(
366 reinterpret_cast<uintptr_t>(sp) + callee_frame_size);
367 uintptr_t outer_pc = QuickArgumentVisitor::GetCallingPc(sp);
368 const OatQuickMethodHeader* current_code = (*caller_sp)->GetOatQuickMethodHeader(outer_pc);
369 if (!current_code->IsOptimized()) {
370 return false;
371 }
372 uintptr_t outer_pc_offset = current_code->NativeQuickPcOffset(outer_pc);
373 CodeInfo code_info = current_code->GetOptimizedCodeInfo();
374 CodeInfoEncoding encoding = code_info.ExtractEncoding();
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700375 MethodInfo method_info = current_code->GetOptimizedMethodInfo();
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800376 InvokeInfo invoke(code_info.GetInvokeInfoForNativePcOffset(outer_pc_offset, encoding));
377 if (invoke.IsValid()) {
378 *invoke_type = static_cast<InvokeType>(invoke.GetInvokeType(encoding.invoke_info.encoding));
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700379 *dex_method_index = invoke.GetMethodIndex(encoding.invoke_info.encoding, method_info);
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800380 return true;
381 }
382 return false;
383 }
384
Ian Rogers936b37f2014-02-14 00:52:24 -0800385 // For the given quick ref and args quick frame, return the caller's PC.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700386 static uintptr_t GetCallingPc(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700387 DCHECK((*sp)->IsCalleeSaveMethod());
Ian Rogers13735952014-10-08 12:43:28 -0700388 uint8_t* lr = reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_LrOffset;
Ian Rogers848871b2013-08-05 10:56:33 -0700389 return *reinterpret_cast<uintptr_t*>(lr);
390 }
391
Mathieu Chartiere401d142015-04-22 13:56:20 -0700392 QuickArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700393 uint32_t shorty_len) REQUIRES_SHARED(Locks::mutator_lock_) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700394 is_static_(is_static), shorty_(shorty), shorty_len_(shorty_len),
Ian Rogers13735952014-10-08 12:43:28 -0700395 gpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset),
396 fpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset),
397 stack_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize
Mathieu Chartiere401d142015-04-22 13:56:20 -0700398 + sizeof(ArtMethod*)), // Skip ArtMethod*.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800399 gpr_index_(0), fpr_index_(0), fpr_double_index_(0), stack_index_(0),
400 cur_type_(Primitive::kPrimVoid), is_split_long_or_double_(false) {
Andreas Gampe575e78c2014-11-03 23:41:03 -0800401 static_assert(kQuickSoftFloatAbi == (kNumQuickFprArgs == 0),
402 "Number of Quick FPR arguments unexpected");
403 static_assert(!(kQuickSoftFloatAbi && kQuickDoubleRegAlignedFloatBackFilled),
404 "Double alignment unexpected");
Zheng Xu5667fdb2014-10-23 18:29:55 +0800405 // For register alignment, we want to assume that counters(fpr_double_index_) are even if the
406 // next register is even.
Andreas Gampe575e78c2014-11-03 23:41:03 -0800407 static_assert(!kQuickDoubleRegAlignedFloatBackFilled || kNumQuickFprArgs % 2 == 0,
408 "Number of Quick FPR arguments not even");
Andreas Gampe542451c2016-07-26 09:02:02 -0700409 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Zheng Xu5667fdb2014-10-23 18:29:55 +0800410 }
Ian Rogers848871b2013-08-05 10:56:33 -0700411
412 virtual ~QuickArgumentVisitor() {}
413
414 virtual void Visit() = 0;
415
Ian Rogers936b37f2014-02-14 00:52:24 -0800416 Primitive::Type GetParamPrimitiveType() const {
417 return cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700418 }
419
Ian Rogers13735952014-10-08 12:43:28 -0700420 uint8_t* GetParamAddress() const {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800421 if (!kQuickSoftFloatAbi) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800422 Primitive::Type type = GetParamPrimitiveType();
423 if (UNLIKELY((type == Primitive::kPrimDouble) || (type == Primitive::kPrimFloat))) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800424 if (type == Primitive::kPrimDouble && kQuickDoubleRegAlignedFloatBackFilled) {
425 if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) {
426 return fpr_args_ + (fpr_double_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
427 }
428 } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000429 return fpr_args_ + (fpr_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800430 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700431 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers936b37f2014-02-14 00:52:24 -0800432 }
433 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800434 if (gpr_index_ < kNumQuickGprArgs) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800435 return gpr_args_ + GprIndexToGprOffset(gpr_index_);
436 }
437 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700438 }
439
440 bool IsSplitLongOrDouble() const {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700441 if ((GetBytesPerGprSpillLocation(kRuntimeISA) == 4) ||
442 (GetBytesPerFprSpillLocation(kRuntimeISA) == 4)) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800443 return is_split_long_or_double_;
444 } else {
445 return false; // An optimization for when GPR and FPRs are 64bit.
446 }
Ian Rogers848871b2013-08-05 10:56:33 -0700447 }
448
Ian Rogers936b37f2014-02-14 00:52:24 -0800449 bool IsParamAReference() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700450 return GetParamPrimitiveType() == Primitive::kPrimNot;
451 }
452
Ian Rogers936b37f2014-02-14 00:52:24 -0800453 bool IsParamALongOrDouble() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700454 Primitive::Type type = GetParamPrimitiveType();
455 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
456 }
457
458 uint64_t ReadSplitLongParam() const {
Nicolas Geoffray425f2392015-01-08 14:52:29 +0000459 // The splitted long is always available through the stack.
460 return *reinterpret_cast<uint64_t*>(stack_args_
461 + stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700462 }
463
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800464 void IncGprIndex() {
465 gpr_index_++;
466 if (kGprFprLockstep) {
467 fpr_index_++;
468 }
469 }
470
471 void IncFprIndex() {
472 fpr_index_++;
473 if (kGprFprLockstep) {
474 gpr_index_++;
475 }
476 }
477
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700478 void VisitArguments() REQUIRES_SHARED(Locks::mutator_lock_) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800479 // (a) 'stack_args_' should point to the first method's argument
480 // (b) whatever the argument type it is, the 'stack_index_' should
481 // be moved forward along with every visiting.
Ian Rogers936b37f2014-02-14 00:52:24 -0800482 gpr_index_ = 0;
483 fpr_index_ = 0;
Zheng Xu5667fdb2014-10-23 18:29:55 +0800484 if (kQuickDoubleRegAlignedFloatBackFilled) {
485 fpr_double_index_ = 0;
486 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800487 stack_index_ = 0;
488 if (!is_static_) { // Handle this.
489 cur_type_ = Primitive::kPrimNot;
490 is_split_long_or_double_ = false;
Ian Rogers848871b2013-08-05 10:56:33 -0700491 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800492 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800493 if (kNumQuickGprArgs > 0) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800494 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800495 }
Ian Rogers848871b2013-08-05 10:56:33 -0700496 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800497 for (uint32_t shorty_index = 1; shorty_index < shorty_len_; ++shorty_index) {
498 cur_type_ = Primitive::GetType(shorty_[shorty_index]);
499 switch (cur_type_) {
500 case Primitive::kPrimNot:
501 case Primitive::kPrimBoolean:
502 case Primitive::kPrimByte:
503 case Primitive::kPrimChar:
504 case Primitive::kPrimShort:
505 case Primitive::kPrimInt:
506 is_split_long_or_double_ = false;
507 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800508 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800509 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800510 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800511 }
512 break;
513 case Primitive::kPrimFloat:
514 is_split_long_or_double_ = false;
515 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800516 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800517 if (kQuickSoftFloatAbi) {
518 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800519 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800520 }
521 } else {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800522 if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800523 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800524 if (kQuickDoubleRegAlignedFloatBackFilled) {
525 // Double should not overlap with float.
526 // For example, if fpr_index_ = 3, fpr_double_index_ should be at least 4.
527 fpr_double_index_ = std::max(fpr_double_index_, RoundUp(fpr_index_, 2));
528 // Float should not overlap with double.
529 if (fpr_index_ % 2 == 0) {
530 fpr_index_ = std::max(fpr_double_index_, fpr_index_);
531 }
Goran Jakovljevicff734982015-08-24 12:58:55 +0000532 } else if (kQuickSkipOddFpRegisters) {
533 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800534 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800535 }
536 }
537 break;
538 case Primitive::kPrimDouble:
539 case Primitive::kPrimLong:
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800540 if (kQuickSoftFloatAbi || (cur_type_ == Primitive::kPrimLong)) {
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800541 if (cur_type_ == Primitive::kPrimLong &&
542#if defined(__mips__) && !defined(__LP64__)
543 (gpr_index_ == 0 || gpr_index_ == 2) &&
544#else
545 gpr_index_ == 0 &&
546#endif
547 kAlignPairRegister) {
548 // Currently, this is only for ARM and MIPS, where we align long parameters with
549 // even-numbered registers by skipping R1 (on ARM) or A1(A3) (on MIPS) and using
550 // R2 (on ARM) or A2(T0) (on MIPS) instead.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800551 IncGprIndex();
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000552 }
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000553 is_split_long_or_double_ = (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) &&
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800554 ((gpr_index_ + 1) == kNumQuickGprArgs);
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500555 if (!kSplitPairAcrossRegisterAndStack && is_split_long_or_double_) {
556 // We don't want to split this. Pass over this register.
557 gpr_index_++;
558 is_split_long_or_double_ = false;
559 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800560 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800561 if (kBytesStackArgLocation == 4) {
562 stack_index_+= 2;
563 } else {
564 CHECK_EQ(kBytesStackArgLocation, 8U);
565 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800566 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700567 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800568 IncGprIndex();
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000569 if (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700570 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800571 IncGprIndex();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700572 }
573 }
574 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800575 } else {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000576 is_split_long_or_double_ = (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) &&
Zheng Xu5667fdb2014-10-23 18:29:55 +0800577 ((fpr_index_ + 1) == kNumQuickFprArgs) && !kQuickDoubleRegAlignedFloatBackFilled;
Ian Rogers936b37f2014-02-14 00:52:24 -0800578 Visit();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700579 if (kBytesStackArgLocation == 4) {
580 stack_index_+= 2;
Ian Rogers936b37f2014-02-14 00:52:24 -0800581 } else {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700582 CHECK_EQ(kBytesStackArgLocation, 8U);
583 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800584 }
Zheng Xu5667fdb2014-10-23 18:29:55 +0800585 if (kQuickDoubleRegAlignedFloatBackFilled) {
586 if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) {
587 fpr_double_index_ += 2;
588 // Float should not overlap with double.
589 if (fpr_index_ % 2 == 0) {
590 fpr_index_ = std::max(fpr_double_index_, fpr_index_);
591 }
592 }
593 } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800594 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800595 if (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) {
596 if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800597 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800598 }
599 }
600 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800601 }
602 break;
603 default:
604 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty_;
605 }
Ian Rogers848871b2013-08-05 10:56:33 -0700606 }
607 }
608
Andreas Gampec200a4a2014-06-16 18:39:09 -0700609 protected:
Ian Rogers848871b2013-08-05 10:56:33 -0700610 const bool is_static_;
611 const char* const shorty_;
612 const uint32_t shorty_len_;
Andreas Gampec200a4a2014-06-16 18:39:09 -0700613
614 private:
Ian Rogers13735952014-10-08 12:43:28 -0700615 uint8_t* const gpr_args_; // Address of GPR arguments in callee save frame.
616 uint8_t* const fpr_args_; // Address of FPR arguments in callee save frame.
617 uint8_t* const stack_args_; // Address of stack arguments in caller's frame.
Ian Rogers936b37f2014-02-14 00:52:24 -0800618 uint32_t gpr_index_; // Index into spilled GPRs.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800619 // Index into spilled FPRs.
620 // In case kQuickDoubleRegAlignedFloatBackFilled, it may index a hole while fpr_double_index_
621 // holds a higher register number.
622 uint32_t fpr_index_;
623 // Index into spilled FPRs for aligned double.
624 // Only used when kQuickDoubleRegAlignedFloatBackFilled. Next available double register indexed in
625 // terms of singles, may be behind fpr_index.
626 uint32_t fpr_double_index_;
Ian Rogers936b37f2014-02-14 00:52:24 -0800627 uint32_t stack_index_; // Index into arguments on the stack.
628 // The current type of argument during VisitArguments.
629 Primitive::Type cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700630 // Does a 64bit parameter straddle the register and stack arguments?
631 bool is_split_long_or_double_;
632};
633
Sebastien Hertza836bc92014-11-25 16:30:53 +0100634// Returns the 'this' object of a proxy method. This function is only used by StackVisitor. It
635// allows to use the QuickArgumentVisitor constants without moving all the code in its own module.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700636extern "C" mirror::Object* artQuickGetProxyThisObject(ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700637 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertza836bc92014-11-25 16:30:53 +0100638 return QuickArgumentVisitor::GetProxyThisObject(sp);
639}
640
Ian Rogers848871b2013-08-05 10:56:33 -0700641// Visits arguments on the stack placing them into the shadow frame.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800642class BuildQuickShadowFrameVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700643 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700644 BuildQuickShadowFrameVisitor(ArtMethod** sp, bool is_static, const char* shorty,
645 uint32_t shorty_len, ShadowFrame* sf, size_t first_arg_reg) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700646 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), sf_(sf), cur_reg_(first_arg_reg) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700647
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700648 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700649
650 private:
Ian Rogers936b37f2014-02-14 00:52:24 -0800651 ShadowFrame* const sf_;
652 uint32_t cur_reg_;
Ian Rogers848871b2013-08-05 10:56:33 -0700653
Dragos Sbirleabd136a22013-08-13 18:07:04 -0700654 DISALLOW_COPY_AND_ASSIGN(BuildQuickShadowFrameVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700655};
656
Andreas Gampec200a4a2014-06-16 18:39:09 -0700657void BuildQuickShadowFrameVisitor::Visit() {
Ian Rogers9758f792014-03-13 09:02:55 -0700658 Primitive::Type type = GetParamPrimitiveType();
659 switch (type) {
660 case Primitive::kPrimLong: // Fall-through.
661 case Primitive::kPrimDouble:
662 if (IsSplitLongOrDouble()) {
663 sf_->SetVRegLong(cur_reg_, ReadSplitLongParam());
664 } else {
665 sf_->SetVRegLong(cur_reg_, *reinterpret_cast<jlong*>(GetParamAddress()));
666 }
667 ++cur_reg_;
668 break;
669 case Primitive::kPrimNot: {
670 StackReference<mirror::Object>* stack_ref =
671 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
672 sf_->SetVRegReference(cur_reg_, stack_ref->AsMirrorPtr());
673 }
674 break;
675 case Primitive::kPrimBoolean: // Fall-through.
676 case Primitive::kPrimByte: // Fall-through.
677 case Primitive::kPrimChar: // Fall-through.
678 case Primitive::kPrimShort: // Fall-through.
679 case Primitive::kPrimInt: // Fall-through.
680 case Primitive::kPrimFloat:
681 sf_->SetVReg(cur_reg_, *reinterpret_cast<jint*>(GetParamAddress()));
682 break;
683 case Primitive::kPrimVoid:
684 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700685 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -0700686 }
687 ++cur_reg_;
688}
689
Mathieu Chartiere401d142015-04-22 13:56:20 -0700690extern "C" uint64_t artQuickToInterpreterBridge(ArtMethod* method, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700691 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers848871b2013-08-05 10:56:33 -0700692 // Ensure we don't get thread suspension until the object arguments are safely in the shadow
693 // frame.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700694 ScopedQuickEntrypointChecks sqec(self);
Ian Rogers848871b2013-08-05 10:56:33 -0700695
Alex Light9139e002015-10-09 15:59:48 -0700696 if (UNLIKELY(!method->IsInvokable())) {
697 method->ThrowInvocationTimeError();
Ian Rogers848871b2013-08-05 10:56:33 -0700698 return 0;
Andreas Gampe639bdd12015-06-03 11:22:45 -0700699 }
700
701 JValue tmp_value;
702 ShadowFrame* deopt_frame = self->PopStackedShadowFrame(
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700703 StackedShadowFrameType::kDeoptimizationShadowFrame, false);
Andreas Gampe639bdd12015-06-03 11:22:45 -0700704 ManagedStack fragment;
705
David Sehr709b0702016-10-13 09:12:37 -0700706 DCHECK(!method->IsNative()) << method->PrettyMethod();
Andreas Gampe639bdd12015-06-03 11:22:45 -0700707 uint32_t shorty_len = 0;
Andreas Gampe542451c2016-07-26 09:02:02 -0700708 ArtMethod* non_proxy_method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Alex Lighte9dd04f2016-03-16 16:09:45 -0700709 const DexFile::CodeItem* code_item = non_proxy_method->GetCodeItem();
David Sehr709b0702016-10-13 09:12:37 -0700710 DCHECK(code_item != nullptr) << method->PrettyMethod();
Andreas Gampe639bdd12015-06-03 11:22:45 -0700711 const char* shorty = non_proxy_method->GetShorty(&shorty_len);
712
713 JValue result;
714
715 if (deopt_frame != nullptr) {
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700716 // Coming from partial-fragment deopt.
Andreas Gampe639bdd12015-06-03 11:22:45 -0700717
718 if (kIsDebugBuild) {
719 // Sanity-check: are the methods as expected? We check that the last shadow frame (the bottom
720 // of the call-stack) corresponds to the called method.
721 ShadowFrame* linked = deopt_frame;
722 while (linked->GetLink() != nullptr) {
723 linked = linked->GetLink();
724 }
David Sehr709b0702016-10-13 09:12:37 -0700725 CHECK_EQ(method, linked->GetMethod()) << method->PrettyMethod() << " "
726 << ArtMethod::PrettyMethod(linked->GetMethod());
Andreas Gampe639bdd12015-06-03 11:22:45 -0700727 }
728
729 if (VLOG_IS_ON(deopt)) {
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700730 // Print out the stack to verify that it was a partial-fragment deopt.
Andreas Gampe639bdd12015-06-03 11:22:45 -0700731 LOG(INFO) << "Continue-ing from deopt. Stack is:";
732 QuickExceptionHandler::DumpFramesWithType(self, true);
733 }
734
Mathieu Chartierf5769e12017-01-10 15:54:41 -0800735 ObjPtr<mirror::Throwable> pending_exception;
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100736 bool from_code = false;
737 self->PopDeoptimizationContext(&result, &pending_exception, /* out */ &from_code);
Andreas Gampe639bdd12015-06-03 11:22:45 -0700738
739 // Push a transition back into managed code onto the linked list in thread.
740 self->PushManagedStackFragment(&fragment);
741
742 // Ensure that the stack is still in order.
743 if (kIsDebugBuild) {
744 class DummyStackVisitor : public StackVisitor {
745 public:
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700746 explicit DummyStackVisitor(Thread* self_in) REQUIRES_SHARED(Locks::mutator_lock_)
Andreas Gampe639bdd12015-06-03 11:22:45 -0700747 : StackVisitor(self_in, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {}
748
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700749 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe639bdd12015-06-03 11:22:45 -0700750 // Nothing to do here. In a debug build, SanityCheckFrame will do the work in the walking
751 // logic. Just always say we want to continue.
752 return true;
753 }
754 };
755 DummyStackVisitor dsv(self);
756 dsv.WalkStack();
757 }
758
759 // Restore the exception that was pending before deoptimization then interpret the
760 // deoptimized frames.
761 if (pending_exception != nullptr) {
762 self->SetException(pending_exception);
763 }
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100764 interpreter::EnterInterpreterFromDeoptimize(self, deopt_frame, from_code, &result);
Ian Rogers848871b2013-08-05 10:56:33 -0700765 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -0700766 const char* old_cause = self->StartAssertNoThreadSuspension(
767 "Building interpreter shadow frame");
Ian Rogers848871b2013-08-05 10:56:33 -0700768 uint16_t num_regs = code_item->registers_size_;
Andreas Gampec200a4a2014-06-16 18:39:09 -0700769 // No last shadow coming from quick.
Andreas Gampeb3025922015-09-01 14:45:00 -0700770 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
Andreas Gampe03ec9302015-08-27 17:41:47 -0700771 CREATE_SHADOW_FRAME(num_regs, /* link */ nullptr, method, /* dex pc */ 0);
Andreas Gampeb3025922015-09-01 14:45:00 -0700772 ShadowFrame* shadow_frame = shadow_frame_unique_ptr.get();
Ian Rogers848871b2013-08-05 10:56:33 -0700773 size_t first_arg_reg = code_item->registers_size_ - code_item->ins_size_;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700774 BuildQuickShadowFrameVisitor shadow_frame_builder(sp, method->IsStatic(), shorty, shorty_len,
Ian Rogers936b37f2014-02-14 00:52:24 -0800775 shadow_frame, first_arg_reg);
Ian Rogers848871b2013-08-05 10:56:33 -0700776 shadow_frame_builder.VisitArguments();
Ian Rogerse94652f2014-12-02 11:13:19 -0800777 const bool needs_initialization =
778 method->IsStatic() && !method->GetDeclaringClass()->IsInitialized();
Ian Rogers848871b2013-08-05 10:56:33 -0700779 // Push a transition back into managed code onto the linked list in thread.
Ian Rogers848871b2013-08-05 10:56:33 -0700780 self->PushManagedStackFragment(&fragment);
781 self->PushShadowFrame(shadow_frame);
782 self->EndAssertNoThreadSuspension(old_cause);
783
Ian Rogerse94652f2014-12-02 11:13:19 -0800784 if (needs_initialization) {
Ian Rogers848871b2013-08-05 10:56:33 -0700785 // Ensure static method's class is initialized.
Ian Rogerse94652f2014-12-02 11:13:19 -0800786 StackHandleScope<1> hs(self);
787 Handle<mirror::Class> h_class(hs.NewHandle(shadow_frame->GetMethod()->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700788 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
David Sehr709b0702016-10-13 09:12:37 -0700789 DCHECK(Thread::Current()->IsExceptionPending())
790 << shadow_frame->GetMethod()->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700791 self->PopManagedStackFragment(fragment);
792 return 0;
793 }
794 }
Daniel Mihalyieb076692014-08-22 17:33:31 +0200795
Andreas Gampe639bdd12015-06-03 11:22:45 -0700796 result = interpreter::EnterInterpreterFromEntryPoint(self, code_item, shadow_frame);
Ian Rogers848871b2013-08-05 10:56:33 -0700797 }
Andreas Gampe639bdd12015-06-03 11:22:45 -0700798
799 // Pop transition.
800 self->PopManagedStackFragment(fragment);
801
802 // Request a stack deoptimization if needed
803 ArtMethod* caller = QuickArgumentVisitor::GetCallingMethod(sp);
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700804 uintptr_t caller_pc = QuickArgumentVisitor::GetCallingPc(sp);
Mingyao Yanga3549d22016-06-02 17:01:02 -0700805 // If caller_pc is the instrumentation exit stub, the stub will check to see if deoptimization
806 // should be done and it knows the real return pc.
807 if (UNLIKELY(caller_pc != reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) &&
Nicolas Geoffray433b79a2017-01-30 20:54:45 +0000808 Dbg::IsForcedInterpreterNeededForUpcall(self, caller))) {
809 if (!Runtime::Current()->IsAsyncDeoptimizeable(caller_pc)) {
810 LOG(WARNING) << "Got a deoptimization request on un-deoptimizable method "
811 << caller->PrettyMethod();
812 } else {
813 // Push the context of the deoptimization stack so we can restore the return value and the
814 // exception before executing the deoptimized frames.
815 self->PushDeoptimizationContext(
816 result, shorty[0] == 'L', /* from_code */ false, self->GetException());
Andreas Gampe639bdd12015-06-03 11:22:45 -0700817
Nicolas Geoffray433b79a2017-01-30 20:54:45 +0000818 // Set special exception to cause deoptimization.
819 self->SetException(Thread::GetDeoptimizationException());
820 }
Andreas Gampe639bdd12015-06-03 11:22:45 -0700821 }
822
823 // No need to restore the args since the method has already been run by the interpreter.
824 return result.GetJ();
Ian Rogers848871b2013-08-05 10:56:33 -0700825}
826
827// Visits arguments on the stack placing them into the args vector, Object* arguments are converted
828// to jobjects.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800829class BuildQuickArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700830 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700831 BuildQuickArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty, uint32_t shorty_len,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700832 ScopedObjectAccessUnchecked* soa, std::vector<jvalue>* args) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700833 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa), args_(args) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700834
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700835 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700836
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700837 void FixupReferences() REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800838
Ian Rogers848871b2013-08-05 10:56:33 -0700839 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700840 ScopedObjectAccessUnchecked* const soa_;
841 std::vector<jvalue>* const args_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800842 // References which we must update when exiting in case the GC moved the objects.
Ian Rogers700a4022014-05-19 16:49:03 -0700843 std::vector<std::pair<jobject, StackReference<mirror::Object>*>> references_;
Ian Rogers9758f792014-03-13 09:02:55 -0700844
Ian Rogers848871b2013-08-05 10:56:33 -0700845 DISALLOW_COPY_AND_ASSIGN(BuildQuickArgumentVisitor);
846};
847
Ian Rogers9758f792014-03-13 09:02:55 -0700848void BuildQuickArgumentVisitor::Visit() {
849 jvalue val;
850 Primitive::Type type = GetParamPrimitiveType();
851 switch (type) {
852 case Primitive::kPrimNot: {
853 StackReference<mirror::Object>* stack_ref =
854 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
855 val.l = soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
856 references_.push_back(std::make_pair(val.l, stack_ref));
857 break;
858 }
859 case Primitive::kPrimLong: // Fall-through.
860 case Primitive::kPrimDouble:
861 if (IsSplitLongOrDouble()) {
862 val.j = ReadSplitLongParam();
863 } else {
864 val.j = *reinterpret_cast<jlong*>(GetParamAddress());
865 }
866 break;
867 case Primitive::kPrimBoolean: // Fall-through.
868 case Primitive::kPrimByte: // Fall-through.
869 case Primitive::kPrimChar: // Fall-through.
870 case Primitive::kPrimShort: // Fall-through.
871 case Primitive::kPrimInt: // Fall-through.
872 case Primitive::kPrimFloat:
873 val.i = *reinterpret_cast<jint*>(GetParamAddress());
874 break;
875 case Primitive::kPrimVoid:
876 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700877 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -0700878 }
879 args_->push_back(val);
880}
881
882void BuildQuickArgumentVisitor::FixupReferences() {
883 // Fixup any references which may have changed.
884 for (const auto& pair : references_) {
Mathieu Chartier1a5337f2016-10-13 13:48:23 -0700885 pair.second->Assign(soa_->Decode<mirror::Object>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -0700886 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -0700887 }
888}
889
Ian Rogers848871b2013-08-05 10:56:33 -0700890// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
891// which is responsible for recording callee save registers. We explicitly place into jobjects the
892// incoming reference arguments (so they survive GC). We invoke the invocation handler, which is a
893// field within the proxy object, which will box the primitive arguments and deal with error cases.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700894extern "C" uint64_t artQuickProxyInvokeHandler(
895 ArtMethod* proxy_method, mirror::Object* receiver, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700896 REQUIRES_SHARED(Locks::mutator_lock_) {
David Sehr709b0702016-10-13 09:12:37 -0700897 DCHECK(proxy_method->IsProxyMethod()) << proxy_method->PrettyMethod();
898 DCHECK(receiver->GetClass()->IsProxyClass()) << proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700899 // Ensure we don't get thread suspension until the object arguments are safely in jobjects.
900 const char* old_cause =
901 self->StartAssertNoThreadSuspension("Adding to IRT proxy object arguments");
902 // Register the top of the managed stack, making stack crawlable.
David Sehr709b0702016-10-13 09:12:37 -0700903 DCHECK_EQ((*sp), proxy_method) << proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700904 self->VerifyStack();
905 // Start new JNI local reference state.
906 JNIEnvExt* env = self->GetJniEnv();
907 ScopedObjectAccessUnchecked soa(env);
908 ScopedJniEnvLocalRefState env_state(env);
909 // Create local ref. copies of proxy method and the receiver.
910 jobject rcvr_jobj = soa.AddLocalReference<jobject>(receiver);
911
912 // Placing arguments into args vector and remove the receiver.
Andreas Gampe542451c2016-07-26 09:02:02 -0700913 ArtMethod* non_proxy_method = proxy_method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
David Sehr709b0702016-10-13 09:12:37 -0700914 CHECK(!non_proxy_method->IsStatic()) << proxy_method->PrettyMethod() << " "
915 << non_proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700916 std::vector<jvalue> args;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700917 uint32_t shorty_len = 0;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700918 const char* shorty = non_proxy_method->GetShorty(&shorty_len);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700919 BuildQuickArgumentVisitor local_ref_visitor(sp, false, shorty, shorty_len, &soa, &args);
Brian Carlstromd3633d52013-08-20 21:06:26 -0700920
Ian Rogers848871b2013-08-05 10:56:33 -0700921 local_ref_visitor.VisitArguments();
David Sehr709b0702016-10-13 09:12:37 -0700922 DCHECK_GT(args.size(), 0U) << proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700923 args.erase(args.begin());
924
925 // Convert proxy method into expected interface method.
Andreas Gampe542451c2016-07-26 09:02:02 -0700926 ArtMethod* interface_method = proxy_method->FindOverriddenMethod(kRuntimePointerSize);
David Sehr709b0702016-10-13 09:12:37 -0700927 DCHECK(interface_method != nullptr) << proxy_method->PrettyMethod();
928 DCHECK(!interface_method->IsProxyMethod()) << interface_method->PrettyMethod();
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700929 self->EndAssertNoThreadSuspension(old_cause);
Andreas Gampe542451c2016-07-26 09:02:02 -0700930 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Andreas Gampee01e3642016-07-25 13:06:04 -0700931 DCHECK(!Runtime::Current()->IsActiveTransaction());
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700932 jobject interface_method_jobj = soa.AddLocalReference<jobject>(
Andreas Gampe542451c2016-07-26 09:02:02 -0700933 mirror::Method::CreateFromArtMethod<kRuntimePointerSize, false>(soa.Self(),
934 interface_method));
Ian Rogers848871b2013-08-05 10:56:33 -0700935
936 // All naked Object*s should now be in jobjects, so its safe to go into the main invoke code
937 // that performs allocations.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700938 JValue result = InvokeProxyInvocationHandler(soa, shorty, rcvr_jobj, interface_method_jobj, args);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800939 // Restore references which might have moved.
940 local_ref_visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -0700941 return result.GetJ();
942}
943
944// Read object references held in arguments from quick frames and place in a JNI local references,
945// so they don't get garbage collected.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800946class RememberForGcArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700947 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700948 RememberForGcArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty,
949 uint32_t shorty_len, ScopedObjectAccessUnchecked* soa) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700950 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700951
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700952 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Mathieu Chartier07d447b2013-09-26 11:57:43 -0700953
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700954 void FixupReferences() REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers848871b2013-08-05 10:56:33 -0700955
956 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700957 ScopedObjectAccessUnchecked* const soa_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800958 // References which we must update when exiting in case the GC moved the objects.
Andreas Gampec200a4a2014-06-16 18:39:09 -0700959 std::vector<std::pair<jobject, StackReference<mirror::Object>*> > references_;
960
Mathieu Chartier590fee92013-09-13 13:46:47 -0700961 DISALLOW_COPY_AND_ASSIGN(RememberForGcArgumentVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700962};
963
Ian Rogers9758f792014-03-13 09:02:55 -0700964void RememberForGcArgumentVisitor::Visit() {
965 if (IsParamAReference()) {
966 StackReference<mirror::Object>* stack_ref =
967 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
968 jobject reference =
969 soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
970 references_.push_back(std::make_pair(reference, stack_ref));
971 }
972}
973
974void RememberForGcArgumentVisitor::FixupReferences() {
975 // Fixup any references which may have changed.
976 for (const auto& pair : references_) {
Mathieu Chartier1a5337f2016-10-13 13:48:23 -0700977 pair.second->Assign(soa_->Decode<mirror::Object>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -0700978 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -0700979 }
980}
981
Ian Rogers848871b2013-08-05 10:56:33 -0700982// Lazily resolve a method for quick. Called by stub code.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700983extern "C" const void* artQuickResolutionTrampoline(
984 ArtMethod* called, mirror::Object* receiver, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700985 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe3b45ef22015-05-26 21:34:09 -0700986 // The resolution trampoline stashes the resolved method into the callee-save frame to transport
987 // it. Thus, when exiting, the stack cannot be verified (as the resolved method most likely
988 // does not have the same stack layout as the callee-save method).
989 ScopedQuickEntrypointChecks sqec(self, kIsDebugBuild, false);
Ian Rogers848871b2013-08-05 10:56:33 -0700990 // Start new JNI local reference state
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800991 JNIEnvExt* env = self->GetJniEnv();
Ian Rogers848871b2013-08-05 10:56:33 -0700992 ScopedObjectAccessUnchecked soa(env);
993 ScopedJniEnvLocalRefState env_state(env);
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800994 const char* old_cause = self->StartAssertNoThreadSuspension("Quick method resolution set up");
Ian Rogers848871b2013-08-05 10:56:33 -0700995
996 // Compute details about the called method (avoid GCs)
997 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Ian Rogers848871b2013-08-05 10:56:33 -0700998 InvokeType invoke_type;
Ian Rogerse0a02da2014-12-02 14:10:53 -0800999 MethodReference called_method(nullptr, 0);
1000 const bool called_method_known_on_entry = !called->IsRuntimeMethod();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001001 ArtMethod* caller = nullptr;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001002 if (!called_method_known_on_entry) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01001003 caller = QuickArgumentVisitor::GetCallingMethod(sp);
Ian Rogerse0a02da2014-12-02 14:10:53 -08001004 called_method.dex_file = caller->GetDexFile();
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001005
1006 InvokeType stack_map_invoke_type;
1007 uint32_t stack_map_dex_method_idx;
1008 const bool found_stack_map = QuickArgumentVisitor::GetInvokeType(sp,
1009 &stack_map_invoke_type,
1010 &stack_map_dex_method_idx);
1011 // For debug builds, we make sure both of the paths are consistent by also looking at the dex
1012 // code.
1013 if (!found_stack_map || kIsDebugBuild) {
1014 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
1015 const DexFile::CodeItem* code;
1016 code = caller->GetCodeItem();
1017 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
1018 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
1019 Instruction::Code instr_code = instr->Opcode();
1020 bool is_range;
1021 switch (instr_code) {
1022 case Instruction::INVOKE_DIRECT:
1023 invoke_type = kDirect;
1024 is_range = false;
1025 break;
1026 case Instruction::INVOKE_DIRECT_RANGE:
1027 invoke_type = kDirect;
1028 is_range = true;
1029 break;
1030 case Instruction::INVOKE_STATIC:
1031 invoke_type = kStatic;
1032 is_range = false;
1033 break;
1034 case Instruction::INVOKE_STATIC_RANGE:
1035 invoke_type = kStatic;
1036 is_range = true;
1037 break;
1038 case Instruction::INVOKE_SUPER:
1039 invoke_type = kSuper;
1040 is_range = false;
1041 break;
1042 case Instruction::INVOKE_SUPER_RANGE:
1043 invoke_type = kSuper;
1044 is_range = true;
1045 break;
1046 case Instruction::INVOKE_VIRTUAL:
1047 invoke_type = kVirtual;
1048 is_range = false;
1049 break;
1050 case Instruction::INVOKE_VIRTUAL_RANGE:
1051 invoke_type = kVirtual;
1052 is_range = true;
1053 break;
1054 case Instruction::INVOKE_INTERFACE:
1055 invoke_type = kInterface;
1056 is_range = false;
1057 break;
1058 case Instruction::INVOKE_INTERFACE_RANGE:
1059 invoke_type = kInterface;
1060 is_range = true;
1061 break;
1062 default:
1063 LOG(FATAL) << "Unexpected call into trampoline: " << instr->DumpString(nullptr);
1064 UNREACHABLE();
1065 }
1066 called_method.dex_method_index = (is_range) ? instr->VRegB_3rc() : instr->VRegB_35c();
1067 // Check that the invoke matches what we expected, note that this path only happens for debug
1068 // builds.
1069 if (found_stack_map) {
1070 DCHECK_EQ(stack_map_invoke_type, invoke_type);
1071 if (invoke_type != kSuper) {
1072 // Super may be sharpened.
1073 DCHECK_EQ(stack_map_dex_method_idx, called_method.dex_method_index)
1074 << called_method.dex_file->PrettyMethod(stack_map_dex_method_idx) << " "
1075 << called_method.dex_file->PrettyMethod(called_method.dex_method_index);
1076 }
1077 } else {
Andreas Gampe9e6dee22017-04-11 13:50:23 -07001078 VLOG(dex) << "Accessed dex file for invoke " << invoke_type << " "
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001079 << called_method.dex_method_index;
1080 }
1081 } else {
1082 invoke_type = stack_map_invoke_type;
1083 called_method.dex_method_index = stack_map_dex_method_idx;
Ian Rogers848871b2013-08-05 10:56:33 -07001084 }
Ian Rogers848871b2013-08-05 10:56:33 -07001085 } else {
1086 invoke_type = kStatic;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001087 called_method.dex_file = called->GetDexFile();
1088 called_method.dex_method_index = called->GetDexMethodIndex();
Ian Rogers848871b2013-08-05 10:56:33 -07001089 }
1090 uint32_t shorty_len;
1091 const char* shorty =
Ian Rogerse0a02da2014-12-02 14:10:53 -08001092 called_method.dex_file->GetMethodShorty(
1093 called_method.dex_file->GetMethodId(called_method.dex_method_index), &shorty_len);
Mathieu Chartier590fee92013-09-13 13:46:47 -07001094 RememberForGcArgumentVisitor visitor(sp, invoke_type == kStatic, shorty, shorty_len, &soa);
Ian Rogers848871b2013-08-05 10:56:33 -07001095 visitor.VisitArguments();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001096 self->EndAssertNoThreadSuspension(old_cause);
Ian Rogerse0a02da2014-12-02 14:10:53 -08001097 const bool virtual_or_interface = invoke_type == kVirtual || invoke_type == kInterface;
Ian Rogers848871b2013-08-05 10:56:33 -07001098 // Resolve method filling in dex cache.
Ian Rogerse0a02da2014-12-02 14:10:53 -08001099 if (!called_method_known_on_entry) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001100 StackHandleScope<1> hs(self);
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001101 mirror::Object* dummy = nullptr;
1102 HandleWrapper<mirror::Object> h_receiver(
1103 hs.NewHandleWrapper(virtual_or_interface ? &receiver : &dummy));
Ian Rogerse0a02da2014-12-02 14:10:53 -08001104 DCHECK_EQ(caller->GetDexFile(), called_method.dex_file);
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08001105 called = linker->ResolveMethod<ClassLinker::kForceICCECheck>(
1106 self, called_method.dex_method_index, caller, invoke_type);
Ian Rogers848871b2013-08-05 10:56:33 -07001107 }
Ian Rogerse0a02da2014-12-02 14:10:53 -08001108 const void* code = nullptr;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001109 if (LIKELY(!self->IsExceptionPending())) {
Ian Rogers848871b2013-08-05 10:56:33 -07001110 // Incompatible class change should have been handled in resolve method.
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001111 CHECK(!called->CheckIncompatibleClassChange(invoke_type))
David Sehr709b0702016-10-13 09:12:37 -07001112 << called->PrettyMethod() << " " << invoke_type;
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001113 if (virtual_or_interface || invoke_type == kSuper) {
1114 // Refine called method based on receiver for kVirtual/kInterface, and
1115 // caller for kSuper.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001116 ArtMethod* orig_called = called;
Mathieu Chartier55871bf2014-02-27 10:24:50 -08001117 if (invoke_type == kVirtual) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001118 CHECK(receiver != nullptr) << invoke_type;
Andreas Gampe542451c2016-07-26 09:02:02 -07001119 called = receiver->GetClass()->FindVirtualMethodForVirtual(called, kRuntimePointerSize);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001120 } else if (invoke_type == kInterface) {
1121 CHECK(receiver != nullptr) << invoke_type;
Andreas Gampe542451c2016-07-26 09:02:02 -07001122 called = receiver->GetClass()->FindVirtualMethodForInterface(called, kRuntimePointerSize);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001123 } else {
1124 DCHECK_EQ(invoke_type, kSuper);
1125 CHECK(caller != nullptr) << invoke_type;
Nicolas Geoffray393fdb82016-04-25 14:58:06 +01001126 StackHandleScope<2> hs(self);
1127 Handle<mirror::DexCache> dex_cache(
1128 hs.NewHandle(caller->GetDeclaringClass()->GetDexCache()));
1129 Handle<mirror::ClassLoader> class_loader(
1130 hs.NewHandle(caller->GetDeclaringClass()->GetClassLoader()));
Alex Lightfedd91d2016-01-07 14:49:16 -08001131 // TODO Maybe put this into a mirror::Class function.
1132 mirror::Class* ref_class = linker->ResolveReferencedClassOfMethod(
Nicolas Geoffray393fdb82016-04-25 14:58:06 +01001133 called_method.dex_method_index, dex_cache, class_loader);
Alex Lightfedd91d2016-01-07 14:49:16 -08001134 if (ref_class->IsInterface()) {
Andreas Gampe542451c2016-07-26 09:02:02 -07001135 called = ref_class->FindVirtualMethodForInterfaceSuper(called, kRuntimePointerSize);
Alex Lightfedd91d2016-01-07 14:49:16 -08001136 } else {
1137 called = caller->GetDeclaringClass()->GetSuperClass()->GetVTableEntry(
Andreas Gampe542451c2016-07-26 09:02:02 -07001138 called->GetMethodIndex(), kRuntimePointerSize);
Alex Lightfedd91d2016-01-07 14:49:16 -08001139 }
Mathieu Chartier55871bf2014-02-27 10:24:50 -08001140 }
Mingyao Yangf4867782014-05-05 11:55:02 -07001141
David Sehr709b0702016-10-13 09:12:37 -07001142 CHECK(called != nullptr) << orig_called->PrettyMethod() << " "
1143 << mirror::Object::PrettyTypeOf(receiver) << " "
Mingyao Yangf4867782014-05-05 11:55:02 -07001144 << invoke_type << " " << orig_called->GetVtableIndex();
1145
Ian Rogers83883d72013-10-21 21:07:24 -07001146 // 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 -08001147 // of the sharpened method avoiding dirtying the dex cache if possible.
Ian Rogers00f15272014-12-02 16:55:46 -08001148 // Note, called_method.dex_method_index references the dex method before the
1149 // FindVirtualMethodFor... This is ok for FindDexMethodIndexInOtherDexFile that only cares
1150 // about the name and signature.
1151 uint32_t update_dex_cache_method_index = called->GetDexMethodIndex();
Andreas Gampe542451c2016-07-26 09:02:02 -07001152 if (!called->HasSameDexCacheResolvedMethods(caller, kRuntimePointerSize)) {
Ian Rogers83883d72013-10-21 21:07:24 -07001153 // Calling from one dex file to another, need to compute the method index appropriate to
Vladimir Markobbcc0c02014-02-03 14:08:42 +00001154 // the caller's dex file. Since we get here only if the original called was a runtime
1155 // method, we've got the correct dex_file and a dex_method_idx from above.
Ian Rogerse0a02da2014-12-02 14:10:53 -08001156 DCHECK(!called_method_known_on_entry);
1157 DCHECK_EQ(caller->GetDexFile(), called_method.dex_file);
1158 const DexFile* caller_dex_file = called_method.dex_file;
1159 uint32_t caller_method_name_and_sig_index = called_method.dex_method_index;
1160 update_dex_cache_method_index =
1161 called->FindDexMethodIndexInOtherDexFile(*caller_dex_file,
1162 caller_method_name_and_sig_index);
1163 }
1164 if ((update_dex_cache_method_index != DexFile::kDexNoIndex) &&
Mathieu Chartiere401d142015-04-22 13:56:20 -07001165 (caller->GetDexCacheResolvedMethod(
Andreas Gampe542451c2016-07-26 09:02:02 -07001166 update_dex_cache_method_index, kRuntimePointerSize) != called)) {
1167 caller->SetDexCacheResolvedMethod(update_dex_cache_method_index,
1168 called,
1169 kRuntimePointerSize);
Ian Rogers83883d72013-10-21 21:07:24 -07001170 }
Mathieu Chartiere4a91bb2015-01-28 13:11:44 -08001171 } else if (invoke_type == kStatic) {
1172 const auto called_dex_method_idx = called->GetDexMethodIndex();
1173 // For static invokes, we may dispatch to the static method in the superclass but resolve
1174 // using the subclass. To prevent getting slow paths on each invoke, we force set the
1175 // resolved method for the super class dex method index if we are in the same dex file.
1176 // b/19175856
1177 if (called->GetDexFile() == called_method.dex_file &&
1178 called_method.dex_method_index != called_dex_method_idx) {
Andreas Gampe542451c2016-07-26 09:02:02 -07001179 called->GetDexCache()->SetResolvedMethod(called_dex_method_idx,
1180 called,
1181 kRuntimePointerSize);
Mathieu Chartiere4a91bb2015-01-28 13:11:44 -08001182 }
Ian Rogers83883d72013-10-21 21:07:24 -07001183 }
Daniel Mihalyieb076692014-08-22 17:33:31 +02001184
Ian Rogers848871b2013-08-05 10:56:33 -07001185 // Ensure that the called method's class is initialized.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001186 StackHandleScope<1> hs(soa.Self());
1187 Handle<mirror::Class> called_class(hs.NewHandle(called->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -07001188 linker->EnsureInitialized(soa.Self(), called_class, true, true);
Ian Rogers848871b2013-08-05 10:56:33 -07001189 if (LIKELY(called_class->IsInitialized())) {
Daniel Mihalyieb076692014-08-22 17:33:31 +02001190 if (UNLIKELY(Dbg::IsForcedInterpreterNeededForResolution(self, called))) {
1191 // If we are single-stepping or the called method is deoptimized (by a
1192 // breakpoint, for example), then we have to execute the called method
1193 // with the interpreter.
1194 code = GetQuickToInterpreterBridge();
1195 } else if (UNLIKELY(Dbg::IsForcedInstrumentationNeededForResolution(self, caller))) {
1196 // If the caller is deoptimized (by a breakpoint, for example), we have to
1197 // continue its execution with interpreter when returning from the called
1198 // method. Because we do not want to execute the called method with the
1199 // interpreter, we wrap its execution into the instrumentation stubs.
1200 // When the called method returns, it will execute the instrumentation
1201 // exit hook that will determine the need of the interpreter with a call
1202 // to Dbg::IsForcedInterpreterNeededForUpcall and deoptimize the stack if
1203 // it is needed.
1204 code = GetQuickInstrumentationEntryPoint();
1205 } else {
1206 code = called->GetEntryPointFromQuickCompiledCode();
1207 }
Ian Rogers848871b2013-08-05 10:56:33 -07001208 } else if (called_class->IsInitializing()) {
Daniel Mihalyieb076692014-08-22 17:33:31 +02001209 if (UNLIKELY(Dbg::IsForcedInterpreterNeededForResolution(self, called))) {
1210 // If we are single-stepping or the called method is deoptimized (by a
1211 // breakpoint, for example), then we have to execute the called method
1212 // with the interpreter.
1213 code = GetQuickToInterpreterBridge();
1214 } else if (invoke_type == kStatic) {
Ian Rogers848871b2013-08-05 10:56:33 -07001215 // Class is still initializing, go to oat and grab code (trampoline must be left in place
1216 // until class is initialized to stop races between threads).
Ian Rogersef7d42f2014-01-06 12:55:46 -08001217 code = linker->GetQuickOatCodeFor(called);
Ian Rogers848871b2013-08-05 10:56:33 -07001218 } else {
1219 // No trampoline for non-static methods.
Ian Rogersef7d42f2014-01-06 12:55:46 -08001220 code = called->GetEntryPointFromQuickCompiledCode();
Ian Rogers848871b2013-08-05 10:56:33 -07001221 }
1222 } else {
1223 DCHECK(called_class->IsErroneous());
1224 }
1225 }
Ian Rogerse0a02da2014-12-02 14:10:53 -08001226 CHECK_EQ(code == nullptr, self->IsExceptionPending());
Mathieu Chartier07d447b2013-09-26 11:57:43 -07001227 // Fixup any locally saved objects may have moved during a GC.
1228 visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -07001229 // Place called method in callee-save frame to be placed as first argument to quick method.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001230 *sp = called;
1231
Ian Rogers848871b2013-08-05 10:56:33 -07001232 return code;
1233}
1234
Andreas Gampec147b002014-03-06 18:11:06 -08001235/*
1236 * This class uses a couple of observations to unite the different calling conventions through
1237 * a few constants.
1238 *
1239 * 1) Number of registers used for passing is normally even, so counting down has no penalty for
1240 * possible alignment.
1241 * 2) Known 64b architectures store 8B units on the stack, both for integral and floating point
1242 * types, so using uintptr_t is OK. Also means that we can use kRegistersNeededX to denote
1243 * when we have to split things
1244 * 3) The only soft-float, Arm, is 32b, so no widening needs to be taken into account for floats
1245 * and we can use Int handling directly.
1246 * 4) Only 64b architectures widen, and their stack is aligned 8B anyways, so no padding code
1247 * necessary when widening. Also, widening of Ints will take place implicitly, and the
1248 * extension should be compatible with Aarch64, which mandates copying the available bits
1249 * into LSB and leaving the rest unspecified.
1250 * 5) Aligning longs and doubles is necessary on arm only, and it's the same in registers and on
1251 * the stack.
1252 * 6) There is only little endian.
1253 *
1254 *
1255 * Actual work is supposed to be done in a delegate of the template type. The interface is as
1256 * follows:
1257 *
1258 * void PushGpr(uintptr_t): Add a value for the next GPR
1259 *
1260 * void PushFpr4(float): Add a value for the next FPR of size 32b. Is only called if we need
1261 * padding, that is, think the architecture is 32b and aligns 64b.
1262 *
1263 * void PushFpr8(uint64_t): Push a double. We _will_ call this on 32b, it's the callee's job to
1264 * split this if necessary. The current state will have aligned, if
1265 * necessary.
1266 *
1267 * void PushStack(uintptr_t): Push a value to the stack.
1268 *
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001269 * uintptr_t PushHandleScope(mirror::Object* ref): Add a reference to the HandleScope. This _will_ have nullptr,
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001270 * as this might be important for null initialization.
Andreas Gampec147b002014-03-06 18:11:06 -08001271 * Must return the jobject, that is, the reference to the
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001272 * entry in the HandleScope (nullptr if necessary).
Andreas Gampec147b002014-03-06 18:11:06 -08001273 *
1274 */
Andreas Gampec200a4a2014-06-16 18:39:09 -07001275template<class T> class BuildNativeCallFrameStateMachine {
Andreas Gampec147b002014-03-06 18:11:06 -08001276 public:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001277#if defined(__arm__)
1278 // TODO: These are all dummy values!
Andreas Gampec147b002014-03-06 18:11:06 -08001279 static constexpr bool kNativeSoftFloatAbi = true;
1280 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs, r0-r3
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001281 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
1282
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001283 static constexpr size_t kRegistersNeededForLong = 2;
1284 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -08001285 static constexpr bool kMultiRegistersAligned = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001286 static constexpr bool kMultiFPRegistersWidened = false;
1287 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001288 static constexpr bool kAlignLongOnStack = true;
1289 static constexpr bool kAlignDoubleOnStack = true;
Stuart Monteithb95a5342014-03-12 13:32:32 +00001290#elif defined(__aarch64__)
1291 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
1292 static constexpr size_t kNumNativeGprArgs = 8; // 6 arguments passed in GPRs.
1293 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
1294
1295 static constexpr size_t kRegistersNeededForLong = 1;
1296 static constexpr size_t kRegistersNeededForDouble = 1;
1297 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001298 static constexpr bool kMultiFPRegistersWidened = false;
1299 static constexpr bool kMultiGPRegistersWidened = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +00001300 static constexpr bool kAlignLongOnStack = false;
1301 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001302#elif defined(__mips__) && !defined(__LP64__)
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001303 static constexpr bool kNativeSoftFloatAbi = true; // This is a hard float ABI.
Douglas Leung735b8552014-10-31 12:21:40 -07001304 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs.
1305 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001306
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001307 static constexpr size_t kRegistersNeededForLong = 2;
1308 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -08001309 static constexpr bool kMultiRegistersAligned = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001310 static constexpr bool kMultiFPRegistersWidened = true;
1311 static constexpr bool kMultiGPRegistersWidened = false;
Douglas Leung735b8552014-10-31 12:21:40 -07001312 static constexpr bool kAlignLongOnStack = true;
1313 static constexpr bool kAlignDoubleOnStack = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001314#elif defined(__mips__) && defined(__LP64__)
1315 // Let the code prepare GPRs only and we will load the FPRs with same data.
1316 static constexpr bool kNativeSoftFloatAbi = true;
1317 static constexpr size_t kNumNativeGprArgs = 8;
1318 static constexpr size_t kNumNativeFprArgs = 0;
1319
1320 static constexpr size_t kRegistersNeededForLong = 1;
1321 static constexpr size_t kRegistersNeededForDouble = 1;
1322 static constexpr bool kMultiRegistersAligned = false;
1323 static constexpr bool kMultiFPRegistersWidened = false;
1324 static constexpr bool kMultiGPRegistersWidened = true;
1325 static constexpr bool kAlignLongOnStack = false;
1326 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001327#elif defined(__i386__)
1328 // TODO: Check these!
Andreas Gampec147b002014-03-06 18:11:06 -08001329 static constexpr bool kNativeSoftFloatAbi = false; // Not using int registers for fp
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001330 static constexpr size_t kNumNativeGprArgs = 0; // 6 arguments passed in GPRs.
1331 static constexpr size_t kNumNativeFprArgs = 0; // 8 arguments passed in FPRs.
1332
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001333 static constexpr size_t kRegistersNeededForLong = 2;
1334 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001335 static constexpr bool kMultiRegistersAligned = false; // x86 not using regs, anyways
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001336 static constexpr bool kMultiFPRegistersWidened = false;
1337 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001338 static constexpr bool kAlignLongOnStack = false;
1339 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001340#elif defined(__x86_64__)
1341 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
1342 static constexpr size_t kNumNativeGprArgs = 6; // 6 arguments passed in GPRs.
1343 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
1344
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001345 static constexpr size_t kRegistersNeededForLong = 1;
1346 static constexpr size_t kRegistersNeededForDouble = 1;
Andreas Gampec147b002014-03-06 18:11:06 -08001347 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001348 static constexpr bool kMultiFPRegistersWidened = false;
1349 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001350 static constexpr bool kAlignLongOnStack = false;
1351 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001352#else
1353#error "Unsupported architecture"
1354#endif
1355
Andreas Gampec147b002014-03-06 18:11:06 -08001356 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001357 explicit BuildNativeCallFrameStateMachine(T* delegate)
1358 : gpr_index_(kNumNativeGprArgs),
1359 fpr_index_(kNumNativeFprArgs),
1360 stack_entries_(0),
1361 delegate_(delegate) {
Andreas Gampec147b002014-03-06 18:11:06 -08001362 // For register alignment, we want to assume that counters (gpr_index_, fpr_index_) are even iff
1363 // the next register is even; counting down is just to make the compiler happy...
Andreas Gampe575e78c2014-11-03 23:41:03 -08001364 static_assert(kNumNativeGprArgs % 2 == 0U, "Number of native GPR arguments not even");
1365 static_assert(kNumNativeFprArgs % 2 == 0U, "Number of native FPR arguments not even");
Andreas Gampec147b002014-03-06 18:11:06 -08001366 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001367
Andreas Gampec200a4a2014-06-16 18:39:09 -07001368 virtual ~BuildNativeCallFrameStateMachine() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001369
Ian Rogers1428dce2014-10-21 15:02:15 -07001370 bool HavePointerGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001371 return gpr_index_ > 0;
1372 }
1373
Andreas Gampec200a4a2014-06-16 18:39:09 -07001374 void AdvancePointer(const void* val) {
Andreas Gampec147b002014-03-06 18:11:06 -08001375 if (HavePointerGpr()) {
1376 gpr_index_--;
1377 PushGpr(reinterpret_cast<uintptr_t>(val));
1378 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001379 stack_entries_++; // TODO: have a field for pointer length as multiple of 32b
Andreas Gampec147b002014-03-06 18:11:06 -08001380 PushStack(reinterpret_cast<uintptr_t>(val));
1381 gpr_index_ = 0;
1382 }
1383 }
1384
Ian Rogers1428dce2014-10-21 15:02:15 -07001385 bool HaveHandleScopeGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001386 return gpr_index_ > 0;
1387 }
1388
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001389 void AdvanceHandleScope(mirror::Object* ptr) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001390 uintptr_t handle = PushHandle(ptr);
1391 if (HaveHandleScopeGpr()) {
Andreas Gampec147b002014-03-06 18:11:06 -08001392 gpr_index_--;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001393 PushGpr(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001394 } else {
1395 stack_entries_++;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001396 PushStack(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001397 gpr_index_ = 0;
1398 }
1399 }
1400
Ian Rogers1428dce2014-10-21 15:02:15 -07001401 bool HaveIntGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001402 return gpr_index_ > 0;
1403 }
1404
1405 void AdvanceInt(uint32_t val) {
1406 if (HaveIntGpr()) {
1407 gpr_index_--;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001408 if (kMultiGPRegistersWidened) {
1409 DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001410 PushGpr(static_cast<int64_t>(bit_cast<int32_t, uint32_t>(val)));
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001411 } else {
1412 PushGpr(val);
1413 }
Andreas Gampec147b002014-03-06 18:11:06 -08001414 } else {
1415 stack_entries_++;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001416 if (kMultiGPRegistersWidened) {
1417 DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001418 PushStack(static_cast<int64_t>(bit_cast<int32_t, uint32_t>(val)));
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001419 } else {
1420 PushStack(val);
1421 }
Andreas Gampec147b002014-03-06 18:11:06 -08001422 gpr_index_ = 0;
1423 }
1424 }
1425
Ian Rogers1428dce2014-10-21 15:02:15 -07001426 bool HaveLongGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001427 return gpr_index_ >= kRegistersNeededForLong + (LongGprNeedsPadding() ? 1 : 0);
1428 }
1429
Ian Rogers1428dce2014-10-21 15:02:15 -07001430 bool LongGprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001431 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1432 kAlignLongOnStack && // and when it needs alignment
1433 (gpr_index_ & 1) == 1; // counter is odd, see constructor
1434 }
1435
Ian Rogers1428dce2014-10-21 15:02:15 -07001436 bool LongStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001437 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1438 kAlignLongOnStack && // and when it needs 8B alignment
1439 (stack_entries_ & 1) == 1; // counter is odd
1440 }
1441
1442 void AdvanceLong(uint64_t val) {
1443 if (HaveLongGpr()) {
1444 if (LongGprNeedsPadding()) {
1445 PushGpr(0);
1446 gpr_index_--;
1447 }
1448 if (kRegistersNeededForLong == 1) {
1449 PushGpr(static_cast<uintptr_t>(val));
1450 } else {
1451 PushGpr(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1452 PushGpr(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1453 }
1454 gpr_index_ -= kRegistersNeededForLong;
1455 } else {
1456 if (LongStackNeedsPadding()) {
1457 PushStack(0);
1458 stack_entries_++;
1459 }
1460 if (kRegistersNeededForLong == 1) {
1461 PushStack(static_cast<uintptr_t>(val));
1462 stack_entries_++;
1463 } else {
1464 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1465 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1466 stack_entries_ += 2;
1467 }
1468 gpr_index_ = 0;
1469 }
1470 }
1471
Ian Rogers1428dce2014-10-21 15:02:15 -07001472 bool HaveFloatFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001473 return fpr_index_ > 0;
1474 }
1475
Andreas Gampec147b002014-03-06 18:11:06 -08001476 void AdvanceFloat(float val) {
1477 if (kNativeSoftFloatAbi) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001478 AdvanceInt(bit_cast<uint32_t, float>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001479 } else {
1480 if (HaveFloatFpr()) {
1481 fpr_index_--;
1482 if (kRegistersNeededForDouble == 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001483 if (kMultiFPRegistersWidened) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001484 PushFpr8(bit_cast<uint64_t, double>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001485 } else {
1486 // No widening, just use the bits.
Roland Levillainda4d79b2015-03-24 14:36:11 +00001487 PushFpr8(static_cast<uint64_t>(bit_cast<uint32_t, float>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001488 }
1489 } else {
1490 PushFpr4(val);
1491 }
1492 } else {
1493 stack_entries_++;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001494 if (kRegistersNeededForDouble == 1 && kMultiFPRegistersWidened) {
Andreas Gampec147b002014-03-06 18:11:06 -08001495 // Need to widen before storing: Note the "double" in the template instantiation.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001496 // Note: We need to jump through those hoops to make the compiler happy.
1497 DCHECK_EQ(sizeof(uintptr_t), sizeof(uint64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001498 PushStack(static_cast<uintptr_t>(bit_cast<uint64_t, double>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001499 } else {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001500 PushStack(static_cast<uintptr_t>(bit_cast<uint32_t, float>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001501 }
1502 fpr_index_ = 0;
1503 }
1504 }
1505 }
1506
Ian Rogers1428dce2014-10-21 15:02:15 -07001507 bool HaveDoubleFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001508 return fpr_index_ >= kRegistersNeededForDouble + (DoubleFprNeedsPadding() ? 1 : 0);
1509 }
1510
Ian Rogers1428dce2014-10-21 15:02:15 -07001511 bool DoubleFprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001512 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1513 kAlignDoubleOnStack && // and when it needs alignment
1514 (fpr_index_ & 1) == 1; // counter is odd, see constructor
1515 }
1516
Ian Rogers1428dce2014-10-21 15:02:15 -07001517 bool DoubleStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001518 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1519 kAlignDoubleOnStack && // and when it needs 8B alignment
1520 (stack_entries_ & 1) == 1; // counter is odd
1521 }
1522
1523 void AdvanceDouble(uint64_t val) {
1524 if (kNativeSoftFloatAbi) {
1525 AdvanceLong(val);
1526 } else {
1527 if (HaveDoubleFpr()) {
1528 if (DoubleFprNeedsPadding()) {
1529 PushFpr4(0);
1530 fpr_index_--;
1531 }
1532 PushFpr8(val);
1533 fpr_index_ -= kRegistersNeededForDouble;
1534 } else {
1535 if (DoubleStackNeedsPadding()) {
1536 PushStack(0);
1537 stack_entries_++;
1538 }
1539 if (kRegistersNeededForDouble == 1) {
1540 PushStack(static_cast<uintptr_t>(val));
1541 stack_entries_++;
1542 } else {
1543 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1544 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1545 stack_entries_ += 2;
1546 }
1547 fpr_index_ = 0;
1548 }
1549 }
1550 }
1551
Ian Rogers1428dce2014-10-21 15:02:15 -07001552 uint32_t GetStackEntries() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001553 return stack_entries_;
1554 }
1555
Ian Rogers1428dce2014-10-21 15:02:15 -07001556 uint32_t GetNumberOfUsedGprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001557 return kNumNativeGprArgs - gpr_index_;
1558 }
1559
Ian Rogers1428dce2014-10-21 15:02:15 -07001560 uint32_t GetNumberOfUsedFprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001561 return kNumNativeFprArgs - fpr_index_;
1562 }
1563
1564 private:
1565 void PushGpr(uintptr_t val) {
1566 delegate_->PushGpr(val);
1567 }
1568 void PushFpr4(float val) {
1569 delegate_->PushFpr4(val);
1570 }
1571 void PushFpr8(uint64_t val) {
1572 delegate_->PushFpr8(val);
1573 }
1574 void PushStack(uintptr_t val) {
1575 delegate_->PushStack(val);
1576 }
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001577 uintptr_t PushHandle(mirror::Object* ref) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001578 return delegate_->PushHandle(ref);
Andreas Gampec147b002014-03-06 18:11:06 -08001579 }
1580
1581 uint32_t gpr_index_; // Number of free GPRs
1582 uint32_t fpr_index_; // Number of free FPRs
1583 uint32_t stack_entries_; // Stack entries are in multiples of 32b, as floats are usually not
1584 // extended
Ian Rogers1428dce2014-10-21 15:02:15 -07001585 T* const delegate_; // What Push implementation gets called
Andreas Gampec147b002014-03-06 18:11:06 -08001586};
1587
Andreas Gampec200a4a2014-06-16 18:39:09 -07001588// Computes the sizes of register stacks and call stack area. Handling of references can be extended
1589// in subclasses.
1590//
1591// To handle native pointers, use "L" in the shorty for an object reference, which simulates
1592// them with handles.
1593class ComputeNativeCallFrameSize {
Andreas Gampec147b002014-03-06 18:11:06 -08001594 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001595 ComputeNativeCallFrameSize() : num_stack_entries_(0) {}
1596
1597 virtual ~ComputeNativeCallFrameSize() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001598
Ian Rogers1428dce2014-10-21 15:02:15 -07001599 uint32_t GetStackSize() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001600 return num_stack_entries_ * sizeof(uintptr_t);
1601 }
1602
Ian Rogers1428dce2014-10-21 15:02:15 -07001603 uint8_t* LayoutCallStack(uint8_t* sp8) const {
Andreas Gampec147b002014-03-06 18:11:06 -08001604 sp8 -= GetStackSize();
Andreas Gampe779f8c92014-06-09 18:29:38 -07001605 // Align by kStackAlignment.
1606 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
Andreas Gampec200a4a2014-06-16 18:39:09 -07001607 return sp8;
Andreas Gampec147b002014-03-06 18:11:06 -08001608 }
1609
Ian Rogers1428dce2014-10-21 15:02:15 -07001610 uint8_t* LayoutCallRegisterStacks(uint8_t* sp8, uintptr_t** start_gpr, uint32_t** start_fpr)
1611 const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001612 // Assumption is OK right now, as we have soft-float arm
1613 size_t fregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeFprArgs;
1614 sp8 -= fregs * sizeof(uintptr_t);
1615 *start_fpr = reinterpret_cast<uint32_t*>(sp8);
1616 size_t iregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeGprArgs;
1617 sp8 -= iregs * sizeof(uintptr_t);
1618 *start_gpr = reinterpret_cast<uintptr_t*>(sp8);
1619 return sp8;
1620 }
Andreas Gampec147b002014-03-06 18:11:06 -08001621
Andreas Gampec200a4a2014-06-16 18:39:09 -07001622 uint8_t* LayoutNativeCall(uint8_t* sp8, uintptr_t** start_stack, uintptr_t** start_gpr,
Ian Rogers1428dce2014-10-21 15:02:15 -07001623 uint32_t** start_fpr) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001624 // Native call stack.
1625 sp8 = LayoutCallStack(sp8);
1626 *start_stack = reinterpret_cast<uintptr_t*>(sp8);
Andreas Gampec147b002014-03-06 18:11:06 -08001627
Andreas Gampec200a4a2014-06-16 18:39:09 -07001628 // Put fprs and gprs below.
1629 sp8 = LayoutCallRegisterStacks(sp8, start_gpr, start_fpr);
Andreas Gampec147b002014-03-06 18:11:06 -08001630
Andreas Gampec200a4a2014-06-16 18:39:09 -07001631 // Return the new bottom.
1632 return sp8;
1633 }
1634
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001635 virtual void WalkHeader(
1636 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm ATTRIBUTE_UNUSED)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001637 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001638 }
Andreas Gampec200a4a2014-06-16 18:39:09 -07001639
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001640 void Walk(const char* shorty, uint32_t shorty_len) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001641 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize> sm(this);
1642
1643 WalkHeader(&sm);
Andreas Gampec147b002014-03-06 18:11:06 -08001644
1645 for (uint32_t i = 1; i < shorty_len; ++i) {
1646 Primitive::Type cur_type_ = Primitive::GetType(shorty[i]);
1647 switch (cur_type_) {
1648 case Primitive::kPrimNot:
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001649 // TODO: fix abuse of mirror types.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001650 sm.AdvanceHandleScope(
1651 reinterpret_cast<mirror::Object*>(0x12345678));
Andreas Gampec147b002014-03-06 18:11:06 -08001652 break;
1653
1654 case Primitive::kPrimBoolean:
1655 case Primitive::kPrimByte:
1656 case Primitive::kPrimChar:
1657 case Primitive::kPrimShort:
1658 case Primitive::kPrimInt:
1659 sm.AdvanceInt(0);
1660 break;
1661 case Primitive::kPrimFloat:
1662 sm.AdvanceFloat(0);
1663 break;
1664 case Primitive::kPrimDouble:
1665 sm.AdvanceDouble(0);
1666 break;
1667 case Primitive::kPrimLong:
1668 sm.AdvanceLong(0);
1669 break;
1670 default:
1671 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001672 UNREACHABLE();
Andreas Gampec147b002014-03-06 18:11:06 -08001673 }
1674 }
1675
Ian Rogers1428dce2014-10-21 15:02:15 -07001676 num_stack_entries_ = sm.GetStackEntries();
Andreas Gampec147b002014-03-06 18:11:06 -08001677 }
1678
1679 void PushGpr(uintptr_t /* val */) {
1680 // not optimizing registers, yet
1681 }
1682
1683 void PushFpr4(float /* val */) {
1684 // not optimizing registers, yet
1685 }
1686
1687 void PushFpr8(uint64_t /* val */) {
1688 // not optimizing registers, yet
1689 }
1690
1691 void PushStack(uintptr_t /* val */) {
1692 // counting is already done in the superclass
1693 }
1694
Andreas Gampec200a4a2014-06-16 18:39:09 -07001695 virtual uintptr_t PushHandle(mirror::Object* /* ptr */) {
Andreas Gampec147b002014-03-06 18:11:06 -08001696 return reinterpret_cast<uintptr_t>(nullptr);
1697 }
1698
Andreas Gampec200a4a2014-06-16 18:39:09 -07001699 protected:
Andreas Gampec147b002014-03-06 18:11:06 -08001700 uint32_t num_stack_entries_;
1701};
1702
Andreas Gampec200a4a2014-06-16 18:39:09 -07001703class ComputeGenericJniFrameSize FINAL : public ComputeNativeCallFrameSize {
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001704 public:
Igor Murashkin06a04e02016-09-13 15:57:37 -07001705 explicit ComputeGenericJniFrameSize(bool critical_native)
1706 : num_handle_scope_references_(0), critical_native_(critical_native) {}
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001707
Andreas Gampec200a4a2014-06-16 18:39:09 -07001708 // Lays out the callee-save frame. Assumes that the incorrect frame corresponding to RefsAndArgs
1709 // is at *m = sp. Will update to point to the bottom of the save frame.
1710 //
1711 // Note: assumes ComputeAll() has been run before.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001712 void LayoutCalleeSaveFrame(Thread* self, ArtMethod*** m, void* sp, HandleScope** handle_scope)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001713 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001714 ArtMethod* method = **m;
1715
Andreas Gampe542451c2016-07-26 09:02:02 -07001716 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001717
Andreas Gampec200a4a2014-06-16 18:39:09 -07001718 uint8_t* sp8 = reinterpret_cast<uint8_t*>(sp);
1719
1720 // First, fix up the layout of the callee-save frame.
1721 // We have to squeeze in the HandleScope, and relocate the method pointer.
1722
1723 // "Free" the slot for the method.
Ian Rogers13735952014-10-08 12:43:28 -07001724 sp8 += sizeof(void*); // In the callee-save frame we use a full pointer.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001725
1726 // Under the callee saves put handle scope and new method stack reference.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001727 size_t handle_scope_size = HandleScope::SizeOf(num_handle_scope_references_);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001728 size_t scope_and_method = handle_scope_size + sizeof(ArtMethod*);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001729
1730 sp8 -= scope_and_method;
1731 // Align by kStackAlignment.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001732 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
Andreas Gampec200a4a2014-06-16 18:39:09 -07001733
Mathieu Chartiere401d142015-04-22 13:56:20 -07001734 uint8_t* sp8_table = sp8 + sizeof(ArtMethod*);
Ian Rogers59c07062014-10-10 13:03:39 -07001735 *handle_scope = HandleScope::Create(sp8_table, self->GetTopHandleScope(),
1736 num_handle_scope_references_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001737
1738 // Add a slot for the method pointer, and fill it. Fix the pointer-pointer given to us.
1739 uint8_t* method_pointer = sp8;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001740 auto** new_method_ref = reinterpret_cast<ArtMethod**>(method_pointer);
1741 *new_method_ref = method;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001742 *m = new_method_ref;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001743 }
1744
Andreas Gampec200a4a2014-06-16 18:39:09 -07001745 // Adds space for the cookie. Note: may leave stack unaligned.
Ian Rogers1428dce2014-10-21 15:02:15 -07001746 void LayoutCookie(uint8_t** sp) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001747 // Reference cookie and padding
1748 *sp -= 8;
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001749 }
1750
Andreas Gampec200a4a2014-06-16 18:39:09 -07001751 // Re-layout the callee-save frame (insert a handle-scope). Then add space for the cookie.
1752 // Returns the new bottom. Note: this may be unaligned.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001753 uint8_t* LayoutJNISaveFrame(Thread* self, ArtMethod*** m, void* sp, HandleScope** handle_scope)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001754 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001755 // First, fix up the layout of the callee-save frame.
1756 // We have to squeeze in the HandleScope, and relocate the method pointer.
Ian Rogers59c07062014-10-10 13:03:39 -07001757 LayoutCalleeSaveFrame(self, m, sp, handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001758
1759 // The bottom of the callee-save frame is now where the method is, *m.
1760 uint8_t* sp8 = reinterpret_cast<uint8_t*>(*m);
1761
1762 // Add space for cookie.
1763 LayoutCookie(&sp8);
1764
1765 return sp8;
1766 }
1767
1768 // WARNING: After this, *sp won't be pointing to the method anymore!
Mathieu Chartiere401d142015-04-22 13:56:20 -07001769 uint8_t* ComputeLayout(Thread* self, ArtMethod*** m, const char* shorty, uint32_t shorty_len,
1770 HandleScope** handle_scope, uintptr_t** start_stack, uintptr_t** start_gpr,
1771 uint32_t** start_fpr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001772 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001773 Walk(shorty, shorty_len);
1774
1775 // JNI part.
Ian Rogers59c07062014-10-10 13:03:39 -07001776 uint8_t* sp8 = LayoutJNISaveFrame(self, m, reinterpret_cast<void*>(*m), handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001777
1778 sp8 = LayoutNativeCall(sp8, start_stack, start_gpr, start_fpr);
1779
1780 // Return the new bottom.
1781 return sp8;
1782 }
1783
1784 uintptr_t PushHandle(mirror::Object* /* ptr */) OVERRIDE;
1785
1786 // Add JNIEnv* and jobj/jclass before the shorty-derived elements.
1787 void WalkHeader(BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) OVERRIDE
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001788 REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001789
1790 private:
1791 uint32_t num_handle_scope_references_;
Igor Murashkin06a04e02016-09-13 15:57:37 -07001792 const bool critical_native_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001793};
1794
1795uintptr_t ComputeGenericJniFrameSize::PushHandle(mirror::Object* /* ptr */) {
1796 num_handle_scope_references_++;
1797 return reinterpret_cast<uintptr_t>(nullptr);
1798}
1799
1800void ComputeGenericJniFrameSize::WalkHeader(
1801 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) {
Igor Murashkin06a04e02016-09-13 15:57:37 -07001802 // First 2 parameters are always excluded for @CriticalNative.
1803 if (UNLIKELY(critical_native_)) {
1804 return;
1805 }
1806
Andreas Gampec200a4a2014-06-16 18:39:09 -07001807 // JNIEnv
1808 sm->AdvancePointer(nullptr);
1809
1810 // Class object or this as first argument
1811 sm->AdvanceHandleScope(reinterpret_cast<mirror::Object*>(0x12345678));
1812}
1813
1814// Class to push values to three separate regions. Used to fill the native call part. Adheres to
1815// the template requirements of BuildGenericJniFrameStateMachine.
1816class FillNativeCall {
1817 public:
1818 FillNativeCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) :
1819 cur_gpr_reg_(gpr_regs), cur_fpr_reg_(fpr_regs), cur_stack_arg_(stack_args) {}
1820
1821 virtual ~FillNativeCall() {}
1822
1823 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) {
1824 cur_gpr_reg_ = gpr_regs;
1825 cur_fpr_reg_ = fpr_regs;
1826 cur_stack_arg_ = stack_args;
Andreas Gampec147b002014-03-06 18:11:06 -08001827 }
1828
1829 void PushGpr(uintptr_t val) {
1830 *cur_gpr_reg_ = val;
1831 cur_gpr_reg_++;
1832 }
1833
1834 void PushFpr4(float val) {
1835 *cur_fpr_reg_ = val;
1836 cur_fpr_reg_++;
1837 }
1838
1839 void PushFpr8(uint64_t val) {
1840 uint64_t* tmp = reinterpret_cast<uint64_t*>(cur_fpr_reg_);
1841 *tmp = val;
1842 cur_fpr_reg_ += 2;
1843 }
1844
1845 void PushStack(uintptr_t val) {
1846 *cur_stack_arg_ = val;
1847 cur_stack_arg_++;
1848 }
1849
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001850 virtual uintptr_t PushHandle(mirror::Object*) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001851 LOG(FATAL) << "(Non-JNI) Native call does not use handles.";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001852 UNREACHABLE();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001853 }
1854
1855 private:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001856 uintptr_t* cur_gpr_reg_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001857 uint32_t* cur_fpr_reg_;
1858 uintptr_t* cur_stack_arg_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001859};
Andreas Gampec147b002014-03-06 18:11:06 -08001860
Andreas Gampec200a4a2014-06-16 18:39:09 -07001861// Visits arguments on the stack placing them into a region lower down the stack for the benefit
1862// of transitioning into native code.
1863class BuildGenericJniFrameVisitor FINAL : public QuickArgumentVisitor {
1864 public:
Igor Murashkin06a04e02016-09-13 15:57:37 -07001865 BuildGenericJniFrameVisitor(Thread* self,
1866 bool is_static,
1867 bool critical_native,
1868 const char* shorty,
1869 uint32_t shorty_len,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001870 ArtMethod*** sp)
Andreas Gampec200a4a2014-06-16 18:39:09 -07001871 : QuickArgumentVisitor(*sp, is_static, shorty, shorty_len),
Igor Murashkin06a04e02016-09-13 15:57:37 -07001872 jni_call_(nullptr, nullptr, nullptr, nullptr, critical_native),
1873 sm_(&jni_call_) {
1874 ComputeGenericJniFrameSize fsc(critical_native);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001875 uintptr_t* start_gpr_reg;
1876 uint32_t* start_fpr_reg;
1877 uintptr_t* start_stack_arg;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001878 bottom_of_used_area_ = fsc.ComputeLayout(self, sp, shorty, shorty_len,
Ian Rogers59c07062014-10-10 13:03:39 -07001879 &handle_scope_,
1880 &start_stack_arg,
Andreas Gampec200a4a2014-06-16 18:39:09 -07001881 &start_gpr_reg, &start_fpr_reg);
1882
Andreas Gampec200a4a2014-06-16 18:39:09 -07001883 jni_call_.Reset(start_gpr_reg, start_fpr_reg, start_stack_arg, handle_scope_);
1884
Igor Murashkin06a04e02016-09-13 15:57:37 -07001885 // First 2 parameters are always excluded for CriticalNative methods.
1886 if (LIKELY(!critical_native)) {
1887 // jni environment is always first argument
1888 sm_.AdvancePointer(self->GetJniEnv());
Andreas Gampec200a4a2014-06-16 18:39:09 -07001889
Igor Murashkin06a04e02016-09-13 15:57:37 -07001890 if (is_static) {
1891 sm_.AdvanceHandleScope((**sp)->GetDeclaringClass());
1892 } // else "this" reference is already handled by QuickArgumentVisitor.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001893 }
1894 }
1895
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001896 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001897
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001898 void FinalizeHandleScope(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001899
Vladimir Markof39745e2016-01-26 12:16:55 +00001900 StackReference<mirror::Object>* GetFirstHandleScopeEntry() {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001901 return handle_scope_->GetHandle(0).GetReference();
1902 }
1903
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001904 jobject GetFirstHandleScopeJObject() const REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001905 return handle_scope_->GetHandle(0).ToJObject();
1906 }
1907
Ian Rogers1428dce2014-10-21 15:02:15 -07001908 void* GetBottomOfUsedArea() const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001909 return bottom_of_used_area_;
1910 }
1911
1912 private:
1913 // A class to fill a JNI call. Adds reference/handle-scope management to FillNativeCall.
1914 class FillJniCall FINAL : public FillNativeCall {
1915 public:
1916 FillJniCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args,
Igor Murashkin06a04e02016-09-13 15:57:37 -07001917 HandleScope* handle_scope, bool critical_native)
1918 : FillNativeCall(gpr_regs, fpr_regs, stack_args),
1919 handle_scope_(handle_scope),
1920 cur_entry_(0),
1921 critical_native_(critical_native) {}
Andreas Gampec200a4a2014-06-16 18:39:09 -07001922
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001923 uintptr_t PushHandle(mirror::Object* ref) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001924
1925 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args, HandleScope* scope) {
1926 FillNativeCall::Reset(gpr_regs, fpr_regs, stack_args);
1927 handle_scope_ = scope;
1928 cur_entry_ = 0U;
1929 }
1930
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001931 void ResetRemainingScopeSlots() REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001932 // Initialize padding entries.
1933 size_t expected_slots = handle_scope_->NumberOfReferences();
1934 while (cur_entry_ < expected_slots) {
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07001935 handle_scope_->GetMutableHandle(cur_entry_++).Assign(nullptr);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001936 }
Igor Murashkin06a04e02016-09-13 15:57:37 -07001937
1938 if (!critical_native_) {
1939 // Non-critical natives have at least the self class (jclass) or this (jobject).
1940 DCHECK_NE(cur_entry_, 0U);
1941 }
Andreas Gampec200a4a2014-06-16 18:39:09 -07001942 }
1943
Mathieu Chartier1432a5b2016-10-04 15:41:42 -07001944 bool CriticalNative() const {
1945 return critical_native_;
1946 }
1947
Andreas Gampec200a4a2014-06-16 18:39:09 -07001948 private:
1949 HandleScope* handle_scope_;
1950 size_t cur_entry_;
Igor Murashkin06a04e02016-09-13 15:57:37 -07001951 const bool critical_native_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001952 };
1953
1954 HandleScope* handle_scope_;
1955 FillJniCall jni_call_;
1956 void* bottom_of_used_area_;
1957
1958 BuildNativeCallFrameStateMachine<FillJniCall> sm_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001959
1960 DISALLOW_COPY_AND_ASSIGN(BuildGenericJniFrameVisitor);
1961};
1962
Andreas Gampec200a4a2014-06-16 18:39:09 -07001963uintptr_t BuildGenericJniFrameVisitor::FillJniCall::PushHandle(mirror::Object* ref) {
1964 uintptr_t tmp;
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07001965 MutableHandle<mirror::Object> h = handle_scope_->GetMutableHandle(cur_entry_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001966 h.Assign(ref);
1967 tmp = reinterpret_cast<uintptr_t>(h.ToJObject());
1968 cur_entry_++;
1969 return tmp;
1970}
1971
Ian Rogers9758f792014-03-13 09:02:55 -07001972void BuildGenericJniFrameVisitor::Visit() {
1973 Primitive::Type type = GetParamPrimitiveType();
1974 switch (type) {
1975 case Primitive::kPrimLong: {
1976 jlong long_arg;
1977 if (IsSplitLongOrDouble()) {
1978 long_arg = ReadSplitLongParam();
1979 } else {
1980 long_arg = *reinterpret_cast<jlong*>(GetParamAddress());
1981 }
1982 sm_.AdvanceLong(long_arg);
1983 break;
1984 }
1985 case Primitive::kPrimDouble: {
1986 uint64_t double_arg;
1987 if (IsSplitLongOrDouble()) {
1988 // Read into union so that we don't case to a double.
1989 double_arg = ReadSplitLongParam();
1990 } else {
1991 double_arg = *reinterpret_cast<uint64_t*>(GetParamAddress());
1992 }
1993 sm_.AdvanceDouble(double_arg);
1994 break;
1995 }
1996 case Primitive::kPrimNot: {
1997 StackReference<mirror::Object>* stack_ref =
1998 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001999 sm_.AdvanceHandleScope(stack_ref->AsMirrorPtr());
Ian Rogers9758f792014-03-13 09:02:55 -07002000 break;
2001 }
2002 case Primitive::kPrimFloat:
2003 sm_.AdvanceFloat(*reinterpret_cast<float*>(GetParamAddress()));
2004 break;
2005 case Primitive::kPrimBoolean: // Fall-through.
2006 case Primitive::kPrimByte: // Fall-through.
2007 case Primitive::kPrimChar: // Fall-through.
2008 case Primitive::kPrimShort: // Fall-through.
2009 case Primitive::kPrimInt: // Fall-through.
2010 sm_.AdvanceInt(*reinterpret_cast<jint*>(GetParamAddress()));
2011 break;
2012 case Primitive::kPrimVoid:
2013 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -07002014 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -07002015 }
2016}
2017
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002018void BuildGenericJniFrameVisitor::FinalizeHandleScope(Thread* self) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002019 // Clear out rest of the scope.
2020 jni_call_.ResetRemainingScopeSlots();
Mathieu Chartier1432a5b2016-10-04 15:41:42 -07002021 if (!jni_call_.CriticalNative()) {
2022 // Install HandleScope.
2023 self->PushHandleScope(handle_scope_);
2024 }
Ian Rogers9758f792014-03-13 09:02:55 -07002025}
2026
Ian Rogers04c31d22014-07-07 21:44:06 -07002027#if defined(__arm__) || defined(__aarch64__)
Alex Lightd78ddec2017-04-18 15:20:38 -07002028extern "C" const void* artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07002029#else
Alex Lightd78ddec2017-04-18 15:20:38 -07002030extern "C" const void* artFindNativeMethod(Thread* self);
Ian Rogers04c31d22014-07-07 21:44:06 -07002031#endif
Andreas Gampe90546832014-03-12 18:07:19 -07002032
Igor Murashkin06a04e02016-09-13 15:57:37 -07002033static uint64_t artQuickGenericJniEndJNIRef(Thread* self,
2034 uint32_t cookie,
2035 bool fast_native ATTRIBUTE_UNUSED,
2036 jobject l,
2037 jobject lock) {
2038 // TODO: add entrypoints for @FastNative returning objects.
Andreas Gampead615172014-04-04 16:20:13 -07002039 if (lock != nullptr) {
2040 return reinterpret_cast<uint64_t>(JniMethodEndWithReferenceSynchronized(l, cookie, lock, self));
2041 } else {
2042 return reinterpret_cast<uint64_t>(JniMethodEndWithReference(l, cookie, self));
2043 }
2044}
2045
Igor Murashkin06a04e02016-09-13 15:57:37 -07002046static void artQuickGenericJniEndJNINonRef(Thread* self,
2047 uint32_t cookie,
2048 bool fast_native,
2049 jobject lock) {
Andreas Gampead615172014-04-04 16:20:13 -07002050 if (lock != nullptr) {
2051 JniMethodEndSynchronized(cookie, lock, self);
Igor Murashkin06a04e02016-09-13 15:57:37 -07002052 // Ignore "fast_native" here because synchronized functions aren't very fast.
Andreas Gampead615172014-04-04 16:20:13 -07002053 } else {
Igor Murashkin06a04e02016-09-13 15:57:37 -07002054 if (UNLIKELY(fast_native)) {
2055 JniMethodFastEnd(cookie, self);
2056 } else {
2057 JniMethodEnd(cookie, self);
2058 }
Andreas Gampead615172014-04-04 16:20:13 -07002059 }
2060}
2061
Andreas Gampec147b002014-03-06 18:11:06 -08002062/*
2063 * Initializes an alloca region assumed to be directly below sp for a native call:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002064 * 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 -08002065 * The final element on the stack is a pointer to the native code.
2066 *
Andreas Gampe36fea8d2014-03-10 13:37:40 -07002067 * On entry, the stack has a standard callee-save frame above sp, and an alloca below it.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002068 * We need to fix this, as the handle scope needs to go into the callee-save frame.
Andreas Gampe36fea8d2014-03-10 13:37:40 -07002069 *
Andreas Gampec147b002014-03-06 18:11:06 -08002070 * The return of this function denotes:
2071 * 1) How many bytes of the alloca can be released, if the value is non-negative.
2072 * 2) An error, if the value is negative.
2073 */
Mathieu Chartiere401d142015-04-22 13:56:20 -07002074extern "C" TwoWordReturn artQuickGenericJniTrampoline(Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002075 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002076 ArtMethod* called = *sp;
David Sehr709b0702016-10-13 09:12:37 -07002077 DCHECK(called->IsNative()) << called->PrettyMethod(true);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002078 uint32_t shorty_len = 0;
2079 const char* shorty = called->GetShorty(&shorty_len);
Igor Murashkin06a04e02016-09-13 15:57:37 -07002080 bool critical_native = called->IsAnnotatedWithCriticalNative();
2081 bool fast_native = called->IsAnnotatedWithFastNative();
2082 bool normal_native = !critical_native && !fast_native;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002083
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002084 // Run the visitor and update sp.
Igor Murashkin06a04e02016-09-13 15:57:37 -07002085 BuildGenericJniFrameVisitor visitor(self,
2086 called->IsStatic(),
2087 critical_native,
2088 shorty,
2089 shorty_len,
2090 &sp);
Mathieu Chartierbe08cf52016-09-13 13:41:24 -07002091 {
2092 ScopedAssertNoThreadSuspension sants(__FUNCTION__);
2093 visitor.VisitArguments();
2094 // FinalizeHandleScope pushes the handle scope on the thread.
2095 visitor.FinalizeHandleScope(self);
2096 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002097
Andreas Gampec200a4a2014-06-16 18:39:09 -07002098 // Fix up managed-stack things in Thread.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002099 self->SetTopOfStack(sp);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002100
Ian Rogerse0dcd462014-03-08 15:21:04 -08002101 self->VerifyStack();
2102
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002103 uint32_t cookie;
Igor Murashkin06a04e02016-09-13 15:57:37 -07002104 uint32_t* sp32;
2105 // Skip calling JniMethodStart for @CriticalNative.
2106 if (LIKELY(!critical_native)) {
2107 // Start JNI, save the cookie.
2108 if (called->IsSynchronized()) {
2109 DCHECK(normal_native) << " @FastNative and synchronize is not supported";
2110 cookie = JniMethodStartSynchronized(visitor.GetFirstHandleScopeJObject(), self);
2111 if (self->IsExceptionPending()) {
2112 self->PopHandleScope();
2113 // A negative value denotes an error.
2114 return GetTwoWordFailureValue();
2115 }
2116 } else {
2117 if (fast_native) {
2118 cookie = JniMethodFastStart(self);
2119 } else {
2120 DCHECK(normal_native);
2121 cookie = JniMethodStart(self);
2122 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002123 }
Igor Murashkin06a04e02016-09-13 15:57:37 -07002124 sp32 = reinterpret_cast<uint32_t*>(sp);
2125 *(sp32 - 1) = cookie;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002126 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002127
Andreas Gampe90546832014-03-12 18:07:19 -07002128 // Retrieve the stored native code.
Alex Lightd78ddec2017-04-18 15:20:38 -07002129 void const* nativeCode = called->GetEntryPointFromJni();
Andreas Gampe90546832014-03-12 18:07:19 -07002130
Andreas Gampe9a6a99a2014-03-14 07:52:20 -07002131 // There are two cases for the content of nativeCode:
2132 // 1) Pointer to the native function.
2133 // 2) Pointer to the trampoline for native code binding.
2134 // In the second case, we need to execute the binding and continue with the actual native function
2135 // pointer.
Andreas Gampe90546832014-03-12 18:07:19 -07002136 DCHECK(nativeCode != nullptr);
2137 if (nativeCode == GetJniDlsymLookupStub()) {
Ian Rogers04c31d22014-07-07 21:44:06 -07002138#if defined(__arm__) || defined(__aarch64__)
Andreas Gampe90546832014-03-12 18:07:19 -07002139 nativeCode = artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07002140#else
2141 nativeCode = artFindNativeMethod(self);
2142#endif
Andreas Gampe90546832014-03-12 18:07:19 -07002143
2144 if (nativeCode == nullptr) {
2145 DCHECK(self->IsExceptionPending()); // There should be an exception pending now.
Andreas Gampead615172014-04-04 16:20:13 -07002146
Igor Murashkin06a04e02016-09-13 15:57:37 -07002147 // @CriticalNative calls do not need to call back into JniMethodEnd.
2148 if (LIKELY(!critical_native)) {
2149 // End JNI, as the assembly will move to deliver the exception.
2150 jobject lock = called->IsSynchronized() ? visitor.GetFirstHandleScopeJObject() : nullptr;
2151 if (shorty[0] == 'L') {
2152 artQuickGenericJniEndJNIRef(self, cookie, fast_native, nullptr, lock);
2153 } else {
2154 artQuickGenericJniEndJNINonRef(self, cookie, fast_native, lock);
2155 }
Andreas Gampead615172014-04-04 16:20:13 -07002156 }
2157
Andreas Gampec200a4a2014-06-16 18:39:09 -07002158 return GetTwoWordFailureValue();
Andreas Gampe90546832014-03-12 18:07:19 -07002159 }
2160 // Note that the native code pointer will be automatically set by artFindNativeMethod().
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002161 }
2162
Alexey Frunze1b8464d2016-11-12 17:22:05 -08002163#if defined(__mips__) && !defined(__LP64__)
2164 // On MIPS32 if the first two arguments are floating-point, we need to know their types
2165 // so that art_quick_generic_jni_trampoline can correctly extract them from the stack
2166 // and load into floating-point registers.
2167 // Possible arrangements of first two floating-point arguments on the stack (32-bit FPU
2168 // view):
2169 // (1)
2170 // | DOUBLE | DOUBLE | other args, if any
2171 // | F12 | F13 | F14 | F15 |
2172 // | SP+0 | SP+4 | SP+8 | SP+12 | SP+16
2173 // (2)
2174 // | DOUBLE | FLOAT | (PAD) | other args, if any
2175 // | F12 | F13 | F14 | |
2176 // | SP+0 | SP+4 | SP+8 | SP+12 | SP+16
2177 // (3)
2178 // | FLOAT | (PAD) | DOUBLE | other args, if any
2179 // | F12 | | F14 | F15 |
2180 // | SP+0 | SP+4 | SP+8 | SP+12 | SP+16
2181 // (4)
2182 // | FLOAT | FLOAT | other args, if any
2183 // | F12 | F14 |
2184 // | SP+0 | SP+4 | SP+8
2185 // As you can see, only the last case (4) is special. In all others we can just
2186 // load F12/F13 and F14/F15 in the same manner.
2187 // Set bit 0 of the native code address to 1 in this case (valid code addresses
2188 // are always a multiple of 4 on MIPS32, so we have 2 spare bits available).
2189 if (nativeCode != nullptr &&
2190 shorty != nullptr &&
2191 shorty_len >= 3 &&
2192 shorty[1] == 'F' &&
2193 shorty[2] == 'F') {
2194 nativeCode = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(nativeCode) | 1);
2195 }
2196#endif
2197
Andreas Gampec200a4a2014-06-16 18:39:09 -07002198 // Return native code addr(lo) and bottom of alloca address(hi).
2199 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(visitor.GetBottomOfUsedArea()),
2200 reinterpret_cast<uintptr_t>(nativeCode));
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002201}
2202
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002203// Defined in quick_jni_entrypoints.cc.
2204extern uint64_t GenericJniMethodEnd(Thread* self, uint32_t saved_local_ref_cookie,
2205 jvalue result, uint64_t result_f, ArtMethod* called,
2206 HandleScope* handle_scope);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002207/*
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002208 * Is called after the native JNI code. Responsible for cleanup (handle scope, saved state) and
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002209 * unlocking.
2210 */
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002211extern "C" uint64_t artQuickGenericJniEndTrampoline(Thread* self,
2212 jvalue result,
2213 uint64_t result_f) {
2214 // We're here just back from a native call. We don't have the shared mutator lock at this point
2215 // yet until we call GoToRunnable() later in GenericJniMethodEnd(). Accessing objects or doing
2216 // anything that requires a mutator lock before that would cause problems as GC may have the
2217 // exclusive mutator lock and may be moving objects, etc.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002218 ArtMethod** sp = self->GetManagedStack()->GetTopQuickFrame();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002219 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002220 ArtMethod* called = *sp;
Ian Rogerse0dcd462014-03-08 15:21:04 -08002221 uint32_t cookie = *(sp32 - 1);
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002222 HandleScope* table = reinterpret_cast<HandleScope*>(reinterpret_cast<uint8_t*>(sp) + sizeof(*sp));
2223 return GenericJniMethodEnd(self, cookie, result, result_f, called, table);
Andreas Gampe2da88232014-02-27 12:26:20 -08002224}
2225
Andreas Gamped58342c2014-06-05 14:18:08 -07002226// We use TwoWordReturn to optimize scalar returns. We use the hi value for code, and the lo value
2227// for the method pointer.
Andreas Gampe51f76352014-05-21 08:28:48 -07002228//
Andreas Gamped58342c2014-06-05 14:18:08 -07002229// 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 -07002230// to hold the mutator lock (see REQUIRES_SHARED(Locks::mutator_lock_) annotations).
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002231
2232template<InvokeType type, bool access_check>
Mathieu Chartieref41db72016-10-25 15:08:01 -07002233static TwoWordReturn artInvokeCommon(uint32_t method_idx,
2234 ObjPtr<mirror::Object> this_object,
2235 Thread* self,
Mathieu Chartiere401d142015-04-22 13:56:20 -07002236 ArtMethod** sp) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002237 ScopedQuickEntrypointChecks sqec(self);
Vladimir Markofd36f1f2016-08-03 18:49:58 +01002238 DCHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(Runtime::kSaveRefsAndArgs));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002239 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
2240 ArtMethod* method = FindMethodFast(method_idx, this_object, caller_method, access_check, type);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002241 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002242 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()->GetDexFile();
2243 uint32_t shorty_len;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002244 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(method_idx), &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002245 {
2246 // Remember the args in case a GC happens in FindMethodFromCode.
2247 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
2248 RememberForGcArgumentVisitor visitor(sp, type == kStatic, shorty, shorty_len, &soa);
2249 visitor.VisitArguments();
Mathieu Chartieref41db72016-10-25 15:08:01 -07002250 method = FindMethodFromCode<type, access_check>(method_idx,
2251 &this_object,
2252 caller_method,
Mathieu Chartier0cd81352014-05-22 16:48:55 -07002253 self);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002254 visitor.FixupReferences();
2255 }
2256
Ian Rogerse0a02da2014-12-02 14:10:53 -08002257 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002258 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07002259 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002260 }
2261 }
2262 DCHECK(!self->IsExceptionPending());
2263 const void* code = method->GetEntryPointFromQuickCompiledCode();
2264
2265 // When we return, the caller will branch to this address, so it had better not be 0!
David Sehr709b0702016-10-13 09:12:37 -07002266 DCHECK(code != nullptr) << "Code was null in method: " << method->PrettyMethod()
Andreas Gampec200a4a2014-06-16 18:39:09 -07002267 << " location: "
2268 << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07002269
Andreas Gamped58342c2014-06-05 14:18:08 -07002270 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
2271 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002272}
2273
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01002274// Explicit artInvokeCommon template function declarations to please analysis tool.
2275#define EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(type, access_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002276 template REQUIRES_SHARED(Locks::mutator_lock_) \
Mathieu Chartiere401d142015-04-22 13:56:20 -07002277 TwoWordReturn artInvokeCommon<type, access_check>( \
Mathieu Chartieref41db72016-10-25 15:08:01 -07002278 uint32_t method_idx, ObjPtr<mirror::Object> his_object, Thread* self, ArtMethod** sp)
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01002279
2280EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, false);
2281EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, true);
2282EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, false);
2283EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, true);
2284EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, false);
2285EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, true);
2286EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, false);
2287EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, true);
2288EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, false);
2289EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, true);
2290#undef EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL
2291
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002292// See comments in runtime_support_asm.S
Andreas Gampec200a4a2014-06-16 18:39:09 -07002293extern "C" TwoWordReturn artInvokeInterfaceTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002294 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002295 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002296 return artInvokeCommon<kInterface, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002297}
2298
Andreas Gampec200a4a2014-06-16 18:39:09 -07002299extern "C" TwoWordReturn artInvokeDirectTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002300 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002301 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002302 return artInvokeCommon<kDirect, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002303}
2304
Andreas Gampec200a4a2014-06-16 18:39:09 -07002305extern "C" TwoWordReturn artInvokeStaticTrampolineWithAccessCheck(
Mathieu Chartieref41db72016-10-25 15:08:01 -07002306 uint32_t method_idx,
2307 mirror::Object* this_object ATTRIBUTE_UNUSED,
2308 Thread* self,
2309 ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
2310 // For static, this_object is not required and may be random garbage. Don't pass it down so that
2311 // it doesn't cause ObjPtr alignment failure check.
2312 return artInvokeCommon<kStatic, true>(method_idx, nullptr, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002313}
2314
Andreas Gampec200a4a2014-06-16 18:39:09 -07002315extern "C" TwoWordReturn artInvokeSuperTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002316 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002317 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002318 return artInvokeCommon<kSuper, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002319}
2320
Andreas Gampec200a4a2014-06-16 18:39:09 -07002321extern "C" TwoWordReturn artInvokeVirtualTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002322 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002323 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002324 return artInvokeCommon<kVirtual, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002325}
2326
Jeff Hao5667f562017-02-27 19:32:01 -08002327// Determine target of interface dispatch. The interface method and this object are known non-null.
2328// The interface method is the method returned by the dex cache in the conflict trampoline.
2329extern "C" TwoWordReturn artInvokeInterfaceTrampoline(ArtMethod* interface_method,
Mathieu Chartieref41db72016-10-25 15:08:01 -07002330 mirror::Object* raw_this_object,
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002331 Thread* self,
2332 ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002333 REQUIRES_SHARED(Locks::mutator_lock_) {
Jeff Hao5667f562017-02-27 19:32:01 -08002334 CHECK(interface_method != nullptr);
Mathieu Chartieref41db72016-10-25 15:08:01 -07002335 ObjPtr<mirror::Object> this_object(raw_this_object);
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002336 ScopedQuickEntrypointChecks sqec(self);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002337 StackHandleScope<1> hs(self);
2338 Handle<mirror::Class> cls(hs.NewHandle(this_object->GetClass()));
2339
Nicolas Geoffray5bf7bac2016-07-06 14:18:23 +00002340 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002341 ArtMethod* method = nullptr;
Andreas Gampe542451c2016-07-26 09:02:02 -07002342 ImTable* imt = cls->GetImt(kRuntimePointerSize);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002343
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002344 if (LIKELY(interface_method->GetDexMethodIndex() != DexFile::kDexNoIndex)) {
Jeff Hao5667f562017-02-27 19:32:01 -08002345 // If the interface method is already resolved, look whether we have a match in the
2346 // ImtConflictTable.
Andreas Gampe75a7db62016-09-26 12:04:26 -07002347 ArtMethod* conflict_method = imt->Get(ImTable::GetImtIndex(interface_method),
2348 kRuntimePointerSize);
Alex Light9fc547a2016-03-31 14:34:33 -07002349 if (LIKELY(conflict_method->IsRuntimeMethod())) {
Andreas Gampe542451c2016-07-26 09:02:02 -07002350 ImtConflictTable* current_table = conflict_method->GetImtConflictTable(kRuntimePointerSize);
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002351 DCHECK(current_table != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -07002352 method = current_table->Lookup(interface_method, kRuntimePointerSize);
Alex Light9fc547a2016-03-31 14:34:33 -07002353 } else {
2354 // It seems we aren't really a conflict method!
Andreas Gampe542451c2016-07-26 09:02:02 -07002355 method = cls->FindVirtualMethodForInterface(interface_method, kRuntimePointerSize);
Alex Light9fc547a2016-03-31 14:34:33 -07002356 }
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002357 if (method != nullptr) {
2358 return GetTwoWordSuccessValue(
2359 reinterpret_cast<uintptr_t>(method->GetEntryPointFromQuickCompiledCode()),
2360 reinterpret_cast<uintptr_t>(method));
2361 }
2362
2363 // No match, use the IfTable.
Andreas Gampe542451c2016-07-26 09:02:02 -07002364 method = cls->FindVirtualMethodForInterface(interface_method, kRuntimePointerSize);
Ian Rogerse0a02da2014-12-02 14:10:53 -08002365 if (UNLIKELY(method == nullptr)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002366 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(
2367 interface_method, this_object, caller_method);
Andreas Gamped58342c2014-06-05 14:18:08 -07002368 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002369 }
2370 } else {
Jeff Hao5667f562017-02-27 19:32:01 -08002371 // The interface method is unresolved, so look it up in the dex file of the caller.
Mathieu Chartier4edd8472015-06-01 10:47:36 -07002372 DCHECK_EQ(interface_method, Runtime::Current()->GetResolutionMethod());
Jeff Hao5667f562017-02-27 19:32:01 -08002373
2374 // Fetch the dex_method_idx of the target interface method from the caller.
2375 uint32_t dex_method_idx;
2376 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
2377 const DexFile::CodeItem* code_item = caller_method->GetCodeItem();
2378 DCHECK_LT(dex_pc, code_item->insns_size_in_code_units_);
2379 const Instruction* instr = Instruction::At(&code_item->insns_[dex_pc]);
2380 Instruction::Code instr_code = instr->Opcode();
2381 DCHECK(instr_code == Instruction::INVOKE_INTERFACE ||
2382 instr_code == Instruction::INVOKE_INTERFACE_RANGE)
2383 << "Unexpected call into interface trampoline: " << instr->DumpString(nullptr);
2384 if (instr_code == Instruction::INVOKE_INTERFACE) {
2385 dex_method_idx = instr->VRegB_35c();
2386 } else {
2387 DCHECK_EQ(instr_code, Instruction::INVOKE_INTERFACE_RANGE);
2388 dex_method_idx = instr->VRegB_3rc();
2389 }
2390
Andreas Gampec200a4a2014-06-16 18:39:09 -07002391 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()
2392 ->GetDexFile();
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002393 uint32_t shorty_len;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002394 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(dex_method_idx),
2395 &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002396 {
2397 // Remember the args in case a GC happens in FindMethodFromCode.
2398 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
2399 RememberForGcArgumentVisitor visitor(sp, false, shorty, shorty_len, &soa);
2400 visitor.VisitArguments();
Mathieu Chartieref41db72016-10-25 15:08:01 -07002401 method = FindMethodFromCode<kInterface, false>(dex_method_idx,
2402 &this_object,
2403 caller_method,
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002404 self);
2405 visitor.FixupReferences();
2406 }
2407
2408 if (UNLIKELY(method == nullptr)) {
2409 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07002410 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002411 }
Andreas Gampe542451c2016-07-26 09:02:02 -07002412 interface_method =
2413 caller_method->GetDexCacheResolvedMethod(dex_method_idx, kRuntimePointerSize);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002414 DCHECK(!interface_method->IsRuntimeMethod());
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002415 }
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002416
2417 // We arrive here if we have found an implementation, and it is not in the ImtConflictTable.
2418 // We create a new table with the new pair { interface_method, method }.
Andreas Gampe75a7db62016-09-26 12:04:26 -07002419 uint32_t imt_index = ImTable::GetImtIndex(interface_method);
Andreas Gampe542451c2016-07-26 09:02:02 -07002420 ArtMethod* conflict_method = imt->Get(imt_index, kRuntimePointerSize);
Alex Light9fc547a2016-03-31 14:34:33 -07002421 if (conflict_method->IsRuntimeMethod()) {
Mathieu Chartier7f98c9a2016-04-14 10:49:19 -07002422 ArtMethod* new_conflict_method = Runtime::Current()->GetClassLinker()->AddMethodToConflictTable(
2423 cls.Get(),
2424 conflict_method,
2425 interface_method,
2426 method,
2427 /*force_new_conflict_method*/false);
2428 if (new_conflict_method != conflict_method) {
Alex Light9fc547a2016-03-31 14:34:33 -07002429 // Update the IMT if we create a new conflict method. No fence needed here, as the
2430 // data is consistent.
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002431 imt->Set(imt_index,
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002432 new_conflict_method,
Andreas Gampe542451c2016-07-26 09:02:02 -07002433 kRuntimePointerSize);
Alex Light9fc547a2016-03-31 14:34:33 -07002434 }
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002435 }
2436
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002437 const void* code = method->GetEntryPointFromQuickCompiledCode();
2438
2439 // When we return, the caller will branch to this address, so it had better not be 0!
David Sehr709b0702016-10-13 09:12:37 -07002440 DCHECK(code != nullptr) << "Code was null in method: " << method->PrettyMethod()
Andreas Gampec200a4a2014-06-16 18:39:09 -07002441 << " location: " << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07002442
Andreas Gamped58342c2014-06-05 14:18:08 -07002443 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
2444 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002445}
2446
Orion Hodsonac141392017-01-13 11:53:47 +00002447// Returns shorty type so the caller can determine how to put |result|
2448// into expected registers. The shorty type is static so the compiler
2449// could call different flavors of this code path depending on the
2450// shorty type though this would require different entry points for
2451// each type.
2452extern "C" uintptr_t artInvokePolymorphic(
2453 JValue* result,
2454 mirror::Object* raw_method_handle,
2455 Thread* self,
2456 ArtMethod** sp)
2457 REQUIRES_SHARED(Locks::mutator_lock_) {
2458 ScopedQuickEntrypointChecks sqec(self);
2459 DCHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(Runtime::kSaveRefsAndArgs));
2460
2461 // Start new JNI local reference state
2462 JNIEnvExt* env = self->GetJniEnv();
2463 ScopedObjectAccessUnchecked soa(env);
2464 ScopedJniEnvLocalRefState env_state(env);
2465 const char* old_cause = self->StartAssertNoThreadSuspension("Making stack arguments safe.");
2466
2467 // From the instruction, get the |callsite_shorty| and expose arguments on the stack to the GC.
2468 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
2469 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
2470 const DexFile::CodeItem* code = caller_method->GetCodeItem();
2471 const Instruction* inst = Instruction::At(&code->insns_[dex_pc]);
2472 DCHECK(inst->Opcode() == Instruction::INVOKE_POLYMORPHIC ||
2473 inst->Opcode() == Instruction::INVOKE_POLYMORPHIC_RANGE);
2474 const DexFile* dex_file = caller_method->GetDexFile();
2475 const uint32_t proto_idx = inst->VRegH();
2476 const char* shorty = dex_file->GetShorty(proto_idx);
2477 const size_t shorty_length = strlen(shorty);
2478 static const bool kMethodIsStatic = false; // invoke() and invokeExact() are not static.
2479 RememberForGcArgumentVisitor gc_visitor(sp, kMethodIsStatic, shorty, shorty_length, &soa);
Orion Hodsonfea84dd2017-01-16 13:52:20 +00002480 gc_visitor.VisitArguments();
Orion Hodsonac141392017-01-13 11:53:47 +00002481
2482 // Wrap raw_method_handle in a Handle for safety.
2483 StackHandleScope<5> hs(self);
Orion Hodsonc069a302017-01-18 09:23:12 +00002484 Handle<mirror::MethodHandle> method_handle(
2485 hs.NewHandle(ObjPtr<mirror::MethodHandle>::DownCast(MakeObjPtr(raw_method_handle))));
Orion Hodsonac141392017-01-13 11:53:47 +00002486 raw_method_handle = nullptr;
2487 self->EndAssertNoThreadSuspension(old_cause);
2488
2489 // Resolve method - it's either MethodHandle.invoke() or MethodHandle.invokeExact().
2490 ClassLinker* linker = Runtime::Current()->GetClassLinker();
2491 ArtMethod* resolved_method = linker->ResolveMethod<ClassLinker::kForceICCECheck>(self,
2492 inst->VRegB(),
2493 caller_method,
2494 kVirtual);
2495 DCHECK((resolved_method ==
2496 jni::DecodeArtMethod(WellKnownClasses::java_lang_invoke_MethodHandle_invokeExact)) ||
2497 (resolved_method ==
2498 jni::DecodeArtMethod(WellKnownClasses::java_lang_invoke_MethodHandle_invoke)));
2499 if (UNLIKELY(method_handle.IsNull())) {
2500 ThrowNullPointerExceptionForMethodAccess(resolved_method, InvokeType::kVirtual);
2501 return static_cast<uintptr_t>('V');
2502 }
2503
2504 Handle<mirror::Class> caller_class(hs.NewHandle(caller_method->GetDeclaringClass()));
2505 Handle<mirror::MethodType> method_type(hs.NewHandle(linker->ResolveMethodType(
2506 *dex_file, proto_idx,
2507 hs.NewHandle<mirror::DexCache>(caller_class->GetDexCache()),
2508 hs.NewHandle<mirror::ClassLoader>(caller_class->GetClassLoader()))));
2509 // This implies we couldn't resolve one or more types in this method handle.
2510 if (UNLIKELY(method_type.IsNull())) {
2511 CHECK(self->IsExceptionPending());
2512 return static_cast<uintptr_t>('V');
2513 }
2514
2515 DCHECK_EQ(ArtMethod::NumArgRegisters(shorty) + 1u, (uint32_t)inst->VRegA());
2516 DCHECK_EQ(resolved_method->IsStatic(), kMethodIsStatic);
2517
2518 // Fix references before constructing the shadow frame.
2519 gc_visitor.FixupReferences();
2520
2521 // Construct shadow frame placing arguments consecutively from |first_arg|.
2522 const bool is_range = (inst->Opcode() == Instruction::INVOKE_POLYMORPHIC_RANGE);
2523 const size_t num_vregs = is_range ? inst->VRegA_4rcc() : inst->VRegA_45cc();
2524 const size_t first_arg = 0;
2525 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
2526 CREATE_SHADOW_FRAME(num_vregs, /* link */ nullptr, resolved_method, dex_pc);
2527 ShadowFrame* shadow_frame = shadow_frame_unique_ptr.get();
2528 ScopedStackedShadowFramePusher
2529 frame_pusher(self, shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
2530 BuildQuickShadowFrameVisitor shadow_frame_builder(sp,
2531 kMethodIsStatic,
2532 shorty,
2533 strlen(shorty),
2534 shadow_frame,
2535 first_arg);
2536 shadow_frame_builder.VisitArguments();
2537
2538 // Push a transition back into managed code onto the linked list in thread.
2539 ManagedStack fragment;
2540 self->PushManagedStackFragment(&fragment);
2541
2542 // Call DoInvokePolymorphic with |is_range| = true, as shadow frame has argument registers in
2543 // consecutive order.
2544 uint32_t unused_args[Instruction::kMaxVarArgRegs] = {};
2545 uint32_t first_callee_arg = first_arg + 1;
Orion Hodsonc069a302017-01-18 09:23:12 +00002546 if (!DoInvokePolymorphic<true /* is_range */>(self,
2547 resolved_method,
2548 *shadow_frame,
2549 method_handle,
2550 method_type,
2551 unused_args,
2552 first_callee_arg,
2553 result)) {
Orion Hodsonac141392017-01-13 11:53:47 +00002554 DCHECK(self->IsExceptionPending());
2555 }
2556
2557 // Pop transition record.
2558 self->PopManagedStackFragment(fragment);
2559
2560 return static_cast<uintptr_t>(shorty[0]);
2561}
2562
Ian Rogers848871b2013-08-05 10:56:33 -07002563} // namespace art