blob: 73b4ddaf3af6b244be17cae8596714453903462f [file] [log] [blame]
Orion Hodson537a4fe2018-05-15 13:57:58 +01001/*
2 * Copyright (C) 2018 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#include "var_handles.h"
18
19#include "common_throws.h"
20#include "dex/dex_instruction.h"
21#include "handle.h"
22#include "method_handles-inl.h"
Vladimir Marko5aead702019-03-27 11:00:36 +000023#include "mirror/method_type-inl.h"
Orion Hodson537a4fe2018-05-15 13:57:58 +010024#include "mirror/var_handle.h"
25
26namespace art {
27
Orion Hodsoncc052472018-05-16 17:12:44 +010028namespace {
Orion Hodson537a4fe2018-05-15 13:57:58 +010029
Orion Hodsoncc052472018-05-16 17:12:44 +010030bool VarHandleInvokeAccessorWithConversions(Thread* self,
31 ShadowFrame& shadow_frame,
32 Handle<mirror::VarHandle> var_handle,
33 Handle<mirror::MethodType> callsite_type,
34 const mirror::VarHandle::AccessMode access_mode,
35 const InstructionOperands* const operands,
36 JValue* result)
37 REQUIRES_SHARED(Locks::mutator_lock_) {
Orion Hodson537a4fe2018-05-15 13:57:58 +010038 StackHandleScope<1> hs(self);
Orion Hodson537a4fe2018-05-15 13:57:58 +010039 Handle<mirror::MethodType> accessor_type(hs.NewHandle(
40 var_handle->GetMethodTypeForAccessMode(self, access_mode)));
41 const size_t num_vregs = accessor_type->NumberOfVRegs();
42 const int num_params = accessor_type->GetPTypes()->GetLength();
43 ShadowFrameAllocaUniquePtr accessor_frame =
44 CREATE_SHADOW_FRAME(num_vregs, nullptr, shadow_frame.GetMethod(), shadow_frame.GetDexPC());
Orion Hodson537a4fe2018-05-15 13:57:58 +010045 ShadowFrameGetter getter(shadow_frame, operands);
46 static const uint32_t kFirstDestinationReg = 0;
47 ShadowFrameSetter setter(accessor_frame.get(), kFirstDestinationReg);
48 if (!PerformConversions(self, callsite_type, accessor_type, &getter, &setter, num_params)) {
49 return false;
50 }
51 RangeInstructionOperands accessor_operands(kFirstDestinationReg,
52 kFirstDestinationReg + num_vregs);
53 if (!var_handle->Access(access_mode, accessor_frame.get(), &accessor_operands, result)) {
54 return false;
55 }
56 return ConvertReturnValue(callsite_type, accessor_type, result);
57}
58
Orion Hodsoncc052472018-05-16 17:12:44 +010059} // namespace
60
61bool VarHandleInvokeAccessor(Thread* self,
62 ShadowFrame& shadow_frame,
63 Handle<mirror::VarHandle> var_handle,
64 Handle<mirror::MethodType> callsite_type,
65 const mirror::VarHandle::AccessMode access_mode,
66 const InstructionOperands* const operands,
67 JValue* result) {
68 if (var_handle.IsNull()) {
69 ThrowNullPointerExceptionFromDexPC();
70 return false;
71 }
72
73 if (!var_handle->IsAccessModeSupported(access_mode)) {
74 ThrowUnsupportedOperationException();
75 return false;
76 }
77
78 mirror::VarHandle::MatchKind match_kind =
79 var_handle->GetMethodTypeMatchForAccessMode(access_mode, callsite_type.Get());
80 if (LIKELY(match_kind == mirror::VarHandle::MatchKind::kExact)) {
81 return var_handle->Access(access_mode, &shadow_frame, operands, result);
82 } else if (match_kind == mirror::VarHandle::MatchKind::kWithConversions) {
83 return VarHandleInvokeAccessorWithConversions(self,
84 shadow_frame,
85 var_handle,
86 callsite_type,
87 access_mode,
88 operands,
89 result);
90 } else {
91 DCHECK_EQ(match_kind, mirror::VarHandle::MatchKind::kNone);
Orion Hodson3f383462018-05-17 14:03:39 +010092 ThrowWrongMethodTypeException(var_handle->PrettyDescriptorForAccessMode(access_mode),
93 callsite_type->PrettyDescriptor());
Orion Hodsoncc052472018-05-16 17:12:44 +010094 return false;
95 }
96}
97
Orion Hodson537a4fe2018-05-15 13:57:58 +010098} // namespace art