blob: a674571a4af38078e8a2b6c57623cb8959526482 [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"
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010018#include <limits>
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080019
Ian Rogers2fa6b2e2012-10-17 00:10:17 -070020namespace art {
21namespace interpreter {
22
Ian Rogers64b6d142012-10-29 16:34:15 -070023// 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 -070024static void UnstartedRuntimeJni(Thread* self, ArtMethod* method,
Jeff Hao5d917302013-02-27 17:57:33 -080025 Object* receiver, uint32_t* args, JValue* result)
Ian Rogers64b6d142012-10-29 16:34:15 -070026 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010027 DCHECK(Runtime::Current()->IsActiveTransaction()) << "Calling native method "
28 << PrettyMethod(method)
29 << " in unstarted runtime should only happen"
30 << " in a transaction";
Ian Rogers64b6d142012-10-29 16:34:15 -070031 std::string name(PrettyMethod(method));
32 if (name == "java.lang.ClassLoader dalvik.system.VMStack.getCallingClassLoader()") {
33 result->SetL(NULL);
34 } else if (name == "java.lang.Class dalvik.system.VMStack.getStackClass2()") {
Ian Rogers7a22fa62013-01-23 12:16:16 -080035 NthCallerVisitor visitor(self, 3);
Ian Rogers64b6d142012-10-29 16:34:15 -070036 visitor.WalkStack();
37 result->SetL(visitor.caller->GetDeclaringClass());
38 } else if (name == "double java.lang.Math.log(double)") {
Jeff Hao5d917302013-02-27 17:57:33 -080039 JValue value;
40 value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]);
41 result->SetD(log(value.GetD()));
Ian Rogers64b6d142012-10-29 16:34:15 -070042 } else if (name == "java.lang.String java.lang.Class.getNameNative()") {
43 result->SetL(receiver->AsClass()->ComputeName());
44 } else if (name == "int java.lang.Float.floatToRawIntBits(float)") {
Jeff Hao5d917302013-02-27 17:57:33 -080045 result->SetI(args[0]);
Ian Rogers64b6d142012-10-29 16:34:15 -070046 } else if (name == "float java.lang.Float.intBitsToFloat(int)") {
Jeff Hao5d917302013-02-27 17:57:33 -080047 result->SetI(args[0]);
Ian Rogers64b6d142012-10-29 16:34:15 -070048 } else if (name == "double java.lang.Math.exp(double)") {
Jeff Hao5d917302013-02-27 17:57:33 -080049 JValue value;
50 value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]);
51 result->SetD(exp(value.GetD()));
Ian Rogers64b6d142012-10-29 16:34:15 -070052 } else if (name == "java.lang.Object java.lang.Object.internalClone()") {
53 result->SetL(receiver->Clone(self));
54 } else if (name == "void java.lang.Object.notifyAll()") {
Ian Rogers05f30572013-02-20 12:13:11 -080055 receiver->NotifyAll(self);
Ian Rogers64b6d142012-10-29 16:34:15 -070056 } else if (name == "int java.lang.String.compareTo(java.lang.String)") {
Jeff Hao5d917302013-02-27 17:57:33 -080057 String* rhs = reinterpret_cast<Object*>(args[0])->AsString();
Ian Rogers64b6d142012-10-29 16:34:15 -070058 CHECK(rhs != NULL);
59 result->SetI(receiver->AsString()->CompareTo(rhs));
60 } else if (name == "java.lang.String java.lang.String.intern()") {
61 result->SetL(receiver->AsString()->Intern());
62 } else if (name == "int java.lang.String.fastIndexOf(int, int)") {
Jeff Hao5d917302013-02-27 17:57:33 -080063 result->SetI(receiver->AsString()->FastIndexOf(args[0], args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -070064 } else if (name == "java.lang.Object java.lang.reflect.Array.createMultiArray(java.lang.Class, int[])") {
Mathieu Chartier5bb99032014-02-08 16:20:58 -080065 SirtRef<mirror::Class> sirt_class(self, reinterpret_cast<Object*>(args[0])->AsClass());
66 SirtRef<mirror::IntArray> sirt_dimensions(self,
67 reinterpret_cast<Object*>(args[1])->AsIntArray());
68 result->SetL(Array::CreateMultiArray(self, sirt_class, sirt_dimensions));
Ian Rogers64b6d142012-10-29 16:34:15 -070069 } else if (name == "java.lang.Object java.lang.Throwable.nativeFillInStackTrace()") {
70 ScopedObjectAccessUnchecked soa(self);
71 result->SetL(soa.Decode<Object*>(self->CreateInternalStackTrace(soa)));
72 } else if (name == "boolean java.nio.ByteOrder.isLittleEndian()") {
73 result->SetJ(JNI_TRUE);
74 } else if (name == "boolean sun.misc.Unsafe.compareAndSwapInt(java.lang.Object, long, int, int)") {
Jeff Hao5d917302013-02-27 17:57:33 -080075 Object* obj = reinterpret_cast<Object*>(args[0]);
76 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
77 jint expectedValue = args[3];
78 jint newValue = args[4];
Ian Rogers64b6d142012-10-29 16:34:15 -070079 byte* raw_addr = reinterpret_cast<byte*>(obj) + offset;
80 volatile int32_t* address = reinterpret_cast<volatile int32_t*>(raw_addr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010081 // Check offset is 32bits to fit in MemberOffset.
82 CHECK_GE(offset, static_cast<jlong>(std::numeric_limits<int32_t>::min()));
83 CHECK_LE(offset, static_cast<jlong>(std::numeric_limits<int32_t>::max()));
84 Runtime::Current()->RecordWriteField32(obj, MemberOffset(offset), *address, true);
Ian Rogers64b6d142012-10-29 16:34:15 -070085 // Note: android_atomic_release_cas() returns 0 on success, not failure.
86 int r = android_atomic_release_cas(expectedValue, newValue, address);
87 result->SetZ(r == 0);
88 } else if (name == "void sun.misc.Unsafe.putObject(java.lang.Object, long, java.lang.Object)") {
Jeff Hao5d917302013-02-27 17:57:33 -080089 Object* obj = reinterpret_cast<Object*>(args[0]);
90 Object* newValue = reinterpret_cast<Object*>(args[3]);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010091 obj->SetFieldObject<true>(MemberOffset((static_cast<uint64_t>(args[2]) << 32) | args[1]),
92 newValue, false);
Hiroshi Yamauchi4d2efce2014-02-10 16:19:09 -080093 } else if (name == "int sun.misc.Unsafe.getArrayBaseOffsetForComponentType(java.lang.Class)") {
94 mirror::Class* component = reinterpret_cast<Object*>(args[0])->AsClass();
95 Primitive::Type primitive_type = component->GetPrimitiveType();
96 result->SetI(mirror::Array::DataOffset(Primitive::ComponentSize(primitive_type)).Int32Value());
97 } else if (name == "int sun.misc.Unsafe.getArrayIndexScaleForComponentType(java.lang.Class)") {
98 mirror::Class* component = reinterpret_cast<Object*>(args[0])->AsClass();
99 Primitive::Type primitive_type = component->GetPrimitiveType();
100 result->SetI(Primitive::ComponentSize(primitive_type));
Ian Rogers64b6d142012-10-29 16:34:15 -0700101 } else {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100102 // Throw an exception so we can abort the transaction and undo every change.
103 ThrowLocation throw_location;
104 self->ThrowNewExceptionF(throw_location, "Ljava/lang/InternalError;",
105 "Attempt to invoke native method in non-started runtime: %s",
106 name.c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -0700107 }
108}
109
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700110static void InterpreterJni(Thread* self, ArtMethod* method, const StringPiece& shorty,
Jeff Hao5d917302013-02-27 17:57:33 -0800111 Object* receiver, uint32_t* args, JValue* result)
Ian Rogers64b6d142012-10-29 16:34:15 -0700112 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
113 // TODO: The following enters JNI code using a typedef-ed function rather than the JNI compiler,
114 // it should be removed and JNI compiled stubs used instead.
115 ScopedObjectAccessUnchecked soa(self);
116 if (method->IsStatic()) {
117 if (shorty == "L") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100118 typedef jobject (fntype)(JNIEnv*, jclass);
119 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700120 ScopedLocalRef<jclass> klass(soa.Env(),
121 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
Ian Rogers556d6372012-11-20 12:19:36 -0800122 jobject jresult;
123 {
124 ScopedThreadStateChange tsc(self, kNative);
125 jresult = fn(soa.Env(), klass.get());
126 }
127 result->SetL(soa.Decode<Object*>(jresult));
Ian Rogers64b6d142012-10-29 16:34:15 -0700128 } else if (shorty == "V") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100129 typedef void (fntype)(JNIEnv*, jclass);
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);
134 fn(soa.Env(), klass.get());
135 } else if (shorty == "Z") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100136 typedef jboolean (fntype)(JNIEnv*, jclass);
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);
141 result->SetZ(fn(soa.Env(), klass.get()));
142 } else if (shorty == "BI") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100143 typedef jbyte (fntype)(JNIEnv*, jclass, jint);
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 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800148 result->SetB(fn(soa.Env(), klass.get(), args[0]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700149 } else if (shorty == "II") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100150 typedef jint (fntype)(JNIEnv*, jclass, jint);
151 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700152 ScopedLocalRef<jclass> klass(soa.Env(),
153 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
154 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800155 result->SetI(fn(soa.Env(), klass.get(), args[0]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700156 } else if (shorty == "LL") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100157 typedef jobject (fntype)(JNIEnv*, jclass, jobject);
158 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700159 ScopedLocalRef<jclass> klass(soa.Env(),
160 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
161 ScopedLocalRef<jobject> arg0(soa.Env(),
Jeff Hao5d917302013-02-27 17:57:33 -0800162 soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[0])));
Ian Rogers556d6372012-11-20 12:19:36 -0800163 jobject jresult;
164 {
165 ScopedThreadStateChange tsc(self, kNative);
166 jresult = fn(soa.Env(), klass.get(), arg0.get());
167 }
168 result->SetL(soa.Decode<Object*>(jresult));
Ian Rogers64b6d142012-10-29 16:34:15 -0700169 } else if (shorty == "IIZ") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100170 typedef jint (fntype)(JNIEnv*, jclass, jint, jboolean);
171 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700172 ScopedLocalRef<jclass> klass(soa.Env(),
173 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
174 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800175 result->SetI(fn(soa.Env(), klass.get(), args[0], args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700176 } else if (shorty == "ILI") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100177 typedef jint (fntype)(JNIEnv*, jclass, jobject, jint);
178 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700179 ScopedLocalRef<jclass> klass(soa.Env(),
180 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
181 ScopedLocalRef<jobject> arg0(soa.Env(),
Jeff Hao5d917302013-02-27 17:57:33 -0800182 soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[0])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700183 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800184 result->SetI(fn(soa.Env(), klass.get(), arg0.get(), args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700185 } else if (shorty == "SIZ") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100186 typedef jshort (fntype)(JNIEnv*, jclass, jint, jboolean);
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 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800191 result->SetS(fn(soa.Env(), klass.get(), args[0], args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700192 } else if (shorty == "VIZ") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100193 typedef void (fntype)(JNIEnv*, jclass, jint, jboolean);
194 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700195 ScopedLocalRef<jclass> klass(soa.Env(),
196 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
197 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800198 fn(soa.Env(), klass.get(), args[0], args[1]);
Ian Rogers64b6d142012-10-29 16:34:15 -0700199 } else if (shorty == "ZLL") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100200 typedef jboolean (fntype)(JNIEnv*, jclass, jobject, jobject);
201 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700202 ScopedLocalRef<jclass> klass(soa.Env(),
203 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
204 ScopedLocalRef<jobject> arg0(soa.Env(),
Jeff Hao5d917302013-02-27 17:57:33 -0800205 soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[0])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700206 ScopedLocalRef<jobject> arg1(soa.Env(),
Jeff Hao5d917302013-02-27 17:57:33 -0800207 soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[1])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700208 ScopedThreadStateChange tsc(self, kNative);
209 result->SetZ(fn(soa.Env(), klass.get(), arg0.get(), arg1.get()));
210 } else if (shorty == "ZILL") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100211 typedef jboolean (fntype)(JNIEnv*, jclass, jint, jobject, jobject);
212 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700213 ScopedLocalRef<jclass> klass(soa.Env(),
214 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
215 ScopedLocalRef<jobject> arg1(soa.Env(),
Jeff Hao5d917302013-02-27 17:57:33 -0800216 soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[1])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700217 ScopedLocalRef<jobject> arg2(soa.Env(),
Jeff Hao5d917302013-02-27 17:57:33 -0800218 soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[2])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700219 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800220 result->SetZ(fn(soa.Env(), klass.get(), args[0], arg1.get(), arg2.get()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700221 } else if (shorty == "VILII") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100222 typedef void (fntype)(JNIEnv*, jclass, jint, jobject, jint, jint);
223 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700224 ScopedLocalRef<jclass> klass(soa.Env(),
225 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
226 ScopedLocalRef<jobject> arg1(soa.Env(),
Jeff Hao5d917302013-02-27 17:57:33 -0800227 soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[1])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700228 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800229 fn(soa.Env(), klass.get(), args[0], arg1.get(), args[2], args[3]);
Ian Rogers64b6d142012-10-29 16:34:15 -0700230 } else if (shorty == "VLILII") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100231 typedef void (fntype)(JNIEnv*, jclass, jobject, jint, jobject, jint, jint);
232 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700233 ScopedLocalRef<jclass> klass(soa.Env(),
234 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
235 ScopedLocalRef<jobject> arg0(soa.Env(),
Jeff Hao5d917302013-02-27 17:57:33 -0800236 soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[0])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700237 ScopedLocalRef<jobject> arg2(soa.Env(),
Jeff Hao5d917302013-02-27 17:57:33 -0800238 soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[2])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700239 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800240 fn(soa.Env(), klass.get(), arg0.get(), args[1], arg2.get(), args[3], args[4]);
Ian Rogers64b6d142012-10-29 16:34:15 -0700241 } else {
242 LOG(FATAL) << "Do something with static native method: " << PrettyMethod(method)
243 << " shorty: " << shorty;
244 }
245 } else {
246 if (shorty == "L") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100247 typedef jobject (fntype)(JNIEnv*, jobject);
248 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700249 ScopedLocalRef<jobject> rcvr(soa.Env(),
250 soa.AddLocalReference<jobject>(receiver));
Ian Rogers556d6372012-11-20 12:19:36 -0800251 jobject jresult;
252 {
253 ScopedThreadStateChange tsc(self, kNative);
254 jresult = fn(soa.Env(), rcvr.get());
255 }
256 result->SetL(soa.Decode<Object*>(jresult));
Jeff Hao3dd9f762013-07-08 13:09:25 -0700257 } else if (shorty == "V") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100258 typedef void (fntype)(JNIEnv*, jobject);
259 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Jeff Hao3dd9f762013-07-08 13:09:25 -0700260 ScopedLocalRef<jobject> rcvr(soa.Env(),
261 soa.AddLocalReference<jobject>(receiver));
262 ScopedThreadStateChange tsc(self, kNative);
263 fn(soa.Env(), rcvr.get());
Ian Rogers64b6d142012-10-29 16:34:15 -0700264 } else if (shorty == "LL") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100265 typedef jobject (fntype)(JNIEnv*, jobject, jobject);
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 ScopedLocalRef<jobject> arg0(soa.Env(),
Jeff Hao5d917302013-02-27 17:57:33 -0800270 soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[0])));
Ian Rogers556d6372012-11-20 12:19:36 -0800271 jobject jresult;
272 {
273 ScopedThreadStateChange tsc(self, kNative);
274 jresult = fn(soa.Env(), rcvr.get(), arg0.get());
Ian Rogers556d6372012-11-20 12:19:36 -0800275 }
276 result->SetL(soa.Decode<Object*>(jresult));
Ian Rogers64b6d142012-10-29 16:34:15 -0700277 ScopedThreadStateChange tsc(self, kNative);
Ian Rogers64b6d142012-10-29 16:34:15 -0700278 } else if (shorty == "III") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100279 typedef jint (fntype)(JNIEnv*, jobject, jint, jint);
280 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700281 ScopedLocalRef<jobject> rcvr(soa.Env(),
282 soa.AddLocalReference<jobject>(receiver));
283 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800284 result->SetI(fn(soa.Env(), rcvr.get(), args[0], args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700285 } else {
286 LOG(FATAL) << "Do something with native method: " << PrettyMethod(method)
287 << " shorty: " << shorty;
288 }
289 }
290}
291
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200292enum InterpreterImplKind {
293 kSwitchImpl, // switch-based interpreter implementation.
294 kComputedGotoImplKind // computed-goto-based interpreter implementation.
295};
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700296
Sebastien Hertz2c88b382013-10-18 10:35:39 +0200297static const InterpreterImplKind kInterpreterImplKind = kComputedGotoImplKind;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700298
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200299static JValue Execute(Thread* self, MethodHelper& mh, const DexFile::CodeItem* code_item,
300 ShadowFrame& shadow_frame, JValue result_register)
301 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
302
303static inline JValue Execute(Thread* self, MethodHelper& mh, const DexFile::CodeItem* code_item,
304 ShadowFrame& shadow_frame, JValue result_register) {
Ian Rogers848871b2013-08-05 10:56:33 -0700305 DCHECK(shadow_frame.GetMethod() == mh.GetMethod() ||
306 shadow_frame.GetMethod()->GetDeclaringClass()->IsProxyClass());
307 DCHECK(!shadow_frame.GetMethod()->IsAbstract());
308 DCHECK(!shadow_frame.GetMethod()->IsNative());
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200309
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100310 bool transaction_active = Runtime::Current()->IsActiveTransaction();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200311 if (LIKELY(shadow_frame.GetMethod()->IsPreverified())) {
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200312 // Enter the "without access check" interpreter.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200313 if (kInterpreterImplKind == kSwitchImpl) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100314 if (transaction_active) {
315 return ExecuteSwitchImpl<false, true>(self, mh, code_item, shadow_frame, result_register);
316 } else {
317 return ExecuteSwitchImpl<false, false>(self, mh, code_item, shadow_frame, result_register);
318 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200319 } else {
320 DCHECK_EQ(kInterpreterImplKind, kComputedGotoImplKind);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100321 if (transaction_active) {
322 return ExecuteGotoImpl<false, true>(self, mh, code_item, shadow_frame, result_register);
323 } else {
324 return ExecuteGotoImpl<false, false>(self, mh, code_item, shadow_frame, result_register);
325 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200326 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200327 } else {
328 // Enter the "with access check" interpreter.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200329 if (kInterpreterImplKind == kSwitchImpl) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100330 if (transaction_active) {
331 return ExecuteSwitchImpl<true, true>(self, mh, code_item, shadow_frame, result_register);
332 } else {
333 return ExecuteSwitchImpl<true, false>(self, mh, code_item, shadow_frame, result_register);
334 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200335 } else {
336 DCHECK_EQ(kInterpreterImplKind, kComputedGotoImplKind);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100337 if (transaction_active) {
338 return ExecuteGotoImpl<true, true>(self, mh, code_item, shadow_frame, result_register);
339 } else {
340 return ExecuteGotoImpl<true, false>(self, mh, code_item, shadow_frame, result_register);
341 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200342 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200343 }
344}
345
Brian Carlstromea46f952013-07-30 01:26:50 -0700346void EnterInterpreterFromInvoke(Thread* self, ArtMethod* method, Object* receiver,
Jeff Hao6474d192013-03-26 14:08:09 -0700347 uint32_t* args, JValue* result) {
Ian Rogers64b6d142012-10-29 16:34:15 -0700348 DCHECK_EQ(self, Thread::Current());
Jeff Hao790ad902013-05-22 15:02:08 -0700349 if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEnd())) {
jeffhaod7521322012-11-21 15:38:24 -0800350 ThrowStackOverflowError(self);
351 return;
352 }
353
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700354 const char* old_cause = self->StartAssertNoThreadSuspension("EnterInterpreterFromInvoke");
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700355 MethodHelper mh(method);
356 const DexFile::CodeItem* code_item = mh.GetCodeItem();
357 uint16_t num_regs;
358 uint16_t num_ins;
359 if (code_item != NULL) {
360 num_regs = code_item->registers_size_;
361 num_ins = code_item->ins_size_;
jeffhao0a9bb732012-11-26 12:28:49 -0800362 } else if (method->IsAbstract()) {
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700363 self->EndAssertNoThreadSuspension(old_cause);
Sebastien Hertz56adf602013-07-09 17:27:07 +0200364 ThrowAbstractMethodError(method);
jeffhao0a9bb732012-11-26 12:28:49 -0800365 return;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700366 } else {
367 DCHECK(method->IsNative());
Brian Carlstromea46f952013-07-30 01:26:50 -0700368 num_regs = num_ins = ArtMethod::NumArgRegisters(mh.GetShorty());
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700369 if (!method->IsStatic()) {
370 num_regs++;
371 num_ins++;
372 }
373 }
374 // Set up shadow frame with matching number of reference slots to vregs.
375 ShadowFrame* last_shadow_frame = self->GetManagedStack()->GetTopShadowFrame();
Jeff Hao66135192013-05-14 11:02:41 -0700376 void* memory = alloca(ShadowFrame::ComputeSize(num_regs));
377 ShadowFrame* shadow_frame(ShadowFrame::Create(num_regs, last_shadow_frame, method, 0, memory));
378 self->PushShadowFrame(shadow_frame);
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700379 self->EndAssertNoThreadSuspension(old_cause);
380
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700381 size_t cur_reg = num_regs - num_ins;
382 if (!method->IsStatic()) {
383 CHECK(receiver != NULL);
TDYa127ce4cc0d2012-11-18 16:59:53 -0800384 shadow_frame->SetVRegReference(cur_reg, receiver);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700385 ++cur_reg;
Sebastien Hertz807a2562013-04-15 09:33:39 +0200386 } else if (UNLIKELY(!method->GetDeclaringClass()->IsInitializing())) {
387 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800388 SirtRef<mirror::Class> sirt_c(self, method->GetDeclaringClass());
389 if (UNLIKELY(!class_linker->EnsureInitialized(sirt_c, true, true))) {
Sebastien Hertz807a2562013-04-15 09:33:39 +0200390 CHECK(self->IsExceptionPending());
391 self->PopShadowFrame();
jeffhao94d6df42012-11-26 16:02:12 -0800392 return;
393 }
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800394 CHECK(sirt_c->IsInitializing());
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700395 }
Ian Rogers64b6d142012-10-29 16:34:15 -0700396 const char* shorty = mh.GetShorty();
Jeff Hao5d917302013-02-27 17:57:33 -0800397 for (size_t shorty_pos = 0, arg_pos = 0; cur_reg < num_regs; ++shorty_pos, ++arg_pos, cur_reg++) {
398 DCHECK_LT(shorty_pos + 1, mh.GetShortyLength());
399 switch (shorty[shorty_pos + 1]) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700400 case 'L': {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800401 Object* o = reinterpret_cast<StackReference<Object>*>(&args[arg_pos])->AsMirrorPtr();
TDYa127ce4cc0d2012-11-18 16:59:53 -0800402 shadow_frame->SetVRegReference(cur_reg, o);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700403 break;
404 }
Jeff Hao5d917302013-02-27 17:57:33 -0800405 case 'J': case 'D': {
406 uint64_t wide_value = (static_cast<uint64_t>(args[arg_pos + 1]) << 32) | args[arg_pos];
407 shadow_frame->SetVRegLong(cur_reg, wide_value);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700408 cur_reg++;
Jeff Hao5d917302013-02-27 17:57:33 -0800409 arg_pos++;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700410 break;
Jeff Hao5d917302013-02-27 17:57:33 -0800411 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700412 default:
Jeff Hao5d917302013-02-27 17:57:33 -0800413 shadow_frame->SetVReg(cur_reg, args[arg_pos]);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700414 break;
415 }
416 }
Ian Rogers64b6d142012-10-29 16:34:15 -0700417 if (LIKELY(!method->IsNative())) {
Jeff Hao66135192013-05-14 11:02:41 -0700418 JValue r = Execute(self, mh, code_item, *shadow_frame, JValue());
Jeff Hao6474d192013-03-26 14:08:09 -0700419 if (result != NULL) {
420 *result = r;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700421 }
422 } else {
Ian Rogers64b6d142012-10-29 16:34:15 -0700423 // We don't expect to be asked to interpret native code (which is entered via a JNI compiler
424 // generated stub) except during testing and image writing.
425 if (!Runtime::Current()->IsStarted()) {
Jeff Hao6474d192013-03-26 14:08:09 -0700426 UnstartedRuntimeJni(self, method, receiver, args, result);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700427 } else {
Jeff Hao6474d192013-03-26 14:08:09 -0700428 InterpreterJni(self, method, shorty, receiver, args, result);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700429 }
430 }
431 self->PopShadowFrame();
432}
433
Ian Rogers62d6c772013-02-27 08:32:07 -0800434void EnterInterpreterFromDeoptimize(Thread* self, ShadowFrame* shadow_frame, JValue* ret_val)
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800435 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
436 JValue value;
Ian Rogers62d6c772013-02-27 08:32:07 -0800437 value.SetJ(ret_val->GetJ()); // Set value to last known result in case the shadow frame chain is empty.
438 MethodHelper mh;
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800439 while (shadow_frame != NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800440 self->SetTopOfShadowStack(shadow_frame);
441 mh.ChangeMethod(shadow_frame->GetMethod());
442 const DexFile::CodeItem* code_item = mh.GetCodeItem();
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800443 value = Execute(self, mh, code_item, *shadow_frame, value);
444 ShadowFrame* old_frame = shadow_frame;
445 shadow_frame = shadow_frame->GetLink();
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800446 delete old_frame;
447 }
448 ret_val->SetJ(value.GetJ());
449}
450
Ian Rogers7db619b2013-01-16 18:35:48 -0800451JValue EnterInterpreterFromStub(Thread* self, MethodHelper& mh, const DexFile::CodeItem* code_item,
Ian Rogers848871b2013-08-05 10:56:33 -0700452 ShadowFrame& shadow_frame) {
Ian Rogersf3e98552013-03-20 15:49:49 -0700453 DCHECK_EQ(self, Thread::Current());
Jeff Hao790ad902013-05-22 15:02:08 -0700454 if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEnd())) {
Ian Rogersf3e98552013-03-20 15:49:49 -0700455 ThrowStackOverflowError(self);
456 return JValue();
457 }
458
Ian Rogers7db619b2013-01-16 18:35:48 -0800459 return Execute(self, mh, code_item, shadow_frame, JValue());
460}
461
Ian Rogers848871b2013-08-05 10:56:33 -0700462extern "C" void artInterpreterToInterpreterBridge(Thread* self, MethodHelper& mh,
463 const DexFile::CodeItem* code_item,
464 ShadowFrame* shadow_frame, JValue* result) {
Jeff Hao790ad902013-05-22 15:02:08 -0700465 if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEnd())) {
Jeff Hao16743632013-05-08 10:59:04 -0700466 ThrowStackOverflowError(self);
Jeff Hao69510672013-05-21 17:34:55 -0700467 return;
Jeff Hao16743632013-05-08 10:59:04 -0700468 }
469
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700470 self->PushShadowFrame(shadow_frame);
Brian Carlstromea46f952013-07-30 01:26:50 -0700471 ArtMethod* method = shadow_frame->GetMethod();
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200472 // Ensure static methods are initialized.
473 if (method->IsStatic()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800474 SirtRef<Class> declaringClass(self, method->GetDeclaringClass());
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200475 if (UNLIKELY(!declaringClass->IsInitializing())) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700476 if (UNLIKELY(!Runtime::Current()->GetClassLinker()->EnsureInitialized(declaringClass, true,
477 true))) {
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200478 DCHECK(Thread::Current()->IsExceptionPending());
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700479 self->PopShadowFrame();
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200480 return;
481 }
482 CHECK(declaringClass->IsInitializing());
Jeff Hao16743632013-05-08 10:59:04 -0700483 }
Jeff Hao16743632013-05-08 10:59:04 -0700484 }
Jeff Hao16743632013-05-08 10:59:04 -0700485
Jeff Hao16743632013-05-08 10:59:04 -0700486 if (LIKELY(!method->IsNative())) {
Jeff Hao69510672013-05-21 17:34:55 -0700487 result->SetJ(Execute(self, mh, code_item, *shadow_frame, JValue()).GetJ());
Jeff Hao16743632013-05-08 10:59:04 -0700488 } else {
489 // We don't expect to be asked to interpret native code (which is entered via a JNI compiler
490 // generated stub) except during testing and image writing.
491 CHECK(!Runtime::Current()->IsStarted());
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700492 Object* receiver = method->IsStatic() ? nullptr : shadow_frame->GetVRegReference(0);
Jeff Hao16743632013-05-08 10:59:04 -0700493 uint32_t* args = shadow_frame->GetVRegArgs(method->IsStatic() ? 0 : 1);
Jeff Hao69510672013-05-21 17:34:55 -0700494 UnstartedRuntimeJni(self, method, receiver, args, result);
Jeff Hao16743632013-05-08 10:59:04 -0700495 }
496
497 self->PopShadowFrame();
Jeff Hao16743632013-05-08 10:59:04 -0700498}
499
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700500} // namespace interpreter
501} // namespace art