blob: b4730cc059195a2b8a313a1683537536b6cb4aab [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
34inline mirror::DexCache* CompilerDriver::GetDexCache(const DexCompilationUnit* mUnit) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -070035 return mUnit->GetClassLinker()->FindDexCache(Thread::Current(), *mUnit->GetDexFile(), false);
Vladimir Markobe0e5462014-02-26 11:24:15 +000036}
37
Mathieu Chartier736b5602015-09-02 14:54:11 -070038inline mirror::ClassLoader* CompilerDriver::GetClassLoader(const ScopedObjectAccess& soa,
Vladimir Markobe0e5462014-02-26 11:24:15 +000039 const DexCompilationUnit* mUnit) {
Mathieu Chartier0795f232016-09-27 18:43:30 -070040 return soa.Decode<mirror::ClassLoader>(mUnit->GetClassLoader()).Decode();
Vladimir Markobe0e5462014-02-26 11:24:15 +000041}
42
Nicolas Geoffray9437b782015-03-25 10:08:51 +000043inline mirror::Class* CompilerDriver::ResolveClass(
44 const ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache,
45 Handle<mirror::ClassLoader> class_loader, uint16_t cls_index,
46 const DexCompilationUnit* mUnit) {
47 DCHECK_EQ(dex_cache->GetDexFile(), mUnit->GetDexFile());
Mathieu Chartier0795f232016-09-27 18:43:30 -070048 DCHECK_EQ(class_loader.Get(), GetClassLoader(soa, mUnit));
Nicolas Geoffray9437b782015-03-25 10:08:51 +000049 mirror::Class* cls = mUnit->GetClassLinker()->ResolveType(
50 *mUnit->GetDexFile(), cls_index, dex_cache, class_loader);
51 DCHECK_EQ(cls == nullptr, soa.Self()->IsExceptionPending());
52 if (UNLIKELY(cls == nullptr)) {
53 // Clean up any exception left by type resolution.
54 soa.Self()->ClearException();
55 }
56 return cls;
57}
58
Vladimir Markobe0e5462014-02-26 11:24:15 +000059inline mirror::Class* CompilerDriver::ResolveCompilingMethodsClass(
Nicolas Geoffraye5038322014-07-04 09:41:32 +010060 const ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache,
Mathieu Chartier0cd81352014-05-22 16:48:55 -070061 Handle<mirror::ClassLoader> class_loader, const DexCompilationUnit* mUnit) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070062 DCHECK_EQ(dex_cache->GetDexFile(), mUnit->GetDexFile());
Mathieu Chartier0795f232016-09-27 18:43:30 -070063 DCHECK_EQ(class_loader.Get(), GetClassLoader(soa, mUnit));
Vladimir Markobe0e5462014-02-26 11:24:15 +000064 const DexFile::MethodId& referrer_method_id =
65 mUnit->GetDexFile()->GetMethodId(mUnit->GetDexMethodIndex());
Nicolas Geoffray9437b782015-03-25 10:08:51 +000066 return ResolveClass(soa, dex_cache, class_loader, referrer_method_id.class_idx_, mUnit);
Vladimir Markobe0e5462014-02-26 11:24:15 +000067}
68
Mathieu Chartierc7853442015-03-27 14:35:38 -070069inline ArtField* CompilerDriver::ResolveFieldWithDexFile(
Nicolas Geoffraye5038322014-07-04 09:41:32 +010070 const ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache,
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080071 Handle<mirror::ClassLoader> class_loader, const DexFile* dex_file,
Vladimir Markobe0e5462014-02-26 11:24:15 +000072 uint32_t field_idx, bool is_static) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080073 DCHECK_EQ(dex_cache->GetDexFile(), dex_file);
Mathieu Chartierc7853442015-03-27 14:35:38 -070074 ArtField* resolved_field = Runtime::Current()->GetClassLinker()->ResolveField(
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080075 *dex_file, field_idx, dex_cache, class_loader, is_static);
Vladimir Markobe0e5462014-02-26 11:24:15 +000076 DCHECK_EQ(resolved_field == nullptr, soa.Self()->IsExceptionPending());
77 if (UNLIKELY(resolved_field == nullptr)) {
78 // Clean up any exception left by type resolution.
79 soa.Self()->ClearException();
80 return nullptr;
81 }
82 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
83 // ClassLinker can return a field of the wrong kind directly from the DexCache.
Mathieu Chartier2cebb242015-04-21 16:50:40 -070084 // Silently return null on such incompatible class change.
Vladimir Markobe0e5462014-02-26 11:24:15 +000085 return nullptr;
86 }
87 return resolved_field;
88}
89
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080090inline mirror::DexCache* CompilerDriver::FindDexCache(const DexFile* dex_file) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -070091 return Runtime::Current()->GetClassLinker()->FindDexCache(Thread::Current(), *dex_file, false);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080092}
93
Mathieu Chartierc7853442015-03-27 14:35:38 -070094inline ArtField* CompilerDriver::ResolveField(
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080095 const ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache,
96 Handle<mirror::ClassLoader> class_loader, const DexCompilationUnit* mUnit,
97 uint32_t field_idx, bool is_static) {
Mathieu Chartier0795f232016-09-27 18:43:30 -070098 DCHECK_EQ(class_loader.Get(), GetClassLoader(soa, mUnit));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080099 return ResolveFieldWithDexFile(soa, dex_cache, class_loader, mUnit->GetDexFile(), field_idx,
100 is_static);
101}
102
Vladimir Markobe0e5462014-02-26 11:24:15 +0000103inline void CompilerDriver::GetResolvedFieldDexFileLocation(
Mathieu Chartierc7853442015-03-27 14:35:38 -0700104 ArtField* resolved_field, const DexFile** declaring_dex_file,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000105 uint16_t* declaring_class_idx, uint16_t* declaring_field_idx) {
Mathieu Chartier3398c782016-09-30 10:27:43 -0700106 ObjPtr<mirror::Class> declaring_class = resolved_field->GetDeclaringClass();
Vladimir Markobe0e5462014-02-26 11:24:15 +0000107 *declaring_dex_file = declaring_class->GetDexCache()->GetDexFile();
108 *declaring_class_idx = declaring_class->GetDexTypeIndex();
109 *declaring_field_idx = resolved_field->GetDexFieldIndex();
110}
111
Mathieu Chartierc7853442015-03-27 14:35:38 -0700112inline bool CompilerDriver::IsFieldVolatile(ArtField* field) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000113 return field->IsVolatile();
114}
115
Mathieu Chartierc7853442015-03-27 14:35:38 -0700116inline MemberOffset CompilerDriver::GetFieldOffset(ArtField* field) {
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100117 return field->GetOffset();
118}
119
Vladimir Markobe0e5462014-02-26 11:24:15 +0000120inline std::pair<bool, bool> CompilerDriver::IsFastInstanceField(
121 mirror::DexCache* dex_cache, mirror::Class* referrer_class,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700122 ArtField* resolved_field, uint16_t field_idx) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000123 DCHECK(!resolved_field->IsStatic());
Mathieu Chartier3398c782016-09-30 10:27:43 -0700124 ObjPtr<mirror::Class> fields_class = resolved_field->GetDeclaringClass();
Vladimir Markobe0e5462014-02-26 11:24:15 +0000125 bool fast_get = referrer_class != nullptr &&
Mathieu Chartier3398c782016-09-30 10:27:43 -0700126 referrer_class->CanAccessResolvedField(fields_class.Decode(),
127 resolved_field,
128 dex_cache,
129 field_idx);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000130 bool fast_put = fast_get && (!resolved_field->IsFinal() || fields_class == referrer_class);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000131 return std::make_pair(fast_get, fast_put);
132}
133
Roland Levillain4c0eb422015-04-24 16:43:49 +0100134template <typename ArtMember>
135inline bool CompilerDriver::CanAccessResolvedMember(mirror::Class* referrer_class ATTRIBUTE_UNUSED,
136 mirror::Class* access_to ATTRIBUTE_UNUSED,
137 ArtMember* member ATTRIBUTE_UNUSED,
138 mirror::DexCache* dex_cache ATTRIBUTE_UNUSED,
139 uint32_t field_idx ATTRIBUTE_UNUSED) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700140 // Not defined for ArtMember values other than ArtField or ArtMethod.
Roland Levillain4c0eb422015-04-24 16:43:49 +0100141 UNREACHABLE();
142}
143
144template <>
145inline bool CompilerDriver::CanAccessResolvedMember<ArtField>(mirror::Class* referrer_class,
146 mirror::Class* access_to,
147 ArtField* field,
148 mirror::DexCache* dex_cache,
149 uint32_t field_idx) {
150 return referrer_class->CanAccessResolvedField(access_to, field, dex_cache, field_idx);
151}
152
153template <>
Mathieu Chartiere401d142015-04-22 13:56:20 -0700154inline bool CompilerDriver::CanAccessResolvedMember<ArtMethod>(
Roland Levillain4c0eb422015-04-24 16:43:49 +0100155 mirror::Class* referrer_class,
156 mirror::Class* access_to,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700157 ArtMethod* method,
Roland Levillain4c0eb422015-04-24 16:43:49 +0100158 mirror::DexCache* dex_cache,
159 uint32_t field_idx) {
160 return referrer_class->CanAccessResolvedMethod(access_to, method, dex_cache, field_idx);
161}
162
163template <typename ArtMember>
164inline std::pair<bool, bool> CompilerDriver::IsClassOfStaticMemberAvailableToReferrer(
165 mirror::DexCache* dex_cache,
166 mirror::Class* referrer_class,
167 ArtMember* resolved_member,
168 uint16_t member_idx,
169 uint32_t* storage_index) {
170 DCHECK(resolved_member->IsStatic());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000171 if (LIKELY(referrer_class != nullptr)) {
Mathieu Chartier3398c782016-09-30 10:27:43 -0700172 ObjPtr<mirror::Class> members_class = resolved_member->GetDeclaringClass();
Roland Levillain4c0eb422015-04-24 16:43:49 +0100173 if (members_class == referrer_class) {
174 *storage_index = members_class->GetDexTypeIndex();
Vladimir Markobe0e5462014-02-26 11:24:15 +0000175 return std::make_pair(true, true);
176 }
Roland Levillain4c0eb422015-04-24 16:43:49 +0100177 if (CanAccessResolvedMember<ArtMember>(
Mathieu Chartier3398c782016-09-30 10:27:43 -0700178 referrer_class, members_class.Decode(), resolved_member, dex_cache, member_idx)) {
Roland Levillain4c0eb422015-04-24 16:43:49 +0100179 // We have the resolved member, we must make it into a index for the referrer
Vladimir Markobe0e5462014-02-26 11:24:15 +0000180 // in its static storage (which may fail if it doesn't have a slot for it)
181 // TODO: for images we can elide the static storage base null check
182 // if we know there's a non-null entry in the image
183 const DexFile* dex_file = dex_cache->GetDexFile();
184 uint32_t storage_idx = DexFile::kDexNoIndex;
Roland Levillain4c0eb422015-04-24 16:43:49 +0100185 if (LIKELY(members_class->GetDexCache() == dex_cache)) {
186 // common case where the dex cache of both the referrer and the member are the same,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000187 // no need to search the dex file
Roland Levillain4c0eb422015-04-24 16:43:49 +0100188 storage_idx = members_class->GetDexTypeIndex();
Vladimir Markobe0e5462014-02-26 11:24:15 +0000189 } else {
Roland Levillain4c0eb422015-04-24 16:43:49 +0100190 // Search dex file for localized ssb index, may fail if member's class is a parent
Vladimir Markobe0e5462014-02-26 11:24:15 +0000191 // of the class mentioned in the dex file and there is no dex cache entry.
Nicolas Geoffraye4084a52016-02-18 14:43:42 +0000192 storage_idx = resolved_member->GetDeclaringClass()->FindTypeIndexInOtherDexFile(*dex_file);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000193 }
194 if (storage_idx != DexFile::kDexNoIndex) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000195 *storage_index = storage_idx;
Roland Levillain4c0eb422015-04-24 16:43:49 +0100196 return std::make_pair(true, !resolved_member->IsFinal());
Vladimir Markobe0e5462014-02-26 11:24:15 +0000197 }
198 }
199 }
200 // Conservative defaults.
Vladimir Markobe0e5462014-02-26 11:24:15 +0000201 *storage_index = DexFile::kDexNoIndex;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000202 return std::make_pair(false, false);
203}
204
Roland Levillain4c0eb422015-04-24 16:43:49 +0100205inline std::pair<bool, bool> CompilerDriver::IsFastStaticField(
206 mirror::DexCache* dex_cache, mirror::Class* referrer_class,
207 ArtField* resolved_field, uint16_t field_idx, uint32_t* storage_index) {
208 return IsClassOfStaticMemberAvailableToReferrer(
209 dex_cache, referrer_class, resolved_field, field_idx, storage_index);
210}
211
212inline bool CompilerDriver::IsClassOfStaticMethodAvailableToReferrer(
213 mirror::DexCache* dex_cache, mirror::Class* referrer_class,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700214 ArtMethod* resolved_method, uint16_t method_idx, uint32_t* storage_index) {
Roland Levillain4c0eb422015-04-24 16:43:49 +0100215 std::pair<bool, bool> result = IsClassOfStaticMemberAvailableToReferrer(
216 dex_cache, referrer_class, resolved_method, method_idx, storage_index);
217 // Only the first member of `result` is meaningful, as there is no
218 // "write access" to a method.
219 return result.first;
220}
221
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100222inline bool CompilerDriver::IsStaticFieldInReferrerClass(mirror::Class* referrer_class,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700223 ArtField* resolved_field) {
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100224 DCHECK(resolved_field->IsStatic());
Mathieu Chartier3398c782016-09-30 10:27:43 -0700225 ObjPtr<mirror::Class> fields_class = resolved_field->GetDeclaringClass();
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100226 return referrer_class == fields_class;
227}
228
Vladimir Marko07785bb2015-06-15 18:52:54 +0100229inline bool CompilerDriver::CanAssumeClassIsInitialized(mirror::Class* klass) {
230 // Being loaded is a pre-requisite for being initialized but let's do the cheap check first.
231 //
232 // NOTE: When AOT compiling an app, we eagerly initialize app classes (and potentially their
233 // super classes in the boot image) but only those that have a trivial initialization, i.e.
234 // without <clinit>() or static values in the dex file for that class or any of its super
235 // classes. So while we could see the klass as initialized during AOT compilation and have
236 // it only loaded at runtime, the needed initialization would have to be trivial and
237 // unobservable from Java, so we may as well treat it as initialized.
238 if (!klass->IsInitialized()) {
239 return false;
240 }
241 return CanAssumeClassIsLoaded(klass);
242}
243
244inline bool CompilerDriver::CanReferrerAssumeClassIsInitialized(mirror::Class* referrer_class,
245 mirror::Class* klass) {
Nicolas Geoffrayb783b402015-06-22 11:06:43 +0100246 return (referrer_class != nullptr
247 && !referrer_class->IsInterface()
248 && referrer_class->IsSubClass(klass))
249 || CanAssumeClassIsInitialized(klass);
Vladimir Marko07785bb2015-06-15 18:52:54 +0100250}
251
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100252inline bool CompilerDriver::IsStaticFieldsClassInitialized(mirror::Class* referrer_class,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700253 ArtField* resolved_field) {
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100254 DCHECK(resolved_field->IsStatic());
Mathieu Chartier3398c782016-09-30 10:27:43 -0700255 ObjPtr<mirror::Class> fields_class = resolved_field->GetDeclaringClass();
256 return CanReferrerAssumeClassIsInitialized(referrer_class, fields_class.Decode());
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100257}
258
Mathieu Chartiere401d142015-04-22 13:56:20 -0700259inline ArtMethod* CompilerDriver::ResolveMethod(
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700260 ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache,
261 Handle<mirror::ClassLoader> class_loader, const DexCompilationUnit* mUnit,
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800262 uint32_t method_idx, InvokeType invoke_type, bool check_incompatible_class_change) {
Mathieu Chartier0795f232016-09-27 18:43:30 -0700263 DCHECK_EQ(class_loader.Get(), GetClassLoader(soa, mUnit));
Andreas Gampe42ef8ab2015-12-03 17:27:32 -0800264 ArtMethod* resolved_method =
265 check_incompatible_class_change
266 ? mUnit->GetClassLinker()->ResolveMethod<ClassLinker::kForceICCECheck>(
267 *dex_cache->GetDexFile(), method_idx, dex_cache, class_loader, nullptr, invoke_type)
268 : mUnit->GetClassLinker()->ResolveMethod<ClassLinker::kNoICCECheckForCache>(
269 *dex_cache->GetDexFile(), method_idx, dex_cache, class_loader, nullptr, invoke_type);
Vladimir Markof096aad2014-01-23 15:51:58 +0000270 if (UNLIKELY(resolved_method == nullptr)) {
Andreas Gampe42ef8ab2015-12-03 17:27:32 -0800271 DCHECK(soa.Self()->IsExceptionPending());
Vladimir Markof096aad2014-01-23 15:51:58 +0000272 // Clean up any exception left by type resolution.
273 soa.Self()->ClearException();
Vladimir Markof096aad2014-01-23 15:51:58 +0000274 }
275 return resolved_method;
276}
277
278inline void CompilerDriver::GetResolvedMethodDexFileLocation(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700279 ArtMethod* resolved_method, const DexFile** declaring_dex_file,
Vladimir Markof096aad2014-01-23 15:51:58 +0000280 uint16_t* declaring_class_idx, uint16_t* declaring_method_idx) {
281 mirror::Class* declaring_class = resolved_method->GetDeclaringClass();
282 *declaring_dex_file = declaring_class->GetDexCache()->GetDexFile();
283 *declaring_class_idx = declaring_class->GetDexTypeIndex();
284 *declaring_method_idx = resolved_method->GetDexMethodIndex();
285}
286
287inline uint16_t CompilerDriver::GetResolvedMethodVTableIndex(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700288 ArtMethod* resolved_method, InvokeType type) {
Vladimir Markof096aad2014-01-23 15:51:58 +0000289 if (type == kVirtual || type == kSuper) {
290 return resolved_method->GetMethodIndex();
291 } else if (type == kInterface) {
292 return resolved_method->GetDexMethodIndex();
293 } else {
294 return DexFile::kDexNoIndex16;
295 }
296}
297
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100298inline bool CompilerDriver::IsMethodsClassInitialized(mirror::Class* referrer_class,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700299 ArtMethod* resolved_method) {
Vladimir Marko9820b7c2014-01-02 16:40:37 +0000300 if (!resolved_method->IsStatic()) {
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100301 return true;
Vladimir Marko9820b7c2014-01-02 16:40:37 +0000302 }
303 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
Vladimir Marko07785bb2015-06-15 18:52:54 +0100304 return CanReferrerAssumeClassIsInitialized(referrer_class, methods_class);
Vladimir Marko9820b7c2014-01-02 16:40:37 +0000305}
306
Vladimir Markobe0e5462014-02-26 11:24:15 +0000307} // namespace art
308
309#endif // ART_COMPILER_DRIVER_COMPILER_DRIVER_INL_H_