blob: 12d70c5244fafeacf1b804988e6cc5fc6c8f075f [file] [log] [blame]
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "interpreter_common.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070018
Andreas Gampef0e128a2015-02-27 20:08:34 -080019#include <cmath>
20
Daniel Mihalyieb076692014-08-22 17:33:31 +020021#include "debugger.h"
Nicolas Geoffray7bf2b4f2015-07-08 10:11:59 +000022#include "entrypoints/runtime_asm_entrypoints.h"
Tamas Berghammerdd5e5e92016-02-12 16:29:00 +000023#include "jit/jit.h"
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010024#include "mirror/array-inl.h"
Andreas Gampeb3025922015-09-01 14:45:00 -070025#include "stack.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070026#include "unstarted_runtime.h"
Jeff Hao848f70a2014-01-15 13:49:50 -080027#include "verifier/method_verifier.h"
Sebastien Hertz8ece0502013-08-07 11:26:41 +020028
29namespace art {
30namespace interpreter {
31
Igor Murashkin6918bf12015-09-27 19:19:06 -070032// All lambda closures have to be a consecutive pair of virtual registers.
33static constexpr size_t kLambdaVirtualRegisterWidth = 2;
34
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000035void ThrowNullPointerExceptionFromInterpreter() {
36 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -070037}
38
39template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check>
40bool DoFieldGet(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst,
41 uint16_t inst_data) {
42 const bool is_static = (find_type == StaticObjectRead) || (find_type == StaticPrimitiveRead);
43 const uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Roland Levillain4b8f1ec2015-08-26 18:34:03 +010044 ArtField* f =
45 FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
46 Primitive::ComponentSize(field_type));
Ian Rogers54874942014-06-10 16:31:03 -070047 if (UNLIKELY(f == nullptr)) {
48 CHECK(self->IsExceptionPending());
49 return false;
50 }
51 Object* obj;
52 if (is_static) {
53 obj = f->GetDeclaringClass();
54 } else {
55 obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
56 if (UNLIKELY(obj == nullptr)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000057 ThrowNullPointerExceptionForFieldAccess(f, true);
Ian Rogers54874942014-06-10 16:31:03 -070058 return false;
59 }
60 }
Sebastien Hertz1edbd8e2014-07-16 20:00:11 +020061 f->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self);
Ian Rogers54874942014-06-10 16:31:03 -070062 // Report this field access to instrumentation if needed.
63 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
64 if (UNLIKELY(instrumentation->HasFieldReadListeners())) {
65 Object* this_object = f->IsStatic() ? nullptr : obj;
66 instrumentation->FieldReadEvent(self, this_object, shadow_frame.GetMethod(),
67 shadow_frame.GetDexPC(), f);
68 }
69 uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
70 switch (field_type) {
71 case Primitive::kPrimBoolean:
72 shadow_frame.SetVReg(vregA, f->GetBoolean(obj));
73 break;
74 case Primitive::kPrimByte:
75 shadow_frame.SetVReg(vregA, f->GetByte(obj));
76 break;
77 case Primitive::kPrimChar:
78 shadow_frame.SetVReg(vregA, f->GetChar(obj));
79 break;
80 case Primitive::kPrimShort:
81 shadow_frame.SetVReg(vregA, f->GetShort(obj));
82 break;
83 case Primitive::kPrimInt:
84 shadow_frame.SetVReg(vregA, f->GetInt(obj));
85 break;
86 case Primitive::kPrimLong:
87 shadow_frame.SetVRegLong(vregA, f->GetLong(obj));
88 break;
89 case Primitive::kPrimNot:
90 shadow_frame.SetVRegReference(vregA, f->GetObject(obj));
91 break;
92 default:
93 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -070094 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -070095 }
96 return true;
97}
98
99// Explicitly instantiate all DoFieldGet functions.
100#define EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, _do_check) \
101 template bool DoFieldGet<_find_type, _field_type, _do_check>(Thread* self, \
102 ShadowFrame& shadow_frame, \
103 const Instruction* inst, \
104 uint16_t inst_data)
105
106#define EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(_find_type, _field_type) \
107 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, false); \
108 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, true);
109
110// iget-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700111EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimBoolean)
112EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimByte)
113EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimChar)
114EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimShort)
115EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimInt)
116EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimLong)
117EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstanceObjectRead, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700118
119// sget-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700120EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimBoolean)
121EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimByte)
122EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimChar)
123EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimShort)
124EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimInt)
125EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimLong)
126EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticObjectRead, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700127
128#undef EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL
129#undef EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL
130
131// Handles iget-quick, iget-wide-quick and iget-object-quick instructions.
132// Returns true on success, otherwise throws an exception and returns false.
133template<Primitive::Type field_type>
134bool DoIGetQuick(ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
135 Object* obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
136 if (UNLIKELY(obj == nullptr)) {
137 // We lost the reference to the field index so we cannot get a more
138 // precised exception message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000139 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -0700140 return false;
141 }
142 MemberOffset field_offset(inst->VRegC_22c());
143 // Report this field access to instrumentation if needed. Since we only have the offset of
144 // the field from the base of the object, we need to look for it first.
145 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
146 if (UNLIKELY(instrumentation->HasFieldReadListeners())) {
147 ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(),
148 field_offset.Uint32Value());
149 DCHECK(f != nullptr);
150 DCHECK(!f->IsStatic());
151 instrumentation->FieldReadEvent(Thread::Current(), obj, shadow_frame.GetMethod(),
152 shadow_frame.GetDexPC(), f);
153 }
154 // Note: iget-x-quick instructions are only for non-volatile fields.
155 const uint32_t vregA = inst->VRegA_22c(inst_data);
156 switch (field_type) {
157 case Primitive::kPrimInt:
158 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetField32(field_offset)));
159 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800160 case Primitive::kPrimBoolean:
161 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldBoolean(field_offset)));
162 break;
163 case Primitive::kPrimByte:
164 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldByte(field_offset)));
165 break;
166 case Primitive::kPrimChar:
167 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldChar(field_offset)));
168 break;
169 case Primitive::kPrimShort:
170 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldShort(field_offset)));
171 break;
Ian Rogers54874942014-06-10 16:31:03 -0700172 case Primitive::kPrimLong:
173 shadow_frame.SetVRegLong(vregA, static_cast<int64_t>(obj->GetField64(field_offset)));
174 break;
175 case Primitive::kPrimNot:
176 shadow_frame.SetVRegReference(vregA, obj->GetFieldObject<mirror::Object>(field_offset));
177 break;
178 default:
179 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700180 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700181 }
182 return true;
183}
184
185// Explicitly instantiate all DoIGetQuick functions.
186#define EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(_field_type) \
187 template bool DoIGetQuick<_field_type>(ShadowFrame& shadow_frame, const Instruction* inst, \
188 uint16_t inst_data)
189
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800190EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimInt); // iget-quick.
191EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimBoolean); // iget-boolean-quick.
192EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimByte); // iget-byte-quick.
193EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimChar); // iget-char-quick.
194EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimShort); // iget-short-quick.
195EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimLong); // iget-wide-quick.
196EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimNot); // iget-object-quick.
Ian Rogers54874942014-06-10 16:31:03 -0700197#undef EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL
198
199template<Primitive::Type field_type>
200static JValue GetFieldValue(const ShadowFrame& shadow_frame, uint32_t vreg)
Mathieu Chartier90443472015-07-16 20:32:27 -0700201 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers54874942014-06-10 16:31:03 -0700202 JValue field_value;
203 switch (field_type) {
204 case Primitive::kPrimBoolean:
205 field_value.SetZ(static_cast<uint8_t>(shadow_frame.GetVReg(vreg)));
206 break;
207 case Primitive::kPrimByte:
208 field_value.SetB(static_cast<int8_t>(shadow_frame.GetVReg(vreg)));
209 break;
210 case Primitive::kPrimChar:
211 field_value.SetC(static_cast<uint16_t>(shadow_frame.GetVReg(vreg)));
212 break;
213 case Primitive::kPrimShort:
214 field_value.SetS(static_cast<int16_t>(shadow_frame.GetVReg(vreg)));
215 break;
216 case Primitive::kPrimInt:
217 field_value.SetI(shadow_frame.GetVReg(vreg));
218 break;
219 case Primitive::kPrimLong:
220 field_value.SetJ(shadow_frame.GetVRegLong(vreg));
221 break;
222 case Primitive::kPrimNot:
223 field_value.SetL(shadow_frame.GetVRegReference(vreg));
224 break;
225 default:
226 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700227 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700228 }
229 return field_value;
230}
231
232template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check,
233 bool transaction_active>
234bool DoFieldPut(Thread* self, const ShadowFrame& shadow_frame, const Instruction* inst,
235 uint16_t inst_data) {
236 bool do_assignability_check = do_access_check;
237 bool is_static = (find_type == StaticObjectWrite) || (find_type == StaticPrimitiveWrite);
238 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Roland Levillain4b8f1ec2015-08-26 18:34:03 +0100239 ArtField* f =
240 FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
241 Primitive::ComponentSize(field_type));
Ian Rogers54874942014-06-10 16:31:03 -0700242 if (UNLIKELY(f == nullptr)) {
243 CHECK(self->IsExceptionPending());
244 return false;
245 }
246 Object* obj;
247 if (is_static) {
248 obj = f->GetDeclaringClass();
249 } else {
250 obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
251 if (UNLIKELY(obj == nullptr)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000252 ThrowNullPointerExceptionForFieldAccess(f, false);
Ian Rogers54874942014-06-10 16:31:03 -0700253 return false;
254 }
255 }
Sebastien Hertz1edbd8e2014-07-16 20:00:11 +0200256 f->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self);
Ian Rogers54874942014-06-10 16:31:03 -0700257 uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
258 // Report this field access to instrumentation if needed. Since we only have the offset of
259 // the field from the base of the object, we need to look for it first.
260 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
261 if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
262 JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA);
263 Object* this_object = f->IsStatic() ? nullptr : obj;
264 instrumentation->FieldWriteEvent(self, this_object, shadow_frame.GetMethod(),
265 shadow_frame.GetDexPC(), f, field_value);
266 }
267 switch (field_type) {
268 case Primitive::kPrimBoolean:
269 f->SetBoolean<transaction_active>(obj, shadow_frame.GetVReg(vregA));
270 break;
271 case Primitive::kPrimByte:
272 f->SetByte<transaction_active>(obj, shadow_frame.GetVReg(vregA));
273 break;
274 case Primitive::kPrimChar:
275 f->SetChar<transaction_active>(obj, shadow_frame.GetVReg(vregA));
276 break;
277 case Primitive::kPrimShort:
278 f->SetShort<transaction_active>(obj, shadow_frame.GetVReg(vregA));
279 break;
280 case Primitive::kPrimInt:
281 f->SetInt<transaction_active>(obj, shadow_frame.GetVReg(vregA));
282 break;
283 case Primitive::kPrimLong:
284 f->SetLong<transaction_active>(obj, shadow_frame.GetVRegLong(vregA));
285 break;
286 case Primitive::kPrimNot: {
287 Object* reg = shadow_frame.GetVRegReference(vregA);
288 if (do_assignability_check && reg != nullptr) {
289 // FieldHelper::GetType can resolve classes, use a handle wrapper which will restore the
290 // object in the destructor.
291 Class* field_class;
292 {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700293 StackHandleScope<2> hs(self);
Ian Rogers54874942014-06-10 16:31:03 -0700294 HandleWrapper<mirror::Object> h_reg(hs.NewHandleWrapper(&reg));
295 HandleWrapper<mirror::Object> h_obj(hs.NewHandleWrapper(&obj));
Mathieu Chartierc7853442015-03-27 14:35:38 -0700296 field_class = f->GetType<true>();
Ian Rogers54874942014-06-10 16:31:03 -0700297 }
298 if (!reg->VerifierInstanceOf(field_class)) {
299 // This should never happen.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700300 std::string temp1, temp2, temp3;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000301 self->ThrowNewExceptionF("Ljava/lang/VirtualMachineError;",
Ian Rogers54874942014-06-10 16:31:03 -0700302 "Put '%s' that is not instance of field '%s' in '%s'",
Ian Rogers1ff3c982014-08-12 02:30:58 -0700303 reg->GetClass()->GetDescriptor(&temp1),
304 field_class->GetDescriptor(&temp2),
305 f->GetDeclaringClass()->GetDescriptor(&temp3));
Ian Rogers54874942014-06-10 16:31:03 -0700306 return false;
307 }
308 }
309 f->SetObj<transaction_active>(obj, reg);
310 break;
311 }
312 default:
313 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700314 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700315 }
316 return true;
317}
318
319// Explicitly instantiate all DoFieldPut functions.
320#define EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, _do_check, _transaction_active) \
321 template bool DoFieldPut<_find_type, _field_type, _do_check, _transaction_active>(Thread* self, \
322 const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data)
323
324#define EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(_find_type, _field_type) \
325 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, false); \
326 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, false); \
327 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, true); \
328 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, true);
329
330// iput-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700331EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimBoolean)
332EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimByte)
333EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimChar)
334EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimShort)
335EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimInt)
336EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimLong)
337EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstanceObjectWrite, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700338
339// sput-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700340EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimBoolean)
341EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimByte)
342EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimChar)
343EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimShort)
344EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimInt)
345EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimLong)
346EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticObjectWrite, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700347
348#undef EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL
349#undef EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL
350
351template<Primitive::Type field_type, bool transaction_active>
352bool DoIPutQuick(const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
353 Object* obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
354 if (UNLIKELY(obj == nullptr)) {
355 // We lost the reference to the field index so we cannot get a more
356 // precised exception message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000357 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -0700358 return false;
359 }
360 MemberOffset field_offset(inst->VRegC_22c());
361 const uint32_t vregA = inst->VRegA_22c(inst_data);
362 // Report this field modification to instrumentation if needed. Since we only have the offset of
363 // the field from the base of the object, we need to look for it first.
364 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
365 if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
366 ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(),
367 field_offset.Uint32Value());
368 DCHECK(f != nullptr);
369 DCHECK(!f->IsStatic());
370 JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA);
371 instrumentation->FieldWriteEvent(Thread::Current(), obj, shadow_frame.GetMethod(),
372 shadow_frame.GetDexPC(), f, field_value);
373 }
374 // Note: iput-x-quick instructions are only for non-volatile fields.
375 switch (field_type) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700376 case Primitive::kPrimBoolean:
377 obj->SetFieldBoolean<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
378 break;
379 case Primitive::kPrimByte:
380 obj->SetFieldByte<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
381 break;
382 case Primitive::kPrimChar:
383 obj->SetFieldChar<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
384 break;
385 case Primitive::kPrimShort:
386 obj->SetFieldShort<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
387 break;
Ian Rogers54874942014-06-10 16:31:03 -0700388 case Primitive::kPrimInt:
389 obj->SetField32<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
390 break;
391 case Primitive::kPrimLong:
392 obj->SetField64<transaction_active>(field_offset, shadow_frame.GetVRegLong(vregA));
393 break;
394 case Primitive::kPrimNot:
395 obj->SetFieldObject<transaction_active>(field_offset, shadow_frame.GetVRegReference(vregA));
396 break;
397 default:
398 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700399 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700400 }
401 return true;
402}
403
404// Explicitly instantiate all DoIPutQuick functions.
405#define EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, _transaction_active) \
406 template bool DoIPutQuick<_field_type, _transaction_active>(const ShadowFrame& shadow_frame, \
407 const Instruction* inst, \
408 uint16_t inst_data)
409
410#define EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(_field_type) \
411 EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, false); \
412 EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, true);
413
Andreas Gampec8ccf682014-09-29 20:07:43 -0700414EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimInt) // iput-quick.
415EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimBoolean) // iput-boolean-quick.
416EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimByte) // iput-byte-quick.
417EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimChar) // iput-char-quick.
418EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimShort) // iput-short-quick.
419EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimLong) // iput-wide-quick.
420EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimNot) // iput-object-quick.
Ian Rogers54874942014-06-10 16:31:03 -0700421#undef EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL
422#undef EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL
423
Sebastien Hertz520633b2015-09-08 17:03:36 +0200424// We accept a null Instrumentation* meaning we must not report anything to the instrumentation.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700425uint32_t FindNextInstructionFollowingException(
426 Thread* self, ShadowFrame& shadow_frame, uint32_t dex_pc,
427 const instrumentation::Instrumentation* instrumentation) {
Ian Rogers54874942014-06-10 16:31:03 -0700428 self->VerifyStack();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700429 StackHandleScope<2> hs(self);
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000430 Handle<mirror::Throwable> exception(hs.NewHandle(self->GetException()));
Sebastien Hertz520633b2015-09-08 17:03:36 +0200431 if (instrumentation != nullptr && instrumentation->HasExceptionCaughtListeners()
Nicolas Geoffray7642cfc2015-02-26 10:56:09 +0000432 && self->IsExceptionThrownByCurrentMethod(exception.Get())) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000433 instrumentation->ExceptionCaughtEvent(self, exception.Get());
Sebastien Hertz9f102032014-05-23 08:59:42 +0200434 }
Ian Rogers54874942014-06-10 16:31:03 -0700435 bool clear_exception = false;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700436 uint32_t found_dex_pc = shadow_frame.GetMethod()->FindCatchBlock(
437 hs.NewHandle(exception->GetClass()), dex_pc, &clear_exception);
Sebastien Hertz520633b2015-09-08 17:03:36 +0200438 if (found_dex_pc == DexFile::kDexNoIndex && instrumentation != nullptr) {
Nicolas Geoffray7642cfc2015-02-26 10:56:09 +0000439 // Exception is not caught by the current method. We will unwind to the
440 // caller. Notify any instrumentation listener.
Sebastien Hertz9f102032014-05-23 08:59:42 +0200441 instrumentation->MethodUnwindEvent(self, shadow_frame.GetThisObject(),
Ian Rogers54874942014-06-10 16:31:03 -0700442 shadow_frame.GetMethod(), dex_pc);
443 } else {
Nicolas Geoffray7642cfc2015-02-26 10:56:09 +0000444 // Exception is caught in the current method. We will jump to the found_dex_pc.
Ian Rogers54874942014-06-10 16:31:03 -0700445 if (clear_exception) {
446 self->ClearException();
447 }
448 }
449 return found_dex_pc;
450}
451
Ian Rogerse94652f2014-12-02 11:13:19 -0800452void UnexpectedOpcode(const Instruction* inst, const ShadowFrame& shadow_frame) {
453 LOG(FATAL) << "Unexpected instruction: "
454 << inst->DumpString(shadow_frame.GetMethod()->GetDexFile());
455 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700456}
457
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200458// Assign register 'src_reg' from shadow_frame to register 'dest_reg' into new_shadow_frame.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800459static inline void AssignRegister(ShadowFrame* new_shadow_frame, const ShadowFrame& shadow_frame,
460 size_t dest_reg, size_t src_reg)
Mathieu Chartier90443472015-07-16 20:32:27 -0700461 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe7104cbf2014-03-21 11:44:43 -0700462 // Uint required, so that sign extension does not make this wrong on 64b systems
463 uint32_t src_value = shadow_frame.GetVReg(src_reg);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800464 mirror::Object* o = shadow_frame.GetVRegReference<kVerifyNone>(src_reg);
Igor Murashkinc449e8b2015-06-10 15:56:42 -0700465
466 // If both register locations contains the same value, the register probably holds a reference.
467 // Note: As an optimization, non-moving collectors leave a stale reference value
468 // in the references array even after the original vreg was overwritten to a non-reference.
Andreas Gampe7104cbf2014-03-21 11:44:43 -0700469 if (src_value == reinterpret_cast<uintptr_t>(o)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800470 new_shadow_frame->SetVRegReference(dest_reg, o);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200471 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800472 new_shadow_frame->SetVReg(dest_reg, src_value);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200473 }
474}
475
Sebastien Hertz45b15972015-04-03 16:07:05 +0200476void AbortTransactionF(Thread* self, const char* fmt, ...) {
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700477 va_list args;
478 va_start(args, fmt);
Sebastien Hertz45b15972015-04-03 16:07:05 +0200479 AbortTransactionV(self, fmt, args);
480 va_end(args);
481}
482
483void AbortTransactionV(Thread* self, const char* fmt, va_list args) {
484 CHECK(Runtime::Current()->IsActiveTransaction());
485 // Constructs abort message.
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100486 std::string abort_msg;
487 StringAppendV(&abort_msg, fmt, args);
488 // Throws an exception so we can abort the transaction and rollback every change.
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200489 Runtime::Current()->AbortTransactionAndThrowAbortError(self, abort_msg);
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700490}
491
Igor Murashkin158f35c2015-06-10 15:55:30 -0700492// Separate declaration is required solely for the attributes.
Igor Murashkin6918bf12015-09-27 19:19:06 -0700493template <bool is_range,
494 bool do_assignability_check,
495 size_t kVarArgMax>
496 SHARED_REQUIRES(Locks::mutator_lock_)
Igor Murashkin158f35c2015-06-10 15:55:30 -0700497static inline bool DoCallCommon(ArtMethod* called_method,
498 Thread* self,
499 ShadowFrame& shadow_frame,
500 JValue* result,
501 uint16_t number_of_inputs,
Igor Murashkin6918bf12015-09-27 19:19:06 -0700502 uint32_t (&arg)[kVarArgMax],
Igor Murashkin158f35c2015-06-10 15:55:30 -0700503 uint32_t vregC) ALWAYS_INLINE;
504
Siva Chandra05d24152016-01-05 17:43:17 -0800505void ArtInterpreterToCompiledCodeBridge(Thread* self,
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100506 ArtMethod* caller,
Siva Chandra05d24152016-01-05 17:43:17 -0800507 const DexFile::CodeItem* code_item,
508 ShadowFrame* shadow_frame,
509 JValue* result)
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700510 SHARED_REQUIRES(Locks::mutator_lock_) {
511 ArtMethod* method = shadow_frame->GetMethod();
512 // Ensure static methods are initialized.
513 if (method->IsStatic()) {
514 mirror::Class* declaringClass = method->GetDeclaringClass();
515 if (UNLIKELY(!declaringClass->IsInitialized())) {
516 self->PushShadowFrame(shadow_frame);
517 StackHandleScope<1> hs(self);
518 Handle<mirror::Class> h_class(hs.NewHandle(declaringClass));
519 if (UNLIKELY(!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true,
520 true))) {
521 self->PopShadowFrame();
522 DCHECK(self->IsExceptionPending());
523 return;
524 }
525 self->PopShadowFrame();
526 CHECK(h_class->IsInitializing());
527 // Reload from shadow frame in case the method moved, this is faster than adding a handle.
528 method = shadow_frame->GetMethod();
529 }
530 }
531 uint16_t arg_offset = (code_item == nullptr)
532 ? 0
533 : code_item->registers_size_ - code_item->ins_size_;
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100534 jit::Jit* jit = Runtime::Current()->GetJit();
535 if (jit != nullptr && caller != nullptr) {
536 jit->NotifyInterpreterToCompiledCodeTransition(self, caller);
537 }
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700538 method->Invoke(self, shadow_frame->GetVRegArgs(arg_offset),
539 (shadow_frame->NumberOfVRegs() - arg_offset) * sizeof(uint32_t),
540 result, method->GetInterfaceMethodIfProxy(sizeof(void*))->GetShorty());
541}
542
Igor Murashkin6918bf12015-09-27 19:19:06 -0700543template <bool is_range,
544 bool do_assignability_check,
545 size_t kVarArgMax>
Igor Murashkin158f35c2015-06-10 15:55:30 -0700546static inline bool DoCallCommon(ArtMethod* called_method,
547 Thread* self,
548 ShadowFrame& shadow_frame,
549 JValue* result,
550 uint16_t number_of_inputs,
Igor Murashkin6918bf12015-09-27 19:19:06 -0700551 uint32_t (&arg)[kVarArgMax],
Igor Murashkin158f35c2015-06-10 15:55:30 -0700552 uint32_t vregC) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800553 bool string_init = false;
554 // Replace calls to String.<init> with equivalent StringFactory call.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700555 if (UNLIKELY(called_method->GetDeclaringClass()->IsStringClass()
556 && called_method->IsConstructor())) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800557 ScopedObjectAccessUnchecked soa(self);
558 jmethodID mid = soa.EncodeMethod(called_method);
559 called_method = soa.DecodeMethod(WellKnownClasses::StringInitToStringFactoryMethodID(mid));
560 string_init = true;
561 }
562
Alex Lightdaf58c82016-03-16 23:00:49 +0000563 // Compute method information.
564 const DexFile::CodeItem* code_item = called_method->GetCodeItem();
Igor Murashkin158f35c2015-06-10 15:55:30 -0700565
566 // Number of registers for the callee's call frame.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200567 uint16_t num_regs;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700568 if (LIKELY(code_item != nullptr)) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200569 num_regs = code_item->registers_size_;
Igor Murashkin158f35c2015-06-10 15:55:30 -0700570 DCHECK_EQ(string_init ? number_of_inputs - 1 : number_of_inputs, code_item->ins_size_);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200571 } else {
Ian Rogerse94652f2014-12-02 11:13:19 -0800572 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
Igor Murashkin158f35c2015-06-10 15:55:30 -0700573 num_regs = number_of_inputs;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200574 }
575
Igor Murashkin158f35c2015-06-10 15:55:30 -0700576 // Hack for String init:
577 //
578 // Rewrite invoke-x java.lang.String.<init>(this, a, b, c, ...) into:
579 // invoke-x StringFactory(a, b, c, ...)
580 // by effectively dropping the first virtual register from the invoke.
581 //
582 // (at this point the ArtMethod has already been replaced,
583 // so we just need to fix-up the arguments)
David Brazdil65902e82016-01-15 09:35:13 +0000584 //
585 // Note that FindMethodFromCode in entrypoint_utils-inl.h was also special-cased
586 // to handle the compiler optimization of replacing `this` with null without
587 // throwing NullPointerException.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700588 uint32_t string_init_vreg_this = is_range ? vregC : arg[0];
Igor Murashkina06b49b2015-06-25 15:18:12 -0700589 if (UNLIKELY(string_init)) {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700590 DCHECK_GT(num_regs, 0u); // As the method is an instance method, there should be at least 1.
Igor Murashkina06b49b2015-06-25 15:18:12 -0700591
Igor Murashkin158f35c2015-06-10 15:55:30 -0700592 // The new StringFactory call is static and has one fewer argument.
Igor Murashkina06b49b2015-06-25 15:18:12 -0700593 if (code_item == nullptr) {
594 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
595 num_regs--;
596 } // else ... don't need to change num_regs since it comes up from the string_init's code item
Igor Murashkin158f35c2015-06-10 15:55:30 -0700597 number_of_inputs--;
598
599 // Rewrite the var-args, dropping the 0th argument ("this")
Igor Murashkin6918bf12015-09-27 19:19:06 -0700600 for (uint32_t i = 1; i < arraysize(arg); ++i) {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700601 arg[i - 1] = arg[i];
602 }
Igor Murashkin6918bf12015-09-27 19:19:06 -0700603 arg[arraysize(arg) - 1] = 0;
Igor Murashkin158f35c2015-06-10 15:55:30 -0700604
605 // Rewrite the non-var-arg case
606 vregC++; // Skips the 0th vreg in the range ("this").
607 }
608
609 // Parameter registers go at the end of the shadow frame.
610 DCHECK_GE(num_regs, number_of_inputs);
611 size_t first_dest_reg = num_regs - number_of_inputs;
612 DCHECK_NE(first_dest_reg, (size_t)-1);
613
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200614 // Allocate shadow frame on the stack.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700615 const char* old_cause = self->StartAssertNoThreadSuspension("DoCallCommon");
Andreas Gampeb3025922015-09-01 14:45:00 -0700616 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
Andreas Gampe03ec9302015-08-27 17:41:47 -0700617 CREATE_SHADOW_FRAME(num_regs, &shadow_frame, called_method, /* dex pc */ 0);
Andreas Gampeb3025922015-09-01 14:45:00 -0700618 ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get();
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200619
Igor Murashkin158f35c2015-06-10 15:55:30 -0700620 // Initialize new shadow frame by copying the registers from the callee shadow frame.
Jeff Haoa3faaf42013-09-03 19:07:00 -0700621 if (do_assignability_check) {
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -0700622 // Slow path.
623 // We might need to do class loading, which incurs a thread state change to kNative. So
624 // register the shadow frame as under construction and allow suspension again.
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700625 ScopedStackedShadowFramePusher pusher(
Sebastien Hertzf7958692015-06-09 14:09:14 +0200626 self, new_shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -0700627 self->EndAssertNoThreadSuspension(old_cause);
628
Igor Murashkin9f95ba72016-02-01 14:21:25 -0800629 // ArtMethod here is needed to check type information of the call site against the callee.
630 // Type information is retrieved from a DexFile/DexCache for that respective declared method.
631 //
632 // As a special case for proxy methods, which are not dex-backed,
633 // we have to retrieve type information from the proxy's method
634 // interface method instead (which is dex backed since proxies are never interfaces).
635 ArtMethod* method = new_shadow_frame->GetMethod()->GetInterfaceMethodIfProxy(sizeof(void*));
636
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -0700637 // We need to do runtime check on reference assignment. We need to load the shorty
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200638 // to get the exact type of each reference argument.
Igor Murashkin9f95ba72016-02-01 14:21:25 -0800639 const DexFile::TypeList* params = method->GetParameterTypeList();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700640 uint32_t shorty_len = 0;
Igor Murashkin9f95ba72016-02-01 14:21:25 -0800641 const char* shorty = method->GetShorty(&shorty_len);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200642
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100643 // Handle receiver apart since it's not part of the shorty.
644 size_t dest_reg = first_dest_reg;
645 size_t arg_offset = 0;
Igor Murashkin158f35c2015-06-10 15:55:30 -0700646
Igor Murashkin9f95ba72016-02-01 14:21:25 -0800647 if (!method->IsStatic()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700648 size_t receiver_reg = is_range ? vregC : arg[0];
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100649 new_shadow_frame->SetVRegReference(dest_reg, shadow_frame.GetVRegReference(receiver_reg));
650 ++dest_reg;
651 ++arg_offset;
Igor Murashkina06b49b2015-06-25 15:18:12 -0700652 DCHECK(!string_init); // All StringFactory methods are static.
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100653 }
Igor Murashkin158f35c2015-06-10 15:55:30 -0700654
655 // Copy the caller's invoke-* arguments into the callee's parameter registers.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800656 for (uint32_t shorty_pos = 0; dest_reg < num_regs; ++shorty_pos, ++dest_reg, ++arg_offset) {
Igor Murashkina06b49b2015-06-25 15:18:12 -0700657 // Skip the 0th 'shorty' type since it represents the return type.
658 DCHECK_LT(shorty_pos + 1, shorty_len) << "for shorty '" << shorty << "'";
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200659 const size_t src_reg = (is_range) ? vregC + arg_offset : arg[arg_offset];
660 switch (shorty[shorty_pos + 1]) {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700661 // Handle Object references. 1 virtual register slot.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200662 case 'L': {
663 Object* o = shadow_frame.GetVRegReference(src_reg);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700664 if (do_assignability_check && o != nullptr) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100665 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Ian Rogersa0485602014-12-02 15:48:04 -0800666 Class* arg_type =
Igor Murashkin9f95ba72016-02-01 14:21:25 -0800667 method->GetClassFromTypeIndex(
Vladimir Marko05792b92015-08-03 11:56:49 +0100668 params->GetTypeItem(shorty_pos).type_idx_, true /* resolve */, pointer_size);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700669 if (arg_type == nullptr) {
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200670 CHECK(self->IsExceptionPending());
671 return false;
672 }
673 if (!o->VerifierInstanceOf(arg_type)) {
674 // This should never happen.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700675 std::string temp1, temp2;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000676 self->ThrowNewExceptionF("Ljava/lang/VirtualMachineError;",
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200677 "Invoking %s with bad arg %d, type '%s' not instance of '%s'",
Ian Rogerse94652f2014-12-02 11:13:19 -0800678 new_shadow_frame->GetMethod()->GetName(), shorty_pos,
Ian Rogers1ff3c982014-08-12 02:30:58 -0700679 o->GetClass()->GetDescriptor(&temp1),
680 arg_type->GetDescriptor(&temp2));
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200681 return false;
682 }
Jeff Haoa3faaf42013-09-03 19:07:00 -0700683 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200684 new_shadow_frame->SetVRegReference(dest_reg, o);
685 break;
Jeff Haoa3faaf42013-09-03 19:07:00 -0700686 }
Igor Murashkin158f35c2015-06-10 15:55:30 -0700687 // Handle doubles and longs. 2 consecutive virtual register slots.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200688 case 'J': case 'D': {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700689 uint64_t wide_value =
690 (static_cast<uint64_t>(shadow_frame.GetVReg(src_reg + 1)) << BitSizeOf<uint32_t>()) |
691 static_cast<uint32_t>(shadow_frame.GetVReg(src_reg));
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200692 new_shadow_frame->SetVRegLong(dest_reg, wide_value);
Igor Murashkin158f35c2015-06-10 15:55:30 -0700693 // Skip the next virtual register slot since we already used it.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200694 ++dest_reg;
695 ++arg_offset;
696 break;
697 }
Igor Murashkin158f35c2015-06-10 15:55:30 -0700698 // Handle all other primitives that are always 1 virtual register slot.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200699 default:
700 new_shadow_frame->SetVReg(dest_reg, shadow_frame.GetVReg(src_reg));
701 break;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200702 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200703 }
704 } else {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700705 size_t arg_index = 0;
706
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200707 // Fast path: no extra checks.
708 if (is_range) {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700709 // TODO: Implement the range version of invoke-lambda
710 uint16_t first_src_reg = vregC;
711
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200712 for (size_t src_reg = first_src_reg, dest_reg = first_dest_reg; dest_reg < num_regs;
713 ++dest_reg, ++src_reg) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800714 AssignRegister(new_shadow_frame, shadow_frame, dest_reg, src_reg);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200715 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200716 } else {
Igor Murashkin6918bf12015-09-27 19:19:06 -0700717 DCHECK_LE(number_of_inputs, arraysize(arg));
Igor Murashkin158f35c2015-06-10 15:55:30 -0700718
719 for (; arg_index < number_of_inputs; ++arg_index) {
720 AssignRegister(new_shadow_frame, shadow_frame, first_dest_reg + arg_index, arg[arg_index]);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200721 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200722 }
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -0700723 self->EndAssertNoThreadSuspension(old_cause);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200724 }
725
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200726 // Do the call now.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200727 if (LIKELY(Runtime::Current()->IsStarted())) {
Tamas Berghammerdd5e5e92016-02-12 16:29:00 +0000728 ArtMethod* target = new_shadow_frame->GetMethod();
729 if (ClassLinker::ShouldUseInterpreterEntrypoint(
730 target,
731 target->GetEntryPointFromQuickCompiledCode())) {
Tamas Berghammerc94a61f2016-02-05 18:09:08 +0000732 ArtInterpreterToInterpreterBridge(self, code_item, new_shadow_frame, result);
Tamas Berghammer3a98aae2016-02-08 20:21:54 +0000733 } else {
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100734 ArtInterpreterToCompiledCodeBridge(
735 self, shadow_frame.GetMethod(), code_item, new_shadow_frame, result);
Nicolas Geoffray7070ccd2015-07-08 09:41:54 +0000736 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200737 } else {
Andreas Gampe799681b2015-05-15 19:24:12 -0700738 UnstartedRuntime::Invoke(self, code_item, new_shadow_frame, result, first_dest_reg);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200739 }
Jeff Hao848f70a2014-01-15 13:49:50 -0800740
741 if (string_init && !self->IsExceptionPending()) {
Nicolas Geoffray98e6ce42016-02-16 18:42:15 +0000742 mirror::Object* existing = shadow_frame.GetVRegReference(string_init_vreg_this);
743 if (existing == nullptr) {
744 // If it's null, we come from compiled code that was deoptimized. Nothing to do,
745 // as the compiler verified there was no alias.
746 // Set the new string result of the StringFactory.
747 shadow_frame.SetVRegReference(string_init_vreg_this, result->GetL());
748 } else {
749 // Replace the fake string that was allocated with the StringFactory result.
750 for (uint32_t i = 0; i < shadow_frame.NumberOfVRegs(); ++i) {
751 if (shadow_frame.GetVRegReference(i) == existing) {
752 DCHECK_EQ(shadow_frame.GetVRegReference(i),
753 reinterpret_cast<mirror::Object*>(shadow_frame.GetVReg(i)));
754 shadow_frame.SetVRegReference(i, result->GetL());
755 DCHECK_EQ(shadow_frame.GetVRegReference(i),
756 reinterpret_cast<mirror::Object*>(shadow_frame.GetVReg(i)));
Jeff Hao848f70a2014-01-15 13:49:50 -0800757 }
758 }
759 }
760 }
761
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200762 return !self->IsExceptionPending();
763}
764
Igor Murashkin158f35c2015-06-10 15:55:30 -0700765template<bool is_range, bool do_assignability_check>
766bool DoLambdaCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame,
Roland Levillain4b8f1ec2015-08-26 18:34:03 +0100767 const Instruction* inst, uint16_t inst_data ATTRIBUTE_UNUSED, JValue* result) {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700768 const uint4_t num_additional_registers = inst->VRegB_25x();
769 // Argument word count.
Igor Murashkin6918bf12015-09-27 19:19:06 -0700770 const uint16_t number_of_inputs = num_additional_registers + kLambdaVirtualRegisterWidth;
771 // The lambda closure register is always present and is not encoded in the count.
772 // Furthermore, the lambda closure register is always wide, so it counts as 2 inputs.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700773
774 // TODO: find a cleaner way to separate non-range and range information without duplicating
775 // code.
Igor Murashkin6918bf12015-09-27 19:19:06 -0700776 uint32_t arg[Instruction::kMaxVarArgRegs25x]; // only used in invoke-XXX.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700777 uint32_t vregC = 0; // only used in invoke-XXX-range.
778 if (is_range) {
779 vregC = inst->VRegC_3rc();
780 } else {
781 // TODO(iam): See if it's possible to remove inst_data dependency from 35x to avoid this path
Igor Murashkin158f35c2015-06-10 15:55:30 -0700782 inst->GetAllArgs25x(arg);
783 }
784
785 // TODO: if there's an assignability check, throw instead?
786 DCHECK(called_method->IsStatic());
787
788 return DoCallCommon<is_range, do_assignability_check>(
789 called_method, self, shadow_frame,
790 result, number_of_inputs, arg, vregC);
791}
792
793template<bool is_range, bool do_assignability_check>
794bool DoCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame,
795 const Instruction* inst, uint16_t inst_data, JValue* result) {
796 // Argument word count.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +0100797 const uint16_t number_of_inputs =
798 (is_range) ? inst->VRegA_3rc(inst_data) : inst->VRegA_35c(inst_data);
Igor Murashkin158f35c2015-06-10 15:55:30 -0700799
800 // TODO: find a cleaner way to separate non-range and range information without duplicating
801 // code.
Igor Murashkin6918bf12015-09-27 19:19:06 -0700802 uint32_t arg[Instruction::kMaxVarArgRegs] = {}; // only used in invoke-XXX.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700803 uint32_t vregC = 0;
804 if (is_range) {
805 vregC = inst->VRegC_3rc();
806 } else {
807 vregC = inst->VRegC_35c();
808 inst->GetVarArgs(arg, inst_data);
809 }
810
811 return DoCallCommon<is_range, do_assignability_check>(
812 called_method, self, shadow_frame,
813 result, number_of_inputs, arg, vregC);
814}
815
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100816template <bool is_range, bool do_access_check, bool transaction_active>
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200817bool DoFilledNewArray(const Instruction* inst, const ShadowFrame& shadow_frame,
818 Thread* self, JValue* result) {
819 DCHECK(inst->Opcode() == Instruction::FILLED_NEW_ARRAY ||
820 inst->Opcode() == Instruction::FILLED_NEW_ARRAY_RANGE);
821 const int32_t length = is_range ? inst->VRegA_3rc() : inst->VRegA_35c();
822 if (!is_range) {
823 // Checks FILLED_NEW_ARRAY's length does not exceed 5 arguments.
824 CHECK_LE(length, 5);
825 }
826 if (UNLIKELY(length < 0)) {
827 ThrowNegativeArraySizeException(length);
828 return false;
829 }
830 uint16_t type_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700831 Class* array_class = ResolveVerifyAndClinit(type_idx, shadow_frame.GetMethod(),
832 self, false, do_access_check);
833 if (UNLIKELY(array_class == nullptr)) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200834 DCHECK(self->IsExceptionPending());
835 return false;
836 }
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700837 CHECK(array_class->IsArrayClass());
838 Class* component_class = array_class->GetComponentType();
839 const bool is_primitive_int_component = component_class->IsPrimitiveInt();
840 if (UNLIKELY(component_class->IsPrimitive() && !is_primitive_int_component)) {
841 if (component_class->IsPrimitiveLong() || component_class->IsPrimitiveDouble()) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200842 ThrowRuntimeException("Bad filled array request for type %s",
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700843 PrettyDescriptor(component_class).c_str());
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200844 } else {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000845 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
Brian Carlstrom4fa0bcd2013-12-10 11:24:21 -0800846 "Found type %s; filled-new-array not implemented for anything but 'int'",
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700847 PrettyDescriptor(component_class).c_str());
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200848 }
849 return false;
850 }
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700851 Object* new_array = Array::Alloc<true>(self, array_class, length,
852 array_class->GetComponentSizeShift(),
853 Runtime::Current()->GetHeap()->GetCurrentAllocator());
854 if (UNLIKELY(new_array == nullptr)) {
855 self->AssertPendingOOMException();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200856 return false;
857 }
Igor Murashkin158f35c2015-06-10 15:55:30 -0700858 uint32_t arg[Instruction::kMaxVarArgRegs]; // only used in filled-new-array.
859 uint32_t vregC = 0; // only used in filled-new-array-range.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200860 if (is_range) {
Sebastien Hertzabff6432014-01-27 18:01:39 +0100861 vregC = inst->VRegC_3rc();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200862 } else {
Ian Rogers29a26482014-05-02 15:27:29 -0700863 inst->GetVarArgs(arg);
Sebastien Hertzabff6432014-01-27 18:01:39 +0100864 }
Sebastien Hertzabff6432014-01-27 18:01:39 +0100865 for (int32_t i = 0; i < length; ++i) {
866 size_t src_reg = is_range ? vregC + i : arg[i];
867 if (is_primitive_int_component) {
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700868 new_array->AsIntArray()->SetWithoutChecks<transaction_active>(
869 i, shadow_frame.GetVReg(src_reg));
Sebastien Hertzabff6432014-01-27 18:01:39 +0100870 } else {
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700871 new_array->AsObjectArray<Object>()->SetWithoutChecks<transaction_active>(
872 i, shadow_frame.GetVRegReference(src_reg));
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200873 }
874 }
875
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700876 result->SetL(new_array);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200877 return true;
878}
879
Mathieu Chartier90443472015-07-16 20:32:27 -0700880// TODO fix thread analysis: should be SHARED_REQUIRES(Locks::mutator_lock_).
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100881template<typename T>
882static void RecordArrayElementsInTransactionImpl(mirror::PrimitiveArray<T>* array, int32_t count)
883 NO_THREAD_SAFETY_ANALYSIS {
884 Runtime* runtime = Runtime::Current();
885 for (int32_t i = 0; i < count; ++i) {
886 runtime->RecordWriteArray(array, i, array->GetWithoutChecks(i));
887 }
888}
889
890void RecordArrayElementsInTransaction(mirror::Array* array, int32_t count)
Mathieu Chartier90443472015-07-16 20:32:27 -0700891 SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100892 DCHECK(Runtime::Current()->IsActiveTransaction());
893 DCHECK(array != nullptr);
894 DCHECK_LE(count, array->GetLength());
895 Primitive::Type primitive_component_type = array->GetClass()->GetComponentType()->GetPrimitiveType();
896 switch (primitive_component_type) {
897 case Primitive::kPrimBoolean:
898 RecordArrayElementsInTransactionImpl(array->AsBooleanArray(), count);
899 break;
900 case Primitive::kPrimByte:
901 RecordArrayElementsInTransactionImpl(array->AsByteArray(), count);
902 break;
903 case Primitive::kPrimChar:
904 RecordArrayElementsInTransactionImpl(array->AsCharArray(), count);
905 break;
906 case Primitive::kPrimShort:
907 RecordArrayElementsInTransactionImpl(array->AsShortArray(), count);
908 break;
909 case Primitive::kPrimInt:
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100910 RecordArrayElementsInTransactionImpl(array->AsIntArray(), count);
911 break;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700912 case Primitive::kPrimFloat:
913 RecordArrayElementsInTransactionImpl(array->AsFloatArray(), count);
914 break;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100915 case Primitive::kPrimLong:
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100916 RecordArrayElementsInTransactionImpl(array->AsLongArray(), count);
917 break;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700918 case Primitive::kPrimDouble:
919 RecordArrayElementsInTransactionImpl(array->AsDoubleArray(), count);
920 break;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100921 default:
922 LOG(FATAL) << "Unsupported primitive type " << primitive_component_type
923 << " in fill-array-data";
924 break;
925 }
926}
927
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200928// Explicit DoCall template function declarations.
Sebastien Hertzc6714852013-09-30 16:42:32 +0200929#define EXPLICIT_DO_CALL_TEMPLATE_DECL(_is_range, _do_assignability_check) \
Mathieu Chartier90ef3db2015-08-04 15:19:41 -0700930 template SHARED_REQUIRES(Locks::mutator_lock_) \
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100931 bool DoCall<_is_range, _do_assignability_check>(ArtMethod* method, Thread* self, \
932 ShadowFrame& shadow_frame, \
Sebastien Hertzc6714852013-09-30 16:42:32 +0200933 const Instruction* inst, uint16_t inst_data, \
934 JValue* result)
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200935EXPLICIT_DO_CALL_TEMPLATE_DECL(false, false);
936EXPLICIT_DO_CALL_TEMPLATE_DECL(false, true);
937EXPLICIT_DO_CALL_TEMPLATE_DECL(true, false);
938EXPLICIT_DO_CALL_TEMPLATE_DECL(true, true);
939#undef EXPLICIT_DO_CALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200940
Igor Murashkin158f35c2015-06-10 15:55:30 -0700941// Explicit DoLambdaCall template function declarations.
942#define EXPLICIT_DO_LAMBDA_CALL_TEMPLATE_DECL(_is_range, _do_assignability_check) \
Mathieu Chartier90ef3db2015-08-04 15:19:41 -0700943 template SHARED_REQUIRES(Locks::mutator_lock_) \
Igor Murashkin158f35c2015-06-10 15:55:30 -0700944 bool DoLambdaCall<_is_range, _do_assignability_check>(ArtMethod* method, Thread* self, \
945 ShadowFrame& shadow_frame, \
946 const Instruction* inst, \
947 uint16_t inst_data, \
948 JValue* result)
949EXPLICIT_DO_LAMBDA_CALL_TEMPLATE_DECL(false, false);
950EXPLICIT_DO_LAMBDA_CALL_TEMPLATE_DECL(false, true);
951EXPLICIT_DO_LAMBDA_CALL_TEMPLATE_DECL(true, false);
952EXPLICIT_DO_LAMBDA_CALL_TEMPLATE_DECL(true, true);
953#undef EXPLICIT_DO_LAMBDA_CALL_TEMPLATE_DECL
954
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200955// Explicit DoFilledNewArray template function declarations.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100956#define EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(_is_range_, _check, _transaction_active) \
Mathieu Chartier90ef3db2015-08-04 15:19:41 -0700957 template SHARED_REQUIRES(Locks::mutator_lock_) \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100958 bool DoFilledNewArray<_is_range_, _check, _transaction_active>(const Instruction* inst, \
959 const ShadowFrame& shadow_frame, \
960 Thread* self, JValue* result)
961#define EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(_transaction_active) \
962 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, false, _transaction_active); \
963 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, true, _transaction_active); \
964 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, false, _transaction_active); \
965 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, true, _transaction_active)
966EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(false);
967EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(true);
968#undef EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200969#undef EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL
970
971} // namespace interpreter
972} // namespace art