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