blob: aa35ec1ca23482d62c77b4e8f6c4ae4beffe78f0 [file] [log] [blame]
Ian Rogers848871b2013-08-05 10:56:33 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Mathieu Chartiere401d142015-04-22 13:56:20 -070017#include "art_method-inl.h"
Ian Rogers848871b2013-08-05 10:56:33 -070018#include "callee_save_frame.h"
Dragos Sbirleabd136a22013-08-13 18:07:04 -070019#include "common_throws.h"
Ian Rogers848871b2013-08-05 10:56:33 -070020#include "dex_file-inl.h"
21#include "dex_instruction-inl.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070022#include "entrypoints/entrypoint_utils-inl.h"
Ian Rogers6f3dbba2014-10-14 17:41:57 -070023#include "entrypoints/runtime_asm_entrypoints.h"
Ian Rogers83883d72013-10-21 21:07:24 -070024#include "gc/accounting/card_table-inl.h"
Ian Rogers848871b2013-08-05 10:56:33 -070025#include "interpreter/interpreter.h"
Ian Rogerse0a02da2014-12-02 14:10:53 -080026#include "method_reference.h"
Ian Rogers848871b2013-08-05 10:56:33 -070027#include "mirror/class-inl.h"
Mathieu Chartier5f3ded42014-04-03 15:25:30 -070028#include "mirror/dex_cache-inl.h"
Mathieu Chartierfc58af42015-04-16 18:00:39 -070029#include "mirror/method.h"
Ian Rogers848871b2013-08-05 10:56:33 -070030#include "mirror/object-inl.h"
31#include "mirror/object_array-inl.h"
Ian Rogers848871b2013-08-05 10:56:33 -070032#include "runtime.h"
Ian Rogers53b8b092014-03-13 23:45:53 -070033#include "scoped_thread_state_change.h"
Daniel Mihalyieb076692014-08-22 17:33:31 +020034#include "debugger.h"
Ian Rogers848871b2013-08-05 10:56:33 -070035
36namespace art {
37
38// Visits the arguments as saved to the stack by a Runtime::kRefAndArgs callee save frame.
39class QuickArgumentVisitor {
Ian Rogers936b37f2014-02-14 00:52:24 -080040 // Number of bytes for each out register in the caller method's frame.
41 static constexpr size_t kBytesStackArgLocation = 4;
Alexei Zavjalov41c507a2014-05-15 16:02:46 +070042 // Frame size in bytes of a callee-save frame for RefsAndArgs.
43 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize =
44 GetCalleeSaveFrameSize(kRuntimeISA, Runtime::kRefsAndArgs);
Ian Rogers848871b2013-08-05 10:56:33 -070045#if defined(__arm__)
46 // The callee save frame is pointed to by SP.
47 // | argN | |
48 // | ... | |
49 // | arg4 | |
50 // | arg3 spill | | Caller's frame
51 // | arg2 spill | |
52 // | arg1 spill | |
53 // | Method* | ---
54 // | LR |
Zheng Xu5667fdb2014-10-23 18:29:55 +080055 // | ... | 4x6 bytes callee saves
56 // | R3 |
57 // | R2 |
58 // | R1 |
59 // | S15 |
60 // | : |
61 // | S0 |
62 // | | 4x2 bytes padding
Ian Rogers848871b2013-08-05 10:56:33 -070063 // | Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -050064 static constexpr bool kSplitPairAcrossRegisterAndStack = kArm32QuickCodeUseSoftFloat;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +000065 static constexpr bool kAlignPairRegister = !kArm32QuickCodeUseSoftFloat;
Zheng Xu5667fdb2014-10-23 18:29:55 +080066 static constexpr bool kQuickSoftFloatAbi = kArm32QuickCodeUseSoftFloat;
67 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = !kArm32QuickCodeUseSoftFloat;
Goran Jakovljevicff734982015-08-24 12:58:55 +000068 static constexpr bool kQuickSkipOddFpRegisters = false;
Zheng Xu5667fdb2014-10-23 18:29:55 +080069 static constexpr size_t kNumQuickGprArgs = 3;
70 static constexpr size_t kNumQuickFprArgs = kArm32QuickCodeUseSoftFloat ? 0 : 16;
Andreas Gampe1a5c4062015-01-15 12:10:47 -080071 static constexpr bool kGprFprLockstep = false;
Zheng Xub551fdc2014-07-25 11:49:42 +080072 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset =
73 arm::ArmCalleeSaveFpr1Offset(Runtime::kRefsAndArgs); // Offset of first FPR arg.
74 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset =
75 arm::ArmCalleeSaveGpr1Offset(Runtime::kRefsAndArgs); // Offset of first GPR arg.
76 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset =
77 arm::ArmCalleeSaveLrOffset(Runtime::kRefsAndArgs); // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -080078 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +000079 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -080080 }
Stuart Monteithb95a5342014-03-12 13:32:32 +000081#elif defined(__aarch64__)
82 // The callee save frame is pointed to by SP.
83 // | argN | |
84 // | ... | |
85 // | arg4 | |
86 // | arg3 spill | | Caller's frame
87 // | arg2 spill | |
88 // | arg1 spill | |
89 // | Method* | ---
90 // | LR |
Zheng Xub551fdc2014-07-25 11:49:42 +080091 // | X29 |
Stuart Monteithb95a5342014-03-12 13:32:32 +000092 // | : |
Serban Constantinescu9bd88b02015-04-22 16:24:46 +010093 // | X20 |
Stuart Monteithb95a5342014-03-12 13:32:32 +000094 // | X7 |
95 // | : |
96 // | X1 |
Zheng Xub551fdc2014-07-25 11:49:42 +080097 // | D7 |
Stuart Monteithb95a5342014-03-12 13:32:32 +000098 // | : |
99 // | D0 |
100 // | | padding
101 // | Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500102 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000103 static constexpr bool kAlignPairRegister = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000104 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800105 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000106 static constexpr bool kQuickSkipOddFpRegisters = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000107 static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs.
108 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800109 static constexpr bool kGprFprLockstep = false;
Zheng Xub551fdc2014-07-25 11:49:42 +0800110 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset =
111 arm64::Arm64CalleeSaveFpr1Offset(Runtime::kRefsAndArgs); // Offset of first FPR arg.
112 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset =
113 arm64::Arm64CalleeSaveGpr1Offset(Runtime::kRefsAndArgs); // Offset of first GPR arg.
114 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset =
115 arm64::Arm64CalleeSaveLrOffset(Runtime::kRefsAndArgs); // Offset of return address.
Stuart Monteithb95a5342014-03-12 13:32:32 +0000116 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000117 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Stuart Monteithb95a5342014-03-12 13:32:32 +0000118 }
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800119#elif defined(__mips__) && !defined(__LP64__)
Ian Rogers848871b2013-08-05 10:56:33 -0700120 // The callee save frame is pointed to by SP.
121 // | argN | |
122 // | ... | |
123 // | arg4 | |
124 // | arg3 spill | | Caller's frame
125 // | arg2 spill | |
126 // | arg1 spill | |
127 // | Method* | ---
128 // | RA |
129 // | ... | callee saves
130 // | A3 | arg3
131 // | A2 | arg2
132 // | A1 | arg1
Goran Jakovljevicff734982015-08-24 12:58:55 +0000133 // | F15 |
134 // | F14 | f_arg1
135 // | F13 |
136 // | F12 | f_arg0
137 // | | padding
Ian Rogers848871b2013-08-05 10:56:33 -0700138 // | A0/Method* | <- sp
Goran Jakovljevicff734982015-08-24 12:58:55 +0000139 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
140 static constexpr bool kAlignPairRegister = true;
141 static constexpr bool kQuickSoftFloatAbi = false;
Zheng Xu5667fdb2014-10-23 18:29:55 +0800142 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000143 static constexpr bool kQuickSkipOddFpRegisters = true;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800144 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs.
Goran Jakovljevicff734982015-08-24 12:58:55 +0000145 static constexpr size_t kNumQuickFprArgs = 4; // 2 arguments passed in FPRs. Floats can be passed
146 // only in even numbered registers and each double
147 // occupies two registers.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800148 static constexpr bool kGprFprLockstep = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000149 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 16; // Offset of first FPR arg.
150 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 32; // Offset of first GPR arg.
151 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 76; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800152 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000153 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800154 }
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800155#elif defined(__mips__) && defined(__LP64__)
156 // The callee save frame is pointed to by SP.
157 // | argN | |
158 // | ... | |
159 // | arg4 | |
160 // | arg3 spill | | Caller's frame
161 // | arg2 spill | |
162 // | arg1 spill | |
163 // | Method* | ---
164 // | RA |
165 // | ... | callee saves
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800166 // | A7 | arg7
167 // | A6 | arg6
168 // | A5 | arg5
169 // | A4 | arg4
170 // | A3 | arg3
171 // | A2 | arg2
172 // | A1 | arg1
Goran Jakovljevicff734982015-08-24 12:58:55 +0000173 // | F19 | f_arg7
174 // | F18 | f_arg6
175 // | F17 | f_arg5
176 // | F16 | f_arg4
177 // | F15 | f_arg3
178 // | F14 | f_arg2
179 // | F13 | f_arg1
180 // | F12 | f_arg0
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800181 // | | padding
182 // | A0/Method* | <- sp
183 // NOTE: for Mip64, when A0 is skipped, F0 is also skipped.
Douglas Leungd18e0832015-02-09 15:22:26 -0800184 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800185 static constexpr bool kAlignPairRegister = false;
186 static constexpr bool kQuickSoftFloatAbi = false;
187 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000188 static constexpr bool kQuickSkipOddFpRegisters = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800189 static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs.
190 static constexpr size_t kNumQuickFprArgs = 7; // 7 arguments passed in FPRs.
191 static constexpr bool kGprFprLockstep = true;
192
193 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 24; // Offset of first FPR arg (F1).
194 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 80; // Offset of first GPR arg (A1).
195 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 200; // Offset of return address.
196 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
197 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
198 }
Ian Rogers848871b2013-08-05 10:56:33 -0700199#elif defined(__i386__)
200 // The callee save frame is pointed to by SP.
201 // | argN | |
202 // | ... | |
203 // | arg4 | |
204 // | arg3 spill | | Caller's frame
205 // | arg2 spill | |
206 // | arg1 spill | |
207 // | Method* | ---
208 // | Return |
209 // | EBP,ESI,EDI | callee saves
210 // | EBX | arg3
211 // | EDX | arg2
212 // | ECX | arg1
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000213 // | XMM3 | float arg 4
214 // | XMM2 | float arg 3
215 // | XMM1 | float arg 2
216 // | XMM0 | float arg 1
Ian Rogers848871b2013-08-05 10:56:33 -0700217 // | EAX/Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500218 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000219 static constexpr bool kAlignPairRegister = false;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000220 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800221 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000222 static constexpr bool kQuickSkipOddFpRegisters = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800223 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs.
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000224 static constexpr size_t kNumQuickFprArgs = 4; // 4 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800225 static constexpr bool kGprFprLockstep = false;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000226 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 4; // Offset of first FPR arg.
227 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 4 + 4*8; // Offset of first GPR arg.
228 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 28 + 4*8; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800229 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000230 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800231 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800232#elif defined(__x86_64__)
Ian Rogers936b37f2014-02-14 00:52:24 -0800233 // The callee save frame is pointed to by SP.
234 // | argN | |
235 // | ... | |
236 // | reg. arg spills | | Caller's frame
237 // | Method* | ---
238 // | Return |
239 // | R15 | callee save
240 // | R14 | callee save
241 // | R13 | callee save
242 // | R12 | callee save
243 // | R9 | arg5
244 // | R8 | arg4
245 // | RSI/R6 | arg1
246 // | RBP/R5 | callee save
247 // | RBX/R3 | callee save
248 // | RDX/R2 | arg2
249 // | RCX/R1 | arg3
250 // | XMM7 | float arg 8
251 // | XMM6 | float arg 7
252 // | XMM5 | float arg 6
253 // | XMM4 | float arg 5
254 // | XMM3 | float arg 4
255 // | XMM2 | float arg 3
256 // | XMM1 | float arg 2
257 // | XMM0 | float arg 1
258 // | Padding |
259 // | RDI/Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500260 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000261 static constexpr bool kAlignPairRegister = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800262 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800263 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000264 static constexpr bool kQuickSkipOddFpRegisters = false;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700265 static constexpr size_t kNumQuickGprArgs = 5; // 5 arguments passed in GPRs.
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700266 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800267 static constexpr bool kGprFprLockstep = false;
Ian Rogers936b37f2014-02-14 00:52:24 -0800268 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 16; // Offset of first FPR arg.
Serguei Katkovc3801912014-07-08 17:21:53 +0700269 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 80 + 4*8; // Offset of first GPR arg.
270 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 168 + 4*8; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800271 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
272 switch (gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000273 case 0: return (4 * GetBytesPerGprSpillLocation(kRuntimeISA));
274 case 1: return (1 * GetBytesPerGprSpillLocation(kRuntimeISA));
275 case 2: return (0 * GetBytesPerGprSpillLocation(kRuntimeISA));
276 case 3: return (5 * GetBytesPerGprSpillLocation(kRuntimeISA));
277 case 4: return (6 * GetBytesPerGprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800278 default:
Andreas Gampec200a4a2014-06-16 18:39:09 -0700279 LOG(FATAL) << "Unexpected GPR index: " << gpr_index;
280 return 0;
Ian Rogers936b37f2014-02-14 00:52:24 -0800281 }
282 }
Ian Rogers848871b2013-08-05 10:56:33 -0700283#else
284#error "Unsupported architecture"
Ian Rogers848871b2013-08-05 10:56:33 -0700285#endif
286
Ian Rogers936b37f2014-02-14 00:52:24 -0800287 public:
Sebastien Hertza836bc92014-11-25 16:30:53 +0100288 // Special handling for proxy methods. Proxy methods are instance methods so the
289 // 'this' object is the 1st argument. They also have the same frame layout as the
290 // kRefAndArgs runtime method. Since 'this' is a reference, it is located in the
291 // 1st GPR.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700292 static mirror::Object* GetProxyThisObject(ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -0700293 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700294 CHECK((*sp)->IsProxyMethod());
295 CHECK_EQ(kQuickCalleeSaveFrame_RefAndArgs_FrameSize, (*sp)->GetFrameSizeInBytes());
Sebastien Hertza836bc92014-11-25 16:30:53 +0100296 CHECK_GT(kNumQuickGprArgs, 0u);
297 constexpr uint32_t kThisGprIndex = 0u; // 'this' is in the 1st GPR.
298 size_t this_arg_offset = kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset +
299 GprIndexToGprOffset(kThisGprIndex);
300 uint8_t* this_arg_address = reinterpret_cast<uint8_t*>(sp) + this_arg_offset;
301 return reinterpret_cast<StackReference<mirror::Object>*>(this_arg_address)->AsMirrorPtr();
302 }
303
Mathieu Chartier90443472015-07-16 20:32:27 -0700304 static ArtMethod* GetCallingMethod(ArtMethod** sp) SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700305 DCHECK((*sp)->IsCalleeSaveMethod());
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +0100306 return GetCalleeSaveMethodCaller(sp, Runtime::kRefsAndArgs);
307 }
308
Mathieu Chartier90443472015-07-16 20:32:27 -0700309 static ArtMethod* GetOuterMethod(ArtMethod** sp) SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700310 DCHECK((*sp)->IsCalleeSaveMethod());
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100311 uint8_t* previous_sp =
312 reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700313 return *reinterpret_cast<ArtMethod**>(previous_sp);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100314 }
315
Mathieu Chartier90443472015-07-16 20:32:27 -0700316 static uint32_t GetCallingDexPc(ArtMethod** sp) SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700317 DCHECK((*sp)->IsCalleeSaveMethod());
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100318 const size_t callee_frame_size = GetCalleeSaveFrameSize(kRuntimeISA, Runtime::kRefsAndArgs);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700319 ArtMethod** caller_sp = reinterpret_cast<ArtMethod**>(
320 reinterpret_cast<uintptr_t>(sp) + callee_frame_size);
321 ArtMethod* outer_method = *caller_sp;
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100322 uintptr_t outer_pc = QuickArgumentVisitor::GetCallingPc(sp);
323 uintptr_t outer_pc_offset = outer_method->NativeQuickPcOffset(outer_pc);
324
325 if (outer_method->IsOptimized(sizeof(void*))) {
326 CodeInfo code_info = outer_method->GetOptimizedCodeInfo();
David Brazdilf677ebf2015-05-29 16:29:43 +0100327 StackMapEncoding encoding = code_info.ExtractEncoding();
328 StackMap stack_map = code_info.GetStackMapForNativePcOffset(outer_pc_offset, encoding);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100329 DCHECK(stack_map.IsValid());
David Brazdilf677ebf2015-05-29 16:29:43 +0100330 if (stack_map.HasInlineInfo(encoding)) {
331 InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map, encoding);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100332 return inline_info.GetDexPcAtDepth(inline_info.GetDepth() - 1);
333 } else {
David Brazdilf677ebf2015-05-29 16:29:43 +0100334 return stack_map.GetDexPc(encoding);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100335 }
336 } else {
337 return outer_method->ToDexPc(outer_pc);
338 }
Ian Rogers848871b2013-08-05 10:56:33 -0700339 }
340
Ian Rogers936b37f2014-02-14 00:52:24 -0800341 // For the given quick ref and args quick frame, return the caller's PC.
Mathieu Chartier90443472015-07-16 20:32:27 -0700342 static uintptr_t GetCallingPc(ArtMethod** sp) SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700343 DCHECK((*sp)->IsCalleeSaveMethod());
Ian Rogers13735952014-10-08 12:43:28 -0700344 uint8_t* lr = reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_LrOffset;
Ian Rogers848871b2013-08-05 10:56:33 -0700345 return *reinterpret_cast<uintptr_t*>(lr);
346 }
347
Mathieu Chartiere401d142015-04-22 13:56:20 -0700348 QuickArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty,
Mathieu Chartier90443472015-07-16 20:32:27 -0700349 uint32_t shorty_len) SHARED_REQUIRES(Locks::mutator_lock_) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700350 is_static_(is_static), shorty_(shorty), shorty_len_(shorty_len),
Ian Rogers13735952014-10-08 12:43:28 -0700351 gpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset),
352 fpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset),
353 stack_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize
Mathieu Chartiere401d142015-04-22 13:56:20 -0700354 + sizeof(ArtMethod*)), // Skip ArtMethod*.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800355 gpr_index_(0), fpr_index_(0), fpr_double_index_(0), stack_index_(0),
356 cur_type_(Primitive::kPrimVoid), is_split_long_or_double_(false) {
Andreas Gampe575e78c2014-11-03 23:41:03 -0800357 static_assert(kQuickSoftFloatAbi == (kNumQuickFprArgs == 0),
358 "Number of Quick FPR arguments unexpected");
359 static_assert(!(kQuickSoftFloatAbi && kQuickDoubleRegAlignedFloatBackFilled),
360 "Double alignment unexpected");
Zheng Xu5667fdb2014-10-23 18:29:55 +0800361 // For register alignment, we want to assume that counters(fpr_double_index_) are even if the
362 // next register is even.
Andreas Gampe575e78c2014-11-03 23:41:03 -0800363 static_assert(!kQuickDoubleRegAlignedFloatBackFilled || kNumQuickFprArgs % 2 == 0,
364 "Number of Quick FPR arguments not even");
Mathieu Chartiere401d142015-04-22 13:56:20 -0700365 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), sizeof(void*));
Zheng Xu5667fdb2014-10-23 18:29:55 +0800366 }
Ian Rogers848871b2013-08-05 10:56:33 -0700367
368 virtual ~QuickArgumentVisitor() {}
369
370 virtual void Visit() = 0;
371
Ian Rogers936b37f2014-02-14 00:52:24 -0800372 Primitive::Type GetParamPrimitiveType() const {
373 return cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700374 }
375
Ian Rogers13735952014-10-08 12:43:28 -0700376 uint8_t* GetParamAddress() const {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800377 if (!kQuickSoftFloatAbi) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800378 Primitive::Type type = GetParamPrimitiveType();
379 if (UNLIKELY((type == Primitive::kPrimDouble) || (type == Primitive::kPrimFloat))) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800380 if (type == Primitive::kPrimDouble && kQuickDoubleRegAlignedFloatBackFilled) {
381 if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) {
382 return fpr_args_ + (fpr_double_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
383 }
384 } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000385 return fpr_args_ + (fpr_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800386 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700387 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers936b37f2014-02-14 00:52:24 -0800388 }
389 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800390 if (gpr_index_ < kNumQuickGprArgs) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800391 return gpr_args_ + GprIndexToGprOffset(gpr_index_);
392 }
393 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700394 }
395
396 bool IsSplitLongOrDouble() const {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700397 if ((GetBytesPerGprSpillLocation(kRuntimeISA) == 4) ||
398 (GetBytesPerFprSpillLocation(kRuntimeISA) == 4)) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800399 return is_split_long_or_double_;
400 } else {
401 return false; // An optimization for when GPR and FPRs are 64bit.
402 }
Ian Rogers848871b2013-08-05 10:56:33 -0700403 }
404
Ian Rogers936b37f2014-02-14 00:52:24 -0800405 bool IsParamAReference() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700406 return GetParamPrimitiveType() == Primitive::kPrimNot;
407 }
408
Ian Rogers936b37f2014-02-14 00:52:24 -0800409 bool IsParamALongOrDouble() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700410 Primitive::Type type = GetParamPrimitiveType();
411 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
412 }
413
414 uint64_t ReadSplitLongParam() const {
Nicolas Geoffray425f2392015-01-08 14:52:29 +0000415 // The splitted long is always available through the stack.
416 return *reinterpret_cast<uint64_t*>(stack_args_
417 + stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700418 }
419
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800420 void IncGprIndex() {
421 gpr_index_++;
422 if (kGprFprLockstep) {
423 fpr_index_++;
424 }
425 }
426
427 void IncFprIndex() {
428 fpr_index_++;
429 if (kGprFprLockstep) {
430 gpr_index_++;
431 }
432 }
433
Mathieu Chartier90443472015-07-16 20:32:27 -0700434 void VisitArguments() SHARED_REQUIRES(Locks::mutator_lock_) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800435 // (a) 'stack_args_' should point to the first method's argument
436 // (b) whatever the argument type it is, the 'stack_index_' should
437 // be moved forward along with every visiting.
Ian Rogers936b37f2014-02-14 00:52:24 -0800438 gpr_index_ = 0;
439 fpr_index_ = 0;
Zheng Xu5667fdb2014-10-23 18:29:55 +0800440 if (kQuickDoubleRegAlignedFloatBackFilled) {
441 fpr_double_index_ = 0;
442 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800443 stack_index_ = 0;
444 if (!is_static_) { // Handle this.
445 cur_type_ = Primitive::kPrimNot;
446 is_split_long_or_double_ = false;
Ian Rogers848871b2013-08-05 10:56:33 -0700447 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800448 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800449 if (kNumQuickGprArgs > 0) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800450 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800451 }
Ian Rogers848871b2013-08-05 10:56:33 -0700452 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800453 for (uint32_t shorty_index = 1; shorty_index < shorty_len_; ++shorty_index) {
454 cur_type_ = Primitive::GetType(shorty_[shorty_index]);
455 switch (cur_type_) {
456 case Primitive::kPrimNot:
457 case Primitive::kPrimBoolean:
458 case Primitive::kPrimByte:
459 case Primitive::kPrimChar:
460 case Primitive::kPrimShort:
461 case Primitive::kPrimInt:
462 is_split_long_or_double_ = false;
463 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800464 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800465 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800466 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800467 }
468 break;
469 case Primitive::kPrimFloat:
470 is_split_long_or_double_ = false;
471 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800472 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800473 if (kQuickSoftFloatAbi) {
474 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800475 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800476 }
477 } else {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800478 if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800479 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800480 if (kQuickDoubleRegAlignedFloatBackFilled) {
481 // Double should not overlap with float.
482 // For example, if fpr_index_ = 3, fpr_double_index_ should be at least 4.
483 fpr_double_index_ = std::max(fpr_double_index_, RoundUp(fpr_index_, 2));
484 // Float should not overlap with double.
485 if (fpr_index_ % 2 == 0) {
486 fpr_index_ = std::max(fpr_double_index_, fpr_index_);
487 }
Goran Jakovljevicff734982015-08-24 12:58:55 +0000488 } else if (kQuickSkipOddFpRegisters) {
489 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800490 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800491 }
492 }
493 break;
494 case Primitive::kPrimDouble:
495 case Primitive::kPrimLong:
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800496 if (kQuickSoftFloatAbi || (cur_type_ == Primitive::kPrimLong)) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000497 if (cur_type_ == Primitive::kPrimLong && kAlignPairRegister && gpr_index_ == 0) {
Goran Jakovljevicff734982015-08-24 12:58:55 +0000498 // Currently, this is only for ARM and MIPS, where the first available parameter
499 // register is R1 (on ARM) or A1 (on MIPS). So we skip it, and use R2 (on ARM) or
500 // A2 (on MIPS) instead.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800501 IncGprIndex();
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000502 }
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000503 is_split_long_or_double_ = (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) &&
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800504 ((gpr_index_ + 1) == kNumQuickGprArgs);
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500505 if (!kSplitPairAcrossRegisterAndStack && is_split_long_or_double_) {
506 // We don't want to split this. Pass over this register.
507 gpr_index_++;
508 is_split_long_or_double_ = false;
509 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800510 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800511 if (kBytesStackArgLocation == 4) {
512 stack_index_+= 2;
513 } else {
514 CHECK_EQ(kBytesStackArgLocation, 8U);
515 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800516 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700517 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800518 IncGprIndex();
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000519 if (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700520 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800521 IncGprIndex();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700522 }
523 }
524 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800525 } else {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000526 is_split_long_or_double_ = (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) &&
Zheng Xu5667fdb2014-10-23 18:29:55 +0800527 ((fpr_index_ + 1) == kNumQuickFprArgs) && !kQuickDoubleRegAlignedFloatBackFilled;
Ian Rogers936b37f2014-02-14 00:52:24 -0800528 Visit();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700529 if (kBytesStackArgLocation == 4) {
530 stack_index_+= 2;
Ian Rogers936b37f2014-02-14 00:52:24 -0800531 } else {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700532 CHECK_EQ(kBytesStackArgLocation, 8U);
533 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800534 }
Zheng Xu5667fdb2014-10-23 18:29:55 +0800535 if (kQuickDoubleRegAlignedFloatBackFilled) {
536 if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) {
537 fpr_double_index_ += 2;
538 // Float should not overlap with double.
539 if (fpr_index_ % 2 == 0) {
540 fpr_index_ = std::max(fpr_double_index_, fpr_index_);
541 }
542 }
543 } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800544 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800545 if (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) {
546 if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800547 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800548 }
549 }
550 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800551 }
552 break;
553 default:
554 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty_;
555 }
Ian Rogers848871b2013-08-05 10:56:33 -0700556 }
557 }
558
Andreas Gampec200a4a2014-06-16 18:39:09 -0700559 protected:
Ian Rogers848871b2013-08-05 10:56:33 -0700560 const bool is_static_;
561 const char* const shorty_;
562 const uint32_t shorty_len_;
Andreas Gampec200a4a2014-06-16 18:39:09 -0700563
564 private:
Ian Rogers13735952014-10-08 12:43:28 -0700565 uint8_t* const gpr_args_; // Address of GPR arguments in callee save frame.
566 uint8_t* const fpr_args_; // Address of FPR arguments in callee save frame.
567 uint8_t* const stack_args_; // Address of stack arguments in caller's frame.
Ian Rogers936b37f2014-02-14 00:52:24 -0800568 uint32_t gpr_index_; // Index into spilled GPRs.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800569 // Index into spilled FPRs.
570 // In case kQuickDoubleRegAlignedFloatBackFilled, it may index a hole while fpr_double_index_
571 // holds a higher register number.
572 uint32_t fpr_index_;
573 // Index into spilled FPRs for aligned double.
574 // Only used when kQuickDoubleRegAlignedFloatBackFilled. Next available double register indexed in
575 // terms of singles, may be behind fpr_index.
576 uint32_t fpr_double_index_;
Ian Rogers936b37f2014-02-14 00:52:24 -0800577 uint32_t stack_index_; // Index into arguments on the stack.
578 // The current type of argument during VisitArguments.
579 Primitive::Type cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700580 // Does a 64bit parameter straddle the register and stack arguments?
581 bool is_split_long_or_double_;
582};
583
Sebastien Hertza836bc92014-11-25 16:30:53 +0100584// Returns the 'this' object of a proxy method. This function is only used by StackVisitor. It
585// allows to use the QuickArgumentVisitor constants without moving all the code in its own module.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700586extern "C" mirror::Object* artQuickGetProxyThisObject(ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -0700587 SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertza836bc92014-11-25 16:30:53 +0100588 return QuickArgumentVisitor::GetProxyThisObject(sp);
589}
590
Ian Rogers848871b2013-08-05 10:56:33 -0700591// Visits arguments on the stack placing them into the shadow frame.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800592class BuildQuickShadowFrameVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700593 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700594 BuildQuickShadowFrameVisitor(ArtMethod** sp, bool is_static, const char* shorty,
595 uint32_t shorty_len, ShadowFrame* sf, size_t first_arg_reg) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700596 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), sf_(sf), cur_reg_(first_arg_reg) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700597
Mathieu Chartier90443472015-07-16 20:32:27 -0700598 void Visit() SHARED_REQUIRES(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700599
600 private:
Ian Rogers936b37f2014-02-14 00:52:24 -0800601 ShadowFrame* const sf_;
602 uint32_t cur_reg_;
Ian Rogers848871b2013-08-05 10:56:33 -0700603
Dragos Sbirleabd136a22013-08-13 18:07:04 -0700604 DISALLOW_COPY_AND_ASSIGN(BuildQuickShadowFrameVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700605};
606
Andreas Gampec200a4a2014-06-16 18:39:09 -0700607void BuildQuickShadowFrameVisitor::Visit() {
Ian Rogers9758f792014-03-13 09:02:55 -0700608 Primitive::Type type = GetParamPrimitiveType();
609 switch (type) {
610 case Primitive::kPrimLong: // Fall-through.
611 case Primitive::kPrimDouble:
612 if (IsSplitLongOrDouble()) {
613 sf_->SetVRegLong(cur_reg_, ReadSplitLongParam());
614 } else {
615 sf_->SetVRegLong(cur_reg_, *reinterpret_cast<jlong*>(GetParamAddress()));
616 }
617 ++cur_reg_;
618 break;
619 case Primitive::kPrimNot: {
620 StackReference<mirror::Object>* stack_ref =
621 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
622 sf_->SetVRegReference(cur_reg_, stack_ref->AsMirrorPtr());
623 }
624 break;
625 case Primitive::kPrimBoolean: // Fall-through.
626 case Primitive::kPrimByte: // Fall-through.
627 case Primitive::kPrimChar: // Fall-through.
628 case Primitive::kPrimShort: // Fall-through.
629 case Primitive::kPrimInt: // Fall-through.
630 case Primitive::kPrimFloat:
631 sf_->SetVReg(cur_reg_, *reinterpret_cast<jint*>(GetParamAddress()));
632 break;
633 case Primitive::kPrimVoid:
634 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700635 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -0700636 }
637 ++cur_reg_;
638}
639
Mathieu Chartiere401d142015-04-22 13:56:20 -0700640extern "C" uint64_t artQuickToInterpreterBridge(ArtMethod* method, Thread* self, ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -0700641 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers848871b2013-08-05 10:56:33 -0700642 // Ensure we don't get thread suspension until the object arguments are safely in the shadow
643 // frame.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700644 ScopedQuickEntrypointChecks sqec(self);
Ian Rogers848871b2013-08-05 10:56:33 -0700645
646 if (method->IsAbstract()) {
647 ThrowAbstractMethodError(method);
648 return 0;
649 } else {
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800650 DCHECK(!method->IsNative()) << PrettyMethod(method);
Andreas Gampec200a4a2014-06-16 18:39:09 -0700651 const char* old_cause = self->StartAssertNoThreadSuspension(
652 "Building interpreter shadow frame");
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700653 const DexFile::CodeItem* code_item = method->GetCodeItem();
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800654 DCHECK(code_item != nullptr) << PrettyMethod(method);
Ian Rogers848871b2013-08-05 10:56:33 -0700655 uint16_t num_regs = code_item->registers_size_;
656 void* memory = alloca(ShadowFrame::ComputeSize(num_regs));
Andreas Gampec200a4a2014-06-16 18:39:09 -0700657 // No last shadow coming from quick.
658 ShadowFrame* shadow_frame(ShadowFrame::Create(num_regs, nullptr, method, 0, memory));
Ian Rogers848871b2013-08-05 10:56:33 -0700659 size_t first_arg_reg = code_item->registers_size_ - code_item->ins_size_;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700660 uint32_t shorty_len = 0;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700661 auto* non_proxy_method = method->GetInterfaceMethodIfProxy(sizeof(void*));
662 const char* shorty = non_proxy_method->GetShorty(&shorty_len);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700663 BuildQuickShadowFrameVisitor shadow_frame_builder(sp, method->IsStatic(), shorty, shorty_len,
Ian Rogers936b37f2014-02-14 00:52:24 -0800664 shadow_frame, first_arg_reg);
Ian Rogers848871b2013-08-05 10:56:33 -0700665 shadow_frame_builder.VisitArguments();
Ian Rogerse94652f2014-12-02 11:13:19 -0800666 const bool needs_initialization =
667 method->IsStatic() && !method->GetDeclaringClass()->IsInitialized();
Ian Rogers848871b2013-08-05 10:56:33 -0700668 // Push a transition back into managed code onto the linked list in thread.
669 ManagedStack fragment;
670 self->PushManagedStackFragment(&fragment);
671 self->PushShadowFrame(shadow_frame);
672 self->EndAssertNoThreadSuspension(old_cause);
673
Ian Rogerse94652f2014-12-02 11:13:19 -0800674 if (needs_initialization) {
Ian Rogers848871b2013-08-05 10:56:33 -0700675 // Ensure static method's class is initialized.
Ian Rogerse94652f2014-12-02 11:13:19 -0800676 StackHandleScope<1> hs(self);
677 Handle<mirror::Class> h_class(hs.NewHandle(shadow_frame->GetMethod()->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700678 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
Ian Rogerse94652f2014-12-02 11:13:19 -0800679 DCHECK(Thread::Current()->IsExceptionPending()) << PrettyMethod(shadow_frame->GetMethod());
Ian Rogers848871b2013-08-05 10:56:33 -0700680 self->PopManagedStackFragment(fragment);
681 return 0;
682 }
683 }
Ian Rogerse94652f2014-12-02 11:13:19 -0800684 JValue result = interpreter::EnterInterpreterFromEntryPoint(self, code_item, shadow_frame);
Ian Rogers848871b2013-08-05 10:56:33 -0700685 // Pop transition.
686 self->PopManagedStackFragment(fragment);
Daniel Mihalyieb076692014-08-22 17:33:31 +0200687
688 // Request a stack deoptimization if needed
Mathieu Chartiere401d142015-04-22 13:56:20 -0700689 ArtMethod* caller = QuickArgumentVisitor::GetCallingMethod(sp);
Daniel Mihalyieb076692014-08-22 17:33:31 +0200690 if (UNLIKELY(Dbg::IsForcedInterpreterNeededForUpcall(self, caller))) {
691 self->SetException(Thread::GetDeoptimizationException());
Sebastien Hertz6e2d5742015-08-25 15:05:17 +0000692 self->SetDeoptimizationReturnValue(result, shorty[0] == 'L');
Daniel Mihalyieb076692014-08-22 17:33:31 +0200693 }
694
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800695 // No need to restore the args since the method has already been run by the interpreter.
Ian Rogers848871b2013-08-05 10:56:33 -0700696 return result.GetJ();
697 }
698}
699
700// Visits arguments on the stack placing them into the args vector, Object* arguments are converted
701// to jobjects.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800702class BuildQuickArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700703 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700704 BuildQuickArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty, uint32_t shorty_len,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700705 ScopedObjectAccessUnchecked* soa, std::vector<jvalue>* args) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700706 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa), args_(args) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700707
Mathieu Chartier90443472015-07-16 20:32:27 -0700708 void Visit() SHARED_REQUIRES(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700709
Mathieu Chartier90443472015-07-16 20:32:27 -0700710 void FixupReferences() SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800711
Ian Rogers848871b2013-08-05 10:56:33 -0700712 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700713 ScopedObjectAccessUnchecked* const soa_;
714 std::vector<jvalue>* const args_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800715 // References which we must update when exiting in case the GC moved the objects.
Ian Rogers700a4022014-05-19 16:49:03 -0700716 std::vector<std::pair<jobject, StackReference<mirror::Object>*>> references_;
Ian Rogers9758f792014-03-13 09:02:55 -0700717
Ian Rogers848871b2013-08-05 10:56:33 -0700718 DISALLOW_COPY_AND_ASSIGN(BuildQuickArgumentVisitor);
719};
720
Ian Rogers9758f792014-03-13 09:02:55 -0700721void BuildQuickArgumentVisitor::Visit() {
722 jvalue val;
723 Primitive::Type type = GetParamPrimitiveType();
724 switch (type) {
725 case Primitive::kPrimNot: {
726 StackReference<mirror::Object>* stack_ref =
727 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
728 val.l = soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
729 references_.push_back(std::make_pair(val.l, stack_ref));
730 break;
731 }
732 case Primitive::kPrimLong: // Fall-through.
733 case Primitive::kPrimDouble:
734 if (IsSplitLongOrDouble()) {
735 val.j = ReadSplitLongParam();
736 } else {
737 val.j = *reinterpret_cast<jlong*>(GetParamAddress());
738 }
739 break;
740 case Primitive::kPrimBoolean: // Fall-through.
741 case Primitive::kPrimByte: // Fall-through.
742 case Primitive::kPrimChar: // Fall-through.
743 case Primitive::kPrimShort: // Fall-through.
744 case Primitive::kPrimInt: // Fall-through.
745 case Primitive::kPrimFloat:
746 val.i = *reinterpret_cast<jint*>(GetParamAddress());
747 break;
748 case Primitive::kPrimVoid:
749 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700750 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -0700751 }
752 args_->push_back(val);
753}
754
755void BuildQuickArgumentVisitor::FixupReferences() {
756 // Fixup any references which may have changed.
757 for (const auto& pair : references_) {
758 pair.second->Assign(soa_->Decode<mirror::Object*>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -0700759 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -0700760 }
761}
762
Ian Rogers848871b2013-08-05 10:56:33 -0700763// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
764// which is responsible for recording callee save registers. We explicitly place into jobjects the
765// incoming reference arguments (so they survive GC). We invoke the invocation handler, which is a
766// field within the proxy object, which will box the primitive arguments and deal with error cases.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700767extern "C" uint64_t artQuickProxyInvokeHandler(
768 ArtMethod* proxy_method, mirror::Object* receiver, Thread* self, ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -0700769 SHARED_REQUIRES(Locks::mutator_lock_) {
Brian Carlstromd3633d52013-08-20 21:06:26 -0700770 DCHECK(proxy_method->IsProxyMethod()) << PrettyMethod(proxy_method);
771 DCHECK(receiver->GetClass()->IsProxyClass()) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700772 // Ensure we don't get thread suspension until the object arguments are safely in jobjects.
773 const char* old_cause =
774 self->StartAssertNoThreadSuspension("Adding to IRT proxy object arguments");
775 // Register the top of the managed stack, making stack crawlable.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700776 DCHECK_EQ((*sp), proxy_method) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700777 DCHECK_EQ(proxy_method->GetFrameSizeInBytes(),
Brian Carlstromd3633d52013-08-20 21:06:26 -0700778 Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes())
779 << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700780 self->VerifyStack();
781 // Start new JNI local reference state.
782 JNIEnvExt* env = self->GetJniEnv();
783 ScopedObjectAccessUnchecked soa(env);
784 ScopedJniEnvLocalRefState env_state(env);
785 // Create local ref. copies of proxy method and the receiver.
786 jobject rcvr_jobj = soa.AddLocalReference<jobject>(receiver);
787
788 // Placing arguments into args vector and remove the receiver.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700789 ArtMethod* non_proxy_method = proxy_method->GetInterfaceMethodIfProxy(sizeof(void*));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700790 CHECK(!non_proxy_method->IsStatic()) << PrettyMethod(proxy_method) << " "
Andreas Gampec200a4a2014-06-16 18:39:09 -0700791 << PrettyMethod(non_proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700792 std::vector<jvalue> args;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700793 uint32_t shorty_len = 0;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700794 const char* shorty = non_proxy_method->GetShorty(&shorty_len);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700795 BuildQuickArgumentVisitor local_ref_visitor(sp, false, shorty, shorty_len, &soa, &args);
Brian Carlstromd3633d52013-08-20 21:06:26 -0700796
Ian Rogers848871b2013-08-05 10:56:33 -0700797 local_ref_visitor.VisitArguments();
Brian Carlstromd3633d52013-08-20 21:06:26 -0700798 DCHECK_GT(args.size(), 0U) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700799 args.erase(args.begin());
800
801 // Convert proxy method into expected interface method.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700802 ArtMethod* interface_method = proxy_method->FindOverriddenMethod(sizeof(void*));
Ian Rogerse0a02da2014-12-02 14:10:53 -0800803 DCHECK(interface_method != nullptr) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700804 DCHECK(!interface_method->IsProxyMethod()) << PrettyMethod(interface_method);
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700805 self->EndAssertNoThreadSuspension(old_cause);
806 jobject interface_method_jobj = soa.AddLocalReference<jobject>(
807 mirror::Method::CreateFromArtMethod(soa.Self(), interface_method));
Ian Rogers848871b2013-08-05 10:56:33 -0700808
809 // All naked Object*s should now be in jobjects, so its safe to go into the main invoke code
810 // that performs allocations.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700811 JValue result = InvokeProxyInvocationHandler(soa, shorty, rcvr_jobj, interface_method_jobj, args);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800812 // Restore references which might have moved.
813 local_ref_visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -0700814 return result.GetJ();
815}
816
817// Read object references held in arguments from quick frames and place in a JNI local references,
818// so they don't get garbage collected.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800819class RememberForGcArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700820 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700821 RememberForGcArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty,
822 uint32_t shorty_len, ScopedObjectAccessUnchecked* soa) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700823 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700824
Mathieu Chartier90443472015-07-16 20:32:27 -0700825 void Visit() SHARED_REQUIRES(Locks::mutator_lock_) OVERRIDE;
Mathieu Chartier07d447b2013-09-26 11:57:43 -0700826
Mathieu Chartier90443472015-07-16 20:32:27 -0700827 void FixupReferences() SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers848871b2013-08-05 10:56:33 -0700828
829 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700830 ScopedObjectAccessUnchecked* const soa_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800831 // References which we must update when exiting in case the GC moved the objects.
Andreas Gampec200a4a2014-06-16 18:39:09 -0700832 std::vector<std::pair<jobject, StackReference<mirror::Object>*> > references_;
833
Mathieu Chartier590fee92013-09-13 13:46:47 -0700834 DISALLOW_COPY_AND_ASSIGN(RememberForGcArgumentVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700835};
836
Ian Rogers9758f792014-03-13 09:02:55 -0700837void RememberForGcArgumentVisitor::Visit() {
838 if (IsParamAReference()) {
839 StackReference<mirror::Object>* stack_ref =
840 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
841 jobject reference =
842 soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
843 references_.push_back(std::make_pair(reference, stack_ref));
844 }
845}
846
847void RememberForGcArgumentVisitor::FixupReferences() {
848 // Fixup any references which may have changed.
849 for (const auto& pair : references_) {
850 pair.second->Assign(soa_->Decode<mirror::Object*>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -0700851 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -0700852 }
853}
854
Ian Rogers848871b2013-08-05 10:56:33 -0700855// Lazily resolve a method for quick. Called by stub code.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700856extern "C" const void* artQuickResolutionTrampoline(
857 ArtMethod* called, mirror::Object* receiver, Thread* self, ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -0700858 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe3b45ef22015-05-26 21:34:09 -0700859 // The resolution trampoline stashes the resolved method into the callee-save frame to transport
860 // it. Thus, when exiting, the stack cannot be verified (as the resolved method most likely
861 // does not have the same stack layout as the callee-save method).
862 ScopedQuickEntrypointChecks sqec(self, kIsDebugBuild, false);
Ian Rogers848871b2013-08-05 10:56:33 -0700863 // Start new JNI local reference state
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800864 JNIEnvExt* env = self->GetJniEnv();
Ian Rogers848871b2013-08-05 10:56:33 -0700865 ScopedObjectAccessUnchecked soa(env);
866 ScopedJniEnvLocalRefState env_state(env);
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800867 const char* old_cause = self->StartAssertNoThreadSuspension("Quick method resolution set up");
Ian Rogers848871b2013-08-05 10:56:33 -0700868
869 // Compute details about the called method (avoid GCs)
870 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Ian Rogers848871b2013-08-05 10:56:33 -0700871 InvokeType invoke_type;
Ian Rogerse0a02da2014-12-02 14:10:53 -0800872 MethodReference called_method(nullptr, 0);
873 const bool called_method_known_on_entry = !called->IsRuntimeMethod();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700874 ArtMethod* caller = nullptr;
Ian Rogerse0a02da2014-12-02 14:10:53 -0800875 if (!called_method_known_on_entry) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +0100876 caller = QuickArgumentVisitor::GetCallingMethod(sp);
877 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
Ian Rogers848871b2013-08-05 10:56:33 -0700878 const DexFile::CodeItem* code;
Ian Rogerse0a02da2014-12-02 14:10:53 -0800879 called_method.dex_file = caller->GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700880 code = caller->GetCodeItem();
Ian Rogers848871b2013-08-05 10:56:33 -0700881 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
882 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
883 Instruction::Code instr_code = instr->Opcode();
884 bool is_range;
885 switch (instr_code) {
886 case Instruction::INVOKE_DIRECT:
887 invoke_type = kDirect;
888 is_range = false;
889 break;
890 case Instruction::INVOKE_DIRECT_RANGE:
891 invoke_type = kDirect;
892 is_range = true;
893 break;
894 case Instruction::INVOKE_STATIC:
895 invoke_type = kStatic;
896 is_range = false;
897 break;
898 case Instruction::INVOKE_STATIC_RANGE:
899 invoke_type = kStatic;
900 is_range = true;
901 break;
902 case Instruction::INVOKE_SUPER:
903 invoke_type = kSuper;
904 is_range = false;
905 break;
906 case Instruction::INVOKE_SUPER_RANGE:
907 invoke_type = kSuper;
908 is_range = true;
909 break;
910 case Instruction::INVOKE_VIRTUAL:
911 invoke_type = kVirtual;
912 is_range = false;
913 break;
914 case Instruction::INVOKE_VIRTUAL_RANGE:
915 invoke_type = kVirtual;
916 is_range = true;
917 break;
918 case Instruction::INVOKE_INTERFACE:
919 invoke_type = kInterface;
920 is_range = false;
921 break;
922 case Instruction::INVOKE_INTERFACE_RANGE:
923 invoke_type = kInterface;
924 is_range = true;
925 break;
926 default:
Ian Rogerse0a02da2014-12-02 14:10:53 -0800927 LOG(FATAL) << "Unexpected call into trampoline: " << instr->DumpString(nullptr);
928 UNREACHABLE();
Ian Rogers848871b2013-08-05 10:56:33 -0700929 }
Ian Rogerse0a02da2014-12-02 14:10:53 -0800930 called_method.dex_method_index = (is_range) ? instr->VRegB_3rc() : instr->VRegB_35c();
Ian Rogers848871b2013-08-05 10:56:33 -0700931 } else {
932 invoke_type = kStatic;
Ian Rogerse0a02da2014-12-02 14:10:53 -0800933 called_method.dex_file = called->GetDexFile();
934 called_method.dex_method_index = called->GetDexMethodIndex();
Ian Rogers848871b2013-08-05 10:56:33 -0700935 }
936 uint32_t shorty_len;
937 const char* shorty =
Ian Rogerse0a02da2014-12-02 14:10:53 -0800938 called_method.dex_file->GetMethodShorty(
939 called_method.dex_file->GetMethodId(called_method.dex_method_index), &shorty_len);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700940 RememberForGcArgumentVisitor visitor(sp, invoke_type == kStatic, shorty, shorty_len, &soa);
Ian Rogers848871b2013-08-05 10:56:33 -0700941 visitor.VisitArguments();
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800942 self->EndAssertNoThreadSuspension(old_cause);
Ian Rogerse0a02da2014-12-02 14:10:53 -0800943 const bool virtual_or_interface = invoke_type == kVirtual || invoke_type == kInterface;
Ian Rogers848871b2013-08-05 10:56:33 -0700944 // Resolve method filling in dex cache.
Ian Rogerse0a02da2014-12-02 14:10:53 -0800945 if (!called_method_known_on_entry) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700946 StackHandleScope<1> hs(self);
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700947 mirror::Object* dummy = nullptr;
948 HandleWrapper<mirror::Object> h_receiver(
949 hs.NewHandleWrapper(virtual_or_interface ? &receiver : &dummy));
Ian Rogerse0a02da2014-12-02 14:10:53 -0800950 DCHECK_EQ(caller->GetDexFile(), called_method.dex_file);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700951 called = linker->ResolveMethod(self, called_method.dex_method_index, caller, invoke_type);
Ian Rogers848871b2013-08-05 10:56:33 -0700952 }
Ian Rogerse0a02da2014-12-02 14:10:53 -0800953 const void* code = nullptr;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800954 if (LIKELY(!self->IsExceptionPending())) {
Ian Rogers848871b2013-08-05 10:56:33 -0700955 // Incompatible class change should have been handled in resolve method.
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800956 CHECK(!called->CheckIncompatibleClassChange(invoke_type))
957 << PrettyMethod(called) << " " << invoke_type;
Mathieu Chartier55871bf2014-02-27 10:24:50 -0800958 if (virtual_or_interface) {
959 // Refine called method based on receiver.
960 CHECK(receiver != nullptr) << invoke_type;
Mingyao Yangf4867782014-05-05 11:55:02 -0700961
Mathieu Chartiere401d142015-04-22 13:56:20 -0700962 ArtMethod* orig_called = called;
Mathieu Chartier55871bf2014-02-27 10:24:50 -0800963 if (invoke_type == kVirtual) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700964 called = receiver->GetClass()->FindVirtualMethodForVirtual(called, sizeof(void*));
Mathieu Chartier55871bf2014-02-27 10:24:50 -0800965 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700966 called = receiver->GetClass()->FindVirtualMethodForInterface(called, sizeof(void*));
Mathieu Chartier55871bf2014-02-27 10:24:50 -0800967 }
Mingyao Yangf4867782014-05-05 11:55:02 -0700968
969 CHECK(called != nullptr) << PrettyMethod(orig_called) << " "
970 << PrettyTypeOf(receiver) << " "
971 << invoke_type << " " << orig_called->GetVtableIndex();
972
Ian Rogers83883d72013-10-21 21:07:24 -0700973 // 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 -0800974 // of the sharpened method avoiding dirtying the dex cache if possible.
Ian Rogers00f15272014-12-02 16:55:46 -0800975 // Note, called_method.dex_method_index references the dex method before the
976 // FindVirtualMethodFor... This is ok for FindDexMethodIndexInOtherDexFile that only cares
977 // about the name and signature.
978 uint32_t update_dex_cache_method_index = called->GetDexMethodIndex();
Ian Rogerse0a02da2014-12-02 14:10:53 -0800979 if (!called->HasSameDexCacheResolvedMethods(caller)) {
Ian Rogers83883d72013-10-21 21:07:24 -0700980 // Calling from one dex file to another, need to compute the method index appropriate to
Vladimir Markobbcc0c02014-02-03 14:08:42 +0000981 // the caller's dex file. Since we get here only if the original called was a runtime
982 // method, we've got the correct dex_file and a dex_method_idx from above.
Ian Rogerse0a02da2014-12-02 14:10:53 -0800983 DCHECK(!called_method_known_on_entry);
984 DCHECK_EQ(caller->GetDexFile(), called_method.dex_file);
985 const DexFile* caller_dex_file = called_method.dex_file;
986 uint32_t caller_method_name_and_sig_index = called_method.dex_method_index;
987 update_dex_cache_method_index =
988 called->FindDexMethodIndexInOtherDexFile(*caller_dex_file,
989 caller_method_name_and_sig_index);
990 }
991 if ((update_dex_cache_method_index != DexFile::kDexNoIndex) &&
Mathieu Chartiere401d142015-04-22 13:56:20 -0700992 (caller->GetDexCacheResolvedMethod(
993 update_dex_cache_method_index, sizeof(void*)) != called)) {
994 caller->SetDexCacheResolvedMethod(update_dex_cache_method_index, called, sizeof(void*));
Ian Rogers83883d72013-10-21 21:07:24 -0700995 }
Mathieu Chartiere4a91bb2015-01-28 13:11:44 -0800996 } else if (invoke_type == kStatic) {
997 const auto called_dex_method_idx = called->GetDexMethodIndex();
998 // For static invokes, we may dispatch to the static method in the superclass but resolve
999 // using the subclass. To prevent getting slow paths on each invoke, we force set the
1000 // resolved method for the super class dex method index if we are in the same dex file.
1001 // b/19175856
1002 if (called->GetDexFile() == called_method.dex_file &&
1003 called_method.dex_method_index != called_dex_method_idx) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001004 called->GetDexCache()->SetResolvedMethod(called_dex_method_idx, called, sizeof(void*));
Mathieu Chartiere4a91bb2015-01-28 13:11:44 -08001005 }
Ian Rogers83883d72013-10-21 21:07:24 -07001006 }
Daniel Mihalyieb076692014-08-22 17:33:31 +02001007
Ian Rogers848871b2013-08-05 10:56:33 -07001008 // Ensure that the called method's class is initialized.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001009 StackHandleScope<1> hs(soa.Self());
1010 Handle<mirror::Class> called_class(hs.NewHandle(called->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -07001011 linker->EnsureInitialized(soa.Self(), called_class, true, true);
Ian Rogers848871b2013-08-05 10:56:33 -07001012 if (LIKELY(called_class->IsInitialized())) {
Daniel Mihalyieb076692014-08-22 17:33:31 +02001013 if (UNLIKELY(Dbg::IsForcedInterpreterNeededForResolution(self, called))) {
1014 // If we are single-stepping or the called method is deoptimized (by a
1015 // breakpoint, for example), then we have to execute the called method
1016 // with the interpreter.
1017 code = GetQuickToInterpreterBridge();
1018 } else if (UNLIKELY(Dbg::IsForcedInstrumentationNeededForResolution(self, caller))) {
1019 // If the caller is deoptimized (by a breakpoint, for example), we have to
1020 // continue its execution with interpreter when returning from the called
1021 // method. Because we do not want to execute the called method with the
1022 // interpreter, we wrap its execution into the instrumentation stubs.
1023 // When the called method returns, it will execute the instrumentation
1024 // exit hook that will determine the need of the interpreter with a call
1025 // to Dbg::IsForcedInterpreterNeededForUpcall and deoptimize the stack if
1026 // it is needed.
1027 code = GetQuickInstrumentationEntryPoint();
1028 } else {
1029 code = called->GetEntryPointFromQuickCompiledCode();
1030 }
Ian Rogers848871b2013-08-05 10:56:33 -07001031 } else if (called_class->IsInitializing()) {
Daniel Mihalyieb076692014-08-22 17:33:31 +02001032 if (UNLIKELY(Dbg::IsForcedInterpreterNeededForResolution(self, called))) {
1033 // If we are single-stepping or the called method is deoptimized (by a
1034 // breakpoint, for example), then we have to execute the called method
1035 // with the interpreter.
1036 code = GetQuickToInterpreterBridge();
1037 } else if (invoke_type == kStatic) {
Ian Rogers848871b2013-08-05 10:56:33 -07001038 // Class is still initializing, go to oat and grab code (trampoline must be left in place
1039 // until class is initialized to stop races between threads).
Ian Rogersef7d42f2014-01-06 12:55:46 -08001040 code = linker->GetQuickOatCodeFor(called);
Ian Rogers848871b2013-08-05 10:56:33 -07001041 } else {
1042 // No trampoline for non-static methods.
Ian Rogersef7d42f2014-01-06 12:55:46 -08001043 code = called->GetEntryPointFromQuickCompiledCode();
Ian Rogers848871b2013-08-05 10:56:33 -07001044 }
1045 } else {
1046 DCHECK(called_class->IsErroneous());
1047 }
1048 }
Ian Rogerse0a02da2014-12-02 14:10:53 -08001049 CHECK_EQ(code == nullptr, self->IsExceptionPending());
Mathieu Chartier07d447b2013-09-26 11:57:43 -07001050 // Fixup any locally saved objects may have moved during a GC.
1051 visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -07001052 // Place called method in callee-save frame to be placed as first argument to quick method.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001053 *sp = called;
1054
Ian Rogers848871b2013-08-05 10:56:33 -07001055 return code;
1056}
1057
Andreas Gampec147b002014-03-06 18:11:06 -08001058/*
1059 * This class uses a couple of observations to unite the different calling conventions through
1060 * a few constants.
1061 *
1062 * 1) Number of registers used for passing is normally even, so counting down has no penalty for
1063 * possible alignment.
1064 * 2) Known 64b architectures store 8B units on the stack, both for integral and floating point
1065 * types, so using uintptr_t is OK. Also means that we can use kRegistersNeededX to denote
1066 * when we have to split things
1067 * 3) The only soft-float, Arm, is 32b, so no widening needs to be taken into account for floats
1068 * and we can use Int handling directly.
1069 * 4) Only 64b architectures widen, and their stack is aligned 8B anyways, so no padding code
1070 * necessary when widening. Also, widening of Ints will take place implicitly, and the
1071 * extension should be compatible with Aarch64, which mandates copying the available bits
1072 * into LSB and leaving the rest unspecified.
1073 * 5) Aligning longs and doubles is necessary on arm only, and it's the same in registers and on
1074 * the stack.
1075 * 6) There is only little endian.
1076 *
1077 *
1078 * Actual work is supposed to be done in a delegate of the template type. The interface is as
1079 * follows:
1080 *
1081 * void PushGpr(uintptr_t): Add a value for the next GPR
1082 *
1083 * void PushFpr4(float): Add a value for the next FPR of size 32b. Is only called if we need
1084 * padding, that is, think the architecture is 32b and aligns 64b.
1085 *
1086 * void PushFpr8(uint64_t): Push a double. We _will_ call this on 32b, it's the callee's job to
1087 * split this if necessary. The current state will have aligned, if
1088 * necessary.
1089 *
1090 * void PushStack(uintptr_t): Push a value to the stack.
1091 *
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001092 * uintptr_t PushHandleScope(mirror::Object* ref): Add a reference to the HandleScope. This _will_ have nullptr,
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001093 * as this might be important for null initialization.
Andreas Gampec147b002014-03-06 18:11:06 -08001094 * Must return the jobject, that is, the reference to the
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001095 * entry in the HandleScope (nullptr if necessary).
Andreas Gampec147b002014-03-06 18:11:06 -08001096 *
1097 */
Andreas Gampec200a4a2014-06-16 18:39:09 -07001098template<class T> class BuildNativeCallFrameStateMachine {
Andreas Gampec147b002014-03-06 18:11:06 -08001099 public:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001100#if defined(__arm__)
1101 // TODO: These are all dummy values!
Andreas Gampec147b002014-03-06 18:11:06 -08001102 static constexpr bool kNativeSoftFloatAbi = true;
1103 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs, r0-r3
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001104 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
1105
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001106 static constexpr size_t kRegistersNeededForLong = 2;
1107 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -08001108 static constexpr bool kMultiRegistersAligned = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001109 static constexpr bool kMultiFPRegistersWidened = false;
1110 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001111 static constexpr bool kAlignLongOnStack = true;
1112 static constexpr bool kAlignDoubleOnStack = true;
Stuart Monteithb95a5342014-03-12 13:32:32 +00001113#elif defined(__aarch64__)
1114 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
1115 static constexpr size_t kNumNativeGprArgs = 8; // 6 arguments passed in GPRs.
1116 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
1117
1118 static constexpr size_t kRegistersNeededForLong = 1;
1119 static constexpr size_t kRegistersNeededForDouble = 1;
1120 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001121 static constexpr bool kMultiFPRegistersWidened = false;
1122 static constexpr bool kMultiGPRegistersWidened = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +00001123 static constexpr bool kAlignLongOnStack = false;
1124 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001125#elif defined(__mips__) && !defined(__LP64__)
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001126 static constexpr bool kNativeSoftFloatAbi = true; // This is a hard float ABI.
Douglas Leung735b8552014-10-31 12:21:40 -07001127 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs.
1128 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001129
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001130 static constexpr size_t kRegistersNeededForLong = 2;
1131 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -08001132 static constexpr bool kMultiRegistersAligned = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001133 static constexpr bool kMultiFPRegistersWidened = true;
1134 static constexpr bool kMultiGPRegistersWidened = false;
Douglas Leung735b8552014-10-31 12:21:40 -07001135 static constexpr bool kAlignLongOnStack = true;
1136 static constexpr bool kAlignDoubleOnStack = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001137#elif defined(__mips__) && defined(__LP64__)
1138 // Let the code prepare GPRs only and we will load the FPRs with same data.
1139 static constexpr bool kNativeSoftFloatAbi = true;
1140 static constexpr size_t kNumNativeGprArgs = 8;
1141 static constexpr size_t kNumNativeFprArgs = 0;
1142
1143 static constexpr size_t kRegistersNeededForLong = 1;
1144 static constexpr size_t kRegistersNeededForDouble = 1;
1145 static constexpr bool kMultiRegistersAligned = false;
1146 static constexpr bool kMultiFPRegistersWidened = false;
1147 static constexpr bool kMultiGPRegistersWidened = true;
1148 static constexpr bool kAlignLongOnStack = false;
1149 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001150#elif defined(__i386__)
1151 // TODO: Check these!
Andreas Gampec147b002014-03-06 18:11:06 -08001152 static constexpr bool kNativeSoftFloatAbi = false; // Not using int registers for fp
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001153 static constexpr size_t kNumNativeGprArgs = 0; // 6 arguments passed in GPRs.
1154 static constexpr size_t kNumNativeFprArgs = 0; // 8 arguments passed in FPRs.
1155
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001156 static constexpr size_t kRegistersNeededForLong = 2;
1157 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001158 static constexpr bool kMultiRegistersAligned = false; // x86 not using regs, anyways
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001159 static constexpr bool kMultiFPRegistersWidened = false;
1160 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001161 static constexpr bool kAlignLongOnStack = false;
1162 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001163#elif defined(__x86_64__)
1164 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
1165 static constexpr size_t kNumNativeGprArgs = 6; // 6 arguments passed in GPRs.
1166 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
1167
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001168 static constexpr size_t kRegistersNeededForLong = 1;
1169 static constexpr size_t kRegistersNeededForDouble = 1;
Andreas Gampec147b002014-03-06 18:11:06 -08001170 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001171 static constexpr bool kMultiFPRegistersWidened = false;
1172 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001173 static constexpr bool kAlignLongOnStack = false;
1174 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001175#else
1176#error "Unsupported architecture"
1177#endif
1178
Andreas Gampec147b002014-03-06 18:11:06 -08001179 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001180 explicit BuildNativeCallFrameStateMachine(T* delegate)
1181 : gpr_index_(kNumNativeGprArgs),
1182 fpr_index_(kNumNativeFprArgs),
1183 stack_entries_(0),
1184 delegate_(delegate) {
Andreas Gampec147b002014-03-06 18:11:06 -08001185 // For register alignment, we want to assume that counters (gpr_index_, fpr_index_) are even iff
1186 // the next register is even; counting down is just to make the compiler happy...
Andreas Gampe575e78c2014-11-03 23:41:03 -08001187 static_assert(kNumNativeGprArgs % 2 == 0U, "Number of native GPR arguments not even");
1188 static_assert(kNumNativeFprArgs % 2 == 0U, "Number of native FPR arguments not even");
Andreas Gampec147b002014-03-06 18:11:06 -08001189 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001190
Andreas Gampec200a4a2014-06-16 18:39:09 -07001191 virtual ~BuildNativeCallFrameStateMachine() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001192
Ian Rogers1428dce2014-10-21 15:02:15 -07001193 bool HavePointerGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001194 return gpr_index_ > 0;
1195 }
1196
Andreas Gampec200a4a2014-06-16 18:39:09 -07001197 void AdvancePointer(const void* val) {
Andreas Gampec147b002014-03-06 18:11:06 -08001198 if (HavePointerGpr()) {
1199 gpr_index_--;
1200 PushGpr(reinterpret_cast<uintptr_t>(val));
1201 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001202 stack_entries_++; // TODO: have a field for pointer length as multiple of 32b
Andreas Gampec147b002014-03-06 18:11:06 -08001203 PushStack(reinterpret_cast<uintptr_t>(val));
1204 gpr_index_ = 0;
1205 }
1206 }
1207
Ian Rogers1428dce2014-10-21 15:02:15 -07001208 bool HaveHandleScopeGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001209 return gpr_index_ > 0;
1210 }
1211
Mathieu Chartier90443472015-07-16 20:32:27 -07001212 void AdvanceHandleScope(mirror::Object* ptr) SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001213 uintptr_t handle = PushHandle(ptr);
1214 if (HaveHandleScopeGpr()) {
Andreas Gampec147b002014-03-06 18:11:06 -08001215 gpr_index_--;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001216 PushGpr(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001217 } else {
1218 stack_entries_++;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001219 PushStack(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001220 gpr_index_ = 0;
1221 }
1222 }
1223
Ian Rogers1428dce2014-10-21 15:02:15 -07001224 bool HaveIntGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001225 return gpr_index_ > 0;
1226 }
1227
1228 void AdvanceInt(uint32_t val) {
1229 if (HaveIntGpr()) {
1230 gpr_index_--;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001231 if (kMultiGPRegistersWidened) {
1232 DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001233 PushGpr(static_cast<int64_t>(bit_cast<int32_t, uint32_t>(val)));
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001234 } else {
1235 PushGpr(val);
1236 }
Andreas Gampec147b002014-03-06 18:11:06 -08001237 } else {
1238 stack_entries_++;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001239 if (kMultiGPRegistersWidened) {
1240 DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001241 PushStack(static_cast<int64_t>(bit_cast<int32_t, uint32_t>(val)));
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001242 } else {
1243 PushStack(val);
1244 }
Andreas Gampec147b002014-03-06 18:11:06 -08001245 gpr_index_ = 0;
1246 }
1247 }
1248
Ian Rogers1428dce2014-10-21 15:02:15 -07001249 bool HaveLongGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001250 return gpr_index_ >= kRegistersNeededForLong + (LongGprNeedsPadding() ? 1 : 0);
1251 }
1252
Ian Rogers1428dce2014-10-21 15:02:15 -07001253 bool LongGprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001254 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1255 kAlignLongOnStack && // and when it needs alignment
1256 (gpr_index_ & 1) == 1; // counter is odd, see constructor
1257 }
1258
Ian Rogers1428dce2014-10-21 15:02:15 -07001259 bool LongStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001260 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1261 kAlignLongOnStack && // and when it needs 8B alignment
1262 (stack_entries_ & 1) == 1; // counter is odd
1263 }
1264
1265 void AdvanceLong(uint64_t val) {
1266 if (HaveLongGpr()) {
1267 if (LongGprNeedsPadding()) {
1268 PushGpr(0);
1269 gpr_index_--;
1270 }
1271 if (kRegistersNeededForLong == 1) {
1272 PushGpr(static_cast<uintptr_t>(val));
1273 } else {
1274 PushGpr(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1275 PushGpr(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1276 }
1277 gpr_index_ -= kRegistersNeededForLong;
1278 } else {
1279 if (LongStackNeedsPadding()) {
1280 PushStack(0);
1281 stack_entries_++;
1282 }
1283 if (kRegistersNeededForLong == 1) {
1284 PushStack(static_cast<uintptr_t>(val));
1285 stack_entries_++;
1286 } else {
1287 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1288 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1289 stack_entries_ += 2;
1290 }
1291 gpr_index_ = 0;
1292 }
1293 }
1294
Ian Rogers1428dce2014-10-21 15:02:15 -07001295 bool HaveFloatFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001296 return fpr_index_ > 0;
1297 }
1298
Andreas Gampec147b002014-03-06 18:11:06 -08001299 void AdvanceFloat(float val) {
1300 if (kNativeSoftFloatAbi) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001301 AdvanceInt(bit_cast<uint32_t, float>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001302 } else {
1303 if (HaveFloatFpr()) {
1304 fpr_index_--;
1305 if (kRegistersNeededForDouble == 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001306 if (kMultiFPRegistersWidened) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001307 PushFpr8(bit_cast<uint64_t, double>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001308 } else {
1309 // No widening, just use the bits.
Roland Levillainda4d79b2015-03-24 14:36:11 +00001310 PushFpr8(static_cast<uint64_t>(bit_cast<uint32_t, float>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001311 }
1312 } else {
1313 PushFpr4(val);
1314 }
1315 } else {
1316 stack_entries_++;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001317 if (kRegistersNeededForDouble == 1 && kMultiFPRegistersWidened) {
Andreas Gampec147b002014-03-06 18:11:06 -08001318 // Need to widen before storing: Note the "double" in the template instantiation.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001319 // Note: We need to jump through those hoops to make the compiler happy.
1320 DCHECK_EQ(sizeof(uintptr_t), sizeof(uint64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001321 PushStack(static_cast<uintptr_t>(bit_cast<uint64_t, double>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001322 } else {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001323 PushStack(static_cast<uintptr_t>(bit_cast<uint32_t, float>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001324 }
1325 fpr_index_ = 0;
1326 }
1327 }
1328 }
1329
Ian Rogers1428dce2014-10-21 15:02:15 -07001330 bool HaveDoubleFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001331 return fpr_index_ >= kRegistersNeededForDouble + (DoubleFprNeedsPadding() ? 1 : 0);
1332 }
1333
Ian Rogers1428dce2014-10-21 15:02:15 -07001334 bool DoubleFprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001335 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1336 kAlignDoubleOnStack && // and when it needs alignment
1337 (fpr_index_ & 1) == 1; // counter is odd, see constructor
1338 }
1339
Ian Rogers1428dce2014-10-21 15:02:15 -07001340 bool DoubleStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001341 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1342 kAlignDoubleOnStack && // and when it needs 8B alignment
1343 (stack_entries_ & 1) == 1; // counter is odd
1344 }
1345
1346 void AdvanceDouble(uint64_t val) {
1347 if (kNativeSoftFloatAbi) {
1348 AdvanceLong(val);
1349 } else {
1350 if (HaveDoubleFpr()) {
1351 if (DoubleFprNeedsPadding()) {
1352 PushFpr4(0);
1353 fpr_index_--;
1354 }
1355 PushFpr8(val);
1356 fpr_index_ -= kRegistersNeededForDouble;
1357 } else {
1358 if (DoubleStackNeedsPadding()) {
1359 PushStack(0);
1360 stack_entries_++;
1361 }
1362 if (kRegistersNeededForDouble == 1) {
1363 PushStack(static_cast<uintptr_t>(val));
1364 stack_entries_++;
1365 } else {
1366 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1367 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1368 stack_entries_ += 2;
1369 }
1370 fpr_index_ = 0;
1371 }
1372 }
1373 }
1374
Ian Rogers1428dce2014-10-21 15:02:15 -07001375 uint32_t GetStackEntries() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001376 return stack_entries_;
1377 }
1378
Ian Rogers1428dce2014-10-21 15:02:15 -07001379 uint32_t GetNumberOfUsedGprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001380 return kNumNativeGprArgs - gpr_index_;
1381 }
1382
Ian Rogers1428dce2014-10-21 15:02:15 -07001383 uint32_t GetNumberOfUsedFprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001384 return kNumNativeFprArgs - fpr_index_;
1385 }
1386
1387 private:
1388 void PushGpr(uintptr_t val) {
1389 delegate_->PushGpr(val);
1390 }
1391 void PushFpr4(float val) {
1392 delegate_->PushFpr4(val);
1393 }
1394 void PushFpr8(uint64_t val) {
1395 delegate_->PushFpr8(val);
1396 }
1397 void PushStack(uintptr_t val) {
1398 delegate_->PushStack(val);
1399 }
Mathieu Chartier90443472015-07-16 20:32:27 -07001400 uintptr_t PushHandle(mirror::Object* ref) SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001401 return delegate_->PushHandle(ref);
Andreas Gampec147b002014-03-06 18:11:06 -08001402 }
1403
1404 uint32_t gpr_index_; // Number of free GPRs
1405 uint32_t fpr_index_; // Number of free FPRs
1406 uint32_t stack_entries_; // Stack entries are in multiples of 32b, as floats are usually not
1407 // extended
Ian Rogers1428dce2014-10-21 15:02:15 -07001408 T* const delegate_; // What Push implementation gets called
Andreas Gampec147b002014-03-06 18:11:06 -08001409};
1410
Andreas Gampec200a4a2014-06-16 18:39:09 -07001411// Computes the sizes of register stacks and call stack area. Handling of references can be extended
1412// in subclasses.
1413//
1414// To handle native pointers, use "L" in the shorty for an object reference, which simulates
1415// them with handles.
1416class ComputeNativeCallFrameSize {
Andreas Gampec147b002014-03-06 18:11:06 -08001417 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001418 ComputeNativeCallFrameSize() : num_stack_entries_(0) {}
1419
1420 virtual ~ComputeNativeCallFrameSize() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001421
Ian Rogers1428dce2014-10-21 15:02:15 -07001422 uint32_t GetStackSize() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001423 return num_stack_entries_ * sizeof(uintptr_t);
1424 }
1425
Ian Rogers1428dce2014-10-21 15:02:15 -07001426 uint8_t* LayoutCallStack(uint8_t* sp8) const {
Andreas Gampec147b002014-03-06 18:11:06 -08001427 sp8 -= GetStackSize();
Andreas Gampe779f8c92014-06-09 18:29:38 -07001428 // Align by kStackAlignment.
1429 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
Andreas Gampec200a4a2014-06-16 18:39:09 -07001430 return sp8;
Andreas Gampec147b002014-03-06 18:11:06 -08001431 }
1432
Ian Rogers1428dce2014-10-21 15:02:15 -07001433 uint8_t* LayoutCallRegisterStacks(uint8_t* sp8, uintptr_t** start_gpr, uint32_t** start_fpr)
1434 const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001435 // Assumption is OK right now, as we have soft-float arm
1436 size_t fregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeFprArgs;
1437 sp8 -= fregs * sizeof(uintptr_t);
1438 *start_fpr = reinterpret_cast<uint32_t*>(sp8);
1439 size_t iregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeGprArgs;
1440 sp8 -= iregs * sizeof(uintptr_t);
1441 *start_gpr = reinterpret_cast<uintptr_t*>(sp8);
1442 return sp8;
1443 }
Andreas Gampec147b002014-03-06 18:11:06 -08001444
Andreas Gampec200a4a2014-06-16 18:39:09 -07001445 uint8_t* LayoutNativeCall(uint8_t* sp8, uintptr_t** start_stack, uintptr_t** start_gpr,
Ian Rogers1428dce2014-10-21 15:02:15 -07001446 uint32_t** start_fpr) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001447 // Native call stack.
1448 sp8 = LayoutCallStack(sp8);
1449 *start_stack = reinterpret_cast<uintptr_t*>(sp8);
Andreas Gampec147b002014-03-06 18:11:06 -08001450
Andreas Gampec200a4a2014-06-16 18:39:09 -07001451 // Put fprs and gprs below.
1452 sp8 = LayoutCallRegisterStacks(sp8, start_gpr, start_fpr);
Andreas Gampec147b002014-03-06 18:11:06 -08001453
Andreas Gampec200a4a2014-06-16 18:39:09 -07001454 // Return the new bottom.
1455 return sp8;
1456 }
1457
1458 virtual void WalkHeader(BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm)
Mathieu Chartier90443472015-07-16 20:32:27 -07001459 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001460 UNUSED(sm);
1461 }
Andreas Gampec200a4a2014-06-16 18:39:09 -07001462
Mathieu Chartier90443472015-07-16 20:32:27 -07001463 void Walk(const char* shorty, uint32_t shorty_len) SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001464 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize> sm(this);
1465
1466 WalkHeader(&sm);
Andreas Gampec147b002014-03-06 18:11:06 -08001467
1468 for (uint32_t i = 1; i < shorty_len; ++i) {
1469 Primitive::Type cur_type_ = Primitive::GetType(shorty[i]);
1470 switch (cur_type_) {
1471 case Primitive::kPrimNot:
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001472 // TODO: fix abuse of mirror types.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001473 sm.AdvanceHandleScope(
1474 reinterpret_cast<mirror::Object*>(0x12345678));
Andreas Gampec147b002014-03-06 18:11:06 -08001475 break;
1476
1477 case Primitive::kPrimBoolean:
1478 case Primitive::kPrimByte:
1479 case Primitive::kPrimChar:
1480 case Primitive::kPrimShort:
1481 case Primitive::kPrimInt:
1482 sm.AdvanceInt(0);
1483 break;
1484 case Primitive::kPrimFloat:
1485 sm.AdvanceFloat(0);
1486 break;
1487 case Primitive::kPrimDouble:
1488 sm.AdvanceDouble(0);
1489 break;
1490 case Primitive::kPrimLong:
1491 sm.AdvanceLong(0);
1492 break;
1493 default:
1494 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001495 UNREACHABLE();
Andreas Gampec147b002014-03-06 18:11:06 -08001496 }
1497 }
1498
Ian Rogers1428dce2014-10-21 15:02:15 -07001499 num_stack_entries_ = sm.GetStackEntries();
Andreas Gampec147b002014-03-06 18:11:06 -08001500 }
1501
1502 void PushGpr(uintptr_t /* val */) {
1503 // not optimizing registers, yet
1504 }
1505
1506 void PushFpr4(float /* val */) {
1507 // not optimizing registers, yet
1508 }
1509
1510 void PushFpr8(uint64_t /* val */) {
1511 // not optimizing registers, yet
1512 }
1513
1514 void PushStack(uintptr_t /* val */) {
1515 // counting is already done in the superclass
1516 }
1517
Andreas Gampec200a4a2014-06-16 18:39:09 -07001518 virtual uintptr_t PushHandle(mirror::Object* /* ptr */) {
Andreas Gampec147b002014-03-06 18:11:06 -08001519 return reinterpret_cast<uintptr_t>(nullptr);
1520 }
1521
Andreas Gampec200a4a2014-06-16 18:39:09 -07001522 protected:
Andreas Gampec147b002014-03-06 18:11:06 -08001523 uint32_t num_stack_entries_;
1524};
1525
Andreas Gampec200a4a2014-06-16 18:39:09 -07001526class ComputeGenericJniFrameSize FINAL : public ComputeNativeCallFrameSize {
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001527 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001528 ComputeGenericJniFrameSize() : num_handle_scope_references_(0) {}
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001529
Andreas Gampec200a4a2014-06-16 18:39:09 -07001530 // Lays out the callee-save frame. Assumes that the incorrect frame corresponding to RefsAndArgs
1531 // is at *m = sp. Will update to point to the bottom of the save frame.
1532 //
1533 // Note: assumes ComputeAll() has been run before.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001534 void LayoutCalleeSaveFrame(Thread* self, ArtMethod*** m, void* sp, HandleScope** handle_scope)
Mathieu Chartier90443472015-07-16 20:32:27 -07001535 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001536 ArtMethod* method = **m;
1537
1538 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), sizeof(void*));
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001539
Andreas Gampec200a4a2014-06-16 18:39:09 -07001540 uint8_t* sp8 = reinterpret_cast<uint8_t*>(sp);
1541
1542 // First, fix up the layout of the callee-save frame.
1543 // We have to squeeze in the HandleScope, and relocate the method pointer.
1544
1545 // "Free" the slot for the method.
Ian Rogers13735952014-10-08 12:43:28 -07001546 sp8 += sizeof(void*); // In the callee-save frame we use a full pointer.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001547
1548 // Under the callee saves put handle scope and new method stack reference.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001549 size_t handle_scope_size = HandleScope::SizeOf(num_handle_scope_references_);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001550 size_t scope_and_method = handle_scope_size + sizeof(ArtMethod*);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001551
1552 sp8 -= scope_and_method;
1553 // Align by kStackAlignment.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001554 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
Andreas Gampec200a4a2014-06-16 18:39:09 -07001555
Mathieu Chartiere401d142015-04-22 13:56:20 -07001556 uint8_t* sp8_table = sp8 + sizeof(ArtMethod*);
Ian Rogers59c07062014-10-10 13:03:39 -07001557 *handle_scope = HandleScope::Create(sp8_table, self->GetTopHandleScope(),
1558 num_handle_scope_references_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001559
1560 // Add a slot for the method pointer, and fill it. Fix the pointer-pointer given to us.
1561 uint8_t* method_pointer = sp8;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001562 auto** new_method_ref = reinterpret_cast<ArtMethod**>(method_pointer);
1563 *new_method_ref = method;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001564 *m = new_method_ref;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001565 }
1566
Andreas Gampec200a4a2014-06-16 18:39:09 -07001567 // Adds space for the cookie. Note: may leave stack unaligned.
Ian Rogers1428dce2014-10-21 15:02:15 -07001568 void LayoutCookie(uint8_t** sp) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001569 // Reference cookie and padding
1570 *sp -= 8;
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001571 }
1572
Andreas Gampec200a4a2014-06-16 18:39:09 -07001573 // Re-layout the callee-save frame (insert a handle-scope). Then add space for the cookie.
1574 // Returns the new bottom. Note: this may be unaligned.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001575 uint8_t* LayoutJNISaveFrame(Thread* self, ArtMethod*** m, void* sp, HandleScope** handle_scope)
Mathieu Chartier90443472015-07-16 20:32:27 -07001576 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001577 // First, fix up the layout of the callee-save frame.
1578 // We have to squeeze in the HandleScope, and relocate the method pointer.
Ian Rogers59c07062014-10-10 13:03:39 -07001579 LayoutCalleeSaveFrame(self, m, sp, handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001580
1581 // The bottom of the callee-save frame is now where the method is, *m.
1582 uint8_t* sp8 = reinterpret_cast<uint8_t*>(*m);
1583
1584 // Add space for cookie.
1585 LayoutCookie(&sp8);
1586
1587 return sp8;
1588 }
1589
1590 // WARNING: After this, *sp won't be pointing to the method anymore!
Mathieu Chartiere401d142015-04-22 13:56:20 -07001591 uint8_t* ComputeLayout(Thread* self, ArtMethod*** m, const char* shorty, uint32_t shorty_len,
1592 HandleScope** handle_scope, uintptr_t** start_stack, uintptr_t** start_gpr,
1593 uint32_t** start_fpr)
Mathieu Chartier90443472015-07-16 20:32:27 -07001594 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001595 Walk(shorty, shorty_len);
1596
1597 // JNI part.
Ian Rogers59c07062014-10-10 13:03:39 -07001598 uint8_t* sp8 = LayoutJNISaveFrame(self, m, reinterpret_cast<void*>(*m), handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001599
1600 sp8 = LayoutNativeCall(sp8, start_stack, start_gpr, start_fpr);
1601
1602 // Return the new bottom.
1603 return sp8;
1604 }
1605
1606 uintptr_t PushHandle(mirror::Object* /* ptr */) OVERRIDE;
1607
1608 // Add JNIEnv* and jobj/jclass before the shorty-derived elements.
1609 void WalkHeader(BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) OVERRIDE
Mathieu Chartier90443472015-07-16 20:32:27 -07001610 SHARED_REQUIRES(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001611
1612 private:
1613 uint32_t num_handle_scope_references_;
1614};
1615
1616uintptr_t ComputeGenericJniFrameSize::PushHandle(mirror::Object* /* ptr */) {
1617 num_handle_scope_references_++;
1618 return reinterpret_cast<uintptr_t>(nullptr);
1619}
1620
1621void ComputeGenericJniFrameSize::WalkHeader(
1622 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) {
1623 // JNIEnv
1624 sm->AdvancePointer(nullptr);
1625
1626 // Class object or this as first argument
1627 sm->AdvanceHandleScope(reinterpret_cast<mirror::Object*>(0x12345678));
1628}
1629
1630// Class to push values to three separate regions. Used to fill the native call part. Adheres to
1631// the template requirements of BuildGenericJniFrameStateMachine.
1632class FillNativeCall {
1633 public:
1634 FillNativeCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) :
1635 cur_gpr_reg_(gpr_regs), cur_fpr_reg_(fpr_regs), cur_stack_arg_(stack_args) {}
1636
1637 virtual ~FillNativeCall() {}
1638
1639 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) {
1640 cur_gpr_reg_ = gpr_regs;
1641 cur_fpr_reg_ = fpr_regs;
1642 cur_stack_arg_ = stack_args;
Andreas Gampec147b002014-03-06 18:11:06 -08001643 }
1644
1645 void PushGpr(uintptr_t val) {
1646 *cur_gpr_reg_ = val;
1647 cur_gpr_reg_++;
1648 }
1649
1650 void PushFpr4(float val) {
1651 *cur_fpr_reg_ = val;
1652 cur_fpr_reg_++;
1653 }
1654
1655 void PushFpr8(uint64_t val) {
1656 uint64_t* tmp = reinterpret_cast<uint64_t*>(cur_fpr_reg_);
1657 *tmp = val;
1658 cur_fpr_reg_ += 2;
1659 }
1660
1661 void PushStack(uintptr_t val) {
1662 *cur_stack_arg_ = val;
1663 cur_stack_arg_++;
1664 }
1665
Mathieu Chartier90443472015-07-16 20:32:27 -07001666 virtual uintptr_t PushHandle(mirror::Object*) SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001667 LOG(FATAL) << "(Non-JNI) Native call does not use handles.";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001668 UNREACHABLE();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001669 }
1670
1671 private:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001672 uintptr_t* cur_gpr_reg_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001673 uint32_t* cur_fpr_reg_;
1674 uintptr_t* cur_stack_arg_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001675};
Andreas Gampec147b002014-03-06 18:11:06 -08001676
Andreas Gampec200a4a2014-06-16 18:39:09 -07001677// Visits arguments on the stack placing them into a region lower down the stack for the benefit
1678// of transitioning into native code.
1679class BuildGenericJniFrameVisitor FINAL : public QuickArgumentVisitor {
1680 public:
Ian Rogers59c07062014-10-10 13:03:39 -07001681 BuildGenericJniFrameVisitor(Thread* self, bool is_static, const char* shorty, uint32_t shorty_len,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001682 ArtMethod*** sp)
Andreas Gampec200a4a2014-06-16 18:39:09 -07001683 : QuickArgumentVisitor(*sp, is_static, shorty, shorty_len),
1684 jni_call_(nullptr, nullptr, nullptr, nullptr), sm_(&jni_call_) {
1685 ComputeGenericJniFrameSize fsc;
1686 uintptr_t* start_gpr_reg;
1687 uint32_t* start_fpr_reg;
1688 uintptr_t* start_stack_arg;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001689 bottom_of_used_area_ = fsc.ComputeLayout(self, sp, shorty, shorty_len,
Ian Rogers59c07062014-10-10 13:03:39 -07001690 &handle_scope_,
1691 &start_stack_arg,
Andreas Gampec200a4a2014-06-16 18:39:09 -07001692 &start_gpr_reg, &start_fpr_reg);
1693
Andreas Gampec200a4a2014-06-16 18:39:09 -07001694 jni_call_.Reset(start_gpr_reg, start_fpr_reg, start_stack_arg, handle_scope_);
1695
1696 // jni environment is always first argument
1697 sm_.AdvancePointer(self->GetJniEnv());
1698
1699 if (is_static) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001700 sm_.AdvanceHandleScope((**sp)->GetDeclaringClass());
Andreas Gampec200a4a2014-06-16 18:39:09 -07001701 }
1702 }
1703
Mathieu Chartier90443472015-07-16 20:32:27 -07001704 void Visit() SHARED_REQUIRES(Locks::mutator_lock_) OVERRIDE;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001705
Mathieu Chartier90443472015-07-16 20:32:27 -07001706 void FinalizeHandleScope(Thread* self) SHARED_REQUIRES(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001707
1708 StackReference<mirror::Object>* GetFirstHandleScopeEntry()
Mathieu Chartier90443472015-07-16 20:32:27 -07001709 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001710 return handle_scope_->GetHandle(0).GetReference();
1711 }
1712
Mathieu Chartier90443472015-07-16 20:32:27 -07001713 jobject GetFirstHandleScopeJObject() const SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001714 return handle_scope_->GetHandle(0).ToJObject();
1715 }
1716
Ian Rogers1428dce2014-10-21 15:02:15 -07001717 void* GetBottomOfUsedArea() const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001718 return bottom_of_used_area_;
1719 }
1720
1721 private:
1722 // A class to fill a JNI call. Adds reference/handle-scope management to FillNativeCall.
1723 class FillJniCall FINAL : public FillNativeCall {
1724 public:
1725 FillJniCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args,
1726 HandleScope* handle_scope) : FillNativeCall(gpr_regs, fpr_regs, stack_args),
1727 handle_scope_(handle_scope), cur_entry_(0) {}
1728
Mathieu Chartier90443472015-07-16 20:32:27 -07001729 uintptr_t PushHandle(mirror::Object* ref) OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001730
1731 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args, HandleScope* scope) {
1732 FillNativeCall::Reset(gpr_regs, fpr_regs, stack_args);
1733 handle_scope_ = scope;
1734 cur_entry_ = 0U;
1735 }
1736
Mathieu Chartier90443472015-07-16 20:32:27 -07001737 void ResetRemainingScopeSlots() SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001738 // Initialize padding entries.
1739 size_t expected_slots = handle_scope_->NumberOfReferences();
1740 while (cur_entry_ < expected_slots) {
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07001741 handle_scope_->GetMutableHandle(cur_entry_++).Assign(nullptr);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001742 }
1743 DCHECK_NE(cur_entry_, 0U);
1744 }
1745
1746 private:
1747 HandleScope* handle_scope_;
1748 size_t cur_entry_;
1749 };
1750
1751 HandleScope* handle_scope_;
1752 FillJniCall jni_call_;
1753 void* bottom_of_used_area_;
1754
1755 BuildNativeCallFrameStateMachine<FillJniCall> sm_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001756
1757 DISALLOW_COPY_AND_ASSIGN(BuildGenericJniFrameVisitor);
1758};
1759
Andreas Gampec200a4a2014-06-16 18:39:09 -07001760uintptr_t BuildGenericJniFrameVisitor::FillJniCall::PushHandle(mirror::Object* ref) {
1761 uintptr_t tmp;
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07001762 MutableHandle<mirror::Object> h = handle_scope_->GetMutableHandle(cur_entry_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001763 h.Assign(ref);
1764 tmp = reinterpret_cast<uintptr_t>(h.ToJObject());
1765 cur_entry_++;
1766 return tmp;
1767}
1768
Ian Rogers9758f792014-03-13 09:02:55 -07001769void BuildGenericJniFrameVisitor::Visit() {
1770 Primitive::Type type = GetParamPrimitiveType();
1771 switch (type) {
1772 case Primitive::kPrimLong: {
1773 jlong long_arg;
1774 if (IsSplitLongOrDouble()) {
1775 long_arg = ReadSplitLongParam();
1776 } else {
1777 long_arg = *reinterpret_cast<jlong*>(GetParamAddress());
1778 }
1779 sm_.AdvanceLong(long_arg);
1780 break;
1781 }
1782 case Primitive::kPrimDouble: {
1783 uint64_t double_arg;
1784 if (IsSplitLongOrDouble()) {
1785 // Read into union so that we don't case to a double.
1786 double_arg = ReadSplitLongParam();
1787 } else {
1788 double_arg = *reinterpret_cast<uint64_t*>(GetParamAddress());
1789 }
1790 sm_.AdvanceDouble(double_arg);
1791 break;
1792 }
1793 case Primitive::kPrimNot: {
1794 StackReference<mirror::Object>* stack_ref =
1795 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001796 sm_.AdvanceHandleScope(stack_ref->AsMirrorPtr());
Ian Rogers9758f792014-03-13 09:02:55 -07001797 break;
1798 }
1799 case Primitive::kPrimFloat:
1800 sm_.AdvanceFloat(*reinterpret_cast<float*>(GetParamAddress()));
1801 break;
1802 case Primitive::kPrimBoolean: // Fall-through.
1803 case Primitive::kPrimByte: // Fall-through.
1804 case Primitive::kPrimChar: // Fall-through.
1805 case Primitive::kPrimShort: // Fall-through.
1806 case Primitive::kPrimInt: // Fall-through.
1807 sm_.AdvanceInt(*reinterpret_cast<jint*>(GetParamAddress()));
1808 break;
1809 case Primitive::kPrimVoid:
1810 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -07001811 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -07001812 }
1813}
1814
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001815void BuildGenericJniFrameVisitor::FinalizeHandleScope(Thread* self) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001816 // Clear out rest of the scope.
1817 jni_call_.ResetRemainingScopeSlots();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001818 // Install HandleScope.
1819 self->PushHandleScope(handle_scope_);
Ian Rogers9758f792014-03-13 09:02:55 -07001820}
1821
Ian Rogers04c31d22014-07-07 21:44:06 -07001822#if defined(__arm__) || defined(__aarch64__)
Andreas Gampe90546832014-03-12 18:07:19 -07001823extern "C" void* artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07001824#else
1825extern "C" void* artFindNativeMethod(Thread* self);
1826#endif
Andreas Gampe90546832014-03-12 18:07:19 -07001827
Andreas Gampead615172014-04-04 16:20:13 -07001828uint64_t artQuickGenericJniEndJNIRef(Thread* self, uint32_t cookie, jobject l, jobject lock) {
1829 if (lock != nullptr) {
1830 return reinterpret_cast<uint64_t>(JniMethodEndWithReferenceSynchronized(l, cookie, lock, self));
1831 } else {
1832 return reinterpret_cast<uint64_t>(JniMethodEndWithReference(l, cookie, self));
1833 }
1834}
1835
1836void artQuickGenericJniEndJNINonRef(Thread* self, uint32_t cookie, jobject lock) {
1837 if (lock != nullptr) {
1838 JniMethodEndSynchronized(cookie, lock, self);
1839 } else {
1840 JniMethodEnd(cookie, self);
1841 }
1842}
1843
Andreas Gampec147b002014-03-06 18:11:06 -08001844/*
1845 * Initializes an alloca region assumed to be directly below sp for a native call:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001846 * 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 -08001847 * The final element on the stack is a pointer to the native code.
1848 *
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001849 * On entry, the stack has a standard callee-save frame above sp, and an alloca below it.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001850 * We need to fix this, as the handle scope needs to go into the callee-save frame.
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001851 *
Andreas Gampec147b002014-03-06 18:11:06 -08001852 * The return of this function denotes:
1853 * 1) How many bytes of the alloca can be released, if the value is non-negative.
1854 * 2) An error, if the value is negative.
1855 */
Mathieu Chartiere401d142015-04-22 13:56:20 -07001856extern "C" TwoWordReturn artQuickGenericJniTrampoline(Thread* self, ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -07001857 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001858 ArtMethod* called = *sp;
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001859 DCHECK(called->IsNative()) << PrettyMethod(called, true);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001860 uint32_t shorty_len = 0;
1861 const char* shorty = called->GetShorty(&shorty_len);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001862
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001863 // Run the visitor and update sp.
Ian Rogers59c07062014-10-10 13:03:39 -07001864 BuildGenericJniFrameVisitor visitor(self, called->IsStatic(), shorty, shorty_len, &sp);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001865 visitor.VisitArguments();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001866 visitor.FinalizeHandleScope(self);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001867
Andreas Gampec200a4a2014-06-16 18:39:09 -07001868 // Fix up managed-stack things in Thread.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001869 self->SetTopOfStack(sp);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001870
Ian Rogerse0dcd462014-03-08 15:21:04 -08001871 self->VerifyStack();
1872
Andreas Gampe90546832014-03-12 18:07:19 -07001873 // Start JNI, save the cookie.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001874 uint32_t cookie;
1875 if (called->IsSynchronized()) {
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001876 cookie = JniMethodStartSynchronized(visitor.GetFirstHandleScopeJObject(), self);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001877 if (self->IsExceptionPending()) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001878 self->PopHandleScope();
Andreas Gampec147b002014-03-06 18:11:06 -08001879 // A negative value denotes an error.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001880 return GetTwoWordFailureValue();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001881 }
1882 } else {
1883 cookie = JniMethodStart(self);
1884 }
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001885 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
Ian Rogerse0dcd462014-03-08 15:21:04 -08001886 *(sp32 - 1) = cookie;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001887
Andreas Gampe90546832014-03-12 18:07:19 -07001888 // Retrieve the stored native code.
Mathieu Chartier2d721012014-11-10 11:08:06 -08001889 void* nativeCode = called->GetEntryPointFromJni();
Andreas Gampe90546832014-03-12 18:07:19 -07001890
Andreas Gampe9a6a99a2014-03-14 07:52:20 -07001891 // There are two cases for the content of nativeCode:
1892 // 1) Pointer to the native function.
1893 // 2) Pointer to the trampoline for native code binding.
1894 // In the second case, we need to execute the binding and continue with the actual native function
1895 // pointer.
Andreas Gampe90546832014-03-12 18:07:19 -07001896 DCHECK(nativeCode != nullptr);
1897 if (nativeCode == GetJniDlsymLookupStub()) {
Ian Rogers04c31d22014-07-07 21:44:06 -07001898#if defined(__arm__) || defined(__aarch64__)
Andreas Gampe90546832014-03-12 18:07:19 -07001899 nativeCode = artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07001900#else
1901 nativeCode = artFindNativeMethod(self);
1902#endif
Andreas Gampe90546832014-03-12 18:07:19 -07001903
1904 if (nativeCode == nullptr) {
1905 DCHECK(self->IsExceptionPending()); // There should be an exception pending now.
Andreas Gampead615172014-04-04 16:20:13 -07001906
1907 // End JNI, as the assembly will move to deliver the exception.
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001908 jobject lock = called->IsSynchronized() ? visitor.GetFirstHandleScopeJObject() : nullptr;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001909 if (shorty[0] == 'L') {
Andreas Gampead615172014-04-04 16:20:13 -07001910 artQuickGenericJniEndJNIRef(self, cookie, nullptr, lock);
1911 } else {
1912 artQuickGenericJniEndJNINonRef(self, cookie, lock);
1913 }
1914
Andreas Gampec200a4a2014-06-16 18:39:09 -07001915 return GetTwoWordFailureValue();
Andreas Gampe90546832014-03-12 18:07:19 -07001916 }
1917 // Note that the native code pointer will be automatically set by artFindNativeMethod().
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001918 }
1919
Andreas Gampec200a4a2014-06-16 18:39:09 -07001920 // Return native code addr(lo) and bottom of alloca address(hi).
1921 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(visitor.GetBottomOfUsedArea()),
1922 reinterpret_cast<uintptr_t>(nativeCode));
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001923}
1924
1925/*
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001926 * Is called after the native JNI code. Responsible for cleanup (handle scope, saved state) and
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001927 * unlocking.
1928 */
Andreas Gampec200a4a2014-06-16 18:39:09 -07001929extern "C" uint64_t artQuickGenericJniEndTrampoline(Thread* self, jvalue result, uint64_t result_f)
Mathieu Chartier90443472015-07-16 20:32:27 -07001930 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001931 ArtMethod** sp = self->GetManagedStack()->GetTopQuickFrame();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001932 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001933 ArtMethod* called = *sp;
Ian Rogerse0dcd462014-03-08 15:21:04 -08001934 uint32_t cookie = *(sp32 - 1);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001935
Andreas Gampead615172014-04-04 16:20:13 -07001936 jobject lock = nullptr;
1937 if (called->IsSynchronized()) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001938 HandleScope* table = reinterpret_cast<HandleScope*>(reinterpret_cast<uint8_t*>(sp)
Mathieu Chartiere401d142015-04-22 13:56:20 -07001939 + sizeof(*sp));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001940 lock = table->GetHandle(0).ToJObject();
Andreas Gampead615172014-04-04 16:20:13 -07001941 }
1942
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001943 char return_shorty_char = called->GetShorty()[0];
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001944
1945 if (return_shorty_char == 'L') {
Andreas Gampead615172014-04-04 16:20:13 -07001946 return artQuickGenericJniEndJNIRef(self, cookie, result.l, lock);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001947 } else {
Andreas Gampead615172014-04-04 16:20:13 -07001948 artQuickGenericJniEndJNINonRef(self, cookie, lock);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001949
1950 switch (return_shorty_char) {
Nicolas Geoffray54accbc2014-08-13 03:40:45 +01001951 case 'F': {
1952 if (kRuntimeISA == kX86) {
1953 // Convert back the result to float.
Roland Levillainda4d79b2015-03-24 14:36:11 +00001954 double d = bit_cast<double, uint64_t>(result_f);
1955 return bit_cast<uint32_t, float>(static_cast<float>(d));
Nicolas Geoffray54accbc2014-08-13 03:40:45 +01001956 } else {
1957 return result_f;
1958 }
1959 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001960 case 'D':
1961 return result_f;
1962 case 'Z':
1963 return result.z;
1964 case 'B':
1965 return result.b;
1966 case 'C':
1967 return result.c;
1968 case 'S':
1969 return result.s;
1970 case 'I':
1971 return result.i;
1972 case 'J':
1973 return result.j;
1974 case 'V':
1975 return 0;
1976 default:
1977 LOG(FATAL) << "Unexpected return shorty character " << return_shorty_char;
1978 return 0;
1979 }
1980 }
Andreas Gampe2da88232014-02-27 12:26:20 -08001981}
1982
Andreas Gamped58342c2014-06-05 14:18:08 -07001983// We use TwoWordReturn to optimize scalar returns. We use the hi value for code, and the lo value
1984// for the method pointer.
Andreas Gampe51f76352014-05-21 08:28:48 -07001985//
Andreas Gamped58342c2014-06-05 14:18:08 -07001986// 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 -07001987// to hold the mutator lock (see SHARED_REQUIRES(Locks::mutator_lock_) annotations).
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001988
1989template<InvokeType type, bool access_check>
Mathieu Chartiere401d142015-04-22 13:56:20 -07001990static TwoWordReturn artInvokeCommon(uint32_t method_idx, mirror::Object* this_object, Thread* self,
1991 ArtMethod** sp) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001992 ScopedQuickEntrypointChecks sqec(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001993 DCHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs));
1994 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
1995 ArtMethod* method = FindMethodFast(method_idx, this_object, caller_method, access_check, type);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001996 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001997 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()->GetDexFile();
1998 uint32_t shorty_len;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001999 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(method_idx), &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002000 {
2001 // Remember the args in case a GC happens in FindMethodFromCode.
2002 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
2003 RememberForGcArgumentVisitor visitor(sp, type == kStatic, shorty, shorty_len, &soa);
2004 visitor.VisitArguments();
Andreas Gampe3a357142015-08-07 17:20:11 -07002005 method = FindMethodFromCode<type, access_check>(method_idx, &this_object, caller_method,
Mathieu Chartier0cd81352014-05-22 16:48:55 -07002006 self);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002007 visitor.FixupReferences();
2008 }
2009
Ian Rogerse0a02da2014-12-02 14:10:53 -08002010 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002011 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07002012 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002013 }
2014 }
2015 DCHECK(!self->IsExceptionPending());
2016 const void* code = method->GetEntryPointFromQuickCompiledCode();
2017
2018 // When we return, the caller will branch to this address, so it had better not be 0!
Ian Rogerse0a02da2014-12-02 14:10:53 -08002019 DCHECK(code != nullptr) << "Code was null in method: " << PrettyMethod(method)
Andreas Gampec200a4a2014-06-16 18:39:09 -07002020 << " location: "
2021 << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07002022
Andreas Gamped58342c2014-06-05 14:18:08 -07002023 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
2024 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002025}
2026
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01002027// Explicit artInvokeCommon template function declarations to please analysis tool.
2028#define EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(type, access_check) \
Mathieu Chartier90443472015-07-16 20:32:27 -07002029 template SHARED_REQUIRES(Locks::mutator_lock_) \
Mathieu Chartiere401d142015-04-22 13:56:20 -07002030 TwoWordReturn artInvokeCommon<type, access_check>( \
2031 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01002032
2033EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, false);
2034EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, true);
2035EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, false);
2036EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, true);
2037EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, false);
2038EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, true);
2039EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, false);
2040EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, true);
2041EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, false);
2042EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, true);
2043#undef EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL
2044
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002045// See comments in runtime_support_asm.S
Andreas Gampec200a4a2014-06-16 18:39:09 -07002046extern "C" TwoWordReturn artInvokeInterfaceTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002047 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -07002048 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002049 return artInvokeCommon<kInterface, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002050}
2051
Andreas Gampec200a4a2014-06-16 18:39:09 -07002052extern "C" TwoWordReturn artInvokeDirectTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002053 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -07002054 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002055 return artInvokeCommon<kDirect, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002056}
2057
Andreas Gampec200a4a2014-06-16 18:39:09 -07002058extern "C" TwoWordReturn artInvokeStaticTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002059 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -07002060 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002061 return artInvokeCommon<kStatic, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002062}
2063
Andreas Gampec200a4a2014-06-16 18:39:09 -07002064extern "C" TwoWordReturn artInvokeSuperTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002065 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -07002066 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002067 return artInvokeCommon<kSuper, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002068}
2069
Andreas Gampec200a4a2014-06-16 18:39:09 -07002070extern "C" TwoWordReturn artInvokeVirtualTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002071 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -07002072 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002073 return artInvokeCommon<kVirtual, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002074}
2075
2076// Determine target of interface dispatch. This object is known non-null.
Nicolas Geoffray8ea18d02015-05-26 16:29:08 +01002077extern "C" TwoWordReturn artInvokeInterfaceTrampoline(uint32_t dex_method_idx,
Andreas Gampe51f76352014-05-21 08:28:48 -07002078 mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -07002079 Thread* self, ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -07002080 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002081 ScopedQuickEntrypointChecks sqec(self);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002082 // The optimizing compiler currently does not inline methods that have an interface
2083 // invocation. We use the outer method directly to avoid fetching a stack map, which is
2084 // more expensive.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002085 ArtMethod* caller_method = QuickArgumentVisitor::GetOuterMethod(sp);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002086 DCHECK_EQ(caller_method, QuickArgumentVisitor::GetCallingMethod(sp));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002087 ArtMethod* interface_method = caller_method->GetDexCacheResolvedMethod(
2088 dex_method_idx, sizeof(void*));
2089 DCHECK(interface_method != nullptr) << dex_method_idx << " " << PrettyMethod(caller_method);
2090 ArtMethod* method;
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002091 if (LIKELY(interface_method->GetDexMethodIndex() != DexFile::kDexNoIndex)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002092 method = this_object->GetClass()->FindVirtualMethodForInterface(
2093 interface_method, sizeof(void*));
Ian Rogerse0a02da2014-12-02 14:10:53 -08002094 if (UNLIKELY(method == nullptr)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002095 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(
2096 interface_method, this_object, caller_method);
Andreas Gamped58342c2014-06-05 14:18:08 -07002097 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002098 }
2099 } else {
Mathieu Chartier4edd8472015-06-01 10:47:36 -07002100 DCHECK_EQ(interface_method, Runtime::Current()->GetResolutionMethod());
Nicolas Geoffray8ea18d02015-05-26 16:29:08 +01002101 if (kIsDebugBuild) {
2102 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
2103 const DexFile::CodeItem* code = caller_method->GetCodeItem();
2104 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
2105 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
2106 Instruction::Code instr_code = instr->Opcode();
2107 CHECK(instr_code == Instruction::INVOKE_INTERFACE ||
2108 instr_code == Instruction::INVOKE_INTERFACE_RANGE)
2109 << "Unexpected call into interface trampoline: " << instr->DumpString(nullptr);
2110 if (instr_code == Instruction::INVOKE_INTERFACE) {
2111 CHECK_EQ(dex_method_idx, instr->VRegB_35c());
2112 } else {
2113 CHECK_EQ(instr_code, Instruction::INVOKE_INTERFACE_RANGE);
2114 CHECK_EQ(dex_method_idx, instr->VRegB_3rc());
2115 }
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002116 }
2117
Andreas Gampec200a4a2014-06-16 18:39:09 -07002118 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()
2119 ->GetDexFile();
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002120 uint32_t shorty_len;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002121 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(dex_method_idx),
2122 &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002123 {
2124 // Remember the args in case a GC happens in FindMethodFromCode.
2125 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
2126 RememberForGcArgumentVisitor visitor(sp, false, shorty, shorty_len, &soa);
2127 visitor.VisitArguments();
Andreas Gampe3a357142015-08-07 17:20:11 -07002128 method = FindMethodFromCode<kInterface, false>(dex_method_idx, &this_object, caller_method,
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002129 self);
2130 visitor.FixupReferences();
2131 }
2132
2133 if (UNLIKELY(method == nullptr)) {
2134 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07002135 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002136 }
2137 }
2138 const void* code = method->GetEntryPointFromQuickCompiledCode();
2139
2140 // When we return, the caller will branch to this address, so it had better not be 0!
Ian Rogerse0a02da2014-12-02 14:10:53 -08002141 DCHECK(code != nullptr) << "Code was null in method: " << PrettyMethod(method)
Andreas Gampec200a4a2014-06-16 18:39:09 -07002142 << " location: " << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07002143
Andreas Gamped58342c2014-06-05 14:18:08 -07002144 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
2145 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002146}
2147
Ian Rogers848871b2013-08-05 10:56:33 -07002148} // namespace art