blob: 3043c836680037263f25ecc9c43cf2ad2161ce6d [file] [log] [blame]
Ian Rogers848871b2013-08-05 10:56:33 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Mathieu Chartiere401d142015-04-22 13:56:20 -070017#include "art_method-inl.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070018#include "base/enums.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"
Nicolas Geoffray796d6302016-03-13 22:22:31 +000027#include "linear_alloc.h"
Ian Rogerse0a02da2014-12-02 14:10:53 -080028#include "method_reference.h"
Ian Rogers848871b2013-08-05 10:56:33 -070029#include "mirror/class-inl.h"
Mathieu Chartier5f3ded42014-04-03 15:25:30 -070030#include "mirror/dex_cache-inl.h"
Mathieu Chartierfc58af42015-04-16 18:00:39 -070031#include "mirror/method.h"
Ian Rogers848871b2013-08-05 10:56:33 -070032#include "mirror/object-inl.h"
33#include "mirror/object_array-inl.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010034#include "oat_quick_method_header.h"
Andreas Gampe639bdd12015-06-03 11:22:45 -070035#include "quick_exception_handler.h"
Ian Rogers848871b2013-08-05 10:56:33 -070036#include "runtime.h"
Ian Rogers53b8b092014-03-13 23:45:53 -070037#include "scoped_thread_state_change.h"
Andreas Gampeb3025922015-09-01 14:45:00 -070038#include "stack.h"
Daniel Mihalyieb076692014-08-22 17:33:31 +020039#include "debugger.h"
Ian Rogers848871b2013-08-05 10:56:33 -070040
41namespace art {
42
43// Visits the arguments as saved to the stack by a Runtime::kRefAndArgs callee save frame.
44class QuickArgumentVisitor {
Ian Rogers936b37f2014-02-14 00:52:24 -080045 // Number of bytes for each out register in the caller method's frame.
46 static constexpr size_t kBytesStackArgLocation = 4;
Alexei Zavjalov41c507a2014-05-15 16:02:46 +070047 // Frame size in bytes of a callee-save frame for RefsAndArgs.
48 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize =
Vladimir Markofd36f1f2016-08-03 18:49:58 +010049 GetCalleeSaveFrameSize(kRuntimeISA, Runtime::kSaveRefsAndArgs);
Ian Rogers848871b2013-08-05 10:56:33 -070050#if defined(__arm__)
51 // The callee save frame is pointed to by SP.
52 // | argN | |
53 // | ... | |
54 // | arg4 | |
55 // | arg3 spill | | Caller's frame
56 // | arg2 spill | |
57 // | arg1 spill | |
58 // | Method* | ---
59 // | LR |
Zheng Xu5667fdb2014-10-23 18:29:55 +080060 // | ... | 4x6 bytes callee saves
61 // | R3 |
62 // | R2 |
63 // | R1 |
64 // | S15 |
65 // | : |
66 // | S0 |
67 // | | 4x2 bytes padding
Ian Rogers848871b2013-08-05 10:56:33 -070068 // | Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -050069 static constexpr bool kSplitPairAcrossRegisterAndStack = kArm32QuickCodeUseSoftFloat;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +000070 static constexpr bool kAlignPairRegister = !kArm32QuickCodeUseSoftFloat;
Zheng Xu5667fdb2014-10-23 18:29:55 +080071 static constexpr bool kQuickSoftFloatAbi = kArm32QuickCodeUseSoftFloat;
72 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = !kArm32QuickCodeUseSoftFloat;
Goran Jakovljevicff734982015-08-24 12:58:55 +000073 static constexpr bool kQuickSkipOddFpRegisters = false;
Zheng Xu5667fdb2014-10-23 18:29:55 +080074 static constexpr size_t kNumQuickGprArgs = 3;
75 static constexpr size_t kNumQuickFprArgs = kArm32QuickCodeUseSoftFloat ? 0 : 16;
Andreas Gampe1a5c4062015-01-15 12:10:47 -080076 static constexpr bool kGprFprLockstep = false;
Zheng Xub551fdc2014-07-25 11:49:42 +080077 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset =
Vladimir Markofd36f1f2016-08-03 18:49:58 +010078 arm::ArmCalleeSaveFpr1Offset(Runtime::kSaveRefsAndArgs); // Offset of first FPR arg.
Zheng Xub551fdc2014-07-25 11:49:42 +080079 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset =
Vladimir Markofd36f1f2016-08-03 18:49:58 +010080 arm::ArmCalleeSaveGpr1Offset(Runtime::kSaveRefsAndArgs); // Offset of first GPR arg.
Zheng Xub551fdc2014-07-25 11:49:42 +080081 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset =
Vladimir Markofd36f1f2016-08-03 18:49:58 +010082 arm::ArmCalleeSaveLrOffset(Runtime::kSaveRefsAndArgs); // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -080083 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +000084 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -080085 }
Stuart Monteithb95a5342014-03-12 13:32:32 +000086#elif defined(__aarch64__)
87 // The callee save frame is pointed to by SP.
88 // | argN | |
89 // | ... | |
90 // | arg4 | |
91 // | arg3 spill | | Caller's frame
92 // | arg2 spill | |
93 // | arg1 spill | |
94 // | Method* | ---
95 // | LR |
Zheng Xub551fdc2014-07-25 11:49:42 +080096 // | X29 |
Stuart Monteithb95a5342014-03-12 13:32:32 +000097 // | : |
Serban Constantinescu9bd88b02015-04-22 16:24:46 +010098 // | X20 |
Stuart Monteithb95a5342014-03-12 13:32:32 +000099 // | X7 |
100 // | : |
101 // | X1 |
Zheng Xub551fdc2014-07-25 11:49:42 +0800102 // | D7 |
Stuart Monteithb95a5342014-03-12 13:32:32 +0000103 // | : |
104 // | D0 |
105 // | | padding
106 // | Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500107 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000108 static constexpr bool kAlignPairRegister = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000109 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800110 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000111 static constexpr bool kQuickSkipOddFpRegisters = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000112 static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs.
113 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800114 static constexpr bool kGprFprLockstep = false;
Zheng Xub551fdc2014-07-25 11:49:42 +0800115 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset =
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100116 arm64::Arm64CalleeSaveFpr1Offset(Runtime::kSaveRefsAndArgs); // Offset of first FPR arg.
Zheng Xub551fdc2014-07-25 11:49:42 +0800117 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset =
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100118 arm64::Arm64CalleeSaveGpr1Offset(Runtime::kSaveRefsAndArgs); // Offset of first GPR arg.
Zheng Xub551fdc2014-07-25 11:49:42 +0800119 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset =
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100120 arm64::Arm64CalleeSaveLrOffset(Runtime::kSaveRefsAndArgs); // Offset of return address.
Stuart Monteithb95a5342014-03-12 13:32:32 +0000121 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000122 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Stuart Monteithb95a5342014-03-12 13:32:32 +0000123 }
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800124#elif defined(__mips__) && !defined(__LP64__)
Ian Rogers848871b2013-08-05 10:56:33 -0700125 // The callee save frame is pointed to by SP.
126 // | argN | |
127 // | ... | |
128 // | arg4 | |
129 // | arg3 spill | | Caller's frame
130 // | arg2 spill | |
131 // | arg1 spill | |
132 // | Method* | ---
133 // | RA |
134 // | ... | callee saves
135 // | A3 | arg3
136 // | A2 | arg2
137 // | A1 | arg1
Goran Jakovljevicff734982015-08-24 12:58:55 +0000138 // | F15 |
139 // | F14 | f_arg1
140 // | F13 |
141 // | F12 | f_arg0
142 // | | padding
Ian Rogers848871b2013-08-05 10:56:33 -0700143 // | A0/Method* | <- sp
Goran Jakovljevicff734982015-08-24 12:58:55 +0000144 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
145 static constexpr bool kAlignPairRegister = true;
146 static constexpr bool kQuickSoftFloatAbi = false;
Zheng Xu5667fdb2014-10-23 18:29:55 +0800147 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000148 static constexpr bool kQuickSkipOddFpRegisters = true;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800149 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs.
Goran Jakovljevicff734982015-08-24 12:58:55 +0000150 static constexpr size_t kNumQuickFprArgs = 4; // 2 arguments passed in FPRs. Floats can be passed
151 // only in even numbered registers and each double
152 // occupies two registers.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800153 static constexpr bool kGprFprLockstep = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000154 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 16; // Offset of first FPR arg.
155 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 32; // Offset of first GPR arg.
156 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 76; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800157 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000158 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800159 }
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800160#elif defined(__mips__) && defined(__LP64__)
161 // The callee save frame is pointed to by SP.
162 // | argN | |
163 // | ... | |
164 // | arg4 | |
165 // | arg3 spill | | Caller's frame
166 // | arg2 spill | |
167 // | arg1 spill | |
168 // | Method* | ---
169 // | RA |
170 // | ... | callee saves
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800171 // | A7 | arg7
172 // | A6 | arg6
173 // | A5 | arg5
174 // | A4 | arg4
175 // | A3 | arg3
176 // | A2 | arg2
177 // | A1 | arg1
Goran Jakovljevicff734982015-08-24 12:58:55 +0000178 // | F19 | f_arg7
179 // | F18 | f_arg6
180 // | F17 | f_arg5
181 // | F16 | f_arg4
182 // | F15 | f_arg3
183 // | F14 | f_arg2
184 // | F13 | f_arg1
185 // | F12 | f_arg0
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800186 // | | padding
187 // | A0/Method* | <- sp
188 // NOTE: for Mip64, when A0 is skipped, F0 is also skipped.
Douglas Leungd18e0832015-02-09 15:22:26 -0800189 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800190 static constexpr bool kAlignPairRegister = false;
191 static constexpr bool kQuickSoftFloatAbi = false;
192 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000193 static constexpr bool kQuickSkipOddFpRegisters = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800194 static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs.
195 static constexpr size_t kNumQuickFprArgs = 7; // 7 arguments passed in FPRs.
196 static constexpr bool kGprFprLockstep = true;
197
198 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 24; // Offset of first FPR arg (F1).
199 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 80; // Offset of first GPR arg (A1).
200 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 200; // Offset of return address.
201 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
202 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
203 }
Ian Rogers848871b2013-08-05 10:56:33 -0700204#elif defined(__i386__)
205 // The callee save frame is pointed to by SP.
206 // | argN | |
207 // | ... | |
208 // | arg4 | |
209 // | arg3 spill | | Caller's frame
210 // | arg2 spill | |
211 // | arg1 spill | |
212 // | Method* | ---
213 // | Return |
214 // | EBP,ESI,EDI | callee saves
215 // | EBX | arg3
216 // | EDX | arg2
217 // | ECX | arg1
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000218 // | XMM3 | float arg 4
219 // | XMM2 | float arg 3
220 // | XMM1 | float arg 2
221 // | XMM0 | float arg 1
Ian Rogers848871b2013-08-05 10:56:33 -0700222 // | EAX/Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500223 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000224 static constexpr bool kAlignPairRegister = false;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000225 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800226 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000227 static constexpr bool kQuickSkipOddFpRegisters = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800228 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs.
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000229 static constexpr size_t kNumQuickFprArgs = 4; // 4 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800230 static constexpr bool kGprFprLockstep = false;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000231 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 4; // Offset of first FPR arg.
232 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 4 + 4*8; // Offset of first GPR arg.
233 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 28 + 4*8; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800234 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000235 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800236 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800237#elif defined(__x86_64__)
Ian Rogers936b37f2014-02-14 00:52:24 -0800238 // The callee save frame is pointed to by SP.
239 // | argN | |
240 // | ... | |
241 // | reg. arg spills | | Caller's frame
242 // | Method* | ---
243 // | Return |
244 // | R15 | callee save
245 // | R14 | callee save
246 // | R13 | callee save
247 // | R12 | callee save
248 // | R9 | arg5
249 // | R8 | arg4
250 // | RSI/R6 | arg1
251 // | RBP/R5 | callee save
252 // | RBX/R3 | callee save
253 // | RDX/R2 | arg2
254 // | RCX/R1 | arg3
255 // | XMM7 | float arg 8
256 // | XMM6 | float arg 7
257 // | XMM5 | float arg 6
258 // | XMM4 | float arg 5
259 // | XMM3 | float arg 4
260 // | XMM2 | float arg 3
261 // | XMM1 | float arg 2
262 // | XMM0 | float arg 1
263 // | Padding |
264 // | RDI/Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500265 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000266 static constexpr bool kAlignPairRegister = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800267 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800268 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000269 static constexpr bool kQuickSkipOddFpRegisters = false;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700270 static constexpr size_t kNumQuickGprArgs = 5; // 5 arguments passed in GPRs.
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700271 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800272 static constexpr bool kGprFprLockstep = false;
Ian Rogers936b37f2014-02-14 00:52:24 -0800273 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 16; // Offset of first FPR arg.
Serguei Katkovc3801912014-07-08 17:21:53 +0700274 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 80 + 4*8; // Offset of first GPR arg.
275 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 168 + 4*8; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800276 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
277 switch (gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000278 case 0: return (4 * GetBytesPerGprSpillLocation(kRuntimeISA));
279 case 1: return (1 * GetBytesPerGprSpillLocation(kRuntimeISA));
280 case 2: return (0 * GetBytesPerGprSpillLocation(kRuntimeISA));
281 case 3: return (5 * GetBytesPerGprSpillLocation(kRuntimeISA));
282 case 4: return (6 * GetBytesPerGprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800283 default:
Andreas Gampec200a4a2014-06-16 18:39:09 -0700284 LOG(FATAL) << "Unexpected GPR index: " << gpr_index;
285 return 0;
Ian Rogers936b37f2014-02-14 00:52:24 -0800286 }
287 }
Ian Rogers848871b2013-08-05 10:56:33 -0700288#else
289#error "Unsupported architecture"
Ian Rogers848871b2013-08-05 10:56:33 -0700290#endif
291
Ian Rogers936b37f2014-02-14 00:52:24 -0800292 public:
Sebastien Hertza836bc92014-11-25 16:30:53 +0100293 // Special handling for proxy methods. Proxy methods are instance methods so the
294 // 'this' object is the 1st argument. They also have the same frame layout as the
295 // kRefAndArgs runtime method. Since 'this' is a reference, it is located in the
296 // 1st GPR.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700297 static mirror::Object* GetProxyThisObject(ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700298 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000299 CHECK((*sp)->IsProxyMethod());
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
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700308 static ArtMethod* GetCallingMethod(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700309 DCHECK((*sp)->IsCalleeSaveMethod());
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100310 return GetCalleeSaveMethodCaller(sp, Runtime::kSaveRefsAndArgs);
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +0100311 }
312
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700313 static ArtMethod* GetOuterMethod(ArtMethod** sp) REQUIRES_SHARED(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
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700320 static uint32_t GetCallingDexPc(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700321 DCHECK((*sp)->IsCalleeSaveMethod());
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100322 const size_t callee_frame_size = GetCalleeSaveFrameSize(kRuntimeISA, Runtime::kSaveRefsAndArgs);
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 Geoffray524e7ea2015-10-16 17:13:34 +0100326 const OatQuickMethodHeader* current_code = (*caller_sp)->GetOatQuickMethodHeader(outer_pc);
327 uintptr_t outer_pc_offset = current_code->NativeQuickPcOffset(outer_pc);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100328
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100329 if (current_code->IsOptimized()) {
330 CodeInfo code_info = current_code->GetOptimizedCodeInfo();
David Srbecky09ed0982016-02-12 21:58:43 +0000331 CodeInfoEncoding encoding = code_info.ExtractEncoding();
David Brazdilf677ebf2015-05-29 16:29:43 +0100332 StackMap stack_map = code_info.GetStackMapForNativePcOffset(outer_pc_offset, encoding);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100333 DCHECK(stack_map.IsValid());
David Srbecky09ed0982016-02-12 21:58:43 +0000334 if (stack_map.HasInlineInfo(encoding.stack_map_encoding)) {
David Brazdilf677ebf2015-05-29 16:29:43 +0100335 InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map, encoding);
David Srbecky61b28a12016-02-25 21:55:03 +0000336 return inline_info.GetDexPcAtDepth(encoding.inline_info_encoding,
337 inline_info.GetDepth(encoding.inline_info_encoding)-1);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100338 } else {
David Srbecky09ed0982016-02-12 21:58:43 +0000339 return stack_map.GetDexPc(encoding.stack_map_encoding);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100340 }
341 } else {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100342 return current_code->ToDexPc(*caller_sp, outer_pc);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100343 }
Ian Rogers848871b2013-08-05 10:56:33 -0700344 }
345
Ian Rogers936b37f2014-02-14 00:52:24 -0800346 // For the given quick ref and args quick frame, return the caller's PC.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700347 static uintptr_t GetCallingPc(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700348 DCHECK((*sp)->IsCalleeSaveMethod());
Ian Rogers13735952014-10-08 12:43:28 -0700349 uint8_t* lr = reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_LrOffset;
Ian Rogers848871b2013-08-05 10:56:33 -0700350 return *reinterpret_cast<uintptr_t*>(lr);
351 }
352
Mathieu Chartiere401d142015-04-22 13:56:20 -0700353 QuickArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700354 uint32_t shorty_len) REQUIRES_SHARED(Locks::mutator_lock_) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700355 is_static_(is_static), shorty_(shorty), shorty_len_(shorty_len),
Ian Rogers13735952014-10-08 12:43:28 -0700356 gpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset),
357 fpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset),
358 stack_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize
Mathieu Chartiere401d142015-04-22 13:56:20 -0700359 + sizeof(ArtMethod*)), // Skip ArtMethod*.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800360 gpr_index_(0), fpr_index_(0), fpr_double_index_(0), stack_index_(0),
361 cur_type_(Primitive::kPrimVoid), is_split_long_or_double_(false) {
Andreas Gampe575e78c2014-11-03 23:41:03 -0800362 static_assert(kQuickSoftFloatAbi == (kNumQuickFprArgs == 0),
363 "Number of Quick FPR arguments unexpected");
364 static_assert(!(kQuickSoftFloatAbi && kQuickDoubleRegAlignedFloatBackFilled),
365 "Double alignment unexpected");
Zheng Xu5667fdb2014-10-23 18:29:55 +0800366 // For register alignment, we want to assume that counters(fpr_double_index_) are even if the
367 // next register is even.
Andreas Gampe575e78c2014-11-03 23:41:03 -0800368 static_assert(!kQuickDoubleRegAlignedFloatBackFilled || kNumQuickFprArgs % 2 == 0,
369 "Number of Quick FPR arguments not even");
Andreas Gampe542451c2016-07-26 09:02:02 -0700370 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Zheng Xu5667fdb2014-10-23 18:29:55 +0800371 }
Ian Rogers848871b2013-08-05 10:56:33 -0700372
373 virtual ~QuickArgumentVisitor() {}
374
375 virtual void Visit() = 0;
376
Ian Rogers936b37f2014-02-14 00:52:24 -0800377 Primitive::Type GetParamPrimitiveType() const {
378 return cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700379 }
380
Ian Rogers13735952014-10-08 12:43:28 -0700381 uint8_t* GetParamAddress() const {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800382 if (!kQuickSoftFloatAbi) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800383 Primitive::Type type = GetParamPrimitiveType();
384 if (UNLIKELY((type == Primitive::kPrimDouble) || (type == Primitive::kPrimFloat))) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800385 if (type == Primitive::kPrimDouble && kQuickDoubleRegAlignedFloatBackFilled) {
386 if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) {
387 return fpr_args_ + (fpr_double_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
388 }
389 } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000390 return fpr_args_ + (fpr_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800391 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700392 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers936b37f2014-02-14 00:52:24 -0800393 }
394 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800395 if (gpr_index_ < kNumQuickGprArgs) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800396 return gpr_args_ + GprIndexToGprOffset(gpr_index_);
397 }
398 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700399 }
400
401 bool IsSplitLongOrDouble() const {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700402 if ((GetBytesPerGprSpillLocation(kRuntimeISA) == 4) ||
403 (GetBytesPerFprSpillLocation(kRuntimeISA) == 4)) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800404 return is_split_long_or_double_;
405 } else {
406 return false; // An optimization for when GPR and FPRs are 64bit.
407 }
Ian Rogers848871b2013-08-05 10:56:33 -0700408 }
409
Ian Rogers936b37f2014-02-14 00:52:24 -0800410 bool IsParamAReference() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700411 return GetParamPrimitiveType() == Primitive::kPrimNot;
412 }
413
Ian Rogers936b37f2014-02-14 00:52:24 -0800414 bool IsParamALongOrDouble() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700415 Primitive::Type type = GetParamPrimitiveType();
416 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
417 }
418
419 uint64_t ReadSplitLongParam() const {
Nicolas Geoffray425f2392015-01-08 14:52:29 +0000420 // The splitted long is always available through the stack.
421 return *reinterpret_cast<uint64_t*>(stack_args_
422 + stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700423 }
424
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800425 void IncGprIndex() {
426 gpr_index_++;
427 if (kGprFprLockstep) {
428 fpr_index_++;
429 }
430 }
431
432 void IncFprIndex() {
433 fpr_index_++;
434 if (kGprFprLockstep) {
435 gpr_index_++;
436 }
437 }
438
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700439 void VisitArguments() REQUIRES_SHARED(Locks::mutator_lock_) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800440 // (a) 'stack_args_' should point to the first method's argument
441 // (b) whatever the argument type it is, the 'stack_index_' should
442 // be moved forward along with every visiting.
Ian Rogers936b37f2014-02-14 00:52:24 -0800443 gpr_index_ = 0;
444 fpr_index_ = 0;
Zheng Xu5667fdb2014-10-23 18:29:55 +0800445 if (kQuickDoubleRegAlignedFloatBackFilled) {
446 fpr_double_index_ = 0;
447 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800448 stack_index_ = 0;
449 if (!is_static_) { // Handle this.
450 cur_type_ = Primitive::kPrimNot;
451 is_split_long_or_double_ = false;
Ian Rogers848871b2013-08-05 10:56:33 -0700452 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800453 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800454 if (kNumQuickGprArgs > 0) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800455 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800456 }
Ian Rogers848871b2013-08-05 10:56:33 -0700457 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800458 for (uint32_t shorty_index = 1; shorty_index < shorty_len_; ++shorty_index) {
459 cur_type_ = Primitive::GetType(shorty_[shorty_index]);
460 switch (cur_type_) {
461 case Primitive::kPrimNot:
462 case Primitive::kPrimBoolean:
463 case Primitive::kPrimByte:
464 case Primitive::kPrimChar:
465 case Primitive::kPrimShort:
466 case Primitive::kPrimInt:
467 is_split_long_or_double_ = false;
468 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800469 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800470 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800471 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800472 }
473 break;
474 case Primitive::kPrimFloat:
475 is_split_long_or_double_ = false;
476 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800477 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800478 if (kQuickSoftFloatAbi) {
479 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800480 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800481 }
482 } else {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800483 if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800484 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800485 if (kQuickDoubleRegAlignedFloatBackFilled) {
486 // Double should not overlap with float.
487 // For example, if fpr_index_ = 3, fpr_double_index_ should be at least 4.
488 fpr_double_index_ = std::max(fpr_double_index_, RoundUp(fpr_index_, 2));
489 // Float should not overlap with double.
490 if (fpr_index_ % 2 == 0) {
491 fpr_index_ = std::max(fpr_double_index_, fpr_index_);
492 }
Goran Jakovljevicff734982015-08-24 12:58:55 +0000493 } else if (kQuickSkipOddFpRegisters) {
494 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800495 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800496 }
497 }
498 break;
499 case Primitive::kPrimDouble:
500 case Primitive::kPrimLong:
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800501 if (kQuickSoftFloatAbi || (cur_type_ == Primitive::kPrimLong)) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000502 if (cur_type_ == Primitive::kPrimLong && kAlignPairRegister && gpr_index_ == 0) {
Goran Jakovljevicff734982015-08-24 12:58:55 +0000503 // Currently, this is only for ARM and MIPS, where the first available parameter
504 // register is R1 (on ARM) or A1 (on MIPS). So we skip it, and use R2 (on ARM) or
505 // A2 (on MIPS) instead.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800506 IncGprIndex();
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000507 }
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000508 is_split_long_or_double_ = (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) &&
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800509 ((gpr_index_ + 1) == kNumQuickGprArgs);
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500510 if (!kSplitPairAcrossRegisterAndStack && is_split_long_or_double_) {
511 // We don't want to split this. Pass over this register.
512 gpr_index_++;
513 is_split_long_or_double_ = false;
514 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800515 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800516 if (kBytesStackArgLocation == 4) {
517 stack_index_+= 2;
518 } else {
519 CHECK_EQ(kBytesStackArgLocation, 8U);
520 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800521 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700522 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800523 IncGprIndex();
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000524 if (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700525 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800526 IncGprIndex();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700527 }
528 }
529 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800530 } else {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000531 is_split_long_or_double_ = (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) &&
Zheng Xu5667fdb2014-10-23 18:29:55 +0800532 ((fpr_index_ + 1) == kNumQuickFprArgs) && !kQuickDoubleRegAlignedFloatBackFilled;
Ian Rogers936b37f2014-02-14 00:52:24 -0800533 Visit();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700534 if (kBytesStackArgLocation == 4) {
535 stack_index_+= 2;
Ian Rogers936b37f2014-02-14 00:52:24 -0800536 } else {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700537 CHECK_EQ(kBytesStackArgLocation, 8U);
538 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800539 }
Zheng Xu5667fdb2014-10-23 18:29:55 +0800540 if (kQuickDoubleRegAlignedFloatBackFilled) {
541 if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) {
542 fpr_double_index_ += 2;
543 // Float should not overlap with double.
544 if (fpr_index_ % 2 == 0) {
545 fpr_index_ = std::max(fpr_double_index_, fpr_index_);
546 }
547 }
548 } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800549 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800550 if (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) {
551 if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800552 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800553 }
554 }
555 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800556 }
557 break;
558 default:
559 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty_;
560 }
Ian Rogers848871b2013-08-05 10:56:33 -0700561 }
562 }
563
Andreas Gampec200a4a2014-06-16 18:39:09 -0700564 protected:
Ian Rogers848871b2013-08-05 10:56:33 -0700565 const bool is_static_;
566 const char* const shorty_;
567 const uint32_t shorty_len_;
Andreas Gampec200a4a2014-06-16 18:39:09 -0700568
569 private:
Ian Rogers13735952014-10-08 12:43:28 -0700570 uint8_t* const gpr_args_; // Address of GPR arguments in callee save frame.
571 uint8_t* const fpr_args_; // Address of FPR arguments in callee save frame.
572 uint8_t* const stack_args_; // Address of stack arguments in caller's frame.
Ian Rogers936b37f2014-02-14 00:52:24 -0800573 uint32_t gpr_index_; // Index into spilled GPRs.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800574 // Index into spilled FPRs.
575 // In case kQuickDoubleRegAlignedFloatBackFilled, it may index a hole while fpr_double_index_
576 // holds a higher register number.
577 uint32_t fpr_index_;
578 // Index into spilled FPRs for aligned double.
579 // Only used when kQuickDoubleRegAlignedFloatBackFilled. Next available double register indexed in
580 // terms of singles, may be behind fpr_index.
581 uint32_t fpr_double_index_;
Ian Rogers936b37f2014-02-14 00:52:24 -0800582 uint32_t stack_index_; // Index into arguments on the stack.
583 // The current type of argument during VisitArguments.
584 Primitive::Type cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700585 // Does a 64bit parameter straddle the register and stack arguments?
586 bool is_split_long_or_double_;
587};
588
Sebastien Hertza836bc92014-11-25 16:30:53 +0100589// Returns the 'this' object of a proxy method. This function is only used by StackVisitor. It
590// allows to use the QuickArgumentVisitor constants without moving all the code in its own module.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700591extern "C" mirror::Object* artQuickGetProxyThisObject(ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700592 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertza836bc92014-11-25 16:30:53 +0100593 return QuickArgumentVisitor::GetProxyThisObject(sp);
594}
595
Ian Rogers848871b2013-08-05 10:56:33 -0700596// Visits arguments on the stack placing them into the shadow frame.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800597class BuildQuickShadowFrameVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700598 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700599 BuildQuickShadowFrameVisitor(ArtMethod** sp, bool is_static, const char* shorty,
600 uint32_t shorty_len, ShadowFrame* sf, size_t first_arg_reg) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700601 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), sf_(sf), cur_reg_(first_arg_reg) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700602
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700603 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700604
605 private:
Ian Rogers936b37f2014-02-14 00:52:24 -0800606 ShadowFrame* const sf_;
607 uint32_t cur_reg_;
Ian Rogers848871b2013-08-05 10:56:33 -0700608
Dragos Sbirleabd136a22013-08-13 18:07:04 -0700609 DISALLOW_COPY_AND_ASSIGN(BuildQuickShadowFrameVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700610};
611
Andreas Gampec200a4a2014-06-16 18:39:09 -0700612void BuildQuickShadowFrameVisitor::Visit() {
Ian Rogers9758f792014-03-13 09:02:55 -0700613 Primitive::Type type = GetParamPrimitiveType();
614 switch (type) {
615 case Primitive::kPrimLong: // Fall-through.
616 case Primitive::kPrimDouble:
617 if (IsSplitLongOrDouble()) {
618 sf_->SetVRegLong(cur_reg_, ReadSplitLongParam());
619 } else {
620 sf_->SetVRegLong(cur_reg_, *reinterpret_cast<jlong*>(GetParamAddress()));
621 }
622 ++cur_reg_;
623 break;
624 case Primitive::kPrimNot: {
625 StackReference<mirror::Object>* stack_ref =
626 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
627 sf_->SetVRegReference(cur_reg_, stack_ref->AsMirrorPtr());
628 }
629 break;
630 case Primitive::kPrimBoolean: // Fall-through.
631 case Primitive::kPrimByte: // Fall-through.
632 case Primitive::kPrimChar: // Fall-through.
633 case Primitive::kPrimShort: // Fall-through.
634 case Primitive::kPrimInt: // Fall-through.
635 case Primitive::kPrimFloat:
636 sf_->SetVReg(cur_reg_, *reinterpret_cast<jint*>(GetParamAddress()));
637 break;
638 case Primitive::kPrimVoid:
639 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700640 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -0700641 }
642 ++cur_reg_;
643}
644
Mathieu Chartiere401d142015-04-22 13:56:20 -0700645extern "C" uint64_t artQuickToInterpreterBridge(ArtMethod* method, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700646 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers848871b2013-08-05 10:56:33 -0700647 // Ensure we don't get thread suspension until the object arguments are safely in the shadow
648 // frame.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700649 ScopedQuickEntrypointChecks sqec(self);
Ian Rogers848871b2013-08-05 10:56:33 -0700650
Alex Light9139e002015-10-09 15:59:48 -0700651 if (UNLIKELY(!method->IsInvokable())) {
652 method->ThrowInvocationTimeError();
Ian Rogers848871b2013-08-05 10:56:33 -0700653 return 0;
Andreas Gampe639bdd12015-06-03 11:22:45 -0700654 }
655
656 JValue tmp_value;
657 ShadowFrame* deopt_frame = self->PopStackedShadowFrame(
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700658 StackedShadowFrameType::kDeoptimizationShadowFrame, false);
Andreas Gampe639bdd12015-06-03 11:22:45 -0700659 ManagedStack fragment;
660
661 DCHECK(!method->IsNative()) << PrettyMethod(method);
662 uint32_t shorty_len = 0;
Andreas Gampe542451c2016-07-26 09:02:02 -0700663 ArtMethod* non_proxy_method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Alex Lighte9dd04f2016-03-16 16:09:45 -0700664 const DexFile::CodeItem* code_item = non_proxy_method->GetCodeItem();
665 DCHECK(code_item != nullptr) << PrettyMethod(method);
Andreas Gampe639bdd12015-06-03 11:22:45 -0700666 const char* shorty = non_proxy_method->GetShorty(&shorty_len);
667
668 JValue result;
669
670 if (deopt_frame != nullptr) {
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700671 // Coming from partial-fragment deopt.
Andreas Gampe639bdd12015-06-03 11:22:45 -0700672
673 if (kIsDebugBuild) {
674 // Sanity-check: are the methods as expected? We check that the last shadow frame (the bottom
675 // of the call-stack) corresponds to the called method.
676 ShadowFrame* linked = deopt_frame;
677 while (linked->GetLink() != nullptr) {
678 linked = linked->GetLink();
679 }
680 CHECK_EQ(method, linked->GetMethod()) << PrettyMethod(method) << " "
681 << PrettyMethod(linked->GetMethod());
682 }
683
684 if (VLOG_IS_ON(deopt)) {
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700685 // Print out the stack to verify that it was a partial-fragment deopt.
Andreas Gampe639bdd12015-06-03 11:22:45 -0700686 LOG(INFO) << "Continue-ing from deopt. Stack is:";
687 QuickExceptionHandler::DumpFramesWithType(self, true);
688 }
689
690 mirror::Throwable* pending_exception = nullptr;
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100691 bool from_code = false;
692 self->PopDeoptimizationContext(&result, &pending_exception, /* out */ &from_code);
Andreas Gampe639bdd12015-06-03 11:22:45 -0700693
694 // Push a transition back into managed code onto the linked list in thread.
695 self->PushManagedStackFragment(&fragment);
696
697 // Ensure that the stack is still in order.
698 if (kIsDebugBuild) {
699 class DummyStackVisitor : public StackVisitor {
700 public:
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700701 explicit DummyStackVisitor(Thread* self_in) REQUIRES_SHARED(Locks::mutator_lock_)
Andreas Gampe639bdd12015-06-03 11:22:45 -0700702 : StackVisitor(self_in, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {}
703
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700704 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe639bdd12015-06-03 11:22:45 -0700705 // Nothing to do here. In a debug build, SanityCheckFrame will do the work in the walking
706 // logic. Just always say we want to continue.
707 return true;
708 }
709 };
710 DummyStackVisitor dsv(self);
711 dsv.WalkStack();
712 }
713
714 // Restore the exception that was pending before deoptimization then interpret the
715 // deoptimized frames.
716 if (pending_exception != nullptr) {
717 self->SetException(pending_exception);
718 }
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100719 interpreter::EnterInterpreterFromDeoptimize(self, deopt_frame, from_code, &result);
Ian Rogers848871b2013-08-05 10:56:33 -0700720 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -0700721 const char* old_cause = self->StartAssertNoThreadSuspension(
722 "Building interpreter shadow frame");
Ian Rogers848871b2013-08-05 10:56:33 -0700723 uint16_t num_regs = code_item->registers_size_;
Andreas Gampec200a4a2014-06-16 18:39:09 -0700724 // No last shadow coming from quick.
Andreas Gampeb3025922015-09-01 14:45:00 -0700725 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
Andreas Gampe03ec9302015-08-27 17:41:47 -0700726 CREATE_SHADOW_FRAME(num_regs, /* link */ nullptr, method, /* dex pc */ 0);
Andreas Gampeb3025922015-09-01 14:45:00 -0700727 ShadowFrame* shadow_frame = shadow_frame_unique_ptr.get();
Ian Rogers848871b2013-08-05 10:56:33 -0700728 size_t first_arg_reg = code_item->registers_size_ - code_item->ins_size_;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700729 BuildQuickShadowFrameVisitor shadow_frame_builder(sp, method->IsStatic(), shorty, shorty_len,
Ian Rogers936b37f2014-02-14 00:52:24 -0800730 shadow_frame, first_arg_reg);
Ian Rogers848871b2013-08-05 10:56:33 -0700731 shadow_frame_builder.VisitArguments();
Ian Rogerse94652f2014-12-02 11:13:19 -0800732 const bool needs_initialization =
733 method->IsStatic() && !method->GetDeclaringClass()->IsInitialized();
Ian Rogers848871b2013-08-05 10:56:33 -0700734 // Push a transition back into managed code onto the linked list in thread.
Ian Rogers848871b2013-08-05 10:56:33 -0700735 self->PushManagedStackFragment(&fragment);
736 self->PushShadowFrame(shadow_frame);
737 self->EndAssertNoThreadSuspension(old_cause);
738
Ian Rogerse94652f2014-12-02 11:13:19 -0800739 if (needs_initialization) {
Ian Rogers848871b2013-08-05 10:56:33 -0700740 // Ensure static method's class is initialized.
Ian Rogerse94652f2014-12-02 11:13:19 -0800741 StackHandleScope<1> hs(self);
742 Handle<mirror::Class> h_class(hs.NewHandle(shadow_frame->GetMethod()->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700743 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
Ian Rogerse94652f2014-12-02 11:13:19 -0800744 DCHECK(Thread::Current()->IsExceptionPending()) << PrettyMethod(shadow_frame->GetMethod());
Ian Rogers848871b2013-08-05 10:56:33 -0700745 self->PopManagedStackFragment(fragment);
746 return 0;
747 }
748 }
Daniel Mihalyieb076692014-08-22 17:33:31 +0200749
Andreas Gampe639bdd12015-06-03 11:22:45 -0700750 result = interpreter::EnterInterpreterFromEntryPoint(self, code_item, shadow_frame);
Ian Rogers848871b2013-08-05 10:56:33 -0700751 }
Andreas Gampe639bdd12015-06-03 11:22:45 -0700752
753 // Pop transition.
754 self->PopManagedStackFragment(fragment);
755
756 // Request a stack deoptimization if needed
757 ArtMethod* caller = QuickArgumentVisitor::GetCallingMethod(sp);
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700758 uintptr_t caller_pc = QuickArgumentVisitor::GetCallingPc(sp);
Mingyao Yanga3549d22016-06-02 17:01:02 -0700759 // If caller_pc is the instrumentation exit stub, the stub will check to see if deoptimization
760 // should be done and it knows the real return pc.
761 if (UNLIKELY(caller_pc != reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) &&
762 Dbg::IsForcedInterpreterNeededForUpcall(self, caller) &&
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700763 Runtime::Current()->IsDeoptimizeable(caller_pc))) {
Andreas Gampe639bdd12015-06-03 11:22:45 -0700764 // Push the context of the deoptimization stack so we can restore the return value and the
765 // exception before executing the deoptimized frames.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100766 self->PushDeoptimizationContext(
767 result, shorty[0] == 'L', /* from_code */ false, self->GetException());
Andreas Gampe639bdd12015-06-03 11:22:45 -0700768
769 // Set special exception to cause deoptimization.
770 self->SetException(Thread::GetDeoptimizationException());
771 }
772
773 // No need to restore the args since the method has already been run by the interpreter.
774 return result.GetJ();
Ian Rogers848871b2013-08-05 10:56:33 -0700775}
776
777// Visits arguments on the stack placing them into the args vector, Object* arguments are converted
778// to jobjects.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800779class BuildQuickArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700780 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700781 BuildQuickArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty, uint32_t shorty_len,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700782 ScopedObjectAccessUnchecked* soa, std::vector<jvalue>* args) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700783 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa), args_(args) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700784
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700785 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700786
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700787 void FixupReferences() REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800788
Ian Rogers848871b2013-08-05 10:56:33 -0700789 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700790 ScopedObjectAccessUnchecked* const soa_;
791 std::vector<jvalue>* const args_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800792 // References which we must update when exiting in case the GC moved the objects.
Ian Rogers700a4022014-05-19 16:49:03 -0700793 std::vector<std::pair<jobject, StackReference<mirror::Object>*>> references_;
Ian Rogers9758f792014-03-13 09:02:55 -0700794
Ian Rogers848871b2013-08-05 10:56:33 -0700795 DISALLOW_COPY_AND_ASSIGN(BuildQuickArgumentVisitor);
796};
797
Ian Rogers9758f792014-03-13 09:02:55 -0700798void BuildQuickArgumentVisitor::Visit() {
799 jvalue val;
800 Primitive::Type type = GetParamPrimitiveType();
801 switch (type) {
802 case Primitive::kPrimNot: {
803 StackReference<mirror::Object>* stack_ref =
804 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
805 val.l = soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
806 references_.push_back(std::make_pair(val.l, stack_ref));
807 break;
808 }
809 case Primitive::kPrimLong: // Fall-through.
810 case Primitive::kPrimDouble:
811 if (IsSplitLongOrDouble()) {
812 val.j = ReadSplitLongParam();
813 } else {
814 val.j = *reinterpret_cast<jlong*>(GetParamAddress());
815 }
816 break;
817 case Primitive::kPrimBoolean: // Fall-through.
818 case Primitive::kPrimByte: // Fall-through.
819 case Primitive::kPrimChar: // Fall-through.
820 case Primitive::kPrimShort: // Fall-through.
821 case Primitive::kPrimInt: // Fall-through.
822 case Primitive::kPrimFloat:
823 val.i = *reinterpret_cast<jint*>(GetParamAddress());
824 break;
825 case Primitive::kPrimVoid:
826 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700827 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -0700828 }
829 args_->push_back(val);
830}
831
832void BuildQuickArgumentVisitor::FixupReferences() {
833 // Fixup any references which may have changed.
834 for (const auto& pair : references_) {
835 pair.second->Assign(soa_->Decode<mirror::Object*>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -0700836 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -0700837 }
838}
839
Ian Rogers848871b2013-08-05 10:56:33 -0700840// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
841// which is responsible for recording callee save registers. We explicitly place into jobjects the
842// incoming reference arguments (so they survive GC). We invoke the invocation handler, which is a
843// field within the proxy object, which will box the primitive arguments and deal with error cases.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700844extern "C" uint64_t artQuickProxyInvokeHandler(
845 ArtMethod* proxy_method, mirror::Object* receiver, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700846 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000847 DCHECK(proxy_method->IsProxyMethod()) << PrettyMethod(proxy_method);
848 DCHECK(receiver->GetClass()->IsProxyClass()) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700849 // Ensure we don't get thread suspension until the object arguments are safely in jobjects.
850 const char* old_cause =
851 self->StartAssertNoThreadSuspension("Adding to IRT proxy object arguments");
852 // Register the top of the managed stack, making stack crawlable.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700853 DCHECK_EQ((*sp), proxy_method) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700854 self->VerifyStack();
855 // Start new JNI local reference state.
856 JNIEnvExt* env = self->GetJniEnv();
857 ScopedObjectAccessUnchecked soa(env);
858 ScopedJniEnvLocalRefState env_state(env);
859 // Create local ref. copies of proxy method and the receiver.
860 jobject rcvr_jobj = soa.AddLocalReference<jobject>(receiver);
861
862 // Placing arguments into args vector and remove the receiver.
Andreas Gampe542451c2016-07-26 09:02:02 -0700863 ArtMethod* non_proxy_method = proxy_method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700864 CHECK(!non_proxy_method->IsStatic()) << PrettyMethod(proxy_method) << " "
Andreas Gampec200a4a2014-06-16 18:39:09 -0700865 << PrettyMethod(non_proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700866 std::vector<jvalue> args;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700867 uint32_t shorty_len = 0;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700868 const char* shorty = non_proxy_method->GetShorty(&shorty_len);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700869 BuildQuickArgumentVisitor local_ref_visitor(sp, false, shorty, shorty_len, &soa, &args);
Brian Carlstromd3633d52013-08-20 21:06:26 -0700870
Ian Rogers848871b2013-08-05 10:56:33 -0700871 local_ref_visitor.VisitArguments();
Brian Carlstromd3633d52013-08-20 21:06:26 -0700872 DCHECK_GT(args.size(), 0U) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700873 args.erase(args.begin());
874
875 // Convert proxy method into expected interface method.
Andreas Gampe542451c2016-07-26 09:02:02 -0700876 ArtMethod* interface_method = proxy_method->FindOverriddenMethod(kRuntimePointerSize);
Ian Rogerse0a02da2014-12-02 14:10:53 -0800877 DCHECK(interface_method != nullptr) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700878 DCHECK(!interface_method->IsProxyMethod()) << PrettyMethod(interface_method);
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700879 self->EndAssertNoThreadSuspension(old_cause);
Andreas Gampe542451c2016-07-26 09:02:02 -0700880 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Andreas Gampee01e3642016-07-25 13:06:04 -0700881 DCHECK(!Runtime::Current()->IsActiveTransaction());
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700882 jobject interface_method_jobj = soa.AddLocalReference<jobject>(
Andreas Gampe542451c2016-07-26 09:02:02 -0700883 mirror::Method::CreateFromArtMethod<kRuntimePointerSize, false>(soa.Self(),
884 interface_method));
Ian Rogers848871b2013-08-05 10:56:33 -0700885
886 // All naked Object*s should now be in jobjects, so its safe to go into the main invoke code
887 // that performs allocations.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700888 JValue result = InvokeProxyInvocationHandler(soa, shorty, rcvr_jobj, interface_method_jobj, args);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800889 // Restore references which might have moved.
890 local_ref_visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -0700891 return result.GetJ();
892}
893
894// Read object references held in arguments from quick frames and place in a JNI local references,
895// so they don't get garbage collected.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800896class RememberForGcArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700897 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700898 RememberForGcArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty,
899 uint32_t shorty_len, ScopedObjectAccessUnchecked* soa) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700900 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700901
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700902 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Mathieu Chartier07d447b2013-09-26 11:57:43 -0700903
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700904 void FixupReferences() REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers848871b2013-08-05 10:56:33 -0700905
906 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700907 ScopedObjectAccessUnchecked* const soa_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800908 // References which we must update when exiting in case the GC moved the objects.
Andreas Gampec200a4a2014-06-16 18:39:09 -0700909 std::vector<std::pair<jobject, StackReference<mirror::Object>*> > references_;
910
Mathieu Chartier590fee92013-09-13 13:46:47 -0700911 DISALLOW_COPY_AND_ASSIGN(RememberForGcArgumentVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700912};
913
Ian Rogers9758f792014-03-13 09:02:55 -0700914void RememberForGcArgumentVisitor::Visit() {
915 if (IsParamAReference()) {
916 StackReference<mirror::Object>* stack_ref =
917 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
918 jobject reference =
919 soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
920 references_.push_back(std::make_pair(reference, stack_ref));
921 }
922}
923
924void RememberForGcArgumentVisitor::FixupReferences() {
925 // Fixup any references which may have changed.
926 for (const auto& pair : references_) {
927 pair.second->Assign(soa_->Decode<mirror::Object*>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -0700928 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -0700929 }
930}
931
Ian Rogers848871b2013-08-05 10:56:33 -0700932// Lazily resolve a method for quick. Called by stub code.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700933extern "C" const void* artQuickResolutionTrampoline(
934 ArtMethod* called, mirror::Object* receiver, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700935 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe3b45ef22015-05-26 21:34:09 -0700936 // The resolution trampoline stashes the resolved method into the callee-save frame to transport
937 // it. Thus, when exiting, the stack cannot be verified (as the resolved method most likely
938 // does not have the same stack layout as the callee-save method).
939 ScopedQuickEntrypointChecks sqec(self, kIsDebugBuild, false);
Ian Rogers848871b2013-08-05 10:56:33 -0700940 // Start new JNI local reference state
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800941 JNIEnvExt* env = self->GetJniEnv();
Ian Rogers848871b2013-08-05 10:56:33 -0700942 ScopedObjectAccessUnchecked soa(env);
943 ScopedJniEnvLocalRefState env_state(env);
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800944 const char* old_cause = self->StartAssertNoThreadSuspension("Quick method resolution set up");
Ian Rogers848871b2013-08-05 10:56:33 -0700945
946 // Compute details about the called method (avoid GCs)
947 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Ian Rogers848871b2013-08-05 10:56:33 -0700948 InvokeType invoke_type;
Ian Rogerse0a02da2014-12-02 14:10:53 -0800949 MethodReference called_method(nullptr, 0);
950 const bool called_method_known_on_entry = !called->IsRuntimeMethod();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700951 ArtMethod* caller = nullptr;
Ian Rogerse0a02da2014-12-02 14:10:53 -0800952 if (!called_method_known_on_entry) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +0100953 caller = QuickArgumentVisitor::GetCallingMethod(sp);
954 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
Ian Rogers848871b2013-08-05 10:56:33 -0700955 const DexFile::CodeItem* code;
Ian Rogerse0a02da2014-12-02 14:10:53 -0800956 called_method.dex_file = caller->GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700957 code = caller->GetCodeItem();
Ian Rogers848871b2013-08-05 10:56:33 -0700958 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
959 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
960 Instruction::Code instr_code = instr->Opcode();
961 bool is_range;
962 switch (instr_code) {
963 case Instruction::INVOKE_DIRECT:
964 invoke_type = kDirect;
965 is_range = false;
966 break;
967 case Instruction::INVOKE_DIRECT_RANGE:
968 invoke_type = kDirect;
969 is_range = true;
970 break;
971 case Instruction::INVOKE_STATIC:
972 invoke_type = kStatic;
973 is_range = false;
974 break;
975 case Instruction::INVOKE_STATIC_RANGE:
976 invoke_type = kStatic;
977 is_range = true;
978 break;
979 case Instruction::INVOKE_SUPER:
980 invoke_type = kSuper;
981 is_range = false;
982 break;
983 case Instruction::INVOKE_SUPER_RANGE:
984 invoke_type = kSuper;
985 is_range = true;
986 break;
987 case Instruction::INVOKE_VIRTUAL:
988 invoke_type = kVirtual;
989 is_range = false;
990 break;
991 case Instruction::INVOKE_VIRTUAL_RANGE:
992 invoke_type = kVirtual;
993 is_range = true;
994 break;
995 case Instruction::INVOKE_INTERFACE:
996 invoke_type = kInterface;
997 is_range = false;
998 break;
999 case Instruction::INVOKE_INTERFACE_RANGE:
1000 invoke_type = kInterface;
1001 is_range = true;
1002 break;
1003 default:
Ian Rogerse0a02da2014-12-02 14:10:53 -08001004 LOG(FATAL) << "Unexpected call into trampoline: " << instr->DumpString(nullptr);
1005 UNREACHABLE();
Ian Rogers848871b2013-08-05 10:56:33 -07001006 }
Ian Rogerse0a02da2014-12-02 14:10:53 -08001007 called_method.dex_method_index = (is_range) ? instr->VRegB_3rc() : instr->VRegB_35c();
Ian Rogers848871b2013-08-05 10:56:33 -07001008 } else {
1009 invoke_type = kStatic;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001010 called_method.dex_file = called->GetDexFile();
1011 called_method.dex_method_index = called->GetDexMethodIndex();
Ian Rogers848871b2013-08-05 10:56:33 -07001012 }
1013 uint32_t shorty_len;
1014 const char* shorty =
Ian Rogerse0a02da2014-12-02 14:10:53 -08001015 called_method.dex_file->GetMethodShorty(
1016 called_method.dex_file->GetMethodId(called_method.dex_method_index), &shorty_len);
Mathieu Chartier590fee92013-09-13 13:46:47 -07001017 RememberForGcArgumentVisitor visitor(sp, invoke_type == kStatic, shorty, shorty_len, &soa);
Ian Rogers848871b2013-08-05 10:56:33 -07001018 visitor.VisitArguments();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001019 self->EndAssertNoThreadSuspension(old_cause);
Ian Rogerse0a02da2014-12-02 14:10:53 -08001020 const bool virtual_or_interface = invoke_type == kVirtual || invoke_type == kInterface;
Ian Rogers848871b2013-08-05 10:56:33 -07001021 // Resolve method filling in dex cache.
Ian Rogerse0a02da2014-12-02 14:10:53 -08001022 if (!called_method_known_on_entry) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001023 StackHandleScope<1> hs(self);
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001024 mirror::Object* dummy = nullptr;
1025 HandleWrapper<mirror::Object> h_receiver(
1026 hs.NewHandleWrapper(virtual_or_interface ? &receiver : &dummy));
Ian Rogerse0a02da2014-12-02 14:10:53 -08001027 DCHECK_EQ(caller->GetDexFile(), called_method.dex_file);
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08001028 called = linker->ResolveMethod<ClassLinker::kForceICCECheck>(
1029 self, called_method.dex_method_index, caller, invoke_type);
Ian Rogers848871b2013-08-05 10:56:33 -07001030 }
Ian Rogerse0a02da2014-12-02 14:10:53 -08001031 const void* code = nullptr;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001032 if (LIKELY(!self->IsExceptionPending())) {
Ian Rogers848871b2013-08-05 10:56:33 -07001033 // Incompatible class change should have been handled in resolve method.
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001034 CHECK(!called->CheckIncompatibleClassChange(invoke_type))
1035 << PrettyMethod(called) << " " << invoke_type;
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001036 if (virtual_or_interface || invoke_type == kSuper) {
1037 // Refine called method based on receiver for kVirtual/kInterface, and
1038 // caller for kSuper.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001039 ArtMethod* orig_called = called;
Mathieu Chartier55871bf2014-02-27 10:24:50 -08001040 if (invoke_type == kVirtual) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001041 CHECK(receiver != nullptr) << invoke_type;
Andreas Gampe542451c2016-07-26 09:02:02 -07001042 called = receiver->GetClass()->FindVirtualMethodForVirtual(called, kRuntimePointerSize);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001043 } else if (invoke_type == kInterface) {
1044 CHECK(receiver != nullptr) << invoke_type;
Andreas Gampe542451c2016-07-26 09:02:02 -07001045 called = receiver->GetClass()->FindVirtualMethodForInterface(called, kRuntimePointerSize);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001046 } else {
1047 DCHECK_EQ(invoke_type, kSuper);
1048 CHECK(caller != nullptr) << invoke_type;
Nicolas Geoffray393fdb82016-04-25 14:58:06 +01001049 StackHandleScope<2> hs(self);
1050 Handle<mirror::DexCache> dex_cache(
1051 hs.NewHandle(caller->GetDeclaringClass()->GetDexCache()));
1052 Handle<mirror::ClassLoader> class_loader(
1053 hs.NewHandle(caller->GetDeclaringClass()->GetClassLoader()));
Alex Lightfedd91d2016-01-07 14:49:16 -08001054 // TODO Maybe put this into a mirror::Class function.
1055 mirror::Class* ref_class = linker->ResolveReferencedClassOfMethod(
Nicolas Geoffray393fdb82016-04-25 14:58:06 +01001056 called_method.dex_method_index, dex_cache, class_loader);
Alex Lightfedd91d2016-01-07 14:49:16 -08001057 if (ref_class->IsInterface()) {
Andreas Gampe542451c2016-07-26 09:02:02 -07001058 called = ref_class->FindVirtualMethodForInterfaceSuper(called, kRuntimePointerSize);
Alex Lightfedd91d2016-01-07 14:49:16 -08001059 } else {
1060 called = caller->GetDeclaringClass()->GetSuperClass()->GetVTableEntry(
Andreas Gampe542451c2016-07-26 09:02:02 -07001061 called->GetMethodIndex(), kRuntimePointerSize);
Alex Lightfedd91d2016-01-07 14:49:16 -08001062 }
Mathieu Chartier55871bf2014-02-27 10:24:50 -08001063 }
Mingyao Yangf4867782014-05-05 11:55:02 -07001064
1065 CHECK(called != nullptr) << PrettyMethod(orig_called) << " "
1066 << PrettyTypeOf(receiver) << " "
1067 << invoke_type << " " << orig_called->GetVtableIndex();
1068
Ian Rogers83883d72013-10-21 21:07:24 -07001069 // 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 -08001070 // of the sharpened method avoiding dirtying the dex cache if possible.
Ian Rogers00f15272014-12-02 16:55:46 -08001071 // Note, called_method.dex_method_index references the dex method before the
1072 // FindVirtualMethodFor... This is ok for FindDexMethodIndexInOtherDexFile that only cares
1073 // about the name and signature.
1074 uint32_t update_dex_cache_method_index = called->GetDexMethodIndex();
Andreas Gampe542451c2016-07-26 09:02:02 -07001075 if (!called->HasSameDexCacheResolvedMethods(caller, kRuntimePointerSize)) {
Ian Rogers83883d72013-10-21 21:07:24 -07001076 // Calling from one dex file to another, need to compute the method index appropriate to
Vladimir Markobbcc0c02014-02-03 14:08:42 +00001077 // the caller's dex file. Since we get here only if the original called was a runtime
1078 // method, we've got the correct dex_file and a dex_method_idx from above.
Ian Rogerse0a02da2014-12-02 14:10:53 -08001079 DCHECK(!called_method_known_on_entry);
1080 DCHECK_EQ(caller->GetDexFile(), called_method.dex_file);
1081 const DexFile* caller_dex_file = called_method.dex_file;
1082 uint32_t caller_method_name_and_sig_index = called_method.dex_method_index;
1083 update_dex_cache_method_index =
1084 called->FindDexMethodIndexInOtherDexFile(*caller_dex_file,
1085 caller_method_name_and_sig_index);
1086 }
1087 if ((update_dex_cache_method_index != DexFile::kDexNoIndex) &&
Mathieu Chartiere401d142015-04-22 13:56:20 -07001088 (caller->GetDexCacheResolvedMethod(
Andreas Gampe542451c2016-07-26 09:02:02 -07001089 update_dex_cache_method_index, kRuntimePointerSize) != called)) {
1090 caller->SetDexCacheResolvedMethod(update_dex_cache_method_index,
1091 called,
1092 kRuntimePointerSize);
Ian Rogers83883d72013-10-21 21:07:24 -07001093 }
Mathieu Chartiere4a91bb2015-01-28 13:11:44 -08001094 } else if (invoke_type == kStatic) {
1095 const auto called_dex_method_idx = called->GetDexMethodIndex();
1096 // For static invokes, we may dispatch to the static method in the superclass but resolve
1097 // using the subclass. To prevent getting slow paths on each invoke, we force set the
1098 // resolved method for the super class dex method index if we are in the same dex file.
1099 // b/19175856
1100 if (called->GetDexFile() == called_method.dex_file &&
1101 called_method.dex_method_index != called_dex_method_idx) {
Andreas Gampe542451c2016-07-26 09:02:02 -07001102 called->GetDexCache()->SetResolvedMethod(called_dex_method_idx,
1103 called,
1104 kRuntimePointerSize);
Mathieu Chartiere4a91bb2015-01-28 13:11:44 -08001105 }
Ian Rogers83883d72013-10-21 21:07:24 -07001106 }
Daniel Mihalyieb076692014-08-22 17:33:31 +02001107
Ian Rogers848871b2013-08-05 10:56:33 -07001108 // Ensure that the called method's class is initialized.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001109 StackHandleScope<1> hs(soa.Self());
1110 Handle<mirror::Class> called_class(hs.NewHandle(called->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -07001111 linker->EnsureInitialized(soa.Self(), called_class, true, true);
Ian Rogers848871b2013-08-05 10:56:33 -07001112 if (LIKELY(called_class->IsInitialized())) {
Daniel Mihalyieb076692014-08-22 17:33:31 +02001113 if (UNLIKELY(Dbg::IsForcedInterpreterNeededForResolution(self, called))) {
1114 // If we are single-stepping or the called method is deoptimized (by a
1115 // breakpoint, for example), then we have to execute the called method
1116 // with the interpreter.
1117 code = GetQuickToInterpreterBridge();
1118 } else if (UNLIKELY(Dbg::IsForcedInstrumentationNeededForResolution(self, caller))) {
1119 // If the caller is deoptimized (by a breakpoint, for example), we have to
1120 // continue its execution with interpreter when returning from the called
1121 // method. Because we do not want to execute the called method with the
1122 // interpreter, we wrap its execution into the instrumentation stubs.
1123 // When the called method returns, it will execute the instrumentation
1124 // exit hook that will determine the need of the interpreter with a call
1125 // to Dbg::IsForcedInterpreterNeededForUpcall and deoptimize the stack if
1126 // it is needed.
1127 code = GetQuickInstrumentationEntryPoint();
1128 } else {
1129 code = called->GetEntryPointFromQuickCompiledCode();
1130 }
Ian Rogers848871b2013-08-05 10:56:33 -07001131 } else if (called_class->IsInitializing()) {
Daniel Mihalyieb076692014-08-22 17:33:31 +02001132 if (UNLIKELY(Dbg::IsForcedInterpreterNeededForResolution(self, called))) {
1133 // If we are single-stepping or the called method is deoptimized (by a
1134 // breakpoint, for example), then we have to execute the called method
1135 // with the interpreter.
1136 code = GetQuickToInterpreterBridge();
1137 } else if (invoke_type == kStatic) {
Ian Rogers848871b2013-08-05 10:56:33 -07001138 // Class is still initializing, go to oat and grab code (trampoline must be left in place
1139 // until class is initialized to stop races between threads).
Ian Rogersef7d42f2014-01-06 12:55:46 -08001140 code = linker->GetQuickOatCodeFor(called);
Ian Rogers848871b2013-08-05 10:56:33 -07001141 } else {
1142 // No trampoline for non-static methods.
Ian Rogersef7d42f2014-01-06 12:55:46 -08001143 code = called->GetEntryPointFromQuickCompiledCode();
Ian Rogers848871b2013-08-05 10:56:33 -07001144 }
1145 } else {
1146 DCHECK(called_class->IsErroneous());
1147 }
1148 }
Ian Rogerse0a02da2014-12-02 14:10:53 -08001149 CHECK_EQ(code == nullptr, self->IsExceptionPending());
Mathieu Chartier07d447b2013-09-26 11:57:43 -07001150 // Fixup any locally saved objects may have moved during a GC.
1151 visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -07001152 // Place called method in callee-save frame to be placed as first argument to quick method.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001153 *sp = called;
1154
Ian Rogers848871b2013-08-05 10:56:33 -07001155 return code;
1156}
1157
Andreas Gampec147b002014-03-06 18:11:06 -08001158/*
1159 * This class uses a couple of observations to unite the different calling conventions through
1160 * a few constants.
1161 *
1162 * 1) Number of registers used for passing is normally even, so counting down has no penalty for
1163 * possible alignment.
1164 * 2) Known 64b architectures store 8B units on the stack, both for integral and floating point
1165 * types, so using uintptr_t is OK. Also means that we can use kRegistersNeededX to denote
1166 * when we have to split things
1167 * 3) The only soft-float, Arm, is 32b, so no widening needs to be taken into account for floats
1168 * and we can use Int handling directly.
1169 * 4) Only 64b architectures widen, and their stack is aligned 8B anyways, so no padding code
1170 * necessary when widening. Also, widening of Ints will take place implicitly, and the
1171 * extension should be compatible with Aarch64, which mandates copying the available bits
1172 * into LSB and leaving the rest unspecified.
1173 * 5) Aligning longs and doubles is necessary on arm only, and it's the same in registers and on
1174 * the stack.
1175 * 6) There is only little endian.
1176 *
1177 *
1178 * Actual work is supposed to be done in a delegate of the template type. The interface is as
1179 * follows:
1180 *
1181 * void PushGpr(uintptr_t): Add a value for the next GPR
1182 *
1183 * void PushFpr4(float): Add a value for the next FPR of size 32b. Is only called if we need
1184 * padding, that is, think the architecture is 32b and aligns 64b.
1185 *
1186 * void PushFpr8(uint64_t): Push a double. We _will_ call this on 32b, it's the callee's job to
1187 * split this if necessary. The current state will have aligned, if
1188 * necessary.
1189 *
1190 * void PushStack(uintptr_t): Push a value to the stack.
1191 *
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001192 * uintptr_t PushHandleScope(mirror::Object* ref): Add a reference to the HandleScope. This _will_ have nullptr,
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001193 * as this might be important for null initialization.
Andreas Gampec147b002014-03-06 18:11:06 -08001194 * Must return the jobject, that is, the reference to the
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001195 * entry in the HandleScope (nullptr if necessary).
Andreas Gampec147b002014-03-06 18:11:06 -08001196 *
1197 */
Andreas Gampec200a4a2014-06-16 18:39:09 -07001198template<class T> class BuildNativeCallFrameStateMachine {
Andreas Gampec147b002014-03-06 18:11:06 -08001199 public:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001200#if defined(__arm__)
1201 // TODO: These are all dummy values!
Andreas Gampec147b002014-03-06 18:11:06 -08001202 static constexpr bool kNativeSoftFloatAbi = true;
1203 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs, r0-r3
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001204 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
1205
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001206 static constexpr size_t kRegistersNeededForLong = 2;
1207 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -08001208 static constexpr bool kMultiRegistersAligned = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001209 static constexpr bool kMultiFPRegistersWidened = false;
1210 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001211 static constexpr bool kAlignLongOnStack = true;
1212 static constexpr bool kAlignDoubleOnStack = true;
Stuart Monteithb95a5342014-03-12 13:32:32 +00001213#elif defined(__aarch64__)
1214 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
1215 static constexpr size_t kNumNativeGprArgs = 8; // 6 arguments passed in GPRs.
1216 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
1217
1218 static constexpr size_t kRegistersNeededForLong = 1;
1219 static constexpr size_t kRegistersNeededForDouble = 1;
1220 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001221 static constexpr bool kMultiFPRegistersWidened = false;
1222 static constexpr bool kMultiGPRegistersWidened = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +00001223 static constexpr bool kAlignLongOnStack = false;
1224 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001225#elif defined(__mips__) && !defined(__LP64__)
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001226 static constexpr bool kNativeSoftFloatAbi = true; // This is a hard float ABI.
Douglas Leung735b8552014-10-31 12:21:40 -07001227 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs.
1228 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001229
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001230 static constexpr size_t kRegistersNeededForLong = 2;
1231 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -08001232 static constexpr bool kMultiRegistersAligned = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001233 static constexpr bool kMultiFPRegistersWidened = true;
1234 static constexpr bool kMultiGPRegistersWidened = false;
Douglas Leung735b8552014-10-31 12:21:40 -07001235 static constexpr bool kAlignLongOnStack = true;
1236 static constexpr bool kAlignDoubleOnStack = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001237#elif defined(__mips__) && defined(__LP64__)
1238 // Let the code prepare GPRs only and we will load the FPRs with same data.
1239 static constexpr bool kNativeSoftFloatAbi = true;
1240 static constexpr size_t kNumNativeGprArgs = 8;
1241 static constexpr size_t kNumNativeFprArgs = 0;
1242
1243 static constexpr size_t kRegistersNeededForLong = 1;
1244 static constexpr size_t kRegistersNeededForDouble = 1;
1245 static constexpr bool kMultiRegistersAligned = false;
1246 static constexpr bool kMultiFPRegistersWidened = false;
1247 static constexpr bool kMultiGPRegistersWidened = true;
1248 static constexpr bool kAlignLongOnStack = false;
1249 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001250#elif defined(__i386__)
1251 // TODO: Check these!
Andreas Gampec147b002014-03-06 18:11:06 -08001252 static constexpr bool kNativeSoftFloatAbi = false; // Not using int registers for fp
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001253 static constexpr size_t kNumNativeGprArgs = 0; // 6 arguments passed in GPRs.
1254 static constexpr size_t kNumNativeFprArgs = 0; // 8 arguments passed in FPRs.
1255
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001256 static constexpr size_t kRegistersNeededForLong = 2;
1257 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001258 static constexpr bool kMultiRegistersAligned = false; // x86 not using regs, anyways
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001259 static constexpr bool kMultiFPRegistersWidened = false;
1260 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001261 static constexpr bool kAlignLongOnStack = false;
1262 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001263#elif defined(__x86_64__)
1264 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
1265 static constexpr size_t kNumNativeGprArgs = 6; // 6 arguments passed in GPRs.
1266 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
1267
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001268 static constexpr size_t kRegistersNeededForLong = 1;
1269 static constexpr size_t kRegistersNeededForDouble = 1;
Andreas Gampec147b002014-03-06 18:11:06 -08001270 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001271 static constexpr bool kMultiFPRegistersWidened = false;
1272 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001273 static constexpr bool kAlignLongOnStack = false;
1274 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001275#else
1276#error "Unsupported architecture"
1277#endif
1278
Andreas Gampec147b002014-03-06 18:11:06 -08001279 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001280 explicit BuildNativeCallFrameStateMachine(T* delegate)
1281 : gpr_index_(kNumNativeGprArgs),
1282 fpr_index_(kNumNativeFprArgs),
1283 stack_entries_(0),
1284 delegate_(delegate) {
Andreas Gampec147b002014-03-06 18:11:06 -08001285 // For register alignment, we want to assume that counters (gpr_index_, fpr_index_) are even iff
1286 // the next register is even; counting down is just to make the compiler happy...
Andreas Gampe575e78c2014-11-03 23:41:03 -08001287 static_assert(kNumNativeGprArgs % 2 == 0U, "Number of native GPR arguments not even");
1288 static_assert(kNumNativeFprArgs % 2 == 0U, "Number of native FPR arguments not even");
Andreas Gampec147b002014-03-06 18:11:06 -08001289 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001290
Andreas Gampec200a4a2014-06-16 18:39:09 -07001291 virtual ~BuildNativeCallFrameStateMachine() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001292
Ian Rogers1428dce2014-10-21 15:02:15 -07001293 bool HavePointerGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001294 return gpr_index_ > 0;
1295 }
1296
Andreas Gampec200a4a2014-06-16 18:39:09 -07001297 void AdvancePointer(const void* val) {
Andreas Gampec147b002014-03-06 18:11:06 -08001298 if (HavePointerGpr()) {
1299 gpr_index_--;
1300 PushGpr(reinterpret_cast<uintptr_t>(val));
1301 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001302 stack_entries_++; // TODO: have a field for pointer length as multiple of 32b
Andreas Gampec147b002014-03-06 18:11:06 -08001303 PushStack(reinterpret_cast<uintptr_t>(val));
1304 gpr_index_ = 0;
1305 }
1306 }
1307
Ian Rogers1428dce2014-10-21 15:02:15 -07001308 bool HaveHandleScopeGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001309 return gpr_index_ > 0;
1310 }
1311
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001312 void AdvanceHandleScope(mirror::Object* ptr) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001313 uintptr_t handle = PushHandle(ptr);
1314 if (HaveHandleScopeGpr()) {
Andreas Gampec147b002014-03-06 18:11:06 -08001315 gpr_index_--;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001316 PushGpr(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001317 } else {
1318 stack_entries_++;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001319 PushStack(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001320 gpr_index_ = 0;
1321 }
1322 }
1323
Ian Rogers1428dce2014-10-21 15:02:15 -07001324 bool HaveIntGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001325 return gpr_index_ > 0;
1326 }
1327
1328 void AdvanceInt(uint32_t val) {
1329 if (HaveIntGpr()) {
1330 gpr_index_--;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001331 if (kMultiGPRegistersWidened) {
1332 DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001333 PushGpr(static_cast<int64_t>(bit_cast<int32_t, uint32_t>(val)));
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001334 } else {
1335 PushGpr(val);
1336 }
Andreas Gampec147b002014-03-06 18:11:06 -08001337 } else {
1338 stack_entries_++;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001339 if (kMultiGPRegistersWidened) {
1340 DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001341 PushStack(static_cast<int64_t>(bit_cast<int32_t, uint32_t>(val)));
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001342 } else {
1343 PushStack(val);
1344 }
Andreas Gampec147b002014-03-06 18:11:06 -08001345 gpr_index_ = 0;
1346 }
1347 }
1348
Ian Rogers1428dce2014-10-21 15:02:15 -07001349 bool HaveLongGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001350 return gpr_index_ >= kRegistersNeededForLong + (LongGprNeedsPadding() ? 1 : 0);
1351 }
1352
Ian Rogers1428dce2014-10-21 15:02:15 -07001353 bool LongGprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001354 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1355 kAlignLongOnStack && // and when it needs alignment
1356 (gpr_index_ & 1) == 1; // counter is odd, see constructor
1357 }
1358
Ian Rogers1428dce2014-10-21 15:02:15 -07001359 bool LongStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001360 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1361 kAlignLongOnStack && // and when it needs 8B alignment
1362 (stack_entries_ & 1) == 1; // counter is odd
1363 }
1364
1365 void AdvanceLong(uint64_t val) {
1366 if (HaveLongGpr()) {
1367 if (LongGprNeedsPadding()) {
1368 PushGpr(0);
1369 gpr_index_--;
1370 }
1371 if (kRegistersNeededForLong == 1) {
1372 PushGpr(static_cast<uintptr_t>(val));
1373 } else {
1374 PushGpr(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1375 PushGpr(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1376 }
1377 gpr_index_ -= kRegistersNeededForLong;
1378 } else {
1379 if (LongStackNeedsPadding()) {
1380 PushStack(0);
1381 stack_entries_++;
1382 }
1383 if (kRegistersNeededForLong == 1) {
1384 PushStack(static_cast<uintptr_t>(val));
1385 stack_entries_++;
1386 } else {
1387 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1388 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1389 stack_entries_ += 2;
1390 }
1391 gpr_index_ = 0;
1392 }
1393 }
1394
Ian Rogers1428dce2014-10-21 15:02:15 -07001395 bool HaveFloatFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001396 return fpr_index_ > 0;
1397 }
1398
Andreas Gampec147b002014-03-06 18:11:06 -08001399 void AdvanceFloat(float val) {
1400 if (kNativeSoftFloatAbi) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001401 AdvanceInt(bit_cast<uint32_t, float>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001402 } else {
1403 if (HaveFloatFpr()) {
1404 fpr_index_--;
1405 if (kRegistersNeededForDouble == 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001406 if (kMultiFPRegistersWidened) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001407 PushFpr8(bit_cast<uint64_t, double>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001408 } else {
1409 // No widening, just use the bits.
Roland Levillainda4d79b2015-03-24 14:36:11 +00001410 PushFpr8(static_cast<uint64_t>(bit_cast<uint32_t, float>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001411 }
1412 } else {
1413 PushFpr4(val);
1414 }
1415 } else {
1416 stack_entries_++;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001417 if (kRegistersNeededForDouble == 1 && kMultiFPRegistersWidened) {
Andreas Gampec147b002014-03-06 18:11:06 -08001418 // Need to widen before storing: Note the "double" in the template instantiation.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001419 // Note: We need to jump through those hoops to make the compiler happy.
1420 DCHECK_EQ(sizeof(uintptr_t), sizeof(uint64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001421 PushStack(static_cast<uintptr_t>(bit_cast<uint64_t, double>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001422 } else {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001423 PushStack(static_cast<uintptr_t>(bit_cast<uint32_t, float>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001424 }
1425 fpr_index_ = 0;
1426 }
1427 }
1428 }
1429
Ian Rogers1428dce2014-10-21 15:02:15 -07001430 bool HaveDoubleFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001431 return fpr_index_ >= kRegistersNeededForDouble + (DoubleFprNeedsPadding() ? 1 : 0);
1432 }
1433
Ian Rogers1428dce2014-10-21 15:02:15 -07001434 bool DoubleFprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001435 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1436 kAlignDoubleOnStack && // and when it needs alignment
1437 (fpr_index_ & 1) == 1; // counter is odd, see constructor
1438 }
1439
Ian Rogers1428dce2014-10-21 15:02:15 -07001440 bool DoubleStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001441 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1442 kAlignDoubleOnStack && // and when it needs 8B alignment
1443 (stack_entries_ & 1) == 1; // counter is odd
1444 }
1445
1446 void AdvanceDouble(uint64_t val) {
1447 if (kNativeSoftFloatAbi) {
1448 AdvanceLong(val);
1449 } else {
1450 if (HaveDoubleFpr()) {
1451 if (DoubleFprNeedsPadding()) {
1452 PushFpr4(0);
1453 fpr_index_--;
1454 }
1455 PushFpr8(val);
1456 fpr_index_ -= kRegistersNeededForDouble;
1457 } else {
1458 if (DoubleStackNeedsPadding()) {
1459 PushStack(0);
1460 stack_entries_++;
1461 }
1462 if (kRegistersNeededForDouble == 1) {
1463 PushStack(static_cast<uintptr_t>(val));
1464 stack_entries_++;
1465 } else {
1466 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1467 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1468 stack_entries_ += 2;
1469 }
1470 fpr_index_ = 0;
1471 }
1472 }
1473 }
1474
Ian Rogers1428dce2014-10-21 15:02:15 -07001475 uint32_t GetStackEntries() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001476 return stack_entries_;
1477 }
1478
Ian Rogers1428dce2014-10-21 15:02:15 -07001479 uint32_t GetNumberOfUsedGprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001480 return kNumNativeGprArgs - gpr_index_;
1481 }
1482
Ian Rogers1428dce2014-10-21 15:02:15 -07001483 uint32_t GetNumberOfUsedFprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001484 return kNumNativeFprArgs - fpr_index_;
1485 }
1486
1487 private:
1488 void PushGpr(uintptr_t val) {
1489 delegate_->PushGpr(val);
1490 }
1491 void PushFpr4(float val) {
1492 delegate_->PushFpr4(val);
1493 }
1494 void PushFpr8(uint64_t val) {
1495 delegate_->PushFpr8(val);
1496 }
1497 void PushStack(uintptr_t val) {
1498 delegate_->PushStack(val);
1499 }
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001500 uintptr_t PushHandle(mirror::Object* ref) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001501 return delegate_->PushHandle(ref);
Andreas Gampec147b002014-03-06 18:11:06 -08001502 }
1503
1504 uint32_t gpr_index_; // Number of free GPRs
1505 uint32_t fpr_index_; // Number of free FPRs
1506 uint32_t stack_entries_; // Stack entries are in multiples of 32b, as floats are usually not
1507 // extended
Ian Rogers1428dce2014-10-21 15:02:15 -07001508 T* const delegate_; // What Push implementation gets called
Andreas Gampec147b002014-03-06 18:11:06 -08001509};
1510
Andreas Gampec200a4a2014-06-16 18:39:09 -07001511// Computes the sizes of register stacks and call stack area. Handling of references can be extended
1512// in subclasses.
1513//
1514// To handle native pointers, use "L" in the shorty for an object reference, which simulates
1515// them with handles.
1516class ComputeNativeCallFrameSize {
Andreas Gampec147b002014-03-06 18:11:06 -08001517 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001518 ComputeNativeCallFrameSize() : num_stack_entries_(0) {}
1519
1520 virtual ~ComputeNativeCallFrameSize() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001521
Ian Rogers1428dce2014-10-21 15:02:15 -07001522 uint32_t GetStackSize() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001523 return num_stack_entries_ * sizeof(uintptr_t);
1524 }
1525
Ian Rogers1428dce2014-10-21 15:02:15 -07001526 uint8_t* LayoutCallStack(uint8_t* sp8) const {
Andreas Gampec147b002014-03-06 18:11:06 -08001527 sp8 -= GetStackSize();
Andreas Gampe779f8c92014-06-09 18:29:38 -07001528 // Align by kStackAlignment.
1529 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
Andreas Gampec200a4a2014-06-16 18:39:09 -07001530 return sp8;
Andreas Gampec147b002014-03-06 18:11:06 -08001531 }
1532
Ian Rogers1428dce2014-10-21 15:02:15 -07001533 uint8_t* LayoutCallRegisterStacks(uint8_t* sp8, uintptr_t** start_gpr, uint32_t** start_fpr)
1534 const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001535 // Assumption is OK right now, as we have soft-float arm
1536 size_t fregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeFprArgs;
1537 sp8 -= fregs * sizeof(uintptr_t);
1538 *start_fpr = reinterpret_cast<uint32_t*>(sp8);
1539 size_t iregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeGprArgs;
1540 sp8 -= iregs * sizeof(uintptr_t);
1541 *start_gpr = reinterpret_cast<uintptr_t*>(sp8);
1542 return sp8;
1543 }
Andreas Gampec147b002014-03-06 18:11:06 -08001544
Andreas Gampec200a4a2014-06-16 18:39:09 -07001545 uint8_t* LayoutNativeCall(uint8_t* sp8, uintptr_t** start_stack, uintptr_t** start_gpr,
Ian Rogers1428dce2014-10-21 15:02:15 -07001546 uint32_t** start_fpr) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001547 // Native call stack.
1548 sp8 = LayoutCallStack(sp8);
1549 *start_stack = reinterpret_cast<uintptr_t*>(sp8);
Andreas Gampec147b002014-03-06 18:11:06 -08001550
Andreas Gampec200a4a2014-06-16 18:39:09 -07001551 // Put fprs and gprs below.
1552 sp8 = LayoutCallRegisterStacks(sp8, start_gpr, start_fpr);
Andreas Gampec147b002014-03-06 18:11:06 -08001553
Andreas Gampec200a4a2014-06-16 18:39:09 -07001554 // Return the new bottom.
1555 return sp8;
1556 }
1557
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001558 virtual void WalkHeader(
1559 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm ATTRIBUTE_UNUSED)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001560 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001561 }
Andreas Gampec200a4a2014-06-16 18:39:09 -07001562
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001563 void Walk(const char* shorty, uint32_t shorty_len) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001564 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize> sm(this);
1565
1566 WalkHeader(&sm);
Andreas Gampec147b002014-03-06 18:11:06 -08001567
1568 for (uint32_t i = 1; i < shorty_len; ++i) {
1569 Primitive::Type cur_type_ = Primitive::GetType(shorty[i]);
1570 switch (cur_type_) {
1571 case Primitive::kPrimNot:
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001572 // TODO: fix abuse of mirror types.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001573 sm.AdvanceHandleScope(
1574 reinterpret_cast<mirror::Object*>(0x12345678));
Andreas Gampec147b002014-03-06 18:11:06 -08001575 break;
1576
1577 case Primitive::kPrimBoolean:
1578 case Primitive::kPrimByte:
1579 case Primitive::kPrimChar:
1580 case Primitive::kPrimShort:
1581 case Primitive::kPrimInt:
1582 sm.AdvanceInt(0);
1583 break;
1584 case Primitive::kPrimFloat:
1585 sm.AdvanceFloat(0);
1586 break;
1587 case Primitive::kPrimDouble:
1588 sm.AdvanceDouble(0);
1589 break;
1590 case Primitive::kPrimLong:
1591 sm.AdvanceLong(0);
1592 break;
1593 default:
1594 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001595 UNREACHABLE();
Andreas Gampec147b002014-03-06 18:11:06 -08001596 }
1597 }
1598
Ian Rogers1428dce2014-10-21 15:02:15 -07001599 num_stack_entries_ = sm.GetStackEntries();
Andreas Gampec147b002014-03-06 18:11:06 -08001600 }
1601
1602 void PushGpr(uintptr_t /* val */) {
1603 // not optimizing registers, yet
1604 }
1605
1606 void PushFpr4(float /* val */) {
1607 // not optimizing registers, yet
1608 }
1609
1610 void PushFpr8(uint64_t /* val */) {
1611 // not optimizing registers, yet
1612 }
1613
1614 void PushStack(uintptr_t /* val */) {
1615 // counting is already done in the superclass
1616 }
1617
Andreas Gampec200a4a2014-06-16 18:39:09 -07001618 virtual uintptr_t PushHandle(mirror::Object* /* ptr */) {
Andreas Gampec147b002014-03-06 18:11:06 -08001619 return reinterpret_cast<uintptr_t>(nullptr);
1620 }
1621
Andreas Gampec200a4a2014-06-16 18:39:09 -07001622 protected:
Andreas Gampec147b002014-03-06 18:11:06 -08001623 uint32_t num_stack_entries_;
1624};
1625
Andreas Gampec200a4a2014-06-16 18:39:09 -07001626class ComputeGenericJniFrameSize FINAL : public ComputeNativeCallFrameSize {
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001627 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001628 ComputeGenericJniFrameSize() : num_handle_scope_references_(0) {}
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001629
Andreas Gampec200a4a2014-06-16 18:39:09 -07001630 // Lays out the callee-save frame. Assumes that the incorrect frame corresponding to RefsAndArgs
1631 // is at *m = sp. Will update to point to the bottom of the save frame.
1632 //
1633 // Note: assumes ComputeAll() has been run before.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001634 void LayoutCalleeSaveFrame(Thread* self, ArtMethod*** m, void* sp, HandleScope** handle_scope)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001635 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001636 ArtMethod* method = **m;
1637
Andreas Gampe542451c2016-07-26 09:02:02 -07001638 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001639
Andreas Gampec200a4a2014-06-16 18:39:09 -07001640 uint8_t* sp8 = reinterpret_cast<uint8_t*>(sp);
1641
1642 // First, fix up the layout of the callee-save frame.
1643 // We have to squeeze in the HandleScope, and relocate the method pointer.
1644
1645 // "Free" the slot for the method.
Ian Rogers13735952014-10-08 12:43:28 -07001646 sp8 += sizeof(void*); // In the callee-save frame we use a full pointer.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001647
1648 // Under the callee saves put handle scope and new method stack reference.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001649 size_t handle_scope_size = HandleScope::SizeOf(num_handle_scope_references_);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001650 size_t scope_and_method = handle_scope_size + sizeof(ArtMethod*);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001651
1652 sp8 -= scope_and_method;
1653 // Align by kStackAlignment.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001654 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
Andreas Gampec200a4a2014-06-16 18:39:09 -07001655
Mathieu Chartiere401d142015-04-22 13:56:20 -07001656 uint8_t* sp8_table = sp8 + sizeof(ArtMethod*);
Ian Rogers59c07062014-10-10 13:03:39 -07001657 *handle_scope = HandleScope::Create(sp8_table, self->GetTopHandleScope(),
1658 num_handle_scope_references_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001659
1660 // Add a slot for the method pointer, and fill it. Fix the pointer-pointer given to us.
1661 uint8_t* method_pointer = sp8;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001662 auto** new_method_ref = reinterpret_cast<ArtMethod**>(method_pointer);
1663 *new_method_ref = method;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001664 *m = new_method_ref;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001665 }
1666
Andreas Gampec200a4a2014-06-16 18:39:09 -07001667 // Adds space for the cookie. Note: may leave stack unaligned.
Ian Rogers1428dce2014-10-21 15:02:15 -07001668 void LayoutCookie(uint8_t** sp) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001669 // Reference cookie and padding
1670 *sp -= 8;
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001671 }
1672
Andreas Gampec200a4a2014-06-16 18:39:09 -07001673 // Re-layout the callee-save frame (insert a handle-scope). Then add space for the cookie.
1674 // Returns the new bottom. Note: this may be unaligned.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001675 uint8_t* LayoutJNISaveFrame(Thread* self, ArtMethod*** m, void* sp, HandleScope** handle_scope)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001676 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001677 // First, fix up the layout of the callee-save frame.
1678 // We have to squeeze in the HandleScope, and relocate the method pointer.
Ian Rogers59c07062014-10-10 13:03:39 -07001679 LayoutCalleeSaveFrame(self, m, sp, handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001680
1681 // The bottom of the callee-save frame is now where the method is, *m.
1682 uint8_t* sp8 = reinterpret_cast<uint8_t*>(*m);
1683
1684 // Add space for cookie.
1685 LayoutCookie(&sp8);
1686
1687 return sp8;
1688 }
1689
1690 // WARNING: After this, *sp won't be pointing to the method anymore!
Mathieu Chartiere401d142015-04-22 13:56:20 -07001691 uint8_t* ComputeLayout(Thread* self, ArtMethod*** m, const char* shorty, uint32_t shorty_len,
1692 HandleScope** handle_scope, uintptr_t** start_stack, uintptr_t** start_gpr,
1693 uint32_t** start_fpr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001694 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001695 Walk(shorty, shorty_len);
1696
1697 // JNI part.
Ian Rogers59c07062014-10-10 13:03:39 -07001698 uint8_t* sp8 = LayoutJNISaveFrame(self, m, reinterpret_cast<void*>(*m), handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001699
1700 sp8 = LayoutNativeCall(sp8, start_stack, start_gpr, start_fpr);
1701
1702 // Return the new bottom.
1703 return sp8;
1704 }
1705
1706 uintptr_t PushHandle(mirror::Object* /* ptr */) OVERRIDE;
1707
1708 // Add JNIEnv* and jobj/jclass before the shorty-derived elements.
1709 void WalkHeader(BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) OVERRIDE
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001710 REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001711
1712 private:
1713 uint32_t num_handle_scope_references_;
1714};
1715
1716uintptr_t ComputeGenericJniFrameSize::PushHandle(mirror::Object* /* ptr */) {
1717 num_handle_scope_references_++;
1718 return reinterpret_cast<uintptr_t>(nullptr);
1719}
1720
1721void ComputeGenericJniFrameSize::WalkHeader(
1722 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) {
1723 // JNIEnv
1724 sm->AdvancePointer(nullptr);
1725
1726 // Class object or this as first argument
1727 sm->AdvanceHandleScope(reinterpret_cast<mirror::Object*>(0x12345678));
1728}
1729
1730// Class to push values to three separate regions. Used to fill the native call part. Adheres to
1731// the template requirements of BuildGenericJniFrameStateMachine.
1732class FillNativeCall {
1733 public:
1734 FillNativeCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) :
1735 cur_gpr_reg_(gpr_regs), cur_fpr_reg_(fpr_regs), cur_stack_arg_(stack_args) {}
1736
1737 virtual ~FillNativeCall() {}
1738
1739 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) {
1740 cur_gpr_reg_ = gpr_regs;
1741 cur_fpr_reg_ = fpr_regs;
1742 cur_stack_arg_ = stack_args;
Andreas Gampec147b002014-03-06 18:11:06 -08001743 }
1744
1745 void PushGpr(uintptr_t val) {
1746 *cur_gpr_reg_ = val;
1747 cur_gpr_reg_++;
1748 }
1749
1750 void PushFpr4(float val) {
1751 *cur_fpr_reg_ = val;
1752 cur_fpr_reg_++;
1753 }
1754
1755 void PushFpr8(uint64_t val) {
1756 uint64_t* tmp = reinterpret_cast<uint64_t*>(cur_fpr_reg_);
1757 *tmp = val;
1758 cur_fpr_reg_ += 2;
1759 }
1760
1761 void PushStack(uintptr_t val) {
1762 *cur_stack_arg_ = val;
1763 cur_stack_arg_++;
1764 }
1765
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001766 virtual uintptr_t PushHandle(mirror::Object*) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001767 LOG(FATAL) << "(Non-JNI) Native call does not use handles.";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001768 UNREACHABLE();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001769 }
1770
1771 private:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001772 uintptr_t* cur_gpr_reg_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001773 uint32_t* cur_fpr_reg_;
1774 uintptr_t* cur_stack_arg_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001775};
Andreas Gampec147b002014-03-06 18:11:06 -08001776
Andreas Gampec200a4a2014-06-16 18:39:09 -07001777// Visits arguments on the stack placing them into a region lower down the stack for the benefit
1778// of transitioning into native code.
1779class BuildGenericJniFrameVisitor FINAL : public QuickArgumentVisitor {
1780 public:
Ian Rogers59c07062014-10-10 13:03:39 -07001781 BuildGenericJniFrameVisitor(Thread* self, bool is_static, const char* shorty, uint32_t shorty_len,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001782 ArtMethod*** sp)
Andreas Gampec200a4a2014-06-16 18:39:09 -07001783 : QuickArgumentVisitor(*sp, is_static, shorty, shorty_len),
1784 jni_call_(nullptr, nullptr, nullptr, nullptr), sm_(&jni_call_) {
1785 ComputeGenericJniFrameSize fsc;
1786 uintptr_t* start_gpr_reg;
1787 uint32_t* start_fpr_reg;
1788 uintptr_t* start_stack_arg;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001789 bottom_of_used_area_ = fsc.ComputeLayout(self, sp, shorty, shorty_len,
Ian Rogers59c07062014-10-10 13:03:39 -07001790 &handle_scope_,
1791 &start_stack_arg,
Andreas Gampec200a4a2014-06-16 18:39:09 -07001792 &start_gpr_reg, &start_fpr_reg);
1793
Andreas Gampec200a4a2014-06-16 18:39:09 -07001794 jni_call_.Reset(start_gpr_reg, start_fpr_reg, start_stack_arg, handle_scope_);
1795
1796 // jni environment is always first argument
1797 sm_.AdvancePointer(self->GetJniEnv());
1798
1799 if (is_static) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001800 sm_.AdvanceHandleScope((**sp)->GetDeclaringClass());
Andreas Gampec200a4a2014-06-16 18:39:09 -07001801 }
1802 }
1803
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001804 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001805
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001806 void FinalizeHandleScope(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001807
Vladimir Markof39745e2016-01-26 12:16:55 +00001808 StackReference<mirror::Object>* GetFirstHandleScopeEntry() {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001809 return handle_scope_->GetHandle(0).GetReference();
1810 }
1811
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001812 jobject GetFirstHandleScopeJObject() const REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001813 return handle_scope_->GetHandle(0).ToJObject();
1814 }
1815
Ian Rogers1428dce2014-10-21 15:02:15 -07001816 void* GetBottomOfUsedArea() const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001817 return bottom_of_used_area_;
1818 }
1819
1820 private:
1821 // A class to fill a JNI call. Adds reference/handle-scope management to FillNativeCall.
1822 class FillJniCall FINAL : public FillNativeCall {
1823 public:
1824 FillJniCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args,
1825 HandleScope* handle_scope) : FillNativeCall(gpr_regs, fpr_regs, stack_args),
1826 handle_scope_(handle_scope), cur_entry_(0) {}
1827
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001828 uintptr_t PushHandle(mirror::Object* ref) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001829
1830 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args, HandleScope* scope) {
1831 FillNativeCall::Reset(gpr_regs, fpr_regs, stack_args);
1832 handle_scope_ = scope;
1833 cur_entry_ = 0U;
1834 }
1835
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001836 void ResetRemainingScopeSlots() REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001837 // Initialize padding entries.
1838 size_t expected_slots = handle_scope_->NumberOfReferences();
1839 while (cur_entry_ < expected_slots) {
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07001840 handle_scope_->GetMutableHandle(cur_entry_++).Assign(nullptr);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001841 }
1842 DCHECK_NE(cur_entry_, 0U);
1843 }
1844
1845 private:
1846 HandleScope* handle_scope_;
1847 size_t cur_entry_;
1848 };
1849
1850 HandleScope* handle_scope_;
1851 FillJniCall jni_call_;
1852 void* bottom_of_used_area_;
1853
1854 BuildNativeCallFrameStateMachine<FillJniCall> sm_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001855
1856 DISALLOW_COPY_AND_ASSIGN(BuildGenericJniFrameVisitor);
1857};
1858
Andreas Gampec200a4a2014-06-16 18:39:09 -07001859uintptr_t BuildGenericJniFrameVisitor::FillJniCall::PushHandle(mirror::Object* ref) {
1860 uintptr_t tmp;
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07001861 MutableHandle<mirror::Object> h = handle_scope_->GetMutableHandle(cur_entry_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001862 h.Assign(ref);
1863 tmp = reinterpret_cast<uintptr_t>(h.ToJObject());
1864 cur_entry_++;
1865 return tmp;
1866}
1867
Ian Rogers9758f792014-03-13 09:02:55 -07001868void BuildGenericJniFrameVisitor::Visit() {
1869 Primitive::Type type = GetParamPrimitiveType();
1870 switch (type) {
1871 case Primitive::kPrimLong: {
1872 jlong long_arg;
1873 if (IsSplitLongOrDouble()) {
1874 long_arg = ReadSplitLongParam();
1875 } else {
1876 long_arg = *reinterpret_cast<jlong*>(GetParamAddress());
1877 }
1878 sm_.AdvanceLong(long_arg);
1879 break;
1880 }
1881 case Primitive::kPrimDouble: {
1882 uint64_t double_arg;
1883 if (IsSplitLongOrDouble()) {
1884 // Read into union so that we don't case to a double.
1885 double_arg = ReadSplitLongParam();
1886 } else {
1887 double_arg = *reinterpret_cast<uint64_t*>(GetParamAddress());
1888 }
1889 sm_.AdvanceDouble(double_arg);
1890 break;
1891 }
1892 case Primitive::kPrimNot: {
1893 StackReference<mirror::Object>* stack_ref =
1894 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001895 sm_.AdvanceHandleScope(stack_ref->AsMirrorPtr());
Ian Rogers9758f792014-03-13 09:02:55 -07001896 break;
1897 }
1898 case Primitive::kPrimFloat:
1899 sm_.AdvanceFloat(*reinterpret_cast<float*>(GetParamAddress()));
1900 break;
1901 case Primitive::kPrimBoolean: // Fall-through.
1902 case Primitive::kPrimByte: // Fall-through.
1903 case Primitive::kPrimChar: // Fall-through.
1904 case Primitive::kPrimShort: // Fall-through.
1905 case Primitive::kPrimInt: // Fall-through.
1906 sm_.AdvanceInt(*reinterpret_cast<jint*>(GetParamAddress()));
1907 break;
1908 case Primitive::kPrimVoid:
1909 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -07001910 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -07001911 }
1912}
1913
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001914void BuildGenericJniFrameVisitor::FinalizeHandleScope(Thread* self) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001915 // Clear out rest of the scope.
1916 jni_call_.ResetRemainingScopeSlots();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001917 // Install HandleScope.
1918 self->PushHandleScope(handle_scope_);
Ian Rogers9758f792014-03-13 09:02:55 -07001919}
1920
Ian Rogers04c31d22014-07-07 21:44:06 -07001921#if defined(__arm__) || defined(__aarch64__)
Andreas Gampe90546832014-03-12 18:07:19 -07001922extern "C" void* artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07001923#else
1924extern "C" void* artFindNativeMethod(Thread* self);
1925#endif
Andreas Gampe90546832014-03-12 18:07:19 -07001926
Andreas Gampead615172014-04-04 16:20:13 -07001927uint64_t artQuickGenericJniEndJNIRef(Thread* self, uint32_t cookie, jobject l, jobject lock) {
1928 if (lock != nullptr) {
1929 return reinterpret_cast<uint64_t>(JniMethodEndWithReferenceSynchronized(l, cookie, lock, self));
1930 } else {
1931 return reinterpret_cast<uint64_t>(JniMethodEndWithReference(l, cookie, self));
1932 }
1933}
1934
1935void artQuickGenericJniEndJNINonRef(Thread* self, uint32_t cookie, jobject lock) {
1936 if (lock != nullptr) {
1937 JniMethodEndSynchronized(cookie, lock, self);
1938 } else {
1939 JniMethodEnd(cookie, self);
1940 }
1941}
1942
Andreas Gampec147b002014-03-06 18:11:06 -08001943/*
1944 * Initializes an alloca region assumed to be directly below sp for a native call:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001945 * 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 -08001946 * The final element on the stack is a pointer to the native code.
1947 *
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001948 * On entry, the stack has a standard callee-save frame above sp, and an alloca below it.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001949 * We need to fix this, as the handle scope needs to go into the callee-save frame.
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001950 *
Andreas Gampec147b002014-03-06 18:11:06 -08001951 * The return of this function denotes:
1952 * 1) How many bytes of the alloca can be released, if the value is non-negative.
1953 * 2) An error, if the value is negative.
1954 */
Mathieu Chartiere401d142015-04-22 13:56:20 -07001955extern "C" TwoWordReturn artQuickGenericJniTrampoline(Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001956 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001957 ArtMethod* called = *sp;
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001958 DCHECK(called->IsNative()) << PrettyMethod(called, true);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001959 uint32_t shorty_len = 0;
1960 const char* shorty = called->GetShorty(&shorty_len);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001961
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001962 // Run the visitor and update sp.
Ian Rogers59c07062014-10-10 13:03:39 -07001963 BuildGenericJniFrameVisitor visitor(self, called->IsStatic(), shorty, shorty_len, &sp);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001964 visitor.VisitArguments();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001965 visitor.FinalizeHandleScope(self);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001966
Andreas Gampec200a4a2014-06-16 18:39:09 -07001967 // Fix up managed-stack things in Thread.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001968 self->SetTopOfStack(sp);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001969
Ian Rogerse0dcd462014-03-08 15:21:04 -08001970 self->VerifyStack();
1971
Andreas Gampe90546832014-03-12 18:07:19 -07001972 // Start JNI, save the cookie.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001973 uint32_t cookie;
1974 if (called->IsSynchronized()) {
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001975 cookie = JniMethodStartSynchronized(visitor.GetFirstHandleScopeJObject(), self);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001976 if (self->IsExceptionPending()) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001977 self->PopHandleScope();
Andreas Gampec147b002014-03-06 18:11:06 -08001978 // A negative value denotes an error.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001979 return GetTwoWordFailureValue();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001980 }
1981 } else {
1982 cookie = JniMethodStart(self);
1983 }
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001984 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
Ian Rogerse0dcd462014-03-08 15:21:04 -08001985 *(sp32 - 1) = cookie;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001986
Andreas Gampe90546832014-03-12 18:07:19 -07001987 // Retrieve the stored native code.
Mathieu Chartier2d721012014-11-10 11:08:06 -08001988 void* nativeCode = called->GetEntryPointFromJni();
Andreas Gampe90546832014-03-12 18:07:19 -07001989
Andreas Gampe9a6a99a2014-03-14 07:52:20 -07001990 // There are two cases for the content of nativeCode:
1991 // 1) Pointer to the native function.
1992 // 2) Pointer to the trampoline for native code binding.
1993 // In the second case, we need to execute the binding and continue with the actual native function
1994 // pointer.
Andreas Gampe90546832014-03-12 18:07:19 -07001995 DCHECK(nativeCode != nullptr);
1996 if (nativeCode == GetJniDlsymLookupStub()) {
Ian Rogers04c31d22014-07-07 21:44:06 -07001997#if defined(__arm__) || defined(__aarch64__)
Andreas Gampe90546832014-03-12 18:07:19 -07001998 nativeCode = artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07001999#else
2000 nativeCode = artFindNativeMethod(self);
2001#endif
Andreas Gampe90546832014-03-12 18:07:19 -07002002
2003 if (nativeCode == nullptr) {
2004 DCHECK(self->IsExceptionPending()); // There should be an exception pending now.
Andreas Gampead615172014-04-04 16:20:13 -07002005
2006 // End JNI, as the assembly will move to deliver the exception.
Mathieu Chartier0cd81352014-05-22 16:48:55 -07002007 jobject lock = called->IsSynchronized() ? visitor.GetFirstHandleScopeJObject() : nullptr;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002008 if (shorty[0] == 'L') {
Andreas Gampead615172014-04-04 16:20:13 -07002009 artQuickGenericJniEndJNIRef(self, cookie, nullptr, lock);
2010 } else {
2011 artQuickGenericJniEndJNINonRef(self, cookie, lock);
2012 }
2013
Andreas Gampec200a4a2014-06-16 18:39:09 -07002014 return GetTwoWordFailureValue();
Andreas Gampe90546832014-03-12 18:07:19 -07002015 }
2016 // Note that the native code pointer will be automatically set by artFindNativeMethod().
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002017 }
2018
Andreas Gampec200a4a2014-06-16 18:39:09 -07002019 // Return native code addr(lo) and bottom of alloca address(hi).
2020 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(visitor.GetBottomOfUsedArea()),
2021 reinterpret_cast<uintptr_t>(nativeCode));
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002022}
2023
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002024// Defined in quick_jni_entrypoints.cc.
2025extern uint64_t GenericJniMethodEnd(Thread* self, uint32_t saved_local_ref_cookie,
2026 jvalue result, uint64_t result_f, ArtMethod* called,
2027 HandleScope* handle_scope);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002028/*
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002029 * Is called after the native JNI code. Responsible for cleanup (handle scope, saved state) and
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002030 * unlocking.
2031 */
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002032extern "C" uint64_t artQuickGenericJniEndTrampoline(Thread* self,
2033 jvalue result,
2034 uint64_t result_f) {
2035 // We're here just back from a native call. We don't have the shared mutator lock at this point
2036 // yet until we call GoToRunnable() later in GenericJniMethodEnd(). Accessing objects or doing
2037 // anything that requires a mutator lock before that would cause problems as GC may have the
2038 // exclusive mutator lock and may be moving objects, etc.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002039 ArtMethod** sp = self->GetManagedStack()->GetTopQuickFrame();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002040 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002041 ArtMethod* called = *sp;
Ian Rogerse0dcd462014-03-08 15:21:04 -08002042 uint32_t cookie = *(sp32 - 1);
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002043 HandleScope* table = reinterpret_cast<HandleScope*>(reinterpret_cast<uint8_t*>(sp) + sizeof(*sp));
2044 return GenericJniMethodEnd(self, cookie, result, result_f, called, table);
Andreas Gampe2da88232014-02-27 12:26:20 -08002045}
2046
Andreas Gamped58342c2014-06-05 14:18:08 -07002047// We use TwoWordReturn to optimize scalar returns. We use the hi value for code, and the lo value
2048// for the method pointer.
Andreas Gampe51f76352014-05-21 08:28:48 -07002049//
Andreas Gamped58342c2014-06-05 14:18:08 -07002050// It is valid to use this, as at the usage points here (returns from C functions) we are assuming
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002051// to hold the mutator lock (see REQUIRES_SHARED(Locks::mutator_lock_) annotations).
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002052
2053template<InvokeType type, bool access_check>
Mathieu Chartiere401d142015-04-22 13:56:20 -07002054static TwoWordReturn artInvokeCommon(uint32_t method_idx, mirror::Object* this_object, Thread* self,
2055 ArtMethod** sp) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002056 ScopedQuickEntrypointChecks sqec(self);
Vladimir Markofd36f1f2016-08-03 18:49:58 +01002057 DCHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(Runtime::kSaveRefsAndArgs));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002058 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
2059 ArtMethod* method = FindMethodFast(method_idx, this_object, caller_method, access_check, type);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002060 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002061 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()->GetDexFile();
2062 uint32_t shorty_len;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002063 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(method_idx), &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002064 {
2065 // Remember the args in case a GC happens in FindMethodFromCode.
2066 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
2067 RememberForGcArgumentVisitor visitor(sp, type == kStatic, shorty, shorty_len, &soa);
2068 visitor.VisitArguments();
Andreas Gampe3a357142015-08-07 17:20:11 -07002069 method = FindMethodFromCode<type, access_check>(method_idx, &this_object, caller_method,
Mathieu Chartier0cd81352014-05-22 16:48:55 -07002070 self);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002071 visitor.FixupReferences();
2072 }
2073
Ian Rogerse0a02da2014-12-02 14:10:53 -08002074 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002075 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07002076 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002077 }
2078 }
2079 DCHECK(!self->IsExceptionPending());
2080 const void* code = method->GetEntryPointFromQuickCompiledCode();
2081
2082 // When we return, the caller will branch to this address, so it had better not be 0!
Ian Rogerse0a02da2014-12-02 14:10:53 -08002083 DCHECK(code != nullptr) << "Code was null in method: " << PrettyMethod(method)
Andreas Gampec200a4a2014-06-16 18:39:09 -07002084 << " location: "
2085 << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07002086
Andreas Gamped58342c2014-06-05 14:18:08 -07002087 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
2088 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002089}
2090
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01002091// Explicit artInvokeCommon template function declarations to please analysis tool.
2092#define EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(type, access_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002093 template REQUIRES_SHARED(Locks::mutator_lock_) \
Mathieu Chartiere401d142015-04-22 13:56:20 -07002094 TwoWordReturn artInvokeCommon<type, access_check>( \
2095 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01002096
2097EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, false);
2098EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, true);
2099EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, false);
2100EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, true);
2101EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, false);
2102EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, true);
2103EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, false);
2104EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, true);
2105EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, false);
2106EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, true);
2107#undef EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL
2108
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002109// See comments in runtime_support_asm.S
Andreas Gampec200a4a2014-06-16 18:39:09 -07002110extern "C" TwoWordReturn artInvokeInterfaceTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002111 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002112 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002113 return artInvokeCommon<kInterface, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002114}
2115
Andreas Gampec200a4a2014-06-16 18:39:09 -07002116extern "C" TwoWordReturn artInvokeDirectTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002117 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002118 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002119 return artInvokeCommon<kDirect, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002120}
2121
Andreas Gampec200a4a2014-06-16 18:39:09 -07002122extern "C" TwoWordReturn artInvokeStaticTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002123 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002124 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002125 return artInvokeCommon<kStatic, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002126}
2127
Andreas Gampec200a4a2014-06-16 18:39:09 -07002128extern "C" TwoWordReturn artInvokeSuperTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002129 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002130 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002131 return artInvokeCommon<kSuper, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002132}
2133
Andreas Gampec200a4a2014-06-16 18:39:09 -07002134extern "C" TwoWordReturn artInvokeVirtualTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002135 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002136 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002137 return artInvokeCommon<kVirtual, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002138}
2139
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002140// Determine target of interface dispatch. This object is known non-null. First argument
2141// is there for consistency but should not be used, as some architectures overwrite it
2142// in the assembly trampoline.
2143extern "C" TwoWordReturn artInvokeInterfaceTrampoline(uint32_t deadbeef ATTRIBUTE_UNUSED,
Andreas Gampe51f76352014-05-21 08:28:48 -07002144 mirror::Object* this_object,
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002145 Thread* self,
2146 ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002147 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002148 ScopedQuickEntrypointChecks sqec(self);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002149 StackHandleScope<1> hs(self);
2150 Handle<mirror::Class> cls(hs.NewHandle(this_object->GetClass()));
2151
Nicolas Geoffray5bf7bac2016-07-06 14:18:23 +00002152 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002153
2154 // Fetch the dex_method_idx of the target interface method from the caller.
2155 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
2156
2157 const DexFile::CodeItem* code_item = caller_method->GetCodeItem();
2158 CHECK_LT(dex_pc, code_item->insns_size_in_code_units_);
2159 const Instruction* instr = Instruction::At(&code_item->insns_[dex_pc]);
2160 Instruction::Code instr_code = instr->Opcode();
2161 CHECK(instr_code == Instruction::INVOKE_INTERFACE ||
2162 instr_code == Instruction::INVOKE_INTERFACE_RANGE)
2163 << "Unexpected call into interface trampoline: " << instr->DumpString(nullptr);
2164 uint32_t dex_method_idx;
2165 if (instr_code == Instruction::INVOKE_INTERFACE) {
2166 dex_method_idx = instr->VRegB_35c();
2167 } else {
2168 CHECK_EQ(instr_code, Instruction::INVOKE_INTERFACE_RANGE);
2169 dex_method_idx = instr->VRegB_3rc();
2170 }
2171
Mathieu Chartiere401d142015-04-22 13:56:20 -07002172 ArtMethod* interface_method = caller_method->GetDexCacheResolvedMethod(
Andreas Gampe542451c2016-07-26 09:02:02 -07002173 dex_method_idx, kRuntimePointerSize);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002174 DCHECK(interface_method != nullptr) << dex_method_idx << " " << PrettyMethod(caller_method);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002175 ArtMethod* method = nullptr;
Andreas Gampe542451c2016-07-26 09:02:02 -07002176 ImTable* imt = cls->GetImt(kRuntimePointerSize);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002177
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002178 if (LIKELY(interface_method->GetDexMethodIndex() != DexFile::kDexNoIndex)) {
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002179 // If the dex cache already resolved the interface method, look whether we have
2180 // a match in the ImtConflictTable.
Andreas Gampe542451c2016-07-26 09:02:02 -07002181 ArtMethod* conflict_method = imt->Get(interface_method->GetImtIndex(), kRuntimePointerSize);
Alex Light9fc547a2016-03-31 14:34:33 -07002182 if (LIKELY(conflict_method->IsRuntimeMethod())) {
Andreas Gampe542451c2016-07-26 09:02:02 -07002183 ImtConflictTable* current_table = conflict_method->GetImtConflictTable(kRuntimePointerSize);
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002184 DCHECK(current_table != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -07002185 method = current_table->Lookup(interface_method, kRuntimePointerSize);
Alex Light9fc547a2016-03-31 14:34:33 -07002186 } else {
2187 // It seems we aren't really a conflict method!
Andreas Gampe542451c2016-07-26 09:02:02 -07002188 method = cls->FindVirtualMethodForInterface(interface_method, kRuntimePointerSize);
Alex Light9fc547a2016-03-31 14:34:33 -07002189 }
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002190 if (method != nullptr) {
2191 return GetTwoWordSuccessValue(
2192 reinterpret_cast<uintptr_t>(method->GetEntryPointFromQuickCompiledCode()),
2193 reinterpret_cast<uintptr_t>(method));
2194 }
2195
2196 // No match, use the IfTable.
Andreas Gampe542451c2016-07-26 09:02:02 -07002197 method = cls->FindVirtualMethodForInterface(interface_method, kRuntimePointerSize);
Ian Rogerse0a02da2014-12-02 14:10:53 -08002198 if (UNLIKELY(method == nullptr)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002199 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(
2200 interface_method, this_object, caller_method);
Andreas Gamped58342c2014-06-05 14:18:08 -07002201 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002202 }
2203 } else {
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002204 // The dex cache did not resolve the method, look it up in the dex file
2205 // of the caller,
Mathieu Chartier4edd8472015-06-01 10:47:36 -07002206 DCHECK_EQ(interface_method, Runtime::Current()->GetResolutionMethod());
Andreas Gampec200a4a2014-06-16 18:39:09 -07002207 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()
2208 ->GetDexFile();
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002209 uint32_t shorty_len;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002210 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(dex_method_idx),
2211 &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002212 {
2213 // Remember the args in case a GC happens in FindMethodFromCode.
2214 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
2215 RememberForGcArgumentVisitor visitor(sp, false, shorty, shorty_len, &soa);
2216 visitor.VisitArguments();
Andreas Gampe3a357142015-08-07 17:20:11 -07002217 method = FindMethodFromCode<kInterface, false>(dex_method_idx, &this_object, caller_method,
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002218 self);
2219 visitor.FixupReferences();
2220 }
2221
2222 if (UNLIKELY(method == nullptr)) {
2223 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07002224 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002225 }
Andreas Gampe542451c2016-07-26 09:02:02 -07002226 interface_method =
2227 caller_method->GetDexCacheResolvedMethod(dex_method_idx, kRuntimePointerSize);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002228 DCHECK(!interface_method->IsRuntimeMethod());
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002229 }
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002230
2231 // We arrive here if we have found an implementation, and it is not in the ImtConflictTable.
2232 // We create a new table with the new pair { interface_method, method }.
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002233 uint32_t imt_index = interface_method->GetImtIndex();
Andreas Gampe542451c2016-07-26 09:02:02 -07002234 ArtMethod* conflict_method = imt->Get(imt_index, kRuntimePointerSize);
Alex Light9fc547a2016-03-31 14:34:33 -07002235 if (conflict_method->IsRuntimeMethod()) {
Mathieu Chartier7f98c9a2016-04-14 10:49:19 -07002236 ArtMethod* new_conflict_method = Runtime::Current()->GetClassLinker()->AddMethodToConflictTable(
2237 cls.Get(),
2238 conflict_method,
2239 interface_method,
2240 method,
2241 /*force_new_conflict_method*/false);
2242 if (new_conflict_method != conflict_method) {
Alex Light9fc547a2016-03-31 14:34:33 -07002243 // Update the IMT if we create a new conflict method. No fence needed here, as the
2244 // data is consistent.
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002245 imt->Set(imt_index,
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002246 new_conflict_method,
Andreas Gampe542451c2016-07-26 09:02:02 -07002247 kRuntimePointerSize);
Alex Light9fc547a2016-03-31 14:34:33 -07002248 }
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002249 }
2250
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002251 const void* code = method->GetEntryPointFromQuickCompiledCode();
2252
2253 // When we return, the caller will branch to this address, so it had better not be 0!
Ian Rogerse0a02da2014-12-02 14:10:53 -08002254 DCHECK(code != nullptr) << "Code was null in method: " << PrettyMethod(method)
Andreas Gampec200a4a2014-06-16 18:39:09 -07002255 << " location: " << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07002256
Andreas Gamped58342c2014-06-05 14:18:08 -07002257 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
2258 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002259}
2260
Ian Rogers848871b2013-08-05 10:56:33 -07002261} // namespace art