blob: 61c261c52c9121028adcfc9df1c385d8d96f6d19 [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"
Andreas Gampeb79674c2019-05-14 16:16:46 -070032#include "dex/signature.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070033#include "gc_root-inl.h"
David Srbeckye36e7f22018-11-14 14:21:23 +000034#include "imtable-inl.h"
David Brazdil87144352018-01-24 12:50:01 +000035#include "intrinsics_enum.h"
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010036#include "jit/profiling_info.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070037#include "mirror/class-inl.h"
Vladimir Marko05792b92015-08-03 11:56:49 +010038#include "mirror/dex_cache-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070039#include "mirror/object-inl.h"
40#include "mirror/object_array.h"
Nicolas Geoffray4c0b4bc2017-03-17 13:08:26 +000041#include "mirror/string.h"
Vladimir Marko96c6ab92014-04-08 14:00:50 +010042#include "oat.h"
Mathieu Chartier28357fa2016-10-18 16:27:40 -070043#include "obj_ptr-inl.h"
Vladimir Marko7624d252014-05-02 14:40:15 +010044#include "quick/quick_method_frame_info.h"
Alex Lightd7661582017-05-01 13:48:16 -070045#include "read_barrier-inl.h"
Vladimir Marko7624d252014-05-02 14:40:15 +010046#include "runtime-inl.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070047#include "thread-current-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080048
49namespace art {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080050
Mathieu Chartiere7f75f32016-02-01 16:08:15 -080051template <ReadBarrierOption kReadBarrierOption>
Vladimir Markod93e3742018-07-18 10:58:13 +010052inline ObjPtr<mirror::Class> ArtMethod::GetDeclaringClassUnchecked() {
Hiroshi Yamauchi3f64f252015-06-12 18:35:06 -070053 GcRootSource gc_root_source(this);
Mathieu Chartiere7f75f32016-02-01 16:08:15 -080054 return declaring_class_.Read<kReadBarrierOption>(&gc_root_source);
Mingyao Yang98d1cc82014-05-15 17:02:16 -070055}
56
Mathieu Chartiere7f75f32016-02-01 16:08:15 -080057template <ReadBarrierOption kReadBarrierOption>
Vladimir Markod93e3742018-07-18 10:58:13 +010058inline ObjPtr<mirror::Class> ArtMethod::GetDeclaringClass() {
59 ObjPtr<mirror::Class> result = GetDeclaringClassUnchecked<kReadBarrierOption>();
Mathieu Chartiere401d142015-04-22 13:56:20 -070060 if (kIsDebugBuild) {
61 if (!IsRuntimeMethod()) {
62 CHECK(result != nullptr) << this;
Mathieu Chartiere401d142015-04-22 13:56:20 -070063 } else {
64 CHECK(result == nullptr) << this;
65 }
66 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080067 return result;
68}
69
Mathieu Chartier28357fa2016-10-18 16:27:40 -070070inline void ArtMethod::SetDeclaringClass(ObjPtr<mirror::Class> new_declaring_class) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070071 declaring_class_ = GcRoot<mirror::Class>(new_declaring_class);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080072}
73
Vladimir Markod93e3742018-07-18 10:58:13 +010074inline bool ArtMethod::CASDeclaringClass(ObjPtr<mirror::Class> expected_class,
75 ObjPtr<mirror::Class> desired_class) {
Mathieu Chartier10e5ea92015-08-13 12:56:31 -070076 GcRoot<mirror::Class> expected_root(expected_class);
77 GcRoot<mirror::Class> desired_root(desired_class);
Orion Hodson4557b382018-01-03 11:47:54 +000078 auto atomic_root_class = reinterpret_cast<Atomic<GcRoot<mirror::Class>>*>(&declaring_class_);
79 return atomic_root_class->CompareAndSetStrongSequentiallyConsistent(expected_root, desired_root);
Mathieu Chartier10e5ea92015-08-13 12:56:31 -070080}
81
Ian Rogersef7d42f2014-01-06 12:55:46 -080082inline uint16_t ArtMethod::GetMethodIndex() {
Vladimir Marko72ab6842017-01-20 19:32:50 +000083 DCHECK(IsRuntimeMethod() || GetDeclaringClass()->IsResolved());
Mathieu Chartiere401d142015-04-22 13:56:20 -070084 return method_index_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080085}
86
Mathieu Chartier9f3629d2014-10-28 18:23:02 -070087inline uint16_t ArtMethod::GetMethodIndexDuringLinking() {
Mathieu Chartiere401d142015-04-22 13:56:20 -070088 return method_index_;
Mathieu Chartier9f3629d2014-10-28 18:23:02 -070089}
90
Vladimir Markob45528c2017-07-27 14:14:28 +010091inline ObjPtr<mirror::Class> ArtMethod::LookupResolvedClassFromTypeIndex(dex::TypeIndex type_idx) {
Vladimir Marko4098a7a2017-11-06 16:00:51 +000092 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Vladimir Marko666ee3d2017-12-11 18:37:36 +000093 ObjPtr<mirror::Class> type =
94 Runtime::Current()->GetClassLinker()->LookupResolvedType(type_idx, this);
95 DCHECK(!Thread::Current()->IsExceptionPending());
96 return type;
Vladimir Markob45528c2017-07-27 14:14:28 +010097}
98
99inline ObjPtr<mirror::Class> ArtMethod::ResolveClassFromTypeIndex(dex::TypeIndex type_idx) {
Vladimir Marko666ee3d2017-12-11 18:37:36 +0000100 ObjPtr<mirror::Class> type = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, this);
101 DCHECK_EQ(type == nullptr, Thread::Current()->IsExceptionPending());
102 return type;
Ian Rogersa0485602014-12-02 15:48:04 -0800103}
104
Brian Carlstromea46f952013-07-30 01:26:50 -0700105inline bool ArtMethod::CheckIncompatibleClassChange(InvokeType type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800106 switch (type) {
107 case kStatic:
108 return !IsStatic();
109 case kDirect:
110 return !IsDirect() || IsStatic();
111 case kVirtual: {
Alex Lightd6e0fa92016-10-17 13:02:39 -0700112 // We have an error if we are direct or a non-copied (i.e. not part of a real class) interface
113 // method.
Vladimir Markod93e3742018-07-18 10:58:13 +0100114 ObjPtr<mirror::Class> methods_class = GetDeclaringClass();
Alex Lightd6e0fa92016-10-17 13:02:39 -0700115 return IsDirect() || (methods_class->IsInterface() && !IsCopied());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800116 }
117 case kSuper:
Andreas Gampe8f252e62014-08-25 20:46:31 -0700118 // Constructors and static methods are called with invoke-direct.
Alex Light705ad492015-09-21 11:36:30 -0700119 return IsConstructor() || IsStatic();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800120 case kInterface: {
Vladimir Markod93e3742018-07-18 10:58:13 +0100121 ObjPtr<mirror::Class> methods_class = GetDeclaringClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800122 return IsDirect() || !(methods_class->IsInterface() || methods_class->IsObjectClass());
123 }
124 default:
125 LOG(FATAL) << "Unreachable - invocation type: " << type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700126 UNREACHABLE();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800127 }
128}
129
Ian Rogersef7d42f2014-01-06 12:55:46 -0800130inline bool ArtMethod::IsCalleeSaveMethod() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800131 if (!IsRuntimeMethod()) {
132 return false;
133 }
134 Runtime* runtime = Runtime::Current();
135 bool result = false;
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700136 for (uint32_t i = 0; i < static_cast<uint32_t>(CalleeSaveType::kLastCalleeSaveType); i++) {
137 if (this == runtime->GetCalleeSaveMethod(CalleeSaveType(i))) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800138 result = true;
139 break;
140 }
141 }
142 return result;
143}
144
Ian Rogersef7d42f2014-01-06 12:55:46 -0800145inline bool ArtMethod::IsResolutionMethod() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800146 bool result = this == Runtime::Current()->GetResolutionMethod();
147 // Check that if we do think it is phony it looks like the resolution method.
148 DCHECK(!result || IsRuntimeMethod());
149 return result;
150}
Jeff Hao88474b42013-10-23 16:24:40 -0700151
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700152inline bool ArtMethod::IsImtUnimplementedMethod() {
153 bool result = this == Runtime::Current()->GetImtUnimplementedMethod();
154 // Check that if we do think it is phony it looks like the imt unimplemented method.
155 DCHECK(!result || IsRuntimeMethod());
156 return result;
157}
158
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700159inline const DexFile* ArtMethod::GetDexFile() {
Mathieu Chartier137cdfa2017-01-26 14:03:11 -0800160 // It is safe to avoid the read barrier here since the dex file is constant, so if we read the
161 // from-space dex file pointer it will be equal to the to-space copy.
162 return GetDexCache<kWithoutReadBarrier>()->GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700163}
164
165inline const char* ArtMethod::GetDeclaringClassDescriptor() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700166 uint32_t dex_method_idx = GetDexMethodIndex();
Andreas Gampee2abbc62017-09-15 11:59:26 -0700167 if (UNLIKELY(dex_method_idx == dex::kDexNoIndex)) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700168 return "<runtime method>";
169 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700170 DCHECK(!IsProxyMethod());
171 const DexFile* dex_file = GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700172 return dex_file->GetMethodDeclaringClassDescriptor(dex_file->GetMethodId(dex_method_idx));
173}
174
Andreas Gampec6ea7d02017-02-01 16:46:28 -0800175inline const char* ArtMethod::GetShorty() {
176 uint32_t unused_length;
177 return GetShorty(&unused_length);
178}
179
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700180inline const char* ArtMethod::GetShorty(uint32_t* out_length) {
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000181 DCHECK(!IsProxyMethod());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700182 const DexFile* dex_file = GetDexFile();
Vladimir Markoc945e0d2018-07-18 17:26:45 +0100183 return dex_file->GetMethodShorty(dex_file->GetMethodId(GetDexMethodIndex()), out_length);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700184}
185
186inline const Signature ArtMethod::GetSignature() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700187 uint32_t dex_method_idx = GetDexMethodIndex();
Andreas Gampee2abbc62017-09-15 11:59:26 -0700188 if (dex_method_idx != dex::kDexNoIndex) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700189 DCHECK(!IsProxyMethod());
190 const DexFile* dex_file = GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700191 return dex_file->GetMethodSignature(dex_file->GetMethodId(dex_method_idx));
192 }
193 return Signature::NoSignature();
194}
195
Ian Rogers1ff3c982014-08-12 02:30:58 -0700196inline const char* ArtMethod::GetName() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700197 uint32_t dex_method_idx = GetDexMethodIndex();
Andreas Gampee2abbc62017-09-15 11:59:26 -0700198 if (LIKELY(dex_method_idx != dex::kDexNoIndex)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700199 DCHECK(!IsProxyMethod());
200 const DexFile* dex_file = GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700201 return dex_file->GetMethodName(dex_file->GetMethodId(dex_method_idx));
202 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700203 Runtime* const runtime = Runtime::Current();
204 if (this == runtime->GetResolutionMethod()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700205 return "<runtime internal resolution method>";
Mathieu Chartiere401d142015-04-22 13:56:20 -0700206 } else if (this == runtime->GetImtConflictMethod()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700207 return "<runtime internal imt conflict method>";
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700208 } else if (this == runtime->GetCalleeSaveMethod(CalleeSaveType::kSaveAllCalleeSaves)) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700209 return "<runtime internal callee-save all registers method>";
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700210 } else if (this == runtime->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsOnly)) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700211 return "<runtime internal callee-save reference registers method>";
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700212 } else if (this == runtime->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs)) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700213 return "<runtime internal callee-save reference and argument registers method>";
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700214 } else if (this == runtime->GetCalleeSaveMethod(CalleeSaveType::kSaveEverything)) {
215 return "<runtime internal save-every-register method>";
Mingyao Yang0a87a652017-04-12 13:43:15 -0700216 } else if (this == runtime->GetCalleeSaveMethod(CalleeSaveType::kSaveEverythingForClinit)) {
217 return "<runtime internal save-every-register method for clinit>";
218 } else if (this == runtime->GetCalleeSaveMethod(CalleeSaveType::kSaveEverythingForSuspendCheck)) {
219 return "<runtime internal save-every-register method for suspend check>";
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700220 } else {
221 return "<unknown runtime internal method>";
222 }
223}
224
Vladimir Marko18090d12018-06-01 16:53:12 +0100225inline ObjPtr<mirror::String> ArtMethod::ResolveNameString() {
226 DCHECK(!IsProxyMethod());
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800227 const dex::MethodId& method_id = GetDexFile()->GetMethodId(GetDexMethodIndex());
Vladimir Marko18090d12018-06-01 16:53:12 +0100228 return Runtime::Current()->GetClassLinker()->ResolveString(method_id.name_idx_, this);
229}
230
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800231inline const dex::CodeItem* ArtMethod::GetCodeItem() {
Alex Lightdba61482016-12-21 08:20:29 -0800232 return GetDexFile()->GetCodeItem(GetCodeItemOffset());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700233}
234
Vladimir Marko942fd312017-01-16 20:52:19 +0000235inline bool ArtMethod::IsResolvedTypeIdx(dex::TypeIndex type_idx) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700236 DCHECK(!IsProxyMethod());
Vladimir Markob45528c2017-07-27 14:14:28 +0100237 return LookupResolvedClassFromTypeIndex(type_idx) != nullptr;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700238}
239
240inline int32_t ArtMethod::GetLineNumFromDexPC(uint32_t dex_pc) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700241 DCHECK(!IsProxyMethod());
Andreas Gampee2abbc62017-09-15 11:59:26 -0700242 if (dex_pc == dex::kDexNoIndex) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700243 return IsNative() ? -2 : -1;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700244 }
David Sehr9323e6e2016-09-13 08:58:35 -0700245 return annotations::GetLineNumFromPC(GetDexFile(), this, dex_pc);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700246}
247
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800248inline const dex::ProtoId& ArtMethod::GetPrototype() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700249 DCHECK(!IsProxyMethod());
250 const DexFile* dex_file = GetDexFile();
251 return dex_file->GetMethodPrototype(dex_file->GetMethodId(GetDexMethodIndex()));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700252}
253
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800254inline const dex::TypeList* ArtMethod::GetParameterTypeList() {
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000255 DCHECK(!IsProxyMethod());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700256 const DexFile* dex_file = GetDexFile();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800257 const dex::ProtoId& proto = dex_file->GetMethodPrototype(
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000258 dex_file->GetMethodId(GetDexMethodIndex()));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700259 return dex_file->GetProtoParameters(proto);
260}
261
262inline const char* ArtMethod::GetDeclaringClassSourceFile() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700263 DCHECK(!IsProxyMethod());
264 return GetDeclaringClass()->GetSourceFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700265}
266
267inline uint16_t ArtMethod::GetClassDefIndex() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700268 DCHECK(!IsProxyMethod());
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000269 if (LIKELY(!IsObsolete())) {
270 return GetDeclaringClass()->GetDexClassDefIndex();
271 } else {
272 return FindObsoleteDexClassDefIndex();
273 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700274}
275
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800276inline const dex::ClassDef& ArtMethod::GetClassDef() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700277 DCHECK(!IsProxyMethod());
278 return GetDexFile()->GetClassDef(GetClassDefIndex());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700279}
280
Orion Hodson58143d22018-02-20 08:44:20 +0000281inline size_t ArtMethod::GetNumberOfParameters() {
282 constexpr size_t return_type_count = 1u;
283 return strlen(GetShorty()) - return_type_count;
284}
285
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700286inline const char* ArtMethod::GetReturnTypeDescriptor() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700287 DCHECK(!IsProxyMethod());
288 const DexFile* dex_file = GetDexFile();
Vladimir Marko666ee3d2017-12-11 18:37:36 +0000289 return dex_file->GetTypeDescriptor(dex_file->GetTypeId(GetReturnTypeIndex()));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700290}
291
Alex Lightd7661582017-05-01 13:48:16 -0700292inline Primitive::Type ArtMethod::GetReturnTypePrimitive() {
293 return Primitive::GetType(GetReturnTypeDescriptor()[0]);
294}
295
Andreas Gampea5b09a62016-11-17 15:21:22 -0800296inline const char* ArtMethod::GetTypeDescriptorFromTypeIdx(dex::TypeIndex type_idx) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700297 DCHECK(!IsProxyMethod());
298 const DexFile* dex_file = GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700299 return dex_file->GetTypeDescriptor(dex_file->GetTypeId(type_idx));
300}
301
Vladimir Markoc524e9e2019-03-26 10:54:50 +0000302inline ObjPtr<mirror::ClassLoader> ArtMethod::GetClassLoader() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700303 DCHECK(!IsProxyMethod());
304 return GetDeclaringClass()->GetClassLoader();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700305}
306
Mathieu Chartier137cdfa2017-01-26 14:03:11 -0800307template <ReadBarrierOption kReadBarrierOption>
Vladimir Markoc524e9e2019-03-26 10:54:50 +0000308inline ObjPtr<mirror::DexCache> ArtMethod::GetDexCache() {
Vladimir Markoc945e0d2018-07-18 17:26:45 +0100309 if (LIKELY(!IsObsolete())) {
Vladimir Marko09c5ca42018-05-31 15:15:31 +0100310 ObjPtr<mirror::Class> klass = GetDeclaringClass<kReadBarrierOption>();
Mathieu Chartier137cdfa2017-01-26 14:03:11 -0800311 return klass->GetDexCache<kDefaultVerifyFlags, kReadBarrierOption>();
Alex Lightdba61482016-12-21 08:20:29 -0800312 } else {
313 DCHECK(!IsProxyMethod());
314 return GetObsoleteDexCache();
Alex Lighta01de592016-11-15 10:43:06 -0800315 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700316}
317
Ian Rogers03b6eaf2014-10-28 09:34:57 -0700318inline bool ArtMethod::IsProxyMethod() {
Roland Levillainfa854e42018-02-07 13:09:55 +0000319 DCHECK(!IsRuntimeMethod()) << "ArtMethod::IsProxyMethod called on a runtime method";
Vladimir Marko415ac852019-03-21 12:49:10 +0000320 // No read barrier needed, we're reading the constant declaring class only to read
321 // the constant proxy flag. See ReadBarrierOption.
Mathieu Chartier90c5a9b2017-02-01 13:10:06 -0800322 return GetDeclaringClass<kWithoutReadBarrier>()->IsProxyClass();
Ian Rogers03b6eaf2014-10-28 09:34:57 -0700323}
324
Vladimir Markod1ee20f2017-08-17 09:21:16 +0000325inline ArtMethod* ArtMethod::GetInterfaceMethodForProxyUnchecked(PointerSize pointer_size) {
326 DCHECK(IsProxyMethod());
327 // Do not check IsAssignableFrom() here as it relies on raw reference comparison
328 // which may give false negatives while visiting references for a non-CC moving GC.
329 return reinterpret_cast<ArtMethod*>(GetDataPtrSize(pointer_size));
330}
331
Andreas Gampe542451c2016-07-26 09:02:02 -0700332inline ArtMethod* ArtMethod::GetInterfaceMethodIfProxy(PointerSize pointer_size) {
Ian Rogers03b6eaf2014-10-28 09:34:57 -0700333 if (LIKELY(!IsProxyMethod())) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700334 return this;
335 }
Vladimir Markod1ee20f2017-08-17 09:21:16 +0000336 ArtMethod* interface_method = GetInterfaceMethodForProxyUnchecked(pointer_size);
337 // We can check that the proxy class implements the interface only if the proxy class
338 // is resolved, otherwise the interface table is not yet initialized.
339 DCHECK(!GetDeclaringClass()->IsResolved() ||
340 interface_method->GetDeclaringClass()->IsAssignableFrom(GetDeclaringClass()));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700341 return interface_method;
342}
343
Vladimir Markob45528c2017-07-27 14:14:28 +0100344inline dex::TypeIndex ArtMethod::GetReturnTypeIndex() {
Ian Rogersded66a02014-10-28 18:12:55 -0700345 DCHECK(!IsProxyMethod());
346 const DexFile* dex_file = GetDexFile();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800347 const dex::MethodId& method_id = dex_file->GetMethodId(GetDexMethodIndex());
348 const dex::ProtoId& proto_id = dex_file->GetMethodPrototype(method_id);
Vladimir Markob45528c2017-07-27 14:14:28 +0100349 return proto_id.return_type_idx_;
350}
351
352inline ObjPtr<mirror::Class> ArtMethod::LookupResolvedReturnType() {
353 return LookupResolvedClassFromTypeIndex(GetReturnTypeIndex());
354}
355
356inline ObjPtr<mirror::Class> ArtMethod::ResolveReturnType() {
357 return ResolveClassFromTypeIndex(GetReturnTypeIndex());
Ian Rogersded66a02014-10-28 18:12:55 -0700358}
359
Alexey Grebenkinbe4c2bd2018-02-01 19:09:59 +0300360template <ReadBarrierOption kReadBarrierOption>
Mingyao Yang063fc772016-08-02 11:02:54 -0700361inline bool ArtMethod::HasSingleImplementation() {
Vladimir Markoc945e0d2018-07-18 17:26:45 +0100362 if (IsFinal() || GetDeclaringClass<kReadBarrierOption>()->IsFinal()) {
Mingyao Yang063fc772016-08-02 11:02:54 -0700363 // We don't set kAccSingleImplementation for these cases since intrinsic
364 // can use the flag also.
365 return true;
366 }
Vladimir Markoc945e0d2018-07-18 17:26:45 +0100367 return (GetAccessFlags() & kAccSingleImplementation) != 0;
Mingyao Yang063fc772016-08-02 11:02:54 -0700368}
369
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -0700370template<ReadBarrierOption kReadBarrierOption, typename RootVisitorType>
Andreas Gampe542451c2016-07-26 09:02:02 -0700371void ArtMethod::VisitRoots(RootVisitorType& visitor, PointerSize pointer_size) {
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -0700372 if (LIKELY(!declaring_class_.IsNull())) {
373 visitor.VisitRoot(declaring_class_.AddressWithoutBarrier());
Vladimir Markod93e3742018-07-18 10:58:13 +0100374 ObjPtr<mirror::Class> klass = declaring_class_.Read<kReadBarrierOption>();
Nicolas Geoffraydec3a122016-02-13 12:38:36 +0000375 if (UNLIKELY(klass->IsProxyClass())) {
376 // For normal methods, dex cache shortcuts will be visited through the declaring class.
377 // However, for proxies we need to keep the interface method alive, so we visit its roots.
Vladimir Markod1ee20f2017-08-17 09:21:16 +0000378 ArtMethod* interface_method = GetInterfaceMethodForProxyUnchecked(pointer_size);
Nicolas Geoffraydec3a122016-02-13 12:38:36 +0000379 DCHECK(interface_method != nullptr);
Nicolas Geoffraydec3a122016-02-13 12:38:36 +0000380 interface_method->VisitRoots(visitor, pointer_size);
381 }
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100382 }
Mathieu Chartier2d721012014-11-10 11:08:06 -0800383}
384
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800385template <typename Visitor>
Andreas Gampe542451c2016-07-26 09:02:02 -0700386inline void ArtMethod::UpdateEntrypoints(const Visitor& visitor, PointerSize pointer_size) {
Vladimir Markoc945e0d2018-07-18 17:26:45 +0100387 if (IsNative()) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800388 const void* old_native_code = GetEntryPointFromJniPtrSize(pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800389 const void* new_native_code = visitor(old_native_code);
390 if (old_native_code != new_native_code) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800391 SetEntryPointFromJniPtrSize(new_native_code, pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800392 }
393 } else {
Andreas Gampe75f08852016-07-19 08:06:07 -0700394 DCHECK(GetDataPtrSize(pointer_size) == nullptr);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800395 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800396 const void* old_code = GetEntryPointFromQuickCompiledCodePtrSize(pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800397 const void* new_code = visitor(old_code);
398 if (old_code != new_code) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800399 SetEntryPointFromQuickCompiledCodePtrSize(new_code, pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800400 }
401}
402
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800403inline CodeItemInstructionAccessor ArtMethod::DexInstructions() {
David Sehr0225f8e2018-01-31 08:52:24 +0000404 return CodeItemInstructionAccessor(*GetDexFile(), GetCodeItem());
405}
406
407inline CodeItemDataAccessor ArtMethod::DexInstructionData() {
408 return CodeItemDataAccessor(*GetDexFile(), GetCodeItem());
409}
410
411inline CodeItemDebugInfoAccessor ArtMethod::DexInstructionDebugInfo() {
412 return CodeItemDebugInfoAccessor(*GetDexFile(), GetCodeItem(), GetDexMethodIndex());
Mathieu Chartier69147f12017-11-06 20:02:24 -0800413}
414
David Srbeckye36e7f22018-11-14 14:21:23 +0000415inline void ArtMethod::SetCounter(int16_t hotness_count) {
416 DCHECK(!IsAbstract()) << PrettyMethod();
417 hotness_count_ = hotness_count;
418}
419
420inline uint16_t ArtMethod::GetCounter() {
421 DCHECK(!IsAbstract()) << PrettyMethod();
422 return hotness_count_;
423}
424
425inline uint32_t ArtMethod::GetImtIndex() {
426 if (LIKELY(IsAbstract() && imt_index_ != 0)) {
427 uint16_t imt_index = ~imt_index_;
428 DCHECK_EQ(imt_index, ImTable::GetImtIndex(this)) << PrettyMethod();
429 return imt_index;
430 } else {
431 return ImTable::GetImtIndex(this);
432 }
433}
434
435inline void ArtMethod::CalculateAndSetImtIndex() {
436 DCHECK(IsAbstract()) << PrettyMethod();
437 imt_index_ = ~ImTable::GetImtIndex(this);
438}
439
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800440} // namespace art
441
Mathieu Chartiere401d142015-04-22 13:56:20 -0700442#endif // ART_RUNTIME_ART_METHOD_INL_H_