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 #ifndef ART_DEX2OAT_DRIVER_COMPILER_DRIVER_INL_H_
18 #define ART_DEX2OAT_DRIVER_COMPILER_DRIVER_INL_H_
19 
20 #include "compiler_driver.h"
21 
22 #include "art_field-inl.h"
23 #include "art_method-inl.h"
24 #include "base/enums.h"
25 #include "class_linker-inl.h"
26 #include "driver/dex_compilation_unit.h"
27 #include "handle_scope-inl.h"
28 #include "mirror/class_loader.h"
29 #include "mirror/dex_cache-inl.h"
30 #include "runtime.h"
31 #include "scoped_thread_state_change-inl.h"
32 
33 namespace art {
34 
ResolveClass(const ScopedObjectAccess & soa,Handle<mirror::DexCache> dex_cache,Handle<mirror::ClassLoader> class_loader,dex::TypeIndex cls_index,const DexCompilationUnit * mUnit)35 inline ObjPtr<mirror::Class> CompilerDriver::ResolveClass(
36     const ScopedObjectAccess& soa,
37     Handle<mirror::DexCache> dex_cache,
38     Handle<mirror::ClassLoader> class_loader,
39     dex::TypeIndex cls_index,
40     const DexCompilationUnit* mUnit) {
41   DCHECK_EQ(dex_cache->GetDexFile(), mUnit->GetDexFile());
42   DCHECK_EQ(class_loader.Get(), mUnit->GetClassLoader().Get());
43   ObjPtr<mirror::Class> cls =
44       mUnit->GetClassLinker()->ResolveType(cls_index, dex_cache, class_loader);
45   DCHECK_EQ(cls == nullptr, soa.Self()->IsExceptionPending());
46   if (UNLIKELY(cls == nullptr)) {
47     // Clean up any exception left by type resolution.
48     soa.Self()->ClearException();
49   }
50   return cls;
51 }
52 
ResolveCompilingMethodsClass(const ScopedObjectAccess & soa,Handle<mirror::DexCache> dex_cache,Handle<mirror::ClassLoader> class_loader,const DexCompilationUnit * mUnit)53 inline ObjPtr<mirror::Class> CompilerDriver::ResolveCompilingMethodsClass(
54     const ScopedObjectAccess& soa,
55     Handle<mirror::DexCache> dex_cache,
56     Handle<mirror::ClassLoader> class_loader,
57     const DexCompilationUnit* mUnit) {
58   DCHECK_EQ(dex_cache->GetDexFile(), mUnit->GetDexFile());
59   DCHECK_EQ(class_loader.Get(), mUnit->GetClassLoader().Get());
60   const dex::MethodId& referrer_method_id =
61       mUnit->GetDexFile()->GetMethodId(mUnit->GetDexMethodIndex());
62   return ResolveClass(soa, dex_cache, class_loader, referrer_method_id.class_idx_, mUnit);
63 }
64 
ResolveField(const ScopedObjectAccess & soa,Handle<mirror::DexCache> dex_cache,Handle<mirror::ClassLoader> class_loader,uint32_t field_idx,bool is_static)65 inline ArtField* CompilerDriver::ResolveField(const ScopedObjectAccess& soa,
66                                               Handle<mirror::DexCache> dex_cache,
67                                               Handle<mirror::ClassLoader> class_loader,
68                                               uint32_t field_idx,
69                                               bool is_static) {
70   ArtField* resolved_field = Runtime::Current()->GetClassLinker()->ResolveField(
71       field_idx, dex_cache, class_loader, is_static);
72   DCHECK_EQ(resolved_field == nullptr, soa.Self()->IsExceptionPending());
73   if (UNLIKELY(resolved_field == nullptr)) {
74     // Clean up any exception left by type resolution.
75     soa.Self()->ClearException();
76     return nullptr;
77   }
78   if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
79     // ClassLinker can return a field of the wrong kind directly from the DexCache.
80     // Silently return null on such incompatible class change.
81     return nullptr;
82   }
83   return resolved_field;
84 }
85 
IsFastInstanceField(ObjPtr<mirror::DexCache> dex_cache,ObjPtr<mirror::Class> referrer_class,ArtField * resolved_field,uint16_t field_idx)86 inline std::pair<bool, bool> CompilerDriver::IsFastInstanceField(
87     ObjPtr<mirror::DexCache> dex_cache,
88     ObjPtr<mirror::Class> referrer_class,
89     ArtField* resolved_field,
90     uint16_t field_idx) {
91   DCHECK(!resolved_field->IsStatic());
92   ObjPtr<mirror::Class> fields_class = resolved_field->GetDeclaringClass();
93   bool fast_get = referrer_class != nullptr &&
94       referrer_class->CanAccessResolvedField(fields_class,
95                                              resolved_field,
96                                              dex_cache,
97                                              field_idx);
98   bool fast_put = fast_get && (!resolved_field->IsFinal() || fields_class == referrer_class);
99   return std::make_pair(fast_get, fast_put);
100 }
101 
102 }  // namespace art
103 
104 #endif  // ART_DEX2OAT_DRIVER_COMPILER_DRIVER_INL_H_
105