blob: 81d80f4f8f09447acb4fddac6ff766f9d0679c73 [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
Mathieu Chartierc7853442015-03-27 14:35:38 -070022#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070023#include "art_method-inl.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070024#include "base/enums.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070025#include "class_linker-inl.h"
Andreas Gampe53c913b2014-08-12 23:19:23 -070026#include "dex_compilation_unit.h"
Vladimir Markobe0e5462014-02-26 11:24:15 +000027#include "mirror/class_loader.h"
Vladimir Markof096aad2014-01-23 15:51:58 +000028#include "mirror/dex_cache-inl.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070029#include "scoped_thread_state_change-inl.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070030#include "handle_scope-inl.h"
Vladimir Markobe0e5462014-02-26 11:24:15 +000031
32namespace art {
33
Nicolas Geoffray9437b782015-03-25 10:08:51 +000034inline mirror::Class* CompilerDriver::ResolveClass(
35 const ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache,
Andreas Gampea5b09a62016-11-17 15:21:22 -080036 Handle<mirror::ClassLoader> class_loader, dex::TypeIndex cls_index,
Nicolas Geoffray9437b782015-03-25 10:08:51 +000037 const DexCompilationUnit* mUnit) {
38 DCHECK_EQ(dex_cache->GetDexFile(), mUnit->GetDexFile());
Vladimir Markoec786222016-12-20 16:24:13 +000039 DCHECK_EQ(class_loader.Get(), mUnit->GetClassLoader().Get());
Nicolas Geoffray9437b782015-03-25 10:08:51 +000040 mirror::Class* cls = mUnit->GetClassLinker()->ResolveType(
41 *mUnit->GetDexFile(), cls_index, dex_cache, class_loader);
42 DCHECK_EQ(cls == nullptr, soa.Self()->IsExceptionPending());
43 if (UNLIKELY(cls == nullptr)) {
44 // Clean up any exception left by type resolution.
45 soa.Self()->ClearException();
46 }
47 return cls;
48}
49
Vladimir Markobe0e5462014-02-26 11:24:15 +000050inline mirror::Class* CompilerDriver::ResolveCompilingMethodsClass(
Nicolas Geoffraye5038322014-07-04 09:41:32 +010051 const ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache,
Mathieu Chartier0cd81352014-05-22 16:48:55 -070052 Handle<mirror::ClassLoader> class_loader, const DexCompilationUnit* mUnit) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070053 DCHECK_EQ(dex_cache->GetDexFile(), mUnit->GetDexFile());
Vladimir Markoec786222016-12-20 16:24:13 +000054 DCHECK_EQ(class_loader.Get(), mUnit->GetClassLoader().Get());
Vladimir Markobe0e5462014-02-26 11:24:15 +000055 const DexFile::MethodId& referrer_method_id =
56 mUnit->GetDexFile()->GetMethodId(mUnit->GetDexMethodIndex());
Nicolas Geoffray9437b782015-03-25 10:08:51 +000057 return ResolveClass(soa, dex_cache, class_loader, referrer_method_id.class_idx_, mUnit);
Vladimir Markobe0e5462014-02-26 11:24:15 +000058}
59
Mathieu Chartierc7853442015-03-27 14:35:38 -070060inline ArtField* CompilerDriver::ResolveFieldWithDexFile(
Nicolas Geoffraye5038322014-07-04 09:41:32 +010061 const ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache,
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080062 Handle<mirror::ClassLoader> class_loader, const DexFile* dex_file,
Vladimir Markobe0e5462014-02-26 11:24:15 +000063 uint32_t field_idx, bool is_static) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080064 DCHECK_EQ(dex_cache->GetDexFile(), dex_file);
Mathieu Chartierc7853442015-03-27 14:35:38 -070065 ArtField* resolved_field = Runtime::Current()->GetClassLinker()->ResolveField(
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080066 *dex_file, 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.
Mathieu Chartier2cebb242015-04-21 16:50:40 -070075 // Silently return null on such incompatible class change.
Vladimir Markobe0e5462014-02-26 11:24:15 +000076 return nullptr;
77 }
78 return resolved_field;
79}
80
Mathieu Chartierc7853442015-03-27 14:35:38 -070081inline ArtField* CompilerDriver::ResolveField(
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080082 const ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache,
83 Handle<mirror::ClassLoader> class_loader, const DexCompilationUnit* mUnit,
84 uint32_t field_idx, bool is_static) {
Vladimir Markoec786222016-12-20 16:24:13 +000085 DCHECK_EQ(class_loader.Get(), mUnit->GetClassLoader().Get());
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080086 return ResolveFieldWithDexFile(soa, dex_cache, class_loader, mUnit->GetDexFile(), field_idx,
87 is_static);
88}
89
Vladimir Markobe0e5462014-02-26 11:24:15 +000090inline std::pair<bool, bool> CompilerDriver::IsFastInstanceField(
91 mirror::DexCache* dex_cache, mirror::Class* referrer_class,
Mathieu Chartierc7853442015-03-27 14:35:38 -070092 ArtField* resolved_field, uint16_t field_idx) {
Vladimir Markobe0e5462014-02-26 11:24:15 +000093 DCHECK(!resolved_field->IsStatic());
Mathieu Chartier3398c782016-09-30 10:27:43 -070094 ObjPtr<mirror::Class> fields_class = resolved_field->GetDeclaringClass();
Vladimir Markobe0e5462014-02-26 11:24:15 +000095 bool fast_get = referrer_class != nullptr &&
Mathieu Chartier1a5337f2016-10-13 13:48:23 -070096 referrer_class->CanAccessResolvedField(fields_class,
Mathieu Chartier3398c782016-09-30 10:27:43 -070097 resolved_field,
98 dex_cache,
99 field_idx);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000100 bool fast_put = fast_get && (!resolved_field->IsFinal() || fields_class == referrer_class);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000101 return std::make_pair(fast_get, fast_put);
102}
103
Roland Levillain4c0eb422015-04-24 16:43:49 +0100104template <typename ArtMember>
105inline bool CompilerDriver::CanAccessResolvedMember(mirror::Class* referrer_class ATTRIBUTE_UNUSED,
106 mirror::Class* access_to ATTRIBUTE_UNUSED,
107 ArtMember* member ATTRIBUTE_UNUSED,
108 mirror::DexCache* dex_cache ATTRIBUTE_UNUSED,
109 uint32_t field_idx ATTRIBUTE_UNUSED) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700110 // Not defined for ArtMember values other than ArtField or ArtMethod.
Roland Levillain4c0eb422015-04-24 16:43:49 +0100111 UNREACHABLE();
112}
113
114template <>
115inline bool CompilerDriver::CanAccessResolvedMember<ArtField>(mirror::Class* referrer_class,
116 mirror::Class* access_to,
117 ArtField* field,
118 mirror::DexCache* dex_cache,
119 uint32_t field_idx) {
120 return referrer_class->CanAccessResolvedField(access_to, field, dex_cache, field_idx);
121}
122
123template <>
Mathieu Chartiere401d142015-04-22 13:56:20 -0700124inline bool CompilerDriver::CanAccessResolvedMember<ArtMethod>(
Roland Levillain4c0eb422015-04-24 16:43:49 +0100125 mirror::Class* referrer_class,
126 mirror::Class* access_to,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700127 ArtMethod* method,
Roland Levillain4c0eb422015-04-24 16:43:49 +0100128 mirror::DexCache* dex_cache,
129 uint32_t field_idx) {
130 return referrer_class->CanAccessResolvedMethod(access_to, method, dex_cache, field_idx);
131}
132
133template <typename ArtMember>
134inline std::pair<bool, bool> CompilerDriver::IsClassOfStaticMemberAvailableToReferrer(
135 mirror::DexCache* dex_cache,
136 mirror::Class* referrer_class,
137 ArtMember* resolved_member,
138 uint16_t member_idx,
Andreas Gampea5b09a62016-11-17 15:21:22 -0800139 dex::TypeIndex* storage_index) {
Roland Levillain4c0eb422015-04-24 16:43:49 +0100140 DCHECK(resolved_member->IsStatic());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000141 if (LIKELY(referrer_class != nullptr)) {
Mathieu Chartier3398c782016-09-30 10:27:43 -0700142 ObjPtr<mirror::Class> members_class = resolved_member->GetDeclaringClass();
Roland Levillain4c0eb422015-04-24 16:43:49 +0100143 if (members_class == referrer_class) {
144 *storage_index = members_class->GetDexTypeIndex();
Vladimir Markobe0e5462014-02-26 11:24:15 +0000145 return std::make_pair(true, true);
146 }
Roland Levillain4c0eb422015-04-24 16:43:49 +0100147 if (CanAccessResolvedMember<ArtMember>(
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700148 referrer_class, members_class.Ptr(), resolved_member, dex_cache, member_idx)) {
Roland Levillain4c0eb422015-04-24 16:43:49 +0100149 // We have the resolved member, we must make it into a index for the referrer
Vladimir Markobe0e5462014-02-26 11:24:15 +0000150 // in its static storage (which may fail if it doesn't have a slot for it)
151 // TODO: for images we can elide the static storage base null check
152 // if we know there's a non-null entry in the image
153 const DexFile* dex_file = dex_cache->GetDexFile();
Andreas Gampea5b09a62016-11-17 15:21:22 -0800154 dex::TypeIndex storage_idx(DexFile::kDexNoIndex16);
Roland Levillain4c0eb422015-04-24 16:43:49 +0100155 if (LIKELY(members_class->GetDexCache() == dex_cache)) {
156 // common case where the dex cache of both the referrer and the member are the same,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000157 // no need to search the dex file
Roland Levillain4c0eb422015-04-24 16:43:49 +0100158 storage_idx = members_class->GetDexTypeIndex();
Vladimir Markobe0e5462014-02-26 11:24:15 +0000159 } else {
Roland Levillain4c0eb422015-04-24 16:43:49 +0100160 // Search dex file for localized ssb index, may fail if member's class is a parent
Vladimir Markobe0e5462014-02-26 11:24:15 +0000161 // of the class mentioned in the dex file and there is no dex cache entry.
Nicolas Geoffraye4084a52016-02-18 14:43:42 +0000162 storage_idx = resolved_member->GetDeclaringClass()->FindTypeIndexInOtherDexFile(*dex_file);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000163 }
Andreas Gampea5b09a62016-11-17 15:21:22 -0800164 if (storage_idx.IsValid()) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000165 *storage_index = storage_idx;
Roland Levillain4c0eb422015-04-24 16:43:49 +0100166 return std::make_pair(true, !resolved_member->IsFinal());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000167 }
168 }
169 }
170 // Conservative defaults.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800171 *storage_index = dex::TypeIndex(DexFile::kDexNoIndex16);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000172 return std::make_pair(false, false);
173}
174
Roland Levillain4c0eb422015-04-24 16:43:49 +0100175inline std::pair<bool, bool> CompilerDriver::IsFastStaticField(
176 mirror::DexCache* dex_cache, mirror::Class* referrer_class,
Andreas Gampea5b09a62016-11-17 15:21:22 -0800177 ArtField* resolved_field, uint16_t field_idx, dex::TypeIndex* storage_index) {
Roland Levillain4c0eb422015-04-24 16:43:49 +0100178 return IsClassOfStaticMemberAvailableToReferrer(
179 dex_cache, referrer_class, resolved_field, field_idx, storage_index);
180}
181
182inline bool CompilerDriver::IsClassOfStaticMethodAvailableToReferrer(
183 mirror::DexCache* dex_cache, mirror::Class* referrer_class,
Andreas Gampea5b09a62016-11-17 15:21:22 -0800184 ArtMethod* resolved_method, uint16_t method_idx, dex::TypeIndex* storage_index) {
Roland Levillain4c0eb422015-04-24 16:43:49 +0100185 std::pair<bool, bool> result = IsClassOfStaticMemberAvailableToReferrer(
186 dex_cache, referrer_class, resolved_method, method_idx, storage_index);
187 // Only the first member of `result` is meaningful, as there is no
188 // "write access" to a method.
189 return result.first;
190}
191
Mathieu Chartiere401d142015-04-22 13:56:20 -0700192inline ArtMethod* CompilerDriver::ResolveMethod(
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700193 ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache,
194 Handle<mirror::ClassLoader> class_loader, const DexCompilationUnit* mUnit,
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800195 uint32_t method_idx, InvokeType invoke_type, bool check_incompatible_class_change) {
Vladimir Markoec786222016-12-20 16:24:13 +0000196 DCHECK_EQ(class_loader.Get(), mUnit->GetClassLoader().Get());
Andreas Gampe42ef8ab2015-12-03 17:27:32 -0800197 ArtMethod* resolved_method =
198 check_incompatible_class_change
199 ? mUnit->GetClassLinker()->ResolveMethod<ClassLinker::kForceICCECheck>(
200 *dex_cache->GetDexFile(), method_idx, dex_cache, class_loader, nullptr, invoke_type)
201 : mUnit->GetClassLinker()->ResolveMethod<ClassLinker::kNoICCECheckForCache>(
202 *dex_cache->GetDexFile(), method_idx, dex_cache, class_loader, nullptr, invoke_type);
Vladimir Markof096aad2014-01-23 15:51:58 +0000203 if (UNLIKELY(resolved_method == nullptr)) {
Andreas Gampe42ef8ab2015-12-03 17:27:32 -0800204 DCHECK(soa.Self()->IsExceptionPending());
Vladimir Markof096aad2014-01-23 15:51:58 +0000205 // Clean up any exception left by type resolution.
206 soa.Self()->ClearException();
Vladimir Markof096aad2014-01-23 15:51:58 +0000207 }
208 return resolved_method;
209}
210
Vladimir Markobe0e5462014-02-26 11:24:15 +0000211} // namespace art
212
213#endif // ART_COMPILER_DRIVER_COMPILER_DRIVER_INL_H_