blob: 25b0ef5c41542a972b8a6535253d757f73ec6341 [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"
Ian Rogers848871b2013-08-05 10:56:33 -070018#include "callee_save_frame.h"
Dragos Sbirleabd136a22013-08-13 18:07:04 -070019#include "common_throws.h"
Ian Rogers848871b2013-08-05 10:56:33 -070020#include "dex_file-inl.h"
21#include "dex_instruction-inl.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070022#include "entrypoints/entrypoint_utils-inl.h"
Ian Rogers6f3dbba2014-10-14 17:41:57 -070023#include "entrypoints/runtime_asm_entrypoints.h"
Ian Rogers83883d72013-10-21 21:07:24 -070024#include "gc/accounting/card_table-inl.h"
Ian Rogers848871b2013-08-05 10:56:33 -070025#include "interpreter/interpreter.h"
Nicolas Geoffray796d6302016-03-13 22:22:31 +000026#include "linear_alloc.h"
Ian Rogerse0a02da2014-12-02 14:10:53 -080027#include "method_reference.h"
Ian Rogers848871b2013-08-05 10:56:33 -070028#include "mirror/class-inl.h"
Mathieu Chartier5f3ded42014-04-03 15:25:30 -070029#include "mirror/dex_cache-inl.h"
Mathieu Chartierfc58af42015-04-16 18:00:39 -070030#include "mirror/method.h"
Ian Rogers848871b2013-08-05 10:56:33 -070031#include "mirror/object-inl.h"
32#include "mirror/object_array-inl.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010033#include "oat_quick_method_header.h"
Andreas Gampe639bdd12015-06-03 11:22:45 -070034#include "quick_exception_handler.h"
Ian Rogers848871b2013-08-05 10:56:33 -070035#include "runtime.h"
Ian Rogers53b8b092014-03-13 23:45:53 -070036#include "scoped_thread_state_change.h"
Andreas Gampeb3025922015-09-01 14:45:00 -070037#include "stack.h"
Daniel Mihalyieb076692014-08-22 17:33:31 +020038#include "debugger.h"
Ian Rogers848871b2013-08-05 10:56:33 -070039
40namespace art {
41
42// Visits the arguments as saved to the stack by a Runtime::kRefAndArgs callee save frame.
43class QuickArgumentVisitor {
Ian Rogers936b37f2014-02-14 00:52:24 -080044 // Number of bytes for each out register in the caller method's frame.
45 static constexpr size_t kBytesStackArgLocation = 4;
Alexei Zavjalov41c507a2014-05-15 16:02:46 +070046 // Frame size in bytes of a callee-save frame for RefsAndArgs.
47 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize =
48 GetCalleeSaveFrameSize(kRuntimeISA, Runtime::kRefsAndArgs);
Ian Rogers848871b2013-08-05 10:56:33 -070049#if defined(__arm__)
50 // The callee save frame is pointed to by SP.
51 // | argN | |
52 // | ... | |
53 // | arg4 | |
54 // | arg3 spill | | Caller's frame
55 // | arg2 spill | |
56 // | arg1 spill | |
57 // | Method* | ---
58 // | LR |
Zheng Xu5667fdb2014-10-23 18:29:55 +080059 // | ... | 4x6 bytes callee saves
60 // | R3 |
61 // | R2 |
62 // | R1 |
63 // | S15 |
64 // | : |
65 // | S0 |
66 // | | 4x2 bytes padding
Ian Rogers848871b2013-08-05 10:56:33 -070067 // | Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -050068 static constexpr bool kSplitPairAcrossRegisterAndStack = kArm32QuickCodeUseSoftFloat;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +000069 static constexpr bool kAlignPairRegister = !kArm32QuickCodeUseSoftFloat;
Zheng Xu5667fdb2014-10-23 18:29:55 +080070 static constexpr bool kQuickSoftFloatAbi = kArm32QuickCodeUseSoftFloat;
71 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = !kArm32QuickCodeUseSoftFloat;
Goran Jakovljevicff734982015-08-24 12:58:55 +000072 static constexpr bool kQuickSkipOddFpRegisters = false;
Zheng Xu5667fdb2014-10-23 18:29:55 +080073 static constexpr size_t kNumQuickGprArgs = 3;
74 static constexpr size_t kNumQuickFprArgs = kArm32QuickCodeUseSoftFloat ? 0 : 16;
Andreas Gampe1a5c4062015-01-15 12:10:47 -080075 static constexpr bool kGprFprLockstep = false;
Zheng Xub551fdc2014-07-25 11:49:42 +080076 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset =
77 arm::ArmCalleeSaveFpr1Offset(Runtime::kRefsAndArgs); // Offset of first FPR arg.
78 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset =
79 arm::ArmCalleeSaveGpr1Offset(Runtime::kRefsAndArgs); // Offset of first GPR arg.
80 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset =
81 arm::ArmCalleeSaveLrOffset(Runtime::kRefsAndArgs); // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -080082 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +000083 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -080084 }
Stuart Monteithb95a5342014-03-12 13:32:32 +000085#elif defined(__aarch64__)
86 // The callee save frame is pointed to by SP.
87 // | argN | |
88 // | ... | |
89 // | arg4 | |
90 // | arg3 spill | | Caller's frame
91 // | arg2 spill | |
92 // | arg1 spill | |
93 // | Method* | ---
94 // | LR |
Zheng Xub551fdc2014-07-25 11:49:42 +080095 // | X29 |
Stuart Monteithb95a5342014-03-12 13:32:32 +000096 // | : |
Serban Constantinescu9bd88b02015-04-22 16:24:46 +010097 // | X20 |
Stuart Monteithb95a5342014-03-12 13:32:32 +000098 // | X7 |
99 // | : |
100 // | X1 |
Zheng Xub551fdc2014-07-25 11:49:42 +0800101 // | D7 |
Stuart Monteithb95a5342014-03-12 13:32:32 +0000102 // | : |
103 // | D0 |
104 // | | padding
105 // | Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500106 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000107 static constexpr bool kAlignPairRegister = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000108 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800109 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000110 static constexpr bool kQuickSkipOddFpRegisters = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000111 static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs.
112 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800113 static constexpr bool kGprFprLockstep = false;
Zheng Xub551fdc2014-07-25 11:49:42 +0800114 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset =
115 arm64::Arm64CalleeSaveFpr1Offset(Runtime::kRefsAndArgs); // Offset of first FPR arg.
116 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset =
117 arm64::Arm64CalleeSaveGpr1Offset(Runtime::kRefsAndArgs); // Offset of first GPR arg.
118 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset =
119 arm64::Arm64CalleeSaveLrOffset(Runtime::kRefsAndArgs); // Offset of return address.
Stuart Monteithb95a5342014-03-12 13:32:32 +0000120 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000121 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Stuart Monteithb95a5342014-03-12 13:32:32 +0000122 }
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800123#elif defined(__mips__) && !defined(__LP64__)
Ian Rogers848871b2013-08-05 10:56:33 -0700124 // The callee save frame is pointed to by SP.
125 // | argN | |
126 // | ... | |
127 // | arg4 | |
128 // | arg3 spill | | Caller's frame
129 // | arg2 spill | |
130 // | arg1 spill | |
131 // | Method* | ---
132 // | RA |
133 // | ... | callee saves
134 // | A3 | arg3
135 // | A2 | arg2
136 // | A1 | arg1
Goran Jakovljevicff734982015-08-24 12:58:55 +0000137 // | F15 |
138 // | F14 | f_arg1
139 // | F13 |
140 // | F12 | f_arg0
141 // | | padding
Ian Rogers848871b2013-08-05 10:56:33 -0700142 // | A0/Method* | <- sp
Goran Jakovljevicff734982015-08-24 12:58:55 +0000143 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
144 static constexpr bool kAlignPairRegister = true;
145 static constexpr bool kQuickSoftFloatAbi = false;
Zheng Xu5667fdb2014-10-23 18:29:55 +0800146 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000147 static constexpr bool kQuickSkipOddFpRegisters = true;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800148 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs.
Goran Jakovljevicff734982015-08-24 12:58:55 +0000149 static constexpr size_t kNumQuickFprArgs = 4; // 2 arguments passed in FPRs. Floats can be passed
150 // only in even numbered registers and each double
151 // occupies two registers.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800152 static constexpr bool kGprFprLockstep = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000153 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 16; // Offset of first FPR arg.
154 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 32; // Offset of first GPR arg.
155 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 76; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800156 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000157 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800158 }
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800159#elif defined(__mips__) && defined(__LP64__)
160 // The callee save frame is pointed to by SP.
161 // | argN | |
162 // | ... | |
163 // | arg4 | |
164 // | arg3 spill | | Caller's frame
165 // | arg2 spill | |
166 // | arg1 spill | |
167 // | Method* | ---
168 // | RA |
169 // | ... | callee saves
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800170 // | A7 | arg7
171 // | A6 | arg6
172 // | A5 | arg5
173 // | A4 | arg4
174 // | A3 | arg3
175 // | A2 | arg2
176 // | A1 | arg1
Goran Jakovljevicff734982015-08-24 12:58:55 +0000177 // | F19 | f_arg7
178 // | F18 | f_arg6
179 // | F17 | f_arg5
180 // | F16 | f_arg4
181 // | F15 | f_arg3
182 // | F14 | f_arg2
183 // | F13 | f_arg1
184 // | F12 | f_arg0
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800185 // | | padding
186 // | A0/Method* | <- sp
187 // NOTE: for Mip64, when A0 is skipped, F0 is also skipped.
Douglas Leungd18e0832015-02-09 15:22:26 -0800188 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800189 static constexpr bool kAlignPairRegister = false;
190 static constexpr bool kQuickSoftFloatAbi = false;
191 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000192 static constexpr bool kQuickSkipOddFpRegisters = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800193 static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs.
194 static constexpr size_t kNumQuickFprArgs = 7; // 7 arguments passed in FPRs.
195 static constexpr bool kGprFprLockstep = true;
196
197 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 24; // Offset of first FPR arg (F1).
198 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 80; // Offset of first GPR arg (A1).
199 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 200; // Offset of return address.
200 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
201 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
202 }
Ian Rogers848871b2013-08-05 10:56:33 -0700203#elif defined(__i386__)
204 // The callee save frame is pointed to by SP.
205 // | argN | |
206 // | ... | |
207 // | arg4 | |
208 // | arg3 spill | | Caller's frame
209 // | arg2 spill | |
210 // | arg1 spill | |
211 // | Method* | ---
212 // | Return |
213 // | EBP,ESI,EDI | callee saves
214 // | EBX | arg3
215 // | EDX | arg2
216 // | ECX | arg1
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000217 // | XMM3 | float arg 4
218 // | XMM2 | float arg 3
219 // | XMM1 | float arg 2
220 // | XMM0 | float arg 1
Ian Rogers848871b2013-08-05 10:56:33 -0700221 // | EAX/Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500222 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000223 static constexpr bool kAlignPairRegister = false;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000224 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800225 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000226 static constexpr bool kQuickSkipOddFpRegisters = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800227 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs.
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000228 static constexpr size_t kNumQuickFprArgs = 4; // 4 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800229 static constexpr bool kGprFprLockstep = false;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000230 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 4; // Offset of first FPR arg.
231 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 4 + 4*8; // Offset of first GPR arg.
232 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 28 + 4*8; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800233 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000234 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800235 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800236#elif defined(__x86_64__)
Ian Rogers936b37f2014-02-14 00:52:24 -0800237 // The callee save frame is pointed to by SP.
238 // | argN | |
239 // | ... | |
240 // | reg. arg spills | | Caller's frame
241 // | Method* | ---
242 // | Return |
243 // | R15 | callee save
244 // | R14 | callee save
245 // | R13 | callee save
246 // | R12 | callee save
247 // | R9 | arg5
248 // | R8 | arg4
249 // | RSI/R6 | arg1
250 // | RBP/R5 | callee save
251 // | RBX/R3 | callee save
252 // | RDX/R2 | arg2
253 // | RCX/R1 | arg3
254 // | XMM7 | float arg 8
255 // | XMM6 | float arg 7
256 // | XMM5 | float arg 6
257 // | XMM4 | float arg 5
258 // | XMM3 | float arg 4
259 // | XMM2 | float arg 3
260 // | XMM1 | float arg 2
261 // | XMM0 | float arg 1
262 // | Padding |
263 // | RDI/Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500264 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000265 static constexpr bool kAlignPairRegister = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800266 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800267 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000268 static constexpr bool kQuickSkipOddFpRegisters = false;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700269 static constexpr size_t kNumQuickGprArgs = 5; // 5 arguments passed in GPRs.
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700270 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800271 static constexpr bool kGprFprLockstep = false;
Ian Rogers936b37f2014-02-14 00:52:24 -0800272 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 16; // Offset of first FPR arg.
Serguei Katkovc3801912014-07-08 17:21:53 +0700273 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 80 + 4*8; // Offset of first GPR arg.
274 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 168 + 4*8; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800275 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
276 switch (gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000277 case 0: return (4 * GetBytesPerGprSpillLocation(kRuntimeISA));
278 case 1: return (1 * GetBytesPerGprSpillLocation(kRuntimeISA));
279 case 2: return (0 * GetBytesPerGprSpillLocation(kRuntimeISA));
280 case 3: return (5 * GetBytesPerGprSpillLocation(kRuntimeISA));
281 case 4: return (6 * GetBytesPerGprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800282 default:
Andreas Gampec200a4a2014-06-16 18:39:09 -0700283 LOG(FATAL) << "Unexpected GPR index: " << gpr_index;
284 return 0;
Ian Rogers936b37f2014-02-14 00:52:24 -0800285 }
286 }
Ian Rogers848871b2013-08-05 10:56:33 -0700287#else
288#error "Unsupported architecture"
Ian Rogers848871b2013-08-05 10:56:33 -0700289#endif
290
Ian Rogers936b37f2014-02-14 00:52:24 -0800291 public:
Sebastien Hertza836bc92014-11-25 16:30:53 +0100292 // Special handling for proxy methods. Proxy methods are instance methods so the
293 // 'this' object is the 1st argument. They also have the same frame layout as the
294 // kRefAndArgs runtime method. Since 'this' is a reference, it is located in the
295 // 1st GPR.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700296 static mirror::Object* GetProxyThisObject(ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -0700297 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000298 CHECK((*sp)->IsProxyMethod());
Sebastien Hertza836bc92014-11-25 16:30:53 +0100299 CHECK_GT(kNumQuickGprArgs, 0u);
300 constexpr uint32_t kThisGprIndex = 0u; // 'this' is in the 1st GPR.
301 size_t this_arg_offset = kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset +
302 GprIndexToGprOffset(kThisGprIndex);
303 uint8_t* this_arg_address = reinterpret_cast<uint8_t*>(sp) + this_arg_offset;
304 return reinterpret_cast<StackReference<mirror::Object>*>(this_arg_address)->AsMirrorPtr();
305 }
306
Mathieu Chartier90443472015-07-16 20:32:27 -0700307 static ArtMethod* GetCallingMethod(ArtMethod** sp) SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700308 DCHECK((*sp)->IsCalleeSaveMethod());
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +0100309 return GetCalleeSaveMethodCaller(sp, Runtime::kRefsAndArgs);
310 }
311
Mathieu Chartier90443472015-07-16 20:32:27 -0700312 static ArtMethod* GetOuterMethod(ArtMethod** sp) SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700313 DCHECK((*sp)->IsCalleeSaveMethod());
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100314 uint8_t* previous_sp =
315 reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700316 return *reinterpret_cast<ArtMethod**>(previous_sp);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100317 }
318
Mathieu Chartier90443472015-07-16 20:32:27 -0700319 static uint32_t GetCallingDexPc(ArtMethod** sp) SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700320 DCHECK((*sp)->IsCalleeSaveMethod());
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100321 const size_t callee_frame_size = GetCalleeSaveFrameSize(kRuntimeISA, Runtime::kRefsAndArgs);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700322 ArtMethod** caller_sp = reinterpret_cast<ArtMethod**>(
323 reinterpret_cast<uintptr_t>(sp) + callee_frame_size);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100324 uintptr_t outer_pc = QuickArgumentVisitor::GetCallingPc(sp);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100325 const OatQuickMethodHeader* current_code = (*caller_sp)->GetOatQuickMethodHeader(outer_pc);
326 uintptr_t outer_pc_offset = current_code->NativeQuickPcOffset(outer_pc);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100327
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100328 if (current_code->IsOptimized()) {
329 CodeInfo code_info = current_code->GetOptimizedCodeInfo();
David Srbecky09ed0982016-02-12 21:58:43 +0000330 CodeInfoEncoding encoding = code_info.ExtractEncoding();
David Brazdilf677ebf2015-05-29 16:29:43 +0100331 StackMap stack_map = code_info.GetStackMapForNativePcOffset(outer_pc_offset, encoding);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100332 DCHECK(stack_map.IsValid());
David Srbecky09ed0982016-02-12 21:58:43 +0000333 if (stack_map.HasInlineInfo(encoding.stack_map_encoding)) {
David Brazdilf677ebf2015-05-29 16:29:43 +0100334 InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map, encoding);
David Srbecky61b28a12016-02-25 21:55:03 +0000335 return inline_info.GetDexPcAtDepth(encoding.inline_info_encoding,
336 inline_info.GetDepth(encoding.inline_info_encoding)-1);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100337 } else {
David Srbecky09ed0982016-02-12 21:58:43 +0000338 return stack_map.GetDexPc(encoding.stack_map_encoding);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100339 }
340 } else {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100341 return current_code->ToDexPc(*caller_sp, outer_pc);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100342 }
Ian Rogers848871b2013-08-05 10:56:33 -0700343 }
344
Ian Rogers936b37f2014-02-14 00:52:24 -0800345 // For the given quick ref and args quick frame, return the caller's PC.
Mathieu Chartier90443472015-07-16 20:32:27 -0700346 static uintptr_t GetCallingPc(ArtMethod** sp) SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700347 DCHECK((*sp)->IsCalleeSaveMethod());
Ian Rogers13735952014-10-08 12:43:28 -0700348 uint8_t* lr = reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_LrOffset;
Ian Rogers848871b2013-08-05 10:56:33 -0700349 return *reinterpret_cast<uintptr_t*>(lr);
350 }
351
Mathieu Chartiere401d142015-04-22 13:56:20 -0700352 QuickArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty,
Mathieu Chartier90443472015-07-16 20:32:27 -0700353 uint32_t shorty_len) SHARED_REQUIRES(Locks::mutator_lock_) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700354 is_static_(is_static), shorty_(shorty), shorty_len_(shorty_len),
Ian Rogers13735952014-10-08 12:43:28 -0700355 gpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset),
356 fpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset),
357 stack_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize
Mathieu Chartiere401d142015-04-22 13:56:20 -0700358 + sizeof(ArtMethod*)), // Skip ArtMethod*.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800359 gpr_index_(0), fpr_index_(0), fpr_double_index_(0), stack_index_(0),
360 cur_type_(Primitive::kPrimVoid), is_split_long_or_double_(false) {
Andreas Gampe575e78c2014-11-03 23:41:03 -0800361 static_assert(kQuickSoftFloatAbi == (kNumQuickFprArgs == 0),
362 "Number of Quick FPR arguments unexpected");
363 static_assert(!(kQuickSoftFloatAbi && kQuickDoubleRegAlignedFloatBackFilled),
364 "Double alignment unexpected");
Zheng Xu5667fdb2014-10-23 18:29:55 +0800365 // For register alignment, we want to assume that counters(fpr_double_index_) are even if the
366 // next register is even.
Andreas Gampe575e78c2014-11-03 23:41:03 -0800367 static_assert(!kQuickDoubleRegAlignedFloatBackFilled || kNumQuickFprArgs % 2 == 0,
368 "Number of Quick FPR arguments not even");
Mathieu Chartiere401d142015-04-22 13:56:20 -0700369 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), sizeof(void*));
Zheng Xu5667fdb2014-10-23 18:29:55 +0800370 }
Ian Rogers848871b2013-08-05 10:56:33 -0700371
372 virtual ~QuickArgumentVisitor() {}
373
374 virtual void Visit() = 0;
375
Ian Rogers936b37f2014-02-14 00:52:24 -0800376 Primitive::Type GetParamPrimitiveType() const {
377 return cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700378 }
379
Ian Rogers13735952014-10-08 12:43:28 -0700380 uint8_t* GetParamAddress() const {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800381 if (!kQuickSoftFloatAbi) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800382 Primitive::Type type = GetParamPrimitiveType();
383 if (UNLIKELY((type == Primitive::kPrimDouble) || (type == Primitive::kPrimFloat))) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800384 if (type == Primitive::kPrimDouble && kQuickDoubleRegAlignedFloatBackFilled) {
385 if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) {
386 return fpr_args_ + (fpr_double_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
387 }
388 } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000389 return fpr_args_ + (fpr_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800390 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700391 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers936b37f2014-02-14 00:52:24 -0800392 }
393 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800394 if (gpr_index_ < kNumQuickGprArgs) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800395 return gpr_args_ + GprIndexToGprOffset(gpr_index_);
396 }
397 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700398 }
399
400 bool IsSplitLongOrDouble() const {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700401 if ((GetBytesPerGprSpillLocation(kRuntimeISA) == 4) ||
402 (GetBytesPerFprSpillLocation(kRuntimeISA) == 4)) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800403 return is_split_long_or_double_;
404 } else {
405 return false; // An optimization for when GPR and FPRs are 64bit.
406 }
Ian Rogers848871b2013-08-05 10:56:33 -0700407 }
408
Ian Rogers936b37f2014-02-14 00:52:24 -0800409 bool IsParamAReference() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700410 return GetParamPrimitiveType() == Primitive::kPrimNot;
411 }
412
Ian Rogers936b37f2014-02-14 00:52:24 -0800413 bool IsParamALongOrDouble() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700414 Primitive::Type type = GetParamPrimitiveType();
415 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
416 }
417
418 uint64_t ReadSplitLongParam() const {
Nicolas Geoffray425f2392015-01-08 14:52:29 +0000419 // The splitted long is always available through the stack.
420 return *reinterpret_cast<uint64_t*>(stack_args_
421 + stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700422 }
423
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800424 void IncGprIndex() {
425 gpr_index_++;
426 if (kGprFprLockstep) {
427 fpr_index_++;
428 }
429 }
430
431 void IncFprIndex() {
432 fpr_index_++;
433 if (kGprFprLockstep) {
434 gpr_index_++;
435 }
436 }
437
Mathieu Chartier90443472015-07-16 20:32:27 -0700438 void VisitArguments() SHARED_REQUIRES(Locks::mutator_lock_) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800439 // (a) 'stack_args_' should point to the first method's argument
440 // (b) whatever the argument type it is, the 'stack_index_' should
441 // be moved forward along with every visiting.
Ian Rogers936b37f2014-02-14 00:52:24 -0800442 gpr_index_ = 0;
443 fpr_index_ = 0;
Zheng Xu5667fdb2014-10-23 18:29:55 +0800444 if (kQuickDoubleRegAlignedFloatBackFilled) {
445 fpr_double_index_ = 0;
446 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800447 stack_index_ = 0;
448 if (!is_static_) { // Handle this.
449 cur_type_ = Primitive::kPrimNot;
450 is_split_long_or_double_ = false;
Ian Rogers848871b2013-08-05 10:56:33 -0700451 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800452 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800453 if (kNumQuickGprArgs > 0) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800454 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800455 }
Ian Rogers848871b2013-08-05 10:56:33 -0700456 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800457 for (uint32_t shorty_index = 1; shorty_index < shorty_len_; ++shorty_index) {
458 cur_type_ = Primitive::GetType(shorty_[shorty_index]);
459 switch (cur_type_) {
460 case Primitive::kPrimNot:
461 case Primitive::kPrimBoolean:
462 case Primitive::kPrimByte:
463 case Primitive::kPrimChar:
464 case Primitive::kPrimShort:
465 case Primitive::kPrimInt:
466 is_split_long_or_double_ = false;
467 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800468 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800469 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800470 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800471 }
472 break;
473 case Primitive::kPrimFloat:
474 is_split_long_or_double_ = false;
475 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800476 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800477 if (kQuickSoftFloatAbi) {
478 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800479 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800480 }
481 } else {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800482 if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800483 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800484 if (kQuickDoubleRegAlignedFloatBackFilled) {
485 // Double should not overlap with float.
486 // For example, if fpr_index_ = 3, fpr_double_index_ should be at least 4.
487 fpr_double_index_ = std::max(fpr_double_index_, RoundUp(fpr_index_, 2));
488 // Float should not overlap with double.
489 if (fpr_index_ % 2 == 0) {
490 fpr_index_ = std::max(fpr_double_index_, fpr_index_);
491 }
Goran Jakovljevicff734982015-08-24 12:58:55 +0000492 } else if (kQuickSkipOddFpRegisters) {
493 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800494 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800495 }
496 }
497 break;
498 case Primitive::kPrimDouble:
499 case Primitive::kPrimLong:
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800500 if (kQuickSoftFloatAbi || (cur_type_ == Primitive::kPrimLong)) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000501 if (cur_type_ == Primitive::kPrimLong && kAlignPairRegister && gpr_index_ == 0) {
Goran Jakovljevicff734982015-08-24 12:58:55 +0000502 // Currently, this is only for ARM and MIPS, where the first available parameter
503 // register is R1 (on ARM) or A1 (on MIPS). So we skip it, and use R2 (on ARM) or
504 // A2 (on MIPS) instead.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800505 IncGprIndex();
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000506 }
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000507 is_split_long_or_double_ = (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) &&
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800508 ((gpr_index_ + 1) == kNumQuickGprArgs);
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500509 if (!kSplitPairAcrossRegisterAndStack && is_split_long_or_double_) {
510 // We don't want to split this. Pass over this register.
511 gpr_index_++;
512 is_split_long_or_double_ = false;
513 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800514 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800515 if (kBytesStackArgLocation == 4) {
516 stack_index_+= 2;
517 } else {
518 CHECK_EQ(kBytesStackArgLocation, 8U);
519 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800520 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700521 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800522 IncGprIndex();
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000523 if (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700524 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800525 IncGprIndex();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700526 }
527 }
528 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800529 } else {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000530 is_split_long_or_double_ = (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) &&
Zheng Xu5667fdb2014-10-23 18:29:55 +0800531 ((fpr_index_ + 1) == kNumQuickFprArgs) && !kQuickDoubleRegAlignedFloatBackFilled;
Ian Rogers936b37f2014-02-14 00:52:24 -0800532 Visit();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700533 if (kBytesStackArgLocation == 4) {
534 stack_index_+= 2;
Ian Rogers936b37f2014-02-14 00:52:24 -0800535 } else {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700536 CHECK_EQ(kBytesStackArgLocation, 8U);
537 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800538 }
Zheng Xu5667fdb2014-10-23 18:29:55 +0800539 if (kQuickDoubleRegAlignedFloatBackFilled) {
540 if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) {
541 fpr_double_index_ += 2;
542 // Float should not overlap with double.
543 if (fpr_index_ % 2 == 0) {
544 fpr_index_ = std::max(fpr_double_index_, fpr_index_);
545 }
546 }
547 } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800548 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800549 if (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) {
550 if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800551 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800552 }
553 }
554 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800555 }
556 break;
557 default:
558 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty_;
559 }
Ian Rogers848871b2013-08-05 10:56:33 -0700560 }
561 }
562
Andreas Gampec200a4a2014-06-16 18:39:09 -0700563 protected:
Ian Rogers848871b2013-08-05 10:56:33 -0700564 const bool is_static_;
565 const char* const shorty_;
566 const uint32_t shorty_len_;
Andreas Gampec200a4a2014-06-16 18:39:09 -0700567
568 private:
Ian Rogers13735952014-10-08 12:43:28 -0700569 uint8_t* const gpr_args_; // Address of GPR arguments in callee save frame.
570 uint8_t* const fpr_args_; // Address of FPR arguments in callee save frame.
571 uint8_t* const stack_args_; // Address of stack arguments in caller's frame.
Ian Rogers936b37f2014-02-14 00:52:24 -0800572 uint32_t gpr_index_; // Index into spilled GPRs.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800573 // Index into spilled FPRs.
574 // In case kQuickDoubleRegAlignedFloatBackFilled, it may index a hole while fpr_double_index_
575 // holds a higher register number.
576 uint32_t fpr_index_;
577 // Index into spilled FPRs for aligned double.
578 // Only used when kQuickDoubleRegAlignedFloatBackFilled. Next available double register indexed in
579 // terms of singles, may be behind fpr_index.
580 uint32_t fpr_double_index_;
Ian Rogers936b37f2014-02-14 00:52:24 -0800581 uint32_t stack_index_; // Index into arguments on the stack.
582 // The current type of argument during VisitArguments.
583 Primitive::Type cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700584 // Does a 64bit parameter straddle the register and stack arguments?
585 bool is_split_long_or_double_;
586};
587
Sebastien Hertza836bc92014-11-25 16:30:53 +0100588// Returns the 'this' object of a proxy method. This function is only used by StackVisitor. It
589// allows to use the QuickArgumentVisitor constants without moving all the code in its own module.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700590extern "C" mirror::Object* artQuickGetProxyThisObject(ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -0700591 SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertza836bc92014-11-25 16:30:53 +0100592 return QuickArgumentVisitor::GetProxyThisObject(sp);
593}
594
Ian Rogers848871b2013-08-05 10:56:33 -0700595// Visits arguments on the stack placing them into the shadow frame.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800596class BuildQuickShadowFrameVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700597 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700598 BuildQuickShadowFrameVisitor(ArtMethod** sp, bool is_static, const char* shorty,
599 uint32_t shorty_len, ShadowFrame* sf, size_t first_arg_reg) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700600 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), sf_(sf), cur_reg_(first_arg_reg) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700601
Mathieu Chartier90443472015-07-16 20:32:27 -0700602 void Visit() SHARED_REQUIRES(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700603
604 private:
Ian Rogers936b37f2014-02-14 00:52:24 -0800605 ShadowFrame* const sf_;
606 uint32_t cur_reg_;
Ian Rogers848871b2013-08-05 10:56:33 -0700607
Dragos Sbirleabd136a22013-08-13 18:07:04 -0700608 DISALLOW_COPY_AND_ASSIGN(BuildQuickShadowFrameVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700609};
610
Andreas Gampec200a4a2014-06-16 18:39:09 -0700611void BuildQuickShadowFrameVisitor::Visit() {
Ian Rogers9758f792014-03-13 09:02:55 -0700612 Primitive::Type type = GetParamPrimitiveType();
613 switch (type) {
614 case Primitive::kPrimLong: // Fall-through.
615 case Primitive::kPrimDouble:
616 if (IsSplitLongOrDouble()) {
617 sf_->SetVRegLong(cur_reg_, ReadSplitLongParam());
618 } else {
619 sf_->SetVRegLong(cur_reg_, *reinterpret_cast<jlong*>(GetParamAddress()));
620 }
621 ++cur_reg_;
622 break;
623 case Primitive::kPrimNot: {
624 StackReference<mirror::Object>* stack_ref =
625 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
626 sf_->SetVRegReference(cur_reg_, stack_ref->AsMirrorPtr());
627 }
628 break;
629 case Primitive::kPrimBoolean: // Fall-through.
630 case Primitive::kPrimByte: // Fall-through.
631 case Primitive::kPrimChar: // Fall-through.
632 case Primitive::kPrimShort: // Fall-through.
633 case Primitive::kPrimInt: // Fall-through.
634 case Primitive::kPrimFloat:
635 sf_->SetVReg(cur_reg_, *reinterpret_cast<jint*>(GetParamAddress()));
636 break;
637 case Primitive::kPrimVoid:
638 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700639 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -0700640 }
641 ++cur_reg_;
642}
643
Mathieu Chartiere401d142015-04-22 13:56:20 -0700644extern "C" uint64_t artQuickToInterpreterBridge(ArtMethod* method, Thread* self, ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -0700645 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers848871b2013-08-05 10:56:33 -0700646 // Ensure we don't get thread suspension until the object arguments are safely in the shadow
647 // frame.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700648 ScopedQuickEntrypointChecks sqec(self);
Ian Rogers848871b2013-08-05 10:56:33 -0700649
Alex Light9139e002015-10-09 15:59:48 -0700650 if (UNLIKELY(!method->IsInvokable())) {
651 method->ThrowInvocationTimeError();
Ian Rogers848871b2013-08-05 10:56:33 -0700652 return 0;
Andreas Gampe639bdd12015-06-03 11:22:45 -0700653 }
654
655 JValue tmp_value;
656 ShadowFrame* deopt_frame = self->PopStackedShadowFrame(
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700657 StackedShadowFrameType::kDeoptimizationShadowFrame, false);
Andreas Gampe639bdd12015-06-03 11:22:45 -0700658 ManagedStack fragment;
659
660 DCHECK(!method->IsNative()) << PrettyMethod(method);
661 uint32_t shorty_len = 0;
Alex Lighte9dd04f2016-03-16 16:09:45 -0700662 ArtMethod* non_proxy_method = method->GetInterfaceMethodIfProxy(sizeof(void*));
663 const DexFile::CodeItem* code_item = non_proxy_method->GetCodeItem();
664 DCHECK(code_item != nullptr) << PrettyMethod(method);
Andreas Gampe639bdd12015-06-03 11:22:45 -0700665 const char* shorty = non_proxy_method->GetShorty(&shorty_len);
666
667 JValue result;
668
669 if (deopt_frame != nullptr) {
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700670 // Coming from partial-fragment deopt.
Andreas Gampe639bdd12015-06-03 11:22:45 -0700671
672 if (kIsDebugBuild) {
673 // Sanity-check: are the methods as expected? We check that the last shadow frame (the bottom
674 // of the call-stack) corresponds to the called method.
675 ShadowFrame* linked = deopt_frame;
676 while (linked->GetLink() != nullptr) {
677 linked = linked->GetLink();
678 }
679 CHECK_EQ(method, linked->GetMethod()) << PrettyMethod(method) << " "
680 << PrettyMethod(linked->GetMethod());
681 }
682
683 if (VLOG_IS_ON(deopt)) {
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700684 // Print out the stack to verify that it was a partial-fragment deopt.
Andreas Gampe639bdd12015-06-03 11:22:45 -0700685 LOG(INFO) << "Continue-ing from deopt. Stack is:";
686 QuickExceptionHandler::DumpFramesWithType(self, true);
687 }
688
689 mirror::Throwable* pending_exception = nullptr;
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100690 bool from_code = false;
691 self->PopDeoptimizationContext(&result, &pending_exception, /* out */ &from_code);
Andreas Gampe639bdd12015-06-03 11:22:45 -0700692
693 // Push a transition back into managed code onto the linked list in thread.
694 self->PushManagedStackFragment(&fragment);
695
696 // Ensure that the stack is still in order.
697 if (kIsDebugBuild) {
698 class DummyStackVisitor : public StackVisitor {
699 public:
700 explicit DummyStackVisitor(Thread* self_in) SHARED_REQUIRES(Locks::mutator_lock_)
701 : StackVisitor(self_in, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {}
702
703 bool VisitFrame() OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
704 // Nothing to do here. In a debug build, SanityCheckFrame will do the work in the walking
705 // logic. Just always say we want to continue.
706 return true;
707 }
708 };
709 DummyStackVisitor dsv(self);
710 dsv.WalkStack();
711 }
712
713 // Restore the exception that was pending before deoptimization then interpret the
714 // deoptimized frames.
715 if (pending_exception != nullptr) {
716 self->SetException(pending_exception);
717 }
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100718 interpreter::EnterInterpreterFromDeoptimize(self, deopt_frame, from_code, &result);
Ian Rogers848871b2013-08-05 10:56:33 -0700719 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -0700720 const char* old_cause = self->StartAssertNoThreadSuspension(
721 "Building interpreter shadow frame");
Ian Rogers848871b2013-08-05 10:56:33 -0700722 uint16_t num_regs = code_item->registers_size_;
Andreas Gampec200a4a2014-06-16 18:39:09 -0700723 // No last shadow coming from quick.
Andreas Gampeb3025922015-09-01 14:45:00 -0700724 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
Andreas Gampe03ec9302015-08-27 17:41:47 -0700725 CREATE_SHADOW_FRAME(num_regs, /* link */ nullptr, method, /* dex pc */ 0);
Andreas Gampeb3025922015-09-01 14:45:00 -0700726 ShadowFrame* shadow_frame = shadow_frame_unique_ptr.get();
Ian Rogers848871b2013-08-05 10:56:33 -0700727 size_t first_arg_reg = code_item->registers_size_ - code_item->ins_size_;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700728 BuildQuickShadowFrameVisitor shadow_frame_builder(sp, method->IsStatic(), shorty, shorty_len,
Ian Rogers936b37f2014-02-14 00:52:24 -0800729 shadow_frame, first_arg_reg);
Ian Rogers848871b2013-08-05 10:56:33 -0700730 shadow_frame_builder.VisitArguments();
Ian Rogerse94652f2014-12-02 11:13:19 -0800731 const bool needs_initialization =
732 method->IsStatic() && !method->GetDeclaringClass()->IsInitialized();
Ian Rogers848871b2013-08-05 10:56:33 -0700733 // Push a transition back into managed code onto the linked list in thread.
Ian Rogers848871b2013-08-05 10:56:33 -0700734 self->PushManagedStackFragment(&fragment);
735 self->PushShadowFrame(shadow_frame);
736 self->EndAssertNoThreadSuspension(old_cause);
737
Ian Rogerse94652f2014-12-02 11:13:19 -0800738 if (needs_initialization) {
Ian Rogers848871b2013-08-05 10:56:33 -0700739 // Ensure static method's class is initialized.
Ian Rogerse94652f2014-12-02 11:13:19 -0800740 StackHandleScope<1> hs(self);
741 Handle<mirror::Class> h_class(hs.NewHandle(shadow_frame->GetMethod()->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700742 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
Ian Rogerse94652f2014-12-02 11:13:19 -0800743 DCHECK(Thread::Current()->IsExceptionPending()) << PrettyMethod(shadow_frame->GetMethod());
Ian Rogers848871b2013-08-05 10:56:33 -0700744 self->PopManagedStackFragment(fragment);
745 return 0;
746 }
747 }
Daniel Mihalyieb076692014-08-22 17:33:31 +0200748
Andreas Gampe639bdd12015-06-03 11:22:45 -0700749 result = interpreter::EnterInterpreterFromEntryPoint(self, code_item, shadow_frame);
Ian Rogers848871b2013-08-05 10:56:33 -0700750 }
Andreas Gampe639bdd12015-06-03 11:22:45 -0700751
752 // Pop transition.
753 self->PopManagedStackFragment(fragment);
754
755 // Request a stack deoptimization if needed
756 ArtMethod* caller = QuickArgumentVisitor::GetCallingMethod(sp);
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700757 uintptr_t caller_pc = QuickArgumentVisitor::GetCallingPc(sp);
758 if (UNLIKELY(Dbg::IsForcedInterpreterNeededForUpcall(self, caller) &&
759 Runtime::Current()->IsDeoptimizeable(caller_pc))) {
Andreas Gampe639bdd12015-06-03 11:22:45 -0700760 // Push the context of the deoptimization stack so we can restore the return value and the
761 // exception before executing the deoptimized frames.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100762 self->PushDeoptimizationContext(
763 result, shorty[0] == 'L', /* from_code */ false, self->GetException());
Andreas Gampe639bdd12015-06-03 11:22:45 -0700764
765 // Set special exception to cause deoptimization.
766 self->SetException(Thread::GetDeoptimizationException());
767 }
768
769 // No need to restore the args since the method has already been run by the interpreter.
770 return result.GetJ();
Ian Rogers848871b2013-08-05 10:56:33 -0700771}
772
773// Visits arguments on the stack placing them into the args vector, Object* arguments are converted
774// to jobjects.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800775class BuildQuickArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700776 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700777 BuildQuickArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty, uint32_t shorty_len,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700778 ScopedObjectAccessUnchecked* soa, std::vector<jvalue>* args) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700779 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa), args_(args) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700780
Mathieu Chartier90443472015-07-16 20:32:27 -0700781 void Visit() SHARED_REQUIRES(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700782
Mathieu Chartier90443472015-07-16 20:32:27 -0700783 void FixupReferences() SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800784
Ian Rogers848871b2013-08-05 10:56:33 -0700785 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700786 ScopedObjectAccessUnchecked* const soa_;
787 std::vector<jvalue>* const args_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800788 // References which we must update when exiting in case the GC moved the objects.
Ian Rogers700a4022014-05-19 16:49:03 -0700789 std::vector<std::pair<jobject, StackReference<mirror::Object>*>> references_;
Ian Rogers9758f792014-03-13 09:02:55 -0700790
Ian Rogers848871b2013-08-05 10:56:33 -0700791 DISALLOW_COPY_AND_ASSIGN(BuildQuickArgumentVisitor);
792};
793
Ian Rogers9758f792014-03-13 09:02:55 -0700794void BuildQuickArgumentVisitor::Visit() {
795 jvalue val;
796 Primitive::Type type = GetParamPrimitiveType();
797 switch (type) {
798 case Primitive::kPrimNot: {
799 StackReference<mirror::Object>* stack_ref =
800 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
801 val.l = soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
802 references_.push_back(std::make_pair(val.l, stack_ref));
803 break;
804 }
805 case Primitive::kPrimLong: // Fall-through.
806 case Primitive::kPrimDouble:
807 if (IsSplitLongOrDouble()) {
808 val.j = ReadSplitLongParam();
809 } else {
810 val.j = *reinterpret_cast<jlong*>(GetParamAddress());
811 }
812 break;
813 case Primitive::kPrimBoolean: // Fall-through.
814 case Primitive::kPrimByte: // Fall-through.
815 case Primitive::kPrimChar: // Fall-through.
816 case Primitive::kPrimShort: // Fall-through.
817 case Primitive::kPrimInt: // Fall-through.
818 case Primitive::kPrimFloat:
819 val.i = *reinterpret_cast<jint*>(GetParamAddress());
820 break;
821 case Primitive::kPrimVoid:
822 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700823 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -0700824 }
825 args_->push_back(val);
826}
827
828void BuildQuickArgumentVisitor::FixupReferences() {
829 // Fixup any references which may have changed.
830 for (const auto& pair : references_) {
831 pair.second->Assign(soa_->Decode<mirror::Object*>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -0700832 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -0700833 }
834}
835
Ian Rogers848871b2013-08-05 10:56:33 -0700836// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
837// which is responsible for recording callee save registers. We explicitly place into jobjects the
838// incoming reference arguments (so they survive GC). We invoke the invocation handler, which is a
839// field within the proxy object, which will box the primitive arguments and deal with error cases.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700840extern "C" uint64_t artQuickProxyInvokeHandler(
841 ArtMethod* proxy_method, mirror::Object* receiver, Thread* self, ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -0700842 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000843 DCHECK(proxy_method->IsProxyMethod()) << PrettyMethod(proxy_method);
844 DCHECK(receiver->GetClass()->IsProxyClass()) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700845 // Ensure we don't get thread suspension until the object arguments are safely in jobjects.
846 const char* old_cause =
847 self->StartAssertNoThreadSuspension("Adding to IRT proxy object arguments");
848 // Register the top of the managed stack, making stack crawlable.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700849 DCHECK_EQ((*sp), proxy_method) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700850 self->VerifyStack();
851 // Start new JNI local reference state.
852 JNIEnvExt* env = self->GetJniEnv();
853 ScopedObjectAccessUnchecked soa(env);
854 ScopedJniEnvLocalRefState env_state(env);
855 // Create local ref. copies of proxy method and the receiver.
856 jobject rcvr_jobj = soa.AddLocalReference<jobject>(receiver);
857
858 // Placing arguments into args vector and remove the receiver.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700859 ArtMethod* non_proxy_method = proxy_method->GetInterfaceMethodIfProxy(sizeof(void*));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700860 CHECK(!non_proxy_method->IsStatic()) << PrettyMethod(proxy_method) << " "
Andreas Gampec200a4a2014-06-16 18:39:09 -0700861 << PrettyMethod(non_proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700862 std::vector<jvalue> args;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700863 uint32_t shorty_len = 0;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700864 const char* shorty = non_proxy_method->GetShorty(&shorty_len);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700865 BuildQuickArgumentVisitor local_ref_visitor(sp, false, shorty, shorty_len, &soa, &args);
Brian Carlstromd3633d52013-08-20 21:06:26 -0700866
Ian Rogers848871b2013-08-05 10:56:33 -0700867 local_ref_visitor.VisitArguments();
Brian Carlstromd3633d52013-08-20 21:06:26 -0700868 DCHECK_GT(args.size(), 0U) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700869 args.erase(args.begin());
870
871 // Convert proxy method into expected interface method.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700872 ArtMethod* interface_method = proxy_method->FindOverriddenMethod(sizeof(void*));
Ian Rogerse0a02da2014-12-02 14:10:53 -0800873 DCHECK(interface_method != nullptr) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700874 DCHECK(!interface_method->IsProxyMethod()) << PrettyMethod(interface_method);
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700875 self->EndAssertNoThreadSuspension(old_cause);
876 jobject interface_method_jobj = soa.AddLocalReference<jobject>(
877 mirror::Method::CreateFromArtMethod(soa.Self(), interface_method));
Ian Rogers848871b2013-08-05 10:56:33 -0700878
879 // All naked Object*s should now be in jobjects, so its safe to go into the main invoke code
880 // that performs allocations.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700881 JValue result = InvokeProxyInvocationHandler(soa, shorty, rcvr_jobj, interface_method_jobj, args);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800882 // Restore references which might have moved.
883 local_ref_visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -0700884 return result.GetJ();
885}
886
887// Read object references held in arguments from quick frames and place in a JNI local references,
888// so they don't get garbage collected.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800889class RememberForGcArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700890 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700891 RememberForGcArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty,
892 uint32_t shorty_len, ScopedObjectAccessUnchecked* soa) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700893 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700894
Mathieu Chartier90443472015-07-16 20:32:27 -0700895 void Visit() SHARED_REQUIRES(Locks::mutator_lock_) OVERRIDE;
Mathieu Chartier07d447b2013-09-26 11:57:43 -0700896
Mathieu Chartier90443472015-07-16 20:32:27 -0700897 void FixupReferences() SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers848871b2013-08-05 10:56:33 -0700898
899 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700900 ScopedObjectAccessUnchecked* const soa_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800901 // References which we must update when exiting in case the GC moved the objects.
Andreas Gampec200a4a2014-06-16 18:39:09 -0700902 std::vector<std::pair<jobject, StackReference<mirror::Object>*> > references_;
903
Mathieu Chartier590fee92013-09-13 13:46:47 -0700904 DISALLOW_COPY_AND_ASSIGN(RememberForGcArgumentVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700905};
906
Ian Rogers9758f792014-03-13 09:02:55 -0700907void RememberForGcArgumentVisitor::Visit() {
908 if (IsParamAReference()) {
909 StackReference<mirror::Object>* stack_ref =
910 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
911 jobject reference =
912 soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
913 references_.push_back(std::make_pair(reference, stack_ref));
914 }
915}
916
917void RememberForGcArgumentVisitor::FixupReferences() {
918 // Fixup any references which may have changed.
919 for (const auto& pair : references_) {
920 pair.second->Assign(soa_->Decode<mirror::Object*>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -0700921 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -0700922 }
923}
924
Ian Rogers848871b2013-08-05 10:56:33 -0700925// Lazily resolve a method for quick. Called by stub code.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700926extern "C" const void* artQuickResolutionTrampoline(
927 ArtMethod* called, mirror::Object* receiver, Thread* self, ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -0700928 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe3b45ef22015-05-26 21:34:09 -0700929 // The resolution trampoline stashes the resolved method into the callee-save frame to transport
930 // it. Thus, when exiting, the stack cannot be verified (as the resolved method most likely
931 // does not have the same stack layout as the callee-save method).
932 ScopedQuickEntrypointChecks sqec(self, kIsDebugBuild, false);
Ian Rogers848871b2013-08-05 10:56:33 -0700933 // Start new JNI local reference state
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800934 JNIEnvExt* env = self->GetJniEnv();
Ian Rogers848871b2013-08-05 10:56:33 -0700935 ScopedObjectAccessUnchecked soa(env);
936 ScopedJniEnvLocalRefState env_state(env);
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800937 const char* old_cause = self->StartAssertNoThreadSuspension("Quick method resolution set up");
Ian Rogers848871b2013-08-05 10:56:33 -0700938
939 // Compute details about the called method (avoid GCs)
940 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Ian Rogers848871b2013-08-05 10:56:33 -0700941 InvokeType invoke_type;
Ian Rogerse0a02da2014-12-02 14:10:53 -0800942 MethodReference called_method(nullptr, 0);
943 const bool called_method_known_on_entry = !called->IsRuntimeMethod();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700944 ArtMethod* caller = nullptr;
Ian Rogerse0a02da2014-12-02 14:10:53 -0800945 if (!called_method_known_on_entry) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +0100946 caller = QuickArgumentVisitor::GetCallingMethod(sp);
947 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
Ian Rogers848871b2013-08-05 10:56:33 -0700948 const DexFile::CodeItem* code;
Ian Rogerse0a02da2014-12-02 14:10:53 -0800949 called_method.dex_file = caller->GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700950 code = caller->GetCodeItem();
Ian Rogers848871b2013-08-05 10:56:33 -0700951 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
952 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
953 Instruction::Code instr_code = instr->Opcode();
954 bool is_range;
955 switch (instr_code) {
956 case Instruction::INVOKE_DIRECT:
957 invoke_type = kDirect;
958 is_range = false;
959 break;
960 case Instruction::INVOKE_DIRECT_RANGE:
961 invoke_type = kDirect;
962 is_range = true;
963 break;
964 case Instruction::INVOKE_STATIC:
965 invoke_type = kStatic;
966 is_range = false;
967 break;
968 case Instruction::INVOKE_STATIC_RANGE:
969 invoke_type = kStatic;
970 is_range = true;
971 break;
972 case Instruction::INVOKE_SUPER:
973 invoke_type = kSuper;
974 is_range = false;
975 break;
976 case Instruction::INVOKE_SUPER_RANGE:
977 invoke_type = kSuper;
978 is_range = true;
979 break;
980 case Instruction::INVOKE_VIRTUAL:
981 invoke_type = kVirtual;
982 is_range = false;
983 break;
984 case Instruction::INVOKE_VIRTUAL_RANGE:
985 invoke_type = kVirtual;
986 is_range = true;
987 break;
988 case Instruction::INVOKE_INTERFACE:
989 invoke_type = kInterface;
990 is_range = false;
991 break;
992 case Instruction::INVOKE_INTERFACE_RANGE:
993 invoke_type = kInterface;
994 is_range = true;
995 break;
996 default:
Ian Rogerse0a02da2014-12-02 14:10:53 -0800997 LOG(FATAL) << "Unexpected call into trampoline: " << instr->DumpString(nullptr);
998 UNREACHABLE();
Ian Rogers848871b2013-08-05 10:56:33 -0700999 }
Ian Rogerse0a02da2014-12-02 14:10:53 -08001000 called_method.dex_method_index = (is_range) ? instr->VRegB_3rc() : instr->VRegB_35c();
Ian Rogers848871b2013-08-05 10:56:33 -07001001 } else {
1002 invoke_type = kStatic;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001003 called_method.dex_file = called->GetDexFile();
1004 called_method.dex_method_index = called->GetDexMethodIndex();
Ian Rogers848871b2013-08-05 10:56:33 -07001005 }
1006 uint32_t shorty_len;
1007 const char* shorty =
Ian Rogerse0a02da2014-12-02 14:10:53 -08001008 called_method.dex_file->GetMethodShorty(
1009 called_method.dex_file->GetMethodId(called_method.dex_method_index), &shorty_len);
Mathieu Chartier590fee92013-09-13 13:46:47 -07001010 RememberForGcArgumentVisitor visitor(sp, invoke_type == kStatic, shorty, shorty_len, &soa);
Ian Rogers848871b2013-08-05 10:56:33 -07001011 visitor.VisitArguments();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001012 self->EndAssertNoThreadSuspension(old_cause);
Ian Rogerse0a02da2014-12-02 14:10:53 -08001013 const bool virtual_or_interface = invoke_type == kVirtual || invoke_type == kInterface;
Ian Rogers848871b2013-08-05 10:56:33 -07001014 // Resolve method filling in dex cache.
Ian Rogerse0a02da2014-12-02 14:10:53 -08001015 if (!called_method_known_on_entry) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001016 StackHandleScope<1> hs(self);
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001017 mirror::Object* dummy = nullptr;
1018 HandleWrapper<mirror::Object> h_receiver(
1019 hs.NewHandleWrapper(virtual_or_interface ? &receiver : &dummy));
Ian Rogerse0a02da2014-12-02 14:10:53 -08001020 DCHECK_EQ(caller->GetDexFile(), called_method.dex_file);
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08001021 called = linker->ResolveMethod<ClassLinker::kForceICCECheck>(
1022 self, called_method.dex_method_index, caller, invoke_type);
Ian Rogers848871b2013-08-05 10:56:33 -07001023 }
Ian Rogerse0a02da2014-12-02 14:10:53 -08001024 const void* code = nullptr;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001025 if (LIKELY(!self->IsExceptionPending())) {
Ian Rogers848871b2013-08-05 10:56:33 -07001026 // Incompatible class change should have been handled in resolve method.
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001027 CHECK(!called->CheckIncompatibleClassChange(invoke_type))
1028 << PrettyMethod(called) << " " << invoke_type;
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001029 if (virtual_or_interface || invoke_type == kSuper) {
1030 // Refine called method based on receiver for kVirtual/kInterface, and
1031 // caller for kSuper.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001032 ArtMethod* orig_called = called;
Mathieu Chartier55871bf2014-02-27 10:24:50 -08001033 if (invoke_type == kVirtual) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001034 CHECK(receiver != nullptr) << invoke_type;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001035 called = receiver->GetClass()->FindVirtualMethodForVirtual(called, sizeof(void*));
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001036 } else if (invoke_type == kInterface) {
1037 CHECK(receiver != nullptr) << invoke_type;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001038 called = receiver->GetClass()->FindVirtualMethodForInterface(called, sizeof(void*));
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001039 } else {
1040 DCHECK_EQ(invoke_type, kSuper);
1041 CHECK(caller != nullptr) << invoke_type;
Nicolas Geoffray393fdb82016-04-25 14:58:06 +01001042 StackHandleScope<2> hs(self);
1043 Handle<mirror::DexCache> dex_cache(
1044 hs.NewHandle(caller->GetDeclaringClass()->GetDexCache()));
1045 Handle<mirror::ClassLoader> class_loader(
1046 hs.NewHandle(caller->GetDeclaringClass()->GetClassLoader()));
Alex Lightfedd91d2016-01-07 14:49:16 -08001047 // TODO Maybe put this into a mirror::Class function.
1048 mirror::Class* ref_class = linker->ResolveReferencedClassOfMethod(
Nicolas Geoffray393fdb82016-04-25 14:58:06 +01001049 called_method.dex_method_index, dex_cache, class_loader);
Alex Lightfedd91d2016-01-07 14:49:16 -08001050 if (ref_class->IsInterface()) {
1051 called = ref_class->FindVirtualMethodForInterfaceSuper(called, sizeof(void*));
1052 } else {
1053 called = caller->GetDeclaringClass()->GetSuperClass()->GetVTableEntry(
1054 called->GetMethodIndex(), sizeof(void*));
1055 }
Mathieu Chartier55871bf2014-02-27 10:24:50 -08001056 }
Mingyao Yangf4867782014-05-05 11:55:02 -07001057
1058 CHECK(called != nullptr) << PrettyMethod(orig_called) << " "
1059 << PrettyTypeOf(receiver) << " "
1060 << invoke_type << " " << orig_called->GetVtableIndex();
1061
Ian Rogers83883d72013-10-21 21:07:24 -07001062 // 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 -08001063 // of the sharpened method avoiding dirtying the dex cache if possible.
Ian Rogers00f15272014-12-02 16:55:46 -08001064 // Note, called_method.dex_method_index references the dex method before the
1065 // FindVirtualMethodFor... This is ok for FindDexMethodIndexInOtherDexFile that only cares
1066 // about the name and signature.
1067 uint32_t update_dex_cache_method_index = called->GetDexMethodIndex();
Vladimir Marko05792b92015-08-03 11:56:49 +01001068 if (!called->HasSameDexCacheResolvedMethods(caller, sizeof(void*))) {
Ian Rogers83883d72013-10-21 21:07:24 -07001069 // Calling from one dex file to another, need to compute the method index appropriate to
Vladimir Markobbcc0c02014-02-03 14:08:42 +00001070 // the caller's dex file. Since we get here only if the original called was a runtime
1071 // method, we've got the correct dex_file and a dex_method_idx from above.
Ian Rogerse0a02da2014-12-02 14:10:53 -08001072 DCHECK(!called_method_known_on_entry);
1073 DCHECK_EQ(caller->GetDexFile(), called_method.dex_file);
1074 const DexFile* caller_dex_file = called_method.dex_file;
1075 uint32_t caller_method_name_and_sig_index = called_method.dex_method_index;
1076 update_dex_cache_method_index =
1077 called->FindDexMethodIndexInOtherDexFile(*caller_dex_file,
1078 caller_method_name_and_sig_index);
1079 }
1080 if ((update_dex_cache_method_index != DexFile::kDexNoIndex) &&
Mathieu Chartiere401d142015-04-22 13:56:20 -07001081 (caller->GetDexCacheResolvedMethod(
1082 update_dex_cache_method_index, sizeof(void*)) != called)) {
1083 caller->SetDexCacheResolvedMethod(update_dex_cache_method_index, called, sizeof(void*));
Ian Rogers83883d72013-10-21 21:07:24 -07001084 }
Mathieu Chartiere4a91bb2015-01-28 13:11:44 -08001085 } else if (invoke_type == kStatic) {
1086 const auto called_dex_method_idx = called->GetDexMethodIndex();
1087 // For static invokes, we may dispatch to the static method in the superclass but resolve
1088 // using the subclass. To prevent getting slow paths on each invoke, we force set the
1089 // resolved method for the super class dex method index if we are in the same dex file.
1090 // b/19175856
1091 if (called->GetDexFile() == called_method.dex_file &&
1092 called_method.dex_method_index != called_dex_method_idx) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001093 called->GetDexCache()->SetResolvedMethod(called_dex_method_idx, called, sizeof(void*));
Mathieu Chartiere4a91bb2015-01-28 13:11:44 -08001094 }
Ian Rogers83883d72013-10-21 21:07:24 -07001095 }
Daniel Mihalyieb076692014-08-22 17:33:31 +02001096
Ian Rogers848871b2013-08-05 10:56:33 -07001097 // Ensure that the called method's class is initialized.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001098 StackHandleScope<1> hs(soa.Self());
1099 Handle<mirror::Class> called_class(hs.NewHandle(called->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -07001100 linker->EnsureInitialized(soa.Self(), called_class, true, true);
Ian Rogers848871b2013-08-05 10:56:33 -07001101 if (LIKELY(called_class->IsInitialized())) {
Daniel Mihalyieb076692014-08-22 17:33:31 +02001102 if (UNLIKELY(Dbg::IsForcedInterpreterNeededForResolution(self, called))) {
1103 // If we are single-stepping or the called method is deoptimized (by a
1104 // breakpoint, for example), then we have to execute the called method
1105 // with the interpreter.
1106 code = GetQuickToInterpreterBridge();
1107 } else if (UNLIKELY(Dbg::IsForcedInstrumentationNeededForResolution(self, caller))) {
1108 // If the caller is deoptimized (by a breakpoint, for example), we have to
1109 // continue its execution with interpreter when returning from the called
1110 // method. Because we do not want to execute the called method with the
1111 // interpreter, we wrap its execution into the instrumentation stubs.
1112 // When the called method returns, it will execute the instrumentation
1113 // exit hook that will determine the need of the interpreter with a call
1114 // to Dbg::IsForcedInterpreterNeededForUpcall and deoptimize the stack if
1115 // it is needed.
1116 code = GetQuickInstrumentationEntryPoint();
1117 } else {
1118 code = called->GetEntryPointFromQuickCompiledCode();
1119 }
Ian Rogers848871b2013-08-05 10:56:33 -07001120 } else if (called_class->IsInitializing()) {
Daniel Mihalyieb076692014-08-22 17:33:31 +02001121 if (UNLIKELY(Dbg::IsForcedInterpreterNeededForResolution(self, called))) {
1122 // If we are single-stepping or the called method is deoptimized (by a
1123 // breakpoint, for example), then we have to execute the called method
1124 // with the interpreter.
1125 code = GetQuickToInterpreterBridge();
1126 } else if (invoke_type == kStatic) {
Ian Rogers848871b2013-08-05 10:56:33 -07001127 // Class is still initializing, go to oat and grab code (trampoline must be left in place
1128 // until class is initialized to stop races between threads).
Ian Rogersef7d42f2014-01-06 12:55:46 -08001129 code = linker->GetQuickOatCodeFor(called);
Ian Rogers848871b2013-08-05 10:56:33 -07001130 } else {
1131 // No trampoline for non-static methods.
Ian Rogersef7d42f2014-01-06 12:55:46 -08001132 code = called->GetEntryPointFromQuickCompiledCode();
Ian Rogers848871b2013-08-05 10:56:33 -07001133 }
1134 } else {
1135 DCHECK(called_class->IsErroneous());
1136 }
1137 }
Ian Rogerse0a02da2014-12-02 14:10:53 -08001138 CHECK_EQ(code == nullptr, self->IsExceptionPending());
Mathieu Chartier07d447b2013-09-26 11:57:43 -07001139 // Fixup any locally saved objects may have moved during a GC.
1140 visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -07001141 // Place called method in callee-save frame to be placed as first argument to quick method.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001142 *sp = called;
1143
Ian Rogers848871b2013-08-05 10:56:33 -07001144 return code;
1145}
1146
Andreas Gampec147b002014-03-06 18:11:06 -08001147/*
1148 * This class uses a couple of observations to unite the different calling conventions through
1149 * a few constants.
1150 *
1151 * 1) Number of registers used for passing is normally even, so counting down has no penalty for
1152 * possible alignment.
1153 * 2) Known 64b architectures store 8B units on the stack, both for integral and floating point
1154 * types, so using uintptr_t is OK. Also means that we can use kRegistersNeededX to denote
1155 * when we have to split things
1156 * 3) The only soft-float, Arm, is 32b, so no widening needs to be taken into account for floats
1157 * and we can use Int handling directly.
1158 * 4) Only 64b architectures widen, and their stack is aligned 8B anyways, so no padding code
1159 * necessary when widening. Also, widening of Ints will take place implicitly, and the
1160 * extension should be compatible with Aarch64, which mandates copying the available bits
1161 * into LSB and leaving the rest unspecified.
1162 * 5) Aligning longs and doubles is necessary on arm only, and it's the same in registers and on
1163 * the stack.
1164 * 6) There is only little endian.
1165 *
1166 *
1167 * Actual work is supposed to be done in a delegate of the template type. The interface is as
1168 * follows:
1169 *
1170 * void PushGpr(uintptr_t): Add a value for the next GPR
1171 *
1172 * void PushFpr4(float): Add a value for the next FPR of size 32b. Is only called if we need
1173 * padding, that is, think the architecture is 32b and aligns 64b.
1174 *
1175 * void PushFpr8(uint64_t): Push a double. We _will_ call this on 32b, it's the callee's job to
1176 * split this if necessary. The current state will have aligned, if
1177 * necessary.
1178 *
1179 * void PushStack(uintptr_t): Push a value to the stack.
1180 *
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001181 * uintptr_t PushHandleScope(mirror::Object* ref): Add a reference to the HandleScope. This _will_ have nullptr,
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001182 * as this might be important for null initialization.
Andreas Gampec147b002014-03-06 18:11:06 -08001183 * Must return the jobject, that is, the reference to the
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001184 * entry in the HandleScope (nullptr if necessary).
Andreas Gampec147b002014-03-06 18:11:06 -08001185 *
1186 */
Andreas Gampec200a4a2014-06-16 18:39:09 -07001187template<class T> class BuildNativeCallFrameStateMachine {
Andreas Gampec147b002014-03-06 18:11:06 -08001188 public:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001189#if defined(__arm__)
1190 // TODO: These are all dummy values!
Andreas Gampec147b002014-03-06 18:11:06 -08001191 static constexpr bool kNativeSoftFloatAbi = true;
1192 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs, r0-r3
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001193 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
1194
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001195 static constexpr size_t kRegistersNeededForLong = 2;
1196 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -08001197 static constexpr bool kMultiRegistersAligned = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001198 static constexpr bool kMultiFPRegistersWidened = false;
1199 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001200 static constexpr bool kAlignLongOnStack = true;
1201 static constexpr bool kAlignDoubleOnStack = true;
Stuart Monteithb95a5342014-03-12 13:32:32 +00001202#elif defined(__aarch64__)
1203 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
1204 static constexpr size_t kNumNativeGprArgs = 8; // 6 arguments passed in GPRs.
1205 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
1206
1207 static constexpr size_t kRegistersNeededForLong = 1;
1208 static constexpr size_t kRegistersNeededForDouble = 1;
1209 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001210 static constexpr bool kMultiFPRegistersWidened = false;
1211 static constexpr bool kMultiGPRegistersWidened = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +00001212 static constexpr bool kAlignLongOnStack = false;
1213 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001214#elif defined(__mips__) && !defined(__LP64__)
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001215 static constexpr bool kNativeSoftFloatAbi = true; // This is a hard float ABI.
Douglas Leung735b8552014-10-31 12:21:40 -07001216 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs.
1217 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001218
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001219 static constexpr size_t kRegistersNeededForLong = 2;
1220 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -08001221 static constexpr bool kMultiRegistersAligned = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001222 static constexpr bool kMultiFPRegistersWidened = true;
1223 static constexpr bool kMultiGPRegistersWidened = false;
Douglas Leung735b8552014-10-31 12:21:40 -07001224 static constexpr bool kAlignLongOnStack = true;
1225 static constexpr bool kAlignDoubleOnStack = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001226#elif defined(__mips__) && defined(__LP64__)
1227 // Let the code prepare GPRs only and we will load the FPRs with same data.
1228 static constexpr bool kNativeSoftFloatAbi = true;
1229 static constexpr size_t kNumNativeGprArgs = 8;
1230 static constexpr size_t kNumNativeFprArgs = 0;
1231
1232 static constexpr size_t kRegistersNeededForLong = 1;
1233 static constexpr size_t kRegistersNeededForDouble = 1;
1234 static constexpr bool kMultiRegistersAligned = false;
1235 static constexpr bool kMultiFPRegistersWidened = false;
1236 static constexpr bool kMultiGPRegistersWidened = true;
1237 static constexpr bool kAlignLongOnStack = false;
1238 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001239#elif defined(__i386__)
1240 // TODO: Check these!
Andreas Gampec147b002014-03-06 18:11:06 -08001241 static constexpr bool kNativeSoftFloatAbi = false; // Not using int registers for fp
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001242 static constexpr size_t kNumNativeGprArgs = 0; // 6 arguments passed in GPRs.
1243 static constexpr size_t kNumNativeFprArgs = 0; // 8 arguments passed in FPRs.
1244
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001245 static constexpr size_t kRegistersNeededForLong = 2;
1246 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001247 static constexpr bool kMultiRegistersAligned = false; // x86 not using regs, anyways
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001248 static constexpr bool kMultiFPRegistersWidened = false;
1249 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001250 static constexpr bool kAlignLongOnStack = false;
1251 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001252#elif defined(__x86_64__)
1253 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
1254 static constexpr size_t kNumNativeGprArgs = 6; // 6 arguments passed in GPRs.
1255 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
1256
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001257 static constexpr size_t kRegistersNeededForLong = 1;
1258 static constexpr size_t kRegistersNeededForDouble = 1;
Andreas Gampec147b002014-03-06 18:11:06 -08001259 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001260 static constexpr bool kMultiFPRegistersWidened = false;
1261 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001262 static constexpr bool kAlignLongOnStack = false;
1263 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001264#else
1265#error "Unsupported architecture"
1266#endif
1267
Andreas Gampec147b002014-03-06 18:11:06 -08001268 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001269 explicit BuildNativeCallFrameStateMachine(T* delegate)
1270 : gpr_index_(kNumNativeGprArgs),
1271 fpr_index_(kNumNativeFprArgs),
1272 stack_entries_(0),
1273 delegate_(delegate) {
Andreas Gampec147b002014-03-06 18:11:06 -08001274 // For register alignment, we want to assume that counters (gpr_index_, fpr_index_) are even iff
1275 // the next register is even; counting down is just to make the compiler happy...
Andreas Gampe575e78c2014-11-03 23:41:03 -08001276 static_assert(kNumNativeGprArgs % 2 == 0U, "Number of native GPR arguments not even");
1277 static_assert(kNumNativeFprArgs % 2 == 0U, "Number of native FPR arguments not even");
Andreas Gampec147b002014-03-06 18:11:06 -08001278 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001279
Andreas Gampec200a4a2014-06-16 18:39:09 -07001280 virtual ~BuildNativeCallFrameStateMachine() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001281
Ian Rogers1428dce2014-10-21 15:02:15 -07001282 bool HavePointerGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001283 return gpr_index_ > 0;
1284 }
1285
Andreas Gampec200a4a2014-06-16 18:39:09 -07001286 void AdvancePointer(const void* val) {
Andreas Gampec147b002014-03-06 18:11:06 -08001287 if (HavePointerGpr()) {
1288 gpr_index_--;
1289 PushGpr(reinterpret_cast<uintptr_t>(val));
1290 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001291 stack_entries_++; // TODO: have a field for pointer length as multiple of 32b
Andreas Gampec147b002014-03-06 18:11:06 -08001292 PushStack(reinterpret_cast<uintptr_t>(val));
1293 gpr_index_ = 0;
1294 }
1295 }
1296
Ian Rogers1428dce2014-10-21 15:02:15 -07001297 bool HaveHandleScopeGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001298 return gpr_index_ > 0;
1299 }
1300
Mathieu Chartier90443472015-07-16 20:32:27 -07001301 void AdvanceHandleScope(mirror::Object* ptr) SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001302 uintptr_t handle = PushHandle(ptr);
1303 if (HaveHandleScopeGpr()) {
Andreas Gampec147b002014-03-06 18:11:06 -08001304 gpr_index_--;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001305 PushGpr(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001306 } else {
1307 stack_entries_++;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001308 PushStack(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001309 gpr_index_ = 0;
1310 }
1311 }
1312
Ian Rogers1428dce2014-10-21 15:02:15 -07001313 bool HaveIntGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001314 return gpr_index_ > 0;
1315 }
1316
1317 void AdvanceInt(uint32_t val) {
1318 if (HaveIntGpr()) {
1319 gpr_index_--;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001320 if (kMultiGPRegistersWidened) {
1321 DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001322 PushGpr(static_cast<int64_t>(bit_cast<int32_t, uint32_t>(val)));
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001323 } else {
1324 PushGpr(val);
1325 }
Andreas Gampec147b002014-03-06 18:11:06 -08001326 } else {
1327 stack_entries_++;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001328 if (kMultiGPRegistersWidened) {
1329 DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001330 PushStack(static_cast<int64_t>(bit_cast<int32_t, uint32_t>(val)));
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001331 } else {
1332 PushStack(val);
1333 }
Andreas Gampec147b002014-03-06 18:11:06 -08001334 gpr_index_ = 0;
1335 }
1336 }
1337
Ian Rogers1428dce2014-10-21 15:02:15 -07001338 bool HaveLongGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001339 return gpr_index_ >= kRegistersNeededForLong + (LongGprNeedsPadding() ? 1 : 0);
1340 }
1341
Ian Rogers1428dce2014-10-21 15:02:15 -07001342 bool LongGprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001343 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1344 kAlignLongOnStack && // and when it needs alignment
1345 (gpr_index_ & 1) == 1; // counter is odd, see constructor
1346 }
1347
Ian Rogers1428dce2014-10-21 15:02:15 -07001348 bool LongStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001349 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1350 kAlignLongOnStack && // and when it needs 8B alignment
1351 (stack_entries_ & 1) == 1; // counter is odd
1352 }
1353
1354 void AdvanceLong(uint64_t val) {
1355 if (HaveLongGpr()) {
1356 if (LongGprNeedsPadding()) {
1357 PushGpr(0);
1358 gpr_index_--;
1359 }
1360 if (kRegistersNeededForLong == 1) {
1361 PushGpr(static_cast<uintptr_t>(val));
1362 } else {
1363 PushGpr(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1364 PushGpr(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1365 }
1366 gpr_index_ -= kRegistersNeededForLong;
1367 } else {
1368 if (LongStackNeedsPadding()) {
1369 PushStack(0);
1370 stack_entries_++;
1371 }
1372 if (kRegistersNeededForLong == 1) {
1373 PushStack(static_cast<uintptr_t>(val));
1374 stack_entries_++;
1375 } else {
1376 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1377 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1378 stack_entries_ += 2;
1379 }
1380 gpr_index_ = 0;
1381 }
1382 }
1383
Ian Rogers1428dce2014-10-21 15:02:15 -07001384 bool HaveFloatFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001385 return fpr_index_ > 0;
1386 }
1387
Andreas Gampec147b002014-03-06 18:11:06 -08001388 void AdvanceFloat(float val) {
1389 if (kNativeSoftFloatAbi) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001390 AdvanceInt(bit_cast<uint32_t, float>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001391 } else {
1392 if (HaveFloatFpr()) {
1393 fpr_index_--;
1394 if (kRegistersNeededForDouble == 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001395 if (kMultiFPRegistersWidened) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001396 PushFpr8(bit_cast<uint64_t, double>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001397 } else {
1398 // No widening, just use the bits.
Roland Levillainda4d79b2015-03-24 14:36:11 +00001399 PushFpr8(static_cast<uint64_t>(bit_cast<uint32_t, float>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001400 }
1401 } else {
1402 PushFpr4(val);
1403 }
1404 } else {
1405 stack_entries_++;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001406 if (kRegistersNeededForDouble == 1 && kMultiFPRegistersWidened) {
Andreas Gampec147b002014-03-06 18:11:06 -08001407 // Need to widen before storing: Note the "double" in the template instantiation.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001408 // Note: We need to jump through those hoops to make the compiler happy.
1409 DCHECK_EQ(sizeof(uintptr_t), sizeof(uint64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001410 PushStack(static_cast<uintptr_t>(bit_cast<uint64_t, double>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001411 } else {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001412 PushStack(static_cast<uintptr_t>(bit_cast<uint32_t, float>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001413 }
1414 fpr_index_ = 0;
1415 }
1416 }
1417 }
1418
Ian Rogers1428dce2014-10-21 15:02:15 -07001419 bool HaveDoubleFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001420 return fpr_index_ >= kRegistersNeededForDouble + (DoubleFprNeedsPadding() ? 1 : 0);
1421 }
1422
Ian Rogers1428dce2014-10-21 15:02:15 -07001423 bool DoubleFprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001424 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1425 kAlignDoubleOnStack && // and when it needs alignment
1426 (fpr_index_ & 1) == 1; // counter is odd, see constructor
1427 }
1428
Ian Rogers1428dce2014-10-21 15:02:15 -07001429 bool DoubleStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001430 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1431 kAlignDoubleOnStack && // and when it needs 8B alignment
1432 (stack_entries_ & 1) == 1; // counter is odd
1433 }
1434
1435 void AdvanceDouble(uint64_t val) {
1436 if (kNativeSoftFloatAbi) {
1437 AdvanceLong(val);
1438 } else {
1439 if (HaveDoubleFpr()) {
1440 if (DoubleFprNeedsPadding()) {
1441 PushFpr4(0);
1442 fpr_index_--;
1443 }
1444 PushFpr8(val);
1445 fpr_index_ -= kRegistersNeededForDouble;
1446 } else {
1447 if (DoubleStackNeedsPadding()) {
1448 PushStack(0);
1449 stack_entries_++;
1450 }
1451 if (kRegistersNeededForDouble == 1) {
1452 PushStack(static_cast<uintptr_t>(val));
1453 stack_entries_++;
1454 } else {
1455 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1456 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1457 stack_entries_ += 2;
1458 }
1459 fpr_index_ = 0;
1460 }
1461 }
1462 }
1463
Ian Rogers1428dce2014-10-21 15:02:15 -07001464 uint32_t GetStackEntries() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001465 return stack_entries_;
1466 }
1467
Ian Rogers1428dce2014-10-21 15:02:15 -07001468 uint32_t GetNumberOfUsedGprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001469 return kNumNativeGprArgs - gpr_index_;
1470 }
1471
Ian Rogers1428dce2014-10-21 15:02:15 -07001472 uint32_t GetNumberOfUsedFprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001473 return kNumNativeFprArgs - fpr_index_;
1474 }
1475
1476 private:
1477 void PushGpr(uintptr_t val) {
1478 delegate_->PushGpr(val);
1479 }
1480 void PushFpr4(float val) {
1481 delegate_->PushFpr4(val);
1482 }
1483 void PushFpr8(uint64_t val) {
1484 delegate_->PushFpr8(val);
1485 }
1486 void PushStack(uintptr_t val) {
1487 delegate_->PushStack(val);
1488 }
Mathieu Chartier90443472015-07-16 20:32:27 -07001489 uintptr_t PushHandle(mirror::Object* ref) SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001490 return delegate_->PushHandle(ref);
Andreas Gampec147b002014-03-06 18:11:06 -08001491 }
1492
1493 uint32_t gpr_index_; // Number of free GPRs
1494 uint32_t fpr_index_; // Number of free FPRs
1495 uint32_t stack_entries_; // Stack entries are in multiples of 32b, as floats are usually not
1496 // extended
Ian Rogers1428dce2014-10-21 15:02:15 -07001497 T* const delegate_; // What Push implementation gets called
Andreas Gampec147b002014-03-06 18:11:06 -08001498};
1499
Andreas Gampec200a4a2014-06-16 18:39:09 -07001500// Computes the sizes of register stacks and call stack area. Handling of references can be extended
1501// in subclasses.
1502//
1503// To handle native pointers, use "L" in the shorty for an object reference, which simulates
1504// them with handles.
1505class ComputeNativeCallFrameSize {
Andreas Gampec147b002014-03-06 18:11:06 -08001506 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001507 ComputeNativeCallFrameSize() : num_stack_entries_(0) {}
1508
1509 virtual ~ComputeNativeCallFrameSize() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001510
Ian Rogers1428dce2014-10-21 15:02:15 -07001511 uint32_t GetStackSize() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001512 return num_stack_entries_ * sizeof(uintptr_t);
1513 }
1514
Ian Rogers1428dce2014-10-21 15:02:15 -07001515 uint8_t* LayoutCallStack(uint8_t* sp8) const {
Andreas Gampec147b002014-03-06 18:11:06 -08001516 sp8 -= GetStackSize();
Andreas Gampe779f8c92014-06-09 18:29:38 -07001517 // Align by kStackAlignment.
1518 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
Andreas Gampec200a4a2014-06-16 18:39:09 -07001519 return sp8;
Andreas Gampec147b002014-03-06 18:11:06 -08001520 }
1521
Ian Rogers1428dce2014-10-21 15:02:15 -07001522 uint8_t* LayoutCallRegisterStacks(uint8_t* sp8, uintptr_t** start_gpr, uint32_t** start_fpr)
1523 const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001524 // Assumption is OK right now, as we have soft-float arm
1525 size_t fregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeFprArgs;
1526 sp8 -= fregs * sizeof(uintptr_t);
1527 *start_fpr = reinterpret_cast<uint32_t*>(sp8);
1528 size_t iregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeGprArgs;
1529 sp8 -= iregs * sizeof(uintptr_t);
1530 *start_gpr = reinterpret_cast<uintptr_t*>(sp8);
1531 return sp8;
1532 }
Andreas Gampec147b002014-03-06 18:11:06 -08001533
Andreas Gampec200a4a2014-06-16 18:39:09 -07001534 uint8_t* LayoutNativeCall(uint8_t* sp8, uintptr_t** start_stack, uintptr_t** start_gpr,
Ian Rogers1428dce2014-10-21 15:02:15 -07001535 uint32_t** start_fpr) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001536 // Native call stack.
1537 sp8 = LayoutCallStack(sp8);
1538 *start_stack = reinterpret_cast<uintptr_t*>(sp8);
Andreas Gampec147b002014-03-06 18:11:06 -08001539
Andreas Gampec200a4a2014-06-16 18:39:09 -07001540 // Put fprs and gprs below.
1541 sp8 = LayoutCallRegisterStacks(sp8, start_gpr, start_fpr);
Andreas Gampec147b002014-03-06 18:11:06 -08001542
Andreas Gampec200a4a2014-06-16 18:39:09 -07001543 // Return the new bottom.
1544 return sp8;
1545 }
1546
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001547 virtual void WalkHeader(
1548 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm ATTRIBUTE_UNUSED)
Mathieu Chartier90443472015-07-16 20:32:27 -07001549 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001550 }
Andreas Gampec200a4a2014-06-16 18:39:09 -07001551
Mathieu Chartier90443472015-07-16 20:32:27 -07001552 void Walk(const char* shorty, uint32_t shorty_len) SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001553 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize> sm(this);
1554
1555 WalkHeader(&sm);
Andreas Gampec147b002014-03-06 18:11:06 -08001556
1557 for (uint32_t i = 1; i < shorty_len; ++i) {
1558 Primitive::Type cur_type_ = Primitive::GetType(shorty[i]);
1559 switch (cur_type_) {
1560 case Primitive::kPrimNot:
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001561 // TODO: fix abuse of mirror types.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001562 sm.AdvanceHandleScope(
1563 reinterpret_cast<mirror::Object*>(0x12345678));
Andreas Gampec147b002014-03-06 18:11:06 -08001564 break;
1565
1566 case Primitive::kPrimBoolean:
1567 case Primitive::kPrimByte:
1568 case Primitive::kPrimChar:
1569 case Primitive::kPrimShort:
1570 case Primitive::kPrimInt:
1571 sm.AdvanceInt(0);
1572 break;
1573 case Primitive::kPrimFloat:
1574 sm.AdvanceFloat(0);
1575 break;
1576 case Primitive::kPrimDouble:
1577 sm.AdvanceDouble(0);
1578 break;
1579 case Primitive::kPrimLong:
1580 sm.AdvanceLong(0);
1581 break;
1582 default:
1583 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001584 UNREACHABLE();
Andreas Gampec147b002014-03-06 18:11:06 -08001585 }
1586 }
1587
Ian Rogers1428dce2014-10-21 15:02:15 -07001588 num_stack_entries_ = sm.GetStackEntries();
Andreas Gampec147b002014-03-06 18:11:06 -08001589 }
1590
1591 void PushGpr(uintptr_t /* val */) {
1592 // not optimizing registers, yet
1593 }
1594
1595 void PushFpr4(float /* val */) {
1596 // not optimizing registers, yet
1597 }
1598
1599 void PushFpr8(uint64_t /* val */) {
1600 // not optimizing registers, yet
1601 }
1602
1603 void PushStack(uintptr_t /* val */) {
1604 // counting is already done in the superclass
1605 }
1606
Andreas Gampec200a4a2014-06-16 18:39:09 -07001607 virtual uintptr_t PushHandle(mirror::Object* /* ptr */) {
Andreas Gampec147b002014-03-06 18:11:06 -08001608 return reinterpret_cast<uintptr_t>(nullptr);
1609 }
1610
Andreas Gampec200a4a2014-06-16 18:39:09 -07001611 protected:
Andreas Gampec147b002014-03-06 18:11:06 -08001612 uint32_t num_stack_entries_;
1613};
1614
Andreas Gampec200a4a2014-06-16 18:39:09 -07001615class ComputeGenericJniFrameSize FINAL : public ComputeNativeCallFrameSize {
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001616 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001617 ComputeGenericJniFrameSize() : num_handle_scope_references_(0) {}
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001618
Andreas Gampec200a4a2014-06-16 18:39:09 -07001619 // Lays out the callee-save frame. Assumes that the incorrect frame corresponding to RefsAndArgs
1620 // is at *m = sp. Will update to point to the bottom of the save frame.
1621 //
1622 // Note: assumes ComputeAll() has been run before.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001623 void LayoutCalleeSaveFrame(Thread* self, ArtMethod*** m, void* sp, HandleScope** handle_scope)
Mathieu Chartier90443472015-07-16 20:32:27 -07001624 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001625 ArtMethod* method = **m;
1626
1627 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), sizeof(void*));
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001628
Andreas Gampec200a4a2014-06-16 18:39:09 -07001629 uint8_t* sp8 = reinterpret_cast<uint8_t*>(sp);
1630
1631 // First, fix up the layout of the callee-save frame.
1632 // We have to squeeze in the HandleScope, and relocate the method pointer.
1633
1634 // "Free" the slot for the method.
Ian Rogers13735952014-10-08 12:43:28 -07001635 sp8 += sizeof(void*); // In the callee-save frame we use a full pointer.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001636
1637 // Under the callee saves put handle scope and new method stack reference.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001638 size_t handle_scope_size = HandleScope::SizeOf(num_handle_scope_references_);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001639 size_t scope_and_method = handle_scope_size + sizeof(ArtMethod*);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001640
1641 sp8 -= scope_and_method;
1642 // Align by kStackAlignment.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001643 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
Andreas Gampec200a4a2014-06-16 18:39:09 -07001644
Mathieu Chartiere401d142015-04-22 13:56:20 -07001645 uint8_t* sp8_table = sp8 + sizeof(ArtMethod*);
Ian Rogers59c07062014-10-10 13:03:39 -07001646 *handle_scope = HandleScope::Create(sp8_table, self->GetTopHandleScope(),
1647 num_handle_scope_references_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001648
1649 // Add a slot for the method pointer, and fill it. Fix the pointer-pointer given to us.
1650 uint8_t* method_pointer = sp8;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001651 auto** new_method_ref = reinterpret_cast<ArtMethod**>(method_pointer);
1652 *new_method_ref = method;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001653 *m = new_method_ref;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001654 }
1655
Andreas Gampec200a4a2014-06-16 18:39:09 -07001656 // Adds space for the cookie. Note: may leave stack unaligned.
Ian Rogers1428dce2014-10-21 15:02:15 -07001657 void LayoutCookie(uint8_t** sp) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001658 // Reference cookie and padding
1659 *sp -= 8;
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001660 }
1661
Andreas Gampec200a4a2014-06-16 18:39:09 -07001662 // Re-layout the callee-save frame (insert a handle-scope). Then add space for the cookie.
1663 // Returns the new bottom. Note: this may be unaligned.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001664 uint8_t* LayoutJNISaveFrame(Thread* self, ArtMethod*** m, void* sp, HandleScope** handle_scope)
Mathieu Chartier90443472015-07-16 20:32:27 -07001665 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001666 // First, fix up the layout of the callee-save frame.
1667 // We have to squeeze in the HandleScope, and relocate the method pointer.
Ian Rogers59c07062014-10-10 13:03:39 -07001668 LayoutCalleeSaveFrame(self, m, sp, handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001669
1670 // The bottom of the callee-save frame is now where the method is, *m.
1671 uint8_t* sp8 = reinterpret_cast<uint8_t*>(*m);
1672
1673 // Add space for cookie.
1674 LayoutCookie(&sp8);
1675
1676 return sp8;
1677 }
1678
1679 // WARNING: After this, *sp won't be pointing to the method anymore!
Mathieu Chartiere401d142015-04-22 13:56:20 -07001680 uint8_t* ComputeLayout(Thread* self, ArtMethod*** m, const char* shorty, uint32_t shorty_len,
1681 HandleScope** handle_scope, uintptr_t** start_stack, uintptr_t** start_gpr,
1682 uint32_t** start_fpr)
Mathieu Chartier90443472015-07-16 20:32:27 -07001683 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001684 Walk(shorty, shorty_len);
1685
1686 // JNI part.
Ian Rogers59c07062014-10-10 13:03:39 -07001687 uint8_t* sp8 = LayoutJNISaveFrame(self, m, reinterpret_cast<void*>(*m), handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001688
1689 sp8 = LayoutNativeCall(sp8, start_stack, start_gpr, start_fpr);
1690
1691 // Return the new bottom.
1692 return sp8;
1693 }
1694
1695 uintptr_t PushHandle(mirror::Object* /* ptr */) OVERRIDE;
1696
1697 // Add JNIEnv* and jobj/jclass before the shorty-derived elements.
1698 void WalkHeader(BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) OVERRIDE
Mathieu Chartier90443472015-07-16 20:32:27 -07001699 SHARED_REQUIRES(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001700
1701 private:
1702 uint32_t num_handle_scope_references_;
1703};
1704
1705uintptr_t ComputeGenericJniFrameSize::PushHandle(mirror::Object* /* ptr */) {
1706 num_handle_scope_references_++;
1707 return reinterpret_cast<uintptr_t>(nullptr);
1708}
1709
1710void ComputeGenericJniFrameSize::WalkHeader(
1711 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) {
1712 // JNIEnv
1713 sm->AdvancePointer(nullptr);
1714
1715 // Class object or this as first argument
1716 sm->AdvanceHandleScope(reinterpret_cast<mirror::Object*>(0x12345678));
1717}
1718
1719// Class to push values to three separate regions. Used to fill the native call part. Adheres to
1720// the template requirements of BuildGenericJniFrameStateMachine.
1721class FillNativeCall {
1722 public:
1723 FillNativeCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) :
1724 cur_gpr_reg_(gpr_regs), cur_fpr_reg_(fpr_regs), cur_stack_arg_(stack_args) {}
1725
1726 virtual ~FillNativeCall() {}
1727
1728 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) {
1729 cur_gpr_reg_ = gpr_regs;
1730 cur_fpr_reg_ = fpr_regs;
1731 cur_stack_arg_ = stack_args;
Andreas Gampec147b002014-03-06 18:11:06 -08001732 }
1733
1734 void PushGpr(uintptr_t val) {
1735 *cur_gpr_reg_ = val;
1736 cur_gpr_reg_++;
1737 }
1738
1739 void PushFpr4(float val) {
1740 *cur_fpr_reg_ = val;
1741 cur_fpr_reg_++;
1742 }
1743
1744 void PushFpr8(uint64_t val) {
1745 uint64_t* tmp = reinterpret_cast<uint64_t*>(cur_fpr_reg_);
1746 *tmp = val;
1747 cur_fpr_reg_ += 2;
1748 }
1749
1750 void PushStack(uintptr_t val) {
1751 *cur_stack_arg_ = val;
1752 cur_stack_arg_++;
1753 }
1754
Mathieu Chartier90443472015-07-16 20:32:27 -07001755 virtual uintptr_t PushHandle(mirror::Object*) SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001756 LOG(FATAL) << "(Non-JNI) Native call does not use handles.";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001757 UNREACHABLE();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001758 }
1759
1760 private:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001761 uintptr_t* cur_gpr_reg_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001762 uint32_t* cur_fpr_reg_;
1763 uintptr_t* cur_stack_arg_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001764};
Andreas Gampec147b002014-03-06 18:11:06 -08001765
Andreas Gampec200a4a2014-06-16 18:39:09 -07001766// Visits arguments on the stack placing them into a region lower down the stack for the benefit
1767// of transitioning into native code.
1768class BuildGenericJniFrameVisitor FINAL : public QuickArgumentVisitor {
1769 public:
Ian Rogers59c07062014-10-10 13:03:39 -07001770 BuildGenericJniFrameVisitor(Thread* self, bool is_static, const char* shorty, uint32_t shorty_len,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001771 ArtMethod*** sp)
Andreas Gampec200a4a2014-06-16 18:39:09 -07001772 : QuickArgumentVisitor(*sp, is_static, shorty, shorty_len),
1773 jni_call_(nullptr, nullptr, nullptr, nullptr), sm_(&jni_call_) {
1774 ComputeGenericJniFrameSize fsc;
1775 uintptr_t* start_gpr_reg;
1776 uint32_t* start_fpr_reg;
1777 uintptr_t* start_stack_arg;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001778 bottom_of_used_area_ = fsc.ComputeLayout(self, sp, shorty, shorty_len,
Ian Rogers59c07062014-10-10 13:03:39 -07001779 &handle_scope_,
1780 &start_stack_arg,
Andreas Gampec200a4a2014-06-16 18:39:09 -07001781 &start_gpr_reg, &start_fpr_reg);
1782
Andreas Gampec200a4a2014-06-16 18:39:09 -07001783 jni_call_.Reset(start_gpr_reg, start_fpr_reg, start_stack_arg, handle_scope_);
1784
1785 // jni environment is always first argument
1786 sm_.AdvancePointer(self->GetJniEnv());
1787
1788 if (is_static) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001789 sm_.AdvanceHandleScope((**sp)->GetDeclaringClass());
Andreas Gampec200a4a2014-06-16 18:39:09 -07001790 }
1791 }
1792
Mathieu Chartier90443472015-07-16 20:32:27 -07001793 void Visit() SHARED_REQUIRES(Locks::mutator_lock_) OVERRIDE;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001794
Mathieu Chartier90443472015-07-16 20:32:27 -07001795 void FinalizeHandleScope(Thread* self) SHARED_REQUIRES(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001796
Vladimir Markof39745e2016-01-26 12:16:55 +00001797 StackReference<mirror::Object>* GetFirstHandleScopeEntry() {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001798 return handle_scope_->GetHandle(0).GetReference();
1799 }
1800
Mathieu Chartier90443472015-07-16 20:32:27 -07001801 jobject GetFirstHandleScopeJObject() const SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001802 return handle_scope_->GetHandle(0).ToJObject();
1803 }
1804
Ian Rogers1428dce2014-10-21 15:02:15 -07001805 void* GetBottomOfUsedArea() const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001806 return bottom_of_used_area_;
1807 }
1808
1809 private:
1810 // A class to fill a JNI call. Adds reference/handle-scope management to FillNativeCall.
1811 class FillJniCall FINAL : public FillNativeCall {
1812 public:
1813 FillJniCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args,
1814 HandleScope* handle_scope) : FillNativeCall(gpr_regs, fpr_regs, stack_args),
1815 handle_scope_(handle_scope), cur_entry_(0) {}
1816
Mathieu Chartier90443472015-07-16 20:32:27 -07001817 uintptr_t PushHandle(mirror::Object* ref) OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001818
1819 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args, HandleScope* scope) {
1820 FillNativeCall::Reset(gpr_regs, fpr_regs, stack_args);
1821 handle_scope_ = scope;
1822 cur_entry_ = 0U;
1823 }
1824
Mathieu Chartier90443472015-07-16 20:32:27 -07001825 void ResetRemainingScopeSlots() SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001826 // Initialize padding entries.
1827 size_t expected_slots = handle_scope_->NumberOfReferences();
1828 while (cur_entry_ < expected_slots) {
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07001829 handle_scope_->GetMutableHandle(cur_entry_++).Assign(nullptr);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001830 }
1831 DCHECK_NE(cur_entry_, 0U);
1832 }
1833
1834 private:
1835 HandleScope* handle_scope_;
1836 size_t cur_entry_;
1837 };
1838
1839 HandleScope* handle_scope_;
1840 FillJniCall jni_call_;
1841 void* bottom_of_used_area_;
1842
1843 BuildNativeCallFrameStateMachine<FillJniCall> sm_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001844
1845 DISALLOW_COPY_AND_ASSIGN(BuildGenericJniFrameVisitor);
1846};
1847
Andreas Gampec200a4a2014-06-16 18:39:09 -07001848uintptr_t BuildGenericJniFrameVisitor::FillJniCall::PushHandle(mirror::Object* ref) {
1849 uintptr_t tmp;
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07001850 MutableHandle<mirror::Object> h = handle_scope_->GetMutableHandle(cur_entry_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001851 h.Assign(ref);
1852 tmp = reinterpret_cast<uintptr_t>(h.ToJObject());
1853 cur_entry_++;
1854 return tmp;
1855}
1856
Ian Rogers9758f792014-03-13 09:02:55 -07001857void BuildGenericJniFrameVisitor::Visit() {
1858 Primitive::Type type = GetParamPrimitiveType();
1859 switch (type) {
1860 case Primitive::kPrimLong: {
1861 jlong long_arg;
1862 if (IsSplitLongOrDouble()) {
1863 long_arg = ReadSplitLongParam();
1864 } else {
1865 long_arg = *reinterpret_cast<jlong*>(GetParamAddress());
1866 }
1867 sm_.AdvanceLong(long_arg);
1868 break;
1869 }
1870 case Primitive::kPrimDouble: {
1871 uint64_t double_arg;
1872 if (IsSplitLongOrDouble()) {
1873 // Read into union so that we don't case to a double.
1874 double_arg = ReadSplitLongParam();
1875 } else {
1876 double_arg = *reinterpret_cast<uint64_t*>(GetParamAddress());
1877 }
1878 sm_.AdvanceDouble(double_arg);
1879 break;
1880 }
1881 case Primitive::kPrimNot: {
1882 StackReference<mirror::Object>* stack_ref =
1883 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001884 sm_.AdvanceHandleScope(stack_ref->AsMirrorPtr());
Ian Rogers9758f792014-03-13 09:02:55 -07001885 break;
1886 }
1887 case Primitive::kPrimFloat:
1888 sm_.AdvanceFloat(*reinterpret_cast<float*>(GetParamAddress()));
1889 break;
1890 case Primitive::kPrimBoolean: // Fall-through.
1891 case Primitive::kPrimByte: // Fall-through.
1892 case Primitive::kPrimChar: // Fall-through.
1893 case Primitive::kPrimShort: // Fall-through.
1894 case Primitive::kPrimInt: // Fall-through.
1895 sm_.AdvanceInt(*reinterpret_cast<jint*>(GetParamAddress()));
1896 break;
1897 case Primitive::kPrimVoid:
1898 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -07001899 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -07001900 }
1901}
1902
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001903void BuildGenericJniFrameVisitor::FinalizeHandleScope(Thread* self) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001904 // Clear out rest of the scope.
1905 jni_call_.ResetRemainingScopeSlots();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001906 // Install HandleScope.
1907 self->PushHandleScope(handle_scope_);
Ian Rogers9758f792014-03-13 09:02:55 -07001908}
1909
Ian Rogers04c31d22014-07-07 21:44:06 -07001910#if defined(__arm__) || defined(__aarch64__)
Andreas Gampe90546832014-03-12 18:07:19 -07001911extern "C" void* artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07001912#else
1913extern "C" void* artFindNativeMethod(Thread* self);
1914#endif
Andreas Gampe90546832014-03-12 18:07:19 -07001915
Andreas Gampead615172014-04-04 16:20:13 -07001916uint64_t artQuickGenericJniEndJNIRef(Thread* self, uint32_t cookie, jobject l, jobject lock) {
1917 if (lock != nullptr) {
1918 return reinterpret_cast<uint64_t>(JniMethodEndWithReferenceSynchronized(l, cookie, lock, self));
1919 } else {
1920 return reinterpret_cast<uint64_t>(JniMethodEndWithReference(l, cookie, self));
1921 }
1922}
1923
1924void artQuickGenericJniEndJNINonRef(Thread* self, uint32_t cookie, jobject lock) {
1925 if (lock != nullptr) {
1926 JniMethodEndSynchronized(cookie, lock, self);
1927 } else {
1928 JniMethodEnd(cookie, self);
1929 }
1930}
1931
Andreas Gampec147b002014-03-06 18:11:06 -08001932/*
1933 * Initializes an alloca region assumed to be directly below sp for a native call:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001934 * 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 -08001935 * The final element on the stack is a pointer to the native code.
1936 *
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001937 * On entry, the stack has a standard callee-save frame above sp, and an alloca below it.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001938 * We need to fix this, as the handle scope needs to go into the callee-save frame.
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001939 *
Andreas Gampec147b002014-03-06 18:11:06 -08001940 * The return of this function denotes:
1941 * 1) How many bytes of the alloca can be released, if the value is non-negative.
1942 * 2) An error, if the value is negative.
1943 */
Mathieu Chartiere401d142015-04-22 13:56:20 -07001944extern "C" TwoWordReturn artQuickGenericJniTrampoline(Thread* self, ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -07001945 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001946 ArtMethod* called = *sp;
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001947 DCHECK(called->IsNative()) << PrettyMethod(called, true);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001948 uint32_t shorty_len = 0;
1949 const char* shorty = called->GetShorty(&shorty_len);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001950
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001951 // Run the visitor and update sp.
Ian Rogers59c07062014-10-10 13:03:39 -07001952 BuildGenericJniFrameVisitor visitor(self, called->IsStatic(), shorty, shorty_len, &sp);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001953 visitor.VisitArguments();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001954 visitor.FinalizeHandleScope(self);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001955
Andreas Gampec200a4a2014-06-16 18:39:09 -07001956 // Fix up managed-stack things in Thread.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001957 self->SetTopOfStack(sp);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001958
Ian Rogerse0dcd462014-03-08 15:21:04 -08001959 self->VerifyStack();
1960
Andreas Gampe90546832014-03-12 18:07:19 -07001961 // Start JNI, save the cookie.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001962 uint32_t cookie;
1963 if (called->IsSynchronized()) {
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001964 cookie = JniMethodStartSynchronized(visitor.GetFirstHandleScopeJObject(), self);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001965 if (self->IsExceptionPending()) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001966 self->PopHandleScope();
Andreas Gampec147b002014-03-06 18:11:06 -08001967 // A negative value denotes an error.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001968 return GetTwoWordFailureValue();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001969 }
1970 } else {
1971 cookie = JniMethodStart(self);
1972 }
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001973 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
Ian Rogerse0dcd462014-03-08 15:21:04 -08001974 *(sp32 - 1) = cookie;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001975
Andreas Gampe90546832014-03-12 18:07:19 -07001976 // Retrieve the stored native code.
Mathieu Chartier2d721012014-11-10 11:08:06 -08001977 void* nativeCode = called->GetEntryPointFromJni();
Andreas Gampe90546832014-03-12 18:07:19 -07001978
Andreas Gampe9a6a99a2014-03-14 07:52:20 -07001979 // There are two cases for the content of nativeCode:
1980 // 1) Pointer to the native function.
1981 // 2) Pointer to the trampoline for native code binding.
1982 // In the second case, we need to execute the binding and continue with the actual native function
1983 // pointer.
Andreas Gampe90546832014-03-12 18:07:19 -07001984 DCHECK(nativeCode != nullptr);
1985 if (nativeCode == GetJniDlsymLookupStub()) {
Ian Rogers04c31d22014-07-07 21:44:06 -07001986#if defined(__arm__) || defined(__aarch64__)
Andreas Gampe90546832014-03-12 18:07:19 -07001987 nativeCode = artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07001988#else
1989 nativeCode = artFindNativeMethod(self);
1990#endif
Andreas Gampe90546832014-03-12 18:07:19 -07001991
1992 if (nativeCode == nullptr) {
1993 DCHECK(self->IsExceptionPending()); // There should be an exception pending now.
Andreas Gampead615172014-04-04 16:20:13 -07001994
1995 // End JNI, as the assembly will move to deliver the exception.
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001996 jobject lock = called->IsSynchronized() ? visitor.GetFirstHandleScopeJObject() : nullptr;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001997 if (shorty[0] == 'L') {
Andreas Gampead615172014-04-04 16:20:13 -07001998 artQuickGenericJniEndJNIRef(self, cookie, nullptr, lock);
1999 } else {
2000 artQuickGenericJniEndJNINonRef(self, cookie, lock);
2001 }
2002
Andreas Gampec200a4a2014-06-16 18:39:09 -07002003 return GetTwoWordFailureValue();
Andreas Gampe90546832014-03-12 18:07:19 -07002004 }
2005 // Note that the native code pointer will be automatically set by artFindNativeMethod().
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002006 }
2007
Andreas Gampec200a4a2014-06-16 18:39:09 -07002008 // Return native code addr(lo) and bottom of alloca address(hi).
2009 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(visitor.GetBottomOfUsedArea()),
2010 reinterpret_cast<uintptr_t>(nativeCode));
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002011}
2012
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002013// Defined in quick_jni_entrypoints.cc.
2014extern uint64_t GenericJniMethodEnd(Thread* self, uint32_t saved_local_ref_cookie,
2015 jvalue result, uint64_t result_f, ArtMethod* called,
2016 HandleScope* handle_scope);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002017/*
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002018 * Is called after the native JNI code. Responsible for cleanup (handle scope, saved state) and
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002019 * unlocking.
2020 */
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002021extern "C" uint64_t artQuickGenericJniEndTrampoline(Thread* self,
2022 jvalue result,
2023 uint64_t result_f) {
2024 // We're here just back from a native call. We don't have the shared mutator lock at this point
2025 // yet until we call GoToRunnable() later in GenericJniMethodEnd(). Accessing objects or doing
2026 // anything that requires a mutator lock before that would cause problems as GC may have the
2027 // exclusive mutator lock and may be moving objects, etc.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002028 ArtMethod** sp = self->GetManagedStack()->GetTopQuickFrame();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002029 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002030 ArtMethod* called = *sp;
Ian Rogerse0dcd462014-03-08 15:21:04 -08002031 uint32_t cookie = *(sp32 - 1);
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002032 HandleScope* table = reinterpret_cast<HandleScope*>(reinterpret_cast<uint8_t*>(sp) + sizeof(*sp));
2033 return GenericJniMethodEnd(self, cookie, result, result_f, called, table);
Andreas Gampe2da88232014-02-27 12:26:20 -08002034}
2035
Andreas Gamped58342c2014-06-05 14:18:08 -07002036// We use TwoWordReturn to optimize scalar returns. We use the hi value for code, and the lo value
2037// for the method pointer.
Andreas Gampe51f76352014-05-21 08:28:48 -07002038//
Andreas Gamped58342c2014-06-05 14:18:08 -07002039// It is valid to use this, as at the usage points here (returns from C functions) we are assuming
Mathieu Chartier90443472015-07-16 20:32:27 -07002040// to hold the mutator lock (see SHARED_REQUIRES(Locks::mutator_lock_) annotations).
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002041
2042template<InvokeType type, bool access_check>
Mathieu Chartiere401d142015-04-22 13:56:20 -07002043static TwoWordReturn artInvokeCommon(uint32_t method_idx, mirror::Object* this_object, Thread* self,
2044 ArtMethod** sp) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002045 ScopedQuickEntrypointChecks sqec(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002046 DCHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs));
2047 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
2048 ArtMethod* method = FindMethodFast(method_idx, this_object, caller_method, access_check, type);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002049 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002050 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()->GetDexFile();
2051 uint32_t shorty_len;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002052 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(method_idx), &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002053 {
2054 // Remember the args in case a GC happens in FindMethodFromCode.
2055 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
2056 RememberForGcArgumentVisitor visitor(sp, type == kStatic, shorty, shorty_len, &soa);
2057 visitor.VisitArguments();
Andreas Gampe3a357142015-08-07 17:20:11 -07002058 method = FindMethodFromCode<type, access_check>(method_idx, &this_object, caller_method,
Mathieu Chartier0cd81352014-05-22 16:48:55 -07002059 self);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002060 visitor.FixupReferences();
2061 }
2062
Ian Rogerse0a02da2014-12-02 14:10:53 -08002063 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002064 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07002065 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002066 }
2067 }
2068 DCHECK(!self->IsExceptionPending());
2069 const void* code = method->GetEntryPointFromQuickCompiledCode();
2070
2071 // When we return, the caller will branch to this address, so it had better not be 0!
Ian Rogerse0a02da2014-12-02 14:10:53 -08002072 DCHECK(code != nullptr) << "Code was null in method: " << PrettyMethod(method)
Andreas Gampec200a4a2014-06-16 18:39:09 -07002073 << " location: "
2074 << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07002075
Andreas Gamped58342c2014-06-05 14:18:08 -07002076 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
2077 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002078}
2079
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01002080// Explicit artInvokeCommon template function declarations to please analysis tool.
2081#define EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(type, access_check) \
Mathieu Chartier90443472015-07-16 20:32:27 -07002082 template SHARED_REQUIRES(Locks::mutator_lock_) \
Mathieu Chartiere401d142015-04-22 13:56:20 -07002083 TwoWordReturn artInvokeCommon<type, access_check>( \
2084 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01002085
2086EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, false);
2087EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, true);
2088EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, false);
2089EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, true);
2090EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, false);
2091EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, true);
2092EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, false);
2093EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, true);
2094EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, false);
2095EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, true);
2096#undef EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL
2097
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002098// See comments in runtime_support_asm.S
Andreas Gampec200a4a2014-06-16 18:39:09 -07002099extern "C" TwoWordReturn artInvokeInterfaceTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002100 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -07002101 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002102 return artInvokeCommon<kInterface, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002103}
2104
Andreas Gampec200a4a2014-06-16 18:39:09 -07002105extern "C" TwoWordReturn artInvokeDirectTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002106 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -07002107 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002108 return artInvokeCommon<kDirect, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002109}
2110
Andreas Gampec200a4a2014-06-16 18:39:09 -07002111extern "C" TwoWordReturn artInvokeStaticTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002112 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -07002113 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002114 return artInvokeCommon<kStatic, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002115}
2116
Andreas Gampec200a4a2014-06-16 18:39:09 -07002117extern "C" TwoWordReturn artInvokeSuperTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002118 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -07002119 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002120 return artInvokeCommon<kSuper, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002121}
2122
Andreas Gampec200a4a2014-06-16 18:39:09 -07002123extern "C" TwoWordReturn artInvokeVirtualTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002124 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -07002125 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002126 return artInvokeCommon<kVirtual, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002127}
2128
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002129// Determine target of interface dispatch. This object is known non-null. First argument
2130// is there for consistency but should not be used, as some architectures overwrite it
2131// in the assembly trampoline.
2132extern "C" TwoWordReturn artInvokeInterfaceTrampoline(uint32_t deadbeef ATTRIBUTE_UNUSED,
Andreas Gampe51f76352014-05-21 08:28:48 -07002133 mirror::Object* this_object,
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002134 Thread* self,
2135 ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -07002136 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002137 ScopedQuickEntrypointChecks sqec(self);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002138 StackHandleScope<1> hs(self);
2139 Handle<mirror::Class> cls(hs.NewHandle(this_object->GetClass()));
2140
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002141 // The optimizing compiler currently does not inline methods that have an interface
2142 // invocation. We use the outer method directly to avoid fetching a stack map, which is
2143 // more expensive.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002144 ArtMethod* caller_method = QuickArgumentVisitor::GetOuterMethod(sp);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002145 DCHECK_EQ(caller_method, QuickArgumentVisitor::GetCallingMethod(sp));
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002146
2147 // Fetch the dex_method_idx of the target interface method from the caller.
2148 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
2149
2150 const DexFile::CodeItem* code_item = caller_method->GetCodeItem();
2151 CHECK_LT(dex_pc, code_item->insns_size_in_code_units_);
2152 const Instruction* instr = Instruction::At(&code_item->insns_[dex_pc]);
2153 Instruction::Code instr_code = instr->Opcode();
2154 CHECK(instr_code == Instruction::INVOKE_INTERFACE ||
2155 instr_code == Instruction::INVOKE_INTERFACE_RANGE)
2156 << "Unexpected call into interface trampoline: " << instr->DumpString(nullptr);
2157 uint32_t dex_method_idx;
2158 if (instr_code == Instruction::INVOKE_INTERFACE) {
2159 dex_method_idx = instr->VRegB_35c();
2160 } else {
2161 CHECK_EQ(instr_code, Instruction::INVOKE_INTERFACE_RANGE);
2162 dex_method_idx = instr->VRegB_3rc();
2163 }
2164
Mathieu Chartiere401d142015-04-22 13:56:20 -07002165 ArtMethod* interface_method = caller_method->GetDexCacheResolvedMethod(
2166 dex_method_idx, sizeof(void*));
2167 DCHECK(interface_method != nullptr) << dex_method_idx << " " << PrettyMethod(caller_method);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002168 ArtMethod* method = nullptr;
2169
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002170 if (LIKELY(interface_method->GetDexMethodIndex() != DexFile::kDexNoIndex)) {
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002171 // If the dex cache already resolved the interface method, look whether we have
2172 // a match in the ImtConflictTable.
2173 uint32_t imt_index = interface_method->GetDexMethodIndex();
2174 ArtMethod* conflict_method = cls->GetEmbeddedImTableEntry(
2175 imt_index % mirror::Class::kImtSize, sizeof(void*));
Alex Light9fc547a2016-03-31 14:34:33 -07002176 if (LIKELY(conflict_method->IsRuntimeMethod())) {
2177 ImtConflictTable* current_table = conflict_method->GetImtConflictTable(sizeof(void*));
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002178 DCHECK(current_table != nullptr);
2179 method = current_table->Lookup(interface_method, sizeof(void*));
Alex Light9fc547a2016-03-31 14:34:33 -07002180 } else {
2181 // It seems we aren't really a conflict method!
2182 method = cls->FindVirtualMethodForInterface(interface_method, sizeof(void*));
2183 }
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002184 if (method != nullptr) {
2185 return GetTwoWordSuccessValue(
2186 reinterpret_cast<uintptr_t>(method->GetEntryPointFromQuickCompiledCode()),
2187 reinterpret_cast<uintptr_t>(method));
2188 }
2189
2190 // No match, use the IfTable.
2191 method = cls->FindVirtualMethodForInterface(interface_method, sizeof(void*));
Ian Rogerse0a02da2014-12-02 14:10:53 -08002192 if (UNLIKELY(method == nullptr)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002193 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(
2194 interface_method, this_object, caller_method);
Andreas Gamped58342c2014-06-05 14:18:08 -07002195 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002196 }
2197 } else {
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002198 // The dex cache did not resolve the method, look it up in the dex file
2199 // of the caller,
Mathieu Chartier4edd8472015-06-01 10:47:36 -07002200 DCHECK_EQ(interface_method, Runtime::Current()->GetResolutionMethod());
Andreas Gampec200a4a2014-06-16 18:39:09 -07002201 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()
2202 ->GetDexFile();
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002203 uint32_t shorty_len;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002204 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(dex_method_idx),
2205 &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002206 {
2207 // Remember the args in case a GC happens in FindMethodFromCode.
2208 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
2209 RememberForGcArgumentVisitor visitor(sp, false, shorty, shorty_len, &soa);
2210 visitor.VisitArguments();
Andreas Gampe3a357142015-08-07 17:20:11 -07002211 method = FindMethodFromCode<kInterface, false>(dex_method_idx, &this_object, caller_method,
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002212 self);
2213 visitor.FixupReferences();
2214 }
2215
2216 if (UNLIKELY(method == nullptr)) {
2217 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07002218 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002219 }
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002220 interface_method = caller_method->GetDexCacheResolvedMethod(dex_method_idx, sizeof(void*));
2221 DCHECK(!interface_method->IsRuntimeMethod());
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002222 }
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002223
2224 // We arrive here if we have found an implementation, and it is not in the ImtConflictTable.
2225 // We create a new table with the new pair { interface_method, method }.
2226 uint32_t imt_index = interface_method->GetDexMethodIndex();
2227 ArtMethod* conflict_method = cls->GetEmbeddedImTableEntry(
2228 imt_index % mirror::Class::kImtSize, sizeof(void*));
Alex Light9fc547a2016-03-31 14:34:33 -07002229 if (conflict_method->IsRuntimeMethod()) {
Mathieu Chartier7f98c9a2016-04-14 10:49:19 -07002230 ArtMethod* new_conflict_method = Runtime::Current()->GetClassLinker()->AddMethodToConflictTable(
2231 cls.Get(),
2232 conflict_method,
2233 interface_method,
2234 method,
2235 /*force_new_conflict_method*/false);
2236 if (new_conflict_method != conflict_method) {
Alex Light9fc547a2016-03-31 14:34:33 -07002237 // Update the IMT if we create a new conflict method. No fence needed here, as the
2238 // data is consistent.
2239 cls->SetEmbeddedImTableEntry(imt_index % mirror::Class::kImtSize,
2240 new_conflict_method,
2241 sizeof(void*));
2242 }
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002243 }
2244
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002245 const void* code = method->GetEntryPointFromQuickCompiledCode();
2246
2247 // When we return, the caller will branch to this address, so it had better not be 0!
Ian Rogerse0a02da2014-12-02 14:10:53 -08002248 DCHECK(code != nullptr) << "Code was null in method: " << PrettyMethod(method)
Andreas Gampec200a4a2014-06-16 18:39:09 -07002249 << " location: " << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07002250
Andreas Gamped58342c2014-06-05 14:18:08 -07002251 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
2252 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002253}
2254
Ian Rogers848871b2013-08-05 10:56:33 -07002255} // namespace art