Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | #include "mir_field_info.h" |
| 18 | |
| 19 | #include <string.h> |
| 20 | |
| 21 | #include "base/logging.h" |
| 22 | #include "driver/compiler_driver.h" |
| 23 | #include "driver/compiler_driver-inl.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 24 | #include "mirror/class_loader.h" // Only to allow casts in Handle<ClassLoader>. |
| 25 | #include "mirror/dex_cache.h" // Only to allow casts in Handle<DexCache>. |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 26 | #include "scoped_thread_state_change.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 27 | #include "handle_scope-inl.h" |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 28 | |
| 29 | namespace art { |
| 30 | |
| 31 | void MirIFieldLoweringInfo::Resolve(CompilerDriver* compiler_driver, |
| 32 | const DexCompilationUnit* mUnit, |
| 33 | MirIFieldLoweringInfo* field_infos, size_t count) { |
| 34 | if (kIsDebugBuild) { |
| 35 | DCHECK(field_infos != nullptr); |
| 36 | DCHECK_NE(count, 0u); |
| 37 | for (auto it = field_infos, end = field_infos + count; it != end; ++it) { |
Nicolas Geoffray | a5ca888 | 2015-02-24 08:10:57 +0000 | [diff] [blame] | 38 | MirIFieldLoweringInfo unresolved(it->field_idx_, it->MemAccessType()); |
| 39 | DCHECK_EQ(memcmp(&unresolved, &*it, sizeof(*it)), 0); |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 40 | } |
| 41 | } |
| 42 | |
| 43 | // We're going to resolve fields and check access in a tight loop. It's better to hold |
| 44 | // the lock and needed references once than re-acquiring them again and again. |
| 45 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 46 | StackHandleScope<3> hs(soa.Self()); |
| 47 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(compiler_driver->GetDexCache(mUnit))); |
| 48 | Handle<mirror::ClassLoader> class_loader( |
| 49 | hs.NewHandle(compiler_driver->GetClassLoader(soa, mUnit))); |
| 50 | Handle<mirror::Class> referrer_class(hs.NewHandle( |
| 51 | compiler_driver->ResolveCompilingMethodsClass(soa, dex_cache, class_loader, mUnit))); |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 52 | // Even if the referrer class is unresolved (i.e. we're compiling a method without class |
| 53 | // definition) we still want to resolve fields and record all available info. |
Nicolas Geoffray | a5ca888 | 2015-02-24 08:10:57 +0000 | [diff] [blame] | 54 | |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 55 | for (auto it = field_infos, end = field_infos + count; it != end; ++it) { |
Nicolas Geoffray | a5ca888 | 2015-02-24 08:10:57 +0000 | [diff] [blame] | 56 | uint32_t field_idx = it->field_idx_; |
| 57 | mirror::ArtField* resolved_field = |
| 58 | compiler_driver->ResolveField(soa, dex_cache, class_loader, mUnit, field_idx, false); |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 59 | if (UNLIKELY(resolved_field == nullptr)) { |
| 60 | continue; |
| 61 | } |
| 62 | compiler_driver->GetResolvedFieldDexFileLocation(resolved_field, |
| 63 | &it->declaring_dex_file_, &it->declaring_class_idx_, &it->declaring_field_idx_); |
| 64 | bool is_volatile = compiler_driver->IsFieldVolatile(resolved_field); |
Vladimir Marko | 66c6d7b | 2014-10-16 15:41:48 +0100 | [diff] [blame] | 65 | it->field_offset_ = compiler_driver->GetFieldOffset(resolved_field); |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 66 | std::pair<bool, bool> fast_path = compiler_driver->IsFastInstanceField( |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 67 | dex_cache.Get(), referrer_class.Get(), resolved_field, field_idx); |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 68 | it->flags_ = 0u | // Without kFlagIsStatic. |
Vladimir Marko | af6925b | 2014-10-31 16:37:32 +0000 | [diff] [blame] | 69 | (it->flags_ & (kMemAccessTypeMask << kBitMemAccessTypeBegin)) | |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 70 | (is_volatile ? kFlagIsVolatile : 0u) | |
| 71 | (fast_path.first ? kFlagFastGet : 0u) | |
| 72 | (fast_path.second ? kFlagFastPut : 0u); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | void MirSFieldLoweringInfo::Resolve(CompilerDriver* compiler_driver, |
| 77 | const DexCompilationUnit* mUnit, |
| 78 | MirSFieldLoweringInfo* field_infos, size_t count) { |
| 79 | if (kIsDebugBuild) { |
| 80 | DCHECK(field_infos != nullptr); |
| 81 | DCHECK_NE(count, 0u); |
| 82 | for (auto it = field_infos, end = field_infos + count; it != end; ++it) { |
Vladimir Marko | af6925b | 2014-10-31 16:37:32 +0000 | [diff] [blame] | 83 | MirSFieldLoweringInfo unresolved(it->field_idx_, it->MemAccessType()); |
Vladimir Marko | 20daf93 | 2014-03-03 17:34:22 +0000 | [diff] [blame] | 84 | // In 64-bit builds, there's padding after storage_index_, don't include it in memcmp. |
| 85 | size_t size = OFFSETOF_MEMBER(MirSFieldLoweringInfo, storage_index_) + |
| 86 | sizeof(it->storage_index_); |
| 87 | DCHECK_EQ(memcmp(&unresolved, &*it, size), 0); |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
| 91 | // We're going to resolve fields and check access in a tight loop. It's better to hold |
| 92 | // the lock and needed references once than re-acquiring them again and again. |
| 93 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 94 | StackHandleScope<3> hs(soa.Self()); |
| 95 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(compiler_driver->GetDexCache(mUnit))); |
| 96 | Handle<mirror::ClassLoader> class_loader( |
| 97 | hs.NewHandle(compiler_driver->GetClassLoader(soa, mUnit))); |
Vladimir Marko | 66c6d7b | 2014-10-16 15:41:48 +0100 | [diff] [blame] | 98 | Handle<mirror::Class> referrer_class_handle(hs.NewHandle( |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 99 | compiler_driver->ResolveCompilingMethodsClass(soa, dex_cache, class_loader, mUnit))); |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 100 | // Even if the referrer class is unresolved (i.e. we're compiling a method without class |
| 101 | // definition) we still want to resolve fields and record all available info. |
| 102 | |
| 103 | for (auto it = field_infos, end = field_infos + count; it != end; ++it) { |
| 104 | uint32_t field_idx = it->field_idx_; |
| 105 | mirror::ArtField* resolved_field = |
| 106 | compiler_driver->ResolveField(soa, dex_cache, class_loader, mUnit, field_idx, true); |
| 107 | if (UNLIKELY(resolved_field == nullptr)) { |
| 108 | continue; |
| 109 | } |
| 110 | compiler_driver->GetResolvedFieldDexFileLocation(resolved_field, |
| 111 | &it->declaring_dex_file_, &it->declaring_class_idx_, &it->declaring_field_idx_); |
| 112 | bool is_volatile = compiler_driver->IsFieldVolatile(resolved_field) ? 1u : 0u; |
| 113 | |
Vladimir Marko | 66c6d7b | 2014-10-16 15:41:48 +0100 | [diff] [blame] | 114 | mirror::Class* referrer_class = referrer_class_handle.Get(); |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 115 | std::pair<bool, bool> fast_path = compiler_driver->IsFastStaticField( |
Vladimir Marko | 66c6d7b | 2014-10-16 15:41:48 +0100 | [diff] [blame] | 116 | dex_cache.Get(), referrer_class, resolved_field, field_idx, &it->storage_index_); |
| 117 | uint16_t flags = kFlagIsStatic | |
Vladimir Marko | af6925b | 2014-10-31 16:37:32 +0000 | [diff] [blame] | 118 | (it->flags_ & (kMemAccessTypeMask << kBitMemAccessTypeBegin)) | |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 119 | (is_volatile ? kFlagIsVolatile : 0u) | |
| 120 | (fast_path.first ? kFlagFastGet : 0u) | |
Vladimir Marko | 66c6d7b | 2014-10-16 15:41:48 +0100 | [diff] [blame] | 121 | (fast_path.second ? kFlagFastPut : 0u); |
| 122 | if (fast_path.first) { |
| 123 | it->field_offset_ = compiler_driver->GetFieldOffset(resolved_field); |
| 124 | bool is_referrers_class = |
| 125 | compiler_driver->IsStaticFieldInReferrerClass(referrer_class, resolved_field); |
| 126 | bool is_class_initialized = |
| 127 | compiler_driver->IsStaticFieldsClassInitialized(referrer_class, resolved_field); |
| 128 | bool is_class_in_dex_cache = !is_referrers_class && // If referrer's class, we don't care. |
| 129 | compiler_driver->CanAssumeTypeIsPresentInDexCache(*dex_cache->GetDexFile(), |
| 130 | it->storage_index_); |
| 131 | flags |= (is_referrers_class ? kFlagIsReferrersClass : 0u) | |
| 132 | (is_class_initialized ? kFlagClassIsInitialized : 0u) | |
| 133 | (is_class_in_dex_cache ? kFlagClassIsInDexCache : 0u); |
| 134 | } |
| 135 | it->flags_ = flags; |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
| 139 | } // namespace art |