blob: 632a50f15c4ff713fd299f07237b49e7763cae88 [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"
Mathieu Chartier7c0fe5e2015-07-17 19:53:47 -070023#include "base/logging.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"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080026#include "dex_file.h"
Elliott Hughes956af0f2014-12-11 14:34:28 -080027#include "dex_file-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070028#include "gc_root-inl.h"
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010029#include "jit/profiling_info.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070030#include "mirror/class-inl.h"
Vladimir Marko05792b92015-08-03 11:56:49 +010031#include "mirror/dex_cache-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070032#include "mirror/object-inl.h"
33#include "mirror/object_array.h"
Vladimir Marko96c6ab92014-04-08 14:00:50 +010034#include "oat.h"
Vladimir Marko7624d252014-05-02 14:40:15 +010035#include "quick/quick_method_frame_info.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070036#include "read_barrier-inl.h"
Vladimir Marko7624d252014-05-02 14:40:15 +010037#include "runtime-inl.h"
Andreas Gampecbc96b82015-09-30 20:05:24 +000038#include "scoped_thread_state_change.h"
39#include "thread-inl.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010040#include "utils.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080041
42namespace art {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080043
Mathieu Chartiere401d142015-04-22 13:56:20 -070044inline mirror::Class* ArtMethod::GetDeclaringClassUnchecked() {
Hiroshi Yamauchi3f64f252015-06-12 18:35:06 -070045 GcRootSource gc_root_source(this);
46 return declaring_class_.Read(&gc_root_source);
Mingyao Yang98d1cc82014-05-15 17:02:16 -070047}
48
Mathieu Chartiere401d142015-04-22 13:56:20 -070049inline mirror::Class* ArtMethod::GetDeclaringClassNoBarrier() {
50 return declaring_class_.Read<kWithoutReadBarrier>();
Mingyao Yang98d1cc82014-05-15 17:02:16 -070051}
52
Mathieu Chartiere401d142015-04-22 13:56:20 -070053inline mirror::Class* ArtMethod::GetDeclaringClass() {
54 mirror::Class* result = GetDeclaringClassUnchecked();
55 if (kIsDebugBuild) {
56 if (!IsRuntimeMethod()) {
57 CHECK(result != nullptr) << this;
58 CHECK(result->IsIdxLoaded() || result->IsErroneous())
59 << result->GetStatus() << " " << PrettyClass(result);
60 } else {
61 CHECK(result == nullptr) << this;
62 }
63 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080064 return result;
65}
66
Mathieu Chartiere401d142015-04-22 13:56:20 -070067inline void ArtMethod::SetDeclaringClass(mirror::Class* new_declaring_class) {
68 declaring_class_ = GcRoot<mirror::Class>(new_declaring_class);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080069}
70
Mathieu Chartier10e5ea92015-08-13 12:56:31 -070071inline bool ArtMethod::CASDeclaringClass(mirror::Class* expected_class,
72 mirror::Class* desired_class) {
73 GcRoot<mirror::Class> expected_root(expected_class);
74 GcRoot<mirror::Class> desired_root(desired_class);
75 return reinterpret_cast<Atomic<GcRoot<mirror::Class>>*>(&declaring_class_)->
76 CompareExchangeStrongSequentiallyConsistent(
77 expected_root, desired_root);
78}
79
Andreas Gampecbc96b82015-09-30 20:05:24 +000080// AssertSharedHeld doesn't work in GetAccessFlags, so use a NO_THREAD_SAFETY_ANALYSIS helper.
81// TODO: Figure out why ASSERT_SHARED_CAPABILITY doesn't work.
82ALWAYS_INLINE
83static inline void DoGetAccessFlagsHelper(ArtMethod* method) NO_THREAD_SAFETY_ANALYSIS {
84 CHECK(method->IsRuntimeMethod() || method->GetDeclaringClass()->IsIdxLoaded() ||
85 method->GetDeclaringClass()->IsErroneous());
86}
87
Ian Rogersef7d42f2014-01-06 12:55:46 -080088inline uint32_t ArtMethod::GetAccessFlags() {
Andreas Gampecbc96b82015-09-30 20:05:24 +000089 if (kIsDebugBuild) {
90 Thread* self = Thread::Current();
91 if (!Locks::mutator_lock_->IsSharedHeld(self)) {
92 ScopedObjectAccess soa(self);
93 CHECK(IsRuntimeMethod() || GetDeclaringClass()->IsIdxLoaded() ||
94 GetDeclaringClass()->IsErroneous());
95 } else {
96 // We cannot use SOA in this case. We might be holding the lock, but may not be in the
97 // runnable state (e.g., during GC).
98 Locks::mutator_lock_->AssertSharedHeld(self);
99 DoGetAccessFlagsHelper(this);
100 }
101 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700102 return access_flags_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800103}
104
Ian Rogersef7d42f2014-01-06 12:55:46 -0800105inline uint16_t ArtMethod::GetMethodIndex() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700106 DCHECK(IsRuntimeMethod() || GetDeclaringClass()->IsResolved() ||
107 GetDeclaringClass()->IsErroneous());
108 return method_index_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800109}
110
Mathieu Chartier9f3629d2014-10-28 18:23:02 -0700111inline uint16_t ArtMethod::GetMethodIndexDuringLinking() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700112 return method_index_;
Mathieu Chartier9f3629d2014-10-28 18:23:02 -0700113}
114
Ian Rogersef7d42f2014-01-06 12:55:46 -0800115inline uint32_t ArtMethod::GetDexMethodIndex() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700116 DCHECK(IsRuntimeMethod() || GetDeclaringClass()->IsIdxLoaded() ||
117 GetDeclaringClass()->IsErroneous());
118 return dex_method_index_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800119}
120
Vladimir Marko05792b92015-08-03 11:56:49 +0100121inline ArtMethod** ArtMethod::GetDexCacheResolvedMethods(size_t pointer_size) {
122 return GetNativePointer<ArtMethod**>(DexCacheResolvedMethodsOffset(pointer_size),
123 pointer_size);
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700124}
125
Mathieu Chartiere401d142015-04-22 13:56:20 -0700126inline ArtMethod* ArtMethod::GetDexCacheResolvedMethod(uint16_t method_index, size_t ptr_size) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100127 // NOTE: Unchecked, i.e. not throwing AIOOB. We don't even know the length here
128 // without accessing the DexCache and we don't want to do that in release build.
129 DCHECK_LT(method_index,
130 GetInterfaceMethodIfProxy(ptr_size)->GetDeclaringClass()
131 ->GetDexCache()->NumResolvedMethods());
132 ArtMethod* method = mirror::DexCache::GetElementPtrSize(GetDexCacheResolvedMethods(ptr_size),
133 method_index,
134 ptr_size);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700135 if (LIKELY(method != nullptr)) {
136 auto* declaring_class = method->GetDeclaringClass();
137 if (LIKELY(declaring_class == nullptr || !declaring_class->IsErroneous())) {
138 return method;
139 }
Andreas Gampe58a5af82014-07-31 16:23:49 -0700140 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700141 return nullptr;
Andreas Gampe58a5af82014-07-31 16:23:49 -0700142}
143
Vladimir Marko05792b92015-08-03 11:56:49 +0100144inline void ArtMethod::SetDexCacheResolvedMethod(uint16_t method_index, ArtMethod* new_method,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700145 size_t ptr_size) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100146 // NOTE: Unchecked, i.e. not throwing AIOOB. We don't even know the length here
147 // without accessing the DexCache and we don't want to do that in release build.
148 DCHECK_LT(method_index,
149 GetInterfaceMethodIfProxy(ptr_size)->GetDeclaringClass()
150 ->GetDexCache()->NumResolvedMethods());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700151 DCHECK(new_method == nullptr || new_method->GetDeclaringClass() != nullptr);
Vladimir Marko05792b92015-08-03 11:56:49 +0100152 mirror::DexCache::SetElementPtrSize(GetDexCacheResolvedMethods(ptr_size),
153 method_index,
154 new_method,
155 ptr_size);
Andreas Gampe58a5af82014-07-31 16:23:49 -0700156}
157
Vladimir Marko05792b92015-08-03 11:56:49 +0100158inline bool ArtMethod::HasDexCacheResolvedMethods(size_t pointer_size) {
159 return GetDexCacheResolvedMethods(pointer_size) != nullptr;
Andreas Gampe58a5af82014-07-31 16:23:49 -0700160}
161
Vladimir Marko05792b92015-08-03 11:56:49 +0100162inline bool ArtMethod::HasSameDexCacheResolvedMethods(ArtMethod** other_cache,
163 size_t pointer_size) {
164 return GetDexCacheResolvedMethods(pointer_size) == other_cache;
Andreas Gampe58a5af82014-07-31 16:23:49 -0700165}
166
Vladimir Marko05792b92015-08-03 11:56:49 +0100167inline bool ArtMethod::HasSameDexCacheResolvedMethods(ArtMethod* other, size_t pointer_size) {
168 return GetDexCacheResolvedMethods(pointer_size) ==
169 other->GetDexCacheResolvedMethods(pointer_size);
Andreas Gampe58a5af82014-07-31 16:23:49 -0700170}
171
Vladimir Marko05792b92015-08-03 11:56:49 +0100172inline GcRoot<mirror::Class>* ArtMethod::GetDexCacheResolvedTypes(size_t pointer_size) {
173 return GetNativePointer<GcRoot<mirror::Class>*>(DexCacheResolvedTypesOffset(pointer_size),
174 pointer_size);
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700175}
176
Andreas Gampe58a5af82014-07-31 16:23:49 -0700177template <bool kWithCheck>
Vladimir Marko05792b92015-08-03 11:56:49 +0100178inline mirror::Class* ArtMethod::GetDexCacheResolvedType(uint32_t type_index, size_t ptr_size) {
179 if (kWithCheck) {
180 mirror::DexCache* dex_cache =
181 GetInterfaceMethodIfProxy(ptr_size)->GetDeclaringClass()->GetDexCache();
182 if (UNLIKELY(type_index >= dex_cache->NumResolvedTypes())) {
183 ThrowArrayIndexOutOfBoundsException(type_index, dex_cache->NumResolvedTypes());
184 return nullptr;
185 }
186 }
187 mirror::Class* klass = GetDexCacheResolvedTypes(ptr_size)[type_index].Read();
Andreas Gampe58a5af82014-07-31 16:23:49 -0700188 return (klass != nullptr && !klass->IsErroneous()) ? klass : nullptr;
189}
190
Vladimir Marko05792b92015-08-03 11:56:49 +0100191inline bool ArtMethod::HasDexCacheResolvedTypes(size_t pointer_size) {
192 return GetDexCacheResolvedTypes(pointer_size) != nullptr;
Andreas Gampe58a5af82014-07-31 16:23:49 -0700193}
194
Vladimir Marko05792b92015-08-03 11:56:49 +0100195inline bool ArtMethod::HasSameDexCacheResolvedTypes(GcRoot<mirror::Class>* other_cache,
196 size_t pointer_size) {
197 return GetDexCacheResolvedTypes(pointer_size) == other_cache;
Andreas Gampe58a5af82014-07-31 16:23:49 -0700198}
199
Vladimir Marko05792b92015-08-03 11:56:49 +0100200inline bool ArtMethod::HasSameDexCacheResolvedTypes(ArtMethod* other, size_t pointer_size) {
201 return GetDexCacheResolvedTypes(pointer_size) == other->GetDexCacheResolvedTypes(pointer_size);
Andreas Gampe58a5af82014-07-31 16:23:49 -0700202}
203
Vladimir Marko05792b92015-08-03 11:56:49 +0100204inline mirror::Class* ArtMethod::GetClassFromTypeIndex(uint16_t type_idx,
205 bool resolve,
206 size_t ptr_size) {
207 mirror::Class* type = GetDexCacheResolvedType(type_idx, ptr_size);
Ian Rogersa0485602014-12-02 15:48:04 -0800208 if (type == nullptr && resolve) {
209 type = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, this);
210 CHECK(type != nullptr || Thread::Current()->IsExceptionPending());
211 }
212 return type;
213}
214
Ian Rogersef7d42f2014-01-06 12:55:46 -0800215inline uint32_t ArtMethod::GetCodeSize() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800216 DCHECK(!IsRuntimeMethod() && !IsProxyMethod()) << PrettyMethod(this);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800217 return GetCodeSize(EntryPointToCodePointer(GetEntryPointFromQuickCompiledCode()));
218}
219
220inline uint32_t ArtMethod::GetCodeSize(const void* code) {
Vladimir Marko8a630572014-04-09 18:45:35 +0100221 if (code == nullptr) {
222 return 0u;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800223 }
Vladimir Marko7624d252014-05-02 14:40:15 +0100224 return reinterpret_cast<const OatQuickMethodHeader*>(code)[-1].code_size_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800225}
226
Brian Carlstromea46f952013-07-30 01:26:50 -0700227inline bool ArtMethod::CheckIncompatibleClassChange(InvokeType type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800228 switch (type) {
229 case kStatic:
230 return !IsStatic();
231 case kDirect:
232 return !IsDirect() || IsStatic();
233 case kVirtual: {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700234 mirror::Class* methods_class = GetDeclaringClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800235 return IsDirect() || (methods_class->IsInterface() && !IsMiranda());
236 }
237 case kSuper:
Andreas Gampe8f252e62014-08-25 20:46:31 -0700238 // Constructors and static methods are called with invoke-direct.
239 // Interface methods cannot be invoked with invoke-super.
240 return IsConstructor() || IsStatic() || GetDeclaringClass()->IsInterface();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800241 case kInterface: {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700242 mirror::Class* methods_class = GetDeclaringClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800243 return IsDirect() || !(methods_class->IsInterface() || methods_class->IsObjectClass());
244 }
245 default:
246 LOG(FATAL) << "Unreachable - invocation type: " << type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700247 UNREACHABLE();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800248 }
249}
250
Ian Rogersef7d42f2014-01-06 12:55:46 -0800251inline uint32_t ArtMethod::GetQuickOatCodeOffset() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800252 DCHECK(!Runtime::Current()->IsStarted());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800253 return PointerToLowMemUInt32(GetEntryPointFromQuickCompiledCode());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800254}
255
Ian Rogersef7d42f2014-01-06 12:55:46 -0800256inline void ArtMethod::SetQuickOatCodeOffset(uint32_t code_offset) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800257 DCHECK(!Runtime::Current()->IsStarted());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800258 SetEntryPointFromQuickCompiledCode(reinterpret_cast<void*>(code_offset));
259}
260
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800261inline const uint8_t* ArtMethod::GetMappingTable(size_t pointer_size) {
262 const void* code_pointer = GetQuickOatCodePointer(pointer_size);
Vladimir Marko4c1c5102014-05-14 16:51:16 +0100263 if (code_pointer == nullptr) {
264 return nullptr;
265 }
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800266 return GetMappingTable(code_pointer, pointer_size);
Vladimir Marko4c1c5102014-05-14 16:51:16 +0100267}
268
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800269inline const uint8_t* ArtMethod::GetMappingTable(const void* code_pointer, size_t pointer_size) {
Vladimir Marko4c1c5102014-05-14 16:51:16 +0100270 DCHECK(code_pointer != nullptr);
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800271 DCHECK_EQ(code_pointer, GetQuickOatCodePointer(pointer_size));
Vladimir Marko4c1c5102014-05-14 16:51:16 +0100272 uint32_t offset =
273 reinterpret_cast<const OatQuickMethodHeader*>(code_pointer)[-1].mapping_table_offset_;
274 if (UNLIKELY(offset == 0u)) {
275 return nullptr;
276 }
277 return reinterpret_cast<const uint8_t*>(code_pointer) - offset;
278}
279
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800280inline const uint8_t* ArtMethod::GetVmapTable(size_t pointer_size) {
281 const void* code_pointer = GetQuickOatCodePointer(pointer_size);
Vladimir Marko4c1c5102014-05-14 16:51:16 +0100282 if (code_pointer == nullptr) {
283 return nullptr;
284 }
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800285 return GetVmapTable(code_pointer, pointer_size);
Vladimir Marko4c1c5102014-05-14 16:51:16 +0100286}
287
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800288inline const uint8_t* ArtMethod::GetVmapTable(const void* code_pointer, size_t pointer_size) {
289 CHECK(!IsOptimized(pointer_size)) << "Unimplemented vmap table for optimized compiler";
Vladimir Marko4c1c5102014-05-14 16:51:16 +0100290 DCHECK(code_pointer != nullptr);
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800291 DCHECK_EQ(code_pointer, GetQuickOatCodePointer(pointer_size));
Vladimir Marko4c1c5102014-05-14 16:51:16 +0100292 uint32_t offset =
293 reinterpret_cast<const OatQuickMethodHeader*>(code_pointer)[-1].vmap_table_offset_;
294 if (UNLIKELY(offset == 0u)) {
295 return nullptr;
296 }
297 return reinterpret_cast<const uint8_t*>(code_pointer) - offset;
298}
299
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100300inline CodeInfo ArtMethod::GetOptimizedCodeInfo() {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800301 DCHECK(IsOptimized(sizeof(void*)));
302 const void* code_pointer = GetQuickOatCodePointer(sizeof(void*));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100303 DCHECK(code_pointer != nullptr);
304 uint32_t offset =
305 reinterpret_cast<const OatQuickMethodHeader*>(code_pointer)[-1].vmap_table_offset_;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700306 const void* data =
307 reinterpret_cast<const void*>(reinterpret_cast<const uint8_t*>(code_pointer) - offset);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100308 return CodeInfo(data);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100309}
310
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800311inline const uint8_t* ArtMethod::GetNativeGcMap(size_t pointer_size) {
312 const void* code_pointer = GetQuickOatCodePointer(pointer_size);
313 if (code_pointer == nullptr) {
314 return nullptr;
315 }
316 return GetNativeGcMap(code_pointer, pointer_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800317}
318
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800319inline const uint8_t* ArtMethod::GetNativeGcMap(const void* code_pointer, size_t pointer_size) {
320 DCHECK(code_pointer != nullptr);
321 DCHECK_EQ(code_pointer, GetQuickOatCodePointer(pointer_size));
322 uint32_t offset =
323 reinterpret_cast<const OatQuickMethodHeader*>(code_pointer)[-1].gc_map_offset_;
324 if (UNLIKELY(offset == 0u)) {
325 return nullptr;
326 }
327 return reinterpret_cast<const uint8_t*>(code_pointer) - offset;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800328}
329
Ian Rogersef7d42f2014-01-06 12:55:46 -0800330inline bool ArtMethod::IsRuntimeMethod() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700331 return dex_method_index_ == DexFile::kDexNoIndex;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800332}
333
Ian Rogersef7d42f2014-01-06 12:55:46 -0800334inline bool ArtMethod::IsCalleeSaveMethod() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800335 if (!IsRuntimeMethod()) {
336 return false;
337 }
338 Runtime* runtime = Runtime::Current();
339 bool result = false;
340 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
341 if (this == runtime->GetCalleeSaveMethod(Runtime::CalleeSaveType(i))) {
342 result = true;
343 break;
344 }
345 }
346 return result;
347}
348
Ian Rogersef7d42f2014-01-06 12:55:46 -0800349inline bool ArtMethod::IsResolutionMethod() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800350 bool result = this == Runtime::Current()->GetResolutionMethod();
351 // Check that if we do think it is phony it looks like the resolution method.
352 DCHECK(!result || IsRuntimeMethod());
353 return result;
354}
Jeff Hao88474b42013-10-23 16:24:40 -0700355
Ian Rogersef7d42f2014-01-06 12:55:46 -0800356inline bool ArtMethod::IsImtConflictMethod() {
Jeff Hao88474b42013-10-23 16:24:40 -0700357 bool result = this == Runtime::Current()->GetImtConflictMethod();
358 // Check that if we do think it is phony it looks like the imt conflict method.
359 DCHECK(!result || IsRuntimeMethod());
360 return result;
361}
Mathieu Chartier4e305412014-02-19 10:54:44 -0800362
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700363inline bool ArtMethod::IsImtUnimplementedMethod() {
364 bool result = this == Runtime::Current()->GetImtUnimplementedMethod();
365 // Check that if we do think it is phony it looks like the imt unimplemented method.
366 DCHECK(!result || IsRuntimeMethod());
367 return result;
368}
369
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700370inline uintptr_t ArtMethod::NativeQuickPcOffset(const uintptr_t pc) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800371 const void* code = Runtime::Current()->GetInstrumentation()->GetQuickCodeFor(
372 this, sizeof(void*));
Vladimir Marko4c1c5102014-05-14 16:51:16 +0100373 return pc - reinterpret_cast<uintptr_t>(code);
374}
375
Vladimir Marko4c1c5102014-05-14 16:51:16 +0100376inline QuickMethodFrameInfo ArtMethod::GetQuickFrameInfo(const void* code_pointer) {
377 DCHECK(code_pointer != nullptr);
Mathieu Chartier7c0fe5e2015-07-17 19:53:47 -0700378 if (kIsDebugBuild && !IsProxyMethod()) {
379 CHECK_EQ(code_pointer, GetQuickOatCodePointer(sizeof(void*)));
380 }
Vladimir Marko7624d252014-05-02 14:40:15 +0100381 return reinterpret_cast<const OatQuickMethodHeader*>(code_pointer)[-1].frame_info_;
382}
383
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700384inline const DexFile* ArtMethod::GetDexFile() {
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700385 return GetDexCache()->GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700386}
387
388inline const char* ArtMethod::GetDeclaringClassDescriptor() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700389 uint32_t dex_method_idx = GetDexMethodIndex();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700390 if (UNLIKELY(dex_method_idx == DexFile::kDexNoIndex)) {
391 return "<runtime method>";
392 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700393 DCHECK(!IsProxyMethod());
394 const DexFile* dex_file = GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700395 return dex_file->GetMethodDeclaringClassDescriptor(dex_file->GetMethodId(dex_method_idx));
396}
397
398inline const char* ArtMethod::GetShorty(uint32_t* out_length) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700399 DCHECK(!IsProxyMethod());
400 const DexFile* dex_file = GetDexFile();
401 return dex_file->GetMethodShorty(dex_file->GetMethodId(GetDexMethodIndex()), out_length);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700402}
403
404inline const Signature ArtMethod::GetSignature() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700405 uint32_t dex_method_idx = GetDexMethodIndex();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700406 if (dex_method_idx != DexFile::kDexNoIndex) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700407 DCHECK(!IsProxyMethod());
408 const DexFile* dex_file = GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700409 return dex_file->GetMethodSignature(dex_file->GetMethodId(dex_method_idx));
410 }
411 return Signature::NoSignature();
412}
413
Ian Rogers1ff3c982014-08-12 02:30:58 -0700414inline const char* ArtMethod::GetName() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700415 uint32_t dex_method_idx = GetDexMethodIndex();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700416 if (LIKELY(dex_method_idx != DexFile::kDexNoIndex)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700417 DCHECK(!IsProxyMethod());
418 const DexFile* dex_file = GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700419 return dex_file->GetMethodName(dex_file->GetMethodId(dex_method_idx));
420 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700421 Runtime* const runtime = Runtime::Current();
422 if (this == runtime->GetResolutionMethod()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700423 return "<runtime internal resolution method>";
Mathieu Chartiere401d142015-04-22 13:56:20 -0700424 } else if (this == runtime->GetImtConflictMethod()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700425 return "<runtime internal imt conflict method>";
Mathieu Chartiere401d142015-04-22 13:56:20 -0700426 } else if (this == runtime->GetCalleeSaveMethod(Runtime::kSaveAll)) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700427 return "<runtime internal callee-save all registers method>";
Mathieu Chartiere401d142015-04-22 13:56:20 -0700428 } else if (this == runtime->GetCalleeSaveMethod(Runtime::kRefsOnly)) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700429 return "<runtime internal callee-save reference registers method>";
Mathieu Chartiere401d142015-04-22 13:56:20 -0700430 } else if (this == runtime->GetCalleeSaveMethod(Runtime::kRefsAndArgs)) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700431 return "<runtime internal callee-save reference and argument registers method>";
432 } else {
433 return "<unknown runtime internal method>";
434 }
435}
436
437inline const DexFile::CodeItem* ArtMethod::GetCodeItem() {
Mathieu Chartier12b3dd72014-12-11 13:25:33 -0800438 return GetDeclaringClass()->GetDexFile().GetCodeItem(GetCodeItemOffset());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700439}
440
Vladimir Marko05792b92015-08-03 11:56:49 +0100441inline bool ArtMethod::IsResolvedTypeIdx(uint16_t type_idx, size_t ptr_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700442 DCHECK(!IsProxyMethod());
Vladimir Marko05792b92015-08-03 11:56:49 +0100443 return GetDexCacheResolvedType(type_idx, ptr_size) != nullptr;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700444}
445
446inline int32_t ArtMethod::GetLineNumFromDexPC(uint32_t dex_pc) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700447 DCHECK(!IsProxyMethod());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700448 if (dex_pc == DexFile::kDexNoIndex) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700449 return IsNative() ? -2 : -1;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700450 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700451 return GetDexFile()->GetLineNumFromPC(this, dex_pc);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700452}
453
454inline const DexFile::ProtoId& ArtMethod::GetPrototype() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700455 DCHECK(!IsProxyMethod());
456 const DexFile* dex_file = GetDexFile();
457 return dex_file->GetMethodPrototype(dex_file->GetMethodId(GetDexMethodIndex()));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700458}
459
460inline const DexFile::TypeList* ArtMethod::GetParameterTypeList() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700461 DCHECK(!IsProxyMethod());
462 const DexFile* dex_file = GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700463 const DexFile::ProtoId& proto = dex_file->GetMethodPrototype(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700464 dex_file->GetMethodId(GetDexMethodIndex()));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700465 return dex_file->GetProtoParameters(proto);
466}
467
468inline const char* ArtMethod::GetDeclaringClassSourceFile() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700469 DCHECK(!IsProxyMethod());
470 return GetDeclaringClass()->GetSourceFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700471}
472
473inline uint16_t ArtMethod::GetClassDefIndex() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700474 DCHECK(!IsProxyMethod());
475 return GetDeclaringClass()->GetDexClassDefIndex();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700476}
477
478inline const DexFile::ClassDef& ArtMethod::GetClassDef() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700479 DCHECK(!IsProxyMethod());
480 return GetDexFile()->GetClassDef(GetClassDefIndex());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700481}
482
483inline const char* ArtMethod::GetReturnTypeDescriptor() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700484 DCHECK(!IsProxyMethod());
485 const DexFile* dex_file = GetDexFile();
486 const DexFile::MethodId& method_id = dex_file->GetMethodId(GetDexMethodIndex());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700487 const DexFile::ProtoId& proto_id = dex_file->GetMethodPrototype(method_id);
488 uint16_t return_type_idx = proto_id.return_type_idx_;
489 return dex_file->GetTypeDescriptor(dex_file->GetTypeId(return_type_idx));
490}
491
492inline const char* ArtMethod::GetTypeDescriptorFromTypeIdx(uint16_t type_idx) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700493 DCHECK(!IsProxyMethod());
494 const DexFile* dex_file = GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700495 return dex_file->GetTypeDescriptor(dex_file->GetTypeId(type_idx));
496}
497
498inline mirror::ClassLoader* ArtMethod::GetClassLoader() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700499 DCHECK(!IsProxyMethod());
500 return GetDeclaringClass()->GetClassLoader();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700501}
502
503inline mirror::DexCache* ArtMethod::GetDexCache() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700504 DCHECK(!IsProxyMethod());
505 return GetDeclaringClass()->GetDexCache();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700506}
507
Ian Rogers03b6eaf2014-10-28 09:34:57 -0700508inline bool ArtMethod::IsProxyMethod() {
509 return GetDeclaringClass()->IsProxyClass();
510}
511
Mathieu Chartiere401d142015-04-22 13:56:20 -0700512inline ArtMethod* ArtMethod::GetInterfaceMethodIfProxy(size_t pointer_size) {
Ian Rogers03b6eaf2014-10-28 09:34:57 -0700513 if (LIKELY(!IsProxyMethod())) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700514 return this;
515 }
Ian Rogers03b6eaf2014-10-28 09:34:57 -0700516 mirror::Class* klass = GetDeclaringClass();
Vladimir Marko05792b92015-08-03 11:56:49 +0100517 ArtMethod* interface_method = mirror::DexCache::GetElementPtrSize(
518 GetDexCacheResolvedMethods(pointer_size),
519 GetDexMethodIndex(),
520 pointer_size);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700521 DCHECK(interface_method != nullptr);
522 DCHECK_EQ(interface_method,
523 Runtime::Current()->GetClassLinker()->FindMethodForProxy(klass, this));
524 return interface_method;
525}
526
Vladimir Marko05792b92015-08-03 11:56:49 +0100527inline void ArtMethod::SetDexCacheResolvedMethods(ArtMethod** new_dex_cache_methods,
528 size_t ptr_size) {
529 SetNativePointer(DexCacheResolvedMethodsOffset(ptr_size), new_dex_cache_methods, ptr_size);
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700530}
531
Vladimir Marko05792b92015-08-03 11:56:49 +0100532inline void ArtMethod::SetDexCacheResolvedTypes(GcRoot<mirror::Class>* new_dex_cache_types,
533 size_t ptr_size) {
534 SetNativePointer(DexCacheResolvedTypesOffset(ptr_size), new_dex_cache_types, ptr_size);
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700535}
536
Vladimir Marko05792b92015-08-03 11:56:49 +0100537inline mirror::Class* ArtMethod::GetReturnType(bool resolve, size_t ptr_size) {
Ian Rogersded66a02014-10-28 18:12:55 -0700538 DCHECK(!IsProxyMethod());
539 const DexFile* dex_file = GetDexFile();
540 const DexFile::MethodId& method_id = dex_file->GetMethodId(GetDexMethodIndex());
541 const DexFile::ProtoId& proto_id = dex_file->GetMethodPrototype(method_id);
542 uint16_t return_type_idx = proto_id.return_type_idx_;
Vladimir Marko05792b92015-08-03 11:56:49 +0100543 mirror::Class* type = GetDexCacheResolvedType(return_type_idx, ptr_size);
Ian Rogersded66a02014-10-28 18:12:55 -0700544 if (type == nullptr && resolve) {
545 type = Runtime::Current()->GetClassLinker()->ResolveType(return_type_idx, this);
546 CHECK(type != nullptr || Thread::Current()->IsExceptionPending());
547 }
548 return type;
549}
550
Mathieu Chartiere401d142015-04-22 13:56:20 -0700551template<typename RootVisitorType>
Mathieu Chartier1147b9b2015-09-14 18:50:08 -0700552void ArtMethod::VisitRoots(RootVisitorType& visitor, size_t pointer_size) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100553 ArtMethod* interface_method = nullptr;
554 mirror::Class* klass = declaring_class_.Read();
555 if (UNLIKELY(klass != nullptr && klass->IsProxyClass())) {
556 // For normal methods, dex cache shortcuts will be visited through the declaring class.
557 // However, for proxies we need to keep the interface method alive, so we visit its roots.
Vladimir Marko05792b92015-08-03 11:56:49 +0100558 interface_method = mirror::DexCache::GetElementPtrSize(
559 GetDexCacheResolvedMethods(pointer_size),
560 GetDexMethodIndex(),
561 pointer_size);
562 DCHECK(interface_method != nullptr);
563 DCHECK_EQ(interface_method,
564 Runtime::Current()->GetClassLinker()->FindMethodForProxy(klass, this));
Mathieu Chartier1147b9b2015-09-14 18:50:08 -0700565 interface_method->VisitRoots(visitor, pointer_size);
Vladimir Marko05792b92015-08-03 11:56:49 +0100566 }
567
Mathieu Chartiere3b034a2015-05-31 14:29:23 -0700568 visitor.VisitRootIfNonNull(declaring_class_.AddressWithoutBarrier());
Mathieu Chartier1147b9b2015-09-14 18:50:08 -0700569 ProfilingInfo* profiling_info = GetProfilingInfo(pointer_size);
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100570 if (hotness_count_ != 0 && !IsNative() && profiling_info != nullptr) {
571 profiling_info->VisitRoots(visitor);
572 }
Mathieu Chartier2d721012014-11-10 11:08:06 -0800573}
574
Mathieu Chartiere401d142015-04-22 13:56:20 -0700575inline void ArtMethod::CopyFrom(const ArtMethod* src, size_t image_pointer_size) {
576 memcpy(reinterpret_cast<void*>(this), reinterpret_cast<const void*>(src),
Vladimir Marko14632852015-08-17 12:07:23 +0100577 Size(image_pointer_size));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700578 declaring_class_ = GcRoot<mirror::Class>(const_cast<ArtMethod*>(src)->GetDeclaringClass());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700579}
580
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800581} // namespace art
582
Mathieu Chartiere401d142015-04-22 13:56:20 -0700583#endif // ART_RUNTIME_ART_METHOD_INL_H_