blob: 1499ae48722d58199298688109ae1ca0206097ea [file] [log] [blame]
Vladimir Markobe0e5462014-02-26 11:24:15 +00001/*
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_COMPILER_DRIVER_COMPILER_DRIVER_INL_H_
18#define ART_COMPILER_DRIVER_COMPILER_DRIVER_INL_H_
19
20#include "compiler_driver.h"
21#include "dex/compiler_ir.h"
22#include "mirror/art_field.h"
23#include "mirror/art_field-inl.h"
24#include "mirror/class_loader.h"
25#include "mirror/dex_cache.h"
26#include "mirror/art_field-inl.h"
27#include "scoped_thread_state_change.h"
Mathieu Chartierc645f1d2014-03-06 18:11:53 -080028#include "sirt_ref-inl.h"
Vladimir Markobe0e5462014-02-26 11:24:15 +000029
30namespace art {
31
32inline mirror::DexCache* CompilerDriver::GetDexCache(const DexCompilationUnit* mUnit) {
33 return mUnit->GetClassLinker()->FindDexCache(*mUnit->GetDexFile());
34}
35
36inline mirror::ClassLoader* CompilerDriver::GetClassLoader(ScopedObjectAccess& soa,
37 const DexCompilationUnit* mUnit) {
38 return soa.Decode<mirror::ClassLoader*>(mUnit->GetClassLoader());
39}
40
41inline mirror::Class* CompilerDriver::ResolveCompilingMethodsClass(
42 ScopedObjectAccess& soa, const SirtRef<mirror::DexCache>& dex_cache,
43 const SirtRef<mirror::ClassLoader>& class_loader, const DexCompilationUnit* mUnit) {
44 DCHECK(dex_cache->GetDexFile() == mUnit->GetDexFile());
45 DCHECK(class_loader.get() == soa.Decode<mirror::ClassLoader*>(mUnit->GetClassLoader()));
46 const DexFile::MethodId& referrer_method_id =
47 mUnit->GetDexFile()->GetMethodId(mUnit->GetDexMethodIndex());
48 mirror::Class* referrer_class = mUnit->GetClassLinker()->ResolveType(
49 *mUnit->GetDexFile(), referrer_method_id.class_idx_, dex_cache, class_loader);
50 DCHECK_EQ(referrer_class == nullptr, soa.Self()->IsExceptionPending());
51 if (UNLIKELY(referrer_class == nullptr)) {
52 // Clean up any exception left by type resolution.
53 soa.Self()->ClearException();
54 }
55 return referrer_class;
56}
57
58inline mirror::ArtField* CompilerDriver::ResolveField(
59 ScopedObjectAccess& soa, const SirtRef<mirror::DexCache>& dex_cache,
60 const SirtRef<mirror::ClassLoader>& class_loader, const DexCompilationUnit* mUnit,
61 uint32_t field_idx, bool is_static) {
62 DCHECK(dex_cache->GetDexFile() == mUnit->GetDexFile());
63 DCHECK(class_loader.get() == soa.Decode<mirror::ClassLoader*>(mUnit->GetClassLoader()));
64 mirror::ArtField* resolved_field = mUnit->GetClassLinker()->ResolveField(
65 *mUnit->GetDexFile(), field_idx, dex_cache, class_loader, is_static);
66 DCHECK_EQ(resolved_field == nullptr, soa.Self()->IsExceptionPending());
67 if (UNLIKELY(resolved_field == nullptr)) {
68 // Clean up any exception left by type resolution.
69 soa.Self()->ClearException();
70 return nullptr;
71 }
72 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
73 // ClassLinker can return a field of the wrong kind directly from the DexCache.
74 // Silently return nullptr on such incompatible class change.
75 return nullptr;
76 }
77 return resolved_field;
78}
79
80inline void CompilerDriver::GetResolvedFieldDexFileLocation(
81 mirror::ArtField* resolved_field, const DexFile** declaring_dex_file,
82 uint16_t* declaring_class_idx, uint16_t* declaring_field_idx) {
83 mirror::Class* declaring_class = resolved_field->GetDeclaringClass();
84 *declaring_dex_file = declaring_class->GetDexCache()->GetDexFile();
85 *declaring_class_idx = declaring_class->GetDexTypeIndex();
86 *declaring_field_idx = resolved_field->GetDexFieldIndex();
87}
88
89inline bool CompilerDriver::IsFieldVolatile(mirror::ArtField* field) {
90 return field->IsVolatile();
91}
92
93inline std::pair<bool, bool> CompilerDriver::IsFastInstanceField(
94 mirror::DexCache* dex_cache, mirror::Class* referrer_class,
95 mirror::ArtField* resolved_field, uint16_t field_idx, MemberOffset* field_offset) {
96 DCHECK(!resolved_field->IsStatic());
97 mirror::Class* fields_class = resolved_field->GetDeclaringClass();
98 bool fast_get = referrer_class != nullptr &&
99 referrer_class->CanAccessResolvedField(fields_class, resolved_field,
100 dex_cache, field_idx);
101 bool fast_put = fast_get && (!resolved_field->IsFinal() || fields_class == referrer_class);
102 *field_offset = fast_get ? resolved_field->GetOffset() : MemberOffset(0u);
103 return std::make_pair(fast_get, fast_put);
104}
105
106inline std::pair<bool, bool> CompilerDriver::IsFastStaticField(
107 mirror::DexCache* dex_cache, mirror::Class* referrer_class,
108 mirror::ArtField* resolved_field, uint16_t field_idx, MemberOffset* field_offset,
109 uint32_t* storage_index, bool* is_referrers_class, bool* is_initialized) {
110 DCHECK(resolved_field->IsStatic());
111 if (LIKELY(referrer_class != nullptr)) {
112 mirror::Class* fields_class = resolved_field->GetDeclaringClass();
113 if (fields_class == referrer_class) {
114 *field_offset = resolved_field->GetOffset();
115 *storage_index = fields_class->GetDexTypeIndex();
116 *is_referrers_class = true; // implies no worrying about class initialization
117 *is_initialized = true;
118 return std::make_pair(true, true);
119 }
120 if (referrer_class->CanAccessResolvedField(fields_class, resolved_field,
121 dex_cache, field_idx)) {
122 // We have the resolved field, we must make it into a index for the referrer
123 // in its static storage (which may fail if it doesn't have a slot for it)
124 // TODO: for images we can elide the static storage base null check
125 // if we know there's a non-null entry in the image
126 const DexFile* dex_file = dex_cache->GetDexFile();
127 uint32_t storage_idx = DexFile::kDexNoIndex;
128 if (LIKELY(fields_class->GetDexCache() == dex_cache)) {
129 // common case where the dex cache of both the referrer and the field are the same,
130 // no need to search the dex file
131 storage_idx = fields_class->GetDexTypeIndex();
132 } else {
133 // Search dex file for localized ssb index, may fail if field's class is a parent
134 // of the class mentioned in the dex file and there is no dex cache entry.
135 const DexFile::StringId* string_id =
136 dex_file->FindStringId(FieldHelper(resolved_field).GetDeclaringClassDescriptor());
137 if (string_id != nullptr) {
138 const DexFile::TypeId* type_id =
139 dex_file->FindTypeId(dex_file->GetIndexForStringId(*string_id));
140 if (type_id != nullptr) {
141 // medium path, needs check of static storage base being initialized
142 storage_idx = dex_file->GetIndexForTypeId(*type_id);
143 }
144 }
145 }
146 if (storage_idx != DexFile::kDexNoIndex) {
147 *field_offset = resolved_field->GetOffset();
148 *storage_index = storage_idx;
149 *is_referrers_class = false;
150 *is_initialized = fields_class->IsInitialized() &&
151 CanAssumeTypeIsPresentInDexCache(*dex_file, storage_idx);
152 return std::make_pair(true, !resolved_field->IsFinal());
153 }
154 }
155 }
156 // Conservative defaults.
157 *field_offset = MemberOffset(0u);
158 *storage_index = DexFile::kDexNoIndex;
159 *is_referrers_class = false;
160 *is_initialized = false;
161 return std::make_pair(false, false);
162}
163
164} // namespace art
165
166#endif // ART_COMPILER_DRIVER_COMPILER_DRIVER_INL_H_