xref: /aosp_15_r20/art/runtime/interpreter/interpreter.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2012 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #include "interpreter.h"
18*795d594fSAndroid Build Coastguard Worker 
19*795d594fSAndroid Build Coastguard Worker #include <limits>
20*795d594fSAndroid Build Coastguard Worker #include <string_view>
21*795d594fSAndroid Build Coastguard Worker 
22*795d594fSAndroid Build Coastguard Worker #include "common_dex_operations.h"
23*795d594fSAndroid Build Coastguard Worker #include "common_throws.h"
24*795d594fSAndroid Build Coastguard Worker #include "dex/dex_file_types.h"
25*795d594fSAndroid Build Coastguard Worker #include "interpreter_common.h"
26*795d594fSAndroid Build Coastguard Worker #include "interpreter_switch_impl.h"
27*795d594fSAndroid Build Coastguard Worker #include "jit/jit.h"
28*795d594fSAndroid Build Coastguard Worker #include "jit/jit_code_cache.h"
29*795d594fSAndroid Build Coastguard Worker #include "jvalue-inl.h"
30*795d594fSAndroid Build Coastguard Worker #include "mirror/string-inl.h"
31*795d594fSAndroid Build Coastguard Worker #include "nativehelper/scoped_local_ref.h"
32*795d594fSAndroid Build Coastguard Worker #include "scoped_thread_state_change-inl.h"
33*795d594fSAndroid Build Coastguard Worker #include "shadow_frame-inl.h"
34*795d594fSAndroid Build Coastguard Worker #include "stack.h"
35*795d594fSAndroid Build Coastguard Worker #include "thread-inl.h"
36*795d594fSAndroid Build Coastguard Worker #include "unstarted_runtime.h"
37*795d594fSAndroid Build Coastguard Worker 
38*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
39*795d594fSAndroid Build Coastguard Worker namespace interpreter {
40*795d594fSAndroid Build Coastguard Worker 
ObjArg(uint32_t arg)41*795d594fSAndroid Build Coastguard Worker ALWAYS_INLINE static ObjPtr<mirror::Object> ObjArg(uint32_t arg)
42*795d594fSAndroid Build Coastguard Worker     REQUIRES_SHARED(Locks::mutator_lock_) {
43*795d594fSAndroid Build Coastguard Worker   return reinterpret_cast<mirror::Object*>(arg);
44*795d594fSAndroid Build Coastguard Worker }
45*795d594fSAndroid Build Coastguard Worker 
InterpreterJni(Thread * self,ArtMethod * method,std::string_view shorty,ObjPtr<mirror::Object> receiver,uint32_t * args,JValue * result)46*795d594fSAndroid Build Coastguard Worker static void InterpreterJni(Thread* self,
47*795d594fSAndroid Build Coastguard Worker                            ArtMethod* method,
48*795d594fSAndroid Build Coastguard Worker                            std::string_view shorty,
49*795d594fSAndroid Build Coastguard Worker                            ObjPtr<mirror::Object> receiver,
50*795d594fSAndroid Build Coastguard Worker                            uint32_t* args,
51*795d594fSAndroid Build Coastguard Worker                            JValue* result)
52*795d594fSAndroid Build Coastguard Worker     REQUIRES_SHARED(Locks::mutator_lock_) {
53*795d594fSAndroid Build Coastguard Worker   // TODO: The following enters JNI code using a typedef-ed function rather than the JNI compiler,
54*795d594fSAndroid Build Coastguard Worker   //       it should be removed and JNI compiled stubs used instead.
55*795d594fSAndroid Build Coastguard Worker   ScopedObjectAccessUnchecked soa(self);
56*795d594fSAndroid Build Coastguard Worker   if (method->IsStatic()) {
57*795d594fSAndroid Build Coastguard Worker     if (shorty == "L") {
58*795d594fSAndroid Build Coastguard Worker       using fntype = jobject(JNIEnv*, jclass);
59*795d594fSAndroid Build Coastguard Worker       fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
60*795d594fSAndroid Build Coastguard Worker       ScopedLocalRef<jclass> klass(soa.Env(),
61*795d594fSAndroid Build Coastguard Worker                                    soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
62*795d594fSAndroid Build Coastguard Worker       jobject jresult;
63*795d594fSAndroid Build Coastguard Worker       {
64*795d594fSAndroid Build Coastguard Worker         ScopedThreadStateChange tsc(self, ThreadState::kNative);
65*795d594fSAndroid Build Coastguard Worker         jresult = fn(soa.Env(), klass.get());
66*795d594fSAndroid Build Coastguard Worker       }
67*795d594fSAndroid Build Coastguard Worker       result->SetL(soa.Decode<mirror::Object>(jresult));
68*795d594fSAndroid Build Coastguard Worker     } else if (shorty == "V") {
69*795d594fSAndroid Build Coastguard Worker       using fntype = void(JNIEnv*, jclass);
70*795d594fSAndroid Build Coastguard Worker       fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
71*795d594fSAndroid Build Coastguard Worker       ScopedLocalRef<jclass> klass(soa.Env(),
72*795d594fSAndroid Build Coastguard Worker                                    soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
73*795d594fSAndroid Build Coastguard Worker       ScopedThreadStateChange tsc(self, ThreadState::kNative);
74*795d594fSAndroid Build Coastguard Worker       fn(soa.Env(), klass.get());
75*795d594fSAndroid Build Coastguard Worker     } else if (shorty == "Z") {
76*795d594fSAndroid Build Coastguard Worker       using fntype = jboolean(JNIEnv*, jclass);
77*795d594fSAndroid Build Coastguard Worker       fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
78*795d594fSAndroid Build Coastguard Worker       ScopedLocalRef<jclass> klass(soa.Env(),
79*795d594fSAndroid Build Coastguard Worker                                    soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
80*795d594fSAndroid Build Coastguard Worker       ScopedThreadStateChange tsc(self, ThreadState::kNative);
81*795d594fSAndroid Build Coastguard Worker       result->SetZ(fn(soa.Env(), klass.get()));
82*795d594fSAndroid Build Coastguard Worker     } else if (shorty == "BI") {
83*795d594fSAndroid Build Coastguard Worker       using fntype = jbyte(JNIEnv*, jclass, jint);
84*795d594fSAndroid Build Coastguard Worker       fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
85*795d594fSAndroid Build Coastguard Worker       ScopedLocalRef<jclass> klass(soa.Env(),
86*795d594fSAndroid Build Coastguard Worker                                    soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
87*795d594fSAndroid Build Coastguard Worker       ScopedThreadStateChange tsc(self, ThreadState::kNative);
88*795d594fSAndroid Build Coastguard Worker       result->SetB(fn(soa.Env(), klass.get(), args[0]));
89*795d594fSAndroid Build Coastguard Worker     } else if (shorty == "II") {
90*795d594fSAndroid Build Coastguard Worker       using fntype = jint(JNIEnv*, jclass, jint);
91*795d594fSAndroid Build Coastguard Worker       fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
92*795d594fSAndroid Build Coastguard Worker       ScopedLocalRef<jclass> klass(soa.Env(),
93*795d594fSAndroid Build Coastguard Worker                                    soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
94*795d594fSAndroid Build Coastguard Worker       ScopedThreadStateChange tsc(self, ThreadState::kNative);
95*795d594fSAndroid Build Coastguard Worker       result->SetI(fn(soa.Env(), klass.get(), args[0]));
96*795d594fSAndroid Build Coastguard Worker     } else if (shorty == "LL") {
97*795d594fSAndroid Build Coastguard Worker       using fntype = jobject(JNIEnv*, jclass, jobject);
98*795d594fSAndroid Build Coastguard Worker       fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
99*795d594fSAndroid Build Coastguard Worker       ScopedLocalRef<jclass> klass(soa.Env(),
100*795d594fSAndroid Build Coastguard Worker                                    soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
101*795d594fSAndroid Build Coastguard Worker       ScopedLocalRef<jobject> arg0(soa.Env(),
102*795d594fSAndroid Build Coastguard Worker                                    soa.AddLocalReference<jobject>(ObjArg(args[0])));
103*795d594fSAndroid Build Coastguard Worker       jobject jresult;
104*795d594fSAndroid Build Coastguard Worker       {
105*795d594fSAndroid Build Coastguard Worker         ScopedThreadStateChange tsc(self, ThreadState::kNative);
106*795d594fSAndroid Build Coastguard Worker         jresult = fn(soa.Env(), klass.get(), arg0.get());
107*795d594fSAndroid Build Coastguard Worker       }
108*795d594fSAndroid Build Coastguard Worker       result->SetL(soa.Decode<mirror::Object>(jresult));
109*795d594fSAndroid Build Coastguard Worker     } else if (shorty == "IIZ") {
110*795d594fSAndroid Build Coastguard Worker       using fntype = jint(JNIEnv*, jclass, jint, jboolean);
111*795d594fSAndroid Build Coastguard Worker       fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
112*795d594fSAndroid Build Coastguard Worker       ScopedLocalRef<jclass> klass(soa.Env(),
113*795d594fSAndroid Build Coastguard Worker                                    soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
114*795d594fSAndroid Build Coastguard Worker       ScopedThreadStateChange tsc(self, ThreadState::kNative);
115*795d594fSAndroid Build Coastguard Worker       result->SetI(fn(soa.Env(), klass.get(), args[0], args[1]));
116*795d594fSAndroid Build Coastguard Worker     } else if (shorty == "ILI") {
117*795d594fSAndroid Build Coastguard Worker       using fntype = jint(JNIEnv*, jclass, jobject, jint);
118*795d594fSAndroid Build Coastguard Worker       fntype* const fn = reinterpret_cast<fntype*>(const_cast<void*>(
119*795d594fSAndroid Build Coastguard Worker           method->GetEntryPointFromJni()));
120*795d594fSAndroid Build Coastguard Worker       ScopedLocalRef<jclass> klass(soa.Env(),
121*795d594fSAndroid Build Coastguard Worker                                    soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
122*795d594fSAndroid Build Coastguard Worker       ScopedLocalRef<jobject> arg0(soa.Env(),
123*795d594fSAndroid Build Coastguard Worker                                    soa.AddLocalReference<jobject>(ObjArg(args[0])));
124*795d594fSAndroid Build Coastguard Worker       ScopedThreadStateChange tsc(self, ThreadState::kNative);
125*795d594fSAndroid Build Coastguard Worker       result->SetI(fn(soa.Env(), klass.get(), arg0.get(), args[1]));
126*795d594fSAndroid Build Coastguard Worker     } else if (shorty == "SIZ") {
127*795d594fSAndroid Build Coastguard Worker       using fntype = jshort(JNIEnv*, jclass, jint, jboolean);
128*795d594fSAndroid Build Coastguard Worker       fntype* const fn =
129*795d594fSAndroid Build Coastguard Worker           reinterpret_cast<fntype*>(const_cast<void*>(method->GetEntryPointFromJni()));
130*795d594fSAndroid Build Coastguard Worker       ScopedLocalRef<jclass> klass(soa.Env(),
131*795d594fSAndroid Build Coastguard Worker                                    soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
132*795d594fSAndroid Build Coastguard Worker       ScopedThreadStateChange tsc(self, ThreadState::kNative);
133*795d594fSAndroid Build Coastguard Worker       result->SetS(fn(soa.Env(), klass.get(), args[0], args[1]));
134*795d594fSAndroid Build Coastguard Worker     } else if (shorty == "VIZ") {
135*795d594fSAndroid Build Coastguard Worker       using fntype = void(JNIEnv*, jclass, jint, jboolean);
136*795d594fSAndroid Build Coastguard Worker       fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
137*795d594fSAndroid Build Coastguard Worker       ScopedLocalRef<jclass> klass(soa.Env(),
138*795d594fSAndroid Build Coastguard Worker                                    soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
139*795d594fSAndroid Build Coastguard Worker       ScopedThreadStateChange tsc(self, ThreadState::kNative);
140*795d594fSAndroid Build Coastguard Worker       fn(soa.Env(), klass.get(), args[0], args[1]);
141*795d594fSAndroid Build Coastguard Worker     } else if (shorty == "ZLL") {
142*795d594fSAndroid Build Coastguard Worker       using fntype = jboolean(JNIEnv*, jclass, jobject, jobject);
143*795d594fSAndroid Build Coastguard Worker       fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
144*795d594fSAndroid Build Coastguard Worker       ScopedLocalRef<jclass> klass(soa.Env(),
145*795d594fSAndroid Build Coastguard Worker                                    soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
146*795d594fSAndroid Build Coastguard Worker       ScopedLocalRef<jobject> arg0(soa.Env(),
147*795d594fSAndroid Build Coastguard Worker                                    soa.AddLocalReference<jobject>(ObjArg(args[0])));
148*795d594fSAndroid Build Coastguard Worker       ScopedLocalRef<jobject> arg1(soa.Env(),
149*795d594fSAndroid Build Coastguard Worker                                    soa.AddLocalReference<jobject>(ObjArg(args[1])));
150*795d594fSAndroid Build Coastguard Worker       ScopedThreadStateChange tsc(self, ThreadState::kNative);
151*795d594fSAndroid Build Coastguard Worker       result->SetZ(fn(soa.Env(), klass.get(), arg0.get(), arg1.get()));
152*795d594fSAndroid Build Coastguard Worker     } else if (shorty == "ZILL") {
153*795d594fSAndroid Build Coastguard Worker       using fntype = jboolean(JNIEnv*, jclass, jint, jobject, jobject);
154*795d594fSAndroid Build Coastguard Worker       fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
155*795d594fSAndroid Build Coastguard Worker       ScopedLocalRef<jclass> klass(soa.Env(),
156*795d594fSAndroid Build Coastguard Worker                                    soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
157*795d594fSAndroid Build Coastguard Worker       ScopedLocalRef<jobject> arg1(soa.Env(),
158*795d594fSAndroid Build Coastguard Worker                                    soa.AddLocalReference<jobject>(ObjArg(args[1])));
159*795d594fSAndroid Build Coastguard Worker       ScopedLocalRef<jobject> arg2(soa.Env(),
160*795d594fSAndroid Build Coastguard Worker                                    soa.AddLocalReference<jobject>(ObjArg(args[2])));
161*795d594fSAndroid Build Coastguard Worker       ScopedThreadStateChange tsc(self, ThreadState::kNative);
162*795d594fSAndroid Build Coastguard Worker       result->SetZ(fn(soa.Env(), klass.get(), args[0], arg1.get(), arg2.get()));
163*795d594fSAndroid Build Coastguard Worker     } else if (shorty == "VILII") {
164*795d594fSAndroid Build Coastguard Worker       using fntype = void(JNIEnv*, jclass, jint, jobject, jint, jint);
165*795d594fSAndroid Build Coastguard Worker       fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
166*795d594fSAndroid Build Coastguard Worker       ScopedLocalRef<jclass> klass(soa.Env(),
167*795d594fSAndroid Build Coastguard Worker                                    soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
168*795d594fSAndroid Build Coastguard Worker       ScopedLocalRef<jobject> arg1(soa.Env(),
169*795d594fSAndroid Build Coastguard Worker                                    soa.AddLocalReference<jobject>(ObjArg(args[1])));
170*795d594fSAndroid Build Coastguard Worker       ScopedThreadStateChange tsc(self, ThreadState::kNative);
171*795d594fSAndroid Build Coastguard Worker       fn(soa.Env(), klass.get(), args[0], arg1.get(), args[2], args[3]);
172*795d594fSAndroid Build Coastguard Worker     } else if (shorty == "VLILII") {
173*795d594fSAndroid Build Coastguard Worker       using fntype = void(JNIEnv*, jclass, jobject, jint, jobject, jint, jint);
174*795d594fSAndroid Build Coastguard Worker       fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
175*795d594fSAndroid Build Coastguard Worker       ScopedLocalRef<jclass> klass(soa.Env(),
176*795d594fSAndroid Build Coastguard Worker                                    soa.AddLocalReference<jclass>(method->GetDeclaringClass()));
177*795d594fSAndroid Build Coastguard Worker       ScopedLocalRef<jobject> arg0(soa.Env(),
178*795d594fSAndroid Build Coastguard Worker                                    soa.AddLocalReference<jobject>(ObjArg(args[0])));
179*795d594fSAndroid Build Coastguard Worker       ScopedLocalRef<jobject> arg2(soa.Env(),
180*795d594fSAndroid Build Coastguard Worker                                    soa.AddLocalReference<jobject>(ObjArg(args[2])));
181*795d594fSAndroid Build Coastguard Worker       ScopedThreadStateChange tsc(self, ThreadState::kNative);
182*795d594fSAndroid Build Coastguard Worker       fn(soa.Env(), klass.get(), arg0.get(), args[1], arg2.get(), args[3], args[4]);
183*795d594fSAndroid Build Coastguard Worker     } else {
184*795d594fSAndroid Build Coastguard Worker       LOG(FATAL) << "Do something with static native method: " << method->PrettyMethod()
185*795d594fSAndroid Build Coastguard Worker           << " shorty: " << shorty;
186*795d594fSAndroid Build Coastguard Worker     }
187*795d594fSAndroid Build Coastguard Worker   } else {
188*795d594fSAndroid Build Coastguard Worker     if (shorty == "L") {
189*795d594fSAndroid Build Coastguard Worker       using fntype = jobject(JNIEnv*, jobject);
190*795d594fSAndroid Build Coastguard Worker       fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
191*795d594fSAndroid Build Coastguard Worker       ScopedLocalRef<jobject> rcvr(soa.Env(),
192*795d594fSAndroid Build Coastguard Worker                                    soa.AddLocalReference<jobject>(receiver));
193*795d594fSAndroid Build Coastguard Worker       jobject jresult;
194*795d594fSAndroid Build Coastguard Worker       {
195*795d594fSAndroid Build Coastguard Worker         ScopedThreadStateChange tsc(self, ThreadState::kNative);
196*795d594fSAndroid Build Coastguard Worker         jresult = fn(soa.Env(), rcvr.get());
197*795d594fSAndroid Build Coastguard Worker       }
198*795d594fSAndroid Build Coastguard Worker       result->SetL(soa.Decode<mirror::Object>(jresult));
199*795d594fSAndroid Build Coastguard Worker     } else if (shorty == "V") {
200*795d594fSAndroid Build Coastguard Worker       using fntype = void(JNIEnv*, jobject);
201*795d594fSAndroid Build Coastguard Worker       fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
202*795d594fSAndroid Build Coastguard Worker       ScopedLocalRef<jobject> rcvr(soa.Env(),
203*795d594fSAndroid Build Coastguard Worker                                    soa.AddLocalReference<jobject>(receiver));
204*795d594fSAndroid Build Coastguard Worker       ScopedThreadStateChange tsc(self, ThreadState::kNative);
205*795d594fSAndroid Build Coastguard Worker       fn(soa.Env(), rcvr.get());
206*795d594fSAndroid Build Coastguard Worker     } else if (shorty == "LL") {
207*795d594fSAndroid Build Coastguard Worker       using fntype = jobject(JNIEnv*, jobject, jobject);
208*795d594fSAndroid Build Coastguard Worker       fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
209*795d594fSAndroid Build Coastguard Worker       ScopedLocalRef<jobject> rcvr(soa.Env(),
210*795d594fSAndroid Build Coastguard Worker                                    soa.AddLocalReference<jobject>(receiver));
211*795d594fSAndroid Build Coastguard Worker       ScopedLocalRef<jobject> arg0(soa.Env(),
212*795d594fSAndroid Build Coastguard Worker                                    soa.AddLocalReference<jobject>(ObjArg(args[0])));
213*795d594fSAndroid Build Coastguard Worker       jobject jresult;
214*795d594fSAndroid Build Coastguard Worker       {
215*795d594fSAndroid Build Coastguard Worker         ScopedThreadStateChange tsc(self, ThreadState::kNative);
216*795d594fSAndroid Build Coastguard Worker         jresult = fn(soa.Env(), rcvr.get(), arg0.get());
217*795d594fSAndroid Build Coastguard Worker       }
218*795d594fSAndroid Build Coastguard Worker       result->SetL(soa.Decode<mirror::Object>(jresult));
219*795d594fSAndroid Build Coastguard Worker       ScopedThreadStateChange tsc(self, ThreadState::kNative);
220*795d594fSAndroid Build Coastguard Worker     } else if (shorty == "III") {
221*795d594fSAndroid Build Coastguard Worker       using fntype = jint(JNIEnv*, jobject, jint, jint);
222*795d594fSAndroid Build Coastguard Worker       fntype* const fn = reinterpret_cast<fntype*>(method->GetEntryPointFromJni());
223*795d594fSAndroid Build Coastguard Worker       ScopedLocalRef<jobject> rcvr(soa.Env(),
224*795d594fSAndroid Build Coastguard Worker                                    soa.AddLocalReference<jobject>(receiver));
225*795d594fSAndroid Build Coastguard Worker       ScopedThreadStateChange tsc(self, ThreadState::kNative);
226*795d594fSAndroid Build Coastguard Worker       result->SetI(fn(soa.Env(), rcvr.get(), args[0], args[1]));
227*795d594fSAndroid Build Coastguard Worker     } else {
228*795d594fSAndroid Build Coastguard Worker       LOG(FATAL) << "Do something with native method: " << method->PrettyMethod()
229*795d594fSAndroid Build Coastguard Worker           << " shorty: " << shorty;
230*795d594fSAndroid Build Coastguard Worker     }
231*795d594fSAndroid Build Coastguard Worker   }
232*795d594fSAndroid Build Coastguard Worker }
233*795d594fSAndroid Build Coastguard Worker 
234*795d594fSAndroid Build Coastguard Worker NO_STACK_PROTECTOR
ExecuteSwitch(Thread * self,const CodeItemDataAccessor & accessor,ShadowFrame & shadow_frame,JValue result_register)235*795d594fSAndroid Build Coastguard Worker static JValue ExecuteSwitch(Thread* self,
236*795d594fSAndroid Build Coastguard Worker                             const CodeItemDataAccessor& accessor,
237*795d594fSAndroid Build Coastguard Worker                             ShadowFrame& shadow_frame,
238*795d594fSAndroid Build Coastguard Worker                             JValue result_register) REQUIRES_SHARED(Locks::mutator_lock_) {
239*795d594fSAndroid Build Coastguard Worker   Runtime* runtime = Runtime::Current();
240*795d594fSAndroid Build Coastguard Worker   auto switch_impl_cpp = runtime->IsActiveTransaction()
241*795d594fSAndroid Build Coastguard Worker       ? runtime->GetClassLinker()->GetTransactionalInterpreter()
242*795d594fSAndroid Build Coastguard Worker       : reinterpret_cast<const void*>(&ExecuteSwitchImplCpp</*transaction_active=*/ false>);
243*795d594fSAndroid Build Coastguard Worker   return ExecuteSwitchImpl(
244*795d594fSAndroid Build Coastguard Worker       self, accessor, shadow_frame, result_register, switch_impl_cpp);
245*795d594fSAndroid Build Coastguard Worker }
246*795d594fSAndroid Build Coastguard Worker 
247*795d594fSAndroid Build Coastguard Worker NO_STACK_PROTECTOR
Execute(Thread * self,const CodeItemDataAccessor & accessor,ShadowFrame & shadow_frame,JValue result_register,bool stay_in_interpreter=false,bool from_deoptimize=false)248*795d594fSAndroid Build Coastguard Worker static inline JValue Execute(
249*795d594fSAndroid Build Coastguard Worker     Thread* self,
250*795d594fSAndroid Build Coastguard Worker     const CodeItemDataAccessor& accessor,
251*795d594fSAndroid Build Coastguard Worker     ShadowFrame& shadow_frame,
252*795d594fSAndroid Build Coastguard Worker     JValue result_register,
253*795d594fSAndroid Build Coastguard Worker     bool stay_in_interpreter = false,
254*795d594fSAndroid Build Coastguard Worker     bool from_deoptimize = false) REQUIRES_SHARED(Locks::mutator_lock_) {
255*795d594fSAndroid Build Coastguard Worker   DCHECK(!shadow_frame.GetMethod()->IsAbstract());
256*795d594fSAndroid Build Coastguard Worker   DCHECK(!shadow_frame.GetMethod()->IsNative());
257*795d594fSAndroid Build Coastguard Worker 
258*795d594fSAndroid Build Coastguard Worker   // We cache the result of NeedsDexPcEvents in the shadow frame so we don't need to call
259*795d594fSAndroid Build Coastguard Worker   // NeedsDexPcEvents on every instruction for better performance. NeedsDexPcEvents only gets
260*795d594fSAndroid Build Coastguard Worker   // updated asynchronoulsy in a SuspendAll scope and any existing shadow frames are updated with
261*795d594fSAndroid Build Coastguard Worker   // new value. So it is safe to cache it here.
262*795d594fSAndroid Build Coastguard Worker   shadow_frame.SetNotifyDexPcMoveEvents(
263*795d594fSAndroid Build Coastguard Worker       Runtime::Current()->GetInstrumentation()->NeedsDexPcEvents(shadow_frame.GetMethod(), self));
264*795d594fSAndroid Build Coastguard Worker 
265*795d594fSAndroid Build Coastguard Worker   if (LIKELY(!from_deoptimize)) {  // Entering the method, but not via deoptimization.
266*795d594fSAndroid Build Coastguard Worker     if (kIsDebugBuild) {
267*795d594fSAndroid Build Coastguard Worker       CHECK_EQ(shadow_frame.GetDexPC(), 0u);
268*795d594fSAndroid Build Coastguard Worker       self->AssertNoPendingException();
269*795d594fSAndroid Build Coastguard Worker     }
270*795d594fSAndroid Build Coastguard Worker     ArtMethod *method = shadow_frame.GetMethod();
271*795d594fSAndroid Build Coastguard Worker 
272*795d594fSAndroid Build Coastguard Worker     // If we can continue in JIT and have JITed code available execute JITed code.
273*795d594fSAndroid Build Coastguard Worker     if (!stay_in_interpreter &&
274*795d594fSAndroid Build Coastguard Worker         !self->IsForceInterpreter() &&
275*795d594fSAndroid Build Coastguard Worker         !shadow_frame.GetForcePopFrame() &&
276*795d594fSAndroid Build Coastguard Worker         !shadow_frame.GetNotifyDexPcMoveEvents()) {
277*795d594fSAndroid Build Coastguard Worker       jit::Jit* jit = Runtime::Current()->GetJit();
278*795d594fSAndroid Build Coastguard Worker       if (jit != nullptr) {
279*795d594fSAndroid Build Coastguard Worker         jit->MethodEntered(self, shadow_frame.GetMethod());
280*795d594fSAndroid Build Coastguard Worker         if (jit->CanInvokeCompiledCode(method)) {
281*795d594fSAndroid Build Coastguard Worker           JValue result;
282*795d594fSAndroid Build Coastguard Worker 
283*795d594fSAndroid Build Coastguard Worker           // Pop the shadow frame before calling into compiled code.
284*795d594fSAndroid Build Coastguard Worker           self->PopShadowFrame();
285*795d594fSAndroid Build Coastguard Worker           // Calculate the offset of the first input reg. The input registers are in the high regs.
286*795d594fSAndroid Build Coastguard Worker           // It's ok to access the code item here since JIT code will have been touched by the
287*795d594fSAndroid Build Coastguard Worker           // interpreter and compiler already.
288*795d594fSAndroid Build Coastguard Worker           uint16_t arg_offset = accessor.RegistersSize() - accessor.InsSize();
289*795d594fSAndroid Build Coastguard Worker           ArtInterpreterToCompiledCodeBridge(self, nullptr, &shadow_frame, arg_offset, &result);
290*795d594fSAndroid Build Coastguard Worker           // Push the shadow frame back as the caller will expect it.
291*795d594fSAndroid Build Coastguard Worker           self->PushShadowFrame(&shadow_frame);
292*795d594fSAndroid Build Coastguard Worker 
293*795d594fSAndroid Build Coastguard Worker           return result;
294*795d594fSAndroid Build Coastguard Worker         }
295*795d594fSAndroid Build Coastguard Worker       }
296*795d594fSAndroid Build Coastguard Worker     }
297*795d594fSAndroid Build Coastguard Worker 
298*795d594fSAndroid Build Coastguard Worker     instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
299*795d594fSAndroid Build Coastguard Worker     if (UNLIKELY(instrumentation->HasMethodEntryListeners() || shadow_frame.GetForcePopFrame())) {
300*795d594fSAndroid Build Coastguard Worker       instrumentation->MethodEnterEvent(self, method);
301*795d594fSAndroid Build Coastguard Worker       if (UNLIKELY(shadow_frame.GetForcePopFrame())) {
302*795d594fSAndroid Build Coastguard Worker         // The caller will retry this invoke or ignore the result. Just return immediately without
303*795d594fSAndroid Build Coastguard Worker         // any value.
304*795d594fSAndroid Build Coastguard Worker         DCHECK(Runtime::Current()->AreNonStandardExitsEnabled());
305*795d594fSAndroid Build Coastguard Worker         JValue ret = JValue();
306*795d594fSAndroid Build Coastguard Worker         PerformNonStandardReturn(self,
307*795d594fSAndroid Build Coastguard Worker                                  shadow_frame,
308*795d594fSAndroid Build Coastguard Worker                                  ret,
309*795d594fSAndroid Build Coastguard Worker                                  instrumentation,
310*795d594fSAndroid Build Coastguard Worker                                  /* unlock_monitors= */ false);
311*795d594fSAndroid Build Coastguard Worker         return ret;
312*795d594fSAndroid Build Coastguard Worker       }
313*795d594fSAndroid Build Coastguard Worker       if (UNLIKELY(self->IsExceptionPending())) {
314*795d594fSAndroid Build Coastguard Worker         instrumentation->MethodUnwindEvent(self,
315*795d594fSAndroid Build Coastguard Worker                                            method,
316*795d594fSAndroid Build Coastguard Worker                                            0);
317*795d594fSAndroid Build Coastguard Worker         JValue ret = JValue();
318*795d594fSAndroid Build Coastguard Worker         if (UNLIKELY(shadow_frame.GetForcePopFrame())) {
319*795d594fSAndroid Build Coastguard Worker           DCHECK(Runtime::Current()->AreNonStandardExitsEnabled());
320*795d594fSAndroid Build Coastguard Worker           PerformNonStandardReturn(self,
321*795d594fSAndroid Build Coastguard Worker                                    shadow_frame,
322*795d594fSAndroid Build Coastguard Worker                                    ret,
323*795d594fSAndroid Build Coastguard Worker                                    instrumentation,
324*795d594fSAndroid Build Coastguard Worker                                    /* unlock_monitors= */ false);
325*795d594fSAndroid Build Coastguard Worker         }
326*795d594fSAndroid Build Coastguard Worker         return ret;
327*795d594fSAndroid Build Coastguard Worker       }
328*795d594fSAndroid Build Coastguard Worker     }
329*795d594fSAndroid Build Coastguard Worker   }
330*795d594fSAndroid Build Coastguard Worker 
331*795d594fSAndroid Build Coastguard Worker   ArtMethod* method = shadow_frame.GetMethod();
332*795d594fSAndroid Build Coastguard Worker 
333*795d594fSAndroid Build Coastguard Worker   DCheckStaticState(self, method);
334*795d594fSAndroid Build Coastguard Worker 
335*795d594fSAndroid Build Coastguard Worker   // Lock counting is a special version of accessibility checks, and for simplicity and
336*795d594fSAndroid Build Coastguard Worker   // reduction of template parameters, we gate it behind access-checks mode.
337*795d594fSAndroid Build Coastguard Worker   DCHECK_IMPLIES(method->SkipAccessChecks(), !method->MustCountLocks());
338*795d594fSAndroid Build Coastguard Worker 
339*795d594fSAndroid Build Coastguard Worker   VLOG(interpreter) << "Interpreting " << method->PrettyMethod();
340*795d594fSAndroid Build Coastguard Worker 
341*795d594fSAndroid Build Coastguard Worker   return ExecuteSwitch(self, accessor, shadow_frame, result_register);
342*795d594fSAndroid Build Coastguard Worker }
343*795d594fSAndroid Build Coastguard Worker 
EnterInterpreterFromInvoke(Thread * self,ArtMethod * method,ObjPtr<mirror::Object> receiver,uint32_t * args,JValue * result,bool stay_in_interpreter)344*795d594fSAndroid Build Coastguard Worker void EnterInterpreterFromInvoke(Thread* self,
345*795d594fSAndroid Build Coastguard Worker                                 ArtMethod* method,
346*795d594fSAndroid Build Coastguard Worker                                 ObjPtr<mirror::Object> receiver,
347*795d594fSAndroid Build Coastguard Worker                                 uint32_t* args,
348*795d594fSAndroid Build Coastguard Worker                                 JValue* result,
349*795d594fSAndroid Build Coastguard Worker                                 bool stay_in_interpreter) {
350*795d594fSAndroid Build Coastguard Worker   DCHECK_EQ(self, Thread::Current());
351*795d594fSAndroid Build Coastguard Worker   bool implicit_check = Runtime::Current()->GetImplicitStackOverflowChecks();
352*795d594fSAndroid Build Coastguard Worker   if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEndForInterpreter(implicit_check))) {
353*795d594fSAndroid Build Coastguard Worker     ThrowStackOverflowError<kNativeStackType>(self);
354*795d594fSAndroid Build Coastguard Worker     return;
355*795d594fSAndroid Build Coastguard Worker   }
356*795d594fSAndroid Build Coastguard Worker 
357*795d594fSAndroid Build Coastguard Worker   // This can happen if we are in forced interpreter mode and an obsolete method is called using
358*795d594fSAndroid Build Coastguard Worker   // reflection.
359*795d594fSAndroid Build Coastguard Worker   if (UNLIKELY(method->IsObsolete())) {
360*795d594fSAndroid Build Coastguard Worker     ThrowInternalError("Attempting to invoke obsolete version of '%s'.",
361*795d594fSAndroid Build Coastguard Worker                        method->PrettyMethod().c_str());
362*795d594fSAndroid Build Coastguard Worker     return;
363*795d594fSAndroid Build Coastguard Worker   }
364*795d594fSAndroid Build Coastguard Worker 
365*795d594fSAndroid Build Coastguard Worker   const char* old_cause = self->StartAssertNoThreadSuspension("EnterInterpreterFromInvoke");
366*795d594fSAndroid Build Coastguard Worker   CodeItemDataAccessor accessor(method->DexInstructionData());
367*795d594fSAndroid Build Coastguard Worker   uint16_t num_regs;
368*795d594fSAndroid Build Coastguard Worker   uint16_t num_ins;
369*795d594fSAndroid Build Coastguard Worker   if (accessor.HasCodeItem()) {
370*795d594fSAndroid Build Coastguard Worker     num_regs =  accessor.RegistersSize();
371*795d594fSAndroid Build Coastguard Worker     num_ins = accessor.InsSize();
372*795d594fSAndroid Build Coastguard Worker   } else if (!method->IsInvokable()) {
373*795d594fSAndroid Build Coastguard Worker     self->EndAssertNoThreadSuspension(old_cause);
374*795d594fSAndroid Build Coastguard Worker     method->ThrowInvocationTimeError(receiver);
375*795d594fSAndroid Build Coastguard Worker     return;
376*795d594fSAndroid Build Coastguard Worker   } else {
377*795d594fSAndroid Build Coastguard Worker     DCHECK(method->IsNative()) << method->PrettyMethod();
378*795d594fSAndroid Build Coastguard Worker     num_regs = num_ins = ArtMethod::NumArgRegisters(method->GetShortyView());
379*795d594fSAndroid Build Coastguard Worker     if (!method->IsStatic()) {
380*795d594fSAndroid Build Coastguard Worker       num_regs++;
381*795d594fSAndroid Build Coastguard Worker       num_ins++;
382*795d594fSAndroid Build Coastguard Worker     }
383*795d594fSAndroid Build Coastguard Worker   }
384*795d594fSAndroid Build Coastguard Worker   // Set up shadow frame with matching number of reference slots to vregs.
385*795d594fSAndroid Build Coastguard Worker   ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
386*795d594fSAndroid Build Coastguard Worker       CREATE_SHADOW_FRAME(num_regs, method, /* dex pc */ 0);
387*795d594fSAndroid Build Coastguard Worker   ShadowFrame* shadow_frame = shadow_frame_unique_ptr.get();
388*795d594fSAndroid Build Coastguard Worker 
389*795d594fSAndroid Build Coastguard Worker   size_t cur_reg = num_regs - num_ins;
390*795d594fSAndroid Build Coastguard Worker   if (!method->IsStatic()) {
391*795d594fSAndroid Build Coastguard Worker     CHECK(receiver != nullptr);
392*795d594fSAndroid Build Coastguard Worker     shadow_frame->SetVRegReference(cur_reg, receiver);
393*795d594fSAndroid Build Coastguard Worker     ++cur_reg;
394*795d594fSAndroid Build Coastguard Worker   }
395*795d594fSAndroid Build Coastguard Worker   uint32_t shorty_len = 0;
396*795d594fSAndroid Build Coastguard Worker   const char* shorty = method->GetShorty(&shorty_len);
397*795d594fSAndroid Build Coastguard Worker   for (size_t shorty_pos = 0, arg_pos = 0; cur_reg < num_regs; ++shorty_pos, ++arg_pos, cur_reg++) {
398*795d594fSAndroid Build Coastguard Worker     DCHECK_LT(shorty_pos + 1, shorty_len);
399*795d594fSAndroid Build Coastguard Worker     switch (shorty[shorty_pos + 1]) {
400*795d594fSAndroid Build Coastguard Worker       case 'L': {
401*795d594fSAndroid Build Coastguard Worker         ObjPtr<mirror::Object> o =
402*795d594fSAndroid Build Coastguard Worker             reinterpret_cast<StackReference<mirror::Object>*>(&args[arg_pos])->AsMirrorPtr();
403*795d594fSAndroid Build Coastguard Worker         shadow_frame->SetVRegReference(cur_reg, o);
404*795d594fSAndroid Build Coastguard Worker         break;
405*795d594fSAndroid Build Coastguard Worker       }
406*795d594fSAndroid Build Coastguard Worker       case 'J': case 'D': {
407*795d594fSAndroid Build Coastguard Worker         uint64_t wide_value = (static_cast<uint64_t>(args[arg_pos + 1]) << 32) | args[arg_pos];
408*795d594fSAndroid Build Coastguard Worker         shadow_frame->SetVRegLong(cur_reg, wide_value);
409*795d594fSAndroid Build Coastguard Worker         cur_reg++;
410*795d594fSAndroid Build Coastguard Worker         arg_pos++;
411*795d594fSAndroid Build Coastguard Worker         break;
412*795d594fSAndroid Build Coastguard Worker       }
413*795d594fSAndroid Build Coastguard Worker       default:
414*795d594fSAndroid Build Coastguard Worker         shadow_frame->SetVReg(cur_reg, args[arg_pos]);
415*795d594fSAndroid Build Coastguard Worker         break;
416*795d594fSAndroid Build Coastguard Worker     }
417*795d594fSAndroid Build Coastguard Worker   }
418*795d594fSAndroid Build Coastguard Worker   self->EndAssertNoThreadSuspension(old_cause);
419*795d594fSAndroid Build Coastguard Worker   if (!EnsureInitialized(self, shadow_frame)) {
420*795d594fSAndroid Build Coastguard Worker     return;
421*795d594fSAndroid Build Coastguard Worker   }
422*795d594fSAndroid Build Coastguard Worker   self->PushShadowFrame(shadow_frame);
423*795d594fSAndroid Build Coastguard Worker   if (LIKELY(!method->IsNative())) {
424*795d594fSAndroid Build Coastguard Worker     JValue r = Execute(self, accessor, *shadow_frame, JValue(), stay_in_interpreter);
425*795d594fSAndroid Build Coastguard Worker     if (result != nullptr) {
426*795d594fSAndroid Build Coastguard Worker       *result = r;
427*795d594fSAndroid Build Coastguard Worker     }
428*795d594fSAndroid Build Coastguard Worker   } else {
429*795d594fSAndroid Build Coastguard Worker     // We don't expect to be asked to interpret native code (which is entered via a JNI compiler
430*795d594fSAndroid Build Coastguard Worker     // generated stub) except during testing and image writing.
431*795d594fSAndroid Build Coastguard Worker     // Update args to be the args in the shadow frame since the input ones could hold stale
432*795d594fSAndroid Build Coastguard Worker     // references pointers due to moving GC.
433*795d594fSAndroid Build Coastguard Worker     args = shadow_frame->GetVRegArgs(method->IsStatic() ? 0 : 1);
434*795d594fSAndroid Build Coastguard Worker     if (!Runtime::Current()->IsStarted()) {
435*795d594fSAndroid Build Coastguard Worker       UnstartedRuntime::Jni(self, method, receiver.Ptr(), args, result);
436*795d594fSAndroid Build Coastguard Worker     } else {
437*795d594fSAndroid Build Coastguard Worker       InterpreterJni(self, method, shorty, receiver, args, result);
438*795d594fSAndroid Build Coastguard Worker     }
439*795d594fSAndroid Build Coastguard Worker   }
440*795d594fSAndroid Build Coastguard Worker   self->PopShadowFrame();
441*795d594fSAndroid Build Coastguard Worker }
442*795d594fSAndroid Build Coastguard Worker 
GetReceiverRegisterForStringInit(const Instruction * instr)443*795d594fSAndroid Build Coastguard Worker static int16_t GetReceiverRegisterForStringInit(const Instruction* instr) {
444*795d594fSAndroid Build Coastguard Worker   DCHECK(instr->Opcode() == Instruction::INVOKE_DIRECT_RANGE ||
445*795d594fSAndroid Build Coastguard Worker          instr->Opcode() == Instruction::INVOKE_DIRECT);
446*795d594fSAndroid Build Coastguard Worker   return (instr->Opcode() == Instruction::INVOKE_DIRECT_RANGE) ?
447*795d594fSAndroid Build Coastguard Worker       instr->VRegC_3rc() : instr->VRegC_35c();
448*795d594fSAndroid Build Coastguard Worker }
449*795d594fSAndroid Build Coastguard Worker 
EnterInterpreterFromDeoptimize(Thread * self,ShadowFrame * shadow_frame,JValue * ret_val,bool from_code,DeoptimizationMethodType deopt_method_type)450*795d594fSAndroid Build Coastguard Worker void EnterInterpreterFromDeoptimize(Thread* self,
451*795d594fSAndroid Build Coastguard Worker                                     ShadowFrame* shadow_frame,
452*795d594fSAndroid Build Coastguard Worker                                     JValue* ret_val,
453*795d594fSAndroid Build Coastguard Worker                                     bool from_code,
454*795d594fSAndroid Build Coastguard Worker                                     DeoptimizationMethodType deopt_method_type)
455*795d594fSAndroid Build Coastguard Worker     REQUIRES_SHARED(Locks::mutator_lock_) {
456*795d594fSAndroid Build Coastguard Worker   JValue value;
457*795d594fSAndroid Build Coastguard Worker   // Set value to last known result in case the shadow frame chain is empty.
458*795d594fSAndroid Build Coastguard Worker   value.SetJ(ret_val->GetJ());
459*795d594fSAndroid Build Coastguard Worker   // How many frames we have executed.
460*795d594fSAndroid Build Coastguard Worker   size_t frame_cnt = 0;
461*795d594fSAndroid Build Coastguard Worker   while (shadow_frame != nullptr) {
462*795d594fSAndroid Build Coastguard Worker     // We do not want to recover lock state for lock counting when deoptimizing. Currently,
463*795d594fSAndroid Build Coastguard Worker     // the compiler should not have compiled a method that failed structured-locking checks.
464*795d594fSAndroid Build Coastguard Worker     DCHECK(!shadow_frame->GetMethod()->MustCountLocks());
465*795d594fSAndroid Build Coastguard Worker 
466*795d594fSAndroid Build Coastguard Worker     self->SetTopOfShadowStack(shadow_frame);
467*795d594fSAndroid Build Coastguard Worker     CodeItemDataAccessor accessor(shadow_frame->GetMethod()->DexInstructionData());
468*795d594fSAndroid Build Coastguard Worker     const uint32_t dex_pc = shadow_frame->GetDexPC();
469*795d594fSAndroid Build Coastguard Worker     uint32_t new_dex_pc = dex_pc;
470*795d594fSAndroid Build Coastguard Worker     if (UNLIKELY(self->IsExceptionPending())) {
471*795d594fSAndroid Build Coastguard Worker       DCHECK(self->GetException() != Thread::GetDeoptimizationException());
472*795d594fSAndroid Build Coastguard Worker       // If we deoptimize from the QuickExceptionHandler, we already reported the exception throw
473*795d594fSAndroid Build Coastguard Worker       // event to the instrumentation. Skip throw listeners for the first frame. The deopt check
474*795d594fSAndroid Build Coastguard Worker       // should happen after the throw listener is called as throw listener can trigger a
475*795d594fSAndroid Build Coastguard Worker       // deoptimization.
476*795d594fSAndroid Build Coastguard Worker       new_dex_pc = MoveToExceptionHandler(self,
477*795d594fSAndroid Build Coastguard Worker                                           *shadow_frame,
478*795d594fSAndroid Build Coastguard Worker                                           /* skip_listeners= */ false,
479*795d594fSAndroid Build Coastguard Worker                                           /* skip_throw_listener= */ frame_cnt == 0) ?
480*795d594fSAndroid Build Coastguard Worker                        shadow_frame->GetDexPC() :
481*795d594fSAndroid Build Coastguard Worker                        dex::kDexNoIndex;
482*795d594fSAndroid Build Coastguard Worker     } else if (!from_code) {
483*795d594fSAndroid Build Coastguard Worker       // Deoptimization is not called from code directly.
484*795d594fSAndroid Build Coastguard Worker       const Instruction* instr = &accessor.InstructionAt(dex_pc);
485*795d594fSAndroid Build Coastguard Worker       if (deopt_method_type == DeoptimizationMethodType::kKeepDexPc ||
486*795d594fSAndroid Build Coastguard Worker           shadow_frame->GetForceRetryInstruction()) {
487*795d594fSAndroid Build Coastguard Worker         DCHECK(frame_cnt == 0 || shadow_frame->GetForceRetryInstruction())
488*795d594fSAndroid Build Coastguard Worker             << "frame_cnt: " << frame_cnt
489*795d594fSAndroid Build Coastguard Worker             << " force-retry: " << shadow_frame->GetForceRetryInstruction();
490*795d594fSAndroid Build Coastguard Worker         // Need to re-execute the dex instruction.
491*795d594fSAndroid Build Coastguard Worker         // (1) An invocation might be split into class initialization and invoke.
492*795d594fSAndroid Build Coastguard Worker         //     In this case, the invoke should not be skipped.
493*795d594fSAndroid Build Coastguard Worker         // (2) A suspend check should also execute the dex instruction at the
494*795d594fSAndroid Build Coastguard Worker         //     corresponding dex pc.
495*795d594fSAndroid Build Coastguard Worker         // If the ForceRetryInstruction bit is set this must be the second frame (the first being
496*795d594fSAndroid Build Coastguard Worker         // the one that is being popped).
497*795d594fSAndroid Build Coastguard Worker         DCHECK_EQ(new_dex_pc, dex_pc);
498*795d594fSAndroid Build Coastguard Worker         shadow_frame->SetForceRetryInstruction(false);
499*795d594fSAndroid Build Coastguard Worker       } else if (instr->Opcode() == Instruction::MONITOR_ENTER ||
500*795d594fSAndroid Build Coastguard Worker                  instr->Opcode() == Instruction::MONITOR_EXIT) {
501*795d594fSAndroid Build Coastguard Worker         DCHECK(deopt_method_type == DeoptimizationMethodType::kDefault);
502*795d594fSAndroid Build Coastguard Worker         DCHECK_EQ(frame_cnt, 0u);
503*795d594fSAndroid Build Coastguard Worker         // Non-idempotent dex instruction should not be re-executed.
504*795d594fSAndroid Build Coastguard Worker         // On the other hand, if a MONITOR_ENTER is at the dex_pc of a suspend
505*795d594fSAndroid Build Coastguard Worker         // check, that MONITOR_ENTER should be executed. That case is handled
506*795d594fSAndroid Build Coastguard Worker         // above.
507*795d594fSAndroid Build Coastguard Worker         new_dex_pc = dex_pc + instr->SizeInCodeUnits();
508*795d594fSAndroid Build Coastguard Worker       } else if (instr->IsInvoke()) {
509*795d594fSAndroid Build Coastguard Worker         DCHECK(deopt_method_type == DeoptimizationMethodType::kDefault);
510*795d594fSAndroid Build Coastguard Worker         if (IsStringInit(*instr, shadow_frame->GetMethod())) {
511*795d594fSAndroid Build Coastguard Worker           uint16_t this_obj_vreg = GetReceiverRegisterForStringInit(instr);
512*795d594fSAndroid Build Coastguard Worker           // Move the StringFactory.newStringFromChars() result into the register representing
513*795d594fSAndroid Build Coastguard Worker           // "this object" when invoking the string constructor in the original dex instruction.
514*795d594fSAndroid Build Coastguard Worker           // Also move the result into all aliases.
515*795d594fSAndroid Build Coastguard Worker           DCHECK(value.GetL()->IsString());
516*795d594fSAndroid Build Coastguard Worker           SetStringInitValueToAllAliases(shadow_frame, this_obj_vreg, value);
517*795d594fSAndroid Build Coastguard Worker           // Calling string constructor in the original dex code doesn't generate a result value.
518*795d594fSAndroid Build Coastguard Worker           value.SetJ(0);
519*795d594fSAndroid Build Coastguard Worker         }
520*795d594fSAndroid Build Coastguard Worker         new_dex_pc = dex_pc + instr->SizeInCodeUnits();
521*795d594fSAndroid Build Coastguard Worker       } else if (instr->Opcode() == Instruction::NEW_INSTANCE) {
522*795d594fSAndroid Build Coastguard Worker         // A NEW_INSTANCE is simply re-executed, including
523*795d594fSAndroid Build Coastguard Worker         // "new-instance String" which is compiled into a call into
524*795d594fSAndroid Build Coastguard Worker         // StringFactory.newEmptyString().
525*795d594fSAndroid Build Coastguard Worker         DCHECK_EQ(new_dex_pc, dex_pc);
526*795d594fSAndroid Build Coastguard Worker       } else {
527*795d594fSAndroid Build Coastguard Worker         DCHECK(deopt_method_type == DeoptimizationMethodType::kDefault);
528*795d594fSAndroid Build Coastguard Worker         DCHECK_EQ(frame_cnt, 0u);
529*795d594fSAndroid Build Coastguard Worker         // By default, we re-execute the dex instruction since if they are not
530*795d594fSAndroid Build Coastguard Worker         // an invoke, so that we don't have to decode the dex instruction to move
531*795d594fSAndroid Build Coastguard Worker         // result into the right vreg. All slow paths have been audited to be
532*795d594fSAndroid Build Coastguard Worker         // idempotent except monitor-enter/exit and invocation stubs.
533*795d594fSAndroid Build Coastguard Worker         // TODO: move result and advance dex pc. That also requires that we
534*795d594fSAndroid Build Coastguard Worker         // can tell the return type of a runtime method, possibly by decoding
535*795d594fSAndroid Build Coastguard Worker         // the dex instruction at the caller.
536*795d594fSAndroid Build Coastguard Worker         DCHECK_EQ(new_dex_pc, dex_pc);
537*795d594fSAndroid Build Coastguard Worker       }
538*795d594fSAndroid Build Coastguard Worker     } else {
539*795d594fSAndroid Build Coastguard Worker       // Nothing to do, the dex_pc is the one at which the code requested
540*795d594fSAndroid Build Coastguard Worker       // the deoptimization.
541*795d594fSAndroid Build Coastguard Worker       DCHECK_EQ(frame_cnt, 0u);
542*795d594fSAndroid Build Coastguard Worker       DCHECK_EQ(new_dex_pc, dex_pc);
543*795d594fSAndroid Build Coastguard Worker     }
544*795d594fSAndroid Build Coastguard Worker     if (new_dex_pc != dex::kDexNoIndex) {
545*795d594fSAndroid Build Coastguard Worker       shadow_frame->SetDexPC(new_dex_pc);
546*795d594fSAndroid Build Coastguard Worker       value = Execute(self,
547*795d594fSAndroid Build Coastguard Worker                       accessor,
548*795d594fSAndroid Build Coastguard Worker                       *shadow_frame,
549*795d594fSAndroid Build Coastguard Worker                       value,
550*795d594fSAndroid Build Coastguard Worker                       /* stay_in_interpreter= */ true,
551*795d594fSAndroid Build Coastguard Worker                       /* from_deoptimize= */ true);
552*795d594fSAndroid Build Coastguard Worker     }
553*795d594fSAndroid Build Coastguard Worker     ShadowFrame* old_frame = shadow_frame;
554*795d594fSAndroid Build Coastguard Worker     shadow_frame = shadow_frame->GetLink();
555*795d594fSAndroid Build Coastguard Worker     ShadowFrame::DeleteDeoptimizedFrame(old_frame);
556*795d594fSAndroid Build Coastguard Worker     // Following deoptimizations of shadow frames must be at invocation point
557*795d594fSAndroid Build Coastguard Worker     // and should advance dex pc past the invoke instruction.
558*795d594fSAndroid Build Coastguard Worker     from_code = false;
559*795d594fSAndroid Build Coastguard Worker     deopt_method_type = DeoptimizationMethodType::kDefault;
560*795d594fSAndroid Build Coastguard Worker     frame_cnt++;
561*795d594fSAndroid Build Coastguard Worker   }
562*795d594fSAndroid Build Coastguard Worker   ret_val->SetJ(value.GetJ());
563*795d594fSAndroid Build Coastguard Worker }
564*795d594fSAndroid Build Coastguard Worker 
565*795d594fSAndroid Build Coastguard Worker NO_STACK_PROTECTOR
EnterInterpreterFromEntryPoint(Thread * self,const CodeItemDataAccessor & accessor,ShadowFrame * shadow_frame)566*795d594fSAndroid Build Coastguard Worker JValue EnterInterpreterFromEntryPoint(Thread* self, const CodeItemDataAccessor& accessor,
567*795d594fSAndroid Build Coastguard Worker                                       ShadowFrame* shadow_frame) {
568*795d594fSAndroid Build Coastguard Worker   DCHECK_EQ(self, Thread::Current());
569*795d594fSAndroid Build Coastguard Worker   bool implicit_check = Runtime::Current()->GetImplicitStackOverflowChecks();
570*795d594fSAndroid Build Coastguard Worker   if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEndForInterpreter(implicit_check))) {
571*795d594fSAndroid Build Coastguard Worker     ThrowStackOverflowError<kNativeStackType>(self);
572*795d594fSAndroid Build Coastguard Worker     return JValue();
573*795d594fSAndroid Build Coastguard Worker   }
574*795d594fSAndroid Build Coastguard Worker 
575*795d594fSAndroid Build Coastguard Worker   jit::Jit* jit = Runtime::Current()->GetJit();
576*795d594fSAndroid Build Coastguard Worker   if (jit != nullptr) {
577*795d594fSAndroid Build Coastguard Worker     jit->NotifyCompiledCodeToInterpreterTransition(self, shadow_frame->GetMethod());
578*795d594fSAndroid Build Coastguard Worker   }
579*795d594fSAndroid Build Coastguard Worker   return Execute(self, accessor, *shadow_frame, JValue());
580*795d594fSAndroid Build Coastguard Worker }
581*795d594fSAndroid Build Coastguard Worker 
582*795d594fSAndroid Build Coastguard Worker NO_STACK_PROTECTOR
ArtInterpreterToInterpreterBridge(Thread * self,const CodeItemDataAccessor & accessor,ShadowFrame * shadow_frame,JValue * result)583*795d594fSAndroid Build Coastguard Worker void ArtInterpreterToInterpreterBridge(Thread* self,
584*795d594fSAndroid Build Coastguard Worker                                        const CodeItemDataAccessor& accessor,
585*795d594fSAndroid Build Coastguard Worker                                        ShadowFrame* shadow_frame,
586*795d594fSAndroid Build Coastguard Worker                                        JValue* result) {
587*795d594fSAndroid Build Coastguard Worker   bool implicit_check = Runtime::Current()->GetImplicitStackOverflowChecks();
588*795d594fSAndroid Build Coastguard Worker   if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEndForInterpreter(implicit_check))) {
589*795d594fSAndroid Build Coastguard Worker     ThrowStackOverflowError<kNativeStackType>(self);
590*795d594fSAndroid Build Coastguard Worker     return;
591*795d594fSAndroid Build Coastguard Worker   }
592*795d594fSAndroid Build Coastguard Worker 
593*795d594fSAndroid Build Coastguard Worker   self->PushShadowFrame(shadow_frame);
594*795d594fSAndroid Build Coastguard Worker 
595*795d594fSAndroid Build Coastguard Worker   if (LIKELY(!shadow_frame->GetMethod()->IsNative())) {
596*795d594fSAndroid Build Coastguard Worker     result->SetJ(Execute(self, accessor, *shadow_frame, JValue()).GetJ());
597*795d594fSAndroid Build Coastguard Worker   } else {
598*795d594fSAndroid Build Coastguard Worker     // We don't expect to be asked to interpret native code (which is entered via a JNI compiler
599*795d594fSAndroid Build Coastguard Worker     // generated stub) except during testing and image writing.
600*795d594fSAndroid Build Coastguard Worker     CHECK(!Runtime::Current()->IsStarted());
601*795d594fSAndroid Build Coastguard Worker     bool is_static = shadow_frame->GetMethod()->IsStatic();
602*795d594fSAndroid Build Coastguard Worker     ObjPtr<mirror::Object> receiver = is_static ? nullptr : shadow_frame->GetVRegReference(0);
603*795d594fSAndroid Build Coastguard Worker     uint32_t* args = shadow_frame->GetVRegArgs(is_static ? 0 : 1);
604*795d594fSAndroid Build Coastguard Worker     UnstartedRuntime::Jni(self, shadow_frame->GetMethod(), receiver.Ptr(), args, result);
605*795d594fSAndroid Build Coastguard Worker   }
606*795d594fSAndroid Build Coastguard Worker 
607*795d594fSAndroid Build Coastguard Worker   self->PopShadowFrame();
608*795d594fSAndroid Build Coastguard Worker }
609*795d594fSAndroid Build Coastguard Worker 
CheckInterpreterAsmConstants()610*795d594fSAndroid Build Coastguard Worker void CheckInterpreterAsmConstants() {
611*795d594fSAndroid Build Coastguard Worker   CheckNterpAsmConstants();
612*795d594fSAndroid Build Coastguard Worker }
613*795d594fSAndroid Build Coastguard Worker 
PrevFrameWillRetry(Thread * self,const ShadowFrame & frame)614*795d594fSAndroid Build Coastguard Worker bool PrevFrameWillRetry(Thread* self, const ShadowFrame& frame) {
615*795d594fSAndroid Build Coastguard Worker   ShadowFrame* prev_frame = frame.GetLink();
616*795d594fSAndroid Build Coastguard Worker   if (prev_frame == nullptr) {
617*795d594fSAndroid Build Coastguard Worker     NthCallerVisitor vis(self, 1, false);
618*795d594fSAndroid Build Coastguard Worker     vis.WalkStack();
619*795d594fSAndroid Build Coastguard Worker     prev_frame = vis.GetCurrentShadowFrame();
620*795d594fSAndroid Build Coastguard Worker     if (prev_frame == nullptr) {
621*795d594fSAndroid Build Coastguard Worker       prev_frame = self->FindDebuggerShadowFrame(vis.GetFrameId());
622*795d594fSAndroid Build Coastguard Worker     }
623*795d594fSAndroid Build Coastguard Worker   }
624*795d594fSAndroid Build Coastguard Worker   return prev_frame != nullptr && prev_frame->GetForceRetryInstruction();
625*795d594fSAndroid Build Coastguard Worker }
626*795d594fSAndroid Build Coastguard Worker 
627*795d594fSAndroid Build Coastguard Worker }  // namespace interpreter
628*795d594fSAndroid Build Coastguard Worker }  // namespace art
629