blob: 5c8ccdf063d1ca377bdc10c3ddf225adf9674e2c [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);
Ian Rogers689d9f02012-11-20 16:30:29 -0800425 return;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700426 }
427 }
428 switch (field_type) {
429 case Primitive::kPrimBoolean:
430 shadow_frame.SetVReg(dec_insn.vA, f->GetBoolean(obj));
431 break;
432 case Primitive::kPrimByte:
433 shadow_frame.SetVReg(dec_insn.vA, f->GetByte(obj));
434 break;
435 case Primitive::kPrimChar:
436 shadow_frame.SetVReg(dec_insn.vA, f->GetChar(obj));
437 break;
438 case Primitive::kPrimShort:
439 shadow_frame.SetVReg(dec_insn.vA, f->GetShort(obj));
440 break;
441 case Primitive::kPrimInt:
442 shadow_frame.SetVReg(dec_insn.vA, f->GetInt(obj));
443 break;
444 case Primitive::kPrimLong:
445 shadow_frame.SetVRegLong(dec_insn.vA, f->GetLong(obj));
446 break;
447 case Primitive::kPrimNot:
448 shadow_frame.SetReferenceAndVReg(dec_insn.vA, f->GetObject(obj));
449 break;
450 default:
451 LOG(FATAL) << "Unreachable: " << field_type;
452 }
453 }
454}
455
456static void DoFieldPut(Thread* self, ShadowFrame& shadow_frame,
457 const DecodedInstruction& dec_insn, FindFieldType find_type,
458 Primitive::Type field_type)
459 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
460 bool is_static = (find_type == StaticObjectWrite) || (find_type == StaticPrimitiveWrite);
461 uint32_t field_idx = is_static ? dec_insn.vB : dec_insn.vC;
462 Field* f = FindFieldFromCode(field_idx, shadow_frame.GetMethod(), self,
463 find_type, Primitive::FieldSize(field_type));
464 if (LIKELY(f != NULL)) {
465 Object* obj;
466 if (is_static) {
467 obj = f->GetDeclaringClass();
468 } else {
469 obj = shadow_frame.GetReference(dec_insn.vB);
470 if (UNLIKELY(obj == NULL)) {
471 ThrowNullPointerExceptionForFieldAccess(f, false);
Ian Rogers689d9f02012-11-20 16:30:29 -0800472 return;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700473 }
474 }
475 switch (field_type) {
476 case Primitive::kPrimBoolean:
477 f->SetBoolean(obj, shadow_frame.GetVReg(dec_insn.vA));
478 break;
479 case Primitive::kPrimByte:
480 f->SetByte(obj, shadow_frame.GetVReg(dec_insn.vA));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700481 break;
482 case Primitive::kPrimChar:
483 f->SetChar(obj, shadow_frame.GetVReg(dec_insn.vA));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700484 break;
485 case Primitive::kPrimShort:
486 f->SetShort(obj, shadow_frame.GetVReg(dec_insn.vA));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700487 break;
488 case Primitive::kPrimInt:
489 f->SetInt(obj, shadow_frame.GetVReg(dec_insn.vA));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700490 break;
491 case Primitive::kPrimLong:
492 f->SetLong(obj, shadow_frame.GetVRegLong(dec_insn.vA));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700493 break;
494 case Primitive::kPrimNot:
495 f->SetObj(obj, shadow_frame.GetReference(dec_insn.vA));
496 break;
497 default:
498 LOG(FATAL) << "Unreachable: " << field_type;
499 }
500 }
501}
502
503static JValue Execute(Thread* self, MethodHelper& mh, const DexFile::CodeItem* code_item,
504 ShadowFrame& shadow_frame) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
505 const uint16_t* insns = code_item->insns_;
506 const Instruction* inst = Instruction::At(insns + shadow_frame.GetDexPC());
507 JValue result_register;
508 while (true) {
jeffhao373c52f2012-11-20 16:11:52 -0800509 CheckSuspend(self);
510 uint32_t dex_pc = inst->GetDexPc(insns);
511 shadow_frame.SetDexPC(dex_pc);
512 Dbg::UpdateDebugger(dex_pc, self);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700513 DecodedInstruction dec_insn(inst);
Ian Rogers64b6d142012-10-29 16:34:15 -0700514 const bool kTracing = false;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700515 if (kTracing) {
516 LOG(INFO) << PrettyMethod(shadow_frame.GetMethod())
517 << StringPrintf("\n0x%x: %s\nReferences:",
518 inst->GetDexPc(insns), inst->DumpString(&mh.GetDexFile()).c_str());
519 for (size_t i = 0; i < shadow_frame.NumberOfReferences(); ++i) {
520 Object* o = shadow_frame.GetReference(i);
521 if (o != NULL) {
522 if (o->GetClass()->IsStringClass() && o->AsString()->GetCharArray() != NULL) {
523 LOG(INFO) << i << ": java.lang.String " << static_cast<void*>(o)
524 << " \"" << o->AsString()->ToModifiedUtf8() << "\"";
525 } else {
526 LOG(INFO) << i << ": " << PrettyTypeOf(o) << " " << static_cast<void*>(o);
527 }
528 } else {
529 LOG(INFO) << i << ": null";
530 }
531 }
532 LOG(INFO) << "vregs:";
533 for (size_t i = 0; i < shadow_frame.NumberOfReferences(); ++i) {
534 LOG(INFO) << StringPrintf("%d: %08x", i, shadow_frame.GetVReg(i));
535 }
536 }
537 const Instruction* next_inst = inst->Next();
538 switch (dec_insn.opcode) {
539 case Instruction::NOP:
540 break;
541 case Instruction::MOVE:
542 case Instruction::MOVE_FROM16:
543 case Instruction::MOVE_16:
544 shadow_frame.SetVReg(dec_insn.vA, shadow_frame.GetVReg(dec_insn.vB));
545 break;
546 case Instruction::MOVE_WIDE:
547 case Instruction::MOVE_WIDE_FROM16:
548 case Instruction::MOVE_WIDE_16:
549 shadow_frame.SetVRegLong(dec_insn.vA, shadow_frame.GetVRegLong(dec_insn.vB));
550 break;
551 case Instruction::MOVE_OBJECT:
552 case Instruction::MOVE_OBJECT_FROM16:
553 case Instruction::MOVE_OBJECT_16:
554 shadow_frame.SetVReg(dec_insn.vA, shadow_frame.GetVReg(dec_insn.vB));
555 shadow_frame.SetReference(dec_insn.vA, shadow_frame.GetReference(dec_insn.vB));
556 break;
557 case Instruction::MOVE_RESULT:
558 shadow_frame.SetVReg(dec_insn.vA, result_register.GetI());
559 break;
560 case Instruction::MOVE_RESULT_WIDE:
561 shadow_frame.SetVRegLong(dec_insn.vA, result_register.GetJ());
562 break;
563 case Instruction::MOVE_RESULT_OBJECT:
564 shadow_frame.SetReferenceAndVReg(dec_insn.vA, result_register.GetL());
565 break;
566 case Instruction::MOVE_EXCEPTION: {
567 Throwable* exception = self->GetException();
568 self->ClearException();
569 shadow_frame.SetReferenceAndVReg(dec_insn.vA, exception);
570 break;
571 }
572 case Instruction::RETURN_VOID: {
573 JValue result;
574 result.SetJ(0);
575 return result;
576 }
577 case Instruction::RETURN: {
578 JValue result;
579 result.SetJ(0);
580 result.SetI(shadow_frame.GetVReg(dec_insn.vA));
581 return result;
582 }
583 case Instruction::RETURN_WIDE: {
584 JValue result;
585 result.SetJ(shadow_frame.GetVRegLong(dec_insn.vA));
586 return result;
587 }
588 case Instruction::RETURN_OBJECT: {
589 JValue result;
590 result.SetJ(0);
591 result.SetL(shadow_frame.GetReference(dec_insn.vA));
592 return result;
593 }
594 case Instruction::CONST_4: {
Ian Rogers64b6d142012-10-29 16:34:15 -0700595 int32_t val = static_cast<int32_t>(dec_insn.vB << 28) >> 28;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700596 shadow_frame.SetVReg(dec_insn.vA, val);
597 if (val == 0) {
598 shadow_frame.SetReference(dec_insn.vA, NULL);
599 }
600 break;
601 }
602 case Instruction::CONST_16: {
603 int32_t val = static_cast<int16_t>(dec_insn.vB);
604 shadow_frame.SetVReg(dec_insn.vA, val);
605 if (val == 0) {
606 shadow_frame.SetReference(dec_insn.vA, NULL);
607 }
608 break;
609 }
610 case Instruction::CONST: {
611 int32_t val = dec_insn.vB;
612 shadow_frame.SetVReg(dec_insn.vA, val);
613 if (val == 0) {
614 shadow_frame.SetReference(dec_insn.vA, NULL);
615 }
616 break;
617 }
618 case Instruction::CONST_HIGH16: {
619 int32_t val = dec_insn.vB << 16;
620 shadow_frame.SetVReg(dec_insn.vA, val);
621 if (val == 0) {
622 shadow_frame.SetReference(dec_insn.vA, NULL);
623 }
624 break;
625 }
Ian Rogers64b6d142012-10-29 16:34:15 -0700626 case Instruction::CONST_WIDE_16:
627 shadow_frame.SetVRegLong(dec_insn.vA, static_cast<int16_t>(dec_insn.vB));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700628 break;
Ian Rogers64b6d142012-10-29 16:34:15 -0700629 case Instruction::CONST_WIDE_32:
630 shadow_frame.SetVRegLong(dec_insn.vA, static_cast<int32_t>(dec_insn.vB));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700631 break;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700632 case Instruction::CONST_WIDE:
Ian Rogers64b6d142012-10-29 16:34:15 -0700633 shadow_frame.SetVRegLong(dec_insn.vA, dec_insn.vB_wide);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700634 break;
635 case Instruction::CONST_WIDE_HIGH16:
Ian Rogers64b6d142012-10-29 16:34:15 -0700636 shadow_frame.SetVRegLong(dec_insn.vA, static_cast<uint64_t>(dec_insn.vB) << 48);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700637 break;
638 case Instruction::CONST_STRING:
639 case Instruction::CONST_STRING_JUMBO: {
640 if (UNLIKELY(!String::GetJavaLangString()->IsInitialized())) {
641 Runtime::Current()->GetClassLinker()->EnsureInitialized(String::GetJavaLangString(),
642 true, true);
643 }
644 String* s = mh.ResolveString(dec_insn.vB);
645 shadow_frame.SetReferenceAndVReg(dec_insn.vA, s);
646 break;
647 }
648 case Instruction::CONST_CLASS:
Ian Rogers17ffcab2012-11-20 15:27:41 -0800649 shadow_frame.SetReferenceAndVReg(dec_insn.vA, mh.ResolveClass(dec_insn.vB));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700650 break;
651 case Instruction::MONITOR_ENTER:
652 DoMonitorEnter(self, shadow_frame.GetReference(dec_insn.vA));
653 break;
654 case Instruction::MONITOR_EXIT:
655 DoMonitorExit(self, shadow_frame.GetReference(dec_insn.vA));
656 break;
657 case Instruction::CHECK_CAST: {
658 Class* c = mh.ResolveClass(dec_insn.vB);
659 Object* obj = shadow_frame.GetReference(dec_insn.vA);
660 if (UNLIKELY(obj != NULL && !obj->InstanceOf(c))) {
661 self->ThrowNewExceptionF("Ljava/lang/ClassCastException;",
662 "%s cannot be cast to %s",
663 PrettyDescriptor(obj->GetClass()).c_str(),
664 PrettyDescriptor(c).c_str());
665 }
666 break;
667 }
668 case Instruction::INSTANCE_OF: {
669 Class* c = mh.ResolveClass(dec_insn.vC);
670 Object* obj = shadow_frame.GetReference(dec_insn.vB);
671 shadow_frame.SetVReg(dec_insn.vA, (obj != NULL && obj->InstanceOf(c)) ? 1 : 0);
672 break;
673 }
674 case Instruction::ARRAY_LENGTH: {
Ian Rogers64b6d142012-10-29 16:34:15 -0700675 Object* array = shadow_frame.GetReference(dec_insn.vB);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700676 if (UNLIKELY(array == NULL)) {
677 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetMethod(), inst->GetDexPc(insns));
678 break;
679 }
Ian Rogers64b6d142012-10-29 16:34:15 -0700680 shadow_frame.SetVReg(dec_insn.vA, array->AsArray()->GetLength());
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700681 break;
682 }
683 case Instruction::NEW_INSTANCE: {
684 Object* obj = AllocObjectFromCode(dec_insn.vB, shadow_frame.GetMethod(), self, true);
685 shadow_frame.SetReferenceAndVReg(dec_insn.vA, obj);
686 break;
687 }
688 case Instruction::NEW_ARRAY: {
689 int32_t length = shadow_frame.GetVReg(dec_insn.vB);
690 Object* obj = AllocArrayFromCode(dec_insn.vC, shadow_frame.GetMethod(), length, self, true);
691 shadow_frame.SetReferenceAndVReg(dec_insn.vA, obj);
692 break;
693 }
694 case Instruction::FILLED_NEW_ARRAY:
Ian Rogers64b6d142012-10-29 16:34:15 -0700695 case Instruction::FILLED_NEW_ARRAY_RANGE: {
696 bool is_range = (dec_insn.opcode == Instruction::FILLED_NEW_ARRAY_RANGE);
697 int32_t length = dec_insn.vA;
698 CHECK(is_range || length <= 5);
699 Class* arrayClass = mh.ResolveClass(dec_insn.vB);
700 CHECK(arrayClass->IsArrayClass());
701 if (arrayClass->GetComponentType()->IsPrimitiveInt()) {
702 IntArray* newArray = IntArray::Alloc(self, length);
703 if (newArray != NULL) {
704 for (int32_t i = 0; i < length; ++i) {
705 if (is_range) {
706 newArray->Set(i, shadow_frame.GetVReg(dec_insn.vC + i));
707 } else {
708 newArray->Set(i, shadow_frame.GetVReg(dec_insn.arg[i]));
709 }
710 }
711 }
712 result_register.SetL(newArray);
713 } else {
714 UNIMPLEMENTED(FATAL) << inst->DumpString(&mh.GetDexFile())
715 << " for array type: " << PrettyDescriptor(arrayClass);
716 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700717 break;
Ian Rogers64b6d142012-10-29 16:34:15 -0700718 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700719 case Instruction::CMPL_FLOAT: {
720 float val1 = shadow_frame.GetVRegFloat(dec_insn.vB);
721 float val2 = shadow_frame.GetVRegFloat(dec_insn.vC);
722 int32_t result;
723 if (val1 == val2) {
724 result = 0;
725 } else if (val1 > val2) {
726 result = 1;
727 } else {
728 result = -1;
729 }
730 shadow_frame.SetVReg(dec_insn.vA, result);
731 break;
732 }
733 case Instruction::CMPG_FLOAT: {
734 float val1 = shadow_frame.GetVRegFloat(dec_insn.vB);
735 float val2 = shadow_frame.GetVRegFloat(dec_insn.vC);
736 int32_t result;
737 if (val1 == val2) {
738 result = 0;
739 } else if (val1 < val2) {
740 result = -1;
741 } else {
742 result = 1;
743 }
744 shadow_frame.SetVReg(dec_insn.vA, result);
745 break;
746 }
747 case Instruction::CMPL_DOUBLE: {
748 double val1 = shadow_frame.GetVRegDouble(dec_insn.vB);
749 double val2 = shadow_frame.GetVRegDouble(dec_insn.vC);
750 int32_t result;
751 if (val1 == val2) {
752 result = 0;
Ian Rogers58bf0c62012-11-20 16:24:12 -0800753 } else if (val1 > val2) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700754 result = 1;
755 } else {
756 result = -1;
757 }
758 shadow_frame.SetVReg(dec_insn.vA, result);
759 break;
760 }
761
762 case Instruction::CMPG_DOUBLE: {
763 double val1 = shadow_frame.GetVRegDouble(dec_insn.vB);
764 double val2 = shadow_frame.GetVRegDouble(dec_insn.vC);
765 int32_t result;
766 if (val1 == val2) {
767 result = 0;
Ian Rogers58bf0c62012-11-20 16:24:12 -0800768 } else if (val1 < val2) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700769 result = -1;
770 } else {
771 result = 1;
772 }
773 shadow_frame.SetVReg(dec_insn.vA, result);
774 break;
775 }
776 case Instruction::CMP_LONG: {
777 int64_t val1 = shadow_frame.GetVRegLong(dec_insn.vB);
778 int64_t val2 = shadow_frame.GetVRegLong(dec_insn.vC);
779 int32_t result;
780 if (val1 < val2) {
Ian Rogers64b6d142012-10-29 16:34:15 -0700781 result = 1;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700782 } else if (val1 == val2) {
783 result = 0;
784 } else {
Ian Rogers64b6d142012-10-29 16:34:15 -0700785 result = -1;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700786 }
787 shadow_frame.SetVReg(dec_insn.vA, result);
788 break;
789 }
790 case Instruction::THROW: {
791 Throwable* t = shadow_frame.GetReference(dec_insn.vA)->AsThrowable();
792 self->SetException(t);
793 break;
794 }
795 case Instruction::GOTO:
796 case Instruction::GOTO_16:
797 case Instruction::GOTO_32: {
798 uint32_t dex_pc = inst->GetDexPc(insns);
799 next_inst = Instruction::At(insns + dex_pc + dec_insn.vA);
800 break;
801 }
Ian Rogers556d6372012-11-20 12:19:36 -0800802 case Instruction::PACKED_SWITCH: {
803 uint32_t dex_pc = inst->GetDexPc(insns);
804 const uint16_t* switch_data = insns + dex_pc + dec_insn.vB;
805 int32_t test_val = shadow_frame.GetVReg(dec_insn.vA);
806 CHECK_EQ(switch_data[0], static_cast<uint16_t>(Instruction::kPackedSwitchSignature));
807 uint16_t size = switch_data[1];
808 CHECK_GT(size, 0);
809 const int32_t* keys = reinterpret_cast<const int32_t*>(&switch_data[2]);
810 CHECK(IsAligned<4>(keys));
811 int32_t first_key = keys[0];
812 const int32_t* targets = reinterpret_cast<const int32_t*>(&switch_data[4]);
813 CHECK(IsAligned<4>(targets));
814 int32_t index = test_val - first_key;
815 if (index >= 0 && index < size) {
816 next_inst = Instruction::At(insns + dex_pc + targets[index]);
817 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700818 break;
Ian Rogers556d6372012-11-20 12:19:36 -0800819 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700820 case Instruction::SPARSE_SWITCH: {
821 uint32_t dex_pc = inst->GetDexPc(insns);
Ian Rogers556d6372012-11-20 12:19:36 -0800822 const uint16_t* switch_data = insns + dex_pc + dec_insn.vB;
823 int32_t test_val = shadow_frame.GetVReg(dec_insn.vA);
824 CHECK_EQ(switch_data[0], static_cast<uint16_t>(Instruction::kSparseSwitchSignature));
825 uint16_t size = switch_data[1];
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700826 CHECK_GT(size, 0);
Ian Rogers556d6372012-11-20 12:19:36 -0800827 const int32_t* keys = reinterpret_cast<const int32_t*>(&switch_data[2]);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700828 CHECK(IsAligned<4>(keys));
829 const int32_t* entries = keys + size;
830 CHECK(IsAligned<4>(entries));
831 int lo = 0;
832 int hi = size - 1;
833 while (lo <= hi) {
834 int mid = (lo + hi) / 2;
835 int32_t foundVal = keys[mid];
Ian Rogers556d6372012-11-20 12:19:36 -0800836 if (test_val < foundVal) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700837 hi = mid - 1;
Ian Rogers556d6372012-11-20 12:19:36 -0800838 } else if (test_val > foundVal) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700839 lo = mid + 1;
840 } else {
841 next_inst = Instruction::At(insns + dex_pc + entries[mid]);
842 break;
843 }
844 }
845 break;
846 }
847 case Instruction::FILL_ARRAY_DATA: {
848 Array* array = shadow_frame.GetReference(dec_insn.vA)->AsArray();
849 if (UNLIKELY(array == NULL)) {
850 Thread::Current()->ThrowNewExceptionF("Ljava/lang/NullPointerException;",
851 "null array in FILL_ARRAY_DATA");
852 break;
853 }
854 DCHECK(array->IsArrayInstance() && !array->IsObjectArray());
855 uint32_t dex_pc = inst->GetDexPc(insns);
856 const Instruction::ArrayDataPayload* payload =
857 reinterpret_cast<const Instruction::ArrayDataPayload*>(insns + dex_pc + dec_insn.vB);
858 if (UNLIKELY(static_cast<int32_t>(payload->element_count) > array->GetLength())) {
859 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
860 "failed FILL_ARRAY_DATA; length=%d, index=%d",
861 array->GetLength(), payload->element_count);
862 break;
863 }
864 uint32_t size_in_bytes = payload->element_count * payload->element_width;
865 memcpy(array->GetRawData(payload->element_width), payload->data, size_in_bytes);
866 break;
867 }
868 case Instruction::IF_EQ: {
869 if (shadow_frame.GetVReg(dec_insn.vA) == shadow_frame.GetVReg(dec_insn.vB)) {
870 uint32_t dex_pc = inst->GetDexPc(insns);
871 next_inst = Instruction::At(insns + dex_pc + dec_insn.vC);
872 }
873 break;
874 }
875 case Instruction::IF_NE: {
876 if (shadow_frame.GetVReg(dec_insn.vA) != shadow_frame.GetVReg(dec_insn.vB)) {
877 uint32_t dex_pc = inst->GetDexPc(insns);
878 next_inst = Instruction::At(insns + dex_pc + dec_insn.vC);
879 }
880 break;
881 }
882 case Instruction::IF_LT: {
883 if (shadow_frame.GetVReg(dec_insn.vA) < shadow_frame.GetVReg(dec_insn.vB)) {
884 uint32_t dex_pc = inst->GetDexPc(insns);
885 next_inst = Instruction::At(insns + dex_pc + dec_insn.vC);
886 }
887 break;
888 }
889 case Instruction::IF_GE: {
890 if (shadow_frame.GetVReg(dec_insn.vA) >= shadow_frame.GetVReg(dec_insn.vB)) {
891 uint32_t dex_pc = inst->GetDexPc(insns);
892 next_inst = Instruction::At(insns + dex_pc + dec_insn.vC);
893 }
894 break;
895 }
896 case Instruction::IF_GT: {
897 if (shadow_frame.GetVReg(dec_insn.vA) > shadow_frame.GetVReg(dec_insn.vB)) {
898 uint32_t dex_pc = inst->GetDexPc(insns);
899 next_inst = Instruction::At(insns + dex_pc + dec_insn.vC);
900 }
901 break;
902 }
903 case Instruction::IF_LE: {
904 if (shadow_frame.GetVReg(dec_insn.vA) <= shadow_frame.GetVReg(dec_insn.vB)) {
905 uint32_t dex_pc = inst->GetDexPc(insns);
906 next_inst = Instruction::At(insns + dex_pc + dec_insn.vC);
907 }
908 break;
909 }
910 case Instruction::IF_EQZ: {
911 if (shadow_frame.GetVReg(dec_insn.vA) == 0) {
912 uint32_t dex_pc = inst->GetDexPc(insns);
913 next_inst = Instruction::At(insns + dex_pc + dec_insn.vB);
914 }
915 break;
916 }
917 case Instruction::IF_NEZ: {
918 if (shadow_frame.GetVReg(dec_insn.vA) != 0) {
919 uint32_t dex_pc = inst->GetDexPc(insns);
920 next_inst = Instruction::At(insns + dex_pc + dec_insn.vB);
921 }
922 break;
923 }
924 case Instruction::IF_LTZ: {
925 if (shadow_frame.GetVReg(dec_insn.vA) < 0) {
926 uint32_t dex_pc = inst->GetDexPc(insns);
927 next_inst = Instruction::At(insns + dex_pc + dec_insn.vB);
928 }
929 break;
930 }
931 case Instruction::IF_GEZ: {
932 if (shadow_frame.GetVReg(dec_insn.vA) >= 0) {
933 uint32_t dex_pc = inst->GetDexPc(insns);
934 next_inst = Instruction::At(insns + dex_pc + dec_insn.vB);
935 }
936 break;
937 }
938 case Instruction::IF_GTZ: {
939 if (shadow_frame.GetVReg(dec_insn.vA) > 0) {
940 uint32_t dex_pc = inst->GetDexPc(insns);
941 next_inst = Instruction::At(insns + dex_pc + dec_insn.vB);
942 }
943 break;
944 }
945 case Instruction::IF_LEZ: {
946 if (shadow_frame.GetVReg(dec_insn.vA) <= 0) {
947 uint32_t dex_pc = inst->GetDexPc(insns);
948 next_inst = Instruction::At(insns + dex_pc + dec_insn.vB);
949 }
950 break;
951 }
952 case Instruction::AGET_BOOLEAN: {
953 BooleanArray* a = shadow_frame.GetReference(dec_insn.vB)->AsBooleanArray();
954 if (UNLIKELY(a == NULL)) {
955 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetMethod(), inst->GetDexPc(insns));
956 break;
957 }
958 int32_t index = shadow_frame.GetVReg(dec_insn.vC);
959 shadow_frame.SetVReg(dec_insn.vA, a->Get(index));
960 break;
961 }
962 case Instruction::AGET_BYTE: {
963 ByteArray* a = shadow_frame.GetReference(dec_insn.vB)->AsByteArray();
964 if (UNLIKELY(a == NULL)) {
965 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetMethod(), inst->GetDexPc(insns));
966 break;
967 }
968 int32_t index = shadow_frame.GetVReg(dec_insn.vC);
969 shadow_frame.SetVReg(dec_insn.vA, a->Get(index));
970 break;
971 }
972 case Instruction::AGET_CHAR: {
973 CharArray* a = shadow_frame.GetReference(dec_insn.vB)->AsCharArray();
974 if (UNLIKELY(a == NULL)) {
975 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetMethod(), inst->GetDexPc(insns));
976 break;
977 }
978 int32_t index = shadow_frame.GetVReg(dec_insn.vC);
979 shadow_frame.SetVReg(dec_insn.vA, a->Get(index));
980 break;
981 }
982 case Instruction::AGET_SHORT: {
983 ShortArray* a = shadow_frame.GetReference(dec_insn.vB)->AsShortArray();
984 if (UNLIKELY(a == NULL)) {
985 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetMethod(), inst->GetDexPc(insns));
986 break;
987 }
988 int32_t index = shadow_frame.GetVReg(dec_insn.vC);
989 shadow_frame.SetVReg(dec_insn.vA, a->Get(index));
990 break;
991 }
992 case Instruction::AGET: {
993 IntArray* a = shadow_frame.GetReference(dec_insn.vB)->AsIntArray();
994 if (UNLIKELY(a == NULL)) {
995 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetMethod(), inst->GetDexPc(insns));
996 break;
997 }
998 int32_t index = shadow_frame.GetVReg(dec_insn.vC);
999 shadow_frame.SetVReg(dec_insn.vA, a->Get(index));
1000 break;
1001 }
1002 case Instruction::AGET_WIDE: {
1003 LongArray* a = shadow_frame.GetReference(dec_insn.vB)->AsLongArray();
1004 if (UNLIKELY(a == NULL)) {
1005 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetMethod(), inst->GetDexPc(insns));
1006 break;
1007 }
1008 int32_t index = shadow_frame.GetVReg(dec_insn.vC);
1009 shadow_frame.SetVRegLong(dec_insn.vA, a->Get(index));
1010 break;
1011 }
1012 case Instruction::AGET_OBJECT: {
1013 ObjectArray<Object>* a = shadow_frame.GetReference(dec_insn.vB)->AsObjectArray<Object>();
1014 if (UNLIKELY(a == NULL)) {
1015 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetMethod(), inst->GetDexPc(insns));
1016 break;
1017 }
1018 int32_t index = shadow_frame.GetVReg(dec_insn.vC);
1019 Object* o = a->Get(index);
1020 shadow_frame.SetReferenceAndVReg(dec_insn.vA, o);
1021 break;
1022 }
1023 case Instruction::APUT_BOOLEAN: {
1024 uint8_t val = shadow_frame.GetVReg(dec_insn.vA);
1025 BooleanArray* a = shadow_frame.GetReference(dec_insn.vB)->AsBooleanArray();
1026 if (UNLIKELY(a == NULL)) {
1027 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetMethod(), inst->GetDexPc(insns));
1028 break;
1029 }
1030 int32_t index = shadow_frame.GetVReg(dec_insn.vC);
1031 a->Set(index, val);
1032 break;
1033 }
1034 case Instruction::APUT_BYTE: {
1035 int8_t val = shadow_frame.GetVReg(dec_insn.vA);
1036 ByteArray* a = shadow_frame.GetReference(dec_insn.vB)->AsByteArray();
1037 if (UNLIKELY(a == NULL)) {
1038 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetMethod(), inst->GetDexPc(insns));
1039 break;
1040 }
1041 int32_t index = shadow_frame.GetVReg(dec_insn.vC);
1042 a->Set(index, val);
1043 break;
1044 }
1045 case Instruction::APUT_CHAR: {
1046 uint16_t val = shadow_frame.GetVReg(dec_insn.vA);
1047 CharArray* a = shadow_frame.GetReference(dec_insn.vB)->AsCharArray();
1048 if (UNLIKELY(a == NULL)) {
1049 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetMethod(), inst->GetDexPc(insns));
1050 break;
1051 }
1052 int32_t index = shadow_frame.GetVReg(dec_insn.vC);
1053 a->Set(index, val);
1054 break;
1055 }
1056 case Instruction::APUT_SHORT: {
1057 int16_t val = shadow_frame.GetVReg(dec_insn.vA);
1058 ShortArray* a = shadow_frame.GetReference(dec_insn.vB)->AsShortArray();
1059 if (UNLIKELY(a == NULL)) {
1060 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetMethod(), inst->GetDexPc(insns));
1061 break;
1062 }
1063 int32_t index = shadow_frame.GetVReg(dec_insn.vC);
1064 a->Set(index, val);
1065 break;
1066 }
1067 case Instruction::APUT: {
1068 int32_t val = shadow_frame.GetVReg(dec_insn.vA);
1069 IntArray* a = shadow_frame.GetReference(dec_insn.vB)->AsIntArray();
1070 if (UNLIKELY(a == NULL)) {
1071 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetMethod(), inst->GetDexPc(insns));
1072 break;
1073 }
1074 int32_t index = shadow_frame.GetVReg(dec_insn.vC);
1075 a->Set(index, val);
1076 break;
1077 }
1078 case Instruction::APUT_WIDE: {
1079 int64_t val = shadow_frame.GetVRegLong(dec_insn.vA);
1080 LongArray* a = shadow_frame.GetReference(dec_insn.vB)->AsLongArray();
1081 if (UNLIKELY(a == NULL)) {
1082 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetMethod(), inst->GetDexPc(insns));
1083 break;
1084 }
1085 int32_t index = shadow_frame.GetVReg(dec_insn.vC);
1086 a->Set(index, val);
1087 break;
1088 }
1089 case Instruction::APUT_OBJECT: {
1090 Object* val = shadow_frame.GetReference(dec_insn.vA);
1091 ObjectArray<Object>* a = shadow_frame.GetReference(dec_insn.vB)->AsObjectArray<Object>();
1092 if (UNLIKELY(a == NULL)) {
1093 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetMethod(), inst->GetDexPc(insns));
1094 break;
1095 }
1096 int32_t index = shadow_frame.GetVReg(dec_insn.vC);
1097 a->Set(index, val);
1098 break;
1099 }
1100 case Instruction::IGET_BOOLEAN:
1101 DoFieldGet(self, shadow_frame, dec_insn, InstancePrimitiveRead, Primitive::kPrimBoolean);
1102 break;
1103 case Instruction::IGET_BYTE:
1104 DoFieldGet(self, shadow_frame, dec_insn, InstancePrimitiveRead, Primitive::kPrimByte);
1105 break;
1106 case Instruction::IGET_CHAR:
1107 DoFieldGet(self, shadow_frame, dec_insn, InstancePrimitiveRead, Primitive::kPrimChar);
1108 break;
1109 case Instruction::IGET_SHORT:
1110 DoFieldGet(self, shadow_frame, dec_insn, InstancePrimitiveRead, Primitive::kPrimShort);
1111 break;
1112 case Instruction::IGET:
1113 DoFieldGet(self, shadow_frame, dec_insn, InstancePrimitiveRead, Primitive::kPrimInt);
1114 break;
1115 case Instruction::IGET_WIDE:
1116 DoFieldGet(self, shadow_frame, dec_insn, InstancePrimitiveRead, Primitive::kPrimLong);
1117 break;
1118 case Instruction::IGET_OBJECT:
1119 DoFieldGet(self, shadow_frame, dec_insn, InstanceObjectRead, Primitive::kPrimNot);
1120 break;
1121 case Instruction::SGET_BOOLEAN:
1122 DoFieldGet(self, shadow_frame, dec_insn, StaticPrimitiveRead, Primitive::kPrimBoolean);
1123 break;
1124 case Instruction::SGET_BYTE:
1125 DoFieldGet(self, shadow_frame, dec_insn, StaticPrimitiveRead, Primitive::kPrimByte);
1126 break;
1127 case Instruction::SGET_CHAR:
1128 DoFieldGet(self, shadow_frame, dec_insn, StaticPrimitiveRead, Primitive::kPrimChar);
1129 break;
1130 case Instruction::SGET_SHORT:
1131 DoFieldGet(self, shadow_frame, dec_insn, StaticPrimitiveRead, Primitive::kPrimShort);
1132 break;
1133 case Instruction::SGET:
1134 DoFieldGet(self, shadow_frame, dec_insn, StaticPrimitiveRead, Primitive::kPrimInt);
1135 break;
1136 case Instruction::SGET_WIDE:
1137 DoFieldGet(self, shadow_frame, dec_insn, StaticPrimitiveRead, Primitive::kPrimLong);
1138 break;
1139 case Instruction::SGET_OBJECT:
1140 DoFieldGet(self, shadow_frame, dec_insn, StaticObjectRead, Primitive::kPrimNot);
1141 break;
1142 case Instruction::IPUT_BOOLEAN:
1143 DoFieldPut(self, shadow_frame, dec_insn, InstancePrimitiveWrite, Primitive::kPrimBoolean);
1144 break;
1145 case Instruction::IPUT_BYTE:
1146 DoFieldPut(self, shadow_frame, dec_insn, InstancePrimitiveWrite, Primitive::kPrimByte);
1147 break;
1148 case Instruction::IPUT_CHAR:
1149 DoFieldPut(self, shadow_frame, dec_insn, InstancePrimitiveWrite, Primitive::kPrimChar);
1150 break;
1151 case Instruction::IPUT_SHORT:
1152 DoFieldPut(self, shadow_frame, dec_insn, InstancePrimitiveWrite, Primitive::kPrimShort);
1153 break;
1154 case Instruction::IPUT:
1155 DoFieldPut(self, shadow_frame, dec_insn, InstancePrimitiveWrite, Primitive::kPrimInt);
1156 break;
1157 case Instruction::IPUT_WIDE:
1158 DoFieldPut(self, shadow_frame, dec_insn, InstancePrimitiveWrite, Primitive::kPrimLong);
1159 break;
1160 case Instruction::IPUT_OBJECT:
1161 DoFieldPut(self, shadow_frame, dec_insn, InstanceObjectWrite, Primitive::kPrimNot);
1162 break;
1163 case Instruction::SPUT_BOOLEAN:
1164 DoFieldPut(self, shadow_frame, dec_insn, StaticPrimitiveWrite, Primitive::kPrimBoolean);
1165 break;
1166 case Instruction::SPUT_BYTE:
1167 DoFieldPut(self, shadow_frame, dec_insn, StaticPrimitiveWrite, Primitive::kPrimByte);
1168 break;
1169 case Instruction::SPUT_CHAR:
1170 DoFieldPut(self, shadow_frame, dec_insn, StaticPrimitiveWrite, Primitive::kPrimChar);
1171 break;
1172 case Instruction::SPUT_SHORT:
1173 DoFieldPut(self, shadow_frame, dec_insn, StaticPrimitiveWrite, Primitive::kPrimShort);
1174 break;
1175 case Instruction::SPUT:
1176 DoFieldPut(self, shadow_frame, dec_insn, StaticPrimitiveWrite, Primitive::kPrimInt);
1177 break;
1178 case Instruction::SPUT_WIDE:
1179 DoFieldPut(self, shadow_frame, dec_insn, StaticPrimitiveWrite, Primitive::kPrimLong);
1180 break;
1181 case Instruction::SPUT_OBJECT:
1182 DoFieldPut(self, shadow_frame, dec_insn, StaticObjectWrite, Primitive::kPrimNot);
1183 break;
1184 case Instruction::INVOKE_VIRTUAL:
1185 DoInvoke(self, mh, shadow_frame, dec_insn, kVirtual, false, &result_register);
1186 break;
1187 case Instruction::INVOKE_VIRTUAL_RANGE:
1188 DoInvoke(self, mh, shadow_frame, dec_insn, kVirtual, true, &result_register);
1189 break;
1190 case Instruction::INVOKE_SUPER:
1191 DoInvoke(self, mh, shadow_frame, dec_insn, kSuper, false, &result_register);
1192 break;
1193 case Instruction::INVOKE_SUPER_RANGE:
1194 DoInvoke(self, mh, shadow_frame, dec_insn, kSuper, true, &result_register);
1195 break;
1196 case Instruction::INVOKE_DIRECT:
1197 DoInvoke(self, mh, shadow_frame, dec_insn, kDirect, false, &result_register);
1198 break;
1199 case Instruction::INVOKE_DIRECT_RANGE:
1200 DoInvoke(self, mh, shadow_frame, dec_insn, kDirect, true, &result_register);
1201 break;
1202 case Instruction::INVOKE_INTERFACE:
1203 DoInvoke(self, mh, shadow_frame, dec_insn, kInterface, false, &result_register);
1204 break;
1205 case Instruction::INVOKE_INTERFACE_RANGE:
1206 DoInvoke(self, mh, shadow_frame, dec_insn, kInterface, true, &result_register);
1207 break;
1208 case Instruction::INVOKE_STATIC:
1209 DoInvoke(self, mh, shadow_frame, dec_insn, kStatic, false, &result_register);
1210 break;
1211 case Instruction::INVOKE_STATIC_RANGE:
1212 DoInvoke(self, mh, shadow_frame, dec_insn, kStatic, true, &result_register);
1213 break;
1214 case Instruction::NEG_INT:
1215 shadow_frame.SetVReg(dec_insn.vA, -shadow_frame.GetVReg(dec_insn.vB));
1216 break;
1217 case Instruction::NOT_INT:
1218 shadow_frame.SetVReg(dec_insn.vA, 0 ^ shadow_frame.GetVReg(dec_insn.vB));
1219 break;
1220 case Instruction::NEG_LONG:
1221 shadow_frame.SetVRegLong(dec_insn.vA, -shadow_frame.GetVRegLong(dec_insn.vB));
1222 break;
1223 case Instruction::NOT_LONG:
1224 shadow_frame.SetVRegLong(dec_insn.vA, 0 ^ shadow_frame.GetVRegLong(dec_insn.vB));
1225 break;
1226 case Instruction::NEG_FLOAT:
1227 shadow_frame.SetVRegFloat(dec_insn.vA, -shadow_frame.GetVRegFloat(dec_insn.vB));
1228 break;
1229 case Instruction::NEG_DOUBLE:
1230 shadow_frame.SetVRegDouble(dec_insn.vA, -shadow_frame.GetVRegDouble(dec_insn.vB));
1231 break;
1232 case Instruction::INT_TO_LONG:
1233 shadow_frame.SetVRegLong(dec_insn.vA, shadow_frame.GetVReg(dec_insn.vB));
1234 break;
1235 case Instruction::INT_TO_FLOAT:
1236 shadow_frame.SetVRegFloat(dec_insn.vA, shadow_frame.GetVReg(dec_insn.vB));
1237 break;
1238 case Instruction::INT_TO_DOUBLE:
1239 shadow_frame.SetVRegDouble(dec_insn.vA, shadow_frame.GetVReg(dec_insn.vB));
1240 break;
1241 case Instruction::LONG_TO_INT:
1242 shadow_frame.SetVReg(dec_insn.vA, shadow_frame.GetVRegLong(dec_insn.vB));
1243 break;
1244 case Instruction::LONG_TO_FLOAT:
1245 shadow_frame.SetVRegFloat(dec_insn.vA, shadow_frame.GetVRegLong(dec_insn.vB));
1246 break;
1247 case Instruction::LONG_TO_DOUBLE:
1248 shadow_frame.SetVRegDouble(dec_insn.vA, shadow_frame.GetVRegLong(dec_insn.vB));
1249 break;
1250 case Instruction::FLOAT_TO_INT:
1251 shadow_frame.SetVReg(dec_insn.vA, shadow_frame.GetVRegFloat(dec_insn.vB));
1252 break;
1253 case Instruction::FLOAT_TO_LONG:
1254 shadow_frame.SetVRegLong(dec_insn.vA, shadow_frame.GetVRegFloat(dec_insn.vB));
1255 break;
1256 case Instruction::FLOAT_TO_DOUBLE:
1257 shadow_frame.SetVRegDouble(dec_insn.vA, shadow_frame.GetVRegFloat(dec_insn.vB));
1258 break;
1259 case Instruction::DOUBLE_TO_INT:
1260 shadow_frame.SetVReg(dec_insn.vA, shadow_frame.GetVRegDouble(dec_insn.vB));
1261 break;
1262 case Instruction::DOUBLE_TO_LONG:
1263 shadow_frame.SetVRegLong(dec_insn.vA, shadow_frame.GetVRegDouble(dec_insn.vB));
1264 break;
1265 case Instruction::DOUBLE_TO_FLOAT:
1266 shadow_frame.SetVRegFloat(dec_insn.vA, shadow_frame.GetVRegDouble(dec_insn.vB));
1267 break;
1268 case Instruction::INT_TO_BYTE:
1269 shadow_frame.SetVReg(dec_insn.vA, static_cast<int8_t>(shadow_frame.GetVReg(dec_insn.vB)));
1270 break;
1271 case Instruction::INT_TO_CHAR:
1272 shadow_frame.SetVReg(dec_insn.vA, static_cast<uint16_t>(shadow_frame.GetVReg(dec_insn.vB)));
1273 break;
1274 case Instruction::INT_TO_SHORT:
1275 shadow_frame.SetVReg(dec_insn.vA, static_cast<int16_t>(shadow_frame.GetVReg(dec_insn.vB)));
1276 break;
1277 case Instruction::ADD_INT:
1278 shadow_frame.SetVReg(dec_insn.vA,
1279 shadow_frame.GetVReg(dec_insn.vB) + shadow_frame.GetVReg(dec_insn.vC));
1280 break;
1281 case Instruction::SUB_INT:
1282 shadow_frame.SetVReg(dec_insn.vA,
1283 shadow_frame.GetVReg(dec_insn.vB) - shadow_frame.GetVReg(dec_insn.vC));
1284 break;
1285 case Instruction::MUL_INT:
1286 shadow_frame.SetVReg(dec_insn.vA,
1287 shadow_frame.GetVReg(dec_insn.vB) * shadow_frame.GetVReg(dec_insn.vC));
1288 break;
1289 case Instruction::REM_INT:
1290 shadow_frame.SetVReg(dec_insn.vA,
1291 shadow_frame.GetVReg(dec_insn.vB) % shadow_frame.GetVReg(dec_insn.vC));
1292 break;
1293 case Instruction::DIV_INT:
1294 shadow_frame.SetVReg(dec_insn.vA,
1295 shadow_frame.GetVReg(dec_insn.vB) / shadow_frame.GetVReg(dec_insn.vC));
1296 break;
1297 case Instruction::SHL_INT:
1298 shadow_frame.SetVReg(dec_insn.vA,
1299 shadow_frame.GetVReg(dec_insn.vB) << shadow_frame.GetVReg(dec_insn.vC));
1300 break;
1301 case Instruction::SHR_INT:
1302 shadow_frame.SetVReg(dec_insn.vA,
1303 shadow_frame.GetVReg(dec_insn.vB) >> shadow_frame.GetVReg(dec_insn.vC));
1304 break;
1305 case Instruction::USHR_INT:
1306 shadow_frame.SetVReg(dec_insn.vA,
1307 static_cast<uint32_t>(shadow_frame.GetVReg(dec_insn.vB)) >>
1308 shadow_frame.GetVReg(dec_insn.vC));
1309 break;
1310 case Instruction::AND_INT:
1311 shadow_frame.SetVReg(dec_insn.vA,
1312 shadow_frame.GetVReg(dec_insn.vB) & shadow_frame.GetVReg(dec_insn.vC));
1313 break;
1314 case Instruction::OR_INT:
1315 shadow_frame.SetVReg(dec_insn.vA,
1316 shadow_frame.GetVReg(dec_insn.vB) | shadow_frame.GetVReg(dec_insn.vC));
1317 break;
1318 case Instruction::XOR_INT:
1319 shadow_frame.SetVReg(dec_insn.vA,
1320 shadow_frame.GetVReg(dec_insn.vB) ^ shadow_frame.GetVReg(dec_insn.vC));
1321 break;
1322 case Instruction::ADD_LONG:
1323 shadow_frame.SetVRegLong(dec_insn.vA,
1324 shadow_frame.GetVRegLong(dec_insn.vB) +
1325 shadow_frame.GetVRegLong(dec_insn.vC));
1326 break;
1327 case Instruction::SUB_LONG:
1328 shadow_frame.SetVRegLong(dec_insn.vA,
1329 shadow_frame.GetVRegLong(dec_insn.vB) -
1330 shadow_frame.GetVRegLong(dec_insn.vC));
1331 break;
1332 case Instruction::MUL_LONG:
1333 shadow_frame.SetVRegLong(dec_insn.vA,
1334 shadow_frame.GetVRegLong(dec_insn.vB) *
1335 shadow_frame.GetVRegLong(dec_insn.vC));
1336 break;
1337 case Instruction::DIV_LONG:
1338 shadow_frame.SetVRegLong(dec_insn.vA,
1339 shadow_frame.GetVRegLong(dec_insn.vB) /
1340 shadow_frame.GetVRegLong(dec_insn.vC));
1341 break;
1342 case Instruction::REM_LONG:
1343 shadow_frame.SetVRegLong(dec_insn.vA,
1344 shadow_frame.GetVRegLong(dec_insn.vB) %
1345 shadow_frame.GetVRegLong(dec_insn.vC));
1346 break;
1347 case Instruction::AND_LONG:
1348 shadow_frame.SetVRegLong(dec_insn.vA,
1349 shadow_frame.GetVRegLong(dec_insn.vB) &
1350 shadow_frame.GetVRegLong(dec_insn.vC));
1351 break;
1352 case Instruction::OR_LONG:
1353 shadow_frame.SetVRegLong(dec_insn.vA,
1354 shadow_frame.GetVRegLong(dec_insn.vB) |
1355 shadow_frame.GetVRegLong(dec_insn.vC));
1356 break;
1357 case Instruction::XOR_LONG:
1358 shadow_frame.SetVRegLong(dec_insn.vA,
1359 shadow_frame.GetVRegLong(dec_insn.vB) ^
1360 shadow_frame.GetVRegLong(dec_insn.vC));
1361 break;
1362 case Instruction::SHL_LONG:
1363 shadow_frame.SetVRegLong(dec_insn.vA,
1364 shadow_frame.GetVRegLong(dec_insn.vB) <<
1365 shadow_frame.GetVReg(dec_insn.vC));
1366 break;
1367 case Instruction::SHR_LONG:
1368 shadow_frame.SetVRegLong(dec_insn.vA,
1369 shadow_frame.GetVRegLong(dec_insn.vB) >>
1370 shadow_frame.GetVReg(dec_insn.vC));
1371 break;
1372 case Instruction::USHR_LONG:
1373 shadow_frame.SetVRegLong(dec_insn.vA,
1374 static_cast<uint64_t>(shadow_frame.GetVRegLong(dec_insn.vB)) >>
1375 shadow_frame.GetVReg(dec_insn.vC));
1376 break;
1377 case Instruction::ADD_FLOAT:
1378 shadow_frame.SetVRegFloat(dec_insn.vA,
1379 shadow_frame.GetVRegFloat(dec_insn.vB) +
1380 shadow_frame.GetVRegFloat(dec_insn.vC));
1381 break;
1382 case Instruction::SUB_FLOAT:
1383 shadow_frame.SetVRegFloat(dec_insn.vA,
1384 shadow_frame.GetVRegFloat(dec_insn.vB) -
1385 shadow_frame.GetVRegFloat(dec_insn.vC));
1386 break;
1387 case Instruction::MUL_FLOAT:
1388 shadow_frame.SetVRegFloat(dec_insn.vA,
1389 shadow_frame.GetVRegFloat(dec_insn.vB) *
1390 shadow_frame.GetVRegFloat(dec_insn.vC));
1391 break;
1392 case Instruction::DIV_FLOAT:
1393 shadow_frame.SetVRegFloat(dec_insn.vA,
1394 shadow_frame.GetVRegFloat(dec_insn.vB) /
1395 shadow_frame.GetVRegFloat(dec_insn.vC));
1396 break;
1397 case Instruction::REM_FLOAT:
1398 shadow_frame.SetVRegFloat(dec_insn.vA,
1399 fmodf(shadow_frame.GetVRegFloat(dec_insn.vB),
1400 shadow_frame.GetVRegFloat(dec_insn.vC)));
1401 break;
1402 case Instruction::ADD_DOUBLE:
1403 shadow_frame.SetVRegDouble(dec_insn.vA,
1404 shadow_frame.GetVRegDouble(dec_insn.vB) +
1405 shadow_frame.GetVRegDouble(dec_insn.vC));
1406 break;
1407 case Instruction::SUB_DOUBLE:
1408 shadow_frame.SetVRegDouble(dec_insn.vA,
1409 shadow_frame.GetVRegDouble(dec_insn.vB) -
1410 shadow_frame.GetVRegDouble(dec_insn.vC));
1411 break;
1412 case Instruction::MUL_DOUBLE:
1413 shadow_frame.SetVRegDouble(dec_insn.vA,
1414 shadow_frame.GetVRegDouble(dec_insn.vB) *
1415 shadow_frame.GetVRegDouble(dec_insn.vC));
1416 break;
1417 case Instruction::DIV_DOUBLE:
1418 shadow_frame.SetVRegDouble(dec_insn.vA,
1419 shadow_frame.GetVRegDouble(dec_insn.vB) /
1420 shadow_frame.GetVRegDouble(dec_insn.vC));
1421 break;
1422 case Instruction::REM_DOUBLE:
1423 shadow_frame.SetVRegDouble(dec_insn.vA,
1424 fmod(shadow_frame.GetVRegDouble(dec_insn.vB),
1425 shadow_frame.GetVRegDouble(dec_insn.vC)));
1426 break;
1427 case Instruction::ADD_INT_2ADDR:
1428 shadow_frame.SetVReg(dec_insn.vA,
1429 shadow_frame.GetVReg(dec_insn.vA) + shadow_frame.GetVReg(dec_insn.vB));
1430 break;
1431 case Instruction::SUB_INT_2ADDR:
1432 shadow_frame.SetVReg(dec_insn.vA,
1433 shadow_frame.GetVReg(dec_insn.vA) - shadow_frame.GetVReg(dec_insn.vB));
1434 break;
1435 case Instruction::MUL_INT_2ADDR:
1436 shadow_frame.SetVReg(dec_insn.vA,
1437 shadow_frame.GetVReg(dec_insn.vA) * shadow_frame.GetVReg(dec_insn.vB));
1438 break;
1439 case Instruction::REM_INT_2ADDR:
1440 shadow_frame.SetVReg(dec_insn.vA,
1441 shadow_frame.GetVReg(dec_insn.vA) % shadow_frame.GetVReg(dec_insn.vB));
1442 break;
1443 case Instruction::SHL_INT_2ADDR:
1444 shadow_frame.SetVReg(dec_insn.vA,
1445 shadow_frame.GetVReg(dec_insn.vA) << shadow_frame.GetVReg(dec_insn.vB));
1446 break;
1447 case Instruction::SHR_INT_2ADDR:
1448 shadow_frame.SetVReg(dec_insn.vA,
1449 shadow_frame.GetVReg(dec_insn.vA) >> shadow_frame.GetVReg(dec_insn.vB));
1450 break;
1451 case Instruction::USHR_INT_2ADDR:
1452 shadow_frame.SetVReg(dec_insn.vA,
1453 static_cast<uint32_t>(shadow_frame.GetVReg(dec_insn.vA)) >>
1454 shadow_frame.GetVReg(dec_insn.vB));
1455 break;
1456 case Instruction::AND_INT_2ADDR:
1457 shadow_frame.SetVReg(dec_insn.vA,
1458 shadow_frame.GetVReg(dec_insn.vA) & shadow_frame.GetVReg(dec_insn.vB));
1459 break;
1460 case Instruction::OR_INT_2ADDR:
1461 shadow_frame.SetVReg(dec_insn.vA,
1462 shadow_frame.GetVReg(dec_insn.vA) | shadow_frame.GetVReg(dec_insn.vB));
1463 break;
1464 case Instruction::XOR_INT_2ADDR:
1465 shadow_frame.SetVReg(dec_insn.vA,
1466 shadow_frame.GetVReg(dec_insn.vA) ^ shadow_frame.GetVReg(dec_insn.vB));
1467 break;
1468 case Instruction::DIV_INT_2ADDR:
1469 shadow_frame.SetVReg(dec_insn.vA,
1470 shadow_frame.GetVReg(dec_insn.vA) / shadow_frame.GetVReg(dec_insn.vB));
1471 break;
1472 case Instruction::ADD_LONG_2ADDR:
1473 shadow_frame.SetVRegLong(dec_insn.vA,
1474 shadow_frame.GetVRegLong(dec_insn.vA) +
1475 shadow_frame.GetVRegLong(dec_insn.vB));
1476 break;
1477 case Instruction::SUB_LONG_2ADDR:
1478 shadow_frame.SetVRegLong(dec_insn.vA,
1479 shadow_frame.GetVRegLong(dec_insn.vA) -
1480 shadow_frame.GetVRegLong(dec_insn.vB));
1481 break;
1482 case Instruction::MUL_LONG_2ADDR:
1483 shadow_frame.SetVRegLong(dec_insn.vA,
Ian Rogers64b6d142012-10-29 16:34:15 -07001484 shadow_frame.GetVRegLong(dec_insn.vA) *
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001485 shadow_frame.GetVRegLong(dec_insn.vB));
1486 break;
1487 case Instruction::DIV_LONG_2ADDR:
1488 shadow_frame.SetVRegLong(dec_insn.vA,
1489 shadow_frame.GetVRegLong(dec_insn.vA) /
1490 shadow_frame.GetVRegLong(dec_insn.vB));
1491 break;
1492 case Instruction::REM_LONG_2ADDR:
1493 shadow_frame.SetVRegLong(dec_insn.vA,
1494 shadow_frame.GetVRegLong(dec_insn.vA) %
1495 shadow_frame.GetVRegLong(dec_insn.vB));
1496 break;
1497 case Instruction::AND_LONG_2ADDR:
1498 shadow_frame.SetVRegLong(dec_insn.vA,
1499 shadow_frame.GetVRegLong(dec_insn.vA) &
1500 shadow_frame.GetVRegLong(dec_insn.vB));
1501 break;
1502 case Instruction::OR_LONG_2ADDR:
1503 shadow_frame.SetVRegLong(dec_insn.vA,
1504 shadow_frame.GetVRegLong(dec_insn.vA) |
1505 shadow_frame.GetVRegLong(dec_insn.vB));
1506 break;
1507 case Instruction::XOR_LONG_2ADDR:
1508 shadow_frame.SetVRegLong(dec_insn.vA,
1509 shadow_frame.GetVRegLong(dec_insn.vA) ^
1510 shadow_frame.GetVRegLong(dec_insn.vB));
1511 break;
1512 case Instruction::SHL_LONG_2ADDR:
1513 shadow_frame.SetVRegLong(dec_insn.vA,
1514 shadow_frame.GetVRegLong(dec_insn.vA) <<
1515 shadow_frame.GetVReg(dec_insn.vB));
1516 break;
1517 case Instruction::SHR_LONG_2ADDR:
1518 shadow_frame.SetVRegLong(dec_insn.vA,
1519 shadow_frame.GetVRegLong(dec_insn.vA) >>
1520 shadow_frame.GetVReg(dec_insn.vB));
1521 break;
1522 case Instruction::USHR_LONG_2ADDR:
1523 shadow_frame.SetVRegLong(dec_insn.vA,
1524 static_cast<uint64_t>(shadow_frame.GetVRegLong(dec_insn.vA)) >>
1525 shadow_frame.GetVReg(dec_insn.vB));
1526 break;
1527 case Instruction::ADD_FLOAT_2ADDR:
1528 shadow_frame.SetVRegFloat(dec_insn.vA,
1529 shadow_frame.GetVRegFloat(dec_insn.vA) +
1530 shadow_frame.GetVRegFloat(dec_insn.vB));
1531 break;
1532 case Instruction::SUB_FLOAT_2ADDR:
1533 shadow_frame.SetVRegFloat(dec_insn.vA,
1534 shadow_frame.GetVRegFloat(dec_insn.vA) -
1535 shadow_frame.GetVRegFloat(dec_insn.vB));
1536 break;
1537 case Instruction::MUL_FLOAT_2ADDR:
1538 shadow_frame.SetVRegFloat(dec_insn.vA,
1539 shadow_frame.GetVRegFloat(dec_insn.vA) *
1540 shadow_frame.GetVRegFloat(dec_insn.vB));
1541 break;
1542 case Instruction::DIV_FLOAT_2ADDR:
1543 shadow_frame.SetVRegFloat(dec_insn.vA,
1544 shadow_frame.GetVRegFloat(dec_insn.vA) /
1545 shadow_frame.GetVRegFloat(dec_insn.vB));
1546 break;
1547 case Instruction::REM_FLOAT_2ADDR:
1548 shadow_frame.SetVRegFloat(dec_insn.vA,
1549 fmodf(shadow_frame.GetVRegFloat(dec_insn.vA),
1550 shadow_frame.GetVRegFloat(dec_insn.vB)));
1551 break;
1552 case Instruction::ADD_DOUBLE_2ADDR:
1553 shadow_frame.SetVRegDouble(dec_insn.vA,
1554 shadow_frame.GetVRegDouble(dec_insn.vA) +
1555 shadow_frame.GetVRegDouble(dec_insn.vB));
1556 break;
1557 case Instruction::SUB_DOUBLE_2ADDR:
1558 shadow_frame.SetVRegDouble(dec_insn.vA,
1559 shadow_frame.GetVRegDouble(dec_insn.vA) -
1560 shadow_frame.GetVRegDouble(dec_insn.vB));
1561 break;
1562 case Instruction::MUL_DOUBLE_2ADDR:
1563 shadow_frame.SetVRegDouble(dec_insn.vA,
1564 shadow_frame.GetVRegDouble(dec_insn.vA) *
1565 shadow_frame.GetVRegDouble(dec_insn.vB));
1566 break;
1567 case Instruction::DIV_DOUBLE_2ADDR:
1568 shadow_frame.SetVRegDouble(dec_insn.vA,
1569 shadow_frame.GetVRegDouble(dec_insn.vA) /
1570 shadow_frame.GetVRegDouble(dec_insn.vB));
1571 break;
1572 case Instruction::REM_DOUBLE_2ADDR:
1573 shadow_frame.SetVRegDouble(dec_insn.vA,
1574 fmod(shadow_frame.GetVRegDouble(dec_insn.vA),
1575 shadow_frame.GetVRegDouble(dec_insn.vB)));
1576 break;
1577 case Instruction::ADD_INT_LIT16:
1578 case Instruction::ADD_INT_LIT8:
1579 shadow_frame.SetVReg(dec_insn.vA, shadow_frame.GetVReg(dec_insn.vB) + dec_insn.vC);
1580 break;
1581 case Instruction::RSUB_INT:
1582 case Instruction::RSUB_INT_LIT8:
1583 shadow_frame.SetVReg(dec_insn.vA, dec_insn.vC - shadow_frame.GetVReg(dec_insn.vB));
1584 break;
1585 case Instruction::MUL_INT_LIT16:
1586 case Instruction::MUL_INT_LIT8:
1587 shadow_frame.SetVReg(dec_insn.vA, shadow_frame.GetVReg(dec_insn.vB) * dec_insn.vC);
1588 break;
1589 case Instruction::DIV_INT_LIT16:
1590 case Instruction::DIV_INT_LIT8:
1591 shadow_frame.SetVReg(dec_insn.vA, shadow_frame.GetVReg(dec_insn.vB) / dec_insn.vC);
1592 break;
1593 case Instruction::REM_INT_LIT16:
1594 case Instruction::REM_INT_LIT8:
1595 shadow_frame.SetVReg(dec_insn.vA, shadow_frame.GetVReg(dec_insn.vB) % dec_insn.vC);
1596 break;
1597 case Instruction::AND_INT_LIT16:
1598 case Instruction::AND_INT_LIT8:
1599 shadow_frame.SetVReg(dec_insn.vA, shadow_frame.GetVReg(dec_insn.vB) & dec_insn.vC);
1600 break;
1601 case Instruction::OR_INT_LIT16:
1602 case Instruction::OR_INT_LIT8:
1603 shadow_frame.SetVReg(dec_insn.vA, shadow_frame.GetVReg(dec_insn.vB) | dec_insn.vC);
1604 break;
1605 case Instruction::XOR_INT_LIT16:
1606 case Instruction::XOR_INT_LIT8:
1607 shadow_frame.SetVReg(dec_insn.vA, shadow_frame.GetVReg(dec_insn.vB) ^ dec_insn.vC);
1608 break;
1609 case Instruction::SHL_INT_LIT8:
1610 shadow_frame.SetVReg(dec_insn.vA, shadow_frame.GetVReg(dec_insn.vB) << dec_insn.vC);
1611 break;
1612 case Instruction::SHR_INT_LIT8:
1613 shadow_frame.SetVReg(dec_insn.vA, shadow_frame.GetVReg(dec_insn.vB) >> dec_insn.vC);
1614 break;
1615 case Instruction::USHR_INT_LIT8:
1616 shadow_frame.SetVReg(dec_insn.vA,
1617 static_cast<uint32_t>(shadow_frame.GetVReg(dec_insn.vB)) >>
1618 dec_insn.vC);
1619 break;
1620 default:
1621 LOG(FATAL) << "Unexpected instruction: " << inst->DumpString(&mh.GetDexFile());
1622 break;
1623 }
1624 if (UNLIKELY(self->IsExceptionPending())) {
1625 uint32_t found_dex_pc =
1626 shadow_frame.GetMethod()->FindCatchBlock(self->GetException()->GetClass(),
1627 inst->GetDexPc(insns));
1628 if (found_dex_pc == DexFile::kDexNoIndex) {
1629 JValue result;
1630 result.SetJ(0);
1631 return result; // Handler in caller.
1632 } else {
1633 next_inst = Instruction::At(insns + found_dex_pc);
1634 }
1635 }
1636 inst = next_inst;
1637 }
1638}
1639
1640void EnterInterpreterFromInvoke(Thread* self, AbstractMethod* method, Object* receiver,
1641 JValue* args, JValue* result) {
Ian Rogers64b6d142012-10-29 16:34:15 -07001642 DCHECK_EQ(self, Thread::Current());
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001643 MethodHelper mh(method);
1644 const DexFile::CodeItem* code_item = mh.GetCodeItem();
1645 uint16_t num_regs;
1646 uint16_t num_ins;
1647 if (code_item != NULL) {
1648 num_regs = code_item->registers_size_;
1649 num_ins = code_item->ins_size_;
1650 } else {
1651 DCHECK(method->IsNative());
1652 num_regs = num_ins = AbstractMethod::NumArgRegisters(mh.GetShorty());
1653 if (!method->IsStatic()) {
1654 num_regs++;
1655 num_ins++;
1656 }
1657 }
1658 // Set up shadow frame with matching number of reference slots to vregs.
1659 ShadowFrame* last_shadow_frame = self->GetManagedStack()->GetTopShadowFrame();
1660 UniquePtr<ShadowFrame> shadow_frame(ShadowFrame::Create(num_regs, num_regs,
1661 (last_shadow_frame == NULL) ? NULL : last_shadow_frame->GetLink(),
1662 method, 0));
1663 self->PushShadowFrame(shadow_frame.get());
1664 size_t cur_reg = num_regs - num_ins;
1665 if (!method->IsStatic()) {
1666 CHECK(receiver != NULL);
1667 shadow_frame->SetReferenceAndVReg(cur_reg, receiver);
1668 ++cur_reg;
1669 } else if (!method->GetDeclaringClass()->IsInitializing()) {
1670 Runtime::Current()->GetClassLinker()->EnsureInitialized(method->GetDeclaringClass(),
1671 true, true);
1672 CHECK(method->GetDeclaringClass()->IsInitializing());
1673 }
Ian Rogers64b6d142012-10-29 16:34:15 -07001674 const char* shorty = mh.GetShorty();
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001675 size_t arg_pos = 0;
1676 for (; cur_reg < num_regs; ++cur_reg, ++arg_pos) {
1677 DCHECK_LT(arg_pos + 1, mh.GetShortyLength());
1678 switch (shorty[arg_pos + 1]) {
1679 case 'L': {
1680 Object* o = args[arg_pos].GetL();
1681 shadow_frame->SetReferenceAndVReg(cur_reg, o);
1682 break;
1683 }
1684 case 'J': case 'D':
1685 shadow_frame->SetVRegLong(cur_reg, args[arg_pos].GetJ());
1686 cur_reg++;
1687 break;
1688 default:
1689 shadow_frame->SetVReg(cur_reg, args[arg_pos].GetI());
1690 break;
1691 }
1692 }
Ian Rogers64b6d142012-10-29 16:34:15 -07001693 if (LIKELY(!method->IsNative())) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001694 JValue r = Execute(self, mh, code_item, *shadow_frame.get());
1695 if (result != NULL) {
1696 *result = r;
1697 }
1698 } else {
Ian Rogers64b6d142012-10-29 16:34:15 -07001699 // We don't expect to be asked to interpret native code (which is entered via a JNI compiler
1700 // generated stub) except during testing and image writing.
1701 if (!Runtime::Current()->IsStarted()) {
1702 UnstartedRuntimeJni(self, method, receiver, args, result);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001703 } else {
Ian Rogers64b6d142012-10-29 16:34:15 -07001704 InterpreterJni(self, method, shorty, receiver, args, result);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001705 }
1706 }
1707 self->PopShadowFrame();
1708}
1709
1710} // namespace interpreter
1711} // namespace art