blob: 06b809f0281d9ed2e5708329d7829b71f2b96150 [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
Sebastien Hertz8ece0502013-08-07 11:26:41 +020027#include "base/logging.h"
Andreas Gampe794ad762015-02-23 08:12:24 -080028#include "base/macros.h"
Sebastien Hertz8ece0502013-08-07 11:26:41 +020029#include "class_linker-inl.h"
30#include "common_throws.h"
31#include "dex_file-inl.h"
32#include "dex_instruction-inl.h"
33#include "dex_instruction.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070034#include "entrypoints/entrypoint_utils-inl.h"
Sebastien Hertz8ece0502013-08-07 11:26:41 +020035#include "gc/accounting/card_table-inl.h"
Mathieu Chartier0cd81352014-05-22 16:48:55 -070036#include "handle_scope-inl.h"
Sebastien Hertz8ece0502013-08-07 11:26:41 +020037#include "nth_caller_visitor.h"
38#include "mirror/art_field-inl.h"
39#include "mirror/art_method.h"
40#include "mirror/art_method-inl.h"
41#include "mirror/class.h"
42#include "mirror/class-inl.h"
43#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"
Sebastien Hertz8ece0502013-08-07 11:26:41 +020046#include "ScopedLocalRef.h"
47#include "scoped_thread_state_change.h"
48#include "thread.h"
49#include "well_known_classes.h"
50
51using ::art::mirror::ArtField;
52using ::art::mirror::ArtMethod;
53using ::art::mirror::Array;
54using ::art::mirror::BooleanArray;
55using ::art::mirror::ByteArray;
56using ::art::mirror::CharArray;
57using ::art::mirror::Class;
58using ::art::mirror::ClassLoader;
59using ::art::mirror::IntArray;
60using ::art::mirror::LongArray;
61using ::art::mirror::Object;
62using ::art::mirror::ObjectArray;
63using ::art::mirror::ShortArray;
64using ::art::mirror::String;
65using ::art::mirror::Throwable;
66
67namespace art {
68namespace interpreter {
69
70// External references to both interpreter implementations.
71
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010072template<bool do_access_check, bool transaction_active>
Ian Rogerse94652f2014-12-02 11:13:19 -080073extern JValue ExecuteSwitchImpl(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
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010076template<bool do_access_check, bool transaction_active>
Ian Rogerse94652f2014-12-02 11:13:19 -080077extern JValue ExecuteGotoImpl(Thread* self, const DexFile::CodeItem* code_item,
Sebastien Hertzc6714852013-09-30 16:42:32 +020078 ShadowFrame& shadow_frame, JValue result_register);
Sebastien Hertz8ece0502013-08-07 11:26:41 +020079
Ian Rogers54874942014-06-10 16:31:03 -070080void ThrowNullPointerExceptionFromInterpreter(const ShadowFrame& shadow_frame)
81 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Sebastien Hertzda843e12014-05-28 19:28:31 +020082
Sebastien Hertz8ece0502013-08-07 11:26:41 +020083static inline void DoMonitorEnter(Thread* self, Object* ref) NO_THREAD_SAFETY_ANALYSIS {
84 ref->MonitorEnter(self);
85}
86
87static inline void DoMonitorExit(Thread* self, Object* ref) NO_THREAD_SAFETY_ANALYSIS {
88 ref->MonitorExit(self);
89}
90
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -070091void AbortTransaction(Thread* self, const char* fmt, ...)
92 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
93
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010094void RecordArrayElementsInTransaction(mirror::Array* array, int32_t count)
95 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
96
Sebastien Hertzc6714852013-09-30 16:42:32 +020097// Invokes the given method. This is part of the invocation support and is used by DoInvoke and
98// DoInvokeVirtualQuick functions.
99// Returns true on success, otherwise throws an exception and returns false.
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200100template<bool is_range, bool do_assignability_check>
Ian Rogerse94652f2014-12-02 11:13:19 -0800101bool DoCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame,
Sebastien Hertzc6714852013-09-30 16:42:32 +0200102 const Instruction* inst, uint16_t inst_data, JValue* result);
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200103
Sebastien Hertzc6714852013-09-30 16:42:32 +0200104// Handles invoke-XXX/range instructions.
105// Returns true on success, otherwise throws an exception and returns false.
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200106template<InvokeType type, bool is_range, bool do_access_check>
107static inline bool DoInvoke(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst,
108 uint16_t inst_data, JValue* result) {
109 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
110 const uint32_t vregC = (is_range) ? inst->VRegC_3rc() : inst->VRegC_35c();
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700111 Object* receiver = (type == kStatic) ? nullptr : shadow_frame.GetVRegReference(vregC);
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700112 mirror::ArtMethod* sf_method = shadow_frame.GetMethod();
Ian Rogerse94652f2014-12-02 11:13:19 -0800113 ArtMethod* const called_method = FindMethodFromCode<type, do_access_check>(
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700114 method_idx, &receiver, &sf_method, self);
115 // The shadow frame should already be pushed, so we don't need to update it.
Ian Rogerse94652f2014-12-02 11:13:19 -0800116 if (UNLIKELY(called_method == nullptr)) {
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200117 CHECK(self->IsExceptionPending());
118 result->SetJ(0);
119 return false;
Ian Rogerse94652f2014-12-02 11:13:19 -0800120 } else if (UNLIKELY(called_method->IsAbstract())) {
121 ThrowAbstractMethodError(called_method);
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200122 result->SetJ(0);
123 return false;
124 } else {
Ian Rogerse94652f2014-12-02 11:13:19 -0800125 return DoCall<is_range, do_access_check>(called_method, self, shadow_frame, inst, inst_data,
126 result);
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200127 }
128}
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200129
Sebastien Hertzc6714852013-09-30 16:42:32 +0200130// Handles invoke-virtual-quick and invoke-virtual-quick-range instructions.
131// Returns true on success, otherwise throws an exception and returns false.
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200132template<bool is_range>
133static inline bool DoInvokeVirtualQuick(Thread* self, ShadowFrame& shadow_frame,
134 const Instruction* inst, uint16_t inst_data,
135 JValue* result) {
136 const uint32_t vregC = (is_range) ? inst->VRegC_3rc() : inst->VRegC_35c();
137 Object* const receiver = shadow_frame.GetVRegReference(vregC);
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200138 if (UNLIKELY(receiver == nullptr)) {
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200139 // We lost the reference to the method index so we cannot get a more
140 // precised exception message.
141 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetCurrentLocationForThrow());
142 return false;
143 }
144 const uint32_t vtable_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700145 CHECK(receiver->GetClass()->ShouldHaveEmbeddedImtAndVTable());
Ian Rogerse94652f2014-12-02 11:13:19 -0800146 ArtMethod* const called_method = receiver->GetClass()->GetEmbeddedVTableEntry(vtable_idx);
147 if (UNLIKELY(called_method == nullptr)) {
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200148 CHECK(self->IsExceptionPending());
149 result->SetJ(0);
150 return false;
Ian Rogerse94652f2014-12-02 11:13:19 -0800151 } else if (UNLIKELY(called_method->IsAbstract())) {
152 ThrowAbstractMethodError(called_method);
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200153 result->SetJ(0);
154 return false;
155 } else {
156 // No need to check since we've been quickened.
Ian Rogerse94652f2014-12-02 11:13:19 -0800157 return DoCall<is_range, false>(called_method, self, shadow_frame, inst, inst_data, result);
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200158 }
159}
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200160
Sebastien Hertzc6714852013-09-30 16:42:32 +0200161// Handles iget-XXX and sget-XXX instructions.
162// Returns true on success, otherwise throws an exception and returns false.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200163template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check>
Ian Rogers54874942014-06-10 16:31:03 -0700164bool DoFieldGet(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst,
165 uint16_t inst_data) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200166
Sebastien Hertzc6714852013-09-30 16:42:32 +0200167// Handles iget-quick, iget-wide-quick and iget-object-quick instructions.
168// Returns true on success, otherwise throws an exception and returns false.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200169template<Primitive::Type field_type>
Ian Rogers54874942014-06-10 16:31:03 -0700170bool DoIGetQuick(ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data)
171 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Sebastien Hertz479fc1e2014-04-04 17:51:34 +0200172
Sebastien Hertzc6714852013-09-30 16:42:32 +0200173// Handles iput-XXX and sput-XXX instructions.
174// Returns true on success, otherwise throws an exception and returns false.
Ian Rogers54874942014-06-10 16:31:03 -0700175template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check,
176 bool transaction_active>
177bool DoFieldPut(Thread* self, const ShadowFrame& shadow_frame, const Instruction* inst,
178 uint16_t inst_data) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200179
Sebastien Hertzc6714852013-09-30 16:42:32 +0200180// Handles iput-quick, iput-wide-quick and iput-object-quick instructions.
181// Returns true on success, otherwise throws an exception and returns false.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100182template<Primitive::Type field_type, bool transaction_active>
Ian Rogers54874942014-06-10 16:31:03 -0700183bool DoIPutQuick(const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data)
184 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
185
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200186
Sebastien Hertzc6714852013-09-30 16:42:32 +0200187// Handles string resolution for const-string and const-string-jumbo instructions. Also ensures the
188// java.lang.String class is initialized.
Ian Rogers6786a582014-10-28 12:49:06 -0700189static inline String* ResolveString(Thread* self, ShadowFrame& shadow_frame, uint32_t string_idx)
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200190 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800191 CHECK(!kMovingMethods);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200192 Class* java_lang_string_class = String::GetJavaLangString();
193 if (UNLIKELY(!java_lang_string_class->IsInitialized())) {
194 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700195 StackHandleScope<1> hs(self);
196 Handle<mirror::Class> h_class(hs.NewHandle(java_lang_string_class));
Ian Rogers7b078e82014-09-10 14:44:24 -0700197 if (UNLIKELY(!class_linker->EnsureInitialized(self, h_class, true, true))) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200198 DCHECK(self->IsExceptionPending());
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800199 return nullptr;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200200 }
201 }
Ian Rogers6786a582014-10-28 12:49:06 -0700202 mirror::ArtMethod* method = shadow_frame.GetMethod();
Mathieu Chartiereace4582014-11-24 18:29:54 -0800203 mirror::Class* declaring_class = method->GetDeclaringClass();
204 mirror::String* s = declaring_class->GetDexCacheStrings()->Get(string_idx);
Ian Rogers6786a582014-10-28 12:49:06 -0700205 if (UNLIKELY(s == nullptr)) {
206 StackHandleScope<1> hs(self);
Mathieu Chartiereace4582014-11-24 18:29:54 -0800207 Handle<mirror::DexCache> dex_cache(hs.NewHandle(declaring_class->GetDexCache()));
Ian Rogers6786a582014-10-28 12:49:06 -0700208 s = Runtime::Current()->GetClassLinker()->ResolveString(*method->GetDexFile(), string_idx,
209 dex_cache);
210 }
211 return s;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200212}
213
Sebastien Hertzc6714852013-09-30 16:42:32 +0200214// Handles div-int, div-int/2addr, div-int/li16 and div-int/lit8 instructions.
215// Returns true on success, otherwise throws a java.lang.ArithmeticException and return false.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200216static inline bool DoIntDivide(ShadowFrame& shadow_frame, size_t result_reg,
217 int32_t dividend, int32_t divisor)
218 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersf72a11d2014-10-30 15:41:08 -0700219 constexpr int32_t kMinInt = std::numeric_limits<int32_t>::min();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200220 if (UNLIKELY(divisor == 0)) {
221 ThrowArithmeticExceptionDivideByZero();
222 return false;
223 }
224 if (UNLIKELY(dividend == kMinInt && divisor == -1)) {
225 shadow_frame.SetVReg(result_reg, kMinInt);
226 } else {
227 shadow_frame.SetVReg(result_reg, dividend / divisor);
228 }
229 return true;
230}
231
Sebastien Hertzc6714852013-09-30 16:42:32 +0200232// Handles rem-int, rem-int/2addr, rem-int/li16 and rem-int/lit8 instructions.
233// Returns true on success, otherwise throws a java.lang.ArithmeticException and return false.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200234static inline bool DoIntRemainder(ShadowFrame& shadow_frame, size_t result_reg,
235 int32_t dividend, int32_t divisor)
236 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersf72a11d2014-10-30 15:41:08 -0700237 constexpr int32_t kMinInt = std::numeric_limits<int32_t>::min();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200238 if (UNLIKELY(divisor == 0)) {
239 ThrowArithmeticExceptionDivideByZero();
240 return false;
241 }
242 if (UNLIKELY(dividend == kMinInt && divisor == -1)) {
243 shadow_frame.SetVReg(result_reg, 0);
244 } else {
245 shadow_frame.SetVReg(result_reg, dividend % divisor);
246 }
247 return true;
248}
249
Sebastien Hertzc6714852013-09-30 16:42:32 +0200250// Handles div-long and div-long-2addr instructions.
251// Returns true on success, otherwise throws a java.lang.ArithmeticException and return false.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200252static inline bool DoLongDivide(ShadowFrame& shadow_frame, size_t result_reg,
253 int64_t dividend, int64_t divisor)
254 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2e2deeb2013-09-23 11:58:57 -0700255 const int64_t kMinLong = std::numeric_limits<int64_t>::min();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200256 if (UNLIKELY(divisor == 0)) {
257 ThrowArithmeticExceptionDivideByZero();
258 return false;
259 }
260 if (UNLIKELY(dividend == kMinLong && divisor == -1)) {
261 shadow_frame.SetVRegLong(result_reg, kMinLong);
262 } else {
263 shadow_frame.SetVRegLong(result_reg, dividend / divisor);
264 }
265 return true;
266}
267
Sebastien Hertzc6714852013-09-30 16:42:32 +0200268// Handles rem-long and rem-long-2addr instructions.
269// Returns true on success, otherwise throws a java.lang.ArithmeticException and return false.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200270static inline bool DoLongRemainder(ShadowFrame& shadow_frame, size_t result_reg,
271 int64_t dividend, int64_t divisor)
272 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2e2deeb2013-09-23 11:58:57 -0700273 const int64_t kMinLong = std::numeric_limits<int64_t>::min();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200274 if (UNLIKELY(divisor == 0)) {
275 ThrowArithmeticExceptionDivideByZero();
276 return false;
277 }
278 if (UNLIKELY(dividend == kMinLong && divisor == -1)) {
279 shadow_frame.SetVRegLong(result_reg, 0);
280 } else {
281 shadow_frame.SetVRegLong(result_reg, dividend % divisor);
282 }
283 return true;
284}
285
Sebastien Hertzc6714852013-09-30 16:42:32 +0200286// Handles filled-new-array and filled-new-array-range instructions.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200287// Returns true on success, otherwise throws an exception and returns false.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100288template <bool is_range, bool do_access_check, bool transaction_active>
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200289bool DoFilledNewArray(const Instruction* inst, const ShadowFrame& shadow_frame,
Sebastien Hertzc6714852013-09-30 16:42:32 +0200290 Thread* self, JValue* result);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200291
Sebastien Hertzc6714852013-09-30 16:42:32 +0200292// Handles packed-switch instruction.
293// Returns the branch offset to the next instruction to execute.
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200294static inline int32_t DoPackedSwitch(const Instruction* inst, const ShadowFrame& shadow_frame,
295 uint16_t inst_data)
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200296 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
297 DCHECK(inst->Opcode() == Instruction::PACKED_SWITCH);
298 const uint16_t* switch_data = reinterpret_cast<const uint16_t*>(inst) + inst->VRegB_31t();
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200299 int32_t test_val = shadow_frame.GetVReg(inst->VRegA_31t(inst_data));
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200300 DCHECK_EQ(switch_data[0], static_cast<uint16_t>(Instruction::kPackedSwitchSignature));
301 uint16_t size = switch_data[1];
302 DCHECK_GT(size, 0);
303 const int32_t* keys = reinterpret_cast<const int32_t*>(&switch_data[2]);
304 DCHECK(IsAligned<4>(keys));
305 int32_t first_key = keys[0];
306 const int32_t* targets = reinterpret_cast<const int32_t*>(&switch_data[4]);
307 DCHECK(IsAligned<4>(targets));
308 int32_t index = test_val - first_key;
309 if (index >= 0 && index < size) {
310 return targets[index];
311 } else {
312 // No corresponding value: move forward by 3 (size of PACKED_SWITCH).
313 return 3;
314 }
315}
316
Sebastien Hertzc6714852013-09-30 16:42:32 +0200317// Handles sparse-switch instruction.
318// Returns the branch offset to the next instruction to execute.
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200319static inline int32_t DoSparseSwitch(const Instruction* inst, const ShadowFrame& shadow_frame,
320 uint16_t inst_data)
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200321 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
322 DCHECK(inst->Opcode() == Instruction::SPARSE_SWITCH);
323 const uint16_t* switch_data = reinterpret_cast<const uint16_t*>(inst) + inst->VRegB_31t();
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200324 int32_t test_val = shadow_frame.GetVReg(inst->VRegA_31t(inst_data));
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200325 DCHECK_EQ(switch_data[0], static_cast<uint16_t>(Instruction::kSparseSwitchSignature));
326 uint16_t size = switch_data[1];
327 DCHECK_GT(size, 0);
328 const int32_t* keys = reinterpret_cast<const int32_t*>(&switch_data[2]);
329 DCHECK(IsAligned<4>(keys));
330 const int32_t* entries = keys + size;
331 DCHECK(IsAligned<4>(entries));
332 int lo = 0;
333 int hi = size - 1;
334 while (lo <= hi) {
335 int mid = (lo + hi) / 2;
336 int32_t foundVal = keys[mid];
337 if (test_val < foundVal) {
338 hi = mid - 1;
339 } else if (test_val > foundVal) {
340 lo = mid + 1;
341 } else {
342 return entries[mid];
343 }
344 }
345 // No corresponding value: move forward by 3 (size of SPARSE_SWITCH).
346 return 3;
347}
348
Ian Rogers54874942014-06-10 16:31:03 -0700349uint32_t FindNextInstructionFollowingException(Thread* self, ShadowFrame& shadow_frame,
Sebastien Hertz9f102032014-05-23 08:59:42 +0200350 uint32_t dex_pc, const instrumentation::Instrumentation* instrumentation)
Ian Rogers54874942014-06-10 16:31:03 -0700351 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200352
Andreas Gampe794ad762015-02-23 08:12:24 -0800353NO_RETURN void UnexpectedOpcode(const Instruction* inst, const ShadowFrame& shadow_frame)
354 __attribute__((cold))
Ian Rogersb48b9eb2014-02-28 16:20:21 -0800355 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200356
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200357static inline void TraceExecution(const ShadowFrame& shadow_frame, const Instruction* inst,
Ian Rogerse94652f2014-12-02 11:13:19 -0800358 const uint32_t dex_pc)
Jeff Haoa3faaf42013-09-03 19:07:00 -0700359 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700360 constexpr bool kTracing = false;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200361 if (kTracing) {
362#define TRACE_LOG std::cerr
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700363 std::ostringstream oss;
364 oss << PrettyMethod(shadow_frame.GetMethod())
365 << StringPrintf("\n0x%x: ", dex_pc)
Ian Rogerse94652f2014-12-02 11:13:19 -0800366 << inst->DumpString(shadow_frame.GetMethod()->GetDexFile()) << "\n";
Ian Rogersef7d42f2014-01-06 12:55:46 -0800367 for (uint32_t i = 0; i < shadow_frame.NumberOfVRegs(); ++i) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200368 uint32_t raw_value = shadow_frame.GetVReg(i);
369 Object* ref_value = shadow_frame.GetVRegReference(i);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800370 oss << StringPrintf(" vreg%u=0x%08X", i, raw_value);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200371 if (ref_value != NULL) {
372 if (ref_value->GetClass()->IsStringClass() &&
373 ref_value->AsString()->GetCharArray() != NULL) {
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700374 oss << "/java.lang.String \"" << ref_value->AsString()->ToModifiedUtf8() << "\"";
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200375 } else {
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700376 oss << "/" << PrettyTypeOf(ref_value);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200377 }
378 }
379 }
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700380 TRACE_LOG << oss.str() << "\n";
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200381#undef TRACE_LOG
382 }
383}
384
Sebastien Hertz1eda2262013-09-09 16:53:14 +0200385static inline bool IsBackwardBranch(int32_t branch_offset) {
386 return branch_offset <= 0;
387}
388
Sebastien Hertzc6714852013-09-30 16:42:32 +0200389// Explicitly instantiate all DoInvoke functions.
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100390#define EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, _is_range, _do_check) \
Ian Rogers54874942014-06-10 16:31:03 -0700391 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) \
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100392 bool DoInvoke<_type, _is_range, _do_check>(Thread* self, ShadowFrame& shadow_frame, \
393 const Instruction* inst, uint16_t inst_data, \
394 JValue* result)
Sebastien Hertzc6714852013-09-30 16:42:32 +0200395
396#define EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(_type) \
397 EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, false, false); \
398 EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, false, true); \
399 EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, true, false); \
400 EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, true, true);
401
Andreas Gampec8ccf682014-09-29 20:07:43 -0700402EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(kStatic) // invoke-static/range.
403EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(kDirect) // invoke-direct/range.
404EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(kVirtual) // invoke-virtual/range.
405EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(kSuper) // invoke-super/range.
406EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(kInterface) // invoke-interface/range.
Sebastien Hertzc6714852013-09-30 16:42:32 +0200407#undef EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL
408#undef EXPLICIT_DO_INVOKE_TEMPLATE_DECL
409
Sebastien Hertzc6714852013-09-30 16:42:32 +0200410// Explicitly instantiate all DoInvokeVirtualQuick functions.
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100411#define EXPLICIT_DO_INVOKE_VIRTUAL_QUICK_TEMPLATE_DECL(_is_range) \
Ian Rogers54874942014-06-10 16:31:03 -0700412 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) \
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100413 bool DoInvokeVirtualQuick<_is_range>(Thread* self, ShadowFrame& shadow_frame, \
414 const Instruction* inst, uint16_t inst_data, \
415 JValue* result)
Sebastien Hertzc6714852013-09-30 16:42:32 +0200416
417EXPLICIT_DO_INVOKE_VIRTUAL_QUICK_TEMPLATE_DECL(false); // invoke-virtual-quick.
418EXPLICIT_DO_INVOKE_VIRTUAL_QUICK_TEMPLATE_DECL(true); // invoke-virtual-quick-range.
419#undef EXPLICIT_INSTANTIATION_DO_INVOKE_VIRTUAL_QUICK
420
Sebastien Hertzc6714852013-09-30 16:42:32 +0200421
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200422} // namespace interpreter
423} // namespace art
424
425#endif // ART_RUNTIME_INTERPRETER_INTERPRETER_COMMON_H_