blob: 0d3af93fd13ca26cb80c29345ff7a20a19638914 [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
Ian Rogersfc0e94b2013-09-23 23:51:32 -070038static void InterpreterJni(Thread* self, ArtMethod* method, const StringPiece& shorty,
Jeff Hao5d917302013-02-27 17:57:33 -080039 Object* receiver, uint32_t* args, JValue* result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070040 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers64b6d142012-10-29 16:34:15 -070041 // TODO: The following enters JNI code using a typedef-ed function rather than the JNI compiler,
42 // it should be removed and JNI compiled stubs used instead.
43 ScopedObjectAccessUnchecked soa(self);
44 if (method->IsStatic()) {
45 if (shorty == "L") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +010046 typedef jobject (fntype)(JNIEnv*, jclass);
Mathieu Chartier2d721012014-11-10 11:08:06 -080047 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -070048 ScopedLocalRef<jclass> klass(soa.Env(),
49 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
Ian Rogers556d6372012-11-20 12:19:36 -080050 jobject jresult;
51 {
52 ScopedThreadStateChange tsc(self, kNative);
53 jresult = fn(soa.Env(), klass.get());
54 }
Mathieu Chartier1cc62e42016-10-03 18:01:28 -070055 result->SetL(soa.Decode<Object>(jresult).Ptr());
Ian Rogers64b6d142012-10-29 16:34:15 -070056 } else if (shorty == "V") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +010057 typedef void (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()));
61 ScopedThreadStateChange tsc(self, kNative);
62 fn(soa.Env(), klass.get());
63 } else if (shorty == "Z") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +010064 typedef jboolean (fntype)(JNIEnv*, jclass);
Mathieu Chartier2d721012014-11-10 11:08:06 -080065 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -070066 ScopedLocalRef<jclass> klass(soa.Env(),
67 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
68 ScopedThreadStateChange tsc(self, kNative);
69 result->SetZ(fn(soa.Env(), klass.get()));
70 } else if (shorty == "BI") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +010071 typedef jbyte (fntype)(JNIEnv*, jclass, jint);
Mathieu Chartier2d721012014-11-10 11:08:06 -080072 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -070073 ScopedLocalRef<jclass> klass(soa.Env(),
74 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
75 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -080076 result->SetB(fn(soa.Env(), klass.get(), args[0]));
Ian Rogers64b6d142012-10-29 16:34:15 -070077 } else if (shorty == "II") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +010078 typedef jint (fntype)(JNIEnv*, jclass, jint);
Mathieu Chartier2d721012014-11-10 11:08:06 -080079 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -070080 ScopedLocalRef<jclass> klass(soa.Env(),
81 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
82 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -080083 result->SetI(fn(soa.Env(), klass.get(), args[0]));
Ian Rogers64b6d142012-10-29 16:34:15 -070084 } else if (shorty == "LL") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +010085 typedef jobject (fntype)(JNIEnv*, jclass, jobject);
Mathieu Chartier2d721012014-11-10 11:08:06 -080086 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -070087 ScopedLocalRef<jclass> klass(soa.Env(),
88 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
89 ScopedLocalRef<jobject> arg0(soa.Env(),
Mathieu Chartier2cebb242015-04-21 16:50:40 -070090 soa.AddLocalReference<jobject>(
91 reinterpret_cast<Object*>(args[0])));
Ian Rogers556d6372012-11-20 12:19:36 -080092 jobject jresult;
93 {
94 ScopedThreadStateChange tsc(self, kNative);
95 jresult = fn(soa.Env(), klass.get(), arg0.get());
96 }
Mathieu Chartier1cc62e42016-10-03 18:01:28 -070097 result->SetL(soa.Decode<Object>(jresult).Ptr());
Ian Rogers64b6d142012-10-29 16:34:15 -070098 } else if (shorty == "IIZ") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +010099 typedef jint (fntype)(JNIEnv*, jclass, jint, jboolean);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800100 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700101 ScopedLocalRef<jclass> klass(soa.Env(),
102 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
103 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800104 result->SetI(fn(soa.Env(), klass.get(), args[0], args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700105 } else if (shorty == "ILI") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100106 typedef jint (fntype)(JNIEnv*, jclass, jobject, jint);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800107 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(
108 method->GetEntryPointFromJni()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700109 ScopedLocalRef<jclass> klass(soa.Env(),
110 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
111 ScopedLocalRef<jobject> arg0(soa.Env(),
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700112 soa.AddLocalReference<jobject>(
113 reinterpret_cast<Object*>(args[0])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700114 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800115 result->SetI(fn(soa.Env(), klass.get(), arg0.get(), args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700116 } else if (shorty == "SIZ") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100117 typedef jshort (fntype)(JNIEnv*, jclass, jint, jboolean);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700118 fntype* const fn =
119 reinterpret_cast<fntype*>(const_cast<void*>(method->GetEntryPointFromJni()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700120 ScopedLocalRef<jclass> klass(soa.Env(),
121 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
122 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800123 result->SetS(fn(soa.Env(), klass.get(), args[0], args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700124 } else if (shorty == "VIZ") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100125 typedef void (fntype)(JNIEnv*, jclass, jint, jboolean);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800126 fntype* const fn = reinterpret_cast<fntype*>(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 fn(soa.Env(), klass.get(), args[0], args[1]);
Ian Rogers64b6d142012-10-29 16:34:15 -0700131 } else if (shorty == "ZLL") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100132 typedef jboolean (fntype)(JNIEnv*, jclass, jobject, jobject);
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 ScopedLocalRef<jobject> arg0(soa.Env(),
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700137 soa.AddLocalReference<jobject>(
138 reinterpret_cast<Object*>(args[0])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700139 ScopedLocalRef<jobject> arg1(soa.Env(),
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700140 soa.AddLocalReference<jobject>(
141 reinterpret_cast<Object*>(args[1])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700142 ScopedThreadStateChange tsc(self, kNative);
143 result->SetZ(fn(soa.Env(), klass.get(), arg0.get(), arg1.get()));
144 } else if (shorty == "ZILL") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100145 typedef jboolean (fntype)(JNIEnv*, jclass, jint, jobject, jobject);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800146 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700147 ScopedLocalRef<jclass> klass(soa.Env(),
148 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
149 ScopedLocalRef<jobject> arg1(soa.Env(),
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700150 soa.AddLocalReference<jobject>(
151 reinterpret_cast<Object*>(args[1])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700152 ScopedLocalRef<jobject> arg2(soa.Env(),
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700153 soa.AddLocalReference<jobject>(
154 reinterpret_cast<Object*>(args[2])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700155 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800156 result->SetZ(fn(soa.Env(), klass.get(), args[0], arg1.get(), arg2.get()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700157 } else if (shorty == "VILII") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100158 typedef void (fntype)(JNIEnv*, jclass, jint, jobject, jint, jint);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800159 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700160 ScopedLocalRef<jclass> klass(soa.Env(),
161 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
162 ScopedLocalRef<jobject> arg1(soa.Env(),
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700163 soa.AddLocalReference<jobject>(
164 reinterpret_cast<Object*>(args[1])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700165 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800166 fn(soa.Env(), klass.get(), args[0], arg1.get(), args[2], args[3]);
Ian Rogers64b6d142012-10-29 16:34:15 -0700167 } else if (shorty == "VLILII") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100168 typedef void (fntype)(JNIEnv*, jclass, jobject, jint, jobject, jint, jint);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800169 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700170 ScopedLocalRef<jclass> klass(soa.Env(),
171 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
172 ScopedLocalRef<jobject> arg0(soa.Env(),
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700173 soa.AddLocalReference<jobject>(
174 reinterpret_cast<Object*>(args[0])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700175 ScopedLocalRef<jobject> arg2(soa.Env(),
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700176 soa.AddLocalReference<jobject>(
177 reinterpret_cast<Object*>(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 {
181 LOG(FATAL) << "Do something with static native method: " << PrettyMethod(method)
182 << " 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 Chartier1cc62e42016-10-03 18:01:28 -0700195 result->SetL(soa.Decode<Object>(jresult).Ptr());
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 Chartier2cebb242015-04-21 16:50:40 -0700209 soa.AddLocalReference<jobject>(
210 reinterpret_cast<Object*>(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 Chartier1cc62e42016-10-03 18:01:28 -0700216 result->SetL(soa.Decode<Object>(jresult).Ptr());
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 {
226 LOG(FATAL) << "Do something with native method: " << PrettyMethod(method)
227 << " 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};
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800236static std::ostream& operator<<(std::ostream& os, const InterpreterImplKind& rhs) {
buzbee1452bee2015-03-06 14:43:04 -0800237 os << ((rhs == kSwitchImplKind)
238 ? "Switch-based interpreter"
buzbeef61df9b2016-09-07 07:12:29 -0700239 : "Asm interpreter");
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700240 return os;
241}
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700242
buzbee1452bee2015-03-06 14:43:04 -0800243static constexpr InterpreterImplKind kInterpreterImplKind = kMterpImplKind;
Alexey Frunze00b53b72016-02-02 20:25:45 -0800244
Aart Bik01223202016-05-05 15:10:42 -0700245static inline JValue Execute(
246 Thread* self,
247 const DexFile::CodeItem* code_item,
248 ShadowFrame& shadow_frame,
249 JValue result_register,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700250 bool stay_in_interpreter = false) REQUIRES_SHARED(Locks::mutator_lock_) {
buzbee1452bee2015-03-06 14:43:04 -0800251 DCHECK(!shadow_frame.GetMethod()->IsAbstract());
Ian Rogers848871b2013-08-05 10:56:33 -0700252 DCHECK(!shadow_frame.GetMethod()->IsNative());
buzbee734f3aa2016-01-28 14:20:06 -0800253 if (LIKELY(shadow_frame.GetDexPC() == 0)) { // Entering the method, but not via deoptimization.
254 if (kIsDebugBuild) {
255 self->AssertNoPendingException();
256 }
257 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
258 ArtMethod *method = shadow_frame.GetMethod();
259
260 if (UNLIKELY(instrumentation->HasMethodEntryListeners())) {
261 instrumentation->MethodEnterEvent(self, shadow_frame.GetThisObject(code_item->ins_size_),
262 method, 0);
263 }
264
Aart Bik01223202016-05-05 15:10:42 -0700265 if (!stay_in_interpreter) {
266 jit::Jit* jit = Runtime::Current()->GetJit();
267 if (jit != nullptr) {
268 jit->MethodEntered(self, shadow_frame.GetMethod());
269 if (jit->CanInvokeCompiledCode(method)) {
270 JValue result;
buzbee734f3aa2016-01-28 14:20:06 -0800271
Aart Bik01223202016-05-05 15:10:42 -0700272 // Pop the shadow frame before calling into compiled code.
273 self->PopShadowFrame();
274 ArtInterpreterToCompiledCodeBridge(self, nullptr, code_item, &shadow_frame, &result);
275 // Push the shadow frame back as the caller will expect it.
276 self->PushShadowFrame(&shadow_frame);
buzbee734f3aa2016-01-28 14:20:06 -0800277
Aart Bik01223202016-05-05 15:10:42 -0700278 return result;
279 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100280 }
buzbee734f3aa2016-01-28 14:20:06 -0800281 }
282 }
283
Sebastien Hertz4e99b3d2014-06-24 14:35:40 +0200284 shadow_frame.GetMethod()->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200285
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700286 // Lock counting is a special version of accessibility checks, and for simplicity and
287 // reduction of template parameters, we gate it behind access-checks mode.
288 ArtMethod* method = shadow_frame.GetMethod();
289 DCHECK(!method->SkipAccessChecks() || !method->MustCountLocks());
290
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100291 bool transaction_active = Runtime::Current()->IsActiveTransaction();
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700292 if (LIKELY(method->SkipAccessChecks())) {
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200293 // Enter the "without access check" interpreter.
buzbee1452bee2015-03-06 14:43:04 -0800294 if (kInterpreterImplKind == kMterpImplKind) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100295 if (transaction_active) {
buzbee1452bee2015-03-06 14:43:04 -0800296 // No Mterp variant - just use the switch interpreter.
297 return ExecuteSwitchImpl<false, true>(self, code_item, shadow_frame, result_register,
298 false);
Bill Buzbeefd522f92016-02-11 22:37:42 +0000299 } else if (UNLIKELY(!Runtime::Current()->IsStarted())) {
300 return ExecuteSwitchImpl<false, false>(self, code_item, shadow_frame, result_register,
301 false);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100302 } else {
buzbee1452bee2015-03-06 14:43:04 -0800303 while (true) {
Bill Buzbeefd522f92016-02-11 22:37:42 +0000304 // Mterp does not support all instrumentation/debugging.
Andreas Gampe67409972016-07-19 22:34:53 -0700305 if (MterpShouldSwitchInterpreters() != 0) {
buzbee1452bee2015-03-06 14:43:04 -0800306 return ExecuteSwitchImpl<false, false>(self, code_item, shadow_frame, result_register,
307 false);
buzbee1452bee2015-03-06 14:43:04 -0800308 }
309 bool returned = ExecuteMterpImpl(self, code_item, &shadow_frame, &result_register);
310 if (returned) {
311 return result_register;
312 } else {
313 // Mterp didn't like that instruction. Single-step it with the reference interpreter.
buzbeed6b48db2016-01-28 15:48:55 -0800314 result_register = ExecuteSwitchImpl<false, false>(self, code_item, shadow_frame,
buzbee1452bee2015-03-06 14:43:04 -0800315 result_register, true);
316 if (shadow_frame.GetDexPC() == DexFile::kDexNoIndex) {
317 // Single-stepped a return or an exception not handled locally. Return to caller.
buzbeed6b48db2016-01-28 15:48:55 -0800318 return result_register;
buzbee1452bee2015-03-06 14:43:04 -0800319 }
320 }
321 }
322 }
buzbeef61df9b2016-09-07 07:12:29 -0700323 } else {
324 DCHECK_EQ(kInterpreterImplKind, kSwitchImplKind);
buzbee1452bee2015-03-06 14:43:04 -0800325 if (transaction_active) {
326 return ExecuteSwitchImpl<false, true>(self, code_item, shadow_frame, result_register,
327 false);
328 } else {
329 return ExecuteSwitchImpl<false, false>(self, code_item, shadow_frame, result_register,
330 false);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100331 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200332 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200333 } else {
334 // Enter the "with access check" interpreter.
buzbee1452bee2015-03-06 14:43:04 -0800335 if (kInterpreterImplKind == kMterpImplKind) {
336 // No access check variants for Mterp. Just use the switch version.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100337 if (transaction_active) {
buzbee1452bee2015-03-06 14:43:04 -0800338 return ExecuteSwitchImpl<true, true>(self, code_item, shadow_frame, result_register,
339 false);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100340 } else {
buzbee1452bee2015-03-06 14:43:04 -0800341 return ExecuteSwitchImpl<true, false>(self, code_item, shadow_frame, result_register,
342 false);
343 }
buzbeef61df9b2016-09-07 07:12:29 -0700344 } else {
345 DCHECK_EQ(kInterpreterImplKind, kSwitchImplKind);
buzbee1452bee2015-03-06 14:43:04 -0800346 if (transaction_active) {
347 return ExecuteSwitchImpl<true, true>(self, code_item, shadow_frame, result_register,
348 false);
349 } else {
350 return ExecuteSwitchImpl<true, false>(self, code_item, shadow_frame, result_register,
351 false);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100352 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200353 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200354 }
355}
356
Brian Carlstromea46f952013-07-30 01:26:50 -0700357void EnterInterpreterFromInvoke(Thread* self, ArtMethod* method, Object* receiver,
Aart Bik01223202016-05-05 15:10:42 -0700358 uint32_t* args, JValue* result,
359 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
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700367 const char* old_cause = self->StartAssertNoThreadSuspension("EnterInterpreterFromInvoke");
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700368 const DexFile::CodeItem* code_item = method->GetCodeItem();
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700369 uint16_t num_regs;
370 uint16_t num_ins;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700371 if (code_item != nullptr) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700372 num_regs = code_item->registers_size_;
373 num_ins = code_item->ins_size_;
Alex Light9139e002015-10-09 15:59:48 -0700374 } else if (!method->IsInvokable()) {
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700375 self->EndAssertNoThreadSuspension(old_cause);
Alex Light9139e002015-10-09 15:59:48 -0700376 method->ThrowInvocationTimeError();
jeffhao0a9bb732012-11-26 12:28:49 -0800377 return;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700378 } else {
379 DCHECK(method->IsNative());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700380 num_regs = num_ins = ArtMethod::NumArgRegisters(method->GetShorty());
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700381 if (!method->IsStatic()) {
382 num_regs++;
383 num_ins++;
384 }
385 }
386 // Set up shadow frame with matching number of reference slots to vregs.
387 ShadowFrame* last_shadow_frame = self->GetManagedStack()->GetTopShadowFrame();
Andreas Gampeb3025922015-09-01 14:45:00 -0700388 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
Andreas Gampe03ec9302015-08-27 17:41:47 -0700389 CREATE_SHADOW_FRAME(num_regs, last_shadow_frame, method, /* dex pc */ 0);
Andreas Gampeb3025922015-09-01 14:45:00 -0700390 ShadowFrame* shadow_frame = shadow_frame_unique_ptr.get();
Jeff Hao66135192013-05-14 11:02:41 -0700391 self->PushShadowFrame(shadow_frame);
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700392
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700393 size_t cur_reg = num_regs - num_ins;
394 if (!method->IsStatic()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700395 CHECK(receiver != nullptr);
TDYa127ce4cc0d2012-11-18 16:59:53 -0800396 shadow_frame->SetVRegReference(cur_reg, receiver);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700397 ++cur_reg;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700398 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700399 uint32_t shorty_len = 0;
400 const char* shorty = method->GetShorty(&shorty_len);
Jeff Hao5d917302013-02-27 17:57:33 -0800401 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 -0700402 DCHECK_LT(shorty_pos + 1, shorty_len);
Jeff Hao5d917302013-02-27 17:57:33 -0800403 switch (shorty[shorty_pos + 1]) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700404 case 'L': {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800405 Object* o = reinterpret_cast<StackReference<Object>*>(&args[arg_pos])->AsMirrorPtr();
TDYa127ce4cc0d2012-11-18 16:59:53 -0800406 shadow_frame->SetVRegReference(cur_reg, o);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700407 break;
408 }
Jeff Hao5d917302013-02-27 17:57:33 -0800409 case 'J': case 'D': {
410 uint64_t wide_value = (static_cast<uint64_t>(args[arg_pos + 1]) << 32) | args[arg_pos];
411 shadow_frame->SetVRegLong(cur_reg, wide_value);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700412 cur_reg++;
Jeff Hao5d917302013-02-27 17:57:33 -0800413 arg_pos++;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700414 break;
Jeff Hao5d917302013-02-27 17:57:33 -0800415 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700416 default:
Jeff Hao5d917302013-02-27 17:57:33 -0800417 shadow_frame->SetVReg(cur_reg, args[arg_pos]);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700418 break;
419 }
420 }
Mathieu Chartier92246bb2014-02-25 18:22:39 -0800421 self->EndAssertNoThreadSuspension(old_cause);
422 // Do this after populating the shadow frame in case EnsureInitialized causes a GC.
Ian Rogers6c5cb212014-06-18 16:07:20 -0700423 if (method->IsStatic() && UNLIKELY(!method->GetDeclaringClass()->IsInitialized())) {
Mathieu Chartier92246bb2014-02-25 18:22:39 -0800424 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700425 StackHandleScope<1> hs(self);
426 Handle<mirror::Class> h_class(hs.NewHandle(method->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700427 if (UNLIKELY(!class_linker->EnsureInitialized(self, h_class, true, true))) {
Mathieu Chartier92246bb2014-02-25 18:22:39 -0800428 CHECK(self->IsExceptionPending());
429 self->PopShadowFrame();
430 return;
431 }
432 }
Ian Rogers64b6d142012-10-29 16:34:15 -0700433 if (LIKELY(!method->IsNative())) {
Aart Bik01223202016-05-05 15:10:42 -0700434 JValue r = Execute(self, code_item, *shadow_frame, JValue(), stay_in_interpreter);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700435 if (result != nullptr) {
Jeff Hao6474d192013-03-26 14:08:09 -0700436 *result = r;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700437 }
438 } else {
Ian Rogers64b6d142012-10-29 16:34:15 -0700439 // We don't expect to be asked to interpret native code (which is entered via a JNI compiler
440 // generated stub) except during testing and image writing.
Mathieu Chartier92246bb2014-02-25 18:22:39 -0800441 // Update args to be the args in the shadow frame since the input ones could hold stale
442 // references pointers due to moving GC.
443 args = shadow_frame->GetVRegArgs(method->IsStatic() ? 0 : 1);
Ian Rogers64b6d142012-10-29 16:34:15 -0700444 if (!Runtime::Current()->IsStarted()) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700445 UnstartedRuntime::Jni(self, method, receiver, args, result);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700446 } else {
Jeff Hao6474d192013-03-26 14:08:09 -0700447 InterpreterJni(self, method, shorty, receiver, args, result);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700448 }
449 }
450 self->PopShadowFrame();
451}
452
Mingyao Yangffedec52016-05-19 10:48:40 -0700453static bool IsStringInit(const Instruction* instr, ArtMethod* caller)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700454 REQUIRES_SHARED(Locks::mutator_lock_) {
Mingyao Yangffedec52016-05-19 10:48:40 -0700455 if (instr->Opcode() == Instruction::INVOKE_DIRECT ||
456 instr->Opcode() == Instruction::INVOKE_DIRECT_RANGE) {
457 // Instead of calling ResolveMethod() which has suspend point and can trigger
458 // GC, look up the callee method symbolically.
459 uint16_t callee_method_idx = (instr->Opcode() == Instruction::INVOKE_DIRECT_RANGE) ?
460 instr->VRegB_3rc() : instr->VRegB_35c();
461 const DexFile* dex_file = caller->GetDexFile();
462 const DexFile::MethodId& method_id = dex_file->GetMethodId(callee_method_idx);
463 const char* class_name = dex_file->StringByTypeIdx(method_id.class_idx_);
464 const char* method_name = dex_file->GetMethodName(method_id);
465 // Compare method's class name and method name against string init.
466 // It's ok since it's not allowed to create your own java/lang/String.
467 // TODO: verify that assumption.
468 if ((strcmp(class_name, "Ljava/lang/String;") == 0) &&
469 (strcmp(method_name, "<init>") == 0)) {
470 return true;
471 }
472 }
473 return false;
474}
475
476static int16_t GetReceiverRegisterForStringInit(const Instruction* instr) {
477 DCHECK(instr->Opcode() == Instruction::INVOKE_DIRECT_RANGE ||
478 instr->Opcode() == Instruction::INVOKE_DIRECT);
479 return (instr->Opcode() == Instruction::INVOKE_DIRECT_RANGE) ?
480 instr->VRegC_3rc() : instr->VRegC_35c();
481}
482
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100483void EnterInterpreterFromDeoptimize(Thread* self,
484 ShadowFrame* shadow_frame,
485 bool from_code,
486 JValue* ret_val)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700487 REQUIRES_SHARED(Locks::mutator_lock_) {
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800488 JValue value;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700489 // Set value to last known result in case the shadow frame chain is empty.
490 value.SetJ(ret_val->GetJ());
Sebastien Hertz520633b2015-09-08 17:03:36 +0200491 // Are we executing the first shadow frame?
492 bool first = true;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700493 while (shadow_frame != nullptr) {
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700494 // We do not want to recover lock state for lock counting when deoptimizing. Currently,
495 // the compiler should not have compiled a method that failed structured-locking checks.
496 DCHECK(!shadow_frame->GetMethod()->MustCountLocks());
497
Ian Rogers62d6c772013-02-27 08:32:07 -0800498 self->SetTopOfShadowStack(shadow_frame);
Ian Rogerse94652f2014-12-02 11:13:19 -0800499 const DexFile::CodeItem* code_item = shadow_frame->GetMethod()->GetCodeItem();
Sebastien Hertz270a0e12015-01-16 19:49:09 +0100500 const uint32_t dex_pc = shadow_frame->GetDexPC();
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100501 uint32_t new_dex_pc = dex_pc;
Sebastien Hertz270a0e12015-01-16 19:49:09 +0100502 if (UNLIKELY(self->IsExceptionPending())) {
Sebastien Hertz520633b2015-09-08 17:03:36 +0200503 // If we deoptimize from the QuickExceptionHandler, we already reported the exception to
504 // the instrumentation. To prevent from reporting it a second time, we simply pass a
505 // null Instrumentation*.
Sebastien Hertz270a0e12015-01-16 19:49:09 +0100506 const instrumentation::Instrumentation* const instrumentation =
Sebastien Hertz520633b2015-09-08 17:03:36 +0200507 first ? nullptr : Runtime::Current()->GetInstrumentation();
Sebastien Hertz270a0e12015-01-16 19:49:09 +0100508 uint32_t found_dex_pc = FindNextInstructionFollowingException(self, *shadow_frame, dex_pc,
509 instrumentation);
510 new_dex_pc = found_dex_pc; // the dex pc of a matching catch handler
511 // or DexFile::kDexNoIndex if there is none.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100512 } else if (!from_code) {
513 // For the debugger and full deoptimization stack, we must go past the invoke
514 // instruction, as it already executed.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700515 // TODO: should be tested more once b/17586779 is fixed.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100516 const Instruction* instr = Instruction::At(&code_item->insns_[dex_pc]);
Mingyao Yang504a6902016-04-28 16:23:01 -0700517 if (instr->IsInvoke()) {
Mingyao Yangffedec52016-05-19 10:48:40 -0700518 if (IsStringInit(instr, shadow_frame->GetMethod())) {
519 uint16_t this_obj_vreg = GetReceiverRegisterForStringInit(instr);
520 // Move the StringFactory.newStringFromChars() result into the register representing
521 // "this object" when invoking the string constructor in the original dex instruction.
522 // Also move the result into all aliases.
523 DCHECK(value.GetL()->IsString());
524 SetStringInitValueToAllAliases(shadow_frame, this_obj_vreg, value);
525 // Calling string constructor in the original dex code doesn't generate a result value.
526 value.SetJ(0);
527 }
Mingyao Yang504a6902016-04-28 16:23:01 -0700528 new_dex_pc = dex_pc + instr->SizeInCodeUnits();
529 } else if (instr->Opcode() == Instruction::NEW_INSTANCE) {
530 // It's possible to deoptimize at a NEW_INSTANCE dex instruciton that's for a
531 // java string, which is turned into a call into StringFactory.newEmptyString();
Mingyao Yangffedec52016-05-19 10:48:40 -0700532 // Move the StringFactory.newEmptyString() result into the destination register.
533 DCHECK(value.GetL()->IsString());
534 shadow_frame->SetVRegReference(instr->VRegA_21c(), value.GetL());
535 // new-instance doesn't generate a result value.
536 value.SetJ(0);
537 // Skip the dex instruction since we essentially come back from an invocation.
538 new_dex_pc = dex_pc + instr->SizeInCodeUnits();
Mingyao Yang504a6902016-04-28 16:23:01 -0700539 if (kIsDebugBuild) {
540 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mingyao Yangffedec52016-05-19 10:48:40 -0700541 // This is a suspend point. But it's ok since value has been set into shadow_frame.
Mingyao Yang504a6902016-04-28 16:23:01 -0700542 mirror::Class* klass = class_linker->ResolveType(
543 instr->VRegB_21c(), shadow_frame->GetMethod());
544 DCHECK(klass->IsStringClass());
545 }
Mingyao Yang504a6902016-04-28 16:23:01 -0700546 } else {
Mingyao Yangffedec52016-05-19 10:48:40 -0700547 CHECK(false) << "Unexpected instruction opcode " << instr->Opcode()
548 << " at dex_pc " << dex_pc
549 << " of method: " << PrettyMethod(shadow_frame->GetMethod(), false);
Mingyao Yang504a6902016-04-28 16:23:01 -0700550 }
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100551 } else {
552 // Nothing to do, the dex_pc is the one at which the code requested
553 // the deoptimization.
Sebastien Hertz270a0e12015-01-16 19:49:09 +0100554 }
555 if (new_dex_pc != DexFile::kDexNoIndex) {
556 shadow_frame->SetDexPC(new_dex_pc);
557 value = Execute(self, code_item, *shadow_frame, value);
558 }
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800559 ShadowFrame* old_frame = shadow_frame;
560 shadow_frame = shadow_frame->GetLink();
Christopher Ferris241a9582015-04-27 15:19:41 -0700561 ShadowFrame::DeleteDeoptimizedFrame(old_frame);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100562 // Following deoptimizations of shadow frames must pass the invoke instruction.
563 from_code = false;
Sebastien Hertz520633b2015-09-08 17:03:36 +0200564 first = false;
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800565 }
566 ret_val->SetJ(value.GetJ());
567}
568
Ian Rogerse94652f2014-12-02 11:13:19 -0800569JValue EnterInterpreterFromEntryPoint(Thread* self, const DexFile::CodeItem* code_item,
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700570 ShadowFrame* shadow_frame) {
Ian Rogersf3e98552013-03-20 15:49:49 -0700571 DCHECK_EQ(self, Thread::Current());
Nicolas Geoffray535a3fb2014-07-22 15:17:38 +0100572 bool implicit_check = !Runtime::Current()->ExplicitStackOverflowChecks();
573 if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEndForInterpreter(implicit_check))) {
Ian Rogersf3e98552013-03-20 15:49:49 -0700574 ThrowStackOverflowError(self);
575 return JValue();
576 }
577
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100578 jit::Jit* jit = Runtime::Current()->GetJit();
579 if (jit != nullptr) {
580 jit->NotifyCompiledCodeToInterpreterTransition(self, shadow_frame->GetMethod());
581 }
Ian Rogerse94652f2014-12-02 11:13:19 -0800582 return Execute(self, code_item, *shadow_frame, JValue());
Ian Rogers7db619b2013-01-16 18:35:48 -0800583}
584
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700585void ArtInterpreterToInterpreterBridge(Thread* self, const DexFile::CodeItem* code_item,
586 ShadowFrame* shadow_frame, JValue* result) {
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))) {
Jeff Hao16743632013-05-08 10:59:04 -0700589 ThrowStackOverflowError(self);
Jeff Hao69510672013-05-21 17:34:55 -0700590 return;
Jeff Hao16743632013-05-08 10:59:04 -0700591 }
592
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700593 self->PushShadowFrame(shadow_frame);
Alex Lighteb7c1442015-08-31 13:17:42 -0700594 ArtMethod* method = shadow_frame->GetMethod();
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200595 // Ensure static methods are initialized.
Alex Lighteb7c1442015-08-31 13:17:42 -0700596 const bool is_static = method->IsStatic();
Ian Rogerse94652f2014-12-02 11:13:19 -0800597 if (is_static) {
Alex Lighteb7c1442015-08-31 13:17:42 -0700598 mirror::Class* declaring_class = method->GetDeclaringClass();
Ian Rogers6c5cb212014-06-18 16:07:20 -0700599 if (UNLIKELY(!declaring_class->IsInitialized())) {
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700600 StackHandleScope<1> hs(self);
601 HandleWrapper<Class> h_declaring_class(hs.NewHandleWrapper(&declaring_class));
602 if (UNLIKELY(!Runtime::Current()->GetClassLinker()->EnsureInitialized(
Ian Rogers7b078e82014-09-10 14:44:24 -0700603 self, h_declaring_class, true, true))) {
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700604 DCHECK(self->IsExceptionPending());
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700605 self->PopShadowFrame();
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200606 return;
607 }
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700608 CHECK(h_declaring_class->IsInitializing());
Jeff Hao16743632013-05-08 10:59:04 -0700609 }
Jeff Hao16743632013-05-08 10:59:04 -0700610 }
Jeff Hao16743632013-05-08 10:59:04 -0700611
Ian Rogerse94652f2014-12-02 11:13:19 -0800612 if (LIKELY(!shadow_frame->GetMethod()->IsNative())) {
613 result->SetJ(Execute(self, code_item, *shadow_frame, JValue()).GetJ());
Jeff Hao16743632013-05-08 10:59:04 -0700614 } else {
615 // We don't expect to be asked to interpret native code (which is entered via a JNI compiler
616 // generated stub) except during testing and image writing.
617 CHECK(!Runtime::Current()->IsStarted());
Ian Rogerse94652f2014-12-02 11:13:19 -0800618 Object* receiver = is_static ? nullptr : shadow_frame->GetVRegReference(0);
619 uint32_t* args = shadow_frame->GetVRegArgs(is_static ? 0 : 1);
Andreas Gampe799681b2015-05-15 19:24:12 -0700620 UnstartedRuntime::Jni(self, shadow_frame->GetMethod(), receiver, args, result);
Jeff Hao16743632013-05-08 10:59:04 -0700621 }
622
623 self->PopShadowFrame();
Jeff Hao16743632013-05-08 10:59:04 -0700624}
625
buzbee1452bee2015-03-06 14:43:04 -0800626void CheckInterpreterAsmConstants() {
627 CheckMterpAsmConstants();
628}
629
630void InitInterpreterTls(Thread* self) {
631 InitMterpTls(self);
632}
633
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700634} // namespace interpreter
635} // namespace art