blob: 13bbc3e98312ac0702f31819fc0330c95aca7010 [file] [log] [blame]
Vladimir Markobe0e5462014-02-26 11:24:15 +00001/*
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 Marko20f85592015-03-19 10:07:02 +000022#include "dex/verified_method.h"
Vladimir Markobe0e5462014-02-26 11:24:15 +000023#include "driver/compiler_driver.h"
24#include "driver/compiler_driver-inl.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070025#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 Markobe0e5462014-02-26 11:24:15 +000027#include "scoped_thread_state_change.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070028#include "handle_scope-inl.h"
Vladimir Markobe0e5462014-02-26 11:24:15 +000029
30namespace art {
31
Mathieu Chartier736b5602015-09-02 14:54:11 -070032void MirIFieldLoweringInfo::Resolve(const ScopedObjectAccess& soa,
33 CompilerDriver* compiler_driver,
Vladimir Markobe0e5462014-02-26 11:24:15 +000034 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 Chartiere5f13e52015-02-24 09:37:21 -080040 MirIFieldLoweringInfo unresolved(it->field_idx_, it->MemAccessType(), it->IsQuickened());
41 unresolved.field_offset_ = it->field_offset_;
42 unresolved.CheckEquals(*it);
Vladimir Markobe0e5462014-02-26 11:24:15 +000043 }
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 Chartiereb8167a2014-05-07 15:43:14 -070048 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 Chartiere5f13e52015-02-24 09:37:21 -080054 const VerifiedMethod* const verified_method = mUnit->GetVerifiedMethod();
Vladimir Markobe0e5462014-02-26 11:24:15 +000055 // 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 Markobe0e5462014-02-26 11:24:15 +000057 for (auto it = field_infos, end = field_infos + count; it != end; ++it) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080058 uint32_t field_idx;
Mathieu Chartierc7853442015-03-27 14:35:38 -070059 ArtField* resolved_field;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080060 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 Markobe0e5462014-02-26 11:24:15 +000078 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 Marko66c6d7b2014-10-16 15:41:48 +010084 it->field_offset_ = compiler_driver->GetFieldOffset(resolved_field);
Vladimir Markobe0e5462014-02-26 11:24:15 +000085 std::pair<bool, bool> fast_path = compiler_driver->IsFastInstanceField(
Nicolas Geoffraye5038322014-07-04 09:41:32 +010086 dex_cache.Get(), referrer_class.Get(), resolved_field, field_idx);
Vladimir Markobe0e5462014-02-26 11:24:15 +000087 it->flags_ = 0u | // Without kFlagIsStatic.
Vladimir Markoaf6925b2014-10-31 16:37:32 +000088 (it->flags_ & (kMemAccessTypeMask << kBitMemAccessTypeBegin)) |
Vladimir Markobe0e5462014-02-26 11:24:15 +000089 (is_volatile ? kFlagIsVolatile : 0u) |
90 (fast_path.first ? kFlagFastGet : 0u) |
91 (fast_path.second ? kFlagFastPut : 0u);
92 }
93}
94
95void 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 Markoaf6925b2014-10-31 16:37:32 +0000102 MirSFieldLoweringInfo unresolved(it->field_idx_, it->MemAccessType());
Vladimir Marko20daf932014-03-03 17:34:22 +0000103 // 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 Markobe0e5462014-02-26 11:24:15 +0000107 }
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 Chartiereb8167a2014-05-07 15:43:14 -0700113 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 Marko66c6d7b2014-10-16 15:41:48 +0100117 Handle<mirror::Class> referrer_class_handle(hs.NewHandle(
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700118 compiler_driver->ResolveCompilingMethodsClass(soa, dex_cache, class_loader, mUnit)));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000119 // 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 Chartierc7853442015-03-27 14:35:38 -0700124 ArtField* resolved_field =
Vladimir Markobe0e5462014-02-26 11:24:15 +0000125 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 Marko66c6d7b2014-10-16 15:41:48 +0100133 mirror::Class* referrer_class = referrer_class_handle.Get();
Vladimir Markobe0e5462014-02-26 11:24:15 +0000134 std::pair<bool, bool> fast_path = compiler_driver->IsFastStaticField(
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100135 dex_cache.Get(), referrer_class, resolved_field, field_idx, &it->storage_index_);
136 uint16_t flags = kFlagIsStatic |
Vladimir Markoaf6925b2014-10-31 16:37:32 +0000137 (it->flags_ & (kMemAccessTypeMask << kBitMemAccessTypeBegin)) |
Vladimir Markobe0e5462014-02-26 11:24:15 +0000138 (is_volatile ? kFlagIsVolatile : 0u) |
139 (fast_path.first ? kFlagFastGet : 0u) |
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100140 (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 Markobe0e5462014-02-26 11:24:15 +0000155 }
156}
157
158} // namespace art