Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1 | /* |
| 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" |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 32 | #include "handle_scope-inl.h" |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 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 | |
| 47 | using ::art::mirror::ArtField; |
| 48 | using ::art::mirror::ArtMethod; |
| 49 | using ::art::mirror::Array; |
| 50 | using ::art::mirror::BooleanArray; |
| 51 | using ::art::mirror::ByteArray; |
| 52 | using ::art::mirror::CharArray; |
| 53 | using ::art::mirror::Class; |
| 54 | using ::art::mirror::ClassLoader; |
| 55 | using ::art::mirror::IntArray; |
| 56 | using ::art::mirror::LongArray; |
| 57 | using ::art::mirror::Object; |
| 58 | using ::art::mirror::ObjectArray; |
| 59 | using ::art::mirror::ShortArray; |
| 60 | using ::art::mirror::String; |
| 61 | using ::art::mirror::Throwable; |
| 62 | |
Sebastien Hertz | 82aeddb | 2014-05-20 20:09:45 +0200 | [diff] [blame] | 63 | // b/14882674 Workaround stack overflow issue with clang |
| 64 | #if defined(__clang__) && defined(__aarch64__) |
| 65 | #define SOMETIMES_INLINE __attribute__((noinline)) |
| 66 | #define SOMETIMES_INLINE_KEYWORD |
| 67 | #else |
| 68 | #define SOMETIMES_INLINE ALWAYS_INLINE |
| 69 | #define SOMETIMES_INLINE_KEYWORD inline |
| 70 | #endif |
| 71 | |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 72 | namespace art { |
| 73 | namespace interpreter { |
| 74 | |
| 75 | // External references to both interpreter implementations. |
| 76 | |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 77 | template<bool do_access_check, bool transaction_active> |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 78 | extern JValue ExecuteSwitchImpl(Thread* self, MethodHelper& mh, |
| 79 | const DexFile::CodeItem* code_item, |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 80 | ShadowFrame& shadow_frame, JValue result_register); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 81 | |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 82 | template<bool do_access_check, bool transaction_active> |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 83 | extern JValue ExecuteGotoImpl(Thread* self, MethodHelper& mh, |
| 84 | const DexFile::CodeItem* code_item, |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 85 | ShadowFrame& shadow_frame, JValue result_register); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 86 | |
| 87 | static inline void DoMonitorEnter(Thread* self, Object* ref) NO_THREAD_SAFETY_ANALYSIS { |
| 88 | ref->MonitorEnter(self); |
| 89 | } |
| 90 | |
| 91 | static inline void DoMonitorExit(Thread* self, Object* ref) NO_THREAD_SAFETY_ANALYSIS { |
| 92 | ref->MonitorExit(self); |
| 93 | } |
| 94 | |
Mathieu Chartier | b2c7ead | 2014-04-29 11:13:16 -0700 | [diff] [blame] | 95 | void AbortTransaction(Thread* self, const char* fmt, ...) |
| 96 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 97 | |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 98 | void RecordArrayElementsInTransaction(mirror::Array* array, int32_t count) |
| 99 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 100 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 101 | // Invokes the given method. This is part of the invocation support and is used by DoInvoke and |
| 102 | // DoInvokeVirtualQuick functions. |
| 103 | // Returns true on success, otherwise throws an exception and returns false. |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 104 | template<bool is_range, bool do_assignability_check> |
Sebastien Hertz | 9119c5f | 2013-12-16 11:31:45 +0100 | [diff] [blame] | 105 | bool DoCall(ArtMethod* method, Thread* self, ShadowFrame& shadow_frame, |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 106 | const Instruction* inst, uint16_t inst_data, JValue* result); |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 107 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 108 | // Handles invoke-XXX/range instructions. |
| 109 | // Returns true on success, otherwise throws an exception and returns false. |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 110 | template<InvokeType type, bool is_range, bool do_access_check> |
| 111 | static inline bool DoInvoke(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst, |
| 112 | uint16_t inst_data, JValue* result) { |
| 113 | const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); |
| 114 | const uint32_t vregC = (is_range) ? inst->VRegC_3rc() : inst->VRegC_35c(); |
Mathieu Chartier | e861ebd | 2013-10-09 15:01:21 -0700 | [diff] [blame] | 115 | Object* receiver = (type == kStatic) ? nullptr : shadow_frame.GetVRegReference(vregC); |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 116 | mirror::ArtMethod* sf_method = shadow_frame.GetMethod(); |
| 117 | ArtMethod* const method = FindMethodFromCode<type, do_access_check>( |
| 118 | method_idx, &receiver, &sf_method, self); |
| 119 | // The shadow frame should already be pushed, so we don't need to update it. |
Sebastien Hertz | d4beb6b | 2013-10-02 17:07:20 +0200 | [diff] [blame] | 120 | if (UNLIKELY(method == nullptr)) { |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 121 | CHECK(self->IsExceptionPending()); |
| 122 | result->SetJ(0); |
| 123 | return false; |
| 124 | } else if (UNLIKELY(method->IsAbstract())) { |
| 125 | ThrowAbstractMethodError(method); |
| 126 | result->SetJ(0); |
| 127 | return false; |
| 128 | } else { |
Sebastien Hertz | 9119c5f | 2013-12-16 11:31:45 +0100 | [diff] [blame] | 129 | return DoCall<is_range, do_access_check>(method, self, shadow_frame, inst, inst_data, result); |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 130 | } |
| 131 | } |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 132 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 133 | // Handles invoke-virtual-quick and invoke-virtual-quick-range instructions. |
| 134 | // Returns true on success, otherwise throws an exception and returns false. |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 135 | template<bool is_range> |
| 136 | static inline bool DoInvokeVirtualQuick(Thread* self, ShadowFrame& shadow_frame, |
| 137 | const Instruction* inst, uint16_t inst_data, |
| 138 | JValue* result) { |
| 139 | const uint32_t vregC = (is_range) ? inst->VRegC_3rc() : inst->VRegC_35c(); |
| 140 | Object* const receiver = shadow_frame.GetVRegReference(vregC); |
Sebastien Hertz | d4beb6b | 2013-10-02 17:07:20 +0200 | [diff] [blame] | 141 | if (UNLIKELY(receiver == nullptr)) { |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 142 | // We lost the reference to the method index so we cannot get a more |
| 143 | // precised exception message. |
| 144 | ThrowNullPointerExceptionFromDexPC(shadow_frame.GetCurrentLocationForThrow()); |
| 145 | return false; |
| 146 | } |
| 147 | const uint32_t vtable_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); |
| 148 | ArtMethod* const method = receiver->GetClass()->GetVTable()->GetWithoutChecks(vtable_idx); |
Sebastien Hertz | d4beb6b | 2013-10-02 17:07:20 +0200 | [diff] [blame] | 149 | if (UNLIKELY(method == nullptr)) { |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 150 | CHECK(self->IsExceptionPending()); |
| 151 | result->SetJ(0); |
| 152 | return false; |
| 153 | } else if (UNLIKELY(method->IsAbstract())) { |
| 154 | ThrowAbstractMethodError(method); |
| 155 | result->SetJ(0); |
| 156 | return false; |
| 157 | } else { |
| 158 | // No need to check since we've been quickened. |
Sebastien Hertz | 9119c5f | 2013-12-16 11:31:45 +0100 | [diff] [blame] | 159 | return DoCall<is_range, false>(method, self, shadow_frame, inst, inst_data, result); |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 160 | } |
| 161 | } |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 162 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 163 | // Handles iget-XXX and sget-XXX instructions. |
| 164 | // Returns true on success, otherwise throws an exception and returns false. |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 165 | template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check> |
Sebastien Hertz | 82aeddb | 2014-05-20 20:09:45 +0200 | [diff] [blame] | 166 | static SOMETIMES_INLINE_KEYWORD bool DoFieldGet(Thread* self, ShadowFrame& shadow_frame, |
| 167 | const Instruction* inst, uint16_t inst_data) { |
Sebastien Hertz | d4beb6b | 2013-10-02 17:07:20 +0200 | [diff] [blame] | 168 | const bool is_static = (find_type == StaticObjectRead) || (find_type == StaticPrimitiveRead); |
| 169 | const uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c(); |
| 170 | ArtField* f = FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self, |
| 171 | Primitive::FieldSize(field_type)); |
| 172 | if (UNLIKELY(f == nullptr)) { |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 173 | CHECK(self->IsExceptionPending()); |
| 174 | return false; |
| 175 | } |
| 176 | Object* obj; |
| 177 | if (is_static) { |
| 178 | obj = f->GetDeclaringClass(); |
| 179 | } else { |
Sebastien Hertz | 3b588e0 | 2013-09-11 14:33:18 +0200 | [diff] [blame] | 180 | obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data)); |
Sebastien Hertz | d4beb6b | 2013-10-02 17:07:20 +0200 | [diff] [blame] | 181 | if (UNLIKELY(obj == nullptr)) { |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 182 | ThrowNullPointerExceptionForFieldAccess(shadow_frame.GetCurrentLocationForThrow(), f, true); |
| 183 | return false; |
| 184 | } |
| 185 | } |
Sebastien Hertz | 479fc1e | 2014-04-04 17:51:34 +0200 | [diff] [blame] | 186 | // Report this field access to instrumentation if needed. |
| 187 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
| 188 | if (UNLIKELY(instrumentation->HasFieldReadListeners())) { |
| 189 | Object* this_object = f->IsStatic() ? nullptr : obj; |
| 190 | instrumentation->FieldReadEvent(self, this_object, shadow_frame.GetMethod(), |
| 191 | shadow_frame.GetDexPC(), f); |
| 192 | } |
Sebastien Hertz | 3b588e0 | 2013-09-11 14:33:18 +0200 | [diff] [blame] | 193 | uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 194 | switch (field_type) { |
| 195 | case Primitive::kPrimBoolean: |
| 196 | shadow_frame.SetVReg(vregA, f->GetBoolean(obj)); |
| 197 | break; |
| 198 | case Primitive::kPrimByte: |
| 199 | shadow_frame.SetVReg(vregA, f->GetByte(obj)); |
| 200 | break; |
| 201 | case Primitive::kPrimChar: |
| 202 | shadow_frame.SetVReg(vregA, f->GetChar(obj)); |
| 203 | break; |
| 204 | case Primitive::kPrimShort: |
| 205 | shadow_frame.SetVReg(vregA, f->GetShort(obj)); |
| 206 | break; |
| 207 | case Primitive::kPrimInt: |
| 208 | shadow_frame.SetVReg(vregA, f->GetInt(obj)); |
| 209 | break; |
| 210 | case Primitive::kPrimLong: |
| 211 | shadow_frame.SetVRegLong(vregA, f->GetLong(obj)); |
| 212 | break; |
| 213 | case Primitive::kPrimNot: |
| 214 | shadow_frame.SetVRegReference(vregA, f->GetObject(obj)); |
| 215 | break; |
| 216 | default: |
| 217 | LOG(FATAL) << "Unreachable: " << field_type; |
| 218 | } |
| 219 | return true; |
| 220 | } |
| 221 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 222 | // Handles iget-quick, iget-wide-quick and iget-object-quick instructions. |
| 223 | // Returns true on success, otherwise throws an exception and returns false. |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 224 | template<Primitive::Type field_type> |
Sebastien Hertz | 82aeddb | 2014-05-20 20:09:45 +0200 | [diff] [blame] | 225 | static SOMETIMES_INLINE_KEYWORD bool DoIGetQuick(ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) { |
Sebastien Hertz | 3b588e0 | 2013-09-11 14:33:18 +0200 | [diff] [blame] | 226 | Object* obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data)); |
Sebastien Hertz | d4beb6b | 2013-10-02 17:07:20 +0200 | [diff] [blame] | 227 | if (UNLIKELY(obj == nullptr)) { |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 228 | // We lost the reference to the field index so we cannot get a more |
| 229 | // precised exception message. |
| 230 | ThrowNullPointerExceptionFromDexPC(shadow_frame.GetCurrentLocationForThrow()); |
| 231 | return false; |
| 232 | } |
| 233 | MemberOffset field_offset(inst->VRegC_22c()); |
Sebastien Hertz | 479fc1e | 2014-04-04 17:51:34 +0200 | [diff] [blame] | 234 | // Report this field access to instrumentation if needed. Since we only have the offset of |
| 235 | // the field from the base of the object, we need to look for it first. |
| 236 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
| 237 | if (UNLIKELY(instrumentation->HasFieldReadListeners())) { |
| 238 | ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(), |
| 239 | field_offset.Uint32Value()); |
| 240 | DCHECK(f != nullptr); |
| 241 | DCHECK(!f->IsStatic()); |
| 242 | instrumentation->FieldReadEvent(Thread::Current(), obj, shadow_frame.GetMethod(), |
| 243 | shadow_frame.GetDexPC(), f); |
| 244 | } |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 245 | // Note: iget-x-quick instructions are only for non-volatile fields. |
Sebastien Hertz | 3b588e0 | 2013-09-11 14:33:18 +0200 | [diff] [blame] | 246 | const uint32_t vregA = inst->VRegA_22c(inst_data); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 247 | switch (field_type) { |
| 248 | case Primitive::kPrimInt: |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 249 | shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetField32(field_offset))); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 250 | break; |
| 251 | case Primitive::kPrimLong: |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 252 | shadow_frame.SetVRegLong(vregA, static_cast<int64_t>(obj->GetField64(field_offset))); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 253 | break; |
| 254 | case Primitive::kPrimNot: |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 255 | shadow_frame.SetVRegReference(vregA, obj->GetFieldObject<mirror::Object>(field_offset)); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 256 | break; |
| 257 | default: |
| 258 | LOG(FATAL) << "Unreachable: " << field_type; |
| 259 | } |
| 260 | return true; |
| 261 | } |
| 262 | |
Sebastien Hertz | 479fc1e | 2014-04-04 17:51:34 +0200 | [diff] [blame] | 263 | template<Primitive::Type field_type> |
| 264 | static inline JValue GetFieldValue(const ShadowFrame& shadow_frame, uint32_t vreg) |
| 265 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 266 | JValue field_value; |
| 267 | switch (field_type) { |
| 268 | case Primitive::kPrimBoolean: |
| 269 | field_value.SetZ(static_cast<uint8_t>(shadow_frame.GetVReg(vreg))); |
| 270 | break; |
| 271 | case Primitive::kPrimByte: |
| 272 | field_value.SetB(static_cast<int8_t>(shadow_frame.GetVReg(vreg))); |
| 273 | break; |
| 274 | case Primitive::kPrimChar: |
| 275 | field_value.SetC(static_cast<uint16_t>(shadow_frame.GetVReg(vreg))); |
| 276 | break; |
| 277 | case Primitive::kPrimShort: |
| 278 | field_value.SetS(static_cast<int16_t>(shadow_frame.GetVReg(vreg))); |
| 279 | break; |
| 280 | case Primitive::kPrimInt: |
| 281 | field_value.SetI(shadow_frame.GetVReg(vreg)); |
| 282 | break; |
| 283 | case Primitive::kPrimLong: |
| 284 | field_value.SetJ(shadow_frame.GetVRegLong(vreg)); |
| 285 | break; |
| 286 | case Primitive::kPrimNot: |
| 287 | field_value.SetL(shadow_frame.GetVRegReference(vreg)); |
| 288 | break; |
| 289 | default: |
| 290 | LOG(FATAL) << "Unreachable: " << field_type; |
| 291 | break; |
| 292 | } |
| 293 | return field_value; |
| 294 | } |
| 295 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 296 | // Handles iput-XXX and sput-XXX instructions. |
| 297 | // Returns true on success, otherwise throws an exception and returns false. |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 298 | template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check, bool transaction_active> |
Sebastien Hertz | 82aeddb | 2014-05-20 20:09:45 +0200 | [diff] [blame] | 299 | static SOMETIMES_INLINE_KEYWORD bool DoFieldPut(Thread* self, const ShadowFrame& shadow_frame, |
| 300 | const Instruction* inst, uint16_t inst_data) { |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 301 | bool do_assignability_check = do_access_check; |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 302 | bool is_static = (find_type == StaticObjectWrite) || (find_type == StaticPrimitiveWrite); |
| 303 | uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c(); |
Sebastien Hertz | d4beb6b | 2013-10-02 17:07:20 +0200 | [diff] [blame] | 304 | ArtField* f = FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self, |
| 305 | Primitive::FieldSize(field_type)); |
| 306 | if (UNLIKELY(f == nullptr)) { |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 307 | CHECK(self->IsExceptionPending()); |
| 308 | return false; |
| 309 | } |
| 310 | Object* obj; |
| 311 | if (is_static) { |
| 312 | obj = f->GetDeclaringClass(); |
| 313 | } else { |
Sebastien Hertz | 3b588e0 | 2013-09-11 14:33:18 +0200 | [diff] [blame] | 314 | obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data)); |
Sebastien Hertz | d4beb6b | 2013-10-02 17:07:20 +0200 | [diff] [blame] | 315 | if (UNLIKELY(obj == nullptr)) { |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 316 | ThrowNullPointerExceptionForFieldAccess(shadow_frame.GetCurrentLocationForThrow(), |
| 317 | f, false); |
| 318 | return false; |
| 319 | } |
| 320 | } |
Sebastien Hertz | 3b588e0 | 2013-09-11 14:33:18 +0200 | [diff] [blame] | 321 | uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data); |
Sebastien Hertz | 479fc1e | 2014-04-04 17:51:34 +0200 | [diff] [blame] | 322 | // Report this field access to instrumentation if needed. Since we only have the offset of |
| 323 | // the field from the base of the object, we need to look for it first. |
| 324 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
| 325 | if (UNLIKELY(instrumentation->HasFieldWriteListeners())) { |
| 326 | JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA); |
| 327 | Object* this_object = f->IsStatic() ? nullptr : obj; |
| 328 | instrumentation->FieldWriteEvent(self, this_object, shadow_frame.GetMethod(), |
| 329 | shadow_frame.GetDexPC(), f, field_value); |
| 330 | } |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 331 | switch (field_type) { |
| 332 | case Primitive::kPrimBoolean: |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 333 | f->SetBoolean<transaction_active>(obj, shadow_frame.GetVReg(vregA)); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 334 | break; |
| 335 | case Primitive::kPrimByte: |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 336 | f->SetByte<transaction_active>(obj, shadow_frame.GetVReg(vregA)); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 337 | break; |
| 338 | case Primitive::kPrimChar: |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 339 | f->SetChar<transaction_active>(obj, shadow_frame.GetVReg(vregA)); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 340 | break; |
| 341 | case Primitive::kPrimShort: |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 342 | f->SetShort<transaction_active>(obj, shadow_frame.GetVReg(vregA)); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 343 | break; |
| 344 | case Primitive::kPrimInt: |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 345 | f->SetInt<transaction_active>(obj, shadow_frame.GetVReg(vregA)); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 346 | break; |
| 347 | case Primitive::kPrimLong: |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 348 | f->SetLong<transaction_active>(obj, shadow_frame.GetVRegLong(vregA)); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 349 | break; |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 350 | case Primitive::kPrimNot: { |
| 351 | Object* reg = shadow_frame.GetVRegReference(vregA); |
Sebastien Hertz | d4beb6b | 2013-10-02 17:07:20 +0200 | [diff] [blame] | 352 | if (do_assignability_check && reg != nullptr) { |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 353 | // FieldHelper::GetType can resolve classes, use a handle wrapper which will restore the |
| 354 | // object in the destructor. |
| 355 | StackHandleScope<1> hs(self); |
| 356 | HandleWrapper<mirror::Object> wrapper(hs.NewHandleWrapper(&obj)); |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 357 | Class* field_class = FieldHelper(f).GetType(); |
| 358 | if (!reg->VerifierInstanceOf(field_class)) { |
| 359 | // This should never happen. |
| 360 | self->ThrowNewExceptionF(self->GetCurrentLocationForThrow(), |
| 361 | "Ljava/lang/VirtualMachineError;", |
| 362 | "Put '%s' that is not instance of field '%s' in '%s'", |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 363 | reg->GetClass()->GetDescriptor().c_str(), |
| 364 | field_class->GetDescriptor().c_str(), |
| 365 | f->GetDeclaringClass()->GetDescriptor().c_str()); |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 366 | return false; |
| 367 | } |
| 368 | } |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 369 | f->SetObj<transaction_active>(obj, reg); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 370 | break; |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 371 | } |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 372 | default: |
| 373 | LOG(FATAL) << "Unreachable: " << field_type; |
| 374 | } |
| 375 | return true; |
| 376 | } |
| 377 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 378 | // Handles iput-quick, iput-wide-quick and iput-object-quick instructions. |
| 379 | // Returns true on success, otherwise throws an exception and returns false. |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 380 | template<Primitive::Type field_type, bool transaction_active> |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 381 | static SOMETIMES_INLINE_KEYWORD bool DoIPutQuick(const ShadowFrame& shadow_frame, |
| 382 | const Instruction* inst, uint16_t inst_data) { |
Sebastien Hertz | 3b588e0 | 2013-09-11 14:33:18 +0200 | [diff] [blame] | 383 | Object* obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data)); |
Sebastien Hertz | d4beb6b | 2013-10-02 17:07:20 +0200 | [diff] [blame] | 384 | if (UNLIKELY(obj == nullptr)) { |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 385 | // We lost the reference to the field index so we cannot get a more |
| 386 | // precised exception message. |
| 387 | ThrowNullPointerExceptionFromDexPC(shadow_frame.GetCurrentLocationForThrow()); |
| 388 | return false; |
| 389 | } |
| 390 | MemberOffset field_offset(inst->VRegC_22c()); |
Sebastien Hertz | 3b588e0 | 2013-09-11 14:33:18 +0200 | [diff] [blame] | 391 | const uint32_t vregA = inst->VRegA_22c(inst_data); |
Sebastien Hertz | 479fc1e | 2014-04-04 17:51:34 +0200 | [diff] [blame] | 392 | // Report this field modification to instrumentation if needed. Since we only have the offset of |
| 393 | // the field from the base of the object, we need to look for it first. |
| 394 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
| 395 | if (UNLIKELY(instrumentation->HasFieldWriteListeners())) { |
| 396 | ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(), |
| 397 | field_offset.Uint32Value()); |
| 398 | DCHECK(f != nullptr); |
| 399 | DCHECK(!f->IsStatic()); |
| 400 | JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA); |
| 401 | instrumentation->FieldWriteEvent(Thread::Current(), obj, shadow_frame.GetMethod(), |
| 402 | shadow_frame.GetDexPC(), f, field_value); |
| 403 | } |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 404 | // Note: iput-x-quick instructions are only for non-volatile fields. |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 405 | switch (field_type) { |
| 406 | case Primitive::kPrimInt: |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 407 | obj->SetField32<transaction_active>(field_offset, shadow_frame.GetVReg(vregA)); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 408 | break; |
| 409 | case Primitive::kPrimLong: |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 410 | obj->SetField64<transaction_active>(field_offset, shadow_frame.GetVRegLong(vregA)); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 411 | break; |
| 412 | case Primitive::kPrimNot: |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 413 | obj->SetFieldObject<transaction_active>(field_offset, shadow_frame.GetVRegReference(vregA)); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 414 | break; |
| 415 | default: |
| 416 | LOG(FATAL) << "Unreachable: " << field_type; |
| 417 | } |
| 418 | return true; |
| 419 | } |
| 420 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 421 | // Handles string resolution for const-string and const-string-jumbo instructions. Also ensures the |
| 422 | // java.lang.String class is initialized. |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 423 | static inline String* ResolveString(Thread* self, MethodHelper& mh, uint32_t string_idx) |
| 424 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 425 | CHECK(!kMovingMethods); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 426 | Class* java_lang_string_class = String::GetJavaLangString(); |
| 427 | if (UNLIKELY(!java_lang_string_class->IsInitialized())) { |
| 428 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 429 | StackHandleScope<1> hs(self); |
| 430 | Handle<mirror::Class> h_class(hs.NewHandle(java_lang_string_class)); |
| 431 | if (UNLIKELY(!class_linker->EnsureInitialized(h_class, true, true))) { |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 432 | DCHECK(self->IsExceptionPending()); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 433 | return nullptr; |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 434 | } |
| 435 | } |
| 436 | return mh.ResolveString(string_idx); |
| 437 | } |
| 438 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 439 | // Handles div-int, div-int/2addr, div-int/li16 and div-int/lit8 instructions. |
| 440 | // Returns true on success, otherwise throws a java.lang.ArithmeticException and return false. |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 441 | static inline bool DoIntDivide(ShadowFrame& shadow_frame, size_t result_reg, |
| 442 | int32_t dividend, int32_t divisor) |
| 443 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 2e2deeb | 2013-09-23 11:58:57 -0700 | [diff] [blame] | 444 | const int32_t kMinInt = std::numeric_limits<int32_t>::min(); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 445 | if (UNLIKELY(divisor == 0)) { |
| 446 | ThrowArithmeticExceptionDivideByZero(); |
| 447 | return false; |
| 448 | } |
| 449 | if (UNLIKELY(dividend == kMinInt && divisor == -1)) { |
| 450 | shadow_frame.SetVReg(result_reg, kMinInt); |
| 451 | } else { |
| 452 | shadow_frame.SetVReg(result_reg, dividend / divisor); |
| 453 | } |
| 454 | return true; |
| 455 | } |
| 456 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 457 | // Handles rem-int, rem-int/2addr, rem-int/li16 and rem-int/lit8 instructions. |
| 458 | // Returns true on success, otherwise throws a java.lang.ArithmeticException and return false. |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 459 | static inline bool DoIntRemainder(ShadowFrame& shadow_frame, size_t result_reg, |
| 460 | int32_t dividend, int32_t divisor) |
| 461 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 2e2deeb | 2013-09-23 11:58:57 -0700 | [diff] [blame] | 462 | const int32_t kMinInt = std::numeric_limits<int32_t>::min(); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 463 | if (UNLIKELY(divisor == 0)) { |
| 464 | ThrowArithmeticExceptionDivideByZero(); |
| 465 | return false; |
| 466 | } |
| 467 | if (UNLIKELY(dividend == kMinInt && divisor == -1)) { |
| 468 | shadow_frame.SetVReg(result_reg, 0); |
| 469 | } else { |
| 470 | shadow_frame.SetVReg(result_reg, dividend % divisor); |
| 471 | } |
| 472 | return true; |
| 473 | } |
| 474 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 475 | // Handles div-long and div-long-2addr instructions. |
| 476 | // Returns true on success, otherwise throws a java.lang.ArithmeticException and return false. |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 477 | static inline bool DoLongDivide(ShadowFrame& shadow_frame, size_t result_reg, |
| 478 | int64_t dividend, int64_t divisor) |
| 479 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 2e2deeb | 2013-09-23 11:58:57 -0700 | [diff] [blame] | 480 | const int64_t kMinLong = std::numeric_limits<int64_t>::min(); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 481 | if (UNLIKELY(divisor == 0)) { |
| 482 | ThrowArithmeticExceptionDivideByZero(); |
| 483 | return false; |
| 484 | } |
| 485 | if (UNLIKELY(dividend == kMinLong && divisor == -1)) { |
| 486 | shadow_frame.SetVRegLong(result_reg, kMinLong); |
| 487 | } else { |
| 488 | shadow_frame.SetVRegLong(result_reg, dividend / divisor); |
| 489 | } |
| 490 | return true; |
| 491 | } |
| 492 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 493 | // Handles rem-long and rem-long-2addr instructions. |
| 494 | // Returns true on success, otherwise throws a java.lang.ArithmeticException and return false. |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 495 | static inline bool DoLongRemainder(ShadowFrame& shadow_frame, size_t result_reg, |
| 496 | int64_t dividend, int64_t divisor) |
| 497 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 2e2deeb | 2013-09-23 11:58:57 -0700 | [diff] [blame] | 498 | const int64_t kMinLong = std::numeric_limits<int64_t>::min(); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 499 | if (UNLIKELY(divisor == 0)) { |
| 500 | ThrowArithmeticExceptionDivideByZero(); |
| 501 | return false; |
| 502 | } |
| 503 | if (UNLIKELY(dividend == kMinLong && divisor == -1)) { |
| 504 | shadow_frame.SetVRegLong(result_reg, 0); |
| 505 | } else { |
| 506 | shadow_frame.SetVRegLong(result_reg, dividend % divisor); |
| 507 | } |
| 508 | return true; |
| 509 | } |
| 510 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 511 | // Handles filled-new-array and filled-new-array-range instructions. |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 512 | // Returns true on success, otherwise throws an exception and returns false. |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 513 | template <bool is_range, bool do_access_check, bool transaction_active> |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 514 | bool DoFilledNewArray(const Instruction* inst, const ShadowFrame& shadow_frame, |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 515 | Thread* self, JValue* result); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 516 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 517 | // Handles packed-switch instruction. |
| 518 | // Returns the branch offset to the next instruction to execute. |
Sebastien Hertz | 3b588e0 | 2013-09-11 14:33:18 +0200 | [diff] [blame] | 519 | static inline int32_t DoPackedSwitch(const Instruction* inst, const ShadowFrame& shadow_frame, |
| 520 | uint16_t inst_data) |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 521 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 522 | DCHECK(inst->Opcode() == Instruction::PACKED_SWITCH); |
| 523 | const uint16_t* switch_data = reinterpret_cast<const uint16_t*>(inst) + inst->VRegB_31t(); |
Sebastien Hertz | 3b588e0 | 2013-09-11 14:33:18 +0200 | [diff] [blame] | 524 | int32_t test_val = shadow_frame.GetVReg(inst->VRegA_31t(inst_data)); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 525 | DCHECK_EQ(switch_data[0], static_cast<uint16_t>(Instruction::kPackedSwitchSignature)); |
| 526 | uint16_t size = switch_data[1]; |
| 527 | DCHECK_GT(size, 0); |
| 528 | const int32_t* keys = reinterpret_cast<const int32_t*>(&switch_data[2]); |
| 529 | DCHECK(IsAligned<4>(keys)); |
| 530 | int32_t first_key = keys[0]; |
| 531 | const int32_t* targets = reinterpret_cast<const int32_t*>(&switch_data[4]); |
| 532 | DCHECK(IsAligned<4>(targets)); |
| 533 | int32_t index = test_val - first_key; |
| 534 | if (index >= 0 && index < size) { |
| 535 | return targets[index]; |
| 536 | } else { |
| 537 | // No corresponding value: move forward by 3 (size of PACKED_SWITCH). |
| 538 | return 3; |
| 539 | } |
| 540 | } |
| 541 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 542 | // Handles sparse-switch instruction. |
| 543 | // Returns the branch offset to the next instruction to execute. |
Sebastien Hertz | 3b588e0 | 2013-09-11 14:33:18 +0200 | [diff] [blame] | 544 | static inline int32_t DoSparseSwitch(const Instruction* inst, const ShadowFrame& shadow_frame, |
| 545 | uint16_t inst_data) |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 546 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 547 | DCHECK(inst->Opcode() == Instruction::SPARSE_SWITCH); |
| 548 | const uint16_t* switch_data = reinterpret_cast<const uint16_t*>(inst) + inst->VRegB_31t(); |
Sebastien Hertz | 3b588e0 | 2013-09-11 14:33:18 +0200 | [diff] [blame] | 549 | int32_t test_val = shadow_frame.GetVReg(inst->VRegA_31t(inst_data)); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 550 | DCHECK_EQ(switch_data[0], static_cast<uint16_t>(Instruction::kSparseSwitchSignature)); |
| 551 | uint16_t size = switch_data[1]; |
| 552 | DCHECK_GT(size, 0); |
| 553 | const int32_t* keys = reinterpret_cast<const int32_t*>(&switch_data[2]); |
| 554 | DCHECK(IsAligned<4>(keys)); |
| 555 | const int32_t* entries = keys + size; |
| 556 | DCHECK(IsAligned<4>(entries)); |
| 557 | int lo = 0; |
| 558 | int hi = size - 1; |
| 559 | while (lo <= hi) { |
| 560 | int mid = (lo + hi) / 2; |
| 561 | int32_t foundVal = keys[mid]; |
| 562 | if (test_val < foundVal) { |
| 563 | hi = mid - 1; |
| 564 | } else if (test_val > foundVal) { |
| 565 | lo = mid + 1; |
| 566 | } else { |
| 567 | return entries[mid]; |
| 568 | } |
| 569 | } |
| 570 | // No corresponding value: move forward by 3 (size of SPARSE_SWITCH). |
| 571 | return 3; |
| 572 | } |
| 573 | |
| 574 | static inline uint32_t FindNextInstructionFollowingException(Thread* self, |
| 575 | ShadowFrame& shadow_frame, |
| 576 | uint32_t dex_pc, |
Sebastien Hertz | 947ff08 | 2013-09-17 14:10:13 +0200 | [diff] [blame] | 577 | mirror::Object* this_object, |
| 578 | const instrumentation::Instrumentation* instrumentation) |
Sebastien Hertz | 82aeddb | 2014-05-20 20:09:45 +0200 | [diff] [blame] | 579 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) SOMETIMES_INLINE; |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 580 | |
| 581 | static inline uint32_t FindNextInstructionFollowingException(Thread* self, |
| 582 | ShadowFrame& shadow_frame, |
| 583 | uint32_t dex_pc, |
Sebastien Hertz | 947ff08 | 2013-09-17 14:10:13 +0200 | [diff] [blame] | 584 | mirror::Object* this_object, |
Sebastien Hertz | 82aeddb | 2014-05-20 20:09:45 +0200 | [diff] [blame] | 585 | const instrumentation::Instrumentation* instrumentation) { |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 586 | self->VerifyStack(); |
| 587 | ThrowLocation throw_location; |
| 588 | mirror::Throwable* exception = self->GetException(&throw_location); |
Sebastien Hertz | 947ff08 | 2013-09-17 14:10:13 +0200 | [diff] [blame] | 589 | bool clear_exception = false; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 590 | StackHandleScope<3> hs(self); |
| 591 | Handle<mirror::Class> exception_class(hs.NewHandle(exception->GetClass())); |
Jeff Hao | aa96191 | 2014-04-22 13:54:32 -0700 | [diff] [blame] | 592 | uint32_t found_dex_pc = shadow_frame.GetMethod()->FindCatchBlock(exception_class, dex_pc, |
Ian Rogers | 822266b | 2014-05-29 16:55:06 -0700 | [diff] [blame^] | 593 | &clear_exception); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 594 | if (found_dex_pc == DexFile::kDexNoIndex) { |
Sebastien Hertz | 947ff08 | 2013-09-17 14:10:13 +0200 | [diff] [blame] | 595 | instrumentation->MethodUnwindEvent(self, this_object, |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 596 | shadow_frame.GetMethod(), dex_pc); |
| 597 | } else { |
| 598 | instrumentation->ExceptionCaughtEvent(self, throw_location, |
| 599 | shadow_frame.GetMethod(), |
| 600 | found_dex_pc, exception); |
| 601 | if (clear_exception) { |
| 602 | self->ClearException(); |
| 603 | } |
| 604 | } |
| 605 | return found_dex_pc; |
| 606 | } |
| 607 | |
Ian Rogers | b48b9eb | 2014-02-28 16:20:21 -0800 | [diff] [blame] | 608 | static inline void UnexpectedOpcode(const Instruction* inst, MethodHelper& mh) |
| 609 | __attribute__((cold, noreturn)) |
| 610 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 611 | |
Ian Rogers | b48b9eb | 2014-02-28 16:20:21 -0800 | [diff] [blame] | 612 | static inline void UnexpectedOpcode(const Instruction* inst, MethodHelper& mh) { |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 613 | LOG(FATAL) << "Unexpected instruction: " << inst->DumpString(&mh.GetDexFile()); |
| 614 | exit(0); // Unreachable, keep GCC happy. |
| 615 | } |
| 616 | |
| 617 | static inline void TraceExecution(const ShadowFrame& shadow_frame, const Instruction* inst, |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 618 | const uint32_t dex_pc, MethodHelper& mh) |
| 619 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | e861ebd | 2013-10-09 15:01:21 -0700 | [diff] [blame] | 620 | constexpr bool kTracing = false; |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 621 | if (kTracing) { |
| 622 | #define TRACE_LOG std::cerr |
Mathieu Chartier | e861ebd | 2013-10-09 15:01:21 -0700 | [diff] [blame] | 623 | std::ostringstream oss; |
| 624 | oss << PrettyMethod(shadow_frame.GetMethod()) |
| 625 | << StringPrintf("\n0x%x: ", dex_pc) |
| 626 | << inst->DumpString(&mh.GetDexFile()) << "\n"; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 627 | for (uint32_t i = 0; i < shadow_frame.NumberOfVRegs(); ++i) { |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 628 | uint32_t raw_value = shadow_frame.GetVReg(i); |
| 629 | Object* ref_value = shadow_frame.GetVRegReference(i); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 630 | oss << StringPrintf(" vreg%u=0x%08X", i, raw_value); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 631 | if (ref_value != NULL) { |
| 632 | if (ref_value->GetClass()->IsStringClass() && |
| 633 | ref_value->AsString()->GetCharArray() != NULL) { |
Mathieu Chartier | e861ebd | 2013-10-09 15:01:21 -0700 | [diff] [blame] | 634 | oss << "/java.lang.String \"" << ref_value->AsString()->ToModifiedUtf8() << "\""; |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 635 | } else { |
Mathieu Chartier | e861ebd | 2013-10-09 15:01:21 -0700 | [diff] [blame] | 636 | oss << "/" << PrettyTypeOf(ref_value); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 637 | } |
| 638 | } |
| 639 | } |
Mathieu Chartier | e861ebd | 2013-10-09 15:01:21 -0700 | [diff] [blame] | 640 | TRACE_LOG << oss.str() << "\n"; |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 641 | #undef TRACE_LOG |
| 642 | } |
| 643 | } |
| 644 | |
Sebastien Hertz | 1eda226 | 2013-09-09 16:53:14 +0200 | [diff] [blame] | 645 | static inline bool IsBackwardBranch(int32_t branch_offset) { |
| 646 | return branch_offset <= 0; |
| 647 | } |
| 648 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 649 | // Explicitly instantiate all DoInvoke functions. |
Bernhard Rosenkränzer | 4605362 | 2013-12-12 02:15:52 +0100 | [diff] [blame] | 650 | #define EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, _is_range, _do_check) \ |
Sebastien Hertz | 82aeddb | 2014-05-20 20:09:45 +0200 | [diff] [blame] | 651 | template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) SOMETIMES_INLINE \ |
Bernhard Rosenkränzer | 4605362 | 2013-12-12 02:15:52 +0100 | [diff] [blame] | 652 | bool DoInvoke<_type, _is_range, _do_check>(Thread* self, ShadowFrame& shadow_frame, \ |
| 653 | const Instruction* inst, uint16_t inst_data, \ |
| 654 | JValue* result) |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 655 | |
| 656 | #define EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(_type) \ |
| 657 | EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, false, false); \ |
| 658 | EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, false, true); \ |
| 659 | EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, true, false); \ |
| 660 | EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, true, true); |
| 661 | |
| 662 | EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(kStatic); // invoke-static/range. |
| 663 | EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(kDirect); // invoke-direct/range. |
| 664 | EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(kVirtual); // invoke-virtual/range. |
| 665 | EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(kSuper); // invoke-super/range. |
| 666 | EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(kInterface); // invoke-interface/range. |
| 667 | #undef EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL |
| 668 | #undef EXPLICIT_DO_INVOKE_TEMPLATE_DECL |
| 669 | |
| 670 | // Explicitly instantiate all DoFieldGet functions. |
Bernhard Rosenkränzer | 4605362 | 2013-12-12 02:15:52 +0100 | [diff] [blame] | 671 | #define EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, _do_check) \ |
Sebastien Hertz | 82aeddb | 2014-05-20 20:09:45 +0200 | [diff] [blame] | 672 | template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) SOMETIMES_INLINE \ |
Bernhard Rosenkränzer | 4605362 | 2013-12-12 02:15:52 +0100 | [diff] [blame] | 673 | bool DoFieldGet<_find_type, _field_type, _do_check>(Thread* self, ShadowFrame& shadow_frame, \ |
| 674 | const Instruction* inst, uint16_t inst_data) |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 675 | |
| 676 | #define EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(_find_type, _field_type) \ |
| 677 | EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, false); \ |
| 678 | EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, true); |
| 679 | |
| 680 | // iget-XXX |
| 681 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimBoolean); |
| 682 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimByte); |
| 683 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimChar); |
| 684 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimShort); |
| 685 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimInt); |
| 686 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimLong); |
| 687 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstanceObjectRead, Primitive::kPrimNot); |
| 688 | |
| 689 | // sget-XXX |
| 690 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimBoolean); |
| 691 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimByte); |
| 692 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimChar); |
| 693 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimShort); |
| 694 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimInt); |
| 695 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimLong); |
| 696 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticObjectRead, Primitive::kPrimNot); |
| 697 | |
| 698 | #undef EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL |
| 699 | #undef EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL |
| 700 | |
| 701 | // Explicitly instantiate all DoFieldPut functions. |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 702 | #define EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, _do_check, _transaction_active) \ |
Sebastien Hertz | 82aeddb | 2014-05-20 20:09:45 +0200 | [diff] [blame] | 703 | template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) SOMETIMES_INLINE \ |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 704 | bool DoFieldPut<_find_type, _field_type, _do_check, _transaction_active>(Thread* self, const ShadowFrame& shadow_frame, \ |
| 705 | const Instruction* inst, uint16_t inst_data) |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 706 | |
| 707 | #define EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(_find_type, _field_type) \ |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 708 | EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, false); \ |
| 709 | EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, false); \ |
| 710 | EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, true); \ |
| 711 | EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, true); |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 712 | |
| 713 | // iput-XXX |
| 714 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimBoolean); |
| 715 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimByte); |
| 716 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimChar); |
| 717 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimShort); |
| 718 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimInt); |
| 719 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimLong); |
| 720 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstanceObjectWrite, Primitive::kPrimNot); |
| 721 | |
| 722 | // sput-XXX |
| 723 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimBoolean); |
| 724 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimByte); |
| 725 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimChar); |
| 726 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimShort); |
| 727 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimInt); |
| 728 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimLong); |
| 729 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticObjectWrite, Primitive::kPrimNot); |
| 730 | |
| 731 | #undef EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL |
| 732 | #undef EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL |
| 733 | |
| 734 | // Explicitly instantiate all DoInvokeVirtualQuick functions. |
Bernhard Rosenkränzer | 4605362 | 2013-12-12 02:15:52 +0100 | [diff] [blame] | 735 | #define EXPLICIT_DO_INVOKE_VIRTUAL_QUICK_TEMPLATE_DECL(_is_range) \ |
Sebastien Hertz | 82aeddb | 2014-05-20 20:09:45 +0200 | [diff] [blame] | 736 | template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) SOMETIMES_INLINE \ |
Bernhard Rosenkränzer | 4605362 | 2013-12-12 02:15:52 +0100 | [diff] [blame] | 737 | bool DoInvokeVirtualQuick<_is_range>(Thread* self, ShadowFrame& shadow_frame, \ |
| 738 | const Instruction* inst, uint16_t inst_data, \ |
| 739 | JValue* result) |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 740 | |
| 741 | EXPLICIT_DO_INVOKE_VIRTUAL_QUICK_TEMPLATE_DECL(false); // invoke-virtual-quick. |
| 742 | EXPLICIT_DO_INVOKE_VIRTUAL_QUICK_TEMPLATE_DECL(true); // invoke-virtual-quick-range. |
| 743 | #undef EXPLICIT_INSTANTIATION_DO_INVOKE_VIRTUAL_QUICK |
| 744 | |
| 745 | // Explicitly instantiate all DoIGetQuick functions. |
Bernhard Rosenkränzer | 4605362 | 2013-12-12 02:15:52 +0100 | [diff] [blame] | 746 | #define EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(_field_type) \ |
Sebastien Hertz | 82aeddb | 2014-05-20 20:09:45 +0200 | [diff] [blame] | 747 | template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) SOMETIMES_INLINE \ |
Bernhard Rosenkränzer | 4605362 | 2013-12-12 02:15:52 +0100 | [diff] [blame] | 748 | bool DoIGetQuick<_field_type>(ShadowFrame& shadow_frame, const Instruction* inst, \ |
| 749 | uint16_t inst_data) |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 750 | |
| 751 | EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimInt); // iget-quick. |
| 752 | EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimLong); // iget-wide-quick. |
| 753 | EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimNot); // iget-object-quick. |
| 754 | #undef EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL |
| 755 | |
| 756 | // Explicitly instantiate all DoIPutQuick functions. |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 757 | #define EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, _transaction_active) \ |
Sebastien Hertz | 82aeddb | 2014-05-20 20:09:45 +0200 | [diff] [blame] | 758 | template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) SOMETIMES_INLINE \ |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 759 | bool DoIPutQuick<_field_type, _transaction_active>(const ShadowFrame& shadow_frame, \ |
| 760 | const Instruction* inst, \ |
| 761 | uint16_t inst_data) |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 762 | |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 763 | #define EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(_field_type) \ |
| 764 | EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, false); \ |
| 765 | EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, true); |
| 766 | |
| 767 | EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimInt); // iget-quick. |
| 768 | EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimLong); // iget-wide-quick. |
| 769 | EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimNot); // iget-object-quick. |
| 770 | #undef EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 771 | #undef EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL |
| 772 | |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 773 | } // namespace interpreter |
| 774 | } // namespace art |
| 775 | |
| 776 | #endif // ART_RUNTIME_INTERPRETER_INTERPRETER_COMMON_H_ |