blob: d2f5232de1f85272c95e2acd22c7b58902b8d28a [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"
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070025#include "jvalue-inl.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070026#include "mirror/string-inl.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070027#include "scoped_thread_state_change-inl.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070028#include "ScopedLocalRef.h"
Andreas Gampeb3025922015-09-01 14:45:00 -070029#include "stack.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070030#include "unstarted_runtime.h"
buzbee1452bee2015-03-06 14:43:04 -080031#include "mterp/mterp.h"
buzbee734f3aa2016-01-28 14:20:06 -080032#include "jit/jit.h"
Tamas Berghammerdd5e5e92016-02-12 16:29:00 +000033#include "jit/jit_code_cache.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070034
Ian Rogers2fa6b2e2012-10-17 00:10:17 -070035namespace art {
36namespace interpreter {
37
Mathieu Chartieref41db72016-10-25 15:08:01 -070038ALWAYS_INLINE static ObjPtr<mirror::Object> ObjArg(uint32_t arg)
39 REQUIRES_SHARED(Locks::mutator_lock_) {
40 return ObjPtr<mirror::Object>(reinterpret_cast<mirror::Object*>(arg));
41}
42
43static void InterpreterJni(Thread* self,
44 ArtMethod* method,
45 const StringPiece& shorty,
46 ObjPtr<mirror::Object> receiver,
47 uint32_t* args,
48 JValue* result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070049 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers64b6d142012-10-29 16:34:15 -070050 // TODO: The following enters JNI code using a typedef-ed function rather than the JNI compiler,
51 // it should be removed and JNI compiled stubs used instead.
52 ScopedObjectAccessUnchecked soa(self);
53 if (method->IsStatic()) {
54 if (shorty == "L") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +010055 typedef jobject (fntype)(JNIEnv*, jclass);
Mathieu Chartier2d721012014-11-10 11:08:06 -080056 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -070057 ScopedLocalRef<jclass> klass(soa.Env(),
58 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
Ian Rogers556d6372012-11-20 12:19:36 -080059 jobject jresult;
60 {
61 ScopedThreadStateChange tsc(self, kNative);
62 jresult = fn(soa.Env(), klass.get());
63 }
Mathieu Chartieref41db72016-10-25 15:08:01 -070064 result->SetL(soa.Decode<mirror::Object>(jresult));
Ian Rogers64b6d142012-10-29 16:34:15 -070065 } else if (shorty == "V") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +010066 typedef void (fntype)(JNIEnv*, jclass);
Mathieu Chartier2d721012014-11-10 11:08:06 -080067 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -070068 ScopedLocalRef<jclass> klass(soa.Env(),
69 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
70 ScopedThreadStateChange tsc(self, kNative);
71 fn(soa.Env(), klass.get());
72 } else if (shorty == "Z") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +010073 typedef jboolean (fntype)(JNIEnv*, jclass);
Mathieu Chartier2d721012014-11-10 11:08:06 -080074 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -070075 ScopedLocalRef<jclass> klass(soa.Env(),
76 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
77 ScopedThreadStateChange tsc(self, kNative);
78 result->SetZ(fn(soa.Env(), klass.get()));
79 } else if (shorty == "BI") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +010080 typedef jbyte (fntype)(JNIEnv*, jclass, jint);
Mathieu Chartier2d721012014-11-10 11:08:06 -080081 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -070082 ScopedLocalRef<jclass> klass(soa.Env(),
83 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
84 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -080085 result->SetB(fn(soa.Env(), klass.get(), args[0]));
Ian Rogers64b6d142012-10-29 16:34:15 -070086 } else if (shorty == "II") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +010087 typedef jint (fntype)(JNIEnv*, jclass, jint);
Mathieu Chartier2d721012014-11-10 11:08:06 -080088 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -070089 ScopedLocalRef<jclass> klass(soa.Env(),
90 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
91 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -080092 result->SetI(fn(soa.Env(), klass.get(), args[0]));
Ian Rogers64b6d142012-10-29 16:34:15 -070093 } else if (shorty == "LL") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +010094 typedef jobject (fntype)(JNIEnv*, jclass, jobject);
Mathieu Chartier2d721012014-11-10 11:08:06 -080095 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -070096 ScopedLocalRef<jclass> klass(soa.Env(),
97 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
98 ScopedLocalRef<jobject> arg0(soa.Env(),
Mathieu Chartieref41db72016-10-25 15:08:01 -070099 soa.AddLocalReference<jobject>(ObjArg(args[0])));
Ian Rogers556d6372012-11-20 12:19:36 -0800100 jobject jresult;
101 {
102 ScopedThreadStateChange tsc(self, kNative);
103 jresult = fn(soa.Env(), klass.get(), arg0.get());
104 }
Mathieu Chartieref41db72016-10-25 15:08:01 -0700105 result->SetL(soa.Decode<mirror::Object>(jresult));
Ian Rogers64b6d142012-10-29 16:34:15 -0700106 } else if (shorty == "IIZ") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100107 typedef jint (fntype)(JNIEnv*, jclass, jint, jboolean);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800108 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700109 ScopedLocalRef<jclass> klass(soa.Env(),
110 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
111 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800112 result->SetI(fn(soa.Env(), klass.get(), args[0], args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700113 } else if (shorty == "ILI") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100114 typedef jint (fntype)(JNIEnv*, jclass, jobject, jint);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800115 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(
116 method->GetEntryPointFromJni()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700117 ScopedLocalRef<jclass> klass(soa.Env(),
118 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
119 ScopedLocalRef<jobject> arg0(soa.Env(),
Mathieu Chartieref41db72016-10-25 15:08:01 -0700120 soa.AddLocalReference<jobject>(ObjArg(args[0])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700121 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800122 result->SetI(fn(soa.Env(), klass.get(), arg0.get(), args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700123 } else if (shorty == "SIZ") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100124 typedef jshort (fntype)(JNIEnv*, jclass, jint, jboolean);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700125 fntype* const fn =
126 reinterpret_cast<fntype*>(const_cast<void*>(method->GetEntryPointFromJni()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700127 ScopedLocalRef<jclass> klass(soa.Env(),
128 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
129 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800130 result->SetS(fn(soa.Env(), klass.get(), args[0], args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700131 } else if (shorty == "VIZ") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100132 typedef void (fntype)(JNIEnv*, jclass, jint, jboolean);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800133 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700134 ScopedLocalRef<jclass> klass(soa.Env(),
135 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
136 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800137 fn(soa.Env(), klass.get(), args[0], args[1]);
Ian Rogers64b6d142012-10-29 16:34:15 -0700138 } else if (shorty == "ZLL") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100139 typedef jboolean (fntype)(JNIEnv*, jclass, jobject, jobject);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800140 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700141 ScopedLocalRef<jclass> klass(soa.Env(),
142 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
143 ScopedLocalRef<jobject> arg0(soa.Env(),
Mathieu Chartieref41db72016-10-25 15:08:01 -0700144 soa.AddLocalReference<jobject>(ObjArg(args[0])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700145 ScopedLocalRef<jobject> arg1(soa.Env(),
Mathieu Chartieref41db72016-10-25 15:08:01 -0700146 soa.AddLocalReference<jobject>(ObjArg(args[1])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700147 ScopedThreadStateChange tsc(self, kNative);
148 result->SetZ(fn(soa.Env(), klass.get(), arg0.get(), arg1.get()));
149 } else if (shorty == "ZILL") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100150 typedef jboolean (fntype)(JNIEnv*, jclass, jint, jobject, jobject);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800151 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700152 ScopedLocalRef<jclass> klass(soa.Env(),
153 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
154 ScopedLocalRef<jobject> arg1(soa.Env(),
Mathieu Chartieref41db72016-10-25 15:08:01 -0700155 soa.AddLocalReference<jobject>(ObjArg(args[1])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700156 ScopedLocalRef<jobject> arg2(soa.Env(),
Mathieu Chartieref41db72016-10-25 15:08:01 -0700157 soa.AddLocalReference<jobject>(ObjArg(args[2])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700158 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800159 result->SetZ(fn(soa.Env(), klass.get(), args[0], arg1.get(), arg2.get()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700160 } else if (shorty == "VILII") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100161 typedef void (fntype)(JNIEnv*, jclass, jint, jobject, jint, jint);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800162 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700163 ScopedLocalRef<jclass> klass(soa.Env(),
164 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
165 ScopedLocalRef<jobject> arg1(soa.Env(),
Mathieu Chartieref41db72016-10-25 15:08:01 -0700166 soa.AddLocalReference<jobject>(ObjArg(args[1])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700167 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800168 fn(soa.Env(), klass.get(), args[0], arg1.get(), args[2], args[3]);
Ian Rogers64b6d142012-10-29 16:34:15 -0700169 } else if (shorty == "VLILII") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100170 typedef void (fntype)(JNIEnv*, jclass, jobject, jint, jobject, jint, jint);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800171 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700172 ScopedLocalRef<jclass> klass(soa.Env(),
173 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
174 ScopedLocalRef<jobject> arg0(soa.Env(),
Mathieu Chartieref41db72016-10-25 15:08:01 -0700175 soa.AddLocalReference<jobject>(ObjArg(args[0])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700176 ScopedLocalRef<jobject> arg2(soa.Env(),
Mathieu Chartieref41db72016-10-25 15:08:01 -0700177 soa.AddLocalReference<jobject>(ObjArg(args[2])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700178 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800179 fn(soa.Env(), klass.get(), arg0.get(), args[1], arg2.get(), args[3], args[4]);
Ian Rogers64b6d142012-10-29 16:34:15 -0700180 } else {
David Sehr709b0702016-10-13 09:12:37 -0700181 LOG(FATAL) << "Do something with static native method: " << method->PrettyMethod()
Ian Rogers64b6d142012-10-29 16:34:15 -0700182 << " shorty: " << shorty;
183 }
184 } else {
185 if (shorty == "L") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100186 typedef jobject (fntype)(JNIEnv*, jobject);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800187 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700188 ScopedLocalRef<jobject> rcvr(soa.Env(),
189 soa.AddLocalReference<jobject>(receiver));
Ian Rogers556d6372012-11-20 12:19:36 -0800190 jobject jresult;
191 {
192 ScopedThreadStateChange tsc(self, kNative);
193 jresult = fn(soa.Env(), rcvr.get());
194 }
Mathieu Chartieref41db72016-10-25 15:08:01 -0700195 result->SetL(soa.Decode<mirror::Object>(jresult));
Jeff Hao3dd9f762013-07-08 13:09:25 -0700196 } else if (shorty == "V") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100197 typedef void (fntype)(JNIEnv*, jobject);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800198 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Jeff Hao3dd9f762013-07-08 13:09:25 -0700199 ScopedLocalRef<jobject> rcvr(soa.Env(),
200 soa.AddLocalReference<jobject>(receiver));
201 ScopedThreadStateChange tsc(self, kNative);
202 fn(soa.Env(), rcvr.get());
Ian Rogers64b6d142012-10-29 16:34:15 -0700203 } else if (shorty == "LL") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100204 typedef jobject (fntype)(JNIEnv*, jobject, jobject);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800205 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700206 ScopedLocalRef<jobject> rcvr(soa.Env(),
207 soa.AddLocalReference<jobject>(receiver));
208 ScopedLocalRef<jobject> arg0(soa.Env(),
Mathieu Chartieref41db72016-10-25 15:08:01 -0700209 soa.AddLocalReference<jobject>(ObjArg(args[0])));
Ian Rogers556d6372012-11-20 12:19:36 -0800210 jobject jresult;
211 {
212 ScopedThreadStateChange tsc(self, kNative);
213 jresult = fn(soa.Env(), rcvr.get(), arg0.get());
Ian Rogers556d6372012-11-20 12:19:36 -0800214 }
Mathieu Chartieref41db72016-10-25 15:08:01 -0700215 result->SetL(soa.Decode<mirror::Object>(jresult));
Ian Rogers64b6d142012-10-29 16:34:15 -0700216 ScopedThreadStateChange tsc(self, kNative);
Ian Rogers64b6d142012-10-29 16:34:15 -0700217 } else if (shorty == "III") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100218 typedef jint (fntype)(JNIEnv*, jobject, jint, jint);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800219 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700220 ScopedLocalRef<jobject> rcvr(soa.Env(),
221 soa.AddLocalReference<jobject>(receiver));
222 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800223 result->SetI(fn(soa.Env(), rcvr.get(), args[0], args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700224 } else {
David Sehr709b0702016-10-13 09:12:37 -0700225 LOG(FATAL) << "Do something with native method: " << method->PrettyMethod()
Ian Rogers64b6d142012-10-29 16:34:15 -0700226 << " shorty: " << shorty;
227 }
228 }
229}
230
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200231enum InterpreterImplKind {
buzbee1452bee2015-03-06 14:43:04 -0800232 kSwitchImplKind, // Switch-based interpreter implementation.
buzbee1452bee2015-03-06 14:43:04 -0800233 kMterpImplKind // Assembly interpreter
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200234};
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700235
buzbee1452bee2015-03-06 14:43:04 -0800236static constexpr InterpreterImplKind kInterpreterImplKind = kMterpImplKind;
Alexey Frunze00b53b72016-02-02 20:25:45 -0800237
Aart Bik01223202016-05-05 15:10:42 -0700238static inline JValue Execute(
239 Thread* self,
240 const DexFile::CodeItem* code_item,
241 ShadowFrame& shadow_frame,
242 JValue result_register,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700243 bool stay_in_interpreter = false) REQUIRES_SHARED(Locks::mutator_lock_) {
buzbee1452bee2015-03-06 14:43:04 -0800244 DCHECK(!shadow_frame.GetMethod()->IsAbstract());
Ian Rogers848871b2013-08-05 10:56:33 -0700245 DCHECK(!shadow_frame.GetMethod()->IsNative());
buzbee734f3aa2016-01-28 14:20:06 -0800246 if (LIKELY(shadow_frame.GetDexPC() == 0)) { // Entering the method, but not via deoptimization.
247 if (kIsDebugBuild) {
248 self->AssertNoPendingException();
249 }
250 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
251 ArtMethod *method = shadow_frame.GetMethod();
252
253 if (UNLIKELY(instrumentation->HasMethodEntryListeners())) {
254 instrumentation->MethodEnterEvent(self, shadow_frame.GetThisObject(code_item->ins_size_),
255 method, 0);
256 }
257
Aart Bik01223202016-05-05 15:10:42 -0700258 if (!stay_in_interpreter) {
259 jit::Jit* jit = Runtime::Current()->GetJit();
260 if (jit != nullptr) {
261 jit->MethodEntered(self, shadow_frame.GetMethod());
262 if (jit->CanInvokeCompiledCode(method)) {
263 JValue result;
buzbee734f3aa2016-01-28 14:20:06 -0800264
Aart Bik01223202016-05-05 15:10:42 -0700265 // Pop the shadow frame before calling into compiled code.
266 self->PopShadowFrame();
Jeff Hao5ea84132017-05-05 16:59:29 -0700267 // Calculate the offset of the first input reg. The input registers are in the high regs.
268 // It's ok to access the code item here since JIT code will have been touched by the
269 // interpreter and compiler already.
270 uint16_t arg_offset = code_item->registers_size_ - code_item->ins_size_;
271 ArtInterpreterToCompiledCodeBridge(self, nullptr, &shadow_frame, arg_offset, &result);
Aart Bik01223202016-05-05 15:10:42 -0700272 // Push the shadow frame back as the caller will expect it.
273 self->PushShadowFrame(&shadow_frame);
buzbee734f3aa2016-01-28 14:20:06 -0800274
Aart Bik01223202016-05-05 15:10:42 -0700275 return result;
276 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100277 }
buzbee734f3aa2016-01-28 14:20:06 -0800278 }
279 }
280
Sebastien Hertz4e99b3d2014-06-24 14:35:40 +0200281 shadow_frame.GetMethod()->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200282
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700283 // Lock counting is a special version of accessibility checks, and for simplicity and
284 // reduction of template parameters, we gate it behind access-checks mode.
285 ArtMethod* method = shadow_frame.GetMethod();
286 DCHECK(!method->SkipAccessChecks() || !method->MustCountLocks());
287
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100288 bool transaction_active = Runtime::Current()->IsActiveTransaction();
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700289 if (LIKELY(method->SkipAccessChecks())) {
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200290 // Enter the "without access check" interpreter.
buzbee1452bee2015-03-06 14:43:04 -0800291 if (kInterpreterImplKind == kMterpImplKind) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100292 if (transaction_active) {
buzbee1452bee2015-03-06 14:43:04 -0800293 // No Mterp variant - just use the switch interpreter.
294 return ExecuteSwitchImpl<false, true>(self, code_item, shadow_frame, result_register,
295 false);
Bill Buzbeefd522f92016-02-11 22:37:42 +0000296 } else if (UNLIKELY(!Runtime::Current()->IsStarted())) {
297 return ExecuteSwitchImpl<false, false>(self, code_item, shadow_frame, result_register,
298 false);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100299 } else {
buzbee1452bee2015-03-06 14:43:04 -0800300 while (true) {
Bill Buzbeefd522f92016-02-11 22:37:42 +0000301 // Mterp does not support all instrumentation/debugging.
Andreas Gampe67409972016-07-19 22:34:53 -0700302 if (MterpShouldSwitchInterpreters() != 0) {
buzbee1452bee2015-03-06 14:43:04 -0800303 return ExecuteSwitchImpl<false, false>(self, code_item, shadow_frame, result_register,
304 false);
buzbee1452bee2015-03-06 14:43:04 -0800305 }
306 bool returned = ExecuteMterpImpl(self, code_item, &shadow_frame, &result_register);
307 if (returned) {
308 return result_register;
309 } else {
310 // Mterp didn't like that instruction. Single-step it with the reference interpreter.
buzbeed6b48db2016-01-28 15:48:55 -0800311 result_register = ExecuteSwitchImpl<false, false>(self, code_item, shadow_frame,
Mathieu Chartieref41db72016-10-25 15:08:01 -0700312 result_register, true);
buzbee1452bee2015-03-06 14:43:04 -0800313 if (shadow_frame.GetDexPC() == DexFile::kDexNoIndex) {
314 // Single-stepped a return or an exception not handled locally. Return to caller.
buzbeed6b48db2016-01-28 15:48:55 -0800315 return result_register;
buzbee1452bee2015-03-06 14:43:04 -0800316 }
317 }
318 }
319 }
buzbeef61df9b2016-09-07 07:12:29 -0700320 } else {
321 DCHECK_EQ(kInterpreterImplKind, kSwitchImplKind);
buzbee1452bee2015-03-06 14:43:04 -0800322 if (transaction_active) {
323 return ExecuteSwitchImpl<false, true>(self, code_item, shadow_frame, result_register,
324 false);
325 } else {
326 return ExecuteSwitchImpl<false, false>(self, code_item, shadow_frame, result_register,
327 false);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100328 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200329 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200330 } else {
331 // Enter the "with access check" interpreter.
buzbee1452bee2015-03-06 14:43:04 -0800332 if (kInterpreterImplKind == kMterpImplKind) {
333 // No access check variants for Mterp. Just use the switch version.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100334 if (transaction_active) {
buzbee1452bee2015-03-06 14:43:04 -0800335 return ExecuteSwitchImpl<true, true>(self, code_item, shadow_frame, result_register,
336 false);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100337 } else {
buzbee1452bee2015-03-06 14:43:04 -0800338 return ExecuteSwitchImpl<true, false>(self, code_item, shadow_frame, result_register,
339 false);
340 }
buzbeef61df9b2016-09-07 07:12:29 -0700341 } else {
342 DCHECK_EQ(kInterpreterImplKind, kSwitchImplKind);
buzbee1452bee2015-03-06 14:43:04 -0800343 if (transaction_active) {
344 return ExecuteSwitchImpl<true, true>(self, code_item, shadow_frame, result_register,
345 false);
346 } else {
347 return ExecuteSwitchImpl<true, false>(self, code_item, shadow_frame, result_register,
348 false);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100349 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200350 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200351 }
352}
353
Mathieu Chartieref41db72016-10-25 15:08:01 -0700354void EnterInterpreterFromInvoke(Thread* self,
355 ArtMethod* method,
356 ObjPtr<mirror::Object> receiver,
357 uint32_t* args,
358 JValue* result,
Aart Bik01223202016-05-05 15:10:42 -0700359 bool stay_in_interpreter) {
Ian Rogers64b6d142012-10-29 16:34:15 -0700360 DCHECK_EQ(self, Thread::Current());
Nicolas Geoffray535a3fb2014-07-22 15:17:38 +0100361 bool implicit_check = !Runtime::Current()->ExplicitStackOverflowChecks();
362 if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEndForInterpreter(implicit_check))) {
jeffhaod7521322012-11-21 15:38:24 -0800363 ThrowStackOverflowError(self);
364 return;
365 }
366
Alex Lightdb01a092017-04-03 15:39:55 -0700367 // This can happen if we are in forced interpreter mode and an obsolete method is called using
368 // reflection.
369 if (UNLIKELY(method->IsObsolete())) {
370 ThrowInternalError("Attempting to invoke obsolete version of '%s'.",
371 method->PrettyMethod().c_str());
372 return;
373 }
374
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700375 const char* old_cause = self->StartAssertNoThreadSuspension("EnterInterpreterFromInvoke");
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700376 const DexFile::CodeItem* code_item = method->GetCodeItem();
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700377 uint16_t num_regs;
378 uint16_t num_ins;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700379 if (code_item != nullptr) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700380 num_regs = code_item->registers_size_;
381 num_ins = code_item->ins_size_;
Alex Light9139e002015-10-09 15:59:48 -0700382 } else if (!method->IsInvokable()) {
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700383 self->EndAssertNoThreadSuspension(old_cause);
Alex Light9139e002015-10-09 15:59:48 -0700384 method->ThrowInvocationTimeError();
jeffhao0a9bb732012-11-26 12:28:49 -0800385 return;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700386 } else {
387 DCHECK(method->IsNative());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700388 num_regs = num_ins = ArtMethod::NumArgRegisters(method->GetShorty());
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700389 if (!method->IsStatic()) {
390 num_regs++;
391 num_ins++;
392 }
393 }
394 // Set up shadow frame with matching number of reference slots to vregs.
395 ShadowFrame* last_shadow_frame = self->GetManagedStack()->GetTopShadowFrame();
Andreas Gampeb3025922015-09-01 14:45:00 -0700396 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
Andreas Gampe03ec9302015-08-27 17:41:47 -0700397 CREATE_SHADOW_FRAME(num_regs, last_shadow_frame, method, /* dex pc */ 0);
Andreas Gampeb3025922015-09-01 14:45:00 -0700398 ShadowFrame* shadow_frame = shadow_frame_unique_ptr.get();
Jeff Hao66135192013-05-14 11:02:41 -0700399 self->PushShadowFrame(shadow_frame);
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700400
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700401 size_t cur_reg = num_regs - num_ins;
402 if (!method->IsStatic()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700403 CHECK(receiver != nullptr);
Mathieu Chartieref41db72016-10-25 15:08:01 -0700404 shadow_frame->SetVRegReference(cur_reg, receiver.Ptr());
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700405 ++cur_reg;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700406 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700407 uint32_t shorty_len = 0;
408 const char* shorty = method->GetShorty(&shorty_len);
Jeff Hao5d917302013-02-27 17:57:33 -0800409 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 -0700410 DCHECK_LT(shorty_pos + 1, shorty_len);
Jeff Hao5d917302013-02-27 17:57:33 -0800411 switch (shorty[shorty_pos + 1]) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700412 case 'L': {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700413 ObjPtr<mirror::Object> o =
414 reinterpret_cast<StackReference<mirror::Object>*>(&args[arg_pos])->AsMirrorPtr();
415 shadow_frame->SetVRegReference(cur_reg, o.Ptr());
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700416 break;
417 }
Jeff Hao5d917302013-02-27 17:57:33 -0800418 case 'J': case 'D': {
419 uint64_t wide_value = (static_cast<uint64_t>(args[arg_pos + 1]) << 32) | args[arg_pos];
420 shadow_frame->SetVRegLong(cur_reg, wide_value);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700421 cur_reg++;
Jeff Hao5d917302013-02-27 17:57:33 -0800422 arg_pos++;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700423 break;
Jeff Hao5d917302013-02-27 17:57:33 -0800424 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700425 default:
Jeff Hao5d917302013-02-27 17:57:33 -0800426 shadow_frame->SetVReg(cur_reg, args[arg_pos]);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700427 break;
428 }
429 }
Mathieu Chartier92246bb2014-02-25 18:22:39 -0800430 self->EndAssertNoThreadSuspension(old_cause);
431 // Do this after populating the shadow frame in case EnsureInitialized causes a GC.
Ian Rogers6c5cb212014-06-18 16:07:20 -0700432 if (method->IsStatic() && UNLIKELY(!method->GetDeclaringClass()->IsInitialized())) {
Mathieu Chartier92246bb2014-02-25 18:22:39 -0800433 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700434 StackHandleScope<1> hs(self);
435 Handle<mirror::Class> h_class(hs.NewHandle(method->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700436 if (UNLIKELY(!class_linker->EnsureInitialized(self, h_class, true, true))) {
Mathieu Chartier92246bb2014-02-25 18:22:39 -0800437 CHECK(self->IsExceptionPending());
438 self->PopShadowFrame();
439 return;
440 }
441 }
Ian Rogers64b6d142012-10-29 16:34:15 -0700442 if (LIKELY(!method->IsNative())) {
Aart Bik01223202016-05-05 15:10:42 -0700443 JValue r = Execute(self, code_item, *shadow_frame, JValue(), stay_in_interpreter);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700444 if (result != nullptr) {
Jeff Hao6474d192013-03-26 14:08:09 -0700445 *result = r;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700446 }
447 } else {
Ian Rogers64b6d142012-10-29 16:34:15 -0700448 // We don't expect to be asked to interpret native code (which is entered via a JNI compiler
449 // generated stub) except during testing and image writing.
Mathieu Chartier92246bb2014-02-25 18:22:39 -0800450 // Update args to be the args in the shadow frame since the input ones could hold stale
451 // references pointers due to moving GC.
452 args = shadow_frame->GetVRegArgs(method->IsStatic() ? 0 : 1);
Ian Rogers64b6d142012-10-29 16:34:15 -0700453 if (!Runtime::Current()->IsStarted()) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700454 UnstartedRuntime::Jni(self, method, receiver.Ptr(), args, result);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700455 } else {
Jeff Hao6474d192013-03-26 14:08:09 -0700456 InterpreterJni(self, method, shorty, receiver, args, result);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700457 }
458 }
459 self->PopShadowFrame();
460}
461
Mingyao Yangffedec52016-05-19 10:48:40 -0700462static bool IsStringInit(const Instruction* instr, ArtMethod* caller)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700463 REQUIRES_SHARED(Locks::mutator_lock_) {
Mingyao Yangffedec52016-05-19 10:48:40 -0700464 if (instr->Opcode() == Instruction::INVOKE_DIRECT ||
465 instr->Opcode() == Instruction::INVOKE_DIRECT_RANGE) {
466 // Instead of calling ResolveMethod() which has suspend point and can trigger
467 // GC, look up the callee method symbolically.
468 uint16_t callee_method_idx = (instr->Opcode() == Instruction::INVOKE_DIRECT_RANGE) ?
469 instr->VRegB_3rc() : instr->VRegB_35c();
470 const DexFile* dex_file = caller->GetDexFile();
471 const DexFile::MethodId& method_id = dex_file->GetMethodId(callee_method_idx);
472 const char* class_name = dex_file->StringByTypeIdx(method_id.class_idx_);
473 const char* method_name = dex_file->GetMethodName(method_id);
474 // Compare method's class name and method name against string init.
475 // It's ok since it's not allowed to create your own java/lang/String.
476 // TODO: verify that assumption.
477 if ((strcmp(class_name, "Ljava/lang/String;") == 0) &&
478 (strcmp(method_name, "<init>") == 0)) {
479 return true;
480 }
481 }
482 return false;
483}
484
485static int16_t GetReceiverRegisterForStringInit(const Instruction* instr) {
486 DCHECK(instr->Opcode() == Instruction::INVOKE_DIRECT_RANGE ||
487 instr->Opcode() == Instruction::INVOKE_DIRECT);
488 return (instr->Opcode() == Instruction::INVOKE_DIRECT_RANGE) ?
489 instr->VRegC_3rc() : instr->VRegC_35c();
490}
491
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100492void EnterInterpreterFromDeoptimize(Thread* self,
493 ShadowFrame* shadow_frame,
494 bool from_code,
495 JValue* ret_val)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700496 REQUIRES_SHARED(Locks::mutator_lock_) {
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800497 JValue value;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700498 // Set value to last known result in case the shadow frame chain is empty.
499 value.SetJ(ret_val->GetJ());
Sebastien Hertz520633b2015-09-08 17:03:36 +0200500 // Are we executing the first shadow frame?
501 bool first = true;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700502 while (shadow_frame != nullptr) {
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700503 // We do not want to recover lock state for lock counting when deoptimizing. Currently,
504 // the compiler should not have compiled a method that failed structured-locking checks.
505 DCHECK(!shadow_frame->GetMethod()->MustCountLocks());
506
Ian Rogers62d6c772013-02-27 08:32:07 -0800507 self->SetTopOfShadowStack(shadow_frame);
Ian Rogerse94652f2014-12-02 11:13:19 -0800508 const DexFile::CodeItem* code_item = shadow_frame->GetMethod()->GetCodeItem();
Sebastien Hertz270a0e12015-01-16 19:49:09 +0100509 const uint32_t dex_pc = shadow_frame->GetDexPC();
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100510 uint32_t new_dex_pc = dex_pc;
Sebastien Hertz270a0e12015-01-16 19:49:09 +0100511 if (UNLIKELY(self->IsExceptionPending())) {
Sebastien Hertz520633b2015-09-08 17:03:36 +0200512 // If we deoptimize from the QuickExceptionHandler, we already reported the exception to
513 // the instrumentation. To prevent from reporting it a second time, we simply pass a
514 // null Instrumentation*.
Sebastien Hertz270a0e12015-01-16 19:49:09 +0100515 const instrumentation::Instrumentation* const instrumentation =
Sebastien Hertz520633b2015-09-08 17:03:36 +0200516 first ? nullptr : Runtime::Current()->GetInstrumentation();
Sebastien Hertz270a0e12015-01-16 19:49:09 +0100517 uint32_t found_dex_pc = FindNextInstructionFollowingException(self, *shadow_frame, dex_pc,
518 instrumentation);
519 new_dex_pc = found_dex_pc; // the dex pc of a matching catch handler
520 // or DexFile::kDexNoIndex if there is none.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100521 } else if (!from_code) {
522 // For the debugger and full deoptimization stack, we must go past the invoke
523 // instruction, as it already executed.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700524 // TODO: should be tested more once b/17586779 is fixed.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100525 const Instruction* instr = Instruction::At(&code_item->insns_[dex_pc]);
Mingyao Yang504a6902016-04-28 16:23:01 -0700526 if (instr->IsInvoke()) {
Mingyao Yangffedec52016-05-19 10:48:40 -0700527 if (IsStringInit(instr, shadow_frame->GetMethod())) {
528 uint16_t this_obj_vreg = GetReceiverRegisterForStringInit(instr);
529 // Move the StringFactory.newStringFromChars() result into the register representing
530 // "this object" when invoking the string constructor in the original dex instruction.
531 // Also move the result into all aliases.
532 DCHECK(value.GetL()->IsString());
533 SetStringInitValueToAllAliases(shadow_frame, this_obj_vreg, value);
534 // Calling string constructor in the original dex code doesn't generate a result value.
535 value.SetJ(0);
536 }
Mingyao Yang504a6902016-04-28 16:23:01 -0700537 new_dex_pc = dex_pc + instr->SizeInCodeUnits();
538 } else if (instr->Opcode() == Instruction::NEW_INSTANCE) {
539 // It's possible to deoptimize at a NEW_INSTANCE dex instruciton that's for a
540 // java string, which is turned into a call into StringFactory.newEmptyString();
Mingyao Yangffedec52016-05-19 10:48:40 -0700541 // Move the StringFactory.newEmptyString() result into the destination register.
542 DCHECK(value.GetL()->IsString());
543 shadow_frame->SetVRegReference(instr->VRegA_21c(), value.GetL());
544 // new-instance doesn't generate a result value.
545 value.SetJ(0);
546 // Skip the dex instruction since we essentially come back from an invocation.
547 new_dex_pc = dex_pc + instr->SizeInCodeUnits();
Mingyao Yang504a6902016-04-28 16:23:01 -0700548 if (kIsDebugBuild) {
549 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mingyao Yangffedec52016-05-19 10:48:40 -0700550 // This is a suspend point. But it's ok since value has been set into shadow_frame.
Mathieu Chartieref41db72016-10-25 15:08:01 -0700551 ObjPtr<mirror::Class> klass = class_linker->ResolveType(
Andreas Gampea5b09a62016-11-17 15:21:22 -0800552 dex::TypeIndex(instr->VRegB_21c()), shadow_frame->GetMethod());
Mingyao Yang504a6902016-04-28 16:23:01 -0700553 DCHECK(klass->IsStringClass());
554 }
Mingyao Yang504a6902016-04-28 16:23:01 -0700555 } else {
Mingyao Yangffedec52016-05-19 10:48:40 -0700556 CHECK(false) << "Unexpected instruction opcode " << instr->Opcode()
557 << " at dex_pc " << dex_pc
David Sehr709b0702016-10-13 09:12:37 -0700558 << " of method: " << ArtMethod::PrettyMethod(shadow_frame->GetMethod(), false);
Mingyao Yang504a6902016-04-28 16:23:01 -0700559 }
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100560 } else {
561 // Nothing to do, the dex_pc is the one at which the code requested
562 // the deoptimization.
Sebastien Hertz270a0e12015-01-16 19:49:09 +0100563 }
564 if (new_dex_pc != DexFile::kDexNoIndex) {
565 shadow_frame->SetDexPC(new_dex_pc);
566 value = Execute(self, code_item, *shadow_frame, value);
567 }
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800568 ShadowFrame* old_frame = shadow_frame;
569 shadow_frame = shadow_frame->GetLink();
Christopher Ferris241a9582015-04-27 15:19:41 -0700570 ShadowFrame::DeleteDeoptimizedFrame(old_frame);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100571 // Following deoptimizations of shadow frames must pass the invoke instruction.
572 from_code = false;
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