blob: 9cc144149b712a62a037b3c5b08356c212cfa7cd [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));
Ian Rogersa4e74132014-05-06 01:14:54 -070031 if (name == "java.lang.Object dalvik.system.VMRuntime.newUnpaddedArray(java.lang.Class, int)") {
32 int32_t length = args[1];
33 DCHECK_GE(length, 0);
34 mirror::Class* element_class = reinterpret_cast<Object*>(args[0])->AsClass();
35 Runtime* runtime = Runtime::Current();
Mathieu Chartierb74cd292014-05-29 14:31:33 -070036 mirror::Class* array_class = runtime->GetClassLinker()->FindArrayClass(self, &element_class);
Ian Rogersa4e74132014-05-06 01:14:54 -070037 DCHECK(array_class != nullptr);
38 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
39 result->SetL(mirror::Array::Alloc<true>(self, array_class, length,
40 array_class->GetComponentSize(), allocator, true));
41 } else if (name == "java.lang.ClassLoader dalvik.system.VMStack.getCallingClassLoader()") {
Ian Rogers64b6d142012-10-29 16:34:15 -070042 result->SetL(NULL);
43 } else if (name == "java.lang.Class dalvik.system.VMStack.getStackClass2()") {
Ian Rogers7a22fa62013-01-23 12:16:16 -080044 NthCallerVisitor visitor(self, 3);
Ian Rogers64b6d142012-10-29 16:34:15 -070045 visitor.WalkStack();
46 result->SetL(visitor.caller->GetDeclaringClass());
47 } else if (name == "double java.lang.Math.log(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(log(value.GetD()));
Ian Rogers64b6d142012-10-29 16:34:15 -070051 } else if (name == "java.lang.String java.lang.Class.getNameNative()") {
Mathieu Chartierf8322842014-05-16 10:59:25 -070052 StackHandleScope<1> hs(self);
53 result->SetL(mirror::Class::ComputeName(hs.NewHandle(receiver->AsClass())));
Ian Rogers64b6d142012-10-29 16:34:15 -070054 } else if (name == "int java.lang.Float.floatToRawIntBits(float)") {
Jeff Hao5d917302013-02-27 17:57:33 -080055 result->SetI(args[0]);
Ian Rogers64b6d142012-10-29 16:34:15 -070056 } else if (name == "float java.lang.Float.intBitsToFloat(int)") {
Jeff Hao5d917302013-02-27 17:57:33 -080057 result->SetI(args[0]);
Ian Rogers64b6d142012-10-29 16:34:15 -070058 } else if (name == "double java.lang.Math.exp(double)") {
Jeff Hao5d917302013-02-27 17:57:33 -080059 JValue value;
60 value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]);
61 result->SetD(exp(value.GetD()));
Ian Rogers64b6d142012-10-29 16:34:15 -070062 } else if (name == "java.lang.Object java.lang.Object.internalClone()") {
63 result->SetL(receiver->Clone(self));
64 } else if (name == "void java.lang.Object.notifyAll()") {
Ian Rogers05f30572013-02-20 12:13:11 -080065 receiver->NotifyAll(self);
Ian Rogers64b6d142012-10-29 16:34:15 -070066 } else if (name == "int java.lang.String.compareTo(java.lang.String)") {
Jeff Hao5d917302013-02-27 17:57:33 -080067 String* rhs = reinterpret_cast<Object*>(args[0])->AsString();
Ian Rogers64b6d142012-10-29 16:34:15 -070068 CHECK(rhs != NULL);
69 result->SetI(receiver->AsString()->CompareTo(rhs));
70 } else if (name == "java.lang.String java.lang.String.intern()") {
71 result->SetL(receiver->AsString()->Intern());
72 } else if (name == "int java.lang.String.fastIndexOf(int, int)") {
Jeff Hao5d917302013-02-27 17:57:33 -080073 result->SetI(receiver->AsString()->FastIndexOf(args[0], args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -070074 } else if (name == "java.lang.Object java.lang.reflect.Array.createMultiArray(java.lang.Class, int[])") {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070075 StackHandleScope<2> hs(self);
76 auto h_class(hs.NewHandle(reinterpret_cast<mirror::Class*>(args[0])->AsClass()));
77 auto h_dimensions(hs.NewHandle(reinterpret_cast<mirror::IntArray*>(args[1])->AsIntArray()));
78 result->SetL(Array::CreateMultiArray(self, h_class, h_dimensions));
Ian Rogers64b6d142012-10-29 16:34:15 -070079 } else if (name == "java.lang.Object java.lang.Throwable.nativeFillInStackTrace()") {
80 ScopedObjectAccessUnchecked soa(self);
Ian Rogers5d27faf2014-05-02 17:17:18 -070081 if (Runtime::Current()->IsActiveTransaction()) {
82 result->SetL(soa.Decode<Object*>(self->CreateInternalStackTrace<true>(soa)));
83 } else {
84 result->SetL(soa.Decode<Object*>(self->CreateInternalStackTrace<false>(soa)));
85 }
Sebastien Hertzf48644b2014-02-17 15:16:03 +010086 } else if (name == "int java.lang.System.identityHashCode(java.lang.Object)") {
87 mirror::Object* obj = reinterpret_cast<Object*>(args[0]);
88 result->SetI((obj != nullptr) ? obj->IdentityHashCode() : 0);
Ian Rogers64b6d142012-10-29 16:34:15 -070089 } else if (name == "boolean java.nio.ByteOrder.isLittleEndian()") {
Sebastien Hertzf48644b2014-02-17 15:16:03 +010090 result->SetZ(JNI_TRUE);
Ian Rogers64b6d142012-10-29 16:34:15 -070091 } else if (name == "boolean sun.misc.Unsafe.compareAndSwapInt(java.lang.Object, long, int, int)") {
Jeff Hao5d917302013-02-27 17:57:33 -080092 Object* obj = reinterpret_cast<Object*>(args[0]);
93 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
94 jint expectedValue = args[3];
95 jint newValue = args[4];
Ian Rogers5d27faf2014-05-02 17:17:18 -070096 bool success;
97 if (Runtime::Current()->IsActiveTransaction()) {
98 success = obj->CasField32<true>(MemberOffset(offset), expectedValue, newValue);
99 } else {
100 success = obj->CasField32<false>(MemberOffset(offset), expectedValue, newValue);
101 }
Sebastien Hertzf48644b2014-02-17 15:16:03 +0100102 result->SetZ(success ? JNI_TRUE : JNI_FALSE);
Ian Rogers64b6d142012-10-29 16:34:15 -0700103 } else if (name == "void sun.misc.Unsafe.putObject(java.lang.Object, long, java.lang.Object)") {
Jeff Hao5d917302013-02-27 17:57:33 -0800104 Object* obj = reinterpret_cast<Object*>(args[0]);
Sebastien Hertzf48644b2014-02-17 15:16:03 +0100105 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
Jeff Hao5d917302013-02-27 17:57:33 -0800106 Object* newValue = reinterpret_cast<Object*>(args[3]);
Ian Rogers5d27faf2014-05-02 17:17:18 -0700107 if (Runtime::Current()->IsActiveTransaction()) {
108 obj->SetFieldObject<true>(MemberOffset(offset), newValue);
109 } else {
110 obj->SetFieldObject<false>(MemberOffset(offset), newValue);
111 }
Hiroshi Yamauchi4d2efce2014-02-10 16:19:09 -0800112 } else if (name == "int sun.misc.Unsafe.getArrayBaseOffsetForComponentType(java.lang.Class)") {
113 mirror::Class* component = reinterpret_cast<Object*>(args[0])->AsClass();
114 Primitive::Type primitive_type = component->GetPrimitiveType();
115 result->SetI(mirror::Array::DataOffset(Primitive::ComponentSize(primitive_type)).Int32Value());
116 } else if (name == "int sun.misc.Unsafe.getArrayIndexScaleForComponentType(java.lang.Class)") {
117 mirror::Class* component = reinterpret_cast<Object*>(args[0])->AsClass();
118 Primitive::Type primitive_type = component->GetPrimitiveType();
119 result->SetI(Primitive::ComponentSize(primitive_type));
Ian Rogers5d27faf2014-05-02 17:17:18 -0700120 } else if (Runtime::Current()->IsActiveTransaction()) {
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700121 AbortTransaction(self, "Attempt to invoke native method in non-started runtime: %s",
122 name.c_str());
Ian Rogers5d27faf2014-05-02 17:17:18 -0700123
124 } else {
125 LOG(FATAL) << "Calling native method " << PrettyMethod(method) << " in an unstarted "
126 "non-transactional runtime";
Ian Rogers64b6d142012-10-29 16:34:15 -0700127 }
128}
129
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700130static void InterpreterJni(Thread* self, ArtMethod* method, const StringPiece& shorty,
Jeff Hao5d917302013-02-27 17:57:33 -0800131 Object* receiver, uint32_t* args, JValue* result)
Ian Rogers64b6d142012-10-29 16:34:15 -0700132 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
133 // TODO: The following enters JNI code using a typedef-ed function rather than the JNI compiler,
134 // it should be removed and JNI compiled stubs used instead.
135 ScopedObjectAccessUnchecked soa(self);
136 if (method->IsStatic()) {
137 if (shorty == "L") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100138 typedef jobject (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()));
Ian Rogers556d6372012-11-20 12:19:36 -0800142 jobject jresult;
143 {
144 ScopedThreadStateChange tsc(self, kNative);
145 jresult = fn(soa.Env(), klass.get());
146 }
147 result->SetL(soa.Decode<Object*>(jresult));
Ian Rogers64b6d142012-10-29 16:34:15 -0700148 } else if (shorty == "V") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100149 typedef void (fntype)(JNIEnv*, jclass);
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);
154 fn(soa.Env(), klass.get());
155 } else if (shorty == "Z") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100156 typedef jboolean (fntype)(JNIEnv*, jclass);
157 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700158 ScopedLocalRef<jclass> klass(soa.Env(),
159 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
160 ScopedThreadStateChange tsc(self, kNative);
161 result->SetZ(fn(soa.Env(), klass.get()));
162 } else if (shorty == "BI") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100163 typedef jbyte (fntype)(JNIEnv*, jclass, jint);
164 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700165 ScopedLocalRef<jclass> klass(soa.Env(),
166 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
167 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800168 result->SetB(fn(soa.Env(), klass.get(), args[0]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700169 } else if (shorty == "II") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100170 typedef jint (fntype)(JNIEnv*, jclass, jint);
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]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700176 } else if (shorty == "LL") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100177 typedef jobject (fntype)(JNIEnv*, jclass, jobject);
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 Rogers556d6372012-11-20 12:19:36 -0800183 jobject jresult;
184 {
185 ScopedThreadStateChange tsc(self, kNative);
186 jresult = fn(soa.Env(), klass.get(), arg0.get());
187 }
188 result->SetL(soa.Decode<Object*>(jresult));
Ian Rogers64b6d142012-10-29 16:34:15 -0700189 } else if (shorty == "IIZ") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100190 typedef jint (fntype)(JNIEnv*, jclass, jint, jboolean);
191 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700192 ScopedLocalRef<jclass> klass(soa.Env(),
193 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
194 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800195 result->SetI(fn(soa.Env(), klass.get(), args[0], args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700196 } else if (shorty == "ILI") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100197 typedef jint (fntype)(JNIEnv*, jclass, jobject, jint);
198 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700199 ScopedLocalRef<jclass> klass(soa.Env(),
200 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
201 ScopedLocalRef<jobject> arg0(soa.Env(),
Jeff Hao5d917302013-02-27 17:57:33 -0800202 soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[0])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700203 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800204 result->SetI(fn(soa.Env(), klass.get(), arg0.get(), args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700205 } else if (shorty == "SIZ") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100206 typedef jshort (fntype)(JNIEnv*, jclass, jint, jboolean);
207 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700208 ScopedLocalRef<jclass> klass(soa.Env(),
209 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
210 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800211 result->SetS(fn(soa.Env(), klass.get(), args[0], args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700212 } else if (shorty == "VIZ") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100213 typedef void (fntype)(JNIEnv*, jclass, jint, jboolean);
214 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700215 ScopedLocalRef<jclass> klass(soa.Env(),
216 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
217 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800218 fn(soa.Env(), klass.get(), args[0], args[1]);
Ian Rogers64b6d142012-10-29 16:34:15 -0700219 } else if (shorty == "ZLL") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100220 typedef jboolean (fntype)(JNIEnv*, jclass, 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> arg0(soa.Env(),
Jeff Hao5d917302013-02-27 17:57:33 -0800225 soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[0])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700226 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);
229 result->SetZ(fn(soa.Env(), klass.get(), arg0.get(), arg1.get()));
230 } else if (shorty == "ZILL") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100231 typedef jboolean (fntype)(JNIEnv*, jclass, jint, jobject, jobject);
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 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 result->SetZ(fn(soa.Env(), klass.get(), args[0], arg1.get(), arg2.get()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700241 } else if (shorty == "VILII") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100242 typedef void (fntype)(JNIEnv*, jclass, jint, jobject, jint, jint);
243 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700244 ScopedLocalRef<jclass> klass(soa.Env(),
245 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
246 ScopedLocalRef<jobject> arg1(soa.Env(),
Jeff Hao5d917302013-02-27 17:57:33 -0800247 soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[1])));
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(), args[0], arg1.get(), args[2], args[3]);
Ian Rogers64b6d142012-10-29 16:34:15 -0700250 } else if (shorty == "VLILII") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100251 typedef void (fntype)(JNIEnv*, jclass, jobject, jint, jobject, jint, jint);
252 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700253 ScopedLocalRef<jclass> klass(soa.Env(),
254 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
255 ScopedLocalRef<jobject> arg0(soa.Env(),
Jeff Hao5d917302013-02-27 17:57:33 -0800256 soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[0])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700257 ScopedLocalRef<jobject> arg2(soa.Env(),
Jeff Hao5d917302013-02-27 17:57:33 -0800258 soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[2])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700259 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800260 fn(soa.Env(), klass.get(), arg0.get(), args[1], arg2.get(), args[3], args[4]);
Ian Rogers64b6d142012-10-29 16:34:15 -0700261 } else {
262 LOG(FATAL) << "Do something with static native method: " << PrettyMethod(method)
263 << " shorty: " << shorty;
264 }
265 } else {
266 if (shorty == "L") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100267 typedef jobject (fntype)(JNIEnv*, jobject);
268 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700269 ScopedLocalRef<jobject> rcvr(soa.Env(),
270 soa.AddLocalReference<jobject>(receiver));
Ian Rogers556d6372012-11-20 12:19:36 -0800271 jobject jresult;
272 {
273 ScopedThreadStateChange tsc(self, kNative);
274 jresult = fn(soa.Env(), rcvr.get());
275 }
276 result->SetL(soa.Decode<Object*>(jresult));
Jeff Hao3dd9f762013-07-08 13:09:25 -0700277 } else if (shorty == "V") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100278 typedef void (fntype)(JNIEnv*, jobject);
279 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Jeff Hao3dd9f762013-07-08 13:09:25 -0700280 ScopedLocalRef<jobject> rcvr(soa.Env(),
281 soa.AddLocalReference<jobject>(receiver));
282 ScopedThreadStateChange tsc(self, kNative);
283 fn(soa.Env(), rcvr.get());
Ian Rogers64b6d142012-10-29 16:34:15 -0700284 } else if (shorty == "LL") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100285 typedef jobject (fntype)(JNIEnv*, jobject, jobject);
286 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700287 ScopedLocalRef<jobject> rcvr(soa.Env(),
288 soa.AddLocalReference<jobject>(receiver));
289 ScopedLocalRef<jobject> arg0(soa.Env(),
Jeff Hao5d917302013-02-27 17:57:33 -0800290 soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[0])));
Ian Rogers556d6372012-11-20 12:19:36 -0800291 jobject jresult;
292 {
293 ScopedThreadStateChange tsc(self, kNative);
294 jresult = fn(soa.Env(), rcvr.get(), arg0.get());
Ian Rogers556d6372012-11-20 12:19:36 -0800295 }
296 result->SetL(soa.Decode<Object*>(jresult));
Ian Rogers64b6d142012-10-29 16:34:15 -0700297 ScopedThreadStateChange tsc(self, kNative);
Ian Rogers64b6d142012-10-29 16:34:15 -0700298 } else if (shorty == "III") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100299 typedef jint (fntype)(JNIEnv*, jobject, jint, jint);
300 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700301 ScopedLocalRef<jobject> rcvr(soa.Env(),
302 soa.AddLocalReference<jobject>(receiver));
303 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800304 result->SetI(fn(soa.Env(), rcvr.get(), args[0], args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700305 } else {
306 LOG(FATAL) << "Do something with native method: " << PrettyMethod(method)
307 << " shorty: " << shorty;
308 }
309 }
310}
311
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200312enum InterpreterImplKind {
Ian Rogersb48b9eb2014-02-28 16:20:21 -0800313 kSwitchImpl, // Switch-based interpreter implementation.
314 kComputedGotoImplKind // Computed-goto-based interpreter implementation.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200315};
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700316
Ian Rogersb48b9eb2014-02-28 16:20:21 -0800317#if !defined(__clang__)
318static constexpr InterpreterImplKind kInterpreterImplKind = kComputedGotoImplKind;
319#else
320// Clang 3.4 fails to build the goto interpreter implementation.
321static constexpr InterpreterImplKind kInterpreterImplKind = kSwitchImpl;
322template<bool do_access_check, bool transaction_active>
323JValue ExecuteGotoImpl(Thread* self, MethodHelper& mh, const DexFile::CodeItem* code_item,
324 ShadowFrame& shadow_frame, JValue result_register) {
325 LOG(FATAL) << "UNREACHABLE";
326 exit(0);
327}
328// Explicit definitions of ExecuteGotoImpl.
Stephen Hines861ea562014-04-23 16:03:57 -0700329template<> SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogersb48b9eb2014-02-28 16:20:21 -0800330JValue ExecuteGotoImpl<true, false>(Thread* self, MethodHelper& mh,
331 const DexFile::CodeItem* code_item,
332 ShadowFrame& shadow_frame, JValue result_register);
Stephen Hines861ea562014-04-23 16:03:57 -0700333template<> SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogersb48b9eb2014-02-28 16:20:21 -0800334JValue ExecuteGotoImpl<false, false>(Thread* self, MethodHelper& mh,
335 const DexFile::CodeItem* code_item,
336 ShadowFrame& shadow_frame, JValue result_register);
Stephen Hines861ea562014-04-23 16:03:57 -0700337template<> SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogersb48b9eb2014-02-28 16:20:21 -0800338JValue ExecuteGotoImpl<true, true>(Thread* self, MethodHelper& mh,
339 const DexFile::CodeItem* code_item,
340 ShadowFrame& shadow_frame, JValue result_register);
Stephen Hines861ea562014-04-23 16:03:57 -0700341template<> SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogersb48b9eb2014-02-28 16:20:21 -0800342JValue ExecuteGotoImpl<false, true>(Thread* self, MethodHelper& mh,
343 const DexFile::CodeItem* code_item,
344 ShadowFrame& shadow_frame, JValue result_register);
345#endif
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700346
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200347static JValue Execute(Thread* self, MethodHelper& mh, const DexFile::CodeItem* code_item,
348 ShadowFrame& shadow_frame, JValue result_register)
349 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
350
351static inline JValue Execute(Thread* self, MethodHelper& mh, const DexFile::CodeItem* code_item,
352 ShadowFrame& shadow_frame, JValue result_register) {
Ian Rogers848871b2013-08-05 10:56:33 -0700353 DCHECK(shadow_frame.GetMethod() == mh.GetMethod() ||
354 shadow_frame.GetMethod()->GetDeclaringClass()->IsProxyClass());
355 DCHECK(!shadow_frame.GetMethod()->IsAbstract());
356 DCHECK(!shadow_frame.GetMethod()->IsNative());
Sebastien Hertz4e99b3d2014-06-24 14:35:40 +0200357 shadow_frame.GetMethod()->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200358
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100359 bool transaction_active = Runtime::Current()->IsActiveTransaction();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200360 if (LIKELY(shadow_frame.GetMethod()->IsPreverified())) {
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200361 // Enter the "without access check" interpreter.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200362 if (kInterpreterImplKind == kSwitchImpl) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100363 if (transaction_active) {
364 return ExecuteSwitchImpl<false, true>(self, mh, code_item, shadow_frame, result_register);
365 } else {
366 return ExecuteSwitchImpl<false, false>(self, mh, code_item, shadow_frame, result_register);
367 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200368 } else {
369 DCHECK_EQ(kInterpreterImplKind, kComputedGotoImplKind);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100370 if (transaction_active) {
371 return ExecuteGotoImpl<false, true>(self, mh, code_item, shadow_frame, result_register);
372 } else {
373 return ExecuteGotoImpl<false, false>(self, mh, code_item, shadow_frame, result_register);
374 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200375 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200376 } else {
377 // Enter the "with access check" interpreter.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200378 if (kInterpreterImplKind == kSwitchImpl) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100379 if (transaction_active) {
380 return ExecuteSwitchImpl<true, true>(self, mh, code_item, shadow_frame, result_register);
381 } else {
382 return ExecuteSwitchImpl<true, false>(self, mh, code_item, shadow_frame, result_register);
383 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200384 } else {
385 DCHECK_EQ(kInterpreterImplKind, kComputedGotoImplKind);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100386 if (transaction_active) {
387 return ExecuteGotoImpl<true, true>(self, mh, code_item, shadow_frame, result_register);
388 } else {
389 return ExecuteGotoImpl<true, false>(self, mh, code_item, shadow_frame, result_register);
390 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200391 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200392 }
393}
394
Brian Carlstromea46f952013-07-30 01:26:50 -0700395void EnterInterpreterFromInvoke(Thread* self, ArtMethod* method, Object* receiver,
Jeff Hao6474d192013-03-26 14:08:09 -0700396 uint32_t* args, JValue* result) {
Ian Rogers64b6d142012-10-29 16:34:15 -0700397 DCHECK_EQ(self, Thread::Current());
Jeff Hao790ad902013-05-22 15:02:08 -0700398 if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEnd())) {
jeffhaod7521322012-11-21 15:38:24 -0800399 ThrowStackOverflowError(self);
400 return;
401 }
402
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700403 const char* old_cause = self->StartAssertNoThreadSuspension("EnterInterpreterFromInvoke");
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700404 const DexFile::CodeItem* code_item = method->GetCodeItem();
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700405 uint16_t num_regs;
406 uint16_t num_ins;
407 if (code_item != NULL) {
408 num_regs = code_item->registers_size_;
409 num_ins = code_item->ins_size_;
jeffhao0a9bb732012-11-26 12:28:49 -0800410 } else if (method->IsAbstract()) {
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700411 self->EndAssertNoThreadSuspension(old_cause);
Sebastien Hertz56adf602013-07-09 17:27:07 +0200412 ThrowAbstractMethodError(method);
jeffhao0a9bb732012-11-26 12:28:49 -0800413 return;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700414 } else {
415 DCHECK(method->IsNative());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700416 num_regs = num_ins = ArtMethod::NumArgRegisters(method->GetShorty());
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700417 if (!method->IsStatic()) {
418 num_regs++;
419 num_ins++;
420 }
421 }
422 // Set up shadow frame with matching number of reference slots to vregs.
423 ShadowFrame* last_shadow_frame = self->GetManagedStack()->GetTopShadowFrame();
Jeff Hao66135192013-05-14 11:02:41 -0700424 void* memory = alloca(ShadowFrame::ComputeSize(num_regs));
425 ShadowFrame* shadow_frame(ShadowFrame::Create(num_regs, last_shadow_frame, method, 0, memory));
426 self->PushShadowFrame(shadow_frame);
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700427
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700428 size_t cur_reg = num_regs - num_ins;
429 if (!method->IsStatic()) {
430 CHECK(receiver != NULL);
TDYa127ce4cc0d2012-11-18 16:59:53 -0800431 shadow_frame->SetVRegReference(cur_reg, receiver);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700432 ++cur_reg;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700433 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700434 uint32_t shorty_len = 0;
435 const char* shorty = method->GetShorty(&shorty_len);
Jeff Hao5d917302013-02-27 17:57:33 -0800436 for (size_t shorty_pos = 0, arg_pos = 0; cur_reg < num_regs; ++shorty_pos, ++arg_pos, cur_reg++) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700437 DCHECK_LT(shorty_pos + 1, shorty_len);
Jeff Hao5d917302013-02-27 17:57:33 -0800438 switch (shorty[shorty_pos + 1]) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700439 case 'L': {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800440 Object* o = reinterpret_cast<StackReference<Object>*>(&args[arg_pos])->AsMirrorPtr();
TDYa127ce4cc0d2012-11-18 16:59:53 -0800441 shadow_frame->SetVRegReference(cur_reg, o);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700442 break;
443 }
Jeff Hao5d917302013-02-27 17:57:33 -0800444 case 'J': case 'D': {
445 uint64_t wide_value = (static_cast<uint64_t>(args[arg_pos + 1]) << 32) | args[arg_pos];
446 shadow_frame->SetVRegLong(cur_reg, wide_value);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700447 cur_reg++;
Jeff Hao5d917302013-02-27 17:57:33 -0800448 arg_pos++;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700449 break;
Jeff Hao5d917302013-02-27 17:57:33 -0800450 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700451 default:
Jeff Hao5d917302013-02-27 17:57:33 -0800452 shadow_frame->SetVReg(cur_reg, args[arg_pos]);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700453 break;
454 }
455 }
Mathieu Chartier92246bb2014-02-25 18:22:39 -0800456 self->EndAssertNoThreadSuspension(old_cause);
457 // Do this after populating the shadow frame in case EnsureInitialized causes a GC.
Ian Rogers6c5cb212014-06-18 16:07:20 -0700458 if (method->IsStatic() && UNLIKELY(!method->GetDeclaringClass()->IsInitialized())) {
Mathieu Chartier92246bb2014-02-25 18:22:39 -0800459 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700460 StackHandleScope<1> hs(self);
461 Handle<mirror::Class> h_class(hs.NewHandle(method->GetDeclaringClass()));
462 if (UNLIKELY(!class_linker->EnsureInitialized(h_class, true, true))) {
Mathieu Chartier92246bb2014-02-25 18:22:39 -0800463 CHECK(self->IsExceptionPending());
464 self->PopShadowFrame();
465 return;
466 }
467 }
Ian Rogers64b6d142012-10-29 16:34:15 -0700468 if (LIKELY(!method->IsNative())) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700469 StackHandleScope<1> hs(self);
470 MethodHelper mh(hs.NewHandle(method));
Jeff Hao66135192013-05-14 11:02:41 -0700471 JValue r = Execute(self, mh, code_item, *shadow_frame, JValue());
Jeff Hao6474d192013-03-26 14:08:09 -0700472 if (result != NULL) {
473 *result = r;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700474 }
475 } else {
Ian Rogers64b6d142012-10-29 16:34:15 -0700476 // We don't expect to be asked to interpret native code (which is entered via a JNI compiler
477 // generated stub) except during testing and image writing.
Mathieu Chartier92246bb2014-02-25 18:22:39 -0800478 // Update args to be the args in the shadow frame since the input ones could hold stale
479 // references pointers due to moving GC.
480 args = shadow_frame->GetVRegArgs(method->IsStatic() ? 0 : 1);
Ian Rogers64b6d142012-10-29 16:34:15 -0700481 if (!Runtime::Current()->IsStarted()) {
Jeff Hao6474d192013-03-26 14:08:09 -0700482 UnstartedRuntimeJni(self, method, receiver, args, result);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700483 } else {
Jeff Hao6474d192013-03-26 14:08:09 -0700484 InterpreterJni(self, method, shorty, receiver, args, result);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700485 }
486 }
487 self->PopShadowFrame();
488}
489
Ian Rogers62d6c772013-02-27 08:32:07 -0800490void EnterInterpreterFromDeoptimize(Thread* self, ShadowFrame* shadow_frame, JValue* ret_val)
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800491 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
492 JValue value;
Ian Rogers62d6c772013-02-27 08:32:07 -0800493 value.SetJ(ret_val->GetJ()); // Set value to last known result in case the shadow frame chain is empty.
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800494 while (shadow_frame != NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800495 self->SetTopOfShadowStack(shadow_frame);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700496 StackHandleScope<1> hs(self);
497 MethodHelper mh(hs.NewHandle(shadow_frame->GetMethod()));
498 const DexFile::CodeItem* code_item = mh.GetMethod()->GetCodeItem();
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800499 value = Execute(self, mh, code_item, *shadow_frame, value);
500 ShadowFrame* old_frame = shadow_frame;
501 shadow_frame = shadow_frame->GetLink();
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800502 delete old_frame;
503 }
504 ret_val->SetJ(value.GetJ());
505}
506
Ian Rogers7db619b2013-01-16 18:35:48 -0800507JValue EnterInterpreterFromStub(Thread* self, MethodHelper& mh, const DexFile::CodeItem* code_item,
Ian Rogers848871b2013-08-05 10:56:33 -0700508 ShadowFrame& shadow_frame) {
Ian Rogersf3e98552013-03-20 15:49:49 -0700509 DCHECK_EQ(self, Thread::Current());
Jeff Hao790ad902013-05-22 15:02:08 -0700510 if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEnd())) {
Ian Rogersf3e98552013-03-20 15:49:49 -0700511 ThrowStackOverflowError(self);
512 return JValue();
513 }
514
Ian Rogers7db619b2013-01-16 18:35:48 -0800515 return Execute(self, mh, code_item, shadow_frame, JValue());
516}
517
Ian Rogers848871b2013-08-05 10:56:33 -0700518extern "C" void artInterpreterToInterpreterBridge(Thread* self, MethodHelper& mh,
519 const DexFile::CodeItem* code_item,
520 ShadowFrame* shadow_frame, JValue* result) {
Jeff Hao790ad902013-05-22 15:02:08 -0700521 if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEnd())) {
Jeff Hao16743632013-05-08 10:59:04 -0700522 ThrowStackOverflowError(self);
Jeff Hao69510672013-05-21 17:34:55 -0700523 return;
Jeff Hao16743632013-05-08 10:59:04 -0700524 }
525
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700526 self->PushShadowFrame(shadow_frame);
Brian Carlstromea46f952013-07-30 01:26:50 -0700527 ArtMethod* method = shadow_frame->GetMethod();
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200528 // Ensure static methods are initialized.
529 if (method->IsStatic()) {
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700530 mirror::Class* declaring_class = method->GetDeclaringClass();
Ian Rogers6c5cb212014-06-18 16:07:20 -0700531 if (UNLIKELY(!declaring_class->IsInitialized())) {
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700532 StackHandleScope<1> hs(self);
533 HandleWrapper<Class> h_declaring_class(hs.NewHandleWrapper(&declaring_class));
534 if (UNLIKELY(!Runtime::Current()->GetClassLinker()->EnsureInitialized(
535 h_declaring_class, true, true))) {
536 DCHECK(self->IsExceptionPending());
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700537 self->PopShadowFrame();
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200538 return;
539 }
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700540 CHECK(h_declaring_class->IsInitializing());
Jeff Hao16743632013-05-08 10:59:04 -0700541 }
Jeff Hao16743632013-05-08 10:59:04 -0700542 }
Jeff Hao16743632013-05-08 10:59:04 -0700543
Jeff Hao16743632013-05-08 10:59:04 -0700544 if (LIKELY(!method->IsNative())) {
Jeff Hao69510672013-05-21 17:34:55 -0700545 result->SetJ(Execute(self, mh, code_item, *shadow_frame, JValue()).GetJ());
Jeff Hao16743632013-05-08 10:59:04 -0700546 } else {
547 // We don't expect to be asked to interpret native code (which is entered via a JNI compiler
548 // generated stub) except during testing and image writing.
549 CHECK(!Runtime::Current()->IsStarted());
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700550 Object* receiver = method->IsStatic() ? nullptr : shadow_frame->GetVRegReference(0);
Jeff Hao16743632013-05-08 10:59:04 -0700551 uint32_t* args = shadow_frame->GetVRegArgs(method->IsStatic() ? 0 : 1);
Jeff Hao69510672013-05-21 17:34:55 -0700552 UnstartedRuntimeJni(self, method, receiver, args, result);
Jeff Hao16743632013-05-08 10:59:04 -0700553 }
554
555 self->PopShadowFrame();
Jeff Hao16743632013-05-08 10:59:04 -0700556}
557
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700558} // namespace interpreter
559} // namespace art