Narayan Kamath | afa4827 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
| 17 | #ifndef ART_RUNTIME_MIRROR_METHOD_HANDLE_IMPL_H_ |
| 18 | #define ART_RUNTIME_MIRROR_METHOD_HANDLE_IMPL_H_ |
| 19 | |
| 20 | #include "class.h" |
| 21 | #include "gc_root.h" |
| 22 | #include "object.h" |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 23 | #include "method_handles.h" |
Narayan Kamath | afa4827 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 24 | #include "method_type.h" |
| 25 | |
| 26 | namespace art { |
| 27 | |
| 28 | struct MethodHandleImplOffsets; |
| 29 | |
| 30 | namespace mirror { |
| 31 | |
| 32 | // C++ mirror of java.lang.invoke.MethodHandle |
| 33 | class MANAGED MethodHandle : public Object { |
| 34 | public: |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 35 | // Defines the behaviour of a given method handle. The behaviour |
| 36 | // of a handle of a given kind is identical to the dex bytecode behaviour |
| 37 | // of the equivalent instruction. |
| 38 | // |
| 39 | // NOTE: These must be kept in sync with the constants defined in |
| 40 | // java.lang.invoke.MethodHandle. |
| 41 | enum Kind { |
| 42 | kInvokeVirtual = 0, |
| 43 | kInvokeSuper, |
| 44 | kInvokeDirect, |
| 45 | kInvokeStatic, |
| 46 | kInvokeInterface, |
| 47 | kInvokeTransform, |
| 48 | kInvokeCallSiteTransform, |
| 49 | kInstanceGet, |
| 50 | kInstancePut, |
| 51 | kStaticGet, |
| 52 | kStaticPut, |
| 53 | kLastValidKind = kStaticPut, |
| 54 | kFirstAccessorKind = kInstanceGet, |
| 55 | kLastAccessorKind = kStaticPut, |
| 56 | kLastInvokeKind = kInvokeCallSiteTransform |
| 57 | }; |
| 58 | |
| 59 | Kind GetHandleKind() REQUIRES_SHARED(Locks::mutator_lock_) { |
| 60 | const int32_t handle_kind = GetField32(OFFSET_OF_OBJECT_MEMBER(MethodHandle, handle_kind_)); |
| 61 | DCHECK(handle_kind >= 0 && |
| 62 | handle_kind <= static_cast<int32_t>(Kind::kLastValidKind)); |
| 63 | return static_cast<Kind>(handle_kind); |
| 64 | } |
| 65 | |
Narayan Kamath | afa4827 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 66 | mirror::MethodType* GetMethodType() REQUIRES_SHARED(Locks::mutator_lock_) { |
| 67 | return GetFieldObject<mirror::MethodType>(OFFSET_OF_OBJECT_MEMBER(MethodHandle, method_type_)); |
| 68 | } |
| 69 | |
Narayan Kamath | 0a8485e | 2016-11-02 18:47:11 +0000 | [diff] [blame] | 70 | mirror::MethodType* GetNominalType() REQUIRES_SHARED(Locks::mutator_lock_) { |
| 71 | return GetFieldObject<mirror::MethodType>(OFFSET_OF_OBJECT_MEMBER(MethodHandle, nominal_type_)); |
| 72 | } |
| 73 | |
Orion Hodson | 3d617ac | 2016-10-19 14:00:46 +0100 | [diff] [blame] | 74 | ArtField* GetTargetField() REQUIRES_SHARED(Locks::mutator_lock_) { |
| 75 | return reinterpret_cast<ArtField*>( |
| 76 | GetField64(OFFSET_OF_OBJECT_MEMBER(MethodHandle, art_field_or_method_))); |
| 77 | } |
| 78 | |
Narayan Kamath | afa4827 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 79 | ArtMethod* GetTargetMethod() REQUIRES_SHARED(Locks::mutator_lock_) { |
| 80 | return reinterpret_cast<ArtMethod*>( |
| 81 | GetField64(OFFSET_OF_OBJECT_MEMBER(MethodHandle, art_field_or_method_))); |
| 82 | } |
| 83 | |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 84 | static mirror::Class* StaticClass() REQUIRES_SHARED(Locks::mutator_lock_); |
| 85 | |
Narayan Kamath | afa4827 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 86 | private: |
Narayan Kamath | c5889ce | 2017-01-19 20:42:23 +0000 | [diff] [blame^] | 87 | // NOTE: cached_spread_invoker_ isn't used by the runtime. |
| 88 | HeapReference<mirror::MethodHandle> cached_spread_invoker_; |
Narayan Kamath | 0a8485e | 2016-11-02 18:47:11 +0000 | [diff] [blame] | 89 | HeapReference<mirror::MethodType> nominal_type_; |
Narayan Kamath | afa4827 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 90 | HeapReference<mirror::MethodType> method_type_; |
Narayan Kamath | afa4827 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 91 | uint32_t handle_kind_; |
Narayan Kamath | c5889ce | 2017-01-19 20:42:23 +0000 | [diff] [blame^] | 92 | uint64_t art_field_or_method_; |
Narayan Kamath | afa4827 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 93 | |
| 94 | private: |
Narayan Kamath | 0a8485e | 2016-11-02 18:47:11 +0000 | [diff] [blame] | 95 | static MemberOffset NominalTypeOffset() { |
| 96 | return MemberOffset(OFFSETOF_MEMBER(MethodHandle, nominal_type_)); |
Narayan Kamath | afa4827 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 97 | } |
| 98 | static MemberOffset MethodTypeOffset() { |
| 99 | return MemberOffset(OFFSETOF_MEMBER(MethodHandle, method_type_)); |
| 100 | } |
| 101 | static MemberOffset ArtFieldOrMethodOffset() { |
| 102 | return MemberOffset(OFFSETOF_MEMBER(MethodHandle, art_field_or_method_)); |
| 103 | } |
| 104 | static MemberOffset HandleKindOffset() { |
| 105 | return MemberOffset(OFFSETOF_MEMBER(MethodHandle, handle_kind_)); |
| 106 | } |
| 107 | |
| 108 | friend struct art::MethodHandleImplOffsets; // for verifying offset information |
| 109 | DISALLOW_IMPLICIT_CONSTRUCTORS(MethodHandle); |
| 110 | }; |
| 111 | |
| 112 | // C++ mirror of java.lang.invoke.MethodHandleImpl |
| 113 | class MANAGED MethodHandleImpl : public MethodHandle { |
| 114 | public: |
| 115 | static mirror::Class* StaticClass() REQUIRES_SHARED(Locks::mutator_lock_) { |
| 116 | return static_class_.Read(); |
| 117 | } |
| 118 | |
| 119 | static void SetClass(Class* klass) REQUIRES_SHARED(Locks::mutator_lock_); |
| 120 | static void ResetClass() REQUIRES_SHARED(Locks::mutator_lock_); |
| 121 | static void VisitRoots(RootVisitor* visitor) REQUIRES_SHARED(Locks::mutator_lock_); |
| 122 | |
| 123 | private: |
| 124 | static GcRoot<mirror::Class> static_class_; // java.lang.invoke.MethodHandleImpl.class |
| 125 | |
| 126 | friend struct art::MethodHandleImplOffsets; // for verifying offset information |
| 127 | DISALLOW_IMPLICIT_CONSTRUCTORS(MethodHandleImpl); |
| 128 | }; |
| 129 | |
| 130 | } // namespace mirror |
| 131 | } // namespace art |
| 132 | |
| 133 | #endif // ART_RUNTIME_MIRROR_METHOD_HANDLE_IMPL_H_ |