blob: a37aee5f9b31a418159c4f4152c540d90ba86990 [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
Sebastien Hertz8ece0502013-08-07 11:26:41 +020017#include "interpreter_common.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
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070021#include "mirror/string-inl.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070022#include "scoped_thread_state_change.h"
23#include "ScopedLocalRef.h"
24#include "unstarted_runtime.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070025
Ian Rogers2fa6b2e2012-10-17 00:10:17 -070026namespace art {
27namespace interpreter {
28
Ian Rogersfc0e94b2013-09-23 23:51:32 -070029static void InterpreterJni(Thread* self, ArtMethod* method, const StringPiece& shorty,
Jeff Hao5d917302013-02-27 17:57:33 -080030 Object* receiver, uint32_t* args, JValue* result)
Ian Rogers64b6d142012-10-29 16:34:15 -070031 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
32 // TODO: The following enters JNI code using a typedef-ed function rather than the JNI compiler,
33 // it should be removed and JNI compiled stubs used instead.
34 ScopedObjectAccessUnchecked soa(self);
35 if (method->IsStatic()) {
36 if (shorty == "L") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +010037 typedef jobject (fntype)(JNIEnv*, jclass);
Mathieu Chartier2d721012014-11-10 11:08:06 -080038 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -070039 ScopedLocalRef<jclass> klass(soa.Env(),
40 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
Ian Rogers556d6372012-11-20 12:19:36 -080041 jobject jresult;
42 {
43 ScopedThreadStateChange tsc(self, kNative);
44 jresult = fn(soa.Env(), klass.get());
45 }
46 result->SetL(soa.Decode<Object*>(jresult));
Ian Rogers64b6d142012-10-29 16:34:15 -070047 } else if (shorty == "V") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +010048 typedef void (fntype)(JNIEnv*, jclass);
Mathieu Chartier2d721012014-11-10 11:08:06 -080049 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -070050 ScopedLocalRef<jclass> klass(soa.Env(),
51 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
52 ScopedThreadStateChange tsc(self, kNative);
53 fn(soa.Env(), klass.get());
54 } else if (shorty == "Z") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +010055 typedef jboolean (fntype)(JNIEnv*, jclass);
Mathieu Chartier2d721012014-11-10 11:08:06 -080056 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -070057 ScopedLocalRef<jclass> klass(soa.Env(),
58 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
59 ScopedThreadStateChange tsc(self, kNative);
60 result->SetZ(fn(soa.Env(), klass.get()));
61 } else if (shorty == "BI") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +010062 typedef jbyte (fntype)(JNIEnv*, jclass, jint);
Mathieu Chartier2d721012014-11-10 11:08:06 -080063 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -070064 ScopedLocalRef<jclass> klass(soa.Env(),
65 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
66 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -080067 result->SetB(fn(soa.Env(), klass.get(), args[0]));
Ian Rogers64b6d142012-10-29 16:34:15 -070068 } else if (shorty == "II") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +010069 typedef jint (fntype)(JNIEnv*, jclass, jint);
Mathieu Chartier2d721012014-11-10 11:08:06 -080070 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -070071 ScopedLocalRef<jclass> klass(soa.Env(),
72 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
73 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -080074 result->SetI(fn(soa.Env(), klass.get(), args[0]));
Ian Rogers64b6d142012-10-29 16:34:15 -070075 } else if (shorty == "LL") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +010076 typedef jobject (fntype)(JNIEnv*, jclass, jobject);
Mathieu Chartier2d721012014-11-10 11:08:06 -080077 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -070078 ScopedLocalRef<jclass> klass(soa.Env(),
79 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
80 ScopedLocalRef<jobject> arg0(soa.Env(),
Mathieu Chartier2cebb242015-04-21 16:50:40 -070081 soa.AddLocalReference<jobject>(
82 reinterpret_cast<Object*>(args[0])));
Ian Rogers556d6372012-11-20 12:19:36 -080083 jobject jresult;
84 {
85 ScopedThreadStateChange tsc(self, kNative);
86 jresult = fn(soa.Env(), klass.get(), arg0.get());
87 }
88 result->SetL(soa.Decode<Object*>(jresult));
Ian Rogers64b6d142012-10-29 16:34:15 -070089 } else if (shorty == "IIZ") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +010090 typedef jint (fntype)(JNIEnv*, jclass, jint, jboolean);
Mathieu Chartier2d721012014-11-10 11:08:06 -080091 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -070092 ScopedLocalRef<jclass> klass(soa.Env(),
93 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
94 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -080095 result->SetI(fn(soa.Env(), klass.get(), args[0], args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -070096 } else if (shorty == "ILI") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +010097 typedef jint (fntype)(JNIEnv*, jclass, jobject, jint);
Mathieu Chartier2d721012014-11-10 11:08:06 -080098 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(
99 method->GetEntryPointFromJni()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700100 ScopedLocalRef<jclass> klass(soa.Env(),
101 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
102 ScopedLocalRef<jobject> arg0(soa.Env(),
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700103 soa.AddLocalReference<jobject>(
104 reinterpret_cast<Object*>(args[0])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700105 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800106 result->SetI(fn(soa.Env(), klass.get(), arg0.get(), args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700107 } else if (shorty == "SIZ") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100108 typedef jshort (fntype)(JNIEnv*, jclass, jint, jboolean);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700109 fntype* const fn =
110 reinterpret_cast<fntype*>(const_cast<void*>(method->GetEntryPointFromJni()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700111 ScopedLocalRef<jclass> klass(soa.Env(),
112 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
113 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800114 result->SetS(fn(soa.Env(), klass.get(), args[0], args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700115 } else if (shorty == "VIZ") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100116 typedef void (fntype)(JNIEnv*, jclass, jint, jboolean);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800117 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700118 ScopedLocalRef<jclass> klass(soa.Env(),
119 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
120 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800121 fn(soa.Env(), klass.get(), args[0], args[1]);
Ian Rogers64b6d142012-10-29 16:34:15 -0700122 } else if (shorty == "ZLL") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100123 typedef jboolean (fntype)(JNIEnv*, jclass, jobject, jobject);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800124 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700125 ScopedLocalRef<jclass> klass(soa.Env(),
126 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
127 ScopedLocalRef<jobject> arg0(soa.Env(),
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700128 soa.AddLocalReference<jobject>(
129 reinterpret_cast<Object*>(args[0])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700130 ScopedLocalRef<jobject> arg1(soa.Env(),
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700131 soa.AddLocalReference<jobject>(
132 reinterpret_cast<Object*>(args[1])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700133 ScopedThreadStateChange tsc(self, kNative);
134 result->SetZ(fn(soa.Env(), klass.get(), arg0.get(), arg1.get()));
135 } else if (shorty == "ZILL") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100136 typedef jboolean (fntype)(JNIEnv*, jclass, jint, jobject, jobject);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800137 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700138 ScopedLocalRef<jclass> klass(soa.Env(),
139 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
140 ScopedLocalRef<jobject> arg1(soa.Env(),
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700141 soa.AddLocalReference<jobject>(
142 reinterpret_cast<Object*>(args[1])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700143 ScopedLocalRef<jobject> arg2(soa.Env(),
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700144 soa.AddLocalReference<jobject>(
145 reinterpret_cast<Object*>(args[2])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700146 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800147 result->SetZ(fn(soa.Env(), klass.get(), args[0], arg1.get(), arg2.get()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700148 } else if (shorty == "VILII") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100149 typedef void (fntype)(JNIEnv*, jclass, jint, jobject, jint, jint);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800150 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700151 ScopedLocalRef<jclass> klass(soa.Env(),
152 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
153 ScopedLocalRef<jobject> arg1(soa.Env(),
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700154 soa.AddLocalReference<jobject>(
155 reinterpret_cast<Object*>(args[1])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700156 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800157 fn(soa.Env(), klass.get(), args[0], arg1.get(), args[2], args[3]);
Ian Rogers64b6d142012-10-29 16:34:15 -0700158 } else if (shorty == "VLILII") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100159 typedef void (fntype)(JNIEnv*, jclass, jobject, jint, jobject, jint, jint);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800160 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700161 ScopedLocalRef<jclass> klass(soa.Env(),
162 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
163 ScopedLocalRef<jobject> arg0(soa.Env(),
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700164 soa.AddLocalReference<jobject>(
165 reinterpret_cast<Object*>(args[0])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700166 ScopedLocalRef<jobject> arg2(soa.Env(),
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700167 soa.AddLocalReference<jobject>(
168 reinterpret_cast<Object*>(args[2])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700169 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800170 fn(soa.Env(), klass.get(), arg0.get(), args[1], arg2.get(), args[3], args[4]);
Ian Rogers64b6d142012-10-29 16:34:15 -0700171 } else {
172 LOG(FATAL) << "Do something with static native method: " << PrettyMethod(method)
173 << " shorty: " << shorty;
174 }
175 } else {
176 if (shorty == "L") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100177 typedef jobject (fntype)(JNIEnv*, jobject);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800178 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700179 ScopedLocalRef<jobject> rcvr(soa.Env(),
180 soa.AddLocalReference<jobject>(receiver));
Ian Rogers556d6372012-11-20 12:19:36 -0800181 jobject jresult;
182 {
183 ScopedThreadStateChange tsc(self, kNative);
184 jresult = fn(soa.Env(), rcvr.get());
185 }
186 result->SetL(soa.Decode<Object*>(jresult));
Jeff Hao3dd9f762013-07-08 13:09:25 -0700187 } else if (shorty == "V") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100188 typedef void (fntype)(JNIEnv*, jobject);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800189 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Jeff Hao3dd9f762013-07-08 13:09:25 -0700190 ScopedLocalRef<jobject> rcvr(soa.Env(),
191 soa.AddLocalReference<jobject>(receiver));
192 ScopedThreadStateChange tsc(self, kNative);
193 fn(soa.Env(), rcvr.get());
Ian Rogers64b6d142012-10-29 16:34:15 -0700194 } else if (shorty == "LL") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100195 typedef jobject (fntype)(JNIEnv*, jobject, jobject);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800196 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700197 ScopedLocalRef<jobject> rcvr(soa.Env(),
198 soa.AddLocalReference<jobject>(receiver));
199 ScopedLocalRef<jobject> arg0(soa.Env(),
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700200 soa.AddLocalReference<jobject>(
201 reinterpret_cast<Object*>(args[0])));
Ian Rogers556d6372012-11-20 12:19:36 -0800202 jobject jresult;
203 {
204 ScopedThreadStateChange tsc(self, kNative);
205 jresult = fn(soa.Env(), rcvr.get(), arg0.get());
Ian Rogers556d6372012-11-20 12:19:36 -0800206 }
207 result->SetL(soa.Decode<Object*>(jresult));
Ian Rogers64b6d142012-10-29 16:34:15 -0700208 ScopedThreadStateChange tsc(self, kNative);
Ian Rogers64b6d142012-10-29 16:34:15 -0700209 } else if (shorty == "III") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100210 typedef jint (fntype)(JNIEnv*, jobject, jint, jint);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800211 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700212 ScopedLocalRef<jobject> rcvr(soa.Env(),
213 soa.AddLocalReference<jobject>(receiver));
214 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800215 result->SetI(fn(soa.Env(), rcvr.get(), args[0], args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700216 } else {
217 LOG(FATAL) << "Do something with native method: " << PrettyMethod(method)
218 << " shorty: " << shorty;
219 }
220 }
221}
222
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200223enum InterpreterImplKind {
Ian Rogersb48b9eb2014-02-28 16:20:21 -0800224 kSwitchImpl, // Switch-based interpreter implementation.
225 kComputedGotoImplKind // Computed-goto-based interpreter implementation.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200226};
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800227static std::ostream& operator<<(std::ostream& os, const InterpreterImplKind& rhs) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700228 os << ((rhs == kSwitchImpl) ? "Switch-based interpreter" : "Computed-goto-based interpreter");
229 return os;
230}
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700231
Sebastien Hertzfa888d02014-09-30 12:00:11 +0200232#if !defined(__clang__)
Ian Rogersb48b9eb2014-02-28 16:20:21 -0800233static constexpr InterpreterImplKind kInterpreterImplKind = kComputedGotoImplKind;
Sebastien Hertzfa888d02014-09-30 12:00:11 +0200234#else
235// Clang 3.4 fails to build the goto interpreter implementation.
236static constexpr InterpreterImplKind kInterpreterImplKind = kSwitchImpl;
237template<bool do_access_check, bool transaction_active>
Ian Rogerse94652f2014-12-02 11:13:19 -0800238JValue ExecuteGotoImpl(Thread*, const DexFile::CodeItem*, ShadowFrame&, JValue) {
Sebastien Hertzfa888d02014-09-30 12:00:11 +0200239 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700240 UNREACHABLE();
Sebastien Hertzfa888d02014-09-30 12:00:11 +0200241}
242// Explicit definitions of ExecuteGotoImpl.
243template<> SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogerse94652f2014-12-02 11:13:19 -0800244JValue ExecuteGotoImpl<true, false>(Thread* self, const DexFile::CodeItem* code_item,
Sebastien Hertzfa888d02014-09-30 12:00:11 +0200245 ShadowFrame& shadow_frame, JValue result_register);
246template<> SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogerse94652f2014-12-02 11:13:19 -0800247JValue ExecuteGotoImpl<false, false>(Thread* self, const DexFile::CodeItem* code_item,
Sebastien Hertzfa888d02014-09-30 12:00:11 +0200248 ShadowFrame& shadow_frame, JValue result_register);
249template<> SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogerse94652f2014-12-02 11:13:19 -0800250JValue ExecuteGotoImpl<true, true>(Thread* self, const DexFile::CodeItem* code_item,
251 ShadowFrame& shadow_frame, JValue result_register);
252template<> SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
253JValue ExecuteGotoImpl<false, true>(Thread* self, const DexFile::CodeItem* code_item,
Sebastien Hertzfa888d02014-09-30 12:00:11 +0200254 ShadowFrame& shadow_frame, JValue result_register);
Sebastien Hertzfa888d02014-09-30 12:00:11 +0200255#endif
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700256
Ian Rogerse94652f2014-12-02 11:13:19 -0800257static JValue Execute(Thread* self, const DexFile::CodeItem* code_item, ShadowFrame& shadow_frame,
258 JValue result_register)
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200259 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
260
Ian Rogerse94652f2014-12-02 11:13:19 -0800261static inline JValue Execute(Thread* self, const DexFile::CodeItem* code_item,
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200262 ShadowFrame& shadow_frame, JValue result_register) {
Ian Rogers848871b2013-08-05 10:56:33 -0700263 DCHECK(!shadow_frame.GetMethod()->IsAbstract());
264 DCHECK(!shadow_frame.GetMethod()->IsNative());
Sebastien Hertz4e99b3d2014-06-24 14:35:40 +0200265 shadow_frame.GetMethod()->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200266
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100267 bool transaction_active = Runtime::Current()->IsActiveTransaction();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200268 if (LIKELY(shadow_frame.GetMethod()->IsPreverified())) {
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200269 // Enter the "without access check" interpreter.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200270 if (kInterpreterImplKind == kSwitchImpl) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100271 if (transaction_active) {
Ian Rogerse94652f2014-12-02 11:13:19 -0800272 return ExecuteSwitchImpl<false, true>(self, code_item, shadow_frame, result_register);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100273 } else {
Ian Rogerse94652f2014-12-02 11:13:19 -0800274 return ExecuteSwitchImpl<false, false>(self, code_item, shadow_frame, result_register);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100275 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200276 } else {
277 DCHECK_EQ(kInterpreterImplKind, kComputedGotoImplKind);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100278 if (transaction_active) {
Ian Rogerse94652f2014-12-02 11:13:19 -0800279 return ExecuteGotoImpl<false, true>(self, code_item, shadow_frame, result_register);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100280 } else {
Ian Rogerse94652f2014-12-02 11:13:19 -0800281 return ExecuteGotoImpl<false, false>(self, code_item, shadow_frame, result_register);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100282 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200283 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200284 } else {
285 // Enter the "with access check" interpreter.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200286 if (kInterpreterImplKind == kSwitchImpl) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100287 if (transaction_active) {
Ian Rogerse94652f2014-12-02 11:13:19 -0800288 return ExecuteSwitchImpl<true, true>(self, code_item, shadow_frame, result_register);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100289 } else {
Ian Rogerse94652f2014-12-02 11:13:19 -0800290 return ExecuteSwitchImpl<true, false>(self, code_item, shadow_frame, result_register);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100291 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200292 } else {
293 DCHECK_EQ(kInterpreterImplKind, kComputedGotoImplKind);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100294 if (transaction_active) {
Ian Rogerse94652f2014-12-02 11:13:19 -0800295 return ExecuteGotoImpl<true, true>(self, code_item, shadow_frame, result_register);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100296 } else {
Ian Rogerse94652f2014-12-02 11:13:19 -0800297 return ExecuteGotoImpl<true, false>(self, code_item, shadow_frame, result_register);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100298 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200299 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200300 }
301}
302
Brian Carlstromea46f952013-07-30 01:26:50 -0700303void EnterInterpreterFromInvoke(Thread* self, ArtMethod* method, Object* receiver,
Jeff Hao6474d192013-03-26 14:08:09 -0700304 uint32_t* args, JValue* result) {
Ian Rogers64b6d142012-10-29 16:34:15 -0700305 DCHECK_EQ(self, Thread::Current());
Nicolas Geoffray535a3fb2014-07-22 15:17:38 +0100306 bool implicit_check = !Runtime::Current()->ExplicitStackOverflowChecks();
307 if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEndForInterpreter(implicit_check))) {
jeffhaod7521322012-11-21 15:38:24 -0800308 ThrowStackOverflowError(self);
309 return;
310 }
311
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700312 const char* old_cause = self->StartAssertNoThreadSuspension("EnterInterpreterFromInvoke");
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700313 const DexFile::CodeItem* code_item = method->GetCodeItem();
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700314 uint16_t num_regs;
315 uint16_t num_ins;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700316 if (code_item != nullptr) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700317 num_regs = code_item->registers_size_;
318 num_ins = code_item->ins_size_;
jeffhao0a9bb732012-11-26 12:28:49 -0800319 } else if (method->IsAbstract()) {
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700320 self->EndAssertNoThreadSuspension(old_cause);
Sebastien Hertz56adf602013-07-09 17:27:07 +0200321 ThrowAbstractMethodError(method);
jeffhao0a9bb732012-11-26 12:28:49 -0800322 return;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700323 } else {
324 DCHECK(method->IsNative());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700325 num_regs = num_ins = ArtMethod::NumArgRegisters(method->GetShorty());
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700326 if (!method->IsStatic()) {
327 num_regs++;
328 num_ins++;
329 }
330 }
331 // Set up shadow frame with matching number of reference slots to vregs.
332 ShadowFrame* last_shadow_frame = self->GetManagedStack()->GetTopShadowFrame();
Jeff Hao66135192013-05-14 11:02:41 -0700333 void* memory = alloca(ShadowFrame::ComputeSize(num_regs));
334 ShadowFrame* shadow_frame(ShadowFrame::Create(num_regs, last_shadow_frame, method, 0, memory));
335 self->PushShadowFrame(shadow_frame);
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700336
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700337 size_t cur_reg = num_regs - num_ins;
338 if (!method->IsStatic()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700339 CHECK(receiver != nullptr);
TDYa127ce4cc0d2012-11-18 16:59:53 -0800340 shadow_frame->SetVRegReference(cur_reg, receiver);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700341 ++cur_reg;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700342 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700343 uint32_t shorty_len = 0;
344 const char* shorty = method->GetShorty(&shorty_len);
Jeff Hao5d917302013-02-27 17:57:33 -0800345 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 -0700346 DCHECK_LT(shorty_pos + 1, shorty_len);
Jeff Hao5d917302013-02-27 17:57:33 -0800347 switch (shorty[shorty_pos + 1]) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700348 case 'L': {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800349 Object* o = reinterpret_cast<StackReference<Object>*>(&args[arg_pos])->AsMirrorPtr();
TDYa127ce4cc0d2012-11-18 16:59:53 -0800350 shadow_frame->SetVRegReference(cur_reg, o);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700351 break;
352 }
Jeff Hao5d917302013-02-27 17:57:33 -0800353 case 'J': case 'D': {
354 uint64_t wide_value = (static_cast<uint64_t>(args[arg_pos + 1]) << 32) | args[arg_pos];
355 shadow_frame->SetVRegLong(cur_reg, wide_value);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700356 cur_reg++;
Jeff Hao5d917302013-02-27 17:57:33 -0800357 arg_pos++;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700358 break;
Jeff Hao5d917302013-02-27 17:57:33 -0800359 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700360 default:
Jeff Hao5d917302013-02-27 17:57:33 -0800361 shadow_frame->SetVReg(cur_reg, args[arg_pos]);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700362 break;
363 }
364 }
Mathieu Chartier92246bb2014-02-25 18:22:39 -0800365 self->EndAssertNoThreadSuspension(old_cause);
366 // Do this after populating the shadow frame in case EnsureInitialized causes a GC.
Ian Rogers6c5cb212014-06-18 16:07:20 -0700367 if (method->IsStatic() && UNLIKELY(!method->GetDeclaringClass()->IsInitialized())) {
Mathieu Chartier92246bb2014-02-25 18:22:39 -0800368 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700369 StackHandleScope<1> hs(self);
370 Handle<mirror::Class> h_class(hs.NewHandle(method->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700371 if (UNLIKELY(!class_linker->EnsureInitialized(self, h_class, true, true))) {
Mathieu Chartier92246bb2014-02-25 18:22:39 -0800372 CHECK(self->IsExceptionPending());
373 self->PopShadowFrame();
374 return;
375 }
376 }
Ian Rogers64b6d142012-10-29 16:34:15 -0700377 if (LIKELY(!method->IsNative())) {
Ian Rogerse94652f2014-12-02 11:13:19 -0800378 JValue r = Execute(self, code_item, *shadow_frame, JValue());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700379 if (result != nullptr) {
Jeff Hao6474d192013-03-26 14:08:09 -0700380 *result = r;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700381 }
382 } else {
Ian Rogers64b6d142012-10-29 16:34:15 -0700383 // We don't expect to be asked to interpret native code (which is entered via a JNI compiler
384 // generated stub) except during testing and image writing.
Mathieu Chartier92246bb2014-02-25 18:22:39 -0800385 // Update args to be the args in the shadow frame since the input ones could hold stale
386 // references pointers due to moving GC.
387 args = shadow_frame->GetVRegArgs(method->IsStatic() ? 0 : 1);
Ian Rogers64b6d142012-10-29 16:34:15 -0700388 if (!Runtime::Current()->IsStarted()) {
Jeff Hao6474d192013-03-26 14:08:09 -0700389 UnstartedRuntimeJni(self, method, receiver, args, result);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700390 } else {
Jeff Hao6474d192013-03-26 14:08:09 -0700391 InterpreterJni(self, method, shorty, receiver, args, result);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700392 }
393 }
394 self->PopShadowFrame();
395}
396
Ian Rogers62d6c772013-02-27 08:32:07 -0800397void EnterInterpreterFromDeoptimize(Thread* self, ShadowFrame* shadow_frame, JValue* ret_val)
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800398 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
399 JValue value;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700400 // Set value to last known result in case the shadow frame chain is empty.
401 value.SetJ(ret_val->GetJ());
402 while (shadow_frame != nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800403 self->SetTopOfShadowStack(shadow_frame);
Ian Rogerse94652f2014-12-02 11:13:19 -0800404 const DexFile::CodeItem* code_item = shadow_frame->GetMethod()->GetCodeItem();
Sebastien Hertz270a0e12015-01-16 19:49:09 +0100405 const uint32_t dex_pc = shadow_frame->GetDexPC();
406 uint32_t new_dex_pc;
407 if (UNLIKELY(self->IsExceptionPending())) {
408 const instrumentation::Instrumentation* const instrumentation =
409 Runtime::Current()->GetInstrumentation();
410 uint32_t found_dex_pc = FindNextInstructionFollowingException(self, *shadow_frame, dex_pc,
411 instrumentation);
412 new_dex_pc = found_dex_pc; // the dex pc of a matching catch handler
413 // or DexFile::kDexNoIndex if there is none.
414 } else {
415 const Instruction* instr = Instruction::At(&code_item->insns_[dex_pc]);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700416 // For an invoke, use the dex pc of the next instruction.
417 // TODO: should be tested more once b/17586779 is fixed.
418 new_dex_pc = dex_pc + (instr->IsInvoke() ? instr->SizeInCodeUnits() : 0);
Sebastien Hertz270a0e12015-01-16 19:49:09 +0100419 }
420 if (new_dex_pc != DexFile::kDexNoIndex) {
421 shadow_frame->SetDexPC(new_dex_pc);
422 value = Execute(self, code_item, *shadow_frame, value);
423 }
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800424 ShadowFrame* old_frame = shadow_frame;
425 shadow_frame = shadow_frame->GetLink();
Christopher Ferris241a9582015-04-27 15:19:41 -0700426 ShadowFrame::DeleteDeoptimizedFrame(old_frame);
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800427 }
428 ret_val->SetJ(value.GetJ());
429}
430
Ian Rogerse94652f2014-12-02 11:13:19 -0800431JValue EnterInterpreterFromEntryPoint(Thread* self, const DexFile::CodeItem* code_item,
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700432 ShadowFrame* shadow_frame) {
Ian Rogersf3e98552013-03-20 15:49:49 -0700433 DCHECK_EQ(self, Thread::Current());
Nicolas Geoffray535a3fb2014-07-22 15:17:38 +0100434 bool implicit_check = !Runtime::Current()->ExplicitStackOverflowChecks();
435 if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEndForInterpreter(implicit_check))) {
Ian Rogersf3e98552013-03-20 15:49:49 -0700436 ThrowStackOverflowError(self);
437 return JValue();
438 }
439
Ian Rogerse94652f2014-12-02 11:13:19 -0800440 return Execute(self, code_item, *shadow_frame, JValue());
Ian Rogers7db619b2013-01-16 18:35:48 -0800441}
442
Ian Rogerse94652f2014-12-02 11:13:19 -0800443extern "C" void artInterpreterToInterpreterBridge(Thread* self, const DexFile::CodeItem* code_item,
Ian Rogers848871b2013-08-05 10:56:33 -0700444 ShadowFrame* shadow_frame, JValue* result) {
Nicolas Geoffray535a3fb2014-07-22 15:17:38 +0100445 bool implicit_check = !Runtime::Current()->ExplicitStackOverflowChecks();
446 if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEndForInterpreter(implicit_check))) {
Jeff Hao16743632013-05-08 10:59:04 -0700447 ThrowStackOverflowError(self);
Jeff Hao69510672013-05-21 17:34:55 -0700448 return;
Jeff Hao16743632013-05-08 10:59:04 -0700449 }
450
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700451 self->PushShadowFrame(shadow_frame);
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200452 // Ensure static methods are initialized.
Ian Rogerse94652f2014-12-02 11:13:19 -0800453 const bool is_static = shadow_frame->GetMethod()->IsStatic();
454 if (is_static) {
455 mirror::Class* declaring_class = shadow_frame->GetMethod()->GetDeclaringClass();
Ian Rogers6c5cb212014-06-18 16:07:20 -0700456 if (UNLIKELY(!declaring_class->IsInitialized())) {
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700457 StackHandleScope<1> hs(self);
458 HandleWrapper<Class> h_declaring_class(hs.NewHandleWrapper(&declaring_class));
459 if (UNLIKELY(!Runtime::Current()->GetClassLinker()->EnsureInitialized(
Ian Rogers7b078e82014-09-10 14:44:24 -0700460 self, h_declaring_class, true, true))) {
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700461 DCHECK(self->IsExceptionPending());
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700462 self->PopShadowFrame();
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200463 return;
464 }
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700465 CHECK(h_declaring_class->IsInitializing());
Jeff Hao16743632013-05-08 10:59:04 -0700466 }
Jeff Hao16743632013-05-08 10:59:04 -0700467 }
Jeff Hao16743632013-05-08 10:59:04 -0700468
Ian Rogerse94652f2014-12-02 11:13:19 -0800469 if (LIKELY(!shadow_frame->GetMethod()->IsNative())) {
470 result->SetJ(Execute(self, code_item, *shadow_frame, JValue()).GetJ());
Jeff Hao16743632013-05-08 10:59:04 -0700471 } else {
472 // We don't expect to be asked to interpret native code (which is entered via a JNI compiler
473 // generated stub) except during testing and image writing.
474 CHECK(!Runtime::Current()->IsStarted());
Ian Rogerse94652f2014-12-02 11:13:19 -0800475 Object* receiver = is_static ? nullptr : shadow_frame->GetVRegReference(0);
476 uint32_t* args = shadow_frame->GetVRegArgs(is_static ? 0 : 1);
477 UnstartedRuntimeJni(self, shadow_frame->GetMethod(), receiver, args, result);
Jeff Hao16743632013-05-08 10:59:04 -0700478 }
479
480 self->PopShadowFrame();
Jeff Hao16743632013-05-08 10:59:04 -0700481}
482
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700483} // namespace interpreter
484} // namespace art