blob: 2a15087f7e897bd98f16b0c9c9d055e393011c2f [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"
Sebastien Hertz8ece0502013-08-07 11:26:41 +020029#include "base/logging.h"
Andreas Gampe794ad762015-02-23 08:12:24 -080030#include "base/macros.h"
Sebastien Hertz8ece0502013-08-07 11:26:41 +020031#include "class_linker-inl.h"
32#include "common_throws.h"
33#include "dex_file-inl.h"
34#include "dex_instruction-inl.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070035#include "entrypoints/entrypoint_utils-inl.h"
Mathieu Chartier0cd81352014-05-22 16:48:55 -070036#include "handle_scope-inl.h"
Sebastien Hertz8ece0502013-08-07 11:26:41 +020037#include "mirror/class-inl.h"
38#include "mirror/object-inl.h"
39#include "mirror/object_array-inl.h"
Douglas Leung4965c022014-06-11 11:41:11 -070040#include "mirror/string-inl.h"
Sebastien Hertz8ece0502013-08-07 11:26:41 +020041#include "thread.h"
42#include "well_known_classes.h"
43
Mathieu Chartiere401d142015-04-22 13:56:20 -070044using ::art::ArtMethod;
Sebastien Hertz8ece0502013-08-07 11:26:41 +020045using ::art::mirror::Array;
46using ::art::mirror::BooleanArray;
47using ::art::mirror::ByteArray;
48using ::art::mirror::CharArray;
49using ::art::mirror::Class;
50using ::art::mirror::ClassLoader;
51using ::art::mirror::IntArray;
52using ::art::mirror::LongArray;
53using ::art::mirror::Object;
54using ::art::mirror::ObjectArray;
55using ::art::mirror::ShortArray;
56using ::art::mirror::String;
57using ::art::mirror::Throwable;
58
59namespace art {
60namespace interpreter {
61
62// External references to both interpreter implementations.
63
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010064template<bool do_access_check, bool transaction_active>
Ian Rogerse94652f2014-12-02 11:13:19 -080065extern JValue ExecuteSwitchImpl(Thread* self, const DexFile::CodeItem* code_item,
Sebastien Hertzc6714852013-09-30 16:42:32 +020066 ShadowFrame& shadow_frame, JValue result_register);
Sebastien Hertz8ece0502013-08-07 11:26:41 +020067
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010068template<bool do_access_check, bool transaction_active>
Ian Rogerse94652f2014-12-02 11:13:19 -080069extern JValue ExecuteGotoImpl(Thread* self, const DexFile::CodeItem* code_item,
Sebastien Hertzc6714852013-09-30 16:42:32 +020070 ShadowFrame& shadow_frame, JValue result_register);
Sebastien Hertz8ece0502013-08-07 11:26:41 +020071
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000072void ThrowNullPointerExceptionFromInterpreter()
Ian Rogers54874942014-06-10 16:31:03 -070073 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Sebastien Hertzda843e12014-05-28 19:28:31 +020074
Sebastien Hertz8ece0502013-08-07 11:26:41 +020075static inline void DoMonitorEnter(Thread* self, Object* ref) NO_THREAD_SAFETY_ANALYSIS {
76 ref->MonitorEnter(self);
77}
78
79static inline void DoMonitorExit(Thread* self, Object* ref) NO_THREAD_SAFETY_ANALYSIS {
80 ref->MonitorExit(self);
81}
82
Sebastien Hertz45b15972015-04-03 16:07:05 +020083void AbortTransactionF(Thread* self, const char* fmt, ...)
84 __attribute__((__format__(__printf__, 2, 3)))
85 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
86
87void AbortTransactionV(Thread* self, const char* fmt, va_list args)
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -070088 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
89
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010090void RecordArrayElementsInTransaction(mirror::Array* array, int32_t count)
91 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
92
Sebastien Hertzc6714852013-09-30 16:42:32 +020093// Invokes the given method. This is part of the invocation support and is used by DoInvoke and
94// DoInvokeVirtualQuick functions.
95// Returns true on success, otherwise throws an exception and returns false.
Sebastien Hertzc61124b2013-09-10 11:44:19 +020096template<bool is_range, bool do_assignability_check>
Ian Rogerse94652f2014-12-02 11:13:19 -080097bool DoCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame,
Sebastien Hertzc6714852013-09-30 16:42:32 +020098 const Instruction* inst, uint16_t inst_data, JValue* result);
Sebastien Hertzc61124b2013-09-10 11:44:19 +020099
Igor Murashkin158f35c2015-06-10 15:55:30 -0700100// Invokes the given lambda closure. This is part of the invocation support and is used by
101// DoLambdaInvoke functions.
102// Returns true on success, otherwise throws an exception and returns false.
103template<bool is_range, bool do_assignability_check>
104bool DoLambdaCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame,
105 const Instruction* inst, uint16_t inst_data, JValue* result);
106
107// Validates that the art method corresponding to a lambda method target
108// is semantically valid:
109//
110// Must be ACC_STATIC and ACC_LAMBDA. Must be a concrete managed implementation
111// (i.e. not native, not proxy, not abstract, ...).
112//
113// If the validation fails, return false and raise an exception.
114static inline bool IsValidLambdaTargetOrThrow(ArtMethod* called_method)
115 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
116 bool success = false;
117
118 if (UNLIKELY(called_method == nullptr)) {
119 // The shadow frame should already be pushed, so we don't need to update it.
120 } else if (UNLIKELY(called_method->IsAbstract())) {
121 ThrowAbstractMethodError(called_method);
122 // TODO(iam): Also handle the case when the method is non-static, what error do we throw?
123 // TODO(iam): Also make sure that ACC_LAMBDA is set.
124 } else if (UNLIKELY(called_method->GetCodeItem() == nullptr)) {
125 // Method could be native, proxy method, etc. Lambda targets have to be concrete impls,
126 // so don't allow this.
127 } else {
128 success = true;
129 }
130
131 return success;
132}
133
134// Handles create-lambda instructions.
135// Returns true on success, otherwise throws an exception and returns false.
136// (Exceptions are thrown by creating a new exception and then being put in the thread TLS)
137//
138// As a work-in-progress implementation, this shoves the ArtMethod object corresponding
139// to the target dex method index into the target register vA and vA + 1.
140template<bool do_access_check>
141static inline bool DoCreateLambda(Thread* self, ShadowFrame& shadow_frame,
142 const Instruction* inst) {
143 /*
144 * create-lambda is opcode 0x21c
145 * - vA is the target register where the closure will be stored into
146 * (also stores into vA + 1)
147 * - vB is the method index which will be the target for a later invoke-lambda
148 */
149 const uint32_t method_idx = inst->VRegB_21c();
150 mirror::Object* receiver = nullptr; // Always static. (see 'kStatic')
151 ArtMethod* sf_method = shadow_frame.GetMethod();
152 ArtMethod* const called_method = FindMethodFromCode<kStatic, do_access_check>(
153 method_idx, &receiver, &sf_method, self);
154
155 uint32_t vregA = inst->VRegA_21c();
156
157 if (UNLIKELY(!IsValidLambdaTargetOrThrow(called_method))) {
158 CHECK(self->IsExceptionPending());
159 shadow_frame.SetVReg(vregA, 0u);
160 shadow_frame.SetVReg(vregA + 1, 0u);
161 return false;
162 }
163
164 // Split the method into a lo and hi 32 bits so we can encode them into 2 virtual registers.
165 uint32_t called_method_lo = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(called_method));
166 uint32_t called_method_hi = static_cast<uint32_t>(reinterpret_cast<uint64_t>(called_method)
167 >> BitSizeOf<uint32_t>());
168 // Use uint64_t instead of uintptr_t to allow shifting past the max on 32-bit.
169 static_assert(sizeof(uint64_t) >= sizeof(uintptr_t), "Impossible");
170
171 DCHECK_NE(called_method_lo | called_method_hi, 0u);
172
173 shadow_frame.SetVReg(vregA, called_method_lo);
174 shadow_frame.SetVReg(vregA + 1, called_method_hi);
175 return true;
176}
177
178template<bool do_access_check>
179static inline bool DoInvokeLambda(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst,
180 uint16_t inst_data, JValue* result) {
181 /*
182 * invoke-lambda is opcode 0x25
183 *
184 * - vC is the closure register (both vC and vC + 1 will be used to store the closure).
185 * - vB is the number of additional registers up to |{vD,vE,vF,vG}| (4)
186 * - the rest of the registers are always var-args
187 *
188 * - reading var-args for 0x25 gets us vD,vE,vF,vG (but not vB)
189 */
190 uint32_t vC = inst->VRegC_25x();
191
192 // TODO(iam): Introduce a closure abstraction that will contain the captured variables
193 // instead of just an ArtMethod. We also should only need to use 1 register instead of 2.
194 uint32_t vc_value_lo = shadow_frame.GetVReg(vC);
195 uint32_t vc_value_hi = shadow_frame.GetVReg(vC + 1);
196
197 uint64_t vc_value_ptr = (static_cast<uint64_t>(vc_value_hi) << BitSizeOf<uint32_t>())
198 | vc_value_lo;
199
200 // Use uint64_t instead of uintptr_t to allow left-shifting past the max on 32-bit.
201 static_assert(sizeof(uint64_t) >= sizeof(uintptr_t), "Impossible");
202 ArtMethod* const called_method = reinterpret_cast<ArtMethod* const>(vc_value_ptr);
203
204 // Guard against the user passing a null closure, which is odd but (sadly) semantically valid.
205 if (UNLIKELY(called_method == nullptr)) {
206 ThrowNullPointerExceptionFromInterpreter();
207 result->SetJ(0);
208 return false;
209 }
210
211 if (UNLIKELY(!IsValidLambdaTargetOrThrow(called_method))) {
212 CHECK(self->IsExceptionPending());
213 result->SetJ(0);
214 return false;
215 } else {
216 return DoLambdaCall<false, do_access_check>(called_method, self, shadow_frame, inst, inst_data,
217 result);
218 }
219}
220
Sebastien Hertzc6714852013-09-30 16:42:32 +0200221// Handles invoke-XXX/range instructions.
222// Returns true on success, otherwise throws an exception and returns false.
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200223template<InvokeType type, bool is_range, bool do_access_check>
224static inline bool DoInvoke(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst,
225 uint16_t inst_data, JValue* result) {
226 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
227 const uint32_t vregC = (is_range) ? inst->VRegC_3rc() : inst->VRegC_35c();
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700228 Object* receiver = (type == kStatic) ? nullptr : shadow_frame.GetVRegReference(vregC);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700229 ArtMethod* sf_method = shadow_frame.GetMethod();
Ian Rogerse94652f2014-12-02 11:13:19 -0800230 ArtMethod* const called_method = FindMethodFromCode<type, do_access_check>(
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700231 method_idx, &receiver, &sf_method, self);
232 // The shadow frame should already be pushed, so we don't need to update it.
Ian Rogerse94652f2014-12-02 11:13:19 -0800233 if (UNLIKELY(called_method == nullptr)) {
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200234 CHECK(self->IsExceptionPending());
235 result->SetJ(0);
236 return false;
Ian Rogerse94652f2014-12-02 11:13:19 -0800237 } else if (UNLIKELY(called_method->IsAbstract())) {
238 ThrowAbstractMethodError(called_method);
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200239 result->SetJ(0);
240 return false;
241 } else {
Ian Rogerse94652f2014-12-02 11:13:19 -0800242 return DoCall<is_range, do_access_check>(called_method, self, shadow_frame, inst, inst_data,
243 result);
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200244 }
245}
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200246
Sebastien Hertzc6714852013-09-30 16:42:32 +0200247// Handles invoke-virtual-quick and invoke-virtual-quick-range instructions.
248// Returns true on success, otherwise throws an exception and returns false.
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200249template<bool is_range>
250static inline bool DoInvokeVirtualQuick(Thread* self, ShadowFrame& shadow_frame,
251 const Instruction* inst, uint16_t inst_data,
252 JValue* result) {
253 const uint32_t vregC = (is_range) ? inst->VRegC_3rc() : inst->VRegC_35c();
254 Object* const receiver = shadow_frame.GetVRegReference(vregC);
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200255 if (UNLIKELY(receiver == nullptr)) {
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200256 // We lost the reference to the method index so we cannot get a more
257 // precised exception message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000258 ThrowNullPointerExceptionFromDexPC();
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200259 return false;
260 }
261 const uint32_t vtable_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700262 CHECK(receiver->GetClass()->ShouldHaveEmbeddedImtAndVTable());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700263 ArtMethod* const called_method = receiver->GetClass()->GetEmbeddedVTableEntry(
264 vtable_idx, sizeof(void*));
Ian Rogerse94652f2014-12-02 11:13:19 -0800265 if (UNLIKELY(called_method == nullptr)) {
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200266 CHECK(self->IsExceptionPending());
267 result->SetJ(0);
268 return false;
Ian Rogerse94652f2014-12-02 11:13:19 -0800269 } else if (UNLIKELY(called_method->IsAbstract())) {
270 ThrowAbstractMethodError(called_method);
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200271 result->SetJ(0);
272 return false;
273 } else {
274 // No need to check since we've been quickened.
Ian Rogerse94652f2014-12-02 11:13:19 -0800275 return DoCall<is_range, false>(called_method, self, shadow_frame, inst, inst_data, result);
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200276 }
277}
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200278
Sebastien Hertzc6714852013-09-30 16:42:32 +0200279// Handles iget-XXX and sget-XXX instructions.
280// Returns true on success, otherwise throws an exception and returns false.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200281template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check>
Ian Rogers54874942014-06-10 16:31:03 -0700282bool DoFieldGet(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst,
283 uint16_t inst_data) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200284
Sebastien Hertzc6714852013-09-30 16:42:32 +0200285// Handles iget-quick, iget-wide-quick and iget-object-quick instructions.
286// Returns true on success, otherwise throws an exception and returns false.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200287template<Primitive::Type field_type>
Ian Rogers54874942014-06-10 16:31:03 -0700288bool DoIGetQuick(ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data)
289 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Sebastien Hertz479fc1e2014-04-04 17:51:34 +0200290
Sebastien Hertzc6714852013-09-30 16:42:32 +0200291// Handles iput-XXX and sput-XXX instructions.
292// Returns true on success, otherwise throws an exception and returns false.
Ian Rogers54874942014-06-10 16:31:03 -0700293template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check,
294 bool transaction_active>
295bool DoFieldPut(Thread* self, const ShadowFrame& shadow_frame, const Instruction* inst,
296 uint16_t inst_data) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200297
Sebastien Hertzc6714852013-09-30 16:42:32 +0200298// Handles iput-quick, iput-wide-quick and iput-object-quick instructions.
299// Returns true on success, otherwise throws an exception and returns false.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100300template<Primitive::Type field_type, bool transaction_active>
Ian Rogers54874942014-06-10 16:31:03 -0700301bool DoIPutQuick(const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data)
302 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
303
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200304
Sebastien Hertzc6714852013-09-30 16:42:32 +0200305// Handles string resolution for const-string and const-string-jumbo instructions. Also ensures the
306// java.lang.String class is initialized.
Ian Rogers6786a582014-10-28 12:49:06 -0700307static inline String* ResolveString(Thread* self, ShadowFrame& shadow_frame, uint32_t string_idx)
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200308 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
309 Class* java_lang_string_class = String::GetJavaLangString();
310 if (UNLIKELY(!java_lang_string_class->IsInitialized())) {
311 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700312 StackHandleScope<1> hs(self);
313 Handle<mirror::Class> h_class(hs.NewHandle(java_lang_string_class));
Ian Rogers7b078e82014-09-10 14:44:24 -0700314 if (UNLIKELY(!class_linker->EnsureInitialized(self, h_class, true, true))) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200315 DCHECK(self->IsExceptionPending());
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800316 return nullptr;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200317 }
318 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700319 ArtMethod* method = shadow_frame.GetMethod();
Mathieu Chartiereace4582014-11-24 18:29:54 -0800320 mirror::Class* declaring_class = method->GetDeclaringClass();
321 mirror::String* s = declaring_class->GetDexCacheStrings()->Get(string_idx);
Ian Rogers6786a582014-10-28 12:49:06 -0700322 if (UNLIKELY(s == nullptr)) {
323 StackHandleScope<1> hs(self);
Mathieu Chartiereace4582014-11-24 18:29:54 -0800324 Handle<mirror::DexCache> dex_cache(hs.NewHandle(declaring_class->GetDexCache()));
Ian Rogers6786a582014-10-28 12:49:06 -0700325 s = Runtime::Current()->GetClassLinker()->ResolveString(*method->GetDexFile(), string_idx,
326 dex_cache);
327 }
328 return s;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200329}
330
Sebastien Hertzc6714852013-09-30 16:42:32 +0200331// Handles div-int, div-int/2addr, div-int/li16 and div-int/lit8 instructions.
332// Returns true on success, otherwise throws a java.lang.ArithmeticException and return false.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200333static inline bool DoIntDivide(ShadowFrame& shadow_frame, size_t result_reg,
334 int32_t dividend, int32_t divisor)
335 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersf72a11d2014-10-30 15:41:08 -0700336 constexpr int32_t kMinInt = std::numeric_limits<int32_t>::min();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200337 if (UNLIKELY(divisor == 0)) {
338 ThrowArithmeticExceptionDivideByZero();
339 return false;
340 }
341 if (UNLIKELY(dividend == kMinInt && divisor == -1)) {
342 shadow_frame.SetVReg(result_reg, kMinInt);
343 } else {
344 shadow_frame.SetVReg(result_reg, dividend / divisor);
345 }
346 return true;
347}
348
Sebastien Hertzc6714852013-09-30 16:42:32 +0200349// Handles rem-int, rem-int/2addr, rem-int/li16 and rem-int/lit8 instructions.
350// Returns true on success, otherwise throws a java.lang.ArithmeticException and return false.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200351static inline bool DoIntRemainder(ShadowFrame& shadow_frame, size_t result_reg,
352 int32_t dividend, int32_t divisor)
353 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersf72a11d2014-10-30 15:41:08 -0700354 constexpr int32_t kMinInt = std::numeric_limits<int32_t>::min();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200355 if (UNLIKELY(divisor == 0)) {
356 ThrowArithmeticExceptionDivideByZero();
357 return false;
358 }
359 if (UNLIKELY(dividend == kMinInt && divisor == -1)) {
360 shadow_frame.SetVReg(result_reg, 0);
361 } else {
362 shadow_frame.SetVReg(result_reg, dividend % divisor);
363 }
364 return true;
365}
366
Sebastien Hertzc6714852013-09-30 16:42:32 +0200367// Handles div-long and div-long-2addr instructions.
368// Returns true on success, otherwise throws a java.lang.ArithmeticException and return false.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200369static inline bool DoLongDivide(ShadowFrame& shadow_frame, size_t result_reg,
370 int64_t dividend, int64_t divisor)
371 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2e2deeb2013-09-23 11:58:57 -0700372 const int64_t kMinLong = std::numeric_limits<int64_t>::min();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200373 if (UNLIKELY(divisor == 0)) {
374 ThrowArithmeticExceptionDivideByZero();
375 return false;
376 }
377 if (UNLIKELY(dividend == kMinLong && divisor == -1)) {
378 shadow_frame.SetVRegLong(result_reg, kMinLong);
379 } else {
380 shadow_frame.SetVRegLong(result_reg, dividend / divisor);
381 }
382 return true;
383}
384
Sebastien Hertzc6714852013-09-30 16:42:32 +0200385// Handles rem-long and rem-long-2addr instructions.
386// Returns true on success, otherwise throws a java.lang.ArithmeticException and return false.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200387static inline bool DoLongRemainder(ShadowFrame& shadow_frame, size_t result_reg,
388 int64_t dividend, int64_t divisor)
389 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2e2deeb2013-09-23 11:58:57 -0700390 const int64_t kMinLong = std::numeric_limits<int64_t>::min();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200391 if (UNLIKELY(divisor == 0)) {
392 ThrowArithmeticExceptionDivideByZero();
393 return false;
394 }
395 if (UNLIKELY(dividend == kMinLong && divisor == -1)) {
396 shadow_frame.SetVRegLong(result_reg, 0);
397 } else {
398 shadow_frame.SetVRegLong(result_reg, dividend % divisor);
399 }
400 return true;
401}
402
Sebastien Hertzc6714852013-09-30 16:42:32 +0200403// Handles filled-new-array and filled-new-array-range instructions.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200404// Returns true on success, otherwise throws an exception and returns false.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100405template <bool is_range, bool do_access_check, bool transaction_active>
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200406bool DoFilledNewArray(const Instruction* inst, const ShadowFrame& shadow_frame,
Sebastien Hertzc6714852013-09-30 16:42:32 +0200407 Thread* self, JValue* result);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200408
Sebastien Hertzc6714852013-09-30 16:42:32 +0200409// Handles packed-switch instruction.
410// Returns the branch offset to the next instruction to execute.
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200411static inline int32_t DoPackedSwitch(const Instruction* inst, const ShadowFrame& shadow_frame,
412 uint16_t inst_data)
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200413 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
414 DCHECK(inst->Opcode() == Instruction::PACKED_SWITCH);
415 const uint16_t* switch_data = reinterpret_cast<const uint16_t*>(inst) + inst->VRegB_31t();
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200416 int32_t test_val = shadow_frame.GetVReg(inst->VRegA_31t(inst_data));
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200417 DCHECK_EQ(switch_data[0], static_cast<uint16_t>(Instruction::kPackedSwitchSignature));
418 uint16_t size = switch_data[1];
419 DCHECK_GT(size, 0);
420 const int32_t* keys = reinterpret_cast<const int32_t*>(&switch_data[2]);
421 DCHECK(IsAligned<4>(keys));
422 int32_t first_key = keys[0];
423 const int32_t* targets = reinterpret_cast<const int32_t*>(&switch_data[4]);
424 DCHECK(IsAligned<4>(targets));
425 int32_t index = test_val - first_key;
426 if (index >= 0 && index < size) {
427 return targets[index];
428 } else {
429 // No corresponding value: move forward by 3 (size of PACKED_SWITCH).
430 return 3;
431 }
432}
433
Sebastien Hertzc6714852013-09-30 16:42:32 +0200434// Handles sparse-switch instruction.
435// Returns the branch offset to the next instruction to execute.
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200436static inline int32_t DoSparseSwitch(const Instruction* inst, const ShadowFrame& shadow_frame,
437 uint16_t inst_data)
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200438 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
439 DCHECK(inst->Opcode() == Instruction::SPARSE_SWITCH);
440 const uint16_t* switch_data = reinterpret_cast<const uint16_t*>(inst) + inst->VRegB_31t();
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200441 int32_t test_val = shadow_frame.GetVReg(inst->VRegA_31t(inst_data));
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200442 DCHECK_EQ(switch_data[0], static_cast<uint16_t>(Instruction::kSparseSwitchSignature));
443 uint16_t size = switch_data[1];
Jeff Hao935e01a2015-03-20 19:44:35 -0700444 // Return length of SPARSE_SWITCH if size is 0.
445 if (size == 0) {
446 return 3;
447 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200448 const int32_t* keys = reinterpret_cast<const int32_t*>(&switch_data[2]);
449 DCHECK(IsAligned<4>(keys));
450 const int32_t* entries = keys + size;
451 DCHECK(IsAligned<4>(entries));
452 int lo = 0;
453 int hi = size - 1;
454 while (lo <= hi) {
455 int mid = (lo + hi) / 2;
456 int32_t foundVal = keys[mid];
457 if (test_val < foundVal) {
458 hi = mid - 1;
459 } else if (test_val > foundVal) {
460 lo = mid + 1;
461 } else {
462 return entries[mid];
463 }
464 }
465 // No corresponding value: move forward by 3 (size of SPARSE_SWITCH).
466 return 3;
467}
468
Ian Rogers54874942014-06-10 16:31:03 -0700469uint32_t FindNextInstructionFollowingException(Thread* self, ShadowFrame& shadow_frame,
Sebastien Hertz9f102032014-05-23 08:59:42 +0200470 uint32_t dex_pc, const instrumentation::Instrumentation* instrumentation)
Ian Rogers54874942014-06-10 16:31:03 -0700471 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200472
Andreas Gampe794ad762015-02-23 08:12:24 -0800473NO_RETURN void UnexpectedOpcode(const Instruction* inst, const ShadowFrame& shadow_frame)
474 __attribute__((cold))
Ian Rogersb48b9eb2014-02-28 16:20:21 -0800475 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200476
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200477static inline void TraceExecution(const ShadowFrame& shadow_frame, const Instruction* inst,
Ian Rogerse94652f2014-12-02 11:13:19 -0800478 const uint32_t dex_pc)
Jeff Haoa3faaf42013-09-03 19:07:00 -0700479 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700480 constexpr bool kTracing = false;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200481 if (kTracing) {
482#define TRACE_LOG std::cerr
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700483 std::ostringstream oss;
484 oss << PrettyMethod(shadow_frame.GetMethod())
485 << StringPrintf("\n0x%x: ", dex_pc)
Ian Rogerse94652f2014-12-02 11:13:19 -0800486 << inst->DumpString(shadow_frame.GetMethod()->GetDexFile()) << "\n";
Ian Rogersef7d42f2014-01-06 12:55:46 -0800487 for (uint32_t i = 0; i < shadow_frame.NumberOfVRegs(); ++i) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200488 uint32_t raw_value = shadow_frame.GetVReg(i);
489 Object* ref_value = shadow_frame.GetVRegReference(i);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800490 oss << StringPrintf(" vreg%u=0x%08X", i, raw_value);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700491 if (ref_value != nullptr) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200492 if (ref_value->GetClass()->IsStringClass() &&
Jeff Hao848f70a2014-01-15 13:49:50 -0800493 ref_value->AsString()->GetValue() != nullptr) {
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700494 oss << "/java.lang.String \"" << ref_value->AsString()->ToModifiedUtf8() << "\"";
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200495 } else {
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700496 oss << "/" << PrettyTypeOf(ref_value);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200497 }
498 }
499 }
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700500 TRACE_LOG << oss.str() << "\n";
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200501#undef TRACE_LOG
502 }
503}
504
Sebastien Hertz1eda2262013-09-09 16:53:14 +0200505static inline bool IsBackwardBranch(int32_t branch_offset) {
506 return branch_offset <= 0;
507}
508
Sebastien Hertzc6714852013-09-30 16:42:32 +0200509// Explicitly instantiate all DoInvoke functions.
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100510#define EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, _is_range, _do_check) \
Ian Rogers54874942014-06-10 16:31:03 -0700511 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) \
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100512 bool DoInvoke<_type, _is_range, _do_check>(Thread* self, ShadowFrame& shadow_frame, \
513 const Instruction* inst, uint16_t inst_data, \
514 JValue* result)
Sebastien Hertzc6714852013-09-30 16:42:32 +0200515
516#define EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(_type) \
517 EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, false, false); \
518 EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, false, true); \
519 EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, true, false); \
520 EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, true, true);
521
Andreas Gampec8ccf682014-09-29 20:07:43 -0700522EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(kStatic) // invoke-static/range.
523EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(kDirect) // invoke-direct/range.
524EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(kVirtual) // invoke-virtual/range.
525EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(kSuper) // invoke-super/range.
526EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(kInterface) // invoke-interface/range.
Sebastien Hertzc6714852013-09-30 16:42:32 +0200527#undef EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL
528#undef EXPLICIT_DO_INVOKE_TEMPLATE_DECL
529
Sebastien Hertzc6714852013-09-30 16:42:32 +0200530// Explicitly instantiate all DoInvokeVirtualQuick functions.
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100531#define EXPLICIT_DO_INVOKE_VIRTUAL_QUICK_TEMPLATE_DECL(_is_range) \
Ian Rogers54874942014-06-10 16:31:03 -0700532 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) \
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100533 bool DoInvokeVirtualQuick<_is_range>(Thread* self, ShadowFrame& shadow_frame, \
534 const Instruction* inst, uint16_t inst_data, \
535 JValue* result)
Sebastien Hertzc6714852013-09-30 16:42:32 +0200536
537EXPLICIT_DO_INVOKE_VIRTUAL_QUICK_TEMPLATE_DECL(false); // invoke-virtual-quick.
538EXPLICIT_DO_INVOKE_VIRTUAL_QUICK_TEMPLATE_DECL(true); // invoke-virtual-quick-range.
539#undef EXPLICIT_INSTANTIATION_DO_INVOKE_VIRTUAL_QUICK
540
Igor Murashkin158f35c2015-06-10 15:55:30 -0700541// Explicitly instantiate all DoCreateLambda functions.
542#define EXPLICIT_DO_CREATE_LAMBDA_DECL(_do_check) \
543template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) \
544bool DoCreateLambda<_do_check>(Thread* self, ShadowFrame& shadow_frame, \
545 const Instruction* inst)
546
547EXPLICIT_DO_CREATE_LAMBDA_DECL(false); // create-lambda
548EXPLICIT_DO_CREATE_LAMBDA_DECL(true); // create-lambda
549#undef EXPLICIT_DO_CREATE_LAMBDA_DECL
550
551// Explicitly instantiate all DoInvokeLambda functions.
552#define EXPLICIT_DO_INVOKE_LAMBDA_DECL(_do_check) \
553template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) \
554bool DoInvokeLambda<_do_check>(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst, \
555 uint16_t inst_data, JValue* result);
556
557EXPLICIT_DO_INVOKE_LAMBDA_DECL(false); // invoke-lambda
558EXPLICIT_DO_INVOKE_LAMBDA_DECL(true); // invoke-lambda
559#undef EXPLICIT_DO_INVOKE_LAMBDA_DECL
560
Sebastien Hertzc6714852013-09-30 16:42:32 +0200561
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200562} // namespace interpreter
563} // namespace art
564
565#endif // ART_RUNTIME_INTERPRETER_INTERPRETER_COMMON_H_