blob: 6c8e53df8eab9c70f95c609e459fef10f45e7ae2 [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
Andreas Gampe542451c2016-07-26 09:02:02 -070021#include "base/enums.h"
Daniel Mihalyieb076692014-08-22 17:33:31 +020022#include "debugger.h"
Nicolas Geoffray7bf2b4f2015-07-08 10:11:59 +000023#include "entrypoints/runtime_asm_entrypoints.h"
Tamas Berghammerdd5e5e92016-02-12 16:29:00 +000024#include "jit/jit.h"
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010025#include "mirror/array-inl.h"
Andreas Gampeb3025922015-09-01 14:45:00 -070026#include "stack.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070027#include "unstarted_runtime.h"
Jeff Hao848f70a2014-01-15 13:49:50 -080028#include "verifier/method_verifier.h"
Sebastien Hertz8ece0502013-08-07 11:26:41 +020029
30namespace art {
31namespace interpreter {
32
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000033void ThrowNullPointerExceptionFromInterpreter() {
34 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -070035}
36
37template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check>
38bool DoFieldGet(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst,
39 uint16_t inst_data) {
40 const bool is_static = (find_type == StaticObjectRead) || (find_type == StaticPrimitiveRead);
41 const uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Roland Levillain4b8f1ec2015-08-26 18:34:03 +010042 ArtField* f =
43 FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
44 Primitive::ComponentSize(field_type));
Ian Rogers54874942014-06-10 16:31:03 -070045 if (UNLIKELY(f == nullptr)) {
46 CHECK(self->IsExceptionPending());
47 return false;
48 }
Mathieu Chartier3398c782016-09-30 10:27:43 -070049 ObjPtr<Object> obj;
Ian Rogers54874942014-06-10 16:31:03 -070050 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 Geoffray0aa50ce2015-03-10 11:03:29 +000055 ThrowNullPointerExceptionForFieldAccess(f, true);
Ian Rogers54874942014-06-10 16:31:03 -070056 return false;
57 }
58 }
Sebastien Hertz1edbd8e2014-07-16 20:00:11 +020059 f->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self);
Ian Rogers54874942014-06-10 16:31:03 -070060 // Report this field access to instrumentation if needed.
61 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
62 if (UNLIKELY(instrumentation->HasFieldReadListeners())) {
Mathieu Chartier3398c782016-09-30 10:27:43 -070063 ObjPtr<Object> this_object;
64 if (!f->IsStatic()) {
65 this_object = obj;
66 }
67 instrumentation->FieldReadEvent(self,
68 this_object.Decode(),
69 shadow_frame.GetMethod(),
70 shadow_frame.GetDexPC(),
71 f);
Ian Rogers54874942014-06-10 16:31:03 -070072 }
73 uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
74 switch (field_type) {
75 case Primitive::kPrimBoolean:
76 shadow_frame.SetVReg(vregA, f->GetBoolean(obj));
77 break;
78 case Primitive::kPrimByte:
79 shadow_frame.SetVReg(vregA, f->GetByte(obj));
80 break;
81 case Primitive::kPrimChar:
82 shadow_frame.SetVReg(vregA, f->GetChar(obj));
83 break;
84 case Primitive::kPrimShort:
85 shadow_frame.SetVReg(vregA, f->GetShort(obj));
86 break;
87 case Primitive::kPrimInt:
88 shadow_frame.SetVReg(vregA, f->GetInt(obj));
89 break;
90 case Primitive::kPrimLong:
91 shadow_frame.SetVRegLong(vregA, f->GetLong(obj));
92 break;
93 case Primitive::kPrimNot:
Mathieu Chartier3398c782016-09-30 10:27:43 -070094 shadow_frame.SetVRegReference(vregA, f->GetObject(obj).Decode());
Ian Rogers54874942014-06-10 16:31:03 -070095 break;
96 default:
97 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -070098 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -070099 }
100 return true;
101}
102
103// Explicitly instantiate all DoFieldGet functions.
104#define EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, _do_check) \
105 template bool DoFieldGet<_find_type, _field_type, _do_check>(Thread* self, \
106 ShadowFrame& shadow_frame, \
107 const Instruction* inst, \
108 uint16_t inst_data)
109
110#define EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(_find_type, _field_type) \
111 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, false); \
112 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, true);
113
114// iget-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700115EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimBoolean)
116EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimByte)
117EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimChar)
118EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimShort)
119EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimInt)
120EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimLong)
121EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstanceObjectRead, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700122
123// sget-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700124EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimBoolean)
125EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimByte)
126EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimChar)
127EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimShort)
128EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimInt)
129EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimLong)
130EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticObjectRead, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700131
132#undef EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL
133#undef EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL
134
135// Handles iget-quick, iget-wide-quick and iget-object-quick instructions.
136// Returns true on success, otherwise throws an exception and returns false.
137template<Primitive::Type field_type>
138bool DoIGetQuick(ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
139 Object* obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
140 if (UNLIKELY(obj == nullptr)) {
141 // We lost the reference to the field index so we cannot get a more
142 // precised exception message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000143 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -0700144 return false;
145 }
146 MemberOffset field_offset(inst->VRegC_22c());
147 // Report this field access to instrumentation if needed. Since we only have the offset of
148 // the field from the base of the object, we need to look for it first.
149 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
150 if (UNLIKELY(instrumentation->HasFieldReadListeners())) {
151 ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(),
152 field_offset.Uint32Value());
153 DCHECK(f != nullptr);
154 DCHECK(!f->IsStatic());
155 instrumentation->FieldReadEvent(Thread::Current(), obj, shadow_frame.GetMethod(),
156 shadow_frame.GetDexPC(), f);
157 }
158 // Note: iget-x-quick instructions are only for non-volatile fields.
159 const uint32_t vregA = inst->VRegA_22c(inst_data);
160 switch (field_type) {
161 case Primitive::kPrimInt:
162 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetField32(field_offset)));
163 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800164 case Primitive::kPrimBoolean:
165 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldBoolean(field_offset)));
166 break;
167 case Primitive::kPrimByte:
168 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldByte(field_offset)));
169 break;
170 case Primitive::kPrimChar:
171 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldChar(field_offset)));
172 break;
173 case Primitive::kPrimShort:
174 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldShort(field_offset)));
175 break;
Ian Rogers54874942014-06-10 16:31:03 -0700176 case Primitive::kPrimLong:
177 shadow_frame.SetVRegLong(vregA, static_cast<int64_t>(obj->GetField64(field_offset)));
178 break;
179 case Primitive::kPrimNot:
180 shadow_frame.SetVRegReference(vregA, obj->GetFieldObject<mirror::Object>(field_offset));
181 break;
182 default:
183 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700184 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700185 }
186 return true;
187}
188
189// Explicitly instantiate all DoIGetQuick functions.
190#define EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(_field_type) \
191 template bool DoIGetQuick<_field_type>(ShadowFrame& shadow_frame, const Instruction* inst, \
192 uint16_t inst_data)
193
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800194EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimInt); // iget-quick.
195EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimBoolean); // iget-boolean-quick.
196EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimByte); // iget-byte-quick.
197EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimChar); // iget-char-quick.
198EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimShort); // iget-short-quick.
199EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimLong); // iget-wide-quick.
200EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimNot); // iget-object-quick.
Ian Rogers54874942014-06-10 16:31:03 -0700201#undef EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL
202
203template<Primitive::Type field_type>
204static JValue GetFieldValue(const ShadowFrame& shadow_frame, uint32_t vreg)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700205 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers54874942014-06-10 16:31:03 -0700206 JValue field_value;
207 switch (field_type) {
208 case Primitive::kPrimBoolean:
209 field_value.SetZ(static_cast<uint8_t>(shadow_frame.GetVReg(vreg)));
210 break;
211 case Primitive::kPrimByte:
212 field_value.SetB(static_cast<int8_t>(shadow_frame.GetVReg(vreg)));
213 break;
214 case Primitive::kPrimChar:
215 field_value.SetC(static_cast<uint16_t>(shadow_frame.GetVReg(vreg)));
216 break;
217 case Primitive::kPrimShort:
218 field_value.SetS(static_cast<int16_t>(shadow_frame.GetVReg(vreg)));
219 break;
220 case Primitive::kPrimInt:
221 field_value.SetI(shadow_frame.GetVReg(vreg));
222 break;
223 case Primitive::kPrimLong:
224 field_value.SetJ(shadow_frame.GetVRegLong(vreg));
225 break;
226 case Primitive::kPrimNot:
227 field_value.SetL(shadow_frame.GetVRegReference(vreg));
228 break;
229 default:
230 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700231 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700232 }
233 return field_value;
234}
235
236template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check,
237 bool transaction_active>
238bool DoFieldPut(Thread* self, const ShadowFrame& shadow_frame, const Instruction* inst,
239 uint16_t inst_data) {
240 bool do_assignability_check = do_access_check;
241 bool is_static = (find_type == StaticObjectWrite) || (find_type == StaticPrimitiveWrite);
242 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Roland Levillain4b8f1ec2015-08-26 18:34:03 +0100243 ArtField* f =
244 FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
245 Primitive::ComponentSize(field_type));
Ian Rogers54874942014-06-10 16:31:03 -0700246 if (UNLIKELY(f == nullptr)) {
247 CHECK(self->IsExceptionPending());
248 return false;
249 }
Mathieu Chartier3398c782016-09-30 10:27:43 -0700250 ObjPtr<Object> obj;
Ian Rogers54874942014-06-10 16:31:03 -0700251 if (is_static) {
252 obj = f->GetDeclaringClass();
253 } else {
254 obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
255 if (UNLIKELY(obj == nullptr)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000256 ThrowNullPointerExceptionForFieldAccess(f, false);
Ian Rogers54874942014-06-10 16:31:03 -0700257 return false;
258 }
259 }
Sebastien Hertz1edbd8e2014-07-16 20:00:11 +0200260 f->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self);
Ian Rogers54874942014-06-10 16:31:03 -0700261 uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
262 // Report this field access to instrumentation if needed. Since we only have the offset of
263 // the field from the base of the object, we need to look for it first.
264 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
265 if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
266 JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA);
Mathieu Chartier3398c782016-09-30 10:27:43 -0700267 ObjPtr<Object> this_object = f->IsStatic() ? nullptr : obj;
268 instrumentation->FieldWriteEvent(self, this_object.Decode(),
269 shadow_frame.GetMethod(),
270 shadow_frame.GetDexPC(),
271 f,
272 field_value);
Ian Rogers54874942014-06-10 16:31:03 -0700273 }
274 switch (field_type) {
275 case Primitive::kPrimBoolean:
276 f->SetBoolean<transaction_active>(obj, shadow_frame.GetVReg(vregA));
277 break;
278 case Primitive::kPrimByte:
279 f->SetByte<transaction_active>(obj, shadow_frame.GetVReg(vregA));
280 break;
281 case Primitive::kPrimChar:
282 f->SetChar<transaction_active>(obj, shadow_frame.GetVReg(vregA));
283 break;
284 case Primitive::kPrimShort:
285 f->SetShort<transaction_active>(obj, shadow_frame.GetVReg(vregA));
286 break;
287 case Primitive::kPrimInt:
288 f->SetInt<transaction_active>(obj, shadow_frame.GetVReg(vregA));
289 break;
290 case Primitive::kPrimLong:
291 f->SetLong<transaction_active>(obj, shadow_frame.GetVRegLong(vregA));
292 break;
293 case Primitive::kPrimNot: {
294 Object* reg = shadow_frame.GetVRegReference(vregA);
295 if (do_assignability_check && reg != nullptr) {
296 // FieldHelper::GetType can resolve classes, use a handle wrapper which will restore the
297 // object in the destructor.
Mathieu Chartier3398c782016-09-30 10:27:43 -0700298 ObjPtr<Class> field_class;
Ian Rogers54874942014-06-10 16:31:03 -0700299 {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700300 StackHandleScope<2> hs(self);
Ian Rogers54874942014-06-10 16:31:03 -0700301 HandleWrapper<mirror::Object> h_reg(hs.NewHandleWrapper(&reg));
Mathieu Chartier3398c782016-09-30 10:27:43 -0700302 HandleWrapperObjPtr<mirror::Object> h_obj(hs.NewHandleWrapper(&obj));
Mathieu Chartierc7853442015-03-27 14:35:38 -0700303 field_class = f->GetType<true>();
Ian Rogers54874942014-06-10 16:31:03 -0700304 }
Mathieu Chartier3398c782016-09-30 10:27:43 -0700305 if (!reg->VerifierInstanceOf(field_class.Decode())) {
Ian Rogers54874942014-06-10 16:31:03 -0700306 // This should never happen.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700307 std::string temp1, temp2, temp3;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000308 self->ThrowNewExceptionF("Ljava/lang/VirtualMachineError;",
Ian Rogers54874942014-06-10 16:31:03 -0700309 "Put '%s' that is not instance of field '%s' in '%s'",
Ian Rogers1ff3c982014-08-12 02:30:58 -0700310 reg->GetClass()->GetDescriptor(&temp1),
311 field_class->GetDescriptor(&temp2),
312 f->GetDeclaringClass()->GetDescriptor(&temp3));
Ian Rogers54874942014-06-10 16:31:03 -0700313 return false;
314 }
315 }
316 f->SetObj<transaction_active>(obj, reg);
317 break;
318 }
319 default:
320 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700321 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700322 }
323 return true;
324}
325
326// Explicitly instantiate all DoFieldPut functions.
327#define EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, _do_check, _transaction_active) \
328 template bool DoFieldPut<_find_type, _field_type, _do_check, _transaction_active>(Thread* self, \
329 const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data)
330
331#define EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(_find_type, _field_type) \
332 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, false); \
333 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, false); \
334 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, true); \
335 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, true);
336
337// iput-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700338EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimBoolean)
339EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimByte)
340EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimChar)
341EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimShort)
342EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimInt)
343EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimLong)
344EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstanceObjectWrite, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700345
346// sput-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700347EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimBoolean)
348EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimByte)
349EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimChar)
350EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimShort)
351EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimInt)
352EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimLong)
353EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticObjectWrite, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700354
355#undef EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL
356#undef EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL
357
358template<Primitive::Type field_type, bool transaction_active>
359bool DoIPutQuick(const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
360 Object* obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
361 if (UNLIKELY(obj == nullptr)) {
362 // We lost the reference to the field index so we cannot get a more
363 // precised exception message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000364 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -0700365 return false;
366 }
367 MemberOffset field_offset(inst->VRegC_22c());
368 const uint32_t vregA = inst->VRegA_22c(inst_data);
369 // Report this field modification to instrumentation if needed. Since we only have the offset of
370 // the field from the base of the object, we need to look for it first.
371 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
372 if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
373 ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(),
374 field_offset.Uint32Value());
375 DCHECK(f != nullptr);
376 DCHECK(!f->IsStatic());
377 JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA);
378 instrumentation->FieldWriteEvent(Thread::Current(), obj, shadow_frame.GetMethod(),
379 shadow_frame.GetDexPC(), f, field_value);
380 }
381 // Note: iput-x-quick instructions are only for non-volatile fields.
382 switch (field_type) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700383 case Primitive::kPrimBoolean:
384 obj->SetFieldBoolean<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
385 break;
386 case Primitive::kPrimByte:
387 obj->SetFieldByte<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
388 break;
389 case Primitive::kPrimChar:
390 obj->SetFieldChar<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
391 break;
392 case Primitive::kPrimShort:
393 obj->SetFieldShort<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
394 break;
Ian Rogers54874942014-06-10 16:31:03 -0700395 case Primitive::kPrimInt:
396 obj->SetField32<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
397 break;
398 case Primitive::kPrimLong:
399 obj->SetField64<transaction_active>(field_offset, shadow_frame.GetVRegLong(vregA));
400 break;
401 case Primitive::kPrimNot:
402 obj->SetFieldObject<transaction_active>(field_offset, shadow_frame.GetVRegReference(vregA));
403 break;
404 default:
405 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700406 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700407 }
408 return true;
409}
410
411// Explicitly instantiate all DoIPutQuick functions.
412#define EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, _transaction_active) \
413 template bool DoIPutQuick<_field_type, _transaction_active>(const ShadowFrame& shadow_frame, \
414 const Instruction* inst, \
415 uint16_t inst_data)
416
417#define EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(_field_type) \
418 EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, false); \
419 EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, true);
420
Andreas Gampec8ccf682014-09-29 20:07:43 -0700421EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimInt) // iput-quick.
422EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimBoolean) // iput-boolean-quick.
423EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimByte) // iput-byte-quick.
424EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimChar) // iput-char-quick.
425EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimShort) // iput-short-quick.
426EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimLong) // iput-wide-quick.
427EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimNot) // iput-object-quick.
Ian Rogers54874942014-06-10 16:31:03 -0700428#undef EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL
429#undef EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL
430
Sebastien Hertz520633b2015-09-08 17:03:36 +0200431// We accept a null Instrumentation* meaning we must not report anything to the instrumentation.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700432uint32_t FindNextInstructionFollowingException(
433 Thread* self, ShadowFrame& shadow_frame, uint32_t dex_pc,
434 const instrumentation::Instrumentation* instrumentation) {
Ian Rogers54874942014-06-10 16:31:03 -0700435 self->VerifyStack();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700436 StackHandleScope<2> hs(self);
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000437 Handle<mirror::Throwable> exception(hs.NewHandle(self->GetException()));
Sebastien Hertz520633b2015-09-08 17:03:36 +0200438 if (instrumentation != nullptr && instrumentation->HasExceptionCaughtListeners()
Nicolas Geoffray7642cfc2015-02-26 10:56:09 +0000439 && self->IsExceptionThrownByCurrentMethod(exception.Get())) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000440 instrumentation->ExceptionCaughtEvent(self, exception.Get());
Sebastien Hertz9f102032014-05-23 08:59:42 +0200441 }
Ian Rogers54874942014-06-10 16:31:03 -0700442 bool clear_exception = false;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700443 uint32_t found_dex_pc = shadow_frame.GetMethod()->FindCatchBlock(
444 hs.NewHandle(exception->GetClass()), dex_pc, &clear_exception);
Sebastien Hertz520633b2015-09-08 17:03:36 +0200445 if (found_dex_pc == DexFile::kDexNoIndex && instrumentation != nullptr) {
Nicolas Geoffray7642cfc2015-02-26 10:56:09 +0000446 // Exception is not caught by the current method. We will unwind to the
447 // caller. Notify any instrumentation listener.
Sebastien Hertz9f102032014-05-23 08:59:42 +0200448 instrumentation->MethodUnwindEvent(self, shadow_frame.GetThisObject(),
Ian Rogers54874942014-06-10 16:31:03 -0700449 shadow_frame.GetMethod(), dex_pc);
450 } else {
Nicolas Geoffray7642cfc2015-02-26 10:56:09 +0000451 // Exception is caught in the current method. We will jump to the found_dex_pc.
Ian Rogers54874942014-06-10 16:31:03 -0700452 if (clear_exception) {
453 self->ClearException();
454 }
455 }
456 return found_dex_pc;
457}
458
Ian Rogerse94652f2014-12-02 11:13:19 -0800459void UnexpectedOpcode(const Instruction* inst, const ShadowFrame& shadow_frame) {
460 LOG(FATAL) << "Unexpected instruction: "
461 << inst->DumpString(shadow_frame.GetMethod()->GetDexFile());
462 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700463}
464
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200465// Assign register 'src_reg' from shadow_frame to register 'dest_reg' into new_shadow_frame.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800466static inline void AssignRegister(ShadowFrame* new_shadow_frame, const ShadowFrame& shadow_frame,
467 size_t dest_reg, size_t src_reg)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700468 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe7104cbf2014-03-21 11:44:43 -0700469 // Uint required, so that sign extension does not make this wrong on 64b systems
470 uint32_t src_value = shadow_frame.GetVReg(src_reg);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800471 mirror::Object* o = shadow_frame.GetVRegReference<kVerifyNone>(src_reg);
Igor Murashkinc449e8b2015-06-10 15:56:42 -0700472
473 // If both register locations contains the same value, the register probably holds a reference.
474 // Note: As an optimization, non-moving collectors leave a stale reference value
475 // in the references array even after the original vreg was overwritten to a non-reference.
Andreas Gampe7104cbf2014-03-21 11:44:43 -0700476 if (src_value == reinterpret_cast<uintptr_t>(o)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800477 new_shadow_frame->SetVRegReference(dest_reg, o);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200478 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800479 new_shadow_frame->SetVReg(dest_reg, src_value);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200480 }
481}
482
Sebastien Hertz45b15972015-04-03 16:07:05 +0200483void AbortTransactionF(Thread* self, const char* fmt, ...) {
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700484 va_list args;
485 va_start(args, fmt);
Sebastien Hertz45b15972015-04-03 16:07:05 +0200486 AbortTransactionV(self, fmt, args);
487 va_end(args);
488}
489
490void AbortTransactionV(Thread* self, const char* fmt, va_list args) {
491 CHECK(Runtime::Current()->IsActiveTransaction());
492 // Constructs abort message.
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100493 std::string abort_msg;
494 StringAppendV(&abort_msg, fmt, args);
495 // Throws an exception so we can abort the transaction and rollback every change.
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200496 Runtime::Current()->AbortTransactionAndThrowAbortError(self, abort_msg);
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700497}
498
Igor Murashkin158f35c2015-06-10 15:55:30 -0700499// Separate declaration is required solely for the attributes.
Igor Murashkin6918bf12015-09-27 19:19:06 -0700500template <bool is_range,
501 bool do_assignability_check,
502 size_t kVarArgMax>
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700503 REQUIRES_SHARED(Locks::mutator_lock_)
Igor Murashkin158f35c2015-06-10 15:55:30 -0700504static inline bool DoCallCommon(ArtMethod* called_method,
505 Thread* self,
506 ShadowFrame& shadow_frame,
507 JValue* result,
508 uint16_t number_of_inputs,
Igor Murashkin6918bf12015-09-27 19:19:06 -0700509 uint32_t (&arg)[kVarArgMax],
Igor Murashkin158f35c2015-06-10 15:55:30 -0700510 uint32_t vregC) ALWAYS_INLINE;
511
Siva Chandra05d24152016-01-05 17:43:17 -0800512void ArtInterpreterToCompiledCodeBridge(Thread* self,
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100513 ArtMethod* caller,
Siva Chandra05d24152016-01-05 17:43:17 -0800514 const DexFile::CodeItem* code_item,
515 ShadowFrame* shadow_frame,
516 JValue* result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700517 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700518 ArtMethod* method = shadow_frame->GetMethod();
519 // Ensure static methods are initialized.
520 if (method->IsStatic()) {
521 mirror::Class* declaringClass = method->GetDeclaringClass();
522 if (UNLIKELY(!declaringClass->IsInitialized())) {
523 self->PushShadowFrame(shadow_frame);
524 StackHandleScope<1> hs(self);
525 Handle<mirror::Class> h_class(hs.NewHandle(declaringClass));
526 if (UNLIKELY(!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true,
527 true))) {
528 self->PopShadowFrame();
529 DCHECK(self->IsExceptionPending());
530 return;
531 }
532 self->PopShadowFrame();
533 CHECK(h_class->IsInitializing());
534 // Reload from shadow frame in case the method moved, this is faster than adding a handle.
535 method = shadow_frame->GetMethod();
536 }
537 }
538 uint16_t arg_offset = (code_item == nullptr)
539 ? 0
540 : code_item->registers_size_ - code_item->ins_size_;
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100541 jit::Jit* jit = Runtime::Current()->GetJit();
542 if (jit != nullptr && caller != nullptr) {
543 jit->NotifyInterpreterToCompiledCodeTransition(self, caller);
544 }
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700545 method->Invoke(self, shadow_frame->GetVRegArgs(arg_offset),
546 (shadow_frame->NumberOfVRegs() - arg_offset) * sizeof(uint32_t),
Andreas Gampe542451c2016-07-26 09:02:02 -0700547 result, method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty());
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700548}
549
Mingyao Yangffedec52016-05-19 10:48:40 -0700550void SetStringInitValueToAllAliases(ShadowFrame* shadow_frame,
551 uint16_t this_obj_vreg,
552 JValue result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700553 REQUIRES_SHARED(Locks::mutator_lock_) {
Mingyao Yangffedec52016-05-19 10:48:40 -0700554 Object* existing = shadow_frame->GetVRegReference(this_obj_vreg);
555 if (existing == nullptr) {
556 // If it's null, we come from compiled code that was deoptimized. Nothing to do,
557 // as the compiler verified there was no alias.
558 // Set the new string result of the StringFactory.
559 shadow_frame->SetVRegReference(this_obj_vreg, result.GetL());
560 return;
561 }
562 // Set the string init result into all aliases.
563 for (uint32_t i = 0, e = shadow_frame->NumberOfVRegs(); i < e; ++i) {
564 if (shadow_frame->GetVRegReference(i) == existing) {
565 DCHECK_EQ(shadow_frame->GetVRegReference(i),
566 reinterpret_cast<mirror::Object*>(shadow_frame->GetVReg(i)));
567 shadow_frame->SetVRegReference(i, result.GetL());
568 DCHECK_EQ(shadow_frame->GetVRegReference(i),
569 reinterpret_cast<mirror::Object*>(shadow_frame->GetVReg(i)));
570 }
571 }
572}
573
Igor Murashkin6918bf12015-09-27 19:19:06 -0700574template <bool is_range,
575 bool do_assignability_check,
576 size_t kVarArgMax>
Igor Murashkin158f35c2015-06-10 15:55:30 -0700577static inline bool DoCallCommon(ArtMethod* called_method,
578 Thread* self,
579 ShadowFrame& shadow_frame,
580 JValue* result,
581 uint16_t number_of_inputs,
Igor Murashkin6918bf12015-09-27 19:19:06 -0700582 uint32_t (&arg)[kVarArgMax],
Igor Murashkin158f35c2015-06-10 15:55:30 -0700583 uint32_t vregC) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800584 bool string_init = false;
585 // Replace calls to String.<init> with equivalent StringFactory call.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700586 if (UNLIKELY(called_method->GetDeclaringClass()->IsStringClass()
587 && called_method->IsConstructor())) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100588 called_method = WellKnownClasses::StringInitToStringFactory(called_method);
Jeff Hao848f70a2014-01-15 13:49:50 -0800589 string_init = true;
590 }
591
Alex Lightdaf58c82016-03-16 23:00:49 +0000592 // Compute method information.
593 const DexFile::CodeItem* code_item = called_method->GetCodeItem();
Igor Murashkin158f35c2015-06-10 15:55:30 -0700594
595 // Number of registers for the callee's call frame.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200596 uint16_t num_regs;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700597 if (LIKELY(code_item != nullptr)) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200598 num_regs = code_item->registers_size_;
Igor Murashkin158f35c2015-06-10 15:55:30 -0700599 DCHECK_EQ(string_init ? number_of_inputs - 1 : number_of_inputs, code_item->ins_size_);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200600 } else {
Ian Rogerse94652f2014-12-02 11:13:19 -0800601 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
Igor Murashkin158f35c2015-06-10 15:55:30 -0700602 num_regs = number_of_inputs;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200603 }
604
Igor Murashkin158f35c2015-06-10 15:55:30 -0700605 // Hack for String init:
606 //
607 // Rewrite invoke-x java.lang.String.<init>(this, a, b, c, ...) into:
608 // invoke-x StringFactory(a, b, c, ...)
609 // by effectively dropping the first virtual register from the invoke.
610 //
611 // (at this point the ArtMethod has already been replaced,
612 // so we just need to fix-up the arguments)
David Brazdil65902e82016-01-15 09:35:13 +0000613 //
614 // Note that FindMethodFromCode in entrypoint_utils-inl.h was also special-cased
615 // to handle the compiler optimization of replacing `this` with null without
616 // throwing NullPointerException.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700617 uint32_t string_init_vreg_this = is_range ? vregC : arg[0];
Igor Murashkina06b49b2015-06-25 15:18:12 -0700618 if (UNLIKELY(string_init)) {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700619 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 -0700620
Igor Murashkin158f35c2015-06-10 15:55:30 -0700621 // The new StringFactory call is static and has one fewer argument.
Igor Murashkina06b49b2015-06-25 15:18:12 -0700622 if (code_item == nullptr) {
623 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
624 num_regs--;
625 } // 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 -0700626 number_of_inputs--;
627
628 // Rewrite the var-args, dropping the 0th argument ("this")
Igor Murashkin6918bf12015-09-27 19:19:06 -0700629 for (uint32_t i = 1; i < arraysize(arg); ++i) {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700630 arg[i - 1] = arg[i];
631 }
Igor Murashkin6918bf12015-09-27 19:19:06 -0700632 arg[arraysize(arg) - 1] = 0;
Igor Murashkin158f35c2015-06-10 15:55:30 -0700633
634 // Rewrite the non-var-arg case
635 vregC++; // Skips the 0th vreg in the range ("this").
636 }
637
638 // Parameter registers go at the end of the shadow frame.
639 DCHECK_GE(num_regs, number_of_inputs);
640 size_t first_dest_reg = num_regs - number_of_inputs;
641 DCHECK_NE(first_dest_reg, (size_t)-1);
642
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200643 // Allocate shadow frame on the stack.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700644 const char* old_cause = self->StartAssertNoThreadSuspension("DoCallCommon");
Andreas Gampeb3025922015-09-01 14:45:00 -0700645 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
Andreas Gampe03ec9302015-08-27 17:41:47 -0700646 CREATE_SHADOW_FRAME(num_regs, &shadow_frame, called_method, /* dex pc */ 0);
Andreas Gampeb3025922015-09-01 14:45:00 -0700647 ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get();
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200648
Igor Murashkin158f35c2015-06-10 15:55:30 -0700649 // Initialize new shadow frame by copying the registers from the callee shadow frame.
Jeff Haoa3faaf42013-09-03 19:07:00 -0700650 if (do_assignability_check) {
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -0700651 // Slow path.
652 // We might need to do class loading, which incurs a thread state change to kNative. So
653 // register the shadow frame as under construction and allow suspension again.
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700654 ScopedStackedShadowFramePusher pusher(
Sebastien Hertzf7958692015-06-09 14:09:14 +0200655 self, new_shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -0700656 self->EndAssertNoThreadSuspension(old_cause);
657
Igor Murashkin9f95ba72016-02-01 14:21:25 -0800658 // ArtMethod here is needed to check type information of the call site against the callee.
659 // Type information is retrieved from a DexFile/DexCache for that respective declared method.
660 //
661 // As a special case for proxy methods, which are not dex-backed,
662 // we have to retrieve type information from the proxy's method
663 // interface method instead (which is dex backed since proxies are never interfaces).
Andreas Gampe542451c2016-07-26 09:02:02 -0700664 ArtMethod* method =
665 new_shadow_frame->GetMethod()->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Igor Murashkin9f95ba72016-02-01 14:21:25 -0800666
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -0700667 // We need to do runtime check on reference assignment. We need to load the shorty
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200668 // to get the exact type of each reference argument.
Igor Murashkin9f95ba72016-02-01 14:21:25 -0800669 const DexFile::TypeList* params = method->GetParameterTypeList();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700670 uint32_t shorty_len = 0;
Igor Murashkin9f95ba72016-02-01 14:21:25 -0800671 const char* shorty = method->GetShorty(&shorty_len);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200672
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100673 // Handle receiver apart since it's not part of the shorty.
674 size_t dest_reg = first_dest_reg;
675 size_t arg_offset = 0;
Igor Murashkin158f35c2015-06-10 15:55:30 -0700676
Igor Murashkin9f95ba72016-02-01 14:21:25 -0800677 if (!method->IsStatic()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700678 size_t receiver_reg = is_range ? vregC : arg[0];
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100679 new_shadow_frame->SetVRegReference(dest_reg, shadow_frame.GetVRegReference(receiver_reg));
680 ++dest_reg;
681 ++arg_offset;
Igor Murashkina06b49b2015-06-25 15:18:12 -0700682 DCHECK(!string_init); // All StringFactory methods are static.
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100683 }
Igor Murashkin158f35c2015-06-10 15:55:30 -0700684
685 // Copy the caller's invoke-* arguments into the callee's parameter registers.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800686 for (uint32_t shorty_pos = 0; dest_reg < num_regs; ++shorty_pos, ++dest_reg, ++arg_offset) {
Igor Murashkina06b49b2015-06-25 15:18:12 -0700687 // Skip the 0th 'shorty' type since it represents the return type.
688 DCHECK_LT(shorty_pos + 1, shorty_len) << "for shorty '" << shorty << "'";
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200689 const size_t src_reg = (is_range) ? vregC + arg_offset : arg[arg_offset];
690 switch (shorty[shorty_pos + 1]) {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700691 // Handle Object references. 1 virtual register slot.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200692 case 'L': {
693 Object* o = shadow_frame.GetVRegReference(src_reg);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700694 if (do_assignability_check && o != nullptr) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700695 PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Ian Rogersa0485602014-12-02 15:48:04 -0800696 Class* arg_type =
Igor Murashkin9f95ba72016-02-01 14:21:25 -0800697 method->GetClassFromTypeIndex(
Vladimir Marko05792b92015-08-03 11:56:49 +0100698 params->GetTypeItem(shorty_pos).type_idx_, true /* resolve */, pointer_size);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700699 if (arg_type == nullptr) {
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200700 CHECK(self->IsExceptionPending());
701 return false;
702 }
703 if (!o->VerifierInstanceOf(arg_type)) {
704 // This should never happen.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700705 std::string temp1, temp2;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000706 self->ThrowNewExceptionF("Ljava/lang/VirtualMachineError;",
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200707 "Invoking %s with bad arg %d, type '%s' not instance of '%s'",
Ian Rogerse94652f2014-12-02 11:13:19 -0800708 new_shadow_frame->GetMethod()->GetName(), shorty_pos,
Ian Rogers1ff3c982014-08-12 02:30:58 -0700709 o->GetClass()->GetDescriptor(&temp1),
710 arg_type->GetDescriptor(&temp2));
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200711 return false;
712 }
Jeff Haoa3faaf42013-09-03 19:07:00 -0700713 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200714 new_shadow_frame->SetVRegReference(dest_reg, o);
715 break;
Jeff Haoa3faaf42013-09-03 19:07:00 -0700716 }
Igor Murashkin158f35c2015-06-10 15:55:30 -0700717 // Handle doubles and longs. 2 consecutive virtual register slots.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200718 case 'J': case 'D': {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700719 uint64_t wide_value =
720 (static_cast<uint64_t>(shadow_frame.GetVReg(src_reg + 1)) << BitSizeOf<uint32_t>()) |
721 static_cast<uint32_t>(shadow_frame.GetVReg(src_reg));
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200722 new_shadow_frame->SetVRegLong(dest_reg, wide_value);
Igor Murashkin158f35c2015-06-10 15:55:30 -0700723 // Skip the next virtual register slot since we already used it.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200724 ++dest_reg;
725 ++arg_offset;
726 break;
727 }
Igor Murashkin158f35c2015-06-10 15:55:30 -0700728 // Handle all other primitives that are always 1 virtual register slot.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200729 default:
730 new_shadow_frame->SetVReg(dest_reg, shadow_frame.GetVReg(src_reg));
731 break;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200732 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200733 }
734 } else {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700735 size_t arg_index = 0;
736
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200737 // Fast path: no extra checks.
738 if (is_range) {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700739 uint16_t first_src_reg = vregC;
740
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200741 for (size_t src_reg = first_src_reg, dest_reg = first_dest_reg; dest_reg < num_regs;
742 ++dest_reg, ++src_reg) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800743 AssignRegister(new_shadow_frame, shadow_frame, dest_reg, src_reg);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200744 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200745 } else {
Igor Murashkin6918bf12015-09-27 19:19:06 -0700746 DCHECK_LE(number_of_inputs, arraysize(arg));
Igor Murashkin158f35c2015-06-10 15:55:30 -0700747
748 for (; arg_index < number_of_inputs; ++arg_index) {
749 AssignRegister(new_shadow_frame, shadow_frame, first_dest_reg + arg_index, arg[arg_index]);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200750 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200751 }
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -0700752 self->EndAssertNoThreadSuspension(old_cause);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200753 }
754
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200755 // Do the call now.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200756 if (LIKELY(Runtime::Current()->IsStarted())) {
Tamas Berghammerdd5e5e92016-02-12 16:29:00 +0000757 ArtMethod* target = new_shadow_frame->GetMethod();
758 if (ClassLinker::ShouldUseInterpreterEntrypoint(
759 target,
760 target->GetEntryPointFromQuickCompiledCode())) {
Tamas Berghammerc94a61f2016-02-05 18:09:08 +0000761 ArtInterpreterToInterpreterBridge(self, code_item, new_shadow_frame, result);
Tamas Berghammer3a98aae2016-02-08 20:21:54 +0000762 } else {
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100763 ArtInterpreterToCompiledCodeBridge(
764 self, shadow_frame.GetMethod(), code_item, new_shadow_frame, result);
Nicolas Geoffray7070ccd2015-07-08 09:41:54 +0000765 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200766 } else {
Andreas Gampe799681b2015-05-15 19:24:12 -0700767 UnstartedRuntime::Invoke(self, code_item, new_shadow_frame, result, first_dest_reg);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200768 }
Jeff Hao848f70a2014-01-15 13:49:50 -0800769
770 if (string_init && !self->IsExceptionPending()) {
Mingyao Yangffedec52016-05-19 10:48:40 -0700771 SetStringInitValueToAllAliases(&shadow_frame, string_init_vreg_this, *result);
Jeff Hao848f70a2014-01-15 13:49:50 -0800772 }
773
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200774 return !self->IsExceptionPending();
775}
776
Igor Murashkin158f35c2015-06-10 15:55:30 -0700777template<bool is_range, bool do_assignability_check>
Igor Murashkin158f35c2015-06-10 15:55:30 -0700778bool DoCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame,
779 const Instruction* inst, uint16_t inst_data, JValue* result) {
780 // Argument word count.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +0100781 const uint16_t number_of_inputs =
782 (is_range) ? inst->VRegA_3rc(inst_data) : inst->VRegA_35c(inst_data);
Igor Murashkin158f35c2015-06-10 15:55:30 -0700783
784 // TODO: find a cleaner way to separate non-range and range information without duplicating
785 // code.
Igor Murashkin6918bf12015-09-27 19:19:06 -0700786 uint32_t arg[Instruction::kMaxVarArgRegs] = {}; // only used in invoke-XXX.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700787 uint32_t vregC = 0;
788 if (is_range) {
789 vregC = inst->VRegC_3rc();
790 } else {
791 vregC = inst->VRegC_35c();
792 inst->GetVarArgs(arg, inst_data);
793 }
794
795 return DoCallCommon<is_range, do_assignability_check>(
796 called_method, self, shadow_frame,
797 result, number_of_inputs, arg, vregC);
798}
799
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100800template <bool is_range, bool do_access_check, bool transaction_active>
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200801bool DoFilledNewArray(const Instruction* inst, const ShadowFrame& shadow_frame,
802 Thread* self, JValue* result) {
803 DCHECK(inst->Opcode() == Instruction::FILLED_NEW_ARRAY ||
804 inst->Opcode() == Instruction::FILLED_NEW_ARRAY_RANGE);
805 const int32_t length = is_range ? inst->VRegA_3rc() : inst->VRegA_35c();
806 if (!is_range) {
807 // Checks FILLED_NEW_ARRAY's length does not exceed 5 arguments.
808 CHECK_LE(length, 5);
809 }
810 if (UNLIKELY(length < 0)) {
811 ThrowNegativeArraySizeException(length);
812 return false;
813 }
814 uint16_t type_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700815 Class* array_class = ResolveVerifyAndClinit(type_idx, shadow_frame.GetMethod(),
816 self, false, do_access_check);
817 if (UNLIKELY(array_class == nullptr)) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200818 DCHECK(self->IsExceptionPending());
819 return false;
820 }
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700821 CHECK(array_class->IsArrayClass());
822 Class* component_class = array_class->GetComponentType();
823 const bool is_primitive_int_component = component_class->IsPrimitiveInt();
824 if (UNLIKELY(component_class->IsPrimitive() && !is_primitive_int_component)) {
825 if (component_class->IsPrimitiveLong() || component_class->IsPrimitiveDouble()) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200826 ThrowRuntimeException("Bad filled array request for type %s",
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700827 PrettyDescriptor(component_class).c_str());
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200828 } else {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000829 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
Brian Carlstrom4fa0bcd2013-12-10 11:24:21 -0800830 "Found type %s; filled-new-array not implemented for anything but 'int'",
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700831 PrettyDescriptor(component_class).c_str());
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200832 }
833 return false;
834 }
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700835 Object* new_array = Array::Alloc<true>(self, array_class, length,
836 array_class->GetComponentSizeShift(),
837 Runtime::Current()->GetHeap()->GetCurrentAllocator());
838 if (UNLIKELY(new_array == nullptr)) {
839 self->AssertPendingOOMException();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200840 return false;
841 }
Igor Murashkin158f35c2015-06-10 15:55:30 -0700842 uint32_t arg[Instruction::kMaxVarArgRegs]; // only used in filled-new-array.
843 uint32_t vregC = 0; // only used in filled-new-array-range.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200844 if (is_range) {
Sebastien Hertzabff6432014-01-27 18:01:39 +0100845 vregC = inst->VRegC_3rc();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200846 } else {
Ian Rogers29a26482014-05-02 15:27:29 -0700847 inst->GetVarArgs(arg);
Sebastien Hertzabff6432014-01-27 18:01:39 +0100848 }
Sebastien Hertzabff6432014-01-27 18:01:39 +0100849 for (int32_t i = 0; i < length; ++i) {
850 size_t src_reg = is_range ? vregC + i : arg[i];
851 if (is_primitive_int_component) {
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700852 new_array->AsIntArray()->SetWithoutChecks<transaction_active>(
853 i, shadow_frame.GetVReg(src_reg));
Sebastien Hertzabff6432014-01-27 18:01:39 +0100854 } else {
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700855 new_array->AsObjectArray<Object>()->SetWithoutChecks<transaction_active>(
856 i, shadow_frame.GetVRegReference(src_reg));
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200857 }
858 }
859
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700860 result->SetL(new_array);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200861 return true;
862}
863
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700864// TODO fix thread analysis: should be REQUIRES_SHARED(Locks::mutator_lock_).
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100865template<typename T>
866static void RecordArrayElementsInTransactionImpl(mirror::PrimitiveArray<T>* array, int32_t count)
867 NO_THREAD_SAFETY_ANALYSIS {
868 Runtime* runtime = Runtime::Current();
869 for (int32_t i = 0; i < count; ++i) {
870 runtime->RecordWriteArray(array, i, array->GetWithoutChecks(i));
871 }
872}
873
874void RecordArrayElementsInTransaction(mirror::Array* array, int32_t count)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700875 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100876 DCHECK(Runtime::Current()->IsActiveTransaction());
877 DCHECK(array != nullptr);
878 DCHECK_LE(count, array->GetLength());
879 Primitive::Type primitive_component_type = array->GetClass()->GetComponentType()->GetPrimitiveType();
880 switch (primitive_component_type) {
881 case Primitive::kPrimBoolean:
882 RecordArrayElementsInTransactionImpl(array->AsBooleanArray(), count);
883 break;
884 case Primitive::kPrimByte:
885 RecordArrayElementsInTransactionImpl(array->AsByteArray(), count);
886 break;
887 case Primitive::kPrimChar:
888 RecordArrayElementsInTransactionImpl(array->AsCharArray(), count);
889 break;
890 case Primitive::kPrimShort:
891 RecordArrayElementsInTransactionImpl(array->AsShortArray(), count);
892 break;
893 case Primitive::kPrimInt:
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100894 RecordArrayElementsInTransactionImpl(array->AsIntArray(), count);
895 break;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700896 case Primitive::kPrimFloat:
897 RecordArrayElementsInTransactionImpl(array->AsFloatArray(), count);
898 break;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100899 case Primitive::kPrimLong:
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100900 RecordArrayElementsInTransactionImpl(array->AsLongArray(), count);
901 break;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700902 case Primitive::kPrimDouble:
903 RecordArrayElementsInTransactionImpl(array->AsDoubleArray(), count);
904 break;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100905 default:
906 LOG(FATAL) << "Unsupported primitive type " << primitive_component_type
907 << " in fill-array-data";
908 break;
909 }
910}
911
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200912// Explicit DoCall template function declarations.
Sebastien Hertzc6714852013-09-30 16:42:32 +0200913#define EXPLICIT_DO_CALL_TEMPLATE_DECL(_is_range, _do_assignability_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700914 template REQUIRES_SHARED(Locks::mutator_lock_) \
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100915 bool DoCall<_is_range, _do_assignability_check>(ArtMethod* method, Thread* self, \
916 ShadowFrame& shadow_frame, \
Sebastien Hertzc6714852013-09-30 16:42:32 +0200917 const Instruction* inst, uint16_t inst_data, \
918 JValue* result)
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200919EXPLICIT_DO_CALL_TEMPLATE_DECL(false, false);
920EXPLICIT_DO_CALL_TEMPLATE_DECL(false, true);
921EXPLICIT_DO_CALL_TEMPLATE_DECL(true, false);
922EXPLICIT_DO_CALL_TEMPLATE_DECL(true, true);
923#undef EXPLICIT_DO_CALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200924
925// Explicit DoFilledNewArray template function declarations.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100926#define EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(_is_range_, _check, _transaction_active) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700927 template REQUIRES_SHARED(Locks::mutator_lock_) \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100928 bool DoFilledNewArray<_is_range_, _check, _transaction_active>(const Instruction* inst, \
929 const ShadowFrame& shadow_frame, \
930 Thread* self, JValue* result)
931#define EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(_transaction_active) \
932 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, false, _transaction_active); \
933 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, true, _transaction_active); \
934 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, false, _transaction_active); \
935 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, true, _transaction_active)
936EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(false);
937EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(true);
938#undef EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200939#undef EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL
940
941} // namespace interpreter
942} // namespace art