blob: 3b8d50bc251d5907f5f0bd44b468cbc080ce491c [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
24#include "base/logging.h"
25#include "class_linker-inl.h"
26#include "common_throws.h"
27#include "dex_file-inl.h"
28#include "dex_instruction-inl.h"
29#include "dex_instruction.h"
30#include "entrypoints/entrypoint_utils.h"
31#include "gc/accounting/card_table-inl.h"
32#include "invoke_arg_array_builder.h"
33#include "nth_caller_visitor.h"
34#include "mirror/art_field-inl.h"
35#include "mirror/art_method.h"
36#include "mirror/art_method-inl.h"
37#include "mirror/class.h"
38#include "mirror/class-inl.h"
39#include "mirror/object-inl.h"
40#include "mirror/object_array-inl.h"
41#include "object_utils.h"
42#include "ScopedLocalRef.h"
43#include "scoped_thread_state_change.h"
44#include "thread.h"
45#include "well_known_classes.h"
46
47using ::art::mirror::ArtField;
48using ::art::mirror::ArtMethod;
49using ::art::mirror::Array;
50using ::art::mirror::BooleanArray;
51using ::art::mirror::ByteArray;
52using ::art::mirror::CharArray;
53using ::art::mirror::Class;
54using ::art::mirror::ClassLoader;
55using ::art::mirror::IntArray;
56using ::art::mirror::LongArray;
57using ::art::mirror::Object;
58using ::art::mirror::ObjectArray;
59using ::art::mirror::ShortArray;
60using ::art::mirror::String;
61using ::art::mirror::Throwable;
62
63namespace art {
64namespace interpreter {
65
66// External references to both interpreter implementations.
67
Sebastien Hertz8ece0502013-08-07 11:26:41 +020068template<bool do_access_check>
69extern JValue ExecuteSwitchImpl(Thread* self, MethodHelper& mh,
70 const DexFile::CodeItem* code_item,
Sebastien Hertzc6714852013-09-30 16:42:32 +020071 ShadowFrame& shadow_frame, JValue result_register);
Sebastien Hertz8ece0502013-08-07 11:26:41 +020072
Sebastien Hertz8ece0502013-08-07 11:26:41 +020073template<bool do_access_check>
74extern JValue ExecuteGotoImpl(Thread* self, MethodHelper& mh,
75 const DexFile::CodeItem* code_item,
Sebastien Hertzc6714852013-09-30 16:42:32 +020076 ShadowFrame& shadow_frame, JValue result_register);
Sebastien Hertz8ece0502013-08-07 11:26:41 +020077
78static inline void DoMonitorEnter(Thread* self, Object* ref) NO_THREAD_SAFETY_ANALYSIS {
79 ref->MonitorEnter(self);
80}
81
82static inline void DoMonitorExit(Thread* self, Object* ref) NO_THREAD_SAFETY_ANALYSIS {
83 ref->MonitorExit(self);
84}
85
Sebastien Hertzc6714852013-09-30 16:42:32 +020086// Invokes the given method. This is part of the invocation support and is used by DoInvoke and
87// DoInvokeVirtualQuick functions.
88// Returns true on success, otherwise throws an exception and returns false.
Sebastien Hertzc61124b2013-09-10 11:44:19 +020089template<bool is_range, bool do_assignability_check>
90bool DoCall(ArtMethod* method, Object* receiver, Thread* self, ShadowFrame& shadow_frame,
Sebastien Hertzc6714852013-09-30 16:42:32 +020091 const Instruction* inst, uint16_t inst_data, JValue* result);
Sebastien Hertzc61124b2013-09-10 11:44:19 +020092
Sebastien Hertzc6714852013-09-30 16:42:32 +020093// Handles invoke-XXX/range instructions.
94// Returns true on success, otherwise throws an exception and returns false.
Sebastien Hertzc61124b2013-09-10 11:44:19 +020095template<InvokeType type, bool is_range, bool do_access_check>
96static inline bool DoInvoke(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst,
97 uint16_t inst_data, JValue* result) {
98 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
99 const uint32_t vregC = (is_range) ? inst->VRegC_3rc() : inst->VRegC_35c();
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700100 Object* receiver = (type == kStatic) ? nullptr : shadow_frame.GetVRegReference(vregC);
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200101 ArtMethod* const method = FindMethodFromCode<type, do_access_check>(method_idx, receiver,
102 shadow_frame.GetMethod(),
103 self);
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700104 if (type != kStatic) {
105 // Reload the vreg since the GC may have moved the object.
106 receiver = shadow_frame.GetVRegReference(vregC);
107 }
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200108 if (UNLIKELY(method == nullptr)) {
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200109 CHECK(self->IsExceptionPending());
110 result->SetJ(0);
111 return false;
112 } else if (UNLIKELY(method->IsAbstract())) {
113 ThrowAbstractMethodError(method);
114 result->SetJ(0);
115 return false;
116 } else {
117 return DoCall<is_range, do_access_check>(method, receiver, self, shadow_frame, inst,
118 inst_data, result);
119 }
120}
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200121
Sebastien Hertzc6714852013-09-30 16:42:32 +0200122// Handles invoke-virtual-quick and invoke-virtual-quick-range instructions.
123// Returns true on success, otherwise throws an exception and returns false.
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200124template<bool is_range>
125static inline bool DoInvokeVirtualQuick(Thread* self, ShadowFrame& shadow_frame,
126 const Instruction* inst, uint16_t inst_data,
127 JValue* result) {
128 const uint32_t vregC = (is_range) ? inst->VRegC_3rc() : inst->VRegC_35c();
129 Object* const receiver = shadow_frame.GetVRegReference(vregC);
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200130 if (UNLIKELY(receiver == nullptr)) {
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200131 // We lost the reference to the method index so we cannot get a more
132 // precised exception message.
133 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetCurrentLocationForThrow());
134 return false;
135 }
136 const uint32_t vtable_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
137 ArtMethod* const method = receiver->GetClass()->GetVTable()->GetWithoutChecks(vtable_idx);
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200138 if (UNLIKELY(method == nullptr)) {
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200139 CHECK(self->IsExceptionPending());
140 result->SetJ(0);
141 return false;
142 } else if (UNLIKELY(method->IsAbstract())) {
143 ThrowAbstractMethodError(method);
144 result->SetJ(0);
145 return false;
146 } else {
147 // No need to check since we've been quickened.
148 return DoCall<is_range, false>(method, receiver, self, shadow_frame, inst, inst_data, result);
149 }
150}
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200151
Sebastien Hertzc6714852013-09-30 16:42:32 +0200152// Handles iget-XXX and sget-XXX instructions.
153// Returns true on success, otherwise throws an exception and returns false.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200154template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check>
155static inline bool DoFieldGet(Thread* self, ShadowFrame& shadow_frame,
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200156 const Instruction* inst, uint16_t inst_data) {
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200157 const bool is_static = (find_type == StaticObjectRead) || (find_type == StaticPrimitiveRead);
158 const uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
159 ArtField* f = FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
160 Primitive::FieldSize(field_type));
161 if (UNLIKELY(f == nullptr)) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200162 CHECK(self->IsExceptionPending());
163 return false;
164 }
165 Object* obj;
166 if (is_static) {
167 obj = f->GetDeclaringClass();
168 } else {
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200169 obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200170 if (UNLIKELY(obj == nullptr)) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200171 ThrowNullPointerExceptionForFieldAccess(shadow_frame.GetCurrentLocationForThrow(), f, true);
172 return false;
173 }
174 }
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200175 uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200176 switch (field_type) {
177 case Primitive::kPrimBoolean:
178 shadow_frame.SetVReg(vregA, f->GetBoolean(obj));
179 break;
180 case Primitive::kPrimByte:
181 shadow_frame.SetVReg(vregA, f->GetByte(obj));
182 break;
183 case Primitive::kPrimChar:
184 shadow_frame.SetVReg(vregA, f->GetChar(obj));
185 break;
186 case Primitive::kPrimShort:
187 shadow_frame.SetVReg(vregA, f->GetShort(obj));
188 break;
189 case Primitive::kPrimInt:
190 shadow_frame.SetVReg(vregA, f->GetInt(obj));
191 break;
192 case Primitive::kPrimLong:
193 shadow_frame.SetVRegLong(vregA, f->GetLong(obj));
194 break;
195 case Primitive::kPrimNot:
196 shadow_frame.SetVRegReference(vregA, f->GetObject(obj));
197 break;
198 default:
199 LOG(FATAL) << "Unreachable: " << field_type;
200 }
201 return true;
202}
203
Sebastien Hertzc6714852013-09-30 16:42:32 +0200204// Handles iget-quick, iget-wide-quick and iget-object-quick instructions.
205// Returns true on success, otherwise throws an exception and returns false.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200206template<Primitive::Type field_type>
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200207static inline bool DoIGetQuick(ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
208 Object* obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200209 if (UNLIKELY(obj == nullptr)) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200210 // We lost the reference to the field index so we cannot get a more
211 // precised exception message.
212 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetCurrentLocationForThrow());
213 return false;
214 }
215 MemberOffset field_offset(inst->VRegC_22c());
216 const bool is_volatile = false; // iget-x-quick only on non volatile fields.
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200217 const uint32_t vregA = inst->VRegA_22c(inst_data);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200218 switch (field_type) {
219 case Primitive::kPrimInt:
220 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetField32(field_offset, is_volatile)));
221 break;
222 case Primitive::kPrimLong:
223 shadow_frame.SetVRegLong(vregA, static_cast<int64_t>(obj->GetField64(field_offset, is_volatile)));
224 break;
225 case Primitive::kPrimNot:
226 shadow_frame.SetVRegReference(vregA, obj->GetFieldObject<mirror::Object*>(field_offset, is_volatile));
227 break;
228 default:
229 LOG(FATAL) << "Unreachable: " << field_type;
230 }
231 return true;
232}
233
Sebastien Hertzc6714852013-09-30 16:42:32 +0200234// Handles iput-XXX and sput-XXX instructions.
235// Returns true on success, otherwise throws an exception and returns false.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200236template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check>
237static inline bool DoFieldPut(Thread* self, const ShadowFrame& shadow_frame,
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200238 const Instruction* inst, uint16_t inst_data) {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700239 bool do_assignability_check = do_access_check;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200240 bool is_static = (find_type == StaticObjectWrite) || (find_type == StaticPrimitiveWrite);
241 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200242 ArtField* f = FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
243 Primitive::FieldSize(field_type));
244 if (UNLIKELY(f == nullptr)) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200245 CHECK(self->IsExceptionPending());
246 return false;
247 }
248 Object* obj;
249 if (is_static) {
250 obj = f->GetDeclaringClass();
251 } else {
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200252 obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200253 if (UNLIKELY(obj == nullptr)) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200254 ThrowNullPointerExceptionForFieldAccess(shadow_frame.GetCurrentLocationForThrow(),
255 f, false);
256 return false;
257 }
258 }
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200259 uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200260 switch (field_type) {
261 case Primitive::kPrimBoolean:
262 f->SetBoolean(obj, shadow_frame.GetVReg(vregA));
263 break;
264 case Primitive::kPrimByte:
265 f->SetByte(obj, shadow_frame.GetVReg(vregA));
266 break;
267 case Primitive::kPrimChar:
268 f->SetChar(obj, shadow_frame.GetVReg(vregA));
269 break;
270 case Primitive::kPrimShort:
271 f->SetShort(obj, shadow_frame.GetVReg(vregA));
272 break;
273 case Primitive::kPrimInt:
274 f->SetInt(obj, shadow_frame.GetVReg(vregA));
275 break;
276 case Primitive::kPrimLong:
277 f->SetLong(obj, shadow_frame.GetVRegLong(vregA));
278 break;
Jeff Haoa3faaf42013-09-03 19:07:00 -0700279 case Primitive::kPrimNot: {
280 Object* reg = shadow_frame.GetVRegReference(vregA);
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200281 if (do_assignability_check && reg != nullptr) {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700282 Class* field_class = FieldHelper(f).GetType();
283 if (!reg->VerifierInstanceOf(field_class)) {
284 // This should never happen.
285 self->ThrowNewExceptionF(self->GetCurrentLocationForThrow(),
286 "Ljava/lang/VirtualMachineError;",
287 "Put '%s' that is not instance of field '%s' in '%s'",
288 ClassHelper(reg->GetClass()).GetDescriptor(),
289 ClassHelper(field_class).GetDescriptor(),
290 ClassHelper(f->GetDeclaringClass()).GetDescriptor());
291 return false;
292 }
293 }
294 f->SetObj(obj, reg);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200295 break;
Jeff Haoa3faaf42013-09-03 19:07:00 -0700296 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200297 default:
298 LOG(FATAL) << "Unreachable: " << field_type;
299 }
300 return true;
301}
302
Sebastien Hertzc6714852013-09-30 16:42:32 +0200303// Handles iput-quick, iput-wide-quick and iput-object-quick instructions.
304// Returns true on success, otherwise throws an exception and returns false.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200305template<Primitive::Type field_type>
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200306static inline bool DoIPutQuick(const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200307 Object* obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200308 if (UNLIKELY(obj == nullptr)) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200309 // We lost the reference to the field index so we cannot get a more
310 // precised exception message.
311 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetCurrentLocationForThrow());
312 return false;
313 }
314 MemberOffset field_offset(inst->VRegC_22c());
315 const bool is_volatile = false; // iput-x-quick only on non volatile fields.
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200316 const uint32_t vregA = inst->VRegA_22c(inst_data);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200317 switch (field_type) {
318 case Primitive::kPrimInt:
319 obj->SetField32(field_offset, shadow_frame.GetVReg(vregA), is_volatile);
320 break;
321 case Primitive::kPrimLong:
322 obj->SetField64(field_offset, shadow_frame.GetVRegLong(vregA), is_volatile);
323 break;
324 case Primitive::kPrimNot:
325 obj->SetFieldObject(field_offset, shadow_frame.GetVRegReference(vregA), is_volatile);
326 break;
327 default:
328 LOG(FATAL) << "Unreachable: " << field_type;
329 }
330 return true;
331}
332
Sebastien Hertzc6714852013-09-30 16:42:32 +0200333// Handles string resolution for const-string and const-string-jumbo instructions. Also ensures the
334// java.lang.String class is initialized.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200335static inline String* ResolveString(Thread* self, MethodHelper& mh, uint32_t string_idx)
336 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800337 CHECK(!kMovingMethods);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200338 Class* java_lang_string_class = String::GetJavaLangString();
339 if (UNLIKELY(!java_lang_string_class->IsInitialized())) {
340 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800341 SirtRef<mirror::Class> sirt_class(self, java_lang_string_class);
342 if (UNLIKELY(!class_linker->EnsureInitialized(sirt_class, true, true))) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200343 DCHECK(self->IsExceptionPending());
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800344 return nullptr;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200345 }
346 }
347 return mh.ResolveString(string_idx);
348}
349
Sebastien Hertzc6714852013-09-30 16:42:32 +0200350// Handles div-int, div-int/2addr, div-int/li16 and div-int/lit8 instructions.
351// Returns true on success, otherwise throws a java.lang.ArithmeticException and return false.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200352static inline bool DoIntDivide(ShadowFrame& shadow_frame, size_t result_reg,
353 int32_t dividend, int32_t divisor)
354 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2e2deeb2013-09-23 11:58:57 -0700355 const int32_t kMinInt = std::numeric_limits<int32_t>::min();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200356 if (UNLIKELY(divisor == 0)) {
357 ThrowArithmeticExceptionDivideByZero();
358 return false;
359 }
360 if (UNLIKELY(dividend == kMinInt && divisor == -1)) {
361 shadow_frame.SetVReg(result_reg, kMinInt);
362 } else {
363 shadow_frame.SetVReg(result_reg, dividend / divisor);
364 }
365 return true;
366}
367
Sebastien Hertzc6714852013-09-30 16:42:32 +0200368// Handles rem-int, rem-int/2addr, rem-int/li16 and rem-int/lit8 instructions.
369// Returns true on success, otherwise throws a java.lang.ArithmeticException and return false.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200370static inline bool DoIntRemainder(ShadowFrame& shadow_frame, size_t result_reg,
371 int32_t dividend, int32_t divisor)
372 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2e2deeb2013-09-23 11:58:57 -0700373 const int32_t kMinInt = std::numeric_limits<int32_t>::min();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200374 if (UNLIKELY(divisor == 0)) {
375 ThrowArithmeticExceptionDivideByZero();
376 return false;
377 }
378 if (UNLIKELY(dividend == kMinInt && divisor == -1)) {
379 shadow_frame.SetVReg(result_reg, 0);
380 } else {
381 shadow_frame.SetVReg(result_reg, dividend % divisor);
382 }
383 return true;
384}
385
Sebastien Hertzc6714852013-09-30 16:42:32 +0200386// Handles div-long and div-long-2addr instructions.
387// Returns true on success, otherwise throws a java.lang.ArithmeticException and return false.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200388static inline bool DoLongDivide(ShadowFrame& shadow_frame, size_t result_reg,
389 int64_t dividend, int64_t divisor)
390 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2e2deeb2013-09-23 11:58:57 -0700391 const int64_t kMinLong = std::numeric_limits<int64_t>::min();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200392 if (UNLIKELY(divisor == 0)) {
393 ThrowArithmeticExceptionDivideByZero();
394 return false;
395 }
396 if (UNLIKELY(dividend == kMinLong && divisor == -1)) {
397 shadow_frame.SetVRegLong(result_reg, kMinLong);
398 } else {
399 shadow_frame.SetVRegLong(result_reg, dividend / divisor);
400 }
401 return true;
402}
403
Sebastien Hertzc6714852013-09-30 16:42:32 +0200404// Handles rem-long and rem-long-2addr instructions.
405// Returns true on success, otherwise throws a java.lang.ArithmeticException and return false.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200406static inline bool DoLongRemainder(ShadowFrame& shadow_frame, size_t result_reg,
407 int64_t dividend, int64_t divisor)
408 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2e2deeb2013-09-23 11:58:57 -0700409 const int64_t kMinLong = std::numeric_limits<int64_t>::min();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200410 if (UNLIKELY(divisor == 0)) {
411 ThrowArithmeticExceptionDivideByZero();
412 return false;
413 }
414 if (UNLIKELY(dividend == kMinLong && divisor == -1)) {
415 shadow_frame.SetVRegLong(result_reg, 0);
416 } else {
417 shadow_frame.SetVRegLong(result_reg, dividend % divisor);
418 }
419 return true;
420}
421
Sebastien Hertzc6714852013-09-30 16:42:32 +0200422// Handles filled-new-array and filled-new-array-range instructions.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200423// Returns true on success, otherwise throws an exception and returns false.
424template <bool is_range, bool do_access_check>
425bool DoFilledNewArray(const Instruction* inst, const ShadowFrame& shadow_frame,
Sebastien Hertzc6714852013-09-30 16:42:32 +0200426 Thread* self, JValue* result);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200427
Sebastien Hertzc6714852013-09-30 16:42:32 +0200428// Handles packed-switch instruction.
429// Returns the branch offset to the next instruction to execute.
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200430static inline int32_t DoPackedSwitch(const Instruction* inst, const ShadowFrame& shadow_frame,
431 uint16_t inst_data)
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200432 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
433 DCHECK(inst->Opcode() == Instruction::PACKED_SWITCH);
434 const uint16_t* switch_data = reinterpret_cast<const uint16_t*>(inst) + inst->VRegB_31t();
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200435 int32_t test_val = shadow_frame.GetVReg(inst->VRegA_31t(inst_data));
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200436 DCHECK_EQ(switch_data[0], static_cast<uint16_t>(Instruction::kPackedSwitchSignature));
437 uint16_t size = switch_data[1];
438 DCHECK_GT(size, 0);
439 const int32_t* keys = reinterpret_cast<const int32_t*>(&switch_data[2]);
440 DCHECK(IsAligned<4>(keys));
441 int32_t first_key = keys[0];
442 const int32_t* targets = reinterpret_cast<const int32_t*>(&switch_data[4]);
443 DCHECK(IsAligned<4>(targets));
444 int32_t index = test_val - first_key;
445 if (index >= 0 && index < size) {
446 return targets[index];
447 } else {
448 // No corresponding value: move forward by 3 (size of PACKED_SWITCH).
449 return 3;
450 }
451}
452
Sebastien Hertzc6714852013-09-30 16:42:32 +0200453// Handles sparse-switch instruction.
454// Returns the branch offset to the next instruction to execute.
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200455static inline int32_t DoSparseSwitch(const Instruction* inst, const ShadowFrame& shadow_frame,
456 uint16_t inst_data)
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200457 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
458 DCHECK(inst->Opcode() == Instruction::SPARSE_SWITCH);
459 const uint16_t* switch_data = reinterpret_cast<const uint16_t*>(inst) + inst->VRegB_31t();
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200460 int32_t test_val = shadow_frame.GetVReg(inst->VRegA_31t(inst_data));
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200461 DCHECK_EQ(switch_data[0], static_cast<uint16_t>(Instruction::kSparseSwitchSignature));
462 uint16_t size = switch_data[1];
463 DCHECK_GT(size, 0);
464 const int32_t* keys = reinterpret_cast<const int32_t*>(&switch_data[2]);
465 DCHECK(IsAligned<4>(keys));
466 const int32_t* entries = keys + size;
467 DCHECK(IsAligned<4>(entries));
468 int lo = 0;
469 int hi = size - 1;
470 while (lo <= hi) {
471 int mid = (lo + hi) / 2;
472 int32_t foundVal = keys[mid];
473 if (test_val < foundVal) {
474 hi = mid - 1;
475 } else if (test_val > foundVal) {
476 lo = mid + 1;
477 } else {
478 return entries[mid];
479 }
480 }
481 // No corresponding value: move forward by 3 (size of SPARSE_SWITCH).
482 return 3;
483}
484
485static inline uint32_t FindNextInstructionFollowingException(Thread* self,
486 ShadowFrame& shadow_frame,
487 uint32_t dex_pc,
Sebastien Hertz947ff082013-09-17 14:10:13 +0200488 mirror::Object* this_object,
489 const instrumentation::Instrumentation* instrumentation)
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200490 ALWAYS_INLINE;
491
492static inline uint32_t FindNextInstructionFollowingException(Thread* self,
493 ShadowFrame& shadow_frame,
494 uint32_t dex_pc,
Sebastien Hertz947ff082013-09-17 14:10:13 +0200495 mirror::Object* this_object,
496 const instrumentation::Instrumentation* instrumentation)
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200497 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
498 self->VerifyStack();
499 ThrowLocation throw_location;
500 mirror::Throwable* exception = self->GetException(&throw_location);
Sebastien Hertz947ff082013-09-17 14:10:13 +0200501 bool clear_exception = false;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200502 uint32_t found_dex_pc = shadow_frame.GetMethod()->FindCatchBlock(exception->GetClass(), dex_pc,
503 &clear_exception);
504 if (found_dex_pc == DexFile::kDexNoIndex) {
Sebastien Hertz947ff082013-09-17 14:10:13 +0200505 instrumentation->MethodUnwindEvent(self, this_object,
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200506 shadow_frame.GetMethod(), dex_pc);
507 } else {
508 instrumentation->ExceptionCaughtEvent(self, throw_location,
509 shadow_frame.GetMethod(),
510 found_dex_pc, exception);
511 if (clear_exception) {
512 self->ClearException();
513 }
514 }
515 return found_dex_pc;
516}
517
518static void UnexpectedOpcode(const Instruction* inst, MethodHelper& mh)
519 __attribute__((cold, noreturn, noinline));
520
521static void UnexpectedOpcode(const Instruction* inst, MethodHelper& mh)
522 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
523 LOG(FATAL) << "Unexpected instruction: " << inst->DumpString(&mh.GetDexFile());
524 exit(0); // Unreachable, keep GCC happy.
525}
526
527static inline void TraceExecution(const ShadowFrame& shadow_frame, const Instruction* inst,
Jeff Haoa3faaf42013-09-03 19:07:00 -0700528 const uint32_t dex_pc, MethodHelper& mh)
529 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700530 constexpr bool kTracing = false;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200531 if (kTracing) {
532#define TRACE_LOG std::cerr
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700533 std::ostringstream oss;
534 oss << PrettyMethod(shadow_frame.GetMethod())
535 << StringPrintf("\n0x%x: ", dex_pc)
536 << inst->DumpString(&mh.GetDexFile()) << "\n";
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200537 for (size_t i = 0; i < shadow_frame.NumberOfVRegs(); ++i) {
538 uint32_t raw_value = shadow_frame.GetVReg(i);
539 Object* ref_value = shadow_frame.GetVRegReference(i);
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700540 oss << StringPrintf(" vreg%d=0x%08X", i, raw_value);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200541 if (ref_value != NULL) {
542 if (ref_value->GetClass()->IsStringClass() &&
543 ref_value->AsString()->GetCharArray() != NULL) {
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700544 oss << "/java.lang.String \"" << ref_value->AsString()->ToModifiedUtf8() << "\"";
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200545 } else {
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700546 oss << "/" << PrettyTypeOf(ref_value);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200547 }
548 }
549 }
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700550 TRACE_LOG << oss.str() << "\n";
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200551#undef TRACE_LOG
552 }
553}
554
Sebastien Hertz1eda2262013-09-09 16:53:14 +0200555static inline bool IsBackwardBranch(int32_t branch_offset) {
556 return branch_offset <= 0;
557}
558
Sebastien Hertzc6714852013-09-30 16:42:32 +0200559// Explicitly instantiate all DoInvoke functions.
560#define EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, _is_range, _do_check) \
561 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) ALWAYS_INLINE \
562 static bool DoInvoke<_type, _is_range, _do_check>(Thread* self, ShadowFrame& shadow_frame, \
563 const Instruction* inst, uint16_t inst_data, \
564 JValue* result)
565
566#define EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(_type) \
567 EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, false, false); \
568 EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, false, true); \
569 EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, true, false); \
570 EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, true, true);
571
572EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(kStatic); // invoke-static/range.
573EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(kDirect); // invoke-direct/range.
574EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(kVirtual); // invoke-virtual/range.
575EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(kSuper); // invoke-super/range.
576EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(kInterface); // invoke-interface/range.
577#undef EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL
578#undef EXPLICIT_DO_INVOKE_TEMPLATE_DECL
579
580// Explicitly instantiate all DoFieldGet functions.
581#define EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, _do_check) \
582 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) ALWAYS_INLINE \
583 static bool DoFieldGet<_find_type, _field_type, _do_check>(Thread* self, ShadowFrame& shadow_frame, \
584 const Instruction* inst, uint16_t inst_data)
585
586#define EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(_find_type, _field_type) \
587 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, false); \
588 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, true);
589
590// iget-XXX
591EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimBoolean);
592EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimByte);
593EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimChar);
594EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimShort);
595EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimInt);
596EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimLong);
597EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstanceObjectRead, Primitive::kPrimNot);
598
599// sget-XXX
600EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimBoolean);
601EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimByte);
602EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimChar);
603EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimShort);
604EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimInt);
605EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimLong);
606EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticObjectRead, Primitive::kPrimNot);
607
608#undef EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL
609#undef EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL
610
611// Explicitly instantiate all DoFieldPut functions.
612#define EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, _do_check) \
613 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) ALWAYS_INLINE \
614 static bool DoFieldPut<_find_type, _field_type, _do_check>(Thread* self, const ShadowFrame& shadow_frame, \
615 const Instruction* inst, uint16_t inst_data)
616
617#define EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(_find_type, _field_type) \
618 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false); \
619 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true);
620
621// iput-XXX
622EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimBoolean);
623EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimByte);
624EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimChar);
625EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimShort);
626EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimInt);
627EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimLong);
628EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstanceObjectWrite, Primitive::kPrimNot);
629
630// sput-XXX
631EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimBoolean);
632EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimByte);
633EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimChar);
634EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimShort);
635EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimInt);
636EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimLong);
637EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticObjectWrite, Primitive::kPrimNot);
638
639#undef EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL
640#undef EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL
641
642// Explicitly instantiate all DoInvokeVirtualQuick functions.
643#define EXPLICIT_DO_INVOKE_VIRTUAL_QUICK_TEMPLATE_DECL(_is_range) \
644 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) ALWAYS_INLINE \
645 static bool DoInvokeVirtualQuick<_is_range>(Thread* self, ShadowFrame& shadow_frame, \
646 const Instruction* inst, uint16_t inst_data, \
647 JValue* result)
648
649EXPLICIT_DO_INVOKE_VIRTUAL_QUICK_TEMPLATE_DECL(false); // invoke-virtual-quick.
650EXPLICIT_DO_INVOKE_VIRTUAL_QUICK_TEMPLATE_DECL(true); // invoke-virtual-quick-range.
651#undef EXPLICIT_INSTANTIATION_DO_INVOKE_VIRTUAL_QUICK
652
653// Explicitly instantiate all DoIGetQuick functions.
654#define EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(_field_type) \
655 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) ALWAYS_INLINE \
656 static bool DoIGetQuick<_field_type>(ShadowFrame& shadow_frame, const Instruction* inst, \
657 uint16_t inst_data)
658
659EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimInt); // iget-quick.
660EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimLong); // iget-wide-quick.
661EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimNot); // iget-object-quick.
662#undef EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL
663
664// Explicitly instantiate all DoIPutQuick functions.
665#define EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type) \
666 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) ALWAYS_INLINE \
667 static bool DoIPutQuick<_field_type>(const ShadowFrame& shadow_frame, const Instruction* inst, \
668 uint16_t inst_data)
669
670EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(Primitive::kPrimInt); // iget-quick.
671EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(Primitive::kPrimLong); // iget-wide-quick.
672EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(Primitive::kPrimNot); // iget-object-quick.
673#undef EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL
674
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200675} // namespace interpreter
676} // namespace art
677
678#endif // ART_RUNTIME_INTERPRETER_INTERPRETER_COMMON_H_