blob: 09d11678f2cf5a1577f514c91e037c47ac2b41b5 [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"
Sebastien Hertz8ece0502013-08-07 11:26:41 +020036
37namespace art {
38namespace interpreter {
39
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000040void ThrowNullPointerExceptionFromInterpreter() {
41 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -070042}
43
44template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check>
45bool DoFieldGet(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst,
46 uint16_t inst_data) {
47 const bool is_static = (find_type == StaticObjectRead) || (find_type == StaticPrimitiveRead);
48 const uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Roland Levillain4b8f1ec2015-08-26 18:34:03 +010049 ArtField* f =
50 FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
51 Primitive::ComponentSize(field_type));
Ian Rogers54874942014-06-10 16:31:03 -070052 if (UNLIKELY(f == nullptr)) {
53 CHECK(self->IsExceptionPending());
54 return false;
55 }
Mathieu Chartier3398c782016-09-30 10:27:43 -070056 ObjPtr<Object> obj;
Ian Rogers54874942014-06-10 16:31:03 -070057 if (is_static) {
58 obj = f->GetDeclaringClass();
59 } else {
60 obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
61 if (UNLIKELY(obj == nullptr)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000062 ThrowNullPointerExceptionForFieldAccess(f, true);
Ian Rogers54874942014-06-10 16:31:03 -070063 return false;
64 }
65 }
Sebastien Hertz1edbd8e2014-07-16 20:00:11 +020066 f->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self);
Ian Rogers54874942014-06-10 16:31:03 -070067 // Report this field access to instrumentation if needed.
68 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
69 if (UNLIKELY(instrumentation->HasFieldReadListeners())) {
Mathieu Chartier0715c0b2016-10-03 22:49:46 -070070 StackHandleScope<1> hs(self);
71 // Wrap in handle wrapper in case the listener does thread suspension.
72 HandleWrapperObjPtr<mirror::Object> h(hs.NewHandleWrapper(&obj));
Mathieu Chartier3398c782016-09-30 10:27:43 -070073 ObjPtr<Object> this_object;
74 if (!f->IsStatic()) {
75 this_object = obj;
76 }
77 instrumentation->FieldReadEvent(self,
Mathieu Chartier1cc62e42016-10-03 18:01:28 -070078 this_object.Ptr(),
Mathieu Chartier3398c782016-09-30 10:27:43 -070079 shadow_frame.GetMethod(),
80 shadow_frame.GetDexPC(),
81 f);
Ian Rogers54874942014-06-10 16:31:03 -070082 }
83 uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
84 switch (field_type) {
85 case Primitive::kPrimBoolean:
86 shadow_frame.SetVReg(vregA, f->GetBoolean(obj));
87 break;
88 case Primitive::kPrimByte:
89 shadow_frame.SetVReg(vregA, f->GetByte(obj));
90 break;
91 case Primitive::kPrimChar:
92 shadow_frame.SetVReg(vregA, f->GetChar(obj));
93 break;
94 case Primitive::kPrimShort:
95 shadow_frame.SetVReg(vregA, f->GetShort(obj));
96 break;
97 case Primitive::kPrimInt:
98 shadow_frame.SetVReg(vregA, f->GetInt(obj));
99 break;
100 case Primitive::kPrimLong:
101 shadow_frame.SetVRegLong(vregA, f->GetLong(obj));
102 break;
103 case Primitive::kPrimNot:
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700104 shadow_frame.SetVRegReference(vregA, f->GetObject(obj).Ptr());
Ian Rogers54874942014-06-10 16:31:03 -0700105 break;
106 default:
107 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700108 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700109 }
110 return true;
111}
112
113// Explicitly instantiate all DoFieldGet functions.
114#define EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, _do_check) \
115 template bool DoFieldGet<_find_type, _field_type, _do_check>(Thread* self, \
116 ShadowFrame& shadow_frame, \
117 const Instruction* inst, \
118 uint16_t inst_data)
119
120#define EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(_find_type, _field_type) \
121 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, false); \
122 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, true);
123
124// iget-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700125EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimBoolean)
126EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimByte)
127EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimChar)
128EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimShort)
129EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimInt)
130EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimLong)
131EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstanceObjectRead, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700132
133// sget-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700134EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimBoolean)
135EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimByte)
136EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimChar)
137EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimShort)
138EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimInt)
139EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimLong)
140EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticObjectRead, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700141
142#undef EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL
143#undef EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL
144
145// Handles iget-quick, iget-wide-quick and iget-object-quick instructions.
146// Returns true on success, otherwise throws an exception and returns false.
147template<Primitive::Type field_type>
148bool DoIGetQuick(ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
149 Object* obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
150 if (UNLIKELY(obj == nullptr)) {
151 // We lost the reference to the field index so we cannot get a more
152 // precised exception message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000153 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -0700154 return false;
155 }
156 MemberOffset field_offset(inst->VRegC_22c());
157 // Report this field access to instrumentation if needed. Since we only have the offset of
158 // the field from the base of the object, we need to look for it first.
159 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
160 if (UNLIKELY(instrumentation->HasFieldReadListeners())) {
161 ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(),
162 field_offset.Uint32Value());
163 DCHECK(f != nullptr);
164 DCHECK(!f->IsStatic());
165 instrumentation->FieldReadEvent(Thread::Current(), obj, shadow_frame.GetMethod(),
166 shadow_frame.GetDexPC(), f);
167 }
168 // Note: iget-x-quick instructions are only for non-volatile fields.
169 const uint32_t vregA = inst->VRegA_22c(inst_data);
170 switch (field_type) {
171 case Primitive::kPrimInt:
172 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetField32(field_offset)));
173 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800174 case Primitive::kPrimBoolean:
175 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldBoolean(field_offset)));
176 break;
177 case Primitive::kPrimByte:
178 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldByte(field_offset)));
179 break;
180 case Primitive::kPrimChar:
181 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldChar(field_offset)));
182 break;
183 case Primitive::kPrimShort:
184 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldShort(field_offset)));
185 break;
Ian Rogers54874942014-06-10 16:31:03 -0700186 case Primitive::kPrimLong:
187 shadow_frame.SetVRegLong(vregA, static_cast<int64_t>(obj->GetField64(field_offset)));
188 break;
189 case Primitive::kPrimNot:
190 shadow_frame.SetVRegReference(vregA, obj->GetFieldObject<mirror::Object>(field_offset));
191 break;
192 default:
193 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700194 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700195 }
196 return true;
197}
198
199// Explicitly instantiate all DoIGetQuick functions.
200#define EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(_field_type) \
201 template bool DoIGetQuick<_field_type>(ShadowFrame& shadow_frame, const Instruction* inst, \
202 uint16_t inst_data)
203
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800204EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimInt); // iget-quick.
205EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimBoolean); // iget-boolean-quick.
206EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimByte); // iget-byte-quick.
207EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimChar); // iget-char-quick.
208EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimShort); // iget-short-quick.
209EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimLong); // iget-wide-quick.
210EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimNot); // iget-object-quick.
Ian Rogers54874942014-06-10 16:31:03 -0700211#undef EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL
212
213template<Primitive::Type field_type>
214static JValue GetFieldValue(const ShadowFrame& shadow_frame, uint32_t vreg)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700215 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers54874942014-06-10 16:31:03 -0700216 JValue field_value;
217 switch (field_type) {
218 case Primitive::kPrimBoolean:
219 field_value.SetZ(static_cast<uint8_t>(shadow_frame.GetVReg(vreg)));
220 break;
221 case Primitive::kPrimByte:
222 field_value.SetB(static_cast<int8_t>(shadow_frame.GetVReg(vreg)));
223 break;
224 case Primitive::kPrimChar:
225 field_value.SetC(static_cast<uint16_t>(shadow_frame.GetVReg(vreg)));
226 break;
227 case Primitive::kPrimShort:
228 field_value.SetS(static_cast<int16_t>(shadow_frame.GetVReg(vreg)));
229 break;
230 case Primitive::kPrimInt:
231 field_value.SetI(shadow_frame.GetVReg(vreg));
232 break;
233 case Primitive::kPrimLong:
234 field_value.SetJ(shadow_frame.GetVRegLong(vreg));
235 break;
236 case Primitive::kPrimNot:
237 field_value.SetL(shadow_frame.GetVRegReference(vreg));
238 break;
239 default:
240 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700241 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700242 }
243 return field_value;
244}
245
246template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check,
247 bool transaction_active>
248bool DoFieldPut(Thread* self, const ShadowFrame& shadow_frame, const Instruction* inst,
249 uint16_t inst_data) {
250 bool do_assignability_check = do_access_check;
251 bool is_static = (find_type == StaticObjectWrite) || (find_type == StaticPrimitiveWrite);
252 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Roland Levillain4b8f1ec2015-08-26 18:34:03 +0100253 ArtField* f =
254 FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
255 Primitive::ComponentSize(field_type));
Ian Rogers54874942014-06-10 16:31:03 -0700256 if (UNLIKELY(f == nullptr)) {
257 CHECK(self->IsExceptionPending());
258 return false;
259 }
Mathieu Chartier3398c782016-09-30 10:27:43 -0700260 ObjPtr<Object> obj;
Ian Rogers54874942014-06-10 16:31:03 -0700261 if (is_static) {
262 obj = f->GetDeclaringClass();
263 } else {
264 obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
265 if (UNLIKELY(obj == nullptr)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000266 ThrowNullPointerExceptionForFieldAccess(f, false);
Ian Rogers54874942014-06-10 16:31:03 -0700267 return false;
268 }
269 }
Sebastien Hertz1edbd8e2014-07-16 20:00:11 +0200270 f->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self);
Ian Rogers54874942014-06-10 16:31:03 -0700271 uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
272 // Report this field access to instrumentation if needed. Since we only have the offset of
273 // the field from the base of the object, we need to look for it first.
274 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
275 if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
Mathieu Chartier0715c0b2016-10-03 22:49:46 -0700276 StackHandleScope<1> hs(self);
277 // Wrap in handle wrapper in case the listener does thread suspension.
278 HandleWrapperObjPtr<mirror::Object> h(hs.NewHandleWrapper(&obj));
Ian Rogers54874942014-06-10 16:31:03 -0700279 JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA);
Mathieu Chartier3398c782016-09-30 10:27:43 -0700280 ObjPtr<Object> this_object = f->IsStatic() ? nullptr : obj;
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700281 instrumentation->FieldWriteEvent(self, this_object.Ptr(),
Mathieu Chartier3398c782016-09-30 10:27:43 -0700282 shadow_frame.GetMethod(),
283 shadow_frame.GetDexPC(),
284 f,
285 field_value);
Ian Rogers54874942014-06-10 16:31:03 -0700286 }
287 switch (field_type) {
288 case Primitive::kPrimBoolean:
289 f->SetBoolean<transaction_active>(obj, shadow_frame.GetVReg(vregA));
290 break;
291 case Primitive::kPrimByte:
292 f->SetByte<transaction_active>(obj, shadow_frame.GetVReg(vregA));
293 break;
294 case Primitive::kPrimChar:
295 f->SetChar<transaction_active>(obj, shadow_frame.GetVReg(vregA));
296 break;
297 case Primitive::kPrimShort:
298 f->SetShort<transaction_active>(obj, shadow_frame.GetVReg(vregA));
299 break;
300 case Primitive::kPrimInt:
301 f->SetInt<transaction_active>(obj, shadow_frame.GetVReg(vregA));
302 break;
303 case Primitive::kPrimLong:
304 f->SetLong<transaction_active>(obj, shadow_frame.GetVRegLong(vregA));
305 break;
306 case Primitive::kPrimNot: {
307 Object* reg = shadow_frame.GetVRegReference(vregA);
308 if (do_assignability_check && reg != nullptr) {
309 // FieldHelper::GetType can resolve classes, use a handle wrapper which will restore the
310 // object in the destructor.
Mathieu Chartier3398c782016-09-30 10:27:43 -0700311 ObjPtr<Class> field_class;
Ian Rogers54874942014-06-10 16:31:03 -0700312 {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700313 StackHandleScope<2> hs(self);
Ian Rogers54874942014-06-10 16:31:03 -0700314 HandleWrapper<mirror::Object> h_reg(hs.NewHandleWrapper(&reg));
Mathieu Chartier3398c782016-09-30 10:27:43 -0700315 HandleWrapperObjPtr<mirror::Object> h_obj(hs.NewHandleWrapper(&obj));
Mathieu Chartierc7853442015-03-27 14:35:38 -0700316 field_class = f->GetType<true>();
Ian Rogers54874942014-06-10 16:31:03 -0700317 }
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700318 if (!reg->VerifierInstanceOf(field_class.Ptr())) {
Ian Rogers54874942014-06-10 16:31:03 -0700319 // This should never happen.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700320 std::string temp1, temp2, temp3;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000321 self->ThrowNewExceptionF("Ljava/lang/VirtualMachineError;",
Ian Rogers54874942014-06-10 16:31:03 -0700322 "Put '%s' that is not instance of field '%s' in '%s'",
Ian Rogers1ff3c982014-08-12 02:30:58 -0700323 reg->GetClass()->GetDescriptor(&temp1),
324 field_class->GetDescriptor(&temp2),
325 f->GetDeclaringClass()->GetDescriptor(&temp3));
Ian Rogers54874942014-06-10 16:31:03 -0700326 return false;
327 }
328 }
329 f->SetObj<transaction_active>(obj, reg);
330 break;
331 }
332 default:
333 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700334 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700335 }
336 return true;
337}
338
339// Explicitly instantiate all DoFieldPut functions.
340#define EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, _do_check, _transaction_active) \
341 template bool DoFieldPut<_find_type, _field_type, _do_check, _transaction_active>(Thread* self, \
342 const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data)
343
344#define EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(_find_type, _field_type) \
345 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, false); \
346 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, false); \
347 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, true); \
348 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, true);
349
350// iput-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700351EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimBoolean)
352EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimByte)
353EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimChar)
354EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimShort)
355EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimInt)
356EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimLong)
357EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstanceObjectWrite, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700358
359// sput-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700360EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimBoolean)
361EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimByte)
362EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimChar)
363EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimShort)
364EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimInt)
365EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimLong)
366EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticObjectWrite, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700367
368#undef EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL
369#undef EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL
370
371template<Primitive::Type field_type, bool transaction_active>
372bool DoIPutQuick(const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
373 Object* obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
374 if (UNLIKELY(obj == nullptr)) {
375 // We lost the reference to the field index so we cannot get a more
376 // precised exception message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000377 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -0700378 return false;
379 }
380 MemberOffset field_offset(inst->VRegC_22c());
381 const uint32_t vregA = inst->VRegA_22c(inst_data);
382 // Report this field modification to instrumentation if needed. Since we only have the offset of
383 // the field from the base of the object, we need to look for it first.
384 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
385 if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
386 ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(),
387 field_offset.Uint32Value());
388 DCHECK(f != nullptr);
389 DCHECK(!f->IsStatic());
390 JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA);
391 instrumentation->FieldWriteEvent(Thread::Current(), obj, shadow_frame.GetMethod(),
392 shadow_frame.GetDexPC(), f, field_value);
393 }
394 // Note: iput-x-quick instructions are only for non-volatile fields.
395 switch (field_type) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700396 case Primitive::kPrimBoolean:
397 obj->SetFieldBoolean<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
398 break;
399 case Primitive::kPrimByte:
400 obj->SetFieldByte<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
401 break;
402 case Primitive::kPrimChar:
403 obj->SetFieldChar<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
404 break;
405 case Primitive::kPrimShort:
406 obj->SetFieldShort<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
407 break;
Ian Rogers54874942014-06-10 16:31:03 -0700408 case Primitive::kPrimInt:
409 obj->SetField32<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
410 break;
411 case Primitive::kPrimLong:
412 obj->SetField64<transaction_active>(field_offset, shadow_frame.GetVRegLong(vregA));
413 break;
414 case Primitive::kPrimNot:
415 obj->SetFieldObject<transaction_active>(field_offset, shadow_frame.GetVRegReference(vregA));
416 break;
417 default:
418 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700419 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700420 }
421 return true;
422}
423
424// Explicitly instantiate all DoIPutQuick functions.
425#define EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, _transaction_active) \
426 template bool DoIPutQuick<_field_type, _transaction_active>(const ShadowFrame& shadow_frame, \
427 const Instruction* inst, \
428 uint16_t inst_data)
429
430#define EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(_field_type) \
431 EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, false); \
432 EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, true);
433
Andreas Gampec8ccf682014-09-29 20:07:43 -0700434EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimInt) // iput-quick.
435EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimBoolean) // iput-boolean-quick.
436EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimByte) // iput-byte-quick.
437EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimChar) // iput-char-quick.
438EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimShort) // iput-short-quick.
439EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimLong) // iput-wide-quick.
440EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimNot) // iput-object-quick.
Ian Rogers54874942014-06-10 16:31:03 -0700441#undef EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL
442#undef EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL
443
Sebastien Hertz520633b2015-09-08 17:03:36 +0200444// We accept a null Instrumentation* meaning we must not report anything to the instrumentation.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700445uint32_t FindNextInstructionFollowingException(
446 Thread* self, ShadowFrame& shadow_frame, uint32_t dex_pc,
447 const instrumentation::Instrumentation* instrumentation) {
Ian Rogers54874942014-06-10 16:31:03 -0700448 self->VerifyStack();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700449 StackHandleScope<2> hs(self);
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000450 Handle<mirror::Throwable> exception(hs.NewHandle(self->GetException()));
Sebastien Hertz520633b2015-09-08 17:03:36 +0200451 if (instrumentation != nullptr && instrumentation->HasExceptionCaughtListeners()
Nicolas Geoffray7642cfc2015-02-26 10:56:09 +0000452 && self->IsExceptionThrownByCurrentMethod(exception.Get())) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000453 instrumentation->ExceptionCaughtEvent(self, exception.Get());
Sebastien Hertz9f102032014-05-23 08:59:42 +0200454 }
Ian Rogers54874942014-06-10 16:31:03 -0700455 bool clear_exception = false;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700456 uint32_t found_dex_pc = shadow_frame.GetMethod()->FindCatchBlock(
457 hs.NewHandle(exception->GetClass()), dex_pc, &clear_exception);
Sebastien Hertz520633b2015-09-08 17:03:36 +0200458 if (found_dex_pc == DexFile::kDexNoIndex && instrumentation != nullptr) {
Nicolas Geoffray7642cfc2015-02-26 10:56:09 +0000459 // Exception is not caught by the current method. We will unwind to the
460 // caller. Notify any instrumentation listener.
Sebastien Hertz9f102032014-05-23 08:59:42 +0200461 instrumentation->MethodUnwindEvent(self, shadow_frame.GetThisObject(),
Ian Rogers54874942014-06-10 16:31:03 -0700462 shadow_frame.GetMethod(), dex_pc);
463 } else {
Nicolas Geoffray7642cfc2015-02-26 10:56:09 +0000464 // Exception is caught in the current method. We will jump to the found_dex_pc.
Ian Rogers54874942014-06-10 16:31:03 -0700465 if (clear_exception) {
466 self->ClearException();
467 }
468 }
469 return found_dex_pc;
470}
471
Ian Rogerse94652f2014-12-02 11:13:19 -0800472void UnexpectedOpcode(const Instruction* inst, const ShadowFrame& shadow_frame) {
473 LOG(FATAL) << "Unexpected instruction: "
474 << inst->DumpString(shadow_frame.GetMethod()->GetDexFile());
475 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700476}
477
Sebastien Hertz45b15972015-04-03 16:07:05 +0200478void AbortTransactionF(Thread* self, const char* fmt, ...) {
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700479 va_list args;
480 va_start(args, fmt);
Sebastien Hertz45b15972015-04-03 16:07:05 +0200481 AbortTransactionV(self, fmt, args);
482 va_end(args);
483}
484
485void AbortTransactionV(Thread* self, const char* fmt, va_list args) {
486 CHECK(Runtime::Current()->IsActiveTransaction());
487 // Constructs abort message.
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100488 std::string abort_msg;
489 StringAppendV(&abort_msg, fmt, args);
490 // Throws an exception so we can abort the transaction and rollback every change.
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200491 Runtime::Current()->AbortTransactionAndThrowAbortError(self, abort_msg);
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700492}
493
Igor Murashkin158f35c2015-06-10 15:55:30 -0700494// Separate declaration is required solely for the attributes.
Narayan Kamath9823e782016-08-03 12:46:58 +0100495template <bool is_range, bool do_assignability_check>
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700496 REQUIRES_SHARED(Locks::mutator_lock_)
Igor Murashkin158f35c2015-06-10 15:55:30 -0700497static inline bool DoCallCommon(ArtMethod* called_method,
498 Thread* self,
499 ShadowFrame& shadow_frame,
500 JValue* result,
501 uint16_t number_of_inputs,
Narayan Kamath370423d2016-10-03 16:51:22 +0100502 uint32_t (&arg)[Instruction::kMaxVarArgRegs],
Igor Murashkin158f35c2015-06-10 15:55:30 -0700503 uint32_t vregC) ALWAYS_INLINE;
504
Narayan Kamath208f8572016-08-03 12:46:58 +0100505// Separate declaration is required solely for the attributes.
506template <bool is_range> REQUIRES_SHARED(Locks::mutator_lock_)
507static inline bool DoCallPolymorphic(ArtMethod* called_method,
508 Handle<mirror::MethodType> callsite_type,
509 Handle<mirror::MethodType> target_type,
510 Thread* self,
511 ShadowFrame& shadow_frame,
512 JValue* result,
513 uint32_t (&arg)[Instruction::kMaxVarArgRegs],
514 uint32_t vregC) ALWAYS_INLINE;
515
Siva Chandra05d24152016-01-05 17:43:17 -0800516void ArtInterpreterToCompiledCodeBridge(Thread* self,
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100517 ArtMethod* caller,
Siva Chandra05d24152016-01-05 17:43:17 -0800518 const DexFile::CodeItem* code_item,
519 ShadowFrame* shadow_frame,
520 JValue* result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700521 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700522 ArtMethod* method = shadow_frame->GetMethod();
523 // Ensure static methods are initialized.
524 if (method->IsStatic()) {
525 mirror::Class* declaringClass = method->GetDeclaringClass();
526 if (UNLIKELY(!declaringClass->IsInitialized())) {
527 self->PushShadowFrame(shadow_frame);
528 StackHandleScope<1> hs(self);
529 Handle<mirror::Class> h_class(hs.NewHandle(declaringClass));
530 if (UNLIKELY(!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true,
531 true))) {
532 self->PopShadowFrame();
533 DCHECK(self->IsExceptionPending());
534 return;
535 }
536 self->PopShadowFrame();
537 CHECK(h_class->IsInitializing());
538 // Reload from shadow frame in case the method moved, this is faster than adding a handle.
539 method = shadow_frame->GetMethod();
540 }
541 }
542 uint16_t arg_offset = (code_item == nullptr)
543 ? 0
544 : code_item->registers_size_ - code_item->ins_size_;
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100545 jit::Jit* jit = Runtime::Current()->GetJit();
546 if (jit != nullptr && caller != nullptr) {
547 jit->NotifyInterpreterToCompiledCodeTransition(self, caller);
548 }
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700549 method->Invoke(self, shadow_frame->GetVRegArgs(arg_offset),
550 (shadow_frame->NumberOfVRegs() - arg_offset) * sizeof(uint32_t),
Andreas Gampe542451c2016-07-26 09:02:02 -0700551 result, method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty());
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700552}
553
Mingyao Yangffedec52016-05-19 10:48:40 -0700554void SetStringInitValueToAllAliases(ShadowFrame* shadow_frame,
555 uint16_t this_obj_vreg,
556 JValue result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700557 REQUIRES_SHARED(Locks::mutator_lock_) {
Mingyao Yangffedec52016-05-19 10:48:40 -0700558 Object* existing = shadow_frame->GetVRegReference(this_obj_vreg);
559 if (existing == nullptr) {
560 // If it's null, we come from compiled code that was deoptimized. Nothing to do,
561 // as the compiler verified there was no alias.
562 // Set the new string result of the StringFactory.
563 shadow_frame->SetVRegReference(this_obj_vreg, result.GetL());
564 return;
565 }
566 // Set the string init result into all aliases.
567 for (uint32_t i = 0, e = shadow_frame->NumberOfVRegs(); i < e; ++i) {
568 if (shadow_frame->GetVRegReference(i) == existing) {
569 DCHECK_EQ(shadow_frame->GetVRegReference(i),
570 reinterpret_cast<mirror::Object*>(shadow_frame->GetVReg(i)));
571 shadow_frame->SetVRegReference(i, result.GetL());
572 DCHECK_EQ(shadow_frame->GetVRegReference(i),
573 reinterpret_cast<mirror::Object*>(shadow_frame->GetVReg(i)));
574 }
575 }
576}
577
Narayan Kamath9823e782016-08-03 12:46:58 +0100578template<bool is_range, bool do_access_check>
579 REQUIRES_SHARED(Locks::mutator_lock_)
580inline bool DoInvokePolymorphic(Thread* self, ShadowFrame& shadow_frame,
581 const Instruction* inst, uint16_t inst_data,
582 JValue* result) {
583 // Invoke-polymorphic instructions always take a receiver. i.e, they are never static.
584 const uint32_t vRegC = (is_range) ? inst->VRegC_4rcc() : inst->VRegC_45cc();
585
586 // The method_idx here is the name of the signature polymorphic method that
587 // was symbolically invoked in bytecode (say MethodHandle.invoke or MethodHandle.invokeExact)
588 // and not the method that we'll dispatch to in the end.
589 //
590 // TODO(narayan) We'll have to check in the verifier that this is in fact a
591 // signature polymorphic method so that we disallow calls via invoke-polymorphic
592 // to non sig-poly methods. This would also have the side effect of verifying
593 // that vRegC really is a reference type.
Narayan Kamath208f8572016-08-03 12:46:58 +0100594 StackHandleScope<6> hs(self);
595 Handle<mirror::MethodHandleImpl> method_handle(hs.NewHandle(
596 reinterpret_cast<mirror::MethodHandleImpl*>(shadow_frame.GetVRegReference(vRegC))));
597 if (UNLIKELY(method_handle.Get() == nullptr)) {
Narayan Kamath9823e782016-08-03 12:46:58 +0100598 const int method_idx = (is_range) ? inst->VRegB_4rcc() : inst->VRegB_45cc();
599 // Note that the invoke type is kVirtual here because a call to a signature
600 // polymorphic method is shaped like a virtual call at the bytecode level.
601 ThrowNullPointerExceptionForMethodAccess(method_idx, InvokeType::kVirtual);
602
603 result->SetJ(0);
604 return false;
605 }
606
607 // The vRegH value gives the index of the proto_id associated with this
608 // signature polymorphic callsite.
609 const uint32_t callsite_proto_id = (is_range) ? inst->VRegH_4rcc() : inst->VRegH_45cc();
610
611 // Call through to the classlinker and ask it to resolve the static type associated
612 // with the callsite. This information is stored in the dex cache so it's
613 // guaranteed to be fast after the first resolution.
Narayan Kamath9823e782016-08-03 12:46:58 +0100614 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Narayan Kamath208f8572016-08-03 12:46:58 +0100615 Handle<mirror::Class> caller_class(hs.NewHandle(shadow_frame.GetMethod()->GetDeclaringClass()));
616 Handle<mirror::MethodType> callsite_type(hs.NewHandle(class_linker->ResolveMethodType(
Narayan Kamath9823e782016-08-03 12:46:58 +0100617 caller_class->GetDexFile(), callsite_proto_id,
618 hs.NewHandle<mirror::DexCache>(caller_class->GetDexCache()),
Narayan Kamath208f8572016-08-03 12:46:58 +0100619 hs.NewHandle<mirror::ClassLoader>(caller_class->GetClassLoader()))));
Narayan Kamath9823e782016-08-03 12:46:58 +0100620
621 // This implies we couldn't resolve one or more types in this method handle.
Narayan Kamath208f8572016-08-03 12:46:58 +0100622 if (UNLIKELY(callsite_type.Get() == nullptr)) {
Narayan Kamath9823e782016-08-03 12:46:58 +0100623 CHECK(self->IsExceptionPending());
624 result->SetJ(0);
625 return false;
626 }
627
Narayan Kamath9823e782016-08-03 12:46:58 +0100628 // Get the method we're actually invoking along with the kind of
629 // invoke that is desired. We don't need to perform access checks at this
630 // point because they would have been performed on our behalf at the point
631 // of creation of the method handle.
632 ArtMethod* called_method = method_handle->GetTargetMethod();
633 const MethodHandleKind handle_kind = method_handle->GetHandleKind();
Narayan Kamath208f8572016-08-03 12:46:58 +0100634 Handle<mirror::MethodType> handle_type(hs.NewHandle(method_handle->GetMethodType()));
Narayan Kamath9823e782016-08-03 12:46:58 +0100635 CHECK(called_method != nullptr);
Narayan Kamath208f8572016-08-03 12:46:58 +0100636 CHECK(handle_type.Get() != nullptr);
Narayan Kamath9823e782016-08-03 12:46:58 +0100637
638 // We now have to massage the number of inputs to the target function.
639 // It's always one less than the number of inputs to the signature polymorphic
640 // invoke, the first input being a reference to the MethodHandle itself.
641 const uint16_t number_of_inputs =
642 ((is_range) ? inst->VRegA_4rcc(inst_data) : inst->VRegA_45cc(inst_data)) - 1;
643
644 uint32_t arg[Instruction::kMaxVarArgRegs] = {};
645 uint32_t receiver_vregC = 0;
646 if (is_range) {
647 receiver_vregC = (inst->VRegC_4rcc() + 1);
648 } else {
649 inst->GetVarArgs(arg, inst_data);
650 arg[0] = arg[1];
651 arg[1] = arg[2];
652 arg[2] = arg[3];
653 arg[3] = arg[4];
654 arg[4] = 0;
655 receiver_vregC = arg[0];
656 }
657
658 if (IsInvoke(handle_kind)) {
659 if (handle_kind == kInvokeVirtual || handle_kind == kInvokeInterface) {
660 mirror::Object* receiver = shadow_frame.GetVRegReference(receiver_vregC);
661 mirror::Class* declaring_class = called_method->GetDeclaringClass();
662 // Verify that _vRegC is an object reference and of the type expected by
663 // the receiver.
664 called_method = receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(
665 called_method, kRuntimePointerSize);
666 if (!VerifyObjectIsClass(receiver, declaring_class)) {
Narayan Kamath9823e782016-08-03 12:46:58 +0100667 return false;
668 }
669 } else if (handle_kind == kInvokeDirect) {
670 // TODO(narayan) : We need to handle the case where the target method is a
671 // constructor here. Also the case where we don't want to dynamically
672 // dispatch based on the type of the receiver.
Narayan Kamath9823e782016-08-03 12:46:58 +0100673 UNIMPLEMENTED(FATAL) << "Direct invokes are not implemented yet.";
674 return false;
675 }
676
677 // NOTE: handle_kind == kInvokeStatic needs no special treatment here. We
678 // can directly make the call. handle_kind == kInvokeSuper doesn't have any
679 // particular use and can probably be dropped.
Narayan Kamath208f8572016-08-03 12:46:58 +0100680
681 if (callsite_type->IsExactMatch(handle_type.Get())) {
Narayan Kamath9823e782016-08-03 12:46:58 +0100682 return DoCallCommon<is_range, do_access_check>(
683 called_method, self, shadow_frame, result, number_of_inputs,
684 arg, receiver_vregC);
Narayan Kamath208f8572016-08-03 12:46:58 +0100685 } else {
686 return DoCallPolymorphic<is_range>(
687 called_method, callsite_type, handle_type, self, shadow_frame,
688 result, arg, receiver_vregC);
Narayan Kamath9823e782016-08-03 12:46:58 +0100689 }
Narayan Kamath9823e782016-08-03 12:46:58 +0100690 } else {
691 // TODO(narayan): Implement field getters and setters.
Narayan Kamath9823e782016-08-03 12:46:58 +0100692 UNIMPLEMENTED(FATAL) << "Field references in method handles are not implemented yet.";
693 return false;
694 }
695}
696
Narayan Kamath208f8572016-08-03 12:46:58 +0100697// Calculate the number of ins for a proxy or native method, where we
698// can't just look at the code item.
699static inline size_t GetInsForProxyOrNativeMethod(ArtMethod* method)
700 REQUIRES_SHARED(Locks::mutator_lock_) {
701 DCHECK(method->IsNative() || method->IsProxyMethod());
702
703 method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
704 size_t num_ins = 0;
705 // Separate accounting for the receiver, which isn't a part of the
706 // shorty.
707 if (!method->IsStatic()) {
708 ++num_ins;
709 }
710
711 uint32_t shorty_len = 0;
712 const char* shorty = method->GetShorty(&shorty_len);
713 for (size_t i = 1; i < shorty_len; ++i) {
714 const char c = shorty[i];
715 ++num_ins;
716 if (c == 'J' || c == 'D') {
717 ++num_ins;
718 }
719 }
720
721 return num_ins;
722}
723
724template <bool is_range>
725static inline bool DoCallPolymorphic(ArtMethod* called_method,
726 Handle<mirror::MethodType> callsite_type,
727 Handle<mirror::MethodType> target_type,
728 Thread* self,
729 ShadowFrame& shadow_frame,
730 JValue* result,
731 uint32_t (&arg)[Instruction::kMaxVarArgRegs],
732 uint32_t vregC) {
733 // TODO(narayan): Wire in the String.init hacks.
734
735 // Compute method information.
736 const DexFile::CodeItem* code_item = called_method->GetCodeItem();
737
738 // Number of registers for the callee's call frame. Note that for non-exact
739 // invokes, we always derive this information from the callee method. We
740 // cannot guarantee during verification that the number of registers encoded
741 // in the invoke is equal to the number of ins for the callee. This is because
742 // some transformations (such as boxing a long -> Long or wideining an
743 // int -> long will change that number.
744 uint16_t num_regs;
745 size_t first_dest_reg;
746 if (LIKELY(code_item != nullptr)) {
747 num_regs = code_item->registers_size_;
748 first_dest_reg = num_regs - code_item->ins_size_;
749 // Parameter registers go at the end of the shadow frame.
750 DCHECK_NE(first_dest_reg, (size_t)-1);
751 } else {
752 // No local regs for proxy and native methods.
753 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
754 num_regs = GetInsForProxyOrNativeMethod(called_method);
755 first_dest_reg = 0;
756 }
757
758 // Allocate shadow frame on the stack.
759 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
760 CREATE_SHADOW_FRAME(num_regs, &shadow_frame, called_method, /* dex pc */ 0);
761 ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get();
762
763 // Thread might be suspended during PerformArgumentConversions due to the
764 // allocations performed during boxing.
765 {
766 ScopedStackedShadowFramePusher pusher(
767 self, new_shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
768 if (!PerformArgumentConversions<is_range>(self, callsite_type, target_type,
769 shadow_frame, vregC, first_dest_reg,
770 arg, new_shadow_frame, result)) {
771 DCHECK(self->IsExceptionPending());
772 result->SetL(0);
773 return false;
774 }
775 }
776
777 // Do the call now.
778 if (LIKELY(Runtime::Current()->IsStarted())) {
779 ArtMethod* target = new_shadow_frame->GetMethod();
780 if (ClassLinker::ShouldUseInterpreterEntrypoint(
781 target,
782 target->GetEntryPointFromQuickCompiledCode())) {
783 ArtInterpreterToInterpreterBridge(self, code_item, new_shadow_frame, result);
784 } else {
785 ArtInterpreterToCompiledCodeBridge(
786 self, shadow_frame.GetMethod(), code_item, new_shadow_frame, result);
787 }
788 } else {
789 UnstartedRuntime::Invoke(self, code_item, new_shadow_frame, result, first_dest_reg);
790 }
791
792 // TODO(narayan): Perform return value conversions.
793
794 return !self->IsExceptionPending();
795}
796
Igor Murashkin6918bf12015-09-27 19:19:06 -0700797template <bool is_range,
Narayan Kamath370423d2016-10-03 16:51:22 +0100798 bool do_assignability_check>
Igor Murashkin158f35c2015-06-10 15:55:30 -0700799static inline bool DoCallCommon(ArtMethod* called_method,
800 Thread* self,
801 ShadowFrame& shadow_frame,
802 JValue* result,
803 uint16_t number_of_inputs,
Narayan Kamath370423d2016-10-03 16:51:22 +0100804 uint32_t (&arg)[Instruction::kMaxVarArgRegs],
Igor Murashkin158f35c2015-06-10 15:55:30 -0700805 uint32_t vregC) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800806 bool string_init = false;
807 // Replace calls to String.<init> with equivalent StringFactory call.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700808 if (UNLIKELY(called_method->GetDeclaringClass()->IsStringClass()
809 && called_method->IsConstructor())) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100810 called_method = WellKnownClasses::StringInitToStringFactory(called_method);
Jeff Hao848f70a2014-01-15 13:49:50 -0800811 string_init = true;
812 }
813
Alex Lightdaf58c82016-03-16 23:00:49 +0000814 // Compute method information.
815 const DexFile::CodeItem* code_item = called_method->GetCodeItem();
Igor Murashkin158f35c2015-06-10 15:55:30 -0700816
817 // Number of registers for the callee's call frame.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200818 uint16_t num_regs;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700819 if (LIKELY(code_item != nullptr)) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200820 num_regs = code_item->registers_size_;
Igor Murashkin158f35c2015-06-10 15:55:30 -0700821 DCHECK_EQ(string_init ? number_of_inputs - 1 : number_of_inputs, code_item->ins_size_);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200822 } else {
Ian Rogerse94652f2014-12-02 11:13:19 -0800823 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
Igor Murashkin158f35c2015-06-10 15:55:30 -0700824 num_regs = number_of_inputs;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200825 }
826
Igor Murashkin158f35c2015-06-10 15:55:30 -0700827 // Hack for String init:
828 //
829 // Rewrite invoke-x java.lang.String.<init>(this, a, b, c, ...) into:
830 // invoke-x StringFactory(a, b, c, ...)
831 // by effectively dropping the first virtual register from the invoke.
832 //
833 // (at this point the ArtMethod has already been replaced,
834 // so we just need to fix-up the arguments)
David Brazdil65902e82016-01-15 09:35:13 +0000835 //
836 // Note that FindMethodFromCode in entrypoint_utils-inl.h was also special-cased
837 // to handle the compiler optimization of replacing `this` with null without
838 // throwing NullPointerException.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700839 uint32_t string_init_vreg_this = is_range ? vregC : arg[0];
Igor Murashkina06b49b2015-06-25 15:18:12 -0700840 if (UNLIKELY(string_init)) {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700841 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 -0700842
Igor Murashkin158f35c2015-06-10 15:55:30 -0700843 // The new StringFactory call is static and has one fewer argument.
Igor Murashkina06b49b2015-06-25 15:18:12 -0700844 if (code_item == nullptr) {
845 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
846 num_regs--;
847 } // 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 -0700848 number_of_inputs--;
849
850 // Rewrite the var-args, dropping the 0th argument ("this")
Igor Murashkin6918bf12015-09-27 19:19:06 -0700851 for (uint32_t i = 1; i < arraysize(arg); ++i) {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700852 arg[i - 1] = arg[i];
853 }
Igor Murashkin6918bf12015-09-27 19:19:06 -0700854 arg[arraysize(arg) - 1] = 0;
Igor Murashkin158f35c2015-06-10 15:55:30 -0700855
856 // Rewrite the non-var-arg case
857 vregC++; // Skips the 0th vreg in the range ("this").
858 }
859
860 // Parameter registers go at the end of the shadow frame.
861 DCHECK_GE(num_regs, number_of_inputs);
862 size_t first_dest_reg = num_regs - number_of_inputs;
863 DCHECK_NE(first_dest_reg, (size_t)-1);
864
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200865 // Allocate shadow frame on the stack.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700866 const char* old_cause = self->StartAssertNoThreadSuspension("DoCallCommon");
Andreas Gampeb3025922015-09-01 14:45:00 -0700867 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
Andreas Gampe03ec9302015-08-27 17:41:47 -0700868 CREATE_SHADOW_FRAME(num_regs, &shadow_frame, called_method, /* dex pc */ 0);
Andreas Gampeb3025922015-09-01 14:45:00 -0700869 ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get();
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200870
Igor Murashkin158f35c2015-06-10 15:55:30 -0700871 // Initialize new shadow frame by copying the registers from the callee shadow frame.
Jeff Haoa3faaf42013-09-03 19:07:00 -0700872 if (do_assignability_check) {
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -0700873 // Slow path.
874 // We might need to do class loading, which incurs a thread state change to kNative. So
875 // register the shadow frame as under construction and allow suspension again.
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700876 ScopedStackedShadowFramePusher pusher(
Sebastien Hertzf7958692015-06-09 14:09:14 +0200877 self, new_shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -0700878 self->EndAssertNoThreadSuspension(old_cause);
879
Igor Murashkin9f95ba72016-02-01 14:21:25 -0800880 // ArtMethod here is needed to check type information of the call site against the callee.
881 // Type information is retrieved from a DexFile/DexCache for that respective declared method.
882 //
883 // As a special case for proxy methods, which are not dex-backed,
884 // we have to retrieve type information from the proxy's method
885 // interface method instead (which is dex backed since proxies are never interfaces).
Andreas Gampe542451c2016-07-26 09:02:02 -0700886 ArtMethod* method =
887 new_shadow_frame->GetMethod()->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Igor Murashkin9f95ba72016-02-01 14:21:25 -0800888
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -0700889 // We need to do runtime check on reference assignment. We need to load the shorty
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200890 // to get the exact type of each reference argument.
Igor Murashkin9f95ba72016-02-01 14:21:25 -0800891 const DexFile::TypeList* params = method->GetParameterTypeList();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700892 uint32_t shorty_len = 0;
Igor Murashkin9f95ba72016-02-01 14:21:25 -0800893 const char* shorty = method->GetShorty(&shorty_len);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200894
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100895 // Handle receiver apart since it's not part of the shorty.
896 size_t dest_reg = first_dest_reg;
897 size_t arg_offset = 0;
Igor Murashkin158f35c2015-06-10 15:55:30 -0700898
Igor Murashkin9f95ba72016-02-01 14:21:25 -0800899 if (!method->IsStatic()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700900 size_t receiver_reg = is_range ? vregC : arg[0];
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100901 new_shadow_frame->SetVRegReference(dest_reg, shadow_frame.GetVRegReference(receiver_reg));
902 ++dest_reg;
903 ++arg_offset;
Igor Murashkina06b49b2015-06-25 15:18:12 -0700904 DCHECK(!string_init); // All StringFactory methods are static.
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100905 }
Igor Murashkin158f35c2015-06-10 15:55:30 -0700906
907 // Copy the caller's invoke-* arguments into the callee's parameter registers.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800908 for (uint32_t shorty_pos = 0; dest_reg < num_regs; ++shorty_pos, ++dest_reg, ++arg_offset) {
Igor Murashkina06b49b2015-06-25 15:18:12 -0700909 // Skip the 0th 'shorty' type since it represents the return type.
910 DCHECK_LT(shorty_pos + 1, shorty_len) << "for shorty '" << shorty << "'";
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200911 const size_t src_reg = (is_range) ? vregC + arg_offset : arg[arg_offset];
912 switch (shorty[shorty_pos + 1]) {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700913 // Handle Object references. 1 virtual register slot.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200914 case 'L': {
915 Object* o = shadow_frame.GetVRegReference(src_reg);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700916 if (do_assignability_check && o != nullptr) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700917 PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Ian Rogersa0485602014-12-02 15:48:04 -0800918 Class* arg_type =
Igor Murashkin9f95ba72016-02-01 14:21:25 -0800919 method->GetClassFromTypeIndex(
Vladimir Marko05792b92015-08-03 11:56:49 +0100920 params->GetTypeItem(shorty_pos).type_idx_, true /* resolve */, pointer_size);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700921 if (arg_type == nullptr) {
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200922 CHECK(self->IsExceptionPending());
923 return false;
924 }
925 if (!o->VerifierInstanceOf(arg_type)) {
926 // This should never happen.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700927 std::string temp1, temp2;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000928 self->ThrowNewExceptionF("Ljava/lang/VirtualMachineError;",
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200929 "Invoking %s with bad arg %d, type '%s' not instance of '%s'",
Ian Rogerse94652f2014-12-02 11:13:19 -0800930 new_shadow_frame->GetMethod()->GetName(), shorty_pos,
Ian Rogers1ff3c982014-08-12 02:30:58 -0700931 o->GetClass()->GetDescriptor(&temp1),
932 arg_type->GetDescriptor(&temp2));
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200933 return false;
934 }
Jeff Haoa3faaf42013-09-03 19:07:00 -0700935 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200936 new_shadow_frame->SetVRegReference(dest_reg, o);
937 break;
Jeff Haoa3faaf42013-09-03 19:07:00 -0700938 }
Igor Murashkin158f35c2015-06-10 15:55:30 -0700939 // Handle doubles and longs. 2 consecutive virtual register slots.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200940 case 'J': case 'D': {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700941 uint64_t wide_value =
942 (static_cast<uint64_t>(shadow_frame.GetVReg(src_reg + 1)) << BitSizeOf<uint32_t>()) |
943 static_cast<uint32_t>(shadow_frame.GetVReg(src_reg));
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200944 new_shadow_frame->SetVRegLong(dest_reg, wide_value);
Igor Murashkin158f35c2015-06-10 15:55:30 -0700945 // Skip the next virtual register slot since we already used it.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200946 ++dest_reg;
947 ++arg_offset;
948 break;
949 }
Igor Murashkin158f35c2015-06-10 15:55:30 -0700950 // Handle all other primitives that are always 1 virtual register slot.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200951 default:
952 new_shadow_frame->SetVReg(dest_reg, shadow_frame.GetVReg(src_reg));
953 break;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200954 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200955 }
956 } else {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700957 size_t arg_index = 0;
958
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200959 // Fast path: no extra checks.
960 if (is_range) {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700961 uint16_t first_src_reg = vregC;
962
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200963 for (size_t src_reg = first_src_reg, dest_reg = first_dest_reg; dest_reg < num_regs;
964 ++dest_reg, ++src_reg) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800965 AssignRegister(new_shadow_frame, shadow_frame, dest_reg, src_reg);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200966 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200967 } else {
Igor Murashkin6918bf12015-09-27 19:19:06 -0700968 DCHECK_LE(number_of_inputs, arraysize(arg));
Igor Murashkin158f35c2015-06-10 15:55:30 -0700969
970 for (; arg_index < number_of_inputs; ++arg_index) {
971 AssignRegister(new_shadow_frame, shadow_frame, first_dest_reg + arg_index, arg[arg_index]);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200972 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200973 }
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -0700974 self->EndAssertNoThreadSuspension(old_cause);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200975 }
976
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200977 // Do the call now.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200978 if (LIKELY(Runtime::Current()->IsStarted())) {
Tamas Berghammerdd5e5e92016-02-12 16:29:00 +0000979 ArtMethod* target = new_shadow_frame->GetMethod();
980 if (ClassLinker::ShouldUseInterpreterEntrypoint(
981 target,
982 target->GetEntryPointFromQuickCompiledCode())) {
Tamas Berghammerc94a61f2016-02-05 18:09:08 +0000983 ArtInterpreterToInterpreterBridge(self, code_item, new_shadow_frame, result);
Tamas Berghammer3a98aae2016-02-08 20:21:54 +0000984 } else {
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100985 ArtInterpreterToCompiledCodeBridge(
986 self, shadow_frame.GetMethod(), code_item, new_shadow_frame, result);
Nicolas Geoffray7070ccd2015-07-08 09:41:54 +0000987 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200988 } else {
Andreas Gampe799681b2015-05-15 19:24:12 -0700989 UnstartedRuntime::Invoke(self, code_item, new_shadow_frame, result, first_dest_reg);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200990 }
Jeff Hao848f70a2014-01-15 13:49:50 -0800991
992 if (string_init && !self->IsExceptionPending()) {
Mingyao Yangffedec52016-05-19 10:48:40 -0700993 SetStringInitValueToAllAliases(&shadow_frame, string_init_vreg_this, *result);
Jeff Hao848f70a2014-01-15 13:49:50 -0800994 }
995
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200996 return !self->IsExceptionPending();
997}
998
Igor Murashkin158f35c2015-06-10 15:55:30 -0700999template<bool is_range, bool do_assignability_check>
Igor Murashkin158f35c2015-06-10 15:55:30 -07001000bool DoCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame,
1001 const Instruction* inst, uint16_t inst_data, JValue* result) {
1002 // Argument word count.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001003 const uint16_t number_of_inputs =
1004 (is_range) ? inst->VRegA_3rc(inst_data) : inst->VRegA_35c(inst_data);
Igor Murashkin158f35c2015-06-10 15:55:30 -07001005
1006 // TODO: find a cleaner way to separate non-range and range information without duplicating
1007 // code.
Igor Murashkin6918bf12015-09-27 19:19:06 -07001008 uint32_t arg[Instruction::kMaxVarArgRegs] = {}; // only used in invoke-XXX.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001009 uint32_t vregC = 0;
1010 if (is_range) {
1011 vregC = inst->VRegC_3rc();
1012 } else {
1013 vregC = inst->VRegC_35c();
1014 inst->GetVarArgs(arg, inst_data);
1015 }
1016
1017 return DoCallCommon<is_range, do_assignability_check>(
1018 called_method, self, shadow_frame,
1019 result, number_of_inputs, arg, vregC);
1020}
1021
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001022template <bool is_range, bool do_access_check, bool transaction_active>
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001023bool DoFilledNewArray(const Instruction* inst, const ShadowFrame& shadow_frame,
1024 Thread* self, JValue* result) {
1025 DCHECK(inst->Opcode() == Instruction::FILLED_NEW_ARRAY ||
1026 inst->Opcode() == Instruction::FILLED_NEW_ARRAY_RANGE);
1027 const int32_t length = is_range ? inst->VRegA_3rc() : inst->VRegA_35c();
1028 if (!is_range) {
1029 // Checks FILLED_NEW_ARRAY's length does not exceed 5 arguments.
1030 CHECK_LE(length, 5);
1031 }
1032 if (UNLIKELY(length < 0)) {
1033 ThrowNegativeArraySizeException(length);
1034 return false;
1035 }
1036 uint16_t type_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001037 Class* array_class = ResolveVerifyAndClinit(type_idx, shadow_frame.GetMethod(),
1038 self, false, do_access_check);
1039 if (UNLIKELY(array_class == nullptr)) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001040 DCHECK(self->IsExceptionPending());
1041 return false;
1042 }
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001043 CHECK(array_class->IsArrayClass());
1044 Class* component_class = array_class->GetComponentType();
1045 const bool is_primitive_int_component = component_class->IsPrimitiveInt();
1046 if (UNLIKELY(component_class->IsPrimitive() && !is_primitive_int_component)) {
1047 if (component_class->IsPrimitiveLong() || component_class->IsPrimitiveDouble()) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001048 ThrowRuntimeException("Bad filled array request for type %s",
David Sehr709b0702016-10-13 09:12:37 -07001049 component_class->PrettyDescriptor().c_str());
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001050 } else {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +00001051 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
Brian Carlstrom4fa0bcd2013-12-10 11:24:21 -08001052 "Found type %s; filled-new-array not implemented for anything but 'int'",
David Sehr709b0702016-10-13 09:12:37 -07001053 component_class->PrettyDescriptor().c_str());
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001054 }
1055 return false;
1056 }
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001057 Object* new_array = Array::Alloc<true>(self, array_class, length,
1058 array_class->GetComponentSizeShift(),
1059 Runtime::Current()->GetHeap()->GetCurrentAllocator());
1060 if (UNLIKELY(new_array == nullptr)) {
1061 self->AssertPendingOOMException();
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001062 return false;
1063 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001064 uint32_t arg[Instruction::kMaxVarArgRegs]; // only used in filled-new-array.
1065 uint32_t vregC = 0; // only used in filled-new-array-range.
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001066 if (is_range) {
Sebastien Hertzabff6432014-01-27 18:01:39 +01001067 vregC = inst->VRegC_3rc();
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001068 } else {
Ian Rogers29a26482014-05-02 15:27:29 -07001069 inst->GetVarArgs(arg);
Sebastien Hertzabff6432014-01-27 18:01:39 +01001070 }
Sebastien Hertzabff6432014-01-27 18:01:39 +01001071 for (int32_t i = 0; i < length; ++i) {
1072 size_t src_reg = is_range ? vregC + i : arg[i];
1073 if (is_primitive_int_component) {
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001074 new_array->AsIntArray()->SetWithoutChecks<transaction_active>(
1075 i, shadow_frame.GetVReg(src_reg));
Sebastien Hertzabff6432014-01-27 18:01:39 +01001076 } else {
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001077 new_array->AsObjectArray<Object>()->SetWithoutChecks<transaction_active>(
1078 i, shadow_frame.GetVRegReference(src_reg));
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001079 }
1080 }
1081
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001082 result->SetL(new_array);
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001083 return true;
1084}
1085
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001086// TODO fix thread analysis: should be REQUIRES_SHARED(Locks::mutator_lock_).
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001087template<typename T>
1088static void RecordArrayElementsInTransactionImpl(mirror::PrimitiveArray<T>* array, int32_t count)
1089 NO_THREAD_SAFETY_ANALYSIS {
1090 Runtime* runtime = Runtime::Current();
1091 for (int32_t i = 0; i < count; ++i) {
1092 runtime->RecordWriteArray(array, i, array->GetWithoutChecks(i));
1093 }
1094}
1095
1096void RecordArrayElementsInTransaction(mirror::Array* array, int32_t count)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001097 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001098 DCHECK(Runtime::Current()->IsActiveTransaction());
1099 DCHECK(array != nullptr);
1100 DCHECK_LE(count, array->GetLength());
1101 Primitive::Type primitive_component_type = array->GetClass()->GetComponentType()->GetPrimitiveType();
1102 switch (primitive_component_type) {
1103 case Primitive::kPrimBoolean:
1104 RecordArrayElementsInTransactionImpl(array->AsBooleanArray(), count);
1105 break;
1106 case Primitive::kPrimByte:
1107 RecordArrayElementsInTransactionImpl(array->AsByteArray(), count);
1108 break;
1109 case Primitive::kPrimChar:
1110 RecordArrayElementsInTransactionImpl(array->AsCharArray(), count);
1111 break;
1112 case Primitive::kPrimShort:
1113 RecordArrayElementsInTransactionImpl(array->AsShortArray(), count);
1114 break;
1115 case Primitive::kPrimInt:
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001116 RecordArrayElementsInTransactionImpl(array->AsIntArray(), count);
1117 break;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001118 case Primitive::kPrimFloat:
1119 RecordArrayElementsInTransactionImpl(array->AsFloatArray(), count);
1120 break;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001121 case Primitive::kPrimLong:
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001122 RecordArrayElementsInTransactionImpl(array->AsLongArray(), count);
1123 break;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001124 case Primitive::kPrimDouble:
1125 RecordArrayElementsInTransactionImpl(array->AsDoubleArray(), count);
1126 break;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001127 default:
1128 LOG(FATAL) << "Unsupported primitive type " << primitive_component_type
1129 << " in fill-array-data";
1130 break;
1131 }
1132}
1133
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001134// Explicit DoCall template function declarations.
Sebastien Hertzc6714852013-09-30 16:42:32 +02001135#define EXPLICIT_DO_CALL_TEMPLATE_DECL(_is_range, _do_assignability_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001136 template REQUIRES_SHARED(Locks::mutator_lock_) \
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001137 bool DoCall<_is_range, _do_assignability_check>(ArtMethod* method, Thread* self, \
1138 ShadowFrame& shadow_frame, \
Sebastien Hertzc6714852013-09-30 16:42:32 +02001139 const Instruction* inst, uint16_t inst_data, \
1140 JValue* result)
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001141EXPLICIT_DO_CALL_TEMPLATE_DECL(false, false);
1142EXPLICIT_DO_CALL_TEMPLATE_DECL(false, true);
1143EXPLICIT_DO_CALL_TEMPLATE_DECL(true, false);
1144EXPLICIT_DO_CALL_TEMPLATE_DECL(true, true);
1145#undef EXPLICIT_DO_CALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001146
Narayan Kamath9823e782016-08-03 12:46:58 +01001147// Explicit DoInvokePolymorphic template function declarations.
1148#define EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(_is_range, _do_assignability_check) \
1149 template REQUIRES_SHARED(Locks::mutator_lock_) \
1150 bool DoInvokePolymorphic<_is_range, _do_assignability_check>( \
1151 Thread* self, ShadowFrame& shadow_frame, const Instruction* inst, \
1152 uint16_t inst_data, JValue* result)
1153
1154EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(false, false);
1155EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(false, true);
1156EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(true, false);
1157EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(true, true);
1158#undef EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL
1159
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001160// Explicit DoFilledNewArray template function declarations.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001161#define EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(_is_range_, _check, _transaction_active) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001162 template REQUIRES_SHARED(Locks::mutator_lock_) \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001163 bool DoFilledNewArray<_is_range_, _check, _transaction_active>(const Instruction* inst, \
1164 const ShadowFrame& shadow_frame, \
1165 Thread* self, JValue* result)
1166#define EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(_transaction_active) \
1167 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, false, _transaction_active); \
1168 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, true, _transaction_active); \
1169 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, false, _transaction_active); \
1170 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, true, _transaction_active)
1171EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(false);
1172EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(true);
1173#undef EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001174#undef EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL
1175
1176} // namespace interpreter
1177} // namespace art