blob: 53afcad87143d4d4c56120f148f0622c1efeee41 [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"
22#include "driver/compiler_driver.h"
23#include "driver/compiler_driver-inl.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070024#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 Markobe0e5462014-02-26 11:24:15 +000026#include "scoped_thread_state_change.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070027#include "handle_scope-inl.h"
Vladimir Markobe0e5462014-02-26 11:24:15 +000028
29namespace art {
30
31void 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 Geoffraya5ca8882015-02-24 08:10:57 +000038 MirIFieldLoweringInfo unresolved(it->field_idx_, it->MemAccessType());
39 DCHECK_EQ(memcmp(&unresolved, &*it, sizeof(*it)), 0);
Vladimir Markobe0e5462014-02-26 11:24:15 +000040 }
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 Chartiereb8167a2014-05-07 15:43:14 -070046 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 Markobe0e5462014-02-26 11:24:15 +000052 // 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 Geoffraya5ca8882015-02-24 08:10:57 +000054
Vladimir Markobe0e5462014-02-26 11:24:15 +000055 for (auto it = field_infos, end = field_infos + count; it != end; ++it) {
Nicolas Geoffraya5ca8882015-02-24 08:10:57 +000056 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 Markobe0e5462014-02-26 11:24:15 +000059 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 Marko66c6d7b2014-10-16 15:41:48 +010065 it->field_offset_ = compiler_driver->GetFieldOffset(resolved_field);
Vladimir Markobe0e5462014-02-26 11:24:15 +000066 std::pair<bool, bool> fast_path = compiler_driver->IsFastInstanceField(
Nicolas Geoffraye5038322014-07-04 09:41:32 +010067 dex_cache.Get(), referrer_class.Get(), resolved_field, field_idx);
Vladimir Markobe0e5462014-02-26 11:24:15 +000068 it->flags_ = 0u | // Without kFlagIsStatic.
Vladimir Markoaf6925b2014-10-31 16:37:32 +000069 (it->flags_ & (kMemAccessTypeMask << kBitMemAccessTypeBegin)) |
Vladimir Markobe0e5462014-02-26 11:24:15 +000070 (is_volatile ? kFlagIsVolatile : 0u) |
71 (fast_path.first ? kFlagFastGet : 0u) |
72 (fast_path.second ? kFlagFastPut : 0u);
73 }
74}
75
76void 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 Markoaf6925b2014-10-31 16:37:32 +000083 MirSFieldLoweringInfo unresolved(it->field_idx_, it->MemAccessType());
Vladimir Marko20daf932014-03-03 17:34:22 +000084 // 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 Markobe0e5462014-02-26 11:24:15 +000088 }
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 Chartiereb8167a2014-05-07 15:43:14 -070094 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 Marko66c6d7b2014-10-16 15:41:48 +010098 Handle<mirror::Class> referrer_class_handle(hs.NewHandle(
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070099 compiler_driver->ResolveCompilingMethodsClass(soa, dex_cache, class_loader, mUnit)));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000100 // 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 Marko66c6d7b2014-10-16 15:41:48 +0100114 mirror::Class* referrer_class = referrer_class_handle.Get();
Vladimir Markobe0e5462014-02-26 11:24:15 +0000115 std::pair<bool, bool> fast_path = compiler_driver->IsFastStaticField(
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100116 dex_cache.Get(), referrer_class, resolved_field, field_idx, &it->storage_index_);
117 uint16_t flags = kFlagIsStatic |
Vladimir Markoaf6925b2014-10-31 16:37:32 +0000118 (it->flags_ & (kMemAccessTypeMask << kBitMemAccessTypeBegin)) |
Vladimir Markobe0e5462014-02-26 11:24:15 +0000119 (is_volatile ? kFlagIsVolatile : 0u) |
120 (fast_path.first ? kFlagFastGet : 0u) |
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100121 (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 Markobe0e5462014-02-26 11:24:15 +0000136 }
137}
138
139} // namespace art