blob: fe82878699d4c30291c8005abc7c1bece7f13766 [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"
Andreas Gampe75a7db62016-09-26 12:04:26 -070026#include "imt_conflict_table.h"
27#include "imtable-inl.h"
Ian Rogers848871b2013-08-05 10:56:33 -070028#include "interpreter/interpreter.h"
Nicolas Geoffray796d6302016-03-13 22:22:31 +000029#include "linear_alloc.h"
Ian Rogerse0a02da2014-12-02 14:10:53 -080030#include "method_reference.h"
Ian Rogers848871b2013-08-05 10:56:33 -070031#include "mirror/class-inl.h"
Mathieu Chartier5f3ded42014-04-03 15:25:30 -070032#include "mirror/dex_cache-inl.h"
Mathieu Chartierfc58af42015-04-16 18:00:39 -070033#include "mirror/method.h"
Ian Rogers848871b2013-08-05 10:56:33 -070034#include "mirror/object-inl.h"
35#include "mirror/object_array-inl.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010036#include "oat_quick_method_header.h"
Andreas Gampe639bdd12015-06-03 11:22:45 -070037#include "quick_exception_handler.h"
Ian Rogers848871b2013-08-05 10:56:33 -070038#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070039#include "scoped_thread_state_change-inl.h"
Andreas Gampeb3025922015-09-01 14:45:00 -070040#include "stack.h"
Daniel Mihalyieb076692014-08-22 17:33:31 +020041#include "debugger.h"
Ian Rogers848871b2013-08-05 10:56:33 -070042
43namespace art {
44
45// Visits the arguments as saved to the stack by a Runtime::kRefAndArgs callee save frame.
46class QuickArgumentVisitor {
Ian Rogers936b37f2014-02-14 00:52:24 -080047 // Number of bytes for each out register in the caller method's frame.
48 static constexpr size_t kBytesStackArgLocation = 4;
Alexei Zavjalov41c507a2014-05-15 16:02:46 +070049 // Frame size in bytes of a callee-save frame for RefsAndArgs.
50 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize =
Vladimir Markofd36f1f2016-08-03 18:49:58 +010051 GetCalleeSaveFrameSize(kRuntimeISA, Runtime::kSaveRefsAndArgs);
Ian Rogers848871b2013-08-05 10:56:33 -070052#if defined(__arm__)
53 // The callee save frame is pointed to by SP.
54 // | argN | |
55 // | ... | |
56 // | arg4 | |
57 // | arg3 spill | | Caller's frame
58 // | arg2 spill | |
59 // | arg1 spill | |
60 // | Method* | ---
61 // | LR |
Zheng Xu5667fdb2014-10-23 18:29:55 +080062 // | ... | 4x6 bytes callee saves
63 // | R3 |
64 // | R2 |
65 // | R1 |
66 // | S15 |
67 // | : |
68 // | S0 |
69 // | | 4x2 bytes padding
Ian Rogers848871b2013-08-05 10:56:33 -070070 // | Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -050071 static constexpr bool kSplitPairAcrossRegisterAndStack = kArm32QuickCodeUseSoftFloat;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +000072 static constexpr bool kAlignPairRegister = !kArm32QuickCodeUseSoftFloat;
Zheng Xu5667fdb2014-10-23 18:29:55 +080073 static constexpr bool kQuickSoftFloatAbi = kArm32QuickCodeUseSoftFloat;
74 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = !kArm32QuickCodeUseSoftFloat;
Goran Jakovljevicff734982015-08-24 12:58:55 +000075 static constexpr bool kQuickSkipOddFpRegisters = false;
Zheng Xu5667fdb2014-10-23 18:29:55 +080076 static constexpr size_t kNumQuickGprArgs = 3;
77 static constexpr size_t kNumQuickFprArgs = kArm32QuickCodeUseSoftFloat ? 0 : 16;
Andreas Gampe1a5c4062015-01-15 12:10:47 -080078 static constexpr bool kGprFprLockstep = false;
Zheng Xub551fdc2014-07-25 11:49:42 +080079 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset =
Vladimir Markofd36f1f2016-08-03 18:49:58 +010080 arm::ArmCalleeSaveFpr1Offset(Runtime::kSaveRefsAndArgs); // Offset of first FPR arg.
Zheng Xub551fdc2014-07-25 11:49:42 +080081 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset =
Vladimir Markofd36f1f2016-08-03 18:49:58 +010082 arm::ArmCalleeSaveGpr1Offset(Runtime::kSaveRefsAndArgs); // Offset of first GPR arg.
Zheng Xub551fdc2014-07-25 11:49:42 +080083 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset =
Vladimir Markofd36f1f2016-08-03 18:49:58 +010084 arm::ArmCalleeSaveLrOffset(Runtime::kSaveRefsAndArgs); // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -080085 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +000086 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -080087 }
Stuart Monteithb95a5342014-03-12 13:32:32 +000088#elif defined(__aarch64__)
89 // The callee save frame is pointed to by SP.
90 // | argN | |
91 // | ... | |
92 // | arg4 | |
93 // | arg3 spill | | Caller's frame
94 // | arg2 spill | |
95 // | arg1 spill | |
96 // | Method* | ---
97 // | LR |
Zheng Xub551fdc2014-07-25 11:49:42 +080098 // | X29 |
Stuart Monteithb95a5342014-03-12 13:32:32 +000099 // | : |
Serban Constantinescu9bd88b02015-04-22 16:24:46 +0100100 // | X20 |
Stuart Monteithb95a5342014-03-12 13:32:32 +0000101 // | X7 |
102 // | : |
103 // | X1 |
Zheng Xub551fdc2014-07-25 11:49:42 +0800104 // | D7 |
Stuart Monteithb95a5342014-03-12 13:32:32 +0000105 // | : |
106 // | D0 |
107 // | | padding
108 // | Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500109 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000110 static constexpr bool kAlignPairRegister = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000111 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800112 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000113 static constexpr bool kQuickSkipOddFpRegisters = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000114 static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs.
115 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800116 static constexpr bool kGprFprLockstep = false;
Zheng Xub551fdc2014-07-25 11:49:42 +0800117 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset =
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100118 arm64::Arm64CalleeSaveFpr1Offset(Runtime::kSaveRefsAndArgs); // Offset of first FPR arg.
Zheng Xub551fdc2014-07-25 11:49:42 +0800119 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset =
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100120 arm64::Arm64CalleeSaveGpr1Offset(Runtime::kSaveRefsAndArgs); // Offset of first GPR arg.
Zheng Xub551fdc2014-07-25 11:49:42 +0800121 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset =
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100122 arm64::Arm64CalleeSaveLrOffset(Runtime::kSaveRefsAndArgs); // Offset of return address.
Stuart Monteithb95a5342014-03-12 13:32:32 +0000123 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000124 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Stuart Monteithb95a5342014-03-12 13:32:32 +0000125 }
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800126#elif defined(__mips__) && !defined(__LP64__)
Ian Rogers848871b2013-08-05 10:56:33 -0700127 // The callee save frame is pointed to by SP.
128 // | argN | |
129 // | ... | |
130 // | arg4 | |
131 // | arg3 spill | | Caller's frame
132 // | arg2 spill | |
133 // | arg1 spill | |
134 // | Method* | ---
135 // | RA |
136 // | ... | callee saves
137 // | A3 | arg3
138 // | A2 | arg2
139 // | A1 | arg1
Goran Jakovljevicff734982015-08-24 12:58:55 +0000140 // | F15 |
141 // | F14 | f_arg1
142 // | F13 |
143 // | F12 | f_arg0
144 // | | padding
Ian Rogers848871b2013-08-05 10:56:33 -0700145 // | A0/Method* | <- sp
Goran Jakovljevicff734982015-08-24 12:58:55 +0000146 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
147 static constexpr bool kAlignPairRegister = true;
148 static constexpr bool kQuickSoftFloatAbi = false;
Zheng Xu5667fdb2014-10-23 18:29:55 +0800149 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000150 static constexpr bool kQuickSkipOddFpRegisters = true;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800151 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs.
Goran Jakovljevicff734982015-08-24 12:58:55 +0000152 static constexpr size_t kNumQuickFprArgs = 4; // 2 arguments passed in FPRs. Floats can be passed
153 // only in even numbered registers and each double
154 // occupies two registers.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800155 static constexpr bool kGprFprLockstep = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000156 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 16; // Offset of first FPR arg.
157 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 32; // Offset of first GPR arg.
158 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 76; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800159 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000160 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800161 }
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800162#elif defined(__mips__) && defined(__LP64__)
163 // The callee save frame is pointed to by SP.
164 // | argN | |
165 // | ... | |
166 // | arg4 | |
167 // | arg3 spill | | Caller's frame
168 // | arg2 spill | |
169 // | arg1 spill | |
170 // | Method* | ---
171 // | RA |
172 // | ... | callee saves
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800173 // | A7 | arg7
174 // | A6 | arg6
175 // | A5 | arg5
176 // | A4 | arg4
177 // | A3 | arg3
178 // | A2 | arg2
179 // | A1 | arg1
Goran Jakovljevicff734982015-08-24 12:58:55 +0000180 // | F19 | f_arg7
181 // | F18 | f_arg6
182 // | F17 | f_arg5
183 // | F16 | f_arg4
184 // | F15 | f_arg3
185 // | F14 | f_arg2
186 // | F13 | f_arg1
187 // | F12 | f_arg0
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800188 // | | padding
189 // | A0/Method* | <- sp
190 // NOTE: for Mip64, when A0 is skipped, F0 is also skipped.
Douglas Leungd18e0832015-02-09 15:22:26 -0800191 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800192 static constexpr bool kAlignPairRegister = false;
193 static constexpr bool kQuickSoftFloatAbi = false;
194 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000195 static constexpr bool kQuickSkipOddFpRegisters = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800196 static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs.
197 static constexpr size_t kNumQuickFprArgs = 7; // 7 arguments passed in FPRs.
198 static constexpr bool kGprFprLockstep = true;
199
200 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 24; // Offset of first FPR arg (F1).
201 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 80; // Offset of first GPR arg (A1).
202 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 200; // Offset of return address.
203 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
204 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
205 }
Ian Rogers848871b2013-08-05 10:56:33 -0700206#elif defined(__i386__)
207 // The callee save frame is pointed to by SP.
208 // | argN | |
209 // | ... | |
210 // | arg4 | |
211 // | arg3 spill | | Caller's frame
212 // | arg2 spill | |
213 // | arg1 spill | |
214 // | Method* | ---
215 // | Return |
216 // | EBP,ESI,EDI | callee saves
217 // | EBX | arg3
218 // | EDX | arg2
219 // | ECX | arg1
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000220 // | XMM3 | float arg 4
221 // | XMM2 | float arg 3
222 // | XMM1 | float arg 2
223 // | XMM0 | float arg 1
Ian Rogers848871b2013-08-05 10:56:33 -0700224 // | EAX/Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500225 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000226 static constexpr bool kAlignPairRegister = false;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000227 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800228 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000229 static constexpr bool kQuickSkipOddFpRegisters = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800230 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs.
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000231 static constexpr size_t kNumQuickFprArgs = 4; // 4 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800232 static constexpr bool kGprFprLockstep = false;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000233 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 4; // Offset of first FPR arg.
234 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 4 + 4*8; // Offset of first GPR arg.
235 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 28 + 4*8; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800236 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000237 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800238 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800239#elif defined(__x86_64__)
Ian Rogers936b37f2014-02-14 00:52:24 -0800240 // The callee save frame is pointed to by SP.
241 // | argN | |
242 // | ... | |
243 // | reg. arg spills | | Caller's frame
244 // | Method* | ---
245 // | Return |
246 // | R15 | callee save
247 // | R14 | callee save
248 // | R13 | callee save
249 // | R12 | callee save
250 // | R9 | arg5
251 // | R8 | arg4
252 // | RSI/R6 | arg1
253 // | RBP/R5 | callee save
254 // | RBX/R3 | callee save
255 // | RDX/R2 | arg2
256 // | RCX/R1 | arg3
257 // | XMM7 | float arg 8
258 // | XMM6 | float arg 7
259 // | XMM5 | float arg 6
260 // | XMM4 | float arg 5
261 // | XMM3 | float arg 4
262 // | XMM2 | float arg 3
263 // | XMM1 | float arg 2
264 // | XMM0 | float arg 1
265 // | Padding |
266 // | RDI/Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500267 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000268 static constexpr bool kAlignPairRegister = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800269 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800270 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000271 static constexpr bool kQuickSkipOddFpRegisters = false;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700272 static constexpr size_t kNumQuickGprArgs = 5; // 5 arguments passed in GPRs.
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700273 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800274 static constexpr bool kGprFprLockstep = false;
Ian Rogers936b37f2014-02-14 00:52:24 -0800275 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 16; // Offset of first FPR arg.
Serguei Katkovc3801912014-07-08 17:21:53 +0700276 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 80 + 4*8; // Offset of first GPR arg.
277 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 168 + 4*8; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800278 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
279 switch (gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000280 case 0: return (4 * GetBytesPerGprSpillLocation(kRuntimeISA));
281 case 1: return (1 * GetBytesPerGprSpillLocation(kRuntimeISA));
282 case 2: return (0 * GetBytesPerGprSpillLocation(kRuntimeISA));
283 case 3: return (5 * GetBytesPerGprSpillLocation(kRuntimeISA));
284 case 4: return (6 * GetBytesPerGprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800285 default:
Andreas Gampec200a4a2014-06-16 18:39:09 -0700286 LOG(FATAL) << "Unexpected GPR index: " << gpr_index;
287 return 0;
Ian Rogers936b37f2014-02-14 00:52:24 -0800288 }
289 }
Ian Rogers848871b2013-08-05 10:56:33 -0700290#else
291#error "Unsupported architecture"
Ian Rogers848871b2013-08-05 10:56:33 -0700292#endif
293
Ian Rogers936b37f2014-02-14 00:52:24 -0800294 public:
Sebastien Hertza836bc92014-11-25 16:30:53 +0100295 // Special handling for proxy methods. Proxy methods are instance methods so the
296 // 'this' object is the 1st argument. They also have the same frame layout as the
297 // kRefAndArgs runtime method. Since 'this' is a reference, it is located in the
298 // 1st GPR.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700299 static mirror::Object* GetProxyThisObject(ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700300 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000301 CHECK((*sp)->IsProxyMethod());
Sebastien Hertza836bc92014-11-25 16:30:53 +0100302 CHECK_GT(kNumQuickGprArgs, 0u);
303 constexpr uint32_t kThisGprIndex = 0u; // 'this' is in the 1st GPR.
304 size_t this_arg_offset = kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset +
305 GprIndexToGprOffset(kThisGprIndex);
306 uint8_t* this_arg_address = reinterpret_cast<uint8_t*>(sp) + this_arg_offset;
307 return reinterpret_cast<StackReference<mirror::Object>*>(this_arg_address)->AsMirrorPtr();
308 }
309
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700310 static ArtMethod* GetCallingMethod(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700311 DCHECK((*sp)->IsCalleeSaveMethod());
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100312 return GetCalleeSaveMethodCaller(sp, Runtime::kSaveRefsAndArgs);
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +0100313 }
314
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700315 static ArtMethod* GetOuterMethod(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700316 DCHECK((*sp)->IsCalleeSaveMethod());
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100317 uint8_t* previous_sp =
318 reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700319 return *reinterpret_cast<ArtMethod**>(previous_sp);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100320 }
321
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700322 static uint32_t GetCallingDexPc(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700323 DCHECK((*sp)->IsCalleeSaveMethod());
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100324 const size_t callee_frame_size = GetCalleeSaveFrameSize(kRuntimeISA, Runtime::kSaveRefsAndArgs);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700325 ArtMethod** caller_sp = reinterpret_cast<ArtMethod**>(
326 reinterpret_cast<uintptr_t>(sp) + callee_frame_size);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100327 uintptr_t outer_pc = QuickArgumentVisitor::GetCallingPc(sp);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100328 const OatQuickMethodHeader* current_code = (*caller_sp)->GetOatQuickMethodHeader(outer_pc);
329 uintptr_t outer_pc_offset = current_code->NativeQuickPcOffset(outer_pc);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100330
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100331 if (current_code->IsOptimized()) {
332 CodeInfo code_info = current_code->GetOptimizedCodeInfo();
David Srbecky09ed0982016-02-12 21:58:43 +0000333 CodeInfoEncoding encoding = code_info.ExtractEncoding();
David Brazdilf677ebf2015-05-29 16:29:43 +0100334 StackMap stack_map = code_info.GetStackMapForNativePcOffset(outer_pc_offset, encoding);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100335 DCHECK(stack_map.IsValid());
David Srbecky09ed0982016-02-12 21:58:43 +0000336 if (stack_map.HasInlineInfo(encoding.stack_map_encoding)) {
David Brazdilf677ebf2015-05-29 16:29:43 +0100337 InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map, encoding);
David Srbecky61b28a12016-02-25 21:55:03 +0000338 return inline_info.GetDexPcAtDepth(encoding.inline_info_encoding,
339 inline_info.GetDepth(encoding.inline_info_encoding)-1);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100340 } else {
David Srbecky09ed0982016-02-12 21:58:43 +0000341 return stack_map.GetDexPc(encoding.stack_map_encoding);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100342 }
343 } else {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100344 return current_code->ToDexPc(*caller_sp, outer_pc);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100345 }
Ian Rogers848871b2013-08-05 10:56:33 -0700346 }
347
Ian Rogers936b37f2014-02-14 00:52:24 -0800348 // For the given quick ref and args quick frame, return the caller's PC.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700349 static uintptr_t GetCallingPc(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700350 DCHECK((*sp)->IsCalleeSaveMethod());
Ian Rogers13735952014-10-08 12:43:28 -0700351 uint8_t* lr = reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_LrOffset;
Ian Rogers848871b2013-08-05 10:56:33 -0700352 return *reinterpret_cast<uintptr_t*>(lr);
353 }
354
Mathieu Chartiere401d142015-04-22 13:56:20 -0700355 QuickArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700356 uint32_t shorty_len) REQUIRES_SHARED(Locks::mutator_lock_) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700357 is_static_(is_static), shorty_(shorty), shorty_len_(shorty_len),
Ian Rogers13735952014-10-08 12:43:28 -0700358 gpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset),
359 fpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset),
360 stack_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize
Mathieu Chartiere401d142015-04-22 13:56:20 -0700361 + sizeof(ArtMethod*)), // Skip ArtMethod*.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800362 gpr_index_(0), fpr_index_(0), fpr_double_index_(0), stack_index_(0),
363 cur_type_(Primitive::kPrimVoid), is_split_long_or_double_(false) {
Andreas Gampe575e78c2014-11-03 23:41:03 -0800364 static_assert(kQuickSoftFloatAbi == (kNumQuickFprArgs == 0),
365 "Number of Quick FPR arguments unexpected");
366 static_assert(!(kQuickSoftFloatAbi && kQuickDoubleRegAlignedFloatBackFilled),
367 "Double alignment unexpected");
Zheng Xu5667fdb2014-10-23 18:29:55 +0800368 // For register alignment, we want to assume that counters(fpr_double_index_) are even if the
369 // next register is even.
Andreas Gampe575e78c2014-11-03 23:41:03 -0800370 static_assert(!kQuickDoubleRegAlignedFloatBackFilled || kNumQuickFprArgs % 2 == 0,
371 "Number of Quick FPR arguments not even");
Andreas Gampe542451c2016-07-26 09:02:02 -0700372 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Zheng Xu5667fdb2014-10-23 18:29:55 +0800373 }
Ian Rogers848871b2013-08-05 10:56:33 -0700374
375 virtual ~QuickArgumentVisitor() {}
376
377 virtual void Visit() = 0;
378
Ian Rogers936b37f2014-02-14 00:52:24 -0800379 Primitive::Type GetParamPrimitiveType() const {
380 return cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700381 }
382
Ian Rogers13735952014-10-08 12:43:28 -0700383 uint8_t* GetParamAddress() const {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800384 if (!kQuickSoftFloatAbi) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800385 Primitive::Type type = GetParamPrimitiveType();
386 if (UNLIKELY((type == Primitive::kPrimDouble) || (type == Primitive::kPrimFloat))) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800387 if (type == Primitive::kPrimDouble && kQuickDoubleRegAlignedFloatBackFilled) {
388 if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) {
389 return fpr_args_ + (fpr_double_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
390 }
391 } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000392 return fpr_args_ + (fpr_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800393 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700394 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers936b37f2014-02-14 00:52:24 -0800395 }
396 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800397 if (gpr_index_ < kNumQuickGprArgs) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800398 return gpr_args_ + GprIndexToGprOffset(gpr_index_);
399 }
400 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700401 }
402
403 bool IsSplitLongOrDouble() const {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700404 if ((GetBytesPerGprSpillLocation(kRuntimeISA) == 4) ||
405 (GetBytesPerFprSpillLocation(kRuntimeISA) == 4)) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800406 return is_split_long_or_double_;
407 } else {
408 return false; // An optimization for when GPR and FPRs are 64bit.
409 }
Ian Rogers848871b2013-08-05 10:56:33 -0700410 }
411
Ian Rogers936b37f2014-02-14 00:52:24 -0800412 bool IsParamAReference() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700413 return GetParamPrimitiveType() == Primitive::kPrimNot;
414 }
415
Ian Rogers936b37f2014-02-14 00:52:24 -0800416 bool IsParamALongOrDouble() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700417 Primitive::Type type = GetParamPrimitiveType();
418 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
419 }
420
421 uint64_t ReadSplitLongParam() const {
Nicolas Geoffray425f2392015-01-08 14:52:29 +0000422 // The splitted long is always available through the stack.
423 return *reinterpret_cast<uint64_t*>(stack_args_
424 + stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700425 }
426
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800427 void IncGprIndex() {
428 gpr_index_++;
429 if (kGprFprLockstep) {
430 fpr_index_++;
431 }
432 }
433
434 void IncFprIndex() {
435 fpr_index_++;
436 if (kGprFprLockstep) {
437 gpr_index_++;
438 }
439 }
440
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700441 void VisitArguments() REQUIRES_SHARED(Locks::mutator_lock_) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800442 // (a) 'stack_args_' should point to the first method's argument
443 // (b) whatever the argument type it is, the 'stack_index_' should
444 // be moved forward along with every visiting.
Ian Rogers936b37f2014-02-14 00:52:24 -0800445 gpr_index_ = 0;
446 fpr_index_ = 0;
Zheng Xu5667fdb2014-10-23 18:29:55 +0800447 if (kQuickDoubleRegAlignedFloatBackFilled) {
448 fpr_double_index_ = 0;
449 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800450 stack_index_ = 0;
451 if (!is_static_) { // Handle this.
452 cur_type_ = Primitive::kPrimNot;
453 is_split_long_or_double_ = false;
Ian Rogers848871b2013-08-05 10:56:33 -0700454 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800455 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800456 if (kNumQuickGprArgs > 0) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800457 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800458 }
Ian Rogers848871b2013-08-05 10:56:33 -0700459 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800460 for (uint32_t shorty_index = 1; shorty_index < shorty_len_; ++shorty_index) {
461 cur_type_ = Primitive::GetType(shorty_[shorty_index]);
462 switch (cur_type_) {
463 case Primitive::kPrimNot:
464 case Primitive::kPrimBoolean:
465 case Primitive::kPrimByte:
466 case Primitive::kPrimChar:
467 case Primitive::kPrimShort:
468 case Primitive::kPrimInt:
469 is_split_long_or_double_ = false;
470 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800471 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800472 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800473 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800474 }
475 break;
476 case Primitive::kPrimFloat:
477 is_split_long_or_double_ = false;
478 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800479 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800480 if (kQuickSoftFloatAbi) {
481 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800482 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800483 }
484 } else {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800485 if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800486 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800487 if (kQuickDoubleRegAlignedFloatBackFilled) {
488 // Double should not overlap with float.
489 // For example, if fpr_index_ = 3, fpr_double_index_ should be at least 4.
490 fpr_double_index_ = std::max(fpr_double_index_, RoundUp(fpr_index_, 2));
491 // Float should not overlap with double.
492 if (fpr_index_ % 2 == 0) {
493 fpr_index_ = std::max(fpr_double_index_, fpr_index_);
494 }
Goran Jakovljevicff734982015-08-24 12:58:55 +0000495 } else if (kQuickSkipOddFpRegisters) {
496 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800497 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800498 }
499 }
500 break;
501 case Primitive::kPrimDouble:
502 case Primitive::kPrimLong:
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800503 if (kQuickSoftFloatAbi || (cur_type_ == Primitive::kPrimLong)) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000504 if (cur_type_ == Primitive::kPrimLong && kAlignPairRegister && gpr_index_ == 0) {
Goran Jakovljevicff734982015-08-24 12:58:55 +0000505 // Currently, this is only for ARM and MIPS, where the first available parameter
506 // register is R1 (on ARM) or A1 (on MIPS). So we skip it, and use R2 (on ARM) or
507 // A2 (on MIPS) instead.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800508 IncGprIndex();
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000509 }
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000510 is_split_long_or_double_ = (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) &&
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800511 ((gpr_index_ + 1) == kNumQuickGprArgs);
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500512 if (!kSplitPairAcrossRegisterAndStack && is_split_long_or_double_) {
513 // We don't want to split this. Pass over this register.
514 gpr_index_++;
515 is_split_long_or_double_ = false;
516 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800517 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800518 if (kBytesStackArgLocation == 4) {
519 stack_index_+= 2;
520 } else {
521 CHECK_EQ(kBytesStackArgLocation, 8U);
522 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800523 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700524 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800525 IncGprIndex();
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000526 if (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700527 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800528 IncGprIndex();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700529 }
530 }
531 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800532 } else {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000533 is_split_long_or_double_ = (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) &&
Zheng Xu5667fdb2014-10-23 18:29:55 +0800534 ((fpr_index_ + 1) == kNumQuickFprArgs) && !kQuickDoubleRegAlignedFloatBackFilled;
Ian Rogers936b37f2014-02-14 00:52:24 -0800535 Visit();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700536 if (kBytesStackArgLocation == 4) {
537 stack_index_+= 2;
Ian Rogers936b37f2014-02-14 00:52:24 -0800538 } else {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700539 CHECK_EQ(kBytesStackArgLocation, 8U);
540 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800541 }
Zheng Xu5667fdb2014-10-23 18:29:55 +0800542 if (kQuickDoubleRegAlignedFloatBackFilled) {
543 if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) {
544 fpr_double_index_ += 2;
545 // Float should not overlap with double.
546 if (fpr_index_ % 2 == 0) {
547 fpr_index_ = std::max(fpr_double_index_, fpr_index_);
548 }
549 }
550 } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800551 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800552 if (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) {
553 if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800554 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800555 }
556 }
557 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800558 }
559 break;
560 default:
561 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty_;
562 }
Ian Rogers848871b2013-08-05 10:56:33 -0700563 }
564 }
565
Andreas Gampec200a4a2014-06-16 18:39:09 -0700566 protected:
Ian Rogers848871b2013-08-05 10:56:33 -0700567 const bool is_static_;
568 const char* const shorty_;
569 const uint32_t shorty_len_;
Andreas Gampec200a4a2014-06-16 18:39:09 -0700570
571 private:
Ian Rogers13735952014-10-08 12:43:28 -0700572 uint8_t* const gpr_args_; // Address of GPR arguments in callee save frame.
573 uint8_t* const fpr_args_; // Address of FPR arguments in callee save frame.
574 uint8_t* const stack_args_; // Address of stack arguments in caller's frame.
Ian Rogers936b37f2014-02-14 00:52:24 -0800575 uint32_t gpr_index_; // Index into spilled GPRs.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800576 // Index into spilled FPRs.
577 // In case kQuickDoubleRegAlignedFloatBackFilled, it may index a hole while fpr_double_index_
578 // holds a higher register number.
579 uint32_t fpr_index_;
580 // Index into spilled FPRs for aligned double.
581 // Only used when kQuickDoubleRegAlignedFloatBackFilled. Next available double register indexed in
582 // terms of singles, may be behind fpr_index.
583 uint32_t fpr_double_index_;
Ian Rogers936b37f2014-02-14 00:52:24 -0800584 uint32_t stack_index_; // Index into arguments on the stack.
585 // The current type of argument during VisitArguments.
586 Primitive::Type cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700587 // Does a 64bit parameter straddle the register and stack arguments?
588 bool is_split_long_or_double_;
589};
590
Sebastien Hertza836bc92014-11-25 16:30:53 +0100591// Returns the 'this' object of a proxy method. This function is only used by StackVisitor. It
592// allows to use the QuickArgumentVisitor constants without moving all the code in its own module.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700593extern "C" mirror::Object* artQuickGetProxyThisObject(ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700594 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertza836bc92014-11-25 16:30:53 +0100595 return QuickArgumentVisitor::GetProxyThisObject(sp);
596}
597
Ian Rogers848871b2013-08-05 10:56:33 -0700598// Visits arguments on the stack placing them into the shadow frame.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800599class BuildQuickShadowFrameVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700600 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700601 BuildQuickShadowFrameVisitor(ArtMethod** sp, bool is_static, const char* shorty,
602 uint32_t shorty_len, ShadowFrame* sf, size_t first_arg_reg) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700603 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), sf_(sf), cur_reg_(first_arg_reg) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700604
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700605 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700606
607 private:
Ian Rogers936b37f2014-02-14 00:52:24 -0800608 ShadowFrame* const sf_;
609 uint32_t cur_reg_;
Ian Rogers848871b2013-08-05 10:56:33 -0700610
Dragos Sbirleabd136a22013-08-13 18:07:04 -0700611 DISALLOW_COPY_AND_ASSIGN(BuildQuickShadowFrameVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700612};
613
Andreas Gampec200a4a2014-06-16 18:39:09 -0700614void BuildQuickShadowFrameVisitor::Visit() {
Ian Rogers9758f792014-03-13 09:02:55 -0700615 Primitive::Type type = GetParamPrimitiveType();
616 switch (type) {
617 case Primitive::kPrimLong: // Fall-through.
618 case Primitive::kPrimDouble:
619 if (IsSplitLongOrDouble()) {
620 sf_->SetVRegLong(cur_reg_, ReadSplitLongParam());
621 } else {
622 sf_->SetVRegLong(cur_reg_, *reinterpret_cast<jlong*>(GetParamAddress()));
623 }
624 ++cur_reg_;
625 break;
626 case Primitive::kPrimNot: {
627 StackReference<mirror::Object>* stack_ref =
628 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
629 sf_->SetVRegReference(cur_reg_, stack_ref->AsMirrorPtr());
630 }
631 break;
632 case Primitive::kPrimBoolean: // Fall-through.
633 case Primitive::kPrimByte: // Fall-through.
634 case Primitive::kPrimChar: // Fall-through.
635 case Primitive::kPrimShort: // Fall-through.
636 case Primitive::kPrimInt: // Fall-through.
637 case Primitive::kPrimFloat:
638 sf_->SetVReg(cur_reg_, *reinterpret_cast<jint*>(GetParamAddress()));
639 break;
640 case Primitive::kPrimVoid:
641 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700642 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -0700643 }
644 ++cur_reg_;
645}
646
Mathieu Chartiere401d142015-04-22 13:56:20 -0700647extern "C" uint64_t artQuickToInterpreterBridge(ArtMethod* method, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700648 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers848871b2013-08-05 10:56:33 -0700649 // Ensure we don't get thread suspension until the object arguments are safely in the shadow
650 // frame.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700651 ScopedQuickEntrypointChecks sqec(self);
Ian Rogers848871b2013-08-05 10:56:33 -0700652
Alex Light9139e002015-10-09 15:59:48 -0700653 if (UNLIKELY(!method->IsInvokable())) {
654 method->ThrowInvocationTimeError();
Ian Rogers848871b2013-08-05 10:56:33 -0700655 return 0;
Andreas Gampe639bdd12015-06-03 11:22:45 -0700656 }
657
658 JValue tmp_value;
659 ShadowFrame* deopt_frame = self->PopStackedShadowFrame(
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700660 StackedShadowFrameType::kDeoptimizationShadowFrame, false);
Andreas Gampe639bdd12015-06-03 11:22:45 -0700661 ManagedStack fragment;
662
David Sehr709b0702016-10-13 09:12:37 -0700663 DCHECK(!method->IsNative()) << method->PrettyMethod();
Andreas Gampe639bdd12015-06-03 11:22:45 -0700664 uint32_t shorty_len = 0;
Andreas Gampe542451c2016-07-26 09:02:02 -0700665 ArtMethod* non_proxy_method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Alex Lighte9dd04f2016-03-16 16:09:45 -0700666 const DexFile::CodeItem* code_item = non_proxy_method->GetCodeItem();
David Sehr709b0702016-10-13 09:12:37 -0700667 DCHECK(code_item != nullptr) << method->PrettyMethod();
Andreas Gampe639bdd12015-06-03 11:22:45 -0700668 const char* shorty = non_proxy_method->GetShorty(&shorty_len);
669
670 JValue result;
671
672 if (deopt_frame != nullptr) {
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700673 // Coming from partial-fragment deopt.
Andreas Gampe639bdd12015-06-03 11:22:45 -0700674
675 if (kIsDebugBuild) {
676 // Sanity-check: are the methods as expected? We check that the last shadow frame (the bottom
677 // of the call-stack) corresponds to the called method.
678 ShadowFrame* linked = deopt_frame;
679 while (linked->GetLink() != nullptr) {
680 linked = linked->GetLink();
681 }
David Sehr709b0702016-10-13 09:12:37 -0700682 CHECK_EQ(method, linked->GetMethod()) << method->PrettyMethod() << " "
683 << ArtMethod::PrettyMethod(linked->GetMethod());
Andreas Gampe639bdd12015-06-03 11:22:45 -0700684 }
685
686 if (VLOG_IS_ON(deopt)) {
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700687 // Print out the stack to verify that it was a partial-fragment deopt.
Andreas Gampe639bdd12015-06-03 11:22:45 -0700688 LOG(INFO) << "Continue-ing from deopt. Stack is:";
689 QuickExceptionHandler::DumpFramesWithType(self, true);
690 }
691
692 mirror::Throwable* pending_exception = nullptr;
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100693 bool from_code = false;
694 self->PopDeoptimizationContext(&result, &pending_exception, /* out */ &from_code);
Andreas Gampe639bdd12015-06-03 11:22:45 -0700695
696 // Push a transition back into managed code onto the linked list in thread.
697 self->PushManagedStackFragment(&fragment);
698
699 // Ensure that the stack is still in order.
700 if (kIsDebugBuild) {
701 class DummyStackVisitor : public StackVisitor {
702 public:
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700703 explicit DummyStackVisitor(Thread* self_in) REQUIRES_SHARED(Locks::mutator_lock_)
Andreas Gampe639bdd12015-06-03 11:22:45 -0700704 : StackVisitor(self_in, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {}
705
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700706 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe639bdd12015-06-03 11:22:45 -0700707 // Nothing to do here. In a debug build, SanityCheckFrame will do the work in the walking
708 // logic. Just always say we want to continue.
709 return true;
710 }
711 };
712 DummyStackVisitor dsv(self);
713 dsv.WalkStack();
714 }
715
716 // Restore the exception that was pending before deoptimization then interpret the
717 // deoptimized frames.
718 if (pending_exception != nullptr) {
719 self->SetException(pending_exception);
720 }
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100721 interpreter::EnterInterpreterFromDeoptimize(self, deopt_frame, from_code, &result);
Ian Rogers848871b2013-08-05 10:56:33 -0700722 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -0700723 const char* old_cause = self->StartAssertNoThreadSuspension(
724 "Building interpreter shadow frame");
Ian Rogers848871b2013-08-05 10:56:33 -0700725 uint16_t num_regs = code_item->registers_size_;
Andreas Gampec200a4a2014-06-16 18:39:09 -0700726 // No last shadow coming from quick.
Andreas Gampeb3025922015-09-01 14:45:00 -0700727 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
Andreas Gampe03ec9302015-08-27 17:41:47 -0700728 CREATE_SHADOW_FRAME(num_regs, /* link */ nullptr, method, /* dex pc */ 0);
Andreas Gampeb3025922015-09-01 14:45:00 -0700729 ShadowFrame* shadow_frame = shadow_frame_unique_ptr.get();
Ian Rogers848871b2013-08-05 10:56:33 -0700730 size_t first_arg_reg = code_item->registers_size_ - code_item->ins_size_;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700731 BuildQuickShadowFrameVisitor shadow_frame_builder(sp, method->IsStatic(), shorty, shorty_len,
Ian Rogers936b37f2014-02-14 00:52:24 -0800732 shadow_frame, first_arg_reg);
Ian Rogers848871b2013-08-05 10:56:33 -0700733 shadow_frame_builder.VisitArguments();
Ian Rogerse94652f2014-12-02 11:13:19 -0800734 const bool needs_initialization =
735 method->IsStatic() && !method->GetDeclaringClass()->IsInitialized();
Ian Rogers848871b2013-08-05 10:56:33 -0700736 // Push a transition back into managed code onto the linked list in thread.
Ian Rogers848871b2013-08-05 10:56:33 -0700737 self->PushManagedStackFragment(&fragment);
738 self->PushShadowFrame(shadow_frame);
739 self->EndAssertNoThreadSuspension(old_cause);
740
Ian Rogerse94652f2014-12-02 11:13:19 -0800741 if (needs_initialization) {
Ian Rogers848871b2013-08-05 10:56:33 -0700742 // Ensure static method's class is initialized.
Ian Rogerse94652f2014-12-02 11:13:19 -0800743 StackHandleScope<1> hs(self);
744 Handle<mirror::Class> h_class(hs.NewHandle(shadow_frame->GetMethod()->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700745 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
David Sehr709b0702016-10-13 09:12:37 -0700746 DCHECK(Thread::Current()->IsExceptionPending())
747 << shadow_frame->GetMethod()->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700748 self->PopManagedStackFragment(fragment);
749 return 0;
750 }
751 }
Daniel Mihalyieb076692014-08-22 17:33:31 +0200752
Andreas Gampe639bdd12015-06-03 11:22:45 -0700753 result = interpreter::EnterInterpreterFromEntryPoint(self, code_item, shadow_frame);
Ian Rogers848871b2013-08-05 10:56:33 -0700754 }
Andreas Gampe639bdd12015-06-03 11:22:45 -0700755
756 // Pop transition.
757 self->PopManagedStackFragment(fragment);
758
759 // Request a stack deoptimization if needed
760 ArtMethod* caller = QuickArgumentVisitor::GetCallingMethod(sp);
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700761 uintptr_t caller_pc = QuickArgumentVisitor::GetCallingPc(sp);
Mingyao Yanga3549d22016-06-02 17:01:02 -0700762 // If caller_pc is the instrumentation exit stub, the stub will check to see if deoptimization
763 // should be done and it knows the real return pc.
764 if (UNLIKELY(caller_pc != reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) &&
765 Dbg::IsForcedInterpreterNeededForUpcall(self, caller) &&
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700766 Runtime::Current()->IsDeoptimizeable(caller_pc))) {
Andreas Gampe639bdd12015-06-03 11:22:45 -0700767 // Push the context of the deoptimization stack so we can restore the return value and the
768 // exception before executing the deoptimized frames.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100769 self->PushDeoptimizationContext(
770 result, shorty[0] == 'L', /* from_code */ false, self->GetException());
Andreas Gampe639bdd12015-06-03 11:22:45 -0700771
772 // Set special exception to cause deoptimization.
773 self->SetException(Thread::GetDeoptimizationException());
774 }
775
776 // No need to restore the args since the method has already been run by the interpreter.
777 return result.GetJ();
Ian Rogers848871b2013-08-05 10:56:33 -0700778}
779
780// Visits arguments on the stack placing them into the args vector, Object* arguments are converted
781// to jobjects.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800782class BuildQuickArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700783 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700784 BuildQuickArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty, uint32_t shorty_len,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700785 ScopedObjectAccessUnchecked* soa, std::vector<jvalue>* args) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700786 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa), args_(args) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700787
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700788 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700789
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700790 void FixupReferences() REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800791
Ian Rogers848871b2013-08-05 10:56:33 -0700792 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700793 ScopedObjectAccessUnchecked* const soa_;
794 std::vector<jvalue>* const args_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800795 // References which we must update when exiting in case the GC moved the objects.
Ian Rogers700a4022014-05-19 16:49:03 -0700796 std::vector<std::pair<jobject, StackReference<mirror::Object>*>> references_;
Ian Rogers9758f792014-03-13 09:02:55 -0700797
Ian Rogers848871b2013-08-05 10:56:33 -0700798 DISALLOW_COPY_AND_ASSIGN(BuildQuickArgumentVisitor);
799};
800
Ian Rogers9758f792014-03-13 09:02:55 -0700801void BuildQuickArgumentVisitor::Visit() {
802 jvalue val;
803 Primitive::Type type = GetParamPrimitiveType();
804 switch (type) {
805 case Primitive::kPrimNot: {
806 StackReference<mirror::Object>* stack_ref =
807 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
808 val.l = soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
809 references_.push_back(std::make_pair(val.l, stack_ref));
810 break;
811 }
812 case Primitive::kPrimLong: // Fall-through.
813 case Primitive::kPrimDouble:
814 if (IsSplitLongOrDouble()) {
815 val.j = ReadSplitLongParam();
816 } else {
817 val.j = *reinterpret_cast<jlong*>(GetParamAddress());
818 }
819 break;
820 case Primitive::kPrimBoolean: // Fall-through.
821 case Primitive::kPrimByte: // Fall-through.
822 case Primitive::kPrimChar: // Fall-through.
823 case Primitive::kPrimShort: // Fall-through.
824 case Primitive::kPrimInt: // Fall-through.
825 case Primitive::kPrimFloat:
826 val.i = *reinterpret_cast<jint*>(GetParamAddress());
827 break;
828 case Primitive::kPrimVoid:
829 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700830 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -0700831 }
832 args_->push_back(val);
833}
834
835void BuildQuickArgumentVisitor::FixupReferences() {
836 // Fixup any references which may have changed.
837 for (const auto& pair : references_) {
Mathieu Chartier1a5337f2016-10-13 13:48:23 -0700838 pair.second->Assign(soa_->Decode<mirror::Object>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -0700839 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -0700840 }
841}
842
Ian Rogers848871b2013-08-05 10:56:33 -0700843// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
844// which is responsible for recording callee save registers. We explicitly place into jobjects the
845// incoming reference arguments (so they survive GC). We invoke the invocation handler, which is a
846// field within the proxy object, which will box the primitive arguments and deal with error cases.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700847extern "C" uint64_t artQuickProxyInvokeHandler(
848 ArtMethod* proxy_method, mirror::Object* receiver, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700849 REQUIRES_SHARED(Locks::mutator_lock_) {
David Sehr709b0702016-10-13 09:12:37 -0700850 DCHECK(proxy_method->IsProxyMethod()) << proxy_method->PrettyMethod();
851 DCHECK(receiver->GetClass()->IsProxyClass()) << proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700852 // Ensure we don't get thread suspension until the object arguments are safely in jobjects.
853 const char* old_cause =
854 self->StartAssertNoThreadSuspension("Adding to IRT proxy object arguments");
855 // Register the top of the managed stack, making stack crawlable.
David Sehr709b0702016-10-13 09:12:37 -0700856 DCHECK_EQ((*sp), proxy_method) << proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700857 self->VerifyStack();
858 // Start new JNI local reference state.
859 JNIEnvExt* env = self->GetJniEnv();
860 ScopedObjectAccessUnchecked soa(env);
861 ScopedJniEnvLocalRefState env_state(env);
862 // Create local ref. copies of proxy method and the receiver.
863 jobject rcvr_jobj = soa.AddLocalReference<jobject>(receiver);
864
865 // Placing arguments into args vector and remove the receiver.
Andreas Gampe542451c2016-07-26 09:02:02 -0700866 ArtMethod* non_proxy_method = proxy_method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
David Sehr709b0702016-10-13 09:12:37 -0700867 CHECK(!non_proxy_method->IsStatic()) << proxy_method->PrettyMethod() << " "
868 << non_proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700869 std::vector<jvalue> args;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700870 uint32_t shorty_len = 0;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700871 const char* shorty = non_proxy_method->GetShorty(&shorty_len);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700872 BuildQuickArgumentVisitor local_ref_visitor(sp, false, shorty, shorty_len, &soa, &args);
Brian Carlstromd3633d52013-08-20 21:06:26 -0700873
Ian Rogers848871b2013-08-05 10:56:33 -0700874 local_ref_visitor.VisitArguments();
David Sehr709b0702016-10-13 09:12:37 -0700875 DCHECK_GT(args.size(), 0U) << proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700876 args.erase(args.begin());
877
878 // Convert proxy method into expected interface method.
Andreas Gampe542451c2016-07-26 09:02:02 -0700879 ArtMethod* interface_method = proxy_method->FindOverriddenMethod(kRuntimePointerSize);
David Sehr709b0702016-10-13 09:12:37 -0700880 DCHECK(interface_method != nullptr) << proxy_method->PrettyMethod();
881 DCHECK(!interface_method->IsProxyMethod()) << interface_method->PrettyMethod();
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700882 self->EndAssertNoThreadSuspension(old_cause);
Andreas Gampe542451c2016-07-26 09:02:02 -0700883 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Andreas Gampee01e3642016-07-25 13:06:04 -0700884 DCHECK(!Runtime::Current()->IsActiveTransaction());
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700885 jobject interface_method_jobj = soa.AddLocalReference<jobject>(
Andreas Gampe542451c2016-07-26 09:02:02 -0700886 mirror::Method::CreateFromArtMethod<kRuntimePointerSize, false>(soa.Self(),
887 interface_method));
Ian Rogers848871b2013-08-05 10:56:33 -0700888
889 // All naked Object*s should now be in jobjects, so its safe to go into the main invoke code
890 // that performs allocations.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700891 JValue result = InvokeProxyInvocationHandler(soa, shorty, rcvr_jobj, interface_method_jobj, args);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800892 // Restore references which might have moved.
893 local_ref_visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -0700894 return result.GetJ();
895}
896
897// Read object references held in arguments from quick frames and place in a JNI local references,
898// so they don't get garbage collected.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800899class RememberForGcArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700900 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700901 RememberForGcArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty,
902 uint32_t shorty_len, ScopedObjectAccessUnchecked* soa) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700903 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700904
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700905 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Mathieu Chartier07d447b2013-09-26 11:57:43 -0700906
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700907 void FixupReferences() REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers848871b2013-08-05 10:56:33 -0700908
909 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700910 ScopedObjectAccessUnchecked* const soa_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800911 // References which we must update when exiting in case the GC moved the objects.
Andreas Gampec200a4a2014-06-16 18:39:09 -0700912 std::vector<std::pair<jobject, StackReference<mirror::Object>*> > references_;
913
Mathieu Chartier590fee92013-09-13 13:46:47 -0700914 DISALLOW_COPY_AND_ASSIGN(RememberForGcArgumentVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700915};
916
Ian Rogers9758f792014-03-13 09:02:55 -0700917void RememberForGcArgumentVisitor::Visit() {
918 if (IsParamAReference()) {
919 StackReference<mirror::Object>* stack_ref =
920 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
921 jobject reference =
922 soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
923 references_.push_back(std::make_pair(reference, stack_ref));
924 }
925}
926
927void RememberForGcArgumentVisitor::FixupReferences() {
928 // Fixup any references which may have changed.
929 for (const auto& pair : references_) {
Mathieu Chartier1a5337f2016-10-13 13:48:23 -0700930 pair.second->Assign(soa_->Decode<mirror::Object>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -0700931 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -0700932 }
933}
934
Ian Rogers848871b2013-08-05 10:56:33 -0700935// Lazily resolve a method for quick. Called by stub code.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700936extern "C" const void* artQuickResolutionTrampoline(
937 ArtMethod* called, mirror::Object* receiver, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700938 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe3b45ef22015-05-26 21:34:09 -0700939 // The resolution trampoline stashes the resolved method into the callee-save frame to transport
940 // it. Thus, when exiting, the stack cannot be verified (as the resolved method most likely
941 // does not have the same stack layout as the callee-save method).
942 ScopedQuickEntrypointChecks sqec(self, kIsDebugBuild, false);
Ian Rogers848871b2013-08-05 10:56:33 -0700943 // Start new JNI local reference state
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800944 JNIEnvExt* env = self->GetJniEnv();
Ian Rogers848871b2013-08-05 10:56:33 -0700945 ScopedObjectAccessUnchecked soa(env);
946 ScopedJniEnvLocalRefState env_state(env);
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800947 const char* old_cause = self->StartAssertNoThreadSuspension("Quick method resolution set up");
Ian Rogers848871b2013-08-05 10:56:33 -0700948
949 // Compute details about the called method (avoid GCs)
950 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Ian Rogers848871b2013-08-05 10:56:33 -0700951 InvokeType invoke_type;
Ian Rogerse0a02da2014-12-02 14:10:53 -0800952 MethodReference called_method(nullptr, 0);
953 const bool called_method_known_on_entry = !called->IsRuntimeMethod();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700954 ArtMethod* caller = nullptr;
Ian Rogerse0a02da2014-12-02 14:10:53 -0800955 if (!called_method_known_on_entry) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +0100956 caller = QuickArgumentVisitor::GetCallingMethod(sp);
957 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
Ian Rogers848871b2013-08-05 10:56:33 -0700958 const DexFile::CodeItem* code;
Ian Rogerse0a02da2014-12-02 14:10:53 -0800959 called_method.dex_file = caller->GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700960 code = caller->GetCodeItem();
Ian Rogers848871b2013-08-05 10:56:33 -0700961 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
962 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
963 Instruction::Code instr_code = instr->Opcode();
964 bool is_range;
965 switch (instr_code) {
966 case Instruction::INVOKE_DIRECT:
967 invoke_type = kDirect;
968 is_range = false;
969 break;
970 case Instruction::INVOKE_DIRECT_RANGE:
971 invoke_type = kDirect;
972 is_range = true;
973 break;
974 case Instruction::INVOKE_STATIC:
975 invoke_type = kStatic;
976 is_range = false;
977 break;
978 case Instruction::INVOKE_STATIC_RANGE:
979 invoke_type = kStatic;
980 is_range = true;
981 break;
982 case Instruction::INVOKE_SUPER:
983 invoke_type = kSuper;
984 is_range = false;
985 break;
986 case Instruction::INVOKE_SUPER_RANGE:
987 invoke_type = kSuper;
988 is_range = true;
989 break;
990 case Instruction::INVOKE_VIRTUAL:
991 invoke_type = kVirtual;
992 is_range = false;
993 break;
994 case Instruction::INVOKE_VIRTUAL_RANGE:
995 invoke_type = kVirtual;
996 is_range = true;
997 break;
998 case Instruction::INVOKE_INTERFACE:
999 invoke_type = kInterface;
1000 is_range = false;
1001 break;
1002 case Instruction::INVOKE_INTERFACE_RANGE:
1003 invoke_type = kInterface;
1004 is_range = true;
1005 break;
1006 default:
Ian Rogerse0a02da2014-12-02 14:10:53 -08001007 LOG(FATAL) << "Unexpected call into trampoline: " << instr->DumpString(nullptr);
1008 UNREACHABLE();
Ian Rogers848871b2013-08-05 10:56:33 -07001009 }
Ian Rogerse0a02da2014-12-02 14:10:53 -08001010 called_method.dex_method_index = (is_range) ? instr->VRegB_3rc() : instr->VRegB_35c();
Ian Rogers848871b2013-08-05 10:56:33 -07001011 } else {
1012 invoke_type = kStatic;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001013 called_method.dex_file = called->GetDexFile();
1014 called_method.dex_method_index = called->GetDexMethodIndex();
Ian Rogers848871b2013-08-05 10:56:33 -07001015 }
1016 uint32_t shorty_len;
1017 const char* shorty =
Ian Rogerse0a02da2014-12-02 14:10:53 -08001018 called_method.dex_file->GetMethodShorty(
1019 called_method.dex_file->GetMethodId(called_method.dex_method_index), &shorty_len);
Mathieu Chartier590fee92013-09-13 13:46:47 -07001020 RememberForGcArgumentVisitor visitor(sp, invoke_type == kStatic, shorty, shorty_len, &soa);
Ian Rogers848871b2013-08-05 10:56:33 -07001021 visitor.VisitArguments();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001022 self->EndAssertNoThreadSuspension(old_cause);
Ian Rogerse0a02da2014-12-02 14:10:53 -08001023 const bool virtual_or_interface = invoke_type == kVirtual || invoke_type == kInterface;
Ian Rogers848871b2013-08-05 10:56:33 -07001024 // Resolve method filling in dex cache.
Ian Rogerse0a02da2014-12-02 14:10:53 -08001025 if (!called_method_known_on_entry) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001026 StackHandleScope<1> hs(self);
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001027 mirror::Object* dummy = nullptr;
1028 HandleWrapper<mirror::Object> h_receiver(
1029 hs.NewHandleWrapper(virtual_or_interface ? &receiver : &dummy));
Ian Rogerse0a02da2014-12-02 14:10:53 -08001030 DCHECK_EQ(caller->GetDexFile(), called_method.dex_file);
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08001031 called = linker->ResolveMethod<ClassLinker::kForceICCECheck>(
1032 self, called_method.dex_method_index, caller, invoke_type);
Ian Rogers848871b2013-08-05 10:56:33 -07001033 }
Ian Rogerse0a02da2014-12-02 14:10:53 -08001034 const void* code = nullptr;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001035 if (LIKELY(!self->IsExceptionPending())) {
Ian Rogers848871b2013-08-05 10:56:33 -07001036 // Incompatible class change should have been handled in resolve method.
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001037 CHECK(!called->CheckIncompatibleClassChange(invoke_type))
David Sehr709b0702016-10-13 09:12:37 -07001038 << called->PrettyMethod() << " " << invoke_type;
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001039 if (virtual_or_interface || invoke_type == kSuper) {
1040 // Refine called method based on receiver for kVirtual/kInterface, and
1041 // caller for kSuper.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001042 ArtMethod* orig_called = called;
Mathieu Chartier55871bf2014-02-27 10:24:50 -08001043 if (invoke_type == kVirtual) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001044 CHECK(receiver != nullptr) << invoke_type;
Andreas Gampe542451c2016-07-26 09:02:02 -07001045 called = receiver->GetClass()->FindVirtualMethodForVirtual(called, kRuntimePointerSize);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001046 } else if (invoke_type == kInterface) {
1047 CHECK(receiver != nullptr) << invoke_type;
Andreas Gampe542451c2016-07-26 09:02:02 -07001048 called = receiver->GetClass()->FindVirtualMethodForInterface(called, kRuntimePointerSize);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001049 } else {
1050 DCHECK_EQ(invoke_type, kSuper);
1051 CHECK(caller != nullptr) << invoke_type;
Nicolas Geoffray393fdb82016-04-25 14:58:06 +01001052 StackHandleScope<2> hs(self);
1053 Handle<mirror::DexCache> dex_cache(
1054 hs.NewHandle(caller->GetDeclaringClass()->GetDexCache()));
1055 Handle<mirror::ClassLoader> class_loader(
1056 hs.NewHandle(caller->GetDeclaringClass()->GetClassLoader()));
Alex Lightfedd91d2016-01-07 14:49:16 -08001057 // TODO Maybe put this into a mirror::Class function.
1058 mirror::Class* ref_class = linker->ResolveReferencedClassOfMethod(
Nicolas Geoffray393fdb82016-04-25 14:58:06 +01001059 called_method.dex_method_index, dex_cache, class_loader);
Alex Lightfedd91d2016-01-07 14:49:16 -08001060 if (ref_class->IsInterface()) {
Andreas Gampe542451c2016-07-26 09:02:02 -07001061 called = ref_class->FindVirtualMethodForInterfaceSuper(called, kRuntimePointerSize);
Alex Lightfedd91d2016-01-07 14:49:16 -08001062 } else {
1063 called = caller->GetDeclaringClass()->GetSuperClass()->GetVTableEntry(
Andreas Gampe542451c2016-07-26 09:02:02 -07001064 called->GetMethodIndex(), kRuntimePointerSize);
Alex Lightfedd91d2016-01-07 14:49:16 -08001065 }
Mathieu Chartier55871bf2014-02-27 10:24:50 -08001066 }
Mingyao Yangf4867782014-05-05 11:55:02 -07001067
David Sehr709b0702016-10-13 09:12:37 -07001068 CHECK(called != nullptr) << orig_called->PrettyMethod() << " "
1069 << mirror::Object::PrettyTypeOf(receiver) << " "
Mingyao Yangf4867782014-05-05 11:55:02 -07001070 << invoke_type << " " << orig_called->GetVtableIndex();
1071
Ian Rogers83883d72013-10-21 21:07:24 -07001072 // 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 -08001073 // of the sharpened method avoiding dirtying the dex cache if possible.
Ian Rogers00f15272014-12-02 16:55:46 -08001074 // Note, called_method.dex_method_index references the dex method before the
1075 // FindVirtualMethodFor... This is ok for FindDexMethodIndexInOtherDexFile that only cares
1076 // about the name and signature.
1077 uint32_t update_dex_cache_method_index = called->GetDexMethodIndex();
Andreas Gampe542451c2016-07-26 09:02:02 -07001078 if (!called->HasSameDexCacheResolvedMethods(caller, kRuntimePointerSize)) {
Ian Rogers83883d72013-10-21 21:07:24 -07001079 // Calling from one dex file to another, need to compute the method index appropriate to
Vladimir Markobbcc0c02014-02-03 14:08:42 +00001080 // the caller's dex file. Since we get here only if the original called was a runtime
1081 // method, we've got the correct dex_file and a dex_method_idx from above.
Ian Rogerse0a02da2014-12-02 14:10:53 -08001082 DCHECK(!called_method_known_on_entry);
1083 DCHECK_EQ(caller->GetDexFile(), called_method.dex_file);
1084 const DexFile* caller_dex_file = called_method.dex_file;
1085 uint32_t caller_method_name_and_sig_index = called_method.dex_method_index;
1086 update_dex_cache_method_index =
1087 called->FindDexMethodIndexInOtherDexFile(*caller_dex_file,
1088 caller_method_name_and_sig_index);
1089 }
1090 if ((update_dex_cache_method_index != DexFile::kDexNoIndex) &&
Mathieu Chartiere401d142015-04-22 13:56:20 -07001091 (caller->GetDexCacheResolvedMethod(
Andreas Gampe542451c2016-07-26 09:02:02 -07001092 update_dex_cache_method_index, kRuntimePointerSize) != called)) {
1093 caller->SetDexCacheResolvedMethod(update_dex_cache_method_index,
1094 called,
1095 kRuntimePointerSize);
Ian Rogers83883d72013-10-21 21:07:24 -07001096 }
Mathieu Chartiere4a91bb2015-01-28 13:11:44 -08001097 } else if (invoke_type == kStatic) {
1098 const auto called_dex_method_idx = called->GetDexMethodIndex();
1099 // For static invokes, we may dispatch to the static method in the superclass but resolve
1100 // using the subclass. To prevent getting slow paths on each invoke, we force set the
1101 // resolved method for the super class dex method index if we are in the same dex file.
1102 // b/19175856
1103 if (called->GetDexFile() == called_method.dex_file &&
1104 called_method.dex_method_index != called_dex_method_idx) {
Andreas Gampe542451c2016-07-26 09:02:02 -07001105 called->GetDexCache()->SetResolvedMethod(called_dex_method_idx,
1106 called,
1107 kRuntimePointerSize);
Mathieu Chartiere4a91bb2015-01-28 13:11:44 -08001108 }
Ian Rogers83883d72013-10-21 21:07:24 -07001109 }
Daniel Mihalyieb076692014-08-22 17:33:31 +02001110
Ian Rogers848871b2013-08-05 10:56:33 -07001111 // Ensure that the called method's class is initialized.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001112 StackHandleScope<1> hs(soa.Self());
1113 Handle<mirror::Class> called_class(hs.NewHandle(called->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -07001114 linker->EnsureInitialized(soa.Self(), called_class, true, true);
Ian Rogers848871b2013-08-05 10:56:33 -07001115 if (LIKELY(called_class->IsInitialized())) {
Daniel Mihalyieb076692014-08-22 17:33:31 +02001116 if (UNLIKELY(Dbg::IsForcedInterpreterNeededForResolution(self, called))) {
1117 // If we are single-stepping or the called method is deoptimized (by a
1118 // breakpoint, for example), then we have to execute the called method
1119 // with the interpreter.
1120 code = GetQuickToInterpreterBridge();
1121 } else if (UNLIKELY(Dbg::IsForcedInstrumentationNeededForResolution(self, caller))) {
1122 // If the caller is deoptimized (by a breakpoint, for example), we have to
1123 // continue its execution with interpreter when returning from the called
1124 // method. Because we do not want to execute the called method with the
1125 // interpreter, we wrap its execution into the instrumentation stubs.
1126 // When the called method returns, it will execute the instrumentation
1127 // exit hook that will determine the need of the interpreter with a call
1128 // to Dbg::IsForcedInterpreterNeededForUpcall and deoptimize the stack if
1129 // it is needed.
1130 code = GetQuickInstrumentationEntryPoint();
1131 } else {
1132 code = called->GetEntryPointFromQuickCompiledCode();
1133 }
Ian Rogers848871b2013-08-05 10:56:33 -07001134 } else if (called_class->IsInitializing()) {
Daniel Mihalyieb076692014-08-22 17:33:31 +02001135 if (UNLIKELY(Dbg::IsForcedInterpreterNeededForResolution(self, called))) {
1136 // If we are single-stepping or the called method is deoptimized (by a
1137 // breakpoint, for example), then we have to execute the called method
1138 // with the interpreter.
1139 code = GetQuickToInterpreterBridge();
1140 } else if (invoke_type == kStatic) {
Ian Rogers848871b2013-08-05 10:56:33 -07001141 // Class is still initializing, go to oat and grab code (trampoline must be left in place
1142 // until class is initialized to stop races between threads).
Ian Rogersef7d42f2014-01-06 12:55:46 -08001143 code = linker->GetQuickOatCodeFor(called);
Ian Rogers848871b2013-08-05 10:56:33 -07001144 } else {
1145 // No trampoline for non-static methods.
Ian Rogersef7d42f2014-01-06 12:55:46 -08001146 code = called->GetEntryPointFromQuickCompiledCode();
Ian Rogers848871b2013-08-05 10:56:33 -07001147 }
1148 } else {
1149 DCHECK(called_class->IsErroneous());
1150 }
1151 }
Ian Rogerse0a02da2014-12-02 14:10:53 -08001152 CHECK_EQ(code == nullptr, self->IsExceptionPending());
Mathieu Chartier07d447b2013-09-26 11:57:43 -07001153 // Fixup any locally saved objects may have moved during a GC.
1154 visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -07001155 // Place called method in callee-save frame to be placed as first argument to quick method.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001156 *sp = called;
1157
Ian Rogers848871b2013-08-05 10:56:33 -07001158 return code;
1159}
1160
Andreas Gampec147b002014-03-06 18:11:06 -08001161/*
1162 * This class uses a couple of observations to unite the different calling conventions through
1163 * a few constants.
1164 *
1165 * 1) Number of registers used for passing is normally even, so counting down has no penalty for
1166 * possible alignment.
1167 * 2) Known 64b architectures store 8B units on the stack, both for integral and floating point
1168 * types, so using uintptr_t is OK. Also means that we can use kRegistersNeededX to denote
1169 * when we have to split things
1170 * 3) The only soft-float, Arm, is 32b, so no widening needs to be taken into account for floats
1171 * and we can use Int handling directly.
1172 * 4) Only 64b architectures widen, and their stack is aligned 8B anyways, so no padding code
1173 * necessary when widening. Also, widening of Ints will take place implicitly, and the
1174 * extension should be compatible with Aarch64, which mandates copying the available bits
1175 * into LSB and leaving the rest unspecified.
1176 * 5) Aligning longs and doubles is necessary on arm only, and it's the same in registers and on
1177 * the stack.
1178 * 6) There is only little endian.
1179 *
1180 *
1181 * Actual work is supposed to be done in a delegate of the template type. The interface is as
1182 * follows:
1183 *
1184 * void PushGpr(uintptr_t): Add a value for the next GPR
1185 *
1186 * void PushFpr4(float): Add a value for the next FPR of size 32b. Is only called if we need
1187 * padding, that is, think the architecture is 32b and aligns 64b.
1188 *
1189 * void PushFpr8(uint64_t): Push a double. We _will_ call this on 32b, it's the callee's job to
1190 * split this if necessary. The current state will have aligned, if
1191 * necessary.
1192 *
1193 * void PushStack(uintptr_t): Push a value to the stack.
1194 *
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001195 * uintptr_t PushHandleScope(mirror::Object* ref): Add a reference to the HandleScope. This _will_ have nullptr,
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001196 * as this might be important for null initialization.
Andreas Gampec147b002014-03-06 18:11:06 -08001197 * Must return the jobject, that is, the reference to the
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001198 * entry in the HandleScope (nullptr if necessary).
Andreas Gampec147b002014-03-06 18:11:06 -08001199 *
1200 */
Andreas Gampec200a4a2014-06-16 18:39:09 -07001201template<class T> class BuildNativeCallFrameStateMachine {
Andreas Gampec147b002014-03-06 18:11:06 -08001202 public:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001203#if defined(__arm__)
1204 // TODO: These are all dummy values!
Andreas Gampec147b002014-03-06 18:11:06 -08001205 static constexpr bool kNativeSoftFloatAbi = true;
1206 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs, r0-r3
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001207 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
1208
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001209 static constexpr size_t kRegistersNeededForLong = 2;
1210 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -08001211 static constexpr bool kMultiRegistersAligned = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001212 static constexpr bool kMultiFPRegistersWidened = false;
1213 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001214 static constexpr bool kAlignLongOnStack = true;
1215 static constexpr bool kAlignDoubleOnStack = true;
Stuart Monteithb95a5342014-03-12 13:32:32 +00001216#elif defined(__aarch64__)
1217 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
1218 static constexpr size_t kNumNativeGprArgs = 8; // 6 arguments passed in GPRs.
1219 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
1220
1221 static constexpr size_t kRegistersNeededForLong = 1;
1222 static constexpr size_t kRegistersNeededForDouble = 1;
1223 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001224 static constexpr bool kMultiFPRegistersWidened = false;
1225 static constexpr bool kMultiGPRegistersWidened = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +00001226 static constexpr bool kAlignLongOnStack = false;
1227 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001228#elif defined(__mips__) && !defined(__LP64__)
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001229 static constexpr bool kNativeSoftFloatAbi = true; // This is a hard float ABI.
Douglas Leung735b8552014-10-31 12:21:40 -07001230 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs.
1231 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001232
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001233 static constexpr size_t kRegistersNeededForLong = 2;
1234 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -08001235 static constexpr bool kMultiRegistersAligned = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001236 static constexpr bool kMultiFPRegistersWidened = true;
1237 static constexpr bool kMultiGPRegistersWidened = false;
Douglas Leung735b8552014-10-31 12:21:40 -07001238 static constexpr bool kAlignLongOnStack = true;
1239 static constexpr bool kAlignDoubleOnStack = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001240#elif defined(__mips__) && defined(__LP64__)
1241 // Let the code prepare GPRs only and we will load the FPRs with same data.
1242 static constexpr bool kNativeSoftFloatAbi = true;
1243 static constexpr size_t kNumNativeGprArgs = 8;
1244 static constexpr size_t kNumNativeFprArgs = 0;
1245
1246 static constexpr size_t kRegistersNeededForLong = 1;
1247 static constexpr size_t kRegistersNeededForDouble = 1;
1248 static constexpr bool kMultiRegistersAligned = false;
1249 static constexpr bool kMultiFPRegistersWidened = false;
1250 static constexpr bool kMultiGPRegistersWidened = true;
1251 static constexpr bool kAlignLongOnStack = false;
1252 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001253#elif defined(__i386__)
1254 // TODO: Check these!
Andreas Gampec147b002014-03-06 18:11:06 -08001255 static constexpr bool kNativeSoftFloatAbi = false; // Not using int registers for fp
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001256 static constexpr size_t kNumNativeGprArgs = 0; // 6 arguments passed in GPRs.
1257 static constexpr size_t kNumNativeFprArgs = 0; // 8 arguments passed in FPRs.
1258
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001259 static constexpr size_t kRegistersNeededForLong = 2;
1260 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001261 static constexpr bool kMultiRegistersAligned = false; // x86 not using regs, anyways
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001262 static constexpr bool kMultiFPRegistersWidened = false;
1263 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001264 static constexpr bool kAlignLongOnStack = false;
1265 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001266#elif defined(__x86_64__)
1267 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
1268 static constexpr size_t kNumNativeGprArgs = 6; // 6 arguments passed in GPRs.
1269 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
1270
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001271 static constexpr size_t kRegistersNeededForLong = 1;
1272 static constexpr size_t kRegistersNeededForDouble = 1;
Andreas Gampec147b002014-03-06 18:11:06 -08001273 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001274 static constexpr bool kMultiFPRegistersWidened = false;
1275 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001276 static constexpr bool kAlignLongOnStack = false;
1277 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001278#else
1279#error "Unsupported architecture"
1280#endif
1281
Andreas Gampec147b002014-03-06 18:11:06 -08001282 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001283 explicit BuildNativeCallFrameStateMachine(T* delegate)
1284 : gpr_index_(kNumNativeGprArgs),
1285 fpr_index_(kNumNativeFprArgs),
1286 stack_entries_(0),
1287 delegate_(delegate) {
Andreas Gampec147b002014-03-06 18:11:06 -08001288 // For register alignment, we want to assume that counters (gpr_index_, fpr_index_) are even iff
1289 // the next register is even; counting down is just to make the compiler happy...
Andreas Gampe575e78c2014-11-03 23:41:03 -08001290 static_assert(kNumNativeGprArgs % 2 == 0U, "Number of native GPR arguments not even");
1291 static_assert(kNumNativeFprArgs % 2 == 0U, "Number of native FPR arguments not even");
Andreas Gampec147b002014-03-06 18:11:06 -08001292 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001293
Andreas Gampec200a4a2014-06-16 18:39:09 -07001294 virtual ~BuildNativeCallFrameStateMachine() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001295
Ian Rogers1428dce2014-10-21 15:02:15 -07001296 bool HavePointerGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001297 return gpr_index_ > 0;
1298 }
1299
Andreas Gampec200a4a2014-06-16 18:39:09 -07001300 void AdvancePointer(const void* val) {
Andreas Gampec147b002014-03-06 18:11:06 -08001301 if (HavePointerGpr()) {
1302 gpr_index_--;
1303 PushGpr(reinterpret_cast<uintptr_t>(val));
1304 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001305 stack_entries_++; // TODO: have a field for pointer length as multiple of 32b
Andreas Gampec147b002014-03-06 18:11:06 -08001306 PushStack(reinterpret_cast<uintptr_t>(val));
1307 gpr_index_ = 0;
1308 }
1309 }
1310
Ian Rogers1428dce2014-10-21 15:02:15 -07001311 bool HaveHandleScopeGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001312 return gpr_index_ > 0;
1313 }
1314
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001315 void AdvanceHandleScope(mirror::Object* ptr) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001316 uintptr_t handle = PushHandle(ptr);
1317 if (HaveHandleScopeGpr()) {
Andreas Gampec147b002014-03-06 18:11:06 -08001318 gpr_index_--;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001319 PushGpr(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001320 } else {
1321 stack_entries_++;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001322 PushStack(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001323 gpr_index_ = 0;
1324 }
1325 }
1326
Ian Rogers1428dce2014-10-21 15:02:15 -07001327 bool HaveIntGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001328 return gpr_index_ > 0;
1329 }
1330
1331 void AdvanceInt(uint32_t val) {
1332 if (HaveIntGpr()) {
1333 gpr_index_--;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001334 if (kMultiGPRegistersWidened) {
1335 DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001336 PushGpr(static_cast<int64_t>(bit_cast<int32_t, uint32_t>(val)));
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001337 } else {
1338 PushGpr(val);
1339 }
Andreas Gampec147b002014-03-06 18:11:06 -08001340 } else {
1341 stack_entries_++;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001342 if (kMultiGPRegistersWidened) {
1343 DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001344 PushStack(static_cast<int64_t>(bit_cast<int32_t, uint32_t>(val)));
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001345 } else {
1346 PushStack(val);
1347 }
Andreas Gampec147b002014-03-06 18:11:06 -08001348 gpr_index_ = 0;
1349 }
1350 }
1351
Ian Rogers1428dce2014-10-21 15:02:15 -07001352 bool HaveLongGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001353 return gpr_index_ >= kRegistersNeededForLong + (LongGprNeedsPadding() ? 1 : 0);
1354 }
1355
Ian Rogers1428dce2014-10-21 15:02:15 -07001356 bool LongGprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001357 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1358 kAlignLongOnStack && // and when it needs alignment
1359 (gpr_index_ & 1) == 1; // counter is odd, see constructor
1360 }
1361
Ian Rogers1428dce2014-10-21 15:02:15 -07001362 bool LongStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001363 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1364 kAlignLongOnStack && // and when it needs 8B alignment
1365 (stack_entries_ & 1) == 1; // counter is odd
1366 }
1367
1368 void AdvanceLong(uint64_t val) {
1369 if (HaveLongGpr()) {
1370 if (LongGprNeedsPadding()) {
1371 PushGpr(0);
1372 gpr_index_--;
1373 }
1374 if (kRegistersNeededForLong == 1) {
1375 PushGpr(static_cast<uintptr_t>(val));
1376 } else {
1377 PushGpr(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1378 PushGpr(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1379 }
1380 gpr_index_ -= kRegistersNeededForLong;
1381 } else {
1382 if (LongStackNeedsPadding()) {
1383 PushStack(0);
1384 stack_entries_++;
1385 }
1386 if (kRegistersNeededForLong == 1) {
1387 PushStack(static_cast<uintptr_t>(val));
1388 stack_entries_++;
1389 } else {
1390 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1391 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1392 stack_entries_ += 2;
1393 }
1394 gpr_index_ = 0;
1395 }
1396 }
1397
Ian Rogers1428dce2014-10-21 15:02:15 -07001398 bool HaveFloatFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001399 return fpr_index_ > 0;
1400 }
1401
Andreas Gampec147b002014-03-06 18:11:06 -08001402 void AdvanceFloat(float val) {
1403 if (kNativeSoftFloatAbi) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001404 AdvanceInt(bit_cast<uint32_t, float>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001405 } else {
1406 if (HaveFloatFpr()) {
1407 fpr_index_--;
1408 if (kRegistersNeededForDouble == 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001409 if (kMultiFPRegistersWidened) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001410 PushFpr8(bit_cast<uint64_t, double>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001411 } else {
1412 // No widening, just use the bits.
Roland Levillainda4d79b2015-03-24 14:36:11 +00001413 PushFpr8(static_cast<uint64_t>(bit_cast<uint32_t, float>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001414 }
1415 } else {
1416 PushFpr4(val);
1417 }
1418 } else {
1419 stack_entries_++;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001420 if (kRegistersNeededForDouble == 1 && kMultiFPRegistersWidened) {
Andreas Gampec147b002014-03-06 18:11:06 -08001421 // Need to widen before storing: Note the "double" in the template instantiation.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001422 // Note: We need to jump through those hoops to make the compiler happy.
1423 DCHECK_EQ(sizeof(uintptr_t), sizeof(uint64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001424 PushStack(static_cast<uintptr_t>(bit_cast<uint64_t, double>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001425 } else {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001426 PushStack(static_cast<uintptr_t>(bit_cast<uint32_t, float>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001427 }
1428 fpr_index_ = 0;
1429 }
1430 }
1431 }
1432
Ian Rogers1428dce2014-10-21 15:02:15 -07001433 bool HaveDoubleFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001434 return fpr_index_ >= kRegistersNeededForDouble + (DoubleFprNeedsPadding() ? 1 : 0);
1435 }
1436
Ian Rogers1428dce2014-10-21 15:02:15 -07001437 bool DoubleFprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001438 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1439 kAlignDoubleOnStack && // and when it needs alignment
1440 (fpr_index_ & 1) == 1; // counter is odd, see constructor
1441 }
1442
Ian Rogers1428dce2014-10-21 15:02:15 -07001443 bool DoubleStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001444 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1445 kAlignDoubleOnStack && // and when it needs 8B alignment
1446 (stack_entries_ & 1) == 1; // counter is odd
1447 }
1448
1449 void AdvanceDouble(uint64_t val) {
1450 if (kNativeSoftFloatAbi) {
1451 AdvanceLong(val);
1452 } else {
1453 if (HaveDoubleFpr()) {
1454 if (DoubleFprNeedsPadding()) {
1455 PushFpr4(0);
1456 fpr_index_--;
1457 }
1458 PushFpr8(val);
1459 fpr_index_ -= kRegistersNeededForDouble;
1460 } else {
1461 if (DoubleStackNeedsPadding()) {
1462 PushStack(0);
1463 stack_entries_++;
1464 }
1465 if (kRegistersNeededForDouble == 1) {
1466 PushStack(static_cast<uintptr_t>(val));
1467 stack_entries_++;
1468 } else {
1469 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1470 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1471 stack_entries_ += 2;
1472 }
1473 fpr_index_ = 0;
1474 }
1475 }
1476 }
1477
Ian Rogers1428dce2014-10-21 15:02:15 -07001478 uint32_t GetStackEntries() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001479 return stack_entries_;
1480 }
1481
Ian Rogers1428dce2014-10-21 15:02:15 -07001482 uint32_t GetNumberOfUsedGprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001483 return kNumNativeGprArgs - gpr_index_;
1484 }
1485
Ian Rogers1428dce2014-10-21 15:02:15 -07001486 uint32_t GetNumberOfUsedFprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001487 return kNumNativeFprArgs - fpr_index_;
1488 }
1489
1490 private:
1491 void PushGpr(uintptr_t val) {
1492 delegate_->PushGpr(val);
1493 }
1494 void PushFpr4(float val) {
1495 delegate_->PushFpr4(val);
1496 }
1497 void PushFpr8(uint64_t val) {
1498 delegate_->PushFpr8(val);
1499 }
1500 void PushStack(uintptr_t val) {
1501 delegate_->PushStack(val);
1502 }
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001503 uintptr_t PushHandle(mirror::Object* ref) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001504 return delegate_->PushHandle(ref);
Andreas Gampec147b002014-03-06 18:11:06 -08001505 }
1506
1507 uint32_t gpr_index_; // Number of free GPRs
1508 uint32_t fpr_index_; // Number of free FPRs
1509 uint32_t stack_entries_; // Stack entries are in multiples of 32b, as floats are usually not
1510 // extended
Ian Rogers1428dce2014-10-21 15:02:15 -07001511 T* const delegate_; // What Push implementation gets called
Andreas Gampec147b002014-03-06 18:11:06 -08001512};
1513
Andreas Gampec200a4a2014-06-16 18:39:09 -07001514// Computes the sizes of register stacks and call stack area. Handling of references can be extended
1515// in subclasses.
1516//
1517// To handle native pointers, use "L" in the shorty for an object reference, which simulates
1518// them with handles.
1519class ComputeNativeCallFrameSize {
Andreas Gampec147b002014-03-06 18:11:06 -08001520 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001521 ComputeNativeCallFrameSize() : num_stack_entries_(0) {}
1522
1523 virtual ~ComputeNativeCallFrameSize() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001524
Ian Rogers1428dce2014-10-21 15:02:15 -07001525 uint32_t GetStackSize() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001526 return num_stack_entries_ * sizeof(uintptr_t);
1527 }
1528
Ian Rogers1428dce2014-10-21 15:02:15 -07001529 uint8_t* LayoutCallStack(uint8_t* sp8) const {
Andreas Gampec147b002014-03-06 18:11:06 -08001530 sp8 -= GetStackSize();
Andreas Gampe779f8c92014-06-09 18:29:38 -07001531 // Align by kStackAlignment.
1532 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
Andreas Gampec200a4a2014-06-16 18:39:09 -07001533 return sp8;
Andreas Gampec147b002014-03-06 18:11:06 -08001534 }
1535
Ian Rogers1428dce2014-10-21 15:02:15 -07001536 uint8_t* LayoutCallRegisterStacks(uint8_t* sp8, uintptr_t** start_gpr, uint32_t** start_fpr)
1537 const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001538 // Assumption is OK right now, as we have soft-float arm
1539 size_t fregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeFprArgs;
1540 sp8 -= fregs * sizeof(uintptr_t);
1541 *start_fpr = reinterpret_cast<uint32_t*>(sp8);
1542 size_t iregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeGprArgs;
1543 sp8 -= iregs * sizeof(uintptr_t);
1544 *start_gpr = reinterpret_cast<uintptr_t*>(sp8);
1545 return sp8;
1546 }
Andreas Gampec147b002014-03-06 18:11:06 -08001547
Andreas Gampec200a4a2014-06-16 18:39:09 -07001548 uint8_t* LayoutNativeCall(uint8_t* sp8, uintptr_t** start_stack, uintptr_t** start_gpr,
Ian Rogers1428dce2014-10-21 15:02:15 -07001549 uint32_t** start_fpr) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001550 // Native call stack.
1551 sp8 = LayoutCallStack(sp8);
1552 *start_stack = reinterpret_cast<uintptr_t*>(sp8);
Andreas Gampec147b002014-03-06 18:11:06 -08001553
Andreas Gampec200a4a2014-06-16 18:39:09 -07001554 // Put fprs and gprs below.
1555 sp8 = LayoutCallRegisterStacks(sp8, start_gpr, start_fpr);
Andreas Gampec147b002014-03-06 18:11:06 -08001556
Andreas Gampec200a4a2014-06-16 18:39:09 -07001557 // Return the new bottom.
1558 return sp8;
1559 }
1560
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001561 virtual void WalkHeader(
1562 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm ATTRIBUTE_UNUSED)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001563 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001564 }
Andreas Gampec200a4a2014-06-16 18:39:09 -07001565
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001566 void Walk(const char* shorty, uint32_t shorty_len) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001567 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize> sm(this);
1568
1569 WalkHeader(&sm);
Andreas Gampec147b002014-03-06 18:11:06 -08001570
1571 for (uint32_t i = 1; i < shorty_len; ++i) {
1572 Primitive::Type cur_type_ = Primitive::GetType(shorty[i]);
1573 switch (cur_type_) {
1574 case Primitive::kPrimNot:
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001575 // TODO: fix abuse of mirror types.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001576 sm.AdvanceHandleScope(
1577 reinterpret_cast<mirror::Object*>(0x12345678));
Andreas Gampec147b002014-03-06 18:11:06 -08001578 break;
1579
1580 case Primitive::kPrimBoolean:
1581 case Primitive::kPrimByte:
1582 case Primitive::kPrimChar:
1583 case Primitive::kPrimShort:
1584 case Primitive::kPrimInt:
1585 sm.AdvanceInt(0);
1586 break;
1587 case Primitive::kPrimFloat:
1588 sm.AdvanceFloat(0);
1589 break;
1590 case Primitive::kPrimDouble:
1591 sm.AdvanceDouble(0);
1592 break;
1593 case Primitive::kPrimLong:
1594 sm.AdvanceLong(0);
1595 break;
1596 default:
1597 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001598 UNREACHABLE();
Andreas Gampec147b002014-03-06 18:11:06 -08001599 }
1600 }
1601
Ian Rogers1428dce2014-10-21 15:02:15 -07001602 num_stack_entries_ = sm.GetStackEntries();
Andreas Gampec147b002014-03-06 18:11:06 -08001603 }
1604
1605 void PushGpr(uintptr_t /* val */) {
1606 // not optimizing registers, yet
1607 }
1608
1609 void PushFpr4(float /* val */) {
1610 // not optimizing registers, yet
1611 }
1612
1613 void PushFpr8(uint64_t /* val */) {
1614 // not optimizing registers, yet
1615 }
1616
1617 void PushStack(uintptr_t /* val */) {
1618 // counting is already done in the superclass
1619 }
1620
Andreas Gampec200a4a2014-06-16 18:39:09 -07001621 virtual uintptr_t PushHandle(mirror::Object* /* ptr */) {
Andreas Gampec147b002014-03-06 18:11:06 -08001622 return reinterpret_cast<uintptr_t>(nullptr);
1623 }
1624
Andreas Gampec200a4a2014-06-16 18:39:09 -07001625 protected:
Andreas Gampec147b002014-03-06 18:11:06 -08001626 uint32_t num_stack_entries_;
1627};
1628
Andreas Gampec200a4a2014-06-16 18:39:09 -07001629class ComputeGenericJniFrameSize FINAL : public ComputeNativeCallFrameSize {
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001630 public:
Igor Murashkin06a04e02016-09-13 15:57:37 -07001631 explicit ComputeGenericJniFrameSize(bool critical_native)
1632 : num_handle_scope_references_(0), critical_native_(critical_native) {}
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001633
Andreas Gampec200a4a2014-06-16 18:39:09 -07001634 // Lays out the callee-save frame. Assumes that the incorrect frame corresponding to RefsAndArgs
1635 // is at *m = sp. Will update to point to the bottom of the save frame.
1636 //
1637 // Note: assumes ComputeAll() has been run before.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001638 void LayoutCalleeSaveFrame(Thread* self, ArtMethod*** m, void* sp, HandleScope** handle_scope)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001639 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001640 ArtMethod* method = **m;
1641
Andreas Gampe542451c2016-07-26 09:02:02 -07001642 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001643
Andreas Gampec200a4a2014-06-16 18:39:09 -07001644 uint8_t* sp8 = reinterpret_cast<uint8_t*>(sp);
1645
1646 // First, fix up the layout of the callee-save frame.
1647 // We have to squeeze in the HandleScope, and relocate the method pointer.
1648
1649 // "Free" the slot for the method.
Ian Rogers13735952014-10-08 12:43:28 -07001650 sp8 += sizeof(void*); // In the callee-save frame we use a full pointer.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001651
1652 // Under the callee saves put handle scope and new method stack reference.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001653 size_t handle_scope_size = HandleScope::SizeOf(num_handle_scope_references_);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001654 size_t scope_and_method = handle_scope_size + sizeof(ArtMethod*);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001655
1656 sp8 -= scope_and_method;
1657 // Align by kStackAlignment.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001658 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
Andreas Gampec200a4a2014-06-16 18:39:09 -07001659
Mathieu Chartiere401d142015-04-22 13:56:20 -07001660 uint8_t* sp8_table = sp8 + sizeof(ArtMethod*);
Ian Rogers59c07062014-10-10 13:03:39 -07001661 *handle_scope = HandleScope::Create(sp8_table, self->GetTopHandleScope(),
1662 num_handle_scope_references_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001663
1664 // Add a slot for the method pointer, and fill it. Fix the pointer-pointer given to us.
1665 uint8_t* method_pointer = sp8;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001666 auto** new_method_ref = reinterpret_cast<ArtMethod**>(method_pointer);
1667 *new_method_ref = method;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001668 *m = new_method_ref;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001669 }
1670
Andreas Gampec200a4a2014-06-16 18:39:09 -07001671 // Adds space for the cookie. Note: may leave stack unaligned.
Ian Rogers1428dce2014-10-21 15:02:15 -07001672 void LayoutCookie(uint8_t** sp) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001673 // Reference cookie and padding
1674 *sp -= 8;
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001675 }
1676
Andreas Gampec200a4a2014-06-16 18:39:09 -07001677 // Re-layout the callee-save frame (insert a handle-scope). Then add space for the cookie.
1678 // Returns the new bottom. Note: this may be unaligned.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001679 uint8_t* LayoutJNISaveFrame(Thread* self, ArtMethod*** m, void* sp, HandleScope** handle_scope)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001680 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001681 // First, fix up the layout of the callee-save frame.
1682 // We have to squeeze in the HandleScope, and relocate the method pointer.
Ian Rogers59c07062014-10-10 13:03:39 -07001683 LayoutCalleeSaveFrame(self, m, sp, handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001684
1685 // The bottom of the callee-save frame is now where the method is, *m.
1686 uint8_t* sp8 = reinterpret_cast<uint8_t*>(*m);
1687
1688 // Add space for cookie.
1689 LayoutCookie(&sp8);
1690
1691 return sp8;
1692 }
1693
1694 // WARNING: After this, *sp won't be pointing to the method anymore!
Mathieu Chartiere401d142015-04-22 13:56:20 -07001695 uint8_t* ComputeLayout(Thread* self, ArtMethod*** m, const char* shorty, uint32_t shorty_len,
1696 HandleScope** handle_scope, uintptr_t** start_stack, uintptr_t** start_gpr,
1697 uint32_t** start_fpr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001698 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001699 Walk(shorty, shorty_len);
1700
1701 // JNI part.
Ian Rogers59c07062014-10-10 13:03:39 -07001702 uint8_t* sp8 = LayoutJNISaveFrame(self, m, reinterpret_cast<void*>(*m), handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001703
1704 sp8 = LayoutNativeCall(sp8, start_stack, start_gpr, start_fpr);
1705
1706 // Return the new bottom.
1707 return sp8;
1708 }
1709
1710 uintptr_t PushHandle(mirror::Object* /* ptr */) OVERRIDE;
1711
1712 // Add JNIEnv* and jobj/jclass before the shorty-derived elements.
1713 void WalkHeader(BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) OVERRIDE
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001714 REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001715
1716 private:
1717 uint32_t num_handle_scope_references_;
Igor Murashkin06a04e02016-09-13 15:57:37 -07001718 const bool critical_native_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001719};
1720
1721uintptr_t ComputeGenericJniFrameSize::PushHandle(mirror::Object* /* ptr */) {
1722 num_handle_scope_references_++;
1723 return reinterpret_cast<uintptr_t>(nullptr);
1724}
1725
1726void ComputeGenericJniFrameSize::WalkHeader(
1727 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) {
Igor Murashkin06a04e02016-09-13 15:57:37 -07001728 // First 2 parameters are always excluded for @CriticalNative.
1729 if (UNLIKELY(critical_native_)) {
1730 return;
1731 }
1732
Andreas Gampec200a4a2014-06-16 18:39:09 -07001733 // JNIEnv
1734 sm->AdvancePointer(nullptr);
1735
1736 // Class object or this as first argument
1737 sm->AdvanceHandleScope(reinterpret_cast<mirror::Object*>(0x12345678));
1738}
1739
1740// Class to push values to three separate regions. Used to fill the native call part. Adheres to
1741// the template requirements of BuildGenericJniFrameStateMachine.
1742class FillNativeCall {
1743 public:
1744 FillNativeCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) :
1745 cur_gpr_reg_(gpr_regs), cur_fpr_reg_(fpr_regs), cur_stack_arg_(stack_args) {}
1746
1747 virtual ~FillNativeCall() {}
1748
1749 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) {
1750 cur_gpr_reg_ = gpr_regs;
1751 cur_fpr_reg_ = fpr_regs;
1752 cur_stack_arg_ = stack_args;
Andreas Gampec147b002014-03-06 18:11:06 -08001753 }
1754
1755 void PushGpr(uintptr_t val) {
1756 *cur_gpr_reg_ = val;
1757 cur_gpr_reg_++;
1758 }
1759
1760 void PushFpr4(float val) {
1761 *cur_fpr_reg_ = val;
1762 cur_fpr_reg_++;
1763 }
1764
1765 void PushFpr8(uint64_t val) {
1766 uint64_t* tmp = reinterpret_cast<uint64_t*>(cur_fpr_reg_);
1767 *tmp = val;
1768 cur_fpr_reg_ += 2;
1769 }
1770
1771 void PushStack(uintptr_t val) {
1772 *cur_stack_arg_ = val;
1773 cur_stack_arg_++;
1774 }
1775
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001776 virtual uintptr_t PushHandle(mirror::Object*) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001777 LOG(FATAL) << "(Non-JNI) Native call does not use handles.";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001778 UNREACHABLE();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001779 }
1780
1781 private:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001782 uintptr_t* cur_gpr_reg_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001783 uint32_t* cur_fpr_reg_;
1784 uintptr_t* cur_stack_arg_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001785};
Andreas Gampec147b002014-03-06 18:11:06 -08001786
Andreas Gampec200a4a2014-06-16 18:39:09 -07001787// Visits arguments on the stack placing them into a region lower down the stack for the benefit
1788// of transitioning into native code.
1789class BuildGenericJniFrameVisitor FINAL : public QuickArgumentVisitor {
1790 public:
Igor Murashkin06a04e02016-09-13 15:57:37 -07001791 BuildGenericJniFrameVisitor(Thread* self,
1792 bool is_static,
1793 bool critical_native,
1794 const char* shorty,
1795 uint32_t shorty_len,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001796 ArtMethod*** sp)
Andreas Gampec200a4a2014-06-16 18:39:09 -07001797 : QuickArgumentVisitor(*sp, is_static, shorty, shorty_len),
Igor Murashkin06a04e02016-09-13 15:57:37 -07001798 jni_call_(nullptr, nullptr, nullptr, nullptr, critical_native),
1799 sm_(&jni_call_) {
1800 ComputeGenericJniFrameSize fsc(critical_native);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001801 uintptr_t* start_gpr_reg;
1802 uint32_t* start_fpr_reg;
1803 uintptr_t* start_stack_arg;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001804 bottom_of_used_area_ = fsc.ComputeLayout(self, sp, shorty, shorty_len,
Ian Rogers59c07062014-10-10 13:03:39 -07001805 &handle_scope_,
1806 &start_stack_arg,
Andreas Gampec200a4a2014-06-16 18:39:09 -07001807 &start_gpr_reg, &start_fpr_reg);
1808
Andreas Gampec200a4a2014-06-16 18:39:09 -07001809 jni_call_.Reset(start_gpr_reg, start_fpr_reg, start_stack_arg, handle_scope_);
1810
Igor Murashkin06a04e02016-09-13 15:57:37 -07001811 // First 2 parameters are always excluded for CriticalNative methods.
1812 if (LIKELY(!critical_native)) {
1813 // jni environment is always first argument
1814 sm_.AdvancePointer(self->GetJniEnv());
Andreas Gampec200a4a2014-06-16 18:39:09 -07001815
Igor Murashkin06a04e02016-09-13 15:57:37 -07001816 if (is_static) {
1817 sm_.AdvanceHandleScope((**sp)->GetDeclaringClass());
1818 } // else "this" reference is already handled by QuickArgumentVisitor.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001819 }
1820 }
1821
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001822 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001823
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001824 void FinalizeHandleScope(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001825
Vladimir Markof39745e2016-01-26 12:16:55 +00001826 StackReference<mirror::Object>* GetFirstHandleScopeEntry() {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001827 return handle_scope_->GetHandle(0).GetReference();
1828 }
1829
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001830 jobject GetFirstHandleScopeJObject() const REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001831 return handle_scope_->GetHandle(0).ToJObject();
1832 }
1833
Ian Rogers1428dce2014-10-21 15:02:15 -07001834 void* GetBottomOfUsedArea() const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001835 return bottom_of_used_area_;
1836 }
1837
1838 private:
1839 // A class to fill a JNI call. Adds reference/handle-scope management to FillNativeCall.
1840 class FillJniCall FINAL : public FillNativeCall {
1841 public:
1842 FillJniCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args,
Igor Murashkin06a04e02016-09-13 15:57:37 -07001843 HandleScope* handle_scope, bool critical_native)
1844 : FillNativeCall(gpr_regs, fpr_regs, stack_args),
1845 handle_scope_(handle_scope),
1846 cur_entry_(0),
1847 critical_native_(critical_native) {}
Andreas Gampec200a4a2014-06-16 18:39:09 -07001848
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001849 uintptr_t PushHandle(mirror::Object* ref) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001850
1851 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args, HandleScope* scope) {
1852 FillNativeCall::Reset(gpr_regs, fpr_regs, stack_args);
1853 handle_scope_ = scope;
1854 cur_entry_ = 0U;
1855 }
1856
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001857 void ResetRemainingScopeSlots() REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001858 // Initialize padding entries.
1859 size_t expected_slots = handle_scope_->NumberOfReferences();
1860 while (cur_entry_ < expected_slots) {
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07001861 handle_scope_->GetMutableHandle(cur_entry_++).Assign(nullptr);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001862 }
Igor Murashkin06a04e02016-09-13 15:57:37 -07001863
1864 if (!critical_native_) {
1865 // Non-critical natives have at least the self class (jclass) or this (jobject).
1866 DCHECK_NE(cur_entry_, 0U);
1867 }
Andreas Gampec200a4a2014-06-16 18:39:09 -07001868 }
1869
Mathieu Chartier1432a5b2016-10-04 15:41:42 -07001870 bool CriticalNative() const {
1871 return critical_native_;
1872 }
1873
Andreas Gampec200a4a2014-06-16 18:39:09 -07001874 private:
1875 HandleScope* handle_scope_;
1876 size_t cur_entry_;
Igor Murashkin06a04e02016-09-13 15:57:37 -07001877 const bool critical_native_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001878 };
1879
1880 HandleScope* handle_scope_;
1881 FillJniCall jni_call_;
1882 void* bottom_of_used_area_;
1883
1884 BuildNativeCallFrameStateMachine<FillJniCall> sm_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001885
1886 DISALLOW_COPY_AND_ASSIGN(BuildGenericJniFrameVisitor);
1887};
1888
Andreas Gampec200a4a2014-06-16 18:39:09 -07001889uintptr_t BuildGenericJniFrameVisitor::FillJniCall::PushHandle(mirror::Object* ref) {
1890 uintptr_t tmp;
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07001891 MutableHandle<mirror::Object> h = handle_scope_->GetMutableHandle(cur_entry_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001892 h.Assign(ref);
1893 tmp = reinterpret_cast<uintptr_t>(h.ToJObject());
1894 cur_entry_++;
1895 return tmp;
1896}
1897
Ian Rogers9758f792014-03-13 09:02:55 -07001898void BuildGenericJniFrameVisitor::Visit() {
1899 Primitive::Type type = GetParamPrimitiveType();
1900 switch (type) {
1901 case Primitive::kPrimLong: {
1902 jlong long_arg;
1903 if (IsSplitLongOrDouble()) {
1904 long_arg = ReadSplitLongParam();
1905 } else {
1906 long_arg = *reinterpret_cast<jlong*>(GetParamAddress());
1907 }
1908 sm_.AdvanceLong(long_arg);
1909 break;
1910 }
1911 case Primitive::kPrimDouble: {
1912 uint64_t double_arg;
1913 if (IsSplitLongOrDouble()) {
1914 // Read into union so that we don't case to a double.
1915 double_arg = ReadSplitLongParam();
1916 } else {
1917 double_arg = *reinterpret_cast<uint64_t*>(GetParamAddress());
1918 }
1919 sm_.AdvanceDouble(double_arg);
1920 break;
1921 }
1922 case Primitive::kPrimNot: {
1923 StackReference<mirror::Object>* stack_ref =
1924 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001925 sm_.AdvanceHandleScope(stack_ref->AsMirrorPtr());
Ian Rogers9758f792014-03-13 09:02:55 -07001926 break;
1927 }
1928 case Primitive::kPrimFloat:
1929 sm_.AdvanceFloat(*reinterpret_cast<float*>(GetParamAddress()));
1930 break;
1931 case Primitive::kPrimBoolean: // Fall-through.
1932 case Primitive::kPrimByte: // Fall-through.
1933 case Primitive::kPrimChar: // Fall-through.
1934 case Primitive::kPrimShort: // Fall-through.
1935 case Primitive::kPrimInt: // Fall-through.
1936 sm_.AdvanceInt(*reinterpret_cast<jint*>(GetParamAddress()));
1937 break;
1938 case Primitive::kPrimVoid:
1939 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -07001940 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -07001941 }
1942}
1943
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001944void BuildGenericJniFrameVisitor::FinalizeHandleScope(Thread* self) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001945 // Clear out rest of the scope.
1946 jni_call_.ResetRemainingScopeSlots();
Mathieu Chartier1432a5b2016-10-04 15:41:42 -07001947 if (!jni_call_.CriticalNative()) {
1948 // Install HandleScope.
1949 self->PushHandleScope(handle_scope_);
1950 }
Ian Rogers9758f792014-03-13 09:02:55 -07001951}
1952
Ian Rogers04c31d22014-07-07 21:44:06 -07001953#if defined(__arm__) || defined(__aarch64__)
Andreas Gampe90546832014-03-12 18:07:19 -07001954extern "C" void* artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07001955#else
1956extern "C" void* artFindNativeMethod(Thread* self);
1957#endif
Andreas Gampe90546832014-03-12 18:07:19 -07001958
Igor Murashkin06a04e02016-09-13 15:57:37 -07001959static uint64_t artQuickGenericJniEndJNIRef(Thread* self,
1960 uint32_t cookie,
1961 bool fast_native ATTRIBUTE_UNUSED,
1962 jobject l,
1963 jobject lock) {
1964 // TODO: add entrypoints for @FastNative returning objects.
Andreas Gampead615172014-04-04 16:20:13 -07001965 if (lock != nullptr) {
1966 return reinterpret_cast<uint64_t>(JniMethodEndWithReferenceSynchronized(l, cookie, lock, self));
1967 } else {
1968 return reinterpret_cast<uint64_t>(JniMethodEndWithReference(l, cookie, self));
1969 }
1970}
1971
Igor Murashkin06a04e02016-09-13 15:57:37 -07001972static void artQuickGenericJniEndJNINonRef(Thread* self,
1973 uint32_t cookie,
1974 bool fast_native,
1975 jobject lock) {
Andreas Gampead615172014-04-04 16:20:13 -07001976 if (lock != nullptr) {
1977 JniMethodEndSynchronized(cookie, lock, self);
Igor Murashkin06a04e02016-09-13 15:57:37 -07001978 // Ignore "fast_native" here because synchronized functions aren't very fast.
Andreas Gampead615172014-04-04 16:20:13 -07001979 } else {
Igor Murashkin06a04e02016-09-13 15:57:37 -07001980 if (UNLIKELY(fast_native)) {
1981 JniMethodFastEnd(cookie, self);
1982 } else {
1983 JniMethodEnd(cookie, self);
1984 }
Andreas Gampead615172014-04-04 16:20:13 -07001985 }
1986}
1987
Andreas Gampec147b002014-03-06 18:11:06 -08001988/*
1989 * Initializes an alloca region assumed to be directly below sp for a native call:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001990 * 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 -08001991 * The final element on the stack is a pointer to the native code.
1992 *
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001993 * On entry, the stack has a standard callee-save frame above sp, and an alloca below it.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001994 * We need to fix this, as the handle scope needs to go into the callee-save frame.
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001995 *
Andreas Gampec147b002014-03-06 18:11:06 -08001996 * The return of this function denotes:
1997 * 1) How many bytes of the alloca can be released, if the value is non-negative.
1998 * 2) An error, if the value is negative.
1999 */
Mathieu Chartiere401d142015-04-22 13:56:20 -07002000extern "C" TwoWordReturn artQuickGenericJniTrampoline(Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002001 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002002 ArtMethod* called = *sp;
David Sehr709b0702016-10-13 09:12:37 -07002003 DCHECK(called->IsNative()) << called->PrettyMethod(true);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002004 uint32_t shorty_len = 0;
2005 const char* shorty = called->GetShorty(&shorty_len);
Igor Murashkin06a04e02016-09-13 15:57:37 -07002006 bool critical_native = called->IsAnnotatedWithCriticalNative();
2007 bool fast_native = called->IsAnnotatedWithFastNative();
2008 bool normal_native = !critical_native && !fast_native;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002009
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002010 // Run the visitor and update sp.
Igor Murashkin06a04e02016-09-13 15:57:37 -07002011 BuildGenericJniFrameVisitor visitor(self,
2012 called->IsStatic(),
2013 critical_native,
2014 shorty,
2015 shorty_len,
2016 &sp);
Mathieu Chartierbe08cf52016-09-13 13:41:24 -07002017 {
2018 ScopedAssertNoThreadSuspension sants(__FUNCTION__);
2019 visitor.VisitArguments();
2020 // FinalizeHandleScope pushes the handle scope on the thread.
2021 visitor.FinalizeHandleScope(self);
2022 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002023
Andreas Gampec200a4a2014-06-16 18:39:09 -07002024 // Fix up managed-stack things in Thread.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002025 self->SetTopOfStack(sp);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002026
Ian Rogerse0dcd462014-03-08 15:21:04 -08002027 self->VerifyStack();
2028
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002029 uint32_t cookie;
Igor Murashkin06a04e02016-09-13 15:57:37 -07002030 uint32_t* sp32;
2031 // Skip calling JniMethodStart for @CriticalNative.
2032 if (LIKELY(!critical_native)) {
2033 // Start JNI, save the cookie.
2034 if (called->IsSynchronized()) {
2035 DCHECK(normal_native) << " @FastNative and synchronize is not supported";
2036 cookie = JniMethodStartSynchronized(visitor.GetFirstHandleScopeJObject(), self);
2037 if (self->IsExceptionPending()) {
2038 self->PopHandleScope();
2039 // A negative value denotes an error.
2040 return GetTwoWordFailureValue();
2041 }
2042 } else {
2043 if (fast_native) {
2044 cookie = JniMethodFastStart(self);
2045 } else {
2046 DCHECK(normal_native);
2047 cookie = JniMethodStart(self);
2048 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002049 }
Igor Murashkin06a04e02016-09-13 15:57:37 -07002050 sp32 = reinterpret_cast<uint32_t*>(sp);
2051 *(sp32 - 1) = cookie;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002052 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002053
Andreas Gampe90546832014-03-12 18:07:19 -07002054 // Retrieve the stored native code.
Mathieu Chartier2d721012014-11-10 11:08:06 -08002055 void* nativeCode = called->GetEntryPointFromJni();
Andreas Gampe90546832014-03-12 18:07:19 -07002056
Andreas Gampe9a6a99a2014-03-14 07:52:20 -07002057 // There are two cases for the content of nativeCode:
2058 // 1) Pointer to the native function.
2059 // 2) Pointer to the trampoline for native code binding.
2060 // In the second case, we need to execute the binding and continue with the actual native function
2061 // pointer.
Andreas Gampe90546832014-03-12 18:07:19 -07002062 DCHECK(nativeCode != nullptr);
2063 if (nativeCode == GetJniDlsymLookupStub()) {
Ian Rogers04c31d22014-07-07 21:44:06 -07002064#if defined(__arm__) || defined(__aarch64__)
Andreas Gampe90546832014-03-12 18:07:19 -07002065 nativeCode = artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07002066#else
2067 nativeCode = artFindNativeMethod(self);
2068#endif
Andreas Gampe90546832014-03-12 18:07:19 -07002069
2070 if (nativeCode == nullptr) {
2071 DCHECK(self->IsExceptionPending()); // There should be an exception pending now.
Andreas Gampead615172014-04-04 16:20:13 -07002072
Igor Murashkin06a04e02016-09-13 15:57:37 -07002073 // @CriticalNative calls do not need to call back into JniMethodEnd.
2074 if (LIKELY(!critical_native)) {
2075 // End JNI, as the assembly will move to deliver the exception.
2076 jobject lock = called->IsSynchronized() ? visitor.GetFirstHandleScopeJObject() : nullptr;
2077 if (shorty[0] == 'L') {
2078 artQuickGenericJniEndJNIRef(self, cookie, fast_native, nullptr, lock);
2079 } else {
2080 artQuickGenericJniEndJNINonRef(self, cookie, fast_native, lock);
2081 }
Andreas Gampead615172014-04-04 16:20:13 -07002082 }
2083
Andreas Gampec200a4a2014-06-16 18:39:09 -07002084 return GetTwoWordFailureValue();
Andreas Gampe90546832014-03-12 18:07:19 -07002085 }
2086 // Note that the native code pointer will be automatically set by artFindNativeMethod().
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002087 }
2088
Andreas Gampec200a4a2014-06-16 18:39:09 -07002089 // Return native code addr(lo) and bottom of alloca address(hi).
2090 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(visitor.GetBottomOfUsedArea()),
2091 reinterpret_cast<uintptr_t>(nativeCode));
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002092}
2093
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002094// Defined in quick_jni_entrypoints.cc.
2095extern uint64_t GenericJniMethodEnd(Thread* self, uint32_t saved_local_ref_cookie,
2096 jvalue result, uint64_t result_f, ArtMethod* called,
2097 HandleScope* handle_scope);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002098/*
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002099 * Is called after the native JNI code. Responsible for cleanup (handle scope, saved state) and
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002100 * unlocking.
2101 */
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002102extern "C" uint64_t artQuickGenericJniEndTrampoline(Thread* self,
2103 jvalue result,
2104 uint64_t result_f) {
2105 // We're here just back from a native call. We don't have the shared mutator lock at this point
2106 // yet until we call GoToRunnable() later in GenericJniMethodEnd(). Accessing objects or doing
2107 // anything that requires a mutator lock before that would cause problems as GC may have the
2108 // exclusive mutator lock and may be moving objects, etc.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002109 ArtMethod** sp = self->GetManagedStack()->GetTopQuickFrame();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002110 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002111 ArtMethod* called = *sp;
Ian Rogerse0dcd462014-03-08 15:21:04 -08002112 uint32_t cookie = *(sp32 - 1);
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002113 HandleScope* table = reinterpret_cast<HandleScope*>(reinterpret_cast<uint8_t*>(sp) + sizeof(*sp));
2114 return GenericJniMethodEnd(self, cookie, result, result_f, called, table);
Andreas Gampe2da88232014-02-27 12:26:20 -08002115}
2116
Andreas Gamped58342c2014-06-05 14:18:08 -07002117// We use TwoWordReturn to optimize scalar returns. We use the hi value for code, and the lo value
2118// for the method pointer.
Andreas Gampe51f76352014-05-21 08:28:48 -07002119//
Andreas Gamped58342c2014-06-05 14:18:08 -07002120// 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 -07002121// to hold the mutator lock (see REQUIRES_SHARED(Locks::mutator_lock_) annotations).
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002122
2123template<InvokeType type, bool access_check>
Mathieu Chartieref41db72016-10-25 15:08:01 -07002124static TwoWordReturn artInvokeCommon(uint32_t method_idx,
2125 ObjPtr<mirror::Object> this_object,
2126 Thread* self,
Mathieu Chartiere401d142015-04-22 13:56:20 -07002127 ArtMethod** sp) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002128 ScopedQuickEntrypointChecks sqec(self);
Vladimir Markofd36f1f2016-08-03 18:49:58 +01002129 DCHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(Runtime::kSaveRefsAndArgs));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002130 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
2131 ArtMethod* method = FindMethodFast(method_idx, this_object, caller_method, access_check, type);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002132 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002133 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()->GetDexFile();
2134 uint32_t shorty_len;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002135 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(method_idx), &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002136 {
2137 // Remember the args in case a GC happens in FindMethodFromCode.
2138 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
2139 RememberForGcArgumentVisitor visitor(sp, type == kStatic, shorty, shorty_len, &soa);
2140 visitor.VisitArguments();
Mathieu Chartieref41db72016-10-25 15:08:01 -07002141 method = FindMethodFromCode<type, access_check>(method_idx,
2142 &this_object,
2143 caller_method,
Mathieu Chartier0cd81352014-05-22 16:48:55 -07002144 self);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002145 visitor.FixupReferences();
2146 }
2147
Ian Rogerse0a02da2014-12-02 14:10:53 -08002148 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002149 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07002150 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002151 }
2152 }
2153 DCHECK(!self->IsExceptionPending());
2154 const void* code = method->GetEntryPointFromQuickCompiledCode();
2155
2156 // When we return, the caller will branch to this address, so it had better not be 0!
David Sehr709b0702016-10-13 09:12:37 -07002157 DCHECK(code != nullptr) << "Code was null in method: " << method->PrettyMethod()
Andreas Gampec200a4a2014-06-16 18:39:09 -07002158 << " location: "
2159 << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07002160
Andreas Gamped58342c2014-06-05 14:18:08 -07002161 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
2162 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002163}
2164
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01002165// Explicit artInvokeCommon template function declarations to please analysis tool.
2166#define EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(type, access_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002167 template REQUIRES_SHARED(Locks::mutator_lock_) \
Mathieu Chartiere401d142015-04-22 13:56:20 -07002168 TwoWordReturn artInvokeCommon<type, access_check>( \
Mathieu Chartieref41db72016-10-25 15:08:01 -07002169 uint32_t method_idx, ObjPtr<mirror::Object> his_object, Thread* self, ArtMethod** sp)
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01002170
2171EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, false);
2172EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, true);
2173EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, false);
2174EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, true);
2175EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, false);
2176EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, true);
2177EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, false);
2178EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, true);
2179EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, false);
2180EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, true);
2181#undef EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL
2182
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002183// See comments in runtime_support_asm.S
Andreas Gampec200a4a2014-06-16 18:39:09 -07002184extern "C" TwoWordReturn artInvokeInterfaceTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002185 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002186 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002187 return artInvokeCommon<kInterface, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002188}
2189
Andreas Gampec200a4a2014-06-16 18:39:09 -07002190extern "C" TwoWordReturn artInvokeDirectTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002191 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002192 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002193 return artInvokeCommon<kDirect, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002194}
2195
Andreas Gampec200a4a2014-06-16 18:39:09 -07002196extern "C" TwoWordReturn artInvokeStaticTrampolineWithAccessCheck(
Mathieu Chartieref41db72016-10-25 15:08:01 -07002197 uint32_t method_idx,
2198 mirror::Object* this_object ATTRIBUTE_UNUSED,
2199 Thread* self,
2200 ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
2201 // For static, this_object is not required and may be random garbage. Don't pass it down so that
2202 // it doesn't cause ObjPtr alignment failure check.
2203 return artInvokeCommon<kStatic, true>(method_idx, nullptr, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002204}
2205
Andreas Gampec200a4a2014-06-16 18:39:09 -07002206extern "C" TwoWordReturn artInvokeSuperTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002207 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002208 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002209 return artInvokeCommon<kSuper, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002210}
2211
Andreas Gampec200a4a2014-06-16 18:39:09 -07002212extern "C" TwoWordReturn artInvokeVirtualTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002213 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002214 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002215 return artInvokeCommon<kVirtual, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002216}
2217
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002218// Determine target of interface dispatch. This object is known non-null. First argument
2219// is there for consistency but should not be used, as some architectures overwrite it
2220// in the assembly trampoline.
2221extern "C" TwoWordReturn artInvokeInterfaceTrampoline(uint32_t deadbeef ATTRIBUTE_UNUSED,
Mathieu Chartieref41db72016-10-25 15:08:01 -07002222 mirror::Object* raw_this_object,
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002223 Thread* self,
2224 ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002225 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartieref41db72016-10-25 15:08:01 -07002226 ObjPtr<mirror::Object> this_object(raw_this_object);
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002227 ScopedQuickEntrypointChecks sqec(self);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002228 StackHandleScope<1> hs(self);
2229 Handle<mirror::Class> cls(hs.NewHandle(this_object->GetClass()));
2230
Nicolas Geoffray5bf7bac2016-07-06 14:18:23 +00002231 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002232
2233 // Fetch the dex_method_idx of the target interface method from the caller.
2234 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
2235
2236 const DexFile::CodeItem* code_item = caller_method->GetCodeItem();
2237 CHECK_LT(dex_pc, code_item->insns_size_in_code_units_);
2238 const Instruction* instr = Instruction::At(&code_item->insns_[dex_pc]);
2239 Instruction::Code instr_code = instr->Opcode();
2240 CHECK(instr_code == Instruction::INVOKE_INTERFACE ||
2241 instr_code == Instruction::INVOKE_INTERFACE_RANGE)
2242 << "Unexpected call into interface trampoline: " << instr->DumpString(nullptr);
2243 uint32_t dex_method_idx;
2244 if (instr_code == Instruction::INVOKE_INTERFACE) {
2245 dex_method_idx = instr->VRegB_35c();
2246 } else {
2247 CHECK_EQ(instr_code, Instruction::INVOKE_INTERFACE_RANGE);
2248 dex_method_idx = instr->VRegB_3rc();
2249 }
2250
Mathieu Chartiere401d142015-04-22 13:56:20 -07002251 ArtMethod* interface_method = caller_method->GetDexCacheResolvedMethod(
Andreas Gampe542451c2016-07-26 09:02:02 -07002252 dex_method_idx, kRuntimePointerSize);
David Sehr709b0702016-10-13 09:12:37 -07002253 DCHECK(interface_method != nullptr) << dex_method_idx << " " << caller_method->PrettyMethod();
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002254 ArtMethod* method = nullptr;
Andreas Gampe542451c2016-07-26 09:02:02 -07002255 ImTable* imt = cls->GetImt(kRuntimePointerSize);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002256
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002257 if (LIKELY(interface_method->GetDexMethodIndex() != DexFile::kDexNoIndex)) {
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002258 // If the dex cache already resolved the interface method, look whether we have
2259 // a match in the ImtConflictTable.
Andreas Gampe75a7db62016-09-26 12:04:26 -07002260 ArtMethod* conflict_method = imt->Get(ImTable::GetImtIndex(interface_method),
2261 kRuntimePointerSize);
Alex Light9fc547a2016-03-31 14:34:33 -07002262 if (LIKELY(conflict_method->IsRuntimeMethod())) {
Andreas Gampe542451c2016-07-26 09:02:02 -07002263 ImtConflictTable* current_table = conflict_method->GetImtConflictTable(kRuntimePointerSize);
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002264 DCHECK(current_table != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -07002265 method = current_table->Lookup(interface_method, kRuntimePointerSize);
Alex Light9fc547a2016-03-31 14:34:33 -07002266 } else {
2267 // It seems we aren't really a conflict method!
Andreas Gampe542451c2016-07-26 09:02:02 -07002268 method = cls->FindVirtualMethodForInterface(interface_method, kRuntimePointerSize);
Alex Light9fc547a2016-03-31 14:34:33 -07002269 }
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002270 if (method != nullptr) {
2271 return GetTwoWordSuccessValue(
2272 reinterpret_cast<uintptr_t>(method->GetEntryPointFromQuickCompiledCode()),
2273 reinterpret_cast<uintptr_t>(method));
2274 }
2275
2276 // No match, use the IfTable.
Andreas Gampe542451c2016-07-26 09:02:02 -07002277 method = cls->FindVirtualMethodForInterface(interface_method, kRuntimePointerSize);
Ian Rogerse0a02da2014-12-02 14:10:53 -08002278 if (UNLIKELY(method == nullptr)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002279 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(
2280 interface_method, this_object, caller_method);
Andreas Gamped58342c2014-06-05 14:18:08 -07002281 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002282 }
2283 } else {
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002284 // The dex cache did not resolve the method, look it up in the dex file
2285 // of the caller,
Mathieu Chartier4edd8472015-06-01 10:47:36 -07002286 DCHECK_EQ(interface_method, Runtime::Current()->GetResolutionMethod());
Andreas Gampec200a4a2014-06-16 18:39:09 -07002287 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()
2288 ->GetDexFile();
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002289 uint32_t shorty_len;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002290 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(dex_method_idx),
2291 &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002292 {
2293 // Remember the args in case a GC happens in FindMethodFromCode.
2294 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
2295 RememberForGcArgumentVisitor visitor(sp, false, shorty, shorty_len, &soa);
2296 visitor.VisitArguments();
Mathieu Chartieref41db72016-10-25 15:08:01 -07002297 method = FindMethodFromCode<kInterface, false>(dex_method_idx,
2298 &this_object,
2299 caller_method,
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002300 self);
2301 visitor.FixupReferences();
2302 }
2303
2304 if (UNLIKELY(method == nullptr)) {
2305 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07002306 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002307 }
Andreas Gampe542451c2016-07-26 09:02:02 -07002308 interface_method =
2309 caller_method->GetDexCacheResolvedMethod(dex_method_idx, kRuntimePointerSize);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002310 DCHECK(!interface_method->IsRuntimeMethod());
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002311 }
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002312
2313 // We arrive here if we have found an implementation, and it is not in the ImtConflictTable.
2314 // We create a new table with the new pair { interface_method, method }.
Andreas Gampe75a7db62016-09-26 12:04:26 -07002315 uint32_t imt_index = ImTable::GetImtIndex(interface_method);
Andreas Gampe542451c2016-07-26 09:02:02 -07002316 ArtMethod* conflict_method = imt->Get(imt_index, kRuntimePointerSize);
Alex Light9fc547a2016-03-31 14:34:33 -07002317 if (conflict_method->IsRuntimeMethod()) {
Mathieu Chartier7f98c9a2016-04-14 10:49:19 -07002318 ArtMethod* new_conflict_method = Runtime::Current()->GetClassLinker()->AddMethodToConflictTable(
2319 cls.Get(),
2320 conflict_method,
2321 interface_method,
2322 method,
2323 /*force_new_conflict_method*/false);
2324 if (new_conflict_method != conflict_method) {
Alex Light9fc547a2016-03-31 14:34:33 -07002325 // Update the IMT if we create a new conflict method. No fence needed here, as the
2326 // data is consistent.
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002327 imt->Set(imt_index,
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002328 new_conflict_method,
Andreas Gampe542451c2016-07-26 09:02:02 -07002329 kRuntimePointerSize);
Alex Light9fc547a2016-03-31 14:34:33 -07002330 }
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002331 }
2332
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002333 const void* code = method->GetEntryPointFromQuickCompiledCode();
2334
2335 // When we return, the caller will branch to this address, so it had better not be 0!
David Sehr709b0702016-10-13 09:12:37 -07002336 DCHECK(code != nullptr) << "Code was null in method: " << method->PrettyMethod()
Andreas Gampec200a4a2014-06-16 18:39:09 -07002337 << " location: " << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07002338
Andreas Gamped58342c2014-06-05 14:18:08 -07002339 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
2340 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002341}
2342
Ian Rogers848871b2013-08-05 10:56:33 -07002343} // namespace art