blob: df66061d0100c8a4e0f8d42851dc306ad44714df [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 Lightb7edcda2017-04-27 13:20:31 -0700264 if (UNLIKELY(self->IsExceptionPending())) {
265 instrumentation->MethodUnwindEvent(self,
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800266 shadow_frame.GetThisObject(accessor.InsSize()),
Alex Lightb7edcda2017-04-27 13:20:31 -0700267 method,
268 0);
269 return JValue();
270 }
buzbee734f3aa2016-01-28 14:20:06 -0800271 }
272
Aart Bik01223202016-05-05 15:10:42 -0700273 if (!stay_in_interpreter) {
274 jit::Jit* jit = Runtime::Current()->GetJit();
275 if (jit != nullptr) {
276 jit->MethodEntered(self, shadow_frame.GetMethod());
277 if (jit->CanInvokeCompiledCode(method)) {
278 JValue result;
buzbee734f3aa2016-01-28 14:20:06 -0800279
Aart Bik01223202016-05-05 15:10:42 -0700280 // Pop the shadow frame before calling into compiled code.
281 self->PopShadowFrame();
Jeff Hao5ea84132017-05-05 16:59:29 -0700282 // Calculate the offset of the first input reg. The input registers are in the high regs.
283 // It's ok to access the code item here since JIT code will have been touched by the
284 // interpreter and compiler already.
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800285 uint16_t arg_offset = accessor.RegistersSize() - accessor.InsSize();
Jeff Hao5ea84132017-05-05 16:59:29 -0700286 ArtInterpreterToCompiledCodeBridge(self, nullptr, &shadow_frame, arg_offset, &result);
Aart Bik01223202016-05-05 15:10:42 -0700287 // Push the shadow frame back as the caller will expect it.
288 self->PushShadowFrame(&shadow_frame);
buzbee734f3aa2016-01-28 14:20:06 -0800289
Aart Bik01223202016-05-05 15:10:42 -0700290 return result;
291 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100292 }
buzbee734f3aa2016-01-28 14:20:06 -0800293 }
294 }
295
Andreas Gampe580667b2017-10-23 11:20:39 -0700296 ArtMethod* method = shadow_frame.GetMethod();
297
298 DCheckStaticState(self, method);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200299
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700300 // Lock counting is a special version of accessibility checks, and for simplicity and
301 // reduction of template parameters, we gate it behind access-checks mode.
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700302 DCHECK(!method->SkipAccessChecks() || !method->MustCountLocks());
303
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100304 bool transaction_active = Runtime::Current()->IsActiveTransaction();
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700305 if (LIKELY(method->SkipAccessChecks())) {
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200306 // Enter the "without access check" interpreter.
buzbee1452bee2015-03-06 14:43:04 -0800307 if (kInterpreterImplKind == kMterpImplKind) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100308 if (transaction_active) {
buzbee1452bee2015-03-06 14:43:04 -0800309 // No Mterp variant - just use the switch interpreter.
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800310 return ExecuteSwitchImpl<false, true>(self, accessor, shadow_frame, result_register,
buzbee1452bee2015-03-06 14:43:04 -0800311 false);
Bill Buzbeefd522f92016-02-11 22:37:42 +0000312 } else if (UNLIKELY(!Runtime::Current()->IsStarted())) {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800313 return ExecuteSwitchImpl<false, false>(self, accessor, shadow_frame, result_register,
Bill Buzbeefd522f92016-02-11 22:37:42 +0000314 false);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100315 } else {
buzbee1452bee2015-03-06 14:43:04 -0800316 while (true) {
Bill Buzbeefd522f92016-02-11 22:37:42 +0000317 // Mterp does not support all instrumentation/debugging.
Andreas Gampe67409972016-07-19 22:34:53 -0700318 if (MterpShouldSwitchInterpreters() != 0) {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800319 return ExecuteSwitchImpl<false, false>(self, accessor, shadow_frame, result_register,
buzbee1452bee2015-03-06 14:43:04 -0800320 false);
buzbee1452bee2015-03-06 14:43:04 -0800321 }
Mathieu Chartierfc9555d2017-11-05 16:32:19 -0800322 bool returned = ExecuteMterpImpl(self,
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800323 accessor.Insns(),
Mathieu Chartierfc9555d2017-11-05 16:32:19 -0800324 &shadow_frame,
325 &result_register);
buzbee1452bee2015-03-06 14:43:04 -0800326 if (returned) {
327 return result_register;
328 } else {
329 // Mterp didn't like that instruction. Single-step it with the reference interpreter.
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800330 result_register = ExecuteSwitchImpl<false, false>(self, accessor, shadow_frame,
Mathieu Chartieref41db72016-10-25 15:08:01 -0700331 result_register, true);
Andreas Gampee2abbc62017-09-15 11:59:26 -0700332 if (shadow_frame.GetDexPC() == dex::kDexNoIndex) {
buzbee1452bee2015-03-06 14:43:04 -0800333 // Single-stepped a return or an exception not handled locally. Return to caller.
buzbeed6b48db2016-01-28 15:48:55 -0800334 return result_register;
buzbee1452bee2015-03-06 14:43:04 -0800335 }
336 }
337 }
338 }
buzbeef61df9b2016-09-07 07:12:29 -0700339 } else {
340 DCHECK_EQ(kInterpreterImplKind, kSwitchImplKind);
buzbee1452bee2015-03-06 14:43:04 -0800341 if (transaction_active) {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800342 return ExecuteSwitchImpl<false, true>(self, accessor, shadow_frame, result_register,
buzbee1452bee2015-03-06 14:43:04 -0800343 false);
344 } else {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800345 return ExecuteSwitchImpl<false, false>(self, accessor, shadow_frame, result_register,
buzbee1452bee2015-03-06 14:43:04 -0800346 false);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100347 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200348 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200349 } else {
350 // Enter the "with access check" interpreter.
buzbee1452bee2015-03-06 14:43:04 -0800351 if (kInterpreterImplKind == kMterpImplKind) {
352 // No access check variants for Mterp. Just use the switch version.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100353 if (transaction_active) {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800354 return ExecuteSwitchImpl<true, true>(self, accessor, shadow_frame, result_register,
buzbee1452bee2015-03-06 14:43:04 -0800355 false);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100356 } else {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800357 return ExecuteSwitchImpl<true, false>(self, accessor, shadow_frame, result_register,
buzbee1452bee2015-03-06 14:43:04 -0800358 false);
359 }
buzbeef61df9b2016-09-07 07:12:29 -0700360 } else {
361 DCHECK_EQ(kInterpreterImplKind, kSwitchImplKind);
buzbee1452bee2015-03-06 14:43:04 -0800362 if (transaction_active) {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800363 return ExecuteSwitchImpl<true, true>(self, accessor, shadow_frame, result_register,
buzbee1452bee2015-03-06 14:43:04 -0800364 false);
365 } else {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800366 return ExecuteSwitchImpl<true, false>(self, accessor, shadow_frame, result_register,
buzbee1452bee2015-03-06 14:43:04 -0800367 false);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100368 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200369 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200370 }
371}
372
Mathieu Chartieref41db72016-10-25 15:08:01 -0700373void EnterInterpreterFromInvoke(Thread* self,
374 ArtMethod* method,
375 ObjPtr<mirror::Object> receiver,
376 uint32_t* args,
377 JValue* result,
Aart Bik01223202016-05-05 15:10:42 -0700378 bool stay_in_interpreter) {
Ian Rogers64b6d142012-10-29 16:34:15 -0700379 DCHECK_EQ(self, Thread::Current());
Nicolas Geoffray535a3fb2014-07-22 15:17:38 +0100380 bool implicit_check = !Runtime::Current()->ExplicitStackOverflowChecks();
381 if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEndForInterpreter(implicit_check))) {
jeffhaod7521322012-11-21 15:38:24 -0800382 ThrowStackOverflowError(self);
383 return;
384 }
385
Alex Lightdb01a092017-04-03 15:39:55 -0700386 // This can happen if we are in forced interpreter mode and an obsolete method is called using
387 // reflection.
388 if (UNLIKELY(method->IsObsolete())) {
389 ThrowInternalError("Attempting to invoke obsolete version of '%s'.",
390 method->PrettyMethod().c_str());
391 return;
392 }
393
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700394 const char* old_cause = self->StartAssertNoThreadSuspension("EnterInterpreterFromInvoke");
David Sehr0225f8e2018-01-31 08:52:24 +0000395 CodeItemDataAccessor accessor(method->DexInstructionData());
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700396 uint16_t num_regs;
397 uint16_t num_ins;
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800398 if (accessor.HasCodeItem()) {
399 num_regs = accessor.RegistersSize();
400 num_ins = accessor.InsSize();
Alex Light9139e002015-10-09 15:59:48 -0700401 } else if (!method->IsInvokable()) {
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700402 self->EndAssertNoThreadSuspension(old_cause);
Alex Light9139e002015-10-09 15:59:48 -0700403 method->ThrowInvocationTimeError();
jeffhao0a9bb732012-11-26 12:28:49 -0800404 return;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700405 } else {
406 DCHECK(method->IsNative());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700407 num_regs = num_ins = ArtMethod::NumArgRegisters(method->GetShorty());
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700408 if (!method->IsStatic()) {
409 num_regs++;
410 num_ins++;
411 }
412 }
413 // Set up shadow frame with matching number of reference slots to vregs.
414 ShadowFrame* last_shadow_frame = self->GetManagedStack()->GetTopShadowFrame();
Andreas Gampeb3025922015-09-01 14:45:00 -0700415 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
Andreas Gampe03ec9302015-08-27 17:41:47 -0700416 CREATE_SHADOW_FRAME(num_regs, last_shadow_frame, method, /* dex pc */ 0);
Andreas Gampeb3025922015-09-01 14:45:00 -0700417 ShadowFrame* shadow_frame = shadow_frame_unique_ptr.get();
Jeff Hao66135192013-05-14 11:02:41 -0700418 self->PushShadowFrame(shadow_frame);
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700419
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700420 size_t cur_reg = num_regs - num_ins;
421 if (!method->IsStatic()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700422 CHECK(receiver != nullptr);
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +0100423 shadow_frame->SetVRegReference(cur_reg, receiver);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700424 ++cur_reg;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700425 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700426 uint32_t shorty_len = 0;
427 const char* shorty = method->GetShorty(&shorty_len);
Jeff Hao5d917302013-02-27 17:57:33 -0800428 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 -0700429 DCHECK_LT(shorty_pos + 1, shorty_len);
Jeff Hao5d917302013-02-27 17:57:33 -0800430 switch (shorty[shorty_pos + 1]) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700431 case 'L': {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700432 ObjPtr<mirror::Object> o =
433 reinterpret_cast<StackReference<mirror::Object>*>(&args[arg_pos])->AsMirrorPtr();
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +0100434 shadow_frame->SetVRegReference(cur_reg, o);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700435 break;
436 }
Jeff Hao5d917302013-02-27 17:57:33 -0800437 case 'J': case 'D': {
438 uint64_t wide_value = (static_cast<uint64_t>(args[arg_pos + 1]) << 32) | args[arg_pos];
439 shadow_frame->SetVRegLong(cur_reg, wide_value);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700440 cur_reg++;
Jeff Hao5d917302013-02-27 17:57:33 -0800441 arg_pos++;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700442 break;
Jeff Hao5d917302013-02-27 17:57:33 -0800443 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700444 default:
Jeff Hao5d917302013-02-27 17:57:33 -0800445 shadow_frame->SetVReg(cur_reg, args[arg_pos]);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700446 break;
447 }
448 }
Mathieu Chartier92246bb2014-02-25 18:22:39 -0800449 self->EndAssertNoThreadSuspension(old_cause);
450 // Do this after populating the shadow frame in case EnsureInitialized causes a GC.
Ian Rogers6c5cb212014-06-18 16:07:20 -0700451 if (method->IsStatic() && UNLIKELY(!method->GetDeclaringClass()->IsInitialized())) {
Mathieu Chartier92246bb2014-02-25 18:22:39 -0800452 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700453 StackHandleScope<1> hs(self);
454 Handle<mirror::Class> h_class(hs.NewHandle(method->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700455 if (UNLIKELY(!class_linker->EnsureInitialized(self, h_class, true, true))) {
Mathieu Chartier92246bb2014-02-25 18:22:39 -0800456 CHECK(self->IsExceptionPending());
457 self->PopShadowFrame();
458 return;
459 }
460 }
Ian Rogers64b6d142012-10-29 16:34:15 -0700461 if (LIKELY(!method->IsNative())) {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800462 JValue r = Execute(self, accessor, *shadow_frame, JValue(), stay_in_interpreter);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700463 if (result != nullptr) {
Jeff Hao6474d192013-03-26 14:08:09 -0700464 *result = r;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700465 }
466 } else {
Ian Rogers64b6d142012-10-29 16:34:15 -0700467 // We don't expect to be asked to interpret native code (which is entered via a JNI compiler
468 // generated stub) except during testing and image writing.
Mathieu Chartier92246bb2014-02-25 18:22:39 -0800469 // Update args to be the args in the shadow frame since the input ones could hold stale
470 // references pointers due to moving GC.
471 args = shadow_frame->GetVRegArgs(method->IsStatic() ? 0 : 1);
Ian Rogers64b6d142012-10-29 16:34:15 -0700472 if (!Runtime::Current()->IsStarted()) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700473 UnstartedRuntime::Jni(self, method, receiver.Ptr(), args, result);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700474 } else {
Jeff Hao6474d192013-03-26 14:08:09 -0700475 InterpreterJni(self, method, shorty, receiver, args, result);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700476 }
477 }
478 self->PopShadowFrame();
479}
480
Mingyao Yangffedec52016-05-19 10:48:40 -0700481static int16_t GetReceiverRegisterForStringInit(const Instruction* instr) {
482 DCHECK(instr->Opcode() == Instruction::INVOKE_DIRECT_RANGE ||
483 instr->Opcode() == Instruction::INVOKE_DIRECT);
484 return (instr->Opcode() == Instruction::INVOKE_DIRECT_RANGE) ?
485 instr->VRegC_3rc() : instr->VRegC_35c();
486}
487
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100488void EnterInterpreterFromDeoptimize(Thread* self,
489 ShadowFrame* shadow_frame,
Mingyao Yang2ee17902017-08-30 11:37:08 -0700490 JValue* ret_val,
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100491 bool from_code,
Mingyao Yang2ee17902017-08-30 11:37:08 -0700492 DeoptimizationMethodType deopt_method_type)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700493 REQUIRES_SHARED(Locks::mutator_lock_) {
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800494 JValue value;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700495 // Set value to last known result in case the shadow frame chain is empty.
496 value.SetJ(ret_val->GetJ());
Sebastien Hertz520633b2015-09-08 17:03:36 +0200497 // Are we executing the first shadow frame?
498 bool first = true;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700499 while (shadow_frame != nullptr) {
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700500 // We do not want to recover lock state for lock counting when deoptimizing. Currently,
501 // the compiler should not have compiled a method that failed structured-locking checks.
502 DCHECK(!shadow_frame->GetMethod()->MustCountLocks());
503
Ian Rogers62d6c772013-02-27 08:32:07 -0800504 self->SetTopOfShadowStack(shadow_frame);
David Sehr0225f8e2018-01-31 08:52:24 +0000505 CodeItemDataAccessor accessor(shadow_frame->GetMethod()->DexInstructionData());
Sebastien Hertz270a0e12015-01-16 19:49:09 +0100506 const uint32_t dex_pc = shadow_frame->GetDexPC();
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100507 uint32_t new_dex_pc = dex_pc;
Sebastien Hertz270a0e12015-01-16 19:49:09 +0100508 if (UNLIKELY(self->IsExceptionPending())) {
Sebastien Hertz520633b2015-09-08 17:03:36 +0200509 // If we deoptimize from the QuickExceptionHandler, we already reported the exception to
510 // the instrumentation. To prevent from reporting it a second time, we simply pass a
511 // null Instrumentation*.
Sebastien Hertz270a0e12015-01-16 19:49:09 +0100512 const instrumentation::Instrumentation* const instrumentation =
Sebastien Hertz520633b2015-09-08 17:03:36 +0200513 first ? nullptr : Runtime::Current()->GetInstrumentation();
Alex Light9fb1ab12017-09-05 09:32:49 -0700514 new_dex_pc = MoveToExceptionHandler(
Andreas Gampee2abbc62017-09-15 11:59:26 -0700515 self, *shadow_frame, instrumentation) ? shadow_frame->GetDexPC() : dex::kDexNoIndex;
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100516 } else if (!from_code) {
Mingyao Yang2ee17902017-08-30 11:37:08 -0700517 // Deoptimization is not called from code directly.
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800518 const Instruction* instr = &accessor.InstructionAt(dex_pc);
Mingyao Yang2ee17902017-08-30 11:37:08 -0700519 if (deopt_method_type == DeoptimizationMethodType::kKeepDexPc) {
520 DCHECK(first);
521 // Need to re-execute the dex instruction.
522 // (1) An invocation might be split into class initialization and invoke.
523 // In this case, the invoke should not be skipped.
524 // (2) A suspend check should also execute the dex instruction at the
525 // corresponding dex pc.
526 DCHECK_EQ(new_dex_pc, dex_pc);
527 } else if (instr->Opcode() == Instruction::MONITOR_ENTER ||
528 instr->Opcode() == Instruction::MONITOR_EXIT) {
529 DCHECK(deopt_method_type == DeoptimizationMethodType::kDefault);
530 DCHECK(first);
531 // Non-idempotent dex instruction should not be re-executed.
532 // On the other hand, if a MONITOR_ENTER is at the dex_pc of a suspend
533 // check, that MONITOR_ENTER should be executed. That case is handled
534 // above.
535 new_dex_pc = dex_pc + instr->SizeInCodeUnits();
536 } else if (instr->IsInvoke()) {
537 DCHECK(deopt_method_type == DeoptimizationMethodType::kDefault);
Mingyao Yangffedec52016-05-19 10:48:40 -0700538 if (IsStringInit(instr, shadow_frame->GetMethod())) {
539 uint16_t this_obj_vreg = GetReceiverRegisterForStringInit(instr);
540 // Move the StringFactory.newStringFromChars() result into the register representing
541 // "this object" when invoking the string constructor in the original dex instruction.
542 // Also move the result into all aliases.
543 DCHECK(value.GetL()->IsString());
544 SetStringInitValueToAllAliases(shadow_frame, this_obj_vreg, value);
545 // Calling string constructor in the original dex code doesn't generate a result value.
546 value.SetJ(0);
547 }
Mingyao Yang504a6902016-04-28 16:23:01 -0700548 new_dex_pc = dex_pc + instr->SizeInCodeUnits();
549 } else if (instr->Opcode() == Instruction::NEW_INSTANCE) {
Mingyao Yang2ee17902017-08-30 11:37:08 -0700550 // A NEW_INSTANCE is simply re-executed, including
551 // "new-instance String" which is compiled into a call into
552 // StringFactory.newEmptyString().
553 DCHECK_EQ(new_dex_pc, dex_pc);
Mingyao Yang504a6902016-04-28 16:23:01 -0700554 } else {
Mingyao Yang2ee17902017-08-30 11:37:08 -0700555 DCHECK(deopt_method_type == DeoptimizationMethodType::kDefault);
556 DCHECK(first);
557 // By default, we re-execute the dex instruction since if they are not
558 // an invoke, so that we don't have to decode the dex instruction to move
559 // result into the right vreg. All slow paths have been audited to be
560 // idempotent except monitor-enter/exit and invocation stubs.
561 // TODO: move result and advance dex pc. That also requires that we
562 // can tell the return type of a runtime method, possibly by decoding
563 // the dex instruction at the caller.
564 DCHECK_EQ(new_dex_pc, dex_pc);
Mingyao Yang504a6902016-04-28 16:23:01 -0700565 }
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100566 } else {
567 // Nothing to do, the dex_pc is the one at which the code requested
568 // the deoptimization.
Mingyao Yang2ee17902017-08-30 11:37:08 -0700569 DCHECK(first);
570 DCHECK_EQ(new_dex_pc, dex_pc);
Sebastien Hertz270a0e12015-01-16 19:49:09 +0100571 }
Andreas Gampee2abbc62017-09-15 11:59:26 -0700572 if (new_dex_pc != dex::kDexNoIndex) {
Nicolas Geoffray95974242017-09-04 08:45:51 +0000573 shadow_frame->SetDexPC(new_dex_pc);
Vladimir Markofe948752018-03-23 18:11:43 +0000574 value = Execute(self,
575 accessor,
576 *shadow_frame,
577 value,
578 /* stay_in_interpreter */ true,
579 /* from_deoptimize */ true);
Sebastien Hertz270a0e12015-01-16 19:49:09 +0100580 }
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800581 ShadowFrame* old_frame = shadow_frame;
582 shadow_frame = shadow_frame->GetLink();
Christopher Ferris241a9582015-04-27 15:19:41 -0700583 ShadowFrame::DeleteDeoptimizedFrame(old_frame);
Mingyao Yang2ee17902017-08-30 11:37:08 -0700584 // Following deoptimizations of shadow frames must be at invocation point
585 // and should advance dex pc past the invoke instruction.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100586 from_code = false;
Mingyao Yang2ee17902017-08-30 11:37:08 -0700587 deopt_method_type = DeoptimizationMethodType::kDefault;
Sebastien Hertz520633b2015-09-08 17:03:36 +0200588 first = false;
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800589 }
590 ret_val->SetJ(value.GetJ());
591}
592
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800593JValue EnterInterpreterFromEntryPoint(Thread* self, const CodeItemDataAccessor& accessor,
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700594 ShadowFrame* shadow_frame) {
Ian Rogersf3e98552013-03-20 15:49:49 -0700595 DCHECK_EQ(self, Thread::Current());
Nicolas Geoffray535a3fb2014-07-22 15:17:38 +0100596 bool implicit_check = !Runtime::Current()->ExplicitStackOverflowChecks();
597 if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEndForInterpreter(implicit_check))) {
Ian Rogersf3e98552013-03-20 15:49:49 -0700598 ThrowStackOverflowError(self);
599 return JValue();
600 }
601
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100602 jit::Jit* jit = Runtime::Current()->GetJit();
603 if (jit != nullptr) {
604 jit->NotifyCompiledCodeToInterpreterTransition(self, shadow_frame->GetMethod());
605 }
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800606 return Execute(self, accessor, *shadow_frame, JValue());
Ian Rogers7db619b2013-01-16 18:35:48 -0800607}
608
Mathieu Chartieref41db72016-10-25 15:08:01 -0700609void ArtInterpreterToInterpreterBridge(Thread* self,
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800610 const CodeItemDataAccessor& accessor,
Mathieu Chartieref41db72016-10-25 15:08:01 -0700611 ShadowFrame* shadow_frame,
612 JValue* result) {
Nicolas Geoffray535a3fb2014-07-22 15:17:38 +0100613 bool implicit_check = !Runtime::Current()->ExplicitStackOverflowChecks();
614 if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEndForInterpreter(implicit_check))) {
Jeff Hao16743632013-05-08 10:59:04 -0700615 ThrowStackOverflowError(self);
Jeff Hao69510672013-05-21 17:34:55 -0700616 return;
Jeff Hao16743632013-05-08 10:59:04 -0700617 }
618
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700619 self->PushShadowFrame(shadow_frame);
Alex Lighteb7c1442015-08-31 13:17:42 -0700620 ArtMethod* method = shadow_frame->GetMethod();
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200621 // Ensure static methods are initialized.
Alex Lighteb7c1442015-08-31 13:17:42 -0700622 const bool is_static = method->IsStatic();
Ian Rogerse94652f2014-12-02 11:13:19 -0800623 if (is_static) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700624 ObjPtr<mirror::Class> declaring_class = method->GetDeclaringClass();
Ian Rogers6c5cb212014-06-18 16:07:20 -0700625 if (UNLIKELY(!declaring_class->IsInitialized())) {
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700626 StackHandleScope<1> hs(self);
Mathieu Chartieref41db72016-10-25 15:08:01 -0700627 HandleWrapperObjPtr<mirror::Class> h_declaring_class(hs.NewHandleWrapper(&declaring_class));
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700628 if (UNLIKELY(!Runtime::Current()->GetClassLinker()->EnsureInitialized(
Ian Rogers7b078e82014-09-10 14:44:24 -0700629 self, h_declaring_class, true, true))) {
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700630 DCHECK(self->IsExceptionPending());
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700631 self->PopShadowFrame();
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200632 return;
633 }
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700634 CHECK(h_declaring_class->IsInitializing());
Jeff Hao16743632013-05-08 10:59:04 -0700635 }
Jeff Hao16743632013-05-08 10:59:04 -0700636 }
Jeff Hao16743632013-05-08 10:59:04 -0700637
Ian Rogerse94652f2014-12-02 11:13:19 -0800638 if (LIKELY(!shadow_frame->GetMethod()->IsNative())) {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800639 result->SetJ(Execute(self, accessor, *shadow_frame, JValue()).GetJ());
Jeff Hao16743632013-05-08 10:59:04 -0700640 } else {
641 // We don't expect to be asked to interpret native code (which is entered via a JNI compiler
642 // generated stub) except during testing and image writing.
643 CHECK(!Runtime::Current()->IsStarted());
Mathieu Chartieref41db72016-10-25 15:08:01 -0700644 ObjPtr<mirror::Object> receiver = is_static ? nullptr : shadow_frame->GetVRegReference(0);
Ian Rogerse94652f2014-12-02 11:13:19 -0800645 uint32_t* args = shadow_frame->GetVRegArgs(is_static ? 0 : 1);
Mathieu Chartieref41db72016-10-25 15:08:01 -0700646 UnstartedRuntime::Jni(self, shadow_frame->GetMethod(), receiver.Ptr(), args, result);
Jeff Hao16743632013-05-08 10:59:04 -0700647 }
648
649 self->PopShadowFrame();
Jeff Hao16743632013-05-08 10:59:04 -0700650}
651
buzbee1452bee2015-03-06 14:43:04 -0800652void CheckInterpreterAsmConstants() {
653 CheckMterpAsmConstants();
654}
655
656void InitInterpreterTls(Thread* self) {
657 InitMterpTls(self);
658}
659
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700660} // namespace interpreter
661} // namespace art