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