blob: 297b540ab288008ae2f7115a18c2dd62e2f1ef84 [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 Gampe8228cdf2017-05-30 15:03:54 -070022#include "base/callee_save_type.h"
Hiroshi Yamauchi00370822015-08-18 14:47:25 -070023#include "class_linker-inl.h"
Vladimir Marko05792b92015-08-03 11:56:49 +010024#include "common_throws.h"
David Sehr9e734c72018-01-04 17:56:19 -080025#include "dex/code_item_accessors-inl.h"
26#include "dex/dex_file-inl.h"
27#include "dex/dex_file_annotations.h"
28#include "dex/dex_file_types.h"
David Sehr8c0961f2018-01-23 16:11:38 -080029#include "dex/invoke_type.h"
David Sehr67bf42e2018-02-26 16:43:04 -080030#include "dex/primitive.h"
Andreas Gampeb79674c2019-05-14 16:16:46 -070031#include "dex/signature.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"
Vladimir Marko1001c2e2024-01-30 16:54:20 +000034#include "jit/jit_options.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070035#include "mirror/class-inl.h"
Vladimir Marko05792b92015-08-03 11:56:49 +010036#include "mirror/dex_cache-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070037#include "mirror/object-inl.h"
38#include "mirror/object_array.h"
Nicolas Geoffray4c0b4bc2017-03-17 13:08:26 +000039#include "mirror/string.h"
Mathieu Chartier28357fa2016-10-18 16:27:40 -070040#include "obj_ptr-inl.h"
Vladimir Marko7624d252014-05-02 14:40:15 +010041#include "quick/quick_method_frame_info.h"
Alex Lightd7661582017-05-01 13:48:16 -070042#include "read_barrier-inl.h"
Vladimir Marko7624d252014-05-02 14:40:15 +010043#include "runtime-inl.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070044#include "thread-current-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080045
Dmitrii Ishcheikin91d4f8c2024-01-17 12:01:01 +000046namespace art HIDDEN {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080047
Vladimir Markoa3749b22022-11-02 13:42:54 +000048namespace detail {
49
50template <> struct ShortyTraits<'V'> {
51 using Type = void;
Stefano Cianciulli78f3c722023-05-16 10:32:54 +000052 static Type Get([[maybe_unused]] const JValue& value) {}
Vladimir Markoa3749b22022-11-02 13:42:54 +000053 // `kVRegCount` and `Set()` are not defined.
54};
55
56template <> struct ShortyTraits<'Z'> {
Vladimir Markoad659832022-11-21 15:08:14 +000057 // Despite using `uint8_t` for `boolean` in `JValue`, we shall use `bool` here.
58 using Type = bool;
59 static Type Get(const JValue& value) { return value.GetZ() != 0u; }
Vladimir Markoa3749b22022-11-02 13:42:54 +000060 static constexpr size_t kVRegCount = 1u;
Vladimir Markoad659832022-11-21 15:08:14 +000061 static void Set(uint32_t* args, Type value) { args[0] = static_cast<uint32_t>(value ? 1u : 0u); }
Vladimir Markoa3749b22022-11-02 13:42:54 +000062};
63
64template <> struct ShortyTraits<'B'> {
65 using Type = int8_t;
66 static Type Get(const JValue& value) { return value.GetB(); }
67 static constexpr size_t kVRegCount = 1u;
68 static void Set(uint32_t* args, Type value) { args[0] = static_cast<uint32_t>(value); }
69};
70
71template <> struct ShortyTraits<'C'> {
72 using Type = uint16_t;
73 static Type Get(const JValue& value) { return value.GetC(); }
74 static constexpr size_t kVRegCount = 1u;
75 static void Set(uint32_t* args, Type value) { args[0] = static_cast<uint32_t>(value); }
76};
77
78template <> struct ShortyTraits<'S'> {
79 using Type = int16_t;
80 static Type Get(const JValue& value) { return value.GetS(); }
81 static constexpr size_t kVRegCount = 1u;
82 static void Set(uint32_t* args, Type value) { args[0] = static_cast<uint32_t>(value); }
83};
84
85template <> struct ShortyTraits<'I'> {
86 using Type = int32_t;
87 static Type Get(const JValue& value) { return value.GetI(); }
88 static constexpr size_t kVRegCount = 1u;
89 static void Set(uint32_t* args, Type value) { args[0] = static_cast<uint32_t>(value); }
90};
91
92template <> struct ShortyTraits<'J'> {
93 using Type = int64_t;
94 static Type Get(const JValue& value) { return value.GetJ(); }
95 static constexpr size_t kVRegCount = 2u;
96 static void Set(uint32_t* args, Type value) {
97 // Little-endian representation.
98 args[0] = static_cast<uint32_t>(value);
99 args[1] = static_cast<uint32_t>(static_cast<uint64_t>(value) >> 32);
100 }
101};
102
103template <> struct ShortyTraits<'F'> {
104 using Type = float;
105 static Type Get(const JValue& value) { return value.GetF(); }
106 static constexpr size_t kVRegCount = 1u;
107 static void Set(uint32_t* args, Type value) { args[0] = bit_cast<uint32_t>(value); }
108};
109
110template <> struct ShortyTraits<'D'> {
111 using Type = double;
112 static Type Get(const JValue& value) { return value.GetD(); }
113 static constexpr size_t kVRegCount = 2u;
114 static void Set(uint32_t* args, Type value) {
115 // Little-endian representation.
116 uint64_t v = bit_cast<uint64_t>(value);
117 args[0] = static_cast<uint32_t>(v);
118 args[1] = static_cast<uint32_t>(v >> 32);
119 }
120};
121
122template <> struct ShortyTraits<'L'> {
123 using Type = ObjPtr<mirror::Object>;
124 static Type Get(const JValue& value) REQUIRES_SHARED(Locks::mutator_lock_) {
125 return value.GetL();
126 }
127 static constexpr size_t kVRegCount = 1u;
128 static void Set(uint32_t* args, Type value) REQUIRES_SHARED(Locks::mutator_lock_) {
129 args[0] = StackReference<mirror::Object>::FromMirrorPtr(value.Ptr()).AsVRegValue();
130 }
131};
132
133template <char... Shorty>
134constexpr auto MaterializeShorty() {
135 constexpr size_t kSize = std::size({Shorty...}) + 1u;
136 return std::array<char, kSize>{Shorty..., '\0'};
137}
138
139template <char... ArgType>
140constexpr size_t NumberOfVRegs() {
141 constexpr size_t kArgVRegCount[] = {
142 ShortyTraits<ArgType>::kVRegCount...
143 };
144 size_t sum = 0u;
145 for (size_t count : kArgVRegCount) {
146 sum += count;
147 }
148 return sum;
149}
150
151template <char... ArgType>
Stefano Cianciulli78f3c722023-05-16 10:32:54 +0000152inline ALWAYS_INLINE void FillVRegs([[maybe_unused]] uint32_t* vregs,
153 [[maybe_unused]] typename ShortyTraits<ArgType>::Type... args)
Vladimir Markoa3749b22022-11-02 13:42:54 +0000154 REQUIRES_SHARED(Locks::mutator_lock_) {}
155
156template <char FirstArgType, char... ArgType>
157inline ALWAYS_INLINE void FillVRegs(uint32_t* vregs,
158 typename ShortyTraits<FirstArgType>::Type first_arg,
159 typename ShortyTraits<ArgType>::Type... args)
160 REQUIRES_SHARED(Locks::mutator_lock_) {
161 ShortyTraits<FirstArgType>::Set(vregs, first_arg);
162 FillVRegs<ArgType...>(vregs + ShortyTraits<FirstArgType>::kVRegCount, args...);
163}
164
165template <char... ArgType>
166inline ALWAYS_INLINE auto MaterializeVRegs(typename ShortyTraits<ArgType>::Type... args)
167 REQUIRES_SHARED(Locks::mutator_lock_) {
168 constexpr size_t kNumVRegs = NumberOfVRegs<ArgType...>();
169 std::array<uint32_t, kNumVRegs> vregs;
170 FillVRegs<ArgType...>(vregs.data(), args...);
171 return vregs;
172}
173
174} // namespace detail
175
176template <char ReturnType, char... ArgType>
177inline typename detail::ShortyTraits<ReturnType>::Type
178ArtMethod::InvokeStatic(Thread* self, typename detail::ShortyTraits<ArgType>::Type... args) {
179 DCHECK(IsStatic());
Vladimir Markob9c73f82022-11-22 15:18:43 +0000180 DCHECK(GetDeclaringClass()->IsInitialized()); // Used only for initialized well-known classes.
Vladimir Markoa3749b22022-11-02 13:42:54 +0000181 JValue result;
182 constexpr auto shorty = detail::MaterializeShorty<ReturnType, ArgType...>();
183 auto vregs = detail::MaterializeVRegs<ArgType...>(args...);
Ryan Prichard2c0caed2023-07-27 00:26:46 -0700184 Invoke(self,
185 vregs.empty() ? nullptr : vregs.data(),
186 vregs.size() * sizeof(typename decltype(vregs)::value_type),
187 &result,
188 shorty.data());
Vladimir Markoa3749b22022-11-02 13:42:54 +0000189 return detail::ShortyTraits<ReturnType>::Get(result);
190}
191
192template <char ReturnType, char... ArgType>
193typename detail::ShortyTraits<ReturnType>::Type
194ArtMethod::InvokeInstance(Thread* self,
195 ObjPtr<mirror::Object> receiver,
196 typename detail::ShortyTraits<ArgType>::Type... args) {
Vladimir Markof7bd1fa2022-11-18 10:55:05 +0000197 DCHECK(!GetDeclaringClass()->IsInterface());
Vladimir Markoa3749b22022-11-02 13:42:54 +0000198 DCHECK(!IsStatic());
199 JValue result;
200 constexpr auto shorty = detail::MaterializeShorty<ReturnType, ArgType...>();
201 auto vregs = detail::MaterializeVRegs<'L', ArgType...>(receiver, args...);
Ryan Prichard2c0caed2023-07-27 00:26:46 -0700202 Invoke(self,
203 vregs.data(),
204 vregs.size() * sizeof(typename decltype(vregs)::value_type),
205 &result,
206 shorty.data());
Vladimir Markoa3749b22022-11-02 13:42:54 +0000207 return detail::ShortyTraits<ReturnType>::Get(result);
208}
209
Vladimir Markof7bd1fa2022-11-18 10:55:05 +0000210template <char ReturnType, char... ArgType>
211typename detail::ShortyTraits<ReturnType>::Type
212ArtMethod::InvokeFinal(Thread* self,
213 ObjPtr<mirror::Object> receiver,
214 typename detail::ShortyTraits<ArgType>::Type... args) {
215 DCHECK(!GetDeclaringClass()->IsInterface());
216 DCHECK(!IsStatic());
Vladimir Markoad659832022-11-21 15:08:14 +0000217 DCHECK(IsFinal() || GetDeclaringClass()->IsFinal());
Vladimir Markof7bd1fa2022-11-18 10:55:05 +0000218 DCHECK(receiver != nullptr);
219 return InvokeInstance<ReturnType, ArgType...>(self, receiver, args...);
220}
221
222template <char ReturnType, char... ArgType>
223typename detail::ShortyTraits<ReturnType>::Type
224ArtMethod::InvokeVirtual(Thread* self,
225 ObjPtr<mirror::Object> receiver,
226 typename detail::ShortyTraits<ArgType>::Type... args) {
227 DCHECK(!GetDeclaringClass()->IsInterface());
228 DCHECK(!IsStatic());
229 DCHECK(!IsFinal());
230 DCHECK(receiver != nullptr);
231 ArtMethod* target_method =
232 receiver->GetClass()->FindVirtualMethodForVirtual(this, kRuntimePointerSize);
233 DCHECK(target_method != nullptr);
234 return target_method->InvokeInstance<ReturnType, ArgType...>(self, receiver, args...);
235}
236
237template <char ReturnType, char... ArgType>
238typename detail::ShortyTraits<ReturnType>::Type
239ArtMethod::InvokeInterface(Thread* self,
240 ObjPtr<mirror::Object> receiver,
241 typename detail::ShortyTraits<ArgType>::Type... args) {
242 DCHECK(GetDeclaringClass()->IsInterface());
243 DCHECK(!IsStatic());
244 DCHECK(receiver != nullptr);
245 ArtMethod* target_method =
246 receiver->GetClass()->FindVirtualMethodForInterface(this, kRuntimePointerSize);
247 DCHECK(target_method != nullptr);
248 return target_method->InvokeInstance<ReturnType, ArgType...>(self, receiver, args...);
249}
250
Mathieu Chartiere7f75f32016-02-01 16:08:15 -0800251template <ReadBarrierOption kReadBarrierOption>
Vladimir Markod93e3742018-07-18 10:58:13 +0100252inline ObjPtr<mirror::Class> ArtMethod::GetDeclaringClassUnchecked() {
Hiroshi Yamauchi3f64f252015-06-12 18:35:06 -0700253 GcRootSource gc_root_source(this);
Mathieu Chartiere7f75f32016-02-01 16:08:15 -0800254 return declaring_class_.Read<kReadBarrierOption>(&gc_root_source);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700255}
256
Mathieu Chartiere7f75f32016-02-01 16:08:15 -0800257template <ReadBarrierOption kReadBarrierOption>
Vladimir Markod93e3742018-07-18 10:58:13 +0100258inline ObjPtr<mirror::Class> ArtMethod::GetDeclaringClass() {
259 ObjPtr<mirror::Class> result = GetDeclaringClassUnchecked<kReadBarrierOption>();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700260 if (kIsDebugBuild) {
261 if (!IsRuntimeMethod()) {
262 CHECK(result != nullptr) << this;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700263 } else {
264 CHECK(result == nullptr) << this;
265 }
266 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800267 return result;
268}
269
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700270inline void ArtMethod::SetDeclaringClass(ObjPtr<mirror::Class> new_declaring_class) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700271 declaring_class_ = GcRoot<mirror::Class>(new_declaring_class);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800272}
273
Vladimir Markod93e3742018-07-18 10:58:13 +0100274inline bool ArtMethod::CASDeclaringClass(ObjPtr<mirror::Class> expected_class,
275 ObjPtr<mirror::Class> desired_class) {
Mathieu Chartier10e5ea92015-08-13 12:56:31 -0700276 GcRoot<mirror::Class> expected_root(expected_class);
277 GcRoot<mirror::Class> desired_root(desired_class);
Orion Hodson4557b382018-01-03 11:47:54 +0000278 auto atomic_root_class = reinterpret_cast<Atomic<GcRoot<mirror::Class>>*>(&declaring_class_);
279 return atomic_root_class->CompareAndSetStrongSequentiallyConsistent(expected_root, desired_root);
Mathieu Chartier10e5ea92015-08-13 12:56:31 -0700280}
281
Ian Rogersef7d42f2014-01-06 12:55:46 -0800282inline uint16_t ArtMethod::GetMethodIndex() {
Vladimir Marko72ab6842017-01-20 19:32:50 +0000283 DCHECK(IsRuntimeMethod() || GetDeclaringClass()->IsResolved());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700284 return method_index_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800285}
286
Mathieu Chartier9f3629d2014-10-28 18:23:02 -0700287inline uint16_t ArtMethod::GetMethodIndexDuringLinking() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700288 return method_index_;
Mathieu Chartier9f3629d2014-10-28 18:23:02 -0700289}
290
Vladimir Markob45528c2017-07-27 14:14:28 +0100291inline ObjPtr<mirror::Class> ArtMethod::LookupResolvedClassFromTypeIndex(dex::TypeIndex type_idx) {
Vladimir Marko4098a7a2017-11-06 16:00:51 +0000292 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Vladimir Marko666ee3d2017-12-11 18:37:36 +0000293 ObjPtr<mirror::Class> type =
294 Runtime::Current()->GetClassLinker()->LookupResolvedType(type_idx, this);
295 DCHECK(!Thread::Current()->IsExceptionPending());
296 return type;
Vladimir Markob45528c2017-07-27 14:14:28 +0100297}
298
299inline ObjPtr<mirror::Class> ArtMethod::ResolveClassFromTypeIndex(dex::TypeIndex type_idx) {
Vladimir Marko666ee3d2017-12-11 18:37:36 +0000300 ObjPtr<mirror::Class> type = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, this);
301 DCHECK_EQ(type == nullptr, Thread::Current()->IsExceptionPending());
302 return type;
Ian Rogersa0485602014-12-02 15:48:04 -0800303}
304
Vladimir Marko7267e1e2022-11-07 12:37:32 +0100305inline bool ArtMethod::IsStringConstructor() {
306 uint32_t access_flags = GetAccessFlags();
307 DCHECK(!IsClassInitializer(access_flags));
308 return IsConstructor(access_flags) &&
309 // No read barrier needed for reading a constant reference only to read
310 // a constant string class flag. See `ReadBarrierOption`.
311 GetDeclaringClass<kWithoutReadBarrier>()->IsStringClass();
312}
313
Vladimir Markobed84ef2022-01-21 13:57:14 +0000314inline bool ArtMethod::IsOverridableByDefaultMethod() {
315 // It is safe to avoid the read barrier here since the constant interface flag
316 // in the `Class` object is stored before creating the `ArtMethod` and storing
317 // the declaring class reference. See `ReadBarrierOption`.
318 return GetDeclaringClass<kWithoutReadBarrier>()->IsInterface();
319}
320
Brian Carlstromea46f952013-07-30 01:26:50 -0700321inline bool ArtMethod::CheckIncompatibleClassChange(InvokeType type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800322 switch (type) {
323 case kStatic:
324 return !IsStatic();
325 case kDirect:
326 return !IsDirect() || IsStatic();
327 case kVirtual: {
Alex Lightd6e0fa92016-10-17 13:02:39 -0700328 // We have an error if we are direct or a non-copied (i.e. not part of a real class) interface
329 // method.
Vladimir Markod93e3742018-07-18 10:58:13 +0100330 ObjPtr<mirror::Class> methods_class = GetDeclaringClass();
Alex Lightd6e0fa92016-10-17 13:02:39 -0700331 return IsDirect() || (methods_class->IsInterface() && !IsCopied());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800332 }
333 case kSuper:
Andreas Gampe8f252e62014-08-25 20:46:31 -0700334 // Constructors and static methods are called with invoke-direct.
Alex Light705ad492015-09-21 11:36:30 -0700335 return IsConstructor() || IsStatic();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800336 case kInterface: {
Vladimir Markod93e3742018-07-18 10:58:13 +0100337 ObjPtr<mirror::Class> methods_class = GetDeclaringClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800338 return IsDirect() || !(methods_class->IsInterface() || methods_class->IsObjectClass());
339 }
Andra Danciua0130e82020-07-23 12:34:56 +0000340 case kPolymorphic:
Orion Hodson81daf3d2020-09-03 14:01:51 +0100341 return !IsSignaturePolymorphic();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800342 default:
343 LOG(FATAL) << "Unreachable - invocation type: " << type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700344 UNREACHABLE();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800345 }
346}
347
Ian Rogersef7d42f2014-01-06 12:55:46 -0800348inline bool ArtMethod::IsCalleeSaveMethod() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800349 if (!IsRuntimeMethod()) {
350 return false;
351 }
352 Runtime* runtime = Runtime::Current();
353 bool result = false;
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700354 for (uint32_t i = 0; i < static_cast<uint32_t>(CalleeSaveType::kLastCalleeSaveType); i++) {
355 if (this == runtime->GetCalleeSaveMethod(CalleeSaveType(i))) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800356 result = true;
357 break;
358 }
359 }
360 return result;
361}
362
Ian Rogersef7d42f2014-01-06 12:55:46 -0800363inline bool ArtMethod::IsResolutionMethod() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800364 bool result = this == Runtime::Current()->GetResolutionMethod();
365 // Check that if we do think it is phony it looks like the resolution method.
Santiago Aboy Solanes6cdabe12022-02-18 15:27:43 +0000366 DCHECK_IMPLIES(result, IsRuntimeMethod());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800367 return result;
368}
Jeff Hao88474b42013-10-23 16:24:40 -0700369
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700370inline bool ArtMethod::IsImtUnimplementedMethod() {
371 bool result = this == Runtime::Current()->GetImtUnimplementedMethod();
372 // Check that if we do think it is phony it looks like the imt unimplemented method.
Santiago Aboy Solanes6cdabe12022-02-18 15:27:43 +0000373 DCHECK_IMPLIES(result, IsRuntimeMethod());
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700374 return result;
375}
376
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700377inline const DexFile* ArtMethod::GetDexFile() {
Mathieu Chartier137cdfa2017-01-26 14:03:11 -0800378 // It is safe to avoid the read barrier here since the dex file is constant, so if we read the
379 // from-space dex file pointer it will be equal to the to-space copy.
380 return GetDexCache<kWithoutReadBarrier>()->GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700381}
382
383inline const char* ArtMethod::GetDeclaringClassDescriptor() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700384 uint32_t dex_method_idx = GetDexMethodIndex();
Andreas Gampee2abbc62017-09-15 11:59:26 -0700385 if (UNLIKELY(dex_method_idx == dex::kDexNoIndex)) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700386 return "<runtime method>";
387 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700388 DCHECK(!IsProxyMethod());
389 const DexFile* dex_file = GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700390 return dex_file->GetMethodDeclaringClassDescriptor(dex_file->GetMethodId(dex_method_idx));
391}
392
Andreas Gampec6ea7d02017-02-01 16:46:28 -0800393inline const char* ArtMethod::GetShorty() {
394 uint32_t unused_length;
395 return GetShorty(&unused_length);
396}
397
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700398inline const char* ArtMethod::GetShorty(uint32_t* out_length) {
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000399 DCHECK(!IsProxyMethod());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700400 const DexFile* dex_file = GetDexFile();
Vladimir Markoc945e0d2018-07-18 17:26:45 +0100401 return dex_file->GetMethodShorty(dex_file->GetMethodId(GetDexMethodIndex()), out_length);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700402}
403
Vladimir Marko473c5a012023-08-01 10:55:27 +0200404inline std::string_view ArtMethod::GetShortyView() {
405 DCHECK(!IsProxyMethod());
406 const DexFile* dex_file = GetDexFile();
407 return dex_file->GetMethodShortyView(dex_file->GetMethodId(GetDexMethodIndex()));
408}
409
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700410inline const Signature ArtMethod::GetSignature() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700411 uint32_t dex_method_idx = GetDexMethodIndex();
Andreas Gampee2abbc62017-09-15 11:59:26 -0700412 if (dex_method_idx != dex::kDexNoIndex) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700413 DCHECK(!IsProxyMethod());
414 const DexFile* dex_file = GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700415 return dex_file->GetMethodSignature(dex_file->GetMethodId(dex_method_idx));
416 }
417 return Signature::NoSignature();
418}
419
Ian Rogers1ff3c982014-08-12 02:30:58 -0700420inline const char* ArtMethod::GetName() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700421 uint32_t dex_method_idx = GetDexMethodIndex();
Andreas Gampee2abbc62017-09-15 11:59:26 -0700422 if (LIKELY(dex_method_idx != dex::kDexNoIndex)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700423 DCHECK(!IsProxyMethod());
424 const DexFile* dex_file = GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700425 return dex_file->GetMethodName(dex_file->GetMethodId(dex_method_idx));
426 }
Eric Holkabdb4592019-05-16 08:33:12 -0700427 return GetRuntimeMethodName();
428}
429
430inline std::string_view ArtMethod::GetNameView() {
431 uint32_t dex_method_idx = GetDexMethodIndex();
432 if (LIKELY(dex_method_idx != dex::kDexNoIndex)) {
433 DCHECK(!IsProxyMethod());
434 const DexFile* dex_file = GetDexFile();
Vladimir Marko4573be32021-06-07 11:07:05 +0100435 return dex_file->GetMethodNameView(dex_method_idx);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700436 }
Eric Holkabdb4592019-05-16 08:33:12 -0700437 return GetRuntimeMethodName();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700438}
439
Vladimir Marko18090d12018-06-01 16:53:12 +0100440inline ObjPtr<mirror::String> ArtMethod::ResolveNameString() {
441 DCHECK(!IsProxyMethod());
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800442 const dex::MethodId& method_id = GetDexFile()->GetMethodId(GetDexMethodIndex());
Vladimir Marko18090d12018-06-01 16:53:12 +0100443 return Runtime::Current()->GetClassLinker()->ResolveString(method_id.name_idx_, this);
444}
445
Vladimir Markod4e07aa2021-10-04 12:56:13 +0100446inline bool ArtMethod::NameEquals(ObjPtr<mirror::String> name) {
447 DCHECK(!IsProxyMethod());
448 const DexFile* dex_file = GetDexFile();
449 const dex::MethodId& method_id = dex_file->GetMethodId(GetDexMethodIndex());
450 const dex::StringIndex name_idx = method_id.name_idx_;
451 uint32_t utf16_length;
452 const char* utf8_name = dex_file->StringDataAndUtf16LengthByIdx(name_idx, &utf16_length);
453 return dchecked_integral_cast<uint32_t>(name->GetLength()) == utf16_length &&
454 name->Equals(utf8_name);
455}
456
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800457inline const dex::CodeItem* ArtMethod::GetCodeItem() {
Nicolas Geoffray47171752020-08-31 15:03:20 +0100458 if (!HasCodeItem()) {
459 return nullptr;
460 }
461 Runtime* runtime = Runtime::Current();
462 PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
463 return runtime->IsAotCompiler()
464 ? GetDexFile()->GetCodeItem(reinterpret_cast32<uint32_t>(GetDataPtrSize(pointer_size)))
Nicolas Geoffraye1d2dce2020-09-21 10:06:31 +0100465 : reinterpret_cast<const dex::CodeItem*>(
466 reinterpret_cast<uintptr_t>(GetDataPtrSize(pointer_size)) & ~1);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700467}
468
Vladimir Marko942fd312017-01-16 20:52:19 +0000469inline bool ArtMethod::IsResolvedTypeIdx(dex::TypeIndex type_idx) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700470 DCHECK(!IsProxyMethod());
Vladimir Markob45528c2017-07-27 14:14:28 +0100471 return LookupResolvedClassFromTypeIndex(type_idx) != nullptr;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700472}
473
474inline int32_t ArtMethod::GetLineNumFromDexPC(uint32_t dex_pc) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700475 DCHECK(!IsProxyMethod());
Andreas Gampee2abbc62017-09-15 11:59:26 -0700476 if (dex_pc == dex::kDexNoIndex) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700477 return IsNative() ? -2 : -1;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700478 }
David Sehr9323e6e2016-09-13 08:58:35 -0700479 return annotations::GetLineNumFromPC(GetDexFile(), this, dex_pc);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700480}
481
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800482inline const dex::ProtoId& ArtMethod::GetPrototype() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700483 DCHECK(!IsProxyMethod());
484 const DexFile* dex_file = GetDexFile();
485 return dex_file->GetMethodPrototype(dex_file->GetMethodId(GetDexMethodIndex()));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700486}
487
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800488inline const dex::TypeList* ArtMethod::GetParameterTypeList() {
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000489 DCHECK(!IsProxyMethod());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700490 const DexFile* dex_file = GetDexFile();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800491 const dex::ProtoId& proto = dex_file->GetMethodPrototype(
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000492 dex_file->GetMethodId(GetDexMethodIndex()));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700493 return dex_file->GetProtoParameters(proto);
494}
495
496inline const char* ArtMethod::GetDeclaringClassSourceFile() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700497 DCHECK(!IsProxyMethod());
498 return GetDeclaringClass()->GetSourceFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700499}
500
501inline uint16_t ArtMethod::GetClassDefIndex() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700502 DCHECK(!IsProxyMethod());
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000503 if (LIKELY(!IsObsolete())) {
504 return GetDeclaringClass()->GetDexClassDefIndex();
505 } else {
506 return FindObsoleteDexClassDefIndex();
507 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700508}
509
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800510inline const dex::ClassDef& ArtMethod::GetClassDef() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700511 DCHECK(!IsProxyMethod());
512 return GetDexFile()->GetClassDef(GetClassDefIndex());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700513}
514
Orion Hodson58143d22018-02-20 08:44:20 +0000515inline size_t ArtMethod::GetNumberOfParameters() {
516 constexpr size_t return_type_count = 1u;
Jared Duke968c8132021-11-02 16:47:54 -0700517 uint32_t shorty_length;
518 GetShorty(&shorty_length);
519 return shorty_length - return_type_count;
Orion Hodson58143d22018-02-20 08:44:20 +0000520}
521
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700522inline const char* ArtMethod::GetReturnTypeDescriptor() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700523 DCHECK(!IsProxyMethod());
524 const DexFile* dex_file = GetDexFile();
Vladimir Marko666ee3d2017-12-11 18:37:36 +0000525 return dex_file->GetTypeDescriptor(dex_file->GetTypeId(GetReturnTypeIndex()));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700526}
527
Alex Lightd7661582017-05-01 13:48:16 -0700528inline Primitive::Type ArtMethod::GetReturnTypePrimitive() {
529 return Primitive::GetType(GetReturnTypeDescriptor()[0]);
530}
531
Andreas Gampea5b09a62016-11-17 15:21:22 -0800532inline const char* ArtMethod::GetTypeDescriptorFromTypeIdx(dex::TypeIndex type_idx) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700533 DCHECK(!IsProxyMethod());
534 const DexFile* dex_file = GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700535 return dex_file->GetTypeDescriptor(dex_file->GetTypeId(type_idx));
536}
537
Vladimir Markoc524e9e2019-03-26 10:54:50 +0000538inline ObjPtr<mirror::ClassLoader> ArtMethod::GetClassLoader() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700539 DCHECK(!IsProxyMethod());
540 return GetDeclaringClass()->GetClassLoader();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700541}
542
Mathieu Chartier137cdfa2017-01-26 14:03:11 -0800543template <ReadBarrierOption kReadBarrierOption>
Vladimir Markoc524e9e2019-03-26 10:54:50 +0000544inline ObjPtr<mirror::DexCache> ArtMethod::GetDexCache() {
Vladimir Markoc945e0d2018-07-18 17:26:45 +0100545 if (LIKELY(!IsObsolete())) {
Vladimir Marko09c5ca42018-05-31 15:15:31 +0100546 ObjPtr<mirror::Class> klass = GetDeclaringClass<kReadBarrierOption>();
Mathieu Chartier137cdfa2017-01-26 14:03:11 -0800547 return klass->GetDexCache<kDefaultVerifyFlags, kReadBarrierOption>();
Alex Lightdba61482016-12-21 08:20:29 -0800548 } else {
549 DCHECK(!IsProxyMethod());
Vladimir Marko5f3c7eb2022-12-06 13:55:03 +0000550 return GetObsoleteDexCache<kReadBarrierOption>();
Alex Lighta01de592016-11-15 10:43:06 -0800551 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700552}
553
Ian Rogers03b6eaf2014-10-28 09:34:57 -0700554inline bool ArtMethod::IsProxyMethod() {
Roland Levillainfa854e42018-02-07 13:09:55 +0000555 DCHECK(!IsRuntimeMethod()) << "ArtMethod::IsProxyMethod called on a runtime method";
Vladimir Marko415ac852019-03-21 12:49:10 +0000556 // No read barrier needed, we're reading the constant declaring class only to read
557 // the constant proxy flag. See ReadBarrierOption.
Mathieu Chartier90c5a9b2017-02-01 13:10:06 -0800558 return GetDeclaringClass<kWithoutReadBarrier>()->IsProxyClass();
Ian Rogers03b6eaf2014-10-28 09:34:57 -0700559}
560
Vladimir Markod1ee20f2017-08-17 09:21:16 +0000561inline ArtMethod* ArtMethod::GetInterfaceMethodForProxyUnchecked(PointerSize pointer_size) {
562 DCHECK(IsProxyMethod());
563 // Do not check IsAssignableFrom() here as it relies on raw reference comparison
564 // which may give false negatives while visiting references for a non-CC moving GC.
565 return reinterpret_cast<ArtMethod*>(GetDataPtrSize(pointer_size));
566}
567
Andreas Gampe542451c2016-07-26 09:02:02 -0700568inline ArtMethod* ArtMethod::GetInterfaceMethodIfProxy(PointerSize pointer_size) {
Ian Rogers03b6eaf2014-10-28 09:34:57 -0700569 if (LIKELY(!IsProxyMethod())) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700570 return this;
571 }
Vladimir Markod1ee20f2017-08-17 09:21:16 +0000572 ArtMethod* interface_method = GetInterfaceMethodForProxyUnchecked(pointer_size);
573 // We can check that the proxy class implements the interface only if the proxy class
574 // is resolved, otherwise the interface table is not yet initialized.
Santiago Aboy Solanes6cdabe12022-02-18 15:27:43 +0000575 DCHECK_IMPLIES(GetDeclaringClass()->IsResolved(),
576 interface_method->GetDeclaringClass()->IsAssignableFrom(GetDeclaringClass()));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700577 return interface_method;
578}
579
Vladimir Markob45528c2017-07-27 14:14:28 +0100580inline dex::TypeIndex ArtMethod::GetReturnTypeIndex() {
Ian Rogersded66a02014-10-28 18:12:55 -0700581 DCHECK(!IsProxyMethod());
582 const DexFile* dex_file = GetDexFile();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800583 const dex::MethodId& method_id = dex_file->GetMethodId(GetDexMethodIndex());
584 const dex::ProtoId& proto_id = dex_file->GetMethodPrototype(method_id);
Vladimir Markob45528c2017-07-27 14:14:28 +0100585 return proto_id.return_type_idx_;
586}
587
588inline ObjPtr<mirror::Class> ArtMethod::LookupResolvedReturnType() {
589 return LookupResolvedClassFromTypeIndex(GetReturnTypeIndex());
590}
591
592inline ObjPtr<mirror::Class> ArtMethod::ResolveReturnType() {
593 return ResolveClassFromTypeIndex(GetReturnTypeIndex());
Ian Rogersded66a02014-10-28 18:12:55 -0700594}
595
Mingyao Yang063fc772016-08-02 11:02:54 -0700596inline bool ArtMethod::HasSingleImplementation() {
Vladimir Marko3292d102022-12-09 14:22:25 +0000597 // No read barrier needed for reading a constant reference only to read
598 // a constant final class flag. See `ReadBarrierOption`.
599 if (IsFinal() || GetDeclaringClass<kWithoutReadBarrier>()->IsFinal()) {
Mingyao Yang063fc772016-08-02 11:02:54 -0700600 // We don't set kAccSingleImplementation for these cases since intrinsic
601 // can use the flag also.
602 return true;
603 }
Vladimir Markoc945e0d2018-07-18 17:26:45 +0100604 return (GetAccessFlags() & kAccSingleImplementation) != 0;
Mingyao Yang063fc772016-08-02 11:02:54 -0700605}
606
Lokesh Gidrab7607c22022-01-28 12:30:31 -0800607template<ReadBarrierOption kReadBarrierOption, bool kVisitProxyMethod, typename RootVisitorType>
Andreas Gampe542451c2016-07-26 09:02:02 -0700608void ArtMethod::VisitRoots(RootVisitorType& visitor, PointerSize pointer_size) {
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -0700609 if (LIKELY(!declaring_class_.IsNull())) {
610 visitor.VisitRoot(declaring_class_.AddressWithoutBarrier());
Lokesh Gidrab7607c22022-01-28 12:30:31 -0800611 if (kVisitProxyMethod) {
612 ObjPtr<mirror::Class> klass = declaring_class_.Read<kReadBarrierOption>();
613 if (UNLIKELY(klass->IsProxyClass())) {
614 // For normal methods, dex cache shortcuts will be visited through the declaring class.
615 // However, for proxies we need to keep the interface method alive, so we visit its roots.
616 ArtMethod* interface_method = GetInterfaceMethodForProxyUnchecked(pointer_size);
617 DCHECK(interface_method != nullptr);
618 interface_method->VisitRoots<kReadBarrierOption, kVisitProxyMethod>(visitor, pointer_size);
619 }
Nicolas Geoffraydec3a122016-02-13 12:38:36 +0000620 }
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100621 }
Mathieu Chartier2d721012014-11-10 11:08:06 -0800622}
623
Lokesh Gidra606bd942022-05-23 19:00:09 +0000624template<typename RootVisitorType>
625void ArtMethod::VisitRoots(RootVisitorType& visitor,
626 uint8_t* start_boundary,
627 uint8_t* end_boundary,
628 ArtMethod* method) {
629 mirror::CompressedReference<mirror::Object>* cls_ptr =
630 reinterpret_cast<mirror::CompressedReference<mirror::Object>*>(
631 reinterpret_cast<uint8_t*>(method) + DeclaringClassOffset().Int32Value());
632 if (reinterpret_cast<uint8_t*>(cls_ptr) >= start_boundary
633 && reinterpret_cast<uint8_t*>(cls_ptr) < end_boundary) {
634 visitor.VisitRootIfNonNull(cls_ptr);
635 }
636}
637
638template<PointerSize kPointerSize, typename RootVisitorType>
639void ArtMethod::VisitArrayRoots(RootVisitorType& visitor,
640 uint8_t* start_boundary,
641 uint8_t* end_boundary,
642 LengthPrefixedArray<ArtMethod>* array) {
643 DCHECK_LE(start_boundary, end_boundary);
644 DCHECK_NE(array->size(), 0u);
645 static constexpr size_t kMethodSize = ArtMethod::Size(kPointerSize);
646 ArtMethod* first_method = &array->At(0, kMethodSize, ArtMethod::Alignment(kPointerSize));
647 DCHECK_LE(static_cast<void*>(end_boundary),
648 static_cast<void*>(reinterpret_cast<uint8_t*>(first_method)
649 + array->size() * kMethodSize));
650 uint8_t* declaring_class =
651 reinterpret_cast<uint8_t*>(first_method) + DeclaringClassOffset().Int32Value();
652 // Jump to the first class to visit.
653 if (declaring_class < start_boundary) {
654 size_t remainder = (start_boundary - declaring_class) % kMethodSize;
655 declaring_class = start_boundary;
656 if (remainder > 0) {
657 declaring_class += kMethodSize - remainder;
658 }
659 }
660 while (declaring_class < end_boundary) {
661 visitor.VisitRootIfNonNull(
662 reinterpret_cast<mirror::CompressedReference<mirror::Object>*>(declaring_class));
663 declaring_class += kMethodSize;
664 }
665}
666
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800667template <typename Visitor>
Andreas Gampe542451c2016-07-26 09:02:02 -0700668inline void ArtMethod::UpdateEntrypoints(const Visitor& visitor, PointerSize pointer_size) {
Vladimir Markoc945e0d2018-07-18 17:26:45 +0100669 if (IsNative()) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800670 const void* old_native_code = GetEntryPointFromJniPtrSize(pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800671 const void* new_native_code = visitor(old_native_code);
672 if (old_native_code != new_native_code) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800673 SetEntryPointFromJniPtrSize(new_native_code, pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800674 }
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800675 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800676 const void* old_code = GetEntryPointFromQuickCompiledCodePtrSize(pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800677 const void* new_code = visitor(old_code);
678 if (old_code != new_code) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800679 SetEntryPointFromQuickCompiledCodePtrSize(new_code, pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800680 }
681}
682
Vladimir Markoa20ec9b2022-12-12 09:12:21 +0000683template <ReadBarrierOption kReadBarrierOption>
684inline bool ArtMethod::StillNeedsClinitCheck() {
685 if (!NeedsClinitCheckBeforeCall()) {
686 return false;
687 }
688 ObjPtr<mirror::Class> klass = GetDeclaringClass<kReadBarrierOption>();
689 return !klass->IsVisiblyInitialized();
690}
691
692inline bool ArtMethod::StillNeedsClinitCheckMayBeDead() {
693 if (!NeedsClinitCheckBeforeCall()) {
694 return false;
695 }
Vladimir Markod44adb62022-12-12 16:28:25 +0000696 ObjPtr<mirror::Class> klass = GetDeclaringClassMayBeDead();
697 return !klass->IsVisiblyInitialized();
698}
699
700inline bool ArtMethod::IsDeclaringClassVerifiedMayBeDead() {
701 ObjPtr<mirror::Class> klass = GetDeclaringClassMayBeDead();
702 return klass->IsVerified();
703}
704
705inline ObjPtr<mirror::Class> ArtMethod::GetDeclaringClassMayBeDead() {
706 // Helper method for checking the status of the declaring class which may be dead.
707 //
Vladimir Markoa20ec9b2022-12-12 09:12:21 +0000708 // To avoid resurrecting an unreachable object, or crashing the GC in some GC phases,
709 // we must not use a full read barrier. Therefore we read the declaring class without
710 // a read barrier and check if it's already marked. If yes, we check the status of the
711 // to-space class object as intended. Otherwise, there is no to-space object and the
712 // from-space class object contains the most recent value of the status field; even if
713 // this races with another thread doing a read barrier and updating the status, that's
714 // no different from a race with a thread that just updates the status.
715 ObjPtr<mirror::Class> klass = GetDeclaringClass<kWithoutReadBarrier>();
716 ObjPtr<mirror::Class> marked = ReadBarrier::IsMarked(klass.Ptr());
Vladimir Markod44adb62022-12-12 16:28:25 +0000717 return (marked != nullptr) ? marked : klass;
Vladimir Markoa20ec9b2022-12-12 09:12:21 +0000718}
719
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800720inline CodeItemInstructionAccessor ArtMethod::DexInstructions() {
David Sehr0225f8e2018-01-31 08:52:24 +0000721 return CodeItemInstructionAccessor(*GetDexFile(), GetCodeItem());
722}
723
724inline CodeItemDataAccessor ArtMethod::DexInstructionData() {
725 return CodeItemDataAccessor(*GetDexFile(), GetCodeItem());
726}
727
728inline CodeItemDebugInfoAccessor ArtMethod::DexInstructionDebugInfo() {
729 return CodeItemDebugInfoAccessor(*GetDexFile(), GetCodeItem(), GetDexMethodIndex());
Mathieu Chartier69147f12017-11-06 20:02:24 -0800730}
731
Nicolas Geoffray58f916c2021-11-15 14:02:07 +0000732inline bool ArtMethod::CounterHasChanged(uint16_t threshold) {
Vladimir Markodd446b12021-05-20 14:35:51 +0100733 DCHECK(!IsAbstract());
Nicolas Geoffray58f916c2021-11-15 14:02:07 +0000734 DCHECK_EQ(threshold, Runtime::Current()->GetJITOptions()->GetWarmupThreshold());
735 return hotness_count_ != threshold;
Nicolas Geoffray61673dc2021-11-06 13:58:31 +0000736}
737
Nicolas Geoffray58f916c2021-11-15 14:02:07 +0000738inline void ArtMethod::ResetCounter(uint16_t new_value) {
739 if (IsAbstract()) {
740 return;
741 }
Nicolas Geoffrayf9ae8e32022-02-15 22:54:11 +0000742 if (IsMemorySharedMethod()) {
743 return;
744 }
Nicolas Geoffray58f916c2021-11-15 14:02:07 +0000745 DCHECK_EQ(new_value, Runtime::Current()->GetJITOptions()->GetWarmupThreshold());
Nicolas Geoffray61673dc2021-11-06 13:58:31 +0000746 // Avoid dirtying the value if possible.
Nicolas Geoffray58f916c2021-11-15 14:02:07 +0000747 if (hotness_count_ != new_value) {
748 hotness_count_ = new_value;
Nicolas Geoffray61673dc2021-11-06 13:58:31 +0000749 }
750}
751
752inline void ArtMethod::SetHotCounter() {
753 DCHECK(!IsAbstract());
754 // Avoid dirtying the value if possible.
755 if (hotness_count_ != 0) {
756 hotness_count_ = 0;
757 }
758}
759
760inline void ArtMethod::UpdateCounter(int32_t new_samples) {
761 DCHECK(!IsAbstract());
762 DCHECK_GT(new_samples, 0);
763 DCHECK_LE(new_samples, std::numeric_limits<uint16_t>::max());
Nicolas Geoffrayf9ae8e32022-02-15 22:54:11 +0000764 if (IsMemorySharedMethod()) {
765 return;
766 }
Nicolas Geoffray61673dc2021-11-06 13:58:31 +0000767 uint16_t old_hotness_count = hotness_count_;
768 uint16_t new_count = (old_hotness_count <= new_samples) ? 0u : old_hotness_count - new_samples;
769 // Avoid dirtying the value if possible.
770 if (old_hotness_count != new_count) {
771 hotness_count_ = new_count;
772 }
773}
774
775inline bool ArtMethod::CounterIsHot() {
776 DCHECK(!IsAbstract());
777 return hotness_count_ == 0;
778}
779
Nicolas Geoffray58f916c2021-11-15 14:02:07 +0000780inline bool ArtMethod::CounterHasReached(uint16_t samples, uint16_t threshold) {
Nicolas Geoffray61673dc2021-11-06 13:58:31 +0000781 DCHECK(!IsAbstract());
Nicolas Geoffray58f916c2021-11-15 14:02:07 +0000782 DCHECK_EQ(threshold, Runtime::Current()->GetJITOptions()->GetWarmupThreshold());
783 DCHECK_LE(samples, threshold);
784 return hotness_count_ <= (threshold - samples);
David Srbeckye36e7f22018-11-14 14:21:23 +0000785}
786
787inline uint16_t ArtMethod::GetCounter() {
Vladimir Markodd446b12021-05-20 14:35:51 +0100788 DCHECK(!IsAbstract());
David Srbeckye36e7f22018-11-14 14:21:23 +0000789 return hotness_count_;
790}
791
792inline uint32_t ArtMethod::GetImtIndex() {
Nicolas Geoffrayd5a86952021-01-19 10:35:54 +0000793 if (LIKELY(IsAbstract())) {
794 return imt_index_;
David Srbeckye36e7f22018-11-14 14:21:23 +0000795 } else {
796 return ImTable::GetImtIndex(this);
797 }
798}
799
800inline void ArtMethod::CalculateAndSetImtIndex() {
801 DCHECK(IsAbstract()) << PrettyMethod();
Nicolas Geoffrayd5a86952021-01-19 10:35:54 +0000802 imt_index_ = ImTable::GetImtIndex(this);
David Srbeckye36e7f22018-11-14 14:21:23 +0000803}
804
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800805} // namespace art
806
Mathieu Chartiere401d142015-04-22 13:56:20 -0700807#endif // ART_RUNTIME_ART_METHOD_INL_H_