blob: e677cfbfe89de3312517e8f177879b7f2688d07b [file] [log] [blame]
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "interpreter_common.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070018
Andreas Gampef0e128a2015-02-27 20:08:34 -080019#include <cmath>
20
Andreas Gampe542451c2016-07-26 09:02:02 -070021#include "base/enums.h"
Daniel Mihalyieb076692014-08-22 17:33:31 +020022#include "debugger.h"
Nicolas Geoffray7bf2b4f2015-07-08 10:11:59 +000023#include "entrypoints/runtime_asm_entrypoints.h"
Tamas Berghammerdd5e5e92016-02-12 16:29:00 +000024#include "jit/jit.h"
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010025#include "mirror/array-inl.h"
Andreas Gampeb3025922015-09-01 14:45:00 -070026#include "stack.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070027#include "unstarted_runtime.h"
Jeff Hao848f70a2014-01-15 13:49:50 -080028#include "verifier/method_verifier.h"
Sebastien Hertz8ece0502013-08-07 11:26:41 +020029
30namespace art {
31namespace interpreter {
32
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000033void ThrowNullPointerExceptionFromInterpreter() {
34 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -070035}
36
37template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check>
38bool DoFieldGet(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst,
39 uint16_t inst_data) {
40 const bool is_static = (find_type == StaticObjectRead) || (find_type == StaticPrimitiveRead);
41 const uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Roland Levillain4b8f1ec2015-08-26 18:34:03 +010042 ArtField* f =
43 FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
44 Primitive::ComponentSize(field_type));
Ian Rogers54874942014-06-10 16:31:03 -070045 if (UNLIKELY(f == nullptr)) {
46 CHECK(self->IsExceptionPending());
47 return false;
48 }
Mathieu Chartier3398c782016-09-30 10:27:43 -070049 ObjPtr<Object> obj;
Ian Rogers54874942014-06-10 16:31:03 -070050 if (is_static) {
51 obj = f->GetDeclaringClass();
52 } else {
53 obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
54 if (UNLIKELY(obj == nullptr)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000055 ThrowNullPointerExceptionForFieldAccess(f, true);
Ian Rogers54874942014-06-10 16:31:03 -070056 return false;
57 }
58 }
Sebastien Hertz1edbd8e2014-07-16 20:00:11 +020059 f->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self);
Ian Rogers54874942014-06-10 16:31:03 -070060 // Report this field access to instrumentation if needed.
61 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
62 if (UNLIKELY(instrumentation->HasFieldReadListeners())) {
Mathieu Chartier0715c0b2016-10-03 22:49:46 -070063 StackHandleScope<1> hs(self);
64 // Wrap in handle wrapper in case the listener does thread suspension.
65 HandleWrapperObjPtr<mirror::Object> h(hs.NewHandleWrapper(&obj));
Mathieu Chartier3398c782016-09-30 10:27:43 -070066 ObjPtr<Object> this_object;
67 if (!f->IsStatic()) {
68 this_object = obj;
69 }
70 instrumentation->FieldReadEvent(self,
Mathieu Chartier1cc62e42016-10-03 18:01:28 -070071 this_object.Ptr(),
Mathieu Chartier3398c782016-09-30 10:27:43 -070072 shadow_frame.GetMethod(),
73 shadow_frame.GetDexPC(),
74 f);
Ian Rogers54874942014-06-10 16:31:03 -070075 }
76 uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
77 switch (field_type) {
78 case Primitive::kPrimBoolean:
79 shadow_frame.SetVReg(vregA, f->GetBoolean(obj));
80 break;
81 case Primitive::kPrimByte:
82 shadow_frame.SetVReg(vregA, f->GetByte(obj));
83 break;
84 case Primitive::kPrimChar:
85 shadow_frame.SetVReg(vregA, f->GetChar(obj));
86 break;
87 case Primitive::kPrimShort:
88 shadow_frame.SetVReg(vregA, f->GetShort(obj));
89 break;
90 case Primitive::kPrimInt:
91 shadow_frame.SetVReg(vregA, f->GetInt(obj));
92 break;
93 case Primitive::kPrimLong:
94 shadow_frame.SetVRegLong(vregA, f->GetLong(obj));
95 break;
96 case Primitive::kPrimNot:
Mathieu Chartier1cc62e42016-10-03 18:01:28 -070097 shadow_frame.SetVRegReference(vregA, f->GetObject(obj).Ptr());
Ian Rogers54874942014-06-10 16:31:03 -070098 break;
99 default:
100 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700101 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700102 }
103 return true;
104}
105
106// Explicitly instantiate all DoFieldGet functions.
107#define EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, _do_check) \
108 template bool DoFieldGet<_find_type, _field_type, _do_check>(Thread* self, \
109 ShadowFrame& shadow_frame, \
110 const Instruction* inst, \
111 uint16_t inst_data)
112
113#define EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(_find_type, _field_type) \
114 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, false); \
115 EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, true);
116
117// iget-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700118EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimBoolean)
119EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimByte)
120EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimChar)
121EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimShort)
122EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimInt)
123EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimLong)
124EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstanceObjectRead, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700125
126// sget-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700127EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimBoolean)
128EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimByte)
129EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimChar)
130EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimShort)
131EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimInt)
132EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimLong)
133EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticObjectRead, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700134
135#undef EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL
136#undef EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL
137
138// Handles iget-quick, iget-wide-quick and iget-object-quick instructions.
139// Returns true on success, otherwise throws an exception and returns false.
140template<Primitive::Type field_type>
141bool DoIGetQuick(ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
142 Object* obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
143 if (UNLIKELY(obj == nullptr)) {
144 // We lost the reference to the field index so we cannot get a more
145 // precised exception message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000146 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -0700147 return false;
148 }
149 MemberOffset field_offset(inst->VRegC_22c());
150 // Report this field access to instrumentation if needed. Since we only have the offset of
151 // the field from the base of the object, we need to look for it first.
152 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
153 if (UNLIKELY(instrumentation->HasFieldReadListeners())) {
154 ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(),
155 field_offset.Uint32Value());
156 DCHECK(f != nullptr);
157 DCHECK(!f->IsStatic());
158 instrumentation->FieldReadEvent(Thread::Current(), obj, shadow_frame.GetMethod(),
159 shadow_frame.GetDexPC(), f);
160 }
161 // Note: iget-x-quick instructions are only for non-volatile fields.
162 const uint32_t vregA = inst->VRegA_22c(inst_data);
163 switch (field_type) {
164 case Primitive::kPrimInt:
165 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetField32(field_offset)));
166 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800167 case Primitive::kPrimBoolean:
168 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldBoolean(field_offset)));
169 break;
170 case Primitive::kPrimByte:
171 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldByte(field_offset)));
172 break;
173 case Primitive::kPrimChar:
174 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldChar(field_offset)));
175 break;
176 case Primitive::kPrimShort:
177 shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldShort(field_offset)));
178 break;
Ian Rogers54874942014-06-10 16:31:03 -0700179 case Primitive::kPrimLong:
180 shadow_frame.SetVRegLong(vregA, static_cast<int64_t>(obj->GetField64(field_offset)));
181 break;
182 case Primitive::kPrimNot:
183 shadow_frame.SetVRegReference(vregA, obj->GetFieldObject<mirror::Object>(field_offset));
184 break;
185 default:
186 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700187 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700188 }
189 return true;
190}
191
192// Explicitly instantiate all DoIGetQuick functions.
193#define EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(_field_type) \
194 template bool DoIGetQuick<_field_type>(ShadowFrame& shadow_frame, const Instruction* inst, \
195 uint16_t inst_data)
196
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800197EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimInt); // iget-quick.
198EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimBoolean); // iget-boolean-quick.
199EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimByte); // iget-byte-quick.
200EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimChar); // iget-char-quick.
201EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimShort); // iget-short-quick.
202EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimLong); // iget-wide-quick.
203EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimNot); // iget-object-quick.
Ian Rogers54874942014-06-10 16:31:03 -0700204#undef EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL
205
206template<Primitive::Type field_type>
207static JValue GetFieldValue(const ShadowFrame& shadow_frame, uint32_t vreg)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700208 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers54874942014-06-10 16:31:03 -0700209 JValue field_value;
210 switch (field_type) {
211 case Primitive::kPrimBoolean:
212 field_value.SetZ(static_cast<uint8_t>(shadow_frame.GetVReg(vreg)));
213 break;
214 case Primitive::kPrimByte:
215 field_value.SetB(static_cast<int8_t>(shadow_frame.GetVReg(vreg)));
216 break;
217 case Primitive::kPrimChar:
218 field_value.SetC(static_cast<uint16_t>(shadow_frame.GetVReg(vreg)));
219 break;
220 case Primitive::kPrimShort:
221 field_value.SetS(static_cast<int16_t>(shadow_frame.GetVReg(vreg)));
222 break;
223 case Primitive::kPrimInt:
224 field_value.SetI(shadow_frame.GetVReg(vreg));
225 break;
226 case Primitive::kPrimLong:
227 field_value.SetJ(shadow_frame.GetVRegLong(vreg));
228 break;
229 case Primitive::kPrimNot:
230 field_value.SetL(shadow_frame.GetVRegReference(vreg));
231 break;
232 default:
233 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700234 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700235 }
236 return field_value;
237}
238
239template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check,
240 bool transaction_active>
241bool DoFieldPut(Thread* self, const ShadowFrame& shadow_frame, const Instruction* inst,
242 uint16_t inst_data) {
243 bool do_assignability_check = do_access_check;
244 bool is_static = (find_type == StaticObjectWrite) || (find_type == StaticPrimitiveWrite);
245 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Roland Levillain4b8f1ec2015-08-26 18:34:03 +0100246 ArtField* f =
247 FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self,
248 Primitive::ComponentSize(field_type));
Ian Rogers54874942014-06-10 16:31:03 -0700249 if (UNLIKELY(f == nullptr)) {
250 CHECK(self->IsExceptionPending());
251 return false;
252 }
Mathieu Chartier3398c782016-09-30 10:27:43 -0700253 ObjPtr<Object> obj;
Ian Rogers54874942014-06-10 16:31:03 -0700254 if (is_static) {
255 obj = f->GetDeclaringClass();
256 } else {
257 obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
258 if (UNLIKELY(obj == nullptr)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000259 ThrowNullPointerExceptionForFieldAccess(f, false);
Ian Rogers54874942014-06-10 16:31:03 -0700260 return false;
261 }
262 }
Sebastien Hertz1edbd8e2014-07-16 20:00:11 +0200263 f->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self);
Ian Rogers54874942014-06-10 16:31:03 -0700264 uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
265 // Report this field access to instrumentation if needed. Since we only have the offset of
266 // the field from the base of the object, we need to look for it first.
267 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
268 if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
Mathieu Chartier0715c0b2016-10-03 22:49:46 -0700269 StackHandleScope<1> hs(self);
270 // Wrap in handle wrapper in case the listener does thread suspension.
271 HandleWrapperObjPtr<mirror::Object> h(hs.NewHandleWrapper(&obj));
Ian Rogers54874942014-06-10 16:31:03 -0700272 JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA);
Mathieu Chartier3398c782016-09-30 10:27:43 -0700273 ObjPtr<Object> this_object = f->IsStatic() ? nullptr : obj;
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700274 instrumentation->FieldWriteEvent(self, this_object.Ptr(),
Mathieu Chartier3398c782016-09-30 10:27:43 -0700275 shadow_frame.GetMethod(),
276 shadow_frame.GetDexPC(),
277 f,
278 field_value);
Ian Rogers54874942014-06-10 16:31:03 -0700279 }
280 switch (field_type) {
281 case Primitive::kPrimBoolean:
282 f->SetBoolean<transaction_active>(obj, shadow_frame.GetVReg(vregA));
283 break;
284 case Primitive::kPrimByte:
285 f->SetByte<transaction_active>(obj, shadow_frame.GetVReg(vregA));
286 break;
287 case Primitive::kPrimChar:
288 f->SetChar<transaction_active>(obj, shadow_frame.GetVReg(vregA));
289 break;
290 case Primitive::kPrimShort:
291 f->SetShort<transaction_active>(obj, shadow_frame.GetVReg(vregA));
292 break;
293 case Primitive::kPrimInt:
294 f->SetInt<transaction_active>(obj, shadow_frame.GetVReg(vregA));
295 break;
296 case Primitive::kPrimLong:
297 f->SetLong<transaction_active>(obj, shadow_frame.GetVRegLong(vregA));
298 break;
299 case Primitive::kPrimNot: {
300 Object* reg = shadow_frame.GetVRegReference(vregA);
301 if (do_assignability_check && reg != nullptr) {
302 // FieldHelper::GetType can resolve classes, use a handle wrapper which will restore the
303 // object in the destructor.
Mathieu Chartier3398c782016-09-30 10:27:43 -0700304 ObjPtr<Class> field_class;
Ian Rogers54874942014-06-10 16:31:03 -0700305 {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700306 StackHandleScope<2> hs(self);
Ian Rogers54874942014-06-10 16:31:03 -0700307 HandleWrapper<mirror::Object> h_reg(hs.NewHandleWrapper(&reg));
Mathieu Chartier3398c782016-09-30 10:27:43 -0700308 HandleWrapperObjPtr<mirror::Object> h_obj(hs.NewHandleWrapper(&obj));
Mathieu Chartierc7853442015-03-27 14:35:38 -0700309 field_class = f->GetType<true>();
Ian Rogers54874942014-06-10 16:31:03 -0700310 }
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700311 if (!reg->VerifierInstanceOf(field_class.Ptr())) {
Ian Rogers54874942014-06-10 16:31:03 -0700312 // This should never happen.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700313 std::string temp1, temp2, temp3;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000314 self->ThrowNewExceptionF("Ljava/lang/VirtualMachineError;",
Ian Rogers54874942014-06-10 16:31:03 -0700315 "Put '%s' that is not instance of field '%s' in '%s'",
Ian Rogers1ff3c982014-08-12 02:30:58 -0700316 reg->GetClass()->GetDescriptor(&temp1),
317 field_class->GetDescriptor(&temp2),
318 f->GetDeclaringClass()->GetDescriptor(&temp3));
Ian Rogers54874942014-06-10 16:31:03 -0700319 return false;
320 }
321 }
322 f->SetObj<transaction_active>(obj, reg);
323 break;
324 }
325 default:
326 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700327 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700328 }
329 return true;
330}
331
332// Explicitly instantiate all DoFieldPut functions.
333#define EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, _do_check, _transaction_active) \
334 template bool DoFieldPut<_find_type, _field_type, _do_check, _transaction_active>(Thread* self, \
335 const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data)
336
337#define EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(_find_type, _field_type) \
338 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, false); \
339 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, false); \
340 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, true); \
341 EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, true);
342
343// iput-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700344EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimBoolean)
345EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimByte)
346EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimChar)
347EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimShort)
348EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimInt)
349EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimLong)
350EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstanceObjectWrite, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700351
352// sput-XXX
Andreas Gampec8ccf682014-09-29 20:07:43 -0700353EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimBoolean)
354EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimByte)
355EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimChar)
356EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimShort)
357EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimInt)
358EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimLong)
359EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticObjectWrite, Primitive::kPrimNot)
Ian Rogers54874942014-06-10 16:31:03 -0700360
361#undef EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL
362#undef EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL
363
364template<Primitive::Type field_type, bool transaction_active>
365bool DoIPutQuick(const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) {
366 Object* obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data));
367 if (UNLIKELY(obj == nullptr)) {
368 // We lost the reference to the field index so we cannot get a more
369 // precised exception message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000370 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -0700371 return false;
372 }
373 MemberOffset field_offset(inst->VRegC_22c());
374 const uint32_t vregA = inst->VRegA_22c(inst_data);
375 // Report this field modification to instrumentation if needed. Since we only have the offset of
376 // the field from the base of the object, we need to look for it first.
377 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
378 if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
379 ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(),
380 field_offset.Uint32Value());
381 DCHECK(f != nullptr);
382 DCHECK(!f->IsStatic());
383 JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA);
384 instrumentation->FieldWriteEvent(Thread::Current(), obj, shadow_frame.GetMethod(),
385 shadow_frame.GetDexPC(), f, field_value);
386 }
387 // Note: iput-x-quick instructions are only for non-volatile fields.
388 switch (field_type) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700389 case Primitive::kPrimBoolean:
390 obj->SetFieldBoolean<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
391 break;
392 case Primitive::kPrimByte:
393 obj->SetFieldByte<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
394 break;
395 case Primitive::kPrimChar:
396 obj->SetFieldChar<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
397 break;
398 case Primitive::kPrimShort:
399 obj->SetFieldShort<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
400 break;
Ian Rogers54874942014-06-10 16:31:03 -0700401 case Primitive::kPrimInt:
402 obj->SetField32<transaction_active>(field_offset, shadow_frame.GetVReg(vregA));
403 break;
404 case Primitive::kPrimLong:
405 obj->SetField64<transaction_active>(field_offset, shadow_frame.GetVRegLong(vregA));
406 break;
407 case Primitive::kPrimNot:
408 obj->SetFieldObject<transaction_active>(field_offset, shadow_frame.GetVRegReference(vregA));
409 break;
410 default:
411 LOG(FATAL) << "Unreachable: " << field_type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700412 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700413 }
414 return true;
415}
416
417// Explicitly instantiate all DoIPutQuick functions.
418#define EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, _transaction_active) \
419 template bool DoIPutQuick<_field_type, _transaction_active>(const ShadowFrame& shadow_frame, \
420 const Instruction* inst, \
421 uint16_t inst_data)
422
423#define EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(_field_type) \
424 EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, false); \
425 EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, true);
426
Andreas Gampec8ccf682014-09-29 20:07:43 -0700427EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimInt) // iput-quick.
428EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimBoolean) // iput-boolean-quick.
429EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimByte) // iput-byte-quick.
430EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimChar) // iput-char-quick.
431EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimShort) // iput-short-quick.
432EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimLong) // iput-wide-quick.
433EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimNot) // iput-object-quick.
Ian Rogers54874942014-06-10 16:31:03 -0700434#undef EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL
435#undef EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL
436
Sebastien Hertz520633b2015-09-08 17:03:36 +0200437// We accept a null Instrumentation* meaning we must not report anything to the instrumentation.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700438uint32_t FindNextInstructionFollowingException(
439 Thread* self, ShadowFrame& shadow_frame, uint32_t dex_pc,
440 const instrumentation::Instrumentation* instrumentation) {
Ian Rogers54874942014-06-10 16:31:03 -0700441 self->VerifyStack();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700442 StackHandleScope<2> hs(self);
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000443 Handle<mirror::Throwable> exception(hs.NewHandle(self->GetException()));
Sebastien Hertz520633b2015-09-08 17:03:36 +0200444 if (instrumentation != nullptr && instrumentation->HasExceptionCaughtListeners()
Nicolas Geoffray7642cfc2015-02-26 10:56:09 +0000445 && self->IsExceptionThrownByCurrentMethod(exception.Get())) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000446 instrumentation->ExceptionCaughtEvent(self, exception.Get());
Sebastien Hertz9f102032014-05-23 08:59:42 +0200447 }
Ian Rogers54874942014-06-10 16:31:03 -0700448 bool clear_exception = false;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700449 uint32_t found_dex_pc = shadow_frame.GetMethod()->FindCatchBlock(
450 hs.NewHandle(exception->GetClass()), dex_pc, &clear_exception);
Sebastien Hertz520633b2015-09-08 17:03:36 +0200451 if (found_dex_pc == DexFile::kDexNoIndex && instrumentation != nullptr) {
Nicolas Geoffray7642cfc2015-02-26 10:56:09 +0000452 // Exception is not caught by the current method. We will unwind to the
453 // caller. Notify any instrumentation listener.
Sebastien Hertz9f102032014-05-23 08:59:42 +0200454 instrumentation->MethodUnwindEvent(self, shadow_frame.GetThisObject(),
Ian Rogers54874942014-06-10 16:31:03 -0700455 shadow_frame.GetMethod(), dex_pc);
456 } else {
Nicolas Geoffray7642cfc2015-02-26 10:56:09 +0000457 // Exception is caught in the current method. We will jump to the found_dex_pc.
Ian Rogers54874942014-06-10 16:31:03 -0700458 if (clear_exception) {
459 self->ClearException();
460 }
461 }
462 return found_dex_pc;
463}
464
Ian Rogerse94652f2014-12-02 11:13:19 -0800465void UnexpectedOpcode(const Instruction* inst, const ShadowFrame& shadow_frame) {
466 LOG(FATAL) << "Unexpected instruction: "
467 << inst->DumpString(shadow_frame.GetMethod()->GetDexFile());
468 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700469}
470
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200471// Assign register 'src_reg' from shadow_frame to register 'dest_reg' into new_shadow_frame.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800472static inline void AssignRegister(ShadowFrame* new_shadow_frame, const ShadowFrame& shadow_frame,
473 size_t dest_reg, size_t src_reg)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700474 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe7104cbf2014-03-21 11:44:43 -0700475 // Uint required, so that sign extension does not make this wrong on 64b systems
476 uint32_t src_value = shadow_frame.GetVReg(src_reg);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800477 mirror::Object* o = shadow_frame.GetVRegReference<kVerifyNone>(src_reg);
Igor Murashkinc449e8b2015-06-10 15:56:42 -0700478
479 // If both register locations contains the same value, the register probably holds a reference.
480 // Note: As an optimization, non-moving collectors leave a stale reference value
481 // in the references array even after the original vreg was overwritten to a non-reference.
Andreas Gampe7104cbf2014-03-21 11:44:43 -0700482 if (src_value == reinterpret_cast<uintptr_t>(o)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800483 new_shadow_frame->SetVRegReference(dest_reg, o);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200484 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800485 new_shadow_frame->SetVReg(dest_reg, src_value);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200486 }
487}
488
Sebastien Hertz45b15972015-04-03 16:07:05 +0200489void AbortTransactionF(Thread* self, const char* fmt, ...) {
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700490 va_list args;
491 va_start(args, fmt);
Sebastien Hertz45b15972015-04-03 16:07:05 +0200492 AbortTransactionV(self, fmt, args);
493 va_end(args);
494}
495
496void AbortTransactionV(Thread* self, const char* fmt, va_list args) {
497 CHECK(Runtime::Current()->IsActiveTransaction());
498 // Constructs abort message.
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100499 std::string abort_msg;
500 StringAppendV(&abort_msg, fmt, args);
501 // Throws an exception so we can abort the transaction and rollback every change.
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200502 Runtime::Current()->AbortTransactionAndThrowAbortError(self, abort_msg);
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700503}
504
Igor Murashkin158f35c2015-06-10 15:55:30 -0700505// Separate declaration is required solely for the attributes.
Igor Murashkin6918bf12015-09-27 19:19:06 -0700506template <bool is_range,
Narayan Kamath370423d2016-10-03 16:51:22 +0100507 bool do_assignability_check>
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700508 REQUIRES_SHARED(Locks::mutator_lock_)
Igor Murashkin158f35c2015-06-10 15:55:30 -0700509static inline bool DoCallCommon(ArtMethod* called_method,
510 Thread* self,
511 ShadowFrame& shadow_frame,
512 JValue* result,
513 uint16_t number_of_inputs,
Narayan Kamath370423d2016-10-03 16:51:22 +0100514 uint32_t (&arg)[Instruction::kMaxVarArgRegs],
Igor Murashkin158f35c2015-06-10 15:55:30 -0700515 uint32_t vregC) ALWAYS_INLINE;
516
Siva Chandra05d24152016-01-05 17:43:17 -0800517void ArtInterpreterToCompiledCodeBridge(Thread* self,
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100518 ArtMethod* caller,
Siva Chandra05d24152016-01-05 17:43:17 -0800519 const DexFile::CodeItem* code_item,
520 ShadowFrame* shadow_frame,
521 JValue* result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700522 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700523 ArtMethod* method = shadow_frame->GetMethod();
524 // Ensure static methods are initialized.
525 if (method->IsStatic()) {
526 mirror::Class* declaringClass = method->GetDeclaringClass();
527 if (UNLIKELY(!declaringClass->IsInitialized())) {
528 self->PushShadowFrame(shadow_frame);
529 StackHandleScope<1> hs(self);
530 Handle<mirror::Class> h_class(hs.NewHandle(declaringClass));
531 if (UNLIKELY(!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true,
532 true))) {
533 self->PopShadowFrame();
534 DCHECK(self->IsExceptionPending());
535 return;
536 }
537 self->PopShadowFrame();
538 CHECK(h_class->IsInitializing());
539 // Reload from shadow frame in case the method moved, this is faster than adding a handle.
540 method = shadow_frame->GetMethod();
541 }
542 }
543 uint16_t arg_offset = (code_item == nullptr)
544 ? 0
545 : code_item->registers_size_ - code_item->ins_size_;
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100546 jit::Jit* jit = Runtime::Current()->GetJit();
547 if (jit != nullptr && caller != nullptr) {
548 jit->NotifyInterpreterToCompiledCodeTransition(self, caller);
549 }
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700550 method->Invoke(self, shadow_frame->GetVRegArgs(arg_offset),
551 (shadow_frame->NumberOfVRegs() - arg_offset) * sizeof(uint32_t),
Andreas Gampe542451c2016-07-26 09:02:02 -0700552 result, method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty());
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700553}
554
Mingyao Yangffedec52016-05-19 10:48:40 -0700555void SetStringInitValueToAllAliases(ShadowFrame* shadow_frame,
556 uint16_t this_obj_vreg,
557 JValue result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700558 REQUIRES_SHARED(Locks::mutator_lock_) {
Mingyao Yangffedec52016-05-19 10:48:40 -0700559 Object* existing = shadow_frame->GetVRegReference(this_obj_vreg);
560 if (existing == nullptr) {
561 // If it's null, we come from compiled code that was deoptimized. Nothing to do,
562 // as the compiler verified there was no alias.
563 // Set the new string result of the StringFactory.
564 shadow_frame->SetVRegReference(this_obj_vreg, result.GetL());
565 return;
566 }
567 // Set the string init result into all aliases.
568 for (uint32_t i = 0, e = shadow_frame->NumberOfVRegs(); i < e; ++i) {
569 if (shadow_frame->GetVRegReference(i) == existing) {
570 DCHECK_EQ(shadow_frame->GetVRegReference(i),
571 reinterpret_cast<mirror::Object*>(shadow_frame->GetVReg(i)));
572 shadow_frame->SetVRegReference(i, result.GetL());
573 DCHECK_EQ(shadow_frame->GetVRegReference(i),
574 reinterpret_cast<mirror::Object*>(shadow_frame->GetVReg(i)));
575 }
576 }
577}
578
Igor Murashkin6918bf12015-09-27 19:19:06 -0700579template <bool is_range,
Narayan Kamath370423d2016-10-03 16:51:22 +0100580 bool do_assignability_check>
Igor Murashkin158f35c2015-06-10 15:55:30 -0700581static inline bool DoCallCommon(ArtMethod* called_method,
582 Thread* self,
583 ShadowFrame& shadow_frame,
584 JValue* result,
585 uint16_t number_of_inputs,
Narayan Kamath370423d2016-10-03 16:51:22 +0100586 uint32_t (&arg)[Instruction::kMaxVarArgRegs],
Igor Murashkin158f35c2015-06-10 15:55:30 -0700587 uint32_t vregC) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800588 bool string_init = false;
589 // Replace calls to String.<init> with equivalent StringFactory call.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700590 if (UNLIKELY(called_method->GetDeclaringClass()->IsStringClass()
591 && called_method->IsConstructor())) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100592 called_method = WellKnownClasses::StringInitToStringFactory(called_method);
Jeff Hao848f70a2014-01-15 13:49:50 -0800593 string_init = true;
594 }
595
Alex Lightdaf58c82016-03-16 23:00:49 +0000596 // Compute method information.
597 const DexFile::CodeItem* code_item = called_method->GetCodeItem();
Igor Murashkin158f35c2015-06-10 15:55:30 -0700598
599 // Number of registers for the callee's call frame.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200600 uint16_t num_regs;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700601 if (LIKELY(code_item != nullptr)) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200602 num_regs = code_item->registers_size_;
Igor Murashkin158f35c2015-06-10 15:55:30 -0700603 DCHECK_EQ(string_init ? number_of_inputs - 1 : number_of_inputs, code_item->ins_size_);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200604 } else {
Ian Rogerse94652f2014-12-02 11:13:19 -0800605 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
Igor Murashkin158f35c2015-06-10 15:55:30 -0700606 num_regs = number_of_inputs;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200607 }
608
Igor Murashkin158f35c2015-06-10 15:55:30 -0700609 // Hack for String init:
610 //
611 // Rewrite invoke-x java.lang.String.<init>(this, a, b, c, ...) into:
612 // invoke-x StringFactory(a, b, c, ...)
613 // by effectively dropping the first virtual register from the invoke.
614 //
615 // (at this point the ArtMethod has already been replaced,
616 // so we just need to fix-up the arguments)
David Brazdil65902e82016-01-15 09:35:13 +0000617 //
618 // Note that FindMethodFromCode in entrypoint_utils-inl.h was also special-cased
619 // to handle the compiler optimization of replacing `this` with null without
620 // throwing NullPointerException.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700621 uint32_t string_init_vreg_this = is_range ? vregC : arg[0];
Igor Murashkina06b49b2015-06-25 15:18:12 -0700622 if (UNLIKELY(string_init)) {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700623 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 -0700624
Igor Murashkin158f35c2015-06-10 15:55:30 -0700625 // The new StringFactory call is static and has one fewer argument.
Igor Murashkina06b49b2015-06-25 15:18:12 -0700626 if (code_item == nullptr) {
627 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
628 num_regs--;
629 } // 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 -0700630 number_of_inputs--;
631
632 // Rewrite the var-args, dropping the 0th argument ("this")
Igor Murashkin6918bf12015-09-27 19:19:06 -0700633 for (uint32_t i = 1; i < arraysize(arg); ++i) {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700634 arg[i - 1] = arg[i];
635 }
Igor Murashkin6918bf12015-09-27 19:19:06 -0700636 arg[arraysize(arg) - 1] = 0;
Igor Murashkin158f35c2015-06-10 15:55:30 -0700637
638 // Rewrite the non-var-arg case
639 vregC++; // Skips the 0th vreg in the range ("this").
640 }
641
642 // Parameter registers go at the end of the shadow frame.
643 DCHECK_GE(num_regs, number_of_inputs);
644 size_t first_dest_reg = num_regs - number_of_inputs;
645 DCHECK_NE(first_dest_reg, (size_t)-1);
646
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200647 // Allocate shadow frame on the stack.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700648 const char* old_cause = self->StartAssertNoThreadSuspension("DoCallCommon");
Andreas Gampeb3025922015-09-01 14:45:00 -0700649 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
Andreas Gampe03ec9302015-08-27 17:41:47 -0700650 CREATE_SHADOW_FRAME(num_regs, &shadow_frame, called_method, /* dex pc */ 0);
Andreas Gampeb3025922015-09-01 14:45:00 -0700651 ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get();
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200652
Igor Murashkin158f35c2015-06-10 15:55:30 -0700653 // Initialize new shadow frame by copying the registers from the callee shadow frame.
Jeff Haoa3faaf42013-09-03 19:07:00 -0700654 if (do_assignability_check) {
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -0700655 // Slow path.
656 // We might need to do class loading, which incurs a thread state change to kNative. So
657 // register the shadow frame as under construction and allow suspension again.
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700658 ScopedStackedShadowFramePusher pusher(
Sebastien Hertzf7958692015-06-09 14:09:14 +0200659 self, new_shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -0700660 self->EndAssertNoThreadSuspension(old_cause);
661
Igor Murashkin9f95ba72016-02-01 14:21:25 -0800662 // ArtMethod here is needed to check type information of the call site against the callee.
663 // Type information is retrieved from a DexFile/DexCache for that respective declared method.
664 //
665 // As a special case for proxy methods, which are not dex-backed,
666 // we have to retrieve type information from the proxy's method
667 // interface method instead (which is dex backed since proxies are never interfaces).
Andreas Gampe542451c2016-07-26 09:02:02 -0700668 ArtMethod* method =
669 new_shadow_frame->GetMethod()->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Igor Murashkin9f95ba72016-02-01 14:21:25 -0800670
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -0700671 // We need to do runtime check on reference assignment. We need to load the shorty
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200672 // to get the exact type of each reference argument.
Igor Murashkin9f95ba72016-02-01 14:21:25 -0800673 const DexFile::TypeList* params = method->GetParameterTypeList();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700674 uint32_t shorty_len = 0;
Igor Murashkin9f95ba72016-02-01 14:21:25 -0800675 const char* shorty = method->GetShorty(&shorty_len);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200676
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100677 // Handle receiver apart since it's not part of the shorty.
678 size_t dest_reg = first_dest_reg;
679 size_t arg_offset = 0;
Igor Murashkin158f35c2015-06-10 15:55:30 -0700680
Igor Murashkin9f95ba72016-02-01 14:21:25 -0800681 if (!method->IsStatic()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700682 size_t receiver_reg = is_range ? vregC : arg[0];
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100683 new_shadow_frame->SetVRegReference(dest_reg, shadow_frame.GetVRegReference(receiver_reg));
684 ++dest_reg;
685 ++arg_offset;
Igor Murashkina06b49b2015-06-25 15:18:12 -0700686 DCHECK(!string_init); // All StringFactory methods are static.
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100687 }
Igor Murashkin158f35c2015-06-10 15:55:30 -0700688
689 // Copy the caller's invoke-* arguments into the callee's parameter registers.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800690 for (uint32_t shorty_pos = 0; dest_reg < num_regs; ++shorty_pos, ++dest_reg, ++arg_offset) {
Igor Murashkina06b49b2015-06-25 15:18:12 -0700691 // Skip the 0th 'shorty' type since it represents the return type.
692 DCHECK_LT(shorty_pos + 1, shorty_len) << "for shorty '" << shorty << "'";
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200693 const size_t src_reg = (is_range) ? vregC + arg_offset : arg[arg_offset];
694 switch (shorty[shorty_pos + 1]) {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700695 // Handle Object references. 1 virtual register slot.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200696 case 'L': {
697 Object* o = shadow_frame.GetVRegReference(src_reg);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700698 if (do_assignability_check && o != nullptr) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700699 PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Ian Rogersa0485602014-12-02 15:48:04 -0800700 Class* arg_type =
Igor Murashkin9f95ba72016-02-01 14:21:25 -0800701 method->GetClassFromTypeIndex(
Vladimir Marko05792b92015-08-03 11:56:49 +0100702 params->GetTypeItem(shorty_pos).type_idx_, true /* resolve */, pointer_size);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700703 if (arg_type == nullptr) {
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200704 CHECK(self->IsExceptionPending());
705 return false;
706 }
707 if (!o->VerifierInstanceOf(arg_type)) {
708 // This should never happen.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700709 std::string temp1, temp2;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000710 self->ThrowNewExceptionF("Ljava/lang/VirtualMachineError;",
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200711 "Invoking %s with bad arg %d, type '%s' not instance of '%s'",
Ian Rogerse94652f2014-12-02 11:13:19 -0800712 new_shadow_frame->GetMethod()->GetName(), shorty_pos,
Ian Rogers1ff3c982014-08-12 02:30:58 -0700713 o->GetClass()->GetDescriptor(&temp1),
714 arg_type->GetDescriptor(&temp2));
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200715 return false;
716 }
Jeff Haoa3faaf42013-09-03 19:07:00 -0700717 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200718 new_shadow_frame->SetVRegReference(dest_reg, o);
719 break;
Jeff Haoa3faaf42013-09-03 19:07:00 -0700720 }
Igor Murashkin158f35c2015-06-10 15:55:30 -0700721 // Handle doubles and longs. 2 consecutive virtual register slots.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200722 case 'J': case 'D': {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700723 uint64_t wide_value =
724 (static_cast<uint64_t>(shadow_frame.GetVReg(src_reg + 1)) << BitSizeOf<uint32_t>()) |
725 static_cast<uint32_t>(shadow_frame.GetVReg(src_reg));
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200726 new_shadow_frame->SetVRegLong(dest_reg, wide_value);
Igor Murashkin158f35c2015-06-10 15:55:30 -0700727 // Skip the next virtual register slot since we already used it.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200728 ++dest_reg;
729 ++arg_offset;
730 break;
731 }
Igor Murashkin158f35c2015-06-10 15:55:30 -0700732 // Handle all other primitives that are always 1 virtual register slot.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200733 default:
734 new_shadow_frame->SetVReg(dest_reg, shadow_frame.GetVReg(src_reg));
735 break;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200736 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200737 }
738 } else {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700739 size_t arg_index = 0;
740
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200741 // Fast path: no extra checks.
742 if (is_range) {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700743 uint16_t first_src_reg = vregC;
744
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200745 for (size_t src_reg = first_src_reg, dest_reg = first_dest_reg; dest_reg < num_regs;
746 ++dest_reg, ++src_reg) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800747 AssignRegister(new_shadow_frame, shadow_frame, dest_reg, src_reg);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200748 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200749 } else {
Igor Murashkin6918bf12015-09-27 19:19:06 -0700750 DCHECK_LE(number_of_inputs, arraysize(arg));
Igor Murashkin158f35c2015-06-10 15:55:30 -0700751
752 for (; arg_index < number_of_inputs; ++arg_index) {
753 AssignRegister(new_shadow_frame, shadow_frame, first_dest_reg + arg_index, arg[arg_index]);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +0200754 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200755 }
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -0700756 self->EndAssertNoThreadSuspension(old_cause);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200757 }
758
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200759 // Do the call now.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200760 if (LIKELY(Runtime::Current()->IsStarted())) {
Tamas Berghammerdd5e5e92016-02-12 16:29:00 +0000761 ArtMethod* target = new_shadow_frame->GetMethod();
762 if (ClassLinker::ShouldUseInterpreterEntrypoint(
763 target,
764 target->GetEntryPointFromQuickCompiledCode())) {
Tamas Berghammerc94a61f2016-02-05 18:09:08 +0000765 ArtInterpreterToInterpreterBridge(self, code_item, new_shadow_frame, result);
Tamas Berghammer3a98aae2016-02-08 20:21:54 +0000766 } else {
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100767 ArtInterpreterToCompiledCodeBridge(
768 self, shadow_frame.GetMethod(), code_item, new_shadow_frame, result);
Nicolas Geoffray7070ccd2015-07-08 09:41:54 +0000769 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200770 } else {
Andreas Gampe799681b2015-05-15 19:24:12 -0700771 UnstartedRuntime::Invoke(self, code_item, new_shadow_frame, result, first_dest_reg);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200772 }
Jeff Hao848f70a2014-01-15 13:49:50 -0800773
774 if (string_init && !self->IsExceptionPending()) {
Mingyao Yangffedec52016-05-19 10:48:40 -0700775 SetStringInitValueToAllAliases(&shadow_frame, string_init_vreg_this, *result);
Jeff Hao848f70a2014-01-15 13:49:50 -0800776 }
777
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200778 return !self->IsExceptionPending();
779}
780
Igor Murashkin158f35c2015-06-10 15:55:30 -0700781template<bool is_range, bool do_assignability_check>
Igor Murashkin158f35c2015-06-10 15:55:30 -0700782bool DoCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame,
783 const Instruction* inst, uint16_t inst_data, JValue* result) {
784 // Argument word count.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +0100785 const uint16_t number_of_inputs =
786 (is_range) ? inst->VRegA_3rc(inst_data) : inst->VRegA_35c(inst_data);
Igor Murashkin158f35c2015-06-10 15:55:30 -0700787
788 // TODO: find a cleaner way to separate non-range and range information without duplicating
789 // code.
Igor Murashkin6918bf12015-09-27 19:19:06 -0700790 uint32_t arg[Instruction::kMaxVarArgRegs] = {}; // only used in invoke-XXX.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700791 uint32_t vregC = 0;
792 if (is_range) {
793 vregC = inst->VRegC_3rc();
794 } else {
795 vregC = inst->VRegC_35c();
796 inst->GetVarArgs(arg, inst_data);
797 }
798
799 return DoCallCommon<is_range, do_assignability_check>(
800 called_method, self, shadow_frame,
801 result, number_of_inputs, arg, vregC);
802}
803
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100804template <bool is_range, bool do_access_check, bool transaction_active>
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200805bool DoFilledNewArray(const Instruction* inst, const ShadowFrame& shadow_frame,
806 Thread* self, JValue* result) {
807 DCHECK(inst->Opcode() == Instruction::FILLED_NEW_ARRAY ||
808 inst->Opcode() == Instruction::FILLED_NEW_ARRAY_RANGE);
809 const int32_t length = is_range ? inst->VRegA_3rc() : inst->VRegA_35c();
810 if (!is_range) {
811 // Checks FILLED_NEW_ARRAY's length does not exceed 5 arguments.
812 CHECK_LE(length, 5);
813 }
814 if (UNLIKELY(length < 0)) {
815 ThrowNegativeArraySizeException(length);
816 return false;
817 }
818 uint16_t type_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700819 Class* array_class = ResolveVerifyAndClinit(type_idx, shadow_frame.GetMethod(),
820 self, false, do_access_check);
821 if (UNLIKELY(array_class == nullptr)) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200822 DCHECK(self->IsExceptionPending());
823 return false;
824 }
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700825 CHECK(array_class->IsArrayClass());
826 Class* component_class = array_class->GetComponentType();
827 const bool is_primitive_int_component = component_class->IsPrimitiveInt();
828 if (UNLIKELY(component_class->IsPrimitive() && !is_primitive_int_component)) {
829 if (component_class->IsPrimitiveLong() || component_class->IsPrimitiveDouble()) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200830 ThrowRuntimeException("Bad filled array request for type %s",
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700831 PrettyDescriptor(component_class).c_str());
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200832 } else {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000833 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
Brian Carlstrom4fa0bcd2013-12-10 11:24:21 -0800834 "Found type %s; filled-new-array not implemented for anything but 'int'",
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700835 PrettyDescriptor(component_class).c_str());
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200836 }
837 return false;
838 }
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700839 Object* new_array = Array::Alloc<true>(self, array_class, length,
840 array_class->GetComponentSizeShift(),
841 Runtime::Current()->GetHeap()->GetCurrentAllocator());
842 if (UNLIKELY(new_array == nullptr)) {
843 self->AssertPendingOOMException();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200844 return false;
845 }
Igor Murashkin158f35c2015-06-10 15:55:30 -0700846 uint32_t arg[Instruction::kMaxVarArgRegs]; // only used in filled-new-array.
847 uint32_t vregC = 0; // only used in filled-new-array-range.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200848 if (is_range) {
Sebastien Hertzabff6432014-01-27 18:01:39 +0100849 vregC = inst->VRegC_3rc();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200850 } else {
Ian Rogers29a26482014-05-02 15:27:29 -0700851 inst->GetVarArgs(arg);
Sebastien Hertzabff6432014-01-27 18:01:39 +0100852 }
Sebastien Hertzabff6432014-01-27 18:01:39 +0100853 for (int32_t i = 0; i < length; ++i) {
854 size_t src_reg = is_range ? vregC + i : arg[i];
855 if (is_primitive_int_component) {
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700856 new_array->AsIntArray()->SetWithoutChecks<transaction_active>(
857 i, shadow_frame.GetVReg(src_reg));
Sebastien Hertzabff6432014-01-27 18:01:39 +0100858 } else {
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700859 new_array->AsObjectArray<Object>()->SetWithoutChecks<transaction_active>(
860 i, shadow_frame.GetVRegReference(src_reg));
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200861 }
862 }
863
Mathieu Chartier52ea33b2015-06-18 16:48:52 -0700864 result->SetL(new_array);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200865 return true;
866}
867
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700868// TODO fix thread analysis: should be REQUIRES_SHARED(Locks::mutator_lock_).
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100869template<typename T>
870static void RecordArrayElementsInTransactionImpl(mirror::PrimitiveArray<T>* array, int32_t count)
871 NO_THREAD_SAFETY_ANALYSIS {
872 Runtime* runtime = Runtime::Current();
873 for (int32_t i = 0; i < count; ++i) {
874 runtime->RecordWriteArray(array, i, array->GetWithoutChecks(i));
875 }
876}
877
878void RecordArrayElementsInTransaction(mirror::Array* array, int32_t count)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700879 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100880 DCHECK(Runtime::Current()->IsActiveTransaction());
881 DCHECK(array != nullptr);
882 DCHECK_LE(count, array->GetLength());
883 Primitive::Type primitive_component_type = array->GetClass()->GetComponentType()->GetPrimitiveType();
884 switch (primitive_component_type) {
885 case Primitive::kPrimBoolean:
886 RecordArrayElementsInTransactionImpl(array->AsBooleanArray(), count);
887 break;
888 case Primitive::kPrimByte:
889 RecordArrayElementsInTransactionImpl(array->AsByteArray(), count);
890 break;
891 case Primitive::kPrimChar:
892 RecordArrayElementsInTransactionImpl(array->AsCharArray(), count);
893 break;
894 case Primitive::kPrimShort:
895 RecordArrayElementsInTransactionImpl(array->AsShortArray(), count);
896 break;
897 case Primitive::kPrimInt:
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100898 RecordArrayElementsInTransactionImpl(array->AsIntArray(), count);
899 break;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700900 case Primitive::kPrimFloat:
901 RecordArrayElementsInTransactionImpl(array->AsFloatArray(), count);
902 break;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100903 case Primitive::kPrimLong:
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100904 RecordArrayElementsInTransactionImpl(array->AsLongArray(), count);
905 break;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700906 case Primitive::kPrimDouble:
907 RecordArrayElementsInTransactionImpl(array->AsDoubleArray(), count);
908 break;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100909 default:
910 LOG(FATAL) << "Unsupported primitive type " << primitive_component_type
911 << " in fill-array-data";
912 break;
913 }
914}
915
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200916// Explicit DoCall template function declarations.
Sebastien Hertzc6714852013-09-30 16:42:32 +0200917#define EXPLICIT_DO_CALL_TEMPLATE_DECL(_is_range, _do_assignability_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700918 template REQUIRES_SHARED(Locks::mutator_lock_) \
Sebastien Hertz9119c5f2013-12-16 11:31:45 +0100919 bool DoCall<_is_range, _do_assignability_check>(ArtMethod* method, Thread* self, \
920 ShadowFrame& shadow_frame, \
Sebastien Hertzc6714852013-09-30 16:42:32 +0200921 const Instruction* inst, uint16_t inst_data, \
922 JValue* result)
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200923EXPLICIT_DO_CALL_TEMPLATE_DECL(false, false);
924EXPLICIT_DO_CALL_TEMPLATE_DECL(false, true);
925EXPLICIT_DO_CALL_TEMPLATE_DECL(true, false);
926EXPLICIT_DO_CALL_TEMPLATE_DECL(true, true);
927#undef EXPLICIT_DO_CALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200928
929// Explicit DoFilledNewArray template function declarations.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100930#define EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(_is_range_, _check, _transaction_active) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700931 template REQUIRES_SHARED(Locks::mutator_lock_) \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100932 bool DoFilledNewArray<_is_range_, _check, _transaction_active>(const Instruction* inst, \
933 const ShadowFrame& shadow_frame, \
934 Thread* self, JValue* result)
935#define EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(_transaction_active) \
936 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, false, _transaction_active); \
937 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, true, _transaction_active); \
938 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, false, _transaction_active); \
939 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, true, _transaction_active)
940EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(false);
941EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(true);
942#undef EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200943#undef EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL
944
945} // namespace interpreter
946} // namespace art