blob: 9948c82663ed0d8035e4c98c612f5f6e7e2f8cdc [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"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070021
Andreas Gampe53c913b2014-08-12 23:19:23 -070022#include "dex_compilation_unit.h"
Vladimir Markobe0e5462014-02-26 11:24:15 +000023#include "mirror/art_field-inl.h"
Vladimir Markof096aad2014-01-23 15:51:58 +000024#include "mirror/art_method-inl.h"
Vladimir Markobe0e5462014-02-26 11:24:15 +000025#include "mirror/class_loader.h"
Vladimir Markof096aad2014-01-23 15:51:58 +000026#include "mirror/dex_cache-inl.h"
Vladimir Markobe0e5462014-02-26 11:24:15 +000027#include "mirror/art_field-inl.h"
28#include "scoped_thread_state_change.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070029#include "handle_scope-inl.h"
Vladimir Markobe0e5462014-02-26 11:24:15 +000030
31namespace art {
32
33inline mirror::DexCache* CompilerDriver::GetDexCache(const DexCompilationUnit* mUnit) {
34 return mUnit->GetClassLinker()->FindDexCache(*mUnit->GetDexFile());
35}
36
37inline mirror::ClassLoader* CompilerDriver::GetClassLoader(ScopedObjectAccess& soa,
38 const DexCompilationUnit* mUnit) {
39 return soa.Decode<mirror::ClassLoader*>(mUnit->GetClassLoader());
40}
41
42inline mirror::Class* CompilerDriver::ResolveCompilingMethodsClass(
Nicolas Geoffraye5038322014-07-04 09:41:32 +010043 const ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache,
Mathieu Chartier0cd81352014-05-22 16:48:55 -070044 Handle<mirror::ClassLoader> class_loader, const DexCompilationUnit* mUnit) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070045 DCHECK_EQ(dex_cache->GetDexFile(), mUnit->GetDexFile());
46 DCHECK_EQ(class_loader.Get(), soa.Decode<mirror::ClassLoader*>(mUnit->GetClassLoader()));
Vladimir Markobe0e5462014-02-26 11:24:15 +000047 const DexFile::MethodId& referrer_method_id =
48 mUnit->GetDexFile()->GetMethodId(mUnit->GetDexMethodIndex());
49 mirror::Class* referrer_class = mUnit->GetClassLinker()->ResolveType(
50 *mUnit->GetDexFile(), referrer_method_id.class_idx_, dex_cache, class_loader);
51 DCHECK_EQ(referrer_class == nullptr, soa.Self()->IsExceptionPending());
52 if (UNLIKELY(referrer_class == nullptr)) {
53 // Clean up any exception left by type resolution.
54 soa.Self()->ClearException();
55 }
56 return referrer_class;
57}
58
Nicolas Geoffraya5ca8882015-02-24 08:10:57 +000059inline mirror::ArtField* CompilerDriver::ResolveField(
Nicolas Geoffraye5038322014-07-04 09:41:32 +010060 const ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache,
Nicolas Geoffraya5ca8882015-02-24 08:10:57 +000061 Handle<mirror::ClassLoader> class_loader, const DexCompilationUnit* mUnit,
Vladimir Markobe0e5462014-02-26 11:24:15 +000062 uint32_t field_idx, bool is_static) {
Nicolas Geoffraya5ca8882015-02-24 08:10:57 +000063 DCHECK_EQ(dex_cache->GetDexFile(), mUnit->GetDexFile());
64 DCHECK_EQ(class_loader.Get(), soa.Decode<mirror::ClassLoader*>(mUnit->GetClassLoader()));
65 mirror::ArtField* resolved_field = mUnit->GetClassLinker()->ResolveField(
66 *mUnit->GetDexFile(), field_idx, dex_cache, class_loader, is_static);
Vladimir Markobe0e5462014-02-26 11:24:15 +000067 DCHECK_EQ(resolved_field == nullptr, soa.Self()->IsExceptionPending());
68 if (UNLIKELY(resolved_field == nullptr)) {
69 // Clean up any exception left by type resolution.
70 soa.Self()->ClearException();
71 return nullptr;
72 }
73 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
74 // ClassLinker can return a field of the wrong kind directly from the DexCache.
75 // Silently return nullptr on such incompatible class change.
76 return nullptr;
77 }
78 return resolved_field;
79}
80
81inline void CompilerDriver::GetResolvedFieldDexFileLocation(
82 mirror::ArtField* resolved_field, const DexFile** declaring_dex_file,
83 uint16_t* declaring_class_idx, uint16_t* declaring_field_idx) {
84 mirror::Class* declaring_class = resolved_field->GetDeclaringClass();
85 *declaring_dex_file = declaring_class->GetDexCache()->GetDexFile();
86 *declaring_class_idx = declaring_class->GetDexTypeIndex();
87 *declaring_field_idx = resolved_field->GetDexFieldIndex();
88}
89
90inline bool CompilerDriver::IsFieldVolatile(mirror::ArtField* field) {
91 return field->IsVolatile();
92}
93
Vladimir Marko66c6d7b2014-10-16 15:41:48 +010094inline MemberOffset CompilerDriver::GetFieldOffset(mirror::ArtField* field) {
95 return field->GetOffset();
96}
97
Vladimir Markobe0e5462014-02-26 11:24:15 +000098inline std::pair<bool, bool> CompilerDriver::IsFastInstanceField(
99 mirror::DexCache* dex_cache, mirror::Class* referrer_class,
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100100 mirror::ArtField* resolved_field, uint16_t field_idx) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000101 DCHECK(!resolved_field->IsStatic());
102 mirror::Class* fields_class = resolved_field->GetDeclaringClass();
103 bool fast_get = referrer_class != nullptr &&
104 referrer_class->CanAccessResolvedField(fields_class, resolved_field,
105 dex_cache, field_idx);
106 bool fast_put = fast_get && (!resolved_field->IsFinal() || fields_class == referrer_class);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000107 return std::make_pair(fast_get, fast_put);
108}
109
110inline std::pair<bool, bool> CompilerDriver::IsFastStaticField(
111 mirror::DexCache* dex_cache, mirror::Class* referrer_class,
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100112 mirror::ArtField* resolved_field, uint16_t field_idx, uint32_t* storage_index) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000113 DCHECK(resolved_field->IsStatic());
114 if (LIKELY(referrer_class != nullptr)) {
115 mirror::Class* fields_class = resolved_field->GetDeclaringClass();
116 if (fields_class == referrer_class) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000117 *storage_index = fields_class->GetDexTypeIndex();
Vladimir Markobe0e5462014-02-26 11:24:15 +0000118 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.
Ian Rogers08f1f502014-12-02 15:04:37 -0800135 std::string temp;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000136 const DexFile::StringId* string_id =
Ian Rogers08f1f502014-12-02 15:04:37 -0800137 dex_file->FindStringId(resolved_field->GetDeclaringClass()->GetDescriptor(&temp));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000138 if (string_id != nullptr) {
139 const DexFile::TypeId* type_id =
140 dex_file->FindTypeId(dex_file->GetIndexForStringId(*string_id));
141 if (type_id != nullptr) {
142 // medium path, needs check of static storage base being initialized
143 storage_idx = dex_file->GetIndexForTypeId(*type_id);
144 }
145 }
146 }
147 if (storage_idx != DexFile::kDexNoIndex) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000148 *storage_index = storage_idx;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000149 return std::make_pair(true, !resolved_field->IsFinal());
150 }
151 }
152 }
153 // Conservative defaults.
Vladimir Markobe0e5462014-02-26 11:24:15 +0000154 *storage_index = DexFile::kDexNoIndex;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000155 return std::make_pair(false, false);
156}
157
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100158inline bool CompilerDriver::IsStaticFieldInReferrerClass(mirror::Class* referrer_class,
159 mirror::ArtField* resolved_field) {
160 DCHECK(resolved_field->IsStatic());
161 mirror::Class* fields_class = resolved_field->GetDeclaringClass();
162 return referrer_class == fields_class;
163}
164
165inline bool CompilerDriver::IsStaticFieldsClassInitialized(mirror::Class* referrer_class,
166 mirror::ArtField* resolved_field) {
167 DCHECK(resolved_field->IsStatic());
168 mirror::Class* fields_class = resolved_field->GetDeclaringClass();
169 return fields_class == referrer_class || fields_class->IsInitialized();
170}
171
Vladimir Markof096aad2014-01-23 15:51:58 +0000172inline mirror::ArtMethod* CompilerDriver::ResolveMethod(
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700173 ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache,
174 Handle<mirror::ClassLoader> class_loader, const DexCompilationUnit* mUnit,
Nicolas Geoffraya5ca8882015-02-24 08:10:57 +0000175 uint32_t method_idx, InvokeType invoke_type) {
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700176 DCHECK_EQ(dex_cache->GetDexFile(), mUnit->GetDexFile());
177 DCHECK_EQ(class_loader.Get(), soa.Decode<mirror::ClassLoader*>(mUnit->GetClassLoader()));
Vladimir Markof096aad2014-01-23 15:51:58 +0000178 mirror::ArtMethod* resolved_method = mUnit->GetClassLinker()->ResolveMethod(
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700179 *mUnit->GetDexFile(), method_idx, dex_cache, class_loader, NullHandle<mirror::ArtMethod>(),
180 invoke_type);
Vladimir Markof096aad2014-01-23 15:51:58 +0000181 DCHECK_EQ(resolved_method == nullptr, soa.Self()->IsExceptionPending());
182 if (UNLIKELY(resolved_method == nullptr)) {
183 // Clean up any exception left by type resolution.
184 soa.Self()->ClearException();
185 return nullptr;
186 }
Nicolas Geoffraya5ca8882015-02-24 08:10:57 +0000187 if (UNLIKELY(resolved_method->CheckIncompatibleClassChange(invoke_type))) {
Vladimir Markof096aad2014-01-23 15:51:58 +0000188 // Silently return nullptr on incompatible class change.
189 return nullptr;
190 }
191 return resolved_method;
192}
193
194inline void CompilerDriver::GetResolvedMethodDexFileLocation(
195 mirror::ArtMethod* resolved_method, const DexFile** declaring_dex_file,
196 uint16_t* declaring_class_idx, uint16_t* declaring_method_idx) {
197 mirror::Class* declaring_class = resolved_method->GetDeclaringClass();
198 *declaring_dex_file = declaring_class->GetDexCache()->GetDexFile();
199 *declaring_class_idx = declaring_class->GetDexTypeIndex();
200 *declaring_method_idx = resolved_method->GetDexMethodIndex();
201}
202
203inline uint16_t CompilerDriver::GetResolvedMethodVTableIndex(
204 mirror::ArtMethod* resolved_method, InvokeType type) {
205 if (type == kVirtual || type == kSuper) {
206 return resolved_method->GetMethodIndex();
207 } else if (type == kInterface) {
208 return resolved_method->GetDexMethodIndex();
209 } else {
210 return DexFile::kDexNoIndex16;
211 }
212}
213
214inline int CompilerDriver::IsFastInvoke(
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700215 ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache,
216 Handle<mirror::ClassLoader> class_loader, const DexCompilationUnit* mUnit,
Vladimir Markof096aad2014-01-23 15:51:58 +0000217 mirror::Class* referrer_class, mirror::ArtMethod* resolved_method, InvokeType* invoke_type,
218 MethodReference* target_method, const MethodReference* devirt_target,
219 uintptr_t* direct_code, uintptr_t* direct_method) {
220 // Don't try to fast-path if we don't understand the caller's class.
221 if (UNLIKELY(referrer_class == nullptr)) {
222 return 0;
223 }
224 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
225 if (UNLIKELY(!referrer_class->CanAccessResolvedMethod(methods_class, resolved_method,
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700226 dex_cache.Get(),
Vladimir Markof096aad2014-01-23 15:51:58 +0000227 target_method->dex_method_index))) {
228 return 0;
229 }
Nicolas Geoffraya5ca8882015-02-24 08:10:57 +0000230
Vladimir Markof096aad2014-01-23 15:51:58 +0000231 // Sharpen a virtual call into a direct call when the target is known not to have been
232 // overridden (ie is final).
Nicolas Geoffraya5ca8882015-02-24 08:10:57 +0000233 bool can_sharpen_virtual_based_on_type =
Vladimir Markof096aad2014-01-23 15:51:58 +0000234 (*invoke_type == kVirtual) && (resolved_method->IsFinal() || methods_class->IsFinal());
235 // For invoke-super, ensure the vtable index will be correct to dispatch in the vtable of
236 // the super class.
Nicolas Geoffraya5ca8882015-02-24 08:10:57 +0000237 bool can_sharpen_super_based_on_type = (*invoke_type == kSuper) &&
Vladimir Markof096aad2014-01-23 15:51:58 +0000238 (referrer_class != methods_class) && referrer_class->IsSubClass(methods_class) &&
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700239 resolved_method->GetMethodIndex() < methods_class->GetVTableLength() &&
Vladimir Marko920506d2014-11-18 14:47:31 +0000240 (methods_class->GetVTableEntry(resolved_method->GetMethodIndex()) == resolved_method) &&
241 !resolved_method->IsAbstract();
Vladimir Markof096aad2014-01-23 15:51:58 +0000242
243 if (can_sharpen_virtual_based_on_type || can_sharpen_super_based_on_type) {
244 // Sharpen a virtual call into a direct call. The method_idx is into referrer's
245 // dex cache, check that this resolved method is where we expect it.
Nicolas Geoffraya5ca8882015-02-24 08:10:57 +0000246 CHECK(target_method->dex_file == mUnit->GetDexFile());
247 DCHECK(dex_cache.Get() == mUnit->GetClassLinker()->FindDexCache(*mUnit->GetDexFile()));
248 CHECK(referrer_class->GetDexCache()->GetResolvedMethod(target_method->dex_method_index) ==
249 resolved_method) << PrettyMethod(resolved_method);
Vladimir Markof096aad2014-01-23 15:51:58 +0000250 int stats_flags = kFlagMethodResolved;
Igor Murashkind6dee672014-10-16 18:36:16 -0700251 GetCodeAndMethodForDirectCall(/*out*/invoke_type,
252 kDirect, // Sharp type
253 false, // The dex cache is guaranteed to be available
254 referrer_class, resolved_method,
255 /*out*/&stats_flags,
256 target_method,
257 /*out*/direct_code,
258 /*out*/direct_method);
Vladimir Markof096aad2014-01-23 15:51:58 +0000259 DCHECK_NE(*invoke_type, kSuper) << PrettyMethod(resolved_method);
260 if (*invoke_type == kDirect) {
261 stats_flags |= kFlagsMethodResolvedVirtualMadeDirect;
262 }
263 return stats_flags;
264 }
265
266 if ((*invoke_type == kVirtual || *invoke_type == kInterface) && devirt_target != nullptr) {
267 // Post-verification callback recorded a more precise invoke target based on its type info.
268 mirror::ArtMethod* called_method;
269 ClassLinker* class_linker = mUnit->GetClassLinker();
270 if (LIKELY(devirt_target->dex_file == mUnit->GetDexFile())) {
271 called_method = class_linker->ResolveMethod(*devirt_target->dex_file,
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700272 devirt_target->dex_method_index, dex_cache,
273 class_loader, NullHandle<mirror::ArtMethod>(),
274 kVirtual);
Vladimir Markof096aad2014-01-23 15:51:58 +0000275 } else {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700276 StackHandleScope<1> hs(soa.Self());
277 Handle<mirror::DexCache> target_dex_cache(
278 hs.NewHandle(class_linker->FindDexCache(*devirt_target->dex_file)));
Vladimir Markof096aad2014-01-23 15:51:58 +0000279 called_method = class_linker->ResolveMethod(*devirt_target->dex_file,
280 devirt_target->dex_method_index,
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700281 target_dex_cache, class_loader,
282 NullHandle<mirror::ArtMethod>(), kVirtual);
Vladimir Markof096aad2014-01-23 15:51:58 +0000283 }
284 CHECK(called_method != NULL);
285 CHECK(!called_method->IsAbstract());
286 int stats_flags = kFlagMethodResolved;
Igor Murashkind6dee672014-10-16 18:36:16 -0700287 GetCodeAndMethodForDirectCall(/*out*/invoke_type,
288 kDirect, // Sharp type
289 true, // The dex cache may not be available
290 referrer_class, called_method,
291 /*out*/&stats_flags,
292 target_method,
293 /*out*/direct_code,
294 /*out*/direct_method);
Vladimir Markof096aad2014-01-23 15:51:58 +0000295 DCHECK_NE(*invoke_type, kSuper);
296 if (*invoke_type == kDirect) {
297 stats_flags |= kFlagsMethodResolvedPreciseTypeDevirtualization;
298 }
299 return stats_flags;
300 }
301
302 if (UNLIKELY(*invoke_type == kSuper)) {
303 // Unsharpened super calls are suspicious so go slow-path.
304 return 0;
305 }
306
307 // Sharpening failed so generate a regular resolved method dispatch.
308 int stats_flags = kFlagMethodResolved;
Igor Murashkind6dee672014-10-16 18:36:16 -0700309 GetCodeAndMethodForDirectCall(/*out*/invoke_type,
310 *invoke_type, // Sharp type
311 false, // The dex cache is guaranteed to be available
312 referrer_class, resolved_method,
313 /*out*/&stats_flags,
314 target_method,
315 /*out*/direct_code,
316 /*out*/direct_method);
Vladimir Markof096aad2014-01-23 15:51:58 +0000317 return stats_flags;
318}
319
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100320inline bool CompilerDriver::IsMethodsClassInitialized(mirror::Class* referrer_class,
321 mirror::ArtMethod* resolved_method) {
Vladimir Marko9820b7c2014-01-02 16:40:37 +0000322 if (!resolved_method->IsStatic()) {
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100323 return true;
Vladimir Marko9820b7c2014-01-02 16:40:37 +0000324 }
325 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100326 return methods_class == referrer_class || methods_class->IsInitialized();
Vladimir Marko9820b7c2014-01-02 16:40:37 +0000327}
328
Vladimir Markobe0e5462014-02-26 11:24:15 +0000329} // namespace art
330
331#endif // ART_COMPILER_DRIVER_COMPILER_DRIVER_INL_H_