blob: c6faf4493f877fb382269cc71bceb36a3bcfa419 [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
Sebastien Hertz8ece0502013-08-07 11:26:41 +020017#include "interpreter_common.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080018
Ian Rogers2fa6b2e2012-10-17 00:10:17 -070019namespace art {
20namespace interpreter {
21
Ian Rogers64b6d142012-10-29 16:34:15 -070022// Hand select a number of methods to be run in a not yet started runtime without using JNI.
Brian Carlstromea46f952013-07-30 01:26:50 -070023static void UnstartedRuntimeJni(Thread* self, ArtMethod* method,
Jeff Hao5d917302013-02-27 17:57:33 -080024 Object* receiver, uint32_t* args, JValue* result)
Ian Rogers64b6d142012-10-29 16:34:15 -070025 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
26 std::string name(PrettyMethod(method));
27 if (name == "java.lang.ClassLoader dalvik.system.VMStack.getCallingClassLoader()") {
28 result->SetL(NULL);
29 } else if (name == "java.lang.Class dalvik.system.VMStack.getStackClass2()") {
Ian Rogers7a22fa62013-01-23 12:16:16 -080030 NthCallerVisitor visitor(self, 3);
Ian Rogers64b6d142012-10-29 16:34:15 -070031 visitor.WalkStack();
32 result->SetL(visitor.caller->GetDeclaringClass());
33 } else if (name == "double java.lang.Math.log(double)") {
Jeff Hao5d917302013-02-27 17:57:33 -080034 JValue value;
35 value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]);
36 result->SetD(log(value.GetD()));
Ian Rogers64b6d142012-10-29 16:34:15 -070037 } else if (name == "java.lang.String java.lang.Class.getNameNative()") {
38 result->SetL(receiver->AsClass()->ComputeName());
39 } else if (name == "int java.lang.Float.floatToRawIntBits(float)") {
Jeff Hao5d917302013-02-27 17:57:33 -080040 result->SetI(args[0]);
Ian Rogers64b6d142012-10-29 16:34:15 -070041 } else if (name == "float java.lang.Float.intBitsToFloat(int)") {
Jeff Hao5d917302013-02-27 17:57:33 -080042 result->SetI(args[0]);
Ian Rogers64b6d142012-10-29 16:34:15 -070043 } else if (name == "double java.lang.Math.exp(double)") {
Jeff Hao5d917302013-02-27 17:57:33 -080044 JValue value;
45 value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]);
46 result->SetD(exp(value.GetD()));
Ian Rogers64b6d142012-10-29 16:34:15 -070047 } else if (name == "java.lang.Object java.lang.Object.internalClone()") {
48 result->SetL(receiver->Clone(self));
49 } else if (name == "void java.lang.Object.notifyAll()") {
Ian Rogers05f30572013-02-20 12:13:11 -080050 receiver->NotifyAll(self);
Ian Rogers64b6d142012-10-29 16:34:15 -070051 } else if (name == "int java.lang.String.compareTo(java.lang.String)") {
Jeff Hao5d917302013-02-27 17:57:33 -080052 String* rhs = reinterpret_cast<Object*>(args[0])->AsString();
Ian Rogers64b6d142012-10-29 16:34:15 -070053 CHECK(rhs != NULL);
54 result->SetI(receiver->AsString()->CompareTo(rhs));
55 } else if (name == "java.lang.String java.lang.String.intern()") {
56 result->SetL(receiver->AsString()->Intern());
57 } else if (name == "int java.lang.String.fastIndexOf(int, int)") {
Jeff Hao5d917302013-02-27 17:57:33 -080058 result->SetI(receiver->AsString()->FastIndexOf(args[0], args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -070059 } else if (name == "java.lang.Object java.lang.reflect.Array.createMultiArray(java.lang.Class, int[])") {
Mathieu Chartier5bb99032014-02-08 16:20:58 -080060 SirtRef<mirror::Class> sirt_class(self, reinterpret_cast<Object*>(args[0])->AsClass());
61 SirtRef<mirror::IntArray> sirt_dimensions(self,
62 reinterpret_cast<Object*>(args[1])->AsIntArray());
63 result->SetL(Array::CreateMultiArray(self, sirt_class, sirt_dimensions));
Ian Rogers64b6d142012-10-29 16:34:15 -070064 } else if (name == "java.lang.Object java.lang.Throwable.nativeFillInStackTrace()") {
65 ScopedObjectAccessUnchecked soa(self);
66 result->SetL(soa.Decode<Object*>(self->CreateInternalStackTrace(soa)));
67 } else if (name == "boolean java.nio.ByteOrder.isLittleEndian()") {
68 result->SetJ(JNI_TRUE);
69 } else if (name == "boolean sun.misc.Unsafe.compareAndSwapInt(java.lang.Object, long, int, int)") {
Jeff Hao5d917302013-02-27 17:57:33 -080070 Object* obj = reinterpret_cast<Object*>(args[0]);
71 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
72 jint expectedValue = args[3];
73 jint newValue = args[4];
Ian Rogers64b6d142012-10-29 16:34:15 -070074 byte* raw_addr = reinterpret_cast<byte*>(obj) + offset;
75 volatile int32_t* address = reinterpret_cast<volatile int32_t*>(raw_addr);
76 // Note: android_atomic_release_cas() returns 0 on success, not failure.
77 int r = android_atomic_release_cas(expectedValue, newValue, address);
78 result->SetZ(r == 0);
79 } else if (name == "void sun.misc.Unsafe.putObject(java.lang.Object, long, java.lang.Object)") {
Jeff Hao5d917302013-02-27 17:57:33 -080080 Object* obj = reinterpret_cast<Object*>(args[0]);
81 Object* newValue = reinterpret_cast<Object*>(args[3]);
82 obj->SetFieldObject(MemberOffset((static_cast<uint64_t>(args[2]) << 32) | args[1]), newValue, false);
Hiroshi Yamauchi4d2efce2014-02-10 16:19:09 -080083 } else if (name == "int sun.misc.Unsafe.getArrayBaseOffsetForComponentType(java.lang.Class)") {
84 mirror::Class* component = reinterpret_cast<Object*>(args[0])->AsClass();
85 Primitive::Type primitive_type = component->GetPrimitiveType();
86 result->SetI(mirror::Array::DataOffset(Primitive::ComponentSize(primitive_type)).Int32Value());
87 } else if (name == "int sun.misc.Unsafe.getArrayIndexScaleForComponentType(java.lang.Class)") {
88 mirror::Class* component = reinterpret_cast<Object*>(args[0])->AsClass();
89 Primitive::Type primitive_type = component->GetPrimitiveType();
90 result->SetI(Primitive::ComponentSize(primitive_type));
Ian Rogers64b6d142012-10-29 16:34:15 -070091 } else {
92 LOG(FATAL) << "Attempt to invoke native method in non-started runtime: " << name;
93 }
94}
95
Ian Rogersfc0e94b2013-09-23 23:51:32 -070096static void InterpreterJni(Thread* self, ArtMethod* method, const StringPiece& shorty,
Jeff Hao5d917302013-02-27 17:57:33 -080097 Object* receiver, uint32_t* args, JValue* result)
Ian Rogers64b6d142012-10-29 16:34:15 -070098 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
99 // TODO: The following enters JNI code using a typedef-ed function rather than the JNI compiler,
100 // it should be removed and JNI compiled stubs used instead.
101 ScopedObjectAccessUnchecked soa(self);
102 if (method->IsStatic()) {
103 if (shorty == "L") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100104 typedef jobject (fntype)(JNIEnv*, jclass);
105 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700106 ScopedLocalRef<jclass> klass(soa.Env(),
107 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
Ian Rogers556d6372012-11-20 12:19:36 -0800108 jobject jresult;
109 {
110 ScopedThreadStateChange tsc(self, kNative);
111 jresult = fn(soa.Env(), klass.get());
112 }
113 result->SetL(soa.Decode<Object*>(jresult));
Ian Rogers64b6d142012-10-29 16:34:15 -0700114 } else if (shorty == "V") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100115 typedef void (fntype)(JNIEnv*, jclass);
116 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700117 ScopedLocalRef<jclass> klass(soa.Env(),
118 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
119 ScopedThreadStateChange tsc(self, kNative);
120 fn(soa.Env(), klass.get());
121 } else if (shorty == "Z") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100122 typedef jboolean (fntype)(JNIEnv*, jclass);
123 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700124 ScopedLocalRef<jclass> klass(soa.Env(),
125 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
126 ScopedThreadStateChange tsc(self, kNative);
127 result->SetZ(fn(soa.Env(), klass.get()));
128 } else if (shorty == "BI") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100129 typedef jbyte (fntype)(JNIEnv*, jclass, jint);
130 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700131 ScopedLocalRef<jclass> klass(soa.Env(),
132 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
133 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800134 result->SetB(fn(soa.Env(), klass.get(), args[0]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700135 } else if (shorty == "II") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100136 typedef jint (fntype)(JNIEnv*, jclass, jint);
137 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700138 ScopedLocalRef<jclass> klass(soa.Env(),
139 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
140 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800141 result->SetI(fn(soa.Env(), klass.get(), args[0]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700142 } else if (shorty == "LL") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100143 typedef jobject (fntype)(JNIEnv*, jclass, jobject);
144 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700145 ScopedLocalRef<jclass> klass(soa.Env(),
146 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
147 ScopedLocalRef<jobject> arg0(soa.Env(),
Jeff Hao5d917302013-02-27 17:57:33 -0800148 soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[0])));
Ian Rogers556d6372012-11-20 12:19:36 -0800149 jobject jresult;
150 {
151 ScopedThreadStateChange tsc(self, kNative);
152 jresult = fn(soa.Env(), klass.get(), arg0.get());
153 }
154 result->SetL(soa.Decode<Object*>(jresult));
Ian Rogers64b6d142012-10-29 16:34:15 -0700155 } else if (shorty == "IIZ") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100156 typedef jint (fntype)(JNIEnv*, jclass, jint, jboolean);
157 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700158 ScopedLocalRef<jclass> klass(soa.Env(),
159 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
160 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800161 result->SetI(fn(soa.Env(), klass.get(), args[0], args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700162 } else if (shorty == "ILI") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100163 typedef jint (fntype)(JNIEnv*, jclass, jobject, jint);
164 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700165 ScopedLocalRef<jclass> klass(soa.Env(),
166 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
167 ScopedLocalRef<jobject> arg0(soa.Env(),
Jeff Hao5d917302013-02-27 17:57:33 -0800168 soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[0])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700169 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800170 result->SetI(fn(soa.Env(), klass.get(), arg0.get(), args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700171 } else if (shorty == "SIZ") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100172 typedef jshort (fntype)(JNIEnv*, jclass, jint, jboolean);
173 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700174 ScopedLocalRef<jclass> klass(soa.Env(),
175 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
176 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800177 result->SetS(fn(soa.Env(), klass.get(), args[0], args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700178 } else if (shorty == "VIZ") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100179 typedef void (fntype)(JNIEnv*, jclass, jint, jboolean);
180 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700181 ScopedLocalRef<jclass> klass(soa.Env(),
182 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
183 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800184 fn(soa.Env(), klass.get(), args[0], args[1]);
Ian Rogers64b6d142012-10-29 16:34:15 -0700185 } else if (shorty == "ZLL") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100186 typedef jboolean (fntype)(JNIEnv*, jclass, jobject, jobject);
187 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700188 ScopedLocalRef<jclass> klass(soa.Env(),
189 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
190 ScopedLocalRef<jobject> arg0(soa.Env(),
Jeff Hao5d917302013-02-27 17:57:33 -0800191 soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[0])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700192 ScopedLocalRef<jobject> arg1(soa.Env(),
Jeff Hao5d917302013-02-27 17:57:33 -0800193 soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[1])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700194 ScopedThreadStateChange tsc(self, kNative);
195 result->SetZ(fn(soa.Env(), klass.get(), arg0.get(), arg1.get()));
196 } else if (shorty == "ZILL") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100197 typedef jboolean (fntype)(JNIEnv*, jclass, jint, jobject, jobject);
198 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700199 ScopedLocalRef<jclass> klass(soa.Env(),
200 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
201 ScopedLocalRef<jobject> arg1(soa.Env(),
Jeff Hao5d917302013-02-27 17:57:33 -0800202 soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[1])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700203 ScopedLocalRef<jobject> arg2(soa.Env(),
Jeff Hao5d917302013-02-27 17:57:33 -0800204 soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[2])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700205 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800206 result->SetZ(fn(soa.Env(), klass.get(), args[0], arg1.get(), arg2.get()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700207 } else if (shorty == "VILII") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100208 typedef void (fntype)(JNIEnv*, jclass, jint, jobject, jint, jint);
209 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700210 ScopedLocalRef<jclass> klass(soa.Env(),
211 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
212 ScopedLocalRef<jobject> arg1(soa.Env(),
Jeff Hao5d917302013-02-27 17:57:33 -0800213 soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[1])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700214 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800215 fn(soa.Env(), klass.get(), args[0], arg1.get(), args[2], args[3]);
Ian Rogers64b6d142012-10-29 16:34:15 -0700216 } else if (shorty == "VLILII") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100217 typedef void (fntype)(JNIEnv*, jclass, jobject, jint, jobject, jint, jint);
218 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700219 ScopedLocalRef<jclass> klass(soa.Env(),
220 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
221 ScopedLocalRef<jobject> arg0(soa.Env(),
Jeff Hao5d917302013-02-27 17:57:33 -0800222 soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[0])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700223 ScopedLocalRef<jobject> arg2(soa.Env(),
Jeff Hao5d917302013-02-27 17:57:33 -0800224 soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[2])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700225 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800226 fn(soa.Env(), klass.get(), arg0.get(), args[1], arg2.get(), args[3], args[4]);
Ian Rogers64b6d142012-10-29 16:34:15 -0700227 } else {
228 LOG(FATAL) << "Do something with static native method: " << PrettyMethod(method)
229 << " shorty: " << shorty;
230 }
231 } else {
232 if (shorty == "L") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100233 typedef jobject (fntype)(JNIEnv*, jobject);
234 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700235 ScopedLocalRef<jobject> rcvr(soa.Env(),
236 soa.AddLocalReference<jobject>(receiver));
Ian Rogers556d6372012-11-20 12:19:36 -0800237 jobject jresult;
238 {
239 ScopedThreadStateChange tsc(self, kNative);
240 jresult = fn(soa.Env(), rcvr.get());
241 }
242 result->SetL(soa.Decode<Object*>(jresult));
Jeff Hao3dd9f762013-07-08 13:09:25 -0700243 } else if (shorty == "V") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100244 typedef void (fntype)(JNIEnv*, jobject);
245 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Jeff Hao3dd9f762013-07-08 13:09:25 -0700246 ScopedLocalRef<jobject> rcvr(soa.Env(),
247 soa.AddLocalReference<jobject>(receiver));
248 ScopedThreadStateChange tsc(self, kNative);
249 fn(soa.Env(), rcvr.get());
Ian Rogers64b6d142012-10-29 16:34:15 -0700250 } else if (shorty == "LL") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100251 typedef jobject (fntype)(JNIEnv*, jobject, jobject);
252 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700253 ScopedLocalRef<jobject> rcvr(soa.Env(),
254 soa.AddLocalReference<jobject>(receiver));
255 ScopedLocalRef<jobject> arg0(soa.Env(),
Jeff Hao5d917302013-02-27 17:57:33 -0800256 soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[0])));
Ian Rogers556d6372012-11-20 12:19:36 -0800257 jobject jresult;
258 {
259 ScopedThreadStateChange tsc(self, kNative);
260 jresult = fn(soa.Env(), rcvr.get(), arg0.get());
Ian Rogers556d6372012-11-20 12:19:36 -0800261 }
262 result->SetL(soa.Decode<Object*>(jresult));
Ian Rogers64b6d142012-10-29 16:34:15 -0700263 ScopedThreadStateChange tsc(self, kNative);
Ian Rogers64b6d142012-10-29 16:34:15 -0700264 } else if (shorty == "III") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100265 typedef jint (fntype)(JNIEnv*, jobject, jint, jint);
266 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700267 ScopedLocalRef<jobject> rcvr(soa.Env(),
268 soa.AddLocalReference<jobject>(receiver));
269 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800270 result->SetI(fn(soa.Env(), rcvr.get(), args[0], args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700271 } else {
272 LOG(FATAL) << "Do something with native method: " << PrettyMethod(method)
273 << " shorty: " << shorty;
274 }
275 }
276}
277
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200278enum InterpreterImplKind {
279 kSwitchImpl, // switch-based interpreter implementation.
280 kComputedGotoImplKind // computed-goto-based interpreter implementation.
281};
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700282
Sebastien Hertz2c88b382013-10-18 10:35:39 +0200283static const InterpreterImplKind kInterpreterImplKind = kComputedGotoImplKind;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700284
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200285static JValue Execute(Thread* self, MethodHelper& mh, const DexFile::CodeItem* code_item,
286 ShadowFrame& shadow_frame, JValue result_register)
287 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
288
289static inline JValue Execute(Thread* self, MethodHelper& mh, const DexFile::CodeItem* code_item,
290 ShadowFrame& shadow_frame, JValue result_register) {
Ian Rogers848871b2013-08-05 10:56:33 -0700291 DCHECK(shadow_frame.GetMethod() == mh.GetMethod() ||
292 shadow_frame.GetMethod()->GetDeclaringClass()->IsProxyClass());
293 DCHECK(!shadow_frame.GetMethod()->IsAbstract());
294 DCHECK(!shadow_frame.GetMethod()->IsNative());
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200295
296 if (LIKELY(shadow_frame.GetMethod()->IsPreverified())) {
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200297 // Enter the "without access check" interpreter.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200298 if (kInterpreterImplKind == kSwitchImpl) {
299 return ExecuteSwitchImpl<false>(self, mh, code_item, shadow_frame, result_register);
300 } else {
301 DCHECK_EQ(kInterpreterImplKind, kComputedGotoImplKind);
302 return ExecuteGotoImpl<false>(self, mh, code_item, shadow_frame, result_register);
303 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200304 } else {
305 // Enter the "with access check" interpreter.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200306 if (kInterpreterImplKind == kSwitchImpl) {
307 return ExecuteSwitchImpl<true>(self, mh, code_item, shadow_frame, result_register);
308 } else {
309 DCHECK_EQ(kInterpreterImplKind, kComputedGotoImplKind);
310 return ExecuteGotoImpl<true>(self, mh, code_item, shadow_frame, result_register);
311 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200312 }
313}
314
Brian Carlstromea46f952013-07-30 01:26:50 -0700315void EnterInterpreterFromInvoke(Thread* self, ArtMethod* method, Object* receiver,
Jeff Hao6474d192013-03-26 14:08:09 -0700316 uint32_t* args, JValue* result) {
Ian Rogers64b6d142012-10-29 16:34:15 -0700317 DCHECK_EQ(self, Thread::Current());
Jeff Hao790ad902013-05-22 15:02:08 -0700318 if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEnd())) {
jeffhaod7521322012-11-21 15:38:24 -0800319 ThrowStackOverflowError(self);
320 return;
321 }
322
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700323 const char* old_cause = self->StartAssertNoThreadSuspension("EnterInterpreterFromInvoke");
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700324 MethodHelper mh(method);
325 const DexFile::CodeItem* code_item = mh.GetCodeItem();
326 uint16_t num_regs;
327 uint16_t num_ins;
328 if (code_item != NULL) {
329 num_regs = code_item->registers_size_;
330 num_ins = code_item->ins_size_;
jeffhao0a9bb732012-11-26 12:28:49 -0800331 } else if (method->IsAbstract()) {
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700332 self->EndAssertNoThreadSuspension(old_cause);
Sebastien Hertz56adf602013-07-09 17:27:07 +0200333 ThrowAbstractMethodError(method);
jeffhao0a9bb732012-11-26 12:28:49 -0800334 return;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700335 } else {
336 DCHECK(method->IsNative());
Brian Carlstromea46f952013-07-30 01:26:50 -0700337 num_regs = num_ins = ArtMethod::NumArgRegisters(mh.GetShorty());
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700338 if (!method->IsStatic()) {
339 num_regs++;
340 num_ins++;
341 }
342 }
343 // Set up shadow frame with matching number of reference slots to vregs.
344 ShadowFrame* last_shadow_frame = self->GetManagedStack()->GetTopShadowFrame();
Jeff Hao66135192013-05-14 11:02:41 -0700345 void* memory = alloca(ShadowFrame::ComputeSize(num_regs));
346 ShadowFrame* shadow_frame(ShadowFrame::Create(num_regs, last_shadow_frame, method, 0, memory));
347 self->PushShadowFrame(shadow_frame);
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700348 self->EndAssertNoThreadSuspension(old_cause);
349
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700350 size_t cur_reg = num_regs - num_ins;
351 if (!method->IsStatic()) {
352 CHECK(receiver != NULL);
TDYa127ce4cc0d2012-11-18 16:59:53 -0800353 shadow_frame->SetVRegReference(cur_reg, receiver);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700354 ++cur_reg;
Sebastien Hertz807a2562013-04-15 09:33:39 +0200355 } else if (UNLIKELY(!method->GetDeclaringClass()->IsInitializing())) {
356 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800357 SirtRef<mirror::Class> sirt_c(self, method->GetDeclaringClass());
358 if (UNLIKELY(!class_linker->EnsureInitialized(sirt_c, true, true))) {
Sebastien Hertz807a2562013-04-15 09:33:39 +0200359 CHECK(self->IsExceptionPending());
360 self->PopShadowFrame();
jeffhao94d6df42012-11-26 16:02:12 -0800361 return;
362 }
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800363 CHECK(sirt_c->IsInitializing());
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700364 }
Ian Rogers64b6d142012-10-29 16:34:15 -0700365 const char* shorty = mh.GetShorty();
Jeff Hao5d917302013-02-27 17:57:33 -0800366 for (size_t shorty_pos = 0, arg_pos = 0; cur_reg < num_regs; ++shorty_pos, ++arg_pos, cur_reg++) {
367 DCHECK_LT(shorty_pos + 1, mh.GetShortyLength());
368 switch (shorty[shorty_pos + 1]) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700369 case 'L': {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800370 Object* o = reinterpret_cast<StackReference<Object>*>(&args[arg_pos])->AsMirrorPtr();
TDYa127ce4cc0d2012-11-18 16:59:53 -0800371 shadow_frame->SetVRegReference(cur_reg, o);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700372 break;
373 }
Jeff Hao5d917302013-02-27 17:57:33 -0800374 case 'J': case 'D': {
375 uint64_t wide_value = (static_cast<uint64_t>(args[arg_pos + 1]) << 32) | args[arg_pos];
376 shadow_frame->SetVRegLong(cur_reg, wide_value);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700377 cur_reg++;
Jeff Hao5d917302013-02-27 17:57:33 -0800378 arg_pos++;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700379 break;
Jeff Hao5d917302013-02-27 17:57:33 -0800380 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700381 default:
Jeff Hao5d917302013-02-27 17:57:33 -0800382 shadow_frame->SetVReg(cur_reg, args[arg_pos]);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700383 break;
384 }
385 }
Ian Rogers64b6d142012-10-29 16:34:15 -0700386 if (LIKELY(!method->IsNative())) {
Jeff Hao66135192013-05-14 11:02:41 -0700387 JValue r = Execute(self, mh, code_item, *shadow_frame, JValue());
Jeff Hao6474d192013-03-26 14:08:09 -0700388 if (result != NULL) {
389 *result = r;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700390 }
391 } else {
Ian Rogers64b6d142012-10-29 16:34:15 -0700392 // We don't expect to be asked to interpret native code (which is entered via a JNI compiler
393 // generated stub) except during testing and image writing.
394 if (!Runtime::Current()->IsStarted()) {
Jeff Hao6474d192013-03-26 14:08:09 -0700395 UnstartedRuntimeJni(self, method, receiver, args, result);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700396 } else {
Jeff Hao6474d192013-03-26 14:08:09 -0700397 InterpreterJni(self, method, shorty, receiver, args, result);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700398 }
399 }
400 self->PopShadowFrame();
401}
402
Ian Rogers62d6c772013-02-27 08:32:07 -0800403void EnterInterpreterFromDeoptimize(Thread* self, ShadowFrame* shadow_frame, JValue* ret_val)
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800404 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
405 JValue value;
Ian Rogers62d6c772013-02-27 08:32:07 -0800406 value.SetJ(ret_val->GetJ()); // Set value to last known result in case the shadow frame chain is empty.
407 MethodHelper mh;
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800408 while (shadow_frame != NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800409 self->SetTopOfShadowStack(shadow_frame);
410 mh.ChangeMethod(shadow_frame->GetMethod());
411 const DexFile::CodeItem* code_item = mh.GetCodeItem();
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800412 value = Execute(self, mh, code_item, *shadow_frame, value);
413 ShadowFrame* old_frame = shadow_frame;
414 shadow_frame = shadow_frame->GetLink();
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800415 delete old_frame;
416 }
417 ret_val->SetJ(value.GetJ());
418}
419
Ian Rogers7db619b2013-01-16 18:35:48 -0800420JValue EnterInterpreterFromStub(Thread* self, MethodHelper& mh, const DexFile::CodeItem* code_item,
Ian Rogers848871b2013-08-05 10:56:33 -0700421 ShadowFrame& shadow_frame) {
Ian Rogersf3e98552013-03-20 15:49:49 -0700422 DCHECK_EQ(self, Thread::Current());
Jeff Hao790ad902013-05-22 15:02:08 -0700423 if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEnd())) {
Ian Rogersf3e98552013-03-20 15:49:49 -0700424 ThrowStackOverflowError(self);
425 return JValue();
426 }
427
Ian Rogers7db619b2013-01-16 18:35:48 -0800428 return Execute(self, mh, code_item, shadow_frame, JValue());
429}
430
Ian Rogers848871b2013-08-05 10:56:33 -0700431extern "C" void artInterpreterToInterpreterBridge(Thread* self, MethodHelper& mh,
432 const DexFile::CodeItem* code_item,
433 ShadowFrame* shadow_frame, JValue* result) {
Jeff Hao790ad902013-05-22 15:02:08 -0700434 if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEnd())) {
Jeff Hao16743632013-05-08 10:59:04 -0700435 ThrowStackOverflowError(self);
Jeff Hao69510672013-05-21 17:34:55 -0700436 return;
Jeff Hao16743632013-05-08 10:59:04 -0700437 }
438
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700439 self->PushShadowFrame(shadow_frame);
Brian Carlstromea46f952013-07-30 01:26:50 -0700440 ArtMethod* method = shadow_frame->GetMethod();
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200441 // Ensure static methods are initialized.
442 if (method->IsStatic()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800443 SirtRef<Class> declaringClass(self, method->GetDeclaringClass());
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200444 if (UNLIKELY(!declaringClass->IsInitializing())) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700445 if (UNLIKELY(!Runtime::Current()->GetClassLinker()->EnsureInitialized(declaringClass, true,
446 true))) {
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200447 DCHECK(Thread::Current()->IsExceptionPending());
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700448 self->PopShadowFrame();
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200449 return;
450 }
451 CHECK(declaringClass->IsInitializing());
Jeff Hao16743632013-05-08 10:59:04 -0700452 }
Jeff Hao16743632013-05-08 10:59:04 -0700453 }
Jeff Hao16743632013-05-08 10:59:04 -0700454
Jeff Hao16743632013-05-08 10:59:04 -0700455 if (LIKELY(!method->IsNative())) {
Jeff Hao69510672013-05-21 17:34:55 -0700456 result->SetJ(Execute(self, mh, code_item, *shadow_frame, JValue()).GetJ());
Jeff Hao16743632013-05-08 10:59:04 -0700457 } else {
458 // We don't expect to be asked to interpret native code (which is entered via a JNI compiler
459 // generated stub) except during testing and image writing.
460 CHECK(!Runtime::Current()->IsStarted());
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700461 Object* receiver = method->IsStatic() ? nullptr : shadow_frame->GetVRegReference(0);
Jeff Hao16743632013-05-08 10:59:04 -0700462 uint32_t* args = shadow_frame->GetVRegArgs(method->IsStatic() ? 0 : 1);
Jeff Hao69510672013-05-21 17:34:55 -0700463 UnstartedRuntimeJni(self, method, receiver, args, result);
Jeff Hao16743632013-05-08 10:59:04 -0700464 }
465
466 self->PopShadowFrame();
Jeff Hao16743632013-05-08 10:59:04 -0700467}
468
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700469} // namespace interpreter
470} // namespace art