blob: 8d5a61a44b1b98e473b3a26e3b8a1e5220e91168 [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"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070023#include "mirror/string-inl.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070024#include "scoped_thread_state_change.h"
25#include "ScopedLocalRef.h"
Andreas Gampeb3025922015-09-01 14:45:00 -070026#include "stack.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070027#include "unstarted_runtime.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070028
Ian Rogers2fa6b2e2012-10-17 00:10:17 -070029namespace art {
30namespace interpreter {
31
Ian Rogersfc0e94b2013-09-23 23:51:32 -070032static void InterpreterJni(Thread* self, ArtMethod* method, const StringPiece& shorty,
Jeff Hao5d917302013-02-27 17:57:33 -080033 Object* receiver, uint32_t* args, JValue* result)
Mathieu Chartier90443472015-07-16 20:32:27 -070034 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers64b6d142012-10-29 16:34:15 -070035 // TODO: The following enters JNI code using a typedef-ed function rather than the JNI compiler,
36 // it should be removed and JNI compiled stubs used instead.
37 ScopedObjectAccessUnchecked soa(self);
38 if (method->IsStatic()) {
39 if (shorty == "L") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +010040 typedef jobject (fntype)(JNIEnv*, jclass);
Mathieu Chartier2d721012014-11-10 11:08:06 -080041 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -070042 ScopedLocalRef<jclass> klass(soa.Env(),
43 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
Ian Rogers556d6372012-11-20 12:19:36 -080044 jobject jresult;
45 {
46 ScopedThreadStateChange tsc(self, kNative);
47 jresult = fn(soa.Env(), klass.get());
48 }
49 result->SetL(soa.Decode<Object*>(jresult));
Ian Rogers64b6d142012-10-29 16:34:15 -070050 } else if (shorty == "V") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +010051 typedef void (fntype)(JNIEnv*, jclass);
Mathieu Chartier2d721012014-11-10 11:08:06 -080052 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -070053 ScopedLocalRef<jclass> klass(soa.Env(),
54 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
55 ScopedThreadStateChange tsc(self, kNative);
56 fn(soa.Env(), klass.get());
57 } else if (shorty == "Z") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +010058 typedef jboolean (fntype)(JNIEnv*, jclass);
Mathieu Chartier2d721012014-11-10 11:08:06 -080059 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -070060 ScopedLocalRef<jclass> klass(soa.Env(),
61 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
62 ScopedThreadStateChange tsc(self, kNative);
63 result->SetZ(fn(soa.Env(), klass.get()));
64 } else if (shorty == "BI") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +010065 typedef jbyte (fntype)(JNIEnv*, jclass, jint);
Mathieu Chartier2d721012014-11-10 11:08:06 -080066 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -070067 ScopedLocalRef<jclass> klass(soa.Env(),
68 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
69 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -080070 result->SetB(fn(soa.Env(), klass.get(), args[0]));
Ian Rogers64b6d142012-10-29 16:34:15 -070071 } else if (shorty == "II") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +010072 typedef jint (fntype)(JNIEnv*, jclass, jint);
Mathieu Chartier2d721012014-11-10 11:08:06 -080073 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -070074 ScopedLocalRef<jclass> klass(soa.Env(),
75 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
76 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -080077 result->SetI(fn(soa.Env(), klass.get(), args[0]));
Ian Rogers64b6d142012-10-29 16:34:15 -070078 } else if (shorty == "LL") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +010079 typedef jobject (fntype)(JNIEnv*, jclass, jobject);
Mathieu Chartier2d721012014-11-10 11:08:06 -080080 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -070081 ScopedLocalRef<jclass> klass(soa.Env(),
82 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
83 ScopedLocalRef<jobject> arg0(soa.Env(),
Mathieu Chartier2cebb242015-04-21 16:50:40 -070084 soa.AddLocalReference<jobject>(
85 reinterpret_cast<Object*>(args[0])));
Ian Rogers556d6372012-11-20 12:19:36 -080086 jobject jresult;
87 {
88 ScopedThreadStateChange tsc(self, kNative);
89 jresult = fn(soa.Env(), klass.get(), arg0.get());
90 }
91 result->SetL(soa.Decode<Object*>(jresult));
Ian Rogers64b6d142012-10-29 16:34:15 -070092 } else if (shorty == "IIZ") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +010093 typedef jint (fntype)(JNIEnv*, jclass, jint, jboolean);
Mathieu Chartier2d721012014-11-10 11:08:06 -080094 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -070095 ScopedLocalRef<jclass> klass(soa.Env(),
96 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
97 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -080098 result->SetI(fn(soa.Env(), klass.get(), args[0], args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -070099 } else if (shorty == "ILI") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100100 typedef jint (fntype)(JNIEnv*, jclass, jobject, jint);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800101 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(
102 method->GetEntryPointFromJni()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700103 ScopedLocalRef<jclass> klass(soa.Env(),
104 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
105 ScopedLocalRef<jobject> arg0(soa.Env(),
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700106 soa.AddLocalReference<jobject>(
107 reinterpret_cast<Object*>(args[0])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700108 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800109 result->SetI(fn(soa.Env(), klass.get(), arg0.get(), args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700110 } else if (shorty == "SIZ") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100111 typedef jshort (fntype)(JNIEnv*, jclass, jint, jboolean);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700112 fntype* const fn =
113 reinterpret_cast<fntype*>(const_cast<void*>(method->GetEntryPointFromJni()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700114 ScopedLocalRef<jclass> klass(soa.Env(),
115 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
116 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800117 result->SetS(fn(soa.Env(), klass.get(), args[0], args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700118 } else if (shorty == "VIZ") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100119 typedef void (fntype)(JNIEnv*, jclass, jint, jboolean);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800120 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700121 ScopedLocalRef<jclass> klass(soa.Env(),
122 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
123 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800124 fn(soa.Env(), klass.get(), args[0], args[1]);
Ian Rogers64b6d142012-10-29 16:34:15 -0700125 } else if (shorty == "ZLL") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100126 typedef jboolean (fntype)(JNIEnv*, jclass, jobject, jobject);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800127 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700128 ScopedLocalRef<jclass> klass(soa.Env(),
129 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
130 ScopedLocalRef<jobject> arg0(soa.Env(),
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700131 soa.AddLocalReference<jobject>(
132 reinterpret_cast<Object*>(args[0])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700133 ScopedLocalRef<jobject> arg1(soa.Env(),
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700134 soa.AddLocalReference<jobject>(
135 reinterpret_cast<Object*>(args[1])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700136 ScopedThreadStateChange tsc(self, kNative);
137 result->SetZ(fn(soa.Env(), klass.get(), arg0.get(), arg1.get()));
138 } else if (shorty == "ZILL") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100139 typedef jboolean (fntype)(JNIEnv*, jclass, jint, 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> arg1(soa.Env(),
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700144 soa.AddLocalReference<jobject>(
145 reinterpret_cast<Object*>(args[1])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700146 ScopedLocalRef<jobject> arg2(soa.Env(),
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700147 soa.AddLocalReference<jobject>(
148 reinterpret_cast<Object*>(args[2])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700149 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800150 result->SetZ(fn(soa.Env(), klass.get(), args[0], arg1.get(), arg2.get()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700151 } else if (shorty == "VILII") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100152 typedef void (fntype)(JNIEnv*, jclass, jint, jobject, jint, jint);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800153 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700154 ScopedLocalRef<jclass> klass(soa.Env(),
155 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
156 ScopedLocalRef<jobject> arg1(soa.Env(),
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700157 soa.AddLocalReference<jobject>(
158 reinterpret_cast<Object*>(args[1])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700159 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800160 fn(soa.Env(), klass.get(), args[0], arg1.get(), args[2], args[3]);
Ian Rogers64b6d142012-10-29 16:34:15 -0700161 } else if (shorty == "VLILII") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100162 typedef void (fntype)(JNIEnv*, jclass, jobject, jint, jobject, jint, jint);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800163 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700164 ScopedLocalRef<jclass> klass(soa.Env(),
165 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
166 ScopedLocalRef<jobject> arg0(soa.Env(),
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700167 soa.AddLocalReference<jobject>(
168 reinterpret_cast<Object*>(args[0])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700169 ScopedLocalRef<jobject> arg2(soa.Env(),
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700170 soa.AddLocalReference<jobject>(
171 reinterpret_cast<Object*>(args[2])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700172 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800173 fn(soa.Env(), klass.get(), arg0.get(), args[1], arg2.get(), args[3], args[4]);
Ian Rogers64b6d142012-10-29 16:34:15 -0700174 } else {
175 LOG(FATAL) << "Do something with static native method: " << PrettyMethod(method)
176 << " shorty: " << shorty;
177 }
178 } else {
179 if (shorty == "L") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100180 typedef jobject (fntype)(JNIEnv*, jobject);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800181 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700182 ScopedLocalRef<jobject> rcvr(soa.Env(),
183 soa.AddLocalReference<jobject>(receiver));
Ian Rogers556d6372012-11-20 12:19:36 -0800184 jobject jresult;
185 {
186 ScopedThreadStateChange tsc(self, kNative);
187 jresult = fn(soa.Env(), rcvr.get());
188 }
189 result->SetL(soa.Decode<Object*>(jresult));
Jeff Hao3dd9f762013-07-08 13:09:25 -0700190 } else if (shorty == "V") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100191 typedef void (fntype)(JNIEnv*, jobject);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800192 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Jeff Hao3dd9f762013-07-08 13:09:25 -0700193 ScopedLocalRef<jobject> rcvr(soa.Env(),
194 soa.AddLocalReference<jobject>(receiver));
195 ScopedThreadStateChange tsc(self, kNative);
196 fn(soa.Env(), rcvr.get());
Ian Rogers64b6d142012-10-29 16:34:15 -0700197 } else if (shorty == "LL") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100198 typedef jobject (fntype)(JNIEnv*, jobject, jobject);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800199 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700200 ScopedLocalRef<jobject> rcvr(soa.Env(),
201 soa.AddLocalReference<jobject>(receiver));
202 ScopedLocalRef<jobject> arg0(soa.Env(),
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700203 soa.AddLocalReference<jobject>(
204 reinterpret_cast<Object*>(args[0])));
Ian Rogers556d6372012-11-20 12:19:36 -0800205 jobject jresult;
206 {
207 ScopedThreadStateChange tsc(self, kNative);
208 jresult = fn(soa.Env(), rcvr.get(), arg0.get());
Ian Rogers556d6372012-11-20 12:19:36 -0800209 }
210 result->SetL(soa.Decode<Object*>(jresult));
Ian Rogers64b6d142012-10-29 16:34:15 -0700211 ScopedThreadStateChange tsc(self, kNative);
Ian Rogers64b6d142012-10-29 16:34:15 -0700212 } else if (shorty == "III") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100213 typedef jint (fntype)(JNIEnv*, jobject, jint, jint);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800214 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700215 ScopedLocalRef<jobject> rcvr(soa.Env(),
216 soa.AddLocalReference<jobject>(receiver));
217 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800218 result->SetI(fn(soa.Env(), rcvr.get(), args[0], args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700219 } else {
220 LOG(FATAL) << "Do something with native method: " << PrettyMethod(method)
221 << " shorty: " << shorty;
222 }
223 }
224}
225
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200226enum InterpreterImplKind {
Ian Rogersb48b9eb2014-02-28 16:20:21 -0800227 kSwitchImpl, // Switch-based interpreter implementation.
228 kComputedGotoImplKind // Computed-goto-based interpreter implementation.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200229};
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800230static std::ostream& operator<<(std::ostream& os, const InterpreterImplKind& rhs) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700231 os << ((rhs == kSwitchImpl) ? "Switch-based interpreter" : "Computed-goto-based interpreter");
232 return os;
233}
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700234
Sebastien Hertzfa888d02014-09-30 12:00:11 +0200235#if !defined(__clang__)
Ian Rogersb48b9eb2014-02-28 16:20:21 -0800236static constexpr InterpreterImplKind kInterpreterImplKind = kComputedGotoImplKind;
Sebastien Hertzfa888d02014-09-30 12:00:11 +0200237#else
238// Clang 3.4 fails to build the goto interpreter implementation.
239static constexpr InterpreterImplKind kInterpreterImplKind = kSwitchImpl;
240template<bool do_access_check, bool transaction_active>
Ian Rogerse94652f2014-12-02 11:13:19 -0800241JValue ExecuteGotoImpl(Thread*, const DexFile::CodeItem*, ShadowFrame&, JValue) {
Sebastien Hertzfa888d02014-09-30 12:00:11 +0200242 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700243 UNREACHABLE();
Sebastien Hertzfa888d02014-09-30 12:00:11 +0200244}
245// Explicit definitions of ExecuteGotoImpl.
Mathieu Chartier90443472015-07-16 20:32:27 -0700246template<> SHARED_REQUIRES(Locks::mutator_lock_)
Ian Rogerse94652f2014-12-02 11:13:19 -0800247JValue ExecuteGotoImpl<true, false>(Thread* self, const DexFile::CodeItem* code_item,
Sebastien Hertzfa888d02014-09-30 12:00:11 +0200248 ShadowFrame& shadow_frame, JValue result_register);
Mathieu Chartier90443472015-07-16 20:32:27 -0700249template<> SHARED_REQUIRES(Locks::mutator_lock_)
Ian Rogerse94652f2014-12-02 11:13:19 -0800250JValue ExecuteGotoImpl<false, false>(Thread* self, const DexFile::CodeItem* code_item,
Sebastien Hertzfa888d02014-09-30 12:00:11 +0200251 ShadowFrame& shadow_frame, JValue result_register);
Mathieu Chartier90443472015-07-16 20:32:27 -0700252template<> SHARED_REQUIRES(Locks::mutator_lock_)
Ian Rogerse94652f2014-12-02 11:13:19 -0800253JValue ExecuteGotoImpl<true, true>(Thread* self, const DexFile::CodeItem* code_item,
254 ShadowFrame& shadow_frame, JValue result_register);
Mathieu Chartier90443472015-07-16 20:32:27 -0700255template<> SHARED_REQUIRES(Locks::mutator_lock_)
Ian Rogerse94652f2014-12-02 11:13:19 -0800256JValue ExecuteGotoImpl<false, true>(Thread* self, const DexFile::CodeItem* code_item,
Sebastien Hertzfa888d02014-09-30 12:00:11 +0200257 ShadowFrame& shadow_frame, JValue result_register);
Sebastien Hertzfa888d02014-09-30 12:00:11 +0200258#endif
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700259
Ian Rogerse94652f2014-12-02 11:13:19 -0800260static JValue Execute(Thread* self, const DexFile::CodeItem* code_item, ShadowFrame& shadow_frame,
261 JValue result_register)
Mathieu Chartier90443472015-07-16 20:32:27 -0700262 SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200263
Ian Rogerse94652f2014-12-02 11:13:19 -0800264static inline JValue Execute(Thread* self, const DexFile::CodeItem* code_item,
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200265 ShadowFrame& shadow_frame, JValue result_register) {
Alex Light9139e002015-10-09 15:59:48 -0700266 DCHECK(shadow_frame.GetMethod()->IsInvokable());
Ian Rogers848871b2013-08-05 10:56:33 -0700267 DCHECK(!shadow_frame.GetMethod()->IsNative());
Sebastien Hertz4e99b3d2014-06-24 14:35:40 +0200268 shadow_frame.GetMethod()->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200269
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100270 bool transaction_active = Runtime::Current()->IsActiveTransaction();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200271 if (LIKELY(shadow_frame.GetMethod()->IsPreverified())) {
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200272 // Enter the "without access check" interpreter.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200273 if (kInterpreterImplKind == kSwitchImpl) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100274 if (transaction_active) {
Ian Rogerse94652f2014-12-02 11:13:19 -0800275 return ExecuteSwitchImpl<false, true>(self, code_item, shadow_frame, result_register);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100276 } else {
Ian Rogerse94652f2014-12-02 11:13:19 -0800277 return ExecuteSwitchImpl<false, false>(self, code_item, shadow_frame, result_register);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100278 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200279 } else {
280 DCHECK_EQ(kInterpreterImplKind, kComputedGotoImplKind);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100281 if (transaction_active) {
Ian Rogerse94652f2014-12-02 11:13:19 -0800282 return ExecuteGotoImpl<false, true>(self, code_item, shadow_frame, result_register);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100283 } else {
Ian Rogerse94652f2014-12-02 11:13:19 -0800284 return ExecuteGotoImpl<false, false>(self, code_item, shadow_frame, result_register);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100285 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200286 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200287 } else {
288 // Enter the "with access check" interpreter.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200289 if (kInterpreterImplKind == kSwitchImpl) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100290 if (transaction_active) {
Ian Rogerse94652f2014-12-02 11:13:19 -0800291 return ExecuteSwitchImpl<true, true>(self, code_item, shadow_frame, result_register);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100292 } else {
Ian Rogerse94652f2014-12-02 11:13:19 -0800293 return ExecuteSwitchImpl<true, false>(self, code_item, shadow_frame, result_register);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100294 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200295 } else {
296 DCHECK_EQ(kInterpreterImplKind, kComputedGotoImplKind);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100297 if (transaction_active) {
Ian Rogerse94652f2014-12-02 11:13:19 -0800298 return ExecuteGotoImpl<true, true>(self, code_item, shadow_frame, result_register);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100299 } else {
Ian Rogerse94652f2014-12-02 11:13:19 -0800300 return ExecuteGotoImpl<true, false>(self, code_item, shadow_frame, result_register);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100301 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200302 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200303 }
304}
305
Brian Carlstromea46f952013-07-30 01:26:50 -0700306void EnterInterpreterFromInvoke(Thread* self, ArtMethod* method, Object* receiver,
Jeff Hao6474d192013-03-26 14:08:09 -0700307 uint32_t* args, JValue* result) {
Ian Rogers64b6d142012-10-29 16:34:15 -0700308 DCHECK_EQ(self, Thread::Current());
Nicolas Geoffray535a3fb2014-07-22 15:17:38 +0100309 bool implicit_check = !Runtime::Current()->ExplicitStackOverflowChecks();
310 if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEndForInterpreter(implicit_check))) {
jeffhaod7521322012-11-21 15:38:24 -0800311 ThrowStackOverflowError(self);
312 return;
313 }
314
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700315 const char* old_cause = self->StartAssertNoThreadSuspension("EnterInterpreterFromInvoke");
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700316 const DexFile::CodeItem* code_item = method->GetCodeItem();
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700317 uint16_t num_regs;
318 uint16_t num_ins;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700319 if (code_item != nullptr) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700320 num_regs = code_item->registers_size_;
321 num_ins = code_item->ins_size_;
Alex Light9139e002015-10-09 15:59:48 -0700322 } else if (!method->IsInvokable()) {
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700323 self->EndAssertNoThreadSuspension(old_cause);
Alex Light9139e002015-10-09 15:59:48 -0700324 method->ThrowInvocationTimeError();
jeffhao0a9bb732012-11-26 12:28:49 -0800325 return;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700326 } else {
327 DCHECK(method->IsNative());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700328 num_regs = num_ins = ArtMethod::NumArgRegisters(method->GetShorty());
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700329 if (!method->IsStatic()) {
330 num_regs++;
331 num_ins++;
332 }
333 }
334 // Set up shadow frame with matching number of reference slots to vregs.
335 ShadowFrame* last_shadow_frame = self->GetManagedStack()->GetTopShadowFrame();
Andreas Gampeb3025922015-09-01 14:45:00 -0700336 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
Andreas Gampe03ec9302015-08-27 17:41:47 -0700337 CREATE_SHADOW_FRAME(num_regs, last_shadow_frame, method, /* dex pc */ 0);
Andreas Gampeb3025922015-09-01 14:45:00 -0700338 ShadowFrame* shadow_frame = shadow_frame_unique_ptr.get();
Jeff Hao66135192013-05-14 11:02:41 -0700339 self->PushShadowFrame(shadow_frame);
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700340
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700341 size_t cur_reg = num_regs - num_ins;
342 if (!method->IsStatic()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700343 CHECK(receiver != nullptr);
TDYa127ce4cc0d2012-11-18 16:59:53 -0800344 shadow_frame->SetVRegReference(cur_reg, receiver);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700345 ++cur_reg;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700346 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700347 uint32_t shorty_len = 0;
348 const char* shorty = method->GetShorty(&shorty_len);
Jeff Hao5d917302013-02-27 17:57:33 -0800349 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 -0700350 DCHECK_LT(shorty_pos + 1, shorty_len);
Jeff Hao5d917302013-02-27 17:57:33 -0800351 switch (shorty[shorty_pos + 1]) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700352 case 'L': {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800353 Object* o = reinterpret_cast<StackReference<Object>*>(&args[arg_pos])->AsMirrorPtr();
TDYa127ce4cc0d2012-11-18 16:59:53 -0800354 shadow_frame->SetVRegReference(cur_reg, o);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700355 break;
356 }
Jeff Hao5d917302013-02-27 17:57:33 -0800357 case 'J': case 'D': {
358 uint64_t wide_value = (static_cast<uint64_t>(args[arg_pos + 1]) << 32) | args[arg_pos];
359 shadow_frame->SetVRegLong(cur_reg, wide_value);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700360 cur_reg++;
Jeff Hao5d917302013-02-27 17:57:33 -0800361 arg_pos++;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700362 break;
Jeff Hao5d917302013-02-27 17:57:33 -0800363 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700364 default:
Jeff Hao5d917302013-02-27 17:57:33 -0800365 shadow_frame->SetVReg(cur_reg, args[arg_pos]);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700366 break;
367 }
368 }
Mathieu Chartier92246bb2014-02-25 18:22:39 -0800369 self->EndAssertNoThreadSuspension(old_cause);
370 // Do this after populating the shadow frame in case EnsureInitialized causes a GC.
Ian Rogers6c5cb212014-06-18 16:07:20 -0700371 if (method->IsStatic() && UNLIKELY(!method->GetDeclaringClass()->IsInitialized())) {
Mathieu Chartier92246bb2014-02-25 18:22:39 -0800372 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700373 StackHandleScope<1> hs(self);
374 Handle<mirror::Class> h_class(hs.NewHandle(method->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700375 if (UNLIKELY(!class_linker->EnsureInitialized(self, h_class, true, true))) {
Mathieu Chartier92246bb2014-02-25 18:22:39 -0800376 CHECK(self->IsExceptionPending());
377 self->PopShadowFrame();
378 return;
379 }
380 }
Ian Rogers64b6d142012-10-29 16:34:15 -0700381 if (LIKELY(!method->IsNative())) {
Ian Rogerse94652f2014-12-02 11:13:19 -0800382 JValue r = Execute(self, code_item, *shadow_frame, JValue());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700383 if (result != nullptr) {
Jeff Hao6474d192013-03-26 14:08:09 -0700384 *result = r;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700385 }
386 } else {
Ian Rogers64b6d142012-10-29 16:34:15 -0700387 // We don't expect to be asked to interpret native code (which is entered via a JNI compiler
388 // generated stub) except during testing and image writing.
Mathieu Chartier92246bb2014-02-25 18:22:39 -0800389 // Update args to be the args in the shadow frame since the input ones could hold stale
390 // references pointers due to moving GC.
391 args = shadow_frame->GetVRegArgs(method->IsStatic() ? 0 : 1);
Ian Rogers64b6d142012-10-29 16:34:15 -0700392 if (!Runtime::Current()->IsStarted()) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700393 UnstartedRuntime::Jni(self, method, receiver, args, result);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700394 } else {
Jeff Hao6474d192013-03-26 14:08:09 -0700395 InterpreterJni(self, method, shorty, receiver, args, result);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700396 }
397 }
398 self->PopShadowFrame();
399}
400
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100401void EnterInterpreterFromDeoptimize(Thread* self,
402 ShadowFrame* shadow_frame,
403 bool from_code,
404 JValue* ret_val)
Mathieu Chartier90443472015-07-16 20:32:27 -0700405 SHARED_REQUIRES(Locks::mutator_lock_) {
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800406 JValue value;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700407 // Set value to last known result in case the shadow frame chain is empty.
408 value.SetJ(ret_val->GetJ());
Sebastien Hertz520633b2015-09-08 17:03:36 +0200409 // Are we executing the first shadow frame?
410 bool first = true;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700411 while (shadow_frame != nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800412 self->SetTopOfShadowStack(shadow_frame);
Ian Rogerse94652f2014-12-02 11:13:19 -0800413 const DexFile::CodeItem* code_item = shadow_frame->GetMethod()->GetCodeItem();
Sebastien Hertz270a0e12015-01-16 19:49:09 +0100414 const uint32_t dex_pc = shadow_frame->GetDexPC();
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100415 uint32_t new_dex_pc = dex_pc;
Sebastien Hertz270a0e12015-01-16 19:49:09 +0100416 if (UNLIKELY(self->IsExceptionPending())) {
Sebastien Hertz520633b2015-09-08 17:03:36 +0200417 // If we deoptimize from the QuickExceptionHandler, we already reported the exception to
418 // the instrumentation. To prevent from reporting it a second time, we simply pass a
419 // null Instrumentation*.
Sebastien Hertz270a0e12015-01-16 19:49:09 +0100420 const instrumentation::Instrumentation* const instrumentation =
Sebastien Hertz520633b2015-09-08 17:03:36 +0200421 first ? nullptr : Runtime::Current()->GetInstrumentation();
Sebastien Hertz270a0e12015-01-16 19:49:09 +0100422 uint32_t found_dex_pc = FindNextInstructionFollowingException(self, *shadow_frame, dex_pc,
423 instrumentation);
424 new_dex_pc = found_dex_pc; // the dex pc of a matching catch handler
425 // or DexFile::kDexNoIndex if there is none.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100426 } else if (!from_code) {
427 // For the debugger and full deoptimization stack, we must go past the invoke
428 // instruction, as it already executed.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700429 // TODO: should be tested more once b/17586779 is fixed.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100430 const Instruction* instr = Instruction::At(&code_item->insns_[dex_pc]);
431 DCHECK(instr->IsInvoke());
432 new_dex_pc = dex_pc + instr->SizeInCodeUnits();
433 } else {
434 // Nothing to do, the dex_pc is the one at which the code requested
435 // the deoptimization.
Sebastien Hertz270a0e12015-01-16 19:49:09 +0100436 }
437 if (new_dex_pc != DexFile::kDexNoIndex) {
438 shadow_frame->SetDexPC(new_dex_pc);
439 value = Execute(self, code_item, *shadow_frame, value);
440 }
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800441 ShadowFrame* old_frame = shadow_frame;
442 shadow_frame = shadow_frame->GetLink();
Christopher Ferris241a9582015-04-27 15:19:41 -0700443 ShadowFrame::DeleteDeoptimizedFrame(old_frame);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100444 // Following deoptimizations of shadow frames must pass the invoke instruction.
445 from_code = false;
Sebastien Hertz520633b2015-09-08 17:03:36 +0200446 first = false;
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800447 }
448 ret_val->SetJ(value.GetJ());
449}
450
Ian Rogerse94652f2014-12-02 11:13:19 -0800451JValue EnterInterpreterFromEntryPoint(Thread* self, const DexFile::CodeItem* code_item,
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700452 ShadowFrame* shadow_frame) {
Ian Rogersf3e98552013-03-20 15:49:49 -0700453 DCHECK_EQ(self, Thread::Current());
Nicolas Geoffray535a3fb2014-07-22 15:17:38 +0100454 bool implicit_check = !Runtime::Current()->ExplicitStackOverflowChecks();
455 if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEndForInterpreter(implicit_check))) {
Ian Rogersf3e98552013-03-20 15:49:49 -0700456 ThrowStackOverflowError(self);
457 return JValue();
458 }
459
Ian Rogerse94652f2014-12-02 11:13:19 -0800460 return Execute(self, code_item, *shadow_frame, JValue());
Ian Rogers7db619b2013-01-16 18:35:48 -0800461}
462
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700463void ArtInterpreterToInterpreterBridge(Thread* self, const DexFile::CodeItem* code_item,
464 ShadowFrame* shadow_frame, JValue* result) {
Nicolas Geoffray535a3fb2014-07-22 15:17:38 +0100465 bool implicit_check = !Runtime::Current()->ExplicitStackOverflowChecks();
466 if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEndForInterpreter(implicit_check))) {
Jeff Hao16743632013-05-08 10:59:04 -0700467 ThrowStackOverflowError(self);
Jeff Hao69510672013-05-21 17:34:55 -0700468 return;
Jeff Hao16743632013-05-08 10:59:04 -0700469 }
470
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700471 self->PushShadowFrame(shadow_frame);
Alex Lighteb7c1442015-08-31 13:17:42 -0700472 ArtMethod* method = shadow_frame->GetMethod();
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200473 // Ensure static methods are initialized.
Alex Lighteb7c1442015-08-31 13:17:42 -0700474 const bool is_static = method->IsStatic();
Ian Rogerse94652f2014-12-02 11:13:19 -0800475 if (is_static) {
Alex Lighteb7c1442015-08-31 13:17:42 -0700476 mirror::Class* declaring_class = method->GetDeclaringClass();
Ian Rogers6c5cb212014-06-18 16:07:20 -0700477 if (UNLIKELY(!declaring_class->IsInitialized())) {
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700478 StackHandleScope<1> hs(self);
479 HandleWrapper<Class> h_declaring_class(hs.NewHandleWrapper(&declaring_class));
480 if (UNLIKELY(!Runtime::Current()->GetClassLinker()->EnsureInitialized(
Ian Rogers7b078e82014-09-10 14:44:24 -0700481 self, h_declaring_class, true, true))) {
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700482 DCHECK(self->IsExceptionPending());
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700483 self->PopShadowFrame();
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200484 return;
485 }
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700486 CHECK(h_declaring_class->IsInitializing());
Jeff Hao16743632013-05-08 10:59:04 -0700487 }
Jeff Hao16743632013-05-08 10:59:04 -0700488 }
Jeff Hao16743632013-05-08 10:59:04 -0700489
Ian Rogerse94652f2014-12-02 11:13:19 -0800490 if (LIKELY(!shadow_frame->GetMethod()->IsNative())) {
491 result->SetJ(Execute(self, code_item, *shadow_frame, JValue()).GetJ());
Jeff Hao16743632013-05-08 10:59:04 -0700492 } else {
493 // We don't expect to be asked to interpret native code (which is entered via a JNI compiler
494 // generated stub) except during testing and image writing.
495 CHECK(!Runtime::Current()->IsStarted());
Ian Rogerse94652f2014-12-02 11:13:19 -0800496 Object* receiver = is_static ? nullptr : shadow_frame->GetVRegReference(0);
497 uint32_t* args = shadow_frame->GetVRegArgs(is_static ? 0 : 1);
Andreas Gampe799681b2015-05-15 19:24:12 -0700498 UnstartedRuntime::Jni(self, shadow_frame->GetMethod(), receiver, args, result);
Jeff Hao16743632013-05-08 10:59:04 -0700499 }
500
501 self->PopShadowFrame();
Jeff Hao16743632013-05-08 10:59:04 -0700502}
503
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700504} // namespace interpreter
505} // namespace art