blob: 7232e5494416cba34461e3250b6b99a2d3237c62 [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 Rogersb0fa5dc2014-04-28 16:47:08 -070018
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010019#include <limits>
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080020
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070021#include "mirror/string-inl.h"
22
Ian Rogers2fa6b2e2012-10-17 00:10:17 -070023namespace art {
24namespace interpreter {
25
Ian Rogers64b6d142012-10-29 16:34:15 -070026// 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 -070027static void UnstartedRuntimeJni(Thread* self, ArtMethod* method,
Jeff Hao5d917302013-02-27 17:57:33 -080028 Object* receiver, uint32_t* args, JValue* result)
Ian Rogers64b6d142012-10-29 16:34:15 -070029 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
30 std::string name(PrettyMethod(method));
31 if (name == "java.lang.ClassLoader dalvik.system.VMStack.getCallingClassLoader()") {
32 result->SetL(NULL);
33 } else if (name == "java.lang.Class dalvik.system.VMStack.getStackClass2()") {
Ian Rogers7a22fa62013-01-23 12:16:16 -080034 NthCallerVisitor visitor(self, 3);
Ian Rogers64b6d142012-10-29 16:34:15 -070035 visitor.WalkStack();
36 result->SetL(visitor.caller->GetDeclaringClass());
37 } else if (name == "double java.lang.Math.log(double)") {
Jeff Hao5d917302013-02-27 17:57:33 -080038 JValue value;
39 value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]);
40 result->SetD(log(value.GetD()));
Ian Rogers64b6d142012-10-29 16:34:15 -070041 } else if (name == "java.lang.String java.lang.Class.getNameNative()") {
42 result->SetL(receiver->AsClass()->ComputeName());
43 } else if (name == "int java.lang.Float.floatToRawIntBits(float)") {
Jeff Hao5d917302013-02-27 17:57:33 -080044 result->SetI(args[0]);
Ian Rogers64b6d142012-10-29 16:34:15 -070045 } else if (name == "float java.lang.Float.intBitsToFloat(int)") {
Jeff Hao5d917302013-02-27 17:57:33 -080046 result->SetI(args[0]);
Ian Rogers64b6d142012-10-29 16:34:15 -070047 } else if (name == "double java.lang.Math.exp(double)") {
Jeff Hao5d917302013-02-27 17:57:33 -080048 JValue value;
49 value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]);
50 result->SetD(exp(value.GetD()));
Ian Rogers64b6d142012-10-29 16:34:15 -070051 } else if (name == "java.lang.Object java.lang.Object.internalClone()") {
52 result->SetL(receiver->Clone(self));
53 } else if (name == "void java.lang.Object.notifyAll()") {
Ian Rogers05f30572013-02-20 12:13:11 -080054 receiver->NotifyAll(self);
Ian Rogers64b6d142012-10-29 16:34:15 -070055 } else if (name == "int java.lang.String.compareTo(java.lang.String)") {
Jeff Hao5d917302013-02-27 17:57:33 -080056 String* rhs = reinterpret_cast<Object*>(args[0])->AsString();
Ian Rogers64b6d142012-10-29 16:34:15 -070057 CHECK(rhs != NULL);
58 result->SetI(receiver->AsString()->CompareTo(rhs));
59 } else if (name == "java.lang.String java.lang.String.intern()") {
60 result->SetL(receiver->AsString()->Intern());
61 } else if (name == "int java.lang.String.fastIndexOf(int, int)") {
Jeff Hao5d917302013-02-27 17:57:33 -080062 result->SetI(receiver->AsString()->FastIndexOf(args[0], args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -070063 } else if (name == "java.lang.Object java.lang.reflect.Array.createMultiArray(java.lang.Class, int[])") {
Mathieu Chartier5bb99032014-02-08 16:20:58 -080064 SirtRef<mirror::Class> sirt_class(self, reinterpret_cast<Object*>(args[0])->AsClass());
65 SirtRef<mirror::IntArray> sirt_dimensions(self,
66 reinterpret_cast<Object*>(args[1])->AsIntArray());
67 result->SetL(Array::CreateMultiArray(self, sirt_class, sirt_dimensions));
Ian Rogers64b6d142012-10-29 16:34:15 -070068 } else if (name == "java.lang.Object java.lang.Throwable.nativeFillInStackTrace()") {
69 ScopedObjectAccessUnchecked soa(self);
Ian Rogers5d27faf2014-05-02 17:17:18 -070070 if (Runtime::Current()->IsActiveTransaction()) {
71 result->SetL(soa.Decode<Object*>(self->CreateInternalStackTrace<true>(soa)));
72 } else {
73 result->SetL(soa.Decode<Object*>(self->CreateInternalStackTrace<false>(soa)));
74 }
Sebastien Hertzf48644b2014-02-17 15:16:03 +010075 } else if (name == "int java.lang.System.identityHashCode(java.lang.Object)") {
76 mirror::Object* obj = reinterpret_cast<Object*>(args[0]);
77 result->SetI((obj != nullptr) ? obj->IdentityHashCode() : 0);
Ian Rogers64b6d142012-10-29 16:34:15 -070078 } else if (name == "boolean java.nio.ByteOrder.isLittleEndian()") {
Sebastien Hertzf48644b2014-02-17 15:16:03 +010079 result->SetZ(JNI_TRUE);
Ian Rogers64b6d142012-10-29 16:34:15 -070080 } else if (name == "boolean sun.misc.Unsafe.compareAndSwapInt(java.lang.Object, long, int, int)") {
Jeff Hao5d917302013-02-27 17:57:33 -080081 Object* obj = reinterpret_cast<Object*>(args[0]);
82 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
83 jint expectedValue = args[3];
84 jint newValue = args[4];
Ian Rogers5d27faf2014-05-02 17:17:18 -070085 bool success;
86 if (Runtime::Current()->IsActiveTransaction()) {
87 success = obj->CasField32<true>(MemberOffset(offset), expectedValue, newValue);
88 } else {
89 success = obj->CasField32<false>(MemberOffset(offset), expectedValue, newValue);
90 }
Sebastien Hertzf48644b2014-02-17 15:16:03 +010091 result->SetZ(success ? JNI_TRUE : JNI_FALSE);
Ian Rogers64b6d142012-10-29 16:34:15 -070092 } else if (name == "void sun.misc.Unsafe.putObject(java.lang.Object, long, java.lang.Object)") {
Jeff Hao5d917302013-02-27 17:57:33 -080093 Object* obj = reinterpret_cast<Object*>(args[0]);
Sebastien Hertzf48644b2014-02-17 15:16:03 +010094 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
Jeff Hao5d917302013-02-27 17:57:33 -080095 Object* newValue = reinterpret_cast<Object*>(args[3]);
Ian Rogers5d27faf2014-05-02 17:17:18 -070096 if (Runtime::Current()->IsActiveTransaction()) {
97 obj->SetFieldObject<true>(MemberOffset(offset), newValue);
98 } else {
99 obj->SetFieldObject<false>(MemberOffset(offset), newValue);
100 }
Hiroshi Yamauchi4d2efce2014-02-10 16:19:09 -0800101 } else if (name == "int sun.misc.Unsafe.getArrayBaseOffsetForComponentType(java.lang.Class)") {
102 mirror::Class* component = reinterpret_cast<Object*>(args[0])->AsClass();
103 Primitive::Type primitive_type = component->GetPrimitiveType();
104 result->SetI(mirror::Array::DataOffset(Primitive::ComponentSize(primitive_type)).Int32Value());
105 } else if (name == "int sun.misc.Unsafe.getArrayIndexScaleForComponentType(java.lang.Class)") {
106 mirror::Class* component = reinterpret_cast<Object*>(args[0])->AsClass();
107 Primitive::Type primitive_type = component->GetPrimitiveType();
108 result->SetI(Primitive::ComponentSize(primitive_type));
Ian Rogers5d27faf2014-05-02 17:17:18 -0700109 } else if (Runtime::Current()->IsActiveTransaction()) {
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700110 AbortTransaction(self, "Attempt to invoke native method in non-started runtime: %s",
111 name.c_str());
Ian Rogers5d27faf2014-05-02 17:17:18 -0700112
113 } else {
114 LOG(FATAL) << "Calling native method " << PrettyMethod(method) << " in an unstarted "
115 "non-transactional runtime";
Ian Rogers64b6d142012-10-29 16:34:15 -0700116 }
117}
118
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700119static void InterpreterJni(Thread* self, ArtMethod* method, const StringPiece& shorty,
Jeff Hao5d917302013-02-27 17:57:33 -0800120 Object* receiver, uint32_t* args, JValue* result)
Ian Rogers64b6d142012-10-29 16:34:15 -0700121 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
122 // TODO: The following enters JNI code using a typedef-ed function rather than the JNI compiler,
123 // it should be removed and JNI compiled stubs used instead.
124 ScopedObjectAccessUnchecked soa(self);
125 if (method->IsStatic()) {
126 if (shorty == "L") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100127 typedef jobject (fntype)(JNIEnv*, jclass);
128 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700129 ScopedLocalRef<jclass> klass(soa.Env(),
130 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
Ian Rogers556d6372012-11-20 12:19:36 -0800131 jobject jresult;
132 {
133 ScopedThreadStateChange tsc(self, kNative);
134 jresult = fn(soa.Env(), klass.get());
135 }
136 result->SetL(soa.Decode<Object*>(jresult));
Ian Rogers64b6d142012-10-29 16:34:15 -0700137 } else if (shorty == "V") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100138 typedef void (fntype)(JNIEnv*, jclass);
139 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700140 ScopedLocalRef<jclass> klass(soa.Env(),
141 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
142 ScopedThreadStateChange tsc(self, kNative);
143 fn(soa.Env(), klass.get());
144 } else if (shorty == "Z") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100145 typedef jboolean (fntype)(JNIEnv*, jclass);
146 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700147 ScopedLocalRef<jclass> klass(soa.Env(),
148 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
149 ScopedThreadStateChange tsc(self, kNative);
150 result->SetZ(fn(soa.Env(), klass.get()));
151 } else if (shorty == "BI") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100152 typedef jbyte (fntype)(JNIEnv*, jclass, jint);
153 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700154 ScopedLocalRef<jclass> klass(soa.Env(),
155 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
156 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800157 result->SetB(fn(soa.Env(), klass.get(), args[0]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700158 } else if (shorty == "II") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100159 typedef jint (fntype)(JNIEnv*, jclass, jint);
160 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700161 ScopedLocalRef<jclass> klass(soa.Env(),
162 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
163 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800164 result->SetI(fn(soa.Env(), klass.get(), args[0]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700165 } else if (shorty == "LL") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100166 typedef jobject (fntype)(JNIEnv*, jclass, jobject);
167 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700168 ScopedLocalRef<jclass> klass(soa.Env(),
169 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
170 ScopedLocalRef<jobject> arg0(soa.Env(),
Jeff Hao5d917302013-02-27 17:57:33 -0800171 soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[0])));
Ian Rogers556d6372012-11-20 12:19:36 -0800172 jobject jresult;
173 {
174 ScopedThreadStateChange tsc(self, kNative);
175 jresult = fn(soa.Env(), klass.get(), arg0.get());
176 }
177 result->SetL(soa.Decode<Object*>(jresult));
Ian Rogers64b6d142012-10-29 16:34:15 -0700178 } else if (shorty == "IIZ") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100179 typedef jint (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 result->SetI(fn(soa.Env(), klass.get(), args[0], args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700185 } else if (shorty == "ILI") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100186 typedef jint (fntype)(JNIEnv*, jclass, jobject, jint);
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 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800193 result->SetI(fn(soa.Env(), klass.get(), arg0.get(), args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700194 } else if (shorty == "SIZ") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100195 typedef jshort (fntype)(JNIEnv*, jclass, jint, jboolean);
196 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700197 ScopedLocalRef<jclass> klass(soa.Env(),
198 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
199 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800200 result->SetS(fn(soa.Env(), klass.get(), args[0], args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700201 } else if (shorty == "VIZ") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100202 typedef void (fntype)(JNIEnv*, jclass, jint, jboolean);
203 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700204 ScopedLocalRef<jclass> klass(soa.Env(),
205 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
206 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800207 fn(soa.Env(), klass.get(), args[0], args[1]);
Ian Rogers64b6d142012-10-29 16:34:15 -0700208 } else if (shorty == "ZLL") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100209 typedef jboolean (fntype)(JNIEnv*, jclass, jobject, jobject);
210 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700211 ScopedLocalRef<jclass> klass(soa.Env(),
212 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
213 ScopedLocalRef<jobject> arg0(soa.Env(),
Jeff Hao5d917302013-02-27 17:57:33 -0800214 soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[0])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700215 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 ScopedThreadStateChange tsc(self, kNative);
218 result->SetZ(fn(soa.Env(), klass.get(), arg0.get(), arg1.get()));
219 } else if (shorty == "ZILL") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100220 typedef jboolean (fntype)(JNIEnv*, jclass, jint, jobject, jobject);
221 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700222 ScopedLocalRef<jclass> klass(soa.Env(),
223 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
224 ScopedLocalRef<jobject> arg1(soa.Env(),
Jeff Hao5d917302013-02-27 17:57:33 -0800225 soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[1])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700226 ScopedLocalRef<jobject> arg2(soa.Env(),
Jeff Hao5d917302013-02-27 17:57:33 -0800227 soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[2])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700228 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800229 result->SetZ(fn(soa.Env(), klass.get(), args[0], arg1.get(), arg2.get()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700230 } else if (shorty == "VILII") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100231 typedef void (fntype)(JNIEnv*, jclass, 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> arg1(soa.Env(),
Jeff Hao5d917302013-02-27 17:57:33 -0800236 soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[1])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700237 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800238 fn(soa.Env(), klass.get(), args[0], arg1.get(), args[2], args[3]);
Ian Rogers64b6d142012-10-29 16:34:15 -0700239 } else if (shorty == "VLILII") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100240 typedef void (fntype)(JNIEnv*, jclass, jobject, jint, jobject, jint, jint);
241 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700242 ScopedLocalRef<jclass> klass(soa.Env(),
243 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
244 ScopedLocalRef<jobject> arg0(soa.Env(),
Jeff Hao5d917302013-02-27 17:57:33 -0800245 soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[0])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700246 ScopedLocalRef<jobject> arg2(soa.Env(),
Jeff Hao5d917302013-02-27 17:57:33 -0800247 soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[2])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700248 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800249 fn(soa.Env(), klass.get(), arg0.get(), args[1], arg2.get(), args[3], args[4]);
Ian Rogers64b6d142012-10-29 16:34:15 -0700250 } else {
251 LOG(FATAL) << "Do something with static native method: " << PrettyMethod(method)
252 << " shorty: " << shorty;
253 }
254 } else {
255 if (shorty == "L") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100256 typedef jobject (fntype)(JNIEnv*, jobject);
257 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700258 ScopedLocalRef<jobject> rcvr(soa.Env(),
259 soa.AddLocalReference<jobject>(receiver));
Ian Rogers556d6372012-11-20 12:19:36 -0800260 jobject jresult;
261 {
262 ScopedThreadStateChange tsc(self, kNative);
263 jresult = fn(soa.Env(), rcvr.get());
264 }
265 result->SetL(soa.Decode<Object*>(jresult));
Jeff Hao3dd9f762013-07-08 13:09:25 -0700266 } else if (shorty == "V") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100267 typedef void (fntype)(JNIEnv*, jobject);
268 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Jeff Hao3dd9f762013-07-08 13:09:25 -0700269 ScopedLocalRef<jobject> rcvr(soa.Env(),
270 soa.AddLocalReference<jobject>(receiver));
271 ScopedThreadStateChange tsc(self, kNative);
272 fn(soa.Env(), rcvr.get());
Ian Rogers64b6d142012-10-29 16:34:15 -0700273 } else if (shorty == "LL") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100274 typedef jobject (fntype)(JNIEnv*, jobject, jobject);
275 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700276 ScopedLocalRef<jobject> rcvr(soa.Env(),
277 soa.AddLocalReference<jobject>(receiver));
278 ScopedLocalRef<jobject> arg0(soa.Env(),
Jeff Hao5d917302013-02-27 17:57:33 -0800279 soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[0])));
Ian Rogers556d6372012-11-20 12:19:36 -0800280 jobject jresult;
281 {
282 ScopedThreadStateChange tsc(self, kNative);
283 jresult = fn(soa.Env(), rcvr.get(), arg0.get());
Ian Rogers556d6372012-11-20 12:19:36 -0800284 }
285 result->SetL(soa.Decode<Object*>(jresult));
Ian Rogers64b6d142012-10-29 16:34:15 -0700286 ScopedThreadStateChange tsc(self, kNative);
Ian Rogers64b6d142012-10-29 16:34:15 -0700287 } else if (shorty == "III") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100288 typedef jint (fntype)(JNIEnv*, jobject, jint, jint);
289 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700290 ScopedLocalRef<jobject> rcvr(soa.Env(),
291 soa.AddLocalReference<jobject>(receiver));
292 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800293 result->SetI(fn(soa.Env(), rcvr.get(), args[0], args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700294 } else {
295 LOG(FATAL) << "Do something with native method: " << PrettyMethod(method)
296 << " shorty: " << shorty;
297 }
298 }
299}
300
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200301enum InterpreterImplKind {
Ian Rogersb48b9eb2014-02-28 16:20:21 -0800302 kSwitchImpl, // Switch-based interpreter implementation.
303 kComputedGotoImplKind // Computed-goto-based interpreter implementation.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200304};
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700305
Ian Rogersb48b9eb2014-02-28 16:20:21 -0800306#if !defined(__clang__)
307static constexpr InterpreterImplKind kInterpreterImplKind = kComputedGotoImplKind;
308#else
309// Clang 3.4 fails to build the goto interpreter implementation.
310static constexpr InterpreterImplKind kInterpreterImplKind = kSwitchImpl;
311template<bool do_access_check, bool transaction_active>
312JValue ExecuteGotoImpl(Thread* self, MethodHelper& mh, const DexFile::CodeItem* code_item,
313 ShadowFrame& shadow_frame, JValue result_register) {
314 LOG(FATAL) << "UNREACHABLE";
315 exit(0);
316}
317// Explicit definitions of ExecuteGotoImpl.
Stephen Hines861ea562014-04-23 16:03:57 -0700318template<> SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogersb48b9eb2014-02-28 16:20:21 -0800319JValue ExecuteGotoImpl<true, false>(Thread* self, MethodHelper& mh,
320 const DexFile::CodeItem* code_item,
321 ShadowFrame& shadow_frame, JValue result_register);
Stephen Hines861ea562014-04-23 16:03:57 -0700322template<> SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogersb48b9eb2014-02-28 16:20:21 -0800323JValue ExecuteGotoImpl<false, false>(Thread* self, MethodHelper& mh,
324 const DexFile::CodeItem* code_item,
325 ShadowFrame& shadow_frame, JValue result_register);
Stephen Hines861ea562014-04-23 16:03:57 -0700326template<> SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogersb48b9eb2014-02-28 16:20:21 -0800327JValue ExecuteGotoImpl<true, true>(Thread* self, MethodHelper& mh,
328 const DexFile::CodeItem* code_item,
329 ShadowFrame& shadow_frame, JValue result_register);
Stephen Hines861ea562014-04-23 16:03:57 -0700330template<> SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogersb48b9eb2014-02-28 16:20:21 -0800331JValue ExecuteGotoImpl<false, true>(Thread* self, MethodHelper& mh,
332 const DexFile::CodeItem* code_item,
333 ShadowFrame& shadow_frame, JValue result_register);
334#endif
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700335
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200336static JValue Execute(Thread* self, MethodHelper& mh, const DexFile::CodeItem* code_item,
337 ShadowFrame& shadow_frame, JValue result_register)
338 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
339
340static inline JValue Execute(Thread* self, MethodHelper& mh, const DexFile::CodeItem* code_item,
341 ShadowFrame& shadow_frame, JValue result_register) {
Ian Rogers848871b2013-08-05 10:56:33 -0700342 DCHECK(shadow_frame.GetMethod() == mh.GetMethod() ||
343 shadow_frame.GetMethod()->GetDeclaringClass()->IsProxyClass());
344 DCHECK(!shadow_frame.GetMethod()->IsAbstract());
345 DCHECK(!shadow_frame.GetMethod()->IsNative());
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200346
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100347 bool transaction_active = Runtime::Current()->IsActiveTransaction();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200348 if (LIKELY(shadow_frame.GetMethod()->IsPreverified())) {
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200349 // Enter the "without access check" interpreter.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200350 if (kInterpreterImplKind == kSwitchImpl) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100351 if (transaction_active) {
352 return ExecuteSwitchImpl<false, true>(self, mh, code_item, shadow_frame, result_register);
353 } else {
354 return ExecuteSwitchImpl<false, false>(self, mh, code_item, shadow_frame, result_register);
355 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200356 } else {
357 DCHECK_EQ(kInterpreterImplKind, kComputedGotoImplKind);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100358 if (transaction_active) {
359 return ExecuteGotoImpl<false, true>(self, mh, code_item, shadow_frame, result_register);
360 } else {
361 return ExecuteGotoImpl<false, false>(self, mh, code_item, shadow_frame, result_register);
362 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200363 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200364 } else {
365 // Enter the "with access check" interpreter.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200366 if (kInterpreterImplKind == kSwitchImpl) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100367 if (transaction_active) {
368 return ExecuteSwitchImpl<true, true>(self, mh, code_item, shadow_frame, result_register);
369 } else {
370 return ExecuteSwitchImpl<true, false>(self, mh, code_item, shadow_frame, result_register);
371 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200372 } else {
373 DCHECK_EQ(kInterpreterImplKind, kComputedGotoImplKind);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100374 if (transaction_active) {
375 return ExecuteGotoImpl<true, true>(self, mh, code_item, shadow_frame, result_register);
376 } else {
377 return ExecuteGotoImpl<true, false>(self, mh, code_item, shadow_frame, result_register);
378 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200379 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200380 }
381}
382
Brian Carlstromea46f952013-07-30 01:26:50 -0700383void EnterInterpreterFromInvoke(Thread* self, ArtMethod* method, Object* receiver,
Jeff Hao6474d192013-03-26 14:08:09 -0700384 uint32_t* args, JValue* result) {
Ian Rogers64b6d142012-10-29 16:34:15 -0700385 DCHECK_EQ(self, Thread::Current());
Jeff Hao790ad902013-05-22 15:02:08 -0700386 if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEnd())) {
jeffhaod7521322012-11-21 15:38:24 -0800387 ThrowStackOverflowError(self);
388 return;
389 }
390
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700391 const char* old_cause = self->StartAssertNoThreadSuspension("EnterInterpreterFromInvoke");
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700392 MethodHelper mh(method);
393 const DexFile::CodeItem* code_item = mh.GetCodeItem();
394 uint16_t num_regs;
395 uint16_t num_ins;
396 if (code_item != NULL) {
397 num_regs = code_item->registers_size_;
398 num_ins = code_item->ins_size_;
jeffhao0a9bb732012-11-26 12:28:49 -0800399 } else if (method->IsAbstract()) {
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700400 self->EndAssertNoThreadSuspension(old_cause);
Sebastien Hertz56adf602013-07-09 17:27:07 +0200401 ThrowAbstractMethodError(method);
jeffhao0a9bb732012-11-26 12:28:49 -0800402 return;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700403 } else {
404 DCHECK(method->IsNative());
Brian Carlstromea46f952013-07-30 01:26:50 -0700405 num_regs = num_ins = ArtMethod::NumArgRegisters(mh.GetShorty());
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700406 if (!method->IsStatic()) {
407 num_regs++;
408 num_ins++;
409 }
410 }
411 // Set up shadow frame with matching number of reference slots to vregs.
412 ShadowFrame* last_shadow_frame = self->GetManagedStack()->GetTopShadowFrame();
Jeff Hao66135192013-05-14 11:02:41 -0700413 void* memory = alloca(ShadowFrame::ComputeSize(num_regs));
414 ShadowFrame* shadow_frame(ShadowFrame::Create(num_regs, last_shadow_frame, method, 0, memory));
415 self->PushShadowFrame(shadow_frame);
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700416
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700417 size_t cur_reg = num_regs - num_ins;
418 if (!method->IsStatic()) {
419 CHECK(receiver != NULL);
TDYa127ce4cc0d2012-11-18 16:59:53 -0800420 shadow_frame->SetVRegReference(cur_reg, receiver);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700421 ++cur_reg;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700422 }
Ian Rogers64b6d142012-10-29 16:34:15 -0700423 const char* shorty = mh.GetShorty();
Jeff Hao5d917302013-02-27 17:57:33 -0800424 for (size_t shorty_pos = 0, arg_pos = 0; cur_reg < num_regs; ++shorty_pos, ++arg_pos, cur_reg++) {
425 DCHECK_LT(shorty_pos + 1, mh.GetShortyLength());
426 switch (shorty[shorty_pos + 1]) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700427 case 'L': {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800428 Object* o = reinterpret_cast<StackReference<Object>*>(&args[arg_pos])->AsMirrorPtr();
TDYa127ce4cc0d2012-11-18 16:59:53 -0800429 shadow_frame->SetVRegReference(cur_reg, o);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700430 break;
431 }
Jeff Hao5d917302013-02-27 17:57:33 -0800432 case 'J': case 'D': {
433 uint64_t wide_value = (static_cast<uint64_t>(args[arg_pos + 1]) << 32) | args[arg_pos];
434 shadow_frame->SetVRegLong(cur_reg, wide_value);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700435 cur_reg++;
Jeff Hao5d917302013-02-27 17:57:33 -0800436 arg_pos++;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700437 break;
Jeff Hao5d917302013-02-27 17:57:33 -0800438 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700439 default:
Jeff Hao5d917302013-02-27 17:57:33 -0800440 shadow_frame->SetVReg(cur_reg, args[arg_pos]);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700441 break;
442 }
443 }
Mathieu Chartier92246bb2014-02-25 18:22:39 -0800444 self->EndAssertNoThreadSuspension(old_cause);
445 // Do this after populating the shadow frame in case EnsureInitialized causes a GC.
446 if (method->IsStatic() && UNLIKELY(!method->GetDeclaringClass()->IsInitializing())) {
447 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
448 SirtRef<mirror::Class> sirt_c(self, method->GetDeclaringClass());
449 if (UNLIKELY(!class_linker->EnsureInitialized(sirt_c, true, true))) {
450 CHECK(self->IsExceptionPending());
451 self->PopShadowFrame();
452 return;
453 }
454 }
Ian Rogers64b6d142012-10-29 16:34:15 -0700455 if (LIKELY(!method->IsNative())) {
Jeff Hao66135192013-05-14 11:02:41 -0700456 JValue r = Execute(self, mh, code_item, *shadow_frame, JValue());
Jeff Hao6474d192013-03-26 14:08:09 -0700457 if (result != NULL) {
458 *result = r;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700459 }
460 } else {
Ian Rogers64b6d142012-10-29 16:34:15 -0700461 // We don't expect to be asked to interpret native code (which is entered via a JNI compiler
462 // generated stub) except during testing and image writing.
Mathieu Chartier92246bb2014-02-25 18:22:39 -0800463 // Update args to be the args in the shadow frame since the input ones could hold stale
464 // references pointers due to moving GC.
465 args = shadow_frame->GetVRegArgs(method->IsStatic() ? 0 : 1);
Ian Rogers64b6d142012-10-29 16:34:15 -0700466 if (!Runtime::Current()->IsStarted()) {
Jeff Hao6474d192013-03-26 14:08:09 -0700467 UnstartedRuntimeJni(self, method, receiver, args, result);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700468 } else {
Jeff Hao6474d192013-03-26 14:08:09 -0700469 InterpreterJni(self, method, shorty, receiver, args, result);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700470 }
471 }
472 self->PopShadowFrame();
473}
474
Ian Rogers62d6c772013-02-27 08:32:07 -0800475void EnterInterpreterFromDeoptimize(Thread* self, ShadowFrame* shadow_frame, JValue* ret_val)
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800476 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
477 JValue value;
Ian Rogers62d6c772013-02-27 08:32:07 -0800478 value.SetJ(ret_val->GetJ()); // Set value to last known result in case the shadow frame chain is empty.
479 MethodHelper mh;
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800480 while (shadow_frame != NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800481 self->SetTopOfShadowStack(shadow_frame);
482 mh.ChangeMethod(shadow_frame->GetMethod());
483 const DexFile::CodeItem* code_item = mh.GetCodeItem();
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800484 value = Execute(self, mh, code_item, *shadow_frame, value);
485 ShadowFrame* old_frame = shadow_frame;
486 shadow_frame = shadow_frame->GetLink();
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800487 delete old_frame;
488 }
489 ret_val->SetJ(value.GetJ());
490}
491
Ian Rogers7db619b2013-01-16 18:35:48 -0800492JValue EnterInterpreterFromStub(Thread* self, MethodHelper& mh, const DexFile::CodeItem* code_item,
Ian Rogers848871b2013-08-05 10:56:33 -0700493 ShadowFrame& shadow_frame) {
Ian Rogersf3e98552013-03-20 15:49:49 -0700494 DCHECK_EQ(self, Thread::Current());
Jeff Hao790ad902013-05-22 15:02:08 -0700495 if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEnd())) {
Ian Rogersf3e98552013-03-20 15:49:49 -0700496 ThrowStackOverflowError(self);
497 return JValue();
498 }
499
Ian Rogers7db619b2013-01-16 18:35:48 -0800500 return Execute(self, mh, code_item, shadow_frame, JValue());
501}
502
Ian Rogers848871b2013-08-05 10:56:33 -0700503extern "C" void artInterpreterToInterpreterBridge(Thread* self, MethodHelper& mh,
504 const DexFile::CodeItem* code_item,
505 ShadowFrame* shadow_frame, JValue* result) {
Jeff Hao790ad902013-05-22 15:02:08 -0700506 if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEnd())) {
Jeff Hao16743632013-05-08 10:59:04 -0700507 ThrowStackOverflowError(self);
Jeff Hao69510672013-05-21 17:34:55 -0700508 return;
Jeff Hao16743632013-05-08 10:59:04 -0700509 }
510
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700511 self->PushShadowFrame(shadow_frame);
Brian Carlstromea46f952013-07-30 01:26:50 -0700512 ArtMethod* method = shadow_frame->GetMethod();
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200513 // Ensure static methods are initialized.
514 if (method->IsStatic()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800515 SirtRef<Class> declaringClass(self, method->GetDeclaringClass());
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200516 if (UNLIKELY(!declaringClass->IsInitializing())) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700517 if (UNLIKELY(!Runtime::Current()->GetClassLinker()->EnsureInitialized(declaringClass, true,
518 true))) {
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200519 DCHECK(Thread::Current()->IsExceptionPending());
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700520 self->PopShadowFrame();
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200521 return;
522 }
523 CHECK(declaringClass->IsInitializing());
Jeff Hao16743632013-05-08 10:59:04 -0700524 }
Jeff Hao16743632013-05-08 10:59:04 -0700525 }
Jeff Hao16743632013-05-08 10:59:04 -0700526
Jeff Hao16743632013-05-08 10:59:04 -0700527 if (LIKELY(!method->IsNative())) {
Jeff Hao69510672013-05-21 17:34:55 -0700528 result->SetJ(Execute(self, mh, code_item, *shadow_frame, JValue()).GetJ());
Jeff Hao16743632013-05-08 10:59:04 -0700529 } else {
530 // We don't expect to be asked to interpret native code (which is entered via a JNI compiler
531 // generated stub) except during testing and image writing.
532 CHECK(!Runtime::Current()->IsStarted());
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700533 Object* receiver = method->IsStatic() ? nullptr : shadow_frame->GetVRegReference(0);
Jeff Hao16743632013-05-08 10:59:04 -0700534 uint32_t* args = shadow_frame->GetVRegArgs(method->IsStatic() ? 0 : 1);
Jeff Hao69510672013-05-21 17:34:55 -0700535 UnstartedRuntimeJni(self, method, receiver, args, result);
Jeff Hao16743632013-05-08 10:59:04 -0700536 }
537
538 self->PopShadowFrame();
Jeff Hao16743632013-05-08 10:59:04 -0700539}
540
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700541} // namespace interpreter
542} // namespace art