blob: 4fd1514e39331608d32173a45026a54b8dfedd76 [file] [log] [blame]
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001/*
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
17#ifndef ART_RUNTIME_INTERPRETER_INTERPRETER_COMMON_H_
18#define ART_RUNTIME_INTERPRETER_INTERPRETER_COMMON_H_
19
20#include "interpreter.h"
21
22#include <math.h>
23
Ian Rogerscf7f1912014-10-22 22:06:39 -070024#include <iostream>
Ian Rogersc7dd2952014-10-21 23:31:19 -070025#include <sstream>
26
Mathieu Chartierc7853442015-03-27 14:35:38 -070027#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070028#include "art_method-inl.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070029#include "base/enums.h"
Sebastien Hertz8ece0502013-08-07 11:26:41 +020030#include "base/logging.h"
Andreas Gampe794ad762015-02-23 08:12:24 -080031#include "base/macros.h"
Sebastien Hertz8ece0502013-08-07 11:26:41 +020032#include "class_linker-inl.h"
33#include "common_throws.h"
34#include "dex_file-inl.h"
35#include "dex_instruction-inl.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070036#include "entrypoints/entrypoint_utils-inl.h"
Mathieu Chartier0cd81352014-05-22 16:48:55 -070037#include "handle_scope-inl.h"
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +010038#include "jit/jit.h"
Sebastien Hertz8ece0502013-08-07 11:26:41 +020039#include "mirror/class-inl.h"
40#include "mirror/object-inl.h"
41#include "mirror/object_array-inl.h"
Douglas Leung4965c022014-06-11 11:41:11 -070042#include "mirror/string-inl.h"
Andreas Gampe03ec9302015-08-27 17:41:47 -070043#include "stack.h"
Sebastien Hertz8ece0502013-08-07 11:26:41 +020044#include "thread.h"
45#include "well_known_classes.h"
46
Mathieu Chartiere401d142015-04-22 13:56:20 -070047using ::art::ArtMethod;
Sebastien Hertz8ece0502013-08-07 11:26:41 +020048using ::art::mirror::Array;
49using ::art::mirror::BooleanArray;
50using ::art::mirror::ByteArray;
51using ::art::mirror::CharArray;
52using ::art::mirror::Class;
53using ::art::mirror::ClassLoader;
54using ::art::mirror::IntArray;
55using ::art::mirror::LongArray;
56using ::art::mirror::Object;
57using ::art::mirror::ObjectArray;
58using ::art::mirror::ShortArray;
59using ::art::mirror::String;
60using ::art::mirror::Throwable;
61
62namespace art {
63namespace interpreter {
64
buzbee1452bee2015-03-06 14:43:04 -080065// External references to all interpreter implementations.
Sebastien Hertz8ece0502013-08-07 11:26:41 +020066
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010067template<bool do_access_check, bool transaction_active>
Ian Rogerse94652f2014-12-02 11:13:19 -080068extern JValue ExecuteSwitchImpl(Thread* self, const DexFile::CodeItem* code_item,
buzbee1452bee2015-03-06 14:43:04 -080069 ShadowFrame& shadow_frame, JValue result_register,
70 bool interpret_one_instruction);
Sebastien Hertz8ece0502013-08-07 11:26:41 +020071
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010072template<bool do_access_check, bool transaction_active>
Ian Rogerse94652f2014-12-02 11:13:19 -080073extern JValue ExecuteGotoImpl(Thread* self, const DexFile::CodeItem* code_item,
Sebastien Hertzc6714852013-09-30 16:42:32 +020074 ShadowFrame& shadow_frame, JValue result_register);
Sebastien Hertz8ece0502013-08-07 11:26:41 +020075
buzbee1452bee2015-03-06 14:43:04 -080076// Mterp does not support transactions or access check, thus no templated versions.
77extern "C" bool ExecuteMterpImpl(Thread* self, const DexFile::CodeItem* code_item,
78 ShadowFrame* shadow_frame, JValue* result_register);
79
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000080void ThrowNullPointerExceptionFromInterpreter()
Mathieu Chartier90443472015-07-16 20:32:27 -070081 SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertzda843e12014-05-28 19:28:31 +020082
Andreas Gampe03ec9302015-08-27 17:41:47 -070083template <bool kMonitorCounting>
84static inline void DoMonitorEnter(Thread* self,
85 ShadowFrame* frame,
Mathieu Chartier2d096c92015-10-12 16:18:20 -070086 Object* ref)
87 NO_THREAD_SAFETY_ANALYSIS
88 REQUIRES(!Roles::uninterruptible_) {
89 StackHandleScope<1> hs(self);
90 Handle<Object> h_ref(hs.NewHandle(ref));
91 h_ref->MonitorEnter(self);
Andreas Gampe56fdd0e2016-04-28 14:56:54 -070092 if (kMonitorCounting && frame->GetMethod()->MustCountLocks()) {
93 frame->GetLockCountData().AddMonitor(self, h_ref.Get());
94 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +020095}
96
Andreas Gampe03ec9302015-08-27 17:41:47 -070097template <bool kMonitorCounting>
98static inline void DoMonitorExit(Thread* self,
99 ShadowFrame* frame,
Mathieu Chartier2d096c92015-10-12 16:18:20 -0700100 Object* ref)
101 NO_THREAD_SAFETY_ANALYSIS
102 REQUIRES(!Roles::uninterruptible_) {
103 StackHandleScope<1> hs(self);
104 Handle<Object> h_ref(hs.NewHandle(ref));
105 h_ref->MonitorExit(self);
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700106 if (kMonitorCounting && frame->GetMethod()->MustCountLocks()) {
107 frame->GetLockCountData().RemoveMonitorOrThrow(self, h_ref.Get());
108 }
109}
110
111template <bool kMonitorCounting>
112static inline bool DoMonitorCheckOnExit(Thread* self, ShadowFrame* frame)
113 NO_THREAD_SAFETY_ANALYSIS
114 REQUIRES(!Roles::uninterruptible_) {
115 if (kMonitorCounting && frame->GetMethod()->MustCountLocks()) {
116 return frame->GetLockCountData().CheckAllMonitorsReleasedOrThrow(self);
117 }
118 return true;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200119}
120
Sebastien Hertz45b15972015-04-03 16:07:05 +0200121void AbortTransactionF(Thread* self, const char* fmt, ...)
122 __attribute__((__format__(__printf__, 2, 3)))
Mathieu Chartier90443472015-07-16 20:32:27 -0700123 SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertz45b15972015-04-03 16:07:05 +0200124
125void AbortTransactionV(Thread* self, const char* fmt, va_list args)
Mathieu Chartier90443472015-07-16 20:32:27 -0700126 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700127
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100128void RecordArrayElementsInTransaction(mirror::Array* array, int32_t count)
Mathieu Chartier90443472015-07-16 20:32:27 -0700129 SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100130
Sebastien Hertzc6714852013-09-30 16:42:32 +0200131// Invokes the given method. This is part of the invocation support and is used by DoInvoke and
132// DoInvokeVirtualQuick functions.
133// Returns true on success, otherwise throws an exception and returns false.
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200134template<bool is_range, bool do_assignability_check>
Ian Rogerse94652f2014-12-02 11:13:19 -0800135bool DoCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame,
Sebastien Hertzc6714852013-09-30 16:42:32 +0200136 const Instruction* inst, uint16_t inst_data, JValue* result);
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200137
Narayan Kamath14832ef2016-08-05 11:44:32 +0100138// Handles invoke-XXX/range instructions.
Sebastien Hertzc6714852013-09-30 16:42:32 +0200139// Returns true on success, otherwise throws an exception and returns false.
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200140template<InvokeType type, bool is_range, bool do_access_check>
141static inline bool DoInvoke(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst,
142 uint16_t inst_data, JValue* result) {
143 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
144 const uint32_t vregC = (is_range) ? inst->VRegC_3rc() : inst->VRegC_35c();
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700145 Object* receiver = (type == kStatic) ? nullptr : shadow_frame.GetVRegReference(vregC);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700146 ArtMethod* sf_method = shadow_frame.GetMethod();
Ian Rogerse94652f2014-12-02 11:13:19 -0800147 ArtMethod* const called_method = FindMethodFromCode<type, do_access_check>(
Andreas Gampe3a357142015-08-07 17:20:11 -0700148 method_idx, &receiver, sf_method, self);
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700149 // The shadow frame should already be pushed, so we don't need to update it.
Ian Rogerse94652f2014-12-02 11:13:19 -0800150 if (UNLIKELY(called_method == nullptr)) {
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200151 CHECK(self->IsExceptionPending());
152 result->SetJ(0);
153 return false;
Alex Light9139e002015-10-09 15:59:48 -0700154 } else if (UNLIKELY(!called_method->IsInvokable())) {
155 called_method->ThrowInvocationTimeError();
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200156 result->SetJ(0);
157 return false;
158 } else {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100159 jit::Jit* jit = Runtime::Current()->GetJit();
160 if (jit != nullptr) {
161 if (type == kVirtual || type == kInterface) {
162 jit->InvokeVirtualOrInterface(
163 self, receiver, sf_method, shadow_frame.GetDexPC(), called_method);
164 }
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100165 jit->AddSamples(self, sf_method, 1, /*with_backedges*/false);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100166 }
167 // TODO: Remove the InvokeVirtualOrInterface instrumentation, as it was only used by the JIT.
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100168 if (type == kVirtual || type == kInterface) {
169 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
170 if (UNLIKELY(instrumentation->HasInvokeVirtualOrInterfaceListeners())) {
171 instrumentation->InvokeVirtualOrInterface(
172 self, receiver, sf_method, shadow_frame.GetDexPC(), called_method);
173 }
174 }
Ian Rogerse94652f2014-12-02 11:13:19 -0800175 return DoCall<is_range, do_access_check>(called_method, self, shadow_frame, inst, inst_data,
176 result);
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200177 }
178}
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200179
Sebastien Hertzc6714852013-09-30 16:42:32 +0200180// Handles invoke-virtual-quick and invoke-virtual-quick-range instructions.
181// Returns true on success, otherwise throws an exception and returns false.
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200182template<bool is_range>
183static inline bool DoInvokeVirtualQuick(Thread* self, ShadowFrame& shadow_frame,
184 const Instruction* inst, uint16_t inst_data,
185 JValue* result) {
186 const uint32_t vregC = (is_range) ? inst->VRegC_3rc() : inst->VRegC_35c();
187 Object* const receiver = shadow_frame.GetVRegReference(vregC);
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200188 if (UNLIKELY(receiver == nullptr)) {
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200189 // We lost the reference to the method index so we cannot get a more
190 // precised exception message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000191 ThrowNullPointerExceptionFromDexPC();
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200192 return false;
193 }
194 const uint32_t vtable_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000195 CHECK(receiver->GetClass()->ShouldHaveEmbeddedVTable());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700196 ArtMethod* const called_method = receiver->GetClass()->GetEmbeddedVTableEntry(
Andreas Gampe542451c2016-07-26 09:02:02 -0700197 vtable_idx, kRuntimePointerSize);
Ian Rogerse94652f2014-12-02 11:13:19 -0800198 if (UNLIKELY(called_method == nullptr)) {
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200199 CHECK(self->IsExceptionPending());
200 result->SetJ(0);
201 return false;
Alex Light9139e002015-10-09 15:59:48 -0700202 } else if (UNLIKELY(!called_method->IsInvokable())) {
203 called_method->ThrowInvocationTimeError();
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200204 result->SetJ(0);
205 return false;
206 } else {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100207 jit::Jit* jit = Runtime::Current()->GetJit();
208 if (jit != nullptr) {
209 jit->InvokeVirtualOrInterface(
210 self, receiver, shadow_frame.GetMethod(), shadow_frame.GetDexPC(), called_method);
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100211 jit->AddSamples(self, shadow_frame.GetMethod(), 1, /*with_backedges*/false);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100212 }
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100213 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100214 // TODO: Remove the InvokeVirtualOrInterface instrumentation, as it was only used by the JIT.
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100215 if (UNLIKELY(instrumentation->HasInvokeVirtualOrInterfaceListeners())) {
216 instrumentation->InvokeVirtualOrInterface(
217 self, receiver, shadow_frame.GetMethod(), shadow_frame.GetDexPC(), called_method);
218 }
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200219 // No need to check since we've been quickened.
Ian Rogerse94652f2014-12-02 11:13:19 -0800220 return DoCall<is_range, false>(called_method, self, shadow_frame, inst, inst_data, result);
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200221 }
222}
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200223
Sebastien Hertzc6714852013-09-30 16:42:32 +0200224// Handles iget-XXX and sget-XXX instructions.
225// Returns true on success, otherwise throws an exception and returns false.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200226template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check>
Ian Rogers54874942014-06-10 16:31:03 -0700227bool DoFieldGet(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst,
Mathieu Chartier90443472015-07-16 20:32:27 -0700228 uint16_t inst_data) SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200229
Sebastien Hertzc6714852013-09-30 16:42:32 +0200230// Handles iget-quick, iget-wide-quick and iget-object-quick instructions.
231// Returns true on success, otherwise throws an exception and returns false.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200232template<Primitive::Type field_type>
Ian Rogers54874942014-06-10 16:31:03 -0700233bool DoIGetQuick(ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data)
Mathieu Chartier90443472015-07-16 20:32:27 -0700234 SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertz479fc1e2014-04-04 17:51:34 +0200235
Sebastien Hertzc6714852013-09-30 16:42:32 +0200236// Handles iput-XXX and sput-XXX instructions.
237// Returns true on success, otherwise throws an exception and returns false.
Ian Rogers54874942014-06-10 16:31:03 -0700238template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check,
239 bool transaction_active>
240bool DoFieldPut(Thread* self, const ShadowFrame& shadow_frame, const Instruction* inst,
Mathieu Chartier90443472015-07-16 20:32:27 -0700241 uint16_t inst_data) SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200242
Sebastien Hertzc6714852013-09-30 16:42:32 +0200243// Handles iput-quick, iput-wide-quick and iput-object-quick instructions.
244// Returns true on success, otherwise throws an exception and returns false.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100245template<Primitive::Type field_type, bool transaction_active>
Ian Rogers54874942014-06-10 16:31:03 -0700246bool DoIPutQuick(const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data)
Mathieu Chartier90443472015-07-16 20:32:27 -0700247 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers54874942014-06-10 16:31:03 -0700248
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200249
Sebastien Hertzc6714852013-09-30 16:42:32 +0200250// Handles string resolution for const-string and const-string-jumbo instructions. Also ensures the
251// java.lang.String class is initialized.
Ian Rogers6786a582014-10-28 12:49:06 -0700252static inline String* ResolveString(Thread* self, ShadowFrame& shadow_frame, uint32_t string_idx)
Mathieu Chartier90443472015-07-16 20:32:27 -0700253 SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200254 Class* java_lang_string_class = String::GetJavaLangString();
255 if (UNLIKELY(!java_lang_string_class->IsInitialized())) {
256 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700257 StackHandleScope<1> hs(self);
258 Handle<mirror::Class> h_class(hs.NewHandle(java_lang_string_class));
Ian Rogers7b078e82014-09-10 14:44:24 -0700259 if (UNLIKELY(!class_linker->EnsureInitialized(self, h_class, true, true))) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200260 DCHECK(self->IsExceptionPending());
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800261 return nullptr;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200262 }
263 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700264 ArtMethod* method = shadow_frame.GetMethod();
Mathieu Chartiereace4582014-11-24 18:29:54 -0800265 mirror::Class* declaring_class = method->GetDeclaringClass();
Vladimir Marko05792b92015-08-03 11:56:49 +0100266 // MethodVerifier refuses methods with string_idx out of bounds.
267 DCHECK_LT(string_idx, declaring_class->GetDexCache()->NumStrings());
268 mirror::String* s = declaring_class->GetDexCacheStrings()[string_idx].Read();
Ian Rogers6786a582014-10-28 12:49:06 -0700269 if (UNLIKELY(s == nullptr)) {
270 StackHandleScope<1> hs(self);
Mathieu Chartiereace4582014-11-24 18:29:54 -0800271 Handle<mirror::DexCache> dex_cache(hs.NewHandle(declaring_class->GetDexCache()));
Ian Rogers6786a582014-10-28 12:49:06 -0700272 s = Runtime::Current()->GetClassLinker()->ResolveString(*method->GetDexFile(), string_idx,
273 dex_cache);
274 }
275 return s;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200276}
277
Sebastien Hertzc6714852013-09-30 16:42:32 +0200278// Handles div-int, div-int/2addr, div-int/li16 and div-int/lit8 instructions.
279// Returns true on success, otherwise throws a java.lang.ArithmeticException and return false.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200280static inline bool DoIntDivide(ShadowFrame& shadow_frame, size_t result_reg,
281 int32_t dividend, int32_t divisor)
Mathieu Chartier90443472015-07-16 20:32:27 -0700282 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersf72a11d2014-10-30 15:41:08 -0700283 constexpr int32_t kMinInt = std::numeric_limits<int32_t>::min();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200284 if (UNLIKELY(divisor == 0)) {
285 ThrowArithmeticExceptionDivideByZero();
286 return false;
287 }
288 if (UNLIKELY(dividend == kMinInt && divisor == -1)) {
289 shadow_frame.SetVReg(result_reg, kMinInt);
290 } else {
291 shadow_frame.SetVReg(result_reg, dividend / divisor);
292 }
293 return true;
294}
295
Sebastien Hertzc6714852013-09-30 16:42:32 +0200296// Handles rem-int, rem-int/2addr, rem-int/li16 and rem-int/lit8 instructions.
297// Returns true on success, otherwise throws a java.lang.ArithmeticException and return false.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200298static inline bool DoIntRemainder(ShadowFrame& shadow_frame, size_t result_reg,
299 int32_t dividend, int32_t divisor)
Mathieu Chartier90443472015-07-16 20:32:27 -0700300 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersf72a11d2014-10-30 15:41:08 -0700301 constexpr int32_t kMinInt = std::numeric_limits<int32_t>::min();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200302 if (UNLIKELY(divisor == 0)) {
303 ThrowArithmeticExceptionDivideByZero();
304 return false;
305 }
306 if (UNLIKELY(dividend == kMinInt && divisor == -1)) {
307 shadow_frame.SetVReg(result_reg, 0);
308 } else {
309 shadow_frame.SetVReg(result_reg, dividend % divisor);
310 }
311 return true;
312}
313
Sebastien Hertzc6714852013-09-30 16:42:32 +0200314// Handles div-long and div-long-2addr instructions.
315// Returns true on success, otherwise throws a java.lang.ArithmeticException and return false.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200316static inline bool DoLongDivide(ShadowFrame& shadow_frame, size_t result_reg,
317 int64_t dividend, int64_t divisor)
Mathieu Chartier90443472015-07-16 20:32:27 -0700318 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers2e2deeb2013-09-23 11:58:57 -0700319 const int64_t kMinLong = std::numeric_limits<int64_t>::min();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200320 if (UNLIKELY(divisor == 0)) {
321 ThrowArithmeticExceptionDivideByZero();
322 return false;
323 }
324 if (UNLIKELY(dividend == kMinLong && divisor == -1)) {
325 shadow_frame.SetVRegLong(result_reg, kMinLong);
326 } else {
327 shadow_frame.SetVRegLong(result_reg, dividend / divisor);
328 }
329 return true;
330}
331
Sebastien Hertzc6714852013-09-30 16:42:32 +0200332// Handles rem-long and rem-long-2addr instructions.
333// Returns true on success, otherwise throws a java.lang.ArithmeticException and return false.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200334static inline bool DoLongRemainder(ShadowFrame& shadow_frame, size_t result_reg,
335 int64_t dividend, int64_t divisor)
Mathieu Chartier90443472015-07-16 20:32:27 -0700336 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers2e2deeb2013-09-23 11:58:57 -0700337 const int64_t kMinLong = std::numeric_limits<int64_t>::min();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200338 if (UNLIKELY(divisor == 0)) {
339 ThrowArithmeticExceptionDivideByZero();
340 return false;
341 }
342 if (UNLIKELY(dividend == kMinLong && divisor == -1)) {
343 shadow_frame.SetVRegLong(result_reg, 0);
344 } else {
345 shadow_frame.SetVRegLong(result_reg, dividend % divisor);
346 }
347 return true;
348}
349
Sebastien Hertzc6714852013-09-30 16:42:32 +0200350// Handles filled-new-array and filled-new-array-range instructions.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200351// Returns true on success, otherwise throws an exception and returns false.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100352template <bool is_range, bool do_access_check, bool transaction_active>
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200353bool DoFilledNewArray(const Instruction* inst, const ShadowFrame& shadow_frame,
Sebastien Hertzc6714852013-09-30 16:42:32 +0200354 Thread* self, JValue* result);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200355
Sebastien Hertzc6714852013-09-30 16:42:32 +0200356// Handles packed-switch instruction.
357// Returns the branch offset to the next instruction to execute.
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200358static inline int32_t DoPackedSwitch(const Instruction* inst, const ShadowFrame& shadow_frame,
359 uint16_t inst_data)
Mathieu Chartier90443472015-07-16 20:32:27 -0700360 SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200361 DCHECK(inst->Opcode() == Instruction::PACKED_SWITCH);
362 const uint16_t* switch_data = reinterpret_cast<const uint16_t*>(inst) + inst->VRegB_31t();
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200363 int32_t test_val = shadow_frame.GetVReg(inst->VRegA_31t(inst_data));
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200364 DCHECK_EQ(switch_data[0], static_cast<uint16_t>(Instruction::kPackedSwitchSignature));
365 uint16_t size = switch_data[1];
David Brazdil2ef645b2015-06-17 18:20:52 +0100366 if (size == 0) {
367 // Empty packed switch, move forward by 3 (size of PACKED_SWITCH).
368 return 3;
369 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200370 const int32_t* keys = reinterpret_cast<const int32_t*>(&switch_data[2]);
Roland Levillain14d90572015-07-16 10:52:26 +0100371 DCHECK_ALIGNED(keys, 4);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200372 int32_t first_key = keys[0];
373 const int32_t* targets = reinterpret_cast<const int32_t*>(&switch_data[4]);
Roland Levillain14d90572015-07-16 10:52:26 +0100374 DCHECK_ALIGNED(targets, 4);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200375 int32_t index = test_val - first_key;
376 if (index >= 0 && index < size) {
377 return targets[index];
378 } else {
379 // No corresponding value: move forward by 3 (size of PACKED_SWITCH).
380 return 3;
381 }
382}
383
Sebastien Hertzc6714852013-09-30 16:42:32 +0200384// Handles sparse-switch instruction.
385// Returns the branch offset to the next instruction to execute.
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200386static inline int32_t DoSparseSwitch(const Instruction* inst, const ShadowFrame& shadow_frame,
387 uint16_t inst_data)
Mathieu Chartier90443472015-07-16 20:32:27 -0700388 SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200389 DCHECK(inst->Opcode() == Instruction::SPARSE_SWITCH);
390 const uint16_t* switch_data = reinterpret_cast<const uint16_t*>(inst) + inst->VRegB_31t();
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200391 int32_t test_val = shadow_frame.GetVReg(inst->VRegA_31t(inst_data));
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200392 DCHECK_EQ(switch_data[0], static_cast<uint16_t>(Instruction::kSparseSwitchSignature));
393 uint16_t size = switch_data[1];
Jeff Hao935e01a2015-03-20 19:44:35 -0700394 // Return length of SPARSE_SWITCH if size is 0.
395 if (size == 0) {
396 return 3;
397 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200398 const int32_t* keys = reinterpret_cast<const int32_t*>(&switch_data[2]);
Roland Levillain14d90572015-07-16 10:52:26 +0100399 DCHECK_ALIGNED(keys, 4);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200400 const int32_t* entries = keys + size;
Roland Levillain14d90572015-07-16 10:52:26 +0100401 DCHECK_ALIGNED(entries, 4);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200402 int lo = 0;
403 int hi = size - 1;
404 while (lo <= hi) {
405 int mid = (lo + hi) / 2;
406 int32_t foundVal = keys[mid];
407 if (test_val < foundVal) {
408 hi = mid - 1;
409 } else if (test_val > foundVal) {
410 lo = mid + 1;
411 } else {
412 return entries[mid];
413 }
414 }
415 // No corresponding value: move forward by 3 (size of SPARSE_SWITCH).
416 return 3;
417}
418
Ian Rogers54874942014-06-10 16:31:03 -0700419uint32_t FindNextInstructionFollowingException(Thread* self, ShadowFrame& shadow_frame,
Sebastien Hertz9f102032014-05-23 08:59:42 +0200420 uint32_t dex_pc, const instrumentation::Instrumentation* instrumentation)
Mathieu Chartier90443472015-07-16 20:32:27 -0700421 SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200422
Andreas Gampe794ad762015-02-23 08:12:24 -0800423NO_RETURN void UnexpectedOpcode(const Instruction* inst, const ShadowFrame& shadow_frame)
424 __attribute__((cold))
Mathieu Chartier90443472015-07-16 20:32:27 -0700425 SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200426
Bill Buzbeed47fd902016-07-07 14:42:43 +0000427// Set true if you want TraceExecution invocation before each bytecode execution.
428constexpr bool kTraceExecutionEnabled = false;
Serguei Katkov9fb0ac72016-02-20 12:55:24 +0600429
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200430static inline void TraceExecution(const ShadowFrame& shadow_frame, const Instruction* inst,
Ian Rogerse94652f2014-12-02 11:13:19 -0800431 const uint32_t dex_pc)
Mathieu Chartier90443472015-07-16 20:32:27 -0700432 SHARED_REQUIRES(Locks::mutator_lock_) {
Bill Buzbeed47fd902016-07-07 14:42:43 +0000433 if (kTraceExecutionEnabled) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200434#define TRACE_LOG std::cerr
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700435 std::ostringstream oss;
436 oss << PrettyMethod(shadow_frame.GetMethod())
437 << StringPrintf("\n0x%x: ", dex_pc)
Ian Rogerse94652f2014-12-02 11:13:19 -0800438 << inst->DumpString(shadow_frame.GetMethod()->GetDexFile()) << "\n";
Ian Rogersef7d42f2014-01-06 12:55:46 -0800439 for (uint32_t i = 0; i < shadow_frame.NumberOfVRegs(); ++i) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200440 uint32_t raw_value = shadow_frame.GetVReg(i);
441 Object* ref_value = shadow_frame.GetVRegReference(i);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800442 oss << StringPrintf(" vreg%u=0x%08X", i, raw_value);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700443 if (ref_value != nullptr) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200444 if (ref_value->GetClass()->IsStringClass() &&
Jeff Hao848f70a2014-01-15 13:49:50 -0800445 ref_value->AsString()->GetValue() != nullptr) {
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700446 oss << "/java.lang.String \"" << ref_value->AsString()->ToModifiedUtf8() << "\"";
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200447 } else {
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700448 oss << "/" << PrettyTypeOf(ref_value);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200449 }
450 }
451 }
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700452 TRACE_LOG << oss.str() << "\n";
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200453#undef TRACE_LOG
454 }
455}
456
Sebastien Hertz1eda2262013-09-09 16:53:14 +0200457static inline bool IsBackwardBranch(int32_t branch_offset) {
458 return branch_offset <= 0;
459}
460
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100461void ArtInterpreterToCompiledCodeBridge(Thread* self,
462 ArtMethod* caller,
463 const DexFile::CodeItem* code_item,
464 ShadowFrame* shadow_frame,
465 JValue* result);
Siva Chandra05d24152016-01-05 17:43:17 -0800466
Mingyao Yangffedec52016-05-19 10:48:40 -0700467// Set string value created from StringFactory.newStringFromXXX() into all aliases of
468// StringFactory.newEmptyString().
469void SetStringInitValueToAllAliases(ShadowFrame* shadow_frame,
470 uint16_t this_obj_vreg,
471 JValue result);
472
Sebastien Hertzc6714852013-09-30 16:42:32 +0200473// Explicitly instantiate all DoInvoke functions.
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100474#define EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, _is_range, _do_check) \
Mathieu Chartier90443472015-07-16 20:32:27 -0700475 template SHARED_REQUIRES(Locks::mutator_lock_) \
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100476 bool DoInvoke<_type, _is_range, _do_check>(Thread* self, ShadowFrame& shadow_frame, \
477 const Instruction* inst, uint16_t inst_data, \
478 JValue* result)
Sebastien Hertzc6714852013-09-30 16:42:32 +0200479
480#define EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(_type) \
481 EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, false, false); \
482 EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, false, true); \
483 EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, true, false); \
484 EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, true, true);
485
Andreas Gampec8ccf682014-09-29 20:07:43 -0700486EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(kStatic) // invoke-static/range.
487EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(kDirect) // invoke-direct/range.
488EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(kVirtual) // invoke-virtual/range.
489EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(kSuper) // invoke-super/range.
490EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(kInterface) // invoke-interface/range.
Sebastien Hertzc6714852013-09-30 16:42:32 +0200491#undef EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL
492#undef EXPLICIT_DO_INVOKE_TEMPLATE_DECL
493
Sebastien Hertzc6714852013-09-30 16:42:32 +0200494// Explicitly instantiate all DoInvokeVirtualQuick functions.
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100495#define EXPLICIT_DO_INVOKE_VIRTUAL_QUICK_TEMPLATE_DECL(_is_range) \
Mathieu Chartier90443472015-07-16 20:32:27 -0700496 template SHARED_REQUIRES(Locks::mutator_lock_) \
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100497 bool DoInvokeVirtualQuick<_is_range>(Thread* self, ShadowFrame& shadow_frame, \
498 const Instruction* inst, uint16_t inst_data, \
499 JValue* result)
Sebastien Hertzc6714852013-09-30 16:42:32 +0200500
501EXPLICIT_DO_INVOKE_VIRTUAL_QUICK_TEMPLATE_DECL(false); // invoke-virtual-quick.
502EXPLICIT_DO_INVOKE_VIRTUAL_QUICK_TEMPLATE_DECL(true); // invoke-virtual-quick-range.
503#undef EXPLICIT_INSTANTIATION_DO_INVOKE_VIRTUAL_QUICK
504
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200505} // namespace interpreter
506} // namespace art
507
508#endif // ART_RUNTIME_INTERPRETER_INTERPRETER_COMMON_H_