blob: 377675ea8581c6c4e982d6758f47badacd189010 [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
Nicolas Geoffray6bc43742015-10-12 18:11:10 +010017#include "art_code.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070018#include "art_method-inl.h"
Ian Rogers848871b2013-08-05 10:56:33 -070019#include "callee_save_frame.h"
Dragos Sbirleabd136a22013-08-13 18:07:04 -070020#include "common_throws.h"
Ian Rogers848871b2013-08-05 10:56:33 -070021#include "dex_file-inl.h"
22#include "dex_instruction-inl.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070023#include "entrypoints/entrypoint_utils-inl.h"
Ian Rogers6f3dbba2014-10-14 17:41:57 -070024#include "entrypoints/runtime_asm_entrypoints.h"
Ian Rogers83883d72013-10-21 21:07:24 -070025#include "gc/accounting/card_table-inl.h"
Ian Rogers848871b2013-08-05 10:56:33 -070026#include "interpreter/interpreter.h"
Ian Rogerse0a02da2014-12-02 14:10:53 -080027#include "method_reference.h"
Ian Rogers848871b2013-08-05 10:56:33 -070028#include "mirror/class-inl.h"
Mathieu Chartier5f3ded42014-04-03 15:25:30 -070029#include "mirror/dex_cache-inl.h"
Mathieu Chartierfc58af42015-04-16 18:00:39 -070030#include "mirror/method.h"
Ian Rogers848871b2013-08-05 10:56:33 -070031#include "mirror/object-inl.h"
32#include "mirror/object_array-inl.h"
Andreas Gampe639bdd12015-06-03 11:22:45 -070033#include "quick_exception_handler.h"
Ian Rogers848871b2013-08-05 10:56:33 -070034#include "runtime.h"
Ian Rogers53b8b092014-03-13 23:45:53 -070035#include "scoped_thread_state_change.h"
Andreas Gampeb3025922015-09-01 14:45:00 -070036#include "stack.h"
Daniel Mihalyieb076692014-08-22 17:33:31 +020037#include "debugger.h"
Ian Rogers848871b2013-08-05 10:56:33 -070038
39namespace art {
40
41// Visits the arguments as saved to the stack by a Runtime::kRefAndArgs callee save frame.
42class QuickArgumentVisitor {
Ian Rogers936b37f2014-02-14 00:52:24 -080043 // Number of bytes for each out register in the caller method's frame.
44 static constexpr size_t kBytesStackArgLocation = 4;
Alexei Zavjalov41c507a2014-05-15 16:02:46 +070045 // Frame size in bytes of a callee-save frame for RefsAndArgs.
46 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize =
47 GetCalleeSaveFrameSize(kRuntimeISA, Runtime::kRefsAndArgs);
Ian Rogers848871b2013-08-05 10:56:33 -070048#if defined(__arm__)
49 // The callee save frame is pointed to by SP.
50 // | argN | |
51 // | ... | |
52 // | arg4 | |
53 // | arg3 spill | | Caller's frame
54 // | arg2 spill | |
55 // | arg1 spill | |
56 // | Method* | ---
57 // | LR |
Zheng Xu5667fdb2014-10-23 18:29:55 +080058 // | ... | 4x6 bytes callee saves
59 // | R3 |
60 // | R2 |
61 // | R1 |
62 // | S15 |
63 // | : |
64 // | S0 |
65 // | | 4x2 bytes padding
Ian Rogers848871b2013-08-05 10:56:33 -070066 // | Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -050067 static constexpr bool kSplitPairAcrossRegisterAndStack = kArm32QuickCodeUseSoftFloat;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +000068 static constexpr bool kAlignPairRegister = !kArm32QuickCodeUseSoftFloat;
Zheng Xu5667fdb2014-10-23 18:29:55 +080069 static constexpr bool kQuickSoftFloatAbi = kArm32QuickCodeUseSoftFloat;
70 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = !kArm32QuickCodeUseSoftFloat;
Goran Jakovljevicff734982015-08-24 12:58:55 +000071 static constexpr bool kQuickSkipOddFpRegisters = false;
Zheng Xu5667fdb2014-10-23 18:29:55 +080072 static constexpr size_t kNumQuickGprArgs = 3;
73 static constexpr size_t kNumQuickFprArgs = kArm32QuickCodeUseSoftFloat ? 0 : 16;
Andreas Gampe1a5c4062015-01-15 12:10:47 -080074 static constexpr bool kGprFprLockstep = false;
Zheng Xub551fdc2014-07-25 11:49:42 +080075 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset =
76 arm::ArmCalleeSaveFpr1Offset(Runtime::kRefsAndArgs); // Offset of first FPR arg.
77 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset =
78 arm::ArmCalleeSaveGpr1Offset(Runtime::kRefsAndArgs); // Offset of first GPR arg.
79 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset =
80 arm::ArmCalleeSaveLrOffset(Runtime::kRefsAndArgs); // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -080081 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +000082 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -080083 }
Stuart Monteithb95a5342014-03-12 13:32:32 +000084#elif defined(__aarch64__)
85 // The callee save frame is pointed to by SP.
86 // | argN | |
87 // | ... | |
88 // | arg4 | |
89 // | arg3 spill | | Caller's frame
90 // | arg2 spill | |
91 // | arg1 spill | |
92 // | Method* | ---
93 // | LR |
Zheng Xub551fdc2014-07-25 11:49:42 +080094 // | X29 |
Stuart Monteithb95a5342014-03-12 13:32:32 +000095 // | : |
Serban Constantinescu9bd88b02015-04-22 16:24:46 +010096 // | X20 |
Stuart Monteithb95a5342014-03-12 13:32:32 +000097 // | X7 |
98 // | : |
99 // | X1 |
Zheng Xub551fdc2014-07-25 11:49:42 +0800100 // | D7 |
Stuart Monteithb95a5342014-03-12 13:32:32 +0000101 // | : |
102 // | D0 |
103 // | | padding
104 // | Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500105 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000106 static constexpr bool kAlignPairRegister = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000107 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800108 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000109 static constexpr bool kQuickSkipOddFpRegisters = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000110 static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs.
111 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800112 static constexpr bool kGprFprLockstep = false;
Zheng Xub551fdc2014-07-25 11:49:42 +0800113 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset =
114 arm64::Arm64CalleeSaveFpr1Offset(Runtime::kRefsAndArgs); // Offset of first FPR arg.
115 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset =
116 arm64::Arm64CalleeSaveGpr1Offset(Runtime::kRefsAndArgs); // Offset of first GPR arg.
117 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset =
118 arm64::Arm64CalleeSaveLrOffset(Runtime::kRefsAndArgs); // Offset of return address.
Stuart Monteithb95a5342014-03-12 13:32:32 +0000119 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000120 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Stuart Monteithb95a5342014-03-12 13:32:32 +0000121 }
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800122#elif defined(__mips__) && !defined(__LP64__)
Ian Rogers848871b2013-08-05 10:56:33 -0700123 // The callee save frame is pointed to by SP.
124 // | argN | |
125 // | ... | |
126 // | arg4 | |
127 // | arg3 spill | | Caller's frame
128 // | arg2 spill | |
129 // | arg1 spill | |
130 // | Method* | ---
131 // | RA |
132 // | ... | callee saves
133 // | A3 | arg3
134 // | A2 | arg2
135 // | A1 | arg1
Goran Jakovljevicff734982015-08-24 12:58:55 +0000136 // | F15 |
137 // | F14 | f_arg1
138 // | F13 |
139 // | F12 | f_arg0
140 // | | padding
Ian Rogers848871b2013-08-05 10:56:33 -0700141 // | A0/Method* | <- sp
Goran Jakovljevicff734982015-08-24 12:58:55 +0000142 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
143 static constexpr bool kAlignPairRegister = true;
144 static constexpr bool kQuickSoftFloatAbi = false;
Zheng Xu5667fdb2014-10-23 18:29:55 +0800145 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000146 static constexpr bool kQuickSkipOddFpRegisters = true;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800147 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs.
Goran Jakovljevicff734982015-08-24 12:58:55 +0000148 static constexpr size_t kNumQuickFprArgs = 4; // 2 arguments passed in FPRs. Floats can be passed
149 // only in even numbered registers and each double
150 // occupies two registers.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800151 static constexpr bool kGprFprLockstep = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000152 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 16; // Offset of first FPR arg.
153 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 32; // Offset of first GPR arg.
154 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 76; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800155 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000156 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800157 }
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800158#elif defined(__mips__) && defined(__LP64__)
159 // The callee save frame is pointed to by SP.
160 // | argN | |
161 // | ... | |
162 // | arg4 | |
163 // | arg3 spill | | Caller's frame
164 // | arg2 spill | |
165 // | arg1 spill | |
166 // | Method* | ---
167 // | RA |
168 // | ... | callee saves
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800169 // | A7 | arg7
170 // | A6 | arg6
171 // | A5 | arg5
172 // | A4 | arg4
173 // | A3 | arg3
174 // | A2 | arg2
175 // | A1 | arg1
Goran Jakovljevicff734982015-08-24 12:58:55 +0000176 // | F19 | f_arg7
177 // | F18 | f_arg6
178 // | F17 | f_arg5
179 // | F16 | f_arg4
180 // | F15 | f_arg3
181 // | F14 | f_arg2
182 // | F13 | f_arg1
183 // | F12 | f_arg0
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800184 // | | padding
185 // | A0/Method* | <- sp
186 // NOTE: for Mip64, when A0 is skipped, F0 is also skipped.
Douglas Leungd18e0832015-02-09 15:22:26 -0800187 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800188 static constexpr bool kAlignPairRegister = false;
189 static constexpr bool kQuickSoftFloatAbi = false;
190 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000191 static constexpr bool kQuickSkipOddFpRegisters = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800192 static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs.
193 static constexpr size_t kNumQuickFprArgs = 7; // 7 arguments passed in FPRs.
194 static constexpr bool kGprFprLockstep = true;
195
196 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 24; // Offset of first FPR arg (F1).
197 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 80; // Offset of first GPR arg (A1).
198 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 200; // Offset of return address.
199 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
200 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
201 }
Ian Rogers848871b2013-08-05 10:56:33 -0700202#elif defined(__i386__)
203 // The callee save frame is pointed to by SP.
204 // | argN | |
205 // | ... | |
206 // | arg4 | |
207 // | arg3 spill | | Caller's frame
208 // | arg2 spill | |
209 // | arg1 spill | |
210 // | Method* | ---
211 // | Return |
212 // | EBP,ESI,EDI | callee saves
213 // | EBX | arg3
214 // | EDX | arg2
215 // | ECX | arg1
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000216 // | XMM3 | float arg 4
217 // | XMM2 | float arg 3
218 // | XMM1 | float arg 2
219 // | XMM0 | float arg 1
Ian Rogers848871b2013-08-05 10:56:33 -0700220 // | EAX/Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500221 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000222 static constexpr bool kAlignPairRegister = false;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000223 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800224 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000225 static constexpr bool kQuickSkipOddFpRegisters = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800226 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs.
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000227 static constexpr size_t kNumQuickFprArgs = 4; // 4 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800228 static constexpr bool kGprFprLockstep = false;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000229 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 4; // Offset of first FPR arg.
230 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 4 + 4*8; // Offset of first GPR arg.
231 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 28 + 4*8; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800232 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000233 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800234 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800235#elif defined(__x86_64__)
Ian Rogers936b37f2014-02-14 00:52:24 -0800236 // The callee save frame is pointed to by SP.
237 // | argN | |
238 // | ... | |
239 // | reg. arg spills | | Caller's frame
240 // | Method* | ---
241 // | Return |
242 // | R15 | callee save
243 // | R14 | callee save
244 // | R13 | callee save
245 // | R12 | callee save
246 // | R9 | arg5
247 // | R8 | arg4
248 // | RSI/R6 | arg1
249 // | RBP/R5 | callee save
250 // | RBX/R3 | callee save
251 // | RDX/R2 | arg2
252 // | RCX/R1 | arg3
253 // | XMM7 | float arg 8
254 // | XMM6 | float arg 7
255 // | XMM5 | float arg 6
256 // | XMM4 | float arg 5
257 // | XMM3 | float arg 4
258 // | XMM2 | float arg 3
259 // | XMM1 | float arg 2
260 // | XMM0 | float arg 1
261 // | Padding |
262 // | RDI/Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500263 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000264 static constexpr bool kAlignPairRegister = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800265 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800266 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000267 static constexpr bool kQuickSkipOddFpRegisters = false;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700268 static constexpr size_t kNumQuickGprArgs = 5; // 5 arguments passed in GPRs.
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700269 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800270 static constexpr bool kGprFprLockstep = false;
Ian Rogers936b37f2014-02-14 00:52:24 -0800271 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 16; // Offset of first FPR arg.
Serguei Katkovc3801912014-07-08 17:21:53 +0700272 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 80 + 4*8; // Offset of first GPR arg.
273 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 168 + 4*8; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800274 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
275 switch (gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000276 case 0: return (4 * GetBytesPerGprSpillLocation(kRuntimeISA));
277 case 1: return (1 * GetBytesPerGprSpillLocation(kRuntimeISA));
278 case 2: return (0 * GetBytesPerGprSpillLocation(kRuntimeISA));
279 case 3: return (5 * GetBytesPerGprSpillLocation(kRuntimeISA));
280 case 4: return (6 * GetBytesPerGprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800281 default:
Andreas Gampec200a4a2014-06-16 18:39:09 -0700282 LOG(FATAL) << "Unexpected GPR index: " << gpr_index;
283 return 0;
Ian Rogers936b37f2014-02-14 00:52:24 -0800284 }
285 }
Ian Rogers848871b2013-08-05 10:56:33 -0700286#else
287#error "Unsupported architecture"
Ian Rogers848871b2013-08-05 10:56:33 -0700288#endif
289
Ian Rogers936b37f2014-02-14 00:52:24 -0800290 public:
Sebastien Hertza836bc92014-11-25 16:30:53 +0100291 // Special handling for proxy methods. Proxy methods are instance methods so the
292 // 'this' object is the 1st argument. They also have the same frame layout as the
293 // kRefAndArgs runtime method. Since 'this' is a reference, it is located in the
294 // 1st GPR.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700295 static mirror::Object* GetProxyThisObject(ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -0700296 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700297 CHECK((*sp)->IsProxyMethod());
Nicolas Geoffray6bc43742015-10-12 18:11:10 +0100298 CHECK_EQ(kQuickCalleeSaveFrame_RefAndArgs_FrameSize,
299 GetCallingCodeFrom(sp).GetFrameSizeInBytes());
Sebastien Hertza836bc92014-11-25 16:30:53 +0100300 CHECK_GT(kNumQuickGprArgs, 0u);
301 constexpr uint32_t kThisGprIndex = 0u; // 'this' is in the 1st GPR.
302 size_t this_arg_offset = kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset +
303 GprIndexToGprOffset(kThisGprIndex);
304 uint8_t* this_arg_address = reinterpret_cast<uint8_t*>(sp) + this_arg_offset;
305 return reinterpret_cast<StackReference<mirror::Object>*>(this_arg_address)->AsMirrorPtr();
306 }
307
Mathieu Chartier90443472015-07-16 20:32:27 -0700308 static ArtMethod* GetCallingMethod(ArtMethod** sp) SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700309 DCHECK((*sp)->IsCalleeSaveMethod());
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +0100310 return GetCalleeSaveMethodCaller(sp, Runtime::kRefsAndArgs);
311 }
312
Mathieu Chartier90443472015-07-16 20:32:27 -0700313 static ArtMethod* GetOuterMethod(ArtMethod** sp) SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700314 DCHECK((*sp)->IsCalleeSaveMethod());
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100315 uint8_t* previous_sp =
316 reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700317 return *reinterpret_cast<ArtMethod**>(previous_sp);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100318 }
319
Mathieu Chartier90443472015-07-16 20:32:27 -0700320 static uint32_t GetCallingDexPc(ArtMethod** sp) SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700321 DCHECK((*sp)->IsCalleeSaveMethod());
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100322 const size_t callee_frame_size = GetCalleeSaveFrameSize(kRuntimeISA, Runtime::kRefsAndArgs);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700323 ArtMethod** caller_sp = reinterpret_cast<ArtMethod**>(
324 reinterpret_cast<uintptr_t>(sp) + callee_frame_size);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100325 uintptr_t outer_pc = QuickArgumentVisitor::GetCallingPc(sp);
Nicolas Geoffray6bc43742015-10-12 18:11:10 +0100326 uintptr_t outer_pc_offset = GetCallingCodeFrom(caller_sp).NativeQuickPcOffset(outer_pc);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100327
Nicolas Geoffray6bc43742015-10-12 18:11:10 +0100328 if (GetCallingCodeFrom(caller_sp).IsOptimized(sizeof(void*))) {
329 CodeInfo code_info = GetCallingCodeFrom(caller_sp).GetOptimizedCodeInfo();
David Brazdilf677ebf2015-05-29 16:29:43 +0100330 StackMapEncoding encoding = code_info.ExtractEncoding();
331 StackMap stack_map = code_info.GetStackMapForNativePcOffset(outer_pc_offset, encoding);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100332 DCHECK(stack_map.IsValid());
David Brazdilf677ebf2015-05-29 16:29:43 +0100333 if (stack_map.HasInlineInfo(encoding)) {
334 InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map, encoding);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100335 return inline_info.GetDexPcAtDepth(inline_info.GetDepth() - 1);
336 } else {
David Brazdilf677ebf2015-05-29 16:29:43 +0100337 return stack_map.GetDexPc(encoding);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100338 }
339 } else {
Nicolas Geoffray6bc43742015-10-12 18:11:10 +0100340 return GetCallingCodeFrom(caller_sp).ToDexPc(outer_pc);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100341 }
Ian Rogers848871b2013-08-05 10:56:33 -0700342 }
343
Ian Rogers936b37f2014-02-14 00:52:24 -0800344 // For the given quick ref and args quick frame, return the caller's PC.
Mathieu Chartier90443472015-07-16 20:32:27 -0700345 static uintptr_t GetCallingPc(ArtMethod** sp) SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700346 DCHECK((*sp)->IsCalleeSaveMethod());
Ian Rogers13735952014-10-08 12:43:28 -0700347 uint8_t* lr = reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_LrOffset;
Ian Rogers848871b2013-08-05 10:56:33 -0700348 return *reinterpret_cast<uintptr_t*>(lr);
349 }
350
Mathieu Chartiere401d142015-04-22 13:56:20 -0700351 QuickArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty,
Mathieu Chartier90443472015-07-16 20:32:27 -0700352 uint32_t shorty_len) SHARED_REQUIRES(Locks::mutator_lock_) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700353 is_static_(is_static), shorty_(shorty), shorty_len_(shorty_len),
Ian Rogers13735952014-10-08 12:43:28 -0700354 gpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset),
355 fpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset),
356 stack_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize
Mathieu Chartiere401d142015-04-22 13:56:20 -0700357 + sizeof(ArtMethod*)), // Skip ArtMethod*.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800358 gpr_index_(0), fpr_index_(0), fpr_double_index_(0), stack_index_(0),
359 cur_type_(Primitive::kPrimVoid), is_split_long_or_double_(false) {
Andreas Gampe575e78c2014-11-03 23:41:03 -0800360 static_assert(kQuickSoftFloatAbi == (kNumQuickFprArgs == 0),
361 "Number of Quick FPR arguments unexpected");
362 static_assert(!(kQuickSoftFloatAbi && kQuickDoubleRegAlignedFloatBackFilled),
363 "Double alignment unexpected");
Zheng Xu5667fdb2014-10-23 18:29:55 +0800364 // For register alignment, we want to assume that counters(fpr_double_index_) are even if the
365 // next register is even.
Andreas Gampe575e78c2014-11-03 23:41:03 -0800366 static_assert(!kQuickDoubleRegAlignedFloatBackFilled || kNumQuickFprArgs % 2 == 0,
367 "Number of Quick FPR arguments not even");
Mathieu Chartiere401d142015-04-22 13:56:20 -0700368 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), sizeof(void*));
Zheng Xu5667fdb2014-10-23 18:29:55 +0800369 }
Ian Rogers848871b2013-08-05 10:56:33 -0700370
371 virtual ~QuickArgumentVisitor() {}
372
373 virtual void Visit() = 0;
374
Ian Rogers936b37f2014-02-14 00:52:24 -0800375 Primitive::Type GetParamPrimitiveType() const {
376 return cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700377 }
378
Ian Rogers13735952014-10-08 12:43:28 -0700379 uint8_t* GetParamAddress() const {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800380 if (!kQuickSoftFloatAbi) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800381 Primitive::Type type = GetParamPrimitiveType();
382 if (UNLIKELY((type == Primitive::kPrimDouble) || (type == Primitive::kPrimFloat))) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800383 if (type == Primitive::kPrimDouble && kQuickDoubleRegAlignedFloatBackFilled) {
384 if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) {
385 return fpr_args_ + (fpr_double_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
386 }
387 } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000388 return fpr_args_ + (fpr_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800389 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700390 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers936b37f2014-02-14 00:52:24 -0800391 }
392 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800393 if (gpr_index_ < kNumQuickGprArgs) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800394 return gpr_args_ + GprIndexToGprOffset(gpr_index_);
395 }
396 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700397 }
398
399 bool IsSplitLongOrDouble() const {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700400 if ((GetBytesPerGprSpillLocation(kRuntimeISA) == 4) ||
401 (GetBytesPerFprSpillLocation(kRuntimeISA) == 4)) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800402 return is_split_long_or_double_;
403 } else {
404 return false; // An optimization for when GPR and FPRs are 64bit.
405 }
Ian Rogers848871b2013-08-05 10:56:33 -0700406 }
407
Ian Rogers936b37f2014-02-14 00:52:24 -0800408 bool IsParamAReference() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700409 return GetParamPrimitiveType() == Primitive::kPrimNot;
410 }
411
Ian Rogers936b37f2014-02-14 00:52:24 -0800412 bool IsParamALongOrDouble() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700413 Primitive::Type type = GetParamPrimitiveType();
414 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
415 }
416
417 uint64_t ReadSplitLongParam() const {
Nicolas Geoffray425f2392015-01-08 14:52:29 +0000418 // The splitted long is always available through the stack.
419 return *reinterpret_cast<uint64_t*>(stack_args_
420 + stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700421 }
422
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800423 void IncGprIndex() {
424 gpr_index_++;
425 if (kGprFprLockstep) {
426 fpr_index_++;
427 }
428 }
429
430 void IncFprIndex() {
431 fpr_index_++;
432 if (kGprFprLockstep) {
433 gpr_index_++;
434 }
435 }
436
Mathieu Chartier90443472015-07-16 20:32:27 -0700437 void VisitArguments() SHARED_REQUIRES(Locks::mutator_lock_) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800438 // (a) 'stack_args_' should point to the first method's argument
439 // (b) whatever the argument type it is, the 'stack_index_' should
440 // be moved forward along with every visiting.
Ian Rogers936b37f2014-02-14 00:52:24 -0800441 gpr_index_ = 0;
442 fpr_index_ = 0;
Zheng Xu5667fdb2014-10-23 18:29:55 +0800443 if (kQuickDoubleRegAlignedFloatBackFilled) {
444 fpr_double_index_ = 0;
445 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800446 stack_index_ = 0;
447 if (!is_static_) { // Handle this.
448 cur_type_ = Primitive::kPrimNot;
449 is_split_long_or_double_ = false;
Ian Rogers848871b2013-08-05 10:56:33 -0700450 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800451 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800452 if (kNumQuickGprArgs > 0) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800453 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800454 }
Ian Rogers848871b2013-08-05 10:56:33 -0700455 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800456 for (uint32_t shorty_index = 1; shorty_index < shorty_len_; ++shorty_index) {
457 cur_type_ = Primitive::GetType(shorty_[shorty_index]);
458 switch (cur_type_) {
459 case Primitive::kPrimNot:
460 case Primitive::kPrimBoolean:
461 case Primitive::kPrimByte:
462 case Primitive::kPrimChar:
463 case Primitive::kPrimShort:
464 case Primitive::kPrimInt:
465 is_split_long_or_double_ = false;
466 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800467 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800468 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800469 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800470 }
471 break;
472 case Primitive::kPrimFloat:
473 is_split_long_or_double_ = false;
474 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800475 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800476 if (kQuickSoftFloatAbi) {
477 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800478 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800479 }
480 } else {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800481 if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800482 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800483 if (kQuickDoubleRegAlignedFloatBackFilled) {
484 // Double should not overlap with float.
485 // For example, if fpr_index_ = 3, fpr_double_index_ should be at least 4.
486 fpr_double_index_ = std::max(fpr_double_index_, RoundUp(fpr_index_, 2));
487 // Float should not overlap with double.
488 if (fpr_index_ % 2 == 0) {
489 fpr_index_ = std::max(fpr_double_index_, fpr_index_);
490 }
Goran Jakovljevicff734982015-08-24 12:58:55 +0000491 } else if (kQuickSkipOddFpRegisters) {
492 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800493 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800494 }
495 }
496 break;
497 case Primitive::kPrimDouble:
498 case Primitive::kPrimLong:
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800499 if (kQuickSoftFloatAbi || (cur_type_ == Primitive::kPrimLong)) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000500 if (cur_type_ == Primitive::kPrimLong && kAlignPairRegister && gpr_index_ == 0) {
Goran Jakovljevicff734982015-08-24 12:58:55 +0000501 // Currently, this is only for ARM and MIPS, where the first available parameter
502 // register is R1 (on ARM) or A1 (on MIPS). So we skip it, and use R2 (on ARM) or
503 // A2 (on MIPS) instead.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800504 IncGprIndex();
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000505 }
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000506 is_split_long_or_double_ = (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) &&
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800507 ((gpr_index_ + 1) == kNumQuickGprArgs);
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500508 if (!kSplitPairAcrossRegisterAndStack && is_split_long_or_double_) {
509 // We don't want to split this. Pass over this register.
510 gpr_index_++;
511 is_split_long_or_double_ = false;
512 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800513 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800514 if (kBytesStackArgLocation == 4) {
515 stack_index_+= 2;
516 } else {
517 CHECK_EQ(kBytesStackArgLocation, 8U);
518 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800519 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700520 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800521 IncGprIndex();
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000522 if (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700523 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800524 IncGprIndex();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700525 }
526 }
527 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800528 } else {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000529 is_split_long_or_double_ = (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) &&
Zheng Xu5667fdb2014-10-23 18:29:55 +0800530 ((fpr_index_ + 1) == kNumQuickFprArgs) && !kQuickDoubleRegAlignedFloatBackFilled;
Ian Rogers936b37f2014-02-14 00:52:24 -0800531 Visit();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700532 if (kBytesStackArgLocation == 4) {
533 stack_index_+= 2;
Ian Rogers936b37f2014-02-14 00:52:24 -0800534 } else {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700535 CHECK_EQ(kBytesStackArgLocation, 8U);
536 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800537 }
Zheng Xu5667fdb2014-10-23 18:29:55 +0800538 if (kQuickDoubleRegAlignedFloatBackFilled) {
539 if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) {
540 fpr_double_index_ += 2;
541 // Float should not overlap with double.
542 if (fpr_index_ % 2 == 0) {
543 fpr_index_ = std::max(fpr_double_index_, fpr_index_);
544 }
545 }
546 } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800547 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800548 if (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) {
549 if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800550 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800551 }
552 }
553 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800554 }
555 break;
556 default:
557 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty_;
558 }
Ian Rogers848871b2013-08-05 10:56:33 -0700559 }
560 }
561
Andreas Gampec200a4a2014-06-16 18:39:09 -0700562 protected:
Ian Rogers848871b2013-08-05 10:56:33 -0700563 const bool is_static_;
564 const char* const shorty_;
565 const uint32_t shorty_len_;
Andreas Gampec200a4a2014-06-16 18:39:09 -0700566
567 private:
Ian Rogers13735952014-10-08 12:43:28 -0700568 uint8_t* const gpr_args_; // Address of GPR arguments in callee save frame.
569 uint8_t* const fpr_args_; // Address of FPR arguments in callee save frame.
570 uint8_t* const stack_args_; // Address of stack arguments in caller's frame.
Ian Rogers936b37f2014-02-14 00:52:24 -0800571 uint32_t gpr_index_; // Index into spilled GPRs.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800572 // Index into spilled FPRs.
573 // In case kQuickDoubleRegAlignedFloatBackFilled, it may index a hole while fpr_double_index_
574 // holds a higher register number.
575 uint32_t fpr_index_;
576 // Index into spilled FPRs for aligned double.
577 // Only used when kQuickDoubleRegAlignedFloatBackFilled. Next available double register indexed in
578 // terms of singles, may be behind fpr_index.
579 uint32_t fpr_double_index_;
Ian Rogers936b37f2014-02-14 00:52:24 -0800580 uint32_t stack_index_; // Index into arguments on the stack.
581 // The current type of argument during VisitArguments.
582 Primitive::Type cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700583 // Does a 64bit parameter straddle the register and stack arguments?
584 bool is_split_long_or_double_;
585};
586
Sebastien Hertza836bc92014-11-25 16:30:53 +0100587// Returns the 'this' object of a proxy method. This function is only used by StackVisitor. It
588// allows to use the QuickArgumentVisitor constants without moving all the code in its own module.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700589extern "C" mirror::Object* artQuickGetProxyThisObject(ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -0700590 SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertza836bc92014-11-25 16:30:53 +0100591 return QuickArgumentVisitor::GetProxyThisObject(sp);
592}
593
Ian Rogers848871b2013-08-05 10:56:33 -0700594// Visits arguments on the stack placing them into the shadow frame.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800595class BuildQuickShadowFrameVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700596 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700597 BuildQuickShadowFrameVisitor(ArtMethod** sp, bool is_static, const char* shorty,
598 uint32_t shorty_len, ShadowFrame* sf, size_t first_arg_reg) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700599 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), sf_(sf), cur_reg_(first_arg_reg) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700600
Mathieu Chartier90443472015-07-16 20:32:27 -0700601 void Visit() SHARED_REQUIRES(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700602
603 private:
Ian Rogers936b37f2014-02-14 00:52:24 -0800604 ShadowFrame* const sf_;
605 uint32_t cur_reg_;
Ian Rogers848871b2013-08-05 10:56:33 -0700606
Dragos Sbirleabd136a22013-08-13 18:07:04 -0700607 DISALLOW_COPY_AND_ASSIGN(BuildQuickShadowFrameVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700608};
609
Andreas Gampec200a4a2014-06-16 18:39:09 -0700610void BuildQuickShadowFrameVisitor::Visit() {
Ian Rogers9758f792014-03-13 09:02:55 -0700611 Primitive::Type type = GetParamPrimitiveType();
612 switch (type) {
613 case Primitive::kPrimLong: // Fall-through.
614 case Primitive::kPrimDouble:
615 if (IsSplitLongOrDouble()) {
616 sf_->SetVRegLong(cur_reg_, ReadSplitLongParam());
617 } else {
618 sf_->SetVRegLong(cur_reg_, *reinterpret_cast<jlong*>(GetParamAddress()));
619 }
620 ++cur_reg_;
621 break;
622 case Primitive::kPrimNot: {
623 StackReference<mirror::Object>* stack_ref =
624 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
625 sf_->SetVRegReference(cur_reg_, stack_ref->AsMirrorPtr());
626 }
627 break;
628 case Primitive::kPrimBoolean: // Fall-through.
629 case Primitive::kPrimByte: // Fall-through.
630 case Primitive::kPrimChar: // Fall-through.
631 case Primitive::kPrimShort: // Fall-through.
632 case Primitive::kPrimInt: // Fall-through.
633 case Primitive::kPrimFloat:
634 sf_->SetVReg(cur_reg_, *reinterpret_cast<jint*>(GetParamAddress()));
635 break;
636 case Primitive::kPrimVoid:
637 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700638 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -0700639 }
640 ++cur_reg_;
641}
642
Mathieu Chartiere401d142015-04-22 13:56:20 -0700643extern "C" uint64_t artQuickToInterpreterBridge(ArtMethod* method, Thread* self, ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -0700644 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers848871b2013-08-05 10:56:33 -0700645 // Ensure we don't get thread suspension until the object arguments are safely in the shadow
646 // frame.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700647 ScopedQuickEntrypointChecks sqec(self);
Ian Rogers848871b2013-08-05 10:56:33 -0700648
649 if (method->IsAbstract()) {
650 ThrowAbstractMethodError(method);
651 return 0;
Andreas Gampe639bdd12015-06-03 11:22:45 -0700652 }
653
654 JValue tmp_value;
655 ShadowFrame* deopt_frame = self->PopStackedShadowFrame(
656 StackedShadowFrameType::kSingleFrameDeoptimizationShadowFrame, false);
657 const DexFile::CodeItem* code_item = method->GetCodeItem();
658 DCHECK(code_item != nullptr) << PrettyMethod(method);
659 ManagedStack fragment;
660
661 DCHECK(!method->IsNative()) << PrettyMethod(method);
662 uint32_t shorty_len = 0;
663 auto* non_proxy_method = method->GetInterfaceMethodIfProxy(sizeof(void*));
664 const char* shorty = non_proxy_method->GetShorty(&shorty_len);
665
666 JValue result;
667
668 if (deopt_frame != nullptr) {
669 // Coming from single-frame deopt.
670
671 if (kIsDebugBuild) {
672 // Sanity-check: are the methods as expected? We check that the last shadow frame (the bottom
673 // of the call-stack) corresponds to the called method.
674 ShadowFrame* linked = deopt_frame;
675 while (linked->GetLink() != nullptr) {
676 linked = linked->GetLink();
677 }
678 CHECK_EQ(method, linked->GetMethod()) << PrettyMethod(method) << " "
679 << PrettyMethod(linked->GetMethod());
680 }
681
682 if (VLOG_IS_ON(deopt)) {
683 // Print out the stack to verify that it was a single-frame deopt.
684 LOG(INFO) << "Continue-ing from deopt. Stack is:";
685 QuickExceptionHandler::DumpFramesWithType(self, true);
686 }
687
688 mirror::Throwable* pending_exception = nullptr;
689 self->PopDeoptimizationContext(&result, &pending_exception);
690
691 // Push a transition back into managed code onto the linked list in thread.
692 self->PushManagedStackFragment(&fragment);
693
694 // Ensure that the stack is still in order.
695 if (kIsDebugBuild) {
696 class DummyStackVisitor : public StackVisitor {
697 public:
698 explicit DummyStackVisitor(Thread* self_in) SHARED_REQUIRES(Locks::mutator_lock_)
699 : StackVisitor(self_in, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {}
700
701 bool VisitFrame() OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
702 // Nothing to do here. In a debug build, SanityCheckFrame will do the work in the walking
703 // logic. Just always say we want to continue.
704 return true;
705 }
706 };
707 DummyStackVisitor dsv(self);
708 dsv.WalkStack();
709 }
710
711 // Restore the exception that was pending before deoptimization then interpret the
712 // deoptimized frames.
713 if (pending_exception != nullptr) {
714 self->SetException(pending_exception);
715 }
716 interpreter::EnterInterpreterFromDeoptimize(self, deopt_frame, &result);
Ian Rogers848871b2013-08-05 10:56:33 -0700717 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -0700718 const char* old_cause = self->StartAssertNoThreadSuspension(
719 "Building interpreter shadow frame");
Ian Rogers848871b2013-08-05 10:56:33 -0700720 uint16_t num_regs = code_item->registers_size_;
Andreas Gampec200a4a2014-06-16 18:39:09 -0700721 // No last shadow coming from quick.
Andreas Gampeb3025922015-09-01 14:45:00 -0700722 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
Andreas Gampe03ec9302015-08-27 17:41:47 -0700723 CREATE_SHADOW_FRAME(num_regs, /* link */ nullptr, method, /* dex pc */ 0);
Andreas Gampeb3025922015-09-01 14:45:00 -0700724 ShadowFrame* shadow_frame = shadow_frame_unique_ptr.get();
Ian Rogers848871b2013-08-05 10:56:33 -0700725 size_t first_arg_reg = code_item->registers_size_ - code_item->ins_size_;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700726 BuildQuickShadowFrameVisitor shadow_frame_builder(sp, method->IsStatic(), shorty, shorty_len,
Ian Rogers936b37f2014-02-14 00:52:24 -0800727 shadow_frame, first_arg_reg);
Ian Rogers848871b2013-08-05 10:56:33 -0700728 shadow_frame_builder.VisitArguments();
Ian Rogerse94652f2014-12-02 11:13:19 -0800729 const bool needs_initialization =
730 method->IsStatic() && !method->GetDeclaringClass()->IsInitialized();
Ian Rogers848871b2013-08-05 10:56:33 -0700731 // Push a transition back into managed code onto the linked list in thread.
Ian Rogers848871b2013-08-05 10:56:33 -0700732 self->PushManagedStackFragment(&fragment);
733 self->PushShadowFrame(shadow_frame);
734 self->EndAssertNoThreadSuspension(old_cause);
735
Ian Rogerse94652f2014-12-02 11:13:19 -0800736 if (needs_initialization) {
Ian Rogers848871b2013-08-05 10:56:33 -0700737 // Ensure static method's class is initialized.
Ian Rogerse94652f2014-12-02 11:13:19 -0800738 StackHandleScope<1> hs(self);
739 Handle<mirror::Class> h_class(hs.NewHandle(shadow_frame->GetMethod()->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700740 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
Ian Rogerse94652f2014-12-02 11:13:19 -0800741 DCHECK(Thread::Current()->IsExceptionPending()) << PrettyMethod(shadow_frame->GetMethod());
Ian Rogers848871b2013-08-05 10:56:33 -0700742 self->PopManagedStackFragment(fragment);
743 return 0;
744 }
745 }
Daniel Mihalyieb076692014-08-22 17:33:31 +0200746
Andreas Gampe639bdd12015-06-03 11:22:45 -0700747 result = interpreter::EnterInterpreterFromEntryPoint(self, code_item, shadow_frame);
Ian Rogers848871b2013-08-05 10:56:33 -0700748 }
Andreas Gampe639bdd12015-06-03 11:22:45 -0700749
750 // Pop transition.
751 self->PopManagedStackFragment(fragment);
752
753 // Request a stack deoptimization if needed
754 ArtMethod* caller = QuickArgumentVisitor::GetCallingMethod(sp);
755 if (UNLIKELY(Dbg::IsForcedInterpreterNeededForUpcall(self, caller))) {
756 // Push the context of the deoptimization stack so we can restore the return value and the
757 // exception before executing the deoptimized frames.
758 self->PushDeoptimizationContext(result, shorty[0] == 'L', self->GetException());
759
760 // Set special exception to cause deoptimization.
761 self->SetException(Thread::GetDeoptimizationException());
762 }
763
764 // No need to restore the args since the method has already been run by the interpreter.
765 return result.GetJ();
Ian Rogers848871b2013-08-05 10:56:33 -0700766}
767
768// Visits arguments on the stack placing them into the args vector, Object* arguments are converted
769// to jobjects.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800770class BuildQuickArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700771 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700772 BuildQuickArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty, uint32_t shorty_len,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700773 ScopedObjectAccessUnchecked* soa, std::vector<jvalue>* args) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700774 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa), args_(args) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700775
Mathieu Chartier90443472015-07-16 20:32:27 -0700776 void Visit() SHARED_REQUIRES(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700777
Mathieu Chartier90443472015-07-16 20:32:27 -0700778 void FixupReferences() SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800779
Ian Rogers848871b2013-08-05 10:56:33 -0700780 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700781 ScopedObjectAccessUnchecked* const soa_;
782 std::vector<jvalue>* const args_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800783 // References which we must update when exiting in case the GC moved the objects.
Ian Rogers700a4022014-05-19 16:49:03 -0700784 std::vector<std::pair<jobject, StackReference<mirror::Object>*>> references_;
Ian Rogers9758f792014-03-13 09:02:55 -0700785
Ian Rogers848871b2013-08-05 10:56:33 -0700786 DISALLOW_COPY_AND_ASSIGN(BuildQuickArgumentVisitor);
787};
788
Ian Rogers9758f792014-03-13 09:02:55 -0700789void BuildQuickArgumentVisitor::Visit() {
790 jvalue val;
791 Primitive::Type type = GetParamPrimitiveType();
792 switch (type) {
793 case Primitive::kPrimNot: {
794 StackReference<mirror::Object>* stack_ref =
795 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
796 val.l = soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
797 references_.push_back(std::make_pair(val.l, stack_ref));
798 break;
799 }
800 case Primitive::kPrimLong: // Fall-through.
801 case Primitive::kPrimDouble:
802 if (IsSplitLongOrDouble()) {
803 val.j = ReadSplitLongParam();
804 } else {
805 val.j = *reinterpret_cast<jlong*>(GetParamAddress());
806 }
807 break;
808 case Primitive::kPrimBoolean: // Fall-through.
809 case Primitive::kPrimByte: // Fall-through.
810 case Primitive::kPrimChar: // Fall-through.
811 case Primitive::kPrimShort: // Fall-through.
812 case Primitive::kPrimInt: // Fall-through.
813 case Primitive::kPrimFloat:
814 val.i = *reinterpret_cast<jint*>(GetParamAddress());
815 break;
816 case Primitive::kPrimVoid:
817 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700818 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -0700819 }
820 args_->push_back(val);
821}
822
823void BuildQuickArgumentVisitor::FixupReferences() {
824 // Fixup any references which may have changed.
825 for (const auto& pair : references_) {
826 pair.second->Assign(soa_->Decode<mirror::Object*>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -0700827 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -0700828 }
829}
830
Ian Rogers848871b2013-08-05 10:56:33 -0700831// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
832// which is responsible for recording callee save registers. We explicitly place into jobjects the
833// incoming reference arguments (so they survive GC). We invoke the invocation handler, which is a
834// field within the proxy object, which will box the primitive arguments and deal with error cases.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700835extern "C" uint64_t artQuickProxyInvokeHandler(
836 ArtMethod* proxy_method, mirror::Object* receiver, Thread* self, ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -0700837 SHARED_REQUIRES(Locks::mutator_lock_) {
Brian Carlstromd3633d52013-08-20 21:06:26 -0700838 DCHECK(proxy_method->IsProxyMethod()) << PrettyMethod(proxy_method);
839 DCHECK(receiver->GetClass()->IsProxyClass()) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700840 // Ensure we don't get thread suspension until the object arguments are safely in jobjects.
841 const char* old_cause =
842 self->StartAssertNoThreadSuspension("Adding to IRT proxy object arguments");
843 // Register the top of the managed stack, making stack crawlable.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700844 DCHECK_EQ((*sp), proxy_method) << PrettyMethod(proxy_method);
Nicolas Geoffray6bc43742015-10-12 18:11:10 +0100845 DCHECK_EQ(GetCallingCodeFrom(sp).GetFrameSizeInBytes(),
846 ArtCode(Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs))
847 .GetFrameSizeInBytes())
Brian Carlstromd3633d52013-08-20 21:06:26 -0700848 << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700849 self->VerifyStack();
850 // Start new JNI local reference state.
851 JNIEnvExt* env = self->GetJniEnv();
852 ScopedObjectAccessUnchecked soa(env);
853 ScopedJniEnvLocalRefState env_state(env);
854 // Create local ref. copies of proxy method and the receiver.
855 jobject rcvr_jobj = soa.AddLocalReference<jobject>(receiver);
856
857 // Placing arguments into args vector and remove the receiver.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700858 ArtMethod* non_proxy_method = proxy_method->GetInterfaceMethodIfProxy(sizeof(void*));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700859 CHECK(!non_proxy_method->IsStatic()) << PrettyMethod(proxy_method) << " "
Andreas Gampec200a4a2014-06-16 18:39:09 -0700860 << PrettyMethod(non_proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700861 std::vector<jvalue> args;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700862 uint32_t shorty_len = 0;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700863 const char* shorty = non_proxy_method->GetShorty(&shorty_len);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700864 BuildQuickArgumentVisitor local_ref_visitor(sp, false, shorty, shorty_len, &soa, &args);
Brian Carlstromd3633d52013-08-20 21:06:26 -0700865
Ian Rogers848871b2013-08-05 10:56:33 -0700866 local_ref_visitor.VisitArguments();
Brian Carlstromd3633d52013-08-20 21:06:26 -0700867 DCHECK_GT(args.size(), 0U) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700868 args.erase(args.begin());
869
870 // Convert proxy method into expected interface method.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700871 ArtMethod* interface_method = proxy_method->FindOverriddenMethod(sizeof(void*));
Ian Rogerse0a02da2014-12-02 14:10:53 -0800872 DCHECK(interface_method != nullptr) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700873 DCHECK(!interface_method->IsProxyMethod()) << PrettyMethod(interface_method);
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700874 self->EndAssertNoThreadSuspension(old_cause);
875 jobject interface_method_jobj = soa.AddLocalReference<jobject>(
876 mirror::Method::CreateFromArtMethod(soa.Self(), interface_method));
Ian Rogers848871b2013-08-05 10:56:33 -0700877
878 // All naked Object*s should now be in jobjects, so its safe to go into the main invoke code
879 // that performs allocations.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700880 JValue result = InvokeProxyInvocationHandler(soa, shorty, rcvr_jobj, interface_method_jobj, args);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800881 // Restore references which might have moved.
882 local_ref_visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -0700883 return result.GetJ();
884}
885
886// Read object references held in arguments from quick frames and place in a JNI local references,
887// so they don't get garbage collected.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800888class RememberForGcArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700889 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700890 RememberForGcArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty,
891 uint32_t shorty_len, ScopedObjectAccessUnchecked* soa) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700892 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700893
Mathieu Chartier90443472015-07-16 20:32:27 -0700894 void Visit() SHARED_REQUIRES(Locks::mutator_lock_) OVERRIDE;
Mathieu Chartier07d447b2013-09-26 11:57:43 -0700895
Mathieu Chartier90443472015-07-16 20:32:27 -0700896 void FixupReferences() SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers848871b2013-08-05 10:56:33 -0700897
898 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700899 ScopedObjectAccessUnchecked* const soa_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800900 // References which we must update when exiting in case the GC moved the objects.
Andreas Gampec200a4a2014-06-16 18:39:09 -0700901 std::vector<std::pair<jobject, StackReference<mirror::Object>*> > references_;
902
Mathieu Chartier590fee92013-09-13 13:46:47 -0700903 DISALLOW_COPY_AND_ASSIGN(RememberForGcArgumentVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700904};
905
Ian Rogers9758f792014-03-13 09:02:55 -0700906void RememberForGcArgumentVisitor::Visit() {
907 if (IsParamAReference()) {
908 StackReference<mirror::Object>* stack_ref =
909 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
910 jobject reference =
911 soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
912 references_.push_back(std::make_pair(reference, stack_ref));
913 }
914}
915
916void RememberForGcArgumentVisitor::FixupReferences() {
917 // Fixup any references which may have changed.
918 for (const auto& pair : references_) {
919 pair.second->Assign(soa_->Decode<mirror::Object*>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -0700920 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -0700921 }
922}
923
Ian Rogers848871b2013-08-05 10:56:33 -0700924// Lazily resolve a method for quick. Called by stub code.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700925extern "C" const void* artQuickResolutionTrampoline(
926 ArtMethod* called, mirror::Object* receiver, Thread* self, ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -0700927 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe3b45ef22015-05-26 21:34:09 -0700928 // The resolution trampoline stashes the resolved method into the callee-save frame to transport
929 // it. Thus, when exiting, the stack cannot be verified (as the resolved method most likely
930 // does not have the same stack layout as the callee-save method).
931 ScopedQuickEntrypointChecks sqec(self, kIsDebugBuild, false);
Ian Rogers848871b2013-08-05 10:56:33 -0700932 // Start new JNI local reference state
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800933 JNIEnvExt* env = self->GetJniEnv();
Ian Rogers848871b2013-08-05 10:56:33 -0700934 ScopedObjectAccessUnchecked soa(env);
935 ScopedJniEnvLocalRefState env_state(env);
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800936 const char* old_cause = self->StartAssertNoThreadSuspension("Quick method resolution set up");
Ian Rogers848871b2013-08-05 10:56:33 -0700937
938 // Compute details about the called method (avoid GCs)
939 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Ian Rogers848871b2013-08-05 10:56:33 -0700940 InvokeType invoke_type;
Ian Rogerse0a02da2014-12-02 14:10:53 -0800941 MethodReference called_method(nullptr, 0);
942 const bool called_method_known_on_entry = !called->IsRuntimeMethod();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700943 ArtMethod* caller = nullptr;
Ian Rogerse0a02da2014-12-02 14:10:53 -0800944 if (!called_method_known_on_entry) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +0100945 caller = QuickArgumentVisitor::GetCallingMethod(sp);
946 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
Ian Rogers848871b2013-08-05 10:56:33 -0700947 const DexFile::CodeItem* code;
Ian Rogerse0a02da2014-12-02 14:10:53 -0800948 called_method.dex_file = caller->GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700949 code = caller->GetCodeItem();
Ian Rogers848871b2013-08-05 10:56:33 -0700950 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
951 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
952 Instruction::Code instr_code = instr->Opcode();
953 bool is_range;
954 switch (instr_code) {
955 case Instruction::INVOKE_DIRECT:
956 invoke_type = kDirect;
957 is_range = false;
958 break;
959 case Instruction::INVOKE_DIRECT_RANGE:
960 invoke_type = kDirect;
961 is_range = true;
962 break;
963 case Instruction::INVOKE_STATIC:
964 invoke_type = kStatic;
965 is_range = false;
966 break;
967 case Instruction::INVOKE_STATIC_RANGE:
968 invoke_type = kStatic;
969 is_range = true;
970 break;
971 case Instruction::INVOKE_SUPER:
972 invoke_type = kSuper;
973 is_range = false;
974 break;
975 case Instruction::INVOKE_SUPER_RANGE:
976 invoke_type = kSuper;
977 is_range = true;
978 break;
979 case Instruction::INVOKE_VIRTUAL:
980 invoke_type = kVirtual;
981 is_range = false;
982 break;
983 case Instruction::INVOKE_VIRTUAL_RANGE:
984 invoke_type = kVirtual;
985 is_range = true;
986 break;
987 case Instruction::INVOKE_INTERFACE:
988 invoke_type = kInterface;
989 is_range = false;
990 break;
991 case Instruction::INVOKE_INTERFACE_RANGE:
992 invoke_type = kInterface;
993 is_range = true;
994 break;
995 default:
Ian Rogerse0a02da2014-12-02 14:10:53 -0800996 LOG(FATAL) << "Unexpected call into trampoline: " << instr->DumpString(nullptr);
997 UNREACHABLE();
Ian Rogers848871b2013-08-05 10:56:33 -0700998 }
Ian Rogerse0a02da2014-12-02 14:10:53 -0800999 called_method.dex_method_index = (is_range) ? instr->VRegB_3rc() : instr->VRegB_35c();
Ian Rogers848871b2013-08-05 10:56:33 -07001000 } else {
1001 invoke_type = kStatic;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001002 called_method.dex_file = called->GetDexFile();
1003 called_method.dex_method_index = called->GetDexMethodIndex();
Ian Rogers848871b2013-08-05 10:56:33 -07001004 }
1005 uint32_t shorty_len;
1006 const char* shorty =
Ian Rogerse0a02da2014-12-02 14:10:53 -08001007 called_method.dex_file->GetMethodShorty(
1008 called_method.dex_file->GetMethodId(called_method.dex_method_index), &shorty_len);
Mathieu Chartier590fee92013-09-13 13:46:47 -07001009 RememberForGcArgumentVisitor visitor(sp, invoke_type == kStatic, shorty, shorty_len, &soa);
Ian Rogers848871b2013-08-05 10:56:33 -07001010 visitor.VisitArguments();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001011 self->EndAssertNoThreadSuspension(old_cause);
Ian Rogerse0a02da2014-12-02 14:10:53 -08001012 const bool virtual_or_interface = invoke_type == kVirtual || invoke_type == kInterface;
Ian Rogers848871b2013-08-05 10:56:33 -07001013 // Resolve method filling in dex cache.
Ian Rogerse0a02da2014-12-02 14:10:53 -08001014 if (!called_method_known_on_entry) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001015 StackHandleScope<1> hs(self);
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001016 mirror::Object* dummy = nullptr;
1017 HandleWrapper<mirror::Object> h_receiver(
1018 hs.NewHandleWrapper(virtual_or_interface ? &receiver : &dummy));
Ian Rogerse0a02da2014-12-02 14:10:53 -08001019 DCHECK_EQ(caller->GetDexFile(), called_method.dex_file);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001020 called = linker->ResolveMethod(self, called_method.dex_method_index, caller, invoke_type);
Ian Rogers848871b2013-08-05 10:56:33 -07001021 }
Ian Rogerse0a02da2014-12-02 14:10:53 -08001022 const void* code = nullptr;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001023 if (LIKELY(!self->IsExceptionPending())) {
Ian Rogers848871b2013-08-05 10:56:33 -07001024 // Incompatible class change should have been handled in resolve method.
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001025 CHECK(!called->CheckIncompatibleClassChange(invoke_type))
1026 << PrettyMethod(called) << " " << invoke_type;
Mathieu Chartier55871bf2014-02-27 10:24:50 -08001027 if (virtual_or_interface) {
1028 // Refine called method based on receiver.
1029 CHECK(receiver != nullptr) << invoke_type;
Mingyao Yangf4867782014-05-05 11:55:02 -07001030
Mathieu Chartiere401d142015-04-22 13:56:20 -07001031 ArtMethod* orig_called = called;
Mathieu Chartier55871bf2014-02-27 10:24:50 -08001032 if (invoke_type == kVirtual) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001033 called = receiver->GetClass()->FindVirtualMethodForVirtual(called, sizeof(void*));
Mathieu Chartier55871bf2014-02-27 10:24:50 -08001034 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001035 called = receiver->GetClass()->FindVirtualMethodForInterface(called, sizeof(void*));
Mathieu Chartier55871bf2014-02-27 10:24:50 -08001036 }
Mingyao Yangf4867782014-05-05 11:55:02 -07001037
1038 CHECK(called != nullptr) << PrettyMethod(orig_called) << " "
1039 << PrettyTypeOf(receiver) << " "
1040 << invoke_type << " " << orig_called->GetVtableIndex();
1041
Ian Rogers83883d72013-10-21 21:07:24 -07001042 // 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 -08001043 // of the sharpened method avoiding dirtying the dex cache if possible.
Ian Rogers00f15272014-12-02 16:55:46 -08001044 // Note, called_method.dex_method_index references the dex method before the
1045 // FindVirtualMethodFor... This is ok for FindDexMethodIndexInOtherDexFile that only cares
1046 // about the name and signature.
1047 uint32_t update_dex_cache_method_index = called->GetDexMethodIndex();
Vladimir Marko05792b92015-08-03 11:56:49 +01001048 if (!called->HasSameDexCacheResolvedMethods(caller, sizeof(void*))) {
Ian Rogers83883d72013-10-21 21:07:24 -07001049 // Calling from one dex file to another, need to compute the method index appropriate to
Vladimir Markobbcc0c02014-02-03 14:08:42 +00001050 // the caller's dex file. Since we get here only if the original called was a runtime
1051 // method, we've got the correct dex_file and a dex_method_idx from above.
Ian Rogerse0a02da2014-12-02 14:10:53 -08001052 DCHECK(!called_method_known_on_entry);
1053 DCHECK_EQ(caller->GetDexFile(), called_method.dex_file);
1054 const DexFile* caller_dex_file = called_method.dex_file;
1055 uint32_t caller_method_name_and_sig_index = called_method.dex_method_index;
1056 update_dex_cache_method_index =
1057 called->FindDexMethodIndexInOtherDexFile(*caller_dex_file,
1058 caller_method_name_and_sig_index);
1059 }
1060 if ((update_dex_cache_method_index != DexFile::kDexNoIndex) &&
Mathieu Chartiere401d142015-04-22 13:56:20 -07001061 (caller->GetDexCacheResolvedMethod(
1062 update_dex_cache_method_index, sizeof(void*)) != called)) {
1063 caller->SetDexCacheResolvedMethod(update_dex_cache_method_index, called, sizeof(void*));
Ian Rogers83883d72013-10-21 21:07:24 -07001064 }
Mathieu Chartiere4a91bb2015-01-28 13:11:44 -08001065 } else if (invoke_type == kStatic) {
1066 const auto called_dex_method_idx = called->GetDexMethodIndex();
1067 // For static invokes, we may dispatch to the static method in the superclass but resolve
1068 // using the subclass. To prevent getting slow paths on each invoke, we force set the
1069 // resolved method for the super class dex method index if we are in the same dex file.
1070 // b/19175856
1071 if (called->GetDexFile() == called_method.dex_file &&
1072 called_method.dex_method_index != called_dex_method_idx) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001073 called->GetDexCache()->SetResolvedMethod(called_dex_method_idx, called, sizeof(void*));
Mathieu Chartiere4a91bb2015-01-28 13:11:44 -08001074 }
Ian Rogers83883d72013-10-21 21:07:24 -07001075 }
Daniel Mihalyieb076692014-08-22 17:33:31 +02001076
Ian Rogers848871b2013-08-05 10:56:33 -07001077 // Ensure that the called method's class is initialized.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001078 StackHandleScope<1> hs(soa.Self());
1079 Handle<mirror::Class> called_class(hs.NewHandle(called->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -07001080 linker->EnsureInitialized(soa.Self(), called_class, true, true);
Ian Rogers848871b2013-08-05 10:56:33 -07001081 if (LIKELY(called_class->IsInitialized())) {
Daniel Mihalyieb076692014-08-22 17:33:31 +02001082 if (UNLIKELY(Dbg::IsForcedInterpreterNeededForResolution(self, called))) {
1083 // If we are single-stepping or the called method is deoptimized (by a
1084 // breakpoint, for example), then we have to execute the called method
1085 // with the interpreter.
1086 code = GetQuickToInterpreterBridge();
1087 } else if (UNLIKELY(Dbg::IsForcedInstrumentationNeededForResolution(self, caller))) {
1088 // If the caller is deoptimized (by a breakpoint, for example), we have to
1089 // continue its execution with interpreter when returning from the called
1090 // method. Because we do not want to execute the called method with the
1091 // interpreter, we wrap its execution into the instrumentation stubs.
1092 // When the called method returns, it will execute the instrumentation
1093 // exit hook that will determine the need of the interpreter with a call
1094 // to Dbg::IsForcedInterpreterNeededForUpcall and deoptimize the stack if
1095 // it is needed.
1096 code = GetQuickInstrumentationEntryPoint();
1097 } else {
1098 code = called->GetEntryPointFromQuickCompiledCode();
1099 }
Ian Rogers848871b2013-08-05 10:56:33 -07001100 } else if (called_class->IsInitializing()) {
Daniel Mihalyieb076692014-08-22 17:33:31 +02001101 if (UNLIKELY(Dbg::IsForcedInterpreterNeededForResolution(self, called))) {
1102 // If we are single-stepping or the called method is deoptimized (by a
1103 // breakpoint, for example), then we have to execute the called method
1104 // with the interpreter.
1105 code = GetQuickToInterpreterBridge();
1106 } else if (invoke_type == kStatic) {
Ian Rogers848871b2013-08-05 10:56:33 -07001107 // Class is still initializing, go to oat and grab code (trampoline must be left in place
1108 // until class is initialized to stop races between threads).
Ian Rogersef7d42f2014-01-06 12:55:46 -08001109 code = linker->GetQuickOatCodeFor(called);
Ian Rogers848871b2013-08-05 10:56:33 -07001110 } else {
1111 // No trampoline for non-static methods.
Ian Rogersef7d42f2014-01-06 12:55:46 -08001112 code = called->GetEntryPointFromQuickCompiledCode();
Ian Rogers848871b2013-08-05 10:56:33 -07001113 }
1114 } else {
1115 DCHECK(called_class->IsErroneous());
1116 }
1117 }
Ian Rogerse0a02da2014-12-02 14:10:53 -08001118 CHECK_EQ(code == nullptr, self->IsExceptionPending());
Mathieu Chartier07d447b2013-09-26 11:57:43 -07001119 // Fixup any locally saved objects may have moved during a GC.
1120 visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -07001121 // Place called method in callee-save frame to be placed as first argument to quick method.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001122 *sp = called;
1123
Ian Rogers848871b2013-08-05 10:56:33 -07001124 return code;
1125}
1126
Andreas Gampec147b002014-03-06 18:11:06 -08001127/*
1128 * This class uses a couple of observations to unite the different calling conventions through
1129 * a few constants.
1130 *
1131 * 1) Number of registers used for passing is normally even, so counting down has no penalty for
1132 * possible alignment.
1133 * 2) Known 64b architectures store 8B units on the stack, both for integral and floating point
1134 * types, so using uintptr_t is OK. Also means that we can use kRegistersNeededX to denote
1135 * when we have to split things
1136 * 3) The only soft-float, Arm, is 32b, so no widening needs to be taken into account for floats
1137 * and we can use Int handling directly.
1138 * 4) Only 64b architectures widen, and their stack is aligned 8B anyways, so no padding code
1139 * necessary when widening. Also, widening of Ints will take place implicitly, and the
1140 * extension should be compatible with Aarch64, which mandates copying the available bits
1141 * into LSB and leaving the rest unspecified.
1142 * 5) Aligning longs and doubles is necessary on arm only, and it's the same in registers and on
1143 * the stack.
1144 * 6) There is only little endian.
1145 *
1146 *
1147 * Actual work is supposed to be done in a delegate of the template type. The interface is as
1148 * follows:
1149 *
1150 * void PushGpr(uintptr_t): Add a value for the next GPR
1151 *
1152 * void PushFpr4(float): Add a value for the next FPR of size 32b. Is only called if we need
1153 * padding, that is, think the architecture is 32b and aligns 64b.
1154 *
1155 * void PushFpr8(uint64_t): Push a double. We _will_ call this on 32b, it's the callee's job to
1156 * split this if necessary. The current state will have aligned, if
1157 * necessary.
1158 *
1159 * void PushStack(uintptr_t): Push a value to the stack.
1160 *
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001161 * uintptr_t PushHandleScope(mirror::Object* ref): Add a reference to the HandleScope. This _will_ have nullptr,
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001162 * as this might be important for null initialization.
Andreas Gampec147b002014-03-06 18:11:06 -08001163 * Must return the jobject, that is, the reference to the
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001164 * entry in the HandleScope (nullptr if necessary).
Andreas Gampec147b002014-03-06 18:11:06 -08001165 *
1166 */
Andreas Gampec200a4a2014-06-16 18:39:09 -07001167template<class T> class BuildNativeCallFrameStateMachine {
Andreas Gampec147b002014-03-06 18:11:06 -08001168 public:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001169#if defined(__arm__)
1170 // TODO: These are all dummy values!
Andreas Gampec147b002014-03-06 18:11:06 -08001171 static constexpr bool kNativeSoftFloatAbi = true;
1172 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs, r0-r3
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001173 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
1174
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001175 static constexpr size_t kRegistersNeededForLong = 2;
1176 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -08001177 static constexpr bool kMultiRegistersAligned = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001178 static constexpr bool kMultiFPRegistersWidened = false;
1179 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001180 static constexpr bool kAlignLongOnStack = true;
1181 static constexpr bool kAlignDoubleOnStack = true;
Stuart Monteithb95a5342014-03-12 13:32:32 +00001182#elif defined(__aarch64__)
1183 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
1184 static constexpr size_t kNumNativeGprArgs = 8; // 6 arguments passed in GPRs.
1185 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
1186
1187 static constexpr size_t kRegistersNeededForLong = 1;
1188 static constexpr size_t kRegistersNeededForDouble = 1;
1189 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001190 static constexpr bool kMultiFPRegistersWidened = false;
1191 static constexpr bool kMultiGPRegistersWidened = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +00001192 static constexpr bool kAlignLongOnStack = false;
1193 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001194#elif defined(__mips__) && !defined(__LP64__)
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001195 static constexpr bool kNativeSoftFloatAbi = true; // This is a hard float ABI.
Douglas Leung735b8552014-10-31 12:21:40 -07001196 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs.
1197 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001198
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001199 static constexpr size_t kRegistersNeededForLong = 2;
1200 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -08001201 static constexpr bool kMultiRegistersAligned = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001202 static constexpr bool kMultiFPRegistersWidened = true;
1203 static constexpr bool kMultiGPRegistersWidened = false;
Douglas Leung735b8552014-10-31 12:21:40 -07001204 static constexpr bool kAlignLongOnStack = true;
1205 static constexpr bool kAlignDoubleOnStack = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001206#elif defined(__mips__) && defined(__LP64__)
1207 // Let the code prepare GPRs only and we will load the FPRs with same data.
1208 static constexpr bool kNativeSoftFloatAbi = true;
1209 static constexpr size_t kNumNativeGprArgs = 8;
1210 static constexpr size_t kNumNativeFprArgs = 0;
1211
1212 static constexpr size_t kRegistersNeededForLong = 1;
1213 static constexpr size_t kRegistersNeededForDouble = 1;
1214 static constexpr bool kMultiRegistersAligned = false;
1215 static constexpr bool kMultiFPRegistersWidened = false;
1216 static constexpr bool kMultiGPRegistersWidened = true;
1217 static constexpr bool kAlignLongOnStack = false;
1218 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001219#elif defined(__i386__)
1220 // TODO: Check these!
Andreas Gampec147b002014-03-06 18:11:06 -08001221 static constexpr bool kNativeSoftFloatAbi = false; // Not using int registers for fp
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001222 static constexpr size_t kNumNativeGprArgs = 0; // 6 arguments passed in GPRs.
1223 static constexpr size_t kNumNativeFprArgs = 0; // 8 arguments passed in FPRs.
1224
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001225 static constexpr size_t kRegistersNeededForLong = 2;
1226 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001227 static constexpr bool kMultiRegistersAligned = false; // x86 not using regs, anyways
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001228 static constexpr bool kMultiFPRegistersWidened = false;
1229 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001230 static constexpr bool kAlignLongOnStack = false;
1231 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001232#elif defined(__x86_64__)
1233 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
1234 static constexpr size_t kNumNativeGprArgs = 6; // 6 arguments passed in GPRs.
1235 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
1236
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001237 static constexpr size_t kRegistersNeededForLong = 1;
1238 static constexpr size_t kRegistersNeededForDouble = 1;
Andreas Gampec147b002014-03-06 18:11:06 -08001239 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001240 static constexpr bool kMultiFPRegistersWidened = false;
1241 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001242 static constexpr bool kAlignLongOnStack = false;
1243 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001244#else
1245#error "Unsupported architecture"
1246#endif
1247
Andreas Gampec147b002014-03-06 18:11:06 -08001248 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001249 explicit BuildNativeCallFrameStateMachine(T* delegate)
1250 : gpr_index_(kNumNativeGprArgs),
1251 fpr_index_(kNumNativeFprArgs),
1252 stack_entries_(0),
1253 delegate_(delegate) {
Andreas Gampec147b002014-03-06 18:11:06 -08001254 // For register alignment, we want to assume that counters (gpr_index_, fpr_index_) are even iff
1255 // the next register is even; counting down is just to make the compiler happy...
Andreas Gampe575e78c2014-11-03 23:41:03 -08001256 static_assert(kNumNativeGprArgs % 2 == 0U, "Number of native GPR arguments not even");
1257 static_assert(kNumNativeFprArgs % 2 == 0U, "Number of native FPR arguments not even");
Andreas Gampec147b002014-03-06 18:11:06 -08001258 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001259
Andreas Gampec200a4a2014-06-16 18:39:09 -07001260 virtual ~BuildNativeCallFrameStateMachine() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001261
Ian Rogers1428dce2014-10-21 15:02:15 -07001262 bool HavePointerGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001263 return gpr_index_ > 0;
1264 }
1265
Andreas Gampec200a4a2014-06-16 18:39:09 -07001266 void AdvancePointer(const void* val) {
Andreas Gampec147b002014-03-06 18:11:06 -08001267 if (HavePointerGpr()) {
1268 gpr_index_--;
1269 PushGpr(reinterpret_cast<uintptr_t>(val));
1270 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001271 stack_entries_++; // TODO: have a field for pointer length as multiple of 32b
Andreas Gampec147b002014-03-06 18:11:06 -08001272 PushStack(reinterpret_cast<uintptr_t>(val));
1273 gpr_index_ = 0;
1274 }
1275 }
1276
Ian Rogers1428dce2014-10-21 15:02:15 -07001277 bool HaveHandleScopeGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001278 return gpr_index_ > 0;
1279 }
1280
Mathieu Chartier90443472015-07-16 20:32:27 -07001281 void AdvanceHandleScope(mirror::Object* ptr) SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001282 uintptr_t handle = PushHandle(ptr);
1283 if (HaveHandleScopeGpr()) {
Andreas Gampec147b002014-03-06 18:11:06 -08001284 gpr_index_--;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001285 PushGpr(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001286 } else {
1287 stack_entries_++;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001288 PushStack(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001289 gpr_index_ = 0;
1290 }
1291 }
1292
Ian Rogers1428dce2014-10-21 15:02:15 -07001293 bool HaveIntGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001294 return gpr_index_ > 0;
1295 }
1296
1297 void AdvanceInt(uint32_t val) {
1298 if (HaveIntGpr()) {
1299 gpr_index_--;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001300 if (kMultiGPRegistersWidened) {
1301 DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001302 PushGpr(static_cast<int64_t>(bit_cast<int32_t, uint32_t>(val)));
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001303 } else {
1304 PushGpr(val);
1305 }
Andreas Gampec147b002014-03-06 18:11:06 -08001306 } else {
1307 stack_entries_++;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001308 if (kMultiGPRegistersWidened) {
1309 DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001310 PushStack(static_cast<int64_t>(bit_cast<int32_t, uint32_t>(val)));
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001311 } else {
1312 PushStack(val);
1313 }
Andreas Gampec147b002014-03-06 18:11:06 -08001314 gpr_index_ = 0;
1315 }
1316 }
1317
Ian Rogers1428dce2014-10-21 15:02:15 -07001318 bool HaveLongGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001319 return gpr_index_ >= kRegistersNeededForLong + (LongGprNeedsPadding() ? 1 : 0);
1320 }
1321
Ian Rogers1428dce2014-10-21 15:02:15 -07001322 bool LongGprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001323 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1324 kAlignLongOnStack && // and when it needs alignment
1325 (gpr_index_ & 1) == 1; // counter is odd, see constructor
1326 }
1327
Ian Rogers1428dce2014-10-21 15:02:15 -07001328 bool LongStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001329 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1330 kAlignLongOnStack && // and when it needs 8B alignment
1331 (stack_entries_ & 1) == 1; // counter is odd
1332 }
1333
1334 void AdvanceLong(uint64_t val) {
1335 if (HaveLongGpr()) {
1336 if (LongGprNeedsPadding()) {
1337 PushGpr(0);
1338 gpr_index_--;
1339 }
1340 if (kRegistersNeededForLong == 1) {
1341 PushGpr(static_cast<uintptr_t>(val));
1342 } else {
1343 PushGpr(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1344 PushGpr(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1345 }
1346 gpr_index_ -= kRegistersNeededForLong;
1347 } else {
1348 if (LongStackNeedsPadding()) {
1349 PushStack(0);
1350 stack_entries_++;
1351 }
1352 if (kRegistersNeededForLong == 1) {
1353 PushStack(static_cast<uintptr_t>(val));
1354 stack_entries_++;
1355 } else {
1356 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1357 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1358 stack_entries_ += 2;
1359 }
1360 gpr_index_ = 0;
1361 }
1362 }
1363
Ian Rogers1428dce2014-10-21 15:02:15 -07001364 bool HaveFloatFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001365 return fpr_index_ > 0;
1366 }
1367
Andreas Gampec147b002014-03-06 18:11:06 -08001368 void AdvanceFloat(float val) {
1369 if (kNativeSoftFloatAbi) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001370 AdvanceInt(bit_cast<uint32_t, float>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001371 } else {
1372 if (HaveFloatFpr()) {
1373 fpr_index_--;
1374 if (kRegistersNeededForDouble == 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001375 if (kMultiFPRegistersWidened) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001376 PushFpr8(bit_cast<uint64_t, double>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001377 } else {
1378 // No widening, just use the bits.
Roland Levillainda4d79b2015-03-24 14:36:11 +00001379 PushFpr8(static_cast<uint64_t>(bit_cast<uint32_t, float>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001380 }
1381 } else {
1382 PushFpr4(val);
1383 }
1384 } else {
1385 stack_entries_++;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001386 if (kRegistersNeededForDouble == 1 && kMultiFPRegistersWidened) {
Andreas Gampec147b002014-03-06 18:11:06 -08001387 // Need to widen before storing: Note the "double" in the template instantiation.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001388 // Note: We need to jump through those hoops to make the compiler happy.
1389 DCHECK_EQ(sizeof(uintptr_t), sizeof(uint64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001390 PushStack(static_cast<uintptr_t>(bit_cast<uint64_t, double>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001391 } else {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001392 PushStack(static_cast<uintptr_t>(bit_cast<uint32_t, float>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001393 }
1394 fpr_index_ = 0;
1395 }
1396 }
1397 }
1398
Ian Rogers1428dce2014-10-21 15:02:15 -07001399 bool HaveDoubleFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001400 return fpr_index_ >= kRegistersNeededForDouble + (DoubleFprNeedsPadding() ? 1 : 0);
1401 }
1402
Ian Rogers1428dce2014-10-21 15:02:15 -07001403 bool DoubleFprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001404 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1405 kAlignDoubleOnStack && // and when it needs alignment
1406 (fpr_index_ & 1) == 1; // counter is odd, see constructor
1407 }
1408
Ian Rogers1428dce2014-10-21 15:02:15 -07001409 bool DoubleStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001410 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1411 kAlignDoubleOnStack && // and when it needs 8B alignment
1412 (stack_entries_ & 1) == 1; // counter is odd
1413 }
1414
1415 void AdvanceDouble(uint64_t val) {
1416 if (kNativeSoftFloatAbi) {
1417 AdvanceLong(val);
1418 } else {
1419 if (HaveDoubleFpr()) {
1420 if (DoubleFprNeedsPadding()) {
1421 PushFpr4(0);
1422 fpr_index_--;
1423 }
1424 PushFpr8(val);
1425 fpr_index_ -= kRegistersNeededForDouble;
1426 } else {
1427 if (DoubleStackNeedsPadding()) {
1428 PushStack(0);
1429 stack_entries_++;
1430 }
1431 if (kRegistersNeededForDouble == 1) {
1432 PushStack(static_cast<uintptr_t>(val));
1433 stack_entries_++;
1434 } else {
1435 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1436 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1437 stack_entries_ += 2;
1438 }
1439 fpr_index_ = 0;
1440 }
1441 }
1442 }
1443
Ian Rogers1428dce2014-10-21 15:02:15 -07001444 uint32_t GetStackEntries() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001445 return stack_entries_;
1446 }
1447
Ian Rogers1428dce2014-10-21 15:02:15 -07001448 uint32_t GetNumberOfUsedGprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001449 return kNumNativeGprArgs - gpr_index_;
1450 }
1451
Ian Rogers1428dce2014-10-21 15:02:15 -07001452 uint32_t GetNumberOfUsedFprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001453 return kNumNativeFprArgs - fpr_index_;
1454 }
1455
1456 private:
1457 void PushGpr(uintptr_t val) {
1458 delegate_->PushGpr(val);
1459 }
1460 void PushFpr4(float val) {
1461 delegate_->PushFpr4(val);
1462 }
1463 void PushFpr8(uint64_t val) {
1464 delegate_->PushFpr8(val);
1465 }
1466 void PushStack(uintptr_t val) {
1467 delegate_->PushStack(val);
1468 }
Mathieu Chartier90443472015-07-16 20:32:27 -07001469 uintptr_t PushHandle(mirror::Object* ref) SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001470 return delegate_->PushHandle(ref);
Andreas Gampec147b002014-03-06 18:11:06 -08001471 }
1472
1473 uint32_t gpr_index_; // Number of free GPRs
1474 uint32_t fpr_index_; // Number of free FPRs
1475 uint32_t stack_entries_; // Stack entries are in multiples of 32b, as floats are usually not
1476 // extended
Ian Rogers1428dce2014-10-21 15:02:15 -07001477 T* const delegate_; // What Push implementation gets called
Andreas Gampec147b002014-03-06 18:11:06 -08001478};
1479
Andreas Gampec200a4a2014-06-16 18:39:09 -07001480// Computes the sizes of register stacks and call stack area. Handling of references can be extended
1481// in subclasses.
1482//
1483// To handle native pointers, use "L" in the shorty for an object reference, which simulates
1484// them with handles.
1485class ComputeNativeCallFrameSize {
Andreas Gampec147b002014-03-06 18:11:06 -08001486 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001487 ComputeNativeCallFrameSize() : num_stack_entries_(0) {}
1488
1489 virtual ~ComputeNativeCallFrameSize() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001490
Ian Rogers1428dce2014-10-21 15:02:15 -07001491 uint32_t GetStackSize() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001492 return num_stack_entries_ * sizeof(uintptr_t);
1493 }
1494
Ian Rogers1428dce2014-10-21 15:02:15 -07001495 uint8_t* LayoutCallStack(uint8_t* sp8) const {
Andreas Gampec147b002014-03-06 18:11:06 -08001496 sp8 -= GetStackSize();
Andreas Gampe779f8c92014-06-09 18:29:38 -07001497 // Align by kStackAlignment.
1498 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
Andreas Gampec200a4a2014-06-16 18:39:09 -07001499 return sp8;
Andreas Gampec147b002014-03-06 18:11:06 -08001500 }
1501
Ian Rogers1428dce2014-10-21 15:02:15 -07001502 uint8_t* LayoutCallRegisterStacks(uint8_t* sp8, uintptr_t** start_gpr, uint32_t** start_fpr)
1503 const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001504 // Assumption is OK right now, as we have soft-float arm
1505 size_t fregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeFprArgs;
1506 sp8 -= fregs * sizeof(uintptr_t);
1507 *start_fpr = reinterpret_cast<uint32_t*>(sp8);
1508 size_t iregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeGprArgs;
1509 sp8 -= iregs * sizeof(uintptr_t);
1510 *start_gpr = reinterpret_cast<uintptr_t*>(sp8);
1511 return sp8;
1512 }
Andreas Gampec147b002014-03-06 18:11:06 -08001513
Andreas Gampec200a4a2014-06-16 18:39:09 -07001514 uint8_t* LayoutNativeCall(uint8_t* sp8, uintptr_t** start_stack, uintptr_t** start_gpr,
Ian Rogers1428dce2014-10-21 15:02:15 -07001515 uint32_t** start_fpr) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001516 // Native call stack.
1517 sp8 = LayoutCallStack(sp8);
1518 *start_stack = reinterpret_cast<uintptr_t*>(sp8);
Andreas Gampec147b002014-03-06 18:11:06 -08001519
Andreas Gampec200a4a2014-06-16 18:39:09 -07001520 // Put fprs and gprs below.
1521 sp8 = LayoutCallRegisterStacks(sp8, start_gpr, start_fpr);
Andreas Gampec147b002014-03-06 18:11:06 -08001522
Andreas Gampec200a4a2014-06-16 18:39:09 -07001523 // Return the new bottom.
1524 return sp8;
1525 }
1526
1527 virtual void WalkHeader(BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm)
Mathieu Chartier90443472015-07-16 20:32:27 -07001528 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001529 UNUSED(sm);
1530 }
Andreas Gampec200a4a2014-06-16 18:39:09 -07001531
Mathieu Chartier90443472015-07-16 20:32:27 -07001532 void Walk(const char* shorty, uint32_t shorty_len) SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001533 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize> sm(this);
1534
1535 WalkHeader(&sm);
Andreas Gampec147b002014-03-06 18:11:06 -08001536
1537 for (uint32_t i = 1; i < shorty_len; ++i) {
1538 Primitive::Type cur_type_ = Primitive::GetType(shorty[i]);
1539 switch (cur_type_) {
1540 case Primitive::kPrimNot:
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001541 // TODO: fix abuse of mirror types.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001542 sm.AdvanceHandleScope(
1543 reinterpret_cast<mirror::Object*>(0x12345678));
Andreas Gampec147b002014-03-06 18:11:06 -08001544 break;
1545
1546 case Primitive::kPrimBoolean:
1547 case Primitive::kPrimByte:
1548 case Primitive::kPrimChar:
1549 case Primitive::kPrimShort:
1550 case Primitive::kPrimInt:
1551 sm.AdvanceInt(0);
1552 break;
1553 case Primitive::kPrimFloat:
1554 sm.AdvanceFloat(0);
1555 break;
1556 case Primitive::kPrimDouble:
1557 sm.AdvanceDouble(0);
1558 break;
1559 case Primitive::kPrimLong:
1560 sm.AdvanceLong(0);
1561 break;
1562 default:
1563 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001564 UNREACHABLE();
Andreas Gampec147b002014-03-06 18:11:06 -08001565 }
1566 }
1567
Ian Rogers1428dce2014-10-21 15:02:15 -07001568 num_stack_entries_ = sm.GetStackEntries();
Andreas Gampec147b002014-03-06 18:11:06 -08001569 }
1570
1571 void PushGpr(uintptr_t /* val */) {
1572 // not optimizing registers, yet
1573 }
1574
1575 void PushFpr4(float /* val */) {
1576 // not optimizing registers, yet
1577 }
1578
1579 void PushFpr8(uint64_t /* val */) {
1580 // not optimizing registers, yet
1581 }
1582
1583 void PushStack(uintptr_t /* val */) {
1584 // counting is already done in the superclass
1585 }
1586
Andreas Gampec200a4a2014-06-16 18:39:09 -07001587 virtual uintptr_t PushHandle(mirror::Object* /* ptr */) {
Andreas Gampec147b002014-03-06 18:11:06 -08001588 return reinterpret_cast<uintptr_t>(nullptr);
1589 }
1590
Andreas Gampec200a4a2014-06-16 18:39:09 -07001591 protected:
Andreas Gampec147b002014-03-06 18:11:06 -08001592 uint32_t num_stack_entries_;
1593};
1594
Andreas Gampec200a4a2014-06-16 18:39:09 -07001595class ComputeGenericJniFrameSize FINAL : public ComputeNativeCallFrameSize {
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001596 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001597 ComputeGenericJniFrameSize() : num_handle_scope_references_(0) {}
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001598
Andreas Gampec200a4a2014-06-16 18:39:09 -07001599 // Lays out the callee-save frame. Assumes that the incorrect frame corresponding to RefsAndArgs
1600 // is at *m = sp. Will update to point to the bottom of the save frame.
1601 //
1602 // Note: assumes ComputeAll() has been run before.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001603 void LayoutCalleeSaveFrame(Thread* self, ArtMethod*** m, void* sp, HandleScope** handle_scope)
Mathieu Chartier90443472015-07-16 20:32:27 -07001604 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001605 ArtMethod* method = **m;
1606
1607 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), sizeof(void*));
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001608
Andreas Gampec200a4a2014-06-16 18:39:09 -07001609 uint8_t* sp8 = reinterpret_cast<uint8_t*>(sp);
1610
1611 // First, fix up the layout of the callee-save frame.
1612 // We have to squeeze in the HandleScope, and relocate the method pointer.
1613
1614 // "Free" the slot for the method.
Ian Rogers13735952014-10-08 12:43:28 -07001615 sp8 += sizeof(void*); // In the callee-save frame we use a full pointer.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001616
1617 // Under the callee saves put handle scope and new method stack reference.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001618 size_t handle_scope_size = HandleScope::SizeOf(num_handle_scope_references_);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001619 size_t scope_and_method = handle_scope_size + sizeof(ArtMethod*);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001620
1621 sp8 -= scope_and_method;
1622 // Align by kStackAlignment.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001623 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
Andreas Gampec200a4a2014-06-16 18:39:09 -07001624
Mathieu Chartiere401d142015-04-22 13:56:20 -07001625 uint8_t* sp8_table = sp8 + sizeof(ArtMethod*);
Ian Rogers59c07062014-10-10 13:03:39 -07001626 *handle_scope = HandleScope::Create(sp8_table, self->GetTopHandleScope(),
1627 num_handle_scope_references_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001628
1629 // Add a slot for the method pointer, and fill it. Fix the pointer-pointer given to us.
1630 uint8_t* method_pointer = sp8;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001631 auto** new_method_ref = reinterpret_cast<ArtMethod**>(method_pointer);
1632 *new_method_ref = method;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001633 *m = new_method_ref;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001634 }
1635
Andreas Gampec200a4a2014-06-16 18:39:09 -07001636 // Adds space for the cookie. Note: may leave stack unaligned.
Ian Rogers1428dce2014-10-21 15:02:15 -07001637 void LayoutCookie(uint8_t** sp) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001638 // Reference cookie and padding
1639 *sp -= 8;
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001640 }
1641
Andreas Gampec200a4a2014-06-16 18:39:09 -07001642 // Re-layout the callee-save frame (insert a handle-scope). Then add space for the cookie.
1643 // Returns the new bottom. Note: this may be unaligned.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001644 uint8_t* LayoutJNISaveFrame(Thread* self, ArtMethod*** m, void* sp, HandleScope** handle_scope)
Mathieu Chartier90443472015-07-16 20:32:27 -07001645 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001646 // First, fix up the layout of the callee-save frame.
1647 // We have to squeeze in the HandleScope, and relocate the method pointer.
Ian Rogers59c07062014-10-10 13:03:39 -07001648 LayoutCalleeSaveFrame(self, m, sp, handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001649
1650 // The bottom of the callee-save frame is now where the method is, *m.
1651 uint8_t* sp8 = reinterpret_cast<uint8_t*>(*m);
1652
1653 // Add space for cookie.
1654 LayoutCookie(&sp8);
1655
1656 return sp8;
1657 }
1658
1659 // WARNING: After this, *sp won't be pointing to the method anymore!
Mathieu Chartiere401d142015-04-22 13:56:20 -07001660 uint8_t* ComputeLayout(Thread* self, ArtMethod*** m, const char* shorty, uint32_t shorty_len,
1661 HandleScope** handle_scope, uintptr_t** start_stack, uintptr_t** start_gpr,
1662 uint32_t** start_fpr)
Mathieu Chartier90443472015-07-16 20:32:27 -07001663 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001664 Walk(shorty, shorty_len);
1665
1666 // JNI part.
Ian Rogers59c07062014-10-10 13:03:39 -07001667 uint8_t* sp8 = LayoutJNISaveFrame(self, m, reinterpret_cast<void*>(*m), handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001668
1669 sp8 = LayoutNativeCall(sp8, start_stack, start_gpr, start_fpr);
1670
1671 // Return the new bottom.
1672 return sp8;
1673 }
1674
1675 uintptr_t PushHandle(mirror::Object* /* ptr */) OVERRIDE;
1676
1677 // Add JNIEnv* and jobj/jclass before the shorty-derived elements.
1678 void WalkHeader(BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) OVERRIDE
Mathieu Chartier90443472015-07-16 20:32:27 -07001679 SHARED_REQUIRES(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001680
1681 private:
1682 uint32_t num_handle_scope_references_;
1683};
1684
1685uintptr_t ComputeGenericJniFrameSize::PushHandle(mirror::Object* /* ptr */) {
1686 num_handle_scope_references_++;
1687 return reinterpret_cast<uintptr_t>(nullptr);
1688}
1689
1690void ComputeGenericJniFrameSize::WalkHeader(
1691 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) {
1692 // JNIEnv
1693 sm->AdvancePointer(nullptr);
1694
1695 // Class object or this as first argument
1696 sm->AdvanceHandleScope(reinterpret_cast<mirror::Object*>(0x12345678));
1697}
1698
1699// Class to push values to three separate regions. Used to fill the native call part. Adheres to
1700// the template requirements of BuildGenericJniFrameStateMachine.
1701class FillNativeCall {
1702 public:
1703 FillNativeCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) :
1704 cur_gpr_reg_(gpr_regs), cur_fpr_reg_(fpr_regs), cur_stack_arg_(stack_args) {}
1705
1706 virtual ~FillNativeCall() {}
1707
1708 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) {
1709 cur_gpr_reg_ = gpr_regs;
1710 cur_fpr_reg_ = fpr_regs;
1711 cur_stack_arg_ = stack_args;
Andreas Gampec147b002014-03-06 18:11:06 -08001712 }
1713
1714 void PushGpr(uintptr_t val) {
1715 *cur_gpr_reg_ = val;
1716 cur_gpr_reg_++;
1717 }
1718
1719 void PushFpr4(float val) {
1720 *cur_fpr_reg_ = val;
1721 cur_fpr_reg_++;
1722 }
1723
1724 void PushFpr8(uint64_t val) {
1725 uint64_t* tmp = reinterpret_cast<uint64_t*>(cur_fpr_reg_);
1726 *tmp = val;
1727 cur_fpr_reg_ += 2;
1728 }
1729
1730 void PushStack(uintptr_t val) {
1731 *cur_stack_arg_ = val;
1732 cur_stack_arg_++;
1733 }
1734
Mathieu Chartier90443472015-07-16 20:32:27 -07001735 virtual uintptr_t PushHandle(mirror::Object*) SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001736 LOG(FATAL) << "(Non-JNI) Native call does not use handles.";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001737 UNREACHABLE();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001738 }
1739
1740 private:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001741 uintptr_t* cur_gpr_reg_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001742 uint32_t* cur_fpr_reg_;
1743 uintptr_t* cur_stack_arg_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001744};
Andreas Gampec147b002014-03-06 18:11:06 -08001745
Andreas Gampec200a4a2014-06-16 18:39:09 -07001746// Visits arguments on the stack placing them into a region lower down the stack for the benefit
1747// of transitioning into native code.
1748class BuildGenericJniFrameVisitor FINAL : public QuickArgumentVisitor {
1749 public:
Ian Rogers59c07062014-10-10 13:03:39 -07001750 BuildGenericJniFrameVisitor(Thread* self, bool is_static, const char* shorty, uint32_t shorty_len,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001751 ArtMethod*** sp)
Andreas Gampec200a4a2014-06-16 18:39:09 -07001752 : QuickArgumentVisitor(*sp, is_static, shorty, shorty_len),
1753 jni_call_(nullptr, nullptr, nullptr, nullptr), sm_(&jni_call_) {
1754 ComputeGenericJniFrameSize fsc;
1755 uintptr_t* start_gpr_reg;
1756 uint32_t* start_fpr_reg;
1757 uintptr_t* start_stack_arg;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001758 bottom_of_used_area_ = fsc.ComputeLayout(self, sp, shorty, shorty_len,
Ian Rogers59c07062014-10-10 13:03:39 -07001759 &handle_scope_,
1760 &start_stack_arg,
Andreas Gampec200a4a2014-06-16 18:39:09 -07001761 &start_gpr_reg, &start_fpr_reg);
1762
Andreas Gampec200a4a2014-06-16 18:39:09 -07001763 jni_call_.Reset(start_gpr_reg, start_fpr_reg, start_stack_arg, handle_scope_);
1764
1765 // jni environment is always first argument
1766 sm_.AdvancePointer(self->GetJniEnv());
1767
1768 if (is_static) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001769 sm_.AdvanceHandleScope((**sp)->GetDeclaringClass());
Andreas Gampec200a4a2014-06-16 18:39:09 -07001770 }
1771 }
1772
Mathieu Chartier90443472015-07-16 20:32:27 -07001773 void Visit() SHARED_REQUIRES(Locks::mutator_lock_) OVERRIDE;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001774
Mathieu Chartier90443472015-07-16 20:32:27 -07001775 void FinalizeHandleScope(Thread* self) SHARED_REQUIRES(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001776
1777 StackReference<mirror::Object>* GetFirstHandleScopeEntry()
Mathieu Chartier90443472015-07-16 20:32:27 -07001778 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001779 return handle_scope_->GetHandle(0).GetReference();
1780 }
1781
Mathieu Chartier90443472015-07-16 20:32:27 -07001782 jobject GetFirstHandleScopeJObject() const SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001783 return handle_scope_->GetHandle(0).ToJObject();
1784 }
1785
Ian Rogers1428dce2014-10-21 15:02:15 -07001786 void* GetBottomOfUsedArea() const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001787 return bottom_of_used_area_;
1788 }
1789
1790 private:
1791 // A class to fill a JNI call. Adds reference/handle-scope management to FillNativeCall.
1792 class FillJniCall FINAL : public FillNativeCall {
1793 public:
1794 FillJniCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args,
1795 HandleScope* handle_scope) : FillNativeCall(gpr_regs, fpr_regs, stack_args),
1796 handle_scope_(handle_scope), cur_entry_(0) {}
1797
Mathieu Chartier90443472015-07-16 20:32:27 -07001798 uintptr_t PushHandle(mirror::Object* ref) OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001799
1800 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args, HandleScope* scope) {
1801 FillNativeCall::Reset(gpr_regs, fpr_regs, stack_args);
1802 handle_scope_ = scope;
1803 cur_entry_ = 0U;
1804 }
1805
Mathieu Chartier90443472015-07-16 20:32:27 -07001806 void ResetRemainingScopeSlots() SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001807 // Initialize padding entries.
1808 size_t expected_slots = handle_scope_->NumberOfReferences();
1809 while (cur_entry_ < expected_slots) {
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07001810 handle_scope_->GetMutableHandle(cur_entry_++).Assign(nullptr);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001811 }
1812 DCHECK_NE(cur_entry_, 0U);
1813 }
1814
1815 private:
1816 HandleScope* handle_scope_;
1817 size_t cur_entry_;
1818 };
1819
1820 HandleScope* handle_scope_;
1821 FillJniCall jni_call_;
1822 void* bottom_of_used_area_;
1823
1824 BuildNativeCallFrameStateMachine<FillJniCall> sm_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001825
1826 DISALLOW_COPY_AND_ASSIGN(BuildGenericJniFrameVisitor);
1827};
1828
Andreas Gampec200a4a2014-06-16 18:39:09 -07001829uintptr_t BuildGenericJniFrameVisitor::FillJniCall::PushHandle(mirror::Object* ref) {
1830 uintptr_t tmp;
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07001831 MutableHandle<mirror::Object> h = handle_scope_->GetMutableHandle(cur_entry_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001832 h.Assign(ref);
1833 tmp = reinterpret_cast<uintptr_t>(h.ToJObject());
1834 cur_entry_++;
1835 return tmp;
1836}
1837
Ian Rogers9758f792014-03-13 09:02:55 -07001838void BuildGenericJniFrameVisitor::Visit() {
1839 Primitive::Type type = GetParamPrimitiveType();
1840 switch (type) {
1841 case Primitive::kPrimLong: {
1842 jlong long_arg;
1843 if (IsSplitLongOrDouble()) {
1844 long_arg = ReadSplitLongParam();
1845 } else {
1846 long_arg = *reinterpret_cast<jlong*>(GetParamAddress());
1847 }
1848 sm_.AdvanceLong(long_arg);
1849 break;
1850 }
1851 case Primitive::kPrimDouble: {
1852 uint64_t double_arg;
1853 if (IsSplitLongOrDouble()) {
1854 // Read into union so that we don't case to a double.
1855 double_arg = ReadSplitLongParam();
1856 } else {
1857 double_arg = *reinterpret_cast<uint64_t*>(GetParamAddress());
1858 }
1859 sm_.AdvanceDouble(double_arg);
1860 break;
1861 }
1862 case Primitive::kPrimNot: {
1863 StackReference<mirror::Object>* stack_ref =
1864 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001865 sm_.AdvanceHandleScope(stack_ref->AsMirrorPtr());
Ian Rogers9758f792014-03-13 09:02:55 -07001866 break;
1867 }
1868 case Primitive::kPrimFloat:
1869 sm_.AdvanceFloat(*reinterpret_cast<float*>(GetParamAddress()));
1870 break;
1871 case Primitive::kPrimBoolean: // Fall-through.
1872 case Primitive::kPrimByte: // Fall-through.
1873 case Primitive::kPrimChar: // Fall-through.
1874 case Primitive::kPrimShort: // Fall-through.
1875 case Primitive::kPrimInt: // Fall-through.
1876 sm_.AdvanceInt(*reinterpret_cast<jint*>(GetParamAddress()));
1877 break;
1878 case Primitive::kPrimVoid:
1879 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -07001880 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -07001881 }
1882}
1883
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001884void BuildGenericJniFrameVisitor::FinalizeHandleScope(Thread* self) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001885 // Clear out rest of the scope.
1886 jni_call_.ResetRemainingScopeSlots();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001887 // Install HandleScope.
1888 self->PushHandleScope(handle_scope_);
Ian Rogers9758f792014-03-13 09:02:55 -07001889}
1890
Ian Rogers04c31d22014-07-07 21:44:06 -07001891#if defined(__arm__) || defined(__aarch64__)
Andreas Gampe90546832014-03-12 18:07:19 -07001892extern "C" void* artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07001893#else
1894extern "C" void* artFindNativeMethod(Thread* self);
1895#endif
Andreas Gampe90546832014-03-12 18:07:19 -07001896
Andreas Gampead615172014-04-04 16:20:13 -07001897uint64_t artQuickGenericJniEndJNIRef(Thread* self, uint32_t cookie, jobject l, jobject lock) {
1898 if (lock != nullptr) {
1899 return reinterpret_cast<uint64_t>(JniMethodEndWithReferenceSynchronized(l, cookie, lock, self));
1900 } else {
1901 return reinterpret_cast<uint64_t>(JniMethodEndWithReference(l, cookie, self));
1902 }
1903}
1904
1905void artQuickGenericJniEndJNINonRef(Thread* self, uint32_t cookie, jobject lock) {
1906 if (lock != nullptr) {
1907 JniMethodEndSynchronized(cookie, lock, self);
1908 } else {
1909 JniMethodEnd(cookie, self);
1910 }
1911}
1912
Andreas Gampec147b002014-03-06 18:11:06 -08001913/*
1914 * Initializes an alloca region assumed to be directly below sp for a native call:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001915 * 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 -08001916 * The final element on the stack is a pointer to the native code.
1917 *
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001918 * On entry, the stack has a standard callee-save frame above sp, and an alloca below it.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001919 * We need to fix this, as the handle scope needs to go into the callee-save frame.
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001920 *
Andreas Gampec147b002014-03-06 18:11:06 -08001921 * The return of this function denotes:
1922 * 1) How many bytes of the alloca can be released, if the value is non-negative.
1923 * 2) An error, if the value is negative.
1924 */
Mathieu Chartiere401d142015-04-22 13:56:20 -07001925extern "C" TwoWordReturn artQuickGenericJniTrampoline(Thread* self, ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -07001926 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001927 ArtMethod* called = *sp;
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001928 DCHECK(called->IsNative()) << PrettyMethod(called, true);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001929 uint32_t shorty_len = 0;
1930 const char* shorty = called->GetShorty(&shorty_len);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001931
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001932 // Run the visitor and update sp.
Ian Rogers59c07062014-10-10 13:03:39 -07001933 BuildGenericJniFrameVisitor visitor(self, called->IsStatic(), shorty, shorty_len, &sp);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001934 visitor.VisitArguments();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001935 visitor.FinalizeHandleScope(self);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001936
Andreas Gampec200a4a2014-06-16 18:39:09 -07001937 // Fix up managed-stack things in Thread.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001938 self->SetTopOfStack(sp);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001939
Ian Rogerse0dcd462014-03-08 15:21:04 -08001940 self->VerifyStack();
1941
Andreas Gampe90546832014-03-12 18:07:19 -07001942 // Start JNI, save the cookie.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001943 uint32_t cookie;
1944 if (called->IsSynchronized()) {
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001945 cookie = JniMethodStartSynchronized(visitor.GetFirstHandleScopeJObject(), self);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001946 if (self->IsExceptionPending()) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001947 self->PopHandleScope();
Andreas Gampec147b002014-03-06 18:11:06 -08001948 // A negative value denotes an error.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001949 return GetTwoWordFailureValue();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001950 }
1951 } else {
1952 cookie = JniMethodStart(self);
1953 }
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001954 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
Ian Rogerse0dcd462014-03-08 15:21:04 -08001955 *(sp32 - 1) = cookie;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001956
Andreas Gampe90546832014-03-12 18:07:19 -07001957 // Retrieve the stored native code.
Mathieu Chartier2d721012014-11-10 11:08:06 -08001958 void* nativeCode = called->GetEntryPointFromJni();
Andreas Gampe90546832014-03-12 18:07:19 -07001959
Andreas Gampe9a6a99a2014-03-14 07:52:20 -07001960 // There are two cases for the content of nativeCode:
1961 // 1) Pointer to the native function.
1962 // 2) Pointer to the trampoline for native code binding.
1963 // In the second case, we need to execute the binding and continue with the actual native function
1964 // pointer.
Andreas Gampe90546832014-03-12 18:07:19 -07001965 DCHECK(nativeCode != nullptr);
1966 if (nativeCode == GetJniDlsymLookupStub()) {
Ian Rogers04c31d22014-07-07 21:44:06 -07001967#if defined(__arm__) || defined(__aarch64__)
Andreas Gampe90546832014-03-12 18:07:19 -07001968 nativeCode = artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07001969#else
1970 nativeCode = artFindNativeMethod(self);
1971#endif
Andreas Gampe90546832014-03-12 18:07:19 -07001972
1973 if (nativeCode == nullptr) {
1974 DCHECK(self->IsExceptionPending()); // There should be an exception pending now.
Andreas Gampead615172014-04-04 16:20:13 -07001975
1976 // End JNI, as the assembly will move to deliver the exception.
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001977 jobject lock = called->IsSynchronized() ? visitor.GetFirstHandleScopeJObject() : nullptr;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001978 if (shorty[0] == 'L') {
Andreas Gampead615172014-04-04 16:20:13 -07001979 artQuickGenericJniEndJNIRef(self, cookie, nullptr, lock);
1980 } else {
1981 artQuickGenericJniEndJNINonRef(self, cookie, lock);
1982 }
1983
Andreas Gampec200a4a2014-06-16 18:39:09 -07001984 return GetTwoWordFailureValue();
Andreas Gampe90546832014-03-12 18:07:19 -07001985 }
1986 // Note that the native code pointer will be automatically set by artFindNativeMethod().
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001987 }
1988
Andreas Gampec200a4a2014-06-16 18:39:09 -07001989 // Return native code addr(lo) and bottom of alloca address(hi).
1990 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(visitor.GetBottomOfUsedArea()),
1991 reinterpret_cast<uintptr_t>(nativeCode));
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001992}
1993
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07001994// Defined in quick_jni_entrypoints.cc.
1995extern uint64_t GenericJniMethodEnd(Thread* self, uint32_t saved_local_ref_cookie,
1996 jvalue result, uint64_t result_f, ArtMethod* called,
1997 HandleScope* handle_scope);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001998/*
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001999 * Is called after the native JNI code. Responsible for cleanup (handle scope, saved state) and
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002000 * unlocking.
2001 */
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002002extern "C" uint64_t artQuickGenericJniEndTrampoline(Thread* self,
2003 jvalue result,
2004 uint64_t result_f) {
2005 // We're here just back from a native call. We don't have the shared mutator lock at this point
2006 // yet until we call GoToRunnable() later in GenericJniMethodEnd(). Accessing objects or doing
2007 // anything that requires a mutator lock before that would cause problems as GC may have the
2008 // exclusive mutator lock and may be moving objects, etc.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002009 ArtMethod** sp = self->GetManagedStack()->GetTopQuickFrame();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002010 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002011 ArtMethod* called = *sp;
Ian Rogerse0dcd462014-03-08 15:21:04 -08002012 uint32_t cookie = *(sp32 - 1);
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002013 HandleScope* table = reinterpret_cast<HandleScope*>(reinterpret_cast<uint8_t*>(sp) + sizeof(*sp));
2014 return GenericJniMethodEnd(self, cookie, result, result_f, called, table);
Andreas Gampe2da88232014-02-27 12:26:20 -08002015}
2016
Andreas Gamped58342c2014-06-05 14:18:08 -07002017// We use TwoWordReturn to optimize scalar returns. We use the hi value for code, and the lo value
2018// for the method pointer.
Andreas Gampe51f76352014-05-21 08:28:48 -07002019//
Andreas Gamped58342c2014-06-05 14:18:08 -07002020// 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 -07002021// to hold the mutator lock (see SHARED_REQUIRES(Locks::mutator_lock_) annotations).
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002022
2023template<InvokeType type, bool access_check>
Mathieu Chartiere401d142015-04-22 13:56:20 -07002024static TwoWordReturn artInvokeCommon(uint32_t method_idx, mirror::Object* this_object, Thread* self,
2025 ArtMethod** sp) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002026 ScopedQuickEntrypointChecks sqec(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002027 DCHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs));
2028 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
2029 ArtMethod* method = FindMethodFast(method_idx, this_object, caller_method, access_check, type);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002030 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002031 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()->GetDexFile();
2032 uint32_t shorty_len;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002033 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(method_idx), &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002034 {
2035 // Remember the args in case a GC happens in FindMethodFromCode.
2036 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
2037 RememberForGcArgumentVisitor visitor(sp, type == kStatic, shorty, shorty_len, &soa);
2038 visitor.VisitArguments();
Andreas Gampe3a357142015-08-07 17:20:11 -07002039 method = FindMethodFromCode<type, access_check>(method_idx, &this_object, caller_method,
Mathieu Chartier0cd81352014-05-22 16:48:55 -07002040 self);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002041 visitor.FixupReferences();
2042 }
2043
Ian Rogerse0a02da2014-12-02 14:10:53 -08002044 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002045 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07002046 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002047 }
2048 }
2049 DCHECK(!self->IsExceptionPending());
2050 const void* code = method->GetEntryPointFromQuickCompiledCode();
2051
2052 // When we return, the caller will branch to this address, so it had better not be 0!
Ian Rogerse0a02da2014-12-02 14:10:53 -08002053 DCHECK(code != nullptr) << "Code was null in method: " << PrettyMethod(method)
Andreas Gampec200a4a2014-06-16 18:39:09 -07002054 << " location: "
2055 << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07002056
Andreas Gamped58342c2014-06-05 14:18:08 -07002057 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
2058 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002059}
2060
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01002061// Explicit artInvokeCommon template function declarations to please analysis tool.
2062#define EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(type, access_check) \
Mathieu Chartier90443472015-07-16 20:32:27 -07002063 template SHARED_REQUIRES(Locks::mutator_lock_) \
Mathieu Chartiere401d142015-04-22 13:56:20 -07002064 TwoWordReturn artInvokeCommon<type, access_check>( \
2065 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01002066
2067EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, false);
2068EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, true);
2069EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, false);
2070EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, true);
2071EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, false);
2072EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, true);
2073EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, false);
2074EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, true);
2075EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, false);
2076EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, true);
2077#undef EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL
2078
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002079// See comments in runtime_support_asm.S
Andreas Gampec200a4a2014-06-16 18:39:09 -07002080extern "C" TwoWordReturn artInvokeInterfaceTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002081 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -07002082 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002083 return artInvokeCommon<kInterface, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002084}
2085
Andreas Gampec200a4a2014-06-16 18:39:09 -07002086extern "C" TwoWordReturn artInvokeDirectTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002087 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -07002088 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002089 return artInvokeCommon<kDirect, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002090}
2091
Andreas Gampec200a4a2014-06-16 18:39:09 -07002092extern "C" TwoWordReturn artInvokeStaticTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002093 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -07002094 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002095 return artInvokeCommon<kStatic, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002096}
2097
Andreas Gampec200a4a2014-06-16 18:39:09 -07002098extern "C" TwoWordReturn artInvokeSuperTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002099 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -07002100 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002101 return artInvokeCommon<kSuper, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002102}
2103
Andreas Gampec200a4a2014-06-16 18:39:09 -07002104extern "C" TwoWordReturn artInvokeVirtualTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002105 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -07002106 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002107 return artInvokeCommon<kVirtual, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002108}
2109
2110// Determine target of interface dispatch. This object is known non-null.
Nicolas Geoffray8ea18d02015-05-26 16:29:08 +01002111extern "C" TwoWordReturn artInvokeInterfaceTrampoline(uint32_t dex_method_idx,
Andreas Gampe51f76352014-05-21 08:28:48 -07002112 mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -07002113 Thread* self, ArtMethod** sp)
Mathieu Chartier90443472015-07-16 20:32:27 -07002114 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002115 ScopedQuickEntrypointChecks sqec(self);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002116 // The optimizing compiler currently does not inline methods that have an interface
2117 // invocation. We use the outer method directly to avoid fetching a stack map, which is
2118 // more expensive.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002119 ArtMethod* caller_method = QuickArgumentVisitor::GetOuterMethod(sp);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002120 DCHECK_EQ(caller_method, QuickArgumentVisitor::GetCallingMethod(sp));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002121 ArtMethod* interface_method = caller_method->GetDexCacheResolvedMethod(
2122 dex_method_idx, sizeof(void*));
2123 DCHECK(interface_method != nullptr) << dex_method_idx << " " << PrettyMethod(caller_method);
2124 ArtMethod* method;
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002125 if (LIKELY(interface_method->GetDexMethodIndex() != DexFile::kDexNoIndex)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002126 method = this_object->GetClass()->FindVirtualMethodForInterface(
2127 interface_method, sizeof(void*));
Ian Rogerse0a02da2014-12-02 14:10:53 -08002128 if (UNLIKELY(method == nullptr)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002129 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(
2130 interface_method, this_object, caller_method);
Andreas Gamped58342c2014-06-05 14:18:08 -07002131 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002132 }
2133 } else {
Mathieu Chartier4edd8472015-06-01 10:47:36 -07002134 DCHECK_EQ(interface_method, Runtime::Current()->GetResolutionMethod());
Nicolas Geoffray8ea18d02015-05-26 16:29:08 +01002135 if (kIsDebugBuild) {
2136 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
2137 const DexFile::CodeItem* code = caller_method->GetCodeItem();
2138 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
2139 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
2140 Instruction::Code instr_code = instr->Opcode();
2141 CHECK(instr_code == Instruction::INVOKE_INTERFACE ||
2142 instr_code == Instruction::INVOKE_INTERFACE_RANGE)
2143 << "Unexpected call into interface trampoline: " << instr->DumpString(nullptr);
2144 if (instr_code == Instruction::INVOKE_INTERFACE) {
2145 CHECK_EQ(dex_method_idx, instr->VRegB_35c());
2146 } else {
2147 CHECK_EQ(instr_code, Instruction::INVOKE_INTERFACE_RANGE);
2148 CHECK_EQ(dex_method_idx, instr->VRegB_3rc());
2149 }
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002150 }
2151
Andreas Gampec200a4a2014-06-16 18:39:09 -07002152 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()
2153 ->GetDexFile();
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002154 uint32_t shorty_len;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002155 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(dex_method_idx),
2156 &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002157 {
2158 // Remember the args in case a GC happens in FindMethodFromCode.
2159 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
2160 RememberForGcArgumentVisitor visitor(sp, false, shorty, shorty_len, &soa);
2161 visitor.VisitArguments();
Andreas Gampe3a357142015-08-07 17:20:11 -07002162 method = FindMethodFromCode<kInterface, false>(dex_method_idx, &this_object, caller_method,
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002163 self);
2164 visitor.FixupReferences();
2165 }
2166
2167 if (UNLIKELY(method == nullptr)) {
2168 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07002169 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002170 }
2171 }
2172 const void* code = method->GetEntryPointFromQuickCompiledCode();
2173
2174 // When we return, the caller will branch to this address, so it had better not be 0!
Ian Rogerse0a02da2014-12-02 14:10:53 -08002175 DCHECK(code != nullptr) << "Code was null in method: " << PrettyMethod(method)
Andreas Gampec200a4a2014-06-16 18:39:09 -07002176 << " location: " << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07002177
Andreas Gamped58342c2014-06-05 14:18:08 -07002178 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
2179 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002180}
2181
Ian Rogers848871b2013-08-05 10:56:33 -07002182} // namespace art