blob: b7ca188d78c0b4c4024b6d2d34e3a961a11a5eb2 [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
Brian Carlstromea46f952013-07-30 01:26:50 -070017#ifndef ART_RUNTIME_MIRROR_ART_METHOD_INL_H_
18#define ART_RUNTIME_MIRROR_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
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080022#include "dex_file.h"
Ian Rogers7655f292013-07-29 11:07:13 -070023#include "entrypoints/entrypoint_utils.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070024#include "object_array.h"
Vladimir Marko96c6ab92014-04-08 14:00:50 +010025#include "oat.h"
Vladimir Marko7624d252014-05-02 14:40:15 +010026#include "quick/quick_method_frame_info.h"
27#include "runtime-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028
29namespace art {
30namespace mirror {
31
Ian Rogersef7d42f2014-01-06 12:55:46 -080032inline Class* ArtMethod::GetDeclaringClass() {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070033 Class* result = GetFieldObject<Class>(OFFSET_OF_OBJECT_MEMBER(ArtMethod, declaring_class_));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034 DCHECK(result != NULL) << this;
35 DCHECK(result->IsIdxLoaded() || result->IsErroneous()) << this;
36 return result;
37}
38
Brian Carlstromea46f952013-07-30 01:26:50 -070039inline void ArtMethod::SetDeclaringClass(Class *new_declaring_class) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010040 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(ArtMethod, declaring_class_),
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070041 new_declaring_class);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080042}
43
Ian Rogersef7d42f2014-01-06 12:55:46 -080044inline uint32_t ArtMethod::GetAccessFlags() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080045 DCHECK(GetDeclaringClass()->IsIdxLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070046 return GetField32(OFFSET_OF_OBJECT_MEMBER(ArtMethod, access_flags_));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080047}
48
Ian Rogersef7d42f2014-01-06 12:55:46 -080049inline uint16_t ArtMethod::GetMethodIndex() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080050 DCHECK(GetDeclaringClass()->IsResolved() || GetDeclaringClass()->IsErroneous());
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070051 return GetField32(OFFSET_OF_OBJECT_MEMBER(ArtMethod, method_index_));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080052}
53
Ian Rogersef7d42f2014-01-06 12:55:46 -080054inline uint32_t ArtMethod::GetDexMethodIndex() {
Dragos Sbirlea90af14d2013-08-15 17:50:16 -070055#ifdef ART_SEA_IR_MODE
56 // TODO: Re-add this check for (PORTABLE + SMALL + ) SEA IR when PORTABLE IS fixed!
57 // DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
58#else
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080059 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Dragos Sbirlea90af14d2013-08-15 17:50:16 -070060#endif
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070061 return GetField32(OFFSET_OF_OBJECT_MEMBER(ArtMethod, dex_method_index_));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080062}
63
Ian Rogersef7d42f2014-01-06 12:55:46 -080064inline ObjectArray<String>* ArtMethod::GetDexCacheStrings() {
Ian Rogers700a4022014-05-19 16:49:03 -070065 return GetFieldObject<ObjectArray<String>>(
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070066 OFFSET_OF_OBJECT_MEMBER(ArtMethod, dex_cache_strings_));
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070067}
68
Ian Rogersef7d42f2014-01-06 12:55:46 -080069inline ObjectArray<ArtMethod>* ArtMethod::GetDexCacheResolvedMethods() {
Ian Rogers700a4022014-05-19 16:49:03 -070070 return GetFieldObject<ObjectArray<ArtMethod>>(
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070071 OFFSET_OF_OBJECT_MEMBER(ArtMethod, dex_cache_resolved_methods_));
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070072}
73
Ian Rogersef7d42f2014-01-06 12:55:46 -080074inline ObjectArray<Class>* ArtMethod::GetDexCacheResolvedTypes() {
Ian Rogers700a4022014-05-19 16:49:03 -070075 return GetFieldObject<ObjectArray<Class>>(
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070076 OFFSET_OF_OBJECT_MEMBER(ArtMethod, dex_cache_resolved_types_));
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070077}
78
Ian Rogersef7d42f2014-01-06 12:55:46 -080079inline uint32_t ArtMethod::GetCodeSize() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080080 DCHECK(!IsRuntimeMethod() && !IsProxyMethod()) << PrettyMethod(this);
Vladimir Marko8a630572014-04-09 18:45:35 +010081 const void* code = EntryPointToCodePointer(GetEntryPointFromQuickCompiledCode());
82 if (code == nullptr) {
83 return 0u;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080084 }
Vladimir Marko7624d252014-05-02 14:40:15 +010085 return reinterpret_cast<const OatQuickMethodHeader*>(code)[-1].code_size_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080086}
87
Brian Carlstromea46f952013-07-30 01:26:50 -070088inline bool ArtMethod::CheckIncompatibleClassChange(InvokeType type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080089 switch (type) {
90 case kStatic:
91 return !IsStatic();
92 case kDirect:
93 return !IsDirect() || IsStatic();
94 case kVirtual: {
95 Class* methods_class = GetDeclaringClass();
96 return IsDirect() || (methods_class->IsInterface() && !IsMiranda());
97 }
98 case kSuper:
99 return false; // TODO: appropriate checks for call to super class.
100 case kInterface: {
101 Class* methods_class = GetDeclaringClass();
102 return IsDirect() || !(methods_class->IsInterface() || methods_class->IsObjectClass());
103 }
104 default:
105 LOG(FATAL) << "Unreachable - invocation type: " << type;
106 return true;
107 }
108}
109
Ian Rogersef7d42f2014-01-06 12:55:46 -0800110inline void ArtMethod::AssertPcIsWithinQuickCode(uintptr_t pc) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800111 if (!kIsDebugBuild) {
112 return;
113 }
114 if (IsNative() || IsRuntimeMethod() || IsProxyMethod()) {
115 return;
116 }
Ian Rogers848871b2013-08-05 10:56:33 -0700117 if (pc == GetQuickInstrumentationExitPc()) {
Jeff Hao65d15d92013-07-16 16:39:33 -0700118 return;
119 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800120 const void* code = GetEntryPointFromQuickCompiledCode();
121 if (code == GetQuickToInterpreterBridge() || code == GetQuickInstrumentationEntryPoint()) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800122 return;
123 }
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700124 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Marko8a630572014-04-09 18:45:35 +0100125 if (code == GetQuickResolutionTrampoline(class_linker) ||
126 code == GetQuickToInterpreterBridgeTrampoline(class_linker)) {
Jeff Hao65d15d92013-07-16 16:39:33 -0700127 return;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800128 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800129 DCHECK(IsWithinQuickCode(pc))
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800130 << PrettyMethod(this)
131 << " pc=" << std::hex << pc
Jeff Hao65d15d92013-07-16 16:39:33 -0700132 << " code=" << code
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800133 << " size=" << GetCodeSize();
134}
135
Ian Rogersef7d42f2014-01-06 12:55:46 -0800136inline uint32_t ArtMethod::GetQuickOatCodeOffset() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800137 DCHECK(!Runtime::Current()->IsStarted());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800138 return PointerToLowMemUInt32(GetEntryPointFromQuickCompiledCode());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800139}
140
Ian Rogersef7d42f2014-01-06 12:55:46 -0800141inline uint32_t ArtMethod::GetPortableOatCodeOffset() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800142 DCHECK(!Runtime::Current()->IsStarted());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800143 return PointerToLowMemUInt32(GetEntryPointFromPortableCompiledCode());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800144}
145
Ian Rogersef7d42f2014-01-06 12:55:46 -0800146inline void ArtMethod::SetQuickOatCodeOffset(uint32_t code_offset) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800147 DCHECK(!Runtime::Current()->IsStarted());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800148 SetEntryPointFromQuickCompiledCode(reinterpret_cast<void*>(code_offset));
149}
150
151inline void ArtMethod::SetPortableOatCodeOffset(uint32_t code_offset) {
152 DCHECK(!Runtime::Current()->IsStarted());
153 SetEntryPointFromPortableCompiledCode(reinterpret_cast<void*>(code_offset));
154}
155
Brian Carlstromea46f952013-07-30 01:26:50 -0700156inline void ArtMethod::SetOatNativeGcMapOffset(uint32_t gc_map_offset) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800157 DCHECK(!Runtime::Current()->IsStarted());
158 SetNativeGcMap(reinterpret_cast<uint8_t*>(gc_map_offset));
159}
160
Ian Rogersef7d42f2014-01-06 12:55:46 -0800161inline uint32_t ArtMethod::GetOatNativeGcMapOffset() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800162 DCHECK(!Runtime::Current()->IsStarted());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800163 return PointerToLowMemUInt32(GetNativeGcMap());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800164}
165
Ian Rogersef7d42f2014-01-06 12:55:46 -0800166inline bool ArtMethod::IsRuntimeMethod() {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700167 return GetDexMethodIndex() == DexFile::kDexNoIndex;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800168}
169
Ian Rogersef7d42f2014-01-06 12:55:46 -0800170inline bool ArtMethod::IsCalleeSaveMethod() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800171 if (!IsRuntimeMethod()) {
172 return false;
173 }
174 Runtime* runtime = Runtime::Current();
175 bool result = false;
176 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
177 if (this == runtime->GetCalleeSaveMethod(Runtime::CalleeSaveType(i))) {
178 result = true;
179 break;
180 }
181 }
182 return result;
183}
184
Ian Rogersef7d42f2014-01-06 12:55:46 -0800185inline bool ArtMethod::IsResolutionMethod() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800186 bool result = this == Runtime::Current()->GetResolutionMethod();
187 // Check that if we do think it is phony it looks like the resolution method.
188 DCHECK(!result || IsRuntimeMethod());
189 return result;
190}
Jeff Hao88474b42013-10-23 16:24:40 -0700191
Ian Rogersef7d42f2014-01-06 12:55:46 -0800192inline bool ArtMethod::IsImtConflictMethod() {
Jeff Hao88474b42013-10-23 16:24:40 -0700193 bool result = this == Runtime::Current()->GetImtConflictMethod();
194 // Check that if we do think it is phony it looks like the imt conflict method.
195 DCHECK(!result || IsRuntimeMethod());
196 return result;
197}
Mathieu Chartier4e305412014-02-19 10:54:44 -0800198
199template<VerifyObjectFlags kVerifyFlags>
200inline void ArtMethod::SetNativeMethod(const void* native_method) {
201 SetFieldPtr<false, true, kVerifyFlags>(
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700202 OFFSET_OF_OBJECT_MEMBER(ArtMethod, entry_point_from_jni_), native_method);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800203}
204
Vladimir Marko7624d252014-05-02 14:40:15 +0100205inline QuickMethodFrameInfo ArtMethod::GetQuickFrameInfo() {
206 if (UNLIKELY(IsPortableCompiled())) {
207 // Portable compiled dex bytecode or jni stub.
208 return QuickMethodFrameInfo(kStackAlignment, 0u, 0u);
209 }
210 Runtime* runtime = Runtime::Current();
211 if (UNLIKELY(IsAbstract()) || UNLIKELY(IsProxyMethod())) {
212 return runtime->GetCalleeSaveMethodFrameInfo(Runtime::kRefsAndArgs);
213 }
214 if (UNLIKELY(IsRuntimeMethod())) {
215 return runtime->GetRuntimeMethodFrameInfo(this);
216 }
217
218 const void* entry_point = runtime->GetInstrumentation()->GetQuickCodeFor(this);
219 // On failure, instead of nullptr we get the quick-generic-jni-trampoline for native method
220 // indicating the generic JNI, or the quick-to-interpreter-bridge (but not the trampoline)
221 // for non-native methods. And we really shouldn't see a failure for non-native methods here.
222 DCHECK(entry_point != GetQuickToInterpreterBridgeTrampoline(runtime->GetClassLinker()));
223 CHECK(entry_point != GetQuickToInterpreterBridge());
224
Dmitry Petrochenkof0972a42014-05-16 17:43:39 +0700225 if (UNLIKELY(entry_point == runtime->GetClassLinker()->GetQuickGenericJniTrampoline())) {
Vladimir Marko7624d252014-05-02 14:40:15 +0100226 // Generic JNI frame.
227 DCHECK(IsNative());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700228 uint32_t handle_refs = MethodHelper(this).GetNumberOfReferenceArgsWithoutReceiver() + 1;
229 size_t scope_size = HandleScope::GetAlignedHandleScopeSize(handle_refs);
Vladimir Marko7624d252014-05-02 14:40:15 +0100230 QuickMethodFrameInfo callee_info = runtime->GetCalleeSaveMethodFrameInfo(Runtime::kRefsAndArgs);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700231 return QuickMethodFrameInfo(callee_info.FrameSizeInBytes() + scope_size,
Vladimir Marko7624d252014-05-02 14:40:15 +0100232 callee_info.CoreSpillMask(), callee_info.FpSpillMask());
233 }
234
235 const void* code_pointer = EntryPointToCodePointer(entry_point);
236 return reinterpret_cast<const OatQuickMethodHeader*>(code_pointer)[-1].frame_info_;
237}
238
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800239} // namespace mirror
240} // namespace art
241
Brian Carlstromea46f952013-07-30 01:26:50 -0700242#endif // ART_RUNTIME_MIRROR_ART_METHOD_INL_H_