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 | #include "interpreter_common.h" |
Ian Rogers | 22d5e73 | 2014-07-15 22:23:51 -0700 | [diff] [blame] | 18 | |
Andreas Gampe | f0e128a | 2015-02-27 20:08:34 -0800 | [diff] [blame] | 19 | #include <cmath> |
| 20 | |
Daniel Mihalyi | eb07669 | 2014-08-22 17:33:31 +0200 | [diff] [blame] | 21 | #include "debugger.h" |
Nicolas Geoffray | 7bf2b4f | 2015-07-08 10:11:59 +0000 | [diff] [blame] | 22 | #include "entrypoints/runtime_asm_entrypoints.h" |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 23 | #include "mirror/array-inl.h" |
Andreas Gampe | b302592 | 2015-09-01 14:45:00 -0700 | [diff] [blame] | 24 | #include "stack.h" |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 25 | #include "unstarted_runtime.h" |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 26 | #include "verifier/method_verifier.h" |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 27 | |
| 28 | namespace art { |
| 29 | namespace interpreter { |
| 30 | |
Igor Murashkin | 6918bf1 | 2015-09-27 19:19:06 -0700 | [diff] [blame] | 31 | // All lambda closures have to be a consecutive pair of virtual registers. |
| 32 | static constexpr size_t kLambdaVirtualRegisterWidth = 2; |
| 33 | |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 34 | void ThrowNullPointerExceptionFromInterpreter() { |
| 35 | ThrowNullPointerExceptionFromDexPC(); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check> |
| 39 | bool DoFieldGet(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst, |
| 40 | uint16_t inst_data) { |
| 41 | const bool is_static = (find_type == StaticObjectRead) || (find_type == StaticPrimitiveRead); |
| 42 | const uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c(); |
| 43 | ArtField* f = FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self, |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 44 | Primitive::ComponentSize(field_type)); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 45 | if (UNLIKELY(f == nullptr)) { |
| 46 | CHECK(self->IsExceptionPending()); |
| 47 | return false; |
| 48 | } |
| 49 | Object* obj; |
| 50 | if (is_static) { |
| 51 | obj = f->GetDeclaringClass(); |
| 52 | } else { |
| 53 | obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data)); |
| 54 | if (UNLIKELY(obj == nullptr)) { |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 55 | ThrowNullPointerExceptionForFieldAccess(f, true); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 56 | return false; |
| 57 | } |
| 58 | } |
Sebastien Hertz | 1edbd8e | 2014-07-16 20:00:11 +0200 | [diff] [blame] | 59 | f->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 60 | // Report this field access to instrumentation if needed. |
| 61 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
| 62 | if (UNLIKELY(instrumentation->HasFieldReadListeners())) { |
| 63 | Object* this_object = f->IsStatic() ? nullptr : obj; |
| 64 | instrumentation->FieldReadEvent(self, this_object, shadow_frame.GetMethod(), |
| 65 | shadow_frame.GetDexPC(), f); |
| 66 | } |
| 67 | uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data); |
| 68 | switch (field_type) { |
| 69 | case Primitive::kPrimBoolean: |
| 70 | shadow_frame.SetVReg(vregA, f->GetBoolean(obj)); |
| 71 | break; |
| 72 | case Primitive::kPrimByte: |
| 73 | shadow_frame.SetVReg(vregA, f->GetByte(obj)); |
| 74 | break; |
| 75 | case Primitive::kPrimChar: |
| 76 | shadow_frame.SetVReg(vregA, f->GetChar(obj)); |
| 77 | break; |
| 78 | case Primitive::kPrimShort: |
| 79 | shadow_frame.SetVReg(vregA, f->GetShort(obj)); |
| 80 | break; |
| 81 | case Primitive::kPrimInt: |
| 82 | shadow_frame.SetVReg(vregA, f->GetInt(obj)); |
| 83 | break; |
| 84 | case Primitive::kPrimLong: |
| 85 | shadow_frame.SetVRegLong(vregA, f->GetLong(obj)); |
| 86 | break; |
| 87 | case Primitive::kPrimNot: |
| 88 | shadow_frame.SetVRegReference(vregA, f->GetObject(obj)); |
| 89 | break; |
| 90 | default: |
| 91 | LOG(FATAL) << "Unreachable: " << field_type; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 92 | UNREACHABLE(); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 93 | } |
| 94 | return true; |
| 95 | } |
| 96 | |
| 97 | // Explicitly instantiate all DoFieldGet functions. |
| 98 | #define EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, _do_check) \ |
| 99 | template bool DoFieldGet<_find_type, _field_type, _do_check>(Thread* self, \ |
| 100 | ShadowFrame& shadow_frame, \ |
| 101 | const Instruction* inst, \ |
| 102 | uint16_t inst_data) |
| 103 | |
| 104 | #define EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(_find_type, _field_type) \ |
| 105 | EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, false); \ |
| 106 | EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, true); |
| 107 | |
| 108 | // iget-XXX |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 109 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimBoolean) |
| 110 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimByte) |
| 111 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimChar) |
| 112 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimShort) |
| 113 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimInt) |
| 114 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimLong) |
| 115 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstanceObjectRead, Primitive::kPrimNot) |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 116 | |
| 117 | // sget-XXX |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 118 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimBoolean) |
| 119 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimByte) |
| 120 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimChar) |
| 121 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimShort) |
| 122 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimInt) |
| 123 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimLong) |
| 124 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticObjectRead, Primitive::kPrimNot) |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 125 | |
| 126 | #undef EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL |
| 127 | #undef EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL |
| 128 | |
| 129 | // Handles iget-quick, iget-wide-quick and iget-object-quick instructions. |
| 130 | // Returns true on success, otherwise throws an exception and returns false. |
| 131 | template<Primitive::Type field_type> |
| 132 | bool DoIGetQuick(ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) { |
| 133 | Object* obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data)); |
| 134 | if (UNLIKELY(obj == nullptr)) { |
| 135 | // We lost the reference to the field index so we cannot get a more |
| 136 | // precised exception message. |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 137 | ThrowNullPointerExceptionFromDexPC(); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 138 | return false; |
| 139 | } |
| 140 | MemberOffset field_offset(inst->VRegC_22c()); |
| 141 | // Report this field access to instrumentation if needed. Since we only have the offset of |
| 142 | // the field from the base of the object, we need to look for it first. |
| 143 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
| 144 | if (UNLIKELY(instrumentation->HasFieldReadListeners())) { |
| 145 | ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(), |
| 146 | field_offset.Uint32Value()); |
| 147 | DCHECK(f != nullptr); |
| 148 | DCHECK(!f->IsStatic()); |
| 149 | instrumentation->FieldReadEvent(Thread::Current(), obj, shadow_frame.GetMethod(), |
| 150 | shadow_frame.GetDexPC(), f); |
| 151 | } |
| 152 | // Note: iget-x-quick instructions are only for non-volatile fields. |
| 153 | const uint32_t vregA = inst->VRegA_22c(inst_data); |
| 154 | switch (field_type) { |
| 155 | case Primitive::kPrimInt: |
| 156 | shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetField32(field_offset))); |
| 157 | break; |
Mathieu Chartier | ffc605c | 2014-12-10 10:35:44 -0800 | [diff] [blame] | 158 | case Primitive::kPrimBoolean: |
| 159 | shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldBoolean(field_offset))); |
| 160 | break; |
| 161 | case Primitive::kPrimByte: |
| 162 | shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldByte(field_offset))); |
| 163 | break; |
| 164 | case Primitive::kPrimChar: |
| 165 | shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldChar(field_offset))); |
| 166 | break; |
| 167 | case Primitive::kPrimShort: |
| 168 | shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldShort(field_offset))); |
| 169 | break; |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 170 | case Primitive::kPrimLong: |
| 171 | shadow_frame.SetVRegLong(vregA, static_cast<int64_t>(obj->GetField64(field_offset))); |
| 172 | break; |
| 173 | case Primitive::kPrimNot: |
| 174 | shadow_frame.SetVRegReference(vregA, obj->GetFieldObject<mirror::Object>(field_offset)); |
| 175 | break; |
| 176 | default: |
| 177 | LOG(FATAL) << "Unreachable: " << field_type; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 178 | UNREACHABLE(); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 179 | } |
| 180 | return true; |
| 181 | } |
| 182 | |
| 183 | // Explicitly instantiate all DoIGetQuick functions. |
| 184 | #define EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(_field_type) \ |
| 185 | template bool DoIGetQuick<_field_type>(ShadowFrame& shadow_frame, const Instruction* inst, \ |
| 186 | uint16_t inst_data) |
| 187 | |
Mathieu Chartier | ffc605c | 2014-12-10 10:35:44 -0800 | [diff] [blame] | 188 | EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimInt); // iget-quick. |
| 189 | EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimBoolean); // iget-boolean-quick. |
| 190 | EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimByte); // iget-byte-quick. |
| 191 | EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimChar); // iget-char-quick. |
| 192 | EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimShort); // iget-short-quick. |
| 193 | EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimLong); // iget-wide-quick. |
| 194 | EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimNot); // iget-object-quick. |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 195 | #undef EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL |
| 196 | |
| 197 | template<Primitive::Type field_type> |
| 198 | static JValue GetFieldValue(const ShadowFrame& shadow_frame, uint32_t vreg) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 199 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 200 | JValue field_value; |
| 201 | switch (field_type) { |
| 202 | case Primitive::kPrimBoolean: |
| 203 | field_value.SetZ(static_cast<uint8_t>(shadow_frame.GetVReg(vreg))); |
| 204 | break; |
| 205 | case Primitive::kPrimByte: |
| 206 | field_value.SetB(static_cast<int8_t>(shadow_frame.GetVReg(vreg))); |
| 207 | break; |
| 208 | case Primitive::kPrimChar: |
| 209 | field_value.SetC(static_cast<uint16_t>(shadow_frame.GetVReg(vreg))); |
| 210 | break; |
| 211 | case Primitive::kPrimShort: |
| 212 | field_value.SetS(static_cast<int16_t>(shadow_frame.GetVReg(vreg))); |
| 213 | break; |
| 214 | case Primitive::kPrimInt: |
| 215 | field_value.SetI(shadow_frame.GetVReg(vreg)); |
| 216 | break; |
| 217 | case Primitive::kPrimLong: |
| 218 | field_value.SetJ(shadow_frame.GetVRegLong(vreg)); |
| 219 | break; |
| 220 | case Primitive::kPrimNot: |
| 221 | field_value.SetL(shadow_frame.GetVRegReference(vreg)); |
| 222 | break; |
| 223 | default: |
| 224 | LOG(FATAL) << "Unreachable: " << field_type; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 225 | UNREACHABLE(); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 226 | } |
| 227 | return field_value; |
| 228 | } |
| 229 | |
| 230 | template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check, |
| 231 | bool transaction_active> |
| 232 | bool DoFieldPut(Thread* self, const ShadowFrame& shadow_frame, const Instruction* inst, |
| 233 | uint16_t inst_data) { |
| 234 | bool do_assignability_check = do_access_check; |
| 235 | bool is_static = (find_type == StaticObjectWrite) || (find_type == StaticPrimitiveWrite); |
| 236 | uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c(); |
| 237 | ArtField* f = FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self, |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 238 | Primitive::ComponentSize(field_type)); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 239 | if (UNLIKELY(f == nullptr)) { |
| 240 | CHECK(self->IsExceptionPending()); |
| 241 | return false; |
| 242 | } |
| 243 | Object* obj; |
| 244 | if (is_static) { |
| 245 | obj = f->GetDeclaringClass(); |
| 246 | } else { |
| 247 | obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data)); |
| 248 | if (UNLIKELY(obj == nullptr)) { |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 249 | ThrowNullPointerExceptionForFieldAccess(f, false); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 250 | return false; |
| 251 | } |
| 252 | } |
Sebastien Hertz | 1edbd8e | 2014-07-16 20:00:11 +0200 | [diff] [blame] | 253 | f->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 254 | uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data); |
| 255 | // Report this field access to instrumentation if needed. Since we only have the offset of |
| 256 | // the field from the base of the object, we need to look for it first. |
| 257 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
| 258 | if (UNLIKELY(instrumentation->HasFieldWriteListeners())) { |
| 259 | JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA); |
| 260 | Object* this_object = f->IsStatic() ? nullptr : obj; |
| 261 | instrumentation->FieldWriteEvent(self, this_object, shadow_frame.GetMethod(), |
| 262 | shadow_frame.GetDexPC(), f, field_value); |
| 263 | } |
| 264 | switch (field_type) { |
| 265 | case Primitive::kPrimBoolean: |
| 266 | f->SetBoolean<transaction_active>(obj, shadow_frame.GetVReg(vregA)); |
| 267 | break; |
| 268 | case Primitive::kPrimByte: |
| 269 | f->SetByte<transaction_active>(obj, shadow_frame.GetVReg(vregA)); |
| 270 | break; |
| 271 | case Primitive::kPrimChar: |
| 272 | f->SetChar<transaction_active>(obj, shadow_frame.GetVReg(vregA)); |
| 273 | break; |
| 274 | case Primitive::kPrimShort: |
| 275 | f->SetShort<transaction_active>(obj, shadow_frame.GetVReg(vregA)); |
| 276 | break; |
| 277 | case Primitive::kPrimInt: |
| 278 | f->SetInt<transaction_active>(obj, shadow_frame.GetVReg(vregA)); |
| 279 | break; |
| 280 | case Primitive::kPrimLong: |
| 281 | f->SetLong<transaction_active>(obj, shadow_frame.GetVRegLong(vregA)); |
| 282 | break; |
| 283 | case Primitive::kPrimNot: { |
| 284 | Object* reg = shadow_frame.GetVRegReference(vregA); |
| 285 | if (do_assignability_check && reg != nullptr) { |
| 286 | // FieldHelper::GetType can resolve classes, use a handle wrapper which will restore the |
| 287 | // object in the destructor. |
| 288 | Class* field_class; |
| 289 | { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 290 | StackHandleScope<2> hs(self); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 291 | HandleWrapper<mirror::Object> h_reg(hs.NewHandleWrapper(®)); |
| 292 | HandleWrapper<mirror::Object> h_obj(hs.NewHandleWrapper(&obj)); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 293 | field_class = f->GetType<true>(); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 294 | } |
| 295 | if (!reg->VerifierInstanceOf(field_class)) { |
| 296 | // This should never happen. |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 297 | std::string temp1, temp2, temp3; |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 298 | self->ThrowNewExceptionF("Ljava/lang/VirtualMachineError;", |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 299 | "Put '%s' that is not instance of field '%s' in '%s'", |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 300 | reg->GetClass()->GetDescriptor(&temp1), |
| 301 | field_class->GetDescriptor(&temp2), |
| 302 | f->GetDeclaringClass()->GetDescriptor(&temp3)); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 303 | return false; |
| 304 | } |
| 305 | } |
| 306 | f->SetObj<transaction_active>(obj, reg); |
| 307 | break; |
| 308 | } |
| 309 | default: |
| 310 | LOG(FATAL) << "Unreachable: " << field_type; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 311 | UNREACHABLE(); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 312 | } |
| 313 | return true; |
| 314 | } |
| 315 | |
| 316 | // Explicitly instantiate all DoFieldPut functions. |
| 317 | #define EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, _do_check, _transaction_active) \ |
| 318 | template bool DoFieldPut<_find_type, _field_type, _do_check, _transaction_active>(Thread* self, \ |
| 319 | const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) |
| 320 | |
| 321 | #define EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(_find_type, _field_type) \ |
| 322 | EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, false); \ |
| 323 | EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, false); \ |
| 324 | EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, true); \ |
| 325 | EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, true); |
| 326 | |
| 327 | // iput-XXX |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 328 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimBoolean) |
| 329 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimByte) |
| 330 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimChar) |
| 331 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimShort) |
| 332 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimInt) |
| 333 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimLong) |
| 334 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstanceObjectWrite, Primitive::kPrimNot) |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 335 | |
| 336 | // sput-XXX |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 337 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimBoolean) |
| 338 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimByte) |
| 339 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimChar) |
| 340 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimShort) |
| 341 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimInt) |
| 342 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimLong) |
| 343 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticObjectWrite, Primitive::kPrimNot) |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 344 | |
| 345 | #undef EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL |
| 346 | #undef EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL |
| 347 | |
| 348 | template<Primitive::Type field_type, bool transaction_active> |
| 349 | bool DoIPutQuick(const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) { |
| 350 | Object* obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data)); |
| 351 | if (UNLIKELY(obj == nullptr)) { |
| 352 | // We lost the reference to the field index so we cannot get a more |
| 353 | // precised exception message. |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 354 | ThrowNullPointerExceptionFromDexPC(); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 355 | return false; |
| 356 | } |
| 357 | MemberOffset field_offset(inst->VRegC_22c()); |
| 358 | const uint32_t vregA = inst->VRegA_22c(inst_data); |
| 359 | // Report this field modification to instrumentation if needed. Since we only have the offset of |
| 360 | // the field from the base of the object, we need to look for it first. |
| 361 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
| 362 | if (UNLIKELY(instrumentation->HasFieldWriteListeners())) { |
| 363 | ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(), |
| 364 | field_offset.Uint32Value()); |
| 365 | DCHECK(f != nullptr); |
| 366 | DCHECK(!f->IsStatic()); |
| 367 | JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA); |
| 368 | instrumentation->FieldWriteEvent(Thread::Current(), obj, shadow_frame.GetMethod(), |
| 369 | shadow_frame.GetDexPC(), f, field_value); |
| 370 | } |
| 371 | // Note: iput-x-quick instructions are only for non-volatile fields. |
| 372 | switch (field_type) { |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 373 | case Primitive::kPrimBoolean: |
| 374 | obj->SetFieldBoolean<transaction_active>(field_offset, shadow_frame.GetVReg(vregA)); |
| 375 | break; |
| 376 | case Primitive::kPrimByte: |
| 377 | obj->SetFieldByte<transaction_active>(field_offset, shadow_frame.GetVReg(vregA)); |
| 378 | break; |
| 379 | case Primitive::kPrimChar: |
| 380 | obj->SetFieldChar<transaction_active>(field_offset, shadow_frame.GetVReg(vregA)); |
| 381 | break; |
| 382 | case Primitive::kPrimShort: |
| 383 | obj->SetFieldShort<transaction_active>(field_offset, shadow_frame.GetVReg(vregA)); |
| 384 | break; |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 385 | case Primitive::kPrimInt: |
| 386 | obj->SetField32<transaction_active>(field_offset, shadow_frame.GetVReg(vregA)); |
| 387 | break; |
| 388 | case Primitive::kPrimLong: |
| 389 | obj->SetField64<transaction_active>(field_offset, shadow_frame.GetVRegLong(vregA)); |
| 390 | break; |
| 391 | case Primitive::kPrimNot: |
| 392 | obj->SetFieldObject<transaction_active>(field_offset, shadow_frame.GetVRegReference(vregA)); |
| 393 | break; |
| 394 | default: |
| 395 | LOG(FATAL) << "Unreachable: " << field_type; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 396 | UNREACHABLE(); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 397 | } |
| 398 | return true; |
| 399 | } |
| 400 | |
| 401 | // Explicitly instantiate all DoIPutQuick functions. |
| 402 | #define EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, _transaction_active) \ |
| 403 | template bool DoIPutQuick<_field_type, _transaction_active>(const ShadowFrame& shadow_frame, \ |
| 404 | const Instruction* inst, \ |
| 405 | uint16_t inst_data) |
| 406 | |
| 407 | #define EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(_field_type) \ |
| 408 | EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, false); \ |
| 409 | EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, true); |
| 410 | |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 411 | EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimInt) // iput-quick. |
| 412 | EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimBoolean) // iput-boolean-quick. |
| 413 | EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimByte) // iput-byte-quick. |
| 414 | EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimChar) // iput-char-quick. |
| 415 | EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimShort) // iput-short-quick. |
| 416 | EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimLong) // iput-wide-quick. |
| 417 | EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimNot) // iput-object-quick. |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 418 | #undef EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL |
| 419 | #undef EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL |
| 420 | |
Sebastien Hertz | 520633b | 2015-09-08 17:03:36 +0200 | [diff] [blame] | 421 | // We accept a null Instrumentation* meaning we must not report anything to the instrumentation. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 422 | uint32_t FindNextInstructionFollowingException( |
| 423 | Thread* self, ShadowFrame& shadow_frame, uint32_t dex_pc, |
| 424 | const instrumentation::Instrumentation* instrumentation) { |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 425 | self->VerifyStack(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 426 | StackHandleScope<2> hs(self); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 427 | Handle<mirror::Throwable> exception(hs.NewHandle(self->GetException())); |
Sebastien Hertz | 520633b | 2015-09-08 17:03:36 +0200 | [diff] [blame] | 428 | if (instrumentation != nullptr && instrumentation->HasExceptionCaughtListeners() |
Nicolas Geoffray | 7642cfc | 2015-02-26 10:56:09 +0000 | [diff] [blame] | 429 | && self->IsExceptionThrownByCurrentMethod(exception.Get())) { |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 430 | instrumentation->ExceptionCaughtEvent(self, exception.Get()); |
Sebastien Hertz | 9f10203 | 2014-05-23 08:59:42 +0200 | [diff] [blame] | 431 | } |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 432 | bool clear_exception = false; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 433 | uint32_t found_dex_pc = shadow_frame.GetMethod()->FindCatchBlock( |
| 434 | hs.NewHandle(exception->GetClass()), dex_pc, &clear_exception); |
Sebastien Hertz | 520633b | 2015-09-08 17:03:36 +0200 | [diff] [blame] | 435 | if (found_dex_pc == DexFile::kDexNoIndex && instrumentation != nullptr) { |
Nicolas Geoffray | 7642cfc | 2015-02-26 10:56:09 +0000 | [diff] [blame] | 436 | // Exception is not caught by the current method. We will unwind to the |
| 437 | // caller. Notify any instrumentation listener. |
Sebastien Hertz | 9f10203 | 2014-05-23 08:59:42 +0200 | [diff] [blame] | 438 | instrumentation->MethodUnwindEvent(self, shadow_frame.GetThisObject(), |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 439 | shadow_frame.GetMethod(), dex_pc); |
| 440 | } else { |
Nicolas Geoffray | 7642cfc | 2015-02-26 10:56:09 +0000 | [diff] [blame] | 441 | // Exception is caught in the current method. We will jump to the found_dex_pc. |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 442 | if (clear_exception) { |
| 443 | self->ClearException(); |
| 444 | } |
| 445 | } |
| 446 | return found_dex_pc; |
| 447 | } |
| 448 | |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 449 | void UnexpectedOpcode(const Instruction* inst, const ShadowFrame& shadow_frame) { |
| 450 | LOG(FATAL) << "Unexpected instruction: " |
| 451 | << inst->DumpString(shadow_frame.GetMethod()->GetDexFile()); |
| 452 | UNREACHABLE(); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 453 | } |
| 454 | |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 455 | // Assign register 'src_reg' from shadow_frame to register 'dest_reg' into new_shadow_frame. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 456 | static inline void AssignRegister(ShadowFrame* new_shadow_frame, const ShadowFrame& shadow_frame, |
| 457 | size_t dest_reg, size_t src_reg) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 458 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Andreas Gampe | 7104cbf | 2014-03-21 11:44:43 -0700 | [diff] [blame] | 459 | // Uint required, so that sign extension does not make this wrong on 64b systems |
| 460 | uint32_t src_value = shadow_frame.GetVReg(src_reg); |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 461 | mirror::Object* o = shadow_frame.GetVRegReference<kVerifyNone>(src_reg); |
Igor Murashkin | c449e8b | 2015-06-10 15:56:42 -0700 | [diff] [blame] | 462 | |
| 463 | // If both register locations contains the same value, the register probably holds a reference. |
| 464 | // Note: As an optimization, non-moving collectors leave a stale reference value |
| 465 | // in the references array even after the original vreg was overwritten to a non-reference. |
Andreas Gampe | 7104cbf | 2014-03-21 11:44:43 -0700 | [diff] [blame] | 466 | if (src_value == reinterpret_cast<uintptr_t>(o)) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 467 | new_shadow_frame->SetVRegReference(dest_reg, o); |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 468 | } else { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 469 | new_shadow_frame->SetVReg(dest_reg, src_value); |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 470 | } |
| 471 | } |
| 472 | |
Sebastien Hertz | 45b1597 | 2015-04-03 16:07:05 +0200 | [diff] [blame] | 473 | void AbortTransactionF(Thread* self, const char* fmt, ...) { |
Mathieu Chartier | b2c7ead | 2014-04-29 11:13:16 -0700 | [diff] [blame] | 474 | va_list args; |
| 475 | va_start(args, fmt); |
Sebastien Hertz | 45b1597 | 2015-04-03 16:07:05 +0200 | [diff] [blame] | 476 | AbortTransactionV(self, fmt, args); |
| 477 | va_end(args); |
| 478 | } |
| 479 | |
| 480 | void AbortTransactionV(Thread* self, const char* fmt, va_list args) { |
| 481 | CHECK(Runtime::Current()->IsActiveTransaction()); |
| 482 | // Constructs abort message. |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 483 | std::string abort_msg; |
| 484 | StringAppendV(&abort_msg, fmt, args); |
| 485 | // Throws an exception so we can abort the transaction and rollback every change. |
Sebastien Hertz | 2fd7e69 | 2015-04-02 11:11:19 +0200 | [diff] [blame] | 486 | Runtime::Current()->AbortTransactionAndThrowAbortError(self, abort_msg); |
Mathieu Chartier | b2c7ead | 2014-04-29 11:13:16 -0700 | [diff] [blame] | 487 | } |
| 488 | |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 489 | // Separate declaration is required solely for the attributes. |
Igor Murashkin | 6918bf1 | 2015-09-27 19:19:06 -0700 | [diff] [blame] | 490 | template <bool is_range, |
| 491 | bool do_assignability_check, |
| 492 | size_t kVarArgMax> |
| 493 | SHARED_REQUIRES(Locks::mutator_lock_) |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 494 | static inline bool DoCallCommon(ArtMethod* called_method, |
| 495 | Thread* self, |
| 496 | ShadowFrame& shadow_frame, |
| 497 | JValue* result, |
| 498 | uint16_t number_of_inputs, |
Igor Murashkin | 6918bf1 | 2015-09-27 19:19:06 -0700 | [diff] [blame] | 499 | uint32_t (&arg)[kVarArgMax], |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 500 | uint32_t vregC) ALWAYS_INLINE; |
| 501 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 502 | SHARED_REQUIRES(Locks::mutator_lock_) |
Nicolas Geoffray | 7bf2b4f | 2015-07-08 10:11:59 +0000 | [diff] [blame] | 503 | static inline bool NeedsInterpreter(Thread* self, ShadowFrame* new_shadow_frame) ALWAYS_INLINE; |
| 504 | |
| 505 | static inline bool NeedsInterpreter(Thread* self, ShadowFrame* new_shadow_frame) { |
| 506 | ArtMethod* target = new_shadow_frame->GetMethod(); |
| 507 | if (UNLIKELY(target->IsNative() || target->IsProxyMethod())) { |
| 508 | return false; |
| 509 | } |
| 510 | Runtime* runtime = Runtime::Current(); |
| 511 | ClassLinker* class_linker = runtime->GetClassLinker(); |
| 512 | return runtime->GetInstrumentation()->IsForcedInterpretOnly() || |
| 513 | // Doing this check avoids doing compiled/interpreter transitions. |
| 514 | class_linker->IsQuickToInterpreterBridge(target->GetEntryPointFromQuickCompiledCode()) || |
| 515 | // Force the use of interpreter when it is required by the debugger. |
| 516 | Dbg::IsForcedInterpreterNeededForCalling(self, target); |
| 517 | } |
| 518 | |
Andreas Gampe | 3cfa4d0 | 2015-10-06 17:04:01 -0700 | [diff] [blame] | 519 | static void ArtInterpreterToCompiledCodeBridge(Thread* self, |
| 520 | const DexFile::CodeItem* code_item, |
| 521 | ShadowFrame* shadow_frame, |
| 522 | JValue* result) |
| 523 | SHARED_REQUIRES(Locks::mutator_lock_) { |
| 524 | ArtMethod* method = shadow_frame->GetMethod(); |
| 525 | // Ensure static methods are initialized. |
| 526 | if (method->IsStatic()) { |
| 527 | mirror::Class* declaringClass = method->GetDeclaringClass(); |
| 528 | if (UNLIKELY(!declaringClass->IsInitialized())) { |
| 529 | self->PushShadowFrame(shadow_frame); |
| 530 | StackHandleScope<1> hs(self); |
| 531 | Handle<mirror::Class> h_class(hs.NewHandle(declaringClass)); |
| 532 | if (UNLIKELY(!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, |
| 533 | true))) { |
| 534 | self->PopShadowFrame(); |
| 535 | DCHECK(self->IsExceptionPending()); |
| 536 | return; |
| 537 | } |
| 538 | self->PopShadowFrame(); |
| 539 | CHECK(h_class->IsInitializing()); |
| 540 | // Reload from shadow frame in case the method moved, this is faster than adding a handle. |
| 541 | method = shadow_frame->GetMethod(); |
| 542 | } |
| 543 | } |
| 544 | uint16_t arg_offset = (code_item == nullptr) |
| 545 | ? 0 |
| 546 | : code_item->registers_size_ - code_item->ins_size_; |
| 547 | method->Invoke(self, shadow_frame->GetVRegArgs(arg_offset), |
| 548 | (shadow_frame->NumberOfVRegs() - arg_offset) * sizeof(uint32_t), |
| 549 | result, method->GetInterfaceMethodIfProxy(sizeof(void*))->GetShorty()); |
| 550 | } |
| 551 | |
Igor Murashkin | 6918bf1 | 2015-09-27 19:19:06 -0700 | [diff] [blame] | 552 | template <bool is_range, |
| 553 | bool do_assignability_check, |
| 554 | size_t kVarArgMax> |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 555 | static inline bool DoCallCommon(ArtMethod* called_method, |
| 556 | Thread* self, |
| 557 | ShadowFrame& shadow_frame, |
| 558 | JValue* result, |
| 559 | uint16_t number_of_inputs, |
Igor Murashkin | 6918bf1 | 2015-09-27 19:19:06 -0700 | [diff] [blame] | 560 | uint32_t (&arg)[kVarArgMax], |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 561 | uint32_t vregC) { |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 562 | bool string_init = false; |
| 563 | // Replace calls to String.<init> with equivalent StringFactory call. |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 564 | if (UNLIKELY(called_method->GetDeclaringClass()->IsStringClass() |
| 565 | && called_method->IsConstructor())) { |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 566 | ScopedObjectAccessUnchecked soa(self); |
| 567 | jmethodID mid = soa.EncodeMethod(called_method); |
| 568 | called_method = soa.DecodeMethod(WellKnownClasses::StringInitToStringFactoryMethodID(mid)); |
| 569 | string_init = true; |
| 570 | } |
| 571 | |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 572 | // Compute method information. |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 573 | const DexFile::CodeItem* code_item = called_method->GetCodeItem(); |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 574 | |
| 575 | // Number of registers for the callee's call frame. |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 576 | uint16_t num_regs; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 577 | if (LIKELY(code_item != nullptr)) { |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 578 | num_regs = code_item->registers_size_; |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 579 | DCHECK_EQ(string_init ? number_of_inputs - 1 : number_of_inputs, code_item->ins_size_); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 580 | } else { |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 581 | DCHECK(called_method->IsNative() || called_method->IsProxyMethod()); |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 582 | num_regs = number_of_inputs; |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 583 | } |
| 584 | |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 585 | // Hack for String init: |
| 586 | // |
| 587 | // Rewrite invoke-x java.lang.String.<init>(this, a, b, c, ...) into: |
| 588 | // invoke-x StringFactory(a, b, c, ...) |
| 589 | // by effectively dropping the first virtual register from the invoke. |
| 590 | // |
| 591 | // (at this point the ArtMethod has already been replaced, |
| 592 | // so we just need to fix-up the arguments) |
| 593 | uint32_t string_init_vreg_this = is_range ? vregC : arg[0]; |
Igor Murashkin | a06b49b | 2015-06-25 15:18:12 -0700 | [diff] [blame] | 594 | if (UNLIKELY(string_init)) { |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 595 | DCHECK_GT(num_regs, 0u); // As the method is an instance method, there should be at least 1. |
Igor Murashkin | a06b49b | 2015-06-25 15:18:12 -0700 | [diff] [blame] | 596 | |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 597 | // The new StringFactory call is static and has one fewer argument. |
Igor Murashkin | a06b49b | 2015-06-25 15:18:12 -0700 | [diff] [blame] | 598 | if (code_item == nullptr) { |
| 599 | DCHECK(called_method->IsNative() || called_method->IsProxyMethod()); |
| 600 | num_regs--; |
| 601 | } // else ... don't need to change num_regs since it comes up from the string_init's code item |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 602 | number_of_inputs--; |
| 603 | |
| 604 | // Rewrite the var-args, dropping the 0th argument ("this") |
Igor Murashkin | 6918bf1 | 2015-09-27 19:19:06 -0700 | [diff] [blame] | 605 | for (uint32_t i = 1; i < arraysize(arg); ++i) { |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 606 | arg[i - 1] = arg[i]; |
| 607 | } |
Igor Murashkin | 6918bf1 | 2015-09-27 19:19:06 -0700 | [diff] [blame] | 608 | arg[arraysize(arg) - 1] = 0; |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 609 | |
| 610 | // Rewrite the non-var-arg case |
| 611 | vregC++; // Skips the 0th vreg in the range ("this"). |
| 612 | } |
| 613 | |
| 614 | // Parameter registers go at the end of the shadow frame. |
| 615 | DCHECK_GE(num_regs, number_of_inputs); |
| 616 | size_t first_dest_reg = num_regs - number_of_inputs; |
| 617 | DCHECK_NE(first_dest_reg, (size_t)-1); |
| 618 | |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 619 | // Allocate shadow frame on the stack. |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 620 | const char* old_cause = self->StartAssertNoThreadSuspension("DoCallCommon"); |
Andreas Gampe | b302592 | 2015-09-01 14:45:00 -0700 | [diff] [blame] | 621 | ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr = |
| 622 | CREATE_SHADOW_FRAME(num_regs, &shadow_frame, called_method, 0); |
| 623 | ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get(); |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 624 | |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 625 | // Initialize new shadow frame by copying the registers from the callee shadow frame. |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 626 | if (do_assignability_check) { |
Andreas Gampe | 2a0d4ec | 2014-06-02 22:05:22 -0700 | [diff] [blame] | 627 | // Slow path. |
| 628 | // We might need to do class loading, which incurs a thread state change to kNative. So |
| 629 | // register the shadow frame as under construction and allow suspension again. |
Mingyao Yang | 1f2d3ba | 2015-05-18 12:12:50 -0700 | [diff] [blame] | 630 | ScopedStackedShadowFramePusher pusher( |
Sebastien Hertz | f795869 | 2015-06-09 14:09:14 +0200 | [diff] [blame] | 631 | self, new_shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction); |
Andreas Gampe | 2a0d4ec | 2014-06-02 22:05:22 -0700 | [diff] [blame] | 632 | self->EndAssertNoThreadSuspension(old_cause); |
| 633 | |
| 634 | // We need to do runtime check on reference assignment. We need to load the shorty |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 635 | // to get the exact type of each reference argument. |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 636 | const DexFile::TypeList* params = new_shadow_frame->GetMethod()->GetParameterTypeList(); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 637 | uint32_t shorty_len = 0; |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 638 | const char* shorty = new_shadow_frame->GetMethod()->GetShorty(&shorty_len); |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 639 | |
Sebastien Hertz | 9119c5f | 2013-12-16 11:31:45 +0100 | [diff] [blame] | 640 | // Handle receiver apart since it's not part of the shorty. |
| 641 | size_t dest_reg = first_dest_reg; |
| 642 | size_t arg_offset = 0; |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 643 | |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 644 | if (!new_shadow_frame->GetMethod()->IsStatic()) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 645 | size_t receiver_reg = is_range ? vregC : arg[0]; |
Sebastien Hertz | 9119c5f | 2013-12-16 11:31:45 +0100 | [diff] [blame] | 646 | new_shadow_frame->SetVRegReference(dest_reg, shadow_frame.GetVRegReference(receiver_reg)); |
| 647 | ++dest_reg; |
| 648 | ++arg_offset; |
Igor Murashkin | a06b49b | 2015-06-25 15:18:12 -0700 | [diff] [blame] | 649 | DCHECK(!string_init); // All StringFactory methods are static. |
Sebastien Hertz | 9119c5f | 2013-12-16 11:31:45 +0100 | [diff] [blame] | 650 | } |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 651 | |
| 652 | // Copy the caller's invoke-* arguments into the callee's parameter registers. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 653 | for (uint32_t shorty_pos = 0; dest_reg < num_regs; ++shorty_pos, ++dest_reg, ++arg_offset) { |
Igor Murashkin | a06b49b | 2015-06-25 15:18:12 -0700 | [diff] [blame] | 654 | // Skip the 0th 'shorty' type since it represents the return type. |
| 655 | DCHECK_LT(shorty_pos + 1, shorty_len) << "for shorty '" << shorty << "'"; |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 656 | const size_t src_reg = (is_range) ? vregC + arg_offset : arg[arg_offset]; |
| 657 | switch (shorty[shorty_pos + 1]) { |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 658 | // Handle Object references. 1 virtual register slot. |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 659 | case 'L': { |
| 660 | Object* o = shadow_frame.GetVRegReference(src_reg); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 661 | if (do_assignability_check && o != nullptr) { |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 662 | size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize(); |
Ian Rogers | a048560 | 2014-12-02 15:48:04 -0800 | [diff] [blame] | 663 | Class* arg_type = |
| 664 | new_shadow_frame->GetMethod()->GetClassFromTypeIndex( |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 665 | params->GetTypeItem(shorty_pos).type_idx_, true /* resolve */, pointer_size); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 666 | if (arg_type == nullptr) { |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 667 | CHECK(self->IsExceptionPending()); |
| 668 | return false; |
| 669 | } |
| 670 | if (!o->VerifierInstanceOf(arg_type)) { |
| 671 | // This should never happen. |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 672 | std::string temp1, temp2; |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 673 | self->ThrowNewExceptionF("Ljava/lang/VirtualMachineError;", |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 674 | "Invoking %s with bad arg %d, type '%s' not instance of '%s'", |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 675 | new_shadow_frame->GetMethod()->GetName(), shorty_pos, |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 676 | o->GetClass()->GetDescriptor(&temp1), |
| 677 | arg_type->GetDescriptor(&temp2)); |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 678 | return false; |
| 679 | } |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 680 | } |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 681 | new_shadow_frame->SetVRegReference(dest_reg, o); |
| 682 | break; |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 683 | } |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 684 | // Handle doubles and longs. 2 consecutive virtual register slots. |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 685 | case 'J': case 'D': { |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 686 | uint64_t wide_value = |
| 687 | (static_cast<uint64_t>(shadow_frame.GetVReg(src_reg + 1)) << BitSizeOf<uint32_t>()) | |
| 688 | static_cast<uint32_t>(shadow_frame.GetVReg(src_reg)); |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 689 | new_shadow_frame->SetVRegLong(dest_reg, wide_value); |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 690 | // Skip the next virtual register slot since we already used it. |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 691 | ++dest_reg; |
| 692 | ++arg_offset; |
| 693 | break; |
| 694 | } |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 695 | // Handle all other primitives that are always 1 virtual register slot. |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 696 | default: |
| 697 | new_shadow_frame->SetVReg(dest_reg, shadow_frame.GetVReg(src_reg)); |
| 698 | break; |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 699 | } |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 700 | } |
| 701 | } else { |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 702 | size_t arg_index = 0; |
| 703 | |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 704 | // Fast path: no extra checks. |
| 705 | if (is_range) { |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 706 | // TODO: Implement the range version of invoke-lambda |
| 707 | uint16_t first_src_reg = vregC; |
| 708 | |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 709 | for (size_t src_reg = first_src_reg, dest_reg = first_dest_reg; dest_reg < num_regs; |
| 710 | ++dest_reg, ++src_reg) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 711 | AssignRegister(new_shadow_frame, shadow_frame, dest_reg, src_reg); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 712 | } |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 713 | } else { |
Igor Murashkin | 6918bf1 | 2015-09-27 19:19:06 -0700 | [diff] [blame] | 714 | DCHECK_LE(number_of_inputs, arraysize(arg)); |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 715 | |
| 716 | for (; arg_index < number_of_inputs; ++arg_index) { |
| 717 | AssignRegister(new_shadow_frame, shadow_frame, first_dest_reg + arg_index, arg[arg_index]); |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 718 | } |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 719 | } |
Andreas Gampe | 2a0d4ec | 2014-06-02 22:05:22 -0700 | [diff] [blame] | 720 | self->EndAssertNoThreadSuspension(old_cause); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 721 | } |
| 722 | |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 723 | // Do the call now. |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 724 | if (LIKELY(Runtime::Current()->IsStarted())) { |
Nicolas Geoffray | 7bf2b4f | 2015-07-08 10:11:59 +0000 | [diff] [blame] | 725 | if (NeedsInterpreter(self, new_shadow_frame)) { |
Andreas Gampe | 3cfa4d0 | 2015-10-06 17:04:01 -0700 | [diff] [blame] | 726 | ArtInterpreterToInterpreterBridge(self, code_item, new_shadow_frame, result); |
Nicolas Geoffray | 7070ccd | 2015-07-08 09:41:54 +0000 | [diff] [blame] | 727 | } else { |
Andreas Gampe | 3cfa4d0 | 2015-10-06 17:04:01 -0700 | [diff] [blame] | 728 | ArtInterpreterToCompiledCodeBridge(self, code_item, new_shadow_frame, result); |
Nicolas Geoffray | 7070ccd | 2015-07-08 09:41:54 +0000 | [diff] [blame] | 729 | } |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 730 | } else { |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 731 | UnstartedRuntime::Invoke(self, code_item, new_shadow_frame, result, first_dest_reg); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 732 | } |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 733 | |
| 734 | if (string_init && !self->IsExceptionPending()) { |
| 735 | // Set the new string result of the StringFactory. |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 736 | shadow_frame.SetVRegReference(string_init_vreg_this, result->GetL()); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 737 | // Overwrite all potential copies of the original result of the new-instance of string with the |
| 738 | // new result of the StringFactory. Use the verifier to find this set of registers. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 739 | ArtMethod* method = shadow_frame.GetMethod(); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 740 | MethodReference method_ref = method->ToMethodReference(); |
Jeff Hao | 4686c52 | 2015-09-18 14:44:32 -0700 | [diff] [blame] | 741 | SafeMap<uint32_t, std::set<uint32_t>>* string_init_map_ptr = nullptr; |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 742 | MethodRefToStringInitRegMap& method_to_string_init_map = Runtime::Current()->GetStringInitMap(); |
Jeff Hao | a2c3864 | 2015-09-17 17:29:01 -0700 | [diff] [blame] | 743 | { |
| 744 | MutexLock mu(self, *Locks::interpreter_string_init_map_lock_); |
Jeff Hao | 4686c52 | 2015-09-18 14:44:32 -0700 | [diff] [blame] | 745 | auto it = method_to_string_init_map.find(method_ref); |
| 746 | if (it != method_to_string_init_map.end()) { |
| 747 | string_init_map_ptr = &it->second; |
Jeff Hao | a2c3864 | 2015-09-17 17:29:01 -0700 | [diff] [blame] | 748 | } |
Jeff Hao | 4686c52 | 2015-09-18 14:44:32 -0700 | [diff] [blame] | 749 | } |
| 750 | if (string_init_map_ptr == nullptr) { |
| 751 | SafeMap<uint32_t, std::set<uint32_t>> string_init_map = |
| 752 | verifier::MethodVerifier::FindStringInitMap(method); |
| 753 | MutexLock mu(self, *Locks::interpreter_string_init_map_lock_); |
Vladimir Marko | 7856835 | 2015-09-21 12:00:16 +0100 | [diff] [blame] | 754 | auto it = method_to_string_init_map.lower_bound(method_ref); |
| 755 | if (it == method_to_string_init_map.end() || |
| 756 | method_to_string_init_map.key_comp()(method_ref, it->first)) { |
| 757 | it = method_to_string_init_map.PutBefore(it, method_ref, std::move(string_init_map)); |
| 758 | } |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 759 | string_init_map_ptr = &it->second; |
| 760 | } |
| 761 | if (string_init_map_ptr->size() != 0) { |
| 762 | uint32_t dex_pc = shadow_frame.GetDexPC(); |
| 763 | auto map_it = string_init_map_ptr->find(dex_pc); |
| 764 | if (map_it != string_init_map_ptr->end()) { |
| 765 | const std::set<uint32_t>& reg_set = map_it->second; |
| 766 | for (auto set_it = reg_set.begin(); set_it != reg_set.end(); ++set_it) { |
| 767 | shadow_frame.SetVRegReference(*set_it, result->GetL()); |
| 768 | } |
| 769 | } |
| 770 | } |
| 771 | } |
| 772 | |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 773 | return !self->IsExceptionPending(); |
| 774 | } |
| 775 | |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 776 | template<bool is_range, bool do_assignability_check> |
| 777 | bool DoLambdaCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame, |
| 778 | const Instruction* inst, uint16_t inst_data, JValue* result) { |
| 779 | const uint4_t num_additional_registers = inst->VRegB_25x(); |
| 780 | // Argument word count. |
Igor Murashkin | 6918bf1 | 2015-09-27 19:19:06 -0700 | [diff] [blame] | 781 | const uint16_t number_of_inputs = num_additional_registers + kLambdaVirtualRegisterWidth; |
| 782 | // The lambda closure register is always present and is not encoded in the count. |
| 783 | // Furthermore, the lambda closure register is always wide, so it counts as 2 inputs. |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 784 | |
| 785 | // TODO: find a cleaner way to separate non-range and range information without duplicating |
| 786 | // code. |
Igor Murashkin | 6918bf1 | 2015-09-27 19:19:06 -0700 | [diff] [blame] | 787 | uint32_t arg[Instruction::kMaxVarArgRegs25x]; // only used in invoke-XXX. |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 788 | uint32_t vregC = 0; // only used in invoke-XXX-range. |
| 789 | if (is_range) { |
| 790 | vregC = inst->VRegC_3rc(); |
| 791 | } else { |
| 792 | // TODO(iam): See if it's possible to remove inst_data dependency from 35x to avoid this path |
| 793 | UNUSED(inst_data); |
| 794 | inst->GetAllArgs25x(arg); |
| 795 | } |
| 796 | |
| 797 | // TODO: if there's an assignability check, throw instead? |
| 798 | DCHECK(called_method->IsStatic()); |
| 799 | |
| 800 | return DoCallCommon<is_range, do_assignability_check>( |
| 801 | called_method, self, shadow_frame, |
| 802 | result, number_of_inputs, arg, vregC); |
| 803 | } |
| 804 | |
| 805 | template<bool is_range, bool do_assignability_check> |
| 806 | bool DoCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame, |
| 807 | const Instruction* inst, uint16_t inst_data, JValue* result) { |
| 808 | // Argument word count. |
| 809 | const uint16_t number_of_inputs = (is_range) ? inst->VRegA_3rc(inst_data) : inst->VRegA_35c(inst_data); |
| 810 | |
| 811 | // TODO: find a cleaner way to separate non-range and range information without duplicating |
| 812 | // code. |
Igor Murashkin | 6918bf1 | 2015-09-27 19:19:06 -0700 | [diff] [blame] | 813 | uint32_t arg[Instruction::kMaxVarArgRegs] = {}; // only used in invoke-XXX. |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 814 | uint32_t vregC = 0; |
| 815 | if (is_range) { |
| 816 | vregC = inst->VRegC_3rc(); |
| 817 | } else { |
| 818 | vregC = inst->VRegC_35c(); |
| 819 | inst->GetVarArgs(arg, inst_data); |
| 820 | } |
| 821 | |
| 822 | return DoCallCommon<is_range, do_assignability_check>( |
| 823 | called_method, self, shadow_frame, |
| 824 | result, number_of_inputs, arg, vregC); |
| 825 | } |
| 826 | |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 827 | template <bool is_range, bool do_access_check, bool transaction_active> |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 828 | bool DoFilledNewArray(const Instruction* inst, const ShadowFrame& shadow_frame, |
| 829 | Thread* self, JValue* result) { |
| 830 | DCHECK(inst->Opcode() == Instruction::FILLED_NEW_ARRAY || |
| 831 | inst->Opcode() == Instruction::FILLED_NEW_ARRAY_RANGE); |
| 832 | const int32_t length = is_range ? inst->VRegA_3rc() : inst->VRegA_35c(); |
| 833 | if (!is_range) { |
| 834 | // Checks FILLED_NEW_ARRAY's length does not exceed 5 arguments. |
| 835 | CHECK_LE(length, 5); |
| 836 | } |
| 837 | if (UNLIKELY(length < 0)) { |
| 838 | ThrowNegativeArraySizeException(length); |
| 839 | return false; |
| 840 | } |
| 841 | uint16_t type_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c(); |
Mathieu Chartier | 52ea33b | 2015-06-18 16:48:52 -0700 | [diff] [blame] | 842 | Class* array_class = ResolveVerifyAndClinit(type_idx, shadow_frame.GetMethod(), |
| 843 | self, false, do_access_check); |
| 844 | if (UNLIKELY(array_class == nullptr)) { |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 845 | DCHECK(self->IsExceptionPending()); |
| 846 | return false; |
| 847 | } |
Mathieu Chartier | 52ea33b | 2015-06-18 16:48:52 -0700 | [diff] [blame] | 848 | CHECK(array_class->IsArrayClass()); |
| 849 | Class* component_class = array_class->GetComponentType(); |
| 850 | const bool is_primitive_int_component = component_class->IsPrimitiveInt(); |
| 851 | if (UNLIKELY(component_class->IsPrimitive() && !is_primitive_int_component)) { |
| 852 | if (component_class->IsPrimitiveLong() || component_class->IsPrimitiveDouble()) { |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 853 | ThrowRuntimeException("Bad filled array request for type %s", |
Mathieu Chartier | 52ea33b | 2015-06-18 16:48:52 -0700 | [diff] [blame] | 854 | PrettyDescriptor(component_class).c_str()); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 855 | } else { |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 856 | self->ThrowNewExceptionF("Ljava/lang/InternalError;", |
Brian Carlstrom | 4fa0bcd | 2013-12-10 11:24:21 -0800 | [diff] [blame] | 857 | "Found type %s; filled-new-array not implemented for anything but 'int'", |
Mathieu Chartier | 52ea33b | 2015-06-18 16:48:52 -0700 | [diff] [blame] | 858 | PrettyDescriptor(component_class).c_str()); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 859 | } |
| 860 | return false; |
| 861 | } |
Mathieu Chartier | 52ea33b | 2015-06-18 16:48:52 -0700 | [diff] [blame] | 862 | Object* new_array = Array::Alloc<true>(self, array_class, length, |
| 863 | array_class->GetComponentSizeShift(), |
| 864 | Runtime::Current()->GetHeap()->GetCurrentAllocator()); |
| 865 | if (UNLIKELY(new_array == nullptr)) { |
| 866 | self->AssertPendingOOMException(); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 867 | return false; |
| 868 | } |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 869 | uint32_t arg[Instruction::kMaxVarArgRegs]; // only used in filled-new-array. |
| 870 | uint32_t vregC = 0; // only used in filled-new-array-range. |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 871 | if (is_range) { |
Sebastien Hertz | abff643 | 2014-01-27 18:01:39 +0100 | [diff] [blame] | 872 | vregC = inst->VRegC_3rc(); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 873 | } else { |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 874 | inst->GetVarArgs(arg); |
Sebastien Hertz | abff643 | 2014-01-27 18:01:39 +0100 | [diff] [blame] | 875 | } |
Sebastien Hertz | abff643 | 2014-01-27 18:01:39 +0100 | [diff] [blame] | 876 | for (int32_t i = 0; i < length; ++i) { |
| 877 | size_t src_reg = is_range ? vregC + i : arg[i]; |
| 878 | if (is_primitive_int_component) { |
Mathieu Chartier | 52ea33b | 2015-06-18 16:48:52 -0700 | [diff] [blame] | 879 | new_array->AsIntArray()->SetWithoutChecks<transaction_active>( |
| 880 | i, shadow_frame.GetVReg(src_reg)); |
Sebastien Hertz | abff643 | 2014-01-27 18:01:39 +0100 | [diff] [blame] | 881 | } else { |
Mathieu Chartier | 52ea33b | 2015-06-18 16:48:52 -0700 | [diff] [blame] | 882 | new_array->AsObjectArray<Object>()->SetWithoutChecks<transaction_active>( |
| 883 | i, shadow_frame.GetVRegReference(src_reg)); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 884 | } |
| 885 | } |
| 886 | |
Mathieu Chartier | 52ea33b | 2015-06-18 16:48:52 -0700 | [diff] [blame] | 887 | result->SetL(new_array); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 888 | return true; |
| 889 | } |
| 890 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 891 | // TODO fix thread analysis: should be SHARED_REQUIRES(Locks::mutator_lock_). |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 892 | template<typename T> |
| 893 | static void RecordArrayElementsInTransactionImpl(mirror::PrimitiveArray<T>* array, int32_t count) |
| 894 | NO_THREAD_SAFETY_ANALYSIS { |
| 895 | Runtime* runtime = Runtime::Current(); |
| 896 | for (int32_t i = 0; i < count; ++i) { |
| 897 | runtime->RecordWriteArray(array, i, array->GetWithoutChecks(i)); |
| 898 | } |
| 899 | } |
| 900 | |
| 901 | void RecordArrayElementsInTransaction(mirror::Array* array, int32_t count) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 902 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 903 | DCHECK(Runtime::Current()->IsActiveTransaction()); |
| 904 | DCHECK(array != nullptr); |
| 905 | DCHECK_LE(count, array->GetLength()); |
| 906 | Primitive::Type primitive_component_type = array->GetClass()->GetComponentType()->GetPrimitiveType(); |
| 907 | switch (primitive_component_type) { |
| 908 | case Primitive::kPrimBoolean: |
| 909 | RecordArrayElementsInTransactionImpl(array->AsBooleanArray(), count); |
| 910 | break; |
| 911 | case Primitive::kPrimByte: |
| 912 | RecordArrayElementsInTransactionImpl(array->AsByteArray(), count); |
| 913 | break; |
| 914 | case Primitive::kPrimChar: |
| 915 | RecordArrayElementsInTransactionImpl(array->AsCharArray(), count); |
| 916 | break; |
| 917 | case Primitive::kPrimShort: |
| 918 | RecordArrayElementsInTransactionImpl(array->AsShortArray(), count); |
| 919 | break; |
| 920 | case Primitive::kPrimInt: |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 921 | RecordArrayElementsInTransactionImpl(array->AsIntArray(), count); |
| 922 | break; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 923 | case Primitive::kPrimFloat: |
| 924 | RecordArrayElementsInTransactionImpl(array->AsFloatArray(), count); |
| 925 | break; |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 926 | case Primitive::kPrimLong: |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 927 | RecordArrayElementsInTransactionImpl(array->AsLongArray(), count); |
| 928 | break; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 929 | case Primitive::kPrimDouble: |
| 930 | RecordArrayElementsInTransactionImpl(array->AsDoubleArray(), count); |
| 931 | break; |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 932 | default: |
| 933 | LOG(FATAL) << "Unsupported primitive type " << primitive_component_type |
| 934 | << " in fill-array-data"; |
| 935 | break; |
| 936 | } |
| 937 | } |
| 938 | |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 939 | // Explicit DoCall template function declarations. |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 940 | #define EXPLICIT_DO_CALL_TEMPLATE_DECL(_is_range, _do_assignability_check) \ |
Mathieu Chartier | 90ef3db | 2015-08-04 15:19:41 -0700 | [diff] [blame] | 941 | template SHARED_REQUIRES(Locks::mutator_lock_) \ |
Sebastien Hertz | 9119c5f | 2013-12-16 11:31:45 +0100 | [diff] [blame] | 942 | bool DoCall<_is_range, _do_assignability_check>(ArtMethod* method, Thread* self, \ |
| 943 | ShadowFrame& shadow_frame, \ |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 944 | const Instruction* inst, uint16_t inst_data, \ |
| 945 | JValue* result) |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 946 | EXPLICIT_DO_CALL_TEMPLATE_DECL(false, false); |
| 947 | EXPLICIT_DO_CALL_TEMPLATE_DECL(false, true); |
| 948 | EXPLICIT_DO_CALL_TEMPLATE_DECL(true, false); |
| 949 | EXPLICIT_DO_CALL_TEMPLATE_DECL(true, true); |
| 950 | #undef EXPLICIT_DO_CALL_TEMPLATE_DECL |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 951 | |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 952 | // Explicit DoLambdaCall template function declarations. |
| 953 | #define EXPLICIT_DO_LAMBDA_CALL_TEMPLATE_DECL(_is_range, _do_assignability_check) \ |
Mathieu Chartier | 90ef3db | 2015-08-04 15:19:41 -0700 | [diff] [blame] | 954 | template SHARED_REQUIRES(Locks::mutator_lock_) \ |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 955 | bool DoLambdaCall<_is_range, _do_assignability_check>(ArtMethod* method, Thread* self, \ |
| 956 | ShadowFrame& shadow_frame, \ |
| 957 | const Instruction* inst, \ |
| 958 | uint16_t inst_data, \ |
| 959 | JValue* result) |
| 960 | EXPLICIT_DO_LAMBDA_CALL_TEMPLATE_DECL(false, false); |
| 961 | EXPLICIT_DO_LAMBDA_CALL_TEMPLATE_DECL(false, true); |
| 962 | EXPLICIT_DO_LAMBDA_CALL_TEMPLATE_DECL(true, false); |
| 963 | EXPLICIT_DO_LAMBDA_CALL_TEMPLATE_DECL(true, true); |
| 964 | #undef EXPLICIT_DO_LAMBDA_CALL_TEMPLATE_DECL |
| 965 | |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 966 | // Explicit DoFilledNewArray template function declarations. |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 967 | #define EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(_is_range_, _check, _transaction_active) \ |
Mathieu Chartier | 90ef3db | 2015-08-04 15:19:41 -0700 | [diff] [blame] | 968 | template SHARED_REQUIRES(Locks::mutator_lock_) \ |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 969 | bool DoFilledNewArray<_is_range_, _check, _transaction_active>(const Instruction* inst, \ |
| 970 | const ShadowFrame& shadow_frame, \ |
| 971 | Thread* self, JValue* result) |
| 972 | #define EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(_transaction_active) \ |
| 973 | EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, false, _transaction_active); \ |
| 974 | EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, true, _transaction_active); \ |
| 975 | EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, false, _transaction_active); \ |
| 976 | EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, true, _transaction_active) |
| 977 | EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(false); |
| 978 | EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(true); |
| 979 | #undef EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 980 | #undef EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL |
| 981 | |
| 982 | } // namespace interpreter |
| 983 | } // namespace art |