blob: ce7c1c38175979682d0d09f4f32ef0345b0a4e04 [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"
28#include "class_linker-inl.h"
29#include "common_throws.h"
30#include "dex_file-inl.h"
31#include "dex_instruction-inl.h"
32#include "dex_instruction.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070033#include "entrypoints/entrypoint_utils-inl.h"
Sebastien Hertz8ece0502013-08-07 11:26:41 +020034#include "gc/accounting/card_table-inl.h"
Mathieu Chartier0cd81352014-05-22 16:48:55 -070035#include "handle_scope-inl.h"
Sebastien Hertz8ece0502013-08-07 11:26:41 +020036#include "nth_caller_visitor.h"
37#include "mirror/art_field-inl.h"
38#include "mirror/art_method.h"
39#include "mirror/art_method-inl.h"
40#include "mirror/class.h"
41#include "mirror/class-inl.h"
42#include "mirror/object-inl.h"
43#include "mirror/object_array-inl.h"
Douglas Leung4965c022014-06-11 11:41:11 -070044#include "mirror/string-inl.h"
Sebastien Hertz8ece0502013-08-07 11:26:41 +020045#include "ScopedLocalRef.h"
46#include "scoped_thread_state_change.h"
47#include "thread.h"
48#include "well_known_classes.h"
49
50using ::art::mirror::ArtField;
51using ::art::mirror::ArtMethod;
52using ::art::mirror::Array;
53using ::art::mirror::BooleanArray;
54using ::art::mirror::ByteArray;
55using ::art::mirror::CharArray;
56using ::art::mirror::Class;
57using ::art::mirror::ClassLoader;
58using ::art::mirror::IntArray;
59using ::art::mirror::LongArray;
60using ::art::mirror::Object;
61using ::art::mirror::ObjectArray;
62using ::art::mirror::ShortArray;
63using ::art::mirror::String;
64using ::art::mirror::Throwable;
65
66namespace art {
67namespace interpreter {
68
69// External references to both interpreter implementations.
70
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010071template<bool do_access_check, bool transaction_active>
Ian Rogerse94652f2014-12-02 11:13:19 -080072extern JValue ExecuteSwitchImpl(Thread* self, const DexFile::CodeItem* code_item,
Sebastien Hertzc6714852013-09-30 16:42:32 +020073 ShadowFrame& shadow_frame, JValue result_register);
Sebastien Hertz8ece0502013-08-07 11:26:41 +020074
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010075template<bool do_access_check, bool transaction_active>
Ian Rogerse94652f2014-12-02 11:13:19 -080076extern JValue ExecuteGotoImpl(Thread* self, const DexFile::CodeItem* code_item,
Sebastien Hertzc6714852013-09-30 16:42:32 +020077 ShadowFrame& shadow_frame, JValue result_register);
Sebastien Hertz8ece0502013-08-07 11:26:41 +020078
Ian Rogers54874942014-06-10 16:31:03 -070079void ThrowNullPointerExceptionFromInterpreter(const ShadowFrame& shadow_frame)
80 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Sebastien Hertzda843e12014-05-28 19:28:31 +020081
Sebastien Hertz8ece0502013-08-07 11:26:41 +020082static inline void DoMonitorEnter(Thread* self, Object* ref) NO_THREAD_SAFETY_ANALYSIS {
83 ref->MonitorEnter(self);
84}
85
86static inline void DoMonitorExit(Thread* self, Object* ref) NO_THREAD_SAFETY_ANALYSIS {
87 ref->MonitorExit(self);
88}
89
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -070090void AbortTransaction(Thread* self, const char* fmt, ...)
91 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
92
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010093void RecordArrayElementsInTransaction(mirror::Array* array, int32_t count)
94 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
95
Sebastien Hertzc6714852013-09-30 16:42:32 +020096// Invokes the given method. This is part of the invocation support and is used by DoInvoke and
97// DoInvokeVirtualQuick functions.
98// Returns true on success, otherwise throws an exception and returns false.
Sebastien Hertzc61124b2013-09-10 11:44:19 +020099template<bool is_range, bool do_assignability_check>
Ian Rogerse94652f2014-12-02 11:13:19 -0800100bool DoCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame,
Sebastien Hertzc6714852013-09-30 16:42:32 +0200101 const Instruction* inst, uint16_t inst_data, JValue* result);
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200102
Sebastien Hertzc6714852013-09-30 16:42:32 +0200103// Handles invoke-XXX/range instructions.
104// Returns true on success, otherwise throws an exception and returns false.
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200105template<InvokeType type, bool is_range, bool do_access_check>
106static inline bool DoInvoke(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst,
107 uint16_t inst_data, JValue* result) {
108 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
109 const uint32_t vregC = (is_range) ? inst->VRegC_3rc() : inst->VRegC_35c();
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700110 Object* receiver = (type == kStatic) ? nullptr : shadow_frame.GetVRegReference(vregC);
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700111 mirror::ArtMethod* sf_method = shadow_frame.GetMethod();
Ian Rogerse94652f2014-12-02 11:13:19 -0800112 ArtMethod* const called_method = FindMethodFromCode<type, do_access_check>(
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700113 method_idx, &receiver, &sf_method, self);
114 // The shadow frame should already be pushed, so we don't need to update it.
Ian Rogerse94652f2014-12-02 11:13:19 -0800115 if (UNLIKELY(called_method == nullptr)) {
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200116 CHECK(self->IsExceptionPending());
117 result->SetJ(0);
118 return false;
Ian Rogerse94652f2014-12-02 11:13:19 -0800119 } else if (UNLIKELY(called_method->IsAbstract())) {
120 ThrowAbstractMethodError(called_method);
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200121 result->SetJ(0);
122 return false;
123 } else {
Ian Rogerse94652f2014-12-02 11:13:19 -0800124 return DoCall<is_range, do_access_check>(called_method, self, shadow_frame, inst, inst_data,
125 result);
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200126 }
127}
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200128
Sebastien Hertzc6714852013-09-30 16:42:32 +0200129// Handles invoke-virtual-quick and invoke-virtual-quick-range instructions.
130// Returns true on success, otherwise throws an exception and returns false.
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200131template<bool is_range>
132static inline bool DoInvokeVirtualQuick(Thread* self, ShadowFrame& shadow_frame,
133 const Instruction* inst, uint16_t inst_data,
134 JValue* result) {
135 const uint32_t vregC = (is_range) ? inst->VRegC_3rc() : inst->VRegC_35c();
136 Object* const receiver = shadow_frame.GetVRegReference(vregC);
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200137 if (UNLIKELY(receiver == nullptr)) {
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200138 // We lost the reference to the method index so we cannot get a more
139 // precised exception message.
140 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetCurrentLocationForThrow());
141 return false;
142 }
143 const uint32_t vtable_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700144 CHECK(receiver->GetClass()->ShouldHaveEmbeddedImtAndVTable());
Ian Rogerse94652f2014-12-02 11:13:19 -0800145 ArtMethod* const called_method = receiver->GetClass()->GetEmbeddedVTableEntry(vtable_idx);
146 if (UNLIKELY(called_method == nullptr)) {
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200147 CHECK(self->IsExceptionPending());
148 result->SetJ(0);
149 return false;
Ian Rogerse94652f2014-12-02 11:13:19 -0800150 } else if (UNLIKELY(called_method->IsAbstract())) {
151 ThrowAbstractMethodError(called_method);
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200152 result->SetJ(0);
153 return false;
154 } else {
155 // No need to check since we've been quickened.
Ian Rogerse94652f2014-12-02 11:13:19 -0800156 return DoCall<is_range, false>(called_method, self, shadow_frame, inst, inst_data, result);
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200157 }
158}
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200159
Sebastien Hertzc6714852013-09-30 16:42:32 +0200160// Handles iget-XXX and sget-XXX instructions.
161// Returns true on success, otherwise throws an exception and returns false.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200162template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check>
Ian Rogers54874942014-06-10 16:31:03 -0700163bool DoFieldGet(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst,
164 uint16_t inst_data) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200165
Sebastien Hertzc6714852013-09-30 16:42:32 +0200166// Handles iget-quick, iget-wide-quick and iget-object-quick instructions.
167// Returns true on success, otherwise throws an exception and returns false.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200168template<Primitive::Type field_type>
Ian Rogers54874942014-06-10 16:31:03 -0700169bool DoIGetQuick(ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data)
170 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Sebastien Hertz479fc1e2014-04-04 17:51:34 +0200171
Sebastien Hertzc6714852013-09-30 16:42:32 +0200172// Handles iput-XXX and sput-XXX instructions.
173// Returns true on success, otherwise throws an exception and returns false.
Ian Rogers54874942014-06-10 16:31:03 -0700174template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check,
175 bool transaction_active>
176bool DoFieldPut(Thread* self, const ShadowFrame& shadow_frame, const Instruction* inst,
177 uint16_t inst_data) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200178
Sebastien Hertzc6714852013-09-30 16:42:32 +0200179// Handles iput-quick, iput-wide-quick and iput-object-quick instructions.
180// Returns true on success, otherwise throws an exception and returns false.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100181template<Primitive::Type field_type, bool transaction_active>
Ian Rogers54874942014-06-10 16:31:03 -0700182bool DoIPutQuick(const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data)
183 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
184
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200185
Sebastien Hertzc6714852013-09-30 16:42:32 +0200186// Handles string resolution for const-string and const-string-jumbo instructions. Also ensures the
187// java.lang.String class is initialized.
Ian Rogers6786a582014-10-28 12:49:06 -0700188static inline String* ResolveString(Thread* self, ShadowFrame& shadow_frame, uint32_t string_idx)
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200189 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800190 CHECK(!kMovingMethods);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200191 Class* java_lang_string_class = String::GetJavaLangString();
192 if (UNLIKELY(!java_lang_string_class->IsInitialized())) {
193 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700194 StackHandleScope<1> hs(self);
195 Handle<mirror::Class> h_class(hs.NewHandle(java_lang_string_class));
Ian Rogers7b078e82014-09-10 14:44:24 -0700196 if (UNLIKELY(!class_linker->EnsureInitialized(self, h_class, true, true))) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200197 DCHECK(self->IsExceptionPending());
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800198 return nullptr;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200199 }
200 }
Ian Rogers6786a582014-10-28 12:49:06 -0700201 mirror::ArtMethod* method = shadow_frame.GetMethod();
Mathieu Chartiereace4582014-11-24 18:29:54 -0800202 mirror::Class* declaring_class = method->GetDeclaringClass();
203 mirror::String* s = declaring_class->GetDexCacheStrings()->Get(string_idx);
Ian Rogers6786a582014-10-28 12:49:06 -0700204 if (UNLIKELY(s == nullptr)) {
205 StackHandleScope<1> hs(self);
Mathieu Chartiereace4582014-11-24 18:29:54 -0800206 Handle<mirror::DexCache> dex_cache(hs.NewHandle(declaring_class->GetDexCache()));
Ian Rogers6786a582014-10-28 12:49:06 -0700207 s = Runtime::Current()->GetClassLinker()->ResolveString(*method->GetDexFile(), string_idx,
208 dex_cache);
209 }
210 return s;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200211}
212
Sebastien Hertzc6714852013-09-30 16:42:32 +0200213// Handles div-int, div-int/2addr, div-int/li16 and div-int/lit8 instructions.
214// Returns true on success, otherwise throws a java.lang.ArithmeticException and return false.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200215static inline bool DoIntDivide(ShadowFrame& shadow_frame, size_t result_reg,
216 int32_t dividend, int32_t divisor)
217 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersf72a11d2014-10-30 15:41:08 -0700218 constexpr int32_t kMinInt = std::numeric_limits<int32_t>::min();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200219 if (UNLIKELY(divisor == 0)) {
220 ThrowArithmeticExceptionDivideByZero();
221 return false;
222 }
223 if (UNLIKELY(dividend == kMinInt && divisor == -1)) {
224 shadow_frame.SetVReg(result_reg, kMinInt);
225 } else {
226 shadow_frame.SetVReg(result_reg, dividend / divisor);
227 }
228 return true;
229}
230
Sebastien Hertzc6714852013-09-30 16:42:32 +0200231// Handles rem-int, rem-int/2addr, rem-int/li16 and rem-int/lit8 instructions.
232// Returns true on success, otherwise throws a java.lang.ArithmeticException and return false.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200233static inline bool DoIntRemainder(ShadowFrame& shadow_frame, size_t result_reg,
234 int32_t dividend, int32_t divisor)
235 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersf72a11d2014-10-30 15:41:08 -0700236 constexpr int32_t kMinInt = std::numeric_limits<int32_t>::min();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200237 if (UNLIKELY(divisor == 0)) {
238 ThrowArithmeticExceptionDivideByZero();
239 return false;
240 }
241 if (UNLIKELY(dividend == kMinInt && divisor == -1)) {
242 shadow_frame.SetVReg(result_reg, 0);
243 } else {
244 shadow_frame.SetVReg(result_reg, dividend % divisor);
245 }
246 return true;
247}
248
Sebastien Hertzc6714852013-09-30 16:42:32 +0200249// Handles div-long and div-long-2addr instructions.
250// Returns true on success, otherwise throws a java.lang.ArithmeticException and return false.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200251static inline bool DoLongDivide(ShadowFrame& shadow_frame, size_t result_reg,
252 int64_t dividend, int64_t divisor)
253 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2e2deeb2013-09-23 11:58:57 -0700254 const int64_t kMinLong = std::numeric_limits<int64_t>::min();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200255 if (UNLIKELY(divisor == 0)) {
256 ThrowArithmeticExceptionDivideByZero();
257 return false;
258 }
259 if (UNLIKELY(dividend == kMinLong && divisor == -1)) {
260 shadow_frame.SetVRegLong(result_reg, kMinLong);
261 } else {
262 shadow_frame.SetVRegLong(result_reg, dividend / divisor);
263 }
264 return true;
265}
266
Sebastien Hertzc6714852013-09-30 16:42:32 +0200267// Handles rem-long and rem-long-2addr instructions.
268// Returns true on success, otherwise throws a java.lang.ArithmeticException and return false.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200269static inline bool DoLongRemainder(ShadowFrame& shadow_frame, size_t result_reg,
270 int64_t dividend, int64_t divisor)
271 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2e2deeb2013-09-23 11:58:57 -0700272 const int64_t kMinLong = std::numeric_limits<int64_t>::min();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200273 if (UNLIKELY(divisor == 0)) {
274 ThrowArithmeticExceptionDivideByZero();
275 return false;
276 }
277 if (UNLIKELY(dividend == kMinLong && divisor == -1)) {
278 shadow_frame.SetVRegLong(result_reg, 0);
279 } else {
280 shadow_frame.SetVRegLong(result_reg, dividend % divisor);
281 }
282 return true;
283}
284
Sebastien Hertzc6714852013-09-30 16:42:32 +0200285// Handles filled-new-array and filled-new-array-range instructions.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200286// Returns true on success, otherwise throws an exception and returns false.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100287template <bool is_range, bool do_access_check, bool transaction_active>
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200288bool DoFilledNewArray(const Instruction* inst, const ShadowFrame& shadow_frame,
Sebastien Hertzc6714852013-09-30 16:42:32 +0200289 Thread* self, JValue* result);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200290
Sebastien Hertzc6714852013-09-30 16:42:32 +0200291// Handles packed-switch instruction.
292// Returns the branch offset to the next instruction to execute.
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200293static inline int32_t DoPackedSwitch(const Instruction* inst, const ShadowFrame& shadow_frame,
294 uint16_t inst_data)
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200295 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
296 DCHECK(inst->Opcode() == Instruction::PACKED_SWITCH);
297 const uint16_t* switch_data = reinterpret_cast<const uint16_t*>(inst) + inst->VRegB_31t();
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200298 int32_t test_val = shadow_frame.GetVReg(inst->VRegA_31t(inst_data));
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200299 DCHECK_EQ(switch_data[0], static_cast<uint16_t>(Instruction::kPackedSwitchSignature));
300 uint16_t size = switch_data[1];
301 DCHECK_GT(size, 0);
302 const int32_t* keys = reinterpret_cast<const int32_t*>(&switch_data[2]);
303 DCHECK(IsAligned<4>(keys));
304 int32_t first_key = keys[0];
305 const int32_t* targets = reinterpret_cast<const int32_t*>(&switch_data[4]);
306 DCHECK(IsAligned<4>(targets));
307 int32_t index = test_val - first_key;
308 if (index >= 0 && index < size) {
309 return targets[index];
310 } else {
311 // No corresponding value: move forward by 3 (size of PACKED_SWITCH).
312 return 3;
313 }
314}
315
Sebastien Hertzc6714852013-09-30 16:42:32 +0200316// Handles sparse-switch instruction.
317// Returns the branch offset to the next instruction to execute.
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200318static inline int32_t DoSparseSwitch(const Instruction* inst, const ShadowFrame& shadow_frame,
319 uint16_t inst_data)
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200320 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
321 DCHECK(inst->Opcode() == Instruction::SPARSE_SWITCH);
322 const uint16_t* switch_data = reinterpret_cast<const uint16_t*>(inst) + inst->VRegB_31t();
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200323 int32_t test_val = shadow_frame.GetVReg(inst->VRegA_31t(inst_data));
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200324 DCHECK_EQ(switch_data[0], static_cast<uint16_t>(Instruction::kSparseSwitchSignature));
325 uint16_t size = switch_data[1];
326 DCHECK_GT(size, 0);
327 const int32_t* keys = reinterpret_cast<const int32_t*>(&switch_data[2]);
328 DCHECK(IsAligned<4>(keys));
329 const int32_t* entries = keys + size;
330 DCHECK(IsAligned<4>(entries));
331 int lo = 0;
332 int hi = size - 1;
333 while (lo <= hi) {
334 int mid = (lo + hi) / 2;
335 int32_t foundVal = keys[mid];
336 if (test_val < foundVal) {
337 hi = mid - 1;
338 } else if (test_val > foundVal) {
339 lo = mid + 1;
340 } else {
341 return entries[mid];
342 }
343 }
344 // No corresponding value: move forward by 3 (size of SPARSE_SWITCH).
345 return 3;
346}
347
Ian Rogers54874942014-06-10 16:31:03 -0700348uint32_t FindNextInstructionFollowingException(Thread* self, ShadowFrame& shadow_frame,
Sebastien Hertz9f102032014-05-23 08:59:42 +0200349 uint32_t dex_pc, const instrumentation::Instrumentation* instrumentation)
Ian Rogers54874942014-06-10 16:31:03 -0700350 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200351
Ian Rogerse94652f2014-12-02 11:13:19 -0800352void UnexpectedOpcode(const Instruction* inst, const ShadowFrame& shadow_frame)
Ian Rogersb48b9eb2014-02-28 16:20:21 -0800353 __attribute__((cold, noreturn))
354 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200355
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200356static inline void TraceExecution(const ShadowFrame& shadow_frame, const Instruction* inst,
Ian Rogerse94652f2014-12-02 11:13:19 -0800357 const uint32_t dex_pc)
Jeff Haoa3faaf42013-09-03 19:07:00 -0700358 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700359 constexpr bool kTracing = false;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200360 if (kTracing) {
361#define TRACE_LOG std::cerr
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700362 std::ostringstream oss;
363 oss << PrettyMethod(shadow_frame.GetMethod())
364 << StringPrintf("\n0x%x: ", dex_pc)
Ian Rogerse94652f2014-12-02 11:13:19 -0800365 << inst->DumpString(shadow_frame.GetMethod()->GetDexFile()) << "\n";
Ian Rogersef7d42f2014-01-06 12:55:46 -0800366 for (uint32_t i = 0; i < shadow_frame.NumberOfVRegs(); ++i) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200367 uint32_t raw_value = shadow_frame.GetVReg(i);
368 Object* ref_value = shadow_frame.GetVRegReference(i);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800369 oss << StringPrintf(" vreg%u=0x%08X", i, raw_value);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200370 if (ref_value != NULL) {
371 if (ref_value->GetClass()->IsStringClass() &&
372 ref_value->AsString()->GetCharArray() != NULL) {
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700373 oss << "/java.lang.String \"" << ref_value->AsString()->ToModifiedUtf8() << "\"";
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200374 } else {
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700375 oss << "/" << PrettyTypeOf(ref_value);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200376 }
377 }
378 }
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700379 TRACE_LOG << oss.str() << "\n";
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200380#undef TRACE_LOG
381 }
382}
383
Sebastien Hertz1eda2262013-09-09 16:53:14 +0200384static inline bool IsBackwardBranch(int32_t branch_offset) {
385 return branch_offset <= 0;
386}
387
Sebastien Hertzc6714852013-09-30 16:42:32 +0200388// Explicitly instantiate all DoInvoke functions.
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100389#define EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, _is_range, _do_check) \
Ian Rogers54874942014-06-10 16:31:03 -0700390 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) \
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100391 bool DoInvoke<_type, _is_range, _do_check>(Thread* self, ShadowFrame& shadow_frame, \
392 const Instruction* inst, uint16_t inst_data, \
393 JValue* result)
Sebastien Hertzc6714852013-09-30 16:42:32 +0200394
395#define EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(_type) \
396 EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, false, false); \
397 EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, false, true); \
398 EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, true, false); \
399 EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, true, true);
400
Andreas Gampec8ccf682014-09-29 20:07:43 -0700401EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(kStatic) // invoke-static/range.
402EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(kDirect) // invoke-direct/range.
403EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(kVirtual) // invoke-virtual/range.
404EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(kSuper) // invoke-super/range.
405EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(kInterface) // invoke-interface/range.
Sebastien Hertzc6714852013-09-30 16:42:32 +0200406#undef EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL
407#undef EXPLICIT_DO_INVOKE_TEMPLATE_DECL
408
Sebastien Hertzc6714852013-09-30 16:42:32 +0200409// Explicitly instantiate all DoInvokeVirtualQuick functions.
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100410#define EXPLICIT_DO_INVOKE_VIRTUAL_QUICK_TEMPLATE_DECL(_is_range) \
Ian Rogers54874942014-06-10 16:31:03 -0700411 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) \
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100412 bool DoInvokeVirtualQuick<_is_range>(Thread* self, ShadowFrame& shadow_frame, \
413 const Instruction* inst, uint16_t inst_data, \
414 JValue* result)
Sebastien Hertzc6714852013-09-30 16:42:32 +0200415
416EXPLICIT_DO_INVOKE_VIRTUAL_QUICK_TEMPLATE_DECL(false); // invoke-virtual-quick.
417EXPLICIT_DO_INVOKE_VIRTUAL_QUICK_TEMPLATE_DECL(true); // invoke-virtual-quick-range.
418#undef EXPLICIT_INSTANTIATION_DO_INVOKE_VIRTUAL_QUICK
419
Sebastien Hertzc6714852013-09-30 16:42:32 +0200420
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200421} // namespace interpreter
422} // namespace art
423
424#endif // ART_RUNTIME_INTERPRETER_INTERPRETER_COMMON_H_