blob: 126e26ccf54d66d48cf179fa82bdd38bfdaeea13 [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
663 DCHECK(!method->IsNative()) << PrettyMethod(method);
664 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();
667 DCHECK(code_item != nullptr) << PrettyMethod(method);
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 }
682 CHECK_EQ(method, linked->GetMethod()) << PrettyMethod(method) << " "
683 << PrettyMethod(linked->GetMethod());
684 }
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)) {
Ian Rogerse94652f2014-12-02 11:13:19 -0800746 DCHECK(Thread::Current()->IsExceptionPending()) << PrettyMethod(shadow_frame->GetMethod());
Ian Rogers848871b2013-08-05 10:56:33 -0700747 self->PopManagedStackFragment(fragment);
748 return 0;
749 }
750 }
Daniel Mihalyieb076692014-08-22 17:33:31 +0200751
Andreas Gampe639bdd12015-06-03 11:22:45 -0700752 result = interpreter::EnterInterpreterFromEntryPoint(self, code_item, shadow_frame);
Ian Rogers848871b2013-08-05 10:56:33 -0700753 }
Andreas Gampe639bdd12015-06-03 11:22:45 -0700754
755 // Pop transition.
756 self->PopManagedStackFragment(fragment);
757
758 // Request a stack deoptimization if needed
759 ArtMethod* caller = QuickArgumentVisitor::GetCallingMethod(sp);
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700760 uintptr_t caller_pc = QuickArgumentVisitor::GetCallingPc(sp);
Mingyao Yanga3549d22016-06-02 17:01:02 -0700761 // If caller_pc is the instrumentation exit stub, the stub will check to see if deoptimization
762 // should be done and it knows the real return pc.
763 if (UNLIKELY(caller_pc != reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) &&
764 Dbg::IsForcedInterpreterNeededForUpcall(self, caller) &&
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700765 Runtime::Current()->IsDeoptimizeable(caller_pc))) {
Andreas Gampe639bdd12015-06-03 11:22:45 -0700766 // Push the context of the deoptimization stack so we can restore the return value and the
767 // exception before executing the deoptimized frames.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100768 self->PushDeoptimizationContext(
769 result, shorty[0] == 'L', /* from_code */ false, self->GetException());
Andreas Gampe639bdd12015-06-03 11:22:45 -0700770
771 // Set special exception to cause deoptimization.
772 self->SetException(Thread::GetDeoptimizationException());
773 }
774
775 // No need to restore the args since the method has already been run by the interpreter.
776 return result.GetJ();
Ian Rogers848871b2013-08-05 10:56:33 -0700777}
778
779// Visits arguments on the stack placing them into the args vector, Object* arguments are converted
780// to jobjects.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800781class BuildQuickArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700782 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700783 BuildQuickArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty, uint32_t shorty_len,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700784 ScopedObjectAccessUnchecked* soa, std::vector<jvalue>* args) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700785 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa), args_(args) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700786
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700787 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700788
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700789 void FixupReferences() REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800790
Ian Rogers848871b2013-08-05 10:56:33 -0700791 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700792 ScopedObjectAccessUnchecked* const soa_;
793 std::vector<jvalue>* const args_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800794 // References which we must update when exiting in case the GC moved the objects.
Ian Rogers700a4022014-05-19 16:49:03 -0700795 std::vector<std::pair<jobject, StackReference<mirror::Object>*>> references_;
Ian Rogers9758f792014-03-13 09:02:55 -0700796
Ian Rogers848871b2013-08-05 10:56:33 -0700797 DISALLOW_COPY_AND_ASSIGN(BuildQuickArgumentVisitor);
798};
799
Ian Rogers9758f792014-03-13 09:02:55 -0700800void BuildQuickArgumentVisitor::Visit() {
801 jvalue val;
802 Primitive::Type type = GetParamPrimitiveType();
803 switch (type) {
804 case Primitive::kPrimNot: {
805 StackReference<mirror::Object>* stack_ref =
806 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
807 val.l = soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
808 references_.push_back(std::make_pair(val.l, stack_ref));
809 break;
810 }
811 case Primitive::kPrimLong: // Fall-through.
812 case Primitive::kPrimDouble:
813 if (IsSplitLongOrDouble()) {
814 val.j = ReadSplitLongParam();
815 } else {
816 val.j = *reinterpret_cast<jlong*>(GetParamAddress());
817 }
818 break;
819 case Primitive::kPrimBoolean: // Fall-through.
820 case Primitive::kPrimByte: // Fall-through.
821 case Primitive::kPrimChar: // Fall-through.
822 case Primitive::kPrimShort: // Fall-through.
823 case Primitive::kPrimInt: // Fall-through.
824 case Primitive::kPrimFloat:
825 val.i = *reinterpret_cast<jint*>(GetParamAddress());
826 break;
827 case Primitive::kPrimVoid:
828 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700829 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -0700830 }
831 args_->push_back(val);
832}
833
834void BuildQuickArgumentVisitor::FixupReferences() {
835 // Fixup any references which may have changed.
836 for (const auto& pair : references_) {
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700837 pair.second->Assign(soa_->Decode<mirror::Object>(pair.first).Ptr());
Mathieu Chartier5f3ded42014-04-03 15:25:30 -0700838 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -0700839 }
840}
841
Ian Rogers848871b2013-08-05 10:56:33 -0700842// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
843// which is responsible for recording callee save registers. We explicitly place into jobjects the
844// incoming reference arguments (so they survive GC). We invoke the invocation handler, which is a
845// field within the proxy object, which will box the primitive arguments and deal with error cases.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700846extern "C" uint64_t artQuickProxyInvokeHandler(
847 ArtMethod* proxy_method, mirror::Object* receiver, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700848 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000849 DCHECK(proxy_method->IsProxyMethod()) << PrettyMethod(proxy_method);
850 DCHECK(receiver->GetClass()->IsProxyClass()) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700851 // Ensure we don't get thread suspension until the object arguments are safely in jobjects.
852 const char* old_cause =
853 self->StartAssertNoThreadSuspension("Adding to IRT proxy object arguments");
854 // Register the top of the managed stack, making stack crawlable.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700855 DCHECK_EQ((*sp), proxy_method) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700856 self->VerifyStack();
857 // Start new JNI local reference state.
858 JNIEnvExt* env = self->GetJniEnv();
859 ScopedObjectAccessUnchecked soa(env);
860 ScopedJniEnvLocalRefState env_state(env);
861 // Create local ref. copies of proxy method and the receiver.
862 jobject rcvr_jobj = soa.AddLocalReference<jobject>(receiver);
863
864 // Placing arguments into args vector and remove the receiver.
Andreas Gampe542451c2016-07-26 09:02:02 -0700865 ArtMethod* non_proxy_method = proxy_method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700866 CHECK(!non_proxy_method->IsStatic()) << PrettyMethod(proxy_method) << " "
Andreas Gampec200a4a2014-06-16 18:39:09 -0700867 << PrettyMethod(non_proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700868 std::vector<jvalue> args;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700869 uint32_t shorty_len = 0;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700870 const char* shorty = non_proxy_method->GetShorty(&shorty_len);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700871 BuildQuickArgumentVisitor local_ref_visitor(sp, false, shorty, shorty_len, &soa, &args);
Brian Carlstromd3633d52013-08-20 21:06:26 -0700872
Ian Rogers848871b2013-08-05 10:56:33 -0700873 local_ref_visitor.VisitArguments();
Brian Carlstromd3633d52013-08-20 21:06:26 -0700874 DCHECK_GT(args.size(), 0U) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700875 args.erase(args.begin());
876
877 // Convert proxy method into expected interface method.
Andreas Gampe542451c2016-07-26 09:02:02 -0700878 ArtMethod* interface_method = proxy_method->FindOverriddenMethod(kRuntimePointerSize);
Ian Rogerse0a02da2014-12-02 14:10:53 -0800879 DCHECK(interface_method != nullptr) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700880 DCHECK(!interface_method->IsProxyMethod()) << PrettyMethod(interface_method);
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700881 self->EndAssertNoThreadSuspension(old_cause);
Andreas Gampe542451c2016-07-26 09:02:02 -0700882 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Andreas Gampee01e3642016-07-25 13:06:04 -0700883 DCHECK(!Runtime::Current()->IsActiveTransaction());
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700884 jobject interface_method_jobj = soa.AddLocalReference<jobject>(
Andreas Gampe542451c2016-07-26 09:02:02 -0700885 mirror::Method::CreateFromArtMethod<kRuntimePointerSize, false>(soa.Self(),
886 interface_method));
Ian Rogers848871b2013-08-05 10:56:33 -0700887
888 // All naked Object*s should now be in jobjects, so its safe to go into the main invoke code
889 // that performs allocations.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700890 JValue result = InvokeProxyInvocationHandler(soa, shorty, rcvr_jobj, interface_method_jobj, args);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800891 // Restore references which might have moved.
892 local_ref_visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -0700893 return result.GetJ();
894}
895
896// Read object references held in arguments from quick frames and place in a JNI local references,
897// so they don't get garbage collected.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800898class RememberForGcArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700899 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700900 RememberForGcArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty,
901 uint32_t shorty_len, ScopedObjectAccessUnchecked* soa) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700902 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700903
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700904 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Mathieu Chartier07d447b2013-09-26 11:57:43 -0700905
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700906 void FixupReferences() REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers848871b2013-08-05 10:56:33 -0700907
908 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700909 ScopedObjectAccessUnchecked* const soa_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800910 // References which we must update when exiting in case the GC moved the objects.
Andreas Gampec200a4a2014-06-16 18:39:09 -0700911 std::vector<std::pair<jobject, StackReference<mirror::Object>*> > references_;
912
Mathieu Chartier590fee92013-09-13 13:46:47 -0700913 DISALLOW_COPY_AND_ASSIGN(RememberForGcArgumentVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700914};
915
Ian Rogers9758f792014-03-13 09:02:55 -0700916void RememberForGcArgumentVisitor::Visit() {
917 if (IsParamAReference()) {
918 StackReference<mirror::Object>* stack_ref =
919 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
920 jobject reference =
921 soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
922 references_.push_back(std::make_pair(reference, stack_ref));
923 }
924}
925
926void RememberForGcArgumentVisitor::FixupReferences() {
927 // Fixup any references which may have changed.
928 for (const auto& pair : references_) {
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700929 pair.second->Assign(soa_->Decode<mirror::Object>(pair.first).Ptr());
Mathieu Chartier5f3ded42014-04-03 15:25:30 -0700930 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -0700931 }
932}
933
Ian Rogers848871b2013-08-05 10:56:33 -0700934// Lazily resolve a method for quick. Called by stub code.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700935extern "C" const void* artQuickResolutionTrampoline(
936 ArtMethod* called, mirror::Object* receiver, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700937 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe3b45ef22015-05-26 21:34:09 -0700938 // The resolution trampoline stashes the resolved method into the callee-save frame to transport
939 // it. Thus, when exiting, the stack cannot be verified (as the resolved method most likely
940 // does not have the same stack layout as the callee-save method).
941 ScopedQuickEntrypointChecks sqec(self, kIsDebugBuild, false);
Ian Rogers848871b2013-08-05 10:56:33 -0700942 // Start new JNI local reference state
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800943 JNIEnvExt* env = self->GetJniEnv();
Ian Rogers848871b2013-08-05 10:56:33 -0700944 ScopedObjectAccessUnchecked soa(env);
945 ScopedJniEnvLocalRefState env_state(env);
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800946 const char* old_cause = self->StartAssertNoThreadSuspension("Quick method resolution set up");
Ian Rogers848871b2013-08-05 10:56:33 -0700947
948 // Compute details about the called method (avoid GCs)
949 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Ian Rogers848871b2013-08-05 10:56:33 -0700950 InvokeType invoke_type;
Ian Rogerse0a02da2014-12-02 14:10:53 -0800951 MethodReference called_method(nullptr, 0);
952 const bool called_method_known_on_entry = !called->IsRuntimeMethod();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700953 ArtMethod* caller = nullptr;
Ian Rogerse0a02da2014-12-02 14:10:53 -0800954 if (!called_method_known_on_entry) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +0100955 caller = QuickArgumentVisitor::GetCallingMethod(sp);
956 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
Ian Rogers848871b2013-08-05 10:56:33 -0700957 const DexFile::CodeItem* code;
Ian Rogerse0a02da2014-12-02 14:10:53 -0800958 called_method.dex_file = caller->GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700959 code = caller->GetCodeItem();
Ian Rogers848871b2013-08-05 10:56:33 -0700960 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
961 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
962 Instruction::Code instr_code = instr->Opcode();
963 bool is_range;
964 switch (instr_code) {
965 case Instruction::INVOKE_DIRECT:
966 invoke_type = kDirect;
967 is_range = false;
968 break;
969 case Instruction::INVOKE_DIRECT_RANGE:
970 invoke_type = kDirect;
971 is_range = true;
972 break;
973 case Instruction::INVOKE_STATIC:
974 invoke_type = kStatic;
975 is_range = false;
976 break;
977 case Instruction::INVOKE_STATIC_RANGE:
978 invoke_type = kStatic;
979 is_range = true;
980 break;
981 case Instruction::INVOKE_SUPER:
982 invoke_type = kSuper;
983 is_range = false;
984 break;
985 case Instruction::INVOKE_SUPER_RANGE:
986 invoke_type = kSuper;
987 is_range = true;
988 break;
989 case Instruction::INVOKE_VIRTUAL:
990 invoke_type = kVirtual;
991 is_range = false;
992 break;
993 case Instruction::INVOKE_VIRTUAL_RANGE:
994 invoke_type = kVirtual;
995 is_range = true;
996 break;
997 case Instruction::INVOKE_INTERFACE:
998 invoke_type = kInterface;
999 is_range = false;
1000 break;
1001 case Instruction::INVOKE_INTERFACE_RANGE:
1002 invoke_type = kInterface;
1003 is_range = true;
1004 break;
1005 default:
Ian Rogerse0a02da2014-12-02 14:10:53 -08001006 LOG(FATAL) << "Unexpected call into trampoline: " << instr->DumpString(nullptr);
1007 UNREACHABLE();
Ian Rogers848871b2013-08-05 10:56:33 -07001008 }
Ian Rogerse0a02da2014-12-02 14:10:53 -08001009 called_method.dex_method_index = (is_range) ? instr->VRegB_3rc() : instr->VRegB_35c();
Ian Rogers848871b2013-08-05 10:56:33 -07001010 } else {
1011 invoke_type = kStatic;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001012 called_method.dex_file = called->GetDexFile();
1013 called_method.dex_method_index = called->GetDexMethodIndex();
Ian Rogers848871b2013-08-05 10:56:33 -07001014 }
1015 uint32_t shorty_len;
1016 const char* shorty =
Ian Rogerse0a02da2014-12-02 14:10:53 -08001017 called_method.dex_file->GetMethodShorty(
1018 called_method.dex_file->GetMethodId(called_method.dex_method_index), &shorty_len);
Mathieu Chartier590fee92013-09-13 13:46:47 -07001019 RememberForGcArgumentVisitor visitor(sp, invoke_type == kStatic, shorty, shorty_len, &soa);
Ian Rogers848871b2013-08-05 10:56:33 -07001020 visitor.VisitArguments();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001021 self->EndAssertNoThreadSuspension(old_cause);
Ian Rogerse0a02da2014-12-02 14:10:53 -08001022 const bool virtual_or_interface = invoke_type == kVirtual || invoke_type == kInterface;
Ian Rogers848871b2013-08-05 10:56:33 -07001023 // Resolve method filling in dex cache.
Ian Rogerse0a02da2014-12-02 14:10:53 -08001024 if (!called_method_known_on_entry) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001025 StackHandleScope<1> hs(self);
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001026 mirror::Object* dummy = nullptr;
1027 HandleWrapper<mirror::Object> h_receiver(
1028 hs.NewHandleWrapper(virtual_or_interface ? &receiver : &dummy));
Ian Rogerse0a02da2014-12-02 14:10:53 -08001029 DCHECK_EQ(caller->GetDexFile(), called_method.dex_file);
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08001030 called = linker->ResolveMethod<ClassLinker::kForceICCECheck>(
1031 self, called_method.dex_method_index, caller, invoke_type);
Ian Rogers848871b2013-08-05 10:56:33 -07001032 }
Ian Rogerse0a02da2014-12-02 14:10:53 -08001033 const void* code = nullptr;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001034 if (LIKELY(!self->IsExceptionPending())) {
Ian Rogers848871b2013-08-05 10:56:33 -07001035 // Incompatible class change should have been handled in resolve method.
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001036 CHECK(!called->CheckIncompatibleClassChange(invoke_type))
1037 << PrettyMethod(called) << " " << invoke_type;
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001038 if (virtual_or_interface || invoke_type == kSuper) {
1039 // Refine called method based on receiver for kVirtual/kInterface, and
1040 // caller for kSuper.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001041 ArtMethod* orig_called = called;
Mathieu Chartier55871bf2014-02-27 10:24:50 -08001042 if (invoke_type == kVirtual) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001043 CHECK(receiver != nullptr) << invoke_type;
Andreas Gampe542451c2016-07-26 09:02:02 -07001044 called = receiver->GetClass()->FindVirtualMethodForVirtual(called, kRuntimePointerSize);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001045 } else if (invoke_type == kInterface) {
1046 CHECK(receiver != nullptr) << invoke_type;
Andreas Gampe542451c2016-07-26 09:02:02 -07001047 called = receiver->GetClass()->FindVirtualMethodForInterface(called, kRuntimePointerSize);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001048 } else {
1049 DCHECK_EQ(invoke_type, kSuper);
1050 CHECK(caller != nullptr) << invoke_type;
Nicolas Geoffray393fdb82016-04-25 14:58:06 +01001051 StackHandleScope<2> hs(self);
1052 Handle<mirror::DexCache> dex_cache(
1053 hs.NewHandle(caller->GetDeclaringClass()->GetDexCache()));
1054 Handle<mirror::ClassLoader> class_loader(
1055 hs.NewHandle(caller->GetDeclaringClass()->GetClassLoader()));
Alex Lightfedd91d2016-01-07 14:49:16 -08001056 // TODO Maybe put this into a mirror::Class function.
1057 mirror::Class* ref_class = linker->ResolveReferencedClassOfMethod(
Nicolas Geoffray393fdb82016-04-25 14:58:06 +01001058 called_method.dex_method_index, dex_cache, class_loader);
Alex Lightfedd91d2016-01-07 14:49:16 -08001059 if (ref_class->IsInterface()) {
Andreas Gampe542451c2016-07-26 09:02:02 -07001060 called = ref_class->FindVirtualMethodForInterfaceSuper(called, kRuntimePointerSize);
Alex Lightfedd91d2016-01-07 14:49:16 -08001061 } else {
1062 called = caller->GetDeclaringClass()->GetSuperClass()->GetVTableEntry(
Andreas Gampe542451c2016-07-26 09:02:02 -07001063 called->GetMethodIndex(), kRuntimePointerSize);
Alex Lightfedd91d2016-01-07 14:49:16 -08001064 }
Mathieu Chartier55871bf2014-02-27 10:24:50 -08001065 }
Mingyao Yangf4867782014-05-05 11:55:02 -07001066
1067 CHECK(called != nullptr) << PrettyMethod(orig_called) << " "
1068 << PrettyTypeOf(receiver) << " "
1069 << invoke_type << " " << orig_called->GetVtableIndex();
1070
Ian Rogers83883d72013-10-21 21:07:24 -07001071 // 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 -08001072 // of the sharpened method avoiding dirtying the dex cache if possible.
Ian Rogers00f15272014-12-02 16:55:46 -08001073 // Note, called_method.dex_method_index references the dex method before the
1074 // FindVirtualMethodFor... This is ok for FindDexMethodIndexInOtherDexFile that only cares
1075 // about the name and signature.
1076 uint32_t update_dex_cache_method_index = called->GetDexMethodIndex();
Andreas Gampe542451c2016-07-26 09:02:02 -07001077 if (!called->HasSameDexCacheResolvedMethods(caller, kRuntimePointerSize)) {
Ian Rogers83883d72013-10-21 21:07:24 -07001078 // Calling from one dex file to another, need to compute the method index appropriate to
Vladimir Markobbcc0c02014-02-03 14:08:42 +00001079 // the caller's dex file. Since we get here only if the original called was a runtime
1080 // method, we've got the correct dex_file and a dex_method_idx from above.
Ian Rogerse0a02da2014-12-02 14:10:53 -08001081 DCHECK(!called_method_known_on_entry);
1082 DCHECK_EQ(caller->GetDexFile(), called_method.dex_file);
1083 const DexFile* caller_dex_file = called_method.dex_file;
1084 uint32_t caller_method_name_and_sig_index = called_method.dex_method_index;
1085 update_dex_cache_method_index =
1086 called->FindDexMethodIndexInOtherDexFile(*caller_dex_file,
1087 caller_method_name_and_sig_index);
1088 }
1089 if ((update_dex_cache_method_index != DexFile::kDexNoIndex) &&
Mathieu Chartiere401d142015-04-22 13:56:20 -07001090 (caller->GetDexCacheResolvedMethod(
Andreas Gampe542451c2016-07-26 09:02:02 -07001091 update_dex_cache_method_index, kRuntimePointerSize) != called)) {
1092 caller->SetDexCacheResolvedMethod(update_dex_cache_method_index,
1093 called,
1094 kRuntimePointerSize);
Ian Rogers83883d72013-10-21 21:07:24 -07001095 }
Mathieu Chartiere4a91bb2015-01-28 13:11:44 -08001096 } else if (invoke_type == kStatic) {
1097 const auto called_dex_method_idx = called->GetDexMethodIndex();
1098 // For static invokes, we may dispatch to the static method in the superclass but resolve
1099 // using the subclass. To prevent getting slow paths on each invoke, we force set the
1100 // resolved method for the super class dex method index if we are in the same dex file.
1101 // b/19175856
1102 if (called->GetDexFile() == called_method.dex_file &&
1103 called_method.dex_method_index != called_dex_method_idx) {
Andreas Gampe542451c2016-07-26 09:02:02 -07001104 called->GetDexCache()->SetResolvedMethod(called_dex_method_idx,
1105 called,
1106 kRuntimePointerSize);
Mathieu Chartiere4a91bb2015-01-28 13:11:44 -08001107 }
Ian Rogers83883d72013-10-21 21:07:24 -07001108 }
Daniel Mihalyieb076692014-08-22 17:33:31 +02001109
Ian Rogers848871b2013-08-05 10:56:33 -07001110 // Ensure that the called method's class is initialized.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001111 StackHandleScope<1> hs(soa.Self());
1112 Handle<mirror::Class> called_class(hs.NewHandle(called->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -07001113 linker->EnsureInitialized(soa.Self(), called_class, true, true);
Ian Rogers848871b2013-08-05 10:56:33 -07001114 if (LIKELY(called_class->IsInitialized())) {
Daniel Mihalyieb076692014-08-22 17:33:31 +02001115 if (UNLIKELY(Dbg::IsForcedInterpreterNeededForResolution(self, called))) {
1116 // If we are single-stepping or the called method is deoptimized (by a
1117 // breakpoint, for example), then we have to execute the called method
1118 // with the interpreter.
1119 code = GetQuickToInterpreterBridge();
1120 } else if (UNLIKELY(Dbg::IsForcedInstrumentationNeededForResolution(self, caller))) {
1121 // If the caller is deoptimized (by a breakpoint, for example), we have to
1122 // continue its execution with interpreter when returning from the called
1123 // method. Because we do not want to execute the called method with the
1124 // interpreter, we wrap its execution into the instrumentation stubs.
1125 // When the called method returns, it will execute the instrumentation
1126 // exit hook that will determine the need of the interpreter with a call
1127 // to Dbg::IsForcedInterpreterNeededForUpcall and deoptimize the stack if
1128 // it is needed.
1129 code = GetQuickInstrumentationEntryPoint();
1130 } else {
1131 code = called->GetEntryPointFromQuickCompiledCode();
1132 }
Ian Rogers848871b2013-08-05 10:56:33 -07001133 } else if (called_class->IsInitializing()) {
Daniel Mihalyieb076692014-08-22 17:33:31 +02001134 if (UNLIKELY(Dbg::IsForcedInterpreterNeededForResolution(self, called))) {
1135 // If we are single-stepping or the called method is deoptimized (by a
1136 // breakpoint, for example), then we have to execute the called method
1137 // with the interpreter.
1138 code = GetQuickToInterpreterBridge();
1139 } else if (invoke_type == kStatic) {
Ian Rogers848871b2013-08-05 10:56:33 -07001140 // Class is still initializing, go to oat and grab code (trampoline must be left in place
1141 // until class is initialized to stop races between threads).
Ian Rogersef7d42f2014-01-06 12:55:46 -08001142 code = linker->GetQuickOatCodeFor(called);
Ian Rogers848871b2013-08-05 10:56:33 -07001143 } else {
1144 // No trampoline for non-static methods.
Ian Rogersef7d42f2014-01-06 12:55:46 -08001145 code = called->GetEntryPointFromQuickCompiledCode();
Ian Rogers848871b2013-08-05 10:56:33 -07001146 }
1147 } else {
1148 DCHECK(called_class->IsErroneous());
1149 }
1150 }
Ian Rogerse0a02da2014-12-02 14:10:53 -08001151 CHECK_EQ(code == nullptr, self->IsExceptionPending());
Mathieu Chartier07d447b2013-09-26 11:57:43 -07001152 // Fixup any locally saved objects may have moved during a GC.
1153 visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -07001154 // Place called method in callee-save frame to be placed as first argument to quick method.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001155 *sp = called;
1156
Ian Rogers848871b2013-08-05 10:56:33 -07001157 return code;
1158}
1159
Andreas Gampec147b002014-03-06 18:11:06 -08001160/*
1161 * This class uses a couple of observations to unite the different calling conventions through
1162 * a few constants.
1163 *
1164 * 1) Number of registers used for passing is normally even, so counting down has no penalty for
1165 * possible alignment.
1166 * 2) Known 64b architectures store 8B units on the stack, both for integral and floating point
1167 * types, so using uintptr_t is OK. Also means that we can use kRegistersNeededX to denote
1168 * when we have to split things
1169 * 3) The only soft-float, Arm, is 32b, so no widening needs to be taken into account for floats
1170 * and we can use Int handling directly.
1171 * 4) Only 64b architectures widen, and their stack is aligned 8B anyways, so no padding code
1172 * necessary when widening. Also, widening of Ints will take place implicitly, and the
1173 * extension should be compatible with Aarch64, which mandates copying the available bits
1174 * into LSB and leaving the rest unspecified.
1175 * 5) Aligning longs and doubles is necessary on arm only, and it's the same in registers and on
1176 * the stack.
1177 * 6) There is only little endian.
1178 *
1179 *
1180 * Actual work is supposed to be done in a delegate of the template type. The interface is as
1181 * follows:
1182 *
1183 * void PushGpr(uintptr_t): Add a value for the next GPR
1184 *
1185 * void PushFpr4(float): Add a value for the next FPR of size 32b. Is only called if we need
1186 * padding, that is, think the architecture is 32b and aligns 64b.
1187 *
1188 * void PushFpr8(uint64_t): Push a double. We _will_ call this on 32b, it's the callee's job to
1189 * split this if necessary. The current state will have aligned, if
1190 * necessary.
1191 *
1192 * void PushStack(uintptr_t): Push a value to the stack.
1193 *
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001194 * uintptr_t PushHandleScope(mirror::Object* ref): Add a reference to the HandleScope. This _will_ have nullptr,
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001195 * as this might be important for null initialization.
Andreas Gampec147b002014-03-06 18:11:06 -08001196 * Must return the jobject, that is, the reference to the
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001197 * entry in the HandleScope (nullptr if necessary).
Andreas Gampec147b002014-03-06 18:11:06 -08001198 *
1199 */
Andreas Gampec200a4a2014-06-16 18:39:09 -07001200template<class T> class BuildNativeCallFrameStateMachine {
Andreas Gampec147b002014-03-06 18:11:06 -08001201 public:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001202#if defined(__arm__)
1203 // TODO: These are all dummy values!
Andreas Gampec147b002014-03-06 18:11:06 -08001204 static constexpr bool kNativeSoftFloatAbi = true;
1205 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs, r0-r3
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001206 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
1207
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001208 static constexpr size_t kRegistersNeededForLong = 2;
1209 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -08001210 static constexpr bool kMultiRegistersAligned = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001211 static constexpr bool kMultiFPRegistersWidened = false;
1212 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001213 static constexpr bool kAlignLongOnStack = true;
1214 static constexpr bool kAlignDoubleOnStack = true;
Stuart Monteithb95a5342014-03-12 13:32:32 +00001215#elif defined(__aarch64__)
1216 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
1217 static constexpr size_t kNumNativeGprArgs = 8; // 6 arguments passed in GPRs.
1218 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
1219
1220 static constexpr size_t kRegistersNeededForLong = 1;
1221 static constexpr size_t kRegistersNeededForDouble = 1;
1222 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001223 static constexpr bool kMultiFPRegistersWidened = false;
1224 static constexpr bool kMultiGPRegistersWidened = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +00001225 static constexpr bool kAlignLongOnStack = false;
1226 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001227#elif defined(__mips__) && !defined(__LP64__)
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001228 static constexpr bool kNativeSoftFloatAbi = true; // This is a hard float ABI.
Douglas Leung735b8552014-10-31 12:21:40 -07001229 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs.
1230 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001231
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001232 static constexpr size_t kRegistersNeededForLong = 2;
1233 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -08001234 static constexpr bool kMultiRegistersAligned = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001235 static constexpr bool kMultiFPRegistersWidened = true;
1236 static constexpr bool kMultiGPRegistersWidened = false;
Douglas Leung735b8552014-10-31 12:21:40 -07001237 static constexpr bool kAlignLongOnStack = true;
1238 static constexpr bool kAlignDoubleOnStack = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001239#elif defined(__mips__) && defined(__LP64__)
1240 // Let the code prepare GPRs only and we will load the FPRs with same data.
1241 static constexpr bool kNativeSoftFloatAbi = true;
1242 static constexpr size_t kNumNativeGprArgs = 8;
1243 static constexpr size_t kNumNativeFprArgs = 0;
1244
1245 static constexpr size_t kRegistersNeededForLong = 1;
1246 static constexpr size_t kRegistersNeededForDouble = 1;
1247 static constexpr bool kMultiRegistersAligned = false;
1248 static constexpr bool kMultiFPRegistersWidened = false;
1249 static constexpr bool kMultiGPRegistersWidened = true;
1250 static constexpr bool kAlignLongOnStack = false;
1251 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001252#elif defined(__i386__)
1253 // TODO: Check these!
Andreas Gampec147b002014-03-06 18:11:06 -08001254 static constexpr bool kNativeSoftFloatAbi = false; // Not using int registers for fp
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001255 static constexpr size_t kNumNativeGprArgs = 0; // 6 arguments passed in GPRs.
1256 static constexpr size_t kNumNativeFprArgs = 0; // 8 arguments passed in FPRs.
1257
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001258 static constexpr size_t kRegistersNeededForLong = 2;
1259 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001260 static constexpr bool kMultiRegistersAligned = false; // x86 not using regs, anyways
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001261 static constexpr bool kMultiFPRegistersWidened = false;
1262 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001263 static constexpr bool kAlignLongOnStack = false;
1264 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001265#elif defined(__x86_64__)
1266 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
1267 static constexpr size_t kNumNativeGprArgs = 6; // 6 arguments passed in GPRs.
1268 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
1269
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001270 static constexpr size_t kRegistersNeededForLong = 1;
1271 static constexpr size_t kRegistersNeededForDouble = 1;
Andreas Gampec147b002014-03-06 18:11:06 -08001272 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001273 static constexpr bool kMultiFPRegistersWidened = false;
1274 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001275 static constexpr bool kAlignLongOnStack = false;
1276 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001277#else
1278#error "Unsupported architecture"
1279#endif
1280
Andreas Gampec147b002014-03-06 18:11:06 -08001281 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001282 explicit BuildNativeCallFrameStateMachine(T* delegate)
1283 : gpr_index_(kNumNativeGprArgs),
1284 fpr_index_(kNumNativeFprArgs),
1285 stack_entries_(0),
1286 delegate_(delegate) {
Andreas Gampec147b002014-03-06 18:11:06 -08001287 // For register alignment, we want to assume that counters (gpr_index_, fpr_index_) are even iff
1288 // the next register is even; counting down is just to make the compiler happy...
Andreas Gampe575e78c2014-11-03 23:41:03 -08001289 static_assert(kNumNativeGprArgs % 2 == 0U, "Number of native GPR arguments not even");
1290 static_assert(kNumNativeFprArgs % 2 == 0U, "Number of native FPR arguments not even");
Andreas Gampec147b002014-03-06 18:11:06 -08001291 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001292
Andreas Gampec200a4a2014-06-16 18:39:09 -07001293 virtual ~BuildNativeCallFrameStateMachine() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001294
Ian Rogers1428dce2014-10-21 15:02:15 -07001295 bool HavePointerGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001296 return gpr_index_ > 0;
1297 }
1298
Andreas Gampec200a4a2014-06-16 18:39:09 -07001299 void AdvancePointer(const void* val) {
Andreas Gampec147b002014-03-06 18:11:06 -08001300 if (HavePointerGpr()) {
1301 gpr_index_--;
1302 PushGpr(reinterpret_cast<uintptr_t>(val));
1303 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001304 stack_entries_++; // TODO: have a field for pointer length as multiple of 32b
Andreas Gampec147b002014-03-06 18:11:06 -08001305 PushStack(reinterpret_cast<uintptr_t>(val));
1306 gpr_index_ = 0;
1307 }
1308 }
1309
Ian Rogers1428dce2014-10-21 15:02:15 -07001310 bool HaveHandleScopeGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001311 return gpr_index_ > 0;
1312 }
1313
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001314 void AdvanceHandleScope(mirror::Object* ptr) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001315 uintptr_t handle = PushHandle(ptr);
1316 if (HaveHandleScopeGpr()) {
Andreas Gampec147b002014-03-06 18:11:06 -08001317 gpr_index_--;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001318 PushGpr(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001319 } else {
1320 stack_entries_++;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001321 PushStack(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001322 gpr_index_ = 0;
1323 }
1324 }
1325
Ian Rogers1428dce2014-10-21 15:02:15 -07001326 bool HaveIntGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001327 return gpr_index_ > 0;
1328 }
1329
1330 void AdvanceInt(uint32_t val) {
1331 if (HaveIntGpr()) {
1332 gpr_index_--;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001333 if (kMultiGPRegistersWidened) {
1334 DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001335 PushGpr(static_cast<int64_t>(bit_cast<int32_t, uint32_t>(val)));
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001336 } else {
1337 PushGpr(val);
1338 }
Andreas Gampec147b002014-03-06 18:11:06 -08001339 } else {
1340 stack_entries_++;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001341 if (kMultiGPRegistersWidened) {
1342 DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001343 PushStack(static_cast<int64_t>(bit_cast<int32_t, uint32_t>(val)));
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001344 } else {
1345 PushStack(val);
1346 }
Andreas Gampec147b002014-03-06 18:11:06 -08001347 gpr_index_ = 0;
1348 }
1349 }
1350
Ian Rogers1428dce2014-10-21 15:02:15 -07001351 bool HaveLongGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001352 return gpr_index_ >= kRegistersNeededForLong + (LongGprNeedsPadding() ? 1 : 0);
1353 }
1354
Ian Rogers1428dce2014-10-21 15:02:15 -07001355 bool LongGprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001356 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1357 kAlignLongOnStack && // and when it needs alignment
1358 (gpr_index_ & 1) == 1; // counter is odd, see constructor
1359 }
1360
Ian Rogers1428dce2014-10-21 15:02:15 -07001361 bool LongStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001362 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1363 kAlignLongOnStack && // and when it needs 8B alignment
1364 (stack_entries_ & 1) == 1; // counter is odd
1365 }
1366
1367 void AdvanceLong(uint64_t val) {
1368 if (HaveLongGpr()) {
1369 if (LongGprNeedsPadding()) {
1370 PushGpr(0);
1371 gpr_index_--;
1372 }
1373 if (kRegistersNeededForLong == 1) {
1374 PushGpr(static_cast<uintptr_t>(val));
1375 } else {
1376 PushGpr(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1377 PushGpr(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1378 }
1379 gpr_index_ -= kRegistersNeededForLong;
1380 } else {
1381 if (LongStackNeedsPadding()) {
1382 PushStack(0);
1383 stack_entries_++;
1384 }
1385 if (kRegistersNeededForLong == 1) {
1386 PushStack(static_cast<uintptr_t>(val));
1387 stack_entries_++;
1388 } else {
1389 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1390 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1391 stack_entries_ += 2;
1392 }
1393 gpr_index_ = 0;
1394 }
1395 }
1396
Ian Rogers1428dce2014-10-21 15:02:15 -07001397 bool HaveFloatFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001398 return fpr_index_ > 0;
1399 }
1400
Andreas Gampec147b002014-03-06 18:11:06 -08001401 void AdvanceFloat(float val) {
1402 if (kNativeSoftFloatAbi) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001403 AdvanceInt(bit_cast<uint32_t, float>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001404 } else {
1405 if (HaveFloatFpr()) {
1406 fpr_index_--;
1407 if (kRegistersNeededForDouble == 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001408 if (kMultiFPRegistersWidened) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001409 PushFpr8(bit_cast<uint64_t, double>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001410 } else {
1411 // No widening, just use the bits.
Roland Levillainda4d79b2015-03-24 14:36:11 +00001412 PushFpr8(static_cast<uint64_t>(bit_cast<uint32_t, float>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001413 }
1414 } else {
1415 PushFpr4(val);
1416 }
1417 } else {
1418 stack_entries_++;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001419 if (kRegistersNeededForDouble == 1 && kMultiFPRegistersWidened) {
Andreas Gampec147b002014-03-06 18:11:06 -08001420 // Need to widen before storing: Note the "double" in the template instantiation.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001421 // Note: We need to jump through those hoops to make the compiler happy.
1422 DCHECK_EQ(sizeof(uintptr_t), sizeof(uint64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001423 PushStack(static_cast<uintptr_t>(bit_cast<uint64_t, double>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001424 } else {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001425 PushStack(static_cast<uintptr_t>(bit_cast<uint32_t, float>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001426 }
1427 fpr_index_ = 0;
1428 }
1429 }
1430 }
1431
Ian Rogers1428dce2014-10-21 15:02:15 -07001432 bool HaveDoubleFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001433 return fpr_index_ >= kRegistersNeededForDouble + (DoubleFprNeedsPadding() ? 1 : 0);
1434 }
1435
Ian Rogers1428dce2014-10-21 15:02:15 -07001436 bool DoubleFprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001437 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1438 kAlignDoubleOnStack && // and when it needs alignment
1439 (fpr_index_ & 1) == 1; // counter is odd, see constructor
1440 }
1441
Ian Rogers1428dce2014-10-21 15:02:15 -07001442 bool DoubleStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001443 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1444 kAlignDoubleOnStack && // and when it needs 8B alignment
1445 (stack_entries_ & 1) == 1; // counter is odd
1446 }
1447
1448 void AdvanceDouble(uint64_t val) {
1449 if (kNativeSoftFloatAbi) {
1450 AdvanceLong(val);
1451 } else {
1452 if (HaveDoubleFpr()) {
1453 if (DoubleFprNeedsPadding()) {
1454 PushFpr4(0);
1455 fpr_index_--;
1456 }
1457 PushFpr8(val);
1458 fpr_index_ -= kRegistersNeededForDouble;
1459 } else {
1460 if (DoubleStackNeedsPadding()) {
1461 PushStack(0);
1462 stack_entries_++;
1463 }
1464 if (kRegistersNeededForDouble == 1) {
1465 PushStack(static_cast<uintptr_t>(val));
1466 stack_entries_++;
1467 } else {
1468 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1469 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1470 stack_entries_ += 2;
1471 }
1472 fpr_index_ = 0;
1473 }
1474 }
1475 }
1476
Ian Rogers1428dce2014-10-21 15:02:15 -07001477 uint32_t GetStackEntries() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001478 return stack_entries_;
1479 }
1480
Ian Rogers1428dce2014-10-21 15:02:15 -07001481 uint32_t GetNumberOfUsedGprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001482 return kNumNativeGprArgs - gpr_index_;
1483 }
1484
Ian Rogers1428dce2014-10-21 15:02:15 -07001485 uint32_t GetNumberOfUsedFprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001486 return kNumNativeFprArgs - fpr_index_;
1487 }
1488
1489 private:
1490 void PushGpr(uintptr_t val) {
1491 delegate_->PushGpr(val);
1492 }
1493 void PushFpr4(float val) {
1494 delegate_->PushFpr4(val);
1495 }
1496 void PushFpr8(uint64_t val) {
1497 delegate_->PushFpr8(val);
1498 }
1499 void PushStack(uintptr_t val) {
1500 delegate_->PushStack(val);
1501 }
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001502 uintptr_t PushHandle(mirror::Object* ref) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001503 return delegate_->PushHandle(ref);
Andreas Gampec147b002014-03-06 18:11:06 -08001504 }
1505
1506 uint32_t gpr_index_; // Number of free GPRs
1507 uint32_t fpr_index_; // Number of free FPRs
1508 uint32_t stack_entries_; // Stack entries are in multiples of 32b, as floats are usually not
1509 // extended
Ian Rogers1428dce2014-10-21 15:02:15 -07001510 T* const delegate_; // What Push implementation gets called
Andreas Gampec147b002014-03-06 18:11:06 -08001511};
1512
Andreas Gampec200a4a2014-06-16 18:39:09 -07001513// Computes the sizes of register stacks and call stack area. Handling of references can be extended
1514// in subclasses.
1515//
1516// To handle native pointers, use "L" in the shorty for an object reference, which simulates
1517// them with handles.
1518class ComputeNativeCallFrameSize {
Andreas Gampec147b002014-03-06 18:11:06 -08001519 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001520 ComputeNativeCallFrameSize() : num_stack_entries_(0) {}
1521
1522 virtual ~ComputeNativeCallFrameSize() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001523
Ian Rogers1428dce2014-10-21 15:02:15 -07001524 uint32_t GetStackSize() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001525 return num_stack_entries_ * sizeof(uintptr_t);
1526 }
1527
Ian Rogers1428dce2014-10-21 15:02:15 -07001528 uint8_t* LayoutCallStack(uint8_t* sp8) const {
Andreas Gampec147b002014-03-06 18:11:06 -08001529 sp8 -= GetStackSize();
Andreas Gampe779f8c92014-06-09 18:29:38 -07001530 // Align by kStackAlignment.
1531 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
Andreas Gampec200a4a2014-06-16 18:39:09 -07001532 return sp8;
Andreas Gampec147b002014-03-06 18:11:06 -08001533 }
1534
Ian Rogers1428dce2014-10-21 15:02:15 -07001535 uint8_t* LayoutCallRegisterStacks(uint8_t* sp8, uintptr_t** start_gpr, uint32_t** start_fpr)
1536 const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001537 // Assumption is OK right now, as we have soft-float arm
1538 size_t fregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeFprArgs;
1539 sp8 -= fregs * sizeof(uintptr_t);
1540 *start_fpr = reinterpret_cast<uint32_t*>(sp8);
1541 size_t iregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeGprArgs;
1542 sp8 -= iregs * sizeof(uintptr_t);
1543 *start_gpr = reinterpret_cast<uintptr_t*>(sp8);
1544 return sp8;
1545 }
Andreas Gampec147b002014-03-06 18:11:06 -08001546
Andreas Gampec200a4a2014-06-16 18:39:09 -07001547 uint8_t* LayoutNativeCall(uint8_t* sp8, uintptr_t** start_stack, uintptr_t** start_gpr,
Ian Rogers1428dce2014-10-21 15:02:15 -07001548 uint32_t** start_fpr) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001549 // Native call stack.
1550 sp8 = LayoutCallStack(sp8);
1551 *start_stack = reinterpret_cast<uintptr_t*>(sp8);
Andreas Gampec147b002014-03-06 18:11:06 -08001552
Andreas Gampec200a4a2014-06-16 18:39:09 -07001553 // Put fprs and gprs below.
1554 sp8 = LayoutCallRegisterStacks(sp8, start_gpr, start_fpr);
Andreas Gampec147b002014-03-06 18:11:06 -08001555
Andreas Gampec200a4a2014-06-16 18:39:09 -07001556 // Return the new bottom.
1557 return sp8;
1558 }
1559
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001560 virtual void WalkHeader(
1561 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm ATTRIBUTE_UNUSED)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001562 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001563 }
Andreas Gampec200a4a2014-06-16 18:39:09 -07001564
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001565 void Walk(const char* shorty, uint32_t shorty_len) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001566 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize> sm(this);
1567
1568 WalkHeader(&sm);
Andreas Gampec147b002014-03-06 18:11:06 -08001569
1570 for (uint32_t i = 1; i < shorty_len; ++i) {
1571 Primitive::Type cur_type_ = Primitive::GetType(shorty[i]);
1572 switch (cur_type_) {
1573 case Primitive::kPrimNot:
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001574 // TODO: fix abuse of mirror types.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001575 sm.AdvanceHandleScope(
1576 reinterpret_cast<mirror::Object*>(0x12345678));
Andreas Gampec147b002014-03-06 18:11:06 -08001577 break;
1578
1579 case Primitive::kPrimBoolean:
1580 case Primitive::kPrimByte:
1581 case Primitive::kPrimChar:
1582 case Primitive::kPrimShort:
1583 case Primitive::kPrimInt:
1584 sm.AdvanceInt(0);
1585 break;
1586 case Primitive::kPrimFloat:
1587 sm.AdvanceFloat(0);
1588 break;
1589 case Primitive::kPrimDouble:
1590 sm.AdvanceDouble(0);
1591 break;
1592 case Primitive::kPrimLong:
1593 sm.AdvanceLong(0);
1594 break;
1595 default:
1596 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001597 UNREACHABLE();
Andreas Gampec147b002014-03-06 18:11:06 -08001598 }
1599 }
1600
Ian Rogers1428dce2014-10-21 15:02:15 -07001601 num_stack_entries_ = sm.GetStackEntries();
Andreas Gampec147b002014-03-06 18:11:06 -08001602 }
1603
1604 void PushGpr(uintptr_t /* val */) {
1605 // not optimizing registers, yet
1606 }
1607
1608 void PushFpr4(float /* val */) {
1609 // not optimizing registers, yet
1610 }
1611
1612 void PushFpr8(uint64_t /* val */) {
1613 // not optimizing registers, yet
1614 }
1615
1616 void PushStack(uintptr_t /* val */) {
1617 // counting is already done in the superclass
1618 }
1619
Andreas Gampec200a4a2014-06-16 18:39:09 -07001620 virtual uintptr_t PushHandle(mirror::Object* /* ptr */) {
Andreas Gampec147b002014-03-06 18:11:06 -08001621 return reinterpret_cast<uintptr_t>(nullptr);
1622 }
1623
Andreas Gampec200a4a2014-06-16 18:39:09 -07001624 protected:
Andreas Gampec147b002014-03-06 18:11:06 -08001625 uint32_t num_stack_entries_;
1626};
1627
Andreas Gampec200a4a2014-06-16 18:39:09 -07001628class ComputeGenericJniFrameSize FINAL : public ComputeNativeCallFrameSize {
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001629 public:
Igor Murashkin06a04e02016-09-13 15:57:37 -07001630 explicit ComputeGenericJniFrameSize(bool critical_native)
1631 : num_handle_scope_references_(0), critical_native_(critical_native) {}
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001632
Andreas Gampec200a4a2014-06-16 18:39:09 -07001633 // Lays out the callee-save frame. Assumes that the incorrect frame corresponding to RefsAndArgs
1634 // is at *m = sp. Will update to point to the bottom of the save frame.
1635 //
1636 // Note: assumes ComputeAll() has been run before.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001637 void LayoutCalleeSaveFrame(Thread* self, ArtMethod*** m, void* sp, HandleScope** handle_scope)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001638 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001639 ArtMethod* method = **m;
1640
Andreas Gampe542451c2016-07-26 09:02:02 -07001641 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001642
Andreas Gampec200a4a2014-06-16 18:39:09 -07001643 uint8_t* sp8 = reinterpret_cast<uint8_t*>(sp);
1644
1645 // First, fix up the layout of the callee-save frame.
1646 // We have to squeeze in the HandleScope, and relocate the method pointer.
1647
1648 // "Free" the slot for the method.
Ian Rogers13735952014-10-08 12:43:28 -07001649 sp8 += sizeof(void*); // In the callee-save frame we use a full pointer.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001650
1651 // Under the callee saves put handle scope and new method stack reference.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001652 size_t handle_scope_size = HandleScope::SizeOf(num_handle_scope_references_);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001653 size_t scope_and_method = handle_scope_size + sizeof(ArtMethod*);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001654
1655 sp8 -= scope_and_method;
1656 // Align by kStackAlignment.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001657 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
Andreas Gampec200a4a2014-06-16 18:39:09 -07001658
Mathieu Chartiere401d142015-04-22 13:56:20 -07001659 uint8_t* sp8_table = sp8 + sizeof(ArtMethod*);
Ian Rogers59c07062014-10-10 13:03:39 -07001660 *handle_scope = HandleScope::Create(sp8_table, self->GetTopHandleScope(),
1661 num_handle_scope_references_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001662
1663 // Add a slot for the method pointer, and fill it. Fix the pointer-pointer given to us.
1664 uint8_t* method_pointer = sp8;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001665 auto** new_method_ref = reinterpret_cast<ArtMethod**>(method_pointer);
1666 *new_method_ref = method;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001667 *m = new_method_ref;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001668 }
1669
Andreas Gampec200a4a2014-06-16 18:39:09 -07001670 // Adds space for the cookie. Note: may leave stack unaligned.
Ian Rogers1428dce2014-10-21 15:02:15 -07001671 void LayoutCookie(uint8_t** sp) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001672 // Reference cookie and padding
1673 *sp -= 8;
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001674 }
1675
Andreas Gampec200a4a2014-06-16 18:39:09 -07001676 // Re-layout the callee-save frame (insert a handle-scope). Then add space for the cookie.
1677 // Returns the new bottom. Note: this may be unaligned.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001678 uint8_t* LayoutJNISaveFrame(Thread* self, ArtMethod*** m, void* sp, HandleScope** handle_scope)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001679 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001680 // First, fix up the layout of the callee-save frame.
1681 // We have to squeeze in the HandleScope, and relocate the method pointer.
Ian Rogers59c07062014-10-10 13:03:39 -07001682 LayoutCalleeSaveFrame(self, m, sp, handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001683
1684 // The bottom of the callee-save frame is now where the method is, *m.
1685 uint8_t* sp8 = reinterpret_cast<uint8_t*>(*m);
1686
1687 // Add space for cookie.
1688 LayoutCookie(&sp8);
1689
1690 return sp8;
1691 }
1692
1693 // WARNING: After this, *sp won't be pointing to the method anymore!
Mathieu Chartiere401d142015-04-22 13:56:20 -07001694 uint8_t* ComputeLayout(Thread* self, ArtMethod*** m, const char* shorty, uint32_t shorty_len,
1695 HandleScope** handle_scope, uintptr_t** start_stack, uintptr_t** start_gpr,
1696 uint32_t** start_fpr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001697 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001698 Walk(shorty, shorty_len);
1699
1700 // JNI part.
Ian Rogers59c07062014-10-10 13:03:39 -07001701 uint8_t* sp8 = LayoutJNISaveFrame(self, m, reinterpret_cast<void*>(*m), handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001702
1703 sp8 = LayoutNativeCall(sp8, start_stack, start_gpr, start_fpr);
1704
1705 // Return the new bottom.
1706 return sp8;
1707 }
1708
1709 uintptr_t PushHandle(mirror::Object* /* ptr */) OVERRIDE;
1710
1711 // Add JNIEnv* and jobj/jclass before the shorty-derived elements.
1712 void WalkHeader(BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) OVERRIDE
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001713 REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001714
1715 private:
1716 uint32_t num_handle_scope_references_;
Igor Murashkin06a04e02016-09-13 15:57:37 -07001717 const bool critical_native_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001718};
1719
1720uintptr_t ComputeGenericJniFrameSize::PushHandle(mirror::Object* /* ptr */) {
1721 num_handle_scope_references_++;
1722 return reinterpret_cast<uintptr_t>(nullptr);
1723}
1724
1725void ComputeGenericJniFrameSize::WalkHeader(
1726 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) {
Igor Murashkin06a04e02016-09-13 15:57:37 -07001727 // First 2 parameters are always excluded for @CriticalNative.
1728 if (UNLIKELY(critical_native_)) {
1729 return;
1730 }
1731
Andreas Gampec200a4a2014-06-16 18:39:09 -07001732 // JNIEnv
1733 sm->AdvancePointer(nullptr);
1734
1735 // Class object or this as first argument
1736 sm->AdvanceHandleScope(reinterpret_cast<mirror::Object*>(0x12345678));
1737}
1738
1739// Class to push values to three separate regions. Used to fill the native call part. Adheres to
1740// the template requirements of BuildGenericJniFrameStateMachine.
1741class FillNativeCall {
1742 public:
1743 FillNativeCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) :
1744 cur_gpr_reg_(gpr_regs), cur_fpr_reg_(fpr_regs), cur_stack_arg_(stack_args) {}
1745
1746 virtual ~FillNativeCall() {}
1747
1748 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) {
1749 cur_gpr_reg_ = gpr_regs;
1750 cur_fpr_reg_ = fpr_regs;
1751 cur_stack_arg_ = stack_args;
Andreas Gampec147b002014-03-06 18:11:06 -08001752 }
1753
1754 void PushGpr(uintptr_t val) {
1755 *cur_gpr_reg_ = val;
1756 cur_gpr_reg_++;
1757 }
1758
1759 void PushFpr4(float val) {
1760 *cur_fpr_reg_ = val;
1761 cur_fpr_reg_++;
1762 }
1763
1764 void PushFpr8(uint64_t val) {
1765 uint64_t* tmp = reinterpret_cast<uint64_t*>(cur_fpr_reg_);
1766 *tmp = val;
1767 cur_fpr_reg_ += 2;
1768 }
1769
1770 void PushStack(uintptr_t val) {
1771 *cur_stack_arg_ = val;
1772 cur_stack_arg_++;
1773 }
1774
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001775 virtual uintptr_t PushHandle(mirror::Object*) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001776 LOG(FATAL) << "(Non-JNI) Native call does not use handles.";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001777 UNREACHABLE();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001778 }
1779
1780 private:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001781 uintptr_t* cur_gpr_reg_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001782 uint32_t* cur_fpr_reg_;
1783 uintptr_t* cur_stack_arg_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001784};
Andreas Gampec147b002014-03-06 18:11:06 -08001785
Andreas Gampec200a4a2014-06-16 18:39:09 -07001786// Visits arguments on the stack placing them into a region lower down the stack for the benefit
1787// of transitioning into native code.
1788class BuildGenericJniFrameVisitor FINAL : public QuickArgumentVisitor {
1789 public:
Igor Murashkin06a04e02016-09-13 15:57:37 -07001790 BuildGenericJniFrameVisitor(Thread* self,
1791 bool is_static,
1792 bool critical_native,
1793 const char* shorty,
1794 uint32_t shorty_len,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001795 ArtMethod*** sp)
Andreas Gampec200a4a2014-06-16 18:39:09 -07001796 : QuickArgumentVisitor(*sp, is_static, shorty, shorty_len),
Igor Murashkin06a04e02016-09-13 15:57:37 -07001797 jni_call_(nullptr, nullptr, nullptr, nullptr, critical_native),
1798 sm_(&jni_call_) {
1799 ComputeGenericJniFrameSize fsc(critical_native);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001800 uintptr_t* start_gpr_reg;
1801 uint32_t* start_fpr_reg;
1802 uintptr_t* start_stack_arg;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001803 bottom_of_used_area_ = fsc.ComputeLayout(self, sp, shorty, shorty_len,
Ian Rogers59c07062014-10-10 13:03:39 -07001804 &handle_scope_,
1805 &start_stack_arg,
Andreas Gampec200a4a2014-06-16 18:39:09 -07001806 &start_gpr_reg, &start_fpr_reg);
1807
Andreas Gampec200a4a2014-06-16 18:39:09 -07001808 jni_call_.Reset(start_gpr_reg, start_fpr_reg, start_stack_arg, handle_scope_);
1809
Igor Murashkin06a04e02016-09-13 15:57:37 -07001810 // First 2 parameters are always excluded for CriticalNative methods.
1811 if (LIKELY(!critical_native)) {
1812 // jni environment is always first argument
1813 sm_.AdvancePointer(self->GetJniEnv());
Andreas Gampec200a4a2014-06-16 18:39:09 -07001814
Igor Murashkin06a04e02016-09-13 15:57:37 -07001815 if (is_static) {
1816 sm_.AdvanceHandleScope((**sp)->GetDeclaringClass());
1817 } // else "this" reference is already handled by QuickArgumentVisitor.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001818 }
1819 }
1820
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001821 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001822
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001823 void FinalizeHandleScope(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001824
Vladimir Markof39745e2016-01-26 12:16:55 +00001825 StackReference<mirror::Object>* GetFirstHandleScopeEntry() {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001826 return handle_scope_->GetHandle(0).GetReference();
1827 }
1828
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001829 jobject GetFirstHandleScopeJObject() const REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001830 return handle_scope_->GetHandle(0).ToJObject();
1831 }
1832
Ian Rogers1428dce2014-10-21 15:02:15 -07001833 void* GetBottomOfUsedArea() const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001834 return bottom_of_used_area_;
1835 }
1836
1837 private:
1838 // A class to fill a JNI call. Adds reference/handle-scope management to FillNativeCall.
1839 class FillJniCall FINAL : public FillNativeCall {
1840 public:
1841 FillJniCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args,
Igor Murashkin06a04e02016-09-13 15:57:37 -07001842 HandleScope* handle_scope, bool critical_native)
1843 : FillNativeCall(gpr_regs, fpr_regs, stack_args),
1844 handle_scope_(handle_scope),
1845 cur_entry_(0),
1846 critical_native_(critical_native) {}
Andreas Gampec200a4a2014-06-16 18:39:09 -07001847
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001848 uintptr_t PushHandle(mirror::Object* ref) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001849
1850 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args, HandleScope* scope) {
1851 FillNativeCall::Reset(gpr_regs, fpr_regs, stack_args);
1852 handle_scope_ = scope;
1853 cur_entry_ = 0U;
1854 }
1855
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001856 void ResetRemainingScopeSlots() REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001857 // Initialize padding entries.
1858 size_t expected_slots = handle_scope_->NumberOfReferences();
1859 while (cur_entry_ < expected_slots) {
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07001860 handle_scope_->GetMutableHandle(cur_entry_++).Assign(nullptr);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001861 }
Igor Murashkin06a04e02016-09-13 15:57:37 -07001862
1863 if (!critical_native_) {
1864 // Non-critical natives have at least the self class (jclass) or this (jobject).
1865 DCHECK_NE(cur_entry_, 0U);
1866 }
Andreas Gampec200a4a2014-06-16 18:39:09 -07001867 }
1868
Mathieu Chartier1432a5b2016-10-04 15:41:42 -07001869 bool CriticalNative() const {
1870 return critical_native_;
1871 }
1872
Andreas Gampec200a4a2014-06-16 18:39:09 -07001873 private:
1874 HandleScope* handle_scope_;
1875 size_t cur_entry_;
Igor Murashkin06a04e02016-09-13 15:57:37 -07001876 const bool critical_native_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001877 };
1878
1879 HandleScope* handle_scope_;
1880 FillJniCall jni_call_;
1881 void* bottom_of_used_area_;
1882
1883 BuildNativeCallFrameStateMachine<FillJniCall> sm_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001884
1885 DISALLOW_COPY_AND_ASSIGN(BuildGenericJniFrameVisitor);
1886};
1887
Andreas Gampec200a4a2014-06-16 18:39:09 -07001888uintptr_t BuildGenericJniFrameVisitor::FillJniCall::PushHandle(mirror::Object* ref) {
1889 uintptr_t tmp;
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07001890 MutableHandle<mirror::Object> h = handle_scope_->GetMutableHandle(cur_entry_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001891 h.Assign(ref);
1892 tmp = reinterpret_cast<uintptr_t>(h.ToJObject());
1893 cur_entry_++;
1894 return tmp;
1895}
1896
Ian Rogers9758f792014-03-13 09:02:55 -07001897void BuildGenericJniFrameVisitor::Visit() {
1898 Primitive::Type type = GetParamPrimitiveType();
1899 switch (type) {
1900 case Primitive::kPrimLong: {
1901 jlong long_arg;
1902 if (IsSplitLongOrDouble()) {
1903 long_arg = ReadSplitLongParam();
1904 } else {
1905 long_arg = *reinterpret_cast<jlong*>(GetParamAddress());
1906 }
1907 sm_.AdvanceLong(long_arg);
1908 break;
1909 }
1910 case Primitive::kPrimDouble: {
1911 uint64_t double_arg;
1912 if (IsSplitLongOrDouble()) {
1913 // Read into union so that we don't case to a double.
1914 double_arg = ReadSplitLongParam();
1915 } else {
1916 double_arg = *reinterpret_cast<uint64_t*>(GetParamAddress());
1917 }
1918 sm_.AdvanceDouble(double_arg);
1919 break;
1920 }
1921 case Primitive::kPrimNot: {
1922 StackReference<mirror::Object>* stack_ref =
1923 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001924 sm_.AdvanceHandleScope(stack_ref->AsMirrorPtr());
Ian Rogers9758f792014-03-13 09:02:55 -07001925 break;
1926 }
1927 case Primitive::kPrimFloat:
1928 sm_.AdvanceFloat(*reinterpret_cast<float*>(GetParamAddress()));
1929 break;
1930 case Primitive::kPrimBoolean: // Fall-through.
1931 case Primitive::kPrimByte: // Fall-through.
1932 case Primitive::kPrimChar: // Fall-through.
1933 case Primitive::kPrimShort: // Fall-through.
1934 case Primitive::kPrimInt: // Fall-through.
1935 sm_.AdvanceInt(*reinterpret_cast<jint*>(GetParamAddress()));
1936 break;
1937 case Primitive::kPrimVoid:
1938 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -07001939 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -07001940 }
1941}
1942
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001943void BuildGenericJniFrameVisitor::FinalizeHandleScope(Thread* self) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001944 // Clear out rest of the scope.
1945 jni_call_.ResetRemainingScopeSlots();
Mathieu Chartier1432a5b2016-10-04 15:41:42 -07001946 if (!jni_call_.CriticalNative()) {
1947 // Install HandleScope.
1948 self->PushHandleScope(handle_scope_);
1949 }
Ian Rogers9758f792014-03-13 09:02:55 -07001950}
1951
Ian Rogers04c31d22014-07-07 21:44:06 -07001952#if defined(__arm__) || defined(__aarch64__)
Andreas Gampe90546832014-03-12 18:07:19 -07001953extern "C" void* artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07001954#else
1955extern "C" void* artFindNativeMethod(Thread* self);
1956#endif
Andreas Gampe90546832014-03-12 18:07:19 -07001957
Igor Murashkin06a04e02016-09-13 15:57:37 -07001958static uint64_t artQuickGenericJniEndJNIRef(Thread* self,
1959 uint32_t cookie,
1960 bool fast_native ATTRIBUTE_UNUSED,
1961 jobject l,
1962 jobject lock) {
1963 // TODO: add entrypoints for @FastNative returning objects.
Andreas Gampead615172014-04-04 16:20:13 -07001964 if (lock != nullptr) {
1965 return reinterpret_cast<uint64_t>(JniMethodEndWithReferenceSynchronized(l, cookie, lock, self));
1966 } else {
1967 return reinterpret_cast<uint64_t>(JniMethodEndWithReference(l, cookie, self));
1968 }
1969}
1970
Igor Murashkin06a04e02016-09-13 15:57:37 -07001971static void artQuickGenericJniEndJNINonRef(Thread* self,
1972 uint32_t cookie,
1973 bool fast_native,
1974 jobject lock) {
Andreas Gampead615172014-04-04 16:20:13 -07001975 if (lock != nullptr) {
1976 JniMethodEndSynchronized(cookie, lock, self);
Igor Murashkin06a04e02016-09-13 15:57:37 -07001977 // Ignore "fast_native" here because synchronized functions aren't very fast.
Andreas Gampead615172014-04-04 16:20:13 -07001978 } else {
Igor Murashkin06a04e02016-09-13 15:57:37 -07001979 if (UNLIKELY(fast_native)) {
1980 JniMethodFastEnd(cookie, self);
1981 } else {
1982 JniMethodEnd(cookie, self);
1983 }
Andreas Gampead615172014-04-04 16:20:13 -07001984 }
1985}
1986
Andreas Gampec147b002014-03-06 18:11:06 -08001987/*
1988 * Initializes an alloca region assumed to be directly below sp for a native call:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001989 * 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 -08001990 * The final element on the stack is a pointer to the native code.
1991 *
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001992 * On entry, the stack has a standard callee-save frame above sp, and an alloca below it.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001993 * We need to fix this, as the handle scope needs to go into the callee-save frame.
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001994 *
Andreas Gampec147b002014-03-06 18:11:06 -08001995 * The return of this function denotes:
1996 * 1) How many bytes of the alloca can be released, if the value is non-negative.
1997 * 2) An error, if the value is negative.
1998 */
Mathieu Chartiere401d142015-04-22 13:56:20 -07001999extern "C" TwoWordReturn artQuickGenericJniTrampoline(Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002000 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002001 ArtMethod* called = *sp;
Andreas Gampe36fea8d2014-03-10 13:37:40 -07002002 DCHECK(called->IsNative()) << PrettyMethod(called, true);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002003 uint32_t shorty_len = 0;
2004 const char* shorty = called->GetShorty(&shorty_len);
Igor Murashkin06a04e02016-09-13 15:57:37 -07002005 bool critical_native = called->IsAnnotatedWithCriticalNative();
2006 bool fast_native = called->IsAnnotatedWithFastNative();
2007 bool normal_native = !critical_native && !fast_native;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002008
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002009 // Run the visitor and update sp.
Igor Murashkin06a04e02016-09-13 15:57:37 -07002010 BuildGenericJniFrameVisitor visitor(self,
2011 called->IsStatic(),
2012 critical_native,
2013 shorty,
2014 shorty_len,
2015 &sp);
Mathieu Chartierbe08cf52016-09-13 13:41:24 -07002016 {
2017 ScopedAssertNoThreadSuspension sants(__FUNCTION__);
2018 visitor.VisitArguments();
2019 // FinalizeHandleScope pushes the handle scope on the thread.
2020 visitor.FinalizeHandleScope(self);
2021 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002022
Andreas Gampec200a4a2014-06-16 18:39:09 -07002023 // Fix up managed-stack things in Thread.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002024 self->SetTopOfStack(sp);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002025
Ian Rogerse0dcd462014-03-08 15:21:04 -08002026 self->VerifyStack();
2027
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002028 uint32_t cookie;
Igor Murashkin06a04e02016-09-13 15:57:37 -07002029 uint32_t* sp32;
2030 // Skip calling JniMethodStart for @CriticalNative.
2031 if (LIKELY(!critical_native)) {
2032 // Start JNI, save the cookie.
2033 if (called->IsSynchronized()) {
2034 DCHECK(normal_native) << " @FastNative and synchronize is not supported";
2035 cookie = JniMethodStartSynchronized(visitor.GetFirstHandleScopeJObject(), self);
2036 if (self->IsExceptionPending()) {
2037 self->PopHandleScope();
2038 // A negative value denotes an error.
2039 return GetTwoWordFailureValue();
2040 }
2041 } else {
2042 if (fast_native) {
2043 cookie = JniMethodFastStart(self);
2044 } else {
2045 DCHECK(normal_native);
2046 cookie = JniMethodStart(self);
2047 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002048 }
Igor Murashkin06a04e02016-09-13 15:57:37 -07002049 sp32 = reinterpret_cast<uint32_t*>(sp);
2050 *(sp32 - 1) = cookie;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002051 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002052
Andreas Gampe90546832014-03-12 18:07:19 -07002053 // Retrieve the stored native code.
Mathieu Chartier2d721012014-11-10 11:08:06 -08002054 void* nativeCode = called->GetEntryPointFromJni();
Andreas Gampe90546832014-03-12 18:07:19 -07002055
Andreas Gampe9a6a99a2014-03-14 07:52:20 -07002056 // There are two cases for the content of nativeCode:
2057 // 1) Pointer to the native function.
2058 // 2) Pointer to the trampoline for native code binding.
2059 // In the second case, we need to execute the binding and continue with the actual native function
2060 // pointer.
Andreas Gampe90546832014-03-12 18:07:19 -07002061 DCHECK(nativeCode != nullptr);
2062 if (nativeCode == GetJniDlsymLookupStub()) {
Ian Rogers04c31d22014-07-07 21:44:06 -07002063#if defined(__arm__) || defined(__aarch64__)
Andreas Gampe90546832014-03-12 18:07:19 -07002064 nativeCode = artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07002065#else
2066 nativeCode = artFindNativeMethod(self);
2067#endif
Andreas Gampe90546832014-03-12 18:07:19 -07002068
2069 if (nativeCode == nullptr) {
2070 DCHECK(self->IsExceptionPending()); // There should be an exception pending now.
Andreas Gampead615172014-04-04 16:20:13 -07002071
Igor Murashkin06a04e02016-09-13 15:57:37 -07002072 // @CriticalNative calls do not need to call back into JniMethodEnd.
2073 if (LIKELY(!critical_native)) {
2074 // End JNI, as the assembly will move to deliver the exception.
2075 jobject lock = called->IsSynchronized() ? visitor.GetFirstHandleScopeJObject() : nullptr;
2076 if (shorty[0] == 'L') {
2077 artQuickGenericJniEndJNIRef(self, cookie, fast_native, nullptr, lock);
2078 } else {
2079 artQuickGenericJniEndJNINonRef(self, cookie, fast_native, lock);
2080 }
Andreas Gampead615172014-04-04 16:20:13 -07002081 }
2082
Andreas Gampec200a4a2014-06-16 18:39:09 -07002083 return GetTwoWordFailureValue();
Andreas Gampe90546832014-03-12 18:07:19 -07002084 }
2085 // Note that the native code pointer will be automatically set by artFindNativeMethod().
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002086 }
2087
Andreas Gampec200a4a2014-06-16 18:39:09 -07002088 // Return native code addr(lo) and bottom of alloca address(hi).
2089 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(visitor.GetBottomOfUsedArea()),
2090 reinterpret_cast<uintptr_t>(nativeCode));
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002091}
2092
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002093// Defined in quick_jni_entrypoints.cc.
2094extern uint64_t GenericJniMethodEnd(Thread* self, uint32_t saved_local_ref_cookie,
2095 jvalue result, uint64_t result_f, ArtMethod* called,
2096 HandleScope* handle_scope);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002097/*
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002098 * Is called after the native JNI code. Responsible for cleanup (handle scope, saved state) and
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002099 * unlocking.
2100 */
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002101extern "C" uint64_t artQuickGenericJniEndTrampoline(Thread* self,
2102 jvalue result,
2103 uint64_t result_f) {
2104 // We're here just back from a native call. We don't have the shared mutator lock at this point
2105 // yet until we call GoToRunnable() later in GenericJniMethodEnd(). Accessing objects or doing
2106 // anything that requires a mutator lock before that would cause problems as GC may have the
2107 // exclusive mutator lock and may be moving objects, etc.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002108 ArtMethod** sp = self->GetManagedStack()->GetTopQuickFrame();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002109 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002110 ArtMethod* called = *sp;
Ian Rogerse0dcd462014-03-08 15:21:04 -08002111 uint32_t cookie = *(sp32 - 1);
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002112 HandleScope* table = reinterpret_cast<HandleScope*>(reinterpret_cast<uint8_t*>(sp) + sizeof(*sp));
2113 return GenericJniMethodEnd(self, cookie, result, result_f, called, table);
Andreas Gampe2da88232014-02-27 12:26:20 -08002114}
2115
Andreas Gamped58342c2014-06-05 14:18:08 -07002116// We use TwoWordReturn to optimize scalar returns. We use the hi value for code, and the lo value
2117// for the method pointer.
Andreas Gampe51f76352014-05-21 08:28:48 -07002118//
Andreas Gamped58342c2014-06-05 14:18:08 -07002119// 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 -07002120// to hold the mutator lock (see REQUIRES_SHARED(Locks::mutator_lock_) annotations).
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002121
2122template<InvokeType type, bool access_check>
Mathieu Chartiere401d142015-04-22 13:56:20 -07002123static TwoWordReturn artInvokeCommon(uint32_t method_idx, mirror::Object* this_object, Thread* self,
2124 ArtMethod** sp) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002125 ScopedQuickEntrypointChecks sqec(self);
Vladimir Markofd36f1f2016-08-03 18:49:58 +01002126 DCHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(Runtime::kSaveRefsAndArgs));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002127 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
2128 ArtMethod* method = FindMethodFast(method_idx, this_object, caller_method, access_check, type);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002129 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002130 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()->GetDexFile();
2131 uint32_t shorty_len;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002132 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(method_idx), &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002133 {
2134 // Remember the args in case a GC happens in FindMethodFromCode.
2135 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
2136 RememberForGcArgumentVisitor visitor(sp, type == kStatic, shorty, shorty_len, &soa);
2137 visitor.VisitArguments();
Andreas Gampe3a357142015-08-07 17:20:11 -07002138 method = FindMethodFromCode<type, access_check>(method_idx, &this_object, caller_method,
Mathieu Chartier0cd81352014-05-22 16:48:55 -07002139 self);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002140 visitor.FixupReferences();
2141 }
2142
Ian Rogerse0a02da2014-12-02 14:10:53 -08002143 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002144 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07002145 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002146 }
2147 }
2148 DCHECK(!self->IsExceptionPending());
2149 const void* code = method->GetEntryPointFromQuickCompiledCode();
2150
2151 // When we return, the caller will branch to this address, so it had better not be 0!
Ian Rogerse0a02da2014-12-02 14:10:53 -08002152 DCHECK(code != nullptr) << "Code was null in method: " << PrettyMethod(method)
Andreas Gampec200a4a2014-06-16 18:39:09 -07002153 << " location: "
2154 << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07002155
Andreas Gamped58342c2014-06-05 14:18:08 -07002156 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
2157 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002158}
2159
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01002160// Explicit artInvokeCommon template function declarations to please analysis tool.
2161#define EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(type, access_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002162 template REQUIRES_SHARED(Locks::mutator_lock_) \
Mathieu Chartiere401d142015-04-22 13:56:20 -07002163 TwoWordReturn artInvokeCommon<type, access_check>( \
2164 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01002165
2166EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, false);
2167EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, true);
2168EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, false);
2169EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, true);
2170EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, false);
2171EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, true);
2172EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, false);
2173EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, true);
2174EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, false);
2175EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, true);
2176#undef EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL
2177
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002178// See comments in runtime_support_asm.S
Andreas Gampec200a4a2014-06-16 18:39:09 -07002179extern "C" TwoWordReturn artInvokeInterfaceTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002180 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002181 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002182 return artInvokeCommon<kInterface, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002183}
2184
Andreas Gampec200a4a2014-06-16 18:39:09 -07002185extern "C" TwoWordReturn artInvokeDirectTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002186 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002187 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002188 return artInvokeCommon<kDirect, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002189}
2190
Andreas Gampec200a4a2014-06-16 18:39:09 -07002191extern "C" TwoWordReturn artInvokeStaticTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002192 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002193 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002194 return artInvokeCommon<kStatic, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002195}
2196
Andreas Gampec200a4a2014-06-16 18:39:09 -07002197extern "C" TwoWordReturn artInvokeSuperTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002198 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002199 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002200 return artInvokeCommon<kSuper, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002201}
2202
Andreas Gampec200a4a2014-06-16 18:39:09 -07002203extern "C" TwoWordReturn artInvokeVirtualTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002204 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002205 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002206 return artInvokeCommon<kVirtual, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002207}
2208
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002209// Determine target of interface dispatch. This object is known non-null. First argument
2210// is there for consistency but should not be used, as some architectures overwrite it
2211// in the assembly trampoline.
2212extern "C" TwoWordReturn artInvokeInterfaceTrampoline(uint32_t deadbeef ATTRIBUTE_UNUSED,
Andreas Gampe51f76352014-05-21 08:28:48 -07002213 mirror::Object* this_object,
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002214 Thread* self,
2215 ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002216 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002217 ScopedQuickEntrypointChecks sqec(self);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002218 StackHandleScope<1> hs(self);
2219 Handle<mirror::Class> cls(hs.NewHandle(this_object->GetClass()));
2220
Nicolas Geoffray5bf7bac2016-07-06 14:18:23 +00002221 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002222
2223 // Fetch the dex_method_idx of the target interface method from the caller.
2224 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
2225
2226 const DexFile::CodeItem* code_item = caller_method->GetCodeItem();
2227 CHECK_LT(dex_pc, code_item->insns_size_in_code_units_);
2228 const Instruction* instr = Instruction::At(&code_item->insns_[dex_pc]);
2229 Instruction::Code instr_code = instr->Opcode();
2230 CHECK(instr_code == Instruction::INVOKE_INTERFACE ||
2231 instr_code == Instruction::INVOKE_INTERFACE_RANGE)
2232 << "Unexpected call into interface trampoline: " << instr->DumpString(nullptr);
2233 uint32_t dex_method_idx;
2234 if (instr_code == Instruction::INVOKE_INTERFACE) {
2235 dex_method_idx = instr->VRegB_35c();
2236 } else {
2237 CHECK_EQ(instr_code, Instruction::INVOKE_INTERFACE_RANGE);
2238 dex_method_idx = instr->VRegB_3rc();
2239 }
2240
Mathieu Chartiere401d142015-04-22 13:56:20 -07002241 ArtMethod* interface_method = caller_method->GetDexCacheResolvedMethod(
Andreas Gampe542451c2016-07-26 09:02:02 -07002242 dex_method_idx, kRuntimePointerSize);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002243 DCHECK(interface_method != nullptr) << dex_method_idx << " " << PrettyMethod(caller_method);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002244 ArtMethod* method = nullptr;
Andreas Gampe542451c2016-07-26 09:02:02 -07002245 ImTable* imt = cls->GetImt(kRuntimePointerSize);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002246
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002247 if (LIKELY(interface_method->GetDexMethodIndex() != DexFile::kDexNoIndex)) {
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002248 // If the dex cache already resolved the interface method, look whether we have
2249 // a match in the ImtConflictTable.
Andreas Gampe75a7db62016-09-26 12:04:26 -07002250 ArtMethod* conflict_method = imt->Get(ImTable::GetImtIndex(interface_method),
2251 kRuntimePointerSize);
Alex Light9fc547a2016-03-31 14:34:33 -07002252 if (LIKELY(conflict_method->IsRuntimeMethod())) {
Andreas Gampe542451c2016-07-26 09:02:02 -07002253 ImtConflictTable* current_table = conflict_method->GetImtConflictTable(kRuntimePointerSize);
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002254 DCHECK(current_table != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -07002255 method = current_table->Lookup(interface_method, kRuntimePointerSize);
Alex Light9fc547a2016-03-31 14:34:33 -07002256 } else {
2257 // It seems we aren't really a conflict method!
Andreas Gampe542451c2016-07-26 09:02:02 -07002258 method = cls->FindVirtualMethodForInterface(interface_method, kRuntimePointerSize);
Alex Light9fc547a2016-03-31 14:34:33 -07002259 }
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002260 if (method != nullptr) {
2261 return GetTwoWordSuccessValue(
2262 reinterpret_cast<uintptr_t>(method->GetEntryPointFromQuickCompiledCode()),
2263 reinterpret_cast<uintptr_t>(method));
2264 }
2265
2266 // No match, use the IfTable.
Andreas Gampe542451c2016-07-26 09:02:02 -07002267 method = cls->FindVirtualMethodForInterface(interface_method, kRuntimePointerSize);
Ian Rogerse0a02da2014-12-02 14:10:53 -08002268 if (UNLIKELY(method == nullptr)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002269 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(
2270 interface_method, this_object, caller_method);
Andreas Gamped58342c2014-06-05 14:18:08 -07002271 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002272 }
2273 } else {
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002274 // The dex cache did not resolve the method, look it up in the dex file
2275 // of the caller,
Mathieu Chartier4edd8472015-06-01 10:47:36 -07002276 DCHECK_EQ(interface_method, Runtime::Current()->GetResolutionMethod());
Andreas Gampec200a4a2014-06-16 18:39:09 -07002277 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()
2278 ->GetDexFile();
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002279 uint32_t shorty_len;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002280 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(dex_method_idx),
2281 &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002282 {
2283 // Remember the args in case a GC happens in FindMethodFromCode.
2284 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
2285 RememberForGcArgumentVisitor visitor(sp, false, shorty, shorty_len, &soa);
2286 visitor.VisitArguments();
Andreas Gampe3a357142015-08-07 17:20:11 -07002287 method = FindMethodFromCode<kInterface, false>(dex_method_idx, &this_object, caller_method,
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002288 self);
2289 visitor.FixupReferences();
2290 }
2291
2292 if (UNLIKELY(method == nullptr)) {
2293 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07002294 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002295 }
Andreas Gampe542451c2016-07-26 09:02:02 -07002296 interface_method =
2297 caller_method->GetDexCacheResolvedMethod(dex_method_idx, kRuntimePointerSize);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002298 DCHECK(!interface_method->IsRuntimeMethod());
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002299 }
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002300
2301 // We arrive here if we have found an implementation, and it is not in the ImtConflictTable.
2302 // We create a new table with the new pair { interface_method, method }.
Andreas Gampe75a7db62016-09-26 12:04:26 -07002303 uint32_t imt_index = ImTable::GetImtIndex(interface_method);
Andreas Gampe542451c2016-07-26 09:02:02 -07002304 ArtMethod* conflict_method = imt->Get(imt_index, kRuntimePointerSize);
Alex Light9fc547a2016-03-31 14:34:33 -07002305 if (conflict_method->IsRuntimeMethod()) {
Mathieu Chartier7f98c9a2016-04-14 10:49:19 -07002306 ArtMethod* new_conflict_method = Runtime::Current()->GetClassLinker()->AddMethodToConflictTable(
2307 cls.Get(),
2308 conflict_method,
2309 interface_method,
2310 method,
2311 /*force_new_conflict_method*/false);
2312 if (new_conflict_method != conflict_method) {
Alex Light9fc547a2016-03-31 14:34:33 -07002313 // Update the IMT if we create a new conflict method. No fence needed here, as the
2314 // data is consistent.
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002315 imt->Set(imt_index,
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002316 new_conflict_method,
Andreas Gampe542451c2016-07-26 09:02:02 -07002317 kRuntimePointerSize);
Alex Light9fc547a2016-03-31 14:34:33 -07002318 }
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002319 }
2320
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002321 const void* code = method->GetEntryPointFromQuickCompiledCode();
2322
2323 // When we return, the caller will branch to this address, so it had better not be 0!
Ian Rogerse0a02da2014-12-02 14:10:53 -08002324 DCHECK(code != nullptr) << "Code was null in method: " << PrettyMethod(method)
Andreas Gampec200a4a2014-06-16 18:39:09 -07002325 << " location: " << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07002326
Andreas Gamped58342c2014-06-05 14:18:08 -07002327 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
2328 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002329}
2330
Ian Rogers848871b2013-08-05 10:56:33 -07002331} // namespace art