blob: 8c2dc3e42aea1ba7a35879b523e996783caca7c4 [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"
Igor Murashkin457e8742015-10-22 17:37:50 -070026#include "lambda/closure.h"
27#include "lambda/art_lambda_method.h"
Ian Rogerse0a02da2014-12-02 14:10:53 -080028#include "method_reference.h"
Ian Rogers848871b2013-08-05 10:56:33 -070029#include "mirror/class-inl.h"
Mathieu Chartier5f3ded42014-04-03 15:25:30 -070030#include "mirror/dex_cache-inl.h"
Igor Murashkin457e8742015-10-22 17:37:50 -070031#include "mirror/lambda_proxy.h"
Mathieu Chartierfc58af42015-04-16 18:00:39 -070032#include "mirror/method.h"
Ian Rogers848871b2013-08-05 10:56:33 -070033#include "mirror/object-inl.h"
34#include "mirror/object_array-inl.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010035#include "oat_quick_method_header.h"
Andreas Gampe639bdd12015-06-03 11:22:45 -070036#include "quick_exception_handler.h"
Ian Rogers848871b2013-08-05 10:56:33 -070037#include "runtime.h"
Ian Rogers53b8b092014-03-13 23:45:53 -070038#include "scoped_thread_state_change.h"
Andreas Gampeb3025922015-09-01 14:45:00 -070039#include "stack.h"
Daniel Mihalyieb076692014-08-22 17:33:31 +020040#include "debugger.h"
Ian Rogers848871b2013-08-05 10:56:33 -070041
42namespace art {
43
44// Visits the arguments as saved to the stack by a Runtime::kRefAndArgs callee save frame.
45class QuickArgumentVisitor {
Ian Rogers936b37f2014-02-14 00:52:24 -080046 // Number of bytes for each out register in the caller method's frame.
47 static constexpr size_t kBytesStackArgLocation = 4;
Alexei Zavjalov41c507a2014-05-15 16:02:46 +070048 // Frame size in bytes of a callee-save frame for RefsAndArgs.
49 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize =
50 GetCalleeSaveFrameSize(kRuntimeISA, Runtime::kRefsAndArgs);
Ian Rogers848871b2013-08-05 10:56:33 -070051#if defined(__arm__)
52 // The callee save frame is pointed to by SP.
53 // | argN | |
54 // | ... | |
55 // | arg4 | |
56 // | arg3 spill | | Caller's frame
57 // | arg2 spill | |
58 // | arg1 spill | |
59 // | Method* | ---
60 // | LR |
Zheng Xu5667fdb2014-10-23 18:29:55 +080061 // | ... | 4x6 bytes callee saves
62 // | R3 |
63 // | R2 |
64 // | R1 |
65 // | S15 |
66 // | : |
67 // | S0 |
68 // | | 4x2 bytes padding
Ian Rogers848871b2013-08-05 10:56:33 -070069 // | Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -050070 static constexpr bool kSplitPairAcrossRegisterAndStack = kArm32QuickCodeUseSoftFloat;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +000071 static constexpr bool kAlignPairRegister = !kArm32QuickCodeUseSoftFloat;
Zheng Xu5667fdb2014-10-23 18:29:55 +080072 static constexpr bool kQuickSoftFloatAbi = kArm32QuickCodeUseSoftFloat;
73 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = !kArm32QuickCodeUseSoftFloat;
Goran Jakovljevicff734982015-08-24 12:58:55 +000074 static constexpr bool kQuickSkipOddFpRegisters = false;
Zheng Xu5667fdb2014-10-23 18:29:55 +080075 static constexpr size_t kNumQuickGprArgs = 3;
76 static constexpr size_t kNumQuickFprArgs = kArm32QuickCodeUseSoftFloat ? 0 : 16;
Andreas Gampe1a5c4062015-01-15 12:10:47 -080077 static constexpr bool kGprFprLockstep = false;
Zheng Xub551fdc2014-07-25 11:49:42 +080078 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset =
79 arm::ArmCalleeSaveFpr1Offset(Runtime::kRefsAndArgs); // Offset of first FPR arg.
80 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset =
81 arm::ArmCalleeSaveGpr1Offset(Runtime::kRefsAndArgs); // Offset of first GPR arg.
82 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset =
83 arm::ArmCalleeSaveLrOffset(Runtime::kRefsAndArgs); // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -080084 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +000085 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -080086 }
Stuart Monteithb95a5342014-03-12 13:32:32 +000087#elif defined(__aarch64__)
88 // The callee save frame is pointed to by SP.
89 // | argN | |
90 // | ... | |
91 // | arg4 | |
92 // | arg3 spill | | Caller's frame
93 // | arg2 spill | |
94 // | arg1 spill | |
95 // | Method* | ---
96 // | LR |
Zheng Xub551fdc2014-07-25 11:49:42 +080097 // | X29 |
Stuart Monteithb95a5342014-03-12 13:32:32 +000098 // | : |
Serban Constantinescu9bd88b02015-04-22 16:24:46 +010099 // | X20 |
Stuart Monteithb95a5342014-03-12 13:32:32 +0000100 // | X7 |
101 // | : |
102 // | X1 |
Zheng Xub551fdc2014-07-25 11:49:42 +0800103 // | D7 |
Stuart Monteithb95a5342014-03-12 13:32:32 +0000104 // | : |
105 // | D0 |
106 // | | padding
107 // | Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500108 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000109 static constexpr bool kAlignPairRegister = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000110 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800111 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000112 static constexpr bool kQuickSkipOddFpRegisters = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000113 static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs.
114 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800115 static constexpr bool kGprFprLockstep = false;
Zheng Xub551fdc2014-07-25 11:49:42 +0800116 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset =
117 arm64::Arm64CalleeSaveFpr1Offset(Runtime::kRefsAndArgs); // Offset of first FPR arg.
118 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset =
119 arm64::Arm64CalleeSaveGpr1Offset(Runtime::kRefsAndArgs); // Offset of first GPR arg.
120 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset =
121 arm64::Arm64CalleeSaveLrOffset(Runtime::kRefsAndArgs); // Offset of return address.
Stuart Monteithb95a5342014-03-12 13:32:32 +0000122 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000123 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Stuart Monteithb95a5342014-03-12 13:32:32 +0000124 }
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800125#elif defined(__mips__) && !defined(__LP64__)
Ian Rogers848871b2013-08-05 10:56:33 -0700126 // The callee save frame is pointed to by SP.
127 // | argN | |
128 // | ... | |
129 // | arg4 | |
130 // | arg3 spill | | Caller's frame
131 // | arg2 spill | |
132 // | arg1 spill | |
133 // | Method* | ---
134 // | RA |
135 // | ... | callee saves
136 // | A3 | arg3
137 // | A2 | arg2
138 // | A1 | arg1
Goran Jakovljevicff734982015-08-24 12:58:55 +0000139 // | F15 |
140 // | F14 | f_arg1
141 // | F13 |
142 // | F12 | f_arg0
143 // | | padding
Ian Rogers848871b2013-08-05 10:56:33 -0700144 // | A0/Method* | <- sp
Goran Jakovljevicff734982015-08-24 12:58:55 +0000145 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
146 static constexpr bool kAlignPairRegister = true;
147 static constexpr bool kQuickSoftFloatAbi = false;
Zheng Xu5667fdb2014-10-23 18:29:55 +0800148 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000149 static constexpr bool kQuickSkipOddFpRegisters = true;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800150 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs.
Goran Jakovljevicff734982015-08-24 12:58:55 +0000151 static constexpr size_t kNumQuickFprArgs = 4; // 2 arguments passed in FPRs. Floats can be passed
152 // only in even numbered registers and each double
153 // occupies two registers.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800154 static constexpr bool kGprFprLockstep = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000155 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 16; // Offset of first FPR arg.
156 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 32; // Offset of first GPR arg.
157 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 76; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800158 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000159 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800160 }
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800161#elif defined(__mips__) && defined(__LP64__)
162 // The callee save frame is pointed to by SP.
163 // | argN | |
164 // | ... | |
165 // | arg4 | |
166 // | arg3 spill | | Caller's frame
167 // | arg2 spill | |
168 // | arg1 spill | |
169 // | Method* | ---
170 // | RA |
171 // | ... | callee saves
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800172 // | A7 | arg7
173 // | A6 | arg6
174 // | A5 | arg5
175 // | A4 | arg4
176 // | A3 | arg3
177 // | A2 | arg2
178 // | A1 | arg1
Goran Jakovljevicff734982015-08-24 12:58:55 +0000179 // | F19 | f_arg7
180 // | F18 | f_arg6
181 // | F17 | f_arg5
182 // | F16 | f_arg4
183 // | F15 | f_arg3
184 // | F14 | f_arg2
185 // | F13 | f_arg1
186 // | F12 | f_arg0
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800187 // | | padding
188 // | A0/Method* | <- sp
189 // NOTE: for Mip64, when A0 is skipped, F0 is also skipped.
Douglas Leungd18e0832015-02-09 15:22:26 -0800190 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800191 static constexpr bool kAlignPairRegister = false;
192 static constexpr bool kQuickSoftFloatAbi = false;
193 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000194 static constexpr bool kQuickSkipOddFpRegisters = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800195 static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs.
196 static constexpr size_t kNumQuickFprArgs = 7; // 7 arguments passed in FPRs.
197 static constexpr bool kGprFprLockstep = true;
198
199 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 24; // Offset of first FPR arg (F1).
200 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 80; // Offset of first GPR arg (A1).
201 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 200; // Offset of return address.
202 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
203 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
204 }
Ian Rogers848871b2013-08-05 10:56:33 -0700205#elif defined(__i386__)
206 // The callee save frame is pointed to by SP.
207 // | argN | |
208 // | ... | |
209 // | arg4 | |
210 // | arg3 spill | | Caller's frame
211 // | arg2 spill | |
212 // | arg1 spill | |
213 // | Method* | ---
214 // | Return |
215 // | EBP,ESI,EDI | callee saves
216 // | EBX | arg3
217 // | EDX | arg2
218 // | ECX | arg1
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000219 // | XMM3 | float arg 4
220 // | XMM2 | float arg 3
221 // | XMM1 | float arg 2
222 // | XMM0 | float arg 1
Ian Rogers848871b2013-08-05 10:56:33 -0700223 // | EAX/Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500224 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000225 static constexpr bool kAlignPairRegister = false;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000226 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800227 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000228 static constexpr bool kQuickSkipOddFpRegisters = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800229 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs.
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000230 static constexpr size_t kNumQuickFprArgs = 4; // 4 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800231 static constexpr bool kGprFprLockstep = false;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000232 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 4; // Offset of first FPR arg.
233 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 4 + 4*8; // Offset of first GPR arg.
234 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 28 + 4*8; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800235 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000236 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800237 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800238#elif defined(__x86_64__)
Ian Rogers936b37f2014-02-14 00:52:24 -0800239 // The callee save frame is pointed to by SP.
240 // | argN | |
241 // | ... | |
242 // | reg. arg spills | | Caller's frame
243 // | Method* | ---
244 // | Return |
245 // | R15 | callee save
246 // | R14 | callee save
247 // | R13 | callee save
248 // | R12 | callee save
249 // | R9 | arg5
250 // | R8 | arg4
251 // | RSI/R6 | arg1
252 // | RBP/R5 | callee save
253 // | RBX/R3 | callee save
254 // | RDX/R2 | arg2
255 // | RCX/R1 | arg3
256 // | XMM7 | float arg 8
257 // | XMM6 | float arg 7
258 // | XMM5 | float arg 6
259 // | XMM4 | float arg 5
260 // | XMM3 | float arg 4
261 // | XMM2 | float arg 3
262 // | XMM1 | float arg 2
263 // | XMM0 | float arg 1
264 // | Padding |
265 // | RDI/Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500266 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000267 static constexpr bool kAlignPairRegister = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800268 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800269 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000270 static constexpr bool kQuickSkipOddFpRegisters = false;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700271 static constexpr size_t kNumQuickGprArgs = 5; // 5 arguments passed in GPRs.
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700272 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800273 static constexpr bool kGprFprLockstep = false;
Ian Rogers936b37f2014-02-14 00:52:24 -0800274 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 16; // Offset of first FPR arg.
Serguei Katkovc3801912014-07-08 17:21:53 +0700275 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 80 + 4*8; // Offset of first GPR arg.
276 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 168 + 4*8; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800277 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
278 switch (gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000279 case 0: return (4 * GetBytesPerGprSpillLocation(kRuntimeISA));
280 case 1: return (1 * GetBytesPerGprSpillLocation(kRuntimeISA));
281 case 2: return (0 * GetBytesPerGprSpillLocation(kRuntimeISA));
282 case 3: return (5 * GetBytesPerGprSpillLocation(kRuntimeISA));
283 case 4: return (6 * GetBytesPerGprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800284 default:
Andreas Gampec200a4a2014-06-16 18:39:09 -0700285 LOG(FATAL) << "Unexpected GPR index: " << gpr_index;
286 return 0;
Ian Rogers936b37f2014-02-14 00:52:24 -0800287 }
288 }
Ian Rogers848871b2013-08-05 10:56:33 -0700289#else
290#error "Unsupported architecture"
Ian Rogers848871b2013-08-05 10:56:33 -0700291#endif
292
Ian Rogers936b37f2014-02-14 00:52:24 -0800293 public:
Sebastien Hertza836bc92014-11-25 16:30:53 +0100294 // Special handling for proxy methods. Proxy methods are instance methods so the
295 // 'this' object is the 1st argument. They also have the same frame layout as the
296 // kRefAndArgs runtime method. Since 'this' is a reference, it is located in the
297 // 1st GPR.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700298 static mirror::Object* GetProxyThisObject(ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -0700299 SHARED_REQUIRES(Locks::mutator_lock_) {
Igor Murashkin457e8742015-10-22 17:37:50 -0700300 // TODO: Lambda proxies only set up a frame when debugging
301 CHECK((*sp)->IsReflectProxyMethod() || ((*sp)->IsLambdaProxyMethod() /*&& kIsDebugBuild*/));
Sebastien Hertza836bc92014-11-25 16:30:53 +0100302 CHECK_GT(kNumQuickGprArgs, 0u);
303 constexpr uint32_t kThisGprIndex = 0u; // 'this' is in the 1st GPR.
304 size_t this_arg_offset = kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset +
305 GprIndexToGprOffset(kThisGprIndex);
306 uint8_t* this_arg_address = reinterpret_cast<uint8_t*>(sp) + this_arg_offset;
307 return reinterpret_cast<StackReference<mirror::Object>*>(this_arg_address)->AsMirrorPtr();
308 }
309
Mathieu Chartier90443472015-07-16 20:32:27 -0700310 static ArtMethod* GetCallingMethod(ArtMethod** sp) SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700311 DCHECK((*sp)->IsCalleeSaveMethod());
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +0100312 return GetCalleeSaveMethodCaller(sp, Runtime::kRefsAndArgs);
313 }
314
Mathieu Chartier90443472015-07-16 20:32:27 -0700315 static ArtMethod* GetOuterMethod(ArtMethod** sp) SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700316 DCHECK((*sp)->IsCalleeSaveMethod());
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100317 uint8_t* previous_sp =
318 reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700319 return *reinterpret_cast<ArtMethod**>(previous_sp);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100320 }
321
Mathieu Chartier90443472015-07-16 20:32:27 -0700322 static uint32_t GetCallingDexPc(ArtMethod** sp) SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700323 DCHECK((*sp)->IsCalleeSaveMethod());
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100324 const size_t callee_frame_size = GetCalleeSaveFrameSize(kRuntimeISA, Runtime::kRefsAndArgs);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700325 ArtMethod** caller_sp = reinterpret_cast<ArtMethod**>(
326 reinterpret_cast<uintptr_t>(sp) + callee_frame_size);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100327 uintptr_t outer_pc = QuickArgumentVisitor::GetCallingPc(sp);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100328 const OatQuickMethodHeader* current_code = (*caller_sp)->GetOatQuickMethodHeader(outer_pc);
329 uintptr_t outer_pc_offset = current_code->NativeQuickPcOffset(outer_pc);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100330
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100331 if (current_code->IsOptimized()) {
332 CodeInfo code_info = current_code->GetOptimizedCodeInfo();
David Brazdilf677ebf2015-05-29 16:29:43 +0100333 StackMapEncoding encoding = code_info.ExtractEncoding();
334 StackMap stack_map = code_info.GetStackMapForNativePcOffset(outer_pc_offset, encoding);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100335 DCHECK(stack_map.IsValid());
David Brazdilf677ebf2015-05-29 16:29:43 +0100336 if (stack_map.HasInlineInfo(encoding)) {
337 InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map, encoding);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100338 return inline_info.GetDexPcAtDepth(inline_info.GetDepth() - 1);
339 } else {
David Brazdilf677ebf2015-05-29 16:29:43 +0100340 return stack_map.GetDexPc(encoding);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100341 }
342 } else {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100343 return current_code->ToDexPc(*caller_sp, outer_pc);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100344 }
Ian Rogers848871b2013-08-05 10:56:33 -0700345 }
346
Ian Rogers936b37f2014-02-14 00:52:24 -0800347 // For the given quick ref and args quick frame, return the caller's PC.
Mathieu Chartier90443472015-07-16 20:32:27 -0700348 static uintptr_t GetCallingPc(ArtMethod** sp) SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700349 DCHECK((*sp)->IsCalleeSaveMethod());
Ian Rogers13735952014-10-08 12:43:28 -0700350 uint8_t* lr = reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_LrOffset;
Ian Rogers848871b2013-08-05 10:56:33 -0700351 return *reinterpret_cast<uintptr_t*>(lr);
352 }
353
Mathieu Chartiere401d142015-04-22 13:56:20 -0700354 QuickArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty,
Mathieu Chartier90443472015-07-16 20:32:27 -0700355 uint32_t shorty_len) SHARED_REQUIRES(Locks::mutator_lock_) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700356 is_static_(is_static), shorty_(shorty), shorty_len_(shorty_len),
Ian Rogers13735952014-10-08 12:43:28 -0700357 gpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset),
358 fpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset),
359 stack_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize
Mathieu Chartiere401d142015-04-22 13:56:20 -0700360 + sizeof(ArtMethod*)), // Skip ArtMethod*.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800361 gpr_index_(0), fpr_index_(0), fpr_double_index_(0), stack_index_(0),
362 cur_type_(Primitive::kPrimVoid), is_split_long_or_double_(false) {
Andreas Gampe575e78c2014-11-03 23:41:03 -0800363 static_assert(kQuickSoftFloatAbi == (kNumQuickFprArgs == 0),
364 "Number of Quick FPR arguments unexpected");
365 static_assert(!(kQuickSoftFloatAbi && kQuickDoubleRegAlignedFloatBackFilled),
366 "Double alignment unexpected");
Zheng Xu5667fdb2014-10-23 18:29:55 +0800367 // For register alignment, we want to assume that counters(fpr_double_index_) are even if the
368 // next register is even.
Andreas Gampe575e78c2014-11-03 23:41:03 -0800369 static_assert(!kQuickDoubleRegAlignedFloatBackFilled || kNumQuickFprArgs % 2 == 0,
370 "Number of Quick FPR arguments not even");
Mathieu Chartiere401d142015-04-22 13:56:20 -0700371 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), sizeof(void*));
Zheng Xu5667fdb2014-10-23 18:29:55 +0800372 }
Ian Rogers848871b2013-08-05 10:56:33 -0700373
374 virtual ~QuickArgumentVisitor() {}
375
376 virtual void Visit() = 0;
377
Ian Rogers936b37f2014-02-14 00:52:24 -0800378 Primitive::Type GetParamPrimitiveType() const {
379 return cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700380 }
381
Ian Rogers13735952014-10-08 12:43:28 -0700382 uint8_t* GetParamAddress() const {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800383 if (!kQuickSoftFloatAbi) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800384 Primitive::Type type = GetParamPrimitiveType();
385 if (UNLIKELY((type == Primitive::kPrimDouble) || (type == Primitive::kPrimFloat))) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800386 if (type == Primitive::kPrimDouble && kQuickDoubleRegAlignedFloatBackFilled) {
387 if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) {
388 return fpr_args_ + (fpr_double_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
389 }
390 } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000391 return fpr_args_ + (fpr_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800392 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700393 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers936b37f2014-02-14 00:52:24 -0800394 }
395 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800396 if (gpr_index_ < kNumQuickGprArgs) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800397 return gpr_args_ + GprIndexToGprOffset(gpr_index_);
398 }
399 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700400 }
401
402 bool IsSplitLongOrDouble() const {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700403 if ((GetBytesPerGprSpillLocation(kRuntimeISA) == 4) ||
404 (GetBytesPerFprSpillLocation(kRuntimeISA) == 4)) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800405 return is_split_long_or_double_;
406 } else {
407 return false; // An optimization for when GPR and FPRs are 64bit.
408 }
Ian Rogers848871b2013-08-05 10:56:33 -0700409 }
410
Ian Rogers936b37f2014-02-14 00:52:24 -0800411 bool IsParamAReference() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700412 return GetParamPrimitiveType() == Primitive::kPrimNot;
413 }
414
Ian Rogers936b37f2014-02-14 00:52:24 -0800415 bool IsParamALongOrDouble() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700416 Primitive::Type type = GetParamPrimitiveType();
417 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
418 }
419
420 uint64_t ReadSplitLongParam() const {
Nicolas Geoffray425f2392015-01-08 14:52:29 +0000421 // The splitted long is always available through the stack.
422 return *reinterpret_cast<uint64_t*>(stack_args_
423 + stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700424 }
425
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800426 void IncGprIndex() {
427 gpr_index_++;
428 if (kGprFprLockstep) {
429 fpr_index_++;
430 }
431 }
432
433 void IncFprIndex() {
434 fpr_index_++;
435 if (kGprFprLockstep) {
436 gpr_index_++;
437 }
438 }
439
Mathieu Chartier90443472015-07-16 20:32:27 -0700440 void VisitArguments() SHARED_REQUIRES(Locks::mutator_lock_) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800441 // (a) 'stack_args_' should point to the first method's argument
442 // (b) whatever the argument type it is, the 'stack_index_' should
443 // be moved forward along with every visiting.
Ian Rogers936b37f2014-02-14 00:52:24 -0800444 gpr_index_ = 0;
445 fpr_index_ = 0;
Zheng Xu5667fdb2014-10-23 18:29:55 +0800446 if (kQuickDoubleRegAlignedFloatBackFilled) {
447 fpr_double_index_ = 0;
448 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800449 stack_index_ = 0;
450 if (!is_static_) { // Handle this.
451 cur_type_ = Primitive::kPrimNot;
452 is_split_long_or_double_ = false;
Ian Rogers848871b2013-08-05 10:56:33 -0700453 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800454 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800455 if (kNumQuickGprArgs > 0) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800456 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800457 }
Ian Rogers848871b2013-08-05 10:56:33 -0700458 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800459 for (uint32_t shorty_index = 1; shorty_index < shorty_len_; ++shorty_index) {
460 cur_type_ = Primitive::GetType(shorty_[shorty_index]);
461 switch (cur_type_) {
462 case Primitive::kPrimNot:
463 case Primitive::kPrimBoolean:
464 case Primitive::kPrimByte:
465 case Primitive::kPrimChar:
466 case Primitive::kPrimShort:
467 case Primitive::kPrimInt:
468 is_split_long_or_double_ = false;
469 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800470 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800471 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800472 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800473 }
474 break;
475 case Primitive::kPrimFloat:
476 is_split_long_or_double_ = false;
477 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800478 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800479 if (kQuickSoftFloatAbi) {
480 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800481 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800482 }
483 } else {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800484 if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800485 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800486 if (kQuickDoubleRegAlignedFloatBackFilled) {
487 // Double should not overlap with float.
488 // For example, if fpr_index_ = 3, fpr_double_index_ should be at least 4.
489 fpr_double_index_ = std::max(fpr_double_index_, RoundUp(fpr_index_, 2));
490 // Float should not overlap with double.
491 if (fpr_index_ % 2 == 0) {
492 fpr_index_ = std::max(fpr_double_index_, fpr_index_);
493 }
Goran Jakovljevicff734982015-08-24 12:58:55 +0000494 } else if (kQuickSkipOddFpRegisters) {
495 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800496 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800497 }
498 }
499 break;
500 case Primitive::kPrimDouble:
501 case Primitive::kPrimLong:
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800502 if (kQuickSoftFloatAbi || (cur_type_ == Primitive::kPrimLong)) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000503 if (cur_type_ == Primitive::kPrimLong && kAlignPairRegister && gpr_index_ == 0) {
Goran Jakovljevicff734982015-08-24 12:58:55 +0000504 // Currently, this is only for ARM and MIPS, where the first available parameter
505 // register is R1 (on ARM) or A1 (on MIPS). So we skip it, and use R2 (on ARM) or
506 // A2 (on MIPS) instead.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800507 IncGprIndex();
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000508 }
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000509 is_split_long_or_double_ = (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) &&
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800510 ((gpr_index_ + 1) == kNumQuickGprArgs);
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500511 if (!kSplitPairAcrossRegisterAndStack && is_split_long_or_double_) {
512 // We don't want to split this. Pass over this register.
513 gpr_index_++;
514 is_split_long_or_double_ = false;
515 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800516 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800517 if (kBytesStackArgLocation == 4) {
518 stack_index_+= 2;
519 } else {
520 CHECK_EQ(kBytesStackArgLocation, 8U);
521 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800522 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700523 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800524 IncGprIndex();
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000525 if (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700526 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800527 IncGprIndex();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700528 }
529 }
530 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800531 } else {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000532 is_split_long_or_double_ = (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) &&
Zheng Xu5667fdb2014-10-23 18:29:55 +0800533 ((fpr_index_ + 1) == kNumQuickFprArgs) && !kQuickDoubleRegAlignedFloatBackFilled;
Ian Rogers936b37f2014-02-14 00:52:24 -0800534 Visit();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700535 if (kBytesStackArgLocation == 4) {
536 stack_index_+= 2;
Ian Rogers936b37f2014-02-14 00:52:24 -0800537 } else {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700538 CHECK_EQ(kBytesStackArgLocation, 8U);
539 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800540 }
Zheng Xu5667fdb2014-10-23 18:29:55 +0800541 if (kQuickDoubleRegAlignedFloatBackFilled) {
542 if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) {
543 fpr_double_index_ += 2;
544 // Float should not overlap with double.
545 if (fpr_index_ % 2 == 0) {
546 fpr_index_ = std::max(fpr_double_index_, fpr_index_);
547 }
548 }
549 } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800550 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800551 if (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) {
552 if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800553 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800554 }
555 }
556 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800557 }
558 break;
559 default:
560 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty_;
561 }
Ian Rogers848871b2013-08-05 10:56:33 -0700562 }
563 }
564
Andreas Gampec200a4a2014-06-16 18:39:09 -0700565 protected:
Ian Rogers848871b2013-08-05 10:56:33 -0700566 const bool is_static_;
567 const char* const shorty_;
568 const uint32_t shorty_len_;
Andreas Gampec200a4a2014-06-16 18:39:09 -0700569
570 private:
Ian Rogers13735952014-10-08 12:43:28 -0700571 uint8_t* const gpr_args_; // Address of GPR arguments in callee save frame.
572 uint8_t* const fpr_args_; // Address of FPR arguments in callee save frame.
573 uint8_t* const stack_args_; // Address of stack arguments in caller's frame.
Ian Rogers936b37f2014-02-14 00:52:24 -0800574 uint32_t gpr_index_; // Index into spilled GPRs.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800575 // Index into spilled FPRs.
576 // In case kQuickDoubleRegAlignedFloatBackFilled, it may index a hole while fpr_double_index_
577 // holds a higher register number.
578 uint32_t fpr_index_;
579 // Index into spilled FPRs for aligned double.
580 // Only used when kQuickDoubleRegAlignedFloatBackFilled. Next available double register indexed in
581 // terms of singles, may be behind fpr_index.
582 uint32_t fpr_double_index_;
Ian Rogers936b37f2014-02-14 00:52:24 -0800583 uint32_t stack_index_; // Index into arguments on the stack.
584 // The current type of argument during VisitArguments.
585 Primitive::Type cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700586 // Does a 64bit parameter straddle the register and stack arguments?
587 bool is_split_long_or_double_;
588};
589
Sebastien Hertza836bc92014-11-25 16:30:53 +0100590// Returns the 'this' object of a proxy method. This function is only used by StackVisitor. It
591// allows to use the QuickArgumentVisitor constants without moving all the code in its own module.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700592extern "C" mirror::Object* artQuickGetProxyThisObject(ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -0700593 SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertza836bc92014-11-25 16:30:53 +0100594 return QuickArgumentVisitor::GetProxyThisObject(sp);
595}
596
Ian Rogers848871b2013-08-05 10:56:33 -0700597// Visits arguments on the stack placing them into the shadow frame.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800598class BuildQuickShadowFrameVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700599 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700600 BuildQuickShadowFrameVisitor(ArtMethod** sp, bool is_static, const char* shorty,
601 uint32_t shorty_len, ShadowFrame* sf, size_t first_arg_reg) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700602 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), sf_(sf), cur_reg_(first_arg_reg) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700603
Mathieu Chartier90443472015-07-16 20:32:27 -0700604 void Visit() SHARED_REQUIRES(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700605
606 private:
Ian Rogers936b37f2014-02-14 00:52:24 -0800607 ShadowFrame* const sf_;
608 uint32_t cur_reg_;
Ian Rogers848871b2013-08-05 10:56:33 -0700609
Dragos Sbirleabd136a22013-08-13 18:07:04 -0700610 DISALLOW_COPY_AND_ASSIGN(BuildQuickShadowFrameVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700611};
612
Andreas Gampec200a4a2014-06-16 18:39:09 -0700613void BuildQuickShadowFrameVisitor::Visit() {
Ian Rogers9758f792014-03-13 09:02:55 -0700614 Primitive::Type type = GetParamPrimitiveType();
615 switch (type) {
616 case Primitive::kPrimLong: // Fall-through.
617 case Primitive::kPrimDouble:
618 if (IsSplitLongOrDouble()) {
619 sf_->SetVRegLong(cur_reg_, ReadSplitLongParam());
620 } else {
621 sf_->SetVRegLong(cur_reg_, *reinterpret_cast<jlong*>(GetParamAddress()));
622 }
623 ++cur_reg_;
624 break;
625 case Primitive::kPrimNot: {
626 StackReference<mirror::Object>* stack_ref =
627 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
628 sf_->SetVRegReference(cur_reg_, stack_ref->AsMirrorPtr());
629 }
630 break;
631 case Primitive::kPrimBoolean: // Fall-through.
632 case Primitive::kPrimByte: // Fall-through.
633 case Primitive::kPrimChar: // Fall-through.
634 case Primitive::kPrimShort: // Fall-through.
635 case Primitive::kPrimInt: // Fall-through.
636 case Primitive::kPrimFloat:
637 sf_->SetVReg(cur_reg_, *reinterpret_cast<jint*>(GetParamAddress()));
638 break;
639 case Primitive::kPrimVoid:
640 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700641 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -0700642 }
643 ++cur_reg_;
644}
645
Mathieu Chartiere401d142015-04-22 13:56:20 -0700646extern "C" uint64_t artQuickToInterpreterBridge(ArtMethod* method, Thread* self, ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -0700647 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers848871b2013-08-05 10:56:33 -0700648 // Ensure we don't get thread suspension until the object arguments are safely in the shadow
649 // frame.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700650 ScopedQuickEntrypointChecks sqec(self);
Ian Rogers848871b2013-08-05 10:56:33 -0700651
Alex Light9139e002015-10-09 15:59:48 -0700652 if (UNLIKELY(!method->IsInvokable())) {
653 method->ThrowInvocationTimeError();
Ian Rogers848871b2013-08-05 10:56:33 -0700654 return 0;
Andreas Gampe639bdd12015-06-03 11:22:45 -0700655 }
656
657 JValue tmp_value;
658 ShadowFrame* deopt_frame = self->PopStackedShadowFrame(
659 StackedShadowFrameType::kSingleFrameDeoptimizationShadowFrame, false);
660 const DexFile::CodeItem* code_item = method->GetCodeItem();
661 DCHECK(code_item != nullptr) << PrettyMethod(method);
662 ManagedStack fragment;
663
664 DCHECK(!method->IsNative()) << PrettyMethod(method);
665 uint32_t shorty_len = 0;
666 auto* non_proxy_method = method->GetInterfaceMethodIfProxy(sizeof(void*));
667 const char* shorty = non_proxy_method->GetShorty(&shorty_len);
668
669 JValue result;
670
671 if (deopt_frame != nullptr) {
672 // Coming from single-frame deopt.
673
674 if (kIsDebugBuild) {
675 // Sanity-check: are the methods as expected? We check that the last shadow frame (the bottom
676 // of the call-stack) corresponds to the called method.
677 ShadowFrame* linked = deopt_frame;
678 while (linked->GetLink() != nullptr) {
679 linked = linked->GetLink();
680 }
681 CHECK_EQ(method, linked->GetMethod()) << PrettyMethod(method) << " "
682 << PrettyMethod(linked->GetMethod());
683 }
684
685 if (VLOG_IS_ON(deopt)) {
686 // Print out the stack to verify that it was a single-frame deopt.
687 LOG(INFO) << "Continue-ing from deopt. Stack is:";
688 QuickExceptionHandler::DumpFramesWithType(self, true);
689 }
690
691 mirror::Throwable* pending_exception = nullptr;
692 self->PopDeoptimizationContext(&result, &pending_exception);
693
694 // Push a transition back into managed code onto the linked list in thread.
695 self->PushManagedStackFragment(&fragment);
696
697 // Ensure that the stack is still in order.
698 if (kIsDebugBuild) {
699 class DummyStackVisitor : public StackVisitor {
700 public:
701 explicit DummyStackVisitor(Thread* self_in) SHARED_REQUIRES(Locks::mutator_lock_)
702 : StackVisitor(self_in, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {}
703
704 bool VisitFrame() OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
705 // Nothing to do here. In a debug build, SanityCheckFrame will do the work in the walking
706 // logic. Just always say we want to continue.
707 return true;
708 }
709 };
710 DummyStackVisitor dsv(self);
711 dsv.WalkStack();
712 }
713
714 // Restore the exception that was pending before deoptimization then interpret the
715 // deoptimized frames.
716 if (pending_exception != nullptr) {
717 self->SetException(pending_exception);
718 }
719 interpreter::EnterInterpreterFromDeoptimize(self, deopt_frame, &result);
Ian Rogers848871b2013-08-05 10:56:33 -0700720 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -0700721 const char* old_cause = self->StartAssertNoThreadSuspension(
722 "Building interpreter shadow frame");
Ian Rogers848871b2013-08-05 10:56:33 -0700723 uint16_t num_regs = code_item->registers_size_;
Andreas Gampec200a4a2014-06-16 18:39:09 -0700724 // No last shadow coming from quick.
Andreas Gampeb3025922015-09-01 14:45:00 -0700725 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
Andreas Gampe03ec9302015-08-27 17:41:47 -0700726 CREATE_SHADOW_FRAME(num_regs, /* link */ nullptr, method, /* dex pc */ 0);
Andreas Gampeb3025922015-09-01 14:45:00 -0700727 ShadowFrame* shadow_frame = shadow_frame_unique_ptr.get();
Ian Rogers848871b2013-08-05 10:56:33 -0700728 size_t first_arg_reg = code_item->registers_size_ - code_item->ins_size_;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700729 BuildQuickShadowFrameVisitor shadow_frame_builder(sp, method->IsStatic(), shorty, shorty_len,
Ian Rogers936b37f2014-02-14 00:52:24 -0800730 shadow_frame, first_arg_reg);
Ian Rogers848871b2013-08-05 10:56:33 -0700731 shadow_frame_builder.VisitArguments();
Ian Rogerse94652f2014-12-02 11:13:19 -0800732 const bool needs_initialization =
733 method->IsStatic() && !method->GetDeclaringClass()->IsInitialized();
Ian Rogers848871b2013-08-05 10:56:33 -0700734 // Push a transition back into managed code onto the linked list in thread.
Ian Rogers848871b2013-08-05 10:56:33 -0700735 self->PushManagedStackFragment(&fragment);
736 self->PushShadowFrame(shadow_frame);
737 self->EndAssertNoThreadSuspension(old_cause);
738
Ian Rogerse94652f2014-12-02 11:13:19 -0800739 if (needs_initialization) {
Ian Rogers848871b2013-08-05 10:56:33 -0700740 // Ensure static method's class is initialized.
Ian Rogerse94652f2014-12-02 11:13:19 -0800741 StackHandleScope<1> hs(self);
742 Handle<mirror::Class> h_class(hs.NewHandle(shadow_frame->GetMethod()->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700743 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
Ian Rogerse94652f2014-12-02 11:13:19 -0800744 DCHECK(Thread::Current()->IsExceptionPending()) << PrettyMethod(shadow_frame->GetMethod());
Ian Rogers848871b2013-08-05 10:56:33 -0700745 self->PopManagedStackFragment(fragment);
746 return 0;
747 }
748 }
Daniel Mihalyieb076692014-08-22 17:33:31 +0200749
Andreas Gampe639bdd12015-06-03 11:22:45 -0700750 result = interpreter::EnterInterpreterFromEntryPoint(self, code_item, shadow_frame);
Ian Rogers848871b2013-08-05 10:56:33 -0700751 }
Andreas Gampe639bdd12015-06-03 11:22:45 -0700752
753 // Pop transition.
754 self->PopManagedStackFragment(fragment);
755
756 // Request a stack deoptimization if needed
757 ArtMethod* caller = QuickArgumentVisitor::GetCallingMethod(sp);
758 if (UNLIKELY(Dbg::IsForcedInterpreterNeededForUpcall(self, caller))) {
759 // Push the context of the deoptimization stack so we can restore the return value and the
760 // exception before executing the deoptimized frames.
761 self->PushDeoptimizationContext(result, shorty[0] == 'L', self->GetException());
762
763 // Set special exception to cause deoptimization.
764 self->SetException(Thread::GetDeoptimizationException());
765 }
766
767 // No need to restore the args since the method has already been run by the interpreter.
768 return result.GetJ();
Ian Rogers848871b2013-08-05 10:56:33 -0700769}
770
771// Visits arguments on the stack placing them into the args vector, Object* arguments are converted
772// to jobjects.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800773class BuildQuickArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700774 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700775 BuildQuickArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty, uint32_t shorty_len,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700776 ScopedObjectAccessUnchecked* soa, std::vector<jvalue>* args) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700777 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa), args_(args) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700778
Mathieu Chartier90443472015-07-16 20:32:27 -0700779 void Visit() SHARED_REQUIRES(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700780
Mathieu Chartier90443472015-07-16 20:32:27 -0700781 void FixupReferences() SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800782
Ian Rogers848871b2013-08-05 10:56:33 -0700783 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700784 ScopedObjectAccessUnchecked* const soa_;
785 std::vector<jvalue>* const args_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800786 // References which we must update when exiting in case the GC moved the objects.
Ian Rogers700a4022014-05-19 16:49:03 -0700787 std::vector<std::pair<jobject, StackReference<mirror::Object>*>> references_;
Ian Rogers9758f792014-03-13 09:02:55 -0700788
Ian Rogers848871b2013-08-05 10:56:33 -0700789 DISALLOW_COPY_AND_ASSIGN(BuildQuickArgumentVisitor);
790};
791
Ian Rogers9758f792014-03-13 09:02:55 -0700792void BuildQuickArgumentVisitor::Visit() {
793 jvalue val;
794 Primitive::Type type = GetParamPrimitiveType();
795 switch (type) {
796 case Primitive::kPrimNot: {
797 StackReference<mirror::Object>* stack_ref =
798 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
799 val.l = soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
800 references_.push_back(std::make_pair(val.l, stack_ref));
801 break;
802 }
803 case Primitive::kPrimLong: // Fall-through.
804 case Primitive::kPrimDouble:
805 if (IsSplitLongOrDouble()) {
806 val.j = ReadSplitLongParam();
807 } else {
808 val.j = *reinterpret_cast<jlong*>(GetParamAddress());
809 }
810 break;
811 case Primitive::kPrimBoolean: // Fall-through.
812 case Primitive::kPrimByte: // Fall-through.
813 case Primitive::kPrimChar: // Fall-through.
814 case Primitive::kPrimShort: // Fall-through.
815 case Primitive::kPrimInt: // Fall-through.
816 case Primitive::kPrimFloat:
817 val.i = *reinterpret_cast<jint*>(GetParamAddress());
818 break;
819 case Primitive::kPrimVoid:
820 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700821 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -0700822 }
823 args_->push_back(val);
824}
825
826void BuildQuickArgumentVisitor::FixupReferences() {
827 // Fixup any references which may have changed.
828 for (const auto& pair : references_) {
829 pair.second->Assign(soa_->Decode<mirror::Object*>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -0700830 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -0700831 }
832}
833
Ian Rogers848871b2013-08-05 10:56:33 -0700834// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
835// which is responsible for recording callee save registers. We explicitly place into jobjects the
836// incoming reference arguments (so they survive GC). We invoke the invocation handler, which is a
837// field within the proxy object, which will box the primitive arguments and deal with error cases.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700838extern "C" uint64_t artQuickProxyInvokeHandler(
839 ArtMethod* proxy_method, mirror::Object* receiver, Thread* self, ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -0700840 SHARED_REQUIRES(Locks::mutator_lock_) {
Igor Murashkin457e8742015-10-22 17:37:50 -0700841 DCHECK(proxy_method->GetDeclaringClass()->IsReflectProxyClass()) << PrettyMethod(proxy_method);
842 DCHECK(proxy_method->IsReflectProxyMethod()) << PrettyMethod(proxy_method);
843 DCHECK(receiver->GetClass()->IsReflectProxyClass()) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700844 // Ensure we don't get thread suspension until the object arguments are safely in jobjects.
845 const char* old_cause =
846 self->StartAssertNoThreadSuspension("Adding to IRT proxy object arguments");
847 // Register the top of the managed stack, making stack crawlable.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700848 DCHECK_EQ((*sp), proxy_method) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700849 self->VerifyStack();
850 // Start new JNI local reference state.
851 JNIEnvExt* env = self->GetJniEnv();
852 ScopedObjectAccessUnchecked soa(env);
853 ScopedJniEnvLocalRefState env_state(env);
854 // Create local ref. copies of proxy method and the receiver.
855 jobject rcvr_jobj = soa.AddLocalReference<jobject>(receiver);
856
857 // Placing arguments into args vector and remove the receiver.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700858 ArtMethod* non_proxy_method = proxy_method->GetInterfaceMethodIfProxy(sizeof(void*));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700859 CHECK(!non_proxy_method->IsStatic()) << PrettyMethod(proxy_method) << " "
Andreas Gampec200a4a2014-06-16 18:39:09 -0700860 << PrettyMethod(non_proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700861 std::vector<jvalue> args;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700862 uint32_t shorty_len = 0;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700863 const char* shorty = non_proxy_method->GetShorty(&shorty_len);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700864 BuildQuickArgumentVisitor local_ref_visitor(sp, false, shorty, shorty_len, &soa, &args);
Brian Carlstromd3633d52013-08-20 21:06:26 -0700865
Ian Rogers848871b2013-08-05 10:56:33 -0700866 local_ref_visitor.VisitArguments();
Brian Carlstromd3633d52013-08-20 21:06:26 -0700867 DCHECK_GT(args.size(), 0U) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700868 args.erase(args.begin());
869
870 // Convert proxy method into expected interface method.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700871 ArtMethod* interface_method = proxy_method->FindOverriddenMethod(sizeof(void*));
Ian Rogerse0a02da2014-12-02 14:10:53 -0800872 DCHECK(interface_method != nullptr) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700873 DCHECK(!interface_method->IsProxyMethod()) << PrettyMethod(interface_method);
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700874 self->EndAssertNoThreadSuspension(old_cause);
875 jobject interface_method_jobj = soa.AddLocalReference<jobject>(
876 mirror::Method::CreateFromArtMethod(soa.Self(), interface_method));
Ian Rogers848871b2013-08-05 10:56:33 -0700877
878 // All naked Object*s should now be in jobjects, so its safe to go into the main invoke code
879 // that performs allocations.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700880 JValue result = InvokeProxyInvocationHandler(soa, shorty, rcvr_jobj, interface_method_jobj, args);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800881 // Restore references which might have moved.
882 local_ref_visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -0700883 return result.GetJ();
884}
885
Igor Murashkin457e8742015-10-22 17:37:50 -0700886extern "C" uint64_t artQuickLambdaProxyInvokeHandler(
887 ArtMethod* proxy_method, mirror::LambdaProxy* receiver, Thread* self, ArtMethod** sp)
888 SHARED_REQUIRES(Locks::mutator_lock_) {
889 using lambda::ShortyFieldType;
890
891 DCHECK(proxy_method->GetDeclaringClass()->IsLambdaProxyClass()) << PrettyMethod(proxy_method);
892 DCHECK(proxy_method->IsLambdaProxyMethod()) << PrettyMethod(proxy_method);
893 DCHECK(receiver->GetClass()->IsLambdaProxyClass()) << PrettyMethod(proxy_method);
894
895 lambda::Closure* lambda_closure = receiver->GetClosure();
896 DCHECK(lambda_closure != nullptr); // Should've NPEd during the invoke-interface.
897 // Learned lambdas have their own implementation of the SAM, they must not go through here.
898 DCHECK(lambda_closure->GetLambdaInfo()->IsInnateLambda());
899 ArtMethod* target_method = lambda_closure->GetTargetMethod();
900
901 // Lambda targets are always static.
902 // TODO: This should really be a target_method->IsLambda(), once we add the access flag.
903 CHECK(target_method->IsStatic()) << PrettyMethod(proxy_method) << " "
904 << PrettyMethod(target_method);
905
906 // Ensure we don't get thread suspension until the object arguments are safely in jobjects.
907 const char* old_cause =
908 self->StartAssertNoThreadSuspension("Adding to IRT/SF lambda proxy object arguments");
909 // Register the top of the managed stack, making stack crawlable.
910 DCHECK_EQ((*sp), proxy_method) << PrettyMethod(proxy_method);
911 self->VerifyStack();
912 // Start new JNI local reference state.
913 JNIEnvExt* env = self->GetJniEnv();
914 ScopedObjectAccessUnchecked soa(env);
915
916 // Placing arguments into args vector and remove the receiver.
917 ArtMethod* non_proxy_method = proxy_method->GetInterfaceMethodIfProxy(sizeof(void*));
918 CHECK(!non_proxy_method->IsStatic()) << PrettyMethod(proxy_method) << " "
919 << PrettyMethod(non_proxy_method);
920 uint32_t shorty_len = 0;
921 const char* shorty = non_proxy_method->GetShorty(/*out*/&shorty_len);
922
923 std::vector<jvalue> args;
924 // Make a quick visitor so we can restore the refs incase they move after a GC.
925 BuildQuickArgumentVisitor local_ref_visitor(sp,
926 false /*is_static*/,
927 shorty,
928 shorty_len,
929 &soa,
930 /*out*/&args);
931 local_ref_visitor.VisitArguments();
932
933 static_assert(lambda::kClosureIsStoredAsLong,
934 "Need to update this code once closures are no "
935 "longer treated as a 'long' in quick abi");
936
937 // Allocate one vreg more than usual because we need to convert our
938 // receiver Object (1 vreg) into a long (2 vregs).
939 // TODO: Ugly... move to traits instead?
940 const uint32_t first_arg_reg = ShortyFieldType(ShortyFieldType::kLambda).GetVirtualRegisterCount()
941 - ShortyFieldType(ShortyFieldType::kObject).GetVirtualRegisterCount();
942 const uint32_t num_vregs = lambda_closure->GetLambdaInfo()->GetArgumentVRegCount();
943 DCHECK_GE(num_vregs, first_arg_reg);
944 if (kIsDebugBuild) {
945 const char* method_shorty = non_proxy_method->GetShorty();
946 DCHECK_NE(*method_shorty, '\0') << method_shorty;
947 const char* arg_shorty = method_shorty + 1; // Skip return type.
948
949 // Proxy method should have an object (1 vreg) receiver,
950 // Lambda method should have a lambda (2 vregs) receiver.
951 // -- All other args are the same as before.
952 // -- Make sure vreg count is what we thought it was.
953 uint32_t non_proxy_num_vregs =
954 ShortyFieldType::CountVirtualRegistersRequired(arg_shorty) // doesn't count receiver
955 + ShortyFieldType(ShortyFieldType::kObject).GetVirtualRegisterCount(); // implicit receiver
956
957 CHECK_EQ(non_proxy_num_vregs + first_arg_reg, num_vregs)
958 << PrettyMethod(non_proxy_method) << " " << PrettyMethod(lambda_closure->GetTargetMethod());
959 }
960
961 ShadowFrameAllocaUniquePtr shadow_frame = CREATE_SHADOW_FRAME(num_vregs,
962 /*link*/nullptr,
963 target_method,
964 /*dex_pc*/0);
965
966 // Copy our proxy method caller's arguments into this ShadowFrame.
967 BuildQuickShadowFrameVisitor local_sf_visitor(sp,
968 /*is_static*/false,
969 shorty,
970 shorty_len,
971 shadow_frame.get(),
972 first_arg_reg);
973
974 local_sf_visitor.VisitArguments();
975 // Now fix up the arguments, with each ArgK being a vreg:
976
977 // (Before):
978 // Arg0 = proxy receiver (LambdaProxy)
979 // Arg1 = first-user defined argument
980 // Arg2 = second user-defined argument
981 // ....
982 // ArgN = ...
983
984 // (After)
985 // Arg0 = closure (hi)
986 // Arg1 = closure (lo) = 0x00 on 32-bit
987 // Arg2 = <?> (first user-defined argument)
988 // Arg3 = <?> (first user-defined argument)
989 // ...
990 // argN+1 = ...
991
992 // Transformation diagram:
993 /*
994 Arg0 Arg2 Arg3 ... ArgN
995 | \ \ \
996 | \ \ \
997 ClHi ClLo Arg2 Arg3 ... ArgN:
998 */
999
1000 // 1) memmove vregs 1-N into 2-N+1
1001 uint32_t* shadow_frame_vregs = shadow_frame->GetVRegArgs(/*i*/0);
1002 if (lambda::kClosureIsStoredAsLong ||
1003 sizeof(void*) != sizeof(mirror::CompressedReference<mirror::LambdaProxy>)) {
1004 // Suspending here would be very bad since we are doing a raw memmove
1005
1006 // Move the primitive vregs over.
1007 {
1008 size_t shadow_frame_vregs_size = num_vregs;
1009 memmove(shadow_frame_vregs + first_arg_reg,
1010 shadow_frame_vregs,
1011 shadow_frame_vregs_size - first_arg_reg);
1012 }
1013
1014 // Move the reference vregs over.
1015 if (LIKELY(shadow_frame->HasReferenceArray())) {
1016 uint32_t* shadow_frame_references = shadow_frame_vregs + num_vregs;
1017 size_t shadow_frame_references_size = num_vregs;
1018 memmove(shadow_frame_references + first_arg_reg,
1019 shadow_frame_references,
1020 shadow_frame_references_size - first_arg_reg);
1021 }
1022
1023 static_assert(lambda::kClosureSupportsReadBarrier == false,
1024 "Using this memmove code with a read barrier GC seems like it could be unsafe.");
1025
1026 static_assert(sizeof(mirror::CompressedReference<mirror::LambdaProxy>) == sizeof(uint32_t),
1027 "This block of code assumes a compressed reference fits into exactly 1 vreg");
1028 }
1029 // 2) replace proxy receiver with lambda
1030 shadow_frame->SetVRegLong(0, static_cast<int64_t>(reinterpret_cast<uintptr_t>(lambda_closure)));
1031
1032 // OK: After we do the invoke, the target method takes over managing the arguments
1033 // and we won't ever access the shadow frame again (if any references moved).
1034 self->EndAssertNoThreadSuspension(old_cause);
1035
1036 // The shadow frame vreg contents are now 'owned' by the Invoke method, and
1037 // will be managed by it during a GC despite being a raw uint32_t array.
1038 // We however have no guarantee that it is updated on the way out, so do not read out of the
1039 // shadow frame after this call.
1040 JValue result;
1041 target_method->Invoke(self,
1042 shadow_frame_vregs,
1043 num_vregs * sizeof(uint32_t),
1044 /*out*/&result,
1045 target_method->GetShorty());
1046
1047 // Restore references on the proxy caller stack frame which might have moved.
1048 // -- This is necessary because the QuickFrameInfo is just the generic runtime "RefsAndArgs"
1049 // which means that the regular stack visitor wouldn't know how to GC-move any references
1050 // that we spilled ourselves in the proxy stub.
1051 local_ref_visitor.FixupReferences();
1052 return result.GetJ();
1053}
1054
Ian Rogers848871b2013-08-05 10:56:33 -07001055// Read object references held in arguments from quick frames and place in a JNI local references,
1056// so they don't get garbage collected.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001057class RememberForGcArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -07001058 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07001059 RememberForGcArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty,
1060 uint32_t shorty_len, ScopedObjectAccessUnchecked* soa) :
Andreas Gampec200a4a2014-06-16 18:39:09 -07001061 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa) {}
Ian Rogers848871b2013-08-05 10:56:33 -07001062
Mathieu Chartier90443472015-07-16 20:32:27 -07001063 void Visit() SHARED_REQUIRES(Locks::mutator_lock_) OVERRIDE;
Mathieu Chartier07d447b2013-09-26 11:57:43 -07001064
Mathieu Chartier90443472015-07-16 20:32:27 -07001065 void FixupReferences() SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers848871b2013-08-05 10:56:33 -07001066
1067 private:
Ian Rogers9758f792014-03-13 09:02:55 -07001068 ScopedObjectAccessUnchecked* const soa_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -08001069 // References which we must update when exiting in case the GC moved the objects.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001070 std::vector<std::pair<jobject, StackReference<mirror::Object>*> > references_;
1071
Mathieu Chartier590fee92013-09-13 13:46:47 -07001072 DISALLOW_COPY_AND_ASSIGN(RememberForGcArgumentVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -07001073};
1074
Ian Rogers9758f792014-03-13 09:02:55 -07001075void RememberForGcArgumentVisitor::Visit() {
1076 if (IsParamAReference()) {
1077 StackReference<mirror::Object>* stack_ref =
1078 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
1079 jobject reference =
1080 soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
1081 references_.push_back(std::make_pair(reference, stack_ref));
1082 }
1083}
1084
1085void RememberForGcArgumentVisitor::FixupReferences() {
1086 // Fixup any references which may have changed.
1087 for (const auto& pair : references_) {
1088 pair.second->Assign(soa_->Decode<mirror::Object*>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001089 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -07001090 }
1091}
1092
Ian Rogers848871b2013-08-05 10:56:33 -07001093// Lazily resolve a method for quick. Called by stub code.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001094extern "C" const void* artQuickResolutionTrampoline(
1095 ArtMethod* called, mirror::Object* receiver, Thread* self, ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -07001096 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe3b45ef22015-05-26 21:34:09 -07001097 // The resolution trampoline stashes the resolved method into the callee-save frame to transport
1098 // it. Thus, when exiting, the stack cannot be verified (as the resolved method most likely
1099 // does not have the same stack layout as the callee-save method).
1100 ScopedQuickEntrypointChecks sqec(self, kIsDebugBuild, false);
Ian Rogers848871b2013-08-05 10:56:33 -07001101 // Start new JNI local reference state
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001102 JNIEnvExt* env = self->GetJniEnv();
Ian Rogers848871b2013-08-05 10:56:33 -07001103 ScopedObjectAccessUnchecked soa(env);
1104 ScopedJniEnvLocalRefState env_state(env);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001105 const char* old_cause = self->StartAssertNoThreadSuspension("Quick method resolution set up");
Ian Rogers848871b2013-08-05 10:56:33 -07001106
1107 // Compute details about the called method (avoid GCs)
1108 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Ian Rogers848871b2013-08-05 10:56:33 -07001109 InvokeType invoke_type;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001110 MethodReference called_method(nullptr, 0);
1111 const bool called_method_known_on_entry = !called->IsRuntimeMethod();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001112 ArtMethod* caller = nullptr;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001113 if (!called_method_known_on_entry) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01001114 caller = QuickArgumentVisitor::GetCallingMethod(sp);
1115 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
Ian Rogers848871b2013-08-05 10:56:33 -07001116 const DexFile::CodeItem* code;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001117 called_method.dex_file = caller->GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001118 code = caller->GetCodeItem();
Ian Rogers848871b2013-08-05 10:56:33 -07001119 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
1120 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
1121 Instruction::Code instr_code = instr->Opcode();
1122 bool is_range;
1123 switch (instr_code) {
1124 case Instruction::INVOKE_DIRECT:
1125 invoke_type = kDirect;
1126 is_range = false;
1127 break;
1128 case Instruction::INVOKE_DIRECT_RANGE:
1129 invoke_type = kDirect;
1130 is_range = true;
1131 break;
1132 case Instruction::INVOKE_STATIC:
1133 invoke_type = kStatic;
1134 is_range = false;
1135 break;
1136 case Instruction::INVOKE_STATIC_RANGE:
1137 invoke_type = kStatic;
1138 is_range = true;
1139 break;
1140 case Instruction::INVOKE_SUPER:
1141 invoke_type = kSuper;
1142 is_range = false;
1143 break;
1144 case Instruction::INVOKE_SUPER_RANGE:
1145 invoke_type = kSuper;
1146 is_range = true;
1147 break;
1148 case Instruction::INVOKE_VIRTUAL:
1149 invoke_type = kVirtual;
1150 is_range = false;
1151 break;
1152 case Instruction::INVOKE_VIRTUAL_RANGE:
1153 invoke_type = kVirtual;
1154 is_range = true;
1155 break;
1156 case Instruction::INVOKE_INTERFACE:
1157 invoke_type = kInterface;
1158 is_range = false;
1159 break;
1160 case Instruction::INVOKE_INTERFACE_RANGE:
1161 invoke_type = kInterface;
1162 is_range = true;
1163 break;
1164 default:
Ian Rogerse0a02da2014-12-02 14:10:53 -08001165 LOG(FATAL) << "Unexpected call into trampoline: " << instr->DumpString(nullptr);
1166 UNREACHABLE();
Ian Rogers848871b2013-08-05 10:56:33 -07001167 }
Ian Rogerse0a02da2014-12-02 14:10:53 -08001168 called_method.dex_method_index = (is_range) ? instr->VRegB_3rc() : instr->VRegB_35c();
Ian Rogers848871b2013-08-05 10:56:33 -07001169 } else {
1170 invoke_type = kStatic;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001171 called_method.dex_file = called->GetDexFile();
1172 called_method.dex_method_index = called->GetDexMethodIndex();
Ian Rogers848871b2013-08-05 10:56:33 -07001173 }
1174 uint32_t shorty_len;
1175 const char* shorty =
Ian Rogerse0a02da2014-12-02 14:10:53 -08001176 called_method.dex_file->GetMethodShorty(
1177 called_method.dex_file->GetMethodId(called_method.dex_method_index), &shorty_len);
Mathieu Chartier590fee92013-09-13 13:46:47 -07001178 RememberForGcArgumentVisitor visitor(sp, invoke_type == kStatic, shorty, shorty_len, &soa);
Ian Rogers848871b2013-08-05 10:56:33 -07001179 visitor.VisitArguments();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001180 self->EndAssertNoThreadSuspension(old_cause);
Ian Rogerse0a02da2014-12-02 14:10:53 -08001181 const bool virtual_or_interface = invoke_type == kVirtual || invoke_type == kInterface;
Ian Rogers848871b2013-08-05 10:56:33 -07001182 // Resolve method filling in dex cache.
Ian Rogerse0a02da2014-12-02 14:10:53 -08001183 if (!called_method_known_on_entry) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001184 StackHandleScope<1> hs(self);
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001185 mirror::Object* dummy = nullptr;
1186 HandleWrapper<mirror::Object> h_receiver(
1187 hs.NewHandleWrapper(virtual_or_interface ? &receiver : &dummy));
Ian Rogerse0a02da2014-12-02 14:10:53 -08001188 DCHECK_EQ(caller->GetDexFile(), called_method.dex_file);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001189 called = linker->ResolveMethod(self, called_method.dex_method_index, caller, invoke_type);
Ian Rogers848871b2013-08-05 10:56:33 -07001190 }
Ian Rogerse0a02da2014-12-02 14:10:53 -08001191 const void* code = nullptr;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001192 if (LIKELY(!self->IsExceptionPending())) {
Ian Rogers848871b2013-08-05 10:56:33 -07001193 // Incompatible class change should have been handled in resolve method.
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001194 CHECK(!called->CheckIncompatibleClassChange(invoke_type))
1195 << PrettyMethod(called) << " " << invoke_type;
Mathieu Chartier55871bf2014-02-27 10:24:50 -08001196 if (virtual_or_interface) {
1197 // Refine called method based on receiver.
1198 CHECK(receiver != nullptr) << invoke_type;
Mingyao Yangf4867782014-05-05 11:55:02 -07001199
Mathieu Chartiere401d142015-04-22 13:56:20 -07001200 ArtMethod* orig_called = called;
Mathieu Chartier55871bf2014-02-27 10:24:50 -08001201 if (invoke_type == kVirtual) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001202 called = receiver->GetClass()->FindVirtualMethodForVirtual(called, sizeof(void*));
Mathieu Chartier55871bf2014-02-27 10:24:50 -08001203 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001204 called = receiver->GetClass()->FindVirtualMethodForInterface(called, sizeof(void*));
Mathieu Chartier55871bf2014-02-27 10:24:50 -08001205 }
Mingyao Yangf4867782014-05-05 11:55:02 -07001206
1207 CHECK(called != nullptr) << PrettyMethod(orig_called) << " "
1208 << PrettyTypeOf(receiver) << " "
1209 << invoke_type << " " << orig_called->GetVtableIndex();
1210
Ian Rogers83883d72013-10-21 21:07:24 -07001211 // 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 -08001212 // of the sharpened method avoiding dirtying the dex cache if possible.
Ian Rogers00f15272014-12-02 16:55:46 -08001213 // Note, called_method.dex_method_index references the dex method before the
1214 // FindVirtualMethodFor... This is ok for FindDexMethodIndexInOtherDexFile that only cares
1215 // about the name and signature.
1216 uint32_t update_dex_cache_method_index = called->GetDexMethodIndex();
Vladimir Marko05792b92015-08-03 11:56:49 +01001217 if (!called->HasSameDexCacheResolvedMethods(caller, sizeof(void*))) {
Ian Rogers83883d72013-10-21 21:07:24 -07001218 // Calling from one dex file to another, need to compute the method index appropriate to
Vladimir Markobbcc0c02014-02-03 14:08:42 +00001219 // the caller's dex file. Since we get here only if the original called was a runtime
1220 // method, we've got the correct dex_file and a dex_method_idx from above.
Ian Rogerse0a02da2014-12-02 14:10:53 -08001221 DCHECK(!called_method_known_on_entry);
1222 DCHECK_EQ(caller->GetDexFile(), called_method.dex_file);
1223 const DexFile* caller_dex_file = called_method.dex_file;
1224 uint32_t caller_method_name_and_sig_index = called_method.dex_method_index;
1225 update_dex_cache_method_index =
1226 called->FindDexMethodIndexInOtherDexFile(*caller_dex_file,
1227 caller_method_name_and_sig_index);
1228 }
1229 if ((update_dex_cache_method_index != DexFile::kDexNoIndex) &&
Mathieu Chartiere401d142015-04-22 13:56:20 -07001230 (caller->GetDexCacheResolvedMethod(
1231 update_dex_cache_method_index, sizeof(void*)) != called)) {
1232 caller->SetDexCacheResolvedMethod(update_dex_cache_method_index, called, sizeof(void*));
Ian Rogers83883d72013-10-21 21:07:24 -07001233 }
Mathieu Chartiere4a91bb2015-01-28 13:11:44 -08001234 } else if (invoke_type == kStatic) {
1235 const auto called_dex_method_idx = called->GetDexMethodIndex();
1236 // For static invokes, we may dispatch to the static method in the superclass but resolve
1237 // using the subclass. To prevent getting slow paths on each invoke, we force set the
1238 // resolved method for the super class dex method index if we are in the same dex file.
1239 // b/19175856
1240 if (called->GetDexFile() == called_method.dex_file &&
1241 called_method.dex_method_index != called_dex_method_idx) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001242 called->GetDexCache()->SetResolvedMethod(called_dex_method_idx, called, sizeof(void*));
Mathieu Chartiere4a91bb2015-01-28 13:11:44 -08001243 }
Ian Rogers83883d72013-10-21 21:07:24 -07001244 }
Daniel Mihalyieb076692014-08-22 17:33:31 +02001245
Ian Rogers848871b2013-08-05 10:56:33 -07001246 // Ensure that the called method's class is initialized.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001247 StackHandleScope<1> hs(soa.Self());
1248 Handle<mirror::Class> called_class(hs.NewHandle(called->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -07001249 linker->EnsureInitialized(soa.Self(), called_class, true, true);
Ian Rogers848871b2013-08-05 10:56:33 -07001250 if (LIKELY(called_class->IsInitialized())) {
Daniel Mihalyieb076692014-08-22 17:33:31 +02001251 if (UNLIKELY(Dbg::IsForcedInterpreterNeededForResolution(self, called))) {
1252 // If we are single-stepping or the called method is deoptimized (by a
1253 // breakpoint, for example), then we have to execute the called method
1254 // with the interpreter.
1255 code = GetQuickToInterpreterBridge();
1256 } else if (UNLIKELY(Dbg::IsForcedInstrumentationNeededForResolution(self, caller))) {
1257 // If the caller is deoptimized (by a breakpoint, for example), we have to
1258 // continue its execution with interpreter when returning from the called
1259 // method. Because we do not want to execute the called method with the
1260 // interpreter, we wrap its execution into the instrumentation stubs.
1261 // When the called method returns, it will execute the instrumentation
1262 // exit hook that will determine the need of the interpreter with a call
1263 // to Dbg::IsForcedInterpreterNeededForUpcall and deoptimize the stack if
1264 // it is needed.
1265 code = GetQuickInstrumentationEntryPoint();
1266 } else {
1267 code = called->GetEntryPointFromQuickCompiledCode();
1268 }
Ian Rogers848871b2013-08-05 10:56:33 -07001269 } else if (called_class->IsInitializing()) {
Daniel Mihalyieb076692014-08-22 17:33:31 +02001270 if (UNLIKELY(Dbg::IsForcedInterpreterNeededForResolution(self, called))) {
1271 // If we are single-stepping or the called method is deoptimized (by a
1272 // breakpoint, for example), then we have to execute the called method
1273 // with the interpreter.
1274 code = GetQuickToInterpreterBridge();
1275 } else if (invoke_type == kStatic) {
Ian Rogers848871b2013-08-05 10:56:33 -07001276 // Class is still initializing, go to oat and grab code (trampoline must be left in place
1277 // until class is initialized to stop races between threads).
Ian Rogersef7d42f2014-01-06 12:55:46 -08001278 code = linker->GetQuickOatCodeFor(called);
Ian Rogers848871b2013-08-05 10:56:33 -07001279 } else {
1280 // No trampoline for non-static methods.
Ian Rogersef7d42f2014-01-06 12:55:46 -08001281 code = called->GetEntryPointFromQuickCompiledCode();
Ian Rogers848871b2013-08-05 10:56:33 -07001282 }
1283 } else {
1284 DCHECK(called_class->IsErroneous());
1285 }
1286 }
Ian Rogerse0a02da2014-12-02 14:10:53 -08001287 CHECK_EQ(code == nullptr, self->IsExceptionPending());
Mathieu Chartier07d447b2013-09-26 11:57:43 -07001288 // Fixup any locally saved objects may have moved during a GC.
1289 visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -07001290 // Place called method in callee-save frame to be placed as first argument to quick method.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001291 *sp = called;
1292
Ian Rogers848871b2013-08-05 10:56:33 -07001293 return code;
1294}
1295
Andreas Gampec147b002014-03-06 18:11:06 -08001296/*
1297 * This class uses a couple of observations to unite the different calling conventions through
1298 * a few constants.
1299 *
1300 * 1) Number of registers used for passing is normally even, so counting down has no penalty for
1301 * possible alignment.
1302 * 2) Known 64b architectures store 8B units on the stack, both for integral and floating point
1303 * types, so using uintptr_t is OK. Also means that we can use kRegistersNeededX to denote
1304 * when we have to split things
1305 * 3) The only soft-float, Arm, is 32b, so no widening needs to be taken into account for floats
1306 * and we can use Int handling directly.
1307 * 4) Only 64b architectures widen, and their stack is aligned 8B anyways, so no padding code
1308 * necessary when widening. Also, widening of Ints will take place implicitly, and the
1309 * extension should be compatible with Aarch64, which mandates copying the available bits
1310 * into LSB and leaving the rest unspecified.
1311 * 5) Aligning longs and doubles is necessary on arm only, and it's the same in registers and on
1312 * the stack.
1313 * 6) There is only little endian.
1314 *
1315 *
1316 * Actual work is supposed to be done in a delegate of the template type. The interface is as
1317 * follows:
1318 *
1319 * void PushGpr(uintptr_t): Add a value for the next GPR
1320 *
1321 * void PushFpr4(float): Add a value for the next FPR of size 32b. Is only called if we need
1322 * padding, that is, think the architecture is 32b and aligns 64b.
1323 *
1324 * void PushFpr8(uint64_t): Push a double. We _will_ call this on 32b, it's the callee's job to
1325 * split this if necessary. The current state will have aligned, if
1326 * necessary.
1327 *
1328 * void PushStack(uintptr_t): Push a value to the stack.
1329 *
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001330 * uintptr_t PushHandleScope(mirror::Object* ref): Add a reference to the HandleScope. This _will_ have nullptr,
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001331 * as this might be important for null initialization.
Andreas Gampec147b002014-03-06 18:11:06 -08001332 * Must return the jobject, that is, the reference to the
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001333 * entry in the HandleScope (nullptr if necessary).
Andreas Gampec147b002014-03-06 18:11:06 -08001334 *
1335 */
Andreas Gampec200a4a2014-06-16 18:39:09 -07001336template<class T> class BuildNativeCallFrameStateMachine {
Andreas Gampec147b002014-03-06 18:11:06 -08001337 public:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001338#if defined(__arm__)
1339 // TODO: These are all dummy values!
Andreas Gampec147b002014-03-06 18:11:06 -08001340 static constexpr bool kNativeSoftFloatAbi = true;
1341 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs, r0-r3
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001342 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
1343
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001344 static constexpr size_t kRegistersNeededForLong = 2;
1345 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -08001346 static constexpr bool kMultiRegistersAligned = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001347 static constexpr bool kMultiFPRegistersWidened = false;
1348 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001349 static constexpr bool kAlignLongOnStack = true;
1350 static constexpr bool kAlignDoubleOnStack = true;
Stuart Monteithb95a5342014-03-12 13:32:32 +00001351#elif defined(__aarch64__)
1352 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
1353 static constexpr size_t kNumNativeGprArgs = 8; // 6 arguments passed in GPRs.
1354 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
1355
1356 static constexpr size_t kRegistersNeededForLong = 1;
1357 static constexpr size_t kRegistersNeededForDouble = 1;
1358 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001359 static constexpr bool kMultiFPRegistersWidened = false;
1360 static constexpr bool kMultiGPRegistersWidened = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +00001361 static constexpr bool kAlignLongOnStack = false;
1362 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001363#elif defined(__mips__) && !defined(__LP64__)
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001364 static constexpr bool kNativeSoftFloatAbi = true; // This is a hard float ABI.
Douglas Leung735b8552014-10-31 12:21:40 -07001365 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs.
1366 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001367
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001368 static constexpr size_t kRegistersNeededForLong = 2;
1369 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -08001370 static constexpr bool kMultiRegistersAligned = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001371 static constexpr bool kMultiFPRegistersWidened = true;
1372 static constexpr bool kMultiGPRegistersWidened = false;
Douglas Leung735b8552014-10-31 12:21:40 -07001373 static constexpr bool kAlignLongOnStack = true;
1374 static constexpr bool kAlignDoubleOnStack = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001375#elif defined(__mips__) && defined(__LP64__)
1376 // Let the code prepare GPRs only and we will load the FPRs with same data.
1377 static constexpr bool kNativeSoftFloatAbi = true;
1378 static constexpr size_t kNumNativeGprArgs = 8;
1379 static constexpr size_t kNumNativeFprArgs = 0;
1380
1381 static constexpr size_t kRegistersNeededForLong = 1;
1382 static constexpr size_t kRegistersNeededForDouble = 1;
1383 static constexpr bool kMultiRegistersAligned = false;
1384 static constexpr bool kMultiFPRegistersWidened = false;
1385 static constexpr bool kMultiGPRegistersWidened = true;
1386 static constexpr bool kAlignLongOnStack = false;
1387 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001388#elif defined(__i386__)
1389 // TODO: Check these!
Andreas Gampec147b002014-03-06 18:11:06 -08001390 static constexpr bool kNativeSoftFloatAbi = false; // Not using int registers for fp
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001391 static constexpr size_t kNumNativeGprArgs = 0; // 6 arguments passed in GPRs.
1392 static constexpr size_t kNumNativeFprArgs = 0; // 8 arguments passed in FPRs.
1393
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001394 static constexpr size_t kRegistersNeededForLong = 2;
1395 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001396 static constexpr bool kMultiRegistersAligned = false; // x86 not using regs, anyways
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001397 static constexpr bool kMultiFPRegistersWidened = false;
1398 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001399 static constexpr bool kAlignLongOnStack = false;
1400 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001401#elif defined(__x86_64__)
1402 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
1403 static constexpr size_t kNumNativeGprArgs = 6; // 6 arguments passed in GPRs.
1404 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
1405
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001406 static constexpr size_t kRegistersNeededForLong = 1;
1407 static constexpr size_t kRegistersNeededForDouble = 1;
Andreas Gampec147b002014-03-06 18:11:06 -08001408 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001409 static constexpr bool kMultiFPRegistersWidened = false;
1410 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001411 static constexpr bool kAlignLongOnStack = false;
1412 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001413#else
1414#error "Unsupported architecture"
1415#endif
1416
Andreas Gampec147b002014-03-06 18:11:06 -08001417 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001418 explicit BuildNativeCallFrameStateMachine(T* delegate)
1419 : gpr_index_(kNumNativeGprArgs),
1420 fpr_index_(kNumNativeFprArgs),
1421 stack_entries_(0),
1422 delegate_(delegate) {
Andreas Gampec147b002014-03-06 18:11:06 -08001423 // For register alignment, we want to assume that counters (gpr_index_, fpr_index_) are even iff
1424 // the next register is even; counting down is just to make the compiler happy...
Andreas Gampe575e78c2014-11-03 23:41:03 -08001425 static_assert(kNumNativeGprArgs % 2 == 0U, "Number of native GPR arguments not even");
1426 static_assert(kNumNativeFprArgs % 2 == 0U, "Number of native FPR arguments not even");
Andreas Gampec147b002014-03-06 18:11:06 -08001427 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001428
Andreas Gampec200a4a2014-06-16 18:39:09 -07001429 virtual ~BuildNativeCallFrameStateMachine() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001430
Ian Rogers1428dce2014-10-21 15:02:15 -07001431 bool HavePointerGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001432 return gpr_index_ > 0;
1433 }
1434
Andreas Gampec200a4a2014-06-16 18:39:09 -07001435 void AdvancePointer(const void* val) {
Andreas Gampec147b002014-03-06 18:11:06 -08001436 if (HavePointerGpr()) {
1437 gpr_index_--;
1438 PushGpr(reinterpret_cast<uintptr_t>(val));
1439 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001440 stack_entries_++; // TODO: have a field for pointer length as multiple of 32b
Andreas Gampec147b002014-03-06 18:11:06 -08001441 PushStack(reinterpret_cast<uintptr_t>(val));
1442 gpr_index_ = 0;
1443 }
1444 }
1445
Ian Rogers1428dce2014-10-21 15:02:15 -07001446 bool HaveHandleScopeGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001447 return gpr_index_ > 0;
1448 }
1449
Mathieu Chartier90443472015-07-16 20:32:27 -07001450 void AdvanceHandleScope(mirror::Object* ptr) SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001451 uintptr_t handle = PushHandle(ptr);
1452 if (HaveHandleScopeGpr()) {
Andreas Gampec147b002014-03-06 18:11:06 -08001453 gpr_index_--;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001454 PushGpr(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001455 } else {
1456 stack_entries_++;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001457 PushStack(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001458 gpr_index_ = 0;
1459 }
1460 }
1461
Ian Rogers1428dce2014-10-21 15:02:15 -07001462 bool HaveIntGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001463 return gpr_index_ > 0;
1464 }
1465
1466 void AdvanceInt(uint32_t val) {
1467 if (HaveIntGpr()) {
1468 gpr_index_--;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001469 if (kMultiGPRegistersWidened) {
1470 DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001471 PushGpr(static_cast<int64_t>(bit_cast<int32_t, uint32_t>(val)));
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001472 } else {
1473 PushGpr(val);
1474 }
Andreas Gampec147b002014-03-06 18:11:06 -08001475 } else {
1476 stack_entries_++;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001477 if (kMultiGPRegistersWidened) {
1478 DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001479 PushStack(static_cast<int64_t>(bit_cast<int32_t, uint32_t>(val)));
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001480 } else {
1481 PushStack(val);
1482 }
Andreas Gampec147b002014-03-06 18:11:06 -08001483 gpr_index_ = 0;
1484 }
1485 }
1486
Ian Rogers1428dce2014-10-21 15:02:15 -07001487 bool HaveLongGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001488 return gpr_index_ >= kRegistersNeededForLong + (LongGprNeedsPadding() ? 1 : 0);
1489 }
1490
Ian Rogers1428dce2014-10-21 15:02:15 -07001491 bool LongGprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001492 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1493 kAlignLongOnStack && // and when it needs alignment
1494 (gpr_index_ & 1) == 1; // counter is odd, see constructor
1495 }
1496
Ian Rogers1428dce2014-10-21 15:02:15 -07001497 bool LongStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001498 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1499 kAlignLongOnStack && // and when it needs 8B alignment
1500 (stack_entries_ & 1) == 1; // counter is odd
1501 }
1502
1503 void AdvanceLong(uint64_t val) {
1504 if (HaveLongGpr()) {
1505 if (LongGprNeedsPadding()) {
1506 PushGpr(0);
1507 gpr_index_--;
1508 }
1509 if (kRegistersNeededForLong == 1) {
1510 PushGpr(static_cast<uintptr_t>(val));
1511 } else {
1512 PushGpr(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1513 PushGpr(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1514 }
1515 gpr_index_ -= kRegistersNeededForLong;
1516 } else {
1517 if (LongStackNeedsPadding()) {
1518 PushStack(0);
1519 stack_entries_++;
1520 }
1521 if (kRegistersNeededForLong == 1) {
1522 PushStack(static_cast<uintptr_t>(val));
1523 stack_entries_++;
1524 } else {
1525 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1526 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1527 stack_entries_ += 2;
1528 }
1529 gpr_index_ = 0;
1530 }
1531 }
1532
Ian Rogers1428dce2014-10-21 15:02:15 -07001533 bool HaveFloatFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001534 return fpr_index_ > 0;
1535 }
1536
Andreas Gampec147b002014-03-06 18:11:06 -08001537 void AdvanceFloat(float val) {
1538 if (kNativeSoftFloatAbi) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001539 AdvanceInt(bit_cast<uint32_t, float>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001540 } else {
1541 if (HaveFloatFpr()) {
1542 fpr_index_--;
1543 if (kRegistersNeededForDouble == 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001544 if (kMultiFPRegistersWidened) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001545 PushFpr8(bit_cast<uint64_t, double>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001546 } else {
1547 // No widening, just use the bits.
Roland Levillainda4d79b2015-03-24 14:36:11 +00001548 PushFpr8(static_cast<uint64_t>(bit_cast<uint32_t, float>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001549 }
1550 } else {
1551 PushFpr4(val);
1552 }
1553 } else {
1554 stack_entries_++;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001555 if (kRegistersNeededForDouble == 1 && kMultiFPRegistersWidened) {
Andreas Gampec147b002014-03-06 18:11:06 -08001556 // Need to widen before storing: Note the "double" in the template instantiation.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001557 // Note: We need to jump through those hoops to make the compiler happy.
1558 DCHECK_EQ(sizeof(uintptr_t), sizeof(uint64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001559 PushStack(static_cast<uintptr_t>(bit_cast<uint64_t, double>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001560 } else {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001561 PushStack(static_cast<uintptr_t>(bit_cast<uint32_t, float>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001562 }
1563 fpr_index_ = 0;
1564 }
1565 }
1566 }
1567
Ian Rogers1428dce2014-10-21 15:02:15 -07001568 bool HaveDoubleFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001569 return fpr_index_ >= kRegistersNeededForDouble + (DoubleFprNeedsPadding() ? 1 : 0);
1570 }
1571
Ian Rogers1428dce2014-10-21 15:02:15 -07001572 bool DoubleFprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001573 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1574 kAlignDoubleOnStack && // and when it needs alignment
1575 (fpr_index_ & 1) == 1; // counter is odd, see constructor
1576 }
1577
Ian Rogers1428dce2014-10-21 15:02:15 -07001578 bool DoubleStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001579 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1580 kAlignDoubleOnStack && // and when it needs 8B alignment
1581 (stack_entries_ & 1) == 1; // counter is odd
1582 }
1583
1584 void AdvanceDouble(uint64_t val) {
1585 if (kNativeSoftFloatAbi) {
1586 AdvanceLong(val);
1587 } else {
1588 if (HaveDoubleFpr()) {
1589 if (DoubleFprNeedsPadding()) {
1590 PushFpr4(0);
1591 fpr_index_--;
1592 }
1593 PushFpr8(val);
1594 fpr_index_ -= kRegistersNeededForDouble;
1595 } else {
1596 if (DoubleStackNeedsPadding()) {
1597 PushStack(0);
1598 stack_entries_++;
1599 }
1600 if (kRegistersNeededForDouble == 1) {
1601 PushStack(static_cast<uintptr_t>(val));
1602 stack_entries_++;
1603 } else {
1604 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1605 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1606 stack_entries_ += 2;
1607 }
1608 fpr_index_ = 0;
1609 }
1610 }
1611 }
1612
Ian Rogers1428dce2014-10-21 15:02:15 -07001613 uint32_t GetStackEntries() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001614 return stack_entries_;
1615 }
1616
Ian Rogers1428dce2014-10-21 15:02:15 -07001617 uint32_t GetNumberOfUsedGprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001618 return kNumNativeGprArgs - gpr_index_;
1619 }
1620
Ian Rogers1428dce2014-10-21 15:02:15 -07001621 uint32_t GetNumberOfUsedFprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001622 return kNumNativeFprArgs - fpr_index_;
1623 }
1624
1625 private:
1626 void PushGpr(uintptr_t val) {
1627 delegate_->PushGpr(val);
1628 }
1629 void PushFpr4(float val) {
1630 delegate_->PushFpr4(val);
1631 }
1632 void PushFpr8(uint64_t val) {
1633 delegate_->PushFpr8(val);
1634 }
1635 void PushStack(uintptr_t val) {
1636 delegate_->PushStack(val);
1637 }
Mathieu Chartier90443472015-07-16 20:32:27 -07001638 uintptr_t PushHandle(mirror::Object* ref) SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001639 return delegate_->PushHandle(ref);
Andreas Gampec147b002014-03-06 18:11:06 -08001640 }
1641
1642 uint32_t gpr_index_; // Number of free GPRs
1643 uint32_t fpr_index_; // Number of free FPRs
1644 uint32_t stack_entries_; // Stack entries are in multiples of 32b, as floats are usually not
1645 // extended
Ian Rogers1428dce2014-10-21 15:02:15 -07001646 T* const delegate_; // What Push implementation gets called
Andreas Gampec147b002014-03-06 18:11:06 -08001647};
1648
Andreas Gampec200a4a2014-06-16 18:39:09 -07001649// Computes the sizes of register stacks and call stack area. Handling of references can be extended
1650// in subclasses.
1651//
1652// To handle native pointers, use "L" in the shorty for an object reference, which simulates
1653// them with handles.
1654class ComputeNativeCallFrameSize {
Andreas Gampec147b002014-03-06 18:11:06 -08001655 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001656 ComputeNativeCallFrameSize() : num_stack_entries_(0) {}
1657
1658 virtual ~ComputeNativeCallFrameSize() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001659
Ian Rogers1428dce2014-10-21 15:02:15 -07001660 uint32_t GetStackSize() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001661 return num_stack_entries_ * sizeof(uintptr_t);
1662 }
1663
Ian Rogers1428dce2014-10-21 15:02:15 -07001664 uint8_t* LayoutCallStack(uint8_t* sp8) const {
Andreas Gampec147b002014-03-06 18:11:06 -08001665 sp8 -= GetStackSize();
Andreas Gampe779f8c92014-06-09 18:29:38 -07001666 // Align by kStackAlignment.
1667 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
Andreas Gampec200a4a2014-06-16 18:39:09 -07001668 return sp8;
Andreas Gampec147b002014-03-06 18:11:06 -08001669 }
1670
Ian Rogers1428dce2014-10-21 15:02:15 -07001671 uint8_t* LayoutCallRegisterStacks(uint8_t* sp8, uintptr_t** start_gpr, uint32_t** start_fpr)
1672 const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001673 // Assumption is OK right now, as we have soft-float arm
1674 size_t fregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeFprArgs;
1675 sp8 -= fregs * sizeof(uintptr_t);
1676 *start_fpr = reinterpret_cast<uint32_t*>(sp8);
1677 size_t iregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeGprArgs;
1678 sp8 -= iregs * sizeof(uintptr_t);
1679 *start_gpr = reinterpret_cast<uintptr_t*>(sp8);
1680 return sp8;
1681 }
Andreas Gampec147b002014-03-06 18:11:06 -08001682
Andreas Gampec200a4a2014-06-16 18:39:09 -07001683 uint8_t* LayoutNativeCall(uint8_t* sp8, uintptr_t** start_stack, uintptr_t** start_gpr,
Ian Rogers1428dce2014-10-21 15:02:15 -07001684 uint32_t** start_fpr) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001685 // Native call stack.
1686 sp8 = LayoutCallStack(sp8);
1687 *start_stack = reinterpret_cast<uintptr_t*>(sp8);
Andreas Gampec147b002014-03-06 18:11:06 -08001688
Andreas Gampec200a4a2014-06-16 18:39:09 -07001689 // Put fprs and gprs below.
1690 sp8 = LayoutCallRegisterStacks(sp8, start_gpr, start_fpr);
Andreas Gampec147b002014-03-06 18:11:06 -08001691
Andreas Gampec200a4a2014-06-16 18:39:09 -07001692 // Return the new bottom.
1693 return sp8;
1694 }
1695
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001696 virtual void WalkHeader(
1697 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm ATTRIBUTE_UNUSED)
Mathieu Chartier90443472015-07-16 20:32:27 -07001698 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001699 }
Andreas Gampec200a4a2014-06-16 18:39:09 -07001700
Mathieu Chartier90443472015-07-16 20:32:27 -07001701 void Walk(const char* shorty, uint32_t shorty_len) SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001702 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize> sm(this);
1703
1704 WalkHeader(&sm);
Andreas Gampec147b002014-03-06 18:11:06 -08001705
1706 for (uint32_t i = 1; i < shorty_len; ++i) {
1707 Primitive::Type cur_type_ = Primitive::GetType(shorty[i]);
1708 switch (cur_type_) {
1709 case Primitive::kPrimNot:
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001710 // TODO: fix abuse of mirror types.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001711 sm.AdvanceHandleScope(
1712 reinterpret_cast<mirror::Object*>(0x12345678));
Andreas Gampec147b002014-03-06 18:11:06 -08001713 break;
1714
1715 case Primitive::kPrimBoolean:
1716 case Primitive::kPrimByte:
1717 case Primitive::kPrimChar:
1718 case Primitive::kPrimShort:
1719 case Primitive::kPrimInt:
1720 sm.AdvanceInt(0);
1721 break;
1722 case Primitive::kPrimFloat:
1723 sm.AdvanceFloat(0);
1724 break;
1725 case Primitive::kPrimDouble:
1726 sm.AdvanceDouble(0);
1727 break;
1728 case Primitive::kPrimLong:
1729 sm.AdvanceLong(0);
1730 break;
1731 default:
1732 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001733 UNREACHABLE();
Andreas Gampec147b002014-03-06 18:11:06 -08001734 }
1735 }
1736
Ian Rogers1428dce2014-10-21 15:02:15 -07001737 num_stack_entries_ = sm.GetStackEntries();
Andreas Gampec147b002014-03-06 18:11:06 -08001738 }
1739
1740 void PushGpr(uintptr_t /* val */) {
1741 // not optimizing registers, yet
1742 }
1743
1744 void PushFpr4(float /* val */) {
1745 // not optimizing registers, yet
1746 }
1747
1748 void PushFpr8(uint64_t /* val */) {
1749 // not optimizing registers, yet
1750 }
1751
1752 void PushStack(uintptr_t /* val */) {
1753 // counting is already done in the superclass
1754 }
1755
Andreas Gampec200a4a2014-06-16 18:39:09 -07001756 virtual uintptr_t PushHandle(mirror::Object* /* ptr */) {
Andreas Gampec147b002014-03-06 18:11:06 -08001757 return reinterpret_cast<uintptr_t>(nullptr);
1758 }
1759
Andreas Gampec200a4a2014-06-16 18:39:09 -07001760 protected:
Andreas Gampec147b002014-03-06 18:11:06 -08001761 uint32_t num_stack_entries_;
1762};
1763
Andreas Gampec200a4a2014-06-16 18:39:09 -07001764class ComputeGenericJniFrameSize FINAL : public ComputeNativeCallFrameSize {
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001765 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001766 ComputeGenericJniFrameSize() : num_handle_scope_references_(0) {}
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001767
Andreas Gampec200a4a2014-06-16 18:39:09 -07001768 // Lays out the callee-save frame. Assumes that the incorrect frame corresponding to RefsAndArgs
1769 // is at *m = sp. Will update to point to the bottom of the save frame.
1770 //
1771 // Note: assumes ComputeAll() has been run before.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001772 void LayoutCalleeSaveFrame(Thread* self, ArtMethod*** m, void* sp, HandleScope** handle_scope)
Mathieu Chartier90443472015-07-16 20:32:27 -07001773 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001774 ArtMethod* method = **m;
1775
1776 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), sizeof(void*));
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001777
Andreas Gampec200a4a2014-06-16 18:39:09 -07001778 uint8_t* sp8 = reinterpret_cast<uint8_t*>(sp);
1779
1780 // First, fix up the layout of the callee-save frame.
1781 // We have to squeeze in the HandleScope, and relocate the method pointer.
1782
1783 // "Free" the slot for the method.
Ian Rogers13735952014-10-08 12:43:28 -07001784 sp8 += sizeof(void*); // In the callee-save frame we use a full pointer.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001785
1786 // Under the callee saves put handle scope and new method stack reference.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001787 size_t handle_scope_size = HandleScope::SizeOf(num_handle_scope_references_);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001788 size_t scope_and_method = handle_scope_size + sizeof(ArtMethod*);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001789
1790 sp8 -= scope_and_method;
1791 // Align by kStackAlignment.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001792 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
Andreas Gampec200a4a2014-06-16 18:39:09 -07001793
Mathieu Chartiere401d142015-04-22 13:56:20 -07001794 uint8_t* sp8_table = sp8 + sizeof(ArtMethod*);
Ian Rogers59c07062014-10-10 13:03:39 -07001795 *handle_scope = HandleScope::Create(sp8_table, self->GetTopHandleScope(),
1796 num_handle_scope_references_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001797
1798 // Add a slot for the method pointer, and fill it. Fix the pointer-pointer given to us.
1799 uint8_t* method_pointer = sp8;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001800 auto** new_method_ref = reinterpret_cast<ArtMethod**>(method_pointer);
1801 *new_method_ref = method;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001802 *m = new_method_ref;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001803 }
1804
Andreas Gampec200a4a2014-06-16 18:39:09 -07001805 // Adds space for the cookie. Note: may leave stack unaligned.
Ian Rogers1428dce2014-10-21 15:02:15 -07001806 void LayoutCookie(uint8_t** sp) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001807 // Reference cookie and padding
1808 *sp -= 8;
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001809 }
1810
Andreas Gampec200a4a2014-06-16 18:39:09 -07001811 // Re-layout the callee-save frame (insert a handle-scope). Then add space for the cookie.
1812 // Returns the new bottom. Note: this may be unaligned.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001813 uint8_t* LayoutJNISaveFrame(Thread* self, ArtMethod*** m, void* sp, HandleScope** handle_scope)
Mathieu Chartier90443472015-07-16 20:32:27 -07001814 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001815 // First, fix up the layout of the callee-save frame.
1816 // We have to squeeze in the HandleScope, and relocate the method pointer.
Ian Rogers59c07062014-10-10 13:03:39 -07001817 LayoutCalleeSaveFrame(self, m, sp, handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001818
1819 // The bottom of the callee-save frame is now where the method is, *m.
1820 uint8_t* sp8 = reinterpret_cast<uint8_t*>(*m);
1821
1822 // Add space for cookie.
1823 LayoutCookie(&sp8);
1824
1825 return sp8;
1826 }
1827
1828 // WARNING: After this, *sp won't be pointing to the method anymore!
Mathieu Chartiere401d142015-04-22 13:56:20 -07001829 uint8_t* ComputeLayout(Thread* self, ArtMethod*** m, const char* shorty, uint32_t shorty_len,
1830 HandleScope** handle_scope, uintptr_t** start_stack, uintptr_t** start_gpr,
1831 uint32_t** start_fpr)
Mathieu Chartier90443472015-07-16 20:32:27 -07001832 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001833 Walk(shorty, shorty_len);
1834
1835 // JNI part.
Ian Rogers59c07062014-10-10 13:03:39 -07001836 uint8_t* sp8 = LayoutJNISaveFrame(self, m, reinterpret_cast<void*>(*m), handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001837
1838 sp8 = LayoutNativeCall(sp8, start_stack, start_gpr, start_fpr);
1839
1840 // Return the new bottom.
1841 return sp8;
1842 }
1843
1844 uintptr_t PushHandle(mirror::Object* /* ptr */) OVERRIDE;
1845
1846 // Add JNIEnv* and jobj/jclass before the shorty-derived elements.
1847 void WalkHeader(BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) OVERRIDE
Mathieu Chartier90443472015-07-16 20:32:27 -07001848 SHARED_REQUIRES(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001849
1850 private:
1851 uint32_t num_handle_scope_references_;
1852};
1853
1854uintptr_t ComputeGenericJniFrameSize::PushHandle(mirror::Object* /* ptr */) {
1855 num_handle_scope_references_++;
1856 return reinterpret_cast<uintptr_t>(nullptr);
1857}
1858
1859void ComputeGenericJniFrameSize::WalkHeader(
1860 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) {
1861 // JNIEnv
1862 sm->AdvancePointer(nullptr);
1863
1864 // Class object or this as first argument
1865 sm->AdvanceHandleScope(reinterpret_cast<mirror::Object*>(0x12345678));
1866}
1867
1868// Class to push values to three separate regions. Used to fill the native call part. Adheres to
1869// the template requirements of BuildGenericJniFrameStateMachine.
1870class FillNativeCall {
1871 public:
1872 FillNativeCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) :
1873 cur_gpr_reg_(gpr_regs), cur_fpr_reg_(fpr_regs), cur_stack_arg_(stack_args) {}
1874
1875 virtual ~FillNativeCall() {}
1876
1877 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) {
1878 cur_gpr_reg_ = gpr_regs;
1879 cur_fpr_reg_ = fpr_regs;
1880 cur_stack_arg_ = stack_args;
Andreas Gampec147b002014-03-06 18:11:06 -08001881 }
1882
1883 void PushGpr(uintptr_t val) {
1884 *cur_gpr_reg_ = val;
1885 cur_gpr_reg_++;
1886 }
1887
1888 void PushFpr4(float val) {
1889 *cur_fpr_reg_ = val;
1890 cur_fpr_reg_++;
1891 }
1892
1893 void PushFpr8(uint64_t val) {
1894 uint64_t* tmp = reinterpret_cast<uint64_t*>(cur_fpr_reg_);
1895 *tmp = val;
1896 cur_fpr_reg_ += 2;
1897 }
1898
1899 void PushStack(uintptr_t val) {
1900 *cur_stack_arg_ = val;
1901 cur_stack_arg_++;
1902 }
1903
Mathieu Chartier90443472015-07-16 20:32:27 -07001904 virtual uintptr_t PushHandle(mirror::Object*) SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001905 LOG(FATAL) << "(Non-JNI) Native call does not use handles.";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001906 UNREACHABLE();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001907 }
1908
1909 private:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001910 uintptr_t* cur_gpr_reg_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001911 uint32_t* cur_fpr_reg_;
1912 uintptr_t* cur_stack_arg_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001913};
Andreas Gampec147b002014-03-06 18:11:06 -08001914
Andreas Gampec200a4a2014-06-16 18:39:09 -07001915// Visits arguments on the stack placing them into a region lower down the stack for the benefit
1916// of transitioning into native code.
1917class BuildGenericJniFrameVisitor FINAL : public QuickArgumentVisitor {
1918 public:
Ian Rogers59c07062014-10-10 13:03:39 -07001919 BuildGenericJniFrameVisitor(Thread* self, bool is_static, const char* shorty, uint32_t shorty_len,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001920 ArtMethod*** sp)
Andreas Gampec200a4a2014-06-16 18:39:09 -07001921 : QuickArgumentVisitor(*sp, is_static, shorty, shorty_len),
1922 jni_call_(nullptr, nullptr, nullptr, nullptr), sm_(&jni_call_) {
1923 ComputeGenericJniFrameSize fsc;
1924 uintptr_t* start_gpr_reg;
1925 uint32_t* start_fpr_reg;
1926 uintptr_t* start_stack_arg;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001927 bottom_of_used_area_ = fsc.ComputeLayout(self, sp, shorty, shorty_len,
Ian Rogers59c07062014-10-10 13:03:39 -07001928 &handle_scope_,
1929 &start_stack_arg,
Andreas Gampec200a4a2014-06-16 18:39:09 -07001930 &start_gpr_reg, &start_fpr_reg);
1931
Andreas Gampec200a4a2014-06-16 18:39:09 -07001932 jni_call_.Reset(start_gpr_reg, start_fpr_reg, start_stack_arg, handle_scope_);
1933
1934 // jni environment is always first argument
1935 sm_.AdvancePointer(self->GetJniEnv());
1936
1937 if (is_static) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001938 sm_.AdvanceHandleScope((**sp)->GetDeclaringClass());
Andreas Gampec200a4a2014-06-16 18:39:09 -07001939 }
1940 }
1941
Mathieu Chartier90443472015-07-16 20:32:27 -07001942 void Visit() SHARED_REQUIRES(Locks::mutator_lock_) OVERRIDE;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001943
Mathieu Chartier90443472015-07-16 20:32:27 -07001944 void FinalizeHandleScope(Thread* self) SHARED_REQUIRES(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001945
1946 StackReference<mirror::Object>* GetFirstHandleScopeEntry()
Mathieu Chartier90443472015-07-16 20:32:27 -07001947 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001948 return handle_scope_->GetHandle(0).GetReference();
1949 }
1950
Mathieu Chartier90443472015-07-16 20:32:27 -07001951 jobject GetFirstHandleScopeJObject() const SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001952 return handle_scope_->GetHandle(0).ToJObject();
1953 }
1954
Ian Rogers1428dce2014-10-21 15:02:15 -07001955 void* GetBottomOfUsedArea() const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001956 return bottom_of_used_area_;
1957 }
1958
1959 private:
1960 // A class to fill a JNI call. Adds reference/handle-scope management to FillNativeCall.
1961 class FillJniCall FINAL : public FillNativeCall {
1962 public:
1963 FillJniCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args,
1964 HandleScope* handle_scope) : FillNativeCall(gpr_regs, fpr_regs, stack_args),
1965 handle_scope_(handle_scope), cur_entry_(0) {}
1966
Mathieu Chartier90443472015-07-16 20:32:27 -07001967 uintptr_t PushHandle(mirror::Object* ref) OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001968
1969 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args, HandleScope* scope) {
1970 FillNativeCall::Reset(gpr_regs, fpr_regs, stack_args);
1971 handle_scope_ = scope;
1972 cur_entry_ = 0U;
1973 }
1974
Mathieu Chartier90443472015-07-16 20:32:27 -07001975 void ResetRemainingScopeSlots() SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001976 // Initialize padding entries.
1977 size_t expected_slots = handle_scope_->NumberOfReferences();
1978 while (cur_entry_ < expected_slots) {
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07001979 handle_scope_->GetMutableHandle(cur_entry_++).Assign(nullptr);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001980 }
1981 DCHECK_NE(cur_entry_, 0U);
1982 }
1983
1984 private:
1985 HandleScope* handle_scope_;
1986 size_t cur_entry_;
1987 };
1988
1989 HandleScope* handle_scope_;
1990 FillJniCall jni_call_;
1991 void* bottom_of_used_area_;
1992
1993 BuildNativeCallFrameStateMachine<FillJniCall> sm_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001994
1995 DISALLOW_COPY_AND_ASSIGN(BuildGenericJniFrameVisitor);
1996};
1997
Andreas Gampec200a4a2014-06-16 18:39:09 -07001998uintptr_t BuildGenericJniFrameVisitor::FillJniCall::PushHandle(mirror::Object* ref) {
1999 uintptr_t tmp;
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07002000 MutableHandle<mirror::Object> h = handle_scope_->GetMutableHandle(cur_entry_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002001 h.Assign(ref);
2002 tmp = reinterpret_cast<uintptr_t>(h.ToJObject());
2003 cur_entry_++;
2004 return tmp;
2005}
2006
Ian Rogers9758f792014-03-13 09:02:55 -07002007void BuildGenericJniFrameVisitor::Visit() {
2008 Primitive::Type type = GetParamPrimitiveType();
2009 switch (type) {
2010 case Primitive::kPrimLong: {
2011 jlong long_arg;
2012 if (IsSplitLongOrDouble()) {
2013 long_arg = ReadSplitLongParam();
2014 } else {
2015 long_arg = *reinterpret_cast<jlong*>(GetParamAddress());
2016 }
2017 sm_.AdvanceLong(long_arg);
2018 break;
2019 }
2020 case Primitive::kPrimDouble: {
2021 uint64_t double_arg;
2022 if (IsSplitLongOrDouble()) {
2023 // Read into union so that we don't case to a double.
2024 double_arg = ReadSplitLongParam();
2025 } else {
2026 double_arg = *reinterpret_cast<uint64_t*>(GetParamAddress());
2027 }
2028 sm_.AdvanceDouble(double_arg);
2029 break;
2030 }
2031 case Primitive::kPrimNot: {
2032 StackReference<mirror::Object>* stack_ref =
2033 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002034 sm_.AdvanceHandleScope(stack_ref->AsMirrorPtr());
Ian Rogers9758f792014-03-13 09:02:55 -07002035 break;
2036 }
2037 case Primitive::kPrimFloat:
2038 sm_.AdvanceFloat(*reinterpret_cast<float*>(GetParamAddress()));
2039 break;
2040 case Primitive::kPrimBoolean: // Fall-through.
2041 case Primitive::kPrimByte: // Fall-through.
2042 case Primitive::kPrimChar: // Fall-through.
2043 case Primitive::kPrimShort: // Fall-through.
2044 case Primitive::kPrimInt: // Fall-through.
2045 sm_.AdvanceInt(*reinterpret_cast<jint*>(GetParamAddress()));
2046 break;
2047 case Primitive::kPrimVoid:
2048 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -07002049 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -07002050 }
2051}
2052
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002053void BuildGenericJniFrameVisitor::FinalizeHandleScope(Thread* self) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002054 // Clear out rest of the scope.
2055 jni_call_.ResetRemainingScopeSlots();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002056 // Install HandleScope.
2057 self->PushHandleScope(handle_scope_);
Ian Rogers9758f792014-03-13 09:02:55 -07002058}
2059
Ian Rogers04c31d22014-07-07 21:44:06 -07002060#if defined(__arm__) || defined(__aarch64__)
Andreas Gampe90546832014-03-12 18:07:19 -07002061extern "C" void* artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07002062#else
2063extern "C" void* artFindNativeMethod(Thread* self);
2064#endif
Andreas Gampe90546832014-03-12 18:07:19 -07002065
Andreas Gampead615172014-04-04 16:20:13 -07002066uint64_t artQuickGenericJniEndJNIRef(Thread* self, uint32_t cookie, jobject l, jobject lock) {
2067 if (lock != nullptr) {
2068 return reinterpret_cast<uint64_t>(JniMethodEndWithReferenceSynchronized(l, cookie, lock, self));
2069 } else {
2070 return reinterpret_cast<uint64_t>(JniMethodEndWithReference(l, cookie, self));
2071 }
2072}
2073
2074void artQuickGenericJniEndJNINonRef(Thread* self, uint32_t cookie, jobject lock) {
2075 if (lock != nullptr) {
2076 JniMethodEndSynchronized(cookie, lock, self);
2077 } else {
2078 JniMethodEnd(cookie, self);
2079 }
2080}
2081
Andreas Gampec147b002014-03-06 18:11:06 -08002082/*
2083 * Initializes an alloca region assumed to be directly below sp for a native call:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002084 * 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 -08002085 * The final element on the stack is a pointer to the native code.
2086 *
Andreas Gampe36fea8d2014-03-10 13:37:40 -07002087 * On entry, the stack has a standard callee-save frame above sp, and an alloca below it.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002088 * We need to fix this, as the handle scope needs to go into the callee-save frame.
Andreas Gampe36fea8d2014-03-10 13:37:40 -07002089 *
Andreas Gampec147b002014-03-06 18:11:06 -08002090 * The return of this function denotes:
2091 * 1) How many bytes of the alloca can be released, if the value is non-negative.
2092 * 2) An error, if the value is negative.
2093 */
Mathieu Chartiere401d142015-04-22 13:56:20 -07002094extern "C" TwoWordReturn artQuickGenericJniTrampoline(Thread* self, ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -07002095 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002096 ArtMethod* called = *sp;
Andreas Gampe36fea8d2014-03-10 13:37:40 -07002097 DCHECK(called->IsNative()) << PrettyMethod(called, true);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002098 uint32_t shorty_len = 0;
2099 const char* shorty = called->GetShorty(&shorty_len);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002100
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002101 // Run the visitor and update sp.
Ian Rogers59c07062014-10-10 13:03:39 -07002102 BuildGenericJniFrameVisitor visitor(self, called->IsStatic(), shorty, shorty_len, &sp);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002103 visitor.VisitArguments();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002104 visitor.FinalizeHandleScope(self);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002105
Andreas Gampec200a4a2014-06-16 18:39:09 -07002106 // Fix up managed-stack things in Thread.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002107 self->SetTopOfStack(sp);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002108
Ian Rogerse0dcd462014-03-08 15:21:04 -08002109 self->VerifyStack();
2110
Andreas Gampe90546832014-03-12 18:07:19 -07002111 // Start JNI, save the cookie.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002112 uint32_t cookie;
2113 if (called->IsSynchronized()) {
Mathieu Chartier0cd81352014-05-22 16:48:55 -07002114 cookie = JniMethodStartSynchronized(visitor.GetFirstHandleScopeJObject(), self);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002115 if (self->IsExceptionPending()) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002116 self->PopHandleScope();
Andreas Gampec147b002014-03-06 18:11:06 -08002117 // A negative value denotes an error.
Andreas Gampec200a4a2014-06-16 18:39:09 -07002118 return GetTwoWordFailureValue();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002119 }
2120 } else {
2121 cookie = JniMethodStart(self);
2122 }
Andreas Gampe36fea8d2014-03-10 13:37:40 -07002123 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
Ian Rogerse0dcd462014-03-08 15:21:04 -08002124 *(sp32 - 1) = cookie;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002125
Andreas Gampe90546832014-03-12 18:07:19 -07002126 // Retrieve the stored native code.
Mathieu Chartier2d721012014-11-10 11:08:06 -08002127 void* nativeCode = called->GetEntryPointFromJni();
Andreas Gampe90546832014-03-12 18:07:19 -07002128
Andreas Gampe9a6a99a2014-03-14 07:52:20 -07002129 // There are two cases for the content of nativeCode:
2130 // 1) Pointer to the native function.
2131 // 2) Pointer to the trampoline for native code binding.
2132 // In the second case, we need to execute the binding and continue with the actual native function
2133 // pointer.
Andreas Gampe90546832014-03-12 18:07:19 -07002134 DCHECK(nativeCode != nullptr);
2135 if (nativeCode == GetJniDlsymLookupStub()) {
Ian Rogers04c31d22014-07-07 21:44:06 -07002136#if defined(__arm__) || defined(__aarch64__)
Andreas Gampe90546832014-03-12 18:07:19 -07002137 nativeCode = artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07002138#else
2139 nativeCode = artFindNativeMethod(self);
2140#endif
Andreas Gampe90546832014-03-12 18:07:19 -07002141
2142 if (nativeCode == nullptr) {
2143 DCHECK(self->IsExceptionPending()); // There should be an exception pending now.
Andreas Gampead615172014-04-04 16:20:13 -07002144
2145 // End JNI, as the assembly will move to deliver the exception.
Mathieu Chartier0cd81352014-05-22 16:48:55 -07002146 jobject lock = called->IsSynchronized() ? visitor.GetFirstHandleScopeJObject() : nullptr;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002147 if (shorty[0] == 'L') {
Andreas Gampead615172014-04-04 16:20:13 -07002148 artQuickGenericJniEndJNIRef(self, cookie, nullptr, lock);
2149 } else {
2150 artQuickGenericJniEndJNINonRef(self, cookie, lock);
2151 }
2152
Andreas Gampec200a4a2014-06-16 18:39:09 -07002153 return GetTwoWordFailureValue();
Andreas Gampe90546832014-03-12 18:07:19 -07002154 }
2155 // Note that the native code pointer will be automatically set by artFindNativeMethod().
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002156 }
2157
Andreas Gampec200a4a2014-06-16 18:39:09 -07002158 // Return native code addr(lo) and bottom of alloca address(hi).
2159 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(visitor.GetBottomOfUsedArea()),
2160 reinterpret_cast<uintptr_t>(nativeCode));
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002161}
2162
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002163// Defined in quick_jni_entrypoints.cc.
2164extern uint64_t GenericJniMethodEnd(Thread* self, uint32_t saved_local_ref_cookie,
2165 jvalue result, uint64_t result_f, ArtMethod* called,
2166 HandleScope* handle_scope);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002167/*
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002168 * Is called after the native JNI code. Responsible for cleanup (handle scope, saved state) and
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002169 * unlocking.
2170 */
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002171extern "C" uint64_t artQuickGenericJniEndTrampoline(Thread* self,
2172 jvalue result,
2173 uint64_t result_f) {
2174 // We're here just back from a native call. We don't have the shared mutator lock at this point
2175 // yet until we call GoToRunnable() later in GenericJniMethodEnd(). Accessing objects or doing
2176 // anything that requires a mutator lock before that would cause problems as GC may have the
2177 // exclusive mutator lock and may be moving objects, etc.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002178 ArtMethod** sp = self->GetManagedStack()->GetTopQuickFrame();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002179 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002180 ArtMethod* called = *sp;
Ian Rogerse0dcd462014-03-08 15:21:04 -08002181 uint32_t cookie = *(sp32 - 1);
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002182 HandleScope* table = reinterpret_cast<HandleScope*>(reinterpret_cast<uint8_t*>(sp) + sizeof(*sp));
2183 return GenericJniMethodEnd(self, cookie, result, result_f, called, table);
Andreas Gampe2da88232014-02-27 12:26:20 -08002184}
2185
Andreas Gamped58342c2014-06-05 14:18:08 -07002186// We use TwoWordReturn to optimize scalar returns. We use the hi value for code, and the lo value
2187// for the method pointer.
Andreas Gampe51f76352014-05-21 08:28:48 -07002188//
Andreas Gamped58342c2014-06-05 14:18:08 -07002189// 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 -07002190// to hold the mutator lock (see SHARED_REQUIRES(Locks::mutator_lock_) annotations).
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002191
2192template<InvokeType type, bool access_check>
Mathieu Chartiere401d142015-04-22 13:56:20 -07002193static TwoWordReturn artInvokeCommon(uint32_t method_idx, mirror::Object* this_object, Thread* self,
2194 ArtMethod** sp) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002195 ScopedQuickEntrypointChecks sqec(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002196 DCHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs));
2197 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
2198 ArtMethod* method = FindMethodFast(method_idx, this_object, caller_method, access_check, type);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002199 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002200 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()->GetDexFile();
2201 uint32_t shorty_len;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002202 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(method_idx), &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002203 {
2204 // Remember the args in case a GC happens in FindMethodFromCode.
2205 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
2206 RememberForGcArgumentVisitor visitor(sp, type == kStatic, shorty, shorty_len, &soa);
2207 visitor.VisitArguments();
Andreas Gampe3a357142015-08-07 17:20:11 -07002208 method = FindMethodFromCode<type, access_check>(method_idx, &this_object, caller_method,
Mathieu Chartier0cd81352014-05-22 16:48:55 -07002209 self);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002210 visitor.FixupReferences();
2211 }
2212
Ian Rogerse0a02da2014-12-02 14:10:53 -08002213 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002214 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07002215 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002216 }
2217 }
2218 DCHECK(!self->IsExceptionPending());
2219 const void* code = method->GetEntryPointFromQuickCompiledCode();
2220
2221 // When we return, the caller will branch to this address, so it had better not be 0!
Ian Rogerse0a02da2014-12-02 14:10:53 -08002222 DCHECK(code != nullptr) << "Code was null in method: " << PrettyMethod(method)
Andreas Gampec200a4a2014-06-16 18:39:09 -07002223 << " location: "
2224 << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07002225
Andreas Gamped58342c2014-06-05 14:18:08 -07002226 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
2227 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002228}
2229
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01002230// Explicit artInvokeCommon template function declarations to please analysis tool.
2231#define EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(type, access_check) \
Mathieu Chartier90443472015-07-16 20:32:27 -07002232 template SHARED_REQUIRES(Locks::mutator_lock_) \
Mathieu Chartiere401d142015-04-22 13:56:20 -07002233 TwoWordReturn artInvokeCommon<type, access_check>( \
2234 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01002235
2236EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, false);
2237EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, true);
2238EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, false);
2239EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, true);
2240EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, false);
2241EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, true);
2242EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, false);
2243EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, true);
2244EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, false);
2245EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, true);
2246#undef EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL
2247
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002248// See comments in runtime_support_asm.S
Andreas Gampec200a4a2014-06-16 18:39:09 -07002249extern "C" TwoWordReturn artInvokeInterfaceTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002250 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -07002251 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002252 return artInvokeCommon<kInterface, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002253}
2254
Andreas Gampec200a4a2014-06-16 18:39:09 -07002255extern "C" TwoWordReturn artInvokeDirectTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002256 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -07002257 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002258 return artInvokeCommon<kDirect, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002259}
2260
Andreas Gampec200a4a2014-06-16 18:39:09 -07002261extern "C" TwoWordReturn artInvokeStaticTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002262 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -07002263 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002264 return artInvokeCommon<kStatic, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002265}
2266
Andreas Gampec200a4a2014-06-16 18:39:09 -07002267extern "C" TwoWordReturn artInvokeSuperTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002268 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -07002269 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002270 return artInvokeCommon<kSuper, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002271}
2272
Andreas Gampec200a4a2014-06-16 18:39:09 -07002273extern "C" TwoWordReturn artInvokeVirtualTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002274 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -07002275 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002276 return artInvokeCommon<kVirtual, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002277}
2278
2279// Determine target of interface dispatch. This object is known non-null.
Nicolas Geoffray8ea18d02015-05-26 16:29:08 +01002280extern "C" TwoWordReturn artInvokeInterfaceTrampoline(uint32_t dex_method_idx,
Andreas Gampe51f76352014-05-21 08:28:48 -07002281 mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -07002282 Thread* self, ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -07002283 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002284 ScopedQuickEntrypointChecks sqec(self);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002285 // The optimizing compiler currently does not inline methods that have an interface
2286 // invocation. We use the outer method directly to avoid fetching a stack map, which is
2287 // more expensive.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002288 ArtMethod* caller_method = QuickArgumentVisitor::GetOuterMethod(sp);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002289 DCHECK_EQ(caller_method, QuickArgumentVisitor::GetCallingMethod(sp));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002290 ArtMethod* interface_method = caller_method->GetDexCacheResolvedMethod(
2291 dex_method_idx, sizeof(void*));
2292 DCHECK(interface_method != nullptr) << dex_method_idx << " " << PrettyMethod(caller_method);
2293 ArtMethod* method;
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002294 if (LIKELY(interface_method->GetDexMethodIndex() != DexFile::kDexNoIndex)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002295 method = this_object->GetClass()->FindVirtualMethodForInterface(
2296 interface_method, sizeof(void*));
Ian Rogerse0a02da2014-12-02 14:10:53 -08002297 if (UNLIKELY(method == nullptr)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002298 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(
2299 interface_method, this_object, caller_method);
Andreas Gamped58342c2014-06-05 14:18:08 -07002300 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002301 }
2302 } else {
Mathieu Chartier4edd8472015-06-01 10:47:36 -07002303 DCHECK_EQ(interface_method, Runtime::Current()->GetResolutionMethod());
Nicolas Geoffray8ea18d02015-05-26 16:29:08 +01002304 if (kIsDebugBuild) {
2305 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
2306 const DexFile::CodeItem* code = caller_method->GetCodeItem();
2307 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
2308 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
2309 Instruction::Code instr_code = instr->Opcode();
2310 CHECK(instr_code == Instruction::INVOKE_INTERFACE ||
2311 instr_code == Instruction::INVOKE_INTERFACE_RANGE)
2312 << "Unexpected call into interface trampoline: " << instr->DumpString(nullptr);
2313 if (instr_code == Instruction::INVOKE_INTERFACE) {
2314 CHECK_EQ(dex_method_idx, instr->VRegB_35c());
2315 } else {
2316 CHECK_EQ(instr_code, Instruction::INVOKE_INTERFACE_RANGE);
2317 CHECK_EQ(dex_method_idx, instr->VRegB_3rc());
2318 }
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002319 }
2320
Andreas Gampec200a4a2014-06-16 18:39:09 -07002321 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()
2322 ->GetDexFile();
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002323 uint32_t shorty_len;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002324 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(dex_method_idx),
2325 &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002326 {
2327 // Remember the args in case a GC happens in FindMethodFromCode.
2328 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
2329 RememberForGcArgumentVisitor visitor(sp, false, shorty, shorty_len, &soa);
2330 visitor.VisitArguments();
Andreas Gampe3a357142015-08-07 17:20:11 -07002331 method = FindMethodFromCode<kInterface, false>(dex_method_idx, &this_object, caller_method,
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002332 self);
2333 visitor.FixupReferences();
2334 }
2335
2336 if (UNLIKELY(method == nullptr)) {
2337 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07002338 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002339 }
2340 }
2341 const void* code = method->GetEntryPointFromQuickCompiledCode();
2342
2343 // When we return, the caller will branch to this address, so it had better not be 0!
Ian Rogerse0a02da2014-12-02 14:10:53 -08002344 DCHECK(code != nullptr) << "Code was null in method: " << PrettyMethod(method)
Andreas Gampec200a4a2014-06-16 18:39:09 -07002345 << " location: " << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07002346
Andreas Gamped58342c2014-06-05 14:18:08 -07002347 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
2348 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002349}
2350
Ian Rogers848871b2013-08-05 10:56:33 -07002351} // namespace art