Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 1 | /* |
| 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 Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 21 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 22 | #include "art_field-inl.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 23 | #include "art_method-inl.h" |
| 24 | #include "class_linker-inl.h" |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 25 | #include "dex_compilation_unit.h" |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 26 | #include "mirror/class_loader.h" |
Vladimir Marko | f096aad | 2014-01-23 15:51:58 +0000 | [diff] [blame] | 27 | #include "mirror/dex_cache-inl.h" |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 28 | #include "scoped_thread_state_change.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 29 | #include "handle_scope-inl.h" |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 30 | |
| 31 | namespace art { |
| 32 | |
| 33 | inline mirror::DexCache* CompilerDriver::GetDexCache(const DexCompilationUnit* mUnit) { |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 34 | return mUnit->GetClassLinker()->FindDexCache(Thread::Current(), *mUnit->GetDexFile(), false); |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 35 | } |
| 36 | |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 37 | inline mirror::ClassLoader* CompilerDriver::GetClassLoader(const ScopedObjectAccess& soa, |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 38 | const DexCompilationUnit* mUnit) { |
| 39 | return soa.Decode<mirror::ClassLoader*>(mUnit->GetClassLoader()); |
| 40 | } |
| 41 | |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 42 | inline 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 Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 58 | inline mirror::Class* CompilerDriver::ResolveCompilingMethodsClass( |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 59 | const ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache, |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 60 | Handle<mirror::ClassLoader> class_loader, const DexCompilationUnit* mUnit) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 61 | DCHECK_EQ(dex_cache->GetDexFile(), mUnit->GetDexFile()); |
| 62 | DCHECK_EQ(class_loader.Get(), soa.Decode<mirror::ClassLoader*>(mUnit->GetClassLoader())); |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 63 | const DexFile::MethodId& referrer_method_id = |
| 64 | mUnit->GetDexFile()->GetMethodId(mUnit->GetDexMethodIndex()); |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 65 | return ResolveClass(soa, dex_cache, class_loader, referrer_method_id.class_idx_, mUnit); |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 68 | inline ArtField* CompilerDriver::ResolveFieldWithDexFile( |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 69 | const ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache, |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 70 | Handle<mirror::ClassLoader> class_loader, const DexFile* dex_file, |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 71 | uint32_t field_idx, bool is_static) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 72 | DCHECK_EQ(dex_cache->GetDexFile(), dex_file); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 73 | ArtField* resolved_field = Runtime::Current()->GetClassLinker()->ResolveField( |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 74 | *dex_file, field_idx, dex_cache, class_loader, is_static); |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 75 | 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. |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 83 | // Silently return null on such incompatible class change. |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 84 | return nullptr; |
| 85 | } |
| 86 | return resolved_field; |
| 87 | } |
| 88 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 89 | inline mirror::DexCache* CompilerDriver::FindDexCache(const DexFile* dex_file) { |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 90 | return Runtime::Current()->GetClassLinker()->FindDexCache(Thread::Current(), *dex_file, false); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 91 | } |
| 92 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 93 | inline ArtField* CompilerDriver::ResolveField( |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 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 Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 102 | inline void CompilerDriver::GetResolvedFieldDexFileLocation( |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 103 | ArtField* resolved_field, const DexFile** declaring_dex_file, |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 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 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 111 | inline bool CompilerDriver::IsFieldVolatile(ArtField* field) { |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 112 | return field->IsVolatile(); |
| 113 | } |
| 114 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 115 | inline MemberOffset CompilerDriver::GetFieldOffset(ArtField* field) { |
Vladimir Marko | 66c6d7b | 2014-10-16 15:41:48 +0100 | [diff] [blame] | 116 | return field->GetOffset(); |
| 117 | } |
| 118 | |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 119 | inline std::pair<bool, bool> CompilerDriver::IsFastInstanceField( |
| 120 | mirror::DexCache* dex_cache, mirror::Class* referrer_class, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 121 | ArtField* resolved_field, uint16_t field_idx) { |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 122 | 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 Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 128 | return std::make_pair(fast_get, fast_put); |
| 129 | } |
| 130 | |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 131 | template <typename ArtMember> |
| 132 | inline bool CompilerDriver::CanAccessResolvedMember(mirror::Class* referrer_class ATTRIBUTE_UNUSED, |
| 133 | mirror::Class* access_to ATTRIBUTE_UNUSED, |
| 134 | ArtMember* member ATTRIBUTE_UNUSED, |
| 135 | mirror::DexCache* dex_cache ATTRIBUTE_UNUSED, |
| 136 | uint32_t field_idx ATTRIBUTE_UNUSED) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 137 | // Not defined for ArtMember values other than ArtField or ArtMethod. |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 138 | UNREACHABLE(); |
| 139 | } |
| 140 | |
| 141 | template <> |
| 142 | inline bool CompilerDriver::CanAccessResolvedMember<ArtField>(mirror::Class* referrer_class, |
| 143 | mirror::Class* access_to, |
| 144 | ArtField* field, |
| 145 | mirror::DexCache* dex_cache, |
| 146 | uint32_t field_idx) { |
| 147 | return referrer_class->CanAccessResolvedField(access_to, field, dex_cache, field_idx); |
| 148 | } |
| 149 | |
| 150 | template <> |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 151 | inline bool CompilerDriver::CanAccessResolvedMember<ArtMethod>( |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 152 | mirror::Class* referrer_class, |
| 153 | mirror::Class* access_to, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 154 | ArtMethod* method, |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 155 | mirror::DexCache* dex_cache, |
| 156 | uint32_t field_idx) { |
| 157 | return referrer_class->CanAccessResolvedMethod(access_to, method, dex_cache, field_idx); |
| 158 | } |
| 159 | |
| 160 | template <typename ArtMember> |
| 161 | inline std::pair<bool, bool> CompilerDriver::IsClassOfStaticMemberAvailableToReferrer( |
| 162 | mirror::DexCache* dex_cache, |
| 163 | mirror::Class* referrer_class, |
| 164 | ArtMember* resolved_member, |
| 165 | uint16_t member_idx, |
| 166 | uint32_t* storage_index) { |
| 167 | DCHECK(resolved_member->IsStatic()); |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 168 | if (LIKELY(referrer_class != nullptr)) { |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 169 | mirror::Class* members_class = resolved_member->GetDeclaringClass(); |
| 170 | if (members_class == referrer_class) { |
| 171 | *storage_index = members_class->GetDexTypeIndex(); |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 172 | return std::make_pair(true, true); |
| 173 | } |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 174 | if (CanAccessResolvedMember<ArtMember>( |
| 175 | referrer_class, members_class, resolved_member, dex_cache, member_idx)) { |
| 176 | // We have the resolved member, we must make it into a index for the referrer |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 177 | // in its static storage (which may fail if it doesn't have a slot for it) |
| 178 | // TODO: for images we can elide the static storage base null check |
| 179 | // if we know there's a non-null entry in the image |
| 180 | const DexFile* dex_file = dex_cache->GetDexFile(); |
| 181 | uint32_t storage_idx = DexFile::kDexNoIndex; |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 182 | if (LIKELY(members_class->GetDexCache() == dex_cache)) { |
| 183 | // common case where the dex cache of both the referrer and the member are the same, |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 184 | // no need to search the dex file |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 185 | storage_idx = members_class->GetDexTypeIndex(); |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 186 | } else { |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 187 | // Search dex file for localized ssb index, may fail if member's class is a parent |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 188 | // of the class mentioned in the dex file and there is no dex cache entry. |
Ian Rogers | 08f1f50 | 2014-12-02 15:04:37 -0800 | [diff] [blame] | 189 | std::string temp; |
Mathieu Chartier | 9507fa2 | 2015-10-29 15:08:57 -0700 | [diff] [blame] | 190 | const DexFile::TypeId* type_id = |
| 191 | dex_file->FindTypeId(resolved_member->GetDeclaringClass()->GetDescriptor(&temp)); |
| 192 | if (type_id != nullptr) { |
| 193 | // medium path, needs check of static storage base being initialized |
| 194 | storage_idx = dex_file->GetIndexForTypeId(*type_id); |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 195 | } |
| 196 | } |
| 197 | if (storage_idx != DexFile::kDexNoIndex) { |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 198 | *storage_index = storage_idx; |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 199 | return std::make_pair(true, !resolved_member->IsFinal()); |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | } |
| 203 | // Conservative defaults. |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 204 | *storage_index = DexFile::kDexNoIndex; |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 205 | return std::make_pair(false, false); |
| 206 | } |
| 207 | |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 208 | inline std::pair<bool, bool> CompilerDriver::IsFastStaticField( |
| 209 | mirror::DexCache* dex_cache, mirror::Class* referrer_class, |
| 210 | ArtField* resolved_field, uint16_t field_idx, uint32_t* storage_index) { |
| 211 | return IsClassOfStaticMemberAvailableToReferrer( |
| 212 | dex_cache, referrer_class, resolved_field, field_idx, storage_index); |
| 213 | } |
| 214 | |
| 215 | inline bool CompilerDriver::IsClassOfStaticMethodAvailableToReferrer( |
| 216 | mirror::DexCache* dex_cache, mirror::Class* referrer_class, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 217 | ArtMethod* resolved_method, uint16_t method_idx, uint32_t* storage_index) { |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 218 | std::pair<bool, bool> result = IsClassOfStaticMemberAvailableToReferrer( |
| 219 | dex_cache, referrer_class, resolved_method, method_idx, storage_index); |
| 220 | // Only the first member of `result` is meaningful, as there is no |
| 221 | // "write access" to a method. |
| 222 | return result.first; |
| 223 | } |
| 224 | |
Vladimir Marko | 66c6d7b | 2014-10-16 15:41:48 +0100 | [diff] [blame] | 225 | inline bool CompilerDriver::IsStaticFieldInReferrerClass(mirror::Class* referrer_class, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 226 | ArtField* resolved_field) { |
Vladimir Marko | 66c6d7b | 2014-10-16 15:41:48 +0100 | [diff] [blame] | 227 | DCHECK(resolved_field->IsStatic()); |
| 228 | mirror::Class* fields_class = resolved_field->GetDeclaringClass(); |
| 229 | return referrer_class == fields_class; |
| 230 | } |
| 231 | |
Vladimir Marko | 07785bb | 2015-06-15 18:52:54 +0100 | [diff] [blame] | 232 | inline bool CompilerDriver::CanAssumeClassIsInitialized(mirror::Class* klass) { |
| 233 | // Being loaded is a pre-requisite for being initialized but let's do the cheap check first. |
| 234 | // |
| 235 | // NOTE: When AOT compiling an app, we eagerly initialize app classes (and potentially their |
| 236 | // super classes in the boot image) but only those that have a trivial initialization, i.e. |
| 237 | // without <clinit>() or static values in the dex file for that class or any of its super |
| 238 | // classes. So while we could see the klass as initialized during AOT compilation and have |
| 239 | // it only loaded at runtime, the needed initialization would have to be trivial and |
| 240 | // unobservable from Java, so we may as well treat it as initialized. |
| 241 | if (!klass->IsInitialized()) { |
| 242 | return false; |
| 243 | } |
| 244 | return CanAssumeClassIsLoaded(klass); |
| 245 | } |
| 246 | |
| 247 | inline bool CompilerDriver::CanReferrerAssumeClassIsInitialized(mirror::Class* referrer_class, |
| 248 | mirror::Class* klass) { |
Nicolas Geoffray | b783b40 | 2015-06-22 11:06:43 +0100 | [diff] [blame] | 249 | return (referrer_class != nullptr |
| 250 | && !referrer_class->IsInterface() |
| 251 | && referrer_class->IsSubClass(klass)) |
| 252 | || CanAssumeClassIsInitialized(klass); |
Vladimir Marko | 07785bb | 2015-06-15 18:52:54 +0100 | [diff] [blame] | 253 | } |
| 254 | |
Vladimir Marko | 66c6d7b | 2014-10-16 15:41:48 +0100 | [diff] [blame] | 255 | inline bool CompilerDriver::IsStaticFieldsClassInitialized(mirror::Class* referrer_class, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 256 | ArtField* resolved_field) { |
Vladimir Marko | 66c6d7b | 2014-10-16 15:41:48 +0100 | [diff] [blame] | 257 | DCHECK(resolved_field->IsStatic()); |
| 258 | mirror::Class* fields_class = resolved_field->GetDeclaringClass(); |
Vladimir Marko | 07785bb | 2015-06-15 18:52:54 +0100 | [diff] [blame] | 259 | return CanReferrerAssumeClassIsInitialized(referrer_class, fields_class); |
Vladimir Marko | 66c6d7b | 2014-10-16 15:41:48 +0100 | [diff] [blame] | 260 | } |
| 261 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 262 | inline ArtMethod* CompilerDriver::ResolveMethod( |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 263 | ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache, |
| 264 | Handle<mirror::ClassLoader> class_loader, const DexCompilationUnit* mUnit, |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 265 | uint32_t method_idx, InvokeType invoke_type, bool check_incompatible_class_change) { |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 266 | DCHECK_EQ(class_loader.Get(), soa.Decode<mirror::ClassLoader*>(mUnit->GetClassLoader())); |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 267 | ArtMethod* resolved_method = |
| 268 | check_incompatible_class_change |
| 269 | ? mUnit->GetClassLinker()->ResolveMethod<ClassLinker::kForceICCECheck>( |
| 270 | *dex_cache->GetDexFile(), method_idx, dex_cache, class_loader, nullptr, invoke_type) |
| 271 | : mUnit->GetClassLinker()->ResolveMethod<ClassLinker::kNoICCECheckForCache>( |
| 272 | *dex_cache->GetDexFile(), method_idx, dex_cache, class_loader, nullptr, invoke_type); |
Vladimir Marko | f096aad | 2014-01-23 15:51:58 +0000 | [diff] [blame] | 273 | if (UNLIKELY(resolved_method == nullptr)) { |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 274 | DCHECK(soa.Self()->IsExceptionPending()); |
Vladimir Marko | f096aad | 2014-01-23 15:51:58 +0000 | [diff] [blame] | 275 | // Clean up any exception left by type resolution. |
| 276 | soa.Self()->ClearException(); |
Vladimir Marko | f096aad | 2014-01-23 15:51:58 +0000 | [diff] [blame] | 277 | } |
| 278 | return resolved_method; |
| 279 | } |
| 280 | |
| 281 | inline void CompilerDriver::GetResolvedMethodDexFileLocation( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 282 | ArtMethod* resolved_method, const DexFile** declaring_dex_file, |
Vladimir Marko | f096aad | 2014-01-23 15:51:58 +0000 | [diff] [blame] | 283 | uint16_t* declaring_class_idx, uint16_t* declaring_method_idx) { |
| 284 | mirror::Class* declaring_class = resolved_method->GetDeclaringClass(); |
| 285 | *declaring_dex_file = declaring_class->GetDexCache()->GetDexFile(); |
| 286 | *declaring_class_idx = declaring_class->GetDexTypeIndex(); |
| 287 | *declaring_method_idx = resolved_method->GetDexMethodIndex(); |
| 288 | } |
| 289 | |
| 290 | inline uint16_t CompilerDriver::GetResolvedMethodVTableIndex( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 291 | ArtMethod* resolved_method, InvokeType type) { |
Vladimir Marko | f096aad | 2014-01-23 15:51:58 +0000 | [diff] [blame] | 292 | if (type == kVirtual || type == kSuper) { |
| 293 | return resolved_method->GetMethodIndex(); |
| 294 | } else if (type == kInterface) { |
| 295 | return resolved_method->GetDexMethodIndex(); |
| 296 | } else { |
| 297 | return DexFile::kDexNoIndex16; |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | inline int CompilerDriver::IsFastInvoke( |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 302 | ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache, |
| 303 | Handle<mirror::ClassLoader> class_loader, const DexCompilationUnit* mUnit, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 304 | mirror::Class* referrer_class, ArtMethod* resolved_method, InvokeType* invoke_type, |
Vladimir Marko | f096aad | 2014-01-23 15:51:58 +0000 | [diff] [blame] | 305 | MethodReference* target_method, const MethodReference* devirt_target, |
| 306 | uintptr_t* direct_code, uintptr_t* direct_method) { |
| 307 | // Don't try to fast-path if we don't understand the caller's class. |
Alex Light | fedd91d | 2016-01-07 14:49:16 -0800 | [diff] [blame^] | 308 | // Referrer_class is the class that this invoke is contained in. |
Vladimir Marko | f096aad | 2014-01-23 15:51:58 +0000 | [diff] [blame] | 309 | if (UNLIKELY(referrer_class == nullptr)) { |
| 310 | return 0; |
| 311 | } |
Alex Light | fedd91d | 2016-01-07 14:49:16 -0800 | [diff] [blame^] | 312 | StackHandleScope<2> hs(soa.Self()); |
| 313 | // Methods_class is the class refered to by the class_idx field of the methodId the method_idx is |
| 314 | // pointing to. |
| 315 | // For example in |
| 316 | // .class LABC; |
| 317 | // .super LDEF; |
| 318 | // .method hi()V |
| 319 | // ... |
| 320 | // invoke-super {p0}, LDEF;->hi()V |
| 321 | // ... |
| 322 | // .end method |
| 323 | // the referrer_class is 'ABC' and the methods_class is DEF. Note that the methods class is 'DEF' |
| 324 | // even if 'DEF' inherits the method from it's superclass. |
| 325 | Handle<mirror::Class> methods_class(hs.NewHandle(mUnit->GetClassLinker()->ResolveType( |
| 326 | *target_method->dex_file, |
| 327 | target_method->dex_file->GetMethodId(target_method->dex_method_index).class_idx_, |
| 328 | dex_cache, |
| 329 | class_loader))); |
| 330 | DCHECK(methods_class.Get() != nullptr); |
| 331 | mirror::Class* methods_declaring_class = resolved_method->GetDeclaringClass(); |
| 332 | if (UNLIKELY(!referrer_class->CanAccessResolvedMethod(methods_declaring_class, resolved_method, |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 333 | dex_cache.Get(), |
Vladimir Marko | f096aad | 2014-01-23 15:51:58 +0000 | [diff] [blame] | 334 | target_method->dex_method_index))) { |
| 335 | return 0; |
| 336 | } |
Vladimir Marko | f096aad | 2014-01-23 15:51:58 +0000 | [diff] [blame] | 337 | // Sharpen a virtual call into a direct call when the target is known not to have been |
| 338 | // overridden (ie is final). |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 339 | const bool same_dex_file = target_method->dex_file == mUnit->GetDexFile(); |
| 340 | bool can_sharpen_virtual_based_on_type = same_dex_file && |
Alex Light | fedd91d | 2016-01-07 14:49:16 -0800 | [diff] [blame^] | 341 | (*invoke_type == kVirtual) && (resolved_method->IsFinal() || |
| 342 | methods_declaring_class->IsFinal()); |
Vladimir Marko | f096aad | 2014-01-23 15:51:58 +0000 | [diff] [blame] | 343 | // For invoke-super, ensure the vtable index will be correct to dispatch in the vtable of |
| 344 | // the super class. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 345 | const size_t pointer_size = InstructionSetPointerSize(GetInstructionSet()); |
Alex Light | fedd91d | 2016-01-07 14:49:16 -0800 | [diff] [blame^] | 346 | // TODO We should be able to sharpen if we are going into the boot image as well. |
| 347 | bool can_sharpen_super_based_on_type = same_dex_file && |
| 348 | (*invoke_type == kSuper) && |
| 349 | !methods_class->IsInterface() && |
| 350 | (referrer_class != methods_declaring_class) && |
| 351 | referrer_class->IsSubClass(methods_declaring_class) && |
| 352 | resolved_method->GetMethodIndex() < methods_declaring_class->GetVTableLength() && |
| 353 | (methods_declaring_class->GetVTableEntry( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 354 | resolved_method->GetMethodIndex(), pointer_size) == resolved_method) && |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 355 | resolved_method->IsInvokable(); |
Alex Light | fedd91d | 2016-01-07 14:49:16 -0800 | [diff] [blame^] | 356 | // TODO We should be able to sharpen if we are going into the boot image as well. |
| 357 | bool can_sharpen_interface_super_based_on_type = same_dex_file && |
| 358 | (*invoke_type == kSuper) && |
| 359 | methods_class->IsInterface() && |
| 360 | methods_class->IsAssignableFrom(referrer_class) && |
| 361 | resolved_method->IsInvokable(); |
Vladimir Marko | f096aad | 2014-01-23 15:51:58 +0000 | [diff] [blame] | 362 | |
Alex Light | fedd91d | 2016-01-07 14:49:16 -0800 | [diff] [blame^] | 363 | if (can_sharpen_virtual_based_on_type || |
| 364 | can_sharpen_super_based_on_type || |
| 365 | can_sharpen_interface_super_based_on_type) { |
Vladimir Marko | f096aad | 2014-01-23 15:51:58 +0000 | [diff] [blame] | 366 | // Sharpen a virtual call into a direct call. The method_idx is into referrer's |
| 367 | // dex cache, check that this resolved method is where we expect it. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 368 | CHECK_EQ(target_method->dex_file, mUnit->GetDexFile()); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 369 | DCHECK_EQ(dex_cache.Get(), mUnit->GetClassLinker()->FindDexCache( |
| 370 | soa.Self(), *mUnit->GetDexFile(), false)); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 371 | CHECK_EQ(referrer_class->GetDexCache()->GetResolvedMethod( |
| 372 | target_method->dex_method_index, pointer_size), |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 373 | resolved_method) << PrettyMethod(resolved_method); |
Vladimir Marko | f096aad | 2014-01-23 15:51:58 +0000 | [diff] [blame] | 374 | int stats_flags = kFlagMethodResolved; |
Igor Murashkin | d6dee67 | 2014-10-16 18:36:16 -0700 | [diff] [blame] | 375 | GetCodeAndMethodForDirectCall(/*out*/invoke_type, |
| 376 | kDirect, // Sharp type |
| 377 | false, // The dex cache is guaranteed to be available |
| 378 | referrer_class, resolved_method, |
| 379 | /*out*/&stats_flags, |
| 380 | target_method, |
| 381 | /*out*/direct_code, |
| 382 | /*out*/direct_method); |
Vladimir Marko | f096aad | 2014-01-23 15:51:58 +0000 | [diff] [blame] | 383 | DCHECK_NE(*invoke_type, kSuper) << PrettyMethod(resolved_method); |
| 384 | if (*invoke_type == kDirect) { |
| 385 | stats_flags |= kFlagsMethodResolvedVirtualMadeDirect; |
| 386 | } |
| 387 | return stats_flags; |
| 388 | } |
| 389 | |
| 390 | if ((*invoke_type == kVirtual || *invoke_type == kInterface) && devirt_target != nullptr) { |
| 391 | // Post-verification callback recorded a more precise invoke target based on its type info. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 392 | ArtMethod* called_method; |
Vladimir Marko | f096aad | 2014-01-23 15:51:58 +0000 | [diff] [blame] | 393 | ClassLinker* class_linker = mUnit->GetClassLinker(); |
| 394 | if (LIKELY(devirt_target->dex_file == mUnit->GetDexFile())) { |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 395 | called_method = class_linker->ResolveMethod<ClassLinker::kNoICCECheckForCache>( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 396 | *devirt_target->dex_file, devirt_target->dex_method_index, dex_cache, class_loader, |
| 397 | nullptr, kVirtual); |
Vladimir Marko | f096aad | 2014-01-23 15:51:58 +0000 | [diff] [blame] | 398 | } else { |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 399 | auto target_dex_cache(hs.NewHandle(class_linker->RegisterDexFile( |
| 400 | *devirt_target->dex_file, |
| 401 | class_linker->GetOrCreateAllocatorForClassLoader(class_loader.Get())))); |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 402 | called_method = class_linker->ResolveMethod<ClassLinker::kNoICCECheckForCache>( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 403 | *devirt_target->dex_file, devirt_target->dex_method_index, target_dex_cache, |
| 404 | class_loader, nullptr, kVirtual); |
Vladimir Marko | f096aad | 2014-01-23 15:51:58 +0000 | [diff] [blame] | 405 | } |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 406 | CHECK(called_method != nullptr); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 407 | CHECK(called_method->IsInvokable()); |
Vladimir Marko | f096aad | 2014-01-23 15:51:58 +0000 | [diff] [blame] | 408 | int stats_flags = kFlagMethodResolved; |
Igor Murashkin | d6dee67 | 2014-10-16 18:36:16 -0700 | [diff] [blame] | 409 | GetCodeAndMethodForDirectCall(/*out*/invoke_type, |
| 410 | kDirect, // Sharp type |
| 411 | true, // The dex cache may not be available |
| 412 | referrer_class, called_method, |
| 413 | /*out*/&stats_flags, |
| 414 | target_method, |
| 415 | /*out*/direct_code, |
| 416 | /*out*/direct_method); |
Vladimir Marko | f096aad | 2014-01-23 15:51:58 +0000 | [diff] [blame] | 417 | DCHECK_NE(*invoke_type, kSuper); |
| 418 | if (*invoke_type == kDirect) { |
| 419 | stats_flags |= kFlagsMethodResolvedPreciseTypeDevirtualization; |
| 420 | } |
| 421 | return stats_flags; |
| 422 | } |
| 423 | |
| 424 | if (UNLIKELY(*invoke_type == kSuper)) { |
| 425 | // Unsharpened super calls are suspicious so go slow-path. |
| 426 | return 0; |
| 427 | } |
| 428 | |
| 429 | // Sharpening failed so generate a regular resolved method dispatch. |
| 430 | int stats_flags = kFlagMethodResolved; |
Igor Murashkin | d6dee67 | 2014-10-16 18:36:16 -0700 | [diff] [blame] | 431 | GetCodeAndMethodForDirectCall(/*out*/invoke_type, |
| 432 | *invoke_type, // Sharp type |
| 433 | false, // The dex cache is guaranteed to be available |
| 434 | referrer_class, resolved_method, |
| 435 | /*out*/&stats_flags, |
| 436 | target_method, |
| 437 | /*out*/direct_code, |
| 438 | /*out*/direct_method); |
Vladimir Marko | f096aad | 2014-01-23 15:51:58 +0000 | [diff] [blame] | 439 | return stats_flags; |
| 440 | } |
| 441 | |
Vladimir Marko | 66c6d7b | 2014-10-16 15:41:48 +0100 | [diff] [blame] | 442 | inline bool CompilerDriver::IsMethodsClassInitialized(mirror::Class* referrer_class, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 443 | ArtMethod* resolved_method) { |
Vladimir Marko | 9820b7c | 2014-01-02 16:40:37 +0000 | [diff] [blame] | 444 | if (!resolved_method->IsStatic()) { |
Vladimir Marko | 66c6d7b | 2014-10-16 15:41:48 +0100 | [diff] [blame] | 445 | return true; |
Vladimir Marko | 9820b7c | 2014-01-02 16:40:37 +0000 | [diff] [blame] | 446 | } |
| 447 | mirror::Class* methods_class = resolved_method->GetDeclaringClass(); |
Vladimir Marko | 07785bb | 2015-06-15 18:52:54 +0100 | [diff] [blame] | 448 | return CanReferrerAssumeClassIsInitialized(referrer_class, methods_class); |
Vladimir Marko | 9820b7c | 2014-01-02 16:40:37 +0000 | [diff] [blame] | 449 | } |
| 450 | |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 451 | } // namespace art |
| 452 | |
| 453 | #endif // ART_COMPILER_DRIVER_COMPILER_DRIVER_INL_H_ |