blob: 3c7613096b3497be8d7bf785bf8ab08ed362aa0e [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"
24#include "mirror/class_loader.h" // Only to allow casts in SirtRef<ClassLoader>.
25#include "mirror/dex_cache.h" // Only to allow casts in SirtRef<DexCache>.
26#include "scoped_thread_state_change.h"
27#include "sirt_ref.h"
28
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());
46 SirtRef<mirror::DexCache> dex_cache(soa.Self(), compiler_driver->GetDexCache(mUnit));
47 SirtRef<mirror::ClassLoader> class_loader(soa.Self(),
48 compiler_driver->GetClassLoader(soa, mUnit));
49 SirtRef<mirror::Class> referrer_class(soa.Self(),
50 compiler_driver->ResolveCompilingMethodsClass(soa, dex_cache, class_loader, mUnit));
51 // Even if the referrer class is unresolved (i.e. we're compiling a method without class
52 // definition) we still want to resolve fields and record all available info.
53
54 for (auto it = field_infos, end = field_infos + count; it != end; ++it) {
55 uint32_t field_idx = it->field_idx_;
56 mirror::ArtField* resolved_field =
57 compiler_driver->ResolveField(soa, dex_cache, class_loader, mUnit, field_idx, false);
58 if (UNLIKELY(resolved_field == nullptr)) {
59 continue;
60 }
61 compiler_driver->GetResolvedFieldDexFileLocation(resolved_field,
62 &it->declaring_dex_file_, &it->declaring_class_idx_, &it->declaring_field_idx_);
63 bool is_volatile = compiler_driver->IsFieldVolatile(resolved_field);
64
65 std::pair<bool, bool> fast_path = compiler_driver->IsFastInstanceField(
66 dex_cache.get(), referrer_class.get(), resolved_field, field_idx, &it->field_offset_);
67 it->flags_ = 0u | // Without kFlagIsStatic.
68 (is_volatile ? kFlagIsVolatile : 0u) |
69 (fast_path.first ? kFlagFastGet : 0u) |
70 (fast_path.second ? kFlagFastPut : 0u);
71 }
72}
73
74void MirSFieldLoweringInfo::Resolve(CompilerDriver* compiler_driver,
75 const DexCompilationUnit* mUnit,
76 MirSFieldLoweringInfo* field_infos, size_t count) {
77 if (kIsDebugBuild) {
78 DCHECK(field_infos != nullptr);
79 DCHECK_NE(count, 0u);
80 for (auto it = field_infos, end = field_infos + count; it != end; ++it) {
81 MirSFieldLoweringInfo unresolved(it->field_idx_);
82 DCHECK_EQ(memcmp(&unresolved, &*it, sizeof(*it)), 0);
83 }
84 }
85
86 // We're going to resolve fields and check access in a tight loop. It's better to hold
87 // the lock and needed references once than re-acquiring them again and again.
88 ScopedObjectAccess soa(Thread::Current());
89 SirtRef<mirror::DexCache> dex_cache(soa.Self(), compiler_driver->GetDexCache(mUnit));
90 SirtRef<mirror::ClassLoader> class_loader(soa.Self(),
91 compiler_driver->GetClassLoader(soa, mUnit));
92 SirtRef<mirror::Class> referrer_class(soa.Self(),
93 compiler_driver->ResolveCompilingMethodsClass(soa, dex_cache, class_loader, mUnit));
94 // Even if the referrer class is unresolved (i.e. we're compiling a method without class
95 // definition) we still want to resolve fields and record all available info.
96
97 for (auto it = field_infos, end = field_infos + count; it != end; ++it) {
98 uint32_t field_idx = it->field_idx_;
99 mirror::ArtField* resolved_field =
100 compiler_driver->ResolveField(soa, dex_cache, class_loader, mUnit, field_idx, true);
101 if (UNLIKELY(resolved_field == nullptr)) {
102 continue;
103 }
104 compiler_driver->GetResolvedFieldDexFileLocation(resolved_field,
105 &it->declaring_dex_file_, &it->declaring_class_idx_, &it->declaring_field_idx_);
106 bool is_volatile = compiler_driver->IsFieldVolatile(resolved_field) ? 1u : 0u;
107
108 bool is_referrers_class, is_initialized;
109 std::pair<bool, bool> fast_path = compiler_driver->IsFastStaticField(
110 dex_cache.get(), referrer_class.get(), resolved_field, field_idx, &it->field_offset_,
111 &it->storage_index_, &is_referrers_class, &is_initialized);
112 it->flags_ = kFlagIsStatic |
113 (is_volatile ? kFlagIsVolatile : 0u) |
114 (fast_path.first ? kFlagFastGet : 0u) |
115 (fast_path.second ? kFlagFastPut : 0u) |
116 (is_referrers_class ? kFlagIsReferrersClass : 0u) |
117 (is_initialized ? kFlagIsInitialized : 0u);
118 }
119}
120
121} // namespace art