blob: 20e2b8da1956cf0489788ec45bb40603d3742d66 [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();
36 mirror::Class* array_class = runtime->GetClassLinker()->FindArrayClass(self, element_class);
37 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()") {
52 result->SetL(receiver->AsClass()->ComputeName());
53 } else if (name == "int java.lang.Float.floatToRawIntBits(float)") {
Jeff Hao5d917302013-02-27 17:57:33 -080054 result->SetI(args[0]);
Ian Rogers64b6d142012-10-29 16:34:15 -070055 } else if (name == "float java.lang.Float.intBitsToFloat(int)") {
Jeff Hao5d917302013-02-27 17:57:33 -080056 result->SetI(args[0]);
Ian Rogers64b6d142012-10-29 16:34:15 -070057 } else if (name == "double java.lang.Math.exp(double)") {
Jeff Hao5d917302013-02-27 17:57:33 -080058 JValue value;
59 value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]);
60 result->SetD(exp(value.GetD()));
Ian Rogers64b6d142012-10-29 16:34:15 -070061 } else if (name == "java.lang.Object java.lang.Object.internalClone()") {
62 result->SetL(receiver->Clone(self));
63 } else if (name == "void java.lang.Object.notifyAll()") {
Ian Rogers05f30572013-02-20 12:13:11 -080064 receiver->NotifyAll(self);
Ian Rogers64b6d142012-10-29 16:34:15 -070065 } else if (name == "int java.lang.String.compareTo(java.lang.String)") {
Jeff Hao5d917302013-02-27 17:57:33 -080066 String* rhs = reinterpret_cast<Object*>(args[0])->AsString();
Ian Rogers64b6d142012-10-29 16:34:15 -070067 CHECK(rhs != NULL);
68 result->SetI(receiver->AsString()->CompareTo(rhs));
69 } else if (name == "java.lang.String java.lang.String.intern()") {
70 result->SetL(receiver->AsString()->Intern());
71 } else if (name == "int java.lang.String.fastIndexOf(int, int)") {
Jeff Hao5d917302013-02-27 17:57:33 -080072 result->SetI(receiver->AsString()->FastIndexOf(args[0], args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -070073 } else if (name == "java.lang.Object java.lang.reflect.Array.createMultiArray(java.lang.Class, int[])") {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070074 StackHandleScope<2> hs(self);
75 auto h_class(hs.NewHandle(reinterpret_cast<mirror::Class*>(args[0])->AsClass()));
76 auto h_dimensions(hs.NewHandle(reinterpret_cast<mirror::IntArray*>(args[1])->AsIntArray()));
77 result->SetL(Array::CreateMultiArray(self, h_class, h_dimensions));
Ian Rogers64b6d142012-10-29 16:34:15 -070078 } else if (name == "java.lang.Object java.lang.Throwable.nativeFillInStackTrace()") {
79 ScopedObjectAccessUnchecked soa(self);
Ian Rogers5d27faf2014-05-02 17:17:18 -070080 if (Runtime::Current()->IsActiveTransaction()) {
81 result->SetL(soa.Decode<Object*>(self->CreateInternalStackTrace<true>(soa)));
82 } else {
83 result->SetL(soa.Decode<Object*>(self->CreateInternalStackTrace<false>(soa)));
84 }
Sebastien Hertzf48644b2014-02-17 15:16:03 +010085 } else if (name == "int java.lang.System.identityHashCode(java.lang.Object)") {
86 mirror::Object* obj = reinterpret_cast<Object*>(args[0]);
87 result->SetI((obj != nullptr) ? obj->IdentityHashCode() : 0);
Ian Rogers64b6d142012-10-29 16:34:15 -070088 } else if (name == "boolean java.nio.ByteOrder.isLittleEndian()") {
Sebastien Hertzf48644b2014-02-17 15:16:03 +010089 result->SetZ(JNI_TRUE);
Ian Rogers64b6d142012-10-29 16:34:15 -070090 } else if (name == "boolean sun.misc.Unsafe.compareAndSwapInt(java.lang.Object, long, int, int)") {
Jeff Hao5d917302013-02-27 17:57:33 -080091 Object* obj = reinterpret_cast<Object*>(args[0]);
92 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
93 jint expectedValue = args[3];
94 jint newValue = args[4];
Ian Rogers5d27faf2014-05-02 17:17:18 -070095 bool success;
96 if (Runtime::Current()->IsActiveTransaction()) {
97 success = obj->CasField32<true>(MemberOffset(offset), expectedValue, newValue);
98 } else {
99 success = obj->CasField32<false>(MemberOffset(offset), expectedValue, newValue);
100 }
Sebastien Hertzf48644b2014-02-17 15:16:03 +0100101 result->SetZ(success ? JNI_TRUE : JNI_FALSE);
Ian Rogers64b6d142012-10-29 16:34:15 -0700102 } else if (name == "void sun.misc.Unsafe.putObject(java.lang.Object, long, java.lang.Object)") {
Jeff Hao5d917302013-02-27 17:57:33 -0800103 Object* obj = reinterpret_cast<Object*>(args[0]);
Sebastien Hertzf48644b2014-02-17 15:16:03 +0100104 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
Jeff Hao5d917302013-02-27 17:57:33 -0800105 Object* newValue = reinterpret_cast<Object*>(args[3]);
Ian Rogers5d27faf2014-05-02 17:17:18 -0700106 if (Runtime::Current()->IsActiveTransaction()) {
107 obj->SetFieldObject<true>(MemberOffset(offset), newValue);
108 } else {
109 obj->SetFieldObject<false>(MemberOffset(offset), newValue);
110 }
Hiroshi Yamauchi4d2efce2014-02-10 16:19:09 -0800111 } else if (name == "int sun.misc.Unsafe.getArrayBaseOffsetForComponentType(java.lang.Class)") {
112 mirror::Class* component = reinterpret_cast<Object*>(args[0])->AsClass();
113 Primitive::Type primitive_type = component->GetPrimitiveType();
114 result->SetI(mirror::Array::DataOffset(Primitive::ComponentSize(primitive_type)).Int32Value());
115 } else if (name == "int sun.misc.Unsafe.getArrayIndexScaleForComponentType(java.lang.Class)") {
116 mirror::Class* component = reinterpret_cast<Object*>(args[0])->AsClass();
117 Primitive::Type primitive_type = component->GetPrimitiveType();
118 result->SetI(Primitive::ComponentSize(primitive_type));
Ian Rogers5d27faf2014-05-02 17:17:18 -0700119 } else if (Runtime::Current()->IsActiveTransaction()) {
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700120 AbortTransaction(self, "Attempt to invoke native method in non-started runtime: %s",
121 name.c_str());
Ian Rogers5d27faf2014-05-02 17:17:18 -0700122
123 } else {
124 LOG(FATAL) << "Calling native method " << PrettyMethod(method) << " in an unstarted "
125 "non-transactional runtime";
Ian Rogers64b6d142012-10-29 16:34:15 -0700126 }
127}
128
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700129static void InterpreterJni(Thread* self, ArtMethod* method, const StringPiece& shorty,
Jeff Hao5d917302013-02-27 17:57:33 -0800130 Object* receiver, uint32_t* args, JValue* result)
Ian Rogers64b6d142012-10-29 16:34:15 -0700131 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
132 // TODO: The following enters JNI code using a typedef-ed function rather than the JNI compiler,
133 // it should be removed and JNI compiled stubs used instead.
134 ScopedObjectAccessUnchecked soa(self);
135 if (method->IsStatic()) {
136 if (shorty == "L") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100137 typedef jobject (fntype)(JNIEnv*, jclass);
138 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700139 ScopedLocalRef<jclass> klass(soa.Env(),
140 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
Ian Rogers556d6372012-11-20 12:19:36 -0800141 jobject jresult;
142 {
143 ScopedThreadStateChange tsc(self, kNative);
144 jresult = fn(soa.Env(), klass.get());
145 }
146 result->SetL(soa.Decode<Object*>(jresult));
Ian Rogers64b6d142012-10-29 16:34:15 -0700147 } else if (shorty == "V") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100148 typedef void (fntype)(JNIEnv*, jclass);
149 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700150 ScopedLocalRef<jclass> klass(soa.Env(),
151 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
152 ScopedThreadStateChange tsc(self, kNative);
153 fn(soa.Env(), klass.get());
154 } else if (shorty == "Z") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100155 typedef jboolean (fntype)(JNIEnv*, jclass);
156 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700157 ScopedLocalRef<jclass> klass(soa.Env(),
158 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
159 ScopedThreadStateChange tsc(self, kNative);
160 result->SetZ(fn(soa.Env(), klass.get()));
161 } else if (shorty == "BI") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100162 typedef jbyte (fntype)(JNIEnv*, jclass, jint);
163 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700164 ScopedLocalRef<jclass> klass(soa.Env(),
165 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
166 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800167 result->SetB(fn(soa.Env(), klass.get(), args[0]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700168 } else if (shorty == "II") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100169 typedef jint (fntype)(JNIEnv*, jclass, jint);
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]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700175 } else if (shorty == "LL") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100176 typedef jobject (fntype)(JNIEnv*, jclass, jobject);
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 Rogers556d6372012-11-20 12:19:36 -0800182 jobject jresult;
183 {
184 ScopedThreadStateChange tsc(self, kNative);
185 jresult = fn(soa.Env(), klass.get(), arg0.get());
186 }
187 result->SetL(soa.Decode<Object*>(jresult));
Ian Rogers64b6d142012-10-29 16:34:15 -0700188 } else if (shorty == "IIZ") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100189 typedef jint (fntype)(JNIEnv*, jclass, jint, jboolean);
190 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700191 ScopedLocalRef<jclass> klass(soa.Env(),
192 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
193 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800194 result->SetI(fn(soa.Env(), klass.get(), args[0], args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700195 } else if (shorty == "ILI") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100196 typedef jint (fntype)(JNIEnv*, jclass, jobject, jint);
197 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700198 ScopedLocalRef<jclass> klass(soa.Env(),
199 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
200 ScopedLocalRef<jobject> arg0(soa.Env(),
Jeff Hao5d917302013-02-27 17:57:33 -0800201 soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[0])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700202 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800203 result->SetI(fn(soa.Env(), klass.get(), arg0.get(), args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700204 } else if (shorty == "SIZ") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100205 typedef jshort (fntype)(JNIEnv*, jclass, jint, jboolean);
206 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700207 ScopedLocalRef<jclass> klass(soa.Env(),
208 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
209 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800210 result->SetS(fn(soa.Env(), klass.get(), args[0], args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700211 } else if (shorty == "VIZ") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100212 typedef void (fntype)(JNIEnv*, jclass, jint, jboolean);
213 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700214 ScopedLocalRef<jclass> klass(soa.Env(),
215 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
216 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800217 fn(soa.Env(), klass.get(), args[0], args[1]);
Ian Rogers64b6d142012-10-29 16:34:15 -0700218 } else if (shorty == "ZLL") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100219 typedef jboolean (fntype)(JNIEnv*, jclass, jobject, jobject);
220 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700221 ScopedLocalRef<jclass> klass(soa.Env(),
222 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
223 ScopedLocalRef<jobject> arg0(soa.Env(),
Jeff Hao5d917302013-02-27 17:57:33 -0800224 soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[0])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700225 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);
228 result->SetZ(fn(soa.Env(), klass.get(), arg0.get(), arg1.get()));
229 } else if (shorty == "ZILL") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100230 typedef jboolean (fntype)(JNIEnv*, jclass, jint, jobject, jobject);
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> arg1(soa.Env(),
Jeff Hao5d917302013-02-27 17:57:33 -0800235 soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[1])));
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 result->SetZ(fn(soa.Env(), klass.get(), args[0], arg1.get(), arg2.get()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700240 } else if (shorty == "VILII") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100241 typedef void (fntype)(JNIEnv*, jclass, jint, jobject, jint, jint);
242 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700243 ScopedLocalRef<jclass> klass(soa.Env(),
244 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
245 ScopedLocalRef<jobject> arg1(soa.Env(),
Jeff Hao5d917302013-02-27 17:57:33 -0800246 soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[1])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700247 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800248 fn(soa.Env(), klass.get(), args[0], arg1.get(), args[2], args[3]);
Ian Rogers64b6d142012-10-29 16:34:15 -0700249 } else if (shorty == "VLILII") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100250 typedef void (fntype)(JNIEnv*, jclass, jobject, jint, jobject, jint, jint);
251 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700252 ScopedLocalRef<jclass> klass(soa.Env(),
253 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
254 ScopedLocalRef<jobject> arg0(soa.Env(),
Jeff Hao5d917302013-02-27 17:57:33 -0800255 soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[0])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700256 ScopedLocalRef<jobject> arg2(soa.Env(),
Jeff Hao5d917302013-02-27 17:57:33 -0800257 soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[2])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700258 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800259 fn(soa.Env(), klass.get(), arg0.get(), args[1], arg2.get(), args[3], args[4]);
Ian Rogers64b6d142012-10-29 16:34:15 -0700260 } else {
261 LOG(FATAL) << "Do something with static native method: " << PrettyMethod(method)
262 << " shorty: " << shorty;
263 }
264 } else {
265 if (shorty == "L") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100266 typedef jobject (fntype)(JNIEnv*, jobject);
267 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700268 ScopedLocalRef<jobject> rcvr(soa.Env(),
269 soa.AddLocalReference<jobject>(receiver));
Ian Rogers556d6372012-11-20 12:19:36 -0800270 jobject jresult;
271 {
272 ScopedThreadStateChange tsc(self, kNative);
273 jresult = fn(soa.Env(), rcvr.get());
274 }
275 result->SetL(soa.Decode<Object*>(jresult));
Jeff Hao3dd9f762013-07-08 13:09:25 -0700276 } else if (shorty == "V") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100277 typedef void (fntype)(JNIEnv*, jobject);
278 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Jeff Hao3dd9f762013-07-08 13:09:25 -0700279 ScopedLocalRef<jobject> rcvr(soa.Env(),
280 soa.AddLocalReference<jobject>(receiver));
281 ScopedThreadStateChange tsc(self, kNative);
282 fn(soa.Env(), rcvr.get());
Ian Rogers64b6d142012-10-29 16:34:15 -0700283 } else if (shorty == "LL") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100284 typedef jobject (fntype)(JNIEnv*, jobject, jobject);
285 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700286 ScopedLocalRef<jobject> rcvr(soa.Env(),
287 soa.AddLocalReference<jobject>(receiver));
288 ScopedLocalRef<jobject> arg0(soa.Env(),
Jeff Hao5d917302013-02-27 17:57:33 -0800289 soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[0])));
Ian Rogers556d6372012-11-20 12:19:36 -0800290 jobject jresult;
291 {
292 ScopedThreadStateChange tsc(self, kNative);
293 jresult = fn(soa.Env(), rcvr.get(), arg0.get());
Ian Rogers556d6372012-11-20 12:19:36 -0800294 }
295 result->SetL(soa.Decode<Object*>(jresult));
Ian Rogers64b6d142012-10-29 16:34:15 -0700296 ScopedThreadStateChange tsc(self, kNative);
Ian Rogers64b6d142012-10-29 16:34:15 -0700297 } else if (shorty == "III") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100298 typedef jint (fntype)(JNIEnv*, jobject, jint, jint);
299 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetNativeMethod()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700300 ScopedLocalRef<jobject> rcvr(soa.Env(),
301 soa.AddLocalReference<jobject>(receiver));
302 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800303 result->SetI(fn(soa.Env(), rcvr.get(), args[0], args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700304 } else {
305 LOG(FATAL) << "Do something with native method: " << PrettyMethod(method)
306 << " shorty: " << shorty;
307 }
308 }
309}
310
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200311enum InterpreterImplKind {
Ian Rogersb48b9eb2014-02-28 16:20:21 -0800312 kSwitchImpl, // Switch-based interpreter implementation.
313 kComputedGotoImplKind // Computed-goto-based interpreter implementation.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200314};
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700315
Ian Rogersb48b9eb2014-02-28 16:20:21 -0800316#if !defined(__clang__)
317static constexpr InterpreterImplKind kInterpreterImplKind = kComputedGotoImplKind;
318#else
319// Clang 3.4 fails to build the goto interpreter implementation.
320static constexpr InterpreterImplKind kInterpreterImplKind = kSwitchImpl;
321template<bool do_access_check, bool transaction_active>
322JValue ExecuteGotoImpl(Thread* self, MethodHelper& mh, const DexFile::CodeItem* code_item,
323 ShadowFrame& shadow_frame, JValue result_register) {
324 LOG(FATAL) << "UNREACHABLE";
325 exit(0);
326}
327// Explicit definitions of ExecuteGotoImpl.
Stephen Hines861ea562014-04-23 16:03:57 -0700328template<> SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogersb48b9eb2014-02-28 16:20:21 -0800329JValue ExecuteGotoImpl<true, false>(Thread* self, MethodHelper& mh,
330 const DexFile::CodeItem* code_item,
331 ShadowFrame& shadow_frame, JValue result_register);
Stephen Hines861ea562014-04-23 16:03:57 -0700332template<> SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogersb48b9eb2014-02-28 16:20:21 -0800333JValue ExecuteGotoImpl<false, false>(Thread* self, MethodHelper& mh,
334 const DexFile::CodeItem* code_item,
335 ShadowFrame& shadow_frame, JValue result_register);
Stephen Hines861ea562014-04-23 16:03:57 -0700336template<> SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogersb48b9eb2014-02-28 16:20:21 -0800337JValue ExecuteGotoImpl<true, true>(Thread* self, MethodHelper& mh,
338 const DexFile::CodeItem* code_item,
339 ShadowFrame& shadow_frame, JValue result_register);
Stephen Hines861ea562014-04-23 16:03:57 -0700340template<> SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogersb48b9eb2014-02-28 16:20:21 -0800341JValue ExecuteGotoImpl<false, true>(Thread* self, MethodHelper& mh,
342 const DexFile::CodeItem* code_item,
343 ShadowFrame& shadow_frame, JValue result_register);
344#endif
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700345
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200346static JValue Execute(Thread* self, MethodHelper& mh, const DexFile::CodeItem* code_item,
347 ShadowFrame& shadow_frame, JValue result_register)
348 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
349
350static inline JValue Execute(Thread* self, MethodHelper& mh, const DexFile::CodeItem* code_item,
351 ShadowFrame& shadow_frame, JValue result_register) {
Ian Rogers848871b2013-08-05 10:56:33 -0700352 DCHECK(shadow_frame.GetMethod() == mh.GetMethod() ||
353 shadow_frame.GetMethod()->GetDeclaringClass()->IsProxyClass());
354 DCHECK(!shadow_frame.GetMethod()->IsAbstract());
355 DCHECK(!shadow_frame.GetMethod()->IsNative());
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200356
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100357 bool transaction_active = Runtime::Current()->IsActiveTransaction();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200358 if (LIKELY(shadow_frame.GetMethod()->IsPreverified())) {
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200359 // Enter the "without access check" interpreter.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200360 if (kInterpreterImplKind == kSwitchImpl) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100361 if (transaction_active) {
362 return ExecuteSwitchImpl<false, true>(self, mh, code_item, shadow_frame, result_register);
363 } else {
364 return ExecuteSwitchImpl<false, false>(self, mh, code_item, shadow_frame, result_register);
365 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200366 } else {
367 DCHECK_EQ(kInterpreterImplKind, kComputedGotoImplKind);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100368 if (transaction_active) {
369 return ExecuteGotoImpl<false, true>(self, mh, code_item, shadow_frame, result_register);
370 } else {
371 return ExecuteGotoImpl<false, false>(self, mh, code_item, shadow_frame, result_register);
372 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200373 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200374 } else {
375 // Enter the "with access check" interpreter.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200376 if (kInterpreterImplKind == kSwitchImpl) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100377 if (transaction_active) {
378 return ExecuteSwitchImpl<true, true>(self, mh, code_item, shadow_frame, result_register);
379 } else {
380 return ExecuteSwitchImpl<true, false>(self, mh, code_item, shadow_frame, result_register);
381 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200382 } else {
383 DCHECK_EQ(kInterpreterImplKind, kComputedGotoImplKind);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100384 if (transaction_active) {
385 return ExecuteGotoImpl<true, true>(self, mh, code_item, shadow_frame, result_register);
386 } else {
387 return ExecuteGotoImpl<true, false>(self, mh, code_item, shadow_frame, result_register);
388 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200389 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200390 }
391}
392
Brian Carlstromea46f952013-07-30 01:26:50 -0700393void EnterInterpreterFromInvoke(Thread* self, ArtMethod* method, Object* receiver,
Jeff Hao6474d192013-03-26 14:08:09 -0700394 uint32_t* args, JValue* result) {
Ian Rogers64b6d142012-10-29 16:34:15 -0700395 DCHECK_EQ(self, Thread::Current());
Jeff Hao790ad902013-05-22 15:02:08 -0700396 if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEnd())) {
jeffhaod7521322012-11-21 15:38:24 -0800397 ThrowStackOverflowError(self);
398 return;
399 }
400
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700401 const char* old_cause = self->StartAssertNoThreadSuspension("EnterInterpreterFromInvoke");
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700402 MethodHelper mh(method);
403 const DexFile::CodeItem* code_item = mh.GetCodeItem();
404 uint16_t num_regs;
405 uint16_t num_ins;
406 if (code_item != NULL) {
407 num_regs = code_item->registers_size_;
408 num_ins = code_item->ins_size_;
jeffhao0a9bb732012-11-26 12:28:49 -0800409 } else if (method->IsAbstract()) {
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700410 self->EndAssertNoThreadSuspension(old_cause);
Sebastien Hertz56adf602013-07-09 17:27:07 +0200411 ThrowAbstractMethodError(method);
jeffhao0a9bb732012-11-26 12:28:49 -0800412 return;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700413 } else {
414 DCHECK(method->IsNative());
Brian Carlstromea46f952013-07-30 01:26:50 -0700415 num_regs = num_ins = ArtMethod::NumArgRegisters(mh.GetShorty());
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700416 if (!method->IsStatic()) {
417 num_regs++;
418 num_ins++;
419 }
420 }
421 // Set up shadow frame with matching number of reference slots to vregs.
422 ShadowFrame* last_shadow_frame = self->GetManagedStack()->GetTopShadowFrame();
Jeff Hao66135192013-05-14 11:02:41 -0700423 void* memory = alloca(ShadowFrame::ComputeSize(num_regs));
424 ShadowFrame* shadow_frame(ShadowFrame::Create(num_regs, last_shadow_frame, method, 0, memory));
425 self->PushShadowFrame(shadow_frame);
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700426
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700427 size_t cur_reg = num_regs - num_ins;
428 if (!method->IsStatic()) {
429 CHECK(receiver != NULL);
TDYa127ce4cc0d2012-11-18 16:59:53 -0800430 shadow_frame->SetVRegReference(cur_reg, receiver);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700431 ++cur_reg;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700432 }
Ian Rogers64b6d142012-10-29 16:34:15 -0700433 const char* shorty = mh.GetShorty();
Jeff Hao5d917302013-02-27 17:57:33 -0800434 for (size_t shorty_pos = 0, arg_pos = 0; cur_reg < num_regs; ++shorty_pos, ++arg_pos, cur_reg++) {
435 DCHECK_LT(shorty_pos + 1, mh.GetShortyLength());
436 switch (shorty[shorty_pos + 1]) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700437 case 'L': {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800438 Object* o = reinterpret_cast<StackReference<Object>*>(&args[arg_pos])->AsMirrorPtr();
TDYa127ce4cc0d2012-11-18 16:59:53 -0800439 shadow_frame->SetVRegReference(cur_reg, o);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700440 break;
441 }
Jeff Hao5d917302013-02-27 17:57:33 -0800442 case 'J': case 'D': {
443 uint64_t wide_value = (static_cast<uint64_t>(args[arg_pos + 1]) << 32) | args[arg_pos];
444 shadow_frame->SetVRegLong(cur_reg, wide_value);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700445 cur_reg++;
Jeff Hao5d917302013-02-27 17:57:33 -0800446 arg_pos++;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700447 break;
Jeff Hao5d917302013-02-27 17:57:33 -0800448 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700449 default:
Jeff Hao5d917302013-02-27 17:57:33 -0800450 shadow_frame->SetVReg(cur_reg, args[arg_pos]);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700451 break;
452 }
453 }
Mathieu Chartier92246bb2014-02-25 18:22:39 -0800454 self->EndAssertNoThreadSuspension(old_cause);
455 // Do this after populating the shadow frame in case EnsureInitialized causes a GC.
456 if (method->IsStatic() && UNLIKELY(!method->GetDeclaringClass()->IsInitializing())) {
457 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700458 StackHandleScope<1> hs(self);
459 Handle<mirror::Class> h_class(hs.NewHandle(method->GetDeclaringClass()));
460 if (UNLIKELY(!class_linker->EnsureInitialized(h_class, true, true))) {
Mathieu Chartier92246bb2014-02-25 18:22:39 -0800461 CHECK(self->IsExceptionPending());
462 self->PopShadowFrame();
463 return;
464 }
465 }
Ian Rogers64b6d142012-10-29 16:34:15 -0700466 if (LIKELY(!method->IsNative())) {
Jeff Hao66135192013-05-14 11:02:41 -0700467 JValue r = Execute(self, mh, code_item, *shadow_frame, JValue());
Jeff Hao6474d192013-03-26 14:08:09 -0700468 if (result != NULL) {
469 *result = r;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700470 }
471 } else {
Ian Rogers64b6d142012-10-29 16:34:15 -0700472 // We don't expect to be asked to interpret native code (which is entered via a JNI compiler
473 // generated stub) except during testing and image writing.
Mathieu Chartier92246bb2014-02-25 18:22:39 -0800474 // Update args to be the args in the shadow frame since the input ones could hold stale
475 // references pointers due to moving GC.
476 args = shadow_frame->GetVRegArgs(method->IsStatic() ? 0 : 1);
Ian Rogers64b6d142012-10-29 16:34:15 -0700477 if (!Runtime::Current()->IsStarted()) {
Jeff Hao6474d192013-03-26 14:08:09 -0700478 UnstartedRuntimeJni(self, method, receiver, args, result);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700479 } else {
Jeff Hao6474d192013-03-26 14:08:09 -0700480 InterpreterJni(self, method, shorty, receiver, args, result);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700481 }
482 }
483 self->PopShadowFrame();
484}
485
Ian Rogers62d6c772013-02-27 08:32:07 -0800486void EnterInterpreterFromDeoptimize(Thread* self, ShadowFrame* shadow_frame, JValue* ret_val)
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800487 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
488 JValue value;
Ian Rogers62d6c772013-02-27 08:32:07 -0800489 value.SetJ(ret_val->GetJ()); // Set value to last known result in case the shadow frame chain is empty.
490 MethodHelper mh;
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800491 while (shadow_frame != NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800492 self->SetTopOfShadowStack(shadow_frame);
493 mh.ChangeMethod(shadow_frame->GetMethod());
494 const DexFile::CodeItem* code_item = mh.GetCodeItem();
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800495 value = Execute(self, mh, code_item, *shadow_frame, value);
496 ShadowFrame* old_frame = shadow_frame;
497 shadow_frame = shadow_frame->GetLink();
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800498 delete old_frame;
499 }
500 ret_val->SetJ(value.GetJ());
501}
502
Ian Rogers7db619b2013-01-16 18:35:48 -0800503JValue EnterInterpreterFromStub(Thread* self, MethodHelper& mh, const DexFile::CodeItem* code_item,
Ian Rogers848871b2013-08-05 10:56:33 -0700504 ShadowFrame& shadow_frame) {
Ian Rogersf3e98552013-03-20 15:49:49 -0700505 DCHECK_EQ(self, Thread::Current());
Jeff Hao790ad902013-05-22 15:02:08 -0700506 if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEnd())) {
Ian Rogersf3e98552013-03-20 15:49:49 -0700507 ThrowStackOverflowError(self);
508 return JValue();
509 }
510
Ian Rogers7db619b2013-01-16 18:35:48 -0800511 return Execute(self, mh, code_item, shadow_frame, JValue());
512}
513
Ian Rogers848871b2013-08-05 10:56:33 -0700514extern "C" void artInterpreterToInterpreterBridge(Thread* self, MethodHelper& mh,
515 const DexFile::CodeItem* code_item,
516 ShadowFrame* shadow_frame, JValue* result) {
Jeff Hao790ad902013-05-22 15:02:08 -0700517 if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEnd())) {
Jeff Hao16743632013-05-08 10:59:04 -0700518 ThrowStackOverflowError(self);
Jeff Hao69510672013-05-21 17:34:55 -0700519 return;
Jeff Hao16743632013-05-08 10:59:04 -0700520 }
521
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700522 self->PushShadowFrame(shadow_frame);
Brian Carlstromea46f952013-07-30 01:26:50 -0700523 ArtMethod* method = shadow_frame->GetMethod();
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200524 // Ensure static methods are initialized.
525 if (method->IsStatic()) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700526 StackHandleScope<1> hs(self);
527 Handle<Class> declaringClass(hs.NewHandle(method->GetDeclaringClass()));
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200528 if (UNLIKELY(!declaringClass->IsInitializing())) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700529 if (UNLIKELY(!Runtime::Current()->GetClassLinker()->EnsureInitialized(declaringClass, true,
530 true))) {
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200531 DCHECK(Thread::Current()->IsExceptionPending());
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700532 self->PopShadowFrame();
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200533 return;
534 }
535 CHECK(declaringClass->IsInitializing());
Jeff Hao16743632013-05-08 10:59:04 -0700536 }
Jeff Hao16743632013-05-08 10:59:04 -0700537 }
Jeff Hao16743632013-05-08 10:59:04 -0700538
Jeff Hao16743632013-05-08 10:59:04 -0700539 if (LIKELY(!method->IsNative())) {
Jeff Hao69510672013-05-21 17:34:55 -0700540 result->SetJ(Execute(self, mh, code_item, *shadow_frame, JValue()).GetJ());
Jeff Hao16743632013-05-08 10:59:04 -0700541 } else {
542 // We don't expect to be asked to interpret native code (which is entered via a JNI compiler
543 // generated stub) except during testing and image writing.
544 CHECK(!Runtime::Current()->IsStarted());
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700545 Object* receiver = method->IsStatic() ? nullptr : shadow_frame->GetVRegReference(0);
Jeff Hao16743632013-05-08 10:59:04 -0700546 uint32_t* args = shadow_frame->GetVRegArgs(method->IsStatic() ? 0 : 1);
Jeff Hao69510672013-05-21 17:34:55 -0700547 UnstartedRuntimeJni(self, method, receiver, args, result);
Jeff Hao16743632013-05-08 10:59:04 -0700548 }
549
550 self->PopShadowFrame();
Jeff Hao16743632013-05-08 10:59:04 -0700551}
552
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700553} // namespace interpreter
554} // namespace art