blob: 101c9a143892c8f644fe89bfa3e8935da048ce24 [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_goto_table_impl.h"
24#include "interpreter_mterp_impl.h"
25#include "interpreter_switch_impl.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070026#include "mirror/string-inl.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070027#include "scoped_thread_state_change.h"
28#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)
Mathieu Chartier90443472015-07-16 20:32:27 -070040 SHARED_REQUIRES(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 }
55 result->SetL(soa.Decode<Object*>(jresult));
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 }
97 result->SetL(soa.Decode<Object*>(jresult));
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 }
195 result->SetL(soa.Decode<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 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 }
216 result->SetL(soa.Decode<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 {
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.
234 kComputedGotoImplKind, // Computed-goto-based interpreter implementation.
235 kMterpImplKind // Assembly interpreter
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200236};
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800237static std::ostream& operator<<(std::ostream& os, const InterpreterImplKind& rhs) {
buzbee1452bee2015-03-06 14:43:04 -0800238 os << ((rhs == kSwitchImplKind)
239 ? "Switch-based interpreter"
240 : (rhs == kComputedGotoImplKind)
241 ? "Computed-goto-based interpreter"
242 : "Asm interpreter");
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700243 return os;
244}
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700245
buzbee1452bee2015-03-06 14:43:04 -0800246static constexpr InterpreterImplKind kInterpreterImplKind = kMterpImplKind;
Alexey Frunze00b53b72016-02-02 20:25:45 -0800247
Aart Bik01223202016-05-05 15:10:42 -0700248static inline JValue Execute(
249 Thread* self,
250 const DexFile::CodeItem* code_item,
251 ShadowFrame& shadow_frame,
252 JValue result_register,
253 bool stay_in_interpreter = false) SHARED_REQUIRES(Locks::mutator_lock_) {
buzbee1452bee2015-03-06 14:43:04 -0800254 DCHECK(!shadow_frame.GetMethod()->IsAbstract());
Ian Rogers848871b2013-08-05 10:56:33 -0700255 DCHECK(!shadow_frame.GetMethod()->IsNative());
buzbee734f3aa2016-01-28 14:20:06 -0800256 if (LIKELY(shadow_frame.GetDexPC() == 0)) { // Entering the method, but not via deoptimization.
257 if (kIsDebugBuild) {
258 self->AssertNoPendingException();
259 }
260 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
261 ArtMethod *method = shadow_frame.GetMethod();
262
263 if (UNLIKELY(instrumentation->HasMethodEntryListeners())) {
264 instrumentation->MethodEnterEvent(self, shadow_frame.GetThisObject(code_item->ins_size_),
265 method, 0);
266 }
267
Aart Bik01223202016-05-05 15:10:42 -0700268 if (!stay_in_interpreter) {
269 jit::Jit* jit = Runtime::Current()->GetJit();
270 if (jit != nullptr) {
271 jit->MethodEntered(self, shadow_frame.GetMethod());
272 if (jit->CanInvokeCompiledCode(method)) {
273 JValue result;
buzbee734f3aa2016-01-28 14:20:06 -0800274
Aart Bik01223202016-05-05 15:10:42 -0700275 // Pop the shadow frame before calling into compiled code.
276 self->PopShadowFrame();
277 ArtInterpreterToCompiledCodeBridge(self, nullptr, code_item, &shadow_frame, &result);
278 // Push the shadow frame back as the caller will expect it.
279 self->PushShadowFrame(&shadow_frame);
buzbee734f3aa2016-01-28 14:20:06 -0800280
Aart Bik01223202016-05-05 15:10:42 -0700281 return result;
282 }
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100283 }
buzbee734f3aa2016-01-28 14:20:06 -0800284 }
285 }
286
Sebastien Hertz4e99b3d2014-06-24 14:35:40 +0200287 shadow_frame.GetMethod()->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200288
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700289 // Lock counting is a special version of accessibility checks, and for simplicity and
290 // reduction of template parameters, we gate it behind access-checks mode.
291 ArtMethod* method = shadow_frame.GetMethod();
292 DCHECK(!method->SkipAccessChecks() || !method->MustCountLocks());
293
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100294 bool transaction_active = Runtime::Current()->IsActiveTransaction();
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700295 if (LIKELY(method->SkipAccessChecks())) {
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200296 // Enter the "without access check" interpreter.
buzbee1452bee2015-03-06 14:43:04 -0800297 if (kInterpreterImplKind == kMterpImplKind) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100298 if (transaction_active) {
buzbee1452bee2015-03-06 14:43:04 -0800299 // No Mterp variant - just use the switch interpreter.
300 return ExecuteSwitchImpl<false, true>(self, code_item, shadow_frame, result_register,
301 false);
Bill Buzbeefd522f92016-02-11 22:37:42 +0000302 } else if (UNLIKELY(!Runtime::Current()->IsStarted())) {
303 return ExecuteSwitchImpl<false, false>(self, code_item, shadow_frame, result_register,
304 false);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100305 } else {
buzbee1452bee2015-03-06 14:43:04 -0800306 while (true) {
Bill Buzbeefd522f92016-02-11 22:37:42 +0000307 // Mterp does not support all instrumentation/debugging.
Andreas Gampe67409972016-07-19 22:34:53 -0700308 if (MterpShouldSwitchInterpreters() != 0) {
buzbee1452bee2015-03-06 14:43:04 -0800309 return ExecuteSwitchImpl<false, false>(self, code_item, shadow_frame, result_register,
310 false);
buzbee1452bee2015-03-06 14:43:04 -0800311 }
312 bool returned = ExecuteMterpImpl(self, code_item, &shadow_frame, &result_register);
313 if (returned) {
314 return result_register;
315 } else {
316 // Mterp didn't like that instruction. Single-step it with the reference interpreter.
buzbeed6b48db2016-01-28 15:48:55 -0800317 result_register = ExecuteSwitchImpl<false, false>(self, code_item, shadow_frame,
buzbee1452bee2015-03-06 14:43:04 -0800318 result_register, true);
319 if (shadow_frame.GetDexPC() == DexFile::kDexNoIndex) {
320 // Single-stepped a return or an exception not handled locally. Return to caller.
buzbeed6b48db2016-01-28 15:48:55 -0800321 return result_register;
buzbee1452bee2015-03-06 14:43:04 -0800322 }
323 }
324 }
325 }
326 } else if (kInterpreterImplKind == kSwitchImplKind) {
327 if (transaction_active) {
328 return ExecuteSwitchImpl<false, true>(self, code_item, shadow_frame, result_register,
329 false);
330 } else {
331 return ExecuteSwitchImpl<false, false>(self, code_item, shadow_frame, result_register,
332 false);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100333 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200334 } else {
335 DCHECK_EQ(kInterpreterImplKind, kComputedGotoImplKind);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100336 if (transaction_active) {
Ian Rogerse94652f2014-12-02 11:13:19 -0800337 return ExecuteGotoImpl<false, true>(self, code_item, shadow_frame, result_register);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100338 } else {
Ian Rogerse94652f2014-12-02 11:13:19 -0800339 return ExecuteGotoImpl<false, false>(self, code_item, shadow_frame, result_register);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100340 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200341 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200342 } else {
343 // Enter the "with access check" interpreter.
buzbee1452bee2015-03-06 14:43:04 -0800344 if (kInterpreterImplKind == kMterpImplKind) {
345 // No access check variants for Mterp. Just use the switch version.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100346 if (transaction_active) {
buzbee1452bee2015-03-06 14:43:04 -0800347 return ExecuteSwitchImpl<true, true>(self, code_item, shadow_frame, result_register,
348 false);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100349 } else {
buzbee1452bee2015-03-06 14:43:04 -0800350 return ExecuteSwitchImpl<true, false>(self, code_item, shadow_frame, result_register,
351 false);
352 }
353 } else if (kInterpreterImplKind == kSwitchImplKind) {
354 if (transaction_active) {
355 return ExecuteSwitchImpl<true, true>(self, code_item, shadow_frame, result_register,
356 false);
357 } else {
358 return ExecuteSwitchImpl<true, false>(self, code_item, shadow_frame, result_register,
359 false);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100360 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200361 } else {
362 DCHECK_EQ(kInterpreterImplKind, kComputedGotoImplKind);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100363 if (transaction_active) {
Ian Rogerse94652f2014-12-02 11:13:19 -0800364 return ExecuteGotoImpl<true, true>(self, code_item, shadow_frame, result_register);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100365 } else {
Ian Rogerse94652f2014-12-02 11:13:19 -0800366 return ExecuteGotoImpl<true, false>(self, code_item, shadow_frame, result_register);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100367 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200368 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200369 }
370}
371
Brian Carlstromea46f952013-07-30 01:26:50 -0700372void EnterInterpreterFromInvoke(Thread* self, ArtMethod* method, Object* receiver,
Aart Bik01223202016-05-05 15:10:42 -0700373 uint32_t* args, JValue* result,
374 bool stay_in_interpreter) {
Ian Rogers64b6d142012-10-29 16:34:15 -0700375 DCHECK_EQ(self, Thread::Current());
Nicolas Geoffray535a3fb2014-07-22 15:17:38 +0100376 bool implicit_check = !Runtime::Current()->ExplicitStackOverflowChecks();
377 if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEndForInterpreter(implicit_check))) {
jeffhaod7521322012-11-21 15:38:24 -0800378 ThrowStackOverflowError(self);
379 return;
380 }
381
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700382 const char* old_cause = self->StartAssertNoThreadSuspension("EnterInterpreterFromInvoke");
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700383 const DexFile::CodeItem* code_item = method->GetCodeItem();
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700384 uint16_t num_regs;
385 uint16_t num_ins;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700386 if (code_item != nullptr) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700387 num_regs = code_item->registers_size_;
388 num_ins = code_item->ins_size_;
Alex Light9139e002015-10-09 15:59:48 -0700389 } else if (!method->IsInvokable()) {
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700390 self->EndAssertNoThreadSuspension(old_cause);
Alex Light9139e002015-10-09 15:59:48 -0700391 method->ThrowInvocationTimeError();
jeffhao0a9bb732012-11-26 12:28:49 -0800392 return;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700393 } else {
394 DCHECK(method->IsNative());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700395 num_regs = num_ins = ArtMethod::NumArgRegisters(method->GetShorty());
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700396 if (!method->IsStatic()) {
397 num_regs++;
398 num_ins++;
399 }
400 }
401 // Set up shadow frame with matching number of reference slots to vregs.
402 ShadowFrame* last_shadow_frame = self->GetManagedStack()->GetTopShadowFrame();
Andreas Gampeb3025922015-09-01 14:45:00 -0700403 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
Andreas Gampe03ec9302015-08-27 17:41:47 -0700404 CREATE_SHADOW_FRAME(num_regs, last_shadow_frame, method, /* dex pc */ 0);
Andreas Gampeb3025922015-09-01 14:45:00 -0700405 ShadowFrame* shadow_frame = shadow_frame_unique_ptr.get();
Jeff Hao66135192013-05-14 11:02:41 -0700406 self->PushShadowFrame(shadow_frame);
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700407
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700408 size_t cur_reg = num_regs - num_ins;
409 if (!method->IsStatic()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700410 CHECK(receiver != nullptr);
TDYa127ce4cc0d2012-11-18 16:59:53 -0800411 shadow_frame->SetVRegReference(cur_reg, receiver);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700412 ++cur_reg;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700413 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700414 uint32_t shorty_len = 0;
415 const char* shorty = method->GetShorty(&shorty_len);
Jeff Hao5d917302013-02-27 17:57:33 -0800416 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 -0700417 DCHECK_LT(shorty_pos + 1, shorty_len);
Jeff Hao5d917302013-02-27 17:57:33 -0800418 switch (shorty[shorty_pos + 1]) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700419 case 'L': {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800420 Object* o = reinterpret_cast<StackReference<Object>*>(&args[arg_pos])->AsMirrorPtr();
TDYa127ce4cc0d2012-11-18 16:59:53 -0800421 shadow_frame->SetVRegReference(cur_reg, o);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700422 break;
423 }
Jeff Hao5d917302013-02-27 17:57:33 -0800424 case 'J': case 'D': {
425 uint64_t wide_value = (static_cast<uint64_t>(args[arg_pos + 1]) << 32) | args[arg_pos];
426 shadow_frame->SetVRegLong(cur_reg, wide_value);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700427 cur_reg++;
Jeff Hao5d917302013-02-27 17:57:33 -0800428 arg_pos++;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700429 break;
Jeff Hao5d917302013-02-27 17:57:33 -0800430 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700431 default:
Jeff Hao5d917302013-02-27 17:57:33 -0800432 shadow_frame->SetVReg(cur_reg, args[arg_pos]);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700433 break;
434 }
435 }
Mathieu Chartier92246bb2014-02-25 18:22:39 -0800436 self->EndAssertNoThreadSuspension(old_cause);
437 // Do this after populating the shadow frame in case EnsureInitialized causes a GC.
Ian Rogers6c5cb212014-06-18 16:07:20 -0700438 if (method->IsStatic() && UNLIKELY(!method->GetDeclaringClass()->IsInitialized())) {
Mathieu Chartier92246bb2014-02-25 18:22:39 -0800439 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700440 StackHandleScope<1> hs(self);
441 Handle<mirror::Class> h_class(hs.NewHandle(method->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700442 if (UNLIKELY(!class_linker->EnsureInitialized(self, h_class, true, true))) {
Mathieu Chartier92246bb2014-02-25 18:22:39 -0800443 CHECK(self->IsExceptionPending());
444 self->PopShadowFrame();
445 return;
446 }
447 }
Ian Rogers64b6d142012-10-29 16:34:15 -0700448 if (LIKELY(!method->IsNative())) {
Aart Bik01223202016-05-05 15:10:42 -0700449 JValue r = Execute(self, code_item, *shadow_frame, JValue(), stay_in_interpreter);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700450 if (result != nullptr) {
Jeff Hao6474d192013-03-26 14:08:09 -0700451 *result = r;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700452 }
453 } else {
Ian Rogers64b6d142012-10-29 16:34:15 -0700454 // We don't expect to be asked to interpret native code (which is entered via a JNI compiler
455 // generated stub) except during testing and image writing.
Mathieu Chartier92246bb2014-02-25 18:22:39 -0800456 // Update args to be the args in the shadow frame since the input ones could hold stale
457 // references pointers due to moving GC.
458 args = shadow_frame->GetVRegArgs(method->IsStatic() ? 0 : 1);
Ian Rogers64b6d142012-10-29 16:34:15 -0700459 if (!Runtime::Current()->IsStarted()) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700460 UnstartedRuntime::Jni(self, method, receiver, args, result);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700461 } else {
Jeff Hao6474d192013-03-26 14:08:09 -0700462 InterpreterJni(self, method, shorty, receiver, args, result);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700463 }
464 }
465 self->PopShadowFrame();
466}
467
Mingyao Yangffedec52016-05-19 10:48:40 -0700468static bool IsStringInit(const Instruction* instr, ArtMethod* caller)
469 SHARED_REQUIRES(Locks::mutator_lock_) {
470 if (instr->Opcode() == Instruction::INVOKE_DIRECT ||
471 instr->Opcode() == Instruction::INVOKE_DIRECT_RANGE) {
472 // Instead of calling ResolveMethod() which has suspend point and can trigger
473 // GC, look up the callee method symbolically.
474 uint16_t callee_method_idx = (instr->Opcode() == Instruction::INVOKE_DIRECT_RANGE) ?
475 instr->VRegB_3rc() : instr->VRegB_35c();
476 const DexFile* dex_file = caller->GetDexFile();
477 const DexFile::MethodId& method_id = dex_file->GetMethodId(callee_method_idx);
478 const char* class_name = dex_file->StringByTypeIdx(method_id.class_idx_);
479 const char* method_name = dex_file->GetMethodName(method_id);
480 // Compare method's class name and method name against string init.
481 // It's ok since it's not allowed to create your own java/lang/String.
482 // TODO: verify that assumption.
483 if ((strcmp(class_name, "Ljava/lang/String;") == 0) &&
484 (strcmp(method_name, "<init>") == 0)) {
485 return true;
486 }
487 }
488 return false;
489}
490
491static int16_t GetReceiverRegisterForStringInit(const Instruction* instr) {
492 DCHECK(instr->Opcode() == Instruction::INVOKE_DIRECT_RANGE ||
493 instr->Opcode() == Instruction::INVOKE_DIRECT);
494 return (instr->Opcode() == Instruction::INVOKE_DIRECT_RANGE) ?
495 instr->VRegC_3rc() : instr->VRegC_35c();
496}
497
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100498void EnterInterpreterFromDeoptimize(Thread* self,
499 ShadowFrame* shadow_frame,
500 bool from_code,
501 JValue* ret_val)
Mathieu Chartier90443472015-07-16 20:32:27 -0700502 SHARED_REQUIRES(Locks::mutator_lock_) {
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800503 JValue value;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700504 // Set value to last known result in case the shadow frame chain is empty.
505 value.SetJ(ret_val->GetJ());
Sebastien Hertz520633b2015-09-08 17:03:36 +0200506 // Are we executing the first shadow frame?
507 bool first = true;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700508 while (shadow_frame != nullptr) {
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700509 // We do not want to recover lock state for lock counting when deoptimizing. Currently,
510 // the compiler should not have compiled a method that failed structured-locking checks.
511 DCHECK(!shadow_frame->GetMethod()->MustCountLocks());
512
Ian Rogers62d6c772013-02-27 08:32:07 -0800513 self->SetTopOfShadowStack(shadow_frame);
Ian Rogerse94652f2014-12-02 11:13:19 -0800514 const DexFile::CodeItem* code_item = shadow_frame->GetMethod()->GetCodeItem();
Sebastien Hertz270a0e12015-01-16 19:49:09 +0100515 const uint32_t dex_pc = shadow_frame->GetDexPC();
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100516 uint32_t new_dex_pc = dex_pc;
Sebastien Hertz270a0e12015-01-16 19:49:09 +0100517 if (UNLIKELY(self->IsExceptionPending())) {
Sebastien Hertz520633b2015-09-08 17:03:36 +0200518 // If we deoptimize from the QuickExceptionHandler, we already reported the exception to
519 // the instrumentation. To prevent from reporting it a second time, we simply pass a
520 // null Instrumentation*.
Sebastien Hertz270a0e12015-01-16 19:49:09 +0100521 const instrumentation::Instrumentation* const instrumentation =
Sebastien Hertz520633b2015-09-08 17:03:36 +0200522 first ? nullptr : Runtime::Current()->GetInstrumentation();
Sebastien Hertz270a0e12015-01-16 19:49:09 +0100523 uint32_t found_dex_pc = FindNextInstructionFollowingException(self, *shadow_frame, dex_pc,
524 instrumentation);
525 new_dex_pc = found_dex_pc; // the dex pc of a matching catch handler
526 // or DexFile::kDexNoIndex if there is none.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100527 } else if (!from_code) {
528 // For the debugger and full deoptimization stack, we must go past the invoke
529 // instruction, as it already executed.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700530 // 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]);
Mingyao Yang504a6902016-04-28 16:23:01 -0700532 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) {
545 // 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();
Mingyao Yangffedec52016-05-19 10:48:40 -0700547 // 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();
Mingyao Yang504a6902016-04-28 16:23:01 -0700554 if (kIsDebugBuild) {
555 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mingyao Yangffedec52016-05-19 10:48:40 -0700556 // This is a suspend point. But it's ok since value has been set into shadow_frame.
Mingyao Yang504a6902016-04-28 16:23:01 -0700557 mirror::Class* klass = class_linker->ResolveType(
558 instr->VRegB_21c(), shadow_frame->GetMethod());
559 DCHECK(klass->IsStringClass());
560 }
Mingyao Yang504a6902016-04-28 16:23:01 -0700561 } else {
Mingyao Yangffedec52016-05-19 10:48:40 -0700562 CHECK(false) << "Unexpected instruction opcode " << instr->Opcode()
563 << " at dex_pc " << dex_pc
564 << " of method: " << 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) {
571 shadow_frame->SetDexPC(new_dex_pc);
572 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 Geoffray73be1e82015-09-17 15:22:56 +0100577 // Following deoptimizations of shadow frames must pass the invoke instruction.
578 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
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700600void ArtInterpreterToInterpreterBridge(Thread* self, const DexFile::CodeItem* code_item,
601 ShadowFrame* shadow_frame, JValue* result) {
Nicolas Geoffray535a3fb2014-07-22 15:17:38 +0100602 bool implicit_check = !Runtime::Current()->ExplicitStackOverflowChecks();
603 if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEndForInterpreter(implicit_check))) {
Jeff Hao16743632013-05-08 10:59:04 -0700604 ThrowStackOverflowError(self);
Jeff Hao69510672013-05-21 17:34:55 -0700605 return;
Jeff Hao16743632013-05-08 10:59:04 -0700606 }
607
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700608 self->PushShadowFrame(shadow_frame);
Alex Lighteb7c1442015-08-31 13:17:42 -0700609 ArtMethod* method = shadow_frame->GetMethod();
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200610 // Ensure static methods are initialized.
Alex Lighteb7c1442015-08-31 13:17:42 -0700611 const bool is_static = method->IsStatic();
Ian Rogerse94652f2014-12-02 11:13:19 -0800612 if (is_static) {
Alex Lighteb7c1442015-08-31 13:17:42 -0700613 mirror::Class* declaring_class = method->GetDeclaringClass();
Ian Rogers6c5cb212014-06-18 16:07:20 -0700614 if (UNLIKELY(!declaring_class->IsInitialized())) {
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700615 StackHandleScope<1> hs(self);
616 HandleWrapper<Class> h_declaring_class(hs.NewHandleWrapper(&declaring_class));
617 if (UNLIKELY(!Runtime::Current()->GetClassLinker()->EnsureInitialized(
Ian Rogers7b078e82014-09-10 14:44:24 -0700618 self, h_declaring_class, true, true))) {
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700619 DCHECK(self->IsExceptionPending());
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700620 self->PopShadowFrame();
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200621 return;
622 }
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700623 CHECK(h_declaring_class->IsInitializing());
Jeff Hao16743632013-05-08 10:59:04 -0700624 }
Jeff Hao16743632013-05-08 10:59:04 -0700625 }
Jeff Hao16743632013-05-08 10:59:04 -0700626
Ian Rogerse94652f2014-12-02 11:13:19 -0800627 if (LIKELY(!shadow_frame->GetMethod()->IsNative())) {
628 result->SetJ(Execute(self, code_item, *shadow_frame, JValue()).GetJ());
Jeff Hao16743632013-05-08 10:59:04 -0700629 } else {
630 // We don't expect to be asked to interpret native code (which is entered via a JNI compiler
631 // generated stub) except during testing and image writing.
632 CHECK(!Runtime::Current()->IsStarted());
Ian Rogerse94652f2014-12-02 11:13:19 -0800633 Object* receiver = is_static ? nullptr : shadow_frame->GetVRegReference(0);
634 uint32_t* args = shadow_frame->GetVRegArgs(is_static ? 0 : 1);
Andreas Gampe799681b2015-05-15 19:24:12 -0700635 UnstartedRuntime::Jni(self, shadow_frame->GetMethod(), receiver, args, result);
Jeff Hao16743632013-05-08 10:59:04 -0700636 }
637
638 self->PopShadowFrame();
Jeff Hao16743632013-05-08 10:59:04 -0700639}
640
buzbee1452bee2015-03-06 14:43:04 -0800641void CheckInterpreterAsmConstants() {
642 CheckMterpAsmConstants();
643}
644
645void InitInterpreterTls(Thread* self) {
646 InitMterpTls(self);
647}
648
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700649} // namespace interpreter
650} // namespace art