blob: c24001790048ad89450c9020fc238521ec8fe6a0 [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"
David Sehrc431b9d2018-03-02 12:01:51 -080024#include "base/utils.h"
Hiroshi Yamauchi00370822015-08-18 14:47:25 -070025#include "class_linker-inl.h"
Vladimir Marko05792b92015-08-03 11:56:49 +010026#include "common_throws.h"
David Sehr9e734c72018-01-04 17:56:19 -080027#include "dex/code_item_accessors-inl.h"
28#include "dex/dex_file-inl.h"
29#include "dex/dex_file_annotations.h"
30#include "dex/dex_file_types.h"
David Sehr8c0961f2018-01-23 16:11:38 -080031#include "dex/invoke_type.h"
David Sehr67bf42e2018-02-26 16:43:04 -080032#include "dex/primitive.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"
Mathieu Chartier0795f232016-09-27 18:43:30 -070047#include "scoped_thread_state_change-inl.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070048#include "thread-current-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080049
50namespace art {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080051
Mathieu Chartiere7f75f32016-02-01 16:08:15 -080052template <ReadBarrierOption kReadBarrierOption>
Vladimir Markod93e3742018-07-18 10:58:13 +010053inline ObjPtr<mirror::Class> ArtMethod::GetDeclaringClassUnchecked() {
Hiroshi Yamauchi3f64f252015-06-12 18:35:06 -070054 GcRootSource gc_root_source(this);
Mathieu Chartiere7f75f32016-02-01 16:08:15 -080055 return declaring_class_.Read<kReadBarrierOption>(&gc_root_source);
Mingyao Yang98d1cc82014-05-15 17:02:16 -070056}
57
Mathieu Chartiere7f75f32016-02-01 16:08:15 -080058template <ReadBarrierOption kReadBarrierOption>
Vladimir Markod93e3742018-07-18 10:58:13 +010059inline ObjPtr<mirror::Class> ArtMethod::GetDeclaringClass() {
60 ObjPtr<mirror::Class> result = GetDeclaringClassUnchecked<kReadBarrierOption>();
Mathieu Chartiere401d142015-04-22 13:56:20 -070061 if (kIsDebugBuild) {
62 if (!IsRuntimeMethod()) {
63 CHECK(result != nullptr) << this;
Mathieu Chartiere401d142015-04-22 13:56:20 -070064 } else {
65 CHECK(result == nullptr) << this;
66 }
67 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080068 return result;
69}
70
Mathieu Chartier28357fa2016-10-18 16:27:40 -070071inline void ArtMethod::SetDeclaringClass(ObjPtr<mirror::Class> new_declaring_class) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070072 declaring_class_ = GcRoot<mirror::Class>(new_declaring_class);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080073}
74
Vladimir Markod93e3742018-07-18 10:58:13 +010075inline bool ArtMethod::CASDeclaringClass(ObjPtr<mirror::Class> expected_class,
76 ObjPtr<mirror::Class> desired_class) {
Mathieu Chartier10e5ea92015-08-13 12:56:31 -070077 GcRoot<mirror::Class> expected_root(expected_class);
78 GcRoot<mirror::Class> desired_root(desired_class);
Orion Hodson4557b382018-01-03 11:47:54 +000079 auto atomic_root_class = reinterpret_cast<Atomic<GcRoot<mirror::Class>>*>(&declaring_class_);
80 return atomic_root_class->CompareAndSetStrongSequentiallyConsistent(expected_root, desired_root);
Mathieu Chartier10e5ea92015-08-13 12:56:31 -070081}
82
Ian Rogersef7d42f2014-01-06 12:55:46 -080083inline uint16_t ArtMethod::GetMethodIndex() {
Vladimir Marko72ab6842017-01-20 19:32:50 +000084 DCHECK(IsRuntimeMethod() || GetDeclaringClass()->IsResolved());
Mathieu Chartiere401d142015-04-22 13:56:20 -070085 return method_index_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080086}
87
Mathieu Chartier9f3629d2014-10-28 18:23:02 -070088inline uint16_t ArtMethod::GetMethodIndexDuringLinking() {
Mathieu Chartiere401d142015-04-22 13:56:20 -070089 return method_index_;
Mathieu Chartier9f3629d2014-10-28 18:23:02 -070090}
91
Vladimir Markob45528c2017-07-27 14:14:28 +010092inline ObjPtr<mirror::Class> ArtMethod::LookupResolvedClassFromTypeIndex(dex::TypeIndex type_idx) {
Vladimir Marko4098a7a2017-11-06 16:00:51 +000093 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Vladimir Marko666ee3d2017-12-11 18:37:36 +000094 ObjPtr<mirror::Class> type =
95 Runtime::Current()->GetClassLinker()->LookupResolvedType(type_idx, this);
96 DCHECK(!Thread::Current()->IsExceptionPending());
97 return type;
Vladimir Markob45528c2017-07-27 14:14:28 +010098}
99
100inline ObjPtr<mirror::Class> ArtMethod::ResolveClassFromTypeIndex(dex::TypeIndex type_idx) {
Vladimir Marko666ee3d2017-12-11 18:37:36 +0000101 ObjPtr<mirror::Class> type = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, this);
102 DCHECK_EQ(type == nullptr, Thread::Current()->IsExceptionPending());
103 return type;
Ian Rogersa0485602014-12-02 15:48:04 -0800104}
105
Brian Carlstromea46f952013-07-30 01:26:50 -0700106inline bool ArtMethod::CheckIncompatibleClassChange(InvokeType type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800107 switch (type) {
108 case kStatic:
109 return !IsStatic();
110 case kDirect:
111 return !IsDirect() || IsStatic();
112 case kVirtual: {
Alex Lightd6e0fa92016-10-17 13:02:39 -0700113 // We have an error if we are direct or a non-copied (i.e. not part of a real class) interface
114 // method.
Vladimir Markod93e3742018-07-18 10:58:13 +0100115 ObjPtr<mirror::Class> methods_class = GetDeclaringClass();
Alex Lightd6e0fa92016-10-17 13:02:39 -0700116 return IsDirect() || (methods_class->IsInterface() && !IsCopied());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800117 }
118 case kSuper:
Andreas Gampe8f252e62014-08-25 20:46:31 -0700119 // Constructors and static methods are called with invoke-direct.
Alex Light705ad492015-09-21 11:36:30 -0700120 return IsConstructor() || IsStatic();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800121 case kInterface: {
Vladimir Markod93e3742018-07-18 10:58:13 +0100122 ObjPtr<mirror::Class> methods_class = GetDeclaringClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800123 return IsDirect() || !(methods_class->IsInterface() || methods_class->IsObjectClass());
124 }
125 default:
126 LOG(FATAL) << "Unreachable - invocation type: " << type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700127 UNREACHABLE();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800128 }
129}
130
Ian Rogersef7d42f2014-01-06 12:55:46 -0800131inline bool ArtMethod::IsCalleeSaveMethod() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800132 if (!IsRuntimeMethod()) {
133 return false;
134 }
135 Runtime* runtime = Runtime::Current();
136 bool result = false;
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700137 for (uint32_t i = 0; i < static_cast<uint32_t>(CalleeSaveType::kLastCalleeSaveType); i++) {
138 if (this == runtime->GetCalleeSaveMethod(CalleeSaveType(i))) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800139 result = true;
140 break;
141 }
142 }
143 return result;
144}
145
Ian Rogersef7d42f2014-01-06 12:55:46 -0800146inline bool ArtMethod::IsResolutionMethod() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800147 bool result = this == Runtime::Current()->GetResolutionMethod();
148 // Check that if we do think it is phony it looks like the resolution method.
149 DCHECK(!result || IsRuntimeMethod());
150 return result;
151}
Jeff Hao88474b42013-10-23 16:24:40 -0700152
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700153inline bool ArtMethod::IsImtUnimplementedMethod() {
154 bool result = this == Runtime::Current()->GetImtUnimplementedMethod();
155 // Check that if we do think it is phony it looks like the imt unimplemented method.
156 DCHECK(!result || IsRuntimeMethod());
157 return result;
158}
159
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700160inline const DexFile* ArtMethod::GetDexFile() {
Mathieu Chartier137cdfa2017-01-26 14:03:11 -0800161 // It is safe to avoid the read barrier here since the dex file is constant, so if we read the
162 // from-space dex file pointer it will be equal to the to-space copy.
163 return GetDexCache<kWithoutReadBarrier>()->GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700164}
165
166inline const char* ArtMethod::GetDeclaringClassDescriptor() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700167 uint32_t dex_method_idx = GetDexMethodIndex();
Andreas Gampee2abbc62017-09-15 11:59:26 -0700168 if (UNLIKELY(dex_method_idx == dex::kDexNoIndex)) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700169 return "<runtime method>";
170 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700171 DCHECK(!IsProxyMethod());
172 const DexFile* dex_file = GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700173 return dex_file->GetMethodDeclaringClassDescriptor(dex_file->GetMethodId(dex_method_idx));
174}
175
Andreas Gampec6ea7d02017-02-01 16:46:28 -0800176inline const char* ArtMethod::GetShorty() {
177 uint32_t unused_length;
178 return GetShorty(&unused_length);
179}
180
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700181inline const char* ArtMethod::GetShorty(uint32_t* out_length) {
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000182 DCHECK(!IsProxyMethod());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700183 const DexFile* dex_file = GetDexFile();
Vladimir Markoc945e0d2018-07-18 17:26:45 +0100184 return dex_file->GetMethodShorty(dex_file->GetMethodId(GetDexMethodIndex()), out_length);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700185}
186
187inline const Signature ArtMethod::GetSignature() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700188 uint32_t dex_method_idx = GetDexMethodIndex();
Andreas Gampee2abbc62017-09-15 11:59:26 -0700189 if (dex_method_idx != dex::kDexNoIndex) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700190 DCHECK(!IsProxyMethod());
191 const DexFile* dex_file = GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700192 return dex_file->GetMethodSignature(dex_file->GetMethodId(dex_method_idx));
193 }
194 return Signature::NoSignature();
195}
196
Ian Rogers1ff3c982014-08-12 02:30:58 -0700197inline const char* ArtMethod::GetName() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700198 uint32_t dex_method_idx = GetDexMethodIndex();
Andreas Gampee2abbc62017-09-15 11:59:26 -0700199 if (LIKELY(dex_method_idx != dex::kDexNoIndex)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700200 DCHECK(!IsProxyMethod());
201 const DexFile* dex_file = GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700202 return dex_file->GetMethodName(dex_file->GetMethodId(dex_method_idx));
203 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700204 Runtime* const runtime = Runtime::Current();
205 if (this == runtime->GetResolutionMethod()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700206 return "<runtime internal resolution method>";
Mathieu Chartiere401d142015-04-22 13:56:20 -0700207 } else if (this == runtime->GetImtConflictMethod()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700208 return "<runtime internal imt conflict method>";
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700209 } else if (this == runtime->GetCalleeSaveMethod(CalleeSaveType::kSaveAllCalleeSaves)) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700210 return "<runtime internal callee-save all registers method>";
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700211 } else if (this == runtime->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsOnly)) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700212 return "<runtime internal callee-save reference registers method>";
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700213 } else if (this == runtime->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs)) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700214 return "<runtime internal callee-save reference and argument registers method>";
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700215 } else if (this == runtime->GetCalleeSaveMethod(CalleeSaveType::kSaveEverything)) {
216 return "<runtime internal save-every-register method>";
Mingyao Yang0a87a652017-04-12 13:43:15 -0700217 } else if (this == runtime->GetCalleeSaveMethod(CalleeSaveType::kSaveEverythingForClinit)) {
218 return "<runtime internal save-every-register method for clinit>";
219 } else if (this == runtime->GetCalleeSaveMethod(CalleeSaveType::kSaveEverythingForSuspendCheck)) {
220 return "<runtime internal save-every-register method for suspend check>";
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700221 } else {
222 return "<unknown runtime internal method>";
223 }
224}
225
Vladimir Marko18090d12018-06-01 16:53:12 +0100226inline ObjPtr<mirror::String> ArtMethod::ResolveNameString() {
227 DCHECK(!IsProxyMethod());
228 const DexFile::MethodId& method_id = GetDexFile()->GetMethodId(GetDexMethodIndex());
229 return Runtime::Current()->GetClassLinker()->ResolveString(method_id.name_idx_, this);
230}
231
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700232inline const DexFile::CodeItem* ArtMethod::GetCodeItem() {
Alex Lightdba61482016-12-21 08:20:29 -0800233 return GetDexFile()->GetCodeItem(GetCodeItemOffset());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700234}
235
Vladimir Marko942fd312017-01-16 20:52:19 +0000236inline bool ArtMethod::IsResolvedTypeIdx(dex::TypeIndex type_idx) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700237 DCHECK(!IsProxyMethod());
Vladimir Markob45528c2017-07-27 14:14:28 +0100238 return LookupResolvedClassFromTypeIndex(type_idx) != nullptr;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700239}
240
241inline int32_t ArtMethod::GetLineNumFromDexPC(uint32_t dex_pc) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700242 DCHECK(!IsProxyMethod());
Andreas Gampee2abbc62017-09-15 11:59:26 -0700243 if (dex_pc == dex::kDexNoIndex) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700244 return IsNative() ? -2 : -1;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700245 }
David Sehr9323e6e2016-09-13 08:58:35 -0700246 return annotations::GetLineNumFromPC(GetDexFile(), this, dex_pc);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700247}
248
249inline const DexFile::ProtoId& ArtMethod::GetPrototype() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700250 DCHECK(!IsProxyMethod());
251 const DexFile* dex_file = GetDexFile();
252 return dex_file->GetMethodPrototype(dex_file->GetMethodId(GetDexMethodIndex()));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700253}
254
255inline const DexFile::TypeList* ArtMethod::GetParameterTypeList() {
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000256 DCHECK(!IsProxyMethod());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700257 const DexFile* dex_file = GetDexFile();
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000258 const DexFile::ProtoId& proto = dex_file->GetMethodPrototype(
259 dex_file->GetMethodId(GetDexMethodIndex()));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700260 return dex_file->GetProtoParameters(proto);
261}
262
263inline const char* ArtMethod::GetDeclaringClassSourceFile() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700264 DCHECK(!IsProxyMethod());
265 return GetDeclaringClass()->GetSourceFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700266}
267
268inline uint16_t ArtMethod::GetClassDefIndex() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700269 DCHECK(!IsProxyMethod());
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000270 if (LIKELY(!IsObsolete())) {
271 return GetDeclaringClass()->GetDexClassDefIndex();
272 } else {
273 return FindObsoleteDexClassDefIndex();
274 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700275}
276
277inline const DexFile::ClassDef& ArtMethod::GetClassDef() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700278 DCHECK(!IsProxyMethod());
279 return GetDexFile()->GetClassDef(GetClassDefIndex());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700280}
281
Orion Hodson58143d22018-02-20 08:44:20 +0000282inline size_t ArtMethod::GetNumberOfParameters() {
283 constexpr size_t return_type_count = 1u;
284 return strlen(GetShorty()) - return_type_count;
285}
286
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700287inline const char* ArtMethod::GetReturnTypeDescriptor() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700288 DCHECK(!IsProxyMethod());
289 const DexFile* dex_file = GetDexFile();
Vladimir Marko666ee3d2017-12-11 18:37:36 +0000290 return dex_file->GetTypeDescriptor(dex_file->GetTypeId(GetReturnTypeIndex()));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700291}
292
Alex Lightd7661582017-05-01 13:48:16 -0700293inline Primitive::Type ArtMethod::GetReturnTypePrimitive() {
294 return Primitive::GetType(GetReturnTypeDescriptor()[0]);
295}
296
Andreas Gampea5b09a62016-11-17 15:21:22 -0800297inline const char* ArtMethod::GetTypeDescriptorFromTypeIdx(dex::TypeIndex type_idx) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700298 DCHECK(!IsProxyMethod());
299 const DexFile* dex_file = GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700300 return dex_file->GetTypeDescriptor(dex_file->GetTypeId(type_idx));
301}
302
303inline mirror::ClassLoader* ArtMethod::GetClassLoader() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700304 DCHECK(!IsProxyMethod());
305 return GetDeclaringClass()->GetClassLoader();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700306}
307
Mathieu Chartier137cdfa2017-01-26 14:03:11 -0800308template <ReadBarrierOption kReadBarrierOption>
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700309inline mirror::DexCache* ArtMethod::GetDexCache() {
Vladimir Markoc945e0d2018-07-18 17:26:45 +0100310 if (LIKELY(!IsObsolete())) {
Vladimir Marko09c5ca42018-05-31 15:15:31 +0100311 ObjPtr<mirror::Class> klass = GetDeclaringClass<kReadBarrierOption>();
Mathieu Chartier137cdfa2017-01-26 14:03:11 -0800312 return klass->GetDexCache<kDefaultVerifyFlags, kReadBarrierOption>();
Alex Lightdba61482016-12-21 08:20:29 -0800313 } else {
314 DCHECK(!IsProxyMethod());
315 return GetObsoleteDexCache();
Alex Lighta01de592016-11-15 10:43:06 -0800316 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700317}
318
Ian Rogers03b6eaf2014-10-28 09:34:57 -0700319inline bool ArtMethod::IsProxyMethod() {
Roland Levillainfa854e42018-02-07 13:09:55 +0000320 DCHECK(!IsRuntimeMethod()) << "ArtMethod::IsProxyMethod called on a runtime method";
Mathieu Chartier90c5a9b2017-02-01 13:10:06 -0800321 // Avoid read barrier since the from-space version of the class will have the correct proxy class
322 // flags since they are constant for the lifetime of the class.
323 return GetDeclaringClass<kWithoutReadBarrier>()->IsProxyClass();
Ian Rogers03b6eaf2014-10-28 09:34:57 -0700324}
325
Vladimir Markod1ee20f2017-08-17 09:21:16 +0000326inline ArtMethod* ArtMethod::GetInterfaceMethodForProxyUnchecked(PointerSize pointer_size) {
327 DCHECK(IsProxyMethod());
328 // Do not check IsAssignableFrom() here as it relies on raw reference comparison
329 // which may give false negatives while visiting references for a non-CC moving GC.
330 return reinterpret_cast<ArtMethod*>(GetDataPtrSize(pointer_size));
331}
332
Andreas Gampe542451c2016-07-26 09:02:02 -0700333inline ArtMethod* ArtMethod::GetInterfaceMethodIfProxy(PointerSize pointer_size) {
Ian Rogers03b6eaf2014-10-28 09:34:57 -0700334 if (LIKELY(!IsProxyMethod())) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700335 return this;
336 }
Vladimir Markod1ee20f2017-08-17 09:21:16 +0000337 ArtMethod* interface_method = GetInterfaceMethodForProxyUnchecked(pointer_size);
338 // We can check that the proxy class implements the interface only if the proxy class
339 // is resolved, otherwise the interface table is not yet initialized.
340 DCHECK(!GetDeclaringClass()->IsResolved() ||
341 interface_method->GetDeclaringClass()->IsAssignableFrom(GetDeclaringClass()));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700342 return interface_method;
343}
344
Vladimir Markob45528c2017-07-27 14:14:28 +0100345inline dex::TypeIndex ArtMethod::GetReturnTypeIndex() {
Ian Rogersded66a02014-10-28 18:12:55 -0700346 DCHECK(!IsProxyMethod());
347 const DexFile* dex_file = GetDexFile();
348 const DexFile::MethodId& method_id = dex_file->GetMethodId(GetDexMethodIndex());
349 const DexFile::ProtoId& proto_id = dex_file->GetMethodPrototype(method_id);
Vladimir Markob45528c2017-07-27 14:14:28 +0100350 return proto_id.return_type_idx_;
351}
352
353inline ObjPtr<mirror::Class> ArtMethod::LookupResolvedReturnType() {
354 return LookupResolvedClassFromTypeIndex(GetReturnTypeIndex());
355}
356
357inline ObjPtr<mirror::Class> ArtMethod::ResolveReturnType() {
358 return ResolveClassFromTypeIndex(GetReturnTypeIndex());
Ian Rogersded66a02014-10-28 18:12:55 -0700359}
360
Alexey Grebenkinbe4c2bd2018-02-01 19:09:59 +0300361template <ReadBarrierOption kReadBarrierOption>
Mingyao Yang063fc772016-08-02 11:02:54 -0700362inline bool ArtMethod::HasSingleImplementation() {
Vladimir Markoc945e0d2018-07-18 17:26:45 +0100363 if (IsFinal() || GetDeclaringClass<kReadBarrierOption>()->IsFinal()) {
Mingyao Yang063fc772016-08-02 11:02:54 -0700364 // We don't set kAccSingleImplementation for these cases since intrinsic
365 // can use the flag also.
366 return true;
367 }
Vladimir Markoc945e0d2018-07-18 17:26:45 +0100368 return (GetAccessFlags() & kAccSingleImplementation) != 0;
Mingyao Yang063fc772016-08-02 11:02:54 -0700369}
370
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -0700371template<ReadBarrierOption kReadBarrierOption, typename RootVisitorType>
Andreas Gampe542451c2016-07-26 09:02:02 -0700372void ArtMethod::VisitRoots(RootVisitorType& visitor, PointerSize pointer_size) {
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -0700373 if (LIKELY(!declaring_class_.IsNull())) {
374 visitor.VisitRoot(declaring_class_.AddressWithoutBarrier());
Vladimir Markod93e3742018-07-18 10:58:13 +0100375 ObjPtr<mirror::Class> klass = declaring_class_.Read<kReadBarrierOption>();
Nicolas Geoffraydec3a122016-02-13 12:38:36 +0000376 if (UNLIKELY(klass->IsProxyClass())) {
377 // For normal methods, dex cache shortcuts will be visited through the declaring class.
378 // However, for proxies we need to keep the interface method alive, so we visit its roots.
Vladimir Markod1ee20f2017-08-17 09:21:16 +0000379 ArtMethod* interface_method = GetInterfaceMethodForProxyUnchecked(pointer_size);
Nicolas Geoffraydec3a122016-02-13 12:38:36 +0000380 DCHECK(interface_method != nullptr);
Nicolas Geoffraydec3a122016-02-13 12:38:36 +0000381 interface_method->VisitRoots(visitor, pointer_size);
382 }
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100383 }
Mathieu Chartier2d721012014-11-10 11:08:06 -0800384}
385
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800386template <typename Visitor>
Vladimir Marko5122e6b2017-08-17 16:10:09 +0100387inline void ArtMethod::UpdateObjectsForImageRelocation(const Visitor& visitor) {
Vladimir Markod93e3742018-07-18 10:58:13 +0100388 ObjPtr<mirror::Class> old_class = GetDeclaringClassUnchecked<kWithoutReadBarrier>();
389 ObjPtr<mirror::Class> new_class = visitor(old_class.Ptr());
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800390 if (old_class != new_class) {
391 SetDeclaringClass(new_class);
392 }
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800393}
394
Vladimir Markoc945e0d2018-07-18 17:26:45 +0100395template <typename Visitor>
Andreas Gampe542451c2016-07-26 09:02:02 -0700396inline void ArtMethod::UpdateEntrypoints(const Visitor& visitor, PointerSize pointer_size) {
Vladimir Markoc945e0d2018-07-18 17:26:45 +0100397 if (IsNative()) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800398 const void* old_native_code = GetEntryPointFromJniPtrSize(pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800399 const void* new_native_code = visitor(old_native_code);
400 if (old_native_code != new_native_code) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800401 SetEntryPointFromJniPtrSize(new_native_code, pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800402 }
403 } else {
Andreas Gampe75f08852016-07-19 08:06:07 -0700404 DCHECK(GetDataPtrSize(pointer_size) == nullptr);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800405 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800406 const void* old_code = GetEntryPointFromQuickCompiledCodePtrSize(pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800407 const void* new_code = visitor(old_code);
408 if (old_code != new_code) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800409 SetEntryPointFromQuickCompiledCodePtrSize(new_code, pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800410 }
411}
412
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800413inline CodeItemInstructionAccessor ArtMethod::DexInstructions() {
David Sehr0225f8e2018-01-31 08:52:24 +0000414 return CodeItemInstructionAccessor(*GetDexFile(), GetCodeItem());
415}
416
417inline CodeItemDataAccessor ArtMethod::DexInstructionData() {
418 return CodeItemDataAccessor(*GetDexFile(), GetCodeItem());
419}
420
421inline CodeItemDebugInfoAccessor ArtMethod::DexInstructionDebugInfo() {
422 return CodeItemDebugInfoAccessor(*GetDexFile(), GetCodeItem(), GetDexMethodIndex());
Mathieu Chartier69147f12017-11-06 20:02:24 -0800423}
424
David Srbeckye36e7f22018-11-14 14:21:23 +0000425inline void ArtMethod::SetCounter(int16_t hotness_count) {
426 DCHECK(!IsAbstract()) << PrettyMethod();
427 hotness_count_ = hotness_count;
428}
429
430inline uint16_t ArtMethod::GetCounter() {
431 DCHECK(!IsAbstract()) << PrettyMethod();
432 return hotness_count_;
433}
434
435inline uint32_t ArtMethod::GetImtIndex() {
436 if (LIKELY(IsAbstract() && imt_index_ != 0)) {
437 uint16_t imt_index = ~imt_index_;
438 DCHECK_EQ(imt_index, ImTable::GetImtIndex(this)) << PrettyMethod();
439 return imt_index;
440 } else {
441 return ImTable::GetImtIndex(this);
442 }
443}
444
445inline void ArtMethod::CalculateAndSetImtIndex() {
446 DCHECK(IsAbstract()) << PrettyMethod();
447 imt_index_ = ~ImTable::GetImtIndex(this);
448}
449
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800450} // namespace art
451
Mathieu Chartiere401d142015-04-22 13:56:20 -0700452#endif // ART_RUNTIME_ART_METHOD_INL_H_