blob: da4b82c43b07fffb28ed07184b04944a2fc45fe0 [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"
Ian Rogers848871b2013-08-05 10:56:33 -070032#include "runtime.h"
Ian Rogers53b8b092014-03-13 23:45:53 -070033#include "scoped_thread_state_change.h"
Daniel Mihalyieb076692014-08-22 17:33:31 +020034#include "debugger.h"
Ian Rogers848871b2013-08-05 10:56:33 -070035
36namespace art {
37
38// Visits the arguments as saved to the stack by a Runtime::kRefAndArgs callee save frame.
39class QuickArgumentVisitor {
Ian Rogers936b37f2014-02-14 00:52:24 -080040 // Number of bytes for each out register in the caller method's frame.
41 static constexpr size_t kBytesStackArgLocation = 4;
Alexei Zavjalov41c507a2014-05-15 16:02:46 +070042 // Frame size in bytes of a callee-save frame for RefsAndArgs.
43 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize =
44 GetCalleeSaveFrameSize(kRuntimeISA, Runtime::kRefsAndArgs);
Ian Rogers848871b2013-08-05 10:56:33 -070045#if defined(__arm__)
46 // The callee save frame is pointed to by SP.
47 // | argN | |
48 // | ... | |
49 // | arg4 | |
50 // | arg3 spill | | Caller's frame
51 // | arg2 spill | |
52 // | arg1 spill | |
53 // | Method* | ---
54 // | LR |
Zheng Xu5667fdb2014-10-23 18:29:55 +080055 // | ... | 4x6 bytes callee saves
56 // | R3 |
57 // | R2 |
58 // | R1 |
59 // | S15 |
60 // | : |
61 // | S0 |
62 // | | 4x2 bytes padding
Ian Rogers848871b2013-08-05 10:56:33 -070063 // | Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -050064 static constexpr bool kSplitPairAcrossRegisterAndStack = kArm32QuickCodeUseSoftFloat;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +000065 static constexpr bool kAlignPairRegister = !kArm32QuickCodeUseSoftFloat;
Zheng Xu5667fdb2014-10-23 18:29:55 +080066 static constexpr bool kQuickSoftFloatAbi = kArm32QuickCodeUseSoftFloat;
67 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = !kArm32QuickCodeUseSoftFloat;
68 static constexpr size_t kNumQuickGprArgs = 3;
69 static constexpr size_t kNumQuickFprArgs = kArm32QuickCodeUseSoftFloat ? 0 : 16;
Andreas Gampe1a5c4062015-01-15 12:10:47 -080070 static constexpr bool kGprFprLockstep = false;
Zheng Xub551fdc2014-07-25 11:49:42 +080071 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset =
72 arm::ArmCalleeSaveFpr1Offset(Runtime::kRefsAndArgs); // Offset of first FPR arg.
73 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset =
74 arm::ArmCalleeSaveGpr1Offset(Runtime::kRefsAndArgs); // Offset of first GPR arg.
75 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset =
76 arm::ArmCalleeSaveLrOffset(Runtime::kRefsAndArgs); // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -080077 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +000078 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -080079 }
Stuart Monteithb95a5342014-03-12 13:32:32 +000080#elif defined(__aarch64__)
81 // The callee save frame is pointed to by SP.
82 // | argN | |
83 // | ... | |
84 // | arg4 | |
85 // | arg3 spill | | Caller's frame
86 // | arg2 spill | |
87 // | arg1 spill | |
88 // | Method* | ---
89 // | LR |
Zheng Xub551fdc2014-07-25 11:49:42 +080090 // | X29 |
Stuart Monteithb95a5342014-03-12 13:32:32 +000091 // | : |
Serban Constantinescu9bd88b02015-04-22 16:24:46 +010092 // | X20 |
Stuart Monteithb95a5342014-03-12 13:32:32 +000093 // | X7 |
94 // | : |
95 // | X1 |
Zheng Xub551fdc2014-07-25 11:49:42 +080096 // | D7 |
Stuart Monteithb95a5342014-03-12 13:32:32 +000097 // | : |
98 // | D0 |
99 // | | padding
100 // | Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500101 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000102 static constexpr bool kAlignPairRegister = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000103 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800104 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000105 static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs.
106 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800107 static constexpr bool kGprFprLockstep = false;
Zheng Xub551fdc2014-07-25 11:49:42 +0800108 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset =
109 arm64::Arm64CalleeSaveFpr1Offset(Runtime::kRefsAndArgs); // Offset of first FPR arg.
110 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset =
111 arm64::Arm64CalleeSaveGpr1Offset(Runtime::kRefsAndArgs); // Offset of first GPR arg.
112 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset =
113 arm64::Arm64CalleeSaveLrOffset(Runtime::kRefsAndArgs); // Offset of return address.
Stuart Monteithb95a5342014-03-12 13:32:32 +0000114 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000115 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Stuart Monteithb95a5342014-03-12 13:32:32 +0000116 }
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800117#elif defined(__mips__) && !defined(__LP64__)
Ian Rogers848871b2013-08-05 10:56:33 -0700118 // The callee save frame is pointed to by SP.
119 // | argN | |
120 // | ... | |
121 // | arg4 | |
122 // | arg3 spill | | Caller's frame
123 // | arg2 spill | |
124 // | arg1 spill | |
125 // | Method* | ---
126 // | RA |
127 // | ... | callee saves
128 // | A3 | arg3
129 // | A2 | arg2
130 // | A1 | arg1
131 // | A0/Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500132 static constexpr bool kSplitPairAcrossRegisterAndStack = true;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000133 static constexpr bool kAlignPairRegister = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800134 static constexpr bool kQuickSoftFloatAbi = true; // This is a soft float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800135 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800136 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs.
137 static constexpr size_t kNumQuickFprArgs = 0; // 0 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800138 static constexpr bool kGprFprLockstep = false;
Ian Rogers936b37f2014-02-14 00:52:24 -0800139 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 0; // Offset of first FPR arg.
Douglas Leungc6d86722014-12-10 16:15:17 -0800140 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 16; // Offset of first GPR arg.
Ian Rogers936b37f2014-02-14 00:52:24 -0800141 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 60; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800142 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000143 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800144 }
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800145#elif defined(__mips__) && defined(__LP64__)
146 // The callee save frame is pointed to by SP.
147 // | argN | |
148 // | ... | |
149 // | arg4 | |
150 // | arg3 spill | | Caller's frame
151 // | arg2 spill | |
152 // | arg1 spill | |
153 // | Method* | ---
154 // | RA |
155 // | ... | callee saves
156 // | F7 | f_arg7
157 // | F6 | f_arg6
158 // | F5 | f_arg5
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800159 // | F4 | f_arg4
160 // | F3 | f_arg3
161 // | F2 | f_arg2
162 // | F1 | f_arg1
163 // | F0 | f_arg0
164 // | A7 | arg7
165 // | A6 | arg6
166 // | A5 | arg5
167 // | A4 | arg4
168 // | A3 | arg3
169 // | A2 | arg2
170 // | A1 | arg1
171 // | | padding
172 // | A0/Method* | <- sp
173 // NOTE: for Mip64, when A0 is skipped, F0 is also skipped.
Douglas Leungd18e0832015-02-09 15:22:26 -0800174 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800175 static constexpr bool kAlignPairRegister = false;
176 static constexpr bool kQuickSoftFloatAbi = false;
177 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
178 // These values are set to zeros because GPR and FPR register
179 // assignments for Mips64 are interleaved, which the current VisitArguments()
180 // function does not support.
181 static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs.
182 static constexpr size_t kNumQuickFprArgs = 7; // 7 arguments passed in FPRs.
183 static constexpr bool kGprFprLockstep = true;
184
185 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 24; // Offset of first FPR arg (F1).
186 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 80; // Offset of first GPR arg (A1).
187 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 200; // Offset of return address.
188 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
189 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
190 }
Ian Rogers848871b2013-08-05 10:56:33 -0700191#elif defined(__i386__)
192 // The callee save frame is pointed to by SP.
193 // | argN | |
194 // | ... | |
195 // | arg4 | |
196 // | arg3 spill | | Caller's frame
197 // | arg2 spill | |
198 // | arg1 spill | |
199 // | Method* | ---
200 // | Return |
201 // | EBP,ESI,EDI | callee saves
202 // | EBX | arg3
203 // | EDX | arg2
204 // | ECX | arg1
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000205 // | XMM3 | float arg 4
206 // | XMM2 | float arg 3
207 // | XMM1 | float arg 2
208 // | XMM0 | float arg 1
Ian Rogers848871b2013-08-05 10:56:33 -0700209 // | EAX/Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500210 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000211 static constexpr bool kAlignPairRegister = false;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000212 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800213 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800214 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs.
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000215 static constexpr size_t kNumQuickFprArgs = 4; // 4 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800216 static constexpr bool kGprFprLockstep = false;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000217 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 4; // Offset of first FPR arg.
218 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 4 + 4*8; // Offset of first GPR arg.
219 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 28 + 4*8; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800220 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000221 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800222 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800223#elif defined(__x86_64__)
Ian Rogers936b37f2014-02-14 00:52:24 -0800224 // The callee save frame is pointed to by SP.
225 // | argN | |
226 // | ... | |
227 // | reg. arg spills | | Caller's frame
228 // | Method* | ---
229 // | Return |
230 // | R15 | callee save
231 // | R14 | callee save
232 // | R13 | callee save
233 // | R12 | callee save
234 // | R9 | arg5
235 // | R8 | arg4
236 // | RSI/R6 | arg1
237 // | RBP/R5 | callee save
238 // | RBX/R3 | callee save
239 // | RDX/R2 | arg2
240 // | RCX/R1 | arg3
241 // | XMM7 | float arg 8
242 // | XMM6 | float arg 7
243 // | XMM5 | float arg 6
244 // | XMM4 | float arg 5
245 // | XMM3 | float arg 4
246 // | XMM2 | float arg 3
247 // | XMM1 | float arg 2
248 // | XMM0 | float arg 1
249 // | Padding |
250 // | RDI/Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500251 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000252 static constexpr bool kAlignPairRegister = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800253 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800254 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700255 static constexpr size_t kNumQuickGprArgs = 5; // 5 arguments passed in GPRs.
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700256 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800257 static constexpr bool kGprFprLockstep = false;
Ian Rogers936b37f2014-02-14 00:52:24 -0800258 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 16; // Offset of first FPR arg.
Serguei Katkovc3801912014-07-08 17:21:53 +0700259 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 80 + 4*8; // Offset of first GPR arg.
260 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 168 + 4*8; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800261 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
262 switch (gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000263 case 0: return (4 * GetBytesPerGprSpillLocation(kRuntimeISA));
264 case 1: return (1 * GetBytesPerGprSpillLocation(kRuntimeISA));
265 case 2: return (0 * GetBytesPerGprSpillLocation(kRuntimeISA));
266 case 3: return (5 * GetBytesPerGprSpillLocation(kRuntimeISA));
267 case 4: return (6 * GetBytesPerGprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800268 default:
Andreas Gampec200a4a2014-06-16 18:39:09 -0700269 LOG(FATAL) << "Unexpected GPR index: " << gpr_index;
270 return 0;
Ian Rogers936b37f2014-02-14 00:52:24 -0800271 }
272 }
Ian Rogers848871b2013-08-05 10:56:33 -0700273#else
274#error "Unsupported architecture"
Ian Rogers848871b2013-08-05 10:56:33 -0700275#endif
276
Ian Rogers936b37f2014-02-14 00:52:24 -0800277 public:
Sebastien Hertza836bc92014-11-25 16:30:53 +0100278 // Special handling for proxy methods. Proxy methods are instance methods so the
279 // 'this' object is the 1st argument. They also have the same frame layout as the
280 // kRefAndArgs runtime method. Since 'this' is a reference, it is located in the
281 // 1st GPR.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700282 static mirror::Object* GetProxyThisObject(ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -0700283 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700284 CHECK((*sp)->IsProxyMethod());
285 CHECK_EQ(kQuickCalleeSaveFrame_RefAndArgs_FrameSize, (*sp)->GetFrameSizeInBytes());
Sebastien Hertza836bc92014-11-25 16:30:53 +0100286 CHECK_GT(kNumQuickGprArgs, 0u);
287 constexpr uint32_t kThisGprIndex = 0u; // 'this' is in the 1st GPR.
288 size_t this_arg_offset = kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset +
289 GprIndexToGprOffset(kThisGprIndex);
290 uint8_t* this_arg_address = reinterpret_cast<uint8_t*>(sp) + this_arg_offset;
291 return reinterpret_cast<StackReference<mirror::Object>*>(this_arg_address)->AsMirrorPtr();
292 }
293
Mathieu Chartier90443472015-07-16 20:32:27 -0700294 static ArtMethod* GetCallingMethod(ArtMethod** sp) SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700295 DCHECK((*sp)->IsCalleeSaveMethod());
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +0100296 return GetCalleeSaveMethodCaller(sp, Runtime::kRefsAndArgs);
297 }
298
Mathieu Chartier90443472015-07-16 20:32:27 -0700299 static ArtMethod* GetOuterMethod(ArtMethod** sp) SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700300 DCHECK((*sp)->IsCalleeSaveMethod());
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100301 uint8_t* previous_sp =
302 reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700303 return *reinterpret_cast<ArtMethod**>(previous_sp);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100304 }
305
Mathieu Chartier90443472015-07-16 20:32:27 -0700306 static uint32_t GetCallingDexPc(ArtMethod** sp) SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700307 DCHECK((*sp)->IsCalleeSaveMethod());
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100308 const size_t callee_frame_size = GetCalleeSaveFrameSize(kRuntimeISA, Runtime::kRefsAndArgs);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700309 ArtMethod** caller_sp = reinterpret_cast<ArtMethod**>(
310 reinterpret_cast<uintptr_t>(sp) + callee_frame_size);
311 ArtMethod* outer_method = *caller_sp;
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100312 uintptr_t outer_pc = QuickArgumentVisitor::GetCallingPc(sp);
313 uintptr_t outer_pc_offset = outer_method->NativeQuickPcOffset(outer_pc);
314
315 if (outer_method->IsOptimized(sizeof(void*))) {
316 CodeInfo code_info = outer_method->GetOptimizedCodeInfo();
David Brazdilf677ebf2015-05-29 16:29:43 +0100317 StackMapEncoding encoding = code_info.ExtractEncoding();
318 StackMap stack_map = code_info.GetStackMapForNativePcOffset(outer_pc_offset, encoding);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100319 DCHECK(stack_map.IsValid());
David Brazdilf677ebf2015-05-29 16:29:43 +0100320 if (stack_map.HasInlineInfo(encoding)) {
321 InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map, encoding);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100322 return inline_info.GetDexPcAtDepth(inline_info.GetDepth() - 1);
323 } else {
David Brazdilf677ebf2015-05-29 16:29:43 +0100324 return stack_map.GetDexPc(encoding);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100325 }
326 } else {
327 return outer_method->ToDexPc(outer_pc);
328 }
Ian Rogers848871b2013-08-05 10:56:33 -0700329 }
330
Ian Rogers936b37f2014-02-14 00:52:24 -0800331 // For the given quick ref and args quick frame, return the caller's PC.
Mathieu Chartier90443472015-07-16 20:32:27 -0700332 static uintptr_t GetCallingPc(ArtMethod** sp) SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700333 DCHECK((*sp)->IsCalleeSaveMethod());
Ian Rogers13735952014-10-08 12:43:28 -0700334 uint8_t* lr = reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_LrOffset;
Ian Rogers848871b2013-08-05 10:56:33 -0700335 return *reinterpret_cast<uintptr_t*>(lr);
336 }
337
Mathieu Chartiere401d142015-04-22 13:56:20 -0700338 QuickArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty,
Mathieu Chartier90443472015-07-16 20:32:27 -0700339 uint32_t shorty_len) SHARED_REQUIRES(Locks::mutator_lock_) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700340 is_static_(is_static), shorty_(shorty), shorty_len_(shorty_len),
Ian Rogers13735952014-10-08 12:43:28 -0700341 gpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset),
342 fpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset),
343 stack_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize
Mathieu Chartiere401d142015-04-22 13:56:20 -0700344 + sizeof(ArtMethod*)), // Skip ArtMethod*.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800345 gpr_index_(0), fpr_index_(0), fpr_double_index_(0), stack_index_(0),
346 cur_type_(Primitive::kPrimVoid), is_split_long_or_double_(false) {
Andreas Gampe575e78c2014-11-03 23:41:03 -0800347 static_assert(kQuickSoftFloatAbi == (kNumQuickFprArgs == 0),
348 "Number of Quick FPR arguments unexpected");
349 static_assert(!(kQuickSoftFloatAbi && kQuickDoubleRegAlignedFloatBackFilled),
350 "Double alignment unexpected");
Zheng Xu5667fdb2014-10-23 18:29:55 +0800351 // For register alignment, we want to assume that counters(fpr_double_index_) are even if the
352 // next register is even.
Andreas Gampe575e78c2014-11-03 23:41:03 -0800353 static_assert(!kQuickDoubleRegAlignedFloatBackFilled || kNumQuickFprArgs % 2 == 0,
354 "Number of Quick FPR arguments not even");
Mathieu Chartiere401d142015-04-22 13:56:20 -0700355 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), sizeof(void*));
Zheng Xu5667fdb2014-10-23 18:29:55 +0800356 }
Ian Rogers848871b2013-08-05 10:56:33 -0700357
358 virtual ~QuickArgumentVisitor() {}
359
360 virtual void Visit() = 0;
361
Ian Rogers936b37f2014-02-14 00:52:24 -0800362 Primitive::Type GetParamPrimitiveType() const {
363 return cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700364 }
365
Ian Rogers13735952014-10-08 12:43:28 -0700366 uint8_t* GetParamAddress() const {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800367 if (!kQuickSoftFloatAbi) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800368 Primitive::Type type = GetParamPrimitiveType();
369 if (UNLIKELY((type == Primitive::kPrimDouble) || (type == Primitive::kPrimFloat))) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800370 if (type == Primitive::kPrimDouble && kQuickDoubleRegAlignedFloatBackFilled) {
371 if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) {
372 return fpr_args_ + (fpr_double_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
373 }
374 } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000375 return fpr_args_ + (fpr_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800376 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700377 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers936b37f2014-02-14 00:52:24 -0800378 }
379 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800380 if (gpr_index_ < kNumQuickGprArgs) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800381 return gpr_args_ + GprIndexToGprOffset(gpr_index_);
382 }
383 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700384 }
385
386 bool IsSplitLongOrDouble() const {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700387 if ((GetBytesPerGprSpillLocation(kRuntimeISA) == 4) ||
388 (GetBytesPerFprSpillLocation(kRuntimeISA) == 4)) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800389 return is_split_long_or_double_;
390 } else {
391 return false; // An optimization for when GPR and FPRs are 64bit.
392 }
Ian Rogers848871b2013-08-05 10:56:33 -0700393 }
394
Ian Rogers936b37f2014-02-14 00:52:24 -0800395 bool IsParamAReference() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700396 return GetParamPrimitiveType() == Primitive::kPrimNot;
397 }
398
Ian Rogers936b37f2014-02-14 00:52:24 -0800399 bool IsParamALongOrDouble() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700400 Primitive::Type type = GetParamPrimitiveType();
401 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
402 }
403
404 uint64_t ReadSplitLongParam() const {
Nicolas Geoffray425f2392015-01-08 14:52:29 +0000405 // The splitted long is always available through the stack.
406 return *reinterpret_cast<uint64_t*>(stack_args_
407 + stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700408 }
409
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800410 void IncGprIndex() {
411 gpr_index_++;
412 if (kGprFprLockstep) {
413 fpr_index_++;
414 }
415 }
416
417 void IncFprIndex() {
418 fpr_index_++;
419 if (kGprFprLockstep) {
420 gpr_index_++;
421 }
422 }
423
Mathieu Chartier90443472015-07-16 20:32:27 -0700424 void VisitArguments() SHARED_REQUIRES(Locks::mutator_lock_) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800425 // (a) 'stack_args_' should point to the first method's argument
426 // (b) whatever the argument type it is, the 'stack_index_' should
427 // be moved forward along with every visiting.
Ian Rogers936b37f2014-02-14 00:52:24 -0800428 gpr_index_ = 0;
429 fpr_index_ = 0;
Zheng Xu5667fdb2014-10-23 18:29:55 +0800430 if (kQuickDoubleRegAlignedFloatBackFilled) {
431 fpr_double_index_ = 0;
432 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800433 stack_index_ = 0;
434 if (!is_static_) { // Handle this.
435 cur_type_ = Primitive::kPrimNot;
436 is_split_long_or_double_ = false;
Ian Rogers848871b2013-08-05 10:56:33 -0700437 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800438 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800439 if (kNumQuickGprArgs > 0) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800440 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800441 }
Ian Rogers848871b2013-08-05 10:56:33 -0700442 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800443 for (uint32_t shorty_index = 1; shorty_index < shorty_len_; ++shorty_index) {
444 cur_type_ = Primitive::GetType(shorty_[shorty_index]);
445 switch (cur_type_) {
446 case Primitive::kPrimNot:
447 case Primitive::kPrimBoolean:
448 case Primitive::kPrimByte:
449 case Primitive::kPrimChar:
450 case Primitive::kPrimShort:
451 case Primitive::kPrimInt:
452 is_split_long_or_double_ = false;
453 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800454 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800455 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800456 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800457 }
458 break;
459 case Primitive::kPrimFloat:
460 is_split_long_or_double_ = false;
461 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800462 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800463 if (kQuickSoftFloatAbi) {
464 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800465 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800466 }
467 } else {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800468 if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800469 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800470 if (kQuickDoubleRegAlignedFloatBackFilled) {
471 // Double should not overlap with float.
472 // For example, if fpr_index_ = 3, fpr_double_index_ should be at least 4.
473 fpr_double_index_ = std::max(fpr_double_index_, RoundUp(fpr_index_, 2));
474 // Float should not overlap with double.
475 if (fpr_index_ % 2 == 0) {
476 fpr_index_ = std::max(fpr_double_index_, fpr_index_);
477 }
478 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800479 }
480 }
481 break;
482 case Primitive::kPrimDouble:
483 case Primitive::kPrimLong:
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800484 if (kQuickSoftFloatAbi || (cur_type_ == Primitive::kPrimLong)) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000485 if (cur_type_ == Primitive::kPrimLong && kAlignPairRegister && gpr_index_ == 0) {
486 // Currently, this is only for ARM, where the first available parameter register
487 // is R1. So we skip it, and use R2 instead.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800488 IncGprIndex();
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000489 }
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000490 is_split_long_or_double_ = (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) &&
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800491 ((gpr_index_ + 1) == kNumQuickGprArgs);
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500492 if (!kSplitPairAcrossRegisterAndStack && is_split_long_or_double_) {
493 // We don't want to split this. Pass over this register.
494 gpr_index_++;
495 is_split_long_or_double_ = false;
496 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800497 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800498 if (kBytesStackArgLocation == 4) {
499 stack_index_+= 2;
500 } else {
501 CHECK_EQ(kBytesStackArgLocation, 8U);
502 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800503 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700504 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800505 IncGprIndex();
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000506 if (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700507 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800508 IncGprIndex();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700509 }
510 }
511 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800512 } else {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000513 is_split_long_or_double_ = (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) &&
Zheng Xu5667fdb2014-10-23 18:29:55 +0800514 ((fpr_index_ + 1) == kNumQuickFprArgs) && !kQuickDoubleRegAlignedFloatBackFilled;
Ian Rogers936b37f2014-02-14 00:52:24 -0800515 Visit();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700516 if (kBytesStackArgLocation == 4) {
517 stack_index_+= 2;
Ian Rogers936b37f2014-02-14 00:52:24 -0800518 } else {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700519 CHECK_EQ(kBytesStackArgLocation, 8U);
520 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800521 }
Zheng Xu5667fdb2014-10-23 18:29:55 +0800522 if (kQuickDoubleRegAlignedFloatBackFilled) {
523 if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) {
524 fpr_double_index_ += 2;
525 // Float should not overlap with double.
526 if (fpr_index_ % 2 == 0) {
527 fpr_index_ = std::max(fpr_double_index_, fpr_index_);
528 }
529 }
530 } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800531 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800532 if (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) {
533 if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800534 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800535 }
536 }
537 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800538 }
539 break;
540 default:
541 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty_;
542 }
Ian Rogers848871b2013-08-05 10:56:33 -0700543 }
544 }
545
Andreas Gampec200a4a2014-06-16 18:39:09 -0700546 protected:
Ian Rogers848871b2013-08-05 10:56:33 -0700547 const bool is_static_;
548 const char* const shorty_;
549 const uint32_t shorty_len_;
Andreas Gampec200a4a2014-06-16 18:39:09 -0700550
551 private:
Ian Rogers13735952014-10-08 12:43:28 -0700552 uint8_t* const gpr_args_; // Address of GPR arguments in callee save frame.
553 uint8_t* const fpr_args_; // Address of FPR arguments in callee save frame.
554 uint8_t* const stack_args_; // Address of stack arguments in caller's frame.
Ian Rogers936b37f2014-02-14 00:52:24 -0800555 uint32_t gpr_index_; // Index into spilled GPRs.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800556 // Index into spilled FPRs.
557 // In case kQuickDoubleRegAlignedFloatBackFilled, it may index a hole while fpr_double_index_
558 // holds a higher register number.
559 uint32_t fpr_index_;
560 // Index into spilled FPRs for aligned double.
561 // Only used when kQuickDoubleRegAlignedFloatBackFilled. Next available double register indexed in
562 // terms of singles, may be behind fpr_index.
563 uint32_t fpr_double_index_;
Ian Rogers936b37f2014-02-14 00:52:24 -0800564 uint32_t stack_index_; // Index into arguments on the stack.
565 // The current type of argument during VisitArguments.
566 Primitive::Type cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700567 // Does a 64bit parameter straddle the register and stack arguments?
568 bool is_split_long_or_double_;
569};
570
Sebastien Hertza836bc92014-11-25 16:30:53 +0100571// Returns the 'this' object of a proxy method. This function is only used by StackVisitor. It
572// allows to use the QuickArgumentVisitor constants without moving all the code in its own module.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700573extern "C" mirror::Object* artQuickGetProxyThisObject(ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -0700574 SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertza836bc92014-11-25 16:30:53 +0100575 return QuickArgumentVisitor::GetProxyThisObject(sp);
576}
577
Ian Rogers848871b2013-08-05 10:56:33 -0700578// Visits arguments on the stack placing them into the shadow frame.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800579class BuildQuickShadowFrameVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700580 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700581 BuildQuickShadowFrameVisitor(ArtMethod** sp, bool is_static, const char* shorty,
582 uint32_t shorty_len, ShadowFrame* sf, size_t first_arg_reg) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700583 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), sf_(sf), cur_reg_(first_arg_reg) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700584
Mathieu Chartier90443472015-07-16 20:32:27 -0700585 void Visit() SHARED_REQUIRES(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700586
587 private:
Ian Rogers936b37f2014-02-14 00:52:24 -0800588 ShadowFrame* const sf_;
589 uint32_t cur_reg_;
Ian Rogers848871b2013-08-05 10:56:33 -0700590
Dragos Sbirleabd136a22013-08-13 18:07:04 -0700591 DISALLOW_COPY_AND_ASSIGN(BuildQuickShadowFrameVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700592};
593
Andreas Gampec200a4a2014-06-16 18:39:09 -0700594void BuildQuickShadowFrameVisitor::Visit() {
Ian Rogers9758f792014-03-13 09:02:55 -0700595 Primitive::Type type = GetParamPrimitiveType();
596 switch (type) {
597 case Primitive::kPrimLong: // Fall-through.
598 case Primitive::kPrimDouble:
599 if (IsSplitLongOrDouble()) {
600 sf_->SetVRegLong(cur_reg_, ReadSplitLongParam());
601 } else {
602 sf_->SetVRegLong(cur_reg_, *reinterpret_cast<jlong*>(GetParamAddress()));
603 }
604 ++cur_reg_;
605 break;
606 case Primitive::kPrimNot: {
607 StackReference<mirror::Object>* stack_ref =
608 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
609 sf_->SetVRegReference(cur_reg_, stack_ref->AsMirrorPtr());
610 }
611 break;
612 case Primitive::kPrimBoolean: // Fall-through.
613 case Primitive::kPrimByte: // Fall-through.
614 case Primitive::kPrimChar: // Fall-through.
615 case Primitive::kPrimShort: // Fall-through.
616 case Primitive::kPrimInt: // Fall-through.
617 case Primitive::kPrimFloat:
618 sf_->SetVReg(cur_reg_, *reinterpret_cast<jint*>(GetParamAddress()));
619 break;
620 case Primitive::kPrimVoid:
621 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700622 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -0700623 }
624 ++cur_reg_;
625}
626
Mathieu Chartiere401d142015-04-22 13:56:20 -0700627extern "C" uint64_t artQuickToInterpreterBridge(ArtMethod* method, Thread* self, ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -0700628 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers848871b2013-08-05 10:56:33 -0700629 // Ensure we don't get thread suspension until the object arguments are safely in the shadow
630 // frame.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700631 ScopedQuickEntrypointChecks sqec(self);
Ian Rogers848871b2013-08-05 10:56:33 -0700632
633 if (method->IsAbstract()) {
634 ThrowAbstractMethodError(method);
635 return 0;
636 } else {
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800637 DCHECK(!method->IsNative()) << PrettyMethod(method);
Andreas Gampec200a4a2014-06-16 18:39:09 -0700638 const char* old_cause = self->StartAssertNoThreadSuspension(
639 "Building interpreter shadow frame");
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700640 const DexFile::CodeItem* code_item = method->GetCodeItem();
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800641 DCHECK(code_item != nullptr) << PrettyMethod(method);
Ian Rogers848871b2013-08-05 10:56:33 -0700642 uint16_t num_regs = code_item->registers_size_;
643 void* memory = alloca(ShadowFrame::ComputeSize(num_regs));
Andreas Gampec200a4a2014-06-16 18:39:09 -0700644 // No last shadow coming from quick.
645 ShadowFrame* shadow_frame(ShadowFrame::Create(num_regs, nullptr, method, 0, memory));
Ian Rogers848871b2013-08-05 10:56:33 -0700646 size_t first_arg_reg = code_item->registers_size_ - code_item->ins_size_;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700647 uint32_t shorty_len = 0;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700648 auto* non_proxy_method = method->GetInterfaceMethodIfProxy(sizeof(void*));
649 const char* shorty = non_proxy_method->GetShorty(&shorty_len);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700650 BuildQuickShadowFrameVisitor shadow_frame_builder(sp, method->IsStatic(), shorty, shorty_len,
Ian Rogers936b37f2014-02-14 00:52:24 -0800651 shadow_frame, first_arg_reg);
Ian Rogers848871b2013-08-05 10:56:33 -0700652 shadow_frame_builder.VisitArguments();
Ian Rogerse94652f2014-12-02 11:13:19 -0800653 const bool needs_initialization =
654 method->IsStatic() && !method->GetDeclaringClass()->IsInitialized();
Ian Rogers848871b2013-08-05 10:56:33 -0700655 // Push a transition back into managed code onto the linked list in thread.
656 ManagedStack fragment;
657 self->PushManagedStackFragment(&fragment);
658 self->PushShadowFrame(shadow_frame);
659 self->EndAssertNoThreadSuspension(old_cause);
660
Ian Rogerse94652f2014-12-02 11:13:19 -0800661 if (needs_initialization) {
Ian Rogers848871b2013-08-05 10:56:33 -0700662 // Ensure static method's class is initialized.
Ian Rogerse94652f2014-12-02 11:13:19 -0800663 StackHandleScope<1> hs(self);
664 Handle<mirror::Class> h_class(hs.NewHandle(shadow_frame->GetMethod()->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700665 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
Ian Rogerse94652f2014-12-02 11:13:19 -0800666 DCHECK(Thread::Current()->IsExceptionPending()) << PrettyMethod(shadow_frame->GetMethod());
Ian Rogers848871b2013-08-05 10:56:33 -0700667 self->PopManagedStackFragment(fragment);
668 return 0;
669 }
670 }
Ian Rogerse94652f2014-12-02 11:13:19 -0800671 JValue result = interpreter::EnterInterpreterFromEntryPoint(self, code_item, shadow_frame);
Ian Rogers848871b2013-08-05 10:56:33 -0700672 // Pop transition.
673 self->PopManagedStackFragment(fragment);
Daniel Mihalyieb076692014-08-22 17:33:31 +0200674
675 // Request a stack deoptimization if needed
Mathieu Chartiere401d142015-04-22 13:56:20 -0700676 ArtMethod* caller = QuickArgumentVisitor::GetCallingMethod(sp);
Daniel Mihalyieb076692014-08-22 17:33:31 +0200677 if (UNLIKELY(Dbg::IsForcedInterpreterNeededForUpcall(self, caller))) {
678 self->SetException(Thread::GetDeoptimizationException());
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700679 self->SetDeoptimizationReturnValue(result, shorty[0] == 'L');
Daniel Mihalyieb076692014-08-22 17:33:31 +0200680 }
681
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800682 // No need to restore the args since the method has already been run by the interpreter.
Ian Rogers848871b2013-08-05 10:56:33 -0700683 return result.GetJ();
684 }
685}
686
687// Visits arguments on the stack placing them into the args vector, Object* arguments are converted
688// to jobjects.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800689class BuildQuickArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700690 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700691 BuildQuickArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty, uint32_t shorty_len,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700692 ScopedObjectAccessUnchecked* soa, std::vector<jvalue>* args) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700693 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa), args_(args) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700694
Mathieu Chartier90443472015-07-16 20:32:27 -0700695 void Visit() SHARED_REQUIRES(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700696
Mathieu Chartier90443472015-07-16 20:32:27 -0700697 void FixupReferences() SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800698
Ian Rogers848871b2013-08-05 10:56:33 -0700699 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700700 ScopedObjectAccessUnchecked* const soa_;
701 std::vector<jvalue>* const args_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800702 // References which we must update when exiting in case the GC moved the objects.
Ian Rogers700a4022014-05-19 16:49:03 -0700703 std::vector<std::pair<jobject, StackReference<mirror::Object>*>> references_;
Ian Rogers9758f792014-03-13 09:02:55 -0700704
Ian Rogers848871b2013-08-05 10:56:33 -0700705 DISALLOW_COPY_AND_ASSIGN(BuildQuickArgumentVisitor);
706};
707
Ian Rogers9758f792014-03-13 09:02:55 -0700708void BuildQuickArgumentVisitor::Visit() {
709 jvalue val;
710 Primitive::Type type = GetParamPrimitiveType();
711 switch (type) {
712 case Primitive::kPrimNot: {
713 StackReference<mirror::Object>* stack_ref =
714 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
715 val.l = soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
716 references_.push_back(std::make_pair(val.l, stack_ref));
717 break;
718 }
719 case Primitive::kPrimLong: // Fall-through.
720 case Primitive::kPrimDouble:
721 if (IsSplitLongOrDouble()) {
722 val.j = ReadSplitLongParam();
723 } else {
724 val.j = *reinterpret_cast<jlong*>(GetParamAddress());
725 }
726 break;
727 case Primitive::kPrimBoolean: // Fall-through.
728 case Primitive::kPrimByte: // Fall-through.
729 case Primitive::kPrimChar: // Fall-through.
730 case Primitive::kPrimShort: // Fall-through.
731 case Primitive::kPrimInt: // Fall-through.
732 case Primitive::kPrimFloat:
733 val.i = *reinterpret_cast<jint*>(GetParamAddress());
734 break;
735 case Primitive::kPrimVoid:
736 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700737 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -0700738 }
739 args_->push_back(val);
740}
741
742void BuildQuickArgumentVisitor::FixupReferences() {
743 // Fixup any references which may have changed.
744 for (const auto& pair : references_) {
745 pair.second->Assign(soa_->Decode<mirror::Object*>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -0700746 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -0700747 }
748}
749
Ian Rogers848871b2013-08-05 10:56:33 -0700750// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
751// which is responsible for recording callee save registers. We explicitly place into jobjects the
752// incoming reference arguments (so they survive GC). We invoke the invocation handler, which is a
753// field within the proxy object, which will box the primitive arguments and deal with error cases.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700754extern "C" uint64_t artQuickProxyInvokeHandler(
755 ArtMethod* proxy_method, mirror::Object* receiver, Thread* self, ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -0700756 SHARED_REQUIRES(Locks::mutator_lock_) {
Brian Carlstromd3633d52013-08-20 21:06:26 -0700757 DCHECK(proxy_method->IsProxyMethod()) << PrettyMethod(proxy_method);
758 DCHECK(receiver->GetClass()->IsProxyClass()) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700759 // Ensure we don't get thread suspension until the object arguments are safely in jobjects.
760 const char* old_cause =
761 self->StartAssertNoThreadSuspension("Adding to IRT proxy object arguments");
762 // Register the top of the managed stack, making stack crawlable.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700763 DCHECK_EQ((*sp), proxy_method) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700764 DCHECK_EQ(proxy_method->GetFrameSizeInBytes(),
Brian Carlstromd3633d52013-08-20 21:06:26 -0700765 Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes())
766 << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700767 self->VerifyStack();
768 // Start new JNI local reference state.
769 JNIEnvExt* env = self->GetJniEnv();
770 ScopedObjectAccessUnchecked soa(env);
771 ScopedJniEnvLocalRefState env_state(env);
772 // Create local ref. copies of proxy method and the receiver.
773 jobject rcvr_jobj = soa.AddLocalReference<jobject>(receiver);
774
775 // Placing arguments into args vector and remove the receiver.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700776 ArtMethod* non_proxy_method = proxy_method->GetInterfaceMethodIfProxy(sizeof(void*));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700777 CHECK(!non_proxy_method->IsStatic()) << PrettyMethod(proxy_method) << " "
Andreas Gampec200a4a2014-06-16 18:39:09 -0700778 << PrettyMethod(non_proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700779 std::vector<jvalue> args;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700780 uint32_t shorty_len = 0;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700781 const char* shorty = non_proxy_method->GetShorty(&shorty_len);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700782 BuildQuickArgumentVisitor local_ref_visitor(sp, false, shorty, shorty_len, &soa, &args);
Brian Carlstromd3633d52013-08-20 21:06:26 -0700783
Ian Rogers848871b2013-08-05 10:56:33 -0700784 local_ref_visitor.VisitArguments();
Brian Carlstromd3633d52013-08-20 21:06:26 -0700785 DCHECK_GT(args.size(), 0U) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700786 args.erase(args.begin());
787
788 // Convert proxy method into expected interface method.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700789 ArtMethod* interface_method = proxy_method->FindOverriddenMethod(sizeof(void*));
Ian Rogerse0a02da2014-12-02 14:10:53 -0800790 DCHECK(interface_method != nullptr) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700791 DCHECK(!interface_method->IsProxyMethod()) << PrettyMethod(interface_method);
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700792 self->EndAssertNoThreadSuspension(old_cause);
793 jobject interface_method_jobj = soa.AddLocalReference<jobject>(
794 mirror::Method::CreateFromArtMethod(soa.Self(), interface_method));
Ian Rogers848871b2013-08-05 10:56:33 -0700795
796 // All naked Object*s should now be in jobjects, so its safe to go into the main invoke code
797 // that performs allocations.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700798 JValue result = InvokeProxyInvocationHandler(soa, shorty, rcvr_jobj, interface_method_jobj, args);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800799 // Restore references which might have moved.
800 local_ref_visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -0700801 return result.GetJ();
802}
803
804// Read object references held in arguments from quick frames and place in a JNI local references,
805// so they don't get garbage collected.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800806class RememberForGcArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700807 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700808 RememberForGcArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty,
809 uint32_t shorty_len, ScopedObjectAccessUnchecked* soa) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700810 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700811
Mathieu Chartier90443472015-07-16 20:32:27 -0700812 void Visit() SHARED_REQUIRES(Locks::mutator_lock_) OVERRIDE;
Mathieu Chartier07d447b2013-09-26 11:57:43 -0700813
Mathieu Chartier90443472015-07-16 20:32:27 -0700814 void FixupReferences() SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers848871b2013-08-05 10:56:33 -0700815
816 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700817 ScopedObjectAccessUnchecked* const soa_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800818 // References which we must update when exiting in case the GC moved the objects.
Andreas Gampec200a4a2014-06-16 18:39:09 -0700819 std::vector<std::pair<jobject, StackReference<mirror::Object>*> > references_;
820
Mathieu Chartier590fee92013-09-13 13:46:47 -0700821 DISALLOW_COPY_AND_ASSIGN(RememberForGcArgumentVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700822};
823
Ian Rogers9758f792014-03-13 09:02:55 -0700824void RememberForGcArgumentVisitor::Visit() {
825 if (IsParamAReference()) {
826 StackReference<mirror::Object>* stack_ref =
827 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
828 jobject reference =
829 soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
830 references_.push_back(std::make_pair(reference, stack_ref));
831 }
832}
833
834void RememberForGcArgumentVisitor::FixupReferences() {
835 // Fixup any references which may have changed.
836 for (const auto& pair : references_) {
837 pair.second->Assign(soa_->Decode<mirror::Object*>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -0700838 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -0700839 }
840}
841
Ian Rogers848871b2013-08-05 10:56:33 -0700842// Lazily resolve a method for quick. Called by stub code.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700843extern "C" const void* artQuickResolutionTrampoline(
844 ArtMethod* called, mirror::Object* receiver, Thread* self, ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -0700845 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe3b45ef22015-05-26 21:34:09 -0700846 // The resolution trampoline stashes the resolved method into the callee-save frame to transport
847 // it. Thus, when exiting, the stack cannot be verified (as the resolved method most likely
848 // does not have the same stack layout as the callee-save method).
849 ScopedQuickEntrypointChecks sqec(self, kIsDebugBuild, false);
Ian Rogers848871b2013-08-05 10:56:33 -0700850 // Start new JNI local reference state
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800851 JNIEnvExt* env = self->GetJniEnv();
Ian Rogers848871b2013-08-05 10:56:33 -0700852 ScopedObjectAccessUnchecked soa(env);
853 ScopedJniEnvLocalRefState env_state(env);
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800854 const char* old_cause = self->StartAssertNoThreadSuspension("Quick method resolution set up");
Ian Rogers848871b2013-08-05 10:56:33 -0700855
856 // Compute details about the called method (avoid GCs)
857 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Ian Rogers848871b2013-08-05 10:56:33 -0700858 InvokeType invoke_type;
Ian Rogerse0a02da2014-12-02 14:10:53 -0800859 MethodReference called_method(nullptr, 0);
860 const bool called_method_known_on_entry = !called->IsRuntimeMethod();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700861 ArtMethod* caller = nullptr;
Ian Rogerse0a02da2014-12-02 14:10:53 -0800862 if (!called_method_known_on_entry) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +0100863 caller = QuickArgumentVisitor::GetCallingMethod(sp);
864 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
Ian Rogers848871b2013-08-05 10:56:33 -0700865 const DexFile::CodeItem* code;
Ian Rogerse0a02da2014-12-02 14:10:53 -0800866 called_method.dex_file = caller->GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700867 code = caller->GetCodeItem();
Ian Rogers848871b2013-08-05 10:56:33 -0700868 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
869 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
870 Instruction::Code instr_code = instr->Opcode();
871 bool is_range;
872 switch (instr_code) {
873 case Instruction::INVOKE_DIRECT:
874 invoke_type = kDirect;
875 is_range = false;
876 break;
877 case Instruction::INVOKE_DIRECT_RANGE:
878 invoke_type = kDirect;
879 is_range = true;
880 break;
881 case Instruction::INVOKE_STATIC:
882 invoke_type = kStatic;
883 is_range = false;
884 break;
885 case Instruction::INVOKE_STATIC_RANGE:
886 invoke_type = kStatic;
887 is_range = true;
888 break;
889 case Instruction::INVOKE_SUPER:
890 invoke_type = kSuper;
891 is_range = false;
892 break;
893 case Instruction::INVOKE_SUPER_RANGE:
894 invoke_type = kSuper;
895 is_range = true;
896 break;
897 case Instruction::INVOKE_VIRTUAL:
898 invoke_type = kVirtual;
899 is_range = false;
900 break;
901 case Instruction::INVOKE_VIRTUAL_RANGE:
902 invoke_type = kVirtual;
903 is_range = true;
904 break;
905 case Instruction::INVOKE_INTERFACE:
906 invoke_type = kInterface;
907 is_range = false;
908 break;
909 case Instruction::INVOKE_INTERFACE_RANGE:
910 invoke_type = kInterface;
911 is_range = true;
912 break;
913 default:
Ian Rogerse0a02da2014-12-02 14:10:53 -0800914 LOG(FATAL) << "Unexpected call into trampoline: " << instr->DumpString(nullptr);
915 UNREACHABLE();
Ian Rogers848871b2013-08-05 10:56:33 -0700916 }
Ian Rogerse0a02da2014-12-02 14:10:53 -0800917 called_method.dex_method_index = (is_range) ? instr->VRegB_3rc() : instr->VRegB_35c();
Ian Rogers848871b2013-08-05 10:56:33 -0700918 } else {
919 invoke_type = kStatic;
Ian Rogerse0a02da2014-12-02 14:10:53 -0800920 called_method.dex_file = called->GetDexFile();
921 called_method.dex_method_index = called->GetDexMethodIndex();
Ian Rogers848871b2013-08-05 10:56:33 -0700922 }
923 uint32_t shorty_len;
924 const char* shorty =
Ian Rogerse0a02da2014-12-02 14:10:53 -0800925 called_method.dex_file->GetMethodShorty(
926 called_method.dex_file->GetMethodId(called_method.dex_method_index), &shorty_len);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700927 RememberForGcArgumentVisitor visitor(sp, invoke_type == kStatic, shorty, shorty_len, &soa);
Ian Rogers848871b2013-08-05 10:56:33 -0700928 visitor.VisitArguments();
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800929 self->EndAssertNoThreadSuspension(old_cause);
Ian Rogerse0a02da2014-12-02 14:10:53 -0800930 const bool virtual_or_interface = invoke_type == kVirtual || invoke_type == kInterface;
Ian Rogers848871b2013-08-05 10:56:33 -0700931 // Resolve method filling in dex cache.
Ian Rogerse0a02da2014-12-02 14:10:53 -0800932 if (!called_method_known_on_entry) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700933 StackHandleScope<1> hs(self);
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700934 mirror::Object* dummy = nullptr;
935 HandleWrapper<mirror::Object> h_receiver(
936 hs.NewHandleWrapper(virtual_or_interface ? &receiver : &dummy));
Ian Rogerse0a02da2014-12-02 14:10:53 -0800937 DCHECK_EQ(caller->GetDexFile(), called_method.dex_file);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700938 called = linker->ResolveMethod(self, called_method.dex_method_index, caller, invoke_type);
Ian Rogers848871b2013-08-05 10:56:33 -0700939 }
Ian Rogerse0a02da2014-12-02 14:10:53 -0800940 const void* code = nullptr;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800941 if (LIKELY(!self->IsExceptionPending())) {
Ian Rogers848871b2013-08-05 10:56:33 -0700942 // Incompatible class change should have been handled in resolve method.
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800943 CHECK(!called->CheckIncompatibleClassChange(invoke_type))
944 << PrettyMethod(called) << " " << invoke_type;
Mathieu Chartier55871bf2014-02-27 10:24:50 -0800945 if (virtual_or_interface) {
946 // Refine called method based on receiver.
947 CHECK(receiver != nullptr) << invoke_type;
Mingyao Yangf4867782014-05-05 11:55:02 -0700948
Mathieu Chartiere401d142015-04-22 13:56:20 -0700949 ArtMethod* orig_called = called;
Mathieu Chartier55871bf2014-02-27 10:24:50 -0800950 if (invoke_type == kVirtual) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700951 called = receiver->GetClass()->FindVirtualMethodForVirtual(called, sizeof(void*));
Mathieu Chartier55871bf2014-02-27 10:24:50 -0800952 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700953 called = receiver->GetClass()->FindVirtualMethodForInterface(called, sizeof(void*));
Mathieu Chartier55871bf2014-02-27 10:24:50 -0800954 }
Mingyao Yangf4867782014-05-05 11:55:02 -0700955
956 CHECK(called != nullptr) << PrettyMethod(orig_called) << " "
957 << PrettyTypeOf(receiver) << " "
958 << invoke_type << " " << orig_called->GetVtableIndex();
959
Ian Rogers83883d72013-10-21 21:07:24 -0700960 // 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 -0800961 // of the sharpened method avoiding dirtying the dex cache if possible.
Ian Rogers00f15272014-12-02 16:55:46 -0800962 // Note, called_method.dex_method_index references the dex method before the
963 // FindVirtualMethodFor... This is ok for FindDexMethodIndexInOtherDexFile that only cares
964 // about the name and signature.
965 uint32_t update_dex_cache_method_index = called->GetDexMethodIndex();
Ian Rogerse0a02da2014-12-02 14:10:53 -0800966 if (!called->HasSameDexCacheResolvedMethods(caller)) {
Ian Rogers83883d72013-10-21 21:07:24 -0700967 // Calling from one dex file to another, need to compute the method index appropriate to
Vladimir Markobbcc0c02014-02-03 14:08:42 +0000968 // the caller's dex file. Since we get here only if the original called was a runtime
969 // method, we've got the correct dex_file and a dex_method_idx from above.
Ian Rogerse0a02da2014-12-02 14:10:53 -0800970 DCHECK(!called_method_known_on_entry);
971 DCHECK_EQ(caller->GetDexFile(), called_method.dex_file);
972 const DexFile* caller_dex_file = called_method.dex_file;
973 uint32_t caller_method_name_and_sig_index = called_method.dex_method_index;
974 update_dex_cache_method_index =
975 called->FindDexMethodIndexInOtherDexFile(*caller_dex_file,
976 caller_method_name_and_sig_index);
977 }
978 if ((update_dex_cache_method_index != DexFile::kDexNoIndex) &&
Mathieu Chartiere401d142015-04-22 13:56:20 -0700979 (caller->GetDexCacheResolvedMethod(
980 update_dex_cache_method_index, sizeof(void*)) != called)) {
981 caller->SetDexCacheResolvedMethod(update_dex_cache_method_index, called, sizeof(void*));
Ian Rogers83883d72013-10-21 21:07:24 -0700982 }
Mathieu Chartiere4a91bb2015-01-28 13:11:44 -0800983 } else if (invoke_type == kStatic) {
984 const auto called_dex_method_idx = called->GetDexMethodIndex();
985 // For static invokes, we may dispatch to the static method in the superclass but resolve
986 // using the subclass. To prevent getting slow paths on each invoke, we force set the
987 // resolved method for the super class dex method index if we are in the same dex file.
988 // b/19175856
989 if (called->GetDexFile() == called_method.dex_file &&
990 called_method.dex_method_index != called_dex_method_idx) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700991 called->GetDexCache()->SetResolvedMethod(called_dex_method_idx, called, sizeof(void*));
Mathieu Chartiere4a91bb2015-01-28 13:11:44 -0800992 }
Ian Rogers83883d72013-10-21 21:07:24 -0700993 }
Daniel Mihalyieb076692014-08-22 17:33:31 +0200994
Ian Rogers848871b2013-08-05 10:56:33 -0700995 // Ensure that the called method's class is initialized.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700996 StackHandleScope<1> hs(soa.Self());
997 Handle<mirror::Class> called_class(hs.NewHandle(called->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700998 linker->EnsureInitialized(soa.Self(), called_class, true, true);
Ian Rogers848871b2013-08-05 10:56:33 -0700999 if (LIKELY(called_class->IsInitialized())) {
Daniel Mihalyieb076692014-08-22 17:33:31 +02001000 if (UNLIKELY(Dbg::IsForcedInterpreterNeededForResolution(self, called))) {
1001 // If we are single-stepping or the called method is deoptimized (by a
1002 // breakpoint, for example), then we have to execute the called method
1003 // with the interpreter.
1004 code = GetQuickToInterpreterBridge();
1005 } else if (UNLIKELY(Dbg::IsForcedInstrumentationNeededForResolution(self, caller))) {
1006 // If the caller is deoptimized (by a breakpoint, for example), we have to
1007 // continue its execution with interpreter when returning from the called
1008 // method. Because we do not want to execute the called method with the
1009 // interpreter, we wrap its execution into the instrumentation stubs.
1010 // When the called method returns, it will execute the instrumentation
1011 // exit hook that will determine the need of the interpreter with a call
1012 // to Dbg::IsForcedInterpreterNeededForUpcall and deoptimize the stack if
1013 // it is needed.
1014 code = GetQuickInstrumentationEntryPoint();
1015 } else {
1016 code = called->GetEntryPointFromQuickCompiledCode();
1017 }
Ian Rogers848871b2013-08-05 10:56:33 -07001018 } else if (called_class->IsInitializing()) {
Daniel Mihalyieb076692014-08-22 17:33:31 +02001019 if (UNLIKELY(Dbg::IsForcedInterpreterNeededForResolution(self, called))) {
1020 // If we are single-stepping or the called method is deoptimized (by a
1021 // breakpoint, for example), then we have to execute the called method
1022 // with the interpreter.
1023 code = GetQuickToInterpreterBridge();
1024 } else if (invoke_type == kStatic) {
Ian Rogers848871b2013-08-05 10:56:33 -07001025 // Class is still initializing, go to oat and grab code (trampoline must be left in place
1026 // until class is initialized to stop races between threads).
Ian Rogersef7d42f2014-01-06 12:55:46 -08001027 code = linker->GetQuickOatCodeFor(called);
Ian Rogers848871b2013-08-05 10:56:33 -07001028 } else {
1029 // No trampoline for non-static methods.
Ian Rogersef7d42f2014-01-06 12:55:46 -08001030 code = called->GetEntryPointFromQuickCompiledCode();
Ian Rogers848871b2013-08-05 10:56:33 -07001031 }
1032 } else {
1033 DCHECK(called_class->IsErroneous());
1034 }
1035 }
Ian Rogerse0a02da2014-12-02 14:10:53 -08001036 CHECK_EQ(code == nullptr, self->IsExceptionPending());
Mathieu Chartier07d447b2013-09-26 11:57:43 -07001037 // Fixup any locally saved objects may have moved during a GC.
1038 visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -07001039 // Place called method in callee-save frame to be placed as first argument to quick method.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001040 *sp = called;
1041
Ian Rogers848871b2013-08-05 10:56:33 -07001042 return code;
1043}
1044
Andreas Gampec147b002014-03-06 18:11:06 -08001045/*
1046 * This class uses a couple of observations to unite the different calling conventions through
1047 * a few constants.
1048 *
1049 * 1) Number of registers used for passing is normally even, so counting down has no penalty for
1050 * possible alignment.
1051 * 2) Known 64b architectures store 8B units on the stack, both for integral and floating point
1052 * types, so using uintptr_t is OK. Also means that we can use kRegistersNeededX to denote
1053 * when we have to split things
1054 * 3) The only soft-float, Arm, is 32b, so no widening needs to be taken into account for floats
1055 * and we can use Int handling directly.
1056 * 4) Only 64b architectures widen, and their stack is aligned 8B anyways, so no padding code
1057 * necessary when widening. Also, widening of Ints will take place implicitly, and the
1058 * extension should be compatible with Aarch64, which mandates copying the available bits
1059 * into LSB and leaving the rest unspecified.
1060 * 5) Aligning longs and doubles is necessary on arm only, and it's the same in registers and on
1061 * the stack.
1062 * 6) There is only little endian.
1063 *
1064 *
1065 * Actual work is supposed to be done in a delegate of the template type. The interface is as
1066 * follows:
1067 *
1068 * void PushGpr(uintptr_t): Add a value for the next GPR
1069 *
1070 * void PushFpr4(float): Add a value for the next FPR of size 32b. Is only called if we need
1071 * padding, that is, think the architecture is 32b and aligns 64b.
1072 *
1073 * void PushFpr8(uint64_t): Push a double. We _will_ call this on 32b, it's the callee's job to
1074 * split this if necessary. The current state will have aligned, if
1075 * necessary.
1076 *
1077 * void PushStack(uintptr_t): Push a value to the stack.
1078 *
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001079 * uintptr_t PushHandleScope(mirror::Object* ref): Add a reference to the HandleScope. This _will_ have nullptr,
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001080 * as this might be important for null initialization.
Andreas Gampec147b002014-03-06 18:11:06 -08001081 * Must return the jobject, that is, the reference to the
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001082 * entry in the HandleScope (nullptr if necessary).
Andreas Gampec147b002014-03-06 18:11:06 -08001083 *
1084 */
Andreas Gampec200a4a2014-06-16 18:39:09 -07001085template<class T> class BuildNativeCallFrameStateMachine {
Andreas Gampec147b002014-03-06 18:11:06 -08001086 public:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001087#if defined(__arm__)
1088 // TODO: These are all dummy values!
Andreas Gampec147b002014-03-06 18:11:06 -08001089 static constexpr bool kNativeSoftFloatAbi = true;
1090 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs, r0-r3
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001091 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
1092
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001093 static constexpr size_t kRegistersNeededForLong = 2;
1094 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -08001095 static constexpr bool kMultiRegistersAligned = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001096 static constexpr bool kMultiFPRegistersWidened = false;
1097 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001098 static constexpr bool kAlignLongOnStack = true;
1099 static constexpr bool kAlignDoubleOnStack = true;
Stuart Monteithb95a5342014-03-12 13:32:32 +00001100#elif defined(__aarch64__)
1101 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
1102 static constexpr size_t kNumNativeGprArgs = 8; // 6 arguments passed in GPRs.
1103 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
1104
1105 static constexpr size_t kRegistersNeededForLong = 1;
1106 static constexpr size_t kRegistersNeededForDouble = 1;
1107 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001108 static constexpr bool kMultiFPRegistersWidened = false;
1109 static constexpr bool kMultiGPRegistersWidened = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +00001110 static constexpr bool kAlignLongOnStack = false;
1111 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001112#elif defined(__mips__) && !defined(__LP64__)
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001113 static constexpr bool kNativeSoftFloatAbi = true; // This is a hard float ABI.
Douglas Leung735b8552014-10-31 12:21:40 -07001114 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs.
1115 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001116
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001117 static constexpr size_t kRegistersNeededForLong = 2;
1118 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -08001119 static constexpr bool kMultiRegistersAligned = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001120 static constexpr bool kMultiFPRegistersWidened = true;
1121 static constexpr bool kMultiGPRegistersWidened = false;
Douglas Leung735b8552014-10-31 12:21:40 -07001122 static constexpr bool kAlignLongOnStack = true;
1123 static constexpr bool kAlignDoubleOnStack = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001124#elif defined(__mips__) && defined(__LP64__)
1125 // Let the code prepare GPRs only and we will load the FPRs with same data.
1126 static constexpr bool kNativeSoftFloatAbi = true;
1127 static constexpr size_t kNumNativeGprArgs = 8;
1128 static constexpr size_t kNumNativeFprArgs = 0;
1129
1130 static constexpr size_t kRegistersNeededForLong = 1;
1131 static constexpr size_t kRegistersNeededForDouble = 1;
1132 static constexpr bool kMultiRegistersAligned = false;
1133 static constexpr bool kMultiFPRegistersWidened = false;
1134 static constexpr bool kMultiGPRegistersWidened = true;
1135 static constexpr bool kAlignLongOnStack = false;
1136 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001137#elif defined(__i386__)
1138 // TODO: Check these!
Andreas Gampec147b002014-03-06 18:11:06 -08001139 static constexpr bool kNativeSoftFloatAbi = false; // Not using int registers for fp
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001140 static constexpr size_t kNumNativeGprArgs = 0; // 6 arguments passed in GPRs.
1141 static constexpr size_t kNumNativeFprArgs = 0; // 8 arguments passed in FPRs.
1142
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001143 static constexpr size_t kRegistersNeededForLong = 2;
1144 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001145 static constexpr bool kMultiRegistersAligned = false; // x86 not using regs, anyways
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001146 static constexpr bool kMultiFPRegistersWidened = false;
1147 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001148 static constexpr bool kAlignLongOnStack = false;
1149 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001150#elif defined(__x86_64__)
1151 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
1152 static constexpr size_t kNumNativeGprArgs = 6; // 6 arguments passed in GPRs.
1153 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
1154
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001155 static constexpr size_t kRegistersNeededForLong = 1;
1156 static constexpr size_t kRegistersNeededForDouble = 1;
Andreas Gampec147b002014-03-06 18:11:06 -08001157 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001158 static constexpr bool kMultiFPRegistersWidened = false;
1159 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001160 static constexpr bool kAlignLongOnStack = false;
1161 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001162#else
1163#error "Unsupported architecture"
1164#endif
1165
Andreas Gampec147b002014-03-06 18:11:06 -08001166 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001167 explicit BuildNativeCallFrameStateMachine(T* delegate)
1168 : gpr_index_(kNumNativeGprArgs),
1169 fpr_index_(kNumNativeFprArgs),
1170 stack_entries_(0),
1171 delegate_(delegate) {
Andreas Gampec147b002014-03-06 18:11:06 -08001172 // For register alignment, we want to assume that counters (gpr_index_, fpr_index_) are even iff
1173 // the next register is even; counting down is just to make the compiler happy...
Andreas Gampe575e78c2014-11-03 23:41:03 -08001174 static_assert(kNumNativeGprArgs % 2 == 0U, "Number of native GPR arguments not even");
1175 static_assert(kNumNativeFprArgs % 2 == 0U, "Number of native FPR arguments not even");
Andreas Gampec147b002014-03-06 18:11:06 -08001176 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001177
Andreas Gampec200a4a2014-06-16 18:39:09 -07001178 virtual ~BuildNativeCallFrameStateMachine() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001179
Ian Rogers1428dce2014-10-21 15:02:15 -07001180 bool HavePointerGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001181 return gpr_index_ > 0;
1182 }
1183
Andreas Gampec200a4a2014-06-16 18:39:09 -07001184 void AdvancePointer(const void* val) {
Andreas Gampec147b002014-03-06 18:11:06 -08001185 if (HavePointerGpr()) {
1186 gpr_index_--;
1187 PushGpr(reinterpret_cast<uintptr_t>(val));
1188 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001189 stack_entries_++; // TODO: have a field for pointer length as multiple of 32b
Andreas Gampec147b002014-03-06 18:11:06 -08001190 PushStack(reinterpret_cast<uintptr_t>(val));
1191 gpr_index_ = 0;
1192 }
1193 }
1194
Ian Rogers1428dce2014-10-21 15:02:15 -07001195 bool HaveHandleScopeGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001196 return gpr_index_ > 0;
1197 }
1198
Mathieu Chartier90443472015-07-16 20:32:27 -07001199 void AdvanceHandleScope(mirror::Object* ptr) SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001200 uintptr_t handle = PushHandle(ptr);
1201 if (HaveHandleScopeGpr()) {
Andreas Gampec147b002014-03-06 18:11:06 -08001202 gpr_index_--;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001203 PushGpr(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001204 } else {
1205 stack_entries_++;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001206 PushStack(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001207 gpr_index_ = 0;
1208 }
1209 }
1210
Ian Rogers1428dce2014-10-21 15:02:15 -07001211 bool HaveIntGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001212 return gpr_index_ > 0;
1213 }
1214
1215 void AdvanceInt(uint32_t val) {
1216 if (HaveIntGpr()) {
1217 gpr_index_--;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001218 if (kMultiGPRegistersWidened) {
1219 DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001220 PushGpr(static_cast<int64_t>(bit_cast<int32_t, uint32_t>(val)));
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001221 } else {
1222 PushGpr(val);
1223 }
Andreas Gampec147b002014-03-06 18:11:06 -08001224 } else {
1225 stack_entries_++;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001226 if (kMultiGPRegistersWidened) {
1227 DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001228 PushStack(static_cast<int64_t>(bit_cast<int32_t, uint32_t>(val)));
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001229 } else {
1230 PushStack(val);
1231 }
Andreas Gampec147b002014-03-06 18:11:06 -08001232 gpr_index_ = 0;
1233 }
1234 }
1235
Ian Rogers1428dce2014-10-21 15:02:15 -07001236 bool HaveLongGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001237 return gpr_index_ >= kRegistersNeededForLong + (LongGprNeedsPadding() ? 1 : 0);
1238 }
1239
Ian Rogers1428dce2014-10-21 15:02:15 -07001240 bool LongGprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001241 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1242 kAlignLongOnStack && // and when it needs alignment
1243 (gpr_index_ & 1) == 1; // counter is odd, see constructor
1244 }
1245
Ian Rogers1428dce2014-10-21 15:02:15 -07001246 bool LongStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001247 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1248 kAlignLongOnStack && // and when it needs 8B alignment
1249 (stack_entries_ & 1) == 1; // counter is odd
1250 }
1251
1252 void AdvanceLong(uint64_t val) {
1253 if (HaveLongGpr()) {
1254 if (LongGprNeedsPadding()) {
1255 PushGpr(0);
1256 gpr_index_--;
1257 }
1258 if (kRegistersNeededForLong == 1) {
1259 PushGpr(static_cast<uintptr_t>(val));
1260 } else {
1261 PushGpr(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1262 PushGpr(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1263 }
1264 gpr_index_ -= kRegistersNeededForLong;
1265 } else {
1266 if (LongStackNeedsPadding()) {
1267 PushStack(0);
1268 stack_entries_++;
1269 }
1270 if (kRegistersNeededForLong == 1) {
1271 PushStack(static_cast<uintptr_t>(val));
1272 stack_entries_++;
1273 } else {
1274 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1275 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1276 stack_entries_ += 2;
1277 }
1278 gpr_index_ = 0;
1279 }
1280 }
1281
Ian Rogers1428dce2014-10-21 15:02:15 -07001282 bool HaveFloatFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001283 return fpr_index_ > 0;
1284 }
1285
Andreas Gampec147b002014-03-06 18:11:06 -08001286 void AdvanceFloat(float val) {
1287 if (kNativeSoftFloatAbi) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001288 AdvanceInt(bit_cast<uint32_t, float>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001289 } else {
1290 if (HaveFloatFpr()) {
1291 fpr_index_--;
1292 if (kRegistersNeededForDouble == 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001293 if (kMultiFPRegistersWidened) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001294 PushFpr8(bit_cast<uint64_t, double>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001295 } else {
1296 // No widening, just use the bits.
Roland Levillainda4d79b2015-03-24 14:36:11 +00001297 PushFpr8(static_cast<uint64_t>(bit_cast<uint32_t, float>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001298 }
1299 } else {
1300 PushFpr4(val);
1301 }
1302 } else {
1303 stack_entries_++;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001304 if (kRegistersNeededForDouble == 1 && kMultiFPRegistersWidened) {
Andreas Gampec147b002014-03-06 18:11:06 -08001305 // Need to widen before storing: Note the "double" in the template instantiation.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001306 // Note: We need to jump through those hoops to make the compiler happy.
1307 DCHECK_EQ(sizeof(uintptr_t), sizeof(uint64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001308 PushStack(static_cast<uintptr_t>(bit_cast<uint64_t, double>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001309 } else {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001310 PushStack(static_cast<uintptr_t>(bit_cast<uint32_t, float>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001311 }
1312 fpr_index_ = 0;
1313 }
1314 }
1315 }
1316
Ian Rogers1428dce2014-10-21 15:02:15 -07001317 bool HaveDoubleFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001318 return fpr_index_ >= kRegistersNeededForDouble + (DoubleFprNeedsPadding() ? 1 : 0);
1319 }
1320
Ian Rogers1428dce2014-10-21 15:02:15 -07001321 bool DoubleFprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001322 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1323 kAlignDoubleOnStack && // and when it needs alignment
1324 (fpr_index_ & 1) == 1; // counter is odd, see constructor
1325 }
1326
Ian Rogers1428dce2014-10-21 15:02:15 -07001327 bool DoubleStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001328 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1329 kAlignDoubleOnStack && // and when it needs 8B alignment
1330 (stack_entries_ & 1) == 1; // counter is odd
1331 }
1332
1333 void AdvanceDouble(uint64_t val) {
1334 if (kNativeSoftFloatAbi) {
1335 AdvanceLong(val);
1336 } else {
1337 if (HaveDoubleFpr()) {
1338 if (DoubleFprNeedsPadding()) {
1339 PushFpr4(0);
1340 fpr_index_--;
1341 }
1342 PushFpr8(val);
1343 fpr_index_ -= kRegistersNeededForDouble;
1344 } else {
1345 if (DoubleStackNeedsPadding()) {
1346 PushStack(0);
1347 stack_entries_++;
1348 }
1349 if (kRegistersNeededForDouble == 1) {
1350 PushStack(static_cast<uintptr_t>(val));
1351 stack_entries_++;
1352 } else {
1353 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1354 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1355 stack_entries_ += 2;
1356 }
1357 fpr_index_ = 0;
1358 }
1359 }
1360 }
1361
Ian Rogers1428dce2014-10-21 15:02:15 -07001362 uint32_t GetStackEntries() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001363 return stack_entries_;
1364 }
1365
Ian Rogers1428dce2014-10-21 15:02:15 -07001366 uint32_t GetNumberOfUsedGprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001367 return kNumNativeGprArgs - gpr_index_;
1368 }
1369
Ian Rogers1428dce2014-10-21 15:02:15 -07001370 uint32_t GetNumberOfUsedFprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001371 return kNumNativeFprArgs - fpr_index_;
1372 }
1373
1374 private:
1375 void PushGpr(uintptr_t val) {
1376 delegate_->PushGpr(val);
1377 }
1378 void PushFpr4(float val) {
1379 delegate_->PushFpr4(val);
1380 }
1381 void PushFpr8(uint64_t val) {
1382 delegate_->PushFpr8(val);
1383 }
1384 void PushStack(uintptr_t val) {
1385 delegate_->PushStack(val);
1386 }
Mathieu Chartier90443472015-07-16 20:32:27 -07001387 uintptr_t PushHandle(mirror::Object* ref) SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001388 return delegate_->PushHandle(ref);
Andreas Gampec147b002014-03-06 18:11:06 -08001389 }
1390
1391 uint32_t gpr_index_; // Number of free GPRs
1392 uint32_t fpr_index_; // Number of free FPRs
1393 uint32_t stack_entries_; // Stack entries are in multiples of 32b, as floats are usually not
1394 // extended
Ian Rogers1428dce2014-10-21 15:02:15 -07001395 T* const delegate_; // What Push implementation gets called
Andreas Gampec147b002014-03-06 18:11:06 -08001396};
1397
Andreas Gampec200a4a2014-06-16 18:39:09 -07001398// Computes the sizes of register stacks and call stack area. Handling of references can be extended
1399// in subclasses.
1400//
1401// To handle native pointers, use "L" in the shorty for an object reference, which simulates
1402// them with handles.
1403class ComputeNativeCallFrameSize {
Andreas Gampec147b002014-03-06 18:11:06 -08001404 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001405 ComputeNativeCallFrameSize() : num_stack_entries_(0) {}
1406
1407 virtual ~ComputeNativeCallFrameSize() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001408
Ian Rogers1428dce2014-10-21 15:02:15 -07001409 uint32_t GetStackSize() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001410 return num_stack_entries_ * sizeof(uintptr_t);
1411 }
1412
Ian Rogers1428dce2014-10-21 15:02:15 -07001413 uint8_t* LayoutCallStack(uint8_t* sp8) const {
Andreas Gampec147b002014-03-06 18:11:06 -08001414 sp8 -= GetStackSize();
Andreas Gampe779f8c92014-06-09 18:29:38 -07001415 // Align by kStackAlignment.
1416 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
Andreas Gampec200a4a2014-06-16 18:39:09 -07001417 return sp8;
Andreas Gampec147b002014-03-06 18:11:06 -08001418 }
1419
Ian Rogers1428dce2014-10-21 15:02:15 -07001420 uint8_t* LayoutCallRegisterStacks(uint8_t* sp8, uintptr_t** start_gpr, uint32_t** start_fpr)
1421 const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001422 // Assumption is OK right now, as we have soft-float arm
1423 size_t fregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeFprArgs;
1424 sp8 -= fregs * sizeof(uintptr_t);
1425 *start_fpr = reinterpret_cast<uint32_t*>(sp8);
1426 size_t iregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeGprArgs;
1427 sp8 -= iregs * sizeof(uintptr_t);
1428 *start_gpr = reinterpret_cast<uintptr_t*>(sp8);
1429 return sp8;
1430 }
Andreas Gampec147b002014-03-06 18:11:06 -08001431
Andreas Gampec200a4a2014-06-16 18:39:09 -07001432 uint8_t* LayoutNativeCall(uint8_t* sp8, uintptr_t** start_stack, uintptr_t** start_gpr,
Ian Rogers1428dce2014-10-21 15:02:15 -07001433 uint32_t** start_fpr) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001434 // Native call stack.
1435 sp8 = LayoutCallStack(sp8);
1436 *start_stack = reinterpret_cast<uintptr_t*>(sp8);
Andreas Gampec147b002014-03-06 18:11:06 -08001437
Andreas Gampec200a4a2014-06-16 18:39:09 -07001438 // Put fprs and gprs below.
1439 sp8 = LayoutCallRegisterStacks(sp8, start_gpr, start_fpr);
Andreas Gampec147b002014-03-06 18:11:06 -08001440
Andreas Gampec200a4a2014-06-16 18:39:09 -07001441 // Return the new bottom.
1442 return sp8;
1443 }
1444
1445 virtual void WalkHeader(BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm)
Mathieu Chartier90443472015-07-16 20:32:27 -07001446 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001447 UNUSED(sm);
1448 }
Andreas Gampec200a4a2014-06-16 18:39:09 -07001449
Mathieu Chartier90443472015-07-16 20:32:27 -07001450 void Walk(const char* shorty, uint32_t shorty_len) SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001451 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize> sm(this);
1452
1453 WalkHeader(&sm);
Andreas Gampec147b002014-03-06 18:11:06 -08001454
1455 for (uint32_t i = 1; i < shorty_len; ++i) {
1456 Primitive::Type cur_type_ = Primitive::GetType(shorty[i]);
1457 switch (cur_type_) {
1458 case Primitive::kPrimNot:
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001459 // TODO: fix abuse of mirror types.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001460 sm.AdvanceHandleScope(
1461 reinterpret_cast<mirror::Object*>(0x12345678));
Andreas Gampec147b002014-03-06 18:11:06 -08001462 break;
1463
1464 case Primitive::kPrimBoolean:
1465 case Primitive::kPrimByte:
1466 case Primitive::kPrimChar:
1467 case Primitive::kPrimShort:
1468 case Primitive::kPrimInt:
1469 sm.AdvanceInt(0);
1470 break;
1471 case Primitive::kPrimFloat:
1472 sm.AdvanceFloat(0);
1473 break;
1474 case Primitive::kPrimDouble:
1475 sm.AdvanceDouble(0);
1476 break;
1477 case Primitive::kPrimLong:
1478 sm.AdvanceLong(0);
1479 break;
1480 default:
1481 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001482 UNREACHABLE();
Andreas Gampec147b002014-03-06 18:11:06 -08001483 }
1484 }
1485
Ian Rogers1428dce2014-10-21 15:02:15 -07001486 num_stack_entries_ = sm.GetStackEntries();
Andreas Gampec147b002014-03-06 18:11:06 -08001487 }
1488
1489 void PushGpr(uintptr_t /* val */) {
1490 // not optimizing registers, yet
1491 }
1492
1493 void PushFpr4(float /* val */) {
1494 // not optimizing registers, yet
1495 }
1496
1497 void PushFpr8(uint64_t /* val */) {
1498 // not optimizing registers, yet
1499 }
1500
1501 void PushStack(uintptr_t /* val */) {
1502 // counting is already done in the superclass
1503 }
1504
Andreas Gampec200a4a2014-06-16 18:39:09 -07001505 virtual uintptr_t PushHandle(mirror::Object* /* ptr */) {
Andreas Gampec147b002014-03-06 18:11:06 -08001506 return reinterpret_cast<uintptr_t>(nullptr);
1507 }
1508
Andreas Gampec200a4a2014-06-16 18:39:09 -07001509 protected:
Andreas Gampec147b002014-03-06 18:11:06 -08001510 uint32_t num_stack_entries_;
1511};
1512
Andreas Gampec200a4a2014-06-16 18:39:09 -07001513class ComputeGenericJniFrameSize FINAL : public ComputeNativeCallFrameSize {
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001514 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001515 ComputeGenericJniFrameSize() : num_handle_scope_references_(0) {}
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001516
Andreas Gampec200a4a2014-06-16 18:39:09 -07001517 // Lays out the callee-save frame. Assumes that the incorrect frame corresponding to RefsAndArgs
1518 // is at *m = sp. Will update to point to the bottom of the save frame.
1519 //
1520 // Note: assumes ComputeAll() has been run before.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001521 void LayoutCalleeSaveFrame(Thread* self, ArtMethod*** m, void* sp, HandleScope** handle_scope)
Mathieu Chartier90443472015-07-16 20:32:27 -07001522 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001523 ArtMethod* method = **m;
1524
1525 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), sizeof(void*));
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001526
Andreas Gampec200a4a2014-06-16 18:39:09 -07001527 uint8_t* sp8 = reinterpret_cast<uint8_t*>(sp);
1528
1529 // First, fix up the layout of the callee-save frame.
1530 // We have to squeeze in the HandleScope, and relocate the method pointer.
1531
1532 // "Free" the slot for the method.
Ian Rogers13735952014-10-08 12:43:28 -07001533 sp8 += sizeof(void*); // In the callee-save frame we use a full pointer.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001534
1535 // Under the callee saves put handle scope and new method stack reference.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001536 size_t handle_scope_size = HandleScope::SizeOf(num_handle_scope_references_);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001537 size_t scope_and_method = handle_scope_size + sizeof(ArtMethod*);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001538
1539 sp8 -= scope_and_method;
1540 // Align by kStackAlignment.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001541 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
Andreas Gampec200a4a2014-06-16 18:39:09 -07001542
Mathieu Chartiere401d142015-04-22 13:56:20 -07001543 uint8_t* sp8_table = sp8 + sizeof(ArtMethod*);
Ian Rogers59c07062014-10-10 13:03:39 -07001544 *handle_scope = HandleScope::Create(sp8_table, self->GetTopHandleScope(),
1545 num_handle_scope_references_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001546
1547 // Add a slot for the method pointer, and fill it. Fix the pointer-pointer given to us.
1548 uint8_t* method_pointer = sp8;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001549 auto** new_method_ref = reinterpret_cast<ArtMethod**>(method_pointer);
1550 *new_method_ref = method;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001551 *m = new_method_ref;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001552 }
1553
Andreas Gampec200a4a2014-06-16 18:39:09 -07001554 // Adds space for the cookie. Note: may leave stack unaligned.
Ian Rogers1428dce2014-10-21 15:02:15 -07001555 void LayoutCookie(uint8_t** sp) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001556 // Reference cookie and padding
1557 *sp -= 8;
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001558 }
1559
Andreas Gampec200a4a2014-06-16 18:39:09 -07001560 // Re-layout the callee-save frame (insert a handle-scope). Then add space for the cookie.
1561 // Returns the new bottom. Note: this may be unaligned.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001562 uint8_t* LayoutJNISaveFrame(Thread* self, ArtMethod*** m, void* sp, HandleScope** handle_scope)
Mathieu Chartier90443472015-07-16 20:32:27 -07001563 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001564 // First, fix up the layout of the callee-save frame.
1565 // We have to squeeze in the HandleScope, and relocate the method pointer.
Ian Rogers59c07062014-10-10 13:03:39 -07001566 LayoutCalleeSaveFrame(self, m, sp, handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001567
1568 // The bottom of the callee-save frame is now where the method is, *m.
1569 uint8_t* sp8 = reinterpret_cast<uint8_t*>(*m);
1570
1571 // Add space for cookie.
1572 LayoutCookie(&sp8);
1573
1574 return sp8;
1575 }
1576
1577 // WARNING: After this, *sp won't be pointing to the method anymore!
Mathieu Chartiere401d142015-04-22 13:56:20 -07001578 uint8_t* ComputeLayout(Thread* self, ArtMethod*** m, const char* shorty, uint32_t shorty_len,
1579 HandleScope** handle_scope, uintptr_t** start_stack, uintptr_t** start_gpr,
1580 uint32_t** start_fpr)
Mathieu Chartier90443472015-07-16 20:32:27 -07001581 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001582 Walk(shorty, shorty_len);
1583
1584 // JNI part.
Ian Rogers59c07062014-10-10 13:03:39 -07001585 uint8_t* sp8 = LayoutJNISaveFrame(self, m, reinterpret_cast<void*>(*m), handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001586
1587 sp8 = LayoutNativeCall(sp8, start_stack, start_gpr, start_fpr);
1588
1589 // Return the new bottom.
1590 return sp8;
1591 }
1592
1593 uintptr_t PushHandle(mirror::Object* /* ptr */) OVERRIDE;
1594
1595 // Add JNIEnv* and jobj/jclass before the shorty-derived elements.
1596 void WalkHeader(BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) OVERRIDE
Mathieu Chartier90443472015-07-16 20:32:27 -07001597 SHARED_REQUIRES(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001598
1599 private:
1600 uint32_t num_handle_scope_references_;
1601};
1602
1603uintptr_t ComputeGenericJniFrameSize::PushHandle(mirror::Object* /* ptr */) {
1604 num_handle_scope_references_++;
1605 return reinterpret_cast<uintptr_t>(nullptr);
1606}
1607
1608void ComputeGenericJniFrameSize::WalkHeader(
1609 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) {
1610 // JNIEnv
1611 sm->AdvancePointer(nullptr);
1612
1613 // Class object or this as first argument
1614 sm->AdvanceHandleScope(reinterpret_cast<mirror::Object*>(0x12345678));
1615}
1616
1617// Class to push values to three separate regions. Used to fill the native call part. Adheres to
1618// the template requirements of BuildGenericJniFrameStateMachine.
1619class FillNativeCall {
1620 public:
1621 FillNativeCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) :
1622 cur_gpr_reg_(gpr_regs), cur_fpr_reg_(fpr_regs), cur_stack_arg_(stack_args) {}
1623
1624 virtual ~FillNativeCall() {}
1625
1626 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) {
1627 cur_gpr_reg_ = gpr_regs;
1628 cur_fpr_reg_ = fpr_regs;
1629 cur_stack_arg_ = stack_args;
Andreas Gampec147b002014-03-06 18:11:06 -08001630 }
1631
1632 void PushGpr(uintptr_t val) {
1633 *cur_gpr_reg_ = val;
1634 cur_gpr_reg_++;
1635 }
1636
1637 void PushFpr4(float val) {
1638 *cur_fpr_reg_ = val;
1639 cur_fpr_reg_++;
1640 }
1641
1642 void PushFpr8(uint64_t val) {
1643 uint64_t* tmp = reinterpret_cast<uint64_t*>(cur_fpr_reg_);
1644 *tmp = val;
1645 cur_fpr_reg_ += 2;
1646 }
1647
1648 void PushStack(uintptr_t val) {
1649 *cur_stack_arg_ = val;
1650 cur_stack_arg_++;
1651 }
1652
Mathieu Chartier90443472015-07-16 20:32:27 -07001653 virtual uintptr_t PushHandle(mirror::Object*) SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001654 LOG(FATAL) << "(Non-JNI) Native call does not use handles.";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001655 UNREACHABLE();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001656 }
1657
1658 private:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001659 uintptr_t* cur_gpr_reg_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001660 uint32_t* cur_fpr_reg_;
1661 uintptr_t* cur_stack_arg_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001662};
Andreas Gampec147b002014-03-06 18:11:06 -08001663
Andreas Gampec200a4a2014-06-16 18:39:09 -07001664// Visits arguments on the stack placing them into a region lower down the stack for the benefit
1665// of transitioning into native code.
1666class BuildGenericJniFrameVisitor FINAL : public QuickArgumentVisitor {
1667 public:
Ian Rogers59c07062014-10-10 13:03:39 -07001668 BuildGenericJniFrameVisitor(Thread* self, bool is_static, const char* shorty, uint32_t shorty_len,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001669 ArtMethod*** sp)
Andreas Gampec200a4a2014-06-16 18:39:09 -07001670 : QuickArgumentVisitor(*sp, is_static, shorty, shorty_len),
1671 jni_call_(nullptr, nullptr, nullptr, nullptr), sm_(&jni_call_) {
1672 ComputeGenericJniFrameSize fsc;
1673 uintptr_t* start_gpr_reg;
1674 uint32_t* start_fpr_reg;
1675 uintptr_t* start_stack_arg;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001676 bottom_of_used_area_ = fsc.ComputeLayout(self, sp, shorty, shorty_len,
Ian Rogers59c07062014-10-10 13:03:39 -07001677 &handle_scope_,
1678 &start_stack_arg,
Andreas Gampec200a4a2014-06-16 18:39:09 -07001679 &start_gpr_reg, &start_fpr_reg);
1680
Andreas Gampec200a4a2014-06-16 18:39:09 -07001681 jni_call_.Reset(start_gpr_reg, start_fpr_reg, start_stack_arg, handle_scope_);
1682
1683 // jni environment is always first argument
1684 sm_.AdvancePointer(self->GetJniEnv());
1685
1686 if (is_static) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001687 sm_.AdvanceHandleScope((**sp)->GetDeclaringClass());
Andreas Gampec200a4a2014-06-16 18:39:09 -07001688 }
1689 }
1690
Mathieu Chartier90443472015-07-16 20:32:27 -07001691 void Visit() SHARED_REQUIRES(Locks::mutator_lock_) OVERRIDE;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001692
Mathieu Chartier90443472015-07-16 20:32:27 -07001693 void FinalizeHandleScope(Thread* self) SHARED_REQUIRES(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001694
1695 StackReference<mirror::Object>* GetFirstHandleScopeEntry()
Mathieu Chartier90443472015-07-16 20:32:27 -07001696 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001697 return handle_scope_->GetHandle(0).GetReference();
1698 }
1699
Mathieu Chartier90443472015-07-16 20:32:27 -07001700 jobject GetFirstHandleScopeJObject() const SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001701 return handle_scope_->GetHandle(0).ToJObject();
1702 }
1703
Ian Rogers1428dce2014-10-21 15:02:15 -07001704 void* GetBottomOfUsedArea() const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001705 return bottom_of_used_area_;
1706 }
1707
1708 private:
1709 // A class to fill a JNI call. Adds reference/handle-scope management to FillNativeCall.
1710 class FillJniCall FINAL : public FillNativeCall {
1711 public:
1712 FillJniCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args,
1713 HandleScope* handle_scope) : FillNativeCall(gpr_regs, fpr_regs, stack_args),
1714 handle_scope_(handle_scope), cur_entry_(0) {}
1715
Mathieu Chartier90443472015-07-16 20:32:27 -07001716 uintptr_t PushHandle(mirror::Object* ref) OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001717
1718 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args, HandleScope* scope) {
1719 FillNativeCall::Reset(gpr_regs, fpr_regs, stack_args);
1720 handle_scope_ = scope;
1721 cur_entry_ = 0U;
1722 }
1723
Mathieu Chartier90443472015-07-16 20:32:27 -07001724 void ResetRemainingScopeSlots() SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001725 // Initialize padding entries.
1726 size_t expected_slots = handle_scope_->NumberOfReferences();
1727 while (cur_entry_ < expected_slots) {
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07001728 handle_scope_->GetMutableHandle(cur_entry_++).Assign(nullptr);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001729 }
1730 DCHECK_NE(cur_entry_, 0U);
1731 }
1732
1733 private:
1734 HandleScope* handle_scope_;
1735 size_t cur_entry_;
1736 };
1737
1738 HandleScope* handle_scope_;
1739 FillJniCall jni_call_;
1740 void* bottom_of_used_area_;
1741
1742 BuildNativeCallFrameStateMachine<FillJniCall> sm_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001743
1744 DISALLOW_COPY_AND_ASSIGN(BuildGenericJniFrameVisitor);
1745};
1746
Andreas Gampec200a4a2014-06-16 18:39:09 -07001747uintptr_t BuildGenericJniFrameVisitor::FillJniCall::PushHandle(mirror::Object* ref) {
1748 uintptr_t tmp;
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07001749 MutableHandle<mirror::Object> h = handle_scope_->GetMutableHandle(cur_entry_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001750 h.Assign(ref);
1751 tmp = reinterpret_cast<uintptr_t>(h.ToJObject());
1752 cur_entry_++;
1753 return tmp;
1754}
1755
Ian Rogers9758f792014-03-13 09:02:55 -07001756void BuildGenericJniFrameVisitor::Visit() {
1757 Primitive::Type type = GetParamPrimitiveType();
1758 switch (type) {
1759 case Primitive::kPrimLong: {
1760 jlong long_arg;
1761 if (IsSplitLongOrDouble()) {
1762 long_arg = ReadSplitLongParam();
1763 } else {
1764 long_arg = *reinterpret_cast<jlong*>(GetParamAddress());
1765 }
1766 sm_.AdvanceLong(long_arg);
1767 break;
1768 }
1769 case Primitive::kPrimDouble: {
1770 uint64_t double_arg;
1771 if (IsSplitLongOrDouble()) {
1772 // Read into union so that we don't case to a double.
1773 double_arg = ReadSplitLongParam();
1774 } else {
1775 double_arg = *reinterpret_cast<uint64_t*>(GetParamAddress());
1776 }
1777 sm_.AdvanceDouble(double_arg);
1778 break;
1779 }
1780 case Primitive::kPrimNot: {
1781 StackReference<mirror::Object>* stack_ref =
1782 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001783 sm_.AdvanceHandleScope(stack_ref->AsMirrorPtr());
Ian Rogers9758f792014-03-13 09:02:55 -07001784 break;
1785 }
1786 case Primitive::kPrimFloat:
1787 sm_.AdvanceFloat(*reinterpret_cast<float*>(GetParamAddress()));
1788 break;
1789 case Primitive::kPrimBoolean: // Fall-through.
1790 case Primitive::kPrimByte: // Fall-through.
1791 case Primitive::kPrimChar: // Fall-through.
1792 case Primitive::kPrimShort: // Fall-through.
1793 case Primitive::kPrimInt: // Fall-through.
1794 sm_.AdvanceInt(*reinterpret_cast<jint*>(GetParamAddress()));
1795 break;
1796 case Primitive::kPrimVoid:
1797 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -07001798 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -07001799 }
1800}
1801
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001802void BuildGenericJniFrameVisitor::FinalizeHandleScope(Thread* self) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001803 // Clear out rest of the scope.
1804 jni_call_.ResetRemainingScopeSlots();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001805 // Install HandleScope.
1806 self->PushHandleScope(handle_scope_);
Ian Rogers9758f792014-03-13 09:02:55 -07001807}
1808
Ian Rogers04c31d22014-07-07 21:44:06 -07001809#if defined(__arm__) || defined(__aarch64__)
Andreas Gampe90546832014-03-12 18:07:19 -07001810extern "C" void* artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07001811#else
1812extern "C" void* artFindNativeMethod(Thread* self);
1813#endif
Andreas Gampe90546832014-03-12 18:07:19 -07001814
Andreas Gampead615172014-04-04 16:20:13 -07001815uint64_t artQuickGenericJniEndJNIRef(Thread* self, uint32_t cookie, jobject l, jobject lock) {
1816 if (lock != nullptr) {
1817 return reinterpret_cast<uint64_t>(JniMethodEndWithReferenceSynchronized(l, cookie, lock, self));
1818 } else {
1819 return reinterpret_cast<uint64_t>(JniMethodEndWithReference(l, cookie, self));
1820 }
1821}
1822
1823void artQuickGenericJniEndJNINonRef(Thread* self, uint32_t cookie, jobject lock) {
1824 if (lock != nullptr) {
1825 JniMethodEndSynchronized(cookie, lock, self);
1826 } else {
1827 JniMethodEnd(cookie, self);
1828 }
1829}
1830
Andreas Gampec147b002014-03-06 18:11:06 -08001831/*
1832 * Initializes an alloca region assumed to be directly below sp for a native call:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001833 * 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 -08001834 * The final element on the stack is a pointer to the native code.
1835 *
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001836 * On entry, the stack has a standard callee-save frame above sp, and an alloca below it.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001837 * We need to fix this, as the handle scope needs to go into the callee-save frame.
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001838 *
Andreas Gampec147b002014-03-06 18:11:06 -08001839 * The return of this function denotes:
1840 * 1) How many bytes of the alloca can be released, if the value is non-negative.
1841 * 2) An error, if the value is negative.
1842 */
Mathieu Chartiere401d142015-04-22 13:56:20 -07001843extern "C" TwoWordReturn artQuickGenericJniTrampoline(Thread* self, ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -07001844 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001845 ArtMethod* called = *sp;
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001846 DCHECK(called->IsNative()) << PrettyMethod(called, true);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001847 uint32_t shorty_len = 0;
1848 const char* shorty = called->GetShorty(&shorty_len);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001849
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001850 // Run the visitor and update sp.
Ian Rogers59c07062014-10-10 13:03:39 -07001851 BuildGenericJniFrameVisitor visitor(self, called->IsStatic(), shorty, shorty_len, &sp);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001852 visitor.VisitArguments();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001853 visitor.FinalizeHandleScope(self);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001854
Andreas Gampec200a4a2014-06-16 18:39:09 -07001855 // Fix up managed-stack things in Thread.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001856 self->SetTopOfStack(sp);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001857
Ian Rogerse0dcd462014-03-08 15:21:04 -08001858 self->VerifyStack();
1859
Andreas Gampe90546832014-03-12 18:07:19 -07001860 // Start JNI, save the cookie.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001861 uint32_t cookie;
1862 if (called->IsSynchronized()) {
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001863 cookie = JniMethodStartSynchronized(visitor.GetFirstHandleScopeJObject(), self);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001864 if (self->IsExceptionPending()) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001865 self->PopHandleScope();
Andreas Gampec147b002014-03-06 18:11:06 -08001866 // A negative value denotes an error.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001867 return GetTwoWordFailureValue();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001868 }
1869 } else {
1870 cookie = JniMethodStart(self);
1871 }
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001872 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
Ian Rogerse0dcd462014-03-08 15:21:04 -08001873 *(sp32 - 1) = cookie;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001874
Andreas Gampe90546832014-03-12 18:07:19 -07001875 // Retrieve the stored native code.
Mathieu Chartier2d721012014-11-10 11:08:06 -08001876 void* nativeCode = called->GetEntryPointFromJni();
Andreas Gampe90546832014-03-12 18:07:19 -07001877
Andreas Gampe9a6a99a2014-03-14 07:52:20 -07001878 // There are two cases for the content of nativeCode:
1879 // 1) Pointer to the native function.
1880 // 2) Pointer to the trampoline for native code binding.
1881 // In the second case, we need to execute the binding and continue with the actual native function
1882 // pointer.
Andreas Gampe90546832014-03-12 18:07:19 -07001883 DCHECK(nativeCode != nullptr);
1884 if (nativeCode == GetJniDlsymLookupStub()) {
Ian Rogers04c31d22014-07-07 21:44:06 -07001885#if defined(__arm__) || defined(__aarch64__)
Andreas Gampe90546832014-03-12 18:07:19 -07001886 nativeCode = artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07001887#else
1888 nativeCode = artFindNativeMethod(self);
1889#endif
Andreas Gampe90546832014-03-12 18:07:19 -07001890
1891 if (nativeCode == nullptr) {
1892 DCHECK(self->IsExceptionPending()); // There should be an exception pending now.
Andreas Gampead615172014-04-04 16:20:13 -07001893
1894 // End JNI, as the assembly will move to deliver the exception.
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001895 jobject lock = called->IsSynchronized() ? visitor.GetFirstHandleScopeJObject() : nullptr;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001896 if (shorty[0] == 'L') {
Andreas Gampead615172014-04-04 16:20:13 -07001897 artQuickGenericJniEndJNIRef(self, cookie, nullptr, lock);
1898 } else {
1899 artQuickGenericJniEndJNINonRef(self, cookie, lock);
1900 }
1901
Andreas Gampec200a4a2014-06-16 18:39:09 -07001902 return GetTwoWordFailureValue();
Andreas Gampe90546832014-03-12 18:07:19 -07001903 }
1904 // Note that the native code pointer will be automatically set by artFindNativeMethod().
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001905 }
1906
Andreas Gampec200a4a2014-06-16 18:39:09 -07001907 // Return native code addr(lo) and bottom of alloca address(hi).
1908 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(visitor.GetBottomOfUsedArea()),
1909 reinterpret_cast<uintptr_t>(nativeCode));
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001910}
1911
1912/*
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001913 * Is called after the native JNI code. Responsible for cleanup (handle scope, saved state) and
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001914 * unlocking.
1915 */
Andreas Gampec200a4a2014-06-16 18:39:09 -07001916extern "C" uint64_t artQuickGenericJniEndTrampoline(Thread* self, jvalue result, uint64_t result_f)
Mathieu Chartier90443472015-07-16 20:32:27 -07001917 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001918 ArtMethod** sp = self->GetManagedStack()->GetTopQuickFrame();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001919 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001920 ArtMethod* called = *sp;
Ian Rogerse0dcd462014-03-08 15:21:04 -08001921 uint32_t cookie = *(sp32 - 1);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001922
Andreas Gampead615172014-04-04 16:20:13 -07001923 jobject lock = nullptr;
1924 if (called->IsSynchronized()) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001925 HandleScope* table = reinterpret_cast<HandleScope*>(reinterpret_cast<uint8_t*>(sp)
Mathieu Chartiere401d142015-04-22 13:56:20 -07001926 + sizeof(*sp));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001927 lock = table->GetHandle(0).ToJObject();
Andreas Gampead615172014-04-04 16:20:13 -07001928 }
1929
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001930 char return_shorty_char = called->GetShorty()[0];
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001931
1932 if (return_shorty_char == 'L') {
Andreas Gampead615172014-04-04 16:20:13 -07001933 return artQuickGenericJniEndJNIRef(self, cookie, result.l, lock);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001934 } else {
Andreas Gampead615172014-04-04 16:20:13 -07001935 artQuickGenericJniEndJNINonRef(self, cookie, lock);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001936
1937 switch (return_shorty_char) {
Nicolas Geoffray54accbc2014-08-13 03:40:45 +01001938 case 'F': {
1939 if (kRuntimeISA == kX86) {
1940 // Convert back the result to float.
Roland Levillainda4d79b2015-03-24 14:36:11 +00001941 double d = bit_cast<double, uint64_t>(result_f);
1942 return bit_cast<uint32_t, float>(static_cast<float>(d));
Nicolas Geoffray54accbc2014-08-13 03:40:45 +01001943 } else {
1944 return result_f;
1945 }
1946 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001947 case 'D':
1948 return result_f;
1949 case 'Z':
1950 return result.z;
1951 case 'B':
1952 return result.b;
1953 case 'C':
1954 return result.c;
1955 case 'S':
1956 return result.s;
1957 case 'I':
1958 return result.i;
1959 case 'J':
1960 return result.j;
1961 case 'V':
1962 return 0;
1963 default:
1964 LOG(FATAL) << "Unexpected return shorty character " << return_shorty_char;
1965 return 0;
1966 }
1967 }
Andreas Gampe2da88232014-02-27 12:26:20 -08001968}
1969
Andreas Gamped58342c2014-06-05 14:18:08 -07001970// We use TwoWordReturn to optimize scalar returns. We use the hi value for code, and the lo value
1971// for the method pointer.
Andreas Gampe51f76352014-05-21 08:28:48 -07001972//
Andreas Gamped58342c2014-06-05 14:18:08 -07001973// 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 -07001974// to hold the mutator lock (see SHARED_REQUIRES(Locks::mutator_lock_) annotations).
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001975
1976template<InvokeType type, bool access_check>
Mathieu Chartiere401d142015-04-22 13:56:20 -07001977static TwoWordReturn artInvokeCommon(uint32_t method_idx, mirror::Object* this_object, Thread* self,
1978 ArtMethod** sp) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001979 ScopedQuickEntrypointChecks sqec(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001980 DCHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs));
1981 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
1982 ArtMethod* method = FindMethodFast(method_idx, this_object, caller_method, access_check, type);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001983 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001984 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()->GetDexFile();
1985 uint32_t shorty_len;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001986 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(method_idx), &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001987 {
1988 // Remember the args in case a GC happens in FindMethodFromCode.
1989 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
1990 RememberForGcArgumentVisitor visitor(sp, type == kStatic, shorty, shorty_len, &soa);
1991 visitor.VisitArguments();
Andreas Gampe3a357142015-08-07 17:20:11 -07001992 method = FindMethodFromCode<type, access_check>(method_idx, &this_object, caller_method,
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001993 self);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001994 visitor.FixupReferences();
1995 }
1996
Ian Rogerse0a02da2014-12-02 14:10:53 -08001997 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001998 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07001999 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002000 }
2001 }
2002 DCHECK(!self->IsExceptionPending());
2003 const void* code = method->GetEntryPointFromQuickCompiledCode();
2004
2005 // When we return, the caller will branch to this address, so it had better not be 0!
Ian Rogerse0a02da2014-12-02 14:10:53 -08002006 DCHECK(code != nullptr) << "Code was null in method: " << PrettyMethod(method)
Andreas Gampec200a4a2014-06-16 18:39:09 -07002007 << " location: "
2008 << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07002009
Andreas Gamped58342c2014-06-05 14:18:08 -07002010 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
2011 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002012}
2013
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01002014// Explicit artInvokeCommon template function declarations to please analysis tool.
2015#define EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(type, access_check) \
Mathieu Chartier90443472015-07-16 20:32:27 -07002016 template SHARED_REQUIRES(Locks::mutator_lock_) \
Mathieu Chartiere401d142015-04-22 13:56:20 -07002017 TwoWordReturn artInvokeCommon<type, access_check>( \
2018 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01002019
2020EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, false);
2021EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, true);
2022EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, false);
2023EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, true);
2024EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, false);
2025EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, true);
2026EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, false);
2027EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, true);
2028EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, false);
2029EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, true);
2030#undef EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL
2031
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002032// See comments in runtime_support_asm.S
Andreas Gampec200a4a2014-06-16 18:39:09 -07002033extern "C" TwoWordReturn artInvokeInterfaceTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002034 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -07002035 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002036 return artInvokeCommon<kInterface, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002037}
2038
Andreas Gampec200a4a2014-06-16 18:39:09 -07002039extern "C" TwoWordReturn artInvokeDirectTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002040 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -07002041 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002042 return artInvokeCommon<kDirect, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002043}
2044
Andreas Gampec200a4a2014-06-16 18:39:09 -07002045extern "C" TwoWordReturn artInvokeStaticTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002046 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -07002047 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002048 return artInvokeCommon<kStatic, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002049}
2050
Andreas Gampec200a4a2014-06-16 18:39:09 -07002051extern "C" TwoWordReturn artInvokeSuperTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002052 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -07002053 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002054 return artInvokeCommon<kSuper, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002055}
2056
Andreas Gampec200a4a2014-06-16 18:39:09 -07002057extern "C" TwoWordReturn artInvokeVirtualTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002058 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -07002059 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002060 return artInvokeCommon<kVirtual, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002061}
2062
2063// Determine target of interface dispatch. This object is known non-null.
Nicolas Geoffray8ea18d02015-05-26 16:29:08 +01002064extern "C" TwoWordReturn artInvokeInterfaceTrampoline(uint32_t dex_method_idx,
Andreas Gampe51f76352014-05-21 08:28:48 -07002065 mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -07002066 Thread* self, ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -07002067 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002068 ScopedQuickEntrypointChecks sqec(self);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002069 // The optimizing compiler currently does not inline methods that have an interface
2070 // invocation. We use the outer method directly to avoid fetching a stack map, which is
2071 // more expensive.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002072 ArtMethod* caller_method = QuickArgumentVisitor::GetOuterMethod(sp);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002073 DCHECK_EQ(caller_method, QuickArgumentVisitor::GetCallingMethod(sp));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002074 ArtMethod* interface_method = caller_method->GetDexCacheResolvedMethod(
2075 dex_method_idx, sizeof(void*));
2076 DCHECK(interface_method != nullptr) << dex_method_idx << " " << PrettyMethod(caller_method);
2077 ArtMethod* method;
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002078 if (LIKELY(interface_method->GetDexMethodIndex() != DexFile::kDexNoIndex)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002079 method = this_object->GetClass()->FindVirtualMethodForInterface(
2080 interface_method, sizeof(void*));
Ian Rogerse0a02da2014-12-02 14:10:53 -08002081 if (UNLIKELY(method == nullptr)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002082 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(
2083 interface_method, this_object, caller_method);
Andreas Gamped58342c2014-06-05 14:18:08 -07002084 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002085 }
2086 } else {
Mathieu Chartier4edd8472015-06-01 10:47:36 -07002087 DCHECK_EQ(interface_method, Runtime::Current()->GetResolutionMethod());
Nicolas Geoffray8ea18d02015-05-26 16:29:08 +01002088 if (kIsDebugBuild) {
2089 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
2090 const DexFile::CodeItem* code = caller_method->GetCodeItem();
2091 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
2092 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
2093 Instruction::Code instr_code = instr->Opcode();
2094 CHECK(instr_code == Instruction::INVOKE_INTERFACE ||
2095 instr_code == Instruction::INVOKE_INTERFACE_RANGE)
2096 << "Unexpected call into interface trampoline: " << instr->DumpString(nullptr);
2097 if (instr_code == Instruction::INVOKE_INTERFACE) {
2098 CHECK_EQ(dex_method_idx, instr->VRegB_35c());
2099 } else {
2100 CHECK_EQ(instr_code, Instruction::INVOKE_INTERFACE_RANGE);
2101 CHECK_EQ(dex_method_idx, instr->VRegB_3rc());
2102 }
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002103 }
2104
Andreas Gampec200a4a2014-06-16 18:39:09 -07002105 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()
2106 ->GetDexFile();
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002107 uint32_t shorty_len;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002108 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(dex_method_idx),
2109 &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002110 {
2111 // Remember the args in case a GC happens in FindMethodFromCode.
2112 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
2113 RememberForGcArgumentVisitor visitor(sp, false, shorty, shorty_len, &soa);
2114 visitor.VisitArguments();
Andreas Gampe3a357142015-08-07 17:20:11 -07002115 method = FindMethodFromCode<kInterface, false>(dex_method_idx, &this_object, caller_method,
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002116 self);
2117 visitor.FixupReferences();
2118 }
2119
2120 if (UNLIKELY(method == nullptr)) {
2121 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07002122 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002123 }
2124 }
2125 const void* code = method->GetEntryPointFromQuickCompiledCode();
2126
2127 // When we return, the caller will branch to this address, so it had better not be 0!
Ian Rogerse0a02da2014-12-02 14:10:53 -08002128 DCHECK(code != nullptr) << "Code was null in method: " << PrettyMethod(method)
Andreas Gampec200a4a2014-06-16 18:39:09 -07002129 << " location: " << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07002130
Andreas Gamped58342c2014-06-05 14:18:08 -07002131 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
2132 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002133}
2134
Ian Rogers848871b2013-08-05 10:56:33 -07002135} // namespace art