blob: 80ebf21b4028941cb6a3c4e3542fdbb4438cc8af [file] [log] [blame]
buzbee1452bee2015-03-06 14:43:04 -08001/*
2 * Copyright (C) 2016 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
17/*
18 * Mterp entry point and support functions.
19 */
buzbee1452bee2015-03-06 14:43:04 -080020#include "mterp.h"
David Sehrc431b9d2018-03-02 12:01:51 -080021
22#include "base/quasi_atomic.h"
Bill Buzbeefd522f92016-02-11 22:37:42 +000023#include "debugger.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070024#include "entrypoints/entrypoint_utils-inl.h"
25#include "interpreter/interpreter_common.h"
26#include "interpreter/interpreter_intrinsics.h"
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +010027#include "interpreter/shadow_frame-inl.h"
Andreas Gampefd63bbf2018-10-29 12:55:35 -070028#include "mirror/string-alloc-inl.h"
buzbee1452bee2015-03-06 14:43:04 -080029
30namespace art {
31namespace interpreter {
32/*
33 * Verify some constants used by the mterp interpreter.
34 */
35void CheckMterpAsmConstants() {
36 /*
37 * If we're using computed goto instruction transitions, make sure
David Srbeckyd88f5f72018-10-16 14:22:33 +010038 * none of the handlers overflows the byte limit. This won't tell
buzbee1452bee2015-03-06 14:43:04 -080039 * which one did, but if any one is too big the total size will
40 * overflow.
41 */
David Srbeckyd88f5f72018-10-16 14:22:33 +010042 const int width = kMterpHandlerSize;
buzbee1452bee2015-03-06 14:43:04 -080043 int interp_size = (uintptr_t) artMterpAsmInstructionEnd -
44 (uintptr_t) artMterpAsmInstructionStart;
45 if ((interp_size == 0) || (interp_size != (art::kNumPackedOpcodes * width))) {
Andreas Gampe3fec9ac2016-09-13 10:47:28 -070046 LOG(FATAL) << "ERROR: unexpected asm interp size " << interp_size
47 << "(did an instruction handler exceed " << width << " bytes?)";
buzbee1452bee2015-03-06 14:43:04 -080048 }
49}
50
51void InitMterpTls(Thread* self) {
David Srbecky776f3f72018-10-15 18:03:55 +010052 self->SetMterpCurrentIBase(artMterpAsmInstructionStart);
buzbee1452bee2015-03-06 14:43:04 -080053}
54
55/*
56 * Find the matching case. Returns the offset to the handler instructions.
57 *
58 * Returns 3 if we don't find a match (it's the size of the sparse-switch
59 * instruction).
60 */
Andreas Gampe67409972016-07-19 22:34:53 -070061extern "C" ssize_t MterpDoSparseSwitch(const uint16_t* switchData, int32_t testVal) {
buzbee1452bee2015-03-06 14:43:04 -080062 const int kInstrLen = 3;
63 uint16_t size;
64 const int32_t* keys;
65 const int32_t* entries;
66
67 /*
68 * Sparse switch data format:
69 * ushort ident = 0x0200 magic value
70 * ushort size number of entries in the table; > 0
71 * int keys[size] keys, sorted low-to-high; 32-bit aligned
72 * int targets[size] branch targets, relative to switch opcode
73 *
74 * Total size is (2+size*4) 16-bit code units.
75 */
76
77 uint16_t signature = *switchData++;
78 DCHECK_EQ(signature, static_cast<uint16_t>(art::Instruction::kSparseSwitchSignature));
79
80 size = *switchData++;
81
82 /* The keys are guaranteed to be aligned on a 32-bit boundary;
83 * we can treat them as a native int array.
84 */
85 keys = reinterpret_cast<const int32_t*>(switchData);
86
87 /* The entries are guaranteed to be aligned on a 32-bit boundary;
88 * we can treat them as a native int array.
89 */
90 entries = keys + size;
91
92 /*
93 * Binary-search through the array of keys, which are guaranteed to
94 * be sorted low-to-high.
95 */
96 int lo = 0;
97 int hi = size - 1;
98 while (lo <= hi) {
99 int mid = (lo + hi) >> 1;
100
101 int32_t foundVal = keys[mid];
102 if (testVal < foundVal) {
103 hi = mid - 1;
104 } else if (testVal > foundVal) {
105 lo = mid + 1;
106 } else {
107 return entries[mid];
108 }
109 }
110 return kInstrLen;
111}
112
Andreas Gampe67409972016-07-19 22:34:53 -0700113extern "C" ssize_t MterpDoPackedSwitch(const uint16_t* switchData, int32_t testVal) {
buzbee1452bee2015-03-06 14:43:04 -0800114 const int kInstrLen = 3;
115
116 /*
117 * Packed switch data format:
118 * ushort ident = 0x0100 magic value
119 * ushort size number of entries in the table
120 * int first_key first (and lowest) switch case value
121 * int targets[size] branch targets, relative to switch opcode
122 *
123 * Total size is (4+size*2) 16-bit code units.
124 */
125 uint16_t signature = *switchData++;
126 DCHECK_EQ(signature, static_cast<uint16_t>(art::Instruction::kPackedSwitchSignature));
127
128 uint16_t size = *switchData++;
129
130 int32_t firstKey = *switchData++;
131 firstKey |= (*switchData++) << 16;
132
133 int index = testVal - firstKey;
134 if (index < 0 || index >= size) {
135 return kInstrLen;
136 }
137
138 /*
139 * The entries are guaranteed to be aligned on a 32-bit boundary;
140 * we can treat them as a native int array.
141 */
142 const int32_t* entries = reinterpret_cast<const int32_t*>(switchData);
143 return entries[index];
144}
145
David Srbecky28f6cff2018-10-16 15:07:28 +0100146bool CanUseMterp()
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700147 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Light848574c2017-09-25 16:59:39 -0700148 const Runtime* const runtime = Runtime::Current();
David Srbecky28f6cff2018-10-16 15:07:28 +0100149 return
David Srbeckycb4f09e2018-10-21 08:45:22 +0100150 runtime->IsStarted() &&
151 !runtime->IsAotCompiler() &&
David Srbecky28f6cff2018-10-16 15:07:28 +0100152 !Dbg::IsDebuggerActive() &&
David Srbeckycb4f09e2018-10-21 08:45:22 +0100153 !runtime->GetInstrumentation()->IsActive() &&
Alex Light0aa7a5a2018-10-10 15:58:14 +0000154 // mterp only knows how to deal with the normal exits. It cannot handle any of the
155 // non-standard force-returns.
David Srbecky28f6cff2018-10-16 15:07:28 +0100156 !runtime->AreNonStandardExitsEnabled() &&
Alex Light848574c2017-09-25 16:59:39 -0700157 // An async exception has been thrown. We need to go to the switch interpreter. MTerp doesn't
158 // know how to deal with these so we could end up never dealing with it if we are in an
David Srbecky28f6cff2018-10-16 15:07:28 +0100159 // infinite loop.
David Srbeckycb4f09e2018-10-21 08:45:22 +0100160 !runtime->AreAsyncExceptionsThrown() &&
161 (runtime->GetJit() == nullptr || !runtime->GetJit()->JitAtFirstUse());
Bill Buzbeefd522f92016-02-11 22:37:42 +0000162}
163
buzbee1452bee2015-03-06 14:43:04 -0800164
Andreas Gampe67409972016-07-19 22:34:53 -0700165extern "C" size_t MterpInvokeVirtual(Thread* self,
166 ShadowFrame* shadow_frame,
167 uint16_t* dex_pc_ptr,
168 uint16_t inst_data)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700169 REQUIRES_SHARED(Locks::mutator_lock_) {
buzbee1452bee2015-03-06 14:43:04 -0800170 JValue* result_register = shadow_frame->GetResultRegister();
171 const Instruction* inst = Instruction::At(dex_pc_ptr);
David Srbecky1f5ab4e2018-10-15 11:46:46 +0100172 return DoInvoke<kVirtual, /*is_range=*/ false, /*do_access_check=*/ false, /*is_mterp=*/ true>(
Andreas Gampe7c5acbb2018-09-20 13:54:52 -0700173 self, *shadow_frame, inst, inst_data, result_register) ? 1u : 0u;
buzbee1452bee2015-03-06 14:43:04 -0800174}
175
Andreas Gampe67409972016-07-19 22:34:53 -0700176extern "C" size_t MterpInvokeSuper(Thread* self,
177 ShadowFrame* shadow_frame,
178 uint16_t* dex_pc_ptr,
179 uint16_t inst_data)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700180 REQUIRES_SHARED(Locks::mutator_lock_) {
buzbee1452bee2015-03-06 14:43:04 -0800181 JValue* result_register = shadow_frame->GetResultRegister();
182 const Instruction* inst = Instruction::At(dex_pc_ptr);
David Srbecky1f5ab4e2018-10-15 11:46:46 +0100183 return DoInvoke<kSuper, /*is_range=*/ false, /*do_access_check=*/ false, /*is_mterp=*/ true>(
Andreas Gampe7c5acbb2018-09-20 13:54:52 -0700184 self, *shadow_frame, inst, inst_data, result_register) ? 1u : 0u;
buzbee1452bee2015-03-06 14:43:04 -0800185}
186
Andreas Gampe67409972016-07-19 22:34:53 -0700187extern "C" size_t MterpInvokeInterface(Thread* self,
188 ShadowFrame* shadow_frame,
189 uint16_t* dex_pc_ptr,
190 uint16_t inst_data)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700191 REQUIRES_SHARED(Locks::mutator_lock_) {
buzbee1452bee2015-03-06 14:43:04 -0800192 JValue* result_register = shadow_frame->GetResultRegister();
193 const Instruction* inst = Instruction::At(dex_pc_ptr);
David Srbecky1f5ab4e2018-10-15 11:46:46 +0100194 return DoInvoke<kInterface, /*is_range=*/ false, /*do_access_check=*/ false, /*is_mterp=*/ true>(
Andreas Gampe7c5acbb2018-09-20 13:54:52 -0700195 self, *shadow_frame, inst, inst_data, result_register) ? 1u : 0u;
buzbee1452bee2015-03-06 14:43:04 -0800196}
197
Andreas Gampe67409972016-07-19 22:34:53 -0700198extern "C" size_t MterpInvokeDirect(Thread* self,
199 ShadowFrame* shadow_frame,
200 uint16_t* dex_pc_ptr,
201 uint16_t inst_data)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700202 REQUIRES_SHARED(Locks::mutator_lock_) {
buzbee1452bee2015-03-06 14:43:04 -0800203 JValue* result_register = shadow_frame->GetResultRegister();
204 const Instruction* inst = Instruction::At(dex_pc_ptr);
David Srbecky1f5ab4e2018-10-15 11:46:46 +0100205 return DoInvoke<kDirect, /*is_range=*/ false, /*do_access_check=*/ false, /*is_mterp=*/ true>(
Andreas Gampe7c5acbb2018-09-20 13:54:52 -0700206 self, *shadow_frame, inst, inst_data, result_register) ? 1u : 0u;
buzbee1452bee2015-03-06 14:43:04 -0800207}
208
Andreas Gampe67409972016-07-19 22:34:53 -0700209extern "C" size_t MterpInvokeStatic(Thread* self,
210 ShadowFrame* shadow_frame,
211 uint16_t* dex_pc_ptr,
212 uint16_t inst_data)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700213 REQUIRES_SHARED(Locks::mutator_lock_) {
buzbee1452bee2015-03-06 14:43:04 -0800214 JValue* result_register = shadow_frame->GetResultRegister();
215 const Instruction* inst = Instruction::At(dex_pc_ptr);
David Srbecky1f5ab4e2018-10-15 11:46:46 +0100216 return DoInvoke<kStatic, /*is_range=*/ false, /*do_access_check=*/ false, /*is_mterp=*/ true>(
Andreas Gampe7c5acbb2018-09-20 13:54:52 -0700217 self, *shadow_frame, inst, inst_data, result_register) ? 1u : 0u;
buzbee1452bee2015-03-06 14:43:04 -0800218}
219
Orion Hodsone7732be2017-10-11 14:35:20 +0100220extern "C" size_t MterpInvokeCustom(Thread* self,
221 ShadowFrame* shadow_frame,
222 uint16_t* dex_pc_ptr,
223 uint16_t inst_data)
224 REQUIRES_SHARED(Locks::mutator_lock_) {
225 JValue* result_register = shadow_frame->GetResultRegister();
226 const Instruction* inst = Instruction::At(dex_pc_ptr);
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700227 return DoInvokeCustom</* is_range= */ false>(
Andreas Gampe7c5acbb2018-09-20 13:54:52 -0700228 self, *shadow_frame, inst, inst_data, result_register) ? 1u : 0u;
Orion Hodsone7732be2017-10-11 14:35:20 +0100229}
230
231extern "C" size_t MterpInvokePolymorphic(Thread* self,
232 ShadowFrame* shadow_frame,
233 uint16_t* dex_pc_ptr,
234 uint16_t inst_data)
235 REQUIRES_SHARED(Locks::mutator_lock_) {
236 JValue* result_register = shadow_frame->GetResultRegister();
237 const Instruction* inst = Instruction::At(dex_pc_ptr);
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700238 return DoInvokePolymorphic</* is_range= */ false>(
Andreas Gampe7c5acbb2018-09-20 13:54:52 -0700239 self, *shadow_frame, inst, inst_data, result_register) ? 1u : 0u;
Orion Hodsone7732be2017-10-11 14:35:20 +0100240}
241
Andreas Gampe67409972016-07-19 22:34:53 -0700242extern "C" size_t MterpInvokeVirtualRange(Thread* self,
243 ShadowFrame* shadow_frame,
244 uint16_t* dex_pc_ptr,
245 uint16_t inst_data)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700246 REQUIRES_SHARED(Locks::mutator_lock_) {
buzbee1452bee2015-03-06 14:43:04 -0800247 JValue* result_register = shadow_frame->GetResultRegister();
248 const Instruction* inst = Instruction::At(dex_pc_ptr);
David Srbecky1f5ab4e2018-10-15 11:46:46 +0100249 return DoInvoke<kVirtual, /*is_range=*/ true, /*do_access_check=*/ false, /*is_mterp=*/ true>(
Andreas Gampe7c5acbb2018-09-20 13:54:52 -0700250 self, *shadow_frame, inst, inst_data, result_register) ? 1u : 0u;
buzbee1452bee2015-03-06 14:43:04 -0800251}
252
Andreas Gampe67409972016-07-19 22:34:53 -0700253extern "C" size_t MterpInvokeSuperRange(Thread* self,
254 ShadowFrame* shadow_frame,
255 uint16_t* dex_pc_ptr,
256 uint16_t inst_data)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700257 REQUIRES_SHARED(Locks::mutator_lock_) {
buzbee1452bee2015-03-06 14:43:04 -0800258 JValue* result_register = shadow_frame->GetResultRegister();
259 const Instruction* inst = Instruction::At(dex_pc_ptr);
David Srbecky1f5ab4e2018-10-15 11:46:46 +0100260 return DoInvoke<kSuper, /*is_range=*/ true, /*do_access_check=*/ false, /*is_mterp=*/ true>(
Andreas Gampe7c5acbb2018-09-20 13:54:52 -0700261 self, *shadow_frame, inst, inst_data, result_register) ? 1u : 0u;
buzbee1452bee2015-03-06 14:43:04 -0800262}
263
Andreas Gampe67409972016-07-19 22:34:53 -0700264extern "C" size_t MterpInvokeInterfaceRange(Thread* self,
265 ShadowFrame* shadow_frame,
266 uint16_t* dex_pc_ptr,
267 uint16_t inst_data)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700268 REQUIRES_SHARED(Locks::mutator_lock_) {
buzbee1452bee2015-03-06 14:43:04 -0800269 JValue* result_register = shadow_frame->GetResultRegister();
270 const Instruction* inst = Instruction::At(dex_pc_ptr);
David Srbecky1f5ab4e2018-10-15 11:46:46 +0100271 return DoInvoke<kInterface, /*is_range=*/ true, /*do_access_check=*/ false, /*is_mterp=*/ true>(
Andreas Gampe7c5acbb2018-09-20 13:54:52 -0700272 self, *shadow_frame, inst, inst_data, result_register) ? 1u : 0u;
buzbee1452bee2015-03-06 14:43:04 -0800273}
274
Andreas Gampe67409972016-07-19 22:34:53 -0700275extern "C" size_t MterpInvokeDirectRange(Thread* self,
276 ShadowFrame* shadow_frame,
277 uint16_t* dex_pc_ptr,
278 uint16_t inst_data)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700279 REQUIRES_SHARED(Locks::mutator_lock_) {
buzbee1452bee2015-03-06 14:43:04 -0800280 JValue* result_register = shadow_frame->GetResultRegister();
281 const Instruction* inst = Instruction::At(dex_pc_ptr);
David Srbecky1f5ab4e2018-10-15 11:46:46 +0100282 return DoInvoke<kDirect, /*is_range=*/ true, /*do_access_check=*/ false, /*is_mterp=*/ true>(
Andreas Gampe7c5acbb2018-09-20 13:54:52 -0700283 self, *shadow_frame, inst, inst_data, result_register) ? 1u : 0u;
buzbee1452bee2015-03-06 14:43:04 -0800284}
285
Andreas Gampe67409972016-07-19 22:34:53 -0700286extern "C" size_t MterpInvokeStaticRange(Thread* self,
287 ShadowFrame* shadow_frame,
288 uint16_t* dex_pc_ptr,
289 uint16_t inst_data)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700290 REQUIRES_SHARED(Locks::mutator_lock_) {
buzbee1452bee2015-03-06 14:43:04 -0800291 JValue* result_register = shadow_frame->GetResultRegister();
292 const Instruction* inst = Instruction::At(dex_pc_ptr);
David Srbecky1f5ab4e2018-10-15 11:46:46 +0100293 return DoInvoke<kStatic, /*is_range=*/ true, /*do_access_check=*/ false, /*is_mterp=*/ true>(
Andreas Gampe7c5acbb2018-09-20 13:54:52 -0700294 self, *shadow_frame, inst, inst_data, result_register) ? 1u : 0u;
buzbee1452bee2015-03-06 14:43:04 -0800295}
296
Orion Hodsone7732be2017-10-11 14:35:20 +0100297extern "C" size_t MterpInvokeCustomRange(Thread* self,
298 ShadowFrame* shadow_frame,
299 uint16_t* dex_pc_ptr,
300 uint16_t inst_data)
301 REQUIRES_SHARED(Locks::mutator_lock_) {
302 JValue* result_register = shadow_frame->GetResultRegister();
303 const Instruction* inst = Instruction::At(dex_pc_ptr);
Andreas Gampe7c5acbb2018-09-20 13:54:52 -0700304 return DoInvokeCustom</*is_range=*/ true>(
305 self, *shadow_frame, inst, inst_data, result_register) ? 1u : 0u;
Orion Hodsone7732be2017-10-11 14:35:20 +0100306}
307
308extern "C" size_t MterpInvokePolymorphicRange(Thread* self,
309 ShadowFrame* shadow_frame,
310 uint16_t* dex_pc_ptr,
311 uint16_t inst_data)
312 REQUIRES_SHARED(Locks::mutator_lock_) {
313 JValue* result_register = shadow_frame->GetResultRegister();
314 const Instruction* inst = Instruction::At(dex_pc_ptr);
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700315 return DoInvokePolymorphic</* is_range= */ true>(
Andreas Gampe7c5acbb2018-09-20 13:54:52 -0700316 self, *shadow_frame, inst, inst_data, result_register) ? 1u : 0u;
Orion Hodsone7732be2017-10-11 14:35:20 +0100317}
318
Andreas Gampe67409972016-07-19 22:34:53 -0700319extern "C" size_t MterpInvokeVirtualQuick(Thread* self,
320 ShadowFrame* shadow_frame,
321 uint16_t* dex_pc_ptr,
322 uint16_t inst_data)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700323 REQUIRES_SHARED(Locks::mutator_lock_) {
buzbee1452bee2015-03-06 14:43:04 -0800324 JValue* result_register = shadow_frame->GetResultRegister();
325 const Instruction* inst = Instruction::At(dex_pc_ptr);
David Srbecky08cb7382018-10-30 09:27:59 +0000326 return DoInvoke<kVirtual, /*is_range=*/ false, /*do_access_check=*/ false, /*is_mterp=*/ true,
Andreas Gampe7c5acbb2018-09-20 13:54:52 -0700327 /*is_quick=*/ true>(self, *shadow_frame, inst, inst_data, result_register) ? 1u : 0u;
buzbee1452bee2015-03-06 14:43:04 -0800328}
329
Andreas Gampe67409972016-07-19 22:34:53 -0700330extern "C" size_t MterpInvokeVirtualQuickRange(Thread* self,
331 ShadowFrame* shadow_frame,
332 uint16_t* dex_pc_ptr,
333 uint16_t inst_data)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700334 REQUIRES_SHARED(Locks::mutator_lock_) {
buzbee1452bee2015-03-06 14:43:04 -0800335 JValue* result_register = shadow_frame->GetResultRegister();
336 const Instruction* inst = Instruction::At(dex_pc_ptr);
David Srbecky08cb7382018-10-30 09:27:59 +0000337 return DoInvoke<kVirtual, /*is_range=*/ true, /*do_access_check=*/ false, /*is_mterp=*/ true,
Andreas Gampe7c5acbb2018-09-20 13:54:52 -0700338 /*is_quick=*/ true>(self, *shadow_frame, inst, inst_data, result_register) ? 1u : 0u;
buzbee1452bee2015-03-06 14:43:04 -0800339}
340
341extern "C" void MterpThreadFenceForConstructor() {
342 QuasiAtomic::ThreadFenceForConstructor();
343}
344
Andreas Gampe67409972016-07-19 22:34:53 -0700345extern "C" size_t MterpConstString(uint32_t index,
346 uint32_t tgt_vreg,
347 ShadowFrame* shadow_frame,
348 Thread* self)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700349 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800350 ObjPtr<mirror::String> s = ResolveString(self, *shadow_frame, dex::StringIndex(index));
buzbee1452bee2015-03-06 14:43:04 -0800351 if (UNLIKELY(s == nullptr)) {
Andreas Gampe7c5acbb2018-09-20 13:54:52 -0700352 return 1u;
buzbee1452bee2015-03-06 14:43:04 -0800353 }
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +0100354 shadow_frame->SetVRegReference(tgt_vreg, s);
Andreas Gampe7c5acbb2018-09-20 13:54:52 -0700355 return 0u;
buzbee1452bee2015-03-06 14:43:04 -0800356}
357
Andreas Gampe67409972016-07-19 22:34:53 -0700358extern "C" size_t MterpConstClass(uint32_t index,
359 uint32_t tgt_vreg,
360 ShadowFrame* shadow_frame,
361 Thread* self)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700362 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko28e012a2017-12-07 11:22:59 +0000363 ObjPtr<mirror::Class> c = ResolveVerifyAndClinit(dex::TypeIndex(index),
364 shadow_frame->GetMethod(),
365 self,
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700366 /* can_run_clinit= */ false,
367 /* verify_access= */ false);
buzbee1452bee2015-03-06 14:43:04 -0800368 if (UNLIKELY(c == nullptr)) {
Andreas Gampe7c5acbb2018-09-20 13:54:52 -0700369 return 1u;
buzbee1452bee2015-03-06 14:43:04 -0800370 }
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +0100371 shadow_frame->SetVRegReference(tgt_vreg, c);
Andreas Gampe7c5acbb2018-09-20 13:54:52 -0700372 return 0u;
buzbee1452bee2015-03-06 14:43:04 -0800373}
374
Orion Hodsone7732be2017-10-11 14:35:20 +0100375extern "C" size_t MterpConstMethodHandle(uint32_t index,
376 uint32_t tgt_vreg,
377 ShadowFrame* shadow_frame,
378 Thread* self)
379 REQUIRES_SHARED(Locks::mutator_lock_) {
380 ObjPtr<mirror::MethodHandle> mh = ResolveMethodHandle(self, index, shadow_frame->GetMethod());
381 if (UNLIKELY(mh == nullptr)) {
Andreas Gampe7c5acbb2018-09-20 13:54:52 -0700382 return 1u;
Orion Hodsone7732be2017-10-11 14:35:20 +0100383 }
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +0100384 shadow_frame->SetVRegReference(tgt_vreg, mh);
Andreas Gampe7c5acbb2018-09-20 13:54:52 -0700385 return 0u;
Orion Hodsone7732be2017-10-11 14:35:20 +0100386}
387
388extern "C" size_t MterpConstMethodType(uint32_t index,
389 uint32_t tgt_vreg,
390 ShadowFrame* shadow_frame,
391 Thread* self)
392 REQUIRES_SHARED(Locks::mutator_lock_) {
Orion Hodson06d10a72018-05-14 08:53:38 +0100393 ObjPtr<mirror::MethodType> mt =
394 ResolveMethodType(self, dex::ProtoIndex(index), shadow_frame->GetMethod());
Orion Hodsone7732be2017-10-11 14:35:20 +0100395 if (UNLIKELY(mt == nullptr)) {
Andreas Gampe7c5acbb2018-09-20 13:54:52 -0700396 return 1u;
Orion Hodsone7732be2017-10-11 14:35:20 +0100397 }
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +0100398 shadow_frame->SetVRegReference(tgt_vreg, mt);
Andreas Gampe7c5acbb2018-09-20 13:54:52 -0700399 return 0u;
Orion Hodsone7732be2017-10-11 14:35:20 +0100400}
401
Andreas Gampe67409972016-07-19 22:34:53 -0700402extern "C" size_t MterpCheckCast(uint32_t index,
403 StackReference<mirror::Object>* vreg_addr,
404 art::ArtMethod* method,
405 Thread* self)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700406 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800407 ObjPtr<mirror::Class> c = ResolveVerifyAndClinit(dex::TypeIndex(index),
408 method,
409 self,
410 false,
411 false);
buzbee1452bee2015-03-06 14:43:04 -0800412 if (UNLIKELY(c == nullptr)) {
Andreas Gampe7c5acbb2018-09-20 13:54:52 -0700413 return 1u;
buzbee1452bee2015-03-06 14:43:04 -0800414 }
buzbeea2c97a92016-01-25 15:41:24 -0800415 // Must load obj from vreg following ResolveVerifyAndClinit due to moving gc.
Vladimir Marko4bb2af52019-03-22 11:09:19 +0000416 ObjPtr<mirror::Object> obj = vreg_addr->AsMirrorPtr();
buzbee1452bee2015-03-06 14:43:04 -0800417 if (UNLIKELY(obj != nullptr && !obj->InstanceOf(c))) {
418 ThrowClassCastException(c, obj->GetClass());
Andreas Gampe7c5acbb2018-09-20 13:54:52 -0700419 return 1u;
buzbee1452bee2015-03-06 14:43:04 -0800420 }
Andreas Gampe7c5acbb2018-09-20 13:54:52 -0700421 return 0u;
buzbee1452bee2015-03-06 14:43:04 -0800422}
423
Andreas Gampe67409972016-07-19 22:34:53 -0700424extern "C" size_t MterpInstanceOf(uint32_t index,
425 StackReference<mirror::Object>* vreg_addr,
426 art::ArtMethod* method,
427 Thread* self)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700428 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800429 ObjPtr<mirror::Class> c = ResolveVerifyAndClinit(dex::TypeIndex(index),
430 method,
431 self,
432 false,
433 false);
buzbee1452bee2015-03-06 14:43:04 -0800434 if (UNLIKELY(c == nullptr)) {
Andreas Gampe7c5acbb2018-09-20 13:54:52 -0700435 return 0u; // Caller will check for pending exception. Return value unimportant.
buzbee1452bee2015-03-06 14:43:04 -0800436 }
buzbeea2c97a92016-01-25 15:41:24 -0800437 // Must load obj from vreg following ResolveVerifyAndClinit due to moving gc.
Vladimir Marko4bb2af52019-03-22 11:09:19 +0000438 ObjPtr<mirror::Object> obj = vreg_addr->AsMirrorPtr();
Andreas Gampe7c5acbb2018-09-20 13:54:52 -0700439 return (obj != nullptr) && obj->InstanceOf(c) ? 1u : 0u;
buzbee1452bee2015-03-06 14:43:04 -0800440}
441
Vladimir Marko4bb2af52019-03-22 11:09:19 +0000442extern "C" size_t MterpFillArrayData(mirror::Object* obj,
443 const Instruction::ArrayDataPayload* payload)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700444 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe7c5acbb2018-09-20 13:54:52 -0700445 return FillArrayData(obj, payload) ? 1u : 0u;
buzbee1452bee2015-03-06 14:43:04 -0800446}
447
Andreas Gampe67409972016-07-19 22:34:53 -0700448extern "C" size_t MterpNewInstance(ShadowFrame* shadow_frame, Thread* self, uint32_t inst_data)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700449 REQUIRES_SHARED(Locks::mutator_lock_) {
buzbee1452bee2015-03-06 14:43:04 -0800450 const Instruction* inst = Instruction::At(shadow_frame->GetDexPCPtr());
Vladimir Marko4bb2af52019-03-22 11:09:19 +0000451 ObjPtr<mirror::Object> obj = nullptr;
Vladimir Marko28e012a2017-12-07 11:22:59 +0000452 ObjPtr<mirror::Class> c = ResolveVerifyAndClinit(dex::TypeIndex(inst->VRegB_21c()),
453 shadow_frame->GetMethod(),
454 self,
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700455 /* can_run_clinit= */ false,
456 /* verify_access= */ false);
buzbee1452bee2015-03-06 14:43:04 -0800457 if (LIKELY(c != nullptr)) {
458 if (UNLIKELY(c->IsStringClass())) {
459 gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
Vladimir Marko9b81ac32019-05-16 16:47:08 +0100460 obj = mirror::String::AllocEmptyString(self, allocator_type);
buzbee1452bee2015-03-06 14:43:04 -0800461 } else {
Vladimir Marko9b81ac32019-05-16 16:47:08 +0100462 obj = AllocObjectFromCode(c, self, Runtime::Current()->GetHeap()->GetCurrentAllocator());
buzbee1452bee2015-03-06 14:43:04 -0800463 }
464 }
465 if (UNLIKELY(obj == nullptr)) {
Andreas Gampe7c5acbb2018-09-20 13:54:52 -0700466 return 0u;
buzbee1452bee2015-03-06 14:43:04 -0800467 }
468 obj->GetClass()->AssertInitializedOrInitializingInThread(self);
469 shadow_frame->SetVRegReference(inst->VRegA_21c(inst_data), obj);
Andreas Gampe7c5acbb2018-09-20 13:54:52 -0700470 return 1u;
buzbee1452bee2015-03-06 14:43:04 -0800471}
472
Andreas Gampe67409972016-07-19 22:34:53 -0700473extern "C" size_t MterpIputObjectQuick(ShadowFrame* shadow_frame,
474 uint16_t* dex_pc_ptr,
475 uint32_t inst_data)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700476 REQUIRES_SHARED(Locks::mutator_lock_) {
buzbee1452bee2015-03-06 14:43:04 -0800477 const Instruction* inst = Instruction::At(dex_pc_ptr);
Andreas Gampe7c5acbb2018-09-20 13:54:52 -0700478 return DoIPutQuick<Primitive::kPrimNot, false>(*shadow_frame, inst, inst_data) ? 1u : 0u;
buzbee1452bee2015-03-06 14:43:04 -0800479}
480
Andreas Gampe67409972016-07-19 22:34:53 -0700481extern "C" size_t MterpAputObject(ShadowFrame* shadow_frame,
482 uint16_t* dex_pc_ptr,
483 uint32_t inst_data)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700484 REQUIRES_SHARED(Locks::mutator_lock_) {
buzbee1452bee2015-03-06 14:43:04 -0800485 const Instruction* inst = Instruction::At(dex_pc_ptr);
Vladimir Marko4bb2af52019-03-22 11:09:19 +0000486 ObjPtr<mirror::Object> a = shadow_frame->GetVRegReference(inst->VRegB_23x());
buzbee1452bee2015-03-06 14:43:04 -0800487 if (UNLIKELY(a == nullptr)) {
Andreas Gampe7c5acbb2018-09-20 13:54:52 -0700488 return 0u;
buzbee1452bee2015-03-06 14:43:04 -0800489 }
490 int32_t index = shadow_frame->GetVReg(inst->VRegC_23x());
Vladimir Marko4bb2af52019-03-22 11:09:19 +0000491 ObjPtr<mirror::Object> val = shadow_frame->GetVRegReference(inst->VRegA_23x(inst_data));
492 ObjPtr<mirror::ObjectArray<mirror::Object>> array = a->AsObjectArray<mirror::Object>();
buzbee1452bee2015-03-06 14:43:04 -0800493 if (array->CheckIsValidIndex(index) && array->CheckAssignable(val)) {
494 array->SetWithoutChecks<false>(index, val);
Andreas Gampe7c5acbb2018-09-20 13:54:52 -0700495 return 1u;
buzbee1452bee2015-03-06 14:43:04 -0800496 }
Andreas Gampe7c5acbb2018-09-20 13:54:52 -0700497 return 0u;
buzbee1452bee2015-03-06 14:43:04 -0800498}
499
Andreas Gampe67409972016-07-19 22:34:53 -0700500extern "C" size_t MterpFilledNewArray(ShadowFrame* shadow_frame,
501 uint16_t* dex_pc_ptr,
502 Thread* self)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700503 REQUIRES_SHARED(Locks::mutator_lock_) {
buzbee1452bee2015-03-06 14:43:04 -0800504 const Instruction* inst = Instruction::At(dex_pc_ptr);
505 return DoFilledNewArray<false, false, false>(inst, *shadow_frame, self,
Andreas Gampe7c5acbb2018-09-20 13:54:52 -0700506 shadow_frame->GetResultRegister()) ? 1u : 0u;
buzbee1452bee2015-03-06 14:43:04 -0800507}
508
Andreas Gampe67409972016-07-19 22:34:53 -0700509extern "C" size_t MterpFilledNewArrayRange(ShadowFrame* shadow_frame,
510 uint16_t* dex_pc_ptr,
511 Thread* self)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700512 REQUIRES_SHARED(Locks::mutator_lock_) {
buzbee1452bee2015-03-06 14:43:04 -0800513 const Instruction* inst = Instruction::At(dex_pc_ptr);
514 return DoFilledNewArray<true, false, false>(inst, *shadow_frame, self,
Andreas Gampe7c5acbb2018-09-20 13:54:52 -0700515 shadow_frame->GetResultRegister()) ? 1u : 0u;
buzbee1452bee2015-03-06 14:43:04 -0800516}
517
Andreas Gampe67409972016-07-19 22:34:53 -0700518extern "C" size_t MterpNewArray(ShadowFrame* shadow_frame,
519 uint16_t* dex_pc_ptr,
520 uint32_t inst_data, Thread* self)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700521 REQUIRES_SHARED(Locks::mutator_lock_) {
buzbee1452bee2015-03-06 14:43:04 -0800522 const Instruction* inst = Instruction::At(dex_pc_ptr);
523 int32_t length = shadow_frame->GetVReg(inst->VRegB_22c(inst_data));
Vladimir Marko9b81ac32019-05-16 16:47:08 +0100524 ObjPtr<mirror::Object> obj = AllocArrayFromCode</*kAccessCheck=*/ false>(
Andreas Gampea5b09a62016-11-17 15:21:22 -0800525 dex::TypeIndex(inst->VRegC_22c()), length, shadow_frame->GetMethod(), self,
buzbee1452bee2015-03-06 14:43:04 -0800526 Runtime::Current()->GetHeap()->GetCurrentAllocator());
527 if (UNLIKELY(obj == nullptr)) {
Andreas Gampe7c5acbb2018-09-20 13:54:52 -0700528 return 0u;
buzbee1452bee2015-03-06 14:43:04 -0800529 }
530 shadow_frame->SetVRegReference(inst->VRegA_22c(inst_data), obj);
Andreas Gampe7c5acbb2018-09-20 13:54:52 -0700531 return 1u;
buzbee1452bee2015-03-06 14:43:04 -0800532}
533
Andreas Gampe67409972016-07-19 22:34:53 -0700534extern "C" size_t MterpHandleException(Thread* self, ShadowFrame* shadow_frame)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700535 REQUIRES_SHARED(Locks::mutator_lock_) {
buzbee1452bee2015-03-06 14:43:04 -0800536 DCHECK(self->IsExceptionPending());
537 const instrumentation::Instrumentation* const instrumentation =
538 Runtime::Current()->GetInstrumentation();
Andreas Gampe7c5acbb2018-09-20 13:54:52 -0700539 return MoveToExceptionHandler(self, *shadow_frame, instrumentation) ? 1u : 0u;
buzbee1452bee2015-03-06 14:43:04 -0800540}
541
David Srbecky68b926e2018-11-28 17:43:42 +0000542struct MterpCheckHelper {
543 DECLARE_RUNTIME_DEBUG_FLAG(kSlowMode);
544};
545DEFINE_RUNTIME_DEBUG_FLAG(MterpCheckHelper, kSlowMode);
546
Bill Buzbeed47fd902016-07-07 14:42:43 +0000547extern "C" void MterpCheckBefore(Thread* self, ShadowFrame* shadow_frame, uint16_t* dex_pc_ptr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700548 REQUIRES_SHARED(Locks::mutator_lock_) {
David Srbeckyd3883902019-02-26 17:29:32 +0000549 // Check that we are using the right interpreter.
550 if (kIsDebugBuild && self->UseMterp() != CanUseMterp()) {
551 // The flag might be currently being updated on all threads. Retry with lock.
552 MutexLock tll_mu(self, *Locks::thread_list_lock_);
553 DCHECK_EQ(self->UseMterp(), CanUseMterp());
554 }
David Srbeckycb4f09e2018-10-21 08:45:22 +0100555 DCHECK(!Runtime::Current()->IsActiveTransaction());
Bill Buzbeed47fd902016-07-07 14:42:43 +0000556 const Instruction* inst = Instruction::At(dex_pc_ptr);
David Srbeckye81f10a2019-07-04 10:00:12 +0000557 uint16_t inst_data = inst->Fetch16(0);
558 if (inst->Opcode(inst_data) == Instruction::MOVE_EXCEPTION) {
559 self->AssertPendingException();
560 } else {
561 self->AssertNoPendingException();
562 }
Bill Buzbeed47fd902016-07-07 14:42:43 +0000563 if (kTraceExecutionEnabled) {
David Srbeckye81f10a2019-07-04 10:00:12 +0000564 uint32_t dex_pc = dex_pc_ptr - shadow_frame->GetDexInstructions();
Bill Buzbeed47fd902016-07-07 14:42:43 +0000565 TraceExecution(*shadow_frame, inst, dex_pc);
566 }
567 if (kTestExportPC) {
568 // Save invalid dex pc to force segfault if improperly used.
569 shadow_frame->SetDexPCPtr(reinterpret_cast<uint16_t*>(kExportPCPoison));
570 }
David Srbecky68b926e2018-11-28 17:43:42 +0000571 if (MterpCheckHelper::kSlowMode) {
572 shadow_frame->CheckConsistentVRegs();
573 }
buzbee1452bee2015-03-06 14:43:04 -0800574}
575
576extern "C" void MterpLogDivideByZeroException(Thread* self, ShadowFrame* shadow_frame)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700577 REQUIRES_SHARED(Locks::mutator_lock_) {
buzbee1452bee2015-03-06 14:43:04 -0800578 UNUSED(self);
579 const Instruction* inst = Instruction::At(shadow_frame->GetDexPCPtr());
580 uint16_t inst_data = inst->Fetch16(0);
581 LOG(INFO) << "DivideByZero: " << inst->Opcode(inst_data);
582}
583
584extern "C" void MterpLogArrayIndexException(Thread* self, ShadowFrame* shadow_frame)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700585 REQUIRES_SHARED(Locks::mutator_lock_) {
buzbee1452bee2015-03-06 14:43:04 -0800586 UNUSED(self);
587 const Instruction* inst = Instruction::At(shadow_frame->GetDexPCPtr());
588 uint16_t inst_data = inst->Fetch16(0);
589 LOG(INFO) << "ArrayIndex: " << inst->Opcode(inst_data);
590}
591
592extern "C" void MterpLogNegativeArraySizeException(Thread* self, ShadowFrame* shadow_frame)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700593 REQUIRES_SHARED(Locks::mutator_lock_) {
buzbee1452bee2015-03-06 14:43:04 -0800594 UNUSED(self);
595 const Instruction* inst = Instruction::At(shadow_frame->GetDexPCPtr());
596 uint16_t inst_data = inst->Fetch16(0);
597 LOG(INFO) << "NegativeArraySize: " << inst->Opcode(inst_data);
598}
599
600extern "C" void MterpLogNoSuchMethodException(Thread* self, ShadowFrame* shadow_frame)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700601 REQUIRES_SHARED(Locks::mutator_lock_) {
buzbee1452bee2015-03-06 14:43:04 -0800602 UNUSED(self);
603 const Instruction* inst = Instruction::At(shadow_frame->GetDexPCPtr());
604 uint16_t inst_data = inst->Fetch16(0);
605 LOG(INFO) << "NoSuchMethod: " << inst->Opcode(inst_data);
606}
607
608extern "C" void MterpLogExceptionThrownException(Thread* self, ShadowFrame* shadow_frame)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700609 REQUIRES_SHARED(Locks::mutator_lock_) {
buzbee1452bee2015-03-06 14:43:04 -0800610 UNUSED(self);
611 const Instruction* inst = Instruction::At(shadow_frame->GetDexPCPtr());
612 uint16_t inst_data = inst->Fetch16(0);
613 LOG(INFO) << "ExceptionThrown: " << inst->Opcode(inst_data);
614}
615
616extern "C" void MterpLogNullObjectException(Thread* self, ShadowFrame* shadow_frame)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700617 REQUIRES_SHARED(Locks::mutator_lock_) {
buzbee1452bee2015-03-06 14:43:04 -0800618 UNUSED(self);
619 const Instruction* inst = Instruction::At(shadow_frame->GetDexPCPtr());
620 uint16_t inst_data = inst->Fetch16(0);
621 LOG(INFO) << "NullObject: " << inst->Opcode(inst_data);
622}
623
624extern "C" void MterpLogFallback(Thread* self, ShadowFrame* shadow_frame)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700625 REQUIRES_SHARED(Locks::mutator_lock_) {
buzbee1452bee2015-03-06 14:43:04 -0800626 UNUSED(self);
627 const Instruction* inst = Instruction::At(shadow_frame->GetDexPCPtr());
628 uint16_t inst_data = inst->Fetch16(0);
629 LOG(INFO) << "Fallback: " << inst->Opcode(inst_data) << ", Suspend Pending?: "
630 << self->IsExceptionPending();
631}
632
Bill Buzbeefd522f92016-02-11 22:37:42 +0000633extern "C" void MterpLogOSR(Thread* self, ShadowFrame* shadow_frame, int32_t offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700634 REQUIRES_SHARED(Locks::mutator_lock_) {
Bill Buzbeefd522f92016-02-11 22:37:42 +0000635 UNUSED(self);
636 const Instruction* inst = Instruction::At(shadow_frame->GetDexPCPtr());
637 uint16_t inst_data = inst->Fetch16(0);
638 LOG(INFO) << "OSR: " << inst->Opcode(inst_data) << ", offset = " << offset;
639}
640
buzbee1452bee2015-03-06 14:43:04 -0800641extern "C" void MterpLogSuspendFallback(Thread* self, ShadowFrame* shadow_frame, uint32_t flags)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700642 REQUIRES_SHARED(Locks::mutator_lock_) {
buzbee1452bee2015-03-06 14:43:04 -0800643 UNUSED(self);
644 const Instruction* inst = Instruction::At(shadow_frame->GetDexPCPtr());
645 uint16_t inst_data = inst->Fetch16(0);
646 if (flags & kCheckpointRequest) {
647 LOG(INFO) << "Checkpoint fallback: " << inst->Opcode(inst_data);
648 } else if (flags & kSuspendRequest) {
649 LOG(INFO) << "Suspend fallback: " << inst->Opcode(inst_data);
Hiroshi Yamauchi30493242016-11-03 13:06:52 -0700650 } else if (flags & kEmptyCheckpointRequest) {
651 LOG(INFO) << "Empty checkpoint fallback: " << inst->Opcode(inst_data);
buzbee1452bee2015-03-06 14:43:04 -0800652 }
653}
654
Andreas Gampe67409972016-07-19 22:34:53 -0700655extern "C" size_t MterpSuspendCheck(Thread* self)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700656 REQUIRES_SHARED(Locks::mutator_lock_) {
buzbee1452bee2015-03-06 14:43:04 -0800657 self->AllowThreadSuspension();
David Srbecky28f6cff2018-10-16 15:07:28 +0100658 return !self->UseMterp();
buzbee1452bee2015-03-06 14:43:04 -0800659}
660
David Srbeckyce32c102018-08-31 07:21:07 +0100661// Execute single field access instruction (get/put, static/instance).
662// The template arguments reduce this to fairly small amount of code.
663// It requires the target object and field to be already resolved.
664template<typename PrimType, FindFieldType kAccessType>
665ALWAYS_INLINE void MterpFieldAccess(Instruction* inst,
666 uint16_t inst_data,
667 ShadowFrame* shadow_frame,
668 ObjPtr<mirror::Object> obj,
669 MemberOffset offset,
670 bool is_volatile)
671 REQUIRES_SHARED(Locks::mutator_lock_) {
672 static_assert(std::is_integral<PrimType>::value, "Unexpected primitive type");
673 constexpr bool kIsStatic = (kAccessType & FindFieldFlags::StaticBit) != 0;
674 constexpr bool kIsPrimitive = (kAccessType & FindFieldFlags::PrimitiveBit) != 0;
675 constexpr bool kIsRead = (kAccessType & FindFieldFlags::ReadBit) != 0;
676
677 uint16_t vRegA = kIsStatic ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
678 if (kIsPrimitive) {
679 if (kIsRead) {
680 PrimType value = UNLIKELY(is_volatile)
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700681 ? obj->GetFieldPrimitive<PrimType, /*kIsVolatile=*/ true>(offset)
682 : obj->GetFieldPrimitive<PrimType, /*kIsVolatile=*/ false>(offset);
David Srbeckyce32c102018-08-31 07:21:07 +0100683 if (sizeof(PrimType) == sizeof(uint64_t)) {
684 shadow_frame->SetVRegLong(vRegA, value); // Set two consecutive registers.
685 } else {
686 shadow_frame->SetVReg(vRegA, static_cast<int32_t>(value)); // Sign/zero extend.
687 }
688 } else { // Write.
689 uint64_t value = (sizeof(PrimType) == sizeof(uint64_t))
690 ? shadow_frame->GetVRegLong(vRegA)
691 : shadow_frame->GetVReg(vRegA);
692 if (UNLIKELY(is_volatile)) {
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700693 obj->SetFieldPrimitive<PrimType, /*kIsVolatile=*/ true>(offset, value);
David Srbeckyce32c102018-08-31 07:21:07 +0100694 } else {
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700695 obj->SetFieldPrimitive<PrimType, /*kIsVolatile=*/ false>(offset, value);
David Srbeckyce32c102018-08-31 07:21:07 +0100696 }
697 }
698 } else { // Object.
699 if (kIsRead) {
700 ObjPtr<mirror::Object> value = UNLIKELY(is_volatile)
701 ? obj->GetFieldObjectVolatile<mirror::Object>(offset)
702 : obj->GetFieldObject<mirror::Object>(offset);
703 shadow_frame->SetVRegReference(vRegA, value);
704 } else { // Write.
705 ObjPtr<mirror::Object> value = shadow_frame->GetVRegReference(vRegA);
706 if (UNLIKELY(is_volatile)) {
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700707 obj->SetFieldObjectVolatile</*kTransactionActive=*/ false>(offset, value);
David Srbeckyce32c102018-08-31 07:21:07 +0100708 } else {
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700709 obj->SetFieldObject</*kTransactionActive=*/ false>(offset, value);
David Srbeckyce32c102018-08-31 07:21:07 +0100710 }
711 }
David Srbecky104bab62018-08-07 17:09:01 +0100712 }
David Srbecky104bab62018-08-07 17:09:01 +0100713}
714
David Srbeckyce32c102018-08-31 07:21:07 +0100715template<typename PrimType, FindFieldType kAccessType>
716NO_INLINE bool MterpFieldAccessSlow(Instruction* inst,
717 uint16_t inst_data,
718 ShadowFrame* shadow_frame,
719 Thread* self)
720 REQUIRES_SHARED(Locks::mutator_lock_) {
721 constexpr bool kIsStatic = (kAccessType & FindFieldFlags::StaticBit) != 0;
722 constexpr bool kIsRead = (kAccessType & FindFieldFlags::ReadBit) != 0;
723
724 // Update the dex pc in shadow frame, just in case anything throws.
725 shadow_frame->SetDexPCPtr(reinterpret_cast<uint16_t*>(inst));
726 ArtMethod* referrer = shadow_frame->GetMethod();
727 uint32_t field_idx = kIsStatic ? inst->VRegB_21c() : inst->VRegC_22c();
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700728 ArtField* field = FindFieldFromCode<kAccessType, /* access_checks= */ false>(
David Srbeckyce32c102018-08-31 07:21:07 +0100729 field_idx, referrer, self, sizeof(PrimType));
730 if (UNLIKELY(field == nullptr)) {
731 DCHECK(self->IsExceptionPending());
732 return false;
733 }
734 ObjPtr<mirror::Object> obj = kIsStatic
735 ? field->GetDeclaringClass().Ptr()
736 : shadow_frame->GetVRegReference(inst->VRegB_22c(inst_data));
737 if (UNLIKELY(obj == nullptr)) {
738 ThrowNullPointerExceptionForFieldAccess(field, kIsRead);
739 return false;
740 }
741 MterpFieldAccess<PrimType, kAccessType>(
742 inst, inst_data, shadow_frame, obj, field->GetOffset(), field->IsVolatile());
743 return true;
744}
745
David Srbeckyef79aa32018-09-08 18:02:36 +0100746// This methods is called from assembly to handle field access instructions.
747//
748// This method is fairly hot. It is long, but it has been carefully optimized.
749// It contains only fully inlined methods -> no spills -> no prologue/epilogue.
David Srbeckyce32c102018-08-31 07:21:07 +0100750template<typename PrimType, FindFieldType kAccessType>
751ALWAYS_INLINE bool MterpFieldAccessFast(Instruction* inst,
752 uint16_t inst_data,
753 ShadowFrame* shadow_frame,
754 Thread* self)
755 REQUIRES_SHARED(Locks::mutator_lock_) {
756 constexpr bool kIsStatic = (kAccessType & FindFieldFlags::StaticBit) != 0;
David Srbeckyffa15ea2018-08-16 10:04:11 +0100757
David Srbeckyef79aa32018-09-08 18:02:36 +0100758 // Try to find the field in small thread-local cache first.
759 InterpreterCache* tls_cache = self->GetInterpreterCache();
760 size_t tls_value;
761 if (LIKELY(tls_cache->Get(inst, &tls_value))) {
762 // The meaning of the cache value is opcode-specific.
763 // It is ArtFiled* for static fields and the raw offset for instance fields.
764 size_t offset = kIsStatic
765 ? reinterpret_cast<ArtField*>(tls_value)->GetOffset().SizeValue()
766 : tls_value;
767 if (kIsDebugBuild) {
768 uint32_t field_idx = kIsStatic ? inst->VRegB_21c() : inst->VRegC_22c();
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700769 ArtField* field = FindFieldFromCode<kAccessType, /* access_checks= */ false>(
David Srbeckyef79aa32018-09-08 18:02:36 +0100770 field_idx, shadow_frame->GetMethod(), self, sizeof(PrimType));
771 DCHECK_EQ(offset, field->GetOffset().SizeValue());
772 }
773 ObjPtr<mirror::Object> obj = kIsStatic
774 ? reinterpret_cast<ArtField*>(tls_value)->GetDeclaringClass()
Vladimir Markod7e9bbf2019-03-28 13:18:57 +0000775 : ObjPtr<mirror::Object>(shadow_frame->GetVRegReference(inst->VRegB_22c(inst_data)));
David Srbeckyef79aa32018-09-08 18:02:36 +0100776 if (LIKELY(obj != nullptr)) {
777 MterpFieldAccess<PrimType, kAccessType>(
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700778 inst, inst_data, shadow_frame, obj, MemberOffset(offset), /* is_volatile= */ false);
David Srbeckyef79aa32018-09-08 18:02:36 +0100779 return true;
780 }
781 }
782
David Srbeckyffa15ea2018-08-16 10:04:11 +0100783 // This effectively inlines the fast path from ArtMethod::GetDexCache.
David Srbeckyce32c102018-08-31 07:21:07 +0100784 ArtMethod* referrer = shadow_frame->GetMethod();
David Srbeckyffa15ea2018-08-16 10:04:11 +0100785 if (LIKELY(!referrer->IsObsolete())) {
786 // Avoid read barriers, since we need only the pointer to the native (non-movable)
787 // DexCache field array which we can get even through from-space objects.
788 ObjPtr<mirror::Class> klass = referrer->GetDeclaringClass<kWithoutReadBarrier>();
Vladimir Markoc524e9e2019-03-26 10:54:50 +0000789 ObjPtr<mirror::DexCache> dex_cache =
790 klass->GetDexCache<kDefaultVerifyFlags, kWithoutReadBarrier>();
David Srbeckyce32c102018-08-31 07:21:07 +0100791
David Srbeckyffa15ea2018-08-16 10:04:11 +0100792 // Try to find the desired field in DexCache.
David Srbeckyce32c102018-08-31 07:21:07 +0100793 uint32_t field_idx = kIsStatic ? inst->VRegB_21c() : inst->VRegC_22c();
David Srbeckyffa15ea2018-08-16 10:04:11 +0100794 ArtField* field = dex_cache->GetResolvedField(field_idx, kRuntimePointerSize);
David Srbeckyce32c102018-08-31 07:21:07 +0100795 if (LIKELY(field != nullptr)) {
796 bool initialized = !kIsStatic || field->GetDeclaringClass()->IsInitialized();
797 if (LIKELY(initialized)) {
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700798 DCHECK_EQ(field, (FindFieldFromCode<kAccessType, /* access_checks= */ false>(
David Srbeckyffa15ea2018-08-16 10:04:11 +0100799 field_idx, referrer, self, sizeof(PrimType))));
David Srbeckyce32c102018-08-31 07:21:07 +0100800 ObjPtr<mirror::Object> obj = kIsStatic
801 ? field->GetDeclaringClass().Ptr()
802 : shadow_frame->GetVRegReference(inst->VRegB_22c(inst_data));
803 if (LIKELY(kIsStatic || obj != nullptr)) {
David Srbeckyef79aa32018-09-08 18:02:36 +0100804 // Only non-volatile fields are allowed in the thread-local cache.
805 if (LIKELY(!field->IsVolatile())) {
806 if (kIsStatic) {
807 tls_cache->Set(inst, reinterpret_cast<uintptr_t>(field));
808 } else {
809 tls_cache->Set(inst, field->GetOffset().SizeValue());
810 }
811 }
David Srbeckyce32c102018-08-31 07:21:07 +0100812 MterpFieldAccess<PrimType, kAccessType>(
813 inst, inst_data, shadow_frame, obj, field->GetOffset(), field->IsVolatile());
814 return true;
815 }
David Srbeckyffa15ea2018-08-16 10:04:11 +0100816 }
David Srbeckyffa15ea2018-08-16 10:04:11 +0100817 }
818 }
David Srbeckyce32c102018-08-31 07:21:07 +0100819
David Srbeckyffa15ea2018-08-16 10:04:11 +0100820 // Slow path. Last and with identical arguments so that it becomes single instruction tail call.
David Srbeckyce32c102018-08-31 07:21:07 +0100821 return MterpFieldAccessSlow<PrimType, kAccessType>(inst, inst_data, shadow_frame, self);
David Srbeckyffa15ea2018-08-16 10:04:11 +0100822}
823
David Srbeckyce32c102018-08-31 07:21:07 +0100824#define MTERP_FIELD_ACCESSOR(Name, PrimType, AccessType) \
825extern "C" bool Name(Instruction* inst, uint16_t inst_data, ShadowFrame* sf, Thread* self) \
826 REQUIRES_SHARED(Locks::mutator_lock_) { \
827 return MterpFieldAccessFast<PrimType, AccessType>(inst, inst_data, sf, self); \
buzbee1452bee2015-03-06 14:43:04 -0800828}
829
David Srbeckyce32c102018-08-31 07:21:07 +0100830#define MTERP_FIELD_ACCESSORS_FOR_TYPE(Sufix, PrimType, Kind) \
831 MTERP_FIELD_ACCESSOR(MterpIGet##Sufix, PrimType, Instance##Kind##Read) \
832 MTERP_FIELD_ACCESSOR(MterpIPut##Sufix, PrimType, Instance##Kind##Write) \
833 MTERP_FIELD_ACCESSOR(MterpSGet##Sufix, PrimType, Static##Kind##Read) \
834 MTERP_FIELD_ACCESSOR(MterpSPut##Sufix, PrimType, Static##Kind##Write)
buzbee1452bee2015-03-06 14:43:04 -0800835
David Srbeckyce32c102018-08-31 07:21:07 +0100836MTERP_FIELD_ACCESSORS_FOR_TYPE(I8, int8_t, Primitive)
837MTERP_FIELD_ACCESSORS_FOR_TYPE(U8, uint8_t, Primitive)
838MTERP_FIELD_ACCESSORS_FOR_TYPE(I16, int16_t, Primitive)
839MTERP_FIELD_ACCESSORS_FOR_TYPE(U16, uint16_t, Primitive)
840MTERP_FIELD_ACCESSORS_FOR_TYPE(U32, uint32_t, Primitive)
841MTERP_FIELD_ACCESSORS_FOR_TYPE(U64, uint64_t, Primitive)
842MTERP_FIELD_ACCESSORS_FOR_TYPE(Obj, uint32_t, Object)
David Srbecky28dfc592018-08-22 15:29:09 +0100843
David Srbeckyce32c102018-08-31 07:21:07 +0100844// Check that the primitive type for Obj variant above is correct.
845// It really must be primitive type for the templates to compile.
846// In the case of objects, it is only used to get the field size.
847static_assert(kHeapReferenceSize == sizeof(uint32_t), "Unexpected kHeapReferenceSize");
David Srbecky28dfc592018-08-22 15:29:09 +0100848
David Srbeckyce32c102018-08-31 07:21:07 +0100849#undef MTERP_FIELD_ACCESSORS_FOR_TYPE
850#undef MTERP_FIELD_ACCESSOR
buzbeefa6adfd2017-02-22 13:40:59 -0800851
852extern "C" mirror::Object* artAGetObjectFromMterp(mirror::Object* arr,
853 int32_t index)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700854 REQUIRES_SHARED(Locks::mutator_lock_) {
buzbee1452bee2015-03-06 14:43:04 -0800855 if (UNLIKELY(arr == nullptr)) {
856 ThrowNullPointerExceptionFromInterpreter();
857 return nullptr;
858 }
Vladimir Marko423bebb2019-03-26 15:17:21 +0000859 ObjPtr<mirror::ObjectArray<mirror::Object>> array = arr->AsObjectArray<mirror::Object>();
buzbee1452bee2015-03-06 14:43:04 -0800860 if (LIKELY(array->CheckIsValidIndex(index))) {
Vladimir Marko423bebb2019-03-26 15:17:21 +0000861 return array->GetWithoutChecks(index).Ptr();
buzbee1452bee2015-03-06 14:43:04 -0800862 } else {
863 return nullptr;
864 }
865}
866
buzbeefa6adfd2017-02-22 13:40:59 -0800867extern "C" mirror::Object* artIGetObjectFromMterp(mirror::Object* obj,
868 uint32_t field_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700869 REQUIRES_SHARED(Locks::mutator_lock_) {
buzbee76833da2016-01-13 13:06:22 -0800870 if (UNLIKELY(obj == nullptr)) {
871 ThrowNullPointerExceptionFromInterpreter();
872 return nullptr;
873 }
874 return obj->GetFieldObject<mirror::Object>(MemberOffset(field_offset));
875}
876
Bill Buzbee1d011d92016-04-04 16:59:29 +0000877/*
878 * Create a hotness_countdown based on the current method hotness_count and profiling
879 * mode. In short, determine how many hotness events we hit before reporting back
880 * to the full instrumentation via MterpAddHotnessBatch. Called once on entry to the method,
881 * and regenerated following batch updates.
882 */
Vladimir Markoa710d912017-09-12 14:56:07 +0100883extern "C" ssize_t MterpSetUpHotnessCountdown(ArtMethod* method,
884 ShadowFrame* shadow_frame,
885 Thread* self)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700886 REQUIRES_SHARED(Locks::mutator_lock_) {
Bill Buzbee1d011d92016-04-04 16:59:29 +0000887 uint16_t hotness_count = method->GetCounter();
888 int32_t countdown_value = jit::kJitHotnessDisabled;
889 jit::Jit* jit = Runtime::Current()->GetJit();
890 if (jit != nullptr) {
Nicolas Geoffray0402f4b2018-11-29 19:18:46 +0000891 int32_t warm_threshold = jit->WarmMethodThreshold();
892 int32_t hot_threshold = jit->HotMethodThreshold();
893 int32_t osr_threshold = jit->OSRMethodThreshold();
Bill Buzbee1d011d92016-04-04 16:59:29 +0000894 if (hotness_count < warm_threshold) {
895 countdown_value = warm_threshold - hotness_count;
896 } else if (hotness_count < hot_threshold) {
897 countdown_value = hot_threshold - hotness_count;
898 } else if (hotness_count < osr_threshold) {
899 countdown_value = osr_threshold - hotness_count;
900 } else {
901 countdown_value = jit::kJitCheckForOSR;
902 }
Vladimir Markoa710d912017-09-12 14:56:07 +0100903 if (jit::Jit::ShouldUsePriorityThreadWeight(self)) {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100904 int32_t priority_thread_weight = jit->PriorityThreadWeight();
Calin Juravleb2771b42016-04-07 17:09:25 +0100905 countdown_value = std::min(countdown_value, countdown_value / priority_thread_weight);
906 }
Bill Buzbee1d011d92016-04-04 16:59:29 +0000907 }
908 /*
909 * The actual hotness threshold may exceed the range of our int16_t countdown value. This is
910 * not a problem, though. We can just break it down into smaller chunks.
911 */
912 countdown_value = std::min(countdown_value,
913 static_cast<int32_t>(std::numeric_limits<int16_t>::max()));
914 shadow_frame->SetCachedHotnessCountdown(countdown_value);
915 shadow_frame->SetHotnessCountdown(countdown_value);
916 return countdown_value;
917}
918
919/*
920 * Report a batch of hotness events to the instrumentation and then return the new
921 * countdown value to the next time we should report.
922 */
Andreas Gampe67409972016-07-19 22:34:53 -0700923extern "C" ssize_t MterpAddHotnessBatch(ArtMethod* method,
Bill Buzbee1d011d92016-04-04 16:59:29 +0000924 ShadowFrame* shadow_frame,
925 Thread* self)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700926 REQUIRES_SHARED(Locks::mutator_lock_) {
Bill Buzbee1d011d92016-04-04 16:59:29 +0000927 jit::Jit* jit = Runtime::Current()->GetJit();
928 if (jit != nullptr) {
929 int16_t count = shadow_frame->GetCachedHotnessCountdown() - shadow_frame->GetHotnessCountdown();
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700930 jit->AddSamples(self, method, count, /*with_backedges=*/ true);
Bill Buzbee1d011d92016-04-04 16:59:29 +0000931 }
Vladimir Markoa710d912017-09-12 14:56:07 +0100932 return MterpSetUpHotnessCountdown(method, shadow_frame, self);
Bill Buzbee1d011d92016-04-04 16:59:29 +0000933}
934
Andreas Gampe67409972016-07-19 22:34:53 -0700935extern "C" size_t MterpMaybeDoOnStackReplacement(Thread* self,
936 ShadowFrame* shadow_frame,
937 int32_t offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700938 REQUIRES_SHARED(Locks::mutator_lock_) {
buzbee42a09cb02017-02-01 09:08:31 -0800939 int16_t osr_countdown = shadow_frame->GetCachedHotnessCountdown() - 1;
940 bool did_osr = false;
941 /*
942 * To reduce the cost of polling the compiler to determine whether the requested OSR
943 * compilation has completed, only check every Nth time. NOTE: the "osr_countdown <= 0"
944 * condition is satisfied either by the decrement below or the initial setting of
945 * the cached countdown field to kJitCheckForOSR, which elsewhere is asserted to be -1.
946 */
947 if (osr_countdown <= 0) {
948 ArtMethod* method = shadow_frame->GetMethod();
949 JValue* result = shadow_frame->GetResultRegister();
950 uint32_t dex_pc = shadow_frame->GetDexPC();
951 jit::Jit* jit = Runtime::Current()->GetJit();
952 osr_countdown = jit::Jit::kJitRecheckOSRThreshold;
953 if (offset <= 0) {
954 // Keep updating hotness in case a compilation request was dropped. Eventually it will retry.
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700955 jit->AddSamples(self, method, osr_countdown, /*with_backedges=*/ true);
buzbee42a09cb02017-02-01 09:08:31 -0800956 }
957 did_osr = jit::Jit::MaybeDoOnStackReplacement(self, method, dex_pc, offset, result);
buzbee0e6aa6d2016-04-11 07:48:18 -0700958 }
buzbee42a09cb02017-02-01 09:08:31 -0800959 shadow_frame->SetCachedHotnessCountdown(osr_countdown);
Andreas Gampe7c5acbb2018-09-20 13:54:52 -0700960 return did_osr ? 1u : 0u;
Bill Buzbeefd522f92016-02-11 22:37:42 +0000961}
962
buzbee1452bee2015-03-06 14:43:04 -0800963} // namespace interpreter
964} // namespace art