1 /*
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 
17 #include "base/logging.h"  // For VLOG_IS_ON.
18 #include "base/mutex.h"
19 #include "callee_save_frame.h"
20 #include "interpreter/interpreter.h"
21 #include "obj_ptr-inl.h"  // TODO: Find the other include that isn't complete, and clean this up.
22 #include "quick_exception_handler.h"
23 #include "runtime.h"
24 #include "thread.h"
25 
26 namespace art HIDDEN {
27 
artDeoptimizeImpl(Thread * self,DeoptimizationKind kind,bool single_frame,bool skip_method_exit_callbacks)28 NO_RETURN static void artDeoptimizeImpl(Thread* self,
29                                         DeoptimizationKind kind,
30                                         bool single_frame,
31                                         bool skip_method_exit_callbacks)
32     REQUIRES_SHARED(Locks::mutator_lock_) {
33   Runtime::Current()->IncrementDeoptimizationCount(kind);
34   if (VLOG_IS_ON(deopt)) {
35     if (single_frame) {
36       // Deopt logging will be in DeoptimizeSingleFrame. It is there to take advantage of the
37       // specialized visitor that will show whether a method is Quick or Shadow.
38     } else {
39       LOG(INFO) << "Deopting:";
40       self->Dump(LOG_STREAM(INFO));
41     }
42   }
43 
44   self->AssertHasDeoptimizationContext();
45   QuickExceptionHandler exception_handler(self, true);
46   if (single_frame) {
47     exception_handler.DeoptimizeSingleFrame(kind);
48   } else {
49     exception_handler.DeoptimizeStack(skip_method_exit_callbacks);
50   }
51   if (exception_handler.IsFullFragmentDone()) {
52     exception_handler.DoLongJump(true);
53   } else {
54     exception_handler.DeoptimizePartialFragmentFixup();
55     // We cannot smash the caller-saves, as we need the ArtMethod in a parameter register that would
56     // be caller-saved. This has the downside that we cannot track incorrect register usage down the
57     // line.
58     exception_handler.DoLongJump(false);
59   }
60 }
61 
artDeoptimize(Thread * self,bool skip_method_exit_callbacks)62 extern "C" NO_RETURN void artDeoptimize(Thread* self, bool skip_method_exit_callbacks)
63     REQUIRES_SHARED(Locks::mutator_lock_) {
64   ScopedQuickEntrypointChecks sqec(self);
65   artDeoptimizeImpl(self, DeoptimizationKind::kFullFrame, false, skip_method_exit_callbacks);
66 }
67 
68 // This is called directly from compiled code by an HDeoptimize.
artDeoptimizeFromCompiledCode(DeoptimizationKind kind,Thread * self)69 extern "C" NO_RETURN void artDeoptimizeFromCompiledCode(DeoptimizationKind kind, Thread* self)
70     REQUIRES_SHARED(Locks::mutator_lock_) {
71   ScopedQuickEntrypointChecks sqec(self);
72   // Before deoptimizing to interpreter, we must push the deoptimization context.
73   JValue return_value;
74   return_value.SetJ(0);  // we never deoptimize from compiled code with an invoke result.
75   self->PushDeoptimizationContext(return_value,
76                                   /* is_reference= */ false,
77                                   self->GetException(),
78                                   /* from_code= */ true,
79                                   DeoptimizationMethodType::kDefault);
80   // Deopting from compiled code, so method exit haven't run yet. Don't skip method exit callbacks
81   // if required.
82   artDeoptimizeImpl(self, kind, true, /* skip_method_exit_callbacks= */ false);
83 }
84 
85 }  // namespace art
86