blob: 8a31985a97960c30d388b19ca210eaab04e94316 [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
Andreas Gampe3cfa4d02015-10-06 17:04:01 -070017#include "interpreter.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
Andreas Gampe580667b2017-10-23 11:20:39 -070021#include "common_dex_operations.h"
Andreas Gampe103992b2016-01-04 15:32:43 -080022#include "common_throws.h"
David Sehr9e734c72018-01-04 17:56:19 -080023#include "dex/dex_file_types.h"
Andreas Gampe3cfa4d02015-10-06 17:04:01 -070024#include "interpreter_common.h"
Andreas Gampe5e26eb12016-08-22 17:54:17 -070025#include "interpreter_mterp_impl.h"
26#include "interpreter_switch_impl.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070027#include "jit/jit.h"
28#include "jit/jit_code_cache.h"
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070029#include "jvalue-inl.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070030#include "mirror/string-inl.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070031#include "mterp/mterp.h"
Andreas Gampe373a9b52017-10-18 09:01:57 -070032#include "nativehelper/scoped_local_ref.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070033#include "scoped_thread_state_change-inl.h"
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +010034#include "shadow_frame-inl.h"
Andreas Gampeb3025922015-09-01 14:45:00 -070035#include "stack.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070036#include "thread-inl.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070037#include "unstarted_runtime.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070038
Ian Rogers2fa6b2e2012-10-17 00:10:17 -070039namespace art {
40namespace interpreter {
41
Mathieu Chartieref41db72016-10-25 15:08:01 -070042ALWAYS_INLINE static ObjPtr<mirror::Object> ObjArg(uint32_t arg)
43 REQUIRES_SHARED(Locks::mutator_lock_) {
44 return ObjPtr<mirror::Object>(reinterpret_cast<mirror::Object*>(arg));
45}
46
47static void InterpreterJni(Thread* self,
48 ArtMethod* method,
49 const StringPiece& shorty,
50 ObjPtr<mirror::Object> receiver,
51 uint32_t* args,
52 JValue* result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070053 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers64b6d142012-10-29 16:34:15 -070054 // TODO: The following enters JNI code using a typedef-ed function rather than the JNI compiler,
55 // it should be removed and JNI compiled stubs used instead.
56 ScopedObjectAccessUnchecked soa(self);
57 if (method->IsStatic()) {
58 if (shorty == "L") {
Andreas Gampec55bb392018-09-21 00:02:02 +000059 using fntype = jobject(JNIEnv*, jclass);
Mathieu Chartier2d721012014-11-10 11:08:06 -080060 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -070061 ScopedLocalRef<jclass> klass(soa.Env(),
62 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
Ian Rogers556d6372012-11-20 12:19:36 -080063 jobject jresult;
64 {
65 ScopedThreadStateChange tsc(self, kNative);
66 jresult = fn(soa.Env(), klass.get());
67 }
Mathieu Chartieref41db72016-10-25 15:08:01 -070068 result->SetL(soa.Decode<mirror::Object>(jresult));
Ian Rogers64b6d142012-10-29 16:34:15 -070069 } else if (shorty == "V") {
Andreas Gampec55bb392018-09-21 00:02:02 +000070 using fntype = void(JNIEnv*, jclass);
Mathieu Chartier2d721012014-11-10 11:08:06 -080071 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -070072 ScopedLocalRef<jclass> klass(soa.Env(),
73 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
74 ScopedThreadStateChange tsc(self, kNative);
75 fn(soa.Env(), klass.get());
76 } else if (shorty == "Z") {
Andreas Gampec55bb392018-09-21 00:02:02 +000077 using fntype = jboolean(JNIEnv*, jclass);
Mathieu Chartier2d721012014-11-10 11:08:06 -080078 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -070079 ScopedLocalRef<jclass> klass(soa.Env(),
80 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
81 ScopedThreadStateChange tsc(self, kNative);
82 result->SetZ(fn(soa.Env(), klass.get()));
83 } else if (shorty == "BI") {
Andreas Gampec55bb392018-09-21 00:02:02 +000084 using fntype = jbyte(JNIEnv*, jclass, jint);
Mathieu Chartier2d721012014-11-10 11:08:06 -080085 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -070086 ScopedLocalRef<jclass> klass(soa.Env(),
87 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
88 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -080089 result->SetB(fn(soa.Env(), klass.get(), args[0]));
Ian Rogers64b6d142012-10-29 16:34:15 -070090 } else if (shorty == "II") {
Andreas Gampec55bb392018-09-21 00:02:02 +000091 using fntype = jint(JNIEnv*, jclass, jint);
Mathieu Chartier2d721012014-11-10 11:08:06 -080092 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -070093 ScopedLocalRef<jclass> klass(soa.Env(),
94 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
95 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -080096 result->SetI(fn(soa.Env(), klass.get(), args[0]));
Ian Rogers64b6d142012-10-29 16:34:15 -070097 } else if (shorty == "LL") {
Andreas Gampec55bb392018-09-21 00:02:02 +000098 using fntype = jobject(JNIEnv*, jclass, jobject);
Mathieu Chartier2d721012014-11-10 11:08:06 -080099 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700100 ScopedLocalRef<jclass> klass(soa.Env(),
101 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
102 ScopedLocalRef<jobject> arg0(soa.Env(),
Mathieu Chartieref41db72016-10-25 15:08:01 -0700103 soa.AddLocalReference<jobject>(ObjArg(args[0])));
Ian Rogers556d6372012-11-20 12:19:36 -0800104 jobject jresult;
105 {
106 ScopedThreadStateChange tsc(self, kNative);
107 jresult = fn(soa.Env(), klass.get(), arg0.get());
108 }
Mathieu Chartieref41db72016-10-25 15:08:01 -0700109 result->SetL(soa.Decode<mirror::Object>(jresult));
Ian Rogers64b6d142012-10-29 16:34:15 -0700110 } else if (shorty == "IIZ") {
Andreas Gampec55bb392018-09-21 00:02:02 +0000111 using fntype = jint(JNIEnv*, jclass, jint, jboolean);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800112 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700113 ScopedLocalRef<jclass> klass(soa.Env(),
114 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
115 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800116 result->SetI(fn(soa.Env(), klass.get(), args[0], args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700117 } else if (shorty == "ILI") {
Andreas Gampec55bb392018-09-21 00:02:02 +0000118 using fntype = jint(JNIEnv*, jclass, jobject, jint);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800119 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(
120 method->GetEntryPointFromJni()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700121 ScopedLocalRef<jclass> klass(soa.Env(),
122 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
123 ScopedLocalRef<jobject> arg0(soa.Env(),
Mathieu Chartieref41db72016-10-25 15:08:01 -0700124 soa.AddLocalReference<jobject>(ObjArg(args[0])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700125 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800126 result->SetI(fn(soa.Env(), klass.get(), arg0.get(), args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700127 } else if (shorty == "SIZ") {
Andreas Gampec55bb392018-09-21 00:02:02 +0000128 using fntype = jshort(JNIEnv*, jclass, jint, jboolean);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700129 fntype* const fn =
130 reinterpret_cast<fntype*>(const_cast<void*>(method->GetEntryPointFromJni()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700131 ScopedLocalRef<jclass> klass(soa.Env(),
132 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
133 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800134 result->SetS(fn(soa.Env(), klass.get(), args[0], args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700135 } else if (shorty == "VIZ") {
Andreas Gampec55bb392018-09-21 00:02:02 +0000136 using fntype = void(JNIEnv*, jclass, jint, jboolean);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800137 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700138 ScopedLocalRef<jclass> klass(soa.Env(),
139 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
140 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800141 fn(soa.Env(), klass.get(), args[0], args[1]);
Ian Rogers64b6d142012-10-29 16:34:15 -0700142 } else if (shorty == "ZLL") {
Andreas Gampec55bb392018-09-21 00:02:02 +0000143 using fntype = jboolean(JNIEnv*, jclass, jobject, jobject);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800144 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700145 ScopedLocalRef<jclass> klass(soa.Env(),
146 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
147 ScopedLocalRef<jobject> arg0(soa.Env(),
Mathieu Chartieref41db72016-10-25 15:08:01 -0700148 soa.AddLocalReference<jobject>(ObjArg(args[0])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700149 ScopedLocalRef<jobject> arg1(soa.Env(),
Mathieu Chartieref41db72016-10-25 15:08:01 -0700150 soa.AddLocalReference<jobject>(ObjArg(args[1])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700151 ScopedThreadStateChange tsc(self, kNative);
152 result->SetZ(fn(soa.Env(), klass.get(), arg0.get(), arg1.get()));
153 } else if (shorty == "ZILL") {
Andreas Gampec55bb392018-09-21 00:02:02 +0000154 using fntype = jboolean(JNIEnv*, jclass, jint, jobject, jobject);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800155 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700156 ScopedLocalRef<jclass> klass(soa.Env(),
157 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
158 ScopedLocalRef<jobject> arg1(soa.Env(),
Mathieu Chartieref41db72016-10-25 15:08:01 -0700159 soa.AddLocalReference<jobject>(ObjArg(args[1])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700160 ScopedLocalRef<jobject> arg2(soa.Env(),
Mathieu Chartieref41db72016-10-25 15:08:01 -0700161 soa.AddLocalReference<jobject>(ObjArg(args[2])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700162 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800163 result->SetZ(fn(soa.Env(), klass.get(), args[0], arg1.get(), arg2.get()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700164 } else if (shorty == "VILII") {
Andreas Gampec55bb392018-09-21 00:02:02 +0000165 using fntype = void(JNIEnv*, jclass, jint, jobject, jint, jint);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800166 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700167 ScopedLocalRef<jclass> klass(soa.Env(),
168 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
169 ScopedLocalRef<jobject> arg1(soa.Env(),
Mathieu Chartieref41db72016-10-25 15:08:01 -0700170 soa.AddLocalReference<jobject>(ObjArg(args[1])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700171 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800172 fn(soa.Env(), klass.get(), args[0], arg1.get(), args[2], args[3]);
Ian Rogers64b6d142012-10-29 16:34:15 -0700173 } else if (shorty == "VLILII") {
Andreas Gampec55bb392018-09-21 00:02:02 +0000174 using fntype = void(JNIEnv*, jclass, jobject, jint, jobject, jint, jint);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800175 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700176 ScopedLocalRef<jclass> klass(soa.Env(),
177 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
178 ScopedLocalRef<jobject> arg0(soa.Env(),
Mathieu Chartieref41db72016-10-25 15:08:01 -0700179 soa.AddLocalReference<jobject>(ObjArg(args[0])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700180 ScopedLocalRef<jobject> arg2(soa.Env(),
Mathieu Chartieref41db72016-10-25 15:08:01 -0700181 soa.AddLocalReference<jobject>(ObjArg(args[2])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700182 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800183 fn(soa.Env(), klass.get(), arg0.get(), args[1], arg2.get(), args[3], args[4]);
Ian Rogers64b6d142012-10-29 16:34:15 -0700184 } else {
David Sehr709b0702016-10-13 09:12:37 -0700185 LOG(FATAL) << "Do something with static native method: " << method->PrettyMethod()
Ian Rogers64b6d142012-10-29 16:34:15 -0700186 << " shorty: " << shorty;
187 }
188 } else {
189 if (shorty == "L") {
Andreas Gampec55bb392018-09-21 00:02:02 +0000190 using fntype = jobject(JNIEnv*, jobject);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800191 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700192 ScopedLocalRef<jobject> rcvr(soa.Env(),
193 soa.AddLocalReference<jobject>(receiver));
Ian Rogers556d6372012-11-20 12:19:36 -0800194 jobject jresult;
195 {
196 ScopedThreadStateChange tsc(self, kNative);
197 jresult = fn(soa.Env(), rcvr.get());
198 }
Mathieu Chartieref41db72016-10-25 15:08:01 -0700199 result->SetL(soa.Decode<mirror::Object>(jresult));
Jeff Hao3dd9f762013-07-08 13:09:25 -0700200 } else if (shorty == "V") {
Andreas Gampec55bb392018-09-21 00:02:02 +0000201 using fntype = void(JNIEnv*, jobject);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800202 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Jeff Hao3dd9f762013-07-08 13:09:25 -0700203 ScopedLocalRef<jobject> rcvr(soa.Env(),
204 soa.AddLocalReference<jobject>(receiver));
205 ScopedThreadStateChange tsc(self, kNative);
206 fn(soa.Env(), rcvr.get());
Ian Rogers64b6d142012-10-29 16:34:15 -0700207 } else if (shorty == "LL") {
Andreas Gampec55bb392018-09-21 00:02:02 +0000208 using fntype = jobject(JNIEnv*, jobject, jobject);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800209 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700210 ScopedLocalRef<jobject> rcvr(soa.Env(),
211 soa.AddLocalReference<jobject>(receiver));
212 ScopedLocalRef<jobject> arg0(soa.Env(),
Mathieu Chartieref41db72016-10-25 15:08:01 -0700213 soa.AddLocalReference<jobject>(ObjArg(args[0])));
Ian Rogers556d6372012-11-20 12:19:36 -0800214 jobject jresult;
215 {
216 ScopedThreadStateChange tsc(self, kNative);
217 jresult = fn(soa.Env(), rcvr.get(), arg0.get());
Ian Rogers556d6372012-11-20 12:19:36 -0800218 }
Mathieu Chartieref41db72016-10-25 15:08:01 -0700219 result->SetL(soa.Decode<mirror::Object>(jresult));
Ian Rogers64b6d142012-10-29 16:34:15 -0700220 ScopedThreadStateChange tsc(self, kNative);
Ian Rogers64b6d142012-10-29 16:34:15 -0700221 } else if (shorty == "III") {
Andreas Gampec55bb392018-09-21 00:02:02 +0000222 using fntype = jint(JNIEnv*, jobject, jint, jint);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800223 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700224 ScopedLocalRef<jobject> rcvr(soa.Env(),
225 soa.AddLocalReference<jobject>(receiver));
226 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800227 result->SetI(fn(soa.Env(), rcvr.get(), args[0], args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700228 } else {
David Sehr709b0702016-10-13 09:12:37 -0700229 LOG(FATAL) << "Do something with native method: " << method->PrettyMethod()
Ian Rogers64b6d142012-10-29 16:34:15 -0700230 << " shorty: " << shorty;
231 }
232 }
233}
234
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200235enum InterpreterImplKind {
buzbee1452bee2015-03-06 14:43:04 -0800236 kSwitchImplKind, // Switch-based interpreter implementation.
buzbee1452bee2015-03-06 14:43:04 -0800237 kMterpImplKind // Assembly interpreter
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200238};
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700239
buzbee1452bee2015-03-06 14:43:04 -0800240static constexpr InterpreterImplKind kInterpreterImplKind = kMterpImplKind;
Alexey Frunze00b53b72016-02-02 20:25:45 -0800241
Aart Bik01223202016-05-05 15:10:42 -0700242static inline JValue Execute(
243 Thread* self,
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800244 const CodeItemDataAccessor& accessor,
Aart Bik01223202016-05-05 15:10:42 -0700245 ShadowFrame& shadow_frame,
246 JValue result_register,
Vladimir Markofe948752018-03-23 18:11:43 +0000247 bool stay_in_interpreter = false,
248 bool from_deoptimize = false) REQUIRES_SHARED(Locks::mutator_lock_) {
buzbee1452bee2015-03-06 14:43:04 -0800249 DCHECK(!shadow_frame.GetMethod()->IsAbstract());
Ian Rogers848871b2013-08-05 10:56:33 -0700250 DCHECK(!shadow_frame.GetMethod()->IsNative());
Vladimir Markofe948752018-03-23 18:11:43 +0000251 if (LIKELY(!from_deoptimize)) { // Entering the method, but not via deoptimization.
buzbee734f3aa2016-01-28 14:20:06 -0800252 if (kIsDebugBuild) {
Vladimir Markofe948752018-03-23 18:11:43 +0000253 CHECK_EQ(shadow_frame.GetDexPC(), 0u);
buzbee734f3aa2016-01-28 14:20:06 -0800254 self->AssertNoPendingException();
255 }
256 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
257 ArtMethod *method = shadow_frame.GetMethod();
258
259 if (UNLIKELY(instrumentation->HasMethodEntryListeners())) {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800260 instrumentation->MethodEnterEvent(self,
261 shadow_frame.GetThisObject(accessor.InsSize()),
262 method,
263 0);
Alex Light0aa7a5a2018-10-10 15:58:14 +0000264 if (UNLIKELY(shadow_frame.GetForcePopFrame())) {
265 // The caller will retry this invoke. Just return immediately without any value.
266 DCHECK(Runtime::Current()->AreNonStandardExitsEnabled());
267 DCHECK(PrevFrameWillRetry(self, shadow_frame));
268 return JValue();
269 }
Alex Lightb7edcda2017-04-27 13:20:31 -0700270 if (UNLIKELY(self->IsExceptionPending())) {
271 instrumentation->MethodUnwindEvent(self,
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800272 shadow_frame.GetThisObject(accessor.InsSize()),
Alex Lightb7edcda2017-04-27 13:20:31 -0700273 method,
274 0);
275 return JValue();
276 }
buzbee734f3aa2016-01-28 14:20:06 -0800277 }
278
Aart Bik01223202016-05-05 15:10:42 -0700279 if (!stay_in_interpreter) {
280 jit::Jit* jit = Runtime::Current()->GetJit();
281 if (jit != nullptr) {
282 jit->MethodEntered(self, shadow_frame.GetMethod());
283 if (jit->CanInvokeCompiledCode(method)) {
284 JValue result;
buzbee734f3aa2016-01-28 14:20:06 -0800285
Aart Bik01223202016-05-05 15:10:42 -0700286 // Pop the shadow frame before calling into compiled code.
287 self->PopShadowFrame();
Jeff Hao5ea84132017-05-05 16:59:29 -0700288 // Calculate the offset of the first input reg. The input registers are in the high regs.
289 // It's ok to access the code item here since JIT code will have been touched by the
290 // interpreter and compiler already.
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800291 uint16_t arg_offset = accessor.RegistersSize() - accessor.InsSize();
Jeff Hao5ea84132017-05-05 16:59:29 -0700292 ArtInterpreterToCompiledCodeBridge(self, nullptr, &shadow_frame, arg_offset, &result);
Aart Bik01223202016-05-05 15:10:42 -0700293 // Push the shadow frame back as the caller will expect it.
294 self->PushShadowFrame(&shadow_frame);
buzbee734f3aa2016-01-28 14:20:06 -0800295
Aart Bik01223202016-05-05 15:10:42 -0700296 return result;
297 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100298 }
buzbee734f3aa2016-01-28 14:20:06 -0800299 }
300 }
301
Andreas Gampe580667b2017-10-23 11:20:39 -0700302 ArtMethod* method = shadow_frame.GetMethod();
303
304 DCheckStaticState(self, method);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200305
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700306 // Lock counting is a special version of accessibility checks, and for simplicity and
307 // reduction of template parameters, we gate it behind access-checks mode.
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700308 DCHECK(!method->SkipAccessChecks() || !method->MustCountLocks());
309
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100310 bool transaction_active = Runtime::Current()->IsActiveTransaction();
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700311 if (LIKELY(method->SkipAccessChecks())) {
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200312 // Enter the "without access check" interpreter.
buzbee1452bee2015-03-06 14:43:04 -0800313 if (kInterpreterImplKind == kMterpImplKind) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100314 if (transaction_active) {
buzbee1452bee2015-03-06 14:43:04 -0800315 // No Mterp variant - just use the switch interpreter.
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800316 return ExecuteSwitchImpl<false, true>(self, accessor, shadow_frame, result_register,
buzbee1452bee2015-03-06 14:43:04 -0800317 false);
Bill Buzbeefd522f92016-02-11 22:37:42 +0000318 } else if (UNLIKELY(!Runtime::Current()->IsStarted())) {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800319 return ExecuteSwitchImpl<false, false>(self, accessor, shadow_frame, result_register,
Bill Buzbeefd522f92016-02-11 22:37:42 +0000320 false);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100321 } else {
buzbee1452bee2015-03-06 14:43:04 -0800322 while (true) {
Bill Buzbeefd522f92016-02-11 22:37:42 +0000323 // Mterp does not support all instrumentation/debugging.
David Srbecky28f6cff2018-10-16 15:07:28 +0100324 if (!self->UseMterp()) {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800325 return ExecuteSwitchImpl<false, false>(self, accessor, shadow_frame, result_register,
buzbee1452bee2015-03-06 14:43:04 -0800326 false);
buzbee1452bee2015-03-06 14:43:04 -0800327 }
Mathieu Chartierfc9555d2017-11-05 16:32:19 -0800328 bool returned = ExecuteMterpImpl(self,
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800329 accessor.Insns(),
Mathieu Chartierfc9555d2017-11-05 16:32:19 -0800330 &shadow_frame,
331 &result_register);
buzbee1452bee2015-03-06 14:43:04 -0800332 if (returned) {
333 return result_register;
334 } else {
335 // Mterp didn't like that instruction. Single-step it with the reference interpreter.
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800336 result_register = ExecuteSwitchImpl<false, false>(self, accessor, shadow_frame,
Mathieu Chartieref41db72016-10-25 15:08:01 -0700337 result_register, true);
Andreas Gampee2abbc62017-09-15 11:59:26 -0700338 if (shadow_frame.GetDexPC() == dex::kDexNoIndex) {
buzbee1452bee2015-03-06 14:43:04 -0800339 // Single-stepped a return or an exception not handled locally. Return to caller.
buzbeed6b48db2016-01-28 15:48:55 -0800340 return result_register;
buzbee1452bee2015-03-06 14:43:04 -0800341 }
342 }
343 }
344 }
buzbeef61df9b2016-09-07 07:12:29 -0700345 } else {
346 DCHECK_EQ(kInterpreterImplKind, kSwitchImplKind);
buzbee1452bee2015-03-06 14:43:04 -0800347 if (transaction_active) {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800348 return ExecuteSwitchImpl<false, true>(self, accessor, shadow_frame, result_register,
buzbee1452bee2015-03-06 14:43:04 -0800349 false);
350 } else {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800351 return ExecuteSwitchImpl<false, false>(self, accessor, shadow_frame, result_register,
buzbee1452bee2015-03-06 14:43:04 -0800352 false);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100353 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200354 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200355 } else {
356 // Enter the "with access check" interpreter.
buzbee1452bee2015-03-06 14:43:04 -0800357 if (kInterpreterImplKind == kMterpImplKind) {
358 // No access check variants for Mterp. Just use the switch version.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100359 if (transaction_active) {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800360 return ExecuteSwitchImpl<true, true>(self, accessor, shadow_frame, result_register,
buzbee1452bee2015-03-06 14:43:04 -0800361 false);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100362 } else {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800363 return ExecuteSwitchImpl<true, false>(self, accessor, shadow_frame, result_register,
buzbee1452bee2015-03-06 14:43:04 -0800364 false);
365 }
buzbeef61df9b2016-09-07 07:12:29 -0700366 } else {
367 DCHECK_EQ(kInterpreterImplKind, kSwitchImplKind);
buzbee1452bee2015-03-06 14:43:04 -0800368 if (transaction_active) {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800369 return ExecuteSwitchImpl<true, true>(self, accessor, shadow_frame, result_register,
buzbee1452bee2015-03-06 14:43:04 -0800370 false);
371 } else {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800372 return ExecuteSwitchImpl<true, false>(self, accessor, shadow_frame, result_register,
buzbee1452bee2015-03-06 14:43:04 -0800373 false);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100374 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200375 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200376 }
377}
378
Mathieu Chartieref41db72016-10-25 15:08:01 -0700379void EnterInterpreterFromInvoke(Thread* self,
380 ArtMethod* method,
381 ObjPtr<mirror::Object> receiver,
382 uint32_t* args,
383 JValue* result,
Aart Bik01223202016-05-05 15:10:42 -0700384 bool stay_in_interpreter) {
Ian Rogers64b6d142012-10-29 16:34:15 -0700385 DCHECK_EQ(self, Thread::Current());
Nicolas Geoffray535a3fb2014-07-22 15:17:38 +0100386 bool implicit_check = !Runtime::Current()->ExplicitStackOverflowChecks();
387 if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEndForInterpreter(implicit_check))) {
jeffhaod7521322012-11-21 15:38:24 -0800388 ThrowStackOverflowError(self);
389 return;
390 }
391
Alex Lightdb01a092017-04-03 15:39:55 -0700392 // This can happen if we are in forced interpreter mode and an obsolete method is called using
393 // reflection.
394 if (UNLIKELY(method->IsObsolete())) {
395 ThrowInternalError("Attempting to invoke obsolete version of '%s'.",
396 method->PrettyMethod().c_str());
397 return;
398 }
399
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700400 const char* old_cause = self->StartAssertNoThreadSuspension("EnterInterpreterFromInvoke");
David Sehr0225f8e2018-01-31 08:52:24 +0000401 CodeItemDataAccessor accessor(method->DexInstructionData());
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700402 uint16_t num_regs;
403 uint16_t num_ins;
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800404 if (accessor.HasCodeItem()) {
405 num_regs = accessor.RegistersSize();
406 num_ins = accessor.InsSize();
Alex Light9139e002015-10-09 15:59:48 -0700407 } else if (!method->IsInvokable()) {
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700408 self->EndAssertNoThreadSuspension(old_cause);
Alex Light9139e002015-10-09 15:59:48 -0700409 method->ThrowInvocationTimeError();
jeffhao0a9bb732012-11-26 12:28:49 -0800410 return;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700411 } else {
412 DCHECK(method->IsNative());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700413 num_regs = num_ins = ArtMethod::NumArgRegisters(method->GetShorty());
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700414 if (!method->IsStatic()) {
415 num_regs++;
416 num_ins++;
417 }
418 }
419 // Set up shadow frame with matching number of reference slots to vregs.
420 ShadowFrame* last_shadow_frame = self->GetManagedStack()->GetTopShadowFrame();
Andreas Gampeb3025922015-09-01 14:45:00 -0700421 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
Andreas Gampe03ec9302015-08-27 17:41:47 -0700422 CREATE_SHADOW_FRAME(num_regs, last_shadow_frame, method, /* dex pc */ 0);
Andreas Gampeb3025922015-09-01 14:45:00 -0700423 ShadowFrame* shadow_frame = shadow_frame_unique_ptr.get();
Jeff Hao66135192013-05-14 11:02:41 -0700424 self->PushShadowFrame(shadow_frame);
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700425
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700426 size_t cur_reg = num_regs - num_ins;
427 if (!method->IsStatic()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700428 CHECK(receiver != nullptr);
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +0100429 shadow_frame->SetVRegReference(cur_reg, receiver);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700430 ++cur_reg;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700431 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700432 uint32_t shorty_len = 0;
433 const char* shorty = method->GetShorty(&shorty_len);
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++) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700435 DCHECK_LT(shorty_pos + 1, shorty_len);
Jeff Hao5d917302013-02-27 17:57:33 -0800436 switch (shorty[shorty_pos + 1]) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700437 case 'L': {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700438 ObjPtr<mirror::Object> o =
439 reinterpret_cast<StackReference<mirror::Object>*>(&args[arg_pos])->AsMirrorPtr();
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +0100440 shadow_frame->SetVRegReference(cur_reg, o);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700441 break;
442 }
Jeff Hao5d917302013-02-27 17:57:33 -0800443 case 'J': case 'D': {
444 uint64_t wide_value = (static_cast<uint64_t>(args[arg_pos + 1]) << 32) | args[arg_pos];
445 shadow_frame->SetVRegLong(cur_reg, wide_value);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700446 cur_reg++;
Jeff Hao5d917302013-02-27 17:57:33 -0800447 arg_pos++;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700448 break;
Jeff Hao5d917302013-02-27 17:57:33 -0800449 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700450 default:
Jeff Hao5d917302013-02-27 17:57:33 -0800451 shadow_frame->SetVReg(cur_reg, args[arg_pos]);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700452 break;
453 }
454 }
Mathieu Chartier92246bb2014-02-25 18:22:39 -0800455 self->EndAssertNoThreadSuspension(old_cause);
456 // Do this after populating the shadow frame in case EnsureInitialized causes a GC.
Ian Rogers6c5cb212014-06-18 16:07:20 -0700457 if (method->IsStatic() && UNLIKELY(!method->GetDeclaringClass()->IsInitialized())) {
Mathieu Chartier92246bb2014-02-25 18:22:39 -0800458 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700459 StackHandleScope<1> hs(self);
460 Handle<mirror::Class> h_class(hs.NewHandle(method->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700461 if (UNLIKELY(!class_linker->EnsureInitialized(self, h_class, true, true))) {
Mathieu Chartier92246bb2014-02-25 18:22:39 -0800462 CHECK(self->IsExceptionPending());
463 self->PopShadowFrame();
464 return;
465 }
466 }
Ian Rogers64b6d142012-10-29 16:34:15 -0700467 if (LIKELY(!method->IsNative())) {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800468 JValue r = Execute(self, accessor, *shadow_frame, JValue(), stay_in_interpreter);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700469 if (result != nullptr) {
Jeff Hao6474d192013-03-26 14:08:09 -0700470 *result = r;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700471 }
472 } else {
Ian Rogers64b6d142012-10-29 16:34:15 -0700473 // We don't expect to be asked to interpret native code (which is entered via a JNI compiler
474 // generated stub) except during testing and image writing.
Mathieu Chartier92246bb2014-02-25 18:22:39 -0800475 // Update args to be the args in the shadow frame since the input ones could hold stale
476 // references pointers due to moving GC.
477 args = shadow_frame->GetVRegArgs(method->IsStatic() ? 0 : 1);
Ian Rogers64b6d142012-10-29 16:34:15 -0700478 if (!Runtime::Current()->IsStarted()) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700479 UnstartedRuntime::Jni(self, method, receiver.Ptr(), args, result);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700480 } else {
Jeff Hao6474d192013-03-26 14:08:09 -0700481 InterpreterJni(self, method, shorty, receiver, args, result);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700482 }
483 }
484 self->PopShadowFrame();
485}
486
Mingyao Yangffedec52016-05-19 10:48:40 -0700487static int16_t GetReceiverRegisterForStringInit(const Instruction* instr) {
488 DCHECK(instr->Opcode() == Instruction::INVOKE_DIRECT_RANGE ||
489 instr->Opcode() == Instruction::INVOKE_DIRECT);
490 return (instr->Opcode() == Instruction::INVOKE_DIRECT_RANGE) ?
491 instr->VRegC_3rc() : instr->VRegC_35c();
492}
493
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100494void EnterInterpreterFromDeoptimize(Thread* self,
495 ShadowFrame* shadow_frame,
Mingyao Yang2ee17902017-08-30 11:37:08 -0700496 JValue* ret_val,
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100497 bool from_code,
Mingyao Yang2ee17902017-08-30 11:37:08 -0700498 DeoptimizationMethodType deopt_method_type)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700499 REQUIRES_SHARED(Locks::mutator_lock_) {
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800500 JValue value;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700501 // Set value to last known result in case the shadow frame chain is empty.
502 value.SetJ(ret_val->GetJ());
Alex Light0aa7a5a2018-10-10 15:58:14 +0000503 // How many frames we have executed.
504 size_t frame_cnt = 0;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700505 while (shadow_frame != nullptr) {
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700506 // We do not want to recover lock state for lock counting when deoptimizing. Currently,
507 // the compiler should not have compiled a method that failed structured-locking checks.
508 DCHECK(!shadow_frame->GetMethod()->MustCountLocks());
509
Ian Rogers62d6c772013-02-27 08:32:07 -0800510 self->SetTopOfShadowStack(shadow_frame);
David Sehr0225f8e2018-01-31 08:52:24 +0000511 CodeItemDataAccessor accessor(shadow_frame->GetMethod()->DexInstructionData());
Sebastien Hertz270a0e12015-01-16 19:49:09 +0100512 const uint32_t dex_pc = shadow_frame->GetDexPC();
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100513 uint32_t new_dex_pc = dex_pc;
Sebastien Hertz270a0e12015-01-16 19:49:09 +0100514 if (UNLIKELY(self->IsExceptionPending())) {
Sebastien Hertz520633b2015-09-08 17:03:36 +0200515 // If we deoptimize from the QuickExceptionHandler, we already reported the exception to
516 // the instrumentation. To prevent from reporting it a second time, we simply pass a
517 // null Instrumentation*.
Sebastien Hertz270a0e12015-01-16 19:49:09 +0100518 const instrumentation::Instrumentation* const instrumentation =
Alex Light0aa7a5a2018-10-10 15:58:14 +0000519 frame_cnt == 0 ? nullptr : Runtime::Current()->GetInstrumentation();
Alex Light9fb1ab12017-09-05 09:32:49 -0700520 new_dex_pc = MoveToExceptionHandler(
Andreas Gampee2abbc62017-09-15 11:59:26 -0700521 self, *shadow_frame, instrumentation) ? shadow_frame->GetDexPC() : dex::kDexNoIndex;
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100522 } else if (!from_code) {
Mingyao Yang2ee17902017-08-30 11:37:08 -0700523 // Deoptimization is not called from code directly.
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800524 const Instruction* instr = &accessor.InstructionAt(dex_pc);
Alex Light0aa7a5a2018-10-10 15:58:14 +0000525 if (deopt_method_type == DeoptimizationMethodType::kKeepDexPc ||
526 shadow_frame->GetForceRetryInstruction()) {
527 DCHECK(frame_cnt == 0 || (frame_cnt == 1 && shadow_frame->GetForceRetryInstruction()))
528 << "frame_cnt: " << frame_cnt
529 << " force-retry: " << shadow_frame->GetForceRetryInstruction();
Mingyao Yang2ee17902017-08-30 11:37:08 -0700530 // Need to re-execute the dex instruction.
531 // (1) An invocation might be split into class initialization and invoke.
532 // In this case, the invoke should not be skipped.
533 // (2) A suspend check should also execute the dex instruction at the
534 // corresponding dex pc.
Alex Light0aa7a5a2018-10-10 15:58:14 +0000535 // If the ForceRetryInstruction bit is set this must be the second frame (the first being
536 // the one that is being popped).
Mingyao Yang2ee17902017-08-30 11:37:08 -0700537 DCHECK_EQ(new_dex_pc, dex_pc);
Alex Light0aa7a5a2018-10-10 15:58:14 +0000538 shadow_frame->SetForceRetryInstruction(false);
Mingyao Yang2ee17902017-08-30 11:37:08 -0700539 } else if (instr->Opcode() == Instruction::MONITOR_ENTER ||
540 instr->Opcode() == Instruction::MONITOR_EXIT) {
541 DCHECK(deopt_method_type == DeoptimizationMethodType::kDefault);
Alex Light0aa7a5a2018-10-10 15:58:14 +0000542 DCHECK_EQ(frame_cnt, 0u);
Mingyao Yang2ee17902017-08-30 11:37:08 -0700543 // Non-idempotent dex instruction should not be re-executed.
544 // On the other hand, if a MONITOR_ENTER is at the dex_pc of a suspend
545 // check, that MONITOR_ENTER should be executed. That case is handled
546 // above.
547 new_dex_pc = dex_pc + instr->SizeInCodeUnits();
548 } else if (instr->IsInvoke()) {
549 DCHECK(deopt_method_type == DeoptimizationMethodType::kDefault);
Mingyao Yangffedec52016-05-19 10:48:40 -0700550 if (IsStringInit(instr, shadow_frame->GetMethod())) {
551 uint16_t this_obj_vreg = GetReceiverRegisterForStringInit(instr);
552 // Move the StringFactory.newStringFromChars() result into the register representing
553 // "this object" when invoking the string constructor in the original dex instruction.
554 // Also move the result into all aliases.
555 DCHECK(value.GetL()->IsString());
556 SetStringInitValueToAllAliases(shadow_frame, this_obj_vreg, value);
557 // Calling string constructor in the original dex code doesn't generate a result value.
558 value.SetJ(0);
559 }
Mingyao Yang504a6902016-04-28 16:23:01 -0700560 new_dex_pc = dex_pc + instr->SizeInCodeUnits();
561 } else if (instr->Opcode() == Instruction::NEW_INSTANCE) {
Mingyao Yang2ee17902017-08-30 11:37:08 -0700562 // A NEW_INSTANCE is simply re-executed, including
563 // "new-instance String" which is compiled into a call into
564 // StringFactory.newEmptyString().
565 DCHECK_EQ(new_dex_pc, dex_pc);
Mingyao Yang504a6902016-04-28 16:23:01 -0700566 } else {
Mingyao Yang2ee17902017-08-30 11:37:08 -0700567 DCHECK(deopt_method_type == DeoptimizationMethodType::kDefault);
Alex Light0aa7a5a2018-10-10 15:58:14 +0000568 DCHECK_EQ(frame_cnt, 0u);
Mingyao Yang2ee17902017-08-30 11:37:08 -0700569 // By default, we re-execute the dex instruction since if they are not
570 // an invoke, so that we don't have to decode the dex instruction to move
571 // result into the right vreg. All slow paths have been audited to be
572 // idempotent except monitor-enter/exit and invocation stubs.
573 // TODO: move result and advance dex pc. That also requires that we
574 // can tell the return type of a runtime method, possibly by decoding
575 // the dex instruction at the caller.
576 DCHECK_EQ(new_dex_pc, dex_pc);
Mingyao Yang504a6902016-04-28 16:23:01 -0700577 }
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100578 } else {
579 // Nothing to do, the dex_pc is the one at which the code requested
580 // the deoptimization.
Alex Light0aa7a5a2018-10-10 15:58:14 +0000581 DCHECK_EQ(frame_cnt, 0u);
Mingyao Yang2ee17902017-08-30 11:37:08 -0700582 DCHECK_EQ(new_dex_pc, dex_pc);
Sebastien Hertz270a0e12015-01-16 19:49:09 +0100583 }
Andreas Gampee2abbc62017-09-15 11:59:26 -0700584 if (new_dex_pc != dex::kDexNoIndex) {
Nicolas Geoffray95974242017-09-04 08:45:51 +0000585 shadow_frame->SetDexPC(new_dex_pc);
Vladimir Markofe948752018-03-23 18:11:43 +0000586 value = Execute(self,
587 accessor,
588 *shadow_frame,
589 value,
590 /* stay_in_interpreter */ true,
591 /* from_deoptimize */ true);
Sebastien Hertz270a0e12015-01-16 19:49:09 +0100592 }
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800593 ShadowFrame* old_frame = shadow_frame;
594 shadow_frame = shadow_frame->GetLink();
Christopher Ferris241a9582015-04-27 15:19:41 -0700595 ShadowFrame::DeleteDeoptimizedFrame(old_frame);
Mingyao Yang2ee17902017-08-30 11:37:08 -0700596 // Following deoptimizations of shadow frames must be at invocation point
597 // and should advance dex pc past the invoke instruction.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100598 from_code = false;
Mingyao Yang2ee17902017-08-30 11:37:08 -0700599 deopt_method_type = DeoptimizationMethodType::kDefault;
Alex Light0aa7a5a2018-10-10 15:58:14 +0000600 frame_cnt++;
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800601 }
602 ret_val->SetJ(value.GetJ());
603}
604
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800605JValue EnterInterpreterFromEntryPoint(Thread* self, const CodeItemDataAccessor& accessor,
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700606 ShadowFrame* shadow_frame) {
Ian Rogersf3e98552013-03-20 15:49:49 -0700607 DCHECK_EQ(self, Thread::Current());
Nicolas Geoffray535a3fb2014-07-22 15:17:38 +0100608 bool implicit_check = !Runtime::Current()->ExplicitStackOverflowChecks();
609 if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEndForInterpreter(implicit_check))) {
Ian Rogersf3e98552013-03-20 15:49:49 -0700610 ThrowStackOverflowError(self);
611 return JValue();
612 }
613
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100614 jit::Jit* jit = Runtime::Current()->GetJit();
615 if (jit != nullptr) {
616 jit->NotifyCompiledCodeToInterpreterTransition(self, shadow_frame->GetMethod());
617 }
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800618 return Execute(self, accessor, *shadow_frame, JValue());
Ian Rogers7db619b2013-01-16 18:35:48 -0800619}
620
Mathieu Chartieref41db72016-10-25 15:08:01 -0700621void ArtInterpreterToInterpreterBridge(Thread* self,
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800622 const CodeItemDataAccessor& accessor,
Mathieu Chartieref41db72016-10-25 15:08:01 -0700623 ShadowFrame* shadow_frame,
624 JValue* result) {
Nicolas Geoffray535a3fb2014-07-22 15:17:38 +0100625 bool implicit_check = !Runtime::Current()->ExplicitStackOverflowChecks();
626 if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEndForInterpreter(implicit_check))) {
Jeff Hao16743632013-05-08 10:59:04 -0700627 ThrowStackOverflowError(self);
Jeff Hao69510672013-05-21 17:34:55 -0700628 return;
Jeff Hao16743632013-05-08 10:59:04 -0700629 }
630
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700631 self->PushShadowFrame(shadow_frame);
Alex Lighteb7c1442015-08-31 13:17:42 -0700632 ArtMethod* method = shadow_frame->GetMethod();
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200633 // Ensure static methods are initialized.
Alex Lighteb7c1442015-08-31 13:17:42 -0700634 const bool is_static = method->IsStatic();
Ian Rogerse94652f2014-12-02 11:13:19 -0800635 if (is_static) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700636 ObjPtr<mirror::Class> declaring_class = method->GetDeclaringClass();
Ian Rogers6c5cb212014-06-18 16:07:20 -0700637 if (UNLIKELY(!declaring_class->IsInitialized())) {
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700638 StackHandleScope<1> hs(self);
Mathieu Chartieref41db72016-10-25 15:08:01 -0700639 HandleWrapperObjPtr<mirror::Class> h_declaring_class(hs.NewHandleWrapper(&declaring_class));
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700640 if (UNLIKELY(!Runtime::Current()->GetClassLinker()->EnsureInitialized(
Ian Rogers7b078e82014-09-10 14:44:24 -0700641 self, h_declaring_class, true, true))) {
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700642 DCHECK(self->IsExceptionPending());
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700643 self->PopShadowFrame();
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200644 return;
645 }
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700646 CHECK(h_declaring_class->IsInitializing());
Jeff Hao16743632013-05-08 10:59:04 -0700647 }
Jeff Hao16743632013-05-08 10:59:04 -0700648 }
Jeff Hao16743632013-05-08 10:59:04 -0700649
Ian Rogerse94652f2014-12-02 11:13:19 -0800650 if (LIKELY(!shadow_frame->GetMethod()->IsNative())) {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800651 result->SetJ(Execute(self, accessor, *shadow_frame, JValue()).GetJ());
Jeff Hao16743632013-05-08 10:59:04 -0700652 } else {
653 // We don't expect to be asked to interpret native code (which is entered via a JNI compiler
654 // generated stub) except during testing and image writing.
655 CHECK(!Runtime::Current()->IsStarted());
Mathieu Chartieref41db72016-10-25 15:08:01 -0700656 ObjPtr<mirror::Object> receiver = is_static ? nullptr : shadow_frame->GetVRegReference(0);
Ian Rogerse94652f2014-12-02 11:13:19 -0800657 uint32_t* args = shadow_frame->GetVRegArgs(is_static ? 0 : 1);
Mathieu Chartieref41db72016-10-25 15:08:01 -0700658 UnstartedRuntime::Jni(self, shadow_frame->GetMethod(), receiver.Ptr(), args, result);
Jeff Hao16743632013-05-08 10:59:04 -0700659 }
660
661 self->PopShadowFrame();
Jeff Hao16743632013-05-08 10:59:04 -0700662}
663
buzbee1452bee2015-03-06 14:43:04 -0800664void CheckInterpreterAsmConstants() {
665 CheckMterpAsmConstants();
666}
667
668void InitInterpreterTls(Thread* self) {
669 InitMterpTls(self);
670}
671
Alex Light0aa7a5a2018-10-10 15:58:14 +0000672bool PrevFrameWillRetry(Thread* self, const ShadowFrame& frame) {
673 ShadowFrame* prev_frame = frame.GetLink();
674 if (prev_frame == nullptr) {
675 NthCallerVisitor vis(self, 1, false);
676 vis.WalkStack();
677 prev_frame = vis.GetCurrentShadowFrame();
678 if (prev_frame == nullptr) {
679 prev_frame = self->FindDebuggerShadowFrame(vis.GetFrameId());
680 }
681 }
682 return prev_frame != nullptr && prev_frame->GetForceRetryInstruction();
683}
684
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700685} // namespace interpreter
686} // namespace art