blob: c5492f1436c63811bcc4b1ca3ed1e05cf9a84da9 [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"
Ian Rogerse0a02da2014-12-02 14:10:53 -080026#include "method_reference.h"
Ian Rogers848871b2013-08-05 10:56:33 -070027#include "mirror/class-inl.h"
Mathieu Chartier5f3ded42014-04-03 15:25:30 -070028#include "mirror/dex_cache-inl.h"
Mathieu Chartierfc58af42015-04-16 18:00:39 -070029#include "mirror/method.h"
Ian Rogers848871b2013-08-05 10:56:33 -070030#include "mirror/object-inl.h"
31#include "mirror/object_array-inl.h"
Andreas Gampe639bdd12015-06-03 11:22:45 -070032#include "quick_exception_handler.h"
Ian Rogers848871b2013-08-05 10:56:33 -070033#include "runtime.h"
Ian Rogers53b8b092014-03-13 23:45:53 -070034#include "scoped_thread_state_change.h"
Andreas Gampeb3025922015-09-01 14:45:00 -070035#include "stack.h"
Daniel Mihalyieb076692014-08-22 17:33:31 +020036#include "debugger.h"
Ian Rogers848871b2013-08-05 10:56:33 -070037
38namespace art {
39
40// Visits the arguments as saved to the stack by a Runtime::kRefAndArgs callee save frame.
41class QuickArgumentVisitor {
Ian Rogers936b37f2014-02-14 00:52:24 -080042 // Number of bytes for each out register in the caller method's frame.
43 static constexpr size_t kBytesStackArgLocation = 4;
Alexei Zavjalov41c507a2014-05-15 16:02:46 +070044 // Frame size in bytes of a callee-save frame for RefsAndArgs.
45 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize =
46 GetCalleeSaveFrameSize(kRuntimeISA, Runtime::kRefsAndArgs);
Ian Rogers848871b2013-08-05 10:56:33 -070047#if defined(__arm__)
48 // The callee save frame is pointed to by SP.
49 // | argN | |
50 // | ... | |
51 // | arg4 | |
52 // | arg3 spill | | Caller's frame
53 // | arg2 spill | |
54 // | arg1 spill | |
55 // | Method* | ---
56 // | LR |
Zheng Xu5667fdb2014-10-23 18:29:55 +080057 // | ... | 4x6 bytes callee saves
58 // | R3 |
59 // | R2 |
60 // | R1 |
61 // | S15 |
62 // | : |
63 // | S0 |
64 // | | 4x2 bytes padding
Ian Rogers848871b2013-08-05 10:56:33 -070065 // | Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -050066 static constexpr bool kSplitPairAcrossRegisterAndStack = kArm32QuickCodeUseSoftFloat;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +000067 static constexpr bool kAlignPairRegister = !kArm32QuickCodeUseSoftFloat;
Zheng Xu5667fdb2014-10-23 18:29:55 +080068 static constexpr bool kQuickSoftFloatAbi = kArm32QuickCodeUseSoftFloat;
69 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = !kArm32QuickCodeUseSoftFloat;
Goran Jakovljevicff734982015-08-24 12:58:55 +000070 static constexpr bool kQuickSkipOddFpRegisters = false;
Zheng Xu5667fdb2014-10-23 18:29:55 +080071 static constexpr size_t kNumQuickGprArgs = 3;
72 static constexpr size_t kNumQuickFprArgs = kArm32QuickCodeUseSoftFloat ? 0 : 16;
Andreas Gampe1a5c4062015-01-15 12:10:47 -080073 static constexpr bool kGprFprLockstep = false;
Zheng Xub551fdc2014-07-25 11:49:42 +080074 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset =
75 arm::ArmCalleeSaveFpr1Offset(Runtime::kRefsAndArgs); // Offset of first FPR arg.
76 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset =
77 arm::ArmCalleeSaveGpr1Offset(Runtime::kRefsAndArgs); // Offset of first GPR arg.
78 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset =
79 arm::ArmCalleeSaveLrOffset(Runtime::kRefsAndArgs); // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -080080 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +000081 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -080082 }
Stuart Monteithb95a5342014-03-12 13:32:32 +000083#elif defined(__aarch64__)
84 // The callee save frame is pointed to by SP.
85 // | argN | |
86 // | ... | |
87 // | arg4 | |
88 // | arg3 spill | | Caller's frame
89 // | arg2 spill | |
90 // | arg1 spill | |
91 // | Method* | ---
92 // | LR |
Zheng Xub551fdc2014-07-25 11:49:42 +080093 // | X29 |
Stuart Monteithb95a5342014-03-12 13:32:32 +000094 // | : |
Serban Constantinescu9bd88b02015-04-22 16:24:46 +010095 // | X20 |
Stuart Monteithb95a5342014-03-12 13:32:32 +000096 // | X7 |
97 // | : |
98 // | X1 |
Zheng Xub551fdc2014-07-25 11:49:42 +080099 // | D7 |
Stuart Monteithb95a5342014-03-12 13:32:32 +0000100 // | : |
101 // | D0 |
102 // | | padding
103 // | Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500104 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000105 static constexpr bool kAlignPairRegister = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000106 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800107 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000108 static constexpr bool kQuickSkipOddFpRegisters = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000109 static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs.
110 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800111 static constexpr bool kGprFprLockstep = false;
Zheng Xub551fdc2014-07-25 11:49:42 +0800112 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset =
113 arm64::Arm64CalleeSaveFpr1Offset(Runtime::kRefsAndArgs); // Offset of first FPR arg.
114 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset =
115 arm64::Arm64CalleeSaveGpr1Offset(Runtime::kRefsAndArgs); // Offset of first GPR arg.
116 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset =
117 arm64::Arm64CalleeSaveLrOffset(Runtime::kRefsAndArgs); // Offset of return address.
Stuart Monteithb95a5342014-03-12 13:32:32 +0000118 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000119 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Stuart Monteithb95a5342014-03-12 13:32:32 +0000120 }
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800121#elif defined(__mips__) && !defined(__LP64__)
Ian Rogers848871b2013-08-05 10:56:33 -0700122 // The callee save frame is pointed to by SP.
123 // | argN | |
124 // | ... | |
125 // | arg4 | |
126 // | arg3 spill | | Caller's frame
127 // | arg2 spill | |
128 // | arg1 spill | |
129 // | Method* | ---
130 // | RA |
131 // | ... | callee saves
132 // | A3 | arg3
133 // | A2 | arg2
134 // | A1 | arg1
Goran Jakovljevicff734982015-08-24 12:58:55 +0000135 // | F15 |
136 // | F14 | f_arg1
137 // | F13 |
138 // | F12 | f_arg0
139 // | | padding
Ian Rogers848871b2013-08-05 10:56:33 -0700140 // | A0/Method* | <- sp
Goran Jakovljevicff734982015-08-24 12:58:55 +0000141 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
142 static constexpr bool kAlignPairRegister = true;
143 static constexpr bool kQuickSoftFloatAbi = false;
Zheng Xu5667fdb2014-10-23 18:29:55 +0800144 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000145 static constexpr bool kQuickSkipOddFpRegisters = true;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800146 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs.
Goran Jakovljevicff734982015-08-24 12:58:55 +0000147 static constexpr size_t kNumQuickFprArgs = 4; // 2 arguments passed in FPRs. Floats can be passed
148 // only in even numbered registers and each double
149 // occupies two registers.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800150 static constexpr bool kGprFprLockstep = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000151 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 16; // Offset of first FPR arg.
152 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 32; // Offset of first GPR arg.
153 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 76; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800154 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000155 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800156 }
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800157#elif defined(__mips__) && defined(__LP64__)
158 // The callee save frame is pointed to by SP.
159 // | argN | |
160 // | ... | |
161 // | arg4 | |
162 // | arg3 spill | | Caller's frame
163 // | arg2 spill | |
164 // | arg1 spill | |
165 // | Method* | ---
166 // | RA |
167 // | ... | callee saves
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800168 // | A7 | arg7
169 // | A6 | arg6
170 // | A5 | arg5
171 // | A4 | arg4
172 // | A3 | arg3
173 // | A2 | arg2
174 // | A1 | arg1
Goran Jakovljevicff734982015-08-24 12:58:55 +0000175 // | F19 | f_arg7
176 // | F18 | f_arg6
177 // | F17 | f_arg5
178 // | F16 | f_arg4
179 // | F15 | f_arg3
180 // | F14 | f_arg2
181 // | F13 | f_arg1
182 // | F12 | f_arg0
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800183 // | | padding
184 // | A0/Method* | <- sp
185 // NOTE: for Mip64, when A0 is skipped, F0 is also skipped.
Douglas Leungd18e0832015-02-09 15:22:26 -0800186 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800187 static constexpr bool kAlignPairRegister = false;
188 static constexpr bool kQuickSoftFloatAbi = false;
189 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000190 static constexpr bool kQuickSkipOddFpRegisters = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800191 static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs.
192 static constexpr size_t kNumQuickFprArgs = 7; // 7 arguments passed in FPRs.
193 static constexpr bool kGprFprLockstep = true;
194
195 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 24; // Offset of first FPR arg (F1).
196 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 80; // Offset of first GPR arg (A1).
197 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 200; // Offset of return address.
198 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
199 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
200 }
Ian Rogers848871b2013-08-05 10:56:33 -0700201#elif defined(__i386__)
202 // The callee save frame is pointed to by SP.
203 // | argN | |
204 // | ... | |
205 // | arg4 | |
206 // | arg3 spill | | Caller's frame
207 // | arg2 spill | |
208 // | arg1 spill | |
209 // | Method* | ---
210 // | Return |
211 // | EBP,ESI,EDI | callee saves
212 // | EBX | arg3
213 // | EDX | arg2
214 // | ECX | arg1
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000215 // | XMM3 | float arg 4
216 // | XMM2 | float arg 3
217 // | XMM1 | float arg 2
218 // | XMM0 | float arg 1
Ian Rogers848871b2013-08-05 10:56:33 -0700219 // | EAX/Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500220 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000221 static constexpr bool kAlignPairRegister = false;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000222 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800223 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000224 static constexpr bool kQuickSkipOddFpRegisters = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800225 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs.
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000226 static constexpr size_t kNumQuickFprArgs = 4; // 4 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800227 static constexpr bool kGprFprLockstep = false;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000228 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 4; // Offset of first FPR arg.
229 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 4 + 4*8; // Offset of first GPR arg.
230 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 28 + 4*8; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800231 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000232 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800233 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800234#elif defined(__x86_64__)
Ian Rogers936b37f2014-02-14 00:52:24 -0800235 // The callee save frame is pointed to by SP.
236 // | argN | |
237 // | ... | |
238 // | reg. arg spills | | Caller's frame
239 // | Method* | ---
240 // | Return |
241 // | R15 | callee save
242 // | R14 | callee save
243 // | R13 | callee save
244 // | R12 | callee save
245 // | R9 | arg5
246 // | R8 | arg4
247 // | RSI/R6 | arg1
248 // | RBP/R5 | callee save
249 // | RBX/R3 | callee save
250 // | RDX/R2 | arg2
251 // | RCX/R1 | arg3
252 // | XMM7 | float arg 8
253 // | XMM6 | float arg 7
254 // | XMM5 | float arg 6
255 // | XMM4 | float arg 5
256 // | XMM3 | float arg 4
257 // | XMM2 | float arg 3
258 // | XMM1 | float arg 2
259 // | XMM0 | float arg 1
260 // | Padding |
261 // | RDI/Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500262 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000263 static constexpr bool kAlignPairRegister = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800264 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800265 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000266 static constexpr bool kQuickSkipOddFpRegisters = false;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700267 static constexpr size_t kNumQuickGprArgs = 5; // 5 arguments passed in GPRs.
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700268 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800269 static constexpr bool kGprFprLockstep = false;
Ian Rogers936b37f2014-02-14 00:52:24 -0800270 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 16; // Offset of first FPR arg.
Serguei Katkovc3801912014-07-08 17:21:53 +0700271 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 80 + 4*8; // Offset of first GPR arg.
272 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 168 + 4*8; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800273 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
274 switch (gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000275 case 0: return (4 * GetBytesPerGprSpillLocation(kRuntimeISA));
276 case 1: return (1 * GetBytesPerGprSpillLocation(kRuntimeISA));
277 case 2: return (0 * GetBytesPerGprSpillLocation(kRuntimeISA));
278 case 3: return (5 * GetBytesPerGprSpillLocation(kRuntimeISA));
279 case 4: return (6 * GetBytesPerGprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800280 default:
Andreas Gampec200a4a2014-06-16 18:39:09 -0700281 LOG(FATAL) << "Unexpected GPR index: " << gpr_index;
282 return 0;
Ian Rogers936b37f2014-02-14 00:52:24 -0800283 }
284 }
Ian Rogers848871b2013-08-05 10:56:33 -0700285#else
286#error "Unsupported architecture"
Ian Rogers848871b2013-08-05 10:56:33 -0700287#endif
288
Ian Rogers936b37f2014-02-14 00:52:24 -0800289 public:
Sebastien Hertza836bc92014-11-25 16:30:53 +0100290 // Special handling for proxy methods. Proxy methods are instance methods so the
291 // 'this' object is the 1st argument. They also have the same frame layout as the
292 // kRefAndArgs runtime method. Since 'this' is a reference, it is located in the
293 // 1st GPR.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700294 static mirror::Object* GetProxyThisObject(ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -0700295 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700296 CHECK((*sp)->IsProxyMethod());
297 CHECK_EQ(kQuickCalleeSaveFrame_RefAndArgs_FrameSize, (*sp)->GetFrameSizeInBytes());
Sebastien Hertza836bc92014-11-25 16:30:53 +0100298 CHECK_GT(kNumQuickGprArgs, 0u);
299 constexpr uint32_t kThisGprIndex = 0u; // 'this' is in the 1st GPR.
300 size_t this_arg_offset = kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset +
301 GprIndexToGprOffset(kThisGprIndex);
302 uint8_t* this_arg_address = reinterpret_cast<uint8_t*>(sp) + this_arg_offset;
303 return reinterpret_cast<StackReference<mirror::Object>*>(this_arg_address)->AsMirrorPtr();
304 }
305
Mathieu Chartier90443472015-07-16 20:32:27 -0700306 static ArtMethod* GetCallingMethod(ArtMethod** sp) SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700307 DCHECK((*sp)->IsCalleeSaveMethod());
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +0100308 return GetCalleeSaveMethodCaller(sp, Runtime::kRefsAndArgs);
309 }
310
Mathieu Chartier90443472015-07-16 20:32:27 -0700311 static ArtMethod* GetOuterMethod(ArtMethod** sp) SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700312 DCHECK((*sp)->IsCalleeSaveMethod());
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100313 uint8_t* previous_sp =
314 reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700315 return *reinterpret_cast<ArtMethod**>(previous_sp);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100316 }
317
Mathieu Chartier90443472015-07-16 20:32:27 -0700318 static uint32_t GetCallingDexPc(ArtMethod** sp) SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700319 DCHECK((*sp)->IsCalleeSaveMethod());
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100320 const size_t callee_frame_size = GetCalleeSaveFrameSize(kRuntimeISA, Runtime::kRefsAndArgs);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700321 ArtMethod** caller_sp = reinterpret_cast<ArtMethod**>(
322 reinterpret_cast<uintptr_t>(sp) + callee_frame_size);
323 ArtMethod* outer_method = *caller_sp;
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100324 uintptr_t outer_pc = QuickArgumentVisitor::GetCallingPc(sp);
325 uintptr_t outer_pc_offset = outer_method->NativeQuickPcOffset(outer_pc);
326
327 if (outer_method->IsOptimized(sizeof(void*))) {
328 CodeInfo code_info = outer_method->GetOptimizedCodeInfo();
David Brazdilf677ebf2015-05-29 16:29:43 +0100329 StackMapEncoding encoding = code_info.ExtractEncoding();
330 StackMap stack_map = code_info.GetStackMapForNativePcOffset(outer_pc_offset, encoding);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100331 DCHECK(stack_map.IsValid());
David Brazdilf677ebf2015-05-29 16:29:43 +0100332 if (stack_map.HasInlineInfo(encoding)) {
333 InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map, encoding);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100334 return inline_info.GetDexPcAtDepth(inline_info.GetDepth() - 1);
335 } else {
David Brazdilf677ebf2015-05-29 16:29:43 +0100336 return stack_map.GetDexPc(encoding);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100337 }
338 } else {
339 return outer_method->ToDexPc(outer_pc);
340 }
Ian Rogers848871b2013-08-05 10:56:33 -0700341 }
342
Ian Rogers936b37f2014-02-14 00:52:24 -0800343 // For the given quick ref and args quick frame, return the caller's PC.
Mathieu Chartier90443472015-07-16 20:32:27 -0700344 static uintptr_t GetCallingPc(ArtMethod** sp) SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700345 DCHECK((*sp)->IsCalleeSaveMethod());
Ian Rogers13735952014-10-08 12:43:28 -0700346 uint8_t* lr = reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_LrOffset;
Ian Rogers848871b2013-08-05 10:56:33 -0700347 return *reinterpret_cast<uintptr_t*>(lr);
348 }
349
Mathieu Chartiere401d142015-04-22 13:56:20 -0700350 QuickArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty,
Mathieu Chartier90443472015-07-16 20:32:27 -0700351 uint32_t shorty_len) SHARED_REQUIRES(Locks::mutator_lock_) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700352 is_static_(is_static), shorty_(shorty), shorty_len_(shorty_len),
Ian Rogers13735952014-10-08 12:43:28 -0700353 gpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset),
354 fpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset),
355 stack_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize
Mathieu Chartiere401d142015-04-22 13:56:20 -0700356 + sizeof(ArtMethod*)), // Skip ArtMethod*.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800357 gpr_index_(0), fpr_index_(0), fpr_double_index_(0), stack_index_(0),
358 cur_type_(Primitive::kPrimVoid), is_split_long_or_double_(false) {
Andreas Gampe575e78c2014-11-03 23:41:03 -0800359 static_assert(kQuickSoftFloatAbi == (kNumQuickFprArgs == 0),
360 "Number of Quick FPR arguments unexpected");
361 static_assert(!(kQuickSoftFloatAbi && kQuickDoubleRegAlignedFloatBackFilled),
362 "Double alignment unexpected");
Zheng Xu5667fdb2014-10-23 18:29:55 +0800363 // For register alignment, we want to assume that counters(fpr_double_index_) are even if the
364 // next register is even.
Andreas Gampe575e78c2014-11-03 23:41:03 -0800365 static_assert(!kQuickDoubleRegAlignedFloatBackFilled || kNumQuickFprArgs % 2 == 0,
366 "Number of Quick FPR arguments not even");
Mathieu Chartiere401d142015-04-22 13:56:20 -0700367 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), sizeof(void*));
Zheng Xu5667fdb2014-10-23 18:29:55 +0800368 }
Ian Rogers848871b2013-08-05 10:56:33 -0700369
370 virtual ~QuickArgumentVisitor() {}
371
372 virtual void Visit() = 0;
373
Ian Rogers936b37f2014-02-14 00:52:24 -0800374 Primitive::Type GetParamPrimitiveType() const {
375 return cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700376 }
377
Ian Rogers13735952014-10-08 12:43:28 -0700378 uint8_t* GetParamAddress() const {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800379 if (!kQuickSoftFloatAbi) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800380 Primitive::Type type = GetParamPrimitiveType();
381 if (UNLIKELY((type == Primitive::kPrimDouble) || (type == Primitive::kPrimFloat))) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800382 if (type == Primitive::kPrimDouble && kQuickDoubleRegAlignedFloatBackFilled) {
383 if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) {
384 return fpr_args_ + (fpr_double_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
385 }
386 } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000387 return fpr_args_ + (fpr_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800388 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700389 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers936b37f2014-02-14 00:52:24 -0800390 }
391 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800392 if (gpr_index_ < kNumQuickGprArgs) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800393 return gpr_args_ + GprIndexToGprOffset(gpr_index_);
394 }
395 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700396 }
397
398 bool IsSplitLongOrDouble() const {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700399 if ((GetBytesPerGprSpillLocation(kRuntimeISA) == 4) ||
400 (GetBytesPerFprSpillLocation(kRuntimeISA) == 4)) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800401 return is_split_long_or_double_;
402 } else {
403 return false; // An optimization for when GPR and FPRs are 64bit.
404 }
Ian Rogers848871b2013-08-05 10:56:33 -0700405 }
406
Ian Rogers936b37f2014-02-14 00:52:24 -0800407 bool IsParamAReference() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700408 return GetParamPrimitiveType() == Primitive::kPrimNot;
409 }
410
Ian Rogers936b37f2014-02-14 00:52:24 -0800411 bool IsParamALongOrDouble() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700412 Primitive::Type type = GetParamPrimitiveType();
413 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
414 }
415
416 uint64_t ReadSplitLongParam() const {
Nicolas Geoffray425f2392015-01-08 14:52:29 +0000417 // The splitted long is always available through the stack.
418 return *reinterpret_cast<uint64_t*>(stack_args_
419 + stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700420 }
421
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800422 void IncGprIndex() {
423 gpr_index_++;
424 if (kGprFprLockstep) {
425 fpr_index_++;
426 }
427 }
428
429 void IncFprIndex() {
430 fpr_index_++;
431 if (kGprFprLockstep) {
432 gpr_index_++;
433 }
434 }
435
Mathieu Chartier90443472015-07-16 20:32:27 -0700436 void VisitArguments() SHARED_REQUIRES(Locks::mutator_lock_) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800437 // (a) 'stack_args_' should point to the first method's argument
438 // (b) whatever the argument type it is, the 'stack_index_' should
439 // be moved forward along with every visiting.
Ian Rogers936b37f2014-02-14 00:52:24 -0800440 gpr_index_ = 0;
441 fpr_index_ = 0;
Zheng Xu5667fdb2014-10-23 18:29:55 +0800442 if (kQuickDoubleRegAlignedFloatBackFilled) {
443 fpr_double_index_ = 0;
444 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800445 stack_index_ = 0;
446 if (!is_static_) { // Handle this.
447 cur_type_ = Primitive::kPrimNot;
448 is_split_long_or_double_ = false;
Ian Rogers848871b2013-08-05 10:56:33 -0700449 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800450 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800451 if (kNumQuickGprArgs > 0) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800452 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800453 }
Ian Rogers848871b2013-08-05 10:56:33 -0700454 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800455 for (uint32_t shorty_index = 1; shorty_index < shorty_len_; ++shorty_index) {
456 cur_type_ = Primitive::GetType(shorty_[shorty_index]);
457 switch (cur_type_) {
458 case Primitive::kPrimNot:
459 case Primitive::kPrimBoolean:
460 case Primitive::kPrimByte:
461 case Primitive::kPrimChar:
462 case Primitive::kPrimShort:
463 case Primitive::kPrimInt:
464 is_split_long_or_double_ = false;
465 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800466 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800467 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800468 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800469 }
470 break;
471 case Primitive::kPrimFloat:
472 is_split_long_or_double_ = false;
473 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800474 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800475 if (kQuickSoftFloatAbi) {
476 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800477 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800478 }
479 } else {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800480 if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800481 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800482 if (kQuickDoubleRegAlignedFloatBackFilled) {
483 // Double should not overlap with float.
484 // For example, if fpr_index_ = 3, fpr_double_index_ should be at least 4.
485 fpr_double_index_ = std::max(fpr_double_index_, RoundUp(fpr_index_, 2));
486 // Float should not overlap with double.
487 if (fpr_index_ % 2 == 0) {
488 fpr_index_ = std::max(fpr_double_index_, fpr_index_);
489 }
Goran Jakovljevicff734982015-08-24 12:58:55 +0000490 } else if (kQuickSkipOddFpRegisters) {
491 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800492 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800493 }
494 }
495 break;
496 case Primitive::kPrimDouble:
497 case Primitive::kPrimLong:
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800498 if (kQuickSoftFloatAbi || (cur_type_ == Primitive::kPrimLong)) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000499 if (cur_type_ == Primitive::kPrimLong && kAlignPairRegister && gpr_index_ == 0) {
Goran Jakovljevicff734982015-08-24 12:58:55 +0000500 // Currently, this is only for ARM and MIPS, where the first available parameter
501 // register is R1 (on ARM) or A1 (on MIPS). So we skip it, and use R2 (on ARM) or
502 // A2 (on MIPS) instead.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800503 IncGprIndex();
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000504 }
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000505 is_split_long_or_double_ = (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) &&
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800506 ((gpr_index_ + 1) == kNumQuickGprArgs);
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500507 if (!kSplitPairAcrossRegisterAndStack && is_split_long_or_double_) {
508 // We don't want to split this. Pass over this register.
509 gpr_index_++;
510 is_split_long_or_double_ = false;
511 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800512 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800513 if (kBytesStackArgLocation == 4) {
514 stack_index_+= 2;
515 } else {
516 CHECK_EQ(kBytesStackArgLocation, 8U);
517 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800518 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700519 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800520 IncGprIndex();
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000521 if (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700522 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800523 IncGprIndex();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700524 }
525 }
526 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800527 } else {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000528 is_split_long_or_double_ = (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) &&
Zheng Xu5667fdb2014-10-23 18:29:55 +0800529 ((fpr_index_ + 1) == kNumQuickFprArgs) && !kQuickDoubleRegAlignedFloatBackFilled;
Ian Rogers936b37f2014-02-14 00:52:24 -0800530 Visit();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700531 if (kBytesStackArgLocation == 4) {
532 stack_index_+= 2;
Ian Rogers936b37f2014-02-14 00:52:24 -0800533 } else {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700534 CHECK_EQ(kBytesStackArgLocation, 8U);
535 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800536 }
Zheng Xu5667fdb2014-10-23 18:29:55 +0800537 if (kQuickDoubleRegAlignedFloatBackFilled) {
538 if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) {
539 fpr_double_index_ += 2;
540 // Float should not overlap with double.
541 if (fpr_index_ % 2 == 0) {
542 fpr_index_ = std::max(fpr_double_index_, fpr_index_);
543 }
544 }
545 } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800546 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800547 if (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) {
548 if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800549 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800550 }
551 }
552 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800553 }
554 break;
555 default:
556 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty_;
557 }
Ian Rogers848871b2013-08-05 10:56:33 -0700558 }
559 }
560
Andreas Gampec200a4a2014-06-16 18:39:09 -0700561 protected:
Ian Rogers848871b2013-08-05 10:56:33 -0700562 const bool is_static_;
563 const char* const shorty_;
564 const uint32_t shorty_len_;
Andreas Gampec200a4a2014-06-16 18:39:09 -0700565
566 private:
Ian Rogers13735952014-10-08 12:43:28 -0700567 uint8_t* const gpr_args_; // Address of GPR arguments in callee save frame.
568 uint8_t* const fpr_args_; // Address of FPR arguments in callee save frame.
569 uint8_t* const stack_args_; // Address of stack arguments in caller's frame.
Ian Rogers936b37f2014-02-14 00:52:24 -0800570 uint32_t gpr_index_; // Index into spilled GPRs.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800571 // Index into spilled FPRs.
572 // In case kQuickDoubleRegAlignedFloatBackFilled, it may index a hole while fpr_double_index_
573 // holds a higher register number.
574 uint32_t fpr_index_;
575 // Index into spilled FPRs for aligned double.
576 // Only used when kQuickDoubleRegAlignedFloatBackFilled. Next available double register indexed in
577 // terms of singles, may be behind fpr_index.
578 uint32_t fpr_double_index_;
Ian Rogers936b37f2014-02-14 00:52:24 -0800579 uint32_t stack_index_; // Index into arguments on the stack.
580 // The current type of argument during VisitArguments.
581 Primitive::Type cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700582 // Does a 64bit parameter straddle the register and stack arguments?
583 bool is_split_long_or_double_;
584};
585
Sebastien Hertza836bc92014-11-25 16:30:53 +0100586// Returns the 'this' object of a proxy method. This function is only used by StackVisitor. It
587// allows to use the QuickArgumentVisitor constants without moving all the code in its own module.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700588extern "C" mirror::Object* artQuickGetProxyThisObject(ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -0700589 SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertza836bc92014-11-25 16:30:53 +0100590 return QuickArgumentVisitor::GetProxyThisObject(sp);
591}
592
Ian Rogers848871b2013-08-05 10:56:33 -0700593// Visits arguments on the stack placing them into the shadow frame.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800594class BuildQuickShadowFrameVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700595 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700596 BuildQuickShadowFrameVisitor(ArtMethod** sp, bool is_static, const char* shorty,
597 uint32_t shorty_len, ShadowFrame* sf, size_t first_arg_reg) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700598 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), sf_(sf), cur_reg_(first_arg_reg) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700599
Mathieu Chartier90443472015-07-16 20:32:27 -0700600 void Visit() SHARED_REQUIRES(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700601
602 private:
Ian Rogers936b37f2014-02-14 00:52:24 -0800603 ShadowFrame* const sf_;
604 uint32_t cur_reg_;
Ian Rogers848871b2013-08-05 10:56:33 -0700605
Dragos Sbirleabd136a22013-08-13 18:07:04 -0700606 DISALLOW_COPY_AND_ASSIGN(BuildQuickShadowFrameVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700607};
608
Andreas Gampec200a4a2014-06-16 18:39:09 -0700609void BuildQuickShadowFrameVisitor::Visit() {
Ian Rogers9758f792014-03-13 09:02:55 -0700610 Primitive::Type type = GetParamPrimitiveType();
611 switch (type) {
612 case Primitive::kPrimLong: // Fall-through.
613 case Primitive::kPrimDouble:
614 if (IsSplitLongOrDouble()) {
615 sf_->SetVRegLong(cur_reg_, ReadSplitLongParam());
616 } else {
617 sf_->SetVRegLong(cur_reg_, *reinterpret_cast<jlong*>(GetParamAddress()));
618 }
619 ++cur_reg_;
620 break;
621 case Primitive::kPrimNot: {
622 StackReference<mirror::Object>* stack_ref =
623 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
624 sf_->SetVRegReference(cur_reg_, stack_ref->AsMirrorPtr());
625 }
626 break;
627 case Primitive::kPrimBoolean: // Fall-through.
628 case Primitive::kPrimByte: // Fall-through.
629 case Primitive::kPrimChar: // Fall-through.
630 case Primitive::kPrimShort: // Fall-through.
631 case Primitive::kPrimInt: // Fall-through.
632 case Primitive::kPrimFloat:
633 sf_->SetVReg(cur_reg_, *reinterpret_cast<jint*>(GetParamAddress()));
634 break;
635 case Primitive::kPrimVoid:
636 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700637 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -0700638 }
639 ++cur_reg_;
640}
641
Mathieu Chartiere401d142015-04-22 13:56:20 -0700642extern "C" uint64_t artQuickToInterpreterBridge(ArtMethod* method, Thread* self, ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -0700643 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers848871b2013-08-05 10:56:33 -0700644 // Ensure we don't get thread suspension until the object arguments are safely in the shadow
645 // frame.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700646 ScopedQuickEntrypointChecks sqec(self);
Ian Rogers848871b2013-08-05 10:56:33 -0700647
648 if (method->IsAbstract()) {
649 ThrowAbstractMethodError(method);
650 return 0;
Andreas Gampe639bdd12015-06-03 11:22:45 -0700651 }
652
653 JValue tmp_value;
654 ShadowFrame* deopt_frame = self->PopStackedShadowFrame(
655 StackedShadowFrameType::kSingleFrameDeoptimizationShadowFrame, false);
656 const DexFile::CodeItem* code_item = method->GetCodeItem();
657 DCHECK(code_item != nullptr) << PrettyMethod(method);
658 ManagedStack fragment;
659
660 DCHECK(!method->IsNative()) << PrettyMethod(method);
661 uint32_t shorty_len = 0;
662 auto* non_proxy_method = method->GetInterfaceMethodIfProxy(sizeof(void*));
663 const char* shorty = non_proxy_method->GetShorty(&shorty_len);
664
665 JValue result;
666
667 if (deopt_frame != nullptr) {
668 // Coming from single-frame deopt.
669
670 if (kIsDebugBuild) {
671 // Sanity-check: are the methods as expected? We check that the last shadow frame (the bottom
672 // of the call-stack) corresponds to the called method.
673 ShadowFrame* linked = deopt_frame;
674 while (linked->GetLink() != nullptr) {
675 linked = linked->GetLink();
676 }
677 CHECK_EQ(method, linked->GetMethod()) << PrettyMethod(method) << " "
678 << PrettyMethod(linked->GetMethod());
679 }
680
681 if (VLOG_IS_ON(deopt)) {
682 // Print out the stack to verify that it was a single-frame deopt.
683 LOG(INFO) << "Continue-ing from deopt. Stack is:";
684 QuickExceptionHandler::DumpFramesWithType(self, true);
685 }
686
687 mirror::Throwable* pending_exception = nullptr;
688 self->PopDeoptimizationContext(&result, &pending_exception);
689
690 // Push a transition back into managed code onto the linked list in thread.
691 self->PushManagedStackFragment(&fragment);
692
693 // Ensure that the stack is still in order.
694 if (kIsDebugBuild) {
695 class DummyStackVisitor : public StackVisitor {
696 public:
697 explicit DummyStackVisitor(Thread* self_in) SHARED_REQUIRES(Locks::mutator_lock_)
698 : StackVisitor(self_in, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {}
699
700 bool VisitFrame() OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
701 // Nothing to do here. In a debug build, SanityCheckFrame will do the work in the walking
702 // logic. Just always say we want to continue.
703 return true;
704 }
705 };
706 DummyStackVisitor dsv(self);
707 dsv.WalkStack();
708 }
709
710 // Restore the exception that was pending before deoptimization then interpret the
711 // deoptimized frames.
712 if (pending_exception != nullptr) {
713 self->SetException(pending_exception);
714 }
715 interpreter::EnterInterpreterFromDeoptimize(self, deopt_frame, &result);
Ian Rogers848871b2013-08-05 10:56:33 -0700716 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -0700717 const char* old_cause = self->StartAssertNoThreadSuspension(
718 "Building interpreter shadow frame");
Ian Rogers848871b2013-08-05 10:56:33 -0700719 uint16_t num_regs = code_item->registers_size_;
Andreas Gampec200a4a2014-06-16 18:39:09 -0700720 // No last shadow coming from quick.
Andreas Gampeb3025922015-09-01 14:45:00 -0700721 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
Andreas Gampe03ec9302015-08-27 17:41:47 -0700722 CREATE_SHADOW_FRAME(num_regs, /* link */ nullptr, method, /* dex pc */ 0);
Andreas Gampeb3025922015-09-01 14:45:00 -0700723 ShadowFrame* shadow_frame = shadow_frame_unique_ptr.get();
Ian Rogers848871b2013-08-05 10:56:33 -0700724 size_t first_arg_reg = code_item->registers_size_ - code_item->ins_size_;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700725 BuildQuickShadowFrameVisitor shadow_frame_builder(sp, method->IsStatic(), shorty, shorty_len,
Ian Rogers936b37f2014-02-14 00:52:24 -0800726 shadow_frame, first_arg_reg);
Ian Rogers848871b2013-08-05 10:56:33 -0700727 shadow_frame_builder.VisitArguments();
Ian Rogerse94652f2014-12-02 11:13:19 -0800728 const bool needs_initialization =
729 method->IsStatic() && !method->GetDeclaringClass()->IsInitialized();
Ian Rogers848871b2013-08-05 10:56:33 -0700730 // Push a transition back into managed code onto the linked list in thread.
Ian Rogers848871b2013-08-05 10:56:33 -0700731 self->PushManagedStackFragment(&fragment);
732 self->PushShadowFrame(shadow_frame);
733 self->EndAssertNoThreadSuspension(old_cause);
734
Ian Rogerse94652f2014-12-02 11:13:19 -0800735 if (needs_initialization) {
Ian Rogers848871b2013-08-05 10:56:33 -0700736 // Ensure static method's class is initialized.
Ian Rogerse94652f2014-12-02 11:13:19 -0800737 StackHandleScope<1> hs(self);
738 Handle<mirror::Class> h_class(hs.NewHandle(shadow_frame->GetMethod()->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700739 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
Ian Rogerse94652f2014-12-02 11:13:19 -0800740 DCHECK(Thread::Current()->IsExceptionPending()) << PrettyMethod(shadow_frame->GetMethod());
Ian Rogers848871b2013-08-05 10:56:33 -0700741 self->PopManagedStackFragment(fragment);
742 return 0;
743 }
744 }
Daniel Mihalyieb076692014-08-22 17:33:31 +0200745
Andreas Gampe639bdd12015-06-03 11:22:45 -0700746 result = interpreter::EnterInterpreterFromEntryPoint(self, code_item, shadow_frame);
Ian Rogers848871b2013-08-05 10:56:33 -0700747 }
Andreas Gampe639bdd12015-06-03 11:22:45 -0700748
749 // Pop transition.
750 self->PopManagedStackFragment(fragment);
751
752 // Request a stack deoptimization if needed
753 ArtMethod* caller = QuickArgumentVisitor::GetCallingMethod(sp);
754 if (UNLIKELY(Dbg::IsForcedInterpreterNeededForUpcall(self, caller))) {
755 // Push the context of the deoptimization stack so we can restore the return value and the
756 // exception before executing the deoptimized frames.
757 self->PushDeoptimizationContext(result, shorty[0] == 'L', self->GetException());
758
759 // Set special exception to cause deoptimization.
760 self->SetException(Thread::GetDeoptimizationException());
761 }
762
763 // No need to restore the args since the method has already been run by the interpreter.
764 return result.GetJ();
Ian Rogers848871b2013-08-05 10:56:33 -0700765}
766
767// Visits arguments on the stack placing them into the args vector, Object* arguments are converted
768// to jobjects.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800769class BuildQuickArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700770 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700771 BuildQuickArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty, uint32_t shorty_len,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700772 ScopedObjectAccessUnchecked* soa, std::vector<jvalue>* args) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700773 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa), args_(args) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700774
Mathieu Chartier90443472015-07-16 20:32:27 -0700775 void Visit() SHARED_REQUIRES(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700776
Mathieu Chartier90443472015-07-16 20:32:27 -0700777 void FixupReferences() SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800778
Ian Rogers848871b2013-08-05 10:56:33 -0700779 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700780 ScopedObjectAccessUnchecked* const soa_;
781 std::vector<jvalue>* const args_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800782 // References which we must update when exiting in case the GC moved the objects.
Ian Rogers700a4022014-05-19 16:49:03 -0700783 std::vector<std::pair<jobject, StackReference<mirror::Object>*>> references_;
Ian Rogers9758f792014-03-13 09:02:55 -0700784
Ian Rogers848871b2013-08-05 10:56:33 -0700785 DISALLOW_COPY_AND_ASSIGN(BuildQuickArgumentVisitor);
786};
787
Ian Rogers9758f792014-03-13 09:02:55 -0700788void BuildQuickArgumentVisitor::Visit() {
789 jvalue val;
790 Primitive::Type type = GetParamPrimitiveType();
791 switch (type) {
792 case Primitive::kPrimNot: {
793 StackReference<mirror::Object>* stack_ref =
794 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
795 val.l = soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
796 references_.push_back(std::make_pair(val.l, stack_ref));
797 break;
798 }
799 case Primitive::kPrimLong: // Fall-through.
800 case Primitive::kPrimDouble:
801 if (IsSplitLongOrDouble()) {
802 val.j = ReadSplitLongParam();
803 } else {
804 val.j = *reinterpret_cast<jlong*>(GetParamAddress());
805 }
806 break;
807 case Primitive::kPrimBoolean: // Fall-through.
808 case Primitive::kPrimByte: // Fall-through.
809 case Primitive::kPrimChar: // Fall-through.
810 case Primitive::kPrimShort: // Fall-through.
811 case Primitive::kPrimInt: // Fall-through.
812 case Primitive::kPrimFloat:
813 val.i = *reinterpret_cast<jint*>(GetParamAddress());
814 break;
815 case Primitive::kPrimVoid:
816 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700817 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -0700818 }
819 args_->push_back(val);
820}
821
822void BuildQuickArgumentVisitor::FixupReferences() {
823 // Fixup any references which may have changed.
824 for (const auto& pair : references_) {
825 pair.second->Assign(soa_->Decode<mirror::Object*>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -0700826 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -0700827 }
828}
829
Ian Rogers848871b2013-08-05 10:56:33 -0700830// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
831// which is responsible for recording callee save registers. We explicitly place into jobjects the
832// incoming reference arguments (so they survive GC). We invoke the invocation handler, which is a
833// field within the proxy object, which will box the primitive arguments and deal with error cases.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700834extern "C" uint64_t artQuickProxyInvokeHandler(
835 ArtMethod* proxy_method, mirror::Object* receiver, Thread* self, ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -0700836 SHARED_REQUIRES(Locks::mutator_lock_) {
Brian Carlstromd3633d52013-08-20 21:06:26 -0700837 DCHECK(proxy_method->IsProxyMethod()) << PrettyMethod(proxy_method);
838 DCHECK(receiver->GetClass()->IsProxyClass()) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700839 // Ensure we don't get thread suspension until the object arguments are safely in jobjects.
840 const char* old_cause =
841 self->StartAssertNoThreadSuspension("Adding to IRT proxy object arguments");
842 // Register the top of the managed stack, making stack crawlable.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700843 DCHECK_EQ((*sp), proxy_method) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700844 DCHECK_EQ(proxy_method->GetFrameSizeInBytes(),
Brian Carlstromd3633d52013-08-20 21:06:26 -0700845 Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes())
846 << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700847 self->VerifyStack();
848 // Start new JNI local reference state.
849 JNIEnvExt* env = self->GetJniEnv();
850 ScopedObjectAccessUnchecked soa(env);
851 ScopedJniEnvLocalRefState env_state(env);
852 // Create local ref. copies of proxy method and the receiver.
853 jobject rcvr_jobj = soa.AddLocalReference<jobject>(receiver);
854
855 // Placing arguments into args vector and remove the receiver.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700856 ArtMethod* non_proxy_method = proxy_method->GetInterfaceMethodIfProxy(sizeof(void*));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700857 CHECK(!non_proxy_method->IsStatic()) << PrettyMethod(proxy_method) << " "
Andreas Gampec200a4a2014-06-16 18:39:09 -0700858 << PrettyMethod(non_proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700859 std::vector<jvalue> args;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700860 uint32_t shorty_len = 0;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700861 const char* shorty = non_proxy_method->GetShorty(&shorty_len);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700862 BuildQuickArgumentVisitor local_ref_visitor(sp, false, shorty, shorty_len, &soa, &args);
Brian Carlstromd3633d52013-08-20 21:06:26 -0700863
Ian Rogers848871b2013-08-05 10:56:33 -0700864 local_ref_visitor.VisitArguments();
Brian Carlstromd3633d52013-08-20 21:06:26 -0700865 DCHECK_GT(args.size(), 0U) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700866 args.erase(args.begin());
867
868 // Convert proxy method into expected interface method.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700869 ArtMethod* interface_method = proxy_method->FindOverriddenMethod(sizeof(void*));
Ian Rogerse0a02da2014-12-02 14:10:53 -0800870 DCHECK(interface_method != nullptr) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700871 DCHECK(!interface_method->IsProxyMethod()) << PrettyMethod(interface_method);
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700872 self->EndAssertNoThreadSuspension(old_cause);
873 jobject interface_method_jobj = soa.AddLocalReference<jobject>(
874 mirror::Method::CreateFromArtMethod(soa.Self(), interface_method));
Ian Rogers848871b2013-08-05 10:56:33 -0700875
876 // All naked Object*s should now be in jobjects, so its safe to go into the main invoke code
877 // that performs allocations.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700878 JValue result = InvokeProxyInvocationHandler(soa, shorty, rcvr_jobj, interface_method_jobj, args);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800879 // Restore references which might have moved.
880 local_ref_visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -0700881 return result.GetJ();
882}
883
884// Read object references held in arguments from quick frames and place in a JNI local references,
885// so they don't get garbage collected.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800886class RememberForGcArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700887 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700888 RememberForGcArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty,
889 uint32_t shorty_len, ScopedObjectAccessUnchecked* soa) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700890 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700891
Mathieu Chartier90443472015-07-16 20:32:27 -0700892 void Visit() SHARED_REQUIRES(Locks::mutator_lock_) OVERRIDE;
Mathieu Chartier07d447b2013-09-26 11:57:43 -0700893
Mathieu Chartier90443472015-07-16 20:32:27 -0700894 void FixupReferences() SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers848871b2013-08-05 10:56:33 -0700895
896 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700897 ScopedObjectAccessUnchecked* const soa_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800898 // References which we must update when exiting in case the GC moved the objects.
Andreas Gampec200a4a2014-06-16 18:39:09 -0700899 std::vector<std::pair<jobject, StackReference<mirror::Object>*> > references_;
900
Mathieu Chartier590fee92013-09-13 13:46:47 -0700901 DISALLOW_COPY_AND_ASSIGN(RememberForGcArgumentVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700902};
903
Ian Rogers9758f792014-03-13 09:02:55 -0700904void RememberForGcArgumentVisitor::Visit() {
905 if (IsParamAReference()) {
906 StackReference<mirror::Object>* stack_ref =
907 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
908 jobject reference =
909 soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
910 references_.push_back(std::make_pair(reference, stack_ref));
911 }
912}
913
914void RememberForGcArgumentVisitor::FixupReferences() {
915 // Fixup any references which may have changed.
916 for (const auto& pair : references_) {
917 pair.second->Assign(soa_->Decode<mirror::Object*>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -0700918 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -0700919 }
920}
921
Ian Rogers848871b2013-08-05 10:56:33 -0700922// Lazily resolve a method for quick. Called by stub code.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700923extern "C" const void* artQuickResolutionTrampoline(
924 ArtMethod* called, mirror::Object* receiver, Thread* self, ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -0700925 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe3b45ef22015-05-26 21:34:09 -0700926 // The resolution trampoline stashes the resolved method into the callee-save frame to transport
927 // it. Thus, when exiting, the stack cannot be verified (as the resolved method most likely
928 // does not have the same stack layout as the callee-save method).
929 ScopedQuickEntrypointChecks sqec(self, kIsDebugBuild, false);
Ian Rogers848871b2013-08-05 10:56:33 -0700930 // Start new JNI local reference state
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800931 JNIEnvExt* env = self->GetJniEnv();
Ian Rogers848871b2013-08-05 10:56:33 -0700932 ScopedObjectAccessUnchecked soa(env);
933 ScopedJniEnvLocalRefState env_state(env);
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800934 const char* old_cause = self->StartAssertNoThreadSuspension("Quick method resolution set up");
Ian Rogers848871b2013-08-05 10:56:33 -0700935
936 // Compute details about the called method (avoid GCs)
937 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Ian Rogers848871b2013-08-05 10:56:33 -0700938 InvokeType invoke_type;
Ian Rogerse0a02da2014-12-02 14:10:53 -0800939 MethodReference called_method(nullptr, 0);
940 const bool called_method_known_on_entry = !called->IsRuntimeMethod();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700941 ArtMethod* caller = nullptr;
Ian Rogerse0a02da2014-12-02 14:10:53 -0800942 if (!called_method_known_on_entry) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +0100943 caller = QuickArgumentVisitor::GetCallingMethod(sp);
944 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
Ian Rogers848871b2013-08-05 10:56:33 -0700945 const DexFile::CodeItem* code;
Ian Rogerse0a02da2014-12-02 14:10:53 -0800946 called_method.dex_file = caller->GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700947 code = caller->GetCodeItem();
Ian Rogers848871b2013-08-05 10:56:33 -0700948 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
949 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
950 Instruction::Code instr_code = instr->Opcode();
951 bool is_range;
952 switch (instr_code) {
953 case Instruction::INVOKE_DIRECT:
954 invoke_type = kDirect;
955 is_range = false;
956 break;
957 case Instruction::INVOKE_DIRECT_RANGE:
958 invoke_type = kDirect;
959 is_range = true;
960 break;
961 case Instruction::INVOKE_STATIC:
962 invoke_type = kStatic;
963 is_range = false;
964 break;
965 case Instruction::INVOKE_STATIC_RANGE:
966 invoke_type = kStatic;
967 is_range = true;
968 break;
969 case Instruction::INVOKE_SUPER:
970 invoke_type = kSuper;
971 is_range = false;
972 break;
973 case Instruction::INVOKE_SUPER_RANGE:
974 invoke_type = kSuper;
975 is_range = true;
976 break;
977 case Instruction::INVOKE_VIRTUAL:
978 invoke_type = kVirtual;
979 is_range = false;
980 break;
981 case Instruction::INVOKE_VIRTUAL_RANGE:
982 invoke_type = kVirtual;
983 is_range = true;
984 break;
985 case Instruction::INVOKE_INTERFACE:
986 invoke_type = kInterface;
987 is_range = false;
988 break;
989 case Instruction::INVOKE_INTERFACE_RANGE:
990 invoke_type = kInterface;
991 is_range = true;
992 break;
993 default:
Ian Rogerse0a02da2014-12-02 14:10:53 -0800994 LOG(FATAL) << "Unexpected call into trampoline: " << instr->DumpString(nullptr);
995 UNREACHABLE();
Ian Rogers848871b2013-08-05 10:56:33 -0700996 }
Ian Rogerse0a02da2014-12-02 14:10:53 -0800997 called_method.dex_method_index = (is_range) ? instr->VRegB_3rc() : instr->VRegB_35c();
Ian Rogers848871b2013-08-05 10:56:33 -0700998 } else {
999 invoke_type = kStatic;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001000 called_method.dex_file = called->GetDexFile();
1001 called_method.dex_method_index = called->GetDexMethodIndex();
Ian Rogers848871b2013-08-05 10:56:33 -07001002 }
1003 uint32_t shorty_len;
1004 const char* shorty =
Ian Rogerse0a02da2014-12-02 14:10:53 -08001005 called_method.dex_file->GetMethodShorty(
1006 called_method.dex_file->GetMethodId(called_method.dex_method_index), &shorty_len);
Mathieu Chartier590fee92013-09-13 13:46:47 -07001007 RememberForGcArgumentVisitor visitor(sp, invoke_type == kStatic, shorty, shorty_len, &soa);
Ian Rogers848871b2013-08-05 10:56:33 -07001008 visitor.VisitArguments();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001009 self->EndAssertNoThreadSuspension(old_cause);
Ian Rogerse0a02da2014-12-02 14:10:53 -08001010 const bool virtual_or_interface = invoke_type == kVirtual || invoke_type == kInterface;
Ian Rogers848871b2013-08-05 10:56:33 -07001011 // Resolve method filling in dex cache.
Ian Rogerse0a02da2014-12-02 14:10:53 -08001012 if (!called_method_known_on_entry) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001013 StackHandleScope<1> hs(self);
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001014 mirror::Object* dummy = nullptr;
1015 HandleWrapper<mirror::Object> h_receiver(
1016 hs.NewHandleWrapper(virtual_or_interface ? &receiver : &dummy));
Ian Rogerse0a02da2014-12-02 14:10:53 -08001017 DCHECK_EQ(caller->GetDexFile(), called_method.dex_file);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001018 called = linker->ResolveMethod(self, called_method.dex_method_index, caller, invoke_type);
Ian Rogers848871b2013-08-05 10:56:33 -07001019 }
Ian Rogerse0a02da2014-12-02 14:10:53 -08001020 const void* code = nullptr;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001021 if (LIKELY(!self->IsExceptionPending())) {
Ian Rogers848871b2013-08-05 10:56:33 -07001022 // Incompatible class change should have been handled in resolve method.
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001023 CHECK(!called->CheckIncompatibleClassChange(invoke_type))
1024 << PrettyMethod(called) << " " << invoke_type;
Mathieu Chartier55871bf2014-02-27 10:24:50 -08001025 if (virtual_or_interface) {
1026 // Refine called method based on receiver.
1027 CHECK(receiver != nullptr) << invoke_type;
Mingyao Yangf4867782014-05-05 11:55:02 -07001028
Mathieu Chartiere401d142015-04-22 13:56:20 -07001029 ArtMethod* orig_called = called;
Mathieu Chartier55871bf2014-02-27 10:24:50 -08001030 if (invoke_type == kVirtual) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001031 called = receiver->GetClass()->FindVirtualMethodForVirtual(called, sizeof(void*));
Mathieu Chartier55871bf2014-02-27 10:24:50 -08001032 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001033 called = receiver->GetClass()->FindVirtualMethodForInterface(called, sizeof(void*));
Mathieu Chartier55871bf2014-02-27 10:24:50 -08001034 }
Mingyao Yangf4867782014-05-05 11:55:02 -07001035
1036 CHECK(called != nullptr) << PrettyMethod(orig_called) << " "
1037 << PrettyTypeOf(receiver) << " "
1038 << invoke_type << " " << orig_called->GetVtableIndex();
1039
Ian Rogers83883d72013-10-21 21:07:24 -07001040 // 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 -08001041 // of the sharpened method avoiding dirtying the dex cache if possible.
Ian Rogers00f15272014-12-02 16:55:46 -08001042 // Note, called_method.dex_method_index references the dex method before the
1043 // FindVirtualMethodFor... This is ok for FindDexMethodIndexInOtherDexFile that only cares
1044 // about the name and signature.
1045 uint32_t update_dex_cache_method_index = called->GetDexMethodIndex();
Vladimir Marko05792b92015-08-03 11:56:49 +01001046 if (!called->HasSameDexCacheResolvedMethods(caller, sizeof(void*))) {
Ian Rogers83883d72013-10-21 21:07:24 -07001047 // Calling from one dex file to another, need to compute the method index appropriate to
Vladimir Markobbcc0c02014-02-03 14:08:42 +00001048 // the caller's dex file. Since we get here only if the original called was a runtime
1049 // method, we've got the correct dex_file and a dex_method_idx from above.
Ian Rogerse0a02da2014-12-02 14:10:53 -08001050 DCHECK(!called_method_known_on_entry);
1051 DCHECK_EQ(caller->GetDexFile(), called_method.dex_file);
1052 const DexFile* caller_dex_file = called_method.dex_file;
1053 uint32_t caller_method_name_and_sig_index = called_method.dex_method_index;
1054 update_dex_cache_method_index =
1055 called->FindDexMethodIndexInOtherDexFile(*caller_dex_file,
1056 caller_method_name_and_sig_index);
1057 }
1058 if ((update_dex_cache_method_index != DexFile::kDexNoIndex) &&
Mathieu Chartiere401d142015-04-22 13:56:20 -07001059 (caller->GetDexCacheResolvedMethod(
1060 update_dex_cache_method_index, sizeof(void*)) != called)) {
1061 caller->SetDexCacheResolvedMethod(update_dex_cache_method_index, called, sizeof(void*));
Ian Rogers83883d72013-10-21 21:07:24 -07001062 }
Mathieu Chartiere4a91bb2015-01-28 13:11:44 -08001063 } else if (invoke_type == kStatic) {
1064 const auto called_dex_method_idx = called->GetDexMethodIndex();
1065 // For static invokes, we may dispatch to the static method in the superclass but resolve
1066 // using the subclass. To prevent getting slow paths on each invoke, we force set the
1067 // resolved method for the super class dex method index if we are in the same dex file.
1068 // b/19175856
1069 if (called->GetDexFile() == called_method.dex_file &&
1070 called_method.dex_method_index != called_dex_method_idx) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001071 called->GetDexCache()->SetResolvedMethod(called_dex_method_idx, called, sizeof(void*));
Mathieu Chartiere4a91bb2015-01-28 13:11:44 -08001072 }
Ian Rogers83883d72013-10-21 21:07:24 -07001073 }
Daniel Mihalyieb076692014-08-22 17:33:31 +02001074
Ian Rogers848871b2013-08-05 10:56:33 -07001075 // Ensure that the called method's class is initialized.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001076 StackHandleScope<1> hs(soa.Self());
1077 Handle<mirror::Class> called_class(hs.NewHandle(called->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -07001078 linker->EnsureInitialized(soa.Self(), called_class, true, true);
Ian Rogers848871b2013-08-05 10:56:33 -07001079 if (LIKELY(called_class->IsInitialized())) {
Daniel Mihalyieb076692014-08-22 17:33:31 +02001080 if (UNLIKELY(Dbg::IsForcedInterpreterNeededForResolution(self, called))) {
1081 // If we are single-stepping or the called method is deoptimized (by a
1082 // breakpoint, for example), then we have to execute the called method
1083 // with the interpreter.
1084 code = GetQuickToInterpreterBridge();
1085 } else if (UNLIKELY(Dbg::IsForcedInstrumentationNeededForResolution(self, caller))) {
1086 // If the caller is deoptimized (by a breakpoint, for example), we have to
1087 // continue its execution with interpreter when returning from the called
1088 // method. Because we do not want to execute the called method with the
1089 // interpreter, we wrap its execution into the instrumentation stubs.
1090 // When the called method returns, it will execute the instrumentation
1091 // exit hook that will determine the need of the interpreter with a call
1092 // to Dbg::IsForcedInterpreterNeededForUpcall and deoptimize the stack if
1093 // it is needed.
1094 code = GetQuickInstrumentationEntryPoint();
1095 } else {
1096 code = called->GetEntryPointFromQuickCompiledCode();
1097 }
Ian Rogers848871b2013-08-05 10:56:33 -07001098 } else if (called_class->IsInitializing()) {
Daniel Mihalyieb076692014-08-22 17:33:31 +02001099 if (UNLIKELY(Dbg::IsForcedInterpreterNeededForResolution(self, called))) {
1100 // If we are single-stepping or the called method is deoptimized (by a
1101 // breakpoint, for example), then we have to execute the called method
1102 // with the interpreter.
1103 code = GetQuickToInterpreterBridge();
1104 } else if (invoke_type == kStatic) {
Ian Rogers848871b2013-08-05 10:56:33 -07001105 // Class is still initializing, go to oat and grab code (trampoline must be left in place
1106 // until class is initialized to stop races between threads).
Ian Rogersef7d42f2014-01-06 12:55:46 -08001107 code = linker->GetQuickOatCodeFor(called);
Ian Rogers848871b2013-08-05 10:56:33 -07001108 } else {
1109 // No trampoline for non-static methods.
Ian Rogersef7d42f2014-01-06 12:55:46 -08001110 code = called->GetEntryPointFromQuickCompiledCode();
Ian Rogers848871b2013-08-05 10:56:33 -07001111 }
1112 } else {
1113 DCHECK(called_class->IsErroneous());
1114 }
1115 }
Ian Rogerse0a02da2014-12-02 14:10:53 -08001116 CHECK_EQ(code == nullptr, self->IsExceptionPending());
Mathieu Chartier07d447b2013-09-26 11:57:43 -07001117 // Fixup any locally saved objects may have moved during a GC.
1118 visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -07001119 // Place called method in callee-save frame to be placed as first argument to quick method.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001120 *sp = called;
1121
Ian Rogers848871b2013-08-05 10:56:33 -07001122 return code;
1123}
1124
Andreas Gampec147b002014-03-06 18:11:06 -08001125/*
1126 * This class uses a couple of observations to unite the different calling conventions through
1127 * a few constants.
1128 *
1129 * 1) Number of registers used for passing is normally even, so counting down has no penalty for
1130 * possible alignment.
1131 * 2) Known 64b architectures store 8B units on the stack, both for integral and floating point
1132 * types, so using uintptr_t is OK. Also means that we can use kRegistersNeededX to denote
1133 * when we have to split things
1134 * 3) The only soft-float, Arm, is 32b, so no widening needs to be taken into account for floats
1135 * and we can use Int handling directly.
1136 * 4) Only 64b architectures widen, and their stack is aligned 8B anyways, so no padding code
1137 * necessary when widening. Also, widening of Ints will take place implicitly, and the
1138 * extension should be compatible with Aarch64, which mandates copying the available bits
1139 * into LSB and leaving the rest unspecified.
1140 * 5) Aligning longs and doubles is necessary on arm only, and it's the same in registers and on
1141 * the stack.
1142 * 6) There is only little endian.
1143 *
1144 *
1145 * Actual work is supposed to be done in a delegate of the template type. The interface is as
1146 * follows:
1147 *
1148 * void PushGpr(uintptr_t): Add a value for the next GPR
1149 *
1150 * void PushFpr4(float): Add a value for the next FPR of size 32b. Is only called if we need
1151 * padding, that is, think the architecture is 32b and aligns 64b.
1152 *
1153 * void PushFpr8(uint64_t): Push a double. We _will_ call this on 32b, it's the callee's job to
1154 * split this if necessary. The current state will have aligned, if
1155 * necessary.
1156 *
1157 * void PushStack(uintptr_t): Push a value to the stack.
1158 *
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001159 * uintptr_t PushHandleScope(mirror::Object* ref): Add a reference to the HandleScope. This _will_ have nullptr,
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001160 * as this might be important for null initialization.
Andreas Gampec147b002014-03-06 18:11:06 -08001161 * Must return the jobject, that is, the reference to the
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001162 * entry in the HandleScope (nullptr if necessary).
Andreas Gampec147b002014-03-06 18:11:06 -08001163 *
1164 */
Andreas Gampec200a4a2014-06-16 18:39:09 -07001165template<class T> class BuildNativeCallFrameStateMachine {
Andreas Gampec147b002014-03-06 18:11:06 -08001166 public:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001167#if defined(__arm__)
1168 // TODO: These are all dummy values!
Andreas Gampec147b002014-03-06 18:11:06 -08001169 static constexpr bool kNativeSoftFloatAbi = true;
1170 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs, r0-r3
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001171 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
1172
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001173 static constexpr size_t kRegistersNeededForLong = 2;
1174 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -08001175 static constexpr bool kMultiRegistersAligned = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001176 static constexpr bool kMultiFPRegistersWidened = false;
1177 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001178 static constexpr bool kAlignLongOnStack = true;
1179 static constexpr bool kAlignDoubleOnStack = true;
Stuart Monteithb95a5342014-03-12 13:32:32 +00001180#elif defined(__aarch64__)
1181 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
1182 static constexpr size_t kNumNativeGprArgs = 8; // 6 arguments passed in GPRs.
1183 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
1184
1185 static constexpr size_t kRegistersNeededForLong = 1;
1186 static constexpr size_t kRegistersNeededForDouble = 1;
1187 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001188 static constexpr bool kMultiFPRegistersWidened = false;
1189 static constexpr bool kMultiGPRegistersWidened = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +00001190 static constexpr bool kAlignLongOnStack = false;
1191 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001192#elif defined(__mips__) && !defined(__LP64__)
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001193 static constexpr bool kNativeSoftFloatAbi = true; // This is a hard float ABI.
Douglas Leung735b8552014-10-31 12:21:40 -07001194 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs.
1195 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001196
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001197 static constexpr size_t kRegistersNeededForLong = 2;
1198 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -08001199 static constexpr bool kMultiRegistersAligned = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001200 static constexpr bool kMultiFPRegistersWidened = true;
1201 static constexpr bool kMultiGPRegistersWidened = false;
Douglas Leung735b8552014-10-31 12:21:40 -07001202 static constexpr bool kAlignLongOnStack = true;
1203 static constexpr bool kAlignDoubleOnStack = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001204#elif defined(__mips__) && defined(__LP64__)
1205 // Let the code prepare GPRs only and we will load the FPRs with same data.
1206 static constexpr bool kNativeSoftFloatAbi = true;
1207 static constexpr size_t kNumNativeGprArgs = 8;
1208 static constexpr size_t kNumNativeFprArgs = 0;
1209
1210 static constexpr size_t kRegistersNeededForLong = 1;
1211 static constexpr size_t kRegistersNeededForDouble = 1;
1212 static constexpr bool kMultiRegistersAligned = false;
1213 static constexpr bool kMultiFPRegistersWidened = false;
1214 static constexpr bool kMultiGPRegistersWidened = true;
1215 static constexpr bool kAlignLongOnStack = false;
1216 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001217#elif defined(__i386__)
1218 // TODO: Check these!
Andreas Gampec147b002014-03-06 18:11:06 -08001219 static constexpr bool kNativeSoftFloatAbi = false; // Not using int registers for fp
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001220 static constexpr size_t kNumNativeGprArgs = 0; // 6 arguments passed in GPRs.
1221 static constexpr size_t kNumNativeFprArgs = 0; // 8 arguments passed in FPRs.
1222
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001223 static constexpr size_t kRegistersNeededForLong = 2;
1224 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001225 static constexpr bool kMultiRegistersAligned = false; // x86 not using regs, anyways
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001226 static constexpr bool kMultiFPRegistersWidened = false;
1227 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001228 static constexpr bool kAlignLongOnStack = false;
1229 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001230#elif defined(__x86_64__)
1231 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
1232 static constexpr size_t kNumNativeGprArgs = 6; // 6 arguments passed in GPRs.
1233 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
1234
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001235 static constexpr size_t kRegistersNeededForLong = 1;
1236 static constexpr size_t kRegistersNeededForDouble = 1;
Andreas Gampec147b002014-03-06 18:11:06 -08001237 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001238 static constexpr bool kMultiFPRegistersWidened = false;
1239 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001240 static constexpr bool kAlignLongOnStack = false;
1241 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001242#else
1243#error "Unsupported architecture"
1244#endif
1245
Andreas Gampec147b002014-03-06 18:11:06 -08001246 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001247 explicit BuildNativeCallFrameStateMachine(T* delegate)
1248 : gpr_index_(kNumNativeGprArgs),
1249 fpr_index_(kNumNativeFprArgs),
1250 stack_entries_(0),
1251 delegate_(delegate) {
Andreas Gampec147b002014-03-06 18:11:06 -08001252 // For register alignment, we want to assume that counters (gpr_index_, fpr_index_) are even iff
1253 // the next register is even; counting down is just to make the compiler happy...
Andreas Gampe575e78c2014-11-03 23:41:03 -08001254 static_assert(kNumNativeGprArgs % 2 == 0U, "Number of native GPR arguments not even");
1255 static_assert(kNumNativeFprArgs % 2 == 0U, "Number of native FPR arguments not even");
Andreas Gampec147b002014-03-06 18:11:06 -08001256 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001257
Andreas Gampec200a4a2014-06-16 18:39:09 -07001258 virtual ~BuildNativeCallFrameStateMachine() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001259
Ian Rogers1428dce2014-10-21 15:02:15 -07001260 bool HavePointerGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001261 return gpr_index_ > 0;
1262 }
1263
Andreas Gampec200a4a2014-06-16 18:39:09 -07001264 void AdvancePointer(const void* val) {
Andreas Gampec147b002014-03-06 18:11:06 -08001265 if (HavePointerGpr()) {
1266 gpr_index_--;
1267 PushGpr(reinterpret_cast<uintptr_t>(val));
1268 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001269 stack_entries_++; // TODO: have a field for pointer length as multiple of 32b
Andreas Gampec147b002014-03-06 18:11:06 -08001270 PushStack(reinterpret_cast<uintptr_t>(val));
1271 gpr_index_ = 0;
1272 }
1273 }
1274
Ian Rogers1428dce2014-10-21 15:02:15 -07001275 bool HaveHandleScopeGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001276 return gpr_index_ > 0;
1277 }
1278
Mathieu Chartier90443472015-07-16 20:32:27 -07001279 void AdvanceHandleScope(mirror::Object* ptr) SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001280 uintptr_t handle = PushHandle(ptr);
1281 if (HaveHandleScopeGpr()) {
Andreas Gampec147b002014-03-06 18:11:06 -08001282 gpr_index_--;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001283 PushGpr(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001284 } else {
1285 stack_entries_++;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001286 PushStack(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001287 gpr_index_ = 0;
1288 }
1289 }
1290
Ian Rogers1428dce2014-10-21 15:02:15 -07001291 bool HaveIntGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001292 return gpr_index_ > 0;
1293 }
1294
1295 void AdvanceInt(uint32_t val) {
1296 if (HaveIntGpr()) {
1297 gpr_index_--;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001298 if (kMultiGPRegistersWidened) {
1299 DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001300 PushGpr(static_cast<int64_t>(bit_cast<int32_t, uint32_t>(val)));
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001301 } else {
1302 PushGpr(val);
1303 }
Andreas Gampec147b002014-03-06 18:11:06 -08001304 } else {
1305 stack_entries_++;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001306 if (kMultiGPRegistersWidened) {
1307 DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001308 PushStack(static_cast<int64_t>(bit_cast<int32_t, uint32_t>(val)));
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001309 } else {
1310 PushStack(val);
1311 }
Andreas Gampec147b002014-03-06 18:11:06 -08001312 gpr_index_ = 0;
1313 }
1314 }
1315
Ian Rogers1428dce2014-10-21 15:02:15 -07001316 bool HaveLongGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001317 return gpr_index_ >= kRegistersNeededForLong + (LongGprNeedsPadding() ? 1 : 0);
1318 }
1319
Ian Rogers1428dce2014-10-21 15:02:15 -07001320 bool LongGprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001321 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1322 kAlignLongOnStack && // and when it needs alignment
1323 (gpr_index_ & 1) == 1; // counter is odd, see constructor
1324 }
1325
Ian Rogers1428dce2014-10-21 15:02:15 -07001326 bool LongStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001327 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1328 kAlignLongOnStack && // and when it needs 8B alignment
1329 (stack_entries_ & 1) == 1; // counter is odd
1330 }
1331
1332 void AdvanceLong(uint64_t val) {
1333 if (HaveLongGpr()) {
1334 if (LongGprNeedsPadding()) {
1335 PushGpr(0);
1336 gpr_index_--;
1337 }
1338 if (kRegistersNeededForLong == 1) {
1339 PushGpr(static_cast<uintptr_t>(val));
1340 } else {
1341 PushGpr(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1342 PushGpr(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1343 }
1344 gpr_index_ -= kRegistersNeededForLong;
1345 } else {
1346 if (LongStackNeedsPadding()) {
1347 PushStack(0);
1348 stack_entries_++;
1349 }
1350 if (kRegistersNeededForLong == 1) {
1351 PushStack(static_cast<uintptr_t>(val));
1352 stack_entries_++;
1353 } else {
1354 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1355 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1356 stack_entries_ += 2;
1357 }
1358 gpr_index_ = 0;
1359 }
1360 }
1361
Ian Rogers1428dce2014-10-21 15:02:15 -07001362 bool HaveFloatFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001363 return fpr_index_ > 0;
1364 }
1365
Andreas Gampec147b002014-03-06 18:11:06 -08001366 void AdvanceFloat(float val) {
1367 if (kNativeSoftFloatAbi) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001368 AdvanceInt(bit_cast<uint32_t, float>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001369 } else {
1370 if (HaveFloatFpr()) {
1371 fpr_index_--;
1372 if (kRegistersNeededForDouble == 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001373 if (kMultiFPRegistersWidened) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001374 PushFpr8(bit_cast<uint64_t, double>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001375 } else {
1376 // No widening, just use the bits.
Roland Levillainda4d79b2015-03-24 14:36:11 +00001377 PushFpr8(static_cast<uint64_t>(bit_cast<uint32_t, float>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001378 }
1379 } else {
1380 PushFpr4(val);
1381 }
1382 } else {
1383 stack_entries_++;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001384 if (kRegistersNeededForDouble == 1 && kMultiFPRegistersWidened) {
Andreas Gampec147b002014-03-06 18:11:06 -08001385 // Need to widen before storing: Note the "double" in the template instantiation.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001386 // Note: We need to jump through those hoops to make the compiler happy.
1387 DCHECK_EQ(sizeof(uintptr_t), sizeof(uint64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001388 PushStack(static_cast<uintptr_t>(bit_cast<uint64_t, double>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001389 } else {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001390 PushStack(static_cast<uintptr_t>(bit_cast<uint32_t, float>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001391 }
1392 fpr_index_ = 0;
1393 }
1394 }
1395 }
1396
Ian Rogers1428dce2014-10-21 15:02:15 -07001397 bool HaveDoubleFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001398 return fpr_index_ >= kRegistersNeededForDouble + (DoubleFprNeedsPadding() ? 1 : 0);
1399 }
1400
Ian Rogers1428dce2014-10-21 15:02:15 -07001401 bool DoubleFprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001402 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1403 kAlignDoubleOnStack && // and when it needs alignment
1404 (fpr_index_ & 1) == 1; // counter is odd, see constructor
1405 }
1406
Ian Rogers1428dce2014-10-21 15:02:15 -07001407 bool DoubleStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001408 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1409 kAlignDoubleOnStack && // and when it needs 8B alignment
1410 (stack_entries_ & 1) == 1; // counter is odd
1411 }
1412
1413 void AdvanceDouble(uint64_t val) {
1414 if (kNativeSoftFloatAbi) {
1415 AdvanceLong(val);
1416 } else {
1417 if (HaveDoubleFpr()) {
1418 if (DoubleFprNeedsPadding()) {
1419 PushFpr4(0);
1420 fpr_index_--;
1421 }
1422 PushFpr8(val);
1423 fpr_index_ -= kRegistersNeededForDouble;
1424 } else {
1425 if (DoubleStackNeedsPadding()) {
1426 PushStack(0);
1427 stack_entries_++;
1428 }
1429 if (kRegistersNeededForDouble == 1) {
1430 PushStack(static_cast<uintptr_t>(val));
1431 stack_entries_++;
1432 } else {
1433 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1434 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1435 stack_entries_ += 2;
1436 }
1437 fpr_index_ = 0;
1438 }
1439 }
1440 }
1441
Ian Rogers1428dce2014-10-21 15:02:15 -07001442 uint32_t GetStackEntries() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001443 return stack_entries_;
1444 }
1445
Ian Rogers1428dce2014-10-21 15:02:15 -07001446 uint32_t GetNumberOfUsedGprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001447 return kNumNativeGprArgs - gpr_index_;
1448 }
1449
Ian Rogers1428dce2014-10-21 15:02:15 -07001450 uint32_t GetNumberOfUsedFprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001451 return kNumNativeFprArgs - fpr_index_;
1452 }
1453
1454 private:
1455 void PushGpr(uintptr_t val) {
1456 delegate_->PushGpr(val);
1457 }
1458 void PushFpr4(float val) {
1459 delegate_->PushFpr4(val);
1460 }
1461 void PushFpr8(uint64_t val) {
1462 delegate_->PushFpr8(val);
1463 }
1464 void PushStack(uintptr_t val) {
1465 delegate_->PushStack(val);
1466 }
Mathieu Chartier90443472015-07-16 20:32:27 -07001467 uintptr_t PushHandle(mirror::Object* ref) SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001468 return delegate_->PushHandle(ref);
Andreas Gampec147b002014-03-06 18:11:06 -08001469 }
1470
1471 uint32_t gpr_index_; // Number of free GPRs
1472 uint32_t fpr_index_; // Number of free FPRs
1473 uint32_t stack_entries_; // Stack entries are in multiples of 32b, as floats are usually not
1474 // extended
Ian Rogers1428dce2014-10-21 15:02:15 -07001475 T* const delegate_; // What Push implementation gets called
Andreas Gampec147b002014-03-06 18:11:06 -08001476};
1477
Andreas Gampec200a4a2014-06-16 18:39:09 -07001478// Computes the sizes of register stacks and call stack area. Handling of references can be extended
1479// in subclasses.
1480//
1481// To handle native pointers, use "L" in the shorty for an object reference, which simulates
1482// them with handles.
1483class ComputeNativeCallFrameSize {
Andreas Gampec147b002014-03-06 18:11:06 -08001484 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001485 ComputeNativeCallFrameSize() : num_stack_entries_(0) {}
1486
1487 virtual ~ComputeNativeCallFrameSize() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001488
Ian Rogers1428dce2014-10-21 15:02:15 -07001489 uint32_t GetStackSize() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001490 return num_stack_entries_ * sizeof(uintptr_t);
1491 }
1492
Ian Rogers1428dce2014-10-21 15:02:15 -07001493 uint8_t* LayoutCallStack(uint8_t* sp8) const {
Andreas Gampec147b002014-03-06 18:11:06 -08001494 sp8 -= GetStackSize();
Andreas Gampe779f8c92014-06-09 18:29:38 -07001495 // Align by kStackAlignment.
1496 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
Andreas Gampec200a4a2014-06-16 18:39:09 -07001497 return sp8;
Andreas Gampec147b002014-03-06 18:11:06 -08001498 }
1499
Ian Rogers1428dce2014-10-21 15:02:15 -07001500 uint8_t* LayoutCallRegisterStacks(uint8_t* sp8, uintptr_t** start_gpr, uint32_t** start_fpr)
1501 const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001502 // Assumption is OK right now, as we have soft-float arm
1503 size_t fregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeFprArgs;
1504 sp8 -= fregs * sizeof(uintptr_t);
1505 *start_fpr = reinterpret_cast<uint32_t*>(sp8);
1506 size_t iregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeGprArgs;
1507 sp8 -= iregs * sizeof(uintptr_t);
1508 *start_gpr = reinterpret_cast<uintptr_t*>(sp8);
1509 return sp8;
1510 }
Andreas Gampec147b002014-03-06 18:11:06 -08001511
Andreas Gampec200a4a2014-06-16 18:39:09 -07001512 uint8_t* LayoutNativeCall(uint8_t* sp8, uintptr_t** start_stack, uintptr_t** start_gpr,
Ian Rogers1428dce2014-10-21 15:02:15 -07001513 uint32_t** start_fpr) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001514 // Native call stack.
1515 sp8 = LayoutCallStack(sp8);
1516 *start_stack = reinterpret_cast<uintptr_t*>(sp8);
Andreas Gampec147b002014-03-06 18:11:06 -08001517
Andreas Gampec200a4a2014-06-16 18:39:09 -07001518 // Put fprs and gprs below.
1519 sp8 = LayoutCallRegisterStacks(sp8, start_gpr, start_fpr);
Andreas Gampec147b002014-03-06 18:11:06 -08001520
Andreas Gampec200a4a2014-06-16 18:39:09 -07001521 // Return the new bottom.
1522 return sp8;
1523 }
1524
1525 virtual void WalkHeader(BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm)
Mathieu Chartier90443472015-07-16 20:32:27 -07001526 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001527 UNUSED(sm);
1528 }
Andreas Gampec200a4a2014-06-16 18:39:09 -07001529
Mathieu Chartier90443472015-07-16 20:32:27 -07001530 void Walk(const char* shorty, uint32_t shorty_len) SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001531 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize> sm(this);
1532
1533 WalkHeader(&sm);
Andreas Gampec147b002014-03-06 18:11:06 -08001534
1535 for (uint32_t i = 1; i < shorty_len; ++i) {
1536 Primitive::Type cur_type_ = Primitive::GetType(shorty[i]);
1537 switch (cur_type_) {
1538 case Primitive::kPrimNot:
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001539 // TODO: fix abuse of mirror types.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001540 sm.AdvanceHandleScope(
1541 reinterpret_cast<mirror::Object*>(0x12345678));
Andreas Gampec147b002014-03-06 18:11:06 -08001542 break;
1543
1544 case Primitive::kPrimBoolean:
1545 case Primitive::kPrimByte:
1546 case Primitive::kPrimChar:
1547 case Primitive::kPrimShort:
1548 case Primitive::kPrimInt:
1549 sm.AdvanceInt(0);
1550 break;
1551 case Primitive::kPrimFloat:
1552 sm.AdvanceFloat(0);
1553 break;
1554 case Primitive::kPrimDouble:
1555 sm.AdvanceDouble(0);
1556 break;
1557 case Primitive::kPrimLong:
1558 sm.AdvanceLong(0);
1559 break;
1560 default:
1561 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001562 UNREACHABLE();
Andreas Gampec147b002014-03-06 18:11:06 -08001563 }
1564 }
1565
Ian Rogers1428dce2014-10-21 15:02:15 -07001566 num_stack_entries_ = sm.GetStackEntries();
Andreas Gampec147b002014-03-06 18:11:06 -08001567 }
1568
1569 void PushGpr(uintptr_t /* val */) {
1570 // not optimizing registers, yet
1571 }
1572
1573 void PushFpr4(float /* val */) {
1574 // not optimizing registers, yet
1575 }
1576
1577 void PushFpr8(uint64_t /* val */) {
1578 // not optimizing registers, yet
1579 }
1580
1581 void PushStack(uintptr_t /* val */) {
1582 // counting is already done in the superclass
1583 }
1584
Andreas Gampec200a4a2014-06-16 18:39:09 -07001585 virtual uintptr_t PushHandle(mirror::Object* /* ptr */) {
Andreas Gampec147b002014-03-06 18:11:06 -08001586 return reinterpret_cast<uintptr_t>(nullptr);
1587 }
1588
Andreas Gampec200a4a2014-06-16 18:39:09 -07001589 protected:
Andreas Gampec147b002014-03-06 18:11:06 -08001590 uint32_t num_stack_entries_;
1591};
1592
Andreas Gampec200a4a2014-06-16 18:39:09 -07001593class ComputeGenericJniFrameSize FINAL : public ComputeNativeCallFrameSize {
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001594 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001595 ComputeGenericJniFrameSize() : num_handle_scope_references_(0) {}
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001596
Andreas Gampec200a4a2014-06-16 18:39:09 -07001597 // Lays out the callee-save frame. Assumes that the incorrect frame corresponding to RefsAndArgs
1598 // is at *m = sp. Will update to point to the bottom of the save frame.
1599 //
1600 // Note: assumes ComputeAll() has been run before.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001601 void LayoutCalleeSaveFrame(Thread* self, ArtMethod*** m, void* sp, HandleScope** handle_scope)
Mathieu Chartier90443472015-07-16 20:32:27 -07001602 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001603 ArtMethod* method = **m;
1604
1605 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), sizeof(void*));
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001606
Andreas Gampec200a4a2014-06-16 18:39:09 -07001607 uint8_t* sp8 = reinterpret_cast<uint8_t*>(sp);
1608
1609 // First, fix up the layout of the callee-save frame.
1610 // We have to squeeze in the HandleScope, and relocate the method pointer.
1611
1612 // "Free" the slot for the method.
Ian Rogers13735952014-10-08 12:43:28 -07001613 sp8 += sizeof(void*); // In the callee-save frame we use a full pointer.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001614
1615 // Under the callee saves put handle scope and new method stack reference.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001616 size_t handle_scope_size = HandleScope::SizeOf(num_handle_scope_references_);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001617 size_t scope_and_method = handle_scope_size + sizeof(ArtMethod*);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001618
1619 sp8 -= scope_and_method;
1620 // Align by kStackAlignment.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001621 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
Andreas Gampec200a4a2014-06-16 18:39:09 -07001622
Mathieu Chartiere401d142015-04-22 13:56:20 -07001623 uint8_t* sp8_table = sp8 + sizeof(ArtMethod*);
Ian Rogers59c07062014-10-10 13:03:39 -07001624 *handle_scope = HandleScope::Create(sp8_table, self->GetTopHandleScope(),
1625 num_handle_scope_references_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001626
1627 // Add a slot for the method pointer, and fill it. Fix the pointer-pointer given to us.
1628 uint8_t* method_pointer = sp8;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001629 auto** new_method_ref = reinterpret_cast<ArtMethod**>(method_pointer);
1630 *new_method_ref = method;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001631 *m = new_method_ref;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001632 }
1633
Andreas Gampec200a4a2014-06-16 18:39:09 -07001634 // Adds space for the cookie. Note: may leave stack unaligned.
Ian Rogers1428dce2014-10-21 15:02:15 -07001635 void LayoutCookie(uint8_t** sp) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001636 // Reference cookie and padding
1637 *sp -= 8;
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001638 }
1639
Andreas Gampec200a4a2014-06-16 18:39:09 -07001640 // Re-layout the callee-save frame (insert a handle-scope). Then add space for the cookie.
1641 // Returns the new bottom. Note: this may be unaligned.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001642 uint8_t* LayoutJNISaveFrame(Thread* self, ArtMethod*** m, void* sp, HandleScope** handle_scope)
Mathieu Chartier90443472015-07-16 20:32:27 -07001643 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001644 // First, fix up the layout of the callee-save frame.
1645 // We have to squeeze in the HandleScope, and relocate the method pointer.
Ian Rogers59c07062014-10-10 13:03:39 -07001646 LayoutCalleeSaveFrame(self, m, sp, handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001647
1648 // The bottom of the callee-save frame is now where the method is, *m.
1649 uint8_t* sp8 = reinterpret_cast<uint8_t*>(*m);
1650
1651 // Add space for cookie.
1652 LayoutCookie(&sp8);
1653
1654 return sp8;
1655 }
1656
1657 // WARNING: After this, *sp won't be pointing to the method anymore!
Mathieu Chartiere401d142015-04-22 13:56:20 -07001658 uint8_t* ComputeLayout(Thread* self, ArtMethod*** m, const char* shorty, uint32_t shorty_len,
1659 HandleScope** handle_scope, uintptr_t** start_stack, uintptr_t** start_gpr,
1660 uint32_t** start_fpr)
Mathieu Chartier90443472015-07-16 20:32:27 -07001661 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001662 Walk(shorty, shorty_len);
1663
1664 // JNI part.
Ian Rogers59c07062014-10-10 13:03:39 -07001665 uint8_t* sp8 = LayoutJNISaveFrame(self, m, reinterpret_cast<void*>(*m), handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001666
1667 sp8 = LayoutNativeCall(sp8, start_stack, start_gpr, start_fpr);
1668
1669 // Return the new bottom.
1670 return sp8;
1671 }
1672
1673 uintptr_t PushHandle(mirror::Object* /* ptr */) OVERRIDE;
1674
1675 // Add JNIEnv* and jobj/jclass before the shorty-derived elements.
1676 void WalkHeader(BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) OVERRIDE
Mathieu Chartier90443472015-07-16 20:32:27 -07001677 SHARED_REQUIRES(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001678
1679 private:
1680 uint32_t num_handle_scope_references_;
1681};
1682
1683uintptr_t ComputeGenericJniFrameSize::PushHandle(mirror::Object* /* ptr */) {
1684 num_handle_scope_references_++;
1685 return reinterpret_cast<uintptr_t>(nullptr);
1686}
1687
1688void ComputeGenericJniFrameSize::WalkHeader(
1689 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) {
1690 // JNIEnv
1691 sm->AdvancePointer(nullptr);
1692
1693 // Class object or this as first argument
1694 sm->AdvanceHandleScope(reinterpret_cast<mirror::Object*>(0x12345678));
1695}
1696
1697// Class to push values to three separate regions. Used to fill the native call part. Adheres to
1698// the template requirements of BuildGenericJniFrameStateMachine.
1699class FillNativeCall {
1700 public:
1701 FillNativeCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) :
1702 cur_gpr_reg_(gpr_regs), cur_fpr_reg_(fpr_regs), cur_stack_arg_(stack_args) {}
1703
1704 virtual ~FillNativeCall() {}
1705
1706 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) {
1707 cur_gpr_reg_ = gpr_regs;
1708 cur_fpr_reg_ = fpr_regs;
1709 cur_stack_arg_ = stack_args;
Andreas Gampec147b002014-03-06 18:11:06 -08001710 }
1711
1712 void PushGpr(uintptr_t val) {
1713 *cur_gpr_reg_ = val;
1714 cur_gpr_reg_++;
1715 }
1716
1717 void PushFpr4(float val) {
1718 *cur_fpr_reg_ = val;
1719 cur_fpr_reg_++;
1720 }
1721
1722 void PushFpr8(uint64_t val) {
1723 uint64_t* tmp = reinterpret_cast<uint64_t*>(cur_fpr_reg_);
1724 *tmp = val;
1725 cur_fpr_reg_ += 2;
1726 }
1727
1728 void PushStack(uintptr_t val) {
1729 *cur_stack_arg_ = val;
1730 cur_stack_arg_++;
1731 }
1732
Mathieu Chartier90443472015-07-16 20:32:27 -07001733 virtual uintptr_t PushHandle(mirror::Object*) SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001734 LOG(FATAL) << "(Non-JNI) Native call does not use handles.";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001735 UNREACHABLE();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001736 }
1737
1738 private:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001739 uintptr_t* cur_gpr_reg_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001740 uint32_t* cur_fpr_reg_;
1741 uintptr_t* cur_stack_arg_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001742};
Andreas Gampec147b002014-03-06 18:11:06 -08001743
Andreas Gampec200a4a2014-06-16 18:39:09 -07001744// Visits arguments on the stack placing them into a region lower down the stack for the benefit
1745// of transitioning into native code.
1746class BuildGenericJniFrameVisitor FINAL : public QuickArgumentVisitor {
1747 public:
Ian Rogers59c07062014-10-10 13:03:39 -07001748 BuildGenericJniFrameVisitor(Thread* self, bool is_static, const char* shorty, uint32_t shorty_len,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001749 ArtMethod*** sp)
Andreas Gampec200a4a2014-06-16 18:39:09 -07001750 : QuickArgumentVisitor(*sp, is_static, shorty, shorty_len),
1751 jni_call_(nullptr, nullptr, nullptr, nullptr), sm_(&jni_call_) {
1752 ComputeGenericJniFrameSize fsc;
1753 uintptr_t* start_gpr_reg;
1754 uint32_t* start_fpr_reg;
1755 uintptr_t* start_stack_arg;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001756 bottom_of_used_area_ = fsc.ComputeLayout(self, sp, shorty, shorty_len,
Ian Rogers59c07062014-10-10 13:03:39 -07001757 &handle_scope_,
1758 &start_stack_arg,
Andreas Gampec200a4a2014-06-16 18:39:09 -07001759 &start_gpr_reg, &start_fpr_reg);
1760
Andreas Gampec200a4a2014-06-16 18:39:09 -07001761 jni_call_.Reset(start_gpr_reg, start_fpr_reg, start_stack_arg, handle_scope_);
1762
1763 // jni environment is always first argument
1764 sm_.AdvancePointer(self->GetJniEnv());
1765
1766 if (is_static) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001767 sm_.AdvanceHandleScope((**sp)->GetDeclaringClass());
Andreas Gampec200a4a2014-06-16 18:39:09 -07001768 }
1769 }
1770
Mathieu Chartier90443472015-07-16 20:32:27 -07001771 void Visit() SHARED_REQUIRES(Locks::mutator_lock_) OVERRIDE;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001772
Mathieu Chartier90443472015-07-16 20:32:27 -07001773 void FinalizeHandleScope(Thread* self) SHARED_REQUIRES(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001774
1775 StackReference<mirror::Object>* GetFirstHandleScopeEntry()
Mathieu Chartier90443472015-07-16 20:32:27 -07001776 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001777 return handle_scope_->GetHandle(0).GetReference();
1778 }
1779
Mathieu Chartier90443472015-07-16 20:32:27 -07001780 jobject GetFirstHandleScopeJObject() const SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001781 return handle_scope_->GetHandle(0).ToJObject();
1782 }
1783
Ian Rogers1428dce2014-10-21 15:02:15 -07001784 void* GetBottomOfUsedArea() const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001785 return bottom_of_used_area_;
1786 }
1787
1788 private:
1789 // A class to fill a JNI call. Adds reference/handle-scope management to FillNativeCall.
1790 class FillJniCall FINAL : public FillNativeCall {
1791 public:
1792 FillJniCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args,
1793 HandleScope* handle_scope) : FillNativeCall(gpr_regs, fpr_regs, stack_args),
1794 handle_scope_(handle_scope), cur_entry_(0) {}
1795
Mathieu Chartier90443472015-07-16 20:32:27 -07001796 uintptr_t PushHandle(mirror::Object* ref) OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001797
1798 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args, HandleScope* scope) {
1799 FillNativeCall::Reset(gpr_regs, fpr_regs, stack_args);
1800 handle_scope_ = scope;
1801 cur_entry_ = 0U;
1802 }
1803
Mathieu Chartier90443472015-07-16 20:32:27 -07001804 void ResetRemainingScopeSlots() SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001805 // Initialize padding entries.
1806 size_t expected_slots = handle_scope_->NumberOfReferences();
1807 while (cur_entry_ < expected_slots) {
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07001808 handle_scope_->GetMutableHandle(cur_entry_++).Assign(nullptr);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001809 }
1810 DCHECK_NE(cur_entry_, 0U);
1811 }
1812
1813 private:
1814 HandleScope* handle_scope_;
1815 size_t cur_entry_;
1816 };
1817
1818 HandleScope* handle_scope_;
1819 FillJniCall jni_call_;
1820 void* bottom_of_used_area_;
1821
1822 BuildNativeCallFrameStateMachine<FillJniCall> sm_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001823
1824 DISALLOW_COPY_AND_ASSIGN(BuildGenericJniFrameVisitor);
1825};
1826
Andreas Gampec200a4a2014-06-16 18:39:09 -07001827uintptr_t BuildGenericJniFrameVisitor::FillJniCall::PushHandle(mirror::Object* ref) {
1828 uintptr_t tmp;
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07001829 MutableHandle<mirror::Object> h = handle_scope_->GetMutableHandle(cur_entry_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001830 h.Assign(ref);
1831 tmp = reinterpret_cast<uintptr_t>(h.ToJObject());
1832 cur_entry_++;
1833 return tmp;
1834}
1835
Ian Rogers9758f792014-03-13 09:02:55 -07001836void BuildGenericJniFrameVisitor::Visit() {
1837 Primitive::Type type = GetParamPrimitiveType();
1838 switch (type) {
1839 case Primitive::kPrimLong: {
1840 jlong long_arg;
1841 if (IsSplitLongOrDouble()) {
1842 long_arg = ReadSplitLongParam();
1843 } else {
1844 long_arg = *reinterpret_cast<jlong*>(GetParamAddress());
1845 }
1846 sm_.AdvanceLong(long_arg);
1847 break;
1848 }
1849 case Primitive::kPrimDouble: {
1850 uint64_t double_arg;
1851 if (IsSplitLongOrDouble()) {
1852 // Read into union so that we don't case to a double.
1853 double_arg = ReadSplitLongParam();
1854 } else {
1855 double_arg = *reinterpret_cast<uint64_t*>(GetParamAddress());
1856 }
1857 sm_.AdvanceDouble(double_arg);
1858 break;
1859 }
1860 case Primitive::kPrimNot: {
1861 StackReference<mirror::Object>* stack_ref =
1862 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001863 sm_.AdvanceHandleScope(stack_ref->AsMirrorPtr());
Ian Rogers9758f792014-03-13 09:02:55 -07001864 break;
1865 }
1866 case Primitive::kPrimFloat:
1867 sm_.AdvanceFloat(*reinterpret_cast<float*>(GetParamAddress()));
1868 break;
1869 case Primitive::kPrimBoolean: // Fall-through.
1870 case Primitive::kPrimByte: // Fall-through.
1871 case Primitive::kPrimChar: // Fall-through.
1872 case Primitive::kPrimShort: // Fall-through.
1873 case Primitive::kPrimInt: // Fall-through.
1874 sm_.AdvanceInt(*reinterpret_cast<jint*>(GetParamAddress()));
1875 break;
1876 case Primitive::kPrimVoid:
1877 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -07001878 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -07001879 }
1880}
1881
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001882void BuildGenericJniFrameVisitor::FinalizeHandleScope(Thread* self) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001883 // Clear out rest of the scope.
1884 jni_call_.ResetRemainingScopeSlots();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001885 // Install HandleScope.
1886 self->PushHandleScope(handle_scope_);
Ian Rogers9758f792014-03-13 09:02:55 -07001887}
1888
Ian Rogers04c31d22014-07-07 21:44:06 -07001889#if defined(__arm__) || defined(__aarch64__)
Andreas Gampe90546832014-03-12 18:07:19 -07001890extern "C" void* artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07001891#else
1892extern "C" void* artFindNativeMethod(Thread* self);
1893#endif
Andreas Gampe90546832014-03-12 18:07:19 -07001894
Andreas Gampead615172014-04-04 16:20:13 -07001895uint64_t artQuickGenericJniEndJNIRef(Thread* self, uint32_t cookie, jobject l, jobject lock) {
1896 if (lock != nullptr) {
1897 return reinterpret_cast<uint64_t>(JniMethodEndWithReferenceSynchronized(l, cookie, lock, self));
1898 } else {
1899 return reinterpret_cast<uint64_t>(JniMethodEndWithReference(l, cookie, self));
1900 }
1901}
1902
1903void artQuickGenericJniEndJNINonRef(Thread* self, uint32_t cookie, jobject lock) {
1904 if (lock != nullptr) {
1905 JniMethodEndSynchronized(cookie, lock, self);
1906 } else {
1907 JniMethodEnd(cookie, self);
1908 }
1909}
1910
Andreas Gampec147b002014-03-06 18:11:06 -08001911/*
1912 * Initializes an alloca region assumed to be directly below sp for a native call:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001913 * 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 -08001914 * The final element on the stack is a pointer to the native code.
1915 *
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001916 * On entry, the stack has a standard callee-save frame above sp, and an alloca below it.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001917 * We need to fix this, as the handle scope needs to go into the callee-save frame.
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001918 *
Andreas Gampec147b002014-03-06 18:11:06 -08001919 * The return of this function denotes:
1920 * 1) How many bytes of the alloca can be released, if the value is non-negative.
1921 * 2) An error, if the value is negative.
1922 */
Mathieu Chartiere401d142015-04-22 13:56:20 -07001923extern "C" TwoWordReturn artQuickGenericJniTrampoline(Thread* self, ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -07001924 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001925 ArtMethod* called = *sp;
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001926 DCHECK(called->IsNative()) << PrettyMethod(called, true);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001927 uint32_t shorty_len = 0;
1928 const char* shorty = called->GetShorty(&shorty_len);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001929
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001930 // Run the visitor and update sp.
Ian Rogers59c07062014-10-10 13:03:39 -07001931 BuildGenericJniFrameVisitor visitor(self, called->IsStatic(), shorty, shorty_len, &sp);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001932 visitor.VisitArguments();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001933 visitor.FinalizeHandleScope(self);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001934
Andreas Gampec200a4a2014-06-16 18:39:09 -07001935 // Fix up managed-stack things in Thread.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001936 self->SetTopOfStack(sp);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001937
Ian Rogerse0dcd462014-03-08 15:21:04 -08001938 self->VerifyStack();
1939
Andreas Gampe90546832014-03-12 18:07:19 -07001940 // Start JNI, save the cookie.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001941 uint32_t cookie;
1942 if (called->IsSynchronized()) {
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001943 cookie = JniMethodStartSynchronized(visitor.GetFirstHandleScopeJObject(), self);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001944 if (self->IsExceptionPending()) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001945 self->PopHandleScope();
Andreas Gampec147b002014-03-06 18:11:06 -08001946 // A negative value denotes an error.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001947 return GetTwoWordFailureValue();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001948 }
1949 } else {
1950 cookie = JniMethodStart(self);
1951 }
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001952 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
Ian Rogerse0dcd462014-03-08 15:21:04 -08001953 *(sp32 - 1) = cookie;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001954
Andreas Gampe90546832014-03-12 18:07:19 -07001955 // Retrieve the stored native code.
Mathieu Chartier2d721012014-11-10 11:08:06 -08001956 void* nativeCode = called->GetEntryPointFromJni();
Andreas Gampe90546832014-03-12 18:07:19 -07001957
Andreas Gampe9a6a99a2014-03-14 07:52:20 -07001958 // There are two cases for the content of nativeCode:
1959 // 1) Pointer to the native function.
1960 // 2) Pointer to the trampoline for native code binding.
1961 // In the second case, we need to execute the binding and continue with the actual native function
1962 // pointer.
Andreas Gampe90546832014-03-12 18:07:19 -07001963 DCHECK(nativeCode != nullptr);
1964 if (nativeCode == GetJniDlsymLookupStub()) {
Ian Rogers04c31d22014-07-07 21:44:06 -07001965#if defined(__arm__) || defined(__aarch64__)
Andreas Gampe90546832014-03-12 18:07:19 -07001966 nativeCode = artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07001967#else
1968 nativeCode = artFindNativeMethod(self);
1969#endif
Andreas Gampe90546832014-03-12 18:07:19 -07001970
1971 if (nativeCode == nullptr) {
1972 DCHECK(self->IsExceptionPending()); // There should be an exception pending now.
Andreas Gampead615172014-04-04 16:20:13 -07001973
1974 // End JNI, as the assembly will move to deliver the exception.
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001975 jobject lock = called->IsSynchronized() ? visitor.GetFirstHandleScopeJObject() : nullptr;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001976 if (shorty[0] == 'L') {
Andreas Gampead615172014-04-04 16:20:13 -07001977 artQuickGenericJniEndJNIRef(self, cookie, nullptr, lock);
1978 } else {
1979 artQuickGenericJniEndJNINonRef(self, cookie, lock);
1980 }
1981
Andreas Gampec200a4a2014-06-16 18:39:09 -07001982 return GetTwoWordFailureValue();
Andreas Gampe90546832014-03-12 18:07:19 -07001983 }
1984 // Note that the native code pointer will be automatically set by artFindNativeMethod().
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001985 }
1986
Andreas Gampec200a4a2014-06-16 18:39:09 -07001987 // Return native code addr(lo) and bottom of alloca address(hi).
1988 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(visitor.GetBottomOfUsedArea()),
1989 reinterpret_cast<uintptr_t>(nativeCode));
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001990}
1991
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07001992// Defined in quick_jni_entrypoints.cc.
1993extern uint64_t GenericJniMethodEnd(Thread* self, uint32_t saved_local_ref_cookie,
1994 jvalue result, uint64_t result_f, ArtMethod* called,
1995 HandleScope* handle_scope);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001996/*
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001997 * Is called after the native JNI code. Responsible for cleanup (handle scope, saved state) and
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001998 * unlocking.
1999 */
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002000extern "C" uint64_t artQuickGenericJniEndTrampoline(Thread* self,
2001 jvalue result,
2002 uint64_t result_f) {
2003 // We're here just back from a native call. We don't have the shared mutator lock at this point
2004 // yet until we call GoToRunnable() later in GenericJniMethodEnd(). Accessing objects or doing
2005 // anything that requires a mutator lock before that would cause problems as GC may have the
2006 // exclusive mutator lock and may be moving objects, etc.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002007 ArtMethod** sp = self->GetManagedStack()->GetTopQuickFrame();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002008 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002009 ArtMethod* called = *sp;
Ian Rogerse0dcd462014-03-08 15:21:04 -08002010 uint32_t cookie = *(sp32 - 1);
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002011 HandleScope* table = reinterpret_cast<HandleScope*>(reinterpret_cast<uint8_t*>(sp) + sizeof(*sp));
2012 return GenericJniMethodEnd(self, cookie, result, result_f, called, table);
Andreas Gampe2da88232014-02-27 12:26:20 -08002013}
2014
Andreas Gamped58342c2014-06-05 14:18:08 -07002015// We use TwoWordReturn to optimize scalar returns. We use the hi value for code, and the lo value
2016// for the method pointer.
Andreas Gampe51f76352014-05-21 08:28:48 -07002017//
Andreas Gamped58342c2014-06-05 14:18:08 -07002018// 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 -07002019// to hold the mutator lock (see SHARED_REQUIRES(Locks::mutator_lock_) annotations).
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002020
2021template<InvokeType type, bool access_check>
Mathieu Chartiere401d142015-04-22 13:56:20 -07002022static TwoWordReturn artInvokeCommon(uint32_t method_idx, mirror::Object* this_object, Thread* self,
2023 ArtMethod** sp) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002024 ScopedQuickEntrypointChecks sqec(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002025 DCHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs));
2026 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
2027 ArtMethod* method = FindMethodFast(method_idx, this_object, caller_method, access_check, type);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002028 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002029 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()->GetDexFile();
2030 uint32_t shorty_len;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002031 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(method_idx), &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002032 {
2033 // Remember the args in case a GC happens in FindMethodFromCode.
2034 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
2035 RememberForGcArgumentVisitor visitor(sp, type == kStatic, shorty, shorty_len, &soa);
2036 visitor.VisitArguments();
Andreas Gampe3a357142015-08-07 17:20:11 -07002037 method = FindMethodFromCode<type, access_check>(method_idx, &this_object, caller_method,
Mathieu Chartier0cd81352014-05-22 16:48:55 -07002038 self);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002039 visitor.FixupReferences();
2040 }
2041
Ian Rogerse0a02da2014-12-02 14:10:53 -08002042 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002043 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07002044 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002045 }
2046 }
2047 DCHECK(!self->IsExceptionPending());
2048 const void* code = method->GetEntryPointFromQuickCompiledCode();
2049
2050 // When we return, the caller will branch to this address, so it had better not be 0!
Ian Rogerse0a02da2014-12-02 14:10:53 -08002051 DCHECK(code != nullptr) << "Code was null in method: " << PrettyMethod(method)
Andreas Gampec200a4a2014-06-16 18:39:09 -07002052 << " location: "
2053 << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07002054
Andreas Gamped58342c2014-06-05 14:18:08 -07002055 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
2056 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002057}
2058
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01002059// Explicit artInvokeCommon template function declarations to please analysis tool.
2060#define EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(type, access_check) \
Mathieu Chartier90443472015-07-16 20:32:27 -07002061 template SHARED_REQUIRES(Locks::mutator_lock_) \
Mathieu Chartiere401d142015-04-22 13:56:20 -07002062 TwoWordReturn artInvokeCommon<type, access_check>( \
2063 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01002064
2065EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, false);
2066EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, true);
2067EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, false);
2068EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, true);
2069EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, false);
2070EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, true);
2071EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, false);
2072EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, true);
2073EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, false);
2074EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, true);
2075#undef EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL
2076
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002077// See comments in runtime_support_asm.S
Andreas Gampec200a4a2014-06-16 18:39:09 -07002078extern "C" TwoWordReturn artInvokeInterfaceTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002079 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -07002080 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002081 return artInvokeCommon<kInterface, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002082}
2083
Andreas Gampec200a4a2014-06-16 18:39:09 -07002084extern "C" TwoWordReturn artInvokeDirectTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002085 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -07002086 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002087 return artInvokeCommon<kDirect, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002088}
2089
Andreas Gampec200a4a2014-06-16 18:39:09 -07002090extern "C" TwoWordReturn artInvokeStaticTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002091 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -07002092 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002093 return artInvokeCommon<kStatic, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002094}
2095
Andreas Gampec200a4a2014-06-16 18:39:09 -07002096extern "C" TwoWordReturn artInvokeSuperTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002097 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -07002098 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002099 return artInvokeCommon<kSuper, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002100}
2101
Andreas Gampec200a4a2014-06-16 18:39:09 -07002102extern "C" TwoWordReturn artInvokeVirtualTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002103 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -07002104 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002105 return artInvokeCommon<kVirtual, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002106}
2107
2108// Determine target of interface dispatch. This object is known non-null.
Nicolas Geoffray8ea18d02015-05-26 16:29:08 +01002109extern "C" TwoWordReturn artInvokeInterfaceTrampoline(uint32_t dex_method_idx,
Andreas Gampe51f76352014-05-21 08:28:48 -07002110 mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -07002111 Thread* self, ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -07002112 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002113 ScopedQuickEntrypointChecks sqec(self);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002114 // The optimizing compiler currently does not inline methods that have an interface
2115 // invocation. We use the outer method directly to avoid fetching a stack map, which is
2116 // more expensive.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002117 ArtMethod* caller_method = QuickArgumentVisitor::GetOuterMethod(sp);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002118 DCHECK_EQ(caller_method, QuickArgumentVisitor::GetCallingMethod(sp));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002119 ArtMethod* interface_method = caller_method->GetDexCacheResolvedMethod(
2120 dex_method_idx, sizeof(void*));
2121 DCHECK(interface_method != nullptr) << dex_method_idx << " " << PrettyMethod(caller_method);
2122 ArtMethod* method;
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002123 if (LIKELY(interface_method->GetDexMethodIndex() != DexFile::kDexNoIndex)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002124 method = this_object->GetClass()->FindVirtualMethodForInterface(
2125 interface_method, sizeof(void*));
Ian Rogerse0a02da2014-12-02 14:10:53 -08002126 if (UNLIKELY(method == nullptr)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002127 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(
2128 interface_method, this_object, caller_method);
Andreas Gamped58342c2014-06-05 14:18:08 -07002129 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002130 }
2131 } else {
Mathieu Chartier4edd8472015-06-01 10:47:36 -07002132 DCHECK_EQ(interface_method, Runtime::Current()->GetResolutionMethod());
Nicolas Geoffray8ea18d02015-05-26 16:29:08 +01002133 if (kIsDebugBuild) {
2134 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
2135 const DexFile::CodeItem* code = caller_method->GetCodeItem();
2136 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
2137 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
2138 Instruction::Code instr_code = instr->Opcode();
2139 CHECK(instr_code == Instruction::INVOKE_INTERFACE ||
2140 instr_code == Instruction::INVOKE_INTERFACE_RANGE)
2141 << "Unexpected call into interface trampoline: " << instr->DumpString(nullptr);
2142 if (instr_code == Instruction::INVOKE_INTERFACE) {
2143 CHECK_EQ(dex_method_idx, instr->VRegB_35c());
2144 } else {
2145 CHECK_EQ(instr_code, Instruction::INVOKE_INTERFACE_RANGE);
2146 CHECK_EQ(dex_method_idx, instr->VRegB_3rc());
2147 }
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002148 }
2149
Andreas Gampec200a4a2014-06-16 18:39:09 -07002150 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()
2151 ->GetDexFile();
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002152 uint32_t shorty_len;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002153 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(dex_method_idx),
2154 &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002155 {
2156 // Remember the args in case a GC happens in FindMethodFromCode.
2157 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
2158 RememberForGcArgumentVisitor visitor(sp, false, shorty, shorty_len, &soa);
2159 visitor.VisitArguments();
Andreas Gampe3a357142015-08-07 17:20:11 -07002160 method = FindMethodFromCode<kInterface, false>(dex_method_idx, &this_object, caller_method,
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002161 self);
2162 visitor.FixupReferences();
2163 }
2164
2165 if (UNLIKELY(method == nullptr)) {
2166 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07002167 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002168 }
2169 }
2170 const void* code = method->GetEntryPointFromQuickCompiledCode();
2171
2172 // When we return, the caller will branch to this address, so it had better not be 0!
Ian Rogerse0a02da2014-12-02 14:10:53 -08002173 DCHECK(code != nullptr) << "Code was null in method: " << PrettyMethod(method)
Andreas Gampec200a4a2014-06-16 18:39:09 -07002174 << " location: " << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07002175
Andreas Gamped58342c2014-06-05 14:18:08 -07002176 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
2177 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002178}
2179
Ian Rogers848871b2013-08-05 10:56:33 -07002180} // namespace art