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" |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 30 | #include "entrypoints/entrypoint_utils-inl.h" |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 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" |
Ian Rogers | 22d5e73 | 2014-07-15 22:23:51 -0700 | [diff] [blame] | 33 | #include "method_helper-inl.h" |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 34 | #include "nth_caller_visitor.h" |
| 35 | #include "mirror/art_field-inl.h" |
| 36 | #include "mirror/art_method.h" |
| 37 | #include "mirror/art_method-inl.h" |
| 38 | #include "mirror/class.h" |
| 39 | #include "mirror/class-inl.h" |
| 40 | #include "mirror/object-inl.h" |
| 41 | #include "mirror/object_array-inl.h" |
Douglas Leung | 4965c02 | 2014-06-11 11:41:11 -0700 | [diff] [blame] | 42 | #include "mirror/string-inl.h" |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 43 | #include "ScopedLocalRef.h" |
| 44 | #include "scoped_thread_state_change.h" |
| 45 | #include "thread.h" |
| 46 | #include "well_known_classes.h" |
| 47 | |
| 48 | using ::art::mirror::ArtField; |
| 49 | using ::art::mirror::ArtMethod; |
| 50 | using ::art::mirror::Array; |
| 51 | using ::art::mirror::BooleanArray; |
| 52 | using ::art::mirror::ByteArray; |
| 53 | using ::art::mirror::CharArray; |
| 54 | using ::art::mirror::Class; |
| 55 | using ::art::mirror::ClassLoader; |
| 56 | using ::art::mirror::IntArray; |
| 57 | using ::art::mirror::LongArray; |
| 58 | using ::art::mirror::Object; |
| 59 | using ::art::mirror::ObjectArray; |
| 60 | using ::art::mirror::ShortArray; |
| 61 | using ::art::mirror::String; |
| 62 | using ::art::mirror::Throwable; |
| 63 | |
| 64 | namespace art { |
| 65 | namespace interpreter { |
| 66 | |
| 67 | // External references to both interpreter implementations. |
| 68 | |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 69 | template<bool do_access_check, bool transaction_active> |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 70 | extern JValue ExecuteSwitchImpl(Thread* self, MethodHelper& mh, |
| 71 | const DexFile::CodeItem* code_item, |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 72 | ShadowFrame& shadow_frame, JValue result_register); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 73 | |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 74 | template<bool do_access_check, bool transaction_active> |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 75 | extern JValue ExecuteGotoImpl(Thread* self, MethodHelper& mh, |
| 76 | const DexFile::CodeItem* code_item, |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 77 | ShadowFrame& shadow_frame, JValue result_register); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 78 | |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 79 | void ThrowNullPointerExceptionFromInterpreter(const ShadowFrame& shadow_frame) |
| 80 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Sebastien Hertz | da843e1 | 2014-05-28 19:28:31 +0200 | [diff] [blame] | 81 | |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 82 | static inline void DoMonitorEnter(Thread* self, Object* ref) NO_THREAD_SAFETY_ANALYSIS { |
| 83 | ref->MonitorEnter(self); |
| 84 | } |
| 85 | |
| 86 | static inline void DoMonitorExit(Thread* self, Object* ref) NO_THREAD_SAFETY_ANALYSIS { |
| 87 | ref->MonitorExit(self); |
| 88 | } |
| 89 | |
Mathieu Chartier | b2c7ead | 2014-04-29 11:13:16 -0700 | [diff] [blame] | 90 | void AbortTransaction(Thread* self, const char* fmt, ...) |
| 91 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 92 | |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 93 | void RecordArrayElementsInTransaction(mirror::Array* array, int32_t count) |
| 94 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 95 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 96 | // Invokes the given method. This is part of the invocation support and is used by DoInvoke and |
| 97 | // DoInvokeVirtualQuick functions. |
| 98 | // Returns true on success, otherwise throws an exception and returns false. |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 99 | template<bool is_range, bool do_assignability_check> |
Sebastien Hertz | 9119c5f | 2013-12-16 11:31:45 +0100 | [diff] [blame] | 100 | bool DoCall(ArtMethod* method, Thread* self, ShadowFrame& shadow_frame, |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 101 | const Instruction* inst, uint16_t inst_data, JValue* result); |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 102 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 103 | // Handles invoke-XXX/range instructions. |
| 104 | // Returns true on success, otherwise throws an exception and returns false. |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 105 | template<InvokeType type, bool is_range, bool do_access_check> |
| 106 | static inline bool DoInvoke(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst, |
| 107 | uint16_t inst_data, JValue* result) { |
| 108 | const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); |
| 109 | const uint32_t vregC = (is_range) ? inst->VRegC_3rc() : inst->VRegC_35c(); |
Mathieu Chartier | e861ebd | 2013-10-09 15:01:21 -0700 | [diff] [blame] | 110 | Object* receiver = (type == kStatic) ? nullptr : shadow_frame.GetVRegReference(vregC); |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 111 | mirror::ArtMethod* sf_method = shadow_frame.GetMethod(); |
| 112 | ArtMethod* const method = FindMethodFromCode<type, do_access_check>( |
| 113 | method_idx, &receiver, &sf_method, self); |
| 114 | // 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] | 115 | if (UNLIKELY(method == nullptr)) { |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 116 | CHECK(self->IsExceptionPending()); |
| 117 | result->SetJ(0); |
| 118 | return false; |
| 119 | } else if (UNLIKELY(method->IsAbstract())) { |
| 120 | ThrowAbstractMethodError(method); |
| 121 | result->SetJ(0); |
| 122 | return false; |
| 123 | } else { |
Sebastien Hertz | 9119c5f | 2013-12-16 11:31:45 +0100 | [diff] [blame] | 124 | 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] | 125 | } |
| 126 | } |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 127 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 128 | // Handles invoke-virtual-quick and invoke-virtual-quick-range instructions. |
| 129 | // Returns true on success, otherwise throws an exception and returns false. |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 130 | template<bool is_range> |
| 131 | static inline bool DoInvokeVirtualQuick(Thread* self, ShadowFrame& shadow_frame, |
| 132 | const Instruction* inst, uint16_t inst_data, |
| 133 | JValue* result) { |
| 134 | const uint32_t vregC = (is_range) ? inst->VRegC_3rc() : inst->VRegC_35c(); |
| 135 | Object* const receiver = shadow_frame.GetVRegReference(vregC); |
Sebastien Hertz | d4beb6b | 2013-10-02 17:07:20 +0200 | [diff] [blame] | 136 | if (UNLIKELY(receiver == nullptr)) { |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 137 | // We lost the reference to the method index so we cannot get a more |
| 138 | // precised exception message. |
| 139 | ThrowNullPointerExceptionFromDexPC(shadow_frame.GetCurrentLocationForThrow()); |
| 140 | return false; |
| 141 | } |
| 142 | const uint32_t vtable_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 143 | CHECK(receiver->GetClass()->ShouldHaveEmbeddedImtAndVTable()); |
| 144 | ArtMethod* const method = receiver->GetClass()->GetEmbeddedVTableEntry(vtable_idx); |
Sebastien Hertz | d4beb6b | 2013-10-02 17:07:20 +0200 | [diff] [blame] | 145 | if (UNLIKELY(method == nullptr)) { |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 146 | CHECK(self->IsExceptionPending()); |
| 147 | result->SetJ(0); |
| 148 | return false; |
| 149 | } else if (UNLIKELY(method->IsAbstract())) { |
| 150 | ThrowAbstractMethodError(method); |
| 151 | result->SetJ(0); |
| 152 | return false; |
| 153 | } else { |
| 154 | // No need to check since we've been quickened. |
Sebastien Hertz | 9119c5f | 2013-12-16 11:31:45 +0100 | [diff] [blame] | 155 | 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] | 156 | } |
| 157 | } |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 158 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 159 | // Handles iget-XXX and sget-XXX instructions. |
| 160 | // Returns true on success, otherwise throws an exception and returns false. |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 161 | template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check> |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 162 | bool DoFieldGet(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst, |
| 163 | uint16_t inst_data) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 164 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 165 | // Handles iget-quick, iget-wide-quick and iget-object-quick instructions. |
| 166 | // Returns true on success, otherwise throws an exception and returns false. |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 167 | template<Primitive::Type field_type> |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 168 | bool DoIGetQuick(ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) |
| 169 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Sebastien Hertz | 479fc1e | 2014-04-04 17:51:34 +0200 | [diff] [blame] | 170 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 171 | // Handles iput-XXX and sput-XXX instructions. |
| 172 | // Returns true on success, otherwise throws an exception and returns false. |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 173 | template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check, |
| 174 | bool transaction_active> |
| 175 | bool DoFieldPut(Thread* self, const ShadowFrame& shadow_frame, const Instruction* inst, |
| 176 | uint16_t inst_data) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 177 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 178 | // Handles iput-quick, iput-wide-quick and iput-object-quick instructions. |
| 179 | // Returns true on success, otherwise throws an exception and returns false. |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 180 | template<Primitive::Type field_type, bool transaction_active> |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 181 | bool DoIPutQuick(const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) |
| 182 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 183 | |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 184 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 185 | // Handles string resolution for const-string and const-string-jumbo instructions. Also ensures the |
| 186 | // java.lang.String class is initialized. |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 187 | static inline String* ResolveString(Thread* self, MethodHelper& mh, uint32_t string_idx) |
| 188 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 189 | CHECK(!kMovingMethods); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 190 | Class* java_lang_string_class = String::GetJavaLangString(); |
| 191 | if (UNLIKELY(!java_lang_string_class->IsInitialized())) { |
| 192 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 193 | StackHandleScope<1> hs(self); |
| 194 | Handle<mirror::Class> h_class(hs.NewHandle(java_lang_string_class)); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 195 | if (UNLIKELY(!class_linker->EnsureInitialized(self, h_class, true, true))) { |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 196 | DCHECK(self->IsExceptionPending()); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 197 | return nullptr; |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | return mh.ResolveString(string_idx); |
| 201 | } |
| 202 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 203 | // Handles div-int, div-int/2addr, div-int/li16 and div-int/lit8 instructions. |
| 204 | // 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] | 205 | static inline bool DoIntDivide(ShadowFrame& shadow_frame, size_t result_reg, |
| 206 | int32_t dividend, int32_t divisor) |
| 207 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 2e2deeb | 2013-09-23 11:58:57 -0700 | [diff] [blame] | 208 | const int32_t kMinInt = std::numeric_limits<int32_t>::min(); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 209 | if (UNLIKELY(divisor == 0)) { |
| 210 | ThrowArithmeticExceptionDivideByZero(); |
| 211 | return false; |
| 212 | } |
| 213 | if (UNLIKELY(dividend == kMinInt && divisor == -1)) { |
| 214 | shadow_frame.SetVReg(result_reg, kMinInt); |
| 215 | } else { |
| 216 | shadow_frame.SetVReg(result_reg, dividend / divisor); |
| 217 | } |
| 218 | return true; |
| 219 | } |
| 220 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 221 | // Handles rem-int, rem-int/2addr, rem-int/li16 and rem-int/lit8 instructions. |
| 222 | // 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] | 223 | static inline bool DoIntRemainder(ShadowFrame& shadow_frame, size_t result_reg, |
| 224 | int32_t dividend, int32_t divisor) |
| 225 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 2e2deeb | 2013-09-23 11:58:57 -0700 | [diff] [blame] | 226 | const int32_t kMinInt = std::numeric_limits<int32_t>::min(); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 227 | if (UNLIKELY(divisor == 0)) { |
| 228 | ThrowArithmeticExceptionDivideByZero(); |
| 229 | return false; |
| 230 | } |
| 231 | if (UNLIKELY(dividend == kMinInt && divisor == -1)) { |
| 232 | shadow_frame.SetVReg(result_reg, 0); |
| 233 | } else { |
| 234 | shadow_frame.SetVReg(result_reg, dividend % divisor); |
| 235 | } |
| 236 | return true; |
| 237 | } |
| 238 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 239 | // Handles div-long and div-long-2addr instructions. |
| 240 | // 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] | 241 | static inline bool DoLongDivide(ShadowFrame& shadow_frame, size_t result_reg, |
| 242 | int64_t dividend, int64_t divisor) |
| 243 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 2e2deeb | 2013-09-23 11:58:57 -0700 | [diff] [blame] | 244 | const int64_t kMinLong = std::numeric_limits<int64_t>::min(); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 245 | if (UNLIKELY(divisor == 0)) { |
| 246 | ThrowArithmeticExceptionDivideByZero(); |
| 247 | return false; |
| 248 | } |
| 249 | if (UNLIKELY(dividend == kMinLong && divisor == -1)) { |
| 250 | shadow_frame.SetVRegLong(result_reg, kMinLong); |
| 251 | } else { |
| 252 | shadow_frame.SetVRegLong(result_reg, dividend / divisor); |
| 253 | } |
| 254 | return true; |
| 255 | } |
| 256 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 257 | // Handles rem-long and rem-long-2addr instructions. |
| 258 | // 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] | 259 | static inline bool DoLongRemainder(ShadowFrame& shadow_frame, size_t result_reg, |
| 260 | int64_t dividend, int64_t divisor) |
| 261 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 2e2deeb | 2013-09-23 11:58:57 -0700 | [diff] [blame] | 262 | const int64_t kMinLong = std::numeric_limits<int64_t>::min(); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 263 | if (UNLIKELY(divisor == 0)) { |
| 264 | ThrowArithmeticExceptionDivideByZero(); |
| 265 | return false; |
| 266 | } |
| 267 | if (UNLIKELY(dividend == kMinLong && divisor == -1)) { |
| 268 | shadow_frame.SetVRegLong(result_reg, 0); |
| 269 | } else { |
| 270 | shadow_frame.SetVRegLong(result_reg, dividend % divisor); |
| 271 | } |
| 272 | return true; |
| 273 | } |
| 274 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 275 | // Handles filled-new-array and filled-new-array-range instructions. |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 276 | // Returns true on success, otherwise throws an exception and returns false. |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 277 | template <bool is_range, bool do_access_check, bool transaction_active> |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 278 | bool DoFilledNewArray(const Instruction* inst, const ShadowFrame& shadow_frame, |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 279 | Thread* self, JValue* result); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 280 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 281 | // Handles packed-switch instruction. |
| 282 | // Returns the branch offset to the next instruction to execute. |
Sebastien Hertz | 3b588e0 | 2013-09-11 14:33:18 +0200 | [diff] [blame] | 283 | static inline int32_t DoPackedSwitch(const Instruction* inst, const ShadowFrame& shadow_frame, |
| 284 | uint16_t inst_data) |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 285 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 286 | DCHECK(inst->Opcode() == Instruction::PACKED_SWITCH); |
| 287 | 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] | 288 | int32_t test_val = shadow_frame.GetVReg(inst->VRegA_31t(inst_data)); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 289 | DCHECK_EQ(switch_data[0], static_cast<uint16_t>(Instruction::kPackedSwitchSignature)); |
| 290 | uint16_t size = switch_data[1]; |
| 291 | DCHECK_GT(size, 0); |
| 292 | const int32_t* keys = reinterpret_cast<const int32_t*>(&switch_data[2]); |
| 293 | DCHECK(IsAligned<4>(keys)); |
| 294 | int32_t first_key = keys[0]; |
| 295 | const int32_t* targets = reinterpret_cast<const int32_t*>(&switch_data[4]); |
| 296 | DCHECK(IsAligned<4>(targets)); |
| 297 | int32_t index = test_val - first_key; |
| 298 | if (index >= 0 && index < size) { |
| 299 | return targets[index]; |
| 300 | } else { |
| 301 | // No corresponding value: move forward by 3 (size of PACKED_SWITCH). |
| 302 | return 3; |
| 303 | } |
| 304 | } |
| 305 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 306 | // Handles sparse-switch instruction. |
| 307 | // Returns the branch offset to the next instruction to execute. |
Sebastien Hertz | 3b588e0 | 2013-09-11 14:33:18 +0200 | [diff] [blame] | 308 | static inline int32_t DoSparseSwitch(const Instruction* inst, const ShadowFrame& shadow_frame, |
| 309 | uint16_t inst_data) |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 310 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 311 | DCHECK(inst->Opcode() == Instruction::SPARSE_SWITCH); |
| 312 | 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] | 313 | int32_t test_val = shadow_frame.GetVReg(inst->VRegA_31t(inst_data)); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 314 | DCHECK_EQ(switch_data[0], static_cast<uint16_t>(Instruction::kSparseSwitchSignature)); |
| 315 | uint16_t size = switch_data[1]; |
| 316 | DCHECK_GT(size, 0); |
| 317 | const int32_t* keys = reinterpret_cast<const int32_t*>(&switch_data[2]); |
| 318 | DCHECK(IsAligned<4>(keys)); |
| 319 | const int32_t* entries = keys + size; |
| 320 | DCHECK(IsAligned<4>(entries)); |
| 321 | int lo = 0; |
| 322 | int hi = size - 1; |
| 323 | while (lo <= hi) { |
| 324 | int mid = (lo + hi) / 2; |
| 325 | int32_t foundVal = keys[mid]; |
| 326 | if (test_val < foundVal) { |
| 327 | hi = mid - 1; |
| 328 | } else if (test_val > foundVal) { |
| 329 | lo = mid + 1; |
| 330 | } else { |
| 331 | return entries[mid]; |
| 332 | } |
| 333 | } |
| 334 | // No corresponding value: move forward by 3 (size of SPARSE_SWITCH). |
| 335 | return 3; |
| 336 | } |
| 337 | |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 338 | uint32_t FindNextInstructionFollowingException(Thread* self, ShadowFrame& shadow_frame, |
Sebastien Hertz | 9f10203 | 2014-05-23 08:59:42 +0200 | [diff] [blame] | 339 | uint32_t dex_pc, const instrumentation::Instrumentation* instrumentation) |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 340 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 341 | |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 342 | void UnexpectedOpcode(const Instruction* inst, MethodHelper& mh) |
Ian Rogers | b48b9eb | 2014-02-28 16:20:21 -0800 | [diff] [blame] | 343 | __attribute__((cold, noreturn)) |
| 344 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 345 | |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 346 | static inline void TraceExecution(const ShadowFrame& shadow_frame, const Instruction* inst, |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 347 | const uint32_t dex_pc, MethodHelper& mh) |
| 348 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | e861ebd | 2013-10-09 15:01:21 -0700 | [diff] [blame] | 349 | constexpr bool kTracing = false; |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 350 | if (kTracing) { |
| 351 | #define TRACE_LOG std::cerr |
Mathieu Chartier | e861ebd | 2013-10-09 15:01:21 -0700 | [diff] [blame] | 352 | std::ostringstream oss; |
| 353 | oss << PrettyMethod(shadow_frame.GetMethod()) |
| 354 | << StringPrintf("\n0x%x: ", dex_pc) |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 355 | << inst->DumpString(mh.GetMethod()->GetDexFile()) << "\n"; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 356 | for (uint32_t i = 0; i < shadow_frame.NumberOfVRegs(); ++i) { |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 357 | uint32_t raw_value = shadow_frame.GetVReg(i); |
| 358 | Object* ref_value = shadow_frame.GetVRegReference(i); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 359 | oss << StringPrintf(" vreg%u=0x%08X", i, raw_value); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 360 | if (ref_value != NULL) { |
| 361 | if (ref_value->GetClass()->IsStringClass() && |
| 362 | ref_value->AsString()->GetCharArray() != NULL) { |
Mathieu Chartier | e861ebd | 2013-10-09 15:01:21 -0700 | [diff] [blame] | 363 | oss << "/java.lang.String \"" << ref_value->AsString()->ToModifiedUtf8() << "\""; |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 364 | } else { |
Mathieu Chartier | e861ebd | 2013-10-09 15:01:21 -0700 | [diff] [blame] | 365 | oss << "/" << PrettyTypeOf(ref_value); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 366 | } |
| 367 | } |
| 368 | } |
Mathieu Chartier | e861ebd | 2013-10-09 15:01:21 -0700 | [diff] [blame] | 369 | TRACE_LOG << oss.str() << "\n"; |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 370 | #undef TRACE_LOG |
| 371 | } |
| 372 | } |
| 373 | |
Sebastien Hertz | 1eda226 | 2013-09-09 16:53:14 +0200 | [diff] [blame] | 374 | static inline bool IsBackwardBranch(int32_t branch_offset) { |
| 375 | return branch_offset <= 0; |
| 376 | } |
| 377 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 378 | // Explicitly instantiate all DoInvoke functions. |
Bernhard Rosenkränzer | 4605362 | 2013-12-12 02:15:52 +0100 | [diff] [blame] | 379 | #define EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, _is_range, _do_check) \ |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 380 | template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) \ |
Bernhard Rosenkränzer | 4605362 | 2013-12-12 02:15:52 +0100 | [diff] [blame] | 381 | bool DoInvoke<_type, _is_range, _do_check>(Thread* self, ShadowFrame& shadow_frame, \ |
| 382 | const Instruction* inst, uint16_t inst_data, \ |
| 383 | JValue* result) |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 384 | |
| 385 | #define EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(_type) \ |
| 386 | EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, false, false); \ |
| 387 | EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, false, true); \ |
| 388 | EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, true, false); \ |
| 389 | EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, true, true); |
| 390 | |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame^] | 391 | EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(kStatic) // invoke-static/range. |
| 392 | EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(kDirect) // invoke-direct/range. |
| 393 | EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(kVirtual) // invoke-virtual/range. |
| 394 | EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(kSuper) // invoke-super/range. |
| 395 | EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(kInterface) // invoke-interface/range. |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 396 | #undef EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL |
| 397 | #undef EXPLICIT_DO_INVOKE_TEMPLATE_DECL |
| 398 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 399 | // Explicitly instantiate all DoInvokeVirtualQuick functions. |
Bernhard Rosenkränzer | 4605362 | 2013-12-12 02:15:52 +0100 | [diff] [blame] | 400 | #define EXPLICIT_DO_INVOKE_VIRTUAL_QUICK_TEMPLATE_DECL(_is_range) \ |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 401 | template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) \ |
Bernhard Rosenkränzer | 4605362 | 2013-12-12 02:15:52 +0100 | [diff] [blame] | 402 | bool DoInvokeVirtualQuick<_is_range>(Thread* self, ShadowFrame& shadow_frame, \ |
| 403 | const Instruction* inst, uint16_t inst_data, \ |
| 404 | JValue* result) |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 405 | |
| 406 | EXPLICIT_DO_INVOKE_VIRTUAL_QUICK_TEMPLATE_DECL(false); // invoke-virtual-quick. |
| 407 | EXPLICIT_DO_INVOKE_VIRTUAL_QUICK_TEMPLATE_DECL(true); // invoke-virtual-quick-range. |
| 408 | #undef EXPLICIT_INSTANTIATION_DO_INVOKE_VIRTUAL_QUICK |
| 409 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 410 | |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 411 | } // namespace interpreter |
| 412 | } // namespace art |
| 413 | |
| 414 | #endif // ART_RUNTIME_INTERPRETER_INTERPRETER_COMMON_H_ |