blob: 768ca336db076b6835c72a6cd6e36d6b5210d300 [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>
Sebastien Hertz9119c5f2013-12-16 11:31:45 +010090bool DoCall(ArtMethod* method, 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);
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200104 if (UNLIKELY(method == nullptr)) {
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200105 CHECK(self->IsExceptionPending());
106 result->SetJ(0);
107 return false;
108 } else if (UNLIKELY(method->IsAbstract())) {
109 ThrowAbstractMethodError(method);
110 result->SetJ(0);
111 return false;
112 } else {
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100113 return DoCall<is_range, do_access_check>(method, self, shadow_frame, inst, inst_data, result);
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200114 }
115}
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200116
Sebastien Hertzc6714852013-09-30 16:42:32 +0200117// Handles invoke-virtual-quick and invoke-virtual-quick-range instructions.
118// Returns true on success, otherwise throws an exception and returns false.
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200119template<bool is_range>
120static inline bool DoInvokeVirtualQuick(Thread* self, ShadowFrame& shadow_frame,
121 const Instruction* inst, uint16_t inst_data,
122 JValue* result) {
123 const uint32_t vregC = (is_range) ? inst->VRegC_3rc() : inst->VRegC_35c();
124 Object* const receiver = shadow_frame.GetVRegReference(vregC);
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200125 if (UNLIKELY(receiver == nullptr)) {
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200126 // We lost the reference to the method index so we cannot get a more
127 // precised exception message.
128 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetCurrentLocationForThrow());
129 return false;
130 }
131 const uint32_t vtable_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
132 ArtMethod* const method = receiver->GetClass()->GetVTable()->GetWithoutChecks(vtable_idx);
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200133 if (UNLIKELY(method == nullptr)) {
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200134 CHECK(self->IsExceptionPending());
135 result->SetJ(0);
136 return false;
137 } else if (UNLIKELY(method->IsAbstract())) {
138 ThrowAbstractMethodError(method);
139 result->SetJ(0);
140 return false;
141 } else {
142 // No need to check since we've been quickened.
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100143 return DoCall<is_range, false>(method, self, shadow_frame, inst, inst_data, result);
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200144 }
145}
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200146
Sebastien Hertzc6714852013-09-30 16:42:32 +0200147// Handles iget-XXX and sget-XXX instructions.
148// Returns true on success, otherwise throws an exception and returns false.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200149template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check>
150static inline bool DoFieldGet(Thread* self, ShadowFrame& shadow_frame,
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200151 const Instruction* inst, uint16_t inst_data) {
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200152 const bool is_static = (find_type == StaticObjectRead) || (find_type == StaticPrimitiveRead);
153 const uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
154 ArtField* f = FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
155 Primitive::FieldSize(field_type));
156 if (UNLIKELY(f == nullptr)) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200157 CHECK(self->IsExceptionPending());
158 return false;
159 }
160 Object* obj;
161 if (is_static) {
162 obj = f->GetDeclaringClass();
163 } else {
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200164 obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200165 if (UNLIKELY(obj == nullptr)) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200166 ThrowNullPointerExceptionForFieldAccess(shadow_frame.GetCurrentLocationForThrow(), f, true);
167 return false;
168 }
169 }
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200170 uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200171 switch (field_type) {
172 case Primitive::kPrimBoolean:
173 shadow_frame.SetVReg(vregA, f->GetBoolean(obj));
174 break;
175 case Primitive::kPrimByte:
176 shadow_frame.SetVReg(vregA, f->GetByte(obj));
177 break;
178 case Primitive::kPrimChar:
179 shadow_frame.SetVReg(vregA, f->GetChar(obj));
180 break;
181 case Primitive::kPrimShort:
182 shadow_frame.SetVReg(vregA, f->GetShort(obj));
183 break;
184 case Primitive::kPrimInt:
185 shadow_frame.SetVReg(vregA, f->GetInt(obj));
186 break;
187 case Primitive::kPrimLong:
188 shadow_frame.SetVRegLong(vregA, f->GetLong(obj));
189 break;
190 case Primitive::kPrimNot:
191 shadow_frame.SetVRegReference(vregA, f->GetObject(obj));
192 break;
193 default:
194 LOG(FATAL) << "Unreachable: " << field_type;
195 }
196 return true;
197}
198
Sebastien Hertzc6714852013-09-30 16:42:32 +0200199// Handles iget-quick, iget-wide-quick and iget-object-quick instructions.
200// Returns true on success, otherwise throws an exception and returns false.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200201template<Primitive::Type field_type>
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200202static inline bool DoIGetQuick(ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
203 Object* obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200204 if (UNLIKELY(obj == nullptr)) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200205 // We lost the reference to the field index so we cannot get a more
206 // precised exception message.
207 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetCurrentLocationForThrow());
208 return false;
209 }
210 MemberOffset field_offset(inst->VRegC_22c());
211 const bool is_volatile = false; // iget-x-quick only on non volatile fields.
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200212 const uint32_t vregA = inst->VRegA_22c(inst_data);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200213 switch (field_type) {
214 case Primitive::kPrimInt:
215 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetField32(field_offset, is_volatile)));
216 break;
217 case Primitive::kPrimLong:
218 shadow_frame.SetVRegLong(vregA, static_cast<int64_t>(obj->GetField64(field_offset, is_volatile)));
219 break;
220 case Primitive::kPrimNot:
Ian Rogersef7d42f2014-01-06 12:55:46 -0800221 shadow_frame.SetVRegReference(vregA, obj->GetFieldObject<mirror::Object>(field_offset, is_volatile));
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200222 break;
223 default:
224 LOG(FATAL) << "Unreachable: " << field_type;
225 }
226 return true;
227}
228
Sebastien Hertzc6714852013-09-30 16:42:32 +0200229// Handles iput-XXX and sput-XXX instructions.
230// Returns true on success, otherwise throws an exception and returns false.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200231template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check>
232static inline bool DoFieldPut(Thread* self, const ShadowFrame& shadow_frame,
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200233 const Instruction* inst, uint16_t inst_data) {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700234 bool do_assignability_check = do_access_check;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200235 bool is_static = (find_type == StaticObjectWrite) || (find_type == StaticPrimitiveWrite);
236 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200237 ArtField* f = FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
238 Primitive::FieldSize(field_type));
239 if (UNLIKELY(f == nullptr)) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200240 CHECK(self->IsExceptionPending());
241 return false;
242 }
243 Object* obj;
244 if (is_static) {
245 obj = f->GetDeclaringClass();
246 } else {
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200247 obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200248 if (UNLIKELY(obj == nullptr)) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200249 ThrowNullPointerExceptionForFieldAccess(shadow_frame.GetCurrentLocationForThrow(),
250 f, false);
251 return false;
252 }
253 }
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200254 uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200255 switch (field_type) {
256 case Primitive::kPrimBoolean:
257 f->SetBoolean(obj, shadow_frame.GetVReg(vregA));
258 break;
259 case Primitive::kPrimByte:
260 f->SetByte(obj, shadow_frame.GetVReg(vregA));
261 break;
262 case Primitive::kPrimChar:
263 f->SetChar(obj, shadow_frame.GetVReg(vregA));
264 break;
265 case Primitive::kPrimShort:
266 f->SetShort(obj, shadow_frame.GetVReg(vregA));
267 break;
268 case Primitive::kPrimInt:
269 f->SetInt(obj, shadow_frame.GetVReg(vregA));
270 break;
271 case Primitive::kPrimLong:
272 f->SetLong(obj, shadow_frame.GetVRegLong(vregA));
273 break;
Jeff Haoa3faaf42013-09-03 19:07:00 -0700274 case Primitive::kPrimNot: {
275 Object* reg = shadow_frame.GetVRegReference(vregA);
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200276 if (do_assignability_check && reg != nullptr) {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700277 Class* field_class = FieldHelper(f).GetType();
278 if (!reg->VerifierInstanceOf(field_class)) {
279 // This should never happen.
280 self->ThrowNewExceptionF(self->GetCurrentLocationForThrow(),
281 "Ljava/lang/VirtualMachineError;",
282 "Put '%s' that is not instance of field '%s' in '%s'",
283 ClassHelper(reg->GetClass()).GetDescriptor(),
284 ClassHelper(field_class).GetDescriptor(),
285 ClassHelper(f->GetDeclaringClass()).GetDescriptor());
286 return false;
287 }
288 }
289 f->SetObj(obj, reg);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200290 break;
Jeff Haoa3faaf42013-09-03 19:07:00 -0700291 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200292 default:
293 LOG(FATAL) << "Unreachable: " << field_type;
294 }
295 return true;
296}
297
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 Hertz8ece0502013-08-07 11:26:41 +0200300template<Primitive::Type field_type>
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200301static inline bool DoIPutQuick(const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200302 Object* obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200303 if (UNLIKELY(obj == nullptr)) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200304 // We lost the reference to the field index so we cannot get a more
305 // precised exception message.
306 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetCurrentLocationForThrow());
307 return false;
308 }
309 MemberOffset field_offset(inst->VRegC_22c());
310 const bool is_volatile = false; // iput-x-quick only on non volatile fields.
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200311 const uint32_t vregA = inst->VRegA_22c(inst_data);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200312 switch (field_type) {
313 case Primitive::kPrimInt:
314 obj->SetField32(field_offset, shadow_frame.GetVReg(vregA), is_volatile);
315 break;
316 case Primitive::kPrimLong:
317 obj->SetField64(field_offset, shadow_frame.GetVRegLong(vregA), is_volatile);
318 break;
319 case Primitive::kPrimNot:
320 obj->SetFieldObject(field_offset, shadow_frame.GetVRegReference(vregA), is_volatile);
321 break;
322 default:
323 LOG(FATAL) << "Unreachable: " << field_type;
324 }
325 return true;
326}
327
Sebastien Hertzc6714852013-09-30 16:42:32 +0200328// Handles string resolution for const-string and const-string-jumbo instructions. Also ensures the
329// java.lang.String class is initialized.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200330static inline String* ResolveString(Thread* self, MethodHelper& mh, uint32_t string_idx)
331 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800332 CHECK(!kMovingMethods);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200333 Class* java_lang_string_class = String::GetJavaLangString();
334 if (UNLIKELY(!java_lang_string_class->IsInitialized())) {
335 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800336 SirtRef<mirror::Class> sirt_class(self, java_lang_string_class);
337 if (UNLIKELY(!class_linker->EnsureInitialized(sirt_class, true, true))) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200338 DCHECK(self->IsExceptionPending());
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800339 return nullptr;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200340 }
341 }
342 return mh.ResolveString(string_idx);
343}
344
Sebastien Hertzc6714852013-09-30 16:42:32 +0200345// Handles div-int, div-int/2addr, div-int/li16 and div-int/lit8 instructions.
346// Returns true on success, otherwise throws a java.lang.ArithmeticException and return false.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200347static inline bool DoIntDivide(ShadowFrame& shadow_frame, size_t result_reg,
348 int32_t dividend, int32_t divisor)
349 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2e2deeb2013-09-23 11:58:57 -0700350 const int32_t kMinInt = std::numeric_limits<int32_t>::min();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200351 if (UNLIKELY(divisor == 0)) {
352 ThrowArithmeticExceptionDivideByZero();
353 return false;
354 }
355 if (UNLIKELY(dividend == kMinInt && divisor == -1)) {
356 shadow_frame.SetVReg(result_reg, kMinInt);
357 } else {
358 shadow_frame.SetVReg(result_reg, dividend / divisor);
359 }
360 return true;
361}
362
Sebastien Hertzc6714852013-09-30 16:42:32 +0200363// Handles rem-int, rem-int/2addr, rem-int/li16 and rem-int/lit8 instructions.
364// Returns true on success, otherwise throws a java.lang.ArithmeticException and return false.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200365static inline bool DoIntRemainder(ShadowFrame& shadow_frame, size_t result_reg,
366 int32_t dividend, int32_t divisor)
367 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2e2deeb2013-09-23 11:58:57 -0700368 const int32_t kMinInt = std::numeric_limits<int32_t>::min();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200369 if (UNLIKELY(divisor == 0)) {
370 ThrowArithmeticExceptionDivideByZero();
371 return false;
372 }
373 if (UNLIKELY(dividend == kMinInt && divisor == -1)) {
374 shadow_frame.SetVReg(result_reg, 0);
375 } else {
376 shadow_frame.SetVReg(result_reg, dividend % divisor);
377 }
378 return true;
379}
380
Sebastien Hertzc6714852013-09-30 16:42:32 +0200381// Handles div-long and div-long-2addr instructions.
382// Returns true on success, otherwise throws a java.lang.ArithmeticException and return false.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200383static inline bool DoLongDivide(ShadowFrame& shadow_frame, size_t result_reg,
384 int64_t dividend, int64_t divisor)
385 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2e2deeb2013-09-23 11:58:57 -0700386 const int64_t kMinLong = std::numeric_limits<int64_t>::min();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200387 if (UNLIKELY(divisor == 0)) {
388 ThrowArithmeticExceptionDivideByZero();
389 return false;
390 }
391 if (UNLIKELY(dividend == kMinLong && divisor == -1)) {
392 shadow_frame.SetVRegLong(result_reg, kMinLong);
393 } else {
394 shadow_frame.SetVRegLong(result_reg, dividend / divisor);
395 }
396 return true;
397}
398
Sebastien Hertzc6714852013-09-30 16:42:32 +0200399// Handles rem-long and rem-long-2addr instructions.
400// Returns true on success, otherwise throws a java.lang.ArithmeticException and return false.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200401static inline bool DoLongRemainder(ShadowFrame& shadow_frame, size_t result_reg,
402 int64_t dividend, int64_t divisor)
403 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2e2deeb2013-09-23 11:58:57 -0700404 const int64_t kMinLong = std::numeric_limits<int64_t>::min();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200405 if (UNLIKELY(divisor == 0)) {
406 ThrowArithmeticExceptionDivideByZero();
407 return false;
408 }
409 if (UNLIKELY(dividend == kMinLong && divisor == -1)) {
410 shadow_frame.SetVRegLong(result_reg, 0);
411 } else {
412 shadow_frame.SetVRegLong(result_reg, dividend % divisor);
413 }
414 return true;
415}
416
Sebastien Hertzc6714852013-09-30 16:42:32 +0200417// Handles filled-new-array and filled-new-array-range instructions.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200418// Returns true on success, otherwise throws an exception and returns false.
419template <bool is_range, bool do_access_check>
420bool DoFilledNewArray(const Instruction* inst, const ShadowFrame& shadow_frame,
Sebastien Hertzc6714852013-09-30 16:42:32 +0200421 Thread* self, JValue* result);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200422
Sebastien Hertzc6714852013-09-30 16:42:32 +0200423// Handles packed-switch instruction.
424// Returns the branch offset to the next instruction to execute.
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200425static inline int32_t DoPackedSwitch(const Instruction* inst, const ShadowFrame& shadow_frame,
426 uint16_t inst_data)
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200427 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
428 DCHECK(inst->Opcode() == Instruction::PACKED_SWITCH);
429 const uint16_t* switch_data = reinterpret_cast<const uint16_t*>(inst) + inst->VRegB_31t();
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200430 int32_t test_val = shadow_frame.GetVReg(inst->VRegA_31t(inst_data));
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200431 DCHECK_EQ(switch_data[0], static_cast<uint16_t>(Instruction::kPackedSwitchSignature));
432 uint16_t size = switch_data[1];
433 DCHECK_GT(size, 0);
434 const int32_t* keys = reinterpret_cast<const int32_t*>(&switch_data[2]);
435 DCHECK(IsAligned<4>(keys));
436 int32_t first_key = keys[0];
437 const int32_t* targets = reinterpret_cast<const int32_t*>(&switch_data[4]);
438 DCHECK(IsAligned<4>(targets));
439 int32_t index = test_val - first_key;
440 if (index >= 0 && index < size) {
441 return targets[index];
442 } else {
443 // No corresponding value: move forward by 3 (size of PACKED_SWITCH).
444 return 3;
445 }
446}
447
Sebastien Hertzc6714852013-09-30 16:42:32 +0200448// Handles sparse-switch instruction.
449// Returns the branch offset to the next instruction to execute.
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200450static inline int32_t DoSparseSwitch(const Instruction* inst, const ShadowFrame& shadow_frame,
451 uint16_t inst_data)
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200452 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
453 DCHECK(inst->Opcode() == Instruction::SPARSE_SWITCH);
454 const uint16_t* switch_data = reinterpret_cast<const uint16_t*>(inst) + inst->VRegB_31t();
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200455 int32_t test_val = shadow_frame.GetVReg(inst->VRegA_31t(inst_data));
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200456 DCHECK_EQ(switch_data[0], static_cast<uint16_t>(Instruction::kSparseSwitchSignature));
457 uint16_t size = switch_data[1];
458 DCHECK_GT(size, 0);
459 const int32_t* keys = reinterpret_cast<const int32_t*>(&switch_data[2]);
460 DCHECK(IsAligned<4>(keys));
461 const int32_t* entries = keys + size;
462 DCHECK(IsAligned<4>(entries));
463 int lo = 0;
464 int hi = size - 1;
465 while (lo <= hi) {
466 int mid = (lo + hi) / 2;
467 int32_t foundVal = keys[mid];
468 if (test_val < foundVal) {
469 hi = mid - 1;
470 } else if (test_val > foundVal) {
471 lo = mid + 1;
472 } else {
473 return entries[mid];
474 }
475 }
476 // No corresponding value: move forward by 3 (size of SPARSE_SWITCH).
477 return 3;
478}
479
480static inline uint32_t FindNextInstructionFollowingException(Thread* self,
481 ShadowFrame& shadow_frame,
482 uint32_t dex_pc,
Sebastien Hertz947ff082013-09-17 14:10:13 +0200483 mirror::Object* this_object,
484 const instrumentation::Instrumentation* instrumentation)
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200485 ALWAYS_INLINE;
486
487static inline uint32_t FindNextInstructionFollowingException(Thread* self,
488 ShadowFrame& shadow_frame,
489 uint32_t dex_pc,
Sebastien Hertz947ff082013-09-17 14:10:13 +0200490 mirror::Object* this_object,
491 const instrumentation::Instrumentation* instrumentation)
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200492 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
493 self->VerifyStack();
494 ThrowLocation throw_location;
495 mirror::Throwable* exception = self->GetException(&throw_location);
Sebastien Hertz947ff082013-09-17 14:10:13 +0200496 bool clear_exception = false;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200497 uint32_t found_dex_pc = shadow_frame.GetMethod()->FindCatchBlock(exception->GetClass(), dex_pc,
498 &clear_exception);
499 if (found_dex_pc == DexFile::kDexNoIndex) {
Sebastien Hertz947ff082013-09-17 14:10:13 +0200500 instrumentation->MethodUnwindEvent(self, this_object,
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200501 shadow_frame.GetMethod(), dex_pc);
502 } else {
503 instrumentation->ExceptionCaughtEvent(self, throw_location,
504 shadow_frame.GetMethod(),
505 found_dex_pc, exception);
506 if (clear_exception) {
507 self->ClearException();
508 }
509 }
510 return found_dex_pc;
511}
512
513static void UnexpectedOpcode(const Instruction* inst, MethodHelper& mh)
514 __attribute__((cold, noreturn, noinline));
515
516static void UnexpectedOpcode(const Instruction* inst, MethodHelper& mh)
517 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
518 LOG(FATAL) << "Unexpected instruction: " << inst->DumpString(&mh.GetDexFile());
519 exit(0); // Unreachable, keep GCC happy.
520}
521
522static inline void TraceExecution(const ShadowFrame& shadow_frame, const Instruction* inst,
Jeff Haoa3faaf42013-09-03 19:07:00 -0700523 const uint32_t dex_pc, MethodHelper& mh)
524 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700525 constexpr bool kTracing = false;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200526 if (kTracing) {
527#define TRACE_LOG std::cerr
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700528 std::ostringstream oss;
529 oss << PrettyMethod(shadow_frame.GetMethod())
530 << StringPrintf("\n0x%x: ", dex_pc)
531 << inst->DumpString(&mh.GetDexFile()) << "\n";
Ian Rogersef7d42f2014-01-06 12:55:46 -0800532 for (uint32_t i = 0; i < shadow_frame.NumberOfVRegs(); ++i) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200533 uint32_t raw_value = shadow_frame.GetVReg(i);
534 Object* ref_value = shadow_frame.GetVRegReference(i);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800535 oss << StringPrintf(" vreg%u=0x%08X", i, raw_value);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200536 if (ref_value != NULL) {
537 if (ref_value->GetClass()->IsStringClass() &&
538 ref_value->AsString()->GetCharArray() != NULL) {
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700539 oss << "/java.lang.String \"" << ref_value->AsString()->ToModifiedUtf8() << "\"";
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200540 } else {
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700541 oss << "/" << PrettyTypeOf(ref_value);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200542 }
543 }
544 }
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700545 TRACE_LOG << oss.str() << "\n";
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200546#undef TRACE_LOG
547 }
548}
549
Sebastien Hertz1eda2262013-09-09 16:53:14 +0200550static inline bool IsBackwardBranch(int32_t branch_offset) {
551 return branch_offset <= 0;
552}
553
Sebastien Hertzc6714852013-09-30 16:42:32 +0200554// Explicitly instantiate all DoInvoke functions.
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100555#define EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, _is_range, _do_check) \
556 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) ALWAYS_INLINE \
557 bool DoInvoke<_type, _is_range, _do_check>(Thread* self, ShadowFrame& shadow_frame, \
558 const Instruction* inst, uint16_t inst_data, \
559 JValue* result)
Sebastien Hertzc6714852013-09-30 16:42:32 +0200560
561#define EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(_type) \
562 EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, false, false); \
563 EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, false, true); \
564 EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, true, false); \
565 EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, true, true);
566
567EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(kStatic); // invoke-static/range.
568EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(kDirect); // invoke-direct/range.
569EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(kVirtual); // invoke-virtual/range.
570EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(kSuper); // invoke-super/range.
571EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(kInterface); // invoke-interface/range.
572#undef EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL
573#undef EXPLICIT_DO_INVOKE_TEMPLATE_DECL
574
575// Explicitly instantiate all DoFieldGet functions.
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100576#define EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, _do_check) \
577 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) ALWAYS_INLINE \
578 bool DoFieldGet<_find_type, _field_type, _do_check>(Thread* self, ShadowFrame& shadow_frame, \
579 const Instruction* inst, uint16_t inst_data)
Sebastien Hertzc6714852013-09-30 16:42:32 +0200580
581#define EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(_find_type, _field_type) \
582 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, false); \
583 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, true);
584
585// iget-XXX
586EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimBoolean);
587EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimByte);
588EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimChar);
589EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimShort);
590EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimInt);
591EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimLong);
592EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstanceObjectRead, Primitive::kPrimNot);
593
594// sget-XXX
595EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimBoolean);
596EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimByte);
597EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimChar);
598EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimShort);
599EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimInt);
600EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimLong);
601EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticObjectRead, Primitive::kPrimNot);
602
603#undef EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL
604#undef EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL
605
606// Explicitly instantiate all DoFieldPut functions.
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100607#define EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, _do_check) \
608 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) ALWAYS_INLINE \
609 bool DoFieldPut<_find_type, _field_type, _do_check>(Thread* self, const ShadowFrame& shadow_frame, \
610 const Instruction* inst, uint16_t inst_data)
Sebastien Hertzc6714852013-09-30 16:42:32 +0200611
612#define EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(_find_type, _field_type) \
613 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false); \
614 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true);
615
616// iput-XXX
617EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimBoolean);
618EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimByte);
619EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimChar);
620EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimShort);
621EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimInt);
622EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimLong);
623EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstanceObjectWrite, Primitive::kPrimNot);
624
625// sput-XXX
626EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimBoolean);
627EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimByte);
628EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimChar);
629EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimShort);
630EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimInt);
631EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimLong);
632EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticObjectWrite, Primitive::kPrimNot);
633
634#undef EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL
635#undef EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL
636
637// Explicitly instantiate all DoInvokeVirtualQuick functions.
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100638#define EXPLICIT_DO_INVOKE_VIRTUAL_QUICK_TEMPLATE_DECL(_is_range) \
639 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) ALWAYS_INLINE \
640 bool DoInvokeVirtualQuick<_is_range>(Thread* self, ShadowFrame& shadow_frame, \
641 const Instruction* inst, uint16_t inst_data, \
642 JValue* result)
Sebastien Hertzc6714852013-09-30 16:42:32 +0200643
644EXPLICIT_DO_INVOKE_VIRTUAL_QUICK_TEMPLATE_DECL(false); // invoke-virtual-quick.
645EXPLICIT_DO_INVOKE_VIRTUAL_QUICK_TEMPLATE_DECL(true); // invoke-virtual-quick-range.
646#undef EXPLICIT_INSTANTIATION_DO_INVOKE_VIRTUAL_QUICK
647
648// Explicitly instantiate all DoIGetQuick functions.
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100649#define EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(_field_type) \
650 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) ALWAYS_INLINE \
651 bool DoIGetQuick<_field_type>(ShadowFrame& shadow_frame, const Instruction* inst, \
652 uint16_t inst_data)
Sebastien Hertzc6714852013-09-30 16:42:32 +0200653
654EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimInt); // iget-quick.
655EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimLong); // iget-wide-quick.
656EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimNot); // iget-object-quick.
657#undef EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL
658
659// Explicitly instantiate all DoIPutQuick functions.
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100660#define EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type) \
661 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) ALWAYS_INLINE \
662 bool DoIPutQuick<_field_type>(const ShadowFrame& shadow_frame, const Instruction* inst, \
663 uint16_t inst_data)
Sebastien Hertzc6714852013-09-30 16:42:32 +0200664
665EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(Primitive::kPrimInt); // iget-quick.
666EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(Primitive::kPrimLong); // iget-wide-quick.
667EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(Primitive::kPrimNot); // iget-object-quick.
668#undef EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL
669
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200670} // namespace interpreter
671} // namespace art
672
673#endif // ART_RUNTIME_INTERPRETER_INTERPRETER_COMMON_H_