blob: d816830beb28bf08e7c54e562c124abc399cc15f [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
Elliott Hughes07ed66b2012-12-12 18:34:25 -080021#include "base/logging.h"
Ian Rogers2fa6b2e2012-10-17 00:10:17 -070022#include "common_throws.h"
jeffhao373c52f2012-11-20 16:11:52 -080023#include "debugger.h"
Ian Rogers2fa6b2e2012-10-17 00:10:17 -070024#include "dex_instruction.h"
25#include "invoke_arg_array_builder.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
jeffhao9b5aa6f2012-12-18 11:47:11 -080042static JDWP::FrameId throw_frame_id_ = 0;
43static AbstractMethod* throw_method_ = NULL;
44static uint32_t throw_dex_pc_ = 0;
45
Ian Rogers64b6d142012-10-29 16:34:15 -070046static void UnstartedRuntimeInvoke(Thread* self, AbstractMethod* target_method,
47 Object* receiver, JValue* args, JValue* result)
48 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
49 // In a runtime that's not started we intercept certain methods to avoid complicated dependency
50 // problems in core libraries.
51 std::string name(PrettyMethod(target_method));
52 if (name == "java.lang.Class java.lang.Class.forName(java.lang.String)") {
53 std::string descriptor(DotToDescriptor(args[0].GetL()->AsString()->ToModifiedUtf8().c_str()));
54 ClassLoader* class_loader = NULL; // shadow_frame.GetMethod()->GetDeclaringClass()->GetClassLoader();
55 Class* found = Runtime::Current()->GetClassLinker()->FindClass(descriptor.c_str(),
56 class_loader);
57 CHECK(found != NULL) << "Class.forName failed in un-started runtime for class: "
58 << PrettyDescriptor(descriptor);
59 result->SetL(found);
60 } else if (name == "java.lang.Object java.lang.Class.newInstance()") {
61 Class* klass = receiver->AsClass();
62 AbstractMethod* c = klass->FindDeclaredDirectMethod("<init>", "()V");
63 CHECK(c != NULL);
64 Object* obj = klass->AllocObject(self);
65 CHECK(obj != NULL);
66 EnterInterpreterFromInvoke(self, c, obj, NULL, NULL);
67 result->SetL(obj);
68 } else if (name == "java.lang.reflect.Field java.lang.Class.getDeclaredField(java.lang.String)") {
69 // Special managed code cut-out to allow field lookup in a un-started runtime that'd fail
70 // going the reflective Dex way.
71 Class* klass = receiver->AsClass();
72 String* name = args[0].GetL()->AsString();
73 Field* found = NULL;
74 FieldHelper fh;
75 ObjectArray<Field>* fields = klass->GetIFields();
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 if (found == NULL) {
84 fields = klass->GetSFields();
85 for (int32_t i = 0; i < fields->GetLength() && found == NULL; ++i) {
86 Field* f = fields->Get(i);
87 fh.ChangeField(f);
88 if (name->Equals(fh.GetName())) {
89 found = f;
90 }
91 }
92 }
93 CHECK(found != NULL)
94 << "Failed to find field in Class.getDeclaredField in un-started runtime. name="
95 << name->ToModifiedUtf8() << " class=" << PrettyDescriptor(klass);
96 // TODO: getDeclaredField calls GetType once the field is found to ensure a
97 // NoClassDefFoundError is thrown if the field's type cannot be resolved.
98 result->SetL(found);
99 } else if (name == "void java.lang.System.arraycopy(java.lang.Object, int, java.lang.Object, int, int)") {
100 // Special case array copying without initializing System.
101 Class* ctype = args[0].GetL()->GetClass()->GetComponentType();
102 jint srcPos = args[1].GetI();
103 jint dstPos = args[3].GetI();
104 jint length = args[4].GetI();
105 if (!ctype->IsPrimitive()) {
106 ObjectArray<Object>* src = args[0].GetL()->AsObjectArray<Object>();
107 ObjectArray<Object>* dst = args[2].GetL()->AsObjectArray<Object>();
108 for (jint i = 0; i < length; ++i) {
109 dst->Set(dstPos + i, src->Get(srcPos + i));
110 }
111 } else if (ctype->IsPrimitiveChar()) {
112 CharArray* src = args[0].GetL()->AsCharArray();
113 CharArray* dst = args[2].GetL()->AsCharArray();
114 for (jint i = 0; i < length; ++i) {
115 dst->Set(dstPos + i, src->Get(srcPos + i));
116 }
117 } else if (ctype->IsPrimitiveInt()) {
118 IntArray* src = args[0].GetL()->AsIntArray();
119 IntArray* dst = args[2].GetL()->AsIntArray();
120 for (jint i = 0; i < length; ++i) {
121 dst->Set(dstPos + i, src->Get(srcPos + i));
122 }
123 } else {
124 UNIMPLEMENTED(FATAL) << "System.arraycopy of unexpected type: " << PrettyDescriptor(ctype);
125 }
126 } else {
127 // Not special, continue with regular interpreter execution.
128 EnterInterpreterFromInvoke(self, target_method, receiver, args, result);
129 }
130}
131
132// Hand select a number of methods to be run in a not yet started runtime without using JNI.
133static void UnstartedRuntimeJni(Thread* self, AbstractMethod* method,
134 Object* receiver, JValue* args, JValue* result)
135 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
136 std::string name(PrettyMethod(method));
137 if (name == "java.lang.ClassLoader dalvik.system.VMStack.getCallingClassLoader()") {
138 result->SetL(NULL);
139 } else if (name == "java.lang.Class dalvik.system.VMStack.getStackClass2()") {
140 NthCallerVisitor visitor(self->GetManagedStack(), NULL, 3);
141 visitor.WalkStack();
142 result->SetL(visitor.caller->GetDeclaringClass());
143 } else if (name == "double java.lang.Math.log(double)") {
144 result->SetD(log(args[0].GetD()));
145 } else if (name == "java.lang.String java.lang.Class.getNameNative()") {
146 result->SetL(receiver->AsClass()->ComputeName());
147 } else if (name == "int java.lang.Float.floatToRawIntBits(float)") {
148 result->SetI(args[0].GetI());
149 } else if (name == "float java.lang.Float.intBitsToFloat(int)") {
150 result->SetF(args[0].GetF());
151 } else if (name == "double java.lang.Math.exp(double)") {
152 result->SetD(exp(args[0].GetD()));
153 } else if (name == "java.lang.Object java.lang.Object.internalClone()") {
154 result->SetL(receiver->Clone(self));
155 } else if (name == "void java.lang.Object.notifyAll()") {
156 receiver->NotifyAll();
157 } else if (name == "int java.lang.String.compareTo(java.lang.String)") {
158 String* rhs = args[0].GetL()->AsString();
159 CHECK(rhs != NULL);
160 result->SetI(receiver->AsString()->CompareTo(rhs));
161 } else if (name == "java.lang.String java.lang.String.intern()") {
162 result->SetL(receiver->AsString()->Intern());
163 } else if (name == "int java.lang.String.fastIndexOf(int, int)") {
164 result->SetI(receiver->AsString()->FastIndexOf(args[0].GetI(), args[1].GetI()));
165 } else if (name == "java.lang.Object java.lang.reflect.Array.createMultiArray(java.lang.Class, int[])") {
166 result->SetL(Array::CreateMultiArray(self, args[0].GetL()->AsClass(), args[1].GetL()->AsIntArray()));
167 } else if (name == "java.lang.Object java.lang.Throwable.nativeFillInStackTrace()") {
168 ScopedObjectAccessUnchecked soa(self);
169 result->SetL(soa.Decode<Object*>(self->CreateInternalStackTrace(soa)));
170 } else if (name == "boolean java.nio.ByteOrder.isLittleEndian()") {
171 result->SetJ(JNI_TRUE);
172 } else if (name == "boolean sun.misc.Unsafe.compareAndSwapInt(java.lang.Object, long, int, int)") {
173 Object* obj = args[0].GetL();
174 jlong offset = args[1].GetJ();
175 jint expectedValue = args[2].GetI();
176 jint newValue = args[3].GetI();
177 byte* raw_addr = reinterpret_cast<byte*>(obj) + offset;
178 volatile int32_t* address = reinterpret_cast<volatile int32_t*>(raw_addr);
179 // Note: android_atomic_release_cas() returns 0 on success, not failure.
180 int r = android_atomic_release_cas(expectedValue, newValue, address);
181 result->SetZ(r == 0);
182 } else if (name == "void sun.misc.Unsafe.putObject(java.lang.Object, long, java.lang.Object)") {
183 Object* obj = args[0].GetL();
184 Object* newValue = args[2].GetL();
185 obj->SetFieldObject(MemberOffset(args[1].GetJ()), newValue, false);
186 } else {
187 LOG(FATAL) << "Attempt to invoke native method in non-started runtime: " << name;
188 }
189}
190
191static void InterpreterJni(Thread* self, AbstractMethod* method, StringPiece shorty,
192 Object* receiver, JValue* args, JValue* result)
193 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
194 // TODO: The following enters JNI code using a typedef-ed function rather than the JNI compiler,
195 // it should be removed and JNI compiled stubs used instead.
196 ScopedObjectAccessUnchecked soa(self);
197 if (method->IsStatic()) {
198 if (shorty == "L") {
199 typedef jobject (fnptr)(JNIEnv*, jclass);
200 fnptr* fn = reinterpret_cast<fnptr*>(method->GetNativeMethod());
201 ScopedLocalRef<jclass> klass(soa.Env(),
202 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
Ian Rogers556d6372012-11-20 12:19:36 -0800203 jobject jresult;
204 {
205 ScopedThreadStateChange tsc(self, kNative);
206 jresult = fn(soa.Env(), klass.get());
207 }
208 result->SetL(soa.Decode<Object*>(jresult));
Ian Rogers64b6d142012-10-29 16:34:15 -0700209 } else if (shorty == "V") {
210 typedef void (fnptr)(JNIEnv*, jclass);
211 fnptr* fn = reinterpret_cast<fnptr*>(method->GetNativeMethod());
212 ScopedLocalRef<jclass> klass(soa.Env(),
213 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
214 ScopedThreadStateChange tsc(self, kNative);
215 fn(soa.Env(), klass.get());
216 } else if (shorty == "Z") {
217 typedef jboolean (fnptr)(JNIEnv*, jclass);
218 fnptr* fn = reinterpret_cast<fnptr*>(method->GetNativeMethod());
219 ScopedLocalRef<jclass> klass(soa.Env(),
220 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
221 ScopedThreadStateChange tsc(self, kNative);
222 result->SetZ(fn(soa.Env(), klass.get()));
223 } else if (shorty == "BI") {
224 typedef jbyte (fnptr)(JNIEnv*, jclass, jint);
225 fnptr* fn = reinterpret_cast<fnptr*>(method->GetNativeMethod());
226 ScopedLocalRef<jclass> klass(soa.Env(),
227 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
228 ScopedThreadStateChange tsc(self, kNative);
229 result->SetB(fn(soa.Env(), klass.get(), args[0].GetI()));
230 } else if (shorty == "II") {
231 typedef jint (fnptr)(JNIEnv*, jclass, jint);
232 fnptr* fn = reinterpret_cast<fnptr*>(method->GetNativeMethod());
233 ScopedLocalRef<jclass> klass(soa.Env(),
234 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
235 ScopedThreadStateChange tsc(self, kNative);
236 result->SetI(fn(soa.Env(), klass.get(), args[0].GetI()));
237 } else if (shorty == "LL") {
238 typedef jobject (fnptr)(JNIEnv*, jclass, jobject);
239 fnptr* fn = reinterpret_cast<fnptr*>(method->GetNativeMethod());
240 ScopedLocalRef<jclass> klass(soa.Env(),
241 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
242 ScopedLocalRef<jobject> arg0(soa.Env(),
243 soa.AddLocalReference<jobject>(args[0].GetL()));
Ian Rogers556d6372012-11-20 12:19:36 -0800244 jobject jresult;
245 {
246 ScopedThreadStateChange tsc(self, kNative);
247 jresult = fn(soa.Env(), klass.get(), arg0.get());
248 }
249 result->SetL(soa.Decode<Object*>(jresult));
Ian Rogers64b6d142012-10-29 16:34:15 -0700250 } else if (shorty == "IIZ") {
251 typedef jint (fnptr)(JNIEnv*, jclass, jint, jboolean);
252 fnptr* fn = reinterpret_cast<fnptr*>(method->GetNativeMethod());
253 ScopedLocalRef<jclass> klass(soa.Env(),
254 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
255 ScopedThreadStateChange tsc(self, kNative);
256 result->SetI(fn(soa.Env(), klass.get(), args[0].GetI(), args[1].GetZ()));
257 } else if (shorty == "ILI") {
258 typedef jint (fnptr)(JNIEnv*, jclass, jobject, jint);
259 fnptr* fn = reinterpret_cast<fnptr*>(method->GetNativeMethod());
260 ScopedLocalRef<jclass> klass(soa.Env(),
261 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
262 ScopedLocalRef<jobject> arg0(soa.Env(),
263 soa.AddLocalReference<jobject>(args[0].GetL()));
264 ScopedThreadStateChange tsc(self, kNative);
265 result->SetI(fn(soa.Env(), klass.get(), arg0.get(), args[1].GetI()));
266 } else if (shorty == "SIZ") {
267 typedef jshort (fnptr)(JNIEnv*, jclass, jint, jboolean);
268 fnptr* fn = reinterpret_cast<fnptr*>(method->GetNativeMethod());
269 ScopedLocalRef<jclass> klass(soa.Env(),
270 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
271 ScopedThreadStateChange tsc(self, kNative);
272 result->SetS(fn(soa.Env(), klass.get(), args[0].GetI(), args[1].GetZ()));
273 } else if (shorty == "VIZ") {
274 typedef void (fnptr)(JNIEnv*, jclass, jint, jboolean);
275 fnptr* fn = reinterpret_cast<fnptr*>(method->GetNativeMethod());
276 ScopedLocalRef<jclass> klass(soa.Env(),
277 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
278 ScopedThreadStateChange tsc(self, kNative);
279 fn(soa.Env(), klass.get(), args[0].GetI(), args[1].GetZ());
280 } else if (shorty == "ZLL") {
281 typedef jboolean (fnptr)(JNIEnv*, jclass, jobject, jobject);
282 fnptr* fn = reinterpret_cast<fnptr*>(method->GetNativeMethod());
283 ScopedLocalRef<jclass> klass(soa.Env(),
284 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
285 ScopedLocalRef<jobject> arg0(soa.Env(),
286 soa.AddLocalReference<jobject>(args[0].GetL()));
287 ScopedLocalRef<jobject> arg1(soa.Env(),
288 soa.AddLocalReference<jobject>(args[1].GetL()));
289 ScopedThreadStateChange tsc(self, kNative);
290 result->SetZ(fn(soa.Env(), klass.get(), arg0.get(), arg1.get()));
291 } else if (shorty == "ZILL") {
292 typedef jboolean (fnptr)(JNIEnv*, jclass, jint, jobject, jobject);
293 fnptr* fn = reinterpret_cast<fnptr*>(method->GetNativeMethod());
294 ScopedLocalRef<jclass> klass(soa.Env(),
295 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
296 ScopedLocalRef<jobject> arg1(soa.Env(),
297 soa.AddLocalReference<jobject>(args[1].GetL()));
298 ScopedLocalRef<jobject> arg2(soa.Env(),
299 soa.AddLocalReference<jobject>(args[2].GetL()));
300 ScopedThreadStateChange tsc(self, kNative);
301 result->SetZ(fn(soa.Env(), klass.get(), args[0].GetI(), arg1.get(), arg2.get()));
302 } else if (shorty == "VILII") {
303 typedef void (fnptr)(JNIEnv*, jclass, 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> arg1(soa.Env(),
308 soa.AddLocalReference<jobject>(args[1].GetL()));
309 ScopedThreadStateChange tsc(self, kNative);
310 fn(soa.Env(), klass.get(), args[0].GetI(), arg1.get(), args[2].GetI(), args[3].GetI());
311 } else if (shorty == "VLILII") {
312 typedef void (fnptr)(JNIEnv*, jclass, jobject, jint, jobject, jint, jint);
313 fnptr* fn = reinterpret_cast<fnptr*>(method->GetNativeMethod());
314 ScopedLocalRef<jclass> klass(soa.Env(),
315 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
316 ScopedLocalRef<jobject> arg0(soa.Env(),
317 soa.AddLocalReference<jobject>(args[0].GetL()));
318 ScopedLocalRef<jobject> arg2(soa.Env(),
319 soa.AddLocalReference<jobject>(args[2].GetL()));
320 ScopedThreadStateChange tsc(self, kNative);
321 fn(soa.Env(), klass.get(), arg0.get(), args[1].GetI(), arg2.get(), args[3].GetI(),
322 args[4].GetI());
323 } else {
324 LOG(FATAL) << "Do something with static native method: " << PrettyMethod(method)
325 << " shorty: " << shorty;
326 }
327 } else {
328 if (shorty == "L") {
329 typedef jobject (fnptr)(JNIEnv*, jobject);
330 fnptr* fn = reinterpret_cast<fnptr*>(method->GetNativeMethod());
331 ScopedLocalRef<jobject> rcvr(soa.Env(),
332 soa.AddLocalReference<jobject>(receiver));
Ian Rogers556d6372012-11-20 12:19:36 -0800333 jobject jresult;
334 {
335 ScopedThreadStateChange tsc(self, kNative);
336 jresult = fn(soa.Env(), rcvr.get());
337 }
338 result->SetL(soa.Decode<Object*>(jresult));
Ian Rogers64b6d142012-10-29 16:34:15 -0700339 } else if (shorty == "LL") {
340 typedef jobject (fnptr)(JNIEnv*, jobject, jobject);
341 fnptr* fn = reinterpret_cast<fnptr*>(method->GetNativeMethod());
342 ScopedLocalRef<jobject> rcvr(soa.Env(),
343 soa.AddLocalReference<jobject>(receiver));
344 ScopedLocalRef<jobject> arg0(soa.Env(),
345 soa.AddLocalReference<jobject>(args[0].GetL()));
Ian Rogers556d6372012-11-20 12:19:36 -0800346 jobject jresult;
347 {
348 ScopedThreadStateChange tsc(self, kNative);
349 jresult = fn(soa.Env(), rcvr.get(), arg0.get());
350
351 }
352 result->SetL(soa.Decode<Object*>(jresult));
Ian Rogers64b6d142012-10-29 16:34:15 -0700353 ScopedThreadStateChange tsc(self, kNative);
Ian Rogers64b6d142012-10-29 16:34:15 -0700354 } else if (shorty == "III") {
355 typedef jint (fnptr)(JNIEnv*, jobject, jint, jint);
356 fnptr* fn = reinterpret_cast<fnptr*>(method->GetNativeMethod());
357 ScopedLocalRef<jobject> rcvr(soa.Env(),
358 soa.AddLocalReference<jobject>(receiver));
359 ScopedThreadStateChange tsc(self, kNative);
360 result->SetI(fn(soa.Env(), rcvr.get(), args[0].GetI(), args[1].GetI()));
361 } else {
362 LOG(FATAL) << "Do something with native method: " << PrettyMethod(method)
363 << " shorty: " << shorty;
364 }
365 }
366}
367
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700368static void DoMonitorEnter(Thread* self, Object* ref) NO_THREAD_SAFETY_ANALYSIS {
369 ref->MonitorEnter(self);
370}
371
372static void DoMonitorExit(Thread* self, Object* ref) NO_THREAD_SAFETY_ANALYSIS {
373 ref->MonitorExit(self);
374}
375
376static void DoInvoke(Thread* self, MethodHelper& mh, ShadowFrame& shadow_frame,
377 const DecodedInstruction& dec_insn, InvokeType type, bool is_range,
378 JValue* result)
379 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
380 Object* receiver;
381 if (type == kStatic) {
382 receiver = NULL;
383 } else {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800384 receiver = shadow_frame.GetVRegReference(dec_insn.vC);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700385 }
386 uint32_t method_idx = dec_insn.vB;
387 AbstractMethod* target_method = FindMethodFromCode(method_idx, receiver,
388 shadow_frame.GetMethod(), self, true,
389 type);
390 if (UNLIKELY(target_method == NULL)) {
391 CHECK(self->IsExceptionPending());
392 result->SetJ(0);
393 return;
394 }
395 mh.ChangeMethod(target_method);
396 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
397 if (is_range) {
398 arg_array.BuildArgArray(shadow_frame, dec_insn.vC + (type != kStatic ? 1 : 0));
399 } else {
400 arg_array.BuildArgArray(shadow_frame, dec_insn.arg + (type != kStatic ? 1 : 0));
401 }
Ian Rogers64b6d142012-10-29 16:34:15 -0700402 if (LIKELY(Runtime::Current()->IsStarted())) {
403 target_method->Invoke(self, receiver, arg_array.get(), result);
404 } else {
405 UnstartedRuntimeInvoke(self, target_method, receiver, arg_array.get(), result);
406 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700407 mh.ChangeMethod(shadow_frame.GetMethod());
408}
409
410static void DoFieldGet(Thread* self, ShadowFrame& shadow_frame,
411 const DecodedInstruction& dec_insn, FindFieldType find_type,
412 Primitive::Type field_type)
413 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
414 bool is_static = (find_type == StaticObjectRead) || (find_type == StaticPrimitiveRead);
415 uint32_t field_idx = is_static ? dec_insn.vB : dec_insn.vC;
416 Field* f = FindFieldFromCode(field_idx, shadow_frame.GetMethod(), self,
417 find_type, Primitive::FieldSize(field_type));
418 if (LIKELY(f != NULL)) {
419 Object* obj;
420 if (is_static) {
421 obj = f->GetDeclaringClass();
422 } else {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800423 obj = shadow_frame.GetVRegReference(dec_insn.vB);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700424 if (UNLIKELY(obj == NULL)) {
425 ThrowNullPointerExceptionForFieldAccess(f, true);
Ian Rogers689d9f02012-11-20 16:30:29 -0800426 return;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700427 }
428 }
429 switch (field_type) {
430 case Primitive::kPrimBoolean:
431 shadow_frame.SetVReg(dec_insn.vA, f->GetBoolean(obj));
432 break;
433 case Primitive::kPrimByte:
434 shadow_frame.SetVReg(dec_insn.vA, f->GetByte(obj));
435 break;
436 case Primitive::kPrimChar:
437 shadow_frame.SetVReg(dec_insn.vA, f->GetChar(obj));
438 break;
439 case Primitive::kPrimShort:
440 shadow_frame.SetVReg(dec_insn.vA, f->GetShort(obj));
441 break;
442 case Primitive::kPrimInt:
443 shadow_frame.SetVReg(dec_insn.vA, f->GetInt(obj));
444 break;
445 case Primitive::kPrimLong:
446 shadow_frame.SetVRegLong(dec_insn.vA, f->GetLong(obj));
447 break;
448 case Primitive::kPrimNot:
TDYa127ce4cc0d2012-11-18 16:59:53 -0800449 shadow_frame.SetVRegReference(dec_insn.vA, f->GetObject(obj));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700450 break;
451 default:
452 LOG(FATAL) << "Unreachable: " << field_type;
453 }
454 }
455}
456
457static void DoFieldPut(Thread* self, ShadowFrame& shadow_frame,
458 const DecodedInstruction& dec_insn, FindFieldType find_type,
459 Primitive::Type field_type)
460 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
461 bool is_static = (find_type == StaticObjectWrite) || (find_type == StaticPrimitiveWrite);
462 uint32_t field_idx = is_static ? dec_insn.vB : dec_insn.vC;
463 Field* f = FindFieldFromCode(field_idx, shadow_frame.GetMethod(), self,
464 find_type, Primitive::FieldSize(field_type));
465 if (LIKELY(f != NULL)) {
466 Object* obj;
467 if (is_static) {
468 obj = f->GetDeclaringClass();
469 } else {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800470 obj = shadow_frame.GetVRegReference(dec_insn.vB);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700471 if (UNLIKELY(obj == NULL)) {
472 ThrowNullPointerExceptionForFieldAccess(f, false);
Ian Rogers689d9f02012-11-20 16:30:29 -0800473 return;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700474 }
475 }
476 switch (field_type) {
477 case Primitive::kPrimBoolean:
478 f->SetBoolean(obj, shadow_frame.GetVReg(dec_insn.vA));
479 break;
480 case Primitive::kPrimByte:
481 f->SetByte(obj, shadow_frame.GetVReg(dec_insn.vA));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700482 break;
483 case Primitive::kPrimChar:
484 f->SetChar(obj, shadow_frame.GetVReg(dec_insn.vA));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700485 break;
486 case Primitive::kPrimShort:
487 f->SetShort(obj, shadow_frame.GetVReg(dec_insn.vA));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700488 break;
489 case Primitive::kPrimInt:
490 f->SetInt(obj, shadow_frame.GetVReg(dec_insn.vA));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700491 break;
492 case Primitive::kPrimLong:
493 f->SetLong(obj, shadow_frame.GetVRegLong(dec_insn.vA));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700494 break;
495 case Primitive::kPrimNot:
TDYa127ce4cc0d2012-11-18 16:59:53 -0800496 f->SetObj(obj, shadow_frame.GetVRegReference(dec_insn.vA));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700497 break;
498 default:
499 LOG(FATAL) << "Unreachable: " << field_type;
500 }
501 }
502}
503
jeffhaod91398c2012-11-20 17:17:33 -0800504static void DoIntDivide(Thread* self, ShadowFrame& shadow_frame, size_t result_reg,
505 int32_t dividend, int32_t divisor) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
jeffhaod91398c2012-11-20 17:17:33 -0800506 if (UNLIKELY(divisor == 0)) {
507 self->ThrowNewException("Ljava/lang/ArithmeticException;", "divide by zero");
508 } else if (UNLIKELY(dividend == kMinInt && divisor == -1)) {
509 shadow_frame.SetVReg(result_reg, kMinInt);
510 } else {
511 shadow_frame.SetVReg(result_reg, dividend / divisor);
512 }
513}
514
515static void DoIntRemainder(Thread* self, ShadowFrame& shadow_frame, size_t result_reg,
516 int32_t dividend, int32_t divisor) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
jeffhaod91398c2012-11-20 17:17:33 -0800517 if (UNLIKELY(divisor == 0)) {
518 self->ThrowNewException("Ljava/lang/ArithmeticException;", "divide by zero");
519 } else if (UNLIKELY(dividend == kMinInt && divisor == -1)) {
520 shadow_frame.SetVReg(result_reg, 0);
521 } else {
522 shadow_frame.SetVReg(result_reg, dividend % divisor);
523 }
524}
525
526static void DoLongDivide(Thread* self, ShadowFrame& shadow_frame, size_t result_reg,
527 int64_t dividend, int64_t divisor) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
jeffhaod91398c2012-11-20 17:17:33 -0800528 if (UNLIKELY(divisor == 0)) {
529 self->ThrowNewException("Ljava/lang/ArithmeticException;", "divide by zero");
530 } else if (UNLIKELY(dividend == kMinLong && divisor == -1)) {
531 shadow_frame.SetVRegLong(result_reg, kMinLong);
532 } else {
533 shadow_frame.SetVRegLong(result_reg, dividend / divisor);
534 }
535}
536
537static void DoLongRemainder(Thread* self, ShadowFrame& shadow_frame, size_t result_reg,
538 int64_t dividend, int64_t divisor) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
jeffhaod91398c2012-11-20 17:17:33 -0800539 if (UNLIKELY(divisor == 0)) {
540 self->ThrowNewException("Ljava/lang/ArithmeticException;", "divide by zero");
541 } else if (UNLIKELY(dividend == kMinLong && divisor == -1)) {
542 shadow_frame.SetVRegLong(result_reg, 0);
543 } else {
544 shadow_frame.SetVRegLong(result_reg, dividend % divisor);
545 }
546}
547
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700548static JValue Execute(Thread* self, MethodHelper& mh, const DexFile::CodeItem* code_item,
Ian Rogers306057f2012-11-26 12:45:53 -0800549 ShadowFrame& shadow_frame, JValue result_register)
550 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700551 const uint16_t* insns = code_item->insns_;
552 const Instruction* inst = Instruction::At(insns + shadow_frame.GetDexPC());
jeffhao14f0db92012-12-14 17:50:42 -0800553 bool entry = (inst->GetDexPc(insns) == 0);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700554 while (true) {
jeffhao373c52f2012-11-20 16:11:52 -0800555 CheckSuspend(self);
556 uint32_t dex_pc = inst->GetDexPc(insns);
557 shadow_frame.SetDexPC(dex_pc);
jeffhao14f0db92012-12-14 17:50:42 -0800558 if (entry) {
559 Dbg::UpdateDebugger(-1, self);
560 }
561 entry = false;
jeffhao373c52f2012-11-20 16:11:52 -0800562 Dbg::UpdateDebugger(dex_pc, self);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700563 DecodedInstruction dec_insn(inst);
Ian Rogers64b6d142012-10-29 16:34:15 -0700564 const bool kTracing = false;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700565 if (kTracing) {
566 LOG(INFO) << PrettyMethod(shadow_frame.GetMethod())
567 << StringPrintf("\n0x%x: %s\nReferences:",
568 inst->GetDexPc(insns), inst->DumpString(&mh.GetDexFile()).c_str());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800569 for (size_t i = 0; i < shadow_frame.NumberOfVRegs(); ++i) {
570 Object* o = shadow_frame.GetVRegReference(i);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700571 if (o != NULL) {
572 if (o->GetClass()->IsStringClass() && o->AsString()->GetCharArray() != NULL) {
573 LOG(INFO) << i << ": java.lang.String " << static_cast<void*>(o)
574 << " \"" << o->AsString()->ToModifiedUtf8() << "\"";
575 } else {
576 LOG(INFO) << i << ": " << PrettyTypeOf(o) << " " << static_cast<void*>(o);
577 }
578 } else {
579 LOG(INFO) << i << ": null";
580 }
581 }
582 LOG(INFO) << "vregs:";
TDYa127ce4cc0d2012-11-18 16:59:53 -0800583 for (size_t i = 0; i < shadow_frame.NumberOfVRegs(); ++i) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700584 LOG(INFO) << StringPrintf("%d: %08x", i, shadow_frame.GetVReg(i));
585 }
586 }
587 const Instruction* next_inst = inst->Next();
588 switch (dec_insn.opcode) {
589 case Instruction::NOP:
590 break;
591 case Instruction::MOVE:
592 case Instruction::MOVE_FROM16:
593 case Instruction::MOVE_16:
594 shadow_frame.SetVReg(dec_insn.vA, shadow_frame.GetVReg(dec_insn.vB));
595 break;
596 case Instruction::MOVE_WIDE:
597 case Instruction::MOVE_WIDE_FROM16:
598 case Instruction::MOVE_WIDE_16:
599 shadow_frame.SetVRegLong(dec_insn.vA, shadow_frame.GetVRegLong(dec_insn.vB));
600 break;
601 case Instruction::MOVE_OBJECT:
602 case Instruction::MOVE_OBJECT_FROM16:
603 case Instruction::MOVE_OBJECT_16:
TDYa127ce4cc0d2012-11-18 16:59:53 -0800604 shadow_frame.SetVRegReference(dec_insn.vA, shadow_frame.GetVRegReference(dec_insn.vB));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700605 break;
606 case Instruction::MOVE_RESULT:
607 shadow_frame.SetVReg(dec_insn.vA, result_register.GetI());
608 break;
609 case Instruction::MOVE_RESULT_WIDE:
610 shadow_frame.SetVRegLong(dec_insn.vA, result_register.GetJ());
611 break;
612 case Instruction::MOVE_RESULT_OBJECT:
TDYa127ce4cc0d2012-11-18 16:59:53 -0800613 shadow_frame.SetVRegReference(dec_insn.vA, result_register.GetL());
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700614 break;
615 case Instruction::MOVE_EXCEPTION: {
616 Throwable* exception = self->GetException();
617 self->ClearException();
TDYa127ce4cc0d2012-11-18 16:59:53 -0800618 shadow_frame.SetVRegReference(dec_insn.vA, exception);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700619 break;
620 }
621 case Instruction::RETURN_VOID: {
622 JValue result;
623 result.SetJ(0);
624 return result;
625 }
626 case Instruction::RETURN: {
627 JValue result;
628 result.SetJ(0);
629 result.SetI(shadow_frame.GetVReg(dec_insn.vA));
630 return result;
631 }
632 case Instruction::RETURN_WIDE: {
633 JValue result;
634 result.SetJ(shadow_frame.GetVRegLong(dec_insn.vA));
635 return result;
636 }
637 case Instruction::RETURN_OBJECT: {
638 JValue result;
639 result.SetJ(0);
TDYa127ce4cc0d2012-11-18 16:59:53 -0800640 result.SetL(shadow_frame.GetVRegReference(dec_insn.vA));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700641 return result;
642 }
643 case Instruction::CONST_4: {
Ian Rogers64b6d142012-10-29 16:34:15 -0700644 int32_t val = static_cast<int32_t>(dec_insn.vB << 28) >> 28;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700645 shadow_frame.SetVReg(dec_insn.vA, val);
646 if (val == 0) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800647 shadow_frame.SetVRegReference(dec_insn.vA, NULL);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700648 }
649 break;
650 }
651 case Instruction::CONST_16: {
652 int32_t val = static_cast<int16_t>(dec_insn.vB);
653 shadow_frame.SetVReg(dec_insn.vA, val);
654 if (val == 0) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800655 shadow_frame.SetVRegReference(dec_insn.vA, NULL);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700656 }
657 break;
658 }
659 case Instruction::CONST: {
660 int32_t val = dec_insn.vB;
661 shadow_frame.SetVReg(dec_insn.vA, val);
662 if (val == 0) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800663 shadow_frame.SetVRegReference(dec_insn.vA, NULL);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700664 }
665 break;
666 }
667 case Instruction::CONST_HIGH16: {
668 int32_t val = dec_insn.vB << 16;
669 shadow_frame.SetVReg(dec_insn.vA, val);
670 if (val == 0) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800671 shadow_frame.SetVRegReference(dec_insn.vA, NULL);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700672 }
673 break;
674 }
Ian Rogers64b6d142012-10-29 16:34:15 -0700675 case Instruction::CONST_WIDE_16:
676 shadow_frame.SetVRegLong(dec_insn.vA, static_cast<int16_t>(dec_insn.vB));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700677 break;
Ian Rogers64b6d142012-10-29 16:34:15 -0700678 case Instruction::CONST_WIDE_32:
679 shadow_frame.SetVRegLong(dec_insn.vA, static_cast<int32_t>(dec_insn.vB));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700680 break;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700681 case Instruction::CONST_WIDE:
Ian Rogers64b6d142012-10-29 16:34:15 -0700682 shadow_frame.SetVRegLong(dec_insn.vA, dec_insn.vB_wide);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700683 break;
684 case Instruction::CONST_WIDE_HIGH16:
Ian Rogers64b6d142012-10-29 16:34:15 -0700685 shadow_frame.SetVRegLong(dec_insn.vA, static_cast<uint64_t>(dec_insn.vB) << 48);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700686 break;
687 case Instruction::CONST_STRING:
688 case Instruction::CONST_STRING_JUMBO: {
689 if (UNLIKELY(!String::GetJavaLangString()->IsInitialized())) {
690 Runtime::Current()->GetClassLinker()->EnsureInitialized(String::GetJavaLangString(),
691 true, true);
692 }
693 String* s = mh.ResolveString(dec_insn.vB);
TDYa127ce4cc0d2012-11-18 16:59:53 -0800694 shadow_frame.SetVRegReference(dec_insn.vA, s);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700695 break;
696 }
jeffhao0a9bb732012-11-26 12:28:49 -0800697 case Instruction::CONST_CLASS: {
698 Class* c = ResolveVerifyAndClinit(dec_insn.vB, shadow_frame.GetMethod(), self, false, true);
TDYa127ce4cc0d2012-11-18 16:59:53 -0800699 shadow_frame.SetVRegReference(dec_insn.vA, c);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700700 break;
jeffhao0a9bb732012-11-26 12:28:49 -0800701 }
jeffhao87a6edd2012-11-20 18:05:18 -0800702 case Instruction::MONITOR_ENTER: {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800703 Object* obj = shadow_frame.GetVRegReference(dec_insn.vA);
jeffhao87a6edd2012-11-20 18:05:18 -0800704 if (UNLIKELY(obj == NULL)) {
705 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetMethod(), inst->GetDexPc(insns));
706 } else {
707 DoMonitorEnter(self, obj);
708 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700709 break;
jeffhao87a6edd2012-11-20 18:05:18 -0800710 }
711 case Instruction::MONITOR_EXIT: {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800712 Object* obj = shadow_frame.GetVRegReference(dec_insn.vA);
jeffhao87a6edd2012-11-20 18:05:18 -0800713 if (UNLIKELY(obj == NULL)) {
714 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetMethod(), inst->GetDexPc(insns));
715 } else {
716 DoMonitorExit(self, obj);
717 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700718 break;
jeffhao87a6edd2012-11-20 18:05:18 -0800719 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700720 case Instruction::CHECK_CAST: {
jeffhao0a9bb732012-11-26 12:28:49 -0800721 Class* c = ResolveVerifyAndClinit(dec_insn.vB, shadow_frame.GetMethod(), self, false, true);
jeffhao87a6edd2012-11-20 18:05:18 -0800722 if (UNLIKELY(c == NULL)) {
723 CHECK(self->IsExceptionPending());
724 } else {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800725 Object* obj = shadow_frame.GetVRegReference(dec_insn.vA);
jeffhao87a6edd2012-11-20 18:05:18 -0800726 if (UNLIKELY(obj != NULL && !obj->InstanceOf(c))) {
727 self->ThrowNewExceptionF("Ljava/lang/ClassCastException;",
728 "%s cannot be cast to %s",
729 PrettyDescriptor(obj->GetClass()).c_str(),
730 PrettyDescriptor(c).c_str());
731 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700732 }
733 break;
734 }
735 case Instruction::INSTANCE_OF: {
jeffhao0a9bb732012-11-26 12:28:49 -0800736 Class* c = ResolveVerifyAndClinit(dec_insn.vC, shadow_frame.GetMethod(), self, false, true);
jeffhao87a6edd2012-11-20 18:05:18 -0800737 if (UNLIKELY(c == NULL)) {
738 CHECK(self->IsExceptionPending());
739 } else {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800740 Object* obj = shadow_frame.GetVRegReference(dec_insn.vB);
jeffhao87a6edd2012-11-20 18:05:18 -0800741 shadow_frame.SetVReg(dec_insn.vA, (obj != NULL && obj->InstanceOf(c)) ? 1 : 0);
742 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700743 break;
744 }
745 case Instruction::ARRAY_LENGTH: {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800746 Object* array = shadow_frame.GetVRegReference(dec_insn.vB);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700747 if (UNLIKELY(array == NULL)) {
748 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetMethod(), inst->GetDexPc(insns));
749 break;
750 }
Ian Rogers64b6d142012-10-29 16:34:15 -0700751 shadow_frame.SetVReg(dec_insn.vA, array->AsArray()->GetLength());
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700752 break;
753 }
754 case Instruction::NEW_INSTANCE: {
755 Object* obj = AllocObjectFromCode(dec_insn.vB, shadow_frame.GetMethod(), self, true);
TDYa127ce4cc0d2012-11-18 16:59:53 -0800756 shadow_frame.SetVRegReference(dec_insn.vA, obj);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700757 break;
758 }
759 case Instruction::NEW_ARRAY: {
760 int32_t length = shadow_frame.GetVReg(dec_insn.vB);
761 Object* obj = AllocArrayFromCode(dec_insn.vC, shadow_frame.GetMethod(), length, self, true);
TDYa127ce4cc0d2012-11-18 16:59:53 -0800762 shadow_frame.SetVRegReference(dec_insn.vA, obj);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700763 break;
764 }
765 case Instruction::FILLED_NEW_ARRAY:
Ian Rogers64b6d142012-10-29 16:34:15 -0700766 case Instruction::FILLED_NEW_ARRAY_RANGE: {
767 bool is_range = (dec_insn.opcode == Instruction::FILLED_NEW_ARRAY_RANGE);
768 int32_t length = dec_insn.vA;
769 CHECK(is_range || length <= 5);
jeffhao42b4dc42012-12-10 10:25:43 -0800770 if (UNLIKELY(length < 0)) {
771 self->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", length);
772 break;
773 }
jeffhao0a9bb732012-11-26 12:28:49 -0800774 Class* arrayClass = ResolveVerifyAndClinit(dec_insn.vB, shadow_frame.GetMethod(), self, false, true);
jeffhao42b4dc42012-12-10 10:25:43 -0800775 if (UNLIKELY(arrayClass == NULL)) {
776 CHECK(self->IsExceptionPending());
777 break;
778 }
Ian Rogers64b6d142012-10-29 16:34:15 -0700779 CHECK(arrayClass->IsArrayClass());
jeffhao42b4dc42012-12-10 10:25:43 -0800780 Class* componentClass = arrayClass->GetComponentType();
781 if (UNLIKELY(componentClass->IsPrimitive() && !componentClass->IsPrimitiveInt())) {
782 if (componentClass->IsPrimitiveLong() || componentClass->IsPrimitiveDouble()) {
783 self->ThrowNewExceptionF("Ljava/lang/RuntimeException;",
784 "Bad filled array request for type %s",
785 PrettyDescriptor(componentClass).c_str());
786 } else {
787 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
788 "Found type %s; filled-new-array not implemented for anything but \'int\'",
789 PrettyDescriptor(componentClass).c_str());
790 }
791 break;
792 }
793 Object* newArray = Array::Alloc(self, arrayClass, length);
794 if (newArray != NULL) {
795 for (int32_t i = 0; i < length; ++i) {
796 if (is_range) {
797 if (componentClass->IsPrimitiveInt()) {
798 newArray->AsIntArray()->Set(i, shadow_frame.GetVReg(dec_insn.vC + i));
Ian Rogers64b6d142012-10-29 16:34:15 -0700799 } else {
jeffhao42b4dc42012-12-10 10:25:43 -0800800 newArray->AsObjectArray<Object>()->Set(i, shadow_frame.GetVRegReference(dec_insn.vC + i));
801 }
802 } else {
803 if (componentClass->IsPrimitiveInt()) {
804 newArray->AsIntArray()->Set(i, shadow_frame.GetVReg(dec_insn.arg[i]));
805 } else {
806 newArray->AsObjectArray<Object>()->Set(i, shadow_frame.GetVRegReference(dec_insn.arg[i]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700807 }
808 }
809 }
Ian Rogers64b6d142012-10-29 16:34:15 -0700810 }
jeffhao42b4dc42012-12-10 10:25:43 -0800811 result_register.SetL(newArray);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700812 break;
Ian Rogers64b6d142012-10-29 16:34:15 -0700813 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700814 case Instruction::CMPL_FLOAT: {
815 float val1 = shadow_frame.GetVRegFloat(dec_insn.vB);
816 float val2 = shadow_frame.GetVRegFloat(dec_insn.vC);
817 int32_t result;
818 if (val1 == val2) {
819 result = 0;
820 } else if (val1 > val2) {
821 result = 1;
822 } else {
823 result = -1;
824 }
825 shadow_frame.SetVReg(dec_insn.vA, result);
826 break;
827 }
828 case Instruction::CMPG_FLOAT: {
829 float val1 = shadow_frame.GetVRegFloat(dec_insn.vB);
830 float val2 = shadow_frame.GetVRegFloat(dec_insn.vC);
831 int32_t result;
832 if (val1 == val2) {
833 result = 0;
834 } else if (val1 < val2) {
835 result = -1;
836 } else {
837 result = 1;
838 }
839 shadow_frame.SetVReg(dec_insn.vA, result);
840 break;
841 }
842 case Instruction::CMPL_DOUBLE: {
843 double val1 = shadow_frame.GetVRegDouble(dec_insn.vB);
844 double val2 = shadow_frame.GetVRegDouble(dec_insn.vC);
845 int32_t result;
846 if (val1 == val2) {
847 result = 0;
Ian Rogers58bf0c62012-11-20 16:24:12 -0800848 } else if (val1 > val2) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700849 result = 1;
850 } else {
851 result = -1;
852 }
853 shadow_frame.SetVReg(dec_insn.vA, result);
854 break;
855 }
856
857 case Instruction::CMPG_DOUBLE: {
858 double val1 = shadow_frame.GetVRegDouble(dec_insn.vB);
859 double val2 = shadow_frame.GetVRegDouble(dec_insn.vC);
860 int32_t result;
861 if (val1 == val2) {
862 result = 0;
Ian Rogers58bf0c62012-11-20 16:24:12 -0800863 } else if (val1 < val2) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700864 result = -1;
865 } else {
866 result = 1;
867 }
868 shadow_frame.SetVReg(dec_insn.vA, result);
869 break;
870 }
871 case Instruction::CMP_LONG: {
872 int64_t val1 = shadow_frame.GetVRegLong(dec_insn.vB);
873 int64_t val2 = shadow_frame.GetVRegLong(dec_insn.vC);
874 int32_t result;
jeffhao87a6edd2012-11-20 18:05:18 -0800875 if (val1 > val2) {
Ian Rogers64b6d142012-10-29 16:34:15 -0700876 result = 1;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700877 } else if (val1 == val2) {
878 result = 0;
879 } else {
Ian Rogers64b6d142012-10-29 16:34:15 -0700880 result = -1;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700881 }
882 shadow_frame.SetVReg(dec_insn.vA, result);
883 break;
884 }
885 case Instruction::THROW: {
Ian Rogers5f548e02013-01-16 22:12:55 -0800886 Object* o = shadow_frame.GetVRegReference(dec_insn.vA);
887 Throwable* t = (o == NULL) ? NULL : o->AsThrowable();
jeffhao94d6df42012-11-26 16:02:12 -0800888 self->DeliverException(t);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700889 break;
890 }
891 case Instruction::GOTO:
892 case Instruction::GOTO_16:
893 case Instruction::GOTO_32: {
894 uint32_t dex_pc = inst->GetDexPc(insns);
895 next_inst = Instruction::At(insns + dex_pc + dec_insn.vA);
896 break;
897 }
Ian Rogers556d6372012-11-20 12:19:36 -0800898 case Instruction::PACKED_SWITCH: {
899 uint32_t dex_pc = inst->GetDexPc(insns);
900 const uint16_t* switch_data = insns + dex_pc + dec_insn.vB;
901 int32_t test_val = shadow_frame.GetVReg(dec_insn.vA);
902 CHECK_EQ(switch_data[0], static_cast<uint16_t>(Instruction::kPackedSwitchSignature));
903 uint16_t size = switch_data[1];
904 CHECK_GT(size, 0);
905 const int32_t* keys = reinterpret_cast<const int32_t*>(&switch_data[2]);
906 CHECK(IsAligned<4>(keys));
907 int32_t first_key = keys[0];
908 const int32_t* targets = reinterpret_cast<const int32_t*>(&switch_data[4]);
909 CHECK(IsAligned<4>(targets));
910 int32_t index = test_val - first_key;
911 if (index >= 0 && index < size) {
912 next_inst = Instruction::At(insns + dex_pc + targets[index]);
913 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700914 break;
Ian Rogers556d6372012-11-20 12:19:36 -0800915 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700916 case Instruction::SPARSE_SWITCH: {
917 uint32_t dex_pc = inst->GetDexPc(insns);
Ian Rogers556d6372012-11-20 12:19:36 -0800918 const uint16_t* switch_data = insns + dex_pc + dec_insn.vB;
919 int32_t test_val = shadow_frame.GetVReg(dec_insn.vA);
920 CHECK_EQ(switch_data[0], static_cast<uint16_t>(Instruction::kSparseSwitchSignature));
921 uint16_t size = switch_data[1];
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700922 CHECK_GT(size, 0);
Ian Rogers556d6372012-11-20 12:19:36 -0800923 const int32_t* keys = reinterpret_cast<const int32_t*>(&switch_data[2]);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700924 CHECK(IsAligned<4>(keys));
925 const int32_t* entries = keys + size;
926 CHECK(IsAligned<4>(entries));
927 int lo = 0;
928 int hi = size - 1;
929 while (lo <= hi) {
930 int mid = (lo + hi) / 2;
931 int32_t foundVal = keys[mid];
Ian Rogers556d6372012-11-20 12:19:36 -0800932 if (test_val < foundVal) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700933 hi = mid - 1;
Ian Rogers556d6372012-11-20 12:19:36 -0800934 } else if (test_val > foundVal) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700935 lo = mid + 1;
936 } else {
937 next_inst = Instruction::At(insns + dex_pc + entries[mid]);
938 break;
939 }
940 }
941 break;
942 }
943 case Instruction::FILL_ARRAY_DATA: {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800944 Object* obj = shadow_frame.GetVRegReference(dec_insn.vA);
jeffhao94d6df42012-11-26 16:02:12 -0800945 if (UNLIKELY(obj == NULL)) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700946 Thread::Current()->ThrowNewExceptionF("Ljava/lang/NullPointerException;",
947 "null array in FILL_ARRAY_DATA");
948 break;
949 }
jeffhao94d6df42012-11-26 16:02:12 -0800950 Array* array = obj->AsArray();
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700951 DCHECK(array->IsArrayInstance() && !array->IsObjectArray());
952 uint32_t dex_pc = inst->GetDexPc(insns);
953 const Instruction::ArrayDataPayload* payload =
954 reinterpret_cast<const Instruction::ArrayDataPayload*>(insns + dex_pc + dec_insn.vB);
955 if (UNLIKELY(static_cast<int32_t>(payload->element_count) > array->GetLength())) {
956 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
957 "failed FILL_ARRAY_DATA; length=%d, index=%d",
958 array->GetLength(), payload->element_count);
959 break;
960 }
961 uint32_t size_in_bytes = payload->element_count * payload->element_width;
962 memcpy(array->GetRawData(payload->element_width), payload->data, size_in_bytes);
963 break;
964 }
965 case Instruction::IF_EQ: {
966 if (shadow_frame.GetVReg(dec_insn.vA) == shadow_frame.GetVReg(dec_insn.vB)) {
967 uint32_t dex_pc = inst->GetDexPc(insns);
968 next_inst = Instruction::At(insns + dex_pc + dec_insn.vC);
969 }
970 break;
971 }
972 case Instruction::IF_NE: {
973 if (shadow_frame.GetVReg(dec_insn.vA) != shadow_frame.GetVReg(dec_insn.vB)) {
974 uint32_t dex_pc = inst->GetDexPc(insns);
975 next_inst = Instruction::At(insns + dex_pc + dec_insn.vC);
976 }
977 break;
978 }
979 case Instruction::IF_LT: {
980 if (shadow_frame.GetVReg(dec_insn.vA) < shadow_frame.GetVReg(dec_insn.vB)) {
981 uint32_t dex_pc = inst->GetDexPc(insns);
982 next_inst = Instruction::At(insns + dex_pc + dec_insn.vC);
983 }
984 break;
985 }
986 case Instruction::IF_GE: {
987 if (shadow_frame.GetVReg(dec_insn.vA) >= shadow_frame.GetVReg(dec_insn.vB)) {
988 uint32_t dex_pc = inst->GetDexPc(insns);
989 next_inst = Instruction::At(insns + dex_pc + dec_insn.vC);
990 }
991 break;
992 }
993 case Instruction::IF_GT: {
994 if (shadow_frame.GetVReg(dec_insn.vA) > shadow_frame.GetVReg(dec_insn.vB)) {
995 uint32_t dex_pc = inst->GetDexPc(insns);
996 next_inst = Instruction::At(insns + dex_pc + dec_insn.vC);
997 }
998 break;
999 }
1000 case Instruction::IF_LE: {
1001 if (shadow_frame.GetVReg(dec_insn.vA) <= shadow_frame.GetVReg(dec_insn.vB)) {
1002 uint32_t dex_pc = inst->GetDexPc(insns);
1003 next_inst = Instruction::At(insns + dex_pc + dec_insn.vC);
1004 }
1005 break;
1006 }
1007 case Instruction::IF_EQZ: {
1008 if (shadow_frame.GetVReg(dec_insn.vA) == 0) {
1009 uint32_t dex_pc = inst->GetDexPc(insns);
1010 next_inst = Instruction::At(insns + dex_pc + dec_insn.vB);
1011 }
1012 break;
1013 }
1014 case Instruction::IF_NEZ: {
1015 if (shadow_frame.GetVReg(dec_insn.vA) != 0) {
1016 uint32_t dex_pc = inst->GetDexPc(insns);
1017 next_inst = Instruction::At(insns + dex_pc + dec_insn.vB);
1018 }
1019 break;
1020 }
1021 case Instruction::IF_LTZ: {
1022 if (shadow_frame.GetVReg(dec_insn.vA) < 0) {
1023 uint32_t dex_pc = inst->GetDexPc(insns);
1024 next_inst = Instruction::At(insns + dex_pc + dec_insn.vB);
1025 }
1026 break;
1027 }
1028 case Instruction::IF_GEZ: {
1029 if (shadow_frame.GetVReg(dec_insn.vA) >= 0) {
1030 uint32_t dex_pc = inst->GetDexPc(insns);
1031 next_inst = Instruction::At(insns + dex_pc + dec_insn.vB);
1032 }
1033 break;
1034 }
1035 case Instruction::IF_GTZ: {
1036 if (shadow_frame.GetVReg(dec_insn.vA) > 0) {
1037 uint32_t dex_pc = inst->GetDexPc(insns);
1038 next_inst = Instruction::At(insns + dex_pc + dec_insn.vB);
1039 }
1040 break;
1041 }
1042 case Instruction::IF_LEZ: {
1043 if (shadow_frame.GetVReg(dec_insn.vA) <= 0) {
1044 uint32_t dex_pc = inst->GetDexPc(insns);
1045 next_inst = Instruction::At(insns + dex_pc + dec_insn.vB);
1046 }
1047 break;
1048 }
1049 case Instruction::AGET_BOOLEAN: {
TDYa127ce4cc0d2012-11-18 16:59:53 -08001050 Object* a = shadow_frame.GetVRegReference(dec_insn.vB);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001051 if (UNLIKELY(a == NULL)) {
1052 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetMethod(), inst->GetDexPc(insns));
1053 break;
1054 }
1055 int32_t index = shadow_frame.GetVReg(dec_insn.vC);
jeffhao94d6df42012-11-26 16:02:12 -08001056 shadow_frame.SetVReg(dec_insn.vA, a->AsBooleanArray()->Get(index));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001057 break;
1058 }
1059 case Instruction::AGET_BYTE: {
TDYa127ce4cc0d2012-11-18 16:59:53 -08001060 Object* a = shadow_frame.GetVRegReference(dec_insn.vB);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001061 if (UNLIKELY(a == NULL)) {
1062 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetMethod(), inst->GetDexPc(insns));
1063 break;
1064 }
1065 int32_t index = shadow_frame.GetVReg(dec_insn.vC);
jeffhao94d6df42012-11-26 16:02:12 -08001066 shadow_frame.SetVReg(dec_insn.vA, a->AsByteArray()->Get(index));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001067 break;
1068 }
1069 case Instruction::AGET_CHAR: {
TDYa127ce4cc0d2012-11-18 16:59:53 -08001070 Object* a = shadow_frame.GetVRegReference(dec_insn.vB);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001071 if (UNLIKELY(a == NULL)) {
1072 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetMethod(), inst->GetDexPc(insns));
1073 break;
1074 }
1075 int32_t index = shadow_frame.GetVReg(dec_insn.vC);
jeffhao94d6df42012-11-26 16:02:12 -08001076 shadow_frame.SetVReg(dec_insn.vA, a->AsCharArray()->Get(index));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001077 break;
1078 }
1079 case Instruction::AGET_SHORT: {
TDYa127ce4cc0d2012-11-18 16:59:53 -08001080 Object* a = shadow_frame.GetVRegReference(dec_insn.vB);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001081 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);
jeffhao94d6df42012-11-26 16:02:12 -08001086 shadow_frame.SetVReg(dec_insn.vA, a->AsShortArray()->Get(index));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001087 break;
1088 }
1089 case Instruction::AGET: {
TDYa127ce4cc0d2012-11-18 16:59:53 -08001090 Object* a = shadow_frame.GetVRegReference(dec_insn.vB);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001091 if (UNLIKELY(a == NULL)) {
1092 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetMethod(), inst->GetDexPc(insns));
1093 break;
1094 }
1095 int32_t index = shadow_frame.GetVReg(dec_insn.vC);
jeffhao94d6df42012-11-26 16:02:12 -08001096 shadow_frame.SetVReg(dec_insn.vA, a->AsIntArray()->Get(index));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001097 break;
1098 }
1099 case Instruction::AGET_WIDE: {
TDYa127ce4cc0d2012-11-18 16:59:53 -08001100 Object* a = shadow_frame.GetVRegReference(dec_insn.vB);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001101 if (UNLIKELY(a == NULL)) {
1102 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetMethod(), inst->GetDexPc(insns));
1103 break;
1104 }
1105 int32_t index = shadow_frame.GetVReg(dec_insn.vC);
jeffhao94d6df42012-11-26 16:02:12 -08001106 shadow_frame.SetVRegLong(dec_insn.vA, a->AsLongArray()->Get(index));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001107 break;
1108 }
1109 case Instruction::AGET_OBJECT: {
TDYa127ce4cc0d2012-11-18 16:59:53 -08001110 Object* a = shadow_frame.GetVRegReference(dec_insn.vB);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001111 if (UNLIKELY(a == NULL)) {
1112 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetMethod(), inst->GetDexPc(insns));
1113 break;
1114 }
1115 int32_t index = shadow_frame.GetVReg(dec_insn.vC);
TDYa127ce4cc0d2012-11-18 16:59:53 -08001116 shadow_frame.SetVRegReference(dec_insn.vA, a->AsObjectArray<Object>()->Get(index));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001117 break;
1118 }
1119 case Instruction::APUT_BOOLEAN: {
1120 uint8_t val = shadow_frame.GetVReg(dec_insn.vA);
TDYa127ce4cc0d2012-11-18 16:59:53 -08001121 Object* a = shadow_frame.GetVRegReference(dec_insn.vB);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001122 if (UNLIKELY(a == NULL)) {
1123 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetMethod(), inst->GetDexPc(insns));
1124 break;
1125 }
1126 int32_t index = shadow_frame.GetVReg(dec_insn.vC);
jeffhao94d6df42012-11-26 16:02:12 -08001127 a->AsBooleanArray()->Set(index, val);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001128 break;
1129 }
1130 case Instruction::APUT_BYTE: {
1131 int8_t val = shadow_frame.GetVReg(dec_insn.vA);
TDYa127ce4cc0d2012-11-18 16:59:53 -08001132 Object* a = shadow_frame.GetVRegReference(dec_insn.vB);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001133 if (UNLIKELY(a == NULL)) {
1134 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetMethod(), inst->GetDexPc(insns));
1135 break;
1136 }
1137 int32_t index = shadow_frame.GetVReg(dec_insn.vC);
jeffhao94d6df42012-11-26 16:02:12 -08001138 a->AsByteArray()->Set(index, val);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001139 break;
1140 }
1141 case Instruction::APUT_CHAR: {
1142 uint16_t val = shadow_frame.GetVReg(dec_insn.vA);
TDYa127ce4cc0d2012-11-18 16:59:53 -08001143 Object* a = shadow_frame.GetVRegReference(dec_insn.vB);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001144 if (UNLIKELY(a == NULL)) {
1145 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetMethod(), inst->GetDexPc(insns));
1146 break;
1147 }
1148 int32_t index = shadow_frame.GetVReg(dec_insn.vC);
jeffhao94d6df42012-11-26 16:02:12 -08001149 a->AsCharArray()->Set(index, val);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001150 break;
1151 }
1152 case Instruction::APUT_SHORT: {
1153 int16_t val = shadow_frame.GetVReg(dec_insn.vA);
TDYa127ce4cc0d2012-11-18 16:59:53 -08001154 Object* a = shadow_frame.GetVRegReference(dec_insn.vB);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001155 if (UNLIKELY(a == NULL)) {
1156 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetMethod(), inst->GetDexPc(insns));
1157 break;
1158 }
1159 int32_t index = shadow_frame.GetVReg(dec_insn.vC);
jeffhao94d6df42012-11-26 16:02:12 -08001160 a->AsShortArray()->Set(index, val);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001161 break;
1162 }
1163 case Instruction::APUT: {
1164 int32_t val = shadow_frame.GetVReg(dec_insn.vA);
TDYa127ce4cc0d2012-11-18 16:59:53 -08001165 Object* a = shadow_frame.GetVRegReference(dec_insn.vB);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001166 if (UNLIKELY(a == NULL)) {
1167 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetMethod(), inst->GetDexPc(insns));
1168 break;
1169 }
1170 int32_t index = shadow_frame.GetVReg(dec_insn.vC);
jeffhao94d6df42012-11-26 16:02:12 -08001171 a->AsIntArray()->Set(index, val);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001172 break;
1173 }
1174 case Instruction::APUT_WIDE: {
1175 int64_t val = shadow_frame.GetVRegLong(dec_insn.vA);
TDYa127ce4cc0d2012-11-18 16:59:53 -08001176 Object* a = shadow_frame.GetVRegReference(dec_insn.vB);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001177 if (UNLIKELY(a == NULL)) {
1178 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetMethod(), inst->GetDexPc(insns));
1179 break;
1180 }
1181 int32_t index = shadow_frame.GetVReg(dec_insn.vC);
jeffhao94d6df42012-11-26 16:02:12 -08001182 a->AsLongArray()->Set(index, val);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001183 break;
1184 }
1185 case Instruction::APUT_OBJECT: {
TDYa127ce4cc0d2012-11-18 16:59:53 -08001186 Object* val = shadow_frame.GetVRegReference(dec_insn.vA);
1187 Object* a = shadow_frame.GetVRegReference(dec_insn.vB);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001188 if (UNLIKELY(a == NULL)) {
1189 ThrowNullPointerExceptionFromDexPC(shadow_frame.GetMethod(), inst->GetDexPc(insns));
1190 break;
1191 }
1192 int32_t index = shadow_frame.GetVReg(dec_insn.vC);
jeffhao94d6df42012-11-26 16:02:12 -08001193 a->AsObjectArray<Object>()->Set(index, val);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001194 break;
1195 }
1196 case Instruction::IGET_BOOLEAN:
1197 DoFieldGet(self, shadow_frame, dec_insn, InstancePrimitiveRead, Primitive::kPrimBoolean);
1198 break;
1199 case Instruction::IGET_BYTE:
1200 DoFieldGet(self, shadow_frame, dec_insn, InstancePrimitiveRead, Primitive::kPrimByte);
1201 break;
1202 case Instruction::IGET_CHAR:
1203 DoFieldGet(self, shadow_frame, dec_insn, InstancePrimitiveRead, Primitive::kPrimChar);
1204 break;
1205 case Instruction::IGET_SHORT:
1206 DoFieldGet(self, shadow_frame, dec_insn, InstancePrimitiveRead, Primitive::kPrimShort);
1207 break;
1208 case Instruction::IGET:
1209 DoFieldGet(self, shadow_frame, dec_insn, InstancePrimitiveRead, Primitive::kPrimInt);
1210 break;
1211 case Instruction::IGET_WIDE:
1212 DoFieldGet(self, shadow_frame, dec_insn, InstancePrimitiveRead, Primitive::kPrimLong);
1213 break;
1214 case Instruction::IGET_OBJECT:
1215 DoFieldGet(self, shadow_frame, dec_insn, InstanceObjectRead, Primitive::kPrimNot);
1216 break;
1217 case Instruction::SGET_BOOLEAN:
1218 DoFieldGet(self, shadow_frame, dec_insn, StaticPrimitiveRead, Primitive::kPrimBoolean);
1219 break;
1220 case Instruction::SGET_BYTE:
1221 DoFieldGet(self, shadow_frame, dec_insn, StaticPrimitiveRead, Primitive::kPrimByte);
1222 break;
1223 case Instruction::SGET_CHAR:
1224 DoFieldGet(self, shadow_frame, dec_insn, StaticPrimitiveRead, Primitive::kPrimChar);
1225 break;
1226 case Instruction::SGET_SHORT:
1227 DoFieldGet(self, shadow_frame, dec_insn, StaticPrimitiveRead, Primitive::kPrimShort);
1228 break;
1229 case Instruction::SGET:
1230 DoFieldGet(self, shadow_frame, dec_insn, StaticPrimitiveRead, Primitive::kPrimInt);
1231 break;
1232 case Instruction::SGET_WIDE:
1233 DoFieldGet(self, shadow_frame, dec_insn, StaticPrimitiveRead, Primitive::kPrimLong);
1234 break;
1235 case Instruction::SGET_OBJECT:
1236 DoFieldGet(self, shadow_frame, dec_insn, StaticObjectRead, Primitive::kPrimNot);
1237 break;
1238 case Instruction::IPUT_BOOLEAN:
1239 DoFieldPut(self, shadow_frame, dec_insn, InstancePrimitiveWrite, Primitive::kPrimBoolean);
1240 break;
1241 case Instruction::IPUT_BYTE:
1242 DoFieldPut(self, shadow_frame, dec_insn, InstancePrimitiveWrite, Primitive::kPrimByte);
1243 break;
1244 case Instruction::IPUT_CHAR:
1245 DoFieldPut(self, shadow_frame, dec_insn, InstancePrimitiveWrite, Primitive::kPrimChar);
1246 break;
1247 case Instruction::IPUT_SHORT:
1248 DoFieldPut(self, shadow_frame, dec_insn, InstancePrimitiveWrite, Primitive::kPrimShort);
1249 break;
1250 case Instruction::IPUT:
1251 DoFieldPut(self, shadow_frame, dec_insn, InstancePrimitiveWrite, Primitive::kPrimInt);
1252 break;
1253 case Instruction::IPUT_WIDE:
1254 DoFieldPut(self, shadow_frame, dec_insn, InstancePrimitiveWrite, Primitive::kPrimLong);
1255 break;
1256 case Instruction::IPUT_OBJECT:
1257 DoFieldPut(self, shadow_frame, dec_insn, InstanceObjectWrite, Primitive::kPrimNot);
1258 break;
1259 case Instruction::SPUT_BOOLEAN:
1260 DoFieldPut(self, shadow_frame, dec_insn, StaticPrimitiveWrite, Primitive::kPrimBoolean);
1261 break;
1262 case Instruction::SPUT_BYTE:
1263 DoFieldPut(self, shadow_frame, dec_insn, StaticPrimitiveWrite, Primitive::kPrimByte);
1264 break;
1265 case Instruction::SPUT_CHAR:
1266 DoFieldPut(self, shadow_frame, dec_insn, StaticPrimitiveWrite, Primitive::kPrimChar);
1267 break;
1268 case Instruction::SPUT_SHORT:
1269 DoFieldPut(self, shadow_frame, dec_insn, StaticPrimitiveWrite, Primitive::kPrimShort);
1270 break;
1271 case Instruction::SPUT:
1272 DoFieldPut(self, shadow_frame, dec_insn, StaticPrimitiveWrite, Primitive::kPrimInt);
1273 break;
1274 case Instruction::SPUT_WIDE:
1275 DoFieldPut(self, shadow_frame, dec_insn, StaticPrimitiveWrite, Primitive::kPrimLong);
1276 break;
1277 case Instruction::SPUT_OBJECT:
1278 DoFieldPut(self, shadow_frame, dec_insn, StaticObjectWrite, Primitive::kPrimNot);
1279 break;
1280 case Instruction::INVOKE_VIRTUAL:
1281 DoInvoke(self, mh, shadow_frame, dec_insn, kVirtual, false, &result_register);
1282 break;
1283 case Instruction::INVOKE_VIRTUAL_RANGE:
1284 DoInvoke(self, mh, shadow_frame, dec_insn, kVirtual, true, &result_register);
1285 break;
1286 case Instruction::INVOKE_SUPER:
1287 DoInvoke(self, mh, shadow_frame, dec_insn, kSuper, false, &result_register);
1288 break;
1289 case Instruction::INVOKE_SUPER_RANGE:
1290 DoInvoke(self, mh, shadow_frame, dec_insn, kSuper, true, &result_register);
1291 break;
1292 case Instruction::INVOKE_DIRECT:
1293 DoInvoke(self, mh, shadow_frame, dec_insn, kDirect, false, &result_register);
1294 break;
1295 case Instruction::INVOKE_DIRECT_RANGE:
1296 DoInvoke(self, mh, shadow_frame, dec_insn, kDirect, true, &result_register);
1297 break;
1298 case Instruction::INVOKE_INTERFACE:
1299 DoInvoke(self, mh, shadow_frame, dec_insn, kInterface, false, &result_register);
1300 break;
1301 case Instruction::INVOKE_INTERFACE_RANGE:
1302 DoInvoke(self, mh, shadow_frame, dec_insn, kInterface, true, &result_register);
1303 break;
1304 case Instruction::INVOKE_STATIC:
1305 DoInvoke(self, mh, shadow_frame, dec_insn, kStatic, false, &result_register);
1306 break;
1307 case Instruction::INVOKE_STATIC_RANGE:
1308 DoInvoke(self, mh, shadow_frame, dec_insn, kStatic, true, &result_register);
1309 break;
1310 case Instruction::NEG_INT:
1311 shadow_frame.SetVReg(dec_insn.vA, -shadow_frame.GetVReg(dec_insn.vB));
1312 break;
1313 case Instruction::NOT_INT:
jeffhao42b4dc42012-12-10 10:25:43 -08001314 shadow_frame.SetVReg(dec_insn.vA, ~shadow_frame.GetVReg(dec_insn.vB));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001315 break;
1316 case Instruction::NEG_LONG:
1317 shadow_frame.SetVRegLong(dec_insn.vA, -shadow_frame.GetVRegLong(dec_insn.vB));
1318 break;
1319 case Instruction::NOT_LONG:
jeffhao42b4dc42012-12-10 10:25:43 -08001320 shadow_frame.SetVRegLong(dec_insn.vA, ~shadow_frame.GetVRegLong(dec_insn.vB));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001321 break;
1322 case Instruction::NEG_FLOAT:
1323 shadow_frame.SetVRegFloat(dec_insn.vA, -shadow_frame.GetVRegFloat(dec_insn.vB));
1324 break;
1325 case Instruction::NEG_DOUBLE:
1326 shadow_frame.SetVRegDouble(dec_insn.vA, -shadow_frame.GetVRegDouble(dec_insn.vB));
1327 break;
1328 case Instruction::INT_TO_LONG:
1329 shadow_frame.SetVRegLong(dec_insn.vA, shadow_frame.GetVReg(dec_insn.vB));
1330 break;
1331 case Instruction::INT_TO_FLOAT:
1332 shadow_frame.SetVRegFloat(dec_insn.vA, shadow_frame.GetVReg(dec_insn.vB));
1333 break;
1334 case Instruction::INT_TO_DOUBLE:
1335 shadow_frame.SetVRegDouble(dec_insn.vA, shadow_frame.GetVReg(dec_insn.vB));
1336 break;
1337 case Instruction::LONG_TO_INT:
1338 shadow_frame.SetVReg(dec_insn.vA, shadow_frame.GetVRegLong(dec_insn.vB));
1339 break;
1340 case Instruction::LONG_TO_FLOAT:
1341 shadow_frame.SetVRegFloat(dec_insn.vA, shadow_frame.GetVRegLong(dec_insn.vB));
1342 break;
1343 case Instruction::LONG_TO_DOUBLE:
1344 shadow_frame.SetVRegDouble(dec_insn.vA, shadow_frame.GetVRegLong(dec_insn.vB));
1345 break;
jeffhao87a6edd2012-11-20 18:05:18 -08001346 case Instruction::FLOAT_TO_INT: {
1347 float val = shadow_frame.GetVRegFloat(dec_insn.vB);
1348 if (val != val) {
1349 shadow_frame.SetVReg(dec_insn.vA, 0);
1350 } else if (val > static_cast<float>(kMaxInt)) {
1351 shadow_frame.SetVReg(dec_insn.vA, kMaxInt);
1352 } else if (val < static_cast<float>(kMinInt)) {
1353 shadow_frame.SetVReg(dec_insn.vA, kMinInt);
1354 } else {
1355 shadow_frame.SetVReg(dec_insn.vA, val);
1356 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001357 break;
jeffhao87a6edd2012-11-20 18:05:18 -08001358 }
1359 case Instruction::FLOAT_TO_LONG: {
1360 float val = shadow_frame.GetVRegFloat(dec_insn.vB);
1361 if (val != val) {
1362 shadow_frame.SetVRegLong(dec_insn.vA, 0);
1363 } else if (val > static_cast<float>(kMaxLong)) {
1364 shadow_frame.SetVRegLong(dec_insn.vA, kMaxLong);
1365 } else if (val < static_cast<float>(kMinLong)) {
1366 shadow_frame.SetVRegLong(dec_insn.vA, kMinLong);
1367 } else {
1368 shadow_frame.SetVRegLong(dec_insn.vA, val);
1369 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001370 break;
jeffhao87a6edd2012-11-20 18:05:18 -08001371 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001372 case Instruction::FLOAT_TO_DOUBLE:
1373 shadow_frame.SetVRegDouble(dec_insn.vA, shadow_frame.GetVRegFloat(dec_insn.vB));
1374 break;
jeffhao87a6edd2012-11-20 18:05:18 -08001375 case Instruction::DOUBLE_TO_INT: {
1376 double val = shadow_frame.GetVRegDouble(dec_insn.vB);
1377 if (val != val) {
1378 shadow_frame.SetVReg(dec_insn.vA, 0);
1379 } else if (val > static_cast<double>(kMaxInt)) {
1380 shadow_frame.SetVReg(dec_insn.vA, kMaxInt);
1381 } else if (val < static_cast<double>(kMinInt)) {
1382 shadow_frame.SetVReg(dec_insn.vA, kMinInt);
1383 } else {
1384 shadow_frame.SetVReg(dec_insn.vA, val);
1385 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001386 break;
jeffhao87a6edd2012-11-20 18:05:18 -08001387 }
1388 case Instruction::DOUBLE_TO_LONG: {
1389 double val = shadow_frame.GetVRegDouble(dec_insn.vB);
1390 if (val != val) {
1391 shadow_frame.SetVRegLong(dec_insn.vA, 0);
1392 } else if (val > static_cast<double>(kMaxLong)) {
1393 shadow_frame.SetVRegLong(dec_insn.vA, kMaxLong);
1394 } else if (val < static_cast<double>(kMinLong)) {
1395 shadow_frame.SetVRegLong(dec_insn.vA, kMinLong);
1396 } else {
1397 shadow_frame.SetVRegLong(dec_insn.vA, val);
1398 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001399 break;
jeffhao87a6edd2012-11-20 18:05:18 -08001400 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001401 case Instruction::DOUBLE_TO_FLOAT:
1402 shadow_frame.SetVRegFloat(dec_insn.vA, shadow_frame.GetVRegDouble(dec_insn.vB));
1403 break;
1404 case Instruction::INT_TO_BYTE:
1405 shadow_frame.SetVReg(dec_insn.vA, static_cast<int8_t>(shadow_frame.GetVReg(dec_insn.vB)));
1406 break;
1407 case Instruction::INT_TO_CHAR:
1408 shadow_frame.SetVReg(dec_insn.vA, static_cast<uint16_t>(shadow_frame.GetVReg(dec_insn.vB)));
1409 break;
1410 case Instruction::INT_TO_SHORT:
1411 shadow_frame.SetVReg(dec_insn.vA, static_cast<int16_t>(shadow_frame.GetVReg(dec_insn.vB)));
1412 break;
1413 case Instruction::ADD_INT:
1414 shadow_frame.SetVReg(dec_insn.vA,
1415 shadow_frame.GetVReg(dec_insn.vB) + shadow_frame.GetVReg(dec_insn.vC));
1416 break;
1417 case Instruction::SUB_INT:
1418 shadow_frame.SetVReg(dec_insn.vA,
1419 shadow_frame.GetVReg(dec_insn.vB) - shadow_frame.GetVReg(dec_insn.vC));
1420 break;
1421 case Instruction::MUL_INT:
1422 shadow_frame.SetVReg(dec_insn.vA,
1423 shadow_frame.GetVReg(dec_insn.vB) * shadow_frame.GetVReg(dec_insn.vC));
1424 break;
1425 case Instruction::REM_INT:
jeffhaod91398c2012-11-20 17:17:33 -08001426 DoIntRemainder(self, shadow_frame, dec_insn.vA, shadow_frame.GetVReg(dec_insn.vB),
1427 shadow_frame.GetVReg(dec_insn.vC));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001428 break;
1429 case Instruction::DIV_INT:
jeffhaod91398c2012-11-20 17:17:33 -08001430 DoIntDivide(self, shadow_frame, dec_insn.vA, shadow_frame.GetVReg(dec_insn.vB),
1431 shadow_frame.GetVReg(dec_insn.vC));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001432 break;
1433 case Instruction::SHL_INT:
jeffhao42b4dc42012-12-10 10:25:43 -08001434 shadow_frame.SetVReg(dec_insn.vA, shadow_frame.GetVReg(dec_insn.vB) <<
1435 (shadow_frame.GetVReg(dec_insn.vC) & 0x1f));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001436 break;
1437 case Instruction::SHR_INT:
jeffhao42b4dc42012-12-10 10:25:43 -08001438 shadow_frame.SetVReg(dec_insn.vA, shadow_frame.GetVReg(dec_insn.vB) >>
1439 (shadow_frame.GetVReg(dec_insn.vC) & 0x1f));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001440 break;
1441 case Instruction::USHR_INT:
1442 shadow_frame.SetVReg(dec_insn.vA,
1443 static_cast<uint32_t>(shadow_frame.GetVReg(dec_insn.vB)) >>
jeffhao42b4dc42012-12-10 10:25:43 -08001444 (shadow_frame.GetVReg(dec_insn.vC) & 0x1f));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001445 break;
1446 case Instruction::AND_INT:
1447 shadow_frame.SetVReg(dec_insn.vA,
1448 shadow_frame.GetVReg(dec_insn.vB) & shadow_frame.GetVReg(dec_insn.vC));
1449 break;
1450 case Instruction::OR_INT:
1451 shadow_frame.SetVReg(dec_insn.vA,
1452 shadow_frame.GetVReg(dec_insn.vB) | shadow_frame.GetVReg(dec_insn.vC));
1453 break;
1454 case Instruction::XOR_INT:
1455 shadow_frame.SetVReg(dec_insn.vA,
1456 shadow_frame.GetVReg(dec_insn.vB) ^ shadow_frame.GetVReg(dec_insn.vC));
1457 break;
1458 case Instruction::ADD_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::SUB_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::MUL_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::DIV_LONG:
jeffhaod91398c2012-11-20 17:17:33 -08001474 DoLongDivide(self, shadow_frame, dec_insn.vA, shadow_frame.GetVRegLong(dec_insn.vB),
1475 shadow_frame.GetVRegLong(dec_insn.vC));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001476 break;
1477 case Instruction::REM_LONG:
jeffhaod91398c2012-11-20 17:17:33 -08001478 DoLongRemainder(self, shadow_frame, dec_insn.vA, shadow_frame.GetVRegLong(dec_insn.vB),
1479 shadow_frame.GetVRegLong(dec_insn.vC));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001480 break;
1481 case Instruction::AND_LONG:
1482 shadow_frame.SetVRegLong(dec_insn.vA,
1483 shadow_frame.GetVRegLong(dec_insn.vB) &
1484 shadow_frame.GetVRegLong(dec_insn.vC));
1485 break;
1486 case Instruction::OR_LONG:
1487 shadow_frame.SetVRegLong(dec_insn.vA,
1488 shadow_frame.GetVRegLong(dec_insn.vB) |
1489 shadow_frame.GetVRegLong(dec_insn.vC));
1490 break;
1491 case Instruction::XOR_LONG:
1492 shadow_frame.SetVRegLong(dec_insn.vA,
1493 shadow_frame.GetVRegLong(dec_insn.vB) ^
1494 shadow_frame.GetVRegLong(dec_insn.vC));
1495 break;
1496 case Instruction::SHL_LONG:
1497 shadow_frame.SetVRegLong(dec_insn.vA,
1498 shadow_frame.GetVRegLong(dec_insn.vB) <<
jeffhao42b4dc42012-12-10 10:25:43 -08001499 (shadow_frame.GetVReg(dec_insn.vC) & 0x3f));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001500 break;
1501 case Instruction::SHR_LONG:
1502 shadow_frame.SetVRegLong(dec_insn.vA,
1503 shadow_frame.GetVRegLong(dec_insn.vB) >>
jeffhao42b4dc42012-12-10 10:25:43 -08001504 (shadow_frame.GetVReg(dec_insn.vC) & 0x3f));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001505 break;
1506 case Instruction::USHR_LONG:
1507 shadow_frame.SetVRegLong(dec_insn.vA,
1508 static_cast<uint64_t>(shadow_frame.GetVRegLong(dec_insn.vB)) >>
jeffhao42b4dc42012-12-10 10:25:43 -08001509 (shadow_frame.GetVReg(dec_insn.vC) & 0x3f));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001510 break;
1511 case Instruction::ADD_FLOAT:
1512 shadow_frame.SetVRegFloat(dec_insn.vA,
1513 shadow_frame.GetVRegFloat(dec_insn.vB) +
1514 shadow_frame.GetVRegFloat(dec_insn.vC));
1515 break;
1516 case Instruction::SUB_FLOAT:
1517 shadow_frame.SetVRegFloat(dec_insn.vA,
1518 shadow_frame.GetVRegFloat(dec_insn.vB) -
1519 shadow_frame.GetVRegFloat(dec_insn.vC));
1520 break;
1521 case Instruction::MUL_FLOAT:
1522 shadow_frame.SetVRegFloat(dec_insn.vA,
1523 shadow_frame.GetVRegFloat(dec_insn.vB) *
1524 shadow_frame.GetVRegFloat(dec_insn.vC));
1525 break;
1526 case Instruction::DIV_FLOAT:
1527 shadow_frame.SetVRegFloat(dec_insn.vA,
1528 shadow_frame.GetVRegFloat(dec_insn.vB) /
1529 shadow_frame.GetVRegFloat(dec_insn.vC));
1530 break;
1531 case Instruction::REM_FLOAT:
1532 shadow_frame.SetVRegFloat(dec_insn.vA,
1533 fmodf(shadow_frame.GetVRegFloat(dec_insn.vB),
1534 shadow_frame.GetVRegFloat(dec_insn.vC)));
1535 break;
1536 case Instruction::ADD_DOUBLE:
1537 shadow_frame.SetVRegDouble(dec_insn.vA,
1538 shadow_frame.GetVRegDouble(dec_insn.vB) +
1539 shadow_frame.GetVRegDouble(dec_insn.vC));
1540 break;
1541 case Instruction::SUB_DOUBLE:
1542 shadow_frame.SetVRegDouble(dec_insn.vA,
1543 shadow_frame.GetVRegDouble(dec_insn.vB) -
1544 shadow_frame.GetVRegDouble(dec_insn.vC));
1545 break;
1546 case Instruction::MUL_DOUBLE:
1547 shadow_frame.SetVRegDouble(dec_insn.vA,
1548 shadow_frame.GetVRegDouble(dec_insn.vB) *
1549 shadow_frame.GetVRegDouble(dec_insn.vC));
1550 break;
1551 case Instruction::DIV_DOUBLE:
1552 shadow_frame.SetVRegDouble(dec_insn.vA,
1553 shadow_frame.GetVRegDouble(dec_insn.vB) /
1554 shadow_frame.GetVRegDouble(dec_insn.vC));
1555 break;
1556 case Instruction::REM_DOUBLE:
1557 shadow_frame.SetVRegDouble(dec_insn.vA,
1558 fmod(shadow_frame.GetVRegDouble(dec_insn.vB),
1559 shadow_frame.GetVRegDouble(dec_insn.vC)));
1560 break;
1561 case Instruction::ADD_INT_2ADDR:
1562 shadow_frame.SetVReg(dec_insn.vA,
1563 shadow_frame.GetVReg(dec_insn.vA) + shadow_frame.GetVReg(dec_insn.vB));
1564 break;
1565 case Instruction::SUB_INT_2ADDR:
1566 shadow_frame.SetVReg(dec_insn.vA,
1567 shadow_frame.GetVReg(dec_insn.vA) - shadow_frame.GetVReg(dec_insn.vB));
1568 break;
1569 case Instruction::MUL_INT_2ADDR:
1570 shadow_frame.SetVReg(dec_insn.vA,
1571 shadow_frame.GetVReg(dec_insn.vA) * shadow_frame.GetVReg(dec_insn.vB));
1572 break;
1573 case Instruction::REM_INT_2ADDR:
jeffhaod91398c2012-11-20 17:17:33 -08001574 DoIntRemainder(self, shadow_frame, dec_insn.vA, shadow_frame.GetVReg(dec_insn.vA),
1575 shadow_frame.GetVReg(dec_insn.vB));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001576 break;
1577 case Instruction::SHL_INT_2ADDR:
jeffhao42b4dc42012-12-10 10:25:43 -08001578 shadow_frame.SetVReg(dec_insn.vA, shadow_frame.GetVReg(dec_insn.vA) <<
1579 (shadow_frame.GetVReg(dec_insn.vB) & 0x1f));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001580 break;
1581 case Instruction::SHR_INT_2ADDR:
jeffhao42b4dc42012-12-10 10:25:43 -08001582 shadow_frame.SetVReg(dec_insn.vA, shadow_frame.GetVReg(dec_insn.vA) >>
1583 (shadow_frame.GetVReg(dec_insn.vB) & 0x1f));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001584 break;
1585 case Instruction::USHR_INT_2ADDR:
1586 shadow_frame.SetVReg(dec_insn.vA,
1587 static_cast<uint32_t>(shadow_frame.GetVReg(dec_insn.vA)) >>
jeffhao42b4dc42012-12-10 10:25:43 -08001588 (shadow_frame.GetVReg(dec_insn.vB) & 0x1f));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001589 break;
1590 case Instruction::AND_INT_2ADDR:
1591 shadow_frame.SetVReg(dec_insn.vA,
1592 shadow_frame.GetVReg(dec_insn.vA) & shadow_frame.GetVReg(dec_insn.vB));
1593 break;
1594 case Instruction::OR_INT_2ADDR:
1595 shadow_frame.SetVReg(dec_insn.vA,
1596 shadow_frame.GetVReg(dec_insn.vA) | shadow_frame.GetVReg(dec_insn.vB));
1597 break;
1598 case Instruction::XOR_INT_2ADDR:
1599 shadow_frame.SetVReg(dec_insn.vA,
1600 shadow_frame.GetVReg(dec_insn.vA) ^ shadow_frame.GetVReg(dec_insn.vB));
1601 break;
1602 case Instruction::DIV_INT_2ADDR:
jeffhaod91398c2012-11-20 17:17:33 -08001603 DoIntDivide(self, shadow_frame, dec_insn.vA, shadow_frame.GetVReg(dec_insn.vA),
1604 shadow_frame.GetVReg(dec_insn.vB));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001605 break;
1606 case Instruction::ADD_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::SUB_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::MUL_LONG_2ADDR:
1617 shadow_frame.SetVRegLong(dec_insn.vA,
Ian Rogers64b6d142012-10-29 16:34:15 -07001618 shadow_frame.GetVRegLong(dec_insn.vA) *
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001619 shadow_frame.GetVRegLong(dec_insn.vB));
1620 break;
1621 case Instruction::DIV_LONG_2ADDR:
jeffhaod91398c2012-11-20 17:17:33 -08001622 DoLongDivide(self, shadow_frame, dec_insn.vA, shadow_frame.GetVRegLong(dec_insn.vA),
1623 shadow_frame.GetVRegLong(dec_insn.vB));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001624 break;
1625 case Instruction::REM_LONG_2ADDR:
jeffhaod91398c2012-11-20 17:17:33 -08001626 DoLongRemainder(self, shadow_frame, dec_insn.vA, shadow_frame.GetVRegLong(dec_insn.vA),
1627 shadow_frame.GetVRegLong(dec_insn.vB));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001628 break;
1629 case Instruction::AND_LONG_2ADDR:
1630 shadow_frame.SetVRegLong(dec_insn.vA,
1631 shadow_frame.GetVRegLong(dec_insn.vA) &
1632 shadow_frame.GetVRegLong(dec_insn.vB));
1633 break;
1634 case Instruction::OR_LONG_2ADDR:
1635 shadow_frame.SetVRegLong(dec_insn.vA,
1636 shadow_frame.GetVRegLong(dec_insn.vA) |
1637 shadow_frame.GetVRegLong(dec_insn.vB));
1638 break;
1639 case Instruction::XOR_LONG_2ADDR:
1640 shadow_frame.SetVRegLong(dec_insn.vA,
1641 shadow_frame.GetVRegLong(dec_insn.vA) ^
1642 shadow_frame.GetVRegLong(dec_insn.vB));
1643 break;
1644 case Instruction::SHL_LONG_2ADDR:
1645 shadow_frame.SetVRegLong(dec_insn.vA,
1646 shadow_frame.GetVRegLong(dec_insn.vA) <<
jeffhao42b4dc42012-12-10 10:25:43 -08001647 (shadow_frame.GetVReg(dec_insn.vB) & 0x3f));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001648 break;
1649 case Instruction::SHR_LONG_2ADDR:
1650 shadow_frame.SetVRegLong(dec_insn.vA,
1651 shadow_frame.GetVRegLong(dec_insn.vA) >>
jeffhao42b4dc42012-12-10 10:25:43 -08001652 (shadow_frame.GetVReg(dec_insn.vB) & 0x3f));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001653 break;
1654 case Instruction::USHR_LONG_2ADDR:
1655 shadow_frame.SetVRegLong(dec_insn.vA,
1656 static_cast<uint64_t>(shadow_frame.GetVRegLong(dec_insn.vA)) >>
jeffhao42b4dc42012-12-10 10:25:43 -08001657 (shadow_frame.GetVReg(dec_insn.vB) & 0x3f));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001658 break;
1659 case Instruction::ADD_FLOAT_2ADDR:
1660 shadow_frame.SetVRegFloat(dec_insn.vA,
1661 shadow_frame.GetVRegFloat(dec_insn.vA) +
1662 shadow_frame.GetVRegFloat(dec_insn.vB));
1663 break;
1664 case Instruction::SUB_FLOAT_2ADDR:
1665 shadow_frame.SetVRegFloat(dec_insn.vA,
1666 shadow_frame.GetVRegFloat(dec_insn.vA) -
1667 shadow_frame.GetVRegFloat(dec_insn.vB));
1668 break;
1669 case Instruction::MUL_FLOAT_2ADDR:
1670 shadow_frame.SetVRegFloat(dec_insn.vA,
1671 shadow_frame.GetVRegFloat(dec_insn.vA) *
1672 shadow_frame.GetVRegFloat(dec_insn.vB));
1673 break;
1674 case Instruction::DIV_FLOAT_2ADDR:
1675 shadow_frame.SetVRegFloat(dec_insn.vA,
1676 shadow_frame.GetVRegFloat(dec_insn.vA) /
1677 shadow_frame.GetVRegFloat(dec_insn.vB));
1678 break;
1679 case Instruction::REM_FLOAT_2ADDR:
1680 shadow_frame.SetVRegFloat(dec_insn.vA,
1681 fmodf(shadow_frame.GetVRegFloat(dec_insn.vA),
1682 shadow_frame.GetVRegFloat(dec_insn.vB)));
1683 break;
1684 case Instruction::ADD_DOUBLE_2ADDR:
1685 shadow_frame.SetVRegDouble(dec_insn.vA,
1686 shadow_frame.GetVRegDouble(dec_insn.vA) +
1687 shadow_frame.GetVRegDouble(dec_insn.vB));
1688 break;
1689 case Instruction::SUB_DOUBLE_2ADDR:
1690 shadow_frame.SetVRegDouble(dec_insn.vA,
1691 shadow_frame.GetVRegDouble(dec_insn.vA) -
1692 shadow_frame.GetVRegDouble(dec_insn.vB));
1693 break;
1694 case Instruction::MUL_DOUBLE_2ADDR:
1695 shadow_frame.SetVRegDouble(dec_insn.vA,
1696 shadow_frame.GetVRegDouble(dec_insn.vA) *
1697 shadow_frame.GetVRegDouble(dec_insn.vB));
1698 break;
1699 case Instruction::DIV_DOUBLE_2ADDR:
1700 shadow_frame.SetVRegDouble(dec_insn.vA,
1701 shadow_frame.GetVRegDouble(dec_insn.vA) /
1702 shadow_frame.GetVRegDouble(dec_insn.vB));
1703 break;
1704 case Instruction::REM_DOUBLE_2ADDR:
1705 shadow_frame.SetVRegDouble(dec_insn.vA,
1706 fmod(shadow_frame.GetVRegDouble(dec_insn.vA),
1707 shadow_frame.GetVRegDouble(dec_insn.vB)));
1708 break;
1709 case Instruction::ADD_INT_LIT16:
1710 case Instruction::ADD_INT_LIT8:
1711 shadow_frame.SetVReg(dec_insn.vA, shadow_frame.GetVReg(dec_insn.vB) + dec_insn.vC);
1712 break;
1713 case Instruction::RSUB_INT:
1714 case Instruction::RSUB_INT_LIT8:
1715 shadow_frame.SetVReg(dec_insn.vA, dec_insn.vC - shadow_frame.GetVReg(dec_insn.vB));
1716 break;
1717 case Instruction::MUL_INT_LIT16:
1718 case Instruction::MUL_INT_LIT8:
1719 shadow_frame.SetVReg(dec_insn.vA, shadow_frame.GetVReg(dec_insn.vB) * dec_insn.vC);
1720 break;
1721 case Instruction::DIV_INT_LIT16:
1722 case Instruction::DIV_INT_LIT8:
jeffhaod91398c2012-11-20 17:17:33 -08001723 DoIntDivide(self, shadow_frame, dec_insn.vA, shadow_frame.GetVReg(dec_insn.vB),
1724 dec_insn.vC);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001725 break;
1726 case Instruction::REM_INT_LIT16:
1727 case Instruction::REM_INT_LIT8:
jeffhaod91398c2012-11-20 17:17:33 -08001728 DoIntRemainder(self, shadow_frame, dec_insn.vA, shadow_frame.GetVReg(dec_insn.vB),
1729 dec_insn.vC);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001730 break;
1731 case Instruction::AND_INT_LIT16:
1732 case Instruction::AND_INT_LIT8:
1733 shadow_frame.SetVReg(dec_insn.vA, shadow_frame.GetVReg(dec_insn.vB) & dec_insn.vC);
1734 break;
1735 case Instruction::OR_INT_LIT16:
1736 case Instruction::OR_INT_LIT8:
1737 shadow_frame.SetVReg(dec_insn.vA, shadow_frame.GetVReg(dec_insn.vB) | dec_insn.vC);
1738 break;
1739 case Instruction::XOR_INT_LIT16:
1740 case Instruction::XOR_INT_LIT8:
1741 shadow_frame.SetVReg(dec_insn.vA, shadow_frame.GetVReg(dec_insn.vB) ^ dec_insn.vC);
1742 break;
1743 case Instruction::SHL_INT_LIT8:
jeffhao42b4dc42012-12-10 10:25:43 -08001744 shadow_frame.SetVReg(dec_insn.vA, shadow_frame.GetVReg(dec_insn.vB) <<
1745 (dec_insn.vC & 0x1f));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001746 break;
1747 case Instruction::SHR_INT_LIT8:
jeffhao42b4dc42012-12-10 10:25:43 -08001748 shadow_frame.SetVReg(dec_insn.vA, shadow_frame.GetVReg(dec_insn.vB) >>
1749 (dec_insn.vC & 0x1f));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001750 break;
1751 case Instruction::USHR_INT_LIT8:
1752 shadow_frame.SetVReg(dec_insn.vA,
1753 static_cast<uint32_t>(shadow_frame.GetVReg(dec_insn.vB)) >>
jeffhao42b4dc42012-12-10 10:25:43 -08001754 (dec_insn.vC & 0x1f));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001755 break;
1756 default:
1757 LOG(FATAL) << "Unexpected instruction: " << inst->DumpString(&mh.GetDexFile());
1758 break;
1759 }
1760 if (UNLIKELY(self->IsExceptionPending())) {
jeffhao9b5aa6f2012-12-18 11:47:11 -08001761 if (throw_frame_id_ == 0) {
1762 throw_method_ = shadow_frame.GetMethod();
1763 throw_dex_pc_ = dex_pc;
1764 }
1765 throw_frame_id_++;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001766 uint32_t found_dex_pc =
1767 shadow_frame.GetMethod()->FindCatchBlock(self->GetException()->GetClass(),
1768 inst->GetDexPc(insns));
1769 if (found_dex_pc == DexFile::kDexNoIndex) {
1770 JValue result;
1771 result.SetJ(0);
1772 return result; // Handler in caller.
1773 } else {
jeffhao9b5aa6f2012-12-18 11:47:11 -08001774 Dbg::PostException(self, throw_frame_id_, throw_method_, throw_dex_pc_,
1775 shadow_frame.GetMethod(), found_dex_pc, self->GetException());
1776 throw_frame_id_ = 0;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001777 next_inst = Instruction::At(insns + found_dex_pc);
1778 }
1779 }
1780 inst = next_inst;
1781 }
1782}
1783
1784void EnterInterpreterFromInvoke(Thread* self, AbstractMethod* method, Object* receiver,
1785 JValue* args, JValue* result) {
Ian Rogers64b6d142012-10-29 16:34:15 -07001786 DCHECK_EQ(self, Thread::Current());
jeffhaod7521322012-11-21 15:38:24 -08001787 if (__builtin_frame_address(0) < self->GetStackEnd()) {
1788 ThrowStackOverflowError(self);
1789 return;
1790 }
1791
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001792 MethodHelper mh(method);
1793 const DexFile::CodeItem* code_item = mh.GetCodeItem();
1794 uint16_t num_regs;
1795 uint16_t num_ins;
1796 if (code_item != NULL) {
1797 num_regs = code_item->registers_size_;
1798 num_ins = code_item->ins_size_;
jeffhao0a9bb732012-11-26 12:28:49 -08001799 } else if (method->IsAbstract()) {
1800 self->ThrowNewExceptionF("Ljava/lang/AbstractMethodError;", "abstract method \"%s\"",
1801 PrettyMethod(method).c_str());
1802 return;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001803 } else {
1804 DCHECK(method->IsNative());
1805 num_regs = num_ins = AbstractMethod::NumArgRegisters(mh.GetShorty());
1806 if (!method->IsStatic()) {
1807 num_regs++;
1808 num_ins++;
1809 }
1810 }
1811 // Set up shadow frame with matching number of reference slots to vregs.
1812 ShadowFrame* last_shadow_frame = self->GetManagedStack()->GetTopShadowFrame();
TDYa127ce4cc0d2012-11-18 16:59:53 -08001813 UniquePtr<ShadowFrame> shadow_frame(ShadowFrame::Create(num_regs,
Ian Rogers7db619b2013-01-16 18:35:48 -08001814 last_shadow_frame,
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001815 method, 0));
1816 self->PushShadowFrame(shadow_frame.get());
1817 size_t cur_reg = num_regs - num_ins;
1818 if (!method->IsStatic()) {
1819 CHECK(receiver != NULL);
TDYa127ce4cc0d2012-11-18 16:59:53 -08001820 shadow_frame->SetVRegReference(cur_reg, receiver);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001821 ++cur_reg;
1822 } else if (!method->GetDeclaringClass()->IsInitializing()) {
jeffhao94d6df42012-11-26 16:02:12 -08001823 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(method->GetDeclaringClass(),
1824 true, true)) {
1825 DCHECK(Thread::Current()->IsExceptionPending());
1826 return;
1827 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001828 CHECK(method->GetDeclaringClass()->IsInitializing());
1829 }
Ian Rogers64b6d142012-10-29 16:34:15 -07001830 const char* shorty = mh.GetShorty();
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001831 size_t arg_pos = 0;
1832 for (; cur_reg < num_regs; ++cur_reg, ++arg_pos) {
1833 DCHECK_LT(arg_pos + 1, mh.GetShortyLength());
1834 switch (shorty[arg_pos + 1]) {
1835 case 'L': {
1836 Object* o = args[arg_pos].GetL();
TDYa127ce4cc0d2012-11-18 16:59:53 -08001837 shadow_frame->SetVRegReference(cur_reg, o);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001838 break;
1839 }
1840 case 'J': case 'D':
1841 shadow_frame->SetVRegLong(cur_reg, args[arg_pos].GetJ());
1842 cur_reg++;
1843 break;
1844 default:
1845 shadow_frame->SetVReg(cur_reg, args[arg_pos].GetI());
1846 break;
1847 }
1848 }
Ian Rogers64b6d142012-10-29 16:34:15 -07001849 if (LIKELY(!method->IsNative())) {
Ian Rogers306057f2012-11-26 12:45:53 -08001850 JValue r = Execute(self, mh, code_item, *shadow_frame.get(), JValue());
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001851 if (result != NULL) {
1852 *result = r;
1853 }
1854 } else {
Ian Rogers64b6d142012-10-29 16:34:15 -07001855 // We don't expect to be asked to interpret native code (which is entered via a JNI compiler
1856 // generated stub) except during testing and image writing.
1857 if (!Runtime::Current()->IsStarted()) {
1858 UnstartedRuntimeJni(self, method, receiver, args, result);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001859 } else {
Ian Rogers64b6d142012-10-29 16:34:15 -07001860 InterpreterJni(self, method, shorty, receiver, args, result);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001861 }
1862 }
1863 self->PopShadowFrame();
1864}
1865
Ian Rogers306057f2012-11-26 12:45:53 -08001866JValue EnterInterpreterFromDeoptimize(Thread* self, ShadowFrame& shadow_frame, JValue ret_val)
1867 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1868 MethodHelper mh(shadow_frame.GetMethod());
1869 const DexFile::CodeItem* code_item = mh.GetCodeItem();
1870 return Execute(self, mh, code_item, shadow_frame, ret_val);
1871}
1872
Ian Rogers7db619b2013-01-16 18:35:48 -08001873JValue EnterInterpreterFromStub(Thread* self, MethodHelper& mh, const DexFile::CodeItem* code_item,
1874 ShadowFrame& shadow_frame)
1875 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1876 return Execute(self, mh, code_item, shadow_frame, JValue());
1877}
1878
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001879} // namespace interpreter
1880} // namespace art