blob: f23304c391fd1dcba83a6392f44c378aace6b959 [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"
Andreas Gampeb3025922015-09-01 14:45:00 -070034#include "stack.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070035#include "thread-inl.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070036#include "unstarted_runtime.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070037
Ian Rogers2fa6b2e2012-10-17 00:10:17 -070038namespace art {
39namespace interpreter {
40
Mathieu Chartieref41db72016-10-25 15:08:01 -070041ALWAYS_INLINE static ObjPtr<mirror::Object> ObjArg(uint32_t arg)
42 REQUIRES_SHARED(Locks::mutator_lock_) {
43 return ObjPtr<mirror::Object>(reinterpret_cast<mirror::Object*>(arg));
44}
45
46static void InterpreterJni(Thread* self,
47 ArtMethod* method,
48 const StringPiece& shorty,
49 ObjPtr<mirror::Object> receiver,
50 uint32_t* args,
51 JValue* result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070052 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers64b6d142012-10-29 16:34:15 -070053 // TODO: The following enters JNI code using a typedef-ed function rather than the JNI compiler,
54 // it should be removed and JNI compiled stubs used instead.
55 ScopedObjectAccessUnchecked soa(self);
56 if (method->IsStatic()) {
57 if (shorty == "L") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +010058 typedef jobject (fntype)(JNIEnv*, jclass);
Mathieu Chartier2d721012014-11-10 11:08:06 -080059 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -070060 ScopedLocalRef<jclass> klass(soa.Env(),
61 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
Ian Rogers556d6372012-11-20 12:19:36 -080062 jobject jresult;
63 {
64 ScopedThreadStateChange tsc(self, kNative);
65 jresult = fn(soa.Env(), klass.get());
66 }
Mathieu Chartieref41db72016-10-25 15:08:01 -070067 result->SetL(soa.Decode<mirror::Object>(jresult));
Ian Rogers64b6d142012-10-29 16:34:15 -070068 } else if (shorty == "V") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +010069 typedef void (fntype)(JNIEnv*, jclass);
Mathieu Chartier2d721012014-11-10 11:08:06 -080070 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -070071 ScopedLocalRef<jclass> klass(soa.Env(),
72 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
73 ScopedThreadStateChange tsc(self, kNative);
74 fn(soa.Env(), klass.get());
75 } else if (shorty == "Z") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +010076 typedef jboolean (fntype)(JNIEnv*, jclass);
Mathieu Chartier2d721012014-11-10 11:08:06 -080077 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -070078 ScopedLocalRef<jclass> klass(soa.Env(),
79 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
80 ScopedThreadStateChange tsc(self, kNative);
81 result->SetZ(fn(soa.Env(), klass.get()));
82 } else if (shorty == "BI") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +010083 typedef jbyte (fntype)(JNIEnv*, jclass, jint);
Mathieu Chartier2d721012014-11-10 11:08:06 -080084 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -070085 ScopedLocalRef<jclass> klass(soa.Env(),
86 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
87 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -080088 result->SetB(fn(soa.Env(), klass.get(), args[0]));
Ian Rogers64b6d142012-10-29 16:34:15 -070089 } else if (shorty == "II") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +010090 typedef jint (fntype)(JNIEnv*, jclass, jint);
Mathieu Chartier2d721012014-11-10 11:08:06 -080091 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -070092 ScopedLocalRef<jclass> klass(soa.Env(),
93 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
94 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -080095 result->SetI(fn(soa.Env(), klass.get(), args[0]));
Ian Rogers64b6d142012-10-29 16:34:15 -070096 } else if (shorty == "LL") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +010097 typedef jobject (fntype)(JNIEnv*, jclass, jobject);
Mathieu Chartier2d721012014-11-10 11:08:06 -080098 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -070099 ScopedLocalRef<jclass> klass(soa.Env(),
100 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
101 ScopedLocalRef<jobject> arg0(soa.Env(),
Mathieu Chartieref41db72016-10-25 15:08:01 -0700102 soa.AddLocalReference<jobject>(ObjArg(args[0])));
Ian Rogers556d6372012-11-20 12:19:36 -0800103 jobject jresult;
104 {
105 ScopedThreadStateChange tsc(self, kNative);
106 jresult = fn(soa.Env(), klass.get(), arg0.get());
107 }
Mathieu Chartieref41db72016-10-25 15:08:01 -0700108 result->SetL(soa.Decode<mirror::Object>(jresult));
Ian Rogers64b6d142012-10-29 16:34:15 -0700109 } else if (shorty == "IIZ") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100110 typedef jint (fntype)(JNIEnv*, jclass, jint, jboolean);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800111 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700112 ScopedLocalRef<jclass> klass(soa.Env(),
113 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
114 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800115 result->SetI(fn(soa.Env(), klass.get(), args[0], args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700116 } else if (shorty == "ILI") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100117 typedef jint (fntype)(JNIEnv*, jclass, jobject, jint);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800118 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(
119 method->GetEntryPointFromJni()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700120 ScopedLocalRef<jclass> klass(soa.Env(),
121 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
122 ScopedLocalRef<jobject> arg0(soa.Env(),
Mathieu Chartieref41db72016-10-25 15:08:01 -0700123 soa.AddLocalReference<jobject>(ObjArg(args[0])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700124 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800125 result->SetI(fn(soa.Env(), klass.get(), arg0.get(), args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700126 } else if (shorty == "SIZ") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100127 typedef jshort (fntype)(JNIEnv*, jclass, jint, jboolean);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700128 fntype* const fn =
129 reinterpret_cast<fntype*>(const_cast<void*>(method->GetEntryPointFromJni()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700130 ScopedLocalRef<jclass> klass(soa.Env(),
131 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
132 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800133 result->SetS(fn(soa.Env(), klass.get(), args[0], args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700134 } else if (shorty == "VIZ") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100135 typedef void (fntype)(JNIEnv*, jclass, jint, jboolean);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800136 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700137 ScopedLocalRef<jclass> klass(soa.Env(),
138 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
139 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800140 fn(soa.Env(), klass.get(), args[0], args[1]);
Ian Rogers64b6d142012-10-29 16:34:15 -0700141 } else if (shorty == "ZLL") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100142 typedef jboolean (fntype)(JNIEnv*, jclass, jobject, jobject);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800143 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700144 ScopedLocalRef<jclass> klass(soa.Env(),
145 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
146 ScopedLocalRef<jobject> arg0(soa.Env(),
Mathieu Chartieref41db72016-10-25 15:08:01 -0700147 soa.AddLocalReference<jobject>(ObjArg(args[0])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700148 ScopedLocalRef<jobject> arg1(soa.Env(),
Mathieu Chartieref41db72016-10-25 15:08:01 -0700149 soa.AddLocalReference<jobject>(ObjArg(args[1])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700150 ScopedThreadStateChange tsc(self, kNative);
151 result->SetZ(fn(soa.Env(), klass.get(), arg0.get(), arg1.get()));
152 } else if (shorty == "ZILL") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100153 typedef jboolean (fntype)(JNIEnv*, jclass, jint, jobject, jobject);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800154 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700155 ScopedLocalRef<jclass> klass(soa.Env(),
156 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
157 ScopedLocalRef<jobject> arg1(soa.Env(),
Mathieu Chartieref41db72016-10-25 15:08:01 -0700158 soa.AddLocalReference<jobject>(ObjArg(args[1])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700159 ScopedLocalRef<jobject> arg2(soa.Env(),
Mathieu Chartieref41db72016-10-25 15:08:01 -0700160 soa.AddLocalReference<jobject>(ObjArg(args[2])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700161 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800162 result->SetZ(fn(soa.Env(), klass.get(), args[0], arg1.get(), arg2.get()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700163 } else if (shorty == "VILII") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100164 typedef void (fntype)(JNIEnv*, jclass, jint, jobject, jint, jint);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800165 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700166 ScopedLocalRef<jclass> klass(soa.Env(),
167 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
168 ScopedLocalRef<jobject> arg1(soa.Env(),
Mathieu Chartieref41db72016-10-25 15:08:01 -0700169 soa.AddLocalReference<jobject>(ObjArg(args[1])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700170 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800171 fn(soa.Env(), klass.get(), args[0], arg1.get(), args[2], args[3]);
Ian Rogers64b6d142012-10-29 16:34:15 -0700172 } else if (shorty == "VLILII") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100173 typedef void (fntype)(JNIEnv*, jclass, jobject, jint, jobject, jint, jint);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800174 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700175 ScopedLocalRef<jclass> klass(soa.Env(),
176 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
177 ScopedLocalRef<jobject> arg0(soa.Env(),
Mathieu Chartieref41db72016-10-25 15:08:01 -0700178 soa.AddLocalReference<jobject>(ObjArg(args[0])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700179 ScopedLocalRef<jobject> arg2(soa.Env(),
Mathieu Chartieref41db72016-10-25 15:08:01 -0700180 soa.AddLocalReference<jobject>(ObjArg(args[2])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700181 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800182 fn(soa.Env(), klass.get(), arg0.get(), args[1], arg2.get(), args[3], args[4]);
Ian Rogers64b6d142012-10-29 16:34:15 -0700183 } else {
David Sehr709b0702016-10-13 09:12:37 -0700184 LOG(FATAL) << "Do something with static native method: " << method->PrettyMethod()
Ian Rogers64b6d142012-10-29 16:34:15 -0700185 << " shorty: " << shorty;
186 }
187 } else {
188 if (shorty == "L") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100189 typedef jobject (fntype)(JNIEnv*, jobject);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800190 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700191 ScopedLocalRef<jobject> rcvr(soa.Env(),
192 soa.AddLocalReference<jobject>(receiver));
Ian Rogers556d6372012-11-20 12:19:36 -0800193 jobject jresult;
194 {
195 ScopedThreadStateChange tsc(self, kNative);
196 jresult = fn(soa.Env(), rcvr.get());
197 }
Mathieu Chartieref41db72016-10-25 15:08:01 -0700198 result->SetL(soa.Decode<mirror::Object>(jresult));
Jeff Hao3dd9f762013-07-08 13:09:25 -0700199 } else if (shorty == "V") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100200 typedef void (fntype)(JNIEnv*, jobject);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800201 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Jeff Hao3dd9f762013-07-08 13:09:25 -0700202 ScopedLocalRef<jobject> rcvr(soa.Env(),
203 soa.AddLocalReference<jobject>(receiver));
204 ScopedThreadStateChange tsc(self, kNative);
205 fn(soa.Env(), rcvr.get());
Ian Rogers64b6d142012-10-29 16:34:15 -0700206 } else if (shorty == "LL") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100207 typedef jobject (fntype)(JNIEnv*, jobject, jobject);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800208 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700209 ScopedLocalRef<jobject> rcvr(soa.Env(),
210 soa.AddLocalReference<jobject>(receiver));
211 ScopedLocalRef<jobject> arg0(soa.Env(),
Mathieu Chartieref41db72016-10-25 15:08:01 -0700212 soa.AddLocalReference<jobject>(ObjArg(args[0])));
Ian Rogers556d6372012-11-20 12:19:36 -0800213 jobject jresult;
214 {
215 ScopedThreadStateChange tsc(self, kNative);
216 jresult = fn(soa.Env(), rcvr.get(), arg0.get());
Ian Rogers556d6372012-11-20 12:19:36 -0800217 }
Mathieu Chartieref41db72016-10-25 15:08:01 -0700218 result->SetL(soa.Decode<mirror::Object>(jresult));
Ian Rogers64b6d142012-10-29 16:34:15 -0700219 ScopedThreadStateChange tsc(self, kNative);
Ian Rogers64b6d142012-10-29 16:34:15 -0700220 } else if (shorty == "III") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100221 typedef jint (fntype)(JNIEnv*, jobject, jint, jint);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800222 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700223 ScopedLocalRef<jobject> rcvr(soa.Env(),
224 soa.AddLocalReference<jobject>(receiver));
225 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800226 result->SetI(fn(soa.Env(), rcvr.get(), args[0], args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700227 } else {
David Sehr709b0702016-10-13 09:12:37 -0700228 LOG(FATAL) << "Do something with native method: " << method->PrettyMethod()
Ian Rogers64b6d142012-10-29 16:34:15 -0700229 << " shorty: " << shorty;
230 }
231 }
232}
233
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200234enum InterpreterImplKind {
buzbee1452bee2015-03-06 14:43:04 -0800235 kSwitchImplKind, // Switch-based interpreter implementation.
buzbee1452bee2015-03-06 14:43:04 -0800236 kMterpImplKind // Assembly interpreter
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200237};
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700238
buzbee1452bee2015-03-06 14:43:04 -0800239static constexpr InterpreterImplKind kInterpreterImplKind = kMterpImplKind;
Alexey Frunze00b53b72016-02-02 20:25:45 -0800240
Aart Bik01223202016-05-05 15:10:42 -0700241static inline JValue Execute(
242 Thread* self,
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800243 const CodeItemDataAccessor& accessor,
Aart Bik01223202016-05-05 15:10:42 -0700244 ShadowFrame& shadow_frame,
245 JValue result_register,
Vladimir Markofe948752018-03-23 18:11:43 +0000246 bool stay_in_interpreter = false,
247 bool from_deoptimize = false) REQUIRES_SHARED(Locks::mutator_lock_) {
buzbee1452bee2015-03-06 14:43:04 -0800248 DCHECK(!shadow_frame.GetMethod()->IsAbstract());
Ian Rogers848871b2013-08-05 10:56:33 -0700249 DCHECK(!shadow_frame.GetMethod()->IsNative());
Vladimir Markofe948752018-03-23 18:11:43 +0000250 if (LIKELY(!from_deoptimize)) { // Entering the method, but not via deoptimization.
buzbee734f3aa2016-01-28 14:20:06 -0800251 if (kIsDebugBuild) {
Vladimir Markofe948752018-03-23 18:11:43 +0000252 CHECK_EQ(shadow_frame.GetDexPC(), 0u);
buzbee734f3aa2016-01-28 14:20:06 -0800253 self->AssertNoPendingException();
254 }
255 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
256 ArtMethod *method = shadow_frame.GetMethod();
257
258 if (UNLIKELY(instrumentation->HasMethodEntryListeners())) {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800259 instrumentation->MethodEnterEvent(self,
260 shadow_frame.GetThisObject(accessor.InsSize()),
261 method,
262 0);
Alex Lightb7edcda2017-04-27 13:20:31 -0700263 if (UNLIKELY(self->IsExceptionPending())) {
264 instrumentation->MethodUnwindEvent(self,
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800265 shadow_frame.GetThisObject(accessor.InsSize()),
Alex Lightb7edcda2017-04-27 13:20:31 -0700266 method,
267 0);
268 return JValue();
269 }
buzbee734f3aa2016-01-28 14:20:06 -0800270 }
271
Aart Bik01223202016-05-05 15:10:42 -0700272 if (!stay_in_interpreter) {
273 jit::Jit* jit = Runtime::Current()->GetJit();
274 if (jit != nullptr) {
275 jit->MethodEntered(self, shadow_frame.GetMethod());
276 if (jit->CanInvokeCompiledCode(method)) {
277 JValue result;
buzbee734f3aa2016-01-28 14:20:06 -0800278
Aart Bik01223202016-05-05 15:10:42 -0700279 // Pop the shadow frame before calling into compiled code.
280 self->PopShadowFrame();
Jeff Hao5ea84132017-05-05 16:59:29 -0700281 // Calculate the offset of the first input reg. The input registers are in the high regs.
282 // It's ok to access the code item here since JIT code will have been touched by the
283 // interpreter and compiler already.
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800284 uint16_t arg_offset = accessor.RegistersSize() - accessor.InsSize();
Jeff Hao5ea84132017-05-05 16:59:29 -0700285 ArtInterpreterToCompiledCodeBridge(self, nullptr, &shadow_frame, arg_offset, &result);
Aart Bik01223202016-05-05 15:10:42 -0700286 // Push the shadow frame back as the caller will expect it.
287 self->PushShadowFrame(&shadow_frame);
buzbee734f3aa2016-01-28 14:20:06 -0800288
Aart Bik01223202016-05-05 15:10:42 -0700289 return result;
290 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100291 }
buzbee734f3aa2016-01-28 14:20:06 -0800292 }
293 }
294
Andreas Gampe580667b2017-10-23 11:20:39 -0700295 ArtMethod* method = shadow_frame.GetMethod();
296
297 DCheckStaticState(self, method);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200298
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700299 // Lock counting is a special version of accessibility checks, and for simplicity and
300 // reduction of template parameters, we gate it behind access-checks mode.
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700301 DCHECK(!method->SkipAccessChecks() || !method->MustCountLocks());
302
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100303 bool transaction_active = Runtime::Current()->IsActiveTransaction();
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700304 if (LIKELY(method->SkipAccessChecks())) {
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200305 // Enter the "without access check" interpreter.
buzbee1452bee2015-03-06 14:43:04 -0800306 if (kInterpreterImplKind == kMterpImplKind) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100307 if (transaction_active) {
buzbee1452bee2015-03-06 14:43:04 -0800308 // No Mterp variant - just use the switch interpreter.
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800309 return ExecuteSwitchImpl<false, true>(self, accessor, shadow_frame, result_register,
buzbee1452bee2015-03-06 14:43:04 -0800310 false);
Bill Buzbeefd522f92016-02-11 22:37:42 +0000311 } else if (UNLIKELY(!Runtime::Current()->IsStarted())) {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800312 return ExecuteSwitchImpl<false, false>(self, accessor, shadow_frame, result_register,
Bill Buzbeefd522f92016-02-11 22:37:42 +0000313 false);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100314 } else {
buzbee1452bee2015-03-06 14:43:04 -0800315 while (true) {
Bill Buzbeefd522f92016-02-11 22:37:42 +0000316 // Mterp does not support all instrumentation/debugging.
Andreas Gampe67409972016-07-19 22:34:53 -0700317 if (MterpShouldSwitchInterpreters() != 0) {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800318 return ExecuteSwitchImpl<false, false>(self, accessor, shadow_frame, result_register,
buzbee1452bee2015-03-06 14:43:04 -0800319 false);
buzbee1452bee2015-03-06 14:43:04 -0800320 }
Mathieu Chartierfc9555d2017-11-05 16:32:19 -0800321 bool returned = ExecuteMterpImpl(self,
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800322 accessor.Insns(),
Mathieu Chartierfc9555d2017-11-05 16:32:19 -0800323 &shadow_frame,
324 &result_register);
buzbee1452bee2015-03-06 14:43:04 -0800325 if (returned) {
326 return result_register;
327 } else {
328 // Mterp didn't like that instruction. Single-step it with the reference interpreter.
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800329 result_register = ExecuteSwitchImpl<false, false>(self, accessor, shadow_frame,
Mathieu Chartieref41db72016-10-25 15:08:01 -0700330 result_register, true);
Andreas Gampee2abbc62017-09-15 11:59:26 -0700331 if (shadow_frame.GetDexPC() == dex::kDexNoIndex) {
buzbee1452bee2015-03-06 14:43:04 -0800332 // Single-stepped a return or an exception not handled locally. Return to caller.
buzbeed6b48db2016-01-28 15:48:55 -0800333 return result_register;
buzbee1452bee2015-03-06 14:43:04 -0800334 }
335 }
336 }
337 }
buzbeef61df9b2016-09-07 07:12:29 -0700338 } else {
339 DCHECK_EQ(kInterpreterImplKind, kSwitchImplKind);
buzbee1452bee2015-03-06 14:43:04 -0800340 if (transaction_active) {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800341 return ExecuteSwitchImpl<false, true>(self, accessor, shadow_frame, result_register,
buzbee1452bee2015-03-06 14:43:04 -0800342 false);
343 } else {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800344 return ExecuteSwitchImpl<false, false>(self, accessor, shadow_frame, result_register,
buzbee1452bee2015-03-06 14:43:04 -0800345 false);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100346 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200347 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200348 } else {
349 // Enter the "with access check" interpreter.
buzbee1452bee2015-03-06 14:43:04 -0800350 if (kInterpreterImplKind == kMterpImplKind) {
351 // No access check variants for Mterp. Just use the switch version.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100352 if (transaction_active) {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800353 return ExecuteSwitchImpl<true, true>(self, accessor, shadow_frame, result_register,
buzbee1452bee2015-03-06 14:43:04 -0800354 false);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100355 } else {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800356 return ExecuteSwitchImpl<true, false>(self, accessor, shadow_frame, result_register,
buzbee1452bee2015-03-06 14:43:04 -0800357 false);
358 }
buzbeef61df9b2016-09-07 07:12:29 -0700359 } else {
360 DCHECK_EQ(kInterpreterImplKind, kSwitchImplKind);
buzbee1452bee2015-03-06 14:43:04 -0800361 if (transaction_active) {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800362 return ExecuteSwitchImpl<true, true>(self, accessor, shadow_frame, result_register,
buzbee1452bee2015-03-06 14:43:04 -0800363 false);
364 } else {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800365 return ExecuteSwitchImpl<true, false>(self, accessor, shadow_frame, result_register,
buzbee1452bee2015-03-06 14:43:04 -0800366 false);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100367 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200368 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200369 }
370}
371
Mathieu Chartieref41db72016-10-25 15:08:01 -0700372void EnterInterpreterFromInvoke(Thread* self,
373 ArtMethod* method,
374 ObjPtr<mirror::Object> receiver,
375 uint32_t* args,
376 JValue* result,
Aart Bik01223202016-05-05 15:10:42 -0700377 bool stay_in_interpreter) {
Ian Rogers64b6d142012-10-29 16:34:15 -0700378 DCHECK_EQ(self, Thread::Current());
Nicolas Geoffray535a3fb2014-07-22 15:17:38 +0100379 bool implicit_check = !Runtime::Current()->ExplicitStackOverflowChecks();
380 if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEndForInterpreter(implicit_check))) {
jeffhaod7521322012-11-21 15:38:24 -0800381 ThrowStackOverflowError(self);
382 return;
383 }
384
Alex Lightdb01a092017-04-03 15:39:55 -0700385 // This can happen if we are in forced interpreter mode and an obsolete method is called using
386 // reflection.
387 if (UNLIKELY(method->IsObsolete())) {
388 ThrowInternalError("Attempting to invoke obsolete version of '%s'.",
389 method->PrettyMethod().c_str());
390 return;
391 }
392
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700393 const char* old_cause = self->StartAssertNoThreadSuspension("EnterInterpreterFromInvoke");
David Sehr0225f8e2018-01-31 08:52:24 +0000394 CodeItemDataAccessor accessor(method->DexInstructionData());
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700395 uint16_t num_regs;
396 uint16_t num_ins;
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800397 if (accessor.HasCodeItem()) {
398 num_regs = accessor.RegistersSize();
399 num_ins = accessor.InsSize();
Alex Light9139e002015-10-09 15:59:48 -0700400 } else if (!method->IsInvokable()) {
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700401 self->EndAssertNoThreadSuspension(old_cause);
Alex Light9139e002015-10-09 15:59:48 -0700402 method->ThrowInvocationTimeError();
jeffhao0a9bb732012-11-26 12:28:49 -0800403 return;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700404 } else {
405 DCHECK(method->IsNative());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700406 num_regs = num_ins = ArtMethod::NumArgRegisters(method->GetShorty());
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700407 if (!method->IsStatic()) {
408 num_regs++;
409 num_ins++;
410 }
411 }
412 // Set up shadow frame with matching number of reference slots to vregs.
413 ShadowFrame* last_shadow_frame = self->GetManagedStack()->GetTopShadowFrame();
Andreas Gampeb3025922015-09-01 14:45:00 -0700414 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
Andreas Gampe03ec9302015-08-27 17:41:47 -0700415 CREATE_SHADOW_FRAME(num_regs, last_shadow_frame, method, /* dex pc */ 0);
Andreas Gampeb3025922015-09-01 14:45:00 -0700416 ShadowFrame* shadow_frame = shadow_frame_unique_ptr.get();
Jeff Hao66135192013-05-14 11:02:41 -0700417 self->PushShadowFrame(shadow_frame);
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700418
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700419 size_t cur_reg = num_regs - num_ins;
420 if (!method->IsStatic()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700421 CHECK(receiver != nullptr);
Mathieu Chartieref41db72016-10-25 15:08:01 -0700422 shadow_frame->SetVRegReference(cur_reg, receiver.Ptr());
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700423 ++cur_reg;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700424 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700425 uint32_t shorty_len = 0;
426 const char* shorty = method->GetShorty(&shorty_len);
Jeff Hao5d917302013-02-27 17:57:33 -0800427 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 -0700428 DCHECK_LT(shorty_pos + 1, shorty_len);
Jeff Hao5d917302013-02-27 17:57:33 -0800429 switch (shorty[shorty_pos + 1]) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700430 case 'L': {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700431 ObjPtr<mirror::Object> o =
432 reinterpret_cast<StackReference<mirror::Object>*>(&args[arg_pos])->AsMirrorPtr();
433 shadow_frame->SetVRegReference(cur_reg, o.Ptr());
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700434 break;
435 }
Jeff Hao5d917302013-02-27 17:57:33 -0800436 case 'J': case 'D': {
437 uint64_t wide_value = (static_cast<uint64_t>(args[arg_pos + 1]) << 32) | args[arg_pos];
438 shadow_frame->SetVRegLong(cur_reg, wide_value);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700439 cur_reg++;
Jeff Hao5d917302013-02-27 17:57:33 -0800440 arg_pos++;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700441 break;
Jeff Hao5d917302013-02-27 17:57:33 -0800442 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700443 default:
Jeff Hao5d917302013-02-27 17:57:33 -0800444 shadow_frame->SetVReg(cur_reg, args[arg_pos]);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700445 break;
446 }
447 }
Mathieu Chartier92246bb2014-02-25 18:22:39 -0800448 self->EndAssertNoThreadSuspension(old_cause);
449 // Do this after populating the shadow frame in case EnsureInitialized causes a GC.
Ian Rogers6c5cb212014-06-18 16:07:20 -0700450 if (method->IsStatic() && UNLIKELY(!method->GetDeclaringClass()->IsInitialized())) {
Mathieu Chartier92246bb2014-02-25 18:22:39 -0800451 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700452 StackHandleScope<1> hs(self);
453 Handle<mirror::Class> h_class(hs.NewHandle(method->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700454 if (UNLIKELY(!class_linker->EnsureInitialized(self, h_class, true, true))) {
Mathieu Chartier92246bb2014-02-25 18:22:39 -0800455 CHECK(self->IsExceptionPending());
456 self->PopShadowFrame();
457 return;
458 }
459 }
Ian Rogers64b6d142012-10-29 16:34:15 -0700460 if (LIKELY(!method->IsNative())) {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800461 JValue r = Execute(self, accessor, *shadow_frame, JValue(), stay_in_interpreter);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700462 if (result != nullptr) {
Jeff Hao6474d192013-03-26 14:08:09 -0700463 *result = r;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700464 }
465 } else {
Ian Rogers64b6d142012-10-29 16:34:15 -0700466 // We don't expect to be asked to interpret native code (which is entered via a JNI compiler
467 // generated stub) except during testing and image writing.
Mathieu Chartier92246bb2014-02-25 18:22:39 -0800468 // Update args to be the args in the shadow frame since the input ones could hold stale
469 // references pointers due to moving GC.
470 args = shadow_frame->GetVRegArgs(method->IsStatic() ? 0 : 1);
Ian Rogers64b6d142012-10-29 16:34:15 -0700471 if (!Runtime::Current()->IsStarted()) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700472 UnstartedRuntime::Jni(self, method, receiver.Ptr(), args, result);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700473 } else {
Jeff Hao6474d192013-03-26 14:08:09 -0700474 InterpreterJni(self, method, shorty, receiver, args, result);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700475 }
476 }
477 self->PopShadowFrame();
478}
479
Mingyao Yangffedec52016-05-19 10:48:40 -0700480static int16_t GetReceiverRegisterForStringInit(const Instruction* instr) {
481 DCHECK(instr->Opcode() == Instruction::INVOKE_DIRECT_RANGE ||
482 instr->Opcode() == Instruction::INVOKE_DIRECT);
483 return (instr->Opcode() == Instruction::INVOKE_DIRECT_RANGE) ?
484 instr->VRegC_3rc() : instr->VRegC_35c();
485}
486
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100487void EnterInterpreterFromDeoptimize(Thread* self,
488 ShadowFrame* shadow_frame,
Mingyao Yang2ee17902017-08-30 11:37:08 -0700489 JValue* ret_val,
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100490 bool from_code,
Mingyao Yang2ee17902017-08-30 11:37:08 -0700491 DeoptimizationMethodType deopt_method_type)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700492 REQUIRES_SHARED(Locks::mutator_lock_) {
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800493 JValue value;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700494 // Set value to last known result in case the shadow frame chain is empty.
495 value.SetJ(ret_val->GetJ());
Sebastien Hertz520633b2015-09-08 17:03:36 +0200496 // Are we executing the first shadow frame?
497 bool first = true;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700498 while (shadow_frame != nullptr) {
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700499 // We do not want to recover lock state for lock counting when deoptimizing. Currently,
500 // the compiler should not have compiled a method that failed structured-locking checks.
501 DCHECK(!shadow_frame->GetMethod()->MustCountLocks());
502
Ian Rogers62d6c772013-02-27 08:32:07 -0800503 self->SetTopOfShadowStack(shadow_frame);
David Sehr0225f8e2018-01-31 08:52:24 +0000504 CodeItemDataAccessor accessor(shadow_frame->GetMethod()->DexInstructionData());
Sebastien Hertz270a0e12015-01-16 19:49:09 +0100505 const uint32_t dex_pc = shadow_frame->GetDexPC();
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100506 uint32_t new_dex_pc = dex_pc;
Sebastien Hertz270a0e12015-01-16 19:49:09 +0100507 if (UNLIKELY(self->IsExceptionPending())) {
Sebastien Hertz520633b2015-09-08 17:03:36 +0200508 // If we deoptimize from the QuickExceptionHandler, we already reported the exception to
509 // the instrumentation. To prevent from reporting it a second time, we simply pass a
510 // null Instrumentation*.
Sebastien Hertz270a0e12015-01-16 19:49:09 +0100511 const instrumentation::Instrumentation* const instrumentation =
Sebastien Hertz520633b2015-09-08 17:03:36 +0200512 first ? nullptr : Runtime::Current()->GetInstrumentation();
Alex Light9fb1ab12017-09-05 09:32:49 -0700513 new_dex_pc = MoveToExceptionHandler(
Andreas Gampee2abbc62017-09-15 11:59:26 -0700514 self, *shadow_frame, instrumentation) ? shadow_frame->GetDexPC() : dex::kDexNoIndex;
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100515 } else if (!from_code) {
Mingyao Yang2ee17902017-08-30 11:37:08 -0700516 // Deoptimization is not called from code directly.
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800517 const Instruction* instr = &accessor.InstructionAt(dex_pc);
Mingyao Yang2ee17902017-08-30 11:37:08 -0700518 if (deopt_method_type == DeoptimizationMethodType::kKeepDexPc) {
519 DCHECK(first);
520 // Need to re-execute the dex instruction.
521 // (1) An invocation might be split into class initialization and invoke.
522 // In this case, the invoke should not be skipped.
523 // (2) A suspend check should also execute the dex instruction at the
524 // corresponding dex pc.
525 DCHECK_EQ(new_dex_pc, dex_pc);
526 } else if (instr->Opcode() == Instruction::MONITOR_ENTER ||
527 instr->Opcode() == Instruction::MONITOR_EXIT) {
528 DCHECK(deopt_method_type == DeoptimizationMethodType::kDefault);
529 DCHECK(first);
530 // Non-idempotent dex instruction should not be re-executed.
531 // On the other hand, if a MONITOR_ENTER is at the dex_pc of a suspend
532 // check, that MONITOR_ENTER should be executed. That case is handled
533 // above.
534 new_dex_pc = dex_pc + instr->SizeInCodeUnits();
535 } else if (instr->IsInvoke()) {
536 DCHECK(deopt_method_type == DeoptimizationMethodType::kDefault);
Mingyao Yangffedec52016-05-19 10:48:40 -0700537 if (IsStringInit(instr, shadow_frame->GetMethod())) {
538 uint16_t this_obj_vreg = GetReceiverRegisterForStringInit(instr);
539 // Move the StringFactory.newStringFromChars() result into the register representing
540 // "this object" when invoking the string constructor in the original dex instruction.
541 // Also move the result into all aliases.
542 DCHECK(value.GetL()->IsString());
543 SetStringInitValueToAllAliases(shadow_frame, this_obj_vreg, value);
544 // Calling string constructor in the original dex code doesn't generate a result value.
545 value.SetJ(0);
546 }
Mingyao Yang504a6902016-04-28 16:23:01 -0700547 new_dex_pc = dex_pc + instr->SizeInCodeUnits();
548 } else if (instr->Opcode() == Instruction::NEW_INSTANCE) {
Mingyao Yang2ee17902017-08-30 11:37:08 -0700549 // A NEW_INSTANCE is simply re-executed, including
550 // "new-instance String" which is compiled into a call into
551 // StringFactory.newEmptyString().
552 DCHECK_EQ(new_dex_pc, dex_pc);
Mingyao Yang504a6902016-04-28 16:23:01 -0700553 } else {
Mingyao Yang2ee17902017-08-30 11:37:08 -0700554 DCHECK(deopt_method_type == DeoptimizationMethodType::kDefault);
555 DCHECK(first);
556 // By default, we re-execute the dex instruction since if they are not
557 // an invoke, so that we don't have to decode the dex instruction to move
558 // result into the right vreg. All slow paths have been audited to be
559 // idempotent except monitor-enter/exit and invocation stubs.
560 // TODO: move result and advance dex pc. That also requires that we
561 // can tell the return type of a runtime method, possibly by decoding
562 // the dex instruction at the caller.
563 DCHECK_EQ(new_dex_pc, dex_pc);
Mingyao Yang504a6902016-04-28 16:23:01 -0700564 }
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100565 } else {
566 // Nothing to do, the dex_pc is the one at which the code requested
567 // the deoptimization.
Mingyao Yang2ee17902017-08-30 11:37:08 -0700568 DCHECK(first);
569 DCHECK_EQ(new_dex_pc, dex_pc);
Sebastien Hertz270a0e12015-01-16 19:49:09 +0100570 }
Andreas Gampee2abbc62017-09-15 11:59:26 -0700571 if (new_dex_pc != dex::kDexNoIndex) {
Nicolas Geoffray95974242017-09-04 08:45:51 +0000572 shadow_frame->SetDexPC(new_dex_pc);
Vladimir Markofe948752018-03-23 18:11:43 +0000573 value = Execute(self,
574 accessor,
575 *shadow_frame,
576 value,
577 /* stay_in_interpreter */ true,
578 /* from_deoptimize */ true);
Sebastien Hertz270a0e12015-01-16 19:49:09 +0100579 }
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800580 ShadowFrame* old_frame = shadow_frame;
581 shadow_frame = shadow_frame->GetLink();
Christopher Ferris241a9582015-04-27 15:19:41 -0700582 ShadowFrame::DeleteDeoptimizedFrame(old_frame);
Mingyao Yang2ee17902017-08-30 11:37:08 -0700583 // Following deoptimizations of shadow frames must be at invocation point
584 // and should advance dex pc past the invoke instruction.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100585 from_code = false;
Mingyao Yang2ee17902017-08-30 11:37:08 -0700586 deopt_method_type = DeoptimizationMethodType::kDefault;
Sebastien Hertz520633b2015-09-08 17:03:36 +0200587 first = false;
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800588 }
589 ret_val->SetJ(value.GetJ());
590}
591
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800592JValue EnterInterpreterFromEntryPoint(Thread* self, const CodeItemDataAccessor& accessor,
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700593 ShadowFrame* shadow_frame) {
Ian Rogersf3e98552013-03-20 15:49:49 -0700594 DCHECK_EQ(self, Thread::Current());
Nicolas Geoffray535a3fb2014-07-22 15:17:38 +0100595 bool implicit_check = !Runtime::Current()->ExplicitStackOverflowChecks();
596 if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEndForInterpreter(implicit_check))) {
Ian Rogersf3e98552013-03-20 15:49:49 -0700597 ThrowStackOverflowError(self);
598 return JValue();
599 }
600
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100601 jit::Jit* jit = Runtime::Current()->GetJit();
602 if (jit != nullptr) {
603 jit->NotifyCompiledCodeToInterpreterTransition(self, shadow_frame->GetMethod());
604 }
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800605 return Execute(self, accessor, *shadow_frame, JValue());
Ian Rogers7db619b2013-01-16 18:35:48 -0800606}
607
Mathieu Chartieref41db72016-10-25 15:08:01 -0700608void ArtInterpreterToInterpreterBridge(Thread* self,
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800609 const CodeItemDataAccessor& accessor,
Mathieu Chartieref41db72016-10-25 15:08:01 -0700610 ShadowFrame* shadow_frame,
611 JValue* result) {
Nicolas Geoffray535a3fb2014-07-22 15:17:38 +0100612 bool implicit_check = !Runtime::Current()->ExplicitStackOverflowChecks();
613 if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEndForInterpreter(implicit_check))) {
Jeff Hao16743632013-05-08 10:59:04 -0700614 ThrowStackOverflowError(self);
Jeff Hao69510672013-05-21 17:34:55 -0700615 return;
Jeff Hao16743632013-05-08 10:59:04 -0700616 }
617
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700618 self->PushShadowFrame(shadow_frame);
Alex Lighteb7c1442015-08-31 13:17:42 -0700619 ArtMethod* method = shadow_frame->GetMethod();
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200620 // Ensure static methods are initialized.
Alex Lighteb7c1442015-08-31 13:17:42 -0700621 const bool is_static = method->IsStatic();
Ian Rogerse94652f2014-12-02 11:13:19 -0800622 if (is_static) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700623 ObjPtr<mirror::Class> declaring_class = method->GetDeclaringClass();
Ian Rogers6c5cb212014-06-18 16:07:20 -0700624 if (UNLIKELY(!declaring_class->IsInitialized())) {
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700625 StackHandleScope<1> hs(self);
Mathieu Chartieref41db72016-10-25 15:08:01 -0700626 HandleWrapperObjPtr<mirror::Class> h_declaring_class(hs.NewHandleWrapper(&declaring_class));
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700627 if (UNLIKELY(!Runtime::Current()->GetClassLinker()->EnsureInitialized(
Ian Rogers7b078e82014-09-10 14:44:24 -0700628 self, h_declaring_class, true, true))) {
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700629 DCHECK(self->IsExceptionPending());
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700630 self->PopShadowFrame();
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200631 return;
632 }
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700633 CHECK(h_declaring_class->IsInitializing());
Jeff Hao16743632013-05-08 10:59:04 -0700634 }
Jeff Hao16743632013-05-08 10:59:04 -0700635 }
Jeff Hao16743632013-05-08 10:59:04 -0700636
Ian Rogerse94652f2014-12-02 11:13:19 -0800637 if (LIKELY(!shadow_frame->GetMethod()->IsNative())) {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800638 result->SetJ(Execute(self, accessor, *shadow_frame, JValue()).GetJ());
Jeff Hao16743632013-05-08 10:59:04 -0700639 } else {
640 // We don't expect to be asked to interpret native code (which is entered via a JNI compiler
641 // generated stub) except during testing and image writing.
642 CHECK(!Runtime::Current()->IsStarted());
Mathieu Chartieref41db72016-10-25 15:08:01 -0700643 ObjPtr<mirror::Object> receiver = is_static ? nullptr : shadow_frame->GetVRegReference(0);
Ian Rogerse94652f2014-12-02 11:13:19 -0800644 uint32_t* args = shadow_frame->GetVRegArgs(is_static ? 0 : 1);
Mathieu Chartieref41db72016-10-25 15:08:01 -0700645 UnstartedRuntime::Jni(self, shadow_frame->GetMethod(), receiver.Ptr(), args, result);
Jeff Hao16743632013-05-08 10:59:04 -0700646 }
647
648 self->PopShadowFrame();
Jeff Hao16743632013-05-08 10:59:04 -0700649}
650
buzbee1452bee2015-03-06 14:43:04 -0800651void CheckInterpreterAsmConstants() {
652 CheckMterpAsmConstants();
653}
654
655void InitInterpreterTls(Thread* self) {
656 InitMterpTls(self);
657}
658
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700659} // namespace interpreter
660} // namespace art