mir_field_info.cc revision 20daf93b38870290e13518ad4db0ccfcd7d9f018
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"
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      // In 64-bit builds, there's padding after storage_index_, don't include it in memcmp.
83      size_t size = OFFSETOF_MEMBER(MirSFieldLoweringInfo, storage_index_) +
84          sizeof(it->storage_index_);
85      DCHECK_EQ(memcmp(&unresolved, &*it, size), 0);
86    }
87  }
88
89  // We're going to resolve fields and check access in a tight loop. It's better to hold
90  // the lock and needed references once than re-acquiring them again and again.
91  ScopedObjectAccess soa(Thread::Current());
92  SirtRef<mirror::DexCache> dex_cache(soa.Self(), compiler_driver->GetDexCache(mUnit));
93  SirtRef<mirror::ClassLoader> class_loader(soa.Self(),
94      compiler_driver->GetClassLoader(soa, mUnit));
95  SirtRef<mirror::Class> referrer_class(soa.Self(),
96      compiler_driver->ResolveCompilingMethodsClass(soa, dex_cache, class_loader, mUnit));
97  // Even if the referrer class is unresolved (i.e. we're compiling a method without class
98  // definition) we still want to resolve fields and record all available info.
99
100  for (auto it = field_infos, end = field_infos + count; it != end; ++it) {
101    uint32_t field_idx = it->field_idx_;
102    mirror::ArtField* resolved_field =
103        compiler_driver->ResolveField(soa, dex_cache, class_loader, mUnit, field_idx, true);
104    if (UNLIKELY(resolved_field == nullptr)) {
105      continue;
106    }
107    compiler_driver->GetResolvedFieldDexFileLocation(resolved_field,
108        &it->declaring_dex_file_, &it->declaring_class_idx_, &it->declaring_field_idx_);
109    bool is_volatile = compiler_driver->IsFieldVolatile(resolved_field) ? 1u : 0u;
110
111    bool is_referrers_class, is_initialized;
112    std::pair<bool, bool> fast_path = compiler_driver->IsFastStaticField(
113        dex_cache.get(), referrer_class.get(), resolved_field, field_idx, &it->field_offset_,
114        &it->storage_index_, &is_referrers_class, &is_initialized);
115    it->flags_ = kFlagIsStatic |
116        (is_volatile ? kFlagIsVolatile : 0u) |
117        (fast_path.first ? kFlagFastGet : 0u) |
118        (fast_path.second ? kFlagFastPut : 0u) |
119        (is_referrers_class ? kFlagIsReferrersClass : 0u) |
120        (is_initialized ? kFlagIsInitialized : 0u);
121  }
122}
123
124}  // namespace art
125