blob: 11b7ef433dbc326b8685db65352a7348bf75f0f2 [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
Igor Murashkin6918bf12015-09-27 19:19:06 -070033// All lambda closures have to be a consecutive pair of virtual registers.
34static constexpr size_t kLambdaVirtualRegisterWidth = 2;
35
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000036void ThrowNullPointerExceptionFromInterpreter() {
37 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -070038}
39
40template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check>
41bool DoFieldGet(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst,
42 uint16_t inst_data) {
43 const bool is_static = (find_type == StaticObjectRead) || (find_type == StaticPrimitiveRead);
44 const uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Roland Levillain4b8f1ec2015-08-26 18:34:03 +010045 ArtField* f =
46 FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
47 Primitive::ComponentSize(field_type));
Ian Rogers54874942014-06-10 16:31:03 -070048 if (UNLIKELY(f == nullptr)) {
49 CHECK(self->IsExceptionPending());
50 return false;
51 }
52 Object* obj;
53 if (is_static) {
54 obj = f->GetDeclaringClass();
55 } else {
56 obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
57 if (UNLIKELY(obj == nullptr)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000058 ThrowNullPointerExceptionForFieldAccess(f, true);
Ian Rogers54874942014-06-10 16:31:03 -070059 return false;
60 }
61 }
Sebastien Hertz1edbd8e2014-07-16 20:00:11 +020062 f->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self);
Ian Rogers54874942014-06-10 16:31:03 -070063 // Report this field access to instrumentation if needed.
64 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
65 if (UNLIKELY(instrumentation->HasFieldReadListeners())) {
66 Object* this_object = f->IsStatic() ? nullptr : obj;
67 instrumentation->FieldReadEvent(self, this_object, shadow_frame.GetMethod(),
68 shadow_frame.GetDexPC(), f);
69 }
70 uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
71 switch (field_type) {
72 case Primitive::kPrimBoolean:
73 shadow_frame.SetVReg(vregA, f->GetBoolean(obj));
74 break;
75 case Primitive::kPrimByte:
76 shadow_frame.SetVReg(vregA, f->GetByte(obj));
77 break;
78 case Primitive::kPrimChar:
79 shadow_frame.SetVReg(vregA, f->GetChar(obj));
80 break;
81 case Primitive::kPrimShort:
82 shadow_frame.SetVReg(vregA, f->GetShort(obj));
83 break;
84 case Primitive::kPrimInt:
85 shadow_frame.SetVReg(vregA, f->GetInt(obj));
86 break;
87 case Primitive::kPrimLong:
88 shadow_frame.SetVRegLong(vregA, f->GetLong(obj));
89 break;
90 case Primitive::kPrimNot:
91 shadow_frame.SetVRegReference(vregA, f->GetObject(obj));
92 break;
93 default:
94 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -070095 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -070096 }
97 return true;
98}
99
100// Explicitly instantiate all DoFieldGet functions.
101#define EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, _do_check) \
102 template bool DoFieldGet<_find_type, _field_type, _do_check>(Thread* self, \
103 ShadowFrame& shadow_frame, \
104 const Instruction* inst, \
105 uint16_t inst_data)
106
107#define EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(_find_type, _field_type) \
108 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, false); \
109 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, true);
110
111// iget-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700112EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimBoolean)
113EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimByte)
114EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimChar)
115EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimShort)
116EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimInt)
117EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimLong)
118EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstanceObjectRead, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700119
120// sget-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700121EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimBoolean)
122EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimByte)
123EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimChar)
124EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimShort)
125EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimInt)
126EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimLong)
127EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticObjectRead, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700128
129#undef EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL
130#undef EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL
131
132// Handles iget-quick, iget-wide-quick and iget-object-quick instructions.
133// Returns true on success, otherwise throws an exception and returns false.
134template<Primitive::Type field_type>
135bool DoIGetQuick(ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
136 Object* obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
137 if (UNLIKELY(obj == nullptr)) {
138 // We lost the reference to the field index so we cannot get a more
139 // precised exception message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000140 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -0700141 return false;
142 }
143 MemberOffset field_offset(inst->VRegC_22c());
144 // Report this field access to instrumentation if needed. Since we only have the offset of
145 // the field from the base of the object, we need to look for it first.
146 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
147 if (UNLIKELY(instrumentation->HasFieldReadListeners())) {
148 ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(),
149 field_offset.Uint32Value());
150 DCHECK(f != nullptr);
151 DCHECK(!f->IsStatic());
152 instrumentation->FieldReadEvent(Thread::Current(), obj, shadow_frame.GetMethod(),
153 shadow_frame.GetDexPC(), f);
154 }
155 // Note: iget-x-quick instructions are only for non-volatile fields.
156 const uint32_t vregA = inst->VRegA_22c(inst_data);
157 switch (field_type) {
158 case Primitive::kPrimInt:
159 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetField32(field_offset)));
160 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800161 case Primitive::kPrimBoolean:
162 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldBoolean(field_offset)));
163 break;
164 case Primitive::kPrimByte:
165 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldByte(field_offset)));
166 break;
167 case Primitive::kPrimChar:
168 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldChar(field_offset)));
169 break;
170 case Primitive::kPrimShort:
171 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldShort(field_offset)));
172 break;
Ian Rogers54874942014-06-10 16:31:03 -0700173 case Primitive::kPrimLong:
174 shadow_frame.SetVRegLong(vregA, static_cast<int64_t>(obj->GetField64(field_offset)));
175 break;
176 case Primitive::kPrimNot:
177 shadow_frame.SetVRegReference(vregA, obj->GetFieldObject<mirror::Object>(field_offset));
178 break;
179 default:
180 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700181 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700182 }
183 return true;
184}
185
186// Explicitly instantiate all DoIGetQuick functions.
187#define EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(_field_type) \
188 template bool DoIGetQuick<_field_type>(ShadowFrame& shadow_frame, const Instruction* inst, \
189 uint16_t inst_data)
190
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800191EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimInt); // iget-quick.
192EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimBoolean); // iget-boolean-quick.
193EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimByte); // iget-byte-quick.
194EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimChar); // iget-char-quick.
195EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimShort); // iget-short-quick.
196EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimLong); // iget-wide-quick.
197EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimNot); // iget-object-quick.
Ian Rogers54874942014-06-10 16:31:03 -0700198#undef EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL
199
200template<Primitive::Type field_type>
201static JValue GetFieldValue(const ShadowFrame& shadow_frame, uint32_t vreg)
Mathieu Chartier90443472015-07-16 20:32:27 -0700202 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers54874942014-06-10 16:31:03 -0700203 JValue field_value;
204 switch (field_type) {
205 case Primitive::kPrimBoolean:
206 field_value.SetZ(static_cast<uint8_t>(shadow_frame.GetVReg(vreg)));
207 break;
208 case Primitive::kPrimByte:
209 field_value.SetB(static_cast<int8_t>(shadow_frame.GetVReg(vreg)));
210 break;
211 case Primitive::kPrimChar:
212 field_value.SetC(static_cast<uint16_t>(shadow_frame.GetVReg(vreg)));
213 break;
214 case Primitive::kPrimShort:
215 field_value.SetS(static_cast<int16_t>(shadow_frame.GetVReg(vreg)));
216 break;
217 case Primitive::kPrimInt:
218 field_value.SetI(shadow_frame.GetVReg(vreg));
219 break;
220 case Primitive::kPrimLong:
221 field_value.SetJ(shadow_frame.GetVRegLong(vreg));
222 break;
223 case Primitive::kPrimNot:
224 field_value.SetL(shadow_frame.GetVRegReference(vreg));
225 break;
226 default:
227 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700228 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700229 }
230 return field_value;
231}
232
233template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check,
234 bool transaction_active>
235bool DoFieldPut(Thread* self, const ShadowFrame& shadow_frame, const Instruction* inst,
236 uint16_t inst_data) {
237 bool do_assignability_check = do_access_check;
238 bool is_static = (find_type == StaticObjectWrite) || (find_type == StaticPrimitiveWrite);
239 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Roland Levillain4b8f1ec2015-08-26 18:34:03 +0100240 ArtField* f =
241 FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
242 Primitive::ComponentSize(field_type));
Ian Rogers54874942014-06-10 16:31:03 -0700243 if (UNLIKELY(f == nullptr)) {
244 CHECK(self->IsExceptionPending());
245 return false;
246 }
247 Object* obj;
248 if (is_static) {
249 obj = f->GetDeclaringClass();
250 } else {
251 obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
252 if (UNLIKELY(obj == nullptr)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000253 ThrowNullPointerExceptionForFieldAccess(f, false);
Ian Rogers54874942014-06-10 16:31:03 -0700254 return false;
255 }
256 }
Sebastien Hertz1edbd8e2014-07-16 20:00:11 +0200257 f->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self);
Ian Rogers54874942014-06-10 16:31:03 -0700258 uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
259 // Report this field access to instrumentation if needed. Since we only have the offset of
260 // the field from the base of the object, we need to look for it first.
261 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
262 if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
263 JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA);
264 Object* this_object = f->IsStatic() ? nullptr : obj;
265 instrumentation->FieldWriteEvent(self, this_object, shadow_frame.GetMethod(),
266 shadow_frame.GetDexPC(), f, field_value);
267 }
268 switch (field_type) {
269 case Primitive::kPrimBoolean:
270 f->SetBoolean<transaction_active>(obj, shadow_frame.GetVReg(vregA));
271 break;
272 case Primitive::kPrimByte:
273 f->SetByte<transaction_active>(obj, shadow_frame.GetVReg(vregA));
274 break;
275 case Primitive::kPrimChar:
276 f->SetChar<transaction_active>(obj, shadow_frame.GetVReg(vregA));
277 break;
278 case Primitive::kPrimShort:
279 f->SetShort<transaction_active>(obj, shadow_frame.GetVReg(vregA));
280 break;
281 case Primitive::kPrimInt:
282 f->SetInt<transaction_active>(obj, shadow_frame.GetVReg(vregA));
283 break;
284 case Primitive::kPrimLong:
285 f->SetLong<transaction_active>(obj, shadow_frame.GetVRegLong(vregA));
286 break;
287 case Primitive::kPrimNot: {
288 Object* reg = shadow_frame.GetVRegReference(vregA);
289 if (do_assignability_check && reg != nullptr) {
290 // FieldHelper::GetType can resolve classes, use a handle wrapper which will restore the
291 // object in the destructor.
292 Class* field_class;
293 {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700294 StackHandleScope<2> hs(self);
Ian Rogers54874942014-06-10 16:31:03 -0700295 HandleWrapper<mirror::Object> h_reg(hs.NewHandleWrapper(&reg));
296 HandleWrapper<mirror::Object> h_obj(hs.NewHandleWrapper(&obj));
Mathieu Chartierc7853442015-03-27 14:35:38 -0700297 field_class = f->GetType<true>();
Ian Rogers54874942014-06-10 16:31:03 -0700298 }
299 if (!reg->VerifierInstanceOf(field_class)) {
300 // This should never happen.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700301 std::string temp1, temp2, temp3;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000302 self->ThrowNewExceptionF("Ljava/lang/VirtualMachineError;",
Ian Rogers54874942014-06-10 16:31:03 -0700303 "Put '%s' that is not instance of field '%s' in '%s'",
Ian Rogers1ff3c982014-08-12 02:30:58 -0700304 reg->GetClass()->GetDescriptor(&temp1),
305 field_class->GetDescriptor(&temp2),
306 f->GetDeclaringClass()->GetDescriptor(&temp3));
Ian Rogers54874942014-06-10 16:31:03 -0700307 return false;
308 }
309 }
310 f->SetObj<transaction_active>(obj, reg);
311 break;
312 }
313 default:
314 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700315 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700316 }
317 return true;
318}
319
320// Explicitly instantiate all DoFieldPut functions.
321#define EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, _do_check, _transaction_active) \
322 template bool DoFieldPut<_find_type, _field_type, _do_check, _transaction_active>(Thread* self, \
323 const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data)
324
325#define EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(_find_type, _field_type) \
326 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, false); \
327 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, false); \
328 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, true); \
329 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, true);
330
331// iput-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700332EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimBoolean)
333EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimByte)
334EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimChar)
335EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimShort)
336EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimInt)
337EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimLong)
338EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstanceObjectWrite, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700339
340// sput-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700341EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimBoolean)
342EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimByte)
343EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimChar)
344EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimShort)
345EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimInt)
346EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimLong)
347EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticObjectWrite, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700348
349#undef EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL
350#undef EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL
351
352template<Primitive::Type field_type, bool transaction_active>
353bool DoIPutQuick(const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
354 Object* obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
355 if (UNLIKELY(obj == nullptr)) {
356 // We lost the reference to the field index so we cannot get a more
357 // precised exception message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000358 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -0700359 return false;
360 }
361 MemberOffset field_offset(inst->VRegC_22c());
362 const uint32_t vregA = inst->VRegA_22c(inst_data);
363 // Report this field modification to instrumentation if needed. Since we only have the offset of
364 // the field from the base of the object, we need to look for it first.
365 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
366 if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
367 ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(),
368 field_offset.Uint32Value());
369 DCHECK(f != nullptr);
370 DCHECK(!f->IsStatic());
371 JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA);
372 instrumentation->FieldWriteEvent(Thread::Current(), obj, shadow_frame.GetMethod(),
373 shadow_frame.GetDexPC(), f, field_value);
374 }
375 // Note: iput-x-quick instructions are only for non-volatile fields.
376 switch (field_type) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700377 case Primitive::kPrimBoolean:
378 obj->SetFieldBoolean<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
379 break;
380 case Primitive::kPrimByte:
381 obj->SetFieldByte<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
382 break;
383 case Primitive::kPrimChar:
384 obj->SetFieldChar<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
385 break;
386 case Primitive::kPrimShort:
387 obj->SetFieldShort<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
388 break;
Ian Rogers54874942014-06-10 16:31:03 -0700389 case Primitive::kPrimInt:
390 obj->SetField32<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
391 break;
392 case Primitive::kPrimLong:
393 obj->SetField64<transaction_active>(field_offset, shadow_frame.GetVRegLong(vregA));
394 break;
395 case Primitive::kPrimNot:
396 obj->SetFieldObject<transaction_active>(field_offset, shadow_frame.GetVRegReference(vregA));
397 break;
398 default:
399 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700400 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700401 }
402 return true;
403}
404
405// Explicitly instantiate all DoIPutQuick functions.
406#define EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, _transaction_active) \
407 template bool DoIPutQuick<_field_type, _transaction_active>(const ShadowFrame& shadow_frame, \
408 const Instruction* inst, \
409 uint16_t inst_data)
410
411#define EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(_field_type) \
412 EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, false); \
413 EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, true);
414
Andreas Gampec8ccf682014-09-29 20:07:43 -0700415EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimInt) // iput-quick.
416EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimBoolean) // iput-boolean-quick.
417EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimByte) // iput-byte-quick.
418EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimChar) // iput-char-quick.
419EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimShort) // iput-short-quick.
420EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimLong) // iput-wide-quick.
421EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimNot) // iput-object-quick.
Ian Rogers54874942014-06-10 16:31:03 -0700422#undef EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL
423#undef EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL
424
Sebastien Hertz520633b2015-09-08 17:03:36 +0200425// We accept a null Instrumentation* meaning we must not report anything to the instrumentation.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700426uint32_t FindNextInstructionFollowingException(
427 Thread* self, ShadowFrame& shadow_frame, uint32_t dex_pc,
428 const instrumentation::Instrumentation* instrumentation) {
Ian Rogers54874942014-06-10 16:31:03 -0700429 self->VerifyStack();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700430 StackHandleScope<2> hs(self);
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000431 Handle<mirror::Throwable> exception(hs.NewHandle(self->GetException()));
Sebastien Hertz520633b2015-09-08 17:03:36 +0200432 if (instrumentation != nullptr && instrumentation->HasExceptionCaughtListeners()
Nicolas Geoffray7642cfc2015-02-26 10:56:09 +0000433 && self->IsExceptionThrownByCurrentMethod(exception.Get())) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000434 instrumentation->ExceptionCaughtEvent(self, exception.Get());
Sebastien Hertz9f102032014-05-23 08:59:42 +0200435 }
Ian Rogers54874942014-06-10 16:31:03 -0700436 bool clear_exception = false;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700437 uint32_t found_dex_pc = shadow_frame.GetMethod()->FindCatchBlock(
438 hs.NewHandle(exception->GetClass()), dex_pc, &clear_exception);
Sebastien Hertz520633b2015-09-08 17:03:36 +0200439 if (found_dex_pc == DexFile::kDexNoIndex && instrumentation != nullptr) {
Nicolas Geoffray7642cfc2015-02-26 10:56:09 +0000440 // Exception is not caught by the current method. We will unwind to the
441 // caller. Notify any instrumentation listener.
Sebastien Hertz9f102032014-05-23 08:59:42 +0200442 instrumentation->MethodUnwindEvent(self, shadow_frame.GetThisObject(),
Ian Rogers54874942014-06-10 16:31:03 -0700443 shadow_frame.GetMethod(), dex_pc);
444 } else {
Nicolas Geoffray7642cfc2015-02-26 10:56:09 +0000445 // Exception is caught in the current method. We will jump to the found_dex_pc.
Ian Rogers54874942014-06-10 16:31:03 -0700446 if (clear_exception) {
447 self->ClearException();
448 }
449 }
450 return found_dex_pc;
451}
452
Ian Rogerse94652f2014-12-02 11:13:19 -0800453void UnexpectedOpcode(const Instruction* inst, const ShadowFrame& shadow_frame) {
454 LOG(FATAL) << "Unexpected instruction: "
455 << inst->DumpString(shadow_frame.GetMethod()->GetDexFile());
456 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700457}
458
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200459// Assign register 'src_reg' from shadow_frame to register 'dest_reg' into new_shadow_frame.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800460static inline void AssignRegister(ShadowFrame* new_shadow_frame, const ShadowFrame& shadow_frame,
461 size_t dest_reg, size_t src_reg)
Mathieu Chartier90443472015-07-16 20:32:27 -0700462 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe7104cbf2014-03-21 11:44:43 -0700463 // Uint required, so that sign extension does not make this wrong on 64b systems
464 uint32_t src_value = shadow_frame.GetVReg(src_reg);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800465 mirror::Object* o = shadow_frame.GetVRegReference<kVerifyNone>(src_reg);
Igor Murashkinc449e8b2015-06-10 15:56:42 -0700466
467 // If both register locations contains the same value, the register probably holds a reference.
468 // Note: As an optimization, non-moving collectors leave a stale reference value
469 // in the references array even after the original vreg was overwritten to a non-reference.
Andreas Gampe7104cbf2014-03-21 11:44:43 -0700470 if (src_value == reinterpret_cast<uintptr_t>(o)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800471 new_shadow_frame->SetVRegReference(dest_reg, o);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200472 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800473 new_shadow_frame->SetVReg(dest_reg, src_value);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200474 }
475}
476
Sebastien Hertz45b15972015-04-03 16:07:05 +0200477void AbortTransactionF(Thread* self, const char* fmt, ...) {
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700478 va_list args;
479 va_start(args, fmt);
Sebastien Hertz45b15972015-04-03 16:07:05 +0200480 AbortTransactionV(self, fmt, args);
481 va_end(args);
482}
483
484void AbortTransactionV(Thread* self, const char* fmt, va_list args) {
485 CHECK(Runtime::Current()->IsActiveTransaction());
486 // Constructs abort message.
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100487 std::string abort_msg;
488 StringAppendV(&abort_msg, fmt, args);
489 // Throws an exception so we can abort the transaction and rollback every change.
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200490 Runtime::Current()->AbortTransactionAndThrowAbortError(self, abort_msg);
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700491}
492
Igor Murashkin158f35c2015-06-10 15:55:30 -0700493// Separate declaration is required solely for the attributes.
Igor Murashkin6918bf12015-09-27 19:19:06 -0700494template <bool is_range,
495 bool do_assignability_check,
496 size_t kVarArgMax>
497 SHARED_REQUIRES(Locks::mutator_lock_)
Igor Murashkin158f35c2015-06-10 15:55:30 -0700498static inline bool DoCallCommon(ArtMethod* called_method,
499 Thread* self,
500 ShadowFrame& shadow_frame,
501 JValue* result,
502 uint16_t number_of_inputs,
Igor Murashkin6918bf12015-09-27 19:19:06 -0700503 uint32_t (&arg)[kVarArgMax],
Igor Murashkin158f35c2015-06-10 15:55:30 -0700504 uint32_t vregC) ALWAYS_INLINE;
505
Siva Chandra05d24152016-01-05 17:43:17 -0800506void ArtInterpreterToCompiledCodeBridge(Thread* self,
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100507 ArtMethod* caller,
Siva Chandra05d24152016-01-05 17:43:17 -0800508 const DexFile::CodeItem* code_item,
509 ShadowFrame* shadow_frame,
510 JValue* result)
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700511 SHARED_REQUIRES(Locks::mutator_lock_) {
512 ArtMethod* method = shadow_frame->GetMethod();
513 // Ensure static methods are initialized.
514 if (method->IsStatic()) {
515 mirror::Class* declaringClass = method->GetDeclaringClass();
516 if (UNLIKELY(!declaringClass->IsInitialized())) {
517 self->PushShadowFrame(shadow_frame);
518 StackHandleScope<1> hs(self);
519 Handle<mirror::Class> h_class(hs.NewHandle(declaringClass));
520 if (UNLIKELY(!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true,
521 true))) {
522 self->PopShadowFrame();
523 DCHECK(self->IsExceptionPending());
524 return;
525 }
526 self->PopShadowFrame();
527 CHECK(h_class->IsInitializing());
528 // Reload from shadow frame in case the method moved, this is faster than adding a handle.
529 method = shadow_frame->GetMethod();
530 }
531 }
532 uint16_t arg_offset = (code_item == nullptr)
533 ? 0
534 : code_item->registers_size_ - code_item->ins_size_;
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100535 jit::Jit* jit = Runtime::Current()->GetJit();
536 if (jit != nullptr && caller != nullptr) {
537 jit->NotifyInterpreterToCompiledCodeTransition(self, caller);
538 }
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700539 method->Invoke(self, shadow_frame->GetVRegArgs(arg_offset),
540 (shadow_frame->NumberOfVRegs() - arg_offset) * sizeof(uint32_t),
Andreas Gampe542451c2016-07-26 09:02:02 -0700541 result, method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty());
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700542}
543
Mingyao Yangffedec52016-05-19 10:48:40 -0700544void SetStringInitValueToAllAliases(ShadowFrame* shadow_frame,
545 uint16_t this_obj_vreg,
546 JValue result)
547 SHARED_REQUIRES(Locks::mutator_lock_) {
548 Object* existing = shadow_frame->GetVRegReference(this_obj_vreg);
549 if (existing == nullptr) {
550 // If it's null, we come from compiled code that was deoptimized. Nothing to do,
551 // as the compiler verified there was no alias.
552 // Set the new string result of the StringFactory.
553 shadow_frame->SetVRegReference(this_obj_vreg, result.GetL());
554 return;
555 }
556 // Set the string init result into all aliases.
557 for (uint32_t i = 0, e = shadow_frame->NumberOfVRegs(); i < e; ++i) {
558 if (shadow_frame->GetVRegReference(i) == existing) {
559 DCHECK_EQ(shadow_frame->GetVRegReference(i),
560 reinterpret_cast<mirror::Object*>(shadow_frame->GetVReg(i)));
561 shadow_frame->SetVRegReference(i, result.GetL());
562 DCHECK_EQ(shadow_frame->GetVRegReference(i),
563 reinterpret_cast<mirror::Object*>(shadow_frame->GetVReg(i)));
564 }
565 }
566}
567
Igor Murashkin6918bf12015-09-27 19:19:06 -0700568template <bool is_range,
569 bool do_assignability_check,
570 size_t kVarArgMax>
Igor Murashkin158f35c2015-06-10 15:55:30 -0700571static inline bool DoCallCommon(ArtMethod* called_method,
572 Thread* self,
573 ShadowFrame& shadow_frame,
574 JValue* result,
575 uint16_t number_of_inputs,
Igor Murashkin6918bf12015-09-27 19:19:06 -0700576 uint32_t (&arg)[kVarArgMax],
Igor Murashkin158f35c2015-06-10 15:55:30 -0700577 uint32_t vregC) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800578 bool string_init = false;
579 // Replace calls to String.<init> with equivalent StringFactory call.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700580 if (UNLIKELY(called_method->GetDeclaringClass()->IsStringClass()
581 && called_method->IsConstructor())) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800582 ScopedObjectAccessUnchecked soa(self);
583 jmethodID mid = soa.EncodeMethod(called_method);
584 called_method = soa.DecodeMethod(WellKnownClasses::StringInitToStringFactoryMethodID(mid));
585 string_init = true;
586 }
587
Alex Lightdaf58c82016-03-16 23:00:49 +0000588 // Compute method information.
589 const DexFile::CodeItem* code_item = called_method->GetCodeItem();
Igor Murashkin158f35c2015-06-10 15:55:30 -0700590
591 // Number of registers for the callee's call frame.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200592 uint16_t num_regs;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700593 if (LIKELY(code_item != nullptr)) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200594 num_regs = code_item->registers_size_;
Igor Murashkin158f35c2015-06-10 15:55:30 -0700595 DCHECK_EQ(string_init ? number_of_inputs - 1 : number_of_inputs, code_item->ins_size_);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200596 } else {
Ian Rogerse94652f2014-12-02 11:13:19 -0800597 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
Igor Murashkin158f35c2015-06-10 15:55:30 -0700598 num_regs = number_of_inputs;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200599 }
600
Igor Murashkin158f35c2015-06-10 15:55:30 -0700601 // Hack for String init:
602 //
603 // Rewrite invoke-x java.lang.String.<init>(this, a, b, c, ...) into:
604 // invoke-x StringFactory(a, b, c, ...)
605 // by effectively dropping the first virtual register from the invoke.
606 //
607 // (at this point the ArtMethod has already been replaced,
608 // so we just need to fix-up the arguments)
David Brazdil65902e82016-01-15 09:35:13 +0000609 //
610 // Note that FindMethodFromCode in entrypoint_utils-inl.h was also special-cased
611 // to handle the compiler optimization of replacing `this` with null without
612 // throwing NullPointerException.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700613 uint32_t string_init_vreg_this = is_range ? vregC : arg[0];
Igor Murashkina06b49b2015-06-25 15:18:12 -0700614 if (UNLIKELY(string_init)) {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700615 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 -0700616
Igor Murashkin158f35c2015-06-10 15:55:30 -0700617 // The new StringFactory call is static and has one fewer argument.
Igor Murashkina06b49b2015-06-25 15:18:12 -0700618 if (code_item == nullptr) {
619 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
620 num_regs--;
621 } // 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 -0700622 number_of_inputs--;
623
624 // Rewrite the var-args, dropping the 0th argument ("this")
Igor Murashkin6918bf12015-09-27 19:19:06 -0700625 for (uint32_t i = 1; i < arraysize(arg); ++i) {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700626 arg[i - 1] = arg[i];
627 }
Igor Murashkin6918bf12015-09-27 19:19:06 -0700628 arg[arraysize(arg) - 1] = 0;
Igor Murashkin158f35c2015-06-10 15:55:30 -0700629
630 // Rewrite the non-var-arg case
631 vregC++; // Skips the 0th vreg in the range ("this").
632 }
633
634 // Parameter registers go at the end of the shadow frame.
635 DCHECK_GE(num_regs, number_of_inputs);
636 size_t first_dest_reg = num_regs - number_of_inputs;
637 DCHECK_NE(first_dest_reg, (size_t)-1);
638
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200639 // Allocate shadow frame on the stack.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700640 const char* old_cause = self->StartAssertNoThreadSuspension("DoCallCommon");
Andreas Gampeb3025922015-09-01 14:45:00 -0700641 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
Andreas Gampe03ec9302015-08-27 17:41:47 -0700642 CREATE_SHADOW_FRAME(num_regs, &shadow_frame, called_method, /* dex pc */ 0);
Andreas Gampeb3025922015-09-01 14:45:00 -0700643 ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get();
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200644
Igor Murashkin158f35c2015-06-10 15:55:30 -0700645 // Initialize new shadow frame by copying the registers from the callee shadow frame.
Jeff Haoa3faaf42013-09-03 19:07:00 -0700646 if (do_assignability_check) {
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -0700647 // Slow path.
648 // We might need to do class loading, which incurs a thread state change to kNative. So
649 // register the shadow frame as under construction and allow suspension again.
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700650 ScopedStackedShadowFramePusher pusher(
Sebastien Hertzf7958692015-06-09 14:09:14 +0200651 self, new_shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -0700652 self->EndAssertNoThreadSuspension(old_cause);
653
Igor Murashkin9f95ba72016-02-01 14:21:25 -0800654 // ArtMethod here is needed to check type information of the call site against the callee.
655 // Type information is retrieved from a DexFile/DexCache for that respective declared method.
656 //
657 // As a special case for proxy methods, which are not dex-backed,
658 // we have to retrieve type information from the proxy's method
659 // interface method instead (which is dex backed since proxies are never interfaces).
Andreas Gampe542451c2016-07-26 09:02:02 -0700660 ArtMethod* method =
661 new_shadow_frame->GetMethod()->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Igor Murashkin9f95ba72016-02-01 14:21:25 -0800662
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -0700663 // We need to do runtime check on reference assignment. We need to load the shorty
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200664 // to get the exact type of each reference argument.
Igor Murashkin9f95ba72016-02-01 14:21:25 -0800665 const DexFile::TypeList* params = method->GetParameterTypeList();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700666 uint32_t shorty_len = 0;
Igor Murashkin9f95ba72016-02-01 14:21:25 -0800667 const char* shorty = method->GetShorty(&shorty_len);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200668
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100669 // Handle receiver apart since it's not part of the shorty.
670 size_t dest_reg = first_dest_reg;
671 size_t arg_offset = 0;
Igor Murashkin158f35c2015-06-10 15:55:30 -0700672
Igor Murashkin9f95ba72016-02-01 14:21:25 -0800673 if (!method->IsStatic()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700674 size_t receiver_reg = is_range ? vregC : arg[0];
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100675 new_shadow_frame->SetVRegReference(dest_reg, shadow_frame.GetVRegReference(receiver_reg));
676 ++dest_reg;
677 ++arg_offset;
Igor Murashkina06b49b2015-06-25 15:18:12 -0700678 DCHECK(!string_init); // All StringFactory methods are static.
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100679 }
Igor Murashkin158f35c2015-06-10 15:55:30 -0700680
681 // Copy the caller's invoke-* arguments into the callee's parameter registers.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800682 for (uint32_t shorty_pos = 0; dest_reg < num_regs; ++shorty_pos, ++dest_reg, ++arg_offset) {
Igor Murashkina06b49b2015-06-25 15:18:12 -0700683 // Skip the 0th 'shorty' type since it represents the return type.
684 DCHECK_LT(shorty_pos + 1, shorty_len) << "for shorty '" << shorty << "'";
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200685 const size_t src_reg = (is_range) ? vregC + arg_offset : arg[arg_offset];
686 switch (shorty[shorty_pos + 1]) {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700687 // Handle Object references. 1 virtual register slot.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200688 case 'L': {
689 Object* o = shadow_frame.GetVRegReference(src_reg);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700690 if (do_assignability_check && o != nullptr) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700691 PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Ian Rogersa0485602014-12-02 15:48:04 -0800692 Class* arg_type =
Igor Murashkin9f95ba72016-02-01 14:21:25 -0800693 method->GetClassFromTypeIndex(
Vladimir Marko05792b92015-08-03 11:56:49 +0100694 params->GetTypeItem(shorty_pos).type_idx_, true /* resolve */, pointer_size);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700695 if (arg_type == nullptr) {
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200696 CHECK(self->IsExceptionPending());
697 return false;
698 }
699 if (!o->VerifierInstanceOf(arg_type)) {
700 // This should never happen.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700701 std::string temp1, temp2;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000702 self->ThrowNewExceptionF("Ljava/lang/VirtualMachineError;",
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200703 "Invoking %s with bad arg %d, type '%s' not instance of '%s'",
Ian Rogerse94652f2014-12-02 11:13:19 -0800704 new_shadow_frame->GetMethod()->GetName(), shorty_pos,
Ian Rogers1ff3c982014-08-12 02:30:58 -0700705 o->GetClass()->GetDescriptor(&temp1),
706 arg_type->GetDescriptor(&temp2));
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200707 return false;
708 }
Jeff Haoa3faaf42013-09-03 19:07:00 -0700709 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200710 new_shadow_frame->SetVRegReference(dest_reg, o);
711 break;
Jeff Haoa3faaf42013-09-03 19:07:00 -0700712 }
Igor Murashkin158f35c2015-06-10 15:55:30 -0700713 // Handle doubles and longs. 2 consecutive virtual register slots.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200714 case 'J': case 'D': {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700715 uint64_t wide_value =
716 (static_cast<uint64_t>(shadow_frame.GetVReg(src_reg + 1)) << BitSizeOf<uint32_t>()) |
717 static_cast<uint32_t>(shadow_frame.GetVReg(src_reg));
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200718 new_shadow_frame->SetVRegLong(dest_reg, wide_value);
Igor Murashkin158f35c2015-06-10 15:55:30 -0700719 // Skip the next virtual register slot since we already used it.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200720 ++dest_reg;
721 ++arg_offset;
722 break;
723 }
Igor Murashkin158f35c2015-06-10 15:55:30 -0700724 // Handle all other primitives that are always 1 virtual register slot.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200725 default:
726 new_shadow_frame->SetVReg(dest_reg, shadow_frame.GetVReg(src_reg));
727 break;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200728 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200729 }
730 } else {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700731 size_t arg_index = 0;
732
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200733 // Fast path: no extra checks.
734 if (is_range) {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700735 // TODO: Implement the range version of invoke-lambda
736 uint16_t first_src_reg = vregC;
737
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200738 for (size_t src_reg = first_src_reg, dest_reg = first_dest_reg; dest_reg < num_regs;
739 ++dest_reg, ++src_reg) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800740 AssignRegister(new_shadow_frame, shadow_frame, dest_reg, src_reg);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200741 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200742 } else {
Igor Murashkin6918bf12015-09-27 19:19:06 -0700743 DCHECK_LE(number_of_inputs, arraysize(arg));
Igor Murashkin158f35c2015-06-10 15:55:30 -0700744
745 for (; arg_index < number_of_inputs; ++arg_index) {
746 AssignRegister(new_shadow_frame, shadow_frame, first_dest_reg + arg_index, arg[arg_index]);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200747 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200748 }
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -0700749 self->EndAssertNoThreadSuspension(old_cause);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200750 }
751
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200752 // Do the call now.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200753 if (LIKELY(Runtime::Current()->IsStarted())) {
Tamas Berghammerdd5e5e92016-02-12 16:29:00 +0000754 ArtMethod* target = new_shadow_frame->GetMethod();
755 if (ClassLinker::ShouldUseInterpreterEntrypoint(
756 target,
757 target->GetEntryPointFromQuickCompiledCode())) {
Tamas Berghammerc94a61f2016-02-05 18:09:08 +0000758 ArtInterpreterToInterpreterBridge(self, code_item, new_shadow_frame, result);
Tamas Berghammer3a98aae2016-02-08 20:21:54 +0000759 } else {
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100760 ArtInterpreterToCompiledCodeBridge(
761 self, shadow_frame.GetMethod(), code_item, new_shadow_frame, result);
Nicolas Geoffray7070ccd2015-07-08 09:41:54 +0000762 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200763 } else {
Andreas Gampe799681b2015-05-15 19:24:12 -0700764 UnstartedRuntime::Invoke(self, code_item, new_shadow_frame, result, first_dest_reg);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200765 }
Jeff Hao848f70a2014-01-15 13:49:50 -0800766
767 if (string_init && !self->IsExceptionPending()) {
Mingyao Yangffedec52016-05-19 10:48:40 -0700768 SetStringInitValueToAllAliases(&shadow_frame, string_init_vreg_this, *result);
Jeff Hao848f70a2014-01-15 13:49:50 -0800769 }
770
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200771 return !self->IsExceptionPending();
772}
773
Igor Murashkin158f35c2015-06-10 15:55:30 -0700774template<bool is_range, bool do_assignability_check>
775bool DoLambdaCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame,
Roland Levillain4b8f1ec2015-08-26 18:34:03 +0100776 const Instruction* inst, uint16_t inst_data ATTRIBUTE_UNUSED, JValue* result) {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700777 const uint4_t num_additional_registers = inst->VRegB_25x();
778 // Argument word count.
Igor Murashkin6918bf12015-09-27 19:19:06 -0700779 const uint16_t number_of_inputs = num_additional_registers + kLambdaVirtualRegisterWidth;
780 // The lambda closure register is always present and is not encoded in the count.
781 // Furthermore, the lambda closure register is always wide, so it counts as 2 inputs.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700782
783 // TODO: find a cleaner way to separate non-range and range information without duplicating
784 // code.
Igor Murashkin6918bf12015-09-27 19:19:06 -0700785 uint32_t arg[Instruction::kMaxVarArgRegs25x]; // only used in invoke-XXX.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700786 uint32_t vregC = 0; // only used in invoke-XXX-range.
787 if (is_range) {
788 vregC = inst->VRegC_3rc();
789 } else {
790 // 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 -0700791 inst->GetAllArgs25x(arg);
792 }
793
794 // TODO: if there's an assignability check, throw instead?
795 DCHECK(called_method->IsStatic());
796
797 return DoCallCommon<is_range, do_assignability_check>(
798 called_method, self, shadow_frame,
799 result, number_of_inputs, arg, vregC);
800}
801
802template<bool is_range, bool do_assignability_check>
803bool DoCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame,
804 const Instruction* inst, uint16_t inst_data, JValue* result) {
805 // Argument word count.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +0100806 const uint16_t number_of_inputs =
807 (is_range) ? inst->VRegA_3rc(inst_data) : inst->VRegA_35c(inst_data);
Igor Murashkin158f35c2015-06-10 15:55:30 -0700808
809 // TODO: find a cleaner way to separate non-range and range information without duplicating
810 // code.
Igor Murashkin6918bf12015-09-27 19:19:06 -0700811 uint32_t arg[Instruction::kMaxVarArgRegs] = {}; // only used in invoke-XXX.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700812 uint32_t vregC = 0;
813 if (is_range) {
814 vregC = inst->VRegC_3rc();
815 } else {
816 vregC = inst->VRegC_35c();
817 inst->GetVarArgs(arg, inst_data);
818 }
819
820 return DoCallCommon<is_range, do_assignability_check>(
821 called_method, self, shadow_frame,
822 result, number_of_inputs, arg, vregC);
823}
824
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100825template <bool is_range, bool do_access_check, bool transaction_active>
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200826bool DoFilledNewArray(const Instruction* inst, const ShadowFrame& shadow_frame,
827 Thread* self, JValue* result) {
828 DCHECK(inst->Opcode() == Instruction::FILLED_NEW_ARRAY ||
829 inst->Opcode() == Instruction::FILLED_NEW_ARRAY_RANGE);
830 const int32_t length = is_range ? inst->VRegA_3rc() : inst->VRegA_35c();
831 if (!is_range) {
832 // Checks FILLED_NEW_ARRAY's length does not exceed 5 arguments.
833 CHECK_LE(length, 5);
834 }
835 if (UNLIKELY(length < 0)) {
836 ThrowNegativeArraySizeException(length);
837 return false;
838 }
839 uint16_t type_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700840 Class* array_class = ResolveVerifyAndClinit(type_idx, shadow_frame.GetMethod(),
841 self, false, do_access_check);
842 if (UNLIKELY(array_class == nullptr)) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200843 DCHECK(self->IsExceptionPending());
844 return false;
845 }
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700846 CHECK(array_class->IsArrayClass());
847 Class* component_class = array_class->GetComponentType();
848 const bool is_primitive_int_component = component_class->IsPrimitiveInt();
849 if (UNLIKELY(component_class->IsPrimitive() && !is_primitive_int_component)) {
850 if (component_class->IsPrimitiveLong() || component_class->IsPrimitiveDouble()) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200851 ThrowRuntimeException("Bad filled array request for type %s",
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700852 PrettyDescriptor(component_class).c_str());
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200853 } else {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000854 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
Brian Carlstrom4fa0bcd2013-12-10 11:24:21 -0800855 "Found type %s; filled-new-array not implemented for anything but 'int'",
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700856 PrettyDescriptor(component_class).c_str());
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200857 }
858 return false;
859 }
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700860 Object* new_array = Array::Alloc<true>(self, array_class, length,
861 array_class->GetComponentSizeShift(),
862 Runtime::Current()->GetHeap()->GetCurrentAllocator());
863 if (UNLIKELY(new_array == nullptr)) {
864 self->AssertPendingOOMException();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200865 return false;
866 }
Igor Murashkin158f35c2015-06-10 15:55:30 -0700867 uint32_t arg[Instruction::kMaxVarArgRegs]; // only used in filled-new-array.
868 uint32_t vregC = 0; // only used in filled-new-array-range.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200869 if (is_range) {
Sebastien Hertzabff6432014-01-27 18:01:39 +0100870 vregC = inst->VRegC_3rc();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200871 } else {
Ian Rogers29a26482014-05-02 15:27:29 -0700872 inst->GetVarArgs(arg);
Sebastien Hertzabff6432014-01-27 18:01:39 +0100873 }
Sebastien Hertzabff6432014-01-27 18:01:39 +0100874 for (int32_t i = 0; i < length; ++i) {
875 size_t src_reg = is_range ? vregC + i : arg[i];
876 if (is_primitive_int_component) {
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700877 new_array->AsIntArray()->SetWithoutChecks<transaction_active>(
878 i, shadow_frame.GetVReg(src_reg));
Sebastien Hertzabff6432014-01-27 18:01:39 +0100879 } else {
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700880 new_array->AsObjectArray<Object>()->SetWithoutChecks<transaction_active>(
881 i, shadow_frame.GetVRegReference(src_reg));
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200882 }
883 }
884
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700885 result->SetL(new_array);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200886 return true;
887}
888
Mathieu Chartier90443472015-07-16 20:32:27 -0700889// TODO fix thread analysis: should be SHARED_REQUIRES(Locks::mutator_lock_).
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100890template<typename T>
891static void RecordArrayElementsInTransactionImpl(mirror::PrimitiveArray<T>* array, int32_t count)
892 NO_THREAD_SAFETY_ANALYSIS {
893 Runtime* runtime = Runtime::Current();
894 for (int32_t i = 0; i < count; ++i) {
895 runtime->RecordWriteArray(array, i, array->GetWithoutChecks(i));
896 }
897}
898
899void RecordArrayElementsInTransaction(mirror::Array* array, int32_t count)
Mathieu Chartier90443472015-07-16 20:32:27 -0700900 SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100901 DCHECK(Runtime::Current()->IsActiveTransaction());
902 DCHECK(array != nullptr);
903 DCHECK_LE(count, array->GetLength());
904 Primitive::Type primitive_component_type = array->GetClass()->GetComponentType()->GetPrimitiveType();
905 switch (primitive_component_type) {
906 case Primitive::kPrimBoolean:
907 RecordArrayElementsInTransactionImpl(array->AsBooleanArray(), count);
908 break;
909 case Primitive::kPrimByte:
910 RecordArrayElementsInTransactionImpl(array->AsByteArray(), count);
911 break;
912 case Primitive::kPrimChar:
913 RecordArrayElementsInTransactionImpl(array->AsCharArray(), count);
914 break;
915 case Primitive::kPrimShort:
916 RecordArrayElementsInTransactionImpl(array->AsShortArray(), count);
917 break;
918 case Primitive::kPrimInt:
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100919 RecordArrayElementsInTransactionImpl(array->AsIntArray(), count);
920 break;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700921 case Primitive::kPrimFloat:
922 RecordArrayElementsInTransactionImpl(array->AsFloatArray(), count);
923 break;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100924 case Primitive::kPrimLong:
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100925 RecordArrayElementsInTransactionImpl(array->AsLongArray(), count);
926 break;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700927 case Primitive::kPrimDouble:
928 RecordArrayElementsInTransactionImpl(array->AsDoubleArray(), count);
929 break;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100930 default:
931 LOG(FATAL) << "Unsupported primitive type " << primitive_component_type
932 << " in fill-array-data";
933 break;
934 }
935}
936
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200937// Explicit DoCall template function declarations.
Sebastien Hertzc6714852013-09-30 16:42:32 +0200938#define EXPLICIT_DO_CALL_TEMPLATE_DECL(_is_range, _do_assignability_check) \
Mathieu Chartier90ef3db2015-08-04 15:19:41 -0700939 template SHARED_REQUIRES(Locks::mutator_lock_) \
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100940 bool DoCall<_is_range, _do_assignability_check>(ArtMethod* method, Thread* self, \
941 ShadowFrame& shadow_frame, \
Sebastien Hertzc6714852013-09-30 16:42:32 +0200942 const Instruction* inst, uint16_t inst_data, \
943 JValue* result)
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200944EXPLICIT_DO_CALL_TEMPLATE_DECL(false, false);
945EXPLICIT_DO_CALL_TEMPLATE_DECL(false, true);
946EXPLICIT_DO_CALL_TEMPLATE_DECL(true, false);
947EXPLICIT_DO_CALL_TEMPLATE_DECL(true, true);
948#undef EXPLICIT_DO_CALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200949
Igor Murashkin158f35c2015-06-10 15:55:30 -0700950// Explicit DoLambdaCall template function declarations.
951#define EXPLICIT_DO_LAMBDA_CALL_TEMPLATE_DECL(_is_range, _do_assignability_check) \
Mathieu Chartier90ef3db2015-08-04 15:19:41 -0700952 template SHARED_REQUIRES(Locks::mutator_lock_) \
Igor Murashkin158f35c2015-06-10 15:55:30 -0700953 bool DoLambdaCall<_is_range, _do_assignability_check>(ArtMethod* method, Thread* self, \
954 ShadowFrame& shadow_frame, \
955 const Instruction* inst, \
956 uint16_t inst_data, \
957 JValue* result)
958EXPLICIT_DO_LAMBDA_CALL_TEMPLATE_DECL(false, false);
959EXPLICIT_DO_LAMBDA_CALL_TEMPLATE_DECL(false, true);
960EXPLICIT_DO_LAMBDA_CALL_TEMPLATE_DECL(true, false);
961EXPLICIT_DO_LAMBDA_CALL_TEMPLATE_DECL(true, true);
962#undef EXPLICIT_DO_LAMBDA_CALL_TEMPLATE_DECL
963
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200964// Explicit DoFilledNewArray template function declarations.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100965#define EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(_is_range_, _check, _transaction_active) \
Mathieu Chartier90ef3db2015-08-04 15:19:41 -0700966 template SHARED_REQUIRES(Locks::mutator_lock_) \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100967 bool DoFilledNewArray<_is_range_, _check, _transaction_active>(const Instruction* inst, \
968 const ShadowFrame& shadow_frame, \
969 Thread* self, JValue* result)
970#define EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(_transaction_active) \
971 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, false, _transaction_active); \
972 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, true, _transaction_active); \
973 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, false, _transaction_active); \
974 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, true, _transaction_active)
975EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(false);
976EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(true);
977#undef EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200978#undef EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL
979
980} // namespace interpreter
981} // namespace art