blob: 3d3d9790c350ef3737f520c0c862f02b1aefdbb3 [file] [log] [blame]
Vladimir Markof096aad2014-01-23 15:51:58 +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_method_info.h"
18
19#include "driver/compiler_driver.h"
20#include "driver/dex_compilation_unit.h"
21#include "driver/compiler_driver-inl.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070022#include "mirror/class_loader.h" // Only to allow casts in Handle<ClassLoader>.
23#include "mirror/dex_cache.h" // Only to allow casts in Handle<DexCache>.
Vladimir Markof096aad2014-01-23 15:51:58 +000024#include "scoped_thread_state_change.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070025#include "handle_scope-inl.h"
Vladimir Markof096aad2014-01-23 15:51:58 +000026
27namespace art {
28
29void MirMethodLoweringInfo::Resolve(CompilerDriver* compiler_driver,
30 const DexCompilationUnit* mUnit,
31 MirMethodLoweringInfo* method_infos, size_t count) {
32 if (kIsDebugBuild) {
33 DCHECK(method_infos != nullptr);
34 DCHECK_NE(count, 0u);
35 for (auto it = method_infos, end = method_infos + count; it != end; ++it) {
Mathieu Chartier2535abe2015-02-17 10:38:49 -080036 MirMethodLoweringInfo unresolved(it->MethodIndex(), it->GetInvokeType(), it->IsQuickened());
37 unresolved.declaring_dex_file_ = it->declaring_dex_file_;
38 unresolved.vtable_idx_ = it->vtable_idx_;
Vladimir Markof096aad2014-01-23 15:51:58 +000039 if (it->target_dex_file_ != nullptr) {
40 unresolved.target_dex_file_ = it->target_dex_file_;
41 unresolved.target_method_idx_ = it->target_method_idx_;
42 }
Mathieu Chartier2535abe2015-02-17 10:38:49 -080043 if (kIsDebugBuild) {
44 unresolved.CheckEquals(*it);
45 }
Vladimir Markof096aad2014-01-23 15:51:58 +000046 }
47 }
48
49 // We're going to resolve methods and check access in a tight loop. It's better to hold
50 // the lock and needed references once than re-acquiring them again and again.
51 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier2535abe2015-02-17 10:38:49 -080052 StackHandleScope<4> hs(soa.Self());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070053 Handle<mirror::DexCache> dex_cache(hs.NewHandle(compiler_driver->GetDexCache(mUnit)));
54 Handle<mirror::ClassLoader> class_loader(
55 hs.NewHandle(compiler_driver->GetClassLoader(soa, mUnit)));
56 Handle<mirror::Class> referrer_class(hs.NewHandle(
57 compiler_driver->ResolveCompilingMethodsClass(soa, dex_cache, class_loader, mUnit)));
Mathieu Chartier2535abe2015-02-17 10:38:49 -080058 auto current_dex_cache(hs.NewHandle<mirror::DexCache>(nullptr));
Vladimir Markof096aad2014-01-23 15:51:58 +000059 // Even if the referrer class is unresolved (i.e. we're compiling a method without class
60 // definition) we still want to resolve methods and record all available info.
Mathieu Chartier2535abe2015-02-17 10:38:49 -080061 const DexFile* const dex_file = mUnit->GetDexFile();
62 const bool use_jit = Runtime::Current()->UseJit();
63 const VerifiedMethod* const verified_method = mUnit->GetVerifiedMethod();
Vladimir Markof096aad2014-01-23 15:51:58 +000064
65 for (auto it = method_infos, end = method_infos + count; it != end; ++it) {
Mathieu Chartier2535abe2015-02-17 10:38:49 -080066 // For quickened invokes, the dex method idx is actually the mir offset.
67 if (it->IsQuickened()) {
68 const auto* dequicken_ref = verified_method->GetDequickenIndex(it->method_idx_);
69 CHECK(dequicken_ref != nullptr);
70 it->target_dex_file_ = dequicken_ref->dex_file;
71 it->target_method_idx_ = dequicken_ref->index;
72 }
Vladimir Markof096aad2014-01-23 15:51:58 +000073 // Remember devirtualized invoke target and set the called method to the default.
74 MethodReference devirt_ref(it->target_dex_file_, it->target_method_idx_);
75 MethodReference* devirt_target = (it->target_dex_file_ != nullptr) ? &devirt_ref : nullptr;
Vladimir Markof096aad2014-01-23 15:51:58 +000076 InvokeType invoke_type = it->GetInvokeType();
Mathieu Chartier2535abe2015-02-17 10:38:49 -080077 mirror::ArtMethod* resolved_method = nullptr;
78 if (!it->IsQuickened()) {
79 it->target_dex_file_ = dex_file;
80 it->target_method_idx_ = it->MethodIndex();
81 current_dex_cache.Assign(dex_cache.Get());
82 resolved_method = compiler_driver->ResolveMethod(soa, dex_cache, class_loader, mUnit,
83 it->MethodIndex(), invoke_type);
84 } else {
85 // The method index is actually the dex PC in this case.
86 // Calculate the proper dex file and target method idx.
87 CHECK(use_jit);
88 CHECK_EQ(invoke_type, kVirtual);
89 // Don't devirt if we are in a different dex file since we can't have direct invokes in
90 // another dex file unless we always put a direct / patch pointer.
91 devirt_target = nullptr;
92 current_dex_cache.Assign(
93 Runtime::Current()->GetClassLinker()->FindDexCache(*it->target_dex_file_));
94 CHECK(current_dex_cache.Get() != nullptr);
95 DexCompilationUnit cu(
96 mUnit->GetCompilationUnit(), mUnit->GetClassLoader(), mUnit->GetClassLinker(),
97 *it->target_dex_file_, nullptr /* code_item not used */, 0u /* class_def_idx not used */,
98 it->target_method_idx_, 0u /* access_flags not used */,
99 nullptr /* verified_method not used */);
100 resolved_method = compiler_driver->ResolveMethod(soa, current_dex_cache, class_loader, &cu,
101 it->target_method_idx_, invoke_type, false);
102 if (resolved_method != nullptr) {
103 // Since this was a dequickened virtual, it is guaranteed to be resolved. However, it may be
104 // resolved to an interface method. If this is the case then change the invoke type to
105 // interface with the assumption that sharp_type will be kVirtual.
106 if (resolved_method->GetInvokeType() == kInterface) {
107 it->flags_ = (it->flags_ & ~(kInvokeTypeMask << kBitInvokeTypeBegin)) |
108 (static_cast<uint16_t>(kInterface) << kBitInvokeTypeBegin);
109 }
110 }
111 }
Vladimir Markof096aad2014-01-23 15:51:58 +0000112 if (UNLIKELY(resolved_method == nullptr)) {
113 continue;
114 }
115 compiler_driver->GetResolvedMethodDexFileLocation(resolved_method,
116 &it->declaring_dex_file_, &it->declaring_class_idx_, &it->declaring_method_idx_);
Mathieu Chartier2535abe2015-02-17 10:38:49 -0800117 if (!it->IsQuickened()) {
118 // For quickened invoke virtuals we may have desharpened to an interface method which
119 // wont give us the right method index, in this case blindly dispatch or else we can't
120 // compile the method. Converting the invoke to interface dispatch doesn't work since we
121 // have no way to get the dex method index for quickened invoke virtuals in the interface
122 // trampolines.
123 it->vtable_idx_ =
124 compiler_driver->GetResolvedMethodVTableIndex(resolved_method, invoke_type);
125 }
Vladimir Markof096aad2014-01-23 15:51:58 +0000126
Mathieu Chartier2535abe2015-02-17 10:38:49 -0800127 MethodReference target_method(it->target_dex_file_, it->target_method_idx_);
Vladimir Markof096aad2014-01-23 15:51:58 +0000128 int fast_path_flags = compiler_driver->IsFastInvoke(
Mathieu Chartier2535abe2015-02-17 10:38:49 -0800129 soa, dex_cache, class_loader, mUnit, referrer_class.Get(), resolved_method,
130 &invoke_type, &target_method, devirt_target, &it->direct_code_, &it->direct_method_);
131 const bool is_referrers_class = referrer_class.Get() == resolved_method->GetDeclaringClass();
132 const bool is_class_initialized =
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100133 compiler_driver->IsMethodsClassInitialized(referrer_class.Get(), resolved_method);
Vladimir Marko9820b7c2014-01-02 16:40:37 +0000134 uint16_t other_flags = it->flags_ &
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100135 ~(kFlagFastPath | kFlagClassIsInitialized | (kInvokeTypeMask << kBitSharpTypeBegin));
Vladimir Markof096aad2014-01-23 15:51:58 +0000136 it->flags_ = other_flags |
137 (fast_path_flags != 0 ? kFlagFastPath : 0u) |
Vladimir Marko9820b7c2014-01-02 16:40:37 +0000138 (static_cast<uint16_t>(invoke_type) << kBitSharpTypeBegin) |
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100139 (is_referrers_class ? kFlagIsReferrersClass : 0u) |
140 (is_class_initialized ? kFlagClassIsInitialized : 0u);
Vladimir Markof096aad2014-01-23 15:51:58 +0000141 it->target_dex_file_ = target_method.dex_file;
142 it->target_method_idx_ = target_method.dex_method_index;
143 it->stats_flags_ = fast_path_flags;
144 }
145}
146
147} // namespace art