blob: 3c6c225c658330ee72579db9396097ea3b2b9151 [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_) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010030 DCHECK(Runtime::Current()->IsActiveTransaction()) << "Calling native method "
31 << PrettyMethod(method)
32 << " in unstarted runtime should only happen"
33 << " in a transaction";
Ian Rogers64b6d142012-10-29 16:34:15 -070034 std::string name(PrettyMethod(method));
35 if (name == "java.lang.ClassLoader dalvik.system.VMStack.getCallingClassLoader()") {
36 result->SetL(NULL);
37 } else if (name == "java.lang.Class dalvik.system.VMStack.getStackClass2()") {
Ian Rogers7a22fa62013-01-23 12:16:16 -080038 NthCallerVisitor visitor(self, 3);
Ian Rogers64b6d142012-10-29 16:34:15 -070039 visitor.WalkStack();
40 result->SetL(visitor.caller->GetDeclaringClass());
41 } else if (name == "double java.lang.Math.log(double)") {
Jeff Hao5d917302013-02-27 17:57:33 -080042 JValue value;
43 value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]);
44 result->SetD(log(value.GetD()));
Ian Rogers64b6d142012-10-29 16:34:15 -070045 } else if (name == "java.lang.String java.lang.Class.getNameNative()") {
46 result->SetL(receiver->AsClass()->ComputeName());
47 } else if (name == "int java.lang.Float.floatToRawIntBits(float)") {
Jeff Hao5d917302013-02-27 17:57:33 -080048 result->SetI(args[0]);
Ian Rogers64b6d142012-10-29 16:34:15 -070049 } else if (name == "float java.lang.Float.intBitsToFloat(int)") {
Jeff Hao5d917302013-02-27 17:57:33 -080050 result->SetI(args[0]);
Ian Rogers64b6d142012-10-29 16:34:15 -070051 } else if (name == "double java.lang.Math.exp(double)") {
Jeff Hao5d917302013-02-27 17:57:33 -080052 JValue value;
53 value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]);
54 result->SetD(exp(value.GetD()));
Ian Rogers64b6d142012-10-29 16:34:15 -070055 } else if (name == "java.lang.Object java.lang.Object.internalClone()") {
56 result->SetL(receiver->Clone(self));
57 } else if (name == "void java.lang.Object.notifyAll()") {
Ian Rogers05f30572013-02-20 12:13:11 -080058 receiver->NotifyAll(self);
Ian Rogers64b6d142012-10-29 16:34:15 -070059 } else if (name == "int java.lang.String.compareTo(java.lang.String)") {
Jeff Hao5d917302013-02-27 17:57:33 -080060 String* rhs = reinterpret_cast<Object*>(args[0])->AsString();
Ian Rogers64b6d142012-10-29 16:34:15 -070061 CHECK(rhs != NULL);
62 result->SetI(receiver->AsString()->CompareTo(rhs));
63 } else if (name == "java.lang.String java.lang.String.intern()") {
64 result->SetL(receiver->AsString()->Intern());
65 } else if (name == "int java.lang.String.fastIndexOf(int, int)") {
Jeff Hao5d917302013-02-27 17:57:33 -080066 result->SetI(receiver->AsString()->FastIndexOf(args[0], args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -070067 } else if (name == "java.lang.Object java.lang.reflect.Array.createMultiArray(java.lang.Class, int[])") {
Mathieu Chartier5bb99032014-02-08 16:20:58 -080068 SirtRef<mirror::Class> sirt_class(self, reinterpret_cast<Object*>(args[0])->AsClass());
69 SirtRef<mirror::IntArray> sirt_dimensions(self,
70 reinterpret_cast<Object*>(args[1])->AsIntArray());
71 result->SetL(Array::CreateMultiArray(self, sirt_class, sirt_dimensions));
Ian Rogers64b6d142012-10-29 16:34:15 -070072 } else if (name == "java.lang.Object java.lang.Throwable.nativeFillInStackTrace()") {
73 ScopedObjectAccessUnchecked soa(self);
Sebastien Hertzee1d79a2014-02-21 15:46:30 +010074 result->SetL(soa.Decode<Object*>(self->CreateInternalStackTrace<true>(soa)));
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];
Sebastien Hertzf48644b2014-02-17 15:16:03 +010085 bool success = obj->CasField32<true>(MemberOffset(offset), expectedValue, newValue);
86 result->SetZ(success ? JNI_TRUE : JNI_FALSE);
Ian Rogers64b6d142012-10-29 16:34:15 -070087 } else if (name == "void sun.misc.Unsafe.putObject(java.lang.Object, long, java.lang.Object)") {
Jeff Hao5d917302013-02-27 17:57:33 -080088 Object* obj = reinterpret_cast<Object*>(args[0]);
Sebastien Hertzf48644b2014-02-17 15:16:03 +010089 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
Jeff Hao5d917302013-02-27 17:57:33 -080090 Object* newValue = reinterpret_cast<Object*>(args[3]);
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070091 obj->SetFieldObject<true>(MemberOffset(offset), newValue);
Hiroshi Yamauchi4d2efce2014-02-10 16:19:09 -080092 } else if (name == "int sun.misc.Unsafe.getArrayBaseOffsetForComponentType(java.lang.Class)") {
93 mirror::Class* component = reinterpret_cast<Object*>(args[0])->AsClass();
94 Primitive::Type primitive_type = component->GetPrimitiveType();
95 result->SetI(mirror::Array::DataOffset(Primitive::ComponentSize(primitive_type)).Int32Value());
96 } else if (name == "int sun.misc.Unsafe.getArrayIndexScaleForComponentType(java.lang.Class)") {
97 mirror::Class* component = reinterpret_cast<Object*>(args[0])->AsClass();
98 Primitive::Type primitive_type = component->GetPrimitiveType();
99 result->SetI(Primitive::ComponentSize(primitive_type));
Ian Rogers64b6d142012-10-29 16:34:15 -0700100 } else {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100101 // Throw an exception so we can abort the transaction and undo every change.
Sebastien Hertzee1d79a2014-02-21 15:46:30 +0100102 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100103 self->ThrowNewExceptionF(throw_location, "Ljava/lang/InternalError;",
104 "Attempt to invoke native method in non-started runtime: %s",
105 name.c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -0700106 }
107}
108
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700109static void InterpreterJni(Thread* self, ArtMethod* method, const StringPiece& shorty,
Jeff Hao5d917302013-02-27 17:57:33 -0800110 Object* receiver, uint32_t* args, JValue* result)
Ian Rogers64b6d142012-10-29 16:34:15 -0700111 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
112 // TODO: The following enters JNI code using a typedef-ed function rather than the JNI compiler,
113 // it should be removed and JNI compiled stubs used instead.
114 ScopedObjectAccessUnchecked soa(self);
115 if (method->IsStatic()) {
116 if (shorty == "L") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100117 typedef jobject (fntype)(JNIEnv*, jclass);
118 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700119 ScopedLocalRef<jclass> klass(soa.Env(),
120 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
Ian Rogers556d6372012-11-20 12:19:36 -0800121 jobject jresult;
122 {
123 ScopedThreadStateChange tsc(self, kNative);
124 jresult = fn(soa.Env(), klass.get());
125 }
126 result->SetL(soa.Decode<Object*>(jresult));
Ian Rogers64b6d142012-10-29 16:34:15 -0700127 } else if (shorty == "V") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100128 typedef void (fntype)(JNIEnv*, jclass);
129 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700130 ScopedLocalRef<jclass> klass(soa.Env(),
131 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
132 ScopedThreadStateChange tsc(self, kNative);
133 fn(soa.Env(), klass.get());
134 } else if (shorty == "Z") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100135 typedef jboolean (fntype)(JNIEnv*, jclass);
136 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700137 ScopedLocalRef<jclass> klass(soa.Env(),
138 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
139 ScopedThreadStateChange tsc(self, kNative);
140 result->SetZ(fn(soa.Env(), klass.get()));
141 } else if (shorty == "BI") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100142 typedef jbyte (fntype)(JNIEnv*, jclass, jint);
143 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700144 ScopedLocalRef<jclass> klass(soa.Env(),
145 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
146 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800147 result->SetB(fn(soa.Env(), klass.get(), args[0]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700148 } else if (shorty == "II") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100149 typedef jint (fntype)(JNIEnv*, jclass, jint);
150 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700151 ScopedLocalRef<jclass> klass(soa.Env(),
152 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
153 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800154 result->SetI(fn(soa.Env(), klass.get(), args[0]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700155 } else if (shorty == "LL") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100156 typedef jobject (fntype)(JNIEnv*, jclass, jobject);
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 ScopedLocalRef<jobject> arg0(soa.Env(),
Jeff Hao5d917302013-02-27 17:57:33 -0800161 soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[0])));
Ian Rogers556d6372012-11-20 12:19:36 -0800162 jobject jresult;
163 {
164 ScopedThreadStateChange tsc(self, kNative);
165 jresult = fn(soa.Env(), klass.get(), arg0.get());
166 }
167 result->SetL(soa.Decode<Object*>(jresult));
Ian Rogers64b6d142012-10-29 16:34:15 -0700168 } else if (shorty == "IIZ") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100169 typedef jint (fntype)(JNIEnv*, jclass, jint, jboolean);
170 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700171 ScopedLocalRef<jclass> klass(soa.Env(),
172 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
173 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800174 result->SetI(fn(soa.Env(), klass.get(), args[0], args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700175 } else if (shorty == "ILI") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100176 typedef jint (fntype)(JNIEnv*, jclass, jobject, jint);
177 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700178 ScopedLocalRef<jclass> klass(soa.Env(),
179 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
180 ScopedLocalRef<jobject> arg0(soa.Env(),
Jeff Hao5d917302013-02-27 17:57:33 -0800181 soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[0])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700182 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800183 result->SetI(fn(soa.Env(), klass.get(), arg0.get(), args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700184 } else if (shorty == "SIZ") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100185 typedef jshort (fntype)(JNIEnv*, jclass, jint, jboolean);
186 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700187 ScopedLocalRef<jclass> klass(soa.Env(),
188 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
189 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800190 result->SetS(fn(soa.Env(), klass.get(), args[0], args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700191 } else if (shorty == "VIZ") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100192 typedef void (fntype)(JNIEnv*, jclass, jint, jboolean);
193 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700194 ScopedLocalRef<jclass> klass(soa.Env(),
195 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
196 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800197 fn(soa.Env(), klass.get(), args[0], args[1]);
Ian Rogers64b6d142012-10-29 16:34:15 -0700198 } else if (shorty == "ZLL") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100199 typedef jboolean (fntype)(JNIEnv*, jclass, jobject, jobject);
200 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700201 ScopedLocalRef<jclass> klass(soa.Env(),
202 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
203 ScopedLocalRef<jobject> arg0(soa.Env(),
Jeff Hao5d917302013-02-27 17:57:33 -0800204 soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[0])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700205 ScopedLocalRef<jobject> arg1(soa.Env(),
Jeff Hao5d917302013-02-27 17:57:33 -0800206 soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[1])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700207 ScopedThreadStateChange tsc(self, kNative);
208 result->SetZ(fn(soa.Env(), klass.get(), arg0.get(), arg1.get()));
209 } else if (shorty == "ZILL") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100210 typedef jboolean (fntype)(JNIEnv*, jclass, jint, jobject, jobject);
211 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700212 ScopedLocalRef<jclass> klass(soa.Env(),
213 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
214 ScopedLocalRef<jobject> arg1(soa.Env(),
Jeff Hao5d917302013-02-27 17:57:33 -0800215 soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[1])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700216 ScopedLocalRef<jobject> arg2(soa.Env(),
Jeff Hao5d917302013-02-27 17:57:33 -0800217 soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[2])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700218 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800219 result->SetZ(fn(soa.Env(), klass.get(), args[0], arg1.get(), arg2.get()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700220 } else if (shorty == "VILII") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100221 typedef void (fntype)(JNIEnv*, jclass, jint, jobject, jint, jint);
222 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700223 ScopedLocalRef<jclass> klass(soa.Env(),
224 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
225 ScopedLocalRef<jobject> arg1(soa.Env(),
Jeff Hao5d917302013-02-27 17:57:33 -0800226 soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[1])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700227 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800228 fn(soa.Env(), klass.get(), args[0], arg1.get(), args[2], args[3]);
Ian Rogers64b6d142012-10-29 16:34:15 -0700229 } else if (shorty == "VLILII") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100230 typedef void (fntype)(JNIEnv*, jclass, jobject, jint, jobject, jint, jint);
231 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700232 ScopedLocalRef<jclass> klass(soa.Env(),
233 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
234 ScopedLocalRef<jobject> arg0(soa.Env(),
Jeff Hao5d917302013-02-27 17:57:33 -0800235 soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[0])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700236 ScopedLocalRef<jobject> arg2(soa.Env(),
Jeff Hao5d917302013-02-27 17:57:33 -0800237 soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[2])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700238 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800239 fn(soa.Env(), klass.get(), arg0.get(), args[1], arg2.get(), args[3], args[4]);
Ian Rogers64b6d142012-10-29 16:34:15 -0700240 } else {
241 LOG(FATAL) << "Do something with static native method: " << PrettyMethod(method)
242 << " shorty: " << shorty;
243 }
244 } else {
245 if (shorty == "L") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100246 typedef jobject (fntype)(JNIEnv*, jobject);
247 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700248 ScopedLocalRef<jobject> rcvr(soa.Env(),
249 soa.AddLocalReference<jobject>(receiver));
Ian Rogers556d6372012-11-20 12:19:36 -0800250 jobject jresult;
251 {
252 ScopedThreadStateChange tsc(self, kNative);
253 jresult = fn(soa.Env(), rcvr.get());
254 }
255 result->SetL(soa.Decode<Object*>(jresult));
Jeff Hao3dd9f762013-07-08 13:09:25 -0700256 } else if (shorty == "V") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100257 typedef void (fntype)(JNIEnv*, jobject);
258 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Jeff Hao3dd9f762013-07-08 13:09:25 -0700259 ScopedLocalRef<jobject> rcvr(soa.Env(),
260 soa.AddLocalReference<jobject>(receiver));
261 ScopedThreadStateChange tsc(self, kNative);
262 fn(soa.Env(), rcvr.get());
Ian Rogers64b6d142012-10-29 16:34:15 -0700263 } else if (shorty == "LL") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100264 typedef jobject (fntype)(JNIEnv*, jobject, jobject);
265 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700266 ScopedLocalRef<jobject> rcvr(soa.Env(),
267 soa.AddLocalReference<jobject>(receiver));
268 ScopedLocalRef<jobject> arg0(soa.Env(),
Jeff Hao5d917302013-02-27 17:57:33 -0800269 soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[0])));
Ian Rogers556d6372012-11-20 12:19:36 -0800270 jobject jresult;
271 {
272 ScopedThreadStateChange tsc(self, kNative);
273 jresult = fn(soa.Env(), rcvr.get(), arg0.get());
Ian Rogers556d6372012-11-20 12:19:36 -0800274 }
275 result->SetL(soa.Decode<Object*>(jresult));
Ian Rogers64b6d142012-10-29 16:34:15 -0700276 ScopedThreadStateChange tsc(self, kNative);
Ian Rogers64b6d142012-10-29 16:34:15 -0700277 } else if (shorty == "III") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100278 typedef jint (fntype)(JNIEnv*, jobject, jint, jint);
279 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700280 ScopedLocalRef<jobject> rcvr(soa.Env(),
281 soa.AddLocalReference<jobject>(receiver));
282 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800283 result->SetI(fn(soa.Env(), rcvr.get(), args[0], args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700284 } else {
285 LOG(FATAL) << "Do something with native method: " << PrettyMethod(method)
286 << " shorty: " << shorty;
287 }
288 }
289}
290
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200291enum InterpreterImplKind {
Ian Rogersb48b9eb2014-02-28 16:20:21 -0800292 kSwitchImpl, // Switch-based interpreter implementation.
293 kComputedGotoImplKind // Computed-goto-based interpreter implementation.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200294};
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700295
Ian Rogersb48b9eb2014-02-28 16:20:21 -0800296#if !defined(__clang__)
297static constexpr InterpreterImplKind kInterpreterImplKind = kComputedGotoImplKind;
298#else
299// Clang 3.4 fails to build the goto interpreter implementation.
300static constexpr InterpreterImplKind kInterpreterImplKind = kSwitchImpl;
301template<bool do_access_check, bool transaction_active>
302JValue ExecuteGotoImpl(Thread* self, MethodHelper& mh, const DexFile::CodeItem* code_item,
303 ShadowFrame& shadow_frame, JValue result_register) {
304 LOG(FATAL) << "UNREACHABLE";
305 exit(0);
306}
307// Explicit definitions of ExecuteGotoImpl.
Stephen Hines861ea562014-04-23 16:03:57 -0700308template<> SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogersb48b9eb2014-02-28 16:20:21 -0800309JValue ExecuteGotoImpl<true, false>(Thread* self, MethodHelper& mh,
310 const DexFile::CodeItem* code_item,
311 ShadowFrame& shadow_frame, JValue result_register);
Stephen Hines861ea562014-04-23 16:03:57 -0700312template<> SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogersb48b9eb2014-02-28 16:20:21 -0800313JValue ExecuteGotoImpl<false, false>(Thread* self, MethodHelper& mh,
314 const DexFile::CodeItem* code_item,
315 ShadowFrame& shadow_frame, JValue result_register);
Stephen Hines861ea562014-04-23 16:03:57 -0700316template<> SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogersb48b9eb2014-02-28 16:20:21 -0800317JValue ExecuteGotoImpl<true, true>(Thread* self, MethodHelper& mh,
318 const DexFile::CodeItem* code_item,
319 ShadowFrame& shadow_frame, JValue result_register);
Stephen Hines861ea562014-04-23 16:03:57 -0700320template<> SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogersb48b9eb2014-02-28 16:20:21 -0800321JValue ExecuteGotoImpl<false, true>(Thread* self, MethodHelper& mh,
322 const DexFile::CodeItem* code_item,
323 ShadowFrame& shadow_frame, JValue result_register);
324#endif
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700325
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200326static JValue Execute(Thread* self, MethodHelper& mh, const DexFile::CodeItem* code_item,
327 ShadowFrame& shadow_frame, JValue result_register)
328 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
329
330static inline JValue Execute(Thread* self, MethodHelper& mh, const DexFile::CodeItem* code_item,
331 ShadowFrame& shadow_frame, JValue result_register) {
Ian Rogers848871b2013-08-05 10:56:33 -0700332 DCHECK(shadow_frame.GetMethod() == mh.GetMethod() ||
333 shadow_frame.GetMethod()->GetDeclaringClass()->IsProxyClass());
334 DCHECK(!shadow_frame.GetMethod()->IsAbstract());
335 DCHECK(!shadow_frame.GetMethod()->IsNative());
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200336
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100337 bool transaction_active = Runtime::Current()->IsActiveTransaction();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200338 if (LIKELY(shadow_frame.GetMethod()->IsPreverified())) {
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200339 // Enter the "without access check" interpreter.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200340 if (kInterpreterImplKind == kSwitchImpl) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100341 if (transaction_active) {
342 return ExecuteSwitchImpl<false, true>(self, mh, code_item, shadow_frame, result_register);
343 } else {
344 return ExecuteSwitchImpl<false, false>(self, mh, code_item, shadow_frame, result_register);
345 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200346 } else {
347 DCHECK_EQ(kInterpreterImplKind, kComputedGotoImplKind);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100348 if (transaction_active) {
349 return ExecuteGotoImpl<false, true>(self, mh, code_item, shadow_frame, result_register);
350 } else {
351 return ExecuteGotoImpl<false, false>(self, mh, code_item, shadow_frame, result_register);
352 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200353 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200354 } else {
355 // Enter the "with access check" interpreter.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200356 if (kInterpreterImplKind == kSwitchImpl) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100357 if (transaction_active) {
358 return ExecuteSwitchImpl<true, true>(self, mh, code_item, shadow_frame, result_register);
359 } else {
360 return ExecuteSwitchImpl<true, false>(self, mh, code_item, shadow_frame, result_register);
361 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200362 } else {
363 DCHECK_EQ(kInterpreterImplKind, kComputedGotoImplKind);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100364 if (transaction_active) {
365 return ExecuteGotoImpl<true, true>(self, mh, code_item, shadow_frame, result_register);
366 } else {
367 return ExecuteGotoImpl<true, false>(self, mh, code_item, shadow_frame, result_register);
368 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200369 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200370 }
371}
372
Brian Carlstromea46f952013-07-30 01:26:50 -0700373void EnterInterpreterFromInvoke(Thread* self, ArtMethod* method, Object* receiver,
Jeff Hao6474d192013-03-26 14:08:09 -0700374 uint32_t* args, JValue* result) {
Ian Rogers64b6d142012-10-29 16:34:15 -0700375 DCHECK_EQ(self, Thread::Current());
Jeff Hao790ad902013-05-22 15:02:08 -0700376 if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEnd())) {
jeffhaod7521322012-11-21 15:38:24 -0800377 ThrowStackOverflowError(self);
378 return;
379 }
380
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700381 const char* old_cause = self->StartAssertNoThreadSuspension("EnterInterpreterFromInvoke");
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700382 MethodHelper mh(method);
383 const DexFile::CodeItem* code_item = mh.GetCodeItem();
384 uint16_t num_regs;
385 uint16_t num_ins;
386 if (code_item != NULL) {
387 num_regs = code_item->registers_size_;
388 num_ins = code_item->ins_size_;
jeffhao0a9bb732012-11-26 12:28:49 -0800389 } else if (method->IsAbstract()) {
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700390 self->EndAssertNoThreadSuspension(old_cause);
Sebastien Hertz56adf602013-07-09 17:27:07 +0200391 ThrowAbstractMethodError(method);
jeffhao0a9bb732012-11-26 12:28:49 -0800392 return;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700393 } else {
394 DCHECK(method->IsNative());
Brian Carlstromea46f952013-07-30 01:26:50 -0700395 num_regs = num_ins = ArtMethod::NumArgRegisters(mh.GetShorty());
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700396 if (!method->IsStatic()) {
397 num_regs++;
398 num_ins++;
399 }
400 }
401 // Set up shadow frame with matching number of reference slots to vregs.
402 ShadowFrame* last_shadow_frame = self->GetManagedStack()->GetTopShadowFrame();
Jeff Hao66135192013-05-14 11:02:41 -0700403 void* memory = alloca(ShadowFrame::ComputeSize(num_regs));
404 ShadowFrame* shadow_frame(ShadowFrame::Create(num_regs, last_shadow_frame, method, 0, memory));
405 self->PushShadowFrame(shadow_frame);
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700406
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700407 size_t cur_reg = num_regs - num_ins;
408 if (!method->IsStatic()) {
409 CHECK(receiver != NULL);
TDYa127ce4cc0d2012-11-18 16:59:53 -0800410 shadow_frame->SetVRegReference(cur_reg, receiver);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700411 ++cur_reg;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700412 }
Ian Rogers64b6d142012-10-29 16:34:15 -0700413 const char* shorty = mh.GetShorty();
Jeff Hao5d917302013-02-27 17:57:33 -0800414 for (size_t shorty_pos = 0, arg_pos = 0; cur_reg < num_regs; ++shorty_pos, ++arg_pos, cur_reg++) {
415 DCHECK_LT(shorty_pos + 1, mh.GetShortyLength());
416 switch (shorty[shorty_pos + 1]) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700417 case 'L': {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800418 Object* o = reinterpret_cast<StackReference<Object>*>(&args[arg_pos])->AsMirrorPtr();
TDYa127ce4cc0d2012-11-18 16:59:53 -0800419 shadow_frame->SetVRegReference(cur_reg, o);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700420 break;
421 }
Jeff Hao5d917302013-02-27 17:57:33 -0800422 case 'J': case 'D': {
423 uint64_t wide_value = (static_cast<uint64_t>(args[arg_pos + 1]) << 32) | args[arg_pos];
424 shadow_frame->SetVRegLong(cur_reg, wide_value);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700425 cur_reg++;
Jeff Hao5d917302013-02-27 17:57:33 -0800426 arg_pos++;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700427 break;
Jeff Hao5d917302013-02-27 17:57:33 -0800428 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700429 default:
Jeff Hao5d917302013-02-27 17:57:33 -0800430 shadow_frame->SetVReg(cur_reg, args[arg_pos]);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700431 break;
432 }
433 }
Mathieu Chartier92246bb2014-02-25 18:22:39 -0800434 self->EndAssertNoThreadSuspension(old_cause);
435 // Do this after populating the shadow frame in case EnsureInitialized causes a GC.
436 if (method->IsStatic() && UNLIKELY(!method->GetDeclaringClass()->IsInitializing())) {
437 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
438 SirtRef<mirror::Class> sirt_c(self, method->GetDeclaringClass());
439 if (UNLIKELY(!class_linker->EnsureInitialized(sirt_c, true, true))) {
440 CHECK(self->IsExceptionPending());
441 self->PopShadowFrame();
442 return;
443 }
444 }
Ian Rogers64b6d142012-10-29 16:34:15 -0700445 if (LIKELY(!method->IsNative())) {
Jeff Hao66135192013-05-14 11:02:41 -0700446 JValue r = Execute(self, mh, code_item, *shadow_frame, JValue());
Jeff Hao6474d192013-03-26 14:08:09 -0700447 if (result != NULL) {
448 *result = r;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700449 }
450 } else {
Ian Rogers64b6d142012-10-29 16:34:15 -0700451 // We don't expect to be asked to interpret native code (which is entered via a JNI compiler
452 // generated stub) except during testing and image writing.
Mathieu Chartier92246bb2014-02-25 18:22:39 -0800453 // Update args to be the args in the shadow frame since the input ones could hold stale
454 // references pointers due to moving GC.
455 args = shadow_frame->GetVRegArgs(method->IsStatic() ? 0 : 1);
Ian Rogers64b6d142012-10-29 16:34:15 -0700456 if (!Runtime::Current()->IsStarted()) {
Jeff Hao6474d192013-03-26 14:08:09 -0700457 UnstartedRuntimeJni(self, method, receiver, args, result);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700458 } else {
Jeff Hao6474d192013-03-26 14:08:09 -0700459 InterpreterJni(self, method, shorty, receiver, args, result);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700460 }
461 }
462 self->PopShadowFrame();
463}
464
Ian Rogers62d6c772013-02-27 08:32:07 -0800465void EnterInterpreterFromDeoptimize(Thread* self, ShadowFrame* shadow_frame, JValue* ret_val)
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800466 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
467 JValue value;
Ian Rogers62d6c772013-02-27 08:32:07 -0800468 value.SetJ(ret_val->GetJ()); // Set value to last known result in case the shadow frame chain is empty.
469 MethodHelper mh;
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800470 while (shadow_frame != NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800471 self->SetTopOfShadowStack(shadow_frame);
472 mh.ChangeMethod(shadow_frame->GetMethod());
473 const DexFile::CodeItem* code_item = mh.GetCodeItem();
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800474 value = Execute(self, mh, code_item, *shadow_frame, value);
475 ShadowFrame* old_frame = shadow_frame;
476 shadow_frame = shadow_frame->GetLink();
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800477 delete old_frame;
478 }
479 ret_val->SetJ(value.GetJ());
480}
481
Ian Rogers7db619b2013-01-16 18:35:48 -0800482JValue EnterInterpreterFromStub(Thread* self, MethodHelper& mh, const DexFile::CodeItem* code_item,
Ian Rogers848871b2013-08-05 10:56:33 -0700483 ShadowFrame& shadow_frame) {
Ian Rogersf3e98552013-03-20 15:49:49 -0700484 DCHECK_EQ(self, Thread::Current());
Jeff Hao790ad902013-05-22 15:02:08 -0700485 if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEnd())) {
Ian Rogersf3e98552013-03-20 15:49:49 -0700486 ThrowStackOverflowError(self);
487 return JValue();
488 }
489
Ian Rogers7db619b2013-01-16 18:35:48 -0800490 return Execute(self, mh, code_item, shadow_frame, JValue());
491}
492
Ian Rogers848871b2013-08-05 10:56:33 -0700493extern "C" void artInterpreterToInterpreterBridge(Thread* self, MethodHelper& mh,
494 const DexFile::CodeItem* code_item,
495 ShadowFrame* shadow_frame, JValue* result) {
Jeff Hao790ad902013-05-22 15:02:08 -0700496 if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEnd())) {
Jeff Hao16743632013-05-08 10:59:04 -0700497 ThrowStackOverflowError(self);
Jeff Hao69510672013-05-21 17:34:55 -0700498 return;
Jeff Hao16743632013-05-08 10:59:04 -0700499 }
500
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700501 self->PushShadowFrame(shadow_frame);
Brian Carlstromea46f952013-07-30 01:26:50 -0700502 ArtMethod* method = shadow_frame->GetMethod();
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200503 // Ensure static methods are initialized.
504 if (method->IsStatic()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800505 SirtRef<Class> declaringClass(self, method->GetDeclaringClass());
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200506 if (UNLIKELY(!declaringClass->IsInitializing())) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700507 if (UNLIKELY(!Runtime::Current()->GetClassLinker()->EnsureInitialized(declaringClass, true,
508 true))) {
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200509 DCHECK(Thread::Current()->IsExceptionPending());
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700510 self->PopShadowFrame();
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200511 return;
512 }
513 CHECK(declaringClass->IsInitializing());
Jeff Hao16743632013-05-08 10:59:04 -0700514 }
Jeff Hao16743632013-05-08 10:59:04 -0700515 }
Jeff Hao16743632013-05-08 10:59:04 -0700516
Jeff Hao16743632013-05-08 10:59:04 -0700517 if (LIKELY(!method->IsNative())) {
Jeff Hao69510672013-05-21 17:34:55 -0700518 result->SetJ(Execute(self, mh, code_item, *shadow_frame, JValue()).GetJ());
Jeff Hao16743632013-05-08 10:59:04 -0700519 } else {
520 // We don't expect to be asked to interpret native code (which is entered via a JNI compiler
521 // generated stub) except during testing and image writing.
522 CHECK(!Runtime::Current()->IsStarted());
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700523 Object* receiver = method->IsStatic() ? nullptr : shadow_frame->GetVRegReference(0);
Jeff Hao16743632013-05-08 10:59:04 -0700524 uint32_t* args = shadow_frame->GetVRegArgs(method->IsStatic() ? 0 : 1);
Jeff Hao69510672013-05-21 17:34:55 -0700525 UnstartedRuntimeJni(self, method, receiver, args, result);
Jeff Hao16743632013-05-08 10:59:04 -0700526 }
527
528 self->PopShadowFrame();
Jeff Hao16743632013-05-08 10:59:04 -0700529}
530
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700531} // namespace interpreter
532} // namespace art