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