blob: 33d2e59bb2c7ca6dbd06020e1c0667a2ae6bb399 [file] [log] [blame]
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001/*
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.h"
18
19#include <math.h>
20
21#include "common_throws.h"
jeffhao373c52f2012-11-20 16:11:52 -080022#include "debugger.h"
Ian Rogers2fa6b2e2012-10-17 00:10:17 -070023#include "dex_instruction.h"
24#include "invoke_arg_array_builder.h"
25#include "logging.h"
Ian Rogers64b6d142012-10-29 16:34:15 -070026#include "nth_caller_visitor.h"
Ian Rogers2fa6b2e2012-10-17 00:10:17 -070027#include "object.h"
28#include "object_utils.h"
29#include "runtime_support.h"
30#include "ScopedLocalRef.h"
31#include "scoped_thread_state_change.h"
32#include "thread.h"
33
34namespace art {
35namespace interpreter {
36
Ian Rogers64b6d142012-10-29 16:34:15 -070037static void UnstartedRuntimeInvoke(Thread* self, AbstractMethod* target_method,
38 Object* receiver, JValue* args, JValue* result)
39 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
40 // In a runtime that's not started we intercept certain methods to avoid complicated dependency
41 // problems in core libraries.
42 std::string name(PrettyMethod(target_method));
43 if (name == "java.lang.Class java.lang.Class.forName(java.lang.String)") {
44 std::string descriptor(DotToDescriptor(args[0].GetL()->AsString()->ToModifiedUtf8().c_str()));
45 ClassLoader* class_loader = NULL; // shadow_frame.GetMethod()->GetDeclaringClass()->GetClassLoader();
46 Class* found = Runtime::Current()->GetClassLinker()->FindClass(descriptor.c_str(),
47 class_loader);
48 CHECK(found != NULL) << "Class.forName failed in un-started runtime for class: "
49 << PrettyDescriptor(descriptor);
50 result->SetL(found);
51 } else if (name == "java.lang.Object java.lang.Class.newInstance()") {
52 Class* klass = receiver->AsClass();
53 AbstractMethod* c = klass->FindDeclaredDirectMethod("<init>", "()V");
54 CHECK(c != NULL);
55 Object* obj = klass->AllocObject(self);
56 CHECK(obj != NULL);
57 EnterInterpreterFromInvoke(self, c, obj, NULL, NULL);
58 result->SetL(obj);
59 } else if (name == "java.lang.reflect.Field java.lang.Class.getDeclaredField(java.lang.String)") {
60 // Special managed code cut-out to allow field lookup in a un-started runtime that'd fail
61 // going the reflective Dex way.
62 Class* klass = receiver->AsClass();
63 String* name = args[0].GetL()->AsString();
64 Field* found = NULL;
65 FieldHelper fh;
66 ObjectArray<Field>* fields = klass->GetIFields();
67 for (int32_t i = 0; i < fields->GetLength() && found == NULL; ++i) {
68 Field* f = fields->Get(i);
69 fh.ChangeField(f);
70 if (name->Equals(fh.GetName())) {
71 found = f;
72 }
73 }
74 if (found == NULL) {
75 fields = klass->GetSFields();
76 for (int32_t i = 0; i < fields->GetLength() && found == NULL; ++i) {
77 Field* f = fields->Get(i);
78 fh.ChangeField(f);
79 if (name->Equals(fh.GetName())) {
80 found = f;
81 }
82 }
83 }
84 CHECK(found != NULL)
85 << "Failed to find field in Class.getDeclaredField in un-started runtime. name="
86 << name->ToModifiedUtf8() << " class=" << PrettyDescriptor(klass);
87 // TODO: getDeclaredField calls GetType once the field is found to ensure a
88 // NoClassDefFoundError is thrown if the field's type cannot be resolved.
89 result->SetL(found);
90 } else if (name == "void java.lang.System.arraycopy(java.lang.Object, int, java.lang.Object, int, int)") {
91 // Special case array copying without initializing System.
92 Class* ctype = args[0].GetL()->GetClass()->GetComponentType();
93 jint srcPos = args[1].GetI();
94 jint dstPos = args[3].GetI();
95 jint length = args[4].GetI();
96 if (!ctype->IsPrimitive()) {
97 ObjectArray<Object>* src = args[0].GetL()->AsObjectArray<Object>();
98 ObjectArray<Object>* dst = args[2].GetL()->AsObjectArray<Object>();
99 for (jint i = 0; i < length; ++i) {
100 dst->Set(dstPos + i, src->Get(srcPos + i));
101 }
102 } else if (ctype->IsPrimitiveChar()) {
103 CharArray* src = args[0].GetL()->AsCharArray();
104 CharArray* dst = args[2].GetL()->AsCharArray();
105 for (jint i = 0; i < length; ++i) {
106 dst->Set(dstPos + i, src->Get(srcPos + i));
107 }
108 } else if (ctype->IsPrimitiveInt()) {
109 IntArray* src = args[0].GetL()->AsIntArray();
110 IntArray* dst = args[2].GetL()->AsIntArray();
111 for (jint i = 0; i < length; ++i) {
112 dst->Set(dstPos + i, src->Get(srcPos + i));
113 }
114 } else {
115 UNIMPLEMENTED(FATAL) << "System.arraycopy of unexpected type: " << PrettyDescriptor(ctype);
116 }
117 } else {
118 // Not special, continue with regular interpreter execution.
119 EnterInterpreterFromInvoke(self, target_method, receiver, args, result);
120 }
121}
122
123// Hand select a number of methods to be run in a not yet started runtime without using JNI.
124static void UnstartedRuntimeJni(Thread* self, AbstractMethod* method,
125 Object* receiver, JValue* args, JValue* result)
126 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
127 std::string name(PrettyMethod(method));
128 if (name == "java.lang.ClassLoader dalvik.system.VMStack.getCallingClassLoader()") {
129 result->SetL(NULL);
130 } else if (name == "java.lang.Class dalvik.system.VMStack.getStackClass2()") {
131 NthCallerVisitor visitor(self->GetManagedStack(), NULL, 3);
132 visitor.WalkStack();
133 result->SetL(visitor.caller->GetDeclaringClass());
134 } else if (name == "double java.lang.Math.log(double)") {
135 result->SetD(log(args[0].GetD()));
136 } else if (name == "java.lang.String java.lang.Class.getNameNative()") {
137 result->SetL(receiver->AsClass()->ComputeName());
138 } else if (name == "int java.lang.Float.floatToRawIntBits(float)") {
139 result->SetI(args[0].GetI());
140 } else if (name == "float java.lang.Float.intBitsToFloat(int)") {
141 result->SetF(args[0].GetF());
142 } else if (name == "double java.lang.Math.exp(double)") {
143 result->SetD(exp(args[0].GetD()));
144 } else if (name == "java.lang.Object java.lang.Object.internalClone()") {
145 result->SetL(receiver->Clone(self));
146 } else if (name == "void java.lang.Object.notifyAll()") {
147 receiver->NotifyAll();
148 } else if (name == "int java.lang.String.compareTo(java.lang.String)") {
149 String* rhs = args[0].GetL()->AsString();
150 CHECK(rhs != NULL);
151 result->SetI(receiver->AsString()->CompareTo(rhs));
152 } else if (name == "java.lang.String java.lang.String.intern()") {
153 result->SetL(receiver->AsString()->Intern());
154 } else if (name == "int java.lang.String.fastIndexOf(int, int)") {
155 result->SetI(receiver->AsString()->FastIndexOf(args[0].GetI(), args[1].GetI()));
156 } else if (name == "java.lang.Object java.lang.reflect.Array.createMultiArray(java.lang.Class, int[])") {
157 result->SetL(Array::CreateMultiArray(self, args[0].GetL()->AsClass(), args[1].GetL()->AsIntArray()));
158 } else if (name == "java.lang.Object java.lang.Throwable.nativeFillInStackTrace()") {
159 ScopedObjectAccessUnchecked soa(self);
160 result->SetL(soa.Decode<Object*>(self->CreateInternalStackTrace(soa)));
161 } else if (name == "boolean java.nio.ByteOrder.isLittleEndian()") {
162 result->SetJ(JNI_TRUE);
163 } else if (name == "boolean sun.misc.Unsafe.compareAndSwapInt(java.lang.Object, long, int, int)") {
164 Object* obj = args[0].GetL();
165 jlong offset = args[1].GetJ();
166 jint expectedValue = args[2].GetI();
167 jint newValue = args[3].GetI();
168 byte* raw_addr = reinterpret_cast<byte*>(obj) + offset;
169 volatile int32_t* address = reinterpret_cast<volatile int32_t*>(raw_addr);
170 // Note: android_atomic_release_cas() returns 0 on success, not failure.
171 int r = android_atomic_release_cas(expectedValue, newValue, address);
172 result->SetZ(r == 0);
173 } else if (name == "void sun.misc.Unsafe.putObject(java.lang.Object, long, java.lang.Object)") {
174 Object* obj = args[0].GetL();
175 Object* newValue = args[2].GetL();
176 obj->SetFieldObject(MemberOffset(args[1].GetJ()), newValue, false);
177 } else {
178 LOG(FATAL) << "Attempt to invoke native method in non-started runtime: " << name;
179 }
180}
181
182static void InterpreterJni(Thread* self, AbstractMethod* method, StringPiece shorty,
183 Object* receiver, JValue* args, JValue* result)
184 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
185 // TODO: The following enters JNI code using a typedef-ed function rather than the JNI compiler,
186 // it should be removed and JNI compiled stubs used instead.
187 ScopedObjectAccessUnchecked soa(self);
188 if (method->IsStatic()) {
189 if (shorty == "L") {
190 typedef jobject (fnptr)(JNIEnv*, jclass);
191 fnptr* fn = reinterpret_cast<fnptr*>(method->GetNativeMethod());
192 ScopedLocalRef<jclass> klass(soa.Env(),
193 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
Ian Rogers556d6372012-11-20 12:19:36 -0800194 jobject jresult;
195 {
196 ScopedThreadStateChange tsc(self, kNative);
197 jresult = fn(soa.Env(), klass.get());
198 }
199 result->SetL(soa.Decode<Object*>(jresult));
Ian Rogers64b6d142012-10-29 16:34:15 -0700200 } else if (shorty == "V") {
201 typedef void (fnptr)(JNIEnv*, jclass);
202 fnptr* fn = reinterpret_cast<fnptr*>(method->GetNativeMethod());
203 ScopedLocalRef<jclass> klass(soa.Env(),
204 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
205 ScopedThreadStateChange tsc(self, kNative);
206 fn(soa.Env(), klass.get());
207 } else if (shorty == "Z") {
208 typedef jboolean (fnptr)(JNIEnv*, jclass);
209 fnptr* fn = reinterpret_cast<fnptr*>(method->GetNativeMethod());
210 ScopedLocalRef<jclass> klass(soa.Env(),
211 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
212 ScopedThreadStateChange tsc(self, kNative);
213 result->SetZ(fn(soa.Env(), klass.get()));
214 } else if (shorty == "BI") {
215 typedef jbyte (fnptr)(JNIEnv*, jclass, jint);
216 fnptr* fn = reinterpret_cast<fnptr*>(method->GetNativeMethod());
217 ScopedLocalRef<jclass> klass(soa.Env(),
218 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
219 ScopedThreadStateChange tsc(self, kNative);
220 result->SetB(fn(soa.Env(), klass.get(), args[0].GetI()));
221 } else if (shorty == "II") {
222 typedef jint (fnptr)(JNIEnv*, jclass, jint);
223 fnptr* fn = reinterpret_cast<fnptr*>(method->GetNativeMethod());
224 ScopedLocalRef<jclass> klass(soa.Env(),
225 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
226 ScopedThreadStateChange tsc(self, kNative);
227 result->SetI(fn(soa.Env(), klass.get(), args[0].GetI()));
228 } else if (shorty == "LL") {
229 typedef jobject (fnptr)(JNIEnv*, jclass, jobject);
230 fnptr* fn = reinterpret_cast<fnptr*>(method->GetNativeMethod());
231 ScopedLocalRef<jclass> klass(soa.Env(),
232 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
233 ScopedLocalRef<jobject> arg0(soa.Env(),
234 soa.AddLocalReference<jobject>(args[0].GetL()));
Ian Rogers556d6372012-11-20 12:19:36 -0800235 jobject jresult;
236 {
237 ScopedThreadStateChange tsc(self, kNative);
238 jresult = fn(soa.Env(), klass.get(), arg0.get());
239 }
240 result->SetL(soa.Decode<Object*>(jresult));
Ian Rogers64b6d142012-10-29 16:34:15 -0700241 } else if (shorty == "IIZ") {
242 typedef jint (fnptr)(JNIEnv*, jclass, jint, jboolean);
243 fnptr* fn = reinterpret_cast<fnptr*>(method->GetNativeMethod());
244 ScopedLocalRef<jclass> klass(soa.Env(),
245 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
246 ScopedThreadStateChange tsc(self, kNative);
247 result->SetI(fn(soa.Env(), klass.get(), args[0].GetI(), args[1].GetZ()));
248 } else if (shorty == "ILI") {
249 typedef jint (fnptr)(JNIEnv*, jclass, jobject, jint);
250 fnptr* fn = reinterpret_cast<fnptr*>(method->GetNativeMethod());
251 ScopedLocalRef<jclass> klass(soa.Env(),
252 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
253 ScopedLocalRef<jobject> arg0(soa.Env(),
254 soa.AddLocalReference<jobject>(args[0].GetL()));
255 ScopedThreadStateChange tsc(self, kNative);
256 result->SetI(fn(soa.Env(), klass.get(), arg0.get(), args[1].GetI()));
257 } else if (shorty == "SIZ") {
258 typedef jshort (fnptr)(JNIEnv*, jclass, jint, jboolean);
259 fnptr* fn = reinterpret_cast<fnptr*>(method->GetNativeMethod());
260 ScopedLocalRef<jclass> klass(soa.Env(),
261 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
262 ScopedThreadStateChange tsc(self, kNative);
263 result->SetS(fn(soa.Env(), klass.get(), args[0].GetI(), args[1].GetZ()));
264 } else if (shorty == "VIZ") {
265 typedef void (fnptr)(JNIEnv*, jclass, jint, jboolean);
266 fnptr* fn = reinterpret_cast<fnptr*>(method->GetNativeMethod());
267 ScopedLocalRef<jclass> klass(soa.Env(),
268 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
269 ScopedThreadStateChange tsc(self, kNative);
270 fn(soa.Env(), klass.get(), args[0].GetI(), args[1].GetZ());
271 } else if (shorty == "ZLL") {
272 typedef jboolean (fnptr)(JNIEnv*, jclass, jobject, jobject);
273 fnptr* fn = reinterpret_cast<fnptr*>(method->GetNativeMethod());
274 ScopedLocalRef<jclass> klass(soa.Env(),
275 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
276 ScopedLocalRef<jobject> arg0(soa.Env(),
277 soa.AddLocalReference<jobject>(args[0].GetL()));
278 ScopedLocalRef<jobject> arg1(soa.Env(),
279 soa.AddLocalReference<jobject>(args[1].GetL()));
280 ScopedThreadStateChange tsc(self, kNative);
281 result->SetZ(fn(soa.Env(), klass.get(), arg0.get(), arg1.get()));
282 } else if (shorty == "ZILL") {
283 typedef jboolean (fnptr)(JNIEnv*, jclass, jint, jobject, jobject);
284 fnptr* fn = reinterpret_cast<fnptr*>(method->GetNativeMethod());
285 ScopedLocalRef<jclass> klass(soa.Env(),
286 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
287 ScopedLocalRef<jobject> arg1(soa.Env(),
288 soa.AddLocalReference<jobject>(args[1].GetL()));
289 ScopedLocalRef<jobject> arg2(soa.Env(),
290 soa.AddLocalReference<jobject>(args[2].GetL()));
291 ScopedThreadStateChange tsc(self, kNative);
292 result->SetZ(fn(soa.Env(), klass.get(), args[0].GetI(), arg1.get(), arg2.get()));
293 } else if (shorty == "VILII") {
294 typedef void (fnptr)(JNIEnv*, jclass, jint, jobject, jint, jint);
295 fnptr* fn = reinterpret_cast<fnptr*>(method->GetNativeMethod());
296 ScopedLocalRef<jclass> klass(soa.Env(),
297 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
298 ScopedLocalRef<jobject> arg1(soa.Env(),
299 soa.AddLocalReference<jobject>(args[1].GetL()));
300 ScopedThreadStateChange tsc(self, kNative);
301 fn(soa.Env(), klass.get(), args[0].GetI(), arg1.get(), args[2].GetI(), args[3].GetI());
302 } else if (shorty == "VLILII") {
303 typedef void (fnptr)(JNIEnv*, jclass, jobject, jint, jobject, jint, jint);
304 fnptr* fn = reinterpret_cast<fnptr*>(method->GetNativeMethod());
305 ScopedLocalRef<jclass> klass(soa.Env(),
306 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
307 ScopedLocalRef<jobject> arg0(soa.Env(),
308 soa.AddLocalReference<jobject>(args[0].GetL()));
309 ScopedLocalRef<jobject> arg2(soa.Env(),
310 soa.AddLocalReference<jobject>(args[2].GetL()));
311 ScopedThreadStateChange tsc(self, kNative);
312 fn(soa.Env(), klass.get(), arg0.get(), args[1].GetI(), arg2.get(), args[3].GetI(),
313 args[4].GetI());
314 } else {
315 LOG(FATAL) << "Do something with static native method: " << PrettyMethod(method)
316 << " shorty: " << shorty;
317 }
318 } else {
319 if (shorty == "L") {
320 typedef jobject (fnptr)(JNIEnv*, jobject);
321 fnptr* fn = reinterpret_cast<fnptr*>(method->GetNativeMethod());
322 ScopedLocalRef<jobject> rcvr(soa.Env(),
323 soa.AddLocalReference<jobject>(receiver));
Ian Rogers556d6372012-11-20 12:19:36 -0800324 jobject jresult;
325 {
326 ScopedThreadStateChange tsc(self, kNative);
327 jresult = fn(soa.Env(), rcvr.get());
328 }
329 result->SetL(soa.Decode<Object*>(jresult));
Ian Rogers64b6d142012-10-29 16:34:15 -0700330 } else if (shorty == "LL") {
331 typedef jobject (fnptr)(JNIEnv*, jobject, jobject);
332 fnptr* fn = reinterpret_cast<fnptr*>(method->GetNativeMethod());
333 ScopedLocalRef<jobject> rcvr(soa.Env(),
334 soa.AddLocalReference<jobject>(receiver));
335 ScopedLocalRef<jobject> arg0(soa.Env(),
336 soa.AddLocalReference<jobject>(args[0].GetL()));
Ian Rogers556d6372012-11-20 12:19:36 -0800337 jobject jresult;
338 {
339 ScopedThreadStateChange tsc(self, kNative);
340 jresult = fn(soa.Env(), rcvr.get(), arg0.get());
341
342 }
343 result->SetL(soa.Decode<Object*>(jresult));
Ian Rogers64b6d142012-10-29 16:34:15 -0700344 ScopedThreadStateChange tsc(self, kNative);
Ian Rogers64b6d142012-10-29 16:34:15 -0700345 } else if (shorty == "III") {
346 typedef jint (fnptr)(JNIEnv*, jobject, jint, jint);
347 fnptr* fn = reinterpret_cast<fnptr*>(method->GetNativeMethod());
348 ScopedLocalRef<jobject> rcvr(soa.Env(),
349 soa.AddLocalReference<jobject>(receiver));
350 ScopedThreadStateChange tsc(self, kNative);
351 result->SetI(fn(soa.Env(), rcvr.get(), args[0].GetI(), args[1].GetI()));
352 } else {
353 LOG(FATAL) << "Do something with native method: " << PrettyMethod(method)
354 << " shorty: " << shorty;
355 }
356 }
357}
358
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700359static void DoMonitorEnter(Thread* self, Object* ref) NO_THREAD_SAFETY_ANALYSIS {
360 ref->MonitorEnter(self);
361}
362
363static void DoMonitorExit(Thread* self, Object* ref) NO_THREAD_SAFETY_ANALYSIS {
364 ref->MonitorExit(self);
365}
366
367static void DoInvoke(Thread* self, MethodHelper& mh, ShadowFrame& shadow_frame,
368 const DecodedInstruction& dec_insn, InvokeType type, bool is_range,
369 JValue* result)
370 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
371 Object* receiver;
372 if (type == kStatic) {
373 receiver = NULL;
374 } else {
375 receiver = shadow_frame.GetReference(dec_insn.vC);
376 if (UNLIKELY(receiver == NULL)) {
377 ThrowNullPointerExceptionForMethodAccess(shadow_frame.GetMethod(), dec_insn.vB, type);
378 result->SetJ(0);
379 return;
380 }
381 }
382 uint32_t method_idx = dec_insn.vB;
383 AbstractMethod* target_method = FindMethodFromCode(method_idx, receiver,
384 shadow_frame.GetMethod(), self, true,
385 type);
386 if (UNLIKELY(target_method == NULL)) {
387 CHECK(self->IsExceptionPending());
388 result->SetJ(0);
389 return;
390 }
391 mh.ChangeMethod(target_method);
392 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
393 if (is_range) {
394 arg_array.BuildArgArray(shadow_frame, dec_insn.vC + (type != kStatic ? 1 : 0));
395 } else {
396 arg_array.BuildArgArray(shadow_frame, dec_insn.arg + (type != kStatic ? 1 : 0));
397 }
Ian Rogers64b6d142012-10-29 16:34:15 -0700398 if (LIKELY(Runtime::Current()->IsStarted())) {
399 target_method->Invoke(self, receiver, arg_array.get(), result);
400 } else {
401 UnstartedRuntimeInvoke(self, target_method, receiver, arg_array.get(), result);
402 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700403 if (!mh.GetReturnType()->IsPrimitive() && result->GetL() != NULL) {
404 CHECK(mh.GetReturnType()->IsAssignableFrom(result->GetL()->GetClass()));
405 }
406 mh.ChangeMethod(shadow_frame.GetMethod());
407}
408
409static void DoFieldGet(Thread* self, ShadowFrame& shadow_frame,
410 const DecodedInstruction& dec_insn, FindFieldType find_type,
411 Primitive::Type field_type)
412 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
413 bool is_static = (find_type == StaticObjectRead) || (find_type == StaticPrimitiveRead);
414 uint32_t field_idx = is_static ? dec_insn.vB : dec_insn.vC;
415 Field* f = FindFieldFromCode(field_idx, shadow_frame.GetMethod(), self,
416 find_type, Primitive::FieldSize(field_type));
417 if (LIKELY(f != NULL)) {
418 Object* obj;
419 if (is_static) {
420 obj = f->GetDeclaringClass();
421 } else {
422 obj = shadow_frame.GetReference(dec_insn.vB);
423 if (UNLIKELY(obj == NULL)) {
424 ThrowNullPointerExceptionForFieldAccess(f, true);
425 }
426 }
427 switch (field_type) {
428 case Primitive::kPrimBoolean:
429 shadow_frame.SetVReg(dec_insn.vA, f->GetBoolean(obj));
430 break;
431 case Primitive::kPrimByte:
432 shadow_frame.SetVReg(dec_insn.vA, f->GetByte(obj));
433 break;
434 case Primitive::kPrimChar:
435 shadow_frame.SetVReg(dec_insn.vA, f->GetChar(obj));
436 break;
437 case Primitive::kPrimShort:
438 shadow_frame.SetVReg(dec_insn.vA, f->GetShort(obj));
439 break;
440 case Primitive::kPrimInt:
441 shadow_frame.SetVReg(dec_insn.vA, f->GetInt(obj));
442 break;
443 case Primitive::kPrimLong:
444 shadow_frame.SetVRegLong(dec_insn.vA, f->GetLong(obj));
445 break;
446 case Primitive::kPrimNot:
447 shadow_frame.SetReferenceAndVReg(dec_insn.vA, f->GetObject(obj));
448 break;
449 default:
450 LOG(FATAL) << "Unreachable: " << field_type;
451 }
452 }
453}
454
455static void DoFieldPut(Thread* self, ShadowFrame& shadow_frame,
456 const DecodedInstruction& dec_insn, FindFieldType find_type,
457 Primitive::Type field_type)
458 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
459 bool is_static = (find_type == StaticObjectWrite) || (find_type == StaticPrimitiveWrite);
460 uint32_t field_idx = is_static ? dec_insn.vB : dec_insn.vC;
461 Field* f = FindFieldFromCode(field_idx, shadow_frame.GetMethod(), self,
462 find_type, Primitive::FieldSize(field_type));
463 if (LIKELY(f != NULL)) {
464 Object* obj;
465 if (is_static) {
466 obj = f->GetDeclaringClass();
467 } else {
468 obj = shadow_frame.GetReference(dec_insn.vB);
469 if (UNLIKELY(obj == NULL)) {
470 ThrowNullPointerExceptionForFieldAccess(f, false);
471 }
472 }
473 switch (field_type) {
474 case Primitive::kPrimBoolean:
475 f->SetBoolean(obj, shadow_frame.GetVReg(dec_insn.vA));
476 break;
477 case Primitive::kPrimByte:
478 f->SetByte(obj, shadow_frame.GetVReg(dec_insn.vA));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700479 break;
480 case Primitive::kPrimChar:
481 f->SetChar(obj, shadow_frame.GetVReg(dec_insn.vA));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700482 break;
483 case Primitive::kPrimShort:
484 f->SetShort(obj, shadow_frame.GetVReg(dec_insn.vA));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700485 break;
486 case Primitive::kPrimInt:
487 f->SetInt(obj, shadow_frame.GetVReg(dec_insn.vA));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700488 break;
489 case Primitive::kPrimLong:
490 f->SetLong(obj, shadow_frame.GetVRegLong(dec_insn.vA));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700491 break;
492 case Primitive::kPrimNot:
493 f->SetObj(obj, shadow_frame.GetReference(dec_insn.vA));
494 break;
495 default:
496 LOG(FATAL) << "Unreachable: " << field_type;
497 }
498 }
499}
500
501static JValue Execute(Thread* self, MethodHelper& mh, const DexFile::CodeItem* code_item,
502 ShadowFrame& shadow_frame) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
503 const uint16_t* insns = code_item->insns_;
504 const Instruction* inst = Instruction::At(insns + shadow_frame.GetDexPC());
505 JValue result_register;
506 while (true) {
jeffhao373c52f2012-11-20 16:11:52 -0800507 CheckSuspend(self);
508 uint32_t dex_pc = inst->GetDexPc(insns);
509 shadow_frame.SetDexPC(dex_pc);
510 Dbg::UpdateDebugger(dex_pc, self);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700511 DecodedInstruction dec_insn(inst);
Ian Rogers64b6d142012-10-29 16:34:15 -0700512 const bool kTracing = false;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700513 if (kTracing) {
514 LOG(INFO) << PrettyMethod(shadow_frame.GetMethod())
515 << StringPrintf("\n0x%x: %s\nReferences:",
516 inst->GetDexPc(insns), inst->DumpString(&mh.GetDexFile()).c_str());
517 for (size_t i = 0; i < shadow_frame.NumberOfReferences(); ++i) {
518 Object* o = shadow_frame.GetReference(i);
519 if (o != NULL) {
520 if (o->GetClass()->IsStringClass() && o->AsString()->GetCharArray() != NULL) {
521 LOG(INFO) << i << ": java.lang.String " << static_cast<void*>(o)
522 << " \"" << o->AsString()->ToModifiedUtf8() << "\"";
523 } else {
524 LOG(INFO) << i << ": " << PrettyTypeOf(o) << " " << static_cast<void*>(o);
525 }
526 } else {
527 LOG(INFO) << i << ": null";
528 }
529 }
530 LOG(INFO) << "vregs:";
531 for (size_t i = 0; i < shadow_frame.NumberOfReferences(); ++i) {
532 LOG(INFO) << StringPrintf("%d: %08x", i, shadow_frame.GetVReg(i));
533 }
534 }
535 const Instruction* next_inst = inst->Next();
536 switch (dec_insn.opcode) {
537 case Instruction::NOP:
538 break;
539 case Instruction::MOVE:
540 case Instruction::MOVE_FROM16:
541 case Instruction::MOVE_16:
542 shadow_frame.SetVReg(dec_insn.vA, shadow_frame.GetVReg(dec_insn.vB));
543 break;
544 case Instruction::MOVE_WIDE:
545 case Instruction::MOVE_WIDE_FROM16:
546 case Instruction::MOVE_WIDE_16:
547 shadow_frame.SetVRegLong(dec_insn.vA, shadow_frame.GetVRegLong(dec_insn.vB));
548 break;
549 case Instruction::MOVE_OBJECT:
550 case Instruction::MOVE_OBJECT_FROM16:
551 case Instruction::MOVE_OBJECT_16:
552 shadow_frame.SetVReg(dec_insn.vA, shadow_frame.GetVReg(dec_insn.vB));
553 shadow_frame.SetReference(dec_insn.vA, shadow_frame.GetReference(dec_insn.vB));
554 break;
555 case Instruction::MOVE_RESULT:
556 shadow_frame.SetVReg(dec_insn.vA, result_register.GetI());
557 break;
558 case Instruction::MOVE_RESULT_WIDE:
559 shadow_frame.SetVRegLong(dec_insn.vA, result_register.GetJ());
560 break;
561 case Instruction::MOVE_RESULT_OBJECT:
562 shadow_frame.SetReferenceAndVReg(dec_insn.vA, result_register.GetL());
563 break;
564 case Instruction::MOVE_EXCEPTION: {
565 Throwable* exception = self->GetException();
566 self->ClearException();
567 shadow_frame.SetReferenceAndVReg(dec_insn.vA, exception);
568 break;
569 }
570 case Instruction::RETURN_VOID: {
571 JValue result;
572 result.SetJ(0);
573 return result;
574 }
575 case Instruction::RETURN: {
576 JValue result;
577 result.SetJ(0);
578 result.SetI(shadow_frame.GetVReg(dec_insn.vA));
579 return result;
580 }
581 case Instruction::RETURN_WIDE: {
582 JValue result;
583 result.SetJ(shadow_frame.GetVRegLong(dec_insn.vA));
584 return result;
585 }
586 case Instruction::RETURN_OBJECT: {
587 JValue result;
588 result.SetJ(0);
589 result.SetL(shadow_frame.GetReference(dec_insn.vA));
590 return result;
591 }
592 case Instruction::CONST_4: {
Ian Rogers64b6d142012-10-29 16:34:15 -0700593 int32_t val = static_cast<int32_t>(dec_insn.vB << 28) >> 28;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700594 shadow_frame.SetVReg(dec_insn.vA, val);
595 if (val == 0) {
596 shadow_frame.SetReference(dec_insn.vA, NULL);
597 }
598 break;
599 }
600 case Instruction::CONST_16: {
601 int32_t val = static_cast<int16_t>(dec_insn.vB);
602 shadow_frame.SetVReg(dec_insn.vA, val);
603 if (val == 0) {
604 shadow_frame.SetReference(dec_insn.vA, NULL);
605 }
606 break;
607 }
608 case Instruction::CONST: {
609 int32_t val = dec_insn.vB;
610 shadow_frame.SetVReg(dec_insn.vA, val);
611 if (val == 0) {
612 shadow_frame.SetReference(dec_insn.vA, NULL);
613 }
614 break;
615 }
616 case Instruction::CONST_HIGH16: {
617 int32_t val = dec_insn.vB << 16;
618 shadow_frame.SetVReg(dec_insn.vA, val);
619 if (val == 0) {
620 shadow_frame.SetReference(dec_insn.vA, NULL);
621 }
622 break;
623 }
Ian Rogers64b6d142012-10-29 16:34:15 -0700624 case Instruction::CONST_WIDE_16:
625 shadow_frame.SetVRegLong(dec_insn.vA, static_cast<int16_t>(dec_insn.vB));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700626 break;
Ian Rogers64b6d142012-10-29 16:34:15 -0700627 case Instruction::CONST_WIDE_32:
628 shadow_frame.SetVRegLong(dec_insn.vA, static_cast<int32_t>(dec_insn.vB));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700629 break;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700630 case Instruction::CONST_WIDE:
Ian Rogers64b6d142012-10-29 16:34:15 -0700631 shadow_frame.SetVRegLong(dec_insn.vA, dec_insn.vB_wide);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700632 break;
633 case Instruction::CONST_WIDE_HIGH16:
Ian Rogers64b6d142012-10-29 16:34:15 -0700634 shadow_frame.SetVRegLong(dec_insn.vA, static_cast<uint64_t>(dec_insn.vB) << 48);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700635 break;
636 case Instruction::CONST_STRING:
637 case Instruction::CONST_STRING_JUMBO: {
638 if (UNLIKELY(!String::GetJavaLangString()->IsInitialized())) {
639 Runtime::Current()->GetClassLinker()->EnsureInitialized(String::GetJavaLangString(),
640 true, true);
641 }
642 String* s = mh.ResolveString(dec_insn.vB);
643 shadow_frame.SetReferenceAndVReg(dec_insn.vA, s);
644 break;
645 }
646 case Instruction::CONST_CLASS:
Ian Rogers17ffcab2012-11-20 15:27:41 -0800647 shadow_frame.SetReferenceAndVReg(dec_insn.vA, mh.ResolveClass(dec_insn.vB));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700648 break;
649 case Instruction::MONITOR_ENTER:
650 DoMonitorEnter(self, shadow_frame.GetReference(dec_insn.vA));
651 break;
652 case Instruction::MONITOR_EXIT:
653 DoMonitorExit(self, shadow_frame.GetReference(dec_insn.vA));
654 break;
655 case Instruction::CHECK_CAST: {
656 Class* c = mh.ResolveClass(dec_insn.vB);
657 Object* obj = shadow_frame.GetReference(dec_insn.vA);
658 if (UNLIKELY(obj != NULL && !obj->InstanceOf(c))) {
659 self->ThrowNewExceptionF("Ljava/lang/ClassCastException;",
660 "%s cannot be cast to %s",
661 PrettyDescriptor(obj->GetClass()).c_str(),
662 PrettyDescriptor(c).c_str());
663 }
664 break;
665 }
666 case Instruction::INSTANCE_OF: {
667 Class* c = mh.ResolveClass(dec_insn.vC);
668 Object* obj = shadow_frame.GetReference(dec_insn.vB);
669 shadow_frame.SetVReg(dec_insn.vA, (obj != NULL && obj->InstanceOf(c)) ? 1 : 0);
670 break;
671 }
672 case Instruction::ARRAY_LENGTH: {
Ian Rogers64b6d142012-10-29 16:34:15 -0700673 Object* array = shadow_frame.GetReference(dec_insn.vB);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700674 if (UNLIKELY(array == NULL)) {
675 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetMethod(), inst->GetDexPc(insns));
676 break;
677 }
Ian Rogers64b6d142012-10-29 16:34:15 -0700678 shadow_frame.SetVReg(dec_insn.vA, array->AsArray()->GetLength());
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700679 break;
680 }
681 case Instruction::NEW_INSTANCE: {
682 Object* obj = AllocObjectFromCode(dec_insn.vB, shadow_frame.GetMethod(), self, true);
683 shadow_frame.SetReferenceAndVReg(dec_insn.vA, obj);
684 break;
685 }
686 case Instruction::NEW_ARRAY: {
687 int32_t length = shadow_frame.GetVReg(dec_insn.vB);
688 Object* obj = AllocArrayFromCode(dec_insn.vC, shadow_frame.GetMethod(), length, self, true);
689 shadow_frame.SetReferenceAndVReg(dec_insn.vA, obj);
690 break;
691 }
692 case Instruction::FILLED_NEW_ARRAY:
Ian Rogers64b6d142012-10-29 16:34:15 -0700693 case Instruction::FILLED_NEW_ARRAY_RANGE: {
694 bool is_range = (dec_insn.opcode == Instruction::FILLED_NEW_ARRAY_RANGE);
695 int32_t length = dec_insn.vA;
696 CHECK(is_range || length <= 5);
697 Class* arrayClass = mh.ResolveClass(dec_insn.vB);
698 CHECK(arrayClass->IsArrayClass());
699 if (arrayClass->GetComponentType()->IsPrimitiveInt()) {
700 IntArray* newArray = IntArray::Alloc(self, length);
701 if (newArray != NULL) {
702 for (int32_t i = 0; i < length; ++i) {
703 if (is_range) {
704 newArray->Set(i, shadow_frame.GetVReg(dec_insn.vC + i));
705 } else {
706 newArray->Set(i, shadow_frame.GetVReg(dec_insn.arg[i]));
707 }
708 }
709 }
710 result_register.SetL(newArray);
711 } else {
712 UNIMPLEMENTED(FATAL) << inst->DumpString(&mh.GetDexFile())
713 << " for array type: " << PrettyDescriptor(arrayClass);
714 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700715 break;
Ian Rogers64b6d142012-10-29 16:34:15 -0700716 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700717 case Instruction::CMPL_FLOAT: {
718 float val1 = shadow_frame.GetVRegFloat(dec_insn.vB);
719 float val2 = shadow_frame.GetVRegFloat(dec_insn.vC);
720 int32_t result;
721 if (val1 == val2) {
722 result = 0;
723 } else if (val1 > val2) {
724 result = 1;
725 } else {
726 result = -1;
727 }
728 shadow_frame.SetVReg(dec_insn.vA, result);
729 break;
730 }
731 case Instruction::CMPG_FLOAT: {
732 float val1 = shadow_frame.GetVRegFloat(dec_insn.vB);
733 float val2 = shadow_frame.GetVRegFloat(dec_insn.vC);
734 int32_t result;
735 if (val1 == val2) {
736 result = 0;
737 } else if (val1 < val2) {
738 result = -1;
739 } else {
740 result = 1;
741 }
742 shadow_frame.SetVReg(dec_insn.vA, result);
743 break;
744 }
745 case Instruction::CMPL_DOUBLE: {
746 double val1 = shadow_frame.GetVRegDouble(dec_insn.vB);
747 double val2 = shadow_frame.GetVRegDouble(dec_insn.vC);
748 int32_t result;
749 if (val1 == val2) {
750 result = 0;
Ian Rogers58bf0c62012-11-20 16:24:12 -0800751 } else if (val1 > val2) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700752 result = 1;
753 } else {
754 result = -1;
755 }
756 shadow_frame.SetVReg(dec_insn.vA, result);
757 break;
758 }
759
760 case Instruction::CMPG_DOUBLE: {
761 double val1 = shadow_frame.GetVRegDouble(dec_insn.vB);
762 double val2 = shadow_frame.GetVRegDouble(dec_insn.vC);
763 int32_t result;
764 if (val1 == val2) {
765 result = 0;
Ian Rogers58bf0c62012-11-20 16:24:12 -0800766 } else if (val1 < val2) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700767 result = -1;
768 } else {
769 result = 1;
770 }
771 shadow_frame.SetVReg(dec_insn.vA, result);
772 break;
773 }
774 case Instruction::CMP_LONG: {
775 int64_t val1 = shadow_frame.GetVRegLong(dec_insn.vB);
776 int64_t val2 = shadow_frame.GetVRegLong(dec_insn.vC);
777 int32_t result;
778 if (val1 < val2) {
Ian Rogers64b6d142012-10-29 16:34:15 -0700779 result = 1;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700780 } else if (val1 == val2) {
781 result = 0;
782 } else {
Ian Rogers64b6d142012-10-29 16:34:15 -0700783 result = -1;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700784 }
785 shadow_frame.SetVReg(dec_insn.vA, result);
786 break;
787 }
788 case Instruction::THROW: {
789 Throwable* t = shadow_frame.GetReference(dec_insn.vA)->AsThrowable();
790 self->SetException(t);
791 break;
792 }
793 case Instruction::GOTO:
794 case Instruction::GOTO_16:
795 case Instruction::GOTO_32: {
796 uint32_t dex_pc = inst->GetDexPc(insns);
797 next_inst = Instruction::At(insns + dex_pc + dec_insn.vA);
798 break;
799 }
Ian Rogers556d6372012-11-20 12:19:36 -0800800 case Instruction::PACKED_SWITCH: {
801 uint32_t dex_pc = inst->GetDexPc(insns);
802 const uint16_t* switch_data = insns + dex_pc + dec_insn.vB;
803 int32_t test_val = shadow_frame.GetVReg(dec_insn.vA);
804 CHECK_EQ(switch_data[0], static_cast<uint16_t>(Instruction::kPackedSwitchSignature));
805 uint16_t size = switch_data[1];
806 CHECK_GT(size, 0);
807 const int32_t* keys = reinterpret_cast<const int32_t*>(&switch_data[2]);
808 CHECK(IsAligned<4>(keys));
809 int32_t first_key = keys[0];
810 const int32_t* targets = reinterpret_cast<const int32_t*>(&switch_data[4]);
811 CHECK(IsAligned<4>(targets));
812 int32_t index = test_val - first_key;
813 if (index >= 0 && index < size) {
814 next_inst = Instruction::At(insns + dex_pc + targets[index]);
815 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700816 break;
Ian Rogers556d6372012-11-20 12:19:36 -0800817 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700818 case Instruction::SPARSE_SWITCH: {
819 uint32_t dex_pc = inst->GetDexPc(insns);
Ian Rogers556d6372012-11-20 12:19:36 -0800820 const uint16_t* switch_data = insns + dex_pc + dec_insn.vB;
821 int32_t test_val = shadow_frame.GetVReg(dec_insn.vA);
822 CHECK_EQ(switch_data[0], static_cast<uint16_t>(Instruction::kSparseSwitchSignature));
823 uint16_t size = switch_data[1];
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700824 CHECK_GT(size, 0);
Ian Rogers556d6372012-11-20 12:19:36 -0800825 const int32_t* keys = reinterpret_cast<const int32_t*>(&switch_data[2]);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700826 CHECK(IsAligned<4>(keys));
827 const int32_t* entries = keys + size;
828 CHECK(IsAligned<4>(entries));
829 int lo = 0;
830 int hi = size - 1;
831 while (lo <= hi) {
832 int mid = (lo + hi) / 2;
833 int32_t foundVal = keys[mid];
Ian Rogers556d6372012-11-20 12:19:36 -0800834 if (test_val < foundVal) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700835 hi = mid - 1;
Ian Rogers556d6372012-11-20 12:19:36 -0800836 } else if (test_val > foundVal) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700837 lo = mid + 1;
838 } else {
839 next_inst = Instruction::At(insns + dex_pc + entries[mid]);
840 break;
841 }
842 }
843 break;
844 }
845 case Instruction::FILL_ARRAY_DATA: {
846 Array* array = shadow_frame.GetReference(dec_insn.vA)->AsArray();
847 if (UNLIKELY(array == NULL)) {
848 Thread::Current()->ThrowNewExceptionF("Ljava/lang/NullPointerException;",
849 "null array in FILL_ARRAY_DATA");
850 break;
851 }
852 DCHECK(array->IsArrayInstance() && !array->IsObjectArray());
853 uint32_t dex_pc = inst->GetDexPc(insns);
854 const Instruction::ArrayDataPayload* payload =
855 reinterpret_cast<const Instruction::ArrayDataPayload*>(insns + dex_pc + dec_insn.vB);
856 if (UNLIKELY(static_cast<int32_t>(payload->element_count) > array->GetLength())) {
857 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
858 "failed FILL_ARRAY_DATA; length=%d, index=%d",
859 array->GetLength(), payload->element_count);
860 break;
861 }
862 uint32_t size_in_bytes = payload->element_count * payload->element_width;
863 memcpy(array->GetRawData(payload->element_width), payload->data, size_in_bytes);
864 break;
865 }
866 case Instruction::IF_EQ: {
867 if (shadow_frame.GetVReg(dec_insn.vA) == shadow_frame.GetVReg(dec_insn.vB)) {
868 uint32_t dex_pc = inst->GetDexPc(insns);
869 next_inst = Instruction::At(insns + dex_pc + dec_insn.vC);
870 }
871 break;
872 }
873 case Instruction::IF_NE: {
874 if (shadow_frame.GetVReg(dec_insn.vA) != shadow_frame.GetVReg(dec_insn.vB)) {
875 uint32_t dex_pc = inst->GetDexPc(insns);
876 next_inst = Instruction::At(insns + dex_pc + dec_insn.vC);
877 }
878 break;
879 }
880 case Instruction::IF_LT: {
881 if (shadow_frame.GetVReg(dec_insn.vA) < shadow_frame.GetVReg(dec_insn.vB)) {
882 uint32_t dex_pc = inst->GetDexPc(insns);
883 next_inst = Instruction::At(insns + dex_pc + dec_insn.vC);
884 }
885 break;
886 }
887 case Instruction::IF_GE: {
888 if (shadow_frame.GetVReg(dec_insn.vA) >= shadow_frame.GetVReg(dec_insn.vB)) {
889 uint32_t dex_pc = inst->GetDexPc(insns);
890 next_inst = Instruction::At(insns + dex_pc + dec_insn.vC);
891 }
892 break;
893 }
894 case Instruction::IF_GT: {
895 if (shadow_frame.GetVReg(dec_insn.vA) > shadow_frame.GetVReg(dec_insn.vB)) {
896 uint32_t dex_pc = inst->GetDexPc(insns);
897 next_inst = Instruction::At(insns + dex_pc + dec_insn.vC);
898 }
899 break;
900 }
901 case Instruction::IF_LE: {
902 if (shadow_frame.GetVReg(dec_insn.vA) <= shadow_frame.GetVReg(dec_insn.vB)) {
903 uint32_t dex_pc = inst->GetDexPc(insns);
904 next_inst = Instruction::At(insns + dex_pc + dec_insn.vC);
905 }
906 break;
907 }
908 case Instruction::IF_EQZ: {
909 if (shadow_frame.GetVReg(dec_insn.vA) == 0) {
910 uint32_t dex_pc = inst->GetDexPc(insns);
911 next_inst = Instruction::At(insns + dex_pc + dec_insn.vB);
912 }
913 break;
914 }
915 case Instruction::IF_NEZ: {
916 if (shadow_frame.GetVReg(dec_insn.vA) != 0) {
917 uint32_t dex_pc = inst->GetDexPc(insns);
918 next_inst = Instruction::At(insns + dex_pc + dec_insn.vB);
919 }
920 break;
921 }
922 case Instruction::IF_LTZ: {
923 if (shadow_frame.GetVReg(dec_insn.vA) < 0) {
924 uint32_t dex_pc = inst->GetDexPc(insns);
925 next_inst = Instruction::At(insns + dex_pc + dec_insn.vB);
926 }
927 break;
928 }
929 case Instruction::IF_GEZ: {
930 if (shadow_frame.GetVReg(dec_insn.vA) >= 0) {
931 uint32_t dex_pc = inst->GetDexPc(insns);
932 next_inst = Instruction::At(insns + dex_pc + dec_insn.vB);
933 }
934 break;
935 }
936 case Instruction::IF_GTZ: {
937 if (shadow_frame.GetVReg(dec_insn.vA) > 0) {
938 uint32_t dex_pc = inst->GetDexPc(insns);
939 next_inst = Instruction::At(insns + dex_pc + dec_insn.vB);
940 }
941 break;
942 }
943 case Instruction::IF_LEZ: {
944 if (shadow_frame.GetVReg(dec_insn.vA) <= 0) {
945 uint32_t dex_pc = inst->GetDexPc(insns);
946 next_inst = Instruction::At(insns + dex_pc + dec_insn.vB);
947 }
948 break;
949 }
950 case Instruction::AGET_BOOLEAN: {
951 BooleanArray* a = shadow_frame.GetReference(dec_insn.vB)->AsBooleanArray();
952 if (UNLIKELY(a == NULL)) {
953 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetMethod(), inst->GetDexPc(insns));
954 break;
955 }
956 int32_t index = shadow_frame.GetVReg(dec_insn.vC);
957 shadow_frame.SetVReg(dec_insn.vA, a->Get(index));
958 break;
959 }
960 case Instruction::AGET_BYTE: {
961 ByteArray* a = shadow_frame.GetReference(dec_insn.vB)->AsByteArray();
962 if (UNLIKELY(a == NULL)) {
963 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetMethod(), inst->GetDexPc(insns));
964 break;
965 }
966 int32_t index = shadow_frame.GetVReg(dec_insn.vC);
967 shadow_frame.SetVReg(dec_insn.vA, a->Get(index));
968 break;
969 }
970 case Instruction::AGET_CHAR: {
971 CharArray* a = shadow_frame.GetReference(dec_insn.vB)->AsCharArray();
972 if (UNLIKELY(a == NULL)) {
973 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetMethod(), inst->GetDexPc(insns));
974 break;
975 }
976 int32_t index = shadow_frame.GetVReg(dec_insn.vC);
977 shadow_frame.SetVReg(dec_insn.vA, a->Get(index));
978 break;
979 }
980 case Instruction::AGET_SHORT: {
981 ShortArray* a = shadow_frame.GetReference(dec_insn.vB)->AsShortArray();
982 if (UNLIKELY(a == NULL)) {
983 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetMethod(), inst->GetDexPc(insns));
984 break;
985 }
986 int32_t index = shadow_frame.GetVReg(dec_insn.vC);
987 shadow_frame.SetVReg(dec_insn.vA, a->Get(index));
988 break;
989 }
990 case Instruction::AGET: {
991 IntArray* a = shadow_frame.GetReference(dec_insn.vB)->AsIntArray();
992 if (UNLIKELY(a == NULL)) {
993 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetMethod(), inst->GetDexPc(insns));
994 break;
995 }
996 int32_t index = shadow_frame.GetVReg(dec_insn.vC);
997 shadow_frame.SetVReg(dec_insn.vA, a->Get(index));
998 break;
999 }
1000 case Instruction::AGET_WIDE: {
1001 LongArray* a = shadow_frame.GetReference(dec_insn.vB)->AsLongArray();
1002 if (UNLIKELY(a == NULL)) {
1003 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetMethod(), inst->GetDexPc(insns));
1004 break;
1005 }
1006 int32_t index = shadow_frame.GetVReg(dec_insn.vC);
1007 shadow_frame.SetVRegLong(dec_insn.vA, a->Get(index));
1008 break;
1009 }
1010 case Instruction::AGET_OBJECT: {
1011 ObjectArray<Object>* a = shadow_frame.GetReference(dec_insn.vB)->AsObjectArray<Object>();
1012 if (UNLIKELY(a == NULL)) {
1013 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetMethod(), inst->GetDexPc(insns));
1014 break;
1015 }
1016 int32_t index = shadow_frame.GetVReg(dec_insn.vC);
1017 Object* o = a->Get(index);
1018 shadow_frame.SetReferenceAndVReg(dec_insn.vA, o);
1019 break;
1020 }
1021 case Instruction::APUT_BOOLEAN: {
1022 uint8_t val = shadow_frame.GetVReg(dec_insn.vA);
1023 BooleanArray* a = shadow_frame.GetReference(dec_insn.vB)->AsBooleanArray();
1024 if (UNLIKELY(a == NULL)) {
1025 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetMethod(), inst->GetDexPc(insns));
1026 break;
1027 }
1028 int32_t index = shadow_frame.GetVReg(dec_insn.vC);
1029 a->Set(index, val);
1030 break;
1031 }
1032 case Instruction::APUT_BYTE: {
1033 int8_t val = shadow_frame.GetVReg(dec_insn.vA);
1034 ByteArray* a = shadow_frame.GetReference(dec_insn.vB)->AsByteArray();
1035 if (UNLIKELY(a == NULL)) {
1036 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetMethod(), inst->GetDexPc(insns));
1037 break;
1038 }
1039 int32_t index = shadow_frame.GetVReg(dec_insn.vC);
1040 a->Set(index, val);
1041 break;
1042 }
1043 case Instruction::APUT_CHAR: {
1044 uint16_t val = shadow_frame.GetVReg(dec_insn.vA);
1045 CharArray* a = shadow_frame.GetReference(dec_insn.vB)->AsCharArray();
1046 if (UNLIKELY(a == NULL)) {
1047 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetMethod(), inst->GetDexPc(insns));
1048 break;
1049 }
1050 int32_t index = shadow_frame.GetVReg(dec_insn.vC);
1051 a->Set(index, val);
1052 break;
1053 }
1054 case Instruction::APUT_SHORT: {
1055 int16_t val = shadow_frame.GetVReg(dec_insn.vA);
1056 ShortArray* a = shadow_frame.GetReference(dec_insn.vB)->AsShortArray();
1057 if (UNLIKELY(a == NULL)) {
1058 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetMethod(), inst->GetDexPc(insns));
1059 break;
1060 }
1061 int32_t index = shadow_frame.GetVReg(dec_insn.vC);
1062 a->Set(index, val);
1063 break;
1064 }
1065 case Instruction::APUT: {
1066 int32_t val = shadow_frame.GetVReg(dec_insn.vA);
1067 IntArray* a = shadow_frame.GetReference(dec_insn.vB)->AsIntArray();
1068 if (UNLIKELY(a == NULL)) {
1069 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetMethod(), inst->GetDexPc(insns));
1070 break;
1071 }
1072 int32_t index = shadow_frame.GetVReg(dec_insn.vC);
1073 a->Set(index, val);
1074 break;
1075 }
1076 case Instruction::APUT_WIDE: {
1077 int64_t val = shadow_frame.GetVRegLong(dec_insn.vA);
1078 LongArray* a = shadow_frame.GetReference(dec_insn.vB)->AsLongArray();
1079 if (UNLIKELY(a == NULL)) {
1080 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetMethod(), inst->GetDexPc(insns));
1081 break;
1082 }
1083 int32_t index = shadow_frame.GetVReg(dec_insn.vC);
1084 a->Set(index, val);
1085 break;
1086 }
1087 case Instruction::APUT_OBJECT: {
1088 Object* val = shadow_frame.GetReference(dec_insn.vA);
1089 ObjectArray<Object>* a = shadow_frame.GetReference(dec_insn.vB)->AsObjectArray<Object>();
1090 if (UNLIKELY(a == NULL)) {
1091 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetMethod(), inst->GetDexPc(insns));
1092 break;
1093 }
1094 int32_t index = shadow_frame.GetVReg(dec_insn.vC);
1095 a->Set(index, val);
1096 break;
1097 }
1098 case Instruction::IGET_BOOLEAN:
1099 DoFieldGet(self, shadow_frame, dec_insn, InstancePrimitiveRead, Primitive::kPrimBoolean);
1100 break;
1101 case Instruction::IGET_BYTE:
1102 DoFieldGet(self, shadow_frame, dec_insn, InstancePrimitiveRead, Primitive::kPrimByte);
1103 break;
1104 case Instruction::IGET_CHAR:
1105 DoFieldGet(self, shadow_frame, dec_insn, InstancePrimitiveRead, Primitive::kPrimChar);
1106 break;
1107 case Instruction::IGET_SHORT:
1108 DoFieldGet(self, shadow_frame, dec_insn, InstancePrimitiveRead, Primitive::kPrimShort);
1109 break;
1110 case Instruction::IGET:
1111 DoFieldGet(self, shadow_frame, dec_insn, InstancePrimitiveRead, Primitive::kPrimInt);
1112 break;
1113 case Instruction::IGET_WIDE:
1114 DoFieldGet(self, shadow_frame, dec_insn, InstancePrimitiveRead, Primitive::kPrimLong);
1115 break;
1116 case Instruction::IGET_OBJECT:
1117 DoFieldGet(self, shadow_frame, dec_insn, InstanceObjectRead, Primitive::kPrimNot);
1118 break;
1119 case Instruction::SGET_BOOLEAN:
1120 DoFieldGet(self, shadow_frame, dec_insn, StaticPrimitiveRead, Primitive::kPrimBoolean);
1121 break;
1122 case Instruction::SGET_BYTE:
1123 DoFieldGet(self, shadow_frame, dec_insn, StaticPrimitiveRead, Primitive::kPrimByte);
1124 break;
1125 case Instruction::SGET_CHAR:
1126 DoFieldGet(self, shadow_frame, dec_insn, StaticPrimitiveRead, Primitive::kPrimChar);
1127 break;
1128 case Instruction::SGET_SHORT:
1129 DoFieldGet(self, shadow_frame, dec_insn, StaticPrimitiveRead, Primitive::kPrimShort);
1130 break;
1131 case Instruction::SGET:
1132 DoFieldGet(self, shadow_frame, dec_insn, StaticPrimitiveRead, Primitive::kPrimInt);
1133 break;
1134 case Instruction::SGET_WIDE:
1135 DoFieldGet(self, shadow_frame, dec_insn, StaticPrimitiveRead, Primitive::kPrimLong);
1136 break;
1137 case Instruction::SGET_OBJECT:
1138 DoFieldGet(self, shadow_frame, dec_insn, StaticObjectRead, Primitive::kPrimNot);
1139 break;
1140 case Instruction::IPUT_BOOLEAN:
1141 DoFieldPut(self, shadow_frame, dec_insn, InstancePrimitiveWrite, Primitive::kPrimBoolean);
1142 break;
1143 case Instruction::IPUT_BYTE:
1144 DoFieldPut(self, shadow_frame, dec_insn, InstancePrimitiveWrite, Primitive::kPrimByte);
1145 break;
1146 case Instruction::IPUT_CHAR:
1147 DoFieldPut(self, shadow_frame, dec_insn, InstancePrimitiveWrite, Primitive::kPrimChar);
1148 break;
1149 case Instruction::IPUT_SHORT:
1150 DoFieldPut(self, shadow_frame, dec_insn, InstancePrimitiveWrite, Primitive::kPrimShort);
1151 break;
1152 case Instruction::IPUT:
1153 DoFieldPut(self, shadow_frame, dec_insn, InstancePrimitiveWrite, Primitive::kPrimInt);
1154 break;
1155 case Instruction::IPUT_WIDE:
1156 DoFieldPut(self, shadow_frame, dec_insn, InstancePrimitiveWrite, Primitive::kPrimLong);
1157 break;
1158 case Instruction::IPUT_OBJECT:
1159 DoFieldPut(self, shadow_frame, dec_insn, InstanceObjectWrite, Primitive::kPrimNot);
1160 break;
1161 case Instruction::SPUT_BOOLEAN:
1162 DoFieldPut(self, shadow_frame, dec_insn, StaticPrimitiveWrite, Primitive::kPrimBoolean);
1163 break;
1164 case Instruction::SPUT_BYTE:
1165 DoFieldPut(self, shadow_frame, dec_insn, StaticPrimitiveWrite, Primitive::kPrimByte);
1166 break;
1167 case Instruction::SPUT_CHAR:
1168 DoFieldPut(self, shadow_frame, dec_insn, StaticPrimitiveWrite, Primitive::kPrimChar);
1169 break;
1170 case Instruction::SPUT_SHORT:
1171 DoFieldPut(self, shadow_frame, dec_insn, StaticPrimitiveWrite, Primitive::kPrimShort);
1172 break;
1173 case Instruction::SPUT:
1174 DoFieldPut(self, shadow_frame, dec_insn, StaticPrimitiveWrite, Primitive::kPrimInt);
1175 break;
1176 case Instruction::SPUT_WIDE:
1177 DoFieldPut(self, shadow_frame, dec_insn, StaticPrimitiveWrite, Primitive::kPrimLong);
1178 break;
1179 case Instruction::SPUT_OBJECT:
1180 DoFieldPut(self, shadow_frame, dec_insn, StaticObjectWrite, Primitive::kPrimNot);
1181 break;
1182 case Instruction::INVOKE_VIRTUAL:
1183 DoInvoke(self, mh, shadow_frame, dec_insn, kVirtual, false, &result_register);
1184 break;
1185 case Instruction::INVOKE_VIRTUAL_RANGE:
1186 DoInvoke(self, mh, shadow_frame, dec_insn, kVirtual, true, &result_register);
1187 break;
1188 case Instruction::INVOKE_SUPER:
1189 DoInvoke(self, mh, shadow_frame, dec_insn, kSuper, false, &result_register);
1190 break;
1191 case Instruction::INVOKE_SUPER_RANGE:
1192 DoInvoke(self, mh, shadow_frame, dec_insn, kSuper, true, &result_register);
1193 break;
1194 case Instruction::INVOKE_DIRECT:
1195 DoInvoke(self, mh, shadow_frame, dec_insn, kDirect, false, &result_register);
1196 break;
1197 case Instruction::INVOKE_DIRECT_RANGE:
1198 DoInvoke(self, mh, shadow_frame, dec_insn, kDirect, true, &result_register);
1199 break;
1200 case Instruction::INVOKE_INTERFACE:
1201 DoInvoke(self, mh, shadow_frame, dec_insn, kInterface, false, &result_register);
1202 break;
1203 case Instruction::INVOKE_INTERFACE_RANGE:
1204 DoInvoke(self, mh, shadow_frame, dec_insn, kInterface, true, &result_register);
1205 break;
1206 case Instruction::INVOKE_STATIC:
1207 DoInvoke(self, mh, shadow_frame, dec_insn, kStatic, false, &result_register);
1208 break;
1209 case Instruction::INVOKE_STATIC_RANGE:
1210 DoInvoke(self, mh, shadow_frame, dec_insn, kStatic, true, &result_register);
1211 break;
1212 case Instruction::NEG_INT:
1213 shadow_frame.SetVReg(dec_insn.vA, -shadow_frame.GetVReg(dec_insn.vB));
1214 break;
1215 case Instruction::NOT_INT:
1216 shadow_frame.SetVReg(dec_insn.vA, 0 ^ shadow_frame.GetVReg(dec_insn.vB));
1217 break;
1218 case Instruction::NEG_LONG:
1219 shadow_frame.SetVRegLong(dec_insn.vA, -shadow_frame.GetVRegLong(dec_insn.vB));
1220 break;
1221 case Instruction::NOT_LONG:
1222 shadow_frame.SetVRegLong(dec_insn.vA, 0 ^ shadow_frame.GetVRegLong(dec_insn.vB));
1223 break;
1224 case Instruction::NEG_FLOAT:
1225 shadow_frame.SetVRegFloat(dec_insn.vA, -shadow_frame.GetVRegFloat(dec_insn.vB));
1226 break;
1227 case Instruction::NEG_DOUBLE:
1228 shadow_frame.SetVRegDouble(dec_insn.vA, -shadow_frame.GetVRegDouble(dec_insn.vB));
1229 break;
1230 case Instruction::INT_TO_LONG:
1231 shadow_frame.SetVRegLong(dec_insn.vA, shadow_frame.GetVReg(dec_insn.vB));
1232 break;
1233 case Instruction::INT_TO_FLOAT:
1234 shadow_frame.SetVRegFloat(dec_insn.vA, shadow_frame.GetVReg(dec_insn.vB));
1235 break;
1236 case Instruction::INT_TO_DOUBLE:
1237 shadow_frame.SetVRegDouble(dec_insn.vA, shadow_frame.GetVReg(dec_insn.vB));
1238 break;
1239 case Instruction::LONG_TO_INT:
1240 shadow_frame.SetVReg(dec_insn.vA, shadow_frame.GetVRegLong(dec_insn.vB));
1241 break;
1242 case Instruction::LONG_TO_FLOAT:
1243 shadow_frame.SetVRegFloat(dec_insn.vA, shadow_frame.GetVRegLong(dec_insn.vB));
1244 break;
1245 case Instruction::LONG_TO_DOUBLE:
1246 shadow_frame.SetVRegDouble(dec_insn.vA, shadow_frame.GetVRegLong(dec_insn.vB));
1247 break;
1248 case Instruction::FLOAT_TO_INT:
1249 shadow_frame.SetVReg(dec_insn.vA, shadow_frame.GetVRegFloat(dec_insn.vB));
1250 break;
1251 case Instruction::FLOAT_TO_LONG:
1252 shadow_frame.SetVRegLong(dec_insn.vA, shadow_frame.GetVRegFloat(dec_insn.vB));
1253 break;
1254 case Instruction::FLOAT_TO_DOUBLE:
1255 shadow_frame.SetVRegDouble(dec_insn.vA, shadow_frame.GetVRegFloat(dec_insn.vB));
1256 break;
1257 case Instruction::DOUBLE_TO_INT:
1258 shadow_frame.SetVReg(dec_insn.vA, shadow_frame.GetVRegDouble(dec_insn.vB));
1259 break;
1260 case Instruction::DOUBLE_TO_LONG:
1261 shadow_frame.SetVRegLong(dec_insn.vA, shadow_frame.GetVRegDouble(dec_insn.vB));
1262 break;
1263 case Instruction::DOUBLE_TO_FLOAT:
1264 shadow_frame.SetVRegFloat(dec_insn.vA, shadow_frame.GetVRegDouble(dec_insn.vB));
1265 break;
1266 case Instruction::INT_TO_BYTE:
1267 shadow_frame.SetVReg(dec_insn.vA, static_cast<int8_t>(shadow_frame.GetVReg(dec_insn.vB)));
1268 break;
1269 case Instruction::INT_TO_CHAR:
1270 shadow_frame.SetVReg(dec_insn.vA, static_cast<uint16_t>(shadow_frame.GetVReg(dec_insn.vB)));
1271 break;
1272 case Instruction::INT_TO_SHORT:
1273 shadow_frame.SetVReg(dec_insn.vA, static_cast<int16_t>(shadow_frame.GetVReg(dec_insn.vB)));
1274 break;
1275 case Instruction::ADD_INT:
1276 shadow_frame.SetVReg(dec_insn.vA,
1277 shadow_frame.GetVReg(dec_insn.vB) + shadow_frame.GetVReg(dec_insn.vC));
1278 break;
1279 case Instruction::SUB_INT:
1280 shadow_frame.SetVReg(dec_insn.vA,
1281 shadow_frame.GetVReg(dec_insn.vB) - shadow_frame.GetVReg(dec_insn.vC));
1282 break;
1283 case Instruction::MUL_INT:
1284 shadow_frame.SetVReg(dec_insn.vA,
1285 shadow_frame.GetVReg(dec_insn.vB) * shadow_frame.GetVReg(dec_insn.vC));
1286 break;
1287 case Instruction::REM_INT:
1288 shadow_frame.SetVReg(dec_insn.vA,
1289 shadow_frame.GetVReg(dec_insn.vB) % shadow_frame.GetVReg(dec_insn.vC));
1290 break;
1291 case Instruction::DIV_INT:
1292 shadow_frame.SetVReg(dec_insn.vA,
1293 shadow_frame.GetVReg(dec_insn.vB) / shadow_frame.GetVReg(dec_insn.vC));
1294 break;
1295 case Instruction::SHL_INT:
1296 shadow_frame.SetVReg(dec_insn.vA,
1297 shadow_frame.GetVReg(dec_insn.vB) << shadow_frame.GetVReg(dec_insn.vC));
1298 break;
1299 case Instruction::SHR_INT:
1300 shadow_frame.SetVReg(dec_insn.vA,
1301 shadow_frame.GetVReg(dec_insn.vB) >> shadow_frame.GetVReg(dec_insn.vC));
1302 break;
1303 case Instruction::USHR_INT:
1304 shadow_frame.SetVReg(dec_insn.vA,
1305 static_cast<uint32_t>(shadow_frame.GetVReg(dec_insn.vB)) >>
1306 shadow_frame.GetVReg(dec_insn.vC));
1307 break;
1308 case Instruction::AND_INT:
1309 shadow_frame.SetVReg(dec_insn.vA,
1310 shadow_frame.GetVReg(dec_insn.vB) & shadow_frame.GetVReg(dec_insn.vC));
1311 break;
1312 case Instruction::OR_INT:
1313 shadow_frame.SetVReg(dec_insn.vA,
1314 shadow_frame.GetVReg(dec_insn.vB) | shadow_frame.GetVReg(dec_insn.vC));
1315 break;
1316 case Instruction::XOR_INT:
1317 shadow_frame.SetVReg(dec_insn.vA,
1318 shadow_frame.GetVReg(dec_insn.vB) ^ shadow_frame.GetVReg(dec_insn.vC));
1319 break;
1320 case Instruction::ADD_LONG:
1321 shadow_frame.SetVRegLong(dec_insn.vA,
1322 shadow_frame.GetVRegLong(dec_insn.vB) +
1323 shadow_frame.GetVRegLong(dec_insn.vC));
1324 break;
1325 case Instruction::SUB_LONG:
1326 shadow_frame.SetVRegLong(dec_insn.vA,
1327 shadow_frame.GetVRegLong(dec_insn.vB) -
1328 shadow_frame.GetVRegLong(dec_insn.vC));
1329 break;
1330 case Instruction::MUL_LONG:
1331 shadow_frame.SetVRegLong(dec_insn.vA,
1332 shadow_frame.GetVRegLong(dec_insn.vB) *
1333 shadow_frame.GetVRegLong(dec_insn.vC));
1334 break;
1335 case Instruction::DIV_LONG:
1336 shadow_frame.SetVRegLong(dec_insn.vA,
1337 shadow_frame.GetVRegLong(dec_insn.vB) /
1338 shadow_frame.GetVRegLong(dec_insn.vC));
1339 break;
1340 case Instruction::REM_LONG:
1341 shadow_frame.SetVRegLong(dec_insn.vA,
1342 shadow_frame.GetVRegLong(dec_insn.vB) %
1343 shadow_frame.GetVRegLong(dec_insn.vC));
1344 break;
1345 case Instruction::AND_LONG:
1346 shadow_frame.SetVRegLong(dec_insn.vA,
1347 shadow_frame.GetVRegLong(dec_insn.vB) &
1348 shadow_frame.GetVRegLong(dec_insn.vC));
1349 break;
1350 case Instruction::OR_LONG:
1351 shadow_frame.SetVRegLong(dec_insn.vA,
1352 shadow_frame.GetVRegLong(dec_insn.vB) |
1353 shadow_frame.GetVRegLong(dec_insn.vC));
1354 break;
1355 case Instruction::XOR_LONG:
1356 shadow_frame.SetVRegLong(dec_insn.vA,
1357 shadow_frame.GetVRegLong(dec_insn.vB) ^
1358 shadow_frame.GetVRegLong(dec_insn.vC));
1359 break;
1360 case Instruction::SHL_LONG:
1361 shadow_frame.SetVRegLong(dec_insn.vA,
1362 shadow_frame.GetVRegLong(dec_insn.vB) <<
1363 shadow_frame.GetVReg(dec_insn.vC));
1364 break;
1365 case Instruction::SHR_LONG:
1366 shadow_frame.SetVRegLong(dec_insn.vA,
1367 shadow_frame.GetVRegLong(dec_insn.vB) >>
1368 shadow_frame.GetVReg(dec_insn.vC));
1369 break;
1370 case Instruction::USHR_LONG:
1371 shadow_frame.SetVRegLong(dec_insn.vA,
1372 static_cast<uint64_t>(shadow_frame.GetVRegLong(dec_insn.vB)) >>
1373 shadow_frame.GetVReg(dec_insn.vC));
1374 break;
1375 case Instruction::ADD_FLOAT:
1376 shadow_frame.SetVRegFloat(dec_insn.vA,
1377 shadow_frame.GetVRegFloat(dec_insn.vB) +
1378 shadow_frame.GetVRegFloat(dec_insn.vC));
1379 break;
1380 case Instruction::SUB_FLOAT:
1381 shadow_frame.SetVRegFloat(dec_insn.vA,
1382 shadow_frame.GetVRegFloat(dec_insn.vB) -
1383 shadow_frame.GetVRegFloat(dec_insn.vC));
1384 break;
1385 case Instruction::MUL_FLOAT:
1386 shadow_frame.SetVRegFloat(dec_insn.vA,
1387 shadow_frame.GetVRegFloat(dec_insn.vB) *
1388 shadow_frame.GetVRegFloat(dec_insn.vC));
1389 break;
1390 case Instruction::DIV_FLOAT:
1391 shadow_frame.SetVRegFloat(dec_insn.vA,
1392 shadow_frame.GetVRegFloat(dec_insn.vB) /
1393 shadow_frame.GetVRegFloat(dec_insn.vC));
1394 break;
1395 case Instruction::REM_FLOAT:
1396 shadow_frame.SetVRegFloat(dec_insn.vA,
1397 fmodf(shadow_frame.GetVRegFloat(dec_insn.vB),
1398 shadow_frame.GetVRegFloat(dec_insn.vC)));
1399 break;
1400 case Instruction::ADD_DOUBLE:
1401 shadow_frame.SetVRegDouble(dec_insn.vA,
1402 shadow_frame.GetVRegDouble(dec_insn.vB) +
1403 shadow_frame.GetVRegDouble(dec_insn.vC));
1404 break;
1405 case Instruction::SUB_DOUBLE:
1406 shadow_frame.SetVRegDouble(dec_insn.vA,
1407 shadow_frame.GetVRegDouble(dec_insn.vB) -
1408 shadow_frame.GetVRegDouble(dec_insn.vC));
1409 break;
1410 case Instruction::MUL_DOUBLE:
1411 shadow_frame.SetVRegDouble(dec_insn.vA,
1412 shadow_frame.GetVRegDouble(dec_insn.vB) *
1413 shadow_frame.GetVRegDouble(dec_insn.vC));
1414 break;
1415 case Instruction::DIV_DOUBLE:
1416 shadow_frame.SetVRegDouble(dec_insn.vA,
1417 shadow_frame.GetVRegDouble(dec_insn.vB) /
1418 shadow_frame.GetVRegDouble(dec_insn.vC));
1419 break;
1420 case Instruction::REM_DOUBLE:
1421 shadow_frame.SetVRegDouble(dec_insn.vA,
1422 fmod(shadow_frame.GetVRegDouble(dec_insn.vB),
1423 shadow_frame.GetVRegDouble(dec_insn.vC)));
1424 break;
1425 case Instruction::ADD_INT_2ADDR:
1426 shadow_frame.SetVReg(dec_insn.vA,
1427 shadow_frame.GetVReg(dec_insn.vA) + shadow_frame.GetVReg(dec_insn.vB));
1428 break;
1429 case Instruction::SUB_INT_2ADDR:
1430 shadow_frame.SetVReg(dec_insn.vA,
1431 shadow_frame.GetVReg(dec_insn.vA) - shadow_frame.GetVReg(dec_insn.vB));
1432 break;
1433 case Instruction::MUL_INT_2ADDR:
1434 shadow_frame.SetVReg(dec_insn.vA,
1435 shadow_frame.GetVReg(dec_insn.vA) * shadow_frame.GetVReg(dec_insn.vB));
1436 break;
1437 case Instruction::REM_INT_2ADDR:
1438 shadow_frame.SetVReg(dec_insn.vA,
1439 shadow_frame.GetVReg(dec_insn.vA) % shadow_frame.GetVReg(dec_insn.vB));
1440 break;
1441 case Instruction::SHL_INT_2ADDR:
1442 shadow_frame.SetVReg(dec_insn.vA,
1443 shadow_frame.GetVReg(dec_insn.vA) << shadow_frame.GetVReg(dec_insn.vB));
1444 break;
1445 case Instruction::SHR_INT_2ADDR:
1446 shadow_frame.SetVReg(dec_insn.vA,
1447 shadow_frame.GetVReg(dec_insn.vA) >> shadow_frame.GetVReg(dec_insn.vB));
1448 break;
1449 case Instruction::USHR_INT_2ADDR:
1450 shadow_frame.SetVReg(dec_insn.vA,
1451 static_cast<uint32_t>(shadow_frame.GetVReg(dec_insn.vA)) >>
1452 shadow_frame.GetVReg(dec_insn.vB));
1453 break;
1454 case Instruction::AND_INT_2ADDR:
1455 shadow_frame.SetVReg(dec_insn.vA,
1456 shadow_frame.GetVReg(dec_insn.vA) & shadow_frame.GetVReg(dec_insn.vB));
1457 break;
1458 case Instruction::OR_INT_2ADDR:
1459 shadow_frame.SetVReg(dec_insn.vA,
1460 shadow_frame.GetVReg(dec_insn.vA) | shadow_frame.GetVReg(dec_insn.vB));
1461 break;
1462 case Instruction::XOR_INT_2ADDR:
1463 shadow_frame.SetVReg(dec_insn.vA,
1464 shadow_frame.GetVReg(dec_insn.vA) ^ shadow_frame.GetVReg(dec_insn.vB));
1465 break;
1466 case Instruction::DIV_INT_2ADDR:
1467 shadow_frame.SetVReg(dec_insn.vA,
1468 shadow_frame.GetVReg(dec_insn.vA) / shadow_frame.GetVReg(dec_insn.vB));
1469 break;
1470 case Instruction::ADD_LONG_2ADDR:
1471 shadow_frame.SetVRegLong(dec_insn.vA,
1472 shadow_frame.GetVRegLong(dec_insn.vA) +
1473 shadow_frame.GetVRegLong(dec_insn.vB));
1474 break;
1475 case Instruction::SUB_LONG_2ADDR:
1476 shadow_frame.SetVRegLong(dec_insn.vA,
1477 shadow_frame.GetVRegLong(dec_insn.vA) -
1478 shadow_frame.GetVRegLong(dec_insn.vB));
1479 break;
1480 case Instruction::MUL_LONG_2ADDR:
1481 shadow_frame.SetVRegLong(dec_insn.vA,
Ian Rogers64b6d142012-10-29 16:34:15 -07001482 shadow_frame.GetVRegLong(dec_insn.vA) *
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001483 shadow_frame.GetVRegLong(dec_insn.vB));
1484 break;
1485 case Instruction::DIV_LONG_2ADDR:
1486 shadow_frame.SetVRegLong(dec_insn.vA,
1487 shadow_frame.GetVRegLong(dec_insn.vA) /
1488 shadow_frame.GetVRegLong(dec_insn.vB));
1489 break;
1490 case Instruction::REM_LONG_2ADDR:
1491 shadow_frame.SetVRegLong(dec_insn.vA,
1492 shadow_frame.GetVRegLong(dec_insn.vA) %
1493 shadow_frame.GetVRegLong(dec_insn.vB));
1494 break;
1495 case Instruction::AND_LONG_2ADDR:
1496 shadow_frame.SetVRegLong(dec_insn.vA,
1497 shadow_frame.GetVRegLong(dec_insn.vA) &
1498 shadow_frame.GetVRegLong(dec_insn.vB));
1499 break;
1500 case Instruction::OR_LONG_2ADDR:
1501 shadow_frame.SetVRegLong(dec_insn.vA,
1502 shadow_frame.GetVRegLong(dec_insn.vA) |
1503 shadow_frame.GetVRegLong(dec_insn.vB));
1504 break;
1505 case Instruction::XOR_LONG_2ADDR:
1506 shadow_frame.SetVRegLong(dec_insn.vA,
1507 shadow_frame.GetVRegLong(dec_insn.vA) ^
1508 shadow_frame.GetVRegLong(dec_insn.vB));
1509 break;
1510 case Instruction::SHL_LONG_2ADDR:
1511 shadow_frame.SetVRegLong(dec_insn.vA,
1512 shadow_frame.GetVRegLong(dec_insn.vA) <<
1513 shadow_frame.GetVReg(dec_insn.vB));
1514 break;
1515 case Instruction::SHR_LONG_2ADDR:
1516 shadow_frame.SetVRegLong(dec_insn.vA,
1517 shadow_frame.GetVRegLong(dec_insn.vA) >>
1518 shadow_frame.GetVReg(dec_insn.vB));
1519 break;
1520 case Instruction::USHR_LONG_2ADDR:
1521 shadow_frame.SetVRegLong(dec_insn.vA,
1522 static_cast<uint64_t>(shadow_frame.GetVRegLong(dec_insn.vA)) >>
1523 shadow_frame.GetVReg(dec_insn.vB));
1524 break;
1525 case Instruction::ADD_FLOAT_2ADDR:
1526 shadow_frame.SetVRegFloat(dec_insn.vA,
1527 shadow_frame.GetVRegFloat(dec_insn.vA) +
1528 shadow_frame.GetVRegFloat(dec_insn.vB));
1529 break;
1530 case Instruction::SUB_FLOAT_2ADDR:
1531 shadow_frame.SetVRegFloat(dec_insn.vA,
1532 shadow_frame.GetVRegFloat(dec_insn.vA) -
1533 shadow_frame.GetVRegFloat(dec_insn.vB));
1534 break;
1535 case Instruction::MUL_FLOAT_2ADDR:
1536 shadow_frame.SetVRegFloat(dec_insn.vA,
1537 shadow_frame.GetVRegFloat(dec_insn.vA) *
1538 shadow_frame.GetVRegFloat(dec_insn.vB));
1539 break;
1540 case Instruction::DIV_FLOAT_2ADDR:
1541 shadow_frame.SetVRegFloat(dec_insn.vA,
1542 shadow_frame.GetVRegFloat(dec_insn.vA) /
1543 shadow_frame.GetVRegFloat(dec_insn.vB));
1544 break;
1545 case Instruction::REM_FLOAT_2ADDR:
1546 shadow_frame.SetVRegFloat(dec_insn.vA,
1547 fmodf(shadow_frame.GetVRegFloat(dec_insn.vA),
1548 shadow_frame.GetVRegFloat(dec_insn.vB)));
1549 break;
1550 case Instruction::ADD_DOUBLE_2ADDR:
1551 shadow_frame.SetVRegDouble(dec_insn.vA,
1552 shadow_frame.GetVRegDouble(dec_insn.vA) +
1553 shadow_frame.GetVRegDouble(dec_insn.vB));
1554 break;
1555 case Instruction::SUB_DOUBLE_2ADDR:
1556 shadow_frame.SetVRegDouble(dec_insn.vA,
1557 shadow_frame.GetVRegDouble(dec_insn.vA) -
1558 shadow_frame.GetVRegDouble(dec_insn.vB));
1559 break;
1560 case Instruction::MUL_DOUBLE_2ADDR:
1561 shadow_frame.SetVRegDouble(dec_insn.vA,
1562 shadow_frame.GetVRegDouble(dec_insn.vA) *
1563 shadow_frame.GetVRegDouble(dec_insn.vB));
1564 break;
1565 case Instruction::DIV_DOUBLE_2ADDR:
1566 shadow_frame.SetVRegDouble(dec_insn.vA,
1567 shadow_frame.GetVRegDouble(dec_insn.vA) /
1568 shadow_frame.GetVRegDouble(dec_insn.vB));
1569 break;
1570 case Instruction::REM_DOUBLE_2ADDR:
1571 shadow_frame.SetVRegDouble(dec_insn.vA,
1572 fmod(shadow_frame.GetVRegDouble(dec_insn.vA),
1573 shadow_frame.GetVRegDouble(dec_insn.vB)));
1574 break;
1575 case Instruction::ADD_INT_LIT16:
1576 case Instruction::ADD_INT_LIT8:
1577 shadow_frame.SetVReg(dec_insn.vA, shadow_frame.GetVReg(dec_insn.vB) + dec_insn.vC);
1578 break;
1579 case Instruction::RSUB_INT:
1580 case Instruction::RSUB_INT_LIT8:
1581 shadow_frame.SetVReg(dec_insn.vA, dec_insn.vC - shadow_frame.GetVReg(dec_insn.vB));
1582 break;
1583 case Instruction::MUL_INT_LIT16:
1584 case Instruction::MUL_INT_LIT8:
1585 shadow_frame.SetVReg(dec_insn.vA, shadow_frame.GetVReg(dec_insn.vB) * dec_insn.vC);
1586 break;
1587 case Instruction::DIV_INT_LIT16:
1588 case Instruction::DIV_INT_LIT8:
1589 shadow_frame.SetVReg(dec_insn.vA, shadow_frame.GetVReg(dec_insn.vB) / dec_insn.vC);
1590 break;
1591 case Instruction::REM_INT_LIT16:
1592 case Instruction::REM_INT_LIT8:
1593 shadow_frame.SetVReg(dec_insn.vA, shadow_frame.GetVReg(dec_insn.vB) % dec_insn.vC);
1594 break;
1595 case Instruction::AND_INT_LIT16:
1596 case Instruction::AND_INT_LIT8:
1597 shadow_frame.SetVReg(dec_insn.vA, shadow_frame.GetVReg(dec_insn.vB) & dec_insn.vC);
1598 break;
1599 case Instruction::OR_INT_LIT16:
1600 case Instruction::OR_INT_LIT8:
1601 shadow_frame.SetVReg(dec_insn.vA, shadow_frame.GetVReg(dec_insn.vB) | dec_insn.vC);
1602 break;
1603 case Instruction::XOR_INT_LIT16:
1604 case Instruction::XOR_INT_LIT8:
1605 shadow_frame.SetVReg(dec_insn.vA, shadow_frame.GetVReg(dec_insn.vB) ^ dec_insn.vC);
1606 break;
1607 case Instruction::SHL_INT_LIT8:
1608 shadow_frame.SetVReg(dec_insn.vA, shadow_frame.GetVReg(dec_insn.vB) << dec_insn.vC);
1609 break;
1610 case Instruction::SHR_INT_LIT8:
1611 shadow_frame.SetVReg(dec_insn.vA, shadow_frame.GetVReg(dec_insn.vB) >> dec_insn.vC);
1612 break;
1613 case Instruction::USHR_INT_LIT8:
1614 shadow_frame.SetVReg(dec_insn.vA,
1615 static_cast<uint32_t>(shadow_frame.GetVReg(dec_insn.vB)) >>
1616 dec_insn.vC);
1617 break;
1618 default:
1619 LOG(FATAL) << "Unexpected instruction: " << inst->DumpString(&mh.GetDexFile());
1620 break;
1621 }
1622 if (UNLIKELY(self->IsExceptionPending())) {
1623 uint32_t found_dex_pc =
1624 shadow_frame.GetMethod()->FindCatchBlock(self->GetException()->GetClass(),
1625 inst->GetDexPc(insns));
1626 if (found_dex_pc == DexFile::kDexNoIndex) {
1627 JValue result;
1628 result.SetJ(0);
1629 return result; // Handler in caller.
1630 } else {
1631 next_inst = Instruction::At(insns + found_dex_pc);
1632 }
1633 }
1634 inst = next_inst;
1635 }
1636}
1637
1638void EnterInterpreterFromInvoke(Thread* self, AbstractMethod* method, Object* receiver,
1639 JValue* args, JValue* result) {
Ian Rogers64b6d142012-10-29 16:34:15 -07001640 DCHECK_EQ(self, Thread::Current());
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001641 MethodHelper mh(method);
1642 const DexFile::CodeItem* code_item = mh.GetCodeItem();
1643 uint16_t num_regs;
1644 uint16_t num_ins;
1645 if (code_item != NULL) {
1646 num_regs = code_item->registers_size_;
1647 num_ins = code_item->ins_size_;
1648 } else {
1649 DCHECK(method->IsNative());
1650 num_regs = num_ins = AbstractMethod::NumArgRegisters(mh.GetShorty());
1651 if (!method->IsStatic()) {
1652 num_regs++;
1653 num_ins++;
1654 }
1655 }
1656 // Set up shadow frame with matching number of reference slots to vregs.
1657 ShadowFrame* last_shadow_frame = self->GetManagedStack()->GetTopShadowFrame();
1658 UniquePtr<ShadowFrame> shadow_frame(ShadowFrame::Create(num_regs, num_regs,
1659 (last_shadow_frame == NULL) ? NULL : last_shadow_frame->GetLink(),
1660 method, 0));
1661 self->PushShadowFrame(shadow_frame.get());
1662 size_t cur_reg = num_regs - num_ins;
1663 if (!method->IsStatic()) {
1664 CHECK(receiver != NULL);
1665 shadow_frame->SetReferenceAndVReg(cur_reg, receiver);
1666 ++cur_reg;
1667 } else if (!method->GetDeclaringClass()->IsInitializing()) {
1668 Runtime::Current()->GetClassLinker()->EnsureInitialized(method->GetDeclaringClass(),
1669 true, true);
1670 CHECK(method->GetDeclaringClass()->IsInitializing());
1671 }
Ian Rogers64b6d142012-10-29 16:34:15 -07001672 const char* shorty = mh.GetShorty();
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001673 size_t arg_pos = 0;
1674 for (; cur_reg < num_regs; ++cur_reg, ++arg_pos) {
1675 DCHECK_LT(arg_pos + 1, mh.GetShortyLength());
1676 switch (shorty[arg_pos + 1]) {
1677 case 'L': {
1678 Object* o = args[arg_pos].GetL();
1679 shadow_frame->SetReferenceAndVReg(cur_reg, o);
1680 break;
1681 }
1682 case 'J': case 'D':
1683 shadow_frame->SetVRegLong(cur_reg, args[arg_pos].GetJ());
1684 cur_reg++;
1685 break;
1686 default:
1687 shadow_frame->SetVReg(cur_reg, args[arg_pos].GetI());
1688 break;
1689 }
1690 }
Ian Rogers64b6d142012-10-29 16:34:15 -07001691 if (LIKELY(!method->IsNative())) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001692 JValue r = Execute(self, mh, code_item, *shadow_frame.get());
1693 if (result != NULL) {
1694 *result = r;
1695 }
1696 } else {
Ian Rogers64b6d142012-10-29 16:34:15 -07001697 // We don't expect to be asked to interpret native code (which is entered via a JNI compiler
1698 // generated stub) except during testing and image writing.
1699 if (!Runtime::Current()->IsStarted()) {
1700 UnstartedRuntimeJni(self, method, receiver, args, result);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001701 } else {
Ian Rogers64b6d142012-10-29 16:34:15 -07001702 InterpreterJni(self, method, shorty, receiver, args, result);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001703 }
1704 }
1705 self->PopShadowFrame();
1706}
1707
1708} // namespace interpreter
1709} // namespace art