blob: 8babc28db9d0202784fb3b8ee6862be9de9e2a1c [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
Nicolas Geoffray9437b782015-03-25 10:08:51 +000042inline mirror::Class* CompilerDriver::ResolveClass(
43 const ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache,
44 Handle<mirror::ClassLoader> class_loader, uint16_t cls_index,
45 const DexCompilationUnit* mUnit) {
46 DCHECK_EQ(dex_cache->GetDexFile(), mUnit->GetDexFile());
47 DCHECK_EQ(class_loader.Get(), soa.Decode<mirror::ClassLoader*>(mUnit->GetClassLoader()));
48 mirror::Class* cls = mUnit->GetClassLinker()->ResolveType(
49 *mUnit->GetDexFile(), cls_index, dex_cache, class_loader);
50 DCHECK_EQ(cls == nullptr, soa.Self()->IsExceptionPending());
51 if (UNLIKELY(cls == nullptr)) {
52 // Clean up any exception left by type resolution.
53 soa.Self()->ClearException();
54 }
55 return cls;
56}
57
Vladimir Markobe0e5462014-02-26 11:24:15 +000058inline mirror::Class* CompilerDriver::ResolveCompilingMethodsClass(
Nicolas Geoffraye5038322014-07-04 09:41:32 +010059 const ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache,
Mathieu Chartier0cd81352014-05-22 16:48:55 -070060 Handle<mirror::ClassLoader> class_loader, const DexCompilationUnit* mUnit) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070061 DCHECK_EQ(dex_cache->GetDexFile(), mUnit->GetDexFile());
62 DCHECK_EQ(class_loader.Get(), soa.Decode<mirror::ClassLoader*>(mUnit->GetClassLoader()));
Vladimir Markobe0e5462014-02-26 11:24:15 +000063 const DexFile::MethodId& referrer_method_id =
64 mUnit->GetDexFile()->GetMethodId(mUnit->GetDexMethodIndex());
Nicolas Geoffray9437b782015-03-25 10:08:51 +000065 return ResolveClass(soa, dex_cache, class_loader, referrer_method_id.class_idx_, mUnit);
Vladimir Markobe0e5462014-02-26 11:24:15 +000066}
67
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080068inline mirror::ArtField* CompilerDriver::ResolveFieldWithDexFile(
Nicolas Geoffraye5038322014-07-04 09:41:32 +010069 const ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache,
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080070 Handle<mirror::ClassLoader> class_loader, const DexFile* dex_file,
Vladimir Markobe0e5462014-02-26 11:24:15 +000071 uint32_t field_idx, bool is_static) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080072 DCHECK_EQ(dex_cache->GetDexFile(), dex_file);
73 mirror::ArtField* resolved_field = Runtime::Current()->GetClassLinker()->ResolveField(
74 *dex_file, field_idx, dex_cache, class_loader, is_static);
Vladimir Markobe0e5462014-02-26 11:24:15 +000075 DCHECK_EQ(resolved_field == nullptr, soa.Self()->IsExceptionPending());
76 if (UNLIKELY(resolved_field == nullptr)) {
77 // Clean up any exception left by type resolution.
78 soa.Self()->ClearException();
79 return nullptr;
80 }
81 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
82 // ClassLinker can return a field of the wrong kind directly from the DexCache.
83 // Silently return nullptr on such incompatible class change.
84 return nullptr;
85 }
86 return resolved_field;
87}
88
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080089inline mirror::DexCache* CompilerDriver::FindDexCache(const DexFile* dex_file) {
90 return Runtime::Current()->GetClassLinker()->FindDexCache(*dex_file);
91}
92
93inline mirror::ArtField* CompilerDriver::ResolveField(
94 const ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache,
95 Handle<mirror::ClassLoader> class_loader, const DexCompilationUnit* mUnit,
96 uint32_t field_idx, bool is_static) {
97 DCHECK_EQ(class_loader.Get(), soa.Decode<mirror::ClassLoader*>(mUnit->GetClassLoader()));
98 return ResolveFieldWithDexFile(soa, dex_cache, class_loader, mUnit->GetDexFile(), field_idx,
99 is_static);
100}
101
Vladimir Markobe0e5462014-02-26 11:24:15 +0000102inline void CompilerDriver::GetResolvedFieldDexFileLocation(
103 mirror::ArtField* resolved_field, const DexFile** declaring_dex_file,
104 uint16_t* declaring_class_idx, uint16_t* declaring_field_idx) {
105 mirror::Class* declaring_class = resolved_field->GetDeclaringClass();
106 *declaring_dex_file = declaring_class->GetDexCache()->GetDexFile();
107 *declaring_class_idx = declaring_class->GetDexTypeIndex();
108 *declaring_field_idx = resolved_field->GetDexFieldIndex();
109}
110
111inline bool CompilerDriver::IsFieldVolatile(mirror::ArtField* field) {
112 return field->IsVolatile();
113}
114
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100115inline MemberOffset CompilerDriver::GetFieldOffset(mirror::ArtField* field) {
116 return field->GetOffset();
117}
118
Vladimir Markobe0e5462014-02-26 11:24:15 +0000119inline std::pair<bool, bool> CompilerDriver::IsFastInstanceField(
120 mirror::DexCache* dex_cache, mirror::Class* referrer_class,
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100121 mirror::ArtField* resolved_field, uint16_t field_idx) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000122 DCHECK(!resolved_field->IsStatic());
123 mirror::Class* fields_class = resolved_field->GetDeclaringClass();
124 bool fast_get = referrer_class != nullptr &&
125 referrer_class->CanAccessResolvedField(fields_class, resolved_field,
126 dex_cache, field_idx);
127 bool fast_put = fast_get && (!resolved_field->IsFinal() || fields_class == referrer_class);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000128 return std::make_pair(fast_get, fast_put);
129}
130
131inline std::pair<bool, bool> CompilerDriver::IsFastStaticField(
132 mirror::DexCache* dex_cache, mirror::Class* referrer_class,
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100133 mirror::ArtField* resolved_field, uint16_t field_idx, uint32_t* storage_index) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000134 DCHECK(resolved_field->IsStatic());
135 if (LIKELY(referrer_class != nullptr)) {
136 mirror::Class* fields_class = resolved_field->GetDeclaringClass();
137 if (fields_class == referrer_class) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000138 *storage_index = fields_class->GetDexTypeIndex();
Vladimir Markobe0e5462014-02-26 11:24:15 +0000139 return std::make_pair(true, true);
140 }
141 if (referrer_class->CanAccessResolvedField(fields_class, resolved_field,
142 dex_cache, field_idx)) {
143 // We have the resolved field, we must make it into a index for the referrer
144 // in its static storage (which may fail if it doesn't have a slot for it)
145 // TODO: for images we can elide the static storage base null check
146 // if we know there's a non-null entry in the image
147 const DexFile* dex_file = dex_cache->GetDexFile();
148 uint32_t storage_idx = DexFile::kDexNoIndex;
149 if (LIKELY(fields_class->GetDexCache() == dex_cache)) {
150 // common case where the dex cache of both the referrer and the field are the same,
151 // no need to search the dex file
152 storage_idx = fields_class->GetDexTypeIndex();
153 } else {
154 // Search dex file for localized ssb index, may fail if field's class is a parent
155 // of the class mentioned in the dex file and there is no dex cache entry.
Ian Rogers08f1f502014-12-02 15:04:37 -0800156 std::string temp;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000157 const DexFile::StringId* string_id =
Ian Rogers08f1f502014-12-02 15:04:37 -0800158 dex_file->FindStringId(resolved_field->GetDeclaringClass()->GetDescriptor(&temp));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000159 if (string_id != nullptr) {
160 const DexFile::TypeId* type_id =
161 dex_file->FindTypeId(dex_file->GetIndexForStringId(*string_id));
162 if (type_id != nullptr) {
163 // medium path, needs check of static storage base being initialized
164 storage_idx = dex_file->GetIndexForTypeId(*type_id);
165 }
166 }
167 }
168 if (storage_idx != DexFile::kDexNoIndex) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000169 *storage_index = storage_idx;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000170 return std::make_pair(true, !resolved_field->IsFinal());
171 }
172 }
173 }
174 // Conservative defaults.
Vladimir Markobe0e5462014-02-26 11:24:15 +0000175 *storage_index = DexFile::kDexNoIndex;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000176 return std::make_pair(false, false);
177}
178
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100179inline bool CompilerDriver::IsStaticFieldInReferrerClass(mirror::Class* referrer_class,
180 mirror::ArtField* resolved_field) {
181 DCHECK(resolved_field->IsStatic());
182 mirror::Class* fields_class = resolved_field->GetDeclaringClass();
183 return referrer_class == fields_class;
184}
185
186inline bool CompilerDriver::IsStaticFieldsClassInitialized(mirror::Class* referrer_class,
187 mirror::ArtField* resolved_field) {
188 DCHECK(resolved_field->IsStatic());
189 mirror::Class* fields_class = resolved_field->GetDeclaringClass();
190 return fields_class == referrer_class || fields_class->IsInitialized();
191}
192
Vladimir Markof096aad2014-01-23 15:51:58 +0000193inline mirror::ArtMethod* CompilerDriver::ResolveMethod(
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700194 ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache,
195 Handle<mirror::ClassLoader> class_loader, const DexCompilationUnit* mUnit,
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800196 uint32_t method_idx, InvokeType invoke_type, bool check_incompatible_class_change) {
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700197 DCHECK_EQ(dex_cache->GetDexFile(), mUnit->GetDexFile());
198 DCHECK_EQ(class_loader.Get(), soa.Decode<mirror::ClassLoader*>(mUnit->GetClassLoader()));
Vladimir Markof096aad2014-01-23 15:51:58 +0000199 mirror::ArtMethod* resolved_method = mUnit->GetClassLinker()->ResolveMethod(
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700200 *mUnit->GetDexFile(), method_idx, dex_cache, class_loader, NullHandle<mirror::ArtMethod>(),
201 invoke_type);
Vladimir Markof096aad2014-01-23 15:51:58 +0000202 DCHECK_EQ(resolved_method == nullptr, soa.Self()->IsExceptionPending());
203 if (UNLIKELY(resolved_method == nullptr)) {
204 // Clean up any exception left by type resolution.
205 soa.Self()->ClearException();
206 return nullptr;
207 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800208 if (check_incompatible_class_change &&
209 UNLIKELY(resolved_method->CheckIncompatibleClassChange(invoke_type))) {
Vladimir Markof096aad2014-01-23 15:51:58 +0000210 // Silently return nullptr on incompatible class change.
211 return nullptr;
212 }
213 return resolved_method;
214}
215
216inline void CompilerDriver::GetResolvedMethodDexFileLocation(
217 mirror::ArtMethod* resolved_method, const DexFile** declaring_dex_file,
218 uint16_t* declaring_class_idx, uint16_t* declaring_method_idx) {
219 mirror::Class* declaring_class = resolved_method->GetDeclaringClass();
220 *declaring_dex_file = declaring_class->GetDexCache()->GetDexFile();
221 *declaring_class_idx = declaring_class->GetDexTypeIndex();
222 *declaring_method_idx = resolved_method->GetDexMethodIndex();
223}
224
225inline uint16_t CompilerDriver::GetResolvedMethodVTableIndex(
226 mirror::ArtMethod* resolved_method, InvokeType type) {
227 if (type == kVirtual || type == kSuper) {
228 return resolved_method->GetMethodIndex();
229 } else if (type == kInterface) {
230 return resolved_method->GetDexMethodIndex();
231 } else {
232 return DexFile::kDexNoIndex16;
233 }
234}
235
236inline int CompilerDriver::IsFastInvoke(
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700237 ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache,
238 Handle<mirror::ClassLoader> class_loader, const DexCompilationUnit* mUnit,
Vladimir Markof096aad2014-01-23 15:51:58 +0000239 mirror::Class* referrer_class, mirror::ArtMethod* resolved_method, InvokeType* invoke_type,
240 MethodReference* target_method, const MethodReference* devirt_target,
241 uintptr_t* direct_code, uintptr_t* direct_method) {
242 // Don't try to fast-path if we don't understand the caller's class.
243 if (UNLIKELY(referrer_class == nullptr)) {
244 return 0;
245 }
246 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
247 if (UNLIKELY(!referrer_class->CanAccessResolvedMethod(methods_class, resolved_method,
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700248 dex_cache.Get(),
Vladimir Markof096aad2014-01-23 15:51:58 +0000249 target_method->dex_method_index))) {
250 return 0;
251 }
Vladimir Markof096aad2014-01-23 15:51:58 +0000252 // Sharpen a virtual call into a direct call when the target is known not to have been
253 // overridden (ie is final).
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800254 const bool same_dex_file = target_method->dex_file == mUnit->GetDexFile();
255 bool can_sharpen_virtual_based_on_type = same_dex_file &&
Vladimir Markof096aad2014-01-23 15:51:58 +0000256 (*invoke_type == kVirtual) && (resolved_method->IsFinal() || methods_class->IsFinal());
257 // For invoke-super, ensure the vtable index will be correct to dispatch in the vtable of
258 // the super class.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800259 bool can_sharpen_super_based_on_type = same_dex_file && (*invoke_type == kSuper) &&
Vladimir Markof096aad2014-01-23 15:51:58 +0000260 (referrer_class != methods_class) && referrer_class->IsSubClass(methods_class) &&
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700261 resolved_method->GetMethodIndex() < methods_class->GetVTableLength() &&
Vladimir Marko920506d2014-11-18 14:47:31 +0000262 (methods_class->GetVTableEntry(resolved_method->GetMethodIndex()) == resolved_method) &&
263 !resolved_method->IsAbstract();
Vladimir Markof096aad2014-01-23 15:51:58 +0000264
265 if (can_sharpen_virtual_based_on_type || can_sharpen_super_based_on_type) {
266 // Sharpen a virtual call into a direct call. The method_idx is into referrer's
267 // dex cache, check that this resolved method is where we expect it.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800268 CHECK_EQ(target_method->dex_file, mUnit->GetDexFile());
269 DCHECK_EQ(dex_cache.Get(), mUnit->GetClassLinker()->FindDexCache(*mUnit->GetDexFile()));
270 CHECK_EQ(referrer_class->GetDexCache()->GetResolvedMethod(target_method->dex_method_index),
271 resolved_method) << PrettyMethod(resolved_method);
Vladimir Markof096aad2014-01-23 15:51:58 +0000272 int stats_flags = kFlagMethodResolved;
Igor Murashkind6dee672014-10-16 18:36:16 -0700273 GetCodeAndMethodForDirectCall(/*out*/invoke_type,
274 kDirect, // Sharp type
275 false, // The dex cache is guaranteed to be available
276 referrer_class, resolved_method,
277 /*out*/&stats_flags,
278 target_method,
279 /*out*/direct_code,
280 /*out*/direct_method);
Vladimir Markof096aad2014-01-23 15:51:58 +0000281 DCHECK_NE(*invoke_type, kSuper) << PrettyMethod(resolved_method);
282 if (*invoke_type == kDirect) {
283 stats_flags |= kFlagsMethodResolvedVirtualMadeDirect;
284 }
285 return stats_flags;
286 }
287
288 if ((*invoke_type == kVirtual || *invoke_type == kInterface) && devirt_target != nullptr) {
289 // Post-verification callback recorded a more precise invoke target based on its type info.
290 mirror::ArtMethod* called_method;
291 ClassLinker* class_linker = mUnit->GetClassLinker();
292 if (LIKELY(devirt_target->dex_file == mUnit->GetDexFile())) {
293 called_method = class_linker->ResolveMethod(*devirt_target->dex_file,
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700294 devirt_target->dex_method_index, dex_cache,
295 class_loader, NullHandle<mirror::ArtMethod>(),
296 kVirtual);
Vladimir Markof096aad2014-01-23 15:51:58 +0000297 } else {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700298 StackHandleScope<1> hs(soa.Self());
299 Handle<mirror::DexCache> target_dex_cache(
300 hs.NewHandle(class_linker->FindDexCache(*devirt_target->dex_file)));
Vladimir Markof096aad2014-01-23 15:51:58 +0000301 called_method = class_linker->ResolveMethod(*devirt_target->dex_file,
302 devirt_target->dex_method_index,
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700303 target_dex_cache, class_loader,
304 NullHandle<mirror::ArtMethod>(), kVirtual);
Vladimir Markof096aad2014-01-23 15:51:58 +0000305 }
306 CHECK(called_method != NULL);
307 CHECK(!called_method->IsAbstract());
308 int stats_flags = kFlagMethodResolved;
Igor Murashkind6dee672014-10-16 18:36:16 -0700309 GetCodeAndMethodForDirectCall(/*out*/invoke_type,
310 kDirect, // Sharp type
311 true, // The dex cache may not be available
312 referrer_class, called_method,
313 /*out*/&stats_flags,
314 target_method,
315 /*out*/direct_code,
316 /*out*/direct_method);
Vladimir Markof096aad2014-01-23 15:51:58 +0000317 DCHECK_NE(*invoke_type, kSuper);
318 if (*invoke_type == kDirect) {
319 stats_flags |= kFlagsMethodResolvedPreciseTypeDevirtualization;
320 }
321 return stats_flags;
322 }
323
324 if (UNLIKELY(*invoke_type == kSuper)) {
325 // Unsharpened super calls are suspicious so go slow-path.
326 return 0;
327 }
328
329 // Sharpening failed so generate a regular resolved method dispatch.
330 int stats_flags = kFlagMethodResolved;
Igor Murashkind6dee672014-10-16 18:36:16 -0700331 GetCodeAndMethodForDirectCall(/*out*/invoke_type,
332 *invoke_type, // Sharp type
333 false, // The dex cache is guaranteed to be available
334 referrer_class, resolved_method,
335 /*out*/&stats_flags,
336 target_method,
337 /*out*/direct_code,
338 /*out*/direct_method);
Vladimir Markof096aad2014-01-23 15:51:58 +0000339 return stats_flags;
340}
341
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100342inline bool CompilerDriver::IsMethodsClassInitialized(mirror::Class* referrer_class,
343 mirror::ArtMethod* resolved_method) {
Vladimir Marko9820b7c2014-01-02 16:40:37 +0000344 if (!resolved_method->IsStatic()) {
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100345 return true;
Vladimir Marko9820b7c2014-01-02 16:40:37 +0000346 }
347 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100348 return methods_class == referrer_class || methods_class->IsInitialized();
Vladimir Marko9820b7c2014-01-02 16:40:37 +0000349}
350
Vladimir Markobe0e5462014-02-26 11:24:15 +0000351} // namespace art
352
353#endif // ART_COMPILER_DRIVER_COMPILER_DRIVER_INL_H_