blob: 2e41a9dec280ba59de2d83677c37592f2f8024dc [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
David Srbecky8ed45c82018-11-08 15:08:57 +0000240#if ART_USE_CXX_INTERPRETER
241static constexpr InterpreterImplKind kInterpreterImplKind = kSwitchImplKind;
242#else
buzbee1452bee2015-03-06 14:43:04 -0800243static constexpr InterpreterImplKind kInterpreterImplKind = kMterpImplKind;
David Srbecky8ed45c82018-11-08 15:08:57 +0000244#endif
Alexey Frunze00b53b72016-02-02 20:25:45 -0800245
Aart Bik01223202016-05-05 15:10:42 -0700246static inline JValue Execute(
247 Thread* self,
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800248 const CodeItemDataAccessor& accessor,
Aart Bik01223202016-05-05 15:10:42 -0700249 ShadowFrame& shadow_frame,
250 JValue result_register,
Vladimir Markofe948752018-03-23 18:11:43 +0000251 bool stay_in_interpreter = false,
252 bool from_deoptimize = false) REQUIRES_SHARED(Locks::mutator_lock_) {
buzbee1452bee2015-03-06 14:43:04 -0800253 DCHECK(!shadow_frame.GetMethod()->IsAbstract());
Ian Rogers848871b2013-08-05 10:56:33 -0700254 DCHECK(!shadow_frame.GetMethod()->IsNative());
David Srbeckycb4f09e2018-10-21 08:45:22 +0100255
Lokesh Gidra9fd68f62018-12-21 00:33:46 +0000256 // Check that we are using the right interpreter.
257 if (kIsDebugBuild && self->UseMterp() != CanUseMterp()) {
258 // The flag might be currently being updated on all threads. Retry with lock.
259 MutexLock tll_mu(self, *Locks::thread_list_lock_);
260 DCHECK_EQ(self->UseMterp(), CanUseMterp());
261 }
262
Vladimir Markofe948752018-03-23 18:11:43 +0000263 if (LIKELY(!from_deoptimize)) { // Entering the method, but not via deoptimization.
buzbee734f3aa2016-01-28 14:20:06 -0800264 if (kIsDebugBuild) {
Vladimir Markofe948752018-03-23 18:11:43 +0000265 CHECK_EQ(shadow_frame.GetDexPC(), 0u);
buzbee734f3aa2016-01-28 14:20:06 -0800266 self->AssertNoPendingException();
267 }
268 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
269 ArtMethod *method = shadow_frame.GetMethod();
270
271 if (UNLIKELY(instrumentation->HasMethodEntryListeners())) {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800272 instrumentation->MethodEnterEvent(self,
273 shadow_frame.GetThisObject(accessor.InsSize()),
274 method,
275 0);
Alex Light0aa7a5a2018-10-10 15:58:14 +0000276 if (UNLIKELY(shadow_frame.GetForcePopFrame())) {
277 // The caller will retry this invoke. Just return immediately without any value.
278 DCHECK(Runtime::Current()->AreNonStandardExitsEnabled());
279 DCHECK(PrevFrameWillRetry(self, shadow_frame));
280 return JValue();
281 }
Alex Lightb7edcda2017-04-27 13:20:31 -0700282 if (UNLIKELY(self->IsExceptionPending())) {
283 instrumentation->MethodUnwindEvent(self,
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800284 shadow_frame.GetThisObject(accessor.InsSize()),
Alex Lightb7edcda2017-04-27 13:20:31 -0700285 method,
286 0);
287 return JValue();
288 }
buzbee734f3aa2016-01-28 14:20:06 -0800289 }
290
Aart Bik01223202016-05-05 15:10:42 -0700291 if (!stay_in_interpreter) {
292 jit::Jit* jit = Runtime::Current()->GetJit();
293 if (jit != nullptr) {
294 jit->MethodEntered(self, shadow_frame.GetMethod());
295 if (jit->CanInvokeCompiledCode(method)) {
296 JValue result;
buzbee734f3aa2016-01-28 14:20:06 -0800297
Aart Bik01223202016-05-05 15:10:42 -0700298 // Pop the shadow frame before calling into compiled code.
299 self->PopShadowFrame();
Jeff Hao5ea84132017-05-05 16:59:29 -0700300 // Calculate the offset of the first input reg. The input registers are in the high regs.
301 // It's ok to access the code item here since JIT code will have been touched by the
302 // interpreter and compiler already.
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800303 uint16_t arg_offset = accessor.RegistersSize() - accessor.InsSize();
Jeff Hao5ea84132017-05-05 16:59:29 -0700304 ArtInterpreterToCompiledCodeBridge(self, nullptr, &shadow_frame, arg_offset, &result);
Aart Bik01223202016-05-05 15:10:42 -0700305 // Push the shadow frame back as the caller will expect it.
306 self->PushShadowFrame(&shadow_frame);
buzbee734f3aa2016-01-28 14:20:06 -0800307
Aart Bik01223202016-05-05 15:10:42 -0700308 return result;
309 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100310 }
buzbee734f3aa2016-01-28 14:20:06 -0800311 }
312 }
313
Andreas Gampe580667b2017-10-23 11:20:39 -0700314 ArtMethod* method = shadow_frame.GetMethod();
315
316 DCheckStaticState(self, method);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200317
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700318 // Lock counting is a special version of accessibility checks, and for simplicity and
319 // reduction of template parameters, we gate it behind access-checks mode.
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700320 DCHECK(!method->SkipAccessChecks() || !method->MustCountLocks());
321
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100322 bool transaction_active = Runtime::Current()->IsActiveTransaction();
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700323 if (LIKELY(method->SkipAccessChecks())) {
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200324 // Enter the "without access check" interpreter.
buzbee1452bee2015-03-06 14:43:04 -0800325 if (kInterpreterImplKind == kMterpImplKind) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100326 if (transaction_active) {
buzbee1452bee2015-03-06 14:43:04 -0800327 // No Mterp variant - just use the switch interpreter.
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800328 return ExecuteSwitchImpl<false, true>(self, accessor, shadow_frame, result_register,
buzbee1452bee2015-03-06 14:43:04 -0800329 false);
Bill Buzbeefd522f92016-02-11 22:37:42 +0000330 } else if (UNLIKELY(!Runtime::Current()->IsStarted())) {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800331 return ExecuteSwitchImpl<false, false>(self, accessor, shadow_frame, result_register,
Bill Buzbeefd522f92016-02-11 22:37:42 +0000332 false);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100333 } else {
buzbee1452bee2015-03-06 14:43:04 -0800334 while (true) {
Bill Buzbeefd522f92016-02-11 22:37:42 +0000335 // Mterp does not support all instrumentation/debugging.
David Srbecky28f6cff2018-10-16 15:07:28 +0100336 if (!self->UseMterp()) {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800337 return ExecuteSwitchImpl<false, false>(self, accessor, shadow_frame, result_register,
buzbee1452bee2015-03-06 14:43:04 -0800338 false);
buzbee1452bee2015-03-06 14:43:04 -0800339 }
Mathieu Chartierfc9555d2017-11-05 16:32:19 -0800340 bool returned = ExecuteMterpImpl(self,
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800341 accessor.Insns(),
Mathieu Chartierfc9555d2017-11-05 16:32:19 -0800342 &shadow_frame,
343 &result_register);
buzbee1452bee2015-03-06 14:43:04 -0800344 if (returned) {
345 return result_register;
346 } else {
347 // Mterp didn't like that instruction. Single-step it with the reference interpreter.
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800348 result_register = ExecuteSwitchImpl<false, false>(self, accessor, shadow_frame,
Mathieu Chartieref41db72016-10-25 15:08:01 -0700349 result_register, true);
Andreas Gampee2abbc62017-09-15 11:59:26 -0700350 if (shadow_frame.GetDexPC() == dex::kDexNoIndex) {
buzbee1452bee2015-03-06 14:43:04 -0800351 // Single-stepped a return or an exception not handled locally. Return to caller.
buzbeed6b48db2016-01-28 15:48:55 -0800352 return result_register;
buzbee1452bee2015-03-06 14:43:04 -0800353 }
354 }
355 }
356 }
buzbeef61df9b2016-09-07 07:12:29 -0700357 } else {
358 DCHECK_EQ(kInterpreterImplKind, kSwitchImplKind);
buzbee1452bee2015-03-06 14:43:04 -0800359 if (transaction_active) {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800360 return ExecuteSwitchImpl<false, true>(self, accessor, shadow_frame, result_register,
buzbee1452bee2015-03-06 14:43:04 -0800361 false);
362 } else {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800363 return ExecuteSwitchImpl<false, false>(self, accessor, shadow_frame, result_register,
buzbee1452bee2015-03-06 14:43:04 -0800364 false);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100365 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200366 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200367 } else {
368 // Enter the "with access check" interpreter.
Andreas Gampe81c61bf2018-10-11 18:57:39 -0700369
370 // The boot classpath should really not have to run access checks.
371 DCHECK(method->GetDeclaringClass()->GetClassLoader() != nullptr
372 || Runtime::Current()->IsVerificationSoftFail()
373 || Runtime::Current()->IsAotCompiler())
374 << method->PrettyMethod();
375
buzbee1452bee2015-03-06 14:43:04 -0800376 if (kInterpreterImplKind == kMterpImplKind) {
377 // No access check variants for Mterp. Just use the switch version.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100378 if (transaction_active) {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800379 return ExecuteSwitchImpl<true, true>(self, accessor, shadow_frame, result_register,
buzbee1452bee2015-03-06 14:43:04 -0800380 false);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100381 } else {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800382 return ExecuteSwitchImpl<true, false>(self, accessor, shadow_frame, result_register,
buzbee1452bee2015-03-06 14:43:04 -0800383 false);
384 }
buzbeef61df9b2016-09-07 07:12:29 -0700385 } else {
386 DCHECK_EQ(kInterpreterImplKind, kSwitchImplKind);
buzbee1452bee2015-03-06 14:43:04 -0800387 if (transaction_active) {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800388 return ExecuteSwitchImpl<true, true>(self, accessor, shadow_frame, result_register,
buzbee1452bee2015-03-06 14:43:04 -0800389 false);
390 } else {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800391 return ExecuteSwitchImpl<true, false>(self, accessor, shadow_frame, result_register,
buzbee1452bee2015-03-06 14:43:04 -0800392 false);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100393 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200394 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200395 }
396}
397
Mathieu Chartieref41db72016-10-25 15:08:01 -0700398void EnterInterpreterFromInvoke(Thread* self,
399 ArtMethod* method,
400 ObjPtr<mirror::Object> receiver,
401 uint32_t* args,
402 JValue* result,
Aart Bik01223202016-05-05 15:10:42 -0700403 bool stay_in_interpreter) {
Ian Rogers64b6d142012-10-29 16:34:15 -0700404 DCHECK_EQ(self, Thread::Current());
Nicolas Geoffray535a3fb2014-07-22 15:17:38 +0100405 bool implicit_check = !Runtime::Current()->ExplicitStackOverflowChecks();
406 if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEndForInterpreter(implicit_check))) {
jeffhaod7521322012-11-21 15:38:24 -0800407 ThrowStackOverflowError(self);
408 return;
409 }
410
Alex Lightdb01a092017-04-03 15:39:55 -0700411 // This can happen if we are in forced interpreter mode and an obsolete method is called using
412 // reflection.
413 if (UNLIKELY(method->IsObsolete())) {
414 ThrowInternalError("Attempting to invoke obsolete version of '%s'.",
415 method->PrettyMethod().c_str());
416 return;
417 }
418
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700419 const char* old_cause = self->StartAssertNoThreadSuspension("EnterInterpreterFromInvoke");
David Sehr0225f8e2018-01-31 08:52:24 +0000420 CodeItemDataAccessor accessor(method->DexInstructionData());
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700421 uint16_t num_regs;
422 uint16_t num_ins;
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800423 if (accessor.HasCodeItem()) {
424 num_regs = accessor.RegistersSize();
425 num_ins = accessor.InsSize();
Alex Light9139e002015-10-09 15:59:48 -0700426 } else if (!method->IsInvokable()) {
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700427 self->EndAssertNoThreadSuspension(old_cause);
Alex Light9139e002015-10-09 15:59:48 -0700428 method->ThrowInvocationTimeError();
jeffhao0a9bb732012-11-26 12:28:49 -0800429 return;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700430 } else {
431 DCHECK(method->IsNative());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700432 num_regs = num_ins = ArtMethod::NumArgRegisters(method->GetShorty());
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700433 if (!method->IsStatic()) {
434 num_regs++;
435 num_ins++;
436 }
437 }
438 // Set up shadow frame with matching number of reference slots to vregs.
439 ShadowFrame* last_shadow_frame = self->GetManagedStack()->GetTopShadowFrame();
Andreas Gampeb3025922015-09-01 14:45:00 -0700440 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
Andreas Gampe03ec9302015-08-27 17:41:47 -0700441 CREATE_SHADOW_FRAME(num_regs, last_shadow_frame, method, /* dex pc */ 0);
Andreas Gampeb3025922015-09-01 14:45:00 -0700442 ShadowFrame* shadow_frame = shadow_frame_unique_ptr.get();
Jeff Hao66135192013-05-14 11:02:41 -0700443 self->PushShadowFrame(shadow_frame);
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700444
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700445 size_t cur_reg = num_regs - num_ins;
446 if (!method->IsStatic()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700447 CHECK(receiver != nullptr);
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +0100448 shadow_frame->SetVRegReference(cur_reg, receiver);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700449 ++cur_reg;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700450 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700451 uint32_t shorty_len = 0;
452 const char* shorty = method->GetShorty(&shorty_len);
Jeff Hao5d917302013-02-27 17:57:33 -0800453 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 -0700454 DCHECK_LT(shorty_pos + 1, shorty_len);
Jeff Hao5d917302013-02-27 17:57:33 -0800455 switch (shorty[shorty_pos + 1]) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700456 case 'L': {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700457 ObjPtr<mirror::Object> o =
458 reinterpret_cast<StackReference<mirror::Object>*>(&args[arg_pos])->AsMirrorPtr();
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +0100459 shadow_frame->SetVRegReference(cur_reg, o);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700460 break;
461 }
Jeff Hao5d917302013-02-27 17:57:33 -0800462 case 'J': case 'D': {
463 uint64_t wide_value = (static_cast<uint64_t>(args[arg_pos + 1]) << 32) | args[arg_pos];
464 shadow_frame->SetVRegLong(cur_reg, wide_value);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700465 cur_reg++;
Jeff Hao5d917302013-02-27 17:57:33 -0800466 arg_pos++;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700467 break;
Jeff Hao5d917302013-02-27 17:57:33 -0800468 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700469 default:
Jeff Hao5d917302013-02-27 17:57:33 -0800470 shadow_frame->SetVReg(cur_reg, args[arg_pos]);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700471 break;
472 }
473 }
Mathieu Chartier92246bb2014-02-25 18:22:39 -0800474 self->EndAssertNoThreadSuspension(old_cause);
475 // Do this after populating the shadow frame in case EnsureInitialized causes a GC.
Ian Rogers6c5cb212014-06-18 16:07:20 -0700476 if (method->IsStatic() && UNLIKELY(!method->GetDeclaringClass()->IsInitialized())) {
Mathieu Chartier92246bb2014-02-25 18:22:39 -0800477 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700478 StackHandleScope<1> hs(self);
479 Handle<mirror::Class> h_class(hs.NewHandle(method->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700480 if (UNLIKELY(!class_linker->EnsureInitialized(self, h_class, true, true))) {
Mathieu Chartier92246bb2014-02-25 18:22:39 -0800481 CHECK(self->IsExceptionPending());
482 self->PopShadowFrame();
483 return;
484 }
485 }
Ian Rogers64b6d142012-10-29 16:34:15 -0700486 if (LIKELY(!method->IsNative())) {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800487 JValue r = Execute(self, accessor, *shadow_frame, JValue(), stay_in_interpreter);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700488 if (result != nullptr) {
Jeff Hao6474d192013-03-26 14:08:09 -0700489 *result = r;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700490 }
491 } else {
Ian Rogers64b6d142012-10-29 16:34:15 -0700492 // We don't expect to be asked to interpret native code (which is entered via a JNI compiler
493 // generated stub) except during testing and image writing.
Mathieu Chartier92246bb2014-02-25 18:22:39 -0800494 // Update args to be the args in the shadow frame since the input ones could hold stale
495 // references pointers due to moving GC.
496 args = shadow_frame->GetVRegArgs(method->IsStatic() ? 0 : 1);
Ian Rogers64b6d142012-10-29 16:34:15 -0700497 if (!Runtime::Current()->IsStarted()) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700498 UnstartedRuntime::Jni(self, method, receiver.Ptr(), args, result);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700499 } else {
Jeff Hao6474d192013-03-26 14:08:09 -0700500 InterpreterJni(self, method, shorty, receiver, args, result);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700501 }
502 }
503 self->PopShadowFrame();
504}
505
Mingyao Yangffedec52016-05-19 10:48:40 -0700506static int16_t GetReceiverRegisterForStringInit(const Instruction* instr) {
507 DCHECK(instr->Opcode() == Instruction::INVOKE_DIRECT_RANGE ||
508 instr->Opcode() == Instruction::INVOKE_DIRECT);
509 return (instr->Opcode() == Instruction::INVOKE_DIRECT_RANGE) ?
510 instr->VRegC_3rc() : instr->VRegC_35c();
511}
512
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100513void EnterInterpreterFromDeoptimize(Thread* self,
514 ShadowFrame* shadow_frame,
Mingyao Yang2ee17902017-08-30 11:37:08 -0700515 JValue* ret_val,
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100516 bool from_code,
Mingyao Yang2ee17902017-08-30 11:37:08 -0700517 DeoptimizationMethodType deopt_method_type)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700518 REQUIRES_SHARED(Locks::mutator_lock_) {
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800519 JValue value;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700520 // Set value to last known result in case the shadow frame chain is empty.
521 value.SetJ(ret_val->GetJ());
Alex Light0aa7a5a2018-10-10 15:58:14 +0000522 // How many frames we have executed.
523 size_t frame_cnt = 0;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700524 while (shadow_frame != nullptr) {
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700525 // We do not want to recover lock state for lock counting when deoptimizing. Currently,
526 // the compiler should not have compiled a method that failed structured-locking checks.
527 DCHECK(!shadow_frame->GetMethod()->MustCountLocks());
528
Ian Rogers62d6c772013-02-27 08:32:07 -0800529 self->SetTopOfShadowStack(shadow_frame);
David Sehr0225f8e2018-01-31 08:52:24 +0000530 CodeItemDataAccessor accessor(shadow_frame->GetMethod()->DexInstructionData());
Sebastien Hertz270a0e12015-01-16 19:49:09 +0100531 const uint32_t dex_pc = shadow_frame->GetDexPC();
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100532 uint32_t new_dex_pc = dex_pc;
Sebastien Hertz270a0e12015-01-16 19:49:09 +0100533 if (UNLIKELY(self->IsExceptionPending())) {
Sebastien Hertz520633b2015-09-08 17:03:36 +0200534 // If we deoptimize from the QuickExceptionHandler, we already reported the exception to
535 // the instrumentation. To prevent from reporting it a second time, we simply pass a
536 // null Instrumentation*.
Sebastien Hertz270a0e12015-01-16 19:49:09 +0100537 const instrumentation::Instrumentation* const instrumentation =
Alex Light0aa7a5a2018-10-10 15:58:14 +0000538 frame_cnt == 0 ? nullptr : Runtime::Current()->GetInstrumentation();
Alex Light9fb1ab12017-09-05 09:32:49 -0700539 new_dex_pc = MoveToExceptionHandler(
Andreas Gampee2abbc62017-09-15 11:59:26 -0700540 self, *shadow_frame, instrumentation) ? shadow_frame->GetDexPC() : dex::kDexNoIndex;
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100541 } else if (!from_code) {
Mingyao Yang2ee17902017-08-30 11:37:08 -0700542 // Deoptimization is not called from code directly.
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800543 const Instruction* instr = &accessor.InstructionAt(dex_pc);
Alex Light0aa7a5a2018-10-10 15:58:14 +0000544 if (deopt_method_type == DeoptimizationMethodType::kKeepDexPc ||
545 shadow_frame->GetForceRetryInstruction()) {
546 DCHECK(frame_cnt == 0 || (frame_cnt == 1 && shadow_frame->GetForceRetryInstruction()))
547 << "frame_cnt: " << frame_cnt
548 << " force-retry: " << shadow_frame->GetForceRetryInstruction();
Mingyao Yang2ee17902017-08-30 11:37:08 -0700549 // Need to re-execute the dex instruction.
550 // (1) An invocation might be split into class initialization and invoke.
551 // In this case, the invoke should not be skipped.
552 // (2) A suspend check should also execute the dex instruction at the
553 // corresponding dex pc.
Alex Light0aa7a5a2018-10-10 15:58:14 +0000554 // If the ForceRetryInstruction bit is set this must be the second frame (the first being
555 // the one that is being popped).
Mingyao Yang2ee17902017-08-30 11:37:08 -0700556 DCHECK_EQ(new_dex_pc, dex_pc);
Alex Light0aa7a5a2018-10-10 15:58:14 +0000557 shadow_frame->SetForceRetryInstruction(false);
Mingyao Yang2ee17902017-08-30 11:37:08 -0700558 } else if (instr->Opcode() == Instruction::MONITOR_ENTER ||
559 instr->Opcode() == Instruction::MONITOR_EXIT) {
560 DCHECK(deopt_method_type == DeoptimizationMethodType::kDefault);
Alex Light0aa7a5a2018-10-10 15:58:14 +0000561 DCHECK_EQ(frame_cnt, 0u);
Mingyao Yang2ee17902017-08-30 11:37:08 -0700562 // Non-idempotent dex instruction should not be re-executed.
563 // On the other hand, if a MONITOR_ENTER is at the dex_pc of a suspend
564 // check, that MONITOR_ENTER should be executed. That case is handled
565 // above.
566 new_dex_pc = dex_pc + instr->SizeInCodeUnits();
567 } else if (instr->IsInvoke()) {
568 DCHECK(deopt_method_type == DeoptimizationMethodType::kDefault);
Mingyao Yangffedec52016-05-19 10:48:40 -0700569 if (IsStringInit(instr, shadow_frame->GetMethod())) {
570 uint16_t this_obj_vreg = GetReceiverRegisterForStringInit(instr);
571 // Move the StringFactory.newStringFromChars() result into the register representing
572 // "this object" when invoking the string constructor in the original dex instruction.
573 // Also move the result into all aliases.
574 DCHECK(value.GetL()->IsString());
575 SetStringInitValueToAllAliases(shadow_frame, this_obj_vreg, value);
576 // Calling string constructor in the original dex code doesn't generate a result value.
577 value.SetJ(0);
578 }
Mingyao Yang504a6902016-04-28 16:23:01 -0700579 new_dex_pc = dex_pc + instr->SizeInCodeUnits();
580 } else if (instr->Opcode() == Instruction::NEW_INSTANCE) {
Mingyao Yang2ee17902017-08-30 11:37:08 -0700581 // A NEW_INSTANCE is simply re-executed, including
582 // "new-instance String" which is compiled into a call into
583 // StringFactory.newEmptyString().
584 DCHECK_EQ(new_dex_pc, dex_pc);
Mingyao Yang504a6902016-04-28 16:23:01 -0700585 } else {
Mingyao Yang2ee17902017-08-30 11:37:08 -0700586 DCHECK(deopt_method_type == DeoptimizationMethodType::kDefault);
Alex Light0aa7a5a2018-10-10 15:58:14 +0000587 DCHECK_EQ(frame_cnt, 0u);
Mingyao Yang2ee17902017-08-30 11:37:08 -0700588 // By default, we re-execute the dex instruction since if they are not
589 // an invoke, so that we don't have to decode the dex instruction to move
590 // result into the right vreg. All slow paths have been audited to be
591 // idempotent except monitor-enter/exit and invocation stubs.
592 // TODO: move result and advance dex pc. That also requires that we
593 // can tell the return type of a runtime method, possibly by decoding
594 // the dex instruction at the caller.
595 DCHECK_EQ(new_dex_pc, dex_pc);
Mingyao Yang504a6902016-04-28 16:23:01 -0700596 }
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100597 } else {
598 // Nothing to do, the dex_pc is the one at which the code requested
599 // the deoptimization.
Alex Light0aa7a5a2018-10-10 15:58:14 +0000600 DCHECK_EQ(frame_cnt, 0u);
Mingyao Yang2ee17902017-08-30 11:37:08 -0700601 DCHECK_EQ(new_dex_pc, dex_pc);
Sebastien Hertz270a0e12015-01-16 19:49:09 +0100602 }
Andreas Gampee2abbc62017-09-15 11:59:26 -0700603 if (new_dex_pc != dex::kDexNoIndex) {
Nicolas Geoffray95974242017-09-04 08:45:51 +0000604 shadow_frame->SetDexPC(new_dex_pc);
Vladimir Markofe948752018-03-23 18:11:43 +0000605 value = Execute(self,
606 accessor,
607 *shadow_frame,
608 value,
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700609 /* stay_in_interpreter= */ true,
610 /* from_deoptimize= */ true);
Sebastien Hertz270a0e12015-01-16 19:49:09 +0100611 }
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800612 ShadowFrame* old_frame = shadow_frame;
613 shadow_frame = shadow_frame->GetLink();
Christopher Ferris241a9582015-04-27 15:19:41 -0700614 ShadowFrame::DeleteDeoptimizedFrame(old_frame);
Mingyao Yang2ee17902017-08-30 11:37:08 -0700615 // Following deoptimizations of shadow frames must be at invocation point
616 // and should advance dex pc past the invoke instruction.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100617 from_code = false;
Mingyao Yang2ee17902017-08-30 11:37:08 -0700618 deopt_method_type = DeoptimizationMethodType::kDefault;
Alex Light0aa7a5a2018-10-10 15:58:14 +0000619 frame_cnt++;
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800620 }
621 ret_val->SetJ(value.GetJ());
622}
623
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800624JValue EnterInterpreterFromEntryPoint(Thread* self, const CodeItemDataAccessor& accessor,
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700625 ShadowFrame* shadow_frame) {
Ian Rogersf3e98552013-03-20 15:49:49 -0700626 DCHECK_EQ(self, Thread::Current());
Nicolas Geoffray535a3fb2014-07-22 15:17:38 +0100627 bool implicit_check = !Runtime::Current()->ExplicitStackOverflowChecks();
628 if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEndForInterpreter(implicit_check))) {
Ian Rogersf3e98552013-03-20 15:49:49 -0700629 ThrowStackOverflowError(self);
630 return JValue();
631 }
632
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100633 jit::Jit* jit = Runtime::Current()->GetJit();
634 if (jit != nullptr) {
635 jit->NotifyCompiledCodeToInterpreterTransition(self, shadow_frame->GetMethod());
636 }
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800637 return Execute(self, accessor, *shadow_frame, JValue());
Ian Rogers7db619b2013-01-16 18:35:48 -0800638}
639
Mathieu Chartieref41db72016-10-25 15:08:01 -0700640void ArtInterpreterToInterpreterBridge(Thread* self,
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800641 const CodeItemDataAccessor& accessor,
Mathieu Chartieref41db72016-10-25 15:08:01 -0700642 ShadowFrame* shadow_frame,
643 JValue* result) {
Nicolas Geoffray535a3fb2014-07-22 15:17:38 +0100644 bool implicit_check = !Runtime::Current()->ExplicitStackOverflowChecks();
645 if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEndForInterpreter(implicit_check))) {
Jeff Hao16743632013-05-08 10:59:04 -0700646 ThrowStackOverflowError(self);
Jeff Hao69510672013-05-21 17:34:55 -0700647 return;
Jeff Hao16743632013-05-08 10:59:04 -0700648 }
649
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700650 self->PushShadowFrame(shadow_frame);
Alex Lighteb7c1442015-08-31 13:17:42 -0700651 ArtMethod* method = shadow_frame->GetMethod();
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200652 // Ensure static methods are initialized.
Alex Lighteb7c1442015-08-31 13:17:42 -0700653 const bool is_static = method->IsStatic();
Ian Rogerse94652f2014-12-02 11:13:19 -0800654 if (is_static) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700655 ObjPtr<mirror::Class> declaring_class = method->GetDeclaringClass();
Ian Rogers6c5cb212014-06-18 16:07:20 -0700656 if (UNLIKELY(!declaring_class->IsInitialized())) {
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700657 StackHandleScope<1> hs(self);
Mathieu Chartieref41db72016-10-25 15:08:01 -0700658 HandleWrapperObjPtr<mirror::Class> h_declaring_class(hs.NewHandleWrapper(&declaring_class));
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700659 if (UNLIKELY(!Runtime::Current()->GetClassLinker()->EnsureInitialized(
Ian Rogers7b078e82014-09-10 14:44:24 -0700660 self, h_declaring_class, true, true))) {
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700661 DCHECK(self->IsExceptionPending());
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700662 self->PopShadowFrame();
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200663 return;
664 }
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700665 CHECK(h_declaring_class->IsInitializing());
Jeff Hao16743632013-05-08 10:59:04 -0700666 }
Jeff Hao16743632013-05-08 10:59:04 -0700667 }
Jeff Hao16743632013-05-08 10:59:04 -0700668
Ian Rogerse94652f2014-12-02 11:13:19 -0800669 if (LIKELY(!shadow_frame->GetMethod()->IsNative())) {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800670 result->SetJ(Execute(self, accessor, *shadow_frame, JValue()).GetJ());
Jeff Hao16743632013-05-08 10:59:04 -0700671 } else {
672 // We don't expect to be asked to interpret native code (which is entered via a JNI compiler
673 // generated stub) except during testing and image writing.
674 CHECK(!Runtime::Current()->IsStarted());
Mathieu Chartieref41db72016-10-25 15:08:01 -0700675 ObjPtr<mirror::Object> receiver = is_static ? nullptr : shadow_frame->GetVRegReference(0);
Ian Rogerse94652f2014-12-02 11:13:19 -0800676 uint32_t* args = shadow_frame->GetVRegArgs(is_static ? 0 : 1);
Mathieu Chartieref41db72016-10-25 15:08:01 -0700677 UnstartedRuntime::Jni(self, shadow_frame->GetMethod(), receiver.Ptr(), args, result);
Jeff Hao16743632013-05-08 10:59:04 -0700678 }
679
680 self->PopShadowFrame();
Jeff Hao16743632013-05-08 10:59:04 -0700681}
682
buzbee1452bee2015-03-06 14:43:04 -0800683void CheckInterpreterAsmConstants() {
684 CheckMterpAsmConstants();
685}
686
687void InitInterpreterTls(Thread* self) {
688 InitMterpTls(self);
689}
690
Alex Light0aa7a5a2018-10-10 15:58:14 +0000691bool PrevFrameWillRetry(Thread* self, const ShadowFrame& frame) {
692 ShadowFrame* prev_frame = frame.GetLink();
693 if (prev_frame == nullptr) {
694 NthCallerVisitor vis(self, 1, false);
695 vis.WalkStack();
696 prev_frame = vis.GetCurrentShadowFrame();
697 if (prev_frame == nullptr) {
698 prev_frame = self->FindDebuggerShadowFrame(vis.GetFrameId());
699 }
700 }
701 return prev_frame != nullptr && prev_frame->GetForceRetryInstruction();
702}
703
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700704} // namespace interpreter
705} // namespace art