blob: b010504e7d0d56a7ad376eecfdaba2a7c3f65bd2 [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 Gampe3cfa4d02015-10-06 17:04:01 -070021#include "interpreter_common.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070022#include "mirror/string-inl.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070023#include "scoped_thread_state_change.h"
24#include "ScopedLocalRef.h"
Andreas Gampeb3025922015-09-01 14:45:00 -070025#include "stack.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070026#include "unstarted_runtime.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070027
Ian Rogers2fa6b2e2012-10-17 00:10:17 -070028namespace art {
29namespace interpreter {
30
Ian Rogersfc0e94b2013-09-23 23:51:32 -070031static void InterpreterJni(Thread* self, ArtMethod* method, const StringPiece& shorty,
Jeff Hao5d917302013-02-27 17:57:33 -080032 Object* receiver, uint32_t* args, JValue* result)
Mathieu Chartier90443472015-07-16 20:32:27 -070033 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers64b6d142012-10-29 16:34:15 -070034 // TODO: The following enters JNI code using a typedef-ed function rather than the JNI compiler,
35 // it should be removed and JNI compiled stubs used instead.
36 ScopedObjectAccessUnchecked soa(self);
37 if (method->IsStatic()) {
38 if (shorty == "L") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +010039 typedef jobject (fntype)(JNIEnv*, jclass);
Mathieu Chartier2d721012014-11-10 11:08:06 -080040 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -070041 ScopedLocalRef<jclass> klass(soa.Env(),
42 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
Ian Rogers556d6372012-11-20 12:19:36 -080043 jobject jresult;
44 {
45 ScopedThreadStateChange tsc(self, kNative);
46 jresult = fn(soa.Env(), klass.get());
47 }
48 result->SetL(soa.Decode<Object*>(jresult));
Ian Rogers64b6d142012-10-29 16:34:15 -070049 } else if (shorty == "V") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +010050 typedef void (fntype)(JNIEnv*, jclass);
Mathieu Chartier2d721012014-11-10 11:08:06 -080051 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -070052 ScopedLocalRef<jclass> klass(soa.Env(),
53 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
54 ScopedThreadStateChange tsc(self, kNative);
55 fn(soa.Env(), klass.get());
56 } else if (shorty == "Z") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +010057 typedef jboolean (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 result->SetZ(fn(soa.Env(), klass.get()));
63 } else if (shorty == "BI") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +010064 typedef jbyte (fntype)(JNIEnv*, jclass, jint);
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);
Jeff Hao5d917302013-02-27 17:57:33 -080069 result->SetB(fn(soa.Env(), klass.get(), args[0]));
Ian Rogers64b6d142012-10-29 16:34:15 -070070 } else if (shorty == "II") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +010071 typedef jint (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->SetI(fn(soa.Env(), klass.get(), args[0]));
Ian Rogers64b6d142012-10-29 16:34:15 -070077 } else if (shorty == "LL") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +010078 typedef jobject (fntype)(JNIEnv*, jclass, jobject);
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 ScopedLocalRef<jobject> arg0(soa.Env(),
Mathieu Chartier2cebb242015-04-21 16:50:40 -070083 soa.AddLocalReference<jobject>(
84 reinterpret_cast<Object*>(args[0])));
Ian Rogers556d6372012-11-20 12:19:36 -080085 jobject jresult;
86 {
87 ScopedThreadStateChange tsc(self, kNative);
88 jresult = fn(soa.Env(), klass.get(), arg0.get());
89 }
90 result->SetL(soa.Decode<Object*>(jresult));
Ian Rogers64b6d142012-10-29 16:34:15 -070091 } else if (shorty == "IIZ") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +010092 typedef jint (fntype)(JNIEnv*, jclass, jint, jboolean);
Mathieu Chartier2d721012014-11-10 11:08:06 -080093 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -070094 ScopedLocalRef<jclass> klass(soa.Env(),
95 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
96 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -080097 result->SetI(fn(soa.Env(), klass.get(), args[0], args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -070098 } else if (shorty == "ILI") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +010099 typedef jint (fntype)(JNIEnv*, jclass, jobject, jint);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800100 fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(
101 method->GetEntryPointFromJni()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700102 ScopedLocalRef<jclass> klass(soa.Env(),
103 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
104 ScopedLocalRef<jobject> arg0(soa.Env(),
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700105 soa.AddLocalReference<jobject>(
106 reinterpret_cast<Object*>(args[0])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700107 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800108 result->SetI(fn(soa.Env(), klass.get(), arg0.get(), args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700109 } else if (shorty == "SIZ") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100110 typedef jshort (fntype)(JNIEnv*, jclass, jint, jboolean);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700111 fntype* const fn =
112 reinterpret_cast<fntype*>(const_cast<void*>(method->GetEntryPointFromJni()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700113 ScopedLocalRef<jclass> klass(soa.Env(),
114 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
115 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800116 result->SetS(fn(soa.Env(), klass.get(), args[0], args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700117 } else if (shorty == "VIZ") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100118 typedef void (fntype)(JNIEnv*, jclass, jint, jboolean);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800119 fntype* const fn = reinterpret_cast<fntype*>(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 fn(soa.Env(), klass.get(), args[0], args[1]);
Ian Rogers64b6d142012-10-29 16:34:15 -0700124 } else if (shorty == "ZLL") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100125 typedef jboolean (fntype)(JNIEnv*, jclass, jobject, jobject);
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 ScopedLocalRef<jobject> arg0(soa.Env(),
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700130 soa.AddLocalReference<jobject>(
131 reinterpret_cast<Object*>(args[0])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700132 ScopedLocalRef<jobject> arg1(soa.Env(),
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700133 soa.AddLocalReference<jobject>(
134 reinterpret_cast<Object*>(args[1])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700135 ScopedThreadStateChange tsc(self, kNative);
136 result->SetZ(fn(soa.Env(), klass.get(), arg0.get(), arg1.get()));
137 } else if (shorty == "ZILL") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100138 typedef jboolean (fntype)(JNIEnv*, jclass, jint, jobject, jobject);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800139 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700140 ScopedLocalRef<jclass> klass(soa.Env(),
141 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
142 ScopedLocalRef<jobject> arg1(soa.Env(),
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700143 soa.AddLocalReference<jobject>(
144 reinterpret_cast<Object*>(args[1])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700145 ScopedLocalRef<jobject> arg2(soa.Env(),
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700146 soa.AddLocalReference<jobject>(
147 reinterpret_cast<Object*>(args[2])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700148 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800149 result->SetZ(fn(soa.Env(), klass.get(), args[0], arg1.get(), arg2.get()));
Ian Rogers64b6d142012-10-29 16:34:15 -0700150 } else if (shorty == "VILII") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100151 typedef void (fntype)(JNIEnv*, jclass, jint, jobject, jint, jint);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800152 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700153 ScopedLocalRef<jclass> klass(soa.Env(),
154 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
155 ScopedLocalRef<jobject> arg1(soa.Env(),
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700156 soa.AddLocalReference<jobject>(
157 reinterpret_cast<Object*>(args[1])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700158 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800159 fn(soa.Env(), klass.get(), args[0], arg1.get(), args[2], args[3]);
Ian Rogers64b6d142012-10-29 16:34:15 -0700160 } else if (shorty == "VLILII") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100161 typedef void (fntype)(JNIEnv*, jclass, jobject, jint, jobject, jint, jint);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800162 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700163 ScopedLocalRef<jclass> klass(soa.Env(),
164 soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
165 ScopedLocalRef<jobject> arg0(soa.Env(),
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700166 soa.AddLocalReference<jobject>(
167 reinterpret_cast<Object*>(args[0])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700168 ScopedLocalRef<jobject> arg2(soa.Env(),
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700169 soa.AddLocalReference<jobject>(
170 reinterpret_cast<Object*>(args[2])));
Ian Rogers64b6d142012-10-29 16:34:15 -0700171 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800172 fn(soa.Env(), klass.get(), arg0.get(), args[1], arg2.get(), args[3], args[4]);
Ian Rogers64b6d142012-10-29 16:34:15 -0700173 } else {
174 LOG(FATAL) << "Do something with static native method: " << PrettyMethod(method)
175 << " shorty: " << shorty;
176 }
177 } else {
178 if (shorty == "L") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100179 typedef jobject (fntype)(JNIEnv*, jobject);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800180 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700181 ScopedLocalRef<jobject> rcvr(soa.Env(),
182 soa.AddLocalReference<jobject>(receiver));
Ian Rogers556d6372012-11-20 12:19:36 -0800183 jobject jresult;
184 {
185 ScopedThreadStateChange tsc(self, kNative);
186 jresult = fn(soa.Env(), rcvr.get());
187 }
188 result->SetL(soa.Decode<Object*>(jresult));
Jeff Hao3dd9f762013-07-08 13:09:25 -0700189 } else if (shorty == "V") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100190 typedef void (fntype)(JNIEnv*, jobject);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800191 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Jeff Hao3dd9f762013-07-08 13:09:25 -0700192 ScopedLocalRef<jobject> rcvr(soa.Env(),
193 soa.AddLocalReference<jobject>(receiver));
194 ScopedThreadStateChange tsc(self, kNative);
195 fn(soa.Env(), rcvr.get());
Ian Rogers64b6d142012-10-29 16:34:15 -0700196 } else if (shorty == "LL") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100197 typedef jobject (fntype)(JNIEnv*, jobject, jobject);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800198 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700199 ScopedLocalRef<jobject> rcvr(soa.Env(),
200 soa.AddLocalReference<jobject>(receiver));
201 ScopedLocalRef<jobject> arg0(soa.Env(),
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700202 soa.AddLocalReference<jobject>(
203 reinterpret_cast<Object*>(args[0])));
Ian Rogers556d6372012-11-20 12:19:36 -0800204 jobject jresult;
205 {
206 ScopedThreadStateChange tsc(self, kNative);
207 jresult = fn(soa.Env(), rcvr.get(), arg0.get());
Ian Rogers556d6372012-11-20 12:19:36 -0800208 }
209 result->SetL(soa.Decode<Object*>(jresult));
Ian Rogers64b6d142012-10-29 16:34:15 -0700210 ScopedThreadStateChange tsc(self, kNative);
Ian Rogers64b6d142012-10-29 16:34:15 -0700211 } else if (shorty == "III") {
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100212 typedef jint (fntype)(JNIEnv*, jobject, jint, jint);
Mathieu Chartier2d721012014-11-10 11:08:06 -0800213 fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
Ian Rogers64b6d142012-10-29 16:34:15 -0700214 ScopedLocalRef<jobject> rcvr(soa.Env(),
215 soa.AddLocalReference<jobject>(receiver));
216 ScopedThreadStateChange tsc(self, kNative);
Jeff Hao5d917302013-02-27 17:57:33 -0800217 result->SetI(fn(soa.Env(), rcvr.get(), args[0], args[1]));
Ian Rogers64b6d142012-10-29 16:34:15 -0700218 } else {
219 LOG(FATAL) << "Do something with native method: " << PrettyMethod(method)
220 << " shorty: " << shorty;
221 }
222 }
223}
224
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200225enum InterpreterImplKind {
Ian Rogersb48b9eb2014-02-28 16:20:21 -0800226 kSwitchImpl, // Switch-based interpreter implementation.
227 kComputedGotoImplKind // Computed-goto-based interpreter implementation.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200228};
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800229static std::ostream& operator<<(std::ostream& os, const InterpreterImplKind& rhs) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700230 os << ((rhs == kSwitchImpl) ? "Switch-based interpreter" : "Computed-goto-based interpreter");
231 return os;
232}
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700233
Sebastien Hertzfa888d02014-09-30 12:00:11 +0200234#if !defined(__clang__)
Ian Rogersb48b9eb2014-02-28 16:20:21 -0800235static constexpr InterpreterImplKind kInterpreterImplKind = kComputedGotoImplKind;
Sebastien Hertzfa888d02014-09-30 12:00:11 +0200236#else
237// Clang 3.4 fails to build the goto interpreter implementation.
238static constexpr InterpreterImplKind kInterpreterImplKind = kSwitchImpl;
239template<bool do_access_check, bool transaction_active>
Ian Rogerse94652f2014-12-02 11:13:19 -0800240JValue ExecuteGotoImpl(Thread*, const DexFile::CodeItem*, ShadowFrame&, JValue) {
Sebastien Hertzfa888d02014-09-30 12:00:11 +0200241 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700242 UNREACHABLE();
Sebastien Hertzfa888d02014-09-30 12:00:11 +0200243}
244// Explicit definitions of ExecuteGotoImpl.
Mathieu Chartier90443472015-07-16 20:32:27 -0700245template<> SHARED_REQUIRES(Locks::mutator_lock_)
Ian Rogerse94652f2014-12-02 11:13:19 -0800246JValue ExecuteGotoImpl<true, false>(Thread* self, const DexFile::CodeItem* code_item,
Sebastien Hertzfa888d02014-09-30 12:00:11 +0200247 ShadowFrame& shadow_frame, JValue result_register);
Mathieu Chartier90443472015-07-16 20:32:27 -0700248template<> SHARED_REQUIRES(Locks::mutator_lock_)
Ian Rogerse94652f2014-12-02 11:13:19 -0800249JValue ExecuteGotoImpl<false, false>(Thread* self, const DexFile::CodeItem* code_item,
Sebastien Hertzfa888d02014-09-30 12:00:11 +0200250 ShadowFrame& shadow_frame, JValue result_register);
Mathieu Chartier90443472015-07-16 20:32:27 -0700251template<> SHARED_REQUIRES(Locks::mutator_lock_)
Ian Rogerse94652f2014-12-02 11:13:19 -0800252JValue ExecuteGotoImpl<true, true>(Thread* self, const DexFile::CodeItem* code_item,
253 ShadowFrame& shadow_frame, JValue result_register);
Mathieu Chartier90443472015-07-16 20:32:27 -0700254template<> SHARED_REQUIRES(Locks::mutator_lock_)
Ian Rogerse94652f2014-12-02 11:13:19 -0800255JValue ExecuteGotoImpl<false, true>(Thread* self, const DexFile::CodeItem* code_item,
Sebastien Hertzfa888d02014-09-30 12:00:11 +0200256 ShadowFrame& shadow_frame, JValue result_register);
Sebastien Hertzfa888d02014-09-30 12:00:11 +0200257#endif
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700258
Ian Rogerse94652f2014-12-02 11:13:19 -0800259static JValue Execute(Thread* self, const DexFile::CodeItem* code_item, ShadowFrame& shadow_frame,
260 JValue result_register)
Mathieu Chartier90443472015-07-16 20:32:27 -0700261 SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200262
Ian Rogerse94652f2014-12-02 11:13:19 -0800263static inline JValue Execute(Thread* self, const DexFile::CodeItem* code_item,
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200264 ShadowFrame& shadow_frame, JValue result_register) {
Ian Rogers848871b2013-08-05 10:56:33 -0700265 DCHECK(!shadow_frame.GetMethod()->IsAbstract());
266 DCHECK(!shadow_frame.GetMethod()->IsNative());
Sebastien Hertz4e99b3d2014-06-24 14:35:40 +0200267 shadow_frame.GetMethod()->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200268
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100269 bool transaction_active = Runtime::Current()->IsActiveTransaction();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200270 if (LIKELY(shadow_frame.GetMethod()->IsPreverified())) {
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200271 // Enter the "without access check" interpreter.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200272 if (kInterpreterImplKind == kSwitchImpl) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100273 if (transaction_active) {
Ian Rogerse94652f2014-12-02 11:13:19 -0800274 return ExecuteSwitchImpl<false, true>(self, code_item, shadow_frame, result_register);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100275 } else {
Ian Rogerse94652f2014-12-02 11:13:19 -0800276 return ExecuteSwitchImpl<false, false>(self, code_item, shadow_frame, result_register);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100277 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200278 } else {
279 DCHECK_EQ(kInterpreterImplKind, kComputedGotoImplKind);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100280 if (transaction_active) {
Ian Rogerse94652f2014-12-02 11:13:19 -0800281 return ExecuteGotoImpl<false, true>(self, code_item, shadow_frame, result_register);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100282 } else {
Ian Rogerse94652f2014-12-02 11:13:19 -0800283 return ExecuteGotoImpl<false, false>(self, code_item, shadow_frame, result_register);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100284 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200285 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200286 } else {
287 // Enter the "with access check" interpreter.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200288 if (kInterpreterImplKind == kSwitchImpl) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100289 if (transaction_active) {
Ian Rogerse94652f2014-12-02 11:13:19 -0800290 return ExecuteSwitchImpl<true, true>(self, code_item, shadow_frame, result_register);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100291 } else {
Ian Rogerse94652f2014-12-02 11:13:19 -0800292 return ExecuteSwitchImpl<true, false>(self, code_item, shadow_frame, result_register);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100293 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200294 } else {
295 DCHECK_EQ(kInterpreterImplKind, kComputedGotoImplKind);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100296 if (transaction_active) {
Ian Rogerse94652f2014-12-02 11:13:19 -0800297 return ExecuteGotoImpl<true, true>(self, code_item, shadow_frame, result_register);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100298 } else {
Ian Rogerse94652f2014-12-02 11:13:19 -0800299 return ExecuteGotoImpl<true, false>(self, code_item, shadow_frame, result_register);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100300 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200301 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200302 }
303}
304
Brian Carlstromea46f952013-07-30 01:26:50 -0700305void EnterInterpreterFromInvoke(Thread* self, ArtMethod* method, Object* receiver,
Jeff Hao6474d192013-03-26 14:08:09 -0700306 uint32_t* args, JValue* result) {
Ian Rogers64b6d142012-10-29 16:34:15 -0700307 DCHECK_EQ(self, Thread::Current());
Nicolas Geoffray535a3fb2014-07-22 15:17:38 +0100308 bool implicit_check = !Runtime::Current()->ExplicitStackOverflowChecks();
309 if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEndForInterpreter(implicit_check))) {
jeffhaod7521322012-11-21 15:38:24 -0800310 ThrowStackOverflowError(self);
311 return;
312 }
313
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700314 const char* old_cause = self->StartAssertNoThreadSuspension("EnterInterpreterFromInvoke");
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700315 const DexFile::CodeItem* code_item = method->GetCodeItem();
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700316 uint16_t num_regs;
317 uint16_t num_ins;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700318 if (code_item != nullptr) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700319 num_regs = code_item->registers_size_;
320 num_ins = code_item->ins_size_;
jeffhao0a9bb732012-11-26 12:28:49 -0800321 } else if (method->IsAbstract()) {
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700322 self->EndAssertNoThreadSuspension(old_cause);
Sebastien Hertz56adf602013-07-09 17:27:07 +0200323 ThrowAbstractMethodError(method);
jeffhao0a9bb732012-11-26 12:28:49 -0800324 return;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700325 } else {
326 DCHECK(method->IsNative());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700327 num_regs = num_ins = ArtMethod::NumArgRegisters(method->GetShorty());
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700328 if (!method->IsStatic()) {
329 num_regs++;
330 num_ins++;
331 }
332 }
333 // Set up shadow frame with matching number of reference slots to vregs.
334 ShadowFrame* last_shadow_frame = self->GetManagedStack()->GetTopShadowFrame();
Andreas Gampeb3025922015-09-01 14:45:00 -0700335 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
Andreas Gampe03ec9302015-08-27 17:41:47 -0700336 CREATE_SHADOW_FRAME(num_regs, last_shadow_frame, method, /* dex pc */ 0);
Andreas Gampeb3025922015-09-01 14:45:00 -0700337 ShadowFrame* shadow_frame = shadow_frame_unique_ptr.get();
Jeff Hao66135192013-05-14 11:02:41 -0700338 self->PushShadowFrame(shadow_frame);
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700339
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700340 size_t cur_reg = num_regs - num_ins;
341 if (!method->IsStatic()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700342 CHECK(receiver != nullptr);
TDYa127ce4cc0d2012-11-18 16:59:53 -0800343 shadow_frame->SetVRegReference(cur_reg, receiver);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700344 ++cur_reg;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700345 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700346 uint32_t shorty_len = 0;
347 const char* shorty = method->GetShorty(&shorty_len);
Jeff Hao5d917302013-02-27 17:57:33 -0800348 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 -0700349 DCHECK_LT(shorty_pos + 1, shorty_len);
Jeff Hao5d917302013-02-27 17:57:33 -0800350 switch (shorty[shorty_pos + 1]) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700351 case 'L': {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800352 Object* o = reinterpret_cast<StackReference<Object>*>(&args[arg_pos])->AsMirrorPtr();
TDYa127ce4cc0d2012-11-18 16:59:53 -0800353 shadow_frame->SetVRegReference(cur_reg, o);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700354 break;
355 }
Jeff Hao5d917302013-02-27 17:57:33 -0800356 case 'J': case 'D': {
357 uint64_t wide_value = (static_cast<uint64_t>(args[arg_pos + 1]) << 32) | args[arg_pos];
358 shadow_frame->SetVRegLong(cur_reg, wide_value);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700359 cur_reg++;
Jeff Hao5d917302013-02-27 17:57:33 -0800360 arg_pos++;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700361 break;
Jeff Hao5d917302013-02-27 17:57:33 -0800362 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700363 default:
Jeff Hao5d917302013-02-27 17:57:33 -0800364 shadow_frame->SetVReg(cur_reg, args[arg_pos]);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700365 break;
366 }
367 }
Mathieu Chartier92246bb2014-02-25 18:22:39 -0800368 self->EndAssertNoThreadSuspension(old_cause);
369 // Do this after populating the shadow frame in case EnsureInitialized causes a GC.
Ian Rogers6c5cb212014-06-18 16:07:20 -0700370 if (method->IsStatic() && UNLIKELY(!method->GetDeclaringClass()->IsInitialized())) {
Mathieu Chartier92246bb2014-02-25 18:22:39 -0800371 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700372 StackHandleScope<1> hs(self);
373 Handle<mirror::Class> h_class(hs.NewHandle(method->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700374 if (UNLIKELY(!class_linker->EnsureInitialized(self, h_class, true, true))) {
Mathieu Chartier92246bb2014-02-25 18:22:39 -0800375 CHECK(self->IsExceptionPending());
376 self->PopShadowFrame();
377 return;
378 }
379 }
Ian Rogers64b6d142012-10-29 16:34:15 -0700380 if (LIKELY(!method->IsNative())) {
Ian Rogerse94652f2014-12-02 11:13:19 -0800381 JValue r = Execute(self, code_item, *shadow_frame, JValue());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700382 if (result != nullptr) {
Jeff Hao6474d192013-03-26 14:08:09 -0700383 *result = r;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700384 }
385 } else {
Ian Rogers64b6d142012-10-29 16:34:15 -0700386 // We don't expect to be asked to interpret native code (which is entered via a JNI compiler
387 // generated stub) except during testing and image writing.
Mathieu Chartier92246bb2014-02-25 18:22:39 -0800388 // Update args to be the args in the shadow frame since the input ones could hold stale
389 // references pointers due to moving GC.
390 args = shadow_frame->GetVRegArgs(method->IsStatic() ? 0 : 1);
Ian Rogers64b6d142012-10-29 16:34:15 -0700391 if (!Runtime::Current()->IsStarted()) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700392 UnstartedRuntime::Jni(self, method, receiver, args, result);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700393 } else {
Jeff Hao6474d192013-03-26 14:08:09 -0700394 InterpreterJni(self, method, shorty, receiver, args, result);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700395 }
396 }
397 self->PopShadowFrame();
398}
399
Ian Rogers62d6c772013-02-27 08:32:07 -0800400void EnterInterpreterFromDeoptimize(Thread* self, ShadowFrame* shadow_frame, JValue* ret_val)
Mathieu Chartier90443472015-07-16 20:32:27 -0700401 SHARED_REQUIRES(Locks::mutator_lock_) {
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800402 JValue value;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700403 // Set value to last known result in case the shadow frame chain is empty.
404 value.SetJ(ret_val->GetJ());
Sebastien Hertz520633b2015-09-08 17:03:36 +0200405 // Are we executing the first shadow frame?
406 bool first = true;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700407 while (shadow_frame != nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800408 self->SetTopOfShadowStack(shadow_frame);
Ian Rogerse94652f2014-12-02 11:13:19 -0800409 const DexFile::CodeItem* code_item = shadow_frame->GetMethod()->GetCodeItem();
Sebastien Hertz270a0e12015-01-16 19:49:09 +0100410 const uint32_t dex_pc = shadow_frame->GetDexPC();
411 uint32_t new_dex_pc;
412 if (UNLIKELY(self->IsExceptionPending())) {
Sebastien Hertz520633b2015-09-08 17:03:36 +0200413 // If we deoptimize from the QuickExceptionHandler, we already reported the exception to
414 // the instrumentation. To prevent from reporting it a second time, we simply pass a
415 // null Instrumentation*.
Sebastien Hertz270a0e12015-01-16 19:49:09 +0100416 const instrumentation::Instrumentation* const instrumentation =
Sebastien Hertz520633b2015-09-08 17:03:36 +0200417 first ? nullptr : Runtime::Current()->GetInstrumentation();
Sebastien Hertz270a0e12015-01-16 19:49:09 +0100418 uint32_t found_dex_pc = FindNextInstructionFollowingException(self, *shadow_frame, dex_pc,
419 instrumentation);
420 new_dex_pc = found_dex_pc; // the dex pc of a matching catch handler
421 // or DexFile::kDexNoIndex if there is none.
422 } else {
423 const Instruction* instr = Instruction::At(&code_item->insns_[dex_pc]);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700424 // For an invoke, use the dex pc of the next instruction.
425 // TODO: should be tested more once b/17586779 is fixed.
426 new_dex_pc = dex_pc + (instr->IsInvoke() ? instr->SizeInCodeUnits() : 0);
Sebastien Hertz270a0e12015-01-16 19:49:09 +0100427 }
428 if (new_dex_pc != DexFile::kDexNoIndex) {
429 shadow_frame->SetDexPC(new_dex_pc);
430 value = Execute(self, code_item, *shadow_frame, value);
431 }
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800432 ShadowFrame* old_frame = shadow_frame;
433 shadow_frame = shadow_frame->GetLink();
Christopher Ferris241a9582015-04-27 15:19:41 -0700434 ShadowFrame::DeleteDeoptimizedFrame(old_frame);
Sebastien Hertz520633b2015-09-08 17:03:36 +0200435 first = false;
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800436 }
437 ret_val->SetJ(value.GetJ());
438}
439
Ian Rogerse94652f2014-12-02 11:13:19 -0800440JValue EnterInterpreterFromEntryPoint(Thread* self, const DexFile::CodeItem* code_item,
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700441 ShadowFrame* shadow_frame) {
Ian Rogersf3e98552013-03-20 15:49:49 -0700442 DCHECK_EQ(self, Thread::Current());
Nicolas Geoffray535a3fb2014-07-22 15:17:38 +0100443 bool implicit_check = !Runtime::Current()->ExplicitStackOverflowChecks();
444 if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEndForInterpreter(implicit_check))) {
Ian Rogersf3e98552013-03-20 15:49:49 -0700445 ThrowStackOverflowError(self);
446 return JValue();
447 }
448
Ian Rogerse94652f2014-12-02 11:13:19 -0800449 return Execute(self, code_item, *shadow_frame, JValue());
Ian Rogers7db619b2013-01-16 18:35:48 -0800450}
451
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700452void ArtInterpreterToInterpreterBridge(Thread* self, const DexFile::CodeItem* code_item,
453 ShadowFrame* shadow_frame, JValue* result) {
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))) {
Jeff Hao16743632013-05-08 10:59:04 -0700456 ThrowStackOverflowError(self);
Jeff Hao69510672013-05-21 17:34:55 -0700457 return;
Jeff Hao16743632013-05-08 10:59:04 -0700458 }
459
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700460 self->PushShadowFrame(shadow_frame);
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200461 // Ensure static methods are initialized.
Ian Rogerse94652f2014-12-02 11:13:19 -0800462 const bool is_static = shadow_frame->GetMethod()->IsStatic();
463 if (is_static) {
464 mirror::Class* declaring_class = shadow_frame->GetMethod()->GetDeclaringClass();
Ian Rogers6c5cb212014-06-18 16:07:20 -0700465 if (UNLIKELY(!declaring_class->IsInitialized())) {
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700466 StackHandleScope<1> hs(self);
467 HandleWrapper<Class> h_declaring_class(hs.NewHandleWrapper(&declaring_class));
468 if (UNLIKELY(!Runtime::Current()->GetClassLinker()->EnsureInitialized(
Ian Rogers7b078e82014-09-10 14:44:24 -0700469 self, h_declaring_class, true, true))) {
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700470 DCHECK(self->IsExceptionPending());
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700471 self->PopShadowFrame();
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200472 return;
473 }
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700474 CHECK(h_declaring_class->IsInitializing());
Jeff Hao16743632013-05-08 10:59:04 -0700475 }
Jeff Hao16743632013-05-08 10:59:04 -0700476 }
Jeff Hao16743632013-05-08 10:59:04 -0700477
Ian Rogerse94652f2014-12-02 11:13:19 -0800478 if (LIKELY(!shadow_frame->GetMethod()->IsNative())) {
479 result->SetJ(Execute(self, code_item, *shadow_frame, JValue()).GetJ());
Jeff Hao16743632013-05-08 10:59:04 -0700480 } else {
481 // We don't expect to be asked to interpret native code (which is entered via a JNI compiler
482 // generated stub) except during testing and image writing.
483 CHECK(!Runtime::Current()->IsStarted());
Ian Rogerse94652f2014-12-02 11:13:19 -0800484 Object* receiver = is_static ? nullptr : shadow_frame->GetVRegReference(0);
485 uint32_t* args = shadow_frame->GetVRegArgs(is_static ? 0 : 1);
Andreas Gampe799681b2015-05-15 19:24:12 -0700486 UnstartedRuntime::Jni(self, shadow_frame->GetMethod(), receiver, args, result);
Jeff Hao16743632013-05-08 10:59:04 -0700487 }
488
489 self->PopShadowFrame();
Jeff Hao16743632013-05-08 10:59:04 -0700490}
491
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700492} // namespace interpreter
493} // namespace art