blob: 33d70657d6a5ba147b66615c370f585a34a511f8 [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
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100300 static mirror::ArtMethod* GetOuterMethod(StackReference<mirror::ArtMethod>* sp)
301 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
302 DCHECK(sp->AsMirrorPtr()->IsCalleeSaveMethod());
303 uint8_t* previous_sp =
304 reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize;
305 return reinterpret_cast<StackReference<mirror::ArtMethod>*>(previous_sp)->AsMirrorPtr();
306 }
307
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +0100308 static uint32_t GetCallingDexPc(StackReference<mirror::ArtMethod>* sp)
309 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
310 DCHECK(sp->AsMirrorPtr()->IsCalleeSaveMethod());
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100311 const size_t callee_frame_size = GetCalleeSaveFrameSize(kRuntimeISA, Runtime::kRefsAndArgs);
312 auto* caller_sp = reinterpret_cast<StackReference<mirror::ArtMethod>*>(
313 reinterpret_cast<uintptr_t>(sp) + callee_frame_size);
314 mirror::ArtMethod* outer_method = caller_sp->AsMirrorPtr();
315 uintptr_t outer_pc = QuickArgumentVisitor::GetCallingPc(sp);
316 uintptr_t outer_pc_offset = outer_method->NativeQuickPcOffset(outer_pc);
317
318 if (outer_method->IsOptimized(sizeof(void*))) {
319 CodeInfo code_info = outer_method->GetOptimizedCodeInfo();
320 StackMap stack_map = code_info.GetStackMapForNativePcOffset(outer_pc_offset);
321 DCHECK(stack_map.IsValid());
322 if (stack_map.HasInlineInfo(code_info)) {
323 InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map);
324 return inline_info.GetDexPcAtDepth(inline_info.GetDepth() - 1);
325 } else {
326 return stack_map.GetDexPc(code_info);
327 }
328 } else {
329 return outer_method->ToDexPc(outer_pc);
330 }
Ian Rogers848871b2013-08-05 10:56:33 -0700331 }
332
Ian Rogers936b37f2014-02-14 00:52:24 -0800333 // For the given quick ref and args quick frame, return the caller's PC.
Andreas Gampecf4035a2014-05-28 22:43:01 -0700334 static uintptr_t GetCallingPc(StackReference<mirror::ArtMethod>* sp)
Ian Rogers936b37f2014-02-14 00:52:24 -0800335 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampecf4035a2014-05-28 22:43:01 -0700336 DCHECK(sp->AsMirrorPtr()->IsCalleeSaveMethod());
Ian Rogers13735952014-10-08 12:43:28 -0700337 uint8_t* lr = reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_LrOffset;
Ian Rogers848871b2013-08-05 10:56:33 -0700338 return *reinterpret_cast<uintptr_t*>(lr);
339 }
340
Andreas Gampec200a4a2014-06-16 18:39:09 -0700341 QuickArgumentVisitor(StackReference<mirror::ArtMethod>* sp, bool is_static, const char* shorty,
342 uint32_t shorty_len) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) :
343 is_static_(is_static), shorty_(shorty), shorty_len_(shorty_len),
Ian Rogers13735952014-10-08 12:43:28 -0700344 gpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset),
345 fpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset),
346 stack_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700347 + sizeof(StackReference<mirror::ArtMethod>)), // Skip StackReference<ArtMethod>.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800348 gpr_index_(0), fpr_index_(0), fpr_double_index_(0), stack_index_(0),
349 cur_type_(Primitive::kPrimVoid), is_split_long_or_double_(false) {
Andreas Gampe575e78c2014-11-03 23:41:03 -0800350 static_assert(kQuickSoftFloatAbi == (kNumQuickFprArgs == 0),
351 "Number of Quick FPR arguments unexpected");
352 static_assert(!(kQuickSoftFloatAbi && kQuickDoubleRegAlignedFloatBackFilled),
353 "Double alignment unexpected");
Zheng Xu5667fdb2014-10-23 18:29:55 +0800354 // For register alignment, we want to assume that counters(fpr_double_index_) are even if the
355 // next register is even.
Andreas Gampe575e78c2014-11-03 23:41:03 -0800356 static_assert(!kQuickDoubleRegAlignedFloatBackFilled || kNumQuickFprArgs % 2 == 0,
357 "Number of Quick FPR arguments not even");
Zheng Xu5667fdb2014-10-23 18:29:55 +0800358 }
Ian Rogers848871b2013-08-05 10:56:33 -0700359
360 virtual ~QuickArgumentVisitor() {}
361
362 virtual void Visit() = 0;
363
Ian Rogers936b37f2014-02-14 00:52:24 -0800364 Primitive::Type GetParamPrimitiveType() const {
365 return cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700366 }
367
Ian Rogers13735952014-10-08 12:43:28 -0700368 uint8_t* GetParamAddress() const {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800369 if (!kQuickSoftFloatAbi) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800370 Primitive::Type type = GetParamPrimitiveType();
371 if (UNLIKELY((type == Primitive::kPrimDouble) || (type == Primitive::kPrimFloat))) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800372 if (type == Primitive::kPrimDouble && kQuickDoubleRegAlignedFloatBackFilled) {
373 if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) {
374 return fpr_args_ + (fpr_double_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
375 }
376 } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000377 return fpr_args_ + (fpr_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800378 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700379 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers936b37f2014-02-14 00:52:24 -0800380 }
381 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800382 if (gpr_index_ < kNumQuickGprArgs) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800383 return gpr_args_ + GprIndexToGprOffset(gpr_index_);
384 }
385 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700386 }
387
388 bool IsSplitLongOrDouble() const {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000389 if ((GetBytesPerGprSpillLocation(kRuntimeISA) == 4) || (GetBytesPerFprSpillLocation(kRuntimeISA) == 4)) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800390 return is_split_long_or_double_;
391 } else {
392 return false; // An optimization for when GPR and FPRs are 64bit.
393 }
Ian Rogers848871b2013-08-05 10:56:33 -0700394 }
395
Ian Rogers936b37f2014-02-14 00:52:24 -0800396 bool IsParamAReference() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700397 return GetParamPrimitiveType() == Primitive::kPrimNot;
398 }
399
Ian Rogers936b37f2014-02-14 00:52:24 -0800400 bool IsParamALongOrDouble() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700401 Primitive::Type type = GetParamPrimitiveType();
402 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
403 }
404
405 uint64_t ReadSplitLongParam() const {
Nicolas Geoffray425f2392015-01-08 14:52:29 +0000406 // The splitted long is always available through the stack.
407 return *reinterpret_cast<uint64_t*>(stack_args_
408 + stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700409 }
410
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800411 void IncGprIndex() {
412 gpr_index_++;
413 if (kGprFprLockstep) {
414 fpr_index_++;
415 }
416 }
417
418 void IncFprIndex() {
419 fpr_index_++;
420 if (kGprFprLockstep) {
421 gpr_index_++;
422 }
423 }
424
Ian Rogers848871b2013-08-05 10:56:33 -0700425 void VisitArguments() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800426 // (a) 'stack_args_' should point to the first method's argument
427 // (b) whatever the argument type it is, the 'stack_index_' should
428 // be moved forward along with every visiting.
Ian Rogers936b37f2014-02-14 00:52:24 -0800429 gpr_index_ = 0;
430 fpr_index_ = 0;
Zheng Xu5667fdb2014-10-23 18:29:55 +0800431 if (kQuickDoubleRegAlignedFloatBackFilled) {
432 fpr_double_index_ = 0;
433 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800434 stack_index_ = 0;
435 if (!is_static_) { // Handle this.
436 cur_type_ = Primitive::kPrimNot;
437 is_split_long_or_double_ = false;
Ian Rogers848871b2013-08-05 10:56:33 -0700438 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800439 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800440 if (kNumQuickGprArgs > 0) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800441 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800442 }
Ian Rogers848871b2013-08-05 10:56:33 -0700443 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800444 for (uint32_t shorty_index = 1; shorty_index < shorty_len_; ++shorty_index) {
445 cur_type_ = Primitive::GetType(shorty_[shorty_index]);
446 switch (cur_type_) {
447 case Primitive::kPrimNot:
448 case Primitive::kPrimBoolean:
449 case Primitive::kPrimByte:
450 case Primitive::kPrimChar:
451 case Primitive::kPrimShort:
452 case Primitive::kPrimInt:
453 is_split_long_or_double_ = false;
454 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800455 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800456 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800457 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800458 }
459 break;
460 case Primitive::kPrimFloat:
461 is_split_long_or_double_ = false;
462 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800463 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800464 if (kQuickSoftFloatAbi) {
465 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800466 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800467 }
468 } else {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800469 if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800470 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800471 if (kQuickDoubleRegAlignedFloatBackFilled) {
472 // Double should not overlap with float.
473 // For example, if fpr_index_ = 3, fpr_double_index_ should be at least 4.
474 fpr_double_index_ = std::max(fpr_double_index_, RoundUp(fpr_index_, 2));
475 // Float should not overlap with double.
476 if (fpr_index_ % 2 == 0) {
477 fpr_index_ = std::max(fpr_double_index_, fpr_index_);
478 }
479 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800480 }
481 }
482 break;
483 case Primitive::kPrimDouble:
484 case Primitive::kPrimLong:
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800485 if (kQuickSoftFloatAbi || (cur_type_ == Primitive::kPrimLong)) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000486 if (cur_type_ == Primitive::kPrimLong && kAlignPairRegister && gpr_index_ == 0) {
487 // Currently, this is only for ARM, where the first available parameter register
488 // is R1. So we skip it, and use R2 instead.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800489 IncGprIndex();
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000490 }
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000491 is_split_long_or_double_ = (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) &&
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800492 ((gpr_index_ + 1) == kNumQuickGprArgs);
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500493 if (!kSplitPairAcrossRegisterAndStack && is_split_long_or_double_) {
494 // We don't want to split this. Pass over this register.
495 gpr_index_++;
496 is_split_long_or_double_ = false;
497 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800498 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800499 if (kBytesStackArgLocation == 4) {
500 stack_index_+= 2;
501 } else {
502 CHECK_EQ(kBytesStackArgLocation, 8U);
503 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800504 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700505 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800506 IncGprIndex();
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000507 if (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700508 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800509 IncGprIndex();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700510 }
511 }
512 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800513 } else {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000514 is_split_long_or_double_ = (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) &&
Zheng Xu5667fdb2014-10-23 18:29:55 +0800515 ((fpr_index_ + 1) == kNumQuickFprArgs) && !kQuickDoubleRegAlignedFloatBackFilled;
Ian Rogers936b37f2014-02-14 00:52:24 -0800516 Visit();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700517 if (kBytesStackArgLocation == 4) {
518 stack_index_+= 2;
Ian Rogers936b37f2014-02-14 00:52:24 -0800519 } else {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700520 CHECK_EQ(kBytesStackArgLocation, 8U);
521 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800522 }
Zheng Xu5667fdb2014-10-23 18:29:55 +0800523 if (kQuickDoubleRegAlignedFloatBackFilled) {
524 if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) {
525 fpr_double_index_ += 2;
526 // Float should not overlap with double.
527 if (fpr_index_ % 2 == 0) {
528 fpr_index_ = std::max(fpr_double_index_, fpr_index_);
529 }
530 }
531 } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800532 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800533 if (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) {
534 if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800535 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800536 }
537 }
538 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800539 }
540 break;
541 default:
542 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty_;
543 }
Ian Rogers848871b2013-08-05 10:56:33 -0700544 }
545 }
546
Andreas Gampec200a4a2014-06-16 18:39:09 -0700547 protected:
Ian Rogers848871b2013-08-05 10:56:33 -0700548 const bool is_static_;
549 const char* const shorty_;
550 const uint32_t shorty_len_;
Andreas Gampec200a4a2014-06-16 18:39:09 -0700551
552 private:
Ian Rogers13735952014-10-08 12:43:28 -0700553 uint8_t* const gpr_args_; // Address of GPR arguments in callee save frame.
554 uint8_t* const fpr_args_; // Address of FPR arguments in callee save frame.
555 uint8_t* const stack_args_; // Address of stack arguments in caller's frame.
Ian Rogers936b37f2014-02-14 00:52:24 -0800556 uint32_t gpr_index_; // Index into spilled GPRs.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800557 // Index into spilled FPRs.
558 // In case kQuickDoubleRegAlignedFloatBackFilled, it may index a hole while fpr_double_index_
559 // holds a higher register number.
560 uint32_t fpr_index_;
561 // Index into spilled FPRs for aligned double.
562 // Only used when kQuickDoubleRegAlignedFloatBackFilled. Next available double register indexed in
563 // terms of singles, may be behind fpr_index.
564 uint32_t fpr_double_index_;
Ian Rogers936b37f2014-02-14 00:52:24 -0800565 uint32_t stack_index_; // Index into arguments on the stack.
566 // The current type of argument during VisitArguments.
567 Primitive::Type cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700568 // Does a 64bit parameter straddle the register and stack arguments?
569 bool is_split_long_or_double_;
570};
571
Sebastien Hertza836bc92014-11-25 16:30:53 +0100572// Returns the 'this' object of a proxy method. This function is only used by StackVisitor. It
573// allows to use the QuickArgumentVisitor constants without moving all the code in its own module.
574extern "C" mirror::Object* artQuickGetProxyThisObject(StackReference<mirror::ArtMethod>* sp)
575 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
576 return QuickArgumentVisitor::GetProxyThisObject(sp);
577}
578
Ian Rogers848871b2013-08-05 10:56:33 -0700579// Visits arguments on the stack placing them into the shadow frame.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800580class BuildQuickShadowFrameVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700581 public:
Andreas Gampecf4035a2014-05-28 22:43:01 -0700582 BuildQuickShadowFrameVisitor(StackReference<mirror::ArtMethod>* sp, bool is_static,
583 const char* shorty, uint32_t shorty_len, ShadowFrame* sf,
584 size_t first_arg_reg) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700585 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), sf_(sf), cur_reg_(first_arg_reg) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700586
Ian Rogers9758f792014-03-13 09:02:55 -0700587 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700588
589 private:
Ian Rogers936b37f2014-02-14 00:52:24 -0800590 ShadowFrame* const sf_;
591 uint32_t cur_reg_;
Ian Rogers848871b2013-08-05 10:56:33 -0700592
Dragos Sbirleabd136a22013-08-13 18:07:04 -0700593 DISALLOW_COPY_AND_ASSIGN(BuildQuickShadowFrameVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700594};
595
Andreas Gampec200a4a2014-06-16 18:39:09 -0700596void BuildQuickShadowFrameVisitor::Visit() {
Ian Rogers9758f792014-03-13 09:02:55 -0700597 Primitive::Type type = GetParamPrimitiveType();
598 switch (type) {
599 case Primitive::kPrimLong: // Fall-through.
600 case Primitive::kPrimDouble:
601 if (IsSplitLongOrDouble()) {
602 sf_->SetVRegLong(cur_reg_, ReadSplitLongParam());
603 } else {
604 sf_->SetVRegLong(cur_reg_, *reinterpret_cast<jlong*>(GetParamAddress()));
605 }
606 ++cur_reg_;
607 break;
608 case Primitive::kPrimNot: {
609 StackReference<mirror::Object>* stack_ref =
610 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
611 sf_->SetVRegReference(cur_reg_, stack_ref->AsMirrorPtr());
612 }
613 break;
614 case Primitive::kPrimBoolean: // Fall-through.
615 case Primitive::kPrimByte: // Fall-through.
616 case Primitive::kPrimChar: // Fall-through.
617 case Primitive::kPrimShort: // Fall-through.
618 case Primitive::kPrimInt: // Fall-through.
619 case Primitive::kPrimFloat:
620 sf_->SetVReg(cur_reg_, *reinterpret_cast<jint*>(GetParamAddress()));
621 break;
622 case Primitive::kPrimVoid:
623 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700624 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -0700625 }
626 ++cur_reg_;
627}
628
Brian Carlstromea46f952013-07-30 01:26:50 -0700629extern "C" uint64_t artQuickToInterpreterBridge(mirror::ArtMethod* method, Thread* self,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700630 StackReference<mirror::ArtMethod>* sp)
Ian Rogers848871b2013-08-05 10:56:33 -0700631 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
632 // Ensure we don't get thread suspension until the object arguments are safely in the shadow
633 // frame.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700634 ScopedQuickEntrypointChecks sqec(self);
Ian Rogers848871b2013-08-05 10:56:33 -0700635
636 if (method->IsAbstract()) {
637 ThrowAbstractMethodError(method);
638 return 0;
639 } else {
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800640 DCHECK(!method->IsNative()) << PrettyMethod(method);
Andreas Gampec200a4a2014-06-16 18:39:09 -0700641 const char* old_cause = self->StartAssertNoThreadSuspension(
642 "Building interpreter shadow frame");
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700643 const DexFile::CodeItem* code_item = method->GetCodeItem();
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800644 DCHECK(code_item != nullptr) << PrettyMethod(method);
Ian Rogers848871b2013-08-05 10:56:33 -0700645 uint16_t num_regs = code_item->registers_size_;
646 void* memory = alloca(ShadowFrame::ComputeSize(num_regs));
Andreas Gampec200a4a2014-06-16 18:39:09 -0700647 // No last shadow coming from quick.
648 ShadowFrame* shadow_frame(ShadowFrame::Create(num_regs, nullptr, method, 0, memory));
Ian Rogers848871b2013-08-05 10:56:33 -0700649 size_t first_arg_reg = code_item->registers_size_ - code_item->ins_size_;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700650 uint32_t shorty_len = 0;
651 const char* shorty = method->GetShorty(&shorty_len);
652 BuildQuickShadowFrameVisitor shadow_frame_builder(sp, method->IsStatic(), shorty, shorty_len,
Ian Rogers936b37f2014-02-14 00:52:24 -0800653 shadow_frame, first_arg_reg);
Ian Rogers848871b2013-08-05 10:56:33 -0700654 shadow_frame_builder.VisitArguments();
Ian Rogerse94652f2014-12-02 11:13:19 -0800655 const bool needs_initialization =
656 method->IsStatic() && !method->GetDeclaringClass()->IsInitialized();
Ian Rogers848871b2013-08-05 10:56:33 -0700657 // Push a transition back into managed code onto the linked list in thread.
658 ManagedStack fragment;
659 self->PushManagedStackFragment(&fragment);
660 self->PushShadowFrame(shadow_frame);
661 self->EndAssertNoThreadSuspension(old_cause);
662
Ian Rogerse94652f2014-12-02 11:13:19 -0800663 if (needs_initialization) {
Ian Rogers848871b2013-08-05 10:56:33 -0700664 // Ensure static method's class is initialized.
Ian Rogerse94652f2014-12-02 11:13:19 -0800665 StackHandleScope<1> hs(self);
666 Handle<mirror::Class> h_class(hs.NewHandle(shadow_frame->GetMethod()->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700667 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
Ian Rogerse94652f2014-12-02 11:13:19 -0800668 DCHECK(Thread::Current()->IsExceptionPending()) << PrettyMethod(shadow_frame->GetMethod());
Ian Rogers848871b2013-08-05 10:56:33 -0700669 self->PopManagedStackFragment(fragment);
670 return 0;
671 }
672 }
Ian Rogerse94652f2014-12-02 11:13:19 -0800673 JValue result = interpreter::EnterInterpreterFromEntryPoint(self, code_item, shadow_frame);
Ian Rogers848871b2013-08-05 10:56:33 -0700674 // Pop transition.
675 self->PopManagedStackFragment(fragment);
Daniel Mihalyieb076692014-08-22 17:33:31 +0200676
677 // Request a stack deoptimization if needed
678 mirror::ArtMethod* caller = QuickArgumentVisitor::GetCallingMethod(sp);
679 if (UNLIKELY(Dbg::IsForcedInterpreterNeededForUpcall(self, caller))) {
680 self->SetException(Thread::GetDeoptimizationException());
681 self->SetDeoptimizationReturnValue(result);
682 }
683
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800684 // No need to restore the args since the method has already been run by the interpreter.
Ian Rogers848871b2013-08-05 10:56:33 -0700685 return result.GetJ();
686 }
687}
688
689// Visits arguments on the stack placing them into the args vector, Object* arguments are converted
690// to jobjects.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800691class BuildQuickArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700692 public:
Andreas Gampecf4035a2014-05-28 22:43:01 -0700693 BuildQuickArgumentVisitor(StackReference<mirror::ArtMethod>* sp, bool is_static,
694 const char* shorty, uint32_t shorty_len,
695 ScopedObjectAccessUnchecked* soa, std::vector<jvalue>* args) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700696 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa), args_(args) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700697
Ian Rogers9758f792014-03-13 09:02:55 -0700698 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700699
Ian Rogers9758f792014-03-13 09:02:55 -0700700 void FixupReferences() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800701
Ian Rogers848871b2013-08-05 10:56:33 -0700702 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700703 ScopedObjectAccessUnchecked* const soa_;
704 std::vector<jvalue>* const args_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800705 // References which we must update when exiting in case the GC moved the objects.
Ian Rogers700a4022014-05-19 16:49:03 -0700706 std::vector<std::pair<jobject, StackReference<mirror::Object>*>> references_;
Ian Rogers9758f792014-03-13 09:02:55 -0700707
Ian Rogers848871b2013-08-05 10:56:33 -0700708 DISALLOW_COPY_AND_ASSIGN(BuildQuickArgumentVisitor);
709};
710
Ian Rogers9758f792014-03-13 09:02:55 -0700711void BuildQuickArgumentVisitor::Visit() {
712 jvalue val;
713 Primitive::Type type = GetParamPrimitiveType();
714 switch (type) {
715 case Primitive::kPrimNot: {
716 StackReference<mirror::Object>* stack_ref =
717 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
718 val.l = soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
719 references_.push_back(std::make_pair(val.l, stack_ref));
720 break;
721 }
722 case Primitive::kPrimLong: // Fall-through.
723 case Primitive::kPrimDouble:
724 if (IsSplitLongOrDouble()) {
725 val.j = ReadSplitLongParam();
726 } else {
727 val.j = *reinterpret_cast<jlong*>(GetParamAddress());
728 }
729 break;
730 case Primitive::kPrimBoolean: // Fall-through.
731 case Primitive::kPrimByte: // Fall-through.
732 case Primitive::kPrimChar: // Fall-through.
733 case Primitive::kPrimShort: // Fall-through.
734 case Primitive::kPrimInt: // Fall-through.
735 case Primitive::kPrimFloat:
736 val.i = *reinterpret_cast<jint*>(GetParamAddress());
737 break;
738 case Primitive::kPrimVoid:
739 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700740 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -0700741 }
742 args_->push_back(val);
743}
744
745void BuildQuickArgumentVisitor::FixupReferences() {
746 // Fixup any references which may have changed.
747 for (const auto& pair : references_) {
748 pair.second->Assign(soa_->Decode<mirror::Object*>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -0700749 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -0700750 }
751}
752
Ian Rogers848871b2013-08-05 10:56:33 -0700753// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
754// which is responsible for recording callee save registers. We explicitly place into jobjects the
755// incoming reference arguments (so they survive GC). We invoke the invocation handler, which is a
756// field within the proxy object, which will box the primitive arguments and deal with error cases.
Brian Carlstromea46f952013-07-30 01:26:50 -0700757extern "C" uint64_t artQuickProxyInvokeHandler(mirror::ArtMethod* proxy_method,
Ian Rogers848871b2013-08-05 10:56:33 -0700758 mirror::Object* receiver,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700759 Thread* self, StackReference<mirror::ArtMethod>* sp)
Ian Rogers848871b2013-08-05 10:56:33 -0700760 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromd3633d52013-08-20 21:06:26 -0700761 DCHECK(proxy_method->IsProxyMethod()) << PrettyMethod(proxy_method);
762 DCHECK(receiver->GetClass()->IsProxyClass()) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700763 // Ensure we don't get thread suspension until the object arguments are safely in jobjects.
764 const char* old_cause =
765 self->StartAssertNoThreadSuspension("Adding to IRT proxy object arguments");
766 // Register the top of the managed stack, making stack crawlable.
Jeff Haof0a3f092014-07-24 16:26:09 -0700767 DCHECK_EQ(sp->AsMirrorPtr(), proxy_method) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700768 DCHECK_EQ(proxy_method->GetFrameSizeInBytes(),
Brian Carlstromd3633d52013-08-20 21:06:26 -0700769 Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes())
770 << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700771 self->VerifyStack();
772 // Start new JNI local reference state.
773 JNIEnvExt* env = self->GetJniEnv();
774 ScopedObjectAccessUnchecked soa(env);
775 ScopedJniEnvLocalRefState env_state(env);
776 // Create local ref. copies of proxy method and the receiver.
777 jobject rcvr_jobj = soa.AddLocalReference<jobject>(receiver);
778
779 // Placing arguments into args vector and remove the receiver.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700780 mirror::ArtMethod* non_proxy_method = proxy_method->GetInterfaceMethodIfProxy();
781 CHECK(!non_proxy_method->IsStatic()) << PrettyMethod(proxy_method) << " "
Andreas Gampec200a4a2014-06-16 18:39:09 -0700782 << PrettyMethod(non_proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700783 std::vector<jvalue> args;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700784 uint32_t shorty_len = 0;
785 const char* shorty = proxy_method->GetShorty(&shorty_len);
786 BuildQuickArgumentVisitor local_ref_visitor(sp, false, shorty, shorty_len, &soa, &args);
Brian Carlstromd3633d52013-08-20 21:06:26 -0700787
Ian Rogers848871b2013-08-05 10:56:33 -0700788 local_ref_visitor.VisitArguments();
Brian Carlstromd3633d52013-08-20 21:06:26 -0700789 DCHECK_GT(args.size(), 0U) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700790 args.erase(args.begin());
791
792 // Convert proxy method into expected interface method.
Brian Carlstromea46f952013-07-30 01:26:50 -0700793 mirror::ArtMethod* interface_method = proxy_method->FindOverriddenMethod();
Ian Rogerse0a02da2014-12-02 14:10:53 -0800794 DCHECK(interface_method != nullptr) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700795 DCHECK(!interface_method->IsProxyMethod()) << PrettyMethod(interface_method);
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700796 self->EndAssertNoThreadSuspension(old_cause);
797 jobject interface_method_jobj = soa.AddLocalReference<jobject>(
798 mirror::Method::CreateFromArtMethod(soa.Self(), interface_method));
Ian Rogers848871b2013-08-05 10:56:33 -0700799
800 // All naked Object*s should now be in jobjects, so its safe to go into the main invoke code
801 // that performs allocations.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700802 JValue result = InvokeProxyInvocationHandler(soa, shorty, rcvr_jobj, interface_method_jobj, args);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800803 // Restore references which might have moved.
804 local_ref_visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -0700805 return result.GetJ();
806}
807
808// Read object references held in arguments from quick frames and place in a JNI local references,
809// so they don't get garbage collected.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800810class RememberForGcArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700811 public:
Andreas Gampecf4035a2014-05-28 22:43:01 -0700812 RememberForGcArgumentVisitor(StackReference<mirror::ArtMethod>* sp, bool is_static,
813 const char* shorty, uint32_t shorty_len,
814 ScopedObjectAccessUnchecked* soa) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700815 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700816
Ian Rogers9758f792014-03-13 09:02:55 -0700817 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
Mathieu Chartier07d447b2013-09-26 11:57:43 -0700818
Ian Rogers9758f792014-03-13 09:02:55 -0700819 void FixupReferences() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers848871b2013-08-05 10:56:33 -0700820
821 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700822 ScopedObjectAccessUnchecked* const soa_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800823 // References which we must update when exiting in case the GC moved the objects.
Andreas Gampec200a4a2014-06-16 18:39:09 -0700824 std::vector<std::pair<jobject, StackReference<mirror::Object>*> > references_;
825
Mathieu Chartier590fee92013-09-13 13:46:47 -0700826 DISALLOW_COPY_AND_ASSIGN(RememberForGcArgumentVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700827};
828
Ian Rogers9758f792014-03-13 09:02:55 -0700829void RememberForGcArgumentVisitor::Visit() {
830 if (IsParamAReference()) {
831 StackReference<mirror::Object>* stack_ref =
832 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
833 jobject reference =
834 soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
835 references_.push_back(std::make_pair(reference, stack_ref));
836 }
837}
838
839void RememberForGcArgumentVisitor::FixupReferences() {
840 // Fixup any references which may have changed.
841 for (const auto& pair : references_) {
842 pair.second->Assign(soa_->Decode<mirror::Object*>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -0700843 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -0700844 }
845}
846
Ian Rogers848871b2013-08-05 10:56:33 -0700847// Lazily resolve a method for quick. Called by stub code.
Brian Carlstromea46f952013-07-30 01:26:50 -0700848extern "C" const void* artQuickResolutionTrampoline(mirror::ArtMethod* called,
Ian Rogers848871b2013-08-05 10:56:33 -0700849 mirror::Object* receiver,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700850 Thread* self,
851 StackReference<mirror::ArtMethod>* sp)
Ian Rogers848871b2013-08-05 10:56:33 -0700852 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampe3b45ef22015-05-26 21:34:09 -0700853 // The resolution trampoline stashes the resolved method into the callee-save frame to transport
854 // it. Thus, when exiting, the stack cannot be verified (as the resolved method most likely
855 // does not have the same stack layout as the callee-save method).
856 ScopedQuickEntrypointChecks sqec(self, kIsDebugBuild, false);
Ian Rogers848871b2013-08-05 10:56:33 -0700857 // Start new JNI local reference state
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800858 JNIEnvExt* env = self->GetJniEnv();
Ian Rogers848871b2013-08-05 10:56:33 -0700859 ScopedObjectAccessUnchecked soa(env);
860 ScopedJniEnvLocalRefState env_state(env);
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800861 const char* old_cause = self->StartAssertNoThreadSuspension("Quick method resolution set up");
Ian Rogers848871b2013-08-05 10:56:33 -0700862
863 // Compute details about the called method (avoid GCs)
864 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Ian Rogers848871b2013-08-05 10:56:33 -0700865 InvokeType invoke_type;
Ian Rogerse0a02da2014-12-02 14:10:53 -0800866 MethodReference called_method(nullptr, 0);
867 const bool called_method_known_on_entry = !called->IsRuntimeMethod();
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +0100868 mirror::ArtMethod* caller = nullptr;
Ian Rogerse0a02da2014-12-02 14:10:53 -0800869 if (!called_method_known_on_entry) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +0100870 caller = QuickArgumentVisitor::GetCallingMethod(sp);
871 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
Ian Rogers848871b2013-08-05 10:56:33 -0700872 const DexFile::CodeItem* code;
Ian Rogerse0a02da2014-12-02 14:10:53 -0800873 called_method.dex_file = caller->GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700874 code = caller->GetCodeItem();
Ian Rogers848871b2013-08-05 10:56:33 -0700875 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
876 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
877 Instruction::Code instr_code = instr->Opcode();
878 bool is_range;
879 switch (instr_code) {
880 case Instruction::INVOKE_DIRECT:
881 invoke_type = kDirect;
882 is_range = false;
883 break;
884 case Instruction::INVOKE_DIRECT_RANGE:
885 invoke_type = kDirect;
886 is_range = true;
887 break;
888 case Instruction::INVOKE_STATIC:
889 invoke_type = kStatic;
890 is_range = false;
891 break;
892 case Instruction::INVOKE_STATIC_RANGE:
893 invoke_type = kStatic;
894 is_range = true;
895 break;
896 case Instruction::INVOKE_SUPER:
897 invoke_type = kSuper;
898 is_range = false;
899 break;
900 case Instruction::INVOKE_SUPER_RANGE:
901 invoke_type = kSuper;
902 is_range = true;
903 break;
904 case Instruction::INVOKE_VIRTUAL:
905 invoke_type = kVirtual;
906 is_range = false;
907 break;
908 case Instruction::INVOKE_VIRTUAL_RANGE:
909 invoke_type = kVirtual;
910 is_range = true;
911 break;
912 case Instruction::INVOKE_INTERFACE:
913 invoke_type = kInterface;
914 is_range = false;
915 break;
916 case Instruction::INVOKE_INTERFACE_RANGE:
917 invoke_type = kInterface;
918 is_range = true;
919 break;
920 default:
Ian Rogerse0a02da2014-12-02 14:10:53 -0800921 LOG(FATAL) << "Unexpected call into trampoline: " << instr->DumpString(nullptr);
922 UNREACHABLE();
Ian Rogers848871b2013-08-05 10:56:33 -0700923 }
Ian Rogerse0a02da2014-12-02 14:10:53 -0800924 called_method.dex_method_index = (is_range) ? instr->VRegB_3rc() : instr->VRegB_35c();
Ian Rogers848871b2013-08-05 10:56:33 -0700925 } else {
926 invoke_type = kStatic;
Ian Rogerse0a02da2014-12-02 14:10:53 -0800927 called_method.dex_file = called->GetDexFile();
928 called_method.dex_method_index = called->GetDexMethodIndex();
Ian Rogers848871b2013-08-05 10:56:33 -0700929 }
930 uint32_t shorty_len;
931 const char* shorty =
Ian Rogerse0a02da2014-12-02 14:10:53 -0800932 called_method.dex_file->GetMethodShorty(
933 called_method.dex_file->GetMethodId(called_method.dex_method_index), &shorty_len);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700934 RememberForGcArgumentVisitor visitor(sp, invoke_type == kStatic, shorty, shorty_len, &soa);
Ian Rogers848871b2013-08-05 10:56:33 -0700935 visitor.VisitArguments();
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800936 self->EndAssertNoThreadSuspension(old_cause);
Ian Rogerse0a02da2014-12-02 14:10:53 -0800937 const bool virtual_or_interface = invoke_type == kVirtual || invoke_type == kInterface;
Ian Rogers848871b2013-08-05 10:56:33 -0700938 // Resolve method filling in dex cache.
Ian Rogerse0a02da2014-12-02 14:10:53 -0800939 if (!called_method_known_on_entry) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700940 StackHandleScope<1> hs(self);
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700941 mirror::Object* dummy = nullptr;
942 HandleWrapper<mirror::Object> h_receiver(
943 hs.NewHandleWrapper(virtual_or_interface ? &receiver : &dummy));
Ian Rogerse0a02da2014-12-02 14:10:53 -0800944 DCHECK_EQ(caller->GetDexFile(), called_method.dex_file);
945 called = linker->ResolveMethod(self, called_method.dex_method_index, &caller, invoke_type);
Ian Rogers848871b2013-08-05 10:56:33 -0700946 }
Ian Rogerse0a02da2014-12-02 14:10:53 -0800947 const void* code = nullptr;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800948 if (LIKELY(!self->IsExceptionPending())) {
Ian Rogers848871b2013-08-05 10:56:33 -0700949 // Incompatible class change should have been handled in resolve method.
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800950 CHECK(!called->CheckIncompatibleClassChange(invoke_type))
951 << PrettyMethod(called) << " " << invoke_type;
Mathieu Chartier55871bf2014-02-27 10:24:50 -0800952 if (virtual_or_interface) {
953 // Refine called method based on receiver.
954 CHECK(receiver != nullptr) << invoke_type;
Mingyao Yangf4867782014-05-05 11:55:02 -0700955
956 mirror::ArtMethod* orig_called = called;
Mathieu Chartier55871bf2014-02-27 10:24:50 -0800957 if (invoke_type == kVirtual) {
958 called = receiver->GetClass()->FindVirtualMethodForVirtual(called);
959 } else {
960 called = receiver->GetClass()->FindVirtualMethodForInterface(called);
961 }
Mingyao Yangf4867782014-05-05 11:55:02 -0700962
963 CHECK(called != nullptr) << PrettyMethod(orig_called) << " "
964 << PrettyTypeOf(receiver) << " "
965 << invoke_type << " " << orig_called->GetVtableIndex();
966
Ian Rogers83883d72013-10-21 21:07:24 -0700967 // 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 -0800968 // of the sharpened method avoiding dirtying the dex cache if possible.
Ian Rogers00f15272014-12-02 16:55:46 -0800969 // Note, called_method.dex_method_index references the dex method before the
970 // FindVirtualMethodFor... This is ok for FindDexMethodIndexInOtherDexFile that only cares
971 // about the name and signature.
972 uint32_t update_dex_cache_method_index = called->GetDexMethodIndex();
Ian Rogerse0a02da2014-12-02 14:10:53 -0800973 if (!called->HasSameDexCacheResolvedMethods(caller)) {
Ian Rogers83883d72013-10-21 21:07:24 -0700974 // Calling from one dex file to another, need to compute the method index appropriate to
Vladimir Markobbcc0c02014-02-03 14:08:42 +0000975 // the caller's dex file. Since we get here only if the original called was a runtime
976 // method, we've got the correct dex_file and a dex_method_idx from above.
Ian Rogerse0a02da2014-12-02 14:10:53 -0800977 DCHECK(!called_method_known_on_entry);
978 DCHECK_EQ(caller->GetDexFile(), called_method.dex_file);
979 const DexFile* caller_dex_file = called_method.dex_file;
980 uint32_t caller_method_name_and_sig_index = called_method.dex_method_index;
981 update_dex_cache_method_index =
982 called->FindDexMethodIndexInOtherDexFile(*caller_dex_file,
983 caller_method_name_and_sig_index);
984 }
985 if ((update_dex_cache_method_index != DexFile::kDexNoIndex) &&
986 (caller->GetDexCacheResolvedMethod(update_dex_cache_method_index) != called)) {
987 caller->SetDexCacheResolvedMethod(update_dex_cache_method_index, called);
Ian Rogers83883d72013-10-21 21:07:24 -0700988 }
Mathieu Chartiere4a91bb2015-01-28 13:11:44 -0800989 } else if (invoke_type == kStatic) {
990 const auto called_dex_method_idx = called->GetDexMethodIndex();
991 // For static invokes, we may dispatch to the static method in the superclass but resolve
992 // using the subclass. To prevent getting slow paths on each invoke, we force set the
993 // resolved method for the super class dex method index if we are in the same dex file.
994 // b/19175856
995 if (called->GetDexFile() == called_method.dex_file &&
996 called_method.dex_method_index != called_dex_method_idx) {
997 called->GetDexCache()->SetResolvedMethod(called_dex_method_idx, called);
998 }
Ian Rogers83883d72013-10-21 21:07:24 -0700999 }
Daniel Mihalyieb076692014-08-22 17:33:31 +02001000
Ian Rogers848871b2013-08-05 10:56:33 -07001001 // Ensure that the called method's class is initialized.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001002 StackHandleScope<1> hs(soa.Self());
1003 Handle<mirror::Class> called_class(hs.NewHandle(called->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -07001004 linker->EnsureInitialized(soa.Self(), called_class, true, true);
Ian Rogers848871b2013-08-05 10:56:33 -07001005 if (LIKELY(called_class->IsInitialized())) {
Daniel Mihalyieb076692014-08-22 17:33:31 +02001006 if (UNLIKELY(Dbg::IsForcedInterpreterNeededForResolution(self, called))) {
1007 // If we are single-stepping or the called method is deoptimized (by a
1008 // breakpoint, for example), then we have to execute the called method
1009 // with the interpreter.
1010 code = GetQuickToInterpreterBridge();
1011 } else if (UNLIKELY(Dbg::IsForcedInstrumentationNeededForResolution(self, caller))) {
1012 // If the caller is deoptimized (by a breakpoint, for example), we have to
1013 // continue its execution with interpreter when returning from the called
1014 // method. Because we do not want to execute the called method with the
1015 // interpreter, we wrap its execution into the instrumentation stubs.
1016 // When the called method returns, it will execute the instrumentation
1017 // exit hook that will determine the need of the interpreter with a call
1018 // to Dbg::IsForcedInterpreterNeededForUpcall and deoptimize the stack if
1019 // it is needed.
1020 code = GetQuickInstrumentationEntryPoint();
1021 } else {
1022 code = called->GetEntryPointFromQuickCompiledCode();
1023 }
Ian Rogers848871b2013-08-05 10:56:33 -07001024 } else if (called_class->IsInitializing()) {
Daniel Mihalyieb076692014-08-22 17:33:31 +02001025 if (UNLIKELY(Dbg::IsForcedInterpreterNeededForResolution(self, called))) {
1026 // If we are single-stepping or the called method is deoptimized (by a
1027 // breakpoint, for example), then we have to execute the called method
1028 // with the interpreter.
1029 code = GetQuickToInterpreterBridge();
1030 } else if (invoke_type == kStatic) {
Ian Rogers848871b2013-08-05 10:56:33 -07001031 // Class is still initializing, go to oat and grab code (trampoline must be left in place
1032 // until class is initialized to stop races between threads).
Ian Rogersef7d42f2014-01-06 12:55:46 -08001033 code = linker->GetQuickOatCodeFor(called);
Ian Rogers848871b2013-08-05 10:56:33 -07001034 } else {
1035 // No trampoline for non-static methods.
Ian Rogersef7d42f2014-01-06 12:55:46 -08001036 code = called->GetEntryPointFromQuickCompiledCode();
Ian Rogers848871b2013-08-05 10:56:33 -07001037 }
1038 } else {
1039 DCHECK(called_class->IsErroneous());
1040 }
1041 }
Ian Rogerse0a02da2014-12-02 14:10:53 -08001042 CHECK_EQ(code == nullptr, self->IsExceptionPending());
Mathieu Chartier07d447b2013-09-26 11:57:43 -07001043 // Fixup any locally saved objects may have moved during a GC.
1044 visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -07001045 // Place called method in callee-save frame to be placed as first argument to quick method.
Andreas Gampecf4035a2014-05-28 22:43:01 -07001046 sp->Assign(called);
Ian Rogers848871b2013-08-05 10:56:33 -07001047 return code;
1048}
1049
Andreas Gampec147b002014-03-06 18:11:06 -08001050/*
1051 * This class uses a couple of observations to unite the different calling conventions through
1052 * a few constants.
1053 *
1054 * 1) Number of registers used for passing is normally even, so counting down has no penalty for
1055 * possible alignment.
1056 * 2) Known 64b architectures store 8B units on the stack, both for integral and floating point
1057 * types, so using uintptr_t is OK. Also means that we can use kRegistersNeededX to denote
1058 * when we have to split things
1059 * 3) The only soft-float, Arm, is 32b, so no widening needs to be taken into account for floats
1060 * and we can use Int handling directly.
1061 * 4) Only 64b architectures widen, and their stack is aligned 8B anyways, so no padding code
1062 * necessary when widening. Also, widening of Ints will take place implicitly, and the
1063 * extension should be compatible with Aarch64, which mandates copying the available bits
1064 * into LSB and leaving the rest unspecified.
1065 * 5) Aligning longs and doubles is necessary on arm only, and it's the same in registers and on
1066 * the stack.
1067 * 6) There is only little endian.
1068 *
1069 *
1070 * Actual work is supposed to be done in a delegate of the template type. The interface is as
1071 * follows:
1072 *
1073 * void PushGpr(uintptr_t): Add a value for the next GPR
1074 *
1075 * void PushFpr4(float): Add a value for the next FPR of size 32b. Is only called if we need
1076 * padding, that is, think the architecture is 32b and aligns 64b.
1077 *
1078 * void PushFpr8(uint64_t): Push a double. We _will_ call this on 32b, it's the callee's job to
1079 * split this if necessary. The current state will have aligned, if
1080 * necessary.
1081 *
1082 * void PushStack(uintptr_t): Push a value to the stack.
1083 *
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001084 * uintptr_t PushHandleScope(mirror::Object* ref): Add a reference to the HandleScope. This _will_ have nullptr,
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001085 * as this might be important for null initialization.
Andreas Gampec147b002014-03-06 18:11:06 -08001086 * Must return the jobject, that is, the reference to the
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001087 * entry in the HandleScope (nullptr if necessary).
Andreas Gampec147b002014-03-06 18:11:06 -08001088 *
1089 */
Andreas Gampec200a4a2014-06-16 18:39:09 -07001090template<class T> class BuildNativeCallFrameStateMachine {
Andreas Gampec147b002014-03-06 18:11:06 -08001091 public:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001092#if defined(__arm__)
1093 // TODO: These are all dummy values!
Andreas Gampec147b002014-03-06 18:11:06 -08001094 static constexpr bool kNativeSoftFloatAbi = true;
1095 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs, r0-r3
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001096 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
1097
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001098 static constexpr size_t kRegistersNeededForLong = 2;
1099 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -08001100 static constexpr bool kMultiRegistersAligned = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001101 static constexpr bool kMultiFPRegistersWidened = false;
1102 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001103 static constexpr bool kAlignLongOnStack = true;
1104 static constexpr bool kAlignDoubleOnStack = true;
Stuart Monteithb95a5342014-03-12 13:32:32 +00001105#elif defined(__aarch64__)
1106 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
1107 static constexpr size_t kNumNativeGprArgs = 8; // 6 arguments passed in GPRs.
1108 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
1109
1110 static constexpr size_t kRegistersNeededForLong = 1;
1111 static constexpr size_t kRegistersNeededForDouble = 1;
1112 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001113 static constexpr bool kMultiFPRegistersWidened = false;
1114 static constexpr bool kMultiGPRegistersWidened = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +00001115 static constexpr bool kAlignLongOnStack = false;
1116 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001117#elif defined(__mips__) && !defined(__LP64__)
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001118 static constexpr bool kNativeSoftFloatAbi = true; // This is a hard float ABI.
Douglas Leung735b8552014-10-31 12:21:40 -07001119 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs.
1120 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001121
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001122 static constexpr size_t kRegistersNeededForLong = 2;
1123 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -08001124 static constexpr bool kMultiRegistersAligned = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001125 static constexpr bool kMultiFPRegistersWidened = true;
1126 static constexpr bool kMultiGPRegistersWidened = false;
Douglas Leung735b8552014-10-31 12:21:40 -07001127 static constexpr bool kAlignLongOnStack = true;
1128 static constexpr bool kAlignDoubleOnStack = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001129#elif defined(__mips__) && defined(__LP64__)
1130 // Let the code prepare GPRs only and we will load the FPRs with same data.
1131 static constexpr bool kNativeSoftFloatAbi = true;
1132 static constexpr size_t kNumNativeGprArgs = 8;
1133 static constexpr size_t kNumNativeFprArgs = 0;
1134
1135 static constexpr size_t kRegistersNeededForLong = 1;
1136 static constexpr size_t kRegistersNeededForDouble = 1;
1137 static constexpr bool kMultiRegistersAligned = false;
1138 static constexpr bool kMultiFPRegistersWidened = false;
1139 static constexpr bool kMultiGPRegistersWidened = true;
1140 static constexpr bool kAlignLongOnStack = false;
1141 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001142#elif defined(__i386__)
1143 // TODO: Check these!
Andreas Gampec147b002014-03-06 18:11:06 -08001144 static constexpr bool kNativeSoftFloatAbi = false; // Not using int registers for fp
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001145 static constexpr size_t kNumNativeGprArgs = 0; // 6 arguments passed in GPRs.
1146 static constexpr size_t kNumNativeFprArgs = 0; // 8 arguments passed in FPRs.
1147
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001148 static constexpr size_t kRegistersNeededForLong = 2;
1149 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001150 static constexpr bool kMultiRegistersAligned = false; // x86 not using regs, anyways
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001151 static constexpr bool kMultiFPRegistersWidened = false;
1152 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001153 static constexpr bool kAlignLongOnStack = false;
1154 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001155#elif defined(__x86_64__)
1156 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
1157 static constexpr size_t kNumNativeGprArgs = 6; // 6 arguments passed in GPRs.
1158 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
1159
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001160 static constexpr size_t kRegistersNeededForLong = 1;
1161 static constexpr size_t kRegistersNeededForDouble = 1;
Andreas Gampec147b002014-03-06 18:11:06 -08001162 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001163 static constexpr bool kMultiFPRegistersWidened = false;
1164 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001165 static constexpr bool kAlignLongOnStack = false;
1166 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001167#else
1168#error "Unsupported architecture"
1169#endif
1170
Andreas Gampec147b002014-03-06 18:11:06 -08001171 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001172 explicit BuildNativeCallFrameStateMachine(T* delegate)
1173 : gpr_index_(kNumNativeGprArgs),
1174 fpr_index_(kNumNativeFprArgs),
1175 stack_entries_(0),
1176 delegate_(delegate) {
Andreas Gampec147b002014-03-06 18:11:06 -08001177 // For register alignment, we want to assume that counters (gpr_index_, fpr_index_) are even iff
1178 // the next register is even; counting down is just to make the compiler happy...
Andreas Gampe575e78c2014-11-03 23:41:03 -08001179 static_assert(kNumNativeGprArgs % 2 == 0U, "Number of native GPR arguments not even");
1180 static_assert(kNumNativeFprArgs % 2 == 0U, "Number of native FPR arguments not even");
Andreas Gampec147b002014-03-06 18:11:06 -08001181 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001182
Andreas Gampec200a4a2014-06-16 18:39:09 -07001183 virtual ~BuildNativeCallFrameStateMachine() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001184
Ian Rogers1428dce2014-10-21 15:02:15 -07001185 bool HavePointerGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001186 return gpr_index_ > 0;
1187 }
1188
Andreas Gampec200a4a2014-06-16 18:39:09 -07001189 void AdvancePointer(const void* val) {
Andreas Gampec147b002014-03-06 18:11:06 -08001190 if (HavePointerGpr()) {
1191 gpr_index_--;
1192 PushGpr(reinterpret_cast<uintptr_t>(val));
1193 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001194 stack_entries_++; // TODO: have a field for pointer length as multiple of 32b
Andreas Gampec147b002014-03-06 18:11:06 -08001195 PushStack(reinterpret_cast<uintptr_t>(val));
1196 gpr_index_ = 0;
1197 }
1198 }
1199
Ian Rogers1428dce2014-10-21 15:02:15 -07001200 bool HaveHandleScopeGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001201 return gpr_index_ > 0;
1202 }
1203
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001204 void AdvanceHandleScope(mirror::Object* ptr) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1205 uintptr_t handle = PushHandle(ptr);
1206 if (HaveHandleScopeGpr()) {
Andreas Gampec147b002014-03-06 18:11:06 -08001207 gpr_index_--;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001208 PushGpr(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001209 } else {
1210 stack_entries_++;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001211 PushStack(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001212 gpr_index_ = 0;
1213 }
1214 }
1215
Ian Rogers1428dce2014-10-21 15:02:15 -07001216 bool HaveIntGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001217 return gpr_index_ > 0;
1218 }
1219
1220 void AdvanceInt(uint32_t val) {
1221 if (HaveIntGpr()) {
1222 gpr_index_--;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001223 if (kMultiGPRegistersWidened) {
1224 DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001225 PushGpr(static_cast<int64_t>(bit_cast<int32_t, uint32_t>(val)));
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001226 } else {
1227 PushGpr(val);
1228 }
Andreas Gampec147b002014-03-06 18:11:06 -08001229 } else {
1230 stack_entries_++;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001231 if (kMultiGPRegistersWidened) {
1232 DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001233 PushStack(static_cast<int64_t>(bit_cast<int32_t, uint32_t>(val)));
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001234 } else {
1235 PushStack(val);
1236 }
Andreas Gampec147b002014-03-06 18:11:06 -08001237 gpr_index_ = 0;
1238 }
1239 }
1240
Ian Rogers1428dce2014-10-21 15:02:15 -07001241 bool HaveLongGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001242 return gpr_index_ >= kRegistersNeededForLong + (LongGprNeedsPadding() ? 1 : 0);
1243 }
1244
Ian Rogers1428dce2014-10-21 15:02:15 -07001245 bool LongGprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001246 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1247 kAlignLongOnStack && // and when it needs alignment
1248 (gpr_index_ & 1) == 1; // counter is odd, see constructor
1249 }
1250
Ian Rogers1428dce2014-10-21 15:02:15 -07001251 bool LongStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001252 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1253 kAlignLongOnStack && // and when it needs 8B alignment
1254 (stack_entries_ & 1) == 1; // counter is odd
1255 }
1256
1257 void AdvanceLong(uint64_t val) {
1258 if (HaveLongGpr()) {
1259 if (LongGprNeedsPadding()) {
1260 PushGpr(0);
1261 gpr_index_--;
1262 }
1263 if (kRegistersNeededForLong == 1) {
1264 PushGpr(static_cast<uintptr_t>(val));
1265 } else {
1266 PushGpr(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1267 PushGpr(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1268 }
1269 gpr_index_ -= kRegistersNeededForLong;
1270 } else {
1271 if (LongStackNeedsPadding()) {
1272 PushStack(0);
1273 stack_entries_++;
1274 }
1275 if (kRegistersNeededForLong == 1) {
1276 PushStack(static_cast<uintptr_t>(val));
1277 stack_entries_++;
1278 } else {
1279 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1280 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1281 stack_entries_ += 2;
1282 }
1283 gpr_index_ = 0;
1284 }
1285 }
1286
Ian Rogers1428dce2014-10-21 15:02:15 -07001287 bool HaveFloatFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001288 return fpr_index_ > 0;
1289 }
1290
Andreas Gampec147b002014-03-06 18:11:06 -08001291 void AdvanceFloat(float val) {
1292 if (kNativeSoftFloatAbi) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001293 AdvanceInt(bit_cast<uint32_t, float>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001294 } else {
1295 if (HaveFloatFpr()) {
1296 fpr_index_--;
1297 if (kRegistersNeededForDouble == 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001298 if (kMultiFPRegistersWidened) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001299 PushFpr8(bit_cast<uint64_t, double>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001300 } else {
1301 // No widening, just use the bits.
Roland Levillainda4d79b2015-03-24 14:36:11 +00001302 PushFpr8(static_cast<uint64_t>(bit_cast<uint32_t, float>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001303 }
1304 } else {
1305 PushFpr4(val);
1306 }
1307 } else {
1308 stack_entries_++;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001309 if (kRegistersNeededForDouble == 1 && kMultiFPRegistersWidened) {
Andreas Gampec147b002014-03-06 18:11:06 -08001310 // Need to widen before storing: Note the "double" in the template instantiation.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001311 // Note: We need to jump through those hoops to make the compiler happy.
1312 DCHECK_EQ(sizeof(uintptr_t), sizeof(uint64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001313 PushStack(static_cast<uintptr_t>(bit_cast<uint64_t, double>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001314 } else {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001315 PushStack(static_cast<uintptr_t>(bit_cast<uint32_t, float>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001316 }
1317 fpr_index_ = 0;
1318 }
1319 }
1320 }
1321
Ian Rogers1428dce2014-10-21 15:02:15 -07001322 bool HaveDoubleFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001323 return fpr_index_ >= kRegistersNeededForDouble + (DoubleFprNeedsPadding() ? 1 : 0);
1324 }
1325
Ian Rogers1428dce2014-10-21 15:02:15 -07001326 bool DoubleFprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001327 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1328 kAlignDoubleOnStack && // and when it needs alignment
1329 (fpr_index_ & 1) == 1; // counter is odd, see constructor
1330 }
1331
Ian Rogers1428dce2014-10-21 15:02:15 -07001332 bool DoubleStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001333 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1334 kAlignDoubleOnStack && // and when it needs 8B alignment
1335 (stack_entries_ & 1) == 1; // counter is odd
1336 }
1337
1338 void AdvanceDouble(uint64_t val) {
1339 if (kNativeSoftFloatAbi) {
1340 AdvanceLong(val);
1341 } else {
1342 if (HaveDoubleFpr()) {
1343 if (DoubleFprNeedsPadding()) {
1344 PushFpr4(0);
1345 fpr_index_--;
1346 }
1347 PushFpr8(val);
1348 fpr_index_ -= kRegistersNeededForDouble;
1349 } else {
1350 if (DoubleStackNeedsPadding()) {
1351 PushStack(0);
1352 stack_entries_++;
1353 }
1354 if (kRegistersNeededForDouble == 1) {
1355 PushStack(static_cast<uintptr_t>(val));
1356 stack_entries_++;
1357 } else {
1358 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1359 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1360 stack_entries_ += 2;
1361 }
1362 fpr_index_ = 0;
1363 }
1364 }
1365 }
1366
Ian Rogers1428dce2014-10-21 15:02:15 -07001367 uint32_t GetStackEntries() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001368 return stack_entries_;
1369 }
1370
Ian Rogers1428dce2014-10-21 15:02:15 -07001371 uint32_t GetNumberOfUsedGprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001372 return kNumNativeGprArgs - gpr_index_;
1373 }
1374
Ian Rogers1428dce2014-10-21 15:02:15 -07001375 uint32_t GetNumberOfUsedFprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001376 return kNumNativeFprArgs - fpr_index_;
1377 }
1378
1379 private:
1380 void PushGpr(uintptr_t val) {
1381 delegate_->PushGpr(val);
1382 }
1383 void PushFpr4(float val) {
1384 delegate_->PushFpr4(val);
1385 }
1386 void PushFpr8(uint64_t val) {
1387 delegate_->PushFpr8(val);
1388 }
1389 void PushStack(uintptr_t val) {
1390 delegate_->PushStack(val);
1391 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001392 uintptr_t PushHandle(mirror::Object* ref) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1393 return delegate_->PushHandle(ref);
Andreas Gampec147b002014-03-06 18:11:06 -08001394 }
1395
1396 uint32_t gpr_index_; // Number of free GPRs
1397 uint32_t fpr_index_; // Number of free FPRs
1398 uint32_t stack_entries_; // Stack entries are in multiples of 32b, as floats are usually not
1399 // extended
Ian Rogers1428dce2014-10-21 15:02:15 -07001400 T* const delegate_; // What Push implementation gets called
Andreas Gampec147b002014-03-06 18:11:06 -08001401};
1402
Andreas Gampec200a4a2014-06-16 18:39:09 -07001403// Computes the sizes of register stacks and call stack area. Handling of references can be extended
1404// in subclasses.
1405//
1406// To handle native pointers, use "L" in the shorty for an object reference, which simulates
1407// them with handles.
1408class ComputeNativeCallFrameSize {
Andreas Gampec147b002014-03-06 18:11:06 -08001409 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001410 ComputeNativeCallFrameSize() : num_stack_entries_(0) {}
1411
1412 virtual ~ComputeNativeCallFrameSize() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001413
Ian Rogers1428dce2014-10-21 15:02:15 -07001414 uint32_t GetStackSize() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001415 return num_stack_entries_ * sizeof(uintptr_t);
1416 }
1417
Ian Rogers1428dce2014-10-21 15:02:15 -07001418 uint8_t* LayoutCallStack(uint8_t* sp8) const {
Andreas Gampec147b002014-03-06 18:11:06 -08001419 sp8 -= GetStackSize();
Andreas Gampe779f8c92014-06-09 18:29:38 -07001420 // Align by kStackAlignment.
1421 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
Andreas Gampec200a4a2014-06-16 18:39:09 -07001422 return sp8;
Andreas Gampec147b002014-03-06 18:11:06 -08001423 }
1424
Ian Rogers1428dce2014-10-21 15:02:15 -07001425 uint8_t* LayoutCallRegisterStacks(uint8_t* sp8, uintptr_t** start_gpr, uint32_t** start_fpr)
1426 const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001427 // Assumption is OK right now, as we have soft-float arm
1428 size_t fregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeFprArgs;
1429 sp8 -= fregs * sizeof(uintptr_t);
1430 *start_fpr = reinterpret_cast<uint32_t*>(sp8);
1431 size_t iregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeGprArgs;
1432 sp8 -= iregs * sizeof(uintptr_t);
1433 *start_gpr = reinterpret_cast<uintptr_t*>(sp8);
1434 return sp8;
1435 }
Andreas Gampec147b002014-03-06 18:11:06 -08001436
Andreas Gampec200a4a2014-06-16 18:39:09 -07001437 uint8_t* LayoutNativeCall(uint8_t* sp8, uintptr_t** start_stack, uintptr_t** start_gpr,
Ian Rogers1428dce2014-10-21 15:02:15 -07001438 uint32_t** start_fpr) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001439 // Native call stack.
1440 sp8 = LayoutCallStack(sp8);
1441 *start_stack = reinterpret_cast<uintptr_t*>(sp8);
Andreas Gampec147b002014-03-06 18:11:06 -08001442
Andreas Gampec200a4a2014-06-16 18:39:09 -07001443 // Put fprs and gprs below.
1444 sp8 = LayoutCallRegisterStacks(sp8, start_gpr, start_fpr);
Andreas Gampec147b002014-03-06 18:11:06 -08001445
Andreas Gampec200a4a2014-06-16 18:39:09 -07001446 // Return the new bottom.
1447 return sp8;
1448 }
1449
1450 virtual void WalkHeader(BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm)
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001451 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1452 UNUSED(sm);
1453 }
Andreas Gampec200a4a2014-06-16 18:39:09 -07001454
1455 void Walk(const char* shorty, uint32_t shorty_len) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1456 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize> sm(this);
1457
1458 WalkHeader(&sm);
Andreas Gampec147b002014-03-06 18:11:06 -08001459
1460 for (uint32_t i = 1; i < shorty_len; ++i) {
1461 Primitive::Type cur_type_ = Primitive::GetType(shorty[i]);
1462 switch (cur_type_) {
1463 case Primitive::kPrimNot:
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001464 // TODO: fix abuse of mirror types.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001465 sm.AdvanceHandleScope(
1466 reinterpret_cast<mirror::Object*>(0x12345678));
Andreas Gampec147b002014-03-06 18:11:06 -08001467 break;
1468
1469 case Primitive::kPrimBoolean:
1470 case Primitive::kPrimByte:
1471 case Primitive::kPrimChar:
1472 case Primitive::kPrimShort:
1473 case Primitive::kPrimInt:
1474 sm.AdvanceInt(0);
1475 break;
1476 case Primitive::kPrimFloat:
1477 sm.AdvanceFloat(0);
1478 break;
1479 case Primitive::kPrimDouble:
1480 sm.AdvanceDouble(0);
1481 break;
1482 case Primitive::kPrimLong:
1483 sm.AdvanceLong(0);
1484 break;
1485 default:
1486 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001487 UNREACHABLE();
Andreas Gampec147b002014-03-06 18:11:06 -08001488 }
1489 }
1490
Ian Rogers1428dce2014-10-21 15:02:15 -07001491 num_stack_entries_ = sm.GetStackEntries();
Andreas Gampec147b002014-03-06 18:11:06 -08001492 }
1493
1494 void PushGpr(uintptr_t /* val */) {
1495 // not optimizing registers, yet
1496 }
1497
1498 void PushFpr4(float /* val */) {
1499 // not optimizing registers, yet
1500 }
1501
1502 void PushFpr8(uint64_t /* val */) {
1503 // not optimizing registers, yet
1504 }
1505
1506 void PushStack(uintptr_t /* val */) {
1507 // counting is already done in the superclass
1508 }
1509
Andreas Gampec200a4a2014-06-16 18:39:09 -07001510 virtual uintptr_t PushHandle(mirror::Object* /* ptr */) {
Andreas Gampec147b002014-03-06 18:11:06 -08001511 return reinterpret_cast<uintptr_t>(nullptr);
1512 }
1513
Andreas Gampec200a4a2014-06-16 18:39:09 -07001514 protected:
Andreas Gampec147b002014-03-06 18:11:06 -08001515 uint32_t num_stack_entries_;
1516};
1517
Andreas Gampec200a4a2014-06-16 18:39:09 -07001518class ComputeGenericJniFrameSize FINAL : public ComputeNativeCallFrameSize {
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001519 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001520 ComputeGenericJniFrameSize() : num_handle_scope_references_(0) {}
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001521
Andreas Gampec200a4a2014-06-16 18:39:09 -07001522 // Lays out the callee-save frame. Assumes that the incorrect frame corresponding to RefsAndArgs
1523 // is at *m = sp. Will update to point to the bottom of the save frame.
1524 //
1525 // Note: assumes ComputeAll() has been run before.
Ian Rogers59c07062014-10-10 13:03:39 -07001526 void LayoutCalleeSaveFrame(Thread* self, StackReference<mirror::ArtMethod>** m, void* sp,
1527 HandleScope** handle_scope)
Andreas Gampec200a4a2014-06-16 18:39:09 -07001528 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1529 mirror::ArtMethod* method = (*m)->AsMirrorPtr();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001530
Andreas Gampec200a4a2014-06-16 18:39:09 -07001531 uint8_t* sp8 = reinterpret_cast<uint8_t*>(sp);
1532
1533 // First, fix up the layout of the callee-save frame.
1534 // We have to squeeze in the HandleScope, and relocate the method pointer.
1535
1536 // "Free" the slot for the method.
Ian Rogers13735952014-10-08 12:43:28 -07001537 sp8 += sizeof(void*); // In the callee-save frame we use a full pointer.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001538
1539 // Under the callee saves put handle scope and new method stack reference.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001540 size_t handle_scope_size = HandleScope::SizeOf(num_handle_scope_references_);
1541 size_t scope_and_method = handle_scope_size + sizeof(StackReference<mirror::ArtMethod>);
1542
1543 sp8 -= scope_and_method;
1544 // Align by kStackAlignment.
1545 sp8 = reinterpret_cast<uint8_t*>(RoundDown(
1546 reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
1547
1548 uint8_t* sp8_table = sp8 + sizeof(StackReference<mirror::ArtMethod>);
Ian Rogers59c07062014-10-10 13:03:39 -07001549 *handle_scope = HandleScope::Create(sp8_table, self->GetTopHandleScope(),
1550 num_handle_scope_references_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001551
1552 // Add a slot for the method pointer, and fill it. Fix the pointer-pointer given to us.
1553 uint8_t* method_pointer = sp8;
1554 StackReference<mirror::ArtMethod>* new_method_ref =
1555 reinterpret_cast<StackReference<mirror::ArtMethod>*>(method_pointer);
1556 new_method_ref->Assign(method);
1557 *m = new_method_ref;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001558 }
1559
Andreas Gampec200a4a2014-06-16 18:39:09 -07001560 // Adds space for the cookie. Note: may leave stack unaligned.
Ian Rogers1428dce2014-10-21 15:02:15 -07001561 void LayoutCookie(uint8_t** sp) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001562 // Reference cookie and padding
1563 *sp -= 8;
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001564 }
1565
Andreas Gampec200a4a2014-06-16 18:39:09 -07001566 // Re-layout the callee-save frame (insert a handle-scope). Then add space for the cookie.
1567 // Returns the new bottom. Note: this may be unaligned.
Ian Rogers59c07062014-10-10 13:03:39 -07001568 uint8_t* LayoutJNISaveFrame(Thread* self, StackReference<mirror::ArtMethod>** m, void* sp,
1569 HandleScope** handle_scope)
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001570 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001571 // First, fix up the layout of the callee-save frame.
1572 // We have to squeeze in the HandleScope, and relocate the method pointer.
Ian Rogers59c07062014-10-10 13:03:39 -07001573 LayoutCalleeSaveFrame(self, m, sp, handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001574
1575 // The bottom of the callee-save frame is now where the method is, *m.
1576 uint8_t* sp8 = reinterpret_cast<uint8_t*>(*m);
1577
1578 // Add space for cookie.
1579 LayoutCookie(&sp8);
1580
1581 return sp8;
1582 }
1583
1584 // WARNING: After this, *sp won't be pointing to the method anymore!
Ian Rogers59c07062014-10-10 13:03:39 -07001585 uint8_t* ComputeLayout(Thread* self, StackReference<mirror::ArtMethod>** m,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001586 const char* shorty, uint32_t shorty_len, HandleScope** handle_scope,
Andreas Gampec200a4a2014-06-16 18:39:09 -07001587 uintptr_t** start_stack, uintptr_t** start_gpr, uint32_t** start_fpr)
1588 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1589 Walk(shorty, shorty_len);
1590
1591 // JNI part.
Ian Rogers59c07062014-10-10 13:03:39 -07001592 uint8_t* sp8 = LayoutJNISaveFrame(self, m, reinterpret_cast<void*>(*m), handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001593
1594 sp8 = LayoutNativeCall(sp8, start_stack, start_gpr, start_fpr);
1595
1596 // Return the new bottom.
1597 return sp8;
1598 }
1599
1600 uintptr_t PushHandle(mirror::Object* /* ptr */) OVERRIDE;
1601
1602 // Add JNIEnv* and jobj/jclass before the shorty-derived elements.
1603 void WalkHeader(BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) OVERRIDE
1604 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
1605
1606 private:
1607 uint32_t num_handle_scope_references_;
1608};
1609
1610uintptr_t ComputeGenericJniFrameSize::PushHandle(mirror::Object* /* ptr */) {
1611 num_handle_scope_references_++;
1612 return reinterpret_cast<uintptr_t>(nullptr);
1613}
1614
1615void ComputeGenericJniFrameSize::WalkHeader(
1616 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) {
1617 // JNIEnv
1618 sm->AdvancePointer(nullptr);
1619
1620 // Class object or this as first argument
1621 sm->AdvanceHandleScope(reinterpret_cast<mirror::Object*>(0x12345678));
1622}
1623
1624// Class to push values to three separate regions. Used to fill the native call part. Adheres to
1625// the template requirements of BuildGenericJniFrameStateMachine.
1626class FillNativeCall {
1627 public:
1628 FillNativeCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) :
1629 cur_gpr_reg_(gpr_regs), cur_fpr_reg_(fpr_regs), cur_stack_arg_(stack_args) {}
1630
1631 virtual ~FillNativeCall() {}
1632
1633 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) {
1634 cur_gpr_reg_ = gpr_regs;
1635 cur_fpr_reg_ = fpr_regs;
1636 cur_stack_arg_ = stack_args;
Andreas Gampec147b002014-03-06 18:11:06 -08001637 }
1638
1639 void PushGpr(uintptr_t val) {
1640 *cur_gpr_reg_ = val;
1641 cur_gpr_reg_++;
1642 }
1643
1644 void PushFpr4(float val) {
1645 *cur_fpr_reg_ = val;
1646 cur_fpr_reg_++;
1647 }
1648
1649 void PushFpr8(uint64_t val) {
1650 uint64_t* tmp = reinterpret_cast<uint64_t*>(cur_fpr_reg_);
1651 *tmp = val;
1652 cur_fpr_reg_ += 2;
1653 }
1654
1655 void PushStack(uintptr_t val) {
1656 *cur_stack_arg_ = val;
1657 cur_stack_arg_++;
1658 }
1659
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001660 virtual uintptr_t PushHandle(mirror::Object*) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001661 LOG(FATAL) << "(Non-JNI) Native call does not use handles.";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001662 UNREACHABLE();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001663 }
1664
1665 private:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001666 uintptr_t* cur_gpr_reg_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001667 uint32_t* cur_fpr_reg_;
1668 uintptr_t* cur_stack_arg_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001669};
Andreas Gampec147b002014-03-06 18:11:06 -08001670
Andreas Gampec200a4a2014-06-16 18:39:09 -07001671// Visits arguments on the stack placing them into a region lower down the stack for the benefit
1672// of transitioning into native code.
1673class BuildGenericJniFrameVisitor FINAL : public QuickArgumentVisitor {
1674 public:
Ian Rogers59c07062014-10-10 13:03:39 -07001675 BuildGenericJniFrameVisitor(Thread* self, bool is_static, const char* shorty, uint32_t shorty_len,
1676 StackReference<mirror::ArtMethod>** sp)
Andreas Gampec200a4a2014-06-16 18:39:09 -07001677 : QuickArgumentVisitor(*sp, is_static, shorty, shorty_len),
1678 jni_call_(nullptr, nullptr, nullptr, nullptr), sm_(&jni_call_) {
1679 ComputeGenericJniFrameSize fsc;
1680 uintptr_t* start_gpr_reg;
1681 uint32_t* start_fpr_reg;
1682 uintptr_t* start_stack_arg;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001683 bottom_of_used_area_ = fsc.ComputeLayout(self, sp, shorty, shorty_len,
Ian Rogers59c07062014-10-10 13:03:39 -07001684 &handle_scope_,
1685 &start_stack_arg,
Andreas Gampec200a4a2014-06-16 18:39:09 -07001686 &start_gpr_reg, &start_fpr_reg);
1687
Andreas Gampec200a4a2014-06-16 18:39:09 -07001688 jni_call_.Reset(start_gpr_reg, start_fpr_reg, start_stack_arg, handle_scope_);
1689
1690 // jni environment is always first argument
1691 sm_.AdvancePointer(self->GetJniEnv());
1692
1693 if (is_static) {
1694 sm_.AdvanceHandleScope((*sp)->AsMirrorPtr()->GetDeclaringClass());
1695 }
1696 }
1697
1698 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
1699
1700 void FinalizeHandleScope(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
1701
1702 StackReference<mirror::Object>* GetFirstHandleScopeEntry()
1703 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1704 return handle_scope_->GetHandle(0).GetReference();
1705 }
1706
Ian Rogers1428dce2014-10-21 15:02:15 -07001707 jobject GetFirstHandleScopeJObject() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001708 return handle_scope_->GetHandle(0).ToJObject();
1709 }
1710
Ian Rogers1428dce2014-10-21 15:02:15 -07001711 void* GetBottomOfUsedArea() const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001712 return bottom_of_used_area_;
1713 }
1714
1715 private:
1716 // A class to fill a JNI call. Adds reference/handle-scope management to FillNativeCall.
1717 class FillJniCall FINAL : public FillNativeCall {
1718 public:
1719 FillJniCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args,
1720 HandleScope* handle_scope) : FillNativeCall(gpr_regs, fpr_regs, stack_args),
1721 handle_scope_(handle_scope), cur_entry_(0) {}
1722
1723 uintptr_t PushHandle(mirror::Object* ref) OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
1724
1725 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args, HandleScope* scope) {
1726 FillNativeCall::Reset(gpr_regs, fpr_regs, stack_args);
1727 handle_scope_ = scope;
1728 cur_entry_ = 0U;
1729 }
1730
1731 void ResetRemainingScopeSlots() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1732 // Initialize padding entries.
1733 size_t expected_slots = handle_scope_->NumberOfReferences();
1734 while (cur_entry_ < expected_slots) {
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07001735 handle_scope_->GetMutableHandle(cur_entry_++).Assign(nullptr);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001736 }
1737 DCHECK_NE(cur_entry_, 0U);
1738 }
1739
1740 private:
1741 HandleScope* handle_scope_;
1742 size_t cur_entry_;
1743 };
1744
1745 HandleScope* handle_scope_;
1746 FillJniCall jni_call_;
1747 void* bottom_of_used_area_;
1748
1749 BuildNativeCallFrameStateMachine<FillJniCall> sm_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001750
1751 DISALLOW_COPY_AND_ASSIGN(BuildGenericJniFrameVisitor);
1752};
1753
Andreas Gampec200a4a2014-06-16 18:39:09 -07001754uintptr_t BuildGenericJniFrameVisitor::FillJniCall::PushHandle(mirror::Object* ref) {
1755 uintptr_t tmp;
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07001756 MutableHandle<mirror::Object> h = handle_scope_->GetMutableHandle(cur_entry_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001757 h.Assign(ref);
1758 tmp = reinterpret_cast<uintptr_t>(h.ToJObject());
1759 cur_entry_++;
1760 return tmp;
1761}
1762
Ian Rogers9758f792014-03-13 09:02:55 -07001763void BuildGenericJniFrameVisitor::Visit() {
1764 Primitive::Type type = GetParamPrimitiveType();
1765 switch (type) {
1766 case Primitive::kPrimLong: {
1767 jlong long_arg;
1768 if (IsSplitLongOrDouble()) {
1769 long_arg = ReadSplitLongParam();
1770 } else {
1771 long_arg = *reinterpret_cast<jlong*>(GetParamAddress());
1772 }
1773 sm_.AdvanceLong(long_arg);
1774 break;
1775 }
1776 case Primitive::kPrimDouble: {
1777 uint64_t double_arg;
1778 if (IsSplitLongOrDouble()) {
1779 // Read into union so that we don't case to a double.
1780 double_arg = ReadSplitLongParam();
1781 } else {
1782 double_arg = *reinterpret_cast<uint64_t*>(GetParamAddress());
1783 }
1784 sm_.AdvanceDouble(double_arg);
1785 break;
1786 }
1787 case Primitive::kPrimNot: {
1788 StackReference<mirror::Object>* stack_ref =
1789 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001790 sm_.AdvanceHandleScope(stack_ref->AsMirrorPtr());
Ian Rogers9758f792014-03-13 09:02:55 -07001791 break;
1792 }
1793 case Primitive::kPrimFloat:
1794 sm_.AdvanceFloat(*reinterpret_cast<float*>(GetParamAddress()));
1795 break;
1796 case Primitive::kPrimBoolean: // Fall-through.
1797 case Primitive::kPrimByte: // Fall-through.
1798 case Primitive::kPrimChar: // Fall-through.
1799 case Primitive::kPrimShort: // Fall-through.
1800 case Primitive::kPrimInt: // Fall-through.
1801 sm_.AdvanceInt(*reinterpret_cast<jint*>(GetParamAddress()));
1802 break;
1803 case Primitive::kPrimVoid:
1804 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -07001805 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -07001806 }
1807}
1808
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001809void BuildGenericJniFrameVisitor::FinalizeHandleScope(Thread* self) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001810 // Clear out rest of the scope.
1811 jni_call_.ResetRemainingScopeSlots();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001812 // Install HandleScope.
1813 self->PushHandleScope(handle_scope_);
Ian Rogers9758f792014-03-13 09:02:55 -07001814}
1815
Ian Rogers04c31d22014-07-07 21:44:06 -07001816#if defined(__arm__) || defined(__aarch64__)
Andreas Gampe90546832014-03-12 18:07:19 -07001817extern "C" void* artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07001818#else
1819extern "C" void* artFindNativeMethod(Thread* self);
1820#endif
Andreas Gampe90546832014-03-12 18:07:19 -07001821
Andreas Gampead615172014-04-04 16:20:13 -07001822uint64_t artQuickGenericJniEndJNIRef(Thread* self, uint32_t cookie, jobject l, jobject lock) {
1823 if (lock != nullptr) {
1824 return reinterpret_cast<uint64_t>(JniMethodEndWithReferenceSynchronized(l, cookie, lock, self));
1825 } else {
1826 return reinterpret_cast<uint64_t>(JniMethodEndWithReference(l, cookie, self));
1827 }
1828}
1829
1830void artQuickGenericJniEndJNINonRef(Thread* self, uint32_t cookie, jobject lock) {
1831 if (lock != nullptr) {
1832 JniMethodEndSynchronized(cookie, lock, self);
1833 } else {
1834 JniMethodEnd(cookie, self);
1835 }
1836}
1837
Andreas Gampec147b002014-03-06 18:11:06 -08001838/*
1839 * Initializes an alloca region assumed to be directly below sp for a native call:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001840 * 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 -08001841 * The final element on the stack is a pointer to the native code.
1842 *
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001843 * On entry, the stack has a standard callee-save frame above sp, and an alloca below it.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001844 * We need to fix this, as the handle scope needs to go into the callee-save frame.
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001845 *
Andreas Gampec147b002014-03-06 18:11:06 -08001846 * The return of this function denotes:
1847 * 1) How many bytes of the alloca can be released, if the value is non-negative.
1848 * 2) An error, if the value is negative.
1849 */
Andreas Gampec200a4a2014-06-16 18:39:09 -07001850extern "C" TwoWordReturn artQuickGenericJniTrampoline(Thread* self,
1851 StackReference<mirror::ArtMethod>* sp)
Andreas Gampe2da88232014-02-27 12:26:20 -08001852 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampecf4035a2014-05-28 22:43:01 -07001853 mirror::ArtMethod* called = sp->AsMirrorPtr();
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001854 DCHECK(called->IsNative()) << PrettyMethod(called, true);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001855 uint32_t shorty_len = 0;
1856 const char* shorty = called->GetShorty(&shorty_len);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001857
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001858 // Run the visitor and update sp.
Ian Rogers59c07062014-10-10 13:03:39 -07001859 BuildGenericJniFrameVisitor visitor(self, called->IsStatic(), shorty, shorty_len, &sp);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001860 visitor.VisitArguments();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001861 visitor.FinalizeHandleScope(self);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001862
Andreas Gampec200a4a2014-06-16 18:39:09 -07001863 // Fix up managed-stack things in Thread.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001864 self->SetTopOfStack(sp);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001865
Ian Rogerse0dcd462014-03-08 15:21:04 -08001866 self->VerifyStack();
1867
Andreas Gampe90546832014-03-12 18:07:19 -07001868 // Start JNI, save the cookie.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001869 uint32_t cookie;
1870 if (called->IsSynchronized()) {
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001871 cookie = JniMethodStartSynchronized(visitor.GetFirstHandleScopeJObject(), self);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001872 if (self->IsExceptionPending()) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001873 self->PopHandleScope();
Andreas Gampec147b002014-03-06 18:11:06 -08001874 // A negative value denotes an error.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001875 return GetTwoWordFailureValue();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001876 }
1877 } else {
1878 cookie = JniMethodStart(self);
1879 }
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001880 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
Ian Rogerse0dcd462014-03-08 15:21:04 -08001881 *(sp32 - 1) = cookie;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001882
Andreas Gampe90546832014-03-12 18:07:19 -07001883 // Retrieve the stored native code.
Mathieu Chartier2d721012014-11-10 11:08:06 -08001884 void* nativeCode = called->GetEntryPointFromJni();
Andreas Gampe90546832014-03-12 18:07:19 -07001885
Andreas Gampe9a6a99a2014-03-14 07:52:20 -07001886 // There are two cases for the content of nativeCode:
1887 // 1) Pointer to the native function.
1888 // 2) Pointer to the trampoline for native code binding.
1889 // In the second case, we need to execute the binding and continue with the actual native function
1890 // pointer.
Andreas Gampe90546832014-03-12 18:07:19 -07001891 DCHECK(nativeCode != nullptr);
1892 if (nativeCode == GetJniDlsymLookupStub()) {
Ian Rogers04c31d22014-07-07 21:44:06 -07001893#if defined(__arm__) || defined(__aarch64__)
Andreas Gampe90546832014-03-12 18:07:19 -07001894 nativeCode = artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07001895#else
1896 nativeCode = artFindNativeMethod(self);
1897#endif
Andreas Gampe90546832014-03-12 18:07:19 -07001898
1899 if (nativeCode == nullptr) {
1900 DCHECK(self->IsExceptionPending()); // There should be an exception pending now.
Andreas Gampead615172014-04-04 16:20:13 -07001901
1902 // End JNI, as the assembly will move to deliver the exception.
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001903 jobject lock = called->IsSynchronized() ? visitor.GetFirstHandleScopeJObject() : nullptr;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001904 if (shorty[0] == 'L') {
Andreas Gampead615172014-04-04 16:20:13 -07001905 artQuickGenericJniEndJNIRef(self, cookie, nullptr, lock);
1906 } else {
1907 artQuickGenericJniEndJNINonRef(self, cookie, lock);
1908 }
1909
Andreas Gampec200a4a2014-06-16 18:39:09 -07001910 return GetTwoWordFailureValue();
Andreas Gampe90546832014-03-12 18:07:19 -07001911 }
1912 // Note that the native code pointer will be automatically set by artFindNativeMethod().
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001913 }
1914
Andreas Gampec200a4a2014-06-16 18:39:09 -07001915 // Return native code addr(lo) and bottom of alloca address(hi).
1916 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(visitor.GetBottomOfUsedArea()),
1917 reinterpret_cast<uintptr_t>(nativeCode));
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001918}
1919
1920/*
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001921 * Is called after the native JNI code. Responsible for cleanup (handle scope, saved state) and
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001922 * unlocking.
1923 */
Andreas Gampec200a4a2014-06-16 18:39:09 -07001924extern "C" uint64_t artQuickGenericJniEndTrampoline(Thread* self, jvalue result, uint64_t result_f)
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001925 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001926 StackReference<mirror::ArtMethod>* sp = self->GetManagedStack()->GetTopQuickFrame();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001927 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
Andreas Gampecf4035a2014-05-28 22:43:01 -07001928 mirror::ArtMethod* called = sp->AsMirrorPtr();
Ian Rogerse0dcd462014-03-08 15:21:04 -08001929 uint32_t cookie = *(sp32 - 1);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001930
Andreas Gampead615172014-04-04 16:20:13 -07001931 jobject lock = nullptr;
1932 if (called->IsSynchronized()) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001933 HandleScope* table = reinterpret_cast<HandleScope*>(reinterpret_cast<uint8_t*>(sp)
1934 + sizeof(StackReference<mirror::ArtMethod>));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001935 lock = table->GetHandle(0).ToJObject();
Andreas Gampead615172014-04-04 16:20:13 -07001936 }
1937
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001938 char return_shorty_char = called->GetShorty()[0];
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001939
1940 if (return_shorty_char == 'L') {
Andreas Gampead615172014-04-04 16:20:13 -07001941 return artQuickGenericJniEndJNIRef(self, cookie, result.l, lock);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001942 } else {
Andreas Gampead615172014-04-04 16:20:13 -07001943 artQuickGenericJniEndJNINonRef(self, cookie, lock);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001944
1945 switch (return_shorty_char) {
Nicolas Geoffray54accbc2014-08-13 03:40:45 +01001946 case 'F': {
1947 if (kRuntimeISA == kX86) {
1948 // Convert back the result to float.
Roland Levillainda4d79b2015-03-24 14:36:11 +00001949 double d = bit_cast<double, uint64_t>(result_f);
1950 return bit_cast<uint32_t, float>(static_cast<float>(d));
Nicolas Geoffray54accbc2014-08-13 03:40:45 +01001951 } else {
1952 return result_f;
1953 }
1954 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001955 case 'D':
1956 return result_f;
1957 case 'Z':
1958 return result.z;
1959 case 'B':
1960 return result.b;
1961 case 'C':
1962 return result.c;
1963 case 'S':
1964 return result.s;
1965 case 'I':
1966 return result.i;
1967 case 'J':
1968 return result.j;
1969 case 'V':
1970 return 0;
1971 default:
1972 LOG(FATAL) << "Unexpected return shorty character " << return_shorty_char;
1973 return 0;
1974 }
1975 }
Andreas Gampe2da88232014-02-27 12:26:20 -08001976}
1977
Andreas Gamped58342c2014-06-05 14:18:08 -07001978// We use TwoWordReturn to optimize scalar returns. We use the hi value for code, and the lo value
1979// for the method pointer.
Andreas Gampe51f76352014-05-21 08:28:48 -07001980//
Andreas Gamped58342c2014-06-05 14:18:08 -07001981// It is valid to use this, as at the usage points here (returns from C functions) we are assuming
1982// to hold the mutator lock (see SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) annotations).
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001983
1984template<InvokeType type, bool access_check>
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01001985static TwoWordReturn artInvokeCommon(uint32_t method_idx,
1986 mirror::Object* this_object,
1987 Thread* self,
1988 StackReference<mirror::ArtMethod>* sp) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001989 ScopedQuickEntrypointChecks sqec(self);
1990 DCHECK_EQ(sp->AsMirrorPtr(), Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs));
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01001991 mirror::ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001992 mirror::ArtMethod* method = FindMethodFast(method_idx, this_object, caller_method, access_check,
1993 type);
1994 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001995 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()->GetDexFile();
1996 uint32_t shorty_len;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001997 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(method_idx), &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001998 {
1999 // Remember the args in case a GC happens in FindMethodFromCode.
2000 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
2001 RememberForGcArgumentVisitor visitor(sp, type == kStatic, shorty, shorty_len, &soa);
2002 visitor.VisitArguments();
Mathieu Chartier0cd81352014-05-22 16:48:55 -07002003 method = FindMethodFromCode<type, access_check>(method_idx, &this_object, &caller_method,
2004 self);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002005 visitor.FixupReferences();
2006 }
2007
Ian Rogerse0a02da2014-12-02 14:10:53 -08002008 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002009 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07002010 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002011 }
2012 }
2013 DCHECK(!self->IsExceptionPending());
2014 const void* code = method->GetEntryPointFromQuickCompiledCode();
2015
2016 // When we return, the caller will branch to this address, so it had better not be 0!
Ian Rogerse0a02da2014-12-02 14:10:53 -08002017 DCHECK(code != nullptr) << "Code was null in method: " << PrettyMethod(method)
Andreas Gampec200a4a2014-06-16 18:39:09 -07002018 << " location: "
2019 << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07002020
Andreas Gamped58342c2014-06-05 14:18:08 -07002021 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
2022 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002023}
2024
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01002025// Explicit artInvokeCommon template function declarations to please analysis tool.
2026#define EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(type, access_check) \
2027 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) \
Andreas Gamped58342c2014-06-05 14:18:08 -07002028 TwoWordReturn artInvokeCommon<type, access_check>(uint32_t method_idx, \
Andreas Gampe51f76352014-05-21 08:28:48 -07002029 mirror::Object* this_object, \
Andreas Gampecf4035a2014-05-28 22:43:01 -07002030 Thread* self, \
2031 StackReference<mirror::ArtMethod>* sp) \
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01002032
2033EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, false);
2034EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, true);
2035EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, false);
2036EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, true);
2037EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, false);
2038EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, true);
2039EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, false);
2040EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, true);
2041EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, false);
2042EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, true);
2043#undef EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL
2044
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002045// See comments in runtime_support_asm.S
Andreas Gampec200a4a2014-06-16 18:39:09 -07002046extern "C" TwoWordReturn artInvokeInterfaceTrampolineWithAccessCheck(
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<kInterface, 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 artInvokeDirectTrampolineWithAccessCheck(
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<kDirect, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002062}
2063
Andreas Gampec200a4a2014-06-16 18:39:09 -07002064extern "C" TwoWordReturn artInvokeStaticTrampolineWithAccessCheck(
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002065 uint32_t method_idx,
2066 mirror::Object* this_object,
2067 Thread* self,
Andreas Gampec200a4a2014-06-16 18:39:09 -07002068 StackReference<mirror::ArtMethod>* sp)
2069 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002070 return artInvokeCommon<kStatic, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002071}
2072
Andreas Gampec200a4a2014-06-16 18:39:09 -07002073extern "C" TwoWordReturn artInvokeSuperTrampolineWithAccessCheck(
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002074 uint32_t method_idx,
2075 mirror::Object* this_object,
2076 Thread* self,
Andreas Gampec200a4a2014-06-16 18:39:09 -07002077 StackReference<mirror::ArtMethod>* sp)
2078 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002079 return artInvokeCommon<kSuper, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002080}
2081
Andreas Gampec200a4a2014-06-16 18:39:09 -07002082extern "C" TwoWordReturn artInvokeVirtualTrampolineWithAccessCheck(
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002083 uint32_t method_idx,
2084 mirror::Object* this_object,
2085 Thread* self,
Andreas Gampec200a4a2014-06-16 18:39:09 -07002086 StackReference<mirror::ArtMethod>* sp)
2087 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002088 return artInvokeCommon<kVirtual, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002089}
2090
2091// Determine target of interface dispatch. This object is known non-null.
Nicolas Geoffray8ea18d02015-05-26 16:29:08 +01002092extern "C" TwoWordReturn artInvokeInterfaceTrampoline(uint32_t dex_method_idx,
Andreas Gampe51f76352014-05-21 08:28:48 -07002093 mirror::Object* this_object,
Andreas Gampecf4035a2014-05-28 22:43:01 -07002094 Thread* self,
2095 StackReference<mirror::ArtMethod>* sp)
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002096 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002097 ScopedQuickEntrypointChecks sqec(self);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002098 // The optimizing compiler currently does not inline methods that have an interface
2099 // invocation. We use the outer method directly to avoid fetching a stack map, which is
2100 // more expensive.
2101 mirror::ArtMethod* caller_method = QuickArgumentVisitor::GetOuterMethod(sp);
2102 DCHECK_EQ(caller_method, QuickArgumentVisitor::GetCallingMethod(sp));
Nicolas Geoffray8ea18d02015-05-26 16:29:08 +01002103 mirror::ArtMethod* interface_method = caller_method->GetDexCacheResolvedMethod(dex_method_idx);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002104 mirror::ArtMethod* method;
2105 if (LIKELY(interface_method->GetDexMethodIndex() != DexFile::kDexNoIndex)) {
2106 method = this_object->GetClass()->FindVirtualMethodForInterface(interface_method);
Ian Rogerse0a02da2014-12-02 14:10:53 -08002107 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002108 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(interface_method, this_object,
2109 caller_method);
Andreas Gamped58342c2014-06-05 14:18:08 -07002110 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002111 }
2112 } else {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002113 DCHECK(interface_method == Runtime::Current()->GetResolutionMethod());
Nicolas Geoffray8ea18d02015-05-26 16:29:08 +01002114 if (kIsDebugBuild) {
2115 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
2116 const DexFile::CodeItem* code = caller_method->GetCodeItem();
2117 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
2118 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
2119 Instruction::Code instr_code = instr->Opcode();
2120 CHECK(instr_code == Instruction::INVOKE_INTERFACE ||
2121 instr_code == Instruction::INVOKE_INTERFACE_RANGE)
2122 << "Unexpected call into interface trampoline: " << instr->DumpString(nullptr);
2123 if (instr_code == Instruction::INVOKE_INTERFACE) {
2124 CHECK_EQ(dex_method_idx, instr->VRegB_35c());
2125 } else {
2126 CHECK_EQ(instr_code, Instruction::INVOKE_INTERFACE_RANGE);
2127 CHECK_EQ(dex_method_idx, instr->VRegB_3rc());
2128 }
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002129 }
2130
Andreas Gampec200a4a2014-06-16 18:39:09 -07002131 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()
2132 ->GetDexFile();
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002133 uint32_t shorty_len;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002134 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(dex_method_idx),
2135 &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002136 {
2137 // Remember the args in case a GC happens in FindMethodFromCode.
2138 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
2139 RememberForGcArgumentVisitor visitor(sp, false, shorty, shorty_len, &soa);
2140 visitor.VisitArguments();
Mathieu Chartier0cd81352014-05-22 16:48:55 -07002141 method = FindMethodFromCode<kInterface, false>(dex_method_idx, &this_object, &caller_method,
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002142 self);
2143 visitor.FixupReferences();
2144 }
2145
2146 if (UNLIKELY(method == nullptr)) {
2147 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07002148 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002149 }
2150 }
2151 const void* code = method->GetEntryPointFromQuickCompiledCode();
2152
2153 // When we return, the caller will branch to this address, so it had better not be 0!
Ian Rogerse0a02da2014-12-02 14:10:53 -08002154 DCHECK(code != nullptr) << "Code was null in method: " << PrettyMethod(method)
Andreas Gampec200a4a2014-06-16 18:39:09 -07002155 << " location: " << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07002156
Andreas Gamped58342c2014-06-05 14:18:08 -07002157 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
2158 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002159}
2160
Ian Rogers848871b2013-08-05 10:56:33 -07002161} // namespace art