blob: 0feb013e5bd677b74eef94ff0836979a8fae61a9 [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>
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -070026#include <atomic>
Ian Rogersc7dd2952014-10-21 23:31:19 -070027
Mathieu Chartierc7853442015-03-27 14:35:38 -070028#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070029#include "art_method-inl.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070030#include "base/enums.h"
Sebastien Hertz8ece0502013-08-07 11:26:41 +020031#include "base/logging.h"
Andreas Gampe794ad762015-02-23 08:12:24 -080032#include "base/macros.h"
Sebastien Hertz8ece0502013-08-07 11:26:41 +020033#include "class_linker-inl.h"
34#include "common_throws.h"
35#include "dex_file-inl.h"
36#include "dex_instruction-inl.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070037#include "entrypoints/entrypoint_utils-inl.h"
Mathieu Chartier0cd81352014-05-22 16:48:55 -070038#include "handle_scope-inl.h"
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +010039#include "jit/jit.h"
Sebastien Hertz8ece0502013-08-07 11:26:41 +020040#include "mirror/class-inl.h"
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -070041#include "mirror/dex_cache.h"
42#include "mirror/method.h"
Sebastien Hertz8ece0502013-08-07 11:26:41 +020043#include "mirror/object-inl.h"
44#include "mirror/object_array-inl.h"
Douglas Leung4965c022014-06-11 11:41:11 -070045#include "mirror/string-inl.h"
Andreas Gampe03ec9302015-08-27 17:41:47 -070046#include "stack.h"
Sebastien Hertz8ece0502013-08-07 11:26:41 +020047#include "thread.h"
48#include "well_known_classes.h"
49
Mathieu Chartiere401d142015-04-22 13:56:20 -070050using ::art::ArtMethod;
Sebastien Hertz8ece0502013-08-07 11:26:41 +020051using ::art::mirror::Array;
52using ::art::mirror::BooleanArray;
53using ::art::mirror::ByteArray;
54using ::art::mirror::CharArray;
55using ::art::mirror::Class;
56using ::art::mirror::ClassLoader;
57using ::art::mirror::IntArray;
58using ::art::mirror::LongArray;
59using ::art::mirror::Object;
60using ::art::mirror::ObjectArray;
61using ::art::mirror::ShortArray;
62using ::art::mirror::String;
63using ::art::mirror::Throwable;
64
65namespace art {
66namespace interpreter {
67
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000068void ThrowNullPointerExceptionFromInterpreter()
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070069 REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertzda843e12014-05-28 19:28:31 +020070
Andreas Gampe03ec9302015-08-27 17:41:47 -070071template <bool kMonitorCounting>
72static inline void DoMonitorEnter(Thread* self,
73 ShadowFrame* frame,
Mathieu Chartier2d096c92015-10-12 16:18:20 -070074 Object* ref)
75 NO_THREAD_SAFETY_ANALYSIS
76 REQUIRES(!Roles::uninterruptible_) {
77 StackHandleScope<1> hs(self);
78 Handle<Object> h_ref(hs.NewHandle(ref));
79 h_ref->MonitorEnter(self);
Andreas Gampe56fdd0e2016-04-28 14:56:54 -070080 if (kMonitorCounting && frame->GetMethod()->MustCountLocks()) {
81 frame->GetLockCountData().AddMonitor(self, h_ref.Get());
82 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +020083}
84
Andreas Gampe03ec9302015-08-27 17:41:47 -070085template <bool kMonitorCounting>
86static inline void DoMonitorExit(Thread* self,
87 ShadowFrame* frame,
Mathieu Chartier2d096c92015-10-12 16:18:20 -070088 Object* ref)
89 NO_THREAD_SAFETY_ANALYSIS
90 REQUIRES(!Roles::uninterruptible_) {
91 StackHandleScope<1> hs(self);
92 Handle<Object> h_ref(hs.NewHandle(ref));
93 h_ref->MonitorExit(self);
Andreas Gampe56fdd0e2016-04-28 14:56:54 -070094 if (kMonitorCounting && frame->GetMethod()->MustCountLocks()) {
95 frame->GetLockCountData().RemoveMonitorOrThrow(self, h_ref.Get());
96 }
97}
98
99template <bool kMonitorCounting>
100static inline bool DoMonitorCheckOnExit(Thread* self, ShadowFrame* frame)
101 NO_THREAD_SAFETY_ANALYSIS
102 REQUIRES(!Roles::uninterruptible_) {
103 if (kMonitorCounting && frame->GetMethod()->MustCountLocks()) {
104 return frame->GetLockCountData().CheckAllMonitorsReleasedOrThrow(self);
105 }
106 return true;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200107}
108
Sebastien Hertz45b15972015-04-03 16:07:05 +0200109void AbortTransactionF(Thread* self, const char* fmt, ...)
110 __attribute__((__format__(__printf__, 2, 3)))
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700111 REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertz45b15972015-04-03 16:07:05 +0200112
113void AbortTransactionV(Thread* self, const char* fmt, va_list args)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700114 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700115
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100116void RecordArrayElementsInTransaction(mirror::Array* array, int32_t count)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700117 REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100118
Sebastien Hertzc6714852013-09-30 16:42:32 +0200119// Invokes the given method. This is part of the invocation support and is used by DoInvoke and
120// DoInvokeVirtualQuick functions.
121// Returns true on success, otherwise throws an exception and returns false.
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200122template<bool is_range, bool do_assignability_check>
Ian Rogerse94652f2014-12-02 11:13:19 -0800123bool DoCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame,
Sebastien Hertzc6714852013-09-30 16:42:32 +0200124 const Instruction* inst, uint16_t inst_data, JValue* result);
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200125
Narayan Kamath14832ef2016-08-05 11:44:32 +0100126// Handles invoke-XXX/range instructions.
Sebastien Hertzc6714852013-09-30 16:42:32 +0200127// Returns true on success, otherwise throws an exception and returns false.
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200128template<InvokeType type, bool is_range, bool do_access_check>
129static inline bool DoInvoke(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst,
130 uint16_t inst_data, JValue* result) {
131 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
132 const uint32_t vregC = (is_range) ? inst->VRegC_3rc() : inst->VRegC_35c();
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700133 Object* receiver = (type == kStatic) ? nullptr : shadow_frame.GetVRegReference(vregC);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700134 ArtMethod* sf_method = shadow_frame.GetMethod();
Ian Rogerse94652f2014-12-02 11:13:19 -0800135 ArtMethod* const called_method = FindMethodFromCode<type, do_access_check>(
Andreas Gampe3a357142015-08-07 17:20:11 -0700136 method_idx, &receiver, sf_method, self);
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700137 // The shadow frame should already be pushed, so we don't need to update it.
Ian Rogerse94652f2014-12-02 11:13:19 -0800138 if (UNLIKELY(called_method == nullptr)) {
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200139 CHECK(self->IsExceptionPending());
140 result->SetJ(0);
141 return false;
Alex Light9139e002015-10-09 15:59:48 -0700142 } else if (UNLIKELY(!called_method->IsInvokable())) {
143 called_method->ThrowInvocationTimeError();
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200144 result->SetJ(0);
145 return false;
146 } else {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100147 jit::Jit* jit = Runtime::Current()->GetJit();
148 if (jit != nullptr) {
149 if (type == kVirtual || type == kInterface) {
Mathieu Chartier268764d2016-09-13 12:09:38 -0700150 jit->InvokeVirtualOrInterface(receiver, sf_method, shadow_frame.GetDexPC(), called_method);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100151 }
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100152 jit->AddSamples(self, sf_method, 1, /*with_backedges*/false);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100153 }
154 // TODO: Remove the InvokeVirtualOrInterface instrumentation, as it was only used by the JIT.
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100155 if (type == kVirtual || type == kInterface) {
156 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
157 if (UNLIKELY(instrumentation->HasInvokeVirtualOrInterfaceListeners())) {
158 instrumentation->InvokeVirtualOrInterface(
159 self, receiver, sf_method, shadow_frame.GetDexPC(), called_method);
160 }
161 }
Ian Rogerse94652f2014-12-02 11:13:19 -0800162 return DoCall<is_range, do_access_check>(called_method, self, shadow_frame, inst, inst_data,
163 result);
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200164 }
165}
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200166
Sebastien Hertzc6714852013-09-30 16:42:32 +0200167// Handles invoke-virtual-quick and invoke-virtual-quick-range instructions.
168// Returns true on success, otherwise throws an exception and returns false.
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200169template<bool is_range>
170static inline bool DoInvokeVirtualQuick(Thread* self, ShadowFrame& shadow_frame,
171 const Instruction* inst, uint16_t inst_data,
172 JValue* result) {
173 const uint32_t vregC = (is_range) ? inst->VRegC_3rc() : inst->VRegC_35c();
174 Object* const receiver = shadow_frame.GetVRegReference(vregC);
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200175 if (UNLIKELY(receiver == nullptr)) {
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200176 // We lost the reference to the method index so we cannot get a more
177 // precised exception message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000178 ThrowNullPointerExceptionFromDexPC();
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200179 return false;
180 }
181 const uint32_t vtable_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000182 CHECK(receiver->GetClass()->ShouldHaveEmbeddedVTable());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700183 ArtMethod* const called_method = receiver->GetClass()->GetEmbeddedVTableEntry(
Andreas Gampe542451c2016-07-26 09:02:02 -0700184 vtable_idx, kRuntimePointerSize);
Ian Rogerse94652f2014-12-02 11:13:19 -0800185 if (UNLIKELY(called_method == nullptr)) {
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200186 CHECK(self->IsExceptionPending());
187 result->SetJ(0);
188 return false;
Alex Light9139e002015-10-09 15:59:48 -0700189 } else if (UNLIKELY(!called_method->IsInvokable())) {
190 called_method->ThrowInvocationTimeError();
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200191 result->SetJ(0);
192 return false;
193 } else {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100194 jit::Jit* jit = Runtime::Current()->GetJit();
195 if (jit != nullptr) {
196 jit->InvokeVirtualOrInterface(
Mathieu Chartier268764d2016-09-13 12:09:38 -0700197 receiver, shadow_frame.GetMethod(), shadow_frame.GetDexPC(), called_method);
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100198 jit->AddSamples(self, shadow_frame.GetMethod(), 1, /*with_backedges*/false);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100199 }
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100200 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100201 // TODO: Remove the InvokeVirtualOrInterface instrumentation, as it was only used by the JIT.
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100202 if (UNLIKELY(instrumentation->HasInvokeVirtualOrInterfaceListeners())) {
203 instrumentation->InvokeVirtualOrInterface(
204 self, receiver, shadow_frame.GetMethod(), shadow_frame.GetDexPC(), called_method);
205 }
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200206 // No need to check since we've been quickened.
Ian Rogerse94652f2014-12-02 11:13:19 -0800207 return DoCall<is_range, false>(called_method, self, shadow_frame, inst, inst_data, result);
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200208 }
209}
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200210
Sebastien Hertzc6714852013-09-30 16:42:32 +0200211// Handles iget-XXX and sget-XXX instructions.
212// Returns true on success, otherwise throws an exception and returns false.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200213template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check>
Ian Rogers54874942014-06-10 16:31:03 -0700214bool DoFieldGet(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700215 uint16_t inst_data) REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200216
Sebastien Hertzc6714852013-09-30 16:42:32 +0200217// Handles iget-quick, iget-wide-quick and iget-object-quick instructions.
218// Returns true on success, otherwise throws an exception and returns false.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200219template<Primitive::Type field_type>
Ian Rogers54874942014-06-10 16:31:03 -0700220bool DoIGetQuick(ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700221 REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertz479fc1e2014-04-04 17:51:34 +0200222
Sebastien Hertzc6714852013-09-30 16:42:32 +0200223// Handles iput-XXX and sput-XXX instructions.
224// Returns true on success, otherwise throws an exception and returns false.
Ian Rogers54874942014-06-10 16:31:03 -0700225template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check,
226 bool transaction_active>
227bool DoFieldPut(Thread* self, const ShadowFrame& shadow_frame, const Instruction* inst,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700228 uint16_t inst_data) REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200229
Sebastien Hertzc6714852013-09-30 16:42:32 +0200230// Handles iput-quick, iput-wide-quick and iput-object-quick instructions.
231// Returns true on success, otherwise throws an exception and returns false.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100232template<Primitive::Type field_type, bool transaction_active>
Ian Rogers54874942014-06-10 16:31:03 -0700233bool DoIPutQuick(const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700234 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers54874942014-06-10 16:31:03 -0700235
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200236
Sebastien Hertzc6714852013-09-30 16:42:32 +0200237// Handles string resolution for const-string and const-string-jumbo instructions. Also ensures the
238// java.lang.String class is initialized.
Ian Rogers6786a582014-10-28 12:49:06 -0700239static inline String* ResolveString(Thread* self, ShadowFrame& shadow_frame, uint32_t string_idx)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700240 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200241 Class* java_lang_string_class = String::GetJavaLangString();
242 if (UNLIKELY(!java_lang_string_class->IsInitialized())) {
243 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700244 StackHandleScope<1> hs(self);
245 Handle<mirror::Class> h_class(hs.NewHandle(java_lang_string_class));
Ian Rogers7b078e82014-09-10 14:44:24 -0700246 if (UNLIKELY(!class_linker->EnsureInitialized(self, h_class, true, true))) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200247 DCHECK(self->IsExceptionPending());
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800248 return nullptr;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200249 }
250 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700251 ArtMethod* method = shadow_frame.GetMethod();
Mathieu Chartiereace4582014-11-24 18:29:54 -0800252 mirror::Class* declaring_class = method->GetDeclaringClass();
Vladimir Marko05792b92015-08-03 11:56:49 +0100253 // MethodVerifier refuses methods with string_idx out of bounds.
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700254 DCHECK_LT(string_idx % mirror::DexCache::kDexCacheStringCacheSize,
255 declaring_class->GetDexFile().NumStringIds());
256 mirror::String* string_ptr =
Narayan Kamathc38a6f82016-09-29 17:07:20 +0100257 mirror::StringDexCachePair::Lookup(declaring_class->GetDexCacheStrings(),
258 string_idx,
259 mirror::DexCache::kDexCacheStringCacheSize).Read();
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700260 if (UNLIKELY(string_ptr == nullptr)) {
Ian Rogers6786a582014-10-28 12:49:06 -0700261 StackHandleScope<1> hs(self);
Mathieu Chartiereace4582014-11-24 18:29:54 -0800262 Handle<mirror::DexCache> dex_cache(hs.NewHandle(declaring_class->GetDexCache()));
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700263 string_ptr = Runtime::Current()->GetClassLinker()->ResolveString(*method->GetDexFile(),
264 string_idx,
265 dex_cache);
Ian Rogers6786a582014-10-28 12:49:06 -0700266 }
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700267 return string_ptr;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200268}
269
Sebastien Hertzc6714852013-09-30 16:42:32 +0200270// Handles div-int, div-int/2addr, div-int/li16 and div-int/lit8 instructions.
271// Returns true on success, otherwise throws a java.lang.ArithmeticException and return false.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200272static inline bool DoIntDivide(ShadowFrame& shadow_frame, size_t result_reg,
273 int32_t dividend, int32_t divisor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700274 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersf72a11d2014-10-30 15:41:08 -0700275 constexpr int32_t kMinInt = std::numeric_limits<int32_t>::min();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200276 if (UNLIKELY(divisor == 0)) {
277 ThrowArithmeticExceptionDivideByZero();
278 return false;
279 }
280 if (UNLIKELY(dividend == kMinInt && divisor == -1)) {
281 shadow_frame.SetVReg(result_reg, kMinInt);
282 } else {
283 shadow_frame.SetVReg(result_reg, dividend / divisor);
284 }
285 return true;
286}
287
Sebastien Hertzc6714852013-09-30 16:42:32 +0200288// Handles rem-int, rem-int/2addr, rem-int/li16 and rem-int/lit8 instructions.
289// Returns true on success, otherwise throws a java.lang.ArithmeticException and return false.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200290static inline bool DoIntRemainder(ShadowFrame& shadow_frame, size_t result_reg,
291 int32_t dividend, int32_t divisor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700292 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersf72a11d2014-10-30 15:41:08 -0700293 constexpr int32_t kMinInt = std::numeric_limits<int32_t>::min();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200294 if (UNLIKELY(divisor == 0)) {
295 ThrowArithmeticExceptionDivideByZero();
296 return false;
297 }
298 if (UNLIKELY(dividend == kMinInt && divisor == -1)) {
299 shadow_frame.SetVReg(result_reg, 0);
300 } else {
301 shadow_frame.SetVReg(result_reg, dividend % divisor);
302 }
303 return true;
304}
305
Sebastien Hertzc6714852013-09-30 16:42:32 +0200306// Handles div-long and div-long-2addr instructions.
307// Returns true on success, otherwise throws a java.lang.ArithmeticException and return false.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200308static inline bool DoLongDivide(ShadowFrame& shadow_frame, size_t result_reg,
309 int64_t dividend, int64_t divisor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700310 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2e2deeb2013-09-23 11:58:57 -0700311 const int64_t kMinLong = std::numeric_limits<int64_t>::min();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200312 if (UNLIKELY(divisor == 0)) {
313 ThrowArithmeticExceptionDivideByZero();
314 return false;
315 }
316 if (UNLIKELY(dividend == kMinLong && divisor == -1)) {
317 shadow_frame.SetVRegLong(result_reg, kMinLong);
318 } else {
319 shadow_frame.SetVRegLong(result_reg, dividend / divisor);
320 }
321 return true;
322}
323
Sebastien Hertzc6714852013-09-30 16:42:32 +0200324// Handles rem-long and rem-long-2addr instructions.
325// Returns true on success, otherwise throws a java.lang.ArithmeticException and return false.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200326static inline bool DoLongRemainder(ShadowFrame& shadow_frame, size_t result_reg,
327 int64_t dividend, int64_t divisor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700328 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2e2deeb2013-09-23 11:58:57 -0700329 const int64_t kMinLong = std::numeric_limits<int64_t>::min();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200330 if (UNLIKELY(divisor == 0)) {
331 ThrowArithmeticExceptionDivideByZero();
332 return false;
333 }
334 if (UNLIKELY(dividend == kMinLong && divisor == -1)) {
335 shadow_frame.SetVRegLong(result_reg, 0);
336 } else {
337 shadow_frame.SetVRegLong(result_reg, dividend % divisor);
338 }
339 return true;
340}
341
Sebastien Hertzc6714852013-09-30 16:42:32 +0200342// Handles filled-new-array and filled-new-array-range instructions.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200343// Returns true on success, otherwise throws an exception and returns false.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100344template <bool is_range, bool do_access_check, bool transaction_active>
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200345bool DoFilledNewArray(const Instruction* inst, const ShadowFrame& shadow_frame,
Sebastien Hertzc6714852013-09-30 16:42:32 +0200346 Thread* self, JValue* result);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200347
Sebastien Hertzc6714852013-09-30 16:42:32 +0200348// Handles packed-switch instruction.
349// Returns the branch offset to the next instruction to execute.
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200350static inline int32_t DoPackedSwitch(const Instruction* inst, const ShadowFrame& shadow_frame,
351 uint16_t inst_data)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700352 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200353 DCHECK(inst->Opcode() == Instruction::PACKED_SWITCH);
354 const uint16_t* switch_data = reinterpret_cast<const uint16_t*>(inst) + inst->VRegB_31t();
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200355 int32_t test_val = shadow_frame.GetVReg(inst->VRegA_31t(inst_data));
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200356 DCHECK_EQ(switch_data[0], static_cast<uint16_t>(Instruction::kPackedSwitchSignature));
357 uint16_t size = switch_data[1];
David Brazdil2ef645b2015-06-17 18:20:52 +0100358 if (size == 0) {
359 // Empty packed switch, move forward by 3 (size of PACKED_SWITCH).
360 return 3;
361 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200362 const int32_t* keys = reinterpret_cast<const int32_t*>(&switch_data[2]);
Roland Levillain14d90572015-07-16 10:52:26 +0100363 DCHECK_ALIGNED(keys, 4);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200364 int32_t first_key = keys[0];
365 const int32_t* targets = reinterpret_cast<const int32_t*>(&switch_data[4]);
Roland Levillain14d90572015-07-16 10:52:26 +0100366 DCHECK_ALIGNED(targets, 4);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200367 int32_t index = test_val - first_key;
368 if (index >= 0 && index < size) {
369 return targets[index];
370 } else {
371 // No corresponding value: move forward by 3 (size of PACKED_SWITCH).
372 return 3;
373 }
374}
375
Sebastien Hertzc6714852013-09-30 16:42:32 +0200376// Handles sparse-switch instruction.
377// Returns the branch offset to the next instruction to execute.
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200378static inline int32_t DoSparseSwitch(const Instruction* inst, const ShadowFrame& shadow_frame,
379 uint16_t inst_data)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700380 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200381 DCHECK(inst->Opcode() == Instruction::SPARSE_SWITCH);
382 const uint16_t* switch_data = reinterpret_cast<const uint16_t*>(inst) + inst->VRegB_31t();
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200383 int32_t test_val = shadow_frame.GetVReg(inst->VRegA_31t(inst_data));
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200384 DCHECK_EQ(switch_data[0], static_cast<uint16_t>(Instruction::kSparseSwitchSignature));
385 uint16_t size = switch_data[1];
Jeff Hao935e01a2015-03-20 19:44:35 -0700386 // Return length of SPARSE_SWITCH if size is 0.
387 if (size == 0) {
388 return 3;
389 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200390 const int32_t* keys = reinterpret_cast<const int32_t*>(&switch_data[2]);
Roland Levillain14d90572015-07-16 10:52:26 +0100391 DCHECK_ALIGNED(keys, 4);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200392 const int32_t* entries = keys + size;
Roland Levillain14d90572015-07-16 10:52:26 +0100393 DCHECK_ALIGNED(entries, 4);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200394 int lo = 0;
395 int hi = size - 1;
396 while (lo <= hi) {
397 int mid = (lo + hi) / 2;
398 int32_t foundVal = keys[mid];
399 if (test_val < foundVal) {
400 hi = mid - 1;
401 } else if (test_val > foundVal) {
402 lo = mid + 1;
403 } else {
404 return entries[mid];
405 }
406 }
407 // No corresponding value: move forward by 3 (size of SPARSE_SWITCH).
408 return 3;
409}
410
Ian Rogers54874942014-06-10 16:31:03 -0700411uint32_t FindNextInstructionFollowingException(Thread* self, ShadowFrame& shadow_frame,
Sebastien Hertz9f102032014-05-23 08:59:42 +0200412 uint32_t dex_pc, const instrumentation::Instrumentation* instrumentation)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700413 REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200414
Andreas Gampe794ad762015-02-23 08:12:24 -0800415NO_RETURN void UnexpectedOpcode(const Instruction* inst, const ShadowFrame& shadow_frame)
416 __attribute__((cold))
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700417 REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200418
Bill Buzbeed47fd902016-07-07 14:42:43 +0000419// Set true if you want TraceExecution invocation before each bytecode execution.
420constexpr bool kTraceExecutionEnabled = false;
Serguei Katkov9fb0ac72016-02-20 12:55:24 +0600421
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200422static inline void TraceExecution(const ShadowFrame& shadow_frame, const Instruction* inst,
Ian Rogerse94652f2014-12-02 11:13:19 -0800423 const uint32_t dex_pc)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700424 REQUIRES_SHARED(Locks::mutator_lock_) {
Bill Buzbeed47fd902016-07-07 14:42:43 +0000425 if (kTraceExecutionEnabled) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200426#define TRACE_LOG std::cerr
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700427 std::ostringstream oss;
428 oss << PrettyMethod(shadow_frame.GetMethod())
429 << StringPrintf("\n0x%x: ", dex_pc)
Ian Rogerse94652f2014-12-02 11:13:19 -0800430 << inst->DumpString(shadow_frame.GetMethod()->GetDexFile()) << "\n";
Ian Rogersef7d42f2014-01-06 12:55:46 -0800431 for (uint32_t i = 0; i < shadow_frame.NumberOfVRegs(); ++i) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200432 uint32_t raw_value = shadow_frame.GetVReg(i);
433 Object* ref_value = shadow_frame.GetVRegReference(i);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800434 oss << StringPrintf(" vreg%u=0x%08X", i, raw_value);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700435 if (ref_value != nullptr) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200436 if (ref_value->GetClass()->IsStringClass() &&
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700437 !ref_value->AsString()->IsValueNull()) {
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700438 oss << "/java.lang.String \"" << ref_value->AsString()->ToModifiedUtf8() << "\"";
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200439 } else {
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700440 oss << "/" << PrettyTypeOf(ref_value);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200441 }
442 }
443 }
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700444 TRACE_LOG << oss.str() << "\n";
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200445#undef TRACE_LOG
446 }
447}
448
Sebastien Hertz1eda2262013-09-09 16:53:14 +0200449static inline bool IsBackwardBranch(int32_t branch_offset) {
450 return branch_offset <= 0;
451}
452
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100453void ArtInterpreterToCompiledCodeBridge(Thread* self,
454 ArtMethod* caller,
455 const DexFile::CodeItem* code_item,
456 ShadowFrame* shadow_frame,
457 JValue* result);
Siva Chandra05d24152016-01-05 17:43:17 -0800458
Mingyao Yangffedec52016-05-19 10:48:40 -0700459// Set string value created from StringFactory.newStringFromXXX() into all aliases of
460// StringFactory.newEmptyString().
461void SetStringInitValueToAllAliases(ShadowFrame* shadow_frame,
462 uint16_t this_obj_vreg,
463 JValue result);
464
Sebastien Hertzc6714852013-09-30 16:42:32 +0200465// Explicitly instantiate all DoInvoke functions.
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100466#define EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, _is_range, _do_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700467 template REQUIRES_SHARED(Locks::mutator_lock_) \
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100468 bool DoInvoke<_type, _is_range, _do_check>(Thread* self, ShadowFrame& shadow_frame, \
469 const Instruction* inst, uint16_t inst_data, \
470 JValue* result)
Sebastien Hertzc6714852013-09-30 16:42:32 +0200471
472#define EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(_type) \
473 EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, false, false); \
474 EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, false, true); \
475 EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, true, false); \
476 EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, true, true);
477
Andreas Gampec8ccf682014-09-29 20:07:43 -0700478EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(kStatic) // invoke-static/range.
479EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(kDirect) // invoke-direct/range.
480EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(kVirtual) // invoke-virtual/range.
481EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(kSuper) // invoke-super/range.
482EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(kInterface) // invoke-interface/range.
Sebastien Hertzc6714852013-09-30 16:42:32 +0200483#undef EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL
484#undef EXPLICIT_DO_INVOKE_TEMPLATE_DECL
485
Sebastien Hertzc6714852013-09-30 16:42:32 +0200486// Explicitly instantiate all DoInvokeVirtualQuick functions.
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100487#define EXPLICIT_DO_INVOKE_VIRTUAL_QUICK_TEMPLATE_DECL(_is_range) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700488 template REQUIRES_SHARED(Locks::mutator_lock_) \
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100489 bool DoInvokeVirtualQuick<_is_range>(Thread* self, ShadowFrame& shadow_frame, \
490 const Instruction* inst, uint16_t inst_data, \
491 JValue* result)
Sebastien Hertzc6714852013-09-30 16:42:32 +0200492
493EXPLICIT_DO_INVOKE_VIRTUAL_QUICK_TEMPLATE_DECL(false); // invoke-virtual-quick.
494EXPLICIT_DO_INVOKE_VIRTUAL_QUICK_TEMPLATE_DECL(true); // invoke-virtual-quick-range.
495#undef EXPLICIT_INSTANTIATION_DO_INVOKE_VIRTUAL_QUICK
496
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200497} // namespace interpreter
498} // namespace art
499
500#endif // ART_RUNTIME_INTERPRETER_INTERPRETER_COMMON_H_