blob: 68a75b0196c81c278e231a3b655d4d2e6f19bf4f [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 Gampe103992b2016-01-04 15:32:43 -080021#include "common_throws.h"
Andreas Gampee2abbc62017-09-15 11:59:26 -070022#include "dex_file_types.h"
Andreas Gampe3cfa4d02015-10-06 17:04:01 -070023#include "interpreter_common.h"
Andreas Gampe5e26eb12016-08-22 17:54:17 -070024#include "interpreter_mterp_impl.h"
25#include "interpreter_switch_impl.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070026#include "jit/jit.h"
27#include "jit/jit_code_cache.h"
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070028#include "jvalue-inl.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070029#include "mirror/string-inl.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070030#include "mterp/mterp.h"
Steven Morelande431e272017-07-18 16:53:49 -070031#include "nativehelper/ScopedLocalRef.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070032#include "scoped_thread_state_change-inl.h"
Andreas Gampeb3025922015-09-01 14:45:00 -070033#include "stack.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070034#include "thread-inl.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070035#include "unstarted_runtime.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070036
Ian Rogers2fa6b2e2012-10-17 00:10:17 -070037namespace art {
38namespace interpreter {
39
Mathieu Chartieref41db72016-10-25 15:08:01 -070040ALWAYS_INLINE static ObjPtr<mirror::Object> ObjArg(uint32_t arg)
41 REQUIRES_SHARED(Locks::mutator_lock_) {
42 return ObjPtr<mirror::Object>(reinterpret_cast<mirror::Object*>(arg));
43}
44
45static void InterpreterJni(Thread* self,
46 ArtMethod* method,
47 const StringPiece& shorty,
48 ObjPtr<mirror::Object> receiver,
49 uint32_t* args,
50 JValue* result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070051 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers64b6d142012-10-29 16:34:15 -070052 // TODO: The following enters JNI code using a typedef-ed function rather than the JNI compiler,
53 // it should be removed and JNI compiled stubs used instead.
54 ScopedObjectAccessUnchecked soa(self);
55 if (method->IsStatic()) {
56 if (shorty == "L") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +010057 typedef jobject (fntype)(JNIEnv*, jclass);
Mathieu Chartier2d721012014-11-10 11:08:06 -080058 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -070059 ScopedLocalRef<jclass> klass(soa.Env(),
60 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
Ian Rogers556d6372012-11-20 12:19:36 -080061 jobject jresult;
62 {
63 ScopedThreadStateChange tsc(self, kNative);
64 jresult = fn(soa.Env(), klass.get());
65 }
Mathieu Chartieref41db72016-10-25 15:08:01 -070066 result->SetL(soa.Decode<mirror::Object>(jresult));
Ian Rogers64b6d142012-10-29 16:34:15 -070067 } else if (shorty == "V") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +010068 typedef void (fntype)(JNIEnv*, jclass);
Mathieu Chartier2d721012014-11-10 11:08:06 -080069 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -070070 ScopedLocalRef<jclass> klass(soa.Env(),
71 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
72 ScopedThreadStateChange tsc(self, kNative);
73 fn(soa.Env(), klass.get());
74 } else if (shorty == "Z") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +010075 typedef jboolean (fntype)(JNIEnv*, jclass);
Mathieu Chartier2d721012014-11-10 11:08:06 -080076 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -070077 ScopedLocalRef<jclass> klass(soa.Env(),
78 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
79 ScopedThreadStateChange tsc(self, kNative);
80 result->SetZ(fn(soa.Env(), klass.get()));
81 } else if (shorty == "BI") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +010082 typedef jbyte (fntype)(JNIEnv*, jclass, jint);
Mathieu Chartier2d721012014-11-10 11:08:06 -080083 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -070084 ScopedLocalRef<jclass> klass(soa.Env(),
85 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
86 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -080087 result->SetB(fn(soa.Env(), klass.get(), args[0]));
Ian Rogers64b6d142012-10-29 16:34:15 -070088 } else if (shorty == "II") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +010089 typedef jint (fntype)(JNIEnv*, jclass, jint);
Mathieu Chartier2d721012014-11-10 11:08:06 -080090 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -070091 ScopedLocalRef<jclass> klass(soa.Env(),
92 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
93 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -080094 result->SetI(fn(soa.Env(), klass.get(), args[0]));
Ian Rogers64b6d142012-10-29 16:34:15 -070095 } else if (shorty == "LL") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +010096 typedef jobject (fntype)(JNIEnv*, jclass, jobject);
Mathieu Chartier2d721012014-11-10 11:08:06 -080097 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -070098 ScopedLocalRef<jclass> klass(soa.Env(),
99 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
100 ScopedLocalRef<jobject> arg0(soa.Env(),
Mathieu Chartieref41db72016-10-25 15:08:01 -0700101 soa.AddLocalReference<jobject>(ObjArg(args[0])));
Ian Rogers556d6372012-11-20 12:19:36 -0800102 jobject jresult;
103 {
104 ScopedThreadStateChange tsc(self, kNative);
105 jresult = fn(soa.Env(), klass.get(), arg0.get());
106 }
Mathieu Chartieref41db72016-10-25 15:08:01 -0700107 result->SetL(soa.Decode<mirror::Object>(jresult));
Ian Rogers64b6d142012-10-29 16:34:15 -0700108 } else if (shorty == "IIZ") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100109 typedef jint (fntype)(JNIEnv*, jclass, jint, jboolean);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800110 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700111 ScopedLocalRef<jclass> klass(soa.Env(),
112 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
113 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800114 result->SetI(fn(soa.Env(), klass.get(), args[0], args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700115 } else if (shorty == "ILI") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100116 typedef jint (fntype)(JNIEnv*, jclass, jobject, jint);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800117 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(
118 method->GetEntryPointFromJni()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700119 ScopedLocalRef<jclass> klass(soa.Env(),
120 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
121 ScopedLocalRef<jobject> arg0(soa.Env(),
Mathieu Chartieref41db72016-10-25 15:08:01 -0700122 soa.AddLocalReference<jobject>(ObjArg(args[0])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700123 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800124 result->SetI(fn(soa.Env(), klass.get(), arg0.get(), args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700125 } else if (shorty == "SIZ") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100126 typedef jshort (fntype)(JNIEnv*, jclass, jint, jboolean);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700127 fntype* const fn =
128 reinterpret_cast<fntype*>(const_cast<void*>(method->GetEntryPointFromJni()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700129 ScopedLocalRef<jclass> klass(soa.Env(),
130 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
131 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800132 result->SetS(fn(soa.Env(), klass.get(), args[0], args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700133 } else if (shorty == "VIZ") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100134 typedef void (fntype)(JNIEnv*, jclass, jint, jboolean);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800135 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700136 ScopedLocalRef<jclass> klass(soa.Env(),
137 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
138 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800139 fn(soa.Env(), klass.get(), args[0], args[1]);
Ian Rogers64b6d142012-10-29 16:34:15 -0700140 } else if (shorty == "ZLL") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100141 typedef jboolean (fntype)(JNIEnv*, jclass, jobject, jobject);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800142 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700143 ScopedLocalRef<jclass> klass(soa.Env(),
144 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
145 ScopedLocalRef<jobject> arg0(soa.Env(),
Mathieu Chartieref41db72016-10-25 15:08:01 -0700146 soa.AddLocalReference<jobject>(ObjArg(args[0])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700147 ScopedLocalRef<jobject> arg1(soa.Env(),
Mathieu Chartieref41db72016-10-25 15:08:01 -0700148 soa.AddLocalReference<jobject>(ObjArg(args[1])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700149 ScopedThreadStateChange tsc(self, kNative);
150 result->SetZ(fn(soa.Env(), klass.get(), arg0.get(), arg1.get()));
151 } else if (shorty == "ZILL") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100152 typedef jboolean (fntype)(JNIEnv*, jclass, jint, jobject, jobject);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800153 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700154 ScopedLocalRef<jclass> klass(soa.Env(),
155 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
156 ScopedLocalRef<jobject> arg1(soa.Env(),
Mathieu Chartieref41db72016-10-25 15:08:01 -0700157 soa.AddLocalReference<jobject>(ObjArg(args[1])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700158 ScopedLocalRef<jobject> arg2(soa.Env(),
Mathieu Chartieref41db72016-10-25 15:08:01 -0700159 soa.AddLocalReference<jobject>(ObjArg(args[2])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700160 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800161 result->SetZ(fn(soa.Env(), klass.get(), args[0], arg1.get(), arg2.get()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700162 } else if (shorty == "VILII") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100163 typedef void (fntype)(JNIEnv*, jclass, jint, jobject, jint, jint);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800164 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700165 ScopedLocalRef<jclass> klass(soa.Env(),
166 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
167 ScopedLocalRef<jobject> arg1(soa.Env(),
Mathieu Chartieref41db72016-10-25 15:08:01 -0700168 soa.AddLocalReference<jobject>(ObjArg(args[1])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700169 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800170 fn(soa.Env(), klass.get(), args[0], arg1.get(), args[2], args[3]);
Ian Rogers64b6d142012-10-29 16:34:15 -0700171 } else if (shorty == "VLILII") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100172 typedef void (fntype)(JNIEnv*, jclass, jobject, jint, jobject, jint, jint);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800173 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700174 ScopedLocalRef<jclass> klass(soa.Env(),
175 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
176 ScopedLocalRef<jobject> arg0(soa.Env(),
Mathieu Chartieref41db72016-10-25 15:08:01 -0700177 soa.AddLocalReference<jobject>(ObjArg(args[0])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700178 ScopedLocalRef<jobject> arg2(soa.Env(),
Mathieu Chartieref41db72016-10-25 15:08:01 -0700179 soa.AddLocalReference<jobject>(ObjArg(args[2])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700180 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800181 fn(soa.Env(), klass.get(), arg0.get(), args[1], arg2.get(), args[3], args[4]);
Ian Rogers64b6d142012-10-29 16:34:15 -0700182 } else {
David Sehr709b0702016-10-13 09:12:37 -0700183 LOG(FATAL) << "Do something with static native method: " << method->PrettyMethod()
Ian Rogers64b6d142012-10-29 16:34:15 -0700184 << " shorty: " << shorty;
185 }
186 } else {
187 if (shorty == "L") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100188 typedef jobject (fntype)(JNIEnv*, jobject);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800189 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700190 ScopedLocalRef<jobject> rcvr(soa.Env(),
191 soa.AddLocalReference<jobject>(receiver));
Ian Rogers556d6372012-11-20 12:19:36 -0800192 jobject jresult;
193 {
194 ScopedThreadStateChange tsc(self, kNative);
195 jresult = fn(soa.Env(), rcvr.get());
196 }
Mathieu Chartieref41db72016-10-25 15:08:01 -0700197 result->SetL(soa.Decode<mirror::Object>(jresult));
Jeff Hao3dd9f762013-07-08 13:09:25 -0700198 } else if (shorty == "V") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100199 typedef void (fntype)(JNIEnv*, jobject);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800200 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Jeff Hao3dd9f762013-07-08 13:09:25 -0700201 ScopedLocalRef<jobject> rcvr(soa.Env(),
202 soa.AddLocalReference<jobject>(receiver));
203 ScopedThreadStateChange tsc(self, kNative);
204 fn(soa.Env(), rcvr.get());
Ian Rogers64b6d142012-10-29 16:34:15 -0700205 } else if (shorty == "LL") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100206 typedef jobject (fntype)(JNIEnv*, jobject, jobject);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800207 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700208 ScopedLocalRef<jobject> rcvr(soa.Env(),
209 soa.AddLocalReference<jobject>(receiver));
210 ScopedLocalRef<jobject> arg0(soa.Env(),
Mathieu Chartieref41db72016-10-25 15:08:01 -0700211 soa.AddLocalReference<jobject>(ObjArg(args[0])));
Ian Rogers556d6372012-11-20 12:19:36 -0800212 jobject jresult;
213 {
214 ScopedThreadStateChange tsc(self, kNative);
215 jresult = fn(soa.Env(), rcvr.get(), arg0.get());
Ian Rogers556d6372012-11-20 12:19:36 -0800216 }
Mathieu Chartieref41db72016-10-25 15:08:01 -0700217 result->SetL(soa.Decode<mirror::Object>(jresult));
Ian Rogers64b6d142012-10-29 16:34:15 -0700218 ScopedThreadStateChange tsc(self, kNative);
Ian Rogers64b6d142012-10-29 16:34:15 -0700219 } else if (shorty == "III") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100220 typedef jint (fntype)(JNIEnv*, jobject, jint, jint);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800221 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700222 ScopedLocalRef<jobject> rcvr(soa.Env(),
223 soa.AddLocalReference<jobject>(receiver));
224 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800225 result->SetI(fn(soa.Env(), rcvr.get(), args[0], args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700226 } else {
David Sehr709b0702016-10-13 09:12:37 -0700227 LOG(FATAL) << "Do something with native method: " << method->PrettyMethod()
Ian Rogers64b6d142012-10-29 16:34:15 -0700228 << " shorty: " << shorty;
229 }
230 }
231}
232
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200233enum InterpreterImplKind {
buzbee1452bee2015-03-06 14:43:04 -0800234 kSwitchImplKind, // Switch-based interpreter implementation.
buzbee1452bee2015-03-06 14:43:04 -0800235 kMterpImplKind // Assembly interpreter
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200236};
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700237
buzbee1452bee2015-03-06 14:43:04 -0800238static constexpr InterpreterImplKind kInterpreterImplKind = kMterpImplKind;
Alexey Frunze00b53b72016-02-02 20:25:45 -0800239
Aart Bik01223202016-05-05 15:10:42 -0700240static inline JValue Execute(
241 Thread* self,
242 const DexFile::CodeItem* code_item,
243 ShadowFrame& shadow_frame,
244 JValue result_register,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700245 bool stay_in_interpreter = false) REQUIRES_SHARED(Locks::mutator_lock_) {
buzbee1452bee2015-03-06 14:43:04 -0800246 DCHECK(!shadow_frame.GetMethod()->IsAbstract());
Ian Rogers848871b2013-08-05 10:56:33 -0700247 DCHECK(!shadow_frame.GetMethod()->IsNative());
buzbee734f3aa2016-01-28 14:20:06 -0800248 if (LIKELY(shadow_frame.GetDexPC() == 0)) { // Entering the method, but not via deoptimization.
249 if (kIsDebugBuild) {
250 self->AssertNoPendingException();
251 }
252 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
253 ArtMethod *method = shadow_frame.GetMethod();
254
255 if (UNLIKELY(instrumentation->HasMethodEntryListeners())) {
256 instrumentation->MethodEnterEvent(self, shadow_frame.GetThisObject(code_item->ins_size_),
257 method, 0);
Alex Lightb7edcda2017-04-27 13:20:31 -0700258 if (UNLIKELY(self->IsExceptionPending())) {
259 instrumentation->MethodUnwindEvent(self,
260 shadow_frame.GetThisObject(code_item->ins_size_),
261 method,
262 0);
263 return JValue();
264 }
buzbee734f3aa2016-01-28 14:20:06 -0800265 }
266
Aart Bik01223202016-05-05 15:10:42 -0700267 if (!stay_in_interpreter) {
268 jit::Jit* jit = Runtime::Current()->GetJit();
269 if (jit != nullptr) {
270 jit->MethodEntered(self, shadow_frame.GetMethod());
271 if (jit->CanInvokeCompiledCode(method)) {
272 JValue result;
buzbee734f3aa2016-01-28 14:20:06 -0800273
Aart Bik01223202016-05-05 15:10:42 -0700274 // Pop the shadow frame before calling into compiled code.
275 self->PopShadowFrame();
Jeff Hao5ea84132017-05-05 16:59:29 -0700276 // Calculate the offset of the first input reg. The input registers are in the high regs.
277 // It's ok to access the code item here since JIT code will have been touched by the
278 // interpreter and compiler already.
279 uint16_t arg_offset = code_item->registers_size_ - code_item->ins_size_;
280 ArtInterpreterToCompiledCodeBridge(self, nullptr, &shadow_frame, arg_offset, &result);
Aart Bik01223202016-05-05 15:10:42 -0700281 // Push the shadow frame back as the caller will expect it.
282 self->PushShadowFrame(&shadow_frame);
buzbee734f3aa2016-01-28 14:20:06 -0800283
Aart Bik01223202016-05-05 15:10:42 -0700284 return result;
285 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100286 }
buzbee734f3aa2016-01-28 14:20:06 -0800287 }
288 }
289
Sebastien Hertz4e99b3d2014-06-24 14:35:40 +0200290 shadow_frame.GetMethod()->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200291
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700292 // Lock counting is a special version of accessibility checks, and for simplicity and
293 // reduction of template parameters, we gate it behind access-checks mode.
294 ArtMethod* method = shadow_frame.GetMethod();
295 DCHECK(!method->SkipAccessChecks() || !method->MustCountLocks());
296
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100297 bool transaction_active = Runtime::Current()->IsActiveTransaction();
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700298 if (LIKELY(method->SkipAccessChecks())) {
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200299 // Enter the "without access check" interpreter.
buzbee1452bee2015-03-06 14:43:04 -0800300 if (kInterpreterImplKind == kMterpImplKind) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100301 if (transaction_active) {
buzbee1452bee2015-03-06 14:43:04 -0800302 // No Mterp variant - just use the switch interpreter.
303 return ExecuteSwitchImpl<false, true>(self, code_item, shadow_frame, result_register,
304 false);
Bill Buzbeefd522f92016-02-11 22:37:42 +0000305 } else if (UNLIKELY(!Runtime::Current()->IsStarted())) {
306 return ExecuteSwitchImpl<false, false>(self, code_item, shadow_frame, result_register,
307 false);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100308 } else {
buzbee1452bee2015-03-06 14:43:04 -0800309 while (true) {
Bill Buzbeefd522f92016-02-11 22:37:42 +0000310 // Mterp does not support all instrumentation/debugging.
Andreas Gampe67409972016-07-19 22:34:53 -0700311 if (MterpShouldSwitchInterpreters() != 0) {
buzbee1452bee2015-03-06 14:43:04 -0800312 return ExecuteSwitchImpl<false, false>(self, code_item, shadow_frame, result_register,
313 false);
buzbee1452bee2015-03-06 14:43:04 -0800314 }
315 bool returned = ExecuteMterpImpl(self, code_item, &shadow_frame, &result_register);
316 if (returned) {
317 return result_register;
318 } else {
319 // Mterp didn't like that instruction. Single-step it with the reference interpreter.
buzbeed6b48db2016-01-28 15:48:55 -0800320 result_register = ExecuteSwitchImpl<false, false>(self, code_item, shadow_frame,
Mathieu Chartieref41db72016-10-25 15:08:01 -0700321 result_register, true);
Andreas Gampee2abbc62017-09-15 11:59:26 -0700322 if (shadow_frame.GetDexPC() == dex::kDexNoIndex) {
buzbee1452bee2015-03-06 14:43:04 -0800323 // Single-stepped a return or an exception not handled locally. Return to caller.
buzbeed6b48db2016-01-28 15:48:55 -0800324 return result_register;
buzbee1452bee2015-03-06 14:43:04 -0800325 }
326 }
327 }
328 }
buzbeef61df9b2016-09-07 07:12:29 -0700329 } else {
330 DCHECK_EQ(kInterpreterImplKind, kSwitchImplKind);
buzbee1452bee2015-03-06 14:43:04 -0800331 if (transaction_active) {
332 return ExecuteSwitchImpl<false, true>(self, code_item, shadow_frame, result_register,
333 false);
334 } else {
335 return ExecuteSwitchImpl<false, false>(self, code_item, shadow_frame, result_register,
336 false);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100337 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200338 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200339 } else {
340 // Enter the "with access check" interpreter.
buzbee1452bee2015-03-06 14:43:04 -0800341 if (kInterpreterImplKind == kMterpImplKind) {
342 // No access check variants for Mterp. Just use the switch version.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100343 if (transaction_active) {
buzbee1452bee2015-03-06 14:43:04 -0800344 return ExecuteSwitchImpl<true, true>(self, code_item, shadow_frame, result_register,
345 false);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100346 } else {
buzbee1452bee2015-03-06 14:43:04 -0800347 return ExecuteSwitchImpl<true, false>(self, code_item, shadow_frame, result_register,
348 false);
349 }
buzbeef61df9b2016-09-07 07:12:29 -0700350 } else {
351 DCHECK_EQ(kInterpreterImplKind, kSwitchImplKind);
buzbee1452bee2015-03-06 14:43:04 -0800352 if (transaction_active) {
353 return ExecuteSwitchImpl<true, true>(self, code_item, shadow_frame, result_register,
354 false);
355 } else {
356 return ExecuteSwitchImpl<true, false>(self, code_item, shadow_frame, result_register,
357 false);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100358 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200359 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200360 }
361}
362
Mathieu Chartieref41db72016-10-25 15:08:01 -0700363void EnterInterpreterFromInvoke(Thread* self,
364 ArtMethod* method,
365 ObjPtr<mirror::Object> receiver,
366 uint32_t* args,
367 JValue* result,
Aart Bik01223202016-05-05 15:10:42 -0700368 bool stay_in_interpreter) {
Ian Rogers64b6d142012-10-29 16:34:15 -0700369 DCHECK_EQ(self, Thread::Current());
Nicolas Geoffray535a3fb2014-07-22 15:17:38 +0100370 bool implicit_check = !Runtime::Current()->ExplicitStackOverflowChecks();
371 if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEndForInterpreter(implicit_check))) {
jeffhaod7521322012-11-21 15:38:24 -0800372 ThrowStackOverflowError(self);
373 return;
374 }
375
Alex Lightdb01a092017-04-03 15:39:55 -0700376 // This can happen if we are in forced interpreter mode and an obsolete method is called using
377 // reflection.
378 if (UNLIKELY(method->IsObsolete())) {
379 ThrowInternalError("Attempting to invoke obsolete version of '%s'.",
380 method->PrettyMethod().c_str());
381 return;
382 }
383
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700384 const char* old_cause = self->StartAssertNoThreadSuspension("EnterInterpreterFromInvoke");
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700385 const DexFile::CodeItem* code_item = method->GetCodeItem();
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700386 uint16_t num_regs;
387 uint16_t num_ins;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700388 if (code_item != nullptr) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700389 num_regs = code_item->registers_size_;
390 num_ins = code_item->ins_size_;
Alex Light9139e002015-10-09 15:59:48 -0700391 } else if (!method->IsInvokable()) {
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700392 self->EndAssertNoThreadSuspension(old_cause);
Alex Light9139e002015-10-09 15:59:48 -0700393 method->ThrowInvocationTimeError();
jeffhao0a9bb732012-11-26 12:28:49 -0800394 return;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700395 } else {
396 DCHECK(method->IsNative());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700397 num_regs = num_ins = ArtMethod::NumArgRegisters(method->GetShorty());
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700398 if (!method->IsStatic()) {
399 num_regs++;
400 num_ins++;
401 }
402 }
403 // Set up shadow frame with matching number of reference slots to vregs.
404 ShadowFrame* last_shadow_frame = self->GetManagedStack()->GetTopShadowFrame();
Andreas Gampeb3025922015-09-01 14:45:00 -0700405 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
Andreas Gampe03ec9302015-08-27 17:41:47 -0700406 CREATE_SHADOW_FRAME(num_regs, last_shadow_frame, method, /* dex pc */ 0);
Andreas Gampeb3025922015-09-01 14:45:00 -0700407 ShadowFrame* shadow_frame = shadow_frame_unique_ptr.get();
Jeff Hao66135192013-05-14 11:02:41 -0700408 self->PushShadowFrame(shadow_frame);
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700409
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700410 size_t cur_reg = num_regs - num_ins;
411 if (!method->IsStatic()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700412 CHECK(receiver != nullptr);
Mathieu Chartieref41db72016-10-25 15:08:01 -0700413 shadow_frame->SetVRegReference(cur_reg, receiver.Ptr());
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700414 ++cur_reg;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700415 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700416 uint32_t shorty_len = 0;
417 const char* shorty = method->GetShorty(&shorty_len);
Jeff Hao5d917302013-02-27 17:57:33 -0800418 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 -0700419 DCHECK_LT(shorty_pos + 1, shorty_len);
Jeff Hao5d917302013-02-27 17:57:33 -0800420 switch (shorty[shorty_pos + 1]) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700421 case 'L': {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700422 ObjPtr<mirror::Object> o =
423 reinterpret_cast<StackReference<mirror::Object>*>(&args[arg_pos])->AsMirrorPtr();
424 shadow_frame->SetVRegReference(cur_reg, o.Ptr());
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700425 break;
426 }
Jeff Hao5d917302013-02-27 17:57:33 -0800427 case 'J': case 'D': {
428 uint64_t wide_value = (static_cast<uint64_t>(args[arg_pos + 1]) << 32) | args[arg_pos];
429 shadow_frame->SetVRegLong(cur_reg, wide_value);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700430 cur_reg++;
Jeff Hao5d917302013-02-27 17:57:33 -0800431 arg_pos++;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700432 break;
Jeff Hao5d917302013-02-27 17:57:33 -0800433 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700434 default:
Jeff Hao5d917302013-02-27 17:57:33 -0800435 shadow_frame->SetVReg(cur_reg, args[arg_pos]);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700436 break;
437 }
438 }
Mathieu Chartier92246bb2014-02-25 18:22:39 -0800439 self->EndAssertNoThreadSuspension(old_cause);
440 // Do this after populating the shadow frame in case EnsureInitialized causes a GC.
Ian Rogers6c5cb212014-06-18 16:07:20 -0700441 if (method->IsStatic() && UNLIKELY(!method->GetDeclaringClass()->IsInitialized())) {
Mathieu Chartier92246bb2014-02-25 18:22:39 -0800442 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700443 StackHandleScope<1> hs(self);
444 Handle<mirror::Class> h_class(hs.NewHandle(method->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700445 if (UNLIKELY(!class_linker->EnsureInitialized(self, h_class, true, true))) {
Mathieu Chartier92246bb2014-02-25 18:22:39 -0800446 CHECK(self->IsExceptionPending());
447 self->PopShadowFrame();
448 return;
449 }
450 }
Ian Rogers64b6d142012-10-29 16:34:15 -0700451 if (LIKELY(!method->IsNative())) {
Aart Bik01223202016-05-05 15:10:42 -0700452 JValue r = Execute(self, code_item, *shadow_frame, JValue(), stay_in_interpreter);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700453 if (result != nullptr) {
Jeff Hao6474d192013-03-26 14:08:09 -0700454 *result = r;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700455 }
456 } else {
Ian Rogers64b6d142012-10-29 16:34:15 -0700457 // We don't expect to be asked to interpret native code (which is entered via a JNI compiler
458 // generated stub) except during testing and image writing.
Mathieu Chartier92246bb2014-02-25 18:22:39 -0800459 // Update args to be the args in the shadow frame since the input ones could hold stale
460 // references pointers due to moving GC.
461 args = shadow_frame->GetVRegArgs(method->IsStatic() ? 0 : 1);
Ian Rogers64b6d142012-10-29 16:34:15 -0700462 if (!Runtime::Current()->IsStarted()) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700463 UnstartedRuntime::Jni(self, method, receiver.Ptr(), args, result);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700464 } else {
Jeff Hao6474d192013-03-26 14:08:09 -0700465 InterpreterJni(self, method, shorty, receiver, args, result);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700466 }
467 }
468 self->PopShadowFrame();
469}
470
Mingyao Yangffedec52016-05-19 10:48:40 -0700471static int16_t GetReceiverRegisterForStringInit(const Instruction* instr) {
472 DCHECK(instr->Opcode() == Instruction::INVOKE_DIRECT_RANGE ||
473 instr->Opcode() == Instruction::INVOKE_DIRECT);
474 return (instr->Opcode() == Instruction::INVOKE_DIRECT_RANGE) ?
475 instr->VRegC_3rc() : instr->VRegC_35c();
476}
477
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100478void EnterInterpreterFromDeoptimize(Thread* self,
479 ShadowFrame* shadow_frame,
Mingyao Yang2ee17902017-08-30 11:37:08 -0700480 JValue* ret_val,
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100481 bool from_code,
Mingyao Yang2ee17902017-08-30 11:37:08 -0700482 DeoptimizationMethodType deopt_method_type)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700483 REQUIRES_SHARED(Locks::mutator_lock_) {
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800484 JValue value;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700485 // Set value to last known result in case the shadow frame chain is empty.
486 value.SetJ(ret_val->GetJ());
Sebastien Hertz520633b2015-09-08 17:03:36 +0200487 // Are we executing the first shadow frame?
488 bool first = true;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700489 while (shadow_frame != nullptr) {
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700490 // We do not want to recover lock state for lock counting when deoptimizing. Currently,
491 // the compiler should not have compiled a method that failed structured-locking checks.
492 DCHECK(!shadow_frame->GetMethod()->MustCountLocks());
493
Ian Rogers62d6c772013-02-27 08:32:07 -0800494 self->SetTopOfShadowStack(shadow_frame);
Ian Rogerse94652f2014-12-02 11:13:19 -0800495 const DexFile::CodeItem* code_item = shadow_frame->GetMethod()->GetCodeItem();
Sebastien Hertz270a0e12015-01-16 19:49:09 +0100496 const uint32_t dex_pc = shadow_frame->GetDexPC();
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100497 uint32_t new_dex_pc = dex_pc;
Sebastien Hertz270a0e12015-01-16 19:49:09 +0100498 if (UNLIKELY(self->IsExceptionPending())) {
Sebastien Hertz520633b2015-09-08 17:03:36 +0200499 // If we deoptimize from the QuickExceptionHandler, we already reported the exception to
500 // the instrumentation. To prevent from reporting it a second time, we simply pass a
501 // null Instrumentation*.
Sebastien Hertz270a0e12015-01-16 19:49:09 +0100502 const instrumentation::Instrumentation* const instrumentation =
Sebastien Hertz520633b2015-09-08 17:03:36 +0200503 first ? nullptr : Runtime::Current()->GetInstrumentation();
Alex Light9fb1ab12017-09-05 09:32:49 -0700504 new_dex_pc = MoveToExceptionHandler(
Andreas Gampee2abbc62017-09-15 11:59:26 -0700505 self, *shadow_frame, instrumentation) ? shadow_frame->GetDexPC() : dex::kDexNoIndex;
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100506 } else if (!from_code) {
Mingyao Yang2ee17902017-08-30 11:37:08 -0700507 // Deoptimization is not called from code directly.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100508 const Instruction* instr = Instruction::At(&code_item->insns_[dex_pc]);
Mingyao Yang2ee17902017-08-30 11:37:08 -0700509 if (deopt_method_type == DeoptimizationMethodType::kKeepDexPc) {
510 DCHECK(first);
511 // Need to re-execute the dex instruction.
512 // (1) An invocation might be split into class initialization and invoke.
513 // In this case, the invoke should not be skipped.
514 // (2) A suspend check should also execute the dex instruction at the
515 // corresponding dex pc.
516 DCHECK_EQ(new_dex_pc, dex_pc);
517 } else if (instr->Opcode() == Instruction::MONITOR_ENTER ||
518 instr->Opcode() == Instruction::MONITOR_EXIT) {
519 DCHECK(deopt_method_type == DeoptimizationMethodType::kDefault);
520 DCHECK(first);
521 // Non-idempotent dex instruction should not be re-executed.
522 // On the other hand, if a MONITOR_ENTER is at the dex_pc of a suspend
523 // check, that MONITOR_ENTER should be executed. That case is handled
524 // above.
525 new_dex_pc = dex_pc + instr->SizeInCodeUnits();
526 } else if (instr->IsInvoke()) {
527 DCHECK(deopt_method_type == DeoptimizationMethodType::kDefault);
Mingyao Yangffedec52016-05-19 10:48:40 -0700528 if (IsStringInit(instr, shadow_frame->GetMethod())) {
529 uint16_t this_obj_vreg = GetReceiverRegisterForStringInit(instr);
530 // Move the StringFactory.newStringFromChars() result into the register representing
531 // "this object" when invoking the string constructor in the original dex instruction.
532 // Also move the result into all aliases.
533 DCHECK(value.GetL()->IsString());
534 SetStringInitValueToAllAliases(shadow_frame, this_obj_vreg, value);
535 // Calling string constructor in the original dex code doesn't generate a result value.
536 value.SetJ(0);
537 }
Mingyao Yang504a6902016-04-28 16:23:01 -0700538 new_dex_pc = dex_pc + instr->SizeInCodeUnits();
539 } else if (instr->Opcode() == Instruction::NEW_INSTANCE) {
Mingyao Yang2ee17902017-08-30 11:37:08 -0700540 // A NEW_INSTANCE is simply re-executed, including
541 // "new-instance String" which is compiled into a call into
542 // StringFactory.newEmptyString().
543 DCHECK_EQ(new_dex_pc, dex_pc);
Mingyao Yang504a6902016-04-28 16:23:01 -0700544 } else {
Mingyao Yang2ee17902017-08-30 11:37:08 -0700545 DCHECK(deopt_method_type == DeoptimizationMethodType::kDefault);
546 DCHECK(first);
547 // By default, we re-execute the dex instruction since if they are not
548 // an invoke, so that we don't have to decode the dex instruction to move
549 // result into the right vreg. All slow paths have been audited to be
550 // idempotent except monitor-enter/exit and invocation stubs.
551 // TODO: move result and advance dex pc. That also requires that we
552 // can tell the return type of a runtime method, possibly by decoding
553 // the dex instruction at the caller.
554 DCHECK_EQ(new_dex_pc, dex_pc);
Mingyao Yang504a6902016-04-28 16:23:01 -0700555 }
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100556 } else {
557 // Nothing to do, the dex_pc is the one at which the code requested
558 // the deoptimization.
Mingyao Yang2ee17902017-08-30 11:37:08 -0700559 DCHECK(first);
560 DCHECK_EQ(new_dex_pc, dex_pc);
Sebastien Hertz270a0e12015-01-16 19:49:09 +0100561 }
Andreas Gampee2abbc62017-09-15 11:59:26 -0700562 if (new_dex_pc != dex::kDexNoIndex) {
Nicolas Geoffray95974242017-09-04 08:45:51 +0000563 shadow_frame->SetDexPC(new_dex_pc);
Sebastien Hertz270a0e12015-01-16 19:49:09 +0100564 value = Execute(self, code_item, *shadow_frame, value);
565 }
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800566 ShadowFrame* old_frame = shadow_frame;
567 shadow_frame = shadow_frame->GetLink();
Christopher Ferris241a9582015-04-27 15:19:41 -0700568 ShadowFrame::DeleteDeoptimizedFrame(old_frame);
Mingyao Yang2ee17902017-08-30 11:37:08 -0700569 // Following deoptimizations of shadow frames must be at invocation point
570 // and should advance dex pc past the invoke instruction.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100571 from_code = false;
Mingyao Yang2ee17902017-08-30 11:37:08 -0700572 deopt_method_type = DeoptimizationMethodType::kDefault;
Sebastien Hertz520633b2015-09-08 17:03:36 +0200573 first = false;
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800574 }
575 ret_val->SetJ(value.GetJ());
576}
577
Ian Rogerse94652f2014-12-02 11:13:19 -0800578JValue EnterInterpreterFromEntryPoint(Thread* self, const DexFile::CodeItem* code_item,
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700579 ShadowFrame* shadow_frame) {
Ian Rogersf3e98552013-03-20 15:49:49 -0700580 DCHECK_EQ(self, Thread::Current());
Nicolas Geoffray535a3fb2014-07-22 15:17:38 +0100581 bool implicit_check = !Runtime::Current()->ExplicitStackOverflowChecks();
582 if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEndForInterpreter(implicit_check))) {
Ian Rogersf3e98552013-03-20 15:49:49 -0700583 ThrowStackOverflowError(self);
584 return JValue();
585 }
586
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100587 jit::Jit* jit = Runtime::Current()->GetJit();
588 if (jit != nullptr) {
589 jit->NotifyCompiledCodeToInterpreterTransition(self, shadow_frame->GetMethod());
590 }
Ian Rogerse94652f2014-12-02 11:13:19 -0800591 return Execute(self, code_item, *shadow_frame, JValue());
Ian Rogers7db619b2013-01-16 18:35:48 -0800592}
593
Mathieu Chartieref41db72016-10-25 15:08:01 -0700594void ArtInterpreterToInterpreterBridge(Thread* self,
595 const DexFile::CodeItem* code_item,
596 ShadowFrame* shadow_frame,
597 JValue* result) {
Nicolas Geoffray535a3fb2014-07-22 15:17:38 +0100598 bool implicit_check = !Runtime::Current()->ExplicitStackOverflowChecks();
599 if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEndForInterpreter(implicit_check))) {
Jeff Hao16743632013-05-08 10:59:04 -0700600 ThrowStackOverflowError(self);
Jeff Hao69510672013-05-21 17:34:55 -0700601 return;
Jeff Hao16743632013-05-08 10:59:04 -0700602 }
603
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700604 self->PushShadowFrame(shadow_frame);
Alex Lighteb7c1442015-08-31 13:17:42 -0700605 ArtMethod* method = shadow_frame->GetMethod();
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200606 // Ensure static methods are initialized.
Alex Lighteb7c1442015-08-31 13:17:42 -0700607 const bool is_static = method->IsStatic();
Ian Rogerse94652f2014-12-02 11:13:19 -0800608 if (is_static) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700609 ObjPtr<mirror::Class> declaring_class = method->GetDeclaringClass();
Ian Rogers6c5cb212014-06-18 16:07:20 -0700610 if (UNLIKELY(!declaring_class->IsInitialized())) {
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700611 StackHandleScope<1> hs(self);
Mathieu Chartieref41db72016-10-25 15:08:01 -0700612 HandleWrapperObjPtr<mirror::Class> h_declaring_class(hs.NewHandleWrapper(&declaring_class));
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700613 if (UNLIKELY(!Runtime::Current()->GetClassLinker()->EnsureInitialized(
Ian Rogers7b078e82014-09-10 14:44:24 -0700614 self, h_declaring_class, true, true))) {
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700615 DCHECK(self->IsExceptionPending());
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700616 self->PopShadowFrame();
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200617 return;
618 }
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700619 CHECK(h_declaring_class->IsInitializing());
Jeff Hao16743632013-05-08 10:59:04 -0700620 }
Jeff Hao16743632013-05-08 10:59:04 -0700621 }
Jeff Hao16743632013-05-08 10:59:04 -0700622
Ian Rogerse94652f2014-12-02 11:13:19 -0800623 if (LIKELY(!shadow_frame->GetMethod()->IsNative())) {
624 result->SetJ(Execute(self, code_item, *shadow_frame, JValue()).GetJ());
Jeff Hao16743632013-05-08 10:59:04 -0700625 } else {
626 // We don't expect to be asked to interpret native code (which is entered via a JNI compiler
627 // generated stub) except during testing and image writing.
628 CHECK(!Runtime::Current()->IsStarted());
Mathieu Chartieref41db72016-10-25 15:08:01 -0700629 ObjPtr<mirror::Object> receiver = is_static ? nullptr : shadow_frame->GetVRegReference(0);
Ian Rogerse94652f2014-12-02 11:13:19 -0800630 uint32_t* args = shadow_frame->GetVRegArgs(is_static ? 0 : 1);
Mathieu Chartieref41db72016-10-25 15:08:01 -0700631 UnstartedRuntime::Jni(self, shadow_frame->GetMethod(), receiver.Ptr(), args, result);
Jeff Hao16743632013-05-08 10:59:04 -0700632 }
633
634 self->PopShadowFrame();
Jeff Hao16743632013-05-08 10:59:04 -0700635}
636
buzbee1452bee2015-03-06 14:43:04 -0800637void CheckInterpreterAsmConstants() {
638 CheckMterpAsmConstants();
639}
640
641void InitInterpreterTls(Thread* self) {
642 InitMterpTls(self);
643}
644
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700645} // namespace interpreter
646} // namespace art