blob: c2488cc5dda07452b4f984a477c68bff537a6249 [file] [log] [blame]
Ian Rogers848871b2013-08-05 10:56:33 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Mathieu Chartiere401d142015-04-22 13:56:20 -070017#include "art_method-inl.h"
Ian Rogers848871b2013-08-05 10:56:33 -070018#include "callee_save_frame.h"
Dragos Sbirleabd136a22013-08-13 18:07:04 -070019#include "common_throws.h"
Ian Rogers848871b2013-08-05 10:56:33 -070020#include "dex_file-inl.h"
21#include "dex_instruction-inl.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070022#include "entrypoints/entrypoint_utils-inl.h"
Ian Rogers6f3dbba2014-10-14 17:41:57 -070023#include "entrypoints/runtime_asm_entrypoints.h"
Ian Rogers83883d72013-10-21 21:07:24 -070024#include "gc/accounting/card_table-inl.h"
Ian Rogers848871b2013-08-05 10:56:33 -070025#include "interpreter/interpreter.h"
Ian Rogerse0a02da2014-12-02 14:10:53 -080026#include "method_reference.h"
Ian Rogers848871b2013-08-05 10:56:33 -070027#include "mirror/class-inl.h"
Mathieu Chartier5f3ded42014-04-03 15:25:30 -070028#include "mirror/dex_cache-inl.h"
Mathieu Chartierfc58af42015-04-16 18:00:39 -070029#include "mirror/method.h"
Ian Rogers848871b2013-08-05 10:56:33 -070030#include "mirror/object-inl.h"
31#include "mirror/object_array-inl.h"
Andreas Gampe639bdd12015-06-03 11:22:45 -070032#include "quick_exception_handler.h"
Ian Rogers848871b2013-08-05 10:56:33 -070033#include "runtime.h"
Ian Rogers53b8b092014-03-13 23:45:53 -070034#include "scoped_thread_state_change.h"
Daniel Mihalyieb076692014-08-22 17:33:31 +020035#include "debugger.h"
Ian Rogers848871b2013-08-05 10:56:33 -070036
37namespace art {
38
39// Visits the arguments as saved to the stack by a Runtime::kRefAndArgs callee save frame.
40class QuickArgumentVisitor {
Ian Rogers936b37f2014-02-14 00:52:24 -080041 // Number of bytes for each out register in the caller method's frame.
42 static constexpr size_t kBytesStackArgLocation = 4;
Alexei Zavjalov41c507a2014-05-15 16:02:46 +070043 // Frame size in bytes of a callee-save frame for RefsAndArgs.
44 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize =
45 GetCalleeSaveFrameSize(kRuntimeISA, Runtime::kRefsAndArgs);
Ian Rogers848871b2013-08-05 10:56:33 -070046#if defined(__arm__)
47 // The callee save frame is pointed to by SP.
48 // | argN | |
49 // | ... | |
50 // | arg4 | |
51 // | arg3 spill | | Caller's frame
52 // | arg2 spill | |
53 // | arg1 spill | |
54 // | Method* | ---
55 // | LR |
Zheng Xu5667fdb2014-10-23 18:29:55 +080056 // | ... | 4x6 bytes callee saves
57 // | R3 |
58 // | R2 |
59 // | R1 |
60 // | S15 |
61 // | : |
62 // | S0 |
63 // | | 4x2 bytes padding
Ian Rogers848871b2013-08-05 10:56:33 -070064 // | Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -050065 static constexpr bool kSplitPairAcrossRegisterAndStack = kArm32QuickCodeUseSoftFloat;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +000066 static constexpr bool kAlignPairRegister = !kArm32QuickCodeUseSoftFloat;
Zheng Xu5667fdb2014-10-23 18:29:55 +080067 static constexpr bool kQuickSoftFloatAbi = kArm32QuickCodeUseSoftFloat;
68 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = !kArm32QuickCodeUseSoftFloat;
Goran Jakovljevicff734982015-08-24 12:58:55 +000069 static constexpr bool kQuickSkipOddFpRegisters = false;
Zheng Xu5667fdb2014-10-23 18:29:55 +080070 static constexpr size_t kNumQuickGprArgs = 3;
71 static constexpr size_t kNumQuickFprArgs = kArm32QuickCodeUseSoftFloat ? 0 : 16;
Andreas Gampe1a5c4062015-01-15 12:10:47 -080072 static constexpr bool kGprFprLockstep = false;
Zheng Xub551fdc2014-07-25 11:49:42 +080073 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset =
74 arm::ArmCalleeSaveFpr1Offset(Runtime::kRefsAndArgs); // Offset of first FPR arg.
75 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset =
76 arm::ArmCalleeSaveGpr1Offset(Runtime::kRefsAndArgs); // Offset of first GPR arg.
77 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset =
78 arm::ArmCalleeSaveLrOffset(Runtime::kRefsAndArgs); // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -080079 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +000080 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -080081 }
Stuart Monteithb95a5342014-03-12 13:32:32 +000082#elif defined(__aarch64__)
83 // The callee save frame is pointed to by SP.
84 // | argN | |
85 // | ... | |
86 // | arg4 | |
87 // | arg3 spill | | Caller's frame
88 // | arg2 spill | |
89 // | arg1 spill | |
90 // | Method* | ---
91 // | LR |
Zheng Xub551fdc2014-07-25 11:49:42 +080092 // | X29 |
Stuart Monteithb95a5342014-03-12 13:32:32 +000093 // | : |
Serban Constantinescu9bd88b02015-04-22 16:24:46 +010094 // | X20 |
Stuart Monteithb95a5342014-03-12 13:32:32 +000095 // | X7 |
96 // | : |
97 // | X1 |
Zheng Xub551fdc2014-07-25 11:49:42 +080098 // | D7 |
Stuart Monteithb95a5342014-03-12 13:32:32 +000099 // | : |
100 // | D0 |
101 // | | padding
102 // | Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500103 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000104 static constexpr bool kAlignPairRegister = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000105 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800106 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000107 static constexpr bool kQuickSkipOddFpRegisters = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000108 static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs.
109 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800110 static constexpr bool kGprFprLockstep = false;
Zheng Xub551fdc2014-07-25 11:49:42 +0800111 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset =
112 arm64::Arm64CalleeSaveFpr1Offset(Runtime::kRefsAndArgs); // Offset of first FPR arg.
113 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset =
114 arm64::Arm64CalleeSaveGpr1Offset(Runtime::kRefsAndArgs); // Offset of first GPR arg.
115 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset =
116 arm64::Arm64CalleeSaveLrOffset(Runtime::kRefsAndArgs); // Offset of return address.
Stuart Monteithb95a5342014-03-12 13:32:32 +0000117 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000118 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Stuart Monteithb95a5342014-03-12 13:32:32 +0000119 }
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800120#elif defined(__mips__) && !defined(__LP64__)
Ian Rogers848871b2013-08-05 10:56:33 -0700121 // The callee save frame is pointed to by SP.
122 // | argN | |
123 // | ... | |
124 // | arg4 | |
125 // | arg3 spill | | Caller's frame
126 // | arg2 spill | |
127 // | arg1 spill | |
128 // | Method* | ---
129 // | RA |
130 // | ... | callee saves
131 // | A3 | arg3
132 // | A2 | arg2
133 // | A1 | arg1
Goran Jakovljevicff734982015-08-24 12:58:55 +0000134 // | F15 |
135 // | F14 | f_arg1
136 // | F13 |
137 // | F12 | f_arg0
138 // | | padding
Ian Rogers848871b2013-08-05 10:56:33 -0700139 // | A0/Method* | <- sp
Goran Jakovljevicff734982015-08-24 12:58:55 +0000140 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
141 static constexpr bool kAlignPairRegister = true;
142 static constexpr bool kQuickSoftFloatAbi = false;
Zheng Xu5667fdb2014-10-23 18:29:55 +0800143 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000144 static constexpr bool kQuickSkipOddFpRegisters = true;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800145 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs.
Goran Jakovljevicff734982015-08-24 12:58:55 +0000146 static constexpr size_t kNumQuickFprArgs = 4; // 2 arguments passed in FPRs. Floats can be passed
147 // only in even numbered registers and each double
148 // occupies two registers.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800149 static constexpr bool kGprFprLockstep = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000150 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 16; // Offset of first FPR arg.
151 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 32; // Offset of first GPR arg.
152 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 76; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800153 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000154 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800155 }
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800156#elif defined(__mips__) && defined(__LP64__)
157 // The callee save frame is pointed to by SP.
158 // | argN | |
159 // | ... | |
160 // | arg4 | |
161 // | arg3 spill | | Caller's frame
162 // | arg2 spill | |
163 // | arg1 spill | |
164 // | Method* | ---
165 // | RA |
166 // | ... | callee saves
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800167 // | A7 | arg7
168 // | A6 | arg6
169 // | A5 | arg5
170 // | A4 | arg4
171 // | A3 | arg3
172 // | A2 | arg2
173 // | A1 | arg1
Goran Jakovljevicff734982015-08-24 12:58:55 +0000174 // | F19 | f_arg7
175 // | F18 | f_arg6
176 // | F17 | f_arg5
177 // | F16 | f_arg4
178 // | F15 | f_arg3
179 // | F14 | f_arg2
180 // | F13 | f_arg1
181 // | F12 | f_arg0
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800182 // | | padding
183 // | A0/Method* | <- sp
184 // NOTE: for Mip64, when A0 is skipped, F0 is also skipped.
Douglas Leungd18e0832015-02-09 15:22:26 -0800185 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800186 static constexpr bool kAlignPairRegister = false;
187 static constexpr bool kQuickSoftFloatAbi = false;
188 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000189 static constexpr bool kQuickSkipOddFpRegisters = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800190 static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs.
191 static constexpr size_t kNumQuickFprArgs = 7; // 7 arguments passed in FPRs.
192 static constexpr bool kGprFprLockstep = true;
193
194 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 24; // Offset of first FPR arg (F1).
195 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 80; // Offset of first GPR arg (A1).
196 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 200; // Offset of return address.
197 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
198 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
199 }
Ian Rogers848871b2013-08-05 10:56:33 -0700200#elif defined(__i386__)
201 // The callee save frame is pointed to by SP.
202 // | argN | |
203 // | ... | |
204 // | arg4 | |
205 // | arg3 spill | | Caller's frame
206 // | arg2 spill | |
207 // | arg1 spill | |
208 // | Method* | ---
209 // | Return |
210 // | EBP,ESI,EDI | callee saves
211 // | EBX | arg3
212 // | EDX | arg2
213 // | ECX | arg1
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000214 // | XMM3 | float arg 4
215 // | XMM2 | float arg 3
216 // | XMM1 | float arg 2
217 // | XMM0 | float arg 1
Ian Rogers848871b2013-08-05 10:56:33 -0700218 // | EAX/Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500219 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000220 static constexpr bool kAlignPairRegister = false;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000221 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800222 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000223 static constexpr bool kQuickSkipOddFpRegisters = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800224 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs.
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000225 static constexpr size_t kNumQuickFprArgs = 4; // 4 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800226 static constexpr bool kGprFprLockstep = false;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000227 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 4; // Offset of first FPR arg.
228 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 4 + 4*8; // Offset of first GPR arg.
229 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 28 + 4*8; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800230 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000231 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800232 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800233#elif defined(__x86_64__)
Ian Rogers936b37f2014-02-14 00:52:24 -0800234 // The callee save frame is pointed to by SP.
235 // | argN | |
236 // | ... | |
237 // | reg. arg spills | | Caller's frame
238 // | Method* | ---
239 // | Return |
240 // | R15 | callee save
241 // | R14 | callee save
242 // | R13 | callee save
243 // | R12 | callee save
244 // | R9 | arg5
245 // | R8 | arg4
246 // | RSI/R6 | arg1
247 // | RBP/R5 | callee save
248 // | RBX/R3 | callee save
249 // | RDX/R2 | arg2
250 // | RCX/R1 | arg3
251 // | XMM7 | float arg 8
252 // | XMM6 | float arg 7
253 // | XMM5 | float arg 6
254 // | XMM4 | float arg 5
255 // | XMM3 | float arg 4
256 // | XMM2 | float arg 3
257 // | XMM1 | float arg 2
258 // | XMM0 | float arg 1
259 // | Padding |
260 // | RDI/Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500261 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000262 static constexpr bool kAlignPairRegister = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800263 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800264 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000265 static constexpr bool kQuickSkipOddFpRegisters = false;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700266 static constexpr size_t kNumQuickGprArgs = 5; // 5 arguments passed in GPRs.
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700267 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800268 static constexpr bool kGprFprLockstep = false;
Ian Rogers936b37f2014-02-14 00:52:24 -0800269 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 16; // Offset of first FPR arg.
Serguei Katkovc3801912014-07-08 17:21:53 +0700270 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 80 + 4*8; // Offset of first GPR arg.
271 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 168 + 4*8; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800272 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
273 switch (gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000274 case 0: return (4 * GetBytesPerGprSpillLocation(kRuntimeISA));
275 case 1: return (1 * GetBytesPerGprSpillLocation(kRuntimeISA));
276 case 2: return (0 * GetBytesPerGprSpillLocation(kRuntimeISA));
277 case 3: return (5 * GetBytesPerGprSpillLocation(kRuntimeISA));
278 case 4: return (6 * GetBytesPerGprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800279 default:
Andreas Gampec200a4a2014-06-16 18:39:09 -0700280 LOG(FATAL) << "Unexpected GPR index: " << gpr_index;
281 return 0;
Ian Rogers936b37f2014-02-14 00:52:24 -0800282 }
283 }
Ian Rogers848871b2013-08-05 10:56:33 -0700284#else
285#error "Unsupported architecture"
Ian Rogers848871b2013-08-05 10:56:33 -0700286#endif
287
Ian Rogers936b37f2014-02-14 00:52:24 -0800288 public:
Sebastien Hertza836bc92014-11-25 16:30:53 +0100289 // Special handling for proxy methods. Proxy methods are instance methods so the
290 // 'this' object is the 1st argument. They also have the same frame layout as the
291 // kRefAndArgs runtime method. Since 'this' is a reference, it is located in the
292 // 1st GPR.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700293 static mirror::Object* GetProxyThisObject(ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -0700294 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700295 CHECK((*sp)->IsProxyMethod());
296 CHECK_EQ(kQuickCalleeSaveFrame_RefAndArgs_FrameSize, (*sp)->GetFrameSizeInBytes());
Sebastien Hertza836bc92014-11-25 16:30:53 +0100297 CHECK_GT(kNumQuickGprArgs, 0u);
298 constexpr uint32_t kThisGprIndex = 0u; // 'this' is in the 1st GPR.
299 size_t this_arg_offset = kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset +
300 GprIndexToGprOffset(kThisGprIndex);
301 uint8_t* this_arg_address = reinterpret_cast<uint8_t*>(sp) + this_arg_offset;
302 return reinterpret_cast<StackReference<mirror::Object>*>(this_arg_address)->AsMirrorPtr();
303 }
304
Mathieu Chartier90443472015-07-16 20:32:27 -0700305 static ArtMethod* GetCallingMethod(ArtMethod** sp) SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700306 DCHECK((*sp)->IsCalleeSaveMethod());
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +0100307 return GetCalleeSaveMethodCaller(sp, Runtime::kRefsAndArgs);
308 }
309
Mathieu Chartier90443472015-07-16 20:32:27 -0700310 static ArtMethod* GetOuterMethod(ArtMethod** sp) SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700311 DCHECK((*sp)->IsCalleeSaveMethod());
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100312 uint8_t* previous_sp =
313 reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700314 return *reinterpret_cast<ArtMethod**>(previous_sp);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100315 }
316
Mathieu Chartier90443472015-07-16 20:32:27 -0700317 static uint32_t GetCallingDexPc(ArtMethod** sp) SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700318 DCHECK((*sp)->IsCalleeSaveMethod());
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100319 const size_t callee_frame_size = GetCalleeSaveFrameSize(kRuntimeISA, Runtime::kRefsAndArgs);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700320 ArtMethod** caller_sp = reinterpret_cast<ArtMethod**>(
321 reinterpret_cast<uintptr_t>(sp) + callee_frame_size);
322 ArtMethod* outer_method = *caller_sp;
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100323 uintptr_t outer_pc = QuickArgumentVisitor::GetCallingPc(sp);
324 uintptr_t outer_pc_offset = outer_method->NativeQuickPcOffset(outer_pc);
325
326 if (outer_method->IsOptimized(sizeof(void*))) {
327 CodeInfo code_info = outer_method->GetOptimizedCodeInfo();
David Brazdilf677ebf2015-05-29 16:29:43 +0100328 StackMapEncoding encoding = code_info.ExtractEncoding();
329 StackMap stack_map = code_info.GetStackMapForNativePcOffset(outer_pc_offset, encoding);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100330 DCHECK(stack_map.IsValid());
David Brazdilf677ebf2015-05-29 16:29:43 +0100331 if (stack_map.HasInlineInfo(encoding)) {
332 InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map, encoding);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100333 return inline_info.GetDexPcAtDepth(inline_info.GetDepth() - 1);
334 } else {
David Brazdilf677ebf2015-05-29 16:29:43 +0100335 return stack_map.GetDexPc(encoding);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100336 }
337 } else {
338 return outer_method->ToDexPc(outer_pc);
339 }
Ian Rogers848871b2013-08-05 10:56:33 -0700340 }
341
Ian Rogers936b37f2014-02-14 00:52:24 -0800342 // For the given quick ref and args quick frame, return the caller's PC.
Mathieu Chartier90443472015-07-16 20:32:27 -0700343 static uintptr_t GetCallingPc(ArtMethod** sp) SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700344 DCHECK((*sp)->IsCalleeSaveMethod());
Ian Rogers13735952014-10-08 12:43:28 -0700345 uint8_t* lr = reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_LrOffset;
Ian Rogers848871b2013-08-05 10:56:33 -0700346 return *reinterpret_cast<uintptr_t*>(lr);
347 }
348
Mathieu Chartiere401d142015-04-22 13:56:20 -0700349 QuickArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty,
Mathieu Chartier90443472015-07-16 20:32:27 -0700350 uint32_t shorty_len) SHARED_REQUIRES(Locks::mutator_lock_) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700351 is_static_(is_static), shorty_(shorty), shorty_len_(shorty_len),
Ian Rogers13735952014-10-08 12:43:28 -0700352 gpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset),
353 fpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset),
354 stack_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize
Mathieu Chartiere401d142015-04-22 13:56:20 -0700355 + sizeof(ArtMethod*)), // Skip ArtMethod*.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800356 gpr_index_(0), fpr_index_(0), fpr_double_index_(0), stack_index_(0),
357 cur_type_(Primitive::kPrimVoid), is_split_long_or_double_(false) {
Andreas Gampe575e78c2014-11-03 23:41:03 -0800358 static_assert(kQuickSoftFloatAbi == (kNumQuickFprArgs == 0),
359 "Number of Quick FPR arguments unexpected");
360 static_assert(!(kQuickSoftFloatAbi && kQuickDoubleRegAlignedFloatBackFilled),
361 "Double alignment unexpected");
Zheng Xu5667fdb2014-10-23 18:29:55 +0800362 // For register alignment, we want to assume that counters(fpr_double_index_) are even if the
363 // next register is even.
Andreas Gampe575e78c2014-11-03 23:41:03 -0800364 static_assert(!kQuickDoubleRegAlignedFloatBackFilled || kNumQuickFprArgs % 2 == 0,
365 "Number of Quick FPR arguments not even");
Mathieu Chartiere401d142015-04-22 13:56:20 -0700366 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), sizeof(void*));
Zheng Xu5667fdb2014-10-23 18:29:55 +0800367 }
Ian Rogers848871b2013-08-05 10:56:33 -0700368
369 virtual ~QuickArgumentVisitor() {}
370
371 virtual void Visit() = 0;
372
Ian Rogers936b37f2014-02-14 00:52:24 -0800373 Primitive::Type GetParamPrimitiveType() const {
374 return cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700375 }
376
Ian Rogers13735952014-10-08 12:43:28 -0700377 uint8_t* GetParamAddress() const {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800378 if (!kQuickSoftFloatAbi) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800379 Primitive::Type type = GetParamPrimitiveType();
380 if (UNLIKELY((type == Primitive::kPrimDouble) || (type == Primitive::kPrimFloat))) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800381 if (type == Primitive::kPrimDouble && kQuickDoubleRegAlignedFloatBackFilled) {
382 if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) {
383 return fpr_args_ + (fpr_double_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
384 }
385 } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000386 return fpr_args_ + (fpr_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800387 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700388 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers936b37f2014-02-14 00:52:24 -0800389 }
390 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800391 if (gpr_index_ < kNumQuickGprArgs) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800392 return gpr_args_ + GprIndexToGprOffset(gpr_index_);
393 }
394 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700395 }
396
397 bool IsSplitLongOrDouble() const {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700398 if ((GetBytesPerGprSpillLocation(kRuntimeISA) == 4) ||
399 (GetBytesPerFprSpillLocation(kRuntimeISA) == 4)) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800400 return is_split_long_or_double_;
401 } else {
402 return false; // An optimization for when GPR and FPRs are 64bit.
403 }
Ian Rogers848871b2013-08-05 10:56:33 -0700404 }
405
Ian Rogers936b37f2014-02-14 00:52:24 -0800406 bool IsParamAReference() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700407 return GetParamPrimitiveType() == Primitive::kPrimNot;
408 }
409
Ian Rogers936b37f2014-02-14 00:52:24 -0800410 bool IsParamALongOrDouble() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700411 Primitive::Type type = GetParamPrimitiveType();
412 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
413 }
414
415 uint64_t ReadSplitLongParam() const {
Nicolas Geoffray425f2392015-01-08 14:52:29 +0000416 // The splitted long is always available through the stack.
417 return *reinterpret_cast<uint64_t*>(stack_args_
418 + stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700419 }
420
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800421 void IncGprIndex() {
422 gpr_index_++;
423 if (kGprFprLockstep) {
424 fpr_index_++;
425 }
426 }
427
428 void IncFprIndex() {
429 fpr_index_++;
430 if (kGprFprLockstep) {
431 gpr_index_++;
432 }
433 }
434
Mathieu Chartier90443472015-07-16 20:32:27 -0700435 void VisitArguments() SHARED_REQUIRES(Locks::mutator_lock_) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800436 // (a) 'stack_args_' should point to the first method's argument
437 // (b) whatever the argument type it is, the 'stack_index_' should
438 // be moved forward along with every visiting.
Ian Rogers936b37f2014-02-14 00:52:24 -0800439 gpr_index_ = 0;
440 fpr_index_ = 0;
Zheng Xu5667fdb2014-10-23 18:29:55 +0800441 if (kQuickDoubleRegAlignedFloatBackFilled) {
442 fpr_double_index_ = 0;
443 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800444 stack_index_ = 0;
445 if (!is_static_) { // Handle this.
446 cur_type_ = Primitive::kPrimNot;
447 is_split_long_or_double_ = false;
Ian Rogers848871b2013-08-05 10:56:33 -0700448 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800449 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800450 if (kNumQuickGprArgs > 0) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800451 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800452 }
Ian Rogers848871b2013-08-05 10:56:33 -0700453 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800454 for (uint32_t shorty_index = 1; shorty_index < shorty_len_; ++shorty_index) {
455 cur_type_ = Primitive::GetType(shorty_[shorty_index]);
456 switch (cur_type_) {
457 case Primitive::kPrimNot:
458 case Primitive::kPrimBoolean:
459 case Primitive::kPrimByte:
460 case Primitive::kPrimChar:
461 case Primitive::kPrimShort:
462 case Primitive::kPrimInt:
463 is_split_long_or_double_ = false;
464 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800465 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800466 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800467 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800468 }
469 break;
470 case Primitive::kPrimFloat:
471 is_split_long_or_double_ = false;
472 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800473 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800474 if (kQuickSoftFloatAbi) {
475 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800476 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800477 }
478 } else {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800479 if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800480 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800481 if (kQuickDoubleRegAlignedFloatBackFilled) {
482 // Double should not overlap with float.
483 // For example, if fpr_index_ = 3, fpr_double_index_ should be at least 4.
484 fpr_double_index_ = std::max(fpr_double_index_, RoundUp(fpr_index_, 2));
485 // Float should not overlap with double.
486 if (fpr_index_ % 2 == 0) {
487 fpr_index_ = std::max(fpr_double_index_, fpr_index_);
488 }
Goran Jakovljevicff734982015-08-24 12:58:55 +0000489 } else if (kQuickSkipOddFpRegisters) {
490 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800491 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800492 }
493 }
494 break;
495 case Primitive::kPrimDouble:
496 case Primitive::kPrimLong:
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800497 if (kQuickSoftFloatAbi || (cur_type_ == Primitive::kPrimLong)) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000498 if (cur_type_ == Primitive::kPrimLong && kAlignPairRegister && gpr_index_ == 0) {
Goran Jakovljevicff734982015-08-24 12:58:55 +0000499 // Currently, this is only for ARM and MIPS, where the first available parameter
500 // register is R1 (on ARM) or A1 (on MIPS). So we skip it, and use R2 (on ARM) or
501 // A2 (on MIPS) instead.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800502 IncGprIndex();
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000503 }
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000504 is_split_long_or_double_ = (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) &&
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800505 ((gpr_index_ + 1) == kNumQuickGprArgs);
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500506 if (!kSplitPairAcrossRegisterAndStack && is_split_long_or_double_) {
507 // We don't want to split this. Pass over this register.
508 gpr_index_++;
509 is_split_long_or_double_ = false;
510 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800511 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800512 if (kBytesStackArgLocation == 4) {
513 stack_index_+= 2;
514 } else {
515 CHECK_EQ(kBytesStackArgLocation, 8U);
516 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800517 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700518 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800519 IncGprIndex();
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000520 if (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700521 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800522 IncGprIndex();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700523 }
524 }
525 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800526 } else {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000527 is_split_long_or_double_ = (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) &&
Zheng Xu5667fdb2014-10-23 18:29:55 +0800528 ((fpr_index_ + 1) == kNumQuickFprArgs) && !kQuickDoubleRegAlignedFloatBackFilled;
Ian Rogers936b37f2014-02-14 00:52:24 -0800529 Visit();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700530 if (kBytesStackArgLocation == 4) {
531 stack_index_+= 2;
Ian Rogers936b37f2014-02-14 00:52:24 -0800532 } else {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700533 CHECK_EQ(kBytesStackArgLocation, 8U);
534 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800535 }
Zheng Xu5667fdb2014-10-23 18:29:55 +0800536 if (kQuickDoubleRegAlignedFloatBackFilled) {
537 if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) {
538 fpr_double_index_ += 2;
539 // Float should not overlap with double.
540 if (fpr_index_ % 2 == 0) {
541 fpr_index_ = std::max(fpr_double_index_, fpr_index_);
542 }
543 }
544 } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800545 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800546 if (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) {
547 if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800548 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800549 }
550 }
551 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800552 }
553 break;
554 default:
555 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty_;
556 }
Ian Rogers848871b2013-08-05 10:56:33 -0700557 }
558 }
559
Andreas Gampec200a4a2014-06-16 18:39:09 -0700560 protected:
Ian Rogers848871b2013-08-05 10:56:33 -0700561 const bool is_static_;
562 const char* const shorty_;
563 const uint32_t shorty_len_;
Andreas Gampec200a4a2014-06-16 18:39:09 -0700564
565 private:
Ian Rogers13735952014-10-08 12:43:28 -0700566 uint8_t* const gpr_args_; // Address of GPR arguments in callee save frame.
567 uint8_t* const fpr_args_; // Address of FPR arguments in callee save frame.
568 uint8_t* const stack_args_; // Address of stack arguments in caller's frame.
Ian Rogers936b37f2014-02-14 00:52:24 -0800569 uint32_t gpr_index_; // Index into spilled GPRs.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800570 // Index into spilled FPRs.
571 // In case kQuickDoubleRegAlignedFloatBackFilled, it may index a hole while fpr_double_index_
572 // holds a higher register number.
573 uint32_t fpr_index_;
574 // Index into spilled FPRs for aligned double.
575 // Only used when kQuickDoubleRegAlignedFloatBackFilled. Next available double register indexed in
576 // terms of singles, may be behind fpr_index.
577 uint32_t fpr_double_index_;
Ian Rogers936b37f2014-02-14 00:52:24 -0800578 uint32_t stack_index_; // Index into arguments on the stack.
579 // The current type of argument during VisitArguments.
580 Primitive::Type cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700581 // Does a 64bit parameter straddle the register and stack arguments?
582 bool is_split_long_or_double_;
583};
584
Sebastien Hertza836bc92014-11-25 16:30:53 +0100585// Returns the 'this' object of a proxy method. This function is only used by StackVisitor. It
586// allows to use the QuickArgumentVisitor constants without moving all the code in its own module.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700587extern "C" mirror::Object* artQuickGetProxyThisObject(ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -0700588 SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertza836bc92014-11-25 16:30:53 +0100589 return QuickArgumentVisitor::GetProxyThisObject(sp);
590}
591
Ian Rogers848871b2013-08-05 10:56:33 -0700592// Visits arguments on the stack placing them into the shadow frame.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800593class BuildQuickShadowFrameVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700594 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700595 BuildQuickShadowFrameVisitor(ArtMethod** sp, bool is_static, const char* shorty,
596 uint32_t shorty_len, ShadowFrame* sf, size_t first_arg_reg) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700597 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), sf_(sf), cur_reg_(first_arg_reg) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700598
Mathieu Chartier90443472015-07-16 20:32:27 -0700599 void Visit() SHARED_REQUIRES(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700600
601 private:
Ian Rogers936b37f2014-02-14 00:52:24 -0800602 ShadowFrame* const sf_;
603 uint32_t cur_reg_;
Ian Rogers848871b2013-08-05 10:56:33 -0700604
Dragos Sbirleabd136a22013-08-13 18:07:04 -0700605 DISALLOW_COPY_AND_ASSIGN(BuildQuickShadowFrameVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700606};
607
Andreas Gampec200a4a2014-06-16 18:39:09 -0700608void BuildQuickShadowFrameVisitor::Visit() {
Ian Rogers9758f792014-03-13 09:02:55 -0700609 Primitive::Type type = GetParamPrimitiveType();
610 switch (type) {
611 case Primitive::kPrimLong: // Fall-through.
612 case Primitive::kPrimDouble:
613 if (IsSplitLongOrDouble()) {
614 sf_->SetVRegLong(cur_reg_, ReadSplitLongParam());
615 } else {
616 sf_->SetVRegLong(cur_reg_, *reinterpret_cast<jlong*>(GetParamAddress()));
617 }
618 ++cur_reg_;
619 break;
620 case Primitive::kPrimNot: {
621 StackReference<mirror::Object>* stack_ref =
622 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
623 sf_->SetVRegReference(cur_reg_, stack_ref->AsMirrorPtr());
624 }
625 break;
626 case Primitive::kPrimBoolean: // Fall-through.
627 case Primitive::kPrimByte: // Fall-through.
628 case Primitive::kPrimChar: // Fall-through.
629 case Primitive::kPrimShort: // Fall-through.
630 case Primitive::kPrimInt: // Fall-through.
631 case Primitive::kPrimFloat:
632 sf_->SetVReg(cur_reg_, *reinterpret_cast<jint*>(GetParamAddress()));
633 break;
634 case Primitive::kPrimVoid:
635 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700636 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -0700637 }
638 ++cur_reg_;
639}
640
Mathieu Chartiere401d142015-04-22 13:56:20 -0700641extern "C" uint64_t artQuickToInterpreterBridge(ArtMethod* method, Thread* self, ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -0700642 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers848871b2013-08-05 10:56:33 -0700643 // Ensure we don't get thread suspension until the object arguments are safely in the shadow
644 // frame.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700645 ScopedQuickEntrypointChecks sqec(self);
Ian Rogers848871b2013-08-05 10:56:33 -0700646
647 if (method->IsAbstract()) {
648 ThrowAbstractMethodError(method);
649 return 0;
Andreas Gampe639bdd12015-06-03 11:22:45 -0700650 }
651
652 JValue tmp_value;
653 ShadowFrame* deopt_frame = self->PopStackedShadowFrame(
654 StackedShadowFrameType::kSingleFrameDeoptimizationShadowFrame, false);
655 const DexFile::CodeItem* code_item = method->GetCodeItem();
656 DCHECK(code_item != nullptr) << PrettyMethod(method);
657 ManagedStack fragment;
658
659 DCHECK(!method->IsNative()) << PrettyMethod(method);
660 uint32_t shorty_len = 0;
661 auto* non_proxy_method = method->GetInterfaceMethodIfProxy(sizeof(void*));
662 const char* shorty = non_proxy_method->GetShorty(&shorty_len);
663
664 JValue result;
665
666 if (deopt_frame != nullptr) {
667 // Coming from single-frame deopt.
668
669 if (kIsDebugBuild) {
670 // Sanity-check: are the methods as expected? We check that the last shadow frame (the bottom
671 // of the call-stack) corresponds to the called method.
672 ShadowFrame* linked = deopt_frame;
673 while (linked->GetLink() != nullptr) {
674 linked = linked->GetLink();
675 }
676 CHECK_EQ(method, linked->GetMethod()) << PrettyMethod(method) << " "
677 << PrettyMethod(linked->GetMethod());
678 }
679
680 if (VLOG_IS_ON(deopt)) {
681 // Print out the stack to verify that it was a single-frame deopt.
682 LOG(INFO) << "Continue-ing from deopt. Stack is:";
683 QuickExceptionHandler::DumpFramesWithType(self, true);
684 }
685
686 mirror::Throwable* pending_exception = nullptr;
687 self->PopDeoptimizationContext(&result, &pending_exception);
688
689 // Push a transition back into managed code onto the linked list in thread.
690 self->PushManagedStackFragment(&fragment);
691
692 // Ensure that the stack is still in order.
693 if (kIsDebugBuild) {
694 class DummyStackVisitor : public StackVisitor {
695 public:
696 explicit DummyStackVisitor(Thread* self_in) SHARED_REQUIRES(Locks::mutator_lock_)
697 : StackVisitor(self_in, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {}
698
699 bool VisitFrame() OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
700 // Nothing to do here. In a debug build, SanityCheckFrame will do the work in the walking
701 // logic. Just always say we want to continue.
702 return true;
703 }
704 };
705 DummyStackVisitor dsv(self);
706 dsv.WalkStack();
707 }
708
709 // Restore the exception that was pending before deoptimization then interpret the
710 // deoptimized frames.
711 if (pending_exception != nullptr) {
712 self->SetException(pending_exception);
713 }
714 interpreter::EnterInterpreterFromDeoptimize(self, deopt_frame, &result);
Ian Rogers848871b2013-08-05 10:56:33 -0700715 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -0700716 const char* old_cause = self->StartAssertNoThreadSuspension(
717 "Building interpreter shadow frame");
Ian Rogers848871b2013-08-05 10:56:33 -0700718 uint16_t num_regs = code_item->registers_size_;
719 void* memory = alloca(ShadowFrame::ComputeSize(num_regs));
Andreas Gampec200a4a2014-06-16 18:39:09 -0700720 // No last shadow coming from quick.
721 ShadowFrame* shadow_frame(ShadowFrame::Create(num_regs, nullptr, method, 0, memory));
Ian Rogers848871b2013-08-05 10:56:33 -0700722 size_t first_arg_reg = code_item->registers_size_ - code_item->ins_size_;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700723 BuildQuickShadowFrameVisitor shadow_frame_builder(sp, method->IsStatic(), shorty, shorty_len,
Ian Rogers936b37f2014-02-14 00:52:24 -0800724 shadow_frame, first_arg_reg);
Ian Rogers848871b2013-08-05 10:56:33 -0700725 shadow_frame_builder.VisitArguments();
Ian Rogerse94652f2014-12-02 11:13:19 -0800726 const bool needs_initialization =
727 method->IsStatic() && !method->GetDeclaringClass()->IsInitialized();
Ian Rogers848871b2013-08-05 10:56:33 -0700728 // Push a transition back into managed code onto the linked list in thread.
Ian Rogers848871b2013-08-05 10:56:33 -0700729 self->PushManagedStackFragment(&fragment);
730 self->PushShadowFrame(shadow_frame);
731 self->EndAssertNoThreadSuspension(old_cause);
732
Ian Rogerse94652f2014-12-02 11:13:19 -0800733 if (needs_initialization) {
Ian Rogers848871b2013-08-05 10:56:33 -0700734 // Ensure static method's class is initialized.
Ian Rogerse94652f2014-12-02 11:13:19 -0800735 StackHandleScope<1> hs(self);
736 Handle<mirror::Class> h_class(hs.NewHandle(shadow_frame->GetMethod()->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700737 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
Ian Rogerse94652f2014-12-02 11:13:19 -0800738 DCHECK(Thread::Current()->IsExceptionPending()) << PrettyMethod(shadow_frame->GetMethod());
Ian Rogers848871b2013-08-05 10:56:33 -0700739 self->PopManagedStackFragment(fragment);
740 return 0;
741 }
742 }
Daniel Mihalyieb076692014-08-22 17:33:31 +0200743
Andreas Gampe639bdd12015-06-03 11:22:45 -0700744 result = interpreter::EnterInterpreterFromEntryPoint(self, code_item, shadow_frame);
Ian Rogers848871b2013-08-05 10:56:33 -0700745 }
Andreas Gampe639bdd12015-06-03 11:22:45 -0700746
747 // Pop transition.
748 self->PopManagedStackFragment(fragment);
749
750 // Request a stack deoptimization if needed
751 ArtMethod* caller = QuickArgumentVisitor::GetCallingMethod(sp);
752 if (UNLIKELY(Dbg::IsForcedInterpreterNeededForUpcall(self, caller))) {
753 // Push the context of the deoptimization stack so we can restore the return value and the
754 // exception before executing the deoptimized frames.
755 self->PushDeoptimizationContext(result, shorty[0] == 'L', self->GetException());
756
757 // Set special exception to cause deoptimization.
758 self->SetException(Thread::GetDeoptimizationException());
759 }
760
761 // No need to restore the args since the method has already been run by the interpreter.
762 return result.GetJ();
Ian Rogers848871b2013-08-05 10:56:33 -0700763}
764
765// Visits arguments on the stack placing them into the args vector, Object* arguments are converted
766// to jobjects.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800767class BuildQuickArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700768 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700769 BuildQuickArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty, uint32_t shorty_len,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700770 ScopedObjectAccessUnchecked* soa, std::vector<jvalue>* args) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700771 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa), args_(args) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700772
Mathieu Chartier90443472015-07-16 20:32:27 -0700773 void Visit() SHARED_REQUIRES(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700774
Mathieu Chartier90443472015-07-16 20:32:27 -0700775 void FixupReferences() SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800776
Ian Rogers848871b2013-08-05 10:56:33 -0700777 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700778 ScopedObjectAccessUnchecked* const soa_;
779 std::vector<jvalue>* const args_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800780 // References which we must update when exiting in case the GC moved the objects.
Ian Rogers700a4022014-05-19 16:49:03 -0700781 std::vector<std::pair<jobject, StackReference<mirror::Object>*>> references_;
Ian Rogers9758f792014-03-13 09:02:55 -0700782
Ian Rogers848871b2013-08-05 10:56:33 -0700783 DISALLOW_COPY_AND_ASSIGN(BuildQuickArgumentVisitor);
784};
785
Ian Rogers9758f792014-03-13 09:02:55 -0700786void BuildQuickArgumentVisitor::Visit() {
787 jvalue val;
788 Primitive::Type type = GetParamPrimitiveType();
789 switch (type) {
790 case Primitive::kPrimNot: {
791 StackReference<mirror::Object>* stack_ref =
792 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
793 val.l = soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
794 references_.push_back(std::make_pair(val.l, stack_ref));
795 break;
796 }
797 case Primitive::kPrimLong: // Fall-through.
798 case Primitive::kPrimDouble:
799 if (IsSplitLongOrDouble()) {
800 val.j = ReadSplitLongParam();
801 } else {
802 val.j = *reinterpret_cast<jlong*>(GetParamAddress());
803 }
804 break;
805 case Primitive::kPrimBoolean: // Fall-through.
806 case Primitive::kPrimByte: // Fall-through.
807 case Primitive::kPrimChar: // Fall-through.
808 case Primitive::kPrimShort: // Fall-through.
809 case Primitive::kPrimInt: // Fall-through.
810 case Primitive::kPrimFloat:
811 val.i = *reinterpret_cast<jint*>(GetParamAddress());
812 break;
813 case Primitive::kPrimVoid:
814 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700815 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -0700816 }
817 args_->push_back(val);
818}
819
820void BuildQuickArgumentVisitor::FixupReferences() {
821 // Fixup any references which may have changed.
822 for (const auto& pair : references_) {
823 pair.second->Assign(soa_->Decode<mirror::Object*>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -0700824 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -0700825 }
826}
827
Ian Rogers848871b2013-08-05 10:56:33 -0700828// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
829// which is responsible for recording callee save registers. We explicitly place into jobjects the
830// incoming reference arguments (so they survive GC). We invoke the invocation handler, which is a
831// field within the proxy object, which will box the primitive arguments and deal with error cases.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700832extern "C" uint64_t artQuickProxyInvokeHandler(
833 ArtMethod* proxy_method, mirror::Object* receiver, Thread* self, ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -0700834 SHARED_REQUIRES(Locks::mutator_lock_) {
Brian Carlstromd3633d52013-08-20 21:06:26 -0700835 DCHECK(proxy_method->IsProxyMethod()) << PrettyMethod(proxy_method);
836 DCHECK(receiver->GetClass()->IsProxyClass()) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700837 // Ensure we don't get thread suspension until the object arguments are safely in jobjects.
838 const char* old_cause =
839 self->StartAssertNoThreadSuspension("Adding to IRT proxy object arguments");
840 // Register the top of the managed stack, making stack crawlable.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700841 DCHECK_EQ((*sp), proxy_method) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700842 DCHECK_EQ(proxy_method->GetFrameSizeInBytes(),
Brian Carlstromd3633d52013-08-20 21:06:26 -0700843 Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes())
844 << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700845 self->VerifyStack();
846 // Start new JNI local reference state.
847 JNIEnvExt* env = self->GetJniEnv();
848 ScopedObjectAccessUnchecked soa(env);
849 ScopedJniEnvLocalRefState env_state(env);
850 // Create local ref. copies of proxy method and the receiver.
851 jobject rcvr_jobj = soa.AddLocalReference<jobject>(receiver);
852
853 // Placing arguments into args vector and remove the receiver.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700854 ArtMethod* non_proxy_method = proxy_method->GetInterfaceMethodIfProxy(sizeof(void*));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700855 CHECK(!non_proxy_method->IsStatic()) << PrettyMethod(proxy_method) << " "
Andreas Gampec200a4a2014-06-16 18:39:09 -0700856 << PrettyMethod(non_proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700857 std::vector<jvalue> args;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700858 uint32_t shorty_len = 0;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700859 const char* shorty = non_proxy_method->GetShorty(&shorty_len);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700860 BuildQuickArgumentVisitor local_ref_visitor(sp, false, shorty, shorty_len, &soa, &args);
Brian Carlstromd3633d52013-08-20 21:06:26 -0700861
Ian Rogers848871b2013-08-05 10:56:33 -0700862 local_ref_visitor.VisitArguments();
Brian Carlstromd3633d52013-08-20 21:06:26 -0700863 DCHECK_GT(args.size(), 0U) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700864 args.erase(args.begin());
865
866 // Convert proxy method into expected interface method.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700867 ArtMethod* interface_method = proxy_method->FindOverriddenMethod(sizeof(void*));
Ian Rogerse0a02da2014-12-02 14:10:53 -0800868 DCHECK(interface_method != nullptr) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700869 DCHECK(!interface_method->IsProxyMethod()) << PrettyMethod(interface_method);
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700870 self->EndAssertNoThreadSuspension(old_cause);
871 jobject interface_method_jobj = soa.AddLocalReference<jobject>(
872 mirror::Method::CreateFromArtMethod(soa.Self(), interface_method));
Ian Rogers848871b2013-08-05 10:56:33 -0700873
874 // All naked Object*s should now be in jobjects, so its safe to go into the main invoke code
875 // that performs allocations.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700876 JValue result = InvokeProxyInvocationHandler(soa, shorty, rcvr_jobj, interface_method_jobj, args);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800877 // Restore references which might have moved.
878 local_ref_visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -0700879 return result.GetJ();
880}
881
882// Read object references held in arguments from quick frames and place in a JNI local references,
883// so they don't get garbage collected.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800884class RememberForGcArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700885 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700886 RememberForGcArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty,
887 uint32_t shorty_len, ScopedObjectAccessUnchecked* soa) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700888 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700889
Mathieu Chartier90443472015-07-16 20:32:27 -0700890 void Visit() SHARED_REQUIRES(Locks::mutator_lock_) OVERRIDE;
Mathieu Chartier07d447b2013-09-26 11:57:43 -0700891
Mathieu Chartier90443472015-07-16 20:32:27 -0700892 void FixupReferences() SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers848871b2013-08-05 10:56:33 -0700893
894 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700895 ScopedObjectAccessUnchecked* const soa_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800896 // References which we must update when exiting in case the GC moved the objects.
Andreas Gampec200a4a2014-06-16 18:39:09 -0700897 std::vector<std::pair<jobject, StackReference<mirror::Object>*> > references_;
898
Mathieu Chartier590fee92013-09-13 13:46:47 -0700899 DISALLOW_COPY_AND_ASSIGN(RememberForGcArgumentVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700900};
901
Ian Rogers9758f792014-03-13 09:02:55 -0700902void RememberForGcArgumentVisitor::Visit() {
903 if (IsParamAReference()) {
904 StackReference<mirror::Object>* stack_ref =
905 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
906 jobject reference =
907 soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
908 references_.push_back(std::make_pair(reference, stack_ref));
909 }
910}
911
912void RememberForGcArgumentVisitor::FixupReferences() {
913 // Fixup any references which may have changed.
914 for (const auto& pair : references_) {
915 pair.second->Assign(soa_->Decode<mirror::Object*>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -0700916 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -0700917 }
918}
919
Ian Rogers848871b2013-08-05 10:56:33 -0700920// Lazily resolve a method for quick. Called by stub code.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700921extern "C" const void* artQuickResolutionTrampoline(
922 ArtMethod* called, mirror::Object* receiver, Thread* self, ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -0700923 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe3b45ef22015-05-26 21:34:09 -0700924 // The resolution trampoline stashes the resolved method into the callee-save frame to transport
925 // it. Thus, when exiting, the stack cannot be verified (as the resolved method most likely
926 // does not have the same stack layout as the callee-save method).
927 ScopedQuickEntrypointChecks sqec(self, kIsDebugBuild, false);
Ian Rogers848871b2013-08-05 10:56:33 -0700928 // Start new JNI local reference state
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800929 JNIEnvExt* env = self->GetJniEnv();
Ian Rogers848871b2013-08-05 10:56:33 -0700930 ScopedObjectAccessUnchecked soa(env);
931 ScopedJniEnvLocalRefState env_state(env);
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800932 const char* old_cause = self->StartAssertNoThreadSuspension("Quick method resolution set up");
Ian Rogers848871b2013-08-05 10:56:33 -0700933
934 // Compute details about the called method (avoid GCs)
935 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Ian Rogers848871b2013-08-05 10:56:33 -0700936 InvokeType invoke_type;
Ian Rogerse0a02da2014-12-02 14:10:53 -0800937 MethodReference called_method(nullptr, 0);
938 const bool called_method_known_on_entry = !called->IsRuntimeMethod();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700939 ArtMethod* caller = nullptr;
Ian Rogerse0a02da2014-12-02 14:10:53 -0800940 if (!called_method_known_on_entry) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +0100941 caller = QuickArgumentVisitor::GetCallingMethod(sp);
942 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
Ian Rogers848871b2013-08-05 10:56:33 -0700943 const DexFile::CodeItem* code;
Ian Rogerse0a02da2014-12-02 14:10:53 -0800944 called_method.dex_file = caller->GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700945 code = caller->GetCodeItem();
Ian Rogers848871b2013-08-05 10:56:33 -0700946 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
947 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
948 Instruction::Code instr_code = instr->Opcode();
949 bool is_range;
950 switch (instr_code) {
951 case Instruction::INVOKE_DIRECT:
952 invoke_type = kDirect;
953 is_range = false;
954 break;
955 case Instruction::INVOKE_DIRECT_RANGE:
956 invoke_type = kDirect;
957 is_range = true;
958 break;
959 case Instruction::INVOKE_STATIC:
960 invoke_type = kStatic;
961 is_range = false;
962 break;
963 case Instruction::INVOKE_STATIC_RANGE:
964 invoke_type = kStatic;
965 is_range = true;
966 break;
967 case Instruction::INVOKE_SUPER:
968 invoke_type = kSuper;
969 is_range = false;
970 break;
971 case Instruction::INVOKE_SUPER_RANGE:
972 invoke_type = kSuper;
973 is_range = true;
974 break;
975 case Instruction::INVOKE_VIRTUAL:
976 invoke_type = kVirtual;
977 is_range = false;
978 break;
979 case Instruction::INVOKE_VIRTUAL_RANGE:
980 invoke_type = kVirtual;
981 is_range = true;
982 break;
983 case Instruction::INVOKE_INTERFACE:
984 invoke_type = kInterface;
985 is_range = false;
986 break;
987 case Instruction::INVOKE_INTERFACE_RANGE:
988 invoke_type = kInterface;
989 is_range = true;
990 break;
991 default:
Ian Rogerse0a02da2014-12-02 14:10:53 -0800992 LOG(FATAL) << "Unexpected call into trampoline: " << instr->DumpString(nullptr);
993 UNREACHABLE();
Ian Rogers848871b2013-08-05 10:56:33 -0700994 }
Ian Rogerse0a02da2014-12-02 14:10:53 -0800995 called_method.dex_method_index = (is_range) ? instr->VRegB_3rc() : instr->VRegB_35c();
Ian Rogers848871b2013-08-05 10:56:33 -0700996 } else {
997 invoke_type = kStatic;
Ian Rogerse0a02da2014-12-02 14:10:53 -0800998 called_method.dex_file = called->GetDexFile();
999 called_method.dex_method_index = called->GetDexMethodIndex();
Ian Rogers848871b2013-08-05 10:56:33 -07001000 }
1001 uint32_t shorty_len;
1002 const char* shorty =
Ian Rogerse0a02da2014-12-02 14:10:53 -08001003 called_method.dex_file->GetMethodShorty(
1004 called_method.dex_file->GetMethodId(called_method.dex_method_index), &shorty_len);
Mathieu Chartier590fee92013-09-13 13:46:47 -07001005 RememberForGcArgumentVisitor visitor(sp, invoke_type == kStatic, shorty, shorty_len, &soa);
Ian Rogers848871b2013-08-05 10:56:33 -07001006 visitor.VisitArguments();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001007 self->EndAssertNoThreadSuspension(old_cause);
Ian Rogerse0a02da2014-12-02 14:10:53 -08001008 const bool virtual_or_interface = invoke_type == kVirtual || invoke_type == kInterface;
Ian Rogers848871b2013-08-05 10:56:33 -07001009 // Resolve method filling in dex cache.
Ian Rogerse0a02da2014-12-02 14:10:53 -08001010 if (!called_method_known_on_entry) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001011 StackHandleScope<1> hs(self);
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001012 mirror::Object* dummy = nullptr;
1013 HandleWrapper<mirror::Object> h_receiver(
1014 hs.NewHandleWrapper(virtual_or_interface ? &receiver : &dummy));
Ian Rogerse0a02da2014-12-02 14:10:53 -08001015 DCHECK_EQ(caller->GetDexFile(), called_method.dex_file);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001016 called = linker->ResolveMethod(self, called_method.dex_method_index, caller, invoke_type);
Ian Rogers848871b2013-08-05 10:56:33 -07001017 }
Ian Rogerse0a02da2014-12-02 14:10:53 -08001018 const void* code = nullptr;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001019 if (LIKELY(!self->IsExceptionPending())) {
Ian Rogers848871b2013-08-05 10:56:33 -07001020 // Incompatible class change should have been handled in resolve method.
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001021 CHECK(!called->CheckIncompatibleClassChange(invoke_type))
1022 << PrettyMethod(called) << " " << invoke_type;
Mathieu Chartier55871bf2014-02-27 10:24:50 -08001023 if (virtual_or_interface) {
1024 // Refine called method based on receiver.
1025 CHECK(receiver != nullptr) << invoke_type;
Mingyao Yangf4867782014-05-05 11:55:02 -07001026
Mathieu Chartiere401d142015-04-22 13:56:20 -07001027 ArtMethod* orig_called = called;
Mathieu Chartier55871bf2014-02-27 10:24:50 -08001028 if (invoke_type == kVirtual) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001029 called = receiver->GetClass()->FindVirtualMethodForVirtual(called, sizeof(void*));
Mathieu Chartier55871bf2014-02-27 10:24:50 -08001030 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001031 called = receiver->GetClass()->FindVirtualMethodForInterface(called, sizeof(void*));
Mathieu Chartier55871bf2014-02-27 10:24:50 -08001032 }
Mingyao Yangf4867782014-05-05 11:55:02 -07001033
1034 CHECK(called != nullptr) << PrettyMethod(orig_called) << " "
1035 << PrettyTypeOf(receiver) << " "
1036 << invoke_type << " " << orig_called->GetVtableIndex();
1037
Ian Rogers83883d72013-10-21 21:07:24 -07001038 // 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 -08001039 // of the sharpened method avoiding dirtying the dex cache if possible.
Ian Rogers00f15272014-12-02 16:55:46 -08001040 // Note, called_method.dex_method_index references the dex method before the
1041 // FindVirtualMethodFor... This is ok for FindDexMethodIndexInOtherDexFile that only cares
1042 // about the name and signature.
1043 uint32_t update_dex_cache_method_index = called->GetDexMethodIndex();
Vladimir Marko05792b92015-08-03 11:56:49 +01001044 if (!called->HasSameDexCacheResolvedMethods(caller, sizeof(void*))) {
Ian Rogers83883d72013-10-21 21:07:24 -07001045 // Calling from one dex file to another, need to compute the method index appropriate to
Vladimir Markobbcc0c02014-02-03 14:08:42 +00001046 // the caller's dex file. Since we get here only if the original called was a runtime
1047 // method, we've got the correct dex_file and a dex_method_idx from above.
Ian Rogerse0a02da2014-12-02 14:10:53 -08001048 DCHECK(!called_method_known_on_entry);
1049 DCHECK_EQ(caller->GetDexFile(), called_method.dex_file);
1050 const DexFile* caller_dex_file = called_method.dex_file;
1051 uint32_t caller_method_name_and_sig_index = called_method.dex_method_index;
1052 update_dex_cache_method_index =
1053 called->FindDexMethodIndexInOtherDexFile(*caller_dex_file,
1054 caller_method_name_and_sig_index);
1055 }
1056 if ((update_dex_cache_method_index != DexFile::kDexNoIndex) &&
Mathieu Chartiere401d142015-04-22 13:56:20 -07001057 (caller->GetDexCacheResolvedMethod(
1058 update_dex_cache_method_index, sizeof(void*)) != called)) {
1059 caller->SetDexCacheResolvedMethod(update_dex_cache_method_index, called, sizeof(void*));
Ian Rogers83883d72013-10-21 21:07:24 -07001060 }
Mathieu Chartiere4a91bb2015-01-28 13:11:44 -08001061 } else if (invoke_type == kStatic) {
1062 const auto called_dex_method_idx = called->GetDexMethodIndex();
1063 // For static invokes, we may dispatch to the static method in the superclass but resolve
1064 // using the subclass. To prevent getting slow paths on each invoke, we force set the
1065 // resolved method for the super class dex method index if we are in the same dex file.
1066 // b/19175856
1067 if (called->GetDexFile() == called_method.dex_file &&
1068 called_method.dex_method_index != called_dex_method_idx) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001069 called->GetDexCache()->SetResolvedMethod(called_dex_method_idx, called, sizeof(void*));
Mathieu Chartiere4a91bb2015-01-28 13:11:44 -08001070 }
Ian Rogers83883d72013-10-21 21:07:24 -07001071 }
Daniel Mihalyieb076692014-08-22 17:33:31 +02001072
Ian Rogers848871b2013-08-05 10:56:33 -07001073 // Ensure that the called method's class is initialized.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001074 StackHandleScope<1> hs(soa.Self());
1075 Handle<mirror::Class> called_class(hs.NewHandle(called->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -07001076 linker->EnsureInitialized(soa.Self(), called_class, true, true);
Ian Rogers848871b2013-08-05 10:56:33 -07001077 if (LIKELY(called_class->IsInitialized())) {
Daniel Mihalyieb076692014-08-22 17:33:31 +02001078 if (UNLIKELY(Dbg::IsForcedInterpreterNeededForResolution(self, called))) {
1079 // If we are single-stepping or the called method is deoptimized (by a
1080 // breakpoint, for example), then we have to execute the called method
1081 // with the interpreter.
1082 code = GetQuickToInterpreterBridge();
1083 } else if (UNLIKELY(Dbg::IsForcedInstrumentationNeededForResolution(self, caller))) {
1084 // If the caller is deoptimized (by a breakpoint, for example), we have to
1085 // continue its execution with interpreter when returning from the called
1086 // method. Because we do not want to execute the called method with the
1087 // interpreter, we wrap its execution into the instrumentation stubs.
1088 // When the called method returns, it will execute the instrumentation
1089 // exit hook that will determine the need of the interpreter with a call
1090 // to Dbg::IsForcedInterpreterNeededForUpcall and deoptimize the stack if
1091 // it is needed.
1092 code = GetQuickInstrumentationEntryPoint();
1093 } else {
1094 code = called->GetEntryPointFromQuickCompiledCode();
1095 }
Ian Rogers848871b2013-08-05 10:56:33 -07001096 } else if (called_class->IsInitializing()) {
Daniel Mihalyieb076692014-08-22 17:33:31 +02001097 if (UNLIKELY(Dbg::IsForcedInterpreterNeededForResolution(self, called))) {
1098 // If we are single-stepping or the called method is deoptimized (by a
1099 // breakpoint, for example), then we have to execute the called method
1100 // with the interpreter.
1101 code = GetQuickToInterpreterBridge();
1102 } else if (invoke_type == kStatic) {
Ian Rogers848871b2013-08-05 10:56:33 -07001103 // Class is still initializing, go to oat and grab code (trampoline must be left in place
1104 // until class is initialized to stop races between threads).
Ian Rogersef7d42f2014-01-06 12:55:46 -08001105 code = linker->GetQuickOatCodeFor(called);
Ian Rogers848871b2013-08-05 10:56:33 -07001106 } else {
1107 // No trampoline for non-static methods.
Ian Rogersef7d42f2014-01-06 12:55:46 -08001108 code = called->GetEntryPointFromQuickCompiledCode();
Ian Rogers848871b2013-08-05 10:56:33 -07001109 }
1110 } else {
1111 DCHECK(called_class->IsErroneous());
1112 }
1113 }
Ian Rogerse0a02da2014-12-02 14:10:53 -08001114 CHECK_EQ(code == nullptr, self->IsExceptionPending());
Mathieu Chartier07d447b2013-09-26 11:57:43 -07001115 // Fixup any locally saved objects may have moved during a GC.
1116 visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -07001117 // Place called method in callee-save frame to be placed as first argument to quick method.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001118 *sp = called;
1119
Ian Rogers848871b2013-08-05 10:56:33 -07001120 return code;
1121}
1122
Andreas Gampec147b002014-03-06 18:11:06 -08001123/*
1124 * This class uses a couple of observations to unite the different calling conventions through
1125 * a few constants.
1126 *
1127 * 1) Number of registers used for passing is normally even, so counting down has no penalty for
1128 * possible alignment.
1129 * 2) Known 64b architectures store 8B units on the stack, both for integral and floating point
1130 * types, so using uintptr_t is OK. Also means that we can use kRegistersNeededX to denote
1131 * when we have to split things
1132 * 3) The only soft-float, Arm, is 32b, so no widening needs to be taken into account for floats
1133 * and we can use Int handling directly.
1134 * 4) Only 64b architectures widen, and their stack is aligned 8B anyways, so no padding code
1135 * necessary when widening. Also, widening of Ints will take place implicitly, and the
1136 * extension should be compatible with Aarch64, which mandates copying the available bits
1137 * into LSB and leaving the rest unspecified.
1138 * 5) Aligning longs and doubles is necessary on arm only, and it's the same in registers and on
1139 * the stack.
1140 * 6) There is only little endian.
1141 *
1142 *
1143 * Actual work is supposed to be done in a delegate of the template type. The interface is as
1144 * follows:
1145 *
1146 * void PushGpr(uintptr_t): Add a value for the next GPR
1147 *
1148 * void PushFpr4(float): Add a value for the next FPR of size 32b. Is only called if we need
1149 * padding, that is, think the architecture is 32b and aligns 64b.
1150 *
1151 * void PushFpr8(uint64_t): Push a double. We _will_ call this on 32b, it's the callee's job to
1152 * split this if necessary. The current state will have aligned, if
1153 * necessary.
1154 *
1155 * void PushStack(uintptr_t): Push a value to the stack.
1156 *
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001157 * uintptr_t PushHandleScope(mirror::Object* ref): Add a reference to the HandleScope. This _will_ have nullptr,
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001158 * as this might be important for null initialization.
Andreas Gampec147b002014-03-06 18:11:06 -08001159 * Must return the jobject, that is, the reference to the
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001160 * entry in the HandleScope (nullptr if necessary).
Andreas Gampec147b002014-03-06 18:11:06 -08001161 *
1162 */
Andreas Gampec200a4a2014-06-16 18:39:09 -07001163template<class T> class BuildNativeCallFrameStateMachine {
Andreas Gampec147b002014-03-06 18:11:06 -08001164 public:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001165#if defined(__arm__)
1166 // TODO: These are all dummy values!
Andreas Gampec147b002014-03-06 18:11:06 -08001167 static constexpr bool kNativeSoftFloatAbi = true;
1168 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs, r0-r3
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001169 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
1170
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001171 static constexpr size_t kRegistersNeededForLong = 2;
1172 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -08001173 static constexpr bool kMultiRegistersAligned = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001174 static constexpr bool kMultiFPRegistersWidened = false;
1175 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001176 static constexpr bool kAlignLongOnStack = true;
1177 static constexpr bool kAlignDoubleOnStack = true;
Stuart Monteithb95a5342014-03-12 13:32:32 +00001178#elif defined(__aarch64__)
1179 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
1180 static constexpr size_t kNumNativeGprArgs = 8; // 6 arguments passed in GPRs.
1181 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
1182
1183 static constexpr size_t kRegistersNeededForLong = 1;
1184 static constexpr size_t kRegistersNeededForDouble = 1;
1185 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001186 static constexpr bool kMultiFPRegistersWidened = false;
1187 static constexpr bool kMultiGPRegistersWidened = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +00001188 static constexpr bool kAlignLongOnStack = false;
1189 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001190#elif defined(__mips__) && !defined(__LP64__)
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001191 static constexpr bool kNativeSoftFloatAbi = true; // This is a hard float ABI.
Douglas Leung735b8552014-10-31 12:21:40 -07001192 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs.
1193 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001194
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001195 static constexpr size_t kRegistersNeededForLong = 2;
1196 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -08001197 static constexpr bool kMultiRegistersAligned = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001198 static constexpr bool kMultiFPRegistersWidened = true;
1199 static constexpr bool kMultiGPRegistersWidened = false;
Douglas Leung735b8552014-10-31 12:21:40 -07001200 static constexpr bool kAlignLongOnStack = true;
1201 static constexpr bool kAlignDoubleOnStack = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001202#elif defined(__mips__) && defined(__LP64__)
1203 // Let the code prepare GPRs only and we will load the FPRs with same data.
1204 static constexpr bool kNativeSoftFloatAbi = true;
1205 static constexpr size_t kNumNativeGprArgs = 8;
1206 static constexpr size_t kNumNativeFprArgs = 0;
1207
1208 static constexpr size_t kRegistersNeededForLong = 1;
1209 static constexpr size_t kRegistersNeededForDouble = 1;
1210 static constexpr bool kMultiRegistersAligned = false;
1211 static constexpr bool kMultiFPRegistersWidened = false;
1212 static constexpr bool kMultiGPRegistersWidened = true;
1213 static constexpr bool kAlignLongOnStack = false;
1214 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001215#elif defined(__i386__)
1216 // TODO: Check these!
Andreas Gampec147b002014-03-06 18:11:06 -08001217 static constexpr bool kNativeSoftFloatAbi = false; // Not using int registers for fp
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001218 static constexpr size_t kNumNativeGprArgs = 0; // 6 arguments passed in GPRs.
1219 static constexpr size_t kNumNativeFprArgs = 0; // 8 arguments passed in FPRs.
1220
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001221 static constexpr size_t kRegistersNeededForLong = 2;
1222 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001223 static constexpr bool kMultiRegistersAligned = false; // x86 not using regs, anyways
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001224 static constexpr bool kMultiFPRegistersWidened = false;
1225 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001226 static constexpr bool kAlignLongOnStack = false;
1227 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001228#elif defined(__x86_64__)
1229 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
1230 static constexpr size_t kNumNativeGprArgs = 6; // 6 arguments passed in GPRs.
1231 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
1232
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001233 static constexpr size_t kRegistersNeededForLong = 1;
1234 static constexpr size_t kRegistersNeededForDouble = 1;
Andreas Gampec147b002014-03-06 18:11:06 -08001235 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001236 static constexpr bool kMultiFPRegistersWidened = false;
1237 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001238 static constexpr bool kAlignLongOnStack = false;
1239 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001240#else
1241#error "Unsupported architecture"
1242#endif
1243
Andreas Gampec147b002014-03-06 18:11:06 -08001244 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001245 explicit BuildNativeCallFrameStateMachine(T* delegate)
1246 : gpr_index_(kNumNativeGprArgs),
1247 fpr_index_(kNumNativeFprArgs),
1248 stack_entries_(0),
1249 delegate_(delegate) {
Andreas Gampec147b002014-03-06 18:11:06 -08001250 // For register alignment, we want to assume that counters (gpr_index_, fpr_index_) are even iff
1251 // the next register is even; counting down is just to make the compiler happy...
Andreas Gampe575e78c2014-11-03 23:41:03 -08001252 static_assert(kNumNativeGprArgs % 2 == 0U, "Number of native GPR arguments not even");
1253 static_assert(kNumNativeFprArgs % 2 == 0U, "Number of native FPR arguments not even");
Andreas Gampec147b002014-03-06 18:11:06 -08001254 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001255
Andreas Gampec200a4a2014-06-16 18:39:09 -07001256 virtual ~BuildNativeCallFrameStateMachine() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001257
Ian Rogers1428dce2014-10-21 15:02:15 -07001258 bool HavePointerGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001259 return gpr_index_ > 0;
1260 }
1261
Andreas Gampec200a4a2014-06-16 18:39:09 -07001262 void AdvancePointer(const void* val) {
Andreas Gampec147b002014-03-06 18:11:06 -08001263 if (HavePointerGpr()) {
1264 gpr_index_--;
1265 PushGpr(reinterpret_cast<uintptr_t>(val));
1266 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001267 stack_entries_++; // TODO: have a field for pointer length as multiple of 32b
Andreas Gampec147b002014-03-06 18:11:06 -08001268 PushStack(reinterpret_cast<uintptr_t>(val));
1269 gpr_index_ = 0;
1270 }
1271 }
1272
Ian Rogers1428dce2014-10-21 15:02:15 -07001273 bool HaveHandleScopeGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001274 return gpr_index_ > 0;
1275 }
1276
Mathieu Chartier90443472015-07-16 20:32:27 -07001277 void AdvanceHandleScope(mirror::Object* ptr) SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001278 uintptr_t handle = PushHandle(ptr);
1279 if (HaveHandleScopeGpr()) {
Andreas Gampec147b002014-03-06 18:11:06 -08001280 gpr_index_--;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001281 PushGpr(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001282 } else {
1283 stack_entries_++;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001284 PushStack(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001285 gpr_index_ = 0;
1286 }
1287 }
1288
Ian Rogers1428dce2014-10-21 15:02:15 -07001289 bool HaveIntGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001290 return gpr_index_ > 0;
1291 }
1292
1293 void AdvanceInt(uint32_t val) {
1294 if (HaveIntGpr()) {
1295 gpr_index_--;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001296 if (kMultiGPRegistersWidened) {
1297 DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001298 PushGpr(static_cast<int64_t>(bit_cast<int32_t, uint32_t>(val)));
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001299 } else {
1300 PushGpr(val);
1301 }
Andreas Gampec147b002014-03-06 18:11:06 -08001302 } else {
1303 stack_entries_++;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001304 if (kMultiGPRegistersWidened) {
1305 DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001306 PushStack(static_cast<int64_t>(bit_cast<int32_t, uint32_t>(val)));
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001307 } else {
1308 PushStack(val);
1309 }
Andreas Gampec147b002014-03-06 18:11:06 -08001310 gpr_index_ = 0;
1311 }
1312 }
1313
Ian Rogers1428dce2014-10-21 15:02:15 -07001314 bool HaveLongGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001315 return gpr_index_ >= kRegistersNeededForLong + (LongGprNeedsPadding() ? 1 : 0);
1316 }
1317
Ian Rogers1428dce2014-10-21 15:02:15 -07001318 bool LongGprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001319 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1320 kAlignLongOnStack && // and when it needs alignment
1321 (gpr_index_ & 1) == 1; // counter is odd, see constructor
1322 }
1323
Ian Rogers1428dce2014-10-21 15:02:15 -07001324 bool LongStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001325 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1326 kAlignLongOnStack && // and when it needs 8B alignment
1327 (stack_entries_ & 1) == 1; // counter is odd
1328 }
1329
1330 void AdvanceLong(uint64_t val) {
1331 if (HaveLongGpr()) {
1332 if (LongGprNeedsPadding()) {
1333 PushGpr(0);
1334 gpr_index_--;
1335 }
1336 if (kRegistersNeededForLong == 1) {
1337 PushGpr(static_cast<uintptr_t>(val));
1338 } else {
1339 PushGpr(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1340 PushGpr(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1341 }
1342 gpr_index_ -= kRegistersNeededForLong;
1343 } else {
1344 if (LongStackNeedsPadding()) {
1345 PushStack(0);
1346 stack_entries_++;
1347 }
1348 if (kRegistersNeededForLong == 1) {
1349 PushStack(static_cast<uintptr_t>(val));
1350 stack_entries_++;
1351 } else {
1352 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1353 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1354 stack_entries_ += 2;
1355 }
1356 gpr_index_ = 0;
1357 }
1358 }
1359
Ian Rogers1428dce2014-10-21 15:02:15 -07001360 bool HaveFloatFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001361 return fpr_index_ > 0;
1362 }
1363
Andreas Gampec147b002014-03-06 18:11:06 -08001364 void AdvanceFloat(float val) {
1365 if (kNativeSoftFloatAbi) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001366 AdvanceInt(bit_cast<uint32_t, float>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001367 } else {
1368 if (HaveFloatFpr()) {
1369 fpr_index_--;
1370 if (kRegistersNeededForDouble == 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001371 if (kMultiFPRegistersWidened) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001372 PushFpr8(bit_cast<uint64_t, double>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001373 } else {
1374 // No widening, just use the bits.
Roland Levillainda4d79b2015-03-24 14:36:11 +00001375 PushFpr8(static_cast<uint64_t>(bit_cast<uint32_t, float>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001376 }
1377 } else {
1378 PushFpr4(val);
1379 }
1380 } else {
1381 stack_entries_++;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001382 if (kRegistersNeededForDouble == 1 && kMultiFPRegistersWidened) {
Andreas Gampec147b002014-03-06 18:11:06 -08001383 // Need to widen before storing: Note the "double" in the template instantiation.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001384 // Note: We need to jump through those hoops to make the compiler happy.
1385 DCHECK_EQ(sizeof(uintptr_t), sizeof(uint64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001386 PushStack(static_cast<uintptr_t>(bit_cast<uint64_t, double>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001387 } else {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001388 PushStack(static_cast<uintptr_t>(bit_cast<uint32_t, float>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001389 }
1390 fpr_index_ = 0;
1391 }
1392 }
1393 }
1394
Ian Rogers1428dce2014-10-21 15:02:15 -07001395 bool HaveDoubleFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001396 return fpr_index_ >= kRegistersNeededForDouble + (DoubleFprNeedsPadding() ? 1 : 0);
1397 }
1398
Ian Rogers1428dce2014-10-21 15:02:15 -07001399 bool DoubleFprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001400 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1401 kAlignDoubleOnStack && // and when it needs alignment
1402 (fpr_index_ & 1) == 1; // counter is odd, see constructor
1403 }
1404
Ian Rogers1428dce2014-10-21 15:02:15 -07001405 bool DoubleStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001406 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1407 kAlignDoubleOnStack && // and when it needs 8B alignment
1408 (stack_entries_ & 1) == 1; // counter is odd
1409 }
1410
1411 void AdvanceDouble(uint64_t val) {
1412 if (kNativeSoftFloatAbi) {
1413 AdvanceLong(val);
1414 } else {
1415 if (HaveDoubleFpr()) {
1416 if (DoubleFprNeedsPadding()) {
1417 PushFpr4(0);
1418 fpr_index_--;
1419 }
1420 PushFpr8(val);
1421 fpr_index_ -= kRegistersNeededForDouble;
1422 } else {
1423 if (DoubleStackNeedsPadding()) {
1424 PushStack(0);
1425 stack_entries_++;
1426 }
1427 if (kRegistersNeededForDouble == 1) {
1428 PushStack(static_cast<uintptr_t>(val));
1429 stack_entries_++;
1430 } else {
1431 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1432 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1433 stack_entries_ += 2;
1434 }
1435 fpr_index_ = 0;
1436 }
1437 }
1438 }
1439
Ian Rogers1428dce2014-10-21 15:02:15 -07001440 uint32_t GetStackEntries() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001441 return stack_entries_;
1442 }
1443
Ian Rogers1428dce2014-10-21 15:02:15 -07001444 uint32_t GetNumberOfUsedGprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001445 return kNumNativeGprArgs - gpr_index_;
1446 }
1447
Ian Rogers1428dce2014-10-21 15:02:15 -07001448 uint32_t GetNumberOfUsedFprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001449 return kNumNativeFprArgs - fpr_index_;
1450 }
1451
1452 private:
1453 void PushGpr(uintptr_t val) {
1454 delegate_->PushGpr(val);
1455 }
1456 void PushFpr4(float val) {
1457 delegate_->PushFpr4(val);
1458 }
1459 void PushFpr8(uint64_t val) {
1460 delegate_->PushFpr8(val);
1461 }
1462 void PushStack(uintptr_t val) {
1463 delegate_->PushStack(val);
1464 }
Mathieu Chartier90443472015-07-16 20:32:27 -07001465 uintptr_t PushHandle(mirror::Object* ref) SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001466 return delegate_->PushHandle(ref);
Andreas Gampec147b002014-03-06 18:11:06 -08001467 }
1468
1469 uint32_t gpr_index_; // Number of free GPRs
1470 uint32_t fpr_index_; // Number of free FPRs
1471 uint32_t stack_entries_; // Stack entries are in multiples of 32b, as floats are usually not
1472 // extended
Ian Rogers1428dce2014-10-21 15:02:15 -07001473 T* const delegate_; // What Push implementation gets called
Andreas Gampec147b002014-03-06 18:11:06 -08001474};
1475
Andreas Gampec200a4a2014-06-16 18:39:09 -07001476// Computes the sizes of register stacks and call stack area. Handling of references can be extended
1477// in subclasses.
1478//
1479// To handle native pointers, use "L" in the shorty for an object reference, which simulates
1480// them with handles.
1481class ComputeNativeCallFrameSize {
Andreas Gampec147b002014-03-06 18:11:06 -08001482 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001483 ComputeNativeCallFrameSize() : num_stack_entries_(0) {}
1484
1485 virtual ~ComputeNativeCallFrameSize() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001486
Ian Rogers1428dce2014-10-21 15:02:15 -07001487 uint32_t GetStackSize() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001488 return num_stack_entries_ * sizeof(uintptr_t);
1489 }
1490
Ian Rogers1428dce2014-10-21 15:02:15 -07001491 uint8_t* LayoutCallStack(uint8_t* sp8) const {
Andreas Gampec147b002014-03-06 18:11:06 -08001492 sp8 -= GetStackSize();
Andreas Gampe779f8c92014-06-09 18:29:38 -07001493 // Align by kStackAlignment.
1494 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
Andreas Gampec200a4a2014-06-16 18:39:09 -07001495 return sp8;
Andreas Gampec147b002014-03-06 18:11:06 -08001496 }
1497
Ian Rogers1428dce2014-10-21 15:02:15 -07001498 uint8_t* LayoutCallRegisterStacks(uint8_t* sp8, uintptr_t** start_gpr, uint32_t** start_fpr)
1499 const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001500 // Assumption is OK right now, as we have soft-float arm
1501 size_t fregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeFprArgs;
1502 sp8 -= fregs * sizeof(uintptr_t);
1503 *start_fpr = reinterpret_cast<uint32_t*>(sp8);
1504 size_t iregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeGprArgs;
1505 sp8 -= iregs * sizeof(uintptr_t);
1506 *start_gpr = reinterpret_cast<uintptr_t*>(sp8);
1507 return sp8;
1508 }
Andreas Gampec147b002014-03-06 18:11:06 -08001509
Andreas Gampec200a4a2014-06-16 18:39:09 -07001510 uint8_t* LayoutNativeCall(uint8_t* sp8, uintptr_t** start_stack, uintptr_t** start_gpr,
Ian Rogers1428dce2014-10-21 15:02:15 -07001511 uint32_t** start_fpr) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001512 // Native call stack.
1513 sp8 = LayoutCallStack(sp8);
1514 *start_stack = reinterpret_cast<uintptr_t*>(sp8);
Andreas Gampec147b002014-03-06 18:11:06 -08001515
Andreas Gampec200a4a2014-06-16 18:39:09 -07001516 // Put fprs and gprs below.
1517 sp8 = LayoutCallRegisterStacks(sp8, start_gpr, start_fpr);
Andreas Gampec147b002014-03-06 18:11:06 -08001518
Andreas Gampec200a4a2014-06-16 18:39:09 -07001519 // Return the new bottom.
1520 return sp8;
1521 }
1522
1523 virtual void WalkHeader(BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm)
Mathieu Chartier90443472015-07-16 20:32:27 -07001524 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001525 UNUSED(sm);
1526 }
Andreas Gampec200a4a2014-06-16 18:39:09 -07001527
Mathieu Chartier90443472015-07-16 20:32:27 -07001528 void Walk(const char* shorty, uint32_t shorty_len) SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001529 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize> sm(this);
1530
1531 WalkHeader(&sm);
Andreas Gampec147b002014-03-06 18:11:06 -08001532
1533 for (uint32_t i = 1; i < shorty_len; ++i) {
1534 Primitive::Type cur_type_ = Primitive::GetType(shorty[i]);
1535 switch (cur_type_) {
1536 case Primitive::kPrimNot:
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001537 // TODO: fix abuse of mirror types.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001538 sm.AdvanceHandleScope(
1539 reinterpret_cast<mirror::Object*>(0x12345678));
Andreas Gampec147b002014-03-06 18:11:06 -08001540 break;
1541
1542 case Primitive::kPrimBoolean:
1543 case Primitive::kPrimByte:
1544 case Primitive::kPrimChar:
1545 case Primitive::kPrimShort:
1546 case Primitive::kPrimInt:
1547 sm.AdvanceInt(0);
1548 break;
1549 case Primitive::kPrimFloat:
1550 sm.AdvanceFloat(0);
1551 break;
1552 case Primitive::kPrimDouble:
1553 sm.AdvanceDouble(0);
1554 break;
1555 case Primitive::kPrimLong:
1556 sm.AdvanceLong(0);
1557 break;
1558 default:
1559 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001560 UNREACHABLE();
Andreas Gampec147b002014-03-06 18:11:06 -08001561 }
1562 }
1563
Ian Rogers1428dce2014-10-21 15:02:15 -07001564 num_stack_entries_ = sm.GetStackEntries();
Andreas Gampec147b002014-03-06 18:11:06 -08001565 }
1566
1567 void PushGpr(uintptr_t /* val */) {
1568 // not optimizing registers, yet
1569 }
1570
1571 void PushFpr4(float /* val */) {
1572 // not optimizing registers, yet
1573 }
1574
1575 void PushFpr8(uint64_t /* val */) {
1576 // not optimizing registers, yet
1577 }
1578
1579 void PushStack(uintptr_t /* val */) {
1580 // counting is already done in the superclass
1581 }
1582
Andreas Gampec200a4a2014-06-16 18:39:09 -07001583 virtual uintptr_t PushHandle(mirror::Object* /* ptr */) {
Andreas Gampec147b002014-03-06 18:11:06 -08001584 return reinterpret_cast<uintptr_t>(nullptr);
1585 }
1586
Andreas Gampec200a4a2014-06-16 18:39:09 -07001587 protected:
Andreas Gampec147b002014-03-06 18:11:06 -08001588 uint32_t num_stack_entries_;
1589};
1590
Andreas Gampec200a4a2014-06-16 18:39:09 -07001591class ComputeGenericJniFrameSize FINAL : public ComputeNativeCallFrameSize {
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001592 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001593 ComputeGenericJniFrameSize() : num_handle_scope_references_(0) {}
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001594
Andreas Gampec200a4a2014-06-16 18:39:09 -07001595 // Lays out the callee-save frame. Assumes that the incorrect frame corresponding to RefsAndArgs
1596 // is at *m = sp. Will update to point to the bottom of the save frame.
1597 //
1598 // Note: assumes ComputeAll() has been run before.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001599 void LayoutCalleeSaveFrame(Thread* self, ArtMethod*** m, void* sp, HandleScope** handle_scope)
Mathieu Chartier90443472015-07-16 20:32:27 -07001600 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001601 ArtMethod* method = **m;
1602
1603 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), sizeof(void*));
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001604
Andreas Gampec200a4a2014-06-16 18:39:09 -07001605 uint8_t* sp8 = reinterpret_cast<uint8_t*>(sp);
1606
1607 // First, fix up the layout of the callee-save frame.
1608 // We have to squeeze in the HandleScope, and relocate the method pointer.
1609
1610 // "Free" the slot for the method.
Ian Rogers13735952014-10-08 12:43:28 -07001611 sp8 += sizeof(void*); // In the callee-save frame we use a full pointer.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001612
1613 // Under the callee saves put handle scope and new method stack reference.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001614 size_t handle_scope_size = HandleScope::SizeOf(num_handle_scope_references_);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001615 size_t scope_and_method = handle_scope_size + sizeof(ArtMethod*);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001616
1617 sp8 -= scope_and_method;
1618 // Align by kStackAlignment.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001619 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
Andreas Gampec200a4a2014-06-16 18:39:09 -07001620
Mathieu Chartiere401d142015-04-22 13:56:20 -07001621 uint8_t* sp8_table = sp8 + sizeof(ArtMethod*);
Ian Rogers59c07062014-10-10 13:03:39 -07001622 *handle_scope = HandleScope::Create(sp8_table, self->GetTopHandleScope(),
1623 num_handle_scope_references_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001624
1625 // Add a slot for the method pointer, and fill it. Fix the pointer-pointer given to us.
1626 uint8_t* method_pointer = sp8;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001627 auto** new_method_ref = reinterpret_cast<ArtMethod**>(method_pointer);
1628 *new_method_ref = method;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001629 *m = new_method_ref;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001630 }
1631
Andreas Gampec200a4a2014-06-16 18:39:09 -07001632 // Adds space for the cookie. Note: may leave stack unaligned.
Ian Rogers1428dce2014-10-21 15:02:15 -07001633 void LayoutCookie(uint8_t** sp) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001634 // Reference cookie and padding
1635 *sp -= 8;
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001636 }
1637
Andreas Gampec200a4a2014-06-16 18:39:09 -07001638 // Re-layout the callee-save frame (insert a handle-scope). Then add space for the cookie.
1639 // Returns the new bottom. Note: this may be unaligned.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001640 uint8_t* LayoutJNISaveFrame(Thread* self, ArtMethod*** m, void* sp, HandleScope** handle_scope)
Mathieu Chartier90443472015-07-16 20:32:27 -07001641 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001642 // First, fix up the layout of the callee-save frame.
1643 // We have to squeeze in the HandleScope, and relocate the method pointer.
Ian Rogers59c07062014-10-10 13:03:39 -07001644 LayoutCalleeSaveFrame(self, m, sp, handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001645
1646 // The bottom of the callee-save frame is now where the method is, *m.
1647 uint8_t* sp8 = reinterpret_cast<uint8_t*>(*m);
1648
1649 // Add space for cookie.
1650 LayoutCookie(&sp8);
1651
1652 return sp8;
1653 }
1654
1655 // WARNING: After this, *sp won't be pointing to the method anymore!
Mathieu Chartiere401d142015-04-22 13:56:20 -07001656 uint8_t* ComputeLayout(Thread* self, ArtMethod*** m, const char* shorty, uint32_t shorty_len,
1657 HandleScope** handle_scope, uintptr_t** start_stack, uintptr_t** start_gpr,
1658 uint32_t** start_fpr)
Mathieu Chartier90443472015-07-16 20:32:27 -07001659 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001660 Walk(shorty, shorty_len);
1661
1662 // JNI part.
Ian Rogers59c07062014-10-10 13:03:39 -07001663 uint8_t* sp8 = LayoutJNISaveFrame(self, m, reinterpret_cast<void*>(*m), handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001664
1665 sp8 = LayoutNativeCall(sp8, start_stack, start_gpr, start_fpr);
1666
1667 // Return the new bottom.
1668 return sp8;
1669 }
1670
1671 uintptr_t PushHandle(mirror::Object* /* ptr */) OVERRIDE;
1672
1673 // Add JNIEnv* and jobj/jclass before the shorty-derived elements.
1674 void WalkHeader(BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) OVERRIDE
Mathieu Chartier90443472015-07-16 20:32:27 -07001675 SHARED_REQUIRES(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001676
1677 private:
1678 uint32_t num_handle_scope_references_;
1679};
1680
1681uintptr_t ComputeGenericJniFrameSize::PushHandle(mirror::Object* /* ptr */) {
1682 num_handle_scope_references_++;
1683 return reinterpret_cast<uintptr_t>(nullptr);
1684}
1685
1686void ComputeGenericJniFrameSize::WalkHeader(
1687 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) {
1688 // JNIEnv
1689 sm->AdvancePointer(nullptr);
1690
1691 // Class object or this as first argument
1692 sm->AdvanceHandleScope(reinterpret_cast<mirror::Object*>(0x12345678));
1693}
1694
1695// Class to push values to three separate regions. Used to fill the native call part. Adheres to
1696// the template requirements of BuildGenericJniFrameStateMachine.
1697class FillNativeCall {
1698 public:
1699 FillNativeCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) :
1700 cur_gpr_reg_(gpr_regs), cur_fpr_reg_(fpr_regs), cur_stack_arg_(stack_args) {}
1701
1702 virtual ~FillNativeCall() {}
1703
1704 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) {
1705 cur_gpr_reg_ = gpr_regs;
1706 cur_fpr_reg_ = fpr_regs;
1707 cur_stack_arg_ = stack_args;
Andreas Gampec147b002014-03-06 18:11:06 -08001708 }
1709
1710 void PushGpr(uintptr_t val) {
1711 *cur_gpr_reg_ = val;
1712 cur_gpr_reg_++;
1713 }
1714
1715 void PushFpr4(float val) {
1716 *cur_fpr_reg_ = val;
1717 cur_fpr_reg_++;
1718 }
1719
1720 void PushFpr8(uint64_t val) {
1721 uint64_t* tmp = reinterpret_cast<uint64_t*>(cur_fpr_reg_);
1722 *tmp = val;
1723 cur_fpr_reg_ += 2;
1724 }
1725
1726 void PushStack(uintptr_t val) {
1727 *cur_stack_arg_ = val;
1728 cur_stack_arg_++;
1729 }
1730
Mathieu Chartier90443472015-07-16 20:32:27 -07001731 virtual uintptr_t PushHandle(mirror::Object*) SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001732 LOG(FATAL) << "(Non-JNI) Native call does not use handles.";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001733 UNREACHABLE();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001734 }
1735
1736 private:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001737 uintptr_t* cur_gpr_reg_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001738 uint32_t* cur_fpr_reg_;
1739 uintptr_t* cur_stack_arg_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001740};
Andreas Gampec147b002014-03-06 18:11:06 -08001741
Andreas Gampec200a4a2014-06-16 18:39:09 -07001742// Visits arguments on the stack placing them into a region lower down the stack for the benefit
1743// of transitioning into native code.
1744class BuildGenericJniFrameVisitor FINAL : public QuickArgumentVisitor {
1745 public:
Ian Rogers59c07062014-10-10 13:03:39 -07001746 BuildGenericJniFrameVisitor(Thread* self, bool is_static, const char* shorty, uint32_t shorty_len,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001747 ArtMethod*** sp)
Andreas Gampec200a4a2014-06-16 18:39:09 -07001748 : QuickArgumentVisitor(*sp, is_static, shorty, shorty_len),
1749 jni_call_(nullptr, nullptr, nullptr, nullptr), sm_(&jni_call_) {
1750 ComputeGenericJniFrameSize fsc;
1751 uintptr_t* start_gpr_reg;
1752 uint32_t* start_fpr_reg;
1753 uintptr_t* start_stack_arg;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001754 bottom_of_used_area_ = fsc.ComputeLayout(self, sp, shorty, shorty_len,
Ian Rogers59c07062014-10-10 13:03:39 -07001755 &handle_scope_,
1756 &start_stack_arg,
Andreas Gampec200a4a2014-06-16 18:39:09 -07001757 &start_gpr_reg, &start_fpr_reg);
1758
Andreas Gampec200a4a2014-06-16 18:39:09 -07001759 jni_call_.Reset(start_gpr_reg, start_fpr_reg, start_stack_arg, handle_scope_);
1760
1761 // jni environment is always first argument
1762 sm_.AdvancePointer(self->GetJniEnv());
1763
1764 if (is_static) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001765 sm_.AdvanceHandleScope((**sp)->GetDeclaringClass());
Andreas Gampec200a4a2014-06-16 18:39:09 -07001766 }
1767 }
1768
Mathieu Chartier90443472015-07-16 20:32:27 -07001769 void Visit() SHARED_REQUIRES(Locks::mutator_lock_) OVERRIDE;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001770
Mathieu Chartier90443472015-07-16 20:32:27 -07001771 void FinalizeHandleScope(Thread* self) SHARED_REQUIRES(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001772
1773 StackReference<mirror::Object>* GetFirstHandleScopeEntry()
Mathieu Chartier90443472015-07-16 20:32:27 -07001774 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001775 return handle_scope_->GetHandle(0).GetReference();
1776 }
1777
Mathieu Chartier90443472015-07-16 20:32:27 -07001778 jobject GetFirstHandleScopeJObject() const SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001779 return handle_scope_->GetHandle(0).ToJObject();
1780 }
1781
Ian Rogers1428dce2014-10-21 15:02:15 -07001782 void* GetBottomOfUsedArea() const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001783 return bottom_of_used_area_;
1784 }
1785
1786 private:
1787 // A class to fill a JNI call. Adds reference/handle-scope management to FillNativeCall.
1788 class FillJniCall FINAL : public FillNativeCall {
1789 public:
1790 FillJniCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args,
1791 HandleScope* handle_scope) : FillNativeCall(gpr_regs, fpr_regs, stack_args),
1792 handle_scope_(handle_scope), cur_entry_(0) {}
1793
Mathieu Chartier90443472015-07-16 20:32:27 -07001794 uintptr_t PushHandle(mirror::Object* ref) OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001795
1796 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args, HandleScope* scope) {
1797 FillNativeCall::Reset(gpr_regs, fpr_regs, stack_args);
1798 handle_scope_ = scope;
1799 cur_entry_ = 0U;
1800 }
1801
Mathieu Chartier90443472015-07-16 20:32:27 -07001802 void ResetRemainingScopeSlots() SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001803 // Initialize padding entries.
1804 size_t expected_slots = handle_scope_->NumberOfReferences();
1805 while (cur_entry_ < expected_slots) {
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07001806 handle_scope_->GetMutableHandle(cur_entry_++).Assign(nullptr);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001807 }
1808 DCHECK_NE(cur_entry_, 0U);
1809 }
1810
1811 private:
1812 HandleScope* handle_scope_;
1813 size_t cur_entry_;
1814 };
1815
1816 HandleScope* handle_scope_;
1817 FillJniCall jni_call_;
1818 void* bottom_of_used_area_;
1819
1820 BuildNativeCallFrameStateMachine<FillJniCall> sm_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001821
1822 DISALLOW_COPY_AND_ASSIGN(BuildGenericJniFrameVisitor);
1823};
1824
Andreas Gampec200a4a2014-06-16 18:39:09 -07001825uintptr_t BuildGenericJniFrameVisitor::FillJniCall::PushHandle(mirror::Object* ref) {
1826 uintptr_t tmp;
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07001827 MutableHandle<mirror::Object> h = handle_scope_->GetMutableHandle(cur_entry_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001828 h.Assign(ref);
1829 tmp = reinterpret_cast<uintptr_t>(h.ToJObject());
1830 cur_entry_++;
1831 return tmp;
1832}
1833
Ian Rogers9758f792014-03-13 09:02:55 -07001834void BuildGenericJniFrameVisitor::Visit() {
1835 Primitive::Type type = GetParamPrimitiveType();
1836 switch (type) {
1837 case Primitive::kPrimLong: {
1838 jlong long_arg;
1839 if (IsSplitLongOrDouble()) {
1840 long_arg = ReadSplitLongParam();
1841 } else {
1842 long_arg = *reinterpret_cast<jlong*>(GetParamAddress());
1843 }
1844 sm_.AdvanceLong(long_arg);
1845 break;
1846 }
1847 case Primitive::kPrimDouble: {
1848 uint64_t double_arg;
1849 if (IsSplitLongOrDouble()) {
1850 // Read into union so that we don't case to a double.
1851 double_arg = ReadSplitLongParam();
1852 } else {
1853 double_arg = *reinterpret_cast<uint64_t*>(GetParamAddress());
1854 }
1855 sm_.AdvanceDouble(double_arg);
1856 break;
1857 }
1858 case Primitive::kPrimNot: {
1859 StackReference<mirror::Object>* stack_ref =
1860 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001861 sm_.AdvanceHandleScope(stack_ref->AsMirrorPtr());
Ian Rogers9758f792014-03-13 09:02:55 -07001862 break;
1863 }
1864 case Primitive::kPrimFloat:
1865 sm_.AdvanceFloat(*reinterpret_cast<float*>(GetParamAddress()));
1866 break;
1867 case Primitive::kPrimBoolean: // Fall-through.
1868 case Primitive::kPrimByte: // Fall-through.
1869 case Primitive::kPrimChar: // Fall-through.
1870 case Primitive::kPrimShort: // Fall-through.
1871 case Primitive::kPrimInt: // Fall-through.
1872 sm_.AdvanceInt(*reinterpret_cast<jint*>(GetParamAddress()));
1873 break;
1874 case Primitive::kPrimVoid:
1875 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -07001876 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -07001877 }
1878}
1879
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001880void BuildGenericJniFrameVisitor::FinalizeHandleScope(Thread* self) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001881 // Clear out rest of the scope.
1882 jni_call_.ResetRemainingScopeSlots();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001883 // Install HandleScope.
1884 self->PushHandleScope(handle_scope_);
Ian Rogers9758f792014-03-13 09:02:55 -07001885}
1886
Ian Rogers04c31d22014-07-07 21:44:06 -07001887#if defined(__arm__) || defined(__aarch64__)
Andreas Gampe90546832014-03-12 18:07:19 -07001888extern "C" void* artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07001889#else
1890extern "C" void* artFindNativeMethod(Thread* self);
1891#endif
Andreas Gampe90546832014-03-12 18:07:19 -07001892
Andreas Gampead615172014-04-04 16:20:13 -07001893uint64_t artQuickGenericJniEndJNIRef(Thread* self, uint32_t cookie, jobject l, jobject lock) {
1894 if (lock != nullptr) {
1895 return reinterpret_cast<uint64_t>(JniMethodEndWithReferenceSynchronized(l, cookie, lock, self));
1896 } else {
1897 return reinterpret_cast<uint64_t>(JniMethodEndWithReference(l, cookie, self));
1898 }
1899}
1900
1901void artQuickGenericJniEndJNINonRef(Thread* self, uint32_t cookie, jobject lock) {
1902 if (lock != nullptr) {
1903 JniMethodEndSynchronized(cookie, lock, self);
1904 } else {
1905 JniMethodEnd(cookie, self);
1906 }
1907}
1908
Andreas Gampec147b002014-03-06 18:11:06 -08001909/*
1910 * Initializes an alloca region assumed to be directly below sp for a native call:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001911 * 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 -08001912 * The final element on the stack is a pointer to the native code.
1913 *
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001914 * On entry, the stack has a standard callee-save frame above sp, and an alloca below it.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001915 * We need to fix this, as the handle scope needs to go into the callee-save frame.
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001916 *
Andreas Gampec147b002014-03-06 18:11:06 -08001917 * The return of this function denotes:
1918 * 1) How many bytes of the alloca can be released, if the value is non-negative.
1919 * 2) An error, if the value is negative.
1920 */
Mathieu Chartiere401d142015-04-22 13:56:20 -07001921extern "C" TwoWordReturn artQuickGenericJniTrampoline(Thread* self, ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -07001922 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001923 ArtMethod* called = *sp;
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001924 DCHECK(called->IsNative()) << PrettyMethod(called, true);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001925 uint32_t shorty_len = 0;
1926 const char* shorty = called->GetShorty(&shorty_len);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001927
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001928 // Run the visitor and update sp.
Ian Rogers59c07062014-10-10 13:03:39 -07001929 BuildGenericJniFrameVisitor visitor(self, called->IsStatic(), shorty, shorty_len, &sp);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001930 visitor.VisitArguments();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001931 visitor.FinalizeHandleScope(self);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001932
Andreas Gampec200a4a2014-06-16 18:39:09 -07001933 // Fix up managed-stack things in Thread.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001934 self->SetTopOfStack(sp);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001935
Ian Rogerse0dcd462014-03-08 15:21:04 -08001936 self->VerifyStack();
1937
Andreas Gampe90546832014-03-12 18:07:19 -07001938 // Start JNI, save the cookie.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001939 uint32_t cookie;
1940 if (called->IsSynchronized()) {
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001941 cookie = JniMethodStartSynchronized(visitor.GetFirstHandleScopeJObject(), self);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001942 if (self->IsExceptionPending()) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001943 self->PopHandleScope();
Andreas Gampec147b002014-03-06 18:11:06 -08001944 // A negative value denotes an error.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001945 return GetTwoWordFailureValue();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001946 }
1947 } else {
1948 cookie = JniMethodStart(self);
1949 }
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001950 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
Ian Rogerse0dcd462014-03-08 15:21:04 -08001951 *(sp32 - 1) = cookie;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001952
Andreas Gampe90546832014-03-12 18:07:19 -07001953 // Retrieve the stored native code.
Mathieu Chartier2d721012014-11-10 11:08:06 -08001954 void* nativeCode = called->GetEntryPointFromJni();
Andreas Gampe90546832014-03-12 18:07:19 -07001955
Andreas Gampe9a6a99a2014-03-14 07:52:20 -07001956 // There are two cases for the content of nativeCode:
1957 // 1) Pointer to the native function.
1958 // 2) Pointer to the trampoline for native code binding.
1959 // In the second case, we need to execute the binding and continue with the actual native function
1960 // pointer.
Andreas Gampe90546832014-03-12 18:07:19 -07001961 DCHECK(nativeCode != nullptr);
1962 if (nativeCode == GetJniDlsymLookupStub()) {
Ian Rogers04c31d22014-07-07 21:44:06 -07001963#if defined(__arm__) || defined(__aarch64__)
Andreas Gampe90546832014-03-12 18:07:19 -07001964 nativeCode = artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07001965#else
1966 nativeCode = artFindNativeMethod(self);
1967#endif
Andreas Gampe90546832014-03-12 18:07:19 -07001968
1969 if (nativeCode == nullptr) {
1970 DCHECK(self->IsExceptionPending()); // There should be an exception pending now.
Andreas Gampead615172014-04-04 16:20:13 -07001971
1972 // End JNI, as the assembly will move to deliver the exception.
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001973 jobject lock = called->IsSynchronized() ? visitor.GetFirstHandleScopeJObject() : nullptr;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001974 if (shorty[0] == 'L') {
Andreas Gampead615172014-04-04 16:20:13 -07001975 artQuickGenericJniEndJNIRef(self, cookie, nullptr, lock);
1976 } else {
1977 artQuickGenericJniEndJNINonRef(self, cookie, lock);
1978 }
1979
Andreas Gampec200a4a2014-06-16 18:39:09 -07001980 return GetTwoWordFailureValue();
Andreas Gampe90546832014-03-12 18:07:19 -07001981 }
1982 // Note that the native code pointer will be automatically set by artFindNativeMethod().
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001983 }
1984
Andreas Gampec200a4a2014-06-16 18:39:09 -07001985 // Return native code addr(lo) and bottom of alloca address(hi).
1986 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(visitor.GetBottomOfUsedArea()),
1987 reinterpret_cast<uintptr_t>(nativeCode));
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001988}
1989
1990/*
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001991 * Is called after the native JNI code. Responsible for cleanup (handle scope, saved state) and
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001992 * unlocking.
1993 */
Andreas Gampec200a4a2014-06-16 18:39:09 -07001994extern "C" uint64_t artQuickGenericJniEndTrampoline(Thread* self, jvalue result, uint64_t result_f)
Mathieu Chartier90443472015-07-16 20:32:27 -07001995 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001996 ArtMethod** sp = self->GetManagedStack()->GetTopQuickFrame();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001997 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001998 ArtMethod* called = *sp;
Ian Rogerse0dcd462014-03-08 15:21:04 -08001999 uint32_t cookie = *(sp32 - 1);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002000
Andreas Gampead615172014-04-04 16:20:13 -07002001 jobject lock = nullptr;
2002 if (called->IsSynchronized()) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002003 HandleScope* table = reinterpret_cast<HandleScope*>(reinterpret_cast<uint8_t*>(sp)
Mathieu Chartiere401d142015-04-22 13:56:20 -07002004 + sizeof(*sp));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002005 lock = table->GetHandle(0).ToJObject();
Andreas Gampead615172014-04-04 16:20:13 -07002006 }
2007
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002008 char return_shorty_char = called->GetShorty()[0];
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002009
2010 if (return_shorty_char == 'L') {
Andreas Gampead615172014-04-04 16:20:13 -07002011 return artQuickGenericJniEndJNIRef(self, cookie, result.l, lock);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002012 } else {
Andreas Gampead615172014-04-04 16:20:13 -07002013 artQuickGenericJniEndJNINonRef(self, cookie, lock);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002014
2015 switch (return_shorty_char) {
Nicolas Geoffray54accbc2014-08-13 03:40:45 +01002016 case 'F': {
2017 if (kRuntimeISA == kX86) {
2018 // Convert back the result to float.
Roland Levillainda4d79b2015-03-24 14:36:11 +00002019 double d = bit_cast<double, uint64_t>(result_f);
2020 return bit_cast<uint32_t, float>(static_cast<float>(d));
Nicolas Geoffray54accbc2014-08-13 03:40:45 +01002021 } else {
2022 return result_f;
2023 }
2024 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002025 case 'D':
2026 return result_f;
2027 case 'Z':
2028 return result.z;
2029 case 'B':
2030 return result.b;
2031 case 'C':
2032 return result.c;
2033 case 'S':
2034 return result.s;
2035 case 'I':
2036 return result.i;
2037 case 'J':
2038 return result.j;
2039 case 'V':
2040 return 0;
2041 default:
2042 LOG(FATAL) << "Unexpected return shorty character " << return_shorty_char;
2043 return 0;
2044 }
2045 }
Andreas Gampe2da88232014-02-27 12:26:20 -08002046}
2047
Andreas Gamped58342c2014-06-05 14:18:08 -07002048// We use TwoWordReturn to optimize scalar returns. We use the hi value for code, and the lo value
2049// for the method pointer.
Andreas Gampe51f76352014-05-21 08:28:48 -07002050//
Andreas Gamped58342c2014-06-05 14:18:08 -07002051// It is valid to use this, as at the usage points here (returns from C functions) we are assuming
Mathieu Chartier90443472015-07-16 20:32:27 -07002052// to hold the mutator lock (see SHARED_REQUIRES(Locks::mutator_lock_) annotations).
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002053
2054template<InvokeType type, bool access_check>
Mathieu Chartiere401d142015-04-22 13:56:20 -07002055static TwoWordReturn artInvokeCommon(uint32_t method_idx, mirror::Object* this_object, Thread* self,
2056 ArtMethod** sp) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002057 ScopedQuickEntrypointChecks sqec(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002058 DCHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs));
2059 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
2060 ArtMethod* method = FindMethodFast(method_idx, this_object, caller_method, access_check, type);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002061 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002062 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()->GetDexFile();
2063 uint32_t shorty_len;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002064 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(method_idx), &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002065 {
2066 // Remember the args in case a GC happens in FindMethodFromCode.
2067 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
2068 RememberForGcArgumentVisitor visitor(sp, type == kStatic, shorty, shorty_len, &soa);
2069 visitor.VisitArguments();
Andreas Gampe3a357142015-08-07 17:20:11 -07002070 method = FindMethodFromCode<type, access_check>(method_idx, &this_object, caller_method,
Mathieu Chartier0cd81352014-05-22 16:48:55 -07002071 self);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002072 visitor.FixupReferences();
2073 }
2074
Ian Rogerse0a02da2014-12-02 14:10:53 -08002075 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002076 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07002077 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002078 }
2079 }
2080 DCHECK(!self->IsExceptionPending());
2081 const void* code = method->GetEntryPointFromQuickCompiledCode();
2082
2083 // When we return, the caller will branch to this address, so it had better not be 0!
Ian Rogerse0a02da2014-12-02 14:10:53 -08002084 DCHECK(code != nullptr) << "Code was null in method: " << PrettyMethod(method)
Andreas Gampec200a4a2014-06-16 18:39:09 -07002085 << " location: "
2086 << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07002087
Andreas Gamped58342c2014-06-05 14:18:08 -07002088 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
2089 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002090}
2091
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01002092// Explicit artInvokeCommon template function declarations to please analysis tool.
2093#define EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(type, access_check) \
Mathieu Chartier90443472015-07-16 20:32:27 -07002094 template SHARED_REQUIRES(Locks::mutator_lock_) \
Mathieu Chartiere401d142015-04-22 13:56:20 -07002095 TwoWordReturn artInvokeCommon<type, access_check>( \
2096 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01002097
2098EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, false);
2099EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, true);
2100EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, false);
2101EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, true);
2102EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, false);
2103EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, true);
2104EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, false);
2105EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, true);
2106EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, false);
2107EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, true);
2108#undef EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL
2109
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002110// See comments in runtime_support_asm.S
Andreas Gampec200a4a2014-06-16 18:39:09 -07002111extern "C" TwoWordReturn artInvokeInterfaceTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002112 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -07002113 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002114 return artInvokeCommon<kInterface, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002115}
2116
Andreas Gampec200a4a2014-06-16 18:39:09 -07002117extern "C" TwoWordReturn artInvokeDirectTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002118 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -07002119 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002120 return artInvokeCommon<kDirect, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002121}
2122
Andreas Gampec200a4a2014-06-16 18:39:09 -07002123extern "C" TwoWordReturn artInvokeStaticTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002124 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -07002125 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002126 return artInvokeCommon<kStatic, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002127}
2128
Andreas Gampec200a4a2014-06-16 18:39:09 -07002129extern "C" TwoWordReturn artInvokeSuperTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002130 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -07002131 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002132 return artInvokeCommon<kSuper, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002133}
2134
Andreas Gampec200a4a2014-06-16 18:39:09 -07002135extern "C" TwoWordReturn artInvokeVirtualTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002136 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -07002137 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002138 return artInvokeCommon<kVirtual, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002139}
2140
2141// Determine target of interface dispatch. This object is known non-null.
Nicolas Geoffray8ea18d02015-05-26 16:29:08 +01002142extern "C" TwoWordReturn artInvokeInterfaceTrampoline(uint32_t dex_method_idx,
Andreas Gampe51f76352014-05-21 08:28:48 -07002143 mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -07002144 Thread* self, ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -07002145 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002146 ScopedQuickEntrypointChecks sqec(self);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002147 // The optimizing compiler currently does not inline methods that have an interface
2148 // invocation. We use the outer method directly to avoid fetching a stack map, which is
2149 // more expensive.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002150 ArtMethod* caller_method = QuickArgumentVisitor::GetOuterMethod(sp);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002151 DCHECK_EQ(caller_method, QuickArgumentVisitor::GetCallingMethod(sp));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002152 ArtMethod* interface_method = caller_method->GetDexCacheResolvedMethod(
2153 dex_method_idx, sizeof(void*));
2154 DCHECK(interface_method != nullptr) << dex_method_idx << " " << PrettyMethod(caller_method);
2155 ArtMethod* method;
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002156 if (LIKELY(interface_method->GetDexMethodIndex() != DexFile::kDexNoIndex)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002157 method = this_object->GetClass()->FindVirtualMethodForInterface(
2158 interface_method, sizeof(void*));
Ian Rogerse0a02da2014-12-02 14:10:53 -08002159 if (UNLIKELY(method == nullptr)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002160 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(
2161 interface_method, this_object, caller_method);
Andreas Gamped58342c2014-06-05 14:18:08 -07002162 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002163 }
2164 } else {
Mathieu Chartier4edd8472015-06-01 10:47:36 -07002165 DCHECK_EQ(interface_method, Runtime::Current()->GetResolutionMethod());
Nicolas Geoffray8ea18d02015-05-26 16:29:08 +01002166 if (kIsDebugBuild) {
2167 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
2168 const DexFile::CodeItem* code = caller_method->GetCodeItem();
2169 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
2170 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
2171 Instruction::Code instr_code = instr->Opcode();
2172 CHECK(instr_code == Instruction::INVOKE_INTERFACE ||
2173 instr_code == Instruction::INVOKE_INTERFACE_RANGE)
2174 << "Unexpected call into interface trampoline: " << instr->DumpString(nullptr);
2175 if (instr_code == Instruction::INVOKE_INTERFACE) {
2176 CHECK_EQ(dex_method_idx, instr->VRegB_35c());
2177 } else {
2178 CHECK_EQ(instr_code, Instruction::INVOKE_INTERFACE_RANGE);
2179 CHECK_EQ(dex_method_idx, instr->VRegB_3rc());
2180 }
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002181 }
2182
Andreas Gampec200a4a2014-06-16 18:39:09 -07002183 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()
2184 ->GetDexFile();
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002185 uint32_t shorty_len;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002186 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(dex_method_idx),
2187 &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002188 {
2189 // Remember the args in case a GC happens in FindMethodFromCode.
2190 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
2191 RememberForGcArgumentVisitor visitor(sp, false, shorty, shorty_len, &soa);
2192 visitor.VisitArguments();
Andreas Gampe3a357142015-08-07 17:20:11 -07002193 method = FindMethodFromCode<kInterface, false>(dex_method_idx, &this_object, caller_method,
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002194 self);
2195 visitor.FixupReferences();
2196 }
2197
2198 if (UNLIKELY(method == nullptr)) {
2199 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07002200 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002201 }
2202 }
2203 const void* code = method->GetEntryPointFromQuickCompiledCode();
2204
2205 // When we return, the caller will branch to this address, so it had better not be 0!
Ian Rogerse0a02da2014-12-02 14:10:53 -08002206 DCHECK(code != nullptr) << "Code was null in method: " << PrettyMethod(method)
Andreas Gampec200a4a2014-06-16 18:39:09 -07002207 << " location: " << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07002208
Andreas Gamped58342c2014-06-05 14:18:08 -07002209 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
2210 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002211}
2212
Ian Rogers848871b2013-08-05 10:56:33 -07002213} // namespace art