1 /*
2  * Copyright (C) 2014 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 #ifndef ART_RUNTIME_RUNTIME_INL_H_
18 #define ART_RUNTIME_RUNTIME_INL_H_
19 
20 #include "runtime.h"
21 
22 #include "arch/instruction_set.h"
23 #include "art_method.h"
24 #include "base/callee_save_type.h"
25 #include "base/casts.h"
26 #include "base/mutex.h"
27 #include "entrypoints/quick/callee_save_frame.h"
28 #include "gc_root-inl.h"
29 #include "interpreter/mterp/mterp.h"
30 #include "obj_ptr-inl.h"
31 #include "thread_list.h"
32 
33 namespace art {
34 
IsClearedJniWeakGlobal(ObjPtr<mirror::Object> obj)35 inline bool Runtime::IsClearedJniWeakGlobal(ObjPtr<mirror::Object> obj) {
36   return obj == GetClearedJniWeakGlobal();
37 }
38 
GetClearedJniWeakGlobal()39 inline mirror::Object* Runtime::GetClearedJniWeakGlobal() {
40   mirror::Object* obj = sentinel_.Read();
41   DCHECK(obj != nullptr);
42   return obj;
43 }
44 
GetRuntimeMethodFrameInfo(ArtMethod * method)45 inline QuickMethodFrameInfo Runtime::GetRuntimeMethodFrameInfo(ArtMethod* method) {
46   DCHECK(method != nullptr);
47   DCHECK_EQ(instruction_set_, kRuntimeISA);
48   // Cannot be imt-conflict-method or resolution-method.
49   DCHECK_NE(method, GetImtConflictMethod());
50   DCHECK_NE(method, GetResolutionMethod());
51   // Don't use GetCalleeSaveMethod(), some tests don't set all callee save methods.
52   if (method == GetCalleeSaveMethodUnchecked(CalleeSaveType::kSaveRefsAndArgs)) {
53     return RuntimeCalleeSaveFrame::GetMethodFrameInfo(CalleeSaveType::kSaveRefsAndArgs);
54   } else if (method == GetCalleeSaveMethodUnchecked(CalleeSaveType::kSaveAllCalleeSaves)) {
55     return RuntimeCalleeSaveFrame::GetMethodFrameInfo(CalleeSaveType::kSaveAllCalleeSaves);
56   } else if (method == GetCalleeSaveMethodUnchecked(CalleeSaveType::kSaveRefsOnly)) {
57     return RuntimeCalleeSaveFrame::GetMethodFrameInfo(CalleeSaveType::kSaveRefsOnly);
58   } else {
59     DCHECK(method == GetCalleeSaveMethodUnchecked(CalleeSaveType::kSaveEverything) ||
60            method == GetCalleeSaveMethodUnchecked(CalleeSaveType::kSaveEverythingForClinit) ||
61            method == GetCalleeSaveMethodUnchecked(CalleeSaveType::kSaveEverythingForSuspendCheck));
62     return RuntimeCalleeSaveFrame::GetMethodFrameInfo(CalleeSaveType::kSaveEverything);
63   }
64 }
65 
GetResolutionMethod()66 inline ArtMethod* Runtime::GetResolutionMethod() {
67   CHECK(HasResolutionMethod());
68   return resolution_method_;
69 }
70 
GetImtConflictMethod()71 inline ArtMethod* Runtime::GetImtConflictMethod() {
72   CHECK(HasImtConflictMethod());
73   return imt_conflict_method_;
74 }
75 
GetImtUnimplementedMethod()76 inline ArtMethod* Runtime::GetImtUnimplementedMethod() {
77   CHECK(imt_unimplemented_method_ != nullptr);
78   return imt_unimplemented_method_;
79 }
80 
GetCalleeSaveMethod(CalleeSaveType type)81 inline ArtMethod* Runtime::GetCalleeSaveMethod(CalleeSaveType type)
82     REQUIRES_SHARED(Locks::mutator_lock_) {
83   DCHECK(HasCalleeSaveMethod(type));
84   return GetCalleeSaveMethodUnchecked(type);
85 }
86 
GetCalleeSaveMethodUnchecked(CalleeSaveType type)87 inline ArtMethod* Runtime::GetCalleeSaveMethodUnchecked(CalleeSaveType type)
88     REQUIRES_SHARED(Locks::mutator_lock_) {
89   return reinterpret_cast64<ArtMethod*>(callee_save_methods_[static_cast<size_t>(type)]);
90 }
91 
92 template<typename Action>
DoAndMaybeSwitchInterpreter(Action lamda)93 void Runtime::DoAndMaybeSwitchInterpreter(Action lamda) {
94   MutexLock tll_mu(Thread::Current(), *Locks::thread_list_lock_);
95   lamda();
96   Runtime::Current()->GetThreadList()->ForEach([](Thread* thread, void*) {
97       thread->tls32_.use_mterp.store(interpreter::CanUseMterp());
98   }, nullptr);
99 }
100 
101 }  // namespace art
102 
103 #endif  // ART_RUNTIME_RUNTIME_INL_H_
104