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) { |
| 38 | MirIFieldLoweringInfo unresolved(it->field_idx_); |
| 39 | DCHECK_EQ(memcmp(&unresolved, &*it, sizeof(*it)), 0); |
| 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. |
| 54 | |
| 55 | for (auto it = field_infos, end = field_infos + count; it != end; ++it) { |
| 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); |
| 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. |
| 69 | (is_volatile ? kFlagIsVolatile : 0u) | |
| 70 | (fast_path.first ? kFlagFastGet : 0u) | |
| 71 | (fast_path.second ? kFlagFastPut : 0u); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | void MirSFieldLoweringInfo::Resolve(CompilerDriver* compiler_driver, |
| 76 | const DexCompilationUnit* mUnit, |
| 77 | MirSFieldLoweringInfo* field_infos, size_t count) { |
| 78 | if (kIsDebugBuild) { |
| 79 | DCHECK(field_infos != nullptr); |
| 80 | DCHECK_NE(count, 0u); |
| 81 | for (auto it = field_infos, end = field_infos + count; it != end; ++it) { |
| 82 | MirSFieldLoweringInfo unresolved(it->field_idx_); |
Vladimir Marko | 20daf93 | 2014-03-03 17:34:22 +0000 | [diff] [blame] | 83 | // In 64-bit builds, there's padding after storage_index_, don't include it in memcmp. |
| 84 | size_t size = OFFSETOF_MEMBER(MirSFieldLoweringInfo, storage_index_) + |
| 85 | sizeof(it->storage_index_); |
| 86 | DCHECK_EQ(memcmp(&unresolved, &*it, size), 0); |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
| 90 | // We're going to resolve fields and check access in a tight loop. It's better to hold |
| 91 | // the lock and needed references once than re-acquiring them again and again. |
| 92 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 93 | StackHandleScope<3> hs(soa.Self()); |
| 94 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(compiler_driver->GetDexCache(mUnit))); |
| 95 | Handle<mirror::ClassLoader> class_loader( |
| 96 | hs.NewHandle(compiler_driver->GetClassLoader(soa, mUnit))); |
Vladimir Marko | 66c6d7b | 2014-10-16 15:41:48 +0100 | [diff] [blame] | 97 | Handle<mirror::Class> referrer_class_handle(hs.NewHandle( |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 98 | compiler_driver->ResolveCompilingMethodsClass(soa, dex_cache, class_loader, mUnit))); |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 99 | // Even if the referrer class is unresolved (i.e. we're compiling a method without class |
| 100 | // definition) we still want to resolve fields and record all available info. |
| 101 | |
| 102 | for (auto it = field_infos, end = field_infos + count; it != end; ++it) { |
| 103 | uint32_t field_idx = it->field_idx_; |
| 104 | mirror::ArtField* resolved_field = |
| 105 | compiler_driver->ResolveField(soa, dex_cache, class_loader, mUnit, field_idx, true); |
| 106 | if (UNLIKELY(resolved_field == nullptr)) { |
| 107 | continue; |
| 108 | } |
| 109 | compiler_driver->GetResolvedFieldDexFileLocation(resolved_field, |
| 110 | &it->declaring_dex_file_, &it->declaring_class_idx_, &it->declaring_field_idx_); |
| 111 | bool is_volatile = compiler_driver->IsFieldVolatile(resolved_field) ? 1u : 0u; |
| 112 | |
Vladimir Marko | 66c6d7b | 2014-10-16 15:41:48 +0100 | [diff] [blame] | 113 | mirror::Class* referrer_class = referrer_class_handle.Get(); |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 114 | std::pair<bool, bool> fast_path = compiler_driver->IsFastStaticField( |
Vladimir Marko | 66c6d7b | 2014-10-16 15:41:48 +0100 | [diff] [blame] | 115 | dex_cache.Get(), referrer_class, resolved_field, field_idx, &it->storage_index_); |
| 116 | uint16_t flags = kFlagIsStatic | |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 117 | (is_volatile ? kFlagIsVolatile : 0u) | |
| 118 | (fast_path.first ? kFlagFastGet : 0u) | |
Vladimir Marko | 66c6d7b | 2014-10-16 15:41:48 +0100 | [diff] [blame] | 119 | (fast_path.second ? kFlagFastPut : 0u); |
| 120 | if (fast_path.first) { |
| 121 | it->field_offset_ = compiler_driver->GetFieldOffset(resolved_field); |
| 122 | bool is_referrers_class = |
| 123 | compiler_driver->IsStaticFieldInReferrerClass(referrer_class, resolved_field); |
| 124 | bool is_class_initialized = |
| 125 | compiler_driver->IsStaticFieldsClassInitialized(referrer_class, resolved_field); |
| 126 | bool is_class_in_dex_cache = !is_referrers_class && // If referrer's class, we don't care. |
| 127 | compiler_driver->CanAssumeTypeIsPresentInDexCache(*dex_cache->GetDexFile(), |
| 128 | it->storage_index_); |
| 129 | flags |= (is_referrers_class ? kFlagIsReferrersClass : 0u) | |
| 130 | (is_class_initialized ? kFlagClassIsInitialized : 0u) | |
| 131 | (is_class_in_dex_cache ? kFlagClassIsInDexCache : 0u); |
| 132 | } |
| 133 | it->flags_ = flags; |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | |
| 137 | } // namespace art |