blob: 73fc410288df9340733e5e7e12691fe513641c28 [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"
Narayan Kamath9823e782016-08-03 12:46:58 +010025#include "jvalue.h"
26#include "method_handles.h"
Narayan Kamath208f8572016-08-03 12:46:58 +010027#include "method_handles-inl.h"
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010028#include "mirror/array-inl.h"
Narayan Kamath9823e782016-08-03 12:46:58 +010029#include "mirror/class.h"
30#include "mirror/method_handle_impl.h"
31#include "reflection.h"
32#include "reflection-inl.h"
Andreas Gampeb3025922015-09-01 14:45:00 -070033#include "stack.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070034#include "unstarted_runtime.h"
Jeff Hao848f70a2014-01-15 13:49:50 -080035#include "verifier/method_verifier.h"
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +010036#include "well_known_classes.h"
Sebastien Hertz8ece0502013-08-07 11:26:41 +020037
38namespace art {
39namespace interpreter {
40
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000041void ThrowNullPointerExceptionFromInterpreter() {
42 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -070043}
44
Orion Hodson3d617ac2016-10-19 14:00:46 +010045template<Primitive::Type field_type>
46static ALWAYS_INLINE void DoFieldGetCommon(Thread* self,
47 const ShadowFrame& shadow_frame,
48 ObjPtr<mirror::Object>& obj,
49 ArtField* field,
50 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
51 field->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self);
52
53 // Report this field access to instrumentation if needed.
54 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
55 if (UNLIKELY(instrumentation->HasFieldReadListeners())) {
56 StackHandleScope<1> hs(self);
57 // Wrap in handle wrapper in case the listener does thread suspension.
58 HandleWrapperObjPtr<mirror::Object> h(hs.NewHandleWrapper(&obj));
59 ObjPtr<mirror::Object> this_object;
60 if (!field->IsStatic()) {
61 this_object = obj;
62 }
63 instrumentation->FieldReadEvent(self,
64 this_object.Ptr(),
65 shadow_frame.GetMethod(),
66 shadow_frame.GetDexPC(),
67 field);
68 }
69
70 switch (field_type) {
71 case Primitive::kPrimBoolean:
72 result->SetZ(field->GetBoolean(obj));
73 break;
74 case Primitive::kPrimByte:
75 result->SetB(field->GetByte(obj));
76 break;
77 case Primitive::kPrimChar:
78 result->SetC(field->GetChar(obj));
79 break;
80 case Primitive::kPrimShort:
81 result->SetS(field->GetShort(obj));
82 break;
83 case Primitive::kPrimInt:
84 result->SetI(field->GetInt(obj));
85 break;
86 case Primitive::kPrimLong:
87 result->SetJ(field->GetLong(obj));
88 break;
89 case Primitive::kPrimNot:
90 result->SetL(field->GetObject(obj));
91 break;
92 default:
93 LOG(FATAL) << "Unreachable: " << field_type;
94 UNREACHABLE();
95 }
96}
97
Ian Rogers54874942014-06-10 16:31:03 -070098template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check>
99bool DoFieldGet(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst,
100 uint16_t inst_data) {
101 const bool is_static = (find_type == StaticObjectRead) || (find_type == StaticPrimitiveRead);
102 const uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Roland Levillain4b8f1ec2015-08-26 18:34:03 +0100103 ArtField* f =
104 FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
105 Primitive::ComponentSize(field_type));
Ian Rogers54874942014-06-10 16:31:03 -0700106 if (UNLIKELY(f == nullptr)) {
107 CHECK(self->IsExceptionPending());
108 return false;
109 }
Mathieu Chartieref41db72016-10-25 15:08:01 -0700110 ObjPtr<mirror::Object> obj;
Ian Rogers54874942014-06-10 16:31:03 -0700111 if (is_static) {
112 obj = f->GetDeclaringClass();
113 } else {
114 obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
115 if (UNLIKELY(obj == nullptr)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000116 ThrowNullPointerExceptionForFieldAccess(f, true);
Ian Rogers54874942014-06-10 16:31:03 -0700117 return false;
118 }
119 }
Orion Hodson3d617ac2016-10-19 14:00:46 +0100120
121 JValue result;
122 DoFieldGetCommon<field_type>(self, shadow_frame, obj, f, &result);
Ian Rogers54874942014-06-10 16:31:03 -0700123 uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
124 switch (field_type) {
125 case Primitive::kPrimBoolean:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100126 shadow_frame.SetVReg(vregA, result.GetZ());
Ian Rogers54874942014-06-10 16:31:03 -0700127 break;
128 case Primitive::kPrimByte:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100129 shadow_frame.SetVReg(vregA, result.GetB());
Ian Rogers54874942014-06-10 16:31:03 -0700130 break;
131 case Primitive::kPrimChar:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100132 shadow_frame.SetVReg(vregA, result.GetC());
Ian Rogers54874942014-06-10 16:31:03 -0700133 break;
134 case Primitive::kPrimShort:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100135 shadow_frame.SetVReg(vregA, result.GetS());
Ian Rogers54874942014-06-10 16:31:03 -0700136 break;
137 case Primitive::kPrimInt:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100138 shadow_frame.SetVReg(vregA, result.GetI());
Ian Rogers54874942014-06-10 16:31:03 -0700139 break;
140 case Primitive::kPrimLong:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100141 shadow_frame.SetVRegLong(vregA, result.GetJ());
Ian Rogers54874942014-06-10 16:31:03 -0700142 break;
143 case Primitive::kPrimNot:
Orion Hodson3d617ac2016-10-19 14:00:46 +0100144 shadow_frame.SetVRegReference(vregA, result.GetL());
Ian Rogers54874942014-06-10 16:31:03 -0700145 break;
146 default:
147 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700148 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700149 }
150 return true;
151}
152
153// Explicitly instantiate all DoFieldGet functions.
154#define EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, _do_check) \
155 template bool DoFieldGet<_find_type, _field_type, _do_check>(Thread* self, \
156 ShadowFrame& shadow_frame, \
157 const Instruction* inst, \
158 uint16_t inst_data)
159
160#define EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(_find_type, _field_type) \
161 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, false); \
162 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, true);
163
164// iget-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700165EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimBoolean)
166EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimByte)
167EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimChar)
168EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimShort)
169EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimInt)
170EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimLong)
171EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstanceObjectRead, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700172
173// sget-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700174EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimBoolean)
175EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimByte)
176EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimChar)
177EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimShort)
178EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimInt)
179EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimLong)
180EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticObjectRead, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700181
182#undef EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL
183#undef EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL
184
Orion Hodson3d617ac2016-10-19 14:00:46 +0100185// Helper for getters in invoke-polymorphic.
186inline static void DoFieldGetForInvokePolymorphic(Thread* self,
187 const ShadowFrame& shadow_frame,
188 ObjPtr<mirror::Object>& obj,
189 ArtField* field,
190 Primitive::Type field_type,
191 JValue* result)
192 REQUIRES_SHARED(Locks::mutator_lock_) {
193 switch (field_type) {
194 case Primitive::kPrimBoolean:
195 DoFieldGetCommon<Primitive::kPrimBoolean>(self, shadow_frame, obj, field, result);
196 break;
197 case Primitive::kPrimByte:
198 DoFieldGetCommon<Primitive::kPrimByte>(self, shadow_frame, obj, field, result);
199 break;
200 case Primitive::kPrimChar:
201 DoFieldGetCommon<Primitive::kPrimChar>(self, shadow_frame, obj, field, result);
202 break;
203 case Primitive::kPrimShort:
204 DoFieldGetCommon<Primitive::kPrimShort>(self, shadow_frame, obj, field, result);
205 break;
206 case Primitive::kPrimInt:
207 DoFieldGetCommon<Primitive::kPrimInt>(self, shadow_frame, obj, field, result);
208 break;
209 case Primitive::kPrimLong:
210 DoFieldGetCommon<Primitive::kPrimLong>(self, shadow_frame, obj, field, result);
211 break;
212 case Primitive::kPrimFloat:
213 DoFieldGetCommon<Primitive::kPrimInt>(self, shadow_frame, obj, field, result);
214 break;
215 case Primitive::kPrimDouble:
216 DoFieldGetCommon<Primitive::kPrimLong>(self, shadow_frame, obj, field, result);
217 break;
218 case Primitive::kPrimNot:
219 DoFieldGetCommon<Primitive::kPrimNot>(self, shadow_frame, obj, field, result);
220 break;
221 case Primitive::kPrimVoid:
222 LOG(FATAL) << "Unreachable: " << field_type;
223 UNREACHABLE();
224 }
225}
226
Ian Rogers54874942014-06-10 16:31:03 -0700227// Handles iget-quick, iget-wide-quick and iget-object-quick instructions.
228// Returns true on success, otherwise throws an exception and returns false.
229template<Primitive::Type field_type>
230bool DoIGetQuick(ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700231 ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
Ian Rogers54874942014-06-10 16:31:03 -0700232 if (UNLIKELY(obj == nullptr)) {
233 // We lost the reference to the field index so we cannot get a more
234 // precised exception message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000235 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -0700236 return false;
237 }
238 MemberOffset field_offset(inst->VRegC_22c());
239 // Report this field access to instrumentation if needed. Since we only have the offset of
240 // the field from the base of the object, we need to look for it first.
241 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
242 if (UNLIKELY(instrumentation->HasFieldReadListeners())) {
243 ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(),
244 field_offset.Uint32Value());
245 DCHECK(f != nullptr);
246 DCHECK(!f->IsStatic());
Mathieu Chartiera3147732016-10-26 22:57:02 -0700247 StackHandleScope<1> hs(Thread::Current());
248 // Save obj in case the instrumentation event has thread suspension.
249 HandleWrapperObjPtr<mirror::Object> h = hs.NewHandleWrapper(&obj);
Mathieu Chartieref41db72016-10-25 15:08:01 -0700250 instrumentation->FieldReadEvent(Thread::Current(),
251 obj.Ptr(),
252 shadow_frame.GetMethod(),
253 shadow_frame.GetDexPC(),
254 f);
Ian Rogers54874942014-06-10 16:31:03 -0700255 }
256 // Note: iget-x-quick instructions are only for non-volatile fields.
257 const uint32_t vregA = inst->VRegA_22c(inst_data);
258 switch (field_type) {
259 case Primitive::kPrimInt:
260 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetField32(field_offset)));
261 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800262 case Primitive::kPrimBoolean:
263 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldBoolean(field_offset)));
264 break;
265 case Primitive::kPrimByte:
266 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldByte(field_offset)));
267 break;
268 case Primitive::kPrimChar:
269 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldChar(field_offset)));
270 break;
271 case Primitive::kPrimShort:
272 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldShort(field_offset)));
273 break;
Ian Rogers54874942014-06-10 16:31:03 -0700274 case Primitive::kPrimLong:
275 shadow_frame.SetVRegLong(vregA, static_cast<int64_t>(obj->GetField64(field_offset)));
276 break;
277 case Primitive::kPrimNot:
278 shadow_frame.SetVRegReference(vregA, obj->GetFieldObject<mirror::Object>(field_offset));
279 break;
280 default:
281 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700282 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700283 }
284 return true;
285}
286
287// Explicitly instantiate all DoIGetQuick functions.
288#define EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(_field_type) \
289 template bool DoIGetQuick<_field_type>(ShadowFrame& shadow_frame, const Instruction* inst, \
290 uint16_t inst_data)
291
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800292EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimInt); // iget-quick.
293EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimBoolean); // iget-boolean-quick.
294EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimByte); // iget-byte-quick.
295EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimChar); // iget-char-quick.
296EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimShort); // iget-short-quick.
297EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimLong); // iget-wide-quick.
298EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimNot); // iget-object-quick.
Ian Rogers54874942014-06-10 16:31:03 -0700299#undef EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL
300
301template<Primitive::Type field_type>
302static JValue GetFieldValue(const ShadowFrame& shadow_frame, uint32_t vreg)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700303 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers54874942014-06-10 16:31:03 -0700304 JValue field_value;
305 switch (field_type) {
306 case Primitive::kPrimBoolean:
307 field_value.SetZ(static_cast<uint8_t>(shadow_frame.GetVReg(vreg)));
308 break;
309 case Primitive::kPrimByte:
310 field_value.SetB(static_cast<int8_t>(shadow_frame.GetVReg(vreg)));
311 break;
312 case Primitive::kPrimChar:
313 field_value.SetC(static_cast<uint16_t>(shadow_frame.GetVReg(vreg)));
314 break;
315 case Primitive::kPrimShort:
316 field_value.SetS(static_cast<int16_t>(shadow_frame.GetVReg(vreg)));
317 break;
318 case Primitive::kPrimInt:
319 field_value.SetI(shadow_frame.GetVReg(vreg));
320 break;
321 case Primitive::kPrimLong:
322 field_value.SetJ(shadow_frame.GetVRegLong(vreg));
323 break;
324 case Primitive::kPrimNot:
325 field_value.SetL(shadow_frame.GetVRegReference(vreg));
326 break;
327 default:
328 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700329 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700330 }
331 return field_value;
332}
333
Orion Hodson3d617ac2016-10-19 14:00:46 +0100334template<Primitive::Type field_type, bool do_assignability_check, bool transaction_active>
335static inline bool DoFieldPutCommon(Thread* self,
336 const ShadowFrame& shadow_frame,
337 ObjPtr<mirror::Object>& obj,
338 ArtField* f,
339 size_t vregA) REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz1edbd8e2014-07-16 20:00:11 +0200340 f->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self);
Orion Hodson3d617ac2016-10-19 14:00:46 +0100341
Ian Rogers54874942014-06-10 16:31:03 -0700342 // Report this field access to instrumentation if needed. Since we only have the offset of
343 // the field from the base of the object, we need to look for it first.
344 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
345 if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
Mathieu Chartier0715c0b2016-10-03 22:49:46 -0700346 StackHandleScope<1> hs(self);
347 // Wrap in handle wrapper in case the listener does thread suspension.
348 HandleWrapperObjPtr<mirror::Object> h(hs.NewHandleWrapper(&obj));
Ian Rogers54874942014-06-10 16:31:03 -0700349 JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA);
Mathieu Chartieref41db72016-10-25 15:08:01 -0700350 ObjPtr<mirror::Object> this_object = f->IsStatic() ? nullptr : obj;
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700351 instrumentation->FieldWriteEvent(self, this_object.Ptr(),
Mathieu Chartier3398c782016-09-30 10:27:43 -0700352 shadow_frame.GetMethod(),
353 shadow_frame.GetDexPC(),
354 f,
355 field_value);
Ian Rogers54874942014-06-10 16:31:03 -0700356 }
Orion Hodson3d617ac2016-10-19 14:00:46 +0100357
Ian Rogers54874942014-06-10 16:31:03 -0700358 switch (field_type) {
359 case Primitive::kPrimBoolean:
360 f->SetBoolean<transaction_active>(obj, shadow_frame.GetVReg(vregA));
361 break;
362 case Primitive::kPrimByte:
363 f->SetByte<transaction_active>(obj, shadow_frame.GetVReg(vregA));
364 break;
365 case Primitive::kPrimChar:
366 f->SetChar<transaction_active>(obj, shadow_frame.GetVReg(vregA));
367 break;
368 case Primitive::kPrimShort:
369 f->SetShort<transaction_active>(obj, shadow_frame.GetVReg(vregA));
370 break;
371 case Primitive::kPrimInt:
372 f->SetInt<transaction_active>(obj, shadow_frame.GetVReg(vregA));
373 break;
374 case Primitive::kPrimLong:
375 f->SetLong<transaction_active>(obj, shadow_frame.GetVRegLong(vregA));
376 break;
377 case Primitive::kPrimNot: {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700378 ObjPtr<mirror::Object> reg = shadow_frame.GetVRegReference(vregA);
Ian Rogers54874942014-06-10 16:31:03 -0700379 if (do_assignability_check && reg != nullptr) {
380 // FieldHelper::GetType can resolve classes, use a handle wrapper which will restore the
381 // object in the destructor.
Mathieu Chartieref41db72016-10-25 15:08:01 -0700382 ObjPtr<mirror::Class> field_class;
Ian Rogers54874942014-06-10 16:31:03 -0700383 {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700384 StackHandleScope<2> hs(self);
Mathieu Chartieref41db72016-10-25 15:08:01 -0700385 HandleWrapperObjPtr<mirror::Object> h_reg(hs.NewHandleWrapper(&reg));
Mathieu Chartier3398c782016-09-30 10:27:43 -0700386 HandleWrapperObjPtr<mirror::Object> h_obj(hs.NewHandleWrapper(&obj));
Mathieu Chartierc7853442015-03-27 14:35:38 -0700387 field_class = f->GetType<true>();
Ian Rogers54874942014-06-10 16:31:03 -0700388 }
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700389 if (!reg->VerifierInstanceOf(field_class.Ptr())) {
Ian Rogers54874942014-06-10 16:31:03 -0700390 // This should never happen.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700391 std::string temp1, temp2, temp3;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000392 self->ThrowNewExceptionF("Ljava/lang/VirtualMachineError;",
Ian Rogers54874942014-06-10 16:31:03 -0700393 "Put '%s' that is not instance of field '%s' in '%s'",
Ian Rogers1ff3c982014-08-12 02:30:58 -0700394 reg->GetClass()->GetDescriptor(&temp1),
395 field_class->GetDescriptor(&temp2),
396 f->GetDeclaringClass()->GetDescriptor(&temp3));
Ian Rogers54874942014-06-10 16:31:03 -0700397 return false;
398 }
399 }
400 f->SetObj<transaction_active>(obj, reg);
401 break;
402 }
403 default:
404 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700405 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700406 }
407 return true;
408}
409
Orion Hodson3d617ac2016-10-19 14:00:46 +0100410template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check,
411 bool transaction_active>
412bool DoFieldPut(Thread* self, const ShadowFrame& shadow_frame, const Instruction* inst,
413 uint16_t inst_data) {
414 const bool do_assignability_check = do_access_check;
415 bool is_static = (find_type == StaticObjectWrite) || (find_type == StaticPrimitiveWrite);
416 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
417 ArtField* f =
418 FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
419 Primitive::ComponentSize(field_type));
420 if (UNLIKELY(f == nullptr)) {
421 CHECK(self->IsExceptionPending());
422 return false;
423 }
424 ObjPtr<mirror::Object> obj;
425 if (is_static) {
426 obj = f->GetDeclaringClass();
427 } else {
428 obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
429 if (UNLIKELY(obj == nullptr)) {
430 ThrowNullPointerExceptionForFieldAccess(f, false);
431 return false;
432 }
433 }
434
435 uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
436 return DoFieldPutCommon<field_type, do_assignability_check, transaction_active>(self,
437 shadow_frame,
438 obj,
439 f,
440 vregA);
441}
442
Ian Rogers54874942014-06-10 16:31:03 -0700443// Explicitly instantiate all DoFieldPut functions.
444#define EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, _do_check, _transaction_active) \
445 template bool DoFieldPut<_find_type, _field_type, _do_check, _transaction_active>(Thread* self, \
446 const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data)
447
448#define EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(_find_type, _field_type) \
449 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, false); \
450 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, false); \
451 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, true); \
452 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, true);
453
454// iput-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700455EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimBoolean)
456EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimByte)
457EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimChar)
458EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimShort)
459EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimInt)
460EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimLong)
461EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstanceObjectWrite, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700462
463// sput-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700464EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimBoolean)
465EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimByte)
466EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimChar)
467EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimShort)
468EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimInt)
469EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimLong)
470EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticObjectWrite, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700471
472#undef EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL
473#undef EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL
474
Orion Hodson3d617ac2016-10-19 14:00:46 +0100475// Helper for setters in invoke-polymorphic.
476bool DoFieldPutForInvokePolymorphic(Thread* self,
477 ShadowFrame& shadow_frame,
478 ObjPtr<mirror::Object>& obj,
479 ArtField* field,
480 Primitive::Type field_type,
481 size_t vregA) REQUIRES_SHARED(Locks::mutator_lock_) {
482 static const bool kDoCheckAssignability = false;
483 static const bool kTransaction = false;
484 switch (field_type) {
485 case Primitive::kPrimBoolean:
486 return DoFieldPutCommon<Primitive::kPrimBoolean, kDoCheckAssignability, kTransaction>(
487 self, shadow_frame, obj, field, vregA);
488 case Primitive::kPrimByte:
489 return DoFieldPutCommon<Primitive::kPrimByte, kDoCheckAssignability, kTransaction>(
490 self, shadow_frame, obj, field, vregA);
491 case Primitive::kPrimChar:
492 return DoFieldPutCommon<Primitive::kPrimChar, kDoCheckAssignability, kTransaction>(
493 self, shadow_frame, obj, field, vregA);
494 case Primitive::kPrimShort:
495 return DoFieldPutCommon<Primitive::kPrimShort, kDoCheckAssignability, kTransaction>(
496 self, shadow_frame, obj, field, vregA);
497 case Primitive::kPrimInt:
498 return DoFieldPutCommon<Primitive::kPrimInt, kDoCheckAssignability, kTransaction>(
499 self, shadow_frame, obj, field, vregA);
500 case Primitive::kPrimLong:
501 return DoFieldPutCommon<Primitive::kPrimLong, kDoCheckAssignability, kTransaction>(
502 self, shadow_frame, obj, field, vregA);
503 case Primitive::kPrimFloat:
504 return DoFieldPutCommon<Primitive::kPrimInt, kDoCheckAssignability, kTransaction>(
505 self, shadow_frame, obj, field, vregA);
506 case Primitive::kPrimDouble:
507 return DoFieldPutCommon<Primitive::kPrimLong, kDoCheckAssignability, kTransaction>(
508 self, shadow_frame, obj, field, vregA);
509 case Primitive::kPrimNot:
510 return DoFieldPutCommon<Primitive::kPrimNot, kDoCheckAssignability, kTransaction>(
511 self, shadow_frame, obj, field, vregA);
512 case Primitive::kPrimVoid:
513 LOG(FATAL) << "Unreachable: " << field_type;
514 UNREACHABLE();
515 }
516}
517
Ian Rogers54874942014-06-10 16:31:03 -0700518template<Primitive::Type field_type, bool transaction_active>
519bool DoIPutQuick(const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700520 ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
Ian Rogers54874942014-06-10 16:31:03 -0700521 if (UNLIKELY(obj == nullptr)) {
522 // We lost the reference to the field index so we cannot get a more
523 // precised exception message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000524 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -0700525 return false;
526 }
527 MemberOffset field_offset(inst->VRegC_22c());
528 const uint32_t vregA = inst->VRegA_22c(inst_data);
529 // Report this field modification to instrumentation if needed. Since we only have the offset of
530 // the field from the base of the object, we need to look for it first.
531 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
532 if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
533 ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(),
534 field_offset.Uint32Value());
535 DCHECK(f != nullptr);
536 DCHECK(!f->IsStatic());
537 JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA);
Mathieu Chartiera3147732016-10-26 22:57:02 -0700538 StackHandleScope<1> hs(Thread::Current());
539 // Save obj in case the instrumentation event has thread suspension.
540 HandleWrapperObjPtr<mirror::Object> h = hs.NewHandleWrapper(&obj);
Mathieu Chartieref41db72016-10-25 15:08:01 -0700541 instrumentation->FieldWriteEvent(Thread::Current(),
542 obj.Ptr(),
543 shadow_frame.GetMethod(),
544 shadow_frame.GetDexPC(),
545 f,
546 field_value);
Ian Rogers54874942014-06-10 16:31:03 -0700547 }
548 // Note: iput-x-quick instructions are only for non-volatile fields.
549 switch (field_type) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700550 case Primitive::kPrimBoolean:
551 obj->SetFieldBoolean<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
552 break;
553 case Primitive::kPrimByte:
554 obj->SetFieldByte<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
555 break;
556 case Primitive::kPrimChar:
557 obj->SetFieldChar<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
558 break;
559 case Primitive::kPrimShort:
560 obj->SetFieldShort<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
561 break;
Ian Rogers54874942014-06-10 16:31:03 -0700562 case Primitive::kPrimInt:
563 obj->SetField32<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
564 break;
565 case Primitive::kPrimLong:
566 obj->SetField64<transaction_active>(field_offset, shadow_frame.GetVRegLong(vregA));
567 break;
568 case Primitive::kPrimNot:
569 obj->SetFieldObject<transaction_active>(field_offset, shadow_frame.GetVRegReference(vregA));
570 break;
571 default:
572 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700573 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700574 }
575 return true;
576}
577
578// Explicitly instantiate all DoIPutQuick functions.
579#define EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, _transaction_active) \
580 template bool DoIPutQuick<_field_type, _transaction_active>(const ShadowFrame& shadow_frame, \
581 const Instruction* inst, \
582 uint16_t inst_data)
583
584#define EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(_field_type) \
585 EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, false); \
586 EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, true);
587
Andreas Gampec8ccf682014-09-29 20:07:43 -0700588EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimInt) // iput-quick.
589EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimBoolean) // iput-boolean-quick.
590EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimByte) // iput-byte-quick.
591EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimChar) // iput-char-quick.
592EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimShort) // iput-short-quick.
593EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimLong) // iput-wide-quick.
594EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimNot) // iput-object-quick.
Ian Rogers54874942014-06-10 16:31:03 -0700595#undef EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL
596#undef EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL
597
Sebastien Hertz520633b2015-09-08 17:03:36 +0200598// We accept a null Instrumentation* meaning we must not report anything to the instrumentation.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700599uint32_t FindNextInstructionFollowingException(
600 Thread* self, ShadowFrame& shadow_frame, uint32_t dex_pc,
601 const instrumentation::Instrumentation* instrumentation) {
Ian Rogers54874942014-06-10 16:31:03 -0700602 self->VerifyStack();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700603 StackHandleScope<2> hs(self);
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000604 Handle<mirror::Throwable> exception(hs.NewHandle(self->GetException()));
Sebastien Hertz520633b2015-09-08 17:03:36 +0200605 if (instrumentation != nullptr && instrumentation->HasExceptionCaughtListeners()
Nicolas Geoffray7642cfc2015-02-26 10:56:09 +0000606 && self->IsExceptionThrownByCurrentMethod(exception.Get())) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000607 instrumentation->ExceptionCaughtEvent(self, exception.Get());
Sebastien Hertz9f102032014-05-23 08:59:42 +0200608 }
Ian Rogers54874942014-06-10 16:31:03 -0700609 bool clear_exception = false;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700610 uint32_t found_dex_pc = shadow_frame.GetMethod()->FindCatchBlock(
611 hs.NewHandle(exception->GetClass()), dex_pc, &clear_exception);
Sebastien Hertz520633b2015-09-08 17:03:36 +0200612 if (found_dex_pc == DexFile::kDexNoIndex && instrumentation != nullptr) {
Nicolas Geoffray7642cfc2015-02-26 10:56:09 +0000613 // Exception is not caught by the current method. We will unwind to the
614 // caller. Notify any instrumentation listener.
Sebastien Hertz9f102032014-05-23 08:59:42 +0200615 instrumentation->MethodUnwindEvent(self, shadow_frame.GetThisObject(),
Ian Rogers54874942014-06-10 16:31:03 -0700616 shadow_frame.GetMethod(), dex_pc);
617 } else {
Nicolas Geoffray7642cfc2015-02-26 10:56:09 +0000618 // Exception is caught in the current method. We will jump to the found_dex_pc.
Ian Rogers54874942014-06-10 16:31:03 -0700619 if (clear_exception) {
620 self->ClearException();
621 }
622 }
623 return found_dex_pc;
624}
625
Ian Rogerse94652f2014-12-02 11:13:19 -0800626void UnexpectedOpcode(const Instruction* inst, const ShadowFrame& shadow_frame) {
627 LOG(FATAL) << "Unexpected instruction: "
628 << inst->DumpString(shadow_frame.GetMethod()->GetDexFile());
629 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700630}
631
Sebastien Hertz45b15972015-04-03 16:07:05 +0200632void AbortTransactionF(Thread* self, const char* fmt, ...) {
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700633 va_list args;
634 va_start(args, fmt);
Sebastien Hertz45b15972015-04-03 16:07:05 +0200635 AbortTransactionV(self, fmt, args);
636 va_end(args);
637}
638
639void AbortTransactionV(Thread* self, const char* fmt, va_list args) {
640 CHECK(Runtime::Current()->IsActiveTransaction());
641 // Constructs abort message.
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100642 std::string abort_msg;
643 StringAppendV(&abort_msg, fmt, args);
644 // Throws an exception so we can abort the transaction and rollback every change.
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200645 Runtime::Current()->AbortTransactionAndThrowAbortError(self, abort_msg);
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700646}
647
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100648// START DECLARATIONS :
649//
650// These additional declarations are required because clang complains
651// about ALWAYS_INLINE (-Werror, -Wgcc-compat) in definitions.
652//
653
Narayan Kamath9823e782016-08-03 12:46:58 +0100654template <bool is_range, bool do_assignability_check>
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700655 REQUIRES_SHARED(Locks::mutator_lock_)
Igor Murashkin158f35c2015-06-10 15:55:30 -0700656static inline bool DoCallCommon(ArtMethod* called_method,
657 Thread* self,
658 ShadowFrame& shadow_frame,
659 JValue* result,
660 uint16_t number_of_inputs,
Narayan Kamath370423d2016-10-03 16:51:22 +0100661 uint32_t (&arg)[Instruction::kMaxVarArgRegs],
Igor Murashkin158f35c2015-06-10 15:55:30 -0700662 uint32_t vregC) ALWAYS_INLINE;
663
Narayan Kamath208f8572016-08-03 12:46:58 +0100664template <bool is_range> REQUIRES_SHARED(Locks::mutator_lock_)
665static inline bool DoCallPolymorphic(ArtMethod* called_method,
666 Handle<mirror::MethodType> callsite_type,
667 Handle<mirror::MethodType> target_type,
668 Thread* self,
669 ShadowFrame& shadow_frame,
670 JValue* result,
671 uint32_t (&arg)[Instruction::kMaxVarArgRegs],
672 uint32_t vregC) ALWAYS_INLINE;
673
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100674REQUIRES_SHARED(Locks::mutator_lock_)
675static inline bool DoCallTransform(ArtMethod* called_method,
676 Handle<mirror::MethodType> callsite_type,
677 Thread* self,
678 ShadowFrame& shadow_frame,
679 Handle<mirror::MethodHandleImpl> receiver,
680 JValue* result) ALWAYS_INLINE;
681
682REQUIRES_SHARED(Locks::mutator_lock_)
683inline void PerformCall(Thread* self,
684 const DexFile::CodeItem* code_item,
685 ArtMethod* caller_method,
686 const size_t first_dest_reg,
687 ShadowFrame* callee_frame,
688 JValue* result) ALWAYS_INLINE;
689
690template <bool is_range>
691REQUIRES_SHARED(Locks::mutator_lock_)
692inline void CopyRegisters(ShadowFrame& caller_frame,
693 ShadowFrame* callee_frame,
694 const uint32_t (&arg)[Instruction::kMaxVarArgRegs],
695 const size_t first_src_reg,
696 const size_t first_dest_reg,
697 const size_t num_regs) ALWAYS_INLINE;
698
699// END DECLARATIONS.
700
Siva Chandra05d24152016-01-05 17:43:17 -0800701void ArtInterpreterToCompiledCodeBridge(Thread* self,
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100702 ArtMethod* caller,
Siva Chandra05d24152016-01-05 17:43:17 -0800703 const DexFile::CodeItem* code_item,
704 ShadowFrame* shadow_frame,
705 JValue* result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700706 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700707 ArtMethod* method = shadow_frame->GetMethod();
708 // Ensure static methods are initialized.
709 if (method->IsStatic()) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700710 ObjPtr<mirror::Class> declaringClass = method->GetDeclaringClass();
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700711 if (UNLIKELY(!declaringClass->IsInitialized())) {
712 self->PushShadowFrame(shadow_frame);
713 StackHandleScope<1> hs(self);
714 Handle<mirror::Class> h_class(hs.NewHandle(declaringClass));
715 if (UNLIKELY(!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true,
716 true))) {
717 self->PopShadowFrame();
718 DCHECK(self->IsExceptionPending());
719 return;
720 }
721 self->PopShadowFrame();
722 CHECK(h_class->IsInitializing());
723 // Reload from shadow frame in case the method moved, this is faster than adding a handle.
724 method = shadow_frame->GetMethod();
725 }
726 }
727 uint16_t arg_offset = (code_item == nullptr)
728 ? 0
729 : code_item->registers_size_ - code_item->ins_size_;
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100730 jit::Jit* jit = Runtime::Current()->GetJit();
731 if (jit != nullptr && caller != nullptr) {
732 jit->NotifyInterpreterToCompiledCodeTransition(self, caller);
733 }
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700734 method->Invoke(self, shadow_frame->GetVRegArgs(arg_offset),
735 (shadow_frame->NumberOfVRegs() - arg_offset) * sizeof(uint32_t),
Andreas Gampe542451c2016-07-26 09:02:02 -0700736 result, method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty());
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700737}
738
Mingyao Yangffedec52016-05-19 10:48:40 -0700739void SetStringInitValueToAllAliases(ShadowFrame* shadow_frame,
740 uint16_t this_obj_vreg,
741 JValue result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700742 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700743 ObjPtr<mirror::Object> existing = shadow_frame->GetVRegReference(this_obj_vreg);
Mingyao Yangffedec52016-05-19 10:48:40 -0700744 if (existing == nullptr) {
745 // If it's null, we come from compiled code that was deoptimized. Nothing to do,
746 // as the compiler verified there was no alias.
747 // Set the new string result of the StringFactory.
748 shadow_frame->SetVRegReference(this_obj_vreg, result.GetL());
749 return;
750 }
751 // Set the string init result into all aliases.
752 for (uint32_t i = 0, e = shadow_frame->NumberOfVRegs(); i < e; ++i) {
753 if (shadow_frame->GetVRegReference(i) == existing) {
754 DCHECK_EQ(shadow_frame->GetVRegReference(i),
755 reinterpret_cast<mirror::Object*>(shadow_frame->GetVReg(i)));
756 shadow_frame->SetVRegReference(i, result.GetL());
757 DCHECK_EQ(shadow_frame->GetVRegReference(i),
758 reinterpret_cast<mirror::Object*>(shadow_frame->GetVReg(i)));
759 }
760 }
761}
762
Orion Hodson3d617ac2016-10-19 14:00:46 +0100763inline static bool IsInvokeExact(const DexFile& dex_file, int invoke_method_idx) {
764 // This check uses string comparison as it needs less code and data
765 // to do than fetching the associated ArtMethod from the DexCache
766 // and checking against ArtMethods in the well known classes. The
767 // verifier needs to perform a more rigorous check.
768 const char* method_name = dex_file.GetMethodName(dex_file.GetMethodId(invoke_method_idx));
769 bool is_invoke_exact = (0 == strcmp(method_name, "invokeExact"));
770 DCHECK(is_invoke_exact || (0 == strcmp(method_name, "invoke")));
771 return is_invoke_exact;
772}
773
Narayan Kamath9823e782016-08-03 12:46:58 +0100774template<bool is_range, bool do_access_check>
Mathieu Chartieref41db72016-10-25 15:08:01 -0700775inline bool DoInvokePolymorphic(Thread* self,
776 ShadowFrame& shadow_frame,
777 const Instruction* inst,
778 uint16_t inst_data,
779 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
Narayan Kamath9823e782016-08-03 12:46:58 +0100780 // Invoke-polymorphic instructions always take a receiver. i.e, they are never static.
781 const uint32_t vRegC = (is_range) ? inst->VRegC_4rcc() : inst->VRegC_45cc();
Orion Hodson3d617ac2016-10-19 14:00:46 +0100782 const int invoke_method_idx = (is_range) ? inst->VRegB_4rcc() : inst->VRegB_45cc();
Narayan Kamath9823e782016-08-03 12:46:58 +0100783
Orion Hodson3d617ac2016-10-19 14:00:46 +0100784 // Determine if this invocation is MethodHandle.invoke() or
785 // MethodHandle.invokeExact().
786 bool is_invoke_exact = IsInvokeExact(shadow_frame.GetMethod()->GetDeclaringClass()->GetDexFile(),
787 invoke_method_idx);
788
789 // The invoke_method_idx here is the name of the signature polymorphic method that
Narayan Kamath9823e782016-08-03 12:46:58 +0100790 // was symbolically invoked in bytecode (say MethodHandle.invoke or MethodHandle.invokeExact)
791 // and not the method that we'll dispatch to in the end.
792 //
793 // TODO(narayan) We'll have to check in the verifier that this is in fact a
794 // signature polymorphic method so that we disallow calls via invoke-polymorphic
795 // to non sig-poly methods. This would also have the side effect of verifying
796 // that vRegC really is a reference type.
Narayan Kamath208f8572016-08-03 12:46:58 +0100797 StackHandleScope<6> hs(self);
798 Handle<mirror::MethodHandleImpl> method_handle(hs.NewHandle(
Mathieu Chartieref41db72016-10-25 15:08:01 -0700799 ObjPtr<mirror::MethodHandleImpl>::DownCast(
800 MakeObjPtr(shadow_frame.GetVRegReference(vRegC)))));
Narayan Kamath208f8572016-08-03 12:46:58 +0100801 if (UNLIKELY(method_handle.Get() == nullptr)) {
Narayan Kamath9823e782016-08-03 12:46:58 +0100802 // Note that the invoke type is kVirtual here because a call to a signature
803 // polymorphic method is shaped like a virtual call at the bytecode level.
Orion Hodson3d617ac2016-10-19 14:00:46 +0100804 ThrowNullPointerExceptionForMethodAccess(invoke_method_idx, InvokeType::kVirtual);
Narayan Kamath9823e782016-08-03 12:46:58 +0100805 result->SetJ(0);
806 return false;
807 }
808
809 // The vRegH value gives the index of the proto_id associated with this
810 // signature polymorphic callsite.
811 const uint32_t callsite_proto_id = (is_range) ? inst->VRegH_4rcc() : inst->VRegH_45cc();
812
813 // Call through to the classlinker and ask it to resolve the static type associated
814 // with the callsite. This information is stored in the dex cache so it's
815 // guaranteed to be fast after the first resolution.
Narayan Kamath9823e782016-08-03 12:46:58 +0100816 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Narayan Kamath208f8572016-08-03 12:46:58 +0100817 Handle<mirror::Class> caller_class(hs.NewHandle(shadow_frame.GetMethod()->GetDeclaringClass()));
818 Handle<mirror::MethodType> callsite_type(hs.NewHandle(class_linker->ResolveMethodType(
Narayan Kamath9823e782016-08-03 12:46:58 +0100819 caller_class->GetDexFile(), callsite_proto_id,
820 hs.NewHandle<mirror::DexCache>(caller_class->GetDexCache()),
Narayan Kamath208f8572016-08-03 12:46:58 +0100821 hs.NewHandle<mirror::ClassLoader>(caller_class->GetClassLoader()))));
Narayan Kamath9823e782016-08-03 12:46:58 +0100822
823 // This implies we couldn't resolve one or more types in this method handle.
Narayan Kamath208f8572016-08-03 12:46:58 +0100824 if (UNLIKELY(callsite_type.Get() == nullptr)) {
Narayan Kamath9823e782016-08-03 12:46:58 +0100825 CHECK(self->IsExceptionPending());
826 result->SetJ(0);
827 return false;
828 }
829
Narayan Kamath9823e782016-08-03 12:46:58 +0100830 const MethodHandleKind handle_kind = method_handle->GetHandleKind();
Narayan Kamath208f8572016-08-03 12:46:58 +0100831 Handle<mirror::MethodType> handle_type(hs.NewHandle(method_handle->GetMethodType()));
Narayan Kamath208f8572016-08-03 12:46:58 +0100832 CHECK(handle_type.Get() != nullptr);
Orion Hodson3d617ac2016-10-19 14:00:46 +0100833 if (UNLIKELY(is_invoke_exact && !callsite_type->IsExactMatch(handle_type.Get()))) {
834 ThrowWrongMethodTypeException(callsite_type.Get(), handle_type.Get());
835 return false;
836 }
Narayan Kamath9823e782016-08-03 12:46:58 +0100837
Narayan Kamath9823e782016-08-03 12:46:58 +0100838 uint32_t arg[Instruction::kMaxVarArgRegs] = {};
839 uint32_t receiver_vregC = 0;
840 if (is_range) {
841 receiver_vregC = (inst->VRegC_4rcc() + 1);
842 } else {
843 inst->GetVarArgs(arg, inst_data);
844 arg[0] = arg[1];
845 arg[1] = arg[2];
846 arg[2] = arg[3];
847 arg[3] = arg[4];
848 arg[4] = 0;
849 receiver_vregC = arg[0];
850 }
851
852 if (IsInvoke(handle_kind)) {
Orion Hodson3d617ac2016-10-19 14:00:46 +0100853 // Get the method we're actually invoking along with the kind of
854 // invoke that is desired. We don't need to perform access checks at this
855 // point because they would have been performed on our behalf at the point
856 // of creation of the method handle.
857 ArtMethod* called_method = method_handle->GetTargetMethod();
858 CHECK(called_method != nullptr);
859
Narayan Kamath9823e782016-08-03 12:46:58 +0100860 if (handle_kind == kInvokeVirtual || handle_kind == kInvokeInterface) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700861 ObjPtr<mirror::Object> receiver = shadow_frame.GetVRegReference(receiver_vregC);
862 ObjPtr<mirror::Class> declaring_class = called_method->GetDeclaringClass();
Narayan Kamath9823e782016-08-03 12:46:58 +0100863 // Verify that _vRegC is an object reference and of the type expected by
864 // the receiver.
865 called_method = receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(
866 called_method, kRuntimePointerSize);
867 if (!VerifyObjectIsClass(receiver, declaring_class)) {
Narayan Kamath9823e782016-08-03 12:46:58 +0100868 return false;
869 }
870 } else if (handle_kind == kInvokeDirect) {
Narayan Kamath9bdaeeb2016-10-20 10:57:45 +0100871 if (called_method->IsConstructor()) {
872 // TODO(narayan) : We need to handle the case where the target method is a
873 // constructor here.
874 UNIMPLEMENTED(FATAL) << "Direct invokes for constructors are not implemented yet.";
875 return false;
876 }
877
878 // Nothing special to do in the case where we're not dealing with a
879 // constructor. It's a private method, and we've already access checked at
880 // the point of creating the handle.
881 } else if (handle_kind == kInvokeSuper) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700882 ObjPtr<mirror::Class> declaring_class = called_method->GetDeclaringClass();
Narayan Kamath9bdaeeb2016-10-20 10:57:45 +0100883
884 // Note that we're not dynamically dispatching on the type of the receiver
885 // here. We use the static type of the "receiver" object that we've
886 // recorded in the method handle's type, which will be the same as the
887 // special caller that was specified at the point of lookup.
Mathieu Chartieref41db72016-10-25 15:08:01 -0700888 ObjPtr<mirror::Class> referrer_class = handle_type->GetPTypes()->Get(0);
Narayan Kamath9bdaeeb2016-10-20 10:57:45 +0100889 if (!declaring_class->IsInterface()) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700890 ObjPtr<mirror::Class> super_class = referrer_class->GetSuperClass();
Narayan Kamath9bdaeeb2016-10-20 10:57:45 +0100891 uint16_t vtable_index = called_method->GetMethodIndex();
892 DCHECK(super_class != nullptr);
893 DCHECK(super_class->HasVTable());
894 // Note that super_class is a super of referrer_class and called_method
895 // will always be declared by super_class (or one of its super classes).
896 DCHECK_LT(vtable_index, super_class->GetVTableLength());
897 called_method = super_class->GetVTableEntry(vtable_index, kRuntimePointerSize);
898 } else {
899 called_method = referrer_class->FindVirtualMethodForInterfaceSuper(
900 called_method, kRuntimePointerSize);
901 }
902
903 CHECK(called_method != nullptr);
Narayan Kamath9823e782016-08-03 12:46:58 +0100904 }
905
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100906 if (handle_kind == kInvokeTransform) {
907 return DoCallTransform(called_method,
908 callsite_type,
909 self,
910 shadow_frame,
911 method_handle /* receiver */,
912 result);
Narayan Kamath208f8572016-08-03 12:46:58 +0100913 } else {
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100914 return DoCallPolymorphic<is_range>(called_method,
915 callsite_type,
916 handle_type,
917 self,
918 shadow_frame,
919 result,
920 arg,
921 receiver_vregC);
Narayan Kamath9823e782016-08-03 12:46:58 +0100922 }
Narayan Kamath9823e782016-08-03 12:46:58 +0100923 } else {
Orion Hodson3d617ac2016-10-19 14:00:46 +0100924 DCHECK(!is_range);
925 ArtField* field = method_handle->GetTargetField();
926 Primitive::Type field_type = field->GetTypeAsPrimitiveType();;
927
928 if (!is_invoke_exact) {
929 // TODO(oth): conversion plumbing for invoke().
930 UNIMPLEMENTED(FATAL);
931 }
932
933 switch (handle_kind) {
934 case kInstanceGet: {
935 ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(receiver_vregC);
936 DoFieldGetForInvokePolymorphic(self, shadow_frame, obj, field, field_type, result);
937 return true;
938 }
939 case kInstancePut: {
940 ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(receiver_vregC);
941 return DoFieldPutForInvokePolymorphic(self, shadow_frame, obj, field, field_type, arg[1]);
942 }
943 case kStaticGet: {
944 ObjPtr<mirror::Object> obj = field->GetDeclaringClass();
945 DoFieldGetForInvokePolymorphic(self, shadow_frame, obj, field, field_type, result);
946 return true;
947 }
948 case kStaticPut: {
949 ObjPtr<mirror::Object> obj = field->GetDeclaringClass();
950 return DoFieldPutForInvokePolymorphic(self, shadow_frame, obj, field, field_type, arg[0]);
951 }
952 default:
953 LOG(FATAL) << "Unreachable: " << handle_kind;
954 UNREACHABLE();
955 }
Narayan Kamath9823e782016-08-03 12:46:58 +0100956 }
957}
958
Narayan Kamath208f8572016-08-03 12:46:58 +0100959// Calculate the number of ins for a proxy or native method, where we
960// can't just look at the code item.
961static inline size_t GetInsForProxyOrNativeMethod(ArtMethod* method)
962 REQUIRES_SHARED(Locks::mutator_lock_) {
963 DCHECK(method->IsNative() || method->IsProxyMethod());
964
965 method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
966 size_t num_ins = 0;
967 // Separate accounting for the receiver, which isn't a part of the
968 // shorty.
969 if (!method->IsStatic()) {
970 ++num_ins;
971 }
972
973 uint32_t shorty_len = 0;
974 const char* shorty = method->GetShorty(&shorty_len);
975 for (size_t i = 1; i < shorty_len; ++i) {
976 const char c = shorty[i];
977 ++num_ins;
978 if (c == 'J' || c == 'D') {
979 ++num_ins;
980 }
981 }
982
983 return num_ins;
984}
985
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100986
987inline void PerformCall(Thread* self,
988 const DexFile::CodeItem* code_item,
989 ArtMethod* caller_method,
990 const size_t first_dest_reg,
991 ShadowFrame* callee_frame,
992 JValue* result) {
993 if (LIKELY(Runtime::Current()->IsStarted())) {
994 ArtMethod* target = callee_frame->GetMethod();
995 if (ClassLinker::ShouldUseInterpreterEntrypoint(
996 target,
997 target->GetEntryPointFromQuickCompiledCode())) {
998 ArtInterpreterToInterpreterBridge(self, code_item, callee_frame, result);
999 } else {
1000 ArtInterpreterToCompiledCodeBridge(
1001 self, caller_method, code_item, callee_frame, result);
1002 }
1003 } else {
1004 UnstartedRuntime::Invoke(self, code_item, callee_frame, result, first_dest_reg);
1005 }
1006}
1007
1008template <bool is_range>
1009inline void CopyRegisters(ShadowFrame& caller_frame,
1010 ShadowFrame* callee_frame,
1011 const uint32_t (&arg)[Instruction::kMaxVarArgRegs],
1012 const size_t first_src_reg,
1013 const size_t first_dest_reg,
1014 const size_t num_regs) {
1015 if (is_range) {
1016 const size_t dest_reg_bound = first_dest_reg + num_regs;
1017 for (size_t src_reg = first_src_reg, dest_reg = first_dest_reg; dest_reg < dest_reg_bound;
1018 ++dest_reg, ++src_reg) {
1019 AssignRegister(callee_frame, caller_frame, dest_reg, src_reg);
1020 }
1021 } else {
1022 DCHECK_LE(num_regs, arraysize(arg));
1023
1024 for (size_t arg_index = 0; arg_index < num_regs; ++arg_index) {
1025 AssignRegister(callee_frame, caller_frame, first_dest_reg + arg_index, arg[arg_index]);
1026 }
1027 }
1028}
1029
1030// Returns true iff. the callsite type for a polymorphic invoke is transformer
1031// like, i.e that it has a single input argument whose type is
1032// dalvik.system.EmulatedStackFrame.
1033static inline bool IsCallerTransformer(Handle<mirror::MethodType> callsite_type)
1034 REQUIRES_SHARED(Locks::mutator_lock_) {
1035 ObjPtr<mirror::ObjectArray<mirror::Class>> param_types(callsite_type->GetPTypes());
1036 if (param_types->GetLength() == 1) {
1037 ObjPtr<mirror::Class> param(param_types->GetWithoutChecks(0));
1038 return param == WellKnownClasses::ToClass(WellKnownClasses::dalvik_system_EmulatedStackFrame);
1039 }
1040
1041 return false;
1042}
1043
Narayan Kamath208f8572016-08-03 12:46:58 +01001044template <bool is_range>
1045static inline bool DoCallPolymorphic(ArtMethod* called_method,
1046 Handle<mirror::MethodType> callsite_type,
1047 Handle<mirror::MethodType> target_type,
1048 Thread* self,
1049 ShadowFrame& shadow_frame,
1050 JValue* result,
1051 uint32_t (&arg)[Instruction::kMaxVarArgRegs],
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001052 uint32_t first_src_reg) {
Narayan Kamath208f8572016-08-03 12:46:58 +01001053 // TODO(narayan): Wire in the String.init hacks.
1054
1055 // Compute method information.
1056 const DexFile::CodeItem* code_item = called_method->GetCodeItem();
1057
1058 // Number of registers for the callee's call frame. Note that for non-exact
1059 // invokes, we always derive this information from the callee method. We
1060 // cannot guarantee during verification that the number of registers encoded
1061 // in the invoke is equal to the number of ins for the callee. This is because
1062 // some transformations (such as boxing a long -> Long or wideining an
1063 // int -> long will change that number.
1064 uint16_t num_regs;
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001065 size_t num_input_regs;
Narayan Kamath208f8572016-08-03 12:46:58 +01001066 size_t first_dest_reg;
1067 if (LIKELY(code_item != nullptr)) {
1068 num_regs = code_item->registers_size_;
1069 first_dest_reg = num_regs - code_item->ins_size_;
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001070 num_input_regs = code_item->ins_size_;
Narayan Kamath208f8572016-08-03 12:46:58 +01001071 // Parameter registers go at the end of the shadow frame.
1072 DCHECK_NE(first_dest_reg, (size_t)-1);
1073 } else {
1074 // No local regs for proxy and native methods.
1075 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001076 num_regs = num_input_regs = GetInsForProxyOrNativeMethod(called_method);
Narayan Kamath208f8572016-08-03 12:46:58 +01001077 first_dest_reg = 0;
1078 }
1079
1080 // Allocate shadow frame on the stack.
1081 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
1082 CREATE_SHADOW_FRAME(num_regs, &shadow_frame, called_method, /* dex pc */ 0);
1083 ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get();
1084
1085 // Thread might be suspended during PerformArgumentConversions due to the
1086 // allocations performed during boxing.
1087 {
1088 ScopedStackedShadowFramePusher pusher(
1089 self, new_shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001090 if (callsite_type->IsExactMatch(target_type.Get())) {
1091 // This is an exact invoke, we can take the fast path of just copying all
1092 // registers without performing any argument conversions.
1093 CopyRegisters<is_range>(shadow_frame,
1094 new_shadow_frame,
1095 arg,
1096 first_src_reg,
1097 first_dest_reg,
1098 num_input_regs);
1099 } else {
1100 // This includes the case where we're entering this invoke-polymorphic
1101 // from a transformer method. In that case, the callsite_type will contain
1102 // a single argument of type dalvik.system.EmulatedStackFrame. In that
1103 // case, we'll have to unmarshal the EmulatedStackFrame into the
1104 // new_shadow_frame and perform argument conversions on it.
1105 if (IsCallerTransformer(callsite_type)) {
1106 // The emulated stack frame will be the first ahnd only argument
1107 // when we're coming through from a transformer.
1108 //
1109 // TODO(narayan): This should be a mirror::EmulatedStackFrame after that
1110 // type is introduced.
1111 ObjPtr<mirror::Object> emulated_stack_frame(
1112 shadow_frame.GetVRegReference(first_src_reg));
1113 if (!ConvertAndCopyArgumentsFromEmulatedStackFrame<is_range>(self,
1114 emulated_stack_frame,
1115 target_type,
1116 first_dest_reg,
1117 new_shadow_frame)) {
1118 DCHECK(self->IsExceptionPending());
1119 result->SetL(0);
1120 return false;
1121 }
1122 } else if (!ConvertAndCopyArgumentsFromCallerFrame<is_range>(self,
1123 callsite_type,
1124 target_type,
1125 shadow_frame,
1126 first_src_reg,
1127 first_dest_reg,
1128 arg,
1129 new_shadow_frame)) {
1130 DCHECK(self->IsExceptionPending());
1131 result->SetL(0);
1132 return false;
1133 }
Narayan Kamath208f8572016-08-03 12:46:58 +01001134 }
1135 }
1136
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001137 PerformCall(self, code_item, shadow_frame.GetMethod(), first_dest_reg, new_shadow_frame, result);
Narayan Kamath208f8572016-08-03 12:46:58 +01001138
1139 // TODO(narayan): Perform return value conversions.
1140
1141 return !self->IsExceptionPending();
1142}
1143
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001144static inline bool DoCallTransform(ArtMethod* called_method,
1145 Handle<mirror::MethodType> callsite_type,
1146 Thread* self,
1147 ShadowFrame& shadow_frame,
1148 Handle<mirror::MethodHandleImpl> receiver,
1149 JValue* result) {
1150 // This can be fixed, because the method we're calling here
1151 // (MethodHandle.transformInternal) doesn't have any locals and the signature
1152 // is known :
1153 //
1154 // private MethodHandle.transformInternal(EmulatedStackFrame sf);
1155 //
1156 // This means we need only two vregs :
1157 // - One for the receiver object.
1158 // - One for the only method argument (an EmulatedStackFrame).
1159 static constexpr size_t kNumRegsForTransform = 2;
1160
1161 const DexFile::CodeItem* code_item = called_method->GetCodeItem();
1162 DCHECK(code_item != nullptr);
1163 DCHECK_EQ(kNumRegsForTransform, code_item->registers_size_);
1164 DCHECK_EQ(kNumRegsForTransform, code_item->ins_size_);
1165
1166 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
1167 CREATE_SHADOW_FRAME(kNumRegsForTransform, &shadow_frame, called_method, /* dex pc */ 0);
1168 ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get();
1169
1170 // TODO(narayan): Perform argument conversions first (if this is an inexact invoke), and
1171 // then construct an argument list object that's passed through to the
1172 // method. Note that the ArgumentList reference is currently a nullptr.
1173 //
1174 // NOTE(narayan): If the caller is a transformer method (i.e, there is only
1175 // one argument and its type is EmulatedStackFrame), we can directly pass that
1176 // through without having to do any additional work.
1177 UNUSED(callsite_type);
1178
1179 new_shadow_frame->SetVRegReference(0, receiver.Get());
1180 // TODO(narayan): This is the EmulatedStackFrame, currently nullptr.
1181 new_shadow_frame->SetVRegReference(1, nullptr);
1182
1183 PerformCall(self,
1184 code_item,
1185 shadow_frame.GetMethod(),
1186 0 /* first dest reg */,
1187 new_shadow_frame,
1188 result);
1189
1190 return !self->IsExceptionPending();
1191}
1192
Igor Murashkin6918bf12015-09-27 19:19:06 -07001193template <bool is_range,
Narayan Kamath370423d2016-10-03 16:51:22 +01001194 bool do_assignability_check>
Igor Murashkin158f35c2015-06-10 15:55:30 -07001195static inline bool DoCallCommon(ArtMethod* called_method,
1196 Thread* self,
1197 ShadowFrame& shadow_frame,
1198 JValue* result,
1199 uint16_t number_of_inputs,
Narayan Kamath370423d2016-10-03 16:51:22 +01001200 uint32_t (&arg)[Instruction::kMaxVarArgRegs],
Igor Murashkin158f35c2015-06-10 15:55:30 -07001201 uint32_t vregC) {
Jeff Hao848f70a2014-01-15 13:49:50 -08001202 bool string_init = false;
1203 // Replace calls to String.<init> with equivalent StringFactory call.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001204 if (UNLIKELY(called_method->GetDeclaringClass()->IsStringClass()
1205 && called_method->IsConstructor())) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01001206 called_method = WellKnownClasses::StringInitToStringFactory(called_method);
Jeff Hao848f70a2014-01-15 13:49:50 -08001207 string_init = true;
1208 }
1209
Alex Lightdaf58c82016-03-16 23:00:49 +00001210 // Compute method information.
1211 const DexFile::CodeItem* code_item = called_method->GetCodeItem();
Igor Murashkin158f35c2015-06-10 15:55:30 -07001212
1213 // Number of registers for the callee's call frame.
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001214 uint16_t num_regs;
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001215 if (LIKELY(code_item != nullptr)) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001216 num_regs = code_item->registers_size_;
Igor Murashkin158f35c2015-06-10 15:55:30 -07001217 DCHECK_EQ(string_init ? number_of_inputs - 1 : number_of_inputs, code_item->ins_size_);
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001218 } else {
Ian Rogerse94652f2014-12-02 11:13:19 -08001219 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
Igor Murashkin158f35c2015-06-10 15:55:30 -07001220 num_regs = number_of_inputs;
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001221 }
1222
Igor Murashkin158f35c2015-06-10 15:55:30 -07001223 // Hack for String init:
1224 //
1225 // Rewrite invoke-x java.lang.String.<init>(this, a, b, c, ...) into:
1226 // invoke-x StringFactory(a, b, c, ...)
1227 // by effectively dropping the first virtual register from the invoke.
1228 //
1229 // (at this point the ArtMethod has already been replaced,
1230 // so we just need to fix-up the arguments)
David Brazdil65902e82016-01-15 09:35:13 +00001231 //
1232 // Note that FindMethodFromCode in entrypoint_utils-inl.h was also special-cased
1233 // to handle the compiler optimization of replacing `this` with null without
1234 // throwing NullPointerException.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001235 uint32_t string_init_vreg_this = is_range ? vregC : arg[0];
Igor Murashkina06b49b2015-06-25 15:18:12 -07001236 if (UNLIKELY(string_init)) {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001237 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 -07001238
Igor Murashkin158f35c2015-06-10 15:55:30 -07001239 // The new StringFactory call is static and has one fewer argument.
Igor Murashkina06b49b2015-06-25 15:18:12 -07001240 if (code_item == nullptr) {
1241 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
1242 num_regs--;
1243 } // 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 -07001244 number_of_inputs--;
1245
1246 // Rewrite the var-args, dropping the 0th argument ("this")
Igor Murashkin6918bf12015-09-27 19:19:06 -07001247 for (uint32_t i = 1; i < arraysize(arg); ++i) {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001248 arg[i - 1] = arg[i];
1249 }
Igor Murashkin6918bf12015-09-27 19:19:06 -07001250 arg[arraysize(arg) - 1] = 0;
Igor Murashkin158f35c2015-06-10 15:55:30 -07001251
1252 // Rewrite the non-var-arg case
1253 vregC++; // Skips the 0th vreg in the range ("this").
1254 }
1255
1256 // Parameter registers go at the end of the shadow frame.
1257 DCHECK_GE(num_regs, number_of_inputs);
1258 size_t first_dest_reg = num_regs - number_of_inputs;
1259 DCHECK_NE(first_dest_reg, (size_t)-1);
1260
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001261 // Allocate shadow frame on the stack.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001262 const char* old_cause = self->StartAssertNoThreadSuspension("DoCallCommon");
Andreas Gampeb3025922015-09-01 14:45:00 -07001263 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
Andreas Gampe03ec9302015-08-27 17:41:47 -07001264 CREATE_SHADOW_FRAME(num_regs, &shadow_frame, called_method, /* dex pc */ 0);
Andreas Gampeb3025922015-09-01 14:45:00 -07001265 ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get();
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001266
Igor Murashkin158f35c2015-06-10 15:55:30 -07001267 // Initialize new shadow frame by copying the registers from the callee shadow frame.
Jeff Haoa3faaf42013-09-03 19:07:00 -07001268 if (do_assignability_check) {
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001269 // Slow path.
1270 // We might need to do class loading, which incurs a thread state change to kNative. So
1271 // register the shadow frame as under construction and allow suspension again.
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -07001272 ScopedStackedShadowFramePusher pusher(
Sebastien Hertzf7958692015-06-09 14:09:14 +02001273 self, new_shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001274 self->EndAssertNoThreadSuspension(old_cause);
1275
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001276 // ArtMethod here is needed to check type information of the call site against the callee.
1277 // Type information is retrieved from a DexFile/DexCache for that respective declared method.
1278 //
1279 // As a special case for proxy methods, which are not dex-backed,
1280 // we have to retrieve type information from the proxy's method
1281 // interface method instead (which is dex backed since proxies are never interfaces).
Andreas Gampe542451c2016-07-26 09:02:02 -07001282 ArtMethod* method =
1283 new_shadow_frame->GetMethod()->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001284
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001285 // We need to do runtime check on reference assignment. We need to load the shorty
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001286 // to get the exact type of each reference argument.
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001287 const DexFile::TypeList* params = method->GetParameterTypeList();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001288 uint32_t shorty_len = 0;
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001289 const char* shorty = method->GetShorty(&shorty_len);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001290
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001291 // Handle receiver apart since it's not part of the shorty.
1292 size_t dest_reg = first_dest_reg;
1293 size_t arg_offset = 0;
Igor Murashkin158f35c2015-06-10 15:55:30 -07001294
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001295 if (!method->IsStatic()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001296 size_t receiver_reg = is_range ? vregC : arg[0];
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001297 new_shadow_frame->SetVRegReference(dest_reg, shadow_frame.GetVRegReference(receiver_reg));
1298 ++dest_reg;
1299 ++arg_offset;
Igor Murashkina06b49b2015-06-25 15:18:12 -07001300 DCHECK(!string_init); // All StringFactory methods are static.
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001301 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001302
1303 // Copy the caller's invoke-* arguments into the callee's parameter registers.
Ian Rogersef7d42f2014-01-06 12:55:46 -08001304 for (uint32_t shorty_pos = 0; dest_reg < num_regs; ++shorty_pos, ++dest_reg, ++arg_offset) {
Igor Murashkina06b49b2015-06-25 15:18:12 -07001305 // Skip the 0th 'shorty' type since it represents the return type.
1306 DCHECK_LT(shorty_pos + 1, shorty_len) << "for shorty '" << shorty << "'";
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001307 const size_t src_reg = (is_range) ? vregC + arg_offset : arg[arg_offset];
1308 switch (shorty[shorty_pos + 1]) {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001309 // Handle Object references. 1 virtual register slot.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001310 case 'L': {
Mathieu Chartieref41db72016-10-25 15:08:01 -07001311 ObjPtr<mirror::Object> o = shadow_frame.GetVRegReference(src_reg);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001312 if (do_assignability_check && o != nullptr) {
Andreas Gampe542451c2016-07-26 09:02:02 -07001313 PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Mathieu Chartiere22305b2016-10-26 21:04:58 -07001314 const uint32_t type_idx = params->GetTypeItem(shorty_pos).type_idx_;
1315 ObjPtr<mirror::Class> arg_type = method->GetDexCacheResolvedType(type_idx,
1316 pointer_size);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001317 if (arg_type == nullptr) {
Mathieu Chartiere22305b2016-10-26 21:04:58 -07001318 StackHandleScope<1> hs(self);
1319 // Preserve o since it is used below and GetClassFromTypeIndex may cause thread
1320 // suspension.
1321 HandleWrapperObjPtr<mirror::Object> h = hs.NewHandleWrapper(&o);
1322 arg_type = method->GetClassFromTypeIndex(type_idx, true /* resolve */, pointer_size);
1323 if (arg_type == nullptr) {
1324 CHECK(self->IsExceptionPending());
1325 return false;
1326 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001327 }
1328 if (!o->VerifierInstanceOf(arg_type)) {
1329 // This should never happen.
Ian Rogers1ff3c982014-08-12 02:30:58 -07001330 std::string temp1, temp2;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +00001331 self->ThrowNewExceptionF("Ljava/lang/VirtualMachineError;",
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001332 "Invoking %s with bad arg %d, type '%s' not instance of '%s'",
Ian Rogerse94652f2014-12-02 11:13:19 -08001333 new_shadow_frame->GetMethod()->GetName(), shorty_pos,
Ian Rogers1ff3c982014-08-12 02:30:58 -07001334 o->GetClass()->GetDescriptor(&temp1),
1335 arg_type->GetDescriptor(&temp2));
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001336 return false;
1337 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07001338 }
Mathieu Chartieref41db72016-10-25 15:08:01 -07001339 new_shadow_frame->SetVRegReference(dest_reg, o.Ptr());
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001340 break;
Jeff Haoa3faaf42013-09-03 19:07:00 -07001341 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001342 // Handle doubles and longs. 2 consecutive virtual register slots.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001343 case 'J': case 'D': {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001344 uint64_t wide_value =
1345 (static_cast<uint64_t>(shadow_frame.GetVReg(src_reg + 1)) << BitSizeOf<uint32_t>()) |
1346 static_cast<uint32_t>(shadow_frame.GetVReg(src_reg));
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001347 new_shadow_frame->SetVRegLong(dest_reg, wide_value);
Igor Murashkin158f35c2015-06-10 15:55:30 -07001348 // Skip the next virtual register slot since we already used it.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001349 ++dest_reg;
1350 ++arg_offset;
1351 break;
1352 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001353 // Handle all other primitives that are always 1 virtual register slot.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001354 default:
1355 new_shadow_frame->SetVReg(dest_reg, shadow_frame.GetVReg(src_reg));
1356 break;
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001357 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001358 }
1359 } else {
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001360 if (is_range) {
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001361 DCHECK_EQ(num_regs, first_dest_reg + number_of_inputs);
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001362 }
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001363
1364 CopyRegisters<is_range>(shadow_frame,
1365 new_shadow_frame,
1366 arg,
1367 vregC,
1368 first_dest_reg,
1369 number_of_inputs);
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001370 self->EndAssertNoThreadSuspension(old_cause);
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001371 }
1372
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001373 PerformCall(self, code_item, shadow_frame.GetMethod(), first_dest_reg, new_shadow_frame, result);
Jeff Hao848f70a2014-01-15 13:49:50 -08001374
1375 if (string_init && !self->IsExceptionPending()) {
Mingyao Yangffedec52016-05-19 10:48:40 -07001376 SetStringInitValueToAllAliases(&shadow_frame, string_init_vreg_this, *result);
Jeff Hao848f70a2014-01-15 13:49:50 -08001377 }
1378
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001379 return !self->IsExceptionPending();
1380}
1381
Igor Murashkin158f35c2015-06-10 15:55:30 -07001382template<bool is_range, bool do_assignability_check>
Igor Murashkin158f35c2015-06-10 15:55:30 -07001383bool DoCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame,
1384 const Instruction* inst, uint16_t inst_data, JValue* result) {
1385 // Argument word count.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001386 const uint16_t number_of_inputs =
1387 (is_range) ? inst->VRegA_3rc(inst_data) : inst->VRegA_35c(inst_data);
Igor Murashkin158f35c2015-06-10 15:55:30 -07001388
1389 // TODO: find a cleaner way to separate non-range and range information without duplicating
1390 // code.
Igor Murashkin6918bf12015-09-27 19:19:06 -07001391 uint32_t arg[Instruction::kMaxVarArgRegs] = {}; // only used in invoke-XXX.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001392 uint32_t vregC = 0;
1393 if (is_range) {
1394 vregC = inst->VRegC_3rc();
1395 } else {
1396 vregC = inst->VRegC_35c();
1397 inst->GetVarArgs(arg, inst_data);
1398 }
1399
1400 return DoCallCommon<is_range, do_assignability_check>(
1401 called_method, self, shadow_frame,
1402 result, number_of_inputs, arg, vregC);
1403}
1404
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001405template <bool is_range, bool do_access_check, bool transaction_active>
Mathieu Chartieref41db72016-10-25 15:08:01 -07001406bool DoFilledNewArray(const Instruction* inst,
1407 const ShadowFrame& shadow_frame,
1408 Thread* self,
1409 JValue* result) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001410 DCHECK(inst->Opcode() == Instruction::FILLED_NEW_ARRAY ||
1411 inst->Opcode() == Instruction::FILLED_NEW_ARRAY_RANGE);
1412 const int32_t length = is_range ? inst->VRegA_3rc() : inst->VRegA_35c();
1413 if (!is_range) {
1414 // Checks FILLED_NEW_ARRAY's length does not exceed 5 arguments.
1415 CHECK_LE(length, 5);
1416 }
1417 if (UNLIKELY(length < 0)) {
1418 ThrowNegativeArraySizeException(length);
1419 return false;
1420 }
1421 uint16_t type_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mathieu Chartieref41db72016-10-25 15:08:01 -07001422 ObjPtr<mirror::Class> array_class = ResolveVerifyAndClinit(type_idx,
1423 shadow_frame.GetMethod(),
1424 self,
1425 false,
1426 do_access_check);
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001427 if (UNLIKELY(array_class == nullptr)) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001428 DCHECK(self->IsExceptionPending());
1429 return false;
1430 }
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001431 CHECK(array_class->IsArrayClass());
Mathieu Chartieref41db72016-10-25 15:08:01 -07001432 ObjPtr<mirror::Class> component_class = array_class->GetComponentType();
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001433 const bool is_primitive_int_component = component_class->IsPrimitiveInt();
1434 if (UNLIKELY(component_class->IsPrimitive() && !is_primitive_int_component)) {
1435 if (component_class->IsPrimitiveLong() || component_class->IsPrimitiveDouble()) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001436 ThrowRuntimeException("Bad filled array request for type %s",
David Sehr709b0702016-10-13 09:12:37 -07001437 component_class->PrettyDescriptor().c_str());
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001438 } else {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +00001439 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
Brian Carlstrom4fa0bcd2013-12-10 11:24:21 -08001440 "Found type %s; filled-new-array not implemented for anything but 'int'",
David Sehr709b0702016-10-13 09:12:37 -07001441 component_class->PrettyDescriptor().c_str());
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001442 }
1443 return false;
1444 }
Mathieu Chartieref41db72016-10-25 15:08:01 -07001445 ObjPtr<mirror::Object> new_array = mirror::Array::Alloc<true>(
1446 self,
1447 array_class,
1448 length,
1449 array_class->GetComponentSizeShift(),
1450 Runtime::Current()->GetHeap()->GetCurrentAllocator());
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001451 if (UNLIKELY(new_array == nullptr)) {
1452 self->AssertPendingOOMException();
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001453 return false;
1454 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001455 uint32_t arg[Instruction::kMaxVarArgRegs]; // only used in filled-new-array.
1456 uint32_t vregC = 0; // only used in filled-new-array-range.
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001457 if (is_range) {
Sebastien Hertzabff6432014-01-27 18:01:39 +01001458 vregC = inst->VRegC_3rc();
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001459 } else {
Ian Rogers29a26482014-05-02 15:27:29 -07001460 inst->GetVarArgs(arg);
Sebastien Hertzabff6432014-01-27 18:01:39 +01001461 }
Sebastien Hertzabff6432014-01-27 18:01:39 +01001462 for (int32_t i = 0; i < length; ++i) {
1463 size_t src_reg = is_range ? vregC + i : arg[i];
1464 if (is_primitive_int_component) {
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001465 new_array->AsIntArray()->SetWithoutChecks<transaction_active>(
1466 i, shadow_frame.GetVReg(src_reg));
Sebastien Hertzabff6432014-01-27 18:01:39 +01001467 } else {
Mathieu Chartieref41db72016-10-25 15:08:01 -07001468 new_array->AsObjectArray<mirror::Object>()->SetWithoutChecks<transaction_active>(
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001469 i, shadow_frame.GetVRegReference(src_reg));
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001470 }
1471 }
1472
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001473 result->SetL(new_array);
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001474 return true;
1475}
1476
Mathieu Chartieref41db72016-10-25 15:08:01 -07001477// TODO: Use ObjPtr here.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001478template<typename T>
Mathieu Chartieref41db72016-10-25 15:08:01 -07001479static void RecordArrayElementsInTransactionImpl(mirror::PrimitiveArray<T>* array,
1480 int32_t count)
1481 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001482 Runtime* runtime = Runtime::Current();
1483 for (int32_t i = 0; i < count; ++i) {
1484 runtime->RecordWriteArray(array, i, array->GetWithoutChecks(i));
1485 }
1486}
1487
Mathieu Chartieref41db72016-10-25 15:08:01 -07001488void RecordArrayElementsInTransaction(ObjPtr<mirror::Array> array, int32_t count)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001489 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001490 DCHECK(Runtime::Current()->IsActiveTransaction());
1491 DCHECK(array != nullptr);
1492 DCHECK_LE(count, array->GetLength());
1493 Primitive::Type primitive_component_type = array->GetClass()->GetComponentType()->GetPrimitiveType();
1494 switch (primitive_component_type) {
1495 case Primitive::kPrimBoolean:
1496 RecordArrayElementsInTransactionImpl(array->AsBooleanArray(), count);
1497 break;
1498 case Primitive::kPrimByte:
1499 RecordArrayElementsInTransactionImpl(array->AsByteArray(), count);
1500 break;
1501 case Primitive::kPrimChar:
1502 RecordArrayElementsInTransactionImpl(array->AsCharArray(), count);
1503 break;
1504 case Primitive::kPrimShort:
1505 RecordArrayElementsInTransactionImpl(array->AsShortArray(), count);
1506 break;
1507 case Primitive::kPrimInt:
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001508 RecordArrayElementsInTransactionImpl(array->AsIntArray(), count);
1509 break;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001510 case Primitive::kPrimFloat:
1511 RecordArrayElementsInTransactionImpl(array->AsFloatArray(), count);
1512 break;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001513 case Primitive::kPrimLong:
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001514 RecordArrayElementsInTransactionImpl(array->AsLongArray(), count);
1515 break;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001516 case Primitive::kPrimDouble:
1517 RecordArrayElementsInTransactionImpl(array->AsDoubleArray(), count);
1518 break;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001519 default:
1520 LOG(FATAL) << "Unsupported primitive type " << primitive_component_type
1521 << " in fill-array-data";
1522 break;
1523 }
1524}
1525
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001526// Explicit DoCall template function declarations.
Sebastien Hertzc6714852013-09-30 16:42:32 +02001527#define EXPLICIT_DO_CALL_TEMPLATE_DECL(_is_range, _do_assignability_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001528 template REQUIRES_SHARED(Locks::mutator_lock_) \
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001529 bool DoCall<_is_range, _do_assignability_check>(ArtMethod* method, Thread* self, \
1530 ShadowFrame& shadow_frame, \
Sebastien Hertzc6714852013-09-30 16:42:32 +02001531 const Instruction* inst, uint16_t inst_data, \
1532 JValue* result)
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001533EXPLICIT_DO_CALL_TEMPLATE_DECL(false, false);
1534EXPLICIT_DO_CALL_TEMPLATE_DECL(false, true);
1535EXPLICIT_DO_CALL_TEMPLATE_DECL(true, false);
1536EXPLICIT_DO_CALL_TEMPLATE_DECL(true, true);
1537#undef EXPLICIT_DO_CALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001538
Narayan Kamath9823e782016-08-03 12:46:58 +01001539// Explicit DoInvokePolymorphic template function declarations.
1540#define EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(_is_range, _do_assignability_check) \
1541 template REQUIRES_SHARED(Locks::mutator_lock_) \
1542 bool DoInvokePolymorphic<_is_range, _do_assignability_check>( \
1543 Thread* self, ShadowFrame& shadow_frame, const Instruction* inst, \
1544 uint16_t inst_data, JValue* result)
1545
1546EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(false, false);
1547EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(false, true);
1548EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(true, false);
1549EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(true, true);
1550#undef EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL
1551
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001552// Explicit DoFilledNewArray template function declarations.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001553#define EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(_is_range_, _check, _transaction_active) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001554 template REQUIRES_SHARED(Locks::mutator_lock_) \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001555 bool DoFilledNewArray<_is_range_, _check, _transaction_active>(const Instruction* inst, \
1556 const ShadowFrame& shadow_frame, \
1557 Thread* self, JValue* result)
1558#define EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(_transaction_active) \
1559 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, false, _transaction_active); \
1560 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, true, _transaction_active); \
1561 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, false, _transaction_active); \
1562 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, true, _transaction_active)
1563EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(false);
1564EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(true);
1565#undef EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001566#undef EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL
1567
1568} // namespace interpreter
1569} // namespace art