blob: e90383320b8bcb79562338954cb75aff50948bf2 [file] [log] [blame]
Ian Rogers848871b2013-08-05 10:56:33 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Mathieu Chartiere401d142015-04-22 13:56:20 -070017#include "art_method-inl.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070018#include "base/enums.h"
Ian Rogers848871b2013-08-05 10:56:33 -070019#include "callee_save_frame.h"
Dragos Sbirleabd136a22013-08-13 18:07:04 -070020#include "common_throws.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070021#include "debugger.h"
Ian Rogers848871b2013-08-05 10:56:33 -070022#include "dex_file-inl.h"
23#include "dex_instruction-inl.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070024#include "entrypoints/entrypoint_utils-inl.h"
Ian Rogers6f3dbba2014-10-14 17:41:57 -070025#include "entrypoints/runtime_asm_entrypoints.h"
Ian Rogers83883d72013-10-21 21:07:24 -070026#include "gc/accounting/card_table-inl.h"
Andreas Gampe75a7db62016-09-26 12:04:26 -070027#include "imt_conflict_table.h"
28#include "imtable-inl.h"
Ian Rogers848871b2013-08-05 10:56:33 -070029#include "interpreter/interpreter.h"
Nicolas Geoffray796d6302016-03-13 22:22:31 +000030#include "linear_alloc.h"
Vladimir Marko0eb882b2017-05-15 13:39:18 +010031#include "method_bss_mapping.h"
Orion Hodsonac141392017-01-13 11:53:47 +000032#include "method_handles.h"
Ian Rogerse0a02da2014-12-02 14:10:53 -080033#include "method_reference.h"
Ian Rogers848871b2013-08-05 10:56:33 -070034#include "mirror/class-inl.h"
Mathieu Chartier5f3ded42014-04-03 15:25:30 -070035#include "mirror/dex_cache-inl.h"
Mathieu Chartierfc58af42015-04-16 18:00:39 -070036#include "mirror/method.h"
Orion Hodsonac141392017-01-13 11:53:47 +000037#include "mirror/method_handle_impl.h"
Ian Rogers848871b2013-08-05 10:56:33 -070038#include "mirror/object-inl.h"
39#include "mirror/object_array-inl.h"
Vladimir Marko0eb882b2017-05-15 13:39:18 +010040#include "oat_file.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010041#include "oat_quick_method_header.h"
Andreas Gampe639bdd12015-06-03 11:22:45 -070042#include "quick_exception_handler.h"
Ian Rogers848871b2013-08-05 10:56:33 -070043#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070044#include "scoped_thread_state_change-inl.h"
Andreas Gampeb3025922015-09-01 14:45:00 -070045#include "stack.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070046#include "thread-inl.h"
Orion Hodsonac141392017-01-13 11:53:47 +000047#include "well_known_classes.h"
Ian Rogers848871b2013-08-05 10:56:33 -070048
49namespace art {
50
51// Visits the arguments as saved to the stack by a Runtime::kRefAndArgs callee save frame.
52class QuickArgumentVisitor {
Ian Rogers936b37f2014-02-14 00:52:24 -080053 // Number of bytes for each out register in the caller method's frame.
54 static constexpr size_t kBytesStackArgLocation = 4;
Alexei Zavjalov41c507a2014-05-15 16:02:46 +070055 // Frame size in bytes of a callee-save frame for RefsAndArgs.
56 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize =
Vladimir Markofd36f1f2016-08-03 18:49:58 +010057 GetCalleeSaveFrameSize(kRuntimeISA, Runtime::kSaveRefsAndArgs);
Ian Rogers848871b2013-08-05 10:56:33 -070058#if defined(__arm__)
59 // The callee save frame is pointed to by SP.
60 // | argN | |
61 // | ... | |
62 // | arg4 | |
63 // | arg3 spill | | Caller's frame
64 // | arg2 spill | |
65 // | arg1 spill | |
66 // | Method* | ---
67 // | LR |
Zheng Xu5667fdb2014-10-23 18:29:55 +080068 // | ... | 4x6 bytes callee saves
69 // | R3 |
70 // | R2 |
71 // | R1 |
72 // | S15 |
73 // | : |
74 // | S0 |
75 // | | 4x2 bytes padding
Ian Rogers848871b2013-08-05 10:56:33 -070076 // | Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -050077 static constexpr bool kSplitPairAcrossRegisterAndStack = kArm32QuickCodeUseSoftFloat;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +000078 static constexpr bool kAlignPairRegister = !kArm32QuickCodeUseSoftFloat;
Zheng Xu5667fdb2014-10-23 18:29:55 +080079 static constexpr bool kQuickSoftFloatAbi = kArm32QuickCodeUseSoftFloat;
80 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = !kArm32QuickCodeUseSoftFloat;
Goran Jakovljevicff734982015-08-24 12:58:55 +000081 static constexpr bool kQuickSkipOddFpRegisters = false;
Zheng Xu5667fdb2014-10-23 18:29:55 +080082 static constexpr size_t kNumQuickGprArgs = 3;
83 static constexpr size_t kNumQuickFprArgs = kArm32QuickCodeUseSoftFloat ? 0 : 16;
Andreas Gampe1a5c4062015-01-15 12:10:47 -080084 static constexpr bool kGprFprLockstep = false;
Zheng Xub551fdc2014-07-25 11:49:42 +080085 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset =
Vladimir Markofd36f1f2016-08-03 18:49:58 +010086 arm::ArmCalleeSaveFpr1Offset(Runtime::kSaveRefsAndArgs); // Offset of first FPR arg.
Zheng Xub551fdc2014-07-25 11:49:42 +080087 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset =
Vladimir Markofd36f1f2016-08-03 18:49:58 +010088 arm::ArmCalleeSaveGpr1Offset(Runtime::kSaveRefsAndArgs); // Offset of first GPR arg.
Zheng Xub551fdc2014-07-25 11:49:42 +080089 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset =
Vladimir Markofd36f1f2016-08-03 18:49:58 +010090 arm::ArmCalleeSaveLrOffset(Runtime::kSaveRefsAndArgs); // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -080091 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +000092 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -080093 }
Stuart Monteithb95a5342014-03-12 13:32:32 +000094#elif defined(__aarch64__)
95 // The callee save frame is pointed to by SP.
96 // | argN | |
97 // | ... | |
98 // | arg4 | |
99 // | arg3 spill | | Caller's frame
100 // | arg2 spill | |
101 // | arg1 spill | |
102 // | Method* | ---
103 // | LR |
Zheng Xub551fdc2014-07-25 11:49:42 +0800104 // | X29 |
Stuart Monteithb95a5342014-03-12 13:32:32 +0000105 // | : |
Serban Constantinescu9bd88b02015-04-22 16:24:46 +0100106 // | X20 |
Stuart Monteithb95a5342014-03-12 13:32:32 +0000107 // | X7 |
108 // | : |
109 // | X1 |
Zheng Xub551fdc2014-07-25 11:49:42 +0800110 // | D7 |
Stuart Monteithb95a5342014-03-12 13:32:32 +0000111 // | : |
112 // | D0 |
113 // | | padding
114 // | Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500115 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000116 static constexpr bool kAlignPairRegister = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000117 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800118 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000119 static constexpr bool kQuickSkipOddFpRegisters = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000120 static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs.
121 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800122 static constexpr bool kGprFprLockstep = false;
Zheng Xub551fdc2014-07-25 11:49:42 +0800123 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset =
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100124 arm64::Arm64CalleeSaveFpr1Offset(Runtime::kSaveRefsAndArgs); // Offset of first FPR arg.
Zheng Xub551fdc2014-07-25 11:49:42 +0800125 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset =
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100126 arm64::Arm64CalleeSaveGpr1Offset(Runtime::kSaveRefsAndArgs); // Offset of first GPR arg.
Zheng Xub551fdc2014-07-25 11:49:42 +0800127 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset =
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100128 arm64::Arm64CalleeSaveLrOffset(Runtime::kSaveRefsAndArgs); // Offset of return address.
Stuart Monteithb95a5342014-03-12 13:32:32 +0000129 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000130 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Stuart Monteithb95a5342014-03-12 13:32:32 +0000131 }
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800132#elif defined(__mips__) && !defined(__LP64__)
Ian Rogers848871b2013-08-05 10:56:33 -0700133 // The callee save frame is pointed to by SP.
134 // | argN | |
135 // | ... | |
136 // | arg4 | |
137 // | arg3 spill | | Caller's frame
138 // | arg2 spill | |
139 // | arg1 spill | |
140 // | Method* | ---
141 // | RA |
142 // | ... | callee saves
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800143 // | T1 | arg5
144 // | T0 | arg4
Ian Rogers848871b2013-08-05 10:56:33 -0700145 // | A3 | arg3
146 // | A2 | arg2
147 // | A1 | arg1
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800148 // | F19 |
149 // | F18 | f_arg5
150 // | F17 |
151 // | F16 | f_arg4
Goran Jakovljevicff734982015-08-24 12:58:55 +0000152 // | F15 |
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800153 // | F14 | f_arg3
Goran Jakovljevicff734982015-08-24 12:58:55 +0000154 // | F13 |
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800155 // | F12 | f_arg2
156 // | F11 |
157 // | F10 | f_arg1
158 // | F9 |
159 // | F8 | f_arg0
Goran Jakovljevicff734982015-08-24 12:58:55 +0000160 // | | padding
Ian Rogers848871b2013-08-05 10:56:33 -0700161 // | A0/Method* | <- sp
Goran Jakovljevicff734982015-08-24 12:58:55 +0000162 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
163 static constexpr bool kAlignPairRegister = true;
164 static constexpr bool kQuickSoftFloatAbi = false;
Zheng Xu5667fdb2014-10-23 18:29:55 +0800165 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000166 static constexpr bool kQuickSkipOddFpRegisters = true;
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800167 static constexpr size_t kNumQuickGprArgs = 5; // 5 arguments passed in GPRs.
168 static constexpr size_t kNumQuickFprArgs = 12; // 6 arguments passed in FPRs. Floats can be
169 // passed only in even numbered registers and each
170 // double occupies two registers.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800171 static constexpr bool kGprFprLockstep = false;
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800172 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 8; // Offset of first FPR arg.
173 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 56; // Offset of first GPR arg.
174 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 108; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800175 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000176 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800177 }
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800178#elif defined(__mips__) && defined(__LP64__)
179 // The callee save frame is pointed to by SP.
180 // | argN | |
181 // | ... | |
182 // | arg4 | |
183 // | arg3 spill | | Caller's frame
184 // | arg2 spill | |
185 // | arg1 spill | |
186 // | Method* | ---
187 // | RA |
188 // | ... | callee saves
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800189 // | A7 | arg7
190 // | A6 | arg6
191 // | A5 | arg5
192 // | A4 | arg4
193 // | A3 | arg3
194 // | A2 | arg2
195 // | A1 | arg1
Goran Jakovljevicff734982015-08-24 12:58:55 +0000196 // | F19 | f_arg7
197 // | F18 | f_arg6
198 // | F17 | f_arg5
199 // | F16 | f_arg4
200 // | F15 | f_arg3
201 // | F14 | f_arg2
202 // | F13 | f_arg1
203 // | F12 | f_arg0
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800204 // | | padding
205 // | A0/Method* | <- sp
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800206 // NOTE: for Mip64, when A0 is skipped, F12 is also skipped.
Douglas Leungd18e0832015-02-09 15:22:26 -0800207 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800208 static constexpr bool kAlignPairRegister = false;
209 static constexpr bool kQuickSoftFloatAbi = false;
210 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000211 static constexpr bool kQuickSkipOddFpRegisters = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800212 static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs.
213 static constexpr size_t kNumQuickFprArgs = 7; // 7 arguments passed in FPRs.
214 static constexpr bool kGprFprLockstep = true;
215
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800216 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 24; // Offset of first FPR arg (F13).
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800217 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 80; // Offset of first GPR arg (A1).
218 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 200; // Offset of return address.
219 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
220 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
221 }
Ian Rogers848871b2013-08-05 10:56:33 -0700222#elif defined(__i386__)
223 // The callee save frame is pointed to by SP.
224 // | argN | |
225 // | ... | |
226 // | arg4 | |
227 // | arg3 spill | | Caller's frame
228 // | arg2 spill | |
229 // | arg1 spill | |
230 // | Method* | ---
231 // | Return |
232 // | EBP,ESI,EDI | callee saves
233 // | EBX | arg3
234 // | EDX | arg2
235 // | ECX | arg1
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000236 // | XMM3 | float arg 4
237 // | XMM2 | float arg 3
238 // | XMM1 | float arg 2
239 // | XMM0 | float arg 1
Ian Rogers848871b2013-08-05 10:56:33 -0700240 // | EAX/Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500241 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000242 static constexpr bool kAlignPairRegister = false;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000243 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800244 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000245 static constexpr bool kQuickSkipOddFpRegisters = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800246 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs.
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000247 static constexpr size_t kNumQuickFprArgs = 4; // 4 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800248 static constexpr bool kGprFprLockstep = false;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000249 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 4; // Offset of first FPR arg.
250 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 4 + 4*8; // Offset of first GPR arg.
251 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 28 + 4*8; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800252 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000253 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800254 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800255#elif defined(__x86_64__)
Ian Rogers936b37f2014-02-14 00:52:24 -0800256 // The callee save frame is pointed to by SP.
257 // | argN | |
258 // | ... | |
259 // | reg. arg spills | | Caller's frame
260 // | Method* | ---
261 // | Return |
262 // | R15 | callee save
263 // | R14 | callee save
264 // | R13 | callee save
265 // | R12 | callee save
266 // | R9 | arg5
267 // | R8 | arg4
268 // | RSI/R6 | arg1
269 // | RBP/R5 | callee save
270 // | RBX/R3 | callee save
271 // | RDX/R2 | arg2
272 // | RCX/R1 | arg3
273 // | XMM7 | float arg 8
274 // | XMM6 | float arg 7
275 // | XMM5 | float arg 6
276 // | XMM4 | float arg 5
277 // | XMM3 | float arg 4
278 // | XMM2 | float arg 3
279 // | XMM1 | float arg 2
280 // | XMM0 | float arg 1
281 // | Padding |
282 // | RDI/Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500283 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000284 static constexpr bool kAlignPairRegister = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800285 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800286 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000287 static constexpr bool kQuickSkipOddFpRegisters = false;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700288 static constexpr size_t kNumQuickGprArgs = 5; // 5 arguments passed in GPRs.
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700289 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800290 static constexpr bool kGprFprLockstep = false;
Ian Rogers936b37f2014-02-14 00:52:24 -0800291 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 16; // Offset of first FPR arg.
Serguei Katkovc3801912014-07-08 17:21:53 +0700292 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 80 + 4*8; // Offset of first GPR arg.
293 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 168 + 4*8; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800294 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
295 switch (gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000296 case 0: return (4 * GetBytesPerGprSpillLocation(kRuntimeISA));
297 case 1: return (1 * GetBytesPerGprSpillLocation(kRuntimeISA));
298 case 2: return (0 * GetBytesPerGprSpillLocation(kRuntimeISA));
299 case 3: return (5 * GetBytesPerGprSpillLocation(kRuntimeISA));
300 case 4: return (6 * GetBytesPerGprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800301 default:
Andreas Gampec200a4a2014-06-16 18:39:09 -0700302 LOG(FATAL) << "Unexpected GPR index: " << gpr_index;
303 return 0;
Ian Rogers936b37f2014-02-14 00:52:24 -0800304 }
305 }
Ian Rogers848871b2013-08-05 10:56:33 -0700306#else
307#error "Unsupported architecture"
Ian Rogers848871b2013-08-05 10:56:33 -0700308#endif
309
Ian Rogers936b37f2014-02-14 00:52:24 -0800310 public:
Sebastien Hertza836bc92014-11-25 16:30:53 +0100311 // Special handling for proxy methods. Proxy methods are instance methods so the
312 // 'this' object is the 1st argument. They also have the same frame layout as the
313 // kRefAndArgs runtime method. Since 'this' is a reference, it is located in the
314 // 1st GPR.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700315 static mirror::Object* GetProxyThisObject(ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700316 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000317 CHECK((*sp)->IsProxyMethod());
Sebastien Hertza836bc92014-11-25 16:30:53 +0100318 CHECK_GT(kNumQuickGprArgs, 0u);
319 constexpr uint32_t kThisGprIndex = 0u; // 'this' is in the 1st GPR.
320 size_t this_arg_offset = kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset +
321 GprIndexToGprOffset(kThisGprIndex);
322 uint8_t* this_arg_address = reinterpret_cast<uint8_t*>(sp) + this_arg_offset;
323 return reinterpret_cast<StackReference<mirror::Object>*>(this_arg_address)->AsMirrorPtr();
324 }
325
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700326 static ArtMethod* GetCallingMethod(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700327 DCHECK((*sp)->IsCalleeSaveMethod());
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100328 return GetCalleeSaveMethodCaller(sp, Runtime::kSaveRefsAndArgs);
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +0100329 }
330
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700331 static ArtMethod* GetOuterMethod(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700332 DCHECK((*sp)->IsCalleeSaveMethod());
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100333 uint8_t* previous_sp =
334 reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700335 return *reinterpret_cast<ArtMethod**>(previous_sp);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100336 }
337
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700338 static uint32_t GetCallingDexPc(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700339 DCHECK((*sp)->IsCalleeSaveMethod());
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100340 const size_t callee_frame_size = GetCalleeSaveFrameSize(kRuntimeISA, Runtime::kSaveRefsAndArgs);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700341 ArtMethod** caller_sp = reinterpret_cast<ArtMethod**>(
342 reinterpret_cast<uintptr_t>(sp) + callee_frame_size);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100343 uintptr_t outer_pc = QuickArgumentVisitor::GetCallingPc(sp);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100344 const OatQuickMethodHeader* current_code = (*caller_sp)->GetOatQuickMethodHeader(outer_pc);
345 uintptr_t outer_pc_offset = current_code->NativeQuickPcOffset(outer_pc);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100346
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100347 if (current_code->IsOptimized()) {
348 CodeInfo code_info = current_code->GetOptimizedCodeInfo();
David Srbecky09ed0982016-02-12 21:58:43 +0000349 CodeInfoEncoding encoding = code_info.ExtractEncoding();
David Brazdilf677ebf2015-05-29 16:29:43 +0100350 StackMap stack_map = code_info.GetStackMapForNativePcOffset(outer_pc_offset, encoding);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100351 DCHECK(stack_map.IsValid());
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800352 if (stack_map.HasInlineInfo(encoding.stack_map.encoding)) {
David Brazdilf677ebf2015-05-29 16:29:43 +0100353 InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map, encoding);
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800354 return inline_info.GetDexPcAtDepth(encoding.inline_info.encoding,
355 inline_info.GetDepth(encoding.inline_info.encoding)-1);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100356 } else {
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800357 return stack_map.GetDexPc(encoding.stack_map.encoding);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100358 }
359 } else {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100360 return current_code->ToDexPc(*caller_sp, outer_pc);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100361 }
Ian Rogers848871b2013-08-05 10:56:33 -0700362 }
363
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800364 static bool GetInvokeType(ArtMethod** sp, InvokeType* invoke_type, uint32_t* dex_method_index)
365 REQUIRES_SHARED(Locks::mutator_lock_) {
366 DCHECK((*sp)->IsCalleeSaveMethod());
367 const size_t callee_frame_size = GetCalleeSaveFrameSize(kRuntimeISA, Runtime::kSaveRefsAndArgs);
368 ArtMethod** caller_sp = reinterpret_cast<ArtMethod**>(
369 reinterpret_cast<uintptr_t>(sp) + callee_frame_size);
370 uintptr_t outer_pc = QuickArgumentVisitor::GetCallingPc(sp);
371 const OatQuickMethodHeader* current_code = (*caller_sp)->GetOatQuickMethodHeader(outer_pc);
372 if (!current_code->IsOptimized()) {
373 return false;
374 }
375 uintptr_t outer_pc_offset = current_code->NativeQuickPcOffset(outer_pc);
376 CodeInfo code_info = current_code->GetOptimizedCodeInfo();
377 CodeInfoEncoding encoding = code_info.ExtractEncoding();
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700378 MethodInfo method_info = current_code->GetOptimizedMethodInfo();
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800379 InvokeInfo invoke(code_info.GetInvokeInfoForNativePcOffset(outer_pc_offset, encoding));
380 if (invoke.IsValid()) {
381 *invoke_type = static_cast<InvokeType>(invoke.GetInvokeType(encoding.invoke_info.encoding));
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700382 *dex_method_index = invoke.GetMethodIndex(encoding.invoke_info.encoding, method_info);
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800383 return true;
384 }
385 return false;
386 }
387
Ian Rogers936b37f2014-02-14 00:52:24 -0800388 // For the given quick ref and args quick frame, return the caller's PC.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700389 static uintptr_t GetCallingPc(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700390 DCHECK((*sp)->IsCalleeSaveMethod());
Ian Rogers13735952014-10-08 12:43:28 -0700391 uint8_t* lr = reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_LrOffset;
Ian Rogers848871b2013-08-05 10:56:33 -0700392 return *reinterpret_cast<uintptr_t*>(lr);
393 }
394
Mathieu Chartiere401d142015-04-22 13:56:20 -0700395 QuickArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700396 uint32_t shorty_len) REQUIRES_SHARED(Locks::mutator_lock_) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700397 is_static_(is_static), shorty_(shorty), shorty_len_(shorty_len),
Ian Rogers13735952014-10-08 12:43:28 -0700398 gpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset),
399 fpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset),
400 stack_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize
Mathieu Chartiere401d142015-04-22 13:56:20 -0700401 + sizeof(ArtMethod*)), // Skip ArtMethod*.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800402 gpr_index_(0), fpr_index_(0), fpr_double_index_(0), stack_index_(0),
403 cur_type_(Primitive::kPrimVoid), is_split_long_or_double_(false) {
Andreas Gampe575e78c2014-11-03 23:41:03 -0800404 static_assert(kQuickSoftFloatAbi == (kNumQuickFprArgs == 0),
405 "Number of Quick FPR arguments unexpected");
406 static_assert(!(kQuickSoftFloatAbi && kQuickDoubleRegAlignedFloatBackFilled),
407 "Double alignment unexpected");
Zheng Xu5667fdb2014-10-23 18:29:55 +0800408 // For register alignment, we want to assume that counters(fpr_double_index_) are even if the
409 // next register is even.
Andreas Gampe575e78c2014-11-03 23:41:03 -0800410 static_assert(!kQuickDoubleRegAlignedFloatBackFilled || kNumQuickFprArgs % 2 == 0,
411 "Number of Quick FPR arguments not even");
Andreas Gampe542451c2016-07-26 09:02:02 -0700412 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Zheng Xu5667fdb2014-10-23 18:29:55 +0800413 }
Ian Rogers848871b2013-08-05 10:56:33 -0700414
415 virtual ~QuickArgumentVisitor() {}
416
417 virtual void Visit() = 0;
418
Ian Rogers936b37f2014-02-14 00:52:24 -0800419 Primitive::Type GetParamPrimitiveType() const {
420 return cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700421 }
422
Ian Rogers13735952014-10-08 12:43:28 -0700423 uint8_t* GetParamAddress() const {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800424 if (!kQuickSoftFloatAbi) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800425 Primitive::Type type = GetParamPrimitiveType();
426 if (UNLIKELY((type == Primitive::kPrimDouble) || (type == Primitive::kPrimFloat))) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800427 if (type == Primitive::kPrimDouble && kQuickDoubleRegAlignedFloatBackFilled) {
428 if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) {
429 return fpr_args_ + (fpr_double_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
430 }
431 } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000432 return fpr_args_ + (fpr_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800433 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700434 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers936b37f2014-02-14 00:52:24 -0800435 }
436 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800437 if (gpr_index_ < kNumQuickGprArgs) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800438 return gpr_args_ + GprIndexToGprOffset(gpr_index_);
439 }
440 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700441 }
442
443 bool IsSplitLongOrDouble() const {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700444 if ((GetBytesPerGprSpillLocation(kRuntimeISA) == 4) ||
445 (GetBytesPerFprSpillLocation(kRuntimeISA) == 4)) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800446 return is_split_long_or_double_;
447 } else {
448 return false; // An optimization for when GPR and FPRs are 64bit.
449 }
Ian Rogers848871b2013-08-05 10:56:33 -0700450 }
451
Ian Rogers936b37f2014-02-14 00:52:24 -0800452 bool IsParamAReference() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700453 return GetParamPrimitiveType() == Primitive::kPrimNot;
454 }
455
Ian Rogers936b37f2014-02-14 00:52:24 -0800456 bool IsParamALongOrDouble() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700457 Primitive::Type type = GetParamPrimitiveType();
458 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
459 }
460
461 uint64_t ReadSplitLongParam() const {
Nicolas Geoffray425f2392015-01-08 14:52:29 +0000462 // The splitted long is always available through the stack.
463 return *reinterpret_cast<uint64_t*>(stack_args_
464 + stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700465 }
466
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800467 void IncGprIndex() {
468 gpr_index_++;
469 if (kGprFprLockstep) {
470 fpr_index_++;
471 }
472 }
473
474 void IncFprIndex() {
475 fpr_index_++;
476 if (kGprFprLockstep) {
477 gpr_index_++;
478 }
479 }
480
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700481 void VisitArguments() REQUIRES_SHARED(Locks::mutator_lock_) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800482 // (a) 'stack_args_' should point to the first method's argument
483 // (b) whatever the argument type it is, the 'stack_index_' should
484 // be moved forward along with every visiting.
Ian Rogers936b37f2014-02-14 00:52:24 -0800485 gpr_index_ = 0;
486 fpr_index_ = 0;
Zheng Xu5667fdb2014-10-23 18:29:55 +0800487 if (kQuickDoubleRegAlignedFloatBackFilled) {
488 fpr_double_index_ = 0;
489 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800490 stack_index_ = 0;
491 if (!is_static_) { // Handle this.
492 cur_type_ = Primitive::kPrimNot;
493 is_split_long_or_double_ = false;
Ian Rogers848871b2013-08-05 10:56:33 -0700494 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800495 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800496 if (kNumQuickGprArgs > 0) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800497 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800498 }
Ian Rogers848871b2013-08-05 10:56:33 -0700499 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800500 for (uint32_t shorty_index = 1; shorty_index < shorty_len_; ++shorty_index) {
501 cur_type_ = Primitive::GetType(shorty_[shorty_index]);
502 switch (cur_type_) {
503 case Primitive::kPrimNot:
504 case Primitive::kPrimBoolean:
505 case Primitive::kPrimByte:
506 case Primitive::kPrimChar:
507 case Primitive::kPrimShort:
508 case Primitive::kPrimInt:
509 is_split_long_or_double_ = false;
510 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800511 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800512 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800513 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800514 }
515 break;
516 case Primitive::kPrimFloat:
517 is_split_long_or_double_ = false;
518 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800519 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800520 if (kQuickSoftFloatAbi) {
521 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800522 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800523 }
524 } else {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800525 if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800526 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800527 if (kQuickDoubleRegAlignedFloatBackFilled) {
528 // Double should not overlap with float.
529 // For example, if fpr_index_ = 3, fpr_double_index_ should be at least 4.
530 fpr_double_index_ = std::max(fpr_double_index_, RoundUp(fpr_index_, 2));
531 // Float should not overlap with double.
532 if (fpr_index_ % 2 == 0) {
533 fpr_index_ = std::max(fpr_double_index_, fpr_index_);
534 }
Goran Jakovljevicff734982015-08-24 12:58:55 +0000535 } else if (kQuickSkipOddFpRegisters) {
536 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800537 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800538 }
539 }
540 break;
541 case Primitive::kPrimDouble:
542 case Primitive::kPrimLong:
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800543 if (kQuickSoftFloatAbi || (cur_type_ == Primitive::kPrimLong)) {
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800544 if (cur_type_ == Primitive::kPrimLong &&
545#if defined(__mips__) && !defined(__LP64__)
546 (gpr_index_ == 0 || gpr_index_ == 2) &&
547#else
548 gpr_index_ == 0 &&
549#endif
550 kAlignPairRegister) {
551 // Currently, this is only for ARM and MIPS, where we align long parameters with
552 // even-numbered registers by skipping R1 (on ARM) or A1(A3) (on MIPS) and using
553 // R2 (on ARM) or A2(T0) (on MIPS) instead.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800554 IncGprIndex();
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000555 }
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000556 is_split_long_or_double_ = (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) &&
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800557 ((gpr_index_ + 1) == kNumQuickGprArgs);
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500558 if (!kSplitPairAcrossRegisterAndStack && is_split_long_or_double_) {
559 // We don't want to split this. Pass over this register.
560 gpr_index_++;
561 is_split_long_or_double_ = false;
562 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800563 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800564 if (kBytesStackArgLocation == 4) {
565 stack_index_+= 2;
566 } else {
567 CHECK_EQ(kBytesStackArgLocation, 8U);
568 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800569 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700570 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800571 IncGprIndex();
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000572 if (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700573 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800574 IncGprIndex();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700575 }
576 }
577 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800578 } else {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000579 is_split_long_or_double_ = (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) &&
Zheng Xu5667fdb2014-10-23 18:29:55 +0800580 ((fpr_index_ + 1) == kNumQuickFprArgs) && !kQuickDoubleRegAlignedFloatBackFilled;
Ian Rogers936b37f2014-02-14 00:52:24 -0800581 Visit();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700582 if (kBytesStackArgLocation == 4) {
583 stack_index_+= 2;
Ian Rogers936b37f2014-02-14 00:52:24 -0800584 } else {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700585 CHECK_EQ(kBytesStackArgLocation, 8U);
586 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800587 }
Zheng Xu5667fdb2014-10-23 18:29:55 +0800588 if (kQuickDoubleRegAlignedFloatBackFilled) {
589 if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) {
590 fpr_double_index_ += 2;
591 // Float should not overlap with double.
592 if (fpr_index_ % 2 == 0) {
593 fpr_index_ = std::max(fpr_double_index_, fpr_index_);
594 }
595 }
596 } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800597 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800598 if (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) {
599 if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800600 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800601 }
602 }
603 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800604 }
605 break;
606 default:
607 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty_;
608 }
Ian Rogers848871b2013-08-05 10:56:33 -0700609 }
610 }
611
Andreas Gampec200a4a2014-06-16 18:39:09 -0700612 protected:
Ian Rogers848871b2013-08-05 10:56:33 -0700613 const bool is_static_;
614 const char* const shorty_;
615 const uint32_t shorty_len_;
Andreas Gampec200a4a2014-06-16 18:39:09 -0700616
617 private:
Ian Rogers13735952014-10-08 12:43:28 -0700618 uint8_t* const gpr_args_; // Address of GPR arguments in callee save frame.
619 uint8_t* const fpr_args_; // Address of FPR arguments in callee save frame.
620 uint8_t* const stack_args_; // Address of stack arguments in caller's frame.
Ian Rogers936b37f2014-02-14 00:52:24 -0800621 uint32_t gpr_index_; // Index into spilled GPRs.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800622 // Index into spilled FPRs.
623 // In case kQuickDoubleRegAlignedFloatBackFilled, it may index a hole while fpr_double_index_
624 // holds a higher register number.
625 uint32_t fpr_index_;
626 // Index into spilled FPRs for aligned double.
627 // Only used when kQuickDoubleRegAlignedFloatBackFilled. Next available double register indexed in
628 // terms of singles, may be behind fpr_index.
629 uint32_t fpr_double_index_;
Ian Rogers936b37f2014-02-14 00:52:24 -0800630 uint32_t stack_index_; // Index into arguments on the stack.
631 // The current type of argument during VisitArguments.
632 Primitive::Type cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700633 // Does a 64bit parameter straddle the register and stack arguments?
634 bool is_split_long_or_double_;
635};
636
Sebastien Hertza836bc92014-11-25 16:30:53 +0100637// Returns the 'this' object of a proxy method. This function is only used by StackVisitor. It
638// allows to use the QuickArgumentVisitor constants without moving all the code in its own module.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700639extern "C" mirror::Object* artQuickGetProxyThisObject(ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700640 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertza836bc92014-11-25 16:30:53 +0100641 return QuickArgumentVisitor::GetProxyThisObject(sp);
642}
643
Ian Rogers848871b2013-08-05 10:56:33 -0700644// Visits arguments on the stack placing them into the shadow frame.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800645class BuildQuickShadowFrameVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700646 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700647 BuildQuickShadowFrameVisitor(ArtMethod** sp, bool is_static, const char* shorty,
648 uint32_t shorty_len, ShadowFrame* sf, size_t first_arg_reg) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700649 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), sf_(sf), cur_reg_(first_arg_reg) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700650
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700651 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700652
653 private:
Ian Rogers936b37f2014-02-14 00:52:24 -0800654 ShadowFrame* const sf_;
655 uint32_t cur_reg_;
Ian Rogers848871b2013-08-05 10:56:33 -0700656
Dragos Sbirleabd136a22013-08-13 18:07:04 -0700657 DISALLOW_COPY_AND_ASSIGN(BuildQuickShadowFrameVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700658};
659
Andreas Gampec200a4a2014-06-16 18:39:09 -0700660void BuildQuickShadowFrameVisitor::Visit() {
Ian Rogers9758f792014-03-13 09:02:55 -0700661 Primitive::Type type = GetParamPrimitiveType();
662 switch (type) {
663 case Primitive::kPrimLong: // Fall-through.
664 case Primitive::kPrimDouble:
665 if (IsSplitLongOrDouble()) {
666 sf_->SetVRegLong(cur_reg_, ReadSplitLongParam());
667 } else {
668 sf_->SetVRegLong(cur_reg_, *reinterpret_cast<jlong*>(GetParamAddress()));
669 }
670 ++cur_reg_;
671 break;
672 case Primitive::kPrimNot: {
673 StackReference<mirror::Object>* stack_ref =
674 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
675 sf_->SetVRegReference(cur_reg_, stack_ref->AsMirrorPtr());
676 }
677 break;
678 case Primitive::kPrimBoolean: // Fall-through.
679 case Primitive::kPrimByte: // Fall-through.
680 case Primitive::kPrimChar: // Fall-through.
681 case Primitive::kPrimShort: // Fall-through.
682 case Primitive::kPrimInt: // Fall-through.
683 case Primitive::kPrimFloat:
684 sf_->SetVReg(cur_reg_, *reinterpret_cast<jint*>(GetParamAddress()));
685 break;
686 case Primitive::kPrimVoid:
687 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700688 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -0700689 }
690 ++cur_reg_;
691}
692
Mathieu Chartiere401d142015-04-22 13:56:20 -0700693extern "C" uint64_t artQuickToInterpreterBridge(ArtMethod* method, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700694 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers848871b2013-08-05 10:56:33 -0700695 // Ensure we don't get thread suspension until the object arguments are safely in the shadow
696 // frame.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700697 ScopedQuickEntrypointChecks sqec(self);
Ian Rogers848871b2013-08-05 10:56:33 -0700698
Alex Light9139e002015-10-09 15:59:48 -0700699 if (UNLIKELY(!method->IsInvokable())) {
700 method->ThrowInvocationTimeError();
Ian Rogers848871b2013-08-05 10:56:33 -0700701 return 0;
Andreas Gampe639bdd12015-06-03 11:22:45 -0700702 }
703
704 JValue tmp_value;
705 ShadowFrame* deopt_frame = self->PopStackedShadowFrame(
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700706 StackedShadowFrameType::kDeoptimizationShadowFrame, false);
Andreas Gampe639bdd12015-06-03 11:22:45 -0700707 ManagedStack fragment;
708
David Sehr709b0702016-10-13 09:12:37 -0700709 DCHECK(!method->IsNative()) << method->PrettyMethod();
Andreas Gampe639bdd12015-06-03 11:22:45 -0700710 uint32_t shorty_len = 0;
Andreas Gampe542451c2016-07-26 09:02:02 -0700711 ArtMethod* non_proxy_method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Alex Lighte9dd04f2016-03-16 16:09:45 -0700712 const DexFile::CodeItem* code_item = non_proxy_method->GetCodeItem();
David Sehr709b0702016-10-13 09:12:37 -0700713 DCHECK(code_item != nullptr) << method->PrettyMethod();
Andreas Gampe639bdd12015-06-03 11:22:45 -0700714 const char* shorty = non_proxy_method->GetShorty(&shorty_len);
715
716 JValue result;
717
718 if (deopt_frame != nullptr) {
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700719 // Coming from partial-fragment deopt.
Andreas Gampe639bdd12015-06-03 11:22:45 -0700720
721 if (kIsDebugBuild) {
722 // Sanity-check: are the methods as expected? We check that the last shadow frame (the bottom
723 // of the call-stack) corresponds to the called method.
724 ShadowFrame* linked = deopt_frame;
725 while (linked->GetLink() != nullptr) {
726 linked = linked->GetLink();
727 }
David Sehr709b0702016-10-13 09:12:37 -0700728 CHECK_EQ(method, linked->GetMethod()) << method->PrettyMethod() << " "
729 << ArtMethod::PrettyMethod(linked->GetMethod());
Andreas Gampe639bdd12015-06-03 11:22:45 -0700730 }
731
732 if (VLOG_IS_ON(deopt)) {
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700733 // Print out the stack to verify that it was a partial-fragment deopt.
Andreas Gampe639bdd12015-06-03 11:22:45 -0700734 LOG(INFO) << "Continue-ing from deopt. Stack is:";
735 QuickExceptionHandler::DumpFramesWithType(self, true);
736 }
737
Mathieu Chartierf5769e12017-01-10 15:54:41 -0800738 ObjPtr<mirror::Throwable> pending_exception;
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100739 bool from_code = false;
740 self->PopDeoptimizationContext(&result, &pending_exception, /* out */ &from_code);
Andreas Gampe639bdd12015-06-03 11:22:45 -0700741
742 // Push a transition back into managed code onto the linked list in thread.
743 self->PushManagedStackFragment(&fragment);
744
745 // Ensure that the stack is still in order.
746 if (kIsDebugBuild) {
747 class DummyStackVisitor : public StackVisitor {
748 public:
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700749 explicit DummyStackVisitor(Thread* self_in) REQUIRES_SHARED(Locks::mutator_lock_)
Andreas Gampe639bdd12015-06-03 11:22:45 -0700750 : StackVisitor(self_in, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {}
751
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700752 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe639bdd12015-06-03 11:22:45 -0700753 // Nothing to do here. In a debug build, SanityCheckFrame will do the work in the walking
754 // logic. Just always say we want to continue.
755 return true;
756 }
757 };
758 DummyStackVisitor dsv(self);
759 dsv.WalkStack();
760 }
761
762 // Restore the exception that was pending before deoptimization then interpret the
763 // deoptimized frames.
764 if (pending_exception != nullptr) {
765 self->SetException(pending_exception);
766 }
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100767 interpreter::EnterInterpreterFromDeoptimize(self, deopt_frame, from_code, &result);
Ian Rogers848871b2013-08-05 10:56:33 -0700768 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -0700769 const char* old_cause = self->StartAssertNoThreadSuspension(
770 "Building interpreter shadow frame");
Ian Rogers848871b2013-08-05 10:56:33 -0700771 uint16_t num_regs = code_item->registers_size_;
Andreas Gampec200a4a2014-06-16 18:39:09 -0700772 // No last shadow coming from quick.
Andreas Gampeb3025922015-09-01 14:45:00 -0700773 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
Andreas Gampe03ec9302015-08-27 17:41:47 -0700774 CREATE_SHADOW_FRAME(num_regs, /* link */ nullptr, method, /* dex pc */ 0);
Andreas Gampeb3025922015-09-01 14:45:00 -0700775 ShadowFrame* shadow_frame = shadow_frame_unique_ptr.get();
Ian Rogers848871b2013-08-05 10:56:33 -0700776 size_t first_arg_reg = code_item->registers_size_ - code_item->ins_size_;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700777 BuildQuickShadowFrameVisitor shadow_frame_builder(sp, method->IsStatic(), shorty, shorty_len,
Ian Rogers936b37f2014-02-14 00:52:24 -0800778 shadow_frame, first_arg_reg);
Ian Rogers848871b2013-08-05 10:56:33 -0700779 shadow_frame_builder.VisitArguments();
Ian Rogerse94652f2014-12-02 11:13:19 -0800780 const bool needs_initialization =
781 method->IsStatic() && !method->GetDeclaringClass()->IsInitialized();
Ian Rogers848871b2013-08-05 10:56:33 -0700782 // Push a transition back into managed code onto the linked list in thread.
Ian Rogers848871b2013-08-05 10:56:33 -0700783 self->PushManagedStackFragment(&fragment);
784 self->PushShadowFrame(shadow_frame);
785 self->EndAssertNoThreadSuspension(old_cause);
786
Ian Rogerse94652f2014-12-02 11:13:19 -0800787 if (needs_initialization) {
Ian Rogers848871b2013-08-05 10:56:33 -0700788 // Ensure static method's class is initialized.
Ian Rogerse94652f2014-12-02 11:13:19 -0800789 StackHandleScope<1> hs(self);
790 Handle<mirror::Class> h_class(hs.NewHandle(shadow_frame->GetMethod()->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700791 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
David Sehr709b0702016-10-13 09:12:37 -0700792 DCHECK(Thread::Current()->IsExceptionPending())
793 << shadow_frame->GetMethod()->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700794 self->PopManagedStackFragment(fragment);
795 return 0;
796 }
797 }
Daniel Mihalyieb076692014-08-22 17:33:31 +0200798
Andreas Gampe639bdd12015-06-03 11:22:45 -0700799 result = interpreter::EnterInterpreterFromEntryPoint(self, code_item, shadow_frame);
Ian Rogers848871b2013-08-05 10:56:33 -0700800 }
Andreas Gampe639bdd12015-06-03 11:22:45 -0700801
802 // Pop transition.
803 self->PopManagedStackFragment(fragment);
804
805 // Request a stack deoptimization if needed
806 ArtMethod* caller = QuickArgumentVisitor::GetCallingMethod(sp);
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700807 uintptr_t caller_pc = QuickArgumentVisitor::GetCallingPc(sp);
Mingyao Yanga3549d22016-06-02 17:01:02 -0700808 // If caller_pc is the instrumentation exit stub, the stub will check to see if deoptimization
809 // should be done and it knows the real return pc.
810 if (UNLIKELY(caller_pc != reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) &&
Nicolas Geoffray433b79a2017-01-30 20:54:45 +0000811 Dbg::IsForcedInterpreterNeededForUpcall(self, caller))) {
812 if (!Runtime::Current()->IsAsyncDeoptimizeable(caller_pc)) {
813 LOG(WARNING) << "Got a deoptimization request on un-deoptimizable method "
814 << caller->PrettyMethod();
815 } else {
816 // Push the context of the deoptimization stack so we can restore the return value and the
817 // exception before executing the deoptimized frames.
818 self->PushDeoptimizationContext(
819 result, shorty[0] == 'L', /* from_code */ false, self->GetException());
Andreas Gampe639bdd12015-06-03 11:22:45 -0700820
Nicolas Geoffray433b79a2017-01-30 20:54:45 +0000821 // Set special exception to cause deoptimization.
822 self->SetException(Thread::GetDeoptimizationException());
823 }
Andreas Gampe639bdd12015-06-03 11:22:45 -0700824 }
825
826 // No need to restore the args since the method has already been run by the interpreter.
827 return result.GetJ();
Ian Rogers848871b2013-08-05 10:56:33 -0700828}
829
830// Visits arguments on the stack placing them into the args vector, Object* arguments are converted
831// to jobjects.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800832class BuildQuickArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700833 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700834 BuildQuickArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty, uint32_t shorty_len,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700835 ScopedObjectAccessUnchecked* soa, std::vector<jvalue>* args) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700836 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa), args_(args) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700837
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700838 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700839
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700840 void FixupReferences() REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800841
Ian Rogers848871b2013-08-05 10:56:33 -0700842 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700843 ScopedObjectAccessUnchecked* const soa_;
844 std::vector<jvalue>* const args_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800845 // References which we must update when exiting in case the GC moved the objects.
Ian Rogers700a4022014-05-19 16:49:03 -0700846 std::vector<std::pair<jobject, StackReference<mirror::Object>*>> references_;
Ian Rogers9758f792014-03-13 09:02:55 -0700847
Ian Rogers848871b2013-08-05 10:56:33 -0700848 DISALLOW_COPY_AND_ASSIGN(BuildQuickArgumentVisitor);
849};
850
Ian Rogers9758f792014-03-13 09:02:55 -0700851void BuildQuickArgumentVisitor::Visit() {
852 jvalue val;
853 Primitive::Type type = GetParamPrimitiveType();
854 switch (type) {
855 case Primitive::kPrimNot: {
856 StackReference<mirror::Object>* stack_ref =
857 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
858 val.l = soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
859 references_.push_back(std::make_pair(val.l, stack_ref));
860 break;
861 }
862 case Primitive::kPrimLong: // Fall-through.
863 case Primitive::kPrimDouble:
864 if (IsSplitLongOrDouble()) {
865 val.j = ReadSplitLongParam();
866 } else {
867 val.j = *reinterpret_cast<jlong*>(GetParamAddress());
868 }
869 break;
870 case Primitive::kPrimBoolean: // Fall-through.
871 case Primitive::kPrimByte: // Fall-through.
872 case Primitive::kPrimChar: // Fall-through.
873 case Primitive::kPrimShort: // Fall-through.
874 case Primitive::kPrimInt: // Fall-through.
875 case Primitive::kPrimFloat:
876 val.i = *reinterpret_cast<jint*>(GetParamAddress());
877 break;
878 case Primitive::kPrimVoid:
879 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700880 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -0700881 }
882 args_->push_back(val);
883}
884
885void BuildQuickArgumentVisitor::FixupReferences() {
886 // Fixup any references which may have changed.
887 for (const auto& pair : references_) {
Mathieu Chartier1a5337f2016-10-13 13:48:23 -0700888 pair.second->Assign(soa_->Decode<mirror::Object>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -0700889 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -0700890 }
891}
892
Ian Rogers848871b2013-08-05 10:56:33 -0700893// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
894// which is responsible for recording callee save registers. We explicitly place into jobjects the
895// incoming reference arguments (so they survive GC). We invoke the invocation handler, which is a
896// field within the proxy object, which will box the primitive arguments and deal with error cases.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700897extern "C" uint64_t artQuickProxyInvokeHandler(
898 ArtMethod* proxy_method, mirror::Object* receiver, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700899 REQUIRES_SHARED(Locks::mutator_lock_) {
David Sehr709b0702016-10-13 09:12:37 -0700900 DCHECK(proxy_method->IsProxyMethod()) << proxy_method->PrettyMethod();
901 DCHECK(receiver->GetClass()->IsProxyClass()) << proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700902 // Ensure we don't get thread suspension until the object arguments are safely in jobjects.
903 const char* old_cause =
904 self->StartAssertNoThreadSuspension("Adding to IRT proxy object arguments");
905 // Register the top of the managed stack, making stack crawlable.
David Sehr709b0702016-10-13 09:12:37 -0700906 DCHECK_EQ((*sp), proxy_method) << proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700907 self->VerifyStack();
908 // Start new JNI local reference state.
909 JNIEnvExt* env = self->GetJniEnv();
910 ScopedObjectAccessUnchecked soa(env);
911 ScopedJniEnvLocalRefState env_state(env);
912 // Create local ref. copies of proxy method and the receiver.
913 jobject rcvr_jobj = soa.AddLocalReference<jobject>(receiver);
914
915 // Placing arguments into args vector and remove the receiver.
Andreas Gampe542451c2016-07-26 09:02:02 -0700916 ArtMethod* non_proxy_method = proxy_method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
David Sehr709b0702016-10-13 09:12:37 -0700917 CHECK(!non_proxy_method->IsStatic()) << proxy_method->PrettyMethod() << " "
918 << non_proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700919 std::vector<jvalue> args;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700920 uint32_t shorty_len = 0;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700921 const char* shorty = non_proxy_method->GetShorty(&shorty_len);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700922 BuildQuickArgumentVisitor local_ref_visitor(sp, false, shorty, shorty_len, &soa, &args);
Brian Carlstromd3633d52013-08-20 21:06:26 -0700923
Ian Rogers848871b2013-08-05 10:56:33 -0700924 local_ref_visitor.VisitArguments();
David Sehr709b0702016-10-13 09:12:37 -0700925 DCHECK_GT(args.size(), 0U) << proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700926 args.erase(args.begin());
927
928 // Convert proxy method into expected interface method.
Andreas Gampe542451c2016-07-26 09:02:02 -0700929 ArtMethod* interface_method = proxy_method->FindOverriddenMethod(kRuntimePointerSize);
David Sehr709b0702016-10-13 09:12:37 -0700930 DCHECK(interface_method != nullptr) << proxy_method->PrettyMethod();
931 DCHECK(!interface_method->IsProxyMethod()) << interface_method->PrettyMethod();
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700932 self->EndAssertNoThreadSuspension(old_cause);
Andreas Gampe542451c2016-07-26 09:02:02 -0700933 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Andreas Gampee01e3642016-07-25 13:06:04 -0700934 DCHECK(!Runtime::Current()->IsActiveTransaction());
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700935 jobject interface_method_jobj = soa.AddLocalReference<jobject>(
Andreas Gampe542451c2016-07-26 09:02:02 -0700936 mirror::Method::CreateFromArtMethod<kRuntimePointerSize, false>(soa.Self(),
937 interface_method));
Ian Rogers848871b2013-08-05 10:56:33 -0700938
939 // All naked Object*s should now be in jobjects, so its safe to go into the main invoke code
940 // that performs allocations.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700941 JValue result = InvokeProxyInvocationHandler(soa, shorty, rcvr_jobj, interface_method_jobj, args);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800942 // Restore references which might have moved.
943 local_ref_visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -0700944 return result.GetJ();
945}
946
947// Read object references held in arguments from quick frames and place in a JNI local references,
948// so they don't get garbage collected.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800949class RememberForGcArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700950 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700951 RememberForGcArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty,
952 uint32_t shorty_len, ScopedObjectAccessUnchecked* soa) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700953 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700954
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700955 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Mathieu Chartier07d447b2013-09-26 11:57:43 -0700956
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700957 void FixupReferences() REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers848871b2013-08-05 10:56:33 -0700958
959 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700960 ScopedObjectAccessUnchecked* const soa_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800961 // References which we must update when exiting in case the GC moved the objects.
Andreas Gampec200a4a2014-06-16 18:39:09 -0700962 std::vector<std::pair<jobject, StackReference<mirror::Object>*> > references_;
963
Mathieu Chartier590fee92013-09-13 13:46:47 -0700964 DISALLOW_COPY_AND_ASSIGN(RememberForGcArgumentVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700965};
966
Ian Rogers9758f792014-03-13 09:02:55 -0700967void RememberForGcArgumentVisitor::Visit() {
968 if (IsParamAReference()) {
969 StackReference<mirror::Object>* stack_ref =
970 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
971 jobject reference =
972 soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
973 references_.push_back(std::make_pair(reference, stack_ref));
974 }
975}
976
977void RememberForGcArgumentVisitor::FixupReferences() {
978 // Fixup any references which may have changed.
979 for (const auto& pair : references_) {
Mathieu Chartier1a5337f2016-10-13 13:48:23 -0700980 pair.second->Assign(soa_->Decode<mirror::Object>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -0700981 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -0700982 }
983}
984
Ian Rogers848871b2013-08-05 10:56:33 -0700985// Lazily resolve a method for quick. Called by stub code.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700986extern "C" const void* artQuickResolutionTrampoline(
987 ArtMethod* called, mirror::Object* receiver, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700988 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe3b45ef22015-05-26 21:34:09 -0700989 // The resolution trampoline stashes the resolved method into the callee-save frame to transport
990 // it. Thus, when exiting, the stack cannot be verified (as the resolved method most likely
991 // does not have the same stack layout as the callee-save method).
992 ScopedQuickEntrypointChecks sqec(self, kIsDebugBuild, false);
Ian Rogers848871b2013-08-05 10:56:33 -0700993 // Start new JNI local reference state
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800994 JNIEnvExt* env = self->GetJniEnv();
Ian Rogers848871b2013-08-05 10:56:33 -0700995 ScopedObjectAccessUnchecked soa(env);
996 ScopedJniEnvLocalRefState env_state(env);
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800997 const char* old_cause = self->StartAssertNoThreadSuspension("Quick method resolution set up");
Ian Rogers848871b2013-08-05 10:56:33 -0700998
999 // Compute details about the called method (avoid GCs)
1000 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Ian Rogers848871b2013-08-05 10:56:33 -07001001 InvokeType invoke_type;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001002 MethodReference called_method(nullptr, 0);
1003 const bool called_method_known_on_entry = !called->IsRuntimeMethod();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001004 ArtMethod* caller = nullptr;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001005 if (!called_method_known_on_entry) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01001006 caller = QuickArgumentVisitor::GetCallingMethod(sp);
Ian Rogerse0a02da2014-12-02 14:10:53 -08001007 called_method.dex_file = caller->GetDexFile();
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001008
1009 InvokeType stack_map_invoke_type;
1010 uint32_t stack_map_dex_method_idx;
1011 const bool found_stack_map = QuickArgumentVisitor::GetInvokeType(sp,
1012 &stack_map_invoke_type,
1013 &stack_map_dex_method_idx);
1014 // For debug builds, we make sure both of the paths are consistent by also looking at the dex
1015 // code.
1016 if (!found_stack_map || kIsDebugBuild) {
1017 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
1018 const DexFile::CodeItem* code;
1019 code = caller->GetCodeItem();
1020 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
1021 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
1022 Instruction::Code instr_code = instr->Opcode();
1023 bool is_range;
1024 switch (instr_code) {
1025 case Instruction::INVOKE_DIRECT:
1026 invoke_type = kDirect;
1027 is_range = false;
1028 break;
1029 case Instruction::INVOKE_DIRECT_RANGE:
1030 invoke_type = kDirect;
1031 is_range = true;
1032 break;
1033 case Instruction::INVOKE_STATIC:
1034 invoke_type = kStatic;
1035 is_range = false;
1036 break;
1037 case Instruction::INVOKE_STATIC_RANGE:
1038 invoke_type = kStatic;
1039 is_range = true;
1040 break;
1041 case Instruction::INVOKE_SUPER:
1042 invoke_type = kSuper;
1043 is_range = false;
1044 break;
1045 case Instruction::INVOKE_SUPER_RANGE:
1046 invoke_type = kSuper;
1047 is_range = true;
1048 break;
1049 case Instruction::INVOKE_VIRTUAL:
1050 invoke_type = kVirtual;
1051 is_range = false;
1052 break;
1053 case Instruction::INVOKE_VIRTUAL_RANGE:
1054 invoke_type = kVirtual;
1055 is_range = true;
1056 break;
1057 case Instruction::INVOKE_INTERFACE:
1058 invoke_type = kInterface;
1059 is_range = false;
1060 break;
1061 case Instruction::INVOKE_INTERFACE_RANGE:
1062 invoke_type = kInterface;
1063 is_range = true;
1064 break;
1065 default:
1066 LOG(FATAL) << "Unexpected call into trampoline: " << instr->DumpString(nullptr);
1067 UNREACHABLE();
1068 }
1069 called_method.dex_method_index = (is_range) ? instr->VRegB_3rc() : instr->VRegB_35c();
1070 // Check that the invoke matches what we expected, note that this path only happens for debug
1071 // builds.
1072 if (found_stack_map) {
1073 DCHECK_EQ(stack_map_invoke_type, invoke_type);
1074 if (invoke_type != kSuper) {
1075 // Super may be sharpened.
1076 DCHECK_EQ(stack_map_dex_method_idx, called_method.dex_method_index)
1077 << called_method.dex_file->PrettyMethod(stack_map_dex_method_idx) << " "
1078 << called_method.dex_file->PrettyMethod(called_method.dex_method_index);
1079 }
1080 } else {
Andreas Gampe9e6dee22017-04-11 13:50:23 -07001081 VLOG(dex) << "Accessed dex file for invoke " << invoke_type << " "
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001082 << called_method.dex_method_index;
1083 }
1084 } else {
1085 invoke_type = stack_map_invoke_type;
1086 called_method.dex_method_index = stack_map_dex_method_idx;
Ian Rogers848871b2013-08-05 10:56:33 -07001087 }
Ian Rogers848871b2013-08-05 10:56:33 -07001088 } else {
1089 invoke_type = kStatic;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001090 called_method.dex_file = called->GetDexFile();
1091 called_method.dex_method_index = called->GetDexMethodIndex();
Ian Rogers848871b2013-08-05 10:56:33 -07001092 }
1093 uint32_t shorty_len;
1094 const char* shorty =
Ian Rogerse0a02da2014-12-02 14:10:53 -08001095 called_method.dex_file->GetMethodShorty(
1096 called_method.dex_file->GetMethodId(called_method.dex_method_index), &shorty_len);
Mathieu Chartier590fee92013-09-13 13:46:47 -07001097 RememberForGcArgumentVisitor visitor(sp, invoke_type == kStatic, shorty, shorty_len, &soa);
Ian Rogers848871b2013-08-05 10:56:33 -07001098 visitor.VisitArguments();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001099 self->EndAssertNoThreadSuspension(old_cause);
Ian Rogerse0a02da2014-12-02 14:10:53 -08001100 const bool virtual_or_interface = invoke_type == kVirtual || invoke_type == kInterface;
Ian Rogers848871b2013-08-05 10:56:33 -07001101 // Resolve method filling in dex cache.
Ian Rogerse0a02da2014-12-02 14:10:53 -08001102 if (!called_method_known_on_entry) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001103 StackHandleScope<1> hs(self);
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001104 mirror::Object* dummy = nullptr;
1105 HandleWrapper<mirror::Object> h_receiver(
1106 hs.NewHandleWrapper(virtual_or_interface ? &receiver : &dummy));
Ian Rogerse0a02da2014-12-02 14:10:53 -08001107 DCHECK_EQ(caller->GetDexFile(), called_method.dex_file);
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08001108 called = linker->ResolveMethod<ClassLinker::kForceICCECheck>(
1109 self, called_method.dex_method_index, caller, invoke_type);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001110
1111 // Update .bss entry in oat file if any.
1112 if (called != nullptr && called_method.dex_file->GetOatDexFile() != nullptr) {
1113 const MethodBssMapping* mapping =
1114 called_method.dex_file->GetOatDexFile()->GetMethodBssMapping();
1115 if (mapping != nullptr) {
1116 auto pp = std::partition_point(
1117 mapping->begin(),
1118 mapping->end(),
1119 [called_method](const MethodBssMappingEntry& entry) {
1120 return entry.method_index < called_method.dex_method_index;
1121 });
1122 if (pp != mapping->end() && pp->CoversIndex(called_method.dex_method_index)) {
1123 size_t bss_offset = pp->GetBssOffset(called_method.dex_method_index,
1124 static_cast<size_t>(kRuntimePointerSize));
1125 DCHECK_ALIGNED(bss_offset, static_cast<size_t>(kRuntimePointerSize));
1126 const OatFile* oat_file = called_method.dex_file->GetOatDexFile()->GetOatFile();
1127 ArtMethod** method_entry = reinterpret_cast<ArtMethod**>(const_cast<uint8_t*>(
1128 oat_file->BssBegin() + bss_offset));
1129 DCHECK_GE(method_entry, oat_file->GetBssMethods().data());
1130 DCHECK_LT(method_entry,
1131 oat_file->GetBssMethods().data() + oat_file->GetBssMethods().size());
1132 *method_entry = called;
1133 }
1134 }
1135 }
Ian Rogers848871b2013-08-05 10:56:33 -07001136 }
Ian Rogerse0a02da2014-12-02 14:10:53 -08001137 const void* code = nullptr;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001138 if (LIKELY(!self->IsExceptionPending())) {
Ian Rogers848871b2013-08-05 10:56:33 -07001139 // Incompatible class change should have been handled in resolve method.
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001140 CHECK(!called->CheckIncompatibleClassChange(invoke_type))
David Sehr709b0702016-10-13 09:12:37 -07001141 << called->PrettyMethod() << " " << invoke_type;
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001142 if (virtual_or_interface || invoke_type == kSuper) {
1143 // Refine called method based on receiver for kVirtual/kInterface, and
1144 // caller for kSuper.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001145 ArtMethod* orig_called = called;
Mathieu Chartier55871bf2014-02-27 10:24:50 -08001146 if (invoke_type == kVirtual) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001147 CHECK(receiver != nullptr) << invoke_type;
Andreas Gampe542451c2016-07-26 09:02:02 -07001148 called = receiver->GetClass()->FindVirtualMethodForVirtual(called, kRuntimePointerSize);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001149 } else if (invoke_type == kInterface) {
1150 CHECK(receiver != nullptr) << invoke_type;
Andreas Gampe542451c2016-07-26 09:02:02 -07001151 called = receiver->GetClass()->FindVirtualMethodForInterface(called, kRuntimePointerSize);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001152 } else {
1153 DCHECK_EQ(invoke_type, kSuper);
1154 CHECK(caller != nullptr) << invoke_type;
Nicolas Geoffray393fdb82016-04-25 14:58:06 +01001155 StackHandleScope<2> hs(self);
1156 Handle<mirror::DexCache> dex_cache(
1157 hs.NewHandle(caller->GetDeclaringClass()->GetDexCache()));
1158 Handle<mirror::ClassLoader> class_loader(
1159 hs.NewHandle(caller->GetDeclaringClass()->GetClassLoader()));
Alex Lightfedd91d2016-01-07 14:49:16 -08001160 // TODO Maybe put this into a mirror::Class function.
1161 mirror::Class* ref_class = linker->ResolveReferencedClassOfMethod(
Nicolas Geoffray393fdb82016-04-25 14:58:06 +01001162 called_method.dex_method_index, dex_cache, class_loader);
Alex Lightfedd91d2016-01-07 14:49:16 -08001163 if (ref_class->IsInterface()) {
Andreas Gampe542451c2016-07-26 09:02:02 -07001164 called = ref_class->FindVirtualMethodForInterfaceSuper(called, kRuntimePointerSize);
Alex Lightfedd91d2016-01-07 14:49:16 -08001165 } else {
1166 called = caller->GetDeclaringClass()->GetSuperClass()->GetVTableEntry(
Andreas Gampe542451c2016-07-26 09:02:02 -07001167 called->GetMethodIndex(), kRuntimePointerSize);
Alex Lightfedd91d2016-01-07 14:49:16 -08001168 }
Mathieu Chartier55871bf2014-02-27 10:24:50 -08001169 }
Mingyao Yangf4867782014-05-05 11:55:02 -07001170
David Sehr709b0702016-10-13 09:12:37 -07001171 CHECK(called != nullptr) << orig_called->PrettyMethod() << " "
1172 << mirror::Object::PrettyTypeOf(receiver) << " "
Mingyao Yangf4867782014-05-05 11:55:02 -07001173 << invoke_type << " " << orig_called->GetVtableIndex();
1174
Ian Rogers83883d72013-10-21 21:07:24 -07001175 // 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 -08001176 // of the sharpened method avoiding dirtying the dex cache if possible.
Ian Rogers00f15272014-12-02 16:55:46 -08001177 // Note, called_method.dex_method_index references the dex method before the
1178 // FindVirtualMethodFor... This is ok for FindDexMethodIndexInOtherDexFile that only cares
1179 // about the name and signature.
1180 uint32_t update_dex_cache_method_index = called->GetDexMethodIndex();
Andreas Gampe542451c2016-07-26 09:02:02 -07001181 if (!called->HasSameDexCacheResolvedMethods(caller, kRuntimePointerSize)) {
Ian Rogers83883d72013-10-21 21:07:24 -07001182 // Calling from one dex file to another, need to compute the method index appropriate to
Vladimir Markobbcc0c02014-02-03 14:08:42 +00001183 // the caller's dex file. Since we get here only if the original called was a runtime
1184 // method, we've got the correct dex_file and a dex_method_idx from above.
Ian Rogerse0a02da2014-12-02 14:10:53 -08001185 DCHECK(!called_method_known_on_entry);
1186 DCHECK_EQ(caller->GetDexFile(), called_method.dex_file);
1187 const DexFile* caller_dex_file = called_method.dex_file;
1188 uint32_t caller_method_name_and_sig_index = called_method.dex_method_index;
1189 update_dex_cache_method_index =
1190 called->FindDexMethodIndexInOtherDexFile(*caller_dex_file,
1191 caller_method_name_and_sig_index);
1192 }
1193 if ((update_dex_cache_method_index != DexFile::kDexNoIndex) &&
Mathieu Chartiere401d142015-04-22 13:56:20 -07001194 (caller->GetDexCacheResolvedMethod(
Andreas Gampe542451c2016-07-26 09:02:02 -07001195 update_dex_cache_method_index, kRuntimePointerSize) != called)) {
1196 caller->SetDexCacheResolvedMethod(update_dex_cache_method_index,
1197 called,
1198 kRuntimePointerSize);
Ian Rogers83883d72013-10-21 21:07:24 -07001199 }
Mathieu Chartiere4a91bb2015-01-28 13:11:44 -08001200 } else if (invoke_type == kStatic) {
1201 const auto called_dex_method_idx = called->GetDexMethodIndex();
1202 // For static invokes, we may dispatch to the static method in the superclass but resolve
1203 // using the subclass. To prevent getting slow paths on each invoke, we force set the
1204 // resolved method for the super class dex method index if we are in the same dex file.
1205 // b/19175856
1206 if (called->GetDexFile() == called_method.dex_file &&
1207 called_method.dex_method_index != called_dex_method_idx) {
Andreas Gampe542451c2016-07-26 09:02:02 -07001208 called->GetDexCache()->SetResolvedMethod(called_dex_method_idx,
1209 called,
1210 kRuntimePointerSize);
Mathieu Chartiere4a91bb2015-01-28 13:11:44 -08001211 }
Ian Rogers83883d72013-10-21 21:07:24 -07001212 }
Daniel Mihalyieb076692014-08-22 17:33:31 +02001213
Ian Rogers848871b2013-08-05 10:56:33 -07001214 // Ensure that the called method's class is initialized.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001215 StackHandleScope<1> hs(soa.Self());
1216 Handle<mirror::Class> called_class(hs.NewHandle(called->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -07001217 linker->EnsureInitialized(soa.Self(), called_class, true, true);
Ian Rogers848871b2013-08-05 10:56:33 -07001218 if (LIKELY(called_class->IsInitialized())) {
Daniel Mihalyieb076692014-08-22 17:33:31 +02001219 if (UNLIKELY(Dbg::IsForcedInterpreterNeededForResolution(self, called))) {
1220 // If we are single-stepping or the called method is deoptimized (by a
1221 // breakpoint, for example), then we have to execute the called method
1222 // with the interpreter.
1223 code = GetQuickToInterpreterBridge();
1224 } else if (UNLIKELY(Dbg::IsForcedInstrumentationNeededForResolution(self, caller))) {
1225 // If the caller is deoptimized (by a breakpoint, for example), we have to
1226 // continue its execution with interpreter when returning from the called
1227 // method. Because we do not want to execute the called method with the
1228 // interpreter, we wrap its execution into the instrumentation stubs.
1229 // When the called method returns, it will execute the instrumentation
1230 // exit hook that will determine the need of the interpreter with a call
1231 // to Dbg::IsForcedInterpreterNeededForUpcall and deoptimize the stack if
1232 // it is needed.
1233 code = GetQuickInstrumentationEntryPoint();
1234 } else {
1235 code = called->GetEntryPointFromQuickCompiledCode();
1236 }
Ian Rogers848871b2013-08-05 10:56:33 -07001237 } else if (called_class->IsInitializing()) {
Daniel Mihalyieb076692014-08-22 17:33:31 +02001238 if (UNLIKELY(Dbg::IsForcedInterpreterNeededForResolution(self, called))) {
1239 // If we are single-stepping or the called method is deoptimized (by a
1240 // breakpoint, for example), then we have to execute the called method
1241 // with the interpreter.
1242 code = GetQuickToInterpreterBridge();
1243 } else if (invoke_type == kStatic) {
Ian Rogers848871b2013-08-05 10:56:33 -07001244 // Class is still initializing, go to oat and grab code (trampoline must be left in place
1245 // until class is initialized to stop races between threads).
Ian Rogersef7d42f2014-01-06 12:55:46 -08001246 code = linker->GetQuickOatCodeFor(called);
Ian Rogers848871b2013-08-05 10:56:33 -07001247 } else {
1248 // No trampoline for non-static methods.
Ian Rogersef7d42f2014-01-06 12:55:46 -08001249 code = called->GetEntryPointFromQuickCompiledCode();
Ian Rogers848871b2013-08-05 10:56:33 -07001250 }
1251 } else {
1252 DCHECK(called_class->IsErroneous());
1253 }
1254 }
Ian Rogerse0a02da2014-12-02 14:10:53 -08001255 CHECK_EQ(code == nullptr, self->IsExceptionPending());
Mathieu Chartier07d447b2013-09-26 11:57:43 -07001256 // Fixup any locally saved objects may have moved during a GC.
1257 visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -07001258 // Place called method in callee-save frame to be placed as first argument to quick method.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001259 *sp = called;
1260
Ian Rogers848871b2013-08-05 10:56:33 -07001261 return code;
1262}
1263
Andreas Gampec147b002014-03-06 18:11:06 -08001264/*
1265 * This class uses a couple of observations to unite the different calling conventions through
1266 * a few constants.
1267 *
1268 * 1) Number of registers used for passing is normally even, so counting down has no penalty for
1269 * possible alignment.
1270 * 2) Known 64b architectures store 8B units on the stack, both for integral and floating point
1271 * types, so using uintptr_t is OK. Also means that we can use kRegistersNeededX to denote
1272 * when we have to split things
1273 * 3) The only soft-float, Arm, is 32b, so no widening needs to be taken into account for floats
1274 * and we can use Int handling directly.
1275 * 4) Only 64b architectures widen, and their stack is aligned 8B anyways, so no padding code
1276 * necessary when widening. Also, widening of Ints will take place implicitly, and the
1277 * extension should be compatible with Aarch64, which mandates copying the available bits
1278 * into LSB and leaving the rest unspecified.
1279 * 5) Aligning longs and doubles is necessary on arm only, and it's the same in registers and on
1280 * the stack.
1281 * 6) There is only little endian.
1282 *
1283 *
1284 * Actual work is supposed to be done in a delegate of the template type. The interface is as
1285 * follows:
1286 *
1287 * void PushGpr(uintptr_t): Add a value for the next GPR
1288 *
1289 * void PushFpr4(float): Add a value for the next FPR of size 32b. Is only called if we need
1290 * padding, that is, think the architecture is 32b and aligns 64b.
1291 *
1292 * void PushFpr8(uint64_t): Push a double. We _will_ call this on 32b, it's the callee's job to
1293 * split this if necessary. The current state will have aligned, if
1294 * necessary.
1295 *
1296 * void PushStack(uintptr_t): Push a value to the stack.
1297 *
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001298 * uintptr_t PushHandleScope(mirror::Object* ref): Add a reference to the HandleScope. This _will_ have nullptr,
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001299 * as this might be important for null initialization.
Andreas Gampec147b002014-03-06 18:11:06 -08001300 * Must return the jobject, that is, the reference to the
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001301 * entry in the HandleScope (nullptr if necessary).
Andreas Gampec147b002014-03-06 18:11:06 -08001302 *
1303 */
Andreas Gampec200a4a2014-06-16 18:39:09 -07001304template<class T> class BuildNativeCallFrameStateMachine {
Andreas Gampec147b002014-03-06 18:11:06 -08001305 public:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001306#if defined(__arm__)
1307 // TODO: These are all dummy values!
Andreas Gampec147b002014-03-06 18:11:06 -08001308 static constexpr bool kNativeSoftFloatAbi = true;
1309 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs, r0-r3
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001310 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
1311
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001312 static constexpr size_t kRegistersNeededForLong = 2;
1313 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -08001314 static constexpr bool kMultiRegistersAligned = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001315 static constexpr bool kMultiFPRegistersWidened = false;
1316 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001317 static constexpr bool kAlignLongOnStack = true;
1318 static constexpr bool kAlignDoubleOnStack = true;
Stuart Monteithb95a5342014-03-12 13:32:32 +00001319#elif defined(__aarch64__)
1320 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
1321 static constexpr size_t kNumNativeGprArgs = 8; // 6 arguments passed in GPRs.
1322 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
1323
1324 static constexpr size_t kRegistersNeededForLong = 1;
1325 static constexpr size_t kRegistersNeededForDouble = 1;
1326 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001327 static constexpr bool kMultiFPRegistersWidened = false;
1328 static constexpr bool kMultiGPRegistersWidened = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +00001329 static constexpr bool kAlignLongOnStack = false;
1330 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001331#elif defined(__mips__) && !defined(__LP64__)
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001332 static constexpr bool kNativeSoftFloatAbi = true; // This is a hard float ABI.
Douglas Leung735b8552014-10-31 12:21:40 -07001333 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs.
1334 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001335
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001336 static constexpr size_t kRegistersNeededForLong = 2;
1337 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -08001338 static constexpr bool kMultiRegistersAligned = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001339 static constexpr bool kMultiFPRegistersWidened = true;
1340 static constexpr bool kMultiGPRegistersWidened = false;
Douglas Leung735b8552014-10-31 12:21:40 -07001341 static constexpr bool kAlignLongOnStack = true;
1342 static constexpr bool kAlignDoubleOnStack = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001343#elif defined(__mips__) && defined(__LP64__)
1344 // Let the code prepare GPRs only and we will load the FPRs with same data.
1345 static constexpr bool kNativeSoftFloatAbi = true;
1346 static constexpr size_t kNumNativeGprArgs = 8;
1347 static constexpr size_t kNumNativeFprArgs = 0;
1348
1349 static constexpr size_t kRegistersNeededForLong = 1;
1350 static constexpr size_t kRegistersNeededForDouble = 1;
1351 static constexpr bool kMultiRegistersAligned = false;
1352 static constexpr bool kMultiFPRegistersWidened = false;
1353 static constexpr bool kMultiGPRegistersWidened = true;
1354 static constexpr bool kAlignLongOnStack = false;
1355 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001356#elif defined(__i386__)
1357 // TODO: Check these!
Andreas Gampec147b002014-03-06 18:11:06 -08001358 static constexpr bool kNativeSoftFloatAbi = false; // Not using int registers for fp
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001359 static constexpr size_t kNumNativeGprArgs = 0; // 6 arguments passed in GPRs.
1360 static constexpr size_t kNumNativeFprArgs = 0; // 8 arguments passed in FPRs.
1361
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001362 static constexpr size_t kRegistersNeededForLong = 2;
1363 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001364 static constexpr bool kMultiRegistersAligned = false; // x86 not using regs, anyways
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001365 static constexpr bool kMultiFPRegistersWidened = false;
1366 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001367 static constexpr bool kAlignLongOnStack = false;
1368 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001369#elif defined(__x86_64__)
1370 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
1371 static constexpr size_t kNumNativeGprArgs = 6; // 6 arguments passed in GPRs.
1372 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
1373
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001374 static constexpr size_t kRegistersNeededForLong = 1;
1375 static constexpr size_t kRegistersNeededForDouble = 1;
Andreas Gampec147b002014-03-06 18:11:06 -08001376 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001377 static constexpr bool kMultiFPRegistersWidened = false;
1378 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001379 static constexpr bool kAlignLongOnStack = false;
1380 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001381#else
1382#error "Unsupported architecture"
1383#endif
1384
Andreas Gampec147b002014-03-06 18:11:06 -08001385 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001386 explicit BuildNativeCallFrameStateMachine(T* delegate)
1387 : gpr_index_(kNumNativeGprArgs),
1388 fpr_index_(kNumNativeFprArgs),
1389 stack_entries_(0),
1390 delegate_(delegate) {
Andreas Gampec147b002014-03-06 18:11:06 -08001391 // For register alignment, we want to assume that counters (gpr_index_, fpr_index_) are even iff
1392 // the next register is even; counting down is just to make the compiler happy...
Andreas Gampe575e78c2014-11-03 23:41:03 -08001393 static_assert(kNumNativeGprArgs % 2 == 0U, "Number of native GPR arguments not even");
1394 static_assert(kNumNativeFprArgs % 2 == 0U, "Number of native FPR arguments not even");
Andreas Gampec147b002014-03-06 18:11:06 -08001395 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001396
Andreas Gampec200a4a2014-06-16 18:39:09 -07001397 virtual ~BuildNativeCallFrameStateMachine() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001398
Ian Rogers1428dce2014-10-21 15:02:15 -07001399 bool HavePointerGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001400 return gpr_index_ > 0;
1401 }
1402
Andreas Gampec200a4a2014-06-16 18:39:09 -07001403 void AdvancePointer(const void* val) {
Andreas Gampec147b002014-03-06 18:11:06 -08001404 if (HavePointerGpr()) {
1405 gpr_index_--;
1406 PushGpr(reinterpret_cast<uintptr_t>(val));
1407 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001408 stack_entries_++; // TODO: have a field for pointer length as multiple of 32b
Andreas Gampec147b002014-03-06 18:11:06 -08001409 PushStack(reinterpret_cast<uintptr_t>(val));
1410 gpr_index_ = 0;
1411 }
1412 }
1413
Ian Rogers1428dce2014-10-21 15:02:15 -07001414 bool HaveHandleScopeGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001415 return gpr_index_ > 0;
1416 }
1417
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001418 void AdvanceHandleScope(mirror::Object* ptr) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001419 uintptr_t handle = PushHandle(ptr);
1420 if (HaveHandleScopeGpr()) {
Andreas Gampec147b002014-03-06 18:11:06 -08001421 gpr_index_--;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001422 PushGpr(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001423 } else {
1424 stack_entries_++;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001425 PushStack(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001426 gpr_index_ = 0;
1427 }
1428 }
1429
Ian Rogers1428dce2014-10-21 15:02:15 -07001430 bool HaveIntGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001431 return gpr_index_ > 0;
1432 }
1433
1434 void AdvanceInt(uint32_t val) {
1435 if (HaveIntGpr()) {
1436 gpr_index_--;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001437 if (kMultiGPRegistersWidened) {
1438 DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001439 PushGpr(static_cast<int64_t>(bit_cast<int32_t, uint32_t>(val)));
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001440 } else {
1441 PushGpr(val);
1442 }
Andreas Gampec147b002014-03-06 18:11:06 -08001443 } else {
1444 stack_entries_++;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001445 if (kMultiGPRegistersWidened) {
1446 DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001447 PushStack(static_cast<int64_t>(bit_cast<int32_t, uint32_t>(val)));
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001448 } else {
1449 PushStack(val);
1450 }
Andreas Gampec147b002014-03-06 18:11:06 -08001451 gpr_index_ = 0;
1452 }
1453 }
1454
Ian Rogers1428dce2014-10-21 15:02:15 -07001455 bool HaveLongGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001456 return gpr_index_ >= kRegistersNeededForLong + (LongGprNeedsPadding() ? 1 : 0);
1457 }
1458
Ian Rogers1428dce2014-10-21 15:02:15 -07001459 bool LongGprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001460 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1461 kAlignLongOnStack && // and when it needs alignment
1462 (gpr_index_ & 1) == 1; // counter is odd, see constructor
1463 }
1464
Ian Rogers1428dce2014-10-21 15:02:15 -07001465 bool LongStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001466 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1467 kAlignLongOnStack && // and when it needs 8B alignment
1468 (stack_entries_ & 1) == 1; // counter is odd
1469 }
1470
1471 void AdvanceLong(uint64_t val) {
1472 if (HaveLongGpr()) {
1473 if (LongGprNeedsPadding()) {
1474 PushGpr(0);
1475 gpr_index_--;
1476 }
1477 if (kRegistersNeededForLong == 1) {
1478 PushGpr(static_cast<uintptr_t>(val));
1479 } else {
1480 PushGpr(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1481 PushGpr(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1482 }
1483 gpr_index_ -= kRegistersNeededForLong;
1484 } else {
1485 if (LongStackNeedsPadding()) {
1486 PushStack(0);
1487 stack_entries_++;
1488 }
1489 if (kRegistersNeededForLong == 1) {
1490 PushStack(static_cast<uintptr_t>(val));
1491 stack_entries_++;
1492 } else {
1493 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1494 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1495 stack_entries_ += 2;
1496 }
1497 gpr_index_ = 0;
1498 }
1499 }
1500
Ian Rogers1428dce2014-10-21 15:02:15 -07001501 bool HaveFloatFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001502 return fpr_index_ > 0;
1503 }
1504
Andreas Gampec147b002014-03-06 18:11:06 -08001505 void AdvanceFloat(float val) {
1506 if (kNativeSoftFloatAbi) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001507 AdvanceInt(bit_cast<uint32_t, float>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001508 } else {
1509 if (HaveFloatFpr()) {
1510 fpr_index_--;
1511 if (kRegistersNeededForDouble == 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001512 if (kMultiFPRegistersWidened) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001513 PushFpr8(bit_cast<uint64_t, double>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001514 } else {
1515 // No widening, just use the bits.
Roland Levillainda4d79b2015-03-24 14:36:11 +00001516 PushFpr8(static_cast<uint64_t>(bit_cast<uint32_t, float>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001517 }
1518 } else {
1519 PushFpr4(val);
1520 }
1521 } else {
1522 stack_entries_++;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001523 if (kRegistersNeededForDouble == 1 && kMultiFPRegistersWidened) {
Andreas Gampec147b002014-03-06 18:11:06 -08001524 // Need to widen before storing: Note the "double" in the template instantiation.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001525 // Note: We need to jump through those hoops to make the compiler happy.
1526 DCHECK_EQ(sizeof(uintptr_t), sizeof(uint64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001527 PushStack(static_cast<uintptr_t>(bit_cast<uint64_t, double>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001528 } else {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001529 PushStack(static_cast<uintptr_t>(bit_cast<uint32_t, float>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001530 }
1531 fpr_index_ = 0;
1532 }
1533 }
1534 }
1535
Ian Rogers1428dce2014-10-21 15:02:15 -07001536 bool HaveDoubleFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001537 return fpr_index_ >= kRegistersNeededForDouble + (DoubleFprNeedsPadding() ? 1 : 0);
1538 }
1539
Ian Rogers1428dce2014-10-21 15:02:15 -07001540 bool DoubleFprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001541 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1542 kAlignDoubleOnStack && // and when it needs alignment
1543 (fpr_index_ & 1) == 1; // counter is odd, see constructor
1544 }
1545
Ian Rogers1428dce2014-10-21 15:02:15 -07001546 bool DoubleStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001547 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1548 kAlignDoubleOnStack && // and when it needs 8B alignment
1549 (stack_entries_ & 1) == 1; // counter is odd
1550 }
1551
1552 void AdvanceDouble(uint64_t val) {
1553 if (kNativeSoftFloatAbi) {
1554 AdvanceLong(val);
1555 } else {
1556 if (HaveDoubleFpr()) {
1557 if (DoubleFprNeedsPadding()) {
1558 PushFpr4(0);
1559 fpr_index_--;
1560 }
1561 PushFpr8(val);
1562 fpr_index_ -= kRegistersNeededForDouble;
1563 } else {
1564 if (DoubleStackNeedsPadding()) {
1565 PushStack(0);
1566 stack_entries_++;
1567 }
1568 if (kRegistersNeededForDouble == 1) {
1569 PushStack(static_cast<uintptr_t>(val));
1570 stack_entries_++;
1571 } else {
1572 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1573 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1574 stack_entries_ += 2;
1575 }
1576 fpr_index_ = 0;
1577 }
1578 }
1579 }
1580
Ian Rogers1428dce2014-10-21 15:02:15 -07001581 uint32_t GetStackEntries() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001582 return stack_entries_;
1583 }
1584
Ian Rogers1428dce2014-10-21 15:02:15 -07001585 uint32_t GetNumberOfUsedGprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001586 return kNumNativeGprArgs - gpr_index_;
1587 }
1588
Ian Rogers1428dce2014-10-21 15:02:15 -07001589 uint32_t GetNumberOfUsedFprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001590 return kNumNativeFprArgs - fpr_index_;
1591 }
1592
1593 private:
1594 void PushGpr(uintptr_t val) {
1595 delegate_->PushGpr(val);
1596 }
1597 void PushFpr4(float val) {
1598 delegate_->PushFpr4(val);
1599 }
1600 void PushFpr8(uint64_t val) {
1601 delegate_->PushFpr8(val);
1602 }
1603 void PushStack(uintptr_t val) {
1604 delegate_->PushStack(val);
1605 }
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001606 uintptr_t PushHandle(mirror::Object* ref) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001607 return delegate_->PushHandle(ref);
Andreas Gampec147b002014-03-06 18:11:06 -08001608 }
1609
1610 uint32_t gpr_index_; // Number of free GPRs
1611 uint32_t fpr_index_; // Number of free FPRs
1612 uint32_t stack_entries_; // Stack entries are in multiples of 32b, as floats are usually not
1613 // extended
Ian Rogers1428dce2014-10-21 15:02:15 -07001614 T* const delegate_; // What Push implementation gets called
Andreas Gampec147b002014-03-06 18:11:06 -08001615};
1616
Andreas Gampec200a4a2014-06-16 18:39:09 -07001617// Computes the sizes of register stacks and call stack area. Handling of references can be extended
1618// in subclasses.
1619//
1620// To handle native pointers, use "L" in the shorty for an object reference, which simulates
1621// them with handles.
1622class ComputeNativeCallFrameSize {
Andreas Gampec147b002014-03-06 18:11:06 -08001623 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001624 ComputeNativeCallFrameSize() : num_stack_entries_(0) {}
1625
1626 virtual ~ComputeNativeCallFrameSize() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001627
Ian Rogers1428dce2014-10-21 15:02:15 -07001628 uint32_t GetStackSize() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001629 return num_stack_entries_ * sizeof(uintptr_t);
1630 }
1631
Ian Rogers1428dce2014-10-21 15:02:15 -07001632 uint8_t* LayoutCallStack(uint8_t* sp8) const {
Andreas Gampec147b002014-03-06 18:11:06 -08001633 sp8 -= GetStackSize();
Andreas Gampe779f8c92014-06-09 18:29:38 -07001634 // Align by kStackAlignment.
1635 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
Andreas Gampec200a4a2014-06-16 18:39:09 -07001636 return sp8;
Andreas Gampec147b002014-03-06 18:11:06 -08001637 }
1638
Ian Rogers1428dce2014-10-21 15:02:15 -07001639 uint8_t* LayoutCallRegisterStacks(uint8_t* sp8, uintptr_t** start_gpr, uint32_t** start_fpr)
1640 const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001641 // Assumption is OK right now, as we have soft-float arm
1642 size_t fregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeFprArgs;
1643 sp8 -= fregs * sizeof(uintptr_t);
1644 *start_fpr = reinterpret_cast<uint32_t*>(sp8);
1645 size_t iregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeGprArgs;
1646 sp8 -= iregs * sizeof(uintptr_t);
1647 *start_gpr = reinterpret_cast<uintptr_t*>(sp8);
1648 return sp8;
1649 }
Andreas Gampec147b002014-03-06 18:11:06 -08001650
Andreas Gampec200a4a2014-06-16 18:39:09 -07001651 uint8_t* LayoutNativeCall(uint8_t* sp8, uintptr_t** start_stack, uintptr_t** start_gpr,
Ian Rogers1428dce2014-10-21 15:02:15 -07001652 uint32_t** start_fpr) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001653 // Native call stack.
1654 sp8 = LayoutCallStack(sp8);
1655 *start_stack = reinterpret_cast<uintptr_t*>(sp8);
Andreas Gampec147b002014-03-06 18:11:06 -08001656
Andreas Gampec200a4a2014-06-16 18:39:09 -07001657 // Put fprs and gprs below.
1658 sp8 = LayoutCallRegisterStacks(sp8, start_gpr, start_fpr);
Andreas Gampec147b002014-03-06 18:11:06 -08001659
Andreas Gampec200a4a2014-06-16 18:39:09 -07001660 // Return the new bottom.
1661 return sp8;
1662 }
1663
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001664 virtual void WalkHeader(
1665 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm ATTRIBUTE_UNUSED)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001666 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001667 }
Andreas Gampec200a4a2014-06-16 18:39:09 -07001668
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001669 void Walk(const char* shorty, uint32_t shorty_len) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001670 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize> sm(this);
1671
1672 WalkHeader(&sm);
Andreas Gampec147b002014-03-06 18:11:06 -08001673
1674 for (uint32_t i = 1; i < shorty_len; ++i) {
1675 Primitive::Type cur_type_ = Primitive::GetType(shorty[i]);
1676 switch (cur_type_) {
1677 case Primitive::kPrimNot:
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001678 // TODO: fix abuse of mirror types.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001679 sm.AdvanceHandleScope(
1680 reinterpret_cast<mirror::Object*>(0x12345678));
Andreas Gampec147b002014-03-06 18:11:06 -08001681 break;
1682
1683 case Primitive::kPrimBoolean:
1684 case Primitive::kPrimByte:
1685 case Primitive::kPrimChar:
1686 case Primitive::kPrimShort:
1687 case Primitive::kPrimInt:
1688 sm.AdvanceInt(0);
1689 break;
1690 case Primitive::kPrimFloat:
1691 sm.AdvanceFloat(0);
1692 break;
1693 case Primitive::kPrimDouble:
1694 sm.AdvanceDouble(0);
1695 break;
1696 case Primitive::kPrimLong:
1697 sm.AdvanceLong(0);
1698 break;
1699 default:
1700 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001701 UNREACHABLE();
Andreas Gampec147b002014-03-06 18:11:06 -08001702 }
1703 }
1704
Ian Rogers1428dce2014-10-21 15:02:15 -07001705 num_stack_entries_ = sm.GetStackEntries();
Andreas Gampec147b002014-03-06 18:11:06 -08001706 }
1707
1708 void PushGpr(uintptr_t /* val */) {
1709 // not optimizing registers, yet
1710 }
1711
1712 void PushFpr4(float /* val */) {
1713 // not optimizing registers, yet
1714 }
1715
1716 void PushFpr8(uint64_t /* val */) {
1717 // not optimizing registers, yet
1718 }
1719
1720 void PushStack(uintptr_t /* val */) {
1721 // counting is already done in the superclass
1722 }
1723
Andreas Gampec200a4a2014-06-16 18:39:09 -07001724 virtual uintptr_t PushHandle(mirror::Object* /* ptr */) {
Andreas Gampec147b002014-03-06 18:11:06 -08001725 return reinterpret_cast<uintptr_t>(nullptr);
1726 }
1727
Andreas Gampec200a4a2014-06-16 18:39:09 -07001728 protected:
Andreas Gampec147b002014-03-06 18:11:06 -08001729 uint32_t num_stack_entries_;
1730};
1731
Andreas Gampec200a4a2014-06-16 18:39:09 -07001732class ComputeGenericJniFrameSize FINAL : public ComputeNativeCallFrameSize {
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001733 public:
Igor Murashkin06a04e02016-09-13 15:57:37 -07001734 explicit ComputeGenericJniFrameSize(bool critical_native)
1735 : num_handle_scope_references_(0), critical_native_(critical_native) {}
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001736
Andreas Gampec200a4a2014-06-16 18:39:09 -07001737 // Lays out the callee-save frame. Assumes that the incorrect frame corresponding to RefsAndArgs
1738 // is at *m = sp. Will update to point to the bottom of the save frame.
1739 //
1740 // Note: assumes ComputeAll() has been run before.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001741 void LayoutCalleeSaveFrame(Thread* self, ArtMethod*** m, void* sp, HandleScope** handle_scope)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001742 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001743 ArtMethod* method = **m;
1744
Andreas Gampe542451c2016-07-26 09:02:02 -07001745 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001746
Andreas Gampec200a4a2014-06-16 18:39:09 -07001747 uint8_t* sp8 = reinterpret_cast<uint8_t*>(sp);
1748
1749 // First, fix up the layout of the callee-save frame.
1750 // We have to squeeze in the HandleScope, and relocate the method pointer.
1751
1752 // "Free" the slot for the method.
Ian Rogers13735952014-10-08 12:43:28 -07001753 sp8 += sizeof(void*); // In the callee-save frame we use a full pointer.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001754
1755 // Under the callee saves put handle scope and new method stack reference.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001756 size_t handle_scope_size = HandleScope::SizeOf(num_handle_scope_references_);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001757 size_t scope_and_method = handle_scope_size + sizeof(ArtMethod*);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001758
1759 sp8 -= scope_and_method;
1760 // Align by kStackAlignment.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001761 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
Andreas Gampec200a4a2014-06-16 18:39:09 -07001762
Mathieu Chartiere401d142015-04-22 13:56:20 -07001763 uint8_t* sp8_table = sp8 + sizeof(ArtMethod*);
Ian Rogers59c07062014-10-10 13:03:39 -07001764 *handle_scope = HandleScope::Create(sp8_table, self->GetTopHandleScope(),
1765 num_handle_scope_references_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001766
1767 // Add a slot for the method pointer, and fill it. Fix the pointer-pointer given to us.
1768 uint8_t* method_pointer = sp8;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001769 auto** new_method_ref = reinterpret_cast<ArtMethod**>(method_pointer);
1770 *new_method_ref = method;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001771 *m = new_method_ref;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001772 }
1773
Andreas Gampec200a4a2014-06-16 18:39:09 -07001774 // Adds space for the cookie. Note: may leave stack unaligned.
Ian Rogers1428dce2014-10-21 15:02:15 -07001775 void LayoutCookie(uint8_t** sp) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001776 // Reference cookie and padding
1777 *sp -= 8;
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001778 }
1779
Andreas Gampec200a4a2014-06-16 18:39:09 -07001780 // Re-layout the callee-save frame (insert a handle-scope). Then add space for the cookie.
1781 // Returns the new bottom. Note: this may be unaligned.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001782 uint8_t* LayoutJNISaveFrame(Thread* self, ArtMethod*** m, void* sp, HandleScope** handle_scope)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001783 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001784 // First, fix up the layout of the callee-save frame.
1785 // We have to squeeze in the HandleScope, and relocate the method pointer.
Ian Rogers59c07062014-10-10 13:03:39 -07001786 LayoutCalleeSaveFrame(self, m, sp, handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001787
1788 // The bottom of the callee-save frame is now where the method is, *m.
1789 uint8_t* sp8 = reinterpret_cast<uint8_t*>(*m);
1790
1791 // Add space for cookie.
1792 LayoutCookie(&sp8);
1793
1794 return sp8;
1795 }
1796
1797 // WARNING: After this, *sp won't be pointing to the method anymore!
Mathieu Chartiere401d142015-04-22 13:56:20 -07001798 uint8_t* ComputeLayout(Thread* self, ArtMethod*** m, const char* shorty, uint32_t shorty_len,
1799 HandleScope** handle_scope, uintptr_t** start_stack, uintptr_t** start_gpr,
1800 uint32_t** start_fpr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001801 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001802 Walk(shorty, shorty_len);
1803
1804 // JNI part.
Ian Rogers59c07062014-10-10 13:03:39 -07001805 uint8_t* sp8 = LayoutJNISaveFrame(self, m, reinterpret_cast<void*>(*m), handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001806
1807 sp8 = LayoutNativeCall(sp8, start_stack, start_gpr, start_fpr);
1808
1809 // Return the new bottom.
1810 return sp8;
1811 }
1812
1813 uintptr_t PushHandle(mirror::Object* /* ptr */) OVERRIDE;
1814
1815 // Add JNIEnv* and jobj/jclass before the shorty-derived elements.
1816 void WalkHeader(BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) OVERRIDE
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001817 REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001818
1819 private:
1820 uint32_t num_handle_scope_references_;
Igor Murashkin06a04e02016-09-13 15:57:37 -07001821 const bool critical_native_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001822};
1823
1824uintptr_t ComputeGenericJniFrameSize::PushHandle(mirror::Object* /* ptr */) {
1825 num_handle_scope_references_++;
1826 return reinterpret_cast<uintptr_t>(nullptr);
1827}
1828
1829void ComputeGenericJniFrameSize::WalkHeader(
1830 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) {
Igor Murashkin06a04e02016-09-13 15:57:37 -07001831 // First 2 parameters are always excluded for @CriticalNative.
1832 if (UNLIKELY(critical_native_)) {
1833 return;
1834 }
1835
Andreas Gampec200a4a2014-06-16 18:39:09 -07001836 // JNIEnv
1837 sm->AdvancePointer(nullptr);
1838
1839 // Class object or this as first argument
1840 sm->AdvanceHandleScope(reinterpret_cast<mirror::Object*>(0x12345678));
1841}
1842
1843// Class to push values to three separate regions. Used to fill the native call part. Adheres to
1844// the template requirements of BuildGenericJniFrameStateMachine.
1845class FillNativeCall {
1846 public:
1847 FillNativeCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) :
1848 cur_gpr_reg_(gpr_regs), cur_fpr_reg_(fpr_regs), cur_stack_arg_(stack_args) {}
1849
1850 virtual ~FillNativeCall() {}
1851
1852 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) {
1853 cur_gpr_reg_ = gpr_regs;
1854 cur_fpr_reg_ = fpr_regs;
1855 cur_stack_arg_ = stack_args;
Andreas Gampec147b002014-03-06 18:11:06 -08001856 }
1857
1858 void PushGpr(uintptr_t val) {
1859 *cur_gpr_reg_ = val;
1860 cur_gpr_reg_++;
1861 }
1862
1863 void PushFpr4(float val) {
1864 *cur_fpr_reg_ = val;
1865 cur_fpr_reg_++;
1866 }
1867
1868 void PushFpr8(uint64_t val) {
1869 uint64_t* tmp = reinterpret_cast<uint64_t*>(cur_fpr_reg_);
1870 *tmp = val;
1871 cur_fpr_reg_ += 2;
1872 }
1873
1874 void PushStack(uintptr_t val) {
1875 *cur_stack_arg_ = val;
1876 cur_stack_arg_++;
1877 }
1878
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001879 virtual uintptr_t PushHandle(mirror::Object*) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001880 LOG(FATAL) << "(Non-JNI) Native call does not use handles.";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001881 UNREACHABLE();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001882 }
1883
1884 private:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001885 uintptr_t* cur_gpr_reg_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001886 uint32_t* cur_fpr_reg_;
1887 uintptr_t* cur_stack_arg_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001888};
Andreas Gampec147b002014-03-06 18:11:06 -08001889
Andreas Gampec200a4a2014-06-16 18:39:09 -07001890// Visits arguments on the stack placing them into a region lower down the stack for the benefit
1891// of transitioning into native code.
1892class BuildGenericJniFrameVisitor FINAL : public QuickArgumentVisitor {
1893 public:
Igor Murashkin06a04e02016-09-13 15:57:37 -07001894 BuildGenericJniFrameVisitor(Thread* self,
1895 bool is_static,
1896 bool critical_native,
1897 const char* shorty,
1898 uint32_t shorty_len,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001899 ArtMethod*** sp)
Andreas Gampec200a4a2014-06-16 18:39:09 -07001900 : QuickArgumentVisitor(*sp, is_static, shorty, shorty_len),
Igor Murashkin06a04e02016-09-13 15:57:37 -07001901 jni_call_(nullptr, nullptr, nullptr, nullptr, critical_native),
1902 sm_(&jni_call_) {
1903 ComputeGenericJniFrameSize fsc(critical_native);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001904 uintptr_t* start_gpr_reg;
1905 uint32_t* start_fpr_reg;
1906 uintptr_t* start_stack_arg;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001907 bottom_of_used_area_ = fsc.ComputeLayout(self, sp, shorty, shorty_len,
Ian Rogers59c07062014-10-10 13:03:39 -07001908 &handle_scope_,
1909 &start_stack_arg,
Andreas Gampec200a4a2014-06-16 18:39:09 -07001910 &start_gpr_reg, &start_fpr_reg);
1911
Andreas Gampec200a4a2014-06-16 18:39:09 -07001912 jni_call_.Reset(start_gpr_reg, start_fpr_reg, start_stack_arg, handle_scope_);
1913
Igor Murashkin06a04e02016-09-13 15:57:37 -07001914 // First 2 parameters are always excluded for CriticalNative methods.
1915 if (LIKELY(!critical_native)) {
1916 // jni environment is always first argument
1917 sm_.AdvancePointer(self->GetJniEnv());
Andreas Gampec200a4a2014-06-16 18:39:09 -07001918
Igor Murashkin06a04e02016-09-13 15:57:37 -07001919 if (is_static) {
1920 sm_.AdvanceHandleScope((**sp)->GetDeclaringClass());
1921 } // else "this" reference is already handled by QuickArgumentVisitor.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001922 }
1923 }
1924
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001925 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001926
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001927 void FinalizeHandleScope(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001928
Vladimir Markof39745e2016-01-26 12:16:55 +00001929 StackReference<mirror::Object>* GetFirstHandleScopeEntry() {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001930 return handle_scope_->GetHandle(0).GetReference();
1931 }
1932
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001933 jobject GetFirstHandleScopeJObject() const REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001934 return handle_scope_->GetHandle(0).ToJObject();
1935 }
1936
Ian Rogers1428dce2014-10-21 15:02:15 -07001937 void* GetBottomOfUsedArea() const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001938 return bottom_of_used_area_;
1939 }
1940
1941 private:
1942 // A class to fill a JNI call. Adds reference/handle-scope management to FillNativeCall.
1943 class FillJniCall FINAL : public FillNativeCall {
1944 public:
1945 FillJniCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args,
Igor Murashkin06a04e02016-09-13 15:57:37 -07001946 HandleScope* handle_scope, bool critical_native)
1947 : FillNativeCall(gpr_regs, fpr_regs, stack_args),
1948 handle_scope_(handle_scope),
1949 cur_entry_(0),
1950 critical_native_(critical_native) {}
Andreas Gampec200a4a2014-06-16 18:39:09 -07001951
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001952 uintptr_t PushHandle(mirror::Object* ref) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001953
1954 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args, HandleScope* scope) {
1955 FillNativeCall::Reset(gpr_regs, fpr_regs, stack_args);
1956 handle_scope_ = scope;
1957 cur_entry_ = 0U;
1958 }
1959
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001960 void ResetRemainingScopeSlots() REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001961 // Initialize padding entries.
1962 size_t expected_slots = handle_scope_->NumberOfReferences();
1963 while (cur_entry_ < expected_slots) {
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07001964 handle_scope_->GetMutableHandle(cur_entry_++).Assign(nullptr);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001965 }
Igor Murashkin06a04e02016-09-13 15:57:37 -07001966
1967 if (!critical_native_) {
1968 // Non-critical natives have at least the self class (jclass) or this (jobject).
1969 DCHECK_NE(cur_entry_, 0U);
1970 }
Andreas Gampec200a4a2014-06-16 18:39:09 -07001971 }
1972
Mathieu Chartier1432a5b2016-10-04 15:41:42 -07001973 bool CriticalNative() const {
1974 return critical_native_;
1975 }
1976
Andreas Gampec200a4a2014-06-16 18:39:09 -07001977 private:
1978 HandleScope* handle_scope_;
1979 size_t cur_entry_;
Igor Murashkin06a04e02016-09-13 15:57:37 -07001980 const bool critical_native_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001981 };
1982
1983 HandleScope* handle_scope_;
1984 FillJniCall jni_call_;
1985 void* bottom_of_used_area_;
1986
1987 BuildNativeCallFrameStateMachine<FillJniCall> sm_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001988
1989 DISALLOW_COPY_AND_ASSIGN(BuildGenericJniFrameVisitor);
1990};
1991
Andreas Gampec200a4a2014-06-16 18:39:09 -07001992uintptr_t BuildGenericJniFrameVisitor::FillJniCall::PushHandle(mirror::Object* ref) {
1993 uintptr_t tmp;
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07001994 MutableHandle<mirror::Object> h = handle_scope_->GetMutableHandle(cur_entry_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001995 h.Assign(ref);
1996 tmp = reinterpret_cast<uintptr_t>(h.ToJObject());
1997 cur_entry_++;
1998 return tmp;
1999}
2000
Ian Rogers9758f792014-03-13 09:02:55 -07002001void BuildGenericJniFrameVisitor::Visit() {
2002 Primitive::Type type = GetParamPrimitiveType();
2003 switch (type) {
2004 case Primitive::kPrimLong: {
2005 jlong long_arg;
2006 if (IsSplitLongOrDouble()) {
2007 long_arg = ReadSplitLongParam();
2008 } else {
2009 long_arg = *reinterpret_cast<jlong*>(GetParamAddress());
2010 }
2011 sm_.AdvanceLong(long_arg);
2012 break;
2013 }
2014 case Primitive::kPrimDouble: {
2015 uint64_t double_arg;
2016 if (IsSplitLongOrDouble()) {
2017 // Read into union so that we don't case to a double.
2018 double_arg = ReadSplitLongParam();
2019 } else {
2020 double_arg = *reinterpret_cast<uint64_t*>(GetParamAddress());
2021 }
2022 sm_.AdvanceDouble(double_arg);
2023 break;
2024 }
2025 case Primitive::kPrimNot: {
2026 StackReference<mirror::Object>* stack_ref =
2027 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002028 sm_.AdvanceHandleScope(stack_ref->AsMirrorPtr());
Ian Rogers9758f792014-03-13 09:02:55 -07002029 break;
2030 }
2031 case Primitive::kPrimFloat:
2032 sm_.AdvanceFloat(*reinterpret_cast<float*>(GetParamAddress()));
2033 break;
2034 case Primitive::kPrimBoolean: // Fall-through.
2035 case Primitive::kPrimByte: // Fall-through.
2036 case Primitive::kPrimChar: // Fall-through.
2037 case Primitive::kPrimShort: // Fall-through.
2038 case Primitive::kPrimInt: // Fall-through.
2039 sm_.AdvanceInt(*reinterpret_cast<jint*>(GetParamAddress()));
2040 break;
2041 case Primitive::kPrimVoid:
2042 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -07002043 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -07002044 }
2045}
2046
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002047void BuildGenericJniFrameVisitor::FinalizeHandleScope(Thread* self) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002048 // Clear out rest of the scope.
2049 jni_call_.ResetRemainingScopeSlots();
Mathieu Chartier1432a5b2016-10-04 15:41:42 -07002050 if (!jni_call_.CriticalNative()) {
2051 // Install HandleScope.
2052 self->PushHandleScope(handle_scope_);
2053 }
Ian Rogers9758f792014-03-13 09:02:55 -07002054}
2055
Ian Rogers04c31d22014-07-07 21:44:06 -07002056#if defined(__arm__) || defined(__aarch64__)
Alex Lightd78ddec2017-04-18 15:20:38 -07002057extern "C" const void* artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07002058#else
Alex Lightd78ddec2017-04-18 15:20:38 -07002059extern "C" const void* artFindNativeMethod(Thread* self);
Ian Rogers04c31d22014-07-07 21:44:06 -07002060#endif
Andreas Gampe90546832014-03-12 18:07:19 -07002061
Igor Murashkin06a04e02016-09-13 15:57:37 -07002062static uint64_t artQuickGenericJniEndJNIRef(Thread* self,
2063 uint32_t cookie,
2064 bool fast_native ATTRIBUTE_UNUSED,
2065 jobject l,
2066 jobject lock) {
2067 // TODO: add entrypoints for @FastNative returning objects.
Andreas Gampead615172014-04-04 16:20:13 -07002068 if (lock != nullptr) {
2069 return reinterpret_cast<uint64_t>(JniMethodEndWithReferenceSynchronized(l, cookie, lock, self));
2070 } else {
2071 return reinterpret_cast<uint64_t>(JniMethodEndWithReference(l, cookie, self));
2072 }
2073}
2074
Igor Murashkin06a04e02016-09-13 15:57:37 -07002075static void artQuickGenericJniEndJNINonRef(Thread* self,
2076 uint32_t cookie,
2077 bool fast_native,
2078 jobject lock) {
Andreas Gampead615172014-04-04 16:20:13 -07002079 if (lock != nullptr) {
2080 JniMethodEndSynchronized(cookie, lock, self);
Igor Murashkin06a04e02016-09-13 15:57:37 -07002081 // Ignore "fast_native" here because synchronized functions aren't very fast.
Andreas Gampead615172014-04-04 16:20:13 -07002082 } else {
Igor Murashkin06a04e02016-09-13 15:57:37 -07002083 if (UNLIKELY(fast_native)) {
2084 JniMethodFastEnd(cookie, self);
2085 } else {
2086 JniMethodEnd(cookie, self);
2087 }
Andreas Gampead615172014-04-04 16:20:13 -07002088 }
2089}
2090
Andreas Gampec147b002014-03-06 18:11:06 -08002091/*
2092 * Initializes an alloca region assumed to be directly below sp for a native call:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002093 * 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 -08002094 * The final element on the stack is a pointer to the native code.
2095 *
Andreas Gampe36fea8d2014-03-10 13:37:40 -07002096 * On entry, the stack has a standard callee-save frame above sp, and an alloca below it.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002097 * We need to fix this, as the handle scope needs to go into the callee-save frame.
Andreas Gampe36fea8d2014-03-10 13:37:40 -07002098 *
Andreas Gampec147b002014-03-06 18:11:06 -08002099 * The return of this function denotes:
2100 * 1) How many bytes of the alloca can be released, if the value is non-negative.
2101 * 2) An error, if the value is negative.
2102 */
Mathieu Chartiere401d142015-04-22 13:56:20 -07002103extern "C" TwoWordReturn artQuickGenericJniTrampoline(Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002104 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002105 ArtMethod* called = *sp;
David Sehr709b0702016-10-13 09:12:37 -07002106 DCHECK(called->IsNative()) << called->PrettyMethod(true);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002107 uint32_t shorty_len = 0;
2108 const char* shorty = called->GetShorty(&shorty_len);
Igor Murashkin06a04e02016-09-13 15:57:37 -07002109 bool critical_native = called->IsAnnotatedWithCriticalNative();
2110 bool fast_native = called->IsAnnotatedWithFastNative();
2111 bool normal_native = !critical_native && !fast_native;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002112
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002113 // Run the visitor and update sp.
Igor Murashkin06a04e02016-09-13 15:57:37 -07002114 BuildGenericJniFrameVisitor visitor(self,
2115 called->IsStatic(),
2116 critical_native,
2117 shorty,
2118 shorty_len,
2119 &sp);
Mathieu Chartierbe08cf52016-09-13 13:41:24 -07002120 {
2121 ScopedAssertNoThreadSuspension sants(__FUNCTION__);
2122 visitor.VisitArguments();
2123 // FinalizeHandleScope pushes the handle scope on the thread.
2124 visitor.FinalizeHandleScope(self);
2125 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002126
Andreas Gampec200a4a2014-06-16 18:39:09 -07002127 // Fix up managed-stack things in Thread.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002128 self->SetTopOfStack(sp);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002129
Ian Rogerse0dcd462014-03-08 15:21:04 -08002130 self->VerifyStack();
2131
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002132 uint32_t cookie;
Igor Murashkin06a04e02016-09-13 15:57:37 -07002133 uint32_t* sp32;
2134 // Skip calling JniMethodStart for @CriticalNative.
2135 if (LIKELY(!critical_native)) {
2136 // Start JNI, save the cookie.
2137 if (called->IsSynchronized()) {
2138 DCHECK(normal_native) << " @FastNative and synchronize is not supported";
2139 cookie = JniMethodStartSynchronized(visitor.GetFirstHandleScopeJObject(), self);
2140 if (self->IsExceptionPending()) {
2141 self->PopHandleScope();
2142 // A negative value denotes an error.
2143 return GetTwoWordFailureValue();
2144 }
2145 } else {
2146 if (fast_native) {
2147 cookie = JniMethodFastStart(self);
2148 } else {
2149 DCHECK(normal_native);
2150 cookie = JniMethodStart(self);
2151 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002152 }
Igor Murashkin06a04e02016-09-13 15:57:37 -07002153 sp32 = reinterpret_cast<uint32_t*>(sp);
2154 *(sp32 - 1) = cookie;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002155 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002156
Andreas Gampe90546832014-03-12 18:07:19 -07002157 // Retrieve the stored native code.
Alex Lightd78ddec2017-04-18 15:20:38 -07002158 void const* nativeCode = called->GetEntryPointFromJni();
Andreas Gampe90546832014-03-12 18:07:19 -07002159
Andreas Gampe9a6a99a2014-03-14 07:52:20 -07002160 // There are two cases for the content of nativeCode:
2161 // 1) Pointer to the native function.
2162 // 2) Pointer to the trampoline for native code binding.
2163 // In the second case, we need to execute the binding and continue with the actual native function
2164 // pointer.
Andreas Gampe90546832014-03-12 18:07:19 -07002165 DCHECK(nativeCode != nullptr);
2166 if (nativeCode == GetJniDlsymLookupStub()) {
Ian Rogers04c31d22014-07-07 21:44:06 -07002167#if defined(__arm__) || defined(__aarch64__)
Andreas Gampe90546832014-03-12 18:07:19 -07002168 nativeCode = artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07002169#else
2170 nativeCode = artFindNativeMethod(self);
2171#endif
Andreas Gampe90546832014-03-12 18:07:19 -07002172
2173 if (nativeCode == nullptr) {
2174 DCHECK(self->IsExceptionPending()); // There should be an exception pending now.
Andreas Gampead615172014-04-04 16:20:13 -07002175
Igor Murashkin06a04e02016-09-13 15:57:37 -07002176 // @CriticalNative calls do not need to call back into JniMethodEnd.
2177 if (LIKELY(!critical_native)) {
2178 // End JNI, as the assembly will move to deliver the exception.
2179 jobject lock = called->IsSynchronized() ? visitor.GetFirstHandleScopeJObject() : nullptr;
2180 if (shorty[0] == 'L') {
2181 artQuickGenericJniEndJNIRef(self, cookie, fast_native, nullptr, lock);
2182 } else {
2183 artQuickGenericJniEndJNINonRef(self, cookie, fast_native, lock);
2184 }
Andreas Gampead615172014-04-04 16:20:13 -07002185 }
2186
Andreas Gampec200a4a2014-06-16 18:39:09 -07002187 return GetTwoWordFailureValue();
Andreas Gampe90546832014-03-12 18:07:19 -07002188 }
2189 // Note that the native code pointer will be automatically set by artFindNativeMethod().
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002190 }
2191
Alexey Frunze1b8464d2016-11-12 17:22:05 -08002192#if defined(__mips__) && !defined(__LP64__)
2193 // On MIPS32 if the first two arguments are floating-point, we need to know their types
2194 // so that art_quick_generic_jni_trampoline can correctly extract them from the stack
2195 // and load into floating-point registers.
2196 // Possible arrangements of first two floating-point arguments on the stack (32-bit FPU
2197 // view):
2198 // (1)
2199 // | DOUBLE | DOUBLE | other args, if any
2200 // | F12 | F13 | F14 | F15 |
2201 // | SP+0 | SP+4 | SP+8 | SP+12 | SP+16
2202 // (2)
2203 // | DOUBLE | FLOAT | (PAD) | other args, if any
2204 // | F12 | F13 | F14 | |
2205 // | SP+0 | SP+4 | SP+8 | SP+12 | SP+16
2206 // (3)
2207 // | FLOAT | (PAD) | DOUBLE | other args, if any
2208 // | F12 | | F14 | F15 |
2209 // | SP+0 | SP+4 | SP+8 | SP+12 | SP+16
2210 // (4)
2211 // | FLOAT | FLOAT | other args, if any
2212 // | F12 | F14 |
2213 // | SP+0 | SP+4 | SP+8
2214 // As you can see, only the last case (4) is special. In all others we can just
2215 // load F12/F13 and F14/F15 in the same manner.
2216 // Set bit 0 of the native code address to 1 in this case (valid code addresses
2217 // are always a multiple of 4 on MIPS32, so we have 2 spare bits available).
2218 if (nativeCode != nullptr &&
2219 shorty != nullptr &&
2220 shorty_len >= 3 &&
2221 shorty[1] == 'F' &&
2222 shorty[2] == 'F') {
2223 nativeCode = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(nativeCode) | 1);
2224 }
2225#endif
2226
Andreas Gampec200a4a2014-06-16 18:39:09 -07002227 // Return native code addr(lo) and bottom of alloca address(hi).
2228 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(visitor.GetBottomOfUsedArea()),
2229 reinterpret_cast<uintptr_t>(nativeCode));
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002230}
2231
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002232// Defined in quick_jni_entrypoints.cc.
2233extern uint64_t GenericJniMethodEnd(Thread* self, uint32_t saved_local_ref_cookie,
2234 jvalue result, uint64_t result_f, ArtMethod* called,
2235 HandleScope* handle_scope);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002236/*
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002237 * Is called after the native JNI code. Responsible for cleanup (handle scope, saved state) and
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002238 * unlocking.
2239 */
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002240extern "C" uint64_t artQuickGenericJniEndTrampoline(Thread* self,
2241 jvalue result,
2242 uint64_t result_f) {
2243 // We're here just back from a native call. We don't have the shared mutator lock at this point
2244 // yet until we call GoToRunnable() later in GenericJniMethodEnd(). Accessing objects or doing
2245 // anything that requires a mutator lock before that would cause problems as GC may have the
2246 // exclusive mutator lock and may be moving objects, etc.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002247 ArtMethod** sp = self->GetManagedStack()->GetTopQuickFrame();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002248 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002249 ArtMethod* called = *sp;
Ian Rogerse0dcd462014-03-08 15:21:04 -08002250 uint32_t cookie = *(sp32 - 1);
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002251 HandleScope* table = reinterpret_cast<HandleScope*>(reinterpret_cast<uint8_t*>(sp) + sizeof(*sp));
2252 return GenericJniMethodEnd(self, cookie, result, result_f, called, table);
Andreas Gampe2da88232014-02-27 12:26:20 -08002253}
2254
Andreas Gamped58342c2014-06-05 14:18:08 -07002255// We use TwoWordReturn to optimize scalar returns. We use the hi value for code, and the lo value
2256// for the method pointer.
Andreas Gampe51f76352014-05-21 08:28:48 -07002257//
Andreas Gamped58342c2014-06-05 14:18:08 -07002258// 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 -07002259// to hold the mutator lock (see REQUIRES_SHARED(Locks::mutator_lock_) annotations).
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002260
2261template<InvokeType type, bool access_check>
Mathieu Chartieref41db72016-10-25 15:08:01 -07002262static TwoWordReturn artInvokeCommon(uint32_t method_idx,
2263 ObjPtr<mirror::Object> this_object,
2264 Thread* self,
Mathieu Chartiere401d142015-04-22 13:56:20 -07002265 ArtMethod** sp) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002266 ScopedQuickEntrypointChecks sqec(self);
Vladimir Markofd36f1f2016-08-03 18:49:58 +01002267 DCHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(Runtime::kSaveRefsAndArgs));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002268 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
2269 ArtMethod* method = FindMethodFast(method_idx, this_object, caller_method, access_check, type);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002270 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002271 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()->GetDexFile();
2272 uint32_t shorty_len;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002273 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(method_idx), &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002274 {
2275 // Remember the args in case a GC happens in FindMethodFromCode.
2276 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
2277 RememberForGcArgumentVisitor visitor(sp, type == kStatic, shorty, shorty_len, &soa);
2278 visitor.VisitArguments();
Mathieu Chartieref41db72016-10-25 15:08:01 -07002279 method = FindMethodFromCode<type, access_check>(method_idx,
2280 &this_object,
2281 caller_method,
Mathieu Chartier0cd81352014-05-22 16:48:55 -07002282 self);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002283 visitor.FixupReferences();
2284 }
2285
Ian Rogerse0a02da2014-12-02 14:10:53 -08002286 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002287 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07002288 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002289 }
2290 }
2291 DCHECK(!self->IsExceptionPending());
2292 const void* code = method->GetEntryPointFromQuickCompiledCode();
2293
2294 // When we return, the caller will branch to this address, so it had better not be 0!
David Sehr709b0702016-10-13 09:12:37 -07002295 DCHECK(code != nullptr) << "Code was null in method: " << method->PrettyMethod()
Andreas Gampec200a4a2014-06-16 18:39:09 -07002296 << " location: "
2297 << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07002298
Andreas Gamped58342c2014-06-05 14:18:08 -07002299 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
2300 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002301}
2302
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01002303// Explicit artInvokeCommon template function declarations to please analysis tool.
2304#define EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(type, access_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002305 template REQUIRES_SHARED(Locks::mutator_lock_) \
Mathieu Chartiere401d142015-04-22 13:56:20 -07002306 TwoWordReturn artInvokeCommon<type, access_check>( \
Mathieu Chartieref41db72016-10-25 15:08:01 -07002307 uint32_t method_idx, ObjPtr<mirror::Object> his_object, Thread* self, ArtMethod** sp)
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01002308
2309EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, false);
2310EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, true);
2311EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, false);
2312EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, true);
2313EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, false);
2314EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, true);
2315EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, false);
2316EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, true);
2317EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, false);
2318EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, true);
2319#undef EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL
2320
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002321// See comments in runtime_support_asm.S
Andreas Gampec200a4a2014-06-16 18:39:09 -07002322extern "C" TwoWordReturn artInvokeInterfaceTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002323 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002324 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002325 return artInvokeCommon<kInterface, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002326}
2327
Andreas Gampec200a4a2014-06-16 18:39:09 -07002328extern "C" TwoWordReturn artInvokeDirectTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002329 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002330 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002331 return artInvokeCommon<kDirect, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002332}
2333
Andreas Gampec200a4a2014-06-16 18:39:09 -07002334extern "C" TwoWordReturn artInvokeStaticTrampolineWithAccessCheck(
Mathieu Chartieref41db72016-10-25 15:08:01 -07002335 uint32_t method_idx,
2336 mirror::Object* this_object ATTRIBUTE_UNUSED,
2337 Thread* self,
2338 ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
2339 // For static, this_object is not required and may be random garbage. Don't pass it down so that
2340 // it doesn't cause ObjPtr alignment failure check.
2341 return artInvokeCommon<kStatic, true>(method_idx, nullptr, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002342}
2343
Andreas Gampec200a4a2014-06-16 18:39:09 -07002344extern "C" TwoWordReturn artInvokeSuperTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002345 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002346 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002347 return artInvokeCommon<kSuper, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002348}
2349
Andreas Gampec200a4a2014-06-16 18:39:09 -07002350extern "C" TwoWordReturn artInvokeVirtualTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002351 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002352 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002353 return artInvokeCommon<kVirtual, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002354}
2355
Jeff Hao5667f562017-02-27 19:32:01 -08002356// Determine target of interface dispatch. The interface method and this object are known non-null.
2357// The interface method is the method returned by the dex cache in the conflict trampoline.
2358extern "C" TwoWordReturn artInvokeInterfaceTrampoline(ArtMethod* interface_method,
Mathieu Chartieref41db72016-10-25 15:08:01 -07002359 mirror::Object* raw_this_object,
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002360 Thread* self,
2361 ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002362 REQUIRES_SHARED(Locks::mutator_lock_) {
Jeff Hao5667f562017-02-27 19:32:01 -08002363 CHECK(interface_method != nullptr);
Mathieu Chartieref41db72016-10-25 15:08:01 -07002364 ObjPtr<mirror::Object> this_object(raw_this_object);
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002365 ScopedQuickEntrypointChecks sqec(self);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002366 StackHandleScope<1> hs(self);
2367 Handle<mirror::Class> cls(hs.NewHandle(this_object->GetClass()));
2368
Nicolas Geoffray5bf7bac2016-07-06 14:18:23 +00002369 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002370 ArtMethod* method = nullptr;
Andreas Gampe542451c2016-07-26 09:02:02 -07002371 ImTable* imt = cls->GetImt(kRuntimePointerSize);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002372
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002373 if (LIKELY(interface_method->GetDexMethodIndex() != DexFile::kDexNoIndex)) {
Jeff Hao5667f562017-02-27 19:32:01 -08002374 // If the interface method is already resolved, look whether we have a match in the
2375 // ImtConflictTable.
Andreas Gampe75a7db62016-09-26 12:04:26 -07002376 ArtMethod* conflict_method = imt->Get(ImTable::GetImtIndex(interface_method),
2377 kRuntimePointerSize);
Alex Light9fc547a2016-03-31 14:34:33 -07002378 if (LIKELY(conflict_method->IsRuntimeMethod())) {
Andreas Gampe542451c2016-07-26 09:02:02 -07002379 ImtConflictTable* current_table = conflict_method->GetImtConflictTable(kRuntimePointerSize);
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002380 DCHECK(current_table != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -07002381 method = current_table->Lookup(interface_method, kRuntimePointerSize);
Alex Light9fc547a2016-03-31 14:34:33 -07002382 } else {
2383 // It seems we aren't really a conflict method!
Andreas Gampe542451c2016-07-26 09:02:02 -07002384 method = cls->FindVirtualMethodForInterface(interface_method, kRuntimePointerSize);
Alex Light9fc547a2016-03-31 14:34:33 -07002385 }
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002386 if (method != nullptr) {
2387 return GetTwoWordSuccessValue(
2388 reinterpret_cast<uintptr_t>(method->GetEntryPointFromQuickCompiledCode()),
2389 reinterpret_cast<uintptr_t>(method));
2390 }
2391
2392 // No match, use the IfTable.
Andreas Gampe542451c2016-07-26 09:02:02 -07002393 method = cls->FindVirtualMethodForInterface(interface_method, kRuntimePointerSize);
Ian Rogerse0a02da2014-12-02 14:10:53 -08002394 if (UNLIKELY(method == nullptr)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002395 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(
2396 interface_method, this_object, caller_method);
Andreas Gamped58342c2014-06-05 14:18:08 -07002397 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002398 }
2399 } else {
Jeff Hao5667f562017-02-27 19:32:01 -08002400 // The interface method is unresolved, so look it up in the dex file of the caller.
Mathieu Chartier4edd8472015-06-01 10:47:36 -07002401 DCHECK_EQ(interface_method, Runtime::Current()->GetResolutionMethod());
Jeff Hao5667f562017-02-27 19:32:01 -08002402
2403 // Fetch the dex_method_idx of the target interface method from the caller.
2404 uint32_t dex_method_idx;
2405 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
2406 const DexFile::CodeItem* code_item = caller_method->GetCodeItem();
2407 DCHECK_LT(dex_pc, code_item->insns_size_in_code_units_);
2408 const Instruction* instr = Instruction::At(&code_item->insns_[dex_pc]);
2409 Instruction::Code instr_code = instr->Opcode();
2410 DCHECK(instr_code == Instruction::INVOKE_INTERFACE ||
2411 instr_code == Instruction::INVOKE_INTERFACE_RANGE)
2412 << "Unexpected call into interface trampoline: " << instr->DumpString(nullptr);
2413 if (instr_code == Instruction::INVOKE_INTERFACE) {
2414 dex_method_idx = instr->VRegB_35c();
2415 } else {
2416 DCHECK_EQ(instr_code, Instruction::INVOKE_INTERFACE_RANGE);
2417 dex_method_idx = instr->VRegB_3rc();
2418 }
2419
Andreas Gampec200a4a2014-06-16 18:39:09 -07002420 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()
2421 ->GetDexFile();
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002422 uint32_t shorty_len;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002423 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(dex_method_idx),
2424 &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002425 {
2426 // Remember the args in case a GC happens in FindMethodFromCode.
2427 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
2428 RememberForGcArgumentVisitor visitor(sp, false, shorty, shorty_len, &soa);
2429 visitor.VisitArguments();
Mathieu Chartieref41db72016-10-25 15:08:01 -07002430 method = FindMethodFromCode<kInterface, false>(dex_method_idx,
2431 &this_object,
2432 caller_method,
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002433 self);
2434 visitor.FixupReferences();
2435 }
2436
2437 if (UNLIKELY(method == nullptr)) {
2438 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07002439 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002440 }
Andreas Gampe542451c2016-07-26 09:02:02 -07002441 interface_method =
2442 caller_method->GetDexCacheResolvedMethod(dex_method_idx, kRuntimePointerSize);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002443 DCHECK(!interface_method->IsRuntimeMethod());
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002444 }
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002445
2446 // We arrive here if we have found an implementation, and it is not in the ImtConflictTable.
2447 // We create a new table with the new pair { interface_method, method }.
Andreas Gampe75a7db62016-09-26 12:04:26 -07002448 uint32_t imt_index = ImTable::GetImtIndex(interface_method);
Andreas Gampe542451c2016-07-26 09:02:02 -07002449 ArtMethod* conflict_method = imt->Get(imt_index, kRuntimePointerSize);
Alex Light9fc547a2016-03-31 14:34:33 -07002450 if (conflict_method->IsRuntimeMethod()) {
Mathieu Chartier7f98c9a2016-04-14 10:49:19 -07002451 ArtMethod* new_conflict_method = Runtime::Current()->GetClassLinker()->AddMethodToConflictTable(
2452 cls.Get(),
2453 conflict_method,
2454 interface_method,
2455 method,
2456 /*force_new_conflict_method*/false);
2457 if (new_conflict_method != conflict_method) {
Alex Light9fc547a2016-03-31 14:34:33 -07002458 // Update the IMT if we create a new conflict method. No fence needed here, as the
2459 // data is consistent.
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002460 imt->Set(imt_index,
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002461 new_conflict_method,
Andreas Gampe542451c2016-07-26 09:02:02 -07002462 kRuntimePointerSize);
Alex Light9fc547a2016-03-31 14:34:33 -07002463 }
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002464 }
2465
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002466 const void* code = method->GetEntryPointFromQuickCompiledCode();
2467
2468 // When we return, the caller will branch to this address, so it had better not be 0!
David Sehr709b0702016-10-13 09:12:37 -07002469 DCHECK(code != nullptr) << "Code was null in method: " << method->PrettyMethod()
Andreas Gampec200a4a2014-06-16 18:39:09 -07002470 << " location: " << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07002471
Andreas Gamped58342c2014-06-05 14:18:08 -07002472 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
2473 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002474}
2475
Orion Hodsonac141392017-01-13 11:53:47 +00002476// Returns shorty type so the caller can determine how to put |result|
2477// into expected registers. The shorty type is static so the compiler
2478// could call different flavors of this code path depending on the
2479// shorty type though this would require different entry points for
2480// each type.
2481extern "C" uintptr_t artInvokePolymorphic(
2482 JValue* result,
2483 mirror::Object* raw_method_handle,
2484 Thread* self,
2485 ArtMethod** sp)
2486 REQUIRES_SHARED(Locks::mutator_lock_) {
2487 ScopedQuickEntrypointChecks sqec(self);
2488 DCHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(Runtime::kSaveRefsAndArgs));
2489
2490 // Start new JNI local reference state
2491 JNIEnvExt* env = self->GetJniEnv();
2492 ScopedObjectAccessUnchecked soa(env);
2493 ScopedJniEnvLocalRefState env_state(env);
2494 const char* old_cause = self->StartAssertNoThreadSuspension("Making stack arguments safe.");
2495
2496 // From the instruction, get the |callsite_shorty| and expose arguments on the stack to the GC.
2497 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
2498 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
2499 const DexFile::CodeItem* code = caller_method->GetCodeItem();
2500 const Instruction* inst = Instruction::At(&code->insns_[dex_pc]);
2501 DCHECK(inst->Opcode() == Instruction::INVOKE_POLYMORPHIC ||
2502 inst->Opcode() == Instruction::INVOKE_POLYMORPHIC_RANGE);
2503 const DexFile* dex_file = caller_method->GetDexFile();
2504 const uint32_t proto_idx = inst->VRegH();
2505 const char* shorty = dex_file->GetShorty(proto_idx);
2506 const size_t shorty_length = strlen(shorty);
2507 static const bool kMethodIsStatic = false; // invoke() and invokeExact() are not static.
2508 RememberForGcArgumentVisitor gc_visitor(sp, kMethodIsStatic, shorty, shorty_length, &soa);
Orion Hodsonfea84dd2017-01-16 13:52:20 +00002509 gc_visitor.VisitArguments();
Orion Hodsonac141392017-01-13 11:53:47 +00002510
2511 // Wrap raw_method_handle in a Handle for safety.
2512 StackHandleScope<5> hs(self);
Orion Hodsonc069a302017-01-18 09:23:12 +00002513 Handle<mirror::MethodHandle> method_handle(
2514 hs.NewHandle(ObjPtr<mirror::MethodHandle>::DownCast(MakeObjPtr(raw_method_handle))));
Orion Hodsonac141392017-01-13 11:53:47 +00002515 raw_method_handle = nullptr;
2516 self->EndAssertNoThreadSuspension(old_cause);
2517
2518 // Resolve method - it's either MethodHandle.invoke() or MethodHandle.invokeExact().
2519 ClassLinker* linker = Runtime::Current()->GetClassLinker();
2520 ArtMethod* resolved_method = linker->ResolveMethod<ClassLinker::kForceICCECheck>(self,
2521 inst->VRegB(),
2522 caller_method,
2523 kVirtual);
2524 DCHECK((resolved_method ==
2525 jni::DecodeArtMethod(WellKnownClasses::java_lang_invoke_MethodHandle_invokeExact)) ||
2526 (resolved_method ==
2527 jni::DecodeArtMethod(WellKnownClasses::java_lang_invoke_MethodHandle_invoke)));
2528 if (UNLIKELY(method_handle.IsNull())) {
2529 ThrowNullPointerExceptionForMethodAccess(resolved_method, InvokeType::kVirtual);
2530 return static_cast<uintptr_t>('V');
2531 }
2532
2533 Handle<mirror::Class> caller_class(hs.NewHandle(caller_method->GetDeclaringClass()));
2534 Handle<mirror::MethodType> method_type(hs.NewHandle(linker->ResolveMethodType(
2535 *dex_file, proto_idx,
2536 hs.NewHandle<mirror::DexCache>(caller_class->GetDexCache()),
2537 hs.NewHandle<mirror::ClassLoader>(caller_class->GetClassLoader()))));
2538 // This implies we couldn't resolve one or more types in this method handle.
2539 if (UNLIKELY(method_type.IsNull())) {
2540 CHECK(self->IsExceptionPending());
2541 return static_cast<uintptr_t>('V');
2542 }
2543
2544 DCHECK_EQ(ArtMethod::NumArgRegisters(shorty) + 1u, (uint32_t)inst->VRegA());
2545 DCHECK_EQ(resolved_method->IsStatic(), kMethodIsStatic);
2546
2547 // Fix references before constructing the shadow frame.
2548 gc_visitor.FixupReferences();
2549
2550 // Construct shadow frame placing arguments consecutively from |first_arg|.
2551 const bool is_range = (inst->Opcode() == Instruction::INVOKE_POLYMORPHIC_RANGE);
2552 const size_t num_vregs = is_range ? inst->VRegA_4rcc() : inst->VRegA_45cc();
2553 const size_t first_arg = 0;
2554 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
2555 CREATE_SHADOW_FRAME(num_vregs, /* link */ nullptr, resolved_method, dex_pc);
2556 ShadowFrame* shadow_frame = shadow_frame_unique_ptr.get();
2557 ScopedStackedShadowFramePusher
2558 frame_pusher(self, shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
2559 BuildQuickShadowFrameVisitor shadow_frame_builder(sp,
2560 kMethodIsStatic,
2561 shorty,
2562 strlen(shorty),
2563 shadow_frame,
2564 first_arg);
2565 shadow_frame_builder.VisitArguments();
2566
2567 // Push a transition back into managed code onto the linked list in thread.
2568 ManagedStack fragment;
2569 self->PushManagedStackFragment(&fragment);
2570
2571 // Call DoInvokePolymorphic with |is_range| = true, as shadow frame has argument registers in
2572 // consecutive order.
2573 uint32_t unused_args[Instruction::kMaxVarArgRegs] = {};
2574 uint32_t first_callee_arg = first_arg + 1;
Orion Hodsonc069a302017-01-18 09:23:12 +00002575 if (!DoInvokePolymorphic<true /* is_range */>(self,
2576 resolved_method,
2577 *shadow_frame,
2578 method_handle,
2579 method_type,
2580 unused_args,
2581 first_callee_arg,
2582 result)) {
Orion Hodsonac141392017-01-13 11:53:47 +00002583 DCHECK(self->IsExceptionPending());
2584 }
2585
2586 // Pop transition record.
2587 self->PopManagedStackFragment(fragment);
2588
2589 return static_cast<uintptr_t>(shorty[0]);
2590}
2591
Ian Rogers848871b2013-08-05 10:56:33 -07002592} // namespace art