blob: 33498335ca665c6511b3a8ed0fa54cd12fe56c86 [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 Gampe3cfa4d02015-10-06 17:04:01 -070022#include "interpreter_common.h"
Andreas Gampe5e26eb12016-08-22 17:54:17 -070023#include "interpreter_mterp_impl.h"
24#include "interpreter_switch_impl.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070025#include "jit/jit.h"
26#include "jit/jit_code_cache.h"
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070027#include "jvalue-inl.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070028#include "mirror/string-inl.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070029#include "mterp/mterp.h"
Steven Morelande431e272017-07-18 16:53:49 -070030#include "nativehelper/ScopedLocalRef.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070031#include "scoped_thread_state_change-inl.h"
Andreas Gampeb3025922015-09-01 14:45:00 -070032#include "stack.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070033#include "thread-inl.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070034#include "unstarted_runtime.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070035
Ian Rogers2fa6b2e2012-10-17 00:10:17 -070036namespace art {
37namespace interpreter {
38
Mathieu Chartieref41db72016-10-25 15:08:01 -070039ALWAYS_INLINE static ObjPtr<mirror::Object> ObjArg(uint32_t arg)
40 REQUIRES_SHARED(Locks::mutator_lock_) {
41 return ObjPtr<mirror::Object>(reinterpret_cast<mirror::Object*>(arg));
42}
43
44static void InterpreterJni(Thread* self,
45 ArtMethod* method,
46 const StringPiece& shorty,
47 ObjPtr<mirror::Object> receiver,
48 uint32_t* args,
49 JValue* result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070050 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers64b6d142012-10-29 16:34:15 -070051 // TODO: The following enters JNI code using a typedef-ed function rather than the JNI compiler,
52 // it should be removed and JNI compiled stubs used instead.
53 ScopedObjectAccessUnchecked soa(self);
54 if (method->IsStatic()) {
55 if (shorty == "L") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +010056 typedef jobject (fntype)(JNIEnv*, jclass);
Mathieu Chartier2d721012014-11-10 11:08:06 -080057 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -070058 ScopedLocalRef<jclass> klass(soa.Env(),
59 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
Ian Rogers556d6372012-11-20 12:19:36 -080060 jobject jresult;
61 {
62 ScopedThreadStateChange tsc(self, kNative);
63 jresult = fn(soa.Env(), klass.get());
64 }
Mathieu Chartieref41db72016-10-25 15:08:01 -070065 result->SetL(soa.Decode<mirror::Object>(jresult));
Ian Rogers64b6d142012-10-29 16:34:15 -070066 } else if (shorty == "V") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +010067 typedef void (fntype)(JNIEnv*, jclass);
Mathieu Chartier2d721012014-11-10 11:08:06 -080068 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -070069 ScopedLocalRef<jclass> klass(soa.Env(),
70 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
71 ScopedThreadStateChange tsc(self, kNative);
72 fn(soa.Env(), klass.get());
73 } else if (shorty == "Z") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +010074 typedef jboolean (fntype)(JNIEnv*, jclass);
Mathieu Chartier2d721012014-11-10 11:08:06 -080075 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -070076 ScopedLocalRef<jclass> klass(soa.Env(),
77 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
78 ScopedThreadStateChange tsc(self, kNative);
79 result->SetZ(fn(soa.Env(), klass.get()));
80 } else if (shorty == "BI") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +010081 typedef jbyte (fntype)(JNIEnv*, jclass, jint);
Mathieu Chartier2d721012014-11-10 11:08:06 -080082 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -070083 ScopedLocalRef<jclass> klass(soa.Env(),
84 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
85 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -080086 result->SetB(fn(soa.Env(), klass.get(), args[0]));
Ian Rogers64b6d142012-10-29 16:34:15 -070087 } else if (shorty == "II") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +010088 typedef jint (fntype)(JNIEnv*, jclass, jint);
Mathieu Chartier2d721012014-11-10 11:08:06 -080089 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -070090 ScopedLocalRef<jclass> klass(soa.Env(),
91 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
92 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -080093 result->SetI(fn(soa.Env(), klass.get(), args[0]));
Ian Rogers64b6d142012-10-29 16:34:15 -070094 } else if (shorty == "LL") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +010095 typedef jobject (fntype)(JNIEnv*, jclass, jobject);
Mathieu Chartier2d721012014-11-10 11:08:06 -080096 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -070097 ScopedLocalRef<jclass> klass(soa.Env(),
98 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
99 ScopedLocalRef<jobject> arg0(soa.Env(),
Mathieu Chartieref41db72016-10-25 15:08:01 -0700100 soa.AddLocalReference<jobject>(ObjArg(args[0])));
Ian Rogers556d6372012-11-20 12:19:36 -0800101 jobject jresult;
102 {
103 ScopedThreadStateChange tsc(self, kNative);
104 jresult = fn(soa.Env(), klass.get(), arg0.get());
105 }
Mathieu Chartieref41db72016-10-25 15:08:01 -0700106 result->SetL(soa.Decode<mirror::Object>(jresult));
Ian Rogers64b6d142012-10-29 16:34:15 -0700107 } else if (shorty == "IIZ") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100108 typedef jint (fntype)(JNIEnv*, jclass, jint, jboolean);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800109 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700110 ScopedLocalRef<jclass> klass(soa.Env(),
111 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
112 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800113 result->SetI(fn(soa.Env(), klass.get(), args[0], args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700114 } else if (shorty == "ILI") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100115 typedef jint (fntype)(JNIEnv*, jclass, jobject, jint);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800116 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(
117 method->GetEntryPointFromJni()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700118 ScopedLocalRef<jclass> klass(soa.Env(),
119 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
120 ScopedLocalRef<jobject> arg0(soa.Env(),
Mathieu Chartieref41db72016-10-25 15:08:01 -0700121 soa.AddLocalReference<jobject>(ObjArg(args[0])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700122 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800123 result->SetI(fn(soa.Env(), klass.get(), arg0.get(), args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700124 } else if (shorty == "SIZ") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100125 typedef jshort (fntype)(JNIEnv*, jclass, jint, jboolean);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700126 fntype* const fn =
127 reinterpret_cast<fntype*>(const_cast<void*>(method->GetEntryPointFromJni()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700128 ScopedLocalRef<jclass> klass(soa.Env(),
129 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
130 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800131 result->SetS(fn(soa.Env(), klass.get(), args[0], args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700132 } else if (shorty == "VIZ") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100133 typedef void (fntype)(JNIEnv*, jclass, jint, jboolean);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800134 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700135 ScopedLocalRef<jclass> klass(soa.Env(),
136 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
137 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800138 fn(soa.Env(), klass.get(), args[0], args[1]);
Ian Rogers64b6d142012-10-29 16:34:15 -0700139 } else if (shorty == "ZLL") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100140 typedef jboolean (fntype)(JNIEnv*, jclass, jobject, jobject);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800141 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700142 ScopedLocalRef<jclass> klass(soa.Env(),
143 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
144 ScopedLocalRef<jobject> arg0(soa.Env(),
Mathieu Chartieref41db72016-10-25 15:08:01 -0700145 soa.AddLocalReference<jobject>(ObjArg(args[0])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700146 ScopedLocalRef<jobject> arg1(soa.Env(),
Mathieu Chartieref41db72016-10-25 15:08:01 -0700147 soa.AddLocalReference<jobject>(ObjArg(args[1])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700148 ScopedThreadStateChange tsc(self, kNative);
149 result->SetZ(fn(soa.Env(), klass.get(), arg0.get(), arg1.get()));
150 } else if (shorty == "ZILL") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100151 typedef jboolean (fntype)(JNIEnv*, jclass, jint, jobject, jobject);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800152 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700153 ScopedLocalRef<jclass> klass(soa.Env(),
154 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
155 ScopedLocalRef<jobject> arg1(soa.Env(),
Mathieu Chartieref41db72016-10-25 15:08:01 -0700156 soa.AddLocalReference<jobject>(ObjArg(args[1])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700157 ScopedLocalRef<jobject> arg2(soa.Env(),
Mathieu Chartieref41db72016-10-25 15:08:01 -0700158 soa.AddLocalReference<jobject>(ObjArg(args[2])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700159 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800160 result->SetZ(fn(soa.Env(), klass.get(), args[0], arg1.get(), arg2.get()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700161 } else if (shorty == "VILII") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100162 typedef void (fntype)(JNIEnv*, jclass, jint, jobject, jint, jint);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800163 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700164 ScopedLocalRef<jclass> klass(soa.Env(),
165 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
166 ScopedLocalRef<jobject> arg1(soa.Env(),
Mathieu Chartieref41db72016-10-25 15:08:01 -0700167 soa.AddLocalReference<jobject>(ObjArg(args[1])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700168 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800169 fn(soa.Env(), klass.get(), args[0], arg1.get(), args[2], args[3]);
Ian Rogers64b6d142012-10-29 16:34:15 -0700170 } else if (shorty == "VLILII") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100171 typedef void (fntype)(JNIEnv*, jclass, jobject, jint, jobject, jint, jint);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800172 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700173 ScopedLocalRef<jclass> klass(soa.Env(),
174 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
175 ScopedLocalRef<jobject> arg0(soa.Env(),
Mathieu Chartieref41db72016-10-25 15:08:01 -0700176 soa.AddLocalReference<jobject>(ObjArg(args[0])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700177 ScopedLocalRef<jobject> arg2(soa.Env(),
Mathieu Chartieref41db72016-10-25 15:08:01 -0700178 soa.AddLocalReference<jobject>(ObjArg(args[2])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700179 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800180 fn(soa.Env(), klass.get(), arg0.get(), args[1], arg2.get(), args[3], args[4]);
Ian Rogers64b6d142012-10-29 16:34:15 -0700181 } else {
David Sehr709b0702016-10-13 09:12:37 -0700182 LOG(FATAL) << "Do something with static native method: " << method->PrettyMethod()
Ian Rogers64b6d142012-10-29 16:34:15 -0700183 << " shorty: " << shorty;
184 }
185 } else {
186 if (shorty == "L") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100187 typedef jobject (fntype)(JNIEnv*, jobject);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800188 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700189 ScopedLocalRef<jobject> rcvr(soa.Env(),
190 soa.AddLocalReference<jobject>(receiver));
Ian Rogers556d6372012-11-20 12:19:36 -0800191 jobject jresult;
192 {
193 ScopedThreadStateChange tsc(self, kNative);
194 jresult = fn(soa.Env(), rcvr.get());
195 }
Mathieu Chartieref41db72016-10-25 15:08:01 -0700196 result->SetL(soa.Decode<mirror::Object>(jresult));
Jeff Hao3dd9f762013-07-08 13:09:25 -0700197 } else if (shorty == "V") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100198 typedef void (fntype)(JNIEnv*, jobject);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800199 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Jeff Hao3dd9f762013-07-08 13:09:25 -0700200 ScopedLocalRef<jobject> rcvr(soa.Env(),
201 soa.AddLocalReference<jobject>(receiver));
202 ScopedThreadStateChange tsc(self, kNative);
203 fn(soa.Env(), rcvr.get());
Ian Rogers64b6d142012-10-29 16:34:15 -0700204 } else if (shorty == "LL") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100205 typedef jobject (fntype)(JNIEnv*, jobject, jobject);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800206 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700207 ScopedLocalRef<jobject> rcvr(soa.Env(),
208 soa.AddLocalReference<jobject>(receiver));
209 ScopedLocalRef<jobject> arg0(soa.Env(),
Mathieu Chartieref41db72016-10-25 15:08:01 -0700210 soa.AddLocalReference<jobject>(ObjArg(args[0])));
Ian Rogers556d6372012-11-20 12:19:36 -0800211 jobject jresult;
212 {
213 ScopedThreadStateChange tsc(self, kNative);
214 jresult = fn(soa.Env(), rcvr.get(), arg0.get());
Ian Rogers556d6372012-11-20 12:19:36 -0800215 }
Mathieu Chartieref41db72016-10-25 15:08:01 -0700216 result->SetL(soa.Decode<mirror::Object>(jresult));
Ian Rogers64b6d142012-10-29 16:34:15 -0700217 ScopedThreadStateChange tsc(self, kNative);
Ian Rogers64b6d142012-10-29 16:34:15 -0700218 } else if (shorty == "III") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100219 typedef jint (fntype)(JNIEnv*, jobject, jint, jint);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800220 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700221 ScopedLocalRef<jobject> rcvr(soa.Env(),
222 soa.AddLocalReference<jobject>(receiver));
223 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800224 result->SetI(fn(soa.Env(), rcvr.get(), args[0], args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700225 } else {
David Sehr709b0702016-10-13 09:12:37 -0700226 LOG(FATAL) << "Do something with native method: " << method->PrettyMethod()
Ian Rogers64b6d142012-10-29 16:34:15 -0700227 << " shorty: " << shorty;
228 }
229 }
230}
231
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200232enum InterpreterImplKind {
buzbee1452bee2015-03-06 14:43:04 -0800233 kSwitchImplKind, // Switch-based interpreter implementation.
buzbee1452bee2015-03-06 14:43:04 -0800234 kMterpImplKind // Assembly interpreter
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200235};
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700236
buzbee1452bee2015-03-06 14:43:04 -0800237static constexpr InterpreterImplKind kInterpreterImplKind = kMterpImplKind;
Alexey Frunze00b53b72016-02-02 20:25:45 -0800238
Aart Bik01223202016-05-05 15:10:42 -0700239static inline JValue Execute(
240 Thread* self,
241 const DexFile::CodeItem* code_item,
242 ShadowFrame& shadow_frame,
243 JValue result_register,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700244 bool stay_in_interpreter = false) REQUIRES_SHARED(Locks::mutator_lock_) {
buzbee1452bee2015-03-06 14:43:04 -0800245 DCHECK(!shadow_frame.GetMethod()->IsAbstract());
Ian Rogers848871b2013-08-05 10:56:33 -0700246 DCHECK(!shadow_frame.GetMethod()->IsNative());
buzbee734f3aa2016-01-28 14:20:06 -0800247 if (LIKELY(shadow_frame.GetDexPC() == 0)) { // Entering the method, but not via deoptimization.
248 if (kIsDebugBuild) {
249 self->AssertNoPendingException();
250 }
251 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
252 ArtMethod *method = shadow_frame.GetMethod();
253
254 if (UNLIKELY(instrumentation->HasMethodEntryListeners())) {
255 instrumentation->MethodEnterEvent(self, shadow_frame.GetThisObject(code_item->ins_size_),
256 method, 0);
Alex Lightb7edcda2017-04-27 13:20:31 -0700257 if (UNLIKELY(self->IsExceptionPending())) {
258 instrumentation->MethodUnwindEvent(self,
259 shadow_frame.GetThisObject(code_item->ins_size_),
260 method,
261 0);
262 return JValue();
263 }
buzbee734f3aa2016-01-28 14:20:06 -0800264 }
265
Aart Bik01223202016-05-05 15:10:42 -0700266 if (!stay_in_interpreter) {
267 jit::Jit* jit = Runtime::Current()->GetJit();
268 if (jit != nullptr) {
269 jit->MethodEntered(self, shadow_frame.GetMethod());
270 if (jit->CanInvokeCompiledCode(method)) {
271 JValue result;
buzbee734f3aa2016-01-28 14:20:06 -0800272
Aart Bik01223202016-05-05 15:10:42 -0700273 // Pop the shadow frame before calling into compiled code.
274 self->PopShadowFrame();
Jeff Hao5ea84132017-05-05 16:59:29 -0700275 // Calculate the offset of the first input reg. The input registers are in the high regs.
276 // It's ok to access the code item here since JIT code will have been touched by the
277 // interpreter and compiler already.
278 uint16_t arg_offset = code_item->registers_size_ - code_item->ins_size_;
279 ArtInterpreterToCompiledCodeBridge(self, nullptr, &shadow_frame, arg_offset, &result);
Aart Bik01223202016-05-05 15:10:42 -0700280 // Push the shadow frame back as the caller will expect it.
281 self->PushShadowFrame(&shadow_frame);
buzbee734f3aa2016-01-28 14:20:06 -0800282
Aart Bik01223202016-05-05 15:10:42 -0700283 return result;
284 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100285 }
buzbee734f3aa2016-01-28 14:20:06 -0800286 }
287 }
288
Sebastien Hertz4e99b3d2014-06-24 14:35:40 +0200289 shadow_frame.GetMethod()->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200290
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700291 // Lock counting is a special version of accessibility checks, and for simplicity and
292 // reduction of template parameters, we gate it behind access-checks mode.
293 ArtMethod* method = shadow_frame.GetMethod();
294 DCHECK(!method->SkipAccessChecks() || !method->MustCountLocks());
295
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100296 bool transaction_active = Runtime::Current()->IsActiveTransaction();
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700297 if (LIKELY(method->SkipAccessChecks())) {
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200298 // Enter the "without access check" interpreter.
buzbee1452bee2015-03-06 14:43:04 -0800299 if (kInterpreterImplKind == kMterpImplKind) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100300 if (transaction_active) {
buzbee1452bee2015-03-06 14:43:04 -0800301 // No Mterp variant - just use the switch interpreter.
302 return ExecuteSwitchImpl<false, true>(self, code_item, shadow_frame, result_register,
303 false);
Bill Buzbeefd522f92016-02-11 22:37:42 +0000304 } else if (UNLIKELY(!Runtime::Current()->IsStarted())) {
305 return ExecuteSwitchImpl<false, false>(self, code_item, shadow_frame, result_register,
306 false);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100307 } else {
buzbee1452bee2015-03-06 14:43:04 -0800308 while (true) {
Bill Buzbeefd522f92016-02-11 22:37:42 +0000309 // Mterp does not support all instrumentation/debugging.
Andreas Gampe67409972016-07-19 22:34:53 -0700310 if (MterpShouldSwitchInterpreters() != 0) {
buzbee1452bee2015-03-06 14:43:04 -0800311 return ExecuteSwitchImpl<false, false>(self, code_item, shadow_frame, result_register,
312 false);
buzbee1452bee2015-03-06 14:43:04 -0800313 }
314 bool returned = ExecuteMterpImpl(self, code_item, &shadow_frame, &result_register);
315 if (returned) {
316 return result_register;
317 } else {
318 // Mterp didn't like that instruction. Single-step it with the reference interpreter.
buzbeed6b48db2016-01-28 15:48:55 -0800319 result_register = ExecuteSwitchImpl<false, false>(self, code_item, shadow_frame,
Mathieu Chartieref41db72016-10-25 15:08:01 -0700320 result_register, true);
buzbee1452bee2015-03-06 14:43:04 -0800321 if (shadow_frame.GetDexPC() == DexFile::kDexNoIndex) {
322 // Single-stepped a return or an exception not handled locally. Return to caller.
buzbeed6b48db2016-01-28 15:48:55 -0800323 return result_register;
buzbee1452bee2015-03-06 14:43:04 -0800324 }
325 }
326 }
327 }
buzbeef61df9b2016-09-07 07:12:29 -0700328 } else {
329 DCHECK_EQ(kInterpreterImplKind, kSwitchImplKind);
buzbee1452bee2015-03-06 14:43:04 -0800330 if (transaction_active) {
331 return ExecuteSwitchImpl<false, true>(self, code_item, shadow_frame, result_register,
332 false);
333 } else {
334 return ExecuteSwitchImpl<false, false>(self, code_item, shadow_frame, result_register,
335 false);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100336 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200337 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200338 } else {
339 // Enter the "with access check" interpreter.
buzbee1452bee2015-03-06 14:43:04 -0800340 if (kInterpreterImplKind == kMterpImplKind) {
341 // No access check variants for Mterp. Just use the switch version.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100342 if (transaction_active) {
buzbee1452bee2015-03-06 14:43:04 -0800343 return ExecuteSwitchImpl<true, true>(self, code_item, shadow_frame, result_register,
344 false);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100345 } else {
buzbee1452bee2015-03-06 14:43:04 -0800346 return ExecuteSwitchImpl<true, false>(self, code_item, shadow_frame, result_register,
347 false);
348 }
buzbeef61df9b2016-09-07 07:12:29 -0700349 } else {
350 DCHECK_EQ(kInterpreterImplKind, kSwitchImplKind);
buzbee1452bee2015-03-06 14:43:04 -0800351 if (transaction_active) {
352 return ExecuteSwitchImpl<true, true>(self, code_item, shadow_frame, result_register,
353 false);
354 } else {
355 return ExecuteSwitchImpl<true, false>(self, code_item, shadow_frame, result_register,
356 false);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100357 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200358 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200359 }
360}
361
Mathieu Chartieref41db72016-10-25 15:08:01 -0700362void EnterInterpreterFromInvoke(Thread* self,
363 ArtMethod* method,
364 ObjPtr<mirror::Object> receiver,
365 uint32_t* args,
366 JValue* result,
Aart Bik01223202016-05-05 15:10:42 -0700367 bool stay_in_interpreter) {
Ian Rogers64b6d142012-10-29 16:34:15 -0700368 DCHECK_EQ(self, Thread::Current());
Nicolas Geoffray535a3fb2014-07-22 15:17:38 +0100369 bool implicit_check = !Runtime::Current()->ExplicitStackOverflowChecks();
370 if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEndForInterpreter(implicit_check))) {
jeffhaod7521322012-11-21 15:38:24 -0800371 ThrowStackOverflowError(self);
372 return;
373 }
374
Alex Lightdb01a092017-04-03 15:39:55 -0700375 // This can happen if we are in forced interpreter mode and an obsolete method is called using
376 // reflection.
377 if (UNLIKELY(method->IsObsolete())) {
378 ThrowInternalError("Attempting to invoke obsolete version of '%s'.",
379 method->PrettyMethod().c_str());
380 return;
381 }
382
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700383 const char* old_cause = self->StartAssertNoThreadSuspension("EnterInterpreterFromInvoke");
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700384 const DexFile::CodeItem* code_item = method->GetCodeItem();
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700385 uint16_t num_regs;
386 uint16_t num_ins;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700387 if (code_item != nullptr) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700388 num_regs = code_item->registers_size_;
389 num_ins = code_item->ins_size_;
Alex Light9139e002015-10-09 15:59:48 -0700390 } else if (!method->IsInvokable()) {
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700391 self->EndAssertNoThreadSuspension(old_cause);
Alex Light9139e002015-10-09 15:59:48 -0700392 method->ThrowInvocationTimeError();
jeffhao0a9bb732012-11-26 12:28:49 -0800393 return;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700394 } else {
395 DCHECK(method->IsNative());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700396 num_regs = num_ins = ArtMethod::NumArgRegisters(method->GetShorty());
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700397 if (!method->IsStatic()) {
398 num_regs++;
399 num_ins++;
400 }
401 }
402 // Set up shadow frame with matching number of reference slots to vregs.
403 ShadowFrame* last_shadow_frame = self->GetManagedStack()->GetTopShadowFrame();
Andreas Gampeb3025922015-09-01 14:45:00 -0700404 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
Andreas Gampe03ec9302015-08-27 17:41:47 -0700405 CREATE_SHADOW_FRAME(num_regs, last_shadow_frame, method, /* dex pc */ 0);
Andreas Gampeb3025922015-09-01 14:45:00 -0700406 ShadowFrame* shadow_frame = shadow_frame_unique_ptr.get();
Jeff Hao66135192013-05-14 11:02:41 -0700407 self->PushShadowFrame(shadow_frame);
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700408
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700409 size_t cur_reg = num_regs - num_ins;
410 if (!method->IsStatic()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700411 CHECK(receiver != nullptr);
Mathieu Chartieref41db72016-10-25 15:08:01 -0700412 shadow_frame->SetVRegReference(cur_reg, receiver.Ptr());
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700413 ++cur_reg;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700414 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700415 uint32_t shorty_len = 0;
416 const char* shorty = method->GetShorty(&shorty_len);
Jeff Hao5d917302013-02-27 17:57:33 -0800417 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 -0700418 DCHECK_LT(shorty_pos + 1, shorty_len);
Jeff Hao5d917302013-02-27 17:57:33 -0800419 switch (shorty[shorty_pos + 1]) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700420 case 'L': {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700421 ObjPtr<mirror::Object> o =
422 reinterpret_cast<StackReference<mirror::Object>*>(&args[arg_pos])->AsMirrorPtr();
423 shadow_frame->SetVRegReference(cur_reg, o.Ptr());
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700424 break;
425 }
Jeff Hao5d917302013-02-27 17:57:33 -0800426 case 'J': case 'D': {
427 uint64_t wide_value = (static_cast<uint64_t>(args[arg_pos + 1]) << 32) | args[arg_pos];
428 shadow_frame->SetVRegLong(cur_reg, wide_value);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700429 cur_reg++;
Jeff Hao5d917302013-02-27 17:57:33 -0800430 arg_pos++;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700431 break;
Jeff Hao5d917302013-02-27 17:57:33 -0800432 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700433 default:
Jeff Hao5d917302013-02-27 17:57:33 -0800434 shadow_frame->SetVReg(cur_reg, args[arg_pos]);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700435 break;
436 }
437 }
Mathieu Chartier92246bb2014-02-25 18:22:39 -0800438 self->EndAssertNoThreadSuspension(old_cause);
439 // Do this after populating the shadow frame in case EnsureInitialized causes a GC.
Ian Rogers6c5cb212014-06-18 16:07:20 -0700440 if (method->IsStatic() && UNLIKELY(!method->GetDeclaringClass()->IsInitialized())) {
Mathieu Chartier92246bb2014-02-25 18:22:39 -0800441 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700442 StackHandleScope<1> hs(self);
443 Handle<mirror::Class> h_class(hs.NewHandle(method->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700444 if (UNLIKELY(!class_linker->EnsureInitialized(self, h_class, true, true))) {
Mathieu Chartier92246bb2014-02-25 18:22:39 -0800445 CHECK(self->IsExceptionPending());
446 self->PopShadowFrame();
447 return;
448 }
449 }
Ian Rogers64b6d142012-10-29 16:34:15 -0700450 if (LIKELY(!method->IsNative())) {
Aart Bik01223202016-05-05 15:10:42 -0700451 JValue r = Execute(self, code_item, *shadow_frame, JValue(), stay_in_interpreter);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700452 if (result != nullptr) {
Jeff Hao6474d192013-03-26 14:08:09 -0700453 *result = r;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700454 }
455 } else {
Ian Rogers64b6d142012-10-29 16:34:15 -0700456 // We don't expect to be asked to interpret native code (which is entered via a JNI compiler
457 // generated stub) except during testing and image writing.
Mathieu Chartier92246bb2014-02-25 18:22:39 -0800458 // Update args to be the args in the shadow frame since the input ones could hold stale
459 // references pointers due to moving GC.
460 args = shadow_frame->GetVRegArgs(method->IsStatic() ? 0 : 1);
Ian Rogers64b6d142012-10-29 16:34:15 -0700461 if (!Runtime::Current()->IsStarted()) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700462 UnstartedRuntime::Jni(self, method, receiver.Ptr(), args, result);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700463 } else {
Jeff Hao6474d192013-03-26 14:08:09 -0700464 InterpreterJni(self, method, shorty, receiver, args, result);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700465 }
466 }
467 self->PopShadowFrame();
468}
469
Mingyao Yangffedec52016-05-19 10:48:40 -0700470static bool IsStringInit(const Instruction* instr, ArtMethod* caller)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700471 REQUIRES_SHARED(Locks::mutator_lock_) {
Mingyao Yangffedec52016-05-19 10:48:40 -0700472 if (instr->Opcode() == Instruction::INVOKE_DIRECT ||
473 instr->Opcode() == Instruction::INVOKE_DIRECT_RANGE) {
474 // Instead of calling ResolveMethod() which has suspend point and can trigger
475 // GC, look up the callee method symbolically.
476 uint16_t callee_method_idx = (instr->Opcode() == Instruction::INVOKE_DIRECT_RANGE) ?
477 instr->VRegB_3rc() : instr->VRegB_35c();
478 const DexFile* dex_file = caller->GetDexFile();
479 const DexFile::MethodId& method_id = dex_file->GetMethodId(callee_method_idx);
480 const char* class_name = dex_file->StringByTypeIdx(method_id.class_idx_);
481 const char* method_name = dex_file->GetMethodName(method_id);
482 // Compare method's class name and method name against string init.
483 // It's ok since it's not allowed to create your own java/lang/String.
484 // TODO: verify that assumption.
485 if ((strcmp(class_name, "Ljava/lang/String;") == 0) &&
486 (strcmp(method_name, "<init>") == 0)) {
487 return true;
488 }
489 }
490 return false;
491}
492
493static int16_t GetReceiverRegisterForStringInit(const Instruction* instr) {
494 DCHECK(instr->Opcode() == Instruction::INVOKE_DIRECT_RANGE ||
495 instr->Opcode() == Instruction::INVOKE_DIRECT);
496 return (instr->Opcode() == Instruction::INVOKE_DIRECT_RANGE) ?
497 instr->VRegC_3rc() : instr->VRegC_35c();
498}
499
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100500void EnterInterpreterFromDeoptimize(Thread* self,
501 ShadowFrame* shadow_frame,
502 bool from_code,
Nicolas Geoffray07c70282017-08-30 08:09:42 +0000503 JValue* ret_val)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700504 REQUIRES_SHARED(Locks::mutator_lock_) {
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800505 JValue value;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700506 // Set value to last known result in case the shadow frame chain is empty.
507 value.SetJ(ret_val->GetJ());
Sebastien Hertz520633b2015-09-08 17:03:36 +0200508 // Are we executing the first shadow frame?
509 bool first = true;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700510 while (shadow_frame != nullptr) {
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700511 // We do not want to recover lock state for lock counting when deoptimizing. Currently,
512 // the compiler should not have compiled a method that failed structured-locking checks.
513 DCHECK(!shadow_frame->GetMethod()->MustCountLocks());
514
Ian Rogers62d6c772013-02-27 08:32:07 -0800515 self->SetTopOfShadowStack(shadow_frame);
Ian Rogerse94652f2014-12-02 11:13:19 -0800516 const DexFile::CodeItem* code_item = shadow_frame->GetMethod()->GetCodeItem();
Sebastien Hertz270a0e12015-01-16 19:49:09 +0100517 const uint32_t dex_pc = shadow_frame->GetDexPC();
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100518 uint32_t new_dex_pc = dex_pc;
Sebastien Hertz270a0e12015-01-16 19:49:09 +0100519 if (UNLIKELY(self->IsExceptionPending())) {
Sebastien Hertz520633b2015-09-08 17:03:36 +0200520 // If we deoptimize from the QuickExceptionHandler, we already reported the exception to
521 // the instrumentation. To prevent from reporting it a second time, we simply pass a
522 // null Instrumentation*.
Sebastien Hertz270a0e12015-01-16 19:49:09 +0100523 const instrumentation::Instrumentation* const instrumentation =
Sebastien Hertz520633b2015-09-08 17:03:36 +0200524 first ? nullptr : Runtime::Current()->GetInstrumentation();
Alex Light9fb1ab12017-09-05 09:32:49 -0700525 new_dex_pc = MoveToExceptionHandler(
526 self, *shadow_frame, instrumentation) ? shadow_frame->GetDexPC() : DexFile::kDexNoIndex;
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100527 } else if (!from_code) {
Nicolas Geoffray07c70282017-08-30 08:09:42 +0000528 // For the debugger and full deoptimization stack, we must go past the invoke
529 // instruction, as it already executed.
530 // TODO: should be tested more once b/17586779 is fixed.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100531 const Instruction* instr = Instruction::At(&code_item->insns_[dex_pc]);
Nicolas Geoffray07c70282017-08-30 08:09:42 +0000532 if (instr->IsInvoke()) {
Mingyao Yangffedec52016-05-19 10:48:40 -0700533 if (IsStringInit(instr, shadow_frame->GetMethod())) {
534 uint16_t this_obj_vreg = GetReceiverRegisterForStringInit(instr);
535 // Move the StringFactory.newStringFromChars() result into the register representing
536 // "this object" when invoking the string constructor in the original dex instruction.
537 // Also move the result into all aliases.
538 DCHECK(value.GetL()->IsString());
539 SetStringInitValueToAllAliases(shadow_frame, this_obj_vreg, value);
540 // Calling string constructor in the original dex code doesn't generate a result value.
541 value.SetJ(0);
542 }
Mingyao Yang504a6902016-04-28 16:23:01 -0700543 new_dex_pc = dex_pc + instr->SizeInCodeUnits();
544 } else if (instr->Opcode() == Instruction::NEW_INSTANCE) {
Nicolas Geoffray07c70282017-08-30 08:09:42 +0000545 // It's possible to deoptimize at a NEW_INSTANCE dex instruciton that's for a
546 // java string, which is turned into a call into StringFactory.newEmptyString();
547 // Move the StringFactory.newEmptyString() result into the destination register.
548 DCHECK(value.GetL()->IsString());
549 shadow_frame->SetVRegReference(instr->VRegA_21c(), value.GetL());
550 // new-instance doesn't generate a result value.
551 value.SetJ(0);
552 // Skip the dex instruction since we essentially come back from an invocation.
553 new_dex_pc = dex_pc + instr->SizeInCodeUnits();
554 if (kIsDebugBuild) {
555 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
556 // This is a suspend point. But it's ok since value has been set into shadow_frame.
557 ObjPtr<mirror::Class> klass = class_linker->ResolveType(
558 dex::TypeIndex(instr->VRegB_21c()), shadow_frame->GetMethod());
559 DCHECK(klass->IsStringClass());
560 }
Mingyao Yang504a6902016-04-28 16:23:01 -0700561 } else {
Nicolas Geoffray07c70282017-08-30 08:09:42 +0000562 CHECK(false) << "Unexpected instruction opcode " << instr->Opcode()
563 << " at dex_pc " << dex_pc
564 << " of method: " << ArtMethod::PrettyMethod(shadow_frame->GetMethod(), false);
Mingyao Yang504a6902016-04-28 16:23:01 -0700565 }
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100566 } else {
567 // Nothing to do, the dex_pc is the one at which the code requested
568 // the deoptimization.
Sebastien Hertz270a0e12015-01-16 19:49:09 +0100569 }
570 if (new_dex_pc != DexFile::kDexNoIndex) {
Nicolas Geoffray95974242017-09-04 08:45:51 +0000571 shadow_frame->SetDexPC(new_dex_pc);
Sebastien Hertz270a0e12015-01-16 19:49:09 +0100572 value = Execute(self, code_item, *shadow_frame, value);
573 }
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800574 ShadowFrame* old_frame = shadow_frame;
575 shadow_frame = shadow_frame->GetLink();
Christopher Ferris241a9582015-04-27 15:19:41 -0700576 ShadowFrame::DeleteDeoptimizedFrame(old_frame);
Nicolas Geoffray07c70282017-08-30 08:09:42 +0000577 // Following deoptimizations of shadow frames must pass the invoke instruction.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100578 from_code = false;
Sebastien Hertz520633b2015-09-08 17:03:36 +0200579 first = false;
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800580 }
581 ret_val->SetJ(value.GetJ());
582}
583
Ian Rogerse94652f2014-12-02 11:13:19 -0800584JValue EnterInterpreterFromEntryPoint(Thread* self, const DexFile::CodeItem* code_item,
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700585 ShadowFrame* shadow_frame) {
Ian Rogersf3e98552013-03-20 15:49:49 -0700586 DCHECK_EQ(self, Thread::Current());
Nicolas Geoffray535a3fb2014-07-22 15:17:38 +0100587 bool implicit_check = !Runtime::Current()->ExplicitStackOverflowChecks();
588 if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEndForInterpreter(implicit_check))) {
Ian Rogersf3e98552013-03-20 15:49:49 -0700589 ThrowStackOverflowError(self);
590 return JValue();
591 }
592
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100593 jit::Jit* jit = Runtime::Current()->GetJit();
594 if (jit != nullptr) {
595 jit->NotifyCompiledCodeToInterpreterTransition(self, shadow_frame->GetMethod());
596 }
Ian Rogerse94652f2014-12-02 11:13:19 -0800597 return Execute(self, code_item, *shadow_frame, JValue());
Ian Rogers7db619b2013-01-16 18:35:48 -0800598}
599
Mathieu Chartieref41db72016-10-25 15:08:01 -0700600void ArtInterpreterToInterpreterBridge(Thread* self,
601 const DexFile::CodeItem* code_item,
602 ShadowFrame* shadow_frame,
603 JValue* result) {
Nicolas Geoffray535a3fb2014-07-22 15:17:38 +0100604 bool implicit_check = !Runtime::Current()->ExplicitStackOverflowChecks();
605 if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEndForInterpreter(implicit_check))) {
Jeff Hao16743632013-05-08 10:59:04 -0700606 ThrowStackOverflowError(self);
Jeff Hao69510672013-05-21 17:34:55 -0700607 return;
Jeff Hao16743632013-05-08 10:59:04 -0700608 }
609
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700610 self->PushShadowFrame(shadow_frame);
Alex Lighteb7c1442015-08-31 13:17:42 -0700611 ArtMethod* method = shadow_frame->GetMethod();
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200612 // Ensure static methods are initialized.
Alex Lighteb7c1442015-08-31 13:17:42 -0700613 const bool is_static = method->IsStatic();
Ian Rogerse94652f2014-12-02 11:13:19 -0800614 if (is_static) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700615 ObjPtr<mirror::Class> declaring_class = method->GetDeclaringClass();
Ian Rogers6c5cb212014-06-18 16:07:20 -0700616 if (UNLIKELY(!declaring_class->IsInitialized())) {
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700617 StackHandleScope<1> hs(self);
Mathieu Chartieref41db72016-10-25 15:08:01 -0700618 HandleWrapperObjPtr<mirror::Class> h_declaring_class(hs.NewHandleWrapper(&declaring_class));
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700619 if (UNLIKELY(!Runtime::Current()->GetClassLinker()->EnsureInitialized(
Ian Rogers7b078e82014-09-10 14:44:24 -0700620 self, h_declaring_class, true, true))) {
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700621 DCHECK(self->IsExceptionPending());
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700622 self->PopShadowFrame();
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200623 return;
624 }
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700625 CHECK(h_declaring_class->IsInitializing());
Jeff Hao16743632013-05-08 10:59:04 -0700626 }
Jeff Hao16743632013-05-08 10:59:04 -0700627 }
Jeff Hao16743632013-05-08 10:59:04 -0700628
Ian Rogerse94652f2014-12-02 11:13:19 -0800629 if (LIKELY(!shadow_frame->GetMethod()->IsNative())) {
630 result->SetJ(Execute(self, code_item, *shadow_frame, JValue()).GetJ());
Jeff Hao16743632013-05-08 10:59:04 -0700631 } else {
632 // We don't expect to be asked to interpret native code (which is entered via a JNI compiler
633 // generated stub) except during testing and image writing.
634 CHECK(!Runtime::Current()->IsStarted());
Mathieu Chartieref41db72016-10-25 15:08:01 -0700635 ObjPtr<mirror::Object> receiver = is_static ? nullptr : shadow_frame->GetVRegReference(0);
Ian Rogerse94652f2014-12-02 11:13:19 -0800636 uint32_t* args = shadow_frame->GetVRegArgs(is_static ? 0 : 1);
Mathieu Chartieref41db72016-10-25 15:08:01 -0700637 UnstartedRuntime::Jni(self, shadow_frame->GetMethod(), receiver.Ptr(), args, result);
Jeff Hao16743632013-05-08 10:59:04 -0700638 }
639
640 self->PopShadowFrame();
Jeff Hao16743632013-05-08 10:59:04 -0700641}
642
buzbee1452bee2015-03-06 14:43:04 -0800643void CheckInterpreterAsmConstants() {
644 CheckMterpAsmConstants();
645}
646
647void InitInterpreterTls(Thread* self) {
648 InitMterpTls(self);
649}
650
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700651} // namespace interpreter
652} // namespace art