blob: a3e5b552b54b496e5cbf0daaf09ac23e9d60c613 [file] [log] [blame]
Ian Rogers848871b2013-08-05 10:56:33 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Mathieu Chartiere401d142015-04-22 13:56:20 -070017#include "art_method-inl.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070018#include "base/enums.h"
Ian Rogers848871b2013-08-05 10:56:33 -070019#include "callee_save_frame.h"
Dragos Sbirleabd136a22013-08-13 18:07:04 -070020#include "common_throws.h"
Ian Rogers848871b2013-08-05 10:56:33 -070021#include "dex_file-inl.h"
22#include "dex_instruction-inl.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070023#include "entrypoints/entrypoint_utils-inl.h"
Ian Rogers6f3dbba2014-10-14 17:41:57 -070024#include "entrypoints/runtime_asm_entrypoints.h"
Ian Rogers83883d72013-10-21 21:07:24 -070025#include "gc/accounting/card_table-inl.h"
Andreas Gampe75a7db62016-09-26 12:04:26 -070026#include "imt_conflict_table.h"
27#include "imtable-inl.h"
Ian Rogers848871b2013-08-05 10:56:33 -070028#include "interpreter/interpreter.h"
Nicolas Geoffray796d6302016-03-13 22:22:31 +000029#include "linear_alloc.h"
Ian Rogerse0a02da2014-12-02 14:10:53 -080030#include "method_reference.h"
Ian Rogers848871b2013-08-05 10:56:33 -070031#include "mirror/class-inl.h"
Mathieu Chartier5f3ded42014-04-03 15:25:30 -070032#include "mirror/dex_cache-inl.h"
Mathieu Chartierfc58af42015-04-16 18:00:39 -070033#include "mirror/method.h"
Ian Rogers848871b2013-08-05 10:56:33 -070034#include "mirror/object-inl.h"
35#include "mirror/object_array-inl.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010036#include "oat_quick_method_header.h"
Andreas Gampe639bdd12015-06-03 11:22:45 -070037#include "quick_exception_handler.h"
Ian Rogers848871b2013-08-05 10:56:33 -070038#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070039#include "scoped_thread_state_change-inl.h"
Andreas Gampeb3025922015-09-01 14:45:00 -070040#include "stack.h"
Daniel Mihalyieb076692014-08-22 17:33:31 +020041#include "debugger.h"
Ian Rogers848871b2013-08-05 10:56:33 -070042
43namespace art {
44
45// Visits the arguments as saved to the stack by a Runtime::kRefAndArgs callee save frame.
46class QuickArgumentVisitor {
Ian Rogers936b37f2014-02-14 00:52:24 -080047 // Number of bytes for each out register in the caller method's frame.
48 static constexpr size_t kBytesStackArgLocation = 4;
Alexei Zavjalov41c507a2014-05-15 16:02:46 +070049 // Frame size in bytes of a callee-save frame for RefsAndArgs.
50 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize =
Vladimir Markofd36f1f2016-08-03 18:49:58 +010051 GetCalleeSaveFrameSize(kRuntimeISA, Runtime::kSaveRefsAndArgs);
Ian Rogers848871b2013-08-05 10:56:33 -070052#if defined(__arm__)
53 // The callee save frame is pointed to by SP.
54 // | argN | |
55 // | ... | |
56 // | arg4 | |
57 // | arg3 spill | | Caller's frame
58 // | arg2 spill | |
59 // | arg1 spill | |
60 // | Method* | ---
61 // | LR |
Zheng Xu5667fdb2014-10-23 18:29:55 +080062 // | ... | 4x6 bytes callee saves
63 // | R3 |
64 // | R2 |
65 // | R1 |
66 // | S15 |
67 // | : |
68 // | S0 |
69 // | | 4x2 bytes padding
Ian Rogers848871b2013-08-05 10:56:33 -070070 // | Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -050071 static constexpr bool kSplitPairAcrossRegisterAndStack = kArm32QuickCodeUseSoftFloat;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +000072 static constexpr bool kAlignPairRegister = !kArm32QuickCodeUseSoftFloat;
Zheng Xu5667fdb2014-10-23 18:29:55 +080073 static constexpr bool kQuickSoftFloatAbi = kArm32QuickCodeUseSoftFloat;
74 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = !kArm32QuickCodeUseSoftFloat;
Goran Jakovljevicff734982015-08-24 12:58:55 +000075 static constexpr bool kQuickSkipOddFpRegisters = false;
Zheng Xu5667fdb2014-10-23 18:29:55 +080076 static constexpr size_t kNumQuickGprArgs = 3;
77 static constexpr size_t kNumQuickFprArgs = kArm32QuickCodeUseSoftFloat ? 0 : 16;
Andreas Gampe1a5c4062015-01-15 12:10:47 -080078 static constexpr bool kGprFprLockstep = false;
Zheng Xub551fdc2014-07-25 11:49:42 +080079 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset =
Vladimir Markofd36f1f2016-08-03 18:49:58 +010080 arm::ArmCalleeSaveFpr1Offset(Runtime::kSaveRefsAndArgs); // Offset of first FPR arg.
Zheng Xub551fdc2014-07-25 11:49:42 +080081 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset =
Vladimir Markofd36f1f2016-08-03 18:49:58 +010082 arm::ArmCalleeSaveGpr1Offset(Runtime::kSaveRefsAndArgs); // Offset of first GPR arg.
Zheng Xub551fdc2014-07-25 11:49:42 +080083 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset =
Vladimir Markofd36f1f2016-08-03 18:49:58 +010084 arm::ArmCalleeSaveLrOffset(Runtime::kSaveRefsAndArgs); // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -080085 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +000086 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -080087 }
Stuart Monteithb95a5342014-03-12 13:32:32 +000088#elif defined(__aarch64__)
89 // The callee save frame is pointed to by SP.
90 // | argN | |
91 // | ... | |
92 // | arg4 | |
93 // | arg3 spill | | Caller's frame
94 // | arg2 spill | |
95 // | arg1 spill | |
96 // | Method* | ---
97 // | LR |
Zheng Xub551fdc2014-07-25 11:49:42 +080098 // | X29 |
Stuart Monteithb95a5342014-03-12 13:32:32 +000099 // | : |
Serban Constantinescu9bd88b02015-04-22 16:24:46 +0100100 // | X20 |
Stuart Monteithb95a5342014-03-12 13:32:32 +0000101 // | X7 |
102 // | : |
103 // | X1 |
Zheng Xub551fdc2014-07-25 11:49:42 +0800104 // | D7 |
Stuart Monteithb95a5342014-03-12 13:32:32 +0000105 // | : |
106 // | D0 |
107 // | | padding
108 // | Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500109 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000110 static constexpr bool kAlignPairRegister = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000111 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800112 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000113 static constexpr bool kQuickSkipOddFpRegisters = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000114 static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs.
115 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800116 static constexpr bool kGprFprLockstep = false;
Zheng Xub551fdc2014-07-25 11:49:42 +0800117 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset =
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100118 arm64::Arm64CalleeSaveFpr1Offset(Runtime::kSaveRefsAndArgs); // Offset of first FPR arg.
Zheng Xub551fdc2014-07-25 11:49:42 +0800119 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset =
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100120 arm64::Arm64CalleeSaveGpr1Offset(Runtime::kSaveRefsAndArgs); // Offset of first GPR arg.
Zheng Xub551fdc2014-07-25 11:49:42 +0800121 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset =
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100122 arm64::Arm64CalleeSaveLrOffset(Runtime::kSaveRefsAndArgs); // Offset of return address.
Stuart Monteithb95a5342014-03-12 13:32:32 +0000123 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000124 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Stuart Monteithb95a5342014-03-12 13:32:32 +0000125 }
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800126#elif defined(__mips__) && !defined(__LP64__)
Ian Rogers848871b2013-08-05 10:56:33 -0700127 // The callee save frame is pointed to by SP.
128 // | argN | |
129 // | ... | |
130 // | arg4 | |
131 // | arg3 spill | | Caller's frame
132 // | arg2 spill | |
133 // | arg1 spill | |
134 // | Method* | ---
135 // | RA |
136 // | ... | callee saves
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800137 // | T1 | arg5
138 // | T0 | arg4
Ian Rogers848871b2013-08-05 10:56:33 -0700139 // | A3 | arg3
140 // | A2 | arg2
141 // | A1 | arg1
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800142 // | F19 |
143 // | F18 | f_arg5
144 // | F17 |
145 // | F16 | f_arg4
Goran Jakovljevicff734982015-08-24 12:58:55 +0000146 // | F15 |
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800147 // | F14 | f_arg3
Goran Jakovljevicff734982015-08-24 12:58:55 +0000148 // | F13 |
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800149 // | F12 | f_arg2
150 // | F11 |
151 // | F10 | f_arg1
152 // | F9 |
153 // | F8 | f_arg0
Goran Jakovljevicff734982015-08-24 12:58:55 +0000154 // | | padding
Ian Rogers848871b2013-08-05 10:56:33 -0700155 // | A0/Method* | <- sp
Goran Jakovljevicff734982015-08-24 12:58:55 +0000156 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
157 static constexpr bool kAlignPairRegister = true;
158 static constexpr bool kQuickSoftFloatAbi = false;
Zheng Xu5667fdb2014-10-23 18:29:55 +0800159 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000160 static constexpr bool kQuickSkipOddFpRegisters = true;
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800161 static constexpr size_t kNumQuickGprArgs = 5; // 5 arguments passed in GPRs.
162 static constexpr size_t kNumQuickFprArgs = 12; // 6 arguments passed in FPRs. Floats can be
163 // passed only in even numbered registers and each
164 // double occupies two registers.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800165 static constexpr bool kGprFprLockstep = false;
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800166 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 8; // Offset of first FPR arg.
167 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 56; // Offset of first GPR arg.
168 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 108; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800169 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000170 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800171 }
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800172#elif defined(__mips__) && defined(__LP64__)
173 // The callee save frame is pointed to by SP.
174 // | argN | |
175 // | ... | |
176 // | arg4 | |
177 // | arg3 spill | | Caller's frame
178 // | arg2 spill | |
179 // | arg1 spill | |
180 // | Method* | ---
181 // | RA |
182 // | ... | callee saves
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800183 // | A7 | arg7
184 // | A6 | arg6
185 // | A5 | arg5
186 // | A4 | arg4
187 // | A3 | arg3
188 // | A2 | arg2
189 // | A1 | arg1
Goran Jakovljevicff734982015-08-24 12:58:55 +0000190 // | F19 | f_arg7
191 // | F18 | f_arg6
192 // | F17 | f_arg5
193 // | F16 | f_arg4
194 // | F15 | f_arg3
195 // | F14 | f_arg2
196 // | F13 | f_arg1
197 // | F12 | f_arg0
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800198 // | | padding
199 // | A0/Method* | <- sp
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800200 // NOTE: for Mip64, when A0 is skipped, F12 is also skipped.
Douglas Leungd18e0832015-02-09 15:22:26 -0800201 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800202 static constexpr bool kAlignPairRegister = false;
203 static constexpr bool kQuickSoftFloatAbi = false;
204 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000205 static constexpr bool kQuickSkipOddFpRegisters = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800206 static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs.
207 static constexpr size_t kNumQuickFprArgs = 7; // 7 arguments passed in FPRs.
208 static constexpr bool kGprFprLockstep = true;
209
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800210 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 24; // Offset of first FPR arg (F13).
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800211 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 80; // Offset of first GPR arg (A1).
212 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 200; // Offset of return address.
213 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
214 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
215 }
Ian Rogers848871b2013-08-05 10:56:33 -0700216#elif defined(__i386__)
217 // The callee save frame is pointed to by SP.
218 // | argN | |
219 // | ... | |
220 // | arg4 | |
221 // | arg3 spill | | Caller's frame
222 // | arg2 spill | |
223 // | arg1 spill | |
224 // | Method* | ---
225 // | Return |
226 // | EBP,ESI,EDI | callee saves
227 // | EBX | arg3
228 // | EDX | arg2
229 // | ECX | arg1
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000230 // | XMM3 | float arg 4
231 // | XMM2 | float arg 3
232 // | XMM1 | float arg 2
233 // | XMM0 | float arg 1
Ian Rogers848871b2013-08-05 10:56:33 -0700234 // | EAX/Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500235 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000236 static constexpr bool kAlignPairRegister = false;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000237 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800238 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000239 static constexpr bool kQuickSkipOddFpRegisters = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800240 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs.
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000241 static constexpr size_t kNumQuickFprArgs = 4; // 4 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800242 static constexpr bool kGprFprLockstep = false;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000243 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 4; // Offset of first FPR arg.
244 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 4 + 4*8; // Offset of first GPR arg.
245 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 28 + 4*8; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800246 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000247 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800248 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800249#elif defined(__x86_64__)
Ian Rogers936b37f2014-02-14 00:52:24 -0800250 // The callee save frame is pointed to by SP.
251 // | argN | |
252 // | ... | |
253 // | reg. arg spills | | Caller's frame
254 // | Method* | ---
255 // | Return |
256 // | R15 | callee save
257 // | R14 | callee save
258 // | R13 | callee save
259 // | R12 | callee save
260 // | R9 | arg5
261 // | R8 | arg4
262 // | RSI/R6 | arg1
263 // | RBP/R5 | callee save
264 // | RBX/R3 | callee save
265 // | RDX/R2 | arg2
266 // | RCX/R1 | arg3
267 // | XMM7 | float arg 8
268 // | XMM6 | float arg 7
269 // | XMM5 | float arg 6
270 // | XMM4 | float arg 5
271 // | XMM3 | float arg 4
272 // | XMM2 | float arg 3
273 // | XMM1 | float arg 2
274 // | XMM0 | float arg 1
275 // | Padding |
276 // | RDI/Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500277 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000278 static constexpr bool kAlignPairRegister = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800279 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800280 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000281 static constexpr bool kQuickSkipOddFpRegisters = false;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700282 static constexpr size_t kNumQuickGprArgs = 5; // 5 arguments passed in GPRs.
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700283 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800284 static constexpr bool kGprFprLockstep = false;
Ian Rogers936b37f2014-02-14 00:52:24 -0800285 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 16; // Offset of first FPR arg.
Serguei Katkovc3801912014-07-08 17:21:53 +0700286 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 80 + 4*8; // Offset of first GPR arg.
287 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 168 + 4*8; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800288 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
289 switch (gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000290 case 0: return (4 * GetBytesPerGprSpillLocation(kRuntimeISA));
291 case 1: return (1 * GetBytesPerGprSpillLocation(kRuntimeISA));
292 case 2: return (0 * GetBytesPerGprSpillLocation(kRuntimeISA));
293 case 3: return (5 * GetBytesPerGprSpillLocation(kRuntimeISA));
294 case 4: return (6 * GetBytesPerGprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800295 default:
Andreas Gampec200a4a2014-06-16 18:39:09 -0700296 LOG(FATAL) << "Unexpected GPR index: " << gpr_index;
297 return 0;
Ian Rogers936b37f2014-02-14 00:52:24 -0800298 }
299 }
Ian Rogers848871b2013-08-05 10:56:33 -0700300#else
301#error "Unsupported architecture"
Ian Rogers848871b2013-08-05 10:56:33 -0700302#endif
303
Ian Rogers936b37f2014-02-14 00:52:24 -0800304 public:
Sebastien Hertza836bc92014-11-25 16:30:53 +0100305 // Special handling for proxy methods. Proxy methods are instance methods so the
306 // 'this' object is the 1st argument. They also have the same frame layout as the
307 // kRefAndArgs runtime method. Since 'this' is a reference, it is located in the
308 // 1st GPR.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700309 static mirror::Object* GetProxyThisObject(ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700310 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000311 CHECK((*sp)->IsProxyMethod());
Sebastien Hertza836bc92014-11-25 16:30:53 +0100312 CHECK_GT(kNumQuickGprArgs, 0u);
313 constexpr uint32_t kThisGprIndex = 0u; // 'this' is in the 1st GPR.
314 size_t this_arg_offset = kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset +
315 GprIndexToGprOffset(kThisGprIndex);
316 uint8_t* this_arg_address = reinterpret_cast<uint8_t*>(sp) + this_arg_offset;
317 return reinterpret_cast<StackReference<mirror::Object>*>(this_arg_address)->AsMirrorPtr();
318 }
319
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700320 static ArtMethod* GetCallingMethod(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700321 DCHECK((*sp)->IsCalleeSaveMethod());
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100322 return GetCalleeSaveMethodCaller(sp, Runtime::kSaveRefsAndArgs);
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +0100323 }
324
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700325 static ArtMethod* GetOuterMethod(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700326 DCHECK((*sp)->IsCalleeSaveMethod());
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100327 uint8_t* previous_sp =
328 reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700329 return *reinterpret_cast<ArtMethod**>(previous_sp);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100330 }
331
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700332 static uint32_t GetCallingDexPc(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700333 DCHECK((*sp)->IsCalleeSaveMethod());
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100334 const size_t callee_frame_size = GetCalleeSaveFrameSize(kRuntimeISA, Runtime::kSaveRefsAndArgs);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700335 ArtMethod** caller_sp = reinterpret_cast<ArtMethod**>(
336 reinterpret_cast<uintptr_t>(sp) + callee_frame_size);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100337 uintptr_t outer_pc = QuickArgumentVisitor::GetCallingPc(sp);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100338 const OatQuickMethodHeader* current_code = (*caller_sp)->GetOatQuickMethodHeader(outer_pc);
339 uintptr_t outer_pc_offset = current_code->NativeQuickPcOffset(outer_pc);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100340
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100341 if (current_code->IsOptimized()) {
342 CodeInfo code_info = current_code->GetOptimizedCodeInfo();
David Srbecky09ed0982016-02-12 21:58:43 +0000343 CodeInfoEncoding encoding = code_info.ExtractEncoding();
David Brazdilf677ebf2015-05-29 16:29:43 +0100344 StackMap stack_map = code_info.GetStackMapForNativePcOffset(outer_pc_offset, encoding);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100345 DCHECK(stack_map.IsValid());
David Srbecky09ed0982016-02-12 21:58:43 +0000346 if (stack_map.HasInlineInfo(encoding.stack_map_encoding)) {
David Brazdilf677ebf2015-05-29 16:29:43 +0100347 InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map, encoding);
David Srbecky61b28a12016-02-25 21:55:03 +0000348 return inline_info.GetDexPcAtDepth(encoding.inline_info_encoding,
349 inline_info.GetDepth(encoding.inline_info_encoding)-1);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100350 } else {
David Srbecky09ed0982016-02-12 21:58:43 +0000351 return stack_map.GetDexPc(encoding.stack_map_encoding);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100352 }
353 } else {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100354 return current_code->ToDexPc(*caller_sp, outer_pc);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100355 }
Ian Rogers848871b2013-08-05 10:56:33 -0700356 }
357
Ian Rogers936b37f2014-02-14 00:52:24 -0800358 // For the given quick ref and args quick frame, return the caller's PC.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700359 static uintptr_t GetCallingPc(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700360 DCHECK((*sp)->IsCalleeSaveMethod());
Ian Rogers13735952014-10-08 12:43:28 -0700361 uint8_t* lr = reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_LrOffset;
Ian Rogers848871b2013-08-05 10:56:33 -0700362 return *reinterpret_cast<uintptr_t*>(lr);
363 }
364
Mathieu Chartiere401d142015-04-22 13:56:20 -0700365 QuickArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700366 uint32_t shorty_len) REQUIRES_SHARED(Locks::mutator_lock_) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700367 is_static_(is_static), shorty_(shorty), shorty_len_(shorty_len),
Ian Rogers13735952014-10-08 12:43:28 -0700368 gpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset),
369 fpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset),
370 stack_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize
Mathieu Chartiere401d142015-04-22 13:56:20 -0700371 + sizeof(ArtMethod*)), // Skip ArtMethod*.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800372 gpr_index_(0), fpr_index_(0), fpr_double_index_(0), stack_index_(0),
373 cur_type_(Primitive::kPrimVoid), is_split_long_or_double_(false) {
Andreas Gampe575e78c2014-11-03 23:41:03 -0800374 static_assert(kQuickSoftFloatAbi == (kNumQuickFprArgs == 0),
375 "Number of Quick FPR arguments unexpected");
376 static_assert(!(kQuickSoftFloatAbi && kQuickDoubleRegAlignedFloatBackFilled),
377 "Double alignment unexpected");
Zheng Xu5667fdb2014-10-23 18:29:55 +0800378 // For register alignment, we want to assume that counters(fpr_double_index_) are even if the
379 // next register is even.
Andreas Gampe575e78c2014-11-03 23:41:03 -0800380 static_assert(!kQuickDoubleRegAlignedFloatBackFilled || kNumQuickFprArgs % 2 == 0,
381 "Number of Quick FPR arguments not even");
Andreas Gampe542451c2016-07-26 09:02:02 -0700382 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Zheng Xu5667fdb2014-10-23 18:29:55 +0800383 }
Ian Rogers848871b2013-08-05 10:56:33 -0700384
385 virtual ~QuickArgumentVisitor() {}
386
387 virtual void Visit() = 0;
388
Ian Rogers936b37f2014-02-14 00:52:24 -0800389 Primitive::Type GetParamPrimitiveType() const {
390 return cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700391 }
392
Ian Rogers13735952014-10-08 12:43:28 -0700393 uint8_t* GetParamAddress() const {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800394 if (!kQuickSoftFloatAbi) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800395 Primitive::Type type = GetParamPrimitiveType();
396 if (UNLIKELY((type == Primitive::kPrimDouble) || (type == Primitive::kPrimFloat))) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800397 if (type == Primitive::kPrimDouble && kQuickDoubleRegAlignedFloatBackFilled) {
398 if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) {
399 return fpr_args_ + (fpr_double_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
400 }
401 } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000402 return fpr_args_ + (fpr_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800403 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700404 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers936b37f2014-02-14 00:52:24 -0800405 }
406 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800407 if (gpr_index_ < kNumQuickGprArgs) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800408 return gpr_args_ + GprIndexToGprOffset(gpr_index_);
409 }
410 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700411 }
412
413 bool IsSplitLongOrDouble() const {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700414 if ((GetBytesPerGprSpillLocation(kRuntimeISA) == 4) ||
415 (GetBytesPerFprSpillLocation(kRuntimeISA) == 4)) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800416 return is_split_long_or_double_;
417 } else {
418 return false; // An optimization for when GPR and FPRs are 64bit.
419 }
Ian Rogers848871b2013-08-05 10:56:33 -0700420 }
421
Ian Rogers936b37f2014-02-14 00:52:24 -0800422 bool IsParamAReference() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700423 return GetParamPrimitiveType() == Primitive::kPrimNot;
424 }
425
Ian Rogers936b37f2014-02-14 00:52:24 -0800426 bool IsParamALongOrDouble() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700427 Primitive::Type type = GetParamPrimitiveType();
428 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
429 }
430
431 uint64_t ReadSplitLongParam() const {
Nicolas Geoffray425f2392015-01-08 14:52:29 +0000432 // The splitted long is always available through the stack.
433 return *reinterpret_cast<uint64_t*>(stack_args_
434 + stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700435 }
436
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800437 void IncGprIndex() {
438 gpr_index_++;
439 if (kGprFprLockstep) {
440 fpr_index_++;
441 }
442 }
443
444 void IncFprIndex() {
445 fpr_index_++;
446 if (kGprFprLockstep) {
447 gpr_index_++;
448 }
449 }
450
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700451 void VisitArguments() REQUIRES_SHARED(Locks::mutator_lock_) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800452 // (a) 'stack_args_' should point to the first method's argument
453 // (b) whatever the argument type it is, the 'stack_index_' should
454 // be moved forward along with every visiting.
Ian Rogers936b37f2014-02-14 00:52:24 -0800455 gpr_index_ = 0;
456 fpr_index_ = 0;
Zheng Xu5667fdb2014-10-23 18:29:55 +0800457 if (kQuickDoubleRegAlignedFloatBackFilled) {
458 fpr_double_index_ = 0;
459 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800460 stack_index_ = 0;
461 if (!is_static_) { // Handle this.
462 cur_type_ = Primitive::kPrimNot;
463 is_split_long_or_double_ = false;
Ian Rogers848871b2013-08-05 10:56:33 -0700464 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800465 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800466 if (kNumQuickGprArgs > 0) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800467 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800468 }
Ian Rogers848871b2013-08-05 10:56:33 -0700469 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800470 for (uint32_t shorty_index = 1; shorty_index < shorty_len_; ++shorty_index) {
471 cur_type_ = Primitive::GetType(shorty_[shorty_index]);
472 switch (cur_type_) {
473 case Primitive::kPrimNot:
474 case Primitive::kPrimBoolean:
475 case Primitive::kPrimByte:
476 case Primitive::kPrimChar:
477 case Primitive::kPrimShort:
478 case Primitive::kPrimInt:
479 is_split_long_or_double_ = false;
480 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800481 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800482 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800483 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800484 }
485 break;
486 case Primitive::kPrimFloat:
487 is_split_long_or_double_ = false;
488 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800489 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800490 if (kQuickSoftFloatAbi) {
491 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800492 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800493 }
494 } else {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800495 if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800496 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800497 if (kQuickDoubleRegAlignedFloatBackFilled) {
498 // Double should not overlap with float.
499 // For example, if fpr_index_ = 3, fpr_double_index_ should be at least 4.
500 fpr_double_index_ = std::max(fpr_double_index_, RoundUp(fpr_index_, 2));
501 // Float should not overlap with double.
502 if (fpr_index_ % 2 == 0) {
503 fpr_index_ = std::max(fpr_double_index_, fpr_index_);
504 }
Goran Jakovljevicff734982015-08-24 12:58:55 +0000505 } else if (kQuickSkipOddFpRegisters) {
506 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800507 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800508 }
509 }
510 break;
511 case Primitive::kPrimDouble:
512 case Primitive::kPrimLong:
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800513 if (kQuickSoftFloatAbi || (cur_type_ == Primitive::kPrimLong)) {
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800514 if (cur_type_ == Primitive::kPrimLong &&
515#if defined(__mips__) && !defined(__LP64__)
516 (gpr_index_ == 0 || gpr_index_ == 2) &&
517#else
518 gpr_index_ == 0 &&
519#endif
520 kAlignPairRegister) {
521 // Currently, this is only for ARM and MIPS, where we align long parameters with
522 // even-numbered registers by skipping R1 (on ARM) or A1(A3) (on MIPS) and using
523 // R2 (on ARM) or A2(T0) (on MIPS) instead.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800524 IncGprIndex();
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000525 }
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000526 is_split_long_or_double_ = (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) &&
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800527 ((gpr_index_ + 1) == kNumQuickGprArgs);
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500528 if (!kSplitPairAcrossRegisterAndStack && is_split_long_or_double_) {
529 // We don't want to split this. Pass over this register.
530 gpr_index_++;
531 is_split_long_or_double_ = false;
532 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800533 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800534 if (kBytesStackArgLocation == 4) {
535 stack_index_+= 2;
536 } else {
537 CHECK_EQ(kBytesStackArgLocation, 8U);
538 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800539 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700540 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800541 IncGprIndex();
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000542 if (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700543 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800544 IncGprIndex();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700545 }
546 }
547 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800548 } else {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000549 is_split_long_or_double_ = (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) &&
Zheng Xu5667fdb2014-10-23 18:29:55 +0800550 ((fpr_index_ + 1) == kNumQuickFprArgs) && !kQuickDoubleRegAlignedFloatBackFilled;
Ian Rogers936b37f2014-02-14 00:52:24 -0800551 Visit();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700552 if (kBytesStackArgLocation == 4) {
553 stack_index_+= 2;
Ian Rogers936b37f2014-02-14 00:52:24 -0800554 } else {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700555 CHECK_EQ(kBytesStackArgLocation, 8U);
556 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800557 }
Zheng Xu5667fdb2014-10-23 18:29:55 +0800558 if (kQuickDoubleRegAlignedFloatBackFilled) {
559 if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) {
560 fpr_double_index_ += 2;
561 // Float should not overlap with double.
562 if (fpr_index_ % 2 == 0) {
563 fpr_index_ = std::max(fpr_double_index_, fpr_index_);
564 }
565 }
566 } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800567 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800568 if (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) {
569 if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800570 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800571 }
572 }
573 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800574 }
575 break;
576 default:
577 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty_;
578 }
Ian Rogers848871b2013-08-05 10:56:33 -0700579 }
580 }
581
Andreas Gampec200a4a2014-06-16 18:39:09 -0700582 protected:
Ian Rogers848871b2013-08-05 10:56:33 -0700583 const bool is_static_;
584 const char* const shorty_;
585 const uint32_t shorty_len_;
Andreas Gampec200a4a2014-06-16 18:39:09 -0700586
587 private:
Ian Rogers13735952014-10-08 12:43:28 -0700588 uint8_t* const gpr_args_; // Address of GPR arguments in callee save frame.
589 uint8_t* const fpr_args_; // Address of FPR arguments in callee save frame.
590 uint8_t* const stack_args_; // Address of stack arguments in caller's frame.
Ian Rogers936b37f2014-02-14 00:52:24 -0800591 uint32_t gpr_index_; // Index into spilled GPRs.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800592 // Index into spilled FPRs.
593 // In case kQuickDoubleRegAlignedFloatBackFilled, it may index a hole while fpr_double_index_
594 // holds a higher register number.
595 uint32_t fpr_index_;
596 // Index into spilled FPRs for aligned double.
597 // Only used when kQuickDoubleRegAlignedFloatBackFilled. Next available double register indexed in
598 // terms of singles, may be behind fpr_index.
599 uint32_t fpr_double_index_;
Ian Rogers936b37f2014-02-14 00:52:24 -0800600 uint32_t stack_index_; // Index into arguments on the stack.
601 // The current type of argument during VisitArguments.
602 Primitive::Type cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700603 // Does a 64bit parameter straddle the register and stack arguments?
604 bool is_split_long_or_double_;
605};
606
Sebastien Hertza836bc92014-11-25 16:30:53 +0100607// Returns the 'this' object of a proxy method. This function is only used by StackVisitor. It
608// allows to use the QuickArgumentVisitor constants without moving all the code in its own module.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700609extern "C" mirror::Object* artQuickGetProxyThisObject(ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700610 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertza836bc92014-11-25 16:30:53 +0100611 return QuickArgumentVisitor::GetProxyThisObject(sp);
612}
613
Ian Rogers848871b2013-08-05 10:56:33 -0700614// Visits arguments on the stack placing them into the shadow frame.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800615class BuildQuickShadowFrameVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700616 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700617 BuildQuickShadowFrameVisitor(ArtMethod** sp, bool is_static, const char* shorty,
618 uint32_t shorty_len, ShadowFrame* sf, size_t first_arg_reg) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700619 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), sf_(sf), cur_reg_(first_arg_reg) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700620
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700621 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700622
623 private:
Ian Rogers936b37f2014-02-14 00:52:24 -0800624 ShadowFrame* const sf_;
625 uint32_t cur_reg_;
Ian Rogers848871b2013-08-05 10:56:33 -0700626
Dragos Sbirleabd136a22013-08-13 18:07:04 -0700627 DISALLOW_COPY_AND_ASSIGN(BuildQuickShadowFrameVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700628};
629
Andreas Gampec200a4a2014-06-16 18:39:09 -0700630void BuildQuickShadowFrameVisitor::Visit() {
Ian Rogers9758f792014-03-13 09:02:55 -0700631 Primitive::Type type = GetParamPrimitiveType();
632 switch (type) {
633 case Primitive::kPrimLong: // Fall-through.
634 case Primitive::kPrimDouble:
635 if (IsSplitLongOrDouble()) {
636 sf_->SetVRegLong(cur_reg_, ReadSplitLongParam());
637 } else {
638 sf_->SetVRegLong(cur_reg_, *reinterpret_cast<jlong*>(GetParamAddress()));
639 }
640 ++cur_reg_;
641 break;
642 case Primitive::kPrimNot: {
643 StackReference<mirror::Object>* stack_ref =
644 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
645 sf_->SetVRegReference(cur_reg_, stack_ref->AsMirrorPtr());
646 }
647 break;
648 case Primitive::kPrimBoolean: // Fall-through.
649 case Primitive::kPrimByte: // Fall-through.
650 case Primitive::kPrimChar: // Fall-through.
651 case Primitive::kPrimShort: // Fall-through.
652 case Primitive::kPrimInt: // Fall-through.
653 case Primitive::kPrimFloat:
654 sf_->SetVReg(cur_reg_, *reinterpret_cast<jint*>(GetParamAddress()));
655 break;
656 case Primitive::kPrimVoid:
657 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700658 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -0700659 }
660 ++cur_reg_;
661}
662
Mathieu Chartiere401d142015-04-22 13:56:20 -0700663extern "C" uint64_t artQuickToInterpreterBridge(ArtMethod* method, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700664 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers848871b2013-08-05 10:56:33 -0700665 // Ensure we don't get thread suspension until the object arguments are safely in the shadow
666 // frame.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700667 ScopedQuickEntrypointChecks sqec(self);
Ian Rogers848871b2013-08-05 10:56:33 -0700668
Alex Light9139e002015-10-09 15:59:48 -0700669 if (UNLIKELY(!method->IsInvokable())) {
670 method->ThrowInvocationTimeError();
Ian Rogers848871b2013-08-05 10:56:33 -0700671 return 0;
Andreas Gampe639bdd12015-06-03 11:22:45 -0700672 }
673
674 JValue tmp_value;
675 ShadowFrame* deopt_frame = self->PopStackedShadowFrame(
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700676 StackedShadowFrameType::kDeoptimizationShadowFrame, false);
Andreas Gampe639bdd12015-06-03 11:22:45 -0700677 ManagedStack fragment;
678
David Sehr709b0702016-10-13 09:12:37 -0700679 DCHECK(!method->IsNative()) << method->PrettyMethod();
Andreas Gampe639bdd12015-06-03 11:22:45 -0700680 uint32_t shorty_len = 0;
Andreas Gampe542451c2016-07-26 09:02:02 -0700681 ArtMethod* non_proxy_method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Alex Lighte9dd04f2016-03-16 16:09:45 -0700682 const DexFile::CodeItem* code_item = non_proxy_method->GetCodeItem();
David Sehr709b0702016-10-13 09:12:37 -0700683 DCHECK(code_item != nullptr) << method->PrettyMethod();
Andreas Gampe639bdd12015-06-03 11:22:45 -0700684 const char* shorty = non_proxy_method->GetShorty(&shorty_len);
685
686 JValue result;
687
688 if (deopt_frame != nullptr) {
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700689 // Coming from partial-fragment deopt.
Andreas Gampe639bdd12015-06-03 11:22:45 -0700690
691 if (kIsDebugBuild) {
692 // Sanity-check: are the methods as expected? We check that the last shadow frame (the bottom
693 // of the call-stack) corresponds to the called method.
694 ShadowFrame* linked = deopt_frame;
695 while (linked->GetLink() != nullptr) {
696 linked = linked->GetLink();
697 }
David Sehr709b0702016-10-13 09:12:37 -0700698 CHECK_EQ(method, linked->GetMethod()) << method->PrettyMethod() << " "
699 << ArtMethod::PrettyMethod(linked->GetMethod());
Andreas Gampe639bdd12015-06-03 11:22:45 -0700700 }
701
702 if (VLOG_IS_ON(deopt)) {
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700703 // Print out the stack to verify that it was a partial-fragment deopt.
Andreas Gampe639bdd12015-06-03 11:22:45 -0700704 LOG(INFO) << "Continue-ing from deopt. Stack is:";
705 QuickExceptionHandler::DumpFramesWithType(self, true);
706 }
707
Mathieu Chartierf5769e12017-01-10 15:54:41 -0800708 ObjPtr<mirror::Throwable> pending_exception;
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100709 bool from_code = false;
710 self->PopDeoptimizationContext(&result, &pending_exception, /* out */ &from_code);
Andreas Gampe639bdd12015-06-03 11:22:45 -0700711
712 // Push a transition back into managed code onto the linked list in thread.
713 self->PushManagedStackFragment(&fragment);
714
715 // Ensure that the stack is still in order.
716 if (kIsDebugBuild) {
717 class DummyStackVisitor : public StackVisitor {
718 public:
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700719 explicit DummyStackVisitor(Thread* self_in) REQUIRES_SHARED(Locks::mutator_lock_)
Andreas Gampe639bdd12015-06-03 11:22:45 -0700720 : StackVisitor(self_in, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {}
721
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700722 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe639bdd12015-06-03 11:22:45 -0700723 // Nothing to do here. In a debug build, SanityCheckFrame will do the work in the walking
724 // logic. Just always say we want to continue.
725 return true;
726 }
727 };
728 DummyStackVisitor dsv(self);
729 dsv.WalkStack();
730 }
731
732 // Restore the exception that was pending before deoptimization then interpret the
733 // deoptimized frames.
734 if (pending_exception != nullptr) {
735 self->SetException(pending_exception);
736 }
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100737 interpreter::EnterInterpreterFromDeoptimize(self, deopt_frame, from_code, &result);
Ian Rogers848871b2013-08-05 10:56:33 -0700738 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -0700739 const char* old_cause = self->StartAssertNoThreadSuspension(
740 "Building interpreter shadow frame");
Ian Rogers848871b2013-08-05 10:56:33 -0700741 uint16_t num_regs = code_item->registers_size_;
Andreas Gampec200a4a2014-06-16 18:39:09 -0700742 // No last shadow coming from quick.
Andreas Gampeb3025922015-09-01 14:45:00 -0700743 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
Andreas Gampe03ec9302015-08-27 17:41:47 -0700744 CREATE_SHADOW_FRAME(num_regs, /* link */ nullptr, method, /* dex pc */ 0);
Andreas Gampeb3025922015-09-01 14:45:00 -0700745 ShadowFrame* shadow_frame = shadow_frame_unique_ptr.get();
Ian Rogers848871b2013-08-05 10:56:33 -0700746 size_t first_arg_reg = code_item->registers_size_ - code_item->ins_size_;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700747 BuildQuickShadowFrameVisitor shadow_frame_builder(sp, method->IsStatic(), shorty, shorty_len,
Ian Rogers936b37f2014-02-14 00:52:24 -0800748 shadow_frame, first_arg_reg);
Ian Rogers848871b2013-08-05 10:56:33 -0700749 shadow_frame_builder.VisitArguments();
Ian Rogerse94652f2014-12-02 11:13:19 -0800750 const bool needs_initialization =
751 method->IsStatic() && !method->GetDeclaringClass()->IsInitialized();
Ian Rogers848871b2013-08-05 10:56:33 -0700752 // Push a transition back into managed code onto the linked list in thread.
Ian Rogers848871b2013-08-05 10:56:33 -0700753 self->PushManagedStackFragment(&fragment);
754 self->PushShadowFrame(shadow_frame);
755 self->EndAssertNoThreadSuspension(old_cause);
756
Ian Rogerse94652f2014-12-02 11:13:19 -0800757 if (needs_initialization) {
Ian Rogers848871b2013-08-05 10:56:33 -0700758 // Ensure static method's class is initialized.
Ian Rogerse94652f2014-12-02 11:13:19 -0800759 StackHandleScope<1> hs(self);
760 Handle<mirror::Class> h_class(hs.NewHandle(shadow_frame->GetMethod()->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700761 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
David Sehr709b0702016-10-13 09:12:37 -0700762 DCHECK(Thread::Current()->IsExceptionPending())
763 << shadow_frame->GetMethod()->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700764 self->PopManagedStackFragment(fragment);
765 return 0;
766 }
767 }
Daniel Mihalyieb076692014-08-22 17:33:31 +0200768
Andreas Gampe639bdd12015-06-03 11:22:45 -0700769 result = interpreter::EnterInterpreterFromEntryPoint(self, code_item, shadow_frame);
Ian Rogers848871b2013-08-05 10:56:33 -0700770 }
Andreas Gampe639bdd12015-06-03 11:22:45 -0700771
772 // Pop transition.
773 self->PopManagedStackFragment(fragment);
774
775 // Request a stack deoptimization if needed
776 ArtMethod* caller = QuickArgumentVisitor::GetCallingMethod(sp);
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700777 uintptr_t caller_pc = QuickArgumentVisitor::GetCallingPc(sp);
Mingyao Yanga3549d22016-06-02 17:01:02 -0700778 // If caller_pc is the instrumentation exit stub, the stub will check to see if deoptimization
779 // should be done and it knows the real return pc.
780 if (UNLIKELY(caller_pc != reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) &&
781 Dbg::IsForcedInterpreterNeededForUpcall(self, caller) &&
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700782 Runtime::Current()->IsDeoptimizeable(caller_pc))) {
Andreas Gampe639bdd12015-06-03 11:22:45 -0700783 // Push the context of the deoptimization stack so we can restore the return value and the
784 // exception before executing the deoptimized frames.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100785 self->PushDeoptimizationContext(
786 result, shorty[0] == 'L', /* from_code */ false, self->GetException());
Andreas Gampe639bdd12015-06-03 11:22:45 -0700787
788 // Set special exception to cause deoptimization.
789 self->SetException(Thread::GetDeoptimizationException());
790 }
791
792 // No need to restore the args since the method has already been run by the interpreter.
793 return result.GetJ();
Ian Rogers848871b2013-08-05 10:56:33 -0700794}
795
796// Visits arguments on the stack placing them into the args vector, Object* arguments are converted
797// to jobjects.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800798class BuildQuickArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700799 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700800 BuildQuickArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty, uint32_t shorty_len,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700801 ScopedObjectAccessUnchecked* soa, std::vector<jvalue>* args) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700802 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa), args_(args) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700803
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700804 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700805
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700806 void FixupReferences() REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800807
Ian Rogers848871b2013-08-05 10:56:33 -0700808 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700809 ScopedObjectAccessUnchecked* const soa_;
810 std::vector<jvalue>* const args_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800811 // References which we must update when exiting in case the GC moved the objects.
Ian Rogers700a4022014-05-19 16:49:03 -0700812 std::vector<std::pair<jobject, StackReference<mirror::Object>*>> references_;
Ian Rogers9758f792014-03-13 09:02:55 -0700813
Ian Rogers848871b2013-08-05 10:56:33 -0700814 DISALLOW_COPY_AND_ASSIGN(BuildQuickArgumentVisitor);
815};
816
Ian Rogers9758f792014-03-13 09:02:55 -0700817void BuildQuickArgumentVisitor::Visit() {
818 jvalue val;
819 Primitive::Type type = GetParamPrimitiveType();
820 switch (type) {
821 case Primitive::kPrimNot: {
822 StackReference<mirror::Object>* stack_ref =
823 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
824 val.l = soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
825 references_.push_back(std::make_pair(val.l, stack_ref));
826 break;
827 }
828 case Primitive::kPrimLong: // Fall-through.
829 case Primitive::kPrimDouble:
830 if (IsSplitLongOrDouble()) {
831 val.j = ReadSplitLongParam();
832 } else {
833 val.j = *reinterpret_cast<jlong*>(GetParamAddress());
834 }
835 break;
836 case Primitive::kPrimBoolean: // Fall-through.
837 case Primitive::kPrimByte: // Fall-through.
838 case Primitive::kPrimChar: // Fall-through.
839 case Primitive::kPrimShort: // Fall-through.
840 case Primitive::kPrimInt: // Fall-through.
841 case Primitive::kPrimFloat:
842 val.i = *reinterpret_cast<jint*>(GetParamAddress());
843 break;
844 case Primitive::kPrimVoid:
845 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700846 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -0700847 }
848 args_->push_back(val);
849}
850
851void BuildQuickArgumentVisitor::FixupReferences() {
852 // Fixup any references which may have changed.
853 for (const auto& pair : references_) {
Mathieu Chartier1a5337f2016-10-13 13:48:23 -0700854 pair.second->Assign(soa_->Decode<mirror::Object>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -0700855 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -0700856 }
857}
858
Ian Rogers848871b2013-08-05 10:56:33 -0700859// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
860// which is responsible for recording callee save registers. We explicitly place into jobjects the
861// incoming reference arguments (so they survive GC). We invoke the invocation handler, which is a
862// field within the proxy object, which will box the primitive arguments and deal with error cases.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700863extern "C" uint64_t artQuickProxyInvokeHandler(
864 ArtMethod* proxy_method, mirror::Object* receiver, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700865 REQUIRES_SHARED(Locks::mutator_lock_) {
David Sehr709b0702016-10-13 09:12:37 -0700866 DCHECK(proxy_method->IsProxyMethod()) << proxy_method->PrettyMethod();
867 DCHECK(receiver->GetClass()->IsProxyClass()) << proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700868 // Ensure we don't get thread suspension until the object arguments are safely in jobjects.
869 const char* old_cause =
870 self->StartAssertNoThreadSuspension("Adding to IRT proxy object arguments");
871 // Register the top of the managed stack, making stack crawlable.
David Sehr709b0702016-10-13 09:12:37 -0700872 DCHECK_EQ((*sp), proxy_method) << proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700873 self->VerifyStack();
874 // Start new JNI local reference state.
875 JNIEnvExt* env = self->GetJniEnv();
876 ScopedObjectAccessUnchecked soa(env);
877 ScopedJniEnvLocalRefState env_state(env);
878 // Create local ref. copies of proxy method and the receiver.
879 jobject rcvr_jobj = soa.AddLocalReference<jobject>(receiver);
880
881 // Placing arguments into args vector and remove the receiver.
Andreas Gampe542451c2016-07-26 09:02:02 -0700882 ArtMethod* non_proxy_method = proxy_method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
David Sehr709b0702016-10-13 09:12:37 -0700883 CHECK(!non_proxy_method->IsStatic()) << proxy_method->PrettyMethod() << " "
884 << non_proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700885 std::vector<jvalue> args;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700886 uint32_t shorty_len = 0;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700887 const char* shorty = non_proxy_method->GetShorty(&shorty_len);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700888 BuildQuickArgumentVisitor local_ref_visitor(sp, false, shorty, shorty_len, &soa, &args);
Brian Carlstromd3633d52013-08-20 21:06:26 -0700889
Ian Rogers848871b2013-08-05 10:56:33 -0700890 local_ref_visitor.VisitArguments();
David Sehr709b0702016-10-13 09:12:37 -0700891 DCHECK_GT(args.size(), 0U) << proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700892 args.erase(args.begin());
893
894 // Convert proxy method into expected interface method.
Andreas Gampe542451c2016-07-26 09:02:02 -0700895 ArtMethod* interface_method = proxy_method->FindOverriddenMethod(kRuntimePointerSize);
David Sehr709b0702016-10-13 09:12:37 -0700896 DCHECK(interface_method != nullptr) << proxy_method->PrettyMethod();
897 DCHECK(!interface_method->IsProxyMethod()) << interface_method->PrettyMethod();
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700898 self->EndAssertNoThreadSuspension(old_cause);
Andreas Gampe542451c2016-07-26 09:02:02 -0700899 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Andreas Gampee01e3642016-07-25 13:06:04 -0700900 DCHECK(!Runtime::Current()->IsActiveTransaction());
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700901 jobject interface_method_jobj = soa.AddLocalReference<jobject>(
Andreas Gampe542451c2016-07-26 09:02:02 -0700902 mirror::Method::CreateFromArtMethod<kRuntimePointerSize, false>(soa.Self(),
903 interface_method));
Ian Rogers848871b2013-08-05 10:56:33 -0700904
905 // All naked Object*s should now be in jobjects, so its safe to go into the main invoke code
906 // that performs allocations.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700907 JValue result = InvokeProxyInvocationHandler(soa, shorty, rcvr_jobj, interface_method_jobj, args);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800908 // Restore references which might have moved.
909 local_ref_visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -0700910 return result.GetJ();
911}
912
913// Read object references held in arguments from quick frames and place in a JNI local references,
914// so they don't get garbage collected.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800915class RememberForGcArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700916 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700917 RememberForGcArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty,
918 uint32_t shorty_len, ScopedObjectAccessUnchecked* soa) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700919 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700920
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700921 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Mathieu Chartier07d447b2013-09-26 11:57:43 -0700922
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700923 void FixupReferences() REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers848871b2013-08-05 10:56:33 -0700924
925 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700926 ScopedObjectAccessUnchecked* const soa_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800927 // References which we must update when exiting in case the GC moved the objects.
Andreas Gampec200a4a2014-06-16 18:39:09 -0700928 std::vector<std::pair<jobject, StackReference<mirror::Object>*> > references_;
929
Mathieu Chartier590fee92013-09-13 13:46:47 -0700930 DISALLOW_COPY_AND_ASSIGN(RememberForGcArgumentVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700931};
932
Ian Rogers9758f792014-03-13 09:02:55 -0700933void RememberForGcArgumentVisitor::Visit() {
934 if (IsParamAReference()) {
935 StackReference<mirror::Object>* stack_ref =
936 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
937 jobject reference =
938 soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
939 references_.push_back(std::make_pair(reference, stack_ref));
940 }
941}
942
943void RememberForGcArgumentVisitor::FixupReferences() {
944 // Fixup any references which may have changed.
945 for (const auto& pair : references_) {
Mathieu Chartier1a5337f2016-10-13 13:48:23 -0700946 pair.second->Assign(soa_->Decode<mirror::Object>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -0700947 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -0700948 }
949}
950
Ian Rogers848871b2013-08-05 10:56:33 -0700951// Lazily resolve a method for quick. Called by stub code.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700952extern "C" const void* artQuickResolutionTrampoline(
953 ArtMethod* called, mirror::Object* receiver, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700954 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe3b45ef22015-05-26 21:34:09 -0700955 // The resolution trampoline stashes the resolved method into the callee-save frame to transport
956 // it. Thus, when exiting, the stack cannot be verified (as the resolved method most likely
957 // does not have the same stack layout as the callee-save method).
958 ScopedQuickEntrypointChecks sqec(self, kIsDebugBuild, false);
Ian Rogers848871b2013-08-05 10:56:33 -0700959 // Start new JNI local reference state
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800960 JNIEnvExt* env = self->GetJniEnv();
Ian Rogers848871b2013-08-05 10:56:33 -0700961 ScopedObjectAccessUnchecked soa(env);
962 ScopedJniEnvLocalRefState env_state(env);
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800963 const char* old_cause = self->StartAssertNoThreadSuspension("Quick method resolution set up");
Ian Rogers848871b2013-08-05 10:56:33 -0700964
965 // Compute details about the called method (avoid GCs)
966 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Ian Rogers848871b2013-08-05 10:56:33 -0700967 InvokeType invoke_type;
Ian Rogerse0a02da2014-12-02 14:10:53 -0800968 MethodReference called_method(nullptr, 0);
969 const bool called_method_known_on_entry = !called->IsRuntimeMethod();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700970 ArtMethod* caller = nullptr;
Ian Rogerse0a02da2014-12-02 14:10:53 -0800971 if (!called_method_known_on_entry) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +0100972 caller = QuickArgumentVisitor::GetCallingMethod(sp);
973 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
Ian Rogers848871b2013-08-05 10:56:33 -0700974 const DexFile::CodeItem* code;
Ian Rogerse0a02da2014-12-02 14:10:53 -0800975 called_method.dex_file = caller->GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700976 code = caller->GetCodeItem();
Ian Rogers848871b2013-08-05 10:56:33 -0700977 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
978 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
979 Instruction::Code instr_code = instr->Opcode();
980 bool is_range;
981 switch (instr_code) {
982 case Instruction::INVOKE_DIRECT:
983 invoke_type = kDirect;
984 is_range = false;
985 break;
986 case Instruction::INVOKE_DIRECT_RANGE:
987 invoke_type = kDirect;
988 is_range = true;
989 break;
990 case Instruction::INVOKE_STATIC:
991 invoke_type = kStatic;
992 is_range = false;
993 break;
994 case Instruction::INVOKE_STATIC_RANGE:
995 invoke_type = kStatic;
996 is_range = true;
997 break;
998 case Instruction::INVOKE_SUPER:
999 invoke_type = kSuper;
1000 is_range = false;
1001 break;
1002 case Instruction::INVOKE_SUPER_RANGE:
1003 invoke_type = kSuper;
1004 is_range = true;
1005 break;
1006 case Instruction::INVOKE_VIRTUAL:
1007 invoke_type = kVirtual;
1008 is_range = false;
1009 break;
1010 case Instruction::INVOKE_VIRTUAL_RANGE:
1011 invoke_type = kVirtual;
1012 is_range = true;
1013 break;
1014 case Instruction::INVOKE_INTERFACE:
1015 invoke_type = kInterface;
1016 is_range = false;
1017 break;
1018 case Instruction::INVOKE_INTERFACE_RANGE:
1019 invoke_type = kInterface;
1020 is_range = true;
1021 break;
1022 default:
Ian Rogerse0a02da2014-12-02 14:10:53 -08001023 LOG(FATAL) << "Unexpected call into trampoline: " << instr->DumpString(nullptr);
1024 UNREACHABLE();
Ian Rogers848871b2013-08-05 10:56:33 -07001025 }
Ian Rogerse0a02da2014-12-02 14:10:53 -08001026 called_method.dex_method_index = (is_range) ? instr->VRegB_3rc() : instr->VRegB_35c();
Ian Rogers848871b2013-08-05 10:56:33 -07001027 } else {
1028 invoke_type = kStatic;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001029 called_method.dex_file = called->GetDexFile();
1030 called_method.dex_method_index = called->GetDexMethodIndex();
Ian Rogers848871b2013-08-05 10:56:33 -07001031 }
1032 uint32_t shorty_len;
1033 const char* shorty =
Ian Rogerse0a02da2014-12-02 14:10:53 -08001034 called_method.dex_file->GetMethodShorty(
1035 called_method.dex_file->GetMethodId(called_method.dex_method_index), &shorty_len);
Mathieu Chartier590fee92013-09-13 13:46:47 -07001036 RememberForGcArgumentVisitor visitor(sp, invoke_type == kStatic, shorty, shorty_len, &soa);
Ian Rogers848871b2013-08-05 10:56:33 -07001037 visitor.VisitArguments();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001038 self->EndAssertNoThreadSuspension(old_cause);
Ian Rogerse0a02da2014-12-02 14:10:53 -08001039 const bool virtual_or_interface = invoke_type == kVirtual || invoke_type == kInterface;
Ian Rogers848871b2013-08-05 10:56:33 -07001040 // Resolve method filling in dex cache.
Ian Rogerse0a02da2014-12-02 14:10:53 -08001041 if (!called_method_known_on_entry) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001042 StackHandleScope<1> hs(self);
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001043 mirror::Object* dummy = nullptr;
1044 HandleWrapper<mirror::Object> h_receiver(
1045 hs.NewHandleWrapper(virtual_or_interface ? &receiver : &dummy));
Ian Rogerse0a02da2014-12-02 14:10:53 -08001046 DCHECK_EQ(caller->GetDexFile(), called_method.dex_file);
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08001047 called = linker->ResolveMethod<ClassLinker::kForceICCECheck>(
1048 self, called_method.dex_method_index, caller, invoke_type);
Ian Rogers848871b2013-08-05 10:56:33 -07001049 }
Ian Rogerse0a02da2014-12-02 14:10:53 -08001050 const void* code = nullptr;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001051 if (LIKELY(!self->IsExceptionPending())) {
Ian Rogers848871b2013-08-05 10:56:33 -07001052 // Incompatible class change should have been handled in resolve method.
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001053 CHECK(!called->CheckIncompatibleClassChange(invoke_type))
David Sehr709b0702016-10-13 09:12:37 -07001054 << called->PrettyMethod() << " " << invoke_type;
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001055 if (virtual_or_interface || invoke_type == kSuper) {
1056 // Refine called method based on receiver for kVirtual/kInterface, and
1057 // caller for kSuper.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001058 ArtMethod* orig_called = called;
Mathieu Chartier55871bf2014-02-27 10:24:50 -08001059 if (invoke_type == kVirtual) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001060 CHECK(receiver != nullptr) << invoke_type;
Andreas Gampe542451c2016-07-26 09:02:02 -07001061 called = receiver->GetClass()->FindVirtualMethodForVirtual(called, kRuntimePointerSize);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001062 } else if (invoke_type == kInterface) {
1063 CHECK(receiver != nullptr) << invoke_type;
Andreas Gampe542451c2016-07-26 09:02:02 -07001064 called = receiver->GetClass()->FindVirtualMethodForInterface(called, kRuntimePointerSize);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001065 } else {
1066 DCHECK_EQ(invoke_type, kSuper);
1067 CHECK(caller != nullptr) << invoke_type;
Nicolas Geoffray393fdb82016-04-25 14:58:06 +01001068 StackHandleScope<2> hs(self);
1069 Handle<mirror::DexCache> dex_cache(
1070 hs.NewHandle(caller->GetDeclaringClass()->GetDexCache()));
1071 Handle<mirror::ClassLoader> class_loader(
1072 hs.NewHandle(caller->GetDeclaringClass()->GetClassLoader()));
Alex Lightfedd91d2016-01-07 14:49:16 -08001073 // TODO Maybe put this into a mirror::Class function.
1074 mirror::Class* ref_class = linker->ResolveReferencedClassOfMethod(
Nicolas Geoffray393fdb82016-04-25 14:58:06 +01001075 called_method.dex_method_index, dex_cache, class_loader);
Alex Lightfedd91d2016-01-07 14:49:16 -08001076 if (ref_class->IsInterface()) {
Andreas Gampe542451c2016-07-26 09:02:02 -07001077 called = ref_class->FindVirtualMethodForInterfaceSuper(called, kRuntimePointerSize);
Alex Lightfedd91d2016-01-07 14:49:16 -08001078 } else {
1079 called = caller->GetDeclaringClass()->GetSuperClass()->GetVTableEntry(
Andreas Gampe542451c2016-07-26 09:02:02 -07001080 called->GetMethodIndex(), kRuntimePointerSize);
Alex Lightfedd91d2016-01-07 14:49:16 -08001081 }
Mathieu Chartier55871bf2014-02-27 10:24:50 -08001082 }
Mingyao Yangf4867782014-05-05 11:55:02 -07001083
David Sehr709b0702016-10-13 09:12:37 -07001084 CHECK(called != nullptr) << orig_called->PrettyMethod() << " "
1085 << mirror::Object::PrettyTypeOf(receiver) << " "
Mingyao Yangf4867782014-05-05 11:55:02 -07001086 << invoke_type << " " << orig_called->GetVtableIndex();
1087
Ian Rogers83883d72013-10-21 21:07:24 -07001088 // 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 -08001089 // of the sharpened method avoiding dirtying the dex cache if possible.
Ian Rogers00f15272014-12-02 16:55:46 -08001090 // Note, called_method.dex_method_index references the dex method before the
1091 // FindVirtualMethodFor... This is ok for FindDexMethodIndexInOtherDexFile that only cares
1092 // about the name and signature.
1093 uint32_t update_dex_cache_method_index = called->GetDexMethodIndex();
Andreas Gampe542451c2016-07-26 09:02:02 -07001094 if (!called->HasSameDexCacheResolvedMethods(caller, kRuntimePointerSize)) {
Ian Rogers83883d72013-10-21 21:07:24 -07001095 // Calling from one dex file to another, need to compute the method index appropriate to
Vladimir Markobbcc0c02014-02-03 14:08:42 +00001096 // the caller's dex file. Since we get here only if the original called was a runtime
1097 // method, we've got the correct dex_file and a dex_method_idx from above.
Ian Rogerse0a02da2014-12-02 14:10:53 -08001098 DCHECK(!called_method_known_on_entry);
1099 DCHECK_EQ(caller->GetDexFile(), called_method.dex_file);
1100 const DexFile* caller_dex_file = called_method.dex_file;
1101 uint32_t caller_method_name_and_sig_index = called_method.dex_method_index;
1102 update_dex_cache_method_index =
1103 called->FindDexMethodIndexInOtherDexFile(*caller_dex_file,
1104 caller_method_name_and_sig_index);
1105 }
1106 if ((update_dex_cache_method_index != DexFile::kDexNoIndex) &&
Mathieu Chartiere401d142015-04-22 13:56:20 -07001107 (caller->GetDexCacheResolvedMethod(
Andreas Gampe542451c2016-07-26 09:02:02 -07001108 update_dex_cache_method_index, kRuntimePointerSize) != called)) {
1109 caller->SetDexCacheResolvedMethod(update_dex_cache_method_index,
1110 called,
1111 kRuntimePointerSize);
Ian Rogers83883d72013-10-21 21:07:24 -07001112 }
Mathieu Chartiere4a91bb2015-01-28 13:11:44 -08001113 } else if (invoke_type == kStatic) {
1114 const auto called_dex_method_idx = called->GetDexMethodIndex();
1115 // For static invokes, we may dispatch to the static method in the superclass but resolve
1116 // using the subclass. To prevent getting slow paths on each invoke, we force set the
1117 // resolved method for the super class dex method index if we are in the same dex file.
1118 // b/19175856
1119 if (called->GetDexFile() == called_method.dex_file &&
1120 called_method.dex_method_index != called_dex_method_idx) {
Andreas Gampe542451c2016-07-26 09:02:02 -07001121 called->GetDexCache()->SetResolvedMethod(called_dex_method_idx,
1122 called,
1123 kRuntimePointerSize);
Mathieu Chartiere4a91bb2015-01-28 13:11:44 -08001124 }
Ian Rogers83883d72013-10-21 21:07:24 -07001125 }
Daniel Mihalyieb076692014-08-22 17:33:31 +02001126
Ian Rogers848871b2013-08-05 10:56:33 -07001127 // Ensure that the called method's class is initialized.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001128 StackHandleScope<1> hs(soa.Self());
1129 Handle<mirror::Class> called_class(hs.NewHandle(called->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -07001130 linker->EnsureInitialized(soa.Self(), called_class, true, true);
Ian Rogers848871b2013-08-05 10:56:33 -07001131 if (LIKELY(called_class->IsInitialized())) {
Daniel Mihalyieb076692014-08-22 17:33:31 +02001132 if (UNLIKELY(Dbg::IsForcedInterpreterNeededForResolution(self, called))) {
1133 // If we are single-stepping or the called method is deoptimized (by a
1134 // breakpoint, for example), then we have to execute the called method
1135 // with the interpreter.
1136 code = GetQuickToInterpreterBridge();
1137 } else if (UNLIKELY(Dbg::IsForcedInstrumentationNeededForResolution(self, caller))) {
1138 // If the caller is deoptimized (by a breakpoint, for example), we have to
1139 // continue its execution with interpreter when returning from the called
1140 // method. Because we do not want to execute the called method with the
1141 // interpreter, we wrap its execution into the instrumentation stubs.
1142 // When the called method returns, it will execute the instrumentation
1143 // exit hook that will determine the need of the interpreter with a call
1144 // to Dbg::IsForcedInterpreterNeededForUpcall and deoptimize the stack if
1145 // it is needed.
1146 code = GetQuickInstrumentationEntryPoint();
1147 } else {
1148 code = called->GetEntryPointFromQuickCompiledCode();
1149 }
Ian Rogers848871b2013-08-05 10:56:33 -07001150 } else if (called_class->IsInitializing()) {
Daniel Mihalyieb076692014-08-22 17:33:31 +02001151 if (UNLIKELY(Dbg::IsForcedInterpreterNeededForResolution(self, called))) {
1152 // If we are single-stepping or the called method is deoptimized (by a
1153 // breakpoint, for example), then we have to execute the called method
1154 // with the interpreter.
1155 code = GetQuickToInterpreterBridge();
1156 } else if (invoke_type == kStatic) {
Ian Rogers848871b2013-08-05 10:56:33 -07001157 // Class is still initializing, go to oat and grab code (trampoline must be left in place
1158 // until class is initialized to stop races between threads).
Ian Rogersef7d42f2014-01-06 12:55:46 -08001159 code = linker->GetQuickOatCodeFor(called);
Ian Rogers848871b2013-08-05 10:56:33 -07001160 } else {
1161 // No trampoline for non-static methods.
Ian Rogersef7d42f2014-01-06 12:55:46 -08001162 code = called->GetEntryPointFromQuickCompiledCode();
Ian Rogers848871b2013-08-05 10:56:33 -07001163 }
1164 } else {
1165 DCHECK(called_class->IsErroneous());
1166 }
1167 }
Ian Rogerse0a02da2014-12-02 14:10:53 -08001168 CHECK_EQ(code == nullptr, self->IsExceptionPending());
Mathieu Chartier07d447b2013-09-26 11:57:43 -07001169 // Fixup any locally saved objects may have moved during a GC.
1170 visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -07001171 // Place called method in callee-save frame to be placed as first argument to quick method.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001172 *sp = called;
1173
Ian Rogers848871b2013-08-05 10:56:33 -07001174 return code;
1175}
1176
Andreas Gampec147b002014-03-06 18:11:06 -08001177/*
1178 * This class uses a couple of observations to unite the different calling conventions through
1179 * a few constants.
1180 *
1181 * 1) Number of registers used for passing is normally even, so counting down has no penalty for
1182 * possible alignment.
1183 * 2) Known 64b architectures store 8B units on the stack, both for integral and floating point
1184 * types, so using uintptr_t is OK. Also means that we can use kRegistersNeededX to denote
1185 * when we have to split things
1186 * 3) The only soft-float, Arm, is 32b, so no widening needs to be taken into account for floats
1187 * and we can use Int handling directly.
1188 * 4) Only 64b architectures widen, and their stack is aligned 8B anyways, so no padding code
1189 * necessary when widening. Also, widening of Ints will take place implicitly, and the
1190 * extension should be compatible with Aarch64, which mandates copying the available bits
1191 * into LSB and leaving the rest unspecified.
1192 * 5) Aligning longs and doubles is necessary on arm only, and it's the same in registers and on
1193 * the stack.
1194 * 6) There is only little endian.
1195 *
1196 *
1197 * Actual work is supposed to be done in a delegate of the template type. The interface is as
1198 * follows:
1199 *
1200 * void PushGpr(uintptr_t): Add a value for the next GPR
1201 *
1202 * void PushFpr4(float): Add a value for the next FPR of size 32b. Is only called if we need
1203 * padding, that is, think the architecture is 32b and aligns 64b.
1204 *
1205 * void PushFpr8(uint64_t): Push a double. We _will_ call this on 32b, it's the callee's job to
1206 * split this if necessary. The current state will have aligned, if
1207 * necessary.
1208 *
1209 * void PushStack(uintptr_t): Push a value to the stack.
1210 *
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001211 * uintptr_t PushHandleScope(mirror::Object* ref): Add a reference to the HandleScope. This _will_ have nullptr,
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001212 * as this might be important for null initialization.
Andreas Gampec147b002014-03-06 18:11:06 -08001213 * Must return the jobject, that is, the reference to the
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001214 * entry in the HandleScope (nullptr if necessary).
Andreas Gampec147b002014-03-06 18:11:06 -08001215 *
1216 */
Andreas Gampec200a4a2014-06-16 18:39:09 -07001217template<class T> class BuildNativeCallFrameStateMachine {
Andreas Gampec147b002014-03-06 18:11:06 -08001218 public:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001219#if defined(__arm__)
1220 // TODO: These are all dummy values!
Andreas Gampec147b002014-03-06 18:11:06 -08001221 static constexpr bool kNativeSoftFloatAbi = true;
1222 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs, r0-r3
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001223 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
1224
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001225 static constexpr size_t kRegistersNeededForLong = 2;
1226 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -08001227 static constexpr bool kMultiRegistersAligned = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001228 static constexpr bool kMultiFPRegistersWidened = false;
1229 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001230 static constexpr bool kAlignLongOnStack = true;
1231 static constexpr bool kAlignDoubleOnStack = true;
Stuart Monteithb95a5342014-03-12 13:32:32 +00001232#elif defined(__aarch64__)
1233 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
1234 static constexpr size_t kNumNativeGprArgs = 8; // 6 arguments passed in GPRs.
1235 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
1236
1237 static constexpr size_t kRegistersNeededForLong = 1;
1238 static constexpr size_t kRegistersNeededForDouble = 1;
1239 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001240 static constexpr bool kMultiFPRegistersWidened = false;
1241 static constexpr bool kMultiGPRegistersWidened = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +00001242 static constexpr bool kAlignLongOnStack = false;
1243 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001244#elif defined(__mips__) && !defined(__LP64__)
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001245 static constexpr bool kNativeSoftFloatAbi = true; // This is a hard float ABI.
Douglas Leung735b8552014-10-31 12:21:40 -07001246 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs.
1247 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001248
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001249 static constexpr size_t kRegistersNeededForLong = 2;
1250 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -08001251 static constexpr bool kMultiRegistersAligned = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001252 static constexpr bool kMultiFPRegistersWidened = true;
1253 static constexpr bool kMultiGPRegistersWidened = false;
Douglas Leung735b8552014-10-31 12:21:40 -07001254 static constexpr bool kAlignLongOnStack = true;
1255 static constexpr bool kAlignDoubleOnStack = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001256#elif defined(__mips__) && defined(__LP64__)
1257 // Let the code prepare GPRs only and we will load the FPRs with same data.
1258 static constexpr bool kNativeSoftFloatAbi = true;
1259 static constexpr size_t kNumNativeGprArgs = 8;
1260 static constexpr size_t kNumNativeFprArgs = 0;
1261
1262 static constexpr size_t kRegistersNeededForLong = 1;
1263 static constexpr size_t kRegistersNeededForDouble = 1;
1264 static constexpr bool kMultiRegistersAligned = false;
1265 static constexpr bool kMultiFPRegistersWidened = false;
1266 static constexpr bool kMultiGPRegistersWidened = true;
1267 static constexpr bool kAlignLongOnStack = false;
1268 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001269#elif defined(__i386__)
1270 // TODO: Check these!
Andreas Gampec147b002014-03-06 18:11:06 -08001271 static constexpr bool kNativeSoftFloatAbi = false; // Not using int registers for fp
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001272 static constexpr size_t kNumNativeGprArgs = 0; // 6 arguments passed in GPRs.
1273 static constexpr size_t kNumNativeFprArgs = 0; // 8 arguments passed in FPRs.
1274
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001275 static constexpr size_t kRegistersNeededForLong = 2;
1276 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001277 static constexpr bool kMultiRegistersAligned = false; // x86 not using regs, anyways
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001278 static constexpr bool kMultiFPRegistersWidened = false;
1279 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001280 static constexpr bool kAlignLongOnStack = false;
1281 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001282#elif defined(__x86_64__)
1283 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
1284 static constexpr size_t kNumNativeGprArgs = 6; // 6 arguments passed in GPRs.
1285 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
1286
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001287 static constexpr size_t kRegistersNeededForLong = 1;
1288 static constexpr size_t kRegistersNeededForDouble = 1;
Andreas Gampec147b002014-03-06 18:11:06 -08001289 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001290 static constexpr bool kMultiFPRegistersWidened = false;
1291 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001292 static constexpr bool kAlignLongOnStack = false;
1293 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001294#else
1295#error "Unsupported architecture"
1296#endif
1297
Andreas Gampec147b002014-03-06 18:11:06 -08001298 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001299 explicit BuildNativeCallFrameStateMachine(T* delegate)
1300 : gpr_index_(kNumNativeGprArgs),
1301 fpr_index_(kNumNativeFprArgs),
1302 stack_entries_(0),
1303 delegate_(delegate) {
Andreas Gampec147b002014-03-06 18:11:06 -08001304 // For register alignment, we want to assume that counters (gpr_index_, fpr_index_) are even iff
1305 // the next register is even; counting down is just to make the compiler happy...
Andreas Gampe575e78c2014-11-03 23:41:03 -08001306 static_assert(kNumNativeGprArgs % 2 == 0U, "Number of native GPR arguments not even");
1307 static_assert(kNumNativeFprArgs % 2 == 0U, "Number of native FPR arguments not even");
Andreas Gampec147b002014-03-06 18:11:06 -08001308 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001309
Andreas Gampec200a4a2014-06-16 18:39:09 -07001310 virtual ~BuildNativeCallFrameStateMachine() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001311
Ian Rogers1428dce2014-10-21 15:02:15 -07001312 bool HavePointerGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001313 return gpr_index_ > 0;
1314 }
1315
Andreas Gampec200a4a2014-06-16 18:39:09 -07001316 void AdvancePointer(const void* val) {
Andreas Gampec147b002014-03-06 18:11:06 -08001317 if (HavePointerGpr()) {
1318 gpr_index_--;
1319 PushGpr(reinterpret_cast<uintptr_t>(val));
1320 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001321 stack_entries_++; // TODO: have a field for pointer length as multiple of 32b
Andreas Gampec147b002014-03-06 18:11:06 -08001322 PushStack(reinterpret_cast<uintptr_t>(val));
1323 gpr_index_ = 0;
1324 }
1325 }
1326
Ian Rogers1428dce2014-10-21 15:02:15 -07001327 bool HaveHandleScopeGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001328 return gpr_index_ > 0;
1329 }
1330
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001331 void AdvanceHandleScope(mirror::Object* ptr) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001332 uintptr_t handle = PushHandle(ptr);
1333 if (HaveHandleScopeGpr()) {
Andreas Gampec147b002014-03-06 18:11:06 -08001334 gpr_index_--;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001335 PushGpr(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001336 } else {
1337 stack_entries_++;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001338 PushStack(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001339 gpr_index_ = 0;
1340 }
1341 }
1342
Ian Rogers1428dce2014-10-21 15:02:15 -07001343 bool HaveIntGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001344 return gpr_index_ > 0;
1345 }
1346
1347 void AdvanceInt(uint32_t val) {
1348 if (HaveIntGpr()) {
1349 gpr_index_--;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001350 if (kMultiGPRegistersWidened) {
1351 DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001352 PushGpr(static_cast<int64_t>(bit_cast<int32_t, uint32_t>(val)));
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001353 } else {
1354 PushGpr(val);
1355 }
Andreas Gampec147b002014-03-06 18:11:06 -08001356 } else {
1357 stack_entries_++;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001358 if (kMultiGPRegistersWidened) {
1359 DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001360 PushStack(static_cast<int64_t>(bit_cast<int32_t, uint32_t>(val)));
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001361 } else {
1362 PushStack(val);
1363 }
Andreas Gampec147b002014-03-06 18:11:06 -08001364 gpr_index_ = 0;
1365 }
1366 }
1367
Ian Rogers1428dce2014-10-21 15:02:15 -07001368 bool HaveLongGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001369 return gpr_index_ >= kRegistersNeededForLong + (LongGprNeedsPadding() ? 1 : 0);
1370 }
1371
Ian Rogers1428dce2014-10-21 15:02:15 -07001372 bool LongGprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001373 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1374 kAlignLongOnStack && // and when it needs alignment
1375 (gpr_index_ & 1) == 1; // counter is odd, see constructor
1376 }
1377
Ian Rogers1428dce2014-10-21 15:02:15 -07001378 bool LongStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001379 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1380 kAlignLongOnStack && // and when it needs 8B alignment
1381 (stack_entries_ & 1) == 1; // counter is odd
1382 }
1383
1384 void AdvanceLong(uint64_t val) {
1385 if (HaveLongGpr()) {
1386 if (LongGprNeedsPadding()) {
1387 PushGpr(0);
1388 gpr_index_--;
1389 }
1390 if (kRegistersNeededForLong == 1) {
1391 PushGpr(static_cast<uintptr_t>(val));
1392 } else {
1393 PushGpr(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1394 PushGpr(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1395 }
1396 gpr_index_ -= kRegistersNeededForLong;
1397 } else {
1398 if (LongStackNeedsPadding()) {
1399 PushStack(0);
1400 stack_entries_++;
1401 }
1402 if (kRegistersNeededForLong == 1) {
1403 PushStack(static_cast<uintptr_t>(val));
1404 stack_entries_++;
1405 } else {
1406 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1407 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1408 stack_entries_ += 2;
1409 }
1410 gpr_index_ = 0;
1411 }
1412 }
1413
Ian Rogers1428dce2014-10-21 15:02:15 -07001414 bool HaveFloatFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001415 return fpr_index_ > 0;
1416 }
1417
Andreas Gampec147b002014-03-06 18:11:06 -08001418 void AdvanceFloat(float val) {
1419 if (kNativeSoftFloatAbi) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001420 AdvanceInt(bit_cast<uint32_t, float>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001421 } else {
1422 if (HaveFloatFpr()) {
1423 fpr_index_--;
1424 if (kRegistersNeededForDouble == 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001425 if (kMultiFPRegistersWidened) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001426 PushFpr8(bit_cast<uint64_t, double>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001427 } else {
1428 // No widening, just use the bits.
Roland Levillainda4d79b2015-03-24 14:36:11 +00001429 PushFpr8(static_cast<uint64_t>(bit_cast<uint32_t, float>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001430 }
1431 } else {
1432 PushFpr4(val);
1433 }
1434 } else {
1435 stack_entries_++;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001436 if (kRegistersNeededForDouble == 1 && kMultiFPRegistersWidened) {
Andreas Gampec147b002014-03-06 18:11:06 -08001437 // Need to widen before storing: Note the "double" in the template instantiation.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001438 // Note: We need to jump through those hoops to make the compiler happy.
1439 DCHECK_EQ(sizeof(uintptr_t), sizeof(uint64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001440 PushStack(static_cast<uintptr_t>(bit_cast<uint64_t, double>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001441 } else {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001442 PushStack(static_cast<uintptr_t>(bit_cast<uint32_t, float>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001443 }
1444 fpr_index_ = 0;
1445 }
1446 }
1447 }
1448
Ian Rogers1428dce2014-10-21 15:02:15 -07001449 bool HaveDoubleFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001450 return fpr_index_ >= kRegistersNeededForDouble + (DoubleFprNeedsPadding() ? 1 : 0);
1451 }
1452
Ian Rogers1428dce2014-10-21 15:02:15 -07001453 bool DoubleFprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001454 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1455 kAlignDoubleOnStack && // and when it needs alignment
1456 (fpr_index_ & 1) == 1; // counter is odd, see constructor
1457 }
1458
Ian Rogers1428dce2014-10-21 15:02:15 -07001459 bool DoubleStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001460 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1461 kAlignDoubleOnStack && // and when it needs 8B alignment
1462 (stack_entries_ & 1) == 1; // counter is odd
1463 }
1464
1465 void AdvanceDouble(uint64_t val) {
1466 if (kNativeSoftFloatAbi) {
1467 AdvanceLong(val);
1468 } else {
1469 if (HaveDoubleFpr()) {
1470 if (DoubleFprNeedsPadding()) {
1471 PushFpr4(0);
1472 fpr_index_--;
1473 }
1474 PushFpr8(val);
1475 fpr_index_ -= kRegistersNeededForDouble;
1476 } else {
1477 if (DoubleStackNeedsPadding()) {
1478 PushStack(0);
1479 stack_entries_++;
1480 }
1481 if (kRegistersNeededForDouble == 1) {
1482 PushStack(static_cast<uintptr_t>(val));
1483 stack_entries_++;
1484 } else {
1485 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1486 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1487 stack_entries_ += 2;
1488 }
1489 fpr_index_ = 0;
1490 }
1491 }
1492 }
1493
Ian Rogers1428dce2014-10-21 15:02:15 -07001494 uint32_t GetStackEntries() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001495 return stack_entries_;
1496 }
1497
Ian Rogers1428dce2014-10-21 15:02:15 -07001498 uint32_t GetNumberOfUsedGprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001499 return kNumNativeGprArgs - gpr_index_;
1500 }
1501
Ian Rogers1428dce2014-10-21 15:02:15 -07001502 uint32_t GetNumberOfUsedFprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001503 return kNumNativeFprArgs - fpr_index_;
1504 }
1505
1506 private:
1507 void PushGpr(uintptr_t val) {
1508 delegate_->PushGpr(val);
1509 }
1510 void PushFpr4(float val) {
1511 delegate_->PushFpr4(val);
1512 }
1513 void PushFpr8(uint64_t val) {
1514 delegate_->PushFpr8(val);
1515 }
1516 void PushStack(uintptr_t val) {
1517 delegate_->PushStack(val);
1518 }
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001519 uintptr_t PushHandle(mirror::Object* ref) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001520 return delegate_->PushHandle(ref);
Andreas Gampec147b002014-03-06 18:11:06 -08001521 }
1522
1523 uint32_t gpr_index_; // Number of free GPRs
1524 uint32_t fpr_index_; // Number of free FPRs
1525 uint32_t stack_entries_; // Stack entries are in multiples of 32b, as floats are usually not
1526 // extended
Ian Rogers1428dce2014-10-21 15:02:15 -07001527 T* const delegate_; // What Push implementation gets called
Andreas Gampec147b002014-03-06 18:11:06 -08001528};
1529
Andreas Gampec200a4a2014-06-16 18:39:09 -07001530// Computes the sizes of register stacks and call stack area. Handling of references can be extended
1531// in subclasses.
1532//
1533// To handle native pointers, use "L" in the shorty for an object reference, which simulates
1534// them with handles.
1535class ComputeNativeCallFrameSize {
Andreas Gampec147b002014-03-06 18:11:06 -08001536 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001537 ComputeNativeCallFrameSize() : num_stack_entries_(0) {}
1538
1539 virtual ~ComputeNativeCallFrameSize() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001540
Ian Rogers1428dce2014-10-21 15:02:15 -07001541 uint32_t GetStackSize() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001542 return num_stack_entries_ * sizeof(uintptr_t);
1543 }
1544
Ian Rogers1428dce2014-10-21 15:02:15 -07001545 uint8_t* LayoutCallStack(uint8_t* sp8) const {
Andreas Gampec147b002014-03-06 18:11:06 -08001546 sp8 -= GetStackSize();
Andreas Gampe779f8c92014-06-09 18:29:38 -07001547 // Align by kStackAlignment.
1548 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
Andreas Gampec200a4a2014-06-16 18:39:09 -07001549 return sp8;
Andreas Gampec147b002014-03-06 18:11:06 -08001550 }
1551
Ian Rogers1428dce2014-10-21 15:02:15 -07001552 uint8_t* LayoutCallRegisterStacks(uint8_t* sp8, uintptr_t** start_gpr, uint32_t** start_fpr)
1553 const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001554 // Assumption is OK right now, as we have soft-float arm
1555 size_t fregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeFprArgs;
1556 sp8 -= fregs * sizeof(uintptr_t);
1557 *start_fpr = reinterpret_cast<uint32_t*>(sp8);
1558 size_t iregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeGprArgs;
1559 sp8 -= iregs * sizeof(uintptr_t);
1560 *start_gpr = reinterpret_cast<uintptr_t*>(sp8);
1561 return sp8;
1562 }
Andreas Gampec147b002014-03-06 18:11:06 -08001563
Andreas Gampec200a4a2014-06-16 18:39:09 -07001564 uint8_t* LayoutNativeCall(uint8_t* sp8, uintptr_t** start_stack, uintptr_t** start_gpr,
Ian Rogers1428dce2014-10-21 15:02:15 -07001565 uint32_t** start_fpr) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001566 // Native call stack.
1567 sp8 = LayoutCallStack(sp8);
1568 *start_stack = reinterpret_cast<uintptr_t*>(sp8);
Andreas Gampec147b002014-03-06 18:11:06 -08001569
Andreas Gampec200a4a2014-06-16 18:39:09 -07001570 // Put fprs and gprs below.
1571 sp8 = LayoutCallRegisterStacks(sp8, start_gpr, start_fpr);
Andreas Gampec147b002014-03-06 18:11:06 -08001572
Andreas Gampec200a4a2014-06-16 18:39:09 -07001573 // Return the new bottom.
1574 return sp8;
1575 }
1576
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001577 virtual void WalkHeader(
1578 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm ATTRIBUTE_UNUSED)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001579 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001580 }
Andreas Gampec200a4a2014-06-16 18:39:09 -07001581
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001582 void Walk(const char* shorty, uint32_t shorty_len) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001583 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize> sm(this);
1584
1585 WalkHeader(&sm);
Andreas Gampec147b002014-03-06 18:11:06 -08001586
1587 for (uint32_t i = 1; i < shorty_len; ++i) {
1588 Primitive::Type cur_type_ = Primitive::GetType(shorty[i]);
1589 switch (cur_type_) {
1590 case Primitive::kPrimNot:
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001591 // TODO: fix abuse of mirror types.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001592 sm.AdvanceHandleScope(
1593 reinterpret_cast<mirror::Object*>(0x12345678));
Andreas Gampec147b002014-03-06 18:11:06 -08001594 break;
1595
1596 case Primitive::kPrimBoolean:
1597 case Primitive::kPrimByte:
1598 case Primitive::kPrimChar:
1599 case Primitive::kPrimShort:
1600 case Primitive::kPrimInt:
1601 sm.AdvanceInt(0);
1602 break;
1603 case Primitive::kPrimFloat:
1604 sm.AdvanceFloat(0);
1605 break;
1606 case Primitive::kPrimDouble:
1607 sm.AdvanceDouble(0);
1608 break;
1609 case Primitive::kPrimLong:
1610 sm.AdvanceLong(0);
1611 break;
1612 default:
1613 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001614 UNREACHABLE();
Andreas Gampec147b002014-03-06 18:11:06 -08001615 }
1616 }
1617
Ian Rogers1428dce2014-10-21 15:02:15 -07001618 num_stack_entries_ = sm.GetStackEntries();
Andreas Gampec147b002014-03-06 18:11:06 -08001619 }
1620
1621 void PushGpr(uintptr_t /* val */) {
1622 // not optimizing registers, yet
1623 }
1624
1625 void PushFpr4(float /* val */) {
1626 // not optimizing registers, yet
1627 }
1628
1629 void PushFpr8(uint64_t /* val */) {
1630 // not optimizing registers, yet
1631 }
1632
1633 void PushStack(uintptr_t /* val */) {
1634 // counting is already done in the superclass
1635 }
1636
Andreas Gampec200a4a2014-06-16 18:39:09 -07001637 virtual uintptr_t PushHandle(mirror::Object* /* ptr */) {
Andreas Gampec147b002014-03-06 18:11:06 -08001638 return reinterpret_cast<uintptr_t>(nullptr);
1639 }
1640
Andreas Gampec200a4a2014-06-16 18:39:09 -07001641 protected:
Andreas Gampec147b002014-03-06 18:11:06 -08001642 uint32_t num_stack_entries_;
1643};
1644
Andreas Gampec200a4a2014-06-16 18:39:09 -07001645class ComputeGenericJniFrameSize FINAL : public ComputeNativeCallFrameSize {
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001646 public:
Igor Murashkin06a04e02016-09-13 15:57:37 -07001647 explicit ComputeGenericJniFrameSize(bool critical_native)
1648 : num_handle_scope_references_(0), critical_native_(critical_native) {}
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001649
Andreas Gampec200a4a2014-06-16 18:39:09 -07001650 // Lays out the callee-save frame. Assumes that the incorrect frame corresponding to RefsAndArgs
1651 // is at *m = sp. Will update to point to the bottom of the save frame.
1652 //
1653 // Note: assumes ComputeAll() has been run before.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001654 void LayoutCalleeSaveFrame(Thread* self, ArtMethod*** m, void* sp, HandleScope** handle_scope)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001655 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001656 ArtMethod* method = **m;
1657
Andreas Gampe542451c2016-07-26 09:02:02 -07001658 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001659
Andreas Gampec200a4a2014-06-16 18:39:09 -07001660 uint8_t* sp8 = reinterpret_cast<uint8_t*>(sp);
1661
1662 // First, fix up the layout of the callee-save frame.
1663 // We have to squeeze in the HandleScope, and relocate the method pointer.
1664
1665 // "Free" the slot for the method.
Ian Rogers13735952014-10-08 12:43:28 -07001666 sp8 += sizeof(void*); // In the callee-save frame we use a full pointer.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001667
1668 // Under the callee saves put handle scope and new method stack reference.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001669 size_t handle_scope_size = HandleScope::SizeOf(num_handle_scope_references_);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001670 size_t scope_and_method = handle_scope_size + sizeof(ArtMethod*);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001671
1672 sp8 -= scope_and_method;
1673 // Align by kStackAlignment.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001674 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
Andreas Gampec200a4a2014-06-16 18:39:09 -07001675
Mathieu Chartiere401d142015-04-22 13:56:20 -07001676 uint8_t* sp8_table = sp8 + sizeof(ArtMethod*);
Ian Rogers59c07062014-10-10 13:03:39 -07001677 *handle_scope = HandleScope::Create(sp8_table, self->GetTopHandleScope(),
1678 num_handle_scope_references_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001679
1680 // Add a slot for the method pointer, and fill it. Fix the pointer-pointer given to us.
1681 uint8_t* method_pointer = sp8;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001682 auto** new_method_ref = reinterpret_cast<ArtMethod**>(method_pointer);
1683 *new_method_ref = method;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001684 *m = new_method_ref;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001685 }
1686
Andreas Gampec200a4a2014-06-16 18:39:09 -07001687 // Adds space for the cookie. Note: may leave stack unaligned.
Ian Rogers1428dce2014-10-21 15:02:15 -07001688 void LayoutCookie(uint8_t** sp) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001689 // Reference cookie and padding
1690 *sp -= 8;
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001691 }
1692
Andreas Gampec200a4a2014-06-16 18:39:09 -07001693 // Re-layout the callee-save frame (insert a handle-scope). Then add space for the cookie.
1694 // Returns the new bottom. Note: this may be unaligned.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001695 uint8_t* LayoutJNISaveFrame(Thread* self, ArtMethod*** m, void* sp, HandleScope** handle_scope)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001696 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001697 // First, fix up the layout of the callee-save frame.
1698 // We have to squeeze in the HandleScope, and relocate the method pointer.
Ian Rogers59c07062014-10-10 13:03:39 -07001699 LayoutCalleeSaveFrame(self, m, sp, handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001700
1701 // The bottom of the callee-save frame is now where the method is, *m.
1702 uint8_t* sp8 = reinterpret_cast<uint8_t*>(*m);
1703
1704 // Add space for cookie.
1705 LayoutCookie(&sp8);
1706
1707 return sp8;
1708 }
1709
1710 // WARNING: After this, *sp won't be pointing to the method anymore!
Mathieu Chartiere401d142015-04-22 13:56:20 -07001711 uint8_t* ComputeLayout(Thread* self, ArtMethod*** m, const char* shorty, uint32_t shorty_len,
1712 HandleScope** handle_scope, uintptr_t** start_stack, uintptr_t** start_gpr,
1713 uint32_t** start_fpr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001714 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001715 Walk(shorty, shorty_len);
1716
1717 // JNI part.
Ian Rogers59c07062014-10-10 13:03:39 -07001718 uint8_t* sp8 = LayoutJNISaveFrame(self, m, reinterpret_cast<void*>(*m), handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001719
1720 sp8 = LayoutNativeCall(sp8, start_stack, start_gpr, start_fpr);
1721
1722 // Return the new bottom.
1723 return sp8;
1724 }
1725
1726 uintptr_t PushHandle(mirror::Object* /* ptr */) OVERRIDE;
1727
1728 // Add JNIEnv* and jobj/jclass before the shorty-derived elements.
1729 void WalkHeader(BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) OVERRIDE
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001730 REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001731
1732 private:
1733 uint32_t num_handle_scope_references_;
Igor Murashkin06a04e02016-09-13 15:57:37 -07001734 const bool critical_native_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001735};
1736
1737uintptr_t ComputeGenericJniFrameSize::PushHandle(mirror::Object* /* ptr */) {
1738 num_handle_scope_references_++;
1739 return reinterpret_cast<uintptr_t>(nullptr);
1740}
1741
1742void ComputeGenericJniFrameSize::WalkHeader(
1743 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) {
Igor Murashkin06a04e02016-09-13 15:57:37 -07001744 // First 2 parameters are always excluded for @CriticalNative.
1745 if (UNLIKELY(critical_native_)) {
1746 return;
1747 }
1748
Andreas Gampec200a4a2014-06-16 18:39:09 -07001749 // JNIEnv
1750 sm->AdvancePointer(nullptr);
1751
1752 // Class object or this as first argument
1753 sm->AdvanceHandleScope(reinterpret_cast<mirror::Object*>(0x12345678));
1754}
1755
1756// Class to push values to three separate regions. Used to fill the native call part. Adheres to
1757// the template requirements of BuildGenericJniFrameStateMachine.
1758class FillNativeCall {
1759 public:
1760 FillNativeCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) :
1761 cur_gpr_reg_(gpr_regs), cur_fpr_reg_(fpr_regs), cur_stack_arg_(stack_args) {}
1762
1763 virtual ~FillNativeCall() {}
1764
1765 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) {
1766 cur_gpr_reg_ = gpr_regs;
1767 cur_fpr_reg_ = fpr_regs;
1768 cur_stack_arg_ = stack_args;
Andreas Gampec147b002014-03-06 18:11:06 -08001769 }
1770
1771 void PushGpr(uintptr_t val) {
1772 *cur_gpr_reg_ = val;
1773 cur_gpr_reg_++;
1774 }
1775
1776 void PushFpr4(float val) {
1777 *cur_fpr_reg_ = val;
1778 cur_fpr_reg_++;
1779 }
1780
1781 void PushFpr8(uint64_t val) {
1782 uint64_t* tmp = reinterpret_cast<uint64_t*>(cur_fpr_reg_);
1783 *tmp = val;
1784 cur_fpr_reg_ += 2;
1785 }
1786
1787 void PushStack(uintptr_t val) {
1788 *cur_stack_arg_ = val;
1789 cur_stack_arg_++;
1790 }
1791
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001792 virtual uintptr_t PushHandle(mirror::Object*) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001793 LOG(FATAL) << "(Non-JNI) Native call does not use handles.";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001794 UNREACHABLE();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001795 }
1796
1797 private:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001798 uintptr_t* cur_gpr_reg_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001799 uint32_t* cur_fpr_reg_;
1800 uintptr_t* cur_stack_arg_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001801};
Andreas Gampec147b002014-03-06 18:11:06 -08001802
Andreas Gampec200a4a2014-06-16 18:39:09 -07001803// Visits arguments on the stack placing them into a region lower down the stack for the benefit
1804// of transitioning into native code.
1805class BuildGenericJniFrameVisitor FINAL : public QuickArgumentVisitor {
1806 public:
Igor Murashkin06a04e02016-09-13 15:57:37 -07001807 BuildGenericJniFrameVisitor(Thread* self,
1808 bool is_static,
1809 bool critical_native,
1810 const char* shorty,
1811 uint32_t shorty_len,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001812 ArtMethod*** sp)
Andreas Gampec200a4a2014-06-16 18:39:09 -07001813 : QuickArgumentVisitor(*sp, is_static, shorty, shorty_len),
Igor Murashkin06a04e02016-09-13 15:57:37 -07001814 jni_call_(nullptr, nullptr, nullptr, nullptr, critical_native),
1815 sm_(&jni_call_) {
1816 ComputeGenericJniFrameSize fsc(critical_native);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001817 uintptr_t* start_gpr_reg;
1818 uint32_t* start_fpr_reg;
1819 uintptr_t* start_stack_arg;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001820 bottom_of_used_area_ = fsc.ComputeLayout(self, sp, shorty, shorty_len,
Ian Rogers59c07062014-10-10 13:03:39 -07001821 &handle_scope_,
1822 &start_stack_arg,
Andreas Gampec200a4a2014-06-16 18:39:09 -07001823 &start_gpr_reg, &start_fpr_reg);
1824
Andreas Gampec200a4a2014-06-16 18:39:09 -07001825 jni_call_.Reset(start_gpr_reg, start_fpr_reg, start_stack_arg, handle_scope_);
1826
Igor Murashkin06a04e02016-09-13 15:57:37 -07001827 // First 2 parameters are always excluded for CriticalNative methods.
1828 if (LIKELY(!critical_native)) {
1829 // jni environment is always first argument
1830 sm_.AdvancePointer(self->GetJniEnv());
Andreas Gampec200a4a2014-06-16 18:39:09 -07001831
Igor Murashkin06a04e02016-09-13 15:57:37 -07001832 if (is_static) {
1833 sm_.AdvanceHandleScope((**sp)->GetDeclaringClass());
1834 } // else "this" reference is already handled by QuickArgumentVisitor.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001835 }
1836 }
1837
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001838 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001839
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001840 void FinalizeHandleScope(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001841
Vladimir Markof39745e2016-01-26 12:16:55 +00001842 StackReference<mirror::Object>* GetFirstHandleScopeEntry() {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001843 return handle_scope_->GetHandle(0).GetReference();
1844 }
1845
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001846 jobject GetFirstHandleScopeJObject() const REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001847 return handle_scope_->GetHandle(0).ToJObject();
1848 }
1849
Ian Rogers1428dce2014-10-21 15:02:15 -07001850 void* GetBottomOfUsedArea() const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001851 return bottom_of_used_area_;
1852 }
1853
1854 private:
1855 // A class to fill a JNI call. Adds reference/handle-scope management to FillNativeCall.
1856 class FillJniCall FINAL : public FillNativeCall {
1857 public:
1858 FillJniCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args,
Igor Murashkin06a04e02016-09-13 15:57:37 -07001859 HandleScope* handle_scope, bool critical_native)
1860 : FillNativeCall(gpr_regs, fpr_regs, stack_args),
1861 handle_scope_(handle_scope),
1862 cur_entry_(0),
1863 critical_native_(critical_native) {}
Andreas Gampec200a4a2014-06-16 18:39:09 -07001864
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001865 uintptr_t PushHandle(mirror::Object* ref) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001866
1867 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args, HandleScope* scope) {
1868 FillNativeCall::Reset(gpr_regs, fpr_regs, stack_args);
1869 handle_scope_ = scope;
1870 cur_entry_ = 0U;
1871 }
1872
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001873 void ResetRemainingScopeSlots() REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001874 // Initialize padding entries.
1875 size_t expected_slots = handle_scope_->NumberOfReferences();
1876 while (cur_entry_ < expected_slots) {
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07001877 handle_scope_->GetMutableHandle(cur_entry_++).Assign(nullptr);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001878 }
Igor Murashkin06a04e02016-09-13 15:57:37 -07001879
1880 if (!critical_native_) {
1881 // Non-critical natives have at least the self class (jclass) or this (jobject).
1882 DCHECK_NE(cur_entry_, 0U);
1883 }
Andreas Gampec200a4a2014-06-16 18:39:09 -07001884 }
1885
Mathieu Chartier1432a5b2016-10-04 15:41:42 -07001886 bool CriticalNative() const {
1887 return critical_native_;
1888 }
1889
Andreas Gampec200a4a2014-06-16 18:39:09 -07001890 private:
1891 HandleScope* handle_scope_;
1892 size_t cur_entry_;
Igor Murashkin06a04e02016-09-13 15:57:37 -07001893 const bool critical_native_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001894 };
1895
1896 HandleScope* handle_scope_;
1897 FillJniCall jni_call_;
1898 void* bottom_of_used_area_;
1899
1900 BuildNativeCallFrameStateMachine<FillJniCall> sm_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001901
1902 DISALLOW_COPY_AND_ASSIGN(BuildGenericJniFrameVisitor);
1903};
1904
Andreas Gampec200a4a2014-06-16 18:39:09 -07001905uintptr_t BuildGenericJniFrameVisitor::FillJniCall::PushHandle(mirror::Object* ref) {
1906 uintptr_t tmp;
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07001907 MutableHandle<mirror::Object> h = handle_scope_->GetMutableHandle(cur_entry_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001908 h.Assign(ref);
1909 tmp = reinterpret_cast<uintptr_t>(h.ToJObject());
1910 cur_entry_++;
1911 return tmp;
1912}
1913
Ian Rogers9758f792014-03-13 09:02:55 -07001914void BuildGenericJniFrameVisitor::Visit() {
1915 Primitive::Type type = GetParamPrimitiveType();
1916 switch (type) {
1917 case Primitive::kPrimLong: {
1918 jlong long_arg;
1919 if (IsSplitLongOrDouble()) {
1920 long_arg = ReadSplitLongParam();
1921 } else {
1922 long_arg = *reinterpret_cast<jlong*>(GetParamAddress());
1923 }
1924 sm_.AdvanceLong(long_arg);
1925 break;
1926 }
1927 case Primitive::kPrimDouble: {
1928 uint64_t double_arg;
1929 if (IsSplitLongOrDouble()) {
1930 // Read into union so that we don't case to a double.
1931 double_arg = ReadSplitLongParam();
1932 } else {
1933 double_arg = *reinterpret_cast<uint64_t*>(GetParamAddress());
1934 }
1935 sm_.AdvanceDouble(double_arg);
1936 break;
1937 }
1938 case Primitive::kPrimNot: {
1939 StackReference<mirror::Object>* stack_ref =
1940 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001941 sm_.AdvanceHandleScope(stack_ref->AsMirrorPtr());
Ian Rogers9758f792014-03-13 09:02:55 -07001942 break;
1943 }
1944 case Primitive::kPrimFloat:
1945 sm_.AdvanceFloat(*reinterpret_cast<float*>(GetParamAddress()));
1946 break;
1947 case Primitive::kPrimBoolean: // Fall-through.
1948 case Primitive::kPrimByte: // Fall-through.
1949 case Primitive::kPrimChar: // Fall-through.
1950 case Primitive::kPrimShort: // Fall-through.
1951 case Primitive::kPrimInt: // Fall-through.
1952 sm_.AdvanceInt(*reinterpret_cast<jint*>(GetParamAddress()));
1953 break;
1954 case Primitive::kPrimVoid:
1955 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -07001956 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -07001957 }
1958}
1959
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001960void BuildGenericJniFrameVisitor::FinalizeHandleScope(Thread* self) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001961 // Clear out rest of the scope.
1962 jni_call_.ResetRemainingScopeSlots();
Mathieu Chartier1432a5b2016-10-04 15:41:42 -07001963 if (!jni_call_.CriticalNative()) {
1964 // Install HandleScope.
1965 self->PushHandleScope(handle_scope_);
1966 }
Ian Rogers9758f792014-03-13 09:02:55 -07001967}
1968
Ian Rogers04c31d22014-07-07 21:44:06 -07001969#if defined(__arm__) || defined(__aarch64__)
Andreas Gampe90546832014-03-12 18:07:19 -07001970extern "C" void* artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07001971#else
1972extern "C" void* artFindNativeMethod(Thread* self);
1973#endif
Andreas Gampe90546832014-03-12 18:07:19 -07001974
Igor Murashkin06a04e02016-09-13 15:57:37 -07001975static uint64_t artQuickGenericJniEndJNIRef(Thread* self,
1976 uint32_t cookie,
1977 bool fast_native ATTRIBUTE_UNUSED,
1978 jobject l,
1979 jobject lock) {
1980 // TODO: add entrypoints for @FastNative returning objects.
Andreas Gampead615172014-04-04 16:20:13 -07001981 if (lock != nullptr) {
1982 return reinterpret_cast<uint64_t>(JniMethodEndWithReferenceSynchronized(l, cookie, lock, self));
1983 } else {
1984 return reinterpret_cast<uint64_t>(JniMethodEndWithReference(l, cookie, self));
1985 }
1986}
1987
Igor Murashkin06a04e02016-09-13 15:57:37 -07001988static void artQuickGenericJniEndJNINonRef(Thread* self,
1989 uint32_t cookie,
1990 bool fast_native,
1991 jobject lock) {
Andreas Gampead615172014-04-04 16:20:13 -07001992 if (lock != nullptr) {
1993 JniMethodEndSynchronized(cookie, lock, self);
Igor Murashkin06a04e02016-09-13 15:57:37 -07001994 // Ignore "fast_native" here because synchronized functions aren't very fast.
Andreas Gampead615172014-04-04 16:20:13 -07001995 } else {
Igor Murashkin06a04e02016-09-13 15:57:37 -07001996 if (UNLIKELY(fast_native)) {
1997 JniMethodFastEnd(cookie, self);
1998 } else {
1999 JniMethodEnd(cookie, self);
2000 }
Andreas Gampead615172014-04-04 16:20:13 -07002001 }
2002}
2003
Andreas Gampec147b002014-03-06 18:11:06 -08002004/*
2005 * Initializes an alloca region assumed to be directly below sp for a native call:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002006 * 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 -08002007 * The final element on the stack is a pointer to the native code.
2008 *
Andreas Gampe36fea8d2014-03-10 13:37:40 -07002009 * On entry, the stack has a standard callee-save frame above sp, and an alloca below it.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002010 * We need to fix this, as the handle scope needs to go into the callee-save frame.
Andreas Gampe36fea8d2014-03-10 13:37:40 -07002011 *
Andreas Gampec147b002014-03-06 18:11:06 -08002012 * The return of this function denotes:
2013 * 1) How many bytes of the alloca can be released, if the value is non-negative.
2014 * 2) An error, if the value is negative.
2015 */
Mathieu Chartiere401d142015-04-22 13:56:20 -07002016extern "C" TwoWordReturn artQuickGenericJniTrampoline(Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002017 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002018 ArtMethod* called = *sp;
David Sehr709b0702016-10-13 09:12:37 -07002019 DCHECK(called->IsNative()) << called->PrettyMethod(true);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002020 uint32_t shorty_len = 0;
2021 const char* shorty = called->GetShorty(&shorty_len);
Igor Murashkin06a04e02016-09-13 15:57:37 -07002022 bool critical_native = called->IsAnnotatedWithCriticalNative();
2023 bool fast_native = called->IsAnnotatedWithFastNative();
2024 bool normal_native = !critical_native && !fast_native;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002025
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002026 // Run the visitor and update sp.
Igor Murashkin06a04e02016-09-13 15:57:37 -07002027 BuildGenericJniFrameVisitor visitor(self,
2028 called->IsStatic(),
2029 critical_native,
2030 shorty,
2031 shorty_len,
2032 &sp);
Mathieu Chartierbe08cf52016-09-13 13:41:24 -07002033 {
2034 ScopedAssertNoThreadSuspension sants(__FUNCTION__);
2035 visitor.VisitArguments();
2036 // FinalizeHandleScope pushes the handle scope on the thread.
2037 visitor.FinalizeHandleScope(self);
2038 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002039
Andreas Gampec200a4a2014-06-16 18:39:09 -07002040 // Fix up managed-stack things in Thread.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002041 self->SetTopOfStack(sp);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002042
Ian Rogerse0dcd462014-03-08 15:21:04 -08002043 self->VerifyStack();
2044
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002045 uint32_t cookie;
Igor Murashkin06a04e02016-09-13 15:57:37 -07002046 uint32_t* sp32;
2047 // Skip calling JniMethodStart for @CriticalNative.
2048 if (LIKELY(!critical_native)) {
2049 // Start JNI, save the cookie.
2050 if (called->IsSynchronized()) {
2051 DCHECK(normal_native) << " @FastNative and synchronize is not supported";
2052 cookie = JniMethodStartSynchronized(visitor.GetFirstHandleScopeJObject(), self);
2053 if (self->IsExceptionPending()) {
2054 self->PopHandleScope();
2055 // A negative value denotes an error.
2056 return GetTwoWordFailureValue();
2057 }
2058 } else {
2059 if (fast_native) {
2060 cookie = JniMethodFastStart(self);
2061 } else {
2062 DCHECK(normal_native);
2063 cookie = JniMethodStart(self);
2064 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002065 }
Igor Murashkin06a04e02016-09-13 15:57:37 -07002066 sp32 = reinterpret_cast<uint32_t*>(sp);
2067 *(sp32 - 1) = cookie;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002068 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002069
Andreas Gampe90546832014-03-12 18:07:19 -07002070 // Retrieve the stored native code.
Mathieu Chartier2d721012014-11-10 11:08:06 -08002071 void* nativeCode = called->GetEntryPointFromJni();
Andreas Gampe90546832014-03-12 18:07:19 -07002072
Andreas Gampe9a6a99a2014-03-14 07:52:20 -07002073 // There are two cases for the content of nativeCode:
2074 // 1) Pointer to the native function.
2075 // 2) Pointer to the trampoline for native code binding.
2076 // In the second case, we need to execute the binding and continue with the actual native function
2077 // pointer.
Andreas Gampe90546832014-03-12 18:07:19 -07002078 DCHECK(nativeCode != nullptr);
2079 if (nativeCode == GetJniDlsymLookupStub()) {
Ian Rogers04c31d22014-07-07 21:44:06 -07002080#if defined(__arm__) || defined(__aarch64__)
Andreas Gampe90546832014-03-12 18:07:19 -07002081 nativeCode = artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07002082#else
2083 nativeCode = artFindNativeMethod(self);
2084#endif
Andreas Gampe90546832014-03-12 18:07:19 -07002085
2086 if (nativeCode == nullptr) {
2087 DCHECK(self->IsExceptionPending()); // There should be an exception pending now.
Andreas Gampead615172014-04-04 16:20:13 -07002088
Igor Murashkin06a04e02016-09-13 15:57:37 -07002089 // @CriticalNative calls do not need to call back into JniMethodEnd.
2090 if (LIKELY(!critical_native)) {
2091 // End JNI, as the assembly will move to deliver the exception.
2092 jobject lock = called->IsSynchronized() ? visitor.GetFirstHandleScopeJObject() : nullptr;
2093 if (shorty[0] == 'L') {
2094 artQuickGenericJniEndJNIRef(self, cookie, fast_native, nullptr, lock);
2095 } else {
2096 artQuickGenericJniEndJNINonRef(self, cookie, fast_native, lock);
2097 }
Andreas Gampead615172014-04-04 16:20:13 -07002098 }
2099
Andreas Gampec200a4a2014-06-16 18:39:09 -07002100 return GetTwoWordFailureValue();
Andreas Gampe90546832014-03-12 18:07:19 -07002101 }
2102 // Note that the native code pointer will be automatically set by artFindNativeMethod().
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002103 }
2104
Alexey Frunze1b8464d2016-11-12 17:22:05 -08002105#if defined(__mips__) && !defined(__LP64__)
2106 // On MIPS32 if the first two arguments are floating-point, we need to know their types
2107 // so that art_quick_generic_jni_trampoline can correctly extract them from the stack
2108 // and load into floating-point registers.
2109 // Possible arrangements of first two floating-point arguments on the stack (32-bit FPU
2110 // view):
2111 // (1)
2112 // | DOUBLE | DOUBLE | other args, if any
2113 // | F12 | F13 | F14 | F15 |
2114 // | SP+0 | SP+4 | SP+8 | SP+12 | SP+16
2115 // (2)
2116 // | DOUBLE | FLOAT | (PAD) | other args, if any
2117 // | F12 | F13 | F14 | |
2118 // | SP+0 | SP+4 | SP+8 | SP+12 | SP+16
2119 // (3)
2120 // | FLOAT | (PAD) | DOUBLE | other args, if any
2121 // | F12 | | F14 | F15 |
2122 // | SP+0 | SP+4 | SP+8 | SP+12 | SP+16
2123 // (4)
2124 // | FLOAT | FLOAT | other args, if any
2125 // | F12 | F14 |
2126 // | SP+0 | SP+4 | SP+8
2127 // As you can see, only the last case (4) is special. In all others we can just
2128 // load F12/F13 and F14/F15 in the same manner.
2129 // Set bit 0 of the native code address to 1 in this case (valid code addresses
2130 // are always a multiple of 4 on MIPS32, so we have 2 spare bits available).
2131 if (nativeCode != nullptr &&
2132 shorty != nullptr &&
2133 shorty_len >= 3 &&
2134 shorty[1] == 'F' &&
2135 shorty[2] == 'F') {
2136 nativeCode = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(nativeCode) | 1);
2137 }
2138#endif
2139
Andreas Gampec200a4a2014-06-16 18:39:09 -07002140 // Return native code addr(lo) and bottom of alloca address(hi).
2141 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(visitor.GetBottomOfUsedArea()),
2142 reinterpret_cast<uintptr_t>(nativeCode));
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002143}
2144
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002145// Defined in quick_jni_entrypoints.cc.
2146extern uint64_t GenericJniMethodEnd(Thread* self, uint32_t saved_local_ref_cookie,
2147 jvalue result, uint64_t result_f, ArtMethod* called,
2148 HandleScope* handle_scope);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002149/*
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002150 * Is called after the native JNI code. Responsible for cleanup (handle scope, saved state) and
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002151 * unlocking.
2152 */
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002153extern "C" uint64_t artQuickGenericJniEndTrampoline(Thread* self,
2154 jvalue result,
2155 uint64_t result_f) {
2156 // We're here just back from a native call. We don't have the shared mutator lock at this point
2157 // yet until we call GoToRunnable() later in GenericJniMethodEnd(). Accessing objects or doing
2158 // anything that requires a mutator lock before that would cause problems as GC may have the
2159 // exclusive mutator lock and may be moving objects, etc.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002160 ArtMethod** sp = self->GetManagedStack()->GetTopQuickFrame();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002161 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002162 ArtMethod* called = *sp;
Ian Rogerse0dcd462014-03-08 15:21:04 -08002163 uint32_t cookie = *(sp32 - 1);
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002164 HandleScope* table = reinterpret_cast<HandleScope*>(reinterpret_cast<uint8_t*>(sp) + sizeof(*sp));
2165 return GenericJniMethodEnd(self, cookie, result, result_f, called, table);
Andreas Gampe2da88232014-02-27 12:26:20 -08002166}
2167
Andreas Gamped58342c2014-06-05 14:18:08 -07002168// We use TwoWordReturn to optimize scalar returns. We use the hi value for code, and the lo value
2169// for the method pointer.
Andreas Gampe51f76352014-05-21 08:28:48 -07002170//
Andreas Gamped58342c2014-06-05 14:18:08 -07002171// 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 -07002172// to hold the mutator lock (see REQUIRES_SHARED(Locks::mutator_lock_) annotations).
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002173
2174template<InvokeType type, bool access_check>
Mathieu Chartieref41db72016-10-25 15:08:01 -07002175static TwoWordReturn artInvokeCommon(uint32_t method_idx,
2176 ObjPtr<mirror::Object> this_object,
2177 Thread* self,
Mathieu Chartiere401d142015-04-22 13:56:20 -07002178 ArtMethod** sp) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002179 ScopedQuickEntrypointChecks sqec(self);
Vladimir Markofd36f1f2016-08-03 18:49:58 +01002180 DCHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(Runtime::kSaveRefsAndArgs));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002181 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
2182 ArtMethod* method = FindMethodFast(method_idx, this_object, caller_method, access_check, type);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002183 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002184 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()->GetDexFile();
2185 uint32_t shorty_len;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002186 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(method_idx), &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002187 {
2188 // Remember the args in case a GC happens in FindMethodFromCode.
2189 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
2190 RememberForGcArgumentVisitor visitor(sp, type == kStatic, shorty, shorty_len, &soa);
2191 visitor.VisitArguments();
Mathieu Chartieref41db72016-10-25 15:08:01 -07002192 method = FindMethodFromCode<type, access_check>(method_idx,
2193 &this_object,
2194 caller_method,
Mathieu Chartier0cd81352014-05-22 16:48:55 -07002195 self);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002196 visitor.FixupReferences();
2197 }
2198
Ian Rogerse0a02da2014-12-02 14:10:53 -08002199 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002200 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07002201 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002202 }
2203 }
2204 DCHECK(!self->IsExceptionPending());
2205 const void* code = method->GetEntryPointFromQuickCompiledCode();
2206
2207 // When we return, the caller will branch to this address, so it had better not be 0!
David Sehr709b0702016-10-13 09:12:37 -07002208 DCHECK(code != nullptr) << "Code was null in method: " << method->PrettyMethod()
Andreas Gampec200a4a2014-06-16 18:39:09 -07002209 << " location: "
2210 << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07002211
Andreas Gamped58342c2014-06-05 14:18:08 -07002212 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
2213 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002214}
2215
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01002216// Explicit artInvokeCommon template function declarations to please analysis tool.
2217#define EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(type, access_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002218 template REQUIRES_SHARED(Locks::mutator_lock_) \
Mathieu Chartiere401d142015-04-22 13:56:20 -07002219 TwoWordReturn artInvokeCommon<type, access_check>( \
Mathieu Chartieref41db72016-10-25 15:08:01 -07002220 uint32_t method_idx, ObjPtr<mirror::Object> his_object, Thread* self, ArtMethod** sp)
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01002221
2222EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, false);
2223EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, true);
2224EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, false);
2225EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, true);
2226EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, false);
2227EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, true);
2228EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, false);
2229EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, true);
2230EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, false);
2231EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, true);
2232#undef EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL
2233
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002234// See comments in runtime_support_asm.S
Andreas Gampec200a4a2014-06-16 18:39:09 -07002235extern "C" TwoWordReturn artInvokeInterfaceTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002236 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002237 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002238 return artInvokeCommon<kInterface, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002239}
2240
Andreas Gampec200a4a2014-06-16 18:39:09 -07002241extern "C" TwoWordReturn artInvokeDirectTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002242 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002243 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002244 return artInvokeCommon<kDirect, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002245}
2246
Andreas Gampec200a4a2014-06-16 18:39:09 -07002247extern "C" TwoWordReturn artInvokeStaticTrampolineWithAccessCheck(
Mathieu Chartieref41db72016-10-25 15:08:01 -07002248 uint32_t method_idx,
2249 mirror::Object* this_object ATTRIBUTE_UNUSED,
2250 Thread* self,
2251 ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
2252 // For static, this_object is not required and may be random garbage. Don't pass it down so that
2253 // it doesn't cause ObjPtr alignment failure check.
2254 return artInvokeCommon<kStatic, true>(method_idx, nullptr, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002255}
2256
Andreas Gampec200a4a2014-06-16 18:39:09 -07002257extern "C" TwoWordReturn artInvokeSuperTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002258 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002259 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002260 return artInvokeCommon<kSuper, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002261}
2262
Andreas Gampec200a4a2014-06-16 18:39:09 -07002263extern "C" TwoWordReturn artInvokeVirtualTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002264 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002265 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002266 return artInvokeCommon<kVirtual, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002267}
2268
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002269// Determine target of interface dispatch. This object is known non-null. First argument
2270// is there for consistency but should not be used, as some architectures overwrite it
2271// in the assembly trampoline.
2272extern "C" TwoWordReturn artInvokeInterfaceTrampoline(uint32_t deadbeef ATTRIBUTE_UNUSED,
Mathieu Chartieref41db72016-10-25 15:08:01 -07002273 mirror::Object* raw_this_object,
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002274 Thread* self,
2275 ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002276 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartieref41db72016-10-25 15:08:01 -07002277 ObjPtr<mirror::Object> this_object(raw_this_object);
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002278 ScopedQuickEntrypointChecks sqec(self);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002279 StackHandleScope<1> hs(self);
2280 Handle<mirror::Class> cls(hs.NewHandle(this_object->GetClass()));
2281
Nicolas Geoffray5bf7bac2016-07-06 14:18:23 +00002282 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002283
2284 // Fetch the dex_method_idx of the target interface method from the caller.
2285 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
2286
2287 const DexFile::CodeItem* code_item = caller_method->GetCodeItem();
2288 CHECK_LT(dex_pc, code_item->insns_size_in_code_units_);
2289 const Instruction* instr = Instruction::At(&code_item->insns_[dex_pc]);
2290 Instruction::Code instr_code = instr->Opcode();
2291 CHECK(instr_code == Instruction::INVOKE_INTERFACE ||
2292 instr_code == Instruction::INVOKE_INTERFACE_RANGE)
2293 << "Unexpected call into interface trampoline: " << instr->DumpString(nullptr);
2294 uint32_t dex_method_idx;
2295 if (instr_code == Instruction::INVOKE_INTERFACE) {
2296 dex_method_idx = instr->VRegB_35c();
2297 } else {
2298 CHECK_EQ(instr_code, Instruction::INVOKE_INTERFACE_RANGE);
2299 dex_method_idx = instr->VRegB_3rc();
2300 }
2301
Mathieu Chartiere401d142015-04-22 13:56:20 -07002302 ArtMethod* interface_method = caller_method->GetDexCacheResolvedMethod(
Andreas Gampe542451c2016-07-26 09:02:02 -07002303 dex_method_idx, kRuntimePointerSize);
David Sehr709b0702016-10-13 09:12:37 -07002304 DCHECK(interface_method != nullptr) << dex_method_idx << " " << caller_method->PrettyMethod();
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002305 ArtMethod* method = nullptr;
Andreas Gampe542451c2016-07-26 09:02:02 -07002306 ImTable* imt = cls->GetImt(kRuntimePointerSize);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002307
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002308 if (LIKELY(interface_method->GetDexMethodIndex() != DexFile::kDexNoIndex)) {
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002309 // If the dex cache already resolved the interface method, look whether we have
2310 // a match in the ImtConflictTable.
Andreas Gampe75a7db62016-09-26 12:04:26 -07002311 ArtMethod* conflict_method = imt->Get(ImTable::GetImtIndex(interface_method),
2312 kRuntimePointerSize);
Alex Light9fc547a2016-03-31 14:34:33 -07002313 if (LIKELY(conflict_method->IsRuntimeMethod())) {
Andreas Gampe542451c2016-07-26 09:02:02 -07002314 ImtConflictTable* current_table = conflict_method->GetImtConflictTable(kRuntimePointerSize);
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002315 DCHECK(current_table != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -07002316 method = current_table->Lookup(interface_method, kRuntimePointerSize);
Alex Light9fc547a2016-03-31 14:34:33 -07002317 } else {
2318 // It seems we aren't really a conflict method!
Andreas Gampe542451c2016-07-26 09:02:02 -07002319 method = cls->FindVirtualMethodForInterface(interface_method, kRuntimePointerSize);
Alex Light9fc547a2016-03-31 14:34:33 -07002320 }
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002321 if (method != nullptr) {
2322 return GetTwoWordSuccessValue(
2323 reinterpret_cast<uintptr_t>(method->GetEntryPointFromQuickCompiledCode()),
2324 reinterpret_cast<uintptr_t>(method));
2325 }
2326
2327 // No match, use the IfTable.
Andreas Gampe542451c2016-07-26 09:02:02 -07002328 method = cls->FindVirtualMethodForInterface(interface_method, kRuntimePointerSize);
Ian Rogerse0a02da2014-12-02 14:10:53 -08002329 if (UNLIKELY(method == nullptr)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002330 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(
2331 interface_method, this_object, caller_method);
Andreas Gamped58342c2014-06-05 14:18:08 -07002332 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002333 }
2334 } else {
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002335 // The dex cache did not resolve the method, look it up in the dex file
2336 // of the caller,
Mathieu Chartier4edd8472015-06-01 10:47:36 -07002337 DCHECK_EQ(interface_method, Runtime::Current()->GetResolutionMethod());
Andreas Gampec200a4a2014-06-16 18:39:09 -07002338 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()
2339 ->GetDexFile();
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002340 uint32_t shorty_len;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002341 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(dex_method_idx),
2342 &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002343 {
2344 // Remember the args in case a GC happens in FindMethodFromCode.
2345 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
2346 RememberForGcArgumentVisitor visitor(sp, false, shorty, shorty_len, &soa);
2347 visitor.VisitArguments();
Mathieu Chartieref41db72016-10-25 15:08:01 -07002348 method = FindMethodFromCode<kInterface, false>(dex_method_idx,
2349 &this_object,
2350 caller_method,
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002351 self);
2352 visitor.FixupReferences();
2353 }
2354
2355 if (UNLIKELY(method == nullptr)) {
2356 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07002357 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002358 }
Andreas Gampe542451c2016-07-26 09:02:02 -07002359 interface_method =
2360 caller_method->GetDexCacheResolvedMethod(dex_method_idx, kRuntimePointerSize);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002361 DCHECK(!interface_method->IsRuntimeMethod());
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002362 }
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002363
2364 // We arrive here if we have found an implementation, and it is not in the ImtConflictTable.
2365 // We create a new table with the new pair { interface_method, method }.
Andreas Gampe75a7db62016-09-26 12:04:26 -07002366 uint32_t imt_index = ImTable::GetImtIndex(interface_method);
Andreas Gampe542451c2016-07-26 09:02:02 -07002367 ArtMethod* conflict_method = imt->Get(imt_index, kRuntimePointerSize);
Alex Light9fc547a2016-03-31 14:34:33 -07002368 if (conflict_method->IsRuntimeMethod()) {
Mathieu Chartier7f98c9a2016-04-14 10:49:19 -07002369 ArtMethod* new_conflict_method = Runtime::Current()->GetClassLinker()->AddMethodToConflictTable(
2370 cls.Get(),
2371 conflict_method,
2372 interface_method,
2373 method,
2374 /*force_new_conflict_method*/false);
2375 if (new_conflict_method != conflict_method) {
Alex Light9fc547a2016-03-31 14:34:33 -07002376 // Update the IMT if we create a new conflict method. No fence needed here, as the
2377 // data is consistent.
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002378 imt->Set(imt_index,
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002379 new_conflict_method,
Andreas Gampe542451c2016-07-26 09:02:02 -07002380 kRuntimePointerSize);
Alex Light9fc547a2016-03-31 14:34:33 -07002381 }
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002382 }
2383
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002384 const void* code = method->GetEntryPointFromQuickCompiledCode();
2385
2386 // When we return, the caller will branch to this address, so it had better not be 0!
David Sehr709b0702016-10-13 09:12:37 -07002387 DCHECK(code != nullptr) << "Code was null in method: " << method->PrettyMethod()
Andreas Gampec200a4a2014-06-16 18:39:09 -07002388 << " location: " << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07002389
Andreas Gamped58342c2014-06-05 14:18:08 -07002390 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
2391 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002392}
2393
Ian Rogers848871b2013-08-05 10:56:33 -07002394} // namespace art