blob: 2670f9130083423184eb8e82667a9a98b3b59069 [file] [log] [blame]
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001/*
2 * Copyright (C) 2011 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
Mathieu Chartiere401d142015-04-22 13:56:20 -070017#ifndef ART_RUNTIME_ART_METHOD_INL_H_
18#define ART_RUNTIME_ART_METHOD_INL_H_
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080019
Brian Carlstromea46f952013-07-30 01:26:50 -070020#include "art_method.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080021
Andreas Gampe58a5af82014-07-31 16:23:49 -070022#include "art_field.h"
Andreas Gampe8228cdf2017-05-30 15:03:54 -070023#include "base/callee_save_type.h"
Hiroshi Yamauchi00370822015-08-18 14:47:25 -070024#include "class_linker-inl.h"
Vladimir Marko05792b92015-08-03 11:56:49 +010025#include "common_throws.h"
David Sehr9e734c72018-01-04 17:56:19 -080026#include "dex/code_item_accessors-inl.h"
27#include "dex/dex_file-inl.h"
28#include "dex/dex_file_annotations.h"
29#include "dex/dex_file_types.h"
David Sehr8c0961f2018-01-23 16:11:38 -080030#include "dex/invoke_type.h"
David Sehr67bf42e2018-02-26 16:43:04 -080031#include "dex/primitive.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070032#include "gc_root-inl.h"
David Srbeckye36e7f22018-11-14 14:21:23 +000033#include "imtable-inl.h"
David Brazdil87144352018-01-24 12:50:01 +000034#include "intrinsics_enum.h"
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010035#include "jit/profiling_info.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070036#include "mirror/class-inl.h"
Vladimir Marko05792b92015-08-03 11:56:49 +010037#include "mirror/dex_cache-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070038#include "mirror/object-inl.h"
39#include "mirror/object_array.h"
Nicolas Geoffray4c0b4bc2017-03-17 13:08:26 +000040#include "mirror/string.h"
Vladimir Marko96c6ab92014-04-08 14:00:50 +010041#include "oat.h"
Mathieu Chartier28357fa2016-10-18 16:27:40 -070042#include "obj_ptr-inl.h"
Vladimir Marko7624d252014-05-02 14:40:15 +010043#include "quick/quick_method_frame_info.h"
Alex Lightd7661582017-05-01 13:48:16 -070044#include "read_barrier-inl.h"
Vladimir Marko7624d252014-05-02 14:40:15 +010045#include "runtime-inl.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070046#include "thread-current-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080047
48namespace art {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080049
Mathieu Chartiere7f75f32016-02-01 16:08:15 -080050template <ReadBarrierOption kReadBarrierOption>
Vladimir Markod93e3742018-07-18 10:58:13 +010051inline ObjPtr<mirror::Class> ArtMethod::GetDeclaringClassUnchecked() {
Hiroshi Yamauchi3f64f252015-06-12 18:35:06 -070052 GcRootSource gc_root_source(this);
Mathieu Chartiere7f75f32016-02-01 16:08:15 -080053 return declaring_class_.Read<kReadBarrierOption>(&gc_root_source);
Mingyao Yang98d1cc82014-05-15 17:02:16 -070054}
55
Mathieu Chartiere7f75f32016-02-01 16:08:15 -080056template <ReadBarrierOption kReadBarrierOption>
Vladimir Markod93e3742018-07-18 10:58:13 +010057inline ObjPtr<mirror::Class> ArtMethod::GetDeclaringClass() {
58 ObjPtr<mirror::Class> result = GetDeclaringClassUnchecked<kReadBarrierOption>();
Mathieu Chartiere401d142015-04-22 13:56:20 -070059 if (kIsDebugBuild) {
60 if (!IsRuntimeMethod()) {
61 CHECK(result != nullptr) << this;
Mathieu Chartiere401d142015-04-22 13:56:20 -070062 } else {
63 CHECK(result == nullptr) << this;
64 }
65 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080066 return result;
67}
68
Mathieu Chartier28357fa2016-10-18 16:27:40 -070069inline void ArtMethod::SetDeclaringClass(ObjPtr<mirror::Class> new_declaring_class) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070070 declaring_class_ = GcRoot<mirror::Class>(new_declaring_class);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080071}
72
Vladimir Markod93e3742018-07-18 10:58:13 +010073inline bool ArtMethod::CASDeclaringClass(ObjPtr<mirror::Class> expected_class,
74 ObjPtr<mirror::Class> desired_class) {
Mathieu Chartier10e5ea92015-08-13 12:56:31 -070075 GcRoot<mirror::Class> expected_root(expected_class);
76 GcRoot<mirror::Class> desired_root(desired_class);
Orion Hodson4557b382018-01-03 11:47:54 +000077 auto atomic_root_class = reinterpret_cast<Atomic<GcRoot<mirror::Class>>*>(&declaring_class_);
78 return atomic_root_class->CompareAndSetStrongSequentiallyConsistent(expected_root, desired_root);
Mathieu Chartier10e5ea92015-08-13 12:56:31 -070079}
80
Ian Rogersef7d42f2014-01-06 12:55:46 -080081inline uint16_t ArtMethod::GetMethodIndex() {
Vladimir Marko72ab6842017-01-20 19:32:50 +000082 DCHECK(IsRuntimeMethod() || GetDeclaringClass()->IsResolved());
Mathieu Chartiere401d142015-04-22 13:56:20 -070083 return method_index_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080084}
85
Mathieu Chartier9f3629d2014-10-28 18:23:02 -070086inline uint16_t ArtMethod::GetMethodIndexDuringLinking() {
Mathieu Chartiere401d142015-04-22 13:56:20 -070087 return method_index_;
Mathieu Chartier9f3629d2014-10-28 18:23:02 -070088}
89
Vladimir Markob45528c2017-07-27 14:14:28 +010090inline ObjPtr<mirror::Class> ArtMethod::LookupResolvedClassFromTypeIndex(dex::TypeIndex type_idx) {
Vladimir Marko4098a7a2017-11-06 16:00:51 +000091 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Vladimir Marko666ee3d2017-12-11 18:37:36 +000092 ObjPtr<mirror::Class> type =
93 Runtime::Current()->GetClassLinker()->LookupResolvedType(type_idx, this);
94 DCHECK(!Thread::Current()->IsExceptionPending());
95 return type;
Vladimir Markob45528c2017-07-27 14:14:28 +010096}
97
98inline ObjPtr<mirror::Class> ArtMethod::ResolveClassFromTypeIndex(dex::TypeIndex type_idx) {
Vladimir Marko666ee3d2017-12-11 18:37:36 +000099 ObjPtr<mirror::Class> type = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, this);
100 DCHECK_EQ(type == nullptr, Thread::Current()->IsExceptionPending());
101 return type;
Ian Rogersa0485602014-12-02 15:48:04 -0800102}
103
Brian Carlstromea46f952013-07-30 01:26:50 -0700104inline bool ArtMethod::CheckIncompatibleClassChange(InvokeType type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800105 switch (type) {
106 case kStatic:
107 return !IsStatic();
108 case kDirect:
109 return !IsDirect() || IsStatic();
110 case kVirtual: {
Alex Lightd6e0fa92016-10-17 13:02:39 -0700111 // We have an error if we are direct or a non-copied (i.e. not part of a real class) interface
112 // method.
Vladimir Markod93e3742018-07-18 10:58:13 +0100113 ObjPtr<mirror::Class> methods_class = GetDeclaringClass();
Alex Lightd6e0fa92016-10-17 13:02:39 -0700114 return IsDirect() || (methods_class->IsInterface() && !IsCopied());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800115 }
116 case kSuper:
Andreas Gampe8f252e62014-08-25 20:46:31 -0700117 // Constructors and static methods are called with invoke-direct.
Alex Light705ad492015-09-21 11:36:30 -0700118 return IsConstructor() || IsStatic();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800119 case kInterface: {
Vladimir Markod93e3742018-07-18 10:58:13 +0100120 ObjPtr<mirror::Class> methods_class = GetDeclaringClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800121 return IsDirect() || !(methods_class->IsInterface() || methods_class->IsObjectClass());
122 }
123 default:
124 LOG(FATAL) << "Unreachable - invocation type: " << type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700125 UNREACHABLE();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800126 }
127}
128
Ian Rogersef7d42f2014-01-06 12:55:46 -0800129inline bool ArtMethod::IsCalleeSaveMethod() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800130 if (!IsRuntimeMethod()) {
131 return false;
132 }
133 Runtime* runtime = Runtime::Current();
134 bool result = false;
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700135 for (uint32_t i = 0; i < static_cast<uint32_t>(CalleeSaveType::kLastCalleeSaveType); i++) {
136 if (this == runtime->GetCalleeSaveMethod(CalleeSaveType(i))) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800137 result = true;
138 break;
139 }
140 }
141 return result;
142}
143
Ian Rogersef7d42f2014-01-06 12:55:46 -0800144inline bool ArtMethod::IsResolutionMethod() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800145 bool result = this == Runtime::Current()->GetResolutionMethod();
146 // Check that if we do think it is phony it looks like the resolution method.
147 DCHECK(!result || IsRuntimeMethod());
148 return result;
149}
Jeff Hao88474b42013-10-23 16:24:40 -0700150
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700151inline bool ArtMethod::IsImtUnimplementedMethod() {
152 bool result = this == Runtime::Current()->GetImtUnimplementedMethod();
153 // Check that if we do think it is phony it looks like the imt unimplemented method.
154 DCHECK(!result || IsRuntimeMethod());
155 return result;
156}
157
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700158inline const DexFile* ArtMethod::GetDexFile() {
Mathieu Chartier137cdfa2017-01-26 14:03:11 -0800159 // It is safe to avoid the read barrier here since the dex file is constant, so if we read the
160 // from-space dex file pointer it will be equal to the to-space copy.
161 return GetDexCache<kWithoutReadBarrier>()->GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700162}
163
164inline const char* ArtMethod::GetDeclaringClassDescriptor() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700165 uint32_t dex_method_idx = GetDexMethodIndex();
Andreas Gampee2abbc62017-09-15 11:59:26 -0700166 if (UNLIKELY(dex_method_idx == dex::kDexNoIndex)) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700167 return "<runtime method>";
168 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700169 DCHECK(!IsProxyMethod());
170 const DexFile* dex_file = GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700171 return dex_file->GetMethodDeclaringClassDescriptor(dex_file->GetMethodId(dex_method_idx));
172}
173
Andreas Gampec6ea7d02017-02-01 16:46:28 -0800174inline const char* ArtMethod::GetShorty() {
175 uint32_t unused_length;
176 return GetShorty(&unused_length);
177}
178
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700179inline const char* ArtMethod::GetShorty(uint32_t* out_length) {
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000180 DCHECK(!IsProxyMethod());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700181 const DexFile* dex_file = GetDexFile();
Vladimir Markoc945e0d2018-07-18 17:26:45 +0100182 return dex_file->GetMethodShorty(dex_file->GetMethodId(GetDexMethodIndex()), out_length);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700183}
184
185inline const Signature ArtMethod::GetSignature() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700186 uint32_t dex_method_idx = GetDexMethodIndex();
Andreas Gampee2abbc62017-09-15 11:59:26 -0700187 if (dex_method_idx != dex::kDexNoIndex) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700188 DCHECK(!IsProxyMethod());
189 const DexFile* dex_file = GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700190 return dex_file->GetMethodSignature(dex_file->GetMethodId(dex_method_idx));
191 }
192 return Signature::NoSignature();
193}
194
Ian Rogers1ff3c982014-08-12 02:30:58 -0700195inline const char* ArtMethod::GetName() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700196 uint32_t dex_method_idx = GetDexMethodIndex();
Andreas Gampee2abbc62017-09-15 11:59:26 -0700197 if (LIKELY(dex_method_idx != dex::kDexNoIndex)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700198 DCHECK(!IsProxyMethod());
199 const DexFile* dex_file = GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700200 return dex_file->GetMethodName(dex_file->GetMethodId(dex_method_idx));
201 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700202 Runtime* const runtime = Runtime::Current();
203 if (this == runtime->GetResolutionMethod()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700204 return "<runtime internal resolution method>";
Mathieu Chartiere401d142015-04-22 13:56:20 -0700205 } else if (this == runtime->GetImtConflictMethod()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700206 return "<runtime internal imt conflict method>";
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700207 } else if (this == runtime->GetCalleeSaveMethod(CalleeSaveType::kSaveAllCalleeSaves)) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700208 return "<runtime internal callee-save all registers method>";
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700209 } else if (this == runtime->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsOnly)) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700210 return "<runtime internal callee-save reference registers method>";
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700211 } else if (this == runtime->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs)) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700212 return "<runtime internal callee-save reference and argument registers method>";
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700213 } else if (this == runtime->GetCalleeSaveMethod(CalleeSaveType::kSaveEverything)) {
214 return "<runtime internal save-every-register method>";
Mingyao Yang0a87a652017-04-12 13:43:15 -0700215 } else if (this == runtime->GetCalleeSaveMethod(CalleeSaveType::kSaveEverythingForClinit)) {
216 return "<runtime internal save-every-register method for clinit>";
217 } else if (this == runtime->GetCalleeSaveMethod(CalleeSaveType::kSaveEverythingForSuspendCheck)) {
218 return "<runtime internal save-every-register method for suspend check>";
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700219 } else {
220 return "<unknown runtime internal method>";
221 }
222}
223
Vladimir Marko18090d12018-06-01 16:53:12 +0100224inline ObjPtr<mirror::String> ArtMethod::ResolveNameString() {
225 DCHECK(!IsProxyMethod());
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800226 const dex::MethodId& method_id = GetDexFile()->GetMethodId(GetDexMethodIndex());
Vladimir Marko18090d12018-06-01 16:53:12 +0100227 return Runtime::Current()->GetClassLinker()->ResolveString(method_id.name_idx_, this);
228}
229
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800230inline const dex::CodeItem* ArtMethod::GetCodeItem() {
Alex Lightdba61482016-12-21 08:20:29 -0800231 return GetDexFile()->GetCodeItem(GetCodeItemOffset());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700232}
233
Vladimir Marko942fd312017-01-16 20:52:19 +0000234inline bool ArtMethod::IsResolvedTypeIdx(dex::TypeIndex type_idx) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700235 DCHECK(!IsProxyMethod());
Vladimir Markob45528c2017-07-27 14:14:28 +0100236 return LookupResolvedClassFromTypeIndex(type_idx) != nullptr;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700237}
238
239inline int32_t ArtMethod::GetLineNumFromDexPC(uint32_t dex_pc) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700240 DCHECK(!IsProxyMethod());
Andreas Gampee2abbc62017-09-15 11:59:26 -0700241 if (dex_pc == dex::kDexNoIndex) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700242 return IsNative() ? -2 : -1;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700243 }
David Sehr9323e6e2016-09-13 08:58:35 -0700244 return annotations::GetLineNumFromPC(GetDexFile(), this, dex_pc);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700245}
246
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800247inline const dex::ProtoId& ArtMethod::GetPrototype() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700248 DCHECK(!IsProxyMethod());
249 const DexFile* dex_file = GetDexFile();
250 return dex_file->GetMethodPrototype(dex_file->GetMethodId(GetDexMethodIndex()));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700251}
252
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800253inline const dex::TypeList* ArtMethod::GetParameterTypeList() {
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000254 DCHECK(!IsProxyMethod());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700255 const DexFile* dex_file = GetDexFile();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800256 const dex::ProtoId& proto = dex_file->GetMethodPrototype(
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000257 dex_file->GetMethodId(GetDexMethodIndex()));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700258 return dex_file->GetProtoParameters(proto);
259}
260
261inline const char* ArtMethod::GetDeclaringClassSourceFile() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700262 DCHECK(!IsProxyMethod());
263 return GetDeclaringClass()->GetSourceFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700264}
265
266inline uint16_t ArtMethod::GetClassDefIndex() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700267 DCHECK(!IsProxyMethod());
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000268 if (LIKELY(!IsObsolete())) {
269 return GetDeclaringClass()->GetDexClassDefIndex();
270 } else {
271 return FindObsoleteDexClassDefIndex();
272 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700273}
274
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800275inline const dex::ClassDef& ArtMethod::GetClassDef() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700276 DCHECK(!IsProxyMethod());
277 return GetDexFile()->GetClassDef(GetClassDefIndex());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700278}
279
Orion Hodson58143d22018-02-20 08:44:20 +0000280inline size_t ArtMethod::GetNumberOfParameters() {
281 constexpr size_t return_type_count = 1u;
282 return strlen(GetShorty()) - return_type_count;
283}
284
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700285inline const char* ArtMethod::GetReturnTypeDescriptor() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700286 DCHECK(!IsProxyMethod());
287 const DexFile* dex_file = GetDexFile();
Vladimir Marko666ee3d2017-12-11 18:37:36 +0000288 return dex_file->GetTypeDescriptor(dex_file->GetTypeId(GetReturnTypeIndex()));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700289}
290
Alex Lightd7661582017-05-01 13:48:16 -0700291inline Primitive::Type ArtMethod::GetReturnTypePrimitive() {
292 return Primitive::GetType(GetReturnTypeDescriptor()[0]);
293}
294
Andreas Gampea5b09a62016-11-17 15:21:22 -0800295inline const char* ArtMethod::GetTypeDescriptorFromTypeIdx(dex::TypeIndex type_idx) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700296 DCHECK(!IsProxyMethod());
297 const DexFile* dex_file = GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700298 return dex_file->GetTypeDescriptor(dex_file->GetTypeId(type_idx));
299}
300
Vladimir Markoc524e9e2019-03-26 10:54:50 +0000301inline ObjPtr<mirror::ClassLoader> ArtMethod::GetClassLoader() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700302 DCHECK(!IsProxyMethod());
303 return GetDeclaringClass()->GetClassLoader();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700304}
305
Mathieu Chartier137cdfa2017-01-26 14:03:11 -0800306template <ReadBarrierOption kReadBarrierOption>
Vladimir Markoc524e9e2019-03-26 10:54:50 +0000307inline ObjPtr<mirror::DexCache> ArtMethod::GetDexCache() {
Vladimir Markoc945e0d2018-07-18 17:26:45 +0100308 if (LIKELY(!IsObsolete())) {
Vladimir Marko09c5ca42018-05-31 15:15:31 +0100309 ObjPtr<mirror::Class> klass = GetDeclaringClass<kReadBarrierOption>();
Mathieu Chartier137cdfa2017-01-26 14:03:11 -0800310 return klass->GetDexCache<kDefaultVerifyFlags, kReadBarrierOption>();
Alex Lightdba61482016-12-21 08:20:29 -0800311 } else {
312 DCHECK(!IsProxyMethod());
313 return GetObsoleteDexCache();
Alex Lighta01de592016-11-15 10:43:06 -0800314 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700315}
316
Ian Rogers03b6eaf2014-10-28 09:34:57 -0700317inline bool ArtMethod::IsProxyMethod() {
Roland Levillainfa854e42018-02-07 13:09:55 +0000318 DCHECK(!IsRuntimeMethod()) << "ArtMethod::IsProxyMethod called on a runtime method";
Vladimir Marko415ac852019-03-21 12:49:10 +0000319 // No read barrier needed, we're reading the constant declaring class only to read
320 // the constant proxy flag. See ReadBarrierOption.
Mathieu Chartier90c5a9b2017-02-01 13:10:06 -0800321 return GetDeclaringClass<kWithoutReadBarrier>()->IsProxyClass();
Ian Rogers03b6eaf2014-10-28 09:34:57 -0700322}
323
Vladimir Markod1ee20f2017-08-17 09:21:16 +0000324inline ArtMethod* ArtMethod::GetInterfaceMethodForProxyUnchecked(PointerSize pointer_size) {
325 DCHECK(IsProxyMethod());
326 // Do not check IsAssignableFrom() here as it relies on raw reference comparison
327 // which may give false negatives while visiting references for a non-CC moving GC.
328 return reinterpret_cast<ArtMethod*>(GetDataPtrSize(pointer_size));
329}
330
Andreas Gampe542451c2016-07-26 09:02:02 -0700331inline ArtMethod* ArtMethod::GetInterfaceMethodIfProxy(PointerSize pointer_size) {
Ian Rogers03b6eaf2014-10-28 09:34:57 -0700332 if (LIKELY(!IsProxyMethod())) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700333 return this;
334 }
Vladimir Markod1ee20f2017-08-17 09:21:16 +0000335 ArtMethod* interface_method = GetInterfaceMethodForProxyUnchecked(pointer_size);
336 // We can check that the proxy class implements the interface only if the proxy class
337 // is resolved, otherwise the interface table is not yet initialized.
338 DCHECK(!GetDeclaringClass()->IsResolved() ||
339 interface_method->GetDeclaringClass()->IsAssignableFrom(GetDeclaringClass()));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700340 return interface_method;
341}
342
Vladimir Markob45528c2017-07-27 14:14:28 +0100343inline dex::TypeIndex ArtMethod::GetReturnTypeIndex() {
Ian Rogersded66a02014-10-28 18:12:55 -0700344 DCHECK(!IsProxyMethod());
345 const DexFile* dex_file = GetDexFile();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800346 const dex::MethodId& method_id = dex_file->GetMethodId(GetDexMethodIndex());
347 const dex::ProtoId& proto_id = dex_file->GetMethodPrototype(method_id);
Vladimir Markob45528c2017-07-27 14:14:28 +0100348 return proto_id.return_type_idx_;
349}
350
351inline ObjPtr<mirror::Class> ArtMethod::LookupResolvedReturnType() {
352 return LookupResolvedClassFromTypeIndex(GetReturnTypeIndex());
353}
354
355inline ObjPtr<mirror::Class> ArtMethod::ResolveReturnType() {
356 return ResolveClassFromTypeIndex(GetReturnTypeIndex());
Ian Rogersded66a02014-10-28 18:12:55 -0700357}
358
Alexey Grebenkinbe4c2bd2018-02-01 19:09:59 +0300359template <ReadBarrierOption kReadBarrierOption>
Mingyao Yang063fc772016-08-02 11:02:54 -0700360inline bool ArtMethod::HasSingleImplementation() {
Vladimir Markoc945e0d2018-07-18 17:26:45 +0100361 if (IsFinal() || GetDeclaringClass<kReadBarrierOption>()->IsFinal()) {
Mingyao Yang063fc772016-08-02 11:02:54 -0700362 // We don't set kAccSingleImplementation for these cases since intrinsic
363 // can use the flag also.
364 return true;
365 }
Vladimir Markoc945e0d2018-07-18 17:26:45 +0100366 return (GetAccessFlags() & kAccSingleImplementation) != 0;
Mingyao Yang063fc772016-08-02 11:02:54 -0700367}
368
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -0700369template<ReadBarrierOption kReadBarrierOption, typename RootVisitorType>
Andreas Gampe542451c2016-07-26 09:02:02 -0700370void ArtMethod::VisitRoots(RootVisitorType& visitor, PointerSize pointer_size) {
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -0700371 if (LIKELY(!declaring_class_.IsNull())) {
372 visitor.VisitRoot(declaring_class_.AddressWithoutBarrier());
Vladimir Markod93e3742018-07-18 10:58:13 +0100373 ObjPtr<mirror::Class> klass = declaring_class_.Read<kReadBarrierOption>();
Nicolas Geoffraydec3a122016-02-13 12:38:36 +0000374 if (UNLIKELY(klass->IsProxyClass())) {
375 // For normal methods, dex cache shortcuts will be visited through the declaring class.
376 // However, for proxies we need to keep the interface method alive, so we visit its roots.
Vladimir Markod1ee20f2017-08-17 09:21:16 +0000377 ArtMethod* interface_method = GetInterfaceMethodForProxyUnchecked(pointer_size);
Nicolas Geoffraydec3a122016-02-13 12:38:36 +0000378 DCHECK(interface_method != nullptr);
Nicolas Geoffraydec3a122016-02-13 12:38:36 +0000379 interface_method->VisitRoots(visitor, pointer_size);
380 }
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100381 }
Mathieu Chartier2d721012014-11-10 11:08:06 -0800382}
383
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800384template <typename Visitor>
Vladimir Marko5122e6b2017-08-17 16:10:09 +0100385inline void ArtMethod::UpdateObjectsForImageRelocation(const Visitor& visitor) {
Vladimir Markod93e3742018-07-18 10:58:13 +0100386 ObjPtr<mirror::Class> old_class = GetDeclaringClassUnchecked<kWithoutReadBarrier>();
387 ObjPtr<mirror::Class> new_class = visitor(old_class.Ptr());
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800388 if (old_class != new_class) {
389 SetDeclaringClass(new_class);
390 }
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800391}
392
Vladimir Markoc945e0d2018-07-18 17:26:45 +0100393template <typename Visitor>
Andreas Gampe542451c2016-07-26 09:02:02 -0700394inline void ArtMethod::UpdateEntrypoints(const Visitor& visitor, PointerSize pointer_size) {
Vladimir Markoc945e0d2018-07-18 17:26:45 +0100395 if (IsNative()) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800396 const void* old_native_code = GetEntryPointFromJniPtrSize(pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800397 const void* new_native_code = visitor(old_native_code);
398 if (old_native_code != new_native_code) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800399 SetEntryPointFromJniPtrSize(new_native_code, pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800400 }
401 } else {
Andreas Gampe75f08852016-07-19 08:06:07 -0700402 DCHECK(GetDataPtrSize(pointer_size) == nullptr);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800403 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800404 const void* old_code = GetEntryPointFromQuickCompiledCodePtrSize(pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800405 const void* new_code = visitor(old_code);
406 if (old_code != new_code) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800407 SetEntryPointFromQuickCompiledCodePtrSize(new_code, pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800408 }
409}
410
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800411inline CodeItemInstructionAccessor ArtMethod::DexInstructions() {
David Sehr0225f8e2018-01-31 08:52:24 +0000412 return CodeItemInstructionAccessor(*GetDexFile(), GetCodeItem());
413}
414
415inline CodeItemDataAccessor ArtMethod::DexInstructionData() {
416 return CodeItemDataAccessor(*GetDexFile(), GetCodeItem());
417}
418
419inline CodeItemDebugInfoAccessor ArtMethod::DexInstructionDebugInfo() {
420 return CodeItemDebugInfoAccessor(*GetDexFile(), GetCodeItem(), GetDexMethodIndex());
Mathieu Chartier69147f12017-11-06 20:02:24 -0800421}
422
David Srbeckye36e7f22018-11-14 14:21:23 +0000423inline void ArtMethod::SetCounter(int16_t hotness_count) {
424 DCHECK(!IsAbstract()) << PrettyMethod();
425 hotness_count_ = hotness_count;
426}
427
428inline uint16_t ArtMethod::GetCounter() {
429 DCHECK(!IsAbstract()) << PrettyMethod();
430 return hotness_count_;
431}
432
433inline uint32_t ArtMethod::GetImtIndex() {
434 if (LIKELY(IsAbstract() && imt_index_ != 0)) {
435 uint16_t imt_index = ~imt_index_;
436 DCHECK_EQ(imt_index, ImTable::GetImtIndex(this)) << PrettyMethod();
437 return imt_index;
438 } else {
439 return ImTable::GetImtIndex(this);
440 }
441}
442
443inline void ArtMethod::CalculateAndSetImtIndex() {
444 DCHECK(IsAbstract()) << PrettyMethod();
445 imt_index_ = ~ImTable::GetImtIndex(this);
446}
447
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800448} // namespace art
449
Mathieu Chartiere401d142015-04-22 13:56:20 -0700450#endif // ART_RUNTIME_ART_METHOD_INL_H_