blob: d7555ddb6a64bef494131ec74021cd1e930b5689 [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 Rogers2dd0e2c2013-01-24 12:42:14 -080018
Ian Rogers2fa6b2e2012-10-17 00:10:17 -070019namespace art {
20namespace interpreter {
21
Ian Rogers64b6d142012-10-29 16:34:15 -070022// 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 -070023static void UnstartedRuntimeJni(Thread* self, ArtMethod* method,
Jeff Hao5d917302013-02-27 17:57:33 -080024 Object* receiver, uint32_t* args, JValue* result)
Ian Rogers64b6d142012-10-29 16:34:15 -070025 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
26 std::string name(PrettyMethod(method));
27 if (name == "java.lang.ClassLoader dalvik.system.VMStack.getCallingClassLoader()") {
28 result->SetL(NULL);
29 } else if (name == "java.lang.Class dalvik.system.VMStack.getStackClass2()") {
Ian Rogers7a22fa62013-01-23 12:16:16 -080030 NthCallerVisitor visitor(self, 3);
Ian Rogers64b6d142012-10-29 16:34:15 -070031 visitor.WalkStack();
32 result->SetL(visitor.caller->GetDeclaringClass());
33 } else if (name == "double java.lang.Math.log(double)") {
Jeff Hao5d917302013-02-27 17:57:33 -080034 JValue value;
35 value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]);
36 result->SetD(log(value.GetD()));
Ian Rogers64b6d142012-10-29 16:34:15 -070037 } else if (name == "java.lang.String java.lang.Class.getNameNative()") {
38 result->SetL(receiver->AsClass()->ComputeName());
39 } else if (name == "int java.lang.Float.floatToRawIntBits(float)") {
Jeff Hao5d917302013-02-27 17:57:33 -080040 result->SetI(args[0]);
Ian Rogers64b6d142012-10-29 16:34:15 -070041 } else if (name == "float java.lang.Float.intBitsToFloat(int)") {
Jeff Hao5d917302013-02-27 17:57:33 -080042 result->SetI(args[0]);
Ian Rogers64b6d142012-10-29 16:34:15 -070043 } else if (name == "double java.lang.Math.exp(double)") {
Jeff Hao5d917302013-02-27 17:57:33 -080044 JValue value;
45 value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]);
46 result->SetD(exp(value.GetD()));
Ian Rogers64b6d142012-10-29 16:34:15 -070047 } else if (name == "java.lang.Object java.lang.Object.internalClone()") {
48 result->SetL(receiver->Clone(self));
49 } else if (name == "void java.lang.Object.notifyAll()") {
Ian Rogers05f30572013-02-20 12:13:11 -080050 receiver->NotifyAll(self);
Ian Rogers64b6d142012-10-29 16:34:15 -070051 } else if (name == "int java.lang.String.compareTo(java.lang.String)") {
Jeff Hao5d917302013-02-27 17:57:33 -080052 String* rhs = reinterpret_cast<Object*>(args[0])->AsString();
Ian Rogers64b6d142012-10-29 16:34:15 -070053 CHECK(rhs != NULL);
54 result->SetI(receiver->AsString()->CompareTo(rhs));
55 } else if (name == "java.lang.String java.lang.String.intern()") {
56 result->SetL(receiver->AsString()->Intern());
57 } else if (name == "int java.lang.String.fastIndexOf(int, int)") {
Jeff Hao5d917302013-02-27 17:57:33 -080058 result->SetI(receiver->AsString()->FastIndexOf(args[0], args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -070059 } else if (name == "java.lang.Object java.lang.reflect.Array.createMultiArray(java.lang.Class, int[])") {
Jeff Hao5d917302013-02-27 17:57:33 -080060 result->SetL(Array::CreateMultiArray(self, reinterpret_cast<Object*>(args[0])->AsClass(), reinterpret_cast<Object*>(args[1])->AsIntArray()));
Ian Rogers64b6d142012-10-29 16:34:15 -070061 } else if (name == "java.lang.Object java.lang.Throwable.nativeFillInStackTrace()") {
62 ScopedObjectAccessUnchecked soa(self);
63 result->SetL(soa.Decode<Object*>(self->CreateInternalStackTrace(soa)));
64 } else if (name == "boolean java.nio.ByteOrder.isLittleEndian()") {
65 result->SetJ(JNI_TRUE);
66 } else if (name == "boolean sun.misc.Unsafe.compareAndSwapInt(java.lang.Object, long, int, int)") {
Jeff Hao5d917302013-02-27 17:57:33 -080067 Object* obj = reinterpret_cast<Object*>(args[0]);
68 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
69 jint expectedValue = args[3];
70 jint newValue = args[4];
Ian Rogers64b6d142012-10-29 16:34:15 -070071 byte* raw_addr = reinterpret_cast<byte*>(obj) + offset;
72 volatile int32_t* address = reinterpret_cast<volatile int32_t*>(raw_addr);
73 // Note: android_atomic_release_cas() returns 0 on success, not failure.
74 int r = android_atomic_release_cas(expectedValue, newValue, address);
75 result->SetZ(r == 0);
76 } else if (name == "void sun.misc.Unsafe.putObject(java.lang.Object, long, java.lang.Object)") {
Jeff Hao5d917302013-02-27 17:57:33 -080077 Object* obj = reinterpret_cast<Object*>(args[0]);
78 Object* newValue = reinterpret_cast<Object*>(args[3]);
79 obj->SetFieldObject(MemberOffset((static_cast<uint64_t>(args[2]) << 32) | args[1]), newValue, false);
Ian Rogers64b6d142012-10-29 16:34:15 -070080 } else {
81 LOG(FATAL) << "Attempt to invoke native method in non-started runtime: " << name;
82 }
83}
84
Ian Rogersfc0e94b2013-09-23 23:51:32 -070085static void InterpreterJni(Thread* self, ArtMethod* method, const StringPiece& shorty,
Jeff Hao5d917302013-02-27 17:57:33 -080086 Object* receiver, uint32_t* args, JValue* result)
Ian Rogers64b6d142012-10-29 16:34:15 -070087 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
88 // TODO: The following enters JNI code using a typedef-ed function rather than the JNI compiler,
89 // it should be removed and JNI compiled stubs used instead.
90 ScopedObjectAccessUnchecked soa(self);
91 if (method->IsStatic()) {
92 if (shorty == "L") {
93 typedef jobject (fnptr)(JNIEnv*, jclass);
Ian Rogersd8274bc2013-05-15 15:54:45 -070094 const fnptr* fn = reinterpret_cast<const fnptr*>(method->GetNativeMethod());
Ian Rogers64b6d142012-10-29 16:34:15 -070095 ScopedLocalRef<jclass> klass(soa.Env(),
96 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
Ian Rogers556d6372012-11-20 12:19:36 -080097 jobject jresult;
98 {
99 ScopedThreadStateChange tsc(self, kNative);
100 jresult = fn(soa.Env(), klass.get());
101 }
102 result->SetL(soa.Decode<Object*>(jresult));
Ian Rogers64b6d142012-10-29 16:34:15 -0700103 } else if (shorty == "V") {
104 typedef void (fnptr)(JNIEnv*, jclass);
Ian Rogersd8274bc2013-05-15 15:54:45 -0700105 const fnptr* fn = reinterpret_cast<const fnptr*>(method->GetNativeMethod());
Ian Rogers64b6d142012-10-29 16:34:15 -0700106 ScopedLocalRef<jclass> klass(soa.Env(),
107 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
108 ScopedThreadStateChange tsc(self, kNative);
109 fn(soa.Env(), klass.get());
110 } else if (shorty == "Z") {
111 typedef jboolean (fnptr)(JNIEnv*, jclass);
Ian Rogersd8274bc2013-05-15 15:54:45 -0700112 const fnptr* fn = reinterpret_cast<const fnptr*>(method->GetNativeMethod());
Ian Rogers64b6d142012-10-29 16:34:15 -0700113 ScopedLocalRef<jclass> klass(soa.Env(),
114 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
115 ScopedThreadStateChange tsc(self, kNative);
116 result->SetZ(fn(soa.Env(), klass.get()));
117 } else if (shorty == "BI") {
118 typedef jbyte (fnptr)(JNIEnv*, jclass, jint);
Ian Rogersd8274bc2013-05-15 15:54:45 -0700119 const fnptr* fn = reinterpret_cast<const fnptr*>(method->GetNativeMethod());
Ian Rogers64b6d142012-10-29 16:34:15 -0700120 ScopedLocalRef<jclass> klass(soa.Env(),
121 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
122 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800123 result->SetB(fn(soa.Env(), klass.get(), args[0]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700124 } else if (shorty == "II") {
125 typedef jint (fnptr)(JNIEnv*, jclass, jint);
Ian Rogersd8274bc2013-05-15 15:54:45 -0700126 const fnptr* fn = reinterpret_cast<const fnptr*>(method->GetNativeMethod());
Ian Rogers64b6d142012-10-29 16:34:15 -0700127 ScopedLocalRef<jclass> klass(soa.Env(),
128 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
129 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800130 result->SetI(fn(soa.Env(), klass.get(), args[0]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700131 } else if (shorty == "LL") {
132 typedef jobject (fnptr)(JNIEnv*, jclass, jobject);
Ian Rogersd8274bc2013-05-15 15:54:45 -0700133 const fnptr* fn = reinterpret_cast<const fnptr*>(method->GetNativeMethod());
Ian Rogers64b6d142012-10-29 16:34:15 -0700134 ScopedLocalRef<jclass> klass(soa.Env(),
135 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
136 ScopedLocalRef<jobject> arg0(soa.Env(),
Jeff Hao5d917302013-02-27 17:57:33 -0800137 soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[0])));
Ian Rogers556d6372012-11-20 12:19:36 -0800138 jobject jresult;
139 {
140 ScopedThreadStateChange tsc(self, kNative);
141 jresult = fn(soa.Env(), klass.get(), arg0.get());
142 }
143 result->SetL(soa.Decode<Object*>(jresult));
Ian Rogers64b6d142012-10-29 16:34:15 -0700144 } else if (shorty == "IIZ") {
145 typedef jint (fnptr)(JNIEnv*, jclass, jint, jboolean);
Ian Rogersd8274bc2013-05-15 15:54:45 -0700146 const fnptr* fn = reinterpret_cast<const fnptr*>(method->GetNativeMethod());
Ian Rogers64b6d142012-10-29 16:34:15 -0700147 ScopedLocalRef<jclass> klass(soa.Env(),
148 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
149 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800150 result->SetI(fn(soa.Env(), klass.get(), args[0], args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700151 } else if (shorty == "ILI") {
152 typedef jint (fnptr)(JNIEnv*, jclass, jobject, jint);
Ian Rogersd8274bc2013-05-15 15:54:45 -0700153 const fnptr* fn = reinterpret_cast<const fnptr*>(method->GetNativeMethod());
Ian Rogers64b6d142012-10-29 16:34:15 -0700154 ScopedLocalRef<jclass> klass(soa.Env(),
155 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
156 ScopedLocalRef<jobject> arg0(soa.Env(),
Jeff Hao5d917302013-02-27 17:57:33 -0800157 soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[0])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700158 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800159 result->SetI(fn(soa.Env(), klass.get(), arg0.get(), args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700160 } else if (shorty == "SIZ") {
161 typedef jshort (fnptr)(JNIEnv*, jclass, jint, jboolean);
Ian Rogersd8274bc2013-05-15 15:54:45 -0700162 const fnptr* fn = reinterpret_cast<const fnptr*>(method->GetNativeMethod());
Ian Rogers64b6d142012-10-29 16:34:15 -0700163 ScopedLocalRef<jclass> klass(soa.Env(),
164 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
165 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800166 result->SetS(fn(soa.Env(), klass.get(), args[0], args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700167 } else if (shorty == "VIZ") {
168 typedef void (fnptr)(JNIEnv*, jclass, jint, jboolean);
Ian Rogersd8274bc2013-05-15 15:54:45 -0700169 const fnptr* fn = reinterpret_cast<const fnptr*>(method->GetNativeMethod());
Ian Rogers64b6d142012-10-29 16:34:15 -0700170 ScopedLocalRef<jclass> klass(soa.Env(),
171 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
172 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800173 fn(soa.Env(), klass.get(), args[0], args[1]);
Ian Rogers64b6d142012-10-29 16:34:15 -0700174 } else if (shorty == "ZLL") {
175 typedef jboolean (fnptr)(JNIEnv*, jclass, jobject, jobject);
Ian Rogersd8274bc2013-05-15 15:54:45 -0700176 const fnptr* fn = reinterpret_cast<const fnptr*>(method->GetNativeMethod());
Ian Rogers64b6d142012-10-29 16:34:15 -0700177 ScopedLocalRef<jclass> klass(soa.Env(),
178 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
179 ScopedLocalRef<jobject> arg0(soa.Env(),
Jeff Hao5d917302013-02-27 17:57:33 -0800180 soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[0])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700181 ScopedLocalRef<jobject> arg1(soa.Env(),
Jeff Hao5d917302013-02-27 17:57:33 -0800182 soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[1])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700183 ScopedThreadStateChange tsc(self, kNative);
184 result->SetZ(fn(soa.Env(), klass.get(), arg0.get(), arg1.get()));
185 } else if (shorty == "ZILL") {
186 typedef jboolean (fnptr)(JNIEnv*, jclass, jint, jobject, jobject);
Ian Rogersd8274bc2013-05-15 15:54:45 -0700187 const fnptr* fn = reinterpret_cast<const fnptr*>(method->GetNativeMethod());
Ian Rogers64b6d142012-10-29 16:34:15 -0700188 ScopedLocalRef<jclass> klass(soa.Env(),
189 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
190 ScopedLocalRef<jobject> arg1(soa.Env(),
Jeff Hao5d917302013-02-27 17:57:33 -0800191 soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[1])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700192 ScopedLocalRef<jobject> arg2(soa.Env(),
Jeff Hao5d917302013-02-27 17:57:33 -0800193 soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[2])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700194 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800195 result->SetZ(fn(soa.Env(), klass.get(), args[0], arg1.get(), arg2.get()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700196 } else if (shorty == "VILII") {
197 typedef void (fnptr)(JNIEnv*, jclass, jint, jobject, jint, jint);
Ian Rogersd8274bc2013-05-15 15:54:45 -0700198 const fnptr* fn = reinterpret_cast<const fnptr*>(method->GetNativeMethod());
Ian Rogers64b6d142012-10-29 16:34:15 -0700199 ScopedLocalRef<jclass> klass(soa.Env(),
200 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
201 ScopedLocalRef<jobject> arg1(soa.Env(),
Jeff Hao5d917302013-02-27 17:57:33 -0800202 soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[1])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700203 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800204 fn(soa.Env(), klass.get(), args[0], arg1.get(), args[2], args[3]);
Ian Rogers64b6d142012-10-29 16:34:15 -0700205 } else if (shorty == "VLILII") {
206 typedef void (fnptr)(JNIEnv*, jclass, jobject, jint, jobject, jint, jint);
Ian Rogersd8274bc2013-05-15 15:54:45 -0700207 const fnptr* fn = reinterpret_cast<const fnptr*>(method->GetNativeMethod());
Ian Rogers64b6d142012-10-29 16:34:15 -0700208 ScopedLocalRef<jclass> klass(soa.Env(),
209 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
210 ScopedLocalRef<jobject> arg0(soa.Env(),
Jeff Hao5d917302013-02-27 17:57:33 -0800211 soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[0])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700212 ScopedLocalRef<jobject> arg2(soa.Env(),
Jeff Hao5d917302013-02-27 17:57:33 -0800213 soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[2])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700214 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800215 fn(soa.Env(), klass.get(), arg0.get(), args[1], arg2.get(), args[3], args[4]);
Ian Rogers64b6d142012-10-29 16:34:15 -0700216 } else {
217 LOG(FATAL) << "Do something with static native method: " << PrettyMethod(method)
218 << " shorty: " << shorty;
219 }
220 } else {
221 if (shorty == "L") {
222 typedef jobject (fnptr)(JNIEnv*, jobject);
Ian Rogersd8274bc2013-05-15 15:54:45 -0700223 const fnptr* fn = reinterpret_cast<const fnptr*>(method->GetNativeMethod());
Ian Rogers64b6d142012-10-29 16:34:15 -0700224 ScopedLocalRef<jobject> rcvr(soa.Env(),
225 soa.AddLocalReference<jobject>(receiver));
Ian Rogers556d6372012-11-20 12:19:36 -0800226 jobject jresult;
227 {
228 ScopedThreadStateChange tsc(self, kNative);
229 jresult = fn(soa.Env(), rcvr.get());
230 }
231 result->SetL(soa.Decode<Object*>(jresult));
Jeff Hao3dd9f762013-07-08 13:09:25 -0700232 } else if (shorty == "V") {
233 typedef void (fnptr)(JNIEnv*, jobject);
234 const fnptr* fn = reinterpret_cast<const fnptr*>(method->GetNativeMethod());
235 ScopedLocalRef<jobject> rcvr(soa.Env(),
236 soa.AddLocalReference<jobject>(receiver));
237 ScopedThreadStateChange tsc(self, kNative);
238 fn(soa.Env(), rcvr.get());
Ian Rogers64b6d142012-10-29 16:34:15 -0700239 } else if (shorty == "LL") {
240 typedef jobject (fnptr)(JNIEnv*, jobject, jobject);
Ian Rogersd8274bc2013-05-15 15:54:45 -0700241 const fnptr* fn = reinterpret_cast<const fnptr*>(method->GetNativeMethod());
Ian Rogers64b6d142012-10-29 16:34:15 -0700242 ScopedLocalRef<jobject> rcvr(soa.Env(),
243 soa.AddLocalReference<jobject>(receiver));
244 ScopedLocalRef<jobject> arg0(soa.Env(),
Jeff Hao5d917302013-02-27 17:57:33 -0800245 soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[0])));
Ian Rogers556d6372012-11-20 12:19:36 -0800246 jobject jresult;
247 {
248 ScopedThreadStateChange tsc(self, kNative);
249 jresult = fn(soa.Env(), rcvr.get(), arg0.get());
Ian Rogers556d6372012-11-20 12:19:36 -0800250 }
251 result->SetL(soa.Decode<Object*>(jresult));
Ian Rogers64b6d142012-10-29 16:34:15 -0700252 ScopedThreadStateChange tsc(self, kNative);
Ian Rogers64b6d142012-10-29 16:34:15 -0700253 } else if (shorty == "III") {
254 typedef jint (fnptr)(JNIEnv*, jobject, jint, jint);
Ian Rogersd8274bc2013-05-15 15:54:45 -0700255 const fnptr* fn = reinterpret_cast<const fnptr*>(method->GetNativeMethod());
Ian Rogers64b6d142012-10-29 16:34:15 -0700256 ScopedLocalRef<jobject> rcvr(soa.Env(),
257 soa.AddLocalReference<jobject>(receiver));
258 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800259 result->SetI(fn(soa.Env(), rcvr.get(), args[0], args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700260 } else {
261 LOG(FATAL) << "Do something with native method: " << PrettyMethod(method)
262 << " shorty: " << shorty;
263 }
264 }
265}
266
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200267enum InterpreterImplKind {
268 kSwitchImpl, // switch-based interpreter implementation.
269 kComputedGotoImplKind // computed-goto-based interpreter implementation.
270};
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700271
Sebastien Hertz2c88b382013-10-18 10:35:39 +0200272static const InterpreterImplKind kInterpreterImplKind = kComputedGotoImplKind;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700273
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200274static JValue Execute(Thread* self, MethodHelper& mh, const DexFile::CodeItem* code_item,
275 ShadowFrame& shadow_frame, JValue result_register)
276 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
277
278static inline JValue Execute(Thread* self, MethodHelper& mh, const DexFile::CodeItem* code_item,
279 ShadowFrame& shadow_frame, JValue result_register) {
Ian Rogers848871b2013-08-05 10:56:33 -0700280 DCHECK(shadow_frame.GetMethod() == mh.GetMethod() ||
281 shadow_frame.GetMethod()->GetDeclaringClass()->IsProxyClass());
282 DCHECK(!shadow_frame.GetMethod()->IsAbstract());
283 DCHECK(!shadow_frame.GetMethod()->IsNative());
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200284
285 if (LIKELY(shadow_frame.GetMethod()->IsPreverified())) {
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200286 // Enter the "without access check" interpreter.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200287 if (kInterpreterImplKind == kSwitchImpl) {
288 return ExecuteSwitchImpl<false>(self, mh, code_item, shadow_frame, result_register);
289 } else {
290 DCHECK_EQ(kInterpreterImplKind, kComputedGotoImplKind);
291 return ExecuteGotoImpl<false>(self, mh, code_item, shadow_frame, result_register);
292 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200293 } else {
294 // Enter the "with access check" interpreter.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200295 if (kInterpreterImplKind == kSwitchImpl) {
296 return ExecuteSwitchImpl<true>(self, mh, code_item, shadow_frame, result_register);
297 } else {
298 DCHECK_EQ(kInterpreterImplKind, kComputedGotoImplKind);
299 return ExecuteGotoImpl<true>(self, mh, code_item, shadow_frame, result_register);
300 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200301 }
302}
303
Brian Carlstromea46f952013-07-30 01:26:50 -0700304void EnterInterpreterFromInvoke(Thread* self, ArtMethod* method, Object* receiver,
Jeff Hao6474d192013-03-26 14:08:09 -0700305 uint32_t* args, JValue* result) {
Ian Rogers64b6d142012-10-29 16:34:15 -0700306 DCHECK_EQ(self, Thread::Current());
Jeff Hao790ad902013-05-22 15:02:08 -0700307 if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEnd())) {
jeffhaod7521322012-11-21 15:38:24 -0800308 ThrowStackOverflowError(self);
309 return;
310 }
311
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700312 const char* old_cause = self->StartAssertNoThreadSuspension("EnterInterpreterFromInvoke");
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700313 MethodHelper mh(method);
314 const DexFile::CodeItem* code_item = mh.GetCodeItem();
315 uint16_t num_regs;
316 uint16_t num_ins;
317 if (code_item != NULL) {
318 num_regs = code_item->registers_size_;
319 num_ins = code_item->ins_size_;
jeffhao0a9bb732012-11-26 12:28:49 -0800320 } else if (method->IsAbstract()) {
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700321 self->EndAssertNoThreadSuspension(old_cause);
Sebastien Hertz56adf602013-07-09 17:27:07 +0200322 ThrowAbstractMethodError(method);
jeffhao0a9bb732012-11-26 12:28:49 -0800323 return;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700324 } else {
325 DCHECK(method->IsNative());
Brian Carlstromea46f952013-07-30 01:26:50 -0700326 num_regs = num_ins = ArtMethod::NumArgRegisters(mh.GetShorty());
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700327 if (!method->IsStatic()) {
328 num_regs++;
329 num_ins++;
330 }
331 }
332 // Set up shadow frame with matching number of reference slots to vregs.
333 ShadowFrame* last_shadow_frame = self->GetManagedStack()->GetTopShadowFrame();
Jeff Hao66135192013-05-14 11:02:41 -0700334 void* memory = alloca(ShadowFrame::ComputeSize(num_regs));
335 ShadowFrame* shadow_frame(ShadowFrame::Create(num_regs, last_shadow_frame, method, 0, memory));
336 self->PushShadowFrame(shadow_frame);
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700337 self->EndAssertNoThreadSuspension(old_cause);
338
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700339 size_t cur_reg = num_regs - num_ins;
340 if (!method->IsStatic()) {
341 CHECK(receiver != NULL);
TDYa127ce4cc0d2012-11-18 16:59:53 -0800342 shadow_frame->SetVRegReference(cur_reg, receiver);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700343 ++cur_reg;
Sebastien Hertz807a2562013-04-15 09:33:39 +0200344 } else if (UNLIKELY(!method->GetDeclaringClass()->IsInitializing())) {
345 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700346 if (UNLIKELY(!class_linker->EnsureInitialized(method->GetDeclaringClass(), true, true))) {
Sebastien Hertz807a2562013-04-15 09:33:39 +0200347 CHECK(self->IsExceptionPending());
348 self->PopShadowFrame();
jeffhao94d6df42012-11-26 16:02:12 -0800349 return;
350 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700351 CHECK(method->GetDeclaringClass()->IsInitializing());
352 }
Ian Rogers64b6d142012-10-29 16:34:15 -0700353 const char* shorty = mh.GetShorty();
Jeff Hao5d917302013-02-27 17:57:33 -0800354 for (size_t shorty_pos = 0, arg_pos = 0; cur_reg < num_regs; ++shorty_pos, ++arg_pos, cur_reg++) {
355 DCHECK_LT(shorty_pos + 1, mh.GetShortyLength());
356 switch (shorty[shorty_pos + 1]) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700357 case 'L': {
Jeff Hao5d917302013-02-27 17:57:33 -0800358 Object* o = reinterpret_cast<Object*>(args[arg_pos]);
TDYa127ce4cc0d2012-11-18 16:59:53 -0800359 shadow_frame->SetVRegReference(cur_reg, o);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700360 break;
361 }
Jeff Hao5d917302013-02-27 17:57:33 -0800362 case 'J': case 'D': {
363 uint64_t wide_value = (static_cast<uint64_t>(args[arg_pos + 1]) << 32) | args[arg_pos];
364 shadow_frame->SetVRegLong(cur_reg, wide_value);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700365 cur_reg++;
Jeff Hao5d917302013-02-27 17:57:33 -0800366 arg_pos++;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700367 break;
Jeff Hao5d917302013-02-27 17:57:33 -0800368 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700369 default:
Jeff Hao5d917302013-02-27 17:57:33 -0800370 shadow_frame->SetVReg(cur_reg, args[arg_pos]);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700371 break;
372 }
373 }
Ian Rogers64b6d142012-10-29 16:34:15 -0700374 if (LIKELY(!method->IsNative())) {
Jeff Hao66135192013-05-14 11:02:41 -0700375 JValue r = Execute(self, mh, code_item, *shadow_frame, JValue());
Jeff Hao6474d192013-03-26 14:08:09 -0700376 if (result != NULL) {
377 *result = r;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700378 }
379 } else {
Ian Rogers64b6d142012-10-29 16:34:15 -0700380 // We don't expect to be asked to interpret native code (which is entered via a JNI compiler
381 // generated stub) except during testing and image writing.
382 if (!Runtime::Current()->IsStarted()) {
Jeff Hao6474d192013-03-26 14:08:09 -0700383 UnstartedRuntimeJni(self, method, receiver, args, result);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700384 } else {
Jeff Hao6474d192013-03-26 14:08:09 -0700385 InterpreterJni(self, method, shorty, receiver, args, result);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700386 }
387 }
388 self->PopShadowFrame();
389}
390
Ian Rogers62d6c772013-02-27 08:32:07 -0800391void EnterInterpreterFromDeoptimize(Thread* self, ShadowFrame* shadow_frame, JValue* ret_val)
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800392 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
393 JValue value;
Ian Rogers62d6c772013-02-27 08:32:07 -0800394 value.SetJ(ret_val->GetJ()); // Set value to last known result in case the shadow frame chain is empty.
395 MethodHelper mh;
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800396 while (shadow_frame != NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800397 self->SetTopOfShadowStack(shadow_frame);
398 mh.ChangeMethod(shadow_frame->GetMethod());
399 const DexFile::CodeItem* code_item = mh.GetCodeItem();
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800400 value = Execute(self, mh, code_item, *shadow_frame, value);
401 ShadowFrame* old_frame = shadow_frame;
402 shadow_frame = shadow_frame->GetLink();
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800403 delete old_frame;
404 }
405 ret_val->SetJ(value.GetJ());
406}
407
Ian Rogers7db619b2013-01-16 18:35:48 -0800408JValue EnterInterpreterFromStub(Thread* self, MethodHelper& mh, const DexFile::CodeItem* code_item,
Ian Rogers848871b2013-08-05 10:56:33 -0700409 ShadowFrame& shadow_frame) {
Ian Rogersf3e98552013-03-20 15:49:49 -0700410 DCHECK_EQ(self, Thread::Current());
Jeff Hao790ad902013-05-22 15:02:08 -0700411 if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEnd())) {
Ian Rogersf3e98552013-03-20 15:49:49 -0700412 ThrowStackOverflowError(self);
413 return JValue();
414 }
415
Ian Rogers7db619b2013-01-16 18:35:48 -0800416 return Execute(self, mh, code_item, shadow_frame, JValue());
417}
418
Ian Rogers848871b2013-08-05 10:56:33 -0700419extern "C" void artInterpreterToInterpreterBridge(Thread* self, MethodHelper& mh,
420 const DexFile::CodeItem* code_item,
421 ShadowFrame* shadow_frame, JValue* result) {
Jeff Hao790ad902013-05-22 15:02:08 -0700422 if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEnd())) {
Jeff Hao16743632013-05-08 10:59:04 -0700423 ThrowStackOverflowError(self);
Jeff Hao69510672013-05-21 17:34:55 -0700424 return;
Jeff Hao16743632013-05-08 10:59:04 -0700425 }
426
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700427 self->PushShadowFrame(shadow_frame);
Brian Carlstromea46f952013-07-30 01:26:50 -0700428 ArtMethod* method = shadow_frame->GetMethod();
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200429 // Ensure static methods are initialized.
430 if (method->IsStatic()) {
431 Class* declaringClass = method->GetDeclaringClass();
432 if (UNLIKELY(!declaringClass->IsInitializing())) {
433 if (UNLIKELY(!Runtime::Current()->GetClassLinker()->EnsureInitialized(declaringClass,
434 true, true))) {
435 DCHECK(Thread::Current()->IsExceptionPending());
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700436 self->PopShadowFrame();
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200437 return;
438 }
439 CHECK(declaringClass->IsInitializing());
Jeff Hao16743632013-05-08 10:59:04 -0700440 }
Jeff Hao16743632013-05-08 10:59:04 -0700441 }
Jeff Hao16743632013-05-08 10:59:04 -0700442
Jeff Hao16743632013-05-08 10:59:04 -0700443 if (LIKELY(!method->IsNative())) {
Jeff Hao69510672013-05-21 17:34:55 -0700444 result->SetJ(Execute(self, mh, code_item, *shadow_frame, JValue()).GetJ());
Jeff Hao16743632013-05-08 10:59:04 -0700445 } else {
446 // We don't expect to be asked to interpret native code (which is entered via a JNI compiler
447 // generated stub) except during testing and image writing.
448 CHECK(!Runtime::Current()->IsStarted());
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700449 Object* receiver = method->IsStatic() ? nullptr : shadow_frame->GetVRegReference(0);
Jeff Hao16743632013-05-08 10:59:04 -0700450 uint32_t* args = shadow_frame->GetVRegArgs(method->IsStatic() ? 0 : 1);
Jeff Hao69510672013-05-21 17:34:55 -0700451 UnstartedRuntimeJni(self, method, receiver, args, result);
Jeff Hao16743632013-05-08 10:59:04 -0700452 }
453
454 self->PopShadowFrame();
Jeff Hao16743632013-05-08 10:59:04 -0700455}
456
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700457} // namespace interpreter
458} // namespace art