blob: 1db3b5b914e941c9144b9f479f9c0ae5b88ee31c [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) {
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 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.
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 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.
69 (is_volatile ? kFlagIsVolatile : 0u) |
70 (fast_path.first ? kFlagFastGet : 0u) |
71 (fast_path.second ? kFlagFastPut : 0u);
72 }
73}
74
75void 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 Marko20daf932014-03-03 17:34:22 +000083 // 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 Markobe0e5462014-02-26 11:24:15 +000087 }
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 Chartiereb8167a2014-05-07 15:43:14 -070093 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 Marko66c6d7b2014-10-16 15:41:48 +010097 Handle<mirror::Class> referrer_class_handle(hs.NewHandle(
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070098 compiler_driver->ResolveCompilingMethodsClass(soa, dex_cache, class_loader, mUnit)));
Vladimir Markobe0e5462014-02-26 11:24:15 +000099 // 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 Marko66c6d7b2014-10-16 15:41:48 +0100113 mirror::Class* referrer_class = referrer_class_handle.Get();
Vladimir Markobe0e5462014-02-26 11:24:15 +0000114 std::pair<bool, bool> fast_path = compiler_driver->IsFastStaticField(
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100115 dex_cache.Get(), referrer_class, resolved_field, field_idx, &it->storage_index_);
116 uint16_t flags = kFlagIsStatic |
Vladimir Markobe0e5462014-02-26 11:24:15 +0000117 (is_volatile ? kFlagIsVolatile : 0u) |
118 (fast_path.first ? kFlagFastGet : 0u) |
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100119 (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 Markobe0e5462014-02-26 11:24:15 +0000134 }
135}
136
137} // namespace art