blob: 686b518c5f289ec69681c565e95ed86a0887a99f [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"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070022#include "scoped_thread_state_change.h"
23#include "ScopedLocalRef.h"
24#include "unstarted_runtime.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070025
Ian Rogers2fa6b2e2012-10-17 00:10:17 -070026namespace art {
27namespace interpreter {
28
Ian Rogersfc0e94b2013-09-23 23:51:32 -070029static void InterpreterJni(Thread* self, ArtMethod* method, const StringPiece& shorty,
Jeff Hao5d917302013-02-27 17:57:33 -080030 Object* receiver, uint32_t* args, JValue* result)
Ian Rogers64b6d142012-10-29 16:34:15 -070031 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
32 // TODO: The following enters JNI code using a typedef-ed function rather than the JNI compiler,
33 // it should be removed and JNI compiled stubs used instead.
34 ScopedObjectAccessUnchecked soa(self);
35 if (method->IsStatic()) {
36 if (shorty == "L") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +010037 typedef jobject (fntype)(JNIEnv*, jclass);
Mathieu Chartier2d721012014-11-10 11:08:06 -080038 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -070039 ScopedLocalRef<jclass> klass(soa.Env(),
40 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
Ian Rogers556d6372012-11-20 12:19:36 -080041 jobject jresult;
42 {
43 ScopedThreadStateChange tsc(self, kNative);
44 jresult = fn(soa.Env(), klass.get());
45 }
46 result->SetL(soa.Decode<Object*>(jresult));
Ian Rogers64b6d142012-10-29 16:34:15 -070047 } else if (shorty == "V") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +010048 typedef void (fntype)(JNIEnv*, jclass);
Mathieu Chartier2d721012014-11-10 11:08:06 -080049 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -070050 ScopedLocalRef<jclass> klass(soa.Env(),
51 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
52 ScopedThreadStateChange tsc(self, kNative);
53 fn(soa.Env(), klass.get());
54 } else if (shorty == "Z") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +010055 typedef jboolean (fntype)(JNIEnv*, jclass);
Mathieu Chartier2d721012014-11-10 11:08:06 -080056 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -070057 ScopedLocalRef<jclass> klass(soa.Env(),
58 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
59 ScopedThreadStateChange tsc(self, kNative);
60 result->SetZ(fn(soa.Env(), klass.get()));
61 } else if (shorty == "BI") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +010062 typedef jbyte (fntype)(JNIEnv*, jclass, jint);
Mathieu Chartier2d721012014-11-10 11:08:06 -080063 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -070064 ScopedLocalRef<jclass> klass(soa.Env(),
65 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
66 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -080067 result->SetB(fn(soa.Env(), klass.get(), args[0]));
Ian Rogers64b6d142012-10-29 16:34:15 -070068 } else if (shorty == "II") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +010069 typedef jint (fntype)(JNIEnv*, jclass, jint);
Mathieu Chartier2d721012014-11-10 11:08:06 -080070 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -070071 ScopedLocalRef<jclass> klass(soa.Env(),
72 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
73 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -080074 result->SetI(fn(soa.Env(), klass.get(), args[0]));
Ian Rogers64b6d142012-10-29 16:34:15 -070075 } else if (shorty == "LL") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +010076 typedef jobject (fntype)(JNIEnv*, jclass, jobject);
Mathieu Chartier2d721012014-11-10 11:08:06 -080077 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -070078 ScopedLocalRef<jclass> klass(soa.Env(),
79 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
80 ScopedLocalRef<jobject> arg0(soa.Env(),
Jeff Hao5d917302013-02-27 17:57:33 -080081 soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[0])));
Ian Rogers556d6372012-11-20 12:19:36 -080082 jobject jresult;
83 {
84 ScopedThreadStateChange tsc(self, kNative);
85 jresult = fn(soa.Env(), klass.get(), arg0.get());
86 }
87 result->SetL(soa.Decode<Object*>(jresult));
Ian Rogers64b6d142012-10-29 16:34:15 -070088 } else if (shorty == "IIZ") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +010089 typedef jint (fntype)(JNIEnv*, jclass, jint, jboolean);
Mathieu Chartier2d721012014-11-10 11:08:06 -080090 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -070091 ScopedLocalRef<jclass> klass(soa.Env(),
92 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
93 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -080094 result->SetI(fn(soa.Env(), klass.get(), args[0], args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -070095 } else if (shorty == "ILI") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +010096 typedef jint (fntype)(JNIEnv*, jclass, jobject, jint);
Mathieu Chartier2d721012014-11-10 11:08:06 -080097 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(
98 method->GetEntryPointFromJni()));
Ian Rogers64b6d142012-10-29 16:34:15 -070099 ScopedLocalRef<jclass> klass(soa.Env(),
100 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
101 ScopedLocalRef<jobject> arg0(soa.Env(),
Jeff Hao5d917302013-02-27 17:57:33 -0800102 soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[0])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700103 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800104 result->SetI(fn(soa.Env(), klass.get(), arg0.get(), args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700105 } else if (shorty == "SIZ") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100106 typedef jshort (fntype)(JNIEnv*, jclass, jint, jboolean);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800107 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(method->GetEntryPointFromJni()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700108 ScopedLocalRef<jclass> klass(soa.Env(),
109 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
110 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800111 result->SetS(fn(soa.Env(), klass.get(), args[0], args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700112 } else if (shorty == "VIZ") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100113 typedef void (fntype)(JNIEnv*, jclass, jint, jboolean);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800114 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700115 ScopedLocalRef<jclass> klass(soa.Env(),
116 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
117 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800118 fn(soa.Env(), klass.get(), args[0], args[1]);
Ian Rogers64b6d142012-10-29 16:34:15 -0700119 } else if (shorty == "ZLL") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100120 typedef jboolean (fntype)(JNIEnv*, jclass, jobject, jobject);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800121 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700122 ScopedLocalRef<jclass> klass(soa.Env(),
123 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
124 ScopedLocalRef<jobject> arg0(soa.Env(),
Jeff Hao5d917302013-02-27 17:57:33 -0800125 soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[0])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700126 ScopedLocalRef<jobject> arg1(soa.Env(),
Jeff Hao5d917302013-02-27 17:57:33 -0800127 soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[1])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700128 ScopedThreadStateChange tsc(self, kNative);
129 result->SetZ(fn(soa.Env(), klass.get(), arg0.get(), arg1.get()));
130 } else if (shorty == "ZILL") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100131 typedef jboolean (fntype)(JNIEnv*, jclass, jint, jobject, jobject);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800132 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700133 ScopedLocalRef<jclass> klass(soa.Env(),
134 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
135 ScopedLocalRef<jobject> arg1(soa.Env(),
Jeff Hao5d917302013-02-27 17:57:33 -0800136 soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[1])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700137 ScopedLocalRef<jobject> arg2(soa.Env(),
Jeff Hao5d917302013-02-27 17:57:33 -0800138 soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[2])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700139 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800140 result->SetZ(fn(soa.Env(), klass.get(), args[0], arg1.get(), arg2.get()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700141 } else if (shorty == "VILII") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100142 typedef void (fntype)(JNIEnv*, jclass, jint, jobject, jint, jint);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800143 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700144 ScopedLocalRef<jclass> klass(soa.Env(),
145 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
146 ScopedLocalRef<jobject> arg1(soa.Env(),
Jeff Hao5d917302013-02-27 17:57:33 -0800147 soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[1])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700148 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800149 fn(soa.Env(), klass.get(), args[0], arg1.get(), args[2], args[3]);
Ian Rogers64b6d142012-10-29 16:34:15 -0700150 } else if (shorty == "VLILII") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100151 typedef void (fntype)(JNIEnv*, jclass, jobject, jint, jobject, jint, jint);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800152 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700153 ScopedLocalRef<jclass> klass(soa.Env(),
154 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
155 ScopedLocalRef<jobject> arg0(soa.Env(),
Jeff Hao5d917302013-02-27 17:57:33 -0800156 soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[0])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700157 ScopedLocalRef<jobject> arg2(soa.Env(),
Jeff Hao5d917302013-02-27 17:57:33 -0800158 soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[2])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700159 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800160 fn(soa.Env(), klass.get(), arg0.get(), args[1], arg2.get(), args[3], args[4]);
Ian Rogers64b6d142012-10-29 16:34:15 -0700161 } else {
162 LOG(FATAL) << "Do something with static native method: " << PrettyMethod(method)
163 << " shorty: " << shorty;
164 }
165 } else {
166 if (shorty == "L") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100167 typedef jobject (fntype)(JNIEnv*, jobject);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800168 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700169 ScopedLocalRef<jobject> rcvr(soa.Env(),
170 soa.AddLocalReference<jobject>(receiver));
Ian Rogers556d6372012-11-20 12:19:36 -0800171 jobject jresult;
172 {
173 ScopedThreadStateChange tsc(self, kNative);
174 jresult = fn(soa.Env(), rcvr.get());
175 }
176 result->SetL(soa.Decode<Object*>(jresult));
Jeff Hao3dd9f762013-07-08 13:09:25 -0700177 } else if (shorty == "V") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100178 typedef void (fntype)(JNIEnv*, jobject);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800179 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Jeff Hao3dd9f762013-07-08 13:09:25 -0700180 ScopedLocalRef<jobject> rcvr(soa.Env(),
181 soa.AddLocalReference<jobject>(receiver));
182 ScopedThreadStateChange tsc(self, kNative);
183 fn(soa.Env(), rcvr.get());
Ian Rogers64b6d142012-10-29 16:34:15 -0700184 } else if (shorty == "LL") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100185 typedef jobject (fntype)(JNIEnv*, jobject, jobject);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800186 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700187 ScopedLocalRef<jobject> rcvr(soa.Env(),
188 soa.AddLocalReference<jobject>(receiver));
189 ScopedLocalRef<jobject> arg0(soa.Env(),
Jeff Hao5d917302013-02-27 17:57:33 -0800190 soa.AddLocalReference<jobject>(reinterpret_cast<Object*>(args[0])));
Ian Rogers556d6372012-11-20 12:19:36 -0800191 jobject jresult;
192 {
193 ScopedThreadStateChange tsc(self, kNative);
194 jresult = fn(soa.Env(), rcvr.get(), arg0.get());
Ian Rogers556d6372012-11-20 12:19:36 -0800195 }
196 result->SetL(soa.Decode<Object*>(jresult));
Ian Rogers64b6d142012-10-29 16:34:15 -0700197 ScopedThreadStateChange tsc(self, kNative);
Ian Rogers64b6d142012-10-29 16:34:15 -0700198 } else if (shorty == "III") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100199 typedef jint (fntype)(JNIEnv*, jobject, jint, jint);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800200 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700201 ScopedLocalRef<jobject> rcvr(soa.Env(),
202 soa.AddLocalReference<jobject>(receiver));
203 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800204 result->SetI(fn(soa.Env(), rcvr.get(), args[0], args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700205 } else {
206 LOG(FATAL) << "Do something with native method: " << PrettyMethod(method)
207 << " shorty: " << shorty;
208 }
209 }
210}
211
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200212enum InterpreterImplKind {
Ian Rogersb48b9eb2014-02-28 16:20:21 -0800213 kSwitchImpl, // Switch-based interpreter implementation.
214 kComputedGotoImplKind // Computed-goto-based interpreter implementation.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200215};
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800216static std::ostream& operator<<(std::ostream& os, const InterpreterImplKind& rhs) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700217 os << ((rhs == kSwitchImpl) ? "Switch-based interpreter" : "Computed-goto-based interpreter");
218 return os;
219}
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700220
Sebastien Hertzfa888d02014-09-30 12:00:11 +0200221#if !defined(__clang__)
Ian Rogersb48b9eb2014-02-28 16:20:21 -0800222static constexpr InterpreterImplKind kInterpreterImplKind = kComputedGotoImplKind;
Sebastien Hertzfa888d02014-09-30 12:00:11 +0200223#else
224// Clang 3.4 fails to build the goto interpreter implementation.
225static constexpr InterpreterImplKind kInterpreterImplKind = kSwitchImpl;
226template<bool do_access_check, bool transaction_active>
Ian Rogerse94652f2014-12-02 11:13:19 -0800227JValue ExecuteGotoImpl(Thread*, const DexFile::CodeItem*, ShadowFrame&, JValue) {
Sebastien Hertzfa888d02014-09-30 12:00:11 +0200228 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700229 UNREACHABLE();
Sebastien Hertzfa888d02014-09-30 12:00:11 +0200230}
231// Explicit definitions of ExecuteGotoImpl.
232template<> SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogerse94652f2014-12-02 11:13:19 -0800233JValue ExecuteGotoImpl<true, false>(Thread* self, const DexFile::CodeItem* code_item,
Sebastien Hertzfa888d02014-09-30 12:00:11 +0200234 ShadowFrame& shadow_frame, JValue result_register);
235template<> SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogerse94652f2014-12-02 11:13:19 -0800236JValue ExecuteGotoImpl<false, false>(Thread* self, const DexFile::CodeItem* code_item,
Sebastien Hertzfa888d02014-09-30 12:00:11 +0200237 ShadowFrame& shadow_frame, JValue result_register);
238template<> SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogerse94652f2014-12-02 11:13:19 -0800239JValue ExecuteGotoImpl<true, true>(Thread* self, const DexFile::CodeItem* code_item,
240 ShadowFrame& shadow_frame, JValue result_register);
241template<> SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
242JValue ExecuteGotoImpl<false, true>(Thread* self, const DexFile::CodeItem* code_item,
Sebastien Hertzfa888d02014-09-30 12:00:11 +0200243 ShadowFrame& shadow_frame, JValue result_register);
Sebastien Hertzfa888d02014-09-30 12:00:11 +0200244#endif
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700245
Ian Rogerse94652f2014-12-02 11:13:19 -0800246static JValue Execute(Thread* self, const DexFile::CodeItem* code_item, ShadowFrame& shadow_frame,
247 JValue result_register)
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200248 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
249
Ian Rogerse94652f2014-12-02 11:13:19 -0800250static inline JValue Execute(Thread* self, const DexFile::CodeItem* code_item,
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200251 ShadowFrame& shadow_frame, JValue result_register) {
Ian Rogers848871b2013-08-05 10:56:33 -0700252 DCHECK(!shadow_frame.GetMethod()->IsAbstract());
253 DCHECK(!shadow_frame.GetMethod()->IsNative());
Sebastien Hertz4e99b3d2014-06-24 14:35:40 +0200254 shadow_frame.GetMethod()->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200255
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100256 bool transaction_active = Runtime::Current()->IsActiveTransaction();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200257 if (LIKELY(shadow_frame.GetMethod()->IsPreverified())) {
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200258 // Enter the "without access check" interpreter.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200259 if (kInterpreterImplKind == kSwitchImpl) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100260 if (transaction_active) {
Ian Rogerse94652f2014-12-02 11:13:19 -0800261 return ExecuteSwitchImpl<false, true>(self, code_item, shadow_frame, result_register);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100262 } else {
Ian Rogerse94652f2014-12-02 11:13:19 -0800263 return ExecuteSwitchImpl<false, false>(self, code_item, shadow_frame, result_register);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100264 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200265 } else {
266 DCHECK_EQ(kInterpreterImplKind, kComputedGotoImplKind);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100267 if (transaction_active) {
Ian Rogerse94652f2014-12-02 11:13:19 -0800268 return ExecuteGotoImpl<false, true>(self, code_item, shadow_frame, result_register);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100269 } else {
Ian Rogerse94652f2014-12-02 11:13:19 -0800270 return ExecuteGotoImpl<false, false>(self, code_item, shadow_frame, result_register);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100271 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200272 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200273 } else {
274 // Enter the "with access check" interpreter.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200275 if (kInterpreterImplKind == kSwitchImpl) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100276 if (transaction_active) {
Ian Rogerse94652f2014-12-02 11:13:19 -0800277 return ExecuteSwitchImpl<true, true>(self, code_item, shadow_frame, result_register);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100278 } else {
Ian Rogerse94652f2014-12-02 11:13:19 -0800279 return ExecuteSwitchImpl<true, false>(self, code_item, shadow_frame, result_register);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100280 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200281 } else {
282 DCHECK_EQ(kInterpreterImplKind, kComputedGotoImplKind);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100283 if (transaction_active) {
Ian Rogerse94652f2014-12-02 11:13:19 -0800284 return ExecuteGotoImpl<true, true>(self, code_item, shadow_frame, result_register);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100285 } else {
Ian Rogerse94652f2014-12-02 11:13:19 -0800286 return ExecuteGotoImpl<true, false>(self, code_item, shadow_frame, result_register);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100287 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200288 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200289 }
290}
291
Brian Carlstromea46f952013-07-30 01:26:50 -0700292void EnterInterpreterFromInvoke(Thread* self, ArtMethod* method, Object* receiver,
Jeff Hao6474d192013-03-26 14:08:09 -0700293 uint32_t* args, JValue* result) {
Ian Rogers64b6d142012-10-29 16:34:15 -0700294 DCHECK_EQ(self, Thread::Current());
Nicolas Geoffray535a3fb2014-07-22 15:17:38 +0100295 bool implicit_check = !Runtime::Current()->ExplicitStackOverflowChecks();
296 if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEndForInterpreter(implicit_check))) {
jeffhaod7521322012-11-21 15:38:24 -0800297 ThrowStackOverflowError(self);
298 return;
299 }
300
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700301 const char* old_cause = self->StartAssertNoThreadSuspension("EnterInterpreterFromInvoke");
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700302 const DexFile::CodeItem* code_item = method->GetCodeItem();
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700303 uint16_t num_regs;
304 uint16_t num_ins;
305 if (code_item != NULL) {
306 num_regs = code_item->registers_size_;
307 num_ins = code_item->ins_size_;
jeffhao0a9bb732012-11-26 12:28:49 -0800308 } else if (method->IsAbstract()) {
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700309 self->EndAssertNoThreadSuspension(old_cause);
Sebastien Hertz56adf602013-07-09 17:27:07 +0200310 ThrowAbstractMethodError(method);
jeffhao0a9bb732012-11-26 12:28:49 -0800311 return;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700312 } else {
313 DCHECK(method->IsNative());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700314 num_regs = num_ins = ArtMethod::NumArgRegisters(method->GetShorty());
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700315 if (!method->IsStatic()) {
316 num_regs++;
317 num_ins++;
318 }
319 }
320 // Set up shadow frame with matching number of reference slots to vregs.
321 ShadowFrame* last_shadow_frame = self->GetManagedStack()->GetTopShadowFrame();
Jeff Hao66135192013-05-14 11:02:41 -0700322 void* memory = alloca(ShadowFrame::ComputeSize(num_regs));
323 ShadowFrame* shadow_frame(ShadowFrame::Create(num_regs, last_shadow_frame, method, 0, memory));
324 self->PushShadowFrame(shadow_frame);
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700325
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700326 size_t cur_reg = num_regs - num_ins;
327 if (!method->IsStatic()) {
328 CHECK(receiver != NULL);
TDYa127ce4cc0d2012-11-18 16:59:53 -0800329 shadow_frame->SetVRegReference(cur_reg, receiver);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700330 ++cur_reg;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700331 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700332 uint32_t shorty_len = 0;
333 const char* shorty = method->GetShorty(&shorty_len);
Jeff Hao5d917302013-02-27 17:57:33 -0800334 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 -0700335 DCHECK_LT(shorty_pos + 1, shorty_len);
Jeff Hao5d917302013-02-27 17:57:33 -0800336 switch (shorty[shorty_pos + 1]) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700337 case 'L': {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800338 Object* o = reinterpret_cast<StackReference<Object>*>(&args[arg_pos])->AsMirrorPtr();
TDYa127ce4cc0d2012-11-18 16:59:53 -0800339 shadow_frame->SetVRegReference(cur_reg, o);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700340 break;
341 }
Jeff Hao5d917302013-02-27 17:57:33 -0800342 case 'J': case 'D': {
343 uint64_t wide_value = (static_cast<uint64_t>(args[arg_pos + 1]) << 32) | args[arg_pos];
344 shadow_frame->SetVRegLong(cur_reg, wide_value);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700345 cur_reg++;
Jeff Hao5d917302013-02-27 17:57:33 -0800346 arg_pos++;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700347 break;
Jeff Hao5d917302013-02-27 17:57:33 -0800348 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700349 default:
Jeff Hao5d917302013-02-27 17:57:33 -0800350 shadow_frame->SetVReg(cur_reg, args[arg_pos]);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700351 break;
352 }
353 }
Mathieu Chartier92246bb2014-02-25 18:22:39 -0800354 self->EndAssertNoThreadSuspension(old_cause);
355 // Do this after populating the shadow frame in case EnsureInitialized causes a GC.
Ian Rogers6c5cb212014-06-18 16:07:20 -0700356 if (method->IsStatic() && UNLIKELY(!method->GetDeclaringClass()->IsInitialized())) {
Mathieu Chartier92246bb2014-02-25 18:22:39 -0800357 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700358 StackHandleScope<1> hs(self);
359 Handle<mirror::Class> h_class(hs.NewHandle(method->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700360 if (UNLIKELY(!class_linker->EnsureInitialized(self, h_class, true, true))) {
Mathieu Chartier92246bb2014-02-25 18:22:39 -0800361 CHECK(self->IsExceptionPending());
362 self->PopShadowFrame();
363 return;
364 }
365 }
Ian Rogers64b6d142012-10-29 16:34:15 -0700366 if (LIKELY(!method->IsNative())) {
Ian Rogerse94652f2014-12-02 11:13:19 -0800367 JValue r = Execute(self, code_item, *shadow_frame, JValue());
Jeff Hao6474d192013-03-26 14:08:09 -0700368 if (result != NULL) {
369 *result = r;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700370 }
371 } else {
Ian Rogers64b6d142012-10-29 16:34:15 -0700372 // We don't expect to be asked to interpret native code (which is entered via a JNI compiler
373 // generated stub) except during testing and image writing.
Mathieu Chartier92246bb2014-02-25 18:22:39 -0800374 // Update args to be the args in the shadow frame since the input ones could hold stale
375 // references pointers due to moving GC.
376 args = shadow_frame->GetVRegArgs(method->IsStatic() ? 0 : 1);
Ian Rogers64b6d142012-10-29 16:34:15 -0700377 if (!Runtime::Current()->IsStarted()) {
Jeff Hao6474d192013-03-26 14:08:09 -0700378 UnstartedRuntimeJni(self, method, receiver, args, result);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700379 } else {
Jeff Hao6474d192013-03-26 14:08:09 -0700380 InterpreterJni(self, method, shorty, receiver, args, result);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700381 }
382 }
383 self->PopShadowFrame();
384}
385
Ian Rogers62d6c772013-02-27 08:32:07 -0800386void EnterInterpreterFromDeoptimize(Thread* self, ShadowFrame* shadow_frame, JValue* ret_val)
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800387 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
388 JValue value;
Ian Rogers62d6c772013-02-27 08:32:07 -0800389 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 -0800390 while (shadow_frame != NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800391 self->SetTopOfShadowStack(shadow_frame);
Ian Rogerse94652f2014-12-02 11:13:19 -0800392 const DexFile::CodeItem* code_item = shadow_frame->GetMethod()->GetCodeItem();
Sebastien Hertz270a0e12015-01-16 19:49:09 +0100393 const uint32_t dex_pc = shadow_frame->GetDexPC();
394 uint32_t new_dex_pc;
395 if (UNLIKELY(self->IsExceptionPending())) {
396 const instrumentation::Instrumentation* const instrumentation =
397 Runtime::Current()->GetInstrumentation();
398 uint32_t found_dex_pc = FindNextInstructionFollowingException(self, *shadow_frame, dex_pc,
399 instrumentation);
400 new_dex_pc = found_dex_pc; // the dex pc of a matching catch handler
401 // or DexFile::kDexNoIndex if there is none.
402 } else {
403 const Instruction* instr = Instruction::At(&code_item->insns_[dex_pc]);
Andreas Gampe0ba62732015-03-24 02:39:46 +0000404 new_dex_pc = dex_pc + instr->SizeInCodeUnits(); // the dex pc of the next instruction.
Sebastien Hertz270a0e12015-01-16 19:49:09 +0100405 }
406 if (new_dex_pc != DexFile::kDexNoIndex) {
407 shadow_frame->SetDexPC(new_dex_pc);
408 value = Execute(self, code_item, *shadow_frame, value);
409 }
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800410 ShadowFrame* old_frame = shadow_frame;
411 shadow_frame = shadow_frame->GetLink();
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800412 delete old_frame;
413 }
414 ret_val->SetJ(value.GetJ());
415}
416
Ian Rogerse94652f2014-12-02 11:13:19 -0800417JValue EnterInterpreterFromEntryPoint(Thread* self, const DexFile::CodeItem* code_item,
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700418 ShadowFrame* shadow_frame) {
Ian Rogersf3e98552013-03-20 15:49:49 -0700419 DCHECK_EQ(self, Thread::Current());
Nicolas Geoffray535a3fb2014-07-22 15:17:38 +0100420 bool implicit_check = !Runtime::Current()->ExplicitStackOverflowChecks();
421 if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEndForInterpreter(implicit_check))) {
Ian Rogersf3e98552013-03-20 15:49:49 -0700422 ThrowStackOverflowError(self);
423 return JValue();
424 }
425
Ian Rogerse94652f2014-12-02 11:13:19 -0800426 return Execute(self, code_item, *shadow_frame, JValue());
Ian Rogers7db619b2013-01-16 18:35:48 -0800427}
428
Ian Rogerse94652f2014-12-02 11:13:19 -0800429extern "C" void artInterpreterToInterpreterBridge(Thread* self, const DexFile::CodeItem* code_item,
Ian Rogers848871b2013-08-05 10:56:33 -0700430 ShadowFrame* shadow_frame, JValue* result) {
Nicolas Geoffray535a3fb2014-07-22 15:17:38 +0100431 bool implicit_check = !Runtime::Current()->ExplicitStackOverflowChecks();
432 if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEndForInterpreter(implicit_check))) {
Jeff Hao16743632013-05-08 10:59:04 -0700433 ThrowStackOverflowError(self);
Jeff Hao69510672013-05-21 17:34:55 -0700434 return;
Jeff Hao16743632013-05-08 10:59:04 -0700435 }
436
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700437 self->PushShadowFrame(shadow_frame);
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200438 // Ensure static methods are initialized.
Ian Rogerse94652f2014-12-02 11:13:19 -0800439 const bool is_static = shadow_frame->GetMethod()->IsStatic();
440 if (is_static) {
441 mirror::Class* declaring_class = shadow_frame->GetMethod()->GetDeclaringClass();
Ian Rogers6c5cb212014-06-18 16:07:20 -0700442 if (UNLIKELY(!declaring_class->IsInitialized())) {
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700443 StackHandleScope<1> hs(self);
444 HandleWrapper<Class> h_declaring_class(hs.NewHandleWrapper(&declaring_class));
445 if (UNLIKELY(!Runtime::Current()->GetClassLinker()->EnsureInitialized(
Ian Rogers7b078e82014-09-10 14:44:24 -0700446 self, h_declaring_class, true, true))) {
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700447 DCHECK(self->IsExceptionPending());
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700448 self->PopShadowFrame();
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200449 return;
450 }
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700451 CHECK(h_declaring_class->IsInitializing());
Jeff Hao16743632013-05-08 10:59:04 -0700452 }
Jeff Hao16743632013-05-08 10:59:04 -0700453 }
Jeff Hao16743632013-05-08 10:59:04 -0700454
Ian Rogerse94652f2014-12-02 11:13:19 -0800455 if (LIKELY(!shadow_frame->GetMethod()->IsNative())) {
456 result->SetJ(Execute(self, code_item, *shadow_frame, JValue()).GetJ());
Jeff Hao16743632013-05-08 10:59:04 -0700457 } else {
458 // We don't expect to be asked to interpret native code (which is entered via a JNI compiler
459 // generated stub) except during testing and image writing.
460 CHECK(!Runtime::Current()->IsStarted());
Ian Rogerse94652f2014-12-02 11:13:19 -0800461 Object* receiver = is_static ? nullptr : shadow_frame->GetVRegReference(0);
462 uint32_t* args = shadow_frame->GetVRegArgs(is_static ? 0 : 1);
463 UnstartedRuntimeJni(self, shadow_frame->GetMethod(), receiver, args, result);
Jeff Hao16743632013-05-08 10:59:04 -0700464 }
465
466 self->PopShadowFrame();
Jeff Hao16743632013-05-08 10:59:04 -0700467}
468
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700469} // namespace interpreter
470} // namespace art