blob: 0a32f8f10ac5d9973f137ade1a74d0db33009093 [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));
479 shadow_frame.SetVReg(dec_insn.vA, f->GetByte(obj));
480 break;
481 case Primitive::kPrimChar:
482 f->SetChar(obj, shadow_frame.GetVReg(dec_insn.vA));
483 shadow_frame.SetVReg(dec_insn.vA, f->GetChar(obj));
484 break;
485 case Primitive::kPrimShort:
486 f->SetShort(obj, shadow_frame.GetVReg(dec_insn.vA));
487 shadow_frame.SetVReg(dec_insn.vA, f->GetShort(obj));
488 break;
489 case Primitive::kPrimInt:
490 f->SetInt(obj, shadow_frame.GetVReg(dec_insn.vA));
491 shadow_frame.SetVReg(dec_insn.vA, f->GetInt(obj));
492 break;
493 case Primitive::kPrimLong:
494 f->SetLong(obj, shadow_frame.GetVRegLong(dec_insn.vA));
495 shadow_frame.SetVRegLong(dec_insn.vA, f->GetLong(obj));
496 break;
497 case Primitive::kPrimNot:
498 f->SetObj(obj, shadow_frame.GetReference(dec_insn.vA));
499 break;
500 default:
501 LOG(FATAL) << "Unreachable: " << field_type;
502 }
503 }
504}
505
506static JValue Execute(Thread* self, MethodHelper& mh, const DexFile::CodeItem* code_item,
507 ShadowFrame& shadow_frame) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
508 const uint16_t* insns = code_item->insns_;
509 const Instruction* inst = Instruction::At(insns + shadow_frame.GetDexPC());
510 JValue result_register;
511 while (true) {
jeffhao373c52f2012-11-20 16:11:52 -0800512 CheckSuspend(self);
513 uint32_t dex_pc = inst->GetDexPc(insns);
514 shadow_frame.SetDexPC(dex_pc);
515 Dbg::UpdateDebugger(dex_pc, self);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700516 DecodedInstruction dec_insn(inst);
Ian Rogers64b6d142012-10-29 16:34:15 -0700517 const bool kTracing = false;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700518 if (kTracing) {
519 LOG(INFO) << PrettyMethod(shadow_frame.GetMethod())
520 << StringPrintf("\n0x%x: %s\nReferences:",
521 inst->GetDexPc(insns), inst->DumpString(&mh.GetDexFile()).c_str());
522 for (size_t i = 0; i < shadow_frame.NumberOfReferences(); ++i) {
523 Object* o = shadow_frame.GetReference(i);
524 if (o != NULL) {
525 if (o->GetClass()->IsStringClass() && o->AsString()->GetCharArray() != NULL) {
526 LOG(INFO) << i << ": java.lang.String " << static_cast<void*>(o)
527 << " \"" << o->AsString()->ToModifiedUtf8() << "\"";
528 } else {
529 LOG(INFO) << i << ": " << PrettyTypeOf(o) << " " << static_cast<void*>(o);
530 }
531 } else {
532 LOG(INFO) << i << ": null";
533 }
534 }
535 LOG(INFO) << "vregs:";
536 for (size_t i = 0; i < shadow_frame.NumberOfReferences(); ++i) {
537 LOG(INFO) << StringPrintf("%d: %08x", i, shadow_frame.GetVReg(i));
538 }
539 }
540 const Instruction* next_inst = inst->Next();
541 switch (dec_insn.opcode) {
542 case Instruction::NOP:
543 break;
544 case Instruction::MOVE:
545 case Instruction::MOVE_FROM16:
546 case Instruction::MOVE_16:
547 shadow_frame.SetVReg(dec_insn.vA, shadow_frame.GetVReg(dec_insn.vB));
548 break;
549 case Instruction::MOVE_WIDE:
550 case Instruction::MOVE_WIDE_FROM16:
551 case Instruction::MOVE_WIDE_16:
552 shadow_frame.SetVRegLong(dec_insn.vA, shadow_frame.GetVRegLong(dec_insn.vB));
553 break;
554 case Instruction::MOVE_OBJECT:
555 case Instruction::MOVE_OBJECT_FROM16:
556 case Instruction::MOVE_OBJECT_16:
557 shadow_frame.SetVReg(dec_insn.vA, shadow_frame.GetVReg(dec_insn.vB));
558 shadow_frame.SetReference(dec_insn.vA, shadow_frame.GetReference(dec_insn.vB));
559 break;
560 case Instruction::MOVE_RESULT:
561 shadow_frame.SetVReg(dec_insn.vA, result_register.GetI());
562 break;
563 case Instruction::MOVE_RESULT_WIDE:
564 shadow_frame.SetVRegLong(dec_insn.vA, result_register.GetJ());
565 break;
566 case Instruction::MOVE_RESULT_OBJECT:
567 shadow_frame.SetReferenceAndVReg(dec_insn.vA, result_register.GetL());
568 break;
569 case Instruction::MOVE_EXCEPTION: {
570 Throwable* exception = self->GetException();
571 self->ClearException();
572 shadow_frame.SetReferenceAndVReg(dec_insn.vA, exception);
573 break;
574 }
575 case Instruction::RETURN_VOID: {
576 JValue result;
577 result.SetJ(0);
578 return result;
579 }
580 case Instruction::RETURN: {
581 JValue result;
582 result.SetJ(0);
583 result.SetI(shadow_frame.GetVReg(dec_insn.vA));
584 return result;
585 }
586 case Instruction::RETURN_WIDE: {
587 JValue result;
588 result.SetJ(shadow_frame.GetVRegLong(dec_insn.vA));
589 return result;
590 }
591 case Instruction::RETURN_OBJECT: {
592 JValue result;
593 result.SetJ(0);
594 result.SetL(shadow_frame.GetReference(dec_insn.vA));
595 return result;
596 }
597 case Instruction::CONST_4: {
Ian Rogers64b6d142012-10-29 16:34:15 -0700598 int32_t val = static_cast<int32_t>(dec_insn.vB << 28) >> 28;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700599 shadow_frame.SetVReg(dec_insn.vA, val);
600 if (val == 0) {
601 shadow_frame.SetReference(dec_insn.vA, NULL);
602 }
603 break;
604 }
605 case Instruction::CONST_16: {
606 int32_t val = static_cast<int16_t>(dec_insn.vB);
607 shadow_frame.SetVReg(dec_insn.vA, val);
608 if (val == 0) {
609 shadow_frame.SetReference(dec_insn.vA, NULL);
610 }
611 break;
612 }
613 case Instruction::CONST: {
614 int32_t val = dec_insn.vB;
615 shadow_frame.SetVReg(dec_insn.vA, val);
616 if (val == 0) {
617 shadow_frame.SetReference(dec_insn.vA, NULL);
618 }
619 break;
620 }
621 case Instruction::CONST_HIGH16: {
622 int32_t val = dec_insn.vB << 16;
623 shadow_frame.SetVReg(dec_insn.vA, val);
624 if (val == 0) {
625 shadow_frame.SetReference(dec_insn.vA, NULL);
626 }
627 break;
628 }
Ian Rogers64b6d142012-10-29 16:34:15 -0700629 case Instruction::CONST_WIDE_16:
630 shadow_frame.SetVRegLong(dec_insn.vA, static_cast<int16_t>(dec_insn.vB));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700631 break;
Ian Rogers64b6d142012-10-29 16:34:15 -0700632 case Instruction::CONST_WIDE_32:
633 shadow_frame.SetVRegLong(dec_insn.vA, static_cast<int32_t>(dec_insn.vB));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700634 break;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700635 case Instruction::CONST_WIDE:
Ian Rogers64b6d142012-10-29 16:34:15 -0700636 shadow_frame.SetVRegLong(dec_insn.vA, dec_insn.vB_wide);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700637 break;
638 case Instruction::CONST_WIDE_HIGH16:
Ian Rogers64b6d142012-10-29 16:34:15 -0700639 shadow_frame.SetVRegLong(dec_insn.vA, static_cast<uint64_t>(dec_insn.vB) << 48);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700640 break;
641 case Instruction::CONST_STRING:
642 case Instruction::CONST_STRING_JUMBO: {
643 if (UNLIKELY(!String::GetJavaLangString()->IsInitialized())) {
644 Runtime::Current()->GetClassLinker()->EnsureInitialized(String::GetJavaLangString(),
645 true, true);
646 }
647 String* s = mh.ResolveString(dec_insn.vB);
648 shadow_frame.SetReferenceAndVReg(dec_insn.vA, s);
649 break;
650 }
651 case Instruction::CONST_CLASS:
Ian Rogers17ffcab2012-11-20 15:27:41 -0800652 shadow_frame.SetReferenceAndVReg(dec_insn.vA, mh.ResolveClass(dec_insn.vB));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700653 break;
654 case Instruction::MONITOR_ENTER:
655 DoMonitorEnter(self, shadow_frame.GetReference(dec_insn.vA));
656 break;
657 case Instruction::MONITOR_EXIT:
658 DoMonitorExit(self, shadow_frame.GetReference(dec_insn.vA));
659 break;
660 case Instruction::CHECK_CAST: {
661 Class* c = mh.ResolveClass(dec_insn.vB);
662 Object* obj = shadow_frame.GetReference(dec_insn.vA);
663 if (UNLIKELY(obj != NULL && !obj->InstanceOf(c))) {
664 self->ThrowNewExceptionF("Ljava/lang/ClassCastException;",
665 "%s cannot be cast to %s",
666 PrettyDescriptor(obj->GetClass()).c_str(),
667 PrettyDescriptor(c).c_str());
668 }
669 break;
670 }
671 case Instruction::INSTANCE_OF: {
672 Class* c = mh.ResolveClass(dec_insn.vC);
673 Object* obj = shadow_frame.GetReference(dec_insn.vB);
674 shadow_frame.SetVReg(dec_insn.vA, (obj != NULL && obj->InstanceOf(c)) ? 1 : 0);
675 break;
676 }
677 case Instruction::ARRAY_LENGTH: {
Ian Rogers64b6d142012-10-29 16:34:15 -0700678 Object* array = shadow_frame.GetReference(dec_insn.vB);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700679 if (UNLIKELY(array == NULL)) {
680 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetMethod(), inst->GetDexPc(insns));
681 break;
682 }
Ian Rogers64b6d142012-10-29 16:34:15 -0700683 shadow_frame.SetVReg(dec_insn.vA, array->AsArray()->GetLength());
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700684 break;
685 }
686 case Instruction::NEW_INSTANCE: {
687 Object* obj = AllocObjectFromCode(dec_insn.vB, shadow_frame.GetMethod(), self, true);
688 shadow_frame.SetReferenceAndVReg(dec_insn.vA, obj);
689 break;
690 }
691 case Instruction::NEW_ARRAY: {
692 int32_t length = shadow_frame.GetVReg(dec_insn.vB);
693 Object* obj = AllocArrayFromCode(dec_insn.vC, shadow_frame.GetMethod(), length, self, true);
694 shadow_frame.SetReferenceAndVReg(dec_insn.vA, obj);
695 break;
696 }
697 case Instruction::FILLED_NEW_ARRAY:
Ian Rogers64b6d142012-10-29 16:34:15 -0700698 case Instruction::FILLED_NEW_ARRAY_RANGE: {
699 bool is_range = (dec_insn.opcode == Instruction::FILLED_NEW_ARRAY_RANGE);
700 int32_t length = dec_insn.vA;
701 CHECK(is_range || length <= 5);
702 Class* arrayClass = mh.ResolveClass(dec_insn.vB);
703 CHECK(arrayClass->IsArrayClass());
704 if (arrayClass->GetComponentType()->IsPrimitiveInt()) {
705 IntArray* newArray = IntArray::Alloc(self, length);
706 if (newArray != NULL) {
707 for (int32_t i = 0; i < length; ++i) {
708 if (is_range) {
709 newArray->Set(i, shadow_frame.GetVReg(dec_insn.vC + i));
710 } else {
711 newArray->Set(i, shadow_frame.GetVReg(dec_insn.arg[i]));
712 }
713 }
714 }
715 result_register.SetL(newArray);
716 } else {
717 UNIMPLEMENTED(FATAL) << inst->DumpString(&mh.GetDexFile())
718 << " for array type: " << PrettyDescriptor(arrayClass);
719 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700720 break;
Ian Rogers64b6d142012-10-29 16:34:15 -0700721 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700722 case Instruction::CMPL_FLOAT: {
723 float val1 = shadow_frame.GetVRegFloat(dec_insn.vB);
724 float val2 = shadow_frame.GetVRegFloat(dec_insn.vC);
725 int32_t result;
726 if (val1 == val2) {
727 result = 0;
728 } else if (val1 > val2) {
729 result = 1;
730 } else {
731 result = -1;
732 }
733 shadow_frame.SetVReg(dec_insn.vA, result);
734 break;
735 }
736 case Instruction::CMPG_FLOAT: {
737 float val1 = shadow_frame.GetVRegFloat(dec_insn.vB);
738 float val2 = shadow_frame.GetVRegFloat(dec_insn.vC);
739 int32_t result;
740 if (val1 == val2) {
741 result = 0;
742 } else if (val1 < val2) {
743 result = -1;
744 } else {
745 result = 1;
746 }
747 shadow_frame.SetVReg(dec_insn.vA, result);
748 break;
749 }
750 case Instruction::CMPL_DOUBLE: {
751 double val1 = shadow_frame.GetVRegDouble(dec_insn.vB);
752 double val2 = shadow_frame.GetVRegDouble(dec_insn.vC);
753 int32_t result;
754 if (val1 == val2) {
755 result = 0;
Ian Rogers64b6d142012-10-29 16:34:15 -0700756 } else if (val1 < val2) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700757 result = 1;
758 } else {
759 result = -1;
760 }
761 shadow_frame.SetVReg(dec_insn.vA, result);
762 break;
763 }
764
765 case Instruction::CMPG_DOUBLE: {
766 double val1 = shadow_frame.GetVRegDouble(dec_insn.vB);
767 double val2 = shadow_frame.GetVRegDouble(dec_insn.vC);
768 int32_t result;
769 if (val1 == val2) {
770 result = 0;
Ian Rogers64b6d142012-10-29 16:34:15 -0700771 } else if (val1 > val2) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700772 result = -1;
773 } else {
774 result = 1;
775 }
776 shadow_frame.SetVReg(dec_insn.vA, result);
777 break;
778 }
779 case Instruction::CMP_LONG: {
780 int64_t val1 = shadow_frame.GetVRegLong(dec_insn.vB);
781 int64_t val2 = shadow_frame.GetVRegLong(dec_insn.vC);
782 int32_t result;
783 if (val1 < val2) {
Ian Rogers64b6d142012-10-29 16:34:15 -0700784 result = 1;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700785 } else if (val1 == val2) {
786 result = 0;
787 } else {
Ian Rogers64b6d142012-10-29 16:34:15 -0700788 result = -1;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700789 }
790 shadow_frame.SetVReg(dec_insn.vA, result);
791 break;
792 }
793 case Instruction::THROW: {
794 Throwable* t = shadow_frame.GetReference(dec_insn.vA)->AsThrowable();
795 self->SetException(t);
796 break;
797 }
798 case Instruction::GOTO:
799 case Instruction::GOTO_16:
800 case Instruction::GOTO_32: {
801 uint32_t dex_pc = inst->GetDexPc(insns);
802 next_inst = Instruction::At(insns + dex_pc + dec_insn.vA);
803 break;
804 }
Ian Rogers556d6372012-11-20 12:19:36 -0800805 case Instruction::PACKED_SWITCH: {
806 uint32_t dex_pc = inst->GetDexPc(insns);
807 const uint16_t* switch_data = insns + dex_pc + dec_insn.vB;
808 int32_t test_val = shadow_frame.GetVReg(dec_insn.vA);
809 CHECK_EQ(switch_data[0], static_cast<uint16_t>(Instruction::kPackedSwitchSignature));
810 uint16_t size = switch_data[1];
811 CHECK_GT(size, 0);
812 const int32_t* keys = reinterpret_cast<const int32_t*>(&switch_data[2]);
813 CHECK(IsAligned<4>(keys));
814 int32_t first_key = keys[0];
815 const int32_t* targets = reinterpret_cast<const int32_t*>(&switch_data[4]);
816 CHECK(IsAligned<4>(targets));
817 int32_t index = test_val - first_key;
818 if (index >= 0 && index < size) {
819 next_inst = Instruction::At(insns + dex_pc + targets[index]);
820 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700821 break;
Ian Rogers556d6372012-11-20 12:19:36 -0800822 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700823 case Instruction::SPARSE_SWITCH: {
824 uint32_t dex_pc = inst->GetDexPc(insns);
Ian Rogers556d6372012-11-20 12:19:36 -0800825 const uint16_t* switch_data = insns + dex_pc + dec_insn.vB;
826 int32_t test_val = shadow_frame.GetVReg(dec_insn.vA);
827 CHECK_EQ(switch_data[0], static_cast<uint16_t>(Instruction::kSparseSwitchSignature));
828 uint16_t size = switch_data[1];
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700829 CHECK_GT(size, 0);
Ian Rogers556d6372012-11-20 12:19:36 -0800830 const int32_t* keys = reinterpret_cast<const int32_t*>(&switch_data[2]);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700831 CHECK(IsAligned<4>(keys));
832 const int32_t* entries = keys + size;
833 CHECK(IsAligned<4>(entries));
834 int lo = 0;
835 int hi = size - 1;
836 while (lo <= hi) {
837 int mid = (lo + hi) / 2;
838 int32_t foundVal = keys[mid];
Ian Rogers556d6372012-11-20 12:19:36 -0800839 if (test_val < foundVal) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700840 hi = mid - 1;
Ian Rogers556d6372012-11-20 12:19:36 -0800841 } else if (test_val > foundVal) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700842 lo = mid + 1;
843 } else {
844 next_inst = Instruction::At(insns + dex_pc + entries[mid]);
845 break;
846 }
847 }
848 break;
849 }
850 case Instruction::FILL_ARRAY_DATA: {
851 Array* array = shadow_frame.GetReference(dec_insn.vA)->AsArray();
852 if (UNLIKELY(array == NULL)) {
853 Thread::Current()->ThrowNewExceptionF("Ljava/lang/NullPointerException;",
854 "null array in FILL_ARRAY_DATA");
855 break;
856 }
857 DCHECK(array->IsArrayInstance() && !array->IsObjectArray());
858 uint32_t dex_pc = inst->GetDexPc(insns);
859 const Instruction::ArrayDataPayload* payload =
860 reinterpret_cast<const Instruction::ArrayDataPayload*>(insns + dex_pc + dec_insn.vB);
861 if (UNLIKELY(static_cast<int32_t>(payload->element_count) > array->GetLength())) {
862 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
863 "failed FILL_ARRAY_DATA; length=%d, index=%d",
864 array->GetLength(), payload->element_count);
865 break;
866 }
867 uint32_t size_in_bytes = payload->element_count * payload->element_width;
868 memcpy(array->GetRawData(payload->element_width), payload->data, size_in_bytes);
869 break;
870 }
871 case Instruction::IF_EQ: {
872 if (shadow_frame.GetVReg(dec_insn.vA) == shadow_frame.GetVReg(dec_insn.vB)) {
873 uint32_t dex_pc = inst->GetDexPc(insns);
874 next_inst = Instruction::At(insns + dex_pc + dec_insn.vC);
875 }
876 break;
877 }
878 case Instruction::IF_NE: {
879 if (shadow_frame.GetVReg(dec_insn.vA) != shadow_frame.GetVReg(dec_insn.vB)) {
880 uint32_t dex_pc = inst->GetDexPc(insns);
881 next_inst = Instruction::At(insns + dex_pc + dec_insn.vC);
882 }
883 break;
884 }
885 case Instruction::IF_LT: {
886 if (shadow_frame.GetVReg(dec_insn.vA) < shadow_frame.GetVReg(dec_insn.vB)) {
887 uint32_t dex_pc = inst->GetDexPc(insns);
888 next_inst = Instruction::At(insns + dex_pc + dec_insn.vC);
889 }
890 break;
891 }
892 case Instruction::IF_GE: {
893 if (shadow_frame.GetVReg(dec_insn.vA) >= shadow_frame.GetVReg(dec_insn.vB)) {
894 uint32_t dex_pc = inst->GetDexPc(insns);
895 next_inst = Instruction::At(insns + dex_pc + dec_insn.vC);
896 }
897 break;
898 }
899 case Instruction::IF_GT: {
900 if (shadow_frame.GetVReg(dec_insn.vA) > shadow_frame.GetVReg(dec_insn.vB)) {
901 uint32_t dex_pc = inst->GetDexPc(insns);
902 next_inst = Instruction::At(insns + dex_pc + dec_insn.vC);
903 }
904 break;
905 }
906 case Instruction::IF_LE: {
907 if (shadow_frame.GetVReg(dec_insn.vA) <= shadow_frame.GetVReg(dec_insn.vB)) {
908 uint32_t dex_pc = inst->GetDexPc(insns);
909 next_inst = Instruction::At(insns + dex_pc + dec_insn.vC);
910 }
911 break;
912 }
913 case Instruction::IF_EQZ: {
914 if (shadow_frame.GetVReg(dec_insn.vA) == 0) {
915 uint32_t dex_pc = inst->GetDexPc(insns);
916 next_inst = Instruction::At(insns + dex_pc + dec_insn.vB);
917 }
918 break;
919 }
920 case Instruction::IF_NEZ: {
921 if (shadow_frame.GetVReg(dec_insn.vA) != 0) {
922 uint32_t dex_pc = inst->GetDexPc(insns);
923 next_inst = Instruction::At(insns + dex_pc + dec_insn.vB);
924 }
925 break;
926 }
927 case Instruction::IF_LTZ: {
928 if (shadow_frame.GetVReg(dec_insn.vA) < 0) {
929 uint32_t dex_pc = inst->GetDexPc(insns);
930 next_inst = Instruction::At(insns + dex_pc + dec_insn.vB);
931 }
932 break;
933 }
934 case Instruction::IF_GEZ: {
935 if (shadow_frame.GetVReg(dec_insn.vA) >= 0) {
936 uint32_t dex_pc = inst->GetDexPc(insns);
937 next_inst = Instruction::At(insns + dex_pc + dec_insn.vB);
938 }
939 break;
940 }
941 case Instruction::IF_GTZ: {
942 if (shadow_frame.GetVReg(dec_insn.vA) > 0) {
943 uint32_t dex_pc = inst->GetDexPc(insns);
944 next_inst = Instruction::At(insns + dex_pc + dec_insn.vB);
945 }
946 break;
947 }
948 case Instruction::IF_LEZ: {
949 if (shadow_frame.GetVReg(dec_insn.vA) <= 0) {
950 uint32_t dex_pc = inst->GetDexPc(insns);
951 next_inst = Instruction::At(insns + dex_pc + dec_insn.vB);
952 }
953 break;
954 }
955 case Instruction::AGET_BOOLEAN: {
956 BooleanArray* a = shadow_frame.GetReference(dec_insn.vB)->AsBooleanArray();
957 if (UNLIKELY(a == NULL)) {
958 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetMethod(), inst->GetDexPc(insns));
959 break;
960 }
961 int32_t index = shadow_frame.GetVReg(dec_insn.vC);
962 shadow_frame.SetVReg(dec_insn.vA, a->Get(index));
963 break;
964 }
965 case Instruction::AGET_BYTE: {
966 ByteArray* a = shadow_frame.GetReference(dec_insn.vB)->AsByteArray();
967 if (UNLIKELY(a == NULL)) {
968 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetMethod(), inst->GetDexPc(insns));
969 break;
970 }
971 int32_t index = shadow_frame.GetVReg(dec_insn.vC);
972 shadow_frame.SetVReg(dec_insn.vA, a->Get(index));
973 break;
974 }
975 case Instruction::AGET_CHAR: {
976 CharArray* a = shadow_frame.GetReference(dec_insn.vB)->AsCharArray();
977 if (UNLIKELY(a == NULL)) {
978 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetMethod(), inst->GetDexPc(insns));
979 break;
980 }
981 int32_t index = shadow_frame.GetVReg(dec_insn.vC);
982 shadow_frame.SetVReg(dec_insn.vA, a->Get(index));
983 break;
984 }
985 case Instruction::AGET_SHORT: {
986 ShortArray* a = shadow_frame.GetReference(dec_insn.vB)->AsShortArray();
987 if (UNLIKELY(a == NULL)) {
988 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetMethod(), inst->GetDexPc(insns));
989 break;
990 }
991 int32_t index = shadow_frame.GetVReg(dec_insn.vC);
992 shadow_frame.SetVReg(dec_insn.vA, a->Get(index));
993 break;
994 }
995 case Instruction::AGET: {
996 IntArray* a = shadow_frame.GetReference(dec_insn.vB)->AsIntArray();
997 if (UNLIKELY(a == NULL)) {
998 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetMethod(), inst->GetDexPc(insns));
999 break;
1000 }
1001 int32_t index = shadow_frame.GetVReg(dec_insn.vC);
1002 shadow_frame.SetVReg(dec_insn.vA, a->Get(index));
1003 break;
1004 }
1005 case Instruction::AGET_WIDE: {
1006 LongArray* a = shadow_frame.GetReference(dec_insn.vB)->AsLongArray();
1007 if (UNLIKELY(a == NULL)) {
1008 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetMethod(), inst->GetDexPc(insns));
1009 break;
1010 }
1011 int32_t index = shadow_frame.GetVReg(dec_insn.vC);
1012 shadow_frame.SetVRegLong(dec_insn.vA, a->Get(index));
1013 break;
1014 }
1015 case Instruction::AGET_OBJECT: {
1016 ObjectArray<Object>* a = shadow_frame.GetReference(dec_insn.vB)->AsObjectArray<Object>();
1017 if (UNLIKELY(a == NULL)) {
1018 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetMethod(), inst->GetDexPc(insns));
1019 break;
1020 }
1021 int32_t index = shadow_frame.GetVReg(dec_insn.vC);
1022 Object* o = a->Get(index);
1023 shadow_frame.SetReferenceAndVReg(dec_insn.vA, o);
1024 break;
1025 }
1026 case Instruction::APUT_BOOLEAN: {
1027 uint8_t val = shadow_frame.GetVReg(dec_insn.vA);
1028 BooleanArray* a = shadow_frame.GetReference(dec_insn.vB)->AsBooleanArray();
1029 if (UNLIKELY(a == NULL)) {
1030 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetMethod(), inst->GetDexPc(insns));
1031 break;
1032 }
1033 int32_t index = shadow_frame.GetVReg(dec_insn.vC);
1034 a->Set(index, val);
1035 break;
1036 }
1037 case Instruction::APUT_BYTE: {
1038 int8_t val = shadow_frame.GetVReg(dec_insn.vA);
1039 ByteArray* a = shadow_frame.GetReference(dec_insn.vB)->AsByteArray();
1040 if (UNLIKELY(a == NULL)) {
1041 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetMethod(), inst->GetDexPc(insns));
1042 break;
1043 }
1044 int32_t index = shadow_frame.GetVReg(dec_insn.vC);
1045 a->Set(index, val);
1046 break;
1047 }
1048 case Instruction::APUT_CHAR: {
1049 uint16_t val = shadow_frame.GetVReg(dec_insn.vA);
1050 CharArray* a = shadow_frame.GetReference(dec_insn.vB)->AsCharArray();
1051 if (UNLIKELY(a == NULL)) {
1052 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetMethod(), inst->GetDexPc(insns));
1053 break;
1054 }
1055 int32_t index = shadow_frame.GetVReg(dec_insn.vC);
1056 a->Set(index, val);
1057 break;
1058 }
1059 case Instruction::APUT_SHORT: {
1060 int16_t val = shadow_frame.GetVReg(dec_insn.vA);
1061 ShortArray* a = shadow_frame.GetReference(dec_insn.vB)->AsShortArray();
1062 if (UNLIKELY(a == NULL)) {
1063 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetMethod(), inst->GetDexPc(insns));
1064 break;
1065 }
1066 int32_t index = shadow_frame.GetVReg(dec_insn.vC);
1067 a->Set(index, val);
1068 break;
1069 }
1070 case Instruction::APUT: {
1071 int32_t val = shadow_frame.GetVReg(dec_insn.vA);
1072 IntArray* a = shadow_frame.GetReference(dec_insn.vB)->AsIntArray();
1073 if (UNLIKELY(a == NULL)) {
1074 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetMethod(), inst->GetDexPc(insns));
1075 break;
1076 }
1077 int32_t index = shadow_frame.GetVReg(dec_insn.vC);
1078 a->Set(index, val);
1079 break;
1080 }
1081 case Instruction::APUT_WIDE: {
1082 int64_t val = shadow_frame.GetVRegLong(dec_insn.vA);
1083 LongArray* a = shadow_frame.GetReference(dec_insn.vB)->AsLongArray();
1084 if (UNLIKELY(a == NULL)) {
1085 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetMethod(), inst->GetDexPc(insns));
1086 break;
1087 }
1088 int32_t index = shadow_frame.GetVReg(dec_insn.vC);
1089 a->Set(index, val);
1090 break;
1091 }
1092 case Instruction::APUT_OBJECT: {
1093 Object* val = shadow_frame.GetReference(dec_insn.vA);
1094 ObjectArray<Object>* a = shadow_frame.GetReference(dec_insn.vB)->AsObjectArray<Object>();
1095 if (UNLIKELY(a == NULL)) {
1096 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetMethod(), inst->GetDexPc(insns));
1097 break;
1098 }
1099 int32_t index = shadow_frame.GetVReg(dec_insn.vC);
1100 a->Set(index, val);
1101 break;
1102 }
1103 case Instruction::IGET_BOOLEAN:
1104 DoFieldGet(self, shadow_frame, dec_insn, InstancePrimitiveRead, Primitive::kPrimBoolean);
1105 break;
1106 case Instruction::IGET_BYTE:
1107 DoFieldGet(self, shadow_frame, dec_insn, InstancePrimitiveRead, Primitive::kPrimByte);
1108 break;
1109 case Instruction::IGET_CHAR:
1110 DoFieldGet(self, shadow_frame, dec_insn, InstancePrimitiveRead, Primitive::kPrimChar);
1111 break;
1112 case Instruction::IGET_SHORT:
1113 DoFieldGet(self, shadow_frame, dec_insn, InstancePrimitiveRead, Primitive::kPrimShort);
1114 break;
1115 case Instruction::IGET:
1116 DoFieldGet(self, shadow_frame, dec_insn, InstancePrimitiveRead, Primitive::kPrimInt);
1117 break;
1118 case Instruction::IGET_WIDE:
1119 DoFieldGet(self, shadow_frame, dec_insn, InstancePrimitiveRead, Primitive::kPrimLong);
1120 break;
1121 case Instruction::IGET_OBJECT:
1122 DoFieldGet(self, shadow_frame, dec_insn, InstanceObjectRead, Primitive::kPrimNot);
1123 break;
1124 case Instruction::SGET_BOOLEAN:
1125 DoFieldGet(self, shadow_frame, dec_insn, StaticPrimitiveRead, Primitive::kPrimBoolean);
1126 break;
1127 case Instruction::SGET_BYTE:
1128 DoFieldGet(self, shadow_frame, dec_insn, StaticPrimitiveRead, Primitive::kPrimByte);
1129 break;
1130 case Instruction::SGET_CHAR:
1131 DoFieldGet(self, shadow_frame, dec_insn, StaticPrimitiveRead, Primitive::kPrimChar);
1132 break;
1133 case Instruction::SGET_SHORT:
1134 DoFieldGet(self, shadow_frame, dec_insn, StaticPrimitiveRead, Primitive::kPrimShort);
1135 break;
1136 case Instruction::SGET:
1137 DoFieldGet(self, shadow_frame, dec_insn, StaticPrimitiveRead, Primitive::kPrimInt);
1138 break;
1139 case Instruction::SGET_WIDE:
1140 DoFieldGet(self, shadow_frame, dec_insn, StaticPrimitiveRead, Primitive::kPrimLong);
1141 break;
1142 case Instruction::SGET_OBJECT:
1143 DoFieldGet(self, shadow_frame, dec_insn, StaticObjectRead, Primitive::kPrimNot);
1144 break;
1145 case Instruction::IPUT_BOOLEAN:
1146 DoFieldPut(self, shadow_frame, dec_insn, InstancePrimitiveWrite, Primitive::kPrimBoolean);
1147 break;
1148 case Instruction::IPUT_BYTE:
1149 DoFieldPut(self, shadow_frame, dec_insn, InstancePrimitiveWrite, Primitive::kPrimByte);
1150 break;
1151 case Instruction::IPUT_CHAR:
1152 DoFieldPut(self, shadow_frame, dec_insn, InstancePrimitiveWrite, Primitive::kPrimChar);
1153 break;
1154 case Instruction::IPUT_SHORT:
1155 DoFieldPut(self, shadow_frame, dec_insn, InstancePrimitiveWrite, Primitive::kPrimShort);
1156 break;
1157 case Instruction::IPUT:
1158 DoFieldPut(self, shadow_frame, dec_insn, InstancePrimitiveWrite, Primitive::kPrimInt);
1159 break;
1160 case Instruction::IPUT_WIDE:
1161 DoFieldPut(self, shadow_frame, dec_insn, InstancePrimitiveWrite, Primitive::kPrimLong);
1162 break;
1163 case Instruction::IPUT_OBJECT:
1164 DoFieldPut(self, shadow_frame, dec_insn, InstanceObjectWrite, Primitive::kPrimNot);
1165 break;
1166 case Instruction::SPUT_BOOLEAN:
1167 DoFieldPut(self, shadow_frame, dec_insn, StaticPrimitiveWrite, Primitive::kPrimBoolean);
1168 break;
1169 case Instruction::SPUT_BYTE:
1170 DoFieldPut(self, shadow_frame, dec_insn, StaticPrimitiveWrite, Primitive::kPrimByte);
1171 break;
1172 case Instruction::SPUT_CHAR:
1173 DoFieldPut(self, shadow_frame, dec_insn, StaticPrimitiveWrite, Primitive::kPrimChar);
1174 break;
1175 case Instruction::SPUT_SHORT:
1176 DoFieldPut(self, shadow_frame, dec_insn, StaticPrimitiveWrite, Primitive::kPrimShort);
1177 break;
1178 case Instruction::SPUT:
1179 DoFieldPut(self, shadow_frame, dec_insn, StaticPrimitiveWrite, Primitive::kPrimInt);
1180 break;
1181 case Instruction::SPUT_WIDE:
1182 DoFieldPut(self, shadow_frame, dec_insn, StaticPrimitiveWrite, Primitive::kPrimLong);
1183 break;
1184 case Instruction::SPUT_OBJECT:
1185 DoFieldPut(self, shadow_frame, dec_insn, StaticObjectWrite, Primitive::kPrimNot);
1186 break;
1187 case Instruction::INVOKE_VIRTUAL:
1188 DoInvoke(self, mh, shadow_frame, dec_insn, kVirtual, false, &result_register);
1189 break;
1190 case Instruction::INVOKE_VIRTUAL_RANGE:
1191 DoInvoke(self, mh, shadow_frame, dec_insn, kVirtual, true, &result_register);
1192 break;
1193 case Instruction::INVOKE_SUPER:
1194 DoInvoke(self, mh, shadow_frame, dec_insn, kSuper, false, &result_register);
1195 break;
1196 case Instruction::INVOKE_SUPER_RANGE:
1197 DoInvoke(self, mh, shadow_frame, dec_insn, kSuper, true, &result_register);
1198 break;
1199 case Instruction::INVOKE_DIRECT:
1200 DoInvoke(self, mh, shadow_frame, dec_insn, kDirect, false, &result_register);
1201 break;
1202 case Instruction::INVOKE_DIRECT_RANGE:
1203 DoInvoke(self, mh, shadow_frame, dec_insn, kDirect, true, &result_register);
1204 break;
1205 case Instruction::INVOKE_INTERFACE:
1206 DoInvoke(self, mh, shadow_frame, dec_insn, kInterface, false, &result_register);
1207 break;
1208 case Instruction::INVOKE_INTERFACE_RANGE:
1209 DoInvoke(self, mh, shadow_frame, dec_insn, kInterface, true, &result_register);
1210 break;
1211 case Instruction::INVOKE_STATIC:
1212 DoInvoke(self, mh, shadow_frame, dec_insn, kStatic, false, &result_register);
1213 break;
1214 case Instruction::INVOKE_STATIC_RANGE:
1215 DoInvoke(self, mh, shadow_frame, dec_insn, kStatic, true, &result_register);
1216 break;
1217 case Instruction::NEG_INT:
1218 shadow_frame.SetVReg(dec_insn.vA, -shadow_frame.GetVReg(dec_insn.vB));
1219 break;
1220 case Instruction::NOT_INT:
1221 shadow_frame.SetVReg(dec_insn.vA, 0 ^ shadow_frame.GetVReg(dec_insn.vB));
1222 break;
1223 case Instruction::NEG_LONG:
1224 shadow_frame.SetVRegLong(dec_insn.vA, -shadow_frame.GetVRegLong(dec_insn.vB));
1225 break;
1226 case Instruction::NOT_LONG:
1227 shadow_frame.SetVRegLong(dec_insn.vA, 0 ^ shadow_frame.GetVRegLong(dec_insn.vB));
1228 break;
1229 case Instruction::NEG_FLOAT:
1230 shadow_frame.SetVRegFloat(dec_insn.vA, -shadow_frame.GetVRegFloat(dec_insn.vB));
1231 break;
1232 case Instruction::NEG_DOUBLE:
1233 shadow_frame.SetVRegDouble(dec_insn.vA, -shadow_frame.GetVRegDouble(dec_insn.vB));
1234 break;
1235 case Instruction::INT_TO_LONG:
1236 shadow_frame.SetVRegLong(dec_insn.vA, shadow_frame.GetVReg(dec_insn.vB));
1237 break;
1238 case Instruction::INT_TO_FLOAT:
1239 shadow_frame.SetVRegFloat(dec_insn.vA, shadow_frame.GetVReg(dec_insn.vB));
1240 break;
1241 case Instruction::INT_TO_DOUBLE:
1242 shadow_frame.SetVRegDouble(dec_insn.vA, shadow_frame.GetVReg(dec_insn.vB));
1243 break;
1244 case Instruction::LONG_TO_INT:
1245 shadow_frame.SetVReg(dec_insn.vA, shadow_frame.GetVRegLong(dec_insn.vB));
1246 break;
1247 case Instruction::LONG_TO_FLOAT:
1248 shadow_frame.SetVRegFloat(dec_insn.vA, shadow_frame.GetVRegLong(dec_insn.vB));
1249 break;
1250 case Instruction::LONG_TO_DOUBLE:
1251 shadow_frame.SetVRegDouble(dec_insn.vA, shadow_frame.GetVRegLong(dec_insn.vB));
1252 break;
1253 case Instruction::FLOAT_TO_INT:
1254 shadow_frame.SetVReg(dec_insn.vA, shadow_frame.GetVRegFloat(dec_insn.vB));
1255 break;
1256 case Instruction::FLOAT_TO_LONG:
1257 shadow_frame.SetVRegLong(dec_insn.vA, shadow_frame.GetVRegFloat(dec_insn.vB));
1258 break;
1259 case Instruction::FLOAT_TO_DOUBLE:
1260 shadow_frame.SetVRegDouble(dec_insn.vA, shadow_frame.GetVRegFloat(dec_insn.vB));
1261 break;
1262 case Instruction::DOUBLE_TO_INT:
1263 shadow_frame.SetVReg(dec_insn.vA, shadow_frame.GetVRegDouble(dec_insn.vB));
1264 break;
1265 case Instruction::DOUBLE_TO_LONG:
1266 shadow_frame.SetVRegLong(dec_insn.vA, shadow_frame.GetVRegDouble(dec_insn.vB));
1267 break;
1268 case Instruction::DOUBLE_TO_FLOAT:
1269 shadow_frame.SetVRegFloat(dec_insn.vA, shadow_frame.GetVRegDouble(dec_insn.vB));
1270 break;
1271 case Instruction::INT_TO_BYTE:
1272 shadow_frame.SetVReg(dec_insn.vA, static_cast<int8_t>(shadow_frame.GetVReg(dec_insn.vB)));
1273 break;
1274 case Instruction::INT_TO_CHAR:
1275 shadow_frame.SetVReg(dec_insn.vA, static_cast<uint16_t>(shadow_frame.GetVReg(dec_insn.vB)));
1276 break;
1277 case Instruction::INT_TO_SHORT:
1278 shadow_frame.SetVReg(dec_insn.vA, static_cast<int16_t>(shadow_frame.GetVReg(dec_insn.vB)));
1279 break;
1280 case Instruction::ADD_INT:
1281 shadow_frame.SetVReg(dec_insn.vA,
1282 shadow_frame.GetVReg(dec_insn.vB) + shadow_frame.GetVReg(dec_insn.vC));
1283 break;
1284 case Instruction::SUB_INT:
1285 shadow_frame.SetVReg(dec_insn.vA,
1286 shadow_frame.GetVReg(dec_insn.vB) - shadow_frame.GetVReg(dec_insn.vC));
1287 break;
1288 case Instruction::MUL_INT:
1289 shadow_frame.SetVReg(dec_insn.vA,
1290 shadow_frame.GetVReg(dec_insn.vB) * shadow_frame.GetVReg(dec_insn.vC));
1291 break;
1292 case Instruction::REM_INT:
1293 shadow_frame.SetVReg(dec_insn.vA,
1294 shadow_frame.GetVReg(dec_insn.vB) % shadow_frame.GetVReg(dec_insn.vC));
1295 break;
1296 case Instruction::DIV_INT:
1297 shadow_frame.SetVReg(dec_insn.vA,
1298 shadow_frame.GetVReg(dec_insn.vB) / shadow_frame.GetVReg(dec_insn.vC));
1299 break;
1300 case Instruction::SHL_INT:
1301 shadow_frame.SetVReg(dec_insn.vA,
1302 shadow_frame.GetVReg(dec_insn.vB) << shadow_frame.GetVReg(dec_insn.vC));
1303 break;
1304 case Instruction::SHR_INT:
1305 shadow_frame.SetVReg(dec_insn.vA,
1306 shadow_frame.GetVReg(dec_insn.vB) >> shadow_frame.GetVReg(dec_insn.vC));
1307 break;
1308 case Instruction::USHR_INT:
1309 shadow_frame.SetVReg(dec_insn.vA,
1310 static_cast<uint32_t>(shadow_frame.GetVReg(dec_insn.vB)) >>
1311 shadow_frame.GetVReg(dec_insn.vC));
1312 break;
1313 case Instruction::AND_INT:
1314 shadow_frame.SetVReg(dec_insn.vA,
1315 shadow_frame.GetVReg(dec_insn.vB) & shadow_frame.GetVReg(dec_insn.vC));
1316 break;
1317 case Instruction::OR_INT:
1318 shadow_frame.SetVReg(dec_insn.vA,
1319 shadow_frame.GetVReg(dec_insn.vB) | shadow_frame.GetVReg(dec_insn.vC));
1320 break;
1321 case Instruction::XOR_INT:
1322 shadow_frame.SetVReg(dec_insn.vA,
1323 shadow_frame.GetVReg(dec_insn.vB) ^ shadow_frame.GetVReg(dec_insn.vC));
1324 break;
1325 case Instruction::ADD_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::SUB_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::MUL_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::DIV_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::REM_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::AND_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::OR_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::XOR_LONG:
1361 shadow_frame.SetVRegLong(dec_insn.vA,
1362 shadow_frame.GetVRegLong(dec_insn.vB) ^
1363 shadow_frame.GetVRegLong(dec_insn.vC));
1364 break;
1365 case Instruction::SHL_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::SHR_LONG:
1371 shadow_frame.SetVRegLong(dec_insn.vA,
1372 shadow_frame.GetVRegLong(dec_insn.vB) >>
1373 shadow_frame.GetVReg(dec_insn.vC));
1374 break;
1375 case Instruction::USHR_LONG:
1376 shadow_frame.SetVRegLong(dec_insn.vA,
1377 static_cast<uint64_t>(shadow_frame.GetVRegLong(dec_insn.vB)) >>
1378 shadow_frame.GetVReg(dec_insn.vC));
1379 break;
1380 case Instruction::ADD_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::SUB_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::MUL_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::DIV_FLOAT:
1396 shadow_frame.SetVRegFloat(dec_insn.vA,
1397 shadow_frame.GetVRegFloat(dec_insn.vB) /
1398 shadow_frame.GetVRegFloat(dec_insn.vC));
1399 break;
1400 case Instruction::REM_FLOAT:
1401 shadow_frame.SetVRegFloat(dec_insn.vA,
1402 fmodf(shadow_frame.GetVRegFloat(dec_insn.vB),
1403 shadow_frame.GetVRegFloat(dec_insn.vC)));
1404 break;
1405 case Instruction::ADD_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::SUB_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::MUL_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::DIV_DOUBLE:
1421 shadow_frame.SetVRegDouble(dec_insn.vA,
1422 shadow_frame.GetVRegDouble(dec_insn.vB) /
1423 shadow_frame.GetVRegDouble(dec_insn.vC));
1424 break;
1425 case Instruction::REM_DOUBLE:
1426 shadow_frame.SetVRegDouble(dec_insn.vA,
1427 fmod(shadow_frame.GetVRegDouble(dec_insn.vB),
1428 shadow_frame.GetVRegDouble(dec_insn.vC)));
1429 break;
1430 case Instruction::ADD_INT_2ADDR:
1431 shadow_frame.SetVReg(dec_insn.vA,
1432 shadow_frame.GetVReg(dec_insn.vA) + shadow_frame.GetVReg(dec_insn.vB));
1433 break;
1434 case Instruction::SUB_INT_2ADDR:
1435 shadow_frame.SetVReg(dec_insn.vA,
1436 shadow_frame.GetVReg(dec_insn.vA) - shadow_frame.GetVReg(dec_insn.vB));
1437 break;
1438 case Instruction::MUL_INT_2ADDR:
1439 shadow_frame.SetVReg(dec_insn.vA,
1440 shadow_frame.GetVReg(dec_insn.vA) * shadow_frame.GetVReg(dec_insn.vB));
1441 break;
1442 case Instruction::REM_INT_2ADDR:
1443 shadow_frame.SetVReg(dec_insn.vA,
1444 shadow_frame.GetVReg(dec_insn.vA) % shadow_frame.GetVReg(dec_insn.vB));
1445 break;
1446 case Instruction::SHL_INT_2ADDR:
1447 shadow_frame.SetVReg(dec_insn.vA,
1448 shadow_frame.GetVReg(dec_insn.vA) << shadow_frame.GetVReg(dec_insn.vB));
1449 break;
1450 case Instruction::SHR_INT_2ADDR:
1451 shadow_frame.SetVReg(dec_insn.vA,
1452 shadow_frame.GetVReg(dec_insn.vA) >> shadow_frame.GetVReg(dec_insn.vB));
1453 break;
1454 case Instruction::USHR_INT_2ADDR:
1455 shadow_frame.SetVReg(dec_insn.vA,
1456 static_cast<uint32_t>(shadow_frame.GetVReg(dec_insn.vA)) >>
1457 shadow_frame.GetVReg(dec_insn.vB));
1458 break;
1459 case Instruction::AND_INT_2ADDR:
1460 shadow_frame.SetVReg(dec_insn.vA,
1461 shadow_frame.GetVReg(dec_insn.vA) & shadow_frame.GetVReg(dec_insn.vB));
1462 break;
1463 case Instruction::OR_INT_2ADDR:
1464 shadow_frame.SetVReg(dec_insn.vA,
1465 shadow_frame.GetVReg(dec_insn.vA) | shadow_frame.GetVReg(dec_insn.vB));
1466 break;
1467 case Instruction::XOR_INT_2ADDR:
1468 shadow_frame.SetVReg(dec_insn.vA,
1469 shadow_frame.GetVReg(dec_insn.vA) ^ shadow_frame.GetVReg(dec_insn.vB));
1470 break;
1471 case Instruction::DIV_INT_2ADDR:
1472 shadow_frame.SetVReg(dec_insn.vA,
1473 shadow_frame.GetVReg(dec_insn.vA) / shadow_frame.GetVReg(dec_insn.vB));
1474 break;
1475 case Instruction::ADD_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::SUB_LONG_2ADDR:
1481 shadow_frame.SetVRegLong(dec_insn.vA,
1482 shadow_frame.GetVRegLong(dec_insn.vA) -
1483 shadow_frame.GetVRegLong(dec_insn.vB));
1484 break;
1485 case Instruction::MUL_LONG_2ADDR:
1486 shadow_frame.SetVRegLong(dec_insn.vA,
Ian Rogers64b6d142012-10-29 16:34:15 -07001487 shadow_frame.GetVRegLong(dec_insn.vA) *
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001488 shadow_frame.GetVRegLong(dec_insn.vB));
1489 break;
1490 case Instruction::DIV_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::REM_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::AND_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::OR_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::XOR_LONG_2ADDR:
1511 shadow_frame.SetVRegLong(dec_insn.vA,
1512 shadow_frame.GetVRegLong(dec_insn.vA) ^
1513 shadow_frame.GetVRegLong(dec_insn.vB));
1514 break;
1515 case Instruction::SHL_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::SHR_LONG_2ADDR:
1521 shadow_frame.SetVRegLong(dec_insn.vA,
1522 shadow_frame.GetVRegLong(dec_insn.vA) >>
1523 shadow_frame.GetVReg(dec_insn.vB));
1524 break;
1525 case Instruction::USHR_LONG_2ADDR:
1526 shadow_frame.SetVRegLong(dec_insn.vA,
1527 static_cast<uint64_t>(shadow_frame.GetVRegLong(dec_insn.vA)) >>
1528 shadow_frame.GetVReg(dec_insn.vB));
1529 break;
1530 case Instruction::ADD_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::SUB_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::MUL_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::DIV_FLOAT_2ADDR:
1546 shadow_frame.SetVRegFloat(dec_insn.vA,
1547 shadow_frame.GetVRegFloat(dec_insn.vA) /
1548 shadow_frame.GetVRegFloat(dec_insn.vB));
1549 break;
1550 case Instruction::REM_FLOAT_2ADDR:
1551 shadow_frame.SetVRegFloat(dec_insn.vA,
1552 fmodf(shadow_frame.GetVRegFloat(dec_insn.vA),
1553 shadow_frame.GetVRegFloat(dec_insn.vB)));
1554 break;
1555 case Instruction::ADD_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::SUB_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::MUL_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::DIV_DOUBLE_2ADDR:
1571 shadow_frame.SetVRegDouble(dec_insn.vA,
1572 shadow_frame.GetVRegDouble(dec_insn.vA) /
1573 shadow_frame.GetVRegDouble(dec_insn.vB));
1574 break;
1575 case Instruction::REM_DOUBLE_2ADDR:
1576 shadow_frame.SetVRegDouble(dec_insn.vA,
1577 fmod(shadow_frame.GetVRegDouble(dec_insn.vA),
1578 shadow_frame.GetVRegDouble(dec_insn.vB)));
1579 break;
1580 case Instruction::ADD_INT_LIT16:
1581 case Instruction::ADD_INT_LIT8:
1582 shadow_frame.SetVReg(dec_insn.vA, shadow_frame.GetVReg(dec_insn.vB) + dec_insn.vC);
1583 break;
1584 case Instruction::RSUB_INT:
1585 case Instruction::RSUB_INT_LIT8:
1586 shadow_frame.SetVReg(dec_insn.vA, dec_insn.vC - shadow_frame.GetVReg(dec_insn.vB));
1587 break;
1588 case Instruction::MUL_INT_LIT16:
1589 case Instruction::MUL_INT_LIT8:
1590 shadow_frame.SetVReg(dec_insn.vA, shadow_frame.GetVReg(dec_insn.vB) * dec_insn.vC);
1591 break;
1592 case Instruction::DIV_INT_LIT16:
1593 case Instruction::DIV_INT_LIT8:
1594 shadow_frame.SetVReg(dec_insn.vA, shadow_frame.GetVReg(dec_insn.vB) / dec_insn.vC);
1595 break;
1596 case Instruction::REM_INT_LIT16:
1597 case Instruction::REM_INT_LIT8:
1598 shadow_frame.SetVReg(dec_insn.vA, shadow_frame.GetVReg(dec_insn.vB) % dec_insn.vC);
1599 break;
1600 case Instruction::AND_INT_LIT16:
1601 case Instruction::AND_INT_LIT8:
1602 shadow_frame.SetVReg(dec_insn.vA, shadow_frame.GetVReg(dec_insn.vB) & dec_insn.vC);
1603 break;
1604 case Instruction::OR_INT_LIT16:
1605 case Instruction::OR_INT_LIT8:
1606 shadow_frame.SetVReg(dec_insn.vA, shadow_frame.GetVReg(dec_insn.vB) | dec_insn.vC);
1607 break;
1608 case Instruction::XOR_INT_LIT16:
1609 case Instruction::XOR_INT_LIT8:
1610 shadow_frame.SetVReg(dec_insn.vA, shadow_frame.GetVReg(dec_insn.vB) ^ dec_insn.vC);
1611 break;
1612 case Instruction::SHL_INT_LIT8:
1613 shadow_frame.SetVReg(dec_insn.vA, shadow_frame.GetVReg(dec_insn.vB) << dec_insn.vC);
1614 break;
1615 case Instruction::SHR_INT_LIT8:
1616 shadow_frame.SetVReg(dec_insn.vA, shadow_frame.GetVReg(dec_insn.vB) >> dec_insn.vC);
1617 break;
1618 case Instruction::USHR_INT_LIT8:
1619 shadow_frame.SetVReg(dec_insn.vA,
1620 static_cast<uint32_t>(shadow_frame.GetVReg(dec_insn.vB)) >>
1621 dec_insn.vC);
1622 break;
1623 default:
1624 LOG(FATAL) << "Unexpected instruction: " << inst->DumpString(&mh.GetDexFile());
1625 break;
1626 }
1627 if (UNLIKELY(self->IsExceptionPending())) {
1628 uint32_t found_dex_pc =
1629 shadow_frame.GetMethod()->FindCatchBlock(self->GetException()->GetClass(),
1630 inst->GetDexPc(insns));
1631 if (found_dex_pc == DexFile::kDexNoIndex) {
1632 JValue result;
1633 result.SetJ(0);
1634 return result; // Handler in caller.
1635 } else {
1636 next_inst = Instruction::At(insns + found_dex_pc);
1637 }
1638 }
1639 inst = next_inst;
1640 }
1641}
1642
1643void EnterInterpreterFromInvoke(Thread* self, AbstractMethod* method, Object* receiver,
1644 JValue* args, JValue* result) {
Ian Rogers64b6d142012-10-29 16:34:15 -07001645 DCHECK_EQ(self, Thread::Current());
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001646 MethodHelper mh(method);
1647 const DexFile::CodeItem* code_item = mh.GetCodeItem();
1648 uint16_t num_regs;
1649 uint16_t num_ins;
1650 if (code_item != NULL) {
1651 num_regs = code_item->registers_size_;
1652 num_ins = code_item->ins_size_;
1653 } else {
1654 DCHECK(method->IsNative());
1655 num_regs = num_ins = AbstractMethod::NumArgRegisters(mh.GetShorty());
1656 if (!method->IsStatic()) {
1657 num_regs++;
1658 num_ins++;
1659 }
1660 }
1661 // Set up shadow frame with matching number of reference slots to vregs.
1662 ShadowFrame* last_shadow_frame = self->GetManagedStack()->GetTopShadowFrame();
1663 UniquePtr<ShadowFrame> shadow_frame(ShadowFrame::Create(num_regs, num_regs,
1664 (last_shadow_frame == NULL) ? NULL : last_shadow_frame->GetLink(),
1665 method, 0));
1666 self->PushShadowFrame(shadow_frame.get());
1667 size_t cur_reg = num_regs - num_ins;
1668 if (!method->IsStatic()) {
1669 CHECK(receiver != NULL);
1670 shadow_frame->SetReferenceAndVReg(cur_reg, receiver);
1671 ++cur_reg;
1672 } else if (!method->GetDeclaringClass()->IsInitializing()) {
1673 Runtime::Current()->GetClassLinker()->EnsureInitialized(method->GetDeclaringClass(),
1674 true, true);
1675 CHECK(method->GetDeclaringClass()->IsInitializing());
1676 }
Ian Rogers64b6d142012-10-29 16:34:15 -07001677 const char* shorty = mh.GetShorty();
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001678 size_t arg_pos = 0;
1679 for (; cur_reg < num_regs; ++cur_reg, ++arg_pos) {
1680 DCHECK_LT(arg_pos + 1, mh.GetShortyLength());
1681 switch (shorty[arg_pos + 1]) {
1682 case 'L': {
1683 Object* o = args[arg_pos].GetL();
1684 shadow_frame->SetReferenceAndVReg(cur_reg, o);
1685 break;
1686 }
1687 case 'J': case 'D':
1688 shadow_frame->SetVRegLong(cur_reg, args[arg_pos].GetJ());
1689 cur_reg++;
1690 break;
1691 default:
1692 shadow_frame->SetVReg(cur_reg, args[arg_pos].GetI());
1693 break;
1694 }
1695 }
Ian Rogers64b6d142012-10-29 16:34:15 -07001696 if (LIKELY(!method->IsNative())) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001697 JValue r = Execute(self, mh, code_item, *shadow_frame.get());
1698 if (result != NULL) {
1699 *result = r;
1700 }
1701 } else {
Ian Rogers64b6d142012-10-29 16:34:15 -07001702 // We don't expect to be asked to interpret native code (which is entered via a JNI compiler
1703 // generated stub) except during testing and image writing.
1704 if (!Runtime::Current()->IsStarted()) {
1705 UnstartedRuntimeJni(self, method, receiver, args, result);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001706 } else {
Ian Rogers64b6d142012-10-29 16:34:15 -07001707 InterpreterJni(self, method, shorty, receiver, args, result);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001708 }
1709 }
1710 self->PopShadowFrame();
1711}
1712
1713} // namespace interpreter
1714} // namespace art