blob: 227c5b0417493e7ec91b2980ea930b6ba78bc915 [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
17#include "callee_save_frame.h"
Dragos Sbirleabd136a22013-08-13 18:07:04 -070018#include "common_throws.h"
Ian Rogers848871b2013-08-05 10:56:33 -070019#include "dex_file-inl.h"
20#include "dex_instruction-inl.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070021#include "entrypoints/entrypoint_utils-inl.h"
Ian Rogers6f3dbba2014-10-14 17:41:57 -070022#include "entrypoints/runtime_asm_entrypoints.h"
Ian Rogers83883d72013-10-21 21:07:24 -070023#include "gc/accounting/card_table-inl.h"
Ian Rogers848871b2013-08-05 10:56:33 -070024#include "interpreter/interpreter.h"
Ian Rogerse0a02da2014-12-02 14:10:53 -080025#include "method_reference.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070026#include "mirror/art_method-inl.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.
282 static mirror::Object* GetProxyThisObject(StackReference<mirror::ArtMethod>* sp)
283 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
284 CHECK(sp->AsMirrorPtr()->IsProxyMethod());
285 CHECK_EQ(kQuickCalleeSaveFrame_RefAndArgs_FrameSize, sp->AsMirrorPtr()->GetFrameSizeInBytes());
286 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
Andreas Gampecf4035a2014-05-28 22:43:01 -0700294 static mirror::ArtMethod* GetCallingMethod(StackReference<mirror::ArtMethod>* sp)
Ian Rogers936b37f2014-02-14 00:52:24 -0800295 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampecf4035a2014-05-28 22:43:01 -0700296 DCHECK(sp->AsMirrorPtr()->IsCalleeSaveMethod());
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +0100297 return GetCalleeSaveMethodCaller(sp, Runtime::kRefsAndArgs);
298 }
299
300 static uint32_t GetCallingDexPc(StackReference<mirror::ArtMethod>* sp)
301 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
302 DCHECK(sp->AsMirrorPtr()->IsCalleeSaveMethod());
303 return GetCallingMethod(sp)->ToDexPc(QuickArgumentVisitor::GetCallingPc(sp));
Ian Rogers848871b2013-08-05 10:56:33 -0700304 }
305
Ian Rogers936b37f2014-02-14 00:52:24 -0800306 // For the given quick ref and args quick frame, return the caller's PC.
Andreas Gampecf4035a2014-05-28 22:43:01 -0700307 static uintptr_t GetCallingPc(StackReference<mirror::ArtMethod>* sp)
Ian Rogers936b37f2014-02-14 00:52:24 -0800308 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampecf4035a2014-05-28 22:43:01 -0700309 DCHECK(sp->AsMirrorPtr()->IsCalleeSaveMethod());
Ian Rogers13735952014-10-08 12:43:28 -0700310 uint8_t* lr = reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_LrOffset;
Ian Rogers848871b2013-08-05 10:56:33 -0700311 return *reinterpret_cast<uintptr_t*>(lr);
312 }
313
Andreas Gampec200a4a2014-06-16 18:39:09 -0700314 QuickArgumentVisitor(StackReference<mirror::ArtMethod>* sp, bool is_static, const char* shorty,
315 uint32_t shorty_len) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) :
316 is_static_(is_static), shorty_(shorty), shorty_len_(shorty_len),
Ian Rogers13735952014-10-08 12:43:28 -0700317 gpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset),
318 fpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset),
319 stack_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700320 + sizeof(StackReference<mirror::ArtMethod>)), // Skip StackReference<ArtMethod>.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800321 gpr_index_(0), fpr_index_(0), fpr_double_index_(0), stack_index_(0),
322 cur_type_(Primitive::kPrimVoid), is_split_long_or_double_(false) {
Andreas Gampe575e78c2014-11-03 23:41:03 -0800323 static_assert(kQuickSoftFloatAbi == (kNumQuickFprArgs == 0),
324 "Number of Quick FPR arguments unexpected");
325 static_assert(!(kQuickSoftFloatAbi && kQuickDoubleRegAlignedFloatBackFilled),
326 "Double alignment unexpected");
Zheng Xu5667fdb2014-10-23 18:29:55 +0800327 // For register alignment, we want to assume that counters(fpr_double_index_) are even if the
328 // next register is even.
Andreas Gampe575e78c2014-11-03 23:41:03 -0800329 static_assert(!kQuickDoubleRegAlignedFloatBackFilled || kNumQuickFprArgs % 2 == 0,
330 "Number of Quick FPR arguments not even");
Zheng Xu5667fdb2014-10-23 18:29:55 +0800331 }
Ian Rogers848871b2013-08-05 10:56:33 -0700332
333 virtual ~QuickArgumentVisitor() {}
334
335 virtual void Visit() = 0;
336
Ian Rogers936b37f2014-02-14 00:52:24 -0800337 Primitive::Type GetParamPrimitiveType() const {
338 return cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700339 }
340
Ian Rogers13735952014-10-08 12:43:28 -0700341 uint8_t* GetParamAddress() const {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800342 if (!kQuickSoftFloatAbi) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800343 Primitive::Type type = GetParamPrimitiveType();
344 if (UNLIKELY((type == Primitive::kPrimDouble) || (type == Primitive::kPrimFloat))) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800345 if (type == Primitive::kPrimDouble && kQuickDoubleRegAlignedFloatBackFilled) {
346 if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) {
347 return fpr_args_ + (fpr_double_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
348 }
349 } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000350 return fpr_args_ + (fpr_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800351 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700352 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers936b37f2014-02-14 00:52:24 -0800353 }
354 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800355 if (gpr_index_ < kNumQuickGprArgs) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800356 return gpr_args_ + GprIndexToGprOffset(gpr_index_);
357 }
358 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700359 }
360
361 bool IsSplitLongOrDouble() const {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000362 if ((GetBytesPerGprSpillLocation(kRuntimeISA) == 4) || (GetBytesPerFprSpillLocation(kRuntimeISA) == 4)) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800363 return is_split_long_or_double_;
364 } else {
365 return false; // An optimization for when GPR and FPRs are 64bit.
366 }
Ian Rogers848871b2013-08-05 10:56:33 -0700367 }
368
Ian Rogers936b37f2014-02-14 00:52:24 -0800369 bool IsParamAReference() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700370 return GetParamPrimitiveType() == Primitive::kPrimNot;
371 }
372
Ian Rogers936b37f2014-02-14 00:52:24 -0800373 bool IsParamALongOrDouble() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700374 Primitive::Type type = GetParamPrimitiveType();
375 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
376 }
377
378 uint64_t ReadSplitLongParam() const {
Nicolas Geoffray425f2392015-01-08 14:52:29 +0000379 // The splitted long is always available through the stack.
380 return *reinterpret_cast<uint64_t*>(stack_args_
381 + stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700382 }
383
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800384 void IncGprIndex() {
385 gpr_index_++;
386 if (kGprFprLockstep) {
387 fpr_index_++;
388 }
389 }
390
391 void IncFprIndex() {
392 fpr_index_++;
393 if (kGprFprLockstep) {
394 gpr_index_++;
395 }
396 }
397
Ian Rogers848871b2013-08-05 10:56:33 -0700398 void VisitArguments() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800399 // (a) 'stack_args_' should point to the first method's argument
400 // (b) whatever the argument type it is, the 'stack_index_' should
401 // be moved forward along with every visiting.
Ian Rogers936b37f2014-02-14 00:52:24 -0800402 gpr_index_ = 0;
403 fpr_index_ = 0;
Zheng Xu5667fdb2014-10-23 18:29:55 +0800404 if (kQuickDoubleRegAlignedFloatBackFilled) {
405 fpr_double_index_ = 0;
406 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800407 stack_index_ = 0;
408 if (!is_static_) { // Handle this.
409 cur_type_ = Primitive::kPrimNot;
410 is_split_long_or_double_ = false;
Ian Rogers848871b2013-08-05 10:56:33 -0700411 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800412 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800413 if (kNumQuickGprArgs > 0) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800414 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800415 }
Ian Rogers848871b2013-08-05 10:56:33 -0700416 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800417 for (uint32_t shorty_index = 1; shorty_index < shorty_len_; ++shorty_index) {
418 cur_type_ = Primitive::GetType(shorty_[shorty_index]);
419 switch (cur_type_) {
420 case Primitive::kPrimNot:
421 case Primitive::kPrimBoolean:
422 case Primitive::kPrimByte:
423 case Primitive::kPrimChar:
424 case Primitive::kPrimShort:
425 case Primitive::kPrimInt:
426 is_split_long_or_double_ = false;
427 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800428 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800429 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800430 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800431 }
432 break;
433 case Primitive::kPrimFloat:
434 is_split_long_or_double_ = false;
435 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800436 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800437 if (kQuickSoftFloatAbi) {
438 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800439 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800440 }
441 } else {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800442 if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800443 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800444 if (kQuickDoubleRegAlignedFloatBackFilled) {
445 // Double should not overlap with float.
446 // For example, if fpr_index_ = 3, fpr_double_index_ should be at least 4.
447 fpr_double_index_ = std::max(fpr_double_index_, RoundUp(fpr_index_, 2));
448 // Float should not overlap with double.
449 if (fpr_index_ % 2 == 0) {
450 fpr_index_ = std::max(fpr_double_index_, fpr_index_);
451 }
452 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800453 }
454 }
455 break;
456 case Primitive::kPrimDouble:
457 case Primitive::kPrimLong:
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800458 if (kQuickSoftFloatAbi || (cur_type_ == Primitive::kPrimLong)) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000459 if (cur_type_ == Primitive::kPrimLong && kAlignPairRegister && gpr_index_ == 0) {
460 // Currently, this is only for ARM, where the first available parameter register
461 // is R1. So we skip it, and use R2 instead.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800462 IncGprIndex();
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000463 }
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000464 is_split_long_or_double_ = (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) &&
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800465 ((gpr_index_ + 1) == kNumQuickGprArgs);
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500466 if (!kSplitPairAcrossRegisterAndStack && is_split_long_or_double_) {
467 // We don't want to split this. Pass over this register.
468 gpr_index_++;
469 is_split_long_or_double_ = false;
470 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800471 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800472 if (kBytesStackArgLocation == 4) {
473 stack_index_+= 2;
474 } else {
475 CHECK_EQ(kBytesStackArgLocation, 8U);
476 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800477 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700478 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800479 IncGprIndex();
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000480 if (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700481 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800482 IncGprIndex();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700483 }
484 }
485 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800486 } else {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000487 is_split_long_or_double_ = (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) &&
Zheng Xu5667fdb2014-10-23 18:29:55 +0800488 ((fpr_index_ + 1) == kNumQuickFprArgs) && !kQuickDoubleRegAlignedFloatBackFilled;
Ian Rogers936b37f2014-02-14 00:52:24 -0800489 Visit();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700490 if (kBytesStackArgLocation == 4) {
491 stack_index_+= 2;
Ian Rogers936b37f2014-02-14 00:52:24 -0800492 } else {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700493 CHECK_EQ(kBytesStackArgLocation, 8U);
494 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800495 }
Zheng Xu5667fdb2014-10-23 18:29:55 +0800496 if (kQuickDoubleRegAlignedFloatBackFilled) {
497 if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) {
498 fpr_double_index_ += 2;
499 // Float should not overlap with double.
500 if (fpr_index_ % 2 == 0) {
501 fpr_index_ = std::max(fpr_double_index_, fpr_index_);
502 }
503 }
504 } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800505 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800506 if (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) {
507 if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800508 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800509 }
510 }
511 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800512 }
513 break;
514 default:
515 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty_;
516 }
Ian Rogers848871b2013-08-05 10:56:33 -0700517 }
518 }
519
Andreas Gampec200a4a2014-06-16 18:39:09 -0700520 protected:
Ian Rogers848871b2013-08-05 10:56:33 -0700521 const bool is_static_;
522 const char* const shorty_;
523 const uint32_t shorty_len_;
Andreas Gampec200a4a2014-06-16 18:39:09 -0700524
525 private:
Ian Rogers13735952014-10-08 12:43:28 -0700526 uint8_t* const gpr_args_; // Address of GPR arguments in callee save frame.
527 uint8_t* const fpr_args_; // Address of FPR arguments in callee save frame.
528 uint8_t* const stack_args_; // Address of stack arguments in caller's frame.
Ian Rogers936b37f2014-02-14 00:52:24 -0800529 uint32_t gpr_index_; // Index into spilled GPRs.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800530 // Index into spilled FPRs.
531 // In case kQuickDoubleRegAlignedFloatBackFilled, it may index a hole while fpr_double_index_
532 // holds a higher register number.
533 uint32_t fpr_index_;
534 // Index into spilled FPRs for aligned double.
535 // Only used when kQuickDoubleRegAlignedFloatBackFilled. Next available double register indexed in
536 // terms of singles, may be behind fpr_index.
537 uint32_t fpr_double_index_;
Ian Rogers936b37f2014-02-14 00:52:24 -0800538 uint32_t stack_index_; // Index into arguments on the stack.
539 // The current type of argument during VisitArguments.
540 Primitive::Type cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700541 // Does a 64bit parameter straddle the register and stack arguments?
542 bool is_split_long_or_double_;
543};
544
Sebastien Hertza836bc92014-11-25 16:30:53 +0100545// Returns the 'this' object of a proxy method. This function is only used by StackVisitor. It
546// allows to use the QuickArgumentVisitor constants without moving all the code in its own module.
547extern "C" mirror::Object* artQuickGetProxyThisObject(StackReference<mirror::ArtMethod>* sp)
548 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
549 return QuickArgumentVisitor::GetProxyThisObject(sp);
550}
551
Ian Rogers848871b2013-08-05 10:56:33 -0700552// Visits arguments on the stack placing them into the shadow frame.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800553class BuildQuickShadowFrameVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700554 public:
Andreas Gampecf4035a2014-05-28 22:43:01 -0700555 BuildQuickShadowFrameVisitor(StackReference<mirror::ArtMethod>* sp, bool is_static,
556 const char* shorty, uint32_t shorty_len, ShadowFrame* sf,
557 size_t first_arg_reg) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700558 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), sf_(sf), cur_reg_(first_arg_reg) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700559
Ian Rogers9758f792014-03-13 09:02:55 -0700560 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700561
562 private:
Ian Rogers936b37f2014-02-14 00:52:24 -0800563 ShadowFrame* const sf_;
564 uint32_t cur_reg_;
Ian Rogers848871b2013-08-05 10:56:33 -0700565
Dragos Sbirleabd136a22013-08-13 18:07:04 -0700566 DISALLOW_COPY_AND_ASSIGN(BuildQuickShadowFrameVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700567};
568
Andreas Gampec200a4a2014-06-16 18:39:09 -0700569void BuildQuickShadowFrameVisitor::Visit() {
Ian Rogers9758f792014-03-13 09:02:55 -0700570 Primitive::Type type = GetParamPrimitiveType();
571 switch (type) {
572 case Primitive::kPrimLong: // Fall-through.
573 case Primitive::kPrimDouble:
574 if (IsSplitLongOrDouble()) {
575 sf_->SetVRegLong(cur_reg_, ReadSplitLongParam());
576 } else {
577 sf_->SetVRegLong(cur_reg_, *reinterpret_cast<jlong*>(GetParamAddress()));
578 }
579 ++cur_reg_;
580 break;
581 case Primitive::kPrimNot: {
582 StackReference<mirror::Object>* stack_ref =
583 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
584 sf_->SetVRegReference(cur_reg_, stack_ref->AsMirrorPtr());
585 }
586 break;
587 case Primitive::kPrimBoolean: // Fall-through.
588 case Primitive::kPrimByte: // Fall-through.
589 case Primitive::kPrimChar: // Fall-through.
590 case Primitive::kPrimShort: // Fall-through.
591 case Primitive::kPrimInt: // Fall-through.
592 case Primitive::kPrimFloat:
593 sf_->SetVReg(cur_reg_, *reinterpret_cast<jint*>(GetParamAddress()));
594 break;
595 case Primitive::kPrimVoid:
596 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700597 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -0700598 }
599 ++cur_reg_;
600}
601
Brian Carlstromea46f952013-07-30 01:26:50 -0700602extern "C" uint64_t artQuickToInterpreterBridge(mirror::ArtMethod* method, Thread* self,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700603 StackReference<mirror::ArtMethod>* sp)
Ian Rogers848871b2013-08-05 10:56:33 -0700604 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
605 // Ensure we don't get thread suspension until the object arguments are safely in the shadow
606 // frame.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700607 ScopedQuickEntrypointChecks sqec(self);
Ian Rogers848871b2013-08-05 10:56:33 -0700608
609 if (method->IsAbstract()) {
610 ThrowAbstractMethodError(method);
611 return 0;
612 } else {
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800613 DCHECK(!method->IsNative()) << PrettyMethod(method);
Andreas Gampec200a4a2014-06-16 18:39:09 -0700614 const char* old_cause = self->StartAssertNoThreadSuspension(
615 "Building interpreter shadow frame");
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700616 const DexFile::CodeItem* code_item = method->GetCodeItem();
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800617 DCHECK(code_item != nullptr) << PrettyMethod(method);
Ian Rogers848871b2013-08-05 10:56:33 -0700618 uint16_t num_regs = code_item->registers_size_;
619 void* memory = alloca(ShadowFrame::ComputeSize(num_regs));
Andreas Gampec200a4a2014-06-16 18:39:09 -0700620 // No last shadow coming from quick.
621 ShadowFrame* shadow_frame(ShadowFrame::Create(num_regs, nullptr, method, 0, memory));
Ian Rogers848871b2013-08-05 10:56:33 -0700622 size_t first_arg_reg = code_item->registers_size_ - code_item->ins_size_;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700623 uint32_t shorty_len = 0;
624 const char* shorty = method->GetShorty(&shorty_len);
625 BuildQuickShadowFrameVisitor shadow_frame_builder(sp, method->IsStatic(), shorty, shorty_len,
Ian Rogers936b37f2014-02-14 00:52:24 -0800626 shadow_frame, first_arg_reg);
Ian Rogers848871b2013-08-05 10:56:33 -0700627 shadow_frame_builder.VisitArguments();
Ian Rogerse94652f2014-12-02 11:13:19 -0800628 const bool needs_initialization =
629 method->IsStatic() && !method->GetDeclaringClass()->IsInitialized();
Ian Rogers848871b2013-08-05 10:56:33 -0700630 // Push a transition back into managed code onto the linked list in thread.
631 ManagedStack fragment;
632 self->PushManagedStackFragment(&fragment);
633 self->PushShadowFrame(shadow_frame);
634 self->EndAssertNoThreadSuspension(old_cause);
635
Ian Rogerse94652f2014-12-02 11:13:19 -0800636 if (needs_initialization) {
Ian Rogers848871b2013-08-05 10:56:33 -0700637 // Ensure static method's class is initialized.
Ian Rogerse94652f2014-12-02 11:13:19 -0800638 StackHandleScope<1> hs(self);
639 Handle<mirror::Class> h_class(hs.NewHandle(shadow_frame->GetMethod()->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700640 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
Ian Rogerse94652f2014-12-02 11:13:19 -0800641 DCHECK(Thread::Current()->IsExceptionPending()) << PrettyMethod(shadow_frame->GetMethod());
Ian Rogers848871b2013-08-05 10:56:33 -0700642 self->PopManagedStackFragment(fragment);
643 return 0;
644 }
645 }
Ian Rogerse94652f2014-12-02 11:13:19 -0800646 JValue result = interpreter::EnterInterpreterFromEntryPoint(self, code_item, shadow_frame);
Ian Rogers848871b2013-08-05 10:56:33 -0700647 // Pop transition.
648 self->PopManagedStackFragment(fragment);
Daniel Mihalyieb076692014-08-22 17:33:31 +0200649
650 // Request a stack deoptimization if needed
651 mirror::ArtMethod* caller = QuickArgumentVisitor::GetCallingMethod(sp);
652 if (UNLIKELY(Dbg::IsForcedInterpreterNeededForUpcall(self, caller))) {
653 self->SetException(Thread::GetDeoptimizationException());
654 self->SetDeoptimizationReturnValue(result);
655 }
656
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800657 // No need to restore the args since the method has already been run by the interpreter.
Ian Rogers848871b2013-08-05 10:56:33 -0700658 return result.GetJ();
659 }
660}
661
662// Visits arguments on the stack placing them into the args vector, Object* arguments are converted
663// to jobjects.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800664class BuildQuickArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700665 public:
Andreas Gampecf4035a2014-05-28 22:43:01 -0700666 BuildQuickArgumentVisitor(StackReference<mirror::ArtMethod>* sp, bool is_static,
667 const char* shorty, uint32_t shorty_len,
668 ScopedObjectAccessUnchecked* soa, std::vector<jvalue>* args) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700669 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa), args_(args) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700670
Ian Rogers9758f792014-03-13 09:02:55 -0700671 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700672
Ian Rogers9758f792014-03-13 09:02:55 -0700673 void FixupReferences() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800674
Ian Rogers848871b2013-08-05 10:56:33 -0700675 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700676 ScopedObjectAccessUnchecked* const soa_;
677 std::vector<jvalue>* const args_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800678 // References which we must update when exiting in case the GC moved the objects.
Ian Rogers700a4022014-05-19 16:49:03 -0700679 std::vector<std::pair<jobject, StackReference<mirror::Object>*>> references_;
Ian Rogers9758f792014-03-13 09:02:55 -0700680
Ian Rogers848871b2013-08-05 10:56:33 -0700681 DISALLOW_COPY_AND_ASSIGN(BuildQuickArgumentVisitor);
682};
683
Ian Rogers9758f792014-03-13 09:02:55 -0700684void BuildQuickArgumentVisitor::Visit() {
685 jvalue val;
686 Primitive::Type type = GetParamPrimitiveType();
687 switch (type) {
688 case Primitive::kPrimNot: {
689 StackReference<mirror::Object>* stack_ref =
690 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
691 val.l = soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
692 references_.push_back(std::make_pair(val.l, stack_ref));
693 break;
694 }
695 case Primitive::kPrimLong: // Fall-through.
696 case Primitive::kPrimDouble:
697 if (IsSplitLongOrDouble()) {
698 val.j = ReadSplitLongParam();
699 } else {
700 val.j = *reinterpret_cast<jlong*>(GetParamAddress());
701 }
702 break;
703 case Primitive::kPrimBoolean: // Fall-through.
704 case Primitive::kPrimByte: // Fall-through.
705 case Primitive::kPrimChar: // Fall-through.
706 case Primitive::kPrimShort: // Fall-through.
707 case Primitive::kPrimInt: // Fall-through.
708 case Primitive::kPrimFloat:
709 val.i = *reinterpret_cast<jint*>(GetParamAddress());
710 break;
711 case Primitive::kPrimVoid:
712 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700713 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -0700714 }
715 args_->push_back(val);
716}
717
718void BuildQuickArgumentVisitor::FixupReferences() {
719 // Fixup any references which may have changed.
720 for (const auto& pair : references_) {
721 pair.second->Assign(soa_->Decode<mirror::Object*>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -0700722 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -0700723 }
724}
725
Ian Rogers848871b2013-08-05 10:56:33 -0700726// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
727// which is responsible for recording callee save registers. We explicitly place into jobjects the
728// incoming reference arguments (so they survive GC). We invoke the invocation handler, which is a
729// field within the proxy object, which will box the primitive arguments and deal with error cases.
Brian Carlstromea46f952013-07-30 01:26:50 -0700730extern "C" uint64_t artQuickProxyInvokeHandler(mirror::ArtMethod* proxy_method,
Ian Rogers848871b2013-08-05 10:56:33 -0700731 mirror::Object* receiver,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700732 Thread* self, StackReference<mirror::ArtMethod>* sp)
Ian Rogers848871b2013-08-05 10:56:33 -0700733 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromd3633d52013-08-20 21:06:26 -0700734 DCHECK(proxy_method->IsProxyMethod()) << PrettyMethod(proxy_method);
735 DCHECK(receiver->GetClass()->IsProxyClass()) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700736 // Ensure we don't get thread suspension until the object arguments are safely in jobjects.
737 const char* old_cause =
738 self->StartAssertNoThreadSuspension("Adding to IRT proxy object arguments");
739 // Register the top of the managed stack, making stack crawlable.
Jeff Haof0a3f092014-07-24 16:26:09 -0700740 DCHECK_EQ(sp->AsMirrorPtr(), proxy_method) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700741 DCHECK_EQ(proxy_method->GetFrameSizeInBytes(),
Brian Carlstromd3633d52013-08-20 21:06:26 -0700742 Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes())
743 << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700744 self->VerifyStack();
745 // Start new JNI local reference state.
746 JNIEnvExt* env = self->GetJniEnv();
747 ScopedObjectAccessUnchecked soa(env);
748 ScopedJniEnvLocalRefState env_state(env);
749 // Create local ref. copies of proxy method and the receiver.
750 jobject rcvr_jobj = soa.AddLocalReference<jobject>(receiver);
751
752 // Placing arguments into args vector and remove the receiver.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700753 mirror::ArtMethod* non_proxy_method = proxy_method->GetInterfaceMethodIfProxy();
754 CHECK(!non_proxy_method->IsStatic()) << PrettyMethod(proxy_method) << " "
Andreas Gampec200a4a2014-06-16 18:39:09 -0700755 << PrettyMethod(non_proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700756 std::vector<jvalue> args;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700757 uint32_t shorty_len = 0;
758 const char* shorty = proxy_method->GetShorty(&shorty_len);
759 BuildQuickArgumentVisitor local_ref_visitor(sp, false, shorty, shorty_len, &soa, &args);
Brian Carlstromd3633d52013-08-20 21:06:26 -0700760
Ian Rogers848871b2013-08-05 10:56:33 -0700761 local_ref_visitor.VisitArguments();
Brian Carlstromd3633d52013-08-20 21:06:26 -0700762 DCHECK_GT(args.size(), 0U) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700763 args.erase(args.begin());
764
765 // Convert proxy method into expected interface method.
Brian Carlstromea46f952013-07-30 01:26:50 -0700766 mirror::ArtMethod* interface_method = proxy_method->FindOverriddenMethod();
Ian Rogerse0a02da2014-12-02 14:10:53 -0800767 DCHECK(interface_method != nullptr) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700768 DCHECK(!interface_method->IsProxyMethod()) << PrettyMethod(interface_method);
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700769 self->EndAssertNoThreadSuspension(old_cause);
770 jobject interface_method_jobj = soa.AddLocalReference<jobject>(
771 mirror::Method::CreateFromArtMethod(soa.Self(), interface_method));
Ian Rogers848871b2013-08-05 10:56:33 -0700772
773 // All naked Object*s should now be in jobjects, so its safe to go into the main invoke code
774 // that performs allocations.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700775 JValue result = InvokeProxyInvocationHandler(soa, shorty, rcvr_jobj, interface_method_jobj, args);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800776 // Restore references which might have moved.
777 local_ref_visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -0700778 return result.GetJ();
779}
780
781// Read object references held in arguments from quick frames and place in a JNI local references,
782// so they don't get garbage collected.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800783class RememberForGcArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700784 public:
Andreas Gampecf4035a2014-05-28 22:43:01 -0700785 RememberForGcArgumentVisitor(StackReference<mirror::ArtMethod>* sp, bool is_static,
786 const char* shorty, uint32_t shorty_len,
787 ScopedObjectAccessUnchecked* soa) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700788 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700789
Ian Rogers9758f792014-03-13 09:02:55 -0700790 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
Mathieu Chartier07d447b2013-09-26 11:57:43 -0700791
Ian Rogers9758f792014-03-13 09:02:55 -0700792 void FixupReferences() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers848871b2013-08-05 10:56:33 -0700793
794 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700795 ScopedObjectAccessUnchecked* const soa_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800796 // References which we must update when exiting in case the GC moved the objects.
Andreas Gampec200a4a2014-06-16 18:39:09 -0700797 std::vector<std::pair<jobject, StackReference<mirror::Object>*> > references_;
798
Mathieu Chartier590fee92013-09-13 13:46:47 -0700799 DISALLOW_COPY_AND_ASSIGN(RememberForGcArgumentVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700800};
801
Ian Rogers9758f792014-03-13 09:02:55 -0700802void RememberForGcArgumentVisitor::Visit() {
803 if (IsParamAReference()) {
804 StackReference<mirror::Object>* stack_ref =
805 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
806 jobject reference =
807 soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
808 references_.push_back(std::make_pair(reference, stack_ref));
809 }
810}
811
812void RememberForGcArgumentVisitor::FixupReferences() {
813 // Fixup any references which may have changed.
814 for (const auto& pair : references_) {
815 pair.second->Assign(soa_->Decode<mirror::Object*>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -0700816 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -0700817 }
818}
819
Ian Rogers848871b2013-08-05 10:56:33 -0700820// Lazily resolve a method for quick. Called by stub code.
Brian Carlstromea46f952013-07-30 01:26:50 -0700821extern "C" const void* artQuickResolutionTrampoline(mirror::ArtMethod* called,
Ian Rogers848871b2013-08-05 10:56:33 -0700822 mirror::Object* receiver,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700823 Thread* self,
824 StackReference<mirror::ArtMethod>* sp)
Ian Rogers848871b2013-08-05 10:56:33 -0700825 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampe3b45ef22015-05-26 21:34:09 -0700826 // The resolution trampoline stashes the resolved method into the callee-save frame to transport
827 // it. Thus, when exiting, the stack cannot be verified (as the resolved method most likely
828 // does not have the same stack layout as the callee-save method).
829 ScopedQuickEntrypointChecks sqec(self, kIsDebugBuild, false);
Ian Rogers848871b2013-08-05 10:56:33 -0700830 // Start new JNI local reference state
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800831 JNIEnvExt* env = self->GetJniEnv();
Ian Rogers848871b2013-08-05 10:56:33 -0700832 ScopedObjectAccessUnchecked soa(env);
833 ScopedJniEnvLocalRefState env_state(env);
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800834 const char* old_cause = self->StartAssertNoThreadSuspension("Quick method resolution set up");
Ian Rogers848871b2013-08-05 10:56:33 -0700835
836 // Compute details about the called method (avoid GCs)
837 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Ian Rogers848871b2013-08-05 10:56:33 -0700838 InvokeType invoke_type;
Ian Rogerse0a02da2014-12-02 14:10:53 -0800839 MethodReference called_method(nullptr, 0);
840 const bool called_method_known_on_entry = !called->IsRuntimeMethod();
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +0100841 mirror::ArtMethod* caller = nullptr;
Ian Rogerse0a02da2014-12-02 14:10:53 -0800842 if (!called_method_known_on_entry) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +0100843 caller = QuickArgumentVisitor::GetCallingMethod(sp);
844 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
Ian Rogers848871b2013-08-05 10:56:33 -0700845 const DexFile::CodeItem* code;
Ian Rogerse0a02da2014-12-02 14:10:53 -0800846 called_method.dex_file = caller->GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700847 code = caller->GetCodeItem();
Ian Rogers848871b2013-08-05 10:56:33 -0700848 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
849 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
850 Instruction::Code instr_code = instr->Opcode();
851 bool is_range;
852 switch (instr_code) {
853 case Instruction::INVOKE_DIRECT:
854 invoke_type = kDirect;
855 is_range = false;
856 break;
857 case Instruction::INVOKE_DIRECT_RANGE:
858 invoke_type = kDirect;
859 is_range = true;
860 break;
861 case Instruction::INVOKE_STATIC:
862 invoke_type = kStatic;
863 is_range = false;
864 break;
865 case Instruction::INVOKE_STATIC_RANGE:
866 invoke_type = kStatic;
867 is_range = true;
868 break;
869 case Instruction::INVOKE_SUPER:
870 invoke_type = kSuper;
871 is_range = false;
872 break;
873 case Instruction::INVOKE_SUPER_RANGE:
874 invoke_type = kSuper;
875 is_range = true;
876 break;
877 case Instruction::INVOKE_VIRTUAL:
878 invoke_type = kVirtual;
879 is_range = false;
880 break;
881 case Instruction::INVOKE_VIRTUAL_RANGE:
882 invoke_type = kVirtual;
883 is_range = true;
884 break;
885 case Instruction::INVOKE_INTERFACE:
886 invoke_type = kInterface;
887 is_range = false;
888 break;
889 case Instruction::INVOKE_INTERFACE_RANGE:
890 invoke_type = kInterface;
891 is_range = true;
892 break;
893 default:
Ian Rogerse0a02da2014-12-02 14:10:53 -0800894 LOG(FATAL) << "Unexpected call into trampoline: " << instr->DumpString(nullptr);
895 UNREACHABLE();
Ian Rogers848871b2013-08-05 10:56:33 -0700896 }
Ian Rogerse0a02da2014-12-02 14:10:53 -0800897 called_method.dex_method_index = (is_range) ? instr->VRegB_3rc() : instr->VRegB_35c();
Ian Rogers848871b2013-08-05 10:56:33 -0700898 } else {
899 invoke_type = kStatic;
Ian Rogerse0a02da2014-12-02 14:10:53 -0800900 called_method.dex_file = called->GetDexFile();
901 called_method.dex_method_index = called->GetDexMethodIndex();
Ian Rogers848871b2013-08-05 10:56:33 -0700902 }
903 uint32_t shorty_len;
904 const char* shorty =
Ian Rogerse0a02da2014-12-02 14:10:53 -0800905 called_method.dex_file->GetMethodShorty(
906 called_method.dex_file->GetMethodId(called_method.dex_method_index), &shorty_len);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700907 RememberForGcArgumentVisitor visitor(sp, invoke_type == kStatic, shorty, shorty_len, &soa);
Ian Rogers848871b2013-08-05 10:56:33 -0700908 visitor.VisitArguments();
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800909 self->EndAssertNoThreadSuspension(old_cause);
Ian Rogerse0a02da2014-12-02 14:10:53 -0800910 const bool virtual_or_interface = invoke_type == kVirtual || invoke_type == kInterface;
Ian Rogers848871b2013-08-05 10:56:33 -0700911 // Resolve method filling in dex cache.
Ian Rogerse0a02da2014-12-02 14:10:53 -0800912 if (!called_method_known_on_entry) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700913 StackHandleScope<1> hs(self);
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700914 mirror::Object* dummy = nullptr;
915 HandleWrapper<mirror::Object> h_receiver(
916 hs.NewHandleWrapper(virtual_or_interface ? &receiver : &dummy));
Ian Rogerse0a02da2014-12-02 14:10:53 -0800917 DCHECK_EQ(caller->GetDexFile(), called_method.dex_file);
918 called = linker->ResolveMethod(self, called_method.dex_method_index, &caller, invoke_type);
Ian Rogers848871b2013-08-05 10:56:33 -0700919 }
Ian Rogerse0a02da2014-12-02 14:10:53 -0800920 const void* code = nullptr;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800921 if (LIKELY(!self->IsExceptionPending())) {
Ian Rogers848871b2013-08-05 10:56:33 -0700922 // Incompatible class change should have been handled in resolve method.
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800923 CHECK(!called->CheckIncompatibleClassChange(invoke_type))
924 << PrettyMethod(called) << " " << invoke_type;
Mathieu Chartier55871bf2014-02-27 10:24:50 -0800925 if (virtual_or_interface) {
926 // Refine called method based on receiver.
927 CHECK(receiver != nullptr) << invoke_type;
Mingyao Yangf4867782014-05-05 11:55:02 -0700928
929 mirror::ArtMethod* orig_called = called;
Mathieu Chartier55871bf2014-02-27 10:24:50 -0800930 if (invoke_type == kVirtual) {
931 called = receiver->GetClass()->FindVirtualMethodForVirtual(called);
932 } else {
933 called = receiver->GetClass()->FindVirtualMethodForInterface(called);
934 }
Mingyao Yangf4867782014-05-05 11:55:02 -0700935
936 CHECK(called != nullptr) << PrettyMethod(orig_called) << " "
937 << PrettyTypeOf(receiver) << " "
938 << invoke_type << " " << orig_called->GetVtableIndex();
939
Ian Rogers83883d72013-10-21 21:07:24 -0700940 // 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 -0800941 // of the sharpened method avoiding dirtying the dex cache if possible.
Ian Rogers00f15272014-12-02 16:55:46 -0800942 // Note, called_method.dex_method_index references the dex method before the
943 // FindVirtualMethodFor... This is ok for FindDexMethodIndexInOtherDexFile that only cares
944 // about the name and signature.
945 uint32_t update_dex_cache_method_index = called->GetDexMethodIndex();
Ian Rogerse0a02da2014-12-02 14:10:53 -0800946 if (!called->HasSameDexCacheResolvedMethods(caller)) {
Ian Rogers83883d72013-10-21 21:07:24 -0700947 // Calling from one dex file to another, need to compute the method index appropriate to
Vladimir Markobbcc0c02014-02-03 14:08:42 +0000948 // the caller's dex file. Since we get here only if the original called was a runtime
949 // method, we've got the correct dex_file and a dex_method_idx from above.
Ian Rogerse0a02da2014-12-02 14:10:53 -0800950 DCHECK(!called_method_known_on_entry);
951 DCHECK_EQ(caller->GetDexFile(), called_method.dex_file);
952 const DexFile* caller_dex_file = called_method.dex_file;
953 uint32_t caller_method_name_and_sig_index = called_method.dex_method_index;
954 update_dex_cache_method_index =
955 called->FindDexMethodIndexInOtherDexFile(*caller_dex_file,
956 caller_method_name_and_sig_index);
957 }
958 if ((update_dex_cache_method_index != DexFile::kDexNoIndex) &&
959 (caller->GetDexCacheResolvedMethod(update_dex_cache_method_index) != called)) {
960 caller->SetDexCacheResolvedMethod(update_dex_cache_method_index, called);
Ian Rogers83883d72013-10-21 21:07:24 -0700961 }
Mathieu Chartiere4a91bb2015-01-28 13:11:44 -0800962 } else if (invoke_type == kStatic) {
963 const auto called_dex_method_idx = called->GetDexMethodIndex();
964 // For static invokes, we may dispatch to the static method in the superclass but resolve
965 // using the subclass. To prevent getting slow paths on each invoke, we force set the
966 // resolved method for the super class dex method index if we are in the same dex file.
967 // b/19175856
968 if (called->GetDexFile() == called_method.dex_file &&
969 called_method.dex_method_index != called_dex_method_idx) {
970 called->GetDexCache()->SetResolvedMethod(called_dex_method_idx, called);
971 }
Ian Rogers83883d72013-10-21 21:07:24 -0700972 }
Daniel Mihalyieb076692014-08-22 17:33:31 +0200973
Ian Rogers848871b2013-08-05 10:56:33 -0700974 // Ensure that the called method's class is initialized.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700975 StackHandleScope<1> hs(soa.Self());
976 Handle<mirror::Class> called_class(hs.NewHandle(called->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700977 linker->EnsureInitialized(soa.Self(), called_class, true, true);
Ian Rogers848871b2013-08-05 10:56:33 -0700978 if (LIKELY(called_class->IsInitialized())) {
Daniel Mihalyieb076692014-08-22 17:33:31 +0200979 if (UNLIKELY(Dbg::IsForcedInterpreterNeededForResolution(self, called))) {
980 // If we are single-stepping or the called method is deoptimized (by a
981 // breakpoint, for example), then we have to execute the called method
982 // with the interpreter.
983 code = GetQuickToInterpreterBridge();
984 } else if (UNLIKELY(Dbg::IsForcedInstrumentationNeededForResolution(self, caller))) {
985 // If the caller is deoptimized (by a breakpoint, for example), we have to
986 // continue its execution with interpreter when returning from the called
987 // method. Because we do not want to execute the called method with the
988 // interpreter, we wrap its execution into the instrumentation stubs.
989 // When the called method returns, it will execute the instrumentation
990 // exit hook that will determine the need of the interpreter with a call
991 // to Dbg::IsForcedInterpreterNeededForUpcall and deoptimize the stack if
992 // it is needed.
993 code = GetQuickInstrumentationEntryPoint();
994 } else {
995 code = called->GetEntryPointFromQuickCompiledCode();
996 }
Ian Rogers848871b2013-08-05 10:56:33 -0700997 } else if (called_class->IsInitializing()) {
Daniel Mihalyieb076692014-08-22 17:33:31 +0200998 if (UNLIKELY(Dbg::IsForcedInterpreterNeededForResolution(self, called))) {
999 // If we are single-stepping or the called method is deoptimized (by a
1000 // breakpoint, for example), then we have to execute the called method
1001 // with the interpreter.
1002 code = GetQuickToInterpreterBridge();
1003 } else if (invoke_type == kStatic) {
Ian Rogers848871b2013-08-05 10:56:33 -07001004 // Class is still initializing, go to oat and grab code (trampoline must be left in place
1005 // until class is initialized to stop races between threads).
Ian Rogersef7d42f2014-01-06 12:55:46 -08001006 code = linker->GetQuickOatCodeFor(called);
Ian Rogers848871b2013-08-05 10:56:33 -07001007 } else {
1008 // No trampoline for non-static methods.
Ian Rogersef7d42f2014-01-06 12:55:46 -08001009 code = called->GetEntryPointFromQuickCompiledCode();
Ian Rogers848871b2013-08-05 10:56:33 -07001010 }
1011 } else {
1012 DCHECK(called_class->IsErroneous());
1013 }
1014 }
Ian Rogerse0a02da2014-12-02 14:10:53 -08001015 CHECK_EQ(code == nullptr, self->IsExceptionPending());
Mathieu Chartier07d447b2013-09-26 11:57:43 -07001016 // Fixup any locally saved objects may have moved during a GC.
1017 visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -07001018 // Place called method in callee-save frame to be placed as first argument to quick method.
Andreas Gampecf4035a2014-05-28 22:43:01 -07001019 sp->Assign(called);
Ian Rogers848871b2013-08-05 10:56:33 -07001020 return code;
1021}
1022
Andreas Gampec147b002014-03-06 18:11:06 -08001023/*
1024 * This class uses a couple of observations to unite the different calling conventions through
1025 * a few constants.
1026 *
1027 * 1) Number of registers used for passing is normally even, so counting down has no penalty for
1028 * possible alignment.
1029 * 2) Known 64b architectures store 8B units on the stack, both for integral and floating point
1030 * types, so using uintptr_t is OK. Also means that we can use kRegistersNeededX to denote
1031 * when we have to split things
1032 * 3) The only soft-float, Arm, is 32b, so no widening needs to be taken into account for floats
1033 * and we can use Int handling directly.
1034 * 4) Only 64b architectures widen, and their stack is aligned 8B anyways, so no padding code
1035 * necessary when widening. Also, widening of Ints will take place implicitly, and the
1036 * extension should be compatible with Aarch64, which mandates copying the available bits
1037 * into LSB and leaving the rest unspecified.
1038 * 5) Aligning longs and doubles is necessary on arm only, and it's the same in registers and on
1039 * the stack.
1040 * 6) There is only little endian.
1041 *
1042 *
1043 * Actual work is supposed to be done in a delegate of the template type. The interface is as
1044 * follows:
1045 *
1046 * void PushGpr(uintptr_t): Add a value for the next GPR
1047 *
1048 * void PushFpr4(float): Add a value for the next FPR of size 32b. Is only called if we need
1049 * padding, that is, think the architecture is 32b and aligns 64b.
1050 *
1051 * void PushFpr8(uint64_t): Push a double. We _will_ call this on 32b, it's the callee's job to
1052 * split this if necessary. The current state will have aligned, if
1053 * necessary.
1054 *
1055 * void PushStack(uintptr_t): Push a value to the stack.
1056 *
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001057 * uintptr_t PushHandleScope(mirror::Object* ref): Add a reference to the HandleScope. This _will_ have nullptr,
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001058 * as this might be important for null initialization.
Andreas Gampec147b002014-03-06 18:11:06 -08001059 * Must return the jobject, that is, the reference to the
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001060 * entry in the HandleScope (nullptr if necessary).
Andreas Gampec147b002014-03-06 18:11:06 -08001061 *
1062 */
Andreas Gampec200a4a2014-06-16 18:39:09 -07001063template<class T> class BuildNativeCallFrameStateMachine {
Andreas Gampec147b002014-03-06 18:11:06 -08001064 public:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001065#if defined(__arm__)
1066 // TODO: These are all dummy values!
Andreas Gampec147b002014-03-06 18:11:06 -08001067 static constexpr bool kNativeSoftFloatAbi = true;
1068 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs, r0-r3
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001069 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
1070
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001071 static constexpr size_t kRegistersNeededForLong = 2;
1072 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -08001073 static constexpr bool kMultiRegistersAligned = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001074 static constexpr bool kMultiFPRegistersWidened = false;
1075 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001076 static constexpr bool kAlignLongOnStack = true;
1077 static constexpr bool kAlignDoubleOnStack = true;
Stuart Monteithb95a5342014-03-12 13:32:32 +00001078#elif defined(__aarch64__)
1079 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
1080 static constexpr size_t kNumNativeGprArgs = 8; // 6 arguments passed in GPRs.
1081 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
1082
1083 static constexpr size_t kRegistersNeededForLong = 1;
1084 static constexpr size_t kRegistersNeededForDouble = 1;
1085 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001086 static constexpr bool kMultiFPRegistersWidened = false;
1087 static constexpr bool kMultiGPRegistersWidened = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +00001088 static constexpr bool kAlignLongOnStack = false;
1089 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001090#elif defined(__mips__) && !defined(__LP64__)
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001091 static constexpr bool kNativeSoftFloatAbi = true; // This is a hard float ABI.
Douglas Leung735b8552014-10-31 12:21:40 -07001092 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs.
1093 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001094
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001095 static constexpr size_t kRegistersNeededForLong = 2;
1096 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -08001097 static constexpr bool kMultiRegistersAligned = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001098 static constexpr bool kMultiFPRegistersWidened = true;
1099 static constexpr bool kMultiGPRegistersWidened = false;
Douglas Leung735b8552014-10-31 12:21:40 -07001100 static constexpr bool kAlignLongOnStack = true;
1101 static constexpr bool kAlignDoubleOnStack = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001102#elif defined(__mips__) && defined(__LP64__)
1103 // Let the code prepare GPRs only and we will load the FPRs with same data.
1104 static constexpr bool kNativeSoftFloatAbi = true;
1105 static constexpr size_t kNumNativeGprArgs = 8;
1106 static constexpr size_t kNumNativeFprArgs = 0;
1107
1108 static constexpr size_t kRegistersNeededForLong = 1;
1109 static constexpr size_t kRegistersNeededForDouble = 1;
1110 static constexpr bool kMultiRegistersAligned = false;
1111 static constexpr bool kMultiFPRegistersWidened = false;
1112 static constexpr bool kMultiGPRegistersWidened = true;
1113 static constexpr bool kAlignLongOnStack = false;
1114 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001115#elif defined(__i386__)
1116 // TODO: Check these!
Andreas Gampec147b002014-03-06 18:11:06 -08001117 static constexpr bool kNativeSoftFloatAbi = false; // Not using int registers for fp
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001118 static constexpr size_t kNumNativeGprArgs = 0; // 6 arguments passed in GPRs.
1119 static constexpr size_t kNumNativeFprArgs = 0; // 8 arguments passed in FPRs.
1120
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001121 static constexpr size_t kRegistersNeededForLong = 2;
1122 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001123 static constexpr bool kMultiRegistersAligned = false; // x86 not using regs, anyways
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001124 static constexpr bool kMultiFPRegistersWidened = false;
1125 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001126 static constexpr bool kAlignLongOnStack = false;
1127 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001128#elif defined(__x86_64__)
1129 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
1130 static constexpr size_t kNumNativeGprArgs = 6; // 6 arguments passed in GPRs.
1131 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
1132
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001133 static constexpr size_t kRegistersNeededForLong = 1;
1134 static constexpr size_t kRegistersNeededForDouble = 1;
Andreas Gampec147b002014-03-06 18:11:06 -08001135 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001136 static constexpr bool kMultiFPRegistersWidened = false;
1137 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001138 static constexpr bool kAlignLongOnStack = false;
1139 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001140#else
1141#error "Unsupported architecture"
1142#endif
1143
Andreas Gampec147b002014-03-06 18:11:06 -08001144 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001145 explicit BuildNativeCallFrameStateMachine(T* delegate)
1146 : gpr_index_(kNumNativeGprArgs),
1147 fpr_index_(kNumNativeFprArgs),
1148 stack_entries_(0),
1149 delegate_(delegate) {
Andreas Gampec147b002014-03-06 18:11:06 -08001150 // For register alignment, we want to assume that counters (gpr_index_, fpr_index_) are even iff
1151 // the next register is even; counting down is just to make the compiler happy...
Andreas Gampe575e78c2014-11-03 23:41:03 -08001152 static_assert(kNumNativeGprArgs % 2 == 0U, "Number of native GPR arguments not even");
1153 static_assert(kNumNativeFprArgs % 2 == 0U, "Number of native FPR arguments not even");
Andreas Gampec147b002014-03-06 18:11:06 -08001154 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001155
Andreas Gampec200a4a2014-06-16 18:39:09 -07001156 virtual ~BuildNativeCallFrameStateMachine() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001157
Ian Rogers1428dce2014-10-21 15:02:15 -07001158 bool HavePointerGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001159 return gpr_index_ > 0;
1160 }
1161
Andreas Gampec200a4a2014-06-16 18:39:09 -07001162 void AdvancePointer(const void* val) {
Andreas Gampec147b002014-03-06 18:11:06 -08001163 if (HavePointerGpr()) {
1164 gpr_index_--;
1165 PushGpr(reinterpret_cast<uintptr_t>(val));
1166 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001167 stack_entries_++; // TODO: have a field for pointer length as multiple of 32b
Andreas Gampec147b002014-03-06 18:11:06 -08001168 PushStack(reinterpret_cast<uintptr_t>(val));
1169 gpr_index_ = 0;
1170 }
1171 }
1172
Ian Rogers1428dce2014-10-21 15:02:15 -07001173 bool HaveHandleScopeGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001174 return gpr_index_ > 0;
1175 }
1176
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001177 void AdvanceHandleScope(mirror::Object* ptr) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1178 uintptr_t handle = PushHandle(ptr);
1179 if (HaveHandleScopeGpr()) {
Andreas Gampec147b002014-03-06 18:11:06 -08001180 gpr_index_--;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001181 PushGpr(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001182 } else {
1183 stack_entries_++;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001184 PushStack(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001185 gpr_index_ = 0;
1186 }
1187 }
1188
Ian Rogers1428dce2014-10-21 15:02:15 -07001189 bool HaveIntGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001190 return gpr_index_ > 0;
1191 }
1192
1193 void AdvanceInt(uint32_t val) {
1194 if (HaveIntGpr()) {
1195 gpr_index_--;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001196 if (kMultiGPRegistersWidened) {
1197 DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001198 PushGpr(static_cast<int64_t>(bit_cast<int32_t, uint32_t>(val)));
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001199 } else {
1200 PushGpr(val);
1201 }
Andreas Gampec147b002014-03-06 18:11:06 -08001202 } else {
1203 stack_entries_++;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001204 if (kMultiGPRegistersWidened) {
1205 DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001206 PushStack(static_cast<int64_t>(bit_cast<int32_t, uint32_t>(val)));
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001207 } else {
1208 PushStack(val);
1209 }
Andreas Gampec147b002014-03-06 18:11:06 -08001210 gpr_index_ = 0;
1211 }
1212 }
1213
Ian Rogers1428dce2014-10-21 15:02:15 -07001214 bool HaveLongGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001215 return gpr_index_ >= kRegistersNeededForLong + (LongGprNeedsPadding() ? 1 : 0);
1216 }
1217
Ian Rogers1428dce2014-10-21 15:02:15 -07001218 bool LongGprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001219 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1220 kAlignLongOnStack && // and when it needs alignment
1221 (gpr_index_ & 1) == 1; // counter is odd, see constructor
1222 }
1223
Ian Rogers1428dce2014-10-21 15:02:15 -07001224 bool LongStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001225 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1226 kAlignLongOnStack && // and when it needs 8B alignment
1227 (stack_entries_ & 1) == 1; // counter is odd
1228 }
1229
1230 void AdvanceLong(uint64_t val) {
1231 if (HaveLongGpr()) {
1232 if (LongGprNeedsPadding()) {
1233 PushGpr(0);
1234 gpr_index_--;
1235 }
1236 if (kRegistersNeededForLong == 1) {
1237 PushGpr(static_cast<uintptr_t>(val));
1238 } else {
1239 PushGpr(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1240 PushGpr(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1241 }
1242 gpr_index_ -= kRegistersNeededForLong;
1243 } else {
1244 if (LongStackNeedsPadding()) {
1245 PushStack(0);
1246 stack_entries_++;
1247 }
1248 if (kRegistersNeededForLong == 1) {
1249 PushStack(static_cast<uintptr_t>(val));
1250 stack_entries_++;
1251 } else {
1252 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1253 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1254 stack_entries_ += 2;
1255 }
1256 gpr_index_ = 0;
1257 }
1258 }
1259
Ian Rogers1428dce2014-10-21 15:02:15 -07001260 bool HaveFloatFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001261 return fpr_index_ > 0;
1262 }
1263
Andreas Gampec147b002014-03-06 18:11:06 -08001264 void AdvanceFloat(float val) {
1265 if (kNativeSoftFloatAbi) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001266 AdvanceInt(bit_cast<uint32_t, float>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001267 } else {
1268 if (HaveFloatFpr()) {
1269 fpr_index_--;
1270 if (kRegistersNeededForDouble == 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001271 if (kMultiFPRegistersWidened) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001272 PushFpr8(bit_cast<uint64_t, double>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001273 } else {
1274 // No widening, just use the bits.
Roland Levillainda4d79b2015-03-24 14:36:11 +00001275 PushFpr8(static_cast<uint64_t>(bit_cast<uint32_t, float>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001276 }
1277 } else {
1278 PushFpr4(val);
1279 }
1280 } else {
1281 stack_entries_++;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001282 if (kRegistersNeededForDouble == 1 && kMultiFPRegistersWidened) {
Andreas Gampec147b002014-03-06 18:11:06 -08001283 // Need to widen before storing: Note the "double" in the template instantiation.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001284 // Note: We need to jump through those hoops to make the compiler happy.
1285 DCHECK_EQ(sizeof(uintptr_t), sizeof(uint64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001286 PushStack(static_cast<uintptr_t>(bit_cast<uint64_t, double>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001287 } else {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001288 PushStack(static_cast<uintptr_t>(bit_cast<uint32_t, float>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001289 }
1290 fpr_index_ = 0;
1291 }
1292 }
1293 }
1294
Ian Rogers1428dce2014-10-21 15:02:15 -07001295 bool HaveDoubleFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001296 return fpr_index_ >= kRegistersNeededForDouble + (DoubleFprNeedsPadding() ? 1 : 0);
1297 }
1298
Ian Rogers1428dce2014-10-21 15:02:15 -07001299 bool DoubleFprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001300 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1301 kAlignDoubleOnStack && // and when it needs alignment
1302 (fpr_index_ & 1) == 1; // counter is odd, see constructor
1303 }
1304
Ian Rogers1428dce2014-10-21 15:02:15 -07001305 bool DoubleStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001306 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1307 kAlignDoubleOnStack && // and when it needs 8B alignment
1308 (stack_entries_ & 1) == 1; // counter is odd
1309 }
1310
1311 void AdvanceDouble(uint64_t val) {
1312 if (kNativeSoftFloatAbi) {
1313 AdvanceLong(val);
1314 } else {
1315 if (HaveDoubleFpr()) {
1316 if (DoubleFprNeedsPadding()) {
1317 PushFpr4(0);
1318 fpr_index_--;
1319 }
1320 PushFpr8(val);
1321 fpr_index_ -= kRegistersNeededForDouble;
1322 } else {
1323 if (DoubleStackNeedsPadding()) {
1324 PushStack(0);
1325 stack_entries_++;
1326 }
1327 if (kRegistersNeededForDouble == 1) {
1328 PushStack(static_cast<uintptr_t>(val));
1329 stack_entries_++;
1330 } else {
1331 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1332 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1333 stack_entries_ += 2;
1334 }
1335 fpr_index_ = 0;
1336 }
1337 }
1338 }
1339
Ian Rogers1428dce2014-10-21 15:02:15 -07001340 uint32_t GetStackEntries() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001341 return stack_entries_;
1342 }
1343
Ian Rogers1428dce2014-10-21 15:02:15 -07001344 uint32_t GetNumberOfUsedGprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001345 return kNumNativeGprArgs - gpr_index_;
1346 }
1347
Ian Rogers1428dce2014-10-21 15:02:15 -07001348 uint32_t GetNumberOfUsedFprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001349 return kNumNativeFprArgs - fpr_index_;
1350 }
1351
1352 private:
1353 void PushGpr(uintptr_t val) {
1354 delegate_->PushGpr(val);
1355 }
1356 void PushFpr4(float val) {
1357 delegate_->PushFpr4(val);
1358 }
1359 void PushFpr8(uint64_t val) {
1360 delegate_->PushFpr8(val);
1361 }
1362 void PushStack(uintptr_t val) {
1363 delegate_->PushStack(val);
1364 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001365 uintptr_t PushHandle(mirror::Object* ref) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1366 return delegate_->PushHandle(ref);
Andreas Gampec147b002014-03-06 18:11:06 -08001367 }
1368
1369 uint32_t gpr_index_; // Number of free GPRs
1370 uint32_t fpr_index_; // Number of free FPRs
1371 uint32_t stack_entries_; // Stack entries are in multiples of 32b, as floats are usually not
1372 // extended
Ian Rogers1428dce2014-10-21 15:02:15 -07001373 T* const delegate_; // What Push implementation gets called
Andreas Gampec147b002014-03-06 18:11:06 -08001374};
1375
Andreas Gampec200a4a2014-06-16 18:39:09 -07001376// Computes the sizes of register stacks and call stack area. Handling of references can be extended
1377// in subclasses.
1378//
1379// To handle native pointers, use "L" in the shorty for an object reference, which simulates
1380// them with handles.
1381class ComputeNativeCallFrameSize {
Andreas Gampec147b002014-03-06 18:11:06 -08001382 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001383 ComputeNativeCallFrameSize() : num_stack_entries_(0) {}
1384
1385 virtual ~ComputeNativeCallFrameSize() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001386
Ian Rogers1428dce2014-10-21 15:02:15 -07001387 uint32_t GetStackSize() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001388 return num_stack_entries_ * sizeof(uintptr_t);
1389 }
1390
Ian Rogers1428dce2014-10-21 15:02:15 -07001391 uint8_t* LayoutCallStack(uint8_t* sp8) const {
Andreas Gampec147b002014-03-06 18:11:06 -08001392 sp8 -= GetStackSize();
Andreas Gampe779f8c92014-06-09 18:29:38 -07001393 // Align by kStackAlignment.
1394 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
Andreas Gampec200a4a2014-06-16 18:39:09 -07001395 return sp8;
Andreas Gampec147b002014-03-06 18:11:06 -08001396 }
1397
Ian Rogers1428dce2014-10-21 15:02:15 -07001398 uint8_t* LayoutCallRegisterStacks(uint8_t* sp8, uintptr_t** start_gpr, uint32_t** start_fpr)
1399 const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001400 // Assumption is OK right now, as we have soft-float arm
1401 size_t fregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeFprArgs;
1402 sp8 -= fregs * sizeof(uintptr_t);
1403 *start_fpr = reinterpret_cast<uint32_t*>(sp8);
1404 size_t iregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeGprArgs;
1405 sp8 -= iregs * sizeof(uintptr_t);
1406 *start_gpr = reinterpret_cast<uintptr_t*>(sp8);
1407 return sp8;
1408 }
Andreas Gampec147b002014-03-06 18:11:06 -08001409
Andreas Gampec200a4a2014-06-16 18:39:09 -07001410 uint8_t* LayoutNativeCall(uint8_t* sp8, uintptr_t** start_stack, uintptr_t** start_gpr,
Ian Rogers1428dce2014-10-21 15:02:15 -07001411 uint32_t** start_fpr) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001412 // Native call stack.
1413 sp8 = LayoutCallStack(sp8);
1414 *start_stack = reinterpret_cast<uintptr_t*>(sp8);
Andreas Gampec147b002014-03-06 18:11:06 -08001415
Andreas Gampec200a4a2014-06-16 18:39:09 -07001416 // Put fprs and gprs below.
1417 sp8 = LayoutCallRegisterStacks(sp8, start_gpr, start_fpr);
Andreas Gampec147b002014-03-06 18:11:06 -08001418
Andreas Gampec200a4a2014-06-16 18:39:09 -07001419 // Return the new bottom.
1420 return sp8;
1421 }
1422
1423 virtual void WalkHeader(BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm)
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001424 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1425 UNUSED(sm);
1426 }
Andreas Gampec200a4a2014-06-16 18:39:09 -07001427
1428 void Walk(const char* shorty, uint32_t shorty_len) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1429 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize> sm(this);
1430
1431 WalkHeader(&sm);
Andreas Gampec147b002014-03-06 18:11:06 -08001432
1433 for (uint32_t i = 1; i < shorty_len; ++i) {
1434 Primitive::Type cur_type_ = Primitive::GetType(shorty[i]);
1435 switch (cur_type_) {
1436 case Primitive::kPrimNot:
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001437 // TODO: fix abuse of mirror types.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001438 sm.AdvanceHandleScope(
1439 reinterpret_cast<mirror::Object*>(0x12345678));
Andreas Gampec147b002014-03-06 18:11:06 -08001440 break;
1441
1442 case Primitive::kPrimBoolean:
1443 case Primitive::kPrimByte:
1444 case Primitive::kPrimChar:
1445 case Primitive::kPrimShort:
1446 case Primitive::kPrimInt:
1447 sm.AdvanceInt(0);
1448 break;
1449 case Primitive::kPrimFloat:
1450 sm.AdvanceFloat(0);
1451 break;
1452 case Primitive::kPrimDouble:
1453 sm.AdvanceDouble(0);
1454 break;
1455 case Primitive::kPrimLong:
1456 sm.AdvanceLong(0);
1457 break;
1458 default:
1459 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001460 UNREACHABLE();
Andreas Gampec147b002014-03-06 18:11:06 -08001461 }
1462 }
1463
Ian Rogers1428dce2014-10-21 15:02:15 -07001464 num_stack_entries_ = sm.GetStackEntries();
Andreas Gampec147b002014-03-06 18:11:06 -08001465 }
1466
1467 void PushGpr(uintptr_t /* val */) {
1468 // not optimizing registers, yet
1469 }
1470
1471 void PushFpr4(float /* val */) {
1472 // not optimizing registers, yet
1473 }
1474
1475 void PushFpr8(uint64_t /* val */) {
1476 // not optimizing registers, yet
1477 }
1478
1479 void PushStack(uintptr_t /* val */) {
1480 // counting is already done in the superclass
1481 }
1482
Andreas Gampec200a4a2014-06-16 18:39:09 -07001483 virtual uintptr_t PushHandle(mirror::Object* /* ptr */) {
Andreas Gampec147b002014-03-06 18:11:06 -08001484 return reinterpret_cast<uintptr_t>(nullptr);
1485 }
1486
Andreas Gampec200a4a2014-06-16 18:39:09 -07001487 protected:
Andreas Gampec147b002014-03-06 18:11:06 -08001488 uint32_t num_stack_entries_;
1489};
1490
Andreas Gampec200a4a2014-06-16 18:39:09 -07001491class ComputeGenericJniFrameSize FINAL : public ComputeNativeCallFrameSize {
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001492 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001493 ComputeGenericJniFrameSize() : num_handle_scope_references_(0) {}
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001494
Andreas Gampec200a4a2014-06-16 18:39:09 -07001495 // Lays out the callee-save frame. Assumes that the incorrect frame corresponding to RefsAndArgs
1496 // is at *m = sp. Will update to point to the bottom of the save frame.
1497 //
1498 // Note: assumes ComputeAll() has been run before.
Ian Rogers59c07062014-10-10 13:03:39 -07001499 void LayoutCalleeSaveFrame(Thread* self, StackReference<mirror::ArtMethod>** m, void* sp,
1500 HandleScope** handle_scope)
Andreas Gampec200a4a2014-06-16 18:39:09 -07001501 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1502 mirror::ArtMethod* method = (*m)->AsMirrorPtr();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001503
Andreas Gampec200a4a2014-06-16 18:39:09 -07001504 uint8_t* sp8 = reinterpret_cast<uint8_t*>(sp);
1505
1506 // First, fix up the layout of the callee-save frame.
1507 // We have to squeeze in the HandleScope, and relocate the method pointer.
1508
1509 // "Free" the slot for the method.
Ian Rogers13735952014-10-08 12:43:28 -07001510 sp8 += sizeof(void*); // In the callee-save frame we use a full pointer.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001511
1512 // Under the callee saves put handle scope and new method stack reference.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001513 size_t handle_scope_size = HandleScope::SizeOf(num_handle_scope_references_);
1514 size_t scope_and_method = handle_scope_size + sizeof(StackReference<mirror::ArtMethod>);
1515
1516 sp8 -= scope_and_method;
1517 // Align by kStackAlignment.
1518 sp8 = reinterpret_cast<uint8_t*>(RoundDown(
1519 reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
1520
1521 uint8_t* sp8_table = sp8 + sizeof(StackReference<mirror::ArtMethod>);
Ian Rogers59c07062014-10-10 13:03:39 -07001522 *handle_scope = HandleScope::Create(sp8_table, self->GetTopHandleScope(),
1523 num_handle_scope_references_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001524
1525 // Add a slot for the method pointer, and fill it. Fix the pointer-pointer given to us.
1526 uint8_t* method_pointer = sp8;
1527 StackReference<mirror::ArtMethod>* new_method_ref =
1528 reinterpret_cast<StackReference<mirror::ArtMethod>*>(method_pointer);
1529 new_method_ref->Assign(method);
1530 *m = new_method_ref;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001531 }
1532
Andreas Gampec200a4a2014-06-16 18:39:09 -07001533 // Adds space for the cookie. Note: may leave stack unaligned.
Ian Rogers1428dce2014-10-21 15:02:15 -07001534 void LayoutCookie(uint8_t** sp) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001535 // Reference cookie and padding
1536 *sp -= 8;
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001537 }
1538
Andreas Gampec200a4a2014-06-16 18:39:09 -07001539 // Re-layout the callee-save frame (insert a handle-scope). Then add space for the cookie.
1540 // Returns the new bottom. Note: this may be unaligned.
Ian Rogers59c07062014-10-10 13:03:39 -07001541 uint8_t* LayoutJNISaveFrame(Thread* self, StackReference<mirror::ArtMethod>** m, void* sp,
1542 HandleScope** handle_scope)
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001543 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001544 // First, fix up the layout of the callee-save frame.
1545 // We have to squeeze in the HandleScope, and relocate the method pointer.
Ian Rogers59c07062014-10-10 13:03:39 -07001546 LayoutCalleeSaveFrame(self, m, sp, handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001547
1548 // The bottom of the callee-save frame is now where the method is, *m.
1549 uint8_t* sp8 = reinterpret_cast<uint8_t*>(*m);
1550
1551 // Add space for cookie.
1552 LayoutCookie(&sp8);
1553
1554 return sp8;
1555 }
1556
1557 // WARNING: After this, *sp won't be pointing to the method anymore!
Ian Rogers59c07062014-10-10 13:03:39 -07001558 uint8_t* ComputeLayout(Thread* self, StackReference<mirror::ArtMethod>** m,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001559 const char* shorty, uint32_t shorty_len, HandleScope** handle_scope,
Andreas Gampec200a4a2014-06-16 18:39:09 -07001560 uintptr_t** start_stack, uintptr_t** start_gpr, uint32_t** start_fpr)
1561 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1562 Walk(shorty, shorty_len);
1563
1564 // JNI part.
Ian Rogers59c07062014-10-10 13:03:39 -07001565 uint8_t* sp8 = LayoutJNISaveFrame(self, m, reinterpret_cast<void*>(*m), handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001566
1567 sp8 = LayoutNativeCall(sp8, start_stack, start_gpr, start_fpr);
1568
1569 // Return the new bottom.
1570 return sp8;
1571 }
1572
1573 uintptr_t PushHandle(mirror::Object* /* ptr */) OVERRIDE;
1574
1575 // Add JNIEnv* and jobj/jclass before the shorty-derived elements.
1576 void WalkHeader(BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) OVERRIDE
1577 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
1578
1579 private:
1580 uint32_t num_handle_scope_references_;
1581};
1582
1583uintptr_t ComputeGenericJniFrameSize::PushHandle(mirror::Object* /* ptr */) {
1584 num_handle_scope_references_++;
1585 return reinterpret_cast<uintptr_t>(nullptr);
1586}
1587
1588void ComputeGenericJniFrameSize::WalkHeader(
1589 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) {
1590 // JNIEnv
1591 sm->AdvancePointer(nullptr);
1592
1593 // Class object or this as first argument
1594 sm->AdvanceHandleScope(reinterpret_cast<mirror::Object*>(0x12345678));
1595}
1596
1597// Class to push values to three separate regions. Used to fill the native call part. Adheres to
1598// the template requirements of BuildGenericJniFrameStateMachine.
1599class FillNativeCall {
1600 public:
1601 FillNativeCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) :
1602 cur_gpr_reg_(gpr_regs), cur_fpr_reg_(fpr_regs), cur_stack_arg_(stack_args) {}
1603
1604 virtual ~FillNativeCall() {}
1605
1606 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) {
1607 cur_gpr_reg_ = gpr_regs;
1608 cur_fpr_reg_ = fpr_regs;
1609 cur_stack_arg_ = stack_args;
Andreas Gampec147b002014-03-06 18:11:06 -08001610 }
1611
1612 void PushGpr(uintptr_t val) {
1613 *cur_gpr_reg_ = val;
1614 cur_gpr_reg_++;
1615 }
1616
1617 void PushFpr4(float val) {
1618 *cur_fpr_reg_ = val;
1619 cur_fpr_reg_++;
1620 }
1621
1622 void PushFpr8(uint64_t val) {
1623 uint64_t* tmp = reinterpret_cast<uint64_t*>(cur_fpr_reg_);
1624 *tmp = val;
1625 cur_fpr_reg_ += 2;
1626 }
1627
1628 void PushStack(uintptr_t val) {
1629 *cur_stack_arg_ = val;
1630 cur_stack_arg_++;
1631 }
1632
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001633 virtual uintptr_t PushHandle(mirror::Object*) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001634 LOG(FATAL) << "(Non-JNI) Native call does not use handles.";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001635 UNREACHABLE();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001636 }
1637
1638 private:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001639 uintptr_t* cur_gpr_reg_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001640 uint32_t* cur_fpr_reg_;
1641 uintptr_t* cur_stack_arg_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001642};
Andreas Gampec147b002014-03-06 18:11:06 -08001643
Andreas Gampec200a4a2014-06-16 18:39:09 -07001644// Visits arguments on the stack placing them into a region lower down the stack for the benefit
1645// of transitioning into native code.
1646class BuildGenericJniFrameVisitor FINAL : public QuickArgumentVisitor {
1647 public:
Ian Rogers59c07062014-10-10 13:03:39 -07001648 BuildGenericJniFrameVisitor(Thread* self, bool is_static, const char* shorty, uint32_t shorty_len,
1649 StackReference<mirror::ArtMethod>** sp)
Andreas Gampec200a4a2014-06-16 18:39:09 -07001650 : QuickArgumentVisitor(*sp, is_static, shorty, shorty_len),
1651 jni_call_(nullptr, nullptr, nullptr, nullptr), sm_(&jni_call_) {
1652 ComputeGenericJniFrameSize fsc;
1653 uintptr_t* start_gpr_reg;
1654 uint32_t* start_fpr_reg;
1655 uintptr_t* start_stack_arg;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001656 bottom_of_used_area_ = fsc.ComputeLayout(self, sp, shorty, shorty_len,
Ian Rogers59c07062014-10-10 13:03:39 -07001657 &handle_scope_,
1658 &start_stack_arg,
Andreas Gampec200a4a2014-06-16 18:39:09 -07001659 &start_gpr_reg, &start_fpr_reg);
1660
Andreas Gampec200a4a2014-06-16 18:39:09 -07001661 jni_call_.Reset(start_gpr_reg, start_fpr_reg, start_stack_arg, handle_scope_);
1662
1663 // jni environment is always first argument
1664 sm_.AdvancePointer(self->GetJniEnv());
1665
1666 if (is_static) {
1667 sm_.AdvanceHandleScope((*sp)->AsMirrorPtr()->GetDeclaringClass());
1668 }
1669 }
1670
1671 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
1672
1673 void FinalizeHandleScope(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
1674
1675 StackReference<mirror::Object>* GetFirstHandleScopeEntry()
1676 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1677 return handle_scope_->GetHandle(0).GetReference();
1678 }
1679
Ian Rogers1428dce2014-10-21 15:02:15 -07001680 jobject GetFirstHandleScopeJObject() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001681 return handle_scope_->GetHandle(0).ToJObject();
1682 }
1683
Ian Rogers1428dce2014-10-21 15:02:15 -07001684 void* GetBottomOfUsedArea() const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001685 return bottom_of_used_area_;
1686 }
1687
1688 private:
1689 // A class to fill a JNI call. Adds reference/handle-scope management to FillNativeCall.
1690 class FillJniCall FINAL : public FillNativeCall {
1691 public:
1692 FillJniCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args,
1693 HandleScope* handle_scope) : FillNativeCall(gpr_regs, fpr_regs, stack_args),
1694 handle_scope_(handle_scope), cur_entry_(0) {}
1695
1696 uintptr_t PushHandle(mirror::Object* ref) OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
1697
1698 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args, HandleScope* scope) {
1699 FillNativeCall::Reset(gpr_regs, fpr_regs, stack_args);
1700 handle_scope_ = scope;
1701 cur_entry_ = 0U;
1702 }
1703
1704 void ResetRemainingScopeSlots() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1705 // Initialize padding entries.
1706 size_t expected_slots = handle_scope_->NumberOfReferences();
1707 while (cur_entry_ < expected_slots) {
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07001708 handle_scope_->GetMutableHandle(cur_entry_++).Assign(nullptr);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001709 }
1710 DCHECK_NE(cur_entry_, 0U);
1711 }
1712
1713 private:
1714 HandleScope* handle_scope_;
1715 size_t cur_entry_;
1716 };
1717
1718 HandleScope* handle_scope_;
1719 FillJniCall jni_call_;
1720 void* bottom_of_used_area_;
1721
1722 BuildNativeCallFrameStateMachine<FillJniCall> sm_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001723
1724 DISALLOW_COPY_AND_ASSIGN(BuildGenericJniFrameVisitor);
1725};
1726
Andreas Gampec200a4a2014-06-16 18:39:09 -07001727uintptr_t BuildGenericJniFrameVisitor::FillJniCall::PushHandle(mirror::Object* ref) {
1728 uintptr_t tmp;
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07001729 MutableHandle<mirror::Object> h = handle_scope_->GetMutableHandle(cur_entry_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001730 h.Assign(ref);
1731 tmp = reinterpret_cast<uintptr_t>(h.ToJObject());
1732 cur_entry_++;
1733 return tmp;
1734}
1735
Ian Rogers9758f792014-03-13 09:02:55 -07001736void BuildGenericJniFrameVisitor::Visit() {
1737 Primitive::Type type = GetParamPrimitiveType();
1738 switch (type) {
1739 case Primitive::kPrimLong: {
1740 jlong long_arg;
1741 if (IsSplitLongOrDouble()) {
1742 long_arg = ReadSplitLongParam();
1743 } else {
1744 long_arg = *reinterpret_cast<jlong*>(GetParamAddress());
1745 }
1746 sm_.AdvanceLong(long_arg);
1747 break;
1748 }
1749 case Primitive::kPrimDouble: {
1750 uint64_t double_arg;
1751 if (IsSplitLongOrDouble()) {
1752 // Read into union so that we don't case to a double.
1753 double_arg = ReadSplitLongParam();
1754 } else {
1755 double_arg = *reinterpret_cast<uint64_t*>(GetParamAddress());
1756 }
1757 sm_.AdvanceDouble(double_arg);
1758 break;
1759 }
1760 case Primitive::kPrimNot: {
1761 StackReference<mirror::Object>* stack_ref =
1762 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001763 sm_.AdvanceHandleScope(stack_ref->AsMirrorPtr());
Ian Rogers9758f792014-03-13 09:02:55 -07001764 break;
1765 }
1766 case Primitive::kPrimFloat:
1767 sm_.AdvanceFloat(*reinterpret_cast<float*>(GetParamAddress()));
1768 break;
1769 case Primitive::kPrimBoolean: // Fall-through.
1770 case Primitive::kPrimByte: // Fall-through.
1771 case Primitive::kPrimChar: // Fall-through.
1772 case Primitive::kPrimShort: // Fall-through.
1773 case Primitive::kPrimInt: // Fall-through.
1774 sm_.AdvanceInt(*reinterpret_cast<jint*>(GetParamAddress()));
1775 break;
1776 case Primitive::kPrimVoid:
1777 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -07001778 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -07001779 }
1780}
1781
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001782void BuildGenericJniFrameVisitor::FinalizeHandleScope(Thread* self) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001783 // Clear out rest of the scope.
1784 jni_call_.ResetRemainingScopeSlots();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001785 // Install HandleScope.
1786 self->PushHandleScope(handle_scope_);
Ian Rogers9758f792014-03-13 09:02:55 -07001787}
1788
Ian Rogers04c31d22014-07-07 21:44:06 -07001789#if defined(__arm__) || defined(__aarch64__)
Andreas Gampe90546832014-03-12 18:07:19 -07001790extern "C" void* artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07001791#else
1792extern "C" void* artFindNativeMethod(Thread* self);
1793#endif
Andreas Gampe90546832014-03-12 18:07:19 -07001794
Andreas Gampead615172014-04-04 16:20:13 -07001795uint64_t artQuickGenericJniEndJNIRef(Thread* self, uint32_t cookie, jobject l, jobject lock) {
1796 if (lock != nullptr) {
1797 return reinterpret_cast<uint64_t>(JniMethodEndWithReferenceSynchronized(l, cookie, lock, self));
1798 } else {
1799 return reinterpret_cast<uint64_t>(JniMethodEndWithReference(l, cookie, self));
1800 }
1801}
1802
1803void artQuickGenericJniEndJNINonRef(Thread* self, uint32_t cookie, jobject lock) {
1804 if (lock != nullptr) {
1805 JniMethodEndSynchronized(cookie, lock, self);
1806 } else {
1807 JniMethodEnd(cookie, self);
1808 }
1809}
1810
Andreas Gampec147b002014-03-06 18:11:06 -08001811/*
1812 * Initializes an alloca region assumed to be directly below sp for a native call:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001813 * 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 -08001814 * The final element on the stack is a pointer to the native code.
1815 *
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001816 * On entry, the stack has a standard callee-save frame above sp, and an alloca below it.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001817 * We need to fix this, as the handle scope needs to go into the callee-save frame.
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001818 *
Andreas Gampec147b002014-03-06 18:11:06 -08001819 * The return of this function denotes:
1820 * 1) How many bytes of the alloca can be released, if the value is non-negative.
1821 * 2) An error, if the value is negative.
1822 */
Andreas Gampec200a4a2014-06-16 18:39:09 -07001823extern "C" TwoWordReturn artQuickGenericJniTrampoline(Thread* self,
1824 StackReference<mirror::ArtMethod>* sp)
Andreas Gampe2da88232014-02-27 12:26:20 -08001825 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampecf4035a2014-05-28 22:43:01 -07001826 mirror::ArtMethod* called = sp->AsMirrorPtr();
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001827 DCHECK(called->IsNative()) << PrettyMethod(called, true);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001828 uint32_t shorty_len = 0;
1829 const char* shorty = called->GetShorty(&shorty_len);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001830
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001831 // Run the visitor and update sp.
Ian Rogers59c07062014-10-10 13:03:39 -07001832 BuildGenericJniFrameVisitor visitor(self, called->IsStatic(), shorty, shorty_len, &sp);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001833 visitor.VisitArguments();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001834 visitor.FinalizeHandleScope(self);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001835
Andreas Gampec200a4a2014-06-16 18:39:09 -07001836 // Fix up managed-stack things in Thread.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001837 self->SetTopOfStack(sp);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001838
Ian Rogerse0dcd462014-03-08 15:21:04 -08001839 self->VerifyStack();
1840
Andreas Gampe90546832014-03-12 18:07:19 -07001841 // Start JNI, save the cookie.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001842 uint32_t cookie;
1843 if (called->IsSynchronized()) {
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001844 cookie = JniMethodStartSynchronized(visitor.GetFirstHandleScopeJObject(), self);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001845 if (self->IsExceptionPending()) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001846 self->PopHandleScope();
Andreas Gampec147b002014-03-06 18:11:06 -08001847 // A negative value denotes an error.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001848 return GetTwoWordFailureValue();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001849 }
1850 } else {
1851 cookie = JniMethodStart(self);
1852 }
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001853 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
Ian Rogerse0dcd462014-03-08 15:21:04 -08001854 *(sp32 - 1) = cookie;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001855
Andreas Gampe90546832014-03-12 18:07:19 -07001856 // Retrieve the stored native code.
Mathieu Chartier2d721012014-11-10 11:08:06 -08001857 void* nativeCode = called->GetEntryPointFromJni();
Andreas Gampe90546832014-03-12 18:07:19 -07001858
Andreas Gampe9a6a99a2014-03-14 07:52:20 -07001859 // There are two cases for the content of nativeCode:
1860 // 1) Pointer to the native function.
1861 // 2) Pointer to the trampoline for native code binding.
1862 // In the second case, we need to execute the binding and continue with the actual native function
1863 // pointer.
Andreas Gampe90546832014-03-12 18:07:19 -07001864 DCHECK(nativeCode != nullptr);
1865 if (nativeCode == GetJniDlsymLookupStub()) {
Ian Rogers04c31d22014-07-07 21:44:06 -07001866#if defined(__arm__) || defined(__aarch64__)
Andreas Gampe90546832014-03-12 18:07:19 -07001867 nativeCode = artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07001868#else
1869 nativeCode = artFindNativeMethod(self);
1870#endif
Andreas Gampe90546832014-03-12 18:07:19 -07001871
1872 if (nativeCode == nullptr) {
1873 DCHECK(self->IsExceptionPending()); // There should be an exception pending now.
Andreas Gampead615172014-04-04 16:20:13 -07001874
1875 // End JNI, as the assembly will move to deliver the exception.
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001876 jobject lock = called->IsSynchronized() ? visitor.GetFirstHandleScopeJObject() : nullptr;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001877 if (shorty[0] == 'L') {
Andreas Gampead615172014-04-04 16:20:13 -07001878 artQuickGenericJniEndJNIRef(self, cookie, nullptr, lock);
1879 } else {
1880 artQuickGenericJniEndJNINonRef(self, cookie, lock);
1881 }
1882
Andreas Gampec200a4a2014-06-16 18:39:09 -07001883 return GetTwoWordFailureValue();
Andreas Gampe90546832014-03-12 18:07:19 -07001884 }
1885 // Note that the native code pointer will be automatically set by artFindNativeMethod().
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001886 }
1887
Andreas Gampec200a4a2014-06-16 18:39:09 -07001888 // Return native code addr(lo) and bottom of alloca address(hi).
1889 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(visitor.GetBottomOfUsedArea()),
1890 reinterpret_cast<uintptr_t>(nativeCode));
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001891}
1892
1893/*
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001894 * Is called after the native JNI code. Responsible for cleanup (handle scope, saved state) and
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001895 * unlocking.
1896 */
Andreas Gampec200a4a2014-06-16 18:39:09 -07001897extern "C" uint64_t artQuickGenericJniEndTrampoline(Thread* self, jvalue result, uint64_t result_f)
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001898 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001899 StackReference<mirror::ArtMethod>* sp = self->GetManagedStack()->GetTopQuickFrame();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001900 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
Andreas Gampecf4035a2014-05-28 22:43:01 -07001901 mirror::ArtMethod* called = sp->AsMirrorPtr();
Ian Rogerse0dcd462014-03-08 15:21:04 -08001902 uint32_t cookie = *(sp32 - 1);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001903
Andreas Gampead615172014-04-04 16:20:13 -07001904 jobject lock = nullptr;
1905 if (called->IsSynchronized()) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001906 HandleScope* table = reinterpret_cast<HandleScope*>(reinterpret_cast<uint8_t*>(sp)
1907 + sizeof(StackReference<mirror::ArtMethod>));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001908 lock = table->GetHandle(0).ToJObject();
Andreas Gampead615172014-04-04 16:20:13 -07001909 }
1910
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001911 char return_shorty_char = called->GetShorty()[0];
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001912
1913 if (return_shorty_char == 'L') {
Andreas Gampead615172014-04-04 16:20:13 -07001914 return artQuickGenericJniEndJNIRef(self, cookie, result.l, lock);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001915 } else {
Andreas Gampead615172014-04-04 16:20:13 -07001916 artQuickGenericJniEndJNINonRef(self, cookie, lock);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001917
1918 switch (return_shorty_char) {
Nicolas Geoffray54accbc2014-08-13 03:40:45 +01001919 case 'F': {
1920 if (kRuntimeISA == kX86) {
1921 // Convert back the result to float.
Roland Levillainda4d79b2015-03-24 14:36:11 +00001922 double d = bit_cast<double, uint64_t>(result_f);
1923 return bit_cast<uint32_t, float>(static_cast<float>(d));
Nicolas Geoffray54accbc2014-08-13 03:40:45 +01001924 } else {
1925 return result_f;
1926 }
1927 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001928 case 'D':
1929 return result_f;
1930 case 'Z':
1931 return result.z;
1932 case 'B':
1933 return result.b;
1934 case 'C':
1935 return result.c;
1936 case 'S':
1937 return result.s;
1938 case 'I':
1939 return result.i;
1940 case 'J':
1941 return result.j;
1942 case 'V':
1943 return 0;
1944 default:
1945 LOG(FATAL) << "Unexpected return shorty character " << return_shorty_char;
1946 return 0;
1947 }
1948 }
Andreas Gampe2da88232014-02-27 12:26:20 -08001949}
1950
Andreas Gamped58342c2014-06-05 14:18:08 -07001951// We use TwoWordReturn to optimize scalar returns. We use the hi value for code, and the lo value
1952// for the method pointer.
Andreas Gampe51f76352014-05-21 08:28:48 -07001953//
Andreas Gamped58342c2014-06-05 14:18:08 -07001954// It is valid to use this, as at the usage points here (returns from C functions) we are assuming
1955// to hold the mutator lock (see SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) annotations).
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001956
1957template<InvokeType type, bool access_check>
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01001958static TwoWordReturn artInvokeCommon(uint32_t method_idx,
1959 mirror::Object* this_object,
1960 Thread* self,
1961 StackReference<mirror::ArtMethod>* sp) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001962 ScopedQuickEntrypointChecks sqec(self);
1963 DCHECK_EQ(sp->AsMirrorPtr(), Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs));
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01001964 mirror::ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001965 mirror::ArtMethod* method = FindMethodFast(method_idx, this_object, caller_method, access_check,
1966 type);
1967 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001968 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()->GetDexFile();
1969 uint32_t shorty_len;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001970 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(method_idx), &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001971 {
1972 // Remember the args in case a GC happens in FindMethodFromCode.
1973 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
1974 RememberForGcArgumentVisitor visitor(sp, type == kStatic, shorty, shorty_len, &soa);
1975 visitor.VisitArguments();
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001976 method = FindMethodFromCode<type, access_check>(method_idx, &this_object, &caller_method,
1977 self);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001978 visitor.FixupReferences();
1979 }
1980
Ian Rogerse0a02da2014-12-02 14:10:53 -08001981 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001982 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07001983 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001984 }
1985 }
1986 DCHECK(!self->IsExceptionPending());
1987 const void* code = method->GetEntryPointFromQuickCompiledCode();
1988
1989 // When we return, the caller will branch to this address, so it had better not be 0!
Ian Rogerse0a02da2014-12-02 14:10:53 -08001990 DCHECK(code != nullptr) << "Code was null in method: " << PrettyMethod(method)
Andreas Gampec200a4a2014-06-16 18:39:09 -07001991 << " location: "
1992 << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07001993
Andreas Gamped58342c2014-06-05 14:18:08 -07001994 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
1995 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001996}
1997
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01001998// Explicit artInvokeCommon template function declarations to please analysis tool.
1999#define EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(type, access_check) \
2000 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) \
Andreas Gamped58342c2014-06-05 14:18:08 -07002001 TwoWordReturn artInvokeCommon<type, access_check>(uint32_t method_idx, \
Andreas Gampe51f76352014-05-21 08:28:48 -07002002 mirror::Object* this_object, \
Andreas Gampecf4035a2014-05-28 22:43:01 -07002003 Thread* self, \
2004 StackReference<mirror::ArtMethod>* sp) \
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01002005
2006EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, false);
2007EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, true);
2008EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, false);
2009EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, true);
2010EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, false);
2011EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, true);
2012EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, false);
2013EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, true);
2014EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, false);
2015EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, true);
2016#undef EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL
2017
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002018// See comments in runtime_support_asm.S
Andreas Gampec200a4a2014-06-16 18:39:09 -07002019extern "C" TwoWordReturn artInvokeInterfaceTrampolineWithAccessCheck(
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002020 uint32_t method_idx,
2021 mirror::Object* this_object,
2022 Thread* self,
Andreas Gampec200a4a2014-06-16 18:39:09 -07002023 StackReference<mirror::ArtMethod>* sp)
2024 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002025 return artInvokeCommon<kInterface, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002026}
2027
Andreas Gampec200a4a2014-06-16 18:39:09 -07002028extern "C" TwoWordReturn artInvokeDirectTrampolineWithAccessCheck(
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002029 uint32_t method_idx,
2030 mirror::Object* this_object,
2031 Thread* self,
Andreas Gampec200a4a2014-06-16 18:39:09 -07002032 StackReference<mirror::ArtMethod>* sp)
2033 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002034 return artInvokeCommon<kDirect, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002035}
2036
Andreas Gampec200a4a2014-06-16 18:39:09 -07002037extern "C" TwoWordReturn artInvokeStaticTrampolineWithAccessCheck(
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002038 uint32_t method_idx,
2039 mirror::Object* this_object,
2040 Thread* self,
Andreas Gampec200a4a2014-06-16 18:39:09 -07002041 StackReference<mirror::ArtMethod>* sp)
2042 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002043 return artInvokeCommon<kStatic, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002044}
2045
Andreas Gampec200a4a2014-06-16 18:39:09 -07002046extern "C" TwoWordReturn artInvokeSuperTrampolineWithAccessCheck(
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002047 uint32_t method_idx,
2048 mirror::Object* this_object,
2049 Thread* self,
Andreas Gampec200a4a2014-06-16 18:39:09 -07002050 StackReference<mirror::ArtMethod>* sp)
2051 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002052 return artInvokeCommon<kSuper, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002053}
2054
Andreas Gampec200a4a2014-06-16 18:39:09 -07002055extern "C" TwoWordReturn artInvokeVirtualTrampolineWithAccessCheck(
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002056 uint32_t method_idx,
2057 mirror::Object* this_object,
2058 Thread* self,
Andreas Gampec200a4a2014-06-16 18:39:09 -07002059 StackReference<mirror::ArtMethod>* sp)
2060 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002061 return artInvokeCommon<kVirtual, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002062}
2063
2064// Determine target of interface dispatch. This object is known non-null.
Andreas Gamped58342c2014-06-05 14:18:08 -07002065extern "C" TwoWordReturn artInvokeInterfaceTrampoline(mirror::ArtMethod* interface_method,
Andreas Gampe51f76352014-05-21 08:28:48 -07002066 mirror::Object* this_object,
Andreas Gampecf4035a2014-05-28 22:43:01 -07002067 Thread* self,
2068 StackReference<mirror::ArtMethod>* sp)
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002069 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002070 ScopedQuickEntrypointChecks sqec(self);
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002071 mirror::ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002072 mirror::ArtMethod* method;
2073 if (LIKELY(interface_method->GetDexMethodIndex() != DexFile::kDexNoIndex)) {
2074 method = this_object->GetClass()->FindVirtualMethodForInterface(interface_method);
Ian Rogerse0a02da2014-12-02 14:10:53 -08002075 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002076 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(interface_method, this_object,
2077 caller_method);
Andreas Gamped58342c2014-06-05 14:18:08 -07002078 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002079 }
2080 } else {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002081 DCHECK(interface_method == Runtime::Current()->GetResolutionMethod());
Alexei Zavjalov41c507a2014-05-15 16:02:46 +07002082
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002083 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002084 const DexFile::CodeItem* code = caller_method->GetCodeItem();
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002085 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
2086 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
2087 Instruction::Code instr_code = instr->Opcode();
2088 CHECK(instr_code == Instruction::INVOKE_INTERFACE ||
2089 instr_code == Instruction::INVOKE_INTERFACE_RANGE)
Ian Rogerse0a02da2014-12-02 14:10:53 -08002090 << "Unexpected call into interface trampoline: " << instr->DumpString(nullptr);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002091 uint32_t dex_method_idx;
2092 if (instr_code == Instruction::INVOKE_INTERFACE) {
2093 dex_method_idx = instr->VRegB_35c();
2094 } else {
2095 DCHECK_EQ(instr_code, Instruction::INVOKE_INTERFACE_RANGE);
2096 dex_method_idx = instr->VRegB_3rc();
2097 }
2098
Andreas Gampec200a4a2014-06-16 18:39:09 -07002099 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()
2100 ->GetDexFile();
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002101 uint32_t shorty_len;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002102 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(dex_method_idx),
2103 &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002104 {
2105 // Remember the args in case a GC happens in FindMethodFromCode.
2106 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
2107 RememberForGcArgumentVisitor visitor(sp, false, shorty, shorty_len, &soa);
2108 visitor.VisitArguments();
Mathieu Chartier0cd81352014-05-22 16:48:55 -07002109 method = FindMethodFromCode<kInterface, false>(dex_method_idx, &this_object, &caller_method,
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002110 self);
2111 visitor.FixupReferences();
2112 }
2113
2114 if (UNLIKELY(method == nullptr)) {
2115 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07002116 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002117 }
2118 }
2119 const void* code = method->GetEntryPointFromQuickCompiledCode();
2120
2121 // When we return, the caller will branch to this address, so it had better not be 0!
Ian Rogerse0a02da2014-12-02 14:10:53 -08002122 DCHECK(code != nullptr) << "Code was null in method: " << PrettyMethod(method)
Andreas Gampec200a4a2014-06-16 18:39:09 -07002123 << " location: " << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07002124
Andreas Gamped58342c2014-06-05 14:18:08 -07002125 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
2126 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002127}
2128
Ian Rogers848871b2013-08-05 10:56:33 -07002129} // namespace art