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