blob: b2416675f0de7e31ed8f0db540d8ee1303559759 [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"
23#include "mirror/method_type.h"
24#include "mirror/var_handle.h"
25
26namespace art {
27
28bool VarHandleInvokeAccessor(Thread* self,
29 ShadowFrame& shadow_frame,
30 Handle<mirror::VarHandle> var_handle,
31 Handle<mirror::MethodType> callsite_type,
32 const mirror::VarHandle::AccessMode access_mode,
33 const InstructionOperands* const operands,
34 JValue* result) {
35 if (var_handle.IsNull()) {
36 ThrowNullPointerExceptionFromDexPC();
37 return false;
38 }
39
40 if (!var_handle->IsAccessModeSupported(access_mode)) {
41 ThrowUnsupportedOperationException();
42 return false;
43 }
44
45 if (!var_handle->IsMethodTypeCompatible(access_mode, callsite_type.Get())) {
46 ThrowWrongMethodTypeException(var_handle->GetMethodTypeForAccessMode(self, access_mode),
47 callsite_type.Get());
48 return false;
49 }
50
51 StackHandleScope<1> hs(self);
52
53 // TODO(oth): GetMethodTypeForAccessMode() allocates a MethodType()
54 // which is only required if we need to convert argument and/or
55 // return types.
56 Handle<mirror::MethodType> accessor_type(hs.NewHandle(
57 var_handle->GetMethodTypeForAccessMode(self, access_mode)));
58 const size_t num_vregs = accessor_type->NumberOfVRegs();
59 const int num_params = accessor_type->GetPTypes()->GetLength();
60 ShadowFrameAllocaUniquePtr accessor_frame =
61 CREATE_SHADOW_FRAME(num_vregs, nullptr, shadow_frame.GetMethod(), shadow_frame.GetDexPC());
62
63 ShadowFrameGetter getter(shadow_frame, operands);
64 static const uint32_t kFirstDestinationReg = 0;
65 ShadowFrameSetter setter(accessor_frame.get(), kFirstDestinationReg);
66 if (!PerformConversions(self, callsite_type, accessor_type, &getter, &setter, num_params)) {
67 return false;
68 }
69 RangeInstructionOperands accessor_operands(kFirstDestinationReg,
70 kFirstDestinationReg + num_vregs);
71 if (!var_handle->Access(access_mode, accessor_frame.get(), &accessor_operands, result)) {
72 return false;
73 }
74 return ConvertReturnValue(callsite_type, accessor_type, result);
75}
76
77} // namespace art